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> |
Arjan van de Ven | 7931805 | 2009-01-04 05:32:28 -0800 | [diff] [blame] | 45 | #include <linux/async.h> |
Mark Lord | e18086d | 2009-03-19 13:32:21 -0400 | [diff] [blame] | 46 | #include <linux/log2.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 47 | #include <linux/slab.h> |
George Spelvin | 428ac5f | 2014-08-06 16:09:27 -0700 | [diff] [blame] | 48 | #include <linux/glob.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | #include <scsi/scsi.h> |
Jeff Garzik | 193515d | 2005-11-07 00:59:37 -0500 | [diff] [blame] | 50 | #include <scsi/scsi_cmnd.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | #include <scsi/scsi_host.h> |
| 52 | #include <linux/libata.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | #include <asm/byteorder.h> |
Hannes Reinecke | fe5af0c | 2016-04-25 12:45:48 +0200 | [diff] [blame] | 54 | #include <asm/unaligned.h> |
Tejun Heo | 140b5e59 | 2007-12-12 12:21:52 +0900 | [diff] [blame] | 55 | #include <linux/cdrom.h> |
Akinobu Mita | 9990b6f | 2010-04-12 21:11:41 +0900 | [diff] [blame] | 56 | #include <linux/ratelimit.h> |
Stephan Linz | eb25cb9 | 2016-06-10 07:59:56 +0200 | [diff] [blame] | 57 | #include <linux/leds.h> |
Lin Ming | 9ee4f39 | 2011-12-05 09:20:28 +0800 | [diff] [blame] | 58 | #include <linux/pm_runtime.h> |
Brian Norris | b7db04d | 2012-11-02 12:29:32 -0700 | [diff] [blame] | 59 | #include <linux/platform_device.h> |
Bartlomiej Zolnierkiewicz | bbf5a09 | 2020-03-26 16:58:01 +0100 | [diff] [blame] | 60 | #include <asm/setup.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | |
Hannes Reinecke | 255c03d | 2015-03-27 16:46:38 +0100 | [diff] [blame] | 62 | #define CREATE_TRACE_POINTS |
| 63 | #include <trace/events/libata.h> |
| 64 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | #include "libata.h" |
Gwendal Grignou | d902747 | 2010-05-25 12:31:38 -0700 | [diff] [blame] | 66 | #include "libata-transport.h" |
Jeff Garzik | fda0efc | 2007-01-31 07:43:15 -0500 | [diff] [blame] | 67 | |
Tejun Heo | 029cfd6 | 2008-03-25 12:22:49 +0900 | [diff] [blame] | 68 | const struct ata_port_operations ata_base_port_ops = { |
Tejun Heo | 0aa1113 | 2008-04-07 22:47:18 +0900 | [diff] [blame] | 69 | .prereset = ata_std_prereset, |
Tejun Heo | 203c75b | 2008-04-07 22:47:18 +0900 | [diff] [blame] | 70 | .postreset = ata_std_postreset, |
Tejun Heo | a1efdab | 2008-03-25 12:22:50 +0900 | [diff] [blame] | 71 | .error_handler = ata_std_error_handler, |
Dan Williams | e4a9c37 | 2012-06-21 23:25:27 -0700 | [diff] [blame] | 72 | .sched_eh = ata_std_sched_eh, |
| 73 | .end_eh = ata_std_end_eh, |
Tejun Heo | 029cfd6 | 2008-03-25 12:22:49 +0900 | [diff] [blame] | 74 | }; |
| 75 | |
| 76 | const struct ata_port_operations sata_port_ops = { |
| 77 | .inherits = &ata_base_port_ops, |
| 78 | |
| 79 | .qc_defer = ata_std_qc_defer, |
Tejun Heo | 57c9efd | 2008-04-07 22:47:19 +0900 | [diff] [blame] | 80 | .hardreset = sata_std_hardreset, |
Tejun Heo | 029cfd6 | 2008-03-25 12:22:49 +0900 | [diff] [blame] | 81 | }; |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 82 | EXPORT_SYMBOL_GPL(sata_port_ops); |
Tejun Heo | 029cfd6 | 2008-03-25 12:22:49 +0900 | [diff] [blame] | 83 | |
Tejun Heo | 3373efd | 2006-05-15 20:57:53 +0900 | [diff] [blame] | 84 | static unsigned int ata_dev_init_params(struct ata_device *dev, |
| 85 | u16 heads, u16 sectors); |
| 86 | static unsigned int ata_dev_set_xfermode(struct ata_device *dev); |
| 87 | static void ata_dev_xfermask(struct ata_device *dev); |
Tejun Heo | 75683fe | 2007-07-05 13:31:27 +0900 | [diff] [blame] | 88 | static unsigned long ata_dev_blacklisted(const struct ata_device *dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | |
Tero Roponen | a78f57a | 2012-04-22 11:38:00 +0300 | [diff] [blame] | 90 | atomic_t ata_print_id = ATOMIC_INIT(0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | |
Bartlomiej Zolnierkiewicz | bf89b0b | 2020-03-26 16:58:22 +0100 | [diff] [blame] | 92 | #ifdef CONFIG_ATA_FORCE |
Tejun Heo | 3326732 | 2008-02-13 09:15:09 +0900 | [diff] [blame] | 93 | struct ata_force_param { |
| 94 | const char *name; |
Bartlomiej Zolnierkiewicz | 8ba5a45 | 2020-03-26 16:58:02 +0100 | [diff] [blame] | 95 | u8 cbl; |
| 96 | u8 spd_limit; |
Tejun Heo | 3326732 | 2008-02-13 09:15:09 +0900 | [diff] [blame] | 97 | unsigned long xfer_mask; |
| 98 | unsigned int horkage_on; |
| 99 | unsigned int horkage_off; |
Bartlomiej Zolnierkiewicz | 8ba5a45 | 2020-03-26 16:58:02 +0100 | [diff] [blame] | 100 | u16 lflags; |
Tejun Heo | 3326732 | 2008-02-13 09:15:09 +0900 | [diff] [blame] | 101 | }; |
| 102 | |
| 103 | struct ata_force_ent { |
| 104 | int port; |
| 105 | int device; |
| 106 | struct ata_force_param param; |
| 107 | }; |
| 108 | |
| 109 | static struct ata_force_ent *ata_force_tbl; |
| 110 | static int ata_force_tbl_size; |
| 111 | |
Bartlomiej Zolnierkiewicz | bbf5a09 | 2020-03-26 16:58:01 +0100 | [diff] [blame] | 112 | static char ata_force_param_buf[COMMAND_LINE_SIZE] __initdata; |
Tejun Heo | 7afb422 | 2008-03-09 20:21:53 +0900 | [diff] [blame] | 113 | /* param_buf is thrown away after initialization, disallow read */ |
| 114 | module_param_string(force, ata_force_param_buf, sizeof(ata_force_param_buf), 0); |
Mauro Carvalho Chehab | 8c27ceff3 | 2016-10-18 10:12:27 -0200 | [diff] [blame] | 115 | MODULE_PARM_DESC(force, "Force ATA configurations including cable type, link speed and transfer mode (see Documentation/admin-guide/kernel-parameters.rst for details)"); |
Bartlomiej Zolnierkiewicz | bf89b0b | 2020-03-26 16:58:22 +0100 | [diff] [blame] | 116 | #endif |
Tejun Heo | 3326732 | 2008-02-13 09:15:09 +0900 | [diff] [blame] | 117 | |
Tejun Heo | 2486fa5 | 2008-07-31 07:52:40 +0900 | [diff] [blame] | 118 | static int atapi_enabled = 1; |
Jeff Garzik | 1623c81 | 2005-08-30 03:37:42 -0400 | [diff] [blame] | 119 | module_param(atapi_enabled, int, 0444); |
Evgeni Golov | ad5d8ea | 2009-06-22 09:46:20 +0200 | [diff] [blame] | 120 | 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] | 121 | |
Adrian Bunk | c5c61bd | 2008-02-25 02:07:25 +0200 | [diff] [blame] | 122 | static int atapi_dmadir = 0; |
Albert Lee | 95de719 | 2006-04-04 10:57:18 +0800 | [diff] [blame] | 123 | module_param(atapi_dmadir, int, 0444); |
Evgeni Golov | ad5d8ea | 2009-06-22 09:46:20 +0200 | [diff] [blame] | 124 | 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] | 125 | |
Mark Lord | baf4fdf | 2007-08-08 01:08:45 +0900 | [diff] [blame] | 126 | int atapi_passthru16 = 1; |
| 127 | module_param(atapi_passthru16, int, 0444); |
Evgeni Golov | ad5d8ea | 2009-06-22 09:46:20 +0200 | [diff] [blame] | 128 | 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] | 129 | |
Jeff Garzik | c3c013a | 2006-02-27 22:31:19 -0500 | [diff] [blame] | 130 | int libata_fua = 0; |
| 131 | module_param_named(fua, libata_fua, int, 0444); |
Evgeni Golov | ad5d8ea | 2009-06-22 09:46:20 +0200 | [diff] [blame] | 132 | MODULE_PARM_DESC(fua, "FUA support (0=off [default], 1=on)"); |
Jeff Garzik | c3c013a | 2006-02-27 22:31:19 -0500 | [diff] [blame] | 133 | |
Jeff Garzik | 2dcb407 | 2007-10-19 06:42:56 -0400 | [diff] [blame] | 134 | static int ata_ignore_hpa; |
Alan Cox | 1e99973 | 2007-04-11 00:23:13 +0100 | [diff] [blame] | 135 | module_param_named(ignore_hpa, ata_ignore_hpa, int, 0644); |
| 136 | MODULE_PARM_DESC(ignore_hpa, "Ignore HPA limit (0=keep BIOS limits, 1=ignore limits, using full disk)"); |
| 137 | |
Alan Cox | b3a7060 | 2007-10-02 12:38:26 -0400 | [diff] [blame] | 138 | static int libata_dma_mask = ATA_DMA_MASK_ATA|ATA_DMA_MASK_ATAPI|ATA_DMA_MASK_CFA; |
| 139 | module_param_named(dma, libata_dma_mask, int, 0444); |
| 140 | MODULE_PARM_DESC(dma, "DMA enable/disable (0x1==ATA, 0x2==ATAPI, 0x4==CF)"); |
| 141 | |
Tejun Heo | 87fbc5a | 2008-05-20 02:17:54 +0900 | [diff] [blame] | 142 | static int ata_probe_timeout; |
Andrew Morton | a8601e5 | 2006-06-25 01:36:52 -0700 | [diff] [blame] | 143 | module_param(ata_probe_timeout, int, 0444); |
| 144 | MODULE_PARM_DESC(ata_probe_timeout, "Set ATA probing timeout (seconds)"); |
| 145 | |
Jeff Garzik | 6ebe9d8 | 2007-10-05 16:28:36 -0400 | [diff] [blame] | 146 | int libata_noacpi = 0; |
Jeff Garzik | d7d0dad | 2007-03-28 01:57:37 -0400 | [diff] [blame] | 147 | module_param_named(noacpi, libata_noacpi, int, 0444); |
Evgeni Golov | ad5d8ea | 2009-06-22 09:46:20 +0200 | [diff] [blame] | 148 | 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] | 149 | |
Alan Cox | ae8d4ee | 2007-11-04 22:05:49 -0500 | [diff] [blame] | 150 | int libata_allow_tpm = 0; |
| 151 | module_param_named(allow_tpm, libata_allow_tpm, int, 0444); |
Evgeni Golov | ad5d8ea | 2009-06-22 09:46:20 +0200 | [diff] [blame] | 152 | 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] | 153 | |
Tejun Heo | e7ecd43 | 2010-05-19 15:38:58 +0200 | [diff] [blame] | 154 | static int atapi_an; |
| 155 | module_param(atapi_an, int, 0444); |
| 156 | MODULE_PARM_DESC(atapi_an, "Enable ATAPI AN media presence notification (0=0ff [default], 1=on)"); |
| 157 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | MODULE_AUTHOR("Jeff Garzik"); |
| 159 | MODULE_DESCRIPTION("Library module for ATA devices"); |
| 160 | MODULE_LICENSE("GPL"); |
| 161 | MODULE_VERSION(DRV_VERSION); |
| 162 | |
Edward Falk | 0baab86 | 2005-06-02 18:17:13 -0400 | [diff] [blame] | 163 | |
Tejun Heo | 9913ff8a | 2009-01-29 20:31:31 +0900 | [diff] [blame] | 164 | static bool ata_sstatus_online(u32 sstatus) |
| 165 | { |
| 166 | return (sstatus & 0xf) == 0x3; |
| 167 | } |
| 168 | |
Tejun Heo | 1eca436 | 2008-11-03 20:03:17 +0900 | [diff] [blame] | 169 | /** |
| 170 | * ata_link_next - link iteration helper |
| 171 | * @link: the previous link, NULL to start |
| 172 | * @ap: ATA port containing links to iterate |
| 173 | * @mode: iteration mode, one of ATA_LITER_* |
Tejun Heo | aadffb6 | 2008-07-31 17:02:41 +0900 | [diff] [blame] | 174 | * |
Tejun Heo | 1eca436 | 2008-11-03 20:03:17 +0900 | [diff] [blame] | 175 | * LOCKING: |
| 176 | * Host lock or EH context. |
| 177 | * |
| 178 | * RETURNS: |
| 179 | * Pointer to the next link. |
Tejun Heo | aadffb6 | 2008-07-31 17:02:41 +0900 | [diff] [blame] | 180 | */ |
Tejun Heo | 1eca436 | 2008-11-03 20:03:17 +0900 | [diff] [blame] | 181 | struct ata_link *ata_link_next(struct ata_link *link, struct ata_port *ap, |
| 182 | enum ata_link_iter_mode mode) |
Tejun Heo | aadffb6 | 2008-07-31 17:02:41 +0900 | [diff] [blame] | 183 | { |
Tejun Heo | 1eca436 | 2008-11-03 20:03:17 +0900 | [diff] [blame] | 184 | BUG_ON(mode != ATA_LITER_EDGE && |
| 185 | mode != ATA_LITER_PMP_FIRST && mode != ATA_LITER_HOST_FIRST); |
Tejun Heo | aadffb6 | 2008-07-31 17:02:41 +0900 | [diff] [blame] | 186 | |
Tejun Heo | 1eca436 | 2008-11-03 20:03:17 +0900 | [diff] [blame] | 187 | /* NULL link indicates start of iteration */ |
| 188 | if (!link) |
| 189 | switch (mode) { |
| 190 | case ATA_LITER_EDGE: |
| 191 | case ATA_LITER_PMP_FIRST: |
| 192 | if (sata_pmp_attached(ap)) |
| 193 | return ap->pmp_link; |
| 194 | /* fall through */ |
| 195 | case ATA_LITER_HOST_FIRST: |
| 196 | return &ap->link; |
| 197 | } |
| 198 | |
| 199 | /* we just iterated over the host link, what's next? */ |
| 200 | if (link == &ap->link) |
| 201 | switch (mode) { |
| 202 | case ATA_LITER_HOST_FIRST: |
| 203 | if (sata_pmp_attached(ap)) |
| 204 | return ap->pmp_link; |
| 205 | /* fall through */ |
| 206 | case ATA_LITER_PMP_FIRST: |
| 207 | if (unlikely(ap->slave_link)) |
Tejun Heo | b1c7291 | 2008-07-31 17:02:43 +0900 | [diff] [blame] | 208 | return ap->slave_link; |
Tejun Heo | 1eca436 | 2008-11-03 20:03:17 +0900 | [diff] [blame] | 209 | /* fall through */ |
| 210 | case ATA_LITER_EDGE: |
Tejun Heo | aadffb6 | 2008-07-31 17:02:41 +0900 | [diff] [blame] | 211 | return NULL; |
Tejun Heo | b1c7291 | 2008-07-31 17:02:43 +0900 | [diff] [blame] | 212 | } |
Tejun Heo | aadffb6 | 2008-07-31 17:02:41 +0900 | [diff] [blame] | 213 | |
Tejun Heo | b1c7291 | 2008-07-31 17:02:43 +0900 | [diff] [blame] | 214 | /* slave_link excludes PMP */ |
| 215 | if (unlikely(link == ap->slave_link)) |
| 216 | return NULL; |
| 217 | |
Tejun Heo | 1eca436 | 2008-11-03 20:03:17 +0900 | [diff] [blame] | 218 | /* we were over a PMP link */ |
Tejun Heo | aadffb6 | 2008-07-31 17:02:41 +0900 | [diff] [blame] | 219 | if (++link < ap->pmp_link + ap->nr_pmp_links) |
| 220 | return link; |
Tejun Heo | 1eca436 | 2008-11-03 20:03:17 +0900 | [diff] [blame] | 221 | |
| 222 | if (mode == ATA_LITER_PMP_FIRST) |
| 223 | return &ap->link; |
| 224 | |
Tejun Heo | aadffb6 | 2008-07-31 17:02:41 +0900 | [diff] [blame] | 225 | return NULL; |
| 226 | } |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 227 | EXPORT_SYMBOL_GPL(ata_link_next); |
Tejun Heo | aadffb6 | 2008-07-31 17:02:41 +0900 | [diff] [blame] | 228 | |
Edward Falk | 0baab86 | 2005-06-02 18:17:13 -0400 | [diff] [blame] | 229 | /** |
Tejun Heo | 1eca436 | 2008-11-03 20:03:17 +0900 | [diff] [blame] | 230 | * ata_dev_next - device iteration helper |
| 231 | * @dev: the previous device, NULL to start |
| 232 | * @link: ATA link containing devices to iterate |
| 233 | * @mode: iteration mode, one of ATA_DITER_* |
| 234 | * |
| 235 | * LOCKING: |
| 236 | * Host lock or EH context. |
| 237 | * |
| 238 | * RETURNS: |
| 239 | * Pointer to the next device. |
| 240 | */ |
| 241 | struct ata_device *ata_dev_next(struct ata_device *dev, struct ata_link *link, |
| 242 | enum ata_dev_iter_mode mode) |
| 243 | { |
| 244 | BUG_ON(mode != ATA_DITER_ENABLED && mode != ATA_DITER_ENABLED_REVERSE && |
| 245 | mode != ATA_DITER_ALL && mode != ATA_DITER_ALL_REVERSE); |
| 246 | |
| 247 | /* NULL dev indicates start of iteration */ |
| 248 | if (!dev) |
| 249 | switch (mode) { |
| 250 | case ATA_DITER_ENABLED: |
| 251 | case ATA_DITER_ALL: |
| 252 | dev = link->device; |
| 253 | goto check; |
| 254 | case ATA_DITER_ENABLED_REVERSE: |
| 255 | case ATA_DITER_ALL_REVERSE: |
| 256 | dev = link->device + ata_link_max_devices(link) - 1; |
| 257 | goto check; |
| 258 | } |
| 259 | |
| 260 | next: |
| 261 | /* move to the next one */ |
| 262 | switch (mode) { |
| 263 | case ATA_DITER_ENABLED: |
| 264 | case ATA_DITER_ALL: |
| 265 | if (++dev < link->device + ata_link_max_devices(link)) |
| 266 | goto check; |
| 267 | return NULL; |
| 268 | case ATA_DITER_ENABLED_REVERSE: |
| 269 | case ATA_DITER_ALL_REVERSE: |
| 270 | if (--dev >= link->device) |
| 271 | goto check; |
| 272 | return NULL; |
| 273 | } |
| 274 | |
| 275 | check: |
| 276 | if ((mode == ATA_DITER_ENABLED || mode == ATA_DITER_ENABLED_REVERSE) && |
| 277 | !ata_dev_enabled(dev)) |
| 278 | goto next; |
| 279 | return dev; |
| 280 | } |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 281 | EXPORT_SYMBOL_GPL(ata_dev_next); |
Tejun Heo | 1eca436 | 2008-11-03 20:03:17 +0900 | [diff] [blame] | 282 | |
| 283 | /** |
Tejun Heo | b1c7291 | 2008-07-31 17:02:43 +0900 | [diff] [blame] | 284 | * ata_dev_phys_link - find physical link for a device |
| 285 | * @dev: ATA device to look up physical link for |
| 286 | * |
| 287 | * Look up physical link which @dev is attached to. Note that |
| 288 | * this is different from @dev->link only when @dev is on slave |
| 289 | * link. For all other cases, it's the same as @dev->link. |
| 290 | * |
| 291 | * LOCKING: |
| 292 | * Don't care. |
| 293 | * |
| 294 | * RETURNS: |
| 295 | * Pointer to the found physical link. |
| 296 | */ |
| 297 | struct ata_link *ata_dev_phys_link(struct ata_device *dev) |
| 298 | { |
| 299 | struct ata_port *ap = dev->link->ap; |
| 300 | |
| 301 | if (!ap->slave_link) |
| 302 | return dev->link; |
| 303 | if (!dev->devno) |
| 304 | return &ap->link; |
| 305 | return ap->slave_link; |
| 306 | } |
| 307 | |
Bartlomiej Zolnierkiewicz | bf89b0b | 2020-03-26 16:58:22 +0100 | [diff] [blame] | 308 | #ifdef CONFIG_ATA_FORCE |
Tejun Heo | b1c7291 | 2008-07-31 17:02:43 +0900 | [diff] [blame] | 309 | /** |
Tejun Heo | 3326732 | 2008-02-13 09:15:09 +0900 | [diff] [blame] | 310 | * ata_force_cbl - force cable type according to libata.force |
Randy Dunlap | 4cdfa1b | 2008-02-22 12:21:37 -0800 | [diff] [blame] | 311 | * @ap: ATA port of interest |
Tejun Heo | 3326732 | 2008-02-13 09:15:09 +0900 | [diff] [blame] | 312 | * |
| 313 | * Force cable type according to libata.force and whine about it. |
| 314 | * The last entry which has matching port number is used, so it |
| 315 | * can be specified as part of device force parameters. For |
| 316 | * example, both "a:40c,1.00:udma4" and "1.00:40c,udma4" have the |
| 317 | * same effect. |
| 318 | * |
| 319 | * LOCKING: |
| 320 | * EH context. |
| 321 | */ |
| 322 | void ata_force_cbl(struct ata_port *ap) |
| 323 | { |
| 324 | int i; |
| 325 | |
| 326 | for (i = ata_force_tbl_size - 1; i >= 0; i--) { |
| 327 | const struct ata_force_ent *fe = &ata_force_tbl[i]; |
| 328 | |
| 329 | if (fe->port != -1 && fe->port != ap->print_id) |
| 330 | continue; |
| 331 | |
| 332 | if (fe->param.cbl == ATA_CBL_NONE) |
| 333 | continue; |
| 334 | |
| 335 | ap->cbl = fe->param.cbl; |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 336 | 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] | 337 | return; |
| 338 | } |
| 339 | } |
| 340 | |
| 341 | /** |
Tejun Heo | 05944bd | 2008-08-13 20:19:09 +0900 | [diff] [blame] | 342 | * ata_force_link_limits - force link limits according to libata.force |
Tejun Heo | 3326732 | 2008-02-13 09:15:09 +0900 | [diff] [blame] | 343 | * @link: ATA link of interest |
| 344 | * |
Tejun Heo | 05944bd | 2008-08-13 20:19:09 +0900 | [diff] [blame] | 345 | * Force link flags and SATA spd limit according to libata.force |
| 346 | * and whine about it. When only the port part is specified |
| 347 | * (e.g. 1:), the limit applies to all links connected to both |
| 348 | * the host link and all fan-out ports connected via PMP. If the |
| 349 | * device part is specified as 0 (e.g. 1.00:), it specifies the |
| 350 | * first fan-out link not the host link. Device number 15 always |
Tejun Heo | b1c7291 | 2008-07-31 17:02:43 +0900 | [diff] [blame] | 351 | * points to the host link whether PMP is attached or not. If the |
| 352 | * controller has slave link, device number 16 points to it. |
Tejun Heo | 3326732 | 2008-02-13 09:15:09 +0900 | [diff] [blame] | 353 | * |
| 354 | * LOCKING: |
| 355 | * EH context. |
| 356 | */ |
Tejun Heo | 05944bd | 2008-08-13 20:19:09 +0900 | [diff] [blame] | 357 | static void ata_force_link_limits(struct ata_link *link) |
Tejun Heo | 3326732 | 2008-02-13 09:15:09 +0900 | [diff] [blame] | 358 | { |
Tejun Heo | 05944bd | 2008-08-13 20:19:09 +0900 | [diff] [blame] | 359 | bool did_spd = false; |
Tejun Heo | b1c7291 | 2008-07-31 17:02:43 +0900 | [diff] [blame] | 360 | int linkno = link->pmp; |
| 361 | int i; |
Tejun Heo | 3326732 | 2008-02-13 09:15:09 +0900 | [diff] [blame] | 362 | |
| 363 | if (ata_is_host_link(link)) |
Tejun Heo | b1c7291 | 2008-07-31 17:02:43 +0900 | [diff] [blame] | 364 | linkno += 15; |
Tejun Heo | 3326732 | 2008-02-13 09:15:09 +0900 | [diff] [blame] | 365 | |
| 366 | for (i = ata_force_tbl_size - 1; i >= 0; i--) { |
| 367 | const struct ata_force_ent *fe = &ata_force_tbl[i]; |
| 368 | |
| 369 | if (fe->port != -1 && fe->port != link->ap->print_id) |
| 370 | continue; |
| 371 | |
| 372 | if (fe->device != -1 && fe->device != linkno) |
| 373 | continue; |
| 374 | |
Tejun Heo | 05944bd | 2008-08-13 20:19:09 +0900 | [diff] [blame] | 375 | /* only honor the first spd limit */ |
| 376 | if (!did_spd && fe->param.spd_limit) { |
| 377 | link->hw_sata_spd_limit = (1 << fe->param.spd_limit) - 1; |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 378 | ata_link_notice(link, "FORCE: PHY spd limit set to %s\n", |
Tejun Heo | 05944bd | 2008-08-13 20:19:09 +0900 | [diff] [blame] | 379 | fe->param.name); |
| 380 | did_spd = true; |
| 381 | } |
Tejun Heo | 3326732 | 2008-02-13 09:15:09 +0900 | [diff] [blame] | 382 | |
Tejun Heo | 05944bd | 2008-08-13 20:19:09 +0900 | [diff] [blame] | 383 | /* let lflags stack */ |
| 384 | if (fe->param.lflags) { |
| 385 | link->flags |= fe->param.lflags; |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 386 | ata_link_notice(link, |
Tejun Heo | 05944bd | 2008-08-13 20:19:09 +0900 | [diff] [blame] | 387 | "FORCE: link flag 0x%x forced -> 0x%x\n", |
| 388 | fe->param.lflags, link->flags); |
| 389 | } |
Tejun Heo | 3326732 | 2008-02-13 09:15:09 +0900 | [diff] [blame] | 390 | } |
| 391 | } |
| 392 | |
| 393 | /** |
| 394 | * ata_force_xfermask - force xfermask according to libata.force |
| 395 | * @dev: ATA device of interest |
| 396 | * |
| 397 | * Force xfer_mask according to libata.force and whine about it. |
| 398 | * For consistency with link selection, device number 15 selects |
| 399 | * the first device connected to the host link. |
| 400 | * |
| 401 | * LOCKING: |
| 402 | * EH context. |
| 403 | */ |
| 404 | static void ata_force_xfermask(struct ata_device *dev) |
| 405 | { |
| 406 | int devno = dev->link->pmp + dev->devno; |
| 407 | int alt_devno = devno; |
| 408 | int i; |
| 409 | |
Tejun Heo | b1c7291 | 2008-07-31 17:02:43 +0900 | [diff] [blame] | 410 | /* allow n.15/16 for devices attached to host port */ |
| 411 | if (ata_is_host_link(dev->link)) |
| 412 | alt_devno += 15; |
Tejun Heo | 3326732 | 2008-02-13 09:15:09 +0900 | [diff] [blame] | 413 | |
| 414 | for (i = ata_force_tbl_size - 1; i >= 0; i--) { |
| 415 | const struct ata_force_ent *fe = &ata_force_tbl[i]; |
| 416 | unsigned long pio_mask, mwdma_mask, udma_mask; |
| 417 | |
| 418 | if (fe->port != -1 && fe->port != dev->link->ap->print_id) |
| 419 | continue; |
| 420 | |
| 421 | if (fe->device != -1 && fe->device != devno && |
| 422 | fe->device != alt_devno) |
| 423 | continue; |
| 424 | |
| 425 | if (!fe->param.xfer_mask) |
| 426 | continue; |
| 427 | |
| 428 | ata_unpack_xfermask(fe->param.xfer_mask, |
| 429 | &pio_mask, &mwdma_mask, &udma_mask); |
| 430 | if (udma_mask) |
| 431 | dev->udma_mask = udma_mask; |
| 432 | else if (mwdma_mask) { |
| 433 | dev->udma_mask = 0; |
| 434 | dev->mwdma_mask = mwdma_mask; |
| 435 | } else { |
| 436 | dev->udma_mask = 0; |
| 437 | dev->mwdma_mask = 0; |
| 438 | dev->pio_mask = pio_mask; |
| 439 | } |
| 440 | |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 441 | ata_dev_notice(dev, "FORCE: xfer_mask set to %s\n", |
| 442 | fe->param.name); |
Tejun Heo | 3326732 | 2008-02-13 09:15:09 +0900 | [diff] [blame] | 443 | return; |
| 444 | } |
| 445 | } |
| 446 | |
| 447 | /** |
| 448 | * ata_force_horkage - force horkage according to libata.force |
| 449 | * @dev: ATA device of interest |
| 450 | * |
| 451 | * Force horkage according to libata.force and whine about it. |
| 452 | * For consistency with link selection, device number 15 selects |
| 453 | * the first device connected to the host link. |
| 454 | * |
| 455 | * LOCKING: |
| 456 | * EH context. |
| 457 | */ |
| 458 | static void ata_force_horkage(struct ata_device *dev) |
| 459 | { |
| 460 | int devno = dev->link->pmp + dev->devno; |
| 461 | int alt_devno = devno; |
| 462 | int i; |
| 463 | |
Tejun Heo | b1c7291 | 2008-07-31 17:02:43 +0900 | [diff] [blame] | 464 | /* allow n.15/16 for devices attached to host port */ |
| 465 | if (ata_is_host_link(dev->link)) |
| 466 | alt_devno += 15; |
Tejun Heo | 3326732 | 2008-02-13 09:15:09 +0900 | [diff] [blame] | 467 | |
| 468 | for (i = 0; i < ata_force_tbl_size; i++) { |
| 469 | const struct ata_force_ent *fe = &ata_force_tbl[i]; |
| 470 | |
| 471 | if (fe->port != -1 && fe->port != dev->link->ap->print_id) |
| 472 | continue; |
| 473 | |
| 474 | if (fe->device != -1 && fe->device != devno && |
| 475 | fe->device != alt_devno) |
| 476 | continue; |
| 477 | |
| 478 | if (!(~dev->horkage & fe->param.horkage_on) && |
| 479 | !(dev->horkage & fe->param.horkage_off)) |
| 480 | continue; |
| 481 | |
| 482 | dev->horkage |= fe->param.horkage_on; |
| 483 | dev->horkage &= ~fe->param.horkage_off; |
| 484 | |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 485 | ata_dev_notice(dev, "FORCE: horkage modified (%s)\n", |
| 486 | fe->param.name); |
Tejun Heo | 3326732 | 2008-02-13 09:15:09 +0900 | [diff] [blame] | 487 | } |
| 488 | } |
Bartlomiej Zolnierkiewicz | bf89b0b | 2020-03-26 16:58:22 +0100 | [diff] [blame] | 489 | #else |
| 490 | static inline void ata_force_link_limits(struct ata_link *link) { } |
| 491 | static inline void ata_force_xfermask(struct ata_device *dev) { } |
| 492 | static inline void ata_force_horkage(struct ata_device *dev) { } |
| 493 | #endif |
Tejun Heo | 3326732 | 2008-02-13 09:15:09 +0900 | [diff] [blame] | 494 | |
| 495 | /** |
Tejun Heo | 436d34b | 2008-04-02 17:28:46 +0900 | [diff] [blame] | 496 | * atapi_cmd_type - Determine ATAPI command type from SCSI opcode |
| 497 | * @opcode: SCSI opcode |
| 498 | * |
| 499 | * Determine ATAPI command type from @opcode. |
| 500 | * |
| 501 | * LOCKING: |
| 502 | * None. |
| 503 | * |
| 504 | * RETURNS: |
| 505 | * ATAPI_{READ|WRITE|READ_CD|PASS_THRU|MISC} |
| 506 | */ |
| 507 | int atapi_cmd_type(u8 opcode) |
| 508 | { |
| 509 | switch (opcode) { |
| 510 | case GPCMD_READ_10: |
| 511 | case GPCMD_READ_12: |
| 512 | return ATAPI_READ; |
| 513 | |
| 514 | case GPCMD_WRITE_10: |
| 515 | case GPCMD_WRITE_12: |
| 516 | case GPCMD_WRITE_AND_VERIFY_10: |
| 517 | return ATAPI_WRITE; |
| 518 | |
| 519 | case GPCMD_READ_CD: |
| 520 | case GPCMD_READ_CD_MSF: |
| 521 | return ATAPI_READ_CD; |
| 522 | |
Tejun Heo | e52dcc4 | 2008-04-02 17:35:19 +0900 | [diff] [blame] | 523 | case ATA_16: |
| 524 | case ATA_12: |
| 525 | if (atapi_passthru16) |
| 526 | return ATAPI_PASS_THRU; |
| 527 | /* fall thru */ |
Tejun Heo | 436d34b | 2008-04-02 17:28:46 +0900 | [diff] [blame] | 528 | default: |
| 529 | return ATAPI_MISC; |
| 530 | } |
| 531 | } |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 532 | EXPORT_SYMBOL_GPL(atapi_cmd_type); |
Tejun Heo | 436d34b | 2008-04-02 17:28:46 +0900 | [diff] [blame] | 533 | |
Albert Lee | 8cbd6df | 2005-10-12 15:06:27 +0800 | [diff] [blame] | 534 | static const u8 ata_rw_cmds[] = { |
| 535 | /* pio multi */ |
| 536 | ATA_CMD_READ_MULTI, |
| 537 | ATA_CMD_WRITE_MULTI, |
| 538 | ATA_CMD_READ_MULTI_EXT, |
| 539 | ATA_CMD_WRITE_MULTI_EXT, |
Tejun Heo | 9a3dccc | 2006-01-06 09:56:18 +0100 | [diff] [blame] | 540 | 0, |
| 541 | 0, |
| 542 | 0, |
| 543 | ATA_CMD_WRITE_MULTI_FUA_EXT, |
Albert Lee | 8cbd6df | 2005-10-12 15:06:27 +0800 | [diff] [blame] | 544 | /* pio */ |
| 545 | ATA_CMD_PIO_READ, |
| 546 | ATA_CMD_PIO_WRITE, |
| 547 | ATA_CMD_PIO_READ_EXT, |
| 548 | ATA_CMD_PIO_WRITE_EXT, |
Tejun Heo | 9a3dccc | 2006-01-06 09:56:18 +0100 | [diff] [blame] | 549 | 0, |
| 550 | 0, |
| 551 | 0, |
| 552 | 0, |
Albert Lee | 8cbd6df | 2005-10-12 15:06:27 +0800 | [diff] [blame] | 553 | /* dma */ |
| 554 | ATA_CMD_READ, |
| 555 | ATA_CMD_WRITE, |
| 556 | ATA_CMD_READ_EXT, |
Tejun Heo | 9a3dccc | 2006-01-06 09:56:18 +0100 | [diff] [blame] | 557 | ATA_CMD_WRITE_EXT, |
| 558 | 0, |
| 559 | 0, |
| 560 | 0, |
| 561 | ATA_CMD_WRITE_FUA_EXT |
Albert Lee | 8cbd6df | 2005-10-12 15:06:27 +0800 | [diff] [blame] | 562 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 563 | |
| 564 | /** |
Albert Lee | 8cbd6df | 2005-10-12 15:06:27 +0800 | [diff] [blame] | 565 | * ata_rwcmd_protocol - set taskfile r/w commands and protocol |
Tejun Heo | bd056d7 | 2006-11-14 22:47:10 +0900 | [diff] [blame] | 566 | * @tf: command to examine and configure |
| 567 | * @dev: device tf belongs to |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 568 | * |
Jeff Garzik | 2e9edbf | 2006-03-24 09:56:57 -0500 | [diff] [blame] | 569 | * Examine the device configuration and tf->flags to calculate |
Albert Lee | 8cbd6df | 2005-10-12 15:06:27 +0800 | [diff] [blame] | 570 | * the proper read/write commands and protocol to use. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 571 | * |
| 572 | * LOCKING: |
| 573 | * caller. |
| 574 | */ |
Tejun Heo | bd056d7 | 2006-11-14 22:47:10 +0900 | [diff] [blame] | 575 | 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] | 576 | { |
Tejun Heo | 9a3dccc | 2006-01-06 09:56:18 +0100 | [diff] [blame] | 577 | u8 cmd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 578 | |
Tejun Heo | 9a3dccc | 2006-01-06 09:56:18 +0100 | [diff] [blame] | 579 | int index, fua, lba48, write; |
Jeff Garzik | 2e9edbf | 2006-03-24 09:56:57 -0500 | [diff] [blame] | 580 | |
Tejun Heo | 9a3dccc | 2006-01-06 09:56:18 +0100 | [diff] [blame] | 581 | fua = (tf->flags & ATA_TFLAG_FUA) ? 4 : 0; |
Albert Lee | 8cbd6df | 2005-10-12 15:06:27 +0800 | [diff] [blame] | 582 | lba48 = (tf->flags & ATA_TFLAG_LBA48) ? 2 : 0; |
| 583 | write = (tf->flags & ATA_TFLAG_WRITE) ? 1 : 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 584 | |
Albert Lee | 8cbd6df | 2005-10-12 15:06:27 +0800 | [diff] [blame] | 585 | if (dev->flags & ATA_DFLAG_PIO) { |
| 586 | tf->protocol = ATA_PROT_PIO; |
Tejun Heo | 9a3dccc | 2006-01-06 09:56:18 +0100 | [diff] [blame] | 587 | index = dev->multi_count ? 0 : 8; |
Tejun Heo | 9af5c9c | 2007-08-06 18:36:22 +0900 | [diff] [blame] | 588 | } else if (lba48 && (dev->link->ap->flags & ATA_FLAG_PIO_LBA48)) { |
Alan Cox | 8d238e0 | 2006-01-17 20:50:31 +0000 | [diff] [blame] | 589 | /* Unable to use DMA due to host limitation */ |
| 590 | tf->protocol = ATA_PROT_PIO; |
Albert Lee | 0565c26 | 2006-02-13 18:55:25 +0800 | [diff] [blame] | 591 | index = dev->multi_count ? 0 : 8; |
Albert Lee | 8cbd6df | 2005-10-12 15:06:27 +0800 | [diff] [blame] | 592 | } else { |
| 593 | tf->protocol = ATA_PROT_DMA; |
Tejun Heo | 9a3dccc | 2006-01-06 09:56:18 +0100 | [diff] [blame] | 594 | index = 16; |
Albert Lee | 8cbd6df | 2005-10-12 15:06:27 +0800 | [diff] [blame] | 595 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 596 | |
Tejun Heo | 9a3dccc | 2006-01-06 09:56:18 +0100 | [diff] [blame] | 597 | cmd = ata_rw_cmds[index + fua + lba48 + write]; |
| 598 | if (cmd) { |
| 599 | tf->command = cmd; |
| 600 | return 0; |
| 601 | } |
| 602 | return -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 603 | } |
| 604 | |
Tejun Heo | cb95d56 | 2006-03-06 04:31:56 +0900 | [diff] [blame] | 605 | /** |
Tejun Heo | 35b649f | 2006-11-14 22:37:35 +0900 | [diff] [blame] | 606 | * ata_tf_read_block - Read block address from ATA taskfile |
| 607 | * @tf: ATA taskfile of interest |
| 608 | * @dev: ATA device @tf belongs to |
| 609 | * |
| 610 | * LOCKING: |
| 611 | * None. |
| 612 | * |
| 613 | * Read block address from @tf. This function can handle all |
| 614 | * three address formats - LBA, LBA48 and CHS. tf->protocol and |
| 615 | * flags select the address format to use. |
| 616 | * |
| 617 | * RETURNS: |
| 618 | * Block address read from @tf. |
| 619 | */ |
Hannes Reinecke | cffd1ee | 2016-04-04 11:43:57 +0200 | [diff] [blame] | 620 | 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] | 621 | { |
| 622 | u64 block = 0; |
| 623 | |
Tejun Heo | fe16d4f | 2015-08-03 11:41:33 -0400 | [diff] [blame] | 624 | if (tf->flags & ATA_TFLAG_LBA) { |
Tejun Heo | 35b649f | 2006-11-14 22:37:35 +0900 | [diff] [blame] | 625 | if (tf->flags & ATA_TFLAG_LBA48) { |
| 626 | block |= (u64)tf->hob_lbah << 40; |
| 627 | block |= (u64)tf->hob_lbam << 32; |
Roland Dreier | 44901a9 | 2008-11-04 10:34:48 -0800 | [diff] [blame] | 628 | block |= (u64)tf->hob_lbal << 24; |
Tejun Heo | 35b649f | 2006-11-14 22:37:35 +0900 | [diff] [blame] | 629 | } else |
| 630 | block |= (tf->device & 0xf) << 24; |
| 631 | |
| 632 | block |= tf->lbah << 16; |
| 633 | block |= tf->lbam << 8; |
| 634 | block |= tf->lbal; |
| 635 | } else { |
| 636 | u32 cyl, head, sect; |
| 637 | |
| 638 | cyl = tf->lbam | (tf->lbah << 8); |
| 639 | head = tf->device & 0xf; |
| 640 | sect = tf->lbal; |
| 641 | |
Tejun Heo | ac8672e | 2009-08-16 21:21:21 +0900 | [diff] [blame] | 642 | if (!sect) { |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 643 | ata_dev_warn(dev, |
| 644 | "device reported invalid CHS sector 0\n"); |
Hannes Reinecke | cffd1ee | 2016-04-04 11:43:57 +0200 | [diff] [blame] | 645 | return U64_MAX; |
Tejun Heo | ac8672e | 2009-08-16 21:21:21 +0900 | [diff] [blame] | 646 | } |
| 647 | |
| 648 | block = (cyl * dev->heads + head) * dev->sectors + sect - 1; |
Tejun Heo | 35b649f | 2006-11-14 22:37:35 +0900 | [diff] [blame] | 649 | } |
| 650 | |
| 651 | return block; |
| 652 | } |
| 653 | |
| 654 | /** |
Tejun Heo | bd056d7 | 2006-11-14 22:47:10 +0900 | [diff] [blame] | 655 | * ata_build_rw_tf - Build ATA taskfile for given read/write request |
| 656 | * @tf: Target ATA taskfile |
| 657 | * @dev: ATA device @tf belongs to |
| 658 | * @block: Block address |
| 659 | * @n_block: Number of blocks |
| 660 | * @tf_flags: RW/FUA etc... |
| 661 | * @tag: tag |
Adam Manzanares | 8e06178 | 2016-10-17 11:27:29 -0700 | [diff] [blame] | 662 | * @class: IO priority class |
Tejun Heo | bd056d7 | 2006-11-14 22:47:10 +0900 | [diff] [blame] | 663 | * |
| 664 | * LOCKING: |
| 665 | * None. |
| 666 | * |
| 667 | * Build ATA taskfile @tf for read/write request described by |
| 668 | * @block, @n_block, @tf_flags and @tag on @dev. |
| 669 | * |
| 670 | * RETURNS: |
| 671 | * |
| 672 | * 0 on success, -ERANGE if the request is too large for @dev, |
| 673 | * -EINVAL if the request is invalid. |
| 674 | */ |
| 675 | int ata_build_rw_tf(struct ata_taskfile *tf, struct ata_device *dev, |
| 676 | u64 block, u32 n_block, unsigned int tf_flags, |
Adam Manzanares | 8e06178 | 2016-10-17 11:27:29 -0700 | [diff] [blame] | 677 | unsigned int tag, int class) |
Tejun Heo | bd056d7 | 2006-11-14 22:47:10 +0900 | [diff] [blame] | 678 | { |
| 679 | tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE; |
| 680 | tf->flags |= tf_flags; |
| 681 | |
Jens Axboe | 2e2cc67 | 2018-05-11 12:51:06 -0600 | [diff] [blame] | 682 | if (ata_ncq_enabled(dev) && !ata_tag_internal(tag)) { |
Tejun Heo | bd056d7 | 2006-11-14 22:47:10 +0900 | [diff] [blame] | 683 | /* yay, NCQ */ |
| 684 | if (!lba_48_ok(block, n_block)) |
| 685 | return -ERANGE; |
| 686 | |
| 687 | tf->protocol = ATA_PROT_NCQ; |
| 688 | tf->flags |= ATA_TFLAG_LBA | ATA_TFLAG_LBA48; |
| 689 | |
| 690 | if (tf->flags & ATA_TFLAG_WRITE) |
| 691 | tf->command = ATA_CMD_FPDMA_WRITE; |
| 692 | else |
| 693 | tf->command = ATA_CMD_FPDMA_READ; |
| 694 | |
| 695 | tf->nsect = tag << 3; |
| 696 | tf->hob_feature = (n_block >> 8) & 0xff; |
| 697 | tf->feature = n_block & 0xff; |
| 698 | |
| 699 | tf->hob_lbah = (block >> 40) & 0xff; |
| 700 | tf->hob_lbam = (block >> 32) & 0xff; |
| 701 | tf->hob_lbal = (block >> 24) & 0xff; |
| 702 | tf->lbah = (block >> 16) & 0xff; |
| 703 | tf->lbam = (block >> 8) & 0xff; |
| 704 | tf->lbal = block & 0xff; |
| 705 | |
Sergei Shtylyov | 9ca7cfa | 2012-08-21 22:18:50 +0400 | [diff] [blame] | 706 | tf->device = ATA_LBA; |
Tejun Heo | bd056d7 | 2006-11-14 22:47:10 +0900 | [diff] [blame] | 707 | if (tf->flags & ATA_TFLAG_FUA) |
| 708 | tf->device |= 1 << 7; |
Adam Manzanares | 8e06178 | 2016-10-17 11:27:29 -0700 | [diff] [blame] | 709 | |
Adam Manzanares | 9f56eca | 2016-12-13 12:00:05 -0800 | [diff] [blame] | 710 | if (dev->flags & ATA_DFLAG_NCQ_PRIO) { |
Adam Manzanares | 8e06178 | 2016-10-17 11:27:29 -0700 | [diff] [blame] | 711 | if (class == IOPRIO_CLASS_RT) |
| 712 | tf->hob_nsect |= ATA_PRIO_HIGH << |
| 713 | ATA_SHIFT_PRIO; |
| 714 | } |
Tejun Heo | bd056d7 | 2006-11-14 22:47:10 +0900 | [diff] [blame] | 715 | } else if (dev->flags & ATA_DFLAG_LBA) { |
| 716 | tf->flags |= ATA_TFLAG_LBA; |
| 717 | |
| 718 | if (lba_28_ok(block, n_block)) { |
| 719 | /* use LBA28 */ |
| 720 | tf->device |= (block >> 24) & 0xf; |
| 721 | } else if (lba_48_ok(block, n_block)) { |
| 722 | if (!(dev->flags & ATA_DFLAG_LBA48)) |
| 723 | return -ERANGE; |
| 724 | |
| 725 | /* use LBA48 */ |
| 726 | tf->flags |= ATA_TFLAG_LBA48; |
| 727 | |
| 728 | tf->hob_nsect = (n_block >> 8) & 0xff; |
| 729 | |
| 730 | tf->hob_lbah = (block >> 40) & 0xff; |
| 731 | tf->hob_lbam = (block >> 32) & 0xff; |
| 732 | tf->hob_lbal = (block >> 24) & 0xff; |
| 733 | } else |
| 734 | /* request too large even for LBA48 */ |
| 735 | return -ERANGE; |
| 736 | |
| 737 | if (unlikely(ata_rwcmd_protocol(tf, dev) < 0)) |
| 738 | return -EINVAL; |
| 739 | |
| 740 | tf->nsect = n_block & 0xff; |
| 741 | |
| 742 | tf->lbah = (block >> 16) & 0xff; |
| 743 | tf->lbam = (block >> 8) & 0xff; |
| 744 | tf->lbal = block & 0xff; |
| 745 | |
| 746 | tf->device |= ATA_LBA; |
| 747 | } else { |
| 748 | /* CHS */ |
| 749 | u32 sect, head, cyl, track; |
| 750 | |
| 751 | /* The request -may- be too large for CHS addressing. */ |
| 752 | if (!lba_28_ok(block, n_block)) |
| 753 | return -ERANGE; |
| 754 | |
| 755 | if (unlikely(ata_rwcmd_protocol(tf, dev) < 0)) |
| 756 | return -EINVAL; |
| 757 | |
| 758 | /* Convert LBA to CHS */ |
| 759 | track = (u32)block / dev->sectors; |
| 760 | cyl = track / dev->heads; |
| 761 | head = track % dev->heads; |
| 762 | sect = (u32)block % dev->sectors + 1; |
| 763 | |
| 764 | DPRINTK("block %u track %u cyl %u head %u sect %u\n", |
| 765 | (u32)block, track, cyl, head, sect); |
| 766 | |
| 767 | /* Check whether the converted CHS can fit. |
| 768 | Cylinder: 0-65535 |
| 769 | Head: 0-15 |
| 770 | Sector: 1-255*/ |
| 771 | if ((cyl >> 16) || (head >> 4) || (sect >> 8) || (!sect)) |
| 772 | return -ERANGE; |
| 773 | |
| 774 | tf->nsect = n_block & 0xff; /* Sector count 0 means 256 sectors */ |
| 775 | tf->lbal = sect; |
| 776 | tf->lbam = cyl; |
| 777 | tf->lbah = cyl >> 8; |
| 778 | tf->device |= head; |
| 779 | } |
| 780 | |
| 781 | return 0; |
| 782 | } |
| 783 | |
| 784 | /** |
Tejun Heo | cb95d56 | 2006-03-06 04:31:56 +0900 | [diff] [blame] | 785 | * ata_pack_xfermask - Pack pio, mwdma and udma masks into xfer_mask |
| 786 | * @pio_mask: pio_mask |
| 787 | * @mwdma_mask: mwdma_mask |
| 788 | * @udma_mask: udma_mask |
| 789 | * |
| 790 | * Pack @pio_mask, @mwdma_mask and @udma_mask into a single |
| 791 | * unsigned int xfer_mask. |
| 792 | * |
| 793 | * LOCKING: |
| 794 | * None. |
| 795 | * |
| 796 | * RETURNS: |
| 797 | * Packed xfer_mask. |
| 798 | */ |
Tejun Heo | 7dc951a | 2007-11-27 19:43:42 +0900 | [diff] [blame] | 799 | unsigned long ata_pack_xfermask(unsigned long pio_mask, |
| 800 | unsigned long mwdma_mask, |
| 801 | unsigned long udma_mask) |
Tejun Heo | cb95d56 | 2006-03-06 04:31:56 +0900 | [diff] [blame] | 802 | { |
| 803 | return ((pio_mask << ATA_SHIFT_PIO) & ATA_MASK_PIO) | |
| 804 | ((mwdma_mask << ATA_SHIFT_MWDMA) & ATA_MASK_MWDMA) | |
| 805 | ((udma_mask << ATA_SHIFT_UDMA) & ATA_MASK_UDMA); |
| 806 | } |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 807 | EXPORT_SYMBOL_GPL(ata_pack_xfermask); |
Tejun Heo | cb95d56 | 2006-03-06 04:31:56 +0900 | [diff] [blame] | 808 | |
Tejun Heo | c0489e4 | 2006-03-24 14:07:49 +0900 | [diff] [blame] | 809 | /** |
| 810 | * ata_unpack_xfermask - Unpack xfer_mask into pio, mwdma and udma masks |
| 811 | * @xfer_mask: xfer_mask to unpack |
| 812 | * @pio_mask: resulting pio_mask |
| 813 | * @mwdma_mask: resulting mwdma_mask |
| 814 | * @udma_mask: resulting udma_mask |
| 815 | * |
| 816 | * Unpack @xfer_mask into @pio_mask, @mwdma_mask and @udma_mask. |
Masanari Iida | c9b5560 | 2016-04-13 23:36:27 +0900 | [diff] [blame] | 817 | * Any NULL destination masks will be ignored. |
Tejun Heo | c0489e4 | 2006-03-24 14:07:49 +0900 | [diff] [blame] | 818 | */ |
Tejun Heo | 7dc951a | 2007-11-27 19:43:42 +0900 | [diff] [blame] | 819 | void ata_unpack_xfermask(unsigned long xfer_mask, unsigned long *pio_mask, |
| 820 | unsigned long *mwdma_mask, unsigned long *udma_mask) |
Tejun Heo | c0489e4 | 2006-03-24 14:07:49 +0900 | [diff] [blame] | 821 | { |
| 822 | if (pio_mask) |
| 823 | *pio_mask = (xfer_mask & ATA_MASK_PIO) >> ATA_SHIFT_PIO; |
| 824 | if (mwdma_mask) |
| 825 | *mwdma_mask = (xfer_mask & ATA_MASK_MWDMA) >> ATA_SHIFT_MWDMA; |
| 826 | if (udma_mask) |
| 827 | *udma_mask = (xfer_mask & ATA_MASK_UDMA) >> ATA_SHIFT_UDMA; |
| 828 | } |
| 829 | |
Tejun Heo | cb95d56 | 2006-03-06 04:31:56 +0900 | [diff] [blame] | 830 | static const struct ata_xfer_ent { |
Tejun Heo | be9a50c | 2006-03-31 22:48:52 +0900 | [diff] [blame] | 831 | int shift, bits; |
Tejun Heo | cb95d56 | 2006-03-06 04:31:56 +0900 | [diff] [blame] | 832 | u8 base; |
| 833 | } ata_xfer_tbl[] = { |
Tejun Heo | 70cd071 | 2007-11-27 19:43:40 +0900 | [diff] [blame] | 834 | { ATA_SHIFT_PIO, ATA_NR_PIO_MODES, XFER_PIO_0 }, |
| 835 | { ATA_SHIFT_MWDMA, ATA_NR_MWDMA_MODES, XFER_MW_DMA_0 }, |
| 836 | { ATA_SHIFT_UDMA, ATA_NR_UDMA_MODES, XFER_UDMA_0 }, |
Tejun Heo | cb95d56 | 2006-03-06 04:31:56 +0900 | [diff] [blame] | 837 | { -1, }, |
| 838 | }; |
| 839 | |
| 840 | /** |
| 841 | * ata_xfer_mask2mode - Find matching XFER_* for the given xfer_mask |
| 842 | * @xfer_mask: xfer_mask of interest |
| 843 | * |
| 844 | * Return matching XFER_* value for @xfer_mask. Only the highest |
| 845 | * bit of @xfer_mask is considered. |
| 846 | * |
| 847 | * LOCKING: |
| 848 | * None. |
| 849 | * |
| 850 | * RETURNS: |
Tejun Heo | 70cd071 | 2007-11-27 19:43:40 +0900 | [diff] [blame] | 851 | * Matching XFER_* value, 0xff if no match found. |
Tejun Heo | cb95d56 | 2006-03-06 04:31:56 +0900 | [diff] [blame] | 852 | */ |
Tejun Heo | 7dc951a | 2007-11-27 19:43:42 +0900 | [diff] [blame] | 853 | u8 ata_xfer_mask2mode(unsigned long xfer_mask) |
Tejun Heo | cb95d56 | 2006-03-06 04:31:56 +0900 | [diff] [blame] | 854 | { |
| 855 | int highbit = fls(xfer_mask) - 1; |
| 856 | const struct ata_xfer_ent *ent; |
| 857 | |
| 858 | for (ent = ata_xfer_tbl; ent->shift >= 0; ent++) |
| 859 | if (highbit >= ent->shift && highbit < ent->shift + ent->bits) |
| 860 | return ent->base + highbit - ent->shift; |
Tejun Heo | 70cd071 | 2007-11-27 19:43:40 +0900 | [diff] [blame] | 861 | return 0xff; |
Tejun Heo | cb95d56 | 2006-03-06 04:31:56 +0900 | [diff] [blame] | 862 | } |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 863 | EXPORT_SYMBOL_GPL(ata_xfer_mask2mode); |
Tejun Heo | cb95d56 | 2006-03-06 04:31:56 +0900 | [diff] [blame] | 864 | |
| 865 | /** |
| 866 | * ata_xfer_mode2mask - Find matching xfer_mask for XFER_* |
| 867 | * @xfer_mode: XFER_* of interest |
| 868 | * |
| 869 | * Return matching xfer_mask for @xfer_mode. |
| 870 | * |
| 871 | * LOCKING: |
| 872 | * None. |
| 873 | * |
| 874 | * RETURNS: |
| 875 | * Matching xfer_mask, 0 if no match found. |
| 876 | */ |
Tejun Heo | 7dc951a | 2007-11-27 19:43:42 +0900 | [diff] [blame] | 877 | unsigned long ata_xfer_mode2mask(u8 xfer_mode) |
Tejun Heo | cb95d56 | 2006-03-06 04:31:56 +0900 | [diff] [blame] | 878 | { |
| 879 | const struct ata_xfer_ent *ent; |
| 880 | |
| 881 | for (ent = ata_xfer_tbl; ent->shift >= 0; ent++) |
| 882 | if (xfer_mode >= ent->base && xfer_mode < ent->base + ent->bits) |
Tejun Heo | 70cd071 | 2007-11-27 19:43:40 +0900 | [diff] [blame] | 883 | return ((2 << (ent->shift + xfer_mode - ent->base)) - 1) |
| 884 | & ~((1 << ent->shift) - 1); |
Tejun Heo | cb95d56 | 2006-03-06 04:31:56 +0900 | [diff] [blame] | 885 | return 0; |
| 886 | } |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 887 | EXPORT_SYMBOL_GPL(ata_xfer_mode2mask); |
Tejun Heo | cb95d56 | 2006-03-06 04:31:56 +0900 | [diff] [blame] | 888 | |
| 889 | /** |
| 890 | * ata_xfer_mode2shift - Find matching xfer_shift for XFER_* |
| 891 | * @xfer_mode: XFER_* of interest |
| 892 | * |
| 893 | * Return matching xfer_shift for @xfer_mode. |
| 894 | * |
| 895 | * LOCKING: |
| 896 | * None. |
| 897 | * |
| 898 | * RETURNS: |
| 899 | * Matching xfer_shift, -1 if no match found. |
| 900 | */ |
Tejun Heo | 7dc951a | 2007-11-27 19:43:42 +0900 | [diff] [blame] | 901 | int ata_xfer_mode2shift(unsigned long xfer_mode) |
Tejun Heo | cb95d56 | 2006-03-06 04:31:56 +0900 | [diff] [blame] | 902 | { |
| 903 | const struct ata_xfer_ent *ent; |
| 904 | |
| 905 | for (ent = ata_xfer_tbl; ent->shift >= 0; ent++) |
| 906 | if (xfer_mode >= ent->base && xfer_mode < ent->base + ent->bits) |
| 907 | return ent->shift; |
| 908 | return -1; |
| 909 | } |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 910 | EXPORT_SYMBOL_GPL(ata_xfer_mode2shift); |
Tejun Heo | cb95d56 | 2006-03-06 04:31:56 +0900 | [diff] [blame] | 911 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 912 | /** |
Tejun Heo | 1da7b0d | 2006-03-06 04:31:56 +0900 | [diff] [blame] | 913 | * ata_mode_string - convert xfer_mask to string |
| 914 | * @xfer_mask: mask of bits supported; only highest bit counts. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 915 | * |
| 916 | * Determine string which represents the highest speed |
Tejun Heo | 1da7b0d | 2006-03-06 04:31:56 +0900 | [diff] [blame] | 917 | * (highest bit in @modemask). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 918 | * |
| 919 | * LOCKING: |
| 920 | * None. |
| 921 | * |
| 922 | * RETURNS: |
| 923 | * Constant C string representing highest speed listed in |
Tejun Heo | 1da7b0d | 2006-03-06 04:31:56 +0900 | [diff] [blame] | 924 | * @mode_mask, or the constant C string "<n/a>". |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 925 | */ |
Tejun Heo | 7dc951a | 2007-11-27 19:43:42 +0900 | [diff] [blame] | 926 | const char *ata_mode_string(unsigned long xfer_mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 927 | { |
Tejun Heo | 75f554b | 2006-03-06 04:31:57 +0900 | [diff] [blame] | 928 | static const char * const xfer_mode_str[] = { |
| 929 | "PIO0", |
| 930 | "PIO1", |
| 931 | "PIO2", |
| 932 | "PIO3", |
| 933 | "PIO4", |
Alan Cox | b352e57 | 2006-08-10 18:52:12 +0100 | [diff] [blame] | 934 | "PIO5", |
| 935 | "PIO6", |
Tejun Heo | 75f554b | 2006-03-06 04:31:57 +0900 | [diff] [blame] | 936 | "MWDMA0", |
| 937 | "MWDMA1", |
| 938 | "MWDMA2", |
Alan Cox | b352e57 | 2006-08-10 18:52:12 +0100 | [diff] [blame] | 939 | "MWDMA3", |
| 940 | "MWDMA4", |
Tejun Heo | 75f554b | 2006-03-06 04:31:57 +0900 | [diff] [blame] | 941 | "UDMA/16", |
| 942 | "UDMA/25", |
| 943 | "UDMA/33", |
| 944 | "UDMA/44", |
| 945 | "UDMA/66", |
| 946 | "UDMA/100", |
| 947 | "UDMA/133", |
| 948 | "UDMA7", |
| 949 | }; |
Tejun Heo | 1da7b0d | 2006-03-06 04:31:56 +0900 | [diff] [blame] | 950 | int highbit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 951 | |
Tejun Heo | 1da7b0d | 2006-03-06 04:31:56 +0900 | [diff] [blame] | 952 | highbit = fls(xfer_mask) - 1; |
| 953 | if (highbit >= 0 && highbit < ARRAY_SIZE(xfer_mode_str)) |
| 954 | return xfer_mode_str[highbit]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 955 | return "<n/a>"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 956 | } |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 957 | EXPORT_SYMBOL_GPL(ata_mode_string); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 958 | |
Gwendal Grignou | d902747 | 2010-05-25 12:31:38 -0700 | [diff] [blame] | 959 | const char *sata_spd_string(unsigned int spd) |
Tejun Heo | 4c360c8 | 2006-04-01 01:38:17 +0900 | [diff] [blame] | 960 | { |
| 961 | static const char * const spd_str[] = { |
| 962 | "1.5 Gbps", |
| 963 | "3.0 Gbps", |
Shane Huang | 8522ee2 | 2008-12-30 11:00:37 +0800 | [diff] [blame] | 964 | "6.0 Gbps", |
Tejun Heo | 4c360c8 | 2006-04-01 01:38:17 +0900 | [diff] [blame] | 965 | }; |
| 966 | |
| 967 | if (spd == 0 || (spd - 1) >= ARRAY_SIZE(spd_str)) |
| 968 | return "<unknown>"; |
| 969 | return spd_str[spd - 1]; |
| 970 | } |
| 971 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 972 | /** |
| 973 | * ata_dev_classify - determine device type based on ATA-spec signature |
| 974 | * @tf: ATA taskfile register set for device to be identified |
| 975 | * |
| 976 | * Determine from taskfile register contents whether a device is |
| 977 | * ATA or ATAPI, as per "Signature and persistence" section |
| 978 | * of ATA/PI spec (volume 1, sect 5.14). |
| 979 | * |
| 980 | * LOCKING: |
| 981 | * None. |
| 982 | * |
| 983 | * RETURNS: |
Hannes Reinecke | 9162c65 | 2014-11-05 13:08:21 +0100 | [diff] [blame] | 984 | * Device type, %ATA_DEV_ATA, %ATA_DEV_ATAPI, %ATA_DEV_PMP, |
| 985 | * %ATA_DEV_ZAC, or %ATA_DEV_UNKNOWN the event of failure. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 986 | */ |
Jeff Garzik | 057ace5 | 2005-10-22 14:27:05 -0400 | [diff] [blame] | 987 | unsigned int ata_dev_classify(const struct ata_taskfile *tf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 988 | { |
| 989 | /* Apple's open source Darwin code hints that some devices only |
| 990 | * put a proper signature into the LBA mid/high registers, |
| 991 | * So, we only check those. It's sufficient for uniqueness. |
Tejun Heo | 633273a | 2007-09-23 13:19:54 +0900 | [diff] [blame] | 992 | * |
| 993 | * ATA/ATAPI-7 (d1532v1r1: Feb. 19, 2003) specified separate |
| 994 | * signatures for ATA and ATAPI devices attached on SerialATA, |
| 995 | * 0x3c/0xc3 and 0x69/0x96 respectively. However, SerialATA |
| 996 | * spec has never mentioned about using different signatures |
| 997 | * for ATA/ATAPI devices. Then, Serial ATA II: Port |
| 998 | * Multiplier specification began to use 0x69/0x96 to identify |
| 999 | * port multpliers and 0x3c/0xc3 to identify SEMB device. |
| 1000 | * ATA/ATAPI-7 dropped descriptions about 0x3c/0xc3 and |
| 1001 | * 0x69/0x96 shortly and described them as reserved for |
| 1002 | * SerialATA. |
| 1003 | * |
| 1004 | * We follow the current spec and consider that 0x69/0x96 |
| 1005 | * identifies a port multiplier and 0x3c/0xc3 a SEMB device. |
Tejun Heo | 79b42ba | 2009-04-15 06:21:10 +0900 | [diff] [blame] | 1006 | * Unfortunately, WDC WD1600JS-62MHB5 (a hard drive) reports |
| 1007 | * SEMB signature. This is worked around in |
| 1008 | * ata_dev_read_id(). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1009 | */ |
Tejun Heo | 633273a | 2007-09-23 13:19:54 +0900 | [diff] [blame] | 1010 | if ((tf->lbam == 0) && (tf->lbah == 0)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1011 | DPRINTK("found ATA device by sig\n"); |
| 1012 | return ATA_DEV_ATA; |
| 1013 | } |
| 1014 | |
Tejun Heo | 633273a | 2007-09-23 13:19:54 +0900 | [diff] [blame] | 1015 | if ((tf->lbam == 0x14) && (tf->lbah == 0xeb)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1016 | DPRINTK("found ATAPI device by sig\n"); |
| 1017 | return ATA_DEV_ATAPI; |
| 1018 | } |
| 1019 | |
Tejun Heo | 633273a | 2007-09-23 13:19:54 +0900 | [diff] [blame] | 1020 | if ((tf->lbam == 0x69) && (tf->lbah == 0x96)) { |
| 1021 | DPRINTK("found PMP device by sig\n"); |
| 1022 | return ATA_DEV_PMP; |
| 1023 | } |
| 1024 | |
| 1025 | if ((tf->lbam == 0x3c) && (tf->lbah == 0xc3)) { |
Tejun Heo | 79b42ba | 2009-04-15 06:21:10 +0900 | [diff] [blame] | 1026 | DPRINTK("found SEMB device by sig (could be ATA device)\n"); |
| 1027 | return ATA_DEV_SEMB; |
Tejun Heo | 633273a | 2007-09-23 13:19:54 +0900 | [diff] [blame] | 1028 | } |
| 1029 | |
Hannes Reinecke | 9162c65 | 2014-11-05 13:08:21 +0100 | [diff] [blame] | 1030 | if ((tf->lbam == 0xcd) && (tf->lbah == 0xab)) { |
| 1031 | DPRINTK("found ZAC device by sig\n"); |
| 1032 | return ATA_DEV_ZAC; |
| 1033 | } |
| 1034 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1035 | DPRINTK("unknown device\n"); |
| 1036 | return ATA_DEV_UNKNOWN; |
| 1037 | } |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 1038 | EXPORT_SYMBOL_GPL(ata_dev_classify); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1039 | |
| 1040 | /** |
Tejun Heo | 6a62a04 | 2006-02-13 10:02:46 +0900 | [diff] [blame] | 1041 | * ata_id_string - Convert IDENTIFY DEVICE page into string |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1042 | * @id: IDENTIFY DEVICE results we will examine |
| 1043 | * @s: string into which data is output |
| 1044 | * @ofs: offset into identify device page |
| 1045 | * @len: length of string to return. must be an even number. |
| 1046 | * |
| 1047 | * The strings in the IDENTIFY DEVICE page are broken up into |
| 1048 | * 16-bit chunks. Run through the string, and output each |
| 1049 | * 8-bit chunk linearly, regardless of platform. |
| 1050 | * |
| 1051 | * LOCKING: |
| 1052 | * caller. |
| 1053 | */ |
| 1054 | |
Tejun Heo | 6a62a04 | 2006-02-13 10:02:46 +0900 | [diff] [blame] | 1055 | void ata_id_string(const u16 *id, unsigned char *s, |
| 1056 | unsigned int ofs, unsigned int len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1057 | { |
| 1058 | unsigned int c; |
| 1059 | |
Alan Cox | 963e497 | 2008-07-24 17:16:06 +0100 | [diff] [blame] | 1060 | BUG_ON(len & 1); |
| 1061 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1062 | while (len > 0) { |
| 1063 | c = id[ofs] >> 8; |
| 1064 | *s = c; |
| 1065 | s++; |
| 1066 | |
| 1067 | c = id[ofs] & 0xff; |
| 1068 | *s = c; |
| 1069 | s++; |
| 1070 | |
| 1071 | ofs++; |
| 1072 | len -= 2; |
| 1073 | } |
| 1074 | } |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 1075 | EXPORT_SYMBOL_GPL(ata_id_string); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1076 | |
Tejun Heo | 0e949ff | 2006-02-12 22:47:04 +0900 | [diff] [blame] | 1077 | /** |
Tejun Heo | 6a62a04 | 2006-02-13 10:02:46 +0900 | [diff] [blame] | 1078 | * ata_id_c_string - Convert IDENTIFY DEVICE page into C string |
Tejun Heo | 0e949ff | 2006-02-12 22:47:04 +0900 | [diff] [blame] | 1079 | * @id: IDENTIFY DEVICE results we will examine |
| 1080 | * @s: string into which data is output |
| 1081 | * @ofs: offset into identify device page |
| 1082 | * @len: length of string to return. must be an odd number. |
| 1083 | * |
Tejun Heo | 6a62a04 | 2006-02-13 10:02:46 +0900 | [diff] [blame] | 1084 | * This function is identical to ata_id_string except that it |
Tejun Heo | 0e949ff | 2006-02-12 22:47:04 +0900 | [diff] [blame] | 1085 | * trims trailing spaces and terminates the resulting string with |
| 1086 | * null. @len must be actual maximum length (even number) + 1. |
| 1087 | * |
| 1088 | * LOCKING: |
| 1089 | * caller. |
| 1090 | */ |
Tejun Heo | 6a62a04 | 2006-02-13 10:02:46 +0900 | [diff] [blame] | 1091 | void ata_id_c_string(const u16 *id, unsigned char *s, |
| 1092 | unsigned int ofs, unsigned int len) |
Tejun Heo | 0e949ff | 2006-02-12 22:47:04 +0900 | [diff] [blame] | 1093 | { |
| 1094 | unsigned char *p; |
| 1095 | |
Tejun Heo | 6a62a04 | 2006-02-13 10:02:46 +0900 | [diff] [blame] | 1096 | ata_id_string(id, s, ofs, len - 1); |
Tejun Heo | 0e949ff | 2006-02-12 22:47:04 +0900 | [diff] [blame] | 1097 | |
| 1098 | p = s + strnlen(s, len - 1); |
| 1099 | while (p > s && p[-1] == ' ') |
| 1100 | p--; |
| 1101 | *p = '\0'; |
| 1102 | } |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 1103 | EXPORT_SYMBOL_GPL(ata_id_c_string); |
Edward Falk | 0baab86 | 2005-06-02 18:17:13 -0400 | [diff] [blame] | 1104 | |
Tejun Heo | db6f875 | 2007-09-03 12:31:58 +0900 | [diff] [blame] | 1105 | static u64 ata_id_n_sectors(const u16 *id) |
| 1106 | { |
| 1107 | if (ata_id_has_lba(id)) { |
| 1108 | if (ata_id_has_lba48(id)) |
Robert Hancock | 968e594 | 2009-02-16 20:15:08 -0600 | [diff] [blame] | 1109 | return ata_id_u64(id, ATA_ID_LBA_CAPACITY_2); |
Tejun Heo | db6f875 | 2007-09-03 12:31:58 +0900 | [diff] [blame] | 1110 | else |
Robert Hancock | 968e594 | 2009-02-16 20:15:08 -0600 | [diff] [blame] | 1111 | return ata_id_u32(id, ATA_ID_LBA_CAPACITY); |
Tejun Heo | db6f875 | 2007-09-03 12:31:58 +0900 | [diff] [blame] | 1112 | } else { |
| 1113 | if (ata_id_current_chs_valid(id)) |
Robert Hancock | 968e594 | 2009-02-16 20:15:08 -0600 | [diff] [blame] | 1114 | return id[ATA_ID_CUR_CYLS] * id[ATA_ID_CUR_HEADS] * |
| 1115 | id[ATA_ID_CUR_SECTORS]; |
Tejun Heo | db6f875 | 2007-09-03 12:31:58 +0900 | [diff] [blame] | 1116 | else |
Robert Hancock | 968e594 | 2009-02-16 20:15:08 -0600 | [diff] [blame] | 1117 | return id[ATA_ID_CYLS] * id[ATA_ID_HEADS] * |
| 1118 | id[ATA_ID_SECTORS]; |
Tejun Heo | db6f875 | 2007-09-03 12:31:58 +0900 | [diff] [blame] | 1119 | } |
| 1120 | } |
| 1121 | |
Tejun Heo | a5987e0 | 2008-03-27 19:14:23 +0900 | [diff] [blame] | 1122 | u64 ata_tf_to_lba48(const struct ata_taskfile *tf) |
Alan Cox | 1e99973 | 2007-04-11 00:23:13 +0100 | [diff] [blame] | 1123 | { |
| 1124 | u64 sectors = 0; |
| 1125 | |
| 1126 | sectors |= ((u64)(tf->hob_lbah & 0xff)) << 40; |
| 1127 | sectors |= ((u64)(tf->hob_lbam & 0xff)) << 32; |
Roland Dreier | ba14a9c | 2008-10-28 16:52:20 -0700 | [diff] [blame] | 1128 | sectors |= ((u64)(tf->hob_lbal & 0xff)) << 24; |
Alan Cox | 1e99973 | 2007-04-11 00:23:13 +0100 | [diff] [blame] | 1129 | sectors |= (tf->lbah & 0xff) << 16; |
| 1130 | sectors |= (tf->lbam & 0xff) << 8; |
| 1131 | sectors |= (tf->lbal & 0xff); |
| 1132 | |
Tejun Heo | a5987e0 | 2008-03-27 19:14:23 +0900 | [diff] [blame] | 1133 | return sectors; |
Alan Cox | 1e99973 | 2007-04-11 00:23:13 +0100 | [diff] [blame] | 1134 | } |
| 1135 | |
Tejun Heo | a5987e0 | 2008-03-27 19:14:23 +0900 | [diff] [blame] | 1136 | u64 ata_tf_to_lba(const struct ata_taskfile *tf) |
Alan Cox | 1e99973 | 2007-04-11 00:23:13 +0100 | [diff] [blame] | 1137 | { |
| 1138 | u64 sectors = 0; |
| 1139 | |
| 1140 | sectors |= (tf->device & 0x0f) << 24; |
| 1141 | sectors |= (tf->lbah & 0xff) << 16; |
| 1142 | sectors |= (tf->lbam & 0xff) << 8; |
| 1143 | sectors |= (tf->lbal & 0xff); |
| 1144 | |
Tejun Heo | a5987e0 | 2008-03-27 19:14:23 +0900 | [diff] [blame] | 1145 | return sectors; |
Alan Cox | 1e99973 | 2007-04-11 00:23:13 +0100 | [diff] [blame] | 1146 | } |
| 1147 | |
| 1148 | /** |
Tejun Heo | c728a91 | 2007-09-03 12:32:30 +0900 | [diff] [blame] | 1149 | * ata_read_native_max_address - Read native max address |
| 1150 | * @dev: target device |
| 1151 | * @max_sectors: out parameter for the result native max address |
Alan Cox | 1e99973 | 2007-04-11 00:23:13 +0100 | [diff] [blame] | 1152 | * |
Tejun Heo | c728a91 | 2007-09-03 12:32:30 +0900 | [diff] [blame] | 1153 | * Perform an LBA48 or LBA28 native size query upon the device in |
| 1154 | * question. |
| 1155 | * |
| 1156 | * RETURNS: |
| 1157 | * 0 on success, -EACCES if command is aborted by the drive. |
| 1158 | * -EIO on other errors. |
Alan Cox | 1e99973 | 2007-04-11 00:23:13 +0100 | [diff] [blame] | 1159 | */ |
Tejun Heo | c728a91 | 2007-09-03 12:32:30 +0900 | [diff] [blame] | 1160 | 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] | 1161 | { |
Tejun Heo | c728a91 | 2007-09-03 12:32:30 +0900 | [diff] [blame] | 1162 | unsigned int err_mask; |
Alan Cox | 1e99973 | 2007-04-11 00:23:13 +0100 | [diff] [blame] | 1163 | struct ata_taskfile tf; |
Tejun Heo | c728a91 | 2007-09-03 12:32:30 +0900 | [diff] [blame] | 1164 | int lba48 = ata_id_has_lba48(dev->id); |
Alan Cox | 1e99973 | 2007-04-11 00:23:13 +0100 | [diff] [blame] | 1165 | |
| 1166 | ata_tf_init(dev, &tf); |
| 1167 | |
Tejun Heo | c728a91 | 2007-09-03 12:32:30 +0900 | [diff] [blame] | 1168 | /* always clear all address registers */ |
Alan Cox | 1e99973 | 2007-04-11 00:23:13 +0100 | [diff] [blame] | 1169 | tf.flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR; |
Tejun Heo | c728a91 | 2007-09-03 12:32:30 +0900 | [diff] [blame] | 1170 | |
| 1171 | if (lba48) { |
| 1172 | tf.command = ATA_CMD_READ_NATIVE_MAX_EXT; |
| 1173 | tf.flags |= ATA_TFLAG_LBA48; |
| 1174 | } else |
| 1175 | tf.command = ATA_CMD_READ_NATIVE_MAX; |
| 1176 | |
Hannes Reinecke | bd18bc0 | 2016-07-14 09:05:46 +0900 | [diff] [blame] | 1177 | tf.protocol = ATA_PROT_NODATA; |
Tejun Heo | c728a91 | 2007-09-03 12:32:30 +0900 | [diff] [blame] | 1178 | tf.device |= ATA_LBA; |
Alan Cox | 1e99973 | 2007-04-11 00:23:13 +0100 | [diff] [blame] | 1179 | |
Tejun Heo | 2b78910 | 2007-10-09 15:05:44 +0900 | [diff] [blame] | 1180 | 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] | 1181 | if (err_mask) { |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 1182 | ata_dev_warn(dev, |
| 1183 | "failed to read native max address (err_mask=0x%x)\n", |
| 1184 | err_mask); |
Tejun Heo | c728a91 | 2007-09-03 12:32:30 +0900 | [diff] [blame] | 1185 | if (err_mask == AC_ERR_DEV && (tf.feature & ATA_ABORTED)) |
| 1186 | return -EACCES; |
| 1187 | return -EIO; |
| 1188 | } |
Alan Cox | 1e99973 | 2007-04-11 00:23:13 +0100 | [diff] [blame] | 1189 | |
Tejun Heo | c728a91 | 2007-09-03 12:32:30 +0900 | [diff] [blame] | 1190 | if (lba48) |
Tejun Heo | a5987e0 | 2008-03-27 19:14:23 +0900 | [diff] [blame] | 1191 | *max_sectors = ata_tf_to_lba48(&tf) + 1; |
Tejun Heo | c728a91 | 2007-09-03 12:32:30 +0900 | [diff] [blame] | 1192 | else |
Tejun Heo | a5987e0 | 2008-03-27 19:14:23 +0900 | [diff] [blame] | 1193 | *max_sectors = ata_tf_to_lba(&tf) + 1; |
Jeff Garzik | 2dcb407 | 2007-10-19 06:42:56 -0400 | [diff] [blame] | 1194 | if (dev->horkage & ATA_HORKAGE_HPA_SIZE) |
Alan Cox | 93328e11 | 2007-09-29 04:06:48 -0400 | [diff] [blame] | 1195 | (*max_sectors)--; |
Tejun Heo | c728a91 | 2007-09-03 12:32:30 +0900 | [diff] [blame] | 1196 | return 0; |
Alan Cox | 1e99973 | 2007-04-11 00:23:13 +0100 | [diff] [blame] | 1197 | } |
| 1198 | |
| 1199 | /** |
Tejun Heo | c728a91 | 2007-09-03 12:32:30 +0900 | [diff] [blame] | 1200 | * ata_set_max_sectors - Set max sectors |
| 1201 | * @dev: target device |
Randy Dunlap | 6b38d1d | 2007-05-01 17:35:55 -0700 | [diff] [blame] | 1202 | * @new_sectors: new max sectors value to set for the device |
Alan Cox | 1e99973 | 2007-04-11 00:23:13 +0100 | [diff] [blame] | 1203 | * |
Tejun Heo | c728a91 | 2007-09-03 12:32:30 +0900 | [diff] [blame] | 1204 | * Set max sectors of @dev to @new_sectors. |
| 1205 | * |
| 1206 | * RETURNS: |
| 1207 | * 0 on success, -EACCES if command is aborted or denied (due to |
| 1208 | * previous non-volatile SET_MAX) by the drive. -EIO on other |
| 1209 | * errors. |
Alan Cox | 1e99973 | 2007-04-11 00:23:13 +0100 | [diff] [blame] | 1210 | */ |
Tejun Heo | 05027ad | 2007-09-03 12:32:57 +0900 | [diff] [blame] | 1211 | 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] | 1212 | { |
Tejun Heo | c728a91 | 2007-09-03 12:32:30 +0900 | [diff] [blame] | 1213 | unsigned int err_mask; |
Alan Cox | 1e99973 | 2007-04-11 00:23:13 +0100 | [diff] [blame] | 1214 | struct ata_taskfile tf; |
Tejun Heo | c728a91 | 2007-09-03 12:32:30 +0900 | [diff] [blame] | 1215 | int lba48 = ata_id_has_lba48(dev->id); |
Alan Cox | 1e99973 | 2007-04-11 00:23:13 +0100 | [diff] [blame] | 1216 | |
| 1217 | new_sectors--; |
| 1218 | |
| 1219 | ata_tf_init(dev, &tf); |
| 1220 | |
Alan Cox | 1e99973 | 2007-04-11 00:23:13 +0100 | [diff] [blame] | 1221 | tf.flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR; |
Tejun Heo | c728a91 | 2007-09-03 12:32:30 +0900 | [diff] [blame] | 1222 | |
| 1223 | if (lba48) { |
| 1224 | tf.command = ATA_CMD_SET_MAX_EXT; |
| 1225 | tf.flags |= ATA_TFLAG_LBA48; |
| 1226 | |
| 1227 | tf.hob_lbal = (new_sectors >> 24) & 0xff; |
| 1228 | tf.hob_lbam = (new_sectors >> 32) & 0xff; |
| 1229 | tf.hob_lbah = (new_sectors >> 40) & 0xff; |
Tejun Heo | 1e582ba | 2007-09-21 20:07:14 +0900 | [diff] [blame] | 1230 | } else { |
Tejun Heo | c728a91 | 2007-09-03 12:32:30 +0900 | [diff] [blame] | 1231 | tf.command = ATA_CMD_SET_MAX; |
| 1232 | |
Tejun Heo | 1e582ba | 2007-09-21 20:07:14 +0900 | [diff] [blame] | 1233 | tf.device |= (new_sectors >> 24) & 0xf; |
| 1234 | } |
| 1235 | |
Hannes Reinecke | bd18bc0 | 2016-07-14 09:05:46 +0900 | [diff] [blame] | 1236 | tf.protocol = ATA_PROT_NODATA; |
Tejun Heo | c728a91 | 2007-09-03 12:32:30 +0900 | [diff] [blame] | 1237 | tf.device |= ATA_LBA; |
Alan Cox | 1e99973 | 2007-04-11 00:23:13 +0100 | [diff] [blame] | 1238 | |
| 1239 | tf.lbal = (new_sectors >> 0) & 0xff; |
| 1240 | tf.lbam = (new_sectors >> 8) & 0xff; |
| 1241 | tf.lbah = (new_sectors >> 16) & 0xff; |
Alan Cox | 1e99973 | 2007-04-11 00:23:13 +0100 | [diff] [blame] | 1242 | |
Tejun Heo | 2b78910 | 2007-10-09 15:05:44 +0900 | [diff] [blame] | 1243 | 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] | 1244 | if (err_mask) { |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 1245 | ata_dev_warn(dev, |
| 1246 | "failed to set max address (err_mask=0x%x)\n", |
| 1247 | err_mask); |
Tejun Heo | c728a91 | 2007-09-03 12:32:30 +0900 | [diff] [blame] | 1248 | if (err_mask == AC_ERR_DEV && |
| 1249 | (tf.feature & (ATA_ABORTED | ATA_IDNF))) |
| 1250 | return -EACCES; |
| 1251 | return -EIO; |
| 1252 | } |
Alan Cox | 1e99973 | 2007-04-11 00:23:13 +0100 | [diff] [blame] | 1253 | |
Tejun Heo | c728a91 | 2007-09-03 12:32:30 +0900 | [diff] [blame] | 1254 | return 0; |
Alan Cox | 1e99973 | 2007-04-11 00:23:13 +0100 | [diff] [blame] | 1255 | } |
| 1256 | |
| 1257 | /** |
| 1258 | * ata_hpa_resize - Resize a device with an HPA set |
| 1259 | * @dev: Device to resize |
| 1260 | * |
| 1261 | * Read the size of an LBA28 or LBA48 disk with HPA features and resize |
| 1262 | * it if required to the full size of the media. The caller must check |
| 1263 | * the drive has the HPA feature set enabled. |
Tejun Heo | 05027ad | 2007-09-03 12:32:57 +0900 | [diff] [blame] | 1264 | * |
| 1265 | * RETURNS: |
| 1266 | * 0 on success, -errno on failure. |
Alan Cox | 1e99973 | 2007-04-11 00:23:13 +0100 | [diff] [blame] | 1267 | */ |
Tejun Heo | 05027ad | 2007-09-03 12:32:57 +0900 | [diff] [blame] | 1268 | static int ata_hpa_resize(struct ata_device *dev) |
Alan Cox | 1e99973 | 2007-04-11 00:23:13 +0100 | [diff] [blame] | 1269 | { |
Tejun Heo | 05027ad | 2007-09-03 12:32:57 +0900 | [diff] [blame] | 1270 | struct ata_eh_context *ehc = &dev->link->eh_context; |
| 1271 | int print_info = ehc->i.flags & ATA_EHI_PRINTINFO; |
Tejun Heo | 445d211 | 2010-04-05 10:33:13 +0900 | [diff] [blame] | 1272 | bool unlock_hpa = ata_ignore_hpa || dev->flags & ATA_DFLAG_UNLOCK_HPA; |
Tejun Heo | 05027ad | 2007-09-03 12:32:57 +0900 | [diff] [blame] | 1273 | u64 sectors = ata_id_n_sectors(dev->id); |
| 1274 | u64 native_sectors; |
Tejun Heo | c728a91 | 2007-09-03 12:32:30 +0900 | [diff] [blame] | 1275 | int rc; |
Jeff Garzik | a617c09 | 2007-05-21 20:14:23 -0400 | [diff] [blame] | 1276 | |
Tejun Heo | 05027ad | 2007-09-03 12:32:57 +0900 | [diff] [blame] | 1277 | /* do we need to do it? */ |
Hannes Reinecke | 9162c65 | 2014-11-05 13:08:21 +0100 | [diff] [blame] | 1278 | if ((dev->class != ATA_DEV_ATA && dev->class != ATA_DEV_ZAC) || |
Tejun Heo | 05027ad | 2007-09-03 12:32:57 +0900 | [diff] [blame] | 1279 | !ata_id_has_lba(dev->id) || !ata_id_hpa_enabled(dev->id) || |
| 1280 | (dev->horkage & ATA_HORKAGE_BROKEN_HPA)) |
Tejun Heo | c728a91 | 2007-09-03 12:32:30 +0900 | [diff] [blame] | 1281 | return 0; |
Alan Cox | 1e99973 | 2007-04-11 00:23:13 +0100 | [diff] [blame] | 1282 | |
Tejun Heo | 05027ad | 2007-09-03 12:32:57 +0900 | [diff] [blame] | 1283 | /* read native max address */ |
| 1284 | rc = ata_read_native_max_address(dev, &native_sectors); |
| 1285 | if (rc) { |
Tejun Heo | dda7aba | 2008-03-23 21:05:15 +0900 | [diff] [blame] | 1286 | /* If device aborted the command or HPA isn't going to |
| 1287 | * be unlocked, skip HPA resizing. |
Tejun Heo | 05027ad | 2007-09-03 12:32:57 +0900 | [diff] [blame] | 1288 | */ |
Tejun Heo | 445d211 | 2010-04-05 10:33:13 +0900 | [diff] [blame] | 1289 | if (rc == -EACCES || !unlock_hpa) { |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 1290 | ata_dev_warn(dev, |
| 1291 | "HPA support seems broken, skipping HPA handling\n"); |
Tejun Heo | 05027ad | 2007-09-03 12:32:57 +0900 | [diff] [blame] | 1292 | dev->horkage |= ATA_HORKAGE_BROKEN_HPA; |
Alan Cox | 1e99973 | 2007-04-11 00:23:13 +0100 | [diff] [blame] | 1293 | |
Tejun Heo | 05027ad | 2007-09-03 12:32:57 +0900 | [diff] [blame] | 1294 | /* we can continue if device aborted the command */ |
| 1295 | if (rc == -EACCES) |
| 1296 | rc = 0; |
Alan Cox | 1e99973 | 2007-04-11 00:23:13 +0100 | [diff] [blame] | 1297 | } |
Tejun Heo | 37301a5 | 2007-06-25 20:45:54 +0900 | [diff] [blame] | 1298 | |
Tejun Heo | 05027ad | 2007-09-03 12:32:57 +0900 | [diff] [blame] | 1299 | return rc; |
| 1300 | } |
Tejun Heo | 5920dad | 2009-07-15 17:11:41 +0900 | [diff] [blame] | 1301 | dev->n_native_sectors = native_sectors; |
Tejun Heo | 05027ad | 2007-09-03 12:32:57 +0900 | [diff] [blame] | 1302 | |
| 1303 | /* nothing to do? */ |
Tejun Heo | 445d211 | 2010-04-05 10:33:13 +0900 | [diff] [blame] | 1304 | if (native_sectors <= sectors || !unlock_hpa) { |
Tejun Heo | 05027ad | 2007-09-03 12:32:57 +0900 | [diff] [blame] | 1305 | if (!print_info || native_sectors == sectors) |
| 1306 | return 0; |
| 1307 | |
| 1308 | if (native_sectors > sectors) |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 1309 | ata_dev_info(dev, |
Tejun Heo | 05027ad | 2007-09-03 12:32:57 +0900 | [diff] [blame] | 1310 | "HPA detected: current %llu, native %llu\n", |
| 1311 | (unsigned long long)sectors, |
| 1312 | (unsigned long long)native_sectors); |
| 1313 | else if (native_sectors < sectors) |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 1314 | ata_dev_warn(dev, |
| 1315 | "native sectors (%llu) is smaller than sectors (%llu)\n", |
Tejun Heo | 05027ad | 2007-09-03 12:32:57 +0900 | [diff] [blame] | 1316 | (unsigned long long)native_sectors, |
| 1317 | (unsigned long long)sectors); |
| 1318 | return 0; |
| 1319 | } |
| 1320 | |
| 1321 | /* let's unlock HPA */ |
| 1322 | rc = ata_set_max_sectors(dev, native_sectors); |
| 1323 | if (rc == -EACCES) { |
| 1324 | /* if device aborted the command, skip HPA resizing */ |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 1325 | ata_dev_warn(dev, |
| 1326 | "device aborted resize (%llu -> %llu), skipping HPA handling\n", |
| 1327 | (unsigned long long)sectors, |
| 1328 | (unsigned long long)native_sectors); |
Tejun Heo | 05027ad | 2007-09-03 12:32:57 +0900 | [diff] [blame] | 1329 | dev->horkage |= ATA_HORKAGE_BROKEN_HPA; |
| 1330 | return 0; |
| 1331 | } else if (rc) |
| 1332 | return rc; |
| 1333 | |
| 1334 | /* re-read IDENTIFY data */ |
| 1335 | rc = ata_dev_reread_id(dev, 0); |
| 1336 | if (rc) { |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 1337 | ata_dev_err(dev, |
| 1338 | "failed to re-read IDENTIFY data after HPA resizing\n"); |
Tejun Heo | 05027ad | 2007-09-03 12:32:57 +0900 | [diff] [blame] | 1339 | return rc; |
| 1340 | } |
| 1341 | |
| 1342 | if (print_info) { |
| 1343 | u64 new_sectors = ata_id_n_sectors(dev->id); |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 1344 | ata_dev_info(dev, |
Tejun Heo | 05027ad | 2007-09-03 12:32:57 +0900 | [diff] [blame] | 1345 | "HPA unlocked: %llu -> %llu, native %llu\n", |
| 1346 | (unsigned long long)sectors, |
| 1347 | (unsigned long long)new_sectors, |
| 1348 | (unsigned long long)native_sectors); |
| 1349 | } |
| 1350 | |
| 1351 | return 0; |
Alan Cox | 1e99973 | 2007-04-11 00:23:13 +0100 | [diff] [blame] | 1352 | } |
| 1353 | |
Edward Falk | 0baab86 | 2005-06-02 18:17:13 -0400 | [diff] [blame] | 1354 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1355 | * ata_dump_id - IDENTIFY DEVICE info debugging output |
Tejun Heo | 0bd3300 | 2006-02-12 22:47:05 +0900 | [diff] [blame] | 1356 | * @id: IDENTIFY DEVICE page to dump |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1357 | * |
Tejun Heo | 0bd3300 | 2006-02-12 22:47:05 +0900 | [diff] [blame] | 1358 | * Dump selected 16-bit words from the given IDENTIFY DEVICE |
| 1359 | * page. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1360 | * |
| 1361 | * LOCKING: |
| 1362 | * caller. |
| 1363 | */ |
| 1364 | |
Tejun Heo | 0bd3300 | 2006-02-12 22:47:05 +0900 | [diff] [blame] | 1365 | static inline void ata_dump_id(const u16 *id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1366 | { |
| 1367 | DPRINTK("49==0x%04x " |
| 1368 | "53==0x%04x " |
| 1369 | "63==0x%04x " |
| 1370 | "64==0x%04x " |
| 1371 | "75==0x%04x \n", |
Tejun Heo | 0bd3300 | 2006-02-12 22:47:05 +0900 | [diff] [blame] | 1372 | id[49], |
| 1373 | id[53], |
| 1374 | id[63], |
| 1375 | id[64], |
| 1376 | id[75]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1377 | DPRINTK("80==0x%04x " |
| 1378 | "81==0x%04x " |
| 1379 | "82==0x%04x " |
| 1380 | "83==0x%04x " |
| 1381 | "84==0x%04x \n", |
Tejun Heo | 0bd3300 | 2006-02-12 22:47:05 +0900 | [diff] [blame] | 1382 | id[80], |
| 1383 | id[81], |
| 1384 | id[82], |
| 1385 | id[83], |
| 1386 | id[84]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1387 | DPRINTK("88==0x%04x " |
| 1388 | "93==0x%04x\n", |
Tejun Heo | 0bd3300 | 2006-02-12 22:47:05 +0900 | [diff] [blame] | 1389 | id[88], |
| 1390 | id[93]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1391 | } |
| 1392 | |
Tejun Heo | cb95d56 | 2006-03-06 04:31:56 +0900 | [diff] [blame] | 1393 | /** |
| 1394 | * ata_id_xfermask - Compute xfermask from the given IDENTIFY data |
| 1395 | * @id: IDENTIFY data to compute xfer mask from |
| 1396 | * |
| 1397 | * Compute the xfermask for this device. This is not as trivial |
| 1398 | * as it seems if we must consider early devices correctly. |
| 1399 | * |
| 1400 | * FIXME: pre IDE drive timing (do we care ?). |
| 1401 | * |
| 1402 | * LOCKING: |
| 1403 | * None. |
| 1404 | * |
| 1405 | * RETURNS: |
| 1406 | * Computed xfermask |
| 1407 | */ |
Tejun Heo | 7dc951a | 2007-11-27 19:43:42 +0900 | [diff] [blame] | 1408 | unsigned long ata_id_xfermask(const u16 *id) |
Tejun Heo | cb95d56 | 2006-03-06 04:31:56 +0900 | [diff] [blame] | 1409 | { |
Tejun Heo | 7dc951a | 2007-11-27 19:43:42 +0900 | [diff] [blame] | 1410 | unsigned long pio_mask, mwdma_mask, udma_mask; |
Tejun Heo | cb95d56 | 2006-03-06 04:31:56 +0900 | [diff] [blame] | 1411 | |
| 1412 | /* Usual case. Word 53 indicates word 64 is valid */ |
| 1413 | if (id[ATA_ID_FIELD_VALID] & (1 << 1)) { |
| 1414 | pio_mask = id[ATA_ID_PIO_MODES] & 0x03; |
| 1415 | pio_mask <<= 3; |
| 1416 | pio_mask |= 0x7; |
| 1417 | } else { |
| 1418 | /* If word 64 isn't valid then Word 51 high byte holds |
| 1419 | * the PIO timing number for the maximum. Turn it into |
| 1420 | * a mask. |
| 1421 | */ |
Lennert Buytenhek | 7a0f1c8 | 2007-01-29 13:28:47 +0100 | [diff] [blame] | 1422 | u8 mode = (id[ATA_ID_OLD_PIO_MODES] >> 8) & 0xFF; |
Alan Cox | 46767aeb | 2006-09-29 18:26:47 +0100 | [diff] [blame] | 1423 | if (mode < 5) /* Valid PIO range */ |
Jeff Garzik | 2dcb407 | 2007-10-19 06:42:56 -0400 | [diff] [blame] | 1424 | pio_mask = (2 << mode) - 1; |
Alan Cox | 46767aeb | 2006-09-29 18:26:47 +0100 | [diff] [blame] | 1425 | else |
| 1426 | pio_mask = 1; |
Tejun Heo | cb95d56 | 2006-03-06 04:31:56 +0900 | [diff] [blame] | 1427 | |
| 1428 | /* But wait.. there's more. Design your standards by |
| 1429 | * committee and you too can get a free iordy field to |
| 1430 | * process. However its the speeds not the modes that |
| 1431 | * are supported... Note drivers using the timing API |
| 1432 | * will get this right anyway |
| 1433 | */ |
| 1434 | } |
| 1435 | |
| 1436 | mwdma_mask = id[ATA_ID_MWDMA_MODES] & 0x07; |
Tejun Heo | fb21f0d | 2006-03-12 12:34:35 +0900 | [diff] [blame] | 1437 | |
Alan Cox | b352e57 | 2006-08-10 18:52:12 +0100 | [diff] [blame] | 1438 | if (ata_id_is_cfa(id)) { |
| 1439 | /* |
| 1440 | * Process compact flash extended modes |
| 1441 | */ |
Sergei Shtylyov | 62afe5d | 2009-04-13 20:50:00 +0400 | [diff] [blame] | 1442 | int pio = (id[ATA_ID_CFA_MODES] >> 0) & 0x7; |
| 1443 | int dma = (id[ATA_ID_CFA_MODES] >> 3) & 0x7; |
Alan Cox | b352e57 | 2006-08-10 18:52:12 +0100 | [diff] [blame] | 1444 | |
| 1445 | if (pio) |
| 1446 | pio_mask |= (1 << 5); |
| 1447 | if (pio > 1) |
| 1448 | pio_mask |= (1 << 6); |
| 1449 | if (dma) |
| 1450 | mwdma_mask |= (1 << 3); |
| 1451 | if (dma > 1) |
| 1452 | mwdma_mask |= (1 << 4); |
| 1453 | } |
| 1454 | |
Tejun Heo | fb21f0d | 2006-03-12 12:34:35 +0900 | [diff] [blame] | 1455 | udma_mask = 0; |
| 1456 | if (id[ATA_ID_FIELD_VALID] & (1 << 2)) |
| 1457 | udma_mask = id[ATA_ID_UDMA_MODES] & 0xff; |
Tejun Heo | cb95d56 | 2006-03-06 04:31:56 +0900 | [diff] [blame] | 1458 | |
| 1459 | return ata_pack_xfermask(pio_mask, mwdma_mask, udma_mask); |
| 1460 | } |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 1461 | EXPORT_SYMBOL_GPL(ata_id_xfermask); |
Tejun Heo | cb95d56 | 2006-03-06 04:31:56 +0900 | [diff] [blame] | 1462 | |
Adrian Bunk | 7102d23 | 2007-01-04 00:09:36 +0100 | [diff] [blame] | 1463 | static void ata_qc_complete_internal(struct ata_queued_cmd *qc) |
Tejun Heo | a2a7a66 | 2005-12-13 14:48:31 +0900 | [diff] [blame] | 1464 | { |
Tejun Heo | 77853bf | 2006-01-23 13:09:36 +0900 | [diff] [blame] | 1465 | struct completion *waiting = qc->private_data; |
Tejun Heo | a2a7a66 | 2005-12-13 14:48:31 +0900 | [diff] [blame] | 1466 | |
Tejun Heo | a2a7a66 | 2005-12-13 14:48:31 +0900 | [diff] [blame] | 1467 | complete(waiting); |
Tejun Heo | a2a7a66 | 2005-12-13 14:48:31 +0900 | [diff] [blame] | 1468 | } |
| 1469 | |
| 1470 | /** |
Tejun Heo | 2432697 | 2006-11-14 22:47:09 +0900 | [diff] [blame] | 1471 | * ata_exec_internal_sg - execute libata internal command |
Tejun Heo | a2a7a66 | 2005-12-13 14:48:31 +0900 | [diff] [blame] | 1472 | * @dev: Device to which the command is sent |
| 1473 | * @tf: Taskfile registers for the command and the result |
Tejun Heo | d69cf37 | 2006-04-02 18:51:53 +0900 | [diff] [blame] | 1474 | * @cdb: CDB for packet command |
Masanari Iida | e227867 | 2014-02-18 22:54:36 +0900 | [diff] [blame] | 1475 | * @dma_dir: Data transfer direction of the command |
Randy Dunlap | 5c1ad8b | 2007-10-18 14:12:26 -0700 | [diff] [blame] | 1476 | * @sgl: sg list for the data buffer of the command |
Tejun Heo | 2432697 | 2006-11-14 22:47:09 +0900 | [diff] [blame] | 1477 | * @n_elem: Number of sg entries |
Tejun Heo | 2b78910 | 2007-10-09 15:05:44 +0900 | [diff] [blame] | 1478 | * @timeout: Timeout in msecs (0 for default) |
Tejun Heo | a2a7a66 | 2005-12-13 14:48:31 +0900 | [diff] [blame] | 1479 | * |
| 1480 | * Executes libata internal command with timeout. @tf contains |
| 1481 | * command on entry and result on return. Timeout and error |
| 1482 | * conditions are reported via return value. No recovery action |
| 1483 | * is taken after a command times out. It's caller's duty to |
| 1484 | * clean up after timeout. |
| 1485 | * |
| 1486 | * LOCKING: |
| 1487 | * None. Should be called with kernel context, might sleep. |
Tejun Heo | 551e888 | 2006-06-12 14:09:49 +0900 | [diff] [blame] | 1488 | * |
| 1489 | * RETURNS: |
| 1490 | * Zero on success, AC_ERR_* mask on failure |
Tejun Heo | a2a7a66 | 2005-12-13 14:48:31 +0900 | [diff] [blame] | 1491 | */ |
Tejun Heo | 2432697 | 2006-11-14 22:47:09 +0900 | [diff] [blame] | 1492 | unsigned ata_exec_internal_sg(struct ata_device *dev, |
| 1493 | struct ata_taskfile *tf, const u8 *cdb, |
Jens Axboe | 8726021 | 2007-10-16 11:14:12 +0200 | [diff] [blame] | 1494 | int dma_dir, struct scatterlist *sgl, |
Tejun Heo | 2b78910 | 2007-10-09 15:05:44 +0900 | [diff] [blame] | 1495 | unsigned int n_elem, unsigned long timeout) |
Tejun Heo | a2a7a66 | 2005-12-13 14:48:31 +0900 | [diff] [blame] | 1496 | { |
Tejun Heo | 9af5c9c | 2007-08-06 18:36:22 +0900 | [diff] [blame] | 1497 | struct ata_link *link = dev->link; |
| 1498 | struct ata_port *ap = link->ap; |
Tejun Heo | a2a7a66 | 2005-12-13 14:48:31 +0900 | [diff] [blame] | 1499 | u8 command = tf->command; |
Tejun Heo | 87fbc5a | 2008-05-20 02:17:54 +0900 | [diff] [blame] | 1500 | int auto_timeout = 0; |
Tejun Heo | a2a7a66 | 2005-12-13 14:48:31 +0900 | [diff] [blame] | 1501 | struct ata_queued_cmd *qc; |
Jens Axboe | 28361c4 | 2018-05-11 12:51:09 -0600 | [diff] [blame] | 1502 | unsigned int preempted_tag; |
Jens Axboe | e3ed8939 | 2018-05-11 12:51:05 -0600 | [diff] [blame] | 1503 | u32 preempted_sactive; |
| 1504 | u64 preempted_qc_active; |
Tejun Heo | da917d6 | 2007-09-23 13:14:12 +0900 | [diff] [blame] | 1505 | int preempted_nr_active_links; |
Ingo Molnar | 60be6b9 | 2006-07-03 00:25:26 -0700 | [diff] [blame] | 1506 | DECLARE_COMPLETION_ONSTACK(wait); |
Tejun Heo | a2a7a66 | 2005-12-13 14:48:31 +0900 | [diff] [blame] | 1507 | unsigned long flags; |
Tejun Heo | 77853bf | 2006-01-23 13:09:36 +0900 | [diff] [blame] | 1508 | unsigned int err_mask; |
Tejun Heo | d95a717 | 2006-05-15 20:58:14 +0900 | [diff] [blame] | 1509 | int rc; |
Tejun Heo | a2a7a66 | 2005-12-13 14:48:31 +0900 | [diff] [blame] | 1510 | |
Jeff Garzik | ba6a130 | 2006-06-22 23:46:10 -0400 | [diff] [blame] | 1511 | spin_lock_irqsave(ap->lock, flags); |
Tejun Heo | a2a7a66 | 2005-12-13 14:48:31 +0900 | [diff] [blame] | 1512 | |
Tejun Heo | e318049 | 2006-05-15 20:58:09 +0900 | [diff] [blame] | 1513 | /* no internal command while frozen */ |
Tejun Heo | b51e9e5 | 2006-06-29 01:29:30 +0900 | [diff] [blame] | 1514 | if (ap->pflags & ATA_PFLAG_FROZEN) { |
Jeff Garzik | ba6a130 | 2006-06-22 23:46:10 -0400 | [diff] [blame] | 1515 | spin_unlock_irqrestore(ap->lock, flags); |
Tejun Heo | e318049 | 2006-05-15 20:58:09 +0900 | [diff] [blame] | 1516 | return AC_ERR_SYSTEM; |
| 1517 | } |
| 1518 | |
Tejun Heo | 2ab7db1 | 2006-05-15 20:58:02 +0900 | [diff] [blame] | 1519 | /* initialize internal qc */ |
Jens Axboe | 28361c4 | 2018-05-11 12:51:09 -0600 | [diff] [blame] | 1520 | qc = __ata_qc_from_tag(ap, ATA_TAG_INTERNAL); |
Tejun Heo | a2a7a66 | 2005-12-13 14:48:31 +0900 | [diff] [blame] | 1521 | |
Jens Axboe | 28361c4 | 2018-05-11 12:51:09 -0600 | [diff] [blame] | 1522 | qc->tag = ATA_TAG_INTERNAL; |
| 1523 | qc->hw_tag = 0; |
Tejun Heo | 2ab7db1 | 2006-05-15 20:58:02 +0900 | [diff] [blame] | 1524 | qc->scsicmd = NULL; |
| 1525 | qc->ap = ap; |
| 1526 | qc->dev = dev; |
| 1527 | ata_qc_reinit(qc); |
| 1528 | |
Tejun Heo | 9af5c9c | 2007-08-06 18:36:22 +0900 | [diff] [blame] | 1529 | preempted_tag = link->active_tag; |
| 1530 | preempted_sactive = link->sactive; |
Tejun Heo | dedaf2b | 2006-05-15 21:03:43 +0900 | [diff] [blame] | 1531 | preempted_qc_active = ap->qc_active; |
Tejun Heo | da917d6 | 2007-09-23 13:14:12 +0900 | [diff] [blame] | 1532 | preempted_nr_active_links = ap->nr_active_links; |
Tejun Heo | 9af5c9c | 2007-08-06 18:36:22 +0900 | [diff] [blame] | 1533 | link->active_tag = ATA_TAG_POISON; |
| 1534 | link->sactive = 0; |
Tejun Heo | dedaf2b | 2006-05-15 21:03:43 +0900 | [diff] [blame] | 1535 | ap->qc_active = 0; |
Tejun Heo | da917d6 | 2007-09-23 13:14:12 +0900 | [diff] [blame] | 1536 | ap->nr_active_links = 0; |
Tejun Heo | 2ab7db1 | 2006-05-15 20:58:02 +0900 | [diff] [blame] | 1537 | |
| 1538 | /* prepare & issue qc */ |
Tejun Heo | a2a7a66 | 2005-12-13 14:48:31 +0900 | [diff] [blame] | 1539 | qc->tf = *tf; |
Tejun Heo | d69cf37 | 2006-04-02 18:51:53 +0900 | [diff] [blame] | 1540 | if (cdb) |
| 1541 | memcpy(qc->cdb, cdb, ATAPI_CDB_LEN); |
Vincent Pelletier | e771451 | 2013-05-18 18:44:04 +0200 | [diff] [blame] | 1542 | |
| 1543 | /* some SATA bridges need us to indicate data xfer direction */ |
| 1544 | if (tf->protocol == ATAPI_PROT_DMA && (dev->flags & ATA_DFLAG_DMADIR) && |
| 1545 | dma_dir == DMA_FROM_DEVICE) |
| 1546 | qc->tf.feature |= ATAPI_DMADIR; |
| 1547 | |
Tejun Heo | e61e067 | 2006-05-15 20:57:40 +0900 | [diff] [blame] | 1548 | qc->flags |= ATA_QCFLAG_RESULT_TF; |
Tejun Heo | a2a7a66 | 2005-12-13 14:48:31 +0900 | [diff] [blame] | 1549 | qc->dma_dir = dma_dir; |
| 1550 | if (dma_dir != DMA_NONE) { |
Tejun Heo | 2432697 | 2006-11-14 22:47:09 +0900 | [diff] [blame] | 1551 | unsigned int i, buflen = 0; |
Jens Axboe | 8726021 | 2007-10-16 11:14:12 +0200 | [diff] [blame] | 1552 | struct scatterlist *sg; |
Tejun Heo | 2432697 | 2006-11-14 22:47:09 +0900 | [diff] [blame] | 1553 | |
Jens Axboe | 8726021 | 2007-10-16 11:14:12 +0200 | [diff] [blame] | 1554 | for_each_sg(sgl, sg, n_elem, i) |
| 1555 | buflen += sg->length; |
Tejun Heo | 2432697 | 2006-11-14 22:47:09 +0900 | [diff] [blame] | 1556 | |
Jens Axboe | 8726021 | 2007-10-16 11:14:12 +0200 | [diff] [blame] | 1557 | ata_sg_init(qc, sgl, n_elem); |
Brian King | 49c8042 | 2007-01-30 11:32:26 -0600 | [diff] [blame] | 1558 | qc->nbytes = buflen; |
Tejun Heo | a2a7a66 | 2005-12-13 14:48:31 +0900 | [diff] [blame] | 1559 | } |
| 1560 | |
Tejun Heo | 77853bf | 2006-01-23 13:09:36 +0900 | [diff] [blame] | 1561 | qc->private_data = &wait; |
Tejun Heo | a2a7a66 | 2005-12-13 14:48:31 +0900 | [diff] [blame] | 1562 | qc->complete_fn = ata_qc_complete_internal; |
| 1563 | |
Tejun Heo | 8e0e694 | 2006-03-31 20:41:11 +0900 | [diff] [blame] | 1564 | ata_qc_issue(qc); |
Tejun Heo | a2a7a66 | 2005-12-13 14:48:31 +0900 | [diff] [blame] | 1565 | |
Jeff Garzik | ba6a130 | 2006-06-22 23:46:10 -0400 | [diff] [blame] | 1566 | spin_unlock_irqrestore(ap->lock, flags); |
Tejun Heo | a2a7a66 | 2005-12-13 14:48:31 +0900 | [diff] [blame] | 1567 | |
Tejun Heo | 87fbc5a | 2008-05-20 02:17:54 +0900 | [diff] [blame] | 1568 | if (!timeout) { |
| 1569 | if (ata_probe_timeout) |
| 1570 | timeout = ata_probe_timeout * 1000; |
| 1571 | else { |
| 1572 | timeout = ata_internal_cmd_timeout(dev, command); |
| 1573 | auto_timeout = 1; |
| 1574 | } |
| 1575 | } |
Tejun Heo | 2b78910 | 2007-10-09 15:05:44 +0900 | [diff] [blame] | 1576 | |
Tejun Heo | c0c362b | 2010-09-06 17:57:14 +0200 | [diff] [blame] | 1577 | if (ap->ops->error_handler) |
| 1578 | ata_eh_release(ap); |
| 1579 | |
Tejun Heo | 2b78910 | 2007-10-09 15:05:44 +0900 | [diff] [blame] | 1580 | rc = wait_for_completion_timeout(&wait, msecs_to_jiffies(timeout)); |
Albert Lee | 41ade50 | 2006-03-14 11:19:04 +0800 | [diff] [blame] | 1581 | |
Tejun Heo | c0c362b | 2010-09-06 17:57:14 +0200 | [diff] [blame] | 1582 | if (ap->ops->error_handler) |
| 1583 | ata_eh_acquire(ap); |
| 1584 | |
Tejun Heo | c429137 | 2010-05-10 21:41:38 +0200 | [diff] [blame] | 1585 | ata_sff_flush_pio_task(ap); |
Tejun Heo | d95a717 | 2006-05-15 20:58:14 +0900 | [diff] [blame] | 1586 | |
| 1587 | if (!rc) { |
Jeff Garzik | ba6a130 | 2006-06-22 23:46:10 -0400 | [diff] [blame] | 1588 | spin_lock_irqsave(ap->lock, flags); |
Tejun Heo | a2a7a66 | 2005-12-13 14:48:31 +0900 | [diff] [blame] | 1589 | |
| 1590 | /* We're racing with irq here. If we lose, the |
| 1591 | * following test prevents us from completing the qc |
Tejun Heo | d95a717 | 2006-05-15 20:58:14 +0900 | [diff] [blame] | 1592 | * twice. If we win, the port is frozen and will be |
| 1593 | * cleaned up by ->post_internal_cmd(). |
Tejun Heo | a2a7a66 | 2005-12-13 14:48:31 +0900 | [diff] [blame] | 1594 | */ |
Tejun Heo | 77853bf | 2006-01-23 13:09:36 +0900 | [diff] [blame] | 1595 | if (qc->flags & ATA_QCFLAG_ACTIVE) { |
Tejun Heo | d95a717 | 2006-05-15 20:58:14 +0900 | [diff] [blame] | 1596 | qc->err_mask |= AC_ERR_TIMEOUT; |
| 1597 | |
| 1598 | if (ap->ops->error_handler) |
| 1599 | ata_port_freeze(ap); |
| 1600 | else |
| 1601 | ata_qc_complete(qc); |
Tejun Heo | f15a1da | 2006-05-15 20:57:56 +0900 | [diff] [blame] | 1602 | |
Borislav Petkov | 0dd4b21 | 2006-06-23 02:29:08 -0400 | [diff] [blame] | 1603 | if (ata_msg_warn(ap)) |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 1604 | ata_dev_warn(dev, "qc timeout (cmd 0x%x)\n", |
| 1605 | command); |
Tejun Heo | a2a7a66 | 2005-12-13 14:48:31 +0900 | [diff] [blame] | 1606 | } |
| 1607 | |
Jeff Garzik | ba6a130 | 2006-06-22 23:46:10 -0400 | [diff] [blame] | 1608 | spin_unlock_irqrestore(ap->lock, flags); |
Tejun Heo | a2a7a66 | 2005-12-13 14:48:31 +0900 | [diff] [blame] | 1609 | } |
| 1610 | |
Tejun Heo | d95a717 | 2006-05-15 20:58:14 +0900 | [diff] [blame] | 1611 | /* do post_internal_cmd */ |
| 1612 | if (ap->ops->post_internal_cmd) |
| 1613 | ap->ops->post_internal_cmd(qc); |
| 1614 | |
Tejun Heo | a51d644 | 2007-03-20 15:24:11 +0900 | [diff] [blame] | 1615 | /* perform minimal error analysis */ |
| 1616 | if (qc->flags & ATA_QCFLAG_FAILED) { |
| 1617 | if (qc->result_tf.command & (ATA_ERR | ATA_DF)) |
| 1618 | qc->err_mask |= AC_ERR_DEV; |
| 1619 | |
| 1620 | if (!qc->err_mask) |
| 1621 | qc->err_mask |= AC_ERR_OTHER; |
| 1622 | |
| 1623 | if (qc->err_mask & ~AC_ERR_OTHER) |
| 1624 | qc->err_mask &= ~AC_ERR_OTHER; |
Damien Le Moal | 2dae995 | 2016-12-19 10:17:40 +0900 | [diff] [blame] | 1625 | } else if (qc->tf.command == ATA_CMD_REQ_SENSE_DATA) { |
| 1626 | qc->result_tf.command |= ATA_SENSE; |
Tejun Heo | d95a717 | 2006-05-15 20:58:14 +0900 | [diff] [blame] | 1627 | } |
| 1628 | |
Tejun Heo | 1586930 | 2006-05-15 20:57:33 +0900 | [diff] [blame] | 1629 | /* finish up */ |
Jeff Garzik | ba6a130 | 2006-06-22 23:46:10 -0400 | [diff] [blame] | 1630 | spin_lock_irqsave(ap->lock, flags); |
Tejun Heo | 1586930 | 2006-05-15 20:57:33 +0900 | [diff] [blame] | 1631 | |
Tejun Heo | e61e067 | 2006-05-15 20:57:40 +0900 | [diff] [blame] | 1632 | *tf = qc->result_tf; |
Tejun Heo | 77853bf | 2006-01-23 13:09:36 +0900 | [diff] [blame] | 1633 | err_mask = qc->err_mask; |
| 1634 | |
| 1635 | ata_qc_free(qc); |
Tejun Heo | 9af5c9c | 2007-08-06 18:36:22 +0900 | [diff] [blame] | 1636 | link->active_tag = preempted_tag; |
| 1637 | link->sactive = preempted_sactive; |
Tejun Heo | dedaf2b | 2006-05-15 21:03:43 +0900 | [diff] [blame] | 1638 | ap->qc_active = preempted_qc_active; |
Tejun Heo | da917d6 | 2007-09-23 13:14:12 +0900 | [diff] [blame] | 1639 | ap->nr_active_links = preempted_nr_active_links; |
Tejun Heo | 77853bf | 2006-01-23 13:09:36 +0900 | [diff] [blame] | 1640 | |
Jeff Garzik | ba6a130 | 2006-06-22 23:46:10 -0400 | [diff] [blame] | 1641 | spin_unlock_irqrestore(ap->lock, flags); |
Tejun Heo | 1586930 | 2006-05-15 20:57:33 +0900 | [diff] [blame] | 1642 | |
Tejun Heo | 87fbc5a | 2008-05-20 02:17:54 +0900 | [diff] [blame] | 1643 | if ((err_mask & AC_ERR_TIMEOUT) && auto_timeout) |
| 1644 | ata_internal_cmd_timed_out(dev, command); |
| 1645 | |
Tejun Heo | 77853bf | 2006-01-23 13:09:36 +0900 | [diff] [blame] | 1646 | return err_mask; |
Tejun Heo | a2a7a66 | 2005-12-13 14:48:31 +0900 | [diff] [blame] | 1647 | } |
| 1648 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1649 | /** |
Tejun Heo | 33480a0 | 2006-12-12 02:15:31 +0900 | [diff] [blame] | 1650 | * ata_exec_internal - execute libata internal command |
Tejun Heo | 2432697 | 2006-11-14 22:47:09 +0900 | [diff] [blame] | 1651 | * @dev: Device to which the command is sent |
| 1652 | * @tf: Taskfile registers for the command and the result |
| 1653 | * @cdb: CDB for packet command |
Masanari Iida | e227867 | 2014-02-18 22:54:36 +0900 | [diff] [blame] | 1654 | * @dma_dir: Data transfer direction of the command |
Tejun Heo | 2432697 | 2006-11-14 22:47:09 +0900 | [diff] [blame] | 1655 | * @buf: Data buffer of the command |
| 1656 | * @buflen: Length of data buffer |
Tejun Heo | 2b78910 | 2007-10-09 15:05:44 +0900 | [diff] [blame] | 1657 | * @timeout: Timeout in msecs (0 for default) |
Tejun Heo | 2432697 | 2006-11-14 22:47:09 +0900 | [diff] [blame] | 1658 | * |
| 1659 | * Wrapper around ata_exec_internal_sg() which takes simple |
| 1660 | * buffer instead of sg list. |
| 1661 | * |
| 1662 | * LOCKING: |
| 1663 | * None. Should be called with kernel context, might sleep. |
| 1664 | * |
| 1665 | * RETURNS: |
| 1666 | * Zero on success, AC_ERR_* mask on failure |
| 1667 | */ |
| 1668 | unsigned ata_exec_internal(struct ata_device *dev, |
| 1669 | struct ata_taskfile *tf, const u8 *cdb, |
Tejun Heo | 2b78910 | 2007-10-09 15:05:44 +0900 | [diff] [blame] | 1670 | int dma_dir, void *buf, unsigned int buflen, |
| 1671 | unsigned long timeout) |
Tejun Heo | 2432697 | 2006-11-14 22:47:09 +0900 | [diff] [blame] | 1672 | { |
Tejun Heo | 33480a0 | 2006-12-12 02:15:31 +0900 | [diff] [blame] | 1673 | struct scatterlist *psg = NULL, sg; |
| 1674 | unsigned int n_elem = 0; |
Tejun Heo | 2432697 | 2006-11-14 22:47:09 +0900 | [diff] [blame] | 1675 | |
Tejun Heo | 33480a0 | 2006-12-12 02:15:31 +0900 | [diff] [blame] | 1676 | if (dma_dir != DMA_NONE) { |
| 1677 | WARN_ON(!buf); |
| 1678 | sg_init_one(&sg, buf, buflen); |
| 1679 | psg = &sg; |
| 1680 | n_elem++; |
| 1681 | } |
Tejun Heo | 2432697 | 2006-11-14 22:47:09 +0900 | [diff] [blame] | 1682 | |
Tejun Heo | 2b78910 | 2007-10-09 15:05:44 +0900 | [diff] [blame] | 1683 | return ata_exec_internal_sg(dev, tf, cdb, dma_dir, psg, n_elem, |
| 1684 | timeout); |
Tejun Heo | 2432697 | 2006-11-14 22:47:09 +0900 | [diff] [blame] | 1685 | } |
| 1686 | |
| 1687 | /** |
Alan Cox | 1bc4ccf | 2006-01-09 17:18:14 +0000 | [diff] [blame] | 1688 | * ata_pio_need_iordy - check if iordy needed |
| 1689 | * @adev: ATA device |
| 1690 | * |
| 1691 | * Check if the current speed of the device requires IORDY. Used |
| 1692 | * by various controllers for chip configuration. |
| 1693 | */ |
Alan Cox | 1bc4ccf | 2006-01-09 17:18:14 +0000 | [diff] [blame] | 1694 | unsigned int ata_pio_need_iordy(const struct ata_device *adev) |
| 1695 | { |
Tejun Heo | 0d9e665 | 2009-06-11 11:04:45 +0900 | [diff] [blame] | 1696 | /* Don't set IORDY if we're preparing for reset. IORDY may |
| 1697 | * lead to controller lock up on certain controllers if the |
| 1698 | * port is not occupied. See bko#11703 for details. |
| 1699 | */ |
| 1700 | if (adev->link->ap->pflags & ATA_PFLAG_RESETTING) |
| 1701 | return 0; |
| 1702 | /* Controller doesn't support IORDY. Probably a pointless |
| 1703 | * check as the caller should know this. |
| 1704 | */ |
Tejun Heo | 9af5c9c | 2007-08-06 18:36:22 +0900 | [diff] [blame] | 1705 | if (adev->link->ap->flags & ATA_FLAG_NO_IORDY) |
Alan Cox | 1bc4ccf | 2006-01-09 17:18:14 +0000 | [diff] [blame] | 1706 | return 0; |
David Daney | 5c18c4d | 2008-12-10 15:39:12 -0800 | [diff] [blame] | 1707 | /* CF spec. r4.1 Table 22 says no iordy on PIO5 and PIO6. */ |
| 1708 | if (ata_id_is_cfa(adev->id) |
| 1709 | && (adev->pio_mode == XFER_PIO_5 || adev->pio_mode == XFER_PIO_6)) |
| 1710 | return 0; |
Alan Cox | 432729f | 2007-03-08 23:22:59 +0000 | [diff] [blame] | 1711 | /* PIO3 and higher it is mandatory */ |
| 1712 | if (adev->pio_mode > XFER_PIO_2) |
Alan Cox | 1bc4ccf | 2006-01-09 17:18:14 +0000 | [diff] [blame] | 1713 | return 1; |
Alan Cox | 432729f | 2007-03-08 23:22:59 +0000 | [diff] [blame] | 1714 | /* We turn it on when possible */ |
| 1715 | if (ata_id_has_iordy(adev->id)) |
| 1716 | return 1; |
| 1717 | return 0; |
| 1718 | } |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 1719 | EXPORT_SYMBOL_GPL(ata_pio_need_iordy); |
Jeff Garzik | 2e9edbf | 2006-03-24 09:56:57 -0500 | [diff] [blame] | 1720 | |
Alan Cox | 432729f | 2007-03-08 23:22:59 +0000 | [diff] [blame] | 1721 | /** |
| 1722 | * ata_pio_mask_no_iordy - Return the non IORDY mask |
| 1723 | * @adev: ATA device |
| 1724 | * |
| 1725 | * Compute the highest mode possible if we are not using iordy. Return |
| 1726 | * -1 if no iordy mode is available. |
| 1727 | */ |
Alan Cox | 432729f | 2007-03-08 23:22:59 +0000 | [diff] [blame] | 1728 | static u32 ata_pio_mask_no_iordy(const struct ata_device *adev) |
| 1729 | { |
Alan Cox | 1bc4ccf | 2006-01-09 17:18:14 +0000 | [diff] [blame] | 1730 | /* 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] | 1731 | if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE */ |
Alan Cox | 432729f | 2007-03-08 23:22:59 +0000 | [diff] [blame] | 1732 | u16 pio = adev->id[ATA_ID_EIDE_PIO]; |
Alan Cox | 1bc4ccf | 2006-01-09 17:18:14 +0000 | [diff] [blame] | 1733 | /* Is the speed faster than the drive allows non IORDY ? */ |
| 1734 | if (pio) { |
| 1735 | /* This is cycle times not frequency - watch the logic! */ |
| 1736 | if (pio > 240) /* PIO2 is 240nS per cycle */ |
Alan Cox | 432729f | 2007-03-08 23:22:59 +0000 | [diff] [blame] | 1737 | return 3 << ATA_SHIFT_PIO; |
| 1738 | return 7 << ATA_SHIFT_PIO; |
Alan Cox | 1bc4ccf | 2006-01-09 17:18:14 +0000 | [diff] [blame] | 1739 | } |
| 1740 | } |
Alan Cox | 432729f | 2007-03-08 23:22:59 +0000 | [diff] [blame] | 1741 | return 3 << ATA_SHIFT_PIO; |
Alan Cox | 1bc4ccf | 2006-01-09 17:18:14 +0000 | [diff] [blame] | 1742 | } |
| 1743 | |
| 1744 | /** |
Alan Cox | 963e497 | 2008-07-24 17:16:06 +0100 | [diff] [blame] | 1745 | * ata_do_dev_read_id - default ID read method |
| 1746 | * @dev: device |
| 1747 | * @tf: proposed taskfile |
| 1748 | * @id: data buffer |
| 1749 | * |
| 1750 | * Issue the identify taskfile and hand back the buffer containing |
| 1751 | * identify data. For some RAID controllers and for pre ATA devices |
| 1752 | * this function is wrapped or replaced by the driver |
| 1753 | */ |
| 1754 | unsigned int ata_do_dev_read_id(struct ata_device *dev, |
| 1755 | struct ata_taskfile *tf, u16 *id) |
| 1756 | { |
| 1757 | return ata_exec_internal(dev, tf, NULL, DMA_FROM_DEVICE, |
| 1758 | id, sizeof(id[0]) * ATA_ID_WORDS, 0); |
| 1759 | } |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 1760 | EXPORT_SYMBOL_GPL(ata_do_dev_read_id); |
Alan Cox | 963e497 | 2008-07-24 17:16:06 +0100 | [diff] [blame] | 1761 | |
| 1762 | /** |
Tejun Heo | 49016ac | 2006-02-21 02:12:11 +0900 | [diff] [blame] | 1763 | * ata_dev_read_id - Read ID data from the specified device |
Tejun Heo | 49016ac | 2006-02-21 02:12:11 +0900 | [diff] [blame] | 1764 | * @dev: target device |
| 1765 | * @p_class: pointer to class of the target device (may be changed) |
Tejun Heo | bff0464 | 2006-11-10 18:08:10 +0900 | [diff] [blame] | 1766 | * @flags: ATA_READID_* flags |
Tejun Heo | fe635c7 | 2006-05-15 20:57:35 +0900 | [diff] [blame] | 1767 | * @id: buffer to read IDENTIFY data into |
Tejun Heo | 49016ac | 2006-02-21 02:12:11 +0900 | [diff] [blame] | 1768 | * |
| 1769 | * Read ID data from the specified device. ATA_CMD_ID_ATA is |
| 1770 | * performed on ATA devices and ATA_CMD_ID_ATAPI on ATAPI |
Tejun Heo | aec5c3c | 2006-03-25 01:33:34 +0900 | [diff] [blame] | 1771 | * devices. This function also issues ATA_CMD_INIT_DEV_PARAMS |
| 1772 | * for pre-ATA4 drives. |
Tejun Heo | 49016ac | 2006-02-21 02:12:11 +0900 | [diff] [blame] | 1773 | * |
Alan Cox | 50a9901 | 2007-08-08 14:27:00 +0100 | [diff] [blame] | 1774 | * FIXME: ATA_CMD_ID_ATA is optional for early drives and right |
Jeff Garzik | 2dcb407 | 2007-10-19 06:42:56 -0400 | [diff] [blame] | 1775 | * now we abort if we hit that case. |
Alan Cox | 50a9901 | 2007-08-08 14:27:00 +0100 | [diff] [blame] | 1776 | * |
Tejun Heo | 49016ac | 2006-02-21 02:12:11 +0900 | [diff] [blame] | 1777 | * LOCKING: |
| 1778 | * Kernel thread context (may sleep) |
| 1779 | * |
| 1780 | * RETURNS: |
| 1781 | * 0 on success, -errno otherwise. |
| 1782 | */ |
Tejun Heo | a9beec9 | 2006-05-31 18:27:44 +0900 | [diff] [blame] | 1783 | 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] | 1784 | unsigned int flags, u16 *id) |
Tejun Heo | 49016ac | 2006-02-21 02:12:11 +0900 | [diff] [blame] | 1785 | { |
Tejun Heo | 9af5c9c | 2007-08-06 18:36:22 +0900 | [diff] [blame] | 1786 | struct ata_port *ap = dev->link->ap; |
Tejun Heo | 49016ac | 2006-02-21 02:12:11 +0900 | [diff] [blame] | 1787 | unsigned int class = *p_class; |
Tejun Heo | 49016ac | 2006-02-21 02:12:11 +0900 | [diff] [blame] | 1788 | struct ata_taskfile tf; |
| 1789 | unsigned int err_mask = 0; |
| 1790 | const char *reason; |
Tejun Heo | 79b42ba | 2009-04-15 06:21:10 +0900 | [diff] [blame] | 1791 | bool is_semb = class == ATA_DEV_SEMB; |
Tejun Heo | 54936f8 | 2007-05-11 14:35:29 +0200 | [diff] [blame] | 1792 | int may_fallback = 1, tried_spinup = 0; |
Tejun Heo | 49016ac | 2006-02-21 02:12:11 +0900 | [diff] [blame] | 1793 | int rc; |
| 1794 | |
Borislav Petkov | 0dd4b21 | 2006-06-23 02:29:08 -0400 | [diff] [blame] | 1795 | if (ata_msg_ctl(ap)) |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 1796 | ata_dev_dbg(dev, "%s: ENTER\n", __func__); |
Tejun Heo | 49016ac | 2006-02-21 02:12:11 +0900 | [diff] [blame] | 1797 | |
Alan Cox | 963e497 | 2008-07-24 17:16:06 +0100 | [diff] [blame] | 1798 | retry: |
Tejun Heo | 3373efd | 2006-05-15 20:57:53 +0900 | [diff] [blame] | 1799 | ata_tf_init(dev, &tf); |
Tejun Heo | 49016ac | 2006-02-21 02:12:11 +0900 | [diff] [blame] | 1800 | |
| 1801 | switch (class) { |
Tejun Heo | 79b42ba | 2009-04-15 06:21:10 +0900 | [diff] [blame] | 1802 | case ATA_DEV_SEMB: |
| 1803 | class = ATA_DEV_ATA; /* some hard drives report SEMB sig */ |
Gustavo A. R. Silva | 05b8360 | 2017-10-12 14:19:16 -0500 | [diff] [blame] | 1804 | /* fall through */ |
Tejun Heo | 49016ac | 2006-02-21 02:12:11 +0900 | [diff] [blame] | 1805 | case ATA_DEV_ATA: |
Hannes Reinecke | 9162c65 | 2014-11-05 13:08:21 +0100 | [diff] [blame] | 1806 | case ATA_DEV_ZAC: |
Tejun Heo | 49016ac | 2006-02-21 02:12:11 +0900 | [diff] [blame] | 1807 | tf.command = ATA_CMD_ID_ATA; |
| 1808 | break; |
| 1809 | case ATA_DEV_ATAPI: |
| 1810 | tf.command = ATA_CMD_ID_ATAPI; |
| 1811 | break; |
| 1812 | default: |
| 1813 | rc = -ENODEV; |
| 1814 | reason = "unsupported class"; |
| 1815 | goto err_out; |
| 1816 | } |
| 1817 | |
| 1818 | tf.protocol = ATA_PROT_PIO; |
Tejun Heo | 81afe89 | 2007-02-07 12:37:41 -0800 | [diff] [blame] | 1819 | |
| 1820 | /* Some devices choke if TF registers contain garbage. Make |
| 1821 | * sure those are properly initialized. |
| 1822 | */ |
| 1823 | tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE; |
| 1824 | |
| 1825 | /* Device presence detection is unreliable on some |
| 1826 | * controllers. Always poll IDENTIFY if available. |
| 1827 | */ |
| 1828 | tf.flags |= ATA_TFLAG_POLLING; |
Tejun Heo | 49016ac | 2006-02-21 02:12:11 +0900 | [diff] [blame] | 1829 | |
Alan Cox | 963e497 | 2008-07-24 17:16:06 +0100 | [diff] [blame] | 1830 | if (ap->ops->read_id) |
| 1831 | err_mask = ap->ops->read_id(dev, &tf, id); |
| 1832 | else |
| 1833 | err_mask = ata_do_dev_read_id(dev, &tf, id); |
| 1834 | |
Tejun Heo | 49016ac | 2006-02-21 02:12:11 +0900 | [diff] [blame] | 1835 | if (err_mask) { |
Tejun Heo | 800b399 | 2006-12-03 21:34:13 +0900 | [diff] [blame] | 1836 | if (err_mask & AC_ERR_NODEV_HINT) { |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 1837 | ata_dev_dbg(dev, "NODEV after polling detection\n"); |
Tejun Heo | 55a8e2c | 2006-11-10 18:08:10 +0900 | [diff] [blame] | 1838 | return -ENOENT; |
| 1839 | } |
| 1840 | |
Tejun Heo | 79b42ba | 2009-04-15 06:21:10 +0900 | [diff] [blame] | 1841 | if (is_semb) { |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 1842 | ata_dev_info(dev, |
| 1843 | "IDENTIFY failed on device w/ SEMB sig, disabled\n"); |
Tejun Heo | 79b42ba | 2009-04-15 06:21:10 +0900 | [diff] [blame] | 1844 | /* SEMB is not supported yet */ |
| 1845 | *p_class = ATA_DEV_SEMB_UNSUP; |
| 1846 | return 0; |
| 1847 | } |
| 1848 | |
Tejun Heo | 1ffc151 | 2008-03-23 15:16:53 +0900 | [diff] [blame] | 1849 | if ((err_mask == AC_ERR_DEV) && (tf.feature & ATA_ABORTED)) { |
| 1850 | /* Device or controller might have reported |
| 1851 | * the wrong device class. Give a shot at the |
| 1852 | * other IDENTIFY if the current one is |
| 1853 | * aborted by the device. |
| 1854 | */ |
| 1855 | if (may_fallback) { |
| 1856 | may_fallback = 0; |
Tejun Heo | 54936f8 | 2007-05-11 14:35:29 +0200 | [diff] [blame] | 1857 | |
Tejun Heo | 1ffc151 | 2008-03-23 15:16:53 +0900 | [diff] [blame] | 1858 | if (class == ATA_DEV_ATA) |
| 1859 | class = ATA_DEV_ATAPI; |
| 1860 | else |
| 1861 | class = ATA_DEV_ATA; |
| 1862 | goto retry; |
| 1863 | } |
| 1864 | |
| 1865 | /* Control reaches here iff the device aborted |
| 1866 | * both flavors of IDENTIFYs which happens |
| 1867 | * sometimes with phantom devices. |
| 1868 | */ |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 1869 | ata_dev_dbg(dev, |
| 1870 | "both IDENTIFYs aborted, assuming NODEV\n"); |
Tejun Heo | 1ffc151 | 2008-03-23 15:16:53 +0900 | [diff] [blame] | 1871 | return -ENOENT; |
Tejun Heo | 54936f8 | 2007-05-11 14:35:29 +0200 | [diff] [blame] | 1872 | } |
| 1873 | |
Tejun Heo | 49016ac | 2006-02-21 02:12:11 +0900 | [diff] [blame] | 1874 | rc = -EIO; |
| 1875 | reason = "I/O error"; |
Tejun Heo | 49016ac | 2006-02-21 02:12:11 +0900 | [diff] [blame] | 1876 | goto err_out; |
| 1877 | } |
| 1878 | |
Tejun Heo | 43c9c59 | 2010-05-23 12:59:11 +0200 | [diff] [blame] | 1879 | if (dev->horkage & ATA_HORKAGE_DUMP_ID) { |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 1880 | ata_dev_dbg(dev, "dumping IDENTIFY data, " |
| 1881 | "class=%d may_fallback=%d tried_spinup=%d\n", |
| 1882 | class, may_fallback, tried_spinup); |
Tejun Heo | 43c9c59 | 2010-05-23 12:59:11 +0200 | [diff] [blame] | 1883 | print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, |
| 1884 | 16, 2, id, ATA_ID_WORDS * sizeof(*id), true); |
| 1885 | } |
| 1886 | |
Tejun Heo | 54936f8 | 2007-05-11 14:35:29 +0200 | [diff] [blame] | 1887 | /* Falling back doesn't make sense if ID data was read |
| 1888 | * successfully at least once. |
| 1889 | */ |
| 1890 | may_fallback = 0; |
| 1891 | |
Tejun Heo | 49016ac | 2006-02-21 02:12:11 +0900 | [diff] [blame] | 1892 | swap_buf_le16(id, ATA_ID_WORDS); |
| 1893 | |
Tejun Heo | 49016ac | 2006-02-21 02:12:11 +0900 | [diff] [blame] | 1894 | /* sanity check */ |
Tejun Heo | a4f5749 | 2006-09-12 20:35:49 -0700 | [diff] [blame] | 1895 | rc = -EINVAL; |
Alan Cox | 6070068 | 2007-06-07 16:13:55 +0100 | [diff] [blame] | 1896 | reason = "device reports invalid type"; |
Tejun Heo | a4f5749 | 2006-09-12 20:35:49 -0700 | [diff] [blame] | 1897 | |
Hannes Reinecke | 9162c65 | 2014-11-05 13:08:21 +0100 | [diff] [blame] | 1898 | if (class == ATA_DEV_ATA || class == ATA_DEV_ZAC) { |
Tejun Heo | a4f5749 | 2006-09-12 20:35:49 -0700 | [diff] [blame] | 1899 | if (!ata_id_is_ata(id) && !ata_id_is_cfa(id)) |
| 1900 | goto err_out; |
Andy Whitcroft | db63a4c | 2012-05-04 22:15:10 +0100 | [diff] [blame] | 1901 | if (ap->host->flags & ATA_HOST_IGNORE_ATA && |
| 1902 | ata_id_is_ata(id)) { |
| 1903 | ata_dev_dbg(dev, |
| 1904 | "host indicates ignore ATA devices, ignored\n"); |
| 1905 | return -ENOENT; |
| 1906 | } |
Tejun Heo | a4f5749 | 2006-09-12 20:35:49 -0700 | [diff] [blame] | 1907 | } else { |
| 1908 | if (ata_id_is_ata(id)) |
| 1909 | goto err_out; |
Tejun Heo | 49016ac | 2006-02-21 02:12:11 +0900 | [diff] [blame] | 1910 | } |
| 1911 | |
Mark Lord | 169439c | 2007-04-17 18:26:07 -0400 | [diff] [blame] | 1912 | if (!tried_spinup && (id[2] == 0x37c8 || id[2] == 0x738c)) { |
| 1913 | tried_spinup = 1; |
| 1914 | /* |
| 1915 | * Drive powered-up in standby mode, and requires a specific |
| 1916 | * SET_FEATURES spin-up subcommand before it will accept |
| 1917 | * anything other than the original IDENTIFY command. |
| 1918 | */ |
Jeff Garzik | 218f3d3 | 2007-10-25 00:33:27 -0400 | [diff] [blame] | 1919 | err_mask = ata_dev_set_feature(dev, SETFEATURES_SPINUP, 0); |
Ryan Power | fb0582f9 | 2007-08-10 13:59:35 -0700 | [diff] [blame] | 1920 | if (err_mask && id[2] != 0x738c) { |
Mark Lord | 169439c | 2007-04-17 18:26:07 -0400 | [diff] [blame] | 1921 | rc = -EIO; |
| 1922 | reason = "SPINUP failed"; |
| 1923 | goto err_out; |
| 1924 | } |
| 1925 | /* |
| 1926 | * If the drive initially returned incomplete IDENTIFY info, |
| 1927 | * we now must reissue the IDENTIFY command. |
| 1928 | */ |
| 1929 | if (id[2] == 0x37c8) |
| 1930 | goto retry; |
| 1931 | } |
| 1932 | |
Hannes Reinecke | 9162c65 | 2014-11-05 13:08:21 +0100 | [diff] [blame] | 1933 | if ((flags & ATA_READID_POSTRESET) && |
| 1934 | (class == ATA_DEV_ATA || class == ATA_DEV_ZAC)) { |
Tejun Heo | 49016ac | 2006-02-21 02:12:11 +0900 | [diff] [blame] | 1935 | /* |
| 1936 | * The exact sequence expected by certain pre-ATA4 drives is: |
| 1937 | * SRST RESET |
Alan Cox | 50a9901 | 2007-08-08 14:27:00 +0100 | [diff] [blame] | 1938 | * IDENTIFY (optional in early ATA) |
| 1939 | * INITIALIZE DEVICE PARAMETERS (later IDE and ATA) |
Tejun Heo | 49016ac | 2006-02-21 02:12:11 +0900 | [diff] [blame] | 1940 | * anything else.. |
| 1941 | * Some drives were very specific about that exact sequence. |
Alan Cox | 50a9901 | 2007-08-08 14:27:00 +0100 | [diff] [blame] | 1942 | * |
| 1943 | * Note that ATA4 says lba is mandatory so the second check |
Adam Buchbinder | c9404c9 | 2009-12-18 15:40:42 -0500 | [diff] [blame] | 1944 | * should never trigger. |
Tejun Heo | 49016ac | 2006-02-21 02:12:11 +0900 | [diff] [blame] | 1945 | */ |
| 1946 | if (ata_id_major_version(id) < 4 || !ata_id_has_lba(id)) { |
Tejun Heo | 3373efd | 2006-05-15 20:57:53 +0900 | [diff] [blame] | 1947 | err_mask = ata_dev_init_params(dev, id[3], id[6]); |
Tejun Heo | 49016ac | 2006-02-21 02:12:11 +0900 | [diff] [blame] | 1948 | if (err_mask) { |
| 1949 | rc = -EIO; |
| 1950 | reason = "INIT_DEV_PARAMS failed"; |
| 1951 | goto err_out; |
| 1952 | } |
| 1953 | |
| 1954 | /* current CHS translation info (id[53-58]) might be |
| 1955 | * changed. reread the identify device info. |
| 1956 | */ |
Tejun Heo | bff0464 | 2006-11-10 18:08:10 +0900 | [diff] [blame] | 1957 | flags &= ~ATA_READID_POSTRESET; |
Tejun Heo | 49016ac | 2006-02-21 02:12:11 +0900 | [diff] [blame] | 1958 | goto retry; |
| 1959 | } |
| 1960 | } |
| 1961 | |
| 1962 | *p_class = class; |
Tejun Heo | fe635c7 | 2006-05-15 20:57:35 +0900 | [diff] [blame] | 1963 | |
Tejun Heo | 49016ac | 2006-02-21 02:12:11 +0900 | [diff] [blame] | 1964 | return 0; |
| 1965 | |
| 1966 | err_out: |
Tejun Heo | 8857455 | 2006-06-25 20:00:35 +0900 | [diff] [blame] | 1967 | if (ata_msg_warn(ap)) |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 1968 | ata_dev_warn(dev, "failed to IDENTIFY (%s, err_mask=0x%x)\n", |
| 1969 | reason, err_mask); |
Tejun Heo | 49016ac | 2006-02-21 02:12:11 +0900 | [diff] [blame] | 1970 | return rc; |
| 1971 | } |
| 1972 | |
Christoph Hellwig | f01f62c | 2017-06-04 14:42:20 +0200 | [diff] [blame] | 1973 | /** |
| 1974 | * ata_read_log_page - read a specific log page |
| 1975 | * @dev: target device |
| 1976 | * @log: log to read |
| 1977 | * @page: page to read |
| 1978 | * @buf: buffer to store read page |
| 1979 | * @sectors: number of sectors to read |
| 1980 | * |
| 1981 | * Read log page using READ_LOG_EXT command. |
| 1982 | * |
| 1983 | * LOCKING: |
| 1984 | * Kernel thread context (may sleep). |
| 1985 | * |
| 1986 | * RETURNS: |
| 1987 | * 0 on success, AC_ERR_* mask otherwise. |
| 1988 | */ |
| 1989 | unsigned int ata_read_log_page(struct ata_device *dev, u8 log, |
| 1990 | u8 page, void *buf, unsigned int sectors) |
| 1991 | { |
| 1992 | unsigned long ap_flags = dev->link->ap->flags; |
| 1993 | struct ata_taskfile tf; |
| 1994 | unsigned int err_mask; |
| 1995 | bool dma = false; |
| 1996 | |
| 1997 | DPRINTK("read log page - log 0x%x, page 0x%x\n", log, page); |
| 1998 | |
| 1999 | /* |
| 2000 | * Return error without actually issuing the command on controllers |
| 2001 | * which e.g. lockup on a read log page. |
| 2002 | */ |
| 2003 | if (ap_flags & ATA_FLAG_NO_LOG_PAGE) |
| 2004 | return AC_ERR_DEV; |
| 2005 | |
| 2006 | retry: |
| 2007 | ata_tf_init(dev, &tf); |
| 2008 | 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] | 2009 | !(dev->horkage & ATA_HORKAGE_NO_DMA_LOG)) { |
Christoph Hellwig | f01f62c | 2017-06-04 14:42:20 +0200 | [diff] [blame] | 2010 | tf.command = ATA_CMD_READ_LOG_DMA_EXT; |
| 2011 | tf.protocol = ATA_PROT_DMA; |
| 2012 | dma = true; |
| 2013 | } else { |
| 2014 | tf.command = ATA_CMD_READ_LOG_EXT; |
| 2015 | tf.protocol = ATA_PROT_PIO; |
| 2016 | dma = false; |
| 2017 | } |
| 2018 | tf.lbal = log; |
| 2019 | tf.lbam = page; |
| 2020 | tf.nsect = sectors; |
| 2021 | tf.hob_nsect = sectors >> 8; |
| 2022 | tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_LBA48 | ATA_TFLAG_DEVICE; |
| 2023 | |
| 2024 | err_mask = ata_exec_internal(dev, &tf, NULL, DMA_FROM_DEVICE, |
| 2025 | buf, sectors * ATA_SECT_SIZE, 0); |
| 2026 | |
| 2027 | if (err_mask && dma) { |
Damien Le Moal | 7cfdfdc | 2017-07-10 14:45:20 +0900 | [diff] [blame] | 2028 | dev->horkage |= ATA_HORKAGE_NO_DMA_LOG; |
| 2029 | ata_dev_warn(dev, "READ LOG DMA EXT failed, trying PIO\n"); |
Christoph Hellwig | f01f62c | 2017-06-04 14:42:20 +0200 | [diff] [blame] | 2030 | goto retry; |
| 2031 | } |
| 2032 | |
| 2033 | DPRINTK("EXIT, err_mask=%x\n", err_mask); |
| 2034 | return err_mask; |
| 2035 | } |
| 2036 | |
Christoph Hellwig | efe205a | 2017-06-04 14:42:21 +0200 | [diff] [blame] | 2037 | static bool ata_log_supported(struct ata_device *dev, u8 log) |
| 2038 | { |
| 2039 | struct ata_port *ap = dev->link->ap; |
| 2040 | |
| 2041 | if (ata_read_log_page(dev, ATA_LOG_DIRECTORY, 0, ap->sector_buf, 1)) |
| 2042 | return false; |
| 2043 | return get_unaligned_le16(&ap->sector_buf[log * 2]) ? true : false; |
| 2044 | } |
| 2045 | |
Christoph Hellwig | a0fd245 | 2017-06-04 14:42:23 +0200 | [diff] [blame] | 2046 | static bool ata_identify_page_supported(struct ata_device *dev, u8 page) |
| 2047 | { |
| 2048 | struct ata_port *ap = dev->link->ap; |
| 2049 | unsigned int err, i; |
| 2050 | |
| 2051 | if (!ata_log_supported(dev, ATA_LOG_IDENTIFY_DEVICE)) { |
| 2052 | ata_dev_warn(dev, "ATA Identify Device Log not supported\n"); |
| 2053 | return false; |
| 2054 | } |
| 2055 | |
| 2056 | /* |
| 2057 | * Read IDENTIFY DEVICE data log, page 0, to figure out if the page is |
| 2058 | * supported. |
| 2059 | */ |
| 2060 | err = ata_read_log_page(dev, ATA_LOG_IDENTIFY_DEVICE, 0, ap->sector_buf, |
| 2061 | 1); |
| 2062 | if (err) { |
| 2063 | ata_dev_info(dev, |
| 2064 | "failed to get Device Identify Log Emask 0x%x\n", |
| 2065 | err); |
| 2066 | return false; |
| 2067 | } |
| 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); |
| 2131 | if (err_mask) { |
| 2132 | ata_dev_dbg(dev, |
| 2133 | "failed to get NCQ Send/Recv Log Emask 0x%x\n", |
| 2134 | err_mask); |
| 2135 | } else { |
| 2136 | u8 *cmds = dev->ncq_send_recv_cmds; |
| 2137 | |
| 2138 | dev->flags |= ATA_DFLAG_NCQ_SEND_RECV; |
| 2139 | memcpy(cmds, ap->sector_buf, ATA_LOG_NCQ_SEND_RECV_SIZE); |
| 2140 | |
| 2141 | if (dev->horkage & ATA_HORKAGE_NO_NCQ_TRIM) { |
| 2142 | ata_dev_dbg(dev, "disabling queued TRIM support\n"); |
| 2143 | cmds[ATA_LOG_NCQ_SEND_RECV_DSM_OFFSET] &= |
| 2144 | ~ATA_LOG_NCQ_SEND_RECV_DSM_TRIM; |
| 2145 | } |
| 2146 | } |
| 2147 | } |
| 2148 | |
Hannes Reinecke | 284b3b7 | 2016-04-25 12:45:54 +0200 | [diff] [blame] | 2149 | static void ata_dev_config_ncq_non_data(struct ata_device *dev) |
| 2150 | { |
| 2151 | struct ata_port *ap = dev->link->ap; |
| 2152 | unsigned int err_mask; |
Hannes Reinecke | 284b3b7 | 2016-04-25 12:45:54 +0200 | [diff] [blame] | 2153 | |
Christoph Hellwig | efe205a | 2017-06-04 14:42:21 +0200 | [diff] [blame] | 2154 | if (!ata_log_supported(dev, ATA_LOG_NCQ_NON_DATA)) { |
Hannes Reinecke | 284b3b7 | 2016-04-25 12:45:54 +0200 | [diff] [blame] | 2155 | ata_dev_warn(dev, |
| 2156 | "NCQ Send/Recv Log not supported\n"); |
| 2157 | return; |
| 2158 | } |
| 2159 | err_mask = ata_read_log_page(dev, ATA_LOG_NCQ_NON_DATA, |
| 2160 | 0, ap->sector_buf, 1); |
| 2161 | if (err_mask) { |
| 2162 | ata_dev_dbg(dev, |
| 2163 | "failed to get NCQ Non-Data Log Emask 0x%x\n", |
| 2164 | err_mask); |
| 2165 | } else { |
| 2166 | u8 *cmds = dev->ncq_non_data_cmds; |
| 2167 | |
| 2168 | memcpy(cmds, ap->sector_buf, ATA_LOG_NCQ_NON_DATA_SIZE); |
| 2169 | } |
| 2170 | } |
| 2171 | |
Adam Manzanares | 8e06178 | 2016-10-17 11:27:29 -0700 | [diff] [blame] | 2172 | static void ata_dev_config_ncq_prio(struct ata_device *dev) |
| 2173 | { |
| 2174 | struct ata_port *ap = dev->link->ap; |
| 2175 | unsigned int err_mask; |
| 2176 | |
Adam Manzanares | 9f56eca | 2016-12-13 12:00:05 -0800 | [diff] [blame] | 2177 | if (!(dev->flags & ATA_DFLAG_NCQ_PRIO_ENABLE)) { |
| 2178 | dev->flags &= ~ATA_DFLAG_NCQ_PRIO; |
| 2179 | return; |
| 2180 | } |
| 2181 | |
Adam Manzanares | 8e06178 | 2016-10-17 11:27:29 -0700 | [diff] [blame] | 2182 | err_mask = ata_read_log_page(dev, |
Christoph Hellwig | 1d51d5f | 2017-06-04 14:42:22 +0200 | [diff] [blame] | 2183 | ATA_LOG_IDENTIFY_DEVICE, |
Adam Manzanares | 8e06178 | 2016-10-17 11:27:29 -0700 | [diff] [blame] | 2184 | ATA_LOG_SATA_SETTINGS, |
| 2185 | ap->sector_buf, |
| 2186 | 1); |
| 2187 | if (err_mask) { |
| 2188 | ata_dev_dbg(dev, |
| 2189 | "failed to get Identify Device data, Emask 0x%x\n", |
| 2190 | err_mask); |
| 2191 | return; |
| 2192 | } |
| 2193 | |
Adam Manzanares | 9f56eca | 2016-12-13 12:00:05 -0800 | [diff] [blame] | 2194 | if (ap->sector_buf[ATA_LOG_NCQ_PRIO_OFFSET] & BIT(3)) { |
Adam Manzanares | 8e06178 | 2016-10-17 11:27:29 -0700 | [diff] [blame] | 2195 | dev->flags |= ATA_DFLAG_NCQ_PRIO; |
Adam Manzanares | 9f56eca | 2016-12-13 12:00:05 -0800 | [diff] [blame] | 2196 | } else { |
| 2197 | dev->flags &= ~ATA_DFLAG_NCQ_PRIO; |
Adam Manzanares | 8e06178 | 2016-10-17 11:27:29 -0700 | [diff] [blame] | 2198 | ata_dev_dbg(dev, "SATA page does not support priority\n"); |
Adam Manzanares | 9f56eca | 2016-12-13 12:00:05 -0800 | [diff] [blame] | 2199 | } |
Adam Manzanares | 8e06178 | 2016-10-17 11:27:29 -0700 | [diff] [blame] | 2200 | |
| 2201 | } |
| 2202 | |
Shaohua Li | 388539f | 2009-07-27 09:24:35 +0800 | [diff] [blame] | 2203 | static int ata_dev_config_ncq(struct ata_device *dev, |
Tejun Heo | a6e6ce8 | 2006-05-15 21:03:48 +0900 | [diff] [blame] | 2204 | char *desc, size_t desc_sz) |
| 2205 | { |
Tejun Heo | 9af5c9c | 2007-08-06 18:36:22 +0900 | [diff] [blame] | 2206 | struct ata_port *ap = dev->link->ap; |
Tejun Heo | a6e6ce8 | 2006-05-15 21:03:48 +0900 | [diff] [blame] | 2207 | int hdepth = 0, ddepth = ata_id_queue_depth(dev->id); |
Shaohua Li | 388539f | 2009-07-27 09:24:35 +0800 | [diff] [blame] | 2208 | unsigned int err_mask; |
| 2209 | char *aa_desc = ""; |
Tejun Heo | a6e6ce8 | 2006-05-15 21:03:48 +0900 | [diff] [blame] | 2210 | |
| 2211 | if (!ata_id_has_ncq(dev->id)) { |
| 2212 | desc[0] = '\0'; |
Shaohua Li | 388539f | 2009-07-27 09:24:35 +0800 | [diff] [blame] | 2213 | return 0; |
Tejun Heo | a6e6ce8 | 2006-05-15 21:03:48 +0900 | [diff] [blame] | 2214 | } |
Bartlomiej Zolnierkiewicz | cba97ea | 2020-03-26 16:58:09 +0100 | [diff] [blame] | 2215 | if (!IS_ENABLED(CONFIG_SATA_HOST)) |
| 2216 | return 0; |
Tejun Heo | 75683fe | 2007-07-05 13:31:27 +0900 | [diff] [blame] | 2217 | if (dev->horkage & ATA_HORKAGE_NONCQ) { |
Alan Cox | 6919a0a | 2006-10-27 19:08:46 -0700 | [diff] [blame] | 2218 | snprintf(desc, desc_sz, "NCQ (not used)"); |
Shaohua Li | 388539f | 2009-07-27 09:24:35 +0800 | [diff] [blame] | 2219 | return 0; |
Alan Cox | 6919a0a | 2006-10-27 19:08:46 -0700 | [diff] [blame] | 2220 | } |
Tejun Heo | a6e6ce8 | 2006-05-15 21:03:48 +0900 | [diff] [blame] | 2221 | if (ap->flags & ATA_FLAG_NCQ) { |
Jens Axboe | 69278f7 | 2018-05-11 12:51:10 -0600 | [diff] [blame] | 2222 | hdepth = min(ap->scsi_host->can_queue, ATA_MAX_QUEUE); |
Tejun Heo | a6e6ce8 | 2006-05-15 21:03:48 +0900 | [diff] [blame] | 2223 | dev->flags |= ATA_DFLAG_NCQ; |
| 2224 | } |
| 2225 | |
Shaohua Li | 388539f | 2009-07-27 09:24:35 +0800 | [diff] [blame] | 2226 | if (!(dev->horkage & ATA_HORKAGE_BROKEN_FPDMA_AA) && |
| 2227 | (ap->flags & ATA_FLAG_FPDMA_AA) && |
| 2228 | ata_id_has_fpdma_aa(dev->id)) { |
| 2229 | err_mask = ata_dev_set_feature(dev, SETFEATURES_SATA_ENABLE, |
| 2230 | SATA_FPDMA_AA); |
| 2231 | if (err_mask) { |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 2232 | ata_dev_err(dev, |
| 2233 | "failed to enable AA (error_mask=0x%x)\n", |
| 2234 | err_mask); |
Shaohua Li | 388539f | 2009-07-27 09:24:35 +0800 | [diff] [blame] | 2235 | if (err_mask != AC_ERR_DEV) { |
| 2236 | dev->horkage |= ATA_HORKAGE_BROKEN_FPDMA_AA; |
| 2237 | return -EIO; |
| 2238 | } |
| 2239 | } else |
| 2240 | aa_desc = ", AA"; |
| 2241 | } |
| 2242 | |
Tejun Heo | a6e6ce8 | 2006-05-15 21:03:48 +0900 | [diff] [blame] | 2243 | if (hdepth >= ddepth) |
Shaohua Li | 388539f | 2009-07-27 09:24:35 +0800 | [diff] [blame] | 2244 | snprintf(desc, desc_sz, "NCQ (depth %d)%s", ddepth, aa_desc); |
Tejun Heo | a6e6ce8 | 2006-05-15 21:03:48 +0900 | [diff] [blame] | 2245 | else |
Shaohua Li | 388539f | 2009-07-27 09:24:35 +0800 | [diff] [blame] | 2246 | snprintf(desc, desc_sz, "NCQ (depth %d/%d)%s", hdepth, |
| 2247 | ddepth, aa_desc); |
Marc Carino | ed36911 | 2013-08-24 23:22:50 -0700 | [diff] [blame] | 2248 | |
Hannes Reinecke | 284b3b7 | 2016-04-25 12:45:54 +0200 | [diff] [blame] | 2249 | if ((ap->flags & ATA_FLAG_FPDMA_AUX)) { |
| 2250 | if (ata_id_has_ncq_send_and_recv(dev->id)) |
| 2251 | ata_dev_config_ncq_send_recv(dev); |
| 2252 | if (ata_id_has_ncq_non_data(dev->id)) |
| 2253 | ata_dev_config_ncq_non_data(dev); |
Adam Manzanares | 8e06178 | 2016-10-17 11:27:29 -0700 | [diff] [blame] | 2254 | if (ata_id_has_ncq_prio(dev->id)) |
| 2255 | ata_dev_config_ncq_prio(dev); |
Marc Carino | ed36911 | 2013-08-24 23:22:50 -0700 | [diff] [blame] | 2256 | } |
| 2257 | |
Shaohua Li | 388539f | 2009-07-27 09:24:35 +0800 | [diff] [blame] | 2258 | return 0; |
Tejun Heo | a6e6ce8 | 2006-05-15 21:03:48 +0900 | [diff] [blame] | 2259 | } |
| 2260 | |
Hannes Reinecke | e87fd28 | 2016-04-04 11:43:55 +0200 | [diff] [blame] | 2261 | static void ata_dev_config_sense_reporting(struct ata_device *dev) |
| 2262 | { |
| 2263 | unsigned int err_mask; |
| 2264 | |
| 2265 | if (!ata_id_has_sense_reporting(dev->id)) |
| 2266 | return; |
| 2267 | |
| 2268 | if (ata_id_sense_reporting_enabled(dev->id)) |
| 2269 | return; |
| 2270 | |
| 2271 | err_mask = ata_dev_set_feature(dev, SETFEATURE_SENSE_DATA, 0x1); |
| 2272 | if (err_mask) { |
| 2273 | ata_dev_dbg(dev, |
| 2274 | "failed to enable Sense Data Reporting, Emask 0x%x\n", |
| 2275 | err_mask); |
| 2276 | } |
| 2277 | } |
| 2278 | |
Hannes Reinecke | 6d1003a | 2016-04-25 12:45:56 +0200 | [diff] [blame] | 2279 | static void ata_dev_config_zac(struct ata_device *dev) |
| 2280 | { |
| 2281 | struct ata_port *ap = dev->link->ap; |
| 2282 | unsigned int err_mask; |
| 2283 | u8 *identify_buf = ap->sector_buf; |
Hannes Reinecke | 6d1003a | 2016-04-25 12:45:56 +0200 | [diff] [blame] | 2284 | |
| 2285 | dev->zac_zones_optimal_open = U32_MAX; |
| 2286 | dev->zac_zones_optimal_nonseq = U32_MAX; |
| 2287 | dev->zac_zones_max_open = U32_MAX; |
| 2288 | |
| 2289 | /* |
| 2290 | * Always set the 'ZAC' flag for Host-managed devices. |
| 2291 | */ |
| 2292 | if (dev->class == ATA_DEV_ZAC) |
| 2293 | dev->flags |= ATA_DFLAG_ZAC; |
| 2294 | else if (ata_id_zoned_cap(dev->id) == 0x01) |
| 2295 | /* |
| 2296 | * Check for host-aware devices. |
| 2297 | */ |
| 2298 | dev->flags |= ATA_DFLAG_ZAC; |
| 2299 | |
| 2300 | if (!(dev->flags & ATA_DFLAG_ZAC)) |
| 2301 | return; |
| 2302 | |
Christoph Hellwig | a0fd245 | 2017-06-04 14:42:23 +0200 | [diff] [blame] | 2303 | if (!ata_identify_page_supported(dev, ATA_LOG_ZONED_INFORMATION)) { |
Hannes Reinecke | 6d1003a | 2016-04-25 12:45:56 +0200 | [diff] [blame] | 2304 | ata_dev_warn(dev, |
| 2305 | "ATA Zoned Information Log not supported\n"); |
| 2306 | return; |
| 2307 | } |
| 2308 | |
| 2309 | /* |
| 2310 | * Read IDENTIFY DEVICE data log, page 9 (Zoned-device information) |
| 2311 | */ |
Christoph Hellwig | 1d51d5f | 2017-06-04 14:42:22 +0200 | [diff] [blame] | 2312 | err_mask = ata_read_log_page(dev, ATA_LOG_IDENTIFY_DEVICE, |
Hannes Reinecke | 6d1003a | 2016-04-25 12:45:56 +0200 | [diff] [blame] | 2313 | ATA_LOG_ZONED_INFORMATION, |
| 2314 | identify_buf, 1); |
| 2315 | if (!err_mask) { |
| 2316 | u64 zoned_cap, opt_open, opt_nonseq, max_open; |
| 2317 | |
| 2318 | zoned_cap = get_unaligned_le64(&identify_buf[8]); |
| 2319 | if ((zoned_cap >> 63)) |
| 2320 | dev->zac_zoned_cap = (zoned_cap & 1); |
| 2321 | opt_open = get_unaligned_le64(&identify_buf[24]); |
| 2322 | if ((opt_open >> 63)) |
| 2323 | dev->zac_zones_optimal_open = (u32)opt_open; |
| 2324 | opt_nonseq = get_unaligned_le64(&identify_buf[32]); |
| 2325 | if ((opt_nonseq >> 63)) |
| 2326 | dev->zac_zones_optimal_nonseq = (u32)opt_nonseq; |
| 2327 | max_open = get_unaligned_le64(&identify_buf[40]); |
| 2328 | if ((max_open >> 63)) |
| 2329 | dev->zac_zones_max_open = (u32)max_open; |
| 2330 | } |
| 2331 | } |
| 2332 | |
Christoph Hellwig | 818831c | 2017-06-04 14:42:24 +0200 | [diff] [blame] | 2333 | static void ata_dev_config_trusted(struct ata_device *dev) |
| 2334 | { |
| 2335 | struct ata_port *ap = dev->link->ap; |
| 2336 | u64 trusted_cap; |
| 2337 | unsigned int err; |
| 2338 | |
Christoph Hellwig | e8f11db | 2017-08-29 14:42:06 +0200 | [diff] [blame] | 2339 | if (!ata_id_has_trusted(dev->id)) |
| 2340 | return; |
| 2341 | |
Christoph Hellwig | 818831c | 2017-06-04 14:42:24 +0200 | [diff] [blame] | 2342 | if (!ata_identify_page_supported(dev, ATA_LOG_SECURITY)) { |
| 2343 | ata_dev_warn(dev, |
| 2344 | "Security Log not supported\n"); |
| 2345 | return; |
| 2346 | } |
| 2347 | |
| 2348 | err = ata_read_log_page(dev, ATA_LOG_IDENTIFY_DEVICE, ATA_LOG_SECURITY, |
| 2349 | ap->sector_buf, 1); |
| 2350 | if (err) { |
| 2351 | ata_dev_dbg(dev, |
| 2352 | "failed to read Security Log, Emask 0x%x\n", err); |
| 2353 | return; |
| 2354 | } |
| 2355 | |
| 2356 | trusted_cap = get_unaligned_le64(&ap->sector_buf[40]); |
| 2357 | if (!(trusted_cap & (1ULL << 63))) { |
| 2358 | ata_dev_dbg(dev, |
| 2359 | "Trusted Computing capability qword not valid!\n"); |
| 2360 | return; |
| 2361 | } |
| 2362 | |
| 2363 | if (trusted_cap & (1 << 0)) |
| 2364 | dev->flags |= ATA_DFLAG_TRUSTED; |
| 2365 | } |
| 2366 | |
Tejun Heo | 49016ac | 2006-02-21 02:12:11 +0900 | [diff] [blame] | 2367 | /** |
Tejun Heo | ffeae41 | 2006-03-01 16:09:35 +0900 | [diff] [blame] | 2368 | * ata_dev_configure - Configure the specified ATA/ATAPI device |
Tejun Heo | ffeae41 | 2006-03-01 16:09:35 +0900 | [diff] [blame] | 2369 | * @dev: Target device to configure |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2370 | * |
Tejun Heo | ffeae41 | 2006-03-01 16:09:35 +0900 | [diff] [blame] | 2371 | * Configure @dev according to @dev->id. Generic and low-level |
| 2372 | * driver specific fixups are also applied. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2373 | * |
| 2374 | * LOCKING: |
Tejun Heo | ffeae41 | 2006-03-01 16:09:35 +0900 | [diff] [blame] | 2375 | * Kernel thread context (may sleep) |
| 2376 | * |
| 2377 | * RETURNS: |
| 2378 | * 0 on success, -errno otherwise |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2379 | */ |
Tejun Heo | efdaedc | 2006-11-01 18:38:52 +0900 | [diff] [blame] | 2380 | int ata_dev_configure(struct ata_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2381 | { |
Tejun Heo | 9af5c9c | 2007-08-06 18:36:22 +0900 | [diff] [blame] | 2382 | struct ata_port *ap = dev->link->ap; |
| 2383 | struct ata_eh_context *ehc = &dev->link->eh_context; |
Tejun Heo | 6746544 | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 2384 | int print_info = ehc->i.flags & ATA_EHI_PRINTINFO; |
Tejun Heo | 1148c3a | 2006-03-13 19:48:04 +0900 | [diff] [blame] | 2385 | const u16 *id = dev->id; |
Tejun Heo | 7dc951a | 2007-11-27 19:43:42 +0900 | [diff] [blame] | 2386 | unsigned long xfer_mask; |
Shane Huang | 65fe1f0 | 2012-09-07 22:40:01 +0800 | [diff] [blame] | 2387 | unsigned int err_mask; |
Alan Cox | b352e57 | 2006-08-10 18:52:12 +0100 | [diff] [blame] | 2388 | char revbuf[7]; /* XYZ-99\0 */ |
Eric D. Mudama | 3f64f56 | 2007-01-30 23:00:40 -0700 | [diff] [blame] | 2389 | char fwrevbuf[ATA_ID_FW_REV_LEN+1]; |
| 2390 | char modelbuf[ATA_ID_PROD_LEN+1]; |
Brian King | e6d902a | 2006-06-28 08:30:31 -0500 | [diff] [blame] | 2391 | int rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2392 | |
Borislav Petkov | 0dd4b21 | 2006-06-23 02:29:08 -0400 | [diff] [blame] | 2393 | if (!ata_dev_enabled(dev) && ata_msg_info(ap)) { |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 2394 | ata_dev_info(dev, "%s: ENTER/EXIT -- nodev\n", __func__); |
Tejun Heo | ffeae41 | 2006-03-01 16:09:35 +0900 | [diff] [blame] | 2395 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2396 | } |
| 2397 | |
Borislav Petkov | 0dd4b21 | 2006-06-23 02:29:08 -0400 | [diff] [blame] | 2398 | if (ata_msg_probe(ap)) |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 2399 | ata_dev_dbg(dev, "%s: ENTER\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2400 | |
Tejun Heo | 75683fe | 2007-07-05 13:31:27 +0900 | [diff] [blame] | 2401 | /* set horkage */ |
| 2402 | dev->horkage |= ata_dev_blacklisted(dev); |
Tejun Heo | 3326732 | 2008-02-13 09:15:09 +0900 | [diff] [blame] | 2403 | ata_force_horkage(dev); |
Tejun Heo | 75683fe | 2007-07-05 13:31:27 +0900 | [diff] [blame] | 2404 | |
Tejun Heo | 50af2fa | 2008-05-19 01:15:14 +0900 | [diff] [blame] | 2405 | if (dev->horkage & ATA_HORKAGE_DISABLE) { |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 2406 | ata_dev_info(dev, "unsupported device, disabling\n"); |
Tejun Heo | 50af2fa | 2008-05-19 01:15:14 +0900 | [diff] [blame] | 2407 | ata_dev_disable(dev); |
| 2408 | return 0; |
| 2409 | } |
| 2410 | |
Tejun Heo | 2486fa5 | 2008-07-31 07:52:40 +0900 | [diff] [blame] | 2411 | if ((!atapi_enabled || (ap->flags & ATA_FLAG_NO_ATAPI)) && |
| 2412 | dev->class == ATA_DEV_ATAPI) { |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 2413 | ata_dev_warn(dev, "WARNING: ATAPI is %s, device ignored\n", |
| 2414 | atapi_enabled ? "not supported with this driver" |
| 2415 | : "disabled"); |
Tejun Heo | 2486fa5 | 2008-07-31 07:52:40 +0900 | [diff] [blame] | 2416 | ata_dev_disable(dev); |
| 2417 | return 0; |
| 2418 | } |
| 2419 | |
Tejun Heo | 9062712 | 2009-01-29 20:31:36 +0900 | [diff] [blame] | 2420 | rc = ata_do_link_spd_horkage(dev); |
| 2421 | if (rc) |
| 2422 | return rc; |
| 2423 | |
Tejun Heo | ecd75ad | 2014-01-16 09:47:17 -0500 | [diff] [blame] | 2424 | /* some WD SATA-1 drives have issues with LPM, turn on NOLPM for them */ |
| 2425 | if ((dev->horkage & ATA_HORKAGE_WD_BROKEN_LPM) && |
| 2426 | (id[ATA_ID_SATA_CAPABILITY] & 0xe) == 0x2) |
| 2427 | dev->horkage |= ATA_HORKAGE_NOLPM; |
| 2428 | |
Hans de Goede | 240630e | 2018-07-01 12:15:46 +0200 | [diff] [blame] | 2429 | if (ap->flags & ATA_FLAG_NO_LPM) |
| 2430 | dev->horkage |= ATA_HORKAGE_NOLPM; |
| 2431 | |
Tejun Heo | ecd75ad | 2014-01-16 09:47:17 -0500 | [diff] [blame] | 2432 | if (dev->horkage & ATA_HORKAGE_NOLPM) { |
| 2433 | ata_dev_warn(dev, "LPM support broken, forcing max_power\n"); |
| 2434 | dev->link->ap->target_lpm_policy = ATA_LPM_MAX_POWER; |
| 2435 | } |
| 2436 | |
Tejun Heo | 6746544 | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 2437 | /* let ACPI work its magic */ |
| 2438 | rc = ata_acpi_on_devcfg(dev); |
| 2439 | if (rc) |
| 2440 | return rc; |
Kristen Carlson Accardi | 08573a8 | 2006-11-10 16:14:47 -0800 | [diff] [blame] | 2441 | |
Tejun Heo | 05027ad | 2007-09-03 12:32:57 +0900 | [diff] [blame] | 2442 | /* massage HPA, do it early as it might change IDENTIFY data */ |
| 2443 | rc = ata_hpa_resize(dev); |
| 2444 | if (rc) |
| 2445 | return rc; |
| 2446 | |
Tejun Heo | c39f5eb | 2006-03-13 19:51:19 +0900 | [diff] [blame] | 2447 | /* print device capabilities */ |
Borislav Petkov | 0dd4b21 | 2006-06-23 02:29:08 -0400 | [diff] [blame] | 2448 | if (ata_msg_probe(ap)) |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 2449 | ata_dev_dbg(dev, |
| 2450 | "%s: cfg 49:%04x 82:%04x 83:%04x 84:%04x " |
| 2451 | "85:%04x 86:%04x 87:%04x 88:%04x\n", |
| 2452 | __func__, |
| 2453 | id[49], id[82], id[83], id[84], |
| 2454 | id[85], id[86], id[87], id[88]); |
Tejun Heo | c39f5eb | 2006-03-13 19:51:19 +0900 | [diff] [blame] | 2455 | |
Tejun Heo | 208a993 | 2006-03-05 17:55:58 +0900 | [diff] [blame] | 2456 | /* initialize to-be-configured parameters */ |
Tejun Heo | ea1dd4e | 2006-04-02 18:51:53 +0900 | [diff] [blame] | 2457 | dev->flags &= ~ATA_DFLAG_CFG_MASK; |
Tejun Heo | 208a993 | 2006-03-05 17:55:58 +0900 | [diff] [blame] | 2458 | dev->max_sectors = 0; |
| 2459 | dev->cdb_len = 0; |
| 2460 | dev->n_sectors = 0; |
| 2461 | dev->cylinders = 0; |
| 2462 | dev->heads = 0; |
| 2463 | dev->sectors = 0; |
Mark Lord | e18086d | 2009-03-19 13:32:21 -0400 | [diff] [blame] | 2464 | dev->multi_count = 0; |
Tejun Heo | 208a993 | 2006-03-05 17:55:58 +0900 | [diff] [blame] | 2465 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2466 | /* |
| 2467 | * common ATA, ATAPI feature tests |
| 2468 | */ |
| 2469 | |
Tejun Heo | ff8854b | 2006-03-06 04:31:56 +0900 | [diff] [blame] | 2470 | /* find max transfer mode; for printk only */ |
Tejun Heo | 1148c3a | 2006-03-13 19:48:04 +0900 | [diff] [blame] | 2471 | xfer_mask = ata_id_xfermask(id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2472 | |
Borislav Petkov | 0dd4b21 | 2006-06-23 02:29:08 -0400 | [diff] [blame] | 2473 | if (ata_msg_probe(ap)) |
| 2474 | ata_dump_id(id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2475 | |
Albert Lee | ef143d5 | 2007-06-05 13:01:33 +0800 | [diff] [blame] | 2476 | /* SCSI only uses 4-char revisions, dump full 8 chars from ATA */ |
| 2477 | ata_id_c_string(dev->id, fwrevbuf, ATA_ID_FW_REV, |
| 2478 | sizeof(fwrevbuf)); |
| 2479 | |
| 2480 | ata_id_c_string(dev->id, modelbuf, ATA_ID_PROD, |
| 2481 | sizeof(modelbuf)); |
| 2482 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2483 | /* ATA-specific feature tests */ |
Hannes Reinecke | 9162c65 | 2014-11-05 13:08:21 +0100 | [diff] [blame] | 2484 | if (dev->class == ATA_DEV_ATA || dev->class == ATA_DEV_ZAC) { |
Alan Cox | b352e57 | 2006-08-10 18:52:12 +0100 | [diff] [blame] | 2485 | if (ata_id_is_cfa(id)) { |
Sergei Shtylyov | 62afe5d | 2009-04-13 20:50:00 +0400 | [diff] [blame] | 2486 | /* CPRM may make this media unusable */ |
| 2487 | if (id[ATA_ID_CFA_KEY_MGMT] & 1) |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 2488 | ata_dev_warn(dev, |
| 2489 | "supports DRM functions and may not be fully accessible\n"); |
Alan Cox | b352e57 | 2006-08-10 18:52:12 +0100 | [diff] [blame] | 2490 | snprintf(revbuf, 7, "CFA"); |
Alan Cox | ae8d4ee | 2007-11-04 22:05:49 -0500 | [diff] [blame] | 2491 | } else { |
Jeff Garzik | 2dcb407 | 2007-10-19 06:42:56 -0400 | [diff] [blame] | 2492 | snprintf(revbuf, 7, "ATA-%d", ata_id_major_version(id)); |
Alan Cox | ae8d4ee | 2007-11-04 22:05:49 -0500 | [diff] [blame] | 2493 | /* Warn the user if the device has TPM extensions */ |
| 2494 | if (ata_id_has_tpm(id)) |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 2495 | ata_dev_warn(dev, |
| 2496 | "supports DRM functions and may not be fully accessible\n"); |
Alan Cox | ae8d4ee | 2007-11-04 22:05:49 -0500 | [diff] [blame] | 2497 | } |
Alan Cox | b352e57 | 2006-08-10 18:52:12 +0100 | [diff] [blame] | 2498 | |
Tejun Heo | 1148c3a | 2006-03-13 19:48:04 +0900 | [diff] [blame] | 2499 | dev->n_sectors = ata_id_n_sectors(id); |
Tejun Heo | 2940740 | 2006-02-12 22:47:04 +0900 | [diff] [blame] | 2500 | |
Mark Lord | e18086d | 2009-03-19 13:32:21 -0400 | [diff] [blame] | 2501 | /* get current R/W Multiple count setting */ |
| 2502 | if ((dev->id[47] >> 8) == 0x80 && (dev->id[59] & 0x100)) { |
| 2503 | unsigned int max = dev->id[47] & 0xff; |
| 2504 | unsigned int cnt = dev->id[59] & 0xff; |
| 2505 | /* only recognize/allow powers of two here */ |
| 2506 | if (is_power_of_2(max) && is_power_of_2(cnt)) |
| 2507 | if (cnt <= max) |
| 2508 | dev->multi_count = cnt; |
| 2509 | } |
Eric D. Mudama | 3f64f56 | 2007-01-30 23:00:40 -0700 | [diff] [blame] | 2510 | |
Tejun Heo | 1148c3a | 2006-03-13 19:48:04 +0900 | [diff] [blame] | 2511 | if (ata_id_has_lba(id)) { |
Tejun Heo | 4c2d721 | 2006-03-05 17:55:58 +0900 | [diff] [blame] | 2512 | const char *lba_desc; |
Shaohua Li | 388539f | 2009-07-27 09:24:35 +0800 | [diff] [blame] | 2513 | char ncq_desc[24]; |
Albert Lee | 8bf62ece | 2005-05-12 15:29:42 -0400 | [diff] [blame] | 2514 | |
Tejun Heo | 4c2d721 | 2006-03-05 17:55:58 +0900 | [diff] [blame] | 2515 | lba_desc = "LBA"; |
| 2516 | dev->flags |= ATA_DFLAG_LBA; |
Tejun Heo | 1148c3a | 2006-03-13 19:48:04 +0900 | [diff] [blame] | 2517 | if (ata_id_has_lba48(id)) { |
Albert Lee | 8bf62ece | 2005-05-12 15:29:42 -0400 | [diff] [blame] | 2518 | dev->flags |= ATA_DFLAG_LBA48; |
Tejun Heo | 4c2d721 | 2006-03-05 17:55:58 +0900 | [diff] [blame] | 2519 | lba_desc = "LBA48"; |
Tejun Heo | 6fc49ad | 2006-11-11 20:10:45 +0900 | [diff] [blame] | 2520 | |
| 2521 | if (dev->n_sectors >= (1UL << 28) && |
| 2522 | ata_id_has_flush_ext(id)) |
| 2523 | dev->flags |= ATA_DFLAG_FLUSH_EXT; |
Tejun Heo | 4c2d721 | 2006-03-05 17:55:58 +0900 | [diff] [blame] | 2524 | } |
Albert Lee | 8bf62ece | 2005-05-12 15:29:42 -0400 | [diff] [blame] | 2525 | |
Tejun Heo | a6e6ce8 | 2006-05-15 21:03:48 +0900 | [diff] [blame] | 2526 | /* config NCQ */ |
Shaohua Li | 388539f | 2009-07-27 09:24:35 +0800 | [diff] [blame] | 2527 | rc = ata_dev_config_ncq(dev, ncq_desc, sizeof(ncq_desc)); |
| 2528 | if (rc) |
| 2529 | return rc; |
Tejun Heo | a6e6ce8 | 2006-05-15 21:03:48 +0900 | [diff] [blame] | 2530 | |
Albert Lee | 8bf62ece | 2005-05-12 15:29:42 -0400 | [diff] [blame] | 2531 | /* print device info to dmesg */ |
Eric D. Mudama | 3f64f56 | 2007-01-30 23:00:40 -0700 | [diff] [blame] | 2532 | if (ata_msg_drv(ap) && print_info) { |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 2533 | ata_dev_info(dev, "%s: %s, %s, max %s\n", |
| 2534 | revbuf, modelbuf, fwrevbuf, |
| 2535 | ata_mode_string(xfer_mask)); |
| 2536 | ata_dev_info(dev, |
| 2537 | "%llu sectors, multi %u: %s %s\n", |
Tejun Heo | f15a1da | 2006-05-15 20:57:56 +0900 | [diff] [blame] | 2538 | (unsigned long long)dev->n_sectors, |
Eric D. Mudama | 3f64f56 | 2007-01-30 23:00:40 -0700 | [diff] [blame] | 2539 | dev->multi_count, lba_desc, ncq_desc); |
| 2540 | } |
Tejun Heo | ffeae41 | 2006-03-01 16:09:35 +0900 | [diff] [blame] | 2541 | } else { |
Albert Lee | 8bf62ece | 2005-05-12 15:29:42 -0400 | [diff] [blame] | 2542 | /* CHS */ |
| 2543 | |
| 2544 | /* Default translation */ |
Tejun Heo | 1148c3a | 2006-03-13 19:48:04 +0900 | [diff] [blame] | 2545 | dev->cylinders = id[1]; |
| 2546 | dev->heads = id[3]; |
| 2547 | dev->sectors = id[6]; |
Albert Lee | 8bf62ece | 2005-05-12 15:29:42 -0400 | [diff] [blame] | 2548 | |
Tejun Heo | 1148c3a | 2006-03-13 19:48:04 +0900 | [diff] [blame] | 2549 | if (ata_id_current_chs_valid(id)) { |
Albert Lee | 8bf62ece | 2005-05-12 15:29:42 -0400 | [diff] [blame] | 2550 | /* Current CHS translation is valid. */ |
Tejun Heo | 1148c3a | 2006-03-13 19:48:04 +0900 | [diff] [blame] | 2551 | dev->cylinders = id[54]; |
| 2552 | dev->heads = id[55]; |
| 2553 | dev->sectors = id[56]; |
Albert Lee | 8bf62ece | 2005-05-12 15:29:42 -0400 | [diff] [blame] | 2554 | } |
| 2555 | |
| 2556 | /* print device info to dmesg */ |
Eric D. Mudama | 3f64f56 | 2007-01-30 23:00:40 -0700 | [diff] [blame] | 2557 | if (ata_msg_drv(ap) && print_info) { |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 2558 | ata_dev_info(dev, "%s: %s, %s, max %s\n", |
| 2559 | revbuf, modelbuf, fwrevbuf, |
| 2560 | ata_mode_string(xfer_mask)); |
| 2561 | ata_dev_info(dev, |
| 2562 | "%llu sectors, multi %u, CHS %u/%u/%u\n", |
| 2563 | (unsigned long long)dev->n_sectors, |
| 2564 | dev->multi_count, dev->cylinders, |
| 2565 | dev->heads, dev->sectors); |
Eric D. Mudama | 3f64f56 | 2007-01-30 23:00:40 -0700 | [diff] [blame] | 2566 | } |
Albert Lee | 07f6f7d | 2005-11-01 19:33:20 +0800 | [diff] [blame] | 2567 | } |
| 2568 | |
Shane Huang | 803739d | 2012-12-17 23:18:59 +0800 | [diff] [blame] | 2569 | /* Check and mark DevSlp capability. Get DevSlp timing variables |
| 2570 | * from SATA Settings page of Identify Device Data Log. |
Shane Huang | 65fe1f0 | 2012-09-07 22:40:01 +0800 | [diff] [blame] | 2571 | */ |
Shane Huang | 803739d | 2012-12-17 23:18:59 +0800 | [diff] [blame] | 2572 | if (ata_id_has_devslp(dev->id)) { |
David Woodhouse | 8e725c7 | 2013-03-29 11:54:55 +0000 | [diff] [blame] | 2573 | u8 *sata_setting = ap->sector_buf; |
Shane Huang | 803739d | 2012-12-17 23:18:59 +0800 | [diff] [blame] | 2574 | int i, j; |
| 2575 | |
| 2576 | dev->flags |= ATA_DFLAG_DEVSLP; |
Shane Huang | 65fe1f0 | 2012-09-07 22:40:01 +0800 | [diff] [blame] | 2577 | err_mask = ata_read_log_page(dev, |
Christoph Hellwig | 1d51d5f | 2017-06-04 14:42:22 +0200 | [diff] [blame] | 2578 | ATA_LOG_IDENTIFY_DEVICE, |
Shane Huang | 65fe1f0 | 2012-09-07 22:40:01 +0800 | [diff] [blame] | 2579 | ATA_LOG_SATA_SETTINGS, |
Shane Huang | 803739d | 2012-12-17 23:18:59 +0800 | [diff] [blame] | 2580 | sata_setting, |
Shane Huang | 65fe1f0 | 2012-09-07 22:40:01 +0800 | [diff] [blame] | 2581 | 1); |
| 2582 | if (err_mask) |
| 2583 | ata_dev_dbg(dev, |
| 2584 | "failed to get Identify Device Data, Emask 0x%x\n", |
| 2585 | err_mask); |
Shane Huang | 803739d | 2012-12-17 23:18:59 +0800 | [diff] [blame] | 2586 | else |
| 2587 | for (i = 0; i < ATA_LOG_DEVSLP_SIZE; i++) { |
| 2588 | j = ATA_LOG_DEVSLP_OFFSET + i; |
| 2589 | dev->devslp_timing[i] = sata_setting[j]; |
| 2590 | } |
Shane Huang | 65fe1f0 | 2012-09-07 22:40:01 +0800 | [diff] [blame] | 2591 | } |
Hannes Reinecke | e87fd28 | 2016-04-04 11:43:55 +0200 | [diff] [blame] | 2592 | ata_dev_config_sense_reporting(dev); |
Hannes Reinecke | 6d1003a | 2016-04-25 12:45:56 +0200 | [diff] [blame] | 2593 | ata_dev_config_zac(dev); |
Christoph Hellwig | 818831c | 2017-06-04 14:42:24 +0200 | [diff] [blame] | 2594 | ata_dev_config_trusted(dev); |
Minwoo Im | b1ffbf8 | 2017-06-24 03:41:10 +0900 | [diff] [blame] | 2595 | dev->cdb_len = 32; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2596 | } |
| 2597 | |
| 2598 | /* ATAPI-specific feature tests */ |
Jeff Garzik | 2c13b7c | 2005-11-14 14:14:16 -0500 | [diff] [blame] | 2599 | else if (dev->class == ATA_DEV_ATAPI) { |
Tejun Heo | 854c73a | 2007-09-23 13:14:11 +0900 | [diff] [blame] | 2600 | const char *cdb_intr_string = ""; |
| 2601 | const char *atapi_an_string = ""; |
Tejun Heo | 9116300 | 2008-02-21 13:25:50 +0900 | [diff] [blame] | 2602 | const char *dma_dir_string = ""; |
Tejun Heo | 7d77b24 | 2007-09-23 13:14:13 +0900 | [diff] [blame] | 2603 | u32 sntf; |
Albert Lee | 08a556d | 2006-03-31 13:29:04 +0800 | [diff] [blame] | 2604 | |
Tejun Heo | 1148c3a | 2006-03-13 19:48:04 +0900 | [diff] [blame] | 2605 | rc = atapi_cdb_len(id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2606 | if ((rc < 12) || (rc > ATAPI_CDB_LEN)) { |
Borislav Petkov | 0dd4b21 | 2006-06-23 02:29:08 -0400 | [diff] [blame] | 2607 | if (ata_msg_warn(ap)) |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 2608 | ata_dev_warn(dev, "unsupported CDB len\n"); |
Tejun Heo | ffeae41 | 2006-03-01 16:09:35 +0900 | [diff] [blame] | 2609 | rc = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2610 | goto err_out_nosup; |
| 2611 | } |
Tejun Heo | 6e7846e | 2006-02-12 23:32:58 +0900 | [diff] [blame] | 2612 | dev->cdb_len = (unsigned int) rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2613 | |
Tejun Heo | 7d77b24 | 2007-09-23 13:14:13 +0900 | [diff] [blame] | 2614 | /* Enable ATAPI AN if both the host and device have |
| 2615 | * the support. If PMP is attached, SNTF is required |
| 2616 | * to enable ATAPI AN to discern between PHY status |
| 2617 | * changed notifications and ATAPI ANs. |
Kristen Carlson Accardi | 9f45cbd | 2007-08-15 03:57:11 -0400 | [diff] [blame] | 2618 | */ |
Tejun Heo | e7ecd43 | 2010-05-19 15:38:58 +0200 | [diff] [blame] | 2619 | if (atapi_an && |
| 2620 | (ap->flags & ATA_FLAG_AN) && ata_id_has_atapi_AN(id) && |
Tejun Heo | 071f44b | 2008-04-07 22:47:22 +0900 | [diff] [blame] | 2621 | (!sata_pmp_attached(ap) || |
Tejun Heo | 7d77b24 | 2007-09-23 13:14:13 +0900 | [diff] [blame] | 2622 | sata_scr_read(&ap->link, SCR_NOTIFICATION, &sntf) == 0)) { |
Kristen Carlson Accardi | 9f45cbd | 2007-08-15 03:57:11 -0400 | [diff] [blame] | 2623 | /* issue SET feature command to turn this on */ |
Jeff Garzik | 218f3d3 | 2007-10-25 00:33:27 -0400 | [diff] [blame] | 2624 | err_mask = ata_dev_set_feature(dev, |
| 2625 | SETFEATURES_SATA_ENABLE, SATA_AN); |
Tejun Heo | 854c73a | 2007-09-23 13:14:11 +0900 | [diff] [blame] | 2626 | if (err_mask) |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 2627 | ata_dev_err(dev, |
| 2628 | "failed to enable ATAPI AN (err_mask=0x%x)\n", |
| 2629 | err_mask); |
Tejun Heo | 854c73a | 2007-09-23 13:14:11 +0900 | [diff] [blame] | 2630 | else { |
Kristen Carlson Accardi | 9f45cbd | 2007-08-15 03:57:11 -0400 | [diff] [blame] | 2631 | dev->flags |= ATA_DFLAG_AN; |
Tejun Heo | 854c73a | 2007-09-23 13:14:11 +0900 | [diff] [blame] | 2632 | atapi_an_string = ", ATAPI AN"; |
| 2633 | } |
Kristen Carlson Accardi | 9f45cbd | 2007-08-15 03:57:11 -0400 | [diff] [blame] | 2634 | } |
| 2635 | |
Albert Lee | 08a556d | 2006-03-31 13:29:04 +0800 | [diff] [blame] | 2636 | if (ata_id_cdb_intr(dev->id)) { |
Albert Lee | 312f7da | 2005-09-27 17:38:03 +0800 | [diff] [blame] | 2637 | dev->flags |= ATA_DFLAG_CDB_INTR; |
Albert Lee | 08a556d | 2006-03-31 13:29:04 +0800 | [diff] [blame] | 2638 | cdb_intr_string = ", CDB intr"; |
| 2639 | } |
Albert Lee | 312f7da | 2005-09-27 17:38:03 +0800 | [diff] [blame] | 2640 | |
Vincent Pelletier | 966fbe1 | 2013-05-21 22:30:58 +0200 | [diff] [blame] | 2641 | 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] | 2642 | dev->flags |= ATA_DFLAG_DMADIR; |
| 2643 | dma_dir_string = ", DMADIR"; |
| 2644 | } |
| 2645 | |
Aaron Lu | afe7595 | 2013-01-15 17:20:58 +0800 | [diff] [blame] | 2646 | if (ata_id_has_da(dev->id)) { |
Lin Ming | b1354cb | 2012-06-25 16:13:08 +0800 | [diff] [blame] | 2647 | dev->flags |= ATA_DFLAG_DA; |
Aaron Lu | afe7595 | 2013-01-15 17:20:58 +0800 | [diff] [blame] | 2648 | zpodd_init(dev); |
| 2649 | } |
Lin Ming | b1354cb | 2012-06-25 16:13:08 +0800 | [diff] [blame] | 2650 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2651 | /* print device info to dmesg */ |
Borislav Petkov | 5afc814 | 2006-06-27 14:51:25 +0200 | [diff] [blame] | 2652 | if (ata_msg_drv(ap) && print_info) |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 2653 | ata_dev_info(dev, |
| 2654 | "ATAPI: %s, %s, max %s%s%s%s\n", |
| 2655 | modelbuf, fwrevbuf, |
| 2656 | ata_mode_string(xfer_mask), |
| 2657 | cdb_intr_string, atapi_an_string, |
| 2658 | dma_dir_string); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2659 | } |
| 2660 | |
Tejun Heo | 914ed35 | 2006-11-01 18:39:55 +0900 | [diff] [blame] | 2661 | /* determine max_sectors */ |
| 2662 | dev->max_sectors = ATA_MAX_SECTORS; |
| 2663 | if (dev->flags & ATA_DFLAG_LBA48) |
| 2664 | dev->max_sectors = ATA_MAX_SECTORS_LBA48; |
| 2665 | |
Alan Cox | c5038fc | 2007-10-25 14:21:16 +0100 | [diff] [blame] | 2666 | /* Limit PATA drive on SATA cable bridge transfers to udma5, |
| 2667 | 200 sectors */ |
Tejun Heo | 3373efd | 2006-05-15 20:57:53 +0900 | [diff] [blame] | 2668 | if (ata_dev_knobble(dev)) { |
Borislav Petkov | 5afc814 | 2006-06-27 14:51:25 +0200 | [diff] [blame] | 2669 | if (ata_msg_drv(ap) && print_info) |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 2670 | ata_dev_info(dev, "applying bridge limits\n"); |
Tejun Heo | 5a52913 | 2006-03-24 14:07:50 +0900 | [diff] [blame] | 2671 | dev->udma_mask &= ATA_UDMA5; |
Tejun Heo | 4b2f3ed | 2006-03-01 16:09:36 +0900 | [diff] [blame] | 2672 | dev->max_sectors = ATA_MAX_SECTORS; |
| 2673 | } |
| 2674 | |
Tony Battersby | f8d8e57 | 2007-10-30 11:44:35 -0400 | [diff] [blame] | 2675 | if ((dev->class == ATA_DEV_ATAPI) && |
Albert Lee | f442cd8 | 2007-11-15 10:35:47 +0900 | [diff] [blame] | 2676 | (atapi_command_packet_set(id) == TYPE_TAPE)) { |
Tony Battersby | f8d8e57 | 2007-10-30 11:44:35 -0400 | [diff] [blame] | 2677 | dev->max_sectors = ATA_MAX_SECTORS_TAPE; |
Albert Lee | f442cd8 | 2007-11-15 10:35:47 +0900 | [diff] [blame] | 2678 | dev->horkage |= ATA_HORKAGE_STUCK_ERR; |
| 2679 | } |
Tony Battersby | f8d8e57 | 2007-10-30 11:44:35 -0400 | [diff] [blame] | 2680 | |
Tejun Heo | 75683fe | 2007-07-05 13:31:27 +0900 | [diff] [blame] | 2681 | if (dev->horkage & ATA_HORKAGE_MAX_SEC_128) |
Tejun Heo | 03ec52d | 2007-04-12 13:38:11 +0900 | [diff] [blame] | 2682 | dev->max_sectors = min_t(unsigned int, ATA_MAX_SECTORS_128, |
| 2683 | dev->max_sectors); |
Albert Lee | 18d6e9d | 2007-04-02 11:34:15 +0800 | [diff] [blame] | 2684 | |
David Milburn | af34d63 | 2015-07-13 11:48:23 -0500 | [diff] [blame] | 2685 | if (dev->horkage & ATA_HORKAGE_MAX_SEC_1024) |
| 2686 | dev->max_sectors = min_t(unsigned int, ATA_MAX_SECTORS_1024, |
| 2687 | dev->max_sectors); |
| 2688 | |
Shan Hai | a32450e | 2013-03-18 10:30:44 +0800 | [diff] [blame] | 2689 | if (dev->horkage & ATA_HORKAGE_MAX_SEC_LBA48) |
| 2690 | dev->max_sectors = ATA_MAX_SECTORS_LBA48; |
| 2691 | |
Tejun Heo | 4b2f3ed | 2006-03-01 16:09:36 +0900 | [diff] [blame] | 2692 | if (ap->ops->dev_config) |
Alan | cd0d3bb | 2007-03-02 00:56:15 +0000 | [diff] [blame] | 2693 | ap->ops->dev_config(dev); |
Tejun Heo | 4b2f3ed | 2006-03-01 16:09:36 +0900 | [diff] [blame] | 2694 | |
Alan Cox | c5038fc | 2007-10-25 14:21:16 +0100 | [diff] [blame] | 2695 | if (dev->horkage & ATA_HORKAGE_DIAGNOSTIC) { |
| 2696 | /* Let the user know. We don't want to disallow opens for |
| 2697 | rescue purposes, or in case the vendor is just a blithering |
| 2698 | idiot. Do this after the dev_config call as some controllers |
| 2699 | with buggy firmware may want to avoid reporting false device |
| 2700 | bugs */ |
| 2701 | |
| 2702 | if (print_info) { |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 2703 | ata_dev_warn(dev, |
Alan Cox | c5038fc | 2007-10-25 14:21:16 +0100 | [diff] [blame] | 2704 | "Drive reports diagnostics failure. This may indicate a drive\n"); |
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 | "fault or invalid emulation. Contact drive vendor for information.\n"); |
| 2707 | } |
| 2708 | } |
| 2709 | |
Tejun Heo | ac70a96 | 2008-11-27 13:36:48 +0900 | [diff] [blame] | 2710 | if ((dev->horkage & ATA_HORKAGE_FIRMWARE_WARN) && print_info) { |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 2711 | ata_dev_warn(dev, "WARNING: device requires firmware update to be fully functional\n"); |
| 2712 | 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] | 2713 | } |
| 2714 | |
Tejun Heo | ffeae41 | 2006-03-01 16:09:35 +0900 | [diff] [blame] | 2715 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2716 | |
| 2717 | err_out_nosup: |
Borislav Petkov | 0dd4b21 | 2006-06-23 02:29:08 -0400 | [diff] [blame] | 2718 | if (ata_msg_probe(ap)) |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 2719 | ata_dev_dbg(dev, "%s: EXIT, err\n", __func__); |
Tejun Heo | ffeae41 | 2006-03-01 16:09:35 +0900 | [diff] [blame] | 2720 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2721 | } |
| 2722 | |
| 2723 | /** |
Alan Cox | 2e41e8e | 2007-03-08 23:19:19 +0000 | [diff] [blame] | 2724 | * ata_cable_40wire - return 40 wire cable type |
Alan Cox | be0d18d | 2007-03-06 02:37:56 -0800 | [diff] [blame] | 2725 | * @ap: port |
| 2726 | * |
Alan Cox | 2e41e8e | 2007-03-08 23:19:19 +0000 | [diff] [blame] | 2727 | * Helper method for drivers which want to hardwire 40 wire cable |
Alan Cox | be0d18d | 2007-03-06 02:37:56 -0800 | [diff] [blame] | 2728 | * detection. |
| 2729 | */ |
| 2730 | |
| 2731 | int ata_cable_40wire(struct ata_port *ap) |
| 2732 | { |
| 2733 | return ATA_CBL_PATA40; |
| 2734 | } |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 2735 | EXPORT_SYMBOL_GPL(ata_cable_40wire); |
Alan Cox | be0d18d | 2007-03-06 02:37:56 -0800 | [diff] [blame] | 2736 | |
| 2737 | /** |
Alan Cox | 2e41e8e | 2007-03-08 23:19:19 +0000 | [diff] [blame] | 2738 | * ata_cable_80wire - return 80 wire cable type |
Alan Cox | be0d18d | 2007-03-06 02:37:56 -0800 | [diff] [blame] | 2739 | * @ap: port |
| 2740 | * |
Alan Cox | 2e41e8e | 2007-03-08 23:19:19 +0000 | [diff] [blame] | 2741 | * Helper method for drivers which want to hardwire 80 wire cable |
Alan Cox | be0d18d | 2007-03-06 02:37:56 -0800 | [diff] [blame] | 2742 | * detection. |
| 2743 | */ |
| 2744 | |
| 2745 | int ata_cable_80wire(struct ata_port *ap) |
| 2746 | { |
| 2747 | return ATA_CBL_PATA80; |
| 2748 | } |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 2749 | EXPORT_SYMBOL_GPL(ata_cable_80wire); |
Alan Cox | be0d18d | 2007-03-06 02:37:56 -0800 | [diff] [blame] | 2750 | |
| 2751 | /** |
| 2752 | * ata_cable_unknown - return unknown PATA cable. |
| 2753 | * @ap: port |
| 2754 | * |
| 2755 | * Helper method for drivers which have no PATA cable detection. |
| 2756 | */ |
| 2757 | |
| 2758 | int ata_cable_unknown(struct ata_port *ap) |
| 2759 | { |
| 2760 | return ATA_CBL_PATA_UNK; |
| 2761 | } |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 2762 | EXPORT_SYMBOL_GPL(ata_cable_unknown); |
Alan Cox | be0d18d | 2007-03-06 02:37:56 -0800 | [diff] [blame] | 2763 | |
| 2764 | /** |
Tejun Heo | c88f90c | 2007-11-27 19:43:48 +0900 | [diff] [blame] | 2765 | * ata_cable_ignore - return ignored PATA cable. |
| 2766 | * @ap: port |
| 2767 | * |
| 2768 | * Helper method for drivers which don't use cable type to limit |
| 2769 | * transfer mode. |
| 2770 | */ |
| 2771 | int ata_cable_ignore(struct ata_port *ap) |
| 2772 | { |
| 2773 | return ATA_CBL_PATA_IGN; |
| 2774 | } |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 2775 | EXPORT_SYMBOL_GPL(ata_cable_ignore); |
Tejun Heo | c88f90c | 2007-11-27 19:43:48 +0900 | [diff] [blame] | 2776 | |
| 2777 | /** |
Alan Cox | be0d18d | 2007-03-06 02:37:56 -0800 | [diff] [blame] | 2778 | * ata_cable_sata - return SATA cable type |
| 2779 | * @ap: port |
| 2780 | * |
| 2781 | * Helper method for drivers which have SATA cables |
| 2782 | */ |
| 2783 | |
| 2784 | int ata_cable_sata(struct ata_port *ap) |
| 2785 | { |
| 2786 | return ATA_CBL_SATA; |
| 2787 | } |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 2788 | EXPORT_SYMBOL_GPL(ata_cable_sata); |
Alan Cox | be0d18d | 2007-03-06 02:37:56 -0800 | [diff] [blame] | 2789 | |
| 2790 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2791 | * ata_bus_probe - Reset and probe ATA bus |
| 2792 | * @ap: Bus to probe |
| 2793 | * |
Jeff Garzik | 0cba632 | 2005-05-30 19:49:12 -0400 | [diff] [blame] | 2794 | * Master ATA bus probing function. Initiates a hardware-dependent |
| 2795 | * bus reset, then attempts to identify any devices found on |
| 2796 | * the bus. |
| 2797 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2798 | * LOCKING: |
Jeff Garzik | 0cba632 | 2005-05-30 19:49:12 -0400 | [diff] [blame] | 2799 | * PCI/etc. bus probe sem. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2800 | * |
| 2801 | * RETURNS: |
Tejun Heo | 96072e6 | 2006-04-01 01:38:17 +0900 | [diff] [blame] | 2802 | * Zero on success, negative errno otherwise. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2803 | */ |
| 2804 | |
Brian King | 8028916 | 2006-08-07 14:27:31 -0500 | [diff] [blame] | 2805 | int ata_bus_probe(struct ata_port *ap) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2806 | { |
Tejun Heo | 28ca5c5 | 2006-03-01 16:09:36 +0900 | [diff] [blame] | 2807 | unsigned int classes[ATA_MAX_DEVICES]; |
Tejun Heo | 14d2bac | 2006-04-02 17:54:46 +0900 | [diff] [blame] | 2808 | int tries[ATA_MAX_DEVICES]; |
Tejun Heo | f58229f | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 2809 | int rc; |
Tejun Heo | e82cbdb | 2006-04-01 01:38:18 +0900 | [diff] [blame] | 2810 | struct ata_device *dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2811 | |
Tejun Heo | 1eca436 | 2008-11-03 20:03:17 +0900 | [diff] [blame] | 2812 | ata_for_each_dev(dev, &ap->link, ALL) |
Tejun Heo | f58229f | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 2813 | tries[dev->devno] = ATA_PROBE_MAX_TRIES; |
Tejun Heo | 14d2bac | 2006-04-02 17:54:46 +0900 | [diff] [blame] | 2814 | |
| 2815 | retry: |
Tejun Heo | 1eca436 | 2008-11-03 20:03:17 +0900 | [diff] [blame] | 2816 | ata_for_each_dev(dev, &ap->link, ALL) { |
Tejun Heo | cdeab11 | 2007-10-29 16:41:09 +0900 | [diff] [blame] | 2817 | /* If we issue an SRST then an ATA drive (not ATAPI) |
| 2818 | * may change configuration and be in PIO0 timing. If |
| 2819 | * we do a hard reset (or are coming from power on) |
| 2820 | * this is true for ATA or ATAPI. Until we've set a |
| 2821 | * suitable controller mode we should not touch the |
| 2822 | * bus as we may be talking too fast. |
| 2823 | */ |
| 2824 | dev->pio_mode = XFER_PIO_0; |
Aaron Lu | 5416912 | 2012-12-03 11:35:02 +0800 | [diff] [blame] | 2825 | dev->dma_mode = 0xff; |
Tejun Heo | cdeab11 | 2007-10-29 16:41:09 +0900 | [diff] [blame] | 2826 | |
| 2827 | /* If the controller has a pio mode setup function |
| 2828 | * then use it to set the chipset to rights. Don't |
| 2829 | * touch the DMA setup as that will be dealt with when |
| 2830 | * configuring devices. |
| 2831 | */ |
| 2832 | if (ap->ops->set_piomode) |
| 2833 | ap->ops->set_piomode(ap, dev); |
| 2834 | } |
| 2835 | |
Tejun Heo | 2044470 | 2006-03-13 01:57:01 +0900 | [diff] [blame] | 2836 | /* reset and determine device classes */ |
Tejun Heo | 52783c5 | 2006-05-31 18:28:22 +0900 | [diff] [blame] | 2837 | ap->ops->phy_reset(ap); |
Tejun Heo | 2061a47 | 2006-03-12 00:57:39 +0900 | [diff] [blame] | 2838 | |
Tejun Heo | 1eca436 | 2008-11-03 20:03:17 +0900 | [diff] [blame] | 2839 | ata_for_each_dev(dev, &ap->link, ALL) { |
Tejun Heo | 3e4ec34 | 2010-05-10 21:41:30 +0200 | [diff] [blame] | 2840 | if (dev->class != ATA_DEV_UNKNOWN) |
Tejun Heo | 52783c5 | 2006-05-31 18:28:22 +0900 | [diff] [blame] | 2841 | classes[dev->devno] = dev->class; |
| 2842 | else |
| 2843 | classes[dev->devno] = ATA_DEV_NONE; |
Tejun Heo | 2044470 | 2006-03-13 01:57:01 +0900 | [diff] [blame] | 2844 | |
Tejun Heo | 52783c5 | 2006-05-31 18:28:22 +0900 | [diff] [blame] | 2845 | dev->class = ATA_DEV_UNKNOWN; |
Tejun Heo | 28ca5c5 | 2006-03-01 16:09:36 +0900 | [diff] [blame] | 2846 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2847 | |
Jeff Garzik | f31f0cc | 2007-03-02 17:47:28 -0500 | [diff] [blame] | 2848 | /* read IDENTIFY page and configure devices. We have to do the identify |
| 2849 | specific sequence bass-ackwards so that PDIAG- is released by |
| 2850 | the slave device */ |
| 2851 | |
Tejun Heo | 1eca436 | 2008-11-03 20:03:17 +0900 | [diff] [blame] | 2852 | ata_for_each_dev(dev, &ap->link, ALL_REVERSE) { |
Tejun Heo | f58229f | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 2853 | if (tries[dev->devno]) |
| 2854 | dev->class = classes[dev->devno]; |
Tejun Heo | 14d2bac | 2006-04-02 17:54:46 +0900 | [diff] [blame] | 2855 | |
Tejun Heo | e1211e3 | 2006-04-01 01:38:18 +0900 | [diff] [blame] | 2856 | if (!ata_dev_enabled(dev)) |
Tejun Heo | ffeae41 | 2006-03-01 16:09:35 +0900 | [diff] [blame] | 2857 | continue; |
| 2858 | |
Tejun Heo | bff0464 | 2006-11-10 18:08:10 +0900 | [diff] [blame] | 2859 | rc = ata_dev_read_id(dev, &dev->class, ATA_READID_POSTRESET, |
| 2860 | dev->id); |
Tejun Heo | 14d2bac | 2006-04-02 17:54:46 +0900 | [diff] [blame] | 2861 | if (rc) |
| 2862 | goto fail; |
Jeff Garzik | f31f0cc | 2007-03-02 17:47:28 -0500 | [diff] [blame] | 2863 | } |
| 2864 | |
Alan Cox | be0d18d | 2007-03-06 02:37:56 -0800 | [diff] [blame] | 2865 | /* Now ask for the cable type as PDIAG- should have been released */ |
| 2866 | if (ap->ops->cable_detect) |
| 2867 | ap->cbl = ap->ops->cable_detect(ap); |
| 2868 | |
Tejun Heo | 1eca436 | 2008-11-03 20:03:17 +0900 | [diff] [blame] | 2869 | /* We may have SATA bridge glue hiding here irrespective of |
| 2870 | * the reported cable types and sensed types. When SATA |
| 2871 | * drives indicate we have a bridge, we don't know which end |
| 2872 | * of the link the bridge is which is a problem. |
| 2873 | */ |
| 2874 | ata_for_each_dev(dev, &ap->link, ENABLED) |
Alan Cox | 614fe29 | 2007-08-22 23:22:45 +0100 | [diff] [blame] | 2875 | if (ata_id_is_sata(dev->id)) |
| 2876 | ap->cbl = ATA_CBL_SATA; |
Alan Cox | 614fe29 | 2007-08-22 23:22:45 +0100 | [diff] [blame] | 2877 | |
Jeff Garzik | f31f0cc | 2007-03-02 17:47:28 -0500 | [diff] [blame] | 2878 | /* After the identify sequence we can now set up the devices. We do |
| 2879 | this in the normal order so that the user doesn't get confused */ |
| 2880 | |
Tejun Heo | 1eca436 | 2008-11-03 20:03:17 +0900 | [diff] [blame] | 2881 | ata_for_each_dev(dev, &ap->link, ENABLED) { |
Tejun Heo | 9af5c9c | 2007-08-06 18:36:22 +0900 | [diff] [blame] | 2882 | ap->link.eh_context.i.flags |= ATA_EHI_PRINTINFO; |
Tejun Heo | efdaedc | 2006-11-01 18:38:52 +0900 | [diff] [blame] | 2883 | rc = ata_dev_configure(dev); |
Tejun Heo | 9af5c9c | 2007-08-06 18:36:22 +0900 | [diff] [blame] | 2884 | ap->link.eh_context.i.flags &= ~ATA_EHI_PRINTINFO; |
Tejun Heo | 14d2bac | 2006-04-02 17:54:46 +0900 | [diff] [blame] | 2885 | if (rc) |
| 2886 | goto fail; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2887 | } |
| 2888 | |
Tejun Heo | e82cbdb | 2006-04-01 01:38:18 +0900 | [diff] [blame] | 2889 | /* configure transfer mode */ |
Tejun Heo | 0260731 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 2890 | rc = ata_set_mode(&ap->link, &dev); |
Tejun Heo | 4ae72a1 | 2007-02-02 16:22:30 +0900 | [diff] [blame] | 2891 | if (rc) |
Tejun Heo | 51713d3 | 2006-04-11 22:26:29 +0900 | [diff] [blame] | 2892 | goto fail; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2893 | |
Tejun Heo | 1eca436 | 2008-11-03 20:03:17 +0900 | [diff] [blame] | 2894 | ata_for_each_dev(dev, &ap->link, ENABLED) |
| 2895 | return 0; |
Alan Cox | e35a9e0 | 2006-03-27 18:46:37 +0100 | [diff] [blame] | 2896 | |
Tejun Heo | 96072e6 | 2006-04-01 01:38:17 +0900 | [diff] [blame] | 2897 | return -ENODEV; |
Tejun Heo | 14d2bac | 2006-04-02 17:54:46 +0900 | [diff] [blame] | 2898 | |
| 2899 | fail: |
Tejun Heo | 4ae72a1 | 2007-02-02 16:22:30 +0900 | [diff] [blame] | 2900 | tries[dev->devno]--; |
| 2901 | |
Tejun Heo | 14d2bac | 2006-04-02 17:54:46 +0900 | [diff] [blame] | 2902 | switch (rc) { |
| 2903 | case -EINVAL: |
Tejun Heo | 4ae72a1 | 2007-02-02 16:22:30 +0900 | [diff] [blame] | 2904 | /* eeek, something went very wrong, give up */ |
Tejun Heo | 14d2bac | 2006-04-02 17:54:46 +0900 | [diff] [blame] | 2905 | tries[dev->devno] = 0; |
| 2906 | break; |
Tejun Heo | 4ae72a1 | 2007-02-02 16:22:30 +0900 | [diff] [blame] | 2907 | |
| 2908 | case -ENODEV: |
| 2909 | /* give it just one more chance */ |
| 2910 | tries[dev->devno] = min(tries[dev->devno], 1); |
Gustavo A. R. Silva | 05b8360 | 2017-10-12 14:19:16 -0500 | [diff] [blame] | 2911 | /* fall through */ |
Tejun Heo | 14d2bac | 2006-04-02 17:54:46 +0900 | [diff] [blame] | 2912 | case -EIO: |
Tejun Heo | 4ae72a1 | 2007-02-02 16:22:30 +0900 | [diff] [blame] | 2913 | if (tries[dev->devno] == 1) { |
| 2914 | /* This is the last chance, better to slow |
| 2915 | * down than lose it. |
| 2916 | */ |
Tejun Heo | a07d499 | 2009-01-29 20:31:33 +0900 | [diff] [blame] | 2917 | sata_down_spd_limit(&ap->link, 0); |
Tejun Heo | 4ae72a1 | 2007-02-02 16:22:30 +0900 | [diff] [blame] | 2918 | ata_down_xfermask_limit(dev, ATA_DNXFER_PIO); |
| 2919 | } |
Tejun Heo | 14d2bac | 2006-04-02 17:54:46 +0900 | [diff] [blame] | 2920 | } |
| 2921 | |
Tejun Heo | 4ae72a1 | 2007-02-02 16:22:30 +0900 | [diff] [blame] | 2922 | if (!tries[dev->devno]) |
Tejun Heo | 3373efd | 2006-05-15 20:57:53 +0900 | [diff] [blame] | 2923 | ata_dev_disable(dev); |
Tejun Heo | ec57375 | 2006-04-11 22:26:29 +0900 | [diff] [blame] | 2924 | |
Tejun Heo | 14d2bac | 2006-04-02 17:54:46 +0900 | [diff] [blame] | 2925 | goto retry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2926 | } |
| 2927 | |
| 2928 | /** |
Tejun Heo | 3be680b | 2005-12-19 22:35:02 +0900 | [diff] [blame] | 2929 | * sata_print_link_status - Print SATA link status |
Tejun Heo | 936fd73 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 2930 | * @link: SATA link to printk link status about |
Tejun Heo | 3be680b | 2005-12-19 22:35:02 +0900 | [diff] [blame] | 2931 | * |
| 2932 | * This function prints link speed and status of a SATA link. |
| 2933 | * |
| 2934 | * LOCKING: |
| 2935 | * None. |
| 2936 | */ |
Adrian Bunk | 6bdb4fc | 2008-04-21 11:51:11 +0300 | [diff] [blame] | 2937 | static void sata_print_link_status(struct ata_link *link) |
Tejun Heo | 3be680b | 2005-12-19 22:35:02 +0900 | [diff] [blame] | 2938 | { |
Tejun Heo | 6d5f973 | 2006-04-03 00:09:41 +0900 | [diff] [blame] | 2939 | u32 sstatus, scontrol, tmp; |
Tejun Heo | 3be680b | 2005-12-19 22:35:02 +0900 | [diff] [blame] | 2940 | |
Tejun Heo | 936fd73 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 2941 | if (sata_scr_read(link, SCR_STATUS, &sstatus)) |
Tejun Heo | 3be680b | 2005-12-19 22:35:02 +0900 | [diff] [blame] | 2942 | return; |
Tejun Heo | 936fd73 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 2943 | sata_scr_read(link, SCR_CONTROL, &scontrol); |
Tejun Heo | 3be680b | 2005-12-19 22:35:02 +0900 | [diff] [blame] | 2944 | |
Tejun Heo | b1c7291 | 2008-07-31 17:02:43 +0900 | [diff] [blame] | 2945 | if (ata_phys_link_online(link)) { |
Tejun Heo | 3be680b | 2005-12-19 22:35:02 +0900 | [diff] [blame] | 2946 | tmp = (sstatus >> 4) & 0xf; |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 2947 | ata_link_info(link, "SATA link up %s (SStatus %X SControl %X)\n", |
| 2948 | sata_spd_string(tmp), sstatus, scontrol); |
Tejun Heo | 3be680b | 2005-12-19 22:35:02 +0900 | [diff] [blame] | 2949 | } else { |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 2950 | ata_link_info(link, "SATA link down (SStatus %X SControl %X)\n", |
| 2951 | sstatus, scontrol); |
Tejun Heo | 3be680b | 2005-12-19 22:35:02 +0900 | [diff] [blame] | 2952 | } |
| 2953 | } |
| 2954 | |
| 2955 | /** |
Alan Cox | ebdfca6e | 2006-03-23 15:38:34 +0000 | [diff] [blame] | 2956 | * ata_dev_pair - return other device on cable |
Alan Cox | ebdfca6e | 2006-03-23 15:38:34 +0000 | [diff] [blame] | 2957 | * @adev: device |
| 2958 | * |
| 2959 | * Obtain the other device on the same cable, or if none is |
| 2960 | * present NULL is returned |
| 2961 | */ |
Jeff Garzik | 2e9edbf | 2006-03-24 09:56:57 -0500 | [diff] [blame] | 2962 | |
Tejun Heo | 3373efd | 2006-05-15 20:57:53 +0900 | [diff] [blame] | 2963 | struct ata_device *ata_dev_pair(struct ata_device *adev) |
Alan Cox | ebdfca6e | 2006-03-23 15:38:34 +0000 | [diff] [blame] | 2964 | { |
Tejun Heo | 9af5c9c | 2007-08-06 18:36:22 +0900 | [diff] [blame] | 2965 | struct ata_link *link = adev->link; |
| 2966 | struct ata_device *pair = &link->device[1 - adev->devno]; |
Tejun Heo | e1211e3 | 2006-04-01 01:38:18 +0900 | [diff] [blame] | 2967 | if (!ata_dev_enabled(pair)) |
Alan Cox | ebdfca6e | 2006-03-23 15:38:34 +0000 | [diff] [blame] | 2968 | return NULL; |
| 2969 | return pair; |
| 2970 | } |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 2971 | EXPORT_SYMBOL_GPL(ata_dev_pair); |
Alan Cox | ebdfca6e | 2006-03-23 15:38:34 +0000 | [diff] [blame] | 2972 | |
| 2973 | /** |
Tejun Heo | 3c567b7 | 2006-05-15 20:57:23 +0900 | [diff] [blame] | 2974 | * sata_down_spd_limit - adjust SATA spd limit downward |
Tejun Heo | 936fd73 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 2975 | * @link: Link to adjust SATA spd limit for |
Tejun Heo | a07d499 | 2009-01-29 20:31:33 +0900 | [diff] [blame] | 2976 | * @spd_limit: Additional limit |
Tejun Heo | 1c3fae4 | 2006-04-02 20:53:28 +0900 | [diff] [blame] | 2977 | * |
Tejun Heo | 936fd73 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 2978 | * Adjust SATA spd limit of @link downward. Note that this |
Tejun Heo | 1c3fae4 | 2006-04-02 20:53:28 +0900 | [diff] [blame] | 2979 | * function only adjusts the limit. The change must be applied |
Tejun Heo | 3c567b7 | 2006-05-15 20:57:23 +0900 | [diff] [blame] | 2980 | * using sata_set_spd(). |
Tejun Heo | 1c3fae4 | 2006-04-02 20:53:28 +0900 | [diff] [blame] | 2981 | * |
Tejun Heo | a07d499 | 2009-01-29 20:31:33 +0900 | [diff] [blame] | 2982 | * If @spd_limit is non-zero, the speed is limited to equal to or |
| 2983 | * lower than @spd_limit if such speed is supported. If |
| 2984 | * @spd_limit is slower than any supported speed, only the lowest |
| 2985 | * supported speed is allowed. |
| 2986 | * |
Tejun Heo | 1c3fae4 | 2006-04-02 20:53:28 +0900 | [diff] [blame] | 2987 | * LOCKING: |
| 2988 | * Inherited from caller. |
| 2989 | * |
| 2990 | * RETURNS: |
| 2991 | * 0 on success, negative errno on failure |
| 2992 | */ |
Tejun Heo | a07d499 | 2009-01-29 20:31:33 +0900 | [diff] [blame] | 2993 | int sata_down_spd_limit(struct ata_link *link, u32 spd_limit) |
Tejun Heo | 1c3fae4 | 2006-04-02 20:53:28 +0900 | [diff] [blame] | 2994 | { |
Tejun Heo | 81952c5 | 2006-05-15 20:57:47 +0900 | [diff] [blame] | 2995 | u32 sstatus, spd, mask; |
Tejun Heo | a07d499 | 2009-01-29 20:31:33 +0900 | [diff] [blame] | 2996 | int rc, bit; |
Tejun Heo | 1c3fae4 | 2006-04-02 20:53:28 +0900 | [diff] [blame] | 2997 | |
Tejun Heo | 936fd73 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 2998 | if (!sata_scr_valid(link)) |
Tejun Heo | 008a789 | 2007-07-16 14:29:40 +0900 | [diff] [blame] | 2999 | return -EOPNOTSUPP; |
| 3000 | |
| 3001 | /* If SCR can be read, use it to determine the current SPD. |
Tejun Heo | 936fd73 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 3002 | * If not, use cached value in link->sata_spd. |
Tejun Heo | 008a789 | 2007-07-16 14:29:40 +0900 | [diff] [blame] | 3003 | */ |
Tejun Heo | 936fd73 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 3004 | rc = sata_scr_read(link, SCR_STATUS, &sstatus); |
Tejun Heo | 9913ff8a | 2009-01-29 20:31:31 +0900 | [diff] [blame] | 3005 | if (rc == 0 && ata_sstatus_online(sstatus)) |
Tejun Heo | 008a789 | 2007-07-16 14:29:40 +0900 | [diff] [blame] | 3006 | spd = (sstatus >> 4) & 0xf; |
| 3007 | else |
Tejun Heo | 936fd73 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 3008 | spd = link->sata_spd; |
Tejun Heo | 1c3fae4 | 2006-04-02 20:53:28 +0900 | [diff] [blame] | 3009 | |
Tejun Heo | 936fd73 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 3010 | mask = link->sata_spd_limit; |
Tejun Heo | 1c3fae4 | 2006-04-02 20:53:28 +0900 | [diff] [blame] | 3011 | if (mask <= 1) |
| 3012 | return -EINVAL; |
Tejun Heo | 008a789 | 2007-07-16 14:29:40 +0900 | [diff] [blame] | 3013 | |
| 3014 | /* unconditionally mask off the highest bit */ |
Tejun Heo | a07d499 | 2009-01-29 20:31:33 +0900 | [diff] [blame] | 3015 | bit = fls(mask) - 1; |
| 3016 | mask &= ~(1 << bit); |
Tejun Heo | 1c3fae4 | 2006-04-02 20:53:28 +0900 | [diff] [blame] | 3017 | |
David Milburn | 2dc0b46 | 2017-11-14 16:17:25 -0600 | [diff] [blame] | 3018 | /* |
| 3019 | * Mask off all speeds higher than or equal to the current one. At |
| 3020 | * this point, if current SPD is not available and we previously |
| 3021 | * recorded the link speed from SStatus, the driver has already |
| 3022 | * masked off the highest bit so mask should already be 1 or 0. |
| 3023 | * Otherwise, we should not force 1.5Gbps on a link where we have |
| 3024 | * not previously recorded speed from SStatus. Just return in this |
| 3025 | * case. |
Tejun Heo | 008a789 | 2007-07-16 14:29:40 +0900 | [diff] [blame] | 3026 | */ |
| 3027 | if (spd > 1) |
| 3028 | mask &= (1 << (spd - 1)) - 1; |
| 3029 | else |
David Milburn | 2dc0b46 | 2017-11-14 16:17:25 -0600 | [diff] [blame] | 3030 | return -EINVAL; |
Tejun Heo | 008a789 | 2007-07-16 14:29:40 +0900 | [diff] [blame] | 3031 | |
| 3032 | /* were we already at the bottom? */ |
Tejun Heo | 1c3fae4 | 2006-04-02 20:53:28 +0900 | [diff] [blame] | 3033 | if (!mask) |
| 3034 | return -EINVAL; |
| 3035 | |
Tejun Heo | a07d499 | 2009-01-29 20:31:33 +0900 | [diff] [blame] | 3036 | if (spd_limit) { |
| 3037 | if (mask & ((1 << spd_limit) - 1)) |
| 3038 | mask &= (1 << spd_limit) - 1; |
| 3039 | else { |
| 3040 | bit = ffs(mask) - 1; |
| 3041 | mask = 1 << bit; |
| 3042 | } |
| 3043 | } |
| 3044 | |
Tejun Heo | 936fd73 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 3045 | link->sata_spd_limit = mask; |
Tejun Heo | 1c3fae4 | 2006-04-02 20:53:28 +0900 | [diff] [blame] | 3046 | |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 3047 | ata_link_warn(link, "limiting SATA link speed to %s\n", |
| 3048 | sata_spd_string(fls(mask))); |
Tejun Heo | 1c3fae4 | 2006-04-02 20:53:28 +0900 | [diff] [blame] | 3049 | |
| 3050 | return 0; |
| 3051 | } |
| 3052 | |
Bartlomiej Zolnierkiewicz | a9b2c12 | 2020-03-26 16:58:07 +0100 | [diff] [blame] | 3053 | #ifdef CONFIG_ATA_ACPI |
Tejun Heo | cf176e1 | 2006-04-02 17:54:46 +0900 | [diff] [blame] | 3054 | /** |
Tejun Heo | a0f79b9 | 2007-12-18 16:33:05 +0900 | [diff] [blame] | 3055 | * ata_timing_cycle2mode - find xfer mode for the specified cycle duration |
| 3056 | * @xfer_shift: ATA_SHIFT_* value for transfer type to examine. |
| 3057 | * @cycle: cycle duration in ns |
| 3058 | * |
| 3059 | * Return matching xfer mode for @cycle. The returned mode is of |
| 3060 | * the transfer type specified by @xfer_shift. If @cycle is too |
| 3061 | * slow for @xfer_shift, 0xff is returned. If @cycle is faster |
| 3062 | * than the fastest known mode, the fasted mode is returned. |
| 3063 | * |
| 3064 | * LOCKING: |
| 3065 | * None. |
| 3066 | * |
| 3067 | * RETURNS: |
| 3068 | * Matching xfer_mode, 0xff if no match found. |
| 3069 | */ |
| 3070 | u8 ata_timing_cycle2mode(unsigned int xfer_shift, int cycle) |
| 3071 | { |
| 3072 | u8 base_mode = 0xff, last_mode = 0xff; |
| 3073 | const struct ata_xfer_ent *ent; |
| 3074 | const struct ata_timing *t; |
| 3075 | |
| 3076 | for (ent = ata_xfer_tbl; ent->shift >= 0; ent++) |
| 3077 | if (ent->shift == xfer_shift) |
| 3078 | base_mode = ent->base; |
| 3079 | |
| 3080 | for (t = ata_timing_find_mode(base_mode); |
| 3081 | t && ata_xfer_mode2shift(t->mode) == xfer_shift; t++) { |
| 3082 | unsigned short this_cycle; |
| 3083 | |
| 3084 | switch (xfer_shift) { |
| 3085 | case ATA_SHIFT_PIO: |
| 3086 | case ATA_SHIFT_MWDMA: |
| 3087 | this_cycle = t->cycle; |
| 3088 | break; |
| 3089 | case ATA_SHIFT_UDMA: |
| 3090 | this_cycle = t->udma; |
| 3091 | break; |
| 3092 | default: |
| 3093 | return 0xff; |
| 3094 | } |
| 3095 | |
| 3096 | if (cycle > this_cycle) |
| 3097 | break; |
| 3098 | |
| 3099 | last_mode = t->mode; |
| 3100 | } |
| 3101 | |
| 3102 | return last_mode; |
| 3103 | } |
Bartlomiej Zolnierkiewicz | a9b2c12 | 2020-03-26 16:58:07 +0100 | [diff] [blame] | 3104 | #endif |
Tejun Heo | a0f79b9 | 2007-12-18 16:33:05 +0900 | [diff] [blame] | 3105 | |
| 3106 | /** |
Tejun Heo | cf176e1 | 2006-04-02 17:54:46 +0900 | [diff] [blame] | 3107 | * ata_down_xfermask_limit - adjust dev xfer masks downward |
Tejun Heo | cf176e1 | 2006-04-02 17:54:46 +0900 | [diff] [blame] | 3108 | * @dev: Device to adjust xfer masks |
Tejun Heo | 458337d | 2007-02-02 16:22:30 +0900 | [diff] [blame] | 3109 | * @sel: ATA_DNXFER_* selector |
Tejun Heo | cf176e1 | 2006-04-02 17:54:46 +0900 | [diff] [blame] | 3110 | * |
| 3111 | * Adjust xfer masks of @dev downward. Note that this function |
| 3112 | * does not apply the change. Invoking ata_set_mode() afterwards |
| 3113 | * will apply the limit. |
| 3114 | * |
| 3115 | * LOCKING: |
| 3116 | * Inherited from caller. |
| 3117 | * |
| 3118 | * RETURNS: |
| 3119 | * 0 on success, negative errno on failure |
| 3120 | */ |
Tejun Heo | 458337d | 2007-02-02 16:22:30 +0900 | [diff] [blame] | 3121 | int ata_down_xfermask_limit(struct ata_device *dev, unsigned int sel) |
Tejun Heo | cf176e1 | 2006-04-02 17:54:46 +0900 | [diff] [blame] | 3122 | { |
Tejun Heo | 458337d | 2007-02-02 16:22:30 +0900 | [diff] [blame] | 3123 | char buf[32]; |
Tejun Heo | 7dc951a | 2007-11-27 19:43:42 +0900 | [diff] [blame] | 3124 | unsigned long orig_mask, xfer_mask; |
| 3125 | unsigned long pio_mask, mwdma_mask, udma_mask; |
Tejun Heo | 458337d | 2007-02-02 16:22:30 +0900 | [diff] [blame] | 3126 | int quiet, highbit; |
Tejun Heo | cf176e1 | 2006-04-02 17:54:46 +0900 | [diff] [blame] | 3127 | |
Tejun Heo | 458337d | 2007-02-02 16:22:30 +0900 | [diff] [blame] | 3128 | quiet = !!(sel & ATA_DNXFER_QUIET); |
| 3129 | sel &= ~ATA_DNXFER_QUIET; |
Tejun Heo | cf176e1 | 2006-04-02 17:54:46 +0900 | [diff] [blame] | 3130 | |
Tejun Heo | 458337d | 2007-02-02 16:22:30 +0900 | [diff] [blame] | 3131 | xfer_mask = orig_mask = ata_pack_xfermask(dev->pio_mask, |
| 3132 | dev->mwdma_mask, |
| 3133 | dev->udma_mask); |
| 3134 | ata_unpack_xfermask(xfer_mask, &pio_mask, &mwdma_mask, &udma_mask); |
Tejun Heo | cf176e1 | 2006-04-02 17:54:46 +0900 | [diff] [blame] | 3135 | |
Tejun Heo | 458337d | 2007-02-02 16:22:30 +0900 | [diff] [blame] | 3136 | switch (sel) { |
| 3137 | case ATA_DNXFER_PIO: |
| 3138 | highbit = fls(pio_mask) - 1; |
| 3139 | pio_mask &= ~(1 << highbit); |
| 3140 | break; |
| 3141 | |
| 3142 | case ATA_DNXFER_DMA: |
| 3143 | if (udma_mask) { |
| 3144 | highbit = fls(udma_mask) - 1; |
| 3145 | udma_mask &= ~(1 << highbit); |
| 3146 | if (!udma_mask) |
| 3147 | return -ENOENT; |
| 3148 | } else if (mwdma_mask) { |
| 3149 | highbit = fls(mwdma_mask) - 1; |
| 3150 | mwdma_mask &= ~(1 << highbit); |
| 3151 | if (!mwdma_mask) |
| 3152 | return -ENOENT; |
| 3153 | } |
| 3154 | break; |
| 3155 | |
| 3156 | case ATA_DNXFER_40C: |
| 3157 | udma_mask &= ATA_UDMA_MASK_40C; |
| 3158 | break; |
| 3159 | |
| 3160 | case ATA_DNXFER_FORCE_PIO0: |
| 3161 | pio_mask &= 1; |
Gustavo A. R. Silva | 05b8360 | 2017-10-12 14:19:16 -0500 | [diff] [blame] | 3162 | /* fall through */ |
Tejun Heo | 458337d | 2007-02-02 16:22:30 +0900 | [diff] [blame] | 3163 | case ATA_DNXFER_FORCE_PIO: |
| 3164 | mwdma_mask = 0; |
| 3165 | udma_mask = 0; |
| 3166 | break; |
| 3167 | |
Tejun Heo | 458337d | 2007-02-02 16:22:30 +0900 | [diff] [blame] | 3168 | default: |
| 3169 | BUG(); |
| 3170 | } |
| 3171 | |
| 3172 | xfer_mask &= ata_pack_xfermask(pio_mask, mwdma_mask, udma_mask); |
| 3173 | |
| 3174 | if (!(xfer_mask & ATA_MASK_PIO) || xfer_mask == orig_mask) |
| 3175 | return -ENOENT; |
| 3176 | |
| 3177 | if (!quiet) { |
| 3178 | if (xfer_mask & (ATA_MASK_MWDMA | ATA_MASK_UDMA)) |
| 3179 | snprintf(buf, sizeof(buf), "%s:%s", |
| 3180 | ata_mode_string(xfer_mask), |
| 3181 | ata_mode_string(xfer_mask & ATA_MASK_PIO)); |
| 3182 | else |
| 3183 | snprintf(buf, sizeof(buf), "%s", |
| 3184 | ata_mode_string(xfer_mask)); |
| 3185 | |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 3186 | ata_dev_warn(dev, "limiting speed to %s\n", buf); |
Tejun Heo | 458337d | 2007-02-02 16:22:30 +0900 | [diff] [blame] | 3187 | } |
Tejun Heo | cf176e1 | 2006-04-02 17:54:46 +0900 | [diff] [blame] | 3188 | |
| 3189 | ata_unpack_xfermask(xfer_mask, &dev->pio_mask, &dev->mwdma_mask, |
| 3190 | &dev->udma_mask); |
| 3191 | |
Tejun Heo | cf176e1 | 2006-04-02 17:54:46 +0900 | [diff] [blame] | 3192 | return 0; |
Tejun Heo | cf176e1 | 2006-04-02 17:54:46 +0900 | [diff] [blame] | 3193 | } |
| 3194 | |
Tejun Heo | 3373efd | 2006-05-15 20:57:53 +0900 | [diff] [blame] | 3195 | static int ata_dev_set_mode(struct ata_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3196 | { |
Tejun Heo | d0cb43b | 2009-07-09 09:27:50 +0900 | [diff] [blame] | 3197 | struct ata_port *ap = dev->link->ap; |
Tejun Heo | 9af5c9c | 2007-08-06 18:36:22 +0900 | [diff] [blame] | 3198 | struct ata_eh_context *ehc = &dev->link->eh_context; |
Tejun Heo | d0cb43b | 2009-07-09 09:27:50 +0900 | [diff] [blame] | 3199 | const bool nosetxfer = dev->horkage & ATA_HORKAGE_NOSETXFER; |
Tejun Heo | 4055dee | 2008-02-07 10:34:08 +0900 | [diff] [blame] | 3200 | const char *dev_err_whine = ""; |
| 3201 | int ign_dev_err = 0; |
Tejun Heo | d0cb43b | 2009-07-09 09:27:50 +0900 | [diff] [blame] | 3202 | unsigned int err_mask = 0; |
Tejun Heo | 83206a2 | 2006-03-24 15:25:31 +0900 | [diff] [blame] | 3203 | int rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3204 | |
Tejun Heo | e838460 | 2006-04-02 18:51:53 +0900 | [diff] [blame] | 3205 | dev->flags &= ~ATA_DFLAG_PIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3206 | if (dev->xfer_shift == ATA_SHIFT_PIO) |
| 3207 | dev->flags |= ATA_DFLAG_PIO; |
| 3208 | |
Tejun Heo | d0cb43b | 2009-07-09 09:27:50 +0900 | [diff] [blame] | 3209 | if (nosetxfer && ap->flags & ATA_FLAG_SATA && ata_id_is_sata(dev->id)) |
| 3210 | dev_err_whine = " (SET_XFERMODE skipped)"; |
| 3211 | else { |
| 3212 | if (nosetxfer) |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 3213 | ata_dev_warn(dev, |
| 3214 | "NOSETXFER but PATA detected - can't " |
| 3215 | "skip SETXFER, might malfunction\n"); |
Tejun Heo | d0cb43b | 2009-07-09 09:27:50 +0900 | [diff] [blame] | 3216 | err_mask = ata_dev_set_xfermode(dev); |
| 3217 | } |
Jeff Garzik | 2dcb407 | 2007-10-19 06:42:56 -0400 | [diff] [blame] | 3218 | |
Tejun Heo | 4055dee | 2008-02-07 10:34:08 +0900 | [diff] [blame] | 3219 | if (err_mask & ~AC_ERR_DEV) |
| 3220 | goto fail; |
Jeff Garzik | 2dcb407 | 2007-10-19 06:42:56 -0400 | [diff] [blame] | 3221 | |
Tejun Heo | 4055dee | 2008-02-07 10:34:08 +0900 | [diff] [blame] | 3222 | /* revalidate */ |
Tejun Heo | baa1e78 | 2006-11-01 18:39:27 +0900 | [diff] [blame] | 3223 | ehc->i.flags |= ATA_EHI_POST_SETMODE; |
Tejun Heo | 422c9da | 2007-09-23 13:14:12 +0900 | [diff] [blame] | 3224 | rc = ata_dev_revalidate(dev, ATA_DEV_UNKNOWN, 0); |
Tejun Heo | baa1e78 | 2006-11-01 18:39:27 +0900 | [diff] [blame] | 3225 | ehc->i.flags &= ~ATA_EHI_POST_SETMODE; |
Tejun Heo | 5eb45c0 | 2006-04-02 18:51:52 +0900 | [diff] [blame] | 3226 | if (rc) |
Tejun Heo | 83206a2 | 2006-03-24 15:25:31 +0900 | [diff] [blame] | 3227 | return rc; |
Tejun Heo | 48a8a14 | 2006-03-05 17:55:58 +0900 | [diff] [blame] | 3228 | |
Alan Cox | b93fda1 | 2008-04-08 16:36:44 +0100 | [diff] [blame] | 3229 | if (dev->xfer_shift == ATA_SHIFT_PIO) { |
| 3230 | /* Old CFA may refuse this command, which is just fine */ |
| 3231 | if (ata_id_is_cfa(dev->id)) |
| 3232 | ign_dev_err = 1; |
| 3233 | /* Catch several broken garbage emulations plus some pre |
| 3234 | ATA devices */ |
| 3235 | if (ata_id_major_version(dev->id) == 0 && |
| 3236 | dev->pio_mode <= XFER_PIO_2) |
| 3237 | ign_dev_err = 1; |
| 3238 | /* Some very old devices and some bad newer ones fail |
| 3239 | any kind of SET_XFERMODE request but support PIO0-2 |
| 3240 | timings and no IORDY */ |
| 3241 | if (!ata_id_has_iordy(dev->id) && dev->pio_mode <= XFER_PIO_2) |
| 3242 | ign_dev_err = 1; |
| 3243 | } |
Tejun Heo | 4055dee | 2008-02-07 10:34:08 +0900 | [diff] [blame] | 3244 | /* Early MWDMA devices do DMA but don't allow DMA mode setting. |
| 3245 | Don't fail an MWDMA0 set IFF the device indicates it is in MWDMA0 */ |
| 3246 | if (dev->xfer_shift == ATA_SHIFT_MWDMA && |
| 3247 | dev->dma_mode == XFER_MW_DMA_0 && |
| 3248 | (dev->id[63] >> 8) & 1) |
| 3249 | ign_dev_err = 1; |
| 3250 | |
| 3251 | /* if the device is actually configured correctly, ignore dev err */ |
| 3252 | if (dev->xfer_mode == ata_xfer_mask2mode(ata_id_xfermask(dev->id))) |
| 3253 | ign_dev_err = 1; |
| 3254 | |
| 3255 | if (err_mask & AC_ERR_DEV) { |
| 3256 | if (!ign_dev_err) |
| 3257 | goto fail; |
| 3258 | else |
| 3259 | dev_err_whine = " (device error ignored)"; |
| 3260 | } |
| 3261 | |
Tejun Heo | 23e71c3 | 2006-03-06 04:31:57 +0900 | [diff] [blame] | 3262 | DPRINTK("xfer_shift=%u, xfer_mode=0x%x\n", |
| 3263 | dev->xfer_shift, (int)dev->xfer_mode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3264 | |
Damien Le Moal | 07b9b6d | 2018-05-09 09:28:10 +0900 | [diff] [blame] | 3265 | if (!(ehc->i.flags & ATA_EHI_QUIET) || |
| 3266 | ehc->i.flags & ATA_EHI_DID_HARDRESET) |
| 3267 | ata_dev_info(dev, "configured for %s%s\n", |
| 3268 | ata_mode_string(ata_xfer_mode2mask(dev->xfer_mode)), |
| 3269 | dev_err_whine); |
Tejun Heo | 4055dee | 2008-02-07 10:34:08 +0900 | [diff] [blame] | 3270 | |
Tejun Heo | 83206a2 | 2006-03-24 15:25:31 +0900 | [diff] [blame] | 3271 | return 0; |
Tejun Heo | 4055dee | 2008-02-07 10:34:08 +0900 | [diff] [blame] | 3272 | |
| 3273 | fail: |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 3274 | 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] | 3275 | return -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3276 | } |
| 3277 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3278 | /** |
Alan | 0435182 | 2007-03-06 02:37:52 -0800 | [diff] [blame] | 3279 | * ata_do_set_mode - Program timings and issue SET FEATURES - XFER |
Tejun Heo | 0260731 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 3280 | * @link: link on which timings will be programmed |
Joe Perches | 1967b7f | 2008-02-03 17:08:11 +0200 | [diff] [blame] | 3281 | * @r_failed_dev: out parameter for failed device |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3282 | * |
Alan | 0435182 | 2007-03-06 02:37:52 -0800 | [diff] [blame] | 3283 | * Standard implementation of the function used to tune and set |
| 3284 | * ATA device disk transfer mode (PIO3, UDMA6, etc.). If |
| 3285 | * ata_dev_set_mode() fails, pointer to the failing device is |
Tejun Heo | e82cbdb | 2006-04-01 01:38:18 +0900 | [diff] [blame] | 3286 | * returned in @r_failed_dev. |
Jeff Garzik | 780a87f | 2005-05-30 15:41:05 -0400 | [diff] [blame] | 3287 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3288 | * LOCKING: |
Jeff Garzik | 0cba632 | 2005-05-30 19:49:12 -0400 | [diff] [blame] | 3289 | * PCI/etc. bus probe sem. |
Tejun Heo | e82cbdb | 2006-04-01 01:38:18 +0900 | [diff] [blame] | 3290 | * |
| 3291 | * RETURNS: |
| 3292 | * 0 on success, negative errno otherwise |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3293 | */ |
Alan | 0435182 | 2007-03-06 02:37:52 -0800 | [diff] [blame] | 3294 | |
Tejun Heo | 0260731 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 3295 | 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] | 3296 | { |
Tejun Heo | 0260731 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 3297 | struct ata_port *ap = link->ap; |
Tejun Heo | e8e0619 | 2006-04-01 01:38:18 +0900 | [diff] [blame] | 3298 | struct ata_device *dev; |
Tejun Heo | f58229f | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 3299 | int rc = 0, used_dma = 0, found = 0; |
Tejun Heo | 3adcebb | 2006-05-15 20:57:37 +0900 | [diff] [blame] | 3300 | |
Tejun Heo | a6d5a51 | 2006-03-06 04:31:57 +0900 | [diff] [blame] | 3301 | /* step 1: calculate xfer_mask */ |
Tejun Heo | 1eca436 | 2008-11-03 20:03:17 +0900 | [diff] [blame] | 3302 | ata_for_each_dev(dev, link, ENABLED) { |
Tejun Heo | 7dc951a | 2007-11-27 19:43:42 +0900 | [diff] [blame] | 3303 | unsigned long pio_mask, dma_mask; |
Alan Cox | b3a7060 | 2007-10-02 12:38:26 -0400 | [diff] [blame] | 3304 | unsigned int mode_mask; |
Tejun Heo | a6d5a51 | 2006-03-06 04:31:57 +0900 | [diff] [blame] | 3305 | |
Alan Cox | b3a7060 | 2007-10-02 12:38:26 -0400 | [diff] [blame] | 3306 | mode_mask = ATA_DMA_MASK_ATA; |
| 3307 | if (dev->class == ATA_DEV_ATAPI) |
| 3308 | mode_mask = ATA_DMA_MASK_ATAPI; |
| 3309 | else if (ata_id_is_cfa(dev->id)) |
| 3310 | mode_mask = ATA_DMA_MASK_CFA; |
| 3311 | |
Tejun Heo | 3373efd | 2006-05-15 20:57:53 +0900 | [diff] [blame] | 3312 | ata_dev_xfermask(dev); |
Tejun Heo | 3326732 | 2008-02-13 09:15:09 +0900 | [diff] [blame] | 3313 | ata_force_xfermask(dev); |
Tejun Heo | a6d5a51 | 2006-03-06 04:31:57 +0900 | [diff] [blame] | 3314 | |
Tejun Heo | acf356b | 2006-03-24 14:07:50 +0900 | [diff] [blame] | 3315 | pio_mask = ata_pack_xfermask(dev->pio_mask, 0, 0); |
Alan Cox | b3a7060 | 2007-10-02 12:38:26 -0400 | [diff] [blame] | 3316 | |
| 3317 | if (libata_dma_mask & mode_mask) |
Sergei Shtylyov | 80a9c43 | 2011-12-22 22:41:32 +0300 | [diff] [blame] | 3318 | dma_mask = ata_pack_xfermask(0, dev->mwdma_mask, |
| 3319 | dev->udma_mask); |
Alan Cox | b3a7060 | 2007-10-02 12:38:26 -0400 | [diff] [blame] | 3320 | else |
| 3321 | dma_mask = 0; |
| 3322 | |
Tejun Heo | acf356b | 2006-03-24 14:07:50 +0900 | [diff] [blame] | 3323 | dev->pio_mode = ata_xfer_mask2mode(pio_mask); |
| 3324 | dev->dma_mode = ata_xfer_mask2mode(dma_mask); |
Alan Cox | 5444a6f | 2006-03-27 18:58:20 +0100 | [diff] [blame] | 3325 | |
Tejun Heo | 4f65977 | 2006-04-01 01:38:18 +0900 | [diff] [blame] | 3326 | found = 1; |
Alan Cox | b15b3eb | 2008-08-01 09:18:34 +0100 | [diff] [blame] | 3327 | if (ata_dma_enabled(dev)) |
Alan Cox | 5444a6f | 2006-03-27 18:58:20 +0100 | [diff] [blame] | 3328 | used_dma = 1; |
Tejun Heo | a6d5a51 | 2006-03-06 04:31:57 +0900 | [diff] [blame] | 3329 | } |
Tejun Heo | 4f65977 | 2006-04-01 01:38:18 +0900 | [diff] [blame] | 3330 | if (!found) |
Tejun Heo | e82cbdb | 2006-04-01 01:38:18 +0900 | [diff] [blame] | 3331 | goto out; |
Tejun Heo | a6d5a51 | 2006-03-06 04:31:57 +0900 | [diff] [blame] | 3332 | |
| 3333 | /* step 2: always set host PIO timings */ |
Tejun Heo | 1eca436 | 2008-11-03 20:03:17 +0900 | [diff] [blame] | 3334 | ata_for_each_dev(dev, link, ENABLED) { |
Tejun Heo | 70cd071 | 2007-11-27 19:43:40 +0900 | [diff] [blame] | 3335 | if (dev->pio_mode == 0xff) { |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 3336 | ata_dev_warn(dev, "no PIO support\n"); |
Tejun Heo | e8e0619 | 2006-04-01 01:38:18 +0900 | [diff] [blame] | 3337 | rc = -EINVAL; |
Tejun Heo | e82cbdb | 2006-04-01 01:38:18 +0900 | [diff] [blame] | 3338 | goto out; |
Tejun Heo | e8e0619 | 2006-04-01 01:38:18 +0900 | [diff] [blame] | 3339 | } |
| 3340 | |
| 3341 | dev->xfer_mode = dev->pio_mode; |
| 3342 | dev->xfer_shift = ATA_SHIFT_PIO; |
| 3343 | if (ap->ops->set_piomode) |
| 3344 | ap->ops->set_piomode(ap, dev); |
| 3345 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3346 | |
Tejun Heo | a6d5a51 | 2006-03-06 04:31:57 +0900 | [diff] [blame] | 3347 | /* step 3: set host DMA timings */ |
Tejun Heo | 1eca436 | 2008-11-03 20:03:17 +0900 | [diff] [blame] | 3348 | ata_for_each_dev(dev, link, ENABLED) { |
| 3349 | if (!ata_dma_enabled(dev)) |
Tejun Heo | e8e0619 | 2006-04-01 01:38:18 +0900 | [diff] [blame] | 3350 | continue; |
| 3351 | |
| 3352 | dev->xfer_mode = dev->dma_mode; |
| 3353 | dev->xfer_shift = ata_xfer_mode2shift(dev->dma_mode); |
| 3354 | if (ap->ops->set_dmamode) |
| 3355 | ap->ops->set_dmamode(ap, dev); |
| 3356 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3357 | |
| 3358 | /* step 4: update devices' xfer mode */ |
Tejun Heo | 1eca436 | 2008-11-03 20:03:17 +0900 | [diff] [blame] | 3359 | ata_for_each_dev(dev, link, ENABLED) { |
Tejun Heo | 3373efd | 2006-05-15 20:57:53 +0900 | [diff] [blame] | 3360 | rc = ata_dev_set_mode(dev); |
Tejun Heo | 5bbc53f | 2006-04-01 01:38:17 +0900 | [diff] [blame] | 3361 | if (rc) |
Tejun Heo | e82cbdb | 2006-04-01 01:38:18 +0900 | [diff] [blame] | 3362 | goto out; |
Tejun Heo | 83206a2 | 2006-03-24 15:25:31 +0900 | [diff] [blame] | 3363 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3364 | |
Tejun Heo | e8e0619 | 2006-04-01 01:38:18 +0900 | [diff] [blame] | 3365 | /* Record simplex status. If we selected DMA then the other |
| 3366 | * host channels are not permitted to do so. |
Alan Cox | 5444a6f | 2006-03-27 18:58:20 +0100 | [diff] [blame] | 3367 | */ |
Jeff Garzik | cca3974 | 2006-08-24 03:19:22 -0400 | [diff] [blame] | 3368 | if (used_dma && (ap->host->flags & ATA_HOST_SIMPLEX)) |
Alan | 032af1c | 2007-03-01 17:36:46 +0000 | [diff] [blame] | 3369 | ap->host->simplex_claimed = ap; |
Alan Cox | 5444a6f | 2006-03-27 18:58:20 +0100 | [diff] [blame] | 3370 | |
Tejun Heo | e82cbdb | 2006-04-01 01:38:18 +0900 | [diff] [blame] | 3371 | out: |
| 3372 | if (rc) |
| 3373 | *r_failed_dev = dev; |
| 3374 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3375 | } |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 3376 | EXPORT_SYMBOL_GPL(ata_do_set_mode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3377 | |
| 3378 | /** |
Tejun Heo | aa2731a | 2008-04-07 22:47:19 +0900 | [diff] [blame] | 3379 | * ata_wait_ready - wait for link to become ready |
| 3380 | * @link: link to be waited on |
| 3381 | * @deadline: deadline jiffies for the operation |
| 3382 | * @check_ready: callback to check link readiness |
| 3383 | * |
| 3384 | * Wait for @link to become ready. @check_ready should return |
| 3385 | * positive number if @link is ready, 0 if it isn't, -ENODEV if |
| 3386 | * link doesn't seem to be occupied, other errno for other error |
| 3387 | * conditions. |
| 3388 | * |
| 3389 | * Transient -ENODEV conditions are allowed for |
| 3390 | * ATA_TMOUT_FF_WAIT. |
| 3391 | * |
| 3392 | * LOCKING: |
| 3393 | * EH context. |
| 3394 | * |
| 3395 | * RETURNS: |
Masanari Iida | c9b5560 | 2016-04-13 23:36:27 +0900 | [diff] [blame] | 3396 | * 0 if @link is ready before @deadline; otherwise, -errno. |
Tejun Heo | aa2731a | 2008-04-07 22:47:19 +0900 | [diff] [blame] | 3397 | */ |
| 3398 | int ata_wait_ready(struct ata_link *link, unsigned long deadline, |
| 3399 | int (*check_ready)(struct ata_link *link)) |
| 3400 | { |
| 3401 | unsigned long start = jiffies; |
Tejun Heo | b48d58f | 2010-04-09 19:46:38 +0900 | [diff] [blame] | 3402 | unsigned long nodev_deadline; |
Tejun Heo | aa2731a | 2008-04-07 22:47:19 +0900 | [diff] [blame] | 3403 | int warned = 0; |
| 3404 | |
Tejun Heo | b48d58f | 2010-04-09 19:46:38 +0900 | [diff] [blame] | 3405 | /* choose which 0xff timeout to use, read comment in libata.h */ |
| 3406 | if (link->ap->host->flags & ATA_HOST_PARALLEL_SCAN) |
| 3407 | nodev_deadline = ata_deadline(start, ATA_TMOUT_FF_WAIT_LONG); |
| 3408 | else |
| 3409 | nodev_deadline = ata_deadline(start, ATA_TMOUT_FF_WAIT); |
| 3410 | |
Tejun Heo | b1c7291 | 2008-07-31 17:02:43 +0900 | [diff] [blame] | 3411 | /* Slave readiness can't be tested separately from master. On |
| 3412 | * M/S emulation configuration, this function should be called |
| 3413 | * only on the master and it will handle both master and slave. |
| 3414 | */ |
| 3415 | WARN_ON(link == link->ap->slave_link); |
| 3416 | |
Tejun Heo | aa2731a | 2008-04-07 22:47:19 +0900 | [diff] [blame] | 3417 | if (time_after(nodev_deadline, deadline)) |
| 3418 | nodev_deadline = deadline; |
| 3419 | |
| 3420 | while (1) { |
| 3421 | unsigned long now = jiffies; |
| 3422 | int ready, tmp; |
| 3423 | |
| 3424 | ready = tmp = check_ready(link); |
| 3425 | if (ready > 0) |
| 3426 | return 0; |
| 3427 | |
Tejun Heo | b48d58f | 2010-04-09 19:46:38 +0900 | [diff] [blame] | 3428 | /* |
| 3429 | * -ENODEV could be transient. Ignore -ENODEV if link |
Tejun Heo | aa2731a | 2008-04-07 22:47:19 +0900 | [diff] [blame] | 3430 | * is online. Also, some SATA devices take a long |
Tejun Heo | b48d58f | 2010-04-09 19:46:38 +0900 | [diff] [blame] | 3431 | * time to clear 0xff after reset. Wait for |
| 3432 | * ATA_TMOUT_FF_WAIT[_LONG] on -ENODEV if link isn't |
| 3433 | * offline. |
Tejun Heo | aa2731a | 2008-04-07 22:47:19 +0900 | [diff] [blame] | 3434 | * |
| 3435 | * Note that some PATA controllers (pata_ali) explode |
| 3436 | * if status register is read more than once when |
| 3437 | * there's no device attached. |
| 3438 | */ |
| 3439 | if (ready == -ENODEV) { |
| 3440 | if (ata_link_online(link)) |
| 3441 | ready = 0; |
| 3442 | else if ((link->ap->flags & ATA_FLAG_SATA) && |
| 3443 | !ata_link_offline(link) && |
| 3444 | time_before(now, nodev_deadline)) |
| 3445 | ready = 0; |
| 3446 | } |
| 3447 | |
| 3448 | if (ready) |
| 3449 | return ready; |
| 3450 | if (time_after(now, deadline)) |
| 3451 | return -EBUSY; |
| 3452 | |
| 3453 | if (!warned && time_after(now, start + 5 * HZ) && |
| 3454 | (deadline - now > 3 * HZ)) { |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 3455 | ata_link_warn(link, |
Tejun Heo | aa2731a | 2008-04-07 22:47:19 +0900 | [diff] [blame] | 3456 | "link is slow to respond, please be patient " |
| 3457 | "(ready=%d)\n", tmp); |
| 3458 | warned = 1; |
| 3459 | } |
| 3460 | |
Tejun Heo | 97750ce | 2010-09-06 17:56:29 +0200 | [diff] [blame] | 3461 | ata_msleep(link->ap, 50); |
Tejun Heo | aa2731a | 2008-04-07 22:47:19 +0900 | [diff] [blame] | 3462 | } |
| 3463 | } |
| 3464 | |
| 3465 | /** |
| 3466 | * ata_wait_after_reset - wait for link to become ready after reset |
| 3467 | * @link: link to be waited on |
| 3468 | * @deadline: deadline jiffies for the operation |
| 3469 | * @check_ready: callback to check link readiness |
| 3470 | * |
| 3471 | * Wait for @link to become ready after reset. |
| 3472 | * |
| 3473 | * LOCKING: |
| 3474 | * EH context. |
| 3475 | * |
| 3476 | * RETURNS: |
Masanari Iida | c9b5560 | 2016-04-13 23:36:27 +0900 | [diff] [blame] | 3477 | * 0 if @link is ready before @deadline; otherwise, -errno. |
Tejun Heo | aa2731a | 2008-04-07 22:47:19 +0900 | [diff] [blame] | 3478 | */ |
Harvey Harrison | 2b4221b | 2008-04-24 18:37:34 -0700 | [diff] [blame] | 3479 | int ata_wait_after_reset(struct ata_link *link, unsigned long deadline, |
Tejun Heo | aa2731a | 2008-04-07 22:47:19 +0900 | [diff] [blame] | 3480 | int (*check_ready)(struct ata_link *link)) |
| 3481 | { |
Tejun Heo | 97750ce | 2010-09-06 17:56:29 +0200 | [diff] [blame] | 3482 | ata_msleep(link->ap, ATA_WAIT_AFTER_RESET); |
Tejun Heo | aa2731a | 2008-04-07 22:47:19 +0900 | [diff] [blame] | 3483 | |
| 3484 | return ata_wait_ready(link, deadline, check_ready); |
| 3485 | } |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 3486 | EXPORT_SYMBOL_GPL(ata_wait_after_reset); |
Tejun Heo | aa2731a | 2008-04-07 22:47:19 +0900 | [diff] [blame] | 3487 | |
| 3488 | /** |
Tejun Heo | 0aa1113 | 2008-04-07 22:47:18 +0900 | [diff] [blame] | 3489 | * ata_std_prereset - prepare for reset |
Tejun Heo | cc0680a | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 3490 | * @link: ATA link to be reset |
Tejun Heo | d4b2bab | 2007-02-02 16:50:52 +0900 | [diff] [blame] | 3491 | * @deadline: deadline jiffies for the operation |
Tejun Heo | f5914a4 | 2006-05-31 18:27:48 +0900 | [diff] [blame] | 3492 | * |
Tejun Heo | cc0680a | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 3493 | * @link is about to be reset. Initialize it. Failure from |
Tejun Heo | b8cffc6 | 2007-02-02 16:50:52 +0900 | [diff] [blame] | 3494 | * prereset makes libata abort whole reset sequence and give up |
| 3495 | * that port, so prereset should be best-effort. It does its |
| 3496 | * best to prepare for reset sequence but if things go wrong, it |
| 3497 | * should just whine, not fail. |
Tejun Heo | f5914a4 | 2006-05-31 18:27:48 +0900 | [diff] [blame] | 3498 | * |
| 3499 | * LOCKING: |
| 3500 | * Kernel thread context (may sleep) |
| 3501 | * |
| 3502 | * RETURNS: |
| 3503 | * 0 on success, -errno otherwise. |
| 3504 | */ |
Tejun Heo | 0aa1113 | 2008-04-07 22:47:18 +0900 | [diff] [blame] | 3505 | int ata_std_prereset(struct ata_link *link, unsigned long deadline) |
Tejun Heo | f5914a4 | 2006-05-31 18:27:48 +0900 | [diff] [blame] | 3506 | { |
Tejun Heo | cc0680a | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 3507 | struct ata_port *ap = link->ap; |
Tejun Heo | 936fd73 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 3508 | struct ata_eh_context *ehc = &link->eh_context; |
Tejun Heo | e9c8391 | 2006-07-03 16:07:26 +0900 | [diff] [blame] | 3509 | const unsigned long *timing = sata_ehc_deb_timing(ehc); |
Tejun Heo | f5914a4 | 2006-05-31 18:27:48 +0900 | [diff] [blame] | 3510 | int rc; |
| 3511 | |
Tejun Heo | f5914a4 | 2006-05-31 18:27:48 +0900 | [diff] [blame] | 3512 | /* if we're about to do hardreset, nothing more to do */ |
| 3513 | if (ehc->i.action & ATA_EH_HARDRESET) |
| 3514 | return 0; |
| 3515 | |
Tejun Heo | 936fd73 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 3516 | /* if SATA, resume link */ |
Tejun Heo | a16abc0 | 2007-05-21 18:33:47 +0200 | [diff] [blame] | 3517 | if (ap->flags & ATA_FLAG_SATA) { |
Tejun Heo | 936fd73 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 3518 | rc = sata_link_resume(link, timing, deadline); |
Tejun Heo | b8cffc6 | 2007-02-02 16:50:52 +0900 | [diff] [blame] | 3519 | /* whine about phy resume failure but proceed */ |
| 3520 | if (rc && rc != -EOPNOTSUPP) |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 3521 | ata_link_warn(link, |
| 3522 | "failed to resume link for reset (errno=%d)\n", |
| 3523 | rc); |
Tejun Heo | f5914a4 | 2006-05-31 18:27:48 +0900 | [diff] [blame] | 3524 | } |
| 3525 | |
Tejun Heo | 45db2f6 | 2008-04-08 01:46:56 +0900 | [diff] [blame] | 3526 | /* no point in trying softreset on offline link */ |
Tejun Heo | b1c7291 | 2008-07-31 17:02:43 +0900 | [diff] [blame] | 3527 | if (ata_phys_link_offline(link)) |
Tejun Heo | 45db2f6 | 2008-04-08 01:46:56 +0900 | [diff] [blame] | 3528 | ehc->i.action &= ~ATA_EH_SOFTRESET; |
| 3529 | |
Tejun Heo | f5914a4 | 2006-05-31 18:27:48 +0900 | [diff] [blame] | 3530 | return 0; |
| 3531 | } |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 3532 | EXPORT_SYMBOL_GPL(ata_std_prereset); |
Tejun Heo | f5914a4 | 2006-05-31 18:27:48 +0900 | [diff] [blame] | 3533 | |
Tejun Heo | c2bd580 | 2006-01-24 17:05:22 +0900 | [diff] [blame] | 3534 | /** |
Tejun Heo | 57c9efd | 2008-04-07 22:47:19 +0900 | [diff] [blame] | 3535 | * sata_std_hardreset - COMRESET w/o waiting or classification |
| 3536 | * @link: link to reset |
| 3537 | * @class: resulting class of attached device |
| 3538 | * @deadline: deadline jiffies for the operation |
| 3539 | * |
| 3540 | * Standard SATA COMRESET w/o waiting or classification. |
| 3541 | * |
| 3542 | * LOCKING: |
| 3543 | * Kernel thread context (may sleep) |
| 3544 | * |
| 3545 | * RETURNS: |
| 3546 | * 0 if link offline, -EAGAIN if link online, -errno on errors. |
| 3547 | */ |
| 3548 | int sata_std_hardreset(struct ata_link *link, unsigned int *class, |
| 3549 | unsigned long deadline) |
| 3550 | { |
| 3551 | const unsigned long *timing = sata_ehc_deb_timing(&link->eh_context); |
| 3552 | bool online; |
| 3553 | int rc; |
| 3554 | |
| 3555 | /* do hardreset */ |
| 3556 | rc = sata_link_hardreset(link, timing, deadline, &online, NULL); |
Tejun Heo | 57c9efd | 2008-04-07 22:47:19 +0900 | [diff] [blame] | 3557 | return online ? -EAGAIN : rc; |
| 3558 | } |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 3559 | EXPORT_SYMBOL_GPL(sata_std_hardreset); |
Tejun Heo | 57c9efd | 2008-04-07 22:47:19 +0900 | [diff] [blame] | 3560 | |
| 3561 | /** |
Tejun Heo | 203c75b | 2008-04-07 22:47:18 +0900 | [diff] [blame] | 3562 | * ata_std_postreset - standard postreset callback |
Tejun Heo | cc0680a | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 3563 | * @link: the target ata_link |
Tejun Heo | c2bd580 | 2006-01-24 17:05:22 +0900 | [diff] [blame] | 3564 | * @classes: classes of attached devices |
| 3565 | * |
| 3566 | * This function is invoked after a successful reset. Note that |
| 3567 | * the device might have been reset more than once using |
| 3568 | * different reset methods before postreset is invoked. |
Tejun Heo | c2bd580 | 2006-01-24 17:05:22 +0900 | [diff] [blame] | 3569 | * |
Tejun Heo | c2bd580 | 2006-01-24 17:05:22 +0900 | [diff] [blame] | 3570 | * LOCKING: |
| 3571 | * Kernel thread context (may sleep) |
| 3572 | */ |
Tejun Heo | 203c75b | 2008-04-07 22:47:18 +0900 | [diff] [blame] | 3573 | void ata_std_postreset(struct ata_link *link, unsigned int *classes) |
Tejun Heo | c2bd580 | 2006-01-24 17:05:22 +0900 | [diff] [blame] | 3574 | { |
Tejun Heo | f046519 | 2008-05-19 01:15:08 +0900 | [diff] [blame] | 3575 | u32 serror; |
| 3576 | |
Tejun Heo | c2bd580 | 2006-01-24 17:05:22 +0900 | [diff] [blame] | 3577 | DPRINTK("ENTER\n"); |
| 3578 | |
Tejun Heo | f046519 | 2008-05-19 01:15:08 +0900 | [diff] [blame] | 3579 | /* reset complete, clear SError */ |
| 3580 | if (!sata_scr_read(link, SCR_ERROR, &serror)) |
| 3581 | sata_scr_write(link, SCR_ERROR, serror); |
| 3582 | |
Tejun Heo | c2bd580 | 2006-01-24 17:05:22 +0900 | [diff] [blame] | 3583 | /* print link status */ |
Tejun Heo | 936fd73 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 3584 | sata_print_link_status(link); |
Tejun Heo | c2bd580 | 2006-01-24 17:05:22 +0900 | [diff] [blame] | 3585 | |
Tejun Heo | c2bd580 | 2006-01-24 17:05:22 +0900 | [diff] [blame] | 3586 | DPRINTK("EXIT\n"); |
| 3587 | } |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 3588 | EXPORT_SYMBOL_GPL(ata_std_postreset); |
Tejun Heo | c2bd580 | 2006-01-24 17:05:22 +0900 | [diff] [blame] | 3589 | |
Tejun Heo | a62c0fc | 2006-01-24 17:05:22 +0900 | [diff] [blame] | 3590 | /** |
Tejun Heo | 623a312 | 2006-03-05 17:55:58 +0900 | [diff] [blame] | 3591 | * ata_dev_same_device - Determine whether new ID matches configured device |
Tejun Heo | 623a312 | 2006-03-05 17:55:58 +0900 | [diff] [blame] | 3592 | * @dev: device to compare against |
| 3593 | * @new_class: class of the new device |
| 3594 | * @new_id: IDENTIFY page of the new device |
| 3595 | * |
| 3596 | * Compare @new_class and @new_id against @dev and determine |
| 3597 | * whether @dev is the device indicated by @new_class and |
| 3598 | * @new_id. |
| 3599 | * |
| 3600 | * LOCKING: |
| 3601 | * None. |
| 3602 | * |
| 3603 | * RETURNS: |
| 3604 | * 1 if @dev matches @new_class and @new_id, 0 otherwise. |
| 3605 | */ |
Tejun Heo | 3373efd | 2006-05-15 20:57:53 +0900 | [diff] [blame] | 3606 | static int ata_dev_same_device(struct ata_device *dev, unsigned int new_class, |
| 3607 | const u16 *new_id) |
Tejun Heo | 623a312 | 2006-03-05 17:55:58 +0900 | [diff] [blame] | 3608 | { |
| 3609 | const u16 *old_id = dev->id; |
Tejun Heo | a0cf733 | 2007-01-02 20:18:49 +0900 | [diff] [blame] | 3610 | unsigned char model[2][ATA_ID_PROD_LEN + 1]; |
| 3611 | unsigned char serial[2][ATA_ID_SERNO_LEN + 1]; |
Tejun Heo | 623a312 | 2006-03-05 17:55:58 +0900 | [diff] [blame] | 3612 | |
| 3613 | if (dev->class != new_class) { |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 3614 | ata_dev_info(dev, "class mismatch %d != %d\n", |
| 3615 | dev->class, new_class); |
Tejun Heo | 623a312 | 2006-03-05 17:55:58 +0900 | [diff] [blame] | 3616 | return 0; |
| 3617 | } |
| 3618 | |
Tejun Heo | a0cf733 | 2007-01-02 20:18:49 +0900 | [diff] [blame] | 3619 | ata_id_c_string(old_id, model[0], ATA_ID_PROD, sizeof(model[0])); |
| 3620 | ata_id_c_string(new_id, model[1], ATA_ID_PROD, sizeof(model[1])); |
| 3621 | ata_id_c_string(old_id, serial[0], ATA_ID_SERNO, sizeof(serial[0])); |
| 3622 | 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] | 3623 | |
| 3624 | if (strcmp(model[0], model[1])) { |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 3625 | ata_dev_info(dev, "model number mismatch '%s' != '%s'\n", |
| 3626 | model[0], model[1]); |
Tejun Heo | 623a312 | 2006-03-05 17:55:58 +0900 | [diff] [blame] | 3627 | return 0; |
| 3628 | } |
| 3629 | |
| 3630 | if (strcmp(serial[0], serial[1])) { |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 3631 | ata_dev_info(dev, "serial number mismatch '%s' != '%s'\n", |
| 3632 | serial[0], serial[1]); |
Tejun Heo | 623a312 | 2006-03-05 17:55:58 +0900 | [diff] [blame] | 3633 | return 0; |
| 3634 | } |
| 3635 | |
Tejun Heo | 623a312 | 2006-03-05 17:55:58 +0900 | [diff] [blame] | 3636 | return 1; |
| 3637 | } |
| 3638 | |
| 3639 | /** |
Tejun Heo | fe30911 | 2007-05-15 03:28:15 +0900 | [diff] [blame] | 3640 | * ata_dev_reread_id - Re-read IDENTIFY data |
Henrik Kretzschmar | 3fae450 | 2007-06-19 10:10:50 +0200 | [diff] [blame] | 3641 | * @dev: target ATA device |
Tejun Heo | bff0464 | 2006-11-10 18:08:10 +0900 | [diff] [blame] | 3642 | * @readid_flags: read ID flags |
Tejun Heo | 623a312 | 2006-03-05 17:55:58 +0900 | [diff] [blame] | 3643 | * |
| 3644 | * Re-read IDENTIFY page and make sure @dev is still attached to |
| 3645 | * the port. |
| 3646 | * |
| 3647 | * LOCKING: |
| 3648 | * Kernel thread context (may sleep) |
| 3649 | * |
| 3650 | * RETURNS: |
| 3651 | * 0 on success, negative errno otherwise |
| 3652 | */ |
Tejun Heo | fe30911 | 2007-05-15 03:28:15 +0900 | [diff] [blame] | 3653 | 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] | 3654 | { |
Tejun Heo | 5eb45c0 | 2006-04-02 18:51:52 +0900 | [diff] [blame] | 3655 | unsigned int class = dev->class; |
Tejun Heo | 9af5c9c | 2007-08-06 18:36:22 +0900 | [diff] [blame] | 3656 | u16 *id = (void *)dev->link->ap->sector_buf; |
Tejun Heo | 623a312 | 2006-03-05 17:55:58 +0900 | [diff] [blame] | 3657 | int rc; |
| 3658 | |
Tejun Heo | fe635c7 | 2006-05-15 20:57:35 +0900 | [diff] [blame] | 3659 | /* read ID data */ |
Tejun Heo | bff0464 | 2006-11-10 18:08:10 +0900 | [diff] [blame] | 3660 | rc = ata_dev_read_id(dev, &class, readid_flags, id); |
Tejun Heo | 623a312 | 2006-03-05 17:55:58 +0900 | [diff] [blame] | 3661 | if (rc) |
Tejun Heo | fe30911 | 2007-05-15 03:28:15 +0900 | [diff] [blame] | 3662 | return rc; |
Tejun Heo | 623a312 | 2006-03-05 17:55:58 +0900 | [diff] [blame] | 3663 | |
| 3664 | /* is the device still there? */ |
Tejun Heo | fe30911 | 2007-05-15 03:28:15 +0900 | [diff] [blame] | 3665 | if (!ata_dev_same_device(dev, class, id)) |
| 3666 | return -ENODEV; |
Tejun Heo | 623a312 | 2006-03-05 17:55:58 +0900 | [diff] [blame] | 3667 | |
Tejun Heo | fe635c7 | 2006-05-15 20:57:35 +0900 | [diff] [blame] | 3668 | memcpy(dev->id, id, sizeof(id[0]) * ATA_ID_WORDS); |
Tejun Heo | fe30911 | 2007-05-15 03:28:15 +0900 | [diff] [blame] | 3669 | return 0; |
| 3670 | } |
| 3671 | |
| 3672 | /** |
| 3673 | * ata_dev_revalidate - Revalidate ATA device |
| 3674 | * @dev: device to revalidate |
Tejun Heo | 422c9da | 2007-09-23 13:14:12 +0900 | [diff] [blame] | 3675 | * @new_class: new class code |
Tejun Heo | fe30911 | 2007-05-15 03:28:15 +0900 | [diff] [blame] | 3676 | * @readid_flags: read ID flags |
| 3677 | * |
| 3678 | * Re-read IDENTIFY page, make sure @dev is still attached to the |
| 3679 | * port and reconfigure it according to the new IDENTIFY page. |
| 3680 | * |
| 3681 | * LOCKING: |
| 3682 | * Kernel thread context (may sleep) |
| 3683 | * |
| 3684 | * RETURNS: |
| 3685 | * 0 on success, negative errno otherwise |
| 3686 | */ |
Tejun Heo | 422c9da | 2007-09-23 13:14:12 +0900 | [diff] [blame] | 3687 | int ata_dev_revalidate(struct ata_device *dev, unsigned int new_class, |
| 3688 | unsigned int readid_flags) |
Tejun Heo | fe30911 | 2007-05-15 03:28:15 +0900 | [diff] [blame] | 3689 | { |
Tejun Heo | 6ddcd3b | 2007-05-15 03:28:15 +0900 | [diff] [blame] | 3690 | u64 n_sectors = dev->n_sectors; |
Tejun Heo | 5920dad | 2009-07-15 17:11:41 +0900 | [diff] [blame] | 3691 | u64 n_native_sectors = dev->n_native_sectors; |
Tejun Heo | fe30911 | 2007-05-15 03:28:15 +0900 | [diff] [blame] | 3692 | int rc; |
| 3693 | |
| 3694 | if (!ata_dev_enabled(dev)) |
| 3695 | return -ENODEV; |
| 3696 | |
Tejun Heo | 422c9da | 2007-09-23 13:14:12 +0900 | [diff] [blame] | 3697 | /* fail early if !ATA && !ATAPI to avoid issuing [P]IDENTIFY to PMP */ |
| 3698 | if (ata_class_enabled(new_class) && |
Borislav Petkov | f0d0613 | 2009-04-26 16:33:34 +0200 | [diff] [blame] | 3699 | new_class != ATA_DEV_ATA && |
| 3700 | new_class != ATA_DEV_ATAPI && |
Hannes Reinecke | 9162c65 | 2014-11-05 13:08:21 +0100 | [diff] [blame] | 3701 | new_class != ATA_DEV_ZAC && |
Borislav Petkov | f0d0613 | 2009-04-26 16:33:34 +0200 | [diff] [blame] | 3702 | new_class != ATA_DEV_SEMB) { |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 3703 | ata_dev_info(dev, "class mismatch %u != %u\n", |
| 3704 | dev->class, new_class); |
Tejun Heo | 422c9da | 2007-09-23 13:14:12 +0900 | [diff] [blame] | 3705 | rc = -ENODEV; |
| 3706 | goto fail; |
| 3707 | } |
| 3708 | |
Tejun Heo | fe30911 | 2007-05-15 03:28:15 +0900 | [diff] [blame] | 3709 | /* re-read ID */ |
| 3710 | rc = ata_dev_reread_id(dev, readid_flags); |
| 3711 | if (rc) |
| 3712 | goto fail; |
Tejun Heo | 623a312 | 2006-03-05 17:55:58 +0900 | [diff] [blame] | 3713 | |
| 3714 | /* configure device according to the new ID */ |
Tejun Heo | efdaedc | 2006-11-01 18:38:52 +0900 | [diff] [blame] | 3715 | rc = ata_dev_configure(dev); |
Tejun Heo | 6ddcd3b | 2007-05-15 03:28:15 +0900 | [diff] [blame] | 3716 | if (rc) |
| 3717 | goto fail; |
| 3718 | |
| 3719 | /* verify n_sectors hasn't changed */ |
Tejun Heo | 445d211 | 2010-04-05 10:33:13 +0900 | [diff] [blame] | 3720 | if (dev->class != ATA_DEV_ATA || !n_sectors || |
| 3721 | dev->n_sectors == n_sectors) |
| 3722 | return 0; |
| 3723 | |
| 3724 | /* n_sectors has changed */ |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 3725 | ata_dev_warn(dev, "n_sectors mismatch %llu != %llu\n", |
| 3726 | (unsigned long long)n_sectors, |
| 3727 | (unsigned long long)dev->n_sectors); |
Tejun Heo | 445d211 | 2010-04-05 10:33:13 +0900 | [diff] [blame] | 3728 | |
| 3729 | /* |
| 3730 | * Something could have caused HPA to be unlocked |
| 3731 | * involuntarily. If n_native_sectors hasn't changed and the |
| 3732 | * new size matches it, keep the device. |
| 3733 | */ |
| 3734 | if (dev->n_native_sectors == n_native_sectors && |
| 3735 | dev->n_sectors > n_sectors && dev->n_sectors == n_native_sectors) { |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 3736 | ata_dev_warn(dev, |
| 3737 | "new n_sectors matches native, probably " |
| 3738 | "late HPA unlock, n_sectors updated\n"); |
Tejun Heo | 68939ce | 2010-05-15 20:09:33 +0200 | [diff] [blame] | 3739 | /* use the larger n_sectors */ |
Tejun Heo | 445d211 | 2010-04-05 10:33:13 +0900 | [diff] [blame] | 3740 | return 0; |
Tejun Heo | 6ddcd3b | 2007-05-15 03:28:15 +0900 | [diff] [blame] | 3741 | } |
| 3742 | |
Tejun Heo | 445d211 | 2010-04-05 10:33:13 +0900 | [diff] [blame] | 3743 | /* |
| 3744 | * Some BIOSes boot w/o HPA but resume w/ HPA locked. Try |
| 3745 | * unlocking HPA in those cases. |
| 3746 | * |
| 3747 | * https://bugzilla.kernel.org/show_bug.cgi?id=15396 |
| 3748 | */ |
| 3749 | if (dev->n_native_sectors == n_native_sectors && |
| 3750 | dev->n_sectors < n_sectors && n_sectors == n_native_sectors && |
| 3751 | !(dev->horkage & ATA_HORKAGE_BROKEN_HPA)) { |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 3752 | ata_dev_warn(dev, |
| 3753 | "old n_sectors matches native, probably " |
| 3754 | "late HPA lock, will try to unlock HPA\n"); |
Tejun Heo | 445d211 | 2010-04-05 10:33:13 +0900 | [diff] [blame] | 3755 | /* try unlocking HPA */ |
| 3756 | dev->flags |= ATA_DFLAG_UNLOCK_HPA; |
| 3757 | rc = -EIO; |
| 3758 | } else |
| 3759 | rc = -ENODEV; |
Tejun Heo | 623a312 | 2006-03-05 17:55:58 +0900 | [diff] [blame] | 3760 | |
Tejun Heo | 445d211 | 2010-04-05 10:33:13 +0900 | [diff] [blame] | 3761 | /* restore original n_[native_]sectors and fail */ |
| 3762 | dev->n_native_sectors = n_native_sectors; |
| 3763 | dev->n_sectors = n_sectors; |
Tejun Heo | 623a312 | 2006-03-05 17:55:58 +0900 | [diff] [blame] | 3764 | fail: |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 3765 | ata_dev_err(dev, "revalidation failed (errno=%d)\n", rc); |
Tejun Heo | 623a312 | 2006-03-05 17:55:58 +0900 | [diff] [blame] | 3766 | return rc; |
| 3767 | } |
| 3768 | |
Alan Cox | 6919a0a | 2006-10-27 19:08:46 -0700 | [diff] [blame] | 3769 | struct ata_blacklist_entry { |
| 3770 | const char *model_num; |
| 3771 | const char *model_rev; |
| 3772 | unsigned long horkage; |
| 3773 | }; |
| 3774 | |
| 3775 | static const struct ata_blacklist_entry ata_device_blacklist [] = { |
| 3776 | /* Devices with DMA related problems under Linux */ |
| 3777 | { "WDC AC11000H", NULL, ATA_HORKAGE_NODMA }, |
| 3778 | { "WDC AC22100H", NULL, ATA_HORKAGE_NODMA }, |
| 3779 | { "WDC AC32500H", NULL, ATA_HORKAGE_NODMA }, |
| 3780 | { "WDC AC33100H", NULL, ATA_HORKAGE_NODMA }, |
| 3781 | { "WDC AC31600H", NULL, ATA_HORKAGE_NODMA }, |
| 3782 | { "WDC AC32100H", "24.09P07", ATA_HORKAGE_NODMA }, |
| 3783 | { "WDC AC23200L", "21.10N21", ATA_HORKAGE_NODMA }, |
| 3784 | { "Compaq CRD-8241B", NULL, ATA_HORKAGE_NODMA }, |
| 3785 | { "CRD-8400B", NULL, ATA_HORKAGE_NODMA }, |
Mark Lord | 7da4c93 | 2010-07-01 18:18:12 -0400 | [diff] [blame] | 3786 | { "CRD-848[02]B", NULL, ATA_HORKAGE_NODMA }, |
Alan Cox | 6919a0a | 2006-10-27 19:08:46 -0700 | [diff] [blame] | 3787 | { "CRD-84", NULL, ATA_HORKAGE_NODMA }, |
| 3788 | { "SanDisk SDP3B", NULL, ATA_HORKAGE_NODMA }, |
| 3789 | { "SanDisk SDP3B-64", NULL, ATA_HORKAGE_NODMA }, |
| 3790 | { "SANYO CD-ROM CRD", NULL, ATA_HORKAGE_NODMA }, |
| 3791 | { "HITACHI CDR-8", NULL, ATA_HORKAGE_NODMA }, |
Mark Lord | 7da4c93 | 2010-07-01 18:18:12 -0400 | [diff] [blame] | 3792 | { "HITACHI CDR-8[34]35",NULL, ATA_HORKAGE_NODMA }, |
Alan Cox | 6919a0a | 2006-10-27 19:08:46 -0700 | [diff] [blame] | 3793 | { "Toshiba CD-ROM XM-6202B", NULL, ATA_HORKAGE_NODMA }, |
| 3794 | { "TOSHIBA CD-ROM XM-1702BC", NULL, ATA_HORKAGE_NODMA }, |
| 3795 | { "CD-532E-A", NULL, ATA_HORKAGE_NODMA }, |
| 3796 | { "E-IDE CD-ROM CR-840",NULL, ATA_HORKAGE_NODMA }, |
| 3797 | { "CD-ROM Drive/F5A", NULL, ATA_HORKAGE_NODMA }, |
| 3798 | { "WPI CDD-820", NULL, ATA_HORKAGE_NODMA }, |
| 3799 | { "SAMSUNG CD-ROM SC-148C", NULL, ATA_HORKAGE_NODMA }, |
| 3800 | { "SAMSUNG CD-ROM SC", NULL, ATA_HORKAGE_NODMA }, |
Alan Cox | 6919a0a | 2006-10-27 19:08:46 -0700 | [diff] [blame] | 3801 | { "ATAPI CD-ROM DRIVE 40X MAXIMUM",NULL,ATA_HORKAGE_NODMA }, |
| 3802 | { "_NEC DV5800A", NULL, ATA_HORKAGE_NODMA }, |
Jeff Garzik | 2dcb407 | 2007-10-19 06:42:56 -0400 | [diff] [blame] | 3803 | { "SAMSUNG CD-ROM SN-124", "N001", ATA_HORKAGE_NODMA }, |
Dave Jones | 39f1988 | 2007-05-21 14:31:03 -0400 | [diff] [blame] | 3804 | { "Seagate STT20000A", NULL, ATA_HORKAGE_NODMA }, |
Prarit Bhargava | d17d794 | 2012-08-23 15:11:52 -0400 | [diff] [blame] | 3805 | { " 2GB ATA Flash Disk", "ADMA428M", ATA_HORKAGE_NODMA }, |
Shikha Jain | b00622f | 2016-01-11 14:55:45 -0800 | [diff] [blame] | 3806 | { "VRFDFC22048UCHC-TE*", NULL, ATA_HORKAGE_NODMA }, |
Tejun Heo | 3af9a77 | 2007-09-23 13:19:54 +0900 | [diff] [blame] | 3807 | /* Odd clown on sil3726/4726 PMPs */ |
Tejun Heo | 50af2fa | 2008-05-19 01:15:14 +0900 | [diff] [blame] | 3808 | { "Config Disk", NULL, ATA_HORKAGE_DISABLE }, |
Alan Cox | 6919a0a | 2006-10-27 19:08:46 -0700 | [diff] [blame] | 3809 | |
Albert Lee | 18d6e9d | 2007-04-02 11:34:15 +0800 | [diff] [blame] | 3810 | /* Weird ATAPI devices */ |
Tejun Heo | 40a1d53 | 2007-06-27 02:49:38 +0900 | [diff] [blame] | 3811 | { "TORiSAN DVD-ROM DRD-N216", NULL, ATA_HORKAGE_MAX_SEC_128 }, |
Tejun Heo | 6a87e42 | 2008-11-03 19:01:09 +0900 | [diff] [blame] | 3812 | { "QUANTUM DAT DAT72-000", NULL, ATA_HORKAGE_ATAPI_MOD16_DMA }, |
Shan Hai | a32450e | 2013-03-18 10:30:44 +0800 | [diff] [blame] | 3813 | { "Slimtype DVD A DS8A8SH", NULL, ATA_HORKAGE_MAX_SEC_LBA48 }, |
Shan Hai | 0523f03 | 2013-10-28 16:08:01 +0800 | [diff] [blame] | 3814 | { "Slimtype DVD A DS8A9SH", NULL, ATA_HORKAGE_MAX_SEC_LBA48 }, |
Albert Lee | 18d6e9d | 2007-04-02 11:34:15 +0800 | [diff] [blame] | 3815 | |
David Milburn | af34d63 | 2015-07-13 11:48:23 -0500 | [diff] [blame] | 3816 | /* |
| 3817 | * Causes silent data corruption with higher max sects. |
| 3818 | * http://lkml.kernel.org/g/x49wpy40ysk.fsf@segfault.boston.devel.redhat.com |
| 3819 | */ |
| 3820 | { "ST380013AS", "3.20", ATA_HORKAGE_MAX_SEC_1024 }, |
| 3821 | |
Tejun Heo | 1488a1e | 2016-07-18 18:40:00 -0400 | [diff] [blame] | 3822 | /* |
Tejun Heo | e0edc8c | 2017-01-06 11:48:50 -0500 | [diff] [blame] | 3823 | * These devices time out with higher max sects. |
Tejun Heo | 1488a1e | 2016-07-18 18:40:00 -0400 | [diff] [blame] | 3824 | * https://bugzilla.kernel.org/show_bug.cgi?id=121671 |
| 3825 | */ |
Tejun Heo | e0edc8c | 2017-01-06 11:48:50 -0500 | [diff] [blame] | 3826 | { "LITEON CX1-JB*-HP", NULL, ATA_HORKAGE_MAX_SEC_1024 }, |
Xinyu Lin | db5ff90 | 2017-12-17 20:13:39 +0800 | [diff] [blame] | 3827 | { "LITEON EP1-*", NULL, ATA_HORKAGE_MAX_SEC_1024 }, |
Tejun Heo | 1488a1e | 2016-07-18 18:40:00 -0400 | [diff] [blame] | 3828 | |
Alan Cox | 6919a0a | 2006-10-27 19:08:46 -0700 | [diff] [blame] | 3829 | /* Devices we expect to fail diagnostics */ |
| 3830 | |
| 3831 | /* Devices where NCQ should be avoided */ |
| 3832 | /* NCQ is slow */ |
Jeff Garzik | 2dcb407 | 2007-10-19 06:42:56 -0400 | [diff] [blame] | 3833 | { "WDC WD740ADFD-00", NULL, ATA_HORKAGE_NONCQ }, |
Tejun Heo | 459ad68 | 2007-12-07 12:46:23 +0900 | [diff] [blame] | 3834 | { "WDC WD740ADFD-00NLR1", NULL, ATA_HORKAGE_NONCQ, }, |
Tejun Heo | 09125ea | 2007-02-28 15:21:23 +0900 | [diff] [blame] | 3835 | /* http://thread.gmane.org/gmane.linux.ide/14907 */ |
| 3836 | { "FUJITSU MHT2060BH", NULL, ATA_HORKAGE_NONCQ }, |
Paul Rolland | 7acfaf3 | 2007-03-26 21:43:44 -0800 | [diff] [blame] | 3837 | /* NCQ is broken */ |
Jeff Garzik | 539cc7c | 2007-09-20 16:31:47 -0400 | [diff] [blame] | 3838 | { "Maxtor *", "BANC*", ATA_HORKAGE_NONCQ }, |
Alan Cox | 0e3dbc0 | 2007-09-20 15:22:47 +0100 | [diff] [blame] | 3839 | { "Maxtor 7V300F0", "VA111630", ATA_HORKAGE_NONCQ }, |
Paolo Ornati | da6f0ec | 2007-10-04 11:06:56 +0900 | [diff] [blame] | 3840 | { "ST380817AS", "3.42", ATA_HORKAGE_NONCQ }, |
Tejun Heo | e41bd3e | 2007-12-09 19:45:39 +0900 | [diff] [blame] | 3841 | { "ST3160023AS", "3.42", ATA_HORKAGE_NONCQ }, |
Lubomir Bulej | 5ccfca9 | 2008-12-22 11:35:22 +0100 | [diff] [blame] | 3842 | { "OCZ CORE_SSD", "02.10104", ATA_HORKAGE_NONCQ }, |
Jeff Garzik | 539cc7c | 2007-09-20 16:31:47 -0400 | [diff] [blame] | 3843 | |
Tejun Heo | ac70a96 | 2008-11-27 13:36:48 +0900 | [diff] [blame] | 3844 | /* Seagate NCQ + FLUSH CACHE firmware bug */ |
Mark Lord | 4d1f908 | 2010-07-05 18:53:37 -0400 | [diff] [blame] | 3845 | { "ST31500341AS", "SD1[5-9]", ATA_HORKAGE_NONCQ | |
Tejun Heo | ac70a96 | 2008-11-27 13:36:48 +0900 | [diff] [blame] | 3846 | ATA_HORKAGE_FIRMWARE_WARN }, |
Tejun Heo | d10d491 | 2008-12-11 13:42:42 +0900 | [diff] [blame] | 3847 | |
Mark Lord | 4d1f908 | 2010-07-05 18:53:37 -0400 | [diff] [blame] | 3848 | { "ST31000333AS", "SD1[5-9]", ATA_HORKAGE_NONCQ | |
Tejun Heo | d10d491 | 2008-12-11 13:42:42 +0900 | [diff] [blame] | 3849 | ATA_HORKAGE_FIRMWARE_WARN }, |
| 3850 | |
Mark Lord | 4d1f908 | 2010-07-05 18:53:37 -0400 | [diff] [blame] | 3851 | { "ST3640[36]23AS", "SD1[5-9]", ATA_HORKAGE_NONCQ | |
Tejun Heo | d10d491 | 2008-12-11 13:42:42 +0900 | [diff] [blame] | 3852 | ATA_HORKAGE_FIRMWARE_WARN }, |
| 3853 | |
Mark Lord | 4d1f908 | 2010-07-05 18:53:37 -0400 | [diff] [blame] | 3854 | { "ST3320[68]13AS", "SD1[5-9]", ATA_HORKAGE_NONCQ | |
Tejun Heo | ac70a96 | 2008-11-27 13:36:48 +0900 | [diff] [blame] | 3855 | ATA_HORKAGE_FIRMWARE_WARN }, |
| 3856 | |
Hans de Goede | 31f6264 | 2019-06-11 16:32:59 +0200 | [diff] [blame] | 3857 | /* drives which fail FPDMA_AA activation (some may freeze afterwards) |
| 3858 | the ST disks also have LPM issues */ |
Hans de Goede | 8756a25 | 2019-07-01 18:58:13 +0200 | [diff] [blame] | 3859 | { "ST1000LM024 HN-M101MBB", NULL, ATA_HORKAGE_BROKEN_FPDMA_AA | |
Hans de Goede | 31f6264 | 2019-06-11 16:32:59 +0200 | [diff] [blame] | 3860 | ATA_HORKAGE_NOLPM, }, |
Aleksei Mamlin | 08c85d2 | 2015-07-01 13:48:30 +0300 | [diff] [blame] | 3861 | { "VB0250EAVER", "HPG7", ATA_HORKAGE_BROKEN_FPDMA_AA }, |
Michele Baldessari | 8780994 | 2013-11-25 19:00:14 +0000 | [diff] [blame] | 3862 | |
Robert Hancock | 36e337d | 2007-04-02 22:05:29 -0600 | [diff] [blame] | 3863 | /* Blacklist entries taken from Silicon Image 3124/3132 |
| 3864 | Windows driver .inf file - also several Linux problem reports */ |
| 3865 | { "HTS541060G9SA00", "MB3OC60D", ATA_HORKAGE_NONCQ, }, |
| 3866 | { "HTS541080G9SA00", "MB4OC60D", ATA_HORKAGE_NONCQ, }, |
| 3867 | { "HTS541010G9SA00", "MBZOC60D", ATA_HORKAGE_NONCQ, }, |
Alan Cox | 6919a0a | 2006-10-27 19:08:46 -0700 | [diff] [blame] | 3868 | |
Tejun Heo | 68b0ddb | 2010-04-05 10:51:26 +0900 | [diff] [blame] | 3869 | /* https://bugzilla.kernel.org/show_bug.cgi?id=15573 */ |
| 3870 | { "C300-CTFDDAC128MAG", "0001", ATA_HORKAGE_NONCQ, }, |
| 3871 | |
Tejun Heo | 322579d | 2018-05-08 14:21:56 -0700 | [diff] [blame] | 3872 | /* Some Sandisk SSDs lock up hard with NCQ enabled. Reported on |
| 3873 | SD7SN6S256G and SD8SN8U256G */ |
| 3874 | { "SanDisk SD[78]SN*G", NULL, ATA_HORKAGE_NONCQ, }, |
| 3875 | |
Tejun Heo | 16c55b0 | 2007-08-29 11:58:33 +0900 | [diff] [blame] | 3876 | /* devices which puke on READ_NATIVE_MAX */ |
| 3877 | { "HDS724040KLSA80", "KFAOA20N", ATA_HORKAGE_BROKEN_HPA, }, |
| 3878 | { "WDC WD3200JD-00KLB0", "WD-WCAMR1130137", ATA_HORKAGE_BROKEN_HPA }, |
| 3879 | { "WDC WD2500JD-00HBB0", "WD-WMAL71490727", ATA_HORKAGE_BROKEN_HPA }, |
| 3880 | { "MAXTOR 6L080L4", "A93.0500", ATA_HORKAGE_BROKEN_HPA }, |
Alan Cox | 6919a0a | 2006-10-27 19:08:46 -0700 | [diff] [blame] | 3881 | |
Tejun Heo | 7831387b | 2009-08-07 01:59:15 +0900 | [diff] [blame] | 3882 | /* this one allows HPA unlocking but fails IOs on the area */ |
| 3883 | { "OCZ-VERTEX", "1.30", ATA_HORKAGE_BROKEN_HPA }, |
| 3884 | |
Alan Cox | 93328e11 | 2007-09-29 04:06:48 -0400 | [diff] [blame] | 3885 | /* Devices which report 1 sector over size HPA */ |
| 3886 | { "ST340823A", NULL, ATA_HORKAGE_HPA_SIZE, }, |
| 3887 | { "ST320413A", NULL, ATA_HORKAGE_HPA_SIZE, }, |
Mikko Rapeli | b152fcd | 2008-02-19 01:41:25 +0100 | [diff] [blame] | 3888 | { "ST310211A", NULL, ATA_HORKAGE_HPA_SIZE, }, |
Alan Cox | 93328e11 | 2007-09-29 04:06:48 -0400 | [diff] [blame] | 3889 | |
Alan Cox | 6bbfd53 | 2007-11-05 22:58:58 +0000 | [diff] [blame] | 3890 | /* Devices which get the IVB wrong */ |
| 3891 | { "QUANTUM FIREBALLlct10 05", "A03.0900", ATA_HORKAGE_IVB, }, |
Alan Cox | a79067e | 2008-04-29 14:08:36 +0100 | [diff] [blame] | 3892 | /* Maybe we should just blacklist TSSTcorp... */ |
Mark Lord | 7da4c93 | 2010-07-01 18:18:12 -0400 | [diff] [blame] | 3893 | { "TSSTcorp CDDVDW SH-S202[HJN]", "SB0[01]", ATA_HORKAGE_IVB, }, |
Alan Cox | 6bbfd53 | 2007-11-05 22:58:58 +0000 | [diff] [blame] | 3894 | |
Jens Axboe | 9ce8e30 | 2008-08-27 15:23:18 +0200 | [diff] [blame] | 3895 | /* Devices that do not need bridging limits applied */ |
| 3896 | { "MTRON MSP-SATA*", NULL, ATA_HORKAGE_BRIDGE_OK, }, |
Jeff Garzik | 04d0f1b8 | 2012-08-17 13:36:59 -0400 | [diff] [blame] | 3897 | { "BUFFALO HD-QSU2/R5", NULL, ATA_HORKAGE_BRIDGE_OK, }, |
Jens Axboe | 9ce8e30 | 2008-08-27 15:23:18 +0200 | [diff] [blame] | 3898 | |
Tejun Heo | 9062712 | 2009-01-29 20:31:36 +0900 | [diff] [blame] | 3899 | /* Devices which aren't very happy with higher link speeds */ |
| 3900 | { "WD My Book", NULL, ATA_HORKAGE_1_5_GBPS, }, |
Daniel J Blueman | c531077 | 2012-07-23 12:22:37 +0800 | [diff] [blame] | 3901 | { "Seagate FreeAgent GoFlex", NULL, ATA_HORKAGE_1_5_GBPS, }, |
Tejun Heo | 9062712 | 2009-01-29 20:31:36 +0900 | [diff] [blame] | 3902 | |
Tejun Heo | d0cb43b | 2009-07-09 09:27:50 +0900 | [diff] [blame] | 3903 | /* |
| 3904 | * Devices which choke on SETXFER. Applies only if both the |
| 3905 | * device and controller are SATA. |
| 3906 | */ |
Tejun Heo | cd69187 | 2011-06-22 12:13:10 +0200 | [diff] [blame] | 3907 | { "PIONEER DVD-RW DVRTD08", NULL, ATA_HORKAGE_NOSETXFER }, |
Vladimir LAVALLADE | 3a25179 | 2012-01-08 13:50:13 +0100 | [diff] [blame] | 3908 | { "PIONEER DVD-RW DVRTD08A", NULL, ATA_HORKAGE_NOSETXFER }, |
| 3909 | { "PIONEER DVD-RW DVR-215", NULL, ATA_HORKAGE_NOSETXFER }, |
Tejun Heo | cd69187 | 2011-06-22 12:13:10 +0200 | [diff] [blame] | 3910 | { "PIONEER DVD-RW DVR-212D", NULL, ATA_HORKAGE_NOSETXFER }, |
| 3911 | { "PIONEER DVD-RW DVR-216D", NULL, ATA_HORKAGE_NOSETXFER }, |
Tejun Heo | d0cb43b | 2009-07-09 09:27:50 +0900 | [diff] [blame] | 3912 | |
Kai-Heng Feng | b17e5729 | 2018-02-18 22:17:09 +0800 | [diff] [blame] | 3913 | /* Crucial BX100 SSD 500GB has broken LPM support */ |
Hans de Goede | 3bf7b5d | 2018-03-19 16:33:59 +0100 | [diff] [blame] | 3914 | { "CT500BX100SSD1", NULL, ATA_HORKAGE_NOLPM }, |
Kai-Heng Feng | b17e5729 | 2018-02-18 22:17:09 +0800 | [diff] [blame] | 3915 | |
Hans de Goede | d418ff5 | 2018-03-19 16:34:00 +0100 | [diff] [blame] | 3916 | /* 512GB MX100 with MU01 firmware has both queued TRIM and LPM issues */ |
| 3917 | { "Crucial_CT512MX100*", "MU01", ATA_HORKAGE_NO_NCQ_TRIM | |
Hans de Goede | 9c7be59f | 2018-02-16 10:48:20 +0100 | [diff] [blame] | 3918 | ATA_HORKAGE_ZERO_AFTER_TRIM | |
| 3919 | ATA_HORKAGE_NOLPM, }, |
Hans de Goede | d418ff5 | 2018-03-19 16:34:00 +0100 | [diff] [blame] | 3920 | /* 512GB MX100 with newer firmware has only LPM issues */ |
| 3921 | { "Crucial_CT512MX100*", NULL, ATA_HORKAGE_ZERO_AFTER_TRIM | |
| 3922 | ATA_HORKAGE_NOLPM, }, |
Hans de Goede | 9c7be59f | 2018-02-16 10:48:20 +0100 | [diff] [blame] | 3923 | |
Hans de Goede | 62ac3f7 | 2018-03-19 16:33:58 +0100 | [diff] [blame] | 3924 | /* 480GB+ M500 SSDs have both queued TRIM and LPM issues */ |
| 3925 | { "Crucial_CT480M500*", NULL, ATA_HORKAGE_NO_NCQ_TRIM | |
| 3926 | ATA_HORKAGE_ZERO_AFTER_TRIM | |
| 3927 | ATA_HORKAGE_NOLPM, }, |
| 3928 | { "Crucial_CT960M500*", NULL, ATA_HORKAGE_NO_NCQ_TRIM | |
| 3929 | ATA_HORKAGE_ZERO_AFTER_TRIM | |
| 3930 | ATA_HORKAGE_NOLPM, }, |
| 3931 | |
François Cami | 76936e9 | 2018-05-13 20:11:15 +0200 | [diff] [blame] | 3932 | /* These specific Samsung models/firmware-revs do not handle LPM well */ |
Hans de Goede | b5b4d3a | 2018-04-24 11:19:07 +0200 | [diff] [blame] | 3933 | { "SAMSUNG MZMPC128HBFU-000MV", "CXM14M1Q", ATA_HORKAGE_NOLPM, }, |
François Cami | 76936e9 | 2018-05-13 20:11:15 +0200 | [diff] [blame] | 3934 | { "SAMSUNG SSD PM830 mSATA *", "CXM13D1Q", ATA_HORKAGE_NOLPM, }, |
Diego Viola | 410b5c7 | 2018-11-12 17:22:52 -0200 | [diff] [blame] | 3935 | { "SAMSUNG MZ7TD256HAFV-000L9", NULL, ATA_HORKAGE_NOLPM, }, |
Hans de Goede | dd95749 | 2019-02-03 10:02:07 +0100 | [diff] [blame] | 3936 | { "SAMSUNG MZ7TE512HMHP-000L1", "EXT06L0Q", ATA_HORKAGE_NOLPM, }, |
Hans de Goede | b5b4d3a | 2018-04-24 11:19:07 +0200 | [diff] [blame] | 3937 | |
Marc Carino | f78dea0 | 2013-12-16 18:15:53 -0800 | [diff] [blame] | 3938 | /* devices that don't properly handle queued TRIM commands */ |
Sudip Mukherjee | 136d769 | 2018-05-19 22:29:36 +0100 | [diff] [blame] | 3939 | { "Micron_M500IT_*", "MU01", ATA_HORKAGE_NO_NCQ_TRIM | |
| 3940 | ATA_HORKAGE_ZERO_AFTER_TRIM, }, |
Martin K. Petersen | 243918b | 2015-06-18 14:50:18 -0400 | [diff] [blame] | 3941 | { "Micron_M500_*", NULL, ATA_HORKAGE_NO_NCQ_TRIM | |
Martin K. Petersen | e61f7d1 | 2015-01-08 10:34:27 -0500 | [diff] [blame] | 3942 | ATA_HORKAGE_ZERO_AFTER_TRIM, }, |
Martin K. Petersen | ff7f53f | 2015-03-27 15:17:20 -0400 | [diff] [blame] | 3943 | { "Crucial_CT*M500*", NULL, ATA_HORKAGE_NO_NCQ_TRIM | |
| 3944 | ATA_HORKAGE_ZERO_AFTER_TRIM, }, |
Martin K. Petersen | 9051bd3 | 2015-07-15 21:03:23 -0400 | [diff] [blame] | 3945 | { "Micron_M5[15]0_*", "MU01", ATA_HORKAGE_NO_NCQ_TRIM | |
Martin K. Petersen | ff7f53f | 2015-03-27 15:17:20 -0400 | [diff] [blame] | 3946 | ATA_HORKAGE_ZERO_AFTER_TRIM, }, |
| 3947 | { "Crucial_CT*M550*", "MU01", ATA_HORKAGE_NO_NCQ_TRIM | |
| 3948 | ATA_HORKAGE_ZERO_AFTER_TRIM, }, |
| 3949 | { "Crucial_CT*MX100*", "MU01", ATA_HORKAGE_NO_NCQ_TRIM | |
| 3950 | ATA_HORKAGE_ZERO_AFTER_TRIM, }, |
Ju Hyung Park | ca6bfcb | 2018-03-11 02:28:35 +0900 | [diff] [blame] | 3951 | { "Samsung SSD 840*", NULL, ATA_HORKAGE_NO_NCQ_TRIM | |
| 3952 | ATA_HORKAGE_ZERO_AFTER_TRIM, }, |
| 3953 | { "Samsung SSD 850*", NULL, ATA_HORKAGE_NO_NCQ_TRIM | |
Martin K. Petersen | 6fc4d97 | 2015-03-27 15:17:21 -0400 | [diff] [blame] | 3954 | ATA_HORKAGE_ZERO_AFTER_TRIM, }, |
Guillermo A. Amaral | 7a7184b | 2015-08-25 23:29:13 -0700 | [diff] [blame] | 3955 | { "FCCT*M500*", NULL, ATA_HORKAGE_NO_NCQ_TRIM | |
| 3956 | ATA_HORKAGE_ZERO_AFTER_TRIM, }, |
Martin K. Petersen | e61f7d1 | 2015-01-08 10:34:27 -0500 | [diff] [blame] | 3957 | |
Arne Fitzenreiter | cda57b1 | 2015-07-15 13:54:37 +0200 | [diff] [blame] | 3958 | /* devices that don't properly handle TRIM commands */ |
| 3959 | { "SuperSSpeed S238*", NULL, ATA_HORKAGE_NOTRIM, }, |
| 3960 | |
Martin K. Petersen | e61f7d1 | 2015-01-08 10:34:27 -0500 | [diff] [blame] | 3961 | /* |
| 3962 | * As defined, the DRAT (Deterministic Read After Trim) and RZAT |
| 3963 | * (Return Zero After Trim) flags in the ATA Command Set are |
| 3964 | * unreliable in the sense that they only define what happens if |
| 3965 | * the device successfully executed the DSM TRIM command. TRIM |
| 3966 | * is only advisory, however, and the device is free to silently |
| 3967 | * ignore all or parts of the request. |
| 3968 | * |
| 3969 | * Whitelist drives that are known to reliably return zeroes |
| 3970 | * after TRIM. |
| 3971 | */ |
| 3972 | |
| 3973 | /* |
| 3974 | * The intel 510 drive has buggy DRAT/RZAT. Explicitly exclude |
| 3975 | * that model before whitelisting all other intel SSDs. |
| 3976 | */ |
| 3977 | { "INTEL*SSDSC2MH*", NULL, 0, }, |
| 3978 | |
Martin K. Petersen | ff7f53f | 2015-03-27 15:17:20 -0400 | [diff] [blame] | 3979 | { "Micron*", NULL, ATA_HORKAGE_ZERO_AFTER_TRIM, }, |
| 3980 | { "Crucial*", NULL, ATA_HORKAGE_ZERO_AFTER_TRIM, }, |
Martin K. Petersen | e61f7d1 | 2015-01-08 10:34:27 -0500 | [diff] [blame] | 3981 | { "INTEL*SSD*", NULL, ATA_HORKAGE_ZERO_AFTER_TRIM, }, |
| 3982 | { "SSD*INTEL*", NULL, ATA_HORKAGE_ZERO_AFTER_TRIM, }, |
| 3983 | { "Samsung*SSD*", NULL, ATA_HORKAGE_ZERO_AFTER_TRIM, }, |
| 3984 | { "SAMSUNG*SSD*", NULL, ATA_HORKAGE_ZERO_AFTER_TRIM, }, |
Juha-Matti Tilli | fd6f32f | 2018-12-02 12:47:08 +0200 | [diff] [blame] | 3985 | { "SAMSUNG*MZ7KM*", NULL, ATA_HORKAGE_ZERO_AFTER_TRIM, }, |
Martin K. Petersen | e61f7d1 | 2015-01-08 10:34:27 -0500 | [diff] [blame] | 3986 | { "ST[1248][0248]0[FH]*", NULL, ATA_HORKAGE_ZERO_AFTER_TRIM, }, |
Marc Carino | f78dea0 | 2013-12-16 18:15:53 -0800 | [diff] [blame] | 3987 | |
Tejun Heo | ecd75ad | 2014-01-16 09:47:17 -0500 | [diff] [blame] | 3988 | /* |
| 3989 | * Some WD SATA-I drives spin up and down erratically when the link |
| 3990 | * is put into the slumber mode. We don't have full list of the |
| 3991 | * affected devices. Disable LPM if the device matches one of the |
| 3992 | * known prefixes and is SATA-1. As a side effect LPM partial is |
| 3993 | * lost too. |
| 3994 | * |
| 3995 | * https://bugzilla.kernel.org/show_bug.cgi?id=57211 |
| 3996 | */ |
| 3997 | { "WDC WD800JD-*", NULL, ATA_HORKAGE_WD_BROKEN_LPM }, |
| 3998 | { "WDC WD1200JD-*", NULL, ATA_HORKAGE_WD_BROKEN_LPM }, |
| 3999 | { "WDC WD1600JD-*", NULL, ATA_HORKAGE_WD_BROKEN_LPM }, |
| 4000 | { "WDC WD2000JD-*", NULL, ATA_HORKAGE_WD_BROKEN_LPM }, |
| 4001 | { "WDC WD2500JD-*", NULL, ATA_HORKAGE_WD_BROKEN_LPM }, |
| 4002 | { "WDC WD3000JD-*", NULL, ATA_HORKAGE_WD_BROKEN_LPM }, |
| 4003 | { "WDC WD3200JD-*", NULL, ATA_HORKAGE_WD_BROKEN_LPM }, |
| 4004 | |
Alan Cox | 6919a0a | 2006-10-27 19:08:46 -0700 | [diff] [blame] | 4005 | /* End Marker */ |
| 4006 | { } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4007 | }; |
Jeff Garzik | 2e9edbf | 2006-03-24 09:56:57 -0500 | [diff] [blame] | 4008 | |
Tejun Heo | 75683fe | 2007-07-05 13:31:27 +0900 | [diff] [blame] | 4009 | static unsigned long ata_dev_blacklisted(const struct ata_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4010 | { |
Tejun Heo | 8bfa79f | 2007-01-02 20:19:40 +0900 | [diff] [blame] | 4011 | unsigned char model_num[ATA_ID_PROD_LEN + 1]; |
| 4012 | unsigned char model_rev[ATA_ID_FW_REV_LEN + 1]; |
Alan Cox | 6919a0a | 2006-10-27 19:08:46 -0700 | [diff] [blame] | 4013 | const struct ata_blacklist_entry *ad = ata_device_blacklist; |
Albert Lee | 3a77827 | 2006-06-22 13:00:25 +0800 | [diff] [blame] | 4014 | |
Tejun Heo | 8bfa79f | 2007-01-02 20:19:40 +0900 | [diff] [blame] | 4015 | ata_id_c_string(dev->id, model_num, ATA_ID_PROD, sizeof(model_num)); |
| 4016 | 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] | 4017 | |
Alan Cox | 6919a0a | 2006-10-27 19:08:46 -0700 | [diff] [blame] | 4018 | while (ad->model_num) { |
George Spelvin | 1c40279 | 2014-10-07 07:26:38 -0400 | [diff] [blame] | 4019 | if (glob_match(ad->model_num, model_num)) { |
Alan Cox | 6919a0a | 2006-10-27 19:08:46 -0700 | [diff] [blame] | 4020 | if (ad->model_rev == NULL) |
| 4021 | return ad->horkage; |
George Spelvin | 1c40279 | 2014-10-07 07:26:38 -0400 | [diff] [blame] | 4022 | if (glob_match(ad->model_rev, model_rev)) |
Alan Cox | 6919a0a | 2006-10-27 19:08:46 -0700 | [diff] [blame] | 4023 | return ad->horkage; |
Alan Cox | f4b15fe | 2006-03-22 15:54:04 +0000 | [diff] [blame] | 4024 | } |
Alan Cox | 6919a0a | 2006-10-27 19:08:46 -0700 | [diff] [blame] | 4025 | ad++; |
Alan Cox | f4b15fe | 2006-03-22 15:54:04 +0000 | [diff] [blame] | 4026 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4027 | return 0; |
| 4028 | } |
| 4029 | |
Alan Cox | 6919a0a | 2006-10-27 19:08:46 -0700 | [diff] [blame] | 4030 | static int ata_dma_blacklisted(const struct ata_device *dev) |
| 4031 | { |
| 4032 | /* We don't support polling DMA. |
| 4033 | * DMA blacklist those ATAPI devices with CDB-intr (and use PIO) |
| 4034 | * if the LLDD handles only interrupts in the HSM_ST_LAST state. |
| 4035 | */ |
Tejun Heo | 9af5c9c | 2007-08-06 18:36:22 +0900 | [diff] [blame] | 4036 | if ((dev->link->ap->flags & ATA_FLAG_PIO_POLLING) && |
Alan Cox | 6919a0a | 2006-10-27 19:08:46 -0700 | [diff] [blame] | 4037 | (dev->flags & ATA_DFLAG_CDB_INTR)) |
| 4038 | return 1; |
Tejun Heo | 75683fe | 2007-07-05 13:31:27 +0900 | [diff] [blame] | 4039 | return (dev->horkage & ATA_HORKAGE_NODMA) ? 1 : 0; |
Alan Cox | 6919a0a | 2006-10-27 19:08:46 -0700 | [diff] [blame] | 4040 | } |
| 4041 | |
Tejun Heo | a6d5a51 | 2006-03-06 04:31:57 +0900 | [diff] [blame] | 4042 | /** |
Alan Cox | 6bbfd53 | 2007-11-05 22:58:58 +0000 | [diff] [blame] | 4043 | * ata_is_40wire - check drive side detection |
| 4044 | * @dev: device |
| 4045 | * |
| 4046 | * Perform drive side detection decoding, allowing for device vendors |
| 4047 | * who can't follow the documentation. |
| 4048 | */ |
| 4049 | |
| 4050 | static int ata_is_40wire(struct ata_device *dev) |
| 4051 | { |
| 4052 | if (dev->horkage & ATA_HORKAGE_IVB) |
| 4053 | return ata_drive_40wire_relaxed(dev->id); |
| 4054 | return ata_drive_40wire(dev->id); |
| 4055 | } |
| 4056 | |
| 4057 | /** |
Alan Cox | 15a5551 | 2008-03-28 14:33:46 -0700 | [diff] [blame] | 4058 | * cable_is_40wire - 40/80/SATA decider |
| 4059 | * @ap: port to consider |
| 4060 | * |
| 4061 | * This function encapsulates the policy for speed management |
| 4062 | * in one place. At the moment we don't cache the result but |
| 4063 | * there is a good case for setting ap->cbl to the result when |
| 4064 | * we are called with unknown cables (and figuring out if it |
| 4065 | * impacts hotplug at all). |
| 4066 | * |
| 4067 | * Return 1 if the cable appears to be 40 wire. |
| 4068 | */ |
| 4069 | |
| 4070 | static int cable_is_40wire(struct ata_port *ap) |
| 4071 | { |
| 4072 | struct ata_link *link; |
| 4073 | struct ata_device *dev; |
| 4074 | |
Tejun Heo | 4a9c7b3 | 2008-10-27 19:59:23 +0900 | [diff] [blame] | 4075 | /* If the controller thinks we are 40 wire, we are. */ |
Alan Cox | 15a5551 | 2008-03-28 14:33:46 -0700 | [diff] [blame] | 4076 | if (ap->cbl == ATA_CBL_PATA40) |
| 4077 | return 1; |
Tejun Heo | 4a9c7b3 | 2008-10-27 19:59:23 +0900 | [diff] [blame] | 4078 | |
| 4079 | /* If the controller thinks we are 80 wire, we are. */ |
Alan Cox | 15a5551 | 2008-03-28 14:33:46 -0700 | [diff] [blame] | 4080 | if (ap->cbl == ATA_CBL_PATA80 || ap->cbl == ATA_CBL_SATA) |
| 4081 | return 0; |
Tejun Heo | 4a9c7b3 | 2008-10-27 19:59:23 +0900 | [diff] [blame] | 4082 | |
| 4083 | /* If the system is known to be 40 wire short cable (eg |
| 4084 | * laptop), then we allow 80 wire modes even if the drive |
| 4085 | * isn't sure. |
| 4086 | */ |
Alan Cox | f792068 | 2008-04-08 16:37:50 +0100 | [diff] [blame] | 4087 | if (ap->cbl == ATA_CBL_PATA40_SHORT) |
| 4088 | return 0; |
Alan Cox | 15a5551 | 2008-03-28 14:33:46 -0700 | [diff] [blame] | 4089 | |
Tejun Heo | 4a9c7b3 | 2008-10-27 19:59:23 +0900 | [diff] [blame] | 4090 | /* If the controller doesn't know, we scan. |
| 4091 | * |
| 4092 | * Note: We look for all 40 wire detects at this point. Any |
| 4093 | * 80 wire detect is taken to be 80 wire cable because |
| 4094 | * - in many setups only the one drive (slave if present) will |
| 4095 | * give a valid detect |
| 4096 | * - if you have a non detect capable drive you don't want it |
| 4097 | * to colour the choice |
| 4098 | */ |
Tejun Heo | 1eca436 | 2008-11-03 20:03:17 +0900 | [diff] [blame] | 4099 | ata_for_each_link(link, ap, EDGE) { |
| 4100 | ata_for_each_dev(dev, link, ENABLED) { |
| 4101 | if (!ata_is_40wire(dev)) |
Alan Cox | 15a5551 | 2008-03-28 14:33:46 -0700 | [diff] [blame] | 4102 | return 0; |
| 4103 | } |
| 4104 | } |
| 4105 | return 1; |
| 4106 | } |
| 4107 | |
| 4108 | /** |
Tejun Heo | a6d5a51 | 2006-03-06 04:31:57 +0900 | [diff] [blame] | 4109 | * ata_dev_xfermask - Compute supported xfermask of the given device |
Tejun Heo | a6d5a51 | 2006-03-06 04:31:57 +0900 | [diff] [blame] | 4110 | * @dev: Device to compute xfermask for |
| 4111 | * |
Tejun Heo | acf356b | 2006-03-24 14:07:50 +0900 | [diff] [blame] | 4112 | * Compute supported xfermask of @dev and store it in |
| 4113 | * dev->*_mask. This function is responsible for applying all |
| 4114 | * known limits including host controller limits, device |
| 4115 | * blacklist, etc... |
Tejun Heo | a6d5a51 | 2006-03-06 04:31:57 +0900 | [diff] [blame] | 4116 | * |
| 4117 | * LOCKING: |
| 4118 | * None. |
Tejun Heo | a6d5a51 | 2006-03-06 04:31:57 +0900 | [diff] [blame] | 4119 | */ |
Tejun Heo | 3373efd | 2006-05-15 20:57:53 +0900 | [diff] [blame] | 4120 | static void ata_dev_xfermask(struct ata_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4121 | { |
Tejun Heo | 9af5c9c | 2007-08-06 18:36:22 +0900 | [diff] [blame] | 4122 | struct ata_link *link = dev->link; |
| 4123 | struct ata_port *ap = link->ap; |
Jeff Garzik | cca3974 | 2006-08-24 03:19:22 -0400 | [diff] [blame] | 4124 | struct ata_host *host = ap->host; |
Tejun Heo | a6d5a51 | 2006-03-06 04:31:57 +0900 | [diff] [blame] | 4125 | unsigned long xfer_mask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4126 | |
Tejun Heo | 37deecb | 2006-08-10 16:59:07 +0900 | [diff] [blame] | 4127 | /* controller modes available */ |
Tejun Heo | 565083e | 2006-04-02 17:54:47 +0900 | [diff] [blame] | 4128 | xfer_mask = ata_pack_xfermask(ap->pio_mask, |
| 4129 | ap->mwdma_mask, ap->udma_mask); |
| 4130 | |
Robert Hancock | 8343f88 | 2007-03-06 02:37:51 -0800 | [diff] [blame] | 4131 | /* drive modes available */ |
Tejun Heo | 37deecb | 2006-08-10 16:59:07 +0900 | [diff] [blame] | 4132 | xfer_mask &= ata_pack_xfermask(dev->pio_mask, |
| 4133 | dev->mwdma_mask, dev->udma_mask); |
| 4134 | xfer_mask &= ata_id_xfermask(dev->id); |
Tejun Heo | 565083e | 2006-04-02 17:54:47 +0900 | [diff] [blame] | 4135 | |
Alan Cox | b352e57 | 2006-08-10 18:52:12 +0100 | [diff] [blame] | 4136 | /* |
| 4137 | * CFA Advanced TrueIDE timings are not allowed on a shared |
| 4138 | * cable |
| 4139 | */ |
| 4140 | if (ata_dev_pair(dev)) { |
| 4141 | /* No PIO5 or PIO6 */ |
| 4142 | xfer_mask &= ~(0x03 << (ATA_SHIFT_PIO + 5)); |
| 4143 | /* No MWDMA3 or MWDMA 4 */ |
| 4144 | xfer_mask &= ~(0x03 << (ATA_SHIFT_MWDMA + 3)); |
| 4145 | } |
| 4146 | |
Tejun Heo | 37deecb | 2006-08-10 16:59:07 +0900 | [diff] [blame] | 4147 | if (ata_dma_blacklisted(dev)) { |
| 4148 | xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA); |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 4149 | ata_dev_warn(dev, |
| 4150 | "device is on DMA blacklist, disabling DMA\n"); |
Tejun Heo | 37deecb | 2006-08-10 16:59:07 +0900 | [diff] [blame] | 4151 | } |
Tejun Heo | a6d5a51 | 2006-03-06 04:31:57 +0900 | [diff] [blame] | 4152 | |
Petr Vandrovec | 14d66ab | 2007-03-08 10:12:12 +0100 | [diff] [blame] | 4153 | if ((host->flags & ATA_HOST_SIMPLEX) && |
Jeff Garzik | 2dcb407 | 2007-10-19 06:42:56 -0400 | [diff] [blame] | 4154 | host->simplex_claimed && host->simplex_claimed != ap) { |
Tejun Heo | 37deecb | 2006-08-10 16:59:07 +0900 | [diff] [blame] | 4155 | xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA); |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 4156 | ata_dev_warn(dev, |
| 4157 | "simplex DMA is claimed by other device, disabling DMA\n"); |
Alan Cox | 5444a6f | 2006-03-27 18:58:20 +0100 | [diff] [blame] | 4158 | } |
Tejun Heo | 565083e | 2006-04-02 17:54:47 +0900 | [diff] [blame] | 4159 | |
Jeff Garzik | e424675 | 2007-03-09 09:56:46 -0500 | [diff] [blame] | 4160 | if (ap->flags & ATA_FLAG_NO_IORDY) |
| 4161 | xfer_mask &= ata_pio_mask_no_iordy(dev); |
| 4162 | |
Alan Cox | 5444a6f | 2006-03-27 18:58:20 +0100 | [diff] [blame] | 4163 | if (ap->ops->mode_filter) |
Alan Cox | a76b62ca | 2007-03-09 09:34:07 -0500 | [diff] [blame] | 4164 | xfer_mask = ap->ops->mode_filter(dev, xfer_mask); |
Alan Cox | 5444a6f | 2006-03-27 18:58:20 +0100 | [diff] [blame] | 4165 | |
Robert Hancock | 8343f88 | 2007-03-06 02:37:51 -0800 | [diff] [blame] | 4166 | /* Apply cable rule here. Don't apply it early because when |
| 4167 | * we handle hot plug the cable type can itself change. |
| 4168 | * Check this last so that we know if the transfer rate was |
| 4169 | * solely limited by the cable. |
| 4170 | * Unknown or 80 wire cables reported host side are checked |
| 4171 | * drive side as well. Cases where we know a 40wire cable |
| 4172 | * is used safely for 80 are not checked here. |
| 4173 | */ |
| 4174 | if (xfer_mask & (0xF8 << ATA_SHIFT_UDMA)) |
| 4175 | /* UDMA/44 or higher would be available */ |
Alan Cox | 15a5551 | 2008-03-28 14:33:46 -0700 | [diff] [blame] | 4176 | if (cable_is_40wire(ap)) { |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 4177 | ata_dev_warn(dev, |
| 4178 | "limited to UDMA/33 due to 40-wire cable\n"); |
Robert Hancock | 8343f88 | 2007-03-06 02:37:51 -0800 | [diff] [blame] | 4179 | xfer_mask &= ~(0xF8 << ATA_SHIFT_UDMA); |
| 4180 | } |
| 4181 | |
Tejun Heo | 565083e | 2006-04-02 17:54:47 +0900 | [diff] [blame] | 4182 | ata_unpack_xfermask(xfer_mask, &dev->pio_mask, |
| 4183 | &dev->mwdma_mask, &dev->udma_mask); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4184 | } |
| 4185 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4186 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4187 | * ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4188 | * @dev: Device to which command will be sent |
| 4189 | * |
Jeff Garzik | 780a87f | 2005-05-30 15:41:05 -0400 | [diff] [blame] | 4190 | * Issue SET FEATURES - XFER MODE command to device @dev |
| 4191 | * on port @ap. |
| 4192 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4193 | * LOCKING: |
Jeff Garzik | 0cba632 | 2005-05-30 19:49:12 -0400 | [diff] [blame] | 4194 | * PCI/etc. bus probe sem. |
Tejun Heo | 83206a2 | 2006-03-24 15:25:31 +0900 | [diff] [blame] | 4195 | * |
| 4196 | * RETURNS: |
| 4197 | * 0 on success, AC_ERR_* mask otherwise. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4198 | */ |
| 4199 | |
Tejun Heo | 3373efd | 2006-05-15 20:57:53 +0900 | [diff] [blame] | 4200 | static unsigned int ata_dev_set_xfermode(struct ata_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4201 | { |
Tejun Heo | a012370 | 2005-12-13 14:49:31 +0900 | [diff] [blame] | 4202 | struct ata_taskfile tf; |
Tejun Heo | 83206a2 | 2006-03-24 15:25:31 +0900 | [diff] [blame] | 4203 | unsigned int err_mask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4204 | |
| 4205 | /* set up set-features taskfile */ |
| 4206 | DPRINTK("set features - xfer mode\n"); |
| 4207 | |
Tejun Heo | 464cf17 | 2007-05-27 15:10:40 +0200 | [diff] [blame] | 4208 | /* Some controllers and ATAPI devices show flaky interrupt |
| 4209 | * behavior after setting xfer mode. Use polling instead. |
| 4210 | */ |
Tejun Heo | 3373efd | 2006-05-15 20:57:53 +0900 | [diff] [blame] | 4211 | ata_tf_init(dev, &tf); |
Tejun Heo | a012370 | 2005-12-13 14:49:31 +0900 | [diff] [blame] | 4212 | tf.command = ATA_CMD_SET_FEATURES; |
| 4213 | tf.feature = SETFEATURES_XFER; |
Tejun Heo | 464cf17 | 2007-05-27 15:10:40 +0200 | [diff] [blame] | 4214 | tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE | ATA_TFLAG_POLLING; |
Tejun Heo | a012370 | 2005-12-13 14:49:31 +0900 | [diff] [blame] | 4215 | tf.protocol = ATA_PROT_NODATA; |
Alan Cox | b9f8ab2 | 2007-11-19 14:33:11 +0000 | [diff] [blame] | 4216 | /* If we are using IORDY we must send the mode setting command */ |
Jeff Garzik | 11b7bec | 2007-11-23 21:12:14 -0500 | [diff] [blame] | 4217 | if (ata_pio_need_iordy(dev)) |
| 4218 | tf.nsect = dev->xfer_mode; |
Alan Cox | b9f8ab2 | 2007-11-19 14:33:11 +0000 | [diff] [blame] | 4219 | /* If the device has IORDY and the controller does not - turn it off */ |
| 4220 | else if (ata_id_has_iordy(dev->id)) |
Jeff Garzik | 11b7bec | 2007-11-23 21:12:14 -0500 | [diff] [blame] | 4221 | tf.nsect = 0x01; |
Alan Cox | b9f8ab2 | 2007-11-19 14:33:11 +0000 | [diff] [blame] | 4222 | else /* In the ancient relic department - skip all of this */ |
| 4223 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4224 | |
Mikulas Patocka | d531be2 | 2015-07-08 13:06:12 -0400 | [diff] [blame] | 4225 | /* On some disks, this command causes spin-up, so we need longer timeout */ |
| 4226 | 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] | 4227 | |
Tejun Heo | 83206a2 | 2006-03-24 15:25:31 +0900 | [diff] [blame] | 4228 | DPRINTK("EXIT, err_mask=%x\n", err_mask); |
| 4229 | return err_mask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4230 | } |
Tejun Heo | 1152b26 | 2010-09-01 17:50:05 +0200 | [diff] [blame] | 4231 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4232 | /** |
Jeff Garzik | 218f3d3 | 2007-10-25 00:33:27 -0400 | [diff] [blame] | 4233 | * ata_dev_set_feature - Issue SET FEATURES - SATA FEATURES |
Kristen Carlson Accardi | 9f45cbd | 2007-08-15 03:57:11 -0400 | [diff] [blame] | 4234 | * @dev: Device to which command will be sent |
| 4235 | * @enable: Whether to enable or disable the feature |
Jeff Garzik | 218f3d3 | 2007-10-25 00:33:27 -0400 | [diff] [blame] | 4236 | * @feature: The sector count represents the feature to set |
Kristen Carlson Accardi | 9f45cbd | 2007-08-15 03:57:11 -0400 | [diff] [blame] | 4237 | * |
| 4238 | * Issue SET FEATURES - SATA FEATURES command to device @dev |
Jeff Garzik | 218f3d3 | 2007-10-25 00:33:27 -0400 | [diff] [blame] | 4239 | * on port @ap with sector count |
Kristen Carlson Accardi | 9f45cbd | 2007-08-15 03:57:11 -0400 | [diff] [blame] | 4240 | * |
| 4241 | * LOCKING: |
| 4242 | * PCI/etc. bus probe sem. |
| 4243 | * |
| 4244 | * RETURNS: |
| 4245 | * 0 on success, AC_ERR_* mask otherwise. |
| 4246 | */ |
Tejun Heo | 1152b26 | 2010-09-01 17:50:05 +0200 | [diff] [blame] | 4247 | 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] | 4248 | { |
| 4249 | struct ata_taskfile tf; |
| 4250 | unsigned int err_mask; |
Damien Le Moal | 974e0a4 | 2016-04-04 12:17:09 -0400 | [diff] [blame] | 4251 | unsigned long timeout = 0; |
Kristen Carlson Accardi | 9f45cbd | 2007-08-15 03:57:11 -0400 | [diff] [blame] | 4252 | |
| 4253 | /* set up set-features taskfile */ |
| 4254 | DPRINTK("set features - SATA features\n"); |
| 4255 | |
| 4256 | ata_tf_init(dev, &tf); |
| 4257 | tf.command = ATA_CMD_SET_FEATURES; |
| 4258 | tf.feature = enable; |
| 4259 | tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE; |
| 4260 | tf.protocol = ATA_PROT_NODATA; |
Jeff Garzik | 218f3d3 | 2007-10-25 00:33:27 -0400 | [diff] [blame] | 4261 | tf.nsect = feature; |
Kristen Carlson Accardi | 9f45cbd | 2007-08-15 03:57:11 -0400 | [diff] [blame] | 4262 | |
Damien Le Moal | 974e0a4 | 2016-04-04 12:17:09 -0400 | [diff] [blame] | 4263 | if (enable == SETFEATURES_SPINUP) |
| 4264 | timeout = ata_probe_timeout ? |
| 4265 | ata_probe_timeout * 1000 : SETFEATURES_SPINUP_TIMEOUT; |
| 4266 | 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] | 4267 | |
| 4268 | DPRINTK("EXIT, err_mask=%x\n", err_mask); |
| 4269 | return err_mask; |
| 4270 | } |
Jeff Garzik | 633de4c | 2012-09-13 01:12:29 -0400 | [diff] [blame] | 4271 | EXPORT_SYMBOL_GPL(ata_dev_set_feature); |
Kristen Carlson Accardi | 9f45cbd | 2007-08-15 03:57:11 -0400 | [diff] [blame] | 4272 | |
| 4273 | /** |
Albert Lee | 8bf62ece | 2005-05-12 15:29:42 -0400 | [diff] [blame] | 4274 | * ata_dev_init_params - Issue INIT DEV PARAMS command |
Albert Lee | 8bf62ece | 2005-05-12 15:29:42 -0400 | [diff] [blame] | 4275 | * @dev: Device to which command will be sent |
Randy Dunlap | e2a7f77 | 2006-05-18 10:50:18 -0700 | [diff] [blame] | 4276 | * @heads: Number of heads (taskfile parameter) |
| 4277 | * @sectors: Number of sectors (taskfile parameter) |
Albert Lee | 8bf62ece | 2005-05-12 15:29:42 -0400 | [diff] [blame] | 4278 | * |
| 4279 | * LOCKING: |
Tejun Heo | 6aff8f1 | 2006-02-15 18:24:09 +0900 | [diff] [blame] | 4280 | * Kernel thread context (may sleep) |
| 4281 | * |
| 4282 | * RETURNS: |
| 4283 | * 0 on success, AC_ERR_* mask otherwise. |
Albert Lee | 8bf62ece | 2005-05-12 15:29:42 -0400 | [diff] [blame] | 4284 | */ |
Tejun Heo | 3373efd | 2006-05-15 20:57:53 +0900 | [diff] [blame] | 4285 | static unsigned int ata_dev_init_params(struct ata_device *dev, |
| 4286 | u16 heads, u16 sectors) |
Albert Lee | 8bf62ece | 2005-05-12 15:29:42 -0400 | [diff] [blame] | 4287 | { |
Tejun Heo | a012370 | 2005-12-13 14:49:31 +0900 | [diff] [blame] | 4288 | struct ata_taskfile tf; |
Tejun Heo | 6aff8f1 | 2006-02-15 18:24:09 +0900 | [diff] [blame] | 4289 | unsigned int err_mask; |
Albert Lee | 8bf62ece | 2005-05-12 15:29:42 -0400 | [diff] [blame] | 4290 | |
| 4291 | /* Number of sectors per track 1-255. Number of heads 1-16 */ |
| 4292 | if (sectors < 1 || sectors > 255 || heads < 1 || heads > 16) |
Albert Lee | 00b6f5e | 2006-03-27 16:39:18 +0800 | [diff] [blame] | 4293 | return AC_ERR_INVALID; |
Albert Lee | 8bf62ece | 2005-05-12 15:29:42 -0400 | [diff] [blame] | 4294 | |
| 4295 | /* set up init dev params taskfile */ |
| 4296 | DPRINTK("init dev params \n"); |
| 4297 | |
Tejun Heo | 3373efd | 2006-05-15 20:57:53 +0900 | [diff] [blame] | 4298 | ata_tf_init(dev, &tf); |
Tejun Heo | a012370 | 2005-12-13 14:49:31 +0900 | [diff] [blame] | 4299 | tf.command = ATA_CMD_INIT_DEV_PARAMS; |
| 4300 | tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE; |
| 4301 | tf.protocol = ATA_PROT_NODATA; |
| 4302 | tf.nsect = sectors; |
| 4303 | tf.device |= (heads - 1) & 0x0f; /* max head = num. of heads - 1 */ |
Albert Lee | 8bf62ece | 2005-05-12 15:29:42 -0400 | [diff] [blame] | 4304 | |
Tejun Heo | 2b78910 | 2007-10-09 15:05:44 +0900 | [diff] [blame] | 4305 | 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] | 4306 | /* A clean abort indicates an original or just out of spec drive |
| 4307 | and we should continue as we issue the setup based on the |
| 4308 | drive reported working geometry */ |
| 4309 | if (err_mask == AC_ERR_DEV && (tf.feature & ATA_ABORTED)) |
| 4310 | err_mask = 0; |
Albert Lee | 8bf62ece | 2005-05-12 15:29:42 -0400 | [diff] [blame] | 4311 | |
Tejun Heo | 6aff8f1 | 2006-02-15 18:24:09 +0900 | [diff] [blame] | 4312 | DPRINTK("EXIT, err_mask=%x\n", err_mask); |
| 4313 | return err_mask; |
Albert Lee | 8bf62ece | 2005-05-12 15:29:42 -0400 | [diff] [blame] | 4314 | } |
| 4315 | |
| 4316 | /** |
Tejun Heo | 5895ef9 | 2008-06-17 12:36:26 +0900 | [diff] [blame] | 4317 | * atapi_check_dma - Check whether ATAPI DMA can be supported |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4318 | * @qc: Metadata associated with taskfile to check |
| 4319 | * |
Jeff Garzik | 780a87f | 2005-05-30 15:41:05 -0400 | [diff] [blame] | 4320 | * Allow low-level driver to filter ATA PACKET commands, returning |
| 4321 | * a status indicating whether or not it is OK to use DMA for the |
| 4322 | * supplied PACKET command. |
| 4323 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4324 | * LOCKING: |
Jeff Garzik | cca3974 | 2006-08-24 03:19:22 -0400 | [diff] [blame] | 4325 | * spin_lock_irqsave(host lock) |
Jeff Garzik | 0cba632 | 2005-05-30 19:49:12 -0400 | [diff] [blame] | 4326 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4327 | * RETURNS: 0 when ATAPI DMA can be used |
| 4328 | * nonzero otherwise |
| 4329 | */ |
Tejun Heo | 5895ef9 | 2008-06-17 12:36:26 +0900 | [diff] [blame] | 4330 | int atapi_check_dma(struct ata_queued_cmd *qc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4331 | { |
| 4332 | struct ata_port *ap = qc->ap; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4333 | |
Tejun Heo | b9a4197 | 2007-06-27 02:48:43 +0900 | [diff] [blame] | 4334 | /* Don't allow DMA if it isn't multiple of 16 bytes. Quite a |
| 4335 | * few ATAPI devices choke on such DMA requests. |
| 4336 | */ |
Tejun Heo | 6a87e42 | 2008-11-03 19:01:09 +0900 | [diff] [blame] | 4337 | if (!(qc->dev->horkage & ATA_HORKAGE_ATAPI_MOD16_DMA) && |
| 4338 | unlikely(qc->nbytes & 15)) |
Tejun Heo | b9a4197 | 2007-06-27 02:48:43 +0900 | [diff] [blame] | 4339 | return 1; |
Albert Lee | 6f23a31 | 2007-04-02 11:39:25 +0800 | [diff] [blame] | 4340 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4341 | if (ap->ops->check_atapi_dma) |
Tejun Heo | b9a4197 | 2007-06-27 02:48:43 +0900 | [diff] [blame] | 4342 | return ap->ops->check_atapi_dma(qc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4343 | |
Tejun Heo | b9a4197 | 2007-06-27 02:48:43 +0900 | [diff] [blame] | 4344 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4345 | } |
Tejun Heo | b9a4197 | 2007-06-27 02:48:43 +0900 | [diff] [blame] | 4346 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4347 | /** |
Tejun Heo | 31cc23b | 2007-09-23 13:14:12 +0900 | [diff] [blame] | 4348 | * ata_std_qc_defer - Check whether a qc needs to be deferred |
| 4349 | * @qc: ATA command in question |
| 4350 | * |
| 4351 | * Non-NCQ commands cannot run with any other command, NCQ or |
| 4352 | * not. As upper layer only knows the queue depth, we are |
| 4353 | * responsible for maintaining exclusion. This function checks |
| 4354 | * whether a new command @qc can be issued. |
| 4355 | * |
| 4356 | * LOCKING: |
| 4357 | * spin_lock_irqsave(host lock) |
| 4358 | * |
| 4359 | * RETURNS: |
| 4360 | * ATA_DEFER_* if deferring is needed, 0 otherwise. |
| 4361 | */ |
| 4362 | int ata_std_qc_defer(struct ata_queued_cmd *qc) |
| 4363 | { |
| 4364 | struct ata_link *link = qc->dev->link; |
| 4365 | |
Hannes Reinecke | 179b310 | 2016-07-14 09:05:43 +0900 | [diff] [blame] | 4366 | if (ata_is_ncq(qc->tf.protocol)) { |
Tejun Heo | 31cc23b | 2007-09-23 13:14:12 +0900 | [diff] [blame] | 4367 | if (!ata_tag_valid(link->active_tag)) |
| 4368 | return 0; |
| 4369 | } else { |
| 4370 | if (!ata_tag_valid(link->active_tag) && !link->sactive) |
| 4371 | return 0; |
| 4372 | } |
| 4373 | |
| 4374 | return ATA_DEFER_LINK; |
| 4375 | } |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 4376 | EXPORT_SYMBOL_GPL(ata_std_qc_defer); |
Tejun Heo | 31cc23b | 2007-09-23 13:14:12 +0900 | [diff] [blame] | 4377 | |
Jiri Slaby | 95364f3 | 2019-10-31 10:59:45 +0100 | [diff] [blame] | 4378 | enum ata_completion_errors ata_noop_qc_prep(struct ata_queued_cmd *qc) |
| 4379 | { |
| 4380 | return AC_ERR_OK; |
| 4381 | } |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 4382 | EXPORT_SYMBOL_GPL(ata_noop_qc_prep); |
Brian King | e46834c | 2006-03-17 17:04:03 -0600 | [diff] [blame] | 4383 | |
Jeff Garzik | 0cba632 | 2005-05-30 19:49:12 -0400 | [diff] [blame] | 4384 | /** |
Jeff Garzik | 0cba632 | 2005-05-30 19:49:12 -0400 | [diff] [blame] | 4385 | * ata_sg_init - Associate command with scatter-gather table. |
| 4386 | * @qc: Command to be associated |
| 4387 | * @sg: Scatter-gather table. |
| 4388 | * @n_elem: Number of elements in s/g table. |
| 4389 | * |
| 4390 | * Initialize the data-related elements of queued_cmd @qc |
| 4391 | * to point to a scatter-gather table @sg, containing @n_elem |
| 4392 | * elements. |
| 4393 | * |
| 4394 | * LOCKING: |
Jeff Garzik | cca3974 | 2006-08-24 03:19:22 -0400 | [diff] [blame] | 4395 | * spin_lock_irqsave(host lock) |
Jeff Garzik | 0cba632 | 2005-05-30 19:49:12 -0400 | [diff] [blame] | 4396 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4397 | void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg, |
| 4398 | unsigned int n_elem) |
| 4399 | { |
Tejun Heo | ff2aeb1 | 2007-12-05 16:43:11 +0900 | [diff] [blame] | 4400 | qc->sg = sg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4401 | qc->n_elem = n_elem; |
Tejun Heo | ff2aeb1 | 2007-12-05 16:43:11 +0900 | [diff] [blame] | 4402 | qc->cursg = qc->sg; |
| 4403 | } |
| 4404 | |
Geert Uytterhoeven | 2874d5e | 2017-01-03 19:09:49 +0100 | [diff] [blame] | 4405 | #ifdef CONFIG_HAS_DMA |
| 4406 | |
| 4407 | /** |
| 4408 | * ata_sg_clean - Unmap DMA memory associated with command |
| 4409 | * @qc: Command containing DMA memory to be released |
| 4410 | * |
| 4411 | * Unmap all mapped DMA memory associated with this command. |
| 4412 | * |
| 4413 | * LOCKING: |
| 4414 | * spin_lock_irqsave(host lock) |
| 4415 | */ |
Jason Yan | af27e01 | 2017-03-10 10:05:40 +0800 | [diff] [blame] | 4416 | static void ata_sg_clean(struct ata_queued_cmd *qc) |
Geert Uytterhoeven | 2874d5e | 2017-01-03 19:09:49 +0100 | [diff] [blame] | 4417 | { |
| 4418 | struct ata_port *ap = qc->ap; |
| 4419 | struct scatterlist *sg = qc->sg; |
| 4420 | int dir = qc->dma_dir; |
| 4421 | |
| 4422 | WARN_ON_ONCE(sg == NULL); |
| 4423 | |
| 4424 | VPRINTK("unmapping %u sg elements\n", qc->n_elem); |
| 4425 | |
| 4426 | if (qc->n_elem) |
| 4427 | dma_unmap_sg(ap->dev, sg, qc->orig_n_elem, dir); |
| 4428 | |
| 4429 | qc->flags &= ~ATA_QCFLAG_DMAMAP; |
| 4430 | qc->sg = NULL; |
| 4431 | } |
| 4432 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4433 | /** |
Jeff Garzik | 0cba632 | 2005-05-30 19:49:12 -0400 | [diff] [blame] | 4434 | * ata_sg_setup - DMA-map the scatter-gather table associated with a command. |
| 4435 | * @qc: Command with scatter-gather table to be mapped. |
| 4436 | * |
| 4437 | * DMA-map the scatter-gather table associated with queued_cmd @qc. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4438 | * |
| 4439 | * LOCKING: |
Jeff Garzik | cca3974 | 2006-08-24 03:19:22 -0400 | [diff] [blame] | 4440 | * spin_lock_irqsave(host lock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4441 | * |
| 4442 | * RETURNS: |
Jeff Garzik | 0cba632 | 2005-05-30 19:49:12 -0400 | [diff] [blame] | 4443 | * Zero on success, negative on error. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4444 | * |
| 4445 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4446 | static int ata_sg_setup(struct ata_queued_cmd *qc) |
| 4447 | { |
| 4448 | struct ata_port *ap = qc->ap; |
James Bottomley | dde2020 | 2008-02-19 11:36:56 +0100 | [diff] [blame] | 4449 | unsigned int n_elem; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4450 | |
Tejun Heo | 44877b4 | 2007-02-21 01:06:51 +0900 | [diff] [blame] | 4451 | VPRINTK("ENTER, ata%u\n", ap->print_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4452 | |
James Bottomley | dde2020 | 2008-02-19 11:36:56 +0100 | [diff] [blame] | 4453 | n_elem = dma_map_sg(ap->dev, qc->sg, qc->n_elem, qc->dma_dir); |
| 4454 | if (n_elem < 1) |
| 4455 | return -1; |
Jeff Garzik | cedc9a4 | 2005-10-05 07:13:30 -0400 | [diff] [blame] | 4456 | |
Paul Menzel | bb37667 | 2018-07-08 09:18:21 +0200 | [diff] [blame] | 4457 | VPRINTK("%d sg elements mapped\n", n_elem); |
FUJITA Tomonori | 5825627 | 2009-02-27 17:35:43 +0900 | [diff] [blame] | 4458 | qc->orig_n_elem = qc->n_elem; |
James Bottomley | dde2020 | 2008-02-19 11:36:56 +0100 | [diff] [blame] | 4459 | qc->n_elem = n_elem; |
Tejun Heo | f92a263 | 2007-12-05 16:43:10 +0900 | [diff] [blame] | 4460 | qc->flags |= ATA_QCFLAG_DMAMAP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4461 | |
| 4462 | return 0; |
| 4463 | } |
| 4464 | |
Geert Uytterhoeven | 2874d5e | 2017-01-03 19:09:49 +0100 | [diff] [blame] | 4465 | #else /* !CONFIG_HAS_DMA */ |
| 4466 | |
| 4467 | static inline void ata_sg_clean(struct ata_queued_cmd *qc) {} |
| 4468 | static inline int ata_sg_setup(struct ata_queued_cmd *qc) { return -1; } |
| 4469 | |
| 4470 | #endif /* !CONFIG_HAS_DMA */ |
| 4471 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4472 | /** |
Randy Dunlap | c893a3a | 2006-01-28 13:15:32 -0500 | [diff] [blame] | 4473 | * swap_buf_le16 - swap halves of 16-bit words in place |
Edward Falk | 0baab86 | 2005-06-02 18:17:13 -0400 | [diff] [blame] | 4474 | * @buf: Buffer to swap |
| 4475 | * @buf_words: Number of 16-bit words in buffer. |
| 4476 | * |
| 4477 | * Swap halves of 16-bit words if needed to convert from |
| 4478 | * little-endian byte order to native cpu byte order, or |
| 4479 | * vice-versa. |
| 4480 | * |
| 4481 | * LOCKING: |
Randy Dunlap | 6f0ef4f | 2005-10-25 01:44:30 -0400 | [diff] [blame] | 4482 | * Inherited from caller. |
Edward Falk | 0baab86 | 2005-06-02 18:17:13 -0400 | [diff] [blame] | 4483 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4484 | void swap_buf_le16(u16 *buf, unsigned int buf_words) |
| 4485 | { |
| 4486 | #ifdef __BIG_ENDIAN |
| 4487 | unsigned int i; |
| 4488 | |
| 4489 | for (i = 0; i < buf_words; i++) |
| 4490 | buf[i] = le16_to_cpu(buf[i]); |
| 4491 | #endif /* __BIG_ENDIAN */ |
| 4492 | } |
| 4493 | |
Albert Lee | 6ae4cfb | 2005-08-12 14:15:34 +0800 | [diff] [blame] | 4494 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4495 | * ata_qc_new_init - Request an available ATA command, and initialize it |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4496 | * @dev: Device from whom we request an available command structure |
Randy Dunlap | 38755e8 | 2015-08-09 18:37:36 -0700 | [diff] [blame] | 4497 | * @tag: tag |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4498 | * |
| 4499 | * LOCKING: |
Jeff Garzik | 0cba632 | 2005-05-30 19:49:12 -0400 | [diff] [blame] | 4500 | * None. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4501 | */ |
| 4502 | |
Shaohua Li | 98bd4be | 2015-01-23 19:52:07 -0800 | [diff] [blame] | 4503 | 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] | 4504 | { |
Tejun Heo | 9af5c9c | 2007-08-06 18:36:22 +0900 | [diff] [blame] | 4505 | struct ata_port *ap = dev->link->ap; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4506 | struct ata_queued_cmd *qc; |
| 4507 | |
Shaohua Li | 98bd4be | 2015-01-23 19:52:07 -0800 | [diff] [blame] | 4508 | /* no command while frozen */ |
| 4509 | if (unlikely(ap->pflags & ATA_PFLAG_FROZEN)) |
| 4510 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4511 | |
Shaohua Li | 98bd4be | 2015-01-23 19:52:07 -0800 | [diff] [blame] | 4512 | /* libsas case */ |
Shaohua Li | 5067c04 | 2015-03-12 10:32:18 -0700 | [diff] [blame] | 4513 | if (ap->flags & ATA_FLAG_SAS_HOST) { |
Shaohua Li | 98bd4be | 2015-01-23 19:52:07 -0800 | [diff] [blame] | 4514 | tag = ata_sas_allocate_tag(ap); |
| 4515 | if (tag < 0) |
| 4516 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4517 | } |
| 4518 | |
Shaohua Li | 98bd4be | 2015-01-23 19:52:07 -0800 | [diff] [blame] | 4519 | qc = __ata_qc_from_tag(ap, tag); |
Jens Axboe | 5ac4079 | 2018-05-11 12:51:03 -0600 | [diff] [blame] | 4520 | qc->tag = qc->hw_tag = tag; |
Shaohua Li | 98bd4be | 2015-01-23 19:52:07 -0800 | [diff] [blame] | 4521 | qc->scsicmd = NULL; |
| 4522 | qc->ap = ap; |
| 4523 | qc->dev = dev; |
| 4524 | |
| 4525 | ata_qc_reinit(qc); |
| 4526 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4527 | return qc; |
| 4528 | } |
| 4529 | |
Tejun Heo | 8a8bc22 | 2008-11-10 14:48:21 +0900 | [diff] [blame] | 4530 | /** |
| 4531 | * ata_qc_free - free unused ata_queued_cmd |
| 4532 | * @qc: Command to complete |
| 4533 | * |
| 4534 | * Designed to free unused ata_queued_cmd object |
| 4535 | * in case something prevents using it. |
| 4536 | * |
| 4537 | * LOCKING: |
| 4538 | * spin_lock_irqsave(host lock) |
| 4539 | */ |
| 4540 | void ata_qc_free(struct ata_queued_cmd *qc) |
| 4541 | { |
Julia Lawall | a110401 | 2009-10-17 08:41:47 +0200 | [diff] [blame] | 4542 | struct ata_port *ap; |
Tejun Heo | 8a8bc22 | 2008-11-10 14:48:21 +0900 | [diff] [blame] | 4543 | unsigned int tag; |
| 4544 | |
Tejun Heo | efcb3cf | 2009-01-09 19:19:14 +0900 | [diff] [blame] | 4545 | WARN_ON_ONCE(qc == NULL); /* ata_qc_from_tag _might_ return NULL */ |
Julia Lawall | a110401 | 2009-10-17 08:41:47 +0200 | [diff] [blame] | 4546 | ap = qc->ap; |
Tejun Heo | 8a8bc22 | 2008-11-10 14:48:21 +0900 | [diff] [blame] | 4547 | |
| 4548 | qc->flags = 0; |
| 4549 | tag = qc->tag; |
Jens Axboe | 28361c4 | 2018-05-11 12:51:09 -0600 | [diff] [blame] | 4550 | if (ata_tag_valid(tag)) { |
Tejun Heo | 8a8bc22 | 2008-11-10 14:48:21 +0900 | [diff] [blame] | 4551 | qc->tag = ATA_TAG_POISON; |
Shaohua Li | 5067c04 | 2015-03-12 10:32:18 -0700 | [diff] [blame] | 4552 | if (ap->flags & ATA_FLAG_SAS_HOST) |
Shaohua Li | 98bd4be | 2015-01-23 19:52:07 -0800 | [diff] [blame] | 4553 | ata_sas_free_tag(tag, ap); |
Tejun Heo | 8a8bc22 | 2008-11-10 14:48:21 +0900 | [diff] [blame] | 4554 | } |
| 4555 | } |
| 4556 | |
Tejun Heo | 7601442 | 2006-02-11 15:13:49 +0900 | [diff] [blame] | 4557 | void __ata_qc_complete(struct ata_queued_cmd *qc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4558 | { |
Julia Lawall | a110401 | 2009-10-17 08:41:47 +0200 | [diff] [blame] | 4559 | struct ata_port *ap; |
| 4560 | struct ata_link *link; |
Tejun Heo | dedaf2b | 2006-05-15 21:03:43 +0900 | [diff] [blame] | 4561 | |
Tejun Heo | efcb3cf | 2009-01-09 19:19:14 +0900 | [diff] [blame] | 4562 | WARN_ON_ONCE(qc == NULL); /* ata_qc_from_tag _might_ return NULL */ |
| 4563 | WARN_ON_ONCE(!(qc->flags & ATA_QCFLAG_ACTIVE)); |
Julia Lawall | a110401 | 2009-10-17 08:41:47 +0200 | [diff] [blame] | 4564 | ap = qc->ap; |
| 4565 | link = qc->dev->link; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4566 | |
| 4567 | if (likely(qc->flags & ATA_QCFLAG_DMAMAP)) |
| 4568 | ata_sg_clean(qc); |
| 4569 | |
Tejun Heo | 7401abf | 2006-05-15 20:57:32 +0900 | [diff] [blame] | 4570 | /* command should be marked inactive atomically with qc completion */ |
Hannes Reinecke | 179b310 | 2016-07-14 09:05:43 +0900 | [diff] [blame] | 4571 | if (ata_is_ncq(qc->tf.protocol)) { |
Jens Axboe | 4e5b626 | 2018-05-11 12:51:04 -0600 | [diff] [blame] | 4572 | link->sactive &= ~(1 << qc->hw_tag); |
Tejun Heo | da917d6 | 2007-09-23 13:14:12 +0900 | [diff] [blame] | 4573 | if (!link->sactive) |
| 4574 | ap->nr_active_links--; |
| 4575 | } else { |
Tejun Heo | 9af5c9c | 2007-08-06 18:36:22 +0900 | [diff] [blame] | 4576 | link->active_tag = ATA_TAG_POISON; |
Tejun Heo | da917d6 | 2007-09-23 13:14:12 +0900 | [diff] [blame] | 4577 | ap->nr_active_links--; |
| 4578 | } |
| 4579 | |
| 4580 | /* clear exclusive status */ |
| 4581 | if (unlikely(qc->flags & ATA_QCFLAG_CLEAR_EXCL && |
| 4582 | ap->excl_link == link)) |
| 4583 | ap->excl_link = NULL; |
Tejun Heo | 7401abf | 2006-05-15 20:57:32 +0900 | [diff] [blame] | 4584 | |
Albert Lee | 3f3791d | 2005-08-16 14:25:38 +0800 | [diff] [blame] | 4585 | /* atapi: mark qc as inactive to prevent the interrupt handler |
| 4586 | * from completing the command twice later, before the error handler |
| 4587 | * is called. (when rc != 0 and atapi request sense is needed) |
| 4588 | */ |
| 4589 | qc->flags &= ~ATA_QCFLAG_ACTIVE; |
Jens Axboe | e3ed8939 | 2018-05-11 12:51:05 -0600 | [diff] [blame] | 4590 | ap->qc_active &= ~(1ULL << qc->tag); |
Albert Lee | 3f3791d | 2005-08-16 14:25:38 +0800 | [diff] [blame] | 4591 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4592 | /* call completion callback */ |
Tejun Heo | 77853bf | 2006-01-23 13:09:36 +0900 | [diff] [blame] | 4593 | qc->complete_fn(qc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4594 | } |
| 4595 | |
Tejun Heo | 39599a5 | 2006-11-14 22:37:35 +0900 | [diff] [blame] | 4596 | static void fill_result_tf(struct ata_queued_cmd *qc) |
| 4597 | { |
| 4598 | struct ata_port *ap = qc->ap; |
| 4599 | |
Tejun Heo | 39599a5 | 2006-11-14 22:37:35 +0900 | [diff] [blame] | 4600 | qc->result_tf.flags = qc->tf.flags; |
Tejun Heo | 22183bf | 2008-04-07 22:47:20 +0900 | [diff] [blame] | 4601 | ap->ops->qc_fill_rtf(qc); |
Tejun Heo | 39599a5 | 2006-11-14 22:37:35 +0900 | [diff] [blame] | 4602 | } |
| 4603 | |
Tejun Heo | 00115e0 | 2007-11-27 19:28:58 +0900 | [diff] [blame] | 4604 | static void ata_verify_xfer(struct ata_queued_cmd *qc) |
| 4605 | { |
| 4606 | struct ata_device *dev = qc->dev; |
| 4607 | |
Christoph Hellwig | eb0effd | 2016-07-16 22:16:41 +0900 | [diff] [blame] | 4608 | if (!ata_is_data(qc->tf.protocol)) |
Tejun Heo | 00115e0 | 2007-11-27 19:28:58 +0900 | [diff] [blame] | 4609 | return; |
| 4610 | |
| 4611 | if ((dev->mwdma_mask || dev->udma_mask) && ata_is_pio(qc->tf.protocol)) |
| 4612 | return; |
| 4613 | |
| 4614 | dev->flags &= ~ATA_DFLAG_DUBIOUS_XFER; |
| 4615 | } |
| 4616 | |
Tejun Heo | f686bcb | 2006-05-15 20:58:05 +0900 | [diff] [blame] | 4617 | /** |
| 4618 | * ata_qc_complete - Complete an active ATA command |
| 4619 | * @qc: Command to complete |
Tejun Heo | f686bcb | 2006-05-15 20:58:05 +0900 | [diff] [blame] | 4620 | * |
Tejun Heo | 1aadf5c | 2010-06-25 15:03:34 +0200 | [diff] [blame] | 4621 | * Indicate to the mid and upper layers that an ATA command has |
| 4622 | * completed, with either an ok or not-ok status. |
| 4623 | * |
| 4624 | * Refrain from calling this function multiple times when |
| 4625 | * successfully completing multiple NCQ commands. |
| 4626 | * ata_qc_complete_multiple() should be used instead, which will |
| 4627 | * properly update IRQ expect state. |
Tejun Heo | f686bcb | 2006-05-15 20:58:05 +0900 | [diff] [blame] | 4628 | * |
| 4629 | * LOCKING: |
Jeff Garzik | cca3974 | 2006-08-24 03:19:22 -0400 | [diff] [blame] | 4630 | * spin_lock_irqsave(host lock) |
Tejun Heo | f686bcb | 2006-05-15 20:58:05 +0900 | [diff] [blame] | 4631 | */ |
| 4632 | void ata_qc_complete(struct ata_queued_cmd *qc) |
| 4633 | { |
| 4634 | struct ata_port *ap = qc->ap; |
| 4635 | |
Stephan Linz | eb25cb9 | 2016-06-10 07:59:56 +0200 | [diff] [blame] | 4636 | /* Trigger the LED (if available) */ |
Linus Walleij | d1ed7c5 | 2018-02-24 23:45:56 +0100 | [diff] [blame] | 4637 | ledtrig_disk_activity(!!(qc->tf.flags & ATA_TFLAG_WRITE)); |
Stephan Linz | eb25cb9 | 2016-06-10 07:59:56 +0200 | [diff] [blame] | 4638 | |
Tejun Heo | f686bcb | 2006-05-15 20:58:05 +0900 | [diff] [blame] | 4639 | /* XXX: New EH and old EH use different mechanisms to |
| 4640 | * synchronize EH with regular execution path. |
| 4641 | * |
| 4642 | * In new EH, a failed qc is marked with ATA_QCFLAG_FAILED. |
| 4643 | * Normal execution path is responsible for not accessing a |
| 4644 | * failed qc. libata core enforces the rule by returning NULL |
| 4645 | * from ata_qc_from_tag() for failed qcs. |
| 4646 | * |
| 4647 | * Old EH depends on ata_qc_complete() nullifying completion |
| 4648 | * requests if ATA_QCFLAG_EH_SCHEDULED is set. Old EH does |
| 4649 | * not synchronize with interrupt handler. Only PIO task is |
| 4650 | * taken care of. |
| 4651 | */ |
| 4652 | if (ap->ops->error_handler) { |
Tejun Heo | 4dbfa39 | 2007-10-25 18:22:44 +0900 | [diff] [blame] | 4653 | struct ata_device *dev = qc->dev; |
| 4654 | struct ata_eh_info *ehi = &dev->link->eh_info; |
| 4655 | |
Tejun Heo | f686bcb | 2006-05-15 20:58:05 +0900 | [diff] [blame] | 4656 | if (unlikely(qc->err_mask)) |
| 4657 | qc->flags |= ATA_QCFLAG_FAILED; |
| 4658 | |
Tejun Heo | f08dc1a | 2010-12-09 15:59:32 +0100 | [diff] [blame] | 4659 | /* |
| 4660 | * Finish internal commands without any further processing |
| 4661 | * and always with the result TF filled. |
| 4662 | */ |
| 4663 | if (unlikely(ata_tag_internal(qc->tag))) { |
Tejun Heo | f4b31db9 | 2009-10-16 13:00:51 +0900 | [diff] [blame] | 4664 | fill_result_tf(qc); |
Hannes Reinecke | 255c03d | 2015-03-27 16:46:38 +0100 | [diff] [blame] | 4665 | trace_ata_qc_complete_internal(qc); |
Tejun Heo | f08dc1a | 2010-12-09 15:59:32 +0100 | [diff] [blame] | 4666 | __ata_qc_complete(qc); |
| 4667 | return; |
| 4668 | } |
Tejun Heo | f4b31db9 | 2009-10-16 13:00:51 +0900 | [diff] [blame] | 4669 | |
Tejun Heo | f08dc1a | 2010-12-09 15:59:32 +0100 | [diff] [blame] | 4670 | /* |
| 4671 | * Non-internal qc has failed. Fill the result TF and |
| 4672 | * summon EH. |
| 4673 | */ |
| 4674 | if (unlikely(qc->flags & ATA_QCFLAG_FAILED)) { |
| 4675 | fill_result_tf(qc); |
Hannes Reinecke | 255c03d | 2015-03-27 16:46:38 +0100 | [diff] [blame] | 4676 | trace_ata_qc_complete_failed(qc); |
Tejun Heo | f08dc1a | 2010-12-09 15:59:32 +0100 | [diff] [blame] | 4677 | ata_qc_schedule_eh(qc); |
Tejun Heo | f4b31db9 | 2009-10-16 13:00:51 +0900 | [diff] [blame] | 4678 | return; |
Tejun Heo | f686bcb | 2006-05-15 20:58:05 +0900 | [diff] [blame] | 4679 | } |
| 4680 | |
Tejun Heo | 4dc738e | 2009-09-17 18:45:27 +0900 | [diff] [blame] | 4681 | WARN_ON_ONCE(ap->pflags & ATA_PFLAG_FROZEN); |
| 4682 | |
Tejun Heo | f686bcb | 2006-05-15 20:58:05 +0900 | [diff] [blame] | 4683 | /* read result TF if requested */ |
| 4684 | if (qc->flags & ATA_QCFLAG_RESULT_TF) |
Tejun Heo | 39599a5 | 2006-11-14 22:37:35 +0900 | [diff] [blame] | 4685 | fill_result_tf(qc); |
Tejun Heo | f686bcb | 2006-05-15 20:58:05 +0900 | [diff] [blame] | 4686 | |
Hannes Reinecke | 255c03d | 2015-03-27 16:46:38 +0100 | [diff] [blame] | 4687 | trace_ata_qc_complete_done(qc); |
Tejun Heo | 4dbfa39 | 2007-10-25 18:22:44 +0900 | [diff] [blame] | 4688 | /* Some commands need post-processing after successful |
| 4689 | * completion. |
| 4690 | */ |
| 4691 | switch (qc->tf.command) { |
| 4692 | case ATA_CMD_SET_FEATURES: |
| 4693 | if (qc->tf.feature != SETFEATURES_WC_ON && |
Tom Yan | 0c12735 | 2016-07-12 21:37:02 +0800 | [diff] [blame] | 4694 | qc->tf.feature != SETFEATURES_WC_OFF && |
| 4695 | qc->tf.feature != SETFEATURES_RA_ON && |
| 4696 | qc->tf.feature != SETFEATURES_RA_OFF) |
Tejun Heo | 4dbfa39 | 2007-10-25 18:22:44 +0900 | [diff] [blame] | 4697 | break; |
| 4698 | /* fall through */ |
| 4699 | case ATA_CMD_INIT_DEV_PARAMS: /* CHS translation changed */ |
| 4700 | case ATA_CMD_SET_MULTI: /* multi_count changed */ |
| 4701 | /* revalidate device */ |
| 4702 | ehi->dev_action[dev->devno] |= ATA_EH_REVALIDATE; |
| 4703 | ata_port_schedule_eh(ap); |
| 4704 | break; |
Tejun Heo | 054a5fb | 2007-10-25 18:30:36 +0900 | [diff] [blame] | 4705 | |
| 4706 | case ATA_CMD_SLEEP: |
| 4707 | dev->flags |= ATA_DFLAG_SLEEPING; |
| 4708 | break; |
Tejun Heo | 4dbfa39 | 2007-10-25 18:22:44 +0900 | [diff] [blame] | 4709 | } |
| 4710 | |
Tejun Heo | 00115e0 | 2007-11-27 19:28:58 +0900 | [diff] [blame] | 4711 | if (unlikely(dev->flags & ATA_DFLAG_DUBIOUS_XFER)) |
| 4712 | ata_verify_xfer(qc); |
| 4713 | |
Tejun Heo | f686bcb | 2006-05-15 20:58:05 +0900 | [diff] [blame] | 4714 | __ata_qc_complete(qc); |
| 4715 | } else { |
| 4716 | if (qc->flags & ATA_QCFLAG_EH_SCHEDULED) |
| 4717 | return; |
| 4718 | |
| 4719 | /* read result TF if failed or requested */ |
| 4720 | if (qc->err_mask || qc->flags & ATA_QCFLAG_RESULT_TF) |
Tejun Heo | 39599a5 | 2006-11-14 22:37:35 +0900 | [diff] [blame] | 4721 | fill_result_tf(qc); |
Tejun Heo | f686bcb | 2006-05-15 20:58:05 +0900 | [diff] [blame] | 4722 | |
| 4723 | __ata_qc_complete(qc); |
| 4724 | } |
| 4725 | } |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 4726 | EXPORT_SYMBOL_GPL(ata_qc_complete); |
Tejun Heo | f686bcb | 2006-05-15 20:58:05 +0900 | [diff] [blame] | 4727 | |
Tejun Heo | dedaf2b | 2006-05-15 21:03:43 +0900 | [diff] [blame] | 4728 | /** |
Sascha Hauer | 8385d75 | 2019-12-13 09:04:08 +0100 | [diff] [blame] | 4729 | * ata_qc_get_active - get bitmask of active qcs |
| 4730 | * @ap: port in question |
| 4731 | * |
| 4732 | * LOCKING: |
| 4733 | * spin_lock_irqsave(host lock) |
| 4734 | * |
| 4735 | * RETURNS: |
| 4736 | * Bitmask of active qcs |
| 4737 | */ |
| 4738 | u64 ata_qc_get_active(struct ata_port *ap) |
| 4739 | { |
| 4740 | u64 qc_active = ap->qc_active; |
| 4741 | |
| 4742 | /* ATA_TAG_INTERNAL is sent to hw as tag 0 */ |
| 4743 | if (qc_active & (1ULL << ATA_TAG_INTERNAL)) { |
| 4744 | qc_active |= (1 << 0); |
| 4745 | qc_active &= ~(1ULL << ATA_TAG_INTERNAL); |
| 4746 | } |
| 4747 | |
| 4748 | return qc_active; |
| 4749 | } |
| 4750 | EXPORT_SYMBOL_GPL(ata_qc_get_active); |
| 4751 | |
| 4752 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4753 | * ata_qc_issue - issue taskfile to device |
| 4754 | * @qc: command to issue to device |
| 4755 | * |
| 4756 | * Prepare an ATA command to submission to device. |
| 4757 | * This includes mapping the data into a DMA-able |
| 4758 | * area, filling in the S/G table, and finally |
| 4759 | * writing the taskfile to hardware, starting the command. |
| 4760 | * |
| 4761 | * LOCKING: |
Jeff Garzik | cca3974 | 2006-08-24 03:19:22 -0400 | [diff] [blame] | 4762 | * spin_lock_irqsave(host lock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4763 | */ |
Tejun Heo | 8e0e694 | 2006-03-31 20:41:11 +0900 | [diff] [blame] | 4764 | void ata_qc_issue(struct ata_queued_cmd *qc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4765 | { |
| 4766 | struct ata_port *ap = qc->ap; |
Tejun Heo | 9af5c9c | 2007-08-06 18:36:22 +0900 | [diff] [blame] | 4767 | struct ata_link *link = qc->dev->link; |
Tejun Heo | 405e66b | 2007-11-27 19:28:53 +0900 | [diff] [blame] | 4768 | u8 prot = qc->tf.protocol; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4769 | |
Tejun Heo | dedaf2b | 2006-05-15 21:03:43 +0900 | [diff] [blame] | 4770 | /* Make sure only one non-NCQ command is outstanding. The |
| 4771 | * check is skipped for old EH because it reuses active qc to |
| 4772 | * request ATAPI sense. |
| 4773 | */ |
Tejun Heo | efcb3cf | 2009-01-09 19:19:14 +0900 | [diff] [blame] | 4774 | 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] | 4775 | |
Tejun Heo | 1973a02 | 2007-12-05 10:36:13 +0900 | [diff] [blame] | 4776 | if (ata_is_ncq(prot)) { |
Jens Axboe | 4e5b626 | 2018-05-11 12:51:04 -0600 | [diff] [blame] | 4777 | WARN_ON_ONCE(link->sactive & (1 << qc->hw_tag)); |
Tejun Heo | da917d6 | 2007-09-23 13:14:12 +0900 | [diff] [blame] | 4778 | |
| 4779 | if (!link->sactive) |
| 4780 | ap->nr_active_links++; |
Jens Axboe | 4e5b626 | 2018-05-11 12:51:04 -0600 | [diff] [blame] | 4781 | link->sactive |= 1 << qc->hw_tag; |
Tejun Heo | dedaf2b | 2006-05-15 21:03:43 +0900 | [diff] [blame] | 4782 | } else { |
Tejun Heo | efcb3cf | 2009-01-09 19:19:14 +0900 | [diff] [blame] | 4783 | WARN_ON_ONCE(link->sactive); |
Tejun Heo | da917d6 | 2007-09-23 13:14:12 +0900 | [diff] [blame] | 4784 | |
| 4785 | ap->nr_active_links++; |
Tejun Heo | 9af5c9c | 2007-08-06 18:36:22 +0900 | [diff] [blame] | 4786 | link->active_tag = qc->tag; |
Tejun Heo | dedaf2b | 2006-05-15 21:03:43 +0900 | [diff] [blame] | 4787 | } |
| 4788 | |
Tejun Heo | e4a70e7 | 2006-03-31 20:36:47 +0900 | [diff] [blame] | 4789 | qc->flags |= ATA_QCFLAG_ACTIVE; |
Jens Axboe | e3ed8939 | 2018-05-11 12:51:05 -0600 | [diff] [blame] | 4790 | ap->qc_active |= 1ULL << qc->tag; |
Tejun Heo | e4a70e7 | 2006-03-31 20:36:47 +0900 | [diff] [blame] | 4791 | |
Tejun Heo | 60f5d6e | 2010-08-23 11:27:27 +0200 | [diff] [blame] | 4792 | /* |
| 4793 | * We guarantee to LLDs that they will have at least one |
Tejun Heo | f92a263 | 2007-12-05 16:43:10 +0900 | [diff] [blame] | 4794 | * non-zero sg if the command is a data command. |
| 4795 | */ |
Eric Biggers | 9173e5e | 2018-02-03 20:33:27 -0800 | [diff] [blame] | 4796 | if (ata_is_data(prot) && (!qc->sg || !qc->n_elem || !qc->nbytes)) |
Tejun Heo | 60f5d6e | 2010-08-23 11:27:27 +0200 | [diff] [blame] | 4797 | goto sys_err; |
Tejun Heo | f92a263 | 2007-12-05 16:43:10 +0900 | [diff] [blame] | 4798 | |
Tejun Heo | 405e66b | 2007-11-27 19:28:53 +0900 | [diff] [blame] | 4799 | if (ata_is_dma(prot) || (ata_is_pio(prot) && |
Tejun Heo | f92a263 | 2007-12-05 16:43:10 +0900 | [diff] [blame] | 4800 | (ap->flags & ATA_FLAG_PIO_DMA))) |
Tejun Heo | 001102d | 2007-12-05 16:43:09 +0900 | [diff] [blame] | 4801 | if (ata_sg_setup(qc)) |
Tejun Heo | 60f5d6e | 2010-08-23 11:27:27 +0200 | [diff] [blame] | 4802 | goto sys_err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4803 | |
Tejun Heo | cf48062 | 2008-01-24 00:05:14 +0900 | [diff] [blame] | 4804 | /* if device is sleeping, schedule reset and abort the link */ |
Tejun Heo | 054a5fb | 2007-10-25 18:30:36 +0900 | [diff] [blame] | 4805 | if (unlikely(qc->dev->flags & ATA_DFLAG_SLEEPING)) { |
Tejun Heo | cf48062 | 2008-01-24 00:05:14 +0900 | [diff] [blame] | 4806 | link->eh_info.action |= ATA_EH_RESET; |
Tejun Heo | 054a5fb | 2007-10-25 18:30:36 +0900 | [diff] [blame] | 4807 | ata_ehi_push_desc(&link->eh_info, "waking up from sleep"); |
| 4808 | ata_link_abort(link); |
| 4809 | return; |
| 4810 | } |
| 4811 | |
Jiri Slaby | 95364f3 | 2019-10-31 10:59:45 +0100 | [diff] [blame] | 4812 | qc->err_mask |= ap->ops->qc_prep(qc); |
| 4813 | if (unlikely(qc->err_mask)) |
| 4814 | goto err; |
Hannes Reinecke | 255c03d | 2015-03-27 16:46:38 +0100 | [diff] [blame] | 4815 | trace_ata_qc_issue(qc); |
Tejun Heo | 8e0e694 | 2006-03-31 20:41:11 +0900 | [diff] [blame] | 4816 | qc->err_mask |= ap->ops->qc_issue(qc); |
| 4817 | if (unlikely(qc->err_mask)) |
| 4818 | goto err; |
| 4819 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4820 | |
Tejun Heo | 60f5d6e | 2010-08-23 11:27:27 +0200 | [diff] [blame] | 4821 | sys_err: |
Tejun Heo | 8e0e694 | 2006-03-31 20:41:11 +0900 | [diff] [blame] | 4822 | qc->err_mask |= AC_ERR_SYSTEM; |
| 4823 | err: |
| 4824 | ata_qc_complete(qc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4825 | } |
| 4826 | |
| 4827 | /** |
Tejun Heo | b1c7291 | 2008-07-31 17:02:43 +0900 | [diff] [blame] | 4828 | * ata_phys_link_online - test whether the given link is online |
Tejun Heo | 936fd73 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 4829 | * @link: ATA link to test |
Tejun Heo | 34bf217 | 2006-05-15 20:57:46 +0900 | [diff] [blame] | 4830 | * |
Tejun Heo | 936fd73 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 4831 | * Test whether @link is online. Note that this function returns |
| 4832 | * 0 if online status of @link cannot be obtained, so |
| 4833 | * ata_link_online(link) != !ata_link_offline(link). |
Tejun Heo | 34bf217 | 2006-05-15 20:57:46 +0900 | [diff] [blame] | 4834 | * |
| 4835 | * LOCKING: |
| 4836 | * None. |
| 4837 | * |
| 4838 | * RETURNS: |
Tejun Heo | b5b3fa3 | 2008-07-31 17:02:42 +0900 | [diff] [blame] | 4839 | * True if the port online status is available and online. |
Tejun Heo | 34bf217 | 2006-05-15 20:57:46 +0900 | [diff] [blame] | 4840 | */ |
Tejun Heo | b1c7291 | 2008-07-31 17:02:43 +0900 | [diff] [blame] | 4841 | bool ata_phys_link_online(struct ata_link *link) |
Tejun Heo | 34bf217 | 2006-05-15 20:57:46 +0900 | [diff] [blame] | 4842 | { |
| 4843 | u32 sstatus; |
| 4844 | |
Tejun Heo | 936fd73 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 4845 | if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0 && |
Tejun Heo | 9913ff8a | 2009-01-29 20:31:31 +0900 | [diff] [blame] | 4846 | ata_sstatus_online(sstatus)) |
Tejun Heo | b5b3fa3 | 2008-07-31 17:02:42 +0900 | [diff] [blame] | 4847 | return true; |
| 4848 | return false; |
Tejun Heo | 34bf217 | 2006-05-15 20:57:46 +0900 | [diff] [blame] | 4849 | } |
| 4850 | |
| 4851 | /** |
Tejun Heo | b1c7291 | 2008-07-31 17:02:43 +0900 | [diff] [blame] | 4852 | * ata_phys_link_offline - test whether the given link is offline |
Tejun Heo | 936fd73 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 4853 | * @link: ATA link to test |
Tejun Heo | 34bf217 | 2006-05-15 20:57:46 +0900 | [diff] [blame] | 4854 | * |
Tejun Heo | 936fd73 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 4855 | * Test whether @link is offline. Note that this function |
| 4856 | * returns 0 if offline status of @link cannot be obtained, so |
| 4857 | * ata_link_online(link) != !ata_link_offline(link). |
Tejun Heo | 34bf217 | 2006-05-15 20:57:46 +0900 | [diff] [blame] | 4858 | * |
| 4859 | * LOCKING: |
| 4860 | * None. |
| 4861 | * |
| 4862 | * RETURNS: |
Tejun Heo | b5b3fa3 | 2008-07-31 17:02:42 +0900 | [diff] [blame] | 4863 | * True if the port offline status is available and offline. |
Tejun Heo | 34bf217 | 2006-05-15 20:57:46 +0900 | [diff] [blame] | 4864 | */ |
Tejun Heo | b1c7291 | 2008-07-31 17:02:43 +0900 | [diff] [blame] | 4865 | bool ata_phys_link_offline(struct ata_link *link) |
Tejun Heo | 34bf217 | 2006-05-15 20:57:46 +0900 | [diff] [blame] | 4866 | { |
| 4867 | u32 sstatus; |
| 4868 | |
Tejun Heo | 936fd73 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 4869 | if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0 && |
Tejun Heo | 9913ff8a | 2009-01-29 20:31:31 +0900 | [diff] [blame] | 4870 | !ata_sstatus_online(sstatus)) |
Tejun Heo | b5b3fa3 | 2008-07-31 17:02:42 +0900 | [diff] [blame] | 4871 | return true; |
| 4872 | return false; |
Tejun Heo | 34bf217 | 2006-05-15 20:57:46 +0900 | [diff] [blame] | 4873 | } |
Edward Falk | 0baab86 | 2005-06-02 18:17:13 -0400 | [diff] [blame] | 4874 | |
Tejun Heo | b1c7291 | 2008-07-31 17:02:43 +0900 | [diff] [blame] | 4875 | /** |
| 4876 | * ata_link_online - test whether the given link is online |
| 4877 | * @link: ATA link to test |
| 4878 | * |
| 4879 | * Test whether @link is online. This is identical to |
| 4880 | * ata_phys_link_online() when there's no slave link. When |
| 4881 | * there's a slave link, this function should only be called on |
| 4882 | * the master link and will return true if any of M/S links is |
| 4883 | * online. |
| 4884 | * |
| 4885 | * LOCKING: |
| 4886 | * None. |
| 4887 | * |
| 4888 | * RETURNS: |
| 4889 | * True if the port online status is available and online. |
| 4890 | */ |
| 4891 | bool ata_link_online(struct ata_link *link) |
| 4892 | { |
| 4893 | struct ata_link *slave = link->ap->slave_link; |
| 4894 | |
| 4895 | WARN_ON(link == slave); /* shouldn't be called on slave link */ |
| 4896 | |
| 4897 | return ata_phys_link_online(link) || |
| 4898 | (slave && ata_phys_link_online(slave)); |
| 4899 | } |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 4900 | EXPORT_SYMBOL_GPL(ata_link_online); |
Tejun Heo | b1c7291 | 2008-07-31 17:02:43 +0900 | [diff] [blame] | 4901 | |
| 4902 | /** |
| 4903 | * ata_link_offline - test whether the given link is offline |
| 4904 | * @link: ATA link to test |
| 4905 | * |
| 4906 | * Test whether @link is offline. This is identical to |
| 4907 | * ata_phys_link_offline() when there's no slave link. When |
| 4908 | * there's a slave link, this function should only be called on |
| 4909 | * the master link and will return true if both M/S links are |
| 4910 | * offline. |
| 4911 | * |
| 4912 | * LOCKING: |
| 4913 | * None. |
| 4914 | * |
| 4915 | * RETURNS: |
| 4916 | * True if the port offline status is available and offline. |
| 4917 | */ |
| 4918 | bool ata_link_offline(struct ata_link *link) |
| 4919 | { |
| 4920 | struct ata_link *slave = link->ap->slave_link; |
| 4921 | |
| 4922 | WARN_ON(link == slave); /* shouldn't be called on slave link */ |
| 4923 | |
| 4924 | return ata_phys_link_offline(link) && |
| 4925 | (!slave || ata_phys_link_offline(slave)); |
| 4926 | } |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 4927 | EXPORT_SYMBOL_GPL(ata_link_offline); |
Tejun Heo | b1c7291 | 2008-07-31 17:02:43 +0900 | [diff] [blame] | 4928 | |
Tejun Heo | 6ffa01d | 2007-03-02 17:32:47 +0900 | [diff] [blame] | 4929 | #ifdef CONFIG_PM |
Dan Williams | bc6e7c4 | 2014-03-14 13:52:48 -0700 | [diff] [blame] | 4930 | static void ata_port_request_pm(struct ata_port *ap, pm_message_t mesg, |
| 4931 | unsigned int action, unsigned int ehi_flags, |
| 4932 | bool async) |
Tejun Heo | 500530f | 2006-07-03 16:07:27 +0900 | [diff] [blame] | 4933 | { |
Lin Ming | 5ef4108 | 2011-12-05 09:20:27 +0800 | [diff] [blame] | 4934 | struct ata_link *link; |
Tejun Heo | 500530f | 2006-07-03 16:07:27 +0900 | [diff] [blame] | 4935 | unsigned long flags; |
Tejun Heo | 500530f | 2006-07-03 16:07:27 +0900 | [diff] [blame] | 4936 | |
Lin Ming | 5ef4108 | 2011-12-05 09:20:27 +0800 | [diff] [blame] | 4937 | /* Previous resume operation might still be in |
| 4938 | * progress. Wait for PM_PENDING to clear. |
| 4939 | */ |
| 4940 | if (ap->pflags & ATA_PFLAG_PM_PENDING) { |
| 4941 | ata_port_wait_eh(ap); |
| 4942 | WARN_ON(ap->pflags & ATA_PFLAG_PM_PENDING); |
Tejun Heo | 500530f | 2006-07-03 16:07:27 +0900 | [diff] [blame] | 4943 | } |
| 4944 | |
Lin Ming | 5ef4108 | 2011-12-05 09:20:27 +0800 | [diff] [blame] | 4945 | /* request PM ops to EH */ |
| 4946 | spin_lock_irqsave(ap->lock, flags); |
| 4947 | |
| 4948 | ap->pm_mesg = mesg; |
Lin Ming | 5ef4108 | 2011-12-05 09:20:27 +0800 | [diff] [blame] | 4949 | ap->pflags |= ATA_PFLAG_PM_PENDING; |
| 4950 | ata_for_each_link(link, ap, HOST_FIRST) { |
| 4951 | link->eh_info.action |= action; |
| 4952 | link->eh_info.flags |= ehi_flags; |
| 4953 | } |
| 4954 | |
| 4955 | ata_port_schedule_eh(ap); |
| 4956 | |
| 4957 | spin_unlock_irqrestore(ap->lock, flags); |
| 4958 | |
Dan Williams | 2fcbdcb | 2012-06-21 23:41:46 -0700 | [diff] [blame] | 4959 | if (!async) { |
Lin Ming | 5ef4108 | 2011-12-05 09:20:27 +0800 | [diff] [blame] | 4960 | ata_port_wait_eh(ap); |
| 4961 | WARN_ON(ap->pflags & ATA_PFLAG_PM_PENDING); |
| 4962 | } |
Tejun Heo | 500530f | 2006-07-03 16:07:27 +0900 | [diff] [blame] | 4963 | } |
| 4964 | |
Dan Williams | bc6e7c4 | 2014-03-14 13:52:48 -0700 | [diff] [blame] | 4965 | /* |
| 4966 | * On some hardware, device fails to respond after spun down for suspend. As |
| 4967 | * the device won't be used before being resumed, we don't need to touch the |
| 4968 | * device. Ask EH to skip the usual stuff and proceed directly to suspend. |
| 4969 | * |
| 4970 | * http://thread.gmane.org/gmane.linux.ide/46764 |
| 4971 | */ |
| 4972 | static const unsigned int ata_port_suspend_ehi = ATA_EHI_QUIET |
| 4973 | | ATA_EHI_NO_AUTOPSY |
| 4974 | | ATA_EHI_NO_RECOVERY; |
| 4975 | |
| 4976 | 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] | 4977 | { |
Dan Williams | bc6e7c4 | 2014-03-14 13:52:48 -0700 | [diff] [blame] | 4978 | ata_port_request_pm(ap, mesg, 0, ata_port_suspend_ehi, false); |
Lin Ming | 5ef4108 | 2011-12-05 09:20:27 +0800 | [diff] [blame] | 4979 | } |
| 4980 | |
Dan Williams | bc6e7c4 | 2014-03-14 13:52:48 -0700 | [diff] [blame] | 4981 | static void ata_port_suspend_async(struct ata_port *ap, pm_message_t mesg) |
| 4982 | { |
| 4983 | ata_port_request_pm(ap, mesg, 0, ata_port_suspend_ehi, true); |
| 4984 | } |
| 4985 | |
| 4986 | static int ata_port_pm_suspend(struct device *dev) |
Dan Williams | 2fcbdcb | 2012-06-21 23:41:46 -0700 | [diff] [blame] | 4987 | { |
| 4988 | struct ata_port *ap = to_ata_port(dev); |
| 4989 | |
Lin Ming | 5ef4108 | 2011-12-05 09:20:27 +0800 | [diff] [blame] | 4990 | if (pm_runtime_suspended(dev)) |
| 4991 | return 0; |
| 4992 | |
Dan Williams | bc6e7c4 | 2014-03-14 13:52:48 -0700 | [diff] [blame] | 4993 | ata_port_suspend(ap, PMSG_SUSPEND); |
| 4994 | return 0; |
Lin Ming | 33574d6 | 2011-12-22 14:50:49 +0800 | [diff] [blame] | 4995 | } |
| 4996 | |
Dan Williams | bc6e7c4 | 2014-03-14 13:52:48 -0700 | [diff] [blame] | 4997 | static int ata_port_pm_freeze(struct device *dev) |
Dan Williams | 2fcbdcb | 2012-06-21 23:41:46 -0700 | [diff] [blame] | 4998 | { |
| 4999 | struct ata_port *ap = to_ata_port(dev); |
| 5000 | |
Dan Williams | bc6e7c4 | 2014-03-14 13:52:48 -0700 | [diff] [blame] | 5001 | if (pm_runtime_suspended(dev)) |
| 5002 | return 0; |
| 5003 | |
| 5004 | ata_port_suspend(ap, PMSG_FREEZE); |
| 5005 | return 0; |
Dan Williams | 2fcbdcb | 2012-06-21 23:41:46 -0700 | [diff] [blame] | 5006 | } |
| 5007 | |
Dan Williams | bc6e7c4 | 2014-03-14 13:52:48 -0700 | [diff] [blame] | 5008 | static int ata_port_pm_poweroff(struct device *dev) |
Lin Ming | e90b1e5 | 2011-12-22 14:50:48 +0800 | [diff] [blame] | 5009 | { |
Dan Williams | bc6e7c4 | 2014-03-14 13:52:48 -0700 | [diff] [blame] | 5010 | ata_port_suspend(to_ata_port(dev), PMSG_HIBERNATE); |
| 5011 | return 0; |
| 5012 | } |
Lin Ming | e90b1e5 | 2011-12-22 14:50:48 +0800 | [diff] [blame] | 5013 | |
Dan Williams | bc6e7c4 | 2014-03-14 13:52:48 -0700 | [diff] [blame] | 5014 | static const unsigned int ata_port_resume_ehi = ATA_EHI_NO_AUTOPSY |
| 5015 | | ATA_EHI_QUIET; |
Lin Ming | e90b1e5 | 2011-12-22 14:50:48 +0800 | [diff] [blame] | 5016 | |
Dan Williams | bc6e7c4 | 2014-03-14 13:52:48 -0700 | [diff] [blame] | 5017 | static void ata_port_resume(struct ata_port *ap, pm_message_t mesg) |
| 5018 | { |
| 5019 | ata_port_request_pm(ap, mesg, ATA_EH_RESET, ata_port_resume_ehi, false); |
| 5020 | } |
| 5021 | |
| 5022 | static void ata_port_resume_async(struct ata_port *ap, pm_message_t mesg) |
| 5023 | { |
| 5024 | ata_port_request_pm(ap, mesg, ATA_EH_RESET, ata_port_resume_ehi, true); |
| 5025 | } |
| 5026 | |
| 5027 | static int ata_port_pm_resume(struct device *dev) |
| 5028 | { |
Todd Brandt | 200421a | 2014-03-14 13:52:54 -0700 | [diff] [blame] | 5029 | ata_port_resume_async(to_ata_port(dev), PMSG_RESUME); |
Dan Williams | bc6e7c4 | 2014-03-14 13:52:48 -0700 | [diff] [blame] | 5030 | pm_runtime_disable(dev); |
| 5031 | pm_runtime_set_active(dev); |
| 5032 | pm_runtime_enable(dev); |
| 5033 | return 0; |
Lin Ming | e90b1e5 | 2011-12-22 14:50:48 +0800 | [diff] [blame] | 5034 | } |
| 5035 | |
Aaron Lu | 7e15e9b | 2013-01-15 17:21:04 +0800 | [diff] [blame] | 5036 | /* |
| 5037 | * For ODDs, the upper layer will poll for media change every few seconds, |
| 5038 | * which will make it enter and leave suspend state every few seconds. And |
| 5039 | * as each suspend will cause a hard/soft reset, the gain of runtime suspend |
| 5040 | * is very little and the ODD may malfunction after constantly being reset. |
| 5041 | * So the idle callback here will not proceed to suspend if a non-ZPODD capable |
| 5042 | * ODD is attached to the port. |
| 5043 | */ |
Lin Ming | 9ee4f39 | 2011-12-05 09:20:28 +0800 | [diff] [blame] | 5044 | static int ata_port_runtime_idle(struct device *dev) |
| 5045 | { |
Aaron Lu | 7e15e9b | 2013-01-15 17:21:04 +0800 | [diff] [blame] | 5046 | struct ata_port *ap = to_ata_port(dev); |
| 5047 | struct ata_link *link; |
| 5048 | struct ata_device *adev; |
| 5049 | |
| 5050 | ata_for_each_link(link, ap, HOST_FIRST) { |
| 5051 | ata_for_each_dev(adev, link, ENABLED) |
| 5052 | if (adev->class == ATA_DEV_ATAPI && |
| 5053 | !zpodd_dev_enabled(adev)) |
| 5054 | return -EBUSY; |
| 5055 | } |
| 5056 | |
Rafael J. Wysocki | 45f0a85 | 2013-06-03 21:49:52 +0200 | [diff] [blame] | 5057 | return 0; |
Lin Ming | 9ee4f39 | 2011-12-05 09:20:28 +0800 | [diff] [blame] | 5058 | } |
| 5059 | |
Aaron Lu | a7ff60d | 2013-01-25 14:29:35 +0800 | [diff] [blame] | 5060 | static int ata_port_runtime_suspend(struct device *dev) |
| 5061 | { |
Dan Williams | bc6e7c4 | 2014-03-14 13:52:48 -0700 | [diff] [blame] | 5062 | ata_port_suspend(to_ata_port(dev), PMSG_AUTO_SUSPEND); |
| 5063 | return 0; |
Aaron Lu | a7ff60d | 2013-01-25 14:29:35 +0800 | [diff] [blame] | 5064 | } |
| 5065 | |
| 5066 | static int ata_port_runtime_resume(struct device *dev) |
| 5067 | { |
Dan Williams | bc6e7c4 | 2014-03-14 13:52:48 -0700 | [diff] [blame] | 5068 | ata_port_resume(to_ata_port(dev), PMSG_AUTO_RESUME); |
| 5069 | return 0; |
Aaron Lu | a7ff60d | 2013-01-25 14:29:35 +0800 | [diff] [blame] | 5070 | } |
| 5071 | |
Lin Ming | 5ef4108 | 2011-12-05 09:20:27 +0800 | [diff] [blame] | 5072 | static const struct dev_pm_ops ata_port_pm_ops = { |
Dan Williams | bc6e7c4 | 2014-03-14 13:52:48 -0700 | [diff] [blame] | 5073 | .suspend = ata_port_pm_suspend, |
| 5074 | .resume = ata_port_pm_resume, |
| 5075 | .freeze = ata_port_pm_freeze, |
| 5076 | .thaw = ata_port_pm_resume, |
| 5077 | .poweroff = ata_port_pm_poweroff, |
| 5078 | .restore = ata_port_pm_resume, |
Lin Ming | 9ee4f39 | 2011-12-05 09:20:28 +0800 | [diff] [blame] | 5079 | |
Aaron Lu | a7ff60d | 2013-01-25 14:29:35 +0800 | [diff] [blame] | 5080 | .runtime_suspend = ata_port_runtime_suspend, |
| 5081 | .runtime_resume = ata_port_runtime_resume, |
Lin Ming | 9ee4f39 | 2011-12-05 09:20:28 +0800 | [diff] [blame] | 5082 | .runtime_idle = ata_port_runtime_idle, |
Lin Ming | 5ef4108 | 2011-12-05 09:20:27 +0800 | [diff] [blame] | 5083 | }; |
| 5084 | |
Dan Williams | 2fcbdcb | 2012-06-21 23:41:46 -0700 | [diff] [blame] | 5085 | /* sas ports don't participate in pm runtime management of ata_ports, |
| 5086 | * and need to resume ata devices at the domain level, not the per-port |
| 5087 | * level. sas suspend/resume is async to allow parallel port recovery |
| 5088 | * since sas has multiple ata_port instances per Scsi_Host. |
| 5089 | */ |
Dan Williams | bc6e7c4 | 2014-03-14 13:52:48 -0700 | [diff] [blame] | 5090 | void ata_sas_port_suspend(struct ata_port *ap) |
Dan Williams | 2fcbdcb | 2012-06-21 23:41:46 -0700 | [diff] [blame] | 5091 | { |
Dan Williams | bc6e7c4 | 2014-03-14 13:52:48 -0700 | [diff] [blame] | 5092 | ata_port_suspend_async(ap, PMSG_SUSPEND); |
Dan Williams | 2fcbdcb | 2012-06-21 23:41:46 -0700 | [diff] [blame] | 5093 | } |
Dan Williams | bc6e7c4 | 2014-03-14 13:52:48 -0700 | [diff] [blame] | 5094 | EXPORT_SYMBOL_GPL(ata_sas_port_suspend); |
Dan Williams | 2fcbdcb | 2012-06-21 23:41:46 -0700 | [diff] [blame] | 5095 | |
Dan Williams | bc6e7c4 | 2014-03-14 13:52:48 -0700 | [diff] [blame] | 5096 | void ata_sas_port_resume(struct ata_port *ap) |
Dan Williams | 2fcbdcb | 2012-06-21 23:41:46 -0700 | [diff] [blame] | 5097 | { |
Dan Williams | bc6e7c4 | 2014-03-14 13:52:48 -0700 | [diff] [blame] | 5098 | ata_port_resume_async(ap, PMSG_RESUME); |
Dan Williams | 2fcbdcb | 2012-06-21 23:41:46 -0700 | [diff] [blame] | 5099 | } |
Dan Williams | bc6e7c4 | 2014-03-14 13:52:48 -0700 | [diff] [blame] | 5100 | EXPORT_SYMBOL_GPL(ata_sas_port_resume); |
Dan Williams | 2fcbdcb | 2012-06-21 23:41:46 -0700 | [diff] [blame] | 5101 | |
Tejun Heo | 500530f | 2006-07-03 16:07:27 +0900 | [diff] [blame] | 5102 | /** |
Jeff Garzik | cca3974 | 2006-08-24 03:19:22 -0400 | [diff] [blame] | 5103 | * ata_host_suspend - suspend host |
| 5104 | * @host: host to suspend |
Tejun Heo | 500530f | 2006-07-03 16:07:27 +0900 | [diff] [blame] | 5105 | * @mesg: PM message |
| 5106 | * |
Lin Ming | 5ef4108 | 2011-12-05 09:20:27 +0800 | [diff] [blame] | 5107 | * Suspend @host. Actual operation is performed by port suspend. |
Tejun Heo | 500530f | 2006-07-03 16:07:27 +0900 | [diff] [blame] | 5108 | */ |
Jeff Garzik | cca3974 | 2006-08-24 03:19:22 -0400 | [diff] [blame] | 5109 | int ata_host_suspend(struct ata_host *host, pm_message_t mesg) |
Tejun Heo | 500530f | 2006-07-03 16:07:27 +0900 | [diff] [blame] | 5110 | { |
Lin Ming | 5ef4108 | 2011-12-05 09:20:27 +0800 | [diff] [blame] | 5111 | host->dev->power.power_state = mesg; |
| 5112 | return 0; |
Tejun Heo | 500530f | 2006-07-03 16:07:27 +0900 | [diff] [blame] | 5113 | } |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 5114 | EXPORT_SYMBOL_GPL(ata_host_suspend); |
Tejun Heo | 500530f | 2006-07-03 16:07:27 +0900 | [diff] [blame] | 5115 | |
| 5116 | /** |
Jeff Garzik | cca3974 | 2006-08-24 03:19:22 -0400 | [diff] [blame] | 5117 | * ata_host_resume - resume host |
| 5118 | * @host: host to resume |
Tejun Heo | 500530f | 2006-07-03 16:07:27 +0900 | [diff] [blame] | 5119 | * |
Lin Ming | 5ef4108 | 2011-12-05 09:20:27 +0800 | [diff] [blame] | 5120 | * Resume @host. Actual operation is performed by port resume. |
Tejun Heo | 500530f | 2006-07-03 16:07:27 +0900 | [diff] [blame] | 5121 | */ |
Jeff Garzik | cca3974 | 2006-08-24 03:19:22 -0400 | [diff] [blame] | 5122 | void ata_host_resume(struct ata_host *host) |
Tejun Heo | 500530f | 2006-07-03 16:07:27 +0900 | [diff] [blame] | 5123 | { |
Jeff Garzik | 72ad6ec | 2008-02-25 17:31:10 -0500 | [diff] [blame] | 5124 | host->dev->power.power_state = PMSG_ON; |
Tejun Heo | 500530f | 2006-07-03 16:07:27 +0900 | [diff] [blame] | 5125 | } |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 5126 | EXPORT_SYMBOL_GPL(ata_host_resume); |
Tejun Heo | 6ffa01d | 2007-03-02 17:32:47 +0900 | [diff] [blame] | 5127 | #endif |
Tejun Heo | 500530f | 2006-07-03 16:07:27 +0900 | [diff] [blame] | 5128 | |
Bhumika Goyal | 8df82c1 | 2017-09-30 22:10:40 +0530 | [diff] [blame] | 5129 | const struct device_type ata_port_type = { |
Lin Ming | 5ef4108 | 2011-12-05 09:20:27 +0800 | [diff] [blame] | 5130 | .name = "ata_port", |
| 5131 | #ifdef CONFIG_PM |
| 5132 | .pm = &ata_port_pm_ops, |
| 5133 | #endif |
| 5134 | }; |
| 5135 | |
Randy Dunlap | c893a3a | 2006-01-28 13:15:32 -0500 | [diff] [blame] | 5136 | /** |
Tejun Heo | 3ef3b43 | 2006-05-31 18:27:30 +0900 | [diff] [blame] | 5137 | * ata_dev_init - Initialize an ata_device structure |
| 5138 | * @dev: Device structure to initialize |
| 5139 | * |
| 5140 | * Initialize @dev in preparation for probing. |
| 5141 | * |
| 5142 | * LOCKING: |
| 5143 | * Inherited from caller. |
| 5144 | */ |
| 5145 | void ata_dev_init(struct ata_device *dev) |
| 5146 | { |
Tejun Heo | b1c7291 | 2008-07-31 17:02:43 +0900 | [diff] [blame] | 5147 | struct ata_link *link = ata_dev_phys_link(dev); |
Tejun Heo | 9af5c9c | 2007-08-06 18:36:22 +0900 | [diff] [blame] | 5148 | struct ata_port *ap = link->ap; |
Tejun Heo | 72fa4b7 | 2006-05-31 18:27:32 +0900 | [diff] [blame] | 5149 | unsigned long flags; |
Tejun Heo | 3ef3b43 | 2006-05-31 18:27:30 +0900 | [diff] [blame] | 5150 | |
Tejun Heo | b1c7291 | 2008-07-31 17:02:43 +0900 | [diff] [blame] | 5151 | /* SATA spd limit is bound to the attached device, reset together */ |
Tejun Heo | 9af5c9c | 2007-08-06 18:36:22 +0900 | [diff] [blame] | 5152 | link->sata_spd_limit = link->hw_sata_spd_limit; |
| 5153 | link->sata_spd = 0; |
Tejun Heo | 5a04bf4 | 2006-05-31 18:27:38 +0900 | [diff] [blame] | 5154 | |
Tejun Heo | 72fa4b7 | 2006-05-31 18:27:32 +0900 | [diff] [blame] | 5155 | /* High bits of dev->flags are used to record warm plug |
| 5156 | * requests which occur asynchronously. Synchronize using |
Jeff Garzik | cca3974 | 2006-08-24 03:19:22 -0400 | [diff] [blame] | 5157 | * host lock. |
Tejun Heo | 72fa4b7 | 2006-05-31 18:27:32 +0900 | [diff] [blame] | 5158 | */ |
Jeff Garzik | ba6a130 | 2006-06-22 23:46:10 -0400 | [diff] [blame] | 5159 | spin_lock_irqsave(ap->lock, flags); |
Tejun Heo | 72fa4b7 | 2006-05-31 18:27:32 +0900 | [diff] [blame] | 5160 | dev->flags &= ~ATA_DFLAG_INIT_MASK; |
Tejun Heo | 3dcc323 | 2007-09-03 12:20:11 +0900 | [diff] [blame] | 5161 | dev->horkage = 0; |
Jeff Garzik | ba6a130 | 2006-06-22 23:46:10 -0400 | [diff] [blame] | 5162 | spin_unlock_irqrestore(ap->lock, flags); |
Tejun Heo | 72fa4b7 | 2006-05-31 18:27:32 +0900 | [diff] [blame] | 5163 | |
Tejun Heo | 99cf610 | 2009-01-29 20:31:32 +0900 | [diff] [blame] | 5164 | memset((void *)dev + ATA_DEVICE_CLEAR_BEGIN, 0, |
| 5165 | ATA_DEVICE_CLEAR_END - ATA_DEVICE_CLEAR_BEGIN); |
Tejun Heo | 3ef3b43 | 2006-05-31 18:27:30 +0900 | [diff] [blame] | 5166 | dev->pio_mask = UINT_MAX; |
| 5167 | dev->mwdma_mask = UINT_MAX; |
| 5168 | dev->udma_mask = UINT_MAX; |
| 5169 | } |
| 5170 | |
| 5171 | /** |
Tejun Heo | 4fb37a2 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 5172 | * ata_link_init - Initialize an ata_link structure |
| 5173 | * @ap: ATA port link is attached to |
| 5174 | * @link: Link structure to initialize |
Tejun Heo | 8989805 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 5175 | * @pmp: Port multiplier port number |
Tejun Heo | 4fb37a2 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 5176 | * |
| 5177 | * Initialize @link. |
| 5178 | * |
| 5179 | * LOCKING: |
| 5180 | * Kernel thread context (may sleep) |
| 5181 | */ |
Tejun Heo | fb7fd61 | 2007-09-23 13:14:12 +0900 | [diff] [blame] | 5182 | 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] | 5183 | { |
| 5184 | int i; |
| 5185 | |
| 5186 | /* clear everything except for devices */ |
Gwendal Grignou | d902747 | 2010-05-25 12:31:38 -0700 | [diff] [blame] | 5187 | memset((void *)link + ATA_LINK_CLEAR_BEGIN, 0, |
| 5188 | ATA_LINK_CLEAR_END - ATA_LINK_CLEAR_BEGIN); |
Tejun Heo | 4fb37a2 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 5189 | |
| 5190 | link->ap = ap; |
Tejun Heo | 8989805 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 5191 | link->pmp = pmp; |
Tejun Heo | 4fb37a2 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 5192 | link->active_tag = ATA_TAG_POISON; |
| 5193 | link->hw_sata_spd_limit = UINT_MAX; |
| 5194 | |
| 5195 | /* can't use iterator, ap isn't initialized yet */ |
| 5196 | for (i = 0; i < ATA_MAX_DEVICES; i++) { |
| 5197 | struct ata_device *dev = &link->device[i]; |
| 5198 | |
| 5199 | dev->link = link; |
| 5200 | dev->devno = dev - link->device; |
Tejun Heo | 110f66d | 2009-09-16 04:17:28 +0900 | [diff] [blame] | 5201 | #ifdef CONFIG_ATA_ACPI |
| 5202 | dev->gtf_filter = ata_acpi_gtf_filter; |
| 5203 | #endif |
Tejun Heo | 4fb37a2 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 5204 | ata_dev_init(dev); |
| 5205 | } |
| 5206 | } |
| 5207 | |
| 5208 | /** |
| 5209 | * sata_link_init_spd - Initialize link->sata_spd_limit |
| 5210 | * @link: Link to configure sata_spd_limit for |
| 5211 | * |
| 5212 | * Initialize @link->[hw_]sata_spd_limit to the currently |
| 5213 | * configured value. |
| 5214 | * |
| 5215 | * LOCKING: |
| 5216 | * Kernel thread context (may sleep). |
| 5217 | * |
| 5218 | * RETURNS: |
| 5219 | * 0 on success, -errno on failure. |
| 5220 | */ |
Tejun Heo | fb7fd61 | 2007-09-23 13:14:12 +0900 | [diff] [blame] | 5221 | int sata_link_init_spd(struct ata_link *link) |
Tejun Heo | 4fb37a2 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 5222 | { |
Tejun Heo | 3326732 | 2008-02-13 09:15:09 +0900 | [diff] [blame] | 5223 | u8 spd; |
Tejun Heo | 4fb37a2 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 5224 | int rc; |
| 5225 | |
Tejun Heo | d127ea7 | 2008-07-31 16:09:34 +0900 | [diff] [blame] | 5226 | rc = sata_scr_read(link, SCR_CONTROL, &link->saved_scontrol); |
Tejun Heo | 4fb37a2 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 5227 | if (rc) |
| 5228 | return rc; |
| 5229 | |
Tejun Heo | d127ea7 | 2008-07-31 16:09:34 +0900 | [diff] [blame] | 5230 | spd = (link->saved_scontrol >> 4) & 0xf; |
Tejun Heo | 4fb37a2 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 5231 | if (spd) |
| 5232 | link->hw_sata_spd_limit &= (1 << spd) - 1; |
| 5233 | |
Tejun Heo | 05944bd | 2008-08-13 20:19:09 +0900 | [diff] [blame] | 5234 | ata_force_link_limits(link); |
Tejun Heo | 3326732 | 2008-02-13 09:15:09 +0900 | [diff] [blame] | 5235 | |
Tejun Heo | 4fb37a2 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 5236 | link->sata_spd_limit = link->hw_sata_spd_limit; |
| 5237 | |
| 5238 | return 0; |
| 5239 | } |
| 5240 | |
| 5241 | /** |
Tejun Heo | f318719 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5242 | * ata_port_alloc - allocate and initialize basic ATA port resources |
| 5243 | * @host: ATA host this allocated port belongs to |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5244 | * |
Tejun Heo | f318719 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5245 | * Allocate and initialize basic ATA port resources. |
| 5246 | * |
| 5247 | * RETURNS: |
| 5248 | * Allocate ATA port on success, NULL on failure. |
Jeff Garzik | 0cba632 | 2005-05-30 19:49:12 -0400 | [diff] [blame] | 5249 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5250 | * LOCKING: |
Tejun Heo | f318719 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5251 | * Inherited from calling layer (may sleep). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5252 | */ |
Tejun Heo | f318719 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5253 | struct ata_port *ata_port_alloc(struct ata_host *host) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5254 | { |
Tejun Heo | f318719 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5255 | struct ata_port *ap; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5256 | |
Tejun Heo | f318719 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5257 | DPRINTK("ENTER\n"); |
| 5258 | |
| 5259 | ap = kzalloc(sizeof(*ap), GFP_KERNEL); |
| 5260 | if (!ap) |
| 5261 | return NULL; |
Jeff Garzik | 4fca377 | 2011-02-15 01:13:24 -0500 | [diff] [blame] | 5262 | |
Maxime Bizon | 7b3a24c5 | 2011-03-16 14:58:32 +0100 | [diff] [blame] | 5263 | ap->pflags |= ATA_PFLAG_INITIALIZING | ATA_PFLAG_FROZEN; |
Jeff Garzik | cca3974 | 2006-08-24 03:19:22 -0400 | [diff] [blame] | 5264 | ap->lock = &host->lock; |
Tejun Heo | f318719 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5265 | ap->print_id = -1; |
David Milburn | e628dc9 | 2013-05-14 13:48:40 -0500 | [diff] [blame] | 5266 | ap->local_port_no = -1; |
Jeff Garzik | cca3974 | 2006-08-24 03:19:22 -0400 | [diff] [blame] | 5267 | ap->host = host; |
Tejun Heo | f318719 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5268 | ap->dev = host->dev; |
Borislav Petkov | bd5d825 | 2006-06-11 23:17:01 -0400 | [diff] [blame] | 5269 | |
| 5270 | #if defined(ATA_VERBOSE_DEBUG) |
| 5271 | /* turn on all debugging levels */ |
| 5272 | ap->msg_enable = 0x00FF; |
| 5273 | #elif defined(ATA_DEBUG) |
| 5274 | 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] | 5275 | #else |
Borislav Petkov | 0dd4b21 | 2006-06-23 02:29:08 -0400 | [diff] [blame] | 5276 | ap->msg_enable = ATA_MSG_DRV | ATA_MSG_ERR | ATA_MSG_WARN; |
Borislav Petkov | bd5d825 | 2006-06-11 23:17:01 -0400 | [diff] [blame] | 5277 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5278 | |
Tejun Heo | ad72cf9 | 2010-07-02 10:03:52 +0200 | [diff] [blame] | 5279 | mutex_init(&ap->scsi_scan_mutex); |
David Howells | 65f27f3 | 2006-11-22 14:55:48 +0000 | [diff] [blame] | 5280 | INIT_DELAYED_WORK(&ap->hotplug_task, ata_scsi_hotplug); |
| 5281 | INIT_WORK(&ap->scsi_rescan_task, ata_scsi_dev_rescan); |
Tejun Heo | a72ec4c | 2006-01-23 13:09:37 +0900 | [diff] [blame] | 5282 | INIT_LIST_HEAD(&ap->eh_done_q); |
Tejun Heo | c6cf9e9 | 2006-05-31 18:27:27 +0900 | [diff] [blame] | 5283 | init_waitqueue_head(&ap->eh_wait_q); |
Elias Oltmanns | 45fabbb | 2008-09-21 11:54:08 +0200 | [diff] [blame] | 5284 | init_completion(&ap->park_req_pending); |
Kees Cook | b93ab33 | 2017-10-16 14:56:42 -0700 | [diff] [blame] | 5285 | timer_setup(&ap->fastdrain_timer, ata_eh_fastdrain_timerfn, |
| 5286 | TIMER_DEFERRABLE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5287 | |
Tejun Heo | 838df62 | 2006-05-15 20:57:44 +0900 | [diff] [blame] | 5288 | ap->cbl = ATA_CBL_NONE; |
Tejun Heo | 838df62 | 2006-05-15 20:57:44 +0900 | [diff] [blame] | 5289 | |
Tejun Heo | 8989805 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 5290 | ata_link_init(ap, &ap->link, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5291 | |
| 5292 | #ifdef ATA_IRQ_TRAP |
| 5293 | ap->stats.unhandled_irq = 1; |
| 5294 | ap->stats.idle_irq = 1; |
| 5295 | #endif |
Tejun Heo | 270390e | 2010-05-10 21:41:35 +0200 | [diff] [blame] | 5296 | ata_sff_port_init(ap); |
| 5297 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5298 | return ap; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5299 | } |
| 5300 | |
Taras Kondratiuk | 2623c7a | 2018-03-09 08:34:41 +0000 | [diff] [blame] | 5301 | static void ata_devres_release(struct device *gendev, void *res) |
Tejun Heo | f0d36ef | 2007-01-20 16:00:28 +0900 | [diff] [blame] | 5302 | { |
| 5303 | struct ata_host *host = dev_get_drvdata(gendev); |
| 5304 | int i; |
| 5305 | |
| 5306 | for (i = 0; i < host->n_ports; i++) { |
| 5307 | struct ata_port *ap = host->ports[i]; |
| 5308 | |
Tejun Heo | ecef725 | 2007-04-17 23:44:06 +0900 | [diff] [blame] | 5309 | if (!ap) |
| 5310 | continue; |
| 5311 | |
Tejun Heo | 4911487 | 2007-04-17 23:44:06 +0900 | [diff] [blame] | 5312 | if (ap->scsi_host) |
Tejun Heo | 1aa506e4 | 2007-03-09 19:36:12 +0900 | [diff] [blame] | 5313 | scsi_host_put(ap->scsi_host); |
| 5314 | |
Taras Kondratiuk | 2623c7a | 2018-03-09 08:34:41 +0000 | [diff] [blame] | 5315 | } |
| 5316 | |
| 5317 | dev_set_drvdata(gendev, NULL); |
| 5318 | ata_host_put(host); |
| 5319 | } |
| 5320 | |
| 5321 | static void ata_host_release(struct kref *kref) |
| 5322 | { |
| 5323 | struct ata_host *host = container_of(kref, struct ata_host, kref); |
| 5324 | int i; |
| 5325 | |
| 5326 | for (i = 0; i < host->n_ports; i++) { |
| 5327 | struct ata_port *ap = host->ports[i]; |
| 5328 | |
Tejun Heo | 633273a | 2007-09-23 13:19:54 +0900 | [diff] [blame] | 5329 | kfree(ap->pmp_link); |
Tejun Heo | b1c7291 | 2008-07-31 17:02:43 +0900 | [diff] [blame] | 5330 | kfree(ap->slave_link); |
Tejun Heo | 4911487 | 2007-04-17 23:44:06 +0900 | [diff] [blame] | 5331 | kfree(ap); |
Tejun Heo | 1aa506e4 | 2007-03-09 19:36:12 +0900 | [diff] [blame] | 5332 | host->ports[i] = NULL; |
| 5333 | } |
Taras Kondratiuk | 2623c7a | 2018-03-09 08:34:41 +0000 | [diff] [blame] | 5334 | kfree(host); |
| 5335 | } |
Tejun Heo | 1aa506e4 | 2007-03-09 19:36:12 +0900 | [diff] [blame] | 5336 | |
Taras Kondratiuk | 2623c7a | 2018-03-09 08:34:41 +0000 | [diff] [blame] | 5337 | void ata_host_get(struct ata_host *host) |
| 5338 | { |
| 5339 | kref_get(&host->kref); |
| 5340 | } |
| 5341 | |
| 5342 | void ata_host_put(struct ata_host *host) |
| 5343 | { |
| 5344 | kref_put(&host->kref, ata_host_release); |
Tejun Heo | f0d36ef | 2007-01-20 16:00:28 +0900 | [diff] [blame] | 5345 | } |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 5346 | EXPORT_SYMBOL_GPL(ata_host_put); |
Tejun Heo | f0d36ef | 2007-01-20 16:00:28 +0900 | [diff] [blame] | 5347 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5348 | /** |
Tejun Heo | f318719 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5349 | * ata_host_alloc - allocate and init basic ATA host resources |
| 5350 | * @dev: generic device this host is associated with |
| 5351 | * @max_ports: maximum number of ATA ports associated with this host |
| 5352 | * |
| 5353 | * Allocate and initialize basic ATA host resources. LLD calls |
| 5354 | * this function to allocate a host, initializes it fully and |
| 5355 | * attaches it using ata_host_register(). |
| 5356 | * |
| 5357 | * @max_ports ports are allocated and host->n_ports is |
| 5358 | * initialized to @max_ports. The caller is allowed to decrease |
| 5359 | * host->n_ports before calling ata_host_register(). The unused |
| 5360 | * ports will be automatically freed on registration. |
| 5361 | * |
| 5362 | * RETURNS: |
| 5363 | * Allocate ATA host on success, NULL on failure. |
| 5364 | * |
| 5365 | * LOCKING: |
| 5366 | * Inherited from calling layer (may sleep). |
| 5367 | */ |
| 5368 | struct ata_host *ata_host_alloc(struct device *dev, int max_ports) |
| 5369 | { |
| 5370 | struct ata_host *host; |
| 5371 | size_t sz; |
| 5372 | int i; |
Taras Kondratiuk | 2623c7a | 2018-03-09 08:34:41 +0000 | [diff] [blame] | 5373 | void *dr; |
Tejun Heo | f318719 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5374 | |
| 5375 | DPRINTK("ENTER\n"); |
| 5376 | |
Tejun Heo | f318719 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5377 | /* alloc a container for our list of ATA ports (buses) */ |
| 5378 | sz = sizeof(struct ata_host) + (max_ports + 1) * sizeof(void *); |
Taras Kondratiuk | 2623c7a | 2018-03-09 08:34:41 +0000 | [diff] [blame] | 5379 | host = kzalloc(sz, GFP_KERNEL); |
Tejun Heo | f318719 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5380 | if (!host) |
Taras Kondratiuk | 2623c7a | 2018-03-09 08:34:41 +0000 | [diff] [blame] | 5381 | return NULL; |
| 5382 | |
Tejun Heo | f318719 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5383 | if (!devres_open_group(dev, NULL, GFP_KERNEL)) |
Colin Ian King | dafd6c4 | 2018-03-27 14:26:01 +0100 | [diff] [blame] | 5384 | goto err_free; |
Tejun Heo | f318719 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5385 | |
Taras Kondratiuk | 2623c7a | 2018-03-09 08:34:41 +0000 | [diff] [blame] | 5386 | dr = devres_alloc(ata_devres_release, 0, GFP_KERNEL); |
| 5387 | if (!dr) |
Tejun Heo | f318719 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5388 | goto err_out; |
| 5389 | |
Taras Kondratiuk | 2623c7a | 2018-03-09 08:34:41 +0000 | [diff] [blame] | 5390 | devres_add(dev, dr); |
Tejun Heo | f318719 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5391 | dev_set_drvdata(dev, host); |
| 5392 | |
| 5393 | spin_lock_init(&host->lock); |
Tejun Heo | c0c362b | 2010-09-06 17:57:14 +0200 | [diff] [blame] | 5394 | mutex_init(&host->eh_mutex); |
Tejun Heo | f318719 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5395 | host->dev = dev; |
| 5396 | host->n_ports = max_ports; |
Taras Kondratiuk | 2623c7a | 2018-03-09 08:34:41 +0000 | [diff] [blame] | 5397 | kref_init(&host->kref); |
Tejun Heo | f318719 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5398 | |
| 5399 | /* allocate ports bound to this host */ |
| 5400 | for (i = 0; i < max_ports; i++) { |
| 5401 | struct ata_port *ap; |
| 5402 | |
| 5403 | ap = ata_port_alloc(host); |
| 5404 | if (!ap) |
| 5405 | goto err_out; |
| 5406 | |
| 5407 | ap->port_no = i; |
| 5408 | host->ports[i] = ap; |
| 5409 | } |
| 5410 | |
| 5411 | devres_remove_group(dev, NULL); |
| 5412 | return host; |
| 5413 | |
| 5414 | err_out: |
| 5415 | devres_release_group(dev, NULL); |
Colin Ian King | dafd6c4 | 2018-03-27 14:26:01 +0100 | [diff] [blame] | 5416 | err_free: |
| 5417 | kfree(host); |
Tejun Heo | f318719 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5418 | return NULL; |
| 5419 | } |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 5420 | EXPORT_SYMBOL_GPL(ata_host_alloc); |
Tejun Heo | f318719 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5421 | |
| 5422 | /** |
Tejun Heo | f5cda25 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5423 | * ata_host_alloc_pinfo - alloc host and init with port_info array |
| 5424 | * @dev: generic device this host is associated with |
| 5425 | * @ppi: array of ATA port_info to initialize host with |
| 5426 | * @n_ports: number of ATA ports attached to this host |
| 5427 | * |
| 5428 | * Allocate ATA host and initialize with info from @ppi. If NULL |
| 5429 | * terminated, @ppi may contain fewer entries than @n_ports. The |
| 5430 | * last entry will be used for the remaining ports. |
| 5431 | * |
| 5432 | * RETURNS: |
| 5433 | * Allocate ATA host on success, NULL on failure. |
| 5434 | * |
| 5435 | * LOCKING: |
| 5436 | * Inherited from calling layer (may sleep). |
| 5437 | */ |
| 5438 | struct ata_host *ata_host_alloc_pinfo(struct device *dev, |
| 5439 | const struct ata_port_info * const * ppi, |
| 5440 | int n_ports) |
| 5441 | { |
| 5442 | const struct ata_port_info *pi; |
| 5443 | struct ata_host *host; |
| 5444 | int i, j; |
| 5445 | |
| 5446 | host = ata_host_alloc(dev, n_ports); |
| 5447 | if (!host) |
| 5448 | return NULL; |
| 5449 | |
| 5450 | for (i = 0, j = 0, pi = NULL; i < host->n_ports; i++) { |
| 5451 | struct ata_port *ap = host->ports[i]; |
| 5452 | |
| 5453 | if (ppi[j]) |
| 5454 | pi = ppi[j++]; |
| 5455 | |
| 5456 | ap->pio_mask = pi->pio_mask; |
| 5457 | ap->mwdma_mask = pi->mwdma_mask; |
| 5458 | ap->udma_mask = pi->udma_mask; |
| 5459 | ap->flags |= pi->flags; |
Tejun Heo | 0c88758 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 5460 | ap->link.flags |= pi->link_flags; |
Tejun Heo | f5cda25 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5461 | ap->ops = pi->port_ops; |
| 5462 | |
| 5463 | if (!host->ops && (pi->port_ops != &ata_dummy_port_ops)) |
| 5464 | host->ops = pi->port_ops; |
Tejun Heo | f5cda25 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5465 | } |
| 5466 | |
| 5467 | return host; |
| 5468 | } |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 5469 | EXPORT_SYMBOL_GPL(ata_host_alloc_pinfo); |
Tejun Heo | f5cda25 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5470 | |
Tejun Heo | 32ebbc0 | 2007-11-08 13:09:00 +0900 | [diff] [blame] | 5471 | static void ata_host_stop(struct device *gendev, void *res) |
| 5472 | { |
| 5473 | struct ata_host *host = dev_get_drvdata(gendev); |
| 5474 | int i; |
| 5475 | |
| 5476 | WARN_ON(!(host->flags & ATA_HOST_STARTED)); |
| 5477 | |
| 5478 | for (i = 0; i < host->n_ports; i++) { |
| 5479 | struct ata_port *ap = host->ports[i]; |
| 5480 | |
| 5481 | if (ap->ops->port_stop) |
| 5482 | ap->ops->port_stop(ap); |
| 5483 | } |
| 5484 | |
| 5485 | if (host->ops->host_stop) |
| 5486 | host->ops->host_stop(host); |
| 5487 | } |
| 5488 | |
Tejun Heo | f5cda25 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5489 | /** |
Tejun Heo | 029cfd6 | 2008-03-25 12:22:49 +0900 | [diff] [blame] | 5490 | * ata_finalize_port_ops - finalize ata_port_operations |
| 5491 | * @ops: ata_port_operations to finalize |
| 5492 | * |
| 5493 | * An ata_port_operations can inherit from another ops and that |
| 5494 | * ops can again inherit from another. This can go on as many |
| 5495 | * times as necessary as long as there is no loop in the |
| 5496 | * inheritance chain. |
| 5497 | * |
| 5498 | * Ops tables are finalized when the host is started. NULL or |
| 5499 | * unspecified entries are inherited from the closet ancestor |
| 5500 | * which has the method and the entry is populated with it. |
| 5501 | * After finalization, the ops table directly points to all the |
| 5502 | * methods and ->inherits is no longer necessary and cleared. |
| 5503 | * |
| 5504 | * Using ATA_OP_NULL, inheriting ops can force a method to NULL. |
| 5505 | * |
| 5506 | * LOCKING: |
| 5507 | * None. |
| 5508 | */ |
| 5509 | static void ata_finalize_port_ops(struct ata_port_operations *ops) |
| 5510 | { |
Pradeep Singh Rautela | 2da6765 | 2008-05-29 23:28:14 +0530 | [diff] [blame] | 5511 | static DEFINE_SPINLOCK(lock); |
Tejun Heo | 029cfd6 | 2008-03-25 12:22:49 +0900 | [diff] [blame] | 5512 | const struct ata_port_operations *cur; |
| 5513 | void **begin = (void **)ops; |
| 5514 | void **end = (void **)&ops->inherits; |
| 5515 | void **pp; |
| 5516 | |
| 5517 | if (!ops || !ops->inherits) |
| 5518 | return; |
| 5519 | |
| 5520 | spin_lock(&lock); |
| 5521 | |
| 5522 | for (cur = ops->inherits; cur; cur = cur->inherits) { |
| 5523 | void **inherit = (void **)cur; |
| 5524 | |
| 5525 | for (pp = begin; pp < end; pp++, inherit++) |
| 5526 | if (!*pp) |
| 5527 | *pp = *inherit; |
| 5528 | } |
| 5529 | |
| 5530 | for (pp = begin; pp < end; pp++) |
| 5531 | if (IS_ERR(*pp)) |
| 5532 | *pp = NULL; |
| 5533 | |
| 5534 | ops->inherits = NULL; |
| 5535 | |
| 5536 | spin_unlock(&lock); |
| 5537 | } |
| 5538 | |
| 5539 | /** |
Tejun Heo | ecef725 | 2007-04-17 23:44:06 +0900 | [diff] [blame] | 5540 | * ata_host_start - start and freeze ports of an ATA host |
| 5541 | * @host: ATA host to start ports for |
| 5542 | * |
| 5543 | * Start and then freeze ports of @host. Started status is |
| 5544 | * recorded in host->flags, so this function can be called |
| 5545 | * multiple times. Ports are guaranteed to get started only |
Tejun Heo | f318719 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5546 | * once. If host->ops isn't initialized yet, its set to the |
| 5547 | * first non-dummy port ops. |
Tejun Heo | ecef725 | 2007-04-17 23:44:06 +0900 | [diff] [blame] | 5548 | * |
| 5549 | * LOCKING: |
| 5550 | * Inherited from calling layer (may sleep). |
| 5551 | * |
| 5552 | * RETURNS: |
| 5553 | * 0 if all ports are started successfully, -errno otherwise. |
| 5554 | */ |
| 5555 | int ata_host_start(struct ata_host *host) |
| 5556 | { |
Tejun Heo | 32ebbc0 | 2007-11-08 13:09:00 +0900 | [diff] [blame] | 5557 | int have_stop = 0; |
| 5558 | void *start_dr = NULL; |
Tejun Heo | ecef725 | 2007-04-17 23:44:06 +0900 | [diff] [blame] | 5559 | int i, rc; |
| 5560 | |
| 5561 | if (host->flags & ATA_HOST_STARTED) |
| 5562 | return 0; |
| 5563 | |
Tejun Heo | 029cfd6 | 2008-03-25 12:22:49 +0900 | [diff] [blame] | 5564 | ata_finalize_port_ops(host->ops); |
| 5565 | |
Tejun Heo | ecef725 | 2007-04-17 23:44:06 +0900 | [diff] [blame] | 5566 | for (i = 0; i < host->n_ports; i++) { |
| 5567 | struct ata_port *ap = host->ports[i]; |
| 5568 | |
Tejun Heo | 029cfd6 | 2008-03-25 12:22:49 +0900 | [diff] [blame] | 5569 | ata_finalize_port_ops(ap->ops); |
| 5570 | |
Tejun Heo | f318719 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5571 | if (!host->ops && !ata_port_is_dummy(ap)) |
| 5572 | host->ops = ap->ops; |
| 5573 | |
Tejun Heo | 32ebbc0 | 2007-11-08 13:09:00 +0900 | [diff] [blame] | 5574 | if (ap->ops->port_stop) |
| 5575 | have_stop = 1; |
| 5576 | } |
| 5577 | |
| 5578 | if (host->ops->host_stop) |
| 5579 | have_stop = 1; |
| 5580 | |
| 5581 | if (have_stop) { |
| 5582 | start_dr = devres_alloc(ata_host_stop, 0, GFP_KERNEL); |
| 5583 | if (!start_dr) |
| 5584 | return -ENOMEM; |
| 5585 | } |
| 5586 | |
| 5587 | for (i = 0; i < host->n_ports; i++) { |
| 5588 | struct ata_port *ap = host->ports[i]; |
| 5589 | |
Tejun Heo | ecef725 | 2007-04-17 23:44:06 +0900 | [diff] [blame] | 5590 | if (ap->ops->port_start) { |
| 5591 | rc = ap->ops->port_start(ap); |
| 5592 | if (rc) { |
Alan Cox | 0f9fe9b | 2007-11-30 15:23:16 +0000 | [diff] [blame] | 5593 | if (rc != -ENODEV) |
Joe Perches | a44fec1 | 2011-04-15 15:51:58 -0700 | [diff] [blame] | 5594 | dev_err(host->dev, |
| 5595 | "failed to start port %d (errno=%d)\n", |
| 5596 | i, rc); |
Tejun Heo | ecef725 | 2007-04-17 23:44:06 +0900 | [diff] [blame] | 5597 | goto err_out; |
| 5598 | } |
| 5599 | } |
Tejun Heo | ecef725 | 2007-04-17 23:44:06 +0900 | [diff] [blame] | 5600 | ata_eh_freeze_port(ap); |
| 5601 | } |
| 5602 | |
Tejun Heo | 32ebbc0 | 2007-11-08 13:09:00 +0900 | [diff] [blame] | 5603 | if (start_dr) |
| 5604 | devres_add(host->dev, start_dr); |
Tejun Heo | ecef725 | 2007-04-17 23:44:06 +0900 | [diff] [blame] | 5605 | host->flags |= ATA_HOST_STARTED; |
| 5606 | return 0; |
| 5607 | |
| 5608 | err_out: |
| 5609 | while (--i >= 0) { |
| 5610 | struct ata_port *ap = host->ports[i]; |
| 5611 | |
| 5612 | if (ap->ops->port_stop) |
| 5613 | ap->ops->port_stop(ap); |
| 5614 | } |
Tejun Heo | 32ebbc0 | 2007-11-08 13:09:00 +0900 | [diff] [blame] | 5615 | devres_free(start_dr); |
Tejun Heo | ecef725 | 2007-04-17 23:44:06 +0900 | [diff] [blame] | 5616 | return rc; |
| 5617 | } |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 5618 | EXPORT_SYMBOL_GPL(ata_host_start); |
Tejun Heo | ecef725 | 2007-04-17 23:44:06 +0900 | [diff] [blame] | 5619 | |
| 5620 | /** |
Dan Williams | 8d8e7d1 | 2012-07-09 21:06:08 -0700 | [diff] [blame] | 5621 | * ata_sas_host_init - Initialize a host struct for sas (ipr, libsas) |
Jeff Garzik | cca3974 | 2006-08-24 03:19:22 -0400 | [diff] [blame] | 5622 | * @host: host to initialize |
| 5623 | * @dev: device host is attached to |
Jeff Garzik | cca3974 | 2006-08-24 03:19:22 -0400 | [diff] [blame] | 5624 | * @ops: port_ops |
Brian King | b03732f | 2006-08-07 14:27:10 -0500 | [diff] [blame] | 5625 | * |
Brian King | b03732f | 2006-08-07 14:27:10 -0500 | [diff] [blame] | 5626 | */ |
Jeff Garzik | cca3974 | 2006-08-24 03:19:22 -0400 | [diff] [blame] | 5627 | void ata_host_init(struct ata_host *host, struct device *dev, |
Dan Williams | 8d8e7d1 | 2012-07-09 21:06:08 -0700 | [diff] [blame] | 5628 | struct ata_port_operations *ops) |
Brian King | b03732f | 2006-08-07 14:27:10 -0500 | [diff] [blame] | 5629 | { |
Jeff Garzik | cca3974 | 2006-08-24 03:19:22 -0400 | [diff] [blame] | 5630 | spin_lock_init(&host->lock); |
Tejun Heo | c0c362b | 2010-09-06 17:57:14 +0200 | [diff] [blame] | 5631 | mutex_init(&host->eh_mutex); |
Jens Axboe | 69278f7 | 2018-05-11 12:51:10 -0600 | [diff] [blame] | 5632 | host->n_tags = ATA_MAX_QUEUE; |
Jeff Garzik | cca3974 | 2006-08-24 03:19:22 -0400 | [diff] [blame] | 5633 | host->dev = dev; |
Jeff Garzik | cca3974 | 2006-08-24 03:19:22 -0400 | [diff] [blame] | 5634 | host->ops = ops; |
Jason Yan | 2fa4a32 | 2018-05-10 11:05:16 +0800 | [diff] [blame] | 5635 | kref_init(&host->kref); |
Brian King | b03732f | 2006-08-07 14:27:10 -0500 | [diff] [blame] | 5636 | } |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 5637 | EXPORT_SYMBOL_GPL(ata_host_init); |
Brian King | b03732f | 2006-08-07 14:27:10 -0500 | [diff] [blame] | 5638 | |
Dan Williams | 9508a66 | 2012-01-18 20:47:01 -0800 | [diff] [blame] | 5639 | void __ata_port_probe(struct ata_port *ap) |
| 5640 | { |
| 5641 | struct ata_eh_info *ehi = &ap->link.eh_info; |
| 5642 | unsigned long flags; |
| 5643 | |
| 5644 | /* kick EH for boot probing */ |
| 5645 | spin_lock_irqsave(ap->lock, flags); |
| 5646 | |
| 5647 | ehi->probe_mask |= ATA_ALL_DEVICES; |
| 5648 | ehi->action |= ATA_EH_RESET; |
| 5649 | ehi->flags |= ATA_EHI_NO_AUTOPSY | ATA_EHI_QUIET; |
| 5650 | |
| 5651 | ap->pflags &= ~ATA_PFLAG_INITIALIZING; |
| 5652 | ap->pflags |= ATA_PFLAG_LOADING; |
| 5653 | ata_port_schedule_eh(ap); |
| 5654 | |
| 5655 | spin_unlock_irqrestore(ap->lock, flags); |
| 5656 | } |
| 5657 | |
James Bottomley | 238c9cf | 2011-01-23 08:28:33 -0600 | [diff] [blame] | 5658 | int ata_port_probe(struct ata_port *ap) |
Arjan van de Ven | 7931805 | 2009-01-04 05:32:28 -0800 | [diff] [blame] | 5659 | { |
James Bottomley | 238c9cf | 2011-01-23 08:28:33 -0600 | [diff] [blame] | 5660 | int rc = 0; |
Arjan van de Ven | 886ad09 | 2009-01-09 15:54:07 -0800 | [diff] [blame] | 5661 | |
Arjan van de Ven | 7931805 | 2009-01-04 05:32:28 -0800 | [diff] [blame] | 5662 | if (ap->ops->error_handler) { |
Dan Williams | 9508a66 | 2012-01-18 20:47:01 -0800 | [diff] [blame] | 5663 | __ata_port_probe(ap); |
Arjan van de Ven | 7931805 | 2009-01-04 05:32:28 -0800 | [diff] [blame] | 5664 | ata_port_wait_eh(ap); |
| 5665 | } else { |
| 5666 | DPRINTK("ata%u: bus probe begin\n", ap->print_id); |
| 5667 | rc = ata_bus_probe(ap); |
| 5668 | DPRINTK("ata%u: bus probe end\n", ap->print_id); |
Arjan van de Ven | 7931805 | 2009-01-04 05:32:28 -0800 | [diff] [blame] | 5669 | } |
James Bottomley | 238c9cf | 2011-01-23 08:28:33 -0600 | [diff] [blame] | 5670 | return rc; |
| 5671 | } |
| 5672 | |
| 5673 | |
| 5674 | static void async_port_probe(void *data, async_cookie_t cookie) |
| 5675 | { |
| 5676 | struct ata_port *ap = data; |
Jeff Garzik | 4fca377 | 2011-02-15 01:13:24 -0500 | [diff] [blame] | 5677 | |
James Bottomley | 238c9cf | 2011-01-23 08:28:33 -0600 | [diff] [blame] | 5678 | /* |
| 5679 | * If we're not allowed to scan this host in parallel, |
| 5680 | * we need to wait until all previous scans have completed |
| 5681 | * before going further. |
| 5682 | * Jeff Garzik says this is only within a controller, so we |
| 5683 | * don't need to wait for port 0, only for later ports. |
| 5684 | */ |
| 5685 | if (!(ap->host->flags & ATA_HOST_PARALLEL_SCAN) && ap->port_no != 0) |
| 5686 | async_synchronize_cookie(cookie); |
| 5687 | |
| 5688 | (void)ata_port_probe(ap); |
Arjan van de Ven | f29d3b2 | 2009-01-05 15:07:07 -0800 | [diff] [blame] | 5689 | |
| 5690 | /* in order to keep device order, we need to synchronize at this point */ |
| 5691 | async_synchronize_cookie(cookie); |
| 5692 | |
| 5693 | ata_scsi_scan_host(ap, 1); |
Arjan van de Ven | 7931805 | 2009-01-04 05:32:28 -0800 | [diff] [blame] | 5694 | } |
James Bottomley | 238c9cf | 2011-01-23 08:28:33 -0600 | [diff] [blame] | 5695 | |
Brian King | b03732f | 2006-08-07 14:27:10 -0500 | [diff] [blame] | 5696 | /** |
Tejun Heo | f318719 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5697 | * ata_host_register - register initialized ATA host |
| 5698 | * @host: ATA host to register |
| 5699 | * @sht: template for SCSI host |
Jeff Garzik | 0cba632 | 2005-05-30 19:49:12 -0400 | [diff] [blame] | 5700 | * |
Tejun Heo | f318719 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5701 | * Register initialized ATA host. @host is allocated using |
| 5702 | * ata_host_alloc() and fully initialized by LLD. This function |
| 5703 | * starts ports, registers @host with ATA and SCSI layers and |
| 5704 | * probe registered devices. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5705 | * |
| 5706 | * LOCKING: |
Tejun Heo | f318719 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5707 | * Inherited from calling layer (may sleep). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5708 | * |
| 5709 | * RETURNS: |
Tejun Heo | f318719 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5710 | * 0 on success, -errno otherwise. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5711 | */ |
Tejun Heo | f318719 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5712 | 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] | 5713 | { |
Tejun Heo | f318719 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5714 | int i, rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5715 | |
Jens Axboe | 69278f7 | 2018-05-11 12:51:10 -0600 | [diff] [blame] | 5716 | host->n_tags = clamp(sht->can_queue, 1, ATA_MAX_QUEUE); |
Kevin Hao | 1871ee1 | 2014-07-12 12:08:24 +0800 | [diff] [blame] | 5717 | |
Tejun Heo | f318719 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5718 | /* host must have been started */ |
| 5719 | if (!(host->flags & ATA_HOST_STARTED)) { |
Joe Perches | a44fec1 | 2011-04-15 15:51:58 -0700 | [diff] [blame] | 5720 | dev_err(host->dev, "BUG: trying to register unstarted host\n"); |
Tejun Heo | f318719 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5721 | WARN_ON(1); |
| 5722 | return -EINVAL; |
Alan Cox | 02f076a | 2006-09-26 17:35:32 +0100 | [diff] [blame] | 5723 | } |
Tejun Heo | f0d36ef | 2007-01-20 16:00:28 +0900 | [diff] [blame] | 5724 | |
Tejun Heo | f318719 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5725 | /* Blow away unused ports. This happens when LLD can't |
| 5726 | * determine the exact number of ports to allocate at |
| 5727 | * allocation time. |
| 5728 | */ |
| 5729 | for (i = host->n_ports; host->ports[i]; i++) |
| 5730 | kfree(host->ports[i]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5731 | |
Tejun Heo | f318719 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5732 | /* give ports names and add SCSI hosts */ |
David Milburn | e628dc9 | 2013-05-14 13:48:40 -0500 | [diff] [blame] | 5733 | for (i = 0; i < host->n_ports; i++) { |
Dan Williams | 85d6725 | 2012-03-10 23:28:46 -0800 | [diff] [blame] | 5734 | host->ports[i]->print_id = atomic_inc_return(&ata_print_id); |
David Milburn | e628dc9 | 2013-05-14 13:48:40 -0500 | [diff] [blame] | 5735 | host->ports[i]->local_port_no = i + 1; |
| 5736 | } |
Jeff Garzik | 4fca377 | 2011-02-15 01:13:24 -0500 | [diff] [blame] | 5737 | |
Gwendal Grignou | d902747 | 2010-05-25 12:31:38 -0700 | [diff] [blame] | 5738 | /* Create associated sysfs transport objects */ |
| 5739 | for (i = 0; i < host->n_ports; i++) { |
| 5740 | rc = ata_tport_add(host->dev,host->ports[i]); |
| 5741 | if (rc) { |
| 5742 | goto err_tadd; |
| 5743 | } |
| 5744 | } |
| 5745 | |
Tejun Heo | f318719 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5746 | rc = ata_scsi_add_hosts(host, sht); |
Tejun Heo | ecef725 | 2007-04-17 23:44:06 +0900 | [diff] [blame] | 5747 | if (rc) |
Gwendal Grignou | d902747 | 2010-05-25 12:31:38 -0700 | [diff] [blame] | 5748 | goto err_tadd; |
Tejun Heo | ecef725 | 2007-04-17 23:44:06 +0900 | [diff] [blame] | 5749 | |
Tejun Heo | f318719 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5750 | /* set cable, sata_spd_limit and report */ |
Jeff Garzik | cca3974 | 2006-08-24 03:19:22 -0400 | [diff] [blame] | 5751 | for (i = 0; i < host->n_ports; i++) { |
| 5752 | struct ata_port *ap = host->ports[i]; |
Tejun Heo | f318719 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5753 | unsigned long xfer_mask; |
| 5754 | |
| 5755 | /* set SATA cable type if still unset */ |
| 5756 | if (ap->cbl == ATA_CBL_NONE && (ap->flags & ATA_FLAG_SATA)) |
| 5757 | ap->cbl = ATA_CBL_SATA; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5758 | |
Tejun Heo | 5a04bf4 | 2006-05-31 18:27:38 +0900 | [diff] [blame] | 5759 | /* init sata_spd_limit to the current value */ |
Tejun Heo | 4fb37a2 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 5760 | sata_link_init_spd(&ap->link); |
Tejun Heo | b1c7291 | 2008-07-31 17:02:43 +0900 | [diff] [blame] | 5761 | if (ap->slave_link) |
| 5762 | sata_link_init_spd(ap->slave_link); |
Tejun Heo | 5a04bf4 | 2006-05-31 18:27:38 +0900 | [diff] [blame] | 5763 | |
Tejun Heo | cbcdd87 | 2007-08-18 13:14:55 +0900 | [diff] [blame] | 5764 | /* print per-port info to dmesg */ |
Tejun Heo | f318719 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5765 | xfer_mask = ata_pack_xfermask(ap->pio_mask, ap->mwdma_mask, |
| 5766 | ap->udma_mask); |
| 5767 | |
Tejun Heo | abf6e8e | 2007-10-09 14:57:25 +0900 | [diff] [blame] | 5768 | if (!ata_port_is_dummy(ap)) { |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 5769 | ata_port_info(ap, "%cATA max %s %s\n", |
| 5770 | (ap->flags & ATA_FLAG_SATA) ? 'S' : 'P', |
| 5771 | ata_mode_string(xfer_mask), |
| 5772 | ap->link.eh_info.desc); |
Tejun Heo | abf6e8e | 2007-10-09 14:57:25 +0900 | [diff] [blame] | 5773 | ata_ehi_clear_desc(&ap->link.eh_info); |
| 5774 | } else |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 5775 | ata_port_info(ap, "DUMMY\n"); |
Tejun Heo | f318719 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5776 | } |
| 5777 | |
Vegard Nossum | f600535 | 2009-04-08 18:19:39 +0200 | [diff] [blame] | 5778 | /* perform each probe asynchronously */ |
Tejun Heo | f318719 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5779 | for (i = 0; i < host->n_ports; i++) { |
| 5780 | struct ata_port *ap = host->ports[i]; |
Arjan van de Ven | 7931805 | 2009-01-04 05:32:28 -0800 | [diff] [blame] | 5781 | async_schedule(async_port_probe, ap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5782 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5783 | |
Tejun Heo | f318719 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5784 | return 0; |
Gwendal Grignou | d902747 | 2010-05-25 12:31:38 -0700 | [diff] [blame] | 5785 | |
| 5786 | err_tadd: |
| 5787 | while (--i >= 0) { |
| 5788 | ata_tport_delete(host->ports[i]); |
| 5789 | } |
| 5790 | return rc; |
| 5791 | |
Tejun Heo | f318719 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5792 | } |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 5793 | EXPORT_SYMBOL_GPL(ata_host_register); |
Tejun Heo | f318719 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5794 | |
| 5795 | /** |
Tejun Heo | f5cda25 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5796 | * ata_host_activate - start host, request IRQ and register it |
| 5797 | * @host: target ATA host |
| 5798 | * @irq: IRQ to request |
| 5799 | * @irq_handler: irq_handler used when requesting IRQ |
| 5800 | * @irq_flags: irq_flags used when requesting IRQ |
| 5801 | * @sht: scsi_host_template to use when registering the host |
| 5802 | * |
| 5803 | * After allocating an ATA host and initializing it, most libata |
| 5804 | * LLDs perform three steps to activate the host - start host, |
Masanari Iida | c9b5560 | 2016-04-13 23:36:27 +0900 | [diff] [blame] | 5805 | * request IRQ and register it. This helper takes necessary |
Tejun Heo | f5cda25 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5806 | * arguments and performs the three steps in one go. |
| 5807 | * |
Paul Mundt | 3d46b2e | 2007-11-08 11:14:56 +0900 | [diff] [blame] | 5808 | * An invalid IRQ skips the IRQ registration and expects the host to |
| 5809 | * have set polling mode on the port. In this case, @irq_handler |
| 5810 | * should be NULL. |
| 5811 | * |
Tejun Heo | f5cda25 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5812 | * LOCKING: |
| 5813 | * Inherited from calling layer (may sleep). |
| 5814 | * |
| 5815 | * RETURNS: |
| 5816 | * 0 on success, -errno otherwise. |
| 5817 | */ |
| 5818 | int ata_host_activate(struct ata_host *host, int irq, |
| 5819 | irq_handler_t irq_handler, unsigned long irq_flags, |
| 5820 | struct scsi_host_template *sht) |
| 5821 | { |
Tejun Heo | cbcdd87 | 2007-08-18 13:14:55 +0900 | [diff] [blame] | 5822 | int i, rc; |
Heiner Kallweit | 7e22c00 | 2015-12-06 21:56:33 +0100 | [diff] [blame] | 5823 | char *irq_desc; |
Tejun Heo | f5cda25 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5824 | |
| 5825 | rc = ata_host_start(host); |
| 5826 | if (rc) |
| 5827 | return rc; |
| 5828 | |
Paul Mundt | 3d46b2e | 2007-11-08 11:14:56 +0900 | [diff] [blame] | 5829 | /* Special case for polling mode */ |
| 5830 | if (!irq) { |
| 5831 | WARN_ON(irq_handler); |
| 5832 | return ata_host_register(host, sht); |
| 5833 | } |
| 5834 | |
Heiner Kallweit | 7e22c00 | 2015-12-06 21:56:33 +0100 | [diff] [blame] | 5835 | irq_desc = devm_kasprintf(host->dev, GFP_KERNEL, "%s[%s]", |
| 5836 | dev_driver_string(host->dev), |
| 5837 | dev_name(host->dev)); |
| 5838 | if (!irq_desc) |
| 5839 | return -ENOMEM; |
| 5840 | |
Tejun Heo | f5cda25 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5841 | rc = devm_request_irq(host->dev, irq, irq_handler, irq_flags, |
Heiner Kallweit | 7e22c00 | 2015-12-06 21:56:33 +0100 | [diff] [blame] | 5842 | irq_desc, host); |
Tejun Heo | f5cda25 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5843 | if (rc) |
| 5844 | return rc; |
| 5845 | |
Tejun Heo | cbcdd87 | 2007-08-18 13:14:55 +0900 | [diff] [blame] | 5846 | for (i = 0; i < host->n_ports; i++) |
| 5847 | ata_port_desc(host->ports[i], "irq %d", irq); |
Tejun Heo | 4031826 | 2007-07-03 01:38:47 +0900 | [diff] [blame] | 5848 | |
Tejun Heo | f5cda25 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5849 | rc = ata_host_register(host, sht); |
| 5850 | /* if failed, just free the IRQ and leave ports alone */ |
| 5851 | if (rc) |
| 5852 | devm_free_irq(host->dev, irq, host); |
| 5853 | |
| 5854 | return rc; |
| 5855 | } |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 5856 | EXPORT_SYMBOL_GPL(ata_host_activate); |
Tejun Heo | f5cda25 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 5857 | |
| 5858 | /** |
Masanari Iida | c9b5560 | 2016-04-13 23:36:27 +0900 | [diff] [blame] | 5859 | * ata_port_detach - Detach ATA port in preparation of device removal |
Tejun Heo | 720ba12 | 2006-05-31 18:28:13 +0900 | [diff] [blame] | 5860 | * @ap: ATA port to be detached |
| 5861 | * |
| 5862 | * Detach all ATA devices and the associated SCSI devices of @ap; |
| 5863 | * then, remove the associated SCSI host. @ap is guaranteed to |
| 5864 | * be quiescent on return from this function. |
| 5865 | * |
| 5866 | * LOCKING: |
| 5867 | * Kernel thread context (may sleep). |
| 5868 | */ |
Adrian Bunk | 741b776 | 2007-10-24 18:23:06 +0200 | [diff] [blame] | 5869 | static void ata_port_detach(struct ata_port *ap) |
Tejun Heo | 720ba12 | 2006-05-31 18:28:13 +0900 | [diff] [blame] | 5870 | { |
| 5871 | unsigned long flags; |
Levente Kurusa | a6f9bf4 | 2014-05-06 15:57:48 +0200 | [diff] [blame] | 5872 | struct ata_link *link; |
| 5873 | struct ata_device *dev; |
Tejun Heo | 720ba12 | 2006-05-31 18:28:13 +0900 | [diff] [blame] | 5874 | |
| 5875 | if (!ap->ops->error_handler) |
Tejun Heo | c3cf30a | 2006-08-05 03:59:11 +0900 | [diff] [blame] | 5876 | goto skip_eh; |
Tejun Heo | 720ba12 | 2006-05-31 18:28:13 +0900 | [diff] [blame] | 5877 | |
| 5878 | /* tell EH we're leaving & flush EH */ |
Jeff Garzik | ba6a130 | 2006-06-22 23:46:10 -0400 | [diff] [blame] | 5879 | spin_lock_irqsave(ap->lock, flags); |
Tejun Heo | b51e9e5 | 2006-06-29 01:29:30 +0900 | [diff] [blame] | 5880 | ap->pflags |= ATA_PFLAG_UNLOADING; |
Tejun Heo | ece180d | 2008-11-03 20:04:37 +0900 | [diff] [blame] | 5881 | ata_port_schedule_eh(ap); |
Jeff Garzik | ba6a130 | 2006-06-22 23:46:10 -0400 | [diff] [blame] | 5882 | spin_unlock_irqrestore(ap->lock, flags); |
Tejun Heo | 720ba12 | 2006-05-31 18:28:13 +0900 | [diff] [blame] | 5883 | |
Tejun Heo | ece180d | 2008-11-03 20:04:37 +0900 | [diff] [blame] | 5884 | /* wait till EH commits suicide */ |
Tejun Heo | 720ba12 | 2006-05-31 18:28:13 +0900 | [diff] [blame] | 5885 | ata_port_wait_eh(ap); |
| 5886 | |
Tejun Heo | ece180d | 2008-11-03 20:04:37 +0900 | [diff] [blame] | 5887 | /* it better be dead now */ |
| 5888 | WARN_ON(!(ap->pflags & ATA_PFLAG_UNLOADED)); |
Tejun Heo | 720ba12 | 2006-05-31 18:28:13 +0900 | [diff] [blame] | 5889 | |
Tejun Heo | afe2c51 | 2010-12-14 16:21:17 +0100 | [diff] [blame] | 5890 | cancel_delayed_work_sync(&ap->hotplug_task); |
Tejun Heo | 720ba12 | 2006-05-31 18:28:13 +0900 | [diff] [blame] | 5891 | |
Tejun Heo | c3cf30a | 2006-08-05 03:59:11 +0900 | [diff] [blame] | 5892 | skip_eh: |
Levente Kurusa | a6f9bf4 | 2014-05-06 15:57:48 +0200 | [diff] [blame] | 5893 | /* clean up zpodd on port removal */ |
| 5894 | ata_for_each_link(link, ap, HOST_FIRST) { |
| 5895 | ata_for_each_dev(dev, link, ALL) { |
| 5896 | if (zpodd_dev_enabled(dev)) |
| 5897 | zpodd_exit(dev); |
| 5898 | } |
| 5899 | } |
Gwendal Grignou | d902747 | 2010-05-25 12:31:38 -0700 | [diff] [blame] | 5900 | if (ap->pmp_link) { |
| 5901 | int i; |
| 5902 | for (i = 0; i < SATA_PMP_MAX_PORTS; i++) |
| 5903 | ata_tlink_delete(&ap->pmp_link[i]); |
| 5904 | } |
Tejun Heo | 720ba12 | 2006-05-31 18:28:13 +0900 | [diff] [blame] | 5905 | /* remove the associated SCSI host */ |
Jeff Garzik | cca3974 | 2006-08-24 03:19:22 -0400 | [diff] [blame] | 5906 | scsi_remove_host(ap->scsi_host); |
Rafael J. Wysocki | c570076 | 2013-11-25 13:19:01 +0100 | [diff] [blame] | 5907 | ata_tport_delete(ap); |
Tejun Heo | 720ba12 | 2006-05-31 18:28:13 +0900 | [diff] [blame] | 5908 | } |
| 5909 | |
| 5910 | /** |
Tejun Heo | 0529c159 | 2007-01-20 16:00:26 +0900 | [diff] [blame] | 5911 | * ata_host_detach - Detach all ports of an ATA host |
| 5912 | * @host: Host to detach |
| 5913 | * |
| 5914 | * Detach all ports of @host. |
| 5915 | * |
| 5916 | * LOCKING: |
| 5917 | * Kernel thread context (may sleep). |
| 5918 | */ |
| 5919 | void ata_host_detach(struct ata_host *host) |
| 5920 | { |
| 5921 | int i; |
| 5922 | |
John Garry | 130f4ca | 2019-10-16 18:19:52 +0800 | [diff] [blame] | 5923 | /* Ensure ata_port probe has completed */ |
| 5924 | async_synchronize_full(); |
| 5925 | |
Tejun Heo | 0529c159 | 2007-01-20 16:00:26 +0900 | [diff] [blame] | 5926 | for (i = 0; i < host->n_ports; i++) |
| 5927 | ata_port_detach(host->ports[i]); |
Tejun Heo | 562f0c2 | 2007-12-15 15:05:01 +0900 | [diff] [blame] | 5928 | |
| 5929 | /* the host is dead now, dissociate ACPI */ |
| 5930 | ata_acpi_dissociate(host); |
Tejun Heo | 0529c159 | 2007-01-20 16:00:26 +0900 | [diff] [blame] | 5931 | } |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 5932 | EXPORT_SYMBOL_GPL(ata_host_detach); |
Tejun Heo | 0529c159 | 2007-01-20 16:00:26 +0900 | [diff] [blame] | 5933 | |
Jeff Garzik | 374b187 | 2005-08-30 05:42:52 -0400 | [diff] [blame] | 5934 | #ifdef CONFIG_PCI |
| 5935 | |
Edward Falk | 0baab86 | 2005-06-02 18:17:13 -0400 | [diff] [blame] | 5936 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5937 | * ata_pci_remove_one - PCI layer callback for device removal |
| 5938 | * @pdev: PCI device that was removed |
| 5939 | * |
Tejun Heo | b878ca5 | 2007-01-20 16:00:28 +0900 | [diff] [blame] | 5940 | * PCI layer indicates to libata via this hook that hot-unplug or |
| 5941 | * module unload event has occurred. Detach all ports. Resource |
| 5942 | * release is handled via devres. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5943 | * |
| 5944 | * LOCKING: |
| 5945 | * Inherited from PCI layer (may sleep). |
| 5946 | */ |
Tejun Heo | f0d36ef | 2007-01-20 16:00:28 +0900 | [diff] [blame] | 5947 | void ata_pci_remove_one(struct pci_dev *pdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5948 | { |
Brian Norris | 04a3f5b | 2012-12-03 10:34:41 -0800 | [diff] [blame] | 5949 | struct ata_host *host = pci_get_drvdata(pdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5950 | |
Tejun Heo | b878ca5 | 2007-01-20 16:00:28 +0900 | [diff] [blame] | 5951 | ata_host_detach(host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5952 | } |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 5953 | EXPORT_SYMBOL_GPL(ata_pci_remove_one); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5954 | |
Prabhakar Kushwaha | 10a663a | 2020-01-25 03:37:29 +0000 | [diff] [blame] | 5955 | void ata_pci_shutdown_one(struct pci_dev *pdev) |
| 5956 | { |
| 5957 | struct ata_host *host = pci_get_drvdata(pdev); |
| 5958 | int i; |
| 5959 | |
| 5960 | for (i = 0; i < host->n_ports; i++) { |
| 5961 | struct ata_port *ap = host->ports[i]; |
| 5962 | |
| 5963 | ap->pflags |= ATA_PFLAG_FROZEN; |
| 5964 | |
| 5965 | /* Disable port interrupts */ |
| 5966 | if (ap->ops->freeze) |
| 5967 | ap->ops->freeze(ap); |
| 5968 | |
| 5969 | /* Stop the port DMA engines */ |
| 5970 | if (ap->ops->port_stop) |
| 5971 | ap->ops->port_stop(ap); |
| 5972 | } |
| 5973 | } |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 5974 | EXPORT_SYMBOL_GPL(ata_pci_shutdown_one); |
Prabhakar Kushwaha | 10a663a | 2020-01-25 03:37:29 +0000 | [diff] [blame] | 5975 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5976 | /* move to PCI subsystem */ |
Jeff Garzik | 057ace5 | 2005-10-22 14:27:05 -0400 | [diff] [blame] | 5977 | 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] | 5978 | { |
| 5979 | unsigned long tmp = 0; |
| 5980 | |
| 5981 | switch (bits->width) { |
| 5982 | case 1: { |
| 5983 | u8 tmp8 = 0; |
| 5984 | pci_read_config_byte(pdev, bits->reg, &tmp8); |
| 5985 | tmp = tmp8; |
| 5986 | break; |
| 5987 | } |
| 5988 | case 2: { |
| 5989 | u16 tmp16 = 0; |
| 5990 | pci_read_config_word(pdev, bits->reg, &tmp16); |
| 5991 | tmp = tmp16; |
| 5992 | break; |
| 5993 | } |
| 5994 | case 4: { |
| 5995 | u32 tmp32 = 0; |
| 5996 | pci_read_config_dword(pdev, bits->reg, &tmp32); |
| 5997 | tmp = tmp32; |
| 5998 | break; |
| 5999 | } |
| 6000 | |
| 6001 | default: |
| 6002 | return -EINVAL; |
| 6003 | } |
| 6004 | |
| 6005 | tmp &= bits->mask; |
| 6006 | |
| 6007 | return (tmp == bits->val) ? 1 : 0; |
| 6008 | } |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 6009 | EXPORT_SYMBOL_GPL(pci_test_config_bits); |
Jens Axboe | 9b84754 | 2006-01-06 09:28:07 +0100 | [diff] [blame] | 6010 | |
Tejun Heo | 6ffa01d | 2007-03-02 17:32:47 +0900 | [diff] [blame] | 6011 | #ifdef CONFIG_PM |
Tejun Heo | 3c5100c | 2006-07-26 16:58:33 +0900 | [diff] [blame] | 6012 | 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] | 6013 | { |
| 6014 | pci_save_state(pdev); |
Tejun Heo | 4c90d97 | 2007-02-20 18:14:48 +0900 | [diff] [blame] | 6015 | pci_disable_device(pdev); |
Tejun Heo | 500530f | 2006-07-03 16:07:27 +0900 | [diff] [blame] | 6016 | |
Rafael J. Wysocki | 3a2d5b7 | 2008-02-23 19:13:25 +0100 | [diff] [blame] | 6017 | if (mesg.event & PM_EVENT_SLEEP) |
Tejun Heo | 500530f | 2006-07-03 16:07:27 +0900 | [diff] [blame] | 6018 | pci_set_power_state(pdev, PCI_D3hot); |
Jens Axboe | 9b84754 | 2006-01-06 09:28:07 +0100 | [diff] [blame] | 6019 | } |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 6020 | EXPORT_SYMBOL_GPL(ata_pci_device_do_suspend); |
Jens Axboe | 9b84754 | 2006-01-06 09:28:07 +0100 | [diff] [blame] | 6021 | |
Tejun Heo | 553c4aa | 2006-12-26 19:39:50 +0900 | [diff] [blame] | 6022 | int ata_pci_device_do_resume(struct pci_dev *pdev) |
Jens Axboe | 9b84754 | 2006-01-06 09:28:07 +0100 | [diff] [blame] | 6023 | { |
Tejun Heo | 553c4aa | 2006-12-26 19:39:50 +0900 | [diff] [blame] | 6024 | int rc; |
| 6025 | |
Jens Axboe | 9b84754 | 2006-01-06 09:28:07 +0100 | [diff] [blame] | 6026 | pci_set_power_state(pdev, PCI_D0); |
| 6027 | pci_restore_state(pdev); |
Tejun Heo | 553c4aa | 2006-12-26 19:39:50 +0900 | [diff] [blame] | 6028 | |
Tejun Heo | b878ca5 | 2007-01-20 16:00:28 +0900 | [diff] [blame] | 6029 | rc = pcim_enable_device(pdev); |
Tejun Heo | 553c4aa | 2006-12-26 19:39:50 +0900 | [diff] [blame] | 6030 | if (rc) { |
Joe Perches | a44fec1 | 2011-04-15 15:51:58 -0700 | [diff] [blame] | 6031 | dev_err(&pdev->dev, |
| 6032 | "failed to enable device after resume (%d)\n", rc); |
Tejun Heo | 553c4aa | 2006-12-26 19:39:50 +0900 | [diff] [blame] | 6033 | return rc; |
| 6034 | } |
| 6035 | |
Jens Axboe | 9b84754 | 2006-01-06 09:28:07 +0100 | [diff] [blame] | 6036 | pci_set_master(pdev); |
Tejun Heo | 553c4aa | 2006-12-26 19:39:50 +0900 | [diff] [blame] | 6037 | return 0; |
Tejun Heo | 500530f | 2006-07-03 16:07:27 +0900 | [diff] [blame] | 6038 | } |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 6039 | EXPORT_SYMBOL_GPL(ata_pci_device_do_resume); |
Tejun Heo | 500530f | 2006-07-03 16:07:27 +0900 | [diff] [blame] | 6040 | |
Tejun Heo | 3c5100c | 2006-07-26 16:58:33 +0900 | [diff] [blame] | 6041 | 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] | 6042 | { |
Brian Norris | 04a3f5b | 2012-12-03 10:34:41 -0800 | [diff] [blame] | 6043 | struct ata_host *host = pci_get_drvdata(pdev); |
Tejun Heo | 500530f | 2006-07-03 16:07:27 +0900 | [diff] [blame] | 6044 | int rc = 0; |
| 6045 | |
Jeff Garzik | cca3974 | 2006-08-24 03:19:22 -0400 | [diff] [blame] | 6046 | rc = ata_host_suspend(host, mesg); |
Tejun Heo | 500530f | 2006-07-03 16:07:27 +0900 | [diff] [blame] | 6047 | if (rc) |
| 6048 | return rc; |
| 6049 | |
Tejun Heo | 3c5100c | 2006-07-26 16:58:33 +0900 | [diff] [blame] | 6050 | ata_pci_device_do_suspend(pdev, mesg); |
Tejun Heo | 500530f | 2006-07-03 16:07:27 +0900 | [diff] [blame] | 6051 | |
| 6052 | return 0; |
| 6053 | } |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 6054 | EXPORT_SYMBOL_GPL(ata_pci_device_suspend); |
Tejun Heo | 500530f | 2006-07-03 16:07:27 +0900 | [diff] [blame] | 6055 | |
| 6056 | int ata_pci_device_resume(struct pci_dev *pdev) |
| 6057 | { |
Brian Norris | 04a3f5b | 2012-12-03 10:34:41 -0800 | [diff] [blame] | 6058 | struct ata_host *host = pci_get_drvdata(pdev); |
Tejun Heo | 553c4aa | 2006-12-26 19:39:50 +0900 | [diff] [blame] | 6059 | int rc; |
Tejun Heo | 500530f | 2006-07-03 16:07:27 +0900 | [diff] [blame] | 6060 | |
Tejun Heo | 553c4aa | 2006-12-26 19:39:50 +0900 | [diff] [blame] | 6061 | rc = ata_pci_device_do_resume(pdev); |
| 6062 | if (rc == 0) |
| 6063 | ata_host_resume(host); |
| 6064 | return rc; |
Jens Axboe | 9b84754 | 2006-01-06 09:28:07 +0100 | [diff] [blame] | 6065 | } |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 6066 | EXPORT_SYMBOL_GPL(ata_pci_device_resume); |
Tejun Heo | 6ffa01d | 2007-03-02 17:32:47 +0900 | [diff] [blame] | 6067 | #endif /* CONFIG_PM */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6068 | #endif /* CONFIG_PCI */ |
| 6069 | |
Brian Norris | b7db04d | 2012-11-02 12:29:32 -0700 | [diff] [blame] | 6070 | /** |
| 6071 | * ata_platform_remove_one - Platform layer callback for device removal |
| 6072 | * @pdev: Platform device that was removed |
| 6073 | * |
| 6074 | * Platform layer indicates to libata via this hook that hot-unplug or |
| 6075 | * module unload event has occurred. Detach all ports. Resource |
| 6076 | * release is handled via devres. |
| 6077 | * |
| 6078 | * LOCKING: |
| 6079 | * Inherited from platform layer (may sleep). |
| 6080 | */ |
| 6081 | int ata_platform_remove_one(struct platform_device *pdev) |
| 6082 | { |
| 6083 | struct ata_host *host = platform_get_drvdata(pdev); |
| 6084 | |
| 6085 | ata_host_detach(host); |
| 6086 | |
| 6087 | return 0; |
| 6088 | } |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 6089 | EXPORT_SYMBOL_GPL(ata_platform_remove_one); |
Brian Norris | b7db04d | 2012-11-02 12:29:32 -0700 | [diff] [blame] | 6090 | |
Bartlomiej Zolnierkiewicz | bf89b0b | 2020-03-26 16:58:22 +0100 | [diff] [blame] | 6091 | #ifdef CONFIG_ATA_FORCE |
Tejun Heo | 3326732 | 2008-02-13 09:15:09 +0900 | [diff] [blame] | 6092 | static int __init ata_parse_force_one(char **cur, |
| 6093 | struct ata_force_ent *force_ent, |
| 6094 | const char **reason) |
| 6095 | { |
Rasmus Villemoes | 0f5f264 | 2015-06-09 15:33:19 +0200 | [diff] [blame] | 6096 | static const struct ata_force_param force_tbl[] __initconst = { |
Tejun Heo | 3326732 | 2008-02-13 09:15:09 +0900 | [diff] [blame] | 6097 | { "40c", .cbl = ATA_CBL_PATA40 }, |
| 6098 | { "80c", .cbl = ATA_CBL_PATA80 }, |
| 6099 | { "short40c", .cbl = ATA_CBL_PATA40_SHORT }, |
| 6100 | { "unk", .cbl = ATA_CBL_PATA_UNK }, |
| 6101 | { "ign", .cbl = ATA_CBL_PATA_IGN }, |
| 6102 | { "sata", .cbl = ATA_CBL_SATA }, |
| 6103 | { "1.5Gbps", .spd_limit = 1 }, |
| 6104 | { "3.0Gbps", .spd_limit = 2 }, |
| 6105 | { "noncq", .horkage_on = ATA_HORKAGE_NONCQ }, |
| 6106 | { "ncq", .horkage_off = ATA_HORKAGE_NONCQ }, |
Martin K. Petersen | d7b16e4 | 2015-05-04 21:54:18 -0400 | [diff] [blame] | 6107 | { "noncqtrim", .horkage_on = ATA_HORKAGE_NO_NCQ_TRIM }, |
| 6108 | { "ncqtrim", .horkage_off = ATA_HORKAGE_NO_NCQ_TRIM }, |
Tejun Heo | 43c9c59 | 2010-05-23 12:59:11 +0200 | [diff] [blame] | 6109 | { "dump_id", .horkage_on = ATA_HORKAGE_DUMP_ID }, |
Tejun Heo | 3326732 | 2008-02-13 09:15:09 +0900 | [diff] [blame] | 6110 | { "pio0", .xfer_mask = 1 << (ATA_SHIFT_PIO + 0) }, |
| 6111 | { "pio1", .xfer_mask = 1 << (ATA_SHIFT_PIO + 1) }, |
| 6112 | { "pio2", .xfer_mask = 1 << (ATA_SHIFT_PIO + 2) }, |
| 6113 | { "pio3", .xfer_mask = 1 << (ATA_SHIFT_PIO + 3) }, |
| 6114 | { "pio4", .xfer_mask = 1 << (ATA_SHIFT_PIO + 4) }, |
| 6115 | { "pio5", .xfer_mask = 1 << (ATA_SHIFT_PIO + 5) }, |
| 6116 | { "pio6", .xfer_mask = 1 << (ATA_SHIFT_PIO + 6) }, |
| 6117 | { "mwdma0", .xfer_mask = 1 << (ATA_SHIFT_MWDMA + 0) }, |
| 6118 | { "mwdma1", .xfer_mask = 1 << (ATA_SHIFT_MWDMA + 1) }, |
| 6119 | { "mwdma2", .xfer_mask = 1 << (ATA_SHIFT_MWDMA + 2) }, |
| 6120 | { "mwdma3", .xfer_mask = 1 << (ATA_SHIFT_MWDMA + 3) }, |
| 6121 | { "mwdma4", .xfer_mask = 1 << (ATA_SHIFT_MWDMA + 4) }, |
| 6122 | { "udma0", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 0) }, |
| 6123 | { "udma16", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 0) }, |
| 6124 | { "udma/16", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 0) }, |
| 6125 | { "udma1", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 1) }, |
| 6126 | { "udma25", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 1) }, |
| 6127 | { "udma/25", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 1) }, |
| 6128 | { "udma2", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 2) }, |
| 6129 | { "udma33", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 2) }, |
| 6130 | { "udma/33", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 2) }, |
| 6131 | { "udma3", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 3) }, |
| 6132 | { "udma44", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 3) }, |
| 6133 | { "udma/44", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 3) }, |
| 6134 | { "udma4", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 4) }, |
| 6135 | { "udma66", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 4) }, |
| 6136 | { "udma/66", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 4) }, |
| 6137 | { "udma5", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 5) }, |
| 6138 | { "udma100", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 5) }, |
| 6139 | { "udma/100", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 5) }, |
| 6140 | { "udma6", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 6) }, |
| 6141 | { "udma133", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 6) }, |
| 6142 | { "udma/133", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 6) }, |
| 6143 | { "udma7", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 7) }, |
Tejun Heo | 05944bd | 2008-08-13 20:19:09 +0900 | [diff] [blame] | 6144 | { "nohrst", .lflags = ATA_LFLAG_NO_HRST }, |
| 6145 | { "nosrst", .lflags = ATA_LFLAG_NO_SRST }, |
| 6146 | { "norst", .lflags = ATA_LFLAG_NO_HRST | ATA_LFLAG_NO_SRST }, |
Dan Williams | ca6d43b | 2012-06-21 23:41:41 -0700 | [diff] [blame] | 6147 | { "rstonce", .lflags = ATA_LFLAG_RST_ONCE }, |
Vincent Pelletier | 966fbe1 | 2013-05-21 22:30:58 +0200 | [diff] [blame] | 6148 | { "atapi_dmadir", .horkage_on = ATA_HORKAGE_ATAPI_DMADIR }, |
Robin H. Johnson | b8bd6dc | 2013-12-16 09:31:19 -0800 | [diff] [blame] | 6149 | { "disable", .horkage_on = ATA_HORKAGE_DISABLE }, |
Tejun Heo | 3326732 | 2008-02-13 09:15:09 +0900 | [diff] [blame] | 6150 | }; |
| 6151 | char *start = *cur, *p = *cur; |
| 6152 | char *id, *val, *endp; |
| 6153 | const struct ata_force_param *match_fp = NULL; |
| 6154 | int nr_matches = 0, i; |
| 6155 | |
| 6156 | /* find where this param ends and update *cur */ |
| 6157 | while (*p != '\0' && *p != ',') |
| 6158 | p++; |
| 6159 | |
| 6160 | if (*p == '\0') |
| 6161 | *cur = p; |
| 6162 | else |
| 6163 | *cur = p + 1; |
| 6164 | |
| 6165 | *p = '\0'; |
| 6166 | |
| 6167 | /* parse */ |
| 6168 | p = strchr(start, ':'); |
| 6169 | if (!p) { |
| 6170 | val = strstrip(start); |
| 6171 | goto parse_val; |
| 6172 | } |
| 6173 | *p = '\0'; |
| 6174 | |
| 6175 | id = strstrip(start); |
| 6176 | val = strstrip(p + 1); |
| 6177 | |
| 6178 | /* parse id */ |
| 6179 | p = strchr(id, '.'); |
| 6180 | if (p) { |
| 6181 | *p++ = '\0'; |
| 6182 | force_ent->device = simple_strtoul(p, &endp, 10); |
| 6183 | if (p == endp || *endp != '\0') { |
| 6184 | *reason = "invalid device"; |
| 6185 | return -EINVAL; |
| 6186 | } |
| 6187 | } |
| 6188 | |
| 6189 | force_ent->port = simple_strtoul(id, &endp, 10); |
Tejun Heo | f7cf69a | 2017-05-31 14:26:26 -0400 | [diff] [blame] | 6190 | if (id == endp || *endp != '\0') { |
Tejun Heo | 3326732 | 2008-02-13 09:15:09 +0900 | [diff] [blame] | 6191 | *reason = "invalid port/link"; |
| 6192 | return -EINVAL; |
| 6193 | } |
| 6194 | |
| 6195 | parse_val: |
| 6196 | /* parse val, allow shortcuts so that both 1.5 and 1.5Gbps work */ |
| 6197 | for (i = 0; i < ARRAY_SIZE(force_tbl); i++) { |
| 6198 | const struct ata_force_param *fp = &force_tbl[i]; |
| 6199 | |
| 6200 | if (strncasecmp(val, fp->name, strlen(val))) |
| 6201 | continue; |
| 6202 | |
| 6203 | nr_matches++; |
| 6204 | match_fp = fp; |
| 6205 | |
| 6206 | if (strcasecmp(val, fp->name) == 0) { |
| 6207 | nr_matches = 1; |
| 6208 | break; |
| 6209 | } |
| 6210 | } |
| 6211 | |
| 6212 | if (!nr_matches) { |
| 6213 | *reason = "unknown value"; |
| 6214 | return -EINVAL; |
| 6215 | } |
| 6216 | if (nr_matches > 1) { |
Arvind Yadav | 9de5535 | 2017-10-28 23:51:47 +0530 | [diff] [blame] | 6217 | *reason = "ambiguous value"; |
Tejun Heo | 3326732 | 2008-02-13 09:15:09 +0900 | [diff] [blame] | 6218 | return -EINVAL; |
| 6219 | } |
| 6220 | |
| 6221 | force_ent->param = *match_fp; |
| 6222 | |
| 6223 | return 0; |
| 6224 | } |
| 6225 | |
| 6226 | static void __init ata_parse_force_param(void) |
| 6227 | { |
| 6228 | int idx = 0, size = 1; |
| 6229 | int last_port = -1, last_device = -1; |
| 6230 | char *p, *cur, *next; |
| 6231 | |
| 6232 | /* calculate maximum number of params and allocate force_tbl */ |
| 6233 | for (p = ata_force_param_buf; *p; p++) |
| 6234 | if (*p == ',') |
| 6235 | size++; |
| 6236 | |
Kees Cook | 6396bb2 | 2018-06-12 14:03:40 -0700 | [diff] [blame] | 6237 | ata_force_tbl = kcalloc(size, sizeof(ata_force_tbl[0]), GFP_KERNEL); |
Tejun Heo | 3326732 | 2008-02-13 09:15:09 +0900 | [diff] [blame] | 6238 | if (!ata_force_tbl) { |
| 6239 | printk(KERN_WARNING "ata: failed to extend force table, " |
| 6240 | "libata.force ignored\n"); |
| 6241 | return; |
| 6242 | } |
| 6243 | |
| 6244 | /* parse and populate the table */ |
| 6245 | for (cur = ata_force_param_buf; *cur != '\0'; cur = next) { |
| 6246 | const char *reason = ""; |
| 6247 | struct ata_force_ent te = { .port = -1, .device = -1 }; |
| 6248 | |
| 6249 | next = cur; |
| 6250 | if (ata_parse_force_one(&next, &te, &reason)) { |
| 6251 | printk(KERN_WARNING "ata: failed to parse force " |
| 6252 | "parameter \"%s\" (%s)\n", |
| 6253 | cur, reason); |
| 6254 | continue; |
| 6255 | } |
| 6256 | |
| 6257 | if (te.port == -1) { |
| 6258 | te.port = last_port; |
| 6259 | te.device = last_device; |
| 6260 | } |
| 6261 | |
| 6262 | ata_force_tbl[idx++] = te; |
| 6263 | |
| 6264 | last_port = te.port; |
| 6265 | last_device = te.device; |
| 6266 | } |
| 6267 | |
| 6268 | ata_force_tbl_size = idx; |
| 6269 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6270 | |
Bartlomiej Zolnierkiewicz | bf89b0b | 2020-03-26 16:58:22 +0100 | [diff] [blame] | 6271 | static void ata_free_force_param(void) |
| 6272 | { |
| 6273 | kfree(ata_force_tbl); |
| 6274 | } |
| 6275 | #else |
| 6276 | static inline void ata_parse_force_param(void) { } |
| 6277 | static inline void ata_free_force_param(void) { } |
| 6278 | #endif |
| 6279 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6280 | static int __init ata_init(void) |
| 6281 | { |
Gwendal Grignou | d902747 | 2010-05-25 12:31:38 -0700 | [diff] [blame] | 6282 | int rc; |
Tejun Heo | 270390e | 2010-05-10 21:41:35 +0200 | [diff] [blame] | 6283 | |
Tejun Heo | 3326732 | 2008-02-13 09:15:09 +0900 | [diff] [blame] | 6284 | ata_parse_force_param(); |
| 6285 | |
Tejun Heo | 270390e | 2010-05-10 21:41:35 +0200 | [diff] [blame] | 6286 | rc = ata_sff_init(); |
Tejun Heo | ad72cf9 | 2010-07-02 10:03:52 +0200 | [diff] [blame] | 6287 | if (rc) { |
Bartlomiej Zolnierkiewicz | bf89b0b | 2020-03-26 16:58:22 +0100 | [diff] [blame] | 6288 | ata_free_force_param(); |
Tejun Heo | ad72cf9 | 2010-07-02 10:03:52 +0200 | [diff] [blame] | 6289 | return rc; |
| 6290 | } |
Tejun Heo | 453b07a | 2006-05-31 18:27:42 +0900 | [diff] [blame] | 6291 | |
Gwendal Grignou | d902747 | 2010-05-25 12:31:38 -0700 | [diff] [blame] | 6292 | libata_transport_init(); |
| 6293 | ata_scsi_transport_template = ata_attach_transport(); |
| 6294 | if (!ata_scsi_transport_template) { |
| 6295 | ata_sff_exit(); |
| 6296 | rc = -ENOMEM; |
| 6297 | goto err_out; |
Jeff Garzik | 4fca377 | 2011-02-15 01:13:24 -0500 | [diff] [blame] | 6298 | } |
Gwendal Grignou | d902747 | 2010-05-25 12:31:38 -0700 | [diff] [blame] | 6299 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6300 | printk(KERN_DEBUG "libata version " DRV_VERSION " loaded.\n"); |
| 6301 | return 0; |
Gwendal Grignou | d902747 | 2010-05-25 12:31:38 -0700 | [diff] [blame] | 6302 | |
| 6303 | err_out: |
| 6304 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6305 | } |
| 6306 | |
| 6307 | static void __exit ata_exit(void) |
| 6308 | { |
Gwendal Grignou | d902747 | 2010-05-25 12:31:38 -0700 | [diff] [blame] | 6309 | ata_release_transport(ata_scsi_transport_template); |
| 6310 | libata_transport_exit(); |
Tejun Heo | 270390e | 2010-05-10 21:41:35 +0200 | [diff] [blame] | 6311 | ata_sff_exit(); |
Bartlomiej Zolnierkiewicz | bf89b0b | 2020-03-26 16:58:22 +0100 | [diff] [blame] | 6312 | ata_free_force_param(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6313 | } |
| 6314 | |
Brian King | a462508 | 2006-11-13 16:32:36 -0600 | [diff] [blame] | 6315 | subsys_initcall(ata_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6316 | module_exit(ata_exit); |
| 6317 | |
Akinobu Mita | 9990b6f | 2010-04-12 21:11:41 +0900 | [diff] [blame] | 6318 | static DEFINE_RATELIMIT_STATE(ratelimit, HZ / 5, 1); |
Jeff Garzik | 67846b3 | 2005-10-05 02:58:32 -0400 | [diff] [blame] | 6319 | |
| 6320 | int ata_ratelimit(void) |
| 6321 | { |
Akinobu Mita | 9990b6f | 2010-04-12 21:11:41 +0900 | [diff] [blame] | 6322 | return __ratelimit(&ratelimit); |
Jeff Garzik | 67846b3 | 2005-10-05 02:58:32 -0400 | [diff] [blame] | 6323 | } |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 6324 | EXPORT_SYMBOL_GPL(ata_ratelimit); |
Jeff Garzik | 67846b3 | 2005-10-05 02:58:32 -0400 | [diff] [blame] | 6325 | |
Tejun Heo | c0c362b | 2010-09-06 17:57:14 +0200 | [diff] [blame] | 6326 | /** |
| 6327 | * ata_msleep - ATA EH owner aware msleep |
| 6328 | * @ap: ATA port to attribute the sleep to |
| 6329 | * @msecs: duration to sleep in milliseconds |
| 6330 | * |
| 6331 | * Sleeps @msecs. If the current task is owner of @ap's EH, the |
| 6332 | * ownership is released before going to sleep and reacquired |
| 6333 | * after the sleep is complete. IOW, other ports sharing the |
| 6334 | * @ap->host will be allowed to own the EH while this task is |
| 6335 | * sleeping. |
| 6336 | * |
| 6337 | * LOCKING: |
| 6338 | * Might sleep. |
| 6339 | */ |
Tejun Heo | 97750ce | 2010-09-06 17:56:29 +0200 | [diff] [blame] | 6340 | void ata_msleep(struct ata_port *ap, unsigned int msecs) |
| 6341 | { |
Tejun Heo | c0c362b | 2010-09-06 17:57:14 +0200 | [diff] [blame] | 6342 | bool owns_eh = ap && ap->host->eh_owner == current; |
| 6343 | |
| 6344 | if (owns_eh) |
| 6345 | ata_eh_release(ap); |
| 6346 | |
Anil Veliyankara Madam | 848c392 | 2016-01-07 21:18:52 -0800 | [diff] [blame] | 6347 | if (msecs < 20) { |
| 6348 | unsigned long usecs = msecs * USEC_PER_MSEC; |
| 6349 | usleep_range(usecs, usecs + 50); |
| 6350 | } else { |
| 6351 | msleep(msecs); |
| 6352 | } |
Tejun Heo | c0c362b | 2010-09-06 17:57:14 +0200 | [diff] [blame] | 6353 | |
| 6354 | if (owns_eh) |
| 6355 | ata_eh_acquire(ap); |
Tejun Heo | 97750ce | 2010-09-06 17:56:29 +0200 | [diff] [blame] | 6356 | } |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 6357 | EXPORT_SYMBOL_GPL(ata_msleep); |
Tejun Heo | 97750ce | 2010-09-06 17:56:29 +0200 | [diff] [blame] | 6358 | |
Tejun Heo | c22daff | 2006-04-11 22:22:29 +0900 | [diff] [blame] | 6359 | /** |
| 6360 | * ata_wait_register - wait until register value changes |
Tejun Heo | 97750ce | 2010-09-06 17:56:29 +0200 | [diff] [blame] | 6361 | * @ap: ATA port to wait register for, can be NULL |
Tejun Heo | c22daff | 2006-04-11 22:22:29 +0900 | [diff] [blame] | 6362 | * @reg: IO-mapped register |
| 6363 | * @mask: Mask to apply to read register value |
| 6364 | * @val: Wait condition |
Tejun Heo | 341c2c9 | 2008-05-20 02:17:51 +0900 | [diff] [blame] | 6365 | * @interval: polling interval in milliseconds |
| 6366 | * @timeout: timeout in milliseconds |
Tejun Heo | c22daff | 2006-04-11 22:22:29 +0900 | [diff] [blame] | 6367 | * |
| 6368 | * Waiting for some bits of register to change is a common |
| 6369 | * operation for ATA controllers. This function reads 32bit LE |
| 6370 | * IO-mapped register @reg and tests for the following condition. |
| 6371 | * |
| 6372 | * (*@reg & mask) != val |
| 6373 | * |
| 6374 | * If the condition is met, it returns; otherwise, the process is |
| 6375 | * repeated after @interval_msec until timeout. |
| 6376 | * |
| 6377 | * LOCKING: |
| 6378 | * Kernel thread context (may sleep) |
| 6379 | * |
| 6380 | * RETURNS: |
| 6381 | * The final register value. |
| 6382 | */ |
Tejun Heo | 97750ce | 2010-09-06 17:56:29 +0200 | [diff] [blame] | 6383 | 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] | 6384 | unsigned long interval, unsigned long timeout) |
Tejun Heo | c22daff | 2006-04-11 22:22:29 +0900 | [diff] [blame] | 6385 | { |
Tejun Heo | 341c2c9 | 2008-05-20 02:17:51 +0900 | [diff] [blame] | 6386 | unsigned long deadline; |
Tejun Heo | c22daff | 2006-04-11 22:22:29 +0900 | [diff] [blame] | 6387 | u32 tmp; |
| 6388 | |
| 6389 | tmp = ioread32(reg); |
| 6390 | |
| 6391 | /* Calculate timeout _after_ the first read to make sure |
| 6392 | * preceding writes reach the controller before starting to |
| 6393 | * eat away the timeout. |
| 6394 | */ |
Tejun Heo | 341c2c9 | 2008-05-20 02:17:51 +0900 | [diff] [blame] | 6395 | deadline = ata_deadline(jiffies, timeout); |
Tejun Heo | c22daff | 2006-04-11 22:22:29 +0900 | [diff] [blame] | 6396 | |
Tejun Heo | 341c2c9 | 2008-05-20 02:17:51 +0900 | [diff] [blame] | 6397 | while ((tmp & mask) == val && time_before(jiffies, deadline)) { |
Tejun Heo | 97750ce | 2010-09-06 17:56:29 +0200 | [diff] [blame] | 6398 | ata_msleep(ap, interval); |
Tejun Heo | c22daff | 2006-04-11 22:22:29 +0900 | [diff] [blame] | 6399 | tmp = ioread32(reg); |
| 6400 | } |
| 6401 | |
| 6402 | return tmp; |
| 6403 | } |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 6404 | EXPORT_SYMBOL_GPL(ata_wait_register); |
Tejun Heo | c22daff | 2006-04-11 22:22:29 +0900 | [diff] [blame] | 6405 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6406 | /* |
Tejun Heo | dd5b06c | 2006-08-10 16:59:12 +0900 | [diff] [blame] | 6407 | * Dummy port_ops |
| 6408 | */ |
Tejun Heo | dd5b06c | 2006-08-10 16:59:12 +0900 | [diff] [blame] | 6409 | static unsigned int ata_dummy_qc_issue(struct ata_queued_cmd *qc) |
| 6410 | { |
| 6411 | return AC_ERR_SYSTEM; |
| 6412 | } |
| 6413 | |
Tejun Heo | 182d7bb | 2008-04-07 22:47:21 +0900 | [diff] [blame] | 6414 | static void ata_dummy_error_handler(struct ata_port *ap) |
| 6415 | { |
| 6416 | /* truly dummy */ |
| 6417 | } |
| 6418 | |
Tejun Heo | 029cfd6 | 2008-03-25 12:22:49 +0900 | [diff] [blame] | 6419 | struct ata_port_operations ata_dummy_port_ops = { |
Tejun Heo | dd5b06c | 2006-08-10 16:59:12 +0900 | [diff] [blame] | 6420 | .qc_prep = ata_noop_qc_prep, |
| 6421 | .qc_issue = ata_dummy_qc_issue, |
Tejun Heo | 182d7bb | 2008-04-07 22:47:21 +0900 | [diff] [blame] | 6422 | .error_handler = ata_dummy_error_handler, |
Dan Williams | e4a9c37 | 2012-06-21 23:25:27 -0700 | [diff] [blame] | 6423 | .sched_eh = ata_std_sched_eh, |
| 6424 | .end_eh = ata_std_end_eh, |
Tejun Heo | dd5b06c | 2006-08-10 16:59:12 +0900 | [diff] [blame] | 6425 | }; |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 6426 | EXPORT_SYMBOL_GPL(ata_dummy_port_ops); |
Tejun Heo | dd5b06c | 2006-08-10 16:59:12 +0900 | [diff] [blame] | 6427 | |
Tejun Heo | 21b0ad4 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 6428 | const struct ata_port_info ata_dummy_port_info = { |
| 6429 | .port_ops = &ata_dummy_port_ops, |
| 6430 | }; |
Bartlomiej Zolnierkiewicz | a52fbcf | 2020-03-26 16:58:04 +0100 | [diff] [blame] | 6431 | EXPORT_SYMBOL_GPL(ata_dummy_port_info); |
Tejun Heo | 21b0ad4 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 6432 | |
Tejun Heo | dd5b06c | 2006-08-10 16:59:12 +0900 | [diff] [blame] | 6433 | /* |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 6434 | * Utility print functions |
| 6435 | */ |
Joe Perches | d7bead1 | 2014-09-22 09:52:18 -0700 | [diff] [blame] | 6436 | void ata_port_printk(const struct ata_port *ap, const char *level, |
| 6437 | const char *fmt, ...) |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 6438 | { |
| 6439 | struct va_format vaf; |
| 6440 | va_list args; |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 6441 | |
| 6442 | va_start(args, fmt); |
| 6443 | |
| 6444 | vaf.fmt = fmt; |
| 6445 | vaf.va = &args; |
| 6446 | |
Joe Perches | d7bead1 | 2014-09-22 09:52:18 -0700 | [diff] [blame] | 6447 | printk("%sata%u: %pV", level, ap->print_id, &vaf); |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 6448 | |
| 6449 | va_end(args); |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 6450 | } |
| 6451 | EXPORT_SYMBOL(ata_port_printk); |
| 6452 | |
Joe Perches | d7bead1 | 2014-09-22 09:52:18 -0700 | [diff] [blame] | 6453 | void ata_link_printk(const struct ata_link *link, const char *level, |
| 6454 | const char *fmt, ...) |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 6455 | { |
| 6456 | struct va_format vaf; |
| 6457 | va_list args; |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 6458 | |
| 6459 | va_start(args, fmt); |
| 6460 | |
| 6461 | vaf.fmt = fmt; |
| 6462 | vaf.va = &args; |
| 6463 | |
| 6464 | if (sata_pmp_attached(link->ap) || link->ap->slave_link) |
Joe Perches | d7bead1 | 2014-09-22 09:52:18 -0700 | [diff] [blame] | 6465 | printk("%sata%u.%02u: %pV", |
| 6466 | level, link->ap->print_id, link->pmp, &vaf); |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 6467 | else |
Joe Perches | d7bead1 | 2014-09-22 09:52:18 -0700 | [diff] [blame] | 6468 | printk("%sata%u: %pV", |
| 6469 | level, link->ap->print_id, &vaf); |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 6470 | |
| 6471 | va_end(args); |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 6472 | } |
| 6473 | EXPORT_SYMBOL(ata_link_printk); |
| 6474 | |
Joe Perches | d7bead1 | 2014-09-22 09:52:18 -0700 | [diff] [blame] | 6475 | void ata_dev_printk(const struct ata_device *dev, const char *level, |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 6476 | const char *fmt, ...) |
| 6477 | { |
| 6478 | struct va_format vaf; |
| 6479 | va_list args; |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 6480 | |
| 6481 | va_start(args, fmt); |
| 6482 | |
| 6483 | vaf.fmt = fmt; |
| 6484 | vaf.va = &args; |
| 6485 | |
Joe Perches | d7bead1 | 2014-09-22 09:52:18 -0700 | [diff] [blame] | 6486 | printk("%sata%u.%02u: %pV", |
| 6487 | level, dev->link->ap->print_id, dev->link->pmp + dev->devno, |
| 6488 | &vaf); |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 6489 | |
| 6490 | va_end(args); |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 6491 | } |
| 6492 | EXPORT_SYMBOL(ata_dev_printk); |
| 6493 | |
Joe Perches | 06296a1 | 2011-04-15 15:52:00 -0700 | [diff] [blame] | 6494 | void ata_print_version(const struct device *dev, const char *version) |
| 6495 | { |
| 6496 | dev_printk(KERN_DEBUG, dev, "version %s\n", version); |
| 6497 | } |
| 6498 | EXPORT_SYMBOL(ata_print_version); |