blob: 0a56968e2e98f5528c5a7f712fb05b05f5cff787 [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>
Arjan van de Ven79318052009-01-04 05:32:28 -080045#include <linux/async.h>
Mark Lorde18086d2009-03-19 13:32:21 -040046#include <linux/log2.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090047#include <linux/slab.h>
George Spelvin428ac5f2014-08-06 16:09:27 -070048#include <linux/glob.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include <scsi/scsi.h>
Jeff Garzik193515d2005-11-07 00:59:37 -050050#include <scsi/scsi_cmnd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <scsi/scsi_host.h>
52#include <linux/libata.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#include <asm/byteorder.h>
Hannes Reineckefe5af0c2016-04-25 12:45:48 +020054#include <asm/unaligned.h>
Tejun Heo140b5e592007-12-12 12:21:52 +090055#include <linux/cdrom.h>
Akinobu Mita9990b6f2010-04-12 21:11:41 +090056#include <linux/ratelimit.h>
Stephan Linzeb25cb92016-06-10 07:59:56 +020057#include <linux/leds.h>
Lin Ming9ee4f392011-12-05 09:20:28 +080058#include <linux/pm_runtime.h>
Brian Norrisb7db04d2012-11-02 12:29:32 -070059#include <linux/platform_device.h>
Bartlomiej Zolnierkiewiczbbf5a092020-03-26 16:58:01 +010060#include <asm/setup.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Hannes Reinecke255c03d2015-03-27 16:46:38 +010062#define CREATE_TRACE_POINTS
63#include <trace/events/libata.h>
64
Linus Torvalds1da177e2005-04-16 15:20:36 -070065#include "libata.h"
Gwendal Grignoud9027472010-05-25 12:31:38 -070066#include "libata-transport.h"
Jeff Garzikfda0efc2007-01-31 07:43:15 -050067
Tejun Heod7bb4cc2006-05-31 18:27:46 +090068/* debounce timing parameters in msecs { interval, duration, timeout } */
Tejun Heoe9c83912006-07-03 16:07:26 +090069const unsigned long sata_deb_timing_normal[] = { 5, 100, 2000 };
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +010070EXPORT_SYMBOL_GPL(sata_deb_timing_normal);
Tejun Heoe9c83912006-07-03 16:07:26 +090071const unsigned long sata_deb_timing_hotplug[] = { 25, 500, 2000 };
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +010072EXPORT_SYMBOL_GPL(sata_deb_timing_hotplug);
Tejun Heoe9c83912006-07-03 16:07:26 +090073const unsigned long sata_deb_timing_long[] = { 100, 2000, 5000 };
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +010074EXPORT_SYMBOL_GPL(sata_deb_timing_long);
Tejun Heod7bb4cc2006-05-31 18:27:46 +090075
Tejun Heo029cfd62008-03-25 12:22:49 +090076const struct ata_port_operations ata_base_port_ops = {
Tejun Heo0aa11132008-04-07 22:47:18 +090077 .prereset = ata_std_prereset,
Tejun Heo203c75b2008-04-07 22:47:18 +090078 .postreset = ata_std_postreset,
Tejun Heoa1efdab2008-03-25 12:22:50 +090079 .error_handler = ata_std_error_handler,
Dan Williamse4a9c372012-06-21 23:25:27 -070080 .sched_eh = ata_std_sched_eh,
81 .end_eh = ata_std_end_eh,
Tejun Heo029cfd62008-03-25 12:22:49 +090082};
83
84const struct ata_port_operations sata_port_ops = {
85 .inherits = &ata_base_port_ops,
86
87 .qc_defer = ata_std_qc_defer,
Tejun Heo57c9efd2008-04-07 22:47:19 +090088 .hardreset = sata_std_hardreset,
Tejun Heo029cfd62008-03-25 12:22:49 +090089};
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +010090EXPORT_SYMBOL_GPL(sata_port_ops);
Tejun Heo029cfd62008-03-25 12:22:49 +090091
Tejun Heo3373efd2006-05-15 20:57:53 +090092static unsigned int ata_dev_init_params(struct ata_device *dev,
93 u16 heads, u16 sectors);
94static unsigned int ata_dev_set_xfermode(struct ata_device *dev);
95static void ata_dev_xfermask(struct ata_device *dev);
Tejun Heo75683fe2007-07-05 13:31:27 +090096static unsigned long ata_dev_blacklisted(const struct ata_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Tero Roponena78f57a2012-04-22 11:38:00 +030098atomic_t ata_print_id = ATOMIC_INIT(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
Tejun Heo33267322008-02-13 09:15:09 +0900100struct ata_force_param {
101 const char *name;
Bartlomiej Zolnierkiewicz8ba5a452020-03-26 16:58:02 +0100102 u8 cbl;
103 u8 spd_limit;
Tejun Heo33267322008-02-13 09:15:09 +0900104 unsigned long xfer_mask;
105 unsigned int horkage_on;
106 unsigned int horkage_off;
Bartlomiej Zolnierkiewicz8ba5a452020-03-26 16:58:02 +0100107 u16 lflags;
Tejun Heo33267322008-02-13 09:15:09 +0900108};
109
110struct ata_force_ent {
111 int port;
112 int device;
113 struct ata_force_param param;
114};
115
116static struct ata_force_ent *ata_force_tbl;
117static int ata_force_tbl_size;
118
Bartlomiej Zolnierkiewiczbbf5a092020-03-26 16:58:01 +0100119static char ata_force_param_buf[COMMAND_LINE_SIZE] __initdata;
Tejun Heo7afb4222008-03-09 20:21:53 +0900120/* param_buf is thrown away after initialization, disallow read */
121module_param_string(force, ata_force_param_buf, sizeof(ata_force_param_buf), 0);
Mauro Carvalho Chehab8c27ceff32016-10-18 10:12:27 -0200122MODULE_PARM_DESC(force, "Force ATA configurations including cable type, link speed and transfer mode (see Documentation/admin-guide/kernel-parameters.rst for details)");
Tejun Heo33267322008-02-13 09:15:09 +0900123
Tejun Heo2486fa52008-07-31 07:52:40 +0900124static int atapi_enabled = 1;
Jeff Garzik1623c812005-08-30 03:37:42 -0400125module_param(atapi_enabled, int, 0444);
Evgeni Golovad5d8ea2009-06-22 09:46:20 +0200126MODULE_PARM_DESC(atapi_enabled, "Enable discovery of ATAPI devices (0=off, 1=on [default])");
Jeff Garzik1623c812005-08-30 03:37:42 -0400127
Adrian Bunkc5c61bd2008-02-25 02:07:25 +0200128static int atapi_dmadir = 0;
Albert Lee95de7192006-04-04 10:57:18 +0800129module_param(atapi_dmadir, int, 0444);
Evgeni Golovad5d8ea2009-06-22 09:46:20 +0200130MODULE_PARM_DESC(atapi_dmadir, "Enable ATAPI DMADIR bridge support (0=off [default], 1=on)");
Albert Lee95de7192006-04-04 10:57:18 +0800131
Mark Lordbaf4fdf2007-08-08 01:08:45 +0900132int atapi_passthru16 = 1;
133module_param(atapi_passthru16, int, 0444);
Evgeni Golovad5d8ea2009-06-22 09:46:20 +0200134MODULE_PARM_DESC(atapi_passthru16, "Enable ATA_16 passthru for ATAPI devices (0=off, 1=on [default])");
Mark Lordbaf4fdf2007-08-08 01:08:45 +0900135
Jeff Garzikc3c013a2006-02-27 22:31:19 -0500136int libata_fua = 0;
137module_param_named(fua, libata_fua, int, 0444);
Evgeni Golovad5d8ea2009-06-22 09:46:20 +0200138MODULE_PARM_DESC(fua, "FUA support (0=off [default], 1=on)");
Jeff Garzikc3c013a2006-02-27 22:31:19 -0500139
Jeff Garzik2dcb4072007-10-19 06:42:56 -0400140static int ata_ignore_hpa;
Alan Cox1e999732007-04-11 00:23:13 +0100141module_param_named(ignore_hpa, ata_ignore_hpa, int, 0644);
142MODULE_PARM_DESC(ignore_hpa, "Ignore HPA limit (0=keep BIOS limits, 1=ignore limits, using full disk)");
143
Alan Coxb3a70602007-10-02 12:38:26 -0400144static int libata_dma_mask = ATA_DMA_MASK_ATA|ATA_DMA_MASK_ATAPI|ATA_DMA_MASK_CFA;
145module_param_named(dma, libata_dma_mask, int, 0444);
146MODULE_PARM_DESC(dma, "DMA enable/disable (0x1==ATA, 0x2==ATAPI, 0x4==CF)");
147
Tejun Heo87fbc5a2008-05-20 02:17:54 +0900148static int ata_probe_timeout;
Andrew Mortona8601e52006-06-25 01:36:52 -0700149module_param(ata_probe_timeout, int, 0444);
150MODULE_PARM_DESC(ata_probe_timeout, "Set ATA probing timeout (seconds)");
151
Jeff Garzik6ebe9d82007-10-05 16:28:36 -0400152int libata_noacpi = 0;
Jeff Garzikd7d0dad2007-03-28 01:57:37 -0400153module_param_named(noacpi, libata_noacpi, int, 0444);
Evgeni Golovad5d8ea2009-06-22 09:46:20 +0200154MODULE_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 -0700155
Alan Coxae8d4ee2007-11-04 22:05:49 -0500156int libata_allow_tpm = 0;
157module_param_named(allow_tpm, libata_allow_tpm, int, 0444);
Evgeni Golovad5d8ea2009-06-22 09:46:20 +0200158MODULE_PARM_DESC(allow_tpm, "Permit the use of TPM commands (0=off [default], 1=on)");
Alan Coxae8d4ee2007-11-04 22:05:49 -0500159
Tejun Heoe7ecd432010-05-19 15:38:58 +0200160static int atapi_an;
161module_param(atapi_an, int, 0444);
162MODULE_PARM_DESC(atapi_an, "Enable ATAPI AN media presence notification (0=0ff [default], 1=on)");
163
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164MODULE_AUTHOR("Jeff Garzik");
165MODULE_DESCRIPTION("Library module for ATA devices");
166MODULE_LICENSE("GPL");
167MODULE_VERSION(DRV_VERSION);
168
Edward Falk0baab862005-06-02 18:17:13 -0400169
Tejun Heo9913ff8a2009-01-29 20:31:31 +0900170static bool ata_sstatus_online(u32 sstatus)
171{
172 return (sstatus & 0xf) == 0x3;
173}
174
Tejun Heo1eca4362008-11-03 20:03:17 +0900175/**
176 * ata_link_next - link iteration helper
177 * @link: the previous link, NULL to start
178 * @ap: ATA port containing links to iterate
179 * @mode: iteration mode, one of ATA_LITER_*
Tejun Heoaadffb62008-07-31 17:02:41 +0900180 *
Tejun Heo1eca4362008-11-03 20:03:17 +0900181 * LOCKING:
182 * Host lock or EH context.
183 *
184 * RETURNS:
185 * Pointer to the next link.
Tejun Heoaadffb62008-07-31 17:02:41 +0900186 */
Tejun Heo1eca4362008-11-03 20:03:17 +0900187struct ata_link *ata_link_next(struct ata_link *link, struct ata_port *ap,
188 enum ata_link_iter_mode mode)
Tejun Heoaadffb62008-07-31 17:02:41 +0900189{
Tejun Heo1eca4362008-11-03 20:03:17 +0900190 BUG_ON(mode != ATA_LITER_EDGE &&
191 mode != ATA_LITER_PMP_FIRST && mode != ATA_LITER_HOST_FIRST);
Tejun Heoaadffb62008-07-31 17:02:41 +0900192
Tejun Heo1eca4362008-11-03 20:03:17 +0900193 /* NULL link indicates start of iteration */
194 if (!link)
195 switch (mode) {
196 case ATA_LITER_EDGE:
197 case ATA_LITER_PMP_FIRST:
198 if (sata_pmp_attached(ap))
199 return ap->pmp_link;
200 /* fall through */
201 case ATA_LITER_HOST_FIRST:
202 return &ap->link;
203 }
204
205 /* we just iterated over the host link, what's next? */
206 if (link == &ap->link)
207 switch (mode) {
208 case ATA_LITER_HOST_FIRST:
209 if (sata_pmp_attached(ap))
210 return ap->pmp_link;
211 /* fall through */
212 case ATA_LITER_PMP_FIRST:
213 if (unlikely(ap->slave_link))
Tejun Heob1c72912008-07-31 17:02:43 +0900214 return ap->slave_link;
Tejun Heo1eca4362008-11-03 20:03:17 +0900215 /* fall through */
216 case ATA_LITER_EDGE:
Tejun Heoaadffb62008-07-31 17:02:41 +0900217 return NULL;
Tejun Heob1c72912008-07-31 17:02:43 +0900218 }
Tejun Heoaadffb62008-07-31 17:02:41 +0900219
Tejun Heob1c72912008-07-31 17:02:43 +0900220 /* slave_link excludes PMP */
221 if (unlikely(link == ap->slave_link))
222 return NULL;
223
Tejun Heo1eca4362008-11-03 20:03:17 +0900224 /* we were over a PMP link */
Tejun Heoaadffb62008-07-31 17:02:41 +0900225 if (++link < ap->pmp_link + ap->nr_pmp_links)
226 return link;
Tejun Heo1eca4362008-11-03 20:03:17 +0900227
228 if (mode == ATA_LITER_PMP_FIRST)
229 return &ap->link;
230
Tejun Heoaadffb62008-07-31 17:02:41 +0900231 return NULL;
232}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +0100233EXPORT_SYMBOL_GPL(ata_link_next);
Tejun Heoaadffb62008-07-31 17:02:41 +0900234
Edward Falk0baab862005-06-02 18:17:13 -0400235/**
Tejun Heo1eca4362008-11-03 20:03:17 +0900236 * ata_dev_next - device iteration helper
237 * @dev: the previous device, NULL to start
238 * @link: ATA link containing devices to iterate
239 * @mode: iteration mode, one of ATA_DITER_*
240 *
241 * LOCKING:
242 * Host lock or EH context.
243 *
244 * RETURNS:
245 * Pointer to the next device.
246 */
247struct ata_device *ata_dev_next(struct ata_device *dev, struct ata_link *link,
248 enum ata_dev_iter_mode mode)
249{
250 BUG_ON(mode != ATA_DITER_ENABLED && mode != ATA_DITER_ENABLED_REVERSE &&
251 mode != ATA_DITER_ALL && mode != ATA_DITER_ALL_REVERSE);
252
253 /* NULL dev indicates start of iteration */
254 if (!dev)
255 switch (mode) {
256 case ATA_DITER_ENABLED:
257 case ATA_DITER_ALL:
258 dev = link->device;
259 goto check;
260 case ATA_DITER_ENABLED_REVERSE:
261 case ATA_DITER_ALL_REVERSE:
262 dev = link->device + ata_link_max_devices(link) - 1;
263 goto check;
264 }
265
266 next:
267 /* move to the next one */
268 switch (mode) {
269 case ATA_DITER_ENABLED:
270 case ATA_DITER_ALL:
271 if (++dev < link->device + ata_link_max_devices(link))
272 goto check;
273 return NULL;
274 case ATA_DITER_ENABLED_REVERSE:
275 case ATA_DITER_ALL_REVERSE:
276 if (--dev >= link->device)
277 goto check;
278 return NULL;
279 }
280
281 check:
282 if ((mode == ATA_DITER_ENABLED || mode == ATA_DITER_ENABLED_REVERSE) &&
283 !ata_dev_enabled(dev))
284 goto next;
285 return dev;
286}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +0100287EXPORT_SYMBOL_GPL(ata_dev_next);
Tejun Heo1eca4362008-11-03 20:03:17 +0900288
289/**
Tejun Heob1c72912008-07-31 17:02:43 +0900290 * ata_dev_phys_link - find physical link for a device
291 * @dev: ATA device to look up physical link for
292 *
293 * Look up physical link which @dev is attached to. Note that
294 * this is different from @dev->link only when @dev is on slave
295 * link. For all other cases, it's the same as @dev->link.
296 *
297 * LOCKING:
298 * Don't care.
299 *
300 * RETURNS:
301 * Pointer to the found physical link.
302 */
303struct ata_link *ata_dev_phys_link(struct ata_device *dev)
304{
305 struct ata_port *ap = dev->link->ap;
306
307 if (!ap->slave_link)
308 return dev->link;
309 if (!dev->devno)
310 return &ap->link;
311 return ap->slave_link;
312}
313
314/**
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}
494
495/**
Tejun Heo436d34b2008-04-02 17:28:46 +0900496 * atapi_cmd_type - Determine ATAPI command type from SCSI opcode
497 * @opcode: SCSI opcode
498 *
499 * Determine ATAPI command type from @opcode.
500 *
501 * LOCKING:
502 * None.
503 *
504 * RETURNS:
505 * ATAPI_{READ|WRITE|READ_CD|PASS_THRU|MISC}
506 */
507int atapi_cmd_type(u8 opcode)
508{
509 switch (opcode) {
510 case GPCMD_READ_10:
511 case GPCMD_READ_12:
512 return ATAPI_READ;
513
514 case GPCMD_WRITE_10:
515 case GPCMD_WRITE_12:
516 case GPCMD_WRITE_AND_VERIFY_10:
517 return ATAPI_WRITE;
518
519 case GPCMD_READ_CD:
520 case GPCMD_READ_CD_MSF:
521 return ATAPI_READ_CD;
522
Tejun Heoe52dcc42008-04-02 17:35:19 +0900523 case ATA_16:
524 case ATA_12:
525 if (atapi_passthru16)
526 return ATAPI_PASS_THRU;
527 /* fall thru */
Tejun Heo436d34b2008-04-02 17:28:46 +0900528 default:
529 return ATAPI_MISC;
530 }
531}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +0100532EXPORT_SYMBOL_GPL(atapi_cmd_type);
Tejun Heo436d34b2008-04-02 17:28:46 +0900533
534/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 * ata_tf_to_fis - Convert ATA taskfile to SATA FIS structure
536 * @tf: Taskfile to convert
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 * @pmp: Port multiplier port
Tejun Heo99771262007-07-16 14:29:38 +0900538 * @is_cmd: This FIS is for command
539 * @fis: Buffer into which data will output
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 *
541 * Converts a standard ATA taskfile to a Serial ATA
542 * FIS structure (Register - Host to Device).
543 *
544 * LOCKING:
545 * Inherited from caller.
546 */
Tejun Heo99771262007-07-16 14:29:38 +0900547void ata_tf_to_fis(const struct ata_taskfile *tf, u8 pmp, int is_cmd, u8 *fis)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548{
Tejun Heo99771262007-07-16 14:29:38 +0900549 fis[0] = 0x27; /* Register - Host to Device FIS */
550 fis[1] = pmp & 0xf; /* Port multiplier number*/
551 if (is_cmd)
552 fis[1] |= (1 << 7); /* bit 7 indicates Command FIS */
553
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 fis[2] = tf->command;
555 fis[3] = tf->feature;
556
557 fis[4] = tf->lbal;
558 fis[5] = tf->lbam;
559 fis[6] = tf->lbah;
560 fis[7] = tf->device;
561
562 fis[8] = tf->hob_lbal;
563 fis[9] = tf->hob_lbam;
564 fis[10] = tf->hob_lbah;
565 fis[11] = tf->hob_feature;
566
567 fis[12] = tf->nsect;
568 fis[13] = tf->hob_nsect;
569 fis[14] = 0;
570 fis[15] = tf->ctl;
571
Marc Carino86a565e2013-09-01 08:59:01 -0700572 fis[16] = tf->auxiliary & 0xff;
573 fis[17] = (tf->auxiliary >> 8) & 0xff;
574 fis[18] = (tf->auxiliary >> 16) & 0xff;
575 fis[19] = (tf->auxiliary >> 24) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +0100577EXPORT_SYMBOL_GPL(ata_tf_to_fis);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
579/**
580 * ata_tf_from_fis - Convert SATA FIS to ATA taskfile
581 * @fis: Buffer from which data will be input
582 * @tf: Taskfile to output
583 *
Mark Lorde12a1be2005-11-12 18:55:45 -0500584 * Converts a serial ATA FIS structure to a standard ATA taskfile.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 *
586 * LOCKING:
587 * Inherited from caller.
588 */
589
Jeff Garzik057ace52005-10-22 14:27:05 -0400590void ata_tf_from_fis(const u8 *fis, struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591{
592 tf->command = fis[2]; /* status */
593 tf->feature = fis[3]; /* error */
594
595 tf->lbal = fis[4];
596 tf->lbam = fis[5];
597 tf->lbah = fis[6];
598 tf->device = fis[7];
599
600 tf->hob_lbal = fis[8];
601 tf->hob_lbam = fis[9];
602 tf->hob_lbah = fis[10];
603
604 tf->nsect = fis[12];
605 tf->hob_nsect = fis[13];
606}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +0100607EXPORT_SYMBOL_GPL(ata_tf_from_fis);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
Albert Lee8cbd6df2005-10-12 15:06:27 +0800609static const u8 ata_rw_cmds[] = {
610 /* pio multi */
611 ATA_CMD_READ_MULTI,
612 ATA_CMD_WRITE_MULTI,
613 ATA_CMD_READ_MULTI_EXT,
614 ATA_CMD_WRITE_MULTI_EXT,
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100615 0,
616 0,
617 0,
618 ATA_CMD_WRITE_MULTI_FUA_EXT,
Albert Lee8cbd6df2005-10-12 15:06:27 +0800619 /* pio */
620 ATA_CMD_PIO_READ,
621 ATA_CMD_PIO_WRITE,
622 ATA_CMD_PIO_READ_EXT,
623 ATA_CMD_PIO_WRITE_EXT,
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100624 0,
625 0,
626 0,
627 0,
Albert Lee8cbd6df2005-10-12 15:06:27 +0800628 /* dma */
629 ATA_CMD_READ,
630 ATA_CMD_WRITE,
631 ATA_CMD_READ_EXT,
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100632 ATA_CMD_WRITE_EXT,
633 0,
634 0,
635 0,
636 ATA_CMD_WRITE_FUA_EXT
Albert Lee8cbd6df2005-10-12 15:06:27 +0800637};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
639/**
Albert Lee8cbd6df2005-10-12 15:06:27 +0800640 * ata_rwcmd_protocol - set taskfile r/w commands and protocol
Tejun Heobd056d72006-11-14 22:47:10 +0900641 * @tf: command to examine and configure
642 * @dev: device tf belongs to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 *
Jeff Garzik2e9edbf2006-03-24 09:56:57 -0500644 * Examine the device configuration and tf->flags to calculate
Albert Lee8cbd6df2005-10-12 15:06:27 +0800645 * the proper read/write commands and protocol to use.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 *
647 * LOCKING:
648 * caller.
649 */
Tejun Heobd056d72006-11-14 22:47:10 +0900650static int ata_rwcmd_protocol(struct ata_taskfile *tf, struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651{
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100652 u8 cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100654 int index, fua, lba48, write;
Jeff Garzik2e9edbf2006-03-24 09:56:57 -0500655
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100656 fua = (tf->flags & ATA_TFLAG_FUA) ? 4 : 0;
Albert Lee8cbd6df2005-10-12 15:06:27 +0800657 lba48 = (tf->flags & ATA_TFLAG_LBA48) ? 2 : 0;
658 write = (tf->flags & ATA_TFLAG_WRITE) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
Albert Lee8cbd6df2005-10-12 15:06:27 +0800660 if (dev->flags & ATA_DFLAG_PIO) {
661 tf->protocol = ATA_PROT_PIO;
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100662 index = dev->multi_count ? 0 : 8;
Tejun Heo9af5c9c2007-08-06 18:36:22 +0900663 } else if (lba48 && (dev->link->ap->flags & ATA_FLAG_PIO_LBA48)) {
Alan Cox8d238e02006-01-17 20:50:31 +0000664 /* Unable to use DMA due to host limitation */
665 tf->protocol = ATA_PROT_PIO;
Albert Lee0565c262006-02-13 18:55:25 +0800666 index = dev->multi_count ? 0 : 8;
Albert Lee8cbd6df2005-10-12 15:06:27 +0800667 } else {
668 tf->protocol = ATA_PROT_DMA;
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100669 index = 16;
Albert Lee8cbd6df2005-10-12 15:06:27 +0800670 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100672 cmd = ata_rw_cmds[index + fua + lba48 + write];
673 if (cmd) {
674 tf->command = cmd;
675 return 0;
676 }
677 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678}
679
Tejun Heocb95d562006-03-06 04:31:56 +0900680/**
Tejun Heo35b649f2006-11-14 22:37:35 +0900681 * ata_tf_read_block - Read block address from ATA taskfile
682 * @tf: ATA taskfile of interest
683 * @dev: ATA device @tf belongs to
684 *
685 * LOCKING:
686 * None.
687 *
688 * Read block address from @tf. This function can handle all
689 * three address formats - LBA, LBA48 and CHS. tf->protocol and
690 * flags select the address format to use.
691 *
692 * RETURNS:
693 * Block address read from @tf.
694 */
Hannes Reineckecffd1ee2016-04-04 11:43:57 +0200695u64 ata_tf_read_block(const struct ata_taskfile *tf, struct ata_device *dev)
Tejun Heo35b649f2006-11-14 22:37:35 +0900696{
697 u64 block = 0;
698
Tejun Heofe16d4f2015-08-03 11:41:33 -0400699 if (tf->flags & ATA_TFLAG_LBA) {
Tejun Heo35b649f2006-11-14 22:37:35 +0900700 if (tf->flags & ATA_TFLAG_LBA48) {
701 block |= (u64)tf->hob_lbah << 40;
702 block |= (u64)tf->hob_lbam << 32;
Roland Dreier44901a92008-11-04 10:34:48 -0800703 block |= (u64)tf->hob_lbal << 24;
Tejun Heo35b649f2006-11-14 22:37:35 +0900704 } else
705 block |= (tf->device & 0xf) << 24;
706
707 block |= tf->lbah << 16;
708 block |= tf->lbam << 8;
709 block |= tf->lbal;
710 } else {
711 u32 cyl, head, sect;
712
713 cyl = tf->lbam | (tf->lbah << 8);
714 head = tf->device & 0xf;
715 sect = tf->lbal;
716
Tejun Heoac8672e2009-08-16 21:21:21 +0900717 if (!sect) {
Joe Perchesa9a79df2011-04-15 15:51:59 -0700718 ata_dev_warn(dev,
719 "device reported invalid CHS sector 0\n");
Hannes Reineckecffd1ee2016-04-04 11:43:57 +0200720 return U64_MAX;
Tejun Heoac8672e2009-08-16 21:21:21 +0900721 }
722
723 block = (cyl * dev->heads + head) * dev->sectors + sect - 1;
Tejun Heo35b649f2006-11-14 22:37:35 +0900724 }
725
726 return block;
727}
728
729/**
Tejun Heobd056d72006-11-14 22:47:10 +0900730 * ata_build_rw_tf - Build ATA taskfile for given read/write request
731 * @tf: Target ATA taskfile
732 * @dev: ATA device @tf belongs to
733 * @block: Block address
734 * @n_block: Number of blocks
735 * @tf_flags: RW/FUA etc...
736 * @tag: tag
Adam Manzanares8e061782016-10-17 11:27:29 -0700737 * @class: IO priority class
Tejun Heobd056d72006-11-14 22:47:10 +0900738 *
739 * LOCKING:
740 * None.
741 *
742 * Build ATA taskfile @tf for read/write request described by
743 * @block, @n_block, @tf_flags and @tag on @dev.
744 *
745 * RETURNS:
746 *
747 * 0 on success, -ERANGE if the request is too large for @dev,
748 * -EINVAL if the request is invalid.
749 */
750int ata_build_rw_tf(struct ata_taskfile *tf, struct ata_device *dev,
751 u64 block, u32 n_block, unsigned int tf_flags,
Adam Manzanares8e061782016-10-17 11:27:29 -0700752 unsigned int tag, int class)
Tejun Heobd056d72006-11-14 22:47:10 +0900753{
754 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
755 tf->flags |= tf_flags;
756
Jens Axboe2e2cc672018-05-11 12:51:06 -0600757 if (ata_ncq_enabled(dev) && !ata_tag_internal(tag)) {
Tejun Heobd056d72006-11-14 22:47:10 +0900758 /* yay, NCQ */
759 if (!lba_48_ok(block, n_block))
760 return -ERANGE;
761
762 tf->protocol = ATA_PROT_NCQ;
763 tf->flags |= ATA_TFLAG_LBA | ATA_TFLAG_LBA48;
764
765 if (tf->flags & ATA_TFLAG_WRITE)
766 tf->command = ATA_CMD_FPDMA_WRITE;
767 else
768 tf->command = ATA_CMD_FPDMA_READ;
769
770 tf->nsect = tag << 3;
771 tf->hob_feature = (n_block >> 8) & 0xff;
772 tf->feature = n_block & 0xff;
773
774 tf->hob_lbah = (block >> 40) & 0xff;
775 tf->hob_lbam = (block >> 32) & 0xff;
776 tf->hob_lbal = (block >> 24) & 0xff;
777 tf->lbah = (block >> 16) & 0xff;
778 tf->lbam = (block >> 8) & 0xff;
779 tf->lbal = block & 0xff;
780
Sergei Shtylyov9ca7cfa2012-08-21 22:18:50 +0400781 tf->device = ATA_LBA;
Tejun Heobd056d72006-11-14 22:47:10 +0900782 if (tf->flags & ATA_TFLAG_FUA)
783 tf->device |= 1 << 7;
Adam Manzanares8e061782016-10-17 11:27:29 -0700784
Adam Manzanares9f56eca2016-12-13 12:00:05 -0800785 if (dev->flags & ATA_DFLAG_NCQ_PRIO) {
Adam Manzanares8e061782016-10-17 11:27:29 -0700786 if (class == IOPRIO_CLASS_RT)
787 tf->hob_nsect |= ATA_PRIO_HIGH <<
788 ATA_SHIFT_PRIO;
789 }
Tejun Heobd056d72006-11-14 22:47:10 +0900790 } else if (dev->flags & ATA_DFLAG_LBA) {
791 tf->flags |= ATA_TFLAG_LBA;
792
793 if (lba_28_ok(block, n_block)) {
794 /* use LBA28 */
795 tf->device |= (block >> 24) & 0xf;
796 } else if (lba_48_ok(block, n_block)) {
797 if (!(dev->flags & ATA_DFLAG_LBA48))
798 return -ERANGE;
799
800 /* use LBA48 */
801 tf->flags |= ATA_TFLAG_LBA48;
802
803 tf->hob_nsect = (n_block >> 8) & 0xff;
804
805 tf->hob_lbah = (block >> 40) & 0xff;
806 tf->hob_lbam = (block >> 32) & 0xff;
807 tf->hob_lbal = (block >> 24) & 0xff;
808 } else
809 /* request too large even for LBA48 */
810 return -ERANGE;
811
812 if (unlikely(ata_rwcmd_protocol(tf, dev) < 0))
813 return -EINVAL;
814
815 tf->nsect = n_block & 0xff;
816
817 tf->lbah = (block >> 16) & 0xff;
818 tf->lbam = (block >> 8) & 0xff;
819 tf->lbal = block & 0xff;
820
821 tf->device |= ATA_LBA;
822 } else {
823 /* CHS */
824 u32 sect, head, cyl, track;
825
826 /* The request -may- be too large for CHS addressing. */
827 if (!lba_28_ok(block, n_block))
828 return -ERANGE;
829
830 if (unlikely(ata_rwcmd_protocol(tf, dev) < 0))
831 return -EINVAL;
832
833 /* Convert LBA to CHS */
834 track = (u32)block / dev->sectors;
835 cyl = track / dev->heads;
836 head = track % dev->heads;
837 sect = (u32)block % dev->sectors + 1;
838
839 DPRINTK("block %u track %u cyl %u head %u sect %u\n",
840 (u32)block, track, cyl, head, sect);
841
842 /* Check whether the converted CHS can fit.
843 Cylinder: 0-65535
844 Head: 0-15
845 Sector: 1-255*/
846 if ((cyl >> 16) || (head >> 4) || (sect >> 8) || (!sect))
847 return -ERANGE;
848
849 tf->nsect = n_block & 0xff; /* Sector count 0 means 256 sectors */
850 tf->lbal = sect;
851 tf->lbam = cyl;
852 tf->lbah = cyl >> 8;
853 tf->device |= head;
854 }
855
856 return 0;
857}
858
859/**
Tejun Heocb95d562006-03-06 04:31:56 +0900860 * ata_pack_xfermask - Pack pio, mwdma and udma masks into xfer_mask
861 * @pio_mask: pio_mask
862 * @mwdma_mask: mwdma_mask
863 * @udma_mask: udma_mask
864 *
865 * Pack @pio_mask, @mwdma_mask and @udma_mask into a single
866 * unsigned int xfer_mask.
867 *
868 * LOCKING:
869 * None.
870 *
871 * RETURNS:
872 * Packed xfer_mask.
873 */
Tejun Heo7dc951a2007-11-27 19:43:42 +0900874unsigned long ata_pack_xfermask(unsigned long pio_mask,
875 unsigned long mwdma_mask,
876 unsigned long udma_mask)
Tejun Heocb95d562006-03-06 04:31:56 +0900877{
878 return ((pio_mask << ATA_SHIFT_PIO) & ATA_MASK_PIO) |
879 ((mwdma_mask << ATA_SHIFT_MWDMA) & ATA_MASK_MWDMA) |
880 ((udma_mask << ATA_SHIFT_UDMA) & ATA_MASK_UDMA);
881}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +0100882EXPORT_SYMBOL_GPL(ata_pack_xfermask);
Tejun Heocb95d562006-03-06 04:31:56 +0900883
Tejun Heoc0489e42006-03-24 14:07:49 +0900884/**
885 * ata_unpack_xfermask - Unpack xfer_mask into pio, mwdma and udma masks
886 * @xfer_mask: xfer_mask to unpack
887 * @pio_mask: resulting pio_mask
888 * @mwdma_mask: resulting mwdma_mask
889 * @udma_mask: resulting udma_mask
890 *
891 * Unpack @xfer_mask into @pio_mask, @mwdma_mask and @udma_mask.
Masanari Iidac9b55602016-04-13 23:36:27 +0900892 * Any NULL destination masks will be ignored.
Tejun Heoc0489e42006-03-24 14:07:49 +0900893 */
Tejun Heo7dc951a2007-11-27 19:43:42 +0900894void ata_unpack_xfermask(unsigned long xfer_mask, unsigned long *pio_mask,
895 unsigned long *mwdma_mask, unsigned long *udma_mask)
Tejun Heoc0489e42006-03-24 14:07:49 +0900896{
897 if (pio_mask)
898 *pio_mask = (xfer_mask & ATA_MASK_PIO) >> ATA_SHIFT_PIO;
899 if (mwdma_mask)
900 *mwdma_mask = (xfer_mask & ATA_MASK_MWDMA) >> ATA_SHIFT_MWDMA;
901 if (udma_mask)
902 *udma_mask = (xfer_mask & ATA_MASK_UDMA) >> ATA_SHIFT_UDMA;
903}
904
Tejun Heocb95d562006-03-06 04:31:56 +0900905static const struct ata_xfer_ent {
Tejun Heobe9a50c2006-03-31 22:48:52 +0900906 int shift, bits;
Tejun Heocb95d562006-03-06 04:31:56 +0900907 u8 base;
908} ata_xfer_tbl[] = {
Tejun Heo70cd0712007-11-27 19:43:40 +0900909 { ATA_SHIFT_PIO, ATA_NR_PIO_MODES, XFER_PIO_0 },
910 { ATA_SHIFT_MWDMA, ATA_NR_MWDMA_MODES, XFER_MW_DMA_0 },
911 { ATA_SHIFT_UDMA, ATA_NR_UDMA_MODES, XFER_UDMA_0 },
Tejun Heocb95d562006-03-06 04:31:56 +0900912 { -1, },
913};
914
915/**
916 * ata_xfer_mask2mode - Find matching XFER_* for the given xfer_mask
917 * @xfer_mask: xfer_mask of interest
918 *
919 * Return matching XFER_* value for @xfer_mask. Only the highest
920 * bit of @xfer_mask is considered.
921 *
922 * LOCKING:
923 * None.
924 *
925 * RETURNS:
Tejun Heo70cd0712007-11-27 19:43:40 +0900926 * Matching XFER_* value, 0xff if no match found.
Tejun Heocb95d562006-03-06 04:31:56 +0900927 */
Tejun Heo7dc951a2007-11-27 19:43:42 +0900928u8 ata_xfer_mask2mode(unsigned long xfer_mask)
Tejun Heocb95d562006-03-06 04:31:56 +0900929{
930 int highbit = fls(xfer_mask) - 1;
931 const struct ata_xfer_ent *ent;
932
933 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
934 if (highbit >= ent->shift && highbit < ent->shift + ent->bits)
935 return ent->base + highbit - ent->shift;
Tejun Heo70cd0712007-11-27 19:43:40 +0900936 return 0xff;
Tejun Heocb95d562006-03-06 04:31:56 +0900937}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +0100938EXPORT_SYMBOL_GPL(ata_xfer_mask2mode);
Tejun Heocb95d562006-03-06 04:31:56 +0900939
940/**
941 * ata_xfer_mode2mask - Find matching xfer_mask for XFER_*
942 * @xfer_mode: XFER_* of interest
943 *
944 * Return matching xfer_mask for @xfer_mode.
945 *
946 * LOCKING:
947 * None.
948 *
949 * RETURNS:
950 * Matching xfer_mask, 0 if no match found.
951 */
Tejun Heo7dc951a2007-11-27 19:43:42 +0900952unsigned long ata_xfer_mode2mask(u8 xfer_mode)
Tejun Heocb95d562006-03-06 04:31:56 +0900953{
954 const struct ata_xfer_ent *ent;
955
956 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
957 if (xfer_mode >= ent->base && xfer_mode < ent->base + ent->bits)
Tejun Heo70cd0712007-11-27 19:43:40 +0900958 return ((2 << (ent->shift + xfer_mode - ent->base)) - 1)
959 & ~((1 << ent->shift) - 1);
Tejun Heocb95d562006-03-06 04:31:56 +0900960 return 0;
961}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +0100962EXPORT_SYMBOL_GPL(ata_xfer_mode2mask);
Tejun Heocb95d562006-03-06 04:31:56 +0900963
964/**
965 * ata_xfer_mode2shift - Find matching xfer_shift for XFER_*
966 * @xfer_mode: XFER_* of interest
967 *
968 * Return matching xfer_shift for @xfer_mode.
969 *
970 * LOCKING:
971 * None.
972 *
973 * RETURNS:
974 * Matching xfer_shift, -1 if no match found.
975 */
Tejun Heo7dc951a2007-11-27 19:43:42 +0900976int ata_xfer_mode2shift(unsigned long xfer_mode)
Tejun Heocb95d562006-03-06 04:31:56 +0900977{
978 const struct ata_xfer_ent *ent;
979
980 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
981 if (xfer_mode >= ent->base && xfer_mode < ent->base + ent->bits)
982 return ent->shift;
983 return -1;
984}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +0100985EXPORT_SYMBOL_GPL(ata_xfer_mode2shift);
Tejun Heocb95d562006-03-06 04:31:56 +0900986
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987/**
Tejun Heo1da7b0d2006-03-06 04:31:56 +0900988 * ata_mode_string - convert xfer_mask to string
989 * @xfer_mask: mask of bits supported; only highest bit counts.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 *
991 * Determine string which represents the highest speed
Tejun Heo1da7b0d2006-03-06 04:31:56 +0900992 * (highest bit in @modemask).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 *
994 * LOCKING:
995 * None.
996 *
997 * RETURNS:
998 * Constant C string representing highest speed listed in
Tejun Heo1da7b0d2006-03-06 04:31:56 +0900999 * @mode_mask, or the constant C string "<n/a>".
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 */
Tejun Heo7dc951a2007-11-27 19:43:42 +09001001const char *ata_mode_string(unsigned long xfer_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002{
Tejun Heo75f554b2006-03-06 04:31:57 +09001003 static const char * const xfer_mode_str[] = {
1004 "PIO0",
1005 "PIO1",
1006 "PIO2",
1007 "PIO3",
1008 "PIO4",
Alan Coxb352e572006-08-10 18:52:12 +01001009 "PIO5",
1010 "PIO6",
Tejun Heo75f554b2006-03-06 04:31:57 +09001011 "MWDMA0",
1012 "MWDMA1",
1013 "MWDMA2",
Alan Coxb352e572006-08-10 18:52:12 +01001014 "MWDMA3",
1015 "MWDMA4",
Tejun Heo75f554b2006-03-06 04:31:57 +09001016 "UDMA/16",
1017 "UDMA/25",
1018 "UDMA/33",
1019 "UDMA/44",
1020 "UDMA/66",
1021 "UDMA/100",
1022 "UDMA/133",
1023 "UDMA7",
1024 };
Tejun Heo1da7b0d2006-03-06 04:31:56 +09001025 int highbit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026
Tejun Heo1da7b0d2006-03-06 04:31:56 +09001027 highbit = fls(xfer_mask) - 1;
1028 if (highbit >= 0 && highbit < ARRAY_SIZE(xfer_mode_str))
1029 return xfer_mode_str[highbit];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 return "<n/a>";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01001032EXPORT_SYMBOL_GPL(ata_mode_string);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033
Gwendal Grignoud9027472010-05-25 12:31:38 -07001034const char *sata_spd_string(unsigned int spd)
Tejun Heo4c360c82006-04-01 01:38:17 +09001035{
1036 static const char * const spd_str[] = {
1037 "1.5 Gbps",
1038 "3.0 Gbps",
Shane Huang8522ee22008-12-30 11:00:37 +08001039 "6.0 Gbps",
Tejun Heo4c360c82006-04-01 01:38:17 +09001040 };
1041
1042 if (spd == 0 || (spd - 1) >= ARRAY_SIZE(spd_str))
1043 return "<unknown>";
1044 return spd_str[spd - 1];
1045}
1046
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047/**
1048 * ata_dev_classify - determine device type based on ATA-spec signature
1049 * @tf: ATA taskfile register set for device to be identified
1050 *
1051 * Determine from taskfile register contents whether a device is
1052 * ATA or ATAPI, as per "Signature and persistence" section
1053 * of ATA/PI spec (volume 1, sect 5.14).
1054 *
1055 * LOCKING:
1056 * None.
1057 *
1058 * RETURNS:
Hannes Reinecke9162c652014-11-05 13:08:21 +01001059 * Device type, %ATA_DEV_ATA, %ATA_DEV_ATAPI, %ATA_DEV_PMP,
1060 * %ATA_DEV_ZAC, or %ATA_DEV_UNKNOWN the event of failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 */
Jeff Garzik057ace52005-10-22 14:27:05 -04001062unsigned int ata_dev_classify(const struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063{
1064 /* Apple's open source Darwin code hints that some devices only
1065 * put a proper signature into the LBA mid/high registers,
1066 * So, we only check those. It's sufficient for uniqueness.
Tejun Heo633273a2007-09-23 13:19:54 +09001067 *
1068 * ATA/ATAPI-7 (d1532v1r1: Feb. 19, 2003) specified separate
1069 * signatures for ATA and ATAPI devices attached on SerialATA,
1070 * 0x3c/0xc3 and 0x69/0x96 respectively. However, SerialATA
1071 * spec has never mentioned about using different signatures
1072 * for ATA/ATAPI devices. Then, Serial ATA II: Port
1073 * Multiplier specification began to use 0x69/0x96 to identify
1074 * port multpliers and 0x3c/0xc3 to identify SEMB device.
1075 * ATA/ATAPI-7 dropped descriptions about 0x3c/0xc3 and
1076 * 0x69/0x96 shortly and described them as reserved for
1077 * SerialATA.
1078 *
1079 * We follow the current spec and consider that 0x69/0x96
1080 * identifies a port multiplier and 0x3c/0xc3 a SEMB device.
Tejun Heo79b42ba2009-04-15 06:21:10 +09001081 * Unfortunately, WDC WD1600JS-62MHB5 (a hard drive) reports
1082 * SEMB signature. This is worked around in
1083 * ata_dev_read_id().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 */
Tejun Heo633273a2007-09-23 13:19:54 +09001085 if ((tf->lbam == 0) && (tf->lbah == 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 DPRINTK("found ATA device by sig\n");
1087 return ATA_DEV_ATA;
1088 }
1089
Tejun Heo633273a2007-09-23 13:19:54 +09001090 if ((tf->lbam == 0x14) && (tf->lbah == 0xeb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 DPRINTK("found ATAPI device by sig\n");
1092 return ATA_DEV_ATAPI;
1093 }
1094
Tejun Heo633273a2007-09-23 13:19:54 +09001095 if ((tf->lbam == 0x69) && (tf->lbah == 0x96)) {
1096 DPRINTK("found PMP device by sig\n");
1097 return ATA_DEV_PMP;
1098 }
1099
1100 if ((tf->lbam == 0x3c) && (tf->lbah == 0xc3)) {
Tejun Heo79b42ba2009-04-15 06:21:10 +09001101 DPRINTK("found SEMB device by sig (could be ATA device)\n");
1102 return ATA_DEV_SEMB;
Tejun Heo633273a2007-09-23 13:19:54 +09001103 }
1104
Hannes Reinecke9162c652014-11-05 13:08:21 +01001105 if ((tf->lbam == 0xcd) && (tf->lbah == 0xab)) {
1106 DPRINTK("found ZAC device by sig\n");
1107 return ATA_DEV_ZAC;
1108 }
1109
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 DPRINTK("unknown device\n");
1111 return ATA_DEV_UNKNOWN;
1112}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01001113EXPORT_SYMBOL_GPL(ata_dev_classify);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114
1115/**
Tejun Heo6a62a042006-02-13 10:02:46 +09001116 * ata_id_string - Convert IDENTIFY DEVICE page into string
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 * @id: IDENTIFY DEVICE results we will examine
1118 * @s: string into which data is output
1119 * @ofs: offset into identify device page
1120 * @len: length of string to return. must be an even number.
1121 *
1122 * The strings in the IDENTIFY DEVICE page are broken up into
1123 * 16-bit chunks. Run through the string, and output each
1124 * 8-bit chunk linearly, regardless of platform.
1125 *
1126 * LOCKING:
1127 * caller.
1128 */
1129
Tejun Heo6a62a042006-02-13 10:02:46 +09001130void ata_id_string(const u16 *id, unsigned char *s,
1131 unsigned int ofs, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132{
1133 unsigned int c;
1134
Alan Cox963e4972008-07-24 17:16:06 +01001135 BUG_ON(len & 1);
1136
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 while (len > 0) {
1138 c = id[ofs] >> 8;
1139 *s = c;
1140 s++;
1141
1142 c = id[ofs] & 0xff;
1143 *s = c;
1144 s++;
1145
1146 ofs++;
1147 len -= 2;
1148 }
1149}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01001150EXPORT_SYMBOL_GPL(ata_id_string);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151
Tejun Heo0e949ff2006-02-12 22:47:04 +09001152/**
Tejun Heo6a62a042006-02-13 10:02:46 +09001153 * ata_id_c_string - Convert IDENTIFY DEVICE page into C string
Tejun Heo0e949ff2006-02-12 22:47:04 +09001154 * @id: IDENTIFY DEVICE results we will examine
1155 * @s: string into which data is output
1156 * @ofs: offset into identify device page
1157 * @len: length of string to return. must be an odd number.
1158 *
Tejun Heo6a62a042006-02-13 10:02:46 +09001159 * This function is identical to ata_id_string except that it
Tejun Heo0e949ff2006-02-12 22:47:04 +09001160 * trims trailing spaces and terminates the resulting string with
1161 * null. @len must be actual maximum length (even number) + 1.
1162 *
1163 * LOCKING:
1164 * caller.
1165 */
Tejun Heo6a62a042006-02-13 10:02:46 +09001166void ata_id_c_string(const u16 *id, unsigned char *s,
1167 unsigned int ofs, unsigned int len)
Tejun Heo0e949ff2006-02-12 22:47:04 +09001168{
1169 unsigned char *p;
1170
Tejun Heo6a62a042006-02-13 10:02:46 +09001171 ata_id_string(id, s, ofs, len - 1);
Tejun Heo0e949ff2006-02-12 22:47:04 +09001172
1173 p = s + strnlen(s, len - 1);
1174 while (p > s && p[-1] == ' ')
1175 p--;
1176 *p = '\0';
1177}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01001178EXPORT_SYMBOL_GPL(ata_id_c_string);
Edward Falk0baab862005-06-02 18:17:13 -04001179
Tejun Heodb6f8752007-09-03 12:31:58 +09001180static u64 ata_id_n_sectors(const u16 *id)
1181{
1182 if (ata_id_has_lba(id)) {
1183 if (ata_id_has_lba48(id))
Robert Hancock968e5942009-02-16 20:15:08 -06001184 return ata_id_u64(id, ATA_ID_LBA_CAPACITY_2);
Tejun Heodb6f8752007-09-03 12:31:58 +09001185 else
Robert Hancock968e5942009-02-16 20:15:08 -06001186 return ata_id_u32(id, ATA_ID_LBA_CAPACITY);
Tejun Heodb6f8752007-09-03 12:31:58 +09001187 } else {
1188 if (ata_id_current_chs_valid(id))
Robert Hancock968e5942009-02-16 20:15:08 -06001189 return id[ATA_ID_CUR_CYLS] * id[ATA_ID_CUR_HEADS] *
1190 id[ATA_ID_CUR_SECTORS];
Tejun Heodb6f8752007-09-03 12:31:58 +09001191 else
Robert Hancock968e5942009-02-16 20:15:08 -06001192 return id[ATA_ID_CYLS] * id[ATA_ID_HEADS] *
1193 id[ATA_ID_SECTORS];
Tejun Heodb6f8752007-09-03 12:31:58 +09001194 }
1195}
1196
Tejun Heoa5987e02008-03-27 19:14:23 +09001197u64 ata_tf_to_lba48(const struct ata_taskfile *tf)
Alan Cox1e999732007-04-11 00:23:13 +01001198{
1199 u64 sectors = 0;
1200
1201 sectors |= ((u64)(tf->hob_lbah & 0xff)) << 40;
1202 sectors |= ((u64)(tf->hob_lbam & 0xff)) << 32;
Roland Dreierba14a9c2008-10-28 16:52:20 -07001203 sectors |= ((u64)(tf->hob_lbal & 0xff)) << 24;
Alan Cox1e999732007-04-11 00:23:13 +01001204 sectors |= (tf->lbah & 0xff) << 16;
1205 sectors |= (tf->lbam & 0xff) << 8;
1206 sectors |= (tf->lbal & 0xff);
1207
Tejun Heoa5987e02008-03-27 19:14:23 +09001208 return sectors;
Alan Cox1e999732007-04-11 00:23:13 +01001209}
1210
Tejun Heoa5987e02008-03-27 19:14:23 +09001211u64 ata_tf_to_lba(const struct ata_taskfile *tf)
Alan Cox1e999732007-04-11 00:23:13 +01001212{
1213 u64 sectors = 0;
1214
1215 sectors |= (tf->device & 0x0f) << 24;
1216 sectors |= (tf->lbah & 0xff) << 16;
1217 sectors |= (tf->lbam & 0xff) << 8;
1218 sectors |= (tf->lbal & 0xff);
1219
Tejun Heoa5987e02008-03-27 19:14:23 +09001220 return sectors;
Alan Cox1e999732007-04-11 00:23:13 +01001221}
1222
1223/**
Tejun Heoc728a912007-09-03 12:32:30 +09001224 * ata_read_native_max_address - Read native max address
1225 * @dev: target device
1226 * @max_sectors: out parameter for the result native max address
Alan Cox1e999732007-04-11 00:23:13 +01001227 *
Tejun Heoc728a912007-09-03 12:32:30 +09001228 * Perform an LBA48 or LBA28 native size query upon the device in
1229 * question.
1230 *
1231 * RETURNS:
1232 * 0 on success, -EACCES if command is aborted by the drive.
1233 * -EIO on other errors.
Alan Cox1e999732007-04-11 00:23:13 +01001234 */
Tejun Heoc728a912007-09-03 12:32:30 +09001235static int ata_read_native_max_address(struct ata_device *dev, u64 *max_sectors)
Alan Cox1e999732007-04-11 00:23:13 +01001236{
Tejun Heoc728a912007-09-03 12:32:30 +09001237 unsigned int err_mask;
Alan Cox1e999732007-04-11 00:23:13 +01001238 struct ata_taskfile tf;
Tejun Heoc728a912007-09-03 12:32:30 +09001239 int lba48 = ata_id_has_lba48(dev->id);
Alan Cox1e999732007-04-11 00:23:13 +01001240
1241 ata_tf_init(dev, &tf);
1242
Tejun Heoc728a912007-09-03 12:32:30 +09001243 /* always clear all address registers */
Alan Cox1e999732007-04-11 00:23:13 +01001244 tf.flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
Tejun Heoc728a912007-09-03 12:32:30 +09001245
1246 if (lba48) {
1247 tf.command = ATA_CMD_READ_NATIVE_MAX_EXT;
1248 tf.flags |= ATA_TFLAG_LBA48;
1249 } else
1250 tf.command = ATA_CMD_READ_NATIVE_MAX;
1251
Hannes Reineckebd18bc02016-07-14 09:05:46 +09001252 tf.protocol = ATA_PROT_NODATA;
Tejun Heoc728a912007-09-03 12:32:30 +09001253 tf.device |= ATA_LBA;
Alan Cox1e999732007-04-11 00:23:13 +01001254
Tejun Heo2b789102007-10-09 15:05:44 +09001255 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
Tejun Heoc728a912007-09-03 12:32:30 +09001256 if (err_mask) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07001257 ata_dev_warn(dev,
1258 "failed to read native max address (err_mask=0x%x)\n",
1259 err_mask);
Tejun Heoc728a912007-09-03 12:32:30 +09001260 if (err_mask == AC_ERR_DEV && (tf.feature & ATA_ABORTED))
1261 return -EACCES;
1262 return -EIO;
1263 }
Alan Cox1e999732007-04-11 00:23:13 +01001264
Tejun Heoc728a912007-09-03 12:32:30 +09001265 if (lba48)
Tejun Heoa5987e02008-03-27 19:14:23 +09001266 *max_sectors = ata_tf_to_lba48(&tf) + 1;
Tejun Heoc728a912007-09-03 12:32:30 +09001267 else
Tejun Heoa5987e02008-03-27 19:14:23 +09001268 *max_sectors = ata_tf_to_lba(&tf) + 1;
Jeff Garzik2dcb4072007-10-19 06:42:56 -04001269 if (dev->horkage & ATA_HORKAGE_HPA_SIZE)
Alan Cox93328e112007-09-29 04:06:48 -04001270 (*max_sectors)--;
Tejun Heoc728a912007-09-03 12:32:30 +09001271 return 0;
Alan Cox1e999732007-04-11 00:23:13 +01001272}
1273
1274/**
Tejun Heoc728a912007-09-03 12:32:30 +09001275 * ata_set_max_sectors - Set max sectors
1276 * @dev: target device
Randy Dunlap6b38d1d2007-05-01 17:35:55 -07001277 * @new_sectors: new max sectors value to set for the device
Alan Cox1e999732007-04-11 00:23:13 +01001278 *
Tejun Heoc728a912007-09-03 12:32:30 +09001279 * Set max sectors of @dev to @new_sectors.
1280 *
1281 * RETURNS:
1282 * 0 on success, -EACCES if command is aborted or denied (due to
1283 * previous non-volatile SET_MAX) by the drive. -EIO on other
1284 * errors.
Alan Cox1e999732007-04-11 00:23:13 +01001285 */
Tejun Heo05027ad2007-09-03 12:32:57 +09001286static int ata_set_max_sectors(struct ata_device *dev, u64 new_sectors)
Alan Cox1e999732007-04-11 00:23:13 +01001287{
Tejun Heoc728a912007-09-03 12:32:30 +09001288 unsigned int err_mask;
Alan Cox1e999732007-04-11 00:23:13 +01001289 struct ata_taskfile tf;
Tejun Heoc728a912007-09-03 12:32:30 +09001290 int lba48 = ata_id_has_lba48(dev->id);
Alan Cox1e999732007-04-11 00:23:13 +01001291
1292 new_sectors--;
1293
1294 ata_tf_init(dev, &tf);
1295
Alan Cox1e999732007-04-11 00:23:13 +01001296 tf.flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
Tejun Heoc728a912007-09-03 12:32:30 +09001297
1298 if (lba48) {
1299 tf.command = ATA_CMD_SET_MAX_EXT;
1300 tf.flags |= ATA_TFLAG_LBA48;
1301
1302 tf.hob_lbal = (new_sectors >> 24) & 0xff;
1303 tf.hob_lbam = (new_sectors >> 32) & 0xff;
1304 tf.hob_lbah = (new_sectors >> 40) & 0xff;
Tejun Heo1e582ba2007-09-21 20:07:14 +09001305 } else {
Tejun Heoc728a912007-09-03 12:32:30 +09001306 tf.command = ATA_CMD_SET_MAX;
1307
Tejun Heo1e582ba2007-09-21 20:07:14 +09001308 tf.device |= (new_sectors >> 24) & 0xf;
1309 }
1310
Hannes Reineckebd18bc02016-07-14 09:05:46 +09001311 tf.protocol = ATA_PROT_NODATA;
Tejun Heoc728a912007-09-03 12:32:30 +09001312 tf.device |= ATA_LBA;
Alan Cox1e999732007-04-11 00:23:13 +01001313
1314 tf.lbal = (new_sectors >> 0) & 0xff;
1315 tf.lbam = (new_sectors >> 8) & 0xff;
1316 tf.lbah = (new_sectors >> 16) & 0xff;
Alan Cox1e999732007-04-11 00:23:13 +01001317
Tejun Heo2b789102007-10-09 15:05:44 +09001318 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
Tejun Heoc728a912007-09-03 12:32:30 +09001319 if (err_mask) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07001320 ata_dev_warn(dev,
1321 "failed to set max address (err_mask=0x%x)\n",
1322 err_mask);
Tejun Heoc728a912007-09-03 12:32:30 +09001323 if (err_mask == AC_ERR_DEV &&
1324 (tf.feature & (ATA_ABORTED | ATA_IDNF)))
1325 return -EACCES;
1326 return -EIO;
1327 }
Alan Cox1e999732007-04-11 00:23:13 +01001328
Tejun Heoc728a912007-09-03 12:32:30 +09001329 return 0;
Alan Cox1e999732007-04-11 00:23:13 +01001330}
1331
1332/**
1333 * ata_hpa_resize - Resize a device with an HPA set
1334 * @dev: Device to resize
1335 *
1336 * Read the size of an LBA28 or LBA48 disk with HPA features and resize
1337 * it if required to the full size of the media. The caller must check
1338 * the drive has the HPA feature set enabled.
Tejun Heo05027ad2007-09-03 12:32:57 +09001339 *
1340 * RETURNS:
1341 * 0 on success, -errno on failure.
Alan Cox1e999732007-04-11 00:23:13 +01001342 */
Tejun Heo05027ad2007-09-03 12:32:57 +09001343static int ata_hpa_resize(struct ata_device *dev)
Alan Cox1e999732007-04-11 00:23:13 +01001344{
Tejun Heo05027ad2007-09-03 12:32:57 +09001345 struct ata_eh_context *ehc = &dev->link->eh_context;
1346 int print_info = ehc->i.flags & ATA_EHI_PRINTINFO;
Tejun Heo445d2112010-04-05 10:33:13 +09001347 bool unlock_hpa = ata_ignore_hpa || dev->flags & ATA_DFLAG_UNLOCK_HPA;
Tejun Heo05027ad2007-09-03 12:32:57 +09001348 u64 sectors = ata_id_n_sectors(dev->id);
1349 u64 native_sectors;
Tejun Heoc728a912007-09-03 12:32:30 +09001350 int rc;
Jeff Garzika617c092007-05-21 20:14:23 -04001351
Tejun Heo05027ad2007-09-03 12:32:57 +09001352 /* do we need to do it? */
Hannes Reinecke9162c652014-11-05 13:08:21 +01001353 if ((dev->class != ATA_DEV_ATA && dev->class != ATA_DEV_ZAC) ||
Tejun Heo05027ad2007-09-03 12:32:57 +09001354 !ata_id_has_lba(dev->id) || !ata_id_hpa_enabled(dev->id) ||
1355 (dev->horkage & ATA_HORKAGE_BROKEN_HPA))
Tejun Heoc728a912007-09-03 12:32:30 +09001356 return 0;
Alan Cox1e999732007-04-11 00:23:13 +01001357
Tejun Heo05027ad2007-09-03 12:32:57 +09001358 /* read native max address */
1359 rc = ata_read_native_max_address(dev, &native_sectors);
1360 if (rc) {
Tejun Heodda7aba2008-03-23 21:05:15 +09001361 /* If device aborted the command or HPA isn't going to
1362 * be unlocked, skip HPA resizing.
Tejun Heo05027ad2007-09-03 12:32:57 +09001363 */
Tejun Heo445d2112010-04-05 10:33:13 +09001364 if (rc == -EACCES || !unlock_hpa) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07001365 ata_dev_warn(dev,
1366 "HPA support seems broken, skipping HPA handling\n");
Tejun Heo05027ad2007-09-03 12:32:57 +09001367 dev->horkage |= ATA_HORKAGE_BROKEN_HPA;
Alan Cox1e999732007-04-11 00:23:13 +01001368
Tejun Heo05027ad2007-09-03 12:32:57 +09001369 /* we can continue if device aborted the command */
1370 if (rc == -EACCES)
1371 rc = 0;
Alan Cox1e999732007-04-11 00:23:13 +01001372 }
Tejun Heo37301a52007-06-25 20:45:54 +09001373
Tejun Heo05027ad2007-09-03 12:32:57 +09001374 return rc;
1375 }
Tejun Heo5920dad2009-07-15 17:11:41 +09001376 dev->n_native_sectors = native_sectors;
Tejun Heo05027ad2007-09-03 12:32:57 +09001377
1378 /* nothing to do? */
Tejun Heo445d2112010-04-05 10:33:13 +09001379 if (native_sectors <= sectors || !unlock_hpa) {
Tejun Heo05027ad2007-09-03 12:32:57 +09001380 if (!print_info || native_sectors == sectors)
1381 return 0;
1382
1383 if (native_sectors > sectors)
Joe Perchesa9a79df2011-04-15 15:51:59 -07001384 ata_dev_info(dev,
Tejun Heo05027ad2007-09-03 12:32:57 +09001385 "HPA detected: current %llu, native %llu\n",
1386 (unsigned long long)sectors,
1387 (unsigned long long)native_sectors);
1388 else if (native_sectors < sectors)
Joe Perchesa9a79df2011-04-15 15:51:59 -07001389 ata_dev_warn(dev,
1390 "native sectors (%llu) is smaller than sectors (%llu)\n",
Tejun Heo05027ad2007-09-03 12:32:57 +09001391 (unsigned long long)native_sectors,
1392 (unsigned long long)sectors);
1393 return 0;
1394 }
1395
1396 /* let's unlock HPA */
1397 rc = ata_set_max_sectors(dev, native_sectors);
1398 if (rc == -EACCES) {
1399 /* if device aborted the command, skip HPA resizing */
Joe Perchesa9a79df2011-04-15 15:51:59 -07001400 ata_dev_warn(dev,
1401 "device aborted resize (%llu -> %llu), skipping HPA handling\n",
1402 (unsigned long long)sectors,
1403 (unsigned long long)native_sectors);
Tejun Heo05027ad2007-09-03 12:32:57 +09001404 dev->horkage |= ATA_HORKAGE_BROKEN_HPA;
1405 return 0;
1406 } else if (rc)
1407 return rc;
1408
1409 /* re-read IDENTIFY data */
1410 rc = ata_dev_reread_id(dev, 0);
1411 if (rc) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07001412 ata_dev_err(dev,
1413 "failed to re-read IDENTIFY data after HPA resizing\n");
Tejun Heo05027ad2007-09-03 12:32:57 +09001414 return rc;
1415 }
1416
1417 if (print_info) {
1418 u64 new_sectors = ata_id_n_sectors(dev->id);
Joe Perchesa9a79df2011-04-15 15:51:59 -07001419 ata_dev_info(dev,
Tejun Heo05027ad2007-09-03 12:32:57 +09001420 "HPA unlocked: %llu -> %llu, native %llu\n",
1421 (unsigned long long)sectors,
1422 (unsigned long long)new_sectors,
1423 (unsigned long long)native_sectors);
1424 }
1425
1426 return 0;
Alan Cox1e999732007-04-11 00:23:13 +01001427}
1428
Edward Falk0baab862005-06-02 18:17:13 -04001429/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 * ata_dump_id - IDENTIFY DEVICE info debugging output
Tejun Heo0bd33002006-02-12 22:47:05 +09001431 * @id: IDENTIFY DEVICE page to dump
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 *
Tejun Heo0bd33002006-02-12 22:47:05 +09001433 * Dump selected 16-bit words from the given IDENTIFY DEVICE
1434 * page.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 *
1436 * LOCKING:
1437 * caller.
1438 */
1439
Tejun Heo0bd33002006-02-12 22:47:05 +09001440static inline void ata_dump_id(const u16 *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441{
1442 DPRINTK("49==0x%04x "
1443 "53==0x%04x "
1444 "63==0x%04x "
1445 "64==0x%04x "
1446 "75==0x%04x \n",
Tejun Heo0bd33002006-02-12 22:47:05 +09001447 id[49],
1448 id[53],
1449 id[63],
1450 id[64],
1451 id[75]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 DPRINTK("80==0x%04x "
1453 "81==0x%04x "
1454 "82==0x%04x "
1455 "83==0x%04x "
1456 "84==0x%04x \n",
Tejun Heo0bd33002006-02-12 22:47:05 +09001457 id[80],
1458 id[81],
1459 id[82],
1460 id[83],
1461 id[84]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 DPRINTK("88==0x%04x "
1463 "93==0x%04x\n",
Tejun Heo0bd33002006-02-12 22:47:05 +09001464 id[88],
1465 id[93]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466}
1467
Tejun Heocb95d562006-03-06 04:31:56 +09001468/**
1469 * ata_id_xfermask - Compute xfermask from the given IDENTIFY data
1470 * @id: IDENTIFY data to compute xfer mask from
1471 *
1472 * Compute the xfermask for this device. This is not as trivial
1473 * as it seems if we must consider early devices correctly.
1474 *
1475 * FIXME: pre IDE drive timing (do we care ?).
1476 *
1477 * LOCKING:
1478 * None.
1479 *
1480 * RETURNS:
1481 * Computed xfermask
1482 */
Tejun Heo7dc951a2007-11-27 19:43:42 +09001483unsigned long ata_id_xfermask(const u16 *id)
Tejun Heocb95d562006-03-06 04:31:56 +09001484{
Tejun Heo7dc951a2007-11-27 19:43:42 +09001485 unsigned long pio_mask, mwdma_mask, udma_mask;
Tejun Heocb95d562006-03-06 04:31:56 +09001486
1487 /* Usual case. Word 53 indicates word 64 is valid */
1488 if (id[ATA_ID_FIELD_VALID] & (1 << 1)) {
1489 pio_mask = id[ATA_ID_PIO_MODES] & 0x03;
1490 pio_mask <<= 3;
1491 pio_mask |= 0x7;
1492 } else {
1493 /* If word 64 isn't valid then Word 51 high byte holds
1494 * the PIO timing number for the maximum. Turn it into
1495 * a mask.
1496 */
Lennert Buytenhek7a0f1c82007-01-29 13:28:47 +01001497 u8 mode = (id[ATA_ID_OLD_PIO_MODES] >> 8) & 0xFF;
Alan Cox46767aeb2006-09-29 18:26:47 +01001498 if (mode < 5) /* Valid PIO range */
Jeff Garzik2dcb4072007-10-19 06:42:56 -04001499 pio_mask = (2 << mode) - 1;
Alan Cox46767aeb2006-09-29 18:26:47 +01001500 else
1501 pio_mask = 1;
Tejun Heocb95d562006-03-06 04:31:56 +09001502
1503 /* But wait.. there's more. Design your standards by
1504 * committee and you too can get a free iordy field to
1505 * process. However its the speeds not the modes that
1506 * are supported... Note drivers using the timing API
1507 * will get this right anyway
1508 */
1509 }
1510
1511 mwdma_mask = id[ATA_ID_MWDMA_MODES] & 0x07;
Tejun Heofb21f0d2006-03-12 12:34:35 +09001512
Alan Coxb352e572006-08-10 18:52:12 +01001513 if (ata_id_is_cfa(id)) {
1514 /*
1515 * Process compact flash extended modes
1516 */
Sergei Shtylyov62afe5d2009-04-13 20:50:00 +04001517 int pio = (id[ATA_ID_CFA_MODES] >> 0) & 0x7;
1518 int dma = (id[ATA_ID_CFA_MODES] >> 3) & 0x7;
Alan Coxb352e572006-08-10 18:52:12 +01001519
1520 if (pio)
1521 pio_mask |= (1 << 5);
1522 if (pio > 1)
1523 pio_mask |= (1 << 6);
1524 if (dma)
1525 mwdma_mask |= (1 << 3);
1526 if (dma > 1)
1527 mwdma_mask |= (1 << 4);
1528 }
1529
Tejun Heofb21f0d2006-03-12 12:34:35 +09001530 udma_mask = 0;
1531 if (id[ATA_ID_FIELD_VALID] & (1 << 2))
1532 udma_mask = id[ATA_ID_UDMA_MODES] & 0xff;
Tejun Heocb95d562006-03-06 04:31:56 +09001533
1534 return ata_pack_xfermask(pio_mask, mwdma_mask, udma_mask);
1535}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01001536EXPORT_SYMBOL_GPL(ata_id_xfermask);
Tejun Heocb95d562006-03-06 04:31:56 +09001537
Adrian Bunk7102d232007-01-04 00:09:36 +01001538static void ata_qc_complete_internal(struct ata_queued_cmd *qc)
Tejun Heoa2a7a662005-12-13 14:48:31 +09001539{
Tejun Heo77853bf2006-01-23 13:09:36 +09001540 struct completion *waiting = qc->private_data;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001541
Tejun Heoa2a7a662005-12-13 14:48:31 +09001542 complete(waiting);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001543}
1544
1545/**
Tejun Heo24326972006-11-14 22:47:09 +09001546 * ata_exec_internal_sg - execute libata internal command
Tejun Heoa2a7a662005-12-13 14:48:31 +09001547 * @dev: Device to which the command is sent
1548 * @tf: Taskfile registers for the command and the result
Tejun Heod69cf372006-04-02 18:51:53 +09001549 * @cdb: CDB for packet command
Masanari Iidae2278672014-02-18 22:54:36 +09001550 * @dma_dir: Data transfer direction of the command
Randy Dunlap5c1ad8b2007-10-18 14:12:26 -07001551 * @sgl: sg list for the data buffer of the command
Tejun Heo24326972006-11-14 22:47:09 +09001552 * @n_elem: Number of sg entries
Tejun Heo2b789102007-10-09 15:05:44 +09001553 * @timeout: Timeout in msecs (0 for default)
Tejun Heoa2a7a662005-12-13 14:48:31 +09001554 *
1555 * Executes libata internal command with timeout. @tf contains
1556 * command on entry and result on return. Timeout and error
1557 * conditions are reported via return value. No recovery action
1558 * is taken after a command times out. It's caller's duty to
1559 * clean up after timeout.
1560 *
1561 * LOCKING:
1562 * None. Should be called with kernel context, might sleep.
Tejun Heo551e8882006-06-12 14:09:49 +09001563 *
1564 * RETURNS:
1565 * Zero on success, AC_ERR_* mask on failure
Tejun Heoa2a7a662005-12-13 14:48:31 +09001566 */
Tejun Heo24326972006-11-14 22:47:09 +09001567unsigned ata_exec_internal_sg(struct ata_device *dev,
1568 struct ata_taskfile *tf, const u8 *cdb,
Jens Axboe87260212007-10-16 11:14:12 +02001569 int dma_dir, struct scatterlist *sgl,
Tejun Heo2b789102007-10-09 15:05:44 +09001570 unsigned int n_elem, unsigned long timeout)
Tejun Heoa2a7a662005-12-13 14:48:31 +09001571{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001572 struct ata_link *link = dev->link;
1573 struct ata_port *ap = link->ap;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001574 u8 command = tf->command;
Tejun Heo87fbc5a2008-05-20 02:17:54 +09001575 int auto_timeout = 0;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001576 struct ata_queued_cmd *qc;
Jens Axboe28361c42018-05-11 12:51:09 -06001577 unsigned int preempted_tag;
Jens Axboee3ed89392018-05-11 12:51:05 -06001578 u32 preempted_sactive;
1579 u64 preempted_qc_active;
Tejun Heoda917d62007-09-23 13:14:12 +09001580 int preempted_nr_active_links;
Ingo Molnar60be6b92006-07-03 00:25:26 -07001581 DECLARE_COMPLETION_ONSTACK(wait);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001582 unsigned long flags;
Tejun Heo77853bf2006-01-23 13:09:36 +09001583 unsigned int err_mask;
Tejun Heod95a7172006-05-15 20:58:14 +09001584 int rc;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001585
Jeff Garzikba6a1302006-06-22 23:46:10 -04001586 spin_lock_irqsave(ap->lock, flags);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001587
Tejun Heoe3180492006-05-15 20:58:09 +09001588 /* no internal command while frozen */
Tejun Heob51e9e52006-06-29 01:29:30 +09001589 if (ap->pflags & ATA_PFLAG_FROZEN) {
Jeff Garzikba6a1302006-06-22 23:46:10 -04001590 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoe3180492006-05-15 20:58:09 +09001591 return AC_ERR_SYSTEM;
1592 }
1593
Tejun Heo2ab7db12006-05-15 20:58:02 +09001594 /* initialize internal qc */
Jens Axboe28361c42018-05-11 12:51:09 -06001595 qc = __ata_qc_from_tag(ap, ATA_TAG_INTERNAL);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001596
Jens Axboe28361c42018-05-11 12:51:09 -06001597 qc->tag = ATA_TAG_INTERNAL;
1598 qc->hw_tag = 0;
Tejun Heo2ab7db12006-05-15 20:58:02 +09001599 qc->scsicmd = NULL;
1600 qc->ap = ap;
1601 qc->dev = dev;
1602 ata_qc_reinit(qc);
1603
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001604 preempted_tag = link->active_tag;
1605 preempted_sactive = link->sactive;
Tejun Heodedaf2b2006-05-15 21:03:43 +09001606 preempted_qc_active = ap->qc_active;
Tejun Heoda917d62007-09-23 13:14:12 +09001607 preempted_nr_active_links = ap->nr_active_links;
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001608 link->active_tag = ATA_TAG_POISON;
1609 link->sactive = 0;
Tejun Heodedaf2b2006-05-15 21:03:43 +09001610 ap->qc_active = 0;
Tejun Heoda917d62007-09-23 13:14:12 +09001611 ap->nr_active_links = 0;
Tejun Heo2ab7db12006-05-15 20:58:02 +09001612
1613 /* prepare & issue qc */
Tejun Heoa2a7a662005-12-13 14:48:31 +09001614 qc->tf = *tf;
Tejun Heod69cf372006-04-02 18:51:53 +09001615 if (cdb)
1616 memcpy(qc->cdb, cdb, ATAPI_CDB_LEN);
Vincent Pelletiere7714512013-05-18 18:44:04 +02001617
1618 /* some SATA bridges need us to indicate data xfer direction */
1619 if (tf->protocol == ATAPI_PROT_DMA && (dev->flags & ATA_DFLAG_DMADIR) &&
1620 dma_dir == DMA_FROM_DEVICE)
1621 qc->tf.feature |= ATAPI_DMADIR;
1622
Tejun Heoe61e0672006-05-15 20:57:40 +09001623 qc->flags |= ATA_QCFLAG_RESULT_TF;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001624 qc->dma_dir = dma_dir;
1625 if (dma_dir != DMA_NONE) {
Tejun Heo24326972006-11-14 22:47:09 +09001626 unsigned int i, buflen = 0;
Jens Axboe87260212007-10-16 11:14:12 +02001627 struct scatterlist *sg;
Tejun Heo24326972006-11-14 22:47:09 +09001628
Jens Axboe87260212007-10-16 11:14:12 +02001629 for_each_sg(sgl, sg, n_elem, i)
1630 buflen += sg->length;
Tejun Heo24326972006-11-14 22:47:09 +09001631
Jens Axboe87260212007-10-16 11:14:12 +02001632 ata_sg_init(qc, sgl, n_elem);
Brian King49c80422007-01-30 11:32:26 -06001633 qc->nbytes = buflen;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001634 }
1635
Tejun Heo77853bf2006-01-23 13:09:36 +09001636 qc->private_data = &wait;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001637 qc->complete_fn = ata_qc_complete_internal;
1638
Tejun Heo8e0e6942006-03-31 20:41:11 +09001639 ata_qc_issue(qc);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001640
Jeff Garzikba6a1302006-06-22 23:46:10 -04001641 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001642
Tejun Heo87fbc5a2008-05-20 02:17:54 +09001643 if (!timeout) {
1644 if (ata_probe_timeout)
1645 timeout = ata_probe_timeout * 1000;
1646 else {
1647 timeout = ata_internal_cmd_timeout(dev, command);
1648 auto_timeout = 1;
1649 }
1650 }
Tejun Heo2b789102007-10-09 15:05:44 +09001651
Tejun Heoc0c362b2010-09-06 17:57:14 +02001652 if (ap->ops->error_handler)
1653 ata_eh_release(ap);
1654
Tejun Heo2b789102007-10-09 15:05:44 +09001655 rc = wait_for_completion_timeout(&wait, msecs_to_jiffies(timeout));
Albert Lee41ade502006-03-14 11:19:04 +08001656
Tejun Heoc0c362b2010-09-06 17:57:14 +02001657 if (ap->ops->error_handler)
1658 ata_eh_acquire(ap);
1659
Tejun Heoc4291372010-05-10 21:41:38 +02001660 ata_sff_flush_pio_task(ap);
Tejun Heod95a7172006-05-15 20:58:14 +09001661
1662 if (!rc) {
Jeff Garzikba6a1302006-06-22 23:46:10 -04001663 spin_lock_irqsave(ap->lock, flags);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001664
1665 /* We're racing with irq here. If we lose, the
1666 * following test prevents us from completing the qc
Tejun Heod95a7172006-05-15 20:58:14 +09001667 * twice. If we win, the port is frozen and will be
1668 * cleaned up by ->post_internal_cmd().
Tejun Heoa2a7a662005-12-13 14:48:31 +09001669 */
Tejun Heo77853bf2006-01-23 13:09:36 +09001670 if (qc->flags & ATA_QCFLAG_ACTIVE) {
Tejun Heod95a7172006-05-15 20:58:14 +09001671 qc->err_mask |= AC_ERR_TIMEOUT;
1672
1673 if (ap->ops->error_handler)
1674 ata_port_freeze(ap);
1675 else
1676 ata_qc_complete(qc);
Tejun Heof15a1da2006-05-15 20:57:56 +09001677
Borislav Petkov0dd4b212006-06-23 02:29:08 -04001678 if (ata_msg_warn(ap))
Joe Perchesa9a79df2011-04-15 15:51:59 -07001679 ata_dev_warn(dev, "qc timeout (cmd 0x%x)\n",
1680 command);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001681 }
1682
Jeff Garzikba6a1302006-06-22 23:46:10 -04001683 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001684 }
1685
Tejun Heod95a7172006-05-15 20:58:14 +09001686 /* do post_internal_cmd */
1687 if (ap->ops->post_internal_cmd)
1688 ap->ops->post_internal_cmd(qc);
1689
Tejun Heoa51d6442007-03-20 15:24:11 +09001690 /* perform minimal error analysis */
1691 if (qc->flags & ATA_QCFLAG_FAILED) {
1692 if (qc->result_tf.command & (ATA_ERR | ATA_DF))
1693 qc->err_mask |= AC_ERR_DEV;
1694
1695 if (!qc->err_mask)
1696 qc->err_mask |= AC_ERR_OTHER;
1697
1698 if (qc->err_mask & ~AC_ERR_OTHER)
1699 qc->err_mask &= ~AC_ERR_OTHER;
Damien Le Moal2dae9952016-12-19 10:17:40 +09001700 } else if (qc->tf.command == ATA_CMD_REQ_SENSE_DATA) {
1701 qc->result_tf.command |= ATA_SENSE;
Tejun Heod95a7172006-05-15 20:58:14 +09001702 }
1703
Tejun Heo15869302006-05-15 20:57:33 +09001704 /* finish up */
Jeff Garzikba6a1302006-06-22 23:46:10 -04001705 spin_lock_irqsave(ap->lock, flags);
Tejun Heo15869302006-05-15 20:57:33 +09001706
Tejun Heoe61e0672006-05-15 20:57:40 +09001707 *tf = qc->result_tf;
Tejun Heo77853bf2006-01-23 13:09:36 +09001708 err_mask = qc->err_mask;
1709
1710 ata_qc_free(qc);
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001711 link->active_tag = preempted_tag;
1712 link->sactive = preempted_sactive;
Tejun Heodedaf2b2006-05-15 21:03:43 +09001713 ap->qc_active = preempted_qc_active;
Tejun Heoda917d62007-09-23 13:14:12 +09001714 ap->nr_active_links = preempted_nr_active_links;
Tejun Heo77853bf2006-01-23 13:09:36 +09001715
Jeff Garzikba6a1302006-06-22 23:46:10 -04001716 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heo15869302006-05-15 20:57:33 +09001717
Tejun Heo87fbc5a2008-05-20 02:17:54 +09001718 if ((err_mask & AC_ERR_TIMEOUT) && auto_timeout)
1719 ata_internal_cmd_timed_out(dev, command);
1720
Tejun Heo77853bf2006-01-23 13:09:36 +09001721 return err_mask;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001722}
1723
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724/**
Tejun Heo33480a02006-12-12 02:15:31 +09001725 * ata_exec_internal - execute libata internal command
Tejun Heo24326972006-11-14 22:47:09 +09001726 * @dev: Device to which the command is sent
1727 * @tf: Taskfile registers for the command and the result
1728 * @cdb: CDB for packet command
Masanari Iidae2278672014-02-18 22:54:36 +09001729 * @dma_dir: Data transfer direction of the command
Tejun Heo24326972006-11-14 22:47:09 +09001730 * @buf: Data buffer of the command
1731 * @buflen: Length of data buffer
Tejun Heo2b789102007-10-09 15:05:44 +09001732 * @timeout: Timeout in msecs (0 for default)
Tejun Heo24326972006-11-14 22:47:09 +09001733 *
1734 * Wrapper around ata_exec_internal_sg() which takes simple
1735 * buffer instead of sg list.
1736 *
1737 * LOCKING:
1738 * None. Should be called with kernel context, might sleep.
1739 *
1740 * RETURNS:
1741 * Zero on success, AC_ERR_* mask on failure
1742 */
1743unsigned ata_exec_internal(struct ata_device *dev,
1744 struct ata_taskfile *tf, const u8 *cdb,
Tejun Heo2b789102007-10-09 15:05:44 +09001745 int dma_dir, void *buf, unsigned int buflen,
1746 unsigned long timeout)
Tejun Heo24326972006-11-14 22:47:09 +09001747{
Tejun Heo33480a02006-12-12 02:15:31 +09001748 struct scatterlist *psg = NULL, sg;
1749 unsigned int n_elem = 0;
Tejun Heo24326972006-11-14 22:47:09 +09001750
Tejun Heo33480a02006-12-12 02:15:31 +09001751 if (dma_dir != DMA_NONE) {
1752 WARN_ON(!buf);
1753 sg_init_one(&sg, buf, buflen);
1754 psg = &sg;
1755 n_elem++;
1756 }
Tejun Heo24326972006-11-14 22:47:09 +09001757
Tejun Heo2b789102007-10-09 15:05:44 +09001758 return ata_exec_internal_sg(dev, tf, cdb, dma_dir, psg, n_elem,
1759 timeout);
Tejun Heo24326972006-11-14 22:47:09 +09001760}
1761
1762/**
Alan Cox1bc4ccf2006-01-09 17:18:14 +00001763 * ata_pio_need_iordy - check if iordy needed
1764 * @adev: ATA device
1765 *
1766 * Check if the current speed of the device requires IORDY. Used
1767 * by various controllers for chip configuration.
1768 */
Alan Cox1bc4ccf2006-01-09 17:18:14 +00001769unsigned int ata_pio_need_iordy(const struct ata_device *adev)
1770{
Tejun Heo0d9e6652009-06-11 11:04:45 +09001771 /* Don't set IORDY if we're preparing for reset. IORDY may
1772 * lead to controller lock up on certain controllers if the
1773 * port is not occupied. See bko#11703 for details.
1774 */
1775 if (adev->link->ap->pflags & ATA_PFLAG_RESETTING)
1776 return 0;
1777 /* Controller doesn't support IORDY. Probably a pointless
1778 * check as the caller should know this.
1779 */
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001780 if (adev->link->ap->flags & ATA_FLAG_NO_IORDY)
Alan Cox1bc4ccf2006-01-09 17:18:14 +00001781 return 0;
David Daney5c18c4d2008-12-10 15:39:12 -08001782 /* CF spec. r4.1 Table 22 says no iordy on PIO5 and PIO6. */
1783 if (ata_id_is_cfa(adev->id)
1784 && (adev->pio_mode == XFER_PIO_5 || adev->pio_mode == XFER_PIO_6))
1785 return 0;
Alan Cox432729f2007-03-08 23:22:59 +00001786 /* PIO3 and higher it is mandatory */
1787 if (adev->pio_mode > XFER_PIO_2)
Alan Cox1bc4ccf2006-01-09 17:18:14 +00001788 return 1;
Alan Cox432729f2007-03-08 23:22:59 +00001789 /* We turn it on when possible */
1790 if (ata_id_has_iordy(adev->id))
1791 return 1;
1792 return 0;
1793}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01001794EXPORT_SYMBOL_GPL(ata_pio_need_iordy);
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05001795
Alan Cox432729f2007-03-08 23:22:59 +00001796/**
1797 * ata_pio_mask_no_iordy - Return the non IORDY mask
1798 * @adev: ATA device
1799 *
1800 * Compute the highest mode possible if we are not using iordy. Return
1801 * -1 if no iordy mode is available.
1802 */
Alan Cox432729f2007-03-08 23:22:59 +00001803static u32 ata_pio_mask_no_iordy(const struct ata_device *adev)
1804{
Alan Cox1bc4ccf2006-01-09 17:18:14 +00001805 /* If we have no drive specific rule, then PIO 2 is non IORDY */
Alan Cox1bc4ccf2006-01-09 17:18:14 +00001806 if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE */
Alan Cox432729f2007-03-08 23:22:59 +00001807 u16 pio = adev->id[ATA_ID_EIDE_PIO];
Alan Cox1bc4ccf2006-01-09 17:18:14 +00001808 /* Is the speed faster than the drive allows non IORDY ? */
1809 if (pio) {
1810 /* This is cycle times not frequency - watch the logic! */
1811 if (pio > 240) /* PIO2 is 240nS per cycle */
Alan Cox432729f2007-03-08 23:22:59 +00001812 return 3 << ATA_SHIFT_PIO;
1813 return 7 << ATA_SHIFT_PIO;
Alan Cox1bc4ccf2006-01-09 17:18:14 +00001814 }
1815 }
Alan Cox432729f2007-03-08 23:22:59 +00001816 return 3 << ATA_SHIFT_PIO;
Alan Cox1bc4ccf2006-01-09 17:18:14 +00001817}
1818
1819/**
Alan Cox963e4972008-07-24 17:16:06 +01001820 * ata_do_dev_read_id - default ID read method
1821 * @dev: device
1822 * @tf: proposed taskfile
1823 * @id: data buffer
1824 *
1825 * Issue the identify taskfile and hand back the buffer containing
1826 * identify data. For some RAID controllers and for pre ATA devices
1827 * this function is wrapped or replaced by the driver
1828 */
1829unsigned int ata_do_dev_read_id(struct ata_device *dev,
1830 struct ata_taskfile *tf, u16 *id)
1831{
1832 return ata_exec_internal(dev, tf, NULL, DMA_FROM_DEVICE,
1833 id, sizeof(id[0]) * ATA_ID_WORDS, 0);
1834}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01001835EXPORT_SYMBOL_GPL(ata_do_dev_read_id);
Alan Cox963e4972008-07-24 17:16:06 +01001836
1837/**
Tejun Heo49016ac2006-02-21 02:12:11 +09001838 * ata_dev_read_id - Read ID data from the specified device
Tejun Heo49016ac2006-02-21 02:12:11 +09001839 * @dev: target device
1840 * @p_class: pointer to class of the target device (may be changed)
Tejun Heobff04642006-11-10 18:08:10 +09001841 * @flags: ATA_READID_* flags
Tejun Heofe635c72006-05-15 20:57:35 +09001842 * @id: buffer to read IDENTIFY data into
Tejun Heo49016ac2006-02-21 02:12:11 +09001843 *
1844 * Read ID data from the specified device. ATA_CMD_ID_ATA is
1845 * performed on ATA devices and ATA_CMD_ID_ATAPI on ATAPI
Tejun Heoaec5c3c2006-03-25 01:33:34 +09001846 * devices. This function also issues ATA_CMD_INIT_DEV_PARAMS
1847 * for pre-ATA4 drives.
Tejun Heo49016ac2006-02-21 02:12:11 +09001848 *
Alan Cox50a99012007-08-08 14:27:00 +01001849 * FIXME: ATA_CMD_ID_ATA is optional for early drives and right
Jeff Garzik2dcb4072007-10-19 06:42:56 -04001850 * now we abort if we hit that case.
Alan Cox50a99012007-08-08 14:27:00 +01001851 *
Tejun Heo49016ac2006-02-21 02:12:11 +09001852 * LOCKING:
1853 * Kernel thread context (may sleep)
1854 *
1855 * RETURNS:
1856 * 0 on success, -errno otherwise.
1857 */
Tejun Heoa9beec92006-05-31 18:27:44 +09001858int ata_dev_read_id(struct ata_device *dev, unsigned int *p_class,
Tejun Heobff04642006-11-10 18:08:10 +09001859 unsigned int flags, u16 *id)
Tejun Heo49016ac2006-02-21 02:12:11 +09001860{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001861 struct ata_port *ap = dev->link->ap;
Tejun Heo49016ac2006-02-21 02:12:11 +09001862 unsigned int class = *p_class;
Tejun Heo49016ac2006-02-21 02:12:11 +09001863 struct ata_taskfile tf;
1864 unsigned int err_mask = 0;
1865 const char *reason;
Tejun Heo79b42ba2009-04-15 06:21:10 +09001866 bool is_semb = class == ATA_DEV_SEMB;
Tejun Heo54936f82007-05-11 14:35:29 +02001867 int may_fallback = 1, tried_spinup = 0;
Tejun Heo49016ac2006-02-21 02:12:11 +09001868 int rc;
1869
Borislav Petkov0dd4b212006-06-23 02:29:08 -04001870 if (ata_msg_ctl(ap))
Joe Perchesa9a79df2011-04-15 15:51:59 -07001871 ata_dev_dbg(dev, "%s: ENTER\n", __func__);
Tejun Heo49016ac2006-02-21 02:12:11 +09001872
Alan Cox963e4972008-07-24 17:16:06 +01001873retry:
Tejun Heo3373efd2006-05-15 20:57:53 +09001874 ata_tf_init(dev, &tf);
Tejun Heo49016ac2006-02-21 02:12:11 +09001875
1876 switch (class) {
Tejun Heo79b42ba2009-04-15 06:21:10 +09001877 case ATA_DEV_SEMB:
1878 class = ATA_DEV_ATA; /* some hard drives report SEMB sig */
Gustavo A. R. Silva05b83602017-10-12 14:19:16 -05001879 /* fall through */
Tejun Heo49016ac2006-02-21 02:12:11 +09001880 case ATA_DEV_ATA:
Hannes Reinecke9162c652014-11-05 13:08:21 +01001881 case ATA_DEV_ZAC:
Tejun Heo49016ac2006-02-21 02:12:11 +09001882 tf.command = ATA_CMD_ID_ATA;
1883 break;
1884 case ATA_DEV_ATAPI:
1885 tf.command = ATA_CMD_ID_ATAPI;
1886 break;
1887 default:
1888 rc = -ENODEV;
1889 reason = "unsupported class";
1890 goto err_out;
1891 }
1892
1893 tf.protocol = ATA_PROT_PIO;
Tejun Heo81afe892007-02-07 12:37:41 -08001894
1895 /* Some devices choke if TF registers contain garbage. Make
1896 * sure those are properly initialized.
1897 */
1898 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
1899
1900 /* Device presence detection is unreliable on some
1901 * controllers. Always poll IDENTIFY if available.
1902 */
1903 tf.flags |= ATA_TFLAG_POLLING;
Tejun Heo49016ac2006-02-21 02:12:11 +09001904
Alan Cox963e4972008-07-24 17:16:06 +01001905 if (ap->ops->read_id)
1906 err_mask = ap->ops->read_id(dev, &tf, id);
1907 else
1908 err_mask = ata_do_dev_read_id(dev, &tf, id);
1909
Tejun Heo49016ac2006-02-21 02:12:11 +09001910 if (err_mask) {
Tejun Heo800b3992006-12-03 21:34:13 +09001911 if (err_mask & AC_ERR_NODEV_HINT) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07001912 ata_dev_dbg(dev, "NODEV after polling detection\n");
Tejun Heo55a8e2c2006-11-10 18:08:10 +09001913 return -ENOENT;
1914 }
1915
Tejun Heo79b42ba2009-04-15 06:21:10 +09001916 if (is_semb) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07001917 ata_dev_info(dev,
1918 "IDENTIFY failed on device w/ SEMB sig, disabled\n");
Tejun Heo79b42ba2009-04-15 06:21:10 +09001919 /* SEMB is not supported yet */
1920 *p_class = ATA_DEV_SEMB_UNSUP;
1921 return 0;
1922 }
1923
Tejun Heo1ffc1512008-03-23 15:16:53 +09001924 if ((err_mask == AC_ERR_DEV) && (tf.feature & ATA_ABORTED)) {
1925 /* Device or controller might have reported
1926 * the wrong device class. Give a shot at the
1927 * other IDENTIFY if the current one is
1928 * aborted by the device.
1929 */
1930 if (may_fallback) {
1931 may_fallback = 0;
Tejun Heo54936f82007-05-11 14:35:29 +02001932
Tejun Heo1ffc1512008-03-23 15:16:53 +09001933 if (class == ATA_DEV_ATA)
1934 class = ATA_DEV_ATAPI;
1935 else
1936 class = ATA_DEV_ATA;
1937 goto retry;
1938 }
1939
1940 /* Control reaches here iff the device aborted
1941 * both flavors of IDENTIFYs which happens
1942 * sometimes with phantom devices.
1943 */
Joe Perchesa9a79df2011-04-15 15:51:59 -07001944 ata_dev_dbg(dev,
1945 "both IDENTIFYs aborted, assuming NODEV\n");
Tejun Heo1ffc1512008-03-23 15:16:53 +09001946 return -ENOENT;
Tejun Heo54936f82007-05-11 14:35:29 +02001947 }
1948
Tejun Heo49016ac2006-02-21 02:12:11 +09001949 rc = -EIO;
1950 reason = "I/O error";
Tejun Heo49016ac2006-02-21 02:12:11 +09001951 goto err_out;
1952 }
1953
Tejun Heo43c9c592010-05-23 12:59:11 +02001954 if (dev->horkage & ATA_HORKAGE_DUMP_ID) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07001955 ata_dev_dbg(dev, "dumping IDENTIFY data, "
1956 "class=%d may_fallback=%d tried_spinup=%d\n",
1957 class, may_fallback, tried_spinup);
Tejun Heo43c9c592010-05-23 12:59:11 +02001958 print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET,
1959 16, 2, id, ATA_ID_WORDS * sizeof(*id), true);
1960 }
1961
Tejun Heo54936f82007-05-11 14:35:29 +02001962 /* Falling back doesn't make sense if ID data was read
1963 * successfully at least once.
1964 */
1965 may_fallback = 0;
1966
Tejun Heo49016ac2006-02-21 02:12:11 +09001967 swap_buf_le16(id, ATA_ID_WORDS);
1968
Tejun Heo49016ac2006-02-21 02:12:11 +09001969 /* sanity check */
Tejun Heoa4f57492006-09-12 20:35:49 -07001970 rc = -EINVAL;
Alan Cox60700682007-06-07 16:13:55 +01001971 reason = "device reports invalid type";
Tejun Heoa4f57492006-09-12 20:35:49 -07001972
Hannes Reinecke9162c652014-11-05 13:08:21 +01001973 if (class == ATA_DEV_ATA || class == ATA_DEV_ZAC) {
Tejun Heoa4f57492006-09-12 20:35:49 -07001974 if (!ata_id_is_ata(id) && !ata_id_is_cfa(id))
1975 goto err_out;
Andy Whitcroftdb63a4c2012-05-04 22:15:10 +01001976 if (ap->host->flags & ATA_HOST_IGNORE_ATA &&
1977 ata_id_is_ata(id)) {
1978 ata_dev_dbg(dev,
1979 "host indicates ignore ATA devices, ignored\n");
1980 return -ENOENT;
1981 }
Tejun Heoa4f57492006-09-12 20:35:49 -07001982 } else {
1983 if (ata_id_is_ata(id))
1984 goto err_out;
Tejun Heo49016ac2006-02-21 02:12:11 +09001985 }
1986
Mark Lord169439c2007-04-17 18:26:07 -04001987 if (!tried_spinup && (id[2] == 0x37c8 || id[2] == 0x738c)) {
1988 tried_spinup = 1;
1989 /*
1990 * Drive powered-up in standby mode, and requires a specific
1991 * SET_FEATURES spin-up subcommand before it will accept
1992 * anything other than the original IDENTIFY command.
1993 */
Jeff Garzik218f3d32007-10-25 00:33:27 -04001994 err_mask = ata_dev_set_feature(dev, SETFEATURES_SPINUP, 0);
Ryan Powerfb0582f92007-08-10 13:59:35 -07001995 if (err_mask && id[2] != 0x738c) {
Mark Lord169439c2007-04-17 18:26:07 -04001996 rc = -EIO;
1997 reason = "SPINUP failed";
1998 goto err_out;
1999 }
2000 /*
2001 * If the drive initially returned incomplete IDENTIFY info,
2002 * we now must reissue the IDENTIFY command.
2003 */
2004 if (id[2] == 0x37c8)
2005 goto retry;
2006 }
2007
Hannes Reinecke9162c652014-11-05 13:08:21 +01002008 if ((flags & ATA_READID_POSTRESET) &&
2009 (class == ATA_DEV_ATA || class == ATA_DEV_ZAC)) {
Tejun Heo49016ac2006-02-21 02:12:11 +09002010 /*
2011 * The exact sequence expected by certain pre-ATA4 drives is:
2012 * SRST RESET
Alan Cox50a99012007-08-08 14:27:00 +01002013 * IDENTIFY (optional in early ATA)
2014 * INITIALIZE DEVICE PARAMETERS (later IDE and ATA)
Tejun Heo49016ac2006-02-21 02:12:11 +09002015 * anything else..
2016 * Some drives were very specific about that exact sequence.
Alan Cox50a99012007-08-08 14:27:00 +01002017 *
2018 * Note that ATA4 says lba is mandatory so the second check
Adam Buchbinderc9404c92009-12-18 15:40:42 -05002019 * should never trigger.
Tejun Heo49016ac2006-02-21 02:12:11 +09002020 */
2021 if (ata_id_major_version(id) < 4 || !ata_id_has_lba(id)) {
Tejun Heo3373efd2006-05-15 20:57:53 +09002022 err_mask = ata_dev_init_params(dev, id[3], id[6]);
Tejun Heo49016ac2006-02-21 02:12:11 +09002023 if (err_mask) {
2024 rc = -EIO;
2025 reason = "INIT_DEV_PARAMS failed";
2026 goto err_out;
2027 }
2028
2029 /* current CHS translation info (id[53-58]) might be
2030 * changed. reread the identify device info.
2031 */
Tejun Heobff04642006-11-10 18:08:10 +09002032 flags &= ~ATA_READID_POSTRESET;
Tejun Heo49016ac2006-02-21 02:12:11 +09002033 goto retry;
2034 }
2035 }
2036
2037 *p_class = class;
Tejun Heofe635c72006-05-15 20:57:35 +09002038
Tejun Heo49016ac2006-02-21 02:12:11 +09002039 return 0;
2040
2041 err_out:
Tejun Heo88574552006-06-25 20:00:35 +09002042 if (ata_msg_warn(ap))
Joe Perchesa9a79df2011-04-15 15:51:59 -07002043 ata_dev_warn(dev, "failed to IDENTIFY (%s, err_mask=0x%x)\n",
2044 reason, err_mask);
Tejun Heo49016ac2006-02-21 02:12:11 +09002045 return rc;
2046}
2047
Christoph Hellwigf01f62c2017-06-04 14:42:20 +02002048/**
2049 * ata_read_log_page - read a specific log page
2050 * @dev: target device
2051 * @log: log to read
2052 * @page: page to read
2053 * @buf: buffer to store read page
2054 * @sectors: number of sectors to read
2055 *
2056 * Read log page using READ_LOG_EXT command.
2057 *
2058 * LOCKING:
2059 * Kernel thread context (may sleep).
2060 *
2061 * RETURNS:
2062 * 0 on success, AC_ERR_* mask otherwise.
2063 */
2064unsigned int ata_read_log_page(struct ata_device *dev, u8 log,
2065 u8 page, void *buf, unsigned int sectors)
2066{
2067 unsigned long ap_flags = dev->link->ap->flags;
2068 struct ata_taskfile tf;
2069 unsigned int err_mask;
2070 bool dma = false;
2071
2072 DPRINTK("read log page - log 0x%x, page 0x%x\n", log, page);
2073
2074 /*
2075 * Return error without actually issuing the command on controllers
2076 * which e.g. lockup on a read log page.
2077 */
2078 if (ap_flags & ATA_FLAG_NO_LOG_PAGE)
2079 return AC_ERR_DEV;
2080
2081retry:
2082 ata_tf_init(dev, &tf);
2083 if (dev->dma_mode && ata_id_has_read_log_dma_ext(dev->id) &&
Damien Le Moal7cfdfdc2017-07-10 14:45:20 +09002084 !(dev->horkage & ATA_HORKAGE_NO_DMA_LOG)) {
Christoph Hellwigf01f62c2017-06-04 14:42:20 +02002085 tf.command = ATA_CMD_READ_LOG_DMA_EXT;
2086 tf.protocol = ATA_PROT_DMA;
2087 dma = true;
2088 } else {
2089 tf.command = ATA_CMD_READ_LOG_EXT;
2090 tf.protocol = ATA_PROT_PIO;
2091 dma = false;
2092 }
2093 tf.lbal = log;
2094 tf.lbam = page;
2095 tf.nsect = sectors;
2096 tf.hob_nsect = sectors >> 8;
2097 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_LBA48 | ATA_TFLAG_DEVICE;
2098
2099 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_FROM_DEVICE,
2100 buf, sectors * ATA_SECT_SIZE, 0);
2101
2102 if (err_mask && dma) {
Damien Le Moal7cfdfdc2017-07-10 14:45:20 +09002103 dev->horkage |= ATA_HORKAGE_NO_DMA_LOG;
2104 ata_dev_warn(dev, "READ LOG DMA EXT failed, trying PIO\n");
Christoph Hellwigf01f62c2017-06-04 14:42:20 +02002105 goto retry;
2106 }
2107
2108 DPRINTK("EXIT, err_mask=%x\n", err_mask);
2109 return err_mask;
2110}
2111
Christoph Hellwigefe205a2017-06-04 14:42:21 +02002112static bool ata_log_supported(struct ata_device *dev, u8 log)
2113{
2114 struct ata_port *ap = dev->link->ap;
2115
2116 if (ata_read_log_page(dev, ATA_LOG_DIRECTORY, 0, ap->sector_buf, 1))
2117 return false;
2118 return get_unaligned_le16(&ap->sector_buf[log * 2]) ? true : false;
2119}
2120
Christoph Hellwiga0fd2452017-06-04 14:42:23 +02002121static bool ata_identify_page_supported(struct ata_device *dev, u8 page)
2122{
2123 struct ata_port *ap = dev->link->ap;
2124 unsigned int err, i;
2125
2126 if (!ata_log_supported(dev, ATA_LOG_IDENTIFY_DEVICE)) {
2127 ata_dev_warn(dev, "ATA Identify Device Log not supported\n");
2128 return false;
2129 }
2130
2131 /*
2132 * Read IDENTIFY DEVICE data log, page 0, to figure out if the page is
2133 * supported.
2134 */
2135 err = ata_read_log_page(dev, ATA_LOG_IDENTIFY_DEVICE, 0, ap->sector_buf,
2136 1);
2137 if (err) {
2138 ata_dev_info(dev,
2139 "failed to get Device Identify Log Emask 0x%x\n",
2140 err);
2141 return false;
2142 }
2143
2144 for (i = 0; i < ap->sector_buf[8]; i++) {
2145 if (ap->sector_buf[9 + i] == page)
2146 return true;
2147 }
2148
2149 return false;
2150}
2151
Tejun Heo90627122009-01-29 20:31:36 +09002152static int ata_do_link_spd_horkage(struct ata_device *dev)
2153{
2154 struct ata_link *plink = ata_dev_phys_link(dev);
2155 u32 target, target_limit;
2156
2157 if (!sata_scr_valid(plink))
2158 return 0;
2159
2160 if (dev->horkage & ATA_HORKAGE_1_5_GBPS)
2161 target = 1;
2162 else
2163 return 0;
2164
2165 target_limit = (1 << target) - 1;
2166
2167 /* if already on stricter limit, no need to push further */
2168 if (plink->sata_spd_limit <= target_limit)
2169 return 0;
2170
2171 plink->sata_spd_limit = target_limit;
2172
2173 /* Request another EH round by returning -EAGAIN if link is
2174 * going faster than the target speed. Forward progress is
2175 * guaranteed by setting sata_spd_limit to target_limit above.
2176 */
2177 if (plink->sata_spd > target) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07002178 ata_dev_info(dev, "applying link speed limit horkage to %s\n",
2179 sata_spd_string(target));
Tejun Heo90627122009-01-29 20:31:36 +09002180 return -EAGAIN;
2181 }
2182 return 0;
2183}
2184
Tejun Heo3373efd2006-05-15 20:57:53 +09002185static inline u8 ata_dev_knobble(struct ata_device *dev)
Tejun Heo4b2f3ed2006-03-01 16:09:36 +09002186{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09002187 struct ata_port *ap = dev->link->ap;
Jens Axboe9ce8e302008-08-27 15:23:18 +02002188
2189 if (ata_dev_blacklisted(dev) & ATA_HORKAGE_BRIDGE_OK)
2190 return 0;
2191
Tejun Heo9af5c9c2007-08-06 18:36:22 +09002192 return ((ap->cbl == ATA_CBL_SATA) && (!ata_id_is_sata(dev->id)));
Tejun Heo4b2f3ed2006-03-01 16:09:36 +09002193}
2194
Hannes Reinecke5a233552016-04-25 12:45:46 +02002195static void ata_dev_config_ncq_send_recv(struct ata_device *dev)
2196{
2197 struct ata_port *ap = dev->link->ap;
2198 unsigned int err_mask;
2199
Christoph Hellwigefe205a2017-06-04 14:42:21 +02002200 if (!ata_log_supported(dev, ATA_LOG_NCQ_SEND_RECV)) {
2201 ata_dev_warn(dev, "NCQ Send/Recv Log not supported\n");
Hannes Reineckefe5af0c2016-04-25 12:45:48 +02002202 return;
2203 }
Hannes Reinecke5a233552016-04-25 12:45:46 +02002204 err_mask = ata_read_log_page(dev, ATA_LOG_NCQ_SEND_RECV,
2205 0, ap->sector_buf, 1);
2206 if (err_mask) {
2207 ata_dev_dbg(dev,
2208 "failed to get NCQ Send/Recv Log Emask 0x%x\n",
2209 err_mask);
2210 } else {
2211 u8 *cmds = dev->ncq_send_recv_cmds;
2212
2213 dev->flags |= ATA_DFLAG_NCQ_SEND_RECV;
2214 memcpy(cmds, ap->sector_buf, ATA_LOG_NCQ_SEND_RECV_SIZE);
2215
2216 if (dev->horkage & ATA_HORKAGE_NO_NCQ_TRIM) {
2217 ata_dev_dbg(dev, "disabling queued TRIM support\n");
2218 cmds[ATA_LOG_NCQ_SEND_RECV_DSM_OFFSET] &=
2219 ~ATA_LOG_NCQ_SEND_RECV_DSM_TRIM;
2220 }
2221 }
2222}
2223
Hannes Reinecke284b3b72016-04-25 12:45:54 +02002224static void ata_dev_config_ncq_non_data(struct ata_device *dev)
2225{
2226 struct ata_port *ap = dev->link->ap;
2227 unsigned int err_mask;
Hannes Reinecke284b3b72016-04-25 12:45:54 +02002228
Christoph Hellwigefe205a2017-06-04 14:42:21 +02002229 if (!ata_log_supported(dev, ATA_LOG_NCQ_NON_DATA)) {
Hannes Reinecke284b3b72016-04-25 12:45:54 +02002230 ata_dev_warn(dev,
2231 "NCQ Send/Recv Log not supported\n");
2232 return;
2233 }
2234 err_mask = ata_read_log_page(dev, ATA_LOG_NCQ_NON_DATA,
2235 0, ap->sector_buf, 1);
2236 if (err_mask) {
2237 ata_dev_dbg(dev,
2238 "failed to get NCQ Non-Data Log Emask 0x%x\n",
2239 err_mask);
2240 } else {
2241 u8 *cmds = dev->ncq_non_data_cmds;
2242
2243 memcpy(cmds, ap->sector_buf, ATA_LOG_NCQ_NON_DATA_SIZE);
2244 }
2245}
2246
Adam Manzanares8e061782016-10-17 11:27:29 -07002247static void ata_dev_config_ncq_prio(struct ata_device *dev)
2248{
2249 struct ata_port *ap = dev->link->ap;
2250 unsigned int err_mask;
2251
Adam Manzanares9f56eca2016-12-13 12:00:05 -08002252 if (!(dev->flags & ATA_DFLAG_NCQ_PRIO_ENABLE)) {
2253 dev->flags &= ~ATA_DFLAG_NCQ_PRIO;
2254 return;
2255 }
2256
Adam Manzanares8e061782016-10-17 11:27:29 -07002257 err_mask = ata_read_log_page(dev,
Christoph Hellwig1d51d5f2017-06-04 14:42:22 +02002258 ATA_LOG_IDENTIFY_DEVICE,
Adam Manzanares8e061782016-10-17 11:27:29 -07002259 ATA_LOG_SATA_SETTINGS,
2260 ap->sector_buf,
2261 1);
2262 if (err_mask) {
2263 ata_dev_dbg(dev,
2264 "failed to get Identify Device data, Emask 0x%x\n",
2265 err_mask);
2266 return;
2267 }
2268
Adam Manzanares9f56eca2016-12-13 12:00:05 -08002269 if (ap->sector_buf[ATA_LOG_NCQ_PRIO_OFFSET] & BIT(3)) {
Adam Manzanares8e061782016-10-17 11:27:29 -07002270 dev->flags |= ATA_DFLAG_NCQ_PRIO;
Adam Manzanares9f56eca2016-12-13 12:00:05 -08002271 } else {
2272 dev->flags &= ~ATA_DFLAG_NCQ_PRIO;
Adam Manzanares8e061782016-10-17 11:27:29 -07002273 ata_dev_dbg(dev, "SATA page does not support priority\n");
Adam Manzanares9f56eca2016-12-13 12:00:05 -08002274 }
Adam Manzanares8e061782016-10-17 11:27:29 -07002275
2276}
2277
Shaohua Li388539f2009-07-27 09:24:35 +08002278static int ata_dev_config_ncq(struct ata_device *dev,
Tejun Heoa6e6ce82006-05-15 21:03:48 +09002279 char *desc, size_t desc_sz)
2280{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09002281 struct ata_port *ap = dev->link->ap;
Tejun Heoa6e6ce82006-05-15 21:03:48 +09002282 int hdepth = 0, ddepth = ata_id_queue_depth(dev->id);
Shaohua Li388539f2009-07-27 09:24:35 +08002283 unsigned int err_mask;
2284 char *aa_desc = "";
Tejun Heoa6e6ce82006-05-15 21:03:48 +09002285
2286 if (!ata_id_has_ncq(dev->id)) {
2287 desc[0] = '\0';
Shaohua Li388539f2009-07-27 09:24:35 +08002288 return 0;
Tejun Heoa6e6ce82006-05-15 21:03:48 +09002289 }
Tejun Heo75683fe2007-07-05 13:31:27 +09002290 if (dev->horkage & ATA_HORKAGE_NONCQ) {
Alan Cox6919a0a2006-10-27 19:08:46 -07002291 snprintf(desc, desc_sz, "NCQ (not used)");
Shaohua Li388539f2009-07-27 09:24:35 +08002292 return 0;
Alan Cox6919a0a2006-10-27 19:08:46 -07002293 }
Tejun Heoa6e6ce82006-05-15 21:03:48 +09002294 if (ap->flags & ATA_FLAG_NCQ) {
Jens Axboe69278f72018-05-11 12:51:10 -06002295 hdepth = min(ap->scsi_host->can_queue, ATA_MAX_QUEUE);
Tejun Heoa6e6ce82006-05-15 21:03:48 +09002296 dev->flags |= ATA_DFLAG_NCQ;
2297 }
2298
Shaohua Li388539f2009-07-27 09:24:35 +08002299 if (!(dev->horkage & ATA_HORKAGE_BROKEN_FPDMA_AA) &&
2300 (ap->flags & ATA_FLAG_FPDMA_AA) &&
2301 ata_id_has_fpdma_aa(dev->id)) {
2302 err_mask = ata_dev_set_feature(dev, SETFEATURES_SATA_ENABLE,
2303 SATA_FPDMA_AA);
2304 if (err_mask) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07002305 ata_dev_err(dev,
2306 "failed to enable AA (error_mask=0x%x)\n",
2307 err_mask);
Shaohua Li388539f2009-07-27 09:24:35 +08002308 if (err_mask != AC_ERR_DEV) {
2309 dev->horkage |= ATA_HORKAGE_BROKEN_FPDMA_AA;
2310 return -EIO;
2311 }
2312 } else
2313 aa_desc = ", AA";
2314 }
2315
Tejun Heoa6e6ce82006-05-15 21:03:48 +09002316 if (hdepth >= ddepth)
Shaohua Li388539f2009-07-27 09:24:35 +08002317 snprintf(desc, desc_sz, "NCQ (depth %d)%s", ddepth, aa_desc);
Tejun Heoa6e6ce82006-05-15 21:03:48 +09002318 else
Shaohua Li388539f2009-07-27 09:24:35 +08002319 snprintf(desc, desc_sz, "NCQ (depth %d/%d)%s", hdepth,
2320 ddepth, aa_desc);
Marc Carinoed369112013-08-24 23:22:50 -07002321
Hannes Reinecke284b3b72016-04-25 12:45:54 +02002322 if ((ap->flags & ATA_FLAG_FPDMA_AUX)) {
2323 if (ata_id_has_ncq_send_and_recv(dev->id))
2324 ata_dev_config_ncq_send_recv(dev);
2325 if (ata_id_has_ncq_non_data(dev->id))
2326 ata_dev_config_ncq_non_data(dev);
Adam Manzanares8e061782016-10-17 11:27:29 -07002327 if (ata_id_has_ncq_prio(dev->id))
2328 ata_dev_config_ncq_prio(dev);
Marc Carinoed369112013-08-24 23:22:50 -07002329 }
2330
Shaohua Li388539f2009-07-27 09:24:35 +08002331 return 0;
Tejun Heoa6e6ce82006-05-15 21:03:48 +09002332}
2333
Hannes Reineckee87fd282016-04-04 11:43:55 +02002334static void ata_dev_config_sense_reporting(struct ata_device *dev)
2335{
2336 unsigned int err_mask;
2337
2338 if (!ata_id_has_sense_reporting(dev->id))
2339 return;
2340
2341 if (ata_id_sense_reporting_enabled(dev->id))
2342 return;
2343
2344 err_mask = ata_dev_set_feature(dev, SETFEATURE_SENSE_DATA, 0x1);
2345 if (err_mask) {
2346 ata_dev_dbg(dev,
2347 "failed to enable Sense Data Reporting, Emask 0x%x\n",
2348 err_mask);
2349 }
2350}
2351
Hannes Reinecke6d1003a2016-04-25 12:45:56 +02002352static void ata_dev_config_zac(struct ata_device *dev)
2353{
2354 struct ata_port *ap = dev->link->ap;
2355 unsigned int err_mask;
2356 u8 *identify_buf = ap->sector_buf;
Hannes Reinecke6d1003a2016-04-25 12:45:56 +02002357
2358 dev->zac_zones_optimal_open = U32_MAX;
2359 dev->zac_zones_optimal_nonseq = U32_MAX;
2360 dev->zac_zones_max_open = U32_MAX;
2361
2362 /*
2363 * Always set the 'ZAC' flag for Host-managed devices.
2364 */
2365 if (dev->class == ATA_DEV_ZAC)
2366 dev->flags |= ATA_DFLAG_ZAC;
2367 else if (ata_id_zoned_cap(dev->id) == 0x01)
2368 /*
2369 * Check for host-aware devices.
2370 */
2371 dev->flags |= ATA_DFLAG_ZAC;
2372
2373 if (!(dev->flags & ATA_DFLAG_ZAC))
2374 return;
2375
Christoph Hellwiga0fd2452017-06-04 14:42:23 +02002376 if (!ata_identify_page_supported(dev, ATA_LOG_ZONED_INFORMATION)) {
Hannes Reinecke6d1003a2016-04-25 12:45:56 +02002377 ata_dev_warn(dev,
2378 "ATA Zoned Information Log not supported\n");
2379 return;
2380 }
2381
2382 /*
2383 * Read IDENTIFY DEVICE data log, page 9 (Zoned-device information)
2384 */
Christoph Hellwig1d51d5f2017-06-04 14:42:22 +02002385 err_mask = ata_read_log_page(dev, ATA_LOG_IDENTIFY_DEVICE,
Hannes Reinecke6d1003a2016-04-25 12:45:56 +02002386 ATA_LOG_ZONED_INFORMATION,
2387 identify_buf, 1);
2388 if (!err_mask) {
2389 u64 zoned_cap, opt_open, opt_nonseq, max_open;
2390
2391 zoned_cap = get_unaligned_le64(&identify_buf[8]);
2392 if ((zoned_cap >> 63))
2393 dev->zac_zoned_cap = (zoned_cap & 1);
2394 opt_open = get_unaligned_le64(&identify_buf[24]);
2395 if ((opt_open >> 63))
2396 dev->zac_zones_optimal_open = (u32)opt_open;
2397 opt_nonseq = get_unaligned_le64(&identify_buf[32]);
2398 if ((opt_nonseq >> 63))
2399 dev->zac_zones_optimal_nonseq = (u32)opt_nonseq;
2400 max_open = get_unaligned_le64(&identify_buf[40]);
2401 if ((max_open >> 63))
2402 dev->zac_zones_max_open = (u32)max_open;
2403 }
2404}
2405
Christoph Hellwig818831c2017-06-04 14:42:24 +02002406static void ata_dev_config_trusted(struct ata_device *dev)
2407{
2408 struct ata_port *ap = dev->link->ap;
2409 u64 trusted_cap;
2410 unsigned int err;
2411
Christoph Hellwige8f11db2017-08-29 14:42:06 +02002412 if (!ata_id_has_trusted(dev->id))
2413 return;
2414
Christoph Hellwig818831c2017-06-04 14:42:24 +02002415 if (!ata_identify_page_supported(dev, ATA_LOG_SECURITY)) {
2416 ata_dev_warn(dev,
2417 "Security Log not supported\n");
2418 return;
2419 }
2420
2421 err = ata_read_log_page(dev, ATA_LOG_IDENTIFY_DEVICE, ATA_LOG_SECURITY,
2422 ap->sector_buf, 1);
2423 if (err) {
2424 ata_dev_dbg(dev,
2425 "failed to read Security Log, Emask 0x%x\n", err);
2426 return;
2427 }
2428
2429 trusted_cap = get_unaligned_le64(&ap->sector_buf[40]);
2430 if (!(trusted_cap & (1ULL << 63))) {
2431 ata_dev_dbg(dev,
2432 "Trusted Computing capability qword not valid!\n");
2433 return;
2434 }
2435
2436 if (trusted_cap & (1 << 0))
2437 dev->flags |= ATA_DFLAG_TRUSTED;
2438}
2439
Tejun Heo49016ac2006-02-21 02:12:11 +09002440/**
Tejun Heoffeae412006-03-01 16:09:35 +09002441 * ata_dev_configure - Configure the specified ATA/ATAPI device
Tejun Heoffeae412006-03-01 16:09:35 +09002442 * @dev: Target device to configure
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 *
Tejun Heoffeae412006-03-01 16:09:35 +09002444 * Configure @dev according to @dev->id. Generic and low-level
2445 * driver specific fixups are also applied.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 *
2447 * LOCKING:
Tejun Heoffeae412006-03-01 16:09:35 +09002448 * Kernel thread context (may sleep)
2449 *
2450 * RETURNS:
2451 * 0 on success, -errno otherwise
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452 */
Tejun Heoefdaedc2006-11-01 18:38:52 +09002453int ata_dev_configure(struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09002455 struct ata_port *ap = dev->link->ap;
2456 struct ata_eh_context *ehc = &dev->link->eh_context;
Tejun Heo67465442007-05-15 03:28:16 +09002457 int print_info = ehc->i.flags & ATA_EHI_PRINTINFO;
Tejun Heo1148c3a2006-03-13 19:48:04 +09002458 const u16 *id = dev->id;
Tejun Heo7dc951a2007-11-27 19:43:42 +09002459 unsigned long xfer_mask;
Shane Huang65fe1f02012-09-07 22:40:01 +08002460 unsigned int err_mask;
Alan Coxb352e572006-08-10 18:52:12 +01002461 char revbuf[7]; /* XYZ-99\0 */
Eric D. Mudama3f64f562007-01-30 23:00:40 -07002462 char fwrevbuf[ATA_ID_FW_REV_LEN+1];
2463 char modelbuf[ATA_ID_PROD_LEN+1];
Brian Kinge6d902a2006-06-28 08:30:31 -05002464 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465
Borislav Petkov0dd4b212006-06-23 02:29:08 -04002466 if (!ata_dev_enabled(dev) && ata_msg_info(ap)) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07002467 ata_dev_info(dev, "%s: ENTER/EXIT -- nodev\n", __func__);
Tejun Heoffeae412006-03-01 16:09:35 +09002468 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469 }
2470
Borislav Petkov0dd4b212006-06-23 02:29:08 -04002471 if (ata_msg_probe(ap))
Joe Perchesa9a79df2011-04-15 15:51:59 -07002472 ata_dev_dbg(dev, "%s: ENTER\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473
Tejun Heo75683fe2007-07-05 13:31:27 +09002474 /* set horkage */
2475 dev->horkage |= ata_dev_blacklisted(dev);
Tejun Heo33267322008-02-13 09:15:09 +09002476 ata_force_horkage(dev);
Tejun Heo75683fe2007-07-05 13:31:27 +09002477
Tejun Heo50af2fa2008-05-19 01:15:14 +09002478 if (dev->horkage & ATA_HORKAGE_DISABLE) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07002479 ata_dev_info(dev, "unsupported device, disabling\n");
Tejun Heo50af2fa2008-05-19 01:15:14 +09002480 ata_dev_disable(dev);
2481 return 0;
2482 }
2483
Tejun Heo2486fa52008-07-31 07:52:40 +09002484 if ((!atapi_enabled || (ap->flags & ATA_FLAG_NO_ATAPI)) &&
2485 dev->class == ATA_DEV_ATAPI) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07002486 ata_dev_warn(dev, "WARNING: ATAPI is %s, device ignored\n",
2487 atapi_enabled ? "not supported with this driver"
2488 : "disabled");
Tejun Heo2486fa52008-07-31 07:52:40 +09002489 ata_dev_disable(dev);
2490 return 0;
2491 }
2492
Tejun Heo90627122009-01-29 20:31:36 +09002493 rc = ata_do_link_spd_horkage(dev);
2494 if (rc)
2495 return rc;
2496
Tejun Heoecd75ad2014-01-16 09:47:17 -05002497 /* some WD SATA-1 drives have issues with LPM, turn on NOLPM for them */
2498 if ((dev->horkage & ATA_HORKAGE_WD_BROKEN_LPM) &&
2499 (id[ATA_ID_SATA_CAPABILITY] & 0xe) == 0x2)
2500 dev->horkage |= ATA_HORKAGE_NOLPM;
2501
Hans de Goede240630e2018-07-01 12:15:46 +02002502 if (ap->flags & ATA_FLAG_NO_LPM)
2503 dev->horkage |= ATA_HORKAGE_NOLPM;
2504
Tejun Heoecd75ad2014-01-16 09:47:17 -05002505 if (dev->horkage & ATA_HORKAGE_NOLPM) {
2506 ata_dev_warn(dev, "LPM support broken, forcing max_power\n");
2507 dev->link->ap->target_lpm_policy = ATA_LPM_MAX_POWER;
2508 }
2509
Tejun Heo67465442007-05-15 03:28:16 +09002510 /* let ACPI work its magic */
2511 rc = ata_acpi_on_devcfg(dev);
2512 if (rc)
2513 return rc;
Kristen Carlson Accardi08573a82006-11-10 16:14:47 -08002514
Tejun Heo05027ad2007-09-03 12:32:57 +09002515 /* massage HPA, do it early as it might change IDENTIFY data */
2516 rc = ata_hpa_resize(dev);
2517 if (rc)
2518 return rc;
2519
Tejun Heoc39f5eb2006-03-13 19:51:19 +09002520 /* print device capabilities */
Borislav Petkov0dd4b212006-06-23 02:29:08 -04002521 if (ata_msg_probe(ap))
Joe Perchesa9a79df2011-04-15 15:51:59 -07002522 ata_dev_dbg(dev,
2523 "%s: cfg 49:%04x 82:%04x 83:%04x 84:%04x "
2524 "85:%04x 86:%04x 87:%04x 88:%04x\n",
2525 __func__,
2526 id[49], id[82], id[83], id[84],
2527 id[85], id[86], id[87], id[88]);
Tejun Heoc39f5eb2006-03-13 19:51:19 +09002528
Tejun Heo208a9932006-03-05 17:55:58 +09002529 /* initialize to-be-configured parameters */
Tejun Heoea1dd4e2006-04-02 18:51:53 +09002530 dev->flags &= ~ATA_DFLAG_CFG_MASK;
Tejun Heo208a9932006-03-05 17:55:58 +09002531 dev->max_sectors = 0;
2532 dev->cdb_len = 0;
2533 dev->n_sectors = 0;
2534 dev->cylinders = 0;
2535 dev->heads = 0;
2536 dev->sectors = 0;
Mark Lorde18086d2009-03-19 13:32:21 -04002537 dev->multi_count = 0;
Tejun Heo208a9932006-03-05 17:55:58 +09002538
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539 /*
2540 * common ATA, ATAPI feature tests
2541 */
2542
Tejun Heoff8854b2006-03-06 04:31:56 +09002543 /* find max transfer mode; for printk only */
Tejun Heo1148c3a2006-03-13 19:48:04 +09002544 xfer_mask = ata_id_xfermask(id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545
Borislav Petkov0dd4b212006-06-23 02:29:08 -04002546 if (ata_msg_probe(ap))
2547 ata_dump_id(id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548
Albert Leeef143d52007-06-05 13:01:33 +08002549 /* SCSI only uses 4-char revisions, dump full 8 chars from ATA */
2550 ata_id_c_string(dev->id, fwrevbuf, ATA_ID_FW_REV,
2551 sizeof(fwrevbuf));
2552
2553 ata_id_c_string(dev->id, modelbuf, ATA_ID_PROD,
2554 sizeof(modelbuf));
2555
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556 /* ATA-specific feature tests */
Hannes Reinecke9162c652014-11-05 13:08:21 +01002557 if (dev->class == ATA_DEV_ATA || dev->class == ATA_DEV_ZAC) {
Alan Coxb352e572006-08-10 18:52:12 +01002558 if (ata_id_is_cfa(id)) {
Sergei Shtylyov62afe5d2009-04-13 20:50:00 +04002559 /* CPRM may make this media unusable */
2560 if (id[ATA_ID_CFA_KEY_MGMT] & 1)
Joe Perchesa9a79df2011-04-15 15:51:59 -07002561 ata_dev_warn(dev,
2562 "supports DRM functions and may not be fully accessible\n");
Alan Coxb352e572006-08-10 18:52:12 +01002563 snprintf(revbuf, 7, "CFA");
Alan Coxae8d4ee2007-11-04 22:05:49 -05002564 } else {
Jeff Garzik2dcb4072007-10-19 06:42:56 -04002565 snprintf(revbuf, 7, "ATA-%d", ata_id_major_version(id));
Alan Coxae8d4ee2007-11-04 22:05:49 -05002566 /* Warn the user if the device has TPM extensions */
2567 if (ata_id_has_tpm(id))
Joe Perchesa9a79df2011-04-15 15:51:59 -07002568 ata_dev_warn(dev,
2569 "supports DRM functions and may not be fully accessible\n");
Alan Coxae8d4ee2007-11-04 22:05:49 -05002570 }
Alan Coxb352e572006-08-10 18:52:12 +01002571
Tejun Heo1148c3a2006-03-13 19:48:04 +09002572 dev->n_sectors = ata_id_n_sectors(id);
Tejun Heo29407402006-02-12 22:47:04 +09002573
Mark Lorde18086d2009-03-19 13:32:21 -04002574 /* get current R/W Multiple count setting */
2575 if ((dev->id[47] >> 8) == 0x80 && (dev->id[59] & 0x100)) {
2576 unsigned int max = dev->id[47] & 0xff;
2577 unsigned int cnt = dev->id[59] & 0xff;
2578 /* only recognize/allow powers of two here */
2579 if (is_power_of_2(max) && is_power_of_2(cnt))
2580 if (cnt <= max)
2581 dev->multi_count = cnt;
2582 }
Eric D. Mudama3f64f562007-01-30 23:00:40 -07002583
Tejun Heo1148c3a2006-03-13 19:48:04 +09002584 if (ata_id_has_lba(id)) {
Tejun Heo4c2d7212006-03-05 17:55:58 +09002585 const char *lba_desc;
Shaohua Li388539f2009-07-27 09:24:35 +08002586 char ncq_desc[24];
Albert Lee8bf62ece2005-05-12 15:29:42 -04002587
Tejun Heo4c2d7212006-03-05 17:55:58 +09002588 lba_desc = "LBA";
2589 dev->flags |= ATA_DFLAG_LBA;
Tejun Heo1148c3a2006-03-13 19:48:04 +09002590 if (ata_id_has_lba48(id)) {
Albert Lee8bf62ece2005-05-12 15:29:42 -04002591 dev->flags |= ATA_DFLAG_LBA48;
Tejun Heo4c2d7212006-03-05 17:55:58 +09002592 lba_desc = "LBA48";
Tejun Heo6fc49ad2006-11-11 20:10:45 +09002593
2594 if (dev->n_sectors >= (1UL << 28) &&
2595 ata_id_has_flush_ext(id))
2596 dev->flags |= ATA_DFLAG_FLUSH_EXT;
Tejun Heo4c2d7212006-03-05 17:55:58 +09002597 }
Albert Lee8bf62ece2005-05-12 15:29:42 -04002598
Tejun Heoa6e6ce82006-05-15 21:03:48 +09002599 /* config NCQ */
Shaohua Li388539f2009-07-27 09:24:35 +08002600 rc = ata_dev_config_ncq(dev, ncq_desc, sizeof(ncq_desc));
2601 if (rc)
2602 return rc;
Tejun Heoa6e6ce82006-05-15 21:03:48 +09002603
Albert Lee8bf62ece2005-05-12 15:29:42 -04002604 /* print device info to dmesg */
Eric D. Mudama3f64f562007-01-30 23:00:40 -07002605 if (ata_msg_drv(ap) && print_info) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07002606 ata_dev_info(dev, "%s: %s, %s, max %s\n",
2607 revbuf, modelbuf, fwrevbuf,
2608 ata_mode_string(xfer_mask));
2609 ata_dev_info(dev,
2610 "%llu sectors, multi %u: %s %s\n",
Tejun Heof15a1da2006-05-15 20:57:56 +09002611 (unsigned long long)dev->n_sectors,
Eric D. Mudama3f64f562007-01-30 23:00:40 -07002612 dev->multi_count, lba_desc, ncq_desc);
2613 }
Tejun Heoffeae412006-03-01 16:09:35 +09002614 } else {
Albert Lee8bf62ece2005-05-12 15:29:42 -04002615 /* CHS */
2616
2617 /* Default translation */
Tejun Heo1148c3a2006-03-13 19:48:04 +09002618 dev->cylinders = id[1];
2619 dev->heads = id[3];
2620 dev->sectors = id[6];
Albert Lee8bf62ece2005-05-12 15:29:42 -04002621
Tejun Heo1148c3a2006-03-13 19:48:04 +09002622 if (ata_id_current_chs_valid(id)) {
Albert Lee8bf62ece2005-05-12 15:29:42 -04002623 /* Current CHS translation is valid. */
Tejun Heo1148c3a2006-03-13 19:48:04 +09002624 dev->cylinders = id[54];
2625 dev->heads = id[55];
2626 dev->sectors = id[56];
Albert Lee8bf62ece2005-05-12 15:29:42 -04002627 }
2628
2629 /* print device info to dmesg */
Eric D. Mudama3f64f562007-01-30 23:00:40 -07002630 if (ata_msg_drv(ap) && print_info) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07002631 ata_dev_info(dev, "%s: %s, %s, max %s\n",
2632 revbuf, modelbuf, fwrevbuf,
2633 ata_mode_string(xfer_mask));
2634 ata_dev_info(dev,
2635 "%llu sectors, multi %u, CHS %u/%u/%u\n",
2636 (unsigned long long)dev->n_sectors,
2637 dev->multi_count, dev->cylinders,
2638 dev->heads, dev->sectors);
Eric D. Mudama3f64f562007-01-30 23:00:40 -07002639 }
Albert Lee07f6f7d2005-11-01 19:33:20 +08002640 }
2641
Shane Huang803739d2012-12-17 23:18:59 +08002642 /* Check and mark DevSlp capability. Get DevSlp timing variables
2643 * from SATA Settings page of Identify Device Data Log.
Shane Huang65fe1f02012-09-07 22:40:01 +08002644 */
Shane Huang803739d2012-12-17 23:18:59 +08002645 if (ata_id_has_devslp(dev->id)) {
David Woodhouse8e725c72013-03-29 11:54:55 +00002646 u8 *sata_setting = ap->sector_buf;
Shane Huang803739d2012-12-17 23:18:59 +08002647 int i, j;
2648
2649 dev->flags |= ATA_DFLAG_DEVSLP;
Shane Huang65fe1f02012-09-07 22:40:01 +08002650 err_mask = ata_read_log_page(dev,
Christoph Hellwig1d51d5f2017-06-04 14:42:22 +02002651 ATA_LOG_IDENTIFY_DEVICE,
Shane Huang65fe1f02012-09-07 22:40:01 +08002652 ATA_LOG_SATA_SETTINGS,
Shane Huang803739d2012-12-17 23:18:59 +08002653 sata_setting,
Shane Huang65fe1f02012-09-07 22:40:01 +08002654 1);
2655 if (err_mask)
2656 ata_dev_dbg(dev,
2657 "failed to get Identify Device Data, Emask 0x%x\n",
2658 err_mask);
Shane Huang803739d2012-12-17 23:18:59 +08002659 else
2660 for (i = 0; i < ATA_LOG_DEVSLP_SIZE; i++) {
2661 j = ATA_LOG_DEVSLP_OFFSET + i;
2662 dev->devslp_timing[i] = sata_setting[j];
2663 }
Shane Huang65fe1f02012-09-07 22:40:01 +08002664 }
Hannes Reineckee87fd282016-04-04 11:43:55 +02002665 ata_dev_config_sense_reporting(dev);
Hannes Reinecke6d1003a2016-04-25 12:45:56 +02002666 ata_dev_config_zac(dev);
Christoph Hellwig818831c2017-06-04 14:42:24 +02002667 ata_dev_config_trusted(dev);
Minwoo Imb1ffbf82017-06-24 03:41:10 +09002668 dev->cdb_len = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669 }
2670
2671 /* ATAPI-specific feature tests */
Jeff Garzik2c13b7c2005-11-14 14:14:16 -05002672 else if (dev->class == ATA_DEV_ATAPI) {
Tejun Heo854c73a2007-09-23 13:14:11 +09002673 const char *cdb_intr_string = "";
2674 const char *atapi_an_string = "";
Tejun Heo91163002008-02-21 13:25:50 +09002675 const char *dma_dir_string = "";
Tejun Heo7d77b242007-09-23 13:14:13 +09002676 u32 sntf;
Albert Lee08a556d2006-03-31 13:29:04 +08002677
Tejun Heo1148c3a2006-03-13 19:48:04 +09002678 rc = atapi_cdb_len(id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679 if ((rc < 12) || (rc > ATAPI_CDB_LEN)) {
Borislav Petkov0dd4b212006-06-23 02:29:08 -04002680 if (ata_msg_warn(ap))
Joe Perchesa9a79df2011-04-15 15:51:59 -07002681 ata_dev_warn(dev, "unsupported CDB len\n");
Tejun Heoffeae412006-03-01 16:09:35 +09002682 rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683 goto err_out_nosup;
2684 }
Tejun Heo6e7846e2006-02-12 23:32:58 +09002685 dev->cdb_len = (unsigned int) rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686
Tejun Heo7d77b242007-09-23 13:14:13 +09002687 /* Enable ATAPI AN if both the host and device have
2688 * the support. If PMP is attached, SNTF is required
2689 * to enable ATAPI AN to discern between PHY status
2690 * changed notifications and ATAPI ANs.
Kristen Carlson Accardi9f45cbd2007-08-15 03:57:11 -04002691 */
Tejun Heoe7ecd432010-05-19 15:38:58 +02002692 if (atapi_an &&
2693 (ap->flags & ATA_FLAG_AN) && ata_id_has_atapi_AN(id) &&
Tejun Heo071f44b2008-04-07 22:47:22 +09002694 (!sata_pmp_attached(ap) ||
Tejun Heo7d77b242007-09-23 13:14:13 +09002695 sata_scr_read(&ap->link, SCR_NOTIFICATION, &sntf) == 0)) {
Kristen Carlson Accardi9f45cbd2007-08-15 03:57:11 -04002696 /* issue SET feature command to turn this on */
Jeff Garzik218f3d32007-10-25 00:33:27 -04002697 err_mask = ata_dev_set_feature(dev,
2698 SETFEATURES_SATA_ENABLE, SATA_AN);
Tejun Heo854c73a2007-09-23 13:14:11 +09002699 if (err_mask)
Joe Perchesa9a79df2011-04-15 15:51:59 -07002700 ata_dev_err(dev,
2701 "failed to enable ATAPI AN (err_mask=0x%x)\n",
2702 err_mask);
Tejun Heo854c73a2007-09-23 13:14:11 +09002703 else {
Kristen Carlson Accardi9f45cbd2007-08-15 03:57:11 -04002704 dev->flags |= ATA_DFLAG_AN;
Tejun Heo854c73a2007-09-23 13:14:11 +09002705 atapi_an_string = ", ATAPI AN";
2706 }
Kristen Carlson Accardi9f45cbd2007-08-15 03:57:11 -04002707 }
2708
Albert Lee08a556d2006-03-31 13:29:04 +08002709 if (ata_id_cdb_intr(dev->id)) {
Albert Lee312f7da2005-09-27 17:38:03 +08002710 dev->flags |= ATA_DFLAG_CDB_INTR;
Albert Lee08a556d2006-03-31 13:29:04 +08002711 cdb_intr_string = ", CDB intr";
2712 }
Albert Lee312f7da2005-09-27 17:38:03 +08002713
Vincent Pelletier966fbe12013-05-21 22:30:58 +02002714 if (atapi_dmadir || (dev->horkage & ATA_HORKAGE_ATAPI_DMADIR) || atapi_id_dmadir(dev->id)) {
Tejun Heo91163002008-02-21 13:25:50 +09002715 dev->flags |= ATA_DFLAG_DMADIR;
2716 dma_dir_string = ", DMADIR";
2717 }
2718
Aaron Luafe75952013-01-15 17:20:58 +08002719 if (ata_id_has_da(dev->id)) {
Lin Mingb1354cb2012-06-25 16:13:08 +08002720 dev->flags |= ATA_DFLAG_DA;
Aaron Luafe75952013-01-15 17:20:58 +08002721 zpodd_init(dev);
2722 }
Lin Mingb1354cb2012-06-25 16:13:08 +08002723
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724 /* print device info to dmesg */
Borislav Petkov5afc8142006-06-27 14:51:25 +02002725 if (ata_msg_drv(ap) && print_info)
Joe Perchesa9a79df2011-04-15 15:51:59 -07002726 ata_dev_info(dev,
2727 "ATAPI: %s, %s, max %s%s%s%s\n",
2728 modelbuf, fwrevbuf,
2729 ata_mode_string(xfer_mask),
2730 cdb_intr_string, atapi_an_string,
2731 dma_dir_string);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732 }
2733
Tejun Heo914ed352006-11-01 18:39:55 +09002734 /* determine max_sectors */
2735 dev->max_sectors = ATA_MAX_SECTORS;
2736 if (dev->flags & ATA_DFLAG_LBA48)
2737 dev->max_sectors = ATA_MAX_SECTORS_LBA48;
2738
Alan Coxc5038fc2007-10-25 14:21:16 +01002739 /* Limit PATA drive on SATA cable bridge transfers to udma5,
2740 200 sectors */
Tejun Heo3373efd2006-05-15 20:57:53 +09002741 if (ata_dev_knobble(dev)) {
Borislav Petkov5afc8142006-06-27 14:51:25 +02002742 if (ata_msg_drv(ap) && print_info)
Joe Perchesa9a79df2011-04-15 15:51:59 -07002743 ata_dev_info(dev, "applying bridge limits\n");
Tejun Heo5a529132006-03-24 14:07:50 +09002744 dev->udma_mask &= ATA_UDMA5;
Tejun Heo4b2f3ed2006-03-01 16:09:36 +09002745 dev->max_sectors = ATA_MAX_SECTORS;
2746 }
2747
Tony Battersbyf8d8e572007-10-30 11:44:35 -04002748 if ((dev->class == ATA_DEV_ATAPI) &&
Albert Leef442cd82007-11-15 10:35:47 +09002749 (atapi_command_packet_set(id) == TYPE_TAPE)) {
Tony Battersbyf8d8e572007-10-30 11:44:35 -04002750 dev->max_sectors = ATA_MAX_SECTORS_TAPE;
Albert Leef442cd82007-11-15 10:35:47 +09002751 dev->horkage |= ATA_HORKAGE_STUCK_ERR;
2752 }
Tony Battersbyf8d8e572007-10-30 11:44:35 -04002753
Tejun Heo75683fe2007-07-05 13:31:27 +09002754 if (dev->horkage & ATA_HORKAGE_MAX_SEC_128)
Tejun Heo03ec52d2007-04-12 13:38:11 +09002755 dev->max_sectors = min_t(unsigned int, ATA_MAX_SECTORS_128,
2756 dev->max_sectors);
Albert Lee18d6e9d2007-04-02 11:34:15 +08002757
David Milburnaf34d632015-07-13 11:48:23 -05002758 if (dev->horkage & ATA_HORKAGE_MAX_SEC_1024)
2759 dev->max_sectors = min_t(unsigned int, ATA_MAX_SECTORS_1024,
2760 dev->max_sectors);
2761
Shan Haia32450e2013-03-18 10:30:44 +08002762 if (dev->horkage & ATA_HORKAGE_MAX_SEC_LBA48)
2763 dev->max_sectors = ATA_MAX_SECTORS_LBA48;
2764
Tejun Heo4b2f3ed2006-03-01 16:09:36 +09002765 if (ap->ops->dev_config)
Alancd0d3bb2007-03-02 00:56:15 +00002766 ap->ops->dev_config(dev);
Tejun Heo4b2f3ed2006-03-01 16:09:36 +09002767
Alan Coxc5038fc2007-10-25 14:21:16 +01002768 if (dev->horkage & ATA_HORKAGE_DIAGNOSTIC) {
2769 /* Let the user know. We don't want to disallow opens for
2770 rescue purposes, or in case the vendor is just a blithering
2771 idiot. Do this after the dev_config call as some controllers
2772 with buggy firmware may want to avoid reporting false device
2773 bugs */
2774
2775 if (print_info) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07002776 ata_dev_warn(dev,
Alan Coxc5038fc2007-10-25 14:21:16 +01002777"Drive reports diagnostics failure. This may indicate a drive\n");
Joe Perchesa9a79df2011-04-15 15:51:59 -07002778 ata_dev_warn(dev,
Alan Coxc5038fc2007-10-25 14:21:16 +01002779"fault or invalid emulation. Contact drive vendor for information.\n");
2780 }
2781 }
2782
Tejun Heoac70a962008-11-27 13:36:48 +09002783 if ((dev->horkage & ATA_HORKAGE_FIRMWARE_WARN) && print_info) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07002784 ata_dev_warn(dev, "WARNING: device requires firmware update to be fully functional\n");
2785 ata_dev_warn(dev, " contact the vendor or visit http://ata.wiki.kernel.org\n");
Tejun Heoac70a962008-11-27 13:36:48 +09002786 }
2787
Tejun Heoffeae412006-03-01 16:09:35 +09002788 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789
2790err_out_nosup:
Borislav Petkov0dd4b212006-06-23 02:29:08 -04002791 if (ata_msg_probe(ap))
Joe Perchesa9a79df2011-04-15 15:51:59 -07002792 ata_dev_dbg(dev, "%s: EXIT, err\n", __func__);
Tejun Heoffeae412006-03-01 16:09:35 +09002793 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794}
2795
2796/**
Alan Cox2e41e8e2007-03-08 23:19:19 +00002797 * ata_cable_40wire - return 40 wire cable type
Alan Coxbe0d18d2007-03-06 02:37:56 -08002798 * @ap: port
2799 *
Alan Cox2e41e8e2007-03-08 23:19:19 +00002800 * Helper method for drivers which want to hardwire 40 wire cable
Alan Coxbe0d18d2007-03-06 02:37:56 -08002801 * detection.
2802 */
2803
2804int ata_cable_40wire(struct ata_port *ap)
2805{
2806 return ATA_CBL_PATA40;
2807}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01002808EXPORT_SYMBOL_GPL(ata_cable_40wire);
Alan Coxbe0d18d2007-03-06 02:37:56 -08002809
2810/**
Alan Cox2e41e8e2007-03-08 23:19:19 +00002811 * ata_cable_80wire - return 80 wire cable type
Alan Coxbe0d18d2007-03-06 02:37:56 -08002812 * @ap: port
2813 *
Alan Cox2e41e8e2007-03-08 23:19:19 +00002814 * Helper method for drivers which want to hardwire 80 wire cable
Alan Coxbe0d18d2007-03-06 02:37:56 -08002815 * detection.
2816 */
2817
2818int ata_cable_80wire(struct ata_port *ap)
2819{
2820 return ATA_CBL_PATA80;
2821}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01002822EXPORT_SYMBOL_GPL(ata_cable_80wire);
Alan Coxbe0d18d2007-03-06 02:37:56 -08002823
2824/**
2825 * ata_cable_unknown - return unknown PATA cable.
2826 * @ap: port
2827 *
2828 * Helper method for drivers which have no PATA cable detection.
2829 */
2830
2831int ata_cable_unknown(struct ata_port *ap)
2832{
2833 return ATA_CBL_PATA_UNK;
2834}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01002835EXPORT_SYMBOL_GPL(ata_cable_unknown);
Alan Coxbe0d18d2007-03-06 02:37:56 -08002836
2837/**
Tejun Heoc88f90c2007-11-27 19:43:48 +09002838 * ata_cable_ignore - return ignored PATA cable.
2839 * @ap: port
2840 *
2841 * Helper method for drivers which don't use cable type to limit
2842 * transfer mode.
2843 */
2844int ata_cable_ignore(struct ata_port *ap)
2845{
2846 return ATA_CBL_PATA_IGN;
2847}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01002848EXPORT_SYMBOL_GPL(ata_cable_ignore);
Tejun Heoc88f90c2007-11-27 19:43:48 +09002849
2850/**
Alan Coxbe0d18d2007-03-06 02:37:56 -08002851 * ata_cable_sata - return SATA cable type
2852 * @ap: port
2853 *
2854 * Helper method for drivers which have SATA cables
2855 */
2856
2857int ata_cable_sata(struct ata_port *ap)
2858{
2859 return ATA_CBL_SATA;
2860}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01002861EXPORT_SYMBOL_GPL(ata_cable_sata);
Alan Coxbe0d18d2007-03-06 02:37:56 -08002862
2863/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864 * ata_bus_probe - Reset and probe ATA bus
2865 * @ap: Bus to probe
2866 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04002867 * Master ATA bus probing function. Initiates a hardware-dependent
2868 * bus reset, then attempts to identify any devices found on
2869 * the bus.
2870 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002872 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873 *
2874 * RETURNS:
Tejun Heo96072e62006-04-01 01:38:17 +09002875 * Zero on success, negative errno otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876 */
2877
Brian King80289162006-08-07 14:27:31 -05002878int ata_bus_probe(struct ata_port *ap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879{
Tejun Heo28ca5c52006-03-01 16:09:36 +09002880 unsigned int classes[ATA_MAX_DEVICES];
Tejun Heo14d2bac2006-04-02 17:54:46 +09002881 int tries[ATA_MAX_DEVICES];
Tejun Heof58229f2007-08-06 18:36:23 +09002882 int rc;
Tejun Heoe82cbdb2006-04-01 01:38:18 +09002883 struct ata_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884
Tejun Heo1eca4362008-11-03 20:03:17 +09002885 ata_for_each_dev(dev, &ap->link, ALL)
Tejun Heof58229f2007-08-06 18:36:23 +09002886 tries[dev->devno] = ATA_PROBE_MAX_TRIES;
Tejun Heo14d2bac2006-04-02 17:54:46 +09002887
2888 retry:
Tejun Heo1eca4362008-11-03 20:03:17 +09002889 ata_for_each_dev(dev, &ap->link, ALL) {
Tejun Heocdeab112007-10-29 16:41:09 +09002890 /* If we issue an SRST then an ATA drive (not ATAPI)
2891 * may change configuration and be in PIO0 timing. If
2892 * we do a hard reset (or are coming from power on)
2893 * this is true for ATA or ATAPI. Until we've set a
2894 * suitable controller mode we should not touch the
2895 * bus as we may be talking too fast.
2896 */
2897 dev->pio_mode = XFER_PIO_0;
Aaron Lu54169122012-12-03 11:35:02 +08002898 dev->dma_mode = 0xff;
Tejun Heocdeab112007-10-29 16:41:09 +09002899
2900 /* If the controller has a pio mode setup function
2901 * then use it to set the chipset to rights. Don't
2902 * touch the DMA setup as that will be dealt with when
2903 * configuring devices.
2904 */
2905 if (ap->ops->set_piomode)
2906 ap->ops->set_piomode(ap, dev);
2907 }
2908
Tejun Heo20444702006-03-13 01:57:01 +09002909 /* reset and determine device classes */
Tejun Heo52783c52006-05-31 18:28:22 +09002910 ap->ops->phy_reset(ap);
Tejun Heo2061a472006-03-12 00:57:39 +09002911
Tejun Heo1eca4362008-11-03 20:03:17 +09002912 ata_for_each_dev(dev, &ap->link, ALL) {
Tejun Heo3e4ec342010-05-10 21:41:30 +02002913 if (dev->class != ATA_DEV_UNKNOWN)
Tejun Heo52783c52006-05-31 18:28:22 +09002914 classes[dev->devno] = dev->class;
2915 else
2916 classes[dev->devno] = ATA_DEV_NONE;
Tejun Heo20444702006-03-13 01:57:01 +09002917
Tejun Heo52783c52006-05-31 18:28:22 +09002918 dev->class = ATA_DEV_UNKNOWN;
Tejun Heo28ca5c52006-03-01 16:09:36 +09002919 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920
Jeff Garzikf31f0cc2007-03-02 17:47:28 -05002921 /* read IDENTIFY page and configure devices. We have to do the identify
2922 specific sequence bass-ackwards so that PDIAG- is released by
2923 the slave device */
2924
Tejun Heo1eca4362008-11-03 20:03:17 +09002925 ata_for_each_dev(dev, &ap->link, ALL_REVERSE) {
Tejun Heof58229f2007-08-06 18:36:23 +09002926 if (tries[dev->devno])
2927 dev->class = classes[dev->devno];
Tejun Heo14d2bac2006-04-02 17:54:46 +09002928
Tejun Heoe1211e32006-04-01 01:38:18 +09002929 if (!ata_dev_enabled(dev))
Tejun Heoffeae412006-03-01 16:09:35 +09002930 continue;
2931
Tejun Heobff04642006-11-10 18:08:10 +09002932 rc = ata_dev_read_id(dev, &dev->class, ATA_READID_POSTRESET,
2933 dev->id);
Tejun Heo14d2bac2006-04-02 17:54:46 +09002934 if (rc)
2935 goto fail;
Jeff Garzikf31f0cc2007-03-02 17:47:28 -05002936 }
2937
Alan Coxbe0d18d2007-03-06 02:37:56 -08002938 /* Now ask for the cable type as PDIAG- should have been released */
2939 if (ap->ops->cable_detect)
2940 ap->cbl = ap->ops->cable_detect(ap);
2941
Tejun Heo1eca4362008-11-03 20:03:17 +09002942 /* We may have SATA bridge glue hiding here irrespective of
2943 * the reported cable types and sensed types. When SATA
2944 * drives indicate we have a bridge, we don't know which end
2945 * of the link the bridge is which is a problem.
2946 */
2947 ata_for_each_dev(dev, &ap->link, ENABLED)
Alan Cox614fe292007-08-22 23:22:45 +01002948 if (ata_id_is_sata(dev->id))
2949 ap->cbl = ATA_CBL_SATA;
Alan Cox614fe292007-08-22 23:22:45 +01002950
Jeff Garzikf31f0cc2007-03-02 17:47:28 -05002951 /* After the identify sequence we can now set up the devices. We do
2952 this in the normal order so that the user doesn't get confused */
2953
Tejun Heo1eca4362008-11-03 20:03:17 +09002954 ata_for_each_dev(dev, &ap->link, ENABLED) {
Tejun Heo9af5c9c2007-08-06 18:36:22 +09002955 ap->link.eh_context.i.flags |= ATA_EHI_PRINTINFO;
Tejun Heoefdaedc2006-11-01 18:38:52 +09002956 rc = ata_dev_configure(dev);
Tejun Heo9af5c9c2007-08-06 18:36:22 +09002957 ap->link.eh_context.i.flags &= ~ATA_EHI_PRINTINFO;
Tejun Heo14d2bac2006-04-02 17:54:46 +09002958 if (rc)
2959 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960 }
2961
Tejun Heoe82cbdb2006-04-01 01:38:18 +09002962 /* configure transfer mode */
Tejun Heo02607312007-08-06 18:36:23 +09002963 rc = ata_set_mode(&ap->link, &dev);
Tejun Heo4ae72a12007-02-02 16:22:30 +09002964 if (rc)
Tejun Heo51713d32006-04-11 22:26:29 +09002965 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966
Tejun Heo1eca4362008-11-03 20:03:17 +09002967 ata_for_each_dev(dev, &ap->link, ENABLED)
2968 return 0;
Alan Coxe35a9e02006-03-27 18:46:37 +01002969
Tejun Heo96072e62006-04-01 01:38:17 +09002970 return -ENODEV;
Tejun Heo14d2bac2006-04-02 17:54:46 +09002971
2972 fail:
Tejun Heo4ae72a12007-02-02 16:22:30 +09002973 tries[dev->devno]--;
2974
Tejun Heo14d2bac2006-04-02 17:54:46 +09002975 switch (rc) {
2976 case -EINVAL:
Tejun Heo4ae72a12007-02-02 16:22:30 +09002977 /* eeek, something went very wrong, give up */
Tejun Heo14d2bac2006-04-02 17:54:46 +09002978 tries[dev->devno] = 0;
2979 break;
Tejun Heo4ae72a12007-02-02 16:22:30 +09002980
2981 case -ENODEV:
2982 /* give it just one more chance */
2983 tries[dev->devno] = min(tries[dev->devno], 1);
Gustavo A. R. Silva05b83602017-10-12 14:19:16 -05002984 /* fall through */
Tejun Heo14d2bac2006-04-02 17:54:46 +09002985 case -EIO:
Tejun Heo4ae72a12007-02-02 16:22:30 +09002986 if (tries[dev->devno] == 1) {
2987 /* This is the last chance, better to slow
2988 * down than lose it.
2989 */
Tejun Heoa07d4992009-01-29 20:31:33 +09002990 sata_down_spd_limit(&ap->link, 0);
Tejun Heo4ae72a12007-02-02 16:22:30 +09002991 ata_down_xfermask_limit(dev, ATA_DNXFER_PIO);
2992 }
Tejun Heo14d2bac2006-04-02 17:54:46 +09002993 }
2994
Tejun Heo4ae72a12007-02-02 16:22:30 +09002995 if (!tries[dev->devno])
Tejun Heo3373efd2006-05-15 20:57:53 +09002996 ata_dev_disable(dev);
Tejun Heoec573752006-04-11 22:26:29 +09002997
Tejun Heo14d2bac2006-04-02 17:54:46 +09002998 goto retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999}
3000
3001/**
Tejun Heo3be680b2005-12-19 22:35:02 +09003002 * sata_print_link_status - Print SATA link status
Tejun Heo936fd732007-08-06 18:36:23 +09003003 * @link: SATA link to printk link status about
Tejun Heo3be680b2005-12-19 22:35:02 +09003004 *
3005 * This function prints link speed and status of a SATA link.
3006 *
3007 * LOCKING:
3008 * None.
3009 */
Adrian Bunk6bdb4fc2008-04-21 11:51:11 +03003010static void sata_print_link_status(struct ata_link *link)
Tejun Heo3be680b2005-12-19 22:35:02 +09003011{
Tejun Heo6d5f9732006-04-03 00:09:41 +09003012 u32 sstatus, scontrol, tmp;
Tejun Heo3be680b2005-12-19 22:35:02 +09003013
Tejun Heo936fd732007-08-06 18:36:23 +09003014 if (sata_scr_read(link, SCR_STATUS, &sstatus))
Tejun Heo3be680b2005-12-19 22:35:02 +09003015 return;
Tejun Heo936fd732007-08-06 18:36:23 +09003016 sata_scr_read(link, SCR_CONTROL, &scontrol);
Tejun Heo3be680b2005-12-19 22:35:02 +09003017
Tejun Heob1c72912008-07-31 17:02:43 +09003018 if (ata_phys_link_online(link)) {
Tejun Heo3be680b2005-12-19 22:35:02 +09003019 tmp = (sstatus >> 4) & 0xf;
Joe Perchesa9a79df2011-04-15 15:51:59 -07003020 ata_link_info(link, "SATA link up %s (SStatus %X SControl %X)\n",
3021 sata_spd_string(tmp), sstatus, scontrol);
Tejun Heo3be680b2005-12-19 22:35:02 +09003022 } else {
Joe Perchesa9a79df2011-04-15 15:51:59 -07003023 ata_link_info(link, "SATA link down (SStatus %X SControl %X)\n",
3024 sstatus, scontrol);
Tejun Heo3be680b2005-12-19 22:35:02 +09003025 }
3026}
3027
3028/**
Alan Coxebdfca6e2006-03-23 15:38:34 +00003029 * ata_dev_pair - return other device on cable
Alan Coxebdfca6e2006-03-23 15:38:34 +00003030 * @adev: device
3031 *
3032 * Obtain the other device on the same cable, or if none is
3033 * present NULL is returned
3034 */
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05003035
Tejun Heo3373efd2006-05-15 20:57:53 +09003036struct ata_device *ata_dev_pair(struct ata_device *adev)
Alan Coxebdfca6e2006-03-23 15:38:34 +00003037{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09003038 struct ata_link *link = adev->link;
3039 struct ata_device *pair = &link->device[1 - adev->devno];
Tejun Heoe1211e32006-04-01 01:38:18 +09003040 if (!ata_dev_enabled(pair))
Alan Coxebdfca6e2006-03-23 15:38:34 +00003041 return NULL;
3042 return pair;
3043}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01003044EXPORT_SYMBOL_GPL(ata_dev_pair);
Alan Coxebdfca6e2006-03-23 15:38:34 +00003045
3046/**
Tejun Heo3c567b72006-05-15 20:57:23 +09003047 * sata_down_spd_limit - adjust SATA spd limit downward
Tejun Heo936fd732007-08-06 18:36:23 +09003048 * @link: Link to adjust SATA spd limit for
Tejun Heoa07d4992009-01-29 20:31:33 +09003049 * @spd_limit: Additional limit
Tejun Heo1c3fae42006-04-02 20:53:28 +09003050 *
Tejun Heo936fd732007-08-06 18:36:23 +09003051 * Adjust SATA spd limit of @link downward. Note that this
Tejun Heo1c3fae42006-04-02 20:53:28 +09003052 * function only adjusts the limit. The change must be applied
Tejun Heo3c567b72006-05-15 20:57:23 +09003053 * using sata_set_spd().
Tejun Heo1c3fae42006-04-02 20:53:28 +09003054 *
Tejun Heoa07d4992009-01-29 20:31:33 +09003055 * If @spd_limit is non-zero, the speed is limited to equal to or
3056 * lower than @spd_limit if such speed is supported. If
3057 * @spd_limit is slower than any supported speed, only the lowest
3058 * supported speed is allowed.
3059 *
Tejun Heo1c3fae42006-04-02 20:53:28 +09003060 * LOCKING:
3061 * Inherited from caller.
3062 *
3063 * RETURNS:
3064 * 0 on success, negative errno on failure
3065 */
Tejun Heoa07d4992009-01-29 20:31:33 +09003066int sata_down_spd_limit(struct ata_link *link, u32 spd_limit)
Tejun Heo1c3fae42006-04-02 20:53:28 +09003067{
Tejun Heo81952c52006-05-15 20:57:47 +09003068 u32 sstatus, spd, mask;
Tejun Heoa07d4992009-01-29 20:31:33 +09003069 int rc, bit;
Tejun Heo1c3fae42006-04-02 20:53:28 +09003070
Tejun Heo936fd732007-08-06 18:36:23 +09003071 if (!sata_scr_valid(link))
Tejun Heo008a7892007-07-16 14:29:40 +09003072 return -EOPNOTSUPP;
3073
3074 /* If SCR can be read, use it to determine the current SPD.
Tejun Heo936fd732007-08-06 18:36:23 +09003075 * If not, use cached value in link->sata_spd.
Tejun Heo008a7892007-07-16 14:29:40 +09003076 */
Tejun Heo936fd732007-08-06 18:36:23 +09003077 rc = sata_scr_read(link, SCR_STATUS, &sstatus);
Tejun Heo9913ff8a2009-01-29 20:31:31 +09003078 if (rc == 0 && ata_sstatus_online(sstatus))
Tejun Heo008a7892007-07-16 14:29:40 +09003079 spd = (sstatus >> 4) & 0xf;
3080 else
Tejun Heo936fd732007-08-06 18:36:23 +09003081 spd = link->sata_spd;
Tejun Heo1c3fae42006-04-02 20:53:28 +09003082
Tejun Heo936fd732007-08-06 18:36:23 +09003083 mask = link->sata_spd_limit;
Tejun Heo1c3fae42006-04-02 20:53:28 +09003084 if (mask <= 1)
3085 return -EINVAL;
Tejun Heo008a7892007-07-16 14:29:40 +09003086
3087 /* unconditionally mask off the highest bit */
Tejun Heoa07d4992009-01-29 20:31:33 +09003088 bit = fls(mask) - 1;
3089 mask &= ~(1 << bit);
Tejun Heo1c3fae42006-04-02 20:53:28 +09003090
David Milburn2dc0b462017-11-14 16:17:25 -06003091 /*
3092 * Mask off all speeds higher than or equal to the current one. At
3093 * this point, if current SPD is not available and we previously
3094 * recorded the link speed from SStatus, the driver has already
3095 * masked off the highest bit so mask should already be 1 or 0.
3096 * Otherwise, we should not force 1.5Gbps on a link where we have
3097 * not previously recorded speed from SStatus. Just return in this
3098 * case.
Tejun Heo008a7892007-07-16 14:29:40 +09003099 */
3100 if (spd > 1)
3101 mask &= (1 << (spd - 1)) - 1;
3102 else
David Milburn2dc0b462017-11-14 16:17:25 -06003103 return -EINVAL;
Tejun Heo008a7892007-07-16 14:29:40 +09003104
3105 /* were we already at the bottom? */
Tejun Heo1c3fae42006-04-02 20:53:28 +09003106 if (!mask)
3107 return -EINVAL;
3108
Tejun Heoa07d4992009-01-29 20:31:33 +09003109 if (spd_limit) {
3110 if (mask & ((1 << spd_limit) - 1))
3111 mask &= (1 << spd_limit) - 1;
3112 else {
3113 bit = ffs(mask) - 1;
3114 mask = 1 << bit;
3115 }
3116 }
3117
Tejun Heo936fd732007-08-06 18:36:23 +09003118 link->sata_spd_limit = mask;
Tejun Heo1c3fae42006-04-02 20:53:28 +09003119
Joe Perchesa9a79df2011-04-15 15:51:59 -07003120 ata_link_warn(link, "limiting SATA link speed to %s\n",
3121 sata_spd_string(fls(mask)));
Tejun Heo1c3fae42006-04-02 20:53:28 +09003122
3123 return 0;
3124}
3125
Tejun Heo936fd732007-08-06 18:36:23 +09003126static int __sata_set_spd_needed(struct ata_link *link, u32 *scontrol)
Tejun Heo1c3fae42006-04-02 20:53:28 +09003127{
Tejun Heo52702222007-10-31 10:17:07 +09003128 struct ata_link *host_link = &link->ap->link;
3129 u32 limit, target, spd;
Tejun Heo1c3fae42006-04-02 20:53:28 +09003130
Tejun Heo52702222007-10-31 10:17:07 +09003131 limit = link->sata_spd_limit;
3132
3133 /* Don't configure downstream link faster than upstream link.
3134 * It doesn't speed up anything and some PMPs choke on such
3135 * configuration.
3136 */
3137 if (!ata_is_host_link(link) && host_link->sata_spd)
3138 limit &= (1 << host_link->sata_spd) - 1;
3139
3140 if (limit == UINT_MAX)
3141 target = 0;
Tejun Heo1c3fae42006-04-02 20:53:28 +09003142 else
Tejun Heo52702222007-10-31 10:17:07 +09003143 target = fls(limit);
Tejun Heo1c3fae42006-04-02 20:53:28 +09003144
3145 spd = (*scontrol >> 4) & 0xf;
Tejun Heo52702222007-10-31 10:17:07 +09003146 *scontrol = (*scontrol & ~0xf0) | ((target & 0xf) << 4);
Tejun Heo1c3fae42006-04-02 20:53:28 +09003147
Tejun Heo52702222007-10-31 10:17:07 +09003148 return spd != target;
Tejun Heo1c3fae42006-04-02 20:53:28 +09003149}
3150
3151/**
Tejun Heo3c567b72006-05-15 20:57:23 +09003152 * sata_set_spd_needed - is SATA spd configuration needed
Tejun Heo936fd732007-08-06 18:36:23 +09003153 * @link: Link in question
Tejun Heo1c3fae42006-04-02 20:53:28 +09003154 *
3155 * Test whether the spd limit in SControl matches
Tejun Heo936fd732007-08-06 18:36:23 +09003156 * @link->sata_spd_limit. This function is used to determine
Tejun Heo1c3fae42006-04-02 20:53:28 +09003157 * whether hardreset is necessary to apply SATA spd
3158 * configuration.
3159 *
3160 * LOCKING:
3161 * Inherited from caller.
3162 *
3163 * RETURNS:
3164 * 1 if SATA spd configuration is needed, 0 otherwise.
3165 */
Adrian Bunk1dc55e82008-04-21 11:51:17 +03003166static int sata_set_spd_needed(struct ata_link *link)
Tejun Heo1c3fae42006-04-02 20:53:28 +09003167{
3168 u32 scontrol;
3169
Tejun Heo936fd732007-08-06 18:36:23 +09003170 if (sata_scr_read(link, SCR_CONTROL, &scontrol))
Tejun Heodb64bcf2007-10-31 10:17:06 +09003171 return 1;
Tejun Heo1c3fae42006-04-02 20:53:28 +09003172
Tejun Heo936fd732007-08-06 18:36:23 +09003173 return __sata_set_spd_needed(link, &scontrol);
Tejun Heo1c3fae42006-04-02 20:53:28 +09003174}
3175
3176/**
Tejun Heo3c567b72006-05-15 20:57:23 +09003177 * sata_set_spd - set SATA spd according to spd limit
Tejun Heo936fd732007-08-06 18:36:23 +09003178 * @link: Link to set SATA spd for
Tejun Heo1c3fae42006-04-02 20:53:28 +09003179 *
Tejun Heo936fd732007-08-06 18:36:23 +09003180 * Set SATA spd of @link according to sata_spd_limit.
Tejun Heo1c3fae42006-04-02 20:53:28 +09003181 *
3182 * LOCKING:
3183 * Inherited from caller.
3184 *
3185 * RETURNS:
3186 * 0 if spd doesn't need to be changed, 1 if spd has been
Tejun Heo81952c52006-05-15 20:57:47 +09003187 * changed. Negative errno if SCR registers are inaccessible.
Tejun Heo1c3fae42006-04-02 20:53:28 +09003188 */
Tejun Heo936fd732007-08-06 18:36:23 +09003189int sata_set_spd(struct ata_link *link)
Tejun Heo1c3fae42006-04-02 20:53:28 +09003190{
3191 u32 scontrol;
Tejun Heo81952c52006-05-15 20:57:47 +09003192 int rc;
Tejun Heo1c3fae42006-04-02 20:53:28 +09003193
Tejun Heo936fd732007-08-06 18:36:23 +09003194 if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
Tejun Heo81952c52006-05-15 20:57:47 +09003195 return rc;
Tejun Heo1c3fae42006-04-02 20:53:28 +09003196
Tejun Heo936fd732007-08-06 18:36:23 +09003197 if (!__sata_set_spd_needed(link, &scontrol))
Tejun Heo1c3fae42006-04-02 20:53:28 +09003198 return 0;
3199
Tejun Heo936fd732007-08-06 18:36:23 +09003200 if ((rc = sata_scr_write(link, SCR_CONTROL, scontrol)))
Tejun Heo81952c52006-05-15 20:57:47 +09003201 return rc;
3202
Tejun Heo1c3fae42006-04-02 20:53:28 +09003203 return 1;
3204}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01003205EXPORT_SYMBOL_GPL(sata_set_spd);
Tejun Heo1c3fae42006-04-02 20:53:28 +09003206
Bartlomiej Zolnierkiewicza9b2c122020-03-26 16:58:07 +01003207#ifdef CONFIG_ATA_ACPI
Tejun Heocf176e12006-04-02 17:54:46 +09003208/**
Tejun Heoa0f79b92007-12-18 16:33:05 +09003209 * ata_timing_cycle2mode - find xfer mode for the specified cycle duration
3210 * @xfer_shift: ATA_SHIFT_* value for transfer type to examine.
3211 * @cycle: cycle duration in ns
3212 *
3213 * Return matching xfer mode for @cycle. The returned mode is of
3214 * the transfer type specified by @xfer_shift. If @cycle is too
3215 * slow for @xfer_shift, 0xff is returned. If @cycle is faster
3216 * than the fastest known mode, the fasted mode is returned.
3217 *
3218 * LOCKING:
3219 * None.
3220 *
3221 * RETURNS:
3222 * Matching xfer_mode, 0xff if no match found.
3223 */
3224u8 ata_timing_cycle2mode(unsigned int xfer_shift, int cycle)
3225{
3226 u8 base_mode = 0xff, last_mode = 0xff;
3227 const struct ata_xfer_ent *ent;
3228 const struct ata_timing *t;
3229
3230 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
3231 if (ent->shift == xfer_shift)
3232 base_mode = ent->base;
3233
3234 for (t = ata_timing_find_mode(base_mode);
3235 t && ata_xfer_mode2shift(t->mode) == xfer_shift; t++) {
3236 unsigned short this_cycle;
3237
3238 switch (xfer_shift) {
3239 case ATA_SHIFT_PIO:
3240 case ATA_SHIFT_MWDMA:
3241 this_cycle = t->cycle;
3242 break;
3243 case ATA_SHIFT_UDMA:
3244 this_cycle = t->udma;
3245 break;
3246 default:
3247 return 0xff;
3248 }
3249
3250 if (cycle > this_cycle)
3251 break;
3252
3253 last_mode = t->mode;
3254 }
3255
3256 return last_mode;
3257}
Bartlomiej Zolnierkiewicza9b2c122020-03-26 16:58:07 +01003258#endif
Tejun Heoa0f79b92007-12-18 16:33:05 +09003259
3260/**
Tejun Heocf176e12006-04-02 17:54:46 +09003261 * ata_down_xfermask_limit - adjust dev xfer masks downward
Tejun Heocf176e12006-04-02 17:54:46 +09003262 * @dev: Device to adjust xfer masks
Tejun Heo458337d2007-02-02 16:22:30 +09003263 * @sel: ATA_DNXFER_* selector
Tejun Heocf176e12006-04-02 17:54:46 +09003264 *
3265 * Adjust xfer masks of @dev downward. Note that this function
3266 * does not apply the change. Invoking ata_set_mode() afterwards
3267 * will apply the limit.
3268 *
3269 * LOCKING:
3270 * Inherited from caller.
3271 *
3272 * RETURNS:
3273 * 0 on success, negative errno on failure
3274 */
Tejun Heo458337d2007-02-02 16:22:30 +09003275int ata_down_xfermask_limit(struct ata_device *dev, unsigned int sel)
Tejun Heocf176e12006-04-02 17:54:46 +09003276{
Tejun Heo458337d2007-02-02 16:22:30 +09003277 char buf[32];
Tejun Heo7dc951a2007-11-27 19:43:42 +09003278 unsigned long orig_mask, xfer_mask;
3279 unsigned long pio_mask, mwdma_mask, udma_mask;
Tejun Heo458337d2007-02-02 16:22:30 +09003280 int quiet, highbit;
Tejun Heocf176e12006-04-02 17:54:46 +09003281
Tejun Heo458337d2007-02-02 16:22:30 +09003282 quiet = !!(sel & ATA_DNXFER_QUIET);
3283 sel &= ~ATA_DNXFER_QUIET;
Tejun Heocf176e12006-04-02 17:54:46 +09003284
Tejun Heo458337d2007-02-02 16:22:30 +09003285 xfer_mask = orig_mask = ata_pack_xfermask(dev->pio_mask,
3286 dev->mwdma_mask,
3287 dev->udma_mask);
3288 ata_unpack_xfermask(xfer_mask, &pio_mask, &mwdma_mask, &udma_mask);
Tejun Heocf176e12006-04-02 17:54:46 +09003289
Tejun Heo458337d2007-02-02 16:22:30 +09003290 switch (sel) {
3291 case ATA_DNXFER_PIO:
3292 highbit = fls(pio_mask) - 1;
3293 pio_mask &= ~(1 << highbit);
3294 break;
3295
3296 case ATA_DNXFER_DMA:
3297 if (udma_mask) {
3298 highbit = fls(udma_mask) - 1;
3299 udma_mask &= ~(1 << highbit);
3300 if (!udma_mask)
3301 return -ENOENT;
3302 } else if (mwdma_mask) {
3303 highbit = fls(mwdma_mask) - 1;
3304 mwdma_mask &= ~(1 << highbit);
3305 if (!mwdma_mask)
3306 return -ENOENT;
3307 }
3308 break;
3309
3310 case ATA_DNXFER_40C:
3311 udma_mask &= ATA_UDMA_MASK_40C;
3312 break;
3313
3314 case ATA_DNXFER_FORCE_PIO0:
3315 pio_mask &= 1;
Gustavo A. R. Silva05b83602017-10-12 14:19:16 -05003316 /* fall through */
Tejun Heo458337d2007-02-02 16:22:30 +09003317 case ATA_DNXFER_FORCE_PIO:
3318 mwdma_mask = 0;
3319 udma_mask = 0;
3320 break;
3321
Tejun Heo458337d2007-02-02 16:22:30 +09003322 default:
3323 BUG();
3324 }
3325
3326 xfer_mask &= ata_pack_xfermask(pio_mask, mwdma_mask, udma_mask);
3327
3328 if (!(xfer_mask & ATA_MASK_PIO) || xfer_mask == orig_mask)
3329 return -ENOENT;
3330
3331 if (!quiet) {
3332 if (xfer_mask & (ATA_MASK_MWDMA | ATA_MASK_UDMA))
3333 snprintf(buf, sizeof(buf), "%s:%s",
3334 ata_mode_string(xfer_mask),
3335 ata_mode_string(xfer_mask & ATA_MASK_PIO));
3336 else
3337 snprintf(buf, sizeof(buf), "%s",
3338 ata_mode_string(xfer_mask));
3339
Joe Perchesa9a79df2011-04-15 15:51:59 -07003340 ata_dev_warn(dev, "limiting speed to %s\n", buf);
Tejun Heo458337d2007-02-02 16:22:30 +09003341 }
Tejun Heocf176e12006-04-02 17:54:46 +09003342
3343 ata_unpack_xfermask(xfer_mask, &dev->pio_mask, &dev->mwdma_mask,
3344 &dev->udma_mask);
3345
Tejun Heocf176e12006-04-02 17:54:46 +09003346 return 0;
Tejun Heocf176e12006-04-02 17:54:46 +09003347}
3348
Tejun Heo3373efd2006-05-15 20:57:53 +09003349static int ata_dev_set_mode(struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003350{
Tejun Heod0cb43b2009-07-09 09:27:50 +09003351 struct ata_port *ap = dev->link->ap;
Tejun Heo9af5c9c2007-08-06 18:36:22 +09003352 struct ata_eh_context *ehc = &dev->link->eh_context;
Tejun Heod0cb43b2009-07-09 09:27:50 +09003353 const bool nosetxfer = dev->horkage & ATA_HORKAGE_NOSETXFER;
Tejun Heo4055dee2008-02-07 10:34:08 +09003354 const char *dev_err_whine = "";
3355 int ign_dev_err = 0;
Tejun Heod0cb43b2009-07-09 09:27:50 +09003356 unsigned int err_mask = 0;
Tejun Heo83206a22006-03-24 15:25:31 +09003357 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358
Tejun Heoe8384602006-04-02 18:51:53 +09003359 dev->flags &= ~ATA_DFLAG_PIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360 if (dev->xfer_shift == ATA_SHIFT_PIO)
3361 dev->flags |= ATA_DFLAG_PIO;
3362
Tejun Heod0cb43b2009-07-09 09:27:50 +09003363 if (nosetxfer && ap->flags & ATA_FLAG_SATA && ata_id_is_sata(dev->id))
3364 dev_err_whine = " (SET_XFERMODE skipped)";
3365 else {
3366 if (nosetxfer)
Joe Perchesa9a79df2011-04-15 15:51:59 -07003367 ata_dev_warn(dev,
3368 "NOSETXFER but PATA detected - can't "
3369 "skip SETXFER, might malfunction\n");
Tejun Heod0cb43b2009-07-09 09:27:50 +09003370 err_mask = ata_dev_set_xfermode(dev);
3371 }
Jeff Garzik2dcb4072007-10-19 06:42:56 -04003372
Tejun Heo4055dee2008-02-07 10:34:08 +09003373 if (err_mask & ~AC_ERR_DEV)
3374 goto fail;
Jeff Garzik2dcb4072007-10-19 06:42:56 -04003375
Tejun Heo4055dee2008-02-07 10:34:08 +09003376 /* revalidate */
Tejun Heobaa1e782006-11-01 18:39:27 +09003377 ehc->i.flags |= ATA_EHI_POST_SETMODE;
Tejun Heo422c9da2007-09-23 13:14:12 +09003378 rc = ata_dev_revalidate(dev, ATA_DEV_UNKNOWN, 0);
Tejun Heobaa1e782006-11-01 18:39:27 +09003379 ehc->i.flags &= ~ATA_EHI_POST_SETMODE;
Tejun Heo5eb45c02006-04-02 18:51:52 +09003380 if (rc)
Tejun Heo83206a22006-03-24 15:25:31 +09003381 return rc;
Tejun Heo48a8a142006-03-05 17:55:58 +09003382
Alan Coxb93fda12008-04-08 16:36:44 +01003383 if (dev->xfer_shift == ATA_SHIFT_PIO) {
3384 /* Old CFA may refuse this command, which is just fine */
3385 if (ata_id_is_cfa(dev->id))
3386 ign_dev_err = 1;
3387 /* Catch several broken garbage emulations plus some pre
3388 ATA devices */
3389 if (ata_id_major_version(dev->id) == 0 &&
3390 dev->pio_mode <= XFER_PIO_2)
3391 ign_dev_err = 1;
3392 /* Some very old devices and some bad newer ones fail
3393 any kind of SET_XFERMODE request but support PIO0-2
3394 timings and no IORDY */
3395 if (!ata_id_has_iordy(dev->id) && dev->pio_mode <= XFER_PIO_2)
3396 ign_dev_err = 1;
3397 }
Tejun Heo4055dee2008-02-07 10:34:08 +09003398 /* Early MWDMA devices do DMA but don't allow DMA mode setting.
3399 Don't fail an MWDMA0 set IFF the device indicates it is in MWDMA0 */
3400 if (dev->xfer_shift == ATA_SHIFT_MWDMA &&
3401 dev->dma_mode == XFER_MW_DMA_0 &&
3402 (dev->id[63] >> 8) & 1)
3403 ign_dev_err = 1;
3404
3405 /* if the device is actually configured correctly, ignore dev err */
3406 if (dev->xfer_mode == ata_xfer_mask2mode(ata_id_xfermask(dev->id)))
3407 ign_dev_err = 1;
3408
3409 if (err_mask & AC_ERR_DEV) {
3410 if (!ign_dev_err)
3411 goto fail;
3412 else
3413 dev_err_whine = " (device error ignored)";
3414 }
3415
Tejun Heo23e71c32006-03-06 04:31:57 +09003416 DPRINTK("xfer_shift=%u, xfer_mode=0x%x\n",
3417 dev->xfer_shift, (int)dev->xfer_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418
Damien Le Moal07b9b6d2018-05-09 09:28:10 +09003419 if (!(ehc->i.flags & ATA_EHI_QUIET) ||
3420 ehc->i.flags & ATA_EHI_DID_HARDRESET)
3421 ata_dev_info(dev, "configured for %s%s\n",
3422 ata_mode_string(ata_xfer_mode2mask(dev->xfer_mode)),
3423 dev_err_whine);
Tejun Heo4055dee2008-02-07 10:34:08 +09003424
Tejun Heo83206a22006-03-24 15:25:31 +09003425 return 0;
Tejun Heo4055dee2008-02-07 10:34:08 +09003426
3427 fail:
Joe Perchesa9a79df2011-04-15 15:51:59 -07003428 ata_dev_err(dev, "failed to set xfermode (err_mask=0x%x)\n", err_mask);
Tejun Heo4055dee2008-02-07 10:34:08 +09003429 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430}
3431
Linus Torvalds1da177e2005-04-16 15:20:36 -07003432/**
Alan04351822007-03-06 02:37:52 -08003433 * ata_do_set_mode - Program timings and issue SET FEATURES - XFER
Tejun Heo02607312007-08-06 18:36:23 +09003434 * @link: link on which timings will be programmed
Joe Perches1967b7f2008-02-03 17:08:11 +02003435 * @r_failed_dev: out parameter for failed device
Linus Torvalds1da177e2005-04-16 15:20:36 -07003436 *
Alan04351822007-03-06 02:37:52 -08003437 * Standard implementation of the function used to tune and set
3438 * ATA device disk transfer mode (PIO3, UDMA6, etc.). If
3439 * ata_dev_set_mode() fails, pointer to the failing device is
Tejun Heoe82cbdb2006-04-01 01:38:18 +09003440 * returned in @r_failed_dev.
Jeff Garzik780a87f2005-05-30 15:41:05 -04003441 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003442 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003443 * PCI/etc. bus probe sem.
Tejun Heoe82cbdb2006-04-01 01:38:18 +09003444 *
3445 * RETURNS:
3446 * 0 on success, negative errno otherwise
Linus Torvalds1da177e2005-04-16 15:20:36 -07003447 */
Alan04351822007-03-06 02:37:52 -08003448
Tejun Heo02607312007-08-06 18:36:23 +09003449int ata_do_set_mode(struct ata_link *link, struct ata_device **r_failed_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003450{
Tejun Heo02607312007-08-06 18:36:23 +09003451 struct ata_port *ap = link->ap;
Tejun Heoe8e06192006-04-01 01:38:18 +09003452 struct ata_device *dev;
Tejun Heof58229f2007-08-06 18:36:23 +09003453 int rc = 0, used_dma = 0, found = 0;
Tejun Heo3adcebb2006-05-15 20:57:37 +09003454
Tejun Heoa6d5a512006-03-06 04:31:57 +09003455 /* step 1: calculate xfer_mask */
Tejun Heo1eca4362008-11-03 20:03:17 +09003456 ata_for_each_dev(dev, link, ENABLED) {
Tejun Heo7dc951a2007-11-27 19:43:42 +09003457 unsigned long pio_mask, dma_mask;
Alan Coxb3a70602007-10-02 12:38:26 -04003458 unsigned int mode_mask;
Tejun Heoa6d5a512006-03-06 04:31:57 +09003459
Alan Coxb3a70602007-10-02 12:38:26 -04003460 mode_mask = ATA_DMA_MASK_ATA;
3461 if (dev->class == ATA_DEV_ATAPI)
3462 mode_mask = ATA_DMA_MASK_ATAPI;
3463 else if (ata_id_is_cfa(dev->id))
3464 mode_mask = ATA_DMA_MASK_CFA;
3465
Tejun Heo3373efd2006-05-15 20:57:53 +09003466 ata_dev_xfermask(dev);
Tejun Heo33267322008-02-13 09:15:09 +09003467 ata_force_xfermask(dev);
Tejun Heoa6d5a512006-03-06 04:31:57 +09003468
Tejun Heoacf356b2006-03-24 14:07:50 +09003469 pio_mask = ata_pack_xfermask(dev->pio_mask, 0, 0);
Alan Coxb3a70602007-10-02 12:38:26 -04003470
3471 if (libata_dma_mask & mode_mask)
Sergei Shtylyov80a9c432011-12-22 22:41:32 +03003472 dma_mask = ata_pack_xfermask(0, dev->mwdma_mask,
3473 dev->udma_mask);
Alan Coxb3a70602007-10-02 12:38:26 -04003474 else
3475 dma_mask = 0;
3476
Tejun Heoacf356b2006-03-24 14:07:50 +09003477 dev->pio_mode = ata_xfer_mask2mode(pio_mask);
3478 dev->dma_mode = ata_xfer_mask2mode(dma_mask);
Alan Cox5444a6f2006-03-27 18:58:20 +01003479
Tejun Heo4f659772006-04-01 01:38:18 +09003480 found = 1;
Alan Coxb15b3eb2008-08-01 09:18:34 +01003481 if (ata_dma_enabled(dev))
Alan Cox5444a6f2006-03-27 18:58:20 +01003482 used_dma = 1;
Tejun Heoa6d5a512006-03-06 04:31:57 +09003483 }
Tejun Heo4f659772006-04-01 01:38:18 +09003484 if (!found)
Tejun Heoe82cbdb2006-04-01 01:38:18 +09003485 goto out;
Tejun Heoa6d5a512006-03-06 04:31:57 +09003486
3487 /* step 2: always set host PIO timings */
Tejun Heo1eca4362008-11-03 20:03:17 +09003488 ata_for_each_dev(dev, link, ENABLED) {
Tejun Heo70cd0712007-11-27 19:43:40 +09003489 if (dev->pio_mode == 0xff) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07003490 ata_dev_warn(dev, "no PIO support\n");
Tejun Heoe8e06192006-04-01 01:38:18 +09003491 rc = -EINVAL;
Tejun Heoe82cbdb2006-04-01 01:38:18 +09003492 goto out;
Tejun Heoe8e06192006-04-01 01:38:18 +09003493 }
3494
3495 dev->xfer_mode = dev->pio_mode;
3496 dev->xfer_shift = ATA_SHIFT_PIO;
3497 if (ap->ops->set_piomode)
3498 ap->ops->set_piomode(ap, dev);
3499 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003500
Tejun Heoa6d5a512006-03-06 04:31:57 +09003501 /* step 3: set host DMA timings */
Tejun Heo1eca4362008-11-03 20:03:17 +09003502 ata_for_each_dev(dev, link, ENABLED) {
3503 if (!ata_dma_enabled(dev))
Tejun Heoe8e06192006-04-01 01:38:18 +09003504 continue;
3505
3506 dev->xfer_mode = dev->dma_mode;
3507 dev->xfer_shift = ata_xfer_mode2shift(dev->dma_mode);
3508 if (ap->ops->set_dmamode)
3509 ap->ops->set_dmamode(ap, dev);
3510 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003511
3512 /* step 4: update devices' xfer mode */
Tejun Heo1eca4362008-11-03 20:03:17 +09003513 ata_for_each_dev(dev, link, ENABLED) {
Tejun Heo3373efd2006-05-15 20:57:53 +09003514 rc = ata_dev_set_mode(dev);
Tejun Heo5bbc53f2006-04-01 01:38:17 +09003515 if (rc)
Tejun Heoe82cbdb2006-04-01 01:38:18 +09003516 goto out;
Tejun Heo83206a22006-03-24 15:25:31 +09003517 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003518
Tejun Heoe8e06192006-04-01 01:38:18 +09003519 /* Record simplex status. If we selected DMA then the other
3520 * host channels are not permitted to do so.
Alan Cox5444a6f2006-03-27 18:58:20 +01003521 */
Jeff Garzikcca39742006-08-24 03:19:22 -04003522 if (used_dma && (ap->host->flags & ATA_HOST_SIMPLEX))
Alan032af1c2007-03-01 17:36:46 +00003523 ap->host->simplex_claimed = ap;
Alan Cox5444a6f2006-03-27 18:58:20 +01003524
Tejun Heoe82cbdb2006-04-01 01:38:18 +09003525 out:
3526 if (rc)
3527 *r_failed_dev = dev;
3528 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003529}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01003530EXPORT_SYMBOL_GPL(ata_do_set_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003531
3532/**
Tejun Heoaa2731a2008-04-07 22:47:19 +09003533 * ata_wait_ready - wait for link to become ready
3534 * @link: link to be waited on
3535 * @deadline: deadline jiffies for the operation
3536 * @check_ready: callback to check link readiness
3537 *
3538 * Wait for @link to become ready. @check_ready should return
3539 * positive number if @link is ready, 0 if it isn't, -ENODEV if
3540 * link doesn't seem to be occupied, other errno for other error
3541 * conditions.
3542 *
3543 * Transient -ENODEV conditions are allowed for
3544 * ATA_TMOUT_FF_WAIT.
3545 *
3546 * LOCKING:
3547 * EH context.
3548 *
3549 * RETURNS:
Masanari Iidac9b55602016-04-13 23:36:27 +09003550 * 0 if @link is ready before @deadline; otherwise, -errno.
Tejun Heoaa2731a2008-04-07 22:47:19 +09003551 */
3552int ata_wait_ready(struct ata_link *link, unsigned long deadline,
3553 int (*check_ready)(struct ata_link *link))
3554{
3555 unsigned long start = jiffies;
Tejun Heob48d58f2010-04-09 19:46:38 +09003556 unsigned long nodev_deadline;
Tejun Heoaa2731a2008-04-07 22:47:19 +09003557 int warned = 0;
3558
Tejun Heob48d58f2010-04-09 19:46:38 +09003559 /* choose which 0xff timeout to use, read comment in libata.h */
3560 if (link->ap->host->flags & ATA_HOST_PARALLEL_SCAN)
3561 nodev_deadline = ata_deadline(start, ATA_TMOUT_FF_WAIT_LONG);
3562 else
3563 nodev_deadline = ata_deadline(start, ATA_TMOUT_FF_WAIT);
3564
Tejun Heob1c72912008-07-31 17:02:43 +09003565 /* Slave readiness can't be tested separately from master. On
3566 * M/S emulation configuration, this function should be called
3567 * only on the master and it will handle both master and slave.
3568 */
3569 WARN_ON(link == link->ap->slave_link);
3570
Tejun Heoaa2731a2008-04-07 22:47:19 +09003571 if (time_after(nodev_deadline, deadline))
3572 nodev_deadline = deadline;
3573
3574 while (1) {
3575 unsigned long now = jiffies;
3576 int ready, tmp;
3577
3578 ready = tmp = check_ready(link);
3579 if (ready > 0)
3580 return 0;
3581
Tejun Heob48d58f2010-04-09 19:46:38 +09003582 /*
3583 * -ENODEV could be transient. Ignore -ENODEV if link
Tejun Heoaa2731a2008-04-07 22:47:19 +09003584 * is online. Also, some SATA devices take a long
Tejun Heob48d58f2010-04-09 19:46:38 +09003585 * time to clear 0xff after reset. Wait for
3586 * ATA_TMOUT_FF_WAIT[_LONG] on -ENODEV if link isn't
3587 * offline.
Tejun Heoaa2731a2008-04-07 22:47:19 +09003588 *
3589 * Note that some PATA controllers (pata_ali) explode
3590 * if status register is read more than once when
3591 * there's no device attached.
3592 */
3593 if (ready == -ENODEV) {
3594 if (ata_link_online(link))
3595 ready = 0;
3596 else if ((link->ap->flags & ATA_FLAG_SATA) &&
3597 !ata_link_offline(link) &&
3598 time_before(now, nodev_deadline))
3599 ready = 0;
3600 }
3601
3602 if (ready)
3603 return ready;
3604 if (time_after(now, deadline))
3605 return -EBUSY;
3606
3607 if (!warned && time_after(now, start + 5 * HZ) &&
3608 (deadline - now > 3 * HZ)) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07003609 ata_link_warn(link,
Tejun Heoaa2731a2008-04-07 22:47:19 +09003610 "link is slow to respond, please be patient "
3611 "(ready=%d)\n", tmp);
3612 warned = 1;
3613 }
3614
Tejun Heo97750ce2010-09-06 17:56:29 +02003615 ata_msleep(link->ap, 50);
Tejun Heoaa2731a2008-04-07 22:47:19 +09003616 }
3617}
3618
3619/**
3620 * ata_wait_after_reset - wait for link to become ready after reset
3621 * @link: link to be waited on
3622 * @deadline: deadline jiffies for the operation
3623 * @check_ready: callback to check link readiness
3624 *
3625 * Wait for @link to become ready after reset.
3626 *
3627 * LOCKING:
3628 * EH context.
3629 *
3630 * RETURNS:
Masanari Iidac9b55602016-04-13 23:36:27 +09003631 * 0 if @link is ready before @deadline; otherwise, -errno.
Tejun Heoaa2731a2008-04-07 22:47:19 +09003632 */
Harvey Harrison2b4221b2008-04-24 18:37:34 -07003633int ata_wait_after_reset(struct ata_link *link, unsigned long deadline,
Tejun Heoaa2731a2008-04-07 22:47:19 +09003634 int (*check_ready)(struct ata_link *link))
3635{
Tejun Heo97750ce2010-09-06 17:56:29 +02003636 ata_msleep(link->ap, ATA_WAIT_AFTER_RESET);
Tejun Heoaa2731a2008-04-07 22:47:19 +09003637
3638 return ata_wait_ready(link, deadline, check_ready);
3639}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01003640EXPORT_SYMBOL_GPL(ata_wait_after_reset);
Tejun Heoaa2731a2008-04-07 22:47:19 +09003641
3642/**
Tejun Heo936fd732007-08-06 18:36:23 +09003643 * sata_link_debounce - debounce SATA phy status
3644 * @link: ATA link to debounce SATA phy status for
Masanari Iidac9b55602016-04-13 23:36:27 +09003645 * @params: timing parameters { interval, duration, timeout } in msec
Tejun Heod4b2bab2007-02-02 16:50:52 +09003646 * @deadline: deadline jiffies for the operation
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003647 *
Tejun Heo1152b262010-09-01 17:50:05 +02003648 * Make sure SStatus of @link reaches stable state, determined by
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003649 * holding the same value where DET is not 1 for @duration polled
3650 * every @interval, before @timeout. Timeout constraints the
Tejun Heod4b2bab2007-02-02 16:50:52 +09003651 * beginning of the stable state. Because DET gets stuck at 1 on
3652 * some controllers after hot unplugging, this functions waits
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003653 * until timeout then returns 0 if DET is stable at 1.
3654 *
Tejun Heod4b2bab2007-02-02 16:50:52 +09003655 * @timeout is further limited by @deadline. The sooner of the
3656 * two is used.
3657 *
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003658 * LOCKING:
3659 * Kernel thread context (may sleep)
3660 *
3661 * RETURNS:
3662 * 0 on success, -errno on failure.
3663 */
Tejun Heo936fd732007-08-06 18:36:23 +09003664int sata_link_debounce(struct ata_link *link, const unsigned long *params,
3665 unsigned long deadline)
Tejun Heo7a7921e2006-02-02 18:20:00 +09003666{
Tejun Heo341c2c92008-05-20 02:17:51 +09003667 unsigned long interval = params[0];
3668 unsigned long duration = params[1];
Tejun Heod4b2bab2007-02-02 16:50:52 +09003669 unsigned long last_jiffies, t;
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003670 u32 last, cur;
3671 int rc;
3672
Tejun Heo341c2c92008-05-20 02:17:51 +09003673 t = ata_deadline(jiffies, params[2]);
Tejun Heod4b2bab2007-02-02 16:50:52 +09003674 if (time_before(t, deadline))
3675 deadline = t;
3676
Tejun Heo936fd732007-08-06 18:36:23 +09003677 if ((rc = sata_scr_read(link, SCR_STATUS, &cur)))
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003678 return rc;
3679 cur &= 0xf;
3680
3681 last = cur;
3682 last_jiffies = jiffies;
3683
3684 while (1) {
Tejun Heo97750ce2010-09-06 17:56:29 +02003685 ata_msleep(link->ap, interval);
Tejun Heo936fd732007-08-06 18:36:23 +09003686 if ((rc = sata_scr_read(link, SCR_STATUS, &cur)))
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003687 return rc;
3688 cur &= 0xf;
3689
3690 /* DET stable? */
3691 if (cur == last) {
Tejun Heod4b2bab2007-02-02 16:50:52 +09003692 if (cur == 1 && time_before(jiffies, deadline))
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003693 continue;
Tejun Heo341c2c92008-05-20 02:17:51 +09003694 if (time_after(jiffies,
3695 ata_deadline(last_jiffies, duration)))
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003696 return 0;
3697 continue;
3698 }
3699
3700 /* unstable, start over */
3701 last = cur;
3702 last_jiffies = jiffies;
3703
Tejun Heof1545152007-07-16 14:29:40 +09003704 /* Check deadline. If debouncing failed, return
3705 * -EPIPE to tell upper layer to lower link speed.
3706 */
Tejun Heod4b2bab2007-02-02 16:50:52 +09003707 if (time_after(jiffies, deadline))
Tejun Heof1545152007-07-16 14:29:40 +09003708 return -EPIPE;
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003709 }
3710}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01003711EXPORT_SYMBOL_GPL(sata_link_debounce);
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003712
3713/**
Tejun Heo936fd732007-08-06 18:36:23 +09003714 * sata_link_resume - resume SATA link
3715 * @link: ATA link to resume SATA
Masanari Iidac9b55602016-04-13 23:36:27 +09003716 * @params: timing parameters { interval, duration, timeout } in msec
Tejun Heod4b2bab2007-02-02 16:50:52 +09003717 * @deadline: deadline jiffies for the operation
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003718 *
Tejun Heo936fd732007-08-06 18:36:23 +09003719 * Resume SATA phy @link and debounce it.
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003720 *
3721 * LOCKING:
3722 * Kernel thread context (may sleep)
3723 *
3724 * RETURNS:
3725 * 0 on success, -errno on failure.
3726 */
Tejun Heo936fd732007-08-06 18:36:23 +09003727int sata_link_resume(struct ata_link *link, const unsigned long *params,
3728 unsigned long deadline)
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003729{
Tejun Heo5040ab62010-01-11 11:14:44 +09003730 int tries = ATA_LINK_RESUME_TRIES;
Tejun Heoac371982008-04-07 22:47:19 +09003731 u32 scontrol, serror;
Tejun Heo81952c52006-05-15 20:57:47 +09003732 int rc;
Tejun Heo7a7921e2006-02-02 18:20:00 +09003733
Tejun Heo936fd732007-08-06 18:36:23 +09003734 if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
Tejun Heo81952c52006-05-15 20:57:47 +09003735 return rc;
3736
Tejun Heo5040ab62010-01-11 11:14:44 +09003737 /*
3738 * Writes to SControl sometimes get ignored under certain
3739 * controllers (ata_piix SIDPR). Make sure DET actually is
3740 * cleared.
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003741 */
Tejun Heo5040ab62010-01-11 11:14:44 +09003742 do {
3743 scontrol = (scontrol & 0x0f0) | 0x300;
3744 if ((rc = sata_scr_write(link, SCR_CONTROL, scontrol)))
3745 return rc;
3746 /*
3747 * Some PHYs react badly if SStatus is pounded
3748 * immediately after resuming. Delay 200ms before
3749 * debouncing.
3750 */
Danesh Petigarae39b2bb2016-01-07 16:03:33 -08003751 if (!(link->flags & ATA_LFLAG_NO_DB_DELAY))
3752 ata_msleep(link->ap, 200);
Tejun Heo5040ab62010-01-11 11:14:44 +09003753
3754 /* is SControl restored correctly? */
3755 if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
3756 return rc;
3757 } while ((scontrol & 0xf0f) != 0x300 && --tries);
3758
3759 if ((scontrol & 0xf0f) != 0x300) {
Tejun Heo38941c92011-07-01 15:12:09 +02003760 ata_link_warn(link, "failed to resume link (SControl %X)\n",
Joe Perchesa9a79df2011-04-15 15:51:59 -07003761 scontrol);
Tejun Heo5040ab62010-01-11 11:14:44 +09003762 return 0;
3763 }
3764
3765 if (tries < ATA_LINK_RESUME_TRIES)
Joe Perchesa9a79df2011-04-15 15:51:59 -07003766 ata_link_warn(link, "link resume succeeded after %d retries\n",
3767 ATA_LINK_RESUME_TRIES - tries);
Tejun Heo7a7921e2006-02-02 18:20:00 +09003768
Tejun Heoac371982008-04-07 22:47:19 +09003769 if ((rc = sata_link_debounce(link, params, deadline)))
3770 return rc;
3771
Tejun Heof0465192008-05-19 01:15:08 +09003772 /* clear SError, some PHYs require this even for SRST to work */
Tejun Heoac371982008-04-07 22:47:19 +09003773 if (!(rc = sata_scr_read(link, SCR_ERROR, &serror)))
3774 rc = sata_scr_write(link, SCR_ERROR, serror);
Tejun Heoac371982008-04-07 22:47:19 +09003775
Tejun Heof0465192008-05-19 01:15:08 +09003776 return rc != -EINVAL ? rc : 0;
Tejun Heo7a7921e2006-02-02 18:20:00 +09003777}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01003778EXPORT_SYMBOL_GPL(sata_link_resume);
Tejun Heo7a7921e2006-02-02 18:20:00 +09003779
Tejun Heof5914a42006-05-31 18:27:48 +09003780/**
Tejun Heo1152b262010-09-01 17:50:05 +02003781 * sata_link_scr_lpm - manipulate SControl IPM and SPM fields
3782 * @link: ATA link to manipulate SControl for
3783 * @policy: LPM policy to configure
3784 * @spm_wakeup: initiate LPM transition to active state
3785 *
3786 * Manipulate the IPM field of the SControl register of @link
3787 * according to @policy. If @policy is ATA_LPM_MAX_POWER and
3788 * @spm_wakeup is %true, the SPM field is manipulated to wake up
3789 * the link. This function also clears PHYRDY_CHG before
3790 * returning.
3791 *
3792 * LOCKING:
3793 * EH context.
3794 *
3795 * RETURNS:
Masanari Iida84851872015-05-29 23:11:51 +09003796 * 0 on success, -errno otherwise.
Tejun Heo1152b262010-09-01 17:50:05 +02003797 */
3798int sata_link_scr_lpm(struct ata_link *link, enum ata_lpm_policy policy,
3799 bool spm_wakeup)
3800{
3801 struct ata_eh_context *ehc = &link->eh_context;
3802 bool woken_up = false;
3803 u32 scontrol;
3804 int rc;
3805
3806 rc = sata_scr_read(link, SCR_CONTROL, &scontrol);
3807 if (rc)
3808 return rc;
3809
3810 switch (policy) {
3811 case ATA_LPM_MAX_POWER:
3812 /* disable all LPM transitions */
Shane Huang65fe1f02012-09-07 22:40:01 +08003813 scontrol |= (0x7 << 8);
Tejun Heo1152b262010-09-01 17:50:05 +02003814 /* initiate transition to active state */
3815 if (spm_wakeup) {
3816 scontrol |= (0x4 << 12);
3817 woken_up = true;
3818 }
3819 break;
3820 case ATA_LPM_MED_POWER:
3821 /* allow LPM to PARTIAL */
3822 scontrol &= ~(0x1 << 8);
Shane Huang65fe1f02012-09-07 22:40:01 +08003823 scontrol |= (0x6 << 8);
Tejun Heo1152b262010-09-01 17:50:05 +02003824 break;
Hans de Goedef4ac6472017-09-14 12:35:36 +02003825 case ATA_LPM_MED_POWER_WITH_DIPM:
Srinivas Pandruvadaa5ec5a7b2018-07-27 13:47:02 -07003826 case ATA_LPM_MIN_POWER_WITH_PARTIAL:
Tejun Heo1152b262010-09-01 17:50:05 +02003827 case ATA_LPM_MIN_POWER:
Kristen Carlson Accardi8a745f12011-03-04 10:24:11 -08003828 if (ata_link_nr_enabled(link) > 0)
3829 /* no restrictions on LPM transitions */
Shane Huang65fe1f02012-09-07 22:40:01 +08003830 scontrol &= ~(0x7 << 8);
Kristen Carlson Accardi8a745f12011-03-04 10:24:11 -08003831 else {
3832 /* empty port, power off */
3833 scontrol &= ~0xf;
3834 scontrol |= (0x1 << 2);
3835 }
Tejun Heo1152b262010-09-01 17:50:05 +02003836 break;
3837 default:
3838 WARN_ON(1);
3839 }
3840
3841 rc = sata_scr_write(link, SCR_CONTROL, scontrol);
3842 if (rc)
3843 return rc;
3844
3845 /* give the link time to transit out of LPM state */
3846 if (woken_up)
3847 msleep(10);
3848
3849 /* clear PHYRDY_CHG from SError */
3850 ehc->i.serror &= ~SERR_PHYRDY_CHG;
3851 return sata_scr_write(link, SCR_ERROR, SERR_PHYRDY_CHG);
3852}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01003853EXPORT_SYMBOL_GPL(sata_link_scr_lpm);
Tejun Heo1152b262010-09-01 17:50:05 +02003854
3855/**
Tejun Heo0aa11132008-04-07 22:47:18 +09003856 * ata_std_prereset - prepare for reset
Tejun Heocc0680a2007-08-06 18:36:23 +09003857 * @link: ATA link to be reset
Tejun Heod4b2bab2007-02-02 16:50:52 +09003858 * @deadline: deadline jiffies for the operation
Tejun Heof5914a42006-05-31 18:27:48 +09003859 *
Tejun Heocc0680a2007-08-06 18:36:23 +09003860 * @link is about to be reset. Initialize it. Failure from
Tejun Heob8cffc62007-02-02 16:50:52 +09003861 * prereset makes libata abort whole reset sequence and give up
3862 * that port, so prereset should be best-effort. It does its
3863 * best to prepare for reset sequence but if things go wrong, it
3864 * should just whine, not fail.
Tejun Heof5914a42006-05-31 18:27:48 +09003865 *
3866 * LOCKING:
3867 * Kernel thread context (may sleep)
3868 *
3869 * RETURNS:
3870 * 0 on success, -errno otherwise.
3871 */
Tejun Heo0aa11132008-04-07 22:47:18 +09003872int ata_std_prereset(struct ata_link *link, unsigned long deadline)
Tejun Heof5914a42006-05-31 18:27:48 +09003873{
Tejun Heocc0680a2007-08-06 18:36:23 +09003874 struct ata_port *ap = link->ap;
Tejun Heo936fd732007-08-06 18:36:23 +09003875 struct ata_eh_context *ehc = &link->eh_context;
Tejun Heoe9c83912006-07-03 16:07:26 +09003876 const unsigned long *timing = sata_ehc_deb_timing(ehc);
Tejun Heof5914a42006-05-31 18:27:48 +09003877 int rc;
3878
Tejun Heof5914a42006-05-31 18:27:48 +09003879 /* if we're about to do hardreset, nothing more to do */
3880 if (ehc->i.action & ATA_EH_HARDRESET)
3881 return 0;
3882
Tejun Heo936fd732007-08-06 18:36:23 +09003883 /* if SATA, resume link */
Tejun Heoa16abc02007-05-21 18:33:47 +02003884 if (ap->flags & ATA_FLAG_SATA) {
Tejun Heo936fd732007-08-06 18:36:23 +09003885 rc = sata_link_resume(link, timing, deadline);
Tejun Heob8cffc62007-02-02 16:50:52 +09003886 /* whine about phy resume failure but proceed */
3887 if (rc && rc != -EOPNOTSUPP)
Joe Perchesa9a79df2011-04-15 15:51:59 -07003888 ata_link_warn(link,
3889 "failed to resume link for reset (errno=%d)\n",
3890 rc);
Tejun Heof5914a42006-05-31 18:27:48 +09003891 }
3892
Tejun Heo45db2f62008-04-08 01:46:56 +09003893 /* no point in trying softreset on offline link */
Tejun Heob1c72912008-07-31 17:02:43 +09003894 if (ata_phys_link_offline(link))
Tejun Heo45db2f62008-04-08 01:46:56 +09003895 ehc->i.action &= ~ATA_EH_SOFTRESET;
3896
Tejun Heof5914a42006-05-31 18:27:48 +09003897 return 0;
3898}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01003899EXPORT_SYMBOL_GPL(ata_std_prereset);
Tejun Heof5914a42006-05-31 18:27:48 +09003900
Tejun Heoc2bd5802006-01-24 17:05:22 +09003901/**
Tejun Heocc0680a2007-08-06 18:36:23 +09003902 * sata_link_hardreset - reset link via SATA phy reset
3903 * @link: link to reset
Masanari Iidac9b55602016-04-13 23:36:27 +09003904 * @timing: timing parameters { interval, duration, timeout } in msec
Tejun Heod4b2bab2007-02-02 16:50:52 +09003905 * @deadline: deadline jiffies for the operation
Tejun Heo9dadd452008-04-07 22:47:19 +09003906 * @online: optional out parameter indicating link onlineness
3907 * @check_ready: optional callback to check link readiness
Tejun Heoc2bd5802006-01-24 17:05:22 +09003908 *
Tejun Heocc0680a2007-08-06 18:36:23 +09003909 * SATA phy-reset @link using DET bits of SControl register.
Tejun Heo9dadd452008-04-07 22:47:19 +09003910 * After hardreset, link readiness is waited upon using
3911 * ata_wait_ready() if @check_ready is specified. LLDs are
3912 * allowed to not specify @check_ready and wait itself after this
3913 * function returns. Device classification is LLD's
3914 * responsibility.
3915 *
3916 * *@online is set to one iff reset succeeded and @link is online
3917 * after reset.
Tejun Heoc2bd5802006-01-24 17:05:22 +09003918 *
3919 * LOCKING:
3920 * Kernel thread context (may sleep)
3921 *
3922 * RETURNS:
3923 * 0 on success, -errno otherwise.
3924 */
Tejun Heocc0680a2007-08-06 18:36:23 +09003925int sata_link_hardreset(struct ata_link *link, const unsigned long *timing,
Tejun Heo9dadd452008-04-07 22:47:19 +09003926 unsigned long deadline,
3927 bool *online, int (*check_ready)(struct ata_link *))
Tejun Heoc2bd5802006-01-24 17:05:22 +09003928{
Tejun Heo852ee162006-04-01 01:38:18 +09003929 u32 scontrol;
Tejun Heo81952c52006-05-15 20:57:47 +09003930 int rc;
Tejun Heo852ee162006-04-01 01:38:18 +09003931
Tejun Heoc2bd5802006-01-24 17:05:22 +09003932 DPRINTK("ENTER\n");
3933
Tejun Heo9dadd452008-04-07 22:47:19 +09003934 if (online)
3935 *online = false;
3936
Tejun Heo936fd732007-08-06 18:36:23 +09003937 if (sata_set_spd_needed(link)) {
Tejun Heo1c3fae42006-04-02 20:53:28 +09003938 /* SATA spec says nothing about how to reconfigure
3939 * spd. To be on the safe side, turn off phy during
3940 * reconfiguration. This works for at least ICH7 AHCI
3941 * and Sil3124.
3942 */
Tejun Heo936fd732007-08-06 18:36:23 +09003943 if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
Tejun Heob6103f62006-11-01 17:59:53 +09003944 goto out;
Tejun Heo81952c52006-05-15 20:57:47 +09003945
Martin Hicksa34b6fc2006-07-05 15:06:13 -04003946 scontrol = (scontrol & 0x0f0) | 0x304;
Tejun Heo81952c52006-05-15 20:57:47 +09003947
Tejun Heo936fd732007-08-06 18:36:23 +09003948 if ((rc = sata_scr_write(link, SCR_CONTROL, scontrol)))
Tejun Heob6103f62006-11-01 17:59:53 +09003949 goto out;
Tejun Heo1c3fae42006-04-02 20:53:28 +09003950
Tejun Heo936fd732007-08-06 18:36:23 +09003951 sata_set_spd(link);
Tejun Heo1c3fae42006-04-02 20:53:28 +09003952 }
3953
3954 /* issue phy wake/reset */
Tejun Heo936fd732007-08-06 18:36:23 +09003955 if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
Tejun Heob6103f62006-11-01 17:59:53 +09003956 goto out;
Tejun Heo81952c52006-05-15 20:57:47 +09003957
Tejun Heo852ee162006-04-01 01:38:18 +09003958 scontrol = (scontrol & 0x0f0) | 0x301;
Tejun Heo81952c52006-05-15 20:57:47 +09003959
Tejun Heo936fd732007-08-06 18:36:23 +09003960 if ((rc = sata_scr_write_flush(link, SCR_CONTROL, scontrol)))
Tejun Heob6103f62006-11-01 17:59:53 +09003961 goto out;
Tejun Heoc2bd5802006-01-24 17:05:22 +09003962
Tejun Heo1c3fae42006-04-02 20:53:28 +09003963 /* Couldn't find anything in SATA I/II specs, but AHCI-1.1
Tejun Heoc2bd5802006-01-24 17:05:22 +09003964 * 10.4.2 says at least 1 ms.
3965 */
Tejun Heo97750ce2010-09-06 17:56:29 +02003966 ata_msleep(link->ap, 1);
Tejun Heoc2bd5802006-01-24 17:05:22 +09003967
Tejun Heo936fd732007-08-06 18:36:23 +09003968 /* bring link back */
3969 rc = sata_link_resume(link, timing, deadline);
Tejun Heo9dadd452008-04-07 22:47:19 +09003970 if (rc)
3971 goto out;
3972 /* if link is offline nothing more to do */
Tejun Heob1c72912008-07-31 17:02:43 +09003973 if (ata_phys_link_offline(link))
Tejun Heo9dadd452008-04-07 22:47:19 +09003974 goto out;
3975
3976 /* Link is online. From this point, -ENODEV too is an error. */
3977 if (online)
3978 *online = true;
3979
Tejun Heo071f44b2008-04-07 22:47:22 +09003980 if (sata_pmp_supported(link->ap) && ata_is_host_link(link)) {
Tejun Heo9dadd452008-04-07 22:47:19 +09003981 /* If PMP is supported, we have to do follow-up SRST.
3982 * Some PMPs don't send D2H Reg FIS after hardreset if
3983 * the first port is empty. Wait only for
3984 * ATA_TMOUT_PMP_SRST_WAIT.
3985 */
3986 if (check_ready) {
3987 unsigned long pmp_deadline;
3988
Tejun Heo341c2c92008-05-20 02:17:51 +09003989 pmp_deadline = ata_deadline(jiffies,
3990 ATA_TMOUT_PMP_SRST_WAIT);
Tejun Heo9dadd452008-04-07 22:47:19 +09003991 if (time_after(pmp_deadline, deadline))
3992 pmp_deadline = deadline;
3993 ata_wait_ready(link, pmp_deadline, check_ready);
3994 }
3995 rc = -EAGAIN;
3996 goto out;
3997 }
3998
3999 rc = 0;
4000 if (check_ready)
4001 rc = ata_wait_ready(link, deadline, check_ready);
Tejun Heob6103f62006-11-01 17:59:53 +09004002 out:
Tejun Heo0cbf0712008-05-19 01:15:05 +09004003 if (rc && rc != -EAGAIN) {
4004 /* online is set iff link is online && reset succeeded */
4005 if (online)
4006 *online = false;
Joe Perchesa9a79df2011-04-15 15:51:59 -07004007 ata_link_err(link, "COMRESET failed (errno=%d)\n", rc);
Tejun Heo0cbf0712008-05-19 01:15:05 +09004008 }
Tejun Heob6103f62006-11-01 17:59:53 +09004009 DPRINTK("EXIT, rc=%d\n", rc);
4010 return rc;
4011}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01004012EXPORT_SYMBOL_GPL(sata_link_hardreset);
Tejun Heob6103f62006-11-01 17:59:53 +09004013
4014/**
Tejun Heo57c9efd2008-04-07 22:47:19 +09004015 * sata_std_hardreset - COMRESET w/o waiting or classification
4016 * @link: link to reset
4017 * @class: resulting class of attached device
4018 * @deadline: deadline jiffies for the operation
4019 *
4020 * Standard SATA COMRESET w/o waiting or classification.
4021 *
4022 * LOCKING:
4023 * Kernel thread context (may sleep)
4024 *
4025 * RETURNS:
4026 * 0 if link offline, -EAGAIN if link online, -errno on errors.
4027 */
4028int sata_std_hardreset(struct ata_link *link, unsigned int *class,
4029 unsigned long deadline)
4030{
4031 const unsigned long *timing = sata_ehc_deb_timing(&link->eh_context);
4032 bool online;
4033 int rc;
4034
4035 /* do hardreset */
4036 rc = sata_link_hardreset(link, timing, deadline, &online, NULL);
Tejun Heo57c9efd2008-04-07 22:47:19 +09004037 return online ? -EAGAIN : rc;
4038}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01004039EXPORT_SYMBOL_GPL(sata_std_hardreset);
Tejun Heo57c9efd2008-04-07 22:47:19 +09004040
4041/**
Tejun Heo203c75b2008-04-07 22:47:18 +09004042 * ata_std_postreset - standard postreset callback
Tejun Heocc0680a2007-08-06 18:36:23 +09004043 * @link: the target ata_link
Tejun Heoc2bd5802006-01-24 17:05:22 +09004044 * @classes: classes of attached devices
4045 *
4046 * This function is invoked after a successful reset. Note that
4047 * the device might have been reset more than once using
4048 * different reset methods before postreset is invoked.
Tejun Heoc2bd5802006-01-24 17:05:22 +09004049 *
Tejun Heoc2bd5802006-01-24 17:05:22 +09004050 * LOCKING:
4051 * Kernel thread context (may sleep)
4052 */
Tejun Heo203c75b2008-04-07 22:47:18 +09004053void ata_std_postreset(struct ata_link *link, unsigned int *classes)
Tejun Heoc2bd5802006-01-24 17:05:22 +09004054{
Tejun Heof0465192008-05-19 01:15:08 +09004055 u32 serror;
4056
Tejun Heoc2bd5802006-01-24 17:05:22 +09004057 DPRINTK("ENTER\n");
4058
Tejun Heof0465192008-05-19 01:15:08 +09004059 /* reset complete, clear SError */
4060 if (!sata_scr_read(link, SCR_ERROR, &serror))
4061 sata_scr_write(link, SCR_ERROR, serror);
4062
Tejun Heoc2bd5802006-01-24 17:05:22 +09004063 /* print link status */
Tejun Heo936fd732007-08-06 18:36:23 +09004064 sata_print_link_status(link);
Tejun Heoc2bd5802006-01-24 17:05:22 +09004065
Tejun Heoc2bd5802006-01-24 17:05:22 +09004066 DPRINTK("EXIT\n");
4067}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01004068EXPORT_SYMBOL_GPL(ata_std_postreset);
Tejun Heoc2bd5802006-01-24 17:05:22 +09004069
Tejun Heoa62c0fc2006-01-24 17:05:22 +09004070/**
Tejun Heo623a3122006-03-05 17:55:58 +09004071 * ata_dev_same_device - Determine whether new ID matches configured device
Tejun Heo623a3122006-03-05 17:55:58 +09004072 * @dev: device to compare against
4073 * @new_class: class of the new device
4074 * @new_id: IDENTIFY page of the new device
4075 *
4076 * Compare @new_class and @new_id against @dev and determine
4077 * whether @dev is the device indicated by @new_class and
4078 * @new_id.
4079 *
4080 * LOCKING:
4081 * None.
4082 *
4083 * RETURNS:
4084 * 1 if @dev matches @new_class and @new_id, 0 otherwise.
4085 */
Tejun Heo3373efd2006-05-15 20:57:53 +09004086static int ata_dev_same_device(struct ata_device *dev, unsigned int new_class,
4087 const u16 *new_id)
Tejun Heo623a3122006-03-05 17:55:58 +09004088{
4089 const u16 *old_id = dev->id;
Tejun Heoa0cf7332007-01-02 20:18:49 +09004090 unsigned char model[2][ATA_ID_PROD_LEN + 1];
4091 unsigned char serial[2][ATA_ID_SERNO_LEN + 1];
Tejun Heo623a3122006-03-05 17:55:58 +09004092
4093 if (dev->class != new_class) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07004094 ata_dev_info(dev, "class mismatch %d != %d\n",
4095 dev->class, new_class);
Tejun Heo623a3122006-03-05 17:55:58 +09004096 return 0;
4097 }
4098
Tejun Heoa0cf7332007-01-02 20:18:49 +09004099 ata_id_c_string(old_id, model[0], ATA_ID_PROD, sizeof(model[0]));
4100 ata_id_c_string(new_id, model[1], ATA_ID_PROD, sizeof(model[1]));
4101 ata_id_c_string(old_id, serial[0], ATA_ID_SERNO, sizeof(serial[0]));
4102 ata_id_c_string(new_id, serial[1], ATA_ID_SERNO, sizeof(serial[1]));
Tejun Heo623a3122006-03-05 17:55:58 +09004103
4104 if (strcmp(model[0], model[1])) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07004105 ata_dev_info(dev, "model number mismatch '%s' != '%s'\n",
4106 model[0], model[1]);
Tejun Heo623a3122006-03-05 17:55:58 +09004107 return 0;
4108 }
4109
4110 if (strcmp(serial[0], serial[1])) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07004111 ata_dev_info(dev, "serial number mismatch '%s' != '%s'\n",
4112 serial[0], serial[1]);
Tejun Heo623a3122006-03-05 17:55:58 +09004113 return 0;
4114 }
4115
Tejun Heo623a3122006-03-05 17:55:58 +09004116 return 1;
4117}
4118
4119/**
Tejun Heofe309112007-05-15 03:28:15 +09004120 * ata_dev_reread_id - Re-read IDENTIFY data
Henrik Kretzschmar3fae4502007-06-19 10:10:50 +02004121 * @dev: target ATA device
Tejun Heobff04642006-11-10 18:08:10 +09004122 * @readid_flags: read ID flags
Tejun Heo623a3122006-03-05 17:55:58 +09004123 *
4124 * Re-read IDENTIFY page and make sure @dev is still attached to
4125 * the port.
4126 *
4127 * LOCKING:
4128 * Kernel thread context (may sleep)
4129 *
4130 * RETURNS:
4131 * 0 on success, negative errno otherwise
4132 */
Tejun Heofe309112007-05-15 03:28:15 +09004133int ata_dev_reread_id(struct ata_device *dev, unsigned int readid_flags)
Tejun Heo623a3122006-03-05 17:55:58 +09004134{
Tejun Heo5eb45c02006-04-02 18:51:52 +09004135 unsigned int class = dev->class;
Tejun Heo9af5c9c2007-08-06 18:36:22 +09004136 u16 *id = (void *)dev->link->ap->sector_buf;
Tejun Heo623a3122006-03-05 17:55:58 +09004137 int rc;
4138
Tejun Heofe635c72006-05-15 20:57:35 +09004139 /* read ID data */
Tejun Heobff04642006-11-10 18:08:10 +09004140 rc = ata_dev_read_id(dev, &class, readid_flags, id);
Tejun Heo623a3122006-03-05 17:55:58 +09004141 if (rc)
Tejun Heofe309112007-05-15 03:28:15 +09004142 return rc;
Tejun Heo623a3122006-03-05 17:55:58 +09004143
4144 /* is the device still there? */
Tejun Heofe309112007-05-15 03:28:15 +09004145 if (!ata_dev_same_device(dev, class, id))
4146 return -ENODEV;
Tejun Heo623a3122006-03-05 17:55:58 +09004147
Tejun Heofe635c72006-05-15 20:57:35 +09004148 memcpy(dev->id, id, sizeof(id[0]) * ATA_ID_WORDS);
Tejun Heofe309112007-05-15 03:28:15 +09004149 return 0;
4150}
4151
4152/**
4153 * ata_dev_revalidate - Revalidate ATA device
4154 * @dev: device to revalidate
Tejun Heo422c9da2007-09-23 13:14:12 +09004155 * @new_class: new class code
Tejun Heofe309112007-05-15 03:28:15 +09004156 * @readid_flags: read ID flags
4157 *
4158 * Re-read IDENTIFY page, make sure @dev is still attached to the
4159 * port and reconfigure it according to the new IDENTIFY page.
4160 *
4161 * LOCKING:
4162 * Kernel thread context (may sleep)
4163 *
4164 * RETURNS:
4165 * 0 on success, negative errno otherwise
4166 */
Tejun Heo422c9da2007-09-23 13:14:12 +09004167int ata_dev_revalidate(struct ata_device *dev, unsigned int new_class,
4168 unsigned int readid_flags)
Tejun Heofe309112007-05-15 03:28:15 +09004169{
Tejun Heo6ddcd3b2007-05-15 03:28:15 +09004170 u64 n_sectors = dev->n_sectors;
Tejun Heo5920dad2009-07-15 17:11:41 +09004171 u64 n_native_sectors = dev->n_native_sectors;
Tejun Heofe309112007-05-15 03:28:15 +09004172 int rc;
4173
4174 if (!ata_dev_enabled(dev))
4175 return -ENODEV;
4176
Tejun Heo422c9da2007-09-23 13:14:12 +09004177 /* fail early if !ATA && !ATAPI to avoid issuing [P]IDENTIFY to PMP */
4178 if (ata_class_enabled(new_class) &&
Borislav Petkovf0d06132009-04-26 16:33:34 +02004179 new_class != ATA_DEV_ATA &&
4180 new_class != ATA_DEV_ATAPI &&
Hannes Reinecke9162c652014-11-05 13:08:21 +01004181 new_class != ATA_DEV_ZAC &&
Borislav Petkovf0d06132009-04-26 16:33:34 +02004182 new_class != ATA_DEV_SEMB) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07004183 ata_dev_info(dev, "class mismatch %u != %u\n",
4184 dev->class, new_class);
Tejun Heo422c9da2007-09-23 13:14:12 +09004185 rc = -ENODEV;
4186 goto fail;
4187 }
4188
Tejun Heofe309112007-05-15 03:28:15 +09004189 /* re-read ID */
4190 rc = ata_dev_reread_id(dev, readid_flags);
4191 if (rc)
4192 goto fail;
Tejun Heo623a3122006-03-05 17:55:58 +09004193
4194 /* configure device according to the new ID */
Tejun Heoefdaedc2006-11-01 18:38:52 +09004195 rc = ata_dev_configure(dev);
Tejun Heo6ddcd3b2007-05-15 03:28:15 +09004196 if (rc)
4197 goto fail;
4198
4199 /* verify n_sectors hasn't changed */
Tejun Heo445d2112010-04-05 10:33:13 +09004200 if (dev->class != ATA_DEV_ATA || !n_sectors ||
4201 dev->n_sectors == n_sectors)
4202 return 0;
4203
4204 /* n_sectors has changed */
Joe Perchesa9a79df2011-04-15 15:51:59 -07004205 ata_dev_warn(dev, "n_sectors mismatch %llu != %llu\n",
4206 (unsigned long long)n_sectors,
4207 (unsigned long long)dev->n_sectors);
Tejun Heo445d2112010-04-05 10:33:13 +09004208
4209 /*
4210 * Something could have caused HPA to be unlocked
4211 * involuntarily. If n_native_sectors hasn't changed and the
4212 * new size matches it, keep the device.
4213 */
4214 if (dev->n_native_sectors == n_native_sectors &&
4215 dev->n_sectors > n_sectors && dev->n_sectors == n_native_sectors) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07004216 ata_dev_warn(dev,
4217 "new n_sectors matches native, probably "
4218 "late HPA unlock, n_sectors updated\n");
Tejun Heo68939ce2010-05-15 20:09:33 +02004219 /* use the larger n_sectors */
Tejun Heo445d2112010-04-05 10:33:13 +09004220 return 0;
Tejun Heo6ddcd3b2007-05-15 03:28:15 +09004221 }
4222
Tejun Heo445d2112010-04-05 10:33:13 +09004223 /*
4224 * Some BIOSes boot w/o HPA but resume w/ HPA locked. Try
4225 * unlocking HPA in those cases.
4226 *
4227 * https://bugzilla.kernel.org/show_bug.cgi?id=15396
4228 */
4229 if (dev->n_native_sectors == n_native_sectors &&
4230 dev->n_sectors < n_sectors && n_sectors == n_native_sectors &&
4231 !(dev->horkage & ATA_HORKAGE_BROKEN_HPA)) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07004232 ata_dev_warn(dev,
4233 "old n_sectors matches native, probably "
4234 "late HPA lock, will try to unlock HPA\n");
Tejun Heo445d2112010-04-05 10:33:13 +09004235 /* try unlocking HPA */
4236 dev->flags |= ATA_DFLAG_UNLOCK_HPA;
4237 rc = -EIO;
4238 } else
4239 rc = -ENODEV;
Tejun Heo623a3122006-03-05 17:55:58 +09004240
Tejun Heo445d2112010-04-05 10:33:13 +09004241 /* restore original n_[native_]sectors and fail */
4242 dev->n_native_sectors = n_native_sectors;
4243 dev->n_sectors = n_sectors;
Tejun Heo623a3122006-03-05 17:55:58 +09004244 fail:
Joe Perchesa9a79df2011-04-15 15:51:59 -07004245 ata_dev_err(dev, "revalidation failed (errno=%d)\n", rc);
Tejun Heo623a3122006-03-05 17:55:58 +09004246 return rc;
4247}
4248
Alan Cox6919a0a2006-10-27 19:08:46 -07004249struct ata_blacklist_entry {
4250 const char *model_num;
4251 const char *model_rev;
4252 unsigned long horkage;
4253};
4254
4255static const struct ata_blacklist_entry ata_device_blacklist [] = {
4256 /* Devices with DMA related problems under Linux */
4257 { "WDC AC11000H", NULL, ATA_HORKAGE_NODMA },
4258 { "WDC AC22100H", NULL, ATA_HORKAGE_NODMA },
4259 { "WDC AC32500H", NULL, ATA_HORKAGE_NODMA },
4260 { "WDC AC33100H", NULL, ATA_HORKAGE_NODMA },
4261 { "WDC AC31600H", NULL, ATA_HORKAGE_NODMA },
4262 { "WDC AC32100H", "24.09P07", ATA_HORKAGE_NODMA },
4263 { "WDC AC23200L", "21.10N21", ATA_HORKAGE_NODMA },
4264 { "Compaq CRD-8241B", NULL, ATA_HORKAGE_NODMA },
4265 { "CRD-8400B", NULL, ATA_HORKAGE_NODMA },
Mark Lord7da4c932010-07-01 18:18:12 -04004266 { "CRD-848[02]B", NULL, ATA_HORKAGE_NODMA },
Alan Cox6919a0a2006-10-27 19:08:46 -07004267 { "CRD-84", NULL, ATA_HORKAGE_NODMA },
4268 { "SanDisk SDP3B", NULL, ATA_HORKAGE_NODMA },
4269 { "SanDisk SDP3B-64", NULL, ATA_HORKAGE_NODMA },
4270 { "SANYO CD-ROM CRD", NULL, ATA_HORKAGE_NODMA },
4271 { "HITACHI CDR-8", NULL, ATA_HORKAGE_NODMA },
Mark Lord7da4c932010-07-01 18:18:12 -04004272 { "HITACHI CDR-8[34]35",NULL, ATA_HORKAGE_NODMA },
Alan Cox6919a0a2006-10-27 19:08:46 -07004273 { "Toshiba CD-ROM XM-6202B", NULL, ATA_HORKAGE_NODMA },
4274 { "TOSHIBA CD-ROM XM-1702BC", NULL, ATA_HORKAGE_NODMA },
4275 { "CD-532E-A", NULL, ATA_HORKAGE_NODMA },
4276 { "E-IDE CD-ROM CR-840",NULL, ATA_HORKAGE_NODMA },
4277 { "CD-ROM Drive/F5A", NULL, ATA_HORKAGE_NODMA },
4278 { "WPI CDD-820", NULL, ATA_HORKAGE_NODMA },
4279 { "SAMSUNG CD-ROM SC-148C", NULL, ATA_HORKAGE_NODMA },
4280 { "SAMSUNG CD-ROM SC", NULL, ATA_HORKAGE_NODMA },
Alan Cox6919a0a2006-10-27 19:08:46 -07004281 { "ATAPI CD-ROM DRIVE 40X MAXIMUM",NULL,ATA_HORKAGE_NODMA },
4282 { "_NEC DV5800A", NULL, ATA_HORKAGE_NODMA },
Jeff Garzik2dcb4072007-10-19 06:42:56 -04004283 { "SAMSUNG CD-ROM SN-124", "N001", ATA_HORKAGE_NODMA },
Dave Jones39f19882007-05-21 14:31:03 -04004284 { "Seagate STT20000A", NULL, ATA_HORKAGE_NODMA },
Prarit Bhargavad17d7942012-08-23 15:11:52 -04004285 { " 2GB ATA Flash Disk", "ADMA428M", ATA_HORKAGE_NODMA },
Shikha Jainb00622f2016-01-11 14:55:45 -08004286 { "VRFDFC22048UCHC-TE*", NULL, ATA_HORKAGE_NODMA },
Tejun Heo3af9a772007-09-23 13:19:54 +09004287 /* Odd clown on sil3726/4726 PMPs */
Tejun Heo50af2fa2008-05-19 01:15:14 +09004288 { "Config Disk", NULL, ATA_HORKAGE_DISABLE },
Alan Cox6919a0a2006-10-27 19:08:46 -07004289
Albert Lee18d6e9d2007-04-02 11:34:15 +08004290 /* Weird ATAPI devices */
Tejun Heo40a1d532007-06-27 02:49:38 +09004291 { "TORiSAN DVD-ROM DRD-N216", NULL, ATA_HORKAGE_MAX_SEC_128 },
Tejun Heo6a87e422008-11-03 19:01:09 +09004292 { "QUANTUM DAT DAT72-000", NULL, ATA_HORKAGE_ATAPI_MOD16_DMA },
Shan Haia32450e2013-03-18 10:30:44 +08004293 { "Slimtype DVD A DS8A8SH", NULL, ATA_HORKAGE_MAX_SEC_LBA48 },
Shan Hai0523f032013-10-28 16:08:01 +08004294 { "Slimtype DVD A DS8A9SH", NULL, ATA_HORKAGE_MAX_SEC_LBA48 },
Albert Lee18d6e9d2007-04-02 11:34:15 +08004295
David Milburnaf34d632015-07-13 11:48:23 -05004296 /*
4297 * Causes silent data corruption with higher max sects.
4298 * http://lkml.kernel.org/g/x49wpy40ysk.fsf@segfault.boston.devel.redhat.com
4299 */
4300 { "ST380013AS", "3.20", ATA_HORKAGE_MAX_SEC_1024 },
4301
Tejun Heo1488a1e2016-07-18 18:40:00 -04004302 /*
Tejun Heoe0edc8c2017-01-06 11:48:50 -05004303 * These devices time out with higher max sects.
Tejun Heo1488a1e2016-07-18 18:40:00 -04004304 * https://bugzilla.kernel.org/show_bug.cgi?id=121671
4305 */
Tejun Heoe0edc8c2017-01-06 11:48:50 -05004306 { "LITEON CX1-JB*-HP", NULL, ATA_HORKAGE_MAX_SEC_1024 },
Xinyu Lindb5ff902017-12-17 20:13:39 +08004307 { "LITEON EP1-*", NULL, ATA_HORKAGE_MAX_SEC_1024 },
Tejun Heo1488a1e2016-07-18 18:40:00 -04004308
Alan Cox6919a0a2006-10-27 19:08:46 -07004309 /* Devices we expect to fail diagnostics */
4310
4311 /* Devices where NCQ should be avoided */
4312 /* NCQ is slow */
Jeff Garzik2dcb4072007-10-19 06:42:56 -04004313 { "WDC WD740ADFD-00", NULL, ATA_HORKAGE_NONCQ },
Tejun Heo459ad682007-12-07 12:46:23 +09004314 { "WDC WD740ADFD-00NLR1", NULL, ATA_HORKAGE_NONCQ, },
Tejun Heo09125ea2007-02-28 15:21:23 +09004315 /* http://thread.gmane.org/gmane.linux.ide/14907 */
4316 { "FUJITSU MHT2060BH", NULL, ATA_HORKAGE_NONCQ },
Paul Rolland7acfaf32007-03-26 21:43:44 -08004317 /* NCQ is broken */
Jeff Garzik539cc7c2007-09-20 16:31:47 -04004318 { "Maxtor *", "BANC*", ATA_HORKAGE_NONCQ },
Alan Cox0e3dbc02007-09-20 15:22:47 +01004319 { "Maxtor 7V300F0", "VA111630", ATA_HORKAGE_NONCQ },
Paolo Ornatida6f0ec2007-10-04 11:06:56 +09004320 { "ST380817AS", "3.42", ATA_HORKAGE_NONCQ },
Tejun Heoe41bd3e2007-12-09 19:45:39 +09004321 { "ST3160023AS", "3.42", ATA_HORKAGE_NONCQ },
Lubomir Bulej5ccfca92008-12-22 11:35:22 +01004322 { "OCZ CORE_SSD", "02.10104", ATA_HORKAGE_NONCQ },
Jeff Garzik539cc7c2007-09-20 16:31:47 -04004323
Tejun Heoac70a962008-11-27 13:36:48 +09004324 /* Seagate NCQ + FLUSH CACHE firmware bug */
Mark Lord4d1f9082010-07-05 18:53:37 -04004325 { "ST31500341AS", "SD1[5-9]", ATA_HORKAGE_NONCQ |
Tejun Heoac70a962008-11-27 13:36:48 +09004326 ATA_HORKAGE_FIRMWARE_WARN },
Tejun Heod10d4912008-12-11 13:42:42 +09004327
Mark Lord4d1f9082010-07-05 18:53:37 -04004328 { "ST31000333AS", "SD1[5-9]", ATA_HORKAGE_NONCQ |
Tejun Heod10d4912008-12-11 13:42:42 +09004329 ATA_HORKAGE_FIRMWARE_WARN },
4330
Mark Lord4d1f9082010-07-05 18:53:37 -04004331 { "ST3640[36]23AS", "SD1[5-9]", ATA_HORKAGE_NONCQ |
Tejun Heod10d4912008-12-11 13:42:42 +09004332 ATA_HORKAGE_FIRMWARE_WARN },
4333
Mark Lord4d1f9082010-07-05 18:53:37 -04004334 { "ST3320[68]13AS", "SD1[5-9]", ATA_HORKAGE_NONCQ |
Tejun Heoac70a962008-11-27 13:36:48 +09004335 ATA_HORKAGE_FIRMWARE_WARN },
4336
Hans de Goede31f62642019-06-11 16:32:59 +02004337 /* drives which fail FPDMA_AA activation (some may freeze afterwards)
4338 the ST disks also have LPM issues */
Hans de Goede8756a252019-07-01 18:58:13 +02004339 { "ST1000LM024 HN-M101MBB", NULL, ATA_HORKAGE_BROKEN_FPDMA_AA |
Hans de Goede31f62642019-06-11 16:32:59 +02004340 ATA_HORKAGE_NOLPM, },
Aleksei Mamlin08c85d22015-07-01 13:48:30 +03004341 { "VB0250EAVER", "HPG7", ATA_HORKAGE_BROKEN_FPDMA_AA },
Michele Baldessari87809942013-11-25 19:00:14 +00004342
Robert Hancock36e337d2007-04-02 22:05:29 -06004343 /* Blacklist entries taken from Silicon Image 3124/3132
4344 Windows driver .inf file - also several Linux problem reports */
4345 { "HTS541060G9SA00", "MB3OC60D", ATA_HORKAGE_NONCQ, },
4346 { "HTS541080G9SA00", "MB4OC60D", ATA_HORKAGE_NONCQ, },
4347 { "HTS541010G9SA00", "MBZOC60D", ATA_HORKAGE_NONCQ, },
Alan Cox6919a0a2006-10-27 19:08:46 -07004348
Tejun Heo68b0ddb2010-04-05 10:51:26 +09004349 /* https://bugzilla.kernel.org/show_bug.cgi?id=15573 */
4350 { "C300-CTFDDAC128MAG", "0001", ATA_HORKAGE_NONCQ, },
4351
Tejun Heo322579d2018-05-08 14:21:56 -07004352 /* Some Sandisk SSDs lock up hard with NCQ enabled. Reported on
4353 SD7SN6S256G and SD8SN8U256G */
4354 { "SanDisk SD[78]SN*G", NULL, ATA_HORKAGE_NONCQ, },
4355
Tejun Heo16c55b02007-08-29 11:58:33 +09004356 /* devices which puke on READ_NATIVE_MAX */
4357 { "HDS724040KLSA80", "KFAOA20N", ATA_HORKAGE_BROKEN_HPA, },
4358 { "WDC WD3200JD-00KLB0", "WD-WCAMR1130137", ATA_HORKAGE_BROKEN_HPA },
4359 { "WDC WD2500JD-00HBB0", "WD-WMAL71490727", ATA_HORKAGE_BROKEN_HPA },
4360 { "MAXTOR 6L080L4", "A93.0500", ATA_HORKAGE_BROKEN_HPA },
Alan Cox6919a0a2006-10-27 19:08:46 -07004361
Tejun Heo7831387b2009-08-07 01:59:15 +09004362 /* this one allows HPA unlocking but fails IOs on the area */
4363 { "OCZ-VERTEX", "1.30", ATA_HORKAGE_BROKEN_HPA },
4364
Alan Cox93328e112007-09-29 04:06:48 -04004365 /* Devices which report 1 sector over size HPA */
4366 { "ST340823A", NULL, ATA_HORKAGE_HPA_SIZE, },
4367 { "ST320413A", NULL, ATA_HORKAGE_HPA_SIZE, },
Mikko Rapelib152fcd2008-02-19 01:41:25 +01004368 { "ST310211A", NULL, ATA_HORKAGE_HPA_SIZE, },
Alan Cox93328e112007-09-29 04:06:48 -04004369
Alan Cox6bbfd532007-11-05 22:58:58 +00004370 /* Devices which get the IVB wrong */
4371 { "QUANTUM FIREBALLlct10 05", "A03.0900", ATA_HORKAGE_IVB, },
Alan Coxa79067e2008-04-29 14:08:36 +01004372 /* Maybe we should just blacklist TSSTcorp... */
Mark Lord7da4c932010-07-01 18:18:12 -04004373 { "TSSTcorp CDDVDW SH-S202[HJN]", "SB0[01]", ATA_HORKAGE_IVB, },
Alan Cox6bbfd532007-11-05 22:58:58 +00004374
Jens Axboe9ce8e302008-08-27 15:23:18 +02004375 /* Devices that do not need bridging limits applied */
4376 { "MTRON MSP-SATA*", NULL, ATA_HORKAGE_BRIDGE_OK, },
Jeff Garzik04d0f1b82012-08-17 13:36:59 -04004377 { "BUFFALO HD-QSU2/R5", NULL, ATA_HORKAGE_BRIDGE_OK, },
Jens Axboe9ce8e302008-08-27 15:23:18 +02004378
Tejun Heo90627122009-01-29 20:31:36 +09004379 /* Devices which aren't very happy with higher link speeds */
4380 { "WD My Book", NULL, ATA_HORKAGE_1_5_GBPS, },
Daniel J Bluemanc5310772012-07-23 12:22:37 +08004381 { "Seagate FreeAgent GoFlex", NULL, ATA_HORKAGE_1_5_GBPS, },
Tejun Heo90627122009-01-29 20:31:36 +09004382
Tejun Heod0cb43b2009-07-09 09:27:50 +09004383 /*
4384 * Devices which choke on SETXFER. Applies only if both the
4385 * device and controller are SATA.
4386 */
Tejun Heocd691872011-06-22 12:13:10 +02004387 { "PIONEER DVD-RW DVRTD08", NULL, ATA_HORKAGE_NOSETXFER },
Vladimir LAVALLADE3a251792012-01-08 13:50:13 +01004388 { "PIONEER DVD-RW DVRTD08A", NULL, ATA_HORKAGE_NOSETXFER },
4389 { "PIONEER DVD-RW DVR-215", NULL, ATA_HORKAGE_NOSETXFER },
Tejun Heocd691872011-06-22 12:13:10 +02004390 { "PIONEER DVD-RW DVR-212D", NULL, ATA_HORKAGE_NOSETXFER },
4391 { "PIONEER DVD-RW DVR-216D", NULL, ATA_HORKAGE_NOSETXFER },
Tejun Heod0cb43b2009-07-09 09:27:50 +09004392
Kai-Heng Fengb17e57292018-02-18 22:17:09 +08004393 /* Crucial BX100 SSD 500GB has broken LPM support */
Hans de Goede3bf7b5d2018-03-19 16:33:59 +01004394 { "CT500BX100SSD1", NULL, ATA_HORKAGE_NOLPM },
Kai-Heng Fengb17e57292018-02-18 22:17:09 +08004395
Hans de Goeded418ff52018-03-19 16:34:00 +01004396 /* 512GB MX100 with MU01 firmware has both queued TRIM and LPM issues */
4397 { "Crucial_CT512MX100*", "MU01", ATA_HORKAGE_NO_NCQ_TRIM |
Hans de Goede9c7be59f2018-02-16 10:48:20 +01004398 ATA_HORKAGE_ZERO_AFTER_TRIM |
4399 ATA_HORKAGE_NOLPM, },
Hans de Goeded418ff52018-03-19 16:34:00 +01004400 /* 512GB MX100 with newer firmware has only LPM issues */
4401 { "Crucial_CT512MX100*", NULL, ATA_HORKAGE_ZERO_AFTER_TRIM |
4402 ATA_HORKAGE_NOLPM, },
Hans de Goede9c7be59f2018-02-16 10:48:20 +01004403
Hans de Goede62ac3f72018-03-19 16:33:58 +01004404 /* 480GB+ M500 SSDs have both queued TRIM and LPM issues */
4405 { "Crucial_CT480M500*", NULL, ATA_HORKAGE_NO_NCQ_TRIM |
4406 ATA_HORKAGE_ZERO_AFTER_TRIM |
4407 ATA_HORKAGE_NOLPM, },
4408 { "Crucial_CT960M500*", NULL, ATA_HORKAGE_NO_NCQ_TRIM |
4409 ATA_HORKAGE_ZERO_AFTER_TRIM |
4410 ATA_HORKAGE_NOLPM, },
4411
François Cami76936e92018-05-13 20:11:15 +02004412 /* These specific Samsung models/firmware-revs do not handle LPM well */
Hans de Goedeb5b4d3a2018-04-24 11:19:07 +02004413 { "SAMSUNG MZMPC128HBFU-000MV", "CXM14M1Q", ATA_HORKAGE_NOLPM, },
François Cami76936e92018-05-13 20:11:15 +02004414 { "SAMSUNG SSD PM830 mSATA *", "CXM13D1Q", ATA_HORKAGE_NOLPM, },
Diego Viola410b5c72018-11-12 17:22:52 -02004415 { "SAMSUNG MZ7TD256HAFV-000L9", NULL, ATA_HORKAGE_NOLPM, },
Hans de Goededd957492019-02-03 10:02:07 +01004416 { "SAMSUNG MZ7TE512HMHP-000L1", "EXT06L0Q", ATA_HORKAGE_NOLPM, },
Hans de Goedeb5b4d3a2018-04-24 11:19:07 +02004417
Marc Carinof78dea02013-12-16 18:15:53 -08004418 /* devices that don't properly handle queued TRIM commands */
Sudip Mukherjee136d7692018-05-19 22:29:36 +01004419 { "Micron_M500IT_*", "MU01", ATA_HORKAGE_NO_NCQ_TRIM |
4420 ATA_HORKAGE_ZERO_AFTER_TRIM, },
Martin K. Petersen243918b2015-06-18 14:50:18 -04004421 { "Micron_M500_*", NULL, ATA_HORKAGE_NO_NCQ_TRIM |
Martin K. Petersene61f7d12015-01-08 10:34:27 -05004422 ATA_HORKAGE_ZERO_AFTER_TRIM, },
Martin K. Petersenff7f53f2015-03-27 15:17:20 -04004423 { "Crucial_CT*M500*", NULL, ATA_HORKAGE_NO_NCQ_TRIM |
4424 ATA_HORKAGE_ZERO_AFTER_TRIM, },
Martin K. Petersen9051bd32015-07-15 21:03:23 -04004425 { "Micron_M5[15]0_*", "MU01", ATA_HORKAGE_NO_NCQ_TRIM |
Martin K. Petersenff7f53f2015-03-27 15:17:20 -04004426 ATA_HORKAGE_ZERO_AFTER_TRIM, },
4427 { "Crucial_CT*M550*", "MU01", ATA_HORKAGE_NO_NCQ_TRIM |
4428 ATA_HORKAGE_ZERO_AFTER_TRIM, },
4429 { "Crucial_CT*MX100*", "MU01", ATA_HORKAGE_NO_NCQ_TRIM |
4430 ATA_HORKAGE_ZERO_AFTER_TRIM, },
Ju Hyung Parkca6bfcb2018-03-11 02:28:35 +09004431 { "Samsung SSD 840*", NULL, ATA_HORKAGE_NO_NCQ_TRIM |
4432 ATA_HORKAGE_ZERO_AFTER_TRIM, },
4433 { "Samsung SSD 850*", NULL, ATA_HORKAGE_NO_NCQ_TRIM |
Martin K. Petersen6fc4d972015-03-27 15:17:21 -04004434 ATA_HORKAGE_ZERO_AFTER_TRIM, },
Guillermo A. Amaral7a7184b2015-08-25 23:29:13 -07004435 { "FCCT*M500*", NULL, ATA_HORKAGE_NO_NCQ_TRIM |
4436 ATA_HORKAGE_ZERO_AFTER_TRIM, },
Martin K. Petersene61f7d12015-01-08 10:34:27 -05004437
Arne Fitzenreitercda57b12015-07-15 13:54:37 +02004438 /* devices that don't properly handle TRIM commands */
4439 { "SuperSSpeed S238*", NULL, ATA_HORKAGE_NOTRIM, },
4440
Martin K. Petersene61f7d12015-01-08 10:34:27 -05004441 /*
4442 * As defined, the DRAT (Deterministic Read After Trim) and RZAT
4443 * (Return Zero After Trim) flags in the ATA Command Set are
4444 * unreliable in the sense that they only define what happens if
4445 * the device successfully executed the DSM TRIM command. TRIM
4446 * is only advisory, however, and the device is free to silently
4447 * ignore all or parts of the request.
4448 *
4449 * Whitelist drives that are known to reliably return zeroes
4450 * after TRIM.
4451 */
4452
4453 /*
4454 * The intel 510 drive has buggy DRAT/RZAT. Explicitly exclude
4455 * that model before whitelisting all other intel SSDs.
4456 */
4457 { "INTEL*SSDSC2MH*", NULL, 0, },
4458
Martin K. Petersenff7f53f2015-03-27 15:17:20 -04004459 { "Micron*", NULL, ATA_HORKAGE_ZERO_AFTER_TRIM, },
4460 { "Crucial*", NULL, ATA_HORKAGE_ZERO_AFTER_TRIM, },
Martin K. Petersene61f7d12015-01-08 10:34:27 -05004461 { "INTEL*SSD*", NULL, ATA_HORKAGE_ZERO_AFTER_TRIM, },
4462 { "SSD*INTEL*", NULL, ATA_HORKAGE_ZERO_AFTER_TRIM, },
4463 { "Samsung*SSD*", NULL, ATA_HORKAGE_ZERO_AFTER_TRIM, },
4464 { "SAMSUNG*SSD*", NULL, ATA_HORKAGE_ZERO_AFTER_TRIM, },
Juha-Matti Tillifd6f32f2018-12-02 12:47:08 +02004465 { "SAMSUNG*MZ7KM*", NULL, ATA_HORKAGE_ZERO_AFTER_TRIM, },
Martin K. Petersene61f7d12015-01-08 10:34:27 -05004466 { "ST[1248][0248]0[FH]*", NULL, ATA_HORKAGE_ZERO_AFTER_TRIM, },
Marc Carinof78dea02013-12-16 18:15:53 -08004467
Tejun Heoecd75ad2014-01-16 09:47:17 -05004468 /*
4469 * Some WD SATA-I drives spin up and down erratically when the link
4470 * is put into the slumber mode. We don't have full list of the
4471 * affected devices. Disable LPM if the device matches one of the
4472 * known prefixes and is SATA-1. As a side effect LPM partial is
4473 * lost too.
4474 *
4475 * https://bugzilla.kernel.org/show_bug.cgi?id=57211
4476 */
4477 { "WDC WD800JD-*", NULL, ATA_HORKAGE_WD_BROKEN_LPM },
4478 { "WDC WD1200JD-*", NULL, ATA_HORKAGE_WD_BROKEN_LPM },
4479 { "WDC WD1600JD-*", NULL, ATA_HORKAGE_WD_BROKEN_LPM },
4480 { "WDC WD2000JD-*", NULL, ATA_HORKAGE_WD_BROKEN_LPM },
4481 { "WDC WD2500JD-*", NULL, ATA_HORKAGE_WD_BROKEN_LPM },
4482 { "WDC WD3000JD-*", NULL, ATA_HORKAGE_WD_BROKEN_LPM },
4483 { "WDC WD3200JD-*", NULL, ATA_HORKAGE_WD_BROKEN_LPM },
4484
Alan Cox6919a0a2006-10-27 19:08:46 -07004485 /* End Marker */
4486 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004487};
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05004488
Tejun Heo75683fe2007-07-05 13:31:27 +09004489static unsigned long ata_dev_blacklisted(const struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004490{
Tejun Heo8bfa79f2007-01-02 20:19:40 +09004491 unsigned char model_num[ATA_ID_PROD_LEN + 1];
4492 unsigned char model_rev[ATA_ID_FW_REV_LEN + 1];
Alan Cox6919a0a2006-10-27 19:08:46 -07004493 const struct ata_blacklist_entry *ad = ata_device_blacklist;
Albert Lee3a778272006-06-22 13:00:25 +08004494
Tejun Heo8bfa79f2007-01-02 20:19:40 +09004495 ata_id_c_string(dev->id, model_num, ATA_ID_PROD, sizeof(model_num));
4496 ata_id_c_string(dev->id, model_rev, ATA_ID_FW_REV, sizeof(model_rev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004497
Alan Cox6919a0a2006-10-27 19:08:46 -07004498 while (ad->model_num) {
George Spelvin1c402792014-10-07 07:26:38 -04004499 if (glob_match(ad->model_num, model_num)) {
Alan Cox6919a0a2006-10-27 19:08:46 -07004500 if (ad->model_rev == NULL)
4501 return ad->horkage;
George Spelvin1c402792014-10-07 07:26:38 -04004502 if (glob_match(ad->model_rev, model_rev))
Alan Cox6919a0a2006-10-27 19:08:46 -07004503 return ad->horkage;
Alan Coxf4b15fe2006-03-22 15:54:04 +00004504 }
Alan Cox6919a0a2006-10-27 19:08:46 -07004505 ad++;
Alan Coxf4b15fe2006-03-22 15:54:04 +00004506 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004507 return 0;
4508}
4509
Alan Cox6919a0a2006-10-27 19:08:46 -07004510static int ata_dma_blacklisted(const struct ata_device *dev)
4511{
4512 /* We don't support polling DMA.
4513 * DMA blacklist those ATAPI devices with CDB-intr (and use PIO)
4514 * if the LLDD handles only interrupts in the HSM_ST_LAST state.
4515 */
Tejun Heo9af5c9c2007-08-06 18:36:22 +09004516 if ((dev->link->ap->flags & ATA_FLAG_PIO_POLLING) &&
Alan Cox6919a0a2006-10-27 19:08:46 -07004517 (dev->flags & ATA_DFLAG_CDB_INTR))
4518 return 1;
Tejun Heo75683fe2007-07-05 13:31:27 +09004519 return (dev->horkage & ATA_HORKAGE_NODMA) ? 1 : 0;
Alan Cox6919a0a2006-10-27 19:08:46 -07004520}
4521
Tejun Heoa6d5a512006-03-06 04:31:57 +09004522/**
Alan Cox6bbfd532007-11-05 22:58:58 +00004523 * ata_is_40wire - check drive side detection
4524 * @dev: device
4525 *
4526 * Perform drive side detection decoding, allowing for device vendors
4527 * who can't follow the documentation.
4528 */
4529
4530static int ata_is_40wire(struct ata_device *dev)
4531{
4532 if (dev->horkage & ATA_HORKAGE_IVB)
4533 return ata_drive_40wire_relaxed(dev->id);
4534 return ata_drive_40wire(dev->id);
4535}
4536
4537/**
Alan Cox15a55512008-03-28 14:33:46 -07004538 * cable_is_40wire - 40/80/SATA decider
4539 * @ap: port to consider
4540 *
4541 * This function encapsulates the policy for speed management
4542 * in one place. At the moment we don't cache the result but
4543 * there is a good case for setting ap->cbl to the result when
4544 * we are called with unknown cables (and figuring out if it
4545 * impacts hotplug at all).
4546 *
4547 * Return 1 if the cable appears to be 40 wire.
4548 */
4549
4550static int cable_is_40wire(struct ata_port *ap)
4551{
4552 struct ata_link *link;
4553 struct ata_device *dev;
4554
Tejun Heo4a9c7b32008-10-27 19:59:23 +09004555 /* If the controller thinks we are 40 wire, we are. */
Alan Cox15a55512008-03-28 14:33:46 -07004556 if (ap->cbl == ATA_CBL_PATA40)
4557 return 1;
Tejun Heo4a9c7b32008-10-27 19:59:23 +09004558
4559 /* If the controller thinks we are 80 wire, we are. */
Alan Cox15a55512008-03-28 14:33:46 -07004560 if (ap->cbl == ATA_CBL_PATA80 || ap->cbl == ATA_CBL_SATA)
4561 return 0;
Tejun Heo4a9c7b32008-10-27 19:59:23 +09004562
4563 /* If the system is known to be 40 wire short cable (eg
4564 * laptop), then we allow 80 wire modes even if the drive
4565 * isn't sure.
4566 */
Alan Coxf7920682008-04-08 16:37:50 +01004567 if (ap->cbl == ATA_CBL_PATA40_SHORT)
4568 return 0;
Alan Cox15a55512008-03-28 14:33:46 -07004569
Tejun Heo4a9c7b32008-10-27 19:59:23 +09004570 /* If the controller doesn't know, we scan.
4571 *
4572 * Note: We look for all 40 wire detects at this point. Any
4573 * 80 wire detect is taken to be 80 wire cable because
4574 * - in many setups only the one drive (slave if present) will
4575 * give a valid detect
4576 * - if you have a non detect capable drive you don't want it
4577 * to colour the choice
4578 */
Tejun Heo1eca4362008-11-03 20:03:17 +09004579 ata_for_each_link(link, ap, EDGE) {
4580 ata_for_each_dev(dev, link, ENABLED) {
4581 if (!ata_is_40wire(dev))
Alan Cox15a55512008-03-28 14:33:46 -07004582 return 0;
4583 }
4584 }
4585 return 1;
4586}
4587
4588/**
Tejun Heoa6d5a512006-03-06 04:31:57 +09004589 * ata_dev_xfermask - Compute supported xfermask of the given device
Tejun Heoa6d5a512006-03-06 04:31:57 +09004590 * @dev: Device to compute xfermask for
4591 *
Tejun Heoacf356b2006-03-24 14:07:50 +09004592 * Compute supported xfermask of @dev and store it in
4593 * dev->*_mask. This function is responsible for applying all
4594 * known limits including host controller limits, device
4595 * blacklist, etc...
Tejun Heoa6d5a512006-03-06 04:31:57 +09004596 *
4597 * LOCKING:
4598 * None.
Tejun Heoa6d5a512006-03-06 04:31:57 +09004599 */
Tejun Heo3373efd2006-05-15 20:57:53 +09004600static void ata_dev_xfermask(struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004601{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09004602 struct ata_link *link = dev->link;
4603 struct ata_port *ap = link->ap;
Jeff Garzikcca39742006-08-24 03:19:22 -04004604 struct ata_host *host = ap->host;
Tejun Heoa6d5a512006-03-06 04:31:57 +09004605 unsigned long xfer_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004606
Tejun Heo37deecb2006-08-10 16:59:07 +09004607 /* controller modes available */
Tejun Heo565083e2006-04-02 17:54:47 +09004608 xfer_mask = ata_pack_xfermask(ap->pio_mask,
4609 ap->mwdma_mask, ap->udma_mask);
4610
Robert Hancock8343f882007-03-06 02:37:51 -08004611 /* drive modes available */
Tejun Heo37deecb2006-08-10 16:59:07 +09004612 xfer_mask &= ata_pack_xfermask(dev->pio_mask,
4613 dev->mwdma_mask, dev->udma_mask);
4614 xfer_mask &= ata_id_xfermask(dev->id);
Tejun Heo565083e2006-04-02 17:54:47 +09004615
Alan Coxb352e572006-08-10 18:52:12 +01004616 /*
4617 * CFA Advanced TrueIDE timings are not allowed on a shared
4618 * cable
4619 */
4620 if (ata_dev_pair(dev)) {
4621 /* No PIO5 or PIO6 */
4622 xfer_mask &= ~(0x03 << (ATA_SHIFT_PIO + 5));
4623 /* No MWDMA3 or MWDMA 4 */
4624 xfer_mask &= ~(0x03 << (ATA_SHIFT_MWDMA + 3));
4625 }
4626
Tejun Heo37deecb2006-08-10 16:59:07 +09004627 if (ata_dma_blacklisted(dev)) {
4628 xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
Joe Perchesa9a79df2011-04-15 15:51:59 -07004629 ata_dev_warn(dev,
4630 "device is on DMA blacklist, disabling DMA\n");
Tejun Heo37deecb2006-08-10 16:59:07 +09004631 }
Tejun Heoa6d5a512006-03-06 04:31:57 +09004632
Petr Vandrovec14d66ab2007-03-08 10:12:12 +01004633 if ((host->flags & ATA_HOST_SIMPLEX) &&
Jeff Garzik2dcb4072007-10-19 06:42:56 -04004634 host->simplex_claimed && host->simplex_claimed != ap) {
Tejun Heo37deecb2006-08-10 16:59:07 +09004635 xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
Joe Perchesa9a79df2011-04-15 15:51:59 -07004636 ata_dev_warn(dev,
4637 "simplex DMA is claimed by other device, disabling DMA\n");
Alan Cox5444a6f2006-03-27 18:58:20 +01004638 }
Tejun Heo565083e2006-04-02 17:54:47 +09004639
Jeff Garzike4246752007-03-09 09:56:46 -05004640 if (ap->flags & ATA_FLAG_NO_IORDY)
4641 xfer_mask &= ata_pio_mask_no_iordy(dev);
4642
Alan Cox5444a6f2006-03-27 18:58:20 +01004643 if (ap->ops->mode_filter)
Alan Coxa76b62ca2007-03-09 09:34:07 -05004644 xfer_mask = ap->ops->mode_filter(dev, xfer_mask);
Alan Cox5444a6f2006-03-27 18:58:20 +01004645
Robert Hancock8343f882007-03-06 02:37:51 -08004646 /* Apply cable rule here. Don't apply it early because when
4647 * we handle hot plug the cable type can itself change.
4648 * Check this last so that we know if the transfer rate was
4649 * solely limited by the cable.
4650 * Unknown or 80 wire cables reported host side are checked
4651 * drive side as well. Cases where we know a 40wire cable
4652 * is used safely for 80 are not checked here.
4653 */
4654 if (xfer_mask & (0xF8 << ATA_SHIFT_UDMA))
4655 /* UDMA/44 or higher would be available */
Alan Cox15a55512008-03-28 14:33:46 -07004656 if (cable_is_40wire(ap)) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07004657 ata_dev_warn(dev,
4658 "limited to UDMA/33 due to 40-wire cable\n");
Robert Hancock8343f882007-03-06 02:37:51 -08004659 xfer_mask &= ~(0xF8 << ATA_SHIFT_UDMA);
4660 }
4661
Tejun Heo565083e2006-04-02 17:54:47 +09004662 ata_unpack_xfermask(xfer_mask, &dev->pio_mask,
4663 &dev->mwdma_mask, &dev->udma_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004664}
4665
Linus Torvalds1da177e2005-04-16 15:20:36 -07004666/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004667 * ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
Linus Torvalds1da177e2005-04-16 15:20:36 -07004668 * @dev: Device to which command will be sent
4669 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04004670 * Issue SET FEATURES - XFER MODE command to device @dev
4671 * on port @ap.
4672 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004673 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004674 * PCI/etc. bus probe sem.
Tejun Heo83206a22006-03-24 15:25:31 +09004675 *
4676 * RETURNS:
4677 * 0 on success, AC_ERR_* mask otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004678 */
4679
Tejun Heo3373efd2006-05-15 20:57:53 +09004680static unsigned int ata_dev_set_xfermode(struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004681{
Tejun Heoa0123702005-12-13 14:49:31 +09004682 struct ata_taskfile tf;
Tejun Heo83206a22006-03-24 15:25:31 +09004683 unsigned int err_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004684
4685 /* set up set-features taskfile */
4686 DPRINTK("set features - xfer mode\n");
4687
Tejun Heo464cf172007-05-27 15:10:40 +02004688 /* Some controllers and ATAPI devices show flaky interrupt
4689 * behavior after setting xfer mode. Use polling instead.
4690 */
Tejun Heo3373efd2006-05-15 20:57:53 +09004691 ata_tf_init(dev, &tf);
Tejun Heoa0123702005-12-13 14:49:31 +09004692 tf.command = ATA_CMD_SET_FEATURES;
4693 tf.feature = SETFEATURES_XFER;
Tejun Heo464cf172007-05-27 15:10:40 +02004694 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE | ATA_TFLAG_POLLING;
Tejun Heoa0123702005-12-13 14:49:31 +09004695 tf.protocol = ATA_PROT_NODATA;
Alan Coxb9f8ab22007-11-19 14:33:11 +00004696 /* If we are using IORDY we must send the mode setting command */
Jeff Garzik11b7bec2007-11-23 21:12:14 -05004697 if (ata_pio_need_iordy(dev))
4698 tf.nsect = dev->xfer_mode;
Alan Coxb9f8ab22007-11-19 14:33:11 +00004699 /* If the device has IORDY and the controller does not - turn it off */
4700 else if (ata_id_has_iordy(dev->id))
Jeff Garzik11b7bec2007-11-23 21:12:14 -05004701 tf.nsect = 0x01;
Alan Coxb9f8ab22007-11-19 14:33:11 +00004702 else /* In the ancient relic department - skip all of this */
4703 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004704
Mikulas Patockad531be22015-07-08 13:06:12 -04004705 /* On some disks, this command causes spin-up, so we need longer timeout */
4706 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 15000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004707
Tejun Heo83206a22006-03-24 15:25:31 +09004708 DPRINTK("EXIT, err_mask=%x\n", err_mask);
4709 return err_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004710}
Tejun Heo1152b262010-09-01 17:50:05 +02004711
Linus Torvalds1da177e2005-04-16 15:20:36 -07004712/**
Jeff Garzik218f3d32007-10-25 00:33:27 -04004713 * ata_dev_set_feature - Issue SET FEATURES - SATA FEATURES
Kristen Carlson Accardi9f45cbd2007-08-15 03:57:11 -04004714 * @dev: Device to which command will be sent
4715 * @enable: Whether to enable or disable the feature
Jeff Garzik218f3d32007-10-25 00:33:27 -04004716 * @feature: The sector count represents the feature to set
Kristen Carlson Accardi9f45cbd2007-08-15 03:57:11 -04004717 *
4718 * Issue SET FEATURES - SATA FEATURES command to device @dev
Jeff Garzik218f3d32007-10-25 00:33:27 -04004719 * on port @ap with sector count
Kristen Carlson Accardi9f45cbd2007-08-15 03:57:11 -04004720 *
4721 * LOCKING:
4722 * PCI/etc. bus probe sem.
4723 *
4724 * RETURNS:
4725 * 0 on success, AC_ERR_* mask otherwise.
4726 */
Tejun Heo1152b262010-09-01 17:50:05 +02004727unsigned int ata_dev_set_feature(struct ata_device *dev, u8 enable, u8 feature)
Kristen Carlson Accardi9f45cbd2007-08-15 03:57:11 -04004728{
4729 struct ata_taskfile tf;
4730 unsigned int err_mask;
Damien Le Moal974e0a42016-04-04 12:17:09 -04004731 unsigned long timeout = 0;
Kristen Carlson Accardi9f45cbd2007-08-15 03:57:11 -04004732
4733 /* set up set-features taskfile */
4734 DPRINTK("set features - SATA features\n");
4735
4736 ata_tf_init(dev, &tf);
4737 tf.command = ATA_CMD_SET_FEATURES;
4738 tf.feature = enable;
4739 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
4740 tf.protocol = ATA_PROT_NODATA;
Jeff Garzik218f3d32007-10-25 00:33:27 -04004741 tf.nsect = feature;
Kristen Carlson Accardi9f45cbd2007-08-15 03:57:11 -04004742
Damien Le Moal974e0a42016-04-04 12:17:09 -04004743 if (enable == SETFEATURES_SPINUP)
4744 timeout = ata_probe_timeout ?
4745 ata_probe_timeout * 1000 : SETFEATURES_SPINUP_TIMEOUT;
4746 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, timeout);
Kristen Carlson Accardi9f45cbd2007-08-15 03:57:11 -04004747
4748 DPRINTK("EXIT, err_mask=%x\n", err_mask);
4749 return err_mask;
4750}
Jeff Garzik633de4c2012-09-13 01:12:29 -04004751EXPORT_SYMBOL_GPL(ata_dev_set_feature);
Kristen Carlson Accardi9f45cbd2007-08-15 03:57:11 -04004752
4753/**
Albert Lee8bf62ece2005-05-12 15:29:42 -04004754 * ata_dev_init_params - Issue INIT DEV PARAMS command
Albert Lee8bf62ece2005-05-12 15:29:42 -04004755 * @dev: Device to which command will be sent
Randy Dunlape2a7f772006-05-18 10:50:18 -07004756 * @heads: Number of heads (taskfile parameter)
4757 * @sectors: Number of sectors (taskfile parameter)
Albert Lee8bf62ece2005-05-12 15:29:42 -04004758 *
4759 * LOCKING:
Tejun Heo6aff8f12006-02-15 18:24:09 +09004760 * Kernel thread context (may sleep)
4761 *
4762 * RETURNS:
4763 * 0 on success, AC_ERR_* mask otherwise.
Albert Lee8bf62ece2005-05-12 15:29:42 -04004764 */
Tejun Heo3373efd2006-05-15 20:57:53 +09004765static unsigned int ata_dev_init_params(struct ata_device *dev,
4766 u16 heads, u16 sectors)
Albert Lee8bf62ece2005-05-12 15:29:42 -04004767{
Tejun Heoa0123702005-12-13 14:49:31 +09004768 struct ata_taskfile tf;
Tejun Heo6aff8f12006-02-15 18:24:09 +09004769 unsigned int err_mask;
Albert Lee8bf62ece2005-05-12 15:29:42 -04004770
4771 /* Number of sectors per track 1-255. Number of heads 1-16 */
4772 if (sectors < 1 || sectors > 255 || heads < 1 || heads > 16)
Albert Lee00b6f5e2006-03-27 16:39:18 +08004773 return AC_ERR_INVALID;
Albert Lee8bf62ece2005-05-12 15:29:42 -04004774
4775 /* set up init dev params taskfile */
4776 DPRINTK("init dev params \n");
4777
Tejun Heo3373efd2006-05-15 20:57:53 +09004778 ata_tf_init(dev, &tf);
Tejun Heoa0123702005-12-13 14:49:31 +09004779 tf.command = ATA_CMD_INIT_DEV_PARAMS;
4780 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
4781 tf.protocol = ATA_PROT_NODATA;
4782 tf.nsect = sectors;
4783 tf.device |= (heads - 1) & 0x0f; /* max head = num. of heads - 1 */
Albert Lee8bf62ece2005-05-12 15:29:42 -04004784
Tejun Heo2b789102007-10-09 15:05:44 +09004785 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
Alan Cox18b24662007-08-08 14:28:49 +01004786 /* A clean abort indicates an original or just out of spec drive
4787 and we should continue as we issue the setup based on the
4788 drive reported working geometry */
4789 if (err_mask == AC_ERR_DEV && (tf.feature & ATA_ABORTED))
4790 err_mask = 0;
Albert Lee8bf62ece2005-05-12 15:29:42 -04004791
Tejun Heo6aff8f12006-02-15 18:24:09 +09004792 DPRINTK("EXIT, err_mask=%x\n", err_mask);
4793 return err_mask;
Albert Lee8bf62ece2005-05-12 15:29:42 -04004794}
4795
4796/**
Tejun Heo5895ef92008-06-17 12:36:26 +09004797 * atapi_check_dma - Check whether ATAPI DMA can be supported
Linus Torvalds1da177e2005-04-16 15:20:36 -07004798 * @qc: Metadata associated with taskfile to check
4799 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04004800 * Allow low-level driver to filter ATA PACKET commands, returning
4801 * a status indicating whether or not it is OK to use DMA for the
4802 * supplied PACKET command.
4803 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004804 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04004805 * spin_lock_irqsave(host lock)
Jeff Garzik0cba6322005-05-30 19:49:12 -04004806 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004807 * RETURNS: 0 when ATAPI DMA can be used
4808 * nonzero otherwise
4809 */
Tejun Heo5895ef92008-06-17 12:36:26 +09004810int atapi_check_dma(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004811{
4812 struct ata_port *ap = qc->ap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004813
Tejun Heob9a41972007-06-27 02:48:43 +09004814 /* Don't allow DMA if it isn't multiple of 16 bytes. Quite a
4815 * few ATAPI devices choke on such DMA requests.
4816 */
Tejun Heo6a87e422008-11-03 19:01:09 +09004817 if (!(qc->dev->horkage & ATA_HORKAGE_ATAPI_MOD16_DMA) &&
4818 unlikely(qc->nbytes & 15))
Tejun Heob9a41972007-06-27 02:48:43 +09004819 return 1;
Albert Lee6f23a312007-04-02 11:39:25 +08004820
Linus Torvalds1da177e2005-04-16 15:20:36 -07004821 if (ap->ops->check_atapi_dma)
Tejun Heob9a41972007-06-27 02:48:43 +09004822 return ap->ops->check_atapi_dma(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004823
Tejun Heob9a41972007-06-27 02:48:43 +09004824 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004825}
Tejun Heob9a41972007-06-27 02:48:43 +09004826
Linus Torvalds1da177e2005-04-16 15:20:36 -07004827/**
Tejun Heo31cc23b2007-09-23 13:14:12 +09004828 * ata_std_qc_defer - Check whether a qc needs to be deferred
4829 * @qc: ATA command in question
4830 *
4831 * Non-NCQ commands cannot run with any other command, NCQ or
4832 * not. As upper layer only knows the queue depth, we are
4833 * responsible for maintaining exclusion. This function checks
4834 * whether a new command @qc can be issued.
4835 *
4836 * LOCKING:
4837 * spin_lock_irqsave(host lock)
4838 *
4839 * RETURNS:
4840 * ATA_DEFER_* if deferring is needed, 0 otherwise.
4841 */
4842int ata_std_qc_defer(struct ata_queued_cmd *qc)
4843{
4844 struct ata_link *link = qc->dev->link;
4845
Hannes Reinecke179b3102016-07-14 09:05:43 +09004846 if (ata_is_ncq(qc->tf.protocol)) {
Tejun Heo31cc23b2007-09-23 13:14:12 +09004847 if (!ata_tag_valid(link->active_tag))
4848 return 0;
4849 } else {
4850 if (!ata_tag_valid(link->active_tag) && !link->sactive)
4851 return 0;
4852 }
4853
4854 return ATA_DEFER_LINK;
4855}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01004856EXPORT_SYMBOL_GPL(ata_std_qc_defer);
Tejun Heo31cc23b2007-09-23 13:14:12 +09004857
Jiri Slaby95364f32019-10-31 10:59:45 +01004858enum ata_completion_errors ata_noop_qc_prep(struct ata_queued_cmd *qc)
4859{
4860 return AC_ERR_OK;
4861}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01004862EXPORT_SYMBOL_GPL(ata_noop_qc_prep);
Brian Kinge46834c2006-03-17 17:04:03 -06004863
Jeff Garzik0cba6322005-05-30 19:49:12 -04004864/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04004865 * ata_sg_init - Associate command with scatter-gather table.
4866 * @qc: Command to be associated
4867 * @sg: Scatter-gather table.
4868 * @n_elem: Number of elements in s/g table.
4869 *
4870 * Initialize the data-related elements of queued_cmd @qc
4871 * to point to a scatter-gather table @sg, containing @n_elem
4872 * elements.
4873 *
4874 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04004875 * spin_lock_irqsave(host lock)
Jeff Garzik0cba6322005-05-30 19:49:12 -04004876 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004877void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
4878 unsigned int n_elem)
4879{
Tejun Heoff2aeb12007-12-05 16:43:11 +09004880 qc->sg = sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004881 qc->n_elem = n_elem;
Tejun Heoff2aeb12007-12-05 16:43:11 +09004882 qc->cursg = qc->sg;
4883}
4884
Geert Uytterhoeven2874d5e2017-01-03 19:09:49 +01004885#ifdef CONFIG_HAS_DMA
4886
4887/**
4888 * ata_sg_clean - Unmap DMA memory associated with command
4889 * @qc: Command containing DMA memory to be released
4890 *
4891 * Unmap all mapped DMA memory associated with this command.
4892 *
4893 * LOCKING:
4894 * spin_lock_irqsave(host lock)
4895 */
Jason Yanaf27e012017-03-10 10:05:40 +08004896static void ata_sg_clean(struct ata_queued_cmd *qc)
Geert Uytterhoeven2874d5e2017-01-03 19:09:49 +01004897{
4898 struct ata_port *ap = qc->ap;
4899 struct scatterlist *sg = qc->sg;
4900 int dir = qc->dma_dir;
4901
4902 WARN_ON_ONCE(sg == NULL);
4903
4904 VPRINTK("unmapping %u sg elements\n", qc->n_elem);
4905
4906 if (qc->n_elem)
4907 dma_unmap_sg(ap->dev, sg, qc->orig_n_elem, dir);
4908
4909 qc->flags &= ~ATA_QCFLAG_DMAMAP;
4910 qc->sg = NULL;
4911}
4912
Linus Torvalds1da177e2005-04-16 15:20:36 -07004913/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04004914 * ata_sg_setup - DMA-map the scatter-gather table associated with a command.
4915 * @qc: Command with scatter-gather table to be mapped.
4916 *
4917 * DMA-map the scatter-gather table associated with queued_cmd @qc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004918 *
4919 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04004920 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004921 *
4922 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004923 * Zero on success, negative on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004924 *
4925 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004926static int ata_sg_setup(struct ata_queued_cmd *qc)
4927{
4928 struct ata_port *ap = qc->ap;
James Bottomleydde20202008-02-19 11:36:56 +01004929 unsigned int n_elem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004930
Tejun Heo44877b42007-02-21 01:06:51 +09004931 VPRINTK("ENTER, ata%u\n", ap->print_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004932
James Bottomleydde20202008-02-19 11:36:56 +01004933 n_elem = dma_map_sg(ap->dev, qc->sg, qc->n_elem, qc->dma_dir);
4934 if (n_elem < 1)
4935 return -1;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004936
Paul Menzelbb376672018-07-08 09:18:21 +02004937 VPRINTK("%d sg elements mapped\n", n_elem);
FUJITA Tomonori58256272009-02-27 17:35:43 +09004938 qc->orig_n_elem = qc->n_elem;
James Bottomleydde20202008-02-19 11:36:56 +01004939 qc->n_elem = n_elem;
Tejun Heof92a2632007-12-05 16:43:10 +09004940 qc->flags |= ATA_QCFLAG_DMAMAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004941
4942 return 0;
4943}
4944
Geert Uytterhoeven2874d5e2017-01-03 19:09:49 +01004945#else /* !CONFIG_HAS_DMA */
4946
4947static inline void ata_sg_clean(struct ata_queued_cmd *qc) {}
4948static inline int ata_sg_setup(struct ata_queued_cmd *qc) { return -1; }
4949
4950#endif /* !CONFIG_HAS_DMA */
4951
Linus Torvalds1da177e2005-04-16 15:20:36 -07004952/**
Randy Dunlapc893a3a2006-01-28 13:15:32 -05004953 * swap_buf_le16 - swap halves of 16-bit words in place
Edward Falk0baab862005-06-02 18:17:13 -04004954 * @buf: Buffer to swap
4955 * @buf_words: Number of 16-bit words in buffer.
4956 *
4957 * Swap halves of 16-bit words if needed to convert from
4958 * little-endian byte order to native cpu byte order, or
4959 * vice-versa.
4960 *
4961 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04004962 * Inherited from caller.
Edward Falk0baab862005-06-02 18:17:13 -04004963 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004964void swap_buf_le16(u16 *buf, unsigned int buf_words)
4965{
4966#ifdef __BIG_ENDIAN
4967 unsigned int i;
4968
4969 for (i = 0; i < buf_words; i++)
4970 buf[i] = le16_to_cpu(buf[i]);
4971#endif /* __BIG_ENDIAN */
4972}
4973
Albert Lee6ae4cfb2005-08-12 14:15:34 +08004974/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004975 * ata_qc_new_init - Request an available ATA command, and initialize it
Linus Torvalds1da177e2005-04-16 15:20:36 -07004976 * @dev: Device from whom we request an available command structure
Randy Dunlap38755e82015-08-09 18:37:36 -07004977 * @tag: tag
Linus Torvalds1da177e2005-04-16 15:20:36 -07004978 *
4979 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004980 * None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004981 */
4982
Shaohua Li98bd4be2015-01-23 19:52:07 -08004983struct ata_queued_cmd *ata_qc_new_init(struct ata_device *dev, int tag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004984{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09004985 struct ata_port *ap = dev->link->ap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004986 struct ata_queued_cmd *qc;
4987
Shaohua Li98bd4be2015-01-23 19:52:07 -08004988 /* no command while frozen */
4989 if (unlikely(ap->pflags & ATA_PFLAG_FROZEN))
4990 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004991
Shaohua Li98bd4be2015-01-23 19:52:07 -08004992 /* libsas case */
Shaohua Li5067c042015-03-12 10:32:18 -07004993 if (ap->flags & ATA_FLAG_SAS_HOST) {
Shaohua Li98bd4be2015-01-23 19:52:07 -08004994 tag = ata_sas_allocate_tag(ap);
4995 if (tag < 0)
4996 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004997 }
4998
Shaohua Li98bd4be2015-01-23 19:52:07 -08004999 qc = __ata_qc_from_tag(ap, tag);
Jens Axboe5ac40792018-05-11 12:51:03 -06005000 qc->tag = qc->hw_tag = tag;
Shaohua Li98bd4be2015-01-23 19:52:07 -08005001 qc->scsicmd = NULL;
5002 qc->ap = ap;
5003 qc->dev = dev;
5004
5005 ata_qc_reinit(qc);
5006
Linus Torvalds1da177e2005-04-16 15:20:36 -07005007 return qc;
5008}
5009
Tejun Heo8a8bc222008-11-10 14:48:21 +09005010/**
5011 * ata_qc_free - free unused ata_queued_cmd
5012 * @qc: Command to complete
5013 *
5014 * Designed to free unused ata_queued_cmd object
5015 * in case something prevents using it.
5016 *
5017 * LOCKING:
5018 * spin_lock_irqsave(host lock)
5019 */
5020void ata_qc_free(struct ata_queued_cmd *qc)
5021{
Julia Lawalla1104012009-10-17 08:41:47 +02005022 struct ata_port *ap;
Tejun Heo8a8bc222008-11-10 14:48:21 +09005023 unsigned int tag;
5024
Tejun Heoefcb3cf2009-01-09 19:19:14 +09005025 WARN_ON_ONCE(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
Julia Lawalla1104012009-10-17 08:41:47 +02005026 ap = qc->ap;
Tejun Heo8a8bc222008-11-10 14:48:21 +09005027
5028 qc->flags = 0;
5029 tag = qc->tag;
Jens Axboe28361c42018-05-11 12:51:09 -06005030 if (ata_tag_valid(tag)) {
Tejun Heo8a8bc222008-11-10 14:48:21 +09005031 qc->tag = ATA_TAG_POISON;
Shaohua Li5067c042015-03-12 10:32:18 -07005032 if (ap->flags & ATA_FLAG_SAS_HOST)
Shaohua Li98bd4be2015-01-23 19:52:07 -08005033 ata_sas_free_tag(tag, ap);
Tejun Heo8a8bc222008-11-10 14:48:21 +09005034 }
5035}
5036
Tejun Heo76014422006-02-11 15:13:49 +09005037void __ata_qc_complete(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005038{
Julia Lawalla1104012009-10-17 08:41:47 +02005039 struct ata_port *ap;
5040 struct ata_link *link;
Tejun Heodedaf2b2006-05-15 21:03:43 +09005041
Tejun Heoefcb3cf2009-01-09 19:19:14 +09005042 WARN_ON_ONCE(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
5043 WARN_ON_ONCE(!(qc->flags & ATA_QCFLAG_ACTIVE));
Julia Lawalla1104012009-10-17 08:41:47 +02005044 ap = qc->ap;
5045 link = qc->dev->link;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005046
5047 if (likely(qc->flags & ATA_QCFLAG_DMAMAP))
5048 ata_sg_clean(qc);
5049
Tejun Heo7401abf2006-05-15 20:57:32 +09005050 /* command should be marked inactive atomically with qc completion */
Hannes Reinecke179b3102016-07-14 09:05:43 +09005051 if (ata_is_ncq(qc->tf.protocol)) {
Jens Axboe4e5b6262018-05-11 12:51:04 -06005052 link->sactive &= ~(1 << qc->hw_tag);
Tejun Heoda917d62007-09-23 13:14:12 +09005053 if (!link->sactive)
5054 ap->nr_active_links--;
5055 } else {
Tejun Heo9af5c9c2007-08-06 18:36:22 +09005056 link->active_tag = ATA_TAG_POISON;
Tejun Heoda917d62007-09-23 13:14:12 +09005057 ap->nr_active_links--;
5058 }
5059
5060 /* clear exclusive status */
5061 if (unlikely(qc->flags & ATA_QCFLAG_CLEAR_EXCL &&
5062 ap->excl_link == link))
5063 ap->excl_link = NULL;
Tejun Heo7401abf2006-05-15 20:57:32 +09005064
Albert Lee3f3791d2005-08-16 14:25:38 +08005065 /* atapi: mark qc as inactive to prevent the interrupt handler
5066 * from completing the command twice later, before the error handler
5067 * is called. (when rc != 0 and atapi request sense is needed)
5068 */
5069 qc->flags &= ~ATA_QCFLAG_ACTIVE;
Jens Axboee3ed89392018-05-11 12:51:05 -06005070 ap->qc_active &= ~(1ULL << qc->tag);
Albert Lee3f3791d2005-08-16 14:25:38 +08005071
Linus Torvalds1da177e2005-04-16 15:20:36 -07005072 /* call completion callback */
Tejun Heo77853bf2006-01-23 13:09:36 +09005073 qc->complete_fn(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005074}
5075
Tejun Heo39599a52006-11-14 22:37:35 +09005076static void fill_result_tf(struct ata_queued_cmd *qc)
5077{
5078 struct ata_port *ap = qc->ap;
5079
Tejun Heo39599a52006-11-14 22:37:35 +09005080 qc->result_tf.flags = qc->tf.flags;
Tejun Heo22183bf2008-04-07 22:47:20 +09005081 ap->ops->qc_fill_rtf(qc);
Tejun Heo39599a52006-11-14 22:37:35 +09005082}
5083
Tejun Heo00115e02007-11-27 19:28:58 +09005084static void ata_verify_xfer(struct ata_queued_cmd *qc)
5085{
5086 struct ata_device *dev = qc->dev;
5087
Christoph Hellwigeb0effd2016-07-16 22:16:41 +09005088 if (!ata_is_data(qc->tf.protocol))
Tejun Heo00115e02007-11-27 19:28:58 +09005089 return;
5090
5091 if ((dev->mwdma_mask || dev->udma_mask) && ata_is_pio(qc->tf.protocol))
5092 return;
5093
5094 dev->flags &= ~ATA_DFLAG_DUBIOUS_XFER;
5095}
5096
Tejun Heof686bcb2006-05-15 20:58:05 +09005097/**
5098 * ata_qc_complete - Complete an active ATA command
5099 * @qc: Command to complete
Tejun Heof686bcb2006-05-15 20:58:05 +09005100 *
Tejun Heo1aadf5c2010-06-25 15:03:34 +02005101 * Indicate to the mid and upper layers that an ATA command has
5102 * completed, with either an ok or not-ok status.
5103 *
5104 * Refrain from calling this function multiple times when
5105 * successfully completing multiple NCQ commands.
5106 * ata_qc_complete_multiple() should be used instead, which will
5107 * properly update IRQ expect state.
Tejun Heof686bcb2006-05-15 20:58:05 +09005108 *
5109 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04005110 * spin_lock_irqsave(host lock)
Tejun Heof686bcb2006-05-15 20:58:05 +09005111 */
5112void ata_qc_complete(struct ata_queued_cmd *qc)
5113{
5114 struct ata_port *ap = qc->ap;
5115
Stephan Linzeb25cb92016-06-10 07:59:56 +02005116 /* Trigger the LED (if available) */
Linus Walleijd1ed7c52018-02-24 23:45:56 +01005117 ledtrig_disk_activity(!!(qc->tf.flags & ATA_TFLAG_WRITE));
Stephan Linzeb25cb92016-06-10 07:59:56 +02005118
Tejun Heof686bcb2006-05-15 20:58:05 +09005119 /* XXX: New EH and old EH use different mechanisms to
5120 * synchronize EH with regular execution path.
5121 *
5122 * In new EH, a failed qc is marked with ATA_QCFLAG_FAILED.
5123 * Normal execution path is responsible for not accessing a
5124 * failed qc. libata core enforces the rule by returning NULL
5125 * from ata_qc_from_tag() for failed qcs.
5126 *
5127 * Old EH depends on ata_qc_complete() nullifying completion
5128 * requests if ATA_QCFLAG_EH_SCHEDULED is set. Old EH does
5129 * not synchronize with interrupt handler. Only PIO task is
5130 * taken care of.
5131 */
5132 if (ap->ops->error_handler) {
Tejun Heo4dbfa392007-10-25 18:22:44 +09005133 struct ata_device *dev = qc->dev;
5134 struct ata_eh_info *ehi = &dev->link->eh_info;
5135
Tejun Heof686bcb2006-05-15 20:58:05 +09005136 if (unlikely(qc->err_mask))
5137 qc->flags |= ATA_QCFLAG_FAILED;
5138
Tejun Heof08dc1a2010-12-09 15:59:32 +01005139 /*
5140 * Finish internal commands without any further processing
5141 * and always with the result TF filled.
5142 */
5143 if (unlikely(ata_tag_internal(qc->tag))) {
Tejun Heof4b31db92009-10-16 13:00:51 +09005144 fill_result_tf(qc);
Hannes Reinecke255c03d2015-03-27 16:46:38 +01005145 trace_ata_qc_complete_internal(qc);
Tejun Heof08dc1a2010-12-09 15:59:32 +01005146 __ata_qc_complete(qc);
5147 return;
5148 }
Tejun Heof4b31db92009-10-16 13:00:51 +09005149
Tejun Heof08dc1a2010-12-09 15:59:32 +01005150 /*
5151 * Non-internal qc has failed. Fill the result TF and
5152 * summon EH.
5153 */
5154 if (unlikely(qc->flags & ATA_QCFLAG_FAILED)) {
5155 fill_result_tf(qc);
Hannes Reinecke255c03d2015-03-27 16:46:38 +01005156 trace_ata_qc_complete_failed(qc);
Tejun Heof08dc1a2010-12-09 15:59:32 +01005157 ata_qc_schedule_eh(qc);
Tejun Heof4b31db92009-10-16 13:00:51 +09005158 return;
Tejun Heof686bcb2006-05-15 20:58:05 +09005159 }
5160
Tejun Heo4dc738e2009-09-17 18:45:27 +09005161 WARN_ON_ONCE(ap->pflags & ATA_PFLAG_FROZEN);
5162
Tejun Heof686bcb2006-05-15 20:58:05 +09005163 /* read result TF if requested */
5164 if (qc->flags & ATA_QCFLAG_RESULT_TF)
Tejun Heo39599a52006-11-14 22:37:35 +09005165 fill_result_tf(qc);
Tejun Heof686bcb2006-05-15 20:58:05 +09005166
Hannes Reinecke255c03d2015-03-27 16:46:38 +01005167 trace_ata_qc_complete_done(qc);
Tejun Heo4dbfa392007-10-25 18:22:44 +09005168 /* Some commands need post-processing after successful
5169 * completion.
5170 */
5171 switch (qc->tf.command) {
5172 case ATA_CMD_SET_FEATURES:
5173 if (qc->tf.feature != SETFEATURES_WC_ON &&
Tom Yan0c127352016-07-12 21:37:02 +08005174 qc->tf.feature != SETFEATURES_WC_OFF &&
5175 qc->tf.feature != SETFEATURES_RA_ON &&
5176 qc->tf.feature != SETFEATURES_RA_OFF)
Tejun Heo4dbfa392007-10-25 18:22:44 +09005177 break;
5178 /* fall through */
5179 case ATA_CMD_INIT_DEV_PARAMS: /* CHS translation changed */
5180 case ATA_CMD_SET_MULTI: /* multi_count changed */
5181 /* revalidate device */
5182 ehi->dev_action[dev->devno] |= ATA_EH_REVALIDATE;
5183 ata_port_schedule_eh(ap);
5184 break;
Tejun Heo054a5fb2007-10-25 18:30:36 +09005185
5186 case ATA_CMD_SLEEP:
5187 dev->flags |= ATA_DFLAG_SLEEPING;
5188 break;
Tejun Heo4dbfa392007-10-25 18:22:44 +09005189 }
5190
Tejun Heo00115e02007-11-27 19:28:58 +09005191 if (unlikely(dev->flags & ATA_DFLAG_DUBIOUS_XFER))
5192 ata_verify_xfer(qc);
5193
Tejun Heof686bcb2006-05-15 20:58:05 +09005194 __ata_qc_complete(qc);
5195 } else {
5196 if (qc->flags & ATA_QCFLAG_EH_SCHEDULED)
5197 return;
5198
5199 /* read result TF if failed or requested */
5200 if (qc->err_mask || qc->flags & ATA_QCFLAG_RESULT_TF)
Tejun Heo39599a52006-11-14 22:37:35 +09005201 fill_result_tf(qc);
Tejun Heof686bcb2006-05-15 20:58:05 +09005202
5203 __ata_qc_complete(qc);
5204 }
5205}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01005206EXPORT_SYMBOL_GPL(ata_qc_complete);
Tejun Heof686bcb2006-05-15 20:58:05 +09005207
Tejun Heodedaf2b2006-05-15 21:03:43 +09005208/**
Sascha Hauer8385d752019-12-13 09:04:08 +01005209 * ata_qc_get_active - get bitmask of active qcs
5210 * @ap: port in question
5211 *
5212 * LOCKING:
5213 * spin_lock_irqsave(host lock)
5214 *
5215 * RETURNS:
5216 * Bitmask of active qcs
5217 */
5218u64 ata_qc_get_active(struct ata_port *ap)
5219{
5220 u64 qc_active = ap->qc_active;
5221
5222 /* ATA_TAG_INTERNAL is sent to hw as tag 0 */
5223 if (qc_active & (1ULL << ATA_TAG_INTERNAL)) {
5224 qc_active |= (1 << 0);
5225 qc_active &= ~(1ULL << ATA_TAG_INTERNAL);
5226 }
5227
5228 return qc_active;
5229}
5230EXPORT_SYMBOL_GPL(ata_qc_get_active);
5231
5232/**
Tejun Heodedaf2b2006-05-15 21:03:43 +09005233 * ata_qc_complete_multiple - Complete multiple qcs successfully
5234 * @ap: port in question
5235 * @qc_active: new qc_active mask
Tejun Heodedaf2b2006-05-15 21:03:43 +09005236 *
5237 * Complete in-flight commands. This functions is meant to be
5238 * called from low-level driver's interrupt routine to complete
5239 * requests normally. ap->qc_active and @qc_active is compared
5240 * and commands are completed accordingly.
5241 *
Tejun Heo1aadf5c2010-06-25 15:03:34 +02005242 * Always use this function when completing multiple NCQ commands
5243 * from IRQ handlers instead of calling ata_qc_complete()
5244 * multiple times to keep IRQ expect status properly in sync.
5245 *
Tejun Heodedaf2b2006-05-15 21:03:43 +09005246 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04005247 * spin_lock_irqsave(host lock)
Tejun Heodedaf2b2006-05-15 21:03:43 +09005248 *
5249 * RETURNS:
5250 * Number of completed commands on success, -errno otherwise.
5251 */
Jens Axboee3ed89392018-05-11 12:51:05 -06005252int ata_qc_complete_multiple(struct ata_port *ap, u64 qc_active)
Tejun Heodedaf2b2006-05-15 21:03:43 +09005253{
Jens Axboe7ce5c8c2018-09-20 08:30:55 -06005254 u64 done_mask, ap_qc_active = ap->qc_active;
Tejun Heodedaf2b2006-05-15 21:03:43 +09005255 int nr_done = 0;
Tejun Heodedaf2b2006-05-15 21:03:43 +09005256
Jens Axboe7ce5c8c2018-09-20 08:30:55 -06005257 /*
5258 * If the internal tag is set on ap->qc_active, then we care about
5259 * bit0 on the passed in qc_active mask. Move that bit up to match
5260 * the internal tag.
5261 */
5262 if (ap_qc_active & (1ULL << ATA_TAG_INTERNAL)) {
5263 qc_active |= (qc_active & 0x01) << ATA_TAG_INTERNAL;
5264 qc_active ^= qc_active & 0x01;
5265 }
5266
5267 done_mask = ap_qc_active ^ qc_active;
Tejun Heodedaf2b2006-05-15 21:03:43 +09005268
5269 if (unlikely(done_mask & qc_active)) {
Jens Axboee3ed89392018-05-11 12:51:05 -06005270 ata_port_err(ap, "illegal qc_active transition (%08llx->%08llx)\n",
Joe Perchesa9a79df2011-04-15 15:51:59 -07005271 ap->qc_active, qc_active);
Tejun Heodedaf2b2006-05-15 21:03:43 +09005272 return -EINVAL;
5273 }
5274
Jens Axboe43768182009-05-20 09:44:39 +02005275 while (done_mask) {
Tejun Heodedaf2b2006-05-15 21:03:43 +09005276 struct ata_queued_cmd *qc;
Jens Axboee3ed89392018-05-11 12:51:05 -06005277 unsigned int tag = __ffs64(done_mask);
Tejun Heodedaf2b2006-05-15 21:03:43 +09005278
Jens Axboe43768182009-05-20 09:44:39 +02005279 qc = ata_qc_from_tag(ap, tag);
5280 if (qc) {
Tejun Heodedaf2b2006-05-15 21:03:43 +09005281 ata_qc_complete(qc);
5282 nr_done++;
5283 }
Jens Axboee3ed89392018-05-11 12:51:05 -06005284 done_mask &= ~(1ULL << tag);
Tejun Heodedaf2b2006-05-15 21:03:43 +09005285 }
5286
5287 return nr_done;
5288}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01005289EXPORT_SYMBOL_GPL(ata_qc_complete_multiple);
Tejun Heodedaf2b2006-05-15 21:03:43 +09005290
Linus Torvalds1da177e2005-04-16 15:20:36 -07005291/**
5292 * ata_qc_issue - issue taskfile to device
5293 * @qc: command to issue to device
5294 *
5295 * Prepare an ATA command to submission to device.
5296 * This includes mapping the data into a DMA-able
5297 * area, filling in the S/G table, and finally
5298 * writing the taskfile to hardware, starting the command.
5299 *
5300 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04005301 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005302 */
Tejun Heo8e0e6942006-03-31 20:41:11 +09005303void ata_qc_issue(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005304{
5305 struct ata_port *ap = qc->ap;
Tejun Heo9af5c9c2007-08-06 18:36:22 +09005306 struct ata_link *link = qc->dev->link;
Tejun Heo405e66b2007-11-27 19:28:53 +09005307 u8 prot = qc->tf.protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005308
Tejun Heodedaf2b2006-05-15 21:03:43 +09005309 /* Make sure only one non-NCQ command is outstanding. The
5310 * check is skipped for old EH because it reuses active qc to
5311 * request ATAPI sense.
5312 */
Tejun Heoefcb3cf2009-01-09 19:19:14 +09005313 WARN_ON_ONCE(ap->ops->error_handler && ata_tag_valid(link->active_tag));
Tejun Heodedaf2b2006-05-15 21:03:43 +09005314
Tejun Heo1973a022007-12-05 10:36:13 +09005315 if (ata_is_ncq(prot)) {
Jens Axboe4e5b6262018-05-11 12:51:04 -06005316 WARN_ON_ONCE(link->sactive & (1 << qc->hw_tag));
Tejun Heoda917d62007-09-23 13:14:12 +09005317
5318 if (!link->sactive)
5319 ap->nr_active_links++;
Jens Axboe4e5b6262018-05-11 12:51:04 -06005320 link->sactive |= 1 << qc->hw_tag;
Tejun Heodedaf2b2006-05-15 21:03:43 +09005321 } else {
Tejun Heoefcb3cf2009-01-09 19:19:14 +09005322 WARN_ON_ONCE(link->sactive);
Tejun Heoda917d62007-09-23 13:14:12 +09005323
5324 ap->nr_active_links++;
Tejun Heo9af5c9c2007-08-06 18:36:22 +09005325 link->active_tag = qc->tag;
Tejun Heodedaf2b2006-05-15 21:03:43 +09005326 }
5327
Tejun Heoe4a70e72006-03-31 20:36:47 +09005328 qc->flags |= ATA_QCFLAG_ACTIVE;
Jens Axboee3ed89392018-05-11 12:51:05 -06005329 ap->qc_active |= 1ULL << qc->tag;
Tejun Heoe4a70e72006-03-31 20:36:47 +09005330
Tejun Heo60f5d6e2010-08-23 11:27:27 +02005331 /*
5332 * We guarantee to LLDs that they will have at least one
Tejun Heof92a2632007-12-05 16:43:10 +09005333 * non-zero sg if the command is a data command.
5334 */
Eric Biggers9173e5e2018-02-03 20:33:27 -08005335 if (ata_is_data(prot) && (!qc->sg || !qc->n_elem || !qc->nbytes))
Tejun Heo60f5d6e2010-08-23 11:27:27 +02005336 goto sys_err;
Tejun Heof92a2632007-12-05 16:43:10 +09005337
Tejun Heo405e66b2007-11-27 19:28:53 +09005338 if (ata_is_dma(prot) || (ata_is_pio(prot) &&
Tejun Heof92a2632007-12-05 16:43:10 +09005339 (ap->flags & ATA_FLAG_PIO_DMA)))
Tejun Heo001102d2007-12-05 16:43:09 +09005340 if (ata_sg_setup(qc))
Tejun Heo60f5d6e2010-08-23 11:27:27 +02005341 goto sys_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005342
Tejun Heocf480622008-01-24 00:05:14 +09005343 /* if device is sleeping, schedule reset and abort the link */
Tejun Heo054a5fb2007-10-25 18:30:36 +09005344 if (unlikely(qc->dev->flags & ATA_DFLAG_SLEEPING)) {
Tejun Heocf480622008-01-24 00:05:14 +09005345 link->eh_info.action |= ATA_EH_RESET;
Tejun Heo054a5fb2007-10-25 18:30:36 +09005346 ata_ehi_push_desc(&link->eh_info, "waking up from sleep");
5347 ata_link_abort(link);
5348 return;
5349 }
5350
Jiri Slaby95364f32019-10-31 10:59:45 +01005351 qc->err_mask |= ap->ops->qc_prep(qc);
5352 if (unlikely(qc->err_mask))
5353 goto err;
Hannes Reinecke255c03d2015-03-27 16:46:38 +01005354 trace_ata_qc_issue(qc);
Tejun Heo8e0e6942006-03-31 20:41:11 +09005355 qc->err_mask |= ap->ops->qc_issue(qc);
5356 if (unlikely(qc->err_mask))
5357 goto err;
5358 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005359
Tejun Heo60f5d6e2010-08-23 11:27:27 +02005360sys_err:
Tejun Heo8e0e6942006-03-31 20:41:11 +09005361 qc->err_mask |= AC_ERR_SYSTEM;
5362err:
5363 ata_qc_complete(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005364}
5365
5366/**
Tejun Heo34bf2172006-05-15 20:57:46 +09005367 * sata_scr_valid - test whether SCRs are accessible
Tejun Heo936fd732007-08-06 18:36:23 +09005368 * @link: ATA link to test SCR accessibility for
Tejun Heo34bf2172006-05-15 20:57:46 +09005369 *
Tejun Heo936fd732007-08-06 18:36:23 +09005370 * Test whether SCRs are accessible for @link.
Tejun Heo34bf2172006-05-15 20:57:46 +09005371 *
5372 * LOCKING:
5373 * None.
5374 *
5375 * RETURNS:
5376 * 1 if SCRs are accessible, 0 otherwise.
5377 */
Tejun Heo936fd732007-08-06 18:36:23 +09005378int sata_scr_valid(struct ata_link *link)
Tejun Heo34bf2172006-05-15 20:57:46 +09005379{
Tejun Heo936fd732007-08-06 18:36:23 +09005380 struct ata_port *ap = link->ap;
5381
Tejun Heoa16abc02007-05-21 18:33:47 +02005382 return (ap->flags & ATA_FLAG_SATA) && ap->ops->scr_read;
Tejun Heo34bf2172006-05-15 20:57:46 +09005383}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01005384EXPORT_SYMBOL_GPL(sata_scr_valid);
Tejun Heo34bf2172006-05-15 20:57:46 +09005385
5386/**
5387 * sata_scr_read - read SCR register of the specified port
Tejun Heo936fd732007-08-06 18:36:23 +09005388 * @link: ATA link to read SCR for
Tejun Heo34bf2172006-05-15 20:57:46 +09005389 * @reg: SCR to read
5390 * @val: Place to store read value
5391 *
Tejun Heo936fd732007-08-06 18:36:23 +09005392 * Read SCR register @reg of @link into *@val. This function is
Tejun Heo633273a2007-09-23 13:19:54 +09005393 * guaranteed to succeed if @link is ap->link, the cable type of
5394 * the port is SATA and the port implements ->scr_read.
Tejun Heo34bf2172006-05-15 20:57:46 +09005395 *
5396 * LOCKING:
Tejun Heo633273a2007-09-23 13:19:54 +09005397 * None if @link is ap->link. Kernel thread context otherwise.
Tejun Heo34bf2172006-05-15 20:57:46 +09005398 *
5399 * RETURNS:
5400 * 0 on success, negative errno on failure.
5401 */
Tejun Heo936fd732007-08-06 18:36:23 +09005402int sata_scr_read(struct ata_link *link, int reg, u32 *val)
Tejun Heo34bf2172006-05-15 20:57:46 +09005403{
Tejun Heo633273a2007-09-23 13:19:54 +09005404 if (ata_is_host_link(link)) {
Tejun Heo633273a2007-09-23 13:19:54 +09005405 if (sata_scr_valid(link))
Tejun Heo82ef04f2008-07-31 17:02:40 +09005406 return link->ap->ops->scr_read(link, reg, val);
Tejun Heo633273a2007-09-23 13:19:54 +09005407 return -EOPNOTSUPP;
5408 }
5409
5410 return sata_pmp_scr_read(link, reg, val);
Tejun Heo34bf2172006-05-15 20:57:46 +09005411}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01005412EXPORT_SYMBOL_GPL(sata_scr_read);
Tejun Heo34bf2172006-05-15 20:57:46 +09005413
5414/**
5415 * sata_scr_write - write SCR register of the specified port
Tejun Heo936fd732007-08-06 18:36:23 +09005416 * @link: ATA link to write SCR for
Tejun Heo34bf2172006-05-15 20:57:46 +09005417 * @reg: SCR to write
5418 * @val: value to write
5419 *
Tejun Heo936fd732007-08-06 18:36:23 +09005420 * Write @val to SCR register @reg of @link. This function is
Tejun Heo633273a2007-09-23 13:19:54 +09005421 * guaranteed to succeed if @link is ap->link, the cable type of
5422 * the port is SATA and the port implements ->scr_read.
Tejun Heo34bf2172006-05-15 20:57:46 +09005423 *
5424 * LOCKING:
Tejun Heo633273a2007-09-23 13:19:54 +09005425 * None if @link is ap->link. Kernel thread context otherwise.
Tejun Heo34bf2172006-05-15 20:57:46 +09005426 *
5427 * RETURNS:
5428 * 0 on success, negative errno on failure.
5429 */
Tejun Heo936fd732007-08-06 18:36:23 +09005430int sata_scr_write(struct ata_link *link, int reg, u32 val)
Tejun Heo34bf2172006-05-15 20:57:46 +09005431{
Tejun Heo633273a2007-09-23 13:19:54 +09005432 if (ata_is_host_link(link)) {
Tejun Heo633273a2007-09-23 13:19:54 +09005433 if (sata_scr_valid(link))
Tejun Heo82ef04f2008-07-31 17:02:40 +09005434 return link->ap->ops->scr_write(link, reg, val);
Tejun Heo633273a2007-09-23 13:19:54 +09005435 return -EOPNOTSUPP;
5436 }
5437
5438 return sata_pmp_scr_write(link, reg, val);
Tejun Heo34bf2172006-05-15 20:57:46 +09005439}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01005440EXPORT_SYMBOL_GPL(sata_scr_write);
Tejun Heo34bf2172006-05-15 20:57:46 +09005441
5442/**
5443 * sata_scr_write_flush - write SCR register of the specified port and flush
Tejun Heo936fd732007-08-06 18:36:23 +09005444 * @link: ATA link to write SCR for
Tejun Heo34bf2172006-05-15 20:57:46 +09005445 * @reg: SCR to write
5446 * @val: value to write
5447 *
5448 * This function is identical to sata_scr_write() except that this
5449 * function performs flush after writing to the register.
5450 *
5451 * LOCKING:
Tejun Heo633273a2007-09-23 13:19:54 +09005452 * None if @link is ap->link. Kernel thread context otherwise.
Tejun Heo34bf2172006-05-15 20:57:46 +09005453 *
5454 * RETURNS:
5455 * 0 on success, negative errno on failure.
5456 */
Tejun Heo936fd732007-08-06 18:36:23 +09005457int sata_scr_write_flush(struct ata_link *link, int reg, u32 val)
Tejun Heo34bf2172006-05-15 20:57:46 +09005458{
Tejun Heo633273a2007-09-23 13:19:54 +09005459 if (ata_is_host_link(link)) {
Tejun Heo633273a2007-09-23 13:19:54 +09005460 int rc;
Tejun Heoda3dbb12007-07-16 14:29:40 +09005461
Tejun Heo633273a2007-09-23 13:19:54 +09005462 if (sata_scr_valid(link)) {
Tejun Heo82ef04f2008-07-31 17:02:40 +09005463 rc = link->ap->ops->scr_write(link, reg, val);
Tejun Heo633273a2007-09-23 13:19:54 +09005464 if (rc == 0)
Tejun Heo82ef04f2008-07-31 17:02:40 +09005465 rc = link->ap->ops->scr_read(link, reg, &val);
Tejun Heo633273a2007-09-23 13:19:54 +09005466 return rc;
5467 }
5468 return -EOPNOTSUPP;
Tejun Heo34bf2172006-05-15 20:57:46 +09005469 }
Tejun Heo633273a2007-09-23 13:19:54 +09005470
5471 return sata_pmp_scr_write(link, reg, val);
Tejun Heo34bf2172006-05-15 20:57:46 +09005472}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01005473EXPORT_SYMBOL_GPL(sata_scr_write_flush);
Tejun Heo34bf2172006-05-15 20:57:46 +09005474
5475/**
Tejun Heob1c72912008-07-31 17:02:43 +09005476 * ata_phys_link_online - test whether the given link is online
Tejun Heo936fd732007-08-06 18:36:23 +09005477 * @link: ATA link to test
Tejun Heo34bf2172006-05-15 20:57:46 +09005478 *
Tejun Heo936fd732007-08-06 18:36:23 +09005479 * Test whether @link is online. Note that this function returns
5480 * 0 if online status of @link cannot be obtained, so
5481 * ata_link_online(link) != !ata_link_offline(link).
Tejun Heo34bf2172006-05-15 20:57:46 +09005482 *
5483 * LOCKING:
5484 * None.
5485 *
5486 * RETURNS:
Tejun Heob5b3fa32008-07-31 17:02:42 +09005487 * True if the port online status is available and online.
Tejun Heo34bf2172006-05-15 20:57:46 +09005488 */
Tejun Heob1c72912008-07-31 17:02:43 +09005489bool ata_phys_link_online(struct ata_link *link)
Tejun Heo34bf2172006-05-15 20:57:46 +09005490{
5491 u32 sstatus;
5492
Tejun Heo936fd732007-08-06 18:36:23 +09005493 if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0 &&
Tejun Heo9913ff8a2009-01-29 20:31:31 +09005494 ata_sstatus_online(sstatus))
Tejun Heob5b3fa32008-07-31 17:02:42 +09005495 return true;
5496 return false;
Tejun Heo34bf2172006-05-15 20:57:46 +09005497}
5498
5499/**
Tejun Heob1c72912008-07-31 17:02:43 +09005500 * ata_phys_link_offline - test whether the given link is offline
Tejun Heo936fd732007-08-06 18:36:23 +09005501 * @link: ATA link to test
Tejun Heo34bf2172006-05-15 20:57:46 +09005502 *
Tejun Heo936fd732007-08-06 18:36:23 +09005503 * Test whether @link is offline. Note that this function
5504 * returns 0 if offline status of @link cannot be obtained, so
5505 * ata_link_online(link) != !ata_link_offline(link).
Tejun Heo34bf2172006-05-15 20:57:46 +09005506 *
5507 * LOCKING:
5508 * None.
5509 *
5510 * RETURNS:
Tejun Heob5b3fa32008-07-31 17:02:42 +09005511 * True if the port offline status is available and offline.
Tejun Heo34bf2172006-05-15 20:57:46 +09005512 */
Tejun Heob1c72912008-07-31 17:02:43 +09005513bool ata_phys_link_offline(struct ata_link *link)
Tejun Heo34bf2172006-05-15 20:57:46 +09005514{
5515 u32 sstatus;
5516
Tejun Heo936fd732007-08-06 18:36:23 +09005517 if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0 &&
Tejun Heo9913ff8a2009-01-29 20:31:31 +09005518 !ata_sstatus_online(sstatus))
Tejun Heob5b3fa32008-07-31 17:02:42 +09005519 return true;
5520 return false;
Tejun Heo34bf2172006-05-15 20:57:46 +09005521}
Edward Falk0baab862005-06-02 18:17:13 -04005522
Tejun Heob1c72912008-07-31 17:02:43 +09005523/**
5524 * ata_link_online - test whether the given link is online
5525 * @link: ATA link to test
5526 *
5527 * Test whether @link is online. This is identical to
5528 * ata_phys_link_online() when there's no slave link. When
5529 * there's a slave link, this function should only be called on
5530 * the master link and will return true if any of M/S links is
5531 * online.
5532 *
5533 * LOCKING:
5534 * None.
5535 *
5536 * RETURNS:
5537 * True if the port online status is available and online.
5538 */
5539bool ata_link_online(struct ata_link *link)
5540{
5541 struct ata_link *slave = link->ap->slave_link;
5542
5543 WARN_ON(link == slave); /* shouldn't be called on slave link */
5544
5545 return ata_phys_link_online(link) ||
5546 (slave && ata_phys_link_online(slave));
5547}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01005548EXPORT_SYMBOL_GPL(ata_link_online);
Tejun Heob1c72912008-07-31 17:02:43 +09005549
5550/**
5551 * ata_link_offline - test whether the given link is offline
5552 * @link: ATA link to test
5553 *
5554 * Test whether @link is offline. This is identical to
5555 * ata_phys_link_offline() when there's no slave link. When
5556 * there's a slave link, this function should only be called on
5557 * the master link and will return true if both M/S links are
5558 * offline.
5559 *
5560 * LOCKING:
5561 * None.
5562 *
5563 * RETURNS:
5564 * True if the port offline status is available and offline.
5565 */
5566bool ata_link_offline(struct ata_link *link)
5567{
5568 struct ata_link *slave = link->ap->slave_link;
5569
5570 WARN_ON(link == slave); /* shouldn't be called on slave link */
5571
5572 return ata_phys_link_offline(link) &&
5573 (!slave || ata_phys_link_offline(slave));
5574}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01005575EXPORT_SYMBOL_GPL(ata_link_offline);
Tejun Heob1c72912008-07-31 17:02:43 +09005576
Tejun Heo6ffa01d2007-03-02 17:32:47 +09005577#ifdef CONFIG_PM
Dan Williamsbc6e7c42014-03-14 13:52:48 -07005578static void ata_port_request_pm(struct ata_port *ap, pm_message_t mesg,
5579 unsigned int action, unsigned int ehi_flags,
5580 bool async)
Tejun Heo500530f2006-07-03 16:07:27 +09005581{
Lin Ming5ef41082011-12-05 09:20:27 +08005582 struct ata_link *link;
Tejun Heo500530f2006-07-03 16:07:27 +09005583 unsigned long flags;
Tejun Heo500530f2006-07-03 16:07:27 +09005584
Lin Ming5ef41082011-12-05 09:20:27 +08005585 /* Previous resume operation might still be in
5586 * progress. Wait for PM_PENDING to clear.
5587 */
5588 if (ap->pflags & ATA_PFLAG_PM_PENDING) {
5589 ata_port_wait_eh(ap);
5590 WARN_ON(ap->pflags & ATA_PFLAG_PM_PENDING);
Tejun Heo500530f2006-07-03 16:07:27 +09005591 }
5592
Lin Ming5ef41082011-12-05 09:20:27 +08005593 /* request PM ops to EH */
5594 spin_lock_irqsave(ap->lock, flags);
5595
5596 ap->pm_mesg = mesg;
Lin Ming5ef41082011-12-05 09:20:27 +08005597 ap->pflags |= ATA_PFLAG_PM_PENDING;
5598 ata_for_each_link(link, ap, HOST_FIRST) {
5599 link->eh_info.action |= action;
5600 link->eh_info.flags |= ehi_flags;
5601 }
5602
5603 ata_port_schedule_eh(ap);
5604
5605 spin_unlock_irqrestore(ap->lock, flags);
5606
Dan Williams2fcbdcb2012-06-21 23:41:46 -07005607 if (!async) {
Lin Ming5ef41082011-12-05 09:20:27 +08005608 ata_port_wait_eh(ap);
5609 WARN_ON(ap->pflags & ATA_PFLAG_PM_PENDING);
5610 }
Tejun Heo500530f2006-07-03 16:07:27 +09005611}
5612
Dan Williamsbc6e7c42014-03-14 13:52:48 -07005613/*
5614 * On some hardware, device fails to respond after spun down for suspend. As
5615 * the device won't be used before being resumed, we don't need to touch the
5616 * device. Ask EH to skip the usual stuff and proceed directly to suspend.
5617 *
5618 * http://thread.gmane.org/gmane.linux.ide/46764
5619 */
5620static const unsigned int ata_port_suspend_ehi = ATA_EHI_QUIET
5621 | ATA_EHI_NO_AUTOPSY
5622 | ATA_EHI_NO_RECOVERY;
5623
5624static void ata_port_suspend(struct ata_port *ap, pm_message_t mesg)
Lin Ming5ef41082011-12-05 09:20:27 +08005625{
Dan Williamsbc6e7c42014-03-14 13:52:48 -07005626 ata_port_request_pm(ap, mesg, 0, ata_port_suspend_ehi, false);
Lin Ming5ef41082011-12-05 09:20:27 +08005627}
5628
Dan Williamsbc6e7c42014-03-14 13:52:48 -07005629static void ata_port_suspend_async(struct ata_port *ap, pm_message_t mesg)
5630{
5631 ata_port_request_pm(ap, mesg, 0, ata_port_suspend_ehi, true);
5632}
5633
5634static int ata_port_pm_suspend(struct device *dev)
Dan Williams2fcbdcb2012-06-21 23:41:46 -07005635{
5636 struct ata_port *ap = to_ata_port(dev);
5637
Lin Ming5ef41082011-12-05 09:20:27 +08005638 if (pm_runtime_suspended(dev))
5639 return 0;
5640
Dan Williamsbc6e7c42014-03-14 13:52:48 -07005641 ata_port_suspend(ap, PMSG_SUSPEND);
5642 return 0;
Lin Ming33574d62011-12-22 14:50:49 +08005643}
5644
Dan Williamsbc6e7c42014-03-14 13:52:48 -07005645static int ata_port_pm_freeze(struct device *dev)
Dan Williams2fcbdcb2012-06-21 23:41:46 -07005646{
5647 struct ata_port *ap = to_ata_port(dev);
5648
Dan Williamsbc6e7c42014-03-14 13:52:48 -07005649 if (pm_runtime_suspended(dev))
5650 return 0;
5651
5652 ata_port_suspend(ap, PMSG_FREEZE);
5653 return 0;
Dan Williams2fcbdcb2012-06-21 23:41:46 -07005654}
5655
Dan Williamsbc6e7c42014-03-14 13:52:48 -07005656static int ata_port_pm_poweroff(struct device *dev)
Lin Minge90b1e52011-12-22 14:50:48 +08005657{
Dan Williamsbc6e7c42014-03-14 13:52:48 -07005658 ata_port_suspend(to_ata_port(dev), PMSG_HIBERNATE);
5659 return 0;
5660}
Lin Minge90b1e52011-12-22 14:50:48 +08005661
Dan Williamsbc6e7c42014-03-14 13:52:48 -07005662static const unsigned int ata_port_resume_ehi = ATA_EHI_NO_AUTOPSY
5663 | ATA_EHI_QUIET;
Lin Minge90b1e52011-12-22 14:50:48 +08005664
Dan Williamsbc6e7c42014-03-14 13:52:48 -07005665static void ata_port_resume(struct ata_port *ap, pm_message_t mesg)
5666{
5667 ata_port_request_pm(ap, mesg, ATA_EH_RESET, ata_port_resume_ehi, false);
5668}
5669
5670static void ata_port_resume_async(struct ata_port *ap, pm_message_t mesg)
5671{
5672 ata_port_request_pm(ap, mesg, ATA_EH_RESET, ata_port_resume_ehi, true);
5673}
5674
5675static int ata_port_pm_resume(struct device *dev)
5676{
Todd Brandt200421a2014-03-14 13:52:54 -07005677 ata_port_resume_async(to_ata_port(dev), PMSG_RESUME);
Dan Williamsbc6e7c42014-03-14 13:52:48 -07005678 pm_runtime_disable(dev);
5679 pm_runtime_set_active(dev);
5680 pm_runtime_enable(dev);
5681 return 0;
Lin Minge90b1e52011-12-22 14:50:48 +08005682}
5683
Aaron Lu7e15e9b2013-01-15 17:21:04 +08005684/*
5685 * For ODDs, the upper layer will poll for media change every few seconds,
5686 * which will make it enter and leave suspend state every few seconds. And
5687 * as each suspend will cause a hard/soft reset, the gain of runtime suspend
5688 * is very little and the ODD may malfunction after constantly being reset.
5689 * So the idle callback here will not proceed to suspend if a non-ZPODD capable
5690 * ODD is attached to the port.
5691 */
Lin Ming9ee4f392011-12-05 09:20:28 +08005692static int ata_port_runtime_idle(struct device *dev)
5693{
Aaron Lu7e15e9b2013-01-15 17:21:04 +08005694 struct ata_port *ap = to_ata_port(dev);
5695 struct ata_link *link;
5696 struct ata_device *adev;
5697
5698 ata_for_each_link(link, ap, HOST_FIRST) {
5699 ata_for_each_dev(adev, link, ENABLED)
5700 if (adev->class == ATA_DEV_ATAPI &&
5701 !zpodd_dev_enabled(adev))
5702 return -EBUSY;
5703 }
5704
Rafael J. Wysocki45f0a852013-06-03 21:49:52 +02005705 return 0;
Lin Ming9ee4f392011-12-05 09:20:28 +08005706}
5707
Aaron Lua7ff60d2013-01-25 14:29:35 +08005708static int ata_port_runtime_suspend(struct device *dev)
5709{
Dan Williamsbc6e7c42014-03-14 13:52:48 -07005710 ata_port_suspend(to_ata_port(dev), PMSG_AUTO_SUSPEND);
5711 return 0;
Aaron Lua7ff60d2013-01-25 14:29:35 +08005712}
5713
5714static int ata_port_runtime_resume(struct device *dev)
5715{
Dan Williamsbc6e7c42014-03-14 13:52:48 -07005716 ata_port_resume(to_ata_port(dev), PMSG_AUTO_RESUME);
5717 return 0;
Aaron Lua7ff60d2013-01-25 14:29:35 +08005718}
5719
Lin Ming5ef41082011-12-05 09:20:27 +08005720static const struct dev_pm_ops ata_port_pm_ops = {
Dan Williamsbc6e7c42014-03-14 13:52:48 -07005721 .suspend = ata_port_pm_suspend,
5722 .resume = ata_port_pm_resume,
5723 .freeze = ata_port_pm_freeze,
5724 .thaw = ata_port_pm_resume,
5725 .poweroff = ata_port_pm_poweroff,
5726 .restore = ata_port_pm_resume,
Lin Ming9ee4f392011-12-05 09:20:28 +08005727
Aaron Lua7ff60d2013-01-25 14:29:35 +08005728 .runtime_suspend = ata_port_runtime_suspend,
5729 .runtime_resume = ata_port_runtime_resume,
Lin Ming9ee4f392011-12-05 09:20:28 +08005730 .runtime_idle = ata_port_runtime_idle,
Lin Ming5ef41082011-12-05 09:20:27 +08005731};
5732
Dan Williams2fcbdcb2012-06-21 23:41:46 -07005733/* sas ports don't participate in pm runtime management of ata_ports,
5734 * and need to resume ata devices at the domain level, not the per-port
5735 * level. sas suspend/resume is async to allow parallel port recovery
5736 * since sas has multiple ata_port instances per Scsi_Host.
5737 */
Dan Williamsbc6e7c42014-03-14 13:52:48 -07005738void ata_sas_port_suspend(struct ata_port *ap)
Dan Williams2fcbdcb2012-06-21 23:41:46 -07005739{
Dan Williamsbc6e7c42014-03-14 13:52:48 -07005740 ata_port_suspend_async(ap, PMSG_SUSPEND);
Dan Williams2fcbdcb2012-06-21 23:41:46 -07005741}
Dan Williamsbc6e7c42014-03-14 13:52:48 -07005742EXPORT_SYMBOL_GPL(ata_sas_port_suspend);
Dan Williams2fcbdcb2012-06-21 23:41:46 -07005743
Dan Williamsbc6e7c42014-03-14 13:52:48 -07005744void ata_sas_port_resume(struct ata_port *ap)
Dan Williams2fcbdcb2012-06-21 23:41:46 -07005745{
Dan Williamsbc6e7c42014-03-14 13:52:48 -07005746 ata_port_resume_async(ap, PMSG_RESUME);
Dan Williams2fcbdcb2012-06-21 23:41:46 -07005747}
Dan Williamsbc6e7c42014-03-14 13:52:48 -07005748EXPORT_SYMBOL_GPL(ata_sas_port_resume);
Dan Williams2fcbdcb2012-06-21 23:41:46 -07005749
Tejun Heo500530f2006-07-03 16:07:27 +09005750/**
Jeff Garzikcca39742006-08-24 03:19:22 -04005751 * ata_host_suspend - suspend host
5752 * @host: host to suspend
Tejun Heo500530f2006-07-03 16:07:27 +09005753 * @mesg: PM message
5754 *
Lin Ming5ef41082011-12-05 09:20:27 +08005755 * Suspend @host. Actual operation is performed by port suspend.
Tejun Heo500530f2006-07-03 16:07:27 +09005756 */
Jeff Garzikcca39742006-08-24 03:19:22 -04005757int ata_host_suspend(struct ata_host *host, pm_message_t mesg)
Tejun Heo500530f2006-07-03 16:07:27 +09005758{
Lin Ming5ef41082011-12-05 09:20:27 +08005759 host->dev->power.power_state = mesg;
5760 return 0;
Tejun Heo500530f2006-07-03 16:07:27 +09005761}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01005762EXPORT_SYMBOL_GPL(ata_host_suspend);
Tejun Heo500530f2006-07-03 16:07:27 +09005763
5764/**
Jeff Garzikcca39742006-08-24 03:19:22 -04005765 * ata_host_resume - resume host
5766 * @host: host to resume
Tejun Heo500530f2006-07-03 16:07:27 +09005767 *
Lin Ming5ef41082011-12-05 09:20:27 +08005768 * Resume @host. Actual operation is performed by port resume.
Tejun Heo500530f2006-07-03 16:07:27 +09005769 */
Jeff Garzikcca39742006-08-24 03:19:22 -04005770void ata_host_resume(struct ata_host *host)
Tejun Heo500530f2006-07-03 16:07:27 +09005771{
Jeff Garzik72ad6ec2008-02-25 17:31:10 -05005772 host->dev->power.power_state = PMSG_ON;
Tejun Heo500530f2006-07-03 16:07:27 +09005773}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01005774EXPORT_SYMBOL_GPL(ata_host_resume);
Tejun Heo6ffa01d2007-03-02 17:32:47 +09005775#endif
Tejun Heo500530f2006-07-03 16:07:27 +09005776
Bhumika Goyal8df82c12017-09-30 22:10:40 +05305777const struct device_type ata_port_type = {
Lin Ming5ef41082011-12-05 09:20:27 +08005778 .name = "ata_port",
5779#ifdef CONFIG_PM
5780 .pm = &ata_port_pm_ops,
5781#endif
5782};
5783
Randy Dunlapc893a3a2006-01-28 13:15:32 -05005784/**
Tejun Heo3ef3b432006-05-31 18:27:30 +09005785 * ata_dev_init - Initialize an ata_device structure
5786 * @dev: Device structure to initialize
5787 *
5788 * Initialize @dev in preparation for probing.
5789 *
5790 * LOCKING:
5791 * Inherited from caller.
5792 */
5793void ata_dev_init(struct ata_device *dev)
5794{
Tejun Heob1c72912008-07-31 17:02:43 +09005795 struct ata_link *link = ata_dev_phys_link(dev);
Tejun Heo9af5c9c2007-08-06 18:36:22 +09005796 struct ata_port *ap = link->ap;
Tejun Heo72fa4b72006-05-31 18:27:32 +09005797 unsigned long flags;
Tejun Heo3ef3b432006-05-31 18:27:30 +09005798
Tejun Heob1c72912008-07-31 17:02:43 +09005799 /* SATA spd limit is bound to the attached device, reset together */
Tejun Heo9af5c9c2007-08-06 18:36:22 +09005800 link->sata_spd_limit = link->hw_sata_spd_limit;
5801 link->sata_spd = 0;
Tejun Heo5a04bf42006-05-31 18:27:38 +09005802
Tejun Heo72fa4b72006-05-31 18:27:32 +09005803 /* High bits of dev->flags are used to record warm plug
5804 * requests which occur asynchronously. Synchronize using
Jeff Garzikcca39742006-08-24 03:19:22 -04005805 * host lock.
Tejun Heo72fa4b72006-05-31 18:27:32 +09005806 */
Jeff Garzikba6a1302006-06-22 23:46:10 -04005807 spin_lock_irqsave(ap->lock, flags);
Tejun Heo72fa4b72006-05-31 18:27:32 +09005808 dev->flags &= ~ATA_DFLAG_INIT_MASK;
Tejun Heo3dcc3232007-09-03 12:20:11 +09005809 dev->horkage = 0;
Jeff Garzikba6a1302006-06-22 23:46:10 -04005810 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heo72fa4b72006-05-31 18:27:32 +09005811
Tejun Heo99cf6102009-01-29 20:31:32 +09005812 memset((void *)dev + ATA_DEVICE_CLEAR_BEGIN, 0,
5813 ATA_DEVICE_CLEAR_END - ATA_DEVICE_CLEAR_BEGIN);
Tejun Heo3ef3b432006-05-31 18:27:30 +09005814 dev->pio_mask = UINT_MAX;
5815 dev->mwdma_mask = UINT_MAX;
5816 dev->udma_mask = UINT_MAX;
5817}
5818
5819/**
Tejun Heo4fb37a22007-08-06 18:36:23 +09005820 * ata_link_init - Initialize an ata_link structure
5821 * @ap: ATA port link is attached to
5822 * @link: Link structure to initialize
Tejun Heo89898052007-08-06 18:36:23 +09005823 * @pmp: Port multiplier port number
Tejun Heo4fb37a22007-08-06 18:36:23 +09005824 *
5825 * Initialize @link.
5826 *
5827 * LOCKING:
5828 * Kernel thread context (may sleep)
5829 */
Tejun Heofb7fd612007-09-23 13:14:12 +09005830void ata_link_init(struct ata_port *ap, struct ata_link *link, int pmp)
Tejun Heo4fb37a22007-08-06 18:36:23 +09005831{
5832 int i;
5833
5834 /* clear everything except for devices */
Gwendal Grignoud9027472010-05-25 12:31:38 -07005835 memset((void *)link + ATA_LINK_CLEAR_BEGIN, 0,
5836 ATA_LINK_CLEAR_END - ATA_LINK_CLEAR_BEGIN);
Tejun Heo4fb37a22007-08-06 18:36:23 +09005837
5838 link->ap = ap;
Tejun Heo89898052007-08-06 18:36:23 +09005839 link->pmp = pmp;
Tejun Heo4fb37a22007-08-06 18:36:23 +09005840 link->active_tag = ATA_TAG_POISON;
5841 link->hw_sata_spd_limit = UINT_MAX;
5842
5843 /* can't use iterator, ap isn't initialized yet */
5844 for (i = 0; i < ATA_MAX_DEVICES; i++) {
5845 struct ata_device *dev = &link->device[i];
5846
5847 dev->link = link;
5848 dev->devno = dev - link->device;
Tejun Heo110f66d2009-09-16 04:17:28 +09005849#ifdef CONFIG_ATA_ACPI
5850 dev->gtf_filter = ata_acpi_gtf_filter;
5851#endif
Tejun Heo4fb37a22007-08-06 18:36:23 +09005852 ata_dev_init(dev);
5853 }
5854}
5855
5856/**
5857 * sata_link_init_spd - Initialize link->sata_spd_limit
5858 * @link: Link to configure sata_spd_limit for
5859 *
5860 * Initialize @link->[hw_]sata_spd_limit to the currently
5861 * configured value.
5862 *
5863 * LOCKING:
5864 * Kernel thread context (may sleep).
5865 *
5866 * RETURNS:
5867 * 0 on success, -errno on failure.
5868 */
Tejun Heofb7fd612007-09-23 13:14:12 +09005869int sata_link_init_spd(struct ata_link *link)
Tejun Heo4fb37a22007-08-06 18:36:23 +09005870{
Tejun Heo33267322008-02-13 09:15:09 +09005871 u8 spd;
Tejun Heo4fb37a22007-08-06 18:36:23 +09005872 int rc;
5873
Tejun Heod127ea72008-07-31 16:09:34 +09005874 rc = sata_scr_read(link, SCR_CONTROL, &link->saved_scontrol);
Tejun Heo4fb37a22007-08-06 18:36:23 +09005875 if (rc)
5876 return rc;
5877
Tejun Heod127ea72008-07-31 16:09:34 +09005878 spd = (link->saved_scontrol >> 4) & 0xf;
Tejun Heo4fb37a22007-08-06 18:36:23 +09005879 if (spd)
5880 link->hw_sata_spd_limit &= (1 << spd) - 1;
5881
Tejun Heo05944bd2008-08-13 20:19:09 +09005882 ata_force_link_limits(link);
Tejun Heo33267322008-02-13 09:15:09 +09005883
Tejun Heo4fb37a22007-08-06 18:36:23 +09005884 link->sata_spd_limit = link->hw_sata_spd_limit;
5885
5886 return 0;
5887}
5888
5889/**
Tejun Heof3187192007-04-17 23:44:07 +09005890 * ata_port_alloc - allocate and initialize basic ATA port resources
5891 * @host: ATA host this allocated port belongs to
Linus Torvalds1da177e2005-04-16 15:20:36 -07005892 *
Tejun Heof3187192007-04-17 23:44:07 +09005893 * Allocate and initialize basic ATA port resources.
5894 *
5895 * RETURNS:
5896 * Allocate ATA port on success, NULL on failure.
Jeff Garzik0cba6322005-05-30 19:49:12 -04005897 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07005898 * LOCKING:
Tejun Heof3187192007-04-17 23:44:07 +09005899 * Inherited from calling layer (may sleep).
Linus Torvalds1da177e2005-04-16 15:20:36 -07005900 */
Tejun Heof3187192007-04-17 23:44:07 +09005901struct ata_port *ata_port_alloc(struct ata_host *host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005902{
Tejun Heof3187192007-04-17 23:44:07 +09005903 struct ata_port *ap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005904
Tejun Heof3187192007-04-17 23:44:07 +09005905 DPRINTK("ENTER\n");
5906
5907 ap = kzalloc(sizeof(*ap), GFP_KERNEL);
5908 if (!ap)
5909 return NULL;
Jeff Garzik4fca3772011-02-15 01:13:24 -05005910
Maxime Bizon7b3a24c52011-03-16 14:58:32 +01005911 ap->pflags |= ATA_PFLAG_INITIALIZING | ATA_PFLAG_FROZEN;
Jeff Garzikcca39742006-08-24 03:19:22 -04005912 ap->lock = &host->lock;
Tejun Heof3187192007-04-17 23:44:07 +09005913 ap->print_id = -1;
David Milburne628dc92013-05-14 13:48:40 -05005914 ap->local_port_no = -1;
Jeff Garzikcca39742006-08-24 03:19:22 -04005915 ap->host = host;
Tejun Heof3187192007-04-17 23:44:07 +09005916 ap->dev = host->dev;
Borislav Petkovbd5d8252006-06-11 23:17:01 -04005917
5918#if defined(ATA_VERBOSE_DEBUG)
5919 /* turn on all debugging levels */
5920 ap->msg_enable = 0x00FF;
5921#elif defined(ATA_DEBUG)
5922 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 +09005923#else
Borislav Petkov0dd4b212006-06-23 02:29:08 -04005924 ap->msg_enable = ATA_MSG_DRV | ATA_MSG_ERR | ATA_MSG_WARN;
Borislav Petkovbd5d8252006-06-11 23:17:01 -04005925#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005926
Tejun Heoad72cf92010-07-02 10:03:52 +02005927 mutex_init(&ap->scsi_scan_mutex);
David Howells65f27f32006-11-22 14:55:48 +00005928 INIT_DELAYED_WORK(&ap->hotplug_task, ata_scsi_hotplug);
5929 INIT_WORK(&ap->scsi_rescan_task, ata_scsi_dev_rescan);
Tejun Heoa72ec4c2006-01-23 13:09:37 +09005930 INIT_LIST_HEAD(&ap->eh_done_q);
Tejun Heoc6cf9e92006-05-31 18:27:27 +09005931 init_waitqueue_head(&ap->eh_wait_q);
Elias Oltmanns45fabbb2008-09-21 11:54:08 +02005932 init_completion(&ap->park_req_pending);
Kees Cookb93ab332017-10-16 14:56:42 -07005933 timer_setup(&ap->fastdrain_timer, ata_eh_fastdrain_timerfn,
5934 TIMER_DEFERRABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005935
Tejun Heo838df622006-05-15 20:57:44 +09005936 ap->cbl = ATA_CBL_NONE;
Tejun Heo838df622006-05-15 20:57:44 +09005937
Tejun Heo89898052007-08-06 18:36:23 +09005938 ata_link_init(ap, &ap->link, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005939
5940#ifdef ATA_IRQ_TRAP
5941 ap->stats.unhandled_irq = 1;
5942 ap->stats.idle_irq = 1;
5943#endif
Tejun Heo270390e2010-05-10 21:41:35 +02005944 ata_sff_port_init(ap);
5945
Linus Torvalds1da177e2005-04-16 15:20:36 -07005946 return ap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005947}
5948
Taras Kondratiuk2623c7a2018-03-09 08:34:41 +00005949static void ata_devres_release(struct device *gendev, void *res)
Tejun Heof0d36ef2007-01-20 16:00:28 +09005950{
5951 struct ata_host *host = dev_get_drvdata(gendev);
5952 int i;
5953
5954 for (i = 0; i < host->n_ports; i++) {
5955 struct ata_port *ap = host->ports[i];
5956
Tejun Heoecef7252007-04-17 23:44:06 +09005957 if (!ap)
5958 continue;
5959
Tejun Heo49114872007-04-17 23:44:06 +09005960 if (ap->scsi_host)
Tejun Heo1aa506e42007-03-09 19:36:12 +09005961 scsi_host_put(ap->scsi_host);
5962
Taras Kondratiuk2623c7a2018-03-09 08:34:41 +00005963 }
5964
5965 dev_set_drvdata(gendev, NULL);
5966 ata_host_put(host);
5967}
5968
5969static void ata_host_release(struct kref *kref)
5970{
5971 struct ata_host *host = container_of(kref, struct ata_host, kref);
5972 int i;
5973
5974 for (i = 0; i < host->n_ports; i++) {
5975 struct ata_port *ap = host->ports[i];
5976
Tejun Heo633273a2007-09-23 13:19:54 +09005977 kfree(ap->pmp_link);
Tejun Heob1c72912008-07-31 17:02:43 +09005978 kfree(ap->slave_link);
Tejun Heo49114872007-04-17 23:44:06 +09005979 kfree(ap);
Tejun Heo1aa506e42007-03-09 19:36:12 +09005980 host->ports[i] = NULL;
5981 }
Taras Kondratiuk2623c7a2018-03-09 08:34:41 +00005982 kfree(host);
5983}
Tejun Heo1aa506e42007-03-09 19:36:12 +09005984
Taras Kondratiuk2623c7a2018-03-09 08:34:41 +00005985void ata_host_get(struct ata_host *host)
5986{
5987 kref_get(&host->kref);
5988}
5989
5990void ata_host_put(struct ata_host *host)
5991{
5992 kref_put(&host->kref, ata_host_release);
Tejun Heof0d36ef2007-01-20 16:00:28 +09005993}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01005994EXPORT_SYMBOL_GPL(ata_host_put);
Tejun Heof0d36ef2007-01-20 16:00:28 +09005995
Linus Torvalds1da177e2005-04-16 15:20:36 -07005996/**
Tejun Heof3187192007-04-17 23:44:07 +09005997 * ata_host_alloc - allocate and init basic ATA host resources
5998 * @dev: generic device this host is associated with
5999 * @max_ports: maximum number of ATA ports associated with this host
6000 *
6001 * Allocate and initialize basic ATA host resources. LLD calls
6002 * this function to allocate a host, initializes it fully and
6003 * attaches it using ata_host_register().
6004 *
6005 * @max_ports ports are allocated and host->n_ports is
6006 * initialized to @max_ports. The caller is allowed to decrease
6007 * host->n_ports before calling ata_host_register(). The unused
6008 * ports will be automatically freed on registration.
6009 *
6010 * RETURNS:
6011 * Allocate ATA host on success, NULL on failure.
6012 *
6013 * LOCKING:
6014 * Inherited from calling layer (may sleep).
6015 */
6016struct ata_host *ata_host_alloc(struct device *dev, int max_ports)
6017{
6018 struct ata_host *host;
6019 size_t sz;
6020 int i;
Taras Kondratiuk2623c7a2018-03-09 08:34:41 +00006021 void *dr;
Tejun Heof3187192007-04-17 23:44:07 +09006022
6023 DPRINTK("ENTER\n");
6024
Tejun Heof3187192007-04-17 23:44:07 +09006025 /* alloc a container for our list of ATA ports (buses) */
6026 sz = sizeof(struct ata_host) + (max_ports + 1) * sizeof(void *);
Taras Kondratiuk2623c7a2018-03-09 08:34:41 +00006027 host = kzalloc(sz, GFP_KERNEL);
Tejun Heof3187192007-04-17 23:44:07 +09006028 if (!host)
Taras Kondratiuk2623c7a2018-03-09 08:34:41 +00006029 return NULL;
6030
Tejun Heof3187192007-04-17 23:44:07 +09006031 if (!devres_open_group(dev, NULL, GFP_KERNEL))
Colin Ian Kingdafd6c42018-03-27 14:26:01 +01006032 goto err_free;
Tejun Heof3187192007-04-17 23:44:07 +09006033
Taras Kondratiuk2623c7a2018-03-09 08:34:41 +00006034 dr = devres_alloc(ata_devres_release, 0, GFP_KERNEL);
6035 if (!dr)
Tejun Heof3187192007-04-17 23:44:07 +09006036 goto err_out;
6037
Taras Kondratiuk2623c7a2018-03-09 08:34:41 +00006038 devres_add(dev, dr);
Tejun Heof3187192007-04-17 23:44:07 +09006039 dev_set_drvdata(dev, host);
6040
6041 spin_lock_init(&host->lock);
Tejun Heoc0c362b2010-09-06 17:57:14 +02006042 mutex_init(&host->eh_mutex);
Tejun Heof3187192007-04-17 23:44:07 +09006043 host->dev = dev;
6044 host->n_ports = max_ports;
Taras Kondratiuk2623c7a2018-03-09 08:34:41 +00006045 kref_init(&host->kref);
Tejun Heof3187192007-04-17 23:44:07 +09006046
6047 /* allocate ports bound to this host */
6048 for (i = 0; i < max_ports; i++) {
6049 struct ata_port *ap;
6050
6051 ap = ata_port_alloc(host);
6052 if (!ap)
6053 goto err_out;
6054
6055 ap->port_no = i;
6056 host->ports[i] = ap;
6057 }
6058
6059 devres_remove_group(dev, NULL);
6060 return host;
6061
6062 err_out:
6063 devres_release_group(dev, NULL);
Colin Ian Kingdafd6c42018-03-27 14:26:01 +01006064 err_free:
6065 kfree(host);
Tejun Heof3187192007-04-17 23:44:07 +09006066 return NULL;
6067}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01006068EXPORT_SYMBOL_GPL(ata_host_alloc);
Tejun Heof3187192007-04-17 23:44:07 +09006069
6070/**
Tejun Heof5cda252007-04-17 23:44:07 +09006071 * ata_host_alloc_pinfo - alloc host and init with port_info array
6072 * @dev: generic device this host is associated with
6073 * @ppi: array of ATA port_info to initialize host with
6074 * @n_ports: number of ATA ports attached to this host
6075 *
6076 * Allocate ATA host and initialize with info from @ppi. If NULL
6077 * terminated, @ppi may contain fewer entries than @n_ports. The
6078 * last entry will be used for the remaining ports.
6079 *
6080 * RETURNS:
6081 * Allocate ATA host on success, NULL on failure.
6082 *
6083 * LOCKING:
6084 * Inherited from calling layer (may sleep).
6085 */
6086struct ata_host *ata_host_alloc_pinfo(struct device *dev,
6087 const struct ata_port_info * const * ppi,
6088 int n_ports)
6089{
6090 const struct ata_port_info *pi;
6091 struct ata_host *host;
6092 int i, j;
6093
6094 host = ata_host_alloc(dev, n_ports);
6095 if (!host)
6096 return NULL;
6097
6098 for (i = 0, j = 0, pi = NULL; i < host->n_ports; i++) {
6099 struct ata_port *ap = host->ports[i];
6100
6101 if (ppi[j])
6102 pi = ppi[j++];
6103
6104 ap->pio_mask = pi->pio_mask;
6105 ap->mwdma_mask = pi->mwdma_mask;
6106 ap->udma_mask = pi->udma_mask;
6107 ap->flags |= pi->flags;
Tejun Heo0c887582007-08-06 18:36:23 +09006108 ap->link.flags |= pi->link_flags;
Tejun Heof5cda252007-04-17 23:44:07 +09006109 ap->ops = pi->port_ops;
6110
6111 if (!host->ops && (pi->port_ops != &ata_dummy_port_ops))
6112 host->ops = pi->port_ops;
Tejun Heof5cda252007-04-17 23:44:07 +09006113 }
6114
6115 return host;
6116}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01006117EXPORT_SYMBOL_GPL(ata_host_alloc_pinfo);
Tejun Heof5cda252007-04-17 23:44:07 +09006118
Tejun Heob1c72912008-07-31 17:02:43 +09006119/**
6120 * ata_slave_link_init - initialize slave link
6121 * @ap: port to initialize slave link for
6122 *
6123 * Create and initialize slave link for @ap. This enables slave
6124 * link handling on the port.
6125 *
6126 * In libata, a port contains links and a link contains devices.
6127 * There is single host link but if a PMP is attached to it,
6128 * there can be multiple fan-out links. On SATA, there's usually
6129 * a single device connected to a link but PATA and SATA
6130 * controllers emulating TF based interface can have two - master
6131 * and slave.
6132 *
6133 * However, there are a few controllers which don't fit into this
6134 * abstraction too well - SATA controllers which emulate TF
6135 * interface with both master and slave devices but also have
6136 * separate SCR register sets for each device. These controllers
6137 * need separate links for physical link handling
6138 * (e.g. onlineness, link speed) but should be treated like a
6139 * traditional M/S controller for everything else (e.g. command
6140 * issue, softreset).
6141 *
6142 * slave_link is libata's way of handling this class of
6143 * controllers without impacting core layer too much. For
6144 * anything other than physical link handling, the default host
6145 * link is used for both master and slave. For physical link
6146 * handling, separate @ap->slave_link is used. All dirty details
6147 * are implemented inside libata core layer. From LLD's POV, the
6148 * only difference is that prereset, hardreset and postreset are
6149 * called once more for the slave link, so the reset sequence
6150 * looks like the following.
6151 *
6152 * prereset(M) -> prereset(S) -> hardreset(M) -> hardreset(S) ->
6153 * softreset(M) -> postreset(M) -> postreset(S)
6154 *
6155 * Note that softreset is called only for the master. Softreset
6156 * resets both M/S by definition, so SRST on master should handle
6157 * both (the standard method will work just fine).
6158 *
6159 * LOCKING:
6160 * Should be called before host is registered.
6161 *
6162 * RETURNS:
6163 * 0 on success, -errno on failure.
6164 */
6165int ata_slave_link_init(struct ata_port *ap)
6166{
6167 struct ata_link *link;
6168
6169 WARN_ON(ap->slave_link);
6170 WARN_ON(ap->flags & ATA_FLAG_PMP);
6171
6172 link = kzalloc(sizeof(*link), GFP_KERNEL);
6173 if (!link)
6174 return -ENOMEM;
6175
6176 ata_link_init(ap, link, 1);
6177 ap->slave_link = link;
6178 return 0;
6179}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01006180EXPORT_SYMBOL_GPL(ata_slave_link_init);
Tejun Heob1c72912008-07-31 17:02:43 +09006181
Tejun Heo32ebbc02007-11-08 13:09:00 +09006182static void ata_host_stop(struct device *gendev, void *res)
6183{
6184 struct ata_host *host = dev_get_drvdata(gendev);
6185 int i;
6186
6187 WARN_ON(!(host->flags & ATA_HOST_STARTED));
6188
6189 for (i = 0; i < host->n_ports; i++) {
6190 struct ata_port *ap = host->ports[i];
6191
6192 if (ap->ops->port_stop)
6193 ap->ops->port_stop(ap);
6194 }
6195
6196 if (host->ops->host_stop)
6197 host->ops->host_stop(host);
6198}
6199
Tejun Heof5cda252007-04-17 23:44:07 +09006200/**
Tejun Heo029cfd62008-03-25 12:22:49 +09006201 * ata_finalize_port_ops - finalize ata_port_operations
6202 * @ops: ata_port_operations to finalize
6203 *
6204 * An ata_port_operations can inherit from another ops and that
6205 * ops can again inherit from another. This can go on as many
6206 * times as necessary as long as there is no loop in the
6207 * inheritance chain.
6208 *
6209 * Ops tables are finalized when the host is started. NULL or
6210 * unspecified entries are inherited from the closet ancestor
6211 * which has the method and the entry is populated with it.
6212 * After finalization, the ops table directly points to all the
6213 * methods and ->inherits is no longer necessary and cleared.
6214 *
6215 * Using ATA_OP_NULL, inheriting ops can force a method to NULL.
6216 *
6217 * LOCKING:
6218 * None.
6219 */
6220static void ata_finalize_port_ops(struct ata_port_operations *ops)
6221{
Pradeep Singh Rautela2da67652008-05-29 23:28:14 +05306222 static DEFINE_SPINLOCK(lock);
Tejun Heo029cfd62008-03-25 12:22:49 +09006223 const struct ata_port_operations *cur;
6224 void **begin = (void **)ops;
6225 void **end = (void **)&ops->inherits;
6226 void **pp;
6227
6228 if (!ops || !ops->inherits)
6229 return;
6230
6231 spin_lock(&lock);
6232
6233 for (cur = ops->inherits; cur; cur = cur->inherits) {
6234 void **inherit = (void **)cur;
6235
6236 for (pp = begin; pp < end; pp++, inherit++)
6237 if (!*pp)
6238 *pp = *inherit;
6239 }
6240
6241 for (pp = begin; pp < end; pp++)
6242 if (IS_ERR(*pp))
6243 *pp = NULL;
6244
6245 ops->inherits = NULL;
6246
6247 spin_unlock(&lock);
6248}
6249
6250/**
Tejun Heoecef7252007-04-17 23:44:06 +09006251 * ata_host_start - start and freeze ports of an ATA host
6252 * @host: ATA host to start ports for
6253 *
6254 * Start and then freeze ports of @host. Started status is
6255 * recorded in host->flags, so this function can be called
6256 * multiple times. Ports are guaranteed to get started only
Tejun Heof3187192007-04-17 23:44:07 +09006257 * once. If host->ops isn't initialized yet, its set to the
6258 * first non-dummy port ops.
Tejun Heoecef7252007-04-17 23:44:06 +09006259 *
6260 * LOCKING:
6261 * Inherited from calling layer (may sleep).
6262 *
6263 * RETURNS:
6264 * 0 if all ports are started successfully, -errno otherwise.
6265 */
6266int ata_host_start(struct ata_host *host)
6267{
Tejun Heo32ebbc02007-11-08 13:09:00 +09006268 int have_stop = 0;
6269 void *start_dr = NULL;
Tejun Heoecef7252007-04-17 23:44:06 +09006270 int i, rc;
6271
6272 if (host->flags & ATA_HOST_STARTED)
6273 return 0;
6274
Tejun Heo029cfd62008-03-25 12:22:49 +09006275 ata_finalize_port_ops(host->ops);
6276
Tejun Heoecef7252007-04-17 23:44:06 +09006277 for (i = 0; i < host->n_ports; i++) {
6278 struct ata_port *ap = host->ports[i];
6279
Tejun Heo029cfd62008-03-25 12:22:49 +09006280 ata_finalize_port_ops(ap->ops);
6281
Tejun Heof3187192007-04-17 23:44:07 +09006282 if (!host->ops && !ata_port_is_dummy(ap))
6283 host->ops = ap->ops;
6284
Tejun Heo32ebbc02007-11-08 13:09:00 +09006285 if (ap->ops->port_stop)
6286 have_stop = 1;
6287 }
6288
6289 if (host->ops->host_stop)
6290 have_stop = 1;
6291
6292 if (have_stop) {
6293 start_dr = devres_alloc(ata_host_stop, 0, GFP_KERNEL);
6294 if (!start_dr)
6295 return -ENOMEM;
6296 }
6297
6298 for (i = 0; i < host->n_ports; i++) {
6299 struct ata_port *ap = host->ports[i];
6300
Tejun Heoecef7252007-04-17 23:44:06 +09006301 if (ap->ops->port_start) {
6302 rc = ap->ops->port_start(ap);
6303 if (rc) {
Alan Cox0f9fe9b2007-11-30 15:23:16 +00006304 if (rc != -ENODEV)
Joe Perchesa44fec12011-04-15 15:51:58 -07006305 dev_err(host->dev,
6306 "failed to start port %d (errno=%d)\n",
6307 i, rc);
Tejun Heoecef7252007-04-17 23:44:06 +09006308 goto err_out;
6309 }
6310 }
Tejun Heoecef7252007-04-17 23:44:06 +09006311 ata_eh_freeze_port(ap);
6312 }
6313
Tejun Heo32ebbc02007-11-08 13:09:00 +09006314 if (start_dr)
6315 devres_add(host->dev, start_dr);
Tejun Heoecef7252007-04-17 23:44:06 +09006316 host->flags |= ATA_HOST_STARTED;
6317 return 0;
6318
6319 err_out:
6320 while (--i >= 0) {
6321 struct ata_port *ap = host->ports[i];
6322
6323 if (ap->ops->port_stop)
6324 ap->ops->port_stop(ap);
6325 }
Tejun Heo32ebbc02007-11-08 13:09:00 +09006326 devres_free(start_dr);
Tejun Heoecef7252007-04-17 23:44:06 +09006327 return rc;
6328}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01006329EXPORT_SYMBOL_GPL(ata_host_start);
Tejun Heoecef7252007-04-17 23:44:06 +09006330
6331/**
Dan Williams8d8e7d12012-07-09 21:06:08 -07006332 * ata_sas_host_init - Initialize a host struct for sas (ipr, libsas)
Jeff Garzikcca39742006-08-24 03:19:22 -04006333 * @host: host to initialize
6334 * @dev: device host is attached to
Jeff Garzikcca39742006-08-24 03:19:22 -04006335 * @ops: port_ops
Brian Kingb03732f2006-08-07 14:27:10 -05006336 *
Brian Kingb03732f2006-08-07 14:27:10 -05006337 */
Jeff Garzikcca39742006-08-24 03:19:22 -04006338void ata_host_init(struct ata_host *host, struct device *dev,
Dan Williams8d8e7d12012-07-09 21:06:08 -07006339 struct ata_port_operations *ops)
Brian Kingb03732f2006-08-07 14:27:10 -05006340{
Jeff Garzikcca39742006-08-24 03:19:22 -04006341 spin_lock_init(&host->lock);
Tejun Heoc0c362b2010-09-06 17:57:14 +02006342 mutex_init(&host->eh_mutex);
Jens Axboe69278f72018-05-11 12:51:10 -06006343 host->n_tags = ATA_MAX_QUEUE;
Jeff Garzikcca39742006-08-24 03:19:22 -04006344 host->dev = dev;
Jeff Garzikcca39742006-08-24 03:19:22 -04006345 host->ops = ops;
Jason Yan2fa4a322018-05-10 11:05:16 +08006346 kref_init(&host->kref);
Brian Kingb03732f2006-08-07 14:27:10 -05006347}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01006348EXPORT_SYMBOL_GPL(ata_host_init);
Brian Kingb03732f2006-08-07 14:27:10 -05006349
Dan Williams9508a662012-01-18 20:47:01 -08006350void __ata_port_probe(struct ata_port *ap)
6351{
6352 struct ata_eh_info *ehi = &ap->link.eh_info;
6353 unsigned long flags;
6354
6355 /* kick EH for boot probing */
6356 spin_lock_irqsave(ap->lock, flags);
6357
6358 ehi->probe_mask |= ATA_ALL_DEVICES;
6359 ehi->action |= ATA_EH_RESET;
6360 ehi->flags |= ATA_EHI_NO_AUTOPSY | ATA_EHI_QUIET;
6361
6362 ap->pflags &= ~ATA_PFLAG_INITIALIZING;
6363 ap->pflags |= ATA_PFLAG_LOADING;
6364 ata_port_schedule_eh(ap);
6365
6366 spin_unlock_irqrestore(ap->lock, flags);
6367}
6368
James Bottomley238c9cf2011-01-23 08:28:33 -06006369int ata_port_probe(struct ata_port *ap)
Arjan van de Ven79318052009-01-04 05:32:28 -08006370{
James Bottomley238c9cf2011-01-23 08:28:33 -06006371 int rc = 0;
Arjan van de Ven886ad092009-01-09 15:54:07 -08006372
Arjan van de Ven79318052009-01-04 05:32:28 -08006373 if (ap->ops->error_handler) {
Dan Williams9508a662012-01-18 20:47:01 -08006374 __ata_port_probe(ap);
Arjan van de Ven79318052009-01-04 05:32:28 -08006375 ata_port_wait_eh(ap);
6376 } else {
6377 DPRINTK("ata%u: bus probe begin\n", ap->print_id);
6378 rc = ata_bus_probe(ap);
6379 DPRINTK("ata%u: bus probe end\n", ap->print_id);
Arjan van de Ven79318052009-01-04 05:32:28 -08006380 }
James Bottomley238c9cf2011-01-23 08:28:33 -06006381 return rc;
6382}
6383
6384
6385static void async_port_probe(void *data, async_cookie_t cookie)
6386{
6387 struct ata_port *ap = data;
Jeff Garzik4fca3772011-02-15 01:13:24 -05006388
James Bottomley238c9cf2011-01-23 08:28:33 -06006389 /*
6390 * If we're not allowed to scan this host in parallel,
6391 * we need to wait until all previous scans have completed
6392 * before going further.
6393 * Jeff Garzik says this is only within a controller, so we
6394 * don't need to wait for port 0, only for later ports.
6395 */
6396 if (!(ap->host->flags & ATA_HOST_PARALLEL_SCAN) && ap->port_no != 0)
6397 async_synchronize_cookie(cookie);
6398
6399 (void)ata_port_probe(ap);
Arjan van de Venf29d3b22009-01-05 15:07:07 -08006400
6401 /* in order to keep device order, we need to synchronize at this point */
6402 async_synchronize_cookie(cookie);
6403
6404 ata_scsi_scan_host(ap, 1);
Arjan van de Ven79318052009-01-04 05:32:28 -08006405}
James Bottomley238c9cf2011-01-23 08:28:33 -06006406
Brian Kingb03732f2006-08-07 14:27:10 -05006407/**
Tejun Heof3187192007-04-17 23:44:07 +09006408 * ata_host_register - register initialized ATA host
6409 * @host: ATA host to register
6410 * @sht: template for SCSI host
Jeff Garzik0cba6322005-05-30 19:49:12 -04006411 *
Tejun Heof3187192007-04-17 23:44:07 +09006412 * Register initialized ATA host. @host is allocated using
6413 * ata_host_alloc() and fully initialized by LLD. This function
6414 * starts ports, registers @host with ATA and SCSI layers and
6415 * probe registered devices.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006416 *
6417 * LOCKING:
Tejun Heof3187192007-04-17 23:44:07 +09006418 * Inherited from calling layer (may sleep).
Linus Torvalds1da177e2005-04-16 15:20:36 -07006419 *
6420 * RETURNS:
Tejun Heof3187192007-04-17 23:44:07 +09006421 * 0 on success, -errno otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006422 */
Tejun Heof3187192007-04-17 23:44:07 +09006423int ata_host_register(struct ata_host *host, struct scsi_host_template *sht)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006424{
Tejun Heof3187192007-04-17 23:44:07 +09006425 int i, rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006426
Jens Axboe69278f72018-05-11 12:51:10 -06006427 host->n_tags = clamp(sht->can_queue, 1, ATA_MAX_QUEUE);
Kevin Hao1871ee12014-07-12 12:08:24 +08006428
Tejun Heof3187192007-04-17 23:44:07 +09006429 /* host must have been started */
6430 if (!(host->flags & ATA_HOST_STARTED)) {
Joe Perchesa44fec12011-04-15 15:51:58 -07006431 dev_err(host->dev, "BUG: trying to register unstarted host\n");
Tejun Heof3187192007-04-17 23:44:07 +09006432 WARN_ON(1);
6433 return -EINVAL;
Alan Cox02f076a2006-09-26 17:35:32 +01006434 }
Tejun Heof0d36ef2007-01-20 16:00:28 +09006435
Tejun Heof3187192007-04-17 23:44:07 +09006436 /* Blow away unused ports. This happens when LLD can't
6437 * determine the exact number of ports to allocate at
6438 * allocation time.
6439 */
6440 for (i = host->n_ports; host->ports[i]; i++)
6441 kfree(host->ports[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006442
Tejun Heof3187192007-04-17 23:44:07 +09006443 /* give ports names and add SCSI hosts */
David Milburne628dc92013-05-14 13:48:40 -05006444 for (i = 0; i < host->n_ports; i++) {
Dan Williams85d67252012-03-10 23:28:46 -08006445 host->ports[i]->print_id = atomic_inc_return(&ata_print_id);
David Milburne628dc92013-05-14 13:48:40 -05006446 host->ports[i]->local_port_no = i + 1;
6447 }
Jeff Garzik4fca3772011-02-15 01:13:24 -05006448
Gwendal Grignoud9027472010-05-25 12:31:38 -07006449 /* Create associated sysfs transport objects */
6450 for (i = 0; i < host->n_ports; i++) {
6451 rc = ata_tport_add(host->dev,host->ports[i]);
6452 if (rc) {
6453 goto err_tadd;
6454 }
6455 }
6456
Tejun Heof3187192007-04-17 23:44:07 +09006457 rc = ata_scsi_add_hosts(host, sht);
Tejun Heoecef7252007-04-17 23:44:06 +09006458 if (rc)
Gwendal Grignoud9027472010-05-25 12:31:38 -07006459 goto err_tadd;
Tejun Heoecef7252007-04-17 23:44:06 +09006460
Tejun Heof3187192007-04-17 23:44:07 +09006461 /* set cable, sata_spd_limit and report */
Jeff Garzikcca39742006-08-24 03:19:22 -04006462 for (i = 0; i < host->n_ports; i++) {
6463 struct ata_port *ap = host->ports[i];
Tejun Heof3187192007-04-17 23:44:07 +09006464 unsigned long xfer_mask;
6465
6466 /* set SATA cable type if still unset */
6467 if (ap->cbl == ATA_CBL_NONE && (ap->flags & ATA_FLAG_SATA))
6468 ap->cbl = ATA_CBL_SATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006469
Tejun Heo5a04bf42006-05-31 18:27:38 +09006470 /* init sata_spd_limit to the current value */
Tejun Heo4fb37a22007-08-06 18:36:23 +09006471 sata_link_init_spd(&ap->link);
Tejun Heob1c72912008-07-31 17:02:43 +09006472 if (ap->slave_link)
6473 sata_link_init_spd(ap->slave_link);
Tejun Heo5a04bf42006-05-31 18:27:38 +09006474
Tejun Heocbcdd872007-08-18 13:14:55 +09006475 /* print per-port info to dmesg */
Tejun Heof3187192007-04-17 23:44:07 +09006476 xfer_mask = ata_pack_xfermask(ap->pio_mask, ap->mwdma_mask,
6477 ap->udma_mask);
6478
Tejun Heoabf6e8e2007-10-09 14:57:25 +09006479 if (!ata_port_is_dummy(ap)) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07006480 ata_port_info(ap, "%cATA max %s %s\n",
6481 (ap->flags & ATA_FLAG_SATA) ? 'S' : 'P',
6482 ata_mode_string(xfer_mask),
6483 ap->link.eh_info.desc);
Tejun Heoabf6e8e2007-10-09 14:57:25 +09006484 ata_ehi_clear_desc(&ap->link.eh_info);
6485 } else
Joe Perchesa9a79df2011-04-15 15:51:59 -07006486 ata_port_info(ap, "DUMMY\n");
Tejun Heof3187192007-04-17 23:44:07 +09006487 }
6488
Vegard Nossumf6005352009-04-08 18:19:39 +02006489 /* perform each probe asynchronously */
Tejun Heof3187192007-04-17 23:44:07 +09006490 for (i = 0; i < host->n_ports; i++) {
6491 struct ata_port *ap = host->ports[i];
Arjan van de Ven79318052009-01-04 05:32:28 -08006492 async_schedule(async_port_probe, ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006493 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006494
Tejun Heof3187192007-04-17 23:44:07 +09006495 return 0;
Gwendal Grignoud9027472010-05-25 12:31:38 -07006496
6497 err_tadd:
6498 while (--i >= 0) {
6499 ata_tport_delete(host->ports[i]);
6500 }
6501 return rc;
6502
Tejun Heof3187192007-04-17 23:44:07 +09006503}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01006504EXPORT_SYMBOL_GPL(ata_host_register);
Tejun Heof3187192007-04-17 23:44:07 +09006505
6506/**
Tejun Heof5cda252007-04-17 23:44:07 +09006507 * ata_host_activate - start host, request IRQ and register it
6508 * @host: target ATA host
6509 * @irq: IRQ to request
6510 * @irq_handler: irq_handler used when requesting IRQ
6511 * @irq_flags: irq_flags used when requesting IRQ
6512 * @sht: scsi_host_template to use when registering the host
6513 *
6514 * After allocating an ATA host and initializing it, most libata
6515 * LLDs perform three steps to activate the host - start host,
Masanari Iidac9b55602016-04-13 23:36:27 +09006516 * request IRQ and register it. This helper takes necessary
Tejun Heof5cda252007-04-17 23:44:07 +09006517 * arguments and performs the three steps in one go.
6518 *
Paul Mundt3d46b2e2007-11-08 11:14:56 +09006519 * An invalid IRQ skips the IRQ registration and expects the host to
6520 * have set polling mode on the port. In this case, @irq_handler
6521 * should be NULL.
6522 *
Tejun Heof5cda252007-04-17 23:44:07 +09006523 * LOCKING:
6524 * Inherited from calling layer (may sleep).
6525 *
6526 * RETURNS:
6527 * 0 on success, -errno otherwise.
6528 */
6529int ata_host_activate(struct ata_host *host, int irq,
6530 irq_handler_t irq_handler, unsigned long irq_flags,
6531 struct scsi_host_template *sht)
6532{
Tejun Heocbcdd872007-08-18 13:14:55 +09006533 int i, rc;
Heiner Kallweit7e22c002015-12-06 21:56:33 +01006534 char *irq_desc;
Tejun Heof5cda252007-04-17 23:44:07 +09006535
6536 rc = ata_host_start(host);
6537 if (rc)
6538 return rc;
6539
Paul Mundt3d46b2e2007-11-08 11:14:56 +09006540 /* Special case for polling mode */
6541 if (!irq) {
6542 WARN_ON(irq_handler);
6543 return ata_host_register(host, sht);
6544 }
6545
Heiner Kallweit7e22c002015-12-06 21:56:33 +01006546 irq_desc = devm_kasprintf(host->dev, GFP_KERNEL, "%s[%s]",
6547 dev_driver_string(host->dev),
6548 dev_name(host->dev));
6549 if (!irq_desc)
6550 return -ENOMEM;
6551
Tejun Heof5cda252007-04-17 23:44:07 +09006552 rc = devm_request_irq(host->dev, irq, irq_handler, irq_flags,
Heiner Kallweit7e22c002015-12-06 21:56:33 +01006553 irq_desc, host);
Tejun Heof5cda252007-04-17 23:44:07 +09006554 if (rc)
6555 return rc;
6556
Tejun Heocbcdd872007-08-18 13:14:55 +09006557 for (i = 0; i < host->n_ports; i++)
6558 ata_port_desc(host->ports[i], "irq %d", irq);
Tejun Heo40318262007-07-03 01:38:47 +09006559
Tejun Heof5cda252007-04-17 23:44:07 +09006560 rc = ata_host_register(host, sht);
6561 /* if failed, just free the IRQ and leave ports alone */
6562 if (rc)
6563 devm_free_irq(host->dev, irq, host);
6564
6565 return rc;
6566}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01006567EXPORT_SYMBOL_GPL(ata_host_activate);
Tejun Heof5cda252007-04-17 23:44:07 +09006568
6569/**
Masanari Iidac9b55602016-04-13 23:36:27 +09006570 * ata_port_detach - Detach ATA port in preparation of device removal
Tejun Heo720ba122006-05-31 18:28:13 +09006571 * @ap: ATA port to be detached
6572 *
6573 * Detach all ATA devices and the associated SCSI devices of @ap;
6574 * then, remove the associated SCSI host. @ap is guaranteed to
6575 * be quiescent on return from this function.
6576 *
6577 * LOCKING:
6578 * Kernel thread context (may sleep).
6579 */
Adrian Bunk741b7762007-10-24 18:23:06 +02006580static void ata_port_detach(struct ata_port *ap)
Tejun Heo720ba122006-05-31 18:28:13 +09006581{
6582 unsigned long flags;
Levente Kurusaa6f9bf42014-05-06 15:57:48 +02006583 struct ata_link *link;
6584 struct ata_device *dev;
Tejun Heo720ba122006-05-31 18:28:13 +09006585
6586 if (!ap->ops->error_handler)
Tejun Heoc3cf30a2006-08-05 03:59:11 +09006587 goto skip_eh;
Tejun Heo720ba122006-05-31 18:28:13 +09006588
6589 /* tell EH we're leaving & flush EH */
Jeff Garzikba6a1302006-06-22 23:46:10 -04006590 spin_lock_irqsave(ap->lock, flags);
Tejun Heob51e9e52006-06-29 01:29:30 +09006591 ap->pflags |= ATA_PFLAG_UNLOADING;
Tejun Heoece180d2008-11-03 20:04:37 +09006592 ata_port_schedule_eh(ap);
Jeff Garzikba6a1302006-06-22 23:46:10 -04006593 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heo720ba122006-05-31 18:28:13 +09006594
Tejun Heoece180d2008-11-03 20:04:37 +09006595 /* wait till EH commits suicide */
Tejun Heo720ba122006-05-31 18:28:13 +09006596 ata_port_wait_eh(ap);
6597
Tejun Heoece180d2008-11-03 20:04:37 +09006598 /* it better be dead now */
6599 WARN_ON(!(ap->pflags & ATA_PFLAG_UNLOADED));
Tejun Heo720ba122006-05-31 18:28:13 +09006600
Tejun Heoafe2c512010-12-14 16:21:17 +01006601 cancel_delayed_work_sync(&ap->hotplug_task);
Tejun Heo720ba122006-05-31 18:28:13 +09006602
Tejun Heoc3cf30a2006-08-05 03:59:11 +09006603 skip_eh:
Levente Kurusaa6f9bf42014-05-06 15:57:48 +02006604 /* clean up zpodd on port removal */
6605 ata_for_each_link(link, ap, HOST_FIRST) {
6606 ata_for_each_dev(dev, link, ALL) {
6607 if (zpodd_dev_enabled(dev))
6608 zpodd_exit(dev);
6609 }
6610 }
Gwendal Grignoud9027472010-05-25 12:31:38 -07006611 if (ap->pmp_link) {
6612 int i;
6613 for (i = 0; i < SATA_PMP_MAX_PORTS; i++)
6614 ata_tlink_delete(&ap->pmp_link[i]);
6615 }
Tejun Heo720ba122006-05-31 18:28:13 +09006616 /* remove the associated SCSI host */
Jeff Garzikcca39742006-08-24 03:19:22 -04006617 scsi_remove_host(ap->scsi_host);
Rafael J. Wysockic5700762013-11-25 13:19:01 +01006618 ata_tport_delete(ap);
Tejun Heo720ba122006-05-31 18:28:13 +09006619}
6620
6621/**
Tejun Heo0529c1592007-01-20 16:00:26 +09006622 * ata_host_detach - Detach all ports of an ATA host
6623 * @host: Host to detach
6624 *
6625 * Detach all ports of @host.
6626 *
6627 * LOCKING:
6628 * Kernel thread context (may sleep).
6629 */
6630void ata_host_detach(struct ata_host *host)
6631{
6632 int i;
6633
John Garry130f4ca2019-10-16 18:19:52 +08006634 /* Ensure ata_port probe has completed */
6635 async_synchronize_full();
6636
Tejun Heo0529c1592007-01-20 16:00:26 +09006637 for (i = 0; i < host->n_ports; i++)
6638 ata_port_detach(host->ports[i]);
Tejun Heo562f0c22007-12-15 15:05:01 +09006639
6640 /* the host is dead now, dissociate ACPI */
6641 ata_acpi_dissociate(host);
Tejun Heo0529c1592007-01-20 16:00:26 +09006642}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01006643EXPORT_SYMBOL_GPL(ata_host_detach);
Tejun Heo0529c1592007-01-20 16:00:26 +09006644
Jeff Garzik374b1872005-08-30 05:42:52 -04006645#ifdef CONFIG_PCI
6646
Edward Falk0baab862005-06-02 18:17:13 -04006647/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07006648 * ata_pci_remove_one - PCI layer callback for device removal
6649 * @pdev: PCI device that was removed
6650 *
Tejun Heob878ca52007-01-20 16:00:28 +09006651 * PCI layer indicates to libata via this hook that hot-unplug or
6652 * module unload event has occurred. Detach all ports. Resource
6653 * release is handled via devres.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006654 *
6655 * LOCKING:
6656 * Inherited from PCI layer (may sleep).
6657 */
Tejun Heof0d36ef2007-01-20 16:00:28 +09006658void ata_pci_remove_one(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006659{
Brian Norris04a3f5b2012-12-03 10:34:41 -08006660 struct ata_host *host = pci_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006661
Tejun Heob878ca52007-01-20 16:00:28 +09006662 ata_host_detach(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006663}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01006664EXPORT_SYMBOL_GPL(ata_pci_remove_one);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006665
Prabhakar Kushwaha10a663a2020-01-25 03:37:29 +00006666void ata_pci_shutdown_one(struct pci_dev *pdev)
6667{
6668 struct ata_host *host = pci_get_drvdata(pdev);
6669 int i;
6670
6671 for (i = 0; i < host->n_ports; i++) {
6672 struct ata_port *ap = host->ports[i];
6673
6674 ap->pflags |= ATA_PFLAG_FROZEN;
6675
6676 /* Disable port interrupts */
6677 if (ap->ops->freeze)
6678 ap->ops->freeze(ap);
6679
6680 /* Stop the port DMA engines */
6681 if (ap->ops->port_stop)
6682 ap->ops->port_stop(ap);
6683 }
6684}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01006685EXPORT_SYMBOL_GPL(ata_pci_shutdown_one);
Prabhakar Kushwaha10a663a2020-01-25 03:37:29 +00006686
Linus Torvalds1da177e2005-04-16 15:20:36 -07006687/* move to PCI subsystem */
Jeff Garzik057ace52005-10-22 14:27:05 -04006688int pci_test_config_bits(struct pci_dev *pdev, const struct pci_bits *bits)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006689{
6690 unsigned long tmp = 0;
6691
6692 switch (bits->width) {
6693 case 1: {
6694 u8 tmp8 = 0;
6695 pci_read_config_byte(pdev, bits->reg, &tmp8);
6696 tmp = tmp8;
6697 break;
6698 }
6699 case 2: {
6700 u16 tmp16 = 0;
6701 pci_read_config_word(pdev, bits->reg, &tmp16);
6702 tmp = tmp16;
6703 break;
6704 }
6705 case 4: {
6706 u32 tmp32 = 0;
6707 pci_read_config_dword(pdev, bits->reg, &tmp32);
6708 tmp = tmp32;
6709 break;
6710 }
6711
6712 default:
6713 return -EINVAL;
6714 }
6715
6716 tmp &= bits->mask;
6717
6718 return (tmp == bits->val) ? 1 : 0;
6719}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01006720EXPORT_SYMBOL_GPL(pci_test_config_bits);
Jens Axboe9b847542006-01-06 09:28:07 +01006721
Tejun Heo6ffa01d2007-03-02 17:32:47 +09006722#ifdef CONFIG_PM
Tejun Heo3c5100c2006-07-26 16:58:33 +09006723void ata_pci_device_do_suspend(struct pci_dev *pdev, pm_message_t mesg)
Jens Axboe9b847542006-01-06 09:28:07 +01006724{
6725 pci_save_state(pdev);
Tejun Heo4c90d972007-02-20 18:14:48 +09006726 pci_disable_device(pdev);
Tejun Heo500530f2006-07-03 16:07:27 +09006727
Rafael J. Wysocki3a2d5b72008-02-23 19:13:25 +01006728 if (mesg.event & PM_EVENT_SLEEP)
Tejun Heo500530f2006-07-03 16:07:27 +09006729 pci_set_power_state(pdev, PCI_D3hot);
Jens Axboe9b847542006-01-06 09:28:07 +01006730}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01006731EXPORT_SYMBOL_GPL(ata_pci_device_do_suspend);
Jens Axboe9b847542006-01-06 09:28:07 +01006732
Tejun Heo553c4aa2006-12-26 19:39:50 +09006733int ata_pci_device_do_resume(struct pci_dev *pdev)
Jens Axboe9b847542006-01-06 09:28:07 +01006734{
Tejun Heo553c4aa2006-12-26 19:39:50 +09006735 int rc;
6736
Jens Axboe9b847542006-01-06 09:28:07 +01006737 pci_set_power_state(pdev, PCI_D0);
6738 pci_restore_state(pdev);
Tejun Heo553c4aa2006-12-26 19:39:50 +09006739
Tejun Heob878ca52007-01-20 16:00:28 +09006740 rc = pcim_enable_device(pdev);
Tejun Heo553c4aa2006-12-26 19:39:50 +09006741 if (rc) {
Joe Perchesa44fec12011-04-15 15:51:58 -07006742 dev_err(&pdev->dev,
6743 "failed to enable device after resume (%d)\n", rc);
Tejun Heo553c4aa2006-12-26 19:39:50 +09006744 return rc;
6745 }
6746
Jens Axboe9b847542006-01-06 09:28:07 +01006747 pci_set_master(pdev);
Tejun Heo553c4aa2006-12-26 19:39:50 +09006748 return 0;
Tejun Heo500530f2006-07-03 16:07:27 +09006749}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01006750EXPORT_SYMBOL_GPL(ata_pci_device_do_resume);
Tejun Heo500530f2006-07-03 16:07:27 +09006751
Tejun Heo3c5100c2006-07-26 16:58:33 +09006752int ata_pci_device_suspend(struct pci_dev *pdev, pm_message_t mesg)
Tejun Heo500530f2006-07-03 16:07:27 +09006753{
Brian Norris04a3f5b2012-12-03 10:34:41 -08006754 struct ata_host *host = pci_get_drvdata(pdev);
Tejun Heo500530f2006-07-03 16:07:27 +09006755 int rc = 0;
6756
Jeff Garzikcca39742006-08-24 03:19:22 -04006757 rc = ata_host_suspend(host, mesg);
Tejun Heo500530f2006-07-03 16:07:27 +09006758 if (rc)
6759 return rc;
6760
Tejun Heo3c5100c2006-07-26 16:58:33 +09006761 ata_pci_device_do_suspend(pdev, mesg);
Tejun Heo500530f2006-07-03 16:07:27 +09006762
6763 return 0;
6764}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01006765EXPORT_SYMBOL_GPL(ata_pci_device_suspend);
Tejun Heo500530f2006-07-03 16:07:27 +09006766
6767int ata_pci_device_resume(struct pci_dev *pdev)
6768{
Brian Norris04a3f5b2012-12-03 10:34:41 -08006769 struct ata_host *host = pci_get_drvdata(pdev);
Tejun Heo553c4aa2006-12-26 19:39:50 +09006770 int rc;
Tejun Heo500530f2006-07-03 16:07:27 +09006771
Tejun Heo553c4aa2006-12-26 19:39:50 +09006772 rc = ata_pci_device_do_resume(pdev);
6773 if (rc == 0)
6774 ata_host_resume(host);
6775 return rc;
Jens Axboe9b847542006-01-06 09:28:07 +01006776}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01006777EXPORT_SYMBOL_GPL(ata_pci_device_resume);
Tejun Heo6ffa01d2007-03-02 17:32:47 +09006778#endif /* CONFIG_PM */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006779#endif /* CONFIG_PCI */
6780
Brian Norrisb7db04d2012-11-02 12:29:32 -07006781/**
6782 * ata_platform_remove_one - Platform layer callback for device removal
6783 * @pdev: Platform device that was removed
6784 *
6785 * Platform layer indicates to libata via this hook that hot-unplug or
6786 * module unload event has occurred. Detach all ports. Resource
6787 * release is handled via devres.
6788 *
6789 * LOCKING:
6790 * Inherited from platform layer (may sleep).
6791 */
6792int ata_platform_remove_one(struct platform_device *pdev)
6793{
6794 struct ata_host *host = platform_get_drvdata(pdev);
6795
6796 ata_host_detach(host);
6797
6798 return 0;
6799}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01006800EXPORT_SYMBOL_GPL(ata_platform_remove_one);
Brian Norrisb7db04d2012-11-02 12:29:32 -07006801
Tejun Heo33267322008-02-13 09:15:09 +09006802static int __init ata_parse_force_one(char **cur,
6803 struct ata_force_ent *force_ent,
6804 const char **reason)
6805{
Rasmus Villemoes0f5f2642015-06-09 15:33:19 +02006806 static const struct ata_force_param force_tbl[] __initconst = {
Tejun Heo33267322008-02-13 09:15:09 +09006807 { "40c", .cbl = ATA_CBL_PATA40 },
6808 { "80c", .cbl = ATA_CBL_PATA80 },
6809 { "short40c", .cbl = ATA_CBL_PATA40_SHORT },
6810 { "unk", .cbl = ATA_CBL_PATA_UNK },
6811 { "ign", .cbl = ATA_CBL_PATA_IGN },
6812 { "sata", .cbl = ATA_CBL_SATA },
6813 { "1.5Gbps", .spd_limit = 1 },
6814 { "3.0Gbps", .spd_limit = 2 },
6815 { "noncq", .horkage_on = ATA_HORKAGE_NONCQ },
6816 { "ncq", .horkage_off = ATA_HORKAGE_NONCQ },
Martin K. Petersend7b16e42015-05-04 21:54:18 -04006817 { "noncqtrim", .horkage_on = ATA_HORKAGE_NO_NCQ_TRIM },
6818 { "ncqtrim", .horkage_off = ATA_HORKAGE_NO_NCQ_TRIM },
Tejun Heo43c9c592010-05-23 12:59:11 +02006819 { "dump_id", .horkage_on = ATA_HORKAGE_DUMP_ID },
Tejun Heo33267322008-02-13 09:15:09 +09006820 { "pio0", .xfer_mask = 1 << (ATA_SHIFT_PIO + 0) },
6821 { "pio1", .xfer_mask = 1 << (ATA_SHIFT_PIO + 1) },
6822 { "pio2", .xfer_mask = 1 << (ATA_SHIFT_PIO + 2) },
6823 { "pio3", .xfer_mask = 1 << (ATA_SHIFT_PIO + 3) },
6824 { "pio4", .xfer_mask = 1 << (ATA_SHIFT_PIO + 4) },
6825 { "pio5", .xfer_mask = 1 << (ATA_SHIFT_PIO + 5) },
6826 { "pio6", .xfer_mask = 1 << (ATA_SHIFT_PIO + 6) },
6827 { "mwdma0", .xfer_mask = 1 << (ATA_SHIFT_MWDMA + 0) },
6828 { "mwdma1", .xfer_mask = 1 << (ATA_SHIFT_MWDMA + 1) },
6829 { "mwdma2", .xfer_mask = 1 << (ATA_SHIFT_MWDMA + 2) },
6830 { "mwdma3", .xfer_mask = 1 << (ATA_SHIFT_MWDMA + 3) },
6831 { "mwdma4", .xfer_mask = 1 << (ATA_SHIFT_MWDMA + 4) },
6832 { "udma0", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 0) },
6833 { "udma16", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 0) },
6834 { "udma/16", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 0) },
6835 { "udma1", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 1) },
6836 { "udma25", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 1) },
6837 { "udma/25", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 1) },
6838 { "udma2", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 2) },
6839 { "udma33", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 2) },
6840 { "udma/33", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 2) },
6841 { "udma3", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 3) },
6842 { "udma44", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 3) },
6843 { "udma/44", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 3) },
6844 { "udma4", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 4) },
6845 { "udma66", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 4) },
6846 { "udma/66", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 4) },
6847 { "udma5", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 5) },
6848 { "udma100", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 5) },
6849 { "udma/100", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 5) },
6850 { "udma6", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 6) },
6851 { "udma133", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 6) },
6852 { "udma/133", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 6) },
6853 { "udma7", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 7) },
Tejun Heo05944bd2008-08-13 20:19:09 +09006854 { "nohrst", .lflags = ATA_LFLAG_NO_HRST },
6855 { "nosrst", .lflags = ATA_LFLAG_NO_SRST },
6856 { "norst", .lflags = ATA_LFLAG_NO_HRST | ATA_LFLAG_NO_SRST },
Dan Williamsca6d43b2012-06-21 23:41:41 -07006857 { "rstonce", .lflags = ATA_LFLAG_RST_ONCE },
Vincent Pelletier966fbe12013-05-21 22:30:58 +02006858 { "atapi_dmadir", .horkage_on = ATA_HORKAGE_ATAPI_DMADIR },
Robin H. Johnsonb8bd6dc2013-12-16 09:31:19 -08006859 { "disable", .horkage_on = ATA_HORKAGE_DISABLE },
Tejun Heo33267322008-02-13 09:15:09 +09006860 };
6861 char *start = *cur, *p = *cur;
6862 char *id, *val, *endp;
6863 const struct ata_force_param *match_fp = NULL;
6864 int nr_matches = 0, i;
6865
6866 /* find where this param ends and update *cur */
6867 while (*p != '\0' && *p != ',')
6868 p++;
6869
6870 if (*p == '\0')
6871 *cur = p;
6872 else
6873 *cur = p + 1;
6874
6875 *p = '\0';
6876
6877 /* parse */
6878 p = strchr(start, ':');
6879 if (!p) {
6880 val = strstrip(start);
6881 goto parse_val;
6882 }
6883 *p = '\0';
6884
6885 id = strstrip(start);
6886 val = strstrip(p + 1);
6887
6888 /* parse id */
6889 p = strchr(id, '.');
6890 if (p) {
6891 *p++ = '\0';
6892 force_ent->device = simple_strtoul(p, &endp, 10);
6893 if (p == endp || *endp != '\0') {
6894 *reason = "invalid device";
6895 return -EINVAL;
6896 }
6897 }
6898
6899 force_ent->port = simple_strtoul(id, &endp, 10);
Tejun Heof7cf69a2017-05-31 14:26:26 -04006900 if (id == endp || *endp != '\0') {
Tejun Heo33267322008-02-13 09:15:09 +09006901 *reason = "invalid port/link";
6902 return -EINVAL;
6903 }
6904
6905 parse_val:
6906 /* parse val, allow shortcuts so that both 1.5 and 1.5Gbps work */
6907 for (i = 0; i < ARRAY_SIZE(force_tbl); i++) {
6908 const struct ata_force_param *fp = &force_tbl[i];
6909
6910 if (strncasecmp(val, fp->name, strlen(val)))
6911 continue;
6912
6913 nr_matches++;
6914 match_fp = fp;
6915
6916 if (strcasecmp(val, fp->name) == 0) {
6917 nr_matches = 1;
6918 break;
6919 }
6920 }
6921
6922 if (!nr_matches) {
6923 *reason = "unknown value";
6924 return -EINVAL;
6925 }
6926 if (nr_matches > 1) {
Arvind Yadav9de55352017-10-28 23:51:47 +05306927 *reason = "ambiguous value";
Tejun Heo33267322008-02-13 09:15:09 +09006928 return -EINVAL;
6929 }
6930
6931 force_ent->param = *match_fp;
6932
6933 return 0;
6934}
6935
6936static void __init ata_parse_force_param(void)
6937{
6938 int idx = 0, size = 1;
6939 int last_port = -1, last_device = -1;
6940 char *p, *cur, *next;
6941
6942 /* calculate maximum number of params and allocate force_tbl */
6943 for (p = ata_force_param_buf; *p; p++)
6944 if (*p == ',')
6945 size++;
6946
Kees Cook6396bb22018-06-12 14:03:40 -07006947 ata_force_tbl = kcalloc(size, sizeof(ata_force_tbl[0]), GFP_KERNEL);
Tejun Heo33267322008-02-13 09:15:09 +09006948 if (!ata_force_tbl) {
6949 printk(KERN_WARNING "ata: failed to extend force table, "
6950 "libata.force ignored\n");
6951 return;
6952 }
6953
6954 /* parse and populate the table */
6955 for (cur = ata_force_param_buf; *cur != '\0'; cur = next) {
6956 const char *reason = "";
6957 struct ata_force_ent te = { .port = -1, .device = -1 };
6958
6959 next = cur;
6960 if (ata_parse_force_one(&next, &te, &reason)) {
6961 printk(KERN_WARNING "ata: failed to parse force "
6962 "parameter \"%s\" (%s)\n",
6963 cur, reason);
6964 continue;
6965 }
6966
6967 if (te.port == -1) {
6968 te.port = last_port;
6969 te.device = last_device;
6970 }
6971
6972 ata_force_tbl[idx++] = te;
6973
6974 last_port = te.port;
6975 last_device = te.device;
6976 }
6977
6978 ata_force_tbl_size = idx;
6979}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006980
Linus Torvalds1da177e2005-04-16 15:20:36 -07006981static int __init ata_init(void)
6982{
Gwendal Grignoud9027472010-05-25 12:31:38 -07006983 int rc;
Tejun Heo270390e2010-05-10 21:41:35 +02006984
Tejun Heo33267322008-02-13 09:15:09 +09006985 ata_parse_force_param();
6986
Tejun Heo270390e2010-05-10 21:41:35 +02006987 rc = ata_sff_init();
Tejun Heoad72cf92010-07-02 10:03:52 +02006988 if (rc) {
6989 kfree(ata_force_tbl);
6990 return rc;
6991 }
Tejun Heo453b07a2006-05-31 18:27:42 +09006992
Gwendal Grignoud9027472010-05-25 12:31:38 -07006993 libata_transport_init();
6994 ata_scsi_transport_template = ata_attach_transport();
6995 if (!ata_scsi_transport_template) {
6996 ata_sff_exit();
6997 rc = -ENOMEM;
6998 goto err_out;
Jeff Garzik4fca3772011-02-15 01:13:24 -05006999 }
Gwendal Grignoud9027472010-05-25 12:31:38 -07007000
Linus Torvalds1da177e2005-04-16 15:20:36 -07007001 printk(KERN_DEBUG "libata version " DRV_VERSION " loaded.\n");
7002 return 0;
Gwendal Grignoud9027472010-05-25 12:31:38 -07007003
7004err_out:
7005 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007006}
7007
7008static void __exit ata_exit(void)
7009{
Gwendal Grignoud9027472010-05-25 12:31:38 -07007010 ata_release_transport(ata_scsi_transport_template);
7011 libata_transport_exit();
Tejun Heo270390e2010-05-10 21:41:35 +02007012 ata_sff_exit();
Tejun Heo33267322008-02-13 09:15:09 +09007013 kfree(ata_force_tbl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007014}
7015
Brian Kinga4625082006-11-13 16:32:36 -06007016subsys_initcall(ata_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007017module_exit(ata_exit);
7018
Akinobu Mita9990b6f2010-04-12 21:11:41 +09007019static DEFINE_RATELIMIT_STATE(ratelimit, HZ / 5, 1);
Jeff Garzik67846b32005-10-05 02:58:32 -04007020
7021int ata_ratelimit(void)
7022{
Akinobu Mita9990b6f2010-04-12 21:11:41 +09007023 return __ratelimit(&ratelimit);
Jeff Garzik67846b32005-10-05 02:58:32 -04007024}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01007025EXPORT_SYMBOL_GPL(ata_ratelimit);
Jeff Garzik67846b32005-10-05 02:58:32 -04007026
Tejun Heoc0c362b2010-09-06 17:57:14 +02007027/**
7028 * ata_msleep - ATA EH owner aware msleep
7029 * @ap: ATA port to attribute the sleep to
7030 * @msecs: duration to sleep in milliseconds
7031 *
7032 * Sleeps @msecs. If the current task is owner of @ap's EH, the
7033 * ownership is released before going to sleep and reacquired
7034 * after the sleep is complete. IOW, other ports sharing the
7035 * @ap->host will be allowed to own the EH while this task is
7036 * sleeping.
7037 *
7038 * LOCKING:
7039 * Might sleep.
7040 */
Tejun Heo97750ce2010-09-06 17:56:29 +02007041void ata_msleep(struct ata_port *ap, unsigned int msecs)
7042{
Tejun Heoc0c362b2010-09-06 17:57:14 +02007043 bool owns_eh = ap && ap->host->eh_owner == current;
7044
7045 if (owns_eh)
7046 ata_eh_release(ap);
7047
Anil Veliyankara Madam848c3922016-01-07 21:18:52 -08007048 if (msecs < 20) {
7049 unsigned long usecs = msecs * USEC_PER_MSEC;
7050 usleep_range(usecs, usecs + 50);
7051 } else {
7052 msleep(msecs);
7053 }
Tejun Heoc0c362b2010-09-06 17:57:14 +02007054
7055 if (owns_eh)
7056 ata_eh_acquire(ap);
Tejun Heo97750ce2010-09-06 17:56:29 +02007057}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01007058EXPORT_SYMBOL_GPL(ata_msleep);
Tejun Heo97750ce2010-09-06 17:56:29 +02007059
Tejun Heoc22daff2006-04-11 22:22:29 +09007060/**
7061 * ata_wait_register - wait until register value changes
Tejun Heo97750ce2010-09-06 17:56:29 +02007062 * @ap: ATA port to wait register for, can be NULL
Tejun Heoc22daff2006-04-11 22:22:29 +09007063 * @reg: IO-mapped register
7064 * @mask: Mask to apply to read register value
7065 * @val: Wait condition
Tejun Heo341c2c92008-05-20 02:17:51 +09007066 * @interval: polling interval in milliseconds
7067 * @timeout: timeout in milliseconds
Tejun Heoc22daff2006-04-11 22:22:29 +09007068 *
7069 * Waiting for some bits of register to change is a common
7070 * operation for ATA controllers. This function reads 32bit LE
7071 * IO-mapped register @reg and tests for the following condition.
7072 *
7073 * (*@reg & mask) != val
7074 *
7075 * If the condition is met, it returns; otherwise, the process is
7076 * repeated after @interval_msec until timeout.
7077 *
7078 * LOCKING:
7079 * Kernel thread context (may sleep)
7080 *
7081 * RETURNS:
7082 * The final register value.
7083 */
Tejun Heo97750ce2010-09-06 17:56:29 +02007084u32 ata_wait_register(struct ata_port *ap, void __iomem *reg, u32 mask, u32 val,
Tejun Heo341c2c92008-05-20 02:17:51 +09007085 unsigned long interval, unsigned long timeout)
Tejun Heoc22daff2006-04-11 22:22:29 +09007086{
Tejun Heo341c2c92008-05-20 02:17:51 +09007087 unsigned long deadline;
Tejun Heoc22daff2006-04-11 22:22:29 +09007088 u32 tmp;
7089
7090 tmp = ioread32(reg);
7091
7092 /* Calculate timeout _after_ the first read to make sure
7093 * preceding writes reach the controller before starting to
7094 * eat away the timeout.
7095 */
Tejun Heo341c2c92008-05-20 02:17:51 +09007096 deadline = ata_deadline(jiffies, timeout);
Tejun Heoc22daff2006-04-11 22:22:29 +09007097
Tejun Heo341c2c92008-05-20 02:17:51 +09007098 while ((tmp & mask) == val && time_before(jiffies, deadline)) {
Tejun Heo97750ce2010-09-06 17:56:29 +02007099 ata_msleep(ap, interval);
Tejun Heoc22daff2006-04-11 22:22:29 +09007100 tmp = ioread32(reg);
7101 }
7102
7103 return tmp;
7104}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01007105EXPORT_SYMBOL_GPL(ata_wait_register);
Tejun Heoc22daff2006-04-11 22:22:29 +09007106
Gabriele Mazzotta8393b812015-04-25 19:52:36 +02007107/**
7108 * sata_lpm_ignore_phy_events - test if PHY event should be ignored
7109 * @link: Link receiving the event
7110 *
7111 * Test whether the received PHY event has to be ignored or not.
7112 *
7113 * LOCKING:
7114 * None:
7115 *
7116 * RETURNS:
7117 * True if the event has to be ignored.
7118 */
7119bool sata_lpm_ignore_phy_events(struct ata_link *link)
7120{
Gabriele Mazzotta09c5b482015-04-25 19:52:37 +02007121 unsigned long lpm_timeout = link->last_lpm_change +
7122 msecs_to_jiffies(ATA_TMOUT_SPURIOUS_PHY);
7123
Gabriele Mazzotta8393b812015-04-25 19:52:36 +02007124 /* if LPM is enabled, PHYRDY doesn't mean anything */
Gabriele Mazzotta09c5b482015-04-25 19:52:37 +02007125 if (link->lpm_policy > ATA_LPM_MAX_POWER)
7126 return true;
7127
7128 /* ignore the first PHY event after the LPM policy changed
7129 * as it is might be spurious
7130 */
7131 if ((link->flags & ATA_LFLAG_CHANGED) &&
7132 time_before(jiffies, lpm_timeout))
7133 return true;
7134
7135 return false;
Gabriele Mazzotta8393b812015-04-25 19:52:36 +02007136}
7137EXPORT_SYMBOL_GPL(sata_lpm_ignore_phy_events);
7138
Linus Torvalds1da177e2005-04-16 15:20:36 -07007139/*
Tejun Heodd5b06c2006-08-10 16:59:12 +09007140 * Dummy port_ops
7141 */
Tejun Heodd5b06c2006-08-10 16:59:12 +09007142static unsigned int ata_dummy_qc_issue(struct ata_queued_cmd *qc)
7143{
7144 return AC_ERR_SYSTEM;
7145}
7146
Tejun Heo182d7bb2008-04-07 22:47:21 +09007147static void ata_dummy_error_handler(struct ata_port *ap)
7148{
7149 /* truly dummy */
7150}
7151
Tejun Heo029cfd62008-03-25 12:22:49 +09007152struct ata_port_operations ata_dummy_port_ops = {
Tejun Heodd5b06c2006-08-10 16:59:12 +09007153 .qc_prep = ata_noop_qc_prep,
7154 .qc_issue = ata_dummy_qc_issue,
Tejun Heo182d7bb2008-04-07 22:47:21 +09007155 .error_handler = ata_dummy_error_handler,
Dan Williamse4a9c372012-06-21 23:25:27 -07007156 .sched_eh = ata_std_sched_eh,
7157 .end_eh = ata_std_end_eh,
Tejun Heodd5b06c2006-08-10 16:59:12 +09007158};
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01007159EXPORT_SYMBOL_GPL(ata_dummy_port_ops);
Tejun Heodd5b06c2006-08-10 16:59:12 +09007160
Tejun Heo21b0ad42007-04-17 23:44:07 +09007161const struct ata_port_info ata_dummy_port_info = {
7162 .port_ops = &ata_dummy_port_ops,
7163};
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01007164EXPORT_SYMBOL_GPL(ata_dummy_port_info);
Tejun Heo21b0ad42007-04-17 23:44:07 +09007165
Tejun Heodd5b06c2006-08-10 16:59:12 +09007166/*
Joe Perchesa9a79df2011-04-15 15:51:59 -07007167 * Utility print functions
7168 */
Joe Perchesd7bead12014-09-22 09:52:18 -07007169void ata_port_printk(const struct ata_port *ap, const char *level,
7170 const char *fmt, ...)
Joe Perchesa9a79df2011-04-15 15:51:59 -07007171{
7172 struct va_format vaf;
7173 va_list args;
Joe Perchesa9a79df2011-04-15 15:51:59 -07007174
7175 va_start(args, fmt);
7176
7177 vaf.fmt = fmt;
7178 vaf.va = &args;
7179
Joe Perchesd7bead12014-09-22 09:52:18 -07007180 printk("%sata%u: %pV", level, ap->print_id, &vaf);
Joe Perchesa9a79df2011-04-15 15:51:59 -07007181
7182 va_end(args);
Joe Perchesa9a79df2011-04-15 15:51:59 -07007183}
7184EXPORT_SYMBOL(ata_port_printk);
7185
Joe Perchesd7bead12014-09-22 09:52:18 -07007186void ata_link_printk(const struct ata_link *link, const char *level,
7187 const char *fmt, ...)
Joe Perchesa9a79df2011-04-15 15:51:59 -07007188{
7189 struct va_format vaf;
7190 va_list args;
Joe Perchesa9a79df2011-04-15 15:51:59 -07007191
7192 va_start(args, fmt);
7193
7194 vaf.fmt = fmt;
7195 vaf.va = &args;
7196
7197 if (sata_pmp_attached(link->ap) || link->ap->slave_link)
Joe Perchesd7bead12014-09-22 09:52:18 -07007198 printk("%sata%u.%02u: %pV",
7199 level, link->ap->print_id, link->pmp, &vaf);
Joe Perchesa9a79df2011-04-15 15:51:59 -07007200 else
Joe Perchesd7bead12014-09-22 09:52:18 -07007201 printk("%sata%u: %pV",
7202 level, link->ap->print_id, &vaf);
Joe Perchesa9a79df2011-04-15 15:51:59 -07007203
7204 va_end(args);
Joe Perchesa9a79df2011-04-15 15:51:59 -07007205}
7206EXPORT_SYMBOL(ata_link_printk);
7207
Joe Perchesd7bead12014-09-22 09:52:18 -07007208void ata_dev_printk(const struct ata_device *dev, const char *level,
Joe Perchesa9a79df2011-04-15 15:51:59 -07007209 const char *fmt, ...)
7210{
7211 struct va_format vaf;
7212 va_list args;
Joe Perchesa9a79df2011-04-15 15:51:59 -07007213
7214 va_start(args, fmt);
7215
7216 vaf.fmt = fmt;
7217 vaf.va = &args;
7218
Joe Perchesd7bead12014-09-22 09:52:18 -07007219 printk("%sata%u.%02u: %pV",
7220 level, dev->link->ap->print_id, dev->link->pmp + dev->devno,
7221 &vaf);
Joe Perchesa9a79df2011-04-15 15:51:59 -07007222
7223 va_end(args);
Joe Perchesa9a79df2011-04-15 15:51:59 -07007224}
7225EXPORT_SYMBOL(ata_dev_printk);
7226
Joe Perches06296a12011-04-15 15:52:00 -07007227void ata_print_version(const struct device *dev, const char *version)
7228{
7229 dev_printk(KERN_DEBUG, dev, "version %s\n", version);
7230}
7231EXPORT_SYMBOL(ata_print_version);