blob: 5befe6ce6039d6f4ae86593a0fd141b76455db5d [file] [log] [blame]
Thomas Gleixnerc82ee6d2019-05-19 15:51:48 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Jeff Garzikaf36d7f2005-08-28 20:18:39 -04003 * libata-core.c - helper library for ATA
4 *
Jeff Garzikaf36d7f2005-08-28 20:18:39 -04005 * Copyright 2003-2004 Red Hat, Inc. All rights reserved.
6 * Copyright 2003-2004 Jeff Garzik
7 *
Jeff Garzikaf36d7f2005-08-28 20:18:39 -04008 * libata documentation is available via 'make {ps|pdf}docs',
Mauro Carvalho Chehab19285f32017-05-14 11:52:56 -03009 * as Documentation/driver-api/libata.rst
Jeff Garzikaf36d7f2005-08-28 20:18:39 -040010 *
11 * Hardware documentation available from http://www.t13.org/ and
12 * http://www.sata-io.org/
13 *
Alan Cox92c52c52007-11-19 14:30:16 +000014 * 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 Zolnierkiewicza52fbcf2020-03-26 16:58:04 +010021 *
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 Torvalds1da177e2005-04-16 15:20:36 -070026 */
27
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#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 Torvalds1da177e2005-04-16 15:20:36 -070034#include <linux/spinlock.h>
35#include <linux/blkdev.h>
36#include <linux/delay.h>
37#include <linux/timer.h>
Anil Veliyankara Madam848c3922016-01-07 21:18:52 -080038#include <linux/time.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/interrupt.h>
40#include <linux/completion.h>
41#include <linux/suspend.h>
42#include <linux/workqueue.h>
David Hardeman378f0582005-09-17 17:55:31 +100043#include <linux/scatterlist.h>
Jeff Garzik2dcb4072007-10-19 06:42:56 -040044#include <linux/io.h>
Mark Lorde18086d2009-03-19 13:32:21 -040045#include <linux/log2.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090046#include <linux/slab.h>
George Spelvin428ac5f2014-08-06 16:09:27 -070047#include <linux/glob.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <scsi/scsi.h>
Jeff Garzik193515d2005-11-07 00:59:37 -050049#include <scsi/scsi_cmnd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#include <scsi/scsi_host.h>
51#include <linux/libata.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <asm/byteorder.h>
Hannes Reineckefe5af0c2016-04-25 12:45:48 +020053#include <asm/unaligned.h>
Tejun Heo140b5e592007-12-12 12:21:52 +090054#include <linux/cdrom.h>
Akinobu Mita9990b6f2010-04-12 21:11:41 +090055#include <linux/ratelimit.h>
Stephan Linzeb25cb92016-06-10 07:59:56 +020056#include <linux/leds.h>
Lin Ming9ee4f392011-12-05 09:20:28 +080057#include <linux/pm_runtime.h>
Brian Norrisb7db04d2012-11-02 12:29:32 -070058#include <linux/platform_device.h>
Bartlomiej Zolnierkiewiczbbf5a092020-03-26 16:58:01 +010059#include <asm/setup.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
Hannes Reinecke255c03d2015-03-27 16:46:38 +010061#define CREATE_TRACE_POINTS
62#include <trace/events/libata.h>
63
Linus Torvalds1da177e2005-04-16 15:20:36 -070064#include "libata.h"
Gwendal Grignoud9027472010-05-25 12:31:38 -070065#include "libata-transport.h"
Jeff Garzikfda0efc2007-01-31 07:43:15 -050066
Tejun Heo029cfd62008-03-25 12:22:49 +090067const struct ata_port_operations ata_base_port_ops = {
Tejun Heo0aa11132008-04-07 22:47:18 +090068 .prereset = ata_std_prereset,
Tejun Heo203c75b2008-04-07 22:47:18 +090069 .postreset = ata_std_postreset,
Tejun Heoa1efdab2008-03-25 12:22:50 +090070 .error_handler = ata_std_error_handler,
Dan Williamse4a9c372012-06-21 23:25:27 -070071 .sched_eh = ata_std_sched_eh,
72 .end_eh = ata_std_end_eh,
Tejun Heo029cfd62008-03-25 12:22:49 +090073};
74
75const struct ata_port_operations sata_port_ops = {
76 .inherits = &ata_base_port_ops,
77
78 .qc_defer = ata_std_qc_defer,
Tejun Heo57c9efd2008-04-07 22:47:19 +090079 .hardreset = sata_std_hardreset,
Tejun Heo029cfd62008-03-25 12:22:49 +090080};
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +010081EXPORT_SYMBOL_GPL(sata_port_ops);
Tejun Heo029cfd62008-03-25 12:22:49 +090082
Tejun Heo3373efd2006-05-15 20:57:53 +090083static unsigned int ata_dev_init_params(struct ata_device *dev,
84 u16 heads, u16 sectors);
85static unsigned int ata_dev_set_xfermode(struct ata_device *dev);
86static void ata_dev_xfermask(struct ata_device *dev);
Tejun Heo75683fe2007-07-05 13:31:27 +090087static unsigned long ata_dev_blacklisted(const struct ata_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
Tero Roponena78f57a2012-04-22 11:38:00 +030089atomic_t ata_print_id = ATOMIC_INIT(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
Bartlomiej Zolnierkiewiczbf89b0b2020-03-26 16:58:22 +010091#ifdef CONFIG_ATA_FORCE
Tejun Heo33267322008-02-13 09:15:09 +090092struct ata_force_param {
93 const char *name;
Bartlomiej Zolnierkiewicz8ba5a452020-03-26 16:58:02 +010094 u8 cbl;
95 u8 spd_limit;
Tejun Heo33267322008-02-13 09:15:09 +090096 unsigned long xfer_mask;
97 unsigned int horkage_on;
98 unsigned int horkage_off;
Bartlomiej Zolnierkiewicz8ba5a452020-03-26 16:58:02 +010099 u16 lflags;
Tejun Heo33267322008-02-13 09:15:09 +0900100};
101
102struct ata_force_ent {
103 int port;
104 int device;
105 struct ata_force_param param;
106};
107
108static struct ata_force_ent *ata_force_tbl;
109static int ata_force_tbl_size;
110
Bartlomiej Zolnierkiewiczbbf5a092020-03-26 16:58:01 +0100111static char ata_force_param_buf[COMMAND_LINE_SIZE] __initdata;
Tejun Heo7afb4222008-03-09 20:21:53 +0900112/* param_buf is thrown away after initialization, disallow read */
113module_param_string(force, ata_force_param_buf, sizeof(ata_force_param_buf), 0);
Mauro Carvalho Chehab8c27ceff32016-10-18 10:12:27 -0200114MODULE_PARM_DESC(force, "Force ATA configurations including cable type, link speed and transfer mode (see Documentation/admin-guide/kernel-parameters.rst for details)");
Bartlomiej Zolnierkiewiczbf89b0b2020-03-26 16:58:22 +0100115#endif
Tejun Heo33267322008-02-13 09:15:09 +0900116
Tejun Heo2486fa52008-07-31 07:52:40 +0900117static int atapi_enabled = 1;
Jeff Garzik1623c812005-08-30 03:37:42 -0400118module_param(atapi_enabled, int, 0444);
Evgeni Golovad5d8ea2009-06-22 09:46:20 +0200119MODULE_PARM_DESC(atapi_enabled, "Enable discovery of ATAPI devices (0=off, 1=on [default])");
Jeff Garzik1623c812005-08-30 03:37:42 -0400120
Adrian Bunkc5c61bd2008-02-25 02:07:25 +0200121static int atapi_dmadir = 0;
Albert Lee95de7192006-04-04 10:57:18 +0800122module_param(atapi_dmadir, int, 0444);
Evgeni Golovad5d8ea2009-06-22 09:46:20 +0200123MODULE_PARM_DESC(atapi_dmadir, "Enable ATAPI DMADIR bridge support (0=off [default], 1=on)");
Albert Lee95de7192006-04-04 10:57:18 +0800124
Mark Lordbaf4fdf2007-08-08 01:08:45 +0900125int atapi_passthru16 = 1;
126module_param(atapi_passthru16, int, 0444);
Evgeni Golovad5d8ea2009-06-22 09:46:20 +0200127MODULE_PARM_DESC(atapi_passthru16, "Enable ATA_16 passthru for ATAPI devices (0=off, 1=on [default])");
Mark Lordbaf4fdf2007-08-08 01:08:45 +0900128
Jeff Garzikc3c013a2006-02-27 22:31:19 -0500129int libata_fua = 0;
130module_param_named(fua, libata_fua, int, 0444);
Evgeni Golovad5d8ea2009-06-22 09:46:20 +0200131MODULE_PARM_DESC(fua, "FUA support (0=off [default], 1=on)");
Jeff Garzikc3c013a2006-02-27 22:31:19 -0500132
Jeff Garzik2dcb4072007-10-19 06:42:56 -0400133static int ata_ignore_hpa;
Alan Cox1e999732007-04-11 00:23:13 +0100134module_param_named(ignore_hpa, ata_ignore_hpa, int, 0644);
135MODULE_PARM_DESC(ignore_hpa, "Ignore HPA limit (0=keep BIOS limits, 1=ignore limits, using full disk)");
136
Alan Coxb3a70602007-10-02 12:38:26 -0400137static int libata_dma_mask = ATA_DMA_MASK_ATA|ATA_DMA_MASK_ATAPI|ATA_DMA_MASK_CFA;
138module_param_named(dma, libata_dma_mask, int, 0444);
139MODULE_PARM_DESC(dma, "DMA enable/disable (0x1==ATA, 0x2==ATAPI, 0x4==CF)");
140
Tejun Heo87fbc5a2008-05-20 02:17:54 +0900141static int ata_probe_timeout;
Andrew Mortona8601e52006-06-25 01:36:52 -0700142module_param(ata_probe_timeout, int, 0444);
143MODULE_PARM_DESC(ata_probe_timeout, "Set ATA probing timeout (seconds)");
144
Jeff Garzik6ebe9d82007-10-05 16:28:36 -0400145int libata_noacpi = 0;
Jeff Garzikd7d0dad2007-03-28 01:57:37 -0400146module_param_named(noacpi, libata_noacpi, int, 0444);
Evgeni Golovad5d8ea2009-06-22 09:46:20 +0200147MODULE_PARM_DESC(noacpi, "Disable the use of ACPI in probe/suspend/resume (0=off [default], 1=on)");
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700148
Alan Coxae8d4ee2007-11-04 22:05:49 -0500149int libata_allow_tpm = 0;
150module_param_named(allow_tpm, libata_allow_tpm, int, 0444);
Evgeni Golovad5d8ea2009-06-22 09:46:20 +0200151MODULE_PARM_DESC(allow_tpm, "Permit the use of TPM commands (0=off [default], 1=on)");
Alan Coxae8d4ee2007-11-04 22:05:49 -0500152
Tejun Heoe7ecd432010-05-19 15:38:58 +0200153static int atapi_an;
154module_param(atapi_an, int, 0444);
155MODULE_PARM_DESC(atapi_an, "Enable ATAPI AN media presence notification (0=0ff [default], 1=on)");
156
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157MODULE_AUTHOR("Jeff Garzik");
158MODULE_DESCRIPTION("Library module for ATA devices");
159MODULE_LICENSE("GPL");
160MODULE_VERSION(DRV_VERSION);
161
Damien Le Moal891fd7c2021-08-16 10:44:50 +0900162static inline bool ata_dev_print_info(struct ata_device *dev)
163{
164 struct ata_eh_context *ehc = &dev->link->eh_context;
165
166 return ehc->i.flags & ATA_EHI_PRINTINFO;
167}
Edward Falk0baab862005-06-02 18:17:13 -0400168
Tejun Heo9913ff8a2009-01-29 20:31:31 +0900169static bool ata_sstatus_online(u32 sstatus)
170{
171 return (sstatus & 0xf) == 0x3;
172}
173
Tejun Heo1eca4362008-11-03 20:03:17 +0900174/**
175 * ata_link_next - link iteration helper
176 * @link: the previous link, NULL to start
177 * @ap: ATA port containing links to iterate
178 * @mode: iteration mode, one of ATA_LITER_*
Tejun Heoaadffb62008-07-31 17:02:41 +0900179 *
Tejun Heo1eca4362008-11-03 20:03:17 +0900180 * LOCKING:
181 * Host lock or EH context.
182 *
183 * RETURNS:
184 * Pointer to the next link.
Tejun Heoaadffb62008-07-31 17:02:41 +0900185 */
Tejun Heo1eca4362008-11-03 20:03:17 +0900186struct ata_link *ata_link_next(struct ata_link *link, struct ata_port *ap,
187 enum ata_link_iter_mode mode)
Tejun Heoaadffb62008-07-31 17:02:41 +0900188{
Tejun Heo1eca4362008-11-03 20:03:17 +0900189 BUG_ON(mode != ATA_LITER_EDGE &&
190 mode != ATA_LITER_PMP_FIRST && mode != ATA_LITER_HOST_FIRST);
Tejun Heoaadffb62008-07-31 17:02:41 +0900191
Tejun Heo1eca4362008-11-03 20:03:17 +0900192 /* NULL link indicates start of iteration */
193 if (!link)
194 switch (mode) {
195 case ATA_LITER_EDGE:
196 case ATA_LITER_PMP_FIRST:
197 if (sata_pmp_attached(ap))
198 return ap->pmp_link;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500199 fallthrough;
Tejun Heo1eca4362008-11-03 20:03:17 +0900200 case ATA_LITER_HOST_FIRST:
201 return &ap->link;
202 }
203
204 /* we just iterated over the host link, what's next? */
205 if (link == &ap->link)
206 switch (mode) {
207 case ATA_LITER_HOST_FIRST:
208 if (sata_pmp_attached(ap))
209 return ap->pmp_link;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500210 fallthrough;
Tejun Heo1eca4362008-11-03 20:03:17 +0900211 case ATA_LITER_PMP_FIRST:
212 if (unlikely(ap->slave_link))
Tejun Heob1c72912008-07-31 17:02:43 +0900213 return ap->slave_link;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500214 fallthrough;
Tejun Heo1eca4362008-11-03 20:03:17 +0900215 case ATA_LITER_EDGE:
Tejun Heoaadffb62008-07-31 17:02:41 +0900216 return NULL;
Tejun Heob1c72912008-07-31 17:02:43 +0900217 }
Tejun Heoaadffb62008-07-31 17:02:41 +0900218
Tejun Heob1c72912008-07-31 17:02:43 +0900219 /* slave_link excludes PMP */
220 if (unlikely(link == ap->slave_link))
221 return NULL;
222
Tejun Heo1eca4362008-11-03 20:03:17 +0900223 /* we were over a PMP link */
Tejun Heoaadffb62008-07-31 17:02:41 +0900224 if (++link < ap->pmp_link + ap->nr_pmp_links)
225 return link;
Tejun Heo1eca4362008-11-03 20:03:17 +0900226
227 if (mode == ATA_LITER_PMP_FIRST)
228 return &ap->link;
229
Tejun Heoaadffb62008-07-31 17:02:41 +0900230 return NULL;
231}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +0100232EXPORT_SYMBOL_GPL(ata_link_next);
Tejun Heoaadffb62008-07-31 17:02:41 +0900233
Edward Falk0baab862005-06-02 18:17:13 -0400234/**
Tejun Heo1eca4362008-11-03 20:03:17 +0900235 * ata_dev_next - device iteration helper
236 * @dev: the previous device, NULL to start
237 * @link: ATA link containing devices to iterate
238 * @mode: iteration mode, one of ATA_DITER_*
239 *
240 * LOCKING:
241 * Host lock or EH context.
242 *
243 * RETURNS:
244 * Pointer to the next device.
245 */
246struct ata_device *ata_dev_next(struct ata_device *dev, struct ata_link *link,
247 enum ata_dev_iter_mode mode)
248{
249 BUG_ON(mode != ATA_DITER_ENABLED && mode != ATA_DITER_ENABLED_REVERSE &&
250 mode != ATA_DITER_ALL && mode != ATA_DITER_ALL_REVERSE);
251
252 /* NULL dev indicates start of iteration */
253 if (!dev)
254 switch (mode) {
255 case ATA_DITER_ENABLED:
256 case ATA_DITER_ALL:
257 dev = link->device;
258 goto check;
259 case ATA_DITER_ENABLED_REVERSE:
260 case ATA_DITER_ALL_REVERSE:
261 dev = link->device + ata_link_max_devices(link) - 1;
262 goto check;
263 }
264
265 next:
266 /* move to the next one */
267 switch (mode) {
268 case ATA_DITER_ENABLED:
269 case ATA_DITER_ALL:
270 if (++dev < link->device + ata_link_max_devices(link))
271 goto check;
272 return NULL;
273 case ATA_DITER_ENABLED_REVERSE:
274 case ATA_DITER_ALL_REVERSE:
275 if (--dev >= link->device)
276 goto check;
277 return NULL;
278 }
279
280 check:
281 if ((mode == ATA_DITER_ENABLED || mode == ATA_DITER_ENABLED_REVERSE) &&
282 !ata_dev_enabled(dev))
283 goto next;
284 return dev;
285}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +0100286EXPORT_SYMBOL_GPL(ata_dev_next);
Tejun Heo1eca4362008-11-03 20:03:17 +0900287
288/**
Tejun Heob1c72912008-07-31 17:02:43 +0900289 * ata_dev_phys_link - find physical link for a device
290 * @dev: ATA device to look up physical link for
291 *
292 * Look up physical link which @dev is attached to. Note that
293 * this is different from @dev->link only when @dev is on slave
294 * link. For all other cases, it's the same as @dev->link.
295 *
296 * LOCKING:
297 * Don't care.
298 *
299 * RETURNS:
300 * Pointer to the found physical link.
301 */
302struct ata_link *ata_dev_phys_link(struct ata_device *dev)
303{
304 struct ata_port *ap = dev->link->ap;
305
306 if (!ap->slave_link)
307 return dev->link;
308 if (!dev->devno)
309 return &ap->link;
310 return ap->slave_link;
311}
312
Bartlomiej Zolnierkiewiczbf89b0b2020-03-26 16:58:22 +0100313#ifdef CONFIG_ATA_FORCE
Tejun Heob1c72912008-07-31 17:02:43 +0900314/**
Tejun Heo33267322008-02-13 09:15:09 +0900315 * ata_force_cbl - force cable type according to libata.force
Randy Dunlap4cdfa1b2008-02-22 12:21:37 -0800316 * @ap: ATA port of interest
Tejun Heo33267322008-02-13 09:15:09 +0900317 *
318 * Force cable type according to libata.force and whine about it.
319 * The last entry which has matching port number is used, so it
320 * can be specified as part of device force parameters. For
321 * example, both "a:40c,1.00:udma4" and "1.00:40c,udma4" have the
322 * same effect.
323 *
324 * LOCKING:
325 * EH context.
326 */
327void ata_force_cbl(struct ata_port *ap)
328{
329 int i;
330
331 for (i = ata_force_tbl_size - 1; i >= 0; i--) {
332 const struct ata_force_ent *fe = &ata_force_tbl[i];
333
334 if (fe->port != -1 && fe->port != ap->print_id)
335 continue;
336
337 if (fe->param.cbl == ATA_CBL_NONE)
338 continue;
339
340 ap->cbl = fe->param.cbl;
Joe Perchesa9a79df2011-04-15 15:51:59 -0700341 ata_port_notice(ap, "FORCE: cable set to %s\n", fe->param.name);
Tejun Heo33267322008-02-13 09:15:09 +0900342 return;
343 }
344}
345
346/**
Tejun Heo05944bd2008-08-13 20:19:09 +0900347 * ata_force_link_limits - force link limits according to libata.force
Tejun Heo33267322008-02-13 09:15:09 +0900348 * @link: ATA link of interest
349 *
Tejun Heo05944bd2008-08-13 20:19:09 +0900350 * Force link flags and SATA spd limit according to libata.force
351 * and whine about it. When only the port part is specified
352 * (e.g. 1:), the limit applies to all links connected to both
353 * the host link and all fan-out ports connected via PMP. If the
354 * device part is specified as 0 (e.g. 1.00:), it specifies the
355 * first fan-out link not the host link. Device number 15 always
Tejun Heob1c72912008-07-31 17:02:43 +0900356 * points to the host link whether PMP is attached or not. If the
357 * controller has slave link, device number 16 points to it.
Tejun Heo33267322008-02-13 09:15:09 +0900358 *
359 * LOCKING:
360 * EH context.
361 */
Tejun Heo05944bd2008-08-13 20:19:09 +0900362static void ata_force_link_limits(struct ata_link *link)
Tejun Heo33267322008-02-13 09:15:09 +0900363{
Tejun Heo05944bd2008-08-13 20:19:09 +0900364 bool did_spd = false;
Tejun Heob1c72912008-07-31 17:02:43 +0900365 int linkno = link->pmp;
366 int i;
Tejun Heo33267322008-02-13 09:15:09 +0900367
368 if (ata_is_host_link(link))
Tejun Heob1c72912008-07-31 17:02:43 +0900369 linkno += 15;
Tejun Heo33267322008-02-13 09:15:09 +0900370
371 for (i = ata_force_tbl_size - 1; i >= 0; i--) {
372 const struct ata_force_ent *fe = &ata_force_tbl[i];
373
374 if (fe->port != -1 && fe->port != link->ap->print_id)
375 continue;
376
377 if (fe->device != -1 && fe->device != linkno)
378 continue;
379
Tejun Heo05944bd2008-08-13 20:19:09 +0900380 /* only honor the first spd limit */
381 if (!did_spd && fe->param.spd_limit) {
382 link->hw_sata_spd_limit = (1 << fe->param.spd_limit) - 1;
Joe Perchesa9a79df2011-04-15 15:51:59 -0700383 ata_link_notice(link, "FORCE: PHY spd limit set to %s\n",
Tejun Heo05944bd2008-08-13 20:19:09 +0900384 fe->param.name);
385 did_spd = true;
386 }
Tejun Heo33267322008-02-13 09:15:09 +0900387
Tejun Heo05944bd2008-08-13 20:19:09 +0900388 /* let lflags stack */
389 if (fe->param.lflags) {
390 link->flags |= fe->param.lflags;
Joe Perchesa9a79df2011-04-15 15:51:59 -0700391 ata_link_notice(link,
Tejun Heo05944bd2008-08-13 20:19:09 +0900392 "FORCE: link flag 0x%x forced -> 0x%x\n",
393 fe->param.lflags, link->flags);
394 }
Tejun Heo33267322008-02-13 09:15:09 +0900395 }
396}
397
398/**
399 * ata_force_xfermask - force xfermask according to libata.force
400 * @dev: ATA device of interest
401 *
402 * Force xfer_mask according to libata.force and whine about it.
403 * For consistency with link selection, device number 15 selects
404 * the first device connected to the host link.
405 *
406 * LOCKING:
407 * EH context.
408 */
409static void ata_force_xfermask(struct ata_device *dev)
410{
411 int devno = dev->link->pmp + dev->devno;
412 int alt_devno = devno;
413 int i;
414
Tejun Heob1c72912008-07-31 17:02:43 +0900415 /* allow n.15/16 for devices attached to host port */
416 if (ata_is_host_link(dev->link))
417 alt_devno += 15;
Tejun Heo33267322008-02-13 09:15:09 +0900418
419 for (i = ata_force_tbl_size - 1; i >= 0; i--) {
420 const struct ata_force_ent *fe = &ata_force_tbl[i];
421 unsigned long pio_mask, mwdma_mask, udma_mask;
422
423 if (fe->port != -1 && fe->port != dev->link->ap->print_id)
424 continue;
425
426 if (fe->device != -1 && fe->device != devno &&
427 fe->device != alt_devno)
428 continue;
429
430 if (!fe->param.xfer_mask)
431 continue;
432
433 ata_unpack_xfermask(fe->param.xfer_mask,
434 &pio_mask, &mwdma_mask, &udma_mask);
435 if (udma_mask)
436 dev->udma_mask = udma_mask;
437 else if (mwdma_mask) {
438 dev->udma_mask = 0;
439 dev->mwdma_mask = mwdma_mask;
440 } else {
441 dev->udma_mask = 0;
442 dev->mwdma_mask = 0;
443 dev->pio_mask = pio_mask;
444 }
445
Joe Perchesa9a79df2011-04-15 15:51:59 -0700446 ata_dev_notice(dev, "FORCE: xfer_mask set to %s\n",
447 fe->param.name);
Tejun Heo33267322008-02-13 09:15:09 +0900448 return;
449 }
450}
451
452/**
453 * ata_force_horkage - force horkage according to libata.force
454 * @dev: ATA device of interest
455 *
456 * Force horkage according to libata.force and whine about it.
457 * For consistency with link selection, device number 15 selects
458 * the first device connected to the host link.
459 *
460 * LOCKING:
461 * EH context.
462 */
463static void ata_force_horkage(struct ata_device *dev)
464{
465 int devno = dev->link->pmp + dev->devno;
466 int alt_devno = devno;
467 int i;
468
Tejun Heob1c72912008-07-31 17:02:43 +0900469 /* allow n.15/16 for devices attached to host port */
470 if (ata_is_host_link(dev->link))
471 alt_devno += 15;
Tejun Heo33267322008-02-13 09:15:09 +0900472
473 for (i = 0; i < ata_force_tbl_size; i++) {
474 const struct ata_force_ent *fe = &ata_force_tbl[i];
475
476 if (fe->port != -1 && fe->port != dev->link->ap->print_id)
477 continue;
478
479 if (fe->device != -1 && fe->device != devno &&
480 fe->device != alt_devno)
481 continue;
482
483 if (!(~dev->horkage & fe->param.horkage_on) &&
484 !(dev->horkage & fe->param.horkage_off))
485 continue;
486
487 dev->horkage |= fe->param.horkage_on;
488 dev->horkage &= ~fe->param.horkage_off;
489
Joe Perchesa9a79df2011-04-15 15:51:59 -0700490 ata_dev_notice(dev, "FORCE: horkage modified (%s)\n",
491 fe->param.name);
Tejun Heo33267322008-02-13 09:15:09 +0900492 }
493}
Bartlomiej Zolnierkiewiczbf89b0b2020-03-26 16:58:22 +0100494#else
495static inline void ata_force_link_limits(struct ata_link *link) { }
496static inline void ata_force_xfermask(struct ata_device *dev) { }
497static inline void ata_force_horkage(struct ata_device *dev) { }
498#endif
Tejun Heo33267322008-02-13 09:15:09 +0900499
500/**
Tejun Heo436d34b2008-04-02 17:28:46 +0900501 * atapi_cmd_type - Determine ATAPI command type from SCSI opcode
502 * @opcode: SCSI opcode
503 *
504 * Determine ATAPI command type from @opcode.
505 *
506 * LOCKING:
507 * None.
508 *
509 * RETURNS:
510 * ATAPI_{READ|WRITE|READ_CD|PASS_THRU|MISC}
511 */
512int atapi_cmd_type(u8 opcode)
513{
514 switch (opcode) {
515 case GPCMD_READ_10:
516 case GPCMD_READ_12:
517 return ATAPI_READ;
518
519 case GPCMD_WRITE_10:
520 case GPCMD_WRITE_12:
521 case GPCMD_WRITE_AND_VERIFY_10:
522 return ATAPI_WRITE;
523
524 case GPCMD_READ_CD:
525 case GPCMD_READ_CD_MSF:
526 return ATAPI_READ_CD;
527
Tejun Heoe52dcc42008-04-02 17:35:19 +0900528 case ATA_16:
529 case ATA_12:
530 if (atapi_passthru16)
531 return ATAPI_PASS_THRU;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500532 fallthrough;
Tejun Heo436d34b2008-04-02 17:28:46 +0900533 default:
534 return ATAPI_MISC;
535 }
536}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +0100537EXPORT_SYMBOL_GPL(atapi_cmd_type);
Tejun Heo436d34b2008-04-02 17:28:46 +0900538
Albert Lee8cbd6df2005-10-12 15:06:27 +0800539static const u8 ata_rw_cmds[] = {
540 /* pio multi */
541 ATA_CMD_READ_MULTI,
542 ATA_CMD_WRITE_MULTI,
543 ATA_CMD_READ_MULTI_EXT,
544 ATA_CMD_WRITE_MULTI_EXT,
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100545 0,
546 0,
547 0,
548 ATA_CMD_WRITE_MULTI_FUA_EXT,
Albert Lee8cbd6df2005-10-12 15:06:27 +0800549 /* pio */
550 ATA_CMD_PIO_READ,
551 ATA_CMD_PIO_WRITE,
552 ATA_CMD_PIO_READ_EXT,
553 ATA_CMD_PIO_WRITE_EXT,
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100554 0,
555 0,
556 0,
557 0,
Albert Lee8cbd6df2005-10-12 15:06:27 +0800558 /* dma */
559 ATA_CMD_READ,
560 ATA_CMD_WRITE,
561 ATA_CMD_READ_EXT,
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100562 ATA_CMD_WRITE_EXT,
563 0,
564 0,
565 0,
566 ATA_CMD_WRITE_FUA_EXT
Albert Lee8cbd6df2005-10-12 15:06:27 +0800567};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
569/**
Albert Lee8cbd6df2005-10-12 15:06:27 +0800570 * ata_rwcmd_protocol - set taskfile r/w commands and protocol
Tejun Heobd056d72006-11-14 22:47:10 +0900571 * @tf: command to examine and configure
572 * @dev: device tf belongs to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 *
Jeff Garzik2e9edbf2006-03-24 09:56:57 -0500574 * Examine the device configuration and tf->flags to calculate
Albert Lee8cbd6df2005-10-12 15:06:27 +0800575 * the proper read/write commands and protocol to use.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 *
577 * LOCKING:
578 * caller.
579 */
Tejun Heobd056d72006-11-14 22:47:10 +0900580static int ata_rwcmd_protocol(struct ata_taskfile *tf, struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581{
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100582 u8 cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100584 int index, fua, lba48, write;
Jeff Garzik2e9edbf2006-03-24 09:56:57 -0500585
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100586 fua = (tf->flags & ATA_TFLAG_FUA) ? 4 : 0;
Albert Lee8cbd6df2005-10-12 15:06:27 +0800587 lba48 = (tf->flags & ATA_TFLAG_LBA48) ? 2 : 0;
588 write = (tf->flags & ATA_TFLAG_WRITE) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589
Albert Lee8cbd6df2005-10-12 15:06:27 +0800590 if (dev->flags & ATA_DFLAG_PIO) {
591 tf->protocol = ATA_PROT_PIO;
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100592 index = dev->multi_count ? 0 : 8;
Tejun Heo9af5c9c2007-08-06 18:36:22 +0900593 } else if (lba48 && (dev->link->ap->flags & ATA_FLAG_PIO_LBA48)) {
Alan Cox8d238e02006-01-17 20:50:31 +0000594 /* Unable to use DMA due to host limitation */
595 tf->protocol = ATA_PROT_PIO;
Albert Lee0565c262006-02-13 18:55:25 +0800596 index = dev->multi_count ? 0 : 8;
Albert Lee8cbd6df2005-10-12 15:06:27 +0800597 } else {
598 tf->protocol = ATA_PROT_DMA;
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100599 index = 16;
Albert Lee8cbd6df2005-10-12 15:06:27 +0800600 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100602 cmd = ata_rw_cmds[index + fua + lba48 + write];
603 if (cmd) {
604 tf->command = cmd;
605 return 0;
606 }
607 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608}
609
Tejun Heocb95d562006-03-06 04:31:56 +0900610/**
Tejun Heo35b649f2006-11-14 22:37:35 +0900611 * ata_tf_read_block - Read block address from ATA taskfile
612 * @tf: ATA taskfile of interest
613 * @dev: ATA device @tf belongs to
614 *
615 * LOCKING:
616 * None.
617 *
618 * Read block address from @tf. This function can handle all
619 * three address formats - LBA, LBA48 and CHS. tf->protocol and
620 * flags select the address format to use.
621 *
622 * RETURNS:
623 * Block address read from @tf.
624 */
Hannes Reineckecffd1ee2016-04-04 11:43:57 +0200625u64 ata_tf_read_block(const struct ata_taskfile *tf, struct ata_device *dev)
Tejun Heo35b649f2006-11-14 22:37:35 +0900626{
627 u64 block = 0;
628
Tejun Heofe16d4f2015-08-03 11:41:33 -0400629 if (tf->flags & ATA_TFLAG_LBA) {
Tejun Heo35b649f2006-11-14 22:37:35 +0900630 if (tf->flags & ATA_TFLAG_LBA48) {
631 block |= (u64)tf->hob_lbah << 40;
632 block |= (u64)tf->hob_lbam << 32;
Roland Dreier44901a92008-11-04 10:34:48 -0800633 block |= (u64)tf->hob_lbal << 24;
Tejun Heo35b649f2006-11-14 22:37:35 +0900634 } else
635 block |= (tf->device & 0xf) << 24;
636
637 block |= tf->lbah << 16;
638 block |= tf->lbam << 8;
639 block |= tf->lbal;
640 } else {
641 u32 cyl, head, sect;
642
643 cyl = tf->lbam | (tf->lbah << 8);
644 head = tf->device & 0xf;
645 sect = tf->lbal;
646
Tejun Heoac8672e2009-08-16 21:21:21 +0900647 if (!sect) {
Joe Perchesa9a79df2011-04-15 15:51:59 -0700648 ata_dev_warn(dev,
649 "device reported invalid CHS sector 0\n");
Hannes Reineckecffd1ee2016-04-04 11:43:57 +0200650 return U64_MAX;
Tejun Heoac8672e2009-08-16 21:21:21 +0900651 }
652
653 block = (cyl * dev->heads + head) * dev->sectors + sect - 1;
Tejun Heo35b649f2006-11-14 22:37:35 +0900654 }
655
656 return block;
657}
658
659/**
Tejun Heobd056d72006-11-14 22:47:10 +0900660 * ata_build_rw_tf - Build ATA taskfile for given read/write request
661 * @tf: Target ATA taskfile
662 * @dev: ATA device @tf belongs to
663 * @block: Block address
664 * @n_block: Number of blocks
665 * @tf_flags: RW/FUA etc...
666 * @tag: tag
Adam Manzanares8e061782016-10-17 11:27:29 -0700667 * @class: IO priority class
Tejun Heobd056d72006-11-14 22:47:10 +0900668 *
669 * LOCKING:
670 * None.
671 *
672 * Build ATA taskfile @tf for read/write request described by
673 * @block, @n_block, @tf_flags and @tag on @dev.
674 *
675 * RETURNS:
676 *
677 * 0 on success, -ERANGE if the request is too large for @dev,
678 * -EINVAL if the request is invalid.
679 */
680int ata_build_rw_tf(struct ata_taskfile *tf, struct ata_device *dev,
681 u64 block, u32 n_block, unsigned int tf_flags,
Adam Manzanares8e061782016-10-17 11:27:29 -0700682 unsigned int tag, int class)
Tejun Heobd056d72006-11-14 22:47:10 +0900683{
684 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
685 tf->flags |= tf_flags;
686
Jens Axboe2e2cc672018-05-11 12:51:06 -0600687 if (ata_ncq_enabled(dev) && !ata_tag_internal(tag)) {
Tejun Heobd056d72006-11-14 22:47:10 +0900688 /* yay, NCQ */
689 if (!lba_48_ok(block, n_block))
690 return -ERANGE;
691
692 tf->protocol = ATA_PROT_NCQ;
693 tf->flags |= ATA_TFLAG_LBA | ATA_TFLAG_LBA48;
694
695 if (tf->flags & ATA_TFLAG_WRITE)
696 tf->command = ATA_CMD_FPDMA_WRITE;
697 else
698 tf->command = ATA_CMD_FPDMA_READ;
699
700 tf->nsect = tag << 3;
701 tf->hob_feature = (n_block >> 8) & 0xff;
702 tf->feature = n_block & 0xff;
703
704 tf->hob_lbah = (block >> 40) & 0xff;
705 tf->hob_lbam = (block >> 32) & 0xff;
706 tf->hob_lbal = (block >> 24) & 0xff;
707 tf->lbah = (block >> 16) & 0xff;
708 tf->lbam = (block >> 8) & 0xff;
709 tf->lbal = block & 0xff;
710
Sergei Shtylyov9ca7cfa2012-08-21 22:18:50 +0400711 tf->device = ATA_LBA;
Tejun Heobd056d72006-11-14 22:47:10 +0900712 if (tf->flags & ATA_TFLAG_FUA)
713 tf->device |= 1 << 7;
Adam Manzanares8e061782016-10-17 11:27:29 -0700714
Damien Le Moal2360fa12021-08-16 10:44:51 +0900715 if (dev->flags & ATA_DFLAG_NCQ_PRIO_ENABLE &&
716 class == IOPRIO_CLASS_RT)
717 tf->hob_nsect |= ATA_PRIO_HIGH << ATA_SHIFT_PRIO;
Tejun Heobd056d72006-11-14 22:47:10 +0900718 } else if (dev->flags & ATA_DFLAG_LBA) {
719 tf->flags |= ATA_TFLAG_LBA;
720
721 if (lba_28_ok(block, n_block)) {
722 /* use LBA28 */
723 tf->device |= (block >> 24) & 0xf;
724 } else if (lba_48_ok(block, n_block)) {
725 if (!(dev->flags & ATA_DFLAG_LBA48))
726 return -ERANGE;
727
728 /* use LBA48 */
729 tf->flags |= ATA_TFLAG_LBA48;
730
731 tf->hob_nsect = (n_block >> 8) & 0xff;
732
733 tf->hob_lbah = (block >> 40) & 0xff;
734 tf->hob_lbam = (block >> 32) & 0xff;
735 tf->hob_lbal = (block >> 24) & 0xff;
736 } else
737 /* request too large even for LBA48 */
738 return -ERANGE;
739
740 if (unlikely(ata_rwcmd_protocol(tf, dev) < 0))
741 return -EINVAL;
742
743 tf->nsect = n_block & 0xff;
744
745 tf->lbah = (block >> 16) & 0xff;
746 tf->lbam = (block >> 8) & 0xff;
747 tf->lbal = block & 0xff;
748
749 tf->device |= ATA_LBA;
750 } else {
751 /* CHS */
752 u32 sect, head, cyl, track;
753
754 /* The request -may- be too large for CHS addressing. */
755 if (!lba_28_ok(block, n_block))
756 return -ERANGE;
757
758 if (unlikely(ata_rwcmd_protocol(tf, dev) < 0))
759 return -EINVAL;
760
761 /* Convert LBA to CHS */
762 track = (u32)block / dev->sectors;
763 cyl = track / dev->heads;
764 head = track % dev->heads;
765 sect = (u32)block % dev->sectors + 1;
766
767 DPRINTK("block %u track %u cyl %u head %u sect %u\n",
768 (u32)block, track, cyl, head, sect);
769
770 /* Check whether the converted CHS can fit.
771 Cylinder: 0-65535
772 Head: 0-15
773 Sector: 1-255*/
774 if ((cyl >> 16) || (head >> 4) || (sect >> 8) || (!sect))
775 return -ERANGE;
776
777 tf->nsect = n_block & 0xff; /* Sector count 0 means 256 sectors */
778 tf->lbal = sect;
779 tf->lbam = cyl;
780 tf->lbah = cyl >> 8;
781 tf->device |= head;
782 }
783
784 return 0;
785}
786
787/**
Tejun Heocb95d562006-03-06 04:31:56 +0900788 * ata_pack_xfermask - Pack pio, mwdma and udma masks into xfer_mask
789 * @pio_mask: pio_mask
790 * @mwdma_mask: mwdma_mask
791 * @udma_mask: udma_mask
792 *
793 * Pack @pio_mask, @mwdma_mask and @udma_mask into a single
794 * unsigned int xfer_mask.
795 *
796 * LOCKING:
797 * None.
798 *
799 * RETURNS:
800 * Packed xfer_mask.
801 */
Tejun Heo7dc951a2007-11-27 19:43:42 +0900802unsigned long ata_pack_xfermask(unsigned long pio_mask,
803 unsigned long mwdma_mask,
804 unsigned long udma_mask)
Tejun Heocb95d562006-03-06 04:31:56 +0900805{
806 return ((pio_mask << ATA_SHIFT_PIO) & ATA_MASK_PIO) |
807 ((mwdma_mask << ATA_SHIFT_MWDMA) & ATA_MASK_MWDMA) |
808 ((udma_mask << ATA_SHIFT_UDMA) & ATA_MASK_UDMA);
809}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +0100810EXPORT_SYMBOL_GPL(ata_pack_xfermask);
Tejun Heocb95d562006-03-06 04:31:56 +0900811
Tejun Heoc0489e42006-03-24 14:07:49 +0900812/**
813 * ata_unpack_xfermask - Unpack xfer_mask into pio, mwdma and udma masks
814 * @xfer_mask: xfer_mask to unpack
815 * @pio_mask: resulting pio_mask
816 * @mwdma_mask: resulting mwdma_mask
817 * @udma_mask: resulting udma_mask
818 *
819 * Unpack @xfer_mask into @pio_mask, @mwdma_mask and @udma_mask.
Masanari Iidac9b55602016-04-13 23:36:27 +0900820 * Any NULL destination masks will be ignored.
Tejun Heoc0489e42006-03-24 14:07:49 +0900821 */
Tejun Heo7dc951a2007-11-27 19:43:42 +0900822void ata_unpack_xfermask(unsigned long xfer_mask, unsigned long *pio_mask,
823 unsigned long *mwdma_mask, unsigned long *udma_mask)
Tejun Heoc0489e42006-03-24 14:07:49 +0900824{
825 if (pio_mask)
826 *pio_mask = (xfer_mask & ATA_MASK_PIO) >> ATA_SHIFT_PIO;
827 if (mwdma_mask)
828 *mwdma_mask = (xfer_mask & ATA_MASK_MWDMA) >> ATA_SHIFT_MWDMA;
829 if (udma_mask)
830 *udma_mask = (xfer_mask & ATA_MASK_UDMA) >> ATA_SHIFT_UDMA;
831}
832
Tejun Heocb95d562006-03-06 04:31:56 +0900833static const struct ata_xfer_ent {
Tejun Heobe9a50c2006-03-31 22:48:52 +0900834 int shift, bits;
Tejun Heocb95d562006-03-06 04:31:56 +0900835 u8 base;
836} ata_xfer_tbl[] = {
Tejun Heo70cd0712007-11-27 19:43:40 +0900837 { ATA_SHIFT_PIO, ATA_NR_PIO_MODES, XFER_PIO_0 },
838 { ATA_SHIFT_MWDMA, ATA_NR_MWDMA_MODES, XFER_MW_DMA_0 },
839 { ATA_SHIFT_UDMA, ATA_NR_UDMA_MODES, XFER_UDMA_0 },
Tejun Heocb95d562006-03-06 04:31:56 +0900840 { -1, },
841};
842
843/**
844 * ata_xfer_mask2mode - Find matching XFER_* for the given xfer_mask
845 * @xfer_mask: xfer_mask of interest
846 *
847 * Return matching XFER_* value for @xfer_mask. Only the highest
848 * bit of @xfer_mask is considered.
849 *
850 * LOCKING:
851 * None.
852 *
853 * RETURNS:
Tejun Heo70cd0712007-11-27 19:43:40 +0900854 * Matching XFER_* value, 0xff if no match found.
Tejun Heocb95d562006-03-06 04:31:56 +0900855 */
Tejun Heo7dc951a2007-11-27 19:43:42 +0900856u8 ata_xfer_mask2mode(unsigned long xfer_mask)
Tejun Heocb95d562006-03-06 04:31:56 +0900857{
858 int highbit = fls(xfer_mask) - 1;
859 const struct ata_xfer_ent *ent;
860
861 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
862 if (highbit >= ent->shift && highbit < ent->shift + ent->bits)
863 return ent->base + highbit - ent->shift;
Tejun Heo70cd0712007-11-27 19:43:40 +0900864 return 0xff;
Tejun Heocb95d562006-03-06 04:31:56 +0900865}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +0100866EXPORT_SYMBOL_GPL(ata_xfer_mask2mode);
Tejun Heocb95d562006-03-06 04:31:56 +0900867
868/**
869 * ata_xfer_mode2mask - Find matching xfer_mask for XFER_*
870 * @xfer_mode: XFER_* of interest
871 *
872 * Return matching xfer_mask for @xfer_mode.
873 *
874 * LOCKING:
875 * None.
876 *
877 * RETURNS:
878 * Matching xfer_mask, 0 if no match found.
879 */
Tejun Heo7dc951a2007-11-27 19:43:42 +0900880unsigned long ata_xfer_mode2mask(u8 xfer_mode)
Tejun Heocb95d562006-03-06 04:31:56 +0900881{
882 const struct ata_xfer_ent *ent;
883
884 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
885 if (xfer_mode >= ent->base && xfer_mode < ent->base + ent->bits)
Tejun Heo70cd0712007-11-27 19:43:40 +0900886 return ((2 << (ent->shift + xfer_mode - ent->base)) - 1)
887 & ~((1 << ent->shift) - 1);
Tejun Heocb95d562006-03-06 04:31:56 +0900888 return 0;
889}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +0100890EXPORT_SYMBOL_GPL(ata_xfer_mode2mask);
Tejun Heocb95d562006-03-06 04:31:56 +0900891
892/**
893 * ata_xfer_mode2shift - Find matching xfer_shift for XFER_*
894 * @xfer_mode: XFER_* of interest
895 *
896 * Return matching xfer_shift for @xfer_mode.
897 *
898 * LOCKING:
899 * None.
900 *
901 * RETURNS:
902 * Matching xfer_shift, -1 if no match found.
903 */
Tejun Heo7dc951a2007-11-27 19:43:42 +0900904int ata_xfer_mode2shift(unsigned long xfer_mode)
Tejun Heocb95d562006-03-06 04:31:56 +0900905{
906 const struct ata_xfer_ent *ent;
907
908 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
909 if (xfer_mode >= ent->base && xfer_mode < ent->base + ent->bits)
910 return ent->shift;
911 return -1;
912}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +0100913EXPORT_SYMBOL_GPL(ata_xfer_mode2shift);
Tejun Heocb95d562006-03-06 04:31:56 +0900914
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915/**
Tejun Heo1da7b0d2006-03-06 04:31:56 +0900916 * ata_mode_string - convert xfer_mask to string
917 * @xfer_mask: mask of bits supported; only highest bit counts.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 *
919 * Determine string which represents the highest speed
Tejun Heo1da7b0d2006-03-06 04:31:56 +0900920 * (highest bit in @modemask).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 *
922 * LOCKING:
923 * None.
924 *
925 * RETURNS:
926 * Constant C string representing highest speed listed in
Tejun Heo1da7b0d2006-03-06 04:31:56 +0900927 * @mode_mask, or the constant C string "<n/a>".
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 */
Tejun Heo7dc951a2007-11-27 19:43:42 +0900929const char *ata_mode_string(unsigned long xfer_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930{
Tejun Heo75f554b2006-03-06 04:31:57 +0900931 static const char * const xfer_mode_str[] = {
932 "PIO0",
933 "PIO1",
934 "PIO2",
935 "PIO3",
936 "PIO4",
Alan Coxb352e572006-08-10 18:52:12 +0100937 "PIO5",
938 "PIO6",
Tejun Heo75f554b2006-03-06 04:31:57 +0900939 "MWDMA0",
940 "MWDMA1",
941 "MWDMA2",
Alan Coxb352e572006-08-10 18:52:12 +0100942 "MWDMA3",
943 "MWDMA4",
Tejun Heo75f554b2006-03-06 04:31:57 +0900944 "UDMA/16",
945 "UDMA/25",
946 "UDMA/33",
947 "UDMA/44",
948 "UDMA/66",
949 "UDMA/100",
950 "UDMA/133",
951 "UDMA7",
952 };
Tejun Heo1da7b0d2006-03-06 04:31:56 +0900953 int highbit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954
Tejun Heo1da7b0d2006-03-06 04:31:56 +0900955 highbit = fls(xfer_mask) - 1;
956 if (highbit >= 0 && highbit < ARRAY_SIZE(xfer_mode_str))
957 return xfer_mode_str[highbit];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 return "<n/a>";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +0100960EXPORT_SYMBOL_GPL(ata_mode_string);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961
Gwendal Grignoud9027472010-05-25 12:31:38 -0700962const char *sata_spd_string(unsigned int spd)
Tejun Heo4c360c82006-04-01 01:38:17 +0900963{
964 static const char * const spd_str[] = {
965 "1.5 Gbps",
966 "3.0 Gbps",
Shane Huang8522ee22008-12-30 11:00:37 +0800967 "6.0 Gbps",
Tejun Heo4c360c82006-04-01 01:38:17 +0900968 };
969
970 if (spd == 0 || (spd - 1) >= ARRAY_SIZE(spd_str))
971 return "<unknown>";
972 return spd_str[spd - 1];
973}
974
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975/**
976 * ata_dev_classify - determine device type based on ATA-spec signature
977 * @tf: ATA taskfile register set for device to be identified
978 *
979 * Determine from taskfile register contents whether a device is
980 * ATA or ATAPI, as per "Signature and persistence" section
981 * of ATA/PI spec (volume 1, sect 5.14).
982 *
983 * LOCKING:
984 * None.
985 *
986 * RETURNS:
Hannes Reinecke9162c652014-11-05 13:08:21 +0100987 * Device type, %ATA_DEV_ATA, %ATA_DEV_ATAPI, %ATA_DEV_PMP,
988 * %ATA_DEV_ZAC, or %ATA_DEV_UNKNOWN the event of failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 */
Jeff Garzik057ace52005-10-22 14:27:05 -0400990unsigned int ata_dev_classify(const struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991{
992 /* Apple's open source Darwin code hints that some devices only
993 * put a proper signature into the LBA mid/high registers,
994 * So, we only check those. It's sufficient for uniqueness.
Tejun Heo633273a2007-09-23 13:19:54 +0900995 *
996 * ATA/ATAPI-7 (d1532v1r1: Feb. 19, 2003) specified separate
997 * signatures for ATA and ATAPI devices attached on SerialATA,
998 * 0x3c/0xc3 and 0x69/0x96 respectively. However, SerialATA
999 * spec has never mentioned about using different signatures
1000 * for ATA/ATAPI devices. Then, Serial ATA II: Port
1001 * Multiplier specification began to use 0x69/0x96 to identify
1002 * port multpliers and 0x3c/0xc3 to identify SEMB device.
1003 * ATA/ATAPI-7 dropped descriptions about 0x3c/0xc3 and
1004 * 0x69/0x96 shortly and described them as reserved for
1005 * SerialATA.
1006 *
1007 * We follow the current spec and consider that 0x69/0x96
1008 * identifies a port multiplier and 0x3c/0xc3 a SEMB device.
Tejun Heo79b42ba2009-04-15 06:21:10 +09001009 * Unfortunately, WDC WD1600JS-62MHB5 (a hard drive) reports
1010 * SEMB signature. This is worked around in
1011 * ata_dev_read_id().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 */
Tejun Heo633273a2007-09-23 13:19:54 +09001013 if ((tf->lbam == 0) && (tf->lbah == 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 DPRINTK("found ATA device by sig\n");
1015 return ATA_DEV_ATA;
1016 }
1017
Tejun Heo633273a2007-09-23 13:19:54 +09001018 if ((tf->lbam == 0x14) && (tf->lbah == 0xeb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 DPRINTK("found ATAPI device by sig\n");
1020 return ATA_DEV_ATAPI;
1021 }
1022
Tejun Heo633273a2007-09-23 13:19:54 +09001023 if ((tf->lbam == 0x69) && (tf->lbah == 0x96)) {
1024 DPRINTK("found PMP device by sig\n");
1025 return ATA_DEV_PMP;
1026 }
1027
1028 if ((tf->lbam == 0x3c) && (tf->lbah == 0xc3)) {
Tejun Heo79b42ba2009-04-15 06:21:10 +09001029 DPRINTK("found SEMB device by sig (could be ATA device)\n");
1030 return ATA_DEV_SEMB;
Tejun Heo633273a2007-09-23 13:19:54 +09001031 }
1032
Hannes Reinecke9162c652014-11-05 13:08:21 +01001033 if ((tf->lbam == 0xcd) && (tf->lbah == 0xab)) {
1034 DPRINTK("found ZAC device by sig\n");
1035 return ATA_DEV_ZAC;
1036 }
1037
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 DPRINTK("unknown device\n");
1039 return ATA_DEV_UNKNOWN;
1040}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01001041EXPORT_SYMBOL_GPL(ata_dev_classify);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042
1043/**
Tejun Heo6a62a042006-02-13 10:02:46 +09001044 * ata_id_string - Convert IDENTIFY DEVICE page into string
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 * @id: IDENTIFY DEVICE results we will examine
1046 * @s: string into which data is output
1047 * @ofs: offset into identify device page
1048 * @len: length of string to return. must be an even number.
1049 *
1050 * The strings in the IDENTIFY DEVICE page are broken up into
1051 * 16-bit chunks. Run through the string, and output each
1052 * 8-bit chunk linearly, regardless of platform.
1053 *
1054 * LOCKING:
1055 * caller.
1056 */
1057
Tejun Heo6a62a042006-02-13 10:02:46 +09001058void ata_id_string(const u16 *id, unsigned char *s,
1059 unsigned int ofs, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060{
1061 unsigned int c;
1062
Alan Cox963e4972008-07-24 17:16:06 +01001063 BUG_ON(len & 1);
1064
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 while (len > 0) {
1066 c = id[ofs] >> 8;
1067 *s = c;
1068 s++;
1069
1070 c = id[ofs] & 0xff;
1071 *s = c;
1072 s++;
1073
1074 ofs++;
1075 len -= 2;
1076 }
1077}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01001078EXPORT_SYMBOL_GPL(ata_id_string);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079
Tejun Heo0e949ff2006-02-12 22:47:04 +09001080/**
Tejun Heo6a62a042006-02-13 10:02:46 +09001081 * ata_id_c_string - Convert IDENTIFY DEVICE page into C string
Tejun Heo0e949ff2006-02-12 22:47:04 +09001082 * @id: IDENTIFY DEVICE results we will examine
1083 * @s: string into which data is output
1084 * @ofs: offset into identify device page
1085 * @len: length of string to return. must be an odd number.
1086 *
Tejun Heo6a62a042006-02-13 10:02:46 +09001087 * This function is identical to ata_id_string except that it
Tejun Heo0e949ff2006-02-12 22:47:04 +09001088 * trims trailing spaces and terminates the resulting string with
1089 * null. @len must be actual maximum length (even number) + 1.
1090 *
1091 * LOCKING:
1092 * caller.
1093 */
Tejun Heo6a62a042006-02-13 10:02:46 +09001094void ata_id_c_string(const u16 *id, unsigned char *s,
1095 unsigned int ofs, unsigned int len)
Tejun Heo0e949ff2006-02-12 22:47:04 +09001096{
1097 unsigned char *p;
1098
Tejun Heo6a62a042006-02-13 10:02:46 +09001099 ata_id_string(id, s, ofs, len - 1);
Tejun Heo0e949ff2006-02-12 22:47:04 +09001100
1101 p = s + strnlen(s, len - 1);
1102 while (p > s && p[-1] == ' ')
1103 p--;
1104 *p = '\0';
1105}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01001106EXPORT_SYMBOL_GPL(ata_id_c_string);
Edward Falk0baab862005-06-02 18:17:13 -04001107
Tejun Heodb6f8752007-09-03 12:31:58 +09001108static u64 ata_id_n_sectors(const u16 *id)
1109{
1110 if (ata_id_has_lba(id)) {
1111 if (ata_id_has_lba48(id))
Robert Hancock968e5942009-02-16 20:15:08 -06001112 return ata_id_u64(id, ATA_ID_LBA_CAPACITY_2);
Tejun Heodb6f8752007-09-03 12:31:58 +09001113 else
Robert Hancock968e5942009-02-16 20:15:08 -06001114 return ata_id_u32(id, ATA_ID_LBA_CAPACITY);
Tejun Heodb6f8752007-09-03 12:31:58 +09001115 } else {
1116 if (ata_id_current_chs_valid(id))
Robert Hancock968e5942009-02-16 20:15:08 -06001117 return id[ATA_ID_CUR_CYLS] * id[ATA_ID_CUR_HEADS] *
1118 id[ATA_ID_CUR_SECTORS];
Tejun Heodb6f8752007-09-03 12:31:58 +09001119 else
Robert Hancock968e5942009-02-16 20:15:08 -06001120 return id[ATA_ID_CYLS] * id[ATA_ID_HEADS] *
1121 id[ATA_ID_SECTORS];
Tejun Heodb6f8752007-09-03 12:31:58 +09001122 }
1123}
1124
Tejun Heoa5987e02008-03-27 19:14:23 +09001125u64 ata_tf_to_lba48(const struct ata_taskfile *tf)
Alan Cox1e999732007-04-11 00:23:13 +01001126{
1127 u64 sectors = 0;
1128
1129 sectors |= ((u64)(tf->hob_lbah & 0xff)) << 40;
1130 sectors |= ((u64)(tf->hob_lbam & 0xff)) << 32;
Roland Dreierba14a9c2008-10-28 16:52:20 -07001131 sectors |= ((u64)(tf->hob_lbal & 0xff)) << 24;
Alan Cox1e999732007-04-11 00:23:13 +01001132 sectors |= (tf->lbah & 0xff) << 16;
1133 sectors |= (tf->lbam & 0xff) << 8;
1134 sectors |= (tf->lbal & 0xff);
1135
Tejun Heoa5987e02008-03-27 19:14:23 +09001136 return sectors;
Alan Cox1e999732007-04-11 00:23:13 +01001137}
1138
Tejun Heoa5987e02008-03-27 19:14:23 +09001139u64 ata_tf_to_lba(const struct ata_taskfile *tf)
Alan Cox1e999732007-04-11 00:23:13 +01001140{
1141 u64 sectors = 0;
1142
1143 sectors |= (tf->device & 0x0f) << 24;
1144 sectors |= (tf->lbah & 0xff) << 16;
1145 sectors |= (tf->lbam & 0xff) << 8;
1146 sectors |= (tf->lbal & 0xff);
1147
Tejun Heoa5987e02008-03-27 19:14:23 +09001148 return sectors;
Alan Cox1e999732007-04-11 00:23:13 +01001149}
1150
1151/**
Tejun Heoc728a912007-09-03 12:32:30 +09001152 * ata_read_native_max_address - Read native max address
1153 * @dev: target device
1154 * @max_sectors: out parameter for the result native max address
Alan Cox1e999732007-04-11 00:23:13 +01001155 *
Tejun Heoc728a912007-09-03 12:32:30 +09001156 * Perform an LBA48 or LBA28 native size query upon the device in
1157 * question.
1158 *
1159 * RETURNS:
1160 * 0 on success, -EACCES if command is aborted by the drive.
1161 * -EIO on other errors.
Alan Cox1e999732007-04-11 00:23:13 +01001162 */
Tejun Heoc728a912007-09-03 12:32:30 +09001163static int ata_read_native_max_address(struct ata_device *dev, u64 *max_sectors)
Alan Cox1e999732007-04-11 00:23:13 +01001164{
Tejun Heoc728a912007-09-03 12:32:30 +09001165 unsigned int err_mask;
Alan Cox1e999732007-04-11 00:23:13 +01001166 struct ata_taskfile tf;
Tejun Heoc728a912007-09-03 12:32:30 +09001167 int lba48 = ata_id_has_lba48(dev->id);
Alan Cox1e999732007-04-11 00:23:13 +01001168
1169 ata_tf_init(dev, &tf);
1170
Tejun Heoc728a912007-09-03 12:32:30 +09001171 /* always clear all address registers */
Alan Cox1e999732007-04-11 00:23:13 +01001172 tf.flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
Tejun Heoc728a912007-09-03 12:32:30 +09001173
1174 if (lba48) {
1175 tf.command = ATA_CMD_READ_NATIVE_MAX_EXT;
1176 tf.flags |= ATA_TFLAG_LBA48;
1177 } else
1178 tf.command = ATA_CMD_READ_NATIVE_MAX;
1179
Hannes Reineckebd18bc02016-07-14 09:05:46 +09001180 tf.protocol = ATA_PROT_NODATA;
Tejun Heoc728a912007-09-03 12:32:30 +09001181 tf.device |= ATA_LBA;
Alan Cox1e999732007-04-11 00:23:13 +01001182
Tejun Heo2b789102007-10-09 15:05:44 +09001183 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
Tejun Heoc728a912007-09-03 12:32:30 +09001184 if (err_mask) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07001185 ata_dev_warn(dev,
1186 "failed to read native max address (err_mask=0x%x)\n",
1187 err_mask);
Tejun Heoc728a912007-09-03 12:32:30 +09001188 if (err_mask == AC_ERR_DEV && (tf.feature & ATA_ABORTED))
1189 return -EACCES;
1190 return -EIO;
1191 }
Alan Cox1e999732007-04-11 00:23:13 +01001192
Tejun Heoc728a912007-09-03 12:32:30 +09001193 if (lba48)
Tejun Heoa5987e02008-03-27 19:14:23 +09001194 *max_sectors = ata_tf_to_lba48(&tf) + 1;
Tejun Heoc728a912007-09-03 12:32:30 +09001195 else
Tejun Heoa5987e02008-03-27 19:14:23 +09001196 *max_sectors = ata_tf_to_lba(&tf) + 1;
Jeff Garzik2dcb4072007-10-19 06:42:56 -04001197 if (dev->horkage & ATA_HORKAGE_HPA_SIZE)
Alan Cox93328e112007-09-29 04:06:48 -04001198 (*max_sectors)--;
Tejun Heoc728a912007-09-03 12:32:30 +09001199 return 0;
Alan Cox1e999732007-04-11 00:23:13 +01001200}
1201
1202/**
Tejun Heoc728a912007-09-03 12:32:30 +09001203 * ata_set_max_sectors - Set max sectors
1204 * @dev: target device
Randy Dunlap6b38d1d2007-05-01 17:35:55 -07001205 * @new_sectors: new max sectors value to set for the device
Alan Cox1e999732007-04-11 00:23:13 +01001206 *
Tejun Heoc728a912007-09-03 12:32:30 +09001207 * Set max sectors of @dev to @new_sectors.
1208 *
1209 * RETURNS:
1210 * 0 on success, -EACCES if command is aborted or denied (due to
1211 * previous non-volatile SET_MAX) by the drive. -EIO on other
1212 * errors.
Alan Cox1e999732007-04-11 00:23:13 +01001213 */
Tejun Heo05027ad2007-09-03 12:32:57 +09001214static int ata_set_max_sectors(struct ata_device *dev, u64 new_sectors)
Alan Cox1e999732007-04-11 00:23:13 +01001215{
Tejun Heoc728a912007-09-03 12:32:30 +09001216 unsigned int err_mask;
Alan Cox1e999732007-04-11 00:23:13 +01001217 struct ata_taskfile tf;
Tejun Heoc728a912007-09-03 12:32:30 +09001218 int lba48 = ata_id_has_lba48(dev->id);
Alan Cox1e999732007-04-11 00:23:13 +01001219
1220 new_sectors--;
1221
1222 ata_tf_init(dev, &tf);
1223
Alan Cox1e999732007-04-11 00:23:13 +01001224 tf.flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
Tejun Heoc728a912007-09-03 12:32:30 +09001225
1226 if (lba48) {
1227 tf.command = ATA_CMD_SET_MAX_EXT;
1228 tf.flags |= ATA_TFLAG_LBA48;
1229
1230 tf.hob_lbal = (new_sectors >> 24) & 0xff;
1231 tf.hob_lbam = (new_sectors >> 32) & 0xff;
1232 tf.hob_lbah = (new_sectors >> 40) & 0xff;
Tejun Heo1e582ba2007-09-21 20:07:14 +09001233 } else {
Tejun Heoc728a912007-09-03 12:32:30 +09001234 tf.command = ATA_CMD_SET_MAX;
1235
Tejun Heo1e582ba2007-09-21 20:07:14 +09001236 tf.device |= (new_sectors >> 24) & 0xf;
1237 }
1238
Hannes Reineckebd18bc02016-07-14 09:05:46 +09001239 tf.protocol = ATA_PROT_NODATA;
Tejun Heoc728a912007-09-03 12:32:30 +09001240 tf.device |= ATA_LBA;
Alan Cox1e999732007-04-11 00:23:13 +01001241
1242 tf.lbal = (new_sectors >> 0) & 0xff;
1243 tf.lbam = (new_sectors >> 8) & 0xff;
1244 tf.lbah = (new_sectors >> 16) & 0xff;
Alan Cox1e999732007-04-11 00:23:13 +01001245
Tejun Heo2b789102007-10-09 15:05:44 +09001246 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
Tejun Heoc728a912007-09-03 12:32:30 +09001247 if (err_mask) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07001248 ata_dev_warn(dev,
1249 "failed to set max address (err_mask=0x%x)\n",
1250 err_mask);
Tejun Heoc728a912007-09-03 12:32:30 +09001251 if (err_mask == AC_ERR_DEV &&
1252 (tf.feature & (ATA_ABORTED | ATA_IDNF)))
1253 return -EACCES;
1254 return -EIO;
1255 }
Alan Cox1e999732007-04-11 00:23:13 +01001256
Tejun Heoc728a912007-09-03 12:32:30 +09001257 return 0;
Alan Cox1e999732007-04-11 00:23:13 +01001258}
1259
1260/**
1261 * ata_hpa_resize - Resize a device with an HPA set
1262 * @dev: Device to resize
1263 *
1264 * Read the size of an LBA28 or LBA48 disk with HPA features and resize
1265 * it if required to the full size of the media. The caller must check
1266 * the drive has the HPA feature set enabled.
Tejun Heo05027ad2007-09-03 12:32:57 +09001267 *
1268 * RETURNS:
1269 * 0 on success, -errno on failure.
Alan Cox1e999732007-04-11 00:23:13 +01001270 */
Tejun Heo05027ad2007-09-03 12:32:57 +09001271static int ata_hpa_resize(struct ata_device *dev)
Alan Cox1e999732007-04-11 00:23:13 +01001272{
Damien Le Moal891fd7c2021-08-16 10:44:50 +09001273 bool print_info = ata_dev_print_info(dev);
Tejun Heo445d2112010-04-05 10:33:13 +09001274 bool unlock_hpa = ata_ignore_hpa || dev->flags & ATA_DFLAG_UNLOCK_HPA;
Tejun Heo05027ad2007-09-03 12:32:57 +09001275 u64 sectors = ata_id_n_sectors(dev->id);
1276 u64 native_sectors;
Tejun Heoc728a912007-09-03 12:32:30 +09001277 int rc;
Jeff Garzika617c092007-05-21 20:14:23 -04001278
Tejun Heo05027ad2007-09-03 12:32:57 +09001279 /* do we need to do it? */
Hannes Reinecke9162c652014-11-05 13:08:21 +01001280 if ((dev->class != ATA_DEV_ATA && dev->class != ATA_DEV_ZAC) ||
Tejun Heo05027ad2007-09-03 12:32:57 +09001281 !ata_id_has_lba(dev->id) || !ata_id_hpa_enabled(dev->id) ||
1282 (dev->horkage & ATA_HORKAGE_BROKEN_HPA))
Tejun Heoc728a912007-09-03 12:32:30 +09001283 return 0;
Alan Cox1e999732007-04-11 00:23:13 +01001284
Tejun Heo05027ad2007-09-03 12:32:57 +09001285 /* read native max address */
1286 rc = ata_read_native_max_address(dev, &native_sectors);
1287 if (rc) {
Tejun Heodda7aba2008-03-23 21:05:15 +09001288 /* If device aborted the command or HPA isn't going to
1289 * be unlocked, skip HPA resizing.
Tejun Heo05027ad2007-09-03 12:32:57 +09001290 */
Tejun Heo445d2112010-04-05 10:33:13 +09001291 if (rc == -EACCES || !unlock_hpa) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07001292 ata_dev_warn(dev,
1293 "HPA support seems broken, skipping HPA handling\n");
Tejun Heo05027ad2007-09-03 12:32:57 +09001294 dev->horkage |= ATA_HORKAGE_BROKEN_HPA;
Alan Cox1e999732007-04-11 00:23:13 +01001295
Tejun Heo05027ad2007-09-03 12:32:57 +09001296 /* we can continue if device aborted the command */
1297 if (rc == -EACCES)
1298 rc = 0;
Alan Cox1e999732007-04-11 00:23:13 +01001299 }
Tejun Heo37301a52007-06-25 20:45:54 +09001300
Tejun Heo05027ad2007-09-03 12:32:57 +09001301 return rc;
1302 }
Tejun Heo5920dad2009-07-15 17:11:41 +09001303 dev->n_native_sectors = native_sectors;
Tejun Heo05027ad2007-09-03 12:32:57 +09001304
1305 /* nothing to do? */
Tejun Heo445d2112010-04-05 10:33:13 +09001306 if (native_sectors <= sectors || !unlock_hpa) {
Tejun Heo05027ad2007-09-03 12:32:57 +09001307 if (!print_info || native_sectors == sectors)
1308 return 0;
1309
1310 if (native_sectors > sectors)
Joe Perchesa9a79df2011-04-15 15:51:59 -07001311 ata_dev_info(dev,
Tejun Heo05027ad2007-09-03 12:32:57 +09001312 "HPA detected: current %llu, native %llu\n",
1313 (unsigned long long)sectors,
1314 (unsigned long long)native_sectors);
1315 else if (native_sectors < sectors)
Joe Perchesa9a79df2011-04-15 15:51:59 -07001316 ata_dev_warn(dev,
1317 "native sectors (%llu) is smaller than sectors (%llu)\n",
Tejun Heo05027ad2007-09-03 12:32:57 +09001318 (unsigned long long)native_sectors,
1319 (unsigned long long)sectors);
1320 return 0;
1321 }
1322
1323 /* let's unlock HPA */
1324 rc = ata_set_max_sectors(dev, native_sectors);
1325 if (rc == -EACCES) {
1326 /* if device aborted the command, skip HPA resizing */
Joe Perchesa9a79df2011-04-15 15:51:59 -07001327 ata_dev_warn(dev,
1328 "device aborted resize (%llu -> %llu), skipping HPA handling\n",
1329 (unsigned long long)sectors,
1330 (unsigned long long)native_sectors);
Tejun Heo05027ad2007-09-03 12:32:57 +09001331 dev->horkage |= ATA_HORKAGE_BROKEN_HPA;
1332 return 0;
1333 } else if (rc)
1334 return rc;
1335
1336 /* re-read IDENTIFY data */
1337 rc = ata_dev_reread_id(dev, 0);
1338 if (rc) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07001339 ata_dev_err(dev,
1340 "failed to re-read IDENTIFY data after HPA resizing\n");
Tejun Heo05027ad2007-09-03 12:32:57 +09001341 return rc;
1342 }
1343
1344 if (print_info) {
1345 u64 new_sectors = ata_id_n_sectors(dev->id);
Joe Perchesa9a79df2011-04-15 15:51:59 -07001346 ata_dev_info(dev,
Tejun Heo05027ad2007-09-03 12:32:57 +09001347 "HPA unlocked: %llu -> %llu, native %llu\n",
1348 (unsigned long long)sectors,
1349 (unsigned long long)new_sectors,
1350 (unsigned long long)native_sectors);
1351 }
1352
1353 return 0;
Alan Cox1e999732007-04-11 00:23:13 +01001354}
1355
Edward Falk0baab862005-06-02 18:17:13 -04001356/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 * ata_dump_id - IDENTIFY DEVICE info debugging output
Tejun Heo0bd33002006-02-12 22:47:05 +09001358 * @id: IDENTIFY DEVICE page to dump
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 *
Tejun Heo0bd33002006-02-12 22:47:05 +09001360 * Dump selected 16-bit words from the given IDENTIFY DEVICE
1361 * page.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 *
1363 * LOCKING:
1364 * caller.
1365 */
1366
Tejun Heo0bd33002006-02-12 22:47:05 +09001367static inline void ata_dump_id(const u16 *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368{
1369 DPRINTK("49==0x%04x "
1370 "53==0x%04x "
1371 "63==0x%04x "
1372 "64==0x%04x "
1373 "75==0x%04x \n",
Tejun Heo0bd33002006-02-12 22:47:05 +09001374 id[49],
1375 id[53],
1376 id[63],
1377 id[64],
1378 id[75]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 DPRINTK("80==0x%04x "
1380 "81==0x%04x "
1381 "82==0x%04x "
1382 "83==0x%04x "
1383 "84==0x%04x \n",
Tejun Heo0bd33002006-02-12 22:47:05 +09001384 id[80],
1385 id[81],
1386 id[82],
1387 id[83],
1388 id[84]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 DPRINTK("88==0x%04x "
1390 "93==0x%04x\n",
Tejun Heo0bd33002006-02-12 22:47:05 +09001391 id[88],
1392 id[93]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393}
1394
Tejun Heocb95d562006-03-06 04:31:56 +09001395/**
1396 * ata_id_xfermask - Compute xfermask from the given IDENTIFY data
1397 * @id: IDENTIFY data to compute xfer mask from
1398 *
1399 * Compute the xfermask for this device. This is not as trivial
1400 * as it seems if we must consider early devices correctly.
1401 *
1402 * FIXME: pre IDE drive timing (do we care ?).
1403 *
1404 * LOCKING:
1405 * None.
1406 *
1407 * RETURNS:
1408 * Computed xfermask
1409 */
Tejun Heo7dc951a2007-11-27 19:43:42 +09001410unsigned long ata_id_xfermask(const u16 *id)
Tejun Heocb95d562006-03-06 04:31:56 +09001411{
Tejun Heo7dc951a2007-11-27 19:43:42 +09001412 unsigned long pio_mask, mwdma_mask, udma_mask;
Tejun Heocb95d562006-03-06 04:31:56 +09001413
1414 /* Usual case. Word 53 indicates word 64 is valid */
1415 if (id[ATA_ID_FIELD_VALID] & (1 << 1)) {
1416 pio_mask = id[ATA_ID_PIO_MODES] & 0x03;
1417 pio_mask <<= 3;
1418 pio_mask |= 0x7;
1419 } else {
1420 /* If word 64 isn't valid then Word 51 high byte holds
1421 * the PIO timing number for the maximum. Turn it into
1422 * a mask.
1423 */
Lennert Buytenhek7a0f1c82007-01-29 13:28:47 +01001424 u8 mode = (id[ATA_ID_OLD_PIO_MODES] >> 8) & 0xFF;
Alan Cox46767aeb2006-09-29 18:26:47 +01001425 if (mode < 5) /* Valid PIO range */
Jeff Garzik2dcb4072007-10-19 06:42:56 -04001426 pio_mask = (2 << mode) - 1;
Alan Cox46767aeb2006-09-29 18:26:47 +01001427 else
1428 pio_mask = 1;
Tejun Heocb95d562006-03-06 04:31:56 +09001429
1430 /* But wait.. there's more. Design your standards by
1431 * committee and you too can get a free iordy field to
1432 * process. However its the speeds not the modes that
1433 * are supported... Note drivers using the timing API
1434 * will get this right anyway
1435 */
1436 }
1437
1438 mwdma_mask = id[ATA_ID_MWDMA_MODES] & 0x07;
Tejun Heofb21f0d2006-03-12 12:34:35 +09001439
Alan Coxb352e572006-08-10 18:52:12 +01001440 if (ata_id_is_cfa(id)) {
1441 /*
1442 * Process compact flash extended modes
1443 */
Sergei Shtylyov62afe5d2009-04-13 20:50:00 +04001444 int pio = (id[ATA_ID_CFA_MODES] >> 0) & 0x7;
1445 int dma = (id[ATA_ID_CFA_MODES] >> 3) & 0x7;
Alan Coxb352e572006-08-10 18:52:12 +01001446
1447 if (pio)
1448 pio_mask |= (1 << 5);
1449 if (pio > 1)
1450 pio_mask |= (1 << 6);
1451 if (dma)
1452 mwdma_mask |= (1 << 3);
1453 if (dma > 1)
1454 mwdma_mask |= (1 << 4);
1455 }
1456
Tejun Heofb21f0d2006-03-12 12:34:35 +09001457 udma_mask = 0;
1458 if (id[ATA_ID_FIELD_VALID] & (1 << 2))
1459 udma_mask = id[ATA_ID_UDMA_MODES] & 0xff;
Tejun Heocb95d562006-03-06 04:31:56 +09001460
1461 return ata_pack_xfermask(pio_mask, mwdma_mask, udma_mask);
1462}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01001463EXPORT_SYMBOL_GPL(ata_id_xfermask);
Tejun Heocb95d562006-03-06 04:31:56 +09001464
Adrian Bunk7102d232007-01-04 00:09:36 +01001465static void ata_qc_complete_internal(struct ata_queued_cmd *qc)
Tejun Heoa2a7a662005-12-13 14:48:31 +09001466{
Tejun Heo77853bf2006-01-23 13:09:36 +09001467 struct completion *waiting = qc->private_data;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001468
Tejun Heoa2a7a662005-12-13 14:48:31 +09001469 complete(waiting);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001470}
1471
1472/**
Tejun Heo24326972006-11-14 22:47:09 +09001473 * ata_exec_internal_sg - execute libata internal command
Tejun Heoa2a7a662005-12-13 14:48:31 +09001474 * @dev: Device to which the command is sent
1475 * @tf: Taskfile registers for the command and the result
Tejun Heod69cf372006-04-02 18:51:53 +09001476 * @cdb: CDB for packet command
Masanari Iidae2278672014-02-18 22:54:36 +09001477 * @dma_dir: Data transfer direction of the command
Randy Dunlap5c1ad8b2007-10-18 14:12:26 -07001478 * @sgl: sg list for the data buffer of the command
Tejun Heo24326972006-11-14 22:47:09 +09001479 * @n_elem: Number of sg entries
Tejun Heo2b789102007-10-09 15:05:44 +09001480 * @timeout: Timeout in msecs (0 for default)
Tejun Heoa2a7a662005-12-13 14:48:31 +09001481 *
1482 * Executes libata internal command with timeout. @tf contains
1483 * command on entry and result on return. Timeout and error
1484 * conditions are reported via return value. No recovery action
1485 * is taken after a command times out. It's caller's duty to
1486 * clean up after timeout.
1487 *
1488 * LOCKING:
1489 * None. Should be called with kernel context, might sleep.
Tejun Heo551e8882006-06-12 14:09:49 +09001490 *
1491 * RETURNS:
1492 * Zero on success, AC_ERR_* mask on failure
Tejun Heoa2a7a662005-12-13 14:48:31 +09001493 */
Tejun Heo24326972006-11-14 22:47:09 +09001494unsigned ata_exec_internal_sg(struct ata_device *dev,
1495 struct ata_taskfile *tf, const u8 *cdb,
Jens Axboe87260212007-10-16 11:14:12 +02001496 int dma_dir, struct scatterlist *sgl,
Tejun Heo2b789102007-10-09 15:05:44 +09001497 unsigned int n_elem, unsigned long timeout)
Tejun Heoa2a7a662005-12-13 14:48:31 +09001498{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001499 struct ata_link *link = dev->link;
1500 struct ata_port *ap = link->ap;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001501 u8 command = tf->command;
Tejun Heo87fbc5a2008-05-20 02:17:54 +09001502 int auto_timeout = 0;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001503 struct ata_queued_cmd *qc;
Jens Axboe28361c42018-05-11 12:51:09 -06001504 unsigned int preempted_tag;
Jens Axboee3ed89392018-05-11 12:51:05 -06001505 u32 preempted_sactive;
1506 u64 preempted_qc_active;
Tejun Heoda917d62007-09-23 13:14:12 +09001507 int preempted_nr_active_links;
Ingo Molnar60be6b92006-07-03 00:25:26 -07001508 DECLARE_COMPLETION_ONSTACK(wait);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001509 unsigned long flags;
Tejun Heo77853bf2006-01-23 13:09:36 +09001510 unsigned int err_mask;
Tejun Heod95a7172006-05-15 20:58:14 +09001511 int rc;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001512
Jeff Garzikba6a1302006-06-22 23:46:10 -04001513 spin_lock_irqsave(ap->lock, flags);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001514
Tejun Heoe3180492006-05-15 20:58:09 +09001515 /* no internal command while frozen */
Tejun Heob51e9e52006-06-29 01:29:30 +09001516 if (ap->pflags & ATA_PFLAG_FROZEN) {
Jeff Garzikba6a1302006-06-22 23:46:10 -04001517 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoe3180492006-05-15 20:58:09 +09001518 return AC_ERR_SYSTEM;
1519 }
1520
Tejun Heo2ab7db12006-05-15 20:58:02 +09001521 /* initialize internal qc */
Jens Axboe28361c42018-05-11 12:51:09 -06001522 qc = __ata_qc_from_tag(ap, ATA_TAG_INTERNAL);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001523
Jens Axboe28361c42018-05-11 12:51:09 -06001524 qc->tag = ATA_TAG_INTERNAL;
1525 qc->hw_tag = 0;
Tejun Heo2ab7db12006-05-15 20:58:02 +09001526 qc->scsicmd = NULL;
1527 qc->ap = ap;
1528 qc->dev = dev;
1529 ata_qc_reinit(qc);
1530
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001531 preempted_tag = link->active_tag;
1532 preempted_sactive = link->sactive;
Tejun Heodedaf2b2006-05-15 21:03:43 +09001533 preempted_qc_active = ap->qc_active;
Tejun Heoda917d62007-09-23 13:14:12 +09001534 preempted_nr_active_links = ap->nr_active_links;
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001535 link->active_tag = ATA_TAG_POISON;
1536 link->sactive = 0;
Tejun Heodedaf2b2006-05-15 21:03:43 +09001537 ap->qc_active = 0;
Tejun Heoda917d62007-09-23 13:14:12 +09001538 ap->nr_active_links = 0;
Tejun Heo2ab7db12006-05-15 20:58:02 +09001539
1540 /* prepare & issue qc */
Tejun Heoa2a7a662005-12-13 14:48:31 +09001541 qc->tf = *tf;
Tejun Heod69cf372006-04-02 18:51:53 +09001542 if (cdb)
1543 memcpy(qc->cdb, cdb, ATAPI_CDB_LEN);
Vincent Pelletiere7714512013-05-18 18:44:04 +02001544
1545 /* some SATA bridges need us to indicate data xfer direction */
1546 if (tf->protocol == ATAPI_PROT_DMA && (dev->flags & ATA_DFLAG_DMADIR) &&
1547 dma_dir == DMA_FROM_DEVICE)
1548 qc->tf.feature |= ATAPI_DMADIR;
1549
Tejun Heoe61e0672006-05-15 20:57:40 +09001550 qc->flags |= ATA_QCFLAG_RESULT_TF;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001551 qc->dma_dir = dma_dir;
1552 if (dma_dir != DMA_NONE) {
Tejun Heo24326972006-11-14 22:47:09 +09001553 unsigned int i, buflen = 0;
Jens Axboe87260212007-10-16 11:14:12 +02001554 struct scatterlist *sg;
Tejun Heo24326972006-11-14 22:47:09 +09001555
Jens Axboe87260212007-10-16 11:14:12 +02001556 for_each_sg(sgl, sg, n_elem, i)
1557 buflen += sg->length;
Tejun Heo24326972006-11-14 22:47:09 +09001558
Jens Axboe87260212007-10-16 11:14:12 +02001559 ata_sg_init(qc, sgl, n_elem);
Brian King49c80422007-01-30 11:32:26 -06001560 qc->nbytes = buflen;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001561 }
1562
Tejun Heo77853bf2006-01-23 13:09:36 +09001563 qc->private_data = &wait;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001564 qc->complete_fn = ata_qc_complete_internal;
1565
Tejun Heo8e0e6942006-03-31 20:41:11 +09001566 ata_qc_issue(qc);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001567
Jeff Garzikba6a1302006-06-22 23:46:10 -04001568 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001569
Tejun Heo87fbc5a2008-05-20 02:17:54 +09001570 if (!timeout) {
1571 if (ata_probe_timeout)
1572 timeout = ata_probe_timeout * 1000;
1573 else {
1574 timeout = ata_internal_cmd_timeout(dev, command);
1575 auto_timeout = 1;
1576 }
1577 }
Tejun Heo2b789102007-10-09 15:05:44 +09001578
Tejun Heoc0c362b2010-09-06 17:57:14 +02001579 if (ap->ops->error_handler)
1580 ata_eh_release(ap);
1581
Tejun Heo2b789102007-10-09 15:05:44 +09001582 rc = wait_for_completion_timeout(&wait, msecs_to_jiffies(timeout));
Albert Lee41ade502006-03-14 11:19:04 +08001583
Tejun Heoc0c362b2010-09-06 17:57:14 +02001584 if (ap->ops->error_handler)
1585 ata_eh_acquire(ap);
1586
Tejun Heoc4291372010-05-10 21:41:38 +02001587 ata_sff_flush_pio_task(ap);
Tejun Heod95a7172006-05-15 20:58:14 +09001588
1589 if (!rc) {
Jeff Garzikba6a1302006-06-22 23:46:10 -04001590 spin_lock_irqsave(ap->lock, flags);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001591
1592 /* We're racing with irq here. If we lose, the
1593 * following test prevents us from completing the qc
Tejun Heod95a7172006-05-15 20:58:14 +09001594 * twice. If we win, the port is frozen and will be
1595 * cleaned up by ->post_internal_cmd().
Tejun Heoa2a7a662005-12-13 14:48:31 +09001596 */
Tejun Heo77853bf2006-01-23 13:09:36 +09001597 if (qc->flags & ATA_QCFLAG_ACTIVE) {
Tejun Heod95a7172006-05-15 20:58:14 +09001598 qc->err_mask |= AC_ERR_TIMEOUT;
1599
1600 if (ap->ops->error_handler)
1601 ata_port_freeze(ap);
1602 else
1603 ata_qc_complete(qc);
Tejun Heof15a1da2006-05-15 20:57:56 +09001604
Borislav Petkov0dd4b212006-06-23 02:29:08 -04001605 if (ata_msg_warn(ap))
Joe Perchesa9a79df2011-04-15 15:51:59 -07001606 ata_dev_warn(dev, "qc timeout (cmd 0x%x)\n",
1607 command);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001608 }
1609
Jeff Garzikba6a1302006-06-22 23:46:10 -04001610 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001611 }
1612
Tejun Heod95a7172006-05-15 20:58:14 +09001613 /* do post_internal_cmd */
1614 if (ap->ops->post_internal_cmd)
1615 ap->ops->post_internal_cmd(qc);
1616
Tejun Heoa51d6442007-03-20 15:24:11 +09001617 /* perform minimal error analysis */
1618 if (qc->flags & ATA_QCFLAG_FAILED) {
1619 if (qc->result_tf.command & (ATA_ERR | ATA_DF))
1620 qc->err_mask |= AC_ERR_DEV;
1621
1622 if (!qc->err_mask)
1623 qc->err_mask |= AC_ERR_OTHER;
1624
1625 if (qc->err_mask & ~AC_ERR_OTHER)
1626 qc->err_mask &= ~AC_ERR_OTHER;
Damien Le Moal2dae9952016-12-19 10:17:40 +09001627 } else if (qc->tf.command == ATA_CMD_REQ_SENSE_DATA) {
1628 qc->result_tf.command |= ATA_SENSE;
Tejun Heod95a7172006-05-15 20:58:14 +09001629 }
1630
Tejun Heo15869302006-05-15 20:57:33 +09001631 /* finish up */
Jeff Garzikba6a1302006-06-22 23:46:10 -04001632 spin_lock_irqsave(ap->lock, flags);
Tejun Heo15869302006-05-15 20:57:33 +09001633
Tejun Heoe61e0672006-05-15 20:57:40 +09001634 *tf = qc->result_tf;
Tejun Heo77853bf2006-01-23 13:09:36 +09001635 err_mask = qc->err_mask;
1636
1637 ata_qc_free(qc);
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001638 link->active_tag = preempted_tag;
1639 link->sactive = preempted_sactive;
Tejun Heodedaf2b2006-05-15 21:03:43 +09001640 ap->qc_active = preempted_qc_active;
Tejun Heoda917d62007-09-23 13:14:12 +09001641 ap->nr_active_links = preempted_nr_active_links;
Tejun Heo77853bf2006-01-23 13:09:36 +09001642
Jeff Garzikba6a1302006-06-22 23:46:10 -04001643 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heo15869302006-05-15 20:57:33 +09001644
Tejun Heo87fbc5a2008-05-20 02:17:54 +09001645 if ((err_mask & AC_ERR_TIMEOUT) && auto_timeout)
1646 ata_internal_cmd_timed_out(dev, command);
1647
Tejun Heo77853bf2006-01-23 13:09:36 +09001648 return err_mask;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001649}
1650
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651/**
Tejun Heo33480a02006-12-12 02:15:31 +09001652 * ata_exec_internal - execute libata internal command
Tejun Heo24326972006-11-14 22:47:09 +09001653 * @dev: Device to which the command is sent
1654 * @tf: Taskfile registers for the command and the result
1655 * @cdb: CDB for packet command
Masanari Iidae2278672014-02-18 22:54:36 +09001656 * @dma_dir: Data transfer direction of the command
Tejun Heo24326972006-11-14 22:47:09 +09001657 * @buf: Data buffer of the command
1658 * @buflen: Length of data buffer
Tejun Heo2b789102007-10-09 15:05:44 +09001659 * @timeout: Timeout in msecs (0 for default)
Tejun Heo24326972006-11-14 22:47:09 +09001660 *
1661 * Wrapper around ata_exec_internal_sg() which takes simple
1662 * buffer instead of sg list.
1663 *
1664 * LOCKING:
1665 * None. Should be called with kernel context, might sleep.
1666 *
1667 * RETURNS:
1668 * Zero on success, AC_ERR_* mask on failure
1669 */
1670unsigned ata_exec_internal(struct ata_device *dev,
1671 struct ata_taskfile *tf, const u8 *cdb,
Tejun Heo2b789102007-10-09 15:05:44 +09001672 int dma_dir, void *buf, unsigned int buflen,
1673 unsigned long timeout)
Tejun Heo24326972006-11-14 22:47:09 +09001674{
Tejun Heo33480a02006-12-12 02:15:31 +09001675 struct scatterlist *psg = NULL, sg;
1676 unsigned int n_elem = 0;
Tejun Heo24326972006-11-14 22:47:09 +09001677
Tejun Heo33480a02006-12-12 02:15:31 +09001678 if (dma_dir != DMA_NONE) {
1679 WARN_ON(!buf);
1680 sg_init_one(&sg, buf, buflen);
1681 psg = &sg;
1682 n_elem++;
1683 }
Tejun Heo24326972006-11-14 22:47:09 +09001684
Tejun Heo2b789102007-10-09 15:05:44 +09001685 return ata_exec_internal_sg(dev, tf, cdb, dma_dir, psg, n_elem,
1686 timeout);
Tejun Heo24326972006-11-14 22:47:09 +09001687}
1688
1689/**
Alan Cox1bc4ccf2006-01-09 17:18:14 +00001690 * ata_pio_need_iordy - check if iordy needed
1691 * @adev: ATA device
1692 *
1693 * Check if the current speed of the device requires IORDY. Used
1694 * by various controllers for chip configuration.
1695 */
Alan Cox1bc4ccf2006-01-09 17:18:14 +00001696unsigned int ata_pio_need_iordy(const struct ata_device *adev)
1697{
Tejun Heo0d9e6652009-06-11 11:04:45 +09001698 /* Don't set IORDY if we're preparing for reset. IORDY may
1699 * lead to controller lock up on certain controllers if the
1700 * port is not occupied. See bko#11703 for details.
1701 */
1702 if (adev->link->ap->pflags & ATA_PFLAG_RESETTING)
1703 return 0;
1704 /* Controller doesn't support IORDY. Probably a pointless
1705 * check as the caller should know this.
1706 */
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001707 if (adev->link->ap->flags & ATA_FLAG_NO_IORDY)
Alan Cox1bc4ccf2006-01-09 17:18:14 +00001708 return 0;
David Daney5c18c4d2008-12-10 15:39:12 -08001709 /* CF spec. r4.1 Table 22 says no iordy on PIO5 and PIO6. */
1710 if (ata_id_is_cfa(adev->id)
1711 && (adev->pio_mode == XFER_PIO_5 || adev->pio_mode == XFER_PIO_6))
1712 return 0;
Alan Cox432729f2007-03-08 23:22:59 +00001713 /* PIO3 and higher it is mandatory */
1714 if (adev->pio_mode > XFER_PIO_2)
Alan Cox1bc4ccf2006-01-09 17:18:14 +00001715 return 1;
Alan Cox432729f2007-03-08 23:22:59 +00001716 /* We turn it on when possible */
1717 if (ata_id_has_iordy(adev->id))
1718 return 1;
1719 return 0;
1720}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01001721EXPORT_SYMBOL_GPL(ata_pio_need_iordy);
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05001722
Alan Cox432729f2007-03-08 23:22:59 +00001723/**
1724 * ata_pio_mask_no_iordy - Return the non IORDY mask
1725 * @adev: ATA device
1726 *
1727 * Compute the highest mode possible if we are not using iordy. Return
1728 * -1 if no iordy mode is available.
1729 */
Alan Cox432729f2007-03-08 23:22:59 +00001730static u32 ata_pio_mask_no_iordy(const struct ata_device *adev)
1731{
Alan Cox1bc4ccf2006-01-09 17:18:14 +00001732 /* If we have no drive specific rule, then PIO 2 is non IORDY */
Alan Cox1bc4ccf2006-01-09 17:18:14 +00001733 if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE */
Alan Cox432729f2007-03-08 23:22:59 +00001734 u16 pio = adev->id[ATA_ID_EIDE_PIO];
Alan Cox1bc4ccf2006-01-09 17:18:14 +00001735 /* Is the speed faster than the drive allows non IORDY ? */
1736 if (pio) {
1737 /* This is cycle times not frequency - watch the logic! */
1738 if (pio > 240) /* PIO2 is 240nS per cycle */
Alan Cox432729f2007-03-08 23:22:59 +00001739 return 3 << ATA_SHIFT_PIO;
1740 return 7 << ATA_SHIFT_PIO;
Alan Cox1bc4ccf2006-01-09 17:18:14 +00001741 }
1742 }
Alan Cox432729f2007-03-08 23:22:59 +00001743 return 3 << ATA_SHIFT_PIO;
Alan Cox1bc4ccf2006-01-09 17:18:14 +00001744}
1745
1746/**
Alan Cox963e4972008-07-24 17:16:06 +01001747 * ata_do_dev_read_id - default ID read method
1748 * @dev: device
1749 * @tf: proposed taskfile
1750 * @id: data buffer
1751 *
1752 * Issue the identify taskfile and hand back the buffer containing
1753 * identify data. For some RAID controllers and for pre ATA devices
1754 * this function is wrapped or replaced by the driver
1755 */
1756unsigned int ata_do_dev_read_id(struct ata_device *dev,
1757 struct ata_taskfile *tf, u16 *id)
1758{
1759 return ata_exec_internal(dev, tf, NULL, DMA_FROM_DEVICE,
1760 id, sizeof(id[0]) * ATA_ID_WORDS, 0);
1761}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01001762EXPORT_SYMBOL_GPL(ata_do_dev_read_id);
Alan Cox963e4972008-07-24 17:16:06 +01001763
1764/**
Tejun Heo49016ac2006-02-21 02:12:11 +09001765 * ata_dev_read_id - Read ID data from the specified device
Tejun Heo49016ac2006-02-21 02:12:11 +09001766 * @dev: target device
1767 * @p_class: pointer to class of the target device (may be changed)
Tejun Heobff04642006-11-10 18:08:10 +09001768 * @flags: ATA_READID_* flags
Tejun Heofe635c72006-05-15 20:57:35 +09001769 * @id: buffer to read IDENTIFY data into
Tejun Heo49016ac2006-02-21 02:12:11 +09001770 *
1771 * Read ID data from the specified device. ATA_CMD_ID_ATA is
1772 * performed on ATA devices and ATA_CMD_ID_ATAPI on ATAPI
Tejun Heoaec5c3c2006-03-25 01:33:34 +09001773 * devices. This function also issues ATA_CMD_INIT_DEV_PARAMS
1774 * for pre-ATA4 drives.
Tejun Heo49016ac2006-02-21 02:12:11 +09001775 *
Alan Cox50a99012007-08-08 14:27:00 +01001776 * FIXME: ATA_CMD_ID_ATA is optional for early drives and right
Jeff Garzik2dcb4072007-10-19 06:42:56 -04001777 * now we abort if we hit that case.
Alan Cox50a99012007-08-08 14:27:00 +01001778 *
Tejun Heo49016ac2006-02-21 02:12:11 +09001779 * LOCKING:
1780 * Kernel thread context (may sleep)
1781 *
1782 * RETURNS:
1783 * 0 on success, -errno otherwise.
1784 */
Tejun Heoa9beec92006-05-31 18:27:44 +09001785int ata_dev_read_id(struct ata_device *dev, unsigned int *p_class,
Tejun Heobff04642006-11-10 18:08:10 +09001786 unsigned int flags, u16 *id)
Tejun Heo49016ac2006-02-21 02:12:11 +09001787{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001788 struct ata_port *ap = dev->link->ap;
Tejun Heo49016ac2006-02-21 02:12:11 +09001789 unsigned int class = *p_class;
Tejun Heo49016ac2006-02-21 02:12:11 +09001790 struct ata_taskfile tf;
1791 unsigned int err_mask = 0;
1792 const char *reason;
Tejun Heo79b42ba2009-04-15 06:21:10 +09001793 bool is_semb = class == ATA_DEV_SEMB;
Tejun Heo54936f82007-05-11 14:35:29 +02001794 int may_fallback = 1, tried_spinup = 0;
Tejun Heo49016ac2006-02-21 02:12:11 +09001795 int rc;
1796
Borislav Petkov0dd4b212006-06-23 02:29:08 -04001797 if (ata_msg_ctl(ap))
Joe Perchesa9a79df2011-04-15 15:51:59 -07001798 ata_dev_dbg(dev, "%s: ENTER\n", __func__);
Tejun Heo49016ac2006-02-21 02:12:11 +09001799
Alan Cox963e4972008-07-24 17:16:06 +01001800retry:
Tejun Heo3373efd2006-05-15 20:57:53 +09001801 ata_tf_init(dev, &tf);
Tejun Heo49016ac2006-02-21 02:12:11 +09001802
1803 switch (class) {
Tejun Heo79b42ba2009-04-15 06:21:10 +09001804 case ATA_DEV_SEMB:
1805 class = ATA_DEV_ATA; /* some hard drives report SEMB sig */
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05001806 fallthrough;
Tejun Heo49016ac2006-02-21 02:12:11 +09001807 case ATA_DEV_ATA:
Hannes Reinecke9162c652014-11-05 13:08:21 +01001808 case ATA_DEV_ZAC:
Tejun Heo49016ac2006-02-21 02:12:11 +09001809 tf.command = ATA_CMD_ID_ATA;
1810 break;
1811 case ATA_DEV_ATAPI:
1812 tf.command = ATA_CMD_ID_ATAPI;
1813 break;
1814 default:
1815 rc = -ENODEV;
1816 reason = "unsupported class";
1817 goto err_out;
1818 }
1819
1820 tf.protocol = ATA_PROT_PIO;
Tejun Heo81afe892007-02-07 12:37:41 -08001821
1822 /* Some devices choke if TF registers contain garbage. Make
1823 * sure those are properly initialized.
1824 */
1825 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
1826
1827 /* Device presence detection is unreliable on some
1828 * controllers. Always poll IDENTIFY if available.
1829 */
1830 tf.flags |= ATA_TFLAG_POLLING;
Tejun Heo49016ac2006-02-21 02:12:11 +09001831
Alan Cox963e4972008-07-24 17:16:06 +01001832 if (ap->ops->read_id)
1833 err_mask = ap->ops->read_id(dev, &tf, id);
1834 else
1835 err_mask = ata_do_dev_read_id(dev, &tf, id);
1836
Tejun Heo49016ac2006-02-21 02:12:11 +09001837 if (err_mask) {
Tejun Heo800b3992006-12-03 21:34:13 +09001838 if (err_mask & AC_ERR_NODEV_HINT) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07001839 ata_dev_dbg(dev, "NODEV after polling detection\n");
Tejun Heo55a8e2c2006-11-10 18:08:10 +09001840 return -ENOENT;
1841 }
1842
Tejun Heo79b42ba2009-04-15 06:21:10 +09001843 if (is_semb) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07001844 ata_dev_info(dev,
1845 "IDENTIFY failed on device w/ SEMB sig, disabled\n");
Tejun Heo79b42ba2009-04-15 06:21:10 +09001846 /* SEMB is not supported yet */
1847 *p_class = ATA_DEV_SEMB_UNSUP;
1848 return 0;
1849 }
1850
Tejun Heo1ffc1512008-03-23 15:16:53 +09001851 if ((err_mask == AC_ERR_DEV) && (tf.feature & ATA_ABORTED)) {
1852 /* Device or controller might have reported
1853 * the wrong device class. Give a shot at the
1854 * other IDENTIFY if the current one is
1855 * aborted by the device.
1856 */
1857 if (may_fallback) {
1858 may_fallback = 0;
Tejun Heo54936f82007-05-11 14:35:29 +02001859
Tejun Heo1ffc1512008-03-23 15:16:53 +09001860 if (class == ATA_DEV_ATA)
1861 class = ATA_DEV_ATAPI;
1862 else
1863 class = ATA_DEV_ATA;
1864 goto retry;
1865 }
1866
1867 /* Control reaches here iff the device aborted
1868 * both flavors of IDENTIFYs which happens
1869 * sometimes with phantom devices.
1870 */
Joe Perchesa9a79df2011-04-15 15:51:59 -07001871 ata_dev_dbg(dev,
1872 "both IDENTIFYs aborted, assuming NODEV\n");
Tejun Heo1ffc1512008-03-23 15:16:53 +09001873 return -ENOENT;
Tejun Heo54936f82007-05-11 14:35:29 +02001874 }
1875
Tejun Heo49016ac2006-02-21 02:12:11 +09001876 rc = -EIO;
1877 reason = "I/O error";
Tejun Heo49016ac2006-02-21 02:12:11 +09001878 goto err_out;
1879 }
1880
Tejun Heo43c9c592010-05-23 12:59:11 +02001881 if (dev->horkage & ATA_HORKAGE_DUMP_ID) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07001882 ata_dev_dbg(dev, "dumping IDENTIFY data, "
1883 "class=%d may_fallback=%d tried_spinup=%d\n",
1884 class, may_fallback, tried_spinup);
Tejun Heo43c9c592010-05-23 12:59:11 +02001885 print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET,
1886 16, 2, id, ATA_ID_WORDS * sizeof(*id), true);
1887 }
1888
Tejun Heo54936f82007-05-11 14:35:29 +02001889 /* Falling back doesn't make sense if ID data was read
1890 * successfully at least once.
1891 */
1892 may_fallback = 0;
1893
Tejun Heo49016ac2006-02-21 02:12:11 +09001894 swap_buf_le16(id, ATA_ID_WORDS);
1895
Tejun Heo49016ac2006-02-21 02:12:11 +09001896 /* sanity check */
Tejun Heoa4f57492006-09-12 20:35:49 -07001897 rc = -EINVAL;
Alan Cox60700682007-06-07 16:13:55 +01001898 reason = "device reports invalid type";
Tejun Heoa4f57492006-09-12 20:35:49 -07001899
Hannes Reinecke9162c652014-11-05 13:08:21 +01001900 if (class == ATA_DEV_ATA || class == ATA_DEV_ZAC) {
Tejun Heoa4f57492006-09-12 20:35:49 -07001901 if (!ata_id_is_ata(id) && !ata_id_is_cfa(id))
1902 goto err_out;
Andy Whitcroftdb63a4c2012-05-04 22:15:10 +01001903 if (ap->host->flags & ATA_HOST_IGNORE_ATA &&
1904 ata_id_is_ata(id)) {
1905 ata_dev_dbg(dev,
1906 "host indicates ignore ATA devices, ignored\n");
1907 return -ENOENT;
1908 }
Tejun Heoa4f57492006-09-12 20:35:49 -07001909 } else {
1910 if (ata_id_is_ata(id))
1911 goto err_out;
Tejun Heo49016ac2006-02-21 02:12:11 +09001912 }
1913
Mark Lord169439c2007-04-17 18:26:07 -04001914 if (!tried_spinup && (id[2] == 0x37c8 || id[2] == 0x738c)) {
1915 tried_spinup = 1;
1916 /*
1917 * Drive powered-up in standby mode, and requires a specific
1918 * SET_FEATURES spin-up subcommand before it will accept
1919 * anything other than the original IDENTIFY command.
1920 */
Jeff Garzik218f3d32007-10-25 00:33:27 -04001921 err_mask = ata_dev_set_feature(dev, SETFEATURES_SPINUP, 0);
Ryan Powerfb0582f92007-08-10 13:59:35 -07001922 if (err_mask && id[2] != 0x738c) {
Mark Lord169439c2007-04-17 18:26:07 -04001923 rc = -EIO;
1924 reason = "SPINUP failed";
1925 goto err_out;
1926 }
1927 /*
1928 * If the drive initially returned incomplete IDENTIFY info,
1929 * we now must reissue the IDENTIFY command.
1930 */
1931 if (id[2] == 0x37c8)
1932 goto retry;
1933 }
1934
Hannes Reinecke9162c652014-11-05 13:08:21 +01001935 if ((flags & ATA_READID_POSTRESET) &&
1936 (class == ATA_DEV_ATA || class == ATA_DEV_ZAC)) {
Tejun Heo49016ac2006-02-21 02:12:11 +09001937 /*
1938 * The exact sequence expected by certain pre-ATA4 drives is:
1939 * SRST RESET
Alan Cox50a99012007-08-08 14:27:00 +01001940 * IDENTIFY (optional in early ATA)
1941 * INITIALIZE DEVICE PARAMETERS (later IDE and ATA)
Tejun Heo49016ac2006-02-21 02:12:11 +09001942 * anything else..
1943 * Some drives were very specific about that exact sequence.
Alan Cox50a99012007-08-08 14:27:00 +01001944 *
1945 * Note that ATA4 says lba is mandatory so the second check
Adam Buchbinderc9404c92009-12-18 15:40:42 -05001946 * should never trigger.
Tejun Heo49016ac2006-02-21 02:12:11 +09001947 */
1948 if (ata_id_major_version(id) < 4 || !ata_id_has_lba(id)) {
Tejun Heo3373efd2006-05-15 20:57:53 +09001949 err_mask = ata_dev_init_params(dev, id[3], id[6]);
Tejun Heo49016ac2006-02-21 02:12:11 +09001950 if (err_mask) {
1951 rc = -EIO;
1952 reason = "INIT_DEV_PARAMS failed";
1953 goto err_out;
1954 }
1955
1956 /* current CHS translation info (id[53-58]) might be
1957 * changed. reread the identify device info.
1958 */
Tejun Heobff04642006-11-10 18:08:10 +09001959 flags &= ~ATA_READID_POSTRESET;
Tejun Heo49016ac2006-02-21 02:12:11 +09001960 goto retry;
1961 }
1962 }
1963
1964 *p_class = class;
Tejun Heofe635c72006-05-15 20:57:35 +09001965
Tejun Heo49016ac2006-02-21 02:12:11 +09001966 return 0;
1967
1968 err_out:
Tejun Heo88574552006-06-25 20:00:35 +09001969 if (ata_msg_warn(ap))
Joe Perchesa9a79df2011-04-15 15:51:59 -07001970 ata_dev_warn(dev, "failed to IDENTIFY (%s, err_mask=0x%x)\n",
1971 reason, err_mask);
Tejun Heo49016ac2006-02-21 02:12:11 +09001972 return rc;
1973}
1974
Christoph Hellwigf01f62c2017-06-04 14:42:20 +02001975/**
1976 * ata_read_log_page - read a specific log page
1977 * @dev: target device
1978 * @log: log to read
1979 * @page: page to read
1980 * @buf: buffer to store read page
1981 * @sectors: number of sectors to read
1982 *
1983 * Read log page using READ_LOG_EXT command.
1984 *
1985 * LOCKING:
1986 * Kernel thread context (may sleep).
1987 *
1988 * RETURNS:
1989 * 0 on success, AC_ERR_* mask otherwise.
1990 */
1991unsigned int ata_read_log_page(struct ata_device *dev, u8 log,
1992 u8 page, void *buf, unsigned int sectors)
1993{
1994 unsigned long ap_flags = dev->link->ap->flags;
1995 struct ata_taskfile tf;
1996 unsigned int err_mask;
1997 bool dma = false;
1998
1999 DPRINTK("read log page - log 0x%x, page 0x%x\n", log, page);
2000
2001 /*
2002 * Return error without actually issuing the command on controllers
2003 * which e.g. lockup on a read log page.
2004 */
2005 if (ap_flags & ATA_FLAG_NO_LOG_PAGE)
2006 return AC_ERR_DEV;
2007
2008retry:
2009 ata_tf_init(dev, &tf);
2010 if (dev->dma_mode && ata_id_has_read_log_dma_ext(dev->id) &&
Damien Le Moal7cfdfdc2017-07-10 14:45:20 +09002011 !(dev->horkage & ATA_HORKAGE_NO_DMA_LOG)) {
Christoph Hellwigf01f62c2017-06-04 14:42:20 +02002012 tf.command = ATA_CMD_READ_LOG_DMA_EXT;
2013 tf.protocol = ATA_PROT_DMA;
2014 dma = true;
2015 } else {
2016 tf.command = ATA_CMD_READ_LOG_EXT;
2017 tf.protocol = ATA_PROT_PIO;
2018 dma = false;
2019 }
2020 tf.lbal = log;
2021 tf.lbam = page;
2022 tf.nsect = sectors;
2023 tf.hob_nsect = sectors >> 8;
2024 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_LBA48 | ATA_TFLAG_DEVICE;
2025
2026 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_FROM_DEVICE,
2027 buf, sectors * ATA_SECT_SIZE, 0);
2028
Damien Le Moalfc5c8aa2021-08-16 10:44:52 +09002029 if (err_mask) {
2030 if (dma) {
2031 dev->horkage |= ATA_HORKAGE_NO_DMA_LOG;
2032 goto retry;
2033 }
2034 ata_dev_err(dev, "Read log page 0x%02x failed, Emask 0x%x\n",
2035 (unsigned int)page, err_mask);
Christoph Hellwigf01f62c2017-06-04 14:42:20 +02002036 }
2037
Christoph Hellwigf01f62c2017-06-04 14:42:20 +02002038 return err_mask;
2039}
2040
Christoph Hellwigefe205a2017-06-04 14:42:21 +02002041static bool ata_log_supported(struct ata_device *dev, u8 log)
2042{
2043 struct ata_port *ap = dev->link->ap;
2044
2045 if (ata_read_log_page(dev, ATA_LOG_DIRECTORY, 0, ap->sector_buf, 1))
2046 return false;
2047 return get_unaligned_le16(&ap->sector_buf[log * 2]) ? true : false;
2048}
2049
Christoph Hellwiga0fd2452017-06-04 14:42:23 +02002050static bool ata_identify_page_supported(struct ata_device *dev, u8 page)
2051{
2052 struct ata_port *ap = dev->link->ap;
2053 unsigned int err, i;
2054
2055 if (!ata_log_supported(dev, ATA_LOG_IDENTIFY_DEVICE)) {
2056 ata_dev_warn(dev, "ATA Identify Device Log not supported\n");
2057 return false;
2058 }
2059
2060 /*
2061 * Read IDENTIFY DEVICE data log, page 0, to figure out if the page is
2062 * supported.
2063 */
2064 err = ata_read_log_page(dev, ATA_LOG_IDENTIFY_DEVICE, 0, ap->sector_buf,
2065 1);
Damien Le Moalfc5c8aa2021-08-16 10:44:52 +09002066 if (err)
Christoph Hellwiga0fd2452017-06-04 14:42:23 +02002067 return false;
Christoph Hellwiga0fd2452017-06-04 14:42:23 +02002068
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 Heo90627122009-01-29 20:31:36 +09002077static 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 Perchesa9a79df2011-04-15 15:51:59 -07002103 ata_dev_info(dev, "applying link speed limit horkage to %s\n",
2104 sata_spd_string(target));
Tejun Heo90627122009-01-29 20:31:36 +09002105 return -EAGAIN;
2106 }
2107 return 0;
2108}
2109
Tejun Heo3373efd2006-05-15 20:57:53 +09002110static inline u8 ata_dev_knobble(struct ata_device *dev)
Tejun Heo4b2f3ed2006-03-01 16:09:36 +09002111{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09002112 struct ata_port *ap = dev->link->ap;
Jens Axboe9ce8e302008-08-27 15:23:18 +02002113
2114 if (ata_dev_blacklisted(dev) & ATA_HORKAGE_BRIDGE_OK)
2115 return 0;
2116
Tejun Heo9af5c9c2007-08-06 18:36:22 +09002117 return ((ap->cbl == ATA_CBL_SATA) && (!ata_id_is_sata(dev->id)));
Tejun Heo4b2f3ed2006-03-01 16:09:36 +09002118}
2119
Hannes Reinecke5a233552016-04-25 12:45:46 +02002120static 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 Hellwigefe205a2017-06-04 14:42:21 +02002125 if (!ata_log_supported(dev, ATA_LOG_NCQ_SEND_RECV)) {
2126 ata_dev_warn(dev, "NCQ Send/Recv Log not supported\n");
Hannes Reineckefe5af0c2016-04-25 12:45:48 +02002127 return;
2128 }
Hannes Reinecke5a233552016-04-25 12:45:46 +02002129 err_mask = ata_read_log_page(dev, ATA_LOG_NCQ_SEND_RECV,
2130 0, ap->sector_buf, 1);
Damien Le Moalfc5c8aa2021-08-16 10:44:52 +09002131 if (!err_mask) {
Hannes Reinecke5a233552016-04-25 12:45:46 +02002132 u8 *cmds = dev->ncq_send_recv_cmds;
2133
2134 dev->flags |= ATA_DFLAG_NCQ_SEND_RECV;
2135 memcpy(cmds, ap->sector_buf, ATA_LOG_NCQ_SEND_RECV_SIZE);
2136
2137 if (dev->horkage & ATA_HORKAGE_NO_NCQ_TRIM) {
2138 ata_dev_dbg(dev, "disabling queued TRIM support\n");
2139 cmds[ATA_LOG_NCQ_SEND_RECV_DSM_OFFSET] &=
2140 ~ATA_LOG_NCQ_SEND_RECV_DSM_TRIM;
2141 }
2142 }
2143}
2144
Hannes Reinecke284b3b72016-04-25 12:45:54 +02002145static void ata_dev_config_ncq_non_data(struct ata_device *dev)
2146{
2147 struct ata_port *ap = dev->link->ap;
2148 unsigned int err_mask;
Hannes Reinecke284b3b72016-04-25 12:45:54 +02002149
Christoph Hellwigefe205a2017-06-04 14:42:21 +02002150 if (!ata_log_supported(dev, ATA_LOG_NCQ_NON_DATA)) {
Hannes Reinecke284b3b72016-04-25 12:45:54 +02002151 ata_dev_warn(dev,
2152 "NCQ Send/Recv Log not supported\n");
2153 return;
2154 }
2155 err_mask = ata_read_log_page(dev, ATA_LOG_NCQ_NON_DATA,
2156 0, ap->sector_buf, 1);
Damien Le Moalfc5c8aa2021-08-16 10:44:52 +09002157 if (!err_mask) {
Hannes Reinecke284b3b72016-04-25 12:45:54 +02002158 u8 *cmds = dev->ncq_non_data_cmds;
2159
2160 memcpy(cmds, ap->sector_buf, ATA_LOG_NCQ_NON_DATA_SIZE);
2161 }
2162}
2163
Adam Manzanares8e061782016-10-17 11:27:29 -07002164static void ata_dev_config_ncq_prio(struct ata_device *dev)
2165{
2166 struct ata_port *ap = dev->link->ap;
2167 unsigned int err_mask;
2168
2169 err_mask = ata_read_log_page(dev,
Christoph Hellwig1d51d5f2017-06-04 14:42:22 +02002170 ATA_LOG_IDENTIFY_DEVICE,
Adam Manzanares8e061782016-10-17 11:27:29 -07002171 ATA_LOG_SATA_SETTINGS,
2172 ap->sector_buf,
2173 1);
Damien Le Moalfc5c8aa2021-08-16 10:44:52 +09002174 if (err_mask)
Damien Le Moal2360fa12021-08-16 10:44:51 +09002175 goto not_supported;
Adam Manzanares8e061782016-10-17 11:27:29 -07002176
Damien Le Moal2360fa12021-08-16 10:44:51 +09002177 if (!(ap->sector_buf[ATA_LOG_NCQ_PRIO_OFFSET] & BIT(3)))
2178 goto not_supported;
Adam Manzanares8e061782016-10-17 11:27:29 -07002179
Damien Le Moal2360fa12021-08-16 10:44:51 +09002180 dev->flags |= ATA_DFLAG_NCQ_PRIO;
2181
2182 return;
2183
2184not_supported:
2185 dev->flags &= ~ATA_DFLAG_NCQ_PRIO_ENABLE;
2186 dev->flags &= ~ATA_DFLAG_NCQ_PRIO;
Adam Manzanares8e061782016-10-17 11:27:29 -07002187}
2188
Shaohua Li388539f2009-07-27 09:24:35 +08002189static int ata_dev_config_ncq(struct ata_device *dev,
Tejun Heoa6e6ce82006-05-15 21:03:48 +09002190 char *desc, size_t desc_sz)
2191{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09002192 struct ata_port *ap = dev->link->ap;
Tejun Heoa6e6ce82006-05-15 21:03:48 +09002193 int hdepth = 0, ddepth = ata_id_queue_depth(dev->id);
Shaohua Li388539f2009-07-27 09:24:35 +08002194 unsigned int err_mask;
2195 char *aa_desc = "";
Tejun Heoa6e6ce82006-05-15 21:03:48 +09002196
2197 if (!ata_id_has_ncq(dev->id)) {
2198 desc[0] = '\0';
Shaohua Li388539f2009-07-27 09:24:35 +08002199 return 0;
Tejun Heoa6e6ce82006-05-15 21:03:48 +09002200 }
Bartlomiej Zolnierkiewiczcba97ea2020-03-26 16:58:09 +01002201 if (!IS_ENABLED(CONFIG_SATA_HOST))
2202 return 0;
Tejun Heo75683fe2007-07-05 13:31:27 +09002203 if (dev->horkage & ATA_HORKAGE_NONCQ) {
Alan Cox6919a0a2006-10-27 19:08:46 -07002204 snprintf(desc, desc_sz, "NCQ (not used)");
Shaohua Li388539f2009-07-27 09:24:35 +08002205 return 0;
Alan Cox6919a0a2006-10-27 19:08:46 -07002206 }
Tejun Heoa6e6ce82006-05-15 21:03:48 +09002207 if (ap->flags & ATA_FLAG_NCQ) {
Jens Axboe69278f72018-05-11 12:51:10 -06002208 hdepth = min(ap->scsi_host->can_queue, ATA_MAX_QUEUE);
Tejun Heoa6e6ce82006-05-15 21:03:48 +09002209 dev->flags |= ATA_DFLAG_NCQ;
2210 }
2211
Shaohua Li388539f2009-07-27 09:24:35 +08002212 if (!(dev->horkage & ATA_HORKAGE_BROKEN_FPDMA_AA) &&
2213 (ap->flags & ATA_FLAG_FPDMA_AA) &&
2214 ata_id_has_fpdma_aa(dev->id)) {
2215 err_mask = ata_dev_set_feature(dev, SETFEATURES_SATA_ENABLE,
2216 SATA_FPDMA_AA);
2217 if (err_mask) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07002218 ata_dev_err(dev,
2219 "failed to enable AA (error_mask=0x%x)\n",
2220 err_mask);
Shaohua Li388539f2009-07-27 09:24:35 +08002221 if (err_mask != AC_ERR_DEV) {
2222 dev->horkage |= ATA_HORKAGE_BROKEN_FPDMA_AA;
2223 return -EIO;
2224 }
2225 } else
2226 aa_desc = ", AA";
2227 }
2228
Tejun Heoa6e6ce82006-05-15 21:03:48 +09002229 if (hdepth >= ddepth)
Shaohua Li388539f2009-07-27 09:24:35 +08002230 snprintf(desc, desc_sz, "NCQ (depth %d)%s", ddepth, aa_desc);
Tejun Heoa6e6ce82006-05-15 21:03:48 +09002231 else
Shaohua Li388539f2009-07-27 09:24:35 +08002232 snprintf(desc, desc_sz, "NCQ (depth %d/%d)%s", hdepth,
2233 ddepth, aa_desc);
Marc Carinoed369112013-08-24 23:22:50 -07002234
Hannes Reinecke284b3b72016-04-25 12:45:54 +02002235 if ((ap->flags & ATA_FLAG_FPDMA_AUX)) {
2236 if (ata_id_has_ncq_send_and_recv(dev->id))
2237 ata_dev_config_ncq_send_recv(dev);
2238 if (ata_id_has_ncq_non_data(dev->id))
2239 ata_dev_config_ncq_non_data(dev);
Adam Manzanares8e061782016-10-17 11:27:29 -07002240 if (ata_id_has_ncq_prio(dev->id))
2241 ata_dev_config_ncq_prio(dev);
Marc Carinoed369112013-08-24 23:22:50 -07002242 }
2243
Shaohua Li388539f2009-07-27 09:24:35 +08002244 return 0;
Tejun Heoa6e6ce82006-05-15 21:03:48 +09002245}
2246
Hannes Reineckee87fd282016-04-04 11:43:55 +02002247static void ata_dev_config_sense_reporting(struct ata_device *dev)
2248{
2249 unsigned int err_mask;
2250
2251 if (!ata_id_has_sense_reporting(dev->id))
2252 return;
2253
2254 if (ata_id_sense_reporting_enabled(dev->id))
2255 return;
2256
2257 err_mask = ata_dev_set_feature(dev, SETFEATURE_SENSE_DATA, 0x1);
2258 if (err_mask) {
2259 ata_dev_dbg(dev,
2260 "failed to enable Sense Data Reporting, Emask 0x%x\n",
2261 err_mask);
2262 }
2263}
2264
Hannes Reinecke6d1003a2016-04-25 12:45:56 +02002265static void ata_dev_config_zac(struct ata_device *dev)
2266{
2267 struct ata_port *ap = dev->link->ap;
2268 unsigned int err_mask;
2269 u8 *identify_buf = ap->sector_buf;
Hannes Reinecke6d1003a2016-04-25 12:45:56 +02002270
2271 dev->zac_zones_optimal_open = U32_MAX;
2272 dev->zac_zones_optimal_nonseq = U32_MAX;
2273 dev->zac_zones_max_open = U32_MAX;
2274
2275 /*
2276 * Always set the 'ZAC' flag for Host-managed devices.
2277 */
2278 if (dev->class == ATA_DEV_ZAC)
2279 dev->flags |= ATA_DFLAG_ZAC;
2280 else if (ata_id_zoned_cap(dev->id) == 0x01)
2281 /*
2282 * Check for host-aware devices.
2283 */
2284 dev->flags |= ATA_DFLAG_ZAC;
2285
2286 if (!(dev->flags & ATA_DFLAG_ZAC))
2287 return;
2288
Christoph Hellwiga0fd2452017-06-04 14:42:23 +02002289 if (!ata_identify_page_supported(dev, ATA_LOG_ZONED_INFORMATION)) {
Hannes Reinecke6d1003a2016-04-25 12:45:56 +02002290 ata_dev_warn(dev,
2291 "ATA Zoned Information Log not supported\n");
2292 return;
2293 }
2294
2295 /*
2296 * Read IDENTIFY DEVICE data log, page 9 (Zoned-device information)
2297 */
Christoph Hellwig1d51d5f2017-06-04 14:42:22 +02002298 err_mask = ata_read_log_page(dev, ATA_LOG_IDENTIFY_DEVICE,
Hannes Reinecke6d1003a2016-04-25 12:45:56 +02002299 ATA_LOG_ZONED_INFORMATION,
2300 identify_buf, 1);
2301 if (!err_mask) {
2302 u64 zoned_cap, opt_open, opt_nonseq, max_open;
2303
2304 zoned_cap = get_unaligned_le64(&identify_buf[8]);
2305 if ((zoned_cap >> 63))
2306 dev->zac_zoned_cap = (zoned_cap & 1);
2307 opt_open = get_unaligned_le64(&identify_buf[24]);
2308 if ((opt_open >> 63))
2309 dev->zac_zones_optimal_open = (u32)opt_open;
2310 opt_nonseq = get_unaligned_le64(&identify_buf[32]);
2311 if ((opt_nonseq >> 63))
2312 dev->zac_zones_optimal_nonseq = (u32)opt_nonseq;
2313 max_open = get_unaligned_le64(&identify_buf[40]);
2314 if ((max_open >> 63))
2315 dev->zac_zones_max_open = (u32)max_open;
2316 }
2317}
2318
Christoph Hellwig818831c2017-06-04 14:42:24 +02002319static void ata_dev_config_trusted(struct ata_device *dev)
2320{
2321 struct ata_port *ap = dev->link->ap;
2322 u64 trusted_cap;
2323 unsigned int err;
2324
Christoph Hellwige8f11db2017-08-29 14:42:06 +02002325 if (!ata_id_has_trusted(dev->id))
2326 return;
2327
Christoph Hellwig818831c2017-06-04 14:42:24 +02002328 if (!ata_identify_page_supported(dev, ATA_LOG_SECURITY)) {
2329 ata_dev_warn(dev,
2330 "Security Log not supported\n");
2331 return;
2332 }
2333
2334 err = ata_read_log_page(dev, ATA_LOG_IDENTIFY_DEVICE, ATA_LOG_SECURITY,
2335 ap->sector_buf, 1);
Damien Le Moalfc5c8aa2021-08-16 10:44:52 +09002336 if (err)
Christoph Hellwig818831c2017-06-04 14:42:24 +02002337 return;
Christoph Hellwig818831c2017-06-04 14:42:24 +02002338
2339 trusted_cap = get_unaligned_le64(&ap->sector_buf[40]);
2340 if (!(trusted_cap & (1ULL << 63))) {
2341 ata_dev_dbg(dev,
2342 "Trusted Computing capability qword not valid!\n");
2343 return;
2344 }
2345
2346 if (trusted_cap & (1 << 0))
2347 dev->flags |= ATA_DFLAG_TRUSTED;
2348}
2349
Damien Le Moal891fd7c2021-08-16 10:44:50 +09002350static int ata_dev_config_lba(struct ata_device *dev)
2351{
2352 struct ata_port *ap = dev->link->ap;
2353 const u16 *id = dev->id;
2354 const char *lba_desc;
2355 char ncq_desc[24];
2356 int ret;
2357
2358 dev->flags |= ATA_DFLAG_LBA;
2359
2360 if (ata_id_has_lba48(id)) {
2361 lba_desc = "LBA48";
2362 dev->flags |= ATA_DFLAG_LBA48;
2363 if (dev->n_sectors >= (1UL << 28) &&
2364 ata_id_has_flush_ext(id))
2365 dev->flags |= ATA_DFLAG_FLUSH_EXT;
2366 } else {
2367 lba_desc = "LBA";
2368 }
2369
2370 /* config NCQ */
2371 ret = ata_dev_config_ncq(dev, ncq_desc, sizeof(ncq_desc));
2372
2373 /* print device info to dmesg */
2374 if (ata_msg_drv(ap) && ata_dev_print_info(dev))
2375 ata_dev_info(dev,
2376 "%llu sectors, multi %u: %s %s\n",
2377 (unsigned long long)dev->n_sectors,
2378 dev->multi_count, lba_desc, ncq_desc);
2379
2380 return ret;
2381}
2382
2383static void ata_dev_config_chs(struct ata_device *dev)
2384{
2385 struct ata_port *ap = dev->link->ap;
2386 const u16 *id = dev->id;
2387
2388 if (ata_id_current_chs_valid(id)) {
2389 /* Current CHS translation is valid. */
2390 dev->cylinders = id[54];
2391 dev->heads = id[55];
2392 dev->sectors = id[56];
2393 } else {
2394 /* Default translation */
2395 dev->cylinders = id[1];
2396 dev->heads = id[3];
2397 dev->sectors = id[6];
2398 }
2399
2400 /* print device info to dmesg */
2401 if (ata_msg_drv(ap) && ata_dev_print_info(dev))
2402 ata_dev_info(dev,
2403 "%llu sectors, multi %u, CHS %u/%u/%u\n",
2404 (unsigned long long)dev->n_sectors,
2405 dev->multi_count, dev->cylinders,
2406 dev->heads, dev->sectors);
2407}
2408
Damien Le Moald8d87782021-08-16 10:44:49 +09002409static void ata_dev_config_devslp(struct ata_device *dev)
2410{
2411 u8 *sata_setting = dev->link->ap->sector_buf;
2412 unsigned int err_mask;
2413 int i, j;
2414
2415 /*
2416 * Check device sleep capability. Get DevSlp timing variables
2417 * from SATA Settings page of Identify Device Data Log.
2418 */
2419 if (!ata_id_has_devslp(dev->id))
2420 return;
2421
2422 err_mask = ata_read_log_page(dev,
2423 ATA_LOG_IDENTIFY_DEVICE,
2424 ATA_LOG_SATA_SETTINGS,
2425 sata_setting, 1);
Damien Le Moalfc5c8aa2021-08-16 10:44:52 +09002426 if (err_mask)
Damien Le Moald8d87782021-08-16 10:44:49 +09002427 return;
Damien Le Moald8d87782021-08-16 10:44:49 +09002428
2429 dev->flags |= ATA_DFLAG_DEVSLP;
2430 for (i = 0; i < ATA_LOG_DEVSLP_SIZE; i++) {
2431 j = ATA_LOG_DEVSLP_OFFSET + i;
2432 dev->devslp_timing[i] = sata_setting[j];
2433 }
2434}
2435
Tejun Heo49016ac2006-02-21 02:12:11 +09002436/**
Tejun Heoffeae412006-03-01 16:09:35 +09002437 * ata_dev_configure - Configure the specified ATA/ATAPI device
Tejun Heoffeae412006-03-01 16:09:35 +09002438 * @dev: Target device to configure
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 *
Tejun Heoffeae412006-03-01 16:09:35 +09002440 * Configure @dev according to @dev->id. Generic and low-level
2441 * driver specific fixups are also applied.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 *
2443 * LOCKING:
Tejun Heoffeae412006-03-01 16:09:35 +09002444 * Kernel thread context (may sleep)
2445 *
2446 * RETURNS:
2447 * 0 on success, -errno otherwise
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 */
Tejun Heoefdaedc2006-11-01 18:38:52 +09002449int ata_dev_configure(struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09002451 struct ata_port *ap = dev->link->ap;
Damien Le Moal891fd7c2021-08-16 10:44:50 +09002452 bool print_info = ata_dev_print_info(dev);
Tejun Heo1148c3a2006-03-13 19:48:04 +09002453 const u16 *id = dev->id;
Tejun Heo7dc951a2007-11-27 19:43:42 +09002454 unsigned long xfer_mask;
Shane Huang65fe1f02012-09-07 22:40:01 +08002455 unsigned int err_mask;
Alan Coxb352e572006-08-10 18:52:12 +01002456 char revbuf[7]; /* XYZ-99\0 */
Eric D. Mudama3f64f562007-01-30 23:00:40 -07002457 char fwrevbuf[ATA_ID_FW_REV_LEN+1];
2458 char modelbuf[ATA_ID_PROD_LEN+1];
Brian Kinge6d902a2006-06-28 08:30:31 -05002459 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460
Borislav Petkov0dd4b212006-06-23 02:29:08 -04002461 if (!ata_dev_enabled(dev) && ata_msg_info(ap)) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07002462 ata_dev_info(dev, "%s: ENTER/EXIT -- nodev\n", __func__);
Tejun Heoffeae412006-03-01 16:09:35 +09002463 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464 }
2465
Borislav Petkov0dd4b212006-06-23 02:29:08 -04002466 if (ata_msg_probe(ap))
Joe Perchesa9a79df2011-04-15 15:51:59 -07002467 ata_dev_dbg(dev, "%s: ENTER\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468
Tejun Heo75683fe2007-07-05 13:31:27 +09002469 /* set horkage */
2470 dev->horkage |= ata_dev_blacklisted(dev);
Tejun Heo33267322008-02-13 09:15:09 +09002471 ata_force_horkage(dev);
Tejun Heo75683fe2007-07-05 13:31:27 +09002472
Tejun Heo50af2fa2008-05-19 01:15:14 +09002473 if (dev->horkage & ATA_HORKAGE_DISABLE) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07002474 ata_dev_info(dev, "unsupported device, disabling\n");
Tejun Heo50af2fa2008-05-19 01:15:14 +09002475 ata_dev_disable(dev);
2476 return 0;
2477 }
2478
Tejun Heo2486fa52008-07-31 07:52:40 +09002479 if ((!atapi_enabled || (ap->flags & ATA_FLAG_NO_ATAPI)) &&
2480 dev->class == ATA_DEV_ATAPI) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07002481 ata_dev_warn(dev, "WARNING: ATAPI is %s, device ignored\n",
2482 atapi_enabled ? "not supported with this driver"
2483 : "disabled");
Tejun Heo2486fa52008-07-31 07:52:40 +09002484 ata_dev_disable(dev);
2485 return 0;
2486 }
2487
Tejun Heo90627122009-01-29 20:31:36 +09002488 rc = ata_do_link_spd_horkage(dev);
2489 if (rc)
2490 return rc;
2491
Tejun Heoecd75ad2014-01-16 09:47:17 -05002492 /* some WD SATA-1 drives have issues with LPM, turn on NOLPM for them */
2493 if ((dev->horkage & ATA_HORKAGE_WD_BROKEN_LPM) &&
2494 (id[ATA_ID_SATA_CAPABILITY] & 0xe) == 0x2)
2495 dev->horkage |= ATA_HORKAGE_NOLPM;
2496
Hans de Goede240630e2018-07-01 12:15:46 +02002497 if (ap->flags & ATA_FLAG_NO_LPM)
2498 dev->horkage |= ATA_HORKAGE_NOLPM;
2499
Tejun Heoecd75ad2014-01-16 09:47:17 -05002500 if (dev->horkage & ATA_HORKAGE_NOLPM) {
2501 ata_dev_warn(dev, "LPM support broken, forcing max_power\n");
2502 dev->link->ap->target_lpm_policy = ATA_LPM_MAX_POWER;
2503 }
2504
Tejun Heo67465442007-05-15 03:28:16 +09002505 /* let ACPI work its magic */
2506 rc = ata_acpi_on_devcfg(dev);
2507 if (rc)
2508 return rc;
Kristen Carlson Accardi08573a82006-11-10 16:14:47 -08002509
Tejun Heo05027ad2007-09-03 12:32:57 +09002510 /* massage HPA, do it early as it might change IDENTIFY data */
2511 rc = ata_hpa_resize(dev);
2512 if (rc)
2513 return rc;
2514
Tejun Heoc39f5eb2006-03-13 19:51:19 +09002515 /* print device capabilities */
Borislav Petkov0dd4b212006-06-23 02:29:08 -04002516 if (ata_msg_probe(ap))
Joe Perchesa9a79df2011-04-15 15:51:59 -07002517 ata_dev_dbg(dev,
2518 "%s: cfg 49:%04x 82:%04x 83:%04x 84:%04x "
2519 "85:%04x 86:%04x 87:%04x 88:%04x\n",
2520 __func__,
2521 id[49], id[82], id[83], id[84],
2522 id[85], id[86], id[87], id[88]);
Tejun Heoc39f5eb2006-03-13 19:51:19 +09002523
Tejun Heo208a9932006-03-05 17:55:58 +09002524 /* initialize to-be-configured parameters */
Tejun Heoea1dd4e2006-04-02 18:51:53 +09002525 dev->flags &= ~ATA_DFLAG_CFG_MASK;
Tejun Heo208a9932006-03-05 17:55:58 +09002526 dev->max_sectors = 0;
2527 dev->cdb_len = 0;
2528 dev->n_sectors = 0;
2529 dev->cylinders = 0;
2530 dev->heads = 0;
2531 dev->sectors = 0;
Mark Lorde18086d2009-03-19 13:32:21 -04002532 dev->multi_count = 0;
Tejun Heo208a9932006-03-05 17:55:58 +09002533
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 /*
2535 * common ATA, ATAPI feature tests
2536 */
2537
Tejun Heoff8854b2006-03-06 04:31:56 +09002538 /* find max transfer mode; for printk only */
Tejun Heo1148c3a2006-03-13 19:48:04 +09002539 xfer_mask = ata_id_xfermask(id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540
Borislav Petkov0dd4b212006-06-23 02:29:08 -04002541 if (ata_msg_probe(ap))
2542 ata_dump_id(id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543
Albert Leeef143d52007-06-05 13:01:33 +08002544 /* SCSI only uses 4-char revisions, dump full 8 chars from ATA */
2545 ata_id_c_string(dev->id, fwrevbuf, ATA_ID_FW_REV,
2546 sizeof(fwrevbuf));
2547
2548 ata_id_c_string(dev->id, modelbuf, ATA_ID_PROD,
2549 sizeof(modelbuf));
2550
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551 /* ATA-specific feature tests */
Hannes Reinecke9162c652014-11-05 13:08:21 +01002552 if (dev->class == ATA_DEV_ATA || dev->class == ATA_DEV_ZAC) {
Alan Coxb352e572006-08-10 18:52:12 +01002553 if (ata_id_is_cfa(id)) {
Sergei Shtylyov62afe5d2009-04-13 20:50:00 +04002554 /* CPRM may make this media unusable */
2555 if (id[ATA_ID_CFA_KEY_MGMT] & 1)
Joe Perchesa9a79df2011-04-15 15:51:59 -07002556 ata_dev_warn(dev,
2557 "supports DRM functions and may not be fully accessible\n");
Alan Coxb352e572006-08-10 18:52:12 +01002558 snprintf(revbuf, 7, "CFA");
Alan Coxae8d4ee2007-11-04 22:05:49 -05002559 } else {
Jeff Garzik2dcb4072007-10-19 06:42:56 -04002560 snprintf(revbuf, 7, "ATA-%d", ata_id_major_version(id));
Alan Coxae8d4ee2007-11-04 22:05:49 -05002561 /* Warn the user if the device has TPM extensions */
2562 if (ata_id_has_tpm(id))
Joe Perchesa9a79df2011-04-15 15:51:59 -07002563 ata_dev_warn(dev,
2564 "supports DRM functions and may not be fully accessible\n");
Alan Coxae8d4ee2007-11-04 22:05:49 -05002565 }
Alan Coxb352e572006-08-10 18:52:12 +01002566
Tejun Heo1148c3a2006-03-13 19:48:04 +09002567 dev->n_sectors = ata_id_n_sectors(id);
Tejun Heo29407402006-02-12 22:47:04 +09002568
Mark Lorde18086d2009-03-19 13:32:21 -04002569 /* get current R/W Multiple count setting */
2570 if ((dev->id[47] >> 8) == 0x80 && (dev->id[59] & 0x100)) {
2571 unsigned int max = dev->id[47] & 0xff;
2572 unsigned int cnt = dev->id[59] & 0xff;
2573 /* only recognize/allow powers of two here */
2574 if (is_power_of_2(max) && is_power_of_2(cnt))
2575 if (cnt <= max)
2576 dev->multi_count = cnt;
2577 }
Eric D. Mudama3f64f562007-01-30 23:00:40 -07002578
Damien Le Moal891fd7c2021-08-16 10:44:50 +09002579 /* print device info to dmesg */
2580 if (ata_msg_drv(ap) && print_info)
2581 ata_dev_info(dev, "%s: %s, %s, max %s\n",
2582 revbuf, modelbuf, fwrevbuf,
2583 ata_mode_string(xfer_mask));
2584
Tejun Heo1148c3a2006-03-13 19:48:04 +09002585 if (ata_id_has_lba(id)) {
Damien Le Moal891fd7c2021-08-16 10:44:50 +09002586 rc = ata_dev_config_lba(dev);
Shaohua Li388539f2009-07-27 09:24:35 +08002587 if (rc)
2588 return rc;
Tejun Heoffeae412006-03-01 16:09:35 +09002589 } else {
Damien Le Moal891fd7c2021-08-16 10:44:50 +09002590 ata_dev_config_chs(dev);
Albert Lee07f6f7d2005-11-01 19:33:20 +08002591 }
2592
Damien Le Moald8d87782021-08-16 10:44:49 +09002593 ata_dev_config_devslp(dev);
Hannes Reineckee87fd282016-04-04 11:43:55 +02002594 ata_dev_config_sense_reporting(dev);
Hannes Reinecke6d1003a2016-04-25 12:45:56 +02002595 ata_dev_config_zac(dev);
Christoph Hellwig818831c2017-06-04 14:42:24 +02002596 ata_dev_config_trusted(dev);
Minwoo Imb1ffbf82017-06-24 03:41:10 +09002597 dev->cdb_len = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598 }
2599
2600 /* ATAPI-specific feature tests */
Jeff Garzik2c13b7c2005-11-14 14:14:16 -05002601 else if (dev->class == ATA_DEV_ATAPI) {
Tejun Heo854c73a2007-09-23 13:14:11 +09002602 const char *cdb_intr_string = "";
2603 const char *atapi_an_string = "";
Tejun Heo91163002008-02-21 13:25:50 +09002604 const char *dma_dir_string = "";
Tejun Heo7d77b242007-09-23 13:14:13 +09002605 u32 sntf;
Albert Lee08a556d2006-03-31 13:29:04 +08002606
Tejun Heo1148c3a2006-03-13 19:48:04 +09002607 rc = atapi_cdb_len(id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608 if ((rc < 12) || (rc > ATAPI_CDB_LEN)) {
Borislav Petkov0dd4b212006-06-23 02:29:08 -04002609 if (ata_msg_warn(ap))
Joe Perchesa9a79df2011-04-15 15:51:59 -07002610 ata_dev_warn(dev, "unsupported CDB len\n");
Tejun Heoffeae412006-03-01 16:09:35 +09002611 rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612 goto err_out_nosup;
2613 }
Tejun Heo6e7846e2006-02-12 23:32:58 +09002614 dev->cdb_len = (unsigned int) rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615
Tejun Heo7d77b242007-09-23 13:14:13 +09002616 /* Enable ATAPI AN if both the host and device have
2617 * the support. If PMP is attached, SNTF is required
2618 * to enable ATAPI AN to discern between PHY status
2619 * changed notifications and ATAPI ANs.
Kristen Carlson Accardi9f45cbd2007-08-15 03:57:11 -04002620 */
Tejun Heoe7ecd432010-05-19 15:38:58 +02002621 if (atapi_an &&
2622 (ap->flags & ATA_FLAG_AN) && ata_id_has_atapi_AN(id) &&
Tejun Heo071f44b2008-04-07 22:47:22 +09002623 (!sata_pmp_attached(ap) ||
Tejun Heo7d77b242007-09-23 13:14:13 +09002624 sata_scr_read(&ap->link, SCR_NOTIFICATION, &sntf) == 0)) {
Kristen Carlson Accardi9f45cbd2007-08-15 03:57:11 -04002625 /* issue SET feature command to turn this on */
Jeff Garzik218f3d32007-10-25 00:33:27 -04002626 err_mask = ata_dev_set_feature(dev,
2627 SETFEATURES_SATA_ENABLE, SATA_AN);
Tejun Heo854c73a2007-09-23 13:14:11 +09002628 if (err_mask)
Joe Perchesa9a79df2011-04-15 15:51:59 -07002629 ata_dev_err(dev,
2630 "failed to enable ATAPI AN (err_mask=0x%x)\n",
2631 err_mask);
Tejun Heo854c73a2007-09-23 13:14:11 +09002632 else {
Kristen Carlson Accardi9f45cbd2007-08-15 03:57:11 -04002633 dev->flags |= ATA_DFLAG_AN;
Tejun Heo854c73a2007-09-23 13:14:11 +09002634 atapi_an_string = ", ATAPI AN";
2635 }
Kristen Carlson Accardi9f45cbd2007-08-15 03:57:11 -04002636 }
2637
Albert Lee08a556d2006-03-31 13:29:04 +08002638 if (ata_id_cdb_intr(dev->id)) {
Albert Lee312f7da2005-09-27 17:38:03 +08002639 dev->flags |= ATA_DFLAG_CDB_INTR;
Albert Lee08a556d2006-03-31 13:29:04 +08002640 cdb_intr_string = ", CDB intr";
2641 }
Albert Lee312f7da2005-09-27 17:38:03 +08002642
Vincent Pelletier966fbe12013-05-21 22:30:58 +02002643 if (atapi_dmadir || (dev->horkage & ATA_HORKAGE_ATAPI_DMADIR) || atapi_id_dmadir(dev->id)) {
Tejun Heo91163002008-02-21 13:25:50 +09002644 dev->flags |= ATA_DFLAG_DMADIR;
2645 dma_dir_string = ", DMADIR";
2646 }
2647
Aaron Luafe75952013-01-15 17:20:58 +08002648 if (ata_id_has_da(dev->id)) {
Lin Mingb1354cb2012-06-25 16:13:08 +08002649 dev->flags |= ATA_DFLAG_DA;
Aaron Luafe75952013-01-15 17:20:58 +08002650 zpodd_init(dev);
2651 }
Lin Mingb1354cb2012-06-25 16:13:08 +08002652
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653 /* print device info to dmesg */
Borislav Petkov5afc8142006-06-27 14:51:25 +02002654 if (ata_msg_drv(ap) && print_info)
Joe Perchesa9a79df2011-04-15 15:51:59 -07002655 ata_dev_info(dev,
2656 "ATAPI: %s, %s, max %s%s%s%s\n",
2657 modelbuf, fwrevbuf,
2658 ata_mode_string(xfer_mask),
2659 cdb_intr_string, atapi_an_string,
2660 dma_dir_string);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661 }
2662
Tejun Heo914ed352006-11-01 18:39:55 +09002663 /* determine max_sectors */
2664 dev->max_sectors = ATA_MAX_SECTORS;
2665 if (dev->flags & ATA_DFLAG_LBA48)
2666 dev->max_sectors = ATA_MAX_SECTORS_LBA48;
2667
Alan Coxc5038fc2007-10-25 14:21:16 +01002668 /* Limit PATA drive on SATA cable bridge transfers to udma5,
2669 200 sectors */
Tejun Heo3373efd2006-05-15 20:57:53 +09002670 if (ata_dev_knobble(dev)) {
Borislav Petkov5afc8142006-06-27 14:51:25 +02002671 if (ata_msg_drv(ap) && print_info)
Joe Perchesa9a79df2011-04-15 15:51:59 -07002672 ata_dev_info(dev, "applying bridge limits\n");
Tejun Heo5a529132006-03-24 14:07:50 +09002673 dev->udma_mask &= ATA_UDMA5;
Tejun Heo4b2f3ed2006-03-01 16:09:36 +09002674 dev->max_sectors = ATA_MAX_SECTORS;
2675 }
2676
Tony Battersbyf8d8e572007-10-30 11:44:35 -04002677 if ((dev->class == ATA_DEV_ATAPI) &&
Albert Leef442cd82007-11-15 10:35:47 +09002678 (atapi_command_packet_set(id) == TYPE_TAPE)) {
Tony Battersbyf8d8e572007-10-30 11:44:35 -04002679 dev->max_sectors = ATA_MAX_SECTORS_TAPE;
Albert Leef442cd82007-11-15 10:35:47 +09002680 dev->horkage |= ATA_HORKAGE_STUCK_ERR;
2681 }
Tony Battersbyf8d8e572007-10-30 11:44:35 -04002682
Tejun Heo75683fe2007-07-05 13:31:27 +09002683 if (dev->horkage & ATA_HORKAGE_MAX_SEC_128)
Tejun Heo03ec52d2007-04-12 13:38:11 +09002684 dev->max_sectors = min_t(unsigned int, ATA_MAX_SECTORS_128,
2685 dev->max_sectors);
Albert Lee18d6e9d2007-04-02 11:34:15 +08002686
David Milburnaf34d632015-07-13 11:48:23 -05002687 if (dev->horkage & ATA_HORKAGE_MAX_SEC_1024)
2688 dev->max_sectors = min_t(unsigned int, ATA_MAX_SECTORS_1024,
2689 dev->max_sectors);
2690
Shan Haia32450e2013-03-18 10:30:44 +08002691 if (dev->horkage & ATA_HORKAGE_MAX_SEC_LBA48)
2692 dev->max_sectors = ATA_MAX_SECTORS_LBA48;
2693
Tejun Heo4b2f3ed2006-03-01 16:09:36 +09002694 if (ap->ops->dev_config)
Alancd0d3bb2007-03-02 00:56:15 +00002695 ap->ops->dev_config(dev);
Tejun Heo4b2f3ed2006-03-01 16:09:36 +09002696
Alan Coxc5038fc2007-10-25 14:21:16 +01002697 if (dev->horkage & ATA_HORKAGE_DIAGNOSTIC) {
2698 /* Let the user know. We don't want to disallow opens for
2699 rescue purposes, or in case the vendor is just a blithering
2700 idiot. Do this after the dev_config call as some controllers
2701 with buggy firmware may want to avoid reporting false device
2702 bugs */
2703
2704 if (print_info) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07002705 ata_dev_warn(dev,
Alan Coxc5038fc2007-10-25 14:21:16 +01002706"Drive reports diagnostics failure. This may indicate a drive\n");
Joe Perchesa9a79df2011-04-15 15:51:59 -07002707 ata_dev_warn(dev,
Alan Coxc5038fc2007-10-25 14:21:16 +01002708"fault or invalid emulation. Contact drive vendor for information.\n");
2709 }
2710 }
2711
Tejun Heoac70a962008-11-27 13:36:48 +09002712 if ((dev->horkage & ATA_HORKAGE_FIRMWARE_WARN) && print_info) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07002713 ata_dev_warn(dev, "WARNING: device requires firmware update to be fully functional\n");
2714 ata_dev_warn(dev, " contact the vendor or visit http://ata.wiki.kernel.org\n");
Tejun Heoac70a962008-11-27 13:36:48 +09002715 }
2716
Tejun Heoffeae412006-03-01 16:09:35 +09002717 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718
2719err_out_nosup:
Borislav Petkov0dd4b212006-06-23 02:29:08 -04002720 if (ata_msg_probe(ap))
Joe Perchesa9a79df2011-04-15 15:51:59 -07002721 ata_dev_dbg(dev, "%s: EXIT, err\n", __func__);
Tejun Heoffeae412006-03-01 16:09:35 +09002722 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723}
2724
2725/**
Alan Cox2e41e8e2007-03-08 23:19:19 +00002726 * ata_cable_40wire - return 40 wire cable type
Alan Coxbe0d18d2007-03-06 02:37:56 -08002727 * @ap: port
2728 *
Alan Cox2e41e8e2007-03-08 23:19:19 +00002729 * Helper method for drivers which want to hardwire 40 wire cable
Alan Coxbe0d18d2007-03-06 02:37:56 -08002730 * detection.
2731 */
2732
2733int ata_cable_40wire(struct ata_port *ap)
2734{
2735 return ATA_CBL_PATA40;
2736}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01002737EXPORT_SYMBOL_GPL(ata_cable_40wire);
Alan Coxbe0d18d2007-03-06 02:37:56 -08002738
2739/**
Alan Cox2e41e8e2007-03-08 23:19:19 +00002740 * ata_cable_80wire - return 80 wire cable type
Alan Coxbe0d18d2007-03-06 02:37:56 -08002741 * @ap: port
2742 *
Alan Cox2e41e8e2007-03-08 23:19:19 +00002743 * Helper method for drivers which want to hardwire 80 wire cable
Alan Coxbe0d18d2007-03-06 02:37:56 -08002744 * detection.
2745 */
2746
2747int ata_cable_80wire(struct ata_port *ap)
2748{
2749 return ATA_CBL_PATA80;
2750}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01002751EXPORT_SYMBOL_GPL(ata_cable_80wire);
Alan Coxbe0d18d2007-03-06 02:37:56 -08002752
2753/**
2754 * ata_cable_unknown - return unknown PATA cable.
2755 * @ap: port
2756 *
2757 * Helper method for drivers which have no PATA cable detection.
2758 */
2759
2760int ata_cable_unknown(struct ata_port *ap)
2761{
2762 return ATA_CBL_PATA_UNK;
2763}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01002764EXPORT_SYMBOL_GPL(ata_cable_unknown);
Alan Coxbe0d18d2007-03-06 02:37:56 -08002765
2766/**
Tejun Heoc88f90c2007-11-27 19:43:48 +09002767 * ata_cable_ignore - return ignored PATA cable.
2768 * @ap: port
2769 *
2770 * Helper method for drivers which don't use cable type to limit
2771 * transfer mode.
2772 */
2773int ata_cable_ignore(struct ata_port *ap)
2774{
2775 return ATA_CBL_PATA_IGN;
2776}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01002777EXPORT_SYMBOL_GPL(ata_cable_ignore);
Tejun Heoc88f90c2007-11-27 19:43:48 +09002778
2779/**
Alan Coxbe0d18d2007-03-06 02:37:56 -08002780 * ata_cable_sata - return SATA cable type
2781 * @ap: port
2782 *
2783 * Helper method for drivers which have SATA cables
2784 */
2785
2786int ata_cable_sata(struct ata_port *ap)
2787{
2788 return ATA_CBL_SATA;
2789}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01002790EXPORT_SYMBOL_GPL(ata_cable_sata);
Alan Coxbe0d18d2007-03-06 02:37:56 -08002791
2792/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793 * ata_bus_probe - Reset and probe ATA bus
2794 * @ap: Bus to probe
2795 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04002796 * Master ATA bus probing function. Initiates a hardware-dependent
2797 * bus reset, then attempts to identify any devices found on
2798 * the bus.
2799 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002801 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802 *
2803 * RETURNS:
Tejun Heo96072e62006-04-01 01:38:17 +09002804 * Zero on success, negative errno otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805 */
2806
Brian King80289162006-08-07 14:27:31 -05002807int ata_bus_probe(struct ata_port *ap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808{
Tejun Heo28ca5c52006-03-01 16:09:36 +09002809 unsigned int classes[ATA_MAX_DEVICES];
Tejun Heo14d2bac2006-04-02 17:54:46 +09002810 int tries[ATA_MAX_DEVICES];
Tejun Heof58229f2007-08-06 18:36:23 +09002811 int rc;
Tejun Heoe82cbdb2006-04-01 01:38:18 +09002812 struct ata_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813
Tejun Heo1eca4362008-11-03 20:03:17 +09002814 ata_for_each_dev(dev, &ap->link, ALL)
Tejun Heof58229f2007-08-06 18:36:23 +09002815 tries[dev->devno] = ATA_PROBE_MAX_TRIES;
Tejun Heo14d2bac2006-04-02 17:54:46 +09002816
2817 retry:
Tejun Heo1eca4362008-11-03 20:03:17 +09002818 ata_for_each_dev(dev, &ap->link, ALL) {
Tejun Heocdeab112007-10-29 16:41:09 +09002819 /* If we issue an SRST then an ATA drive (not ATAPI)
2820 * may change configuration and be in PIO0 timing. If
2821 * we do a hard reset (or are coming from power on)
2822 * this is true for ATA or ATAPI. Until we've set a
2823 * suitable controller mode we should not touch the
2824 * bus as we may be talking too fast.
2825 */
2826 dev->pio_mode = XFER_PIO_0;
Aaron Lu54169122012-12-03 11:35:02 +08002827 dev->dma_mode = 0xff;
Tejun Heocdeab112007-10-29 16:41:09 +09002828
2829 /* If the controller has a pio mode setup function
2830 * then use it to set the chipset to rights. Don't
2831 * touch the DMA setup as that will be dealt with when
2832 * configuring devices.
2833 */
2834 if (ap->ops->set_piomode)
2835 ap->ops->set_piomode(ap, dev);
2836 }
2837
Tejun Heo20444702006-03-13 01:57:01 +09002838 /* reset and determine device classes */
Tejun Heo52783c52006-05-31 18:28:22 +09002839 ap->ops->phy_reset(ap);
Tejun Heo2061a472006-03-12 00:57:39 +09002840
Tejun Heo1eca4362008-11-03 20:03:17 +09002841 ata_for_each_dev(dev, &ap->link, ALL) {
Tejun Heo3e4ec342010-05-10 21:41:30 +02002842 if (dev->class != ATA_DEV_UNKNOWN)
Tejun Heo52783c52006-05-31 18:28:22 +09002843 classes[dev->devno] = dev->class;
2844 else
2845 classes[dev->devno] = ATA_DEV_NONE;
Tejun Heo20444702006-03-13 01:57:01 +09002846
Tejun Heo52783c52006-05-31 18:28:22 +09002847 dev->class = ATA_DEV_UNKNOWN;
Tejun Heo28ca5c52006-03-01 16:09:36 +09002848 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849
Jeff Garzikf31f0cc2007-03-02 17:47:28 -05002850 /* read IDENTIFY page and configure devices. We have to do the identify
2851 specific sequence bass-ackwards so that PDIAG- is released by
2852 the slave device */
2853
Tejun Heo1eca4362008-11-03 20:03:17 +09002854 ata_for_each_dev(dev, &ap->link, ALL_REVERSE) {
Tejun Heof58229f2007-08-06 18:36:23 +09002855 if (tries[dev->devno])
2856 dev->class = classes[dev->devno];
Tejun Heo14d2bac2006-04-02 17:54:46 +09002857
Tejun Heoe1211e32006-04-01 01:38:18 +09002858 if (!ata_dev_enabled(dev))
Tejun Heoffeae412006-03-01 16:09:35 +09002859 continue;
2860
Tejun Heobff04642006-11-10 18:08:10 +09002861 rc = ata_dev_read_id(dev, &dev->class, ATA_READID_POSTRESET,
2862 dev->id);
Tejun Heo14d2bac2006-04-02 17:54:46 +09002863 if (rc)
2864 goto fail;
Jeff Garzikf31f0cc2007-03-02 17:47:28 -05002865 }
2866
Alan Coxbe0d18d2007-03-06 02:37:56 -08002867 /* Now ask for the cable type as PDIAG- should have been released */
2868 if (ap->ops->cable_detect)
2869 ap->cbl = ap->ops->cable_detect(ap);
2870
Tejun Heo1eca4362008-11-03 20:03:17 +09002871 /* We may have SATA bridge glue hiding here irrespective of
2872 * the reported cable types and sensed types. When SATA
2873 * drives indicate we have a bridge, we don't know which end
2874 * of the link the bridge is which is a problem.
2875 */
2876 ata_for_each_dev(dev, &ap->link, ENABLED)
Alan Cox614fe292007-08-22 23:22:45 +01002877 if (ata_id_is_sata(dev->id))
2878 ap->cbl = ATA_CBL_SATA;
Alan Cox614fe292007-08-22 23:22:45 +01002879
Jeff Garzikf31f0cc2007-03-02 17:47:28 -05002880 /* After the identify sequence we can now set up the devices. We do
2881 this in the normal order so that the user doesn't get confused */
2882
Tejun Heo1eca4362008-11-03 20:03:17 +09002883 ata_for_each_dev(dev, &ap->link, ENABLED) {
Tejun Heo9af5c9c2007-08-06 18:36:22 +09002884 ap->link.eh_context.i.flags |= ATA_EHI_PRINTINFO;
Tejun Heoefdaedc2006-11-01 18:38:52 +09002885 rc = ata_dev_configure(dev);
Tejun Heo9af5c9c2007-08-06 18:36:22 +09002886 ap->link.eh_context.i.flags &= ~ATA_EHI_PRINTINFO;
Tejun Heo14d2bac2006-04-02 17:54:46 +09002887 if (rc)
2888 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889 }
2890
Tejun Heoe82cbdb2006-04-01 01:38:18 +09002891 /* configure transfer mode */
Tejun Heo02607312007-08-06 18:36:23 +09002892 rc = ata_set_mode(&ap->link, &dev);
Tejun Heo4ae72a12007-02-02 16:22:30 +09002893 if (rc)
Tejun Heo51713d32006-04-11 22:26:29 +09002894 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895
Tejun Heo1eca4362008-11-03 20:03:17 +09002896 ata_for_each_dev(dev, &ap->link, ENABLED)
2897 return 0;
Alan Coxe35a9e02006-03-27 18:46:37 +01002898
Tejun Heo96072e62006-04-01 01:38:17 +09002899 return -ENODEV;
Tejun Heo14d2bac2006-04-02 17:54:46 +09002900
2901 fail:
Tejun Heo4ae72a12007-02-02 16:22:30 +09002902 tries[dev->devno]--;
2903
Tejun Heo14d2bac2006-04-02 17:54:46 +09002904 switch (rc) {
2905 case -EINVAL:
Tejun Heo4ae72a12007-02-02 16:22:30 +09002906 /* eeek, something went very wrong, give up */
Tejun Heo14d2bac2006-04-02 17:54:46 +09002907 tries[dev->devno] = 0;
2908 break;
Tejun Heo4ae72a12007-02-02 16:22:30 +09002909
2910 case -ENODEV:
2911 /* give it just one more chance */
2912 tries[dev->devno] = min(tries[dev->devno], 1);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05002913 fallthrough;
Tejun Heo14d2bac2006-04-02 17:54:46 +09002914 case -EIO:
Tejun Heo4ae72a12007-02-02 16:22:30 +09002915 if (tries[dev->devno] == 1) {
2916 /* This is the last chance, better to slow
2917 * down than lose it.
2918 */
Tejun Heoa07d4992009-01-29 20:31:33 +09002919 sata_down_spd_limit(&ap->link, 0);
Tejun Heo4ae72a12007-02-02 16:22:30 +09002920 ata_down_xfermask_limit(dev, ATA_DNXFER_PIO);
2921 }
Tejun Heo14d2bac2006-04-02 17:54:46 +09002922 }
2923
Tejun Heo4ae72a12007-02-02 16:22:30 +09002924 if (!tries[dev->devno])
Tejun Heo3373efd2006-05-15 20:57:53 +09002925 ata_dev_disable(dev);
Tejun Heoec573752006-04-11 22:26:29 +09002926
Tejun Heo14d2bac2006-04-02 17:54:46 +09002927 goto retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928}
2929
2930/**
Tejun Heo3be680b2005-12-19 22:35:02 +09002931 * sata_print_link_status - Print SATA link status
Tejun Heo936fd732007-08-06 18:36:23 +09002932 * @link: SATA link to printk link status about
Tejun Heo3be680b2005-12-19 22:35:02 +09002933 *
2934 * This function prints link speed and status of a SATA link.
2935 *
2936 * LOCKING:
2937 * None.
2938 */
Adrian Bunk6bdb4fc2008-04-21 11:51:11 +03002939static void sata_print_link_status(struct ata_link *link)
Tejun Heo3be680b2005-12-19 22:35:02 +09002940{
Tejun Heo6d5f9732006-04-03 00:09:41 +09002941 u32 sstatus, scontrol, tmp;
Tejun Heo3be680b2005-12-19 22:35:02 +09002942
Tejun Heo936fd732007-08-06 18:36:23 +09002943 if (sata_scr_read(link, SCR_STATUS, &sstatus))
Tejun Heo3be680b2005-12-19 22:35:02 +09002944 return;
Tejun Heo936fd732007-08-06 18:36:23 +09002945 sata_scr_read(link, SCR_CONTROL, &scontrol);
Tejun Heo3be680b2005-12-19 22:35:02 +09002946
Tejun Heob1c72912008-07-31 17:02:43 +09002947 if (ata_phys_link_online(link)) {
Tejun Heo3be680b2005-12-19 22:35:02 +09002948 tmp = (sstatus >> 4) & 0xf;
Joe Perchesa9a79df2011-04-15 15:51:59 -07002949 ata_link_info(link, "SATA link up %s (SStatus %X SControl %X)\n",
2950 sata_spd_string(tmp), sstatus, scontrol);
Tejun Heo3be680b2005-12-19 22:35:02 +09002951 } else {
Joe Perchesa9a79df2011-04-15 15:51:59 -07002952 ata_link_info(link, "SATA link down (SStatus %X SControl %X)\n",
2953 sstatus, scontrol);
Tejun Heo3be680b2005-12-19 22:35:02 +09002954 }
2955}
2956
2957/**
Alan Coxebdfca6e2006-03-23 15:38:34 +00002958 * ata_dev_pair - return other device on cable
Alan Coxebdfca6e2006-03-23 15:38:34 +00002959 * @adev: device
2960 *
2961 * Obtain the other device on the same cable, or if none is
2962 * present NULL is returned
2963 */
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05002964
Tejun Heo3373efd2006-05-15 20:57:53 +09002965struct ata_device *ata_dev_pair(struct ata_device *adev)
Alan Coxebdfca6e2006-03-23 15:38:34 +00002966{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09002967 struct ata_link *link = adev->link;
2968 struct ata_device *pair = &link->device[1 - adev->devno];
Tejun Heoe1211e32006-04-01 01:38:18 +09002969 if (!ata_dev_enabled(pair))
Alan Coxebdfca6e2006-03-23 15:38:34 +00002970 return NULL;
2971 return pair;
2972}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01002973EXPORT_SYMBOL_GPL(ata_dev_pair);
Alan Coxebdfca6e2006-03-23 15:38:34 +00002974
2975/**
Tejun Heo3c567b72006-05-15 20:57:23 +09002976 * sata_down_spd_limit - adjust SATA spd limit downward
Tejun Heo936fd732007-08-06 18:36:23 +09002977 * @link: Link to adjust SATA spd limit for
Tejun Heoa07d4992009-01-29 20:31:33 +09002978 * @spd_limit: Additional limit
Tejun Heo1c3fae42006-04-02 20:53:28 +09002979 *
Tejun Heo936fd732007-08-06 18:36:23 +09002980 * Adjust SATA spd limit of @link downward. Note that this
Tejun Heo1c3fae42006-04-02 20:53:28 +09002981 * function only adjusts the limit. The change must be applied
Tejun Heo3c567b72006-05-15 20:57:23 +09002982 * using sata_set_spd().
Tejun Heo1c3fae42006-04-02 20:53:28 +09002983 *
Tejun Heoa07d4992009-01-29 20:31:33 +09002984 * If @spd_limit is non-zero, the speed is limited to equal to or
2985 * lower than @spd_limit if such speed is supported. If
2986 * @spd_limit is slower than any supported speed, only the lowest
2987 * supported speed is allowed.
2988 *
Tejun Heo1c3fae42006-04-02 20:53:28 +09002989 * LOCKING:
2990 * Inherited from caller.
2991 *
2992 * RETURNS:
2993 * 0 on success, negative errno on failure
2994 */
Tejun Heoa07d4992009-01-29 20:31:33 +09002995int sata_down_spd_limit(struct ata_link *link, u32 spd_limit)
Tejun Heo1c3fae42006-04-02 20:53:28 +09002996{
Tejun Heo81952c52006-05-15 20:57:47 +09002997 u32 sstatus, spd, mask;
Tejun Heoa07d4992009-01-29 20:31:33 +09002998 int rc, bit;
Tejun Heo1c3fae42006-04-02 20:53:28 +09002999
Tejun Heo936fd732007-08-06 18:36:23 +09003000 if (!sata_scr_valid(link))
Tejun Heo008a7892007-07-16 14:29:40 +09003001 return -EOPNOTSUPP;
3002
3003 /* If SCR can be read, use it to determine the current SPD.
Tejun Heo936fd732007-08-06 18:36:23 +09003004 * If not, use cached value in link->sata_spd.
Tejun Heo008a7892007-07-16 14:29:40 +09003005 */
Tejun Heo936fd732007-08-06 18:36:23 +09003006 rc = sata_scr_read(link, SCR_STATUS, &sstatus);
Tejun Heo9913ff8a2009-01-29 20:31:31 +09003007 if (rc == 0 && ata_sstatus_online(sstatus))
Tejun Heo008a7892007-07-16 14:29:40 +09003008 spd = (sstatus >> 4) & 0xf;
3009 else
Tejun Heo936fd732007-08-06 18:36:23 +09003010 spd = link->sata_spd;
Tejun Heo1c3fae42006-04-02 20:53:28 +09003011
Tejun Heo936fd732007-08-06 18:36:23 +09003012 mask = link->sata_spd_limit;
Tejun Heo1c3fae42006-04-02 20:53:28 +09003013 if (mask <= 1)
3014 return -EINVAL;
Tejun Heo008a7892007-07-16 14:29:40 +09003015
3016 /* unconditionally mask off the highest bit */
Tejun Heoa07d4992009-01-29 20:31:33 +09003017 bit = fls(mask) - 1;
3018 mask &= ~(1 << bit);
Tejun Heo1c3fae42006-04-02 20:53:28 +09003019
David Milburn2dc0b462017-11-14 16:17:25 -06003020 /*
3021 * Mask off all speeds higher than or equal to the current one. At
3022 * this point, if current SPD is not available and we previously
3023 * recorded the link speed from SStatus, the driver has already
3024 * masked off the highest bit so mask should already be 1 or 0.
3025 * Otherwise, we should not force 1.5Gbps on a link where we have
3026 * not previously recorded speed from SStatus. Just return in this
3027 * case.
Tejun Heo008a7892007-07-16 14:29:40 +09003028 */
3029 if (spd > 1)
3030 mask &= (1 << (spd - 1)) - 1;
3031 else
David Milburn2dc0b462017-11-14 16:17:25 -06003032 return -EINVAL;
Tejun Heo008a7892007-07-16 14:29:40 +09003033
3034 /* were we already at the bottom? */
Tejun Heo1c3fae42006-04-02 20:53:28 +09003035 if (!mask)
3036 return -EINVAL;
3037
Tejun Heoa07d4992009-01-29 20:31:33 +09003038 if (spd_limit) {
3039 if (mask & ((1 << spd_limit) - 1))
3040 mask &= (1 << spd_limit) - 1;
3041 else {
3042 bit = ffs(mask) - 1;
3043 mask = 1 << bit;
3044 }
3045 }
3046
Tejun Heo936fd732007-08-06 18:36:23 +09003047 link->sata_spd_limit = mask;
Tejun Heo1c3fae42006-04-02 20:53:28 +09003048
Joe Perchesa9a79df2011-04-15 15:51:59 -07003049 ata_link_warn(link, "limiting SATA link speed to %s\n",
3050 sata_spd_string(fls(mask)));
Tejun Heo1c3fae42006-04-02 20:53:28 +09003051
3052 return 0;
3053}
3054
Bartlomiej Zolnierkiewicza9b2c122020-03-26 16:58:07 +01003055#ifdef CONFIG_ATA_ACPI
Tejun Heocf176e12006-04-02 17:54:46 +09003056/**
Tejun Heoa0f79b92007-12-18 16:33:05 +09003057 * ata_timing_cycle2mode - find xfer mode for the specified cycle duration
3058 * @xfer_shift: ATA_SHIFT_* value for transfer type to examine.
3059 * @cycle: cycle duration in ns
3060 *
3061 * Return matching xfer mode for @cycle. The returned mode is of
3062 * the transfer type specified by @xfer_shift. If @cycle is too
3063 * slow for @xfer_shift, 0xff is returned. If @cycle is faster
3064 * than the fastest known mode, the fasted mode is returned.
3065 *
3066 * LOCKING:
3067 * None.
3068 *
3069 * RETURNS:
3070 * Matching xfer_mode, 0xff if no match found.
3071 */
3072u8 ata_timing_cycle2mode(unsigned int xfer_shift, int cycle)
3073{
3074 u8 base_mode = 0xff, last_mode = 0xff;
3075 const struct ata_xfer_ent *ent;
3076 const struct ata_timing *t;
3077
3078 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
3079 if (ent->shift == xfer_shift)
3080 base_mode = ent->base;
3081
3082 for (t = ata_timing_find_mode(base_mode);
3083 t && ata_xfer_mode2shift(t->mode) == xfer_shift; t++) {
3084 unsigned short this_cycle;
3085
3086 switch (xfer_shift) {
3087 case ATA_SHIFT_PIO:
3088 case ATA_SHIFT_MWDMA:
3089 this_cycle = t->cycle;
3090 break;
3091 case ATA_SHIFT_UDMA:
3092 this_cycle = t->udma;
3093 break;
3094 default:
3095 return 0xff;
3096 }
3097
3098 if (cycle > this_cycle)
3099 break;
3100
3101 last_mode = t->mode;
3102 }
3103
3104 return last_mode;
3105}
Bartlomiej Zolnierkiewicza9b2c122020-03-26 16:58:07 +01003106#endif
Tejun Heoa0f79b92007-12-18 16:33:05 +09003107
3108/**
Tejun Heocf176e12006-04-02 17:54:46 +09003109 * ata_down_xfermask_limit - adjust dev xfer masks downward
Tejun Heocf176e12006-04-02 17:54:46 +09003110 * @dev: Device to adjust xfer masks
Tejun Heo458337d2007-02-02 16:22:30 +09003111 * @sel: ATA_DNXFER_* selector
Tejun Heocf176e12006-04-02 17:54:46 +09003112 *
3113 * Adjust xfer masks of @dev downward. Note that this function
3114 * does not apply the change. Invoking ata_set_mode() afterwards
3115 * will apply the limit.
3116 *
3117 * LOCKING:
3118 * Inherited from caller.
3119 *
3120 * RETURNS:
3121 * 0 on success, negative errno on failure
3122 */
Tejun Heo458337d2007-02-02 16:22:30 +09003123int ata_down_xfermask_limit(struct ata_device *dev, unsigned int sel)
Tejun Heocf176e12006-04-02 17:54:46 +09003124{
Tejun Heo458337d2007-02-02 16:22:30 +09003125 char buf[32];
Tejun Heo7dc951a2007-11-27 19:43:42 +09003126 unsigned long orig_mask, xfer_mask;
3127 unsigned long pio_mask, mwdma_mask, udma_mask;
Tejun Heo458337d2007-02-02 16:22:30 +09003128 int quiet, highbit;
Tejun Heocf176e12006-04-02 17:54:46 +09003129
Tejun Heo458337d2007-02-02 16:22:30 +09003130 quiet = !!(sel & ATA_DNXFER_QUIET);
3131 sel &= ~ATA_DNXFER_QUIET;
Tejun Heocf176e12006-04-02 17:54:46 +09003132
Tejun Heo458337d2007-02-02 16:22:30 +09003133 xfer_mask = orig_mask = ata_pack_xfermask(dev->pio_mask,
3134 dev->mwdma_mask,
3135 dev->udma_mask);
3136 ata_unpack_xfermask(xfer_mask, &pio_mask, &mwdma_mask, &udma_mask);
Tejun Heocf176e12006-04-02 17:54:46 +09003137
Tejun Heo458337d2007-02-02 16:22:30 +09003138 switch (sel) {
3139 case ATA_DNXFER_PIO:
3140 highbit = fls(pio_mask) - 1;
3141 pio_mask &= ~(1 << highbit);
3142 break;
3143
3144 case ATA_DNXFER_DMA:
3145 if (udma_mask) {
3146 highbit = fls(udma_mask) - 1;
3147 udma_mask &= ~(1 << highbit);
3148 if (!udma_mask)
3149 return -ENOENT;
3150 } else if (mwdma_mask) {
3151 highbit = fls(mwdma_mask) - 1;
3152 mwdma_mask &= ~(1 << highbit);
3153 if (!mwdma_mask)
3154 return -ENOENT;
3155 }
3156 break;
3157
3158 case ATA_DNXFER_40C:
3159 udma_mask &= ATA_UDMA_MASK_40C;
3160 break;
3161
3162 case ATA_DNXFER_FORCE_PIO0:
3163 pio_mask &= 1;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05003164 fallthrough;
Tejun Heo458337d2007-02-02 16:22:30 +09003165 case ATA_DNXFER_FORCE_PIO:
3166 mwdma_mask = 0;
3167 udma_mask = 0;
3168 break;
3169
Tejun Heo458337d2007-02-02 16:22:30 +09003170 default:
3171 BUG();
3172 }
3173
3174 xfer_mask &= ata_pack_xfermask(pio_mask, mwdma_mask, udma_mask);
3175
3176 if (!(xfer_mask & ATA_MASK_PIO) || xfer_mask == orig_mask)
3177 return -ENOENT;
3178
3179 if (!quiet) {
3180 if (xfer_mask & (ATA_MASK_MWDMA | ATA_MASK_UDMA))
3181 snprintf(buf, sizeof(buf), "%s:%s",
3182 ata_mode_string(xfer_mask),
3183 ata_mode_string(xfer_mask & ATA_MASK_PIO));
3184 else
3185 snprintf(buf, sizeof(buf), "%s",
3186 ata_mode_string(xfer_mask));
3187
Joe Perchesa9a79df2011-04-15 15:51:59 -07003188 ata_dev_warn(dev, "limiting speed to %s\n", buf);
Tejun Heo458337d2007-02-02 16:22:30 +09003189 }
Tejun Heocf176e12006-04-02 17:54:46 +09003190
3191 ata_unpack_xfermask(xfer_mask, &dev->pio_mask, &dev->mwdma_mask,
3192 &dev->udma_mask);
3193
Tejun Heocf176e12006-04-02 17:54:46 +09003194 return 0;
Tejun Heocf176e12006-04-02 17:54:46 +09003195}
3196
Tejun Heo3373efd2006-05-15 20:57:53 +09003197static int ata_dev_set_mode(struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198{
Tejun Heod0cb43b2009-07-09 09:27:50 +09003199 struct ata_port *ap = dev->link->ap;
Tejun Heo9af5c9c2007-08-06 18:36:22 +09003200 struct ata_eh_context *ehc = &dev->link->eh_context;
Tejun Heod0cb43b2009-07-09 09:27:50 +09003201 const bool nosetxfer = dev->horkage & ATA_HORKAGE_NOSETXFER;
Tejun Heo4055dee2008-02-07 10:34:08 +09003202 const char *dev_err_whine = "";
3203 int ign_dev_err = 0;
Tejun Heod0cb43b2009-07-09 09:27:50 +09003204 unsigned int err_mask = 0;
Tejun Heo83206a22006-03-24 15:25:31 +09003205 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206
Tejun Heoe8384602006-04-02 18:51:53 +09003207 dev->flags &= ~ATA_DFLAG_PIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208 if (dev->xfer_shift == ATA_SHIFT_PIO)
3209 dev->flags |= ATA_DFLAG_PIO;
3210
Tejun Heod0cb43b2009-07-09 09:27:50 +09003211 if (nosetxfer && ap->flags & ATA_FLAG_SATA && ata_id_is_sata(dev->id))
3212 dev_err_whine = " (SET_XFERMODE skipped)";
3213 else {
3214 if (nosetxfer)
Joe Perchesa9a79df2011-04-15 15:51:59 -07003215 ata_dev_warn(dev,
3216 "NOSETXFER but PATA detected - can't "
3217 "skip SETXFER, might malfunction\n");
Tejun Heod0cb43b2009-07-09 09:27:50 +09003218 err_mask = ata_dev_set_xfermode(dev);
3219 }
Jeff Garzik2dcb4072007-10-19 06:42:56 -04003220
Tejun Heo4055dee2008-02-07 10:34:08 +09003221 if (err_mask & ~AC_ERR_DEV)
3222 goto fail;
Jeff Garzik2dcb4072007-10-19 06:42:56 -04003223
Tejun Heo4055dee2008-02-07 10:34:08 +09003224 /* revalidate */
Tejun Heobaa1e782006-11-01 18:39:27 +09003225 ehc->i.flags |= ATA_EHI_POST_SETMODE;
Tejun Heo422c9da2007-09-23 13:14:12 +09003226 rc = ata_dev_revalidate(dev, ATA_DEV_UNKNOWN, 0);
Tejun Heobaa1e782006-11-01 18:39:27 +09003227 ehc->i.flags &= ~ATA_EHI_POST_SETMODE;
Tejun Heo5eb45c02006-04-02 18:51:52 +09003228 if (rc)
Tejun Heo83206a22006-03-24 15:25:31 +09003229 return rc;
Tejun Heo48a8a142006-03-05 17:55:58 +09003230
Alan Coxb93fda12008-04-08 16:36:44 +01003231 if (dev->xfer_shift == ATA_SHIFT_PIO) {
3232 /* Old CFA may refuse this command, which is just fine */
3233 if (ata_id_is_cfa(dev->id))
3234 ign_dev_err = 1;
3235 /* Catch several broken garbage emulations plus some pre
3236 ATA devices */
3237 if (ata_id_major_version(dev->id) == 0 &&
3238 dev->pio_mode <= XFER_PIO_2)
3239 ign_dev_err = 1;
3240 /* Some very old devices and some bad newer ones fail
3241 any kind of SET_XFERMODE request but support PIO0-2
3242 timings and no IORDY */
3243 if (!ata_id_has_iordy(dev->id) && dev->pio_mode <= XFER_PIO_2)
3244 ign_dev_err = 1;
3245 }
Tejun Heo4055dee2008-02-07 10:34:08 +09003246 /* Early MWDMA devices do DMA but don't allow DMA mode setting.
3247 Don't fail an MWDMA0 set IFF the device indicates it is in MWDMA0 */
3248 if (dev->xfer_shift == ATA_SHIFT_MWDMA &&
3249 dev->dma_mode == XFER_MW_DMA_0 &&
3250 (dev->id[63] >> 8) & 1)
3251 ign_dev_err = 1;
3252
3253 /* if the device is actually configured correctly, ignore dev err */
3254 if (dev->xfer_mode == ata_xfer_mask2mode(ata_id_xfermask(dev->id)))
3255 ign_dev_err = 1;
3256
3257 if (err_mask & AC_ERR_DEV) {
3258 if (!ign_dev_err)
3259 goto fail;
3260 else
3261 dev_err_whine = " (device error ignored)";
3262 }
3263
Tejun Heo23e71c32006-03-06 04:31:57 +09003264 DPRINTK("xfer_shift=%u, xfer_mode=0x%x\n",
3265 dev->xfer_shift, (int)dev->xfer_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266
Damien Le Moal07b9b6d2018-05-09 09:28:10 +09003267 if (!(ehc->i.flags & ATA_EHI_QUIET) ||
3268 ehc->i.flags & ATA_EHI_DID_HARDRESET)
3269 ata_dev_info(dev, "configured for %s%s\n",
3270 ata_mode_string(ata_xfer_mode2mask(dev->xfer_mode)),
3271 dev_err_whine);
Tejun Heo4055dee2008-02-07 10:34:08 +09003272
Tejun Heo83206a22006-03-24 15:25:31 +09003273 return 0;
Tejun Heo4055dee2008-02-07 10:34:08 +09003274
3275 fail:
Joe Perchesa9a79df2011-04-15 15:51:59 -07003276 ata_dev_err(dev, "failed to set xfermode (err_mask=0x%x)\n", err_mask);
Tejun Heo4055dee2008-02-07 10:34:08 +09003277 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278}
3279
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280/**
Alan04351822007-03-06 02:37:52 -08003281 * ata_do_set_mode - Program timings and issue SET FEATURES - XFER
Tejun Heo02607312007-08-06 18:36:23 +09003282 * @link: link on which timings will be programmed
Joe Perches1967b7f2008-02-03 17:08:11 +02003283 * @r_failed_dev: out parameter for failed device
Linus Torvalds1da177e2005-04-16 15:20:36 -07003284 *
Alan04351822007-03-06 02:37:52 -08003285 * Standard implementation of the function used to tune and set
3286 * ATA device disk transfer mode (PIO3, UDMA6, etc.). If
3287 * ata_dev_set_mode() fails, pointer to the failing device is
Tejun Heoe82cbdb2006-04-01 01:38:18 +09003288 * returned in @r_failed_dev.
Jeff Garzik780a87f2005-05-30 15:41:05 -04003289 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003291 * PCI/etc. bus probe sem.
Tejun Heoe82cbdb2006-04-01 01:38:18 +09003292 *
3293 * RETURNS:
3294 * 0 on success, negative errno otherwise
Linus Torvalds1da177e2005-04-16 15:20:36 -07003295 */
Alan04351822007-03-06 02:37:52 -08003296
Tejun Heo02607312007-08-06 18:36:23 +09003297int ata_do_set_mode(struct ata_link *link, struct ata_device **r_failed_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298{
Tejun Heo02607312007-08-06 18:36:23 +09003299 struct ata_port *ap = link->ap;
Tejun Heoe8e06192006-04-01 01:38:18 +09003300 struct ata_device *dev;
Tejun Heof58229f2007-08-06 18:36:23 +09003301 int rc = 0, used_dma = 0, found = 0;
Tejun Heo3adcebb2006-05-15 20:57:37 +09003302
Tejun Heoa6d5a512006-03-06 04:31:57 +09003303 /* step 1: calculate xfer_mask */
Tejun Heo1eca4362008-11-03 20:03:17 +09003304 ata_for_each_dev(dev, link, ENABLED) {
Tejun Heo7dc951a2007-11-27 19:43:42 +09003305 unsigned long pio_mask, dma_mask;
Alan Coxb3a70602007-10-02 12:38:26 -04003306 unsigned int mode_mask;
Tejun Heoa6d5a512006-03-06 04:31:57 +09003307
Alan Coxb3a70602007-10-02 12:38:26 -04003308 mode_mask = ATA_DMA_MASK_ATA;
3309 if (dev->class == ATA_DEV_ATAPI)
3310 mode_mask = ATA_DMA_MASK_ATAPI;
3311 else if (ata_id_is_cfa(dev->id))
3312 mode_mask = ATA_DMA_MASK_CFA;
3313
Tejun Heo3373efd2006-05-15 20:57:53 +09003314 ata_dev_xfermask(dev);
Tejun Heo33267322008-02-13 09:15:09 +09003315 ata_force_xfermask(dev);
Tejun Heoa6d5a512006-03-06 04:31:57 +09003316
Tejun Heoacf356b2006-03-24 14:07:50 +09003317 pio_mask = ata_pack_xfermask(dev->pio_mask, 0, 0);
Alan Coxb3a70602007-10-02 12:38:26 -04003318
3319 if (libata_dma_mask & mode_mask)
Sergei Shtylyov80a9c432011-12-22 22:41:32 +03003320 dma_mask = ata_pack_xfermask(0, dev->mwdma_mask,
3321 dev->udma_mask);
Alan Coxb3a70602007-10-02 12:38:26 -04003322 else
3323 dma_mask = 0;
3324
Tejun Heoacf356b2006-03-24 14:07:50 +09003325 dev->pio_mode = ata_xfer_mask2mode(pio_mask);
3326 dev->dma_mode = ata_xfer_mask2mode(dma_mask);
Alan Cox5444a6f2006-03-27 18:58:20 +01003327
Tejun Heo4f659772006-04-01 01:38:18 +09003328 found = 1;
Alan Coxb15b3eb2008-08-01 09:18:34 +01003329 if (ata_dma_enabled(dev))
Alan Cox5444a6f2006-03-27 18:58:20 +01003330 used_dma = 1;
Tejun Heoa6d5a512006-03-06 04:31:57 +09003331 }
Tejun Heo4f659772006-04-01 01:38:18 +09003332 if (!found)
Tejun Heoe82cbdb2006-04-01 01:38:18 +09003333 goto out;
Tejun Heoa6d5a512006-03-06 04:31:57 +09003334
3335 /* step 2: always set host PIO timings */
Tejun Heo1eca4362008-11-03 20:03:17 +09003336 ata_for_each_dev(dev, link, ENABLED) {
Tejun Heo70cd0712007-11-27 19:43:40 +09003337 if (dev->pio_mode == 0xff) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07003338 ata_dev_warn(dev, "no PIO support\n");
Tejun Heoe8e06192006-04-01 01:38:18 +09003339 rc = -EINVAL;
Tejun Heoe82cbdb2006-04-01 01:38:18 +09003340 goto out;
Tejun Heoe8e06192006-04-01 01:38:18 +09003341 }
3342
3343 dev->xfer_mode = dev->pio_mode;
3344 dev->xfer_shift = ATA_SHIFT_PIO;
3345 if (ap->ops->set_piomode)
3346 ap->ops->set_piomode(ap, dev);
3347 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348
Tejun Heoa6d5a512006-03-06 04:31:57 +09003349 /* step 3: set host DMA timings */
Tejun Heo1eca4362008-11-03 20:03:17 +09003350 ata_for_each_dev(dev, link, ENABLED) {
3351 if (!ata_dma_enabled(dev))
Tejun Heoe8e06192006-04-01 01:38:18 +09003352 continue;
3353
3354 dev->xfer_mode = dev->dma_mode;
3355 dev->xfer_shift = ata_xfer_mode2shift(dev->dma_mode);
3356 if (ap->ops->set_dmamode)
3357 ap->ops->set_dmamode(ap, dev);
3358 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003359
3360 /* step 4: update devices' xfer mode */
Tejun Heo1eca4362008-11-03 20:03:17 +09003361 ata_for_each_dev(dev, link, ENABLED) {
Tejun Heo3373efd2006-05-15 20:57:53 +09003362 rc = ata_dev_set_mode(dev);
Tejun Heo5bbc53f2006-04-01 01:38:17 +09003363 if (rc)
Tejun Heoe82cbdb2006-04-01 01:38:18 +09003364 goto out;
Tejun Heo83206a22006-03-24 15:25:31 +09003365 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003366
Tejun Heoe8e06192006-04-01 01:38:18 +09003367 /* Record simplex status. If we selected DMA then the other
3368 * host channels are not permitted to do so.
Alan Cox5444a6f2006-03-27 18:58:20 +01003369 */
Jeff Garzikcca39742006-08-24 03:19:22 -04003370 if (used_dma && (ap->host->flags & ATA_HOST_SIMPLEX))
Alan032af1c2007-03-01 17:36:46 +00003371 ap->host->simplex_claimed = ap;
Alan Cox5444a6f2006-03-27 18:58:20 +01003372
Tejun Heoe82cbdb2006-04-01 01:38:18 +09003373 out:
3374 if (rc)
3375 *r_failed_dev = dev;
3376 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01003378EXPORT_SYMBOL_GPL(ata_do_set_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379
3380/**
Tejun Heoaa2731a2008-04-07 22:47:19 +09003381 * ata_wait_ready - wait for link to become ready
3382 * @link: link to be waited on
3383 * @deadline: deadline jiffies for the operation
3384 * @check_ready: callback to check link readiness
3385 *
3386 * Wait for @link to become ready. @check_ready should return
3387 * positive number if @link is ready, 0 if it isn't, -ENODEV if
3388 * link doesn't seem to be occupied, other errno for other error
3389 * conditions.
3390 *
3391 * Transient -ENODEV conditions are allowed for
3392 * ATA_TMOUT_FF_WAIT.
3393 *
3394 * LOCKING:
3395 * EH context.
3396 *
3397 * RETURNS:
Masanari Iidac9b55602016-04-13 23:36:27 +09003398 * 0 if @link is ready before @deadline; otherwise, -errno.
Tejun Heoaa2731a2008-04-07 22:47:19 +09003399 */
3400int ata_wait_ready(struct ata_link *link, unsigned long deadline,
3401 int (*check_ready)(struct ata_link *link))
3402{
3403 unsigned long start = jiffies;
Tejun Heob48d58f2010-04-09 19:46:38 +09003404 unsigned long nodev_deadline;
Tejun Heoaa2731a2008-04-07 22:47:19 +09003405 int warned = 0;
3406
Tejun Heob48d58f2010-04-09 19:46:38 +09003407 /* choose which 0xff timeout to use, read comment in libata.h */
3408 if (link->ap->host->flags & ATA_HOST_PARALLEL_SCAN)
3409 nodev_deadline = ata_deadline(start, ATA_TMOUT_FF_WAIT_LONG);
3410 else
3411 nodev_deadline = ata_deadline(start, ATA_TMOUT_FF_WAIT);
3412
Tejun Heob1c72912008-07-31 17:02:43 +09003413 /* Slave readiness can't be tested separately from master. On
3414 * M/S emulation configuration, this function should be called
3415 * only on the master and it will handle both master and slave.
3416 */
3417 WARN_ON(link == link->ap->slave_link);
3418
Tejun Heoaa2731a2008-04-07 22:47:19 +09003419 if (time_after(nodev_deadline, deadline))
3420 nodev_deadline = deadline;
3421
3422 while (1) {
3423 unsigned long now = jiffies;
3424 int ready, tmp;
3425
3426 ready = tmp = check_ready(link);
3427 if (ready > 0)
3428 return 0;
3429
Tejun Heob48d58f2010-04-09 19:46:38 +09003430 /*
3431 * -ENODEV could be transient. Ignore -ENODEV if link
Tejun Heoaa2731a2008-04-07 22:47:19 +09003432 * is online. Also, some SATA devices take a long
Tejun Heob48d58f2010-04-09 19:46:38 +09003433 * time to clear 0xff after reset. Wait for
3434 * ATA_TMOUT_FF_WAIT[_LONG] on -ENODEV if link isn't
3435 * offline.
Tejun Heoaa2731a2008-04-07 22:47:19 +09003436 *
3437 * Note that some PATA controllers (pata_ali) explode
3438 * if status register is read more than once when
3439 * there's no device attached.
3440 */
3441 if (ready == -ENODEV) {
3442 if (ata_link_online(link))
3443 ready = 0;
3444 else if ((link->ap->flags & ATA_FLAG_SATA) &&
3445 !ata_link_offline(link) &&
3446 time_before(now, nodev_deadline))
3447 ready = 0;
3448 }
3449
3450 if (ready)
3451 return ready;
3452 if (time_after(now, deadline))
3453 return -EBUSY;
3454
3455 if (!warned && time_after(now, start + 5 * HZ) &&
3456 (deadline - now > 3 * HZ)) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07003457 ata_link_warn(link,
Tejun Heoaa2731a2008-04-07 22:47:19 +09003458 "link is slow to respond, please be patient "
3459 "(ready=%d)\n", tmp);
3460 warned = 1;
3461 }
3462
Tejun Heo97750ce2010-09-06 17:56:29 +02003463 ata_msleep(link->ap, 50);
Tejun Heoaa2731a2008-04-07 22:47:19 +09003464 }
3465}
3466
3467/**
3468 * ata_wait_after_reset - wait for link to become ready after reset
3469 * @link: link to be waited on
3470 * @deadline: deadline jiffies for the operation
3471 * @check_ready: callback to check link readiness
3472 *
3473 * Wait for @link to become ready after reset.
3474 *
3475 * LOCKING:
3476 * EH context.
3477 *
3478 * RETURNS:
Masanari Iidac9b55602016-04-13 23:36:27 +09003479 * 0 if @link is ready before @deadline; otherwise, -errno.
Tejun Heoaa2731a2008-04-07 22:47:19 +09003480 */
Harvey Harrison2b4221b2008-04-24 18:37:34 -07003481int ata_wait_after_reset(struct ata_link *link, unsigned long deadline,
Tejun Heoaa2731a2008-04-07 22:47:19 +09003482 int (*check_ready)(struct ata_link *link))
3483{
Tejun Heo97750ce2010-09-06 17:56:29 +02003484 ata_msleep(link->ap, ATA_WAIT_AFTER_RESET);
Tejun Heoaa2731a2008-04-07 22:47:19 +09003485
3486 return ata_wait_ready(link, deadline, check_ready);
3487}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01003488EXPORT_SYMBOL_GPL(ata_wait_after_reset);
Tejun Heoaa2731a2008-04-07 22:47:19 +09003489
3490/**
Tejun Heo0aa11132008-04-07 22:47:18 +09003491 * ata_std_prereset - prepare for reset
Tejun Heocc0680a2007-08-06 18:36:23 +09003492 * @link: ATA link to be reset
Tejun Heod4b2bab2007-02-02 16:50:52 +09003493 * @deadline: deadline jiffies for the operation
Tejun Heof5914a42006-05-31 18:27:48 +09003494 *
Tejun Heocc0680a2007-08-06 18:36:23 +09003495 * @link is about to be reset. Initialize it. Failure from
Tejun Heob8cffc62007-02-02 16:50:52 +09003496 * prereset makes libata abort whole reset sequence and give up
3497 * that port, so prereset should be best-effort. It does its
3498 * best to prepare for reset sequence but if things go wrong, it
3499 * should just whine, not fail.
Tejun Heof5914a42006-05-31 18:27:48 +09003500 *
3501 * LOCKING:
3502 * Kernel thread context (may sleep)
3503 *
3504 * RETURNS:
3505 * 0 on success, -errno otherwise.
3506 */
Tejun Heo0aa11132008-04-07 22:47:18 +09003507int ata_std_prereset(struct ata_link *link, unsigned long deadline)
Tejun Heof5914a42006-05-31 18:27:48 +09003508{
Tejun Heocc0680a2007-08-06 18:36:23 +09003509 struct ata_port *ap = link->ap;
Tejun Heo936fd732007-08-06 18:36:23 +09003510 struct ata_eh_context *ehc = &link->eh_context;
Tejun Heoe9c83912006-07-03 16:07:26 +09003511 const unsigned long *timing = sata_ehc_deb_timing(ehc);
Tejun Heof5914a42006-05-31 18:27:48 +09003512 int rc;
3513
Tejun Heof5914a42006-05-31 18:27:48 +09003514 /* if we're about to do hardreset, nothing more to do */
3515 if (ehc->i.action & ATA_EH_HARDRESET)
3516 return 0;
3517
Tejun Heo936fd732007-08-06 18:36:23 +09003518 /* if SATA, resume link */
Tejun Heoa16abc02007-05-21 18:33:47 +02003519 if (ap->flags & ATA_FLAG_SATA) {
Tejun Heo936fd732007-08-06 18:36:23 +09003520 rc = sata_link_resume(link, timing, deadline);
Tejun Heob8cffc62007-02-02 16:50:52 +09003521 /* whine about phy resume failure but proceed */
3522 if (rc && rc != -EOPNOTSUPP)
Joe Perchesa9a79df2011-04-15 15:51:59 -07003523 ata_link_warn(link,
3524 "failed to resume link for reset (errno=%d)\n",
3525 rc);
Tejun Heof5914a42006-05-31 18:27:48 +09003526 }
3527
Tejun Heo45db2f62008-04-08 01:46:56 +09003528 /* no point in trying softreset on offline link */
Tejun Heob1c72912008-07-31 17:02:43 +09003529 if (ata_phys_link_offline(link))
Tejun Heo45db2f62008-04-08 01:46:56 +09003530 ehc->i.action &= ~ATA_EH_SOFTRESET;
3531
Tejun Heof5914a42006-05-31 18:27:48 +09003532 return 0;
3533}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01003534EXPORT_SYMBOL_GPL(ata_std_prereset);
Tejun Heof5914a42006-05-31 18:27:48 +09003535
Tejun Heoc2bd5802006-01-24 17:05:22 +09003536/**
Tejun Heo57c9efd2008-04-07 22:47:19 +09003537 * sata_std_hardreset - COMRESET w/o waiting or classification
3538 * @link: link to reset
3539 * @class: resulting class of attached device
3540 * @deadline: deadline jiffies for the operation
3541 *
3542 * Standard SATA COMRESET w/o waiting or classification.
3543 *
3544 * LOCKING:
3545 * Kernel thread context (may sleep)
3546 *
3547 * RETURNS:
3548 * 0 if link offline, -EAGAIN if link online, -errno on errors.
3549 */
3550int sata_std_hardreset(struct ata_link *link, unsigned int *class,
3551 unsigned long deadline)
3552{
3553 const unsigned long *timing = sata_ehc_deb_timing(&link->eh_context);
3554 bool online;
3555 int rc;
3556
3557 /* do hardreset */
3558 rc = sata_link_hardreset(link, timing, deadline, &online, NULL);
Tejun Heo57c9efd2008-04-07 22:47:19 +09003559 return online ? -EAGAIN : rc;
3560}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01003561EXPORT_SYMBOL_GPL(sata_std_hardreset);
Tejun Heo57c9efd2008-04-07 22:47:19 +09003562
3563/**
Tejun Heo203c75b2008-04-07 22:47:18 +09003564 * ata_std_postreset - standard postreset callback
Tejun Heocc0680a2007-08-06 18:36:23 +09003565 * @link: the target ata_link
Tejun Heoc2bd5802006-01-24 17:05:22 +09003566 * @classes: classes of attached devices
3567 *
3568 * This function is invoked after a successful reset. Note that
3569 * the device might have been reset more than once using
3570 * different reset methods before postreset is invoked.
Tejun Heoc2bd5802006-01-24 17:05:22 +09003571 *
Tejun Heoc2bd5802006-01-24 17:05:22 +09003572 * LOCKING:
3573 * Kernel thread context (may sleep)
3574 */
Tejun Heo203c75b2008-04-07 22:47:18 +09003575void ata_std_postreset(struct ata_link *link, unsigned int *classes)
Tejun Heoc2bd5802006-01-24 17:05:22 +09003576{
Tejun Heof0465192008-05-19 01:15:08 +09003577 u32 serror;
3578
Tejun Heoc2bd5802006-01-24 17:05:22 +09003579 DPRINTK("ENTER\n");
3580
Tejun Heof0465192008-05-19 01:15:08 +09003581 /* reset complete, clear SError */
3582 if (!sata_scr_read(link, SCR_ERROR, &serror))
3583 sata_scr_write(link, SCR_ERROR, serror);
3584
Tejun Heoc2bd5802006-01-24 17:05:22 +09003585 /* print link status */
Tejun Heo936fd732007-08-06 18:36:23 +09003586 sata_print_link_status(link);
Tejun Heoc2bd5802006-01-24 17:05:22 +09003587
Tejun Heoc2bd5802006-01-24 17:05:22 +09003588 DPRINTK("EXIT\n");
3589}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01003590EXPORT_SYMBOL_GPL(ata_std_postreset);
Tejun Heoc2bd5802006-01-24 17:05:22 +09003591
Tejun Heoa62c0fc2006-01-24 17:05:22 +09003592/**
Tejun Heo623a3122006-03-05 17:55:58 +09003593 * ata_dev_same_device - Determine whether new ID matches configured device
Tejun Heo623a3122006-03-05 17:55:58 +09003594 * @dev: device to compare against
3595 * @new_class: class of the new device
3596 * @new_id: IDENTIFY page of the new device
3597 *
3598 * Compare @new_class and @new_id against @dev and determine
3599 * whether @dev is the device indicated by @new_class and
3600 * @new_id.
3601 *
3602 * LOCKING:
3603 * None.
3604 *
3605 * RETURNS:
3606 * 1 if @dev matches @new_class and @new_id, 0 otherwise.
3607 */
Tejun Heo3373efd2006-05-15 20:57:53 +09003608static int ata_dev_same_device(struct ata_device *dev, unsigned int new_class,
3609 const u16 *new_id)
Tejun Heo623a3122006-03-05 17:55:58 +09003610{
3611 const u16 *old_id = dev->id;
Tejun Heoa0cf7332007-01-02 20:18:49 +09003612 unsigned char model[2][ATA_ID_PROD_LEN + 1];
3613 unsigned char serial[2][ATA_ID_SERNO_LEN + 1];
Tejun Heo623a3122006-03-05 17:55:58 +09003614
3615 if (dev->class != new_class) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07003616 ata_dev_info(dev, "class mismatch %d != %d\n",
3617 dev->class, new_class);
Tejun Heo623a3122006-03-05 17:55:58 +09003618 return 0;
3619 }
3620
Tejun Heoa0cf7332007-01-02 20:18:49 +09003621 ata_id_c_string(old_id, model[0], ATA_ID_PROD, sizeof(model[0]));
3622 ata_id_c_string(new_id, model[1], ATA_ID_PROD, sizeof(model[1]));
3623 ata_id_c_string(old_id, serial[0], ATA_ID_SERNO, sizeof(serial[0]));
3624 ata_id_c_string(new_id, serial[1], ATA_ID_SERNO, sizeof(serial[1]));
Tejun Heo623a3122006-03-05 17:55:58 +09003625
3626 if (strcmp(model[0], model[1])) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07003627 ata_dev_info(dev, "model number mismatch '%s' != '%s'\n",
3628 model[0], model[1]);
Tejun Heo623a3122006-03-05 17:55:58 +09003629 return 0;
3630 }
3631
3632 if (strcmp(serial[0], serial[1])) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07003633 ata_dev_info(dev, "serial number mismatch '%s' != '%s'\n",
3634 serial[0], serial[1]);
Tejun Heo623a3122006-03-05 17:55:58 +09003635 return 0;
3636 }
3637
Tejun Heo623a3122006-03-05 17:55:58 +09003638 return 1;
3639}
3640
3641/**
Tejun Heofe309112007-05-15 03:28:15 +09003642 * ata_dev_reread_id - Re-read IDENTIFY data
Henrik Kretzschmar3fae4502007-06-19 10:10:50 +02003643 * @dev: target ATA device
Tejun Heobff04642006-11-10 18:08:10 +09003644 * @readid_flags: read ID flags
Tejun Heo623a3122006-03-05 17:55:58 +09003645 *
3646 * Re-read IDENTIFY page and make sure @dev is still attached to
3647 * the port.
3648 *
3649 * LOCKING:
3650 * Kernel thread context (may sleep)
3651 *
3652 * RETURNS:
3653 * 0 on success, negative errno otherwise
3654 */
Tejun Heofe309112007-05-15 03:28:15 +09003655int ata_dev_reread_id(struct ata_device *dev, unsigned int readid_flags)
Tejun Heo623a3122006-03-05 17:55:58 +09003656{
Tejun Heo5eb45c02006-04-02 18:51:52 +09003657 unsigned int class = dev->class;
Tejun Heo9af5c9c2007-08-06 18:36:22 +09003658 u16 *id = (void *)dev->link->ap->sector_buf;
Tejun Heo623a3122006-03-05 17:55:58 +09003659 int rc;
3660
Tejun Heofe635c72006-05-15 20:57:35 +09003661 /* read ID data */
Tejun Heobff04642006-11-10 18:08:10 +09003662 rc = ata_dev_read_id(dev, &class, readid_flags, id);
Tejun Heo623a3122006-03-05 17:55:58 +09003663 if (rc)
Tejun Heofe309112007-05-15 03:28:15 +09003664 return rc;
Tejun Heo623a3122006-03-05 17:55:58 +09003665
3666 /* is the device still there? */
Tejun Heofe309112007-05-15 03:28:15 +09003667 if (!ata_dev_same_device(dev, class, id))
3668 return -ENODEV;
Tejun Heo623a3122006-03-05 17:55:58 +09003669
Tejun Heofe635c72006-05-15 20:57:35 +09003670 memcpy(dev->id, id, sizeof(id[0]) * ATA_ID_WORDS);
Tejun Heofe309112007-05-15 03:28:15 +09003671 return 0;
3672}
3673
3674/**
3675 * ata_dev_revalidate - Revalidate ATA device
3676 * @dev: device to revalidate
Tejun Heo422c9da2007-09-23 13:14:12 +09003677 * @new_class: new class code
Tejun Heofe309112007-05-15 03:28:15 +09003678 * @readid_flags: read ID flags
3679 *
3680 * Re-read IDENTIFY page, make sure @dev is still attached to the
3681 * port and reconfigure it according to the new IDENTIFY page.
3682 *
3683 * LOCKING:
3684 * Kernel thread context (may sleep)
3685 *
3686 * RETURNS:
3687 * 0 on success, negative errno otherwise
3688 */
Tejun Heo422c9da2007-09-23 13:14:12 +09003689int ata_dev_revalidate(struct ata_device *dev, unsigned int new_class,
3690 unsigned int readid_flags)
Tejun Heofe309112007-05-15 03:28:15 +09003691{
Tejun Heo6ddcd3b2007-05-15 03:28:15 +09003692 u64 n_sectors = dev->n_sectors;
Tejun Heo5920dad2009-07-15 17:11:41 +09003693 u64 n_native_sectors = dev->n_native_sectors;
Tejun Heofe309112007-05-15 03:28:15 +09003694 int rc;
3695
3696 if (!ata_dev_enabled(dev))
3697 return -ENODEV;
3698
Tejun Heo422c9da2007-09-23 13:14:12 +09003699 /* fail early if !ATA && !ATAPI to avoid issuing [P]IDENTIFY to PMP */
3700 if (ata_class_enabled(new_class) &&
Borislav Petkovf0d06132009-04-26 16:33:34 +02003701 new_class != ATA_DEV_ATA &&
3702 new_class != ATA_DEV_ATAPI &&
Hannes Reinecke9162c652014-11-05 13:08:21 +01003703 new_class != ATA_DEV_ZAC &&
Borislav Petkovf0d06132009-04-26 16:33:34 +02003704 new_class != ATA_DEV_SEMB) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07003705 ata_dev_info(dev, "class mismatch %u != %u\n",
3706 dev->class, new_class);
Tejun Heo422c9da2007-09-23 13:14:12 +09003707 rc = -ENODEV;
3708 goto fail;
3709 }
3710
Tejun Heofe309112007-05-15 03:28:15 +09003711 /* re-read ID */
3712 rc = ata_dev_reread_id(dev, readid_flags);
3713 if (rc)
3714 goto fail;
Tejun Heo623a3122006-03-05 17:55:58 +09003715
3716 /* configure device according to the new ID */
Tejun Heoefdaedc2006-11-01 18:38:52 +09003717 rc = ata_dev_configure(dev);
Tejun Heo6ddcd3b2007-05-15 03:28:15 +09003718 if (rc)
3719 goto fail;
3720
3721 /* verify n_sectors hasn't changed */
Tejun Heo445d2112010-04-05 10:33:13 +09003722 if (dev->class != ATA_DEV_ATA || !n_sectors ||
3723 dev->n_sectors == n_sectors)
3724 return 0;
3725
3726 /* n_sectors has changed */
Joe Perchesa9a79df2011-04-15 15:51:59 -07003727 ata_dev_warn(dev, "n_sectors mismatch %llu != %llu\n",
3728 (unsigned long long)n_sectors,
3729 (unsigned long long)dev->n_sectors);
Tejun Heo445d2112010-04-05 10:33:13 +09003730
3731 /*
3732 * Something could have caused HPA to be unlocked
3733 * involuntarily. If n_native_sectors hasn't changed and the
3734 * new size matches it, keep the device.
3735 */
3736 if (dev->n_native_sectors == n_native_sectors &&
3737 dev->n_sectors > n_sectors && dev->n_sectors == n_native_sectors) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07003738 ata_dev_warn(dev,
3739 "new n_sectors matches native, probably "
3740 "late HPA unlock, n_sectors updated\n");
Tejun Heo68939ce2010-05-15 20:09:33 +02003741 /* use the larger n_sectors */
Tejun Heo445d2112010-04-05 10:33:13 +09003742 return 0;
Tejun Heo6ddcd3b2007-05-15 03:28:15 +09003743 }
3744
Tejun Heo445d2112010-04-05 10:33:13 +09003745 /*
3746 * Some BIOSes boot w/o HPA but resume w/ HPA locked. Try
3747 * unlocking HPA in those cases.
3748 *
3749 * https://bugzilla.kernel.org/show_bug.cgi?id=15396
3750 */
3751 if (dev->n_native_sectors == n_native_sectors &&
3752 dev->n_sectors < n_sectors && n_sectors == n_native_sectors &&
3753 !(dev->horkage & ATA_HORKAGE_BROKEN_HPA)) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07003754 ata_dev_warn(dev,
3755 "old n_sectors matches native, probably "
3756 "late HPA lock, will try to unlock HPA\n");
Tejun Heo445d2112010-04-05 10:33:13 +09003757 /* try unlocking HPA */
3758 dev->flags |= ATA_DFLAG_UNLOCK_HPA;
3759 rc = -EIO;
3760 } else
3761 rc = -ENODEV;
Tejun Heo623a3122006-03-05 17:55:58 +09003762
Tejun Heo445d2112010-04-05 10:33:13 +09003763 /* restore original n_[native_]sectors and fail */
3764 dev->n_native_sectors = n_native_sectors;
3765 dev->n_sectors = n_sectors;
Tejun Heo623a3122006-03-05 17:55:58 +09003766 fail:
Joe Perchesa9a79df2011-04-15 15:51:59 -07003767 ata_dev_err(dev, "revalidation failed (errno=%d)\n", rc);
Tejun Heo623a3122006-03-05 17:55:58 +09003768 return rc;
3769}
3770
Alan Cox6919a0a2006-10-27 19:08:46 -07003771struct ata_blacklist_entry {
3772 const char *model_num;
3773 const char *model_rev;
3774 unsigned long horkage;
3775};
3776
3777static const struct ata_blacklist_entry ata_device_blacklist [] = {
3778 /* Devices with DMA related problems under Linux */
3779 { "WDC AC11000H", NULL, ATA_HORKAGE_NODMA },
3780 { "WDC AC22100H", NULL, ATA_HORKAGE_NODMA },
3781 { "WDC AC32500H", NULL, ATA_HORKAGE_NODMA },
3782 { "WDC AC33100H", NULL, ATA_HORKAGE_NODMA },
3783 { "WDC AC31600H", NULL, ATA_HORKAGE_NODMA },
3784 { "WDC AC32100H", "24.09P07", ATA_HORKAGE_NODMA },
3785 { "WDC AC23200L", "21.10N21", ATA_HORKAGE_NODMA },
3786 { "Compaq CRD-8241B", NULL, ATA_HORKAGE_NODMA },
3787 { "CRD-8400B", NULL, ATA_HORKAGE_NODMA },
Mark Lord7da4c932010-07-01 18:18:12 -04003788 { "CRD-848[02]B", NULL, ATA_HORKAGE_NODMA },
Alan Cox6919a0a2006-10-27 19:08:46 -07003789 { "CRD-84", NULL, ATA_HORKAGE_NODMA },
3790 { "SanDisk SDP3B", NULL, ATA_HORKAGE_NODMA },
3791 { "SanDisk SDP3B-64", NULL, ATA_HORKAGE_NODMA },
3792 { "SANYO CD-ROM CRD", NULL, ATA_HORKAGE_NODMA },
3793 { "HITACHI CDR-8", NULL, ATA_HORKAGE_NODMA },
Mark Lord7da4c932010-07-01 18:18:12 -04003794 { "HITACHI CDR-8[34]35",NULL, ATA_HORKAGE_NODMA },
Alan Cox6919a0a2006-10-27 19:08:46 -07003795 { "Toshiba CD-ROM XM-6202B", NULL, ATA_HORKAGE_NODMA },
3796 { "TOSHIBA CD-ROM XM-1702BC", NULL, ATA_HORKAGE_NODMA },
3797 { "CD-532E-A", NULL, ATA_HORKAGE_NODMA },
3798 { "E-IDE CD-ROM CR-840",NULL, ATA_HORKAGE_NODMA },
3799 { "CD-ROM Drive/F5A", NULL, ATA_HORKAGE_NODMA },
3800 { "WPI CDD-820", NULL, ATA_HORKAGE_NODMA },
3801 { "SAMSUNG CD-ROM SC-148C", NULL, ATA_HORKAGE_NODMA },
3802 { "SAMSUNG CD-ROM SC", NULL, ATA_HORKAGE_NODMA },
Alan Cox6919a0a2006-10-27 19:08:46 -07003803 { "ATAPI CD-ROM DRIVE 40X MAXIMUM",NULL,ATA_HORKAGE_NODMA },
3804 { "_NEC DV5800A", NULL, ATA_HORKAGE_NODMA },
Jeff Garzik2dcb4072007-10-19 06:42:56 -04003805 { "SAMSUNG CD-ROM SN-124", "N001", ATA_HORKAGE_NODMA },
Dave Jones39f19882007-05-21 14:31:03 -04003806 { "Seagate STT20000A", NULL, ATA_HORKAGE_NODMA },
Prarit Bhargavad17d7942012-08-23 15:11:52 -04003807 { " 2GB ATA Flash Disk", "ADMA428M", ATA_HORKAGE_NODMA },
Shikha Jainb00622f2016-01-11 14:55:45 -08003808 { "VRFDFC22048UCHC-TE*", NULL, ATA_HORKAGE_NODMA },
Tejun Heo3af9a772007-09-23 13:19:54 +09003809 /* Odd clown on sil3726/4726 PMPs */
Tejun Heo50af2fa2008-05-19 01:15:14 +09003810 { "Config Disk", NULL, ATA_HORKAGE_DISABLE },
Alan Cox6919a0a2006-10-27 19:08:46 -07003811
Albert Lee18d6e9d2007-04-02 11:34:15 +08003812 /* Weird ATAPI devices */
Tejun Heo40a1d532007-06-27 02:49:38 +09003813 { "TORiSAN DVD-ROM DRD-N216", NULL, ATA_HORKAGE_MAX_SEC_128 },
Tejun Heo6a87e422008-11-03 19:01:09 +09003814 { "QUANTUM DAT DAT72-000", NULL, ATA_HORKAGE_ATAPI_MOD16_DMA },
Shan Haia32450e2013-03-18 10:30:44 +08003815 { "Slimtype DVD A DS8A8SH", NULL, ATA_HORKAGE_MAX_SEC_LBA48 },
Shan Hai0523f032013-10-28 16:08:01 +08003816 { "Slimtype DVD A DS8A9SH", NULL, ATA_HORKAGE_MAX_SEC_LBA48 },
Albert Lee18d6e9d2007-04-02 11:34:15 +08003817
David Milburnaf34d632015-07-13 11:48:23 -05003818 /*
3819 * Causes silent data corruption with higher max sects.
3820 * http://lkml.kernel.org/g/x49wpy40ysk.fsf@segfault.boston.devel.redhat.com
3821 */
3822 { "ST380013AS", "3.20", ATA_HORKAGE_MAX_SEC_1024 },
3823
Tejun Heo1488a1e2016-07-18 18:40:00 -04003824 /*
Tejun Heoe0edc8c2017-01-06 11:48:50 -05003825 * These devices time out with higher max sects.
Tejun Heo1488a1e2016-07-18 18:40:00 -04003826 * https://bugzilla.kernel.org/show_bug.cgi?id=121671
3827 */
Tejun Heoe0edc8c2017-01-06 11:48:50 -05003828 { "LITEON CX1-JB*-HP", NULL, ATA_HORKAGE_MAX_SEC_1024 },
Xinyu Lindb5ff902017-12-17 20:13:39 +08003829 { "LITEON EP1-*", NULL, ATA_HORKAGE_MAX_SEC_1024 },
Tejun Heo1488a1e2016-07-18 18:40:00 -04003830
Alan Cox6919a0a2006-10-27 19:08:46 -07003831 /* Devices we expect to fail diagnostics */
3832
3833 /* Devices where NCQ should be avoided */
3834 /* NCQ is slow */
Jeff Garzik2dcb4072007-10-19 06:42:56 -04003835 { "WDC WD740ADFD-00", NULL, ATA_HORKAGE_NONCQ },
Tejun Heo459ad682007-12-07 12:46:23 +09003836 { "WDC WD740ADFD-00NLR1", NULL, ATA_HORKAGE_NONCQ, },
Tejun Heo09125ea2007-02-28 15:21:23 +09003837 /* http://thread.gmane.org/gmane.linux.ide/14907 */
3838 { "FUJITSU MHT2060BH", NULL, ATA_HORKAGE_NONCQ },
Paul Rolland7acfaf32007-03-26 21:43:44 -08003839 /* NCQ is broken */
Jeff Garzik539cc7c2007-09-20 16:31:47 -04003840 { "Maxtor *", "BANC*", ATA_HORKAGE_NONCQ },
Alan Cox0e3dbc02007-09-20 15:22:47 +01003841 { "Maxtor 7V300F0", "VA111630", ATA_HORKAGE_NONCQ },
Paolo Ornatida6f0ec2007-10-04 11:06:56 +09003842 { "ST380817AS", "3.42", ATA_HORKAGE_NONCQ },
Tejun Heoe41bd3e2007-12-09 19:45:39 +09003843 { "ST3160023AS", "3.42", ATA_HORKAGE_NONCQ },
Lubomir Bulej5ccfca92008-12-22 11:35:22 +01003844 { "OCZ CORE_SSD", "02.10104", ATA_HORKAGE_NONCQ },
Jeff Garzik539cc7c2007-09-20 16:31:47 -04003845
Tejun Heoac70a962008-11-27 13:36:48 +09003846 /* Seagate NCQ + FLUSH CACHE firmware bug */
Mark Lord4d1f9082010-07-05 18:53:37 -04003847 { "ST31500341AS", "SD1[5-9]", ATA_HORKAGE_NONCQ |
Tejun Heoac70a962008-11-27 13:36:48 +09003848 ATA_HORKAGE_FIRMWARE_WARN },
Tejun Heod10d4912008-12-11 13:42:42 +09003849
Mark Lord4d1f9082010-07-05 18:53:37 -04003850 { "ST31000333AS", "SD1[5-9]", ATA_HORKAGE_NONCQ |
Tejun Heod10d4912008-12-11 13:42:42 +09003851 ATA_HORKAGE_FIRMWARE_WARN },
3852
Mark Lord4d1f9082010-07-05 18:53:37 -04003853 { "ST3640[36]23AS", "SD1[5-9]", ATA_HORKAGE_NONCQ |
Tejun Heod10d4912008-12-11 13:42:42 +09003854 ATA_HORKAGE_FIRMWARE_WARN },
3855
Mark Lord4d1f9082010-07-05 18:53:37 -04003856 { "ST3320[68]13AS", "SD1[5-9]", ATA_HORKAGE_NONCQ |
Tejun Heoac70a962008-11-27 13:36:48 +09003857 ATA_HORKAGE_FIRMWARE_WARN },
3858
Hans de Goede31f62642019-06-11 16:32:59 +02003859 /* drives which fail FPDMA_AA activation (some may freeze afterwards)
3860 the ST disks also have LPM issues */
Hans de Goede8756a252019-07-01 18:58:13 +02003861 { "ST1000LM024 HN-M101MBB", NULL, ATA_HORKAGE_BROKEN_FPDMA_AA |
Hans de Goede31f62642019-06-11 16:32:59 +02003862 ATA_HORKAGE_NOLPM, },
Aleksei Mamlin08c85d22015-07-01 13:48:30 +03003863 { "VB0250EAVER", "HPG7", ATA_HORKAGE_BROKEN_FPDMA_AA },
Michele Baldessari87809942013-11-25 19:00:14 +00003864
Robert Hancock36e337d2007-04-02 22:05:29 -06003865 /* Blacklist entries taken from Silicon Image 3124/3132
3866 Windows driver .inf file - also several Linux problem reports */
3867 { "HTS541060G9SA00", "MB3OC60D", ATA_HORKAGE_NONCQ, },
3868 { "HTS541080G9SA00", "MB4OC60D", ATA_HORKAGE_NONCQ, },
3869 { "HTS541010G9SA00", "MBZOC60D", ATA_HORKAGE_NONCQ, },
Alan Cox6919a0a2006-10-27 19:08:46 -07003870
Tejun Heo68b0ddb2010-04-05 10:51:26 +09003871 /* https://bugzilla.kernel.org/show_bug.cgi?id=15573 */
3872 { "C300-CTFDDAC128MAG", "0001", ATA_HORKAGE_NONCQ, },
3873
Tejun Heo3b545562020-09-02 12:32:45 -04003874 /* Sandisk SD7/8/9s lock up hard on large trims */
3875 { "SanDisk SD[789]*", NULL, ATA_HORKAGE_MAX_TRIM_128M, },
Tejun Heo322579d2018-05-08 14:21:56 -07003876
Tejun Heo16c55b02007-08-29 11:58:33 +09003877 /* devices which puke on READ_NATIVE_MAX */
3878 { "HDS724040KLSA80", "KFAOA20N", ATA_HORKAGE_BROKEN_HPA, },
3879 { "WDC WD3200JD-00KLB0", "WD-WCAMR1130137", ATA_HORKAGE_BROKEN_HPA },
3880 { "WDC WD2500JD-00HBB0", "WD-WMAL71490727", ATA_HORKAGE_BROKEN_HPA },
3881 { "MAXTOR 6L080L4", "A93.0500", ATA_HORKAGE_BROKEN_HPA },
Alan Cox6919a0a2006-10-27 19:08:46 -07003882
Tejun Heo7831387b2009-08-07 01:59:15 +09003883 /* this one allows HPA unlocking but fails IOs on the area */
3884 { "OCZ-VERTEX", "1.30", ATA_HORKAGE_BROKEN_HPA },
3885
Alan Cox93328e112007-09-29 04:06:48 -04003886 /* Devices which report 1 sector over size HPA */
3887 { "ST340823A", NULL, ATA_HORKAGE_HPA_SIZE, },
3888 { "ST320413A", NULL, ATA_HORKAGE_HPA_SIZE, },
Mikko Rapelib152fcd2008-02-19 01:41:25 +01003889 { "ST310211A", NULL, ATA_HORKAGE_HPA_SIZE, },
Alan Cox93328e112007-09-29 04:06:48 -04003890
Alan Cox6bbfd532007-11-05 22:58:58 +00003891 /* Devices which get the IVB wrong */
3892 { "QUANTUM FIREBALLlct10 05", "A03.0900", ATA_HORKAGE_IVB, },
Alan Coxa79067e2008-04-29 14:08:36 +01003893 /* Maybe we should just blacklist TSSTcorp... */
Mark Lord7da4c932010-07-01 18:18:12 -04003894 { "TSSTcorp CDDVDW SH-S202[HJN]", "SB0[01]", ATA_HORKAGE_IVB, },
Alan Cox6bbfd532007-11-05 22:58:58 +00003895
Jens Axboe9ce8e302008-08-27 15:23:18 +02003896 /* Devices that do not need bridging limits applied */
3897 { "MTRON MSP-SATA*", NULL, ATA_HORKAGE_BRIDGE_OK, },
Jeff Garzik04d0f1b82012-08-17 13:36:59 -04003898 { "BUFFALO HD-QSU2/R5", NULL, ATA_HORKAGE_BRIDGE_OK, },
Jens Axboe9ce8e302008-08-27 15:23:18 +02003899
Tejun Heo90627122009-01-29 20:31:36 +09003900 /* Devices which aren't very happy with higher link speeds */
3901 { "WD My Book", NULL, ATA_HORKAGE_1_5_GBPS, },
Daniel J Bluemanc5310772012-07-23 12:22:37 +08003902 { "Seagate FreeAgent GoFlex", NULL, ATA_HORKAGE_1_5_GBPS, },
Tejun Heo90627122009-01-29 20:31:36 +09003903
Tejun Heod0cb43b2009-07-09 09:27:50 +09003904 /*
3905 * Devices which choke on SETXFER. Applies only if both the
3906 * device and controller are SATA.
3907 */
Tejun Heocd691872011-06-22 12:13:10 +02003908 { "PIONEER DVD-RW DVRTD08", NULL, ATA_HORKAGE_NOSETXFER },
Vladimir LAVALLADE3a251792012-01-08 13:50:13 +01003909 { "PIONEER DVD-RW DVRTD08A", NULL, ATA_HORKAGE_NOSETXFER },
3910 { "PIONEER DVD-RW DVR-215", NULL, ATA_HORKAGE_NOSETXFER },
Tejun Heocd691872011-06-22 12:13:10 +02003911 { "PIONEER DVD-RW DVR-212D", NULL, ATA_HORKAGE_NOSETXFER },
3912 { "PIONEER DVD-RW DVR-216D", NULL, ATA_HORKAGE_NOSETXFER },
Tejun Heod0cb43b2009-07-09 09:27:50 +09003913
Kai-Heng Fengb17e57292018-02-18 22:17:09 +08003914 /* Crucial BX100 SSD 500GB has broken LPM support */
Hans de Goede3bf7b5d2018-03-19 16:33:59 +01003915 { "CT500BX100SSD1", NULL, ATA_HORKAGE_NOLPM },
Kai-Heng Fengb17e57292018-02-18 22:17:09 +08003916
Hans de Goeded418ff52018-03-19 16:34:00 +01003917 /* 512GB MX100 with MU01 firmware has both queued TRIM and LPM issues */
3918 { "Crucial_CT512MX100*", "MU01", ATA_HORKAGE_NO_NCQ_TRIM |
Hans de Goede9c7be59f2018-02-16 10:48:20 +01003919 ATA_HORKAGE_ZERO_AFTER_TRIM |
3920 ATA_HORKAGE_NOLPM, },
Hans de Goeded418ff52018-03-19 16:34:00 +01003921 /* 512GB MX100 with newer firmware has only LPM issues */
3922 { "Crucial_CT512MX100*", NULL, ATA_HORKAGE_ZERO_AFTER_TRIM |
3923 ATA_HORKAGE_NOLPM, },
Hans de Goede9c7be59f2018-02-16 10:48:20 +01003924
Hans de Goede62ac3f72018-03-19 16:33:58 +01003925 /* 480GB+ M500 SSDs have both queued TRIM and LPM issues */
3926 { "Crucial_CT480M500*", NULL, ATA_HORKAGE_NO_NCQ_TRIM |
3927 ATA_HORKAGE_ZERO_AFTER_TRIM |
3928 ATA_HORKAGE_NOLPM, },
3929 { "Crucial_CT960M500*", NULL, ATA_HORKAGE_NO_NCQ_TRIM |
3930 ATA_HORKAGE_ZERO_AFTER_TRIM |
3931 ATA_HORKAGE_NOLPM, },
3932
François Cami76936e92018-05-13 20:11:15 +02003933 /* These specific Samsung models/firmware-revs do not handle LPM well */
Hans de Goedeb5b4d3a2018-04-24 11:19:07 +02003934 { "SAMSUNG MZMPC128HBFU-000MV", "CXM14M1Q", ATA_HORKAGE_NOLPM, },
François Cami76936e92018-05-13 20:11:15 +02003935 { "SAMSUNG SSD PM830 mSATA *", "CXM13D1Q", ATA_HORKAGE_NOLPM, },
Diego Viola410b5c72018-11-12 17:22:52 -02003936 { "SAMSUNG MZ7TD256HAFV-000L9", NULL, ATA_HORKAGE_NOLPM, },
Hans de Goededd957492019-02-03 10:02:07 +01003937 { "SAMSUNG MZ7TE512HMHP-000L1", "EXT06L0Q", ATA_HORKAGE_NOLPM, },
Hans de Goedeb5b4d3a2018-04-24 11:19:07 +02003938
Marc Carinof78dea02013-12-16 18:15:53 -08003939 /* devices that don't properly handle queued TRIM commands */
Sudip Mukherjee136d7692018-05-19 22:29:36 +01003940 { "Micron_M500IT_*", "MU01", ATA_HORKAGE_NO_NCQ_TRIM |
3941 ATA_HORKAGE_ZERO_AFTER_TRIM, },
Martin K. Petersen243918b2015-06-18 14:50:18 -04003942 { "Micron_M500_*", NULL, ATA_HORKAGE_NO_NCQ_TRIM |
Martin K. Petersene61f7d12015-01-08 10:34:27 -05003943 ATA_HORKAGE_ZERO_AFTER_TRIM, },
Martin K. Petersenff7f53f2015-03-27 15:17:20 -04003944 { "Crucial_CT*M500*", NULL, ATA_HORKAGE_NO_NCQ_TRIM |
3945 ATA_HORKAGE_ZERO_AFTER_TRIM, },
Martin K. Petersen9051bd32015-07-15 21:03:23 -04003946 { "Micron_M5[15]0_*", "MU01", ATA_HORKAGE_NO_NCQ_TRIM |
Martin K. Petersenff7f53f2015-03-27 15:17:20 -04003947 ATA_HORKAGE_ZERO_AFTER_TRIM, },
3948 { "Crucial_CT*M550*", "MU01", ATA_HORKAGE_NO_NCQ_TRIM |
3949 ATA_HORKAGE_ZERO_AFTER_TRIM, },
3950 { "Crucial_CT*MX100*", "MU01", ATA_HORKAGE_NO_NCQ_TRIM |
3951 ATA_HORKAGE_ZERO_AFTER_TRIM, },
Ju Hyung Parkca6bfcb2018-03-11 02:28:35 +09003952 { "Samsung SSD 840*", NULL, ATA_HORKAGE_NO_NCQ_TRIM |
3953 ATA_HORKAGE_ZERO_AFTER_TRIM, },
3954 { "Samsung SSD 850*", NULL, ATA_HORKAGE_NO_NCQ_TRIM |
Martin K. Petersen6fc4d972015-03-27 15:17:21 -04003955 ATA_HORKAGE_ZERO_AFTER_TRIM, },
Guillermo A. Amaral7a7184b2015-08-25 23:29:13 -07003956 { "FCCT*M500*", NULL, ATA_HORKAGE_NO_NCQ_TRIM |
3957 ATA_HORKAGE_ZERO_AFTER_TRIM, },
Martin K. Petersene61f7d12015-01-08 10:34:27 -05003958
Arne Fitzenreitercda57b12015-07-15 13:54:37 +02003959 /* devices that don't properly handle TRIM commands */
3960 { "SuperSSpeed S238*", NULL, ATA_HORKAGE_NOTRIM, },
3961
Martin K. Petersene61f7d12015-01-08 10:34:27 -05003962 /*
3963 * As defined, the DRAT (Deterministic Read After Trim) and RZAT
3964 * (Return Zero After Trim) flags in the ATA Command Set are
3965 * unreliable in the sense that they only define what happens if
3966 * the device successfully executed the DSM TRIM command. TRIM
3967 * is only advisory, however, and the device is free to silently
3968 * ignore all or parts of the request.
3969 *
3970 * Whitelist drives that are known to reliably return zeroes
3971 * after TRIM.
3972 */
3973
3974 /*
3975 * The intel 510 drive has buggy DRAT/RZAT. Explicitly exclude
3976 * that model before whitelisting all other intel SSDs.
3977 */
3978 { "INTEL*SSDSC2MH*", NULL, 0, },
3979
Martin K. Petersenff7f53f2015-03-27 15:17:20 -04003980 { "Micron*", NULL, ATA_HORKAGE_ZERO_AFTER_TRIM, },
3981 { "Crucial*", NULL, ATA_HORKAGE_ZERO_AFTER_TRIM, },
Martin K. Petersene61f7d12015-01-08 10:34:27 -05003982 { "INTEL*SSD*", NULL, ATA_HORKAGE_ZERO_AFTER_TRIM, },
3983 { "SSD*INTEL*", NULL, ATA_HORKAGE_ZERO_AFTER_TRIM, },
3984 { "Samsung*SSD*", NULL, ATA_HORKAGE_ZERO_AFTER_TRIM, },
3985 { "SAMSUNG*SSD*", NULL, ATA_HORKAGE_ZERO_AFTER_TRIM, },
Juha-Matti Tillifd6f32f2018-12-02 12:47:08 +02003986 { "SAMSUNG*MZ7KM*", NULL, ATA_HORKAGE_ZERO_AFTER_TRIM, },
Martin K. Petersene61f7d12015-01-08 10:34:27 -05003987 { "ST[1248][0248]0[FH]*", NULL, ATA_HORKAGE_ZERO_AFTER_TRIM, },
Marc Carinof78dea02013-12-16 18:15:53 -08003988
Tejun Heoecd75ad2014-01-16 09:47:17 -05003989 /*
3990 * Some WD SATA-I drives spin up and down erratically when the link
3991 * is put into the slumber mode. We don't have full list of the
3992 * affected devices. Disable LPM if the device matches one of the
3993 * known prefixes and is SATA-1. As a side effect LPM partial is
3994 * lost too.
3995 *
3996 * https://bugzilla.kernel.org/show_bug.cgi?id=57211
3997 */
3998 { "WDC WD800JD-*", NULL, ATA_HORKAGE_WD_BROKEN_LPM },
3999 { "WDC WD1200JD-*", NULL, ATA_HORKAGE_WD_BROKEN_LPM },
4000 { "WDC WD1600JD-*", NULL, ATA_HORKAGE_WD_BROKEN_LPM },
4001 { "WDC WD2000JD-*", NULL, ATA_HORKAGE_WD_BROKEN_LPM },
4002 { "WDC WD2500JD-*", NULL, ATA_HORKAGE_WD_BROKEN_LPM },
4003 { "WDC WD3000JD-*", NULL, ATA_HORKAGE_WD_BROKEN_LPM },
4004 { "WDC WD3200JD-*", NULL, ATA_HORKAGE_WD_BROKEN_LPM },
4005
Alan Cox6919a0a2006-10-27 19:08:46 -07004006 /* End Marker */
4007 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004008};
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05004009
Tejun Heo75683fe2007-07-05 13:31:27 +09004010static unsigned long ata_dev_blacklisted(const struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004011{
Tejun Heo8bfa79f2007-01-02 20:19:40 +09004012 unsigned char model_num[ATA_ID_PROD_LEN + 1];
4013 unsigned char model_rev[ATA_ID_FW_REV_LEN + 1];
Alan Cox6919a0a2006-10-27 19:08:46 -07004014 const struct ata_blacklist_entry *ad = ata_device_blacklist;
Albert Lee3a778272006-06-22 13:00:25 +08004015
Tejun Heo8bfa79f2007-01-02 20:19:40 +09004016 ata_id_c_string(dev->id, model_num, ATA_ID_PROD, sizeof(model_num));
4017 ata_id_c_string(dev->id, model_rev, ATA_ID_FW_REV, sizeof(model_rev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004018
Alan Cox6919a0a2006-10-27 19:08:46 -07004019 while (ad->model_num) {
George Spelvin1c402792014-10-07 07:26:38 -04004020 if (glob_match(ad->model_num, model_num)) {
Alan Cox6919a0a2006-10-27 19:08:46 -07004021 if (ad->model_rev == NULL)
4022 return ad->horkage;
George Spelvin1c402792014-10-07 07:26:38 -04004023 if (glob_match(ad->model_rev, model_rev))
Alan Cox6919a0a2006-10-27 19:08:46 -07004024 return ad->horkage;
Alan Coxf4b15fe2006-03-22 15:54:04 +00004025 }
Alan Cox6919a0a2006-10-27 19:08:46 -07004026 ad++;
Alan Coxf4b15fe2006-03-22 15:54:04 +00004027 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004028 return 0;
4029}
4030
Alan Cox6919a0a2006-10-27 19:08:46 -07004031static int ata_dma_blacklisted(const struct ata_device *dev)
4032{
4033 /* We don't support polling DMA.
4034 * DMA blacklist those ATAPI devices with CDB-intr (and use PIO)
4035 * if the LLDD handles only interrupts in the HSM_ST_LAST state.
4036 */
Tejun Heo9af5c9c2007-08-06 18:36:22 +09004037 if ((dev->link->ap->flags & ATA_FLAG_PIO_POLLING) &&
Alan Cox6919a0a2006-10-27 19:08:46 -07004038 (dev->flags & ATA_DFLAG_CDB_INTR))
4039 return 1;
Tejun Heo75683fe2007-07-05 13:31:27 +09004040 return (dev->horkage & ATA_HORKAGE_NODMA) ? 1 : 0;
Alan Cox6919a0a2006-10-27 19:08:46 -07004041}
4042
Tejun Heoa6d5a512006-03-06 04:31:57 +09004043/**
Alan Cox6bbfd532007-11-05 22:58:58 +00004044 * ata_is_40wire - check drive side detection
4045 * @dev: device
4046 *
4047 * Perform drive side detection decoding, allowing for device vendors
4048 * who can't follow the documentation.
4049 */
4050
4051static int ata_is_40wire(struct ata_device *dev)
4052{
4053 if (dev->horkage & ATA_HORKAGE_IVB)
4054 return ata_drive_40wire_relaxed(dev->id);
4055 return ata_drive_40wire(dev->id);
4056}
4057
4058/**
Alan Cox15a55512008-03-28 14:33:46 -07004059 * cable_is_40wire - 40/80/SATA decider
4060 * @ap: port to consider
4061 *
4062 * This function encapsulates the policy for speed management
4063 * in one place. At the moment we don't cache the result but
4064 * there is a good case for setting ap->cbl to the result when
4065 * we are called with unknown cables (and figuring out if it
4066 * impacts hotplug at all).
4067 *
4068 * Return 1 if the cable appears to be 40 wire.
4069 */
4070
4071static int cable_is_40wire(struct ata_port *ap)
4072{
4073 struct ata_link *link;
4074 struct ata_device *dev;
4075
Tejun Heo4a9c7b32008-10-27 19:59:23 +09004076 /* If the controller thinks we are 40 wire, we are. */
Alan Cox15a55512008-03-28 14:33:46 -07004077 if (ap->cbl == ATA_CBL_PATA40)
4078 return 1;
Tejun Heo4a9c7b32008-10-27 19:59:23 +09004079
4080 /* If the controller thinks we are 80 wire, we are. */
Alan Cox15a55512008-03-28 14:33:46 -07004081 if (ap->cbl == ATA_CBL_PATA80 || ap->cbl == ATA_CBL_SATA)
4082 return 0;
Tejun Heo4a9c7b32008-10-27 19:59:23 +09004083
4084 /* If the system is known to be 40 wire short cable (eg
4085 * laptop), then we allow 80 wire modes even if the drive
4086 * isn't sure.
4087 */
Alan Coxf7920682008-04-08 16:37:50 +01004088 if (ap->cbl == ATA_CBL_PATA40_SHORT)
4089 return 0;
Alan Cox15a55512008-03-28 14:33:46 -07004090
Tejun Heo4a9c7b32008-10-27 19:59:23 +09004091 /* If the controller doesn't know, we scan.
4092 *
4093 * Note: We look for all 40 wire detects at this point. Any
4094 * 80 wire detect is taken to be 80 wire cable because
4095 * - in many setups only the one drive (slave if present) will
4096 * give a valid detect
4097 * - if you have a non detect capable drive you don't want it
4098 * to colour the choice
4099 */
Tejun Heo1eca4362008-11-03 20:03:17 +09004100 ata_for_each_link(link, ap, EDGE) {
4101 ata_for_each_dev(dev, link, ENABLED) {
4102 if (!ata_is_40wire(dev))
Alan Cox15a55512008-03-28 14:33:46 -07004103 return 0;
4104 }
4105 }
4106 return 1;
4107}
4108
4109/**
Tejun Heoa6d5a512006-03-06 04:31:57 +09004110 * ata_dev_xfermask - Compute supported xfermask of the given device
Tejun Heoa6d5a512006-03-06 04:31:57 +09004111 * @dev: Device to compute xfermask for
4112 *
Tejun Heoacf356b2006-03-24 14:07:50 +09004113 * Compute supported xfermask of @dev and store it in
4114 * dev->*_mask. This function is responsible for applying all
4115 * known limits including host controller limits, device
4116 * blacklist, etc...
Tejun Heoa6d5a512006-03-06 04:31:57 +09004117 *
4118 * LOCKING:
4119 * None.
Tejun Heoa6d5a512006-03-06 04:31:57 +09004120 */
Tejun Heo3373efd2006-05-15 20:57:53 +09004121static void ata_dev_xfermask(struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004122{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09004123 struct ata_link *link = dev->link;
4124 struct ata_port *ap = link->ap;
Jeff Garzikcca39742006-08-24 03:19:22 -04004125 struct ata_host *host = ap->host;
Tejun Heoa6d5a512006-03-06 04:31:57 +09004126 unsigned long xfer_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004127
Tejun Heo37deecb2006-08-10 16:59:07 +09004128 /* controller modes available */
Tejun Heo565083e2006-04-02 17:54:47 +09004129 xfer_mask = ata_pack_xfermask(ap->pio_mask,
4130 ap->mwdma_mask, ap->udma_mask);
4131
Robert Hancock8343f882007-03-06 02:37:51 -08004132 /* drive modes available */
Tejun Heo37deecb2006-08-10 16:59:07 +09004133 xfer_mask &= ata_pack_xfermask(dev->pio_mask,
4134 dev->mwdma_mask, dev->udma_mask);
4135 xfer_mask &= ata_id_xfermask(dev->id);
Tejun Heo565083e2006-04-02 17:54:47 +09004136
Alan Coxb352e572006-08-10 18:52:12 +01004137 /*
4138 * CFA Advanced TrueIDE timings are not allowed on a shared
4139 * cable
4140 */
4141 if (ata_dev_pair(dev)) {
4142 /* No PIO5 or PIO6 */
4143 xfer_mask &= ~(0x03 << (ATA_SHIFT_PIO + 5));
4144 /* No MWDMA3 or MWDMA 4 */
4145 xfer_mask &= ~(0x03 << (ATA_SHIFT_MWDMA + 3));
4146 }
4147
Tejun Heo37deecb2006-08-10 16:59:07 +09004148 if (ata_dma_blacklisted(dev)) {
4149 xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
Joe Perchesa9a79df2011-04-15 15:51:59 -07004150 ata_dev_warn(dev,
4151 "device is on DMA blacklist, disabling DMA\n");
Tejun Heo37deecb2006-08-10 16:59:07 +09004152 }
Tejun Heoa6d5a512006-03-06 04:31:57 +09004153
Petr Vandrovec14d66ab2007-03-08 10:12:12 +01004154 if ((host->flags & ATA_HOST_SIMPLEX) &&
Jeff Garzik2dcb4072007-10-19 06:42:56 -04004155 host->simplex_claimed && host->simplex_claimed != ap) {
Tejun Heo37deecb2006-08-10 16:59:07 +09004156 xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
Joe Perchesa9a79df2011-04-15 15:51:59 -07004157 ata_dev_warn(dev,
4158 "simplex DMA is claimed by other device, disabling DMA\n");
Alan Cox5444a6f2006-03-27 18:58:20 +01004159 }
Tejun Heo565083e2006-04-02 17:54:47 +09004160
Jeff Garzike4246752007-03-09 09:56:46 -05004161 if (ap->flags & ATA_FLAG_NO_IORDY)
4162 xfer_mask &= ata_pio_mask_no_iordy(dev);
4163
Alan Cox5444a6f2006-03-27 18:58:20 +01004164 if (ap->ops->mode_filter)
Alan Coxa76b62ca2007-03-09 09:34:07 -05004165 xfer_mask = ap->ops->mode_filter(dev, xfer_mask);
Alan Cox5444a6f2006-03-27 18:58:20 +01004166
Robert Hancock8343f882007-03-06 02:37:51 -08004167 /* Apply cable rule here. Don't apply it early because when
4168 * we handle hot plug the cable type can itself change.
4169 * Check this last so that we know if the transfer rate was
4170 * solely limited by the cable.
4171 * Unknown or 80 wire cables reported host side are checked
4172 * drive side as well. Cases where we know a 40wire cable
4173 * is used safely for 80 are not checked here.
4174 */
4175 if (xfer_mask & (0xF8 << ATA_SHIFT_UDMA))
4176 /* UDMA/44 or higher would be available */
Alan Cox15a55512008-03-28 14:33:46 -07004177 if (cable_is_40wire(ap)) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07004178 ata_dev_warn(dev,
4179 "limited to UDMA/33 due to 40-wire cable\n");
Robert Hancock8343f882007-03-06 02:37:51 -08004180 xfer_mask &= ~(0xF8 << ATA_SHIFT_UDMA);
4181 }
4182
Tejun Heo565083e2006-04-02 17:54:47 +09004183 ata_unpack_xfermask(xfer_mask, &dev->pio_mask,
4184 &dev->mwdma_mask, &dev->udma_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004185}
4186
Linus Torvalds1da177e2005-04-16 15:20:36 -07004187/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004188 * ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
Linus Torvalds1da177e2005-04-16 15:20:36 -07004189 * @dev: Device to which command will be sent
4190 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04004191 * Issue SET FEATURES - XFER MODE command to device @dev
4192 * on port @ap.
4193 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004194 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004195 * PCI/etc. bus probe sem.
Tejun Heo83206a22006-03-24 15:25:31 +09004196 *
4197 * RETURNS:
4198 * 0 on success, AC_ERR_* mask otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004199 */
4200
Tejun Heo3373efd2006-05-15 20:57:53 +09004201static unsigned int ata_dev_set_xfermode(struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004202{
Tejun Heoa0123702005-12-13 14:49:31 +09004203 struct ata_taskfile tf;
Tejun Heo83206a22006-03-24 15:25:31 +09004204 unsigned int err_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004205
4206 /* set up set-features taskfile */
4207 DPRINTK("set features - xfer mode\n");
4208
Tejun Heo464cf172007-05-27 15:10:40 +02004209 /* Some controllers and ATAPI devices show flaky interrupt
4210 * behavior after setting xfer mode. Use polling instead.
4211 */
Tejun Heo3373efd2006-05-15 20:57:53 +09004212 ata_tf_init(dev, &tf);
Tejun Heoa0123702005-12-13 14:49:31 +09004213 tf.command = ATA_CMD_SET_FEATURES;
4214 tf.feature = SETFEATURES_XFER;
Tejun Heo464cf172007-05-27 15:10:40 +02004215 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE | ATA_TFLAG_POLLING;
Tejun Heoa0123702005-12-13 14:49:31 +09004216 tf.protocol = ATA_PROT_NODATA;
Alan Coxb9f8ab22007-11-19 14:33:11 +00004217 /* If we are using IORDY we must send the mode setting command */
Jeff Garzik11b7bec2007-11-23 21:12:14 -05004218 if (ata_pio_need_iordy(dev))
4219 tf.nsect = dev->xfer_mode;
Alan Coxb9f8ab22007-11-19 14:33:11 +00004220 /* If the device has IORDY and the controller does not - turn it off */
4221 else if (ata_id_has_iordy(dev->id))
Jeff Garzik11b7bec2007-11-23 21:12:14 -05004222 tf.nsect = 0x01;
Alan Coxb9f8ab22007-11-19 14:33:11 +00004223 else /* In the ancient relic department - skip all of this */
4224 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004225
Mikulas Patockad531be22015-07-08 13:06:12 -04004226 /* On some disks, this command causes spin-up, so we need longer timeout */
4227 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 15000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004228
Tejun Heo83206a22006-03-24 15:25:31 +09004229 DPRINTK("EXIT, err_mask=%x\n", err_mask);
4230 return err_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004231}
Tejun Heo1152b262010-09-01 17:50:05 +02004232
Linus Torvalds1da177e2005-04-16 15:20:36 -07004233/**
Jeff Garzik218f3d32007-10-25 00:33:27 -04004234 * ata_dev_set_feature - Issue SET FEATURES - SATA FEATURES
Kristen Carlson Accardi9f45cbd2007-08-15 03:57:11 -04004235 * @dev: Device to which command will be sent
4236 * @enable: Whether to enable or disable the feature
Jeff Garzik218f3d32007-10-25 00:33:27 -04004237 * @feature: The sector count represents the feature to set
Kristen Carlson Accardi9f45cbd2007-08-15 03:57:11 -04004238 *
4239 * Issue SET FEATURES - SATA FEATURES command to device @dev
Jeff Garzik218f3d32007-10-25 00:33:27 -04004240 * on port @ap with sector count
Kristen Carlson Accardi9f45cbd2007-08-15 03:57:11 -04004241 *
4242 * LOCKING:
4243 * PCI/etc. bus probe sem.
4244 *
4245 * RETURNS:
4246 * 0 on success, AC_ERR_* mask otherwise.
4247 */
Tejun Heo1152b262010-09-01 17:50:05 +02004248unsigned int ata_dev_set_feature(struct ata_device *dev, u8 enable, u8 feature)
Kristen Carlson Accardi9f45cbd2007-08-15 03:57:11 -04004249{
4250 struct ata_taskfile tf;
4251 unsigned int err_mask;
Damien Le Moal974e0a42016-04-04 12:17:09 -04004252 unsigned long timeout = 0;
Kristen Carlson Accardi9f45cbd2007-08-15 03:57:11 -04004253
4254 /* set up set-features taskfile */
4255 DPRINTK("set features - SATA features\n");
4256
4257 ata_tf_init(dev, &tf);
4258 tf.command = ATA_CMD_SET_FEATURES;
4259 tf.feature = enable;
4260 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
4261 tf.protocol = ATA_PROT_NODATA;
Jeff Garzik218f3d32007-10-25 00:33:27 -04004262 tf.nsect = feature;
Kristen Carlson Accardi9f45cbd2007-08-15 03:57:11 -04004263
Damien Le Moal974e0a42016-04-04 12:17:09 -04004264 if (enable == SETFEATURES_SPINUP)
4265 timeout = ata_probe_timeout ?
4266 ata_probe_timeout * 1000 : SETFEATURES_SPINUP_TIMEOUT;
4267 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, timeout);
Kristen Carlson Accardi9f45cbd2007-08-15 03:57:11 -04004268
4269 DPRINTK("EXIT, err_mask=%x\n", err_mask);
4270 return err_mask;
4271}
Jeff Garzik633de4c2012-09-13 01:12:29 -04004272EXPORT_SYMBOL_GPL(ata_dev_set_feature);
Kristen Carlson Accardi9f45cbd2007-08-15 03:57:11 -04004273
4274/**
Albert Lee8bf62ece2005-05-12 15:29:42 -04004275 * ata_dev_init_params - Issue INIT DEV PARAMS command
Albert Lee8bf62ece2005-05-12 15:29:42 -04004276 * @dev: Device to which command will be sent
Randy Dunlape2a7f772006-05-18 10:50:18 -07004277 * @heads: Number of heads (taskfile parameter)
4278 * @sectors: Number of sectors (taskfile parameter)
Albert Lee8bf62ece2005-05-12 15:29:42 -04004279 *
4280 * LOCKING:
Tejun Heo6aff8f12006-02-15 18:24:09 +09004281 * Kernel thread context (may sleep)
4282 *
4283 * RETURNS:
4284 * 0 on success, AC_ERR_* mask otherwise.
Albert Lee8bf62ece2005-05-12 15:29:42 -04004285 */
Tejun Heo3373efd2006-05-15 20:57:53 +09004286static unsigned int ata_dev_init_params(struct ata_device *dev,
4287 u16 heads, u16 sectors)
Albert Lee8bf62ece2005-05-12 15:29:42 -04004288{
Tejun Heoa0123702005-12-13 14:49:31 +09004289 struct ata_taskfile tf;
Tejun Heo6aff8f12006-02-15 18:24:09 +09004290 unsigned int err_mask;
Albert Lee8bf62ece2005-05-12 15:29:42 -04004291
4292 /* Number of sectors per track 1-255. Number of heads 1-16 */
4293 if (sectors < 1 || sectors > 255 || heads < 1 || heads > 16)
Albert Lee00b6f5e2006-03-27 16:39:18 +08004294 return AC_ERR_INVALID;
Albert Lee8bf62ece2005-05-12 15:29:42 -04004295
4296 /* set up init dev params taskfile */
4297 DPRINTK("init dev params \n");
4298
Tejun Heo3373efd2006-05-15 20:57:53 +09004299 ata_tf_init(dev, &tf);
Tejun Heoa0123702005-12-13 14:49:31 +09004300 tf.command = ATA_CMD_INIT_DEV_PARAMS;
4301 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
4302 tf.protocol = ATA_PROT_NODATA;
4303 tf.nsect = sectors;
4304 tf.device |= (heads - 1) & 0x0f; /* max head = num. of heads - 1 */
Albert Lee8bf62ece2005-05-12 15:29:42 -04004305
Tejun Heo2b789102007-10-09 15:05:44 +09004306 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
Alan Cox18b24662007-08-08 14:28:49 +01004307 /* A clean abort indicates an original or just out of spec drive
4308 and we should continue as we issue the setup based on the
4309 drive reported working geometry */
4310 if (err_mask == AC_ERR_DEV && (tf.feature & ATA_ABORTED))
4311 err_mask = 0;
Albert Lee8bf62ece2005-05-12 15:29:42 -04004312
Tejun Heo6aff8f12006-02-15 18:24:09 +09004313 DPRINTK("EXIT, err_mask=%x\n", err_mask);
4314 return err_mask;
Albert Lee8bf62ece2005-05-12 15:29:42 -04004315}
4316
4317/**
Tejun Heo5895ef92008-06-17 12:36:26 +09004318 * atapi_check_dma - Check whether ATAPI DMA can be supported
Linus Torvalds1da177e2005-04-16 15:20:36 -07004319 * @qc: Metadata associated with taskfile to check
4320 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04004321 * Allow low-level driver to filter ATA PACKET commands, returning
4322 * a status indicating whether or not it is OK to use DMA for the
4323 * supplied PACKET command.
4324 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004325 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04004326 * spin_lock_irqsave(host lock)
Jeff Garzik0cba6322005-05-30 19:49:12 -04004327 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004328 * RETURNS: 0 when ATAPI DMA can be used
4329 * nonzero otherwise
4330 */
Tejun Heo5895ef92008-06-17 12:36:26 +09004331int atapi_check_dma(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004332{
4333 struct ata_port *ap = qc->ap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004334
Tejun Heob9a41972007-06-27 02:48:43 +09004335 /* Don't allow DMA if it isn't multiple of 16 bytes. Quite a
4336 * few ATAPI devices choke on such DMA requests.
4337 */
Tejun Heo6a87e422008-11-03 19:01:09 +09004338 if (!(qc->dev->horkage & ATA_HORKAGE_ATAPI_MOD16_DMA) &&
4339 unlikely(qc->nbytes & 15))
Tejun Heob9a41972007-06-27 02:48:43 +09004340 return 1;
Albert Lee6f23a312007-04-02 11:39:25 +08004341
Linus Torvalds1da177e2005-04-16 15:20:36 -07004342 if (ap->ops->check_atapi_dma)
Tejun Heob9a41972007-06-27 02:48:43 +09004343 return ap->ops->check_atapi_dma(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004344
Tejun Heob9a41972007-06-27 02:48:43 +09004345 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004346}
Tejun Heob9a41972007-06-27 02:48:43 +09004347
Linus Torvalds1da177e2005-04-16 15:20:36 -07004348/**
Tejun Heo31cc23b2007-09-23 13:14:12 +09004349 * ata_std_qc_defer - Check whether a qc needs to be deferred
4350 * @qc: ATA command in question
4351 *
4352 * Non-NCQ commands cannot run with any other command, NCQ or
4353 * not. As upper layer only knows the queue depth, we are
4354 * responsible for maintaining exclusion. This function checks
4355 * whether a new command @qc can be issued.
4356 *
4357 * LOCKING:
4358 * spin_lock_irqsave(host lock)
4359 *
4360 * RETURNS:
4361 * ATA_DEFER_* if deferring is needed, 0 otherwise.
4362 */
4363int ata_std_qc_defer(struct ata_queued_cmd *qc)
4364{
4365 struct ata_link *link = qc->dev->link;
4366
Hannes Reinecke179b3102016-07-14 09:05:43 +09004367 if (ata_is_ncq(qc->tf.protocol)) {
Tejun Heo31cc23b2007-09-23 13:14:12 +09004368 if (!ata_tag_valid(link->active_tag))
4369 return 0;
4370 } else {
4371 if (!ata_tag_valid(link->active_tag) && !link->sactive)
4372 return 0;
4373 }
4374
4375 return ATA_DEFER_LINK;
4376}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01004377EXPORT_SYMBOL_GPL(ata_std_qc_defer);
Tejun Heo31cc23b2007-09-23 13:14:12 +09004378
Jiri Slaby95364f32019-10-31 10:59:45 +01004379enum ata_completion_errors ata_noop_qc_prep(struct ata_queued_cmd *qc)
4380{
4381 return AC_ERR_OK;
4382}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01004383EXPORT_SYMBOL_GPL(ata_noop_qc_prep);
Brian Kinge46834c2006-03-17 17:04:03 -06004384
Jeff Garzik0cba6322005-05-30 19:49:12 -04004385/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04004386 * ata_sg_init - Associate command with scatter-gather table.
4387 * @qc: Command to be associated
4388 * @sg: Scatter-gather table.
4389 * @n_elem: Number of elements in s/g table.
4390 *
4391 * Initialize the data-related elements of queued_cmd @qc
4392 * to point to a scatter-gather table @sg, containing @n_elem
4393 * elements.
4394 *
4395 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04004396 * spin_lock_irqsave(host lock)
Jeff Garzik0cba6322005-05-30 19:49:12 -04004397 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004398void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
4399 unsigned int n_elem)
4400{
Tejun Heoff2aeb12007-12-05 16:43:11 +09004401 qc->sg = sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004402 qc->n_elem = n_elem;
Tejun Heoff2aeb12007-12-05 16:43:11 +09004403 qc->cursg = qc->sg;
4404}
4405
Geert Uytterhoeven2874d5e2017-01-03 19:09:49 +01004406#ifdef CONFIG_HAS_DMA
4407
4408/**
4409 * ata_sg_clean - Unmap DMA memory associated with command
4410 * @qc: Command containing DMA memory to be released
4411 *
4412 * Unmap all mapped DMA memory associated with this command.
4413 *
4414 * LOCKING:
4415 * spin_lock_irqsave(host lock)
4416 */
Jason Yanaf27e012017-03-10 10:05:40 +08004417static void ata_sg_clean(struct ata_queued_cmd *qc)
Geert Uytterhoeven2874d5e2017-01-03 19:09:49 +01004418{
4419 struct ata_port *ap = qc->ap;
4420 struct scatterlist *sg = qc->sg;
4421 int dir = qc->dma_dir;
4422
4423 WARN_ON_ONCE(sg == NULL);
4424
4425 VPRINTK("unmapping %u sg elements\n", qc->n_elem);
4426
4427 if (qc->n_elem)
4428 dma_unmap_sg(ap->dev, sg, qc->orig_n_elem, dir);
4429
4430 qc->flags &= ~ATA_QCFLAG_DMAMAP;
4431 qc->sg = NULL;
4432}
4433
Linus Torvalds1da177e2005-04-16 15:20:36 -07004434/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04004435 * ata_sg_setup - DMA-map the scatter-gather table associated with a command.
4436 * @qc: Command with scatter-gather table to be mapped.
4437 *
4438 * DMA-map the scatter-gather table associated with queued_cmd @qc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004439 *
4440 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04004441 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004442 *
4443 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004444 * Zero on success, negative on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004445 *
4446 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004447static int ata_sg_setup(struct ata_queued_cmd *qc)
4448{
4449 struct ata_port *ap = qc->ap;
James Bottomleydde20202008-02-19 11:36:56 +01004450 unsigned int n_elem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004451
Tejun Heo44877b42007-02-21 01:06:51 +09004452 VPRINTK("ENTER, ata%u\n", ap->print_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004453
James Bottomleydde20202008-02-19 11:36:56 +01004454 n_elem = dma_map_sg(ap->dev, qc->sg, qc->n_elem, qc->dma_dir);
4455 if (n_elem < 1)
4456 return -1;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004457
Paul Menzelbb376672018-07-08 09:18:21 +02004458 VPRINTK("%d sg elements mapped\n", n_elem);
FUJITA Tomonori58256272009-02-27 17:35:43 +09004459 qc->orig_n_elem = qc->n_elem;
James Bottomleydde20202008-02-19 11:36:56 +01004460 qc->n_elem = n_elem;
Tejun Heof92a2632007-12-05 16:43:10 +09004461 qc->flags |= ATA_QCFLAG_DMAMAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004462
4463 return 0;
4464}
4465
Geert Uytterhoeven2874d5e2017-01-03 19:09:49 +01004466#else /* !CONFIG_HAS_DMA */
4467
4468static inline void ata_sg_clean(struct ata_queued_cmd *qc) {}
4469static inline int ata_sg_setup(struct ata_queued_cmd *qc) { return -1; }
4470
4471#endif /* !CONFIG_HAS_DMA */
4472
Linus Torvalds1da177e2005-04-16 15:20:36 -07004473/**
Randy Dunlapc893a3a2006-01-28 13:15:32 -05004474 * swap_buf_le16 - swap halves of 16-bit words in place
Edward Falk0baab862005-06-02 18:17:13 -04004475 * @buf: Buffer to swap
4476 * @buf_words: Number of 16-bit words in buffer.
4477 *
4478 * Swap halves of 16-bit words if needed to convert from
4479 * little-endian byte order to native cpu byte order, or
4480 * vice-versa.
4481 *
4482 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04004483 * Inherited from caller.
Edward Falk0baab862005-06-02 18:17:13 -04004484 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004485void swap_buf_le16(u16 *buf, unsigned int buf_words)
4486{
4487#ifdef __BIG_ENDIAN
4488 unsigned int i;
4489
4490 for (i = 0; i < buf_words; i++)
4491 buf[i] = le16_to_cpu(buf[i]);
4492#endif /* __BIG_ENDIAN */
4493}
4494
Albert Lee6ae4cfb2005-08-12 14:15:34 +08004495/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004496 * ata_qc_new_init - Request an available ATA command, and initialize it
Linus Torvalds1da177e2005-04-16 15:20:36 -07004497 * @dev: Device from whom we request an available command structure
Randy Dunlap38755e82015-08-09 18:37:36 -07004498 * @tag: tag
Linus Torvalds1da177e2005-04-16 15:20:36 -07004499 *
4500 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004501 * None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004502 */
4503
Shaohua Li98bd4be2015-01-23 19:52:07 -08004504struct ata_queued_cmd *ata_qc_new_init(struct ata_device *dev, int tag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004505{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09004506 struct ata_port *ap = dev->link->ap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004507 struct ata_queued_cmd *qc;
4508
Shaohua Li98bd4be2015-01-23 19:52:07 -08004509 /* no command while frozen */
4510 if (unlikely(ap->pflags & ATA_PFLAG_FROZEN))
4511 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004512
Shaohua Li98bd4be2015-01-23 19:52:07 -08004513 /* libsas case */
Shaohua Li5067c042015-03-12 10:32:18 -07004514 if (ap->flags & ATA_FLAG_SAS_HOST) {
Shaohua Li98bd4be2015-01-23 19:52:07 -08004515 tag = ata_sas_allocate_tag(ap);
4516 if (tag < 0)
4517 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004518 }
4519
Shaohua Li98bd4be2015-01-23 19:52:07 -08004520 qc = __ata_qc_from_tag(ap, tag);
Jens Axboe5ac40792018-05-11 12:51:03 -06004521 qc->tag = qc->hw_tag = tag;
Shaohua Li98bd4be2015-01-23 19:52:07 -08004522 qc->scsicmd = NULL;
4523 qc->ap = ap;
4524 qc->dev = dev;
4525
4526 ata_qc_reinit(qc);
4527
Linus Torvalds1da177e2005-04-16 15:20:36 -07004528 return qc;
4529}
4530
Tejun Heo8a8bc222008-11-10 14:48:21 +09004531/**
4532 * ata_qc_free - free unused ata_queued_cmd
4533 * @qc: Command to complete
4534 *
4535 * Designed to free unused ata_queued_cmd object
4536 * in case something prevents using it.
4537 *
4538 * LOCKING:
4539 * spin_lock_irqsave(host lock)
4540 */
4541void ata_qc_free(struct ata_queued_cmd *qc)
4542{
Julia Lawalla1104012009-10-17 08:41:47 +02004543 struct ata_port *ap;
Tejun Heo8a8bc222008-11-10 14:48:21 +09004544 unsigned int tag;
4545
Tejun Heoefcb3cf2009-01-09 19:19:14 +09004546 WARN_ON_ONCE(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
Julia Lawalla1104012009-10-17 08:41:47 +02004547 ap = qc->ap;
Tejun Heo8a8bc222008-11-10 14:48:21 +09004548
4549 qc->flags = 0;
4550 tag = qc->tag;
Jens Axboe28361c42018-05-11 12:51:09 -06004551 if (ata_tag_valid(tag)) {
Tejun Heo8a8bc222008-11-10 14:48:21 +09004552 qc->tag = ATA_TAG_POISON;
Shaohua Li5067c042015-03-12 10:32:18 -07004553 if (ap->flags & ATA_FLAG_SAS_HOST)
Shaohua Li98bd4be2015-01-23 19:52:07 -08004554 ata_sas_free_tag(tag, ap);
Tejun Heo8a8bc222008-11-10 14:48:21 +09004555 }
4556}
4557
Tejun Heo76014422006-02-11 15:13:49 +09004558void __ata_qc_complete(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004559{
Julia Lawalla1104012009-10-17 08:41:47 +02004560 struct ata_port *ap;
4561 struct ata_link *link;
Tejun Heodedaf2b2006-05-15 21:03:43 +09004562
Tejun Heoefcb3cf2009-01-09 19:19:14 +09004563 WARN_ON_ONCE(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
4564 WARN_ON_ONCE(!(qc->flags & ATA_QCFLAG_ACTIVE));
Julia Lawalla1104012009-10-17 08:41:47 +02004565 ap = qc->ap;
4566 link = qc->dev->link;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004567
4568 if (likely(qc->flags & ATA_QCFLAG_DMAMAP))
4569 ata_sg_clean(qc);
4570
Tejun Heo7401abf2006-05-15 20:57:32 +09004571 /* command should be marked inactive atomically with qc completion */
Hannes Reinecke179b3102016-07-14 09:05:43 +09004572 if (ata_is_ncq(qc->tf.protocol)) {
Jens Axboe4e5b6262018-05-11 12:51:04 -06004573 link->sactive &= ~(1 << qc->hw_tag);
Tejun Heoda917d62007-09-23 13:14:12 +09004574 if (!link->sactive)
4575 ap->nr_active_links--;
4576 } else {
Tejun Heo9af5c9c2007-08-06 18:36:22 +09004577 link->active_tag = ATA_TAG_POISON;
Tejun Heoda917d62007-09-23 13:14:12 +09004578 ap->nr_active_links--;
4579 }
4580
4581 /* clear exclusive status */
4582 if (unlikely(qc->flags & ATA_QCFLAG_CLEAR_EXCL &&
4583 ap->excl_link == link))
4584 ap->excl_link = NULL;
Tejun Heo7401abf2006-05-15 20:57:32 +09004585
Albert Lee3f3791d2005-08-16 14:25:38 +08004586 /* atapi: mark qc as inactive to prevent the interrupt handler
4587 * from completing the command twice later, before the error handler
4588 * is called. (when rc != 0 and atapi request sense is needed)
4589 */
4590 qc->flags &= ~ATA_QCFLAG_ACTIVE;
Jens Axboee3ed89392018-05-11 12:51:05 -06004591 ap->qc_active &= ~(1ULL << qc->tag);
Albert Lee3f3791d2005-08-16 14:25:38 +08004592
Linus Torvalds1da177e2005-04-16 15:20:36 -07004593 /* call completion callback */
Tejun Heo77853bf2006-01-23 13:09:36 +09004594 qc->complete_fn(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004595}
4596
Tejun Heo39599a52006-11-14 22:37:35 +09004597static void fill_result_tf(struct ata_queued_cmd *qc)
4598{
4599 struct ata_port *ap = qc->ap;
4600
Tejun Heo39599a52006-11-14 22:37:35 +09004601 qc->result_tf.flags = qc->tf.flags;
Tejun Heo22183bf2008-04-07 22:47:20 +09004602 ap->ops->qc_fill_rtf(qc);
Tejun Heo39599a52006-11-14 22:37:35 +09004603}
4604
Tejun Heo00115e02007-11-27 19:28:58 +09004605static void ata_verify_xfer(struct ata_queued_cmd *qc)
4606{
4607 struct ata_device *dev = qc->dev;
4608
Christoph Hellwigeb0effd2016-07-16 22:16:41 +09004609 if (!ata_is_data(qc->tf.protocol))
Tejun Heo00115e02007-11-27 19:28:58 +09004610 return;
4611
4612 if ((dev->mwdma_mask || dev->udma_mask) && ata_is_pio(qc->tf.protocol))
4613 return;
4614
4615 dev->flags &= ~ATA_DFLAG_DUBIOUS_XFER;
4616}
4617
Tejun Heof686bcb2006-05-15 20:58:05 +09004618/**
4619 * ata_qc_complete - Complete an active ATA command
4620 * @qc: Command to complete
Tejun Heof686bcb2006-05-15 20:58:05 +09004621 *
Tejun Heo1aadf5c2010-06-25 15:03:34 +02004622 * Indicate to the mid and upper layers that an ATA command has
4623 * completed, with either an ok or not-ok status.
4624 *
4625 * Refrain from calling this function multiple times when
4626 * successfully completing multiple NCQ commands.
4627 * ata_qc_complete_multiple() should be used instead, which will
4628 * properly update IRQ expect state.
Tejun Heof686bcb2006-05-15 20:58:05 +09004629 *
4630 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04004631 * spin_lock_irqsave(host lock)
Tejun Heof686bcb2006-05-15 20:58:05 +09004632 */
4633void ata_qc_complete(struct ata_queued_cmd *qc)
4634{
4635 struct ata_port *ap = qc->ap;
4636
Stephan Linzeb25cb92016-06-10 07:59:56 +02004637 /* Trigger the LED (if available) */
Linus Walleijd1ed7c52018-02-24 23:45:56 +01004638 ledtrig_disk_activity(!!(qc->tf.flags & ATA_TFLAG_WRITE));
Stephan Linzeb25cb92016-06-10 07:59:56 +02004639
Tejun Heof686bcb2006-05-15 20:58:05 +09004640 /* XXX: New EH and old EH use different mechanisms to
4641 * synchronize EH with regular execution path.
4642 *
4643 * In new EH, a failed qc is marked with ATA_QCFLAG_FAILED.
4644 * Normal execution path is responsible for not accessing a
4645 * failed qc. libata core enforces the rule by returning NULL
4646 * from ata_qc_from_tag() for failed qcs.
4647 *
4648 * Old EH depends on ata_qc_complete() nullifying completion
4649 * requests if ATA_QCFLAG_EH_SCHEDULED is set. Old EH does
4650 * not synchronize with interrupt handler. Only PIO task is
4651 * taken care of.
4652 */
4653 if (ap->ops->error_handler) {
Tejun Heo4dbfa392007-10-25 18:22:44 +09004654 struct ata_device *dev = qc->dev;
4655 struct ata_eh_info *ehi = &dev->link->eh_info;
4656
Tejun Heof686bcb2006-05-15 20:58:05 +09004657 if (unlikely(qc->err_mask))
4658 qc->flags |= ATA_QCFLAG_FAILED;
4659
Tejun Heof08dc1a2010-12-09 15:59:32 +01004660 /*
4661 * Finish internal commands without any further processing
4662 * and always with the result TF filled.
4663 */
4664 if (unlikely(ata_tag_internal(qc->tag))) {
Tejun Heof4b31db92009-10-16 13:00:51 +09004665 fill_result_tf(qc);
Hannes Reinecke255c03d2015-03-27 16:46:38 +01004666 trace_ata_qc_complete_internal(qc);
Tejun Heof08dc1a2010-12-09 15:59:32 +01004667 __ata_qc_complete(qc);
4668 return;
4669 }
Tejun Heof4b31db92009-10-16 13:00:51 +09004670
Tejun Heof08dc1a2010-12-09 15:59:32 +01004671 /*
4672 * Non-internal qc has failed. Fill the result TF and
4673 * summon EH.
4674 */
4675 if (unlikely(qc->flags & ATA_QCFLAG_FAILED)) {
4676 fill_result_tf(qc);
Hannes Reinecke255c03d2015-03-27 16:46:38 +01004677 trace_ata_qc_complete_failed(qc);
Tejun Heof08dc1a2010-12-09 15:59:32 +01004678 ata_qc_schedule_eh(qc);
Tejun Heof4b31db92009-10-16 13:00:51 +09004679 return;
Tejun Heof686bcb2006-05-15 20:58:05 +09004680 }
4681
Tejun Heo4dc738e2009-09-17 18:45:27 +09004682 WARN_ON_ONCE(ap->pflags & ATA_PFLAG_FROZEN);
4683
Tejun Heof686bcb2006-05-15 20:58:05 +09004684 /* read result TF if requested */
4685 if (qc->flags & ATA_QCFLAG_RESULT_TF)
Tejun Heo39599a52006-11-14 22:37:35 +09004686 fill_result_tf(qc);
Tejun Heof686bcb2006-05-15 20:58:05 +09004687
Hannes Reinecke255c03d2015-03-27 16:46:38 +01004688 trace_ata_qc_complete_done(qc);
Tejun Heo4dbfa392007-10-25 18:22:44 +09004689 /* Some commands need post-processing after successful
4690 * completion.
4691 */
4692 switch (qc->tf.command) {
4693 case ATA_CMD_SET_FEATURES:
4694 if (qc->tf.feature != SETFEATURES_WC_ON &&
Tom Yan0c127352016-07-12 21:37:02 +08004695 qc->tf.feature != SETFEATURES_WC_OFF &&
4696 qc->tf.feature != SETFEATURES_RA_ON &&
4697 qc->tf.feature != SETFEATURES_RA_OFF)
Tejun Heo4dbfa392007-10-25 18:22:44 +09004698 break;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05004699 fallthrough;
Tejun Heo4dbfa392007-10-25 18:22:44 +09004700 case ATA_CMD_INIT_DEV_PARAMS: /* CHS translation changed */
4701 case ATA_CMD_SET_MULTI: /* multi_count changed */
4702 /* revalidate device */
4703 ehi->dev_action[dev->devno] |= ATA_EH_REVALIDATE;
4704 ata_port_schedule_eh(ap);
4705 break;
Tejun Heo054a5fb2007-10-25 18:30:36 +09004706
4707 case ATA_CMD_SLEEP:
4708 dev->flags |= ATA_DFLAG_SLEEPING;
4709 break;
Tejun Heo4dbfa392007-10-25 18:22:44 +09004710 }
4711
Tejun Heo00115e02007-11-27 19:28:58 +09004712 if (unlikely(dev->flags & ATA_DFLAG_DUBIOUS_XFER))
4713 ata_verify_xfer(qc);
4714
Tejun Heof686bcb2006-05-15 20:58:05 +09004715 __ata_qc_complete(qc);
4716 } else {
4717 if (qc->flags & ATA_QCFLAG_EH_SCHEDULED)
4718 return;
4719
4720 /* read result TF if failed or requested */
4721 if (qc->err_mask || qc->flags & ATA_QCFLAG_RESULT_TF)
Tejun Heo39599a52006-11-14 22:37:35 +09004722 fill_result_tf(qc);
Tejun Heof686bcb2006-05-15 20:58:05 +09004723
4724 __ata_qc_complete(qc);
4725 }
4726}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01004727EXPORT_SYMBOL_GPL(ata_qc_complete);
Tejun Heof686bcb2006-05-15 20:58:05 +09004728
Tejun Heodedaf2b2006-05-15 21:03:43 +09004729/**
Sascha Hauer8385d752019-12-13 09:04:08 +01004730 * ata_qc_get_active - get bitmask of active qcs
4731 * @ap: port in question
4732 *
4733 * LOCKING:
4734 * spin_lock_irqsave(host lock)
4735 *
4736 * RETURNS:
4737 * Bitmask of active qcs
4738 */
4739u64 ata_qc_get_active(struct ata_port *ap)
4740{
4741 u64 qc_active = ap->qc_active;
4742
4743 /* ATA_TAG_INTERNAL is sent to hw as tag 0 */
4744 if (qc_active & (1ULL << ATA_TAG_INTERNAL)) {
4745 qc_active |= (1 << 0);
4746 qc_active &= ~(1ULL << ATA_TAG_INTERNAL);
4747 }
4748
4749 return qc_active;
4750}
4751EXPORT_SYMBOL_GPL(ata_qc_get_active);
4752
4753/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004754 * ata_qc_issue - issue taskfile to device
4755 * @qc: command to issue to device
4756 *
4757 * Prepare an ATA command to submission to device.
4758 * This includes mapping the data into a DMA-able
4759 * area, filling in the S/G table, and finally
4760 * writing the taskfile to hardware, starting the command.
4761 *
4762 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04004763 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004764 */
Tejun Heo8e0e6942006-03-31 20:41:11 +09004765void ata_qc_issue(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004766{
4767 struct ata_port *ap = qc->ap;
Tejun Heo9af5c9c2007-08-06 18:36:22 +09004768 struct ata_link *link = qc->dev->link;
Tejun Heo405e66b2007-11-27 19:28:53 +09004769 u8 prot = qc->tf.protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004770
Tejun Heodedaf2b2006-05-15 21:03:43 +09004771 /* Make sure only one non-NCQ command is outstanding. The
4772 * check is skipped for old EH because it reuses active qc to
4773 * request ATAPI sense.
4774 */
Tejun Heoefcb3cf2009-01-09 19:19:14 +09004775 WARN_ON_ONCE(ap->ops->error_handler && ata_tag_valid(link->active_tag));
Tejun Heodedaf2b2006-05-15 21:03:43 +09004776
Tejun Heo1973a022007-12-05 10:36:13 +09004777 if (ata_is_ncq(prot)) {
Jens Axboe4e5b6262018-05-11 12:51:04 -06004778 WARN_ON_ONCE(link->sactive & (1 << qc->hw_tag));
Tejun Heoda917d62007-09-23 13:14:12 +09004779
4780 if (!link->sactive)
4781 ap->nr_active_links++;
Jens Axboe4e5b6262018-05-11 12:51:04 -06004782 link->sactive |= 1 << qc->hw_tag;
Tejun Heodedaf2b2006-05-15 21:03:43 +09004783 } else {
Tejun Heoefcb3cf2009-01-09 19:19:14 +09004784 WARN_ON_ONCE(link->sactive);
Tejun Heoda917d62007-09-23 13:14:12 +09004785
4786 ap->nr_active_links++;
Tejun Heo9af5c9c2007-08-06 18:36:22 +09004787 link->active_tag = qc->tag;
Tejun Heodedaf2b2006-05-15 21:03:43 +09004788 }
4789
Tejun Heoe4a70e72006-03-31 20:36:47 +09004790 qc->flags |= ATA_QCFLAG_ACTIVE;
Jens Axboee3ed89392018-05-11 12:51:05 -06004791 ap->qc_active |= 1ULL << qc->tag;
Tejun Heoe4a70e72006-03-31 20:36:47 +09004792
Tejun Heo60f5d6e2010-08-23 11:27:27 +02004793 /*
4794 * We guarantee to LLDs that they will have at least one
Tejun Heof92a2632007-12-05 16:43:10 +09004795 * non-zero sg if the command is a data command.
4796 */
Eric Biggers9173e5e2018-02-03 20:33:27 -08004797 if (ata_is_data(prot) && (!qc->sg || !qc->n_elem || !qc->nbytes))
Tejun Heo60f5d6e2010-08-23 11:27:27 +02004798 goto sys_err;
Tejun Heof92a2632007-12-05 16:43:10 +09004799
Tejun Heo405e66b2007-11-27 19:28:53 +09004800 if (ata_is_dma(prot) || (ata_is_pio(prot) &&
Tejun Heof92a2632007-12-05 16:43:10 +09004801 (ap->flags & ATA_FLAG_PIO_DMA)))
Tejun Heo001102d2007-12-05 16:43:09 +09004802 if (ata_sg_setup(qc))
Tejun Heo60f5d6e2010-08-23 11:27:27 +02004803 goto sys_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004804
Tejun Heocf480622008-01-24 00:05:14 +09004805 /* if device is sleeping, schedule reset and abort the link */
Tejun Heo054a5fb2007-10-25 18:30:36 +09004806 if (unlikely(qc->dev->flags & ATA_DFLAG_SLEEPING)) {
Tejun Heocf480622008-01-24 00:05:14 +09004807 link->eh_info.action |= ATA_EH_RESET;
Tejun Heo054a5fb2007-10-25 18:30:36 +09004808 ata_ehi_push_desc(&link->eh_info, "waking up from sleep");
4809 ata_link_abort(link);
4810 return;
4811 }
4812
Jiri Slaby95364f32019-10-31 10:59:45 +01004813 qc->err_mask |= ap->ops->qc_prep(qc);
4814 if (unlikely(qc->err_mask))
4815 goto err;
Hannes Reinecke255c03d2015-03-27 16:46:38 +01004816 trace_ata_qc_issue(qc);
Tejun Heo8e0e6942006-03-31 20:41:11 +09004817 qc->err_mask |= ap->ops->qc_issue(qc);
4818 if (unlikely(qc->err_mask))
4819 goto err;
4820 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004821
Tejun Heo60f5d6e2010-08-23 11:27:27 +02004822sys_err:
Tejun Heo8e0e6942006-03-31 20:41:11 +09004823 qc->err_mask |= AC_ERR_SYSTEM;
4824err:
4825 ata_qc_complete(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004826}
4827
4828/**
Tejun Heob1c72912008-07-31 17:02:43 +09004829 * ata_phys_link_online - test whether the given link is online
Tejun Heo936fd732007-08-06 18:36:23 +09004830 * @link: ATA link to test
Tejun Heo34bf2172006-05-15 20:57:46 +09004831 *
Tejun Heo936fd732007-08-06 18:36:23 +09004832 * Test whether @link is online. Note that this function returns
4833 * 0 if online status of @link cannot be obtained, so
4834 * ata_link_online(link) != !ata_link_offline(link).
Tejun Heo34bf2172006-05-15 20:57:46 +09004835 *
4836 * LOCKING:
4837 * None.
4838 *
4839 * RETURNS:
Tejun Heob5b3fa32008-07-31 17:02:42 +09004840 * True if the port online status is available and online.
Tejun Heo34bf2172006-05-15 20:57:46 +09004841 */
Tejun Heob1c72912008-07-31 17:02:43 +09004842bool ata_phys_link_online(struct ata_link *link)
Tejun Heo34bf2172006-05-15 20:57:46 +09004843{
4844 u32 sstatus;
4845
Tejun Heo936fd732007-08-06 18:36:23 +09004846 if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0 &&
Tejun Heo9913ff8a2009-01-29 20:31:31 +09004847 ata_sstatus_online(sstatus))
Tejun Heob5b3fa32008-07-31 17:02:42 +09004848 return true;
4849 return false;
Tejun Heo34bf2172006-05-15 20:57:46 +09004850}
4851
4852/**
Tejun Heob1c72912008-07-31 17:02:43 +09004853 * ata_phys_link_offline - test whether the given link is offline
Tejun Heo936fd732007-08-06 18:36:23 +09004854 * @link: ATA link to test
Tejun Heo34bf2172006-05-15 20:57:46 +09004855 *
Tejun Heo936fd732007-08-06 18:36:23 +09004856 * Test whether @link is offline. Note that this function
4857 * returns 0 if offline status of @link cannot be obtained, so
4858 * ata_link_online(link) != !ata_link_offline(link).
Tejun Heo34bf2172006-05-15 20:57:46 +09004859 *
4860 * LOCKING:
4861 * None.
4862 *
4863 * RETURNS:
Tejun Heob5b3fa32008-07-31 17:02:42 +09004864 * True if the port offline status is available and offline.
Tejun Heo34bf2172006-05-15 20:57:46 +09004865 */
Tejun Heob1c72912008-07-31 17:02:43 +09004866bool ata_phys_link_offline(struct ata_link *link)
Tejun Heo34bf2172006-05-15 20:57:46 +09004867{
4868 u32 sstatus;
4869
Tejun Heo936fd732007-08-06 18:36:23 +09004870 if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0 &&
Tejun Heo9913ff8a2009-01-29 20:31:31 +09004871 !ata_sstatus_online(sstatus))
Tejun Heob5b3fa32008-07-31 17:02:42 +09004872 return true;
4873 return false;
Tejun Heo34bf2172006-05-15 20:57:46 +09004874}
Edward Falk0baab862005-06-02 18:17:13 -04004875
Tejun Heob1c72912008-07-31 17:02:43 +09004876/**
4877 * ata_link_online - test whether the given link is online
4878 * @link: ATA link to test
4879 *
4880 * Test whether @link is online. This is identical to
4881 * ata_phys_link_online() when there's no slave link. When
4882 * there's a slave link, this function should only be called on
4883 * the master link and will return true if any of M/S links is
4884 * online.
4885 *
4886 * LOCKING:
4887 * None.
4888 *
4889 * RETURNS:
4890 * True if the port online status is available and online.
4891 */
4892bool ata_link_online(struct ata_link *link)
4893{
4894 struct ata_link *slave = link->ap->slave_link;
4895
4896 WARN_ON(link == slave); /* shouldn't be called on slave link */
4897
4898 return ata_phys_link_online(link) ||
4899 (slave && ata_phys_link_online(slave));
4900}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01004901EXPORT_SYMBOL_GPL(ata_link_online);
Tejun Heob1c72912008-07-31 17:02:43 +09004902
4903/**
4904 * ata_link_offline - test whether the given link is offline
4905 * @link: ATA link to test
4906 *
4907 * Test whether @link is offline. This is identical to
4908 * ata_phys_link_offline() when there's no slave link. When
4909 * there's a slave link, this function should only be called on
4910 * the master link and will return true if both M/S links are
4911 * offline.
4912 *
4913 * LOCKING:
4914 * None.
4915 *
4916 * RETURNS:
4917 * True if the port offline status is available and offline.
4918 */
4919bool ata_link_offline(struct ata_link *link)
4920{
4921 struct ata_link *slave = link->ap->slave_link;
4922
4923 WARN_ON(link == slave); /* shouldn't be called on slave link */
4924
4925 return ata_phys_link_offline(link) &&
4926 (!slave || ata_phys_link_offline(slave));
4927}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01004928EXPORT_SYMBOL_GPL(ata_link_offline);
Tejun Heob1c72912008-07-31 17:02:43 +09004929
Tejun Heo6ffa01d2007-03-02 17:32:47 +09004930#ifdef CONFIG_PM
Dan Williamsbc6e7c42014-03-14 13:52:48 -07004931static void ata_port_request_pm(struct ata_port *ap, pm_message_t mesg,
4932 unsigned int action, unsigned int ehi_flags,
4933 bool async)
Tejun Heo500530f2006-07-03 16:07:27 +09004934{
Lin Ming5ef41082011-12-05 09:20:27 +08004935 struct ata_link *link;
Tejun Heo500530f2006-07-03 16:07:27 +09004936 unsigned long flags;
Tejun Heo500530f2006-07-03 16:07:27 +09004937
Lin Ming5ef41082011-12-05 09:20:27 +08004938 /* Previous resume operation might still be in
4939 * progress. Wait for PM_PENDING to clear.
4940 */
4941 if (ap->pflags & ATA_PFLAG_PM_PENDING) {
4942 ata_port_wait_eh(ap);
4943 WARN_ON(ap->pflags & ATA_PFLAG_PM_PENDING);
Tejun Heo500530f2006-07-03 16:07:27 +09004944 }
4945
Lin Ming5ef41082011-12-05 09:20:27 +08004946 /* request PM ops to EH */
4947 spin_lock_irqsave(ap->lock, flags);
4948
4949 ap->pm_mesg = mesg;
Lin Ming5ef41082011-12-05 09:20:27 +08004950 ap->pflags |= ATA_PFLAG_PM_PENDING;
4951 ata_for_each_link(link, ap, HOST_FIRST) {
4952 link->eh_info.action |= action;
4953 link->eh_info.flags |= ehi_flags;
4954 }
4955
4956 ata_port_schedule_eh(ap);
4957
4958 spin_unlock_irqrestore(ap->lock, flags);
4959
Dan Williams2fcbdcb2012-06-21 23:41:46 -07004960 if (!async) {
Lin Ming5ef41082011-12-05 09:20:27 +08004961 ata_port_wait_eh(ap);
4962 WARN_ON(ap->pflags & ATA_PFLAG_PM_PENDING);
4963 }
Tejun Heo500530f2006-07-03 16:07:27 +09004964}
4965
Dan Williamsbc6e7c42014-03-14 13:52:48 -07004966/*
4967 * On some hardware, device fails to respond after spun down for suspend. As
4968 * the device won't be used before being resumed, we don't need to touch the
4969 * device. Ask EH to skip the usual stuff and proceed directly to suspend.
4970 *
4971 * http://thread.gmane.org/gmane.linux.ide/46764
4972 */
4973static const unsigned int ata_port_suspend_ehi = ATA_EHI_QUIET
4974 | ATA_EHI_NO_AUTOPSY
4975 | ATA_EHI_NO_RECOVERY;
4976
4977static void ata_port_suspend(struct ata_port *ap, pm_message_t mesg)
Lin Ming5ef41082011-12-05 09:20:27 +08004978{
Dan Williamsbc6e7c42014-03-14 13:52:48 -07004979 ata_port_request_pm(ap, mesg, 0, ata_port_suspend_ehi, false);
Lin Ming5ef41082011-12-05 09:20:27 +08004980}
4981
Dan Williamsbc6e7c42014-03-14 13:52:48 -07004982static void ata_port_suspend_async(struct ata_port *ap, pm_message_t mesg)
4983{
4984 ata_port_request_pm(ap, mesg, 0, ata_port_suspend_ehi, true);
4985}
4986
4987static int ata_port_pm_suspend(struct device *dev)
Dan Williams2fcbdcb2012-06-21 23:41:46 -07004988{
4989 struct ata_port *ap = to_ata_port(dev);
4990
Lin Ming5ef41082011-12-05 09:20:27 +08004991 if (pm_runtime_suspended(dev))
4992 return 0;
4993
Dan Williamsbc6e7c42014-03-14 13:52:48 -07004994 ata_port_suspend(ap, PMSG_SUSPEND);
4995 return 0;
Lin Ming33574d62011-12-22 14:50:49 +08004996}
4997
Dan Williamsbc6e7c42014-03-14 13:52:48 -07004998static int ata_port_pm_freeze(struct device *dev)
Dan Williams2fcbdcb2012-06-21 23:41:46 -07004999{
5000 struct ata_port *ap = to_ata_port(dev);
5001
Dan Williamsbc6e7c42014-03-14 13:52:48 -07005002 if (pm_runtime_suspended(dev))
5003 return 0;
5004
5005 ata_port_suspend(ap, PMSG_FREEZE);
5006 return 0;
Dan Williams2fcbdcb2012-06-21 23:41:46 -07005007}
5008
Dan Williamsbc6e7c42014-03-14 13:52:48 -07005009static int ata_port_pm_poweroff(struct device *dev)
Lin Minge90b1e52011-12-22 14:50:48 +08005010{
Dan Williamsbc6e7c42014-03-14 13:52:48 -07005011 ata_port_suspend(to_ata_port(dev), PMSG_HIBERNATE);
5012 return 0;
5013}
Lin Minge90b1e52011-12-22 14:50:48 +08005014
Dan Williamsbc6e7c42014-03-14 13:52:48 -07005015static const unsigned int ata_port_resume_ehi = ATA_EHI_NO_AUTOPSY
5016 | ATA_EHI_QUIET;
Lin Minge90b1e52011-12-22 14:50:48 +08005017
Dan Williamsbc6e7c42014-03-14 13:52:48 -07005018static void ata_port_resume(struct ata_port *ap, pm_message_t mesg)
5019{
5020 ata_port_request_pm(ap, mesg, ATA_EH_RESET, ata_port_resume_ehi, false);
5021}
5022
5023static void ata_port_resume_async(struct ata_port *ap, pm_message_t mesg)
5024{
5025 ata_port_request_pm(ap, mesg, ATA_EH_RESET, ata_port_resume_ehi, true);
5026}
5027
5028static int ata_port_pm_resume(struct device *dev)
5029{
Todd Brandt200421a2014-03-14 13:52:54 -07005030 ata_port_resume_async(to_ata_port(dev), PMSG_RESUME);
Dan Williamsbc6e7c42014-03-14 13:52:48 -07005031 pm_runtime_disable(dev);
5032 pm_runtime_set_active(dev);
5033 pm_runtime_enable(dev);
5034 return 0;
Lin Minge90b1e52011-12-22 14:50:48 +08005035}
5036
Aaron Lu7e15e9b2013-01-15 17:21:04 +08005037/*
5038 * For ODDs, the upper layer will poll for media change every few seconds,
5039 * which will make it enter and leave suspend state every few seconds. And
5040 * as each suspend will cause a hard/soft reset, the gain of runtime suspend
5041 * is very little and the ODD may malfunction after constantly being reset.
5042 * So the idle callback here will not proceed to suspend if a non-ZPODD capable
5043 * ODD is attached to the port.
5044 */
Lin Ming9ee4f392011-12-05 09:20:28 +08005045static int ata_port_runtime_idle(struct device *dev)
5046{
Aaron Lu7e15e9b2013-01-15 17:21:04 +08005047 struct ata_port *ap = to_ata_port(dev);
5048 struct ata_link *link;
5049 struct ata_device *adev;
5050
5051 ata_for_each_link(link, ap, HOST_FIRST) {
5052 ata_for_each_dev(adev, link, ENABLED)
5053 if (adev->class == ATA_DEV_ATAPI &&
5054 !zpodd_dev_enabled(adev))
5055 return -EBUSY;
5056 }
5057
Rafael J. Wysocki45f0a852013-06-03 21:49:52 +02005058 return 0;
Lin Ming9ee4f392011-12-05 09:20:28 +08005059}
5060
Aaron Lua7ff60d2013-01-25 14:29:35 +08005061static int ata_port_runtime_suspend(struct device *dev)
5062{
Dan Williamsbc6e7c42014-03-14 13:52:48 -07005063 ata_port_suspend(to_ata_port(dev), PMSG_AUTO_SUSPEND);
5064 return 0;
Aaron Lua7ff60d2013-01-25 14:29:35 +08005065}
5066
5067static int ata_port_runtime_resume(struct device *dev)
5068{
Dan Williamsbc6e7c42014-03-14 13:52:48 -07005069 ata_port_resume(to_ata_port(dev), PMSG_AUTO_RESUME);
5070 return 0;
Aaron Lua7ff60d2013-01-25 14:29:35 +08005071}
5072
Lin Ming5ef41082011-12-05 09:20:27 +08005073static const struct dev_pm_ops ata_port_pm_ops = {
Dan Williamsbc6e7c42014-03-14 13:52:48 -07005074 .suspend = ata_port_pm_suspend,
5075 .resume = ata_port_pm_resume,
5076 .freeze = ata_port_pm_freeze,
5077 .thaw = ata_port_pm_resume,
5078 .poweroff = ata_port_pm_poweroff,
5079 .restore = ata_port_pm_resume,
Lin Ming9ee4f392011-12-05 09:20:28 +08005080
Aaron Lua7ff60d2013-01-25 14:29:35 +08005081 .runtime_suspend = ata_port_runtime_suspend,
5082 .runtime_resume = ata_port_runtime_resume,
Lin Ming9ee4f392011-12-05 09:20:28 +08005083 .runtime_idle = ata_port_runtime_idle,
Lin Ming5ef41082011-12-05 09:20:27 +08005084};
5085
Dan Williams2fcbdcb2012-06-21 23:41:46 -07005086/* sas ports don't participate in pm runtime management of ata_ports,
5087 * and need to resume ata devices at the domain level, not the per-port
5088 * level. sas suspend/resume is async to allow parallel port recovery
5089 * since sas has multiple ata_port instances per Scsi_Host.
5090 */
Dan Williamsbc6e7c42014-03-14 13:52:48 -07005091void ata_sas_port_suspend(struct ata_port *ap)
Dan Williams2fcbdcb2012-06-21 23:41:46 -07005092{
Dan Williamsbc6e7c42014-03-14 13:52:48 -07005093 ata_port_suspend_async(ap, PMSG_SUSPEND);
Dan Williams2fcbdcb2012-06-21 23:41:46 -07005094}
Dan Williamsbc6e7c42014-03-14 13:52:48 -07005095EXPORT_SYMBOL_GPL(ata_sas_port_suspend);
Dan Williams2fcbdcb2012-06-21 23:41:46 -07005096
Dan Williamsbc6e7c42014-03-14 13:52:48 -07005097void ata_sas_port_resume(struct ata_port *ap)
Dan Williams2fcbdcb2012-06-21 23:41:46 -07005098{
Dan Williamsbc6e7c42014-03-14 13:52:48 -07005099 ata_port_resume_async(ap, PMSG_RESUME);
Dan Williams2fcbdcb2012-06-21 23:41:46 -07005100}
Dan Williamsbc6e7c42014-03-14 13:52:48 -07005101EXPORT_SYMBOL_GPL(ata_sas_port_resume);
Dan Williams2fcbdcb2012-06-21 23:41:46 -07005102
Tejun Heo500530f2006-07-03 16:07:27 +09005103/**
Jeff Garzikcca39742006-08-24 03:19:22 -04005104 * ata_host_suspend - suspend host
5105 * @host: host to suspend
Tejun Heo500530f2006-07-03 16:07:27 +09005106 * @mesg: PM message
5107 *
Lin Ming5ef41082011-12-05 09:20:27 +08005108 * Suspend @host. Actual operation is performed by port suspend.
Tejun Heo500530f2006-07-03 16:07:27 +09005109 */
Jeff Garzikcca39742006-08-24 03:19:22 -04005110int ata_host_suspend(struct ata_host *host, pm_message_t mesg)
Tejun Heo500530f2006-07-03 16:07:27 +09005111{
Lin Ming5ef41082011-12-05 09:20:27 +08005112 host->dev->power.power_state = mesg;
5113 return 0;
Tejun Heo500530f2006-07-03 16:07:27 +09005114}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01005115EXPORT_SYMBOL_GPL(ata_host_suspend);
Tejun Heo500530f2006-07-03 16:07:27 +09005116
5117/**
Jeff Garzikcca39742006-08-24 03:19:22 -04005118 * ata_host_resume - resume host
5119 * @host: host to resume
Tejun Heo500530f2006-07-03 16:07:27 +09005120 *
Lin Ming5ef41082011-12-05 09:20:27 +08005121 * Resume @host. Actual operation is performed by port resume.
Tejun Heo500530f2006-07-03 16:07:27 +09005122 */
Jeff Garzikcca39742006-08-24 03:19:22 -04005123void ata_host_resume(struct ata_host *host)
Tejun Heo500530f2006-07-03 16:07:27 +09005124{
Jeff Garzik72ad6ec2008-02-25 17:31:10 -05005125 host->dev->power.power_state = PMSG_ON;
Tejun Heo500530f2006-07-03 16:07:27 +09005126}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01005127EXPORT_SYMBOL_GPL(ata_host_resume);
Tejun Heo6ffa01d2007-03-02 17:32:47 +09005128#endif
Tejun Heo500530f2006-07-03 16:07:27 +09005129
Bhumika Goyal8df82c12017-09-30 22:10:40 +05305130const struct device_type ata_port_type = {
Lin Ming5ef41082011-12-05 09:20:27 +08005131 .name = "ata_port",
5132#ifdef CONFIG_PM
5133 .pm = &ata_port_pm_ops,
5134#endif
5135};
5136
Randy Dunlapc893a3a2006-01-28 13:15:32 -05005137/**
Tejun Heo3ef3b432006-05-31 18:27:30 +09005138 * ata_dev_init - Initialize an ata_device structure
5139 * @dev: Device structure to initialize
5140 *
5141 * Initialize @dev in preparation for probing.
5142 *
5143 * LOCKING:
5144 * Inherited from caller.
5145 */
5146void ata_dev_init(struct ata_device *dev)
5147{
Tejun Heob1c72912008-07-31 17:02:43 +09005148 struct ata_link *link = ata_dev_phys_link(dev);
Tejun Heo9af5c9c2007-08-06 18:36:22 +09005149 struct ata_port *ap = link->ap;
Tejun Heo72fa4b72006-05-31 18:27:32 +09005150 unsigned long flags;
Tejun Heo3ef3b432006-05-31 18:27:30 +09005151
Tejun Heob1c72912008-07-31 17:02:43 +09005152 /* SATA spd limit is bound to the attached device, reset together */
Tejun Heo9af5c9c2007-08-06 18:36:22 +09005153 link->sata_spd_limit = link->hw_sata_spd_limit;
5154 link->sata_spd = 0;
Tejun Heo5a04bf42006-05-31 18:27:38 +09005155
Tejun Heo72fa4b72006-05-31 18:27:32 +09005156 /* High bits of dev->flags are used to record warm plug
5157 * requests which occur asynchronously. Synchronize using
Jeff Garzikcca39742006-08-24 03:19:22 -04005158 * host lock.
Tejun Heo72fa4b72006-05-31 18:27:32 +09005159 */
Jeff Garzikba6a1302006-06-22 23:46:10 -04005160 spin_lock_irqsave(ap->lock, flags);
Tejun Heo72fa4b72006-05-31 18:27:32 +09005161 dev->flags &= ~ATA_DFLAG_INIT_MASK;
Tejun Heo3dcc3232007-09-03 12:20:11 +09005162 dev->horkage = 0;
Jeff Garzikba6a1302006-06-22 23:46:10 -04005163 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heo72fa4b72006-05-31 18:27:32 +09005164
Tejun Heo99cf6102009-01-29 20:31:32 +09005165 memset((void *)dev + ATA_DEVICE_CLEAR_BEGIN, 0,
5166 ATA_DEVICE_CLEAR_END - ATA_DEVICE_CLEAR_BEGIN);
Tejun Heo3ef3b432006-05-31 18:27:30 +09005167 dev->pio_mask = UINT_MAX;
5168 dev->mwdma_mask = UINT_MAX;
5169 dev->udma_mask = UINT_MAX;
5170}
5171
5172/**
Tejun Heo4fb37a22007-08-06 18:36:23 +09005173 * ata_link_init - Initialize an ata_link structure
5174 * @ap: ATA port link is attached to
5175 * @link: Link structure to initialize
Tejun Heo89898052007-08-06 18:36:23 +09005176 * @pmp: Port multiplier port number
Tejun Heo4fb37a22007-08-06 18:36:23 +09005177 *
5178 * Initialize @link.
5179 *
5180 * LOCKING:
5181 * Kernel thread context (may sleep)
5182 */
Tejun Heofb7fd612007-09-23 13:14:12 +09005183void ata_link_init(struct ata_port *ap, struct ata_link *link, int pmp)
Tejun Heo4fb37a22007-08-06 18:36:23 +09005184{
5185 int i;
5186
5187 /* clear everything except for devices */
Gwendal Grignoud9027472010-05-25 12:31:38 -07005188 memset((void *)link + ATA_LINK_CLEAR_BEGIN, 0,
5189 ATA_LINK_CLEAR_END - ATA_LINK_CLEAR_BEGIN);
Tejun Heo4fb37a22007-08-06 18:36:23 +09005190
5191 link->ap = ap;
Tejun Heo89898052007-08-06 18:36:23 +09005192 link->pmp = pmp;
Tejun Heo4fb37a22007-08-06 18:36:23 +09005193 link->active_tag = ATA_TAG_POISON;
5194 link->hw_sata_spd_limit = UINT_MAX;
5195
5196 /* can't use iterator, ap isn't initialized yet */
5197 for (i = 0; i < ATA_MAX_DEVICES; i++) {
5198 struct ata_device *dev = &link->device[i];
5199
5200 dev->link = link;
5201 dev->devno = dev - link->device;
Tejun Heo110f66d2009-09-16 04:17:28 +09005202#ifdef CONFIG_ATA_ACPI
5203 dev->gtf_filter = ata_acpi_gtf_filter;
5204#endif
Tejun Heo4fb37a22007-08-06 18:36:23 +09005205 ata_dev_init(dev);
5206 }
5207}
5208
5209/**
5210 * sata_link_init_spd - Initialize link->sata_spd_limit
5211 * @link: Link to configure sata_spd_limit for
5212 *
Mauro Carvalho Chehaba31a6992020-04-14 18:48:55 +02005213 * Initialize ``link->[hw_]sata_spd_limit`` to the currently
Tejun Heo4fb37a22007-08-06 18:36:23 +09005214 * configured value.
5215 *
5216 * LOCKING:
5217 * Kernel thread context (may sleep).
5218 *
5219 * RETURNS:
5220 * 0 on success, -errno on failure.
5221 */
Tejun Heofb7fd612007-09-23 13:14:12 +09005222int sata_link_init_spd(struct ata_link *link)
Tejun Heo4fb37a22007-08-06 18:36:23 +09005223{
Tejun Heo33267322008-02-13 09:15:09 +09005224 u8 spd;
Tejun Heo4fb37a22007-08-06 18:36:23 +09005225 int rc;
5226
Tejun Heod127ea72008-07-31 16:09:34 +09005227 rc = sata_scr_read(link, SCR_CONTROL, &link->saved_scontrol);
Tejun Heo4fb37a22007-08-06 18:36:23 +09005228 if (rc)
5229 return rc;
5230
Tejun Heod127ea72008-07-31 16:09:34 +09005231 spd = (link->saved_scontrol >> 4) & 0xf;
Tejun Heo4fb37a22007-08-06 18:36:23 +09005232 if (spd)
5233 link->hw_sata_spd_limit &= (1 << spd) - 1;
5234
Tejun Heo05944bd2008-08-13 20:19:09 +09005235 ata_force_link_limits(link);
Tejun Heo33267322008-02-13 09:15:09 +09005236
Tejun Heo4fb37a22007-08-06 18:36:23 +09005237 link->sata_spd_limit = link->hw_sata_spd_limit;
5238
5239 return 0;
5240}
5241
5242/**
Tejun Heof3187192007-04-17 23:44:07 +09005243 * ata_port_alloc - allocate and initialize basic ATA port resources
5244 * @host: ATA host this allocated port belongs to
Linus Torvalds1da177e2005-04-16 15:20:36 -07005245 *
Tejun Heof3187192007-04-17 23:44:07 +09005246 * Allocate and initialize basic ATA port resources.
5247 *
5248 * RETURNS:
5249 * Allocate ATA port on success, NULL on failure.
Jeff Garzik0cba6322005-05-30 19:49:12 -04005250 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07005251 * LOCKING:
Tejun Heof3187192007-04-17 23:44:07 +09005252 * Inherited from calling layer (may sleep).
Linus Torvalds1da177e2005-04-16 15:20:36 -07005253 */
Tejun Heof3187192007-04-17 23:44:07 +09005254struct ata_port *ata_port_alloc(struct ata_host *host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005255{
Tejun Heof3187192007-04-17 23:44:07 +09005256 struct ata_port *ap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005257
Tejun Heof3187192007-04-17 23:44:07 +09005258 DPRINTK("ENTER\n");
5259
5260 ap = kzalloc(sizeof(*ap), GFP_KERNEL);
5261 if (!ap)
5262 return NULL;
Jeff Garzik4fca3772011-02-15 01:13:24 -05005263
Maxime Bizon7b3a24c52011-03-16 14:58:32 +01005264 ap->pflags |= ATA_PFLAG_INITIALIZING | ATA_PFLAG_FROZEN;
Jeff Garzikcca39742006-08-24 03:19:22 -04005265 ap->lock = &host->lock;
Tejun Heof3187192007-04-17 23:44:07 +09005266 ap->print_id = -1;
David Milburne628dc92013-05-14 13:48:40 -05005267 ap->local_port_no = -1;
Jeff Garzikcca39742006-08-24 03:19:22 -04005268 ap->host = host;
Tejun Heof3187192007-04-17 23:44:07 +09005269 ap->dev = host->dev;
Borislav Petkovbd5d8252006-06-11 23:17:01 -04005270
5271#if defined(ATA_VERBOSE_DEBUG)
5272 /* turn on all debugging levels */
5273 ap->msg_enable = 0x00FF;
5274#elif defined(ATA_DEBUG)
5275 ap->msg_enable = ATA_MSG_DRV | ATA_MSG_INFO | ATA_MSG_CTL | ATA_MSG_WARN | ATA_MSG_ERR;
Tejun Heo88574552006-06-25 20:00:35 +09005276#else
Borislav Petkov0dd4b212006-06-23 02:29:08 -04005277 ap->msg_enable = ATA_MSG_DRV | ATA_MSG_ERR | ATA_MSG_WARN;
Borislav Petkovbd5d8252006-06-11 23:17:01 -04005278#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005279
Tejun Heoad72cf92010-07-02 10:03:52 +02005280 mutex_init(&ap->scsi_scan_mutex);
David Howells65f27f32006-11-22 14:55:48 +00005281 INIT_DELAYED_WORK(&ap->hotplug_task, ata_scsi_hotplug);
5282 INIT_WORK(&ap->scsi_rescan_task, ata_scsi_dev_rescan);
Tejun Heoa72ec4c2006-01-23 13:09:37 +09005283 INIT_LIST_HEAD(&ap->eh_done_q);
Tejun Heoc6cf9e92006-05-31 18:27:27 +09005284 init_waitqueue_head(&ap->eh_wait_q);
Elias Oltmanns45fabbb2008-09-21 11:54:08 +02005285 init_completion(&ap->park_req_pending);
Kees Cookb93ab332017-10-16 14:56:42 -07005286 timer_setup(&ap->fastdrain_timer, ata_eh_fastdrain_timerfn,
5287 TIMER_DEFERRABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005288
Tejun Heo838df622006-05-15 20:57:44 +09005289 ap->cbl = ATA_CBL_NONE;
Tejun Heo838df622006-05-15 20:57:44 +09005290
Tejun Heo89898052007-08-06 18:36:23 +09005291 ata_link_init(ap, &ap->link, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005292
5293#ifdef ATA_IRQ_TRAP
5294 ap->stats.unhandled_irq = 1;
5295 ap->stats.idle_irq = 1;
5296#endif
Tejun Heo270390e2010-05-10 21:41:35 +02005297 ata_sff_port_init(ap);
5298
Linus Torvalds1da177e2005-04-16 15:20:36 -07005299 return ap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005300}
5301
Taras Kondratiuk2623c7a2018-03-09 08:34:41 +00005302static void ata_devres_release(struct device *gendev, void *res)
Tejun Heof0d36ef2007-01-20 16:00:28 +09005303{
5304 struct ata_host *host = dev_get_drvdata(gendev);
5305 int i;
5306
5307 for (i = 0; i < host->n_ports; i++) {
5308 struct ata_port *ap = host->ports[i];
5309
Tejun Heoecef7252007-04-17 23:44:06 +09005310 if (!ap)
5311 continue;
5312
Tejun Heo49114872007-04-17 23:44:06 +09005313 if (ap->scsi_host)
Tejun Heo1aa506e42007-03-09 19:36:12 +09005314 scsi_host_put(ap->scsi_host);
5315
Taras Kondratiuk2623c7a2018-03-09 08:34:41 +00005316 }
5317
5318 dev_set_drvdata(gendev, NULL);
5319 ata_host_put(host);
5320}
5321
5322static void ata_host_release(struct kref *kref)
5323{
5324 struct ata_host *host = container_of(kref, struct ata_host, kref);
5325 int i;
5326
5327 for (i = 0; i < host->n_ports; i++) {
5328 struct ata_port *ap = host->ports[i];
5329
Tejun Heo633273a2007-09-23 13:19:54 +09005330 kfree(ap->pmp_link);
Tejun Heob1c72912008-07-31 17:02:43 +09005331 kfree(ap->slave_link);
Tejun Heo49114872007-04-17 23:44:06 +09005332 kfree(ap);
Tejun Heo1aa506e42007-03-09 19:36:12 +09005333 host->ports[i] = NULL;
5334 }
Taras Kondratiuk2623c7a2018-03-09 08:34:41 +00005335 kfree(host);
5336}
Tejun Heo1aa506e42007-03-09 19:36:12 +09005337
Taras Kondratiuk2623c7a2018-03-09 08:34:41 +00005338void ata_host_get(struct ata_host *host)
5339{
5340 kref_get(&host->kref);
5341}
5342
5343void ata_host_put(struct ata_host *host)
5344{
5345 kref_put(&host->kref, ata_host_release);
Tejun Heof0d36ef2007-01-20 16:00:28 +09005346}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01005347EXPORT_SYMBOL_GPL(ata_host_put);
Tejun Heof0d36ef2007-01-20 16:00:28 +09005348
Linus Torvalds1da177e2005-04-16 15:20:36 -07005349/**
Tejun Heof3187192007-04-17 23:44:07 +09005350 * ata_host_alloc - allocate and init basic ATA host resources
5351 * @dev: generic device this host is associated with
5352 * @max_ports: maximum number of ATA ports associated with this host
5353 *
5354 * Allocate and initialize basic ATA host resources. LLD calls
5355 * this function to allocate a host, initializes it fully and
5356 * attaches it using ata_host_register().
5357 *
5358 * @max_ports ports are allocated and host->n_ports is
5359 * initialized to @max_ports. The caller is allowed to decrease
5360 * host->n_ports before calling ata_host_register(). The unused
5361 * ports will be automatically freed on registration.
5362 *
5363 * RETURNS:
5364 * Allocate ATA host on success, NULL on failure.
5365 *
5366 * LOCKING:
5367 * Inherited from calling layer (may sleep).
5368 */
5369struct ata_host *ata_host_alloc(struct device *dev, int max_ports)
5370{
5371 struct ata_host *host;
5372 size_t sz;
5373 int i;
Taras Kondratiuk2623c7a2018-03-09 08:34:41 +00005374 void *dr;
Tejun Heof3187192007-04-17 23:44:07 +09005375
5376 DPRINTK("ENTER\n");
5377
Tejun Heof3187192007-04-17 23:44:07 +09005378 /* alloc a container for our list of ATA ports (buses) */
5379 sz = sizeof(struct ata_host) + (max_ports + 1) * sizeof(void *);
Taras Kondratiuk2623c7a2018-03-09 08:34:41 +00005380 host = kzalloc(sz, GFP_KERNEL);
Tejun Heof3187192007-04-17 23:44:07 +09005381 if (!host)
Taras Kondratiuk2623c7a2018-03-09 08:34:41 +00005382 return NULL;
5383
Tejun Heof3187192007-04-17 23:44:07 +09005384 if (!devres_open_group(dev, NULL, GFP_KERNEL))
Colin Ian Kingdafd6c42018-03-27 14:26:01 +01005385 goto err_free;
Tejun Heof3187192007-04-17 23:44:07 +09005386
Taras Kondratiuk2623c7a2018-03-09 08:34:41 +00005387 dr = devres_alloc(ata_devres_release, 0, GFP_KERNEL);
5388 if (!dr)
Tejun Heof3187192007-04-17 23:44:07 +09005389 goto err_out;
5390
Taras Kondratiuk2623c7a2018-03-09 08:34:41 +00005391 devres_add(dev, dr);
Tejun Heof3187192007-04-17 23:44:07 +09005392 dev_set_drvdata(dev, host);
5393
5394 spin_lock_init(&host->lock);
Tejun Heoc0c362b2010-09-06 17:57:14 +02005395 mutex_init(&host->eh_mutex);
Tejun Heof3187192007-04-17 23:44:07 +09005396 host->dev = dev;
5397 host->n_ports = max_ports;
Taras Kondratiuk2623c7a2018-03-09 08:34:41 +00005398 kref_init(&host->kref);
Tejun Heof3187192007-04-17 23:44:07 +09005399
5400 /* allocate ports bound to this host */
5401 for (i = 0; i < max_ports; i++) {
5402 struct ata_port *ap;
5403
5404 ap = ata_port_alloc(host);
5405 if (!ap)
5406 goto err_out;
5407
5408 ap->port_no = i;
5409 host->ports[i] = ap;
5410 }
5411
5412 devres_remove_group(dev, NULL);
5413 return host;
5414
5415 err_out:
5416 devres_release_group(dev, NULL);
Colin Ian Kingdafd6c42018-03-27 14:26:01 +01005417 err_free:
5418 kfree(host);
Tejun Heof3187192007-04-17 23:44:07 +09005419 return NULL;
5420}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01005421EXPORT_SYMBOL_GPL(ata_host_alloc);
Tejun Heof3187192007-04-17 23:44:07 +09005422
5423/**
Tejun Heof5cda252007-04-17 23:44:07 +09005424 * ata_host_alloc_pinfo - alloc host and init with port_info array
5425 * @dev: generic device this host is associated with
5426 * @ppi: array of ATA port_info to initialize host with
5427 * @n_ports: number of ATA ports attached to this host
5428 *
5429 * Allocate ATA host and initialize with info from @ppi. If NULL
5430 * terminated, @ppi may contain fewer entries than @n_ports. The
5431 * last entry will be used for the remaining ports.
5432 *
5433 * RETURNS:
5434 * Allocate ATA host on success, NULL on failure.
5435 *
5436 * LOCKING:
5437 * Inherited from calling layer (may sleep).
5438 */
5439struct ata_host *ata_host_alloc_pinfo(struct device *dev,
5440 const struct ata_port_info * const * ppi,
5441 int n_ports)
5442{
5443 const struct ata_port_info *pi;
5444 struct ata_host *host;
5445 int i, j;
5446
5447 host = ata_host_alloc(dev, n_ports);
5448 if (!host)
5449 return NULL;
5450
5451 for (i = 0, j = 0, pi = NULL; i < host->n_ports; i++) {
5452 struct ata_port *ap = host->ports[i];
5453
5454 if (ppi[j])
5455 pi = ppi[j++];
5456
5457 ap->pio_mask = pi->pio_mask;
5458 ap->mwdma_mask = pi->mwdma_mask;
5459 ap->udma_mask = pi->udma_mask;
5460 ap->flags |= pi->flags;
Tejun Heo0c887582007-08-06 18:36:23 +09005461 ap->link.flags |= pi->link_flags;
Tejun Heof5cda252007-04-17 23:44:07 +09005462 ap->ops = pi->port_ops;
5463
5464 if (!host->ops && (pi->port_ops != &ata_dummy_port_ops))
5465 host->ops = pi->port_ops;
Tejun Heof5cda252007-04-17 23:44:07 +09005466 }
5467
5468 return host;
5469}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01005470EXPORT_SYMBOL_GPL(ata_host_alloc_pinfo);
Tejun Heof5cda252007-04-17 23:44:07 +09005471
Tejun Heo32ebbc02007-11-08 13:09:00 +09005472static void ata_host_stop(struct device *gendev, void *res)
5473{
5474 struct ata_host *host = dev_get_drvdata(gendev);
5475 int i;
5476
5477 WARN_ON(!(host->flags & ATA_HOST_STARTED));
5478
5479 for (i = 0; i < host->n_ports; i++) {
5480 struct ata_port *ap = host->ports[i];
5481
5482 if (ap->ops->port_stop)
5483 ap->ops->port_stop(ap);
5484 }
5485
5486 if (host->ops->host_stop)
5487 host->ops->host_stop(host);
5488}
5489
Tejun Heof5cda252007-04-17 23:44:07 +09005490/**
Tejun Heo029cfd62008-03-25 12:22:49 +09005491 * ata_finalize_port_ops - finalize ata_port_operations
5492 * @ops: ata_port_operations to finalize
5493 *
5494 * An ata_port_operations can inherit from another ops and that
5495 * ops can again inherit from another. This can go on as many
5496 * times as necessary as long as there is no loop in the
5497 * inheritance chain.
5498 *
5499 * Ops tables are finalized when the host is started. NULL or
5500 * unspecified entries are inherited from the closet ancestor
5501 * which has the method and the entry is populated with it.
5502 * After finalization, the ops table directly points to all the
5503 * methods and ->inherits is no longer necessary and cleared.
5504 *
5505 * Using ATA_OP_NULL, inheriting ops can force a method to NULL.
5506 *
5507 * LOCKING:
5508 * None.
5509 */
5510static void ata_finalize_port_ops(struct ata_port_operations *ops)
5511{
Pradeep Singh Rautela2da67652008-05-29 23:28:14 +05305512 static DEFINE_SPINLOCK(lock);
Tejun Heo029cfd62008-03-25 12:22:49 +09005513 const struct ata_port_operations *cur;
5514 void **begin = (void **)ops;
5515 void **end = (void **)&ops->inherits;
5516 void **pp;
5517
5518 if (!ops || !ops->inherits)
5519 return;
5520
5521 spin_lock(&lock);
5522
5523 for (cur = ops->inherits; cur; cur = cur->inherits) {
5524 void **inherit = (void **)cur;
5525
5526 for (pp = begin; pp < end; pp++, inherit++)
5527 if (!*pp)
5528 *pp = *inherit;
5529 }
5530
5531 for (pp = begin; pp < end; pp++)
5532 if (IS_ERR(*pp))
5533 *pp = NULL;
5534
5535 ops->inherits = NULL;
5536
5537 spin_unlock(&lock);
5538}
5539
5540/**
Tejun Heoecef7252007-04-17 23:44:06 +09005541 * ata_host_start - start and freeze ports of an ATA host
5542 * @host: ATA host to start ports for
5543 *
5544 * Start and then freeze ports of @host. Started status is
5545 * recorded in host->flags, so this function can be called
5546 * multiple times. Ports are guaranteed to get started only
Tejun Heof3187192007-04-17 23:44:07 +09005547 * once. If host->ops isn't initialized yet, its set to the
5548 * first non-dummy port ops.
Tejun Heoecef7252007-04-17 23:44:06 +09005549 *
5550 * LOCKING:
5551 * Inherited from calling layer (may sleep).
5552 *
5553 * RETURNS:
5554 * 0 if all ports are started successfully, -errno otherwise.
5555 */
5556int ata_host_start(struct ata_host *host)
5557{
Tejun Heo32ebbc02007-11-08 13:09:00 +09005558 int have_stop = 0;
5559 void *start_dr = NULL;
Tejun Heoecef7252007-04-17 23:44:06 +09005560 int i, rc;
5561
5562 if (host->flags & ATA_HOST_STARTED)
5563 return 0;
5564
Tejun Heo029cfd62008-03-25 12:22:49 +09005565 ata_finalize_port_ops(host->ops);
5566
Tejun Heoecef7252007-04-17 23:44:06 +09005567 for (i = 0; i < host->n_ports; i++) {
5568 struct ata_port *ap = host->ports[i];
5569
Tejun Heo029cfd62008-03-25 12:22:49 +09005570 ata_finalize_port_ops(ap->ops);
5571
Tejun Heof3187192007-04-17 23:44:07 +09005572 if (!host->ops && !ata_port_is_dummy(ap))
5573 host->ops = ap->ops;
5574
Tejun Heo32ebbc02007-11-08 13:09:00 +09005575 if (ap->ops->port_stop)
5576 have_stop = 1;
5577 }
5578
Damien Le Moal355a8032021-08-16 10:44:47 +09005579 if (host->ops && host->ops->host_stop)
Tejun Heo32ebbc02007-11-08 13:09:00 +09005580 have_stop = 1;
5581
5582 if (have_stop) {
5583 start_dr = devres_alloc(ata_host_stop, 0, GFP_KERNEL);
5584 if (!start_dr)
5585 return -ENOMEM;
5586 }
5587
5588 for (i = 0; i < host->n_ports; i++) {
5589 struct ata_port *ap = host->ports[i];
5590
Tejun Heoecef7252007-04-17 23:44:06 +09005591 if (ap->ops->port_start) {
5592 rc = ap->ops->port_start(ap);
5593 if (rc) {
Alan Cox0f9fe9b2007-11-30 15:23:16 +00005594 if (rc != -ENODEV)
Joe Perchesa44fec12011-04-15 15:51:58 -07005595 dev_err(host->dev,
5596 "failed to start port %d (errno=%d)\n",
5597 i, rc);
Tejun Heoecef7252007-04-17 23:44:06 +09005598 goto err_out;
5599 }
5600 }
Tejun Heoecef7252007-04-17 23:44:06 +09005601 ata_eh_freeze_port(ap);
5602 }
5603
Tejun Heo32ebbc02007-11-08 13:09:00 +09005604 if (start_dr)
5605 devres_add(host->dev, start_dr);
Tejun Heoecef7252007-04-17 23:44:06 +09005606 host->flags |= ATA_HOST_STARTED;
5607 return 0;
5608
5609 err_out:
5610 while (--i >= 0) {
5611 struct ata_port *ap = host->ports[i];
5612
5613 if (ap->ops->port_stop)
5614 ap->ops->port_stop(ap);
5615 }
Tejun Heo32ebbc02007-11-08 13:09:00 +09005616 devres_free(start_dr);
Tejun Heoecef7252007-04-17 23:44:06 +09005617 return rc;
5618}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01005619EXPORT_SYMBOL_GPL(ata_host_start);
Tejun Heoecef7252007-04-17 23:44:06 +09005620
5621/**
Mauro Carvalho Chehab94bd5712020-10-23 18:32:55 +02005622 * ata_host_init - Initialize a host struct for sas (ipr, libsas)
Jeff Garzikcca39742006-08-24 03:19:22 -04005623 * @host: host to initialize
5624 * @dev: device host is attached to
Jeff Garzikcca39742006-08-24 03:19:22 -04005625 * @ops: port_ops
Brian Kingb03732f2006-08-07 14:27:10 -05005626 *
Brian Kingb03732f2006-08-07 14:27:10 -05005627 */
Jeff Garzikcca39742006-08-24 03:19:22 -04005628void ata_host_init(struct ata_host *host, struct device *dev,
Dan Williams8d8e7d12012-07-09 21:06:08 -07005629 struct ata_port_operations *ops)
Brian Kingb03732f2006-08-07 14:27:10 -05005630{
Jeff Garzikcca39742006-08-24 03:19:22 -04005631 spin_lock_init(&host->lock);
Tejun Heoc0c362b2010-09-06 17:57:14 +02005632 mutex_init(&host->eh_mutex);
Jens Axboe69278f72018-05-11 12:51:10 -06005633 host->n_tags = ATA_MAX_QUEUE;
Jeff Garzikcca39742006-08-24 03:19:22 -04005634 host->dev = dev;
Jeff Garzikcca39742006-08-24 03:19:22 -04005635 host->ops = ops;
Jason Yan2fa4a322018-05-10 11:05:16 +08005636 kref_init(&host->kref);
Brian Kingb03732f2006-08-07 14:27:10 -05005637}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01005638EXPORT_SYMBOL_GPL(ata_host_init);
Brian Kingb03732f2006-08-07 14:27:10 -05005639
Dan Williams9508a662012-01-18 20:47:01 -08005640void __ata_port_probe(struct ata_port *ap)
5641{
5642 struct ata_eh_info *ehi = &ap->link.eh_info;
5643 unsigned long flags;
5644
5645 /* kick EH for boot probing */
5646 spin_lock_irqsave(ap->lock, flags);
5647
5648 ehi->probe_mask |= ATA_ALL_DEVICES;
5649 ehi->action |= ATA_EH_RESET;
5650 ehi->flags |= ATA_EHI_NO_AUTOPSY | ATA_EHI_QUIET;
5651
5652 ap->pflags &= ~ATA_PFLAG_INITIALIZING;
5653 ap->pflags |= ATA_PFLAG_LOADING;
5654 ata_port_schedule_eh(ap);
5655
5656 spin_unlock_irqrestore(ap->lock, flags);
5657}
5658
James Bottomley238c9cf2011-01-23 08:28:33 -06005659int ata_port_probe(struct ata_port *ap)
Arjan van de Ven79318052009-01-04 05:32:28 -08005660{
James Bottomley238c9cf2011-01-23 08:28:33 -06005661 int rc = 0;
Arjan van de Ven886ad092009-01-09 15:54:07 -08005662
Arjan van de Ven79318052009-01-04 05:32:28 -08005663 if (ap->ops->error_handler) {
Dan Williams9508a662012-01-18 20:47:01 -08005664 __ata_port_probe(ap);
Arjan van de Ven79318052009-01-04 05:32:28 -08005665 ata_port_wait_eh(ap);
5666 } else {
5667 DPRINTK("ata%u: bus probe begin\n", ap->print_id);
5668 rc = ata_bus_probe(ap);
5669 DPRINTK("ata%u: bus probe end\n", ap->print_id);
Arjan van de Ven79318052009-01-04 05:32:28 -08005670 }
James Bottomley238c9cf2011-01-23 08:28:33 -06005671 return rc;
5672}
5673
5674
5675static void async_port_probe(void *data, async_cookie_t cookie)
5676{
5677 struct ata_port *ap = data;
Jeff Garzik4fca3772011-02-15 01:13:24 -05005678
James Bottomley238c9cf2011-01-23 08:28:33 -06005679 /*
5680 * If we're not allowed to scan this host in parallel,
5681 * we need to wait until all previous scans have completed
5682 * before going further.
5683 * Jeff Garzik says this is only within a controller, so we
5684 * don't need to wait for port 0, only for later ports.
5685 */
5686 if (!(ap->host->flags & ATA_HOST_PARALLEL_SCAN) && ap->port_no != 0)
5687 async_synchronize_cookie(cookie);
5688
5689 (void)ata_port_probe(ap);
Arjan van de Venf29d3b22009-01-05 15:07:07 -08005690
5691 /* in order to keep device order, we need to synchronize at this point */
5692 async_synchronize_cookie(cookie);
5693
5694 ata_scsi_scan_host(ap, 1);
Arjan van de Ven79318052009-01-04 05:32:28 -08005695}
James Bottomley238c9cf2011-01-23 08:28:33 -06005696
Brian Kingb03732f2006-08-07 14:27:10 -05005697/**
Tejun Heof3187192007-04-17 23:44:07 +09005698 * ata_host_register - register initialized ATA host
5699 * @host: ATA host to register
5700 * @sht: template for SCSI host
Jeff Garzik0cba6322005-05-30 19:49:12 -04005701 *
Tejun Heof3187192007-04-17 23:44:07 +09005702 * Register initialized ATA host. @host is allocated using
5703 * ata_host_alloc() and fully initialized by LLD. This function
5704 * starts ports, registers @host with ATA and SCSI layers and
5705 * probe registered devices.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005706 *
5707 * LOCKING:
Tejun Heof3187192007-04-17 23:44:07 +09005708 * Inherited from calling layer (may sleep).
Linus Torvalds1da177e2005-04-16 15:20:36 -07005709 *
5710 * RETURNS:
Tejun Heof3187192007-04-17 23:44:07 +09005711 * 0 on success, -errno otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005712 */
Tejun Heof3187192007-04-17 23:44:07 +09005713int ata_host_register(struct ata_host *host, struct scsi_host_template *sht)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005714{
Tejun Heof3187192007-04-17 23:44:07 +09005715 int i, rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005716
Jens Axboe69278f72018-05-11 12:51:10 -06005717 host->n_tags = clamp(sht->can_queue, 1, ATA_MAX_QUEUE);
Kevin Hao1871ee12014-07-12 12:08:24 +08005718
Tejun Heof3187192007-04-17 23:44:07 +09005719 /* host must have been started */
5720 if (!(host->flags & ATA_HOST_STARTED)) {
Joe Perchesa44fec12011-04-15 15:51:58 -07005721 dev_err(host->dev, "BUG: trying to register unstarted host\n");
Tejun Heof3187192007-04-17 23:44:07 +09005722 WARN_ON(1);
5723 return -EINVAL;
Alan Cox02f076a2006-09-26 17:35:32 +01005724 }
Tejun Heof0d36ef2007-01-20 16:00:28 +09005725
Tejun Heof3187192007-04-17 23:44:07 +09005726 /* Blow away unused ports. This happens when LLD can't
5727 * determine the exact number of ports to allocate at
5728 * allocation time.
5729 */
5730 for (i = host->n_ports; host->ports[i]; i++)
5731 kfree(host->ports[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005732
Tejun Heof3187192007-04-17 23:44:07 +09005733 /* give ports names and add SCSI hosts */
David Milburne628dc92013-05-14 13:48:40 -05005734 for (i = 0; i < host->n_ports; i++) {
Dan Williams85d67252012-03-10 23:28:46 -08005735 host->ports[i]->print_id = atomic_inc_return(&ata_print_id);
David Milburne628dc92013-05-14 13:48:40 -05005736 host->ports[i]->local_port_no = i + 1;
5737 }
Jeff Garzik4fca3772011-02-15 01:13:24 -05005738
Gwendal Grignoud9027472010-05-25 12:31:38 -07005739 /* Create associated sysfs transport objects */
5740 for (i = 0; i < host->n_ports; i++) {
5741 rc = ata_tport_add(host->dev,host->ports[i]);
5742 if (rc) {
5743 goto err_tadd;
5744 }
5745 }
5746
Tejun Heof3187192007-04-17 23:44:07 +09005747 rc = ata_scsi_add_hosts(host, sht);
Tejun Heoecef7252007-04-17 23:44:06 +09005748 if (rc)
Gwendal Grignoud9027472010-05-25 12:31:38 -07005749 goto err_tadd;
Tejun Heoecef7252007-04-17 23:44:06 +09005750
Tejun Heof3187192007-04-17 23:44:07 +09005751 /* set cable, sata_spd_limit and report */
Jeff Garzikcca39742006-08-24 03:19:22 -04005752 for (i = 0; i < host->n_ports; i++) {
5753 struct ata_port *ap = host->ports[i];
Tejun Heof3187192007-04-17 23:44:07 +09005754 unsigned long xfer_mask;
5755
5756 /* set SATA cable type if still unset */
5757 if (ap->cbl == ATA_CBL_NONE && (ap->flags & ATA_FLAG_SATA))
5758 ap->cbl = ATA_CBL_SATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005759
Tejun Heo5a04bf42006-05-31 18:27:38 +09005760 /* init sata_spd_limit to the current value */
Tejun Heo4fb37a22007-08-06 18:36:23 +09005761 sata_link_init_spd(&ap->link);
Tejun Heob1c72912008-07-31 17:02:43 +09005762 if (ap->slave_link)
5763 sata_link_init_spd(ap->slave_link);
Tejun Heo5a04bf42006-05-31 18:27:38 +09005764
Tejun Heocbcdd872007-08-18 13:14:55 +09005765 /* print per-port info to dmesg */
Tejun Heof3187192007-04-17 23:44:07 +09005766 xfer_mask = ata_pack_xfermask(ap->pio_mask, ap->mwdma_mask,
5767 ap->udma_mask);
5768
Tejun Heoabf6e8e2007-10-09 14:57:25 +09005769 if (!ata_port_is_dummy(ap)) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07005770 ata_port_info(ap, "%cATA max %s %s\n",
5771 (ap->flags & ATA_FLAG_SATA) ? 'S' : 'P',
5772 ata_mode_string(xfer_mask),
5773 ap->link.eh_info.desc);
Tejun Heoabf6e8e2007-10-09 14:57:25 +09005774 ata_ehi_clear_desc(&ap->link.eh_info);
5775 } else
Joe Perchesa9a79df2011-04-15 15:51:59 -07005776 ata_port_info(ap, "DUMMY\n");
Tejun Heof3187192007-04-17 23:44:07 +09005777 }
5778
Vegard Nossumf6005352009-04-08 18:19:39 +02005779 /* perform each probe asynchronously */
Tejun Heof3187192007-04-17 23:44:07 +09005780 for (i = 0; i < host->n_ports; i++) {
5781 struct ata_port *ap = host->ports[i];
Kai-Heng Fengb5292112020-06-03 15:48:19 +08005782 ap->cookie = async_schedule(async_port_probe, ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005783 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005784
Tejun Heof3187192007-04-17 23:44:07 +09005785 return 0;
Gwendal Grignoud9027472010-05-25 12:31:38 -07005786
5787 err_tadd:
5788 while (--i >= 0) {
5789 ata_tport_delete(host->ports[i]);
5790 }
5791 return rc;
5792
Tejun Heof3187192007-04-17 23:44:07 +09005793}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01005794EXPORT_SYMBOL_GPL(ata_host_register);
Tejun Heof3187192007-04-17 23:44:07 +09005795
5796/**
Tejun Heof5cda252007-04-17 23:44:07 +09005797 * ata_host_activate - start host, request IRQ and register it
5798 * @host: target ATA host
5799 * @irq: IRQ to request
5800 * @irq_handler: irq_handler used when requesting IRQ
5801 * @irq_flags: irq_flags used when requesting IRQ
5802 * @sht: scsi_host_template to use when registering the host
5803 *
5804 * After allocating an ATA host and initializing it, most libata
5805 * LLDs perform three steps to activate the host - start host,
Masanari Iidac9b55602016-04-13 23:36:27 +09005806 * request IRQ and register it. This helper takes necessary
Tejun Heof5cda252007-04-17 23:44:07 +09005807 * arguments and performs the three steps in one go.
5808 *
Paul Mundt3d46b2e2007-11-08 11:14:56 +09005809 * An invalid IRQ skips the IRQ registration and expects the host to
5810 * have set polling mode on the port. In this case, @irq_handler
5811 * should be NULL.
5812 *
Tejun Heof5cda252007-04-17 23:44:07 +09005813 * LOCKING:
5814 * Inherited from calling layer (may sleep).
5815 *
5816 * RETURNS:
5817 * 0 on success, -errno otherwise.
5818 */
5819int ata_host_activate(struct ata_host *host, int irq,
5820 irq_handler_t irq_handler, unsigned long irq_flags,
5821 struct scsi_host_template *sht)
5822{
Tejun Heocbcdd872007-08-18 13:14:55 +09005823 int i, rc;
Heiner Kallweit7e22c002015-12-06 21:56:33 +01005824 char *irq_desc;
Tejun Heof5cda252007-04-17 23:44:07 +09005825
5826 rc = ata_host_start(host);
5827 if (rc)
5828 return rc;
5829
Paul Mundt3d46b2e2007-11-08 11:14:56 +09005830 /* Special case for polling mode */
5831 if (!irq) {
5832 WARN_ON(irq_handler);
5833 return ata_host_register(host, sht);
5834 }
5835
Heiner Kallweit7e22c002015-12-06 21:56:33 +01005836 irq_desc = devm_kasprintf(host->dev, GFP_KERNEL, "%s[%s]",
5837 dev_driver_string(host->dev),
5838 dev_name(host->dev));
5839 if (!irq_desc)
5840 return -ENOMEM;
5841
Tejun Heof5cda252007-04-17 23:44:07 +09005842 rc = devm_request_irq(host->dev, irq, irq_handler, irq_flags,
Heiner Kallweit7e22c002015-12-06 21:56:33 +01005843 irq_desc, host);
Tejun Heof5cda252007-04-17 23:44:07 +09005844 if (rc)
5845 return rc;
5846
Tejun Heocbcdd872007-08-18 13:14:55 +09005847 for (i = 0; i < host->n_ports; i++)
5848 ata_port_desc(host->ports[i], "irq %d", irq);
Tejun Heo40318262007-07-03 01:38:47 +09005849
Tejun Heof5cda252007-04-17 23:44:07 +09005850 rc = ata_host_register(host, sht);
5851 /* if failed, just free the IRQ and leave ports alone */
5852 if (rc)
5853 devm_free_irq(host->dev, irq, host);
5854
5855 return rc;
5856}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01005857EXPORT_SYMBOL_GPL(ata_host_activate);
Tejun Heof5cda252007-04-17 23:44:07 +09005858
5859/**
Masanari Iidac9b55602016-04-13 23:36:27 +09005860 * ata_port_detach - Detach ATA port in preparation of device removal
Tejun Heo720ba122006-05-31 18:28:13 +09005861 * @ap: ATA port to be detached
5862 *
5863 * Detach all ATA devices and the associated SCSI devices of @ap;
5864 * then, remove the associated SCSI host. @ap is guaranteed to
5865 * be quiescent on return from this function.
5866 *
5867 * LOCKING:
5868 * Kernel thread context (may sleep).
5869 */
Adrian Bunk741b7762007-10-24 18:23:06 +02005870static void ata_port_detach(struct ata_port *ap)
Tejun Heo720ba122006-05-31 18:28:13 +09005871{
5872 unsigned long flags;
Levente Kurusaa6f9bf42014-05-06 15:57:48 +02005873 struct ata_link *link;
5874 struct ata_device *dev;
Tejun Heo720ba122006-05-31 18:28:13 +09005875
5876 if (!ap->ops->error_handler)
Tejun Heoc3cf30a2006-08-05 03:59:11 +09005877 goto skip_eh;
Tejun Heo720ba122006-05-31 18:28:13 +09005878
5879 /* tell EH we're leaving & flush EH */
Jeff Garzikba6a1302006-06-22 23:46:10 -04005880 spin_lock_irqsave(ap->lock, flags);
Tejun Heob51e9e52006-06-29 01:29:30 +09005881 ap->pflags |= ATA_PFLAG_UNLOADING;
Tejun Heoece180d2008-11-03 20:04:37 +09005882 ata_port_schedule_eh(ap);
Jeff Garzikba6a1302006-06-22 23:46:10 -04005883 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heo720ba122006-05-31 18:28:13 +09005884
Tejun Heoece180d2008-11-03 20:04:37 +09005885 /* wait till EH commits suicide */
Tejun Heo720ba122006-05-31 18:28:13 +09005886 ata_port_wait_eh(ap);
5887
Tejun Heoece180d2008-11-03 20:04:37 +09005888 /* it better be dead now */
5889 WARN_ON(!(ap->pflags & ATA_PFLAG_UNLOADED));
Tejun Heo720ba122006-05-31 18:28:13 +09005890
Tejun Heoafe2c512010-12-14 16:21:17 +01005891 cancel_delayed_work_sync(&ap->hotplug_task);
Tejun Heo720ba122006-05-31 18:28:13 +09005892
Tejun Heoc3cf30a2006-08-05 03:59:11 +09005893 skip_eh:
Levente Kurusaa6f9bf42014-05-06 15:57:48 +02005894 /* clean up zpodd on port removal */
5895 ata_for_each_link(link, ap, HOST_FIRST) {
5896 ata_for_each_dev(dev, link, ALL) {
5897 if (zpodd_dev_enabled(dev))
5898 zpodd_exit(dev);
5899 }
5900 }
Gwendal Grignoud9027472010-05-25 12:31:38 -07005901 if (ap->pmp_link) {
5902 int i;
5903 for (i = 0; i < SATA_PMP_MAX_PORTS; i++)
5904 ata_tlink_delete(&ap->pmp_link[i]);
5905 }
Tejun Heo720ba122006-05-31 18:28:13 +09005906 /* remove the associated SCSI host */
Jeff Garzikcca39742006-08-24 03:19:22 -04005907 scsi_remove_host(ap->scsi_host);
Rafael J. Wysockic5700762013-11-25 13:19:01 +01005908 ata_tport_delete(ap);
Tejun Heo720ba122006-05-31 18:28:13 +09005909}
5910
5911/**
Tejun Heo0529c1592007-01-20 16:00:26 +09005912 * ata_host_detach - Detach all ports of an ATA host
5913 * @host: Host to detach
5914 *
5915 * Detach all ports of @host.
5916 *
5917 * LOCKING:
5918 * Kernel thread context (may sleep).
5919 */
5920void ata_host_detach(struct ata_host *host)
5921{
5922 int i;
5923
Kai-Heng Fengb5292112020-06-03 15:48:19 +08005924 for (i = 0; i < host->n_ports; i++) {
5925 /* Ensure ata_port probe has completed */
5926 async_synchronize_cookie(host->ports[i]->cookie + 1);
Tejun Heo0529c1592007-01-20 16:00:26 +09005927 ata_port_detach(host->ports[i]);
Kai-Heng Fengb5292112020-06-03 15:48:19 +08005928 }
Tejun Heo562f0c22007-12-15 15:05:01 +09005929
5930 /* the host is dead now, dissociate ACPI */
5931 ata_acpi_dissociate(host);
Tejun Heo0529c1592007-01-20 16:00:26 +09005932}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01005933EXPORT_SYMBOL_GPL(ata_host_detach);
Tejun Heo0529c1592007-01-20 16:00:26 +09005934
Jeff Garzik374b1872005-08-30 05:42:52 -04005935#ifdef CONFIG_PCI
5936
Edward Falk0baab862005-06-02 18:17:13 -04005937/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07005938 * ata_pci_remove_one - PCI layer callback for device removal
5939 * @pdev: PCI device that was removed
5940 *
Tejun Heob878ca52007-01-20 16:00:28 +09005941 * PCI layer indicates to libata via this hook that hot-unplug or
5942 * module unload event has occurred. Detach all ports. Resource
5943 * release is handled via devres.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005944 *
5945 * LOCKING:
5946 * Inherited from PCI layer (may sleep).
5947 */
Tejun Heof0d36ef2007-01-20 16:00:28 +09005948void ata_pci_remove_one(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005949{
Brian Norris04a3f5b2012-12-03 10:34:41 -08005950 struct ata_host *host = pci_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005951
Tejun Heob878ca52007-01-20 16:00:28 +09005952 ata_host_detach(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005953}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01005954EXPORT_SYMBOL_GPL(ata_pci_remove_one);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005955
Prabhakar Kushwaha10a663a2020-01-25 03:37:29 +00005956void ata_pci_shutdown_one(struct pci_dev *pdev)
5957{
5958 struct ata_host *host = pci_get_drvdata(pdev);
5959 int i;
5960
5961 for (i = 0; i < host->n_ports; i++) {
5962 struct ata_port *ap = host->ports[i];
5963
5964 ap->pflags |= ATA_PFLAG_FROZEN;
5965
5966 /* Disable port interrupts */
5967 if (ap->ops->freeze)
5968 ap->ops->freeze(ap);
5969
5970 /* Stop the port DMA engines */
5971 if (ap->ops->port_stop)
5972 ap->ops->port_stop(ap);
5973 }
5974}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01005975EXPORT_SYMBOL_GPL(ata_pci_shutdown_one);
Prabhakar Kushwaha10a663a2020-01-25 03:37:29 +00005976
Linus Torvalds1da177e2005-04-16 15:20:36 -07005977/* move to PCI subsystem */
Jeff Garzik057ace52005-10-22 14:27:05 -04005978int pci_test_config_bits(struct pci_dev *pdev, const struct pci_bits *bits)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005979{
5980 unsigned long tmp = 0;
5981
5982 switch (bits->width) {
5983 case 1: {
5984 u8 tmp8 = 0;
5985 pci_read_config_byte(pdev, bits->reg, &tmp8);
5986 tmp = tmp8;
5987 break;
5988 }
5989 case 2: {
5990 u16 tmp16 = 0;
5991 pci_read_config_word(pdev, bits->reg, &tmp16);
5992 tmp = tmp16;
5993 break;
5994 }
5995 case 4: {
5996 u32 tmp32 = 0;
5997 pci_read_config_dword(pdev, bits->reg, &tmp32);
5998 tmp = tmp32;
5999 break;
6000 }
6001
6002 default:
6003 return -EINVAL;
6004 }
6005
6006 tmp &= bits->mask;
6007
6008 return (tmp == bits->val) ? 1 : 0;
6009}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01006010EXPORT_SYMBOL_GPL(pci_test_config_bits);
Jens Axboe9b847542006-01-06 09:28:07 +01006011
Tejun Heo6ffa01d2007-03-02 17:32:47 +09006012#ifdef CONFIG_PM
Tejun Heo3c5100c2006-07-26 16:58:33 +09006013void ata_pci_device_do_suspend(struct pci_dev *pdev, pm_message_t mesg)
Jens Axboe9b847542006-01-06 09:28:07 +01006014{
6015 pci_save_state(pdev);
Tejun Heo4c90d972007-02-20 18:14:48 +09006016 pci_disable_device(pdev);
Tejun Heo500530f2006-07-03 16:07:27 +09006017
Rafael J. Wysocki3a2d5b72008-02-23 19:13:25 +01006018 if (mesg.event & PM_EVENT_SLEEP)
Tejun Heo500530f2006-07-03 16:07:27 +09006019 pci_set_power_state(pdev, PCI_D3hot);
Jens Axboe9b847542006-01-06 09:28:07 +01006020}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01006021EXPORT_SYMBOL_GPL(ata_pci_device_do_suspend);
Jens Axboe9b847542006-01-06 09:28:07 +01006022
Tejun Heo553c4aa2006-12-26 19:39:50 +09006023int ata_pci_device_do_resume(struct pci_dev *pdev)
Jens Axboe9b847542006-01-06 09:28:07 +01006024{
Tejun Heo553c4aa2006-12-26 19:39:50 +09006025 int rc;
6026
Jens Axboe9b847542006-01-06 09:28:07 +01006027 pci_set_power_state(pdev, PCI_D0);
6028 pci_restore_state(pdev);
Tejun Heo553c4aa2006-12-26 19:39:50 +09006029
Tejun Heob878ca52007-01-20 16:00:28 +09006030 rc = pcim_enable_device(pdev);
Tejun Heo553c4aa2006-12-26 19:39:50 +09006031 if (rc) {
Joe Perchesa44fec12011-04-15 15:51:58 -07006032 dev_err(&pdev->dev,
6033 "failed to enable device after resume (%d)\n", rc);
Tejun Heo553c4aa2006-12-26 19:39:50 +09006034 return rc;
6035 }
6036
Jens Axboe9b847542006-01-06 09:28:07 +01006037 pci_set_master(pdev);
Tejun Heo553c4aa2006-12-26 19:39:50 +09006038 return 0;
Tejun Heo500530f2006-07-03 16:07:27 +09006039}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01006040EXPORT_SYMBOL_GPL(ata_pci_device_do_resume);
Tejun Heo500530f2006-07-03 16:07:27 +09006041
Tejun Heo3c5100c2006-07-26 16:58:33 +09006042int ata_pci_device_suspend(struct pci_dev *pdev, pm_message_t mesg)
Tejun Heo500530f2006-07-03 16:07:27 +09006043{
Brian Norris04a3f5b2012-12-03 10:34:41 -08006044 struct ata_host *host = pci_get_drvdata(pdev);
Tejun Heo500530f2006-07-03 16:07:27 +09006045 int rc = 0;
6046
Jeff Garzikcca39742006-08-24 03:19:22 -04006047 rc = ata_host_suspend(host, mesg);
Tejun Heo500530f2006-07-03 16:07:27 +09006048 if (rc)
6049 return rc;
6050
Tejun Heo3c5100c2006-07-26 16:58:33 +09006051 ata_pci_device_do_suspend(pdev, mesg);
Tejun Heo500530f2006-07-03 16:07:27 +09006052
6053 return 0;
6054}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01006055EXPORT_SYMBOL_GPL(ata_pci_device_suspend);
Tejun Heo500530f2006-07-03 16:07:27 +09006056
6057int ata_pci_device_resume(struct pci_dev *pdev)
6058{
Brian Norris04a3f5b2012-12-03 10:34:41 -08006059 struct ata_host *host = pci_get_drvdata(pdev);
Tejun Heo553c4aa2006-12-26 19:39:50 +09006060 int rc;
Tejun Heo500530f2006-07-03 16:07:27 +09006061
Tejun Heo553c4aa2006-12-26 19:39:50 +09006062 rc = ata_pci_device_do_resume(pdev);
6063 if (rc == 0)
6064 ata_host_resume(host);
6065 return rc;
Jens Axboe9b847542006-01-06 09:28:07 +01006066}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01006067EXPORT_SYMBOL_GPL(ata_pci_device_resume);
Tejun Heo6ffa01d2007-03-02 17:32:47 +09006068#endif /* CONFIG_PM */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006069#endif /* CONFIG_PCI */
6070
Brian Norrisb7db04d2012-11-02 12:29:32 -07006071/**
6072 * ata_platform_remove_one - Platform layer callback for device removal
6073 * @pdev: Platform device that was removed
6074 *
6075 * Platform layer indicates to libata via this hook that hot-unplug or
6076 * module unload event has occurred. Detach all ports. Resource
6077 * release is handled via devres.
6078 *
6079 * LOCKING:
6080 * Inherited from platform layer (may sleep).
6081 */
6082int ata_platform_remove_one(struct platform_device *pdev)
6083{
6084 struct ata_host *host = platform_get_drvdata(pdev);
6085
6086 ata_host_detach(host);
6087
6088 return 0;
6089}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01006090EXPORT_SYMBOL_GPL(ata_platform_remove_one);
Brian Norrisb7db04d2012-11-02 12:29:32 -07006091
Bartlomiej Zolnierkiewiczbf89b0b2020-03-26 16:58:22 +01006092#ifdef CONFIG_ATA_FORCE
Tejun Heo33267322008-02-13 09:15:09 +09006093static int __init ata_parse_force_one(char **cur,
6094 struct ata_force_ent *force_ent,
6095 const char **reason)
6096{
Rasmus Villemoes0f5f2642015-06-09 15:33:19 +02006097 static const struct ata_force_param force_tbl[] __initconst = {
Tejun Heo33267322008-02-13 09:15:09 +09006098 { "40c", .cbl = ATA_CBL_PATA40 },
6099 { "80c", .cbl = ATA_CBL_PATA80 },
6100 { "short40c", .cbl = ATA_CBL_PATA40_SHORT },
6101 { "unk", .cbl = ATA_CBL_PATA_UNK },
6102 { "ign", .cbl = ATA_CBL_PATA_IGN },
6103 { "sata", .cbl = ATA_CBL_SATA },
6104 { "1.5Gbps", .spd_limit = 1 },
6105 { "3.0Gbps", .spd_limit = 2 },
6106 { "noncq", .horkage_on = ATA_HORKAGE_NONCQ },
6107 { "ncq", .horkage_off = ATA_HORKAGE_NONCQ },
Martin K. Petersend7b16e42015-05-04 21:54:18 -04006108 { "noncqtrim", .horkage_on = ATA_HORKAGE_NO_NCQ_TRIM },
6109 { "ncqtrim", .horkage_off = ATA_HORKAGE_NO_NCQ_TRIM },
Tejun Heo43c9c592010-05-23 12:59:11 +02006110 { "dump_id", .horkage_on = ATA_HORKAGE_DUMP_ID },
Tejun Heo33267322008-02-13 09:15:09 +09006111 { "pio0", .xfer_mask = 1 << (ATA_SHIFT_PIO + 0) },
6112 { "pio1", .xfer_mask = 1 << (ATA_SHIFT_PIO + 1) },
6113 { "pio2", .xfer_mask = 1 << (ATA_SHIFT_PIO + 2) },
6114 { "pio3", .xfer_mask = 1 << (ATA_SHIFT_PIO + 3) },
6115 { "pio4", .xfer_mask = 1 << (ATA_SHIFT_PIO + 4) },
6116 { "pio5", .xfer_mask = 1 << (ATA_SHIFT_PIO + 5) },
6117 { "pio6", .xfer_mask = 1 << (ATA_SHIFT_PIO + 6) },
6118 { "mwdma0", .xfer_mask = 1 << (ATA_SHIFT_MWDMA + 0) },
6119 { "mwdma1", .xfer_mask = 1 << (ATA_SHIFT_MWDMA + 1) },
6120 { "mwdma2", .xfer_mask = 1 << (ATA_SHIFT_MWDMA + 2) },
6121 { "mwdma3", .xfer_mask = 1 << (ATA_SHIFT_MWDMA + 3) },
6122 { "mwdma4", .xfer_mask = 1 << (ATA_SHIFT_MWDMA + 4) },
6123 { "udma0", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 0) },
6124 { "udma16", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 0) },
6125 { "udma/16", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 0) },
6126 { "udma1", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 1) },
6127 { "udma25", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 1) },
6128 { "udma/25", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 1) },
6129 { "udma2", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 2) },
6130 { "udma33", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 2) },
6131 { "udma/33", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 2) },
6132 { "udma3", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 3) },
6133 { "udma44", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 3) },
6134 { "udma/44", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 3) },
6135 { "udma4", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 4) },
6136 { "udma66", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 4) },
6137 { "udma/66", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 4) },
6138 { "udma5", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 5) },
6139 { "udma100", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 5) },
6140 { "udma/100", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 5) },
6141 { "udma6", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 6) },
6142 { "udma133", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 6) },
6143 { "udma/133", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 6) },
6144 { "udma7", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 7) },
Tejun Heo05944bd2008-08-13 20:19:09 +09006145 { "nohrst", .lflags = ATA_LFLAG_NO_HRST },
6146 { "nosrst", .lflags = ATA_LFLAG_NO_SRST },
6147 { "norst", .lflags = ATA_LFLAG_NO_HRST | ATA_LFLAG_NO_SRST },
Dan Williamsca6d43b2012-06-21 23:41:41 -07006148 { "rstonce", .lflags = ATA_LFLAG_RST_ONCE },
Vincent Pelletier966fbe12013-05-21 22:30:58 +02006149 { "atapi_dmadir", .horkage_on = ATA_HORKAGE_ATAPI_DMADIR },
Robin H. Johnsonb8bd6dc2013-12-16 09:31:19 -08006150 { "disable", .horkage_on = ATA_HORKAGE_DISABLE },
Tejun Heo33267322008-02-13 09:15:09 +09006151 };
6152 char *start = *cur, *p = *cur;
6153 char *id, *val, *endp;
6154 const struct ata_force_param *match_fp = NULL;
6155 int nr_matches = 0, i;
6156
6157 /* find where this param ends and update *cur */
6158 while (*p != '\0' && *p != ',')
6159 p++;
6160
6161 if (*p == '\0')
6162 *cur = p;
6163 else
6164 *cur = p + 1;
6165
6166 *p = '\0';
6167
6168 /* parse */
6169 p = strchr(start, ':');
6170 if (!p) {
6171 val = strstrip(start);
6172 goto parse_val;
6173 }
6174 *p = '\0';
6175
6176 id = strstrip(start);
6177 val = strstrip(p + 1);
6178
6179 /* parse id */
6180 p = strchr(id, '.');
6181 if (p) {
6182 *p++ = '\0';
6183 force_ent->device = simple_strtoul(p, &endp, 10);
6184 if (p == endp || *endp != '\0') {
6185 *reason = "invalid device";
6186 return -EINVAL;
6187 }
6188 }
6189
6190 force_ent->port = simple_strtoul(id, &endp, 10);
Tejun Heof7cf69a2017-05-31 14:26:26 -04006191 if (id == endp || *endp != '\0') {
Tejun Heo33267322008-02-13 09:15:09 +09006192 *reason = "invalid port/link";
6193 return -EINVAL;
6194 }
6195
6196 parse_val:
6197 /* parse val, allow shortcuts so that both 1.5 and 1.5Gbps work */
6198 for (i = 0; i < ARRAY_SIZE(force_tbl); i++) {
6199 const struct ata_force_param *fp = &force_tbl[i];
6200
6201 if (strncasecmp(val, fp->name, strlen(val)))
6202 continue;
6203
6204 nr_matches++;
6205 match_fp = fp;
6206
6207 if (strcasecmp(val, fp->name) == 0) {
6208 nr_matches = 1;
6209 break;
6210 }
6211 }
6212
6213 if (!nr_matches) {
6214 *reason = "unknown value";
6215 return -EINVAL;
6216 }
6217 if (nr_matches > 1) {
Arvind Yadav9de55352017-10-28 23:51:47 +05306218 *reason = "ambiguous value";
Tejun Heo33267322008-02-13 09:15:09 +09006219 return -EINVAL;
6220 }
6221
6222 force_ent->param = *match_fp;
6223
6224 return 0;
6225}
6226
6227static void __init ata_parse_force_param(void)
6228{
6229 int idx = 0, size = 1;
6230 int last_port = -1, last_device = -1;
6231 char *p, *cur, *next;
6232
6233 /* calculate maximum number of params and allocate force_tbl */
6234 for (p = ata_force_param_buf; *p; p++)
6235 if (*p == ',')
6236 size++;
6237
Kees Cook6396bb22018-06-12 14:03:40 -07006238 ata_force_tbl = kcalloc(size, sizeof(ata_force_tbl[0]), GFP_KERNEL);
Tejun Heo33267322008-02-13 09:15:09 +09006239 if (!ata_force_tbl) {
6240 printk(KERN_WARNING "ata: failed to extend force table, "
6241 "libata.force ignored\n");
6242 return;
6243 }
6244
6245 /* parse and populate the table */
6246 for (cur = ata_force_param_buf; *cur != '\0'; cur = next) {
6247 const char *reason = "";
6248 struct ata_force_ent te = { .port = -1, .device = -1 };
6249
6250 next = cur;
6251 if (ata_parse_force_one(&next, &te, &reason)) {
6252 printk(KERN_WARNING "ata: failed to parse force "
6253 "parameter \"%s\" (%s)\n",
6254 cur, reason);
6255 continue;
6256 }
6257
6258 if (te.port == -1) {
6259 te.port = last_port;
6260 te.device = last_device;
6261 }
6262
6263 ata_force_tbl[idx++] = te;
6264
6265 last_port = te.port;
6266 last_device = te.device;
6267 }
6268
6269 ata_force_tbl_size = idx;
6270}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006271
Bartlomiej Zolnierkiewiczbf89b0b2020-03-26 16:58:22 +01006272static void ata_free_force_param(void)
6273{
6274 kfree(ata_force_tbl);
6275}
6276#else
6277static inline void ata_parse_force_param(void) { }
6278static inline void ata_free_force_param(void) { }
6279#endif
6280
Linus Torvalds1da177e2005-04-16 15:20:36 -07006281static int __init ata_init(void)
6282{
Gwendal Grignoud9027472010-05-25 12:31:38 -07006283 int rc;
Tejun Heo270390e2010-05-10 21:41:35 +02006284
Tejun Heo33267322008-02-13 09:15:09 +09006285 ata_parse_force_param();
6286
Tejun Heo270390e2010-05-10 21:41:35 +02006287 rc = ata_sff_init();
Tejun Heoad72cf92010-07-02 10:03:52 +02006288 if (rc) {
Bartlomiej Zolnierkiewiczbf89b0b2020-03-26 16:58:22 +01006289 ata_free_force_param();
Tejun Heoad72cf92010-07-02 10:03:52 +02006290 return rc;
6291 }
Tejun Heo453b07a2006-05-31 18:27:42 +09006292
Gwendal Grignoud9027472010-05-25 12:31:38 -07006293 libata_transport_init();
6294 ata_scsi_transport_template = ata_attach_transport();
6295 if (!ata_scsi_transport_template) {
6296 ata_sff_exit();
6297 rc = -ENOMEM;
6298 goto err_out;
Jeff Garzik4fca3772011-02-15 01:13:24 -05006299 }
Gwendal Grignoud9027472010-05-25 12:31:38 -07006300
Linus Torvalds1da177e2005-04-16 15:20:36 -07006301 printk(KERN_DEBUG "libata version " DRV_VERSION " loaded.\n");
6302 return 0;
Gwendal Grignoud9027472010-05-25 12:31:38 -07006303
6304err_out:
6305 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006306}
6307
6308static void __exit ata_exit(void)
6309{
Gwendal Grignoud9027472010-05-25 12:31:38 -07006310 ata_release_transport(ata_scsi_transport_template);
6311 libata_transport_exit();
Tejun Heo270390e2010-05-10 21:41:35 +02006312 ata_sff_exit();
Bartlomiej Zolnierkiewiczbf89b0b2020-03-26 16:58:22 +01006313 ata_free_force_param();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006314}
6315
Brian Kinga4625082006-11-13 16:32:36 -06006316subsys_initcall(ata_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006317module_exit(ata_exit);
6318
Akinobu Mita9990b6f2010-04-12 21:11:41 +09006319static DEFINE_RATELIMIT_STATE(ratelimit, HZ / 5, 1);
Jeff Garzik67846b32005-10-05 02:58:32 -04006320
6321int ata_ratelimit(void)
6322{
Akinobu Mita9990b6f2010-04-12 21:11:41 +09006323 return __ratelimit(&ratelimit);
Jeff Garzik67846b32005-10-05 02:58:32 -04006324}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01006325EXPORT_SYMBOL_GPL(ata_ratelimit);
Jeff Garzik67846b32005-10-05 02:58:32 -04006326
Tejun Heoc0c362b2010-09-06 17:57:14 +02006327/**
6328 * ata_msleep - ATA EH owner aware msleep
6329 * @ap: ATA port to attribute the sleep to
6330 * @msecs: duration to sleep in milliseconds
6331 *
6332 * Sleeps @msecs. If the current task is owner of @ap's EH, the
6333 * ownership is released before going to sleep and reacquired
6334 * after the sleep is complete. IOW, other ports sharing the
6335 * @ap->host will be allowed to own the EH while this task is
6336 * sleeping.
6337 *
6338 * LOCKING:
6339 * Might sleep.
6340 */
Tejun Heo97750ce2010-09-06 17:56:29 +02006341void ata_msleep(struct ata_port *ap, unsigned int msecs)
6342{
Tejun Heoc0c362b2010-09-06 17:57:14 +02006343 bool owns_eh = ap && ap->host->eh_owner == current;
6344
6345 if (owns_eh)
6346 ata_eh_release(ap);
6347
Anil Veliyankara Madam848c3922016-01-07 21:18:52 -08006348 if (msecs < 20) {
6349 unsigned long usecs = msecs * USEC_PER_MSEC;
6350 usleep_range(usecs, usecs + 50);
6351 } else {
6352 msleep(msecs);
6353 }
Tejun Heoc0c362b2010-09-06 17:57:14 +02006354
6355 if (owns_eh)
6356 ata_eh_acquire(ap);
Tejun Heo97750ce2010-09-06 17:56:29 +02006357}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01006358EXPORT_SYMBOL_GPL(ata_msleep);
Tejun Heo97750ce2010-09-06 17:56:29 +02006359
Tejun Heoc22daff2006-04-11 22:22:29 +09006360/**
6361 * ata_wait_register - wait until register value changes
Tejun Heo97750ce2010-09-06 17:56:29 +02006362 * @ap: ATA port to wait register for, can be NULL
Tejun Heoc22daff2006-04-11 22:22:29 +09006363 * @reg: IO-mapped register
6364 * @mask: Mask to apply to read register value
6365 * @val: Wait condition
Tejun Heo341c2c92008-05-20 02:17:51 +09006366 * @interval: polling interval in milliseconds
6367 * @timeout: timeout in milliseconds
Tejun Heoc22daff2006-04-11 22:22:29 +09006368 *
6369 * Waiting for some bits of register to change is a common
6370 * operation for ATA controllers. This function reads 32bit LE
6371 * IO-mapped register @reg and tests for the following condition.
6372 *
6373 * (*@reg & mask) != val
6374 *
6375 * If the condition is met, it returns; otherwise, the process is
6376 * repeated after @interval_msec until timeout.
6377 *
6378 * LOCKING:
6379 * Kernel thread context (may sleep)
6380 *
6381 * RETURNS:
6382 * The final register value.
6383 */
Tejun Heo97750ce2010-09-06 17:56:29 +02006384u32 ata_wait_register(struct ata_port *ap, void __iomem *reg, u32 mask, u32 val,
Tejun Heo341c2c92008-05-20 02:17:51 +09006385 unsigned long interval, unsigned long timeout)
Tejun Heoc22daff2006-04-11 22:22:29 +09006386{
Tejun Heo341c2c92008-05-20 02:17:51 +09006387 unsigned long deadline;
Tejun Heoc22daff2006-04-11 22:22:29 +09006388 u32 tmp;
6389
6390 tmp = ioread32(reg);
6391
6392 /* Calculate timeout _after_ the first read to make sure
6393 * preceding writes reach the controller before starting to
6394 * eat away the timeout.
6395 */
Tejun Heo341c2c92008-05-20 02:17:51 +09006396 deadline = ata_deadline(jiffies, timeout);
Tejun Heoc22daff2006-04-11 22:22:29 +09006397
Tejun Heo341c2c92008-05-20 02:17:51 +09006398 while ((tmp & mask) == val && time_before(jiffies, deadline)) {
Tejun Heo97750ce2010-09-06 17:56:29 +02006399 ata_msleep(ap, interval);
Tejun Heoc22daff2006-04-11 22:22:29 +09006400 tmp = ioread32(reg);
6401 }
6402
6403 return tmp;
6404}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01006405EXPORT_SYMBOL_GPL(ata_wait_register);
Tejun Heoc22daff2006-04-11 22:22:29 +09006406
Linus Torvalds1da177e2005-04-16 15:20:36 -07006407/*
Tejun Heodd5b06c2006-08-10 16:59:12 +09006408 * Dummy port_ops
6409 */
Tejun Heodd5b06c2006-08-10 16:59:12 +09006410static unsigned int ata_dummy_qc_issue(struct ata_queued_cmd *qc)
6411{
6412 return AC_ERR_SYSTEM;
6413}
6414
Tejun Heo182d7bb2008-04-07 22:47:21 +09006415static void ata_dummy_error_handler(struct ata_port *ap)
6416{
6417 /* truly dummy */
6418}
6419
Tejun Heo029cfd62008-03-25 12:22:49 +09006420struct ata_port_operations ata_dummy_port_ops = {
Tejun Heodd5b06c2006-08-10 16:59:12 +09006421 .qc_prep = ata_noop_qc_prep,
6422 .qc_issue = ata_dummy_qc_issue,
Tejun Heo182d7bb2008-04-07 22:47:21 +09006423 .error_handler = ata_dummy_error_handler,
Dan Williamse4a9c372012-06-21 23:25:27 -07006424 .sched_eh = ata_std_sched_eh,
6425 .end_eh = ata_std_end_eh,
Tejun Heodd5b06c2006-08-10 16:59:12 +09006426};
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01006427EXPORT_SYMBOL_GPL(ata_dummy_port_ops);
Tejun Heodd5b06c2006-08-10 16:59:12 +09006428
Tejun Heo21b0ad42007-04-17 23:44:07 +09006429const struct ata_port_info ata_dummy_port_info = {
6430 .port_ops = &ata_dummy_port_ops,
6431};
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01006432EXPORT_SYMBOL_GPL(ata_dummy_port_info);
Tejun Heo21b0ad42007-04-17 23:44:07 +09006433
Tejun Heodd5b06c2006-08-10 16:59:12 +09006434/*
Joe Perchesa9a79df2011-04-15 15:51:59 -07006435 * Utility print functions
6436 */
Joe Perchesd7bead12014-09-22 09:52:18 -07006437void ata_port_printk(const struct ata_port *ap, const char *level,
6438 const char *fmt, ...)
Joe Perchesa9a79df2011-04-15 15:51:59 -07006439{
6440 struct va_format vaf;
6441 va_list args;
Joe Perchesa9a79df2011-04-15 15:51:59 -07006442
6443 va_start(args, fmt);
6444
6445 vaf.fmt = fmt;
6446 vaf.va = &args;
6447
Joe Perchesd7bead12014-09-22 09:52:18 -07006448 printk("%sata%u: %pV", level, ap->print_id, &vaf);
Joe Perchesa9a79df2011-04-15 15:51:59 -07006449
6450 va_end(args);
Joe Perchesa9a79df2011-04-15 15:51:59 -07006451}
6452EXPORT_SYMBOL(ata_port_printk);
6453
Joe Perchesd7bead12014-09-22 09:52:18 -07006454void ata_link_printk(const struct ata_link *link, const char *level,
6455 const char *fmt, ...)
Joe Perchesa9a79df2011-04-15 15:51:59 -07006456{
6457 struct va_format vaf;
6458 va_list args;
Joe Perchesa9a79df2011-04-15 15:51:59 -07006459
6460 va_start(args, fmt);
6461
6462 vaf.fmt = fmt;
6463 vaf.va = &args;
6464
6465 if (sata_pmp_attached(link->ap) || link->ap->slave_link)
Joe Perchesd7bead12014-09-22 09:52:18 -07006466 printk("%sata%u.%02u: %pV",
6467 level, link->ap->print_id, link->pmp, &vaf);
Joe Perchesa9a79df2011-04-15 15:51:59 -07006468 else
Joe Perchesd7bead12014-09-22 09:52:18 -07006469 printk("%sata%u: %pV",
6470 level, link->ap->print_id, &vaf);
Joe Perchesa9a79df2011-04-15 15:51:59 -07006471
6472 va_end(args);
Joe Perchesa9a79df2011-04-15 15:51:59 -07006473}
6474EXPORT_SYMBOL(ata_link_printk);
6475
Joe Perchesd7bead12014-09-22 09:52:18 -07006476void ata_dev_printk(const struct ata_device *dev, const char *level,
Joe Perchesa9a79df2011-04-15 15:51:59 -07006477 const char *fmt, ...)
6478{
6479 struct va_format vaf;
6480 va_list args;
Joe Perchesa9a79df2011-04-15 15:51:59 -07006481
6482 va_start(args, fmt);
6483
6484 vaf.fmt = fmt;
6485 vaf.va = &args;
6486
Joe Perchesd7bead12014-09-22 09:52:18 -07006487 printk("%sata%u.%02u: %pV",
6488 level, dev->link->ap->print_id, dev->link->pmp + dev->devno,
6489 &vaf);
Joe Perchesa9a79df2011-04-15 15:51:59 -07006490
6491 va_end(args);
Joe Perchesa9a79df2011-04-15 15:51:59 -07006492}
6493EXPORT_SYMBOL(ata_dev_printk);
6494
Joe Perches06296a12011-04-15 15:52:00 -07006495void ata_print_version(const struct device *dev, const char *version)
6496{
6497 dev_printk(KERN_DEBUG, dev, "version %s\n", version);
6498}
6499EXPORT_SYMBOL(ata_print_version);