blob: aba0c67d1bd6563d34250e4b8b11114612e91dcf [file] [log] [blame]
Thomas Gleixnerc82ee6d2019-05-19 15:51:48 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Jeff Garzikaf36d7f2005-08-28 20:18:39 -04003 * libata-core.c - helper library for ATA
4 *
Jeff Garzikaf36d7f2005-08-28 20:18:39 -04005 * Copyright 2003-2004 Red Hat, Inc. All rights reserved.
6 * Copyright 2003-2004 Jeff Garzik
7 *
Jeff Garzikaf36d7f2005-08-28 20:18:39 -04008 * libata documentation is available via 'make {ps|pdf}docs',
Mauro Carvalho Chehab19285f32017-05-14 11:52:56 -03009 * as Documentation/driver-api/libata.rst
Jeff Garzikaf36d7f2005-08-28 20:18:39 -040010 *
11 * Hardware documentation available from http://www.t13.org/ and
12 * http://www.sata-io.org/
13 *
Alan Cox92c52c52007-11-19 14:30:16 +000014 * Standards documents from:
15 * http://www.t13.org (ATA standards, PCI DMA IDE spec)
16 * http://www.t10.org (SCSI MMC - for ATAPI MMC)
17 * http://www.sata-io.org (SATA)
18 * http://www.compactflash.org (CF)
19 * http://www.qic.org (QIC157 - Tape and DSC)
20 * http://www.ce-ata.org (CE-ATA: not supported)
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +010021 *
22 * libata is essentially a library of internal helper functions for
23 * low-level ATA host controller drivers. As such, the API/ABI is
24 * likely to change as new drivers are added and updated.
25 * Do not depend on ABI/API stability.
Linus Torvalds1da177e2005-04-16 15:20:36 -070026 */
27
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/kernel.h>
29#include <linux/module.h>
30#include <linux/pci.h>
31#include <linux/init.h>
32#include <linux/list.h>
33#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <linux/spinlock.h>
35#include <linux/blkdev.h>
36#include <linux/delay.h>
37#include <linux/timer.h>
Anil Veliyankara Madam848c3922016-01-07 21:18:52 -080038#include <linux/time.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/interrupt.h>
40#include <linux/completion.h>
41#include <linux/suspend.h>
42#include <linux/workqueue.h>
David Hardeman378f0582005-09-17 17:55:31 +100043#include <linux/scatterlist.h>
Jeff Garzik2dcb4072007-10-19 06:42:56 -040044#include <linux/io.h>
Mark Lorde18086d2009-03-19 13:32:21 -040045#include <linux/log2.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090046#include <linux/slab.h>
George Spelvin428ac5f2014-08-06 16:09:27 -070047#include <linux/glob.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <scsi/scsi.h>
Jeff Garzik193515d2005-11-07 00:59:37 -050049#include <scsi/scsi_cmnd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#include <scsi/scsi_host.h>
51#include <linux/libata.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <asm/byteorder.h>
Hannes Reineckefe5af0c2016-04-25 12:45:48 +020053#include <asm/unaligned.h>
Tejun Heo140b5e592007-12-12 12:21:52 +090054#include <linux/cdrom.h>
Akinobu Mita9990b6f2010-04-12 21:11:41 +090055#include <linux/ratelimit.h>
Stephan Linzeb25cb92016-06-10 07:59:56 +020056#include <linux/leds.h>
Lin Ming9ee4f392011-12-05 09:20:28 +080057#include <linux/pm_runtime.h>
Brian Norrisb7db04d2012-11-02 12:29:32 -070058#include <linux/platform_device.h>
Bartlomiej Zolnierkiewiczbbf5a092020-03-26 16:58:01 +010059#include <asm/setup.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
Hannes Reinecke255c03d2015-03-27 16:46:38 +010061#define CREATE_TRACE_POINTS
62#include <trace/events/libata.h>
63
Linus Torvalds1da177e2005-04-16 15:20:36 -070064#include "libata.h"
Gwendal Grignoud9027472010-05-25 12:31:38 -070065#include "libata-transport.h"
Jeff Garzikfda0efc2007-01-31 07:43:15 -050066
Tejun Heo029cfd62008-03-25 12:22:49 +090067const struct ata_port_operations ata_base_port_ops = {
Tejun Heo0aa11132008-04-07 22:47:18 +090068 .prereset = ata_std_prereset,
Tejun Heo203c75b2008-04-07 22:47:18 +090069 .postreset = ata_std_postreset,
Tejun Heoa1efdab2008-03-25 12:22:50 +090070 .error_handler = ata_std_error_handler,
Dan Williamse4a9c372012-06-21 23:25:27 -070071 .sched_eh = ata_std_sched_eh,
72 .end_eh = ata_std_end_eh,
Tejun Heo029cfd62008-03-25 12:22:49 +090073};
74
75const struct ata_port_operations sata_port_ops = {
76 .inherits = &ata_base_port_ops,
77
78 .qc_defer = ata_std_qc_defer,
Tejun Heo57c9efd2008-04-07 22:47:19 +090079 .hardreset = sata_std_hardreset,
Tejun Heo029cfd62008-03-25 12:22:49 +090080};
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +010081EXPORT_SYMBOL_GPL(sata_port_ops);
Tejun Heo029cfd62008-03-25 12:22:49 +090082
Tejun Heo3373efd2006-05-15 20:57:53 +090083static unsigned int ata_dev_init_params(struct ata_device *dev,
84 u16 heads, u16 sectors);
85static unsigned int ata_dev_set_xfermode(struct ata_device *dev);
86static void ata_dev_xfermask(struct ata_device *dev);
Tejun Heo75683fe2007-07-05 13:31:27 +090087static unsigned long ata_dev_blacklisted(const struct ata_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
Tero Roponena78f57a2012-04-22 11:38:00 +030089atomic_t ata_print_id = ATOMIC_INIT(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
Bartlomiej Zolnierkiewiczbf89b0b2020-03-26 16:58:22 +010091#ifdef CONFIG_ATA_FORCE
Tejun Heo33267322008-02-13 09:15:09 +090092struct ata_force_param {
93 const char *name;
Bartlomiej Zolnierkiewicz8ba5a452020-03-26 16:58:02 +010094 u8 cbl;
95 u8 spd_limit;
Tejun Heo33267322008-02-13 09:15:09 +090096 unsigned long xfer_mask;
97 unsigned int horkage_on;
98 unsigned int horkage_off;
Bartlomiej Zolnierkiewicz8ba5a452020-03-26 16:58:02 +010099 u16 lflags;
Tejun Heo33267322008-02-13 09:15:09 +0900100};
101
102struct ata_force_ent {
103 int port;
104 int device;
105 struct ata_force_param param;
106};
107
108static struct ata_force_ent *ata_force_tbl;
109static int ata_force_tbl_size;
110
Bartlomiej Zolnierkiewiczbbf5a092020-03-26 16:58:01 +0100111static char ata_force_param_buf[COMMAND_LINE_SIZE] __initdata;
Tejun Heo7afb4222008-03-09 20:21:53 +0900112/* param_buf is thrown away after initialization, disallow read */
113module_param_string(force, ata_force_param_buf, sizeof(ata_force_param_buf), 0);
Mauro Carvalho Chehab8c27ceff32016-10-18 10:12:27 -0200114MODULE_PARM_DESC(force, "Force ATA configurations including cable type, link speed and transfer mode (see Documentation/admin-guide/kernel-parameters.rst for details)");
Bartlomiej Zolnierkiewiczbf89b0b2020-03-26 16:58:22 +0100115#endif
Tejun Heo33267322008-02-13 09:15:09 +0900116
Tejun Heo2486fa52008-07-31 07:52:40 +0900117static int atapi_enabled = 1;
Jeff Garzik1623c812005-08-30 03:37:42 -0400118module_param(atapi_enabled, int, 0444);
Evgeni Golovad5d8ea2009-06-22 09:46:20 +0200119MODULE_PARM_DESC(atapi_enabled, "Enable discovery of ATAPI devices (0=off, 1=on [default])");
Jeff Garzik1623c812005-08-30 03:37:42 -0400120
Adrian Bunkc5c61bd2008-02-25 02:07:25 +0200121static int atapi_dmadir = 0;
Albert Lee95de7192006-04-04 10:57:18 +0800122module_param(atapi_dmadir, int, 0444);
Evgeni Golovad5d8ea2009-06-22 09:46:20 +0200123MODULE_PARM_DESC(atapi_dmadir, "Enable ATAPI DMADIR bridge support (0=off [default], 1=on)");
Albert Lee95de7192006-04-04 10:57:18 +0800124
Mark Lordbaf4fdf2007-08-08 01:08:45 +0900125int atapi_passthru16 = 1;
126module_param(atapi_passthru16, int, 0444);
Evgeni Golovad5d8ea2009-06-22 09:46:20 +0200127MODULE_PARM_DESC(atapi_passthru16, "Enable ATA_16 passthru for ATAPI devices (0=off, 1=on [default])");
Mark Lordbaf4fdf2007-08-08 01:08:45 +0900128
Jeff Garzikc3c013a2006-02-27 22:31:19 -0500129int libata_fua = 0;
130module_param_named(fua, libata_fua, int, 0444);
Evgeni Golovad5d8ea2009-06-22 09:46:20 +0200131MODULE_PARM_DESC(fua, "FUA support (0=off [default], 1=on)");
Jeff Garzikc3c013a2006-02-27 22:31:19 -0500132
Jeff Garzik2dcb4072007-10-19 06:42:56 -0400133static int ata_ignore_hpa;
Alan Cox1e999732007-04-11 00:23:13 +0100134module_param_named(ignore_hpa, ata_ignore_hpa, int, 0644);
135MODULE_PARM_DESC(ignore_hpa, "Ignore HPA limit (0=keep BIOS limits, 1=ignore limits, using full disk)");
136
Alan Coxb3a70602007-10-02 12:38:26 -0400137static int libata_dma_mask = ATA_DMA_MASK_ATA|ATA_DMA_MASK_ATAPI|ATA_DMA_MASK_CFA;
138module_param_named(dma, libata_dma_mask, int, 0444);
139MODULE_PARM_DESC(dma, "DMA enable/disable (0x1==ATA, 0x2==ATAPI, 0x4==CF)");
140
Tejun Heo87fbc5a2008-05-20 02:17:54 +0900141static int ata_probe_timeout;
Andrew Mortona8601e52006-06-25 01:36:52 -0700142module_param(ata_probe_timeout, int, 0444);
143MODULE_PARM_DESC(ata_probe_timeout, "Set ATA probing timeout (seconds)");
144
Jeff Garzik6ebe9d82007-10-05 16:28:36 -0400145int libata_noacpi = 0;
Jeff Garzikd7d0dad2007-03-28 01:57:37 -0400146module_param_named(noacpi, libata_noacpi, int, 0444);
Evgeni Golovad5d8ea2009-06-22 09:46:20 +0200147MODULE_PARM_DESC(noacpi, "Disable the use of ACPI in probe/suspend/resume (0=off [default], 1=on)");
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700148
Alan Coxae8d4ee2007-11-04 22:05:49 -0500149int libata_allow_tpm = 0;
150module_param_named(allow_tpm, libata_allow_tpm, int, 0444);
Evgeni Golovad5d8ea2009-06-22 09:46:20 +0200151MODULE_PARM_DESC(allow_tpm, "Permit the use of TPM commands (0=off [default], 1=on)");
Alan Coxae8d4ee2007-11-04 22:05:49 -0500152
Tejun Heoe7ecd432010-05-19 15:38:58 +0200153static int atapi_an;
154module_param(atapi_an, int, 0444);
155MODULE_PARM_DESC(atapi_an, "Enable ATAPI AN media presence notification (0=0ff [default], 1=on)");
156
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157MODULE_AUTHOR("Jeff Garzik");
158MODULE_DESCRIPTION("Library module for ATA devices");
159MODULE_LICENSE("GPL");
160MODULE_VERSION(DRV_VERSION);
161
Damien Le Moal891fd7c2021-08-16 10:44:50 +0900162static inline bool ata_dev_print_info(struct ata_device *dev)
163{
164 struct ata_eh_context *ehc = &dev->link->eh_context;
165
166 return ehc->i.flags & ATA_EHI_PRINTINFO;
167}
Edward Falk0baab862005-06-02 18:17:13 -0400168
Tejun Heo9913ff8a2009-01-29 20:31:31 +0900169static bool ata_sstatus_online(u32 sstatus)
170{
171 return (sstatus & 0xf) == 0x3;
172}
173
Tejun Heo1eca4362008-11-03 20:03:17 +0900174/**
175 * ata_link_next - link iteration helper
176 * @link: the previous link, NULL to start
177 * @ap: ATA port containing links to iterate
178 * @mode: iteration mode, one of ATA_LITER_*
Tejun Heoaadffb62008-07-31 17:02:41 +0900179 *
Tejun Heo1eca4362008-11-03 20:03:17 +0900180 * LOCKING:
181 * Host lock or EH context.
182 *
183 * RETURNS:
184 * Pointer to the next link.
Tejun Heoaadffb62008-07-31 17:02:41 +0900185 */
Tejun Heo1eca4362008-11-03 20:03:17 +0900186struct ata_link *ata_link_next(struct ata_link *link, struct ata_port *ap,
187 enum ata_link_iter_mode mode)
Tejun Heoaadffb62008-07-31 17:02:41 +0900188{
Tejun Heo1eca4362008-11-03 20:03:17 +0900189 BUG_ON(mode != ATA_LITER_EDGE &&
190 mode != ATA_LITER_PMP_FIRST && mode != ATA_LITER_HOST_FIRST);
Tejun Heoaadffb62008-07-31 17:02:41 +0900191
Tejun Heo1eca4362008-11-03 20:03:17 +0900192 /* NULL link indicates start of iteration */
193 if (!link)
194 switch (mode) {
195 case ATA_LITER_EDGE:
196 case ATA_LITER_PMP_FIRST:
197 if (sata_pmp_attached(ap))
198 return ap->pmp_link;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500199 fallthrough;
Tejun Heo1eca4362008-11-03 20:03:17 +0900200 case ATA_LITER_HOST_FIRST:
201 return &ap->link;
202 }
203
204 /* we just iterated over the host link, what's next? */
205 if (link == &ap->link)
206 switch (mode) {
207 case ATA_LITER_HOST_FIRST:
208 if (sata_pmp_attached(ap))
209 return ap->pmp_link;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500210 fallthrough;
Tejun Heo1eca4362008-11-03 20:03:17 +0900211 case ATA_LITER_PMP_FIRST:
212 if (unlikely(ap->slave_link))
Tejun Heob1c72912008-07-31 17:02:43 +0900213 return ap->slave_link;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500214 fallthrough;
Tejun Heo1eca4362008-11-03 20:03:17 +0900215 case ATA_LITER_EDGE:
Tejun Heoaadffb62008-07-31 17:02:41 +0900216 return NULL;
Tejun Heob1c72912008-07-31 17:02:43 +0900217 }
Tejun Heoaadffb62008-07-31 17:02:41 +0900218
Tejun Heob1c72912008-07-31 17:02:43 +0900219 /* slave_link excludes PMP */
220 if (unlikely(link == ap->slave_link))
221 return NULL;
222
Tejun Heo1eca4362008-11-03 20:03:17 +0900223 /* we were over a PMP link */
Tejun Heoaadffb62008-07-31 17:02:41 +0900224 if (++link < ap->pmp_link + ap->nr_pmp_links)
225 return link;
Tejun Heo1eca4362008-11-03 20:03:17 +0900226
227 if (mode == ATA_LITER_PMP_FIRST)
228 return &ap->link;
229
Tejun Heoaadffb62008-07-31 17:02:41 +0900230 return NULL;
231}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +0100232EXPORT_SYMBOL_GPL(ata_link_next);
Tejun Heoaadffb62008-07-31 17:02:41 +0900233
Edward Falk0baab862005-06-02 18:17:13 -0400234/**
Tejun Heo1eca4362008-11-03 20:03:17 +0900235 * ata_dev_next - device iteration helper
236 * @dev: the previous device, NULL to start
237 * @link: ATA link containing devices to iterate
238 * @mode: iteration mode, one of ATA_DITER_*
239 *
240 * LOCKING:
241 * Host lock or EH context.
242 *
243 * RETURNS:
244 * Pointer to the next device.
245 */
246struct ata_device *ata_dev_next(struct ata_device *dev, struct ata_link *link,
247 enum ata_dev_iter_mode mode)
248{
249 BUG_ON(mode != ATA_DITER_ENABLED && mode != ATA_DITER_ENABLED_REVERSE &&
250 mode != ATA_DITER_ALL && mode != ATA_DITER_ALL_REVERSE);
251
252 /* NULL dev indicates start of iteration */
253 if (!dev)
254 switch (mode) {
255 case ATA_DITER_ENABLED:
256 case ATA_DITER_ALL:
257 dev = link->device;
258 goto check;
259 case ATA_DITER_ENABLED_REVERSE:
260 case ATA_DITER_ALL_REVERSE:
261 dev = link->device + ata_link_max_devices(link) - 1;
262 goto check;
263 }
264
265 next:
266 /* move to the next one */
267 switch (mode) {
268 case ATA_DITER_ENABLED:
269 case ATA_DITER_ALL:
270 if (++dev < link->device + ata_link_max_devices(link))
271 goto check;
272 return NULL;
273 case ATA_DITER_ENABLED_REVERSE:
274 case ATA_DITER_ALL_REVERSE:
275 if (--dev >= link->device)
276 goto check;
277 return NULL;
278 }
279
280 check:
281 if ((mode == ATA_DITER_ENABLED || mode == ATA_DITER_ENABLED_REVERSE) &&
282 !ata_dev_enabled(dev))
283 goto next;
284 return dev;
285}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +0100286EXPORT_SYMBOL_GPL(ata_dev_next);
Tejun Heo1eca4362008-11-03 20:03:17 +0900287
288/**
Tejun Heob1c72912008-07-31 17:02:43 +0900289 * ata_dev_phys_link - find physical link for a device
290 * @dev: ATA device to look up physical link for
291 *
292 * Look up physical link which @dev is attached to. Note that
293 * this is different from @dev->link only when @dev is on slave
294 * link. For all other cases, it's the same as @dev->link.
295 *
296 * LOCKING:
297 * Don't care.
298 *
299 * RETURNS:
300 * Pointer to the found physical link.
301 */
302struct ata_link *ata_dev_phys_link(struct ata_device *dev)
303{
304 struct ata_port *ap = dev->link->ap;
305
306 if (!ap->slave_link)
307 return dev->link;
308 if (!dev->devno)
309 return &ap->link;
310 return ap->slave_link;
311}
312
Bartlomiej Zolnierkiewiczbf89b0b2020-03-26 16:58:22 +0100313#ifdef CONFIG_ATA_FORCE
Tejun Heob1c72912008-07-31 17:02:43 +0900314/**
Tejun Heo33267322008-02-13 09:15:09 +0900315 * ata_force_cbl - force cable type according to libata.force
Randy Dunlap4cdfa1b2008-02-22 12:21:37 -0800316 * @ap: ATA port of interest
Tejun Heo33267322008-02-13 09:15:09 +0900317 *
318 * Force cable type according to libata.force and whine about it.
319 * The last entry which has matching port number is used, so it
320 * can be specified as part of device force parameters. For
321 * example, both "a:40c,1.00:udma4" and "1.00:40c,udma4" have the
322 * same effect.
323 *
324 * LOCKING:
325 * EH context.
326 */
327void ata_force_cbl(struct ata_port *ap)
328{
329 int i;
330
331 for (i = ata_force_tbl_size - 1; i >= 0; i--) {
332 const struct ata_force_ent *fe = &ata_force_tbl[i];
333
334 if (fe->port != -1 && fe->port != ap->print_id)
335 continue;
336
337 if (fe->param.cbl == ATA_CBL_NONE)
338 continue;
339
340 ap->cbl = fe->param.cbl;
Joe Perchesa9a79df2011-04-15 15:51:59 -0700341 ata_port_notice(ap, "FORCE: cable set to %s\n", fe->param.name);
Tejun Heo33267322008-02-13 09:15:09 +0900342 return;
343 }
344}
345
346/**
Tejun Heo05944bd2008-08-13 20:19:09 +0900347 * ata_force_link_limits - force link limits according to libata.force
Tejun Heo33267322008-02-13 09:15:09 +0900348 * @link: ATA link of interest
349 *
Tejun Heo05944bd2008-08-13 20:19:09 +0900350 * Force link flags and SATA spd limit according to libata.force
351 * and whine about it. When only the port part is specified
352 * (e.g. 1:), the limit applies to all links connected to both
353 * the host link and all fan-out ports connected via PMP. If the
354 * device part is specified as 0 (e.g. 1.00:), it specifies the
355 * first fan-out link not the host link. Device number 15 always
Tejun Heob1c72912008-07-31 17:02:43 +0900356 * points to the host link whether PMP is attached or not. If the
357 * controller has slave link, device number 16 points to it.
Tejun Heo33267322008-02-13 09:15:09 +0900358 *
359 * LOCKING:
360 * EH context.
361 */
Tejun Heo05944bd2008-08-13 20:19:09 +0900362static void ata_force_link_limits(struct ata_link *link)
Tejun Heo33267322008-02-13 09:15:09 +0900363{
Tejun Heo05944bd2008-08-13 20:19:09 +0900364 bool did_spd = false;
Tejun Heob1c72912008-07-31 17:02:43 +0900365 int linkno = link->pmp;
366 int i;
Tejun Heo33267322008-02-13 09:15:09 +0900367
368 if (ata_is_host_link(link))
Tejun Heob1c72912008-07-31 17:02:43 +0900369 linkno += 15;
Tejun Heo33267322008-02-13 09:15:09 +0900370
371 for (i = ata_force_tbl_size - 1; i >= 0; i--) {
372 const struct ata_force_ent *fe = &ata_force_tbl[i];
373
374 if (fe->port != -1 && fe->port != link->ap->print_id)
375 continue;
376
377 if (fe->device != -1 && fe->device != linkno)
378 continue;
379
Tejun Heo05944bd2008-08-13 20:19:09 +0900380 /* only honor the first spd limit */
381 if (!did_spd && fe->param.spd_limit) {
382 link->hw_sata_spd_limit = (1 << fe->param.spd_limit) - 1;
Joe Perchesa9a79df2011-04-15 15:51:59 -0700383 ata_link_notice(link, "FORCE: PHY spd limit set to %s\n",
Tejun Heo05944bd2008-08-13 20:19:09 +0900384 fe->param.name);
385 did_spd = true;
386 }
Tejun Heo33267322008-02-13 09:15:09 +0900387
Tejun Heo05944bd2008-08-13 20:19:09 +0900388 /* let lflags stack */
389 if (fe->param.lflags) {
390 link->flags |= fe->param.lflags;
Joe Perchesa9a79df2011-04-15 15:51:59 -0700391 ata_link_notice(link,
Tejun Heo05944bd2008-08-13 20:19:09 +0900392 "FORCE: link flag 0x%x forced -> 0x%x\n",
393 fe->param.lflags, link->flags);
394 }
Tejun Heo33267322008-02-13 09:15:09 +0900395 }
396}
397
398/**
399 * ata_force_xfermask - force xfermask according to libata.force
400 * @dev: ATA device of interest
401 *
402 * Force xfer_mask according to libata.force and whine about it.
403 * For consistency with link selection, device number 15 selects
404 * the first device connected to the host link.
405 *
406 * LOCKING:
407 * EH context.
408 */
409static void ata_force_xfermask(struct ata_device *dev)
410{
411 int devno = dev->link->pmp + dev->devno;
412 int alt_devno = devno;
413 int i;
414
Tejun Heob1c72912008-07-31 17:02:43 +0900415 /* allow n.15/16 for devices attached to host port */
416 if (ata_is_host_link(dev->link))
417 alt_devno += 15;
Tejun Heo33267322008-02-13 09:15:09 +0900418
419 for (i = ata_force_tbl_size - 1; i >= 0; i--) {
420 const struct ata_force_ent *fe = &ata_force_tbl[i];
421 unsigned long pio_mask, mwdma_mask, udma_mask;
422
423 if (fe->port != -1 && fe->port != dev->link->ap->print_id)
424 continue;
425
426 if (fe->device != -1 && fe->device != devno &&
427 fe->device != alt_devno)
428 continue;
429
430 if (!fe->param.xfer_mask)
431 continue;
432
433 ata_unpack_xfermask(fe->param.xfer_mask,
434 &pio_mask, &mwdma_mask, &udma_mask);
435 if (udma_mask)
436 dev->udma_mask = udma_mask;
437 else if (mwdma_mask) {
438 dev->udma_mask = 0;
439 dev->mwdma_mask = mwdma_mask;
440 } else {
441 dev->udma_mask = 0;
442 dev->mwdma_mask = 0;
443 dev->pio_mask = pio_mask;
444 }
445
Joe Perchesa9a79df2011-04-15 15:51:59 -0700446 ata_dev_notice(dev, "FORCE: xfer_mask set to %s\n",
447 fe->param.name);
Tejun Heo33267322008-02-13 09:15:09 +0900448 return;
449 }
450}
451
452/**
453 * ata_force_horkage - force horkage according to libata.force
454 * @dev: ATA device of interest
455 *
456 * Force horkage according to libata.force and whine about it.
457 * For consistency with link selection, device number 15 selects
458 * the first device connected to the host link.
459 *
460 * LOCKING:
461 * EH context.
462 */
463static void ata_force_horkage(struct ata_device *dev)
464{
465 int devno = dev->link->pmp + dev->devno;
466 int alt_devno = devno;
467 int i;
468
Tejun Heob1c72912008-07-31 17:02:43 +0900469 /* allow n.15/16 for devices attached to host port */
470 if (ata_is_host_link(dev->link))
471 alt_devno += 15;
Tejun Heo33267322008-02-13 09:15:09 +0900472
473 for (i = 0; i < ata_force_tbl_size; i++) {
474 const struct ata_force_ent *fe = &ata_force_tbl[i];
475
476 if (fe->port != -1 && fe->port != dev->link->ap->print_id)
477 continue;
478
479 if (fe->device != -1 && fe->device != devno &&
480 fe->device != alt_devno)
481 continue;
482
483 if (!(~dev->horkage & fe->param.horkage_on) &&
484 !(dev->horkage & fe->param.horkage_off))
485 continue;
486
487 dev->horkage |= fe->param.horkage_on;
488 dev->horkage &= ~fe->param.horkage_off;
489
Joe Perchesa9a79df2011-04-15 15:51:59 -0700490 ata_dev_notice(dev, "FORCE: horkage modified (%s)\n",
491 fe->param.name);
Tejun Heo33267322008-02-13 09:15:09 +0900492 }
493}
Bartlomiej Zolnierkiewiczbf89b0b2020-03-26 16:58:22 +0100494#else
495static inline void ata_force_link_limits(struct ata_link *link) { }
496static inline void ata_force_xfermask(struct ata_device *dev) { }
497static inline void ata_force_horkage(struct ata_device *dev) { }
498#endif
Tejun Heo33267322008-02-13 09:15:09 +0900499
500/**
Tejun Heo436d34b2008-04-02 17:28:46 +0900501 * atapi_cmd_type - Determine ATAPI command type from SCSI opcode
502 * @opcode: SCSI opcode
503 *
504 * Determine ATAPI command type from @opcode.
505 *
506 * LOCKING:
507 * None.
508 *
509 * RETURNS:
510 * ATAPI_{READ|WRITE|READ_CD|PASS_THRU|MISC}
511 */
512int atapi_cmd_type(u8 opcode)
513{
514 switch (opcode) {
515 case GPCMD_READ_10:
516 case GPCMD_READ_12:
517 return ATAPI_READ;
518
519 case GPCMD_WRITE_10:
520 case GPCMD_WRITE_12:
521 case GPCMD_WRITE_AND_VERIFY_10:
522 return ATAPI_WRITE;
523
524 case GPCMD_READ_CD:
525 case GPCMD_READ_CD_MSF:
526 return ATAPI_READ_CD;
527
Tejun Heoe52dcc42008-04-02 17:35:19 +0900528 case ATA_16:
529 case ATA_12:
530 if (atapi_passthru16)
531 return ATAPI_PASS_THRU;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500532 fallthrough;
Tejun Heo436d34b2008-04-02 17:28:46 +0900533 default:
534 return ATAPI_MISC;
535 }
536}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +0100537EXPORT_SYMBOL_GPL(atapi_cmd_type);
Tejun Heo436d34b2008-04-02 17:28:46 +0900538
Albert Lee8cbd6df2005-10-12 15:06:27 +0800539static const u8 ata_rw_cmds[] = {
540 /* pio multi */
541 ATA_CMD_READ_MULTI,
542 ATA_CMD_WRITE_MULTI,
543 ATA_CMD_READ_MULTI_EXT,
544 ATA_CMD_WRITE_MULTI_EXT,
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100545 0,
546 0,
547 0,
548 ATA_CMD_WRITE_MULTI_FUA_EXT,
Albert Lee8cbd6df2005-10-12 15:06:27 +0800549 /* pio */
550 ATA_CMD_PIO_READ,
551 ATA_CMD_PIO_WRITE,
552 ATA_CMD_PIO_READ_EXT,
553 ATA_CMD_PIO_WRITE_EXT,
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100554 0,
555 0,
556 0,
557 0,
Albert Lee8cbd6df2005-10-12 15:06:27 +0800558 /* dma */
559 ATA_CMD_READ,
560 ATA_CMD_WRITE,
561 ATA_CMD_READ_EXT,
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100562 ATA_CMD_WRITE_EXT,
563 0,
564 0,
565 0,
566 ATA_CMD_WRITE_FUA_EXT
Albert Lee8cbd6df2005-10-12 15:06:27 +0800567};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
569/**
Albert Lee8cbd6df2005-10-12 15:06:27 +0800570 * ata_rwcmd_protocol - set taskfile r/w commands and protocol
Tejun Heobd056d72006-11-14 22:47:10 +0900571 * @tf: command to examine and configure
572 * @dev: device tf belongs to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 *
Jeff Garzik2e9edbf2006-03-24 09:56:57 -0500574 * Examine the device configuration and tf->flags to calculate
Albert Lee8cbd6df2005-10-12 15:06:27 +0800575 * the proper read/write commands and protocol to use.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 *
577 * LOCKING:
578 * caller.
579 */
Tejun Heobd056d72006-11-14 22:47:10 +0900580static int ata_rwcmd_protocol(struct ata_taskfile *tf, struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581{
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100582 u8 cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100584 int index, fua, lba48, write;
Jeff Garzik2e9edbf2006-03-24 09:56:57 -0500585
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100586 fua = (tf->flags & ATA_TFLAG_FUA) ? 4 : 0;
Albert Lee8cbd6df2005-10-12 15:06:27 +0800587 lba48 = (tf->flags & ATA_TFLAG_LBA48) ? 2 : 0;
588 write = (tf->flags & ATA_TFLAG_WRITE) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589
Albert Lee8cbd6df2005-10-12 15:06:27 +0800590 if (dev->flags & ATA_DFLAG_PIO) {
591 tf->protocol = ATA_PROT_PIO;
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100592 index = dev->multi_count ? 0 : 8;
Tejun Heo9af5c9c2007-08-06 18:36:22 +0900593 } else if (lba48 && (dev->link->ap->flags & ATA_FLAG_PIO_LBA48)) {
Alan Cox8d238e02006-01-17 20:50:31 +0000594 /* Unable to use DMA due to host limitation */
595 tf->protocol = ATA_PROT_PIO;
Albert Lee0565c262006-02-13 18:55:25 +0800596 index = dev->multi_count ? 0 : 8;
Albert Lee8cbd6df2005-10-12 15:06:27 +0800597 } else {
598 tf->protocol = ATA_PROT_DMA;
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100599 index = 16;
Albert Lee8cbd6df2005-10-12 15:06:27 +0800600 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100602 cmd = ata_rw_cmds[index + fua + lba48 + write];
603 if (cmd) {
604 tf->command = cmd;
605 return 0;
606 }
607 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608}
609
Tejun Heocb95d562006-03-06 04:31:56 +0900610/**
Tejun Heo35b649f2006-11-14 22:37:35 +0900611 * ata_tf_read_block - Read block address from ATA taskfile
612 * @tf: ATA taskfile of interest
613 * @dev: ATA device @tf belongs to
614 *
615 * LOCKING:
616 * None.
617 *
618 * Read block address from @tf. This function can handle all
619 * three address formats - LBA, LBA48 and CHS. tf->protocol and
620 * flags select the address format to use.
621 *
622 * RETURNS:
623 * Block address read from @tf.
624 */
Hannes Reineckecffd1ee2016-04-04 11:43:57 +0200625u64 ata_tf_read_block(const struct ata_taskfile *tf, struct ata_device *dev)
Tejun Heo35b649f2006-11-14 22:37:35 +0900626{
627 u64 block = 0;
628
Tejun Heofe16d4f2015-08-03 11:41:33 -0400629 if (tf->flags & ATA_TFLAG_LBA) {
Tejun Heo35b649f2006-11-14 22:37:35 +0900630 if (tf->flags & ATA_TFLAG_LBA48) {
631 block |= (u64)tf->hob_lbah << 40;
632 block |= (u64)tf->hob_lbam << 32;
Roland Dreier44901a92008-11-04 10:34:48 -0800633 block |= (u64)tf->hob_lbal << 24;
Tejun Heo35b649f2006-11-14 22:37:35 +0900634 } else
635 block |= (tf->device & 0xf) << 24;
636
637 block |= tf->lbah << 16;
638 block |= tf->lbam << 8;
639 block |= tf->lbal;
640 } else {
641 u32 cyl, head, sect;
642
643 cyl = tf->lbam | (tf->lbah << 8);
644 head = tf->device & 0xf;
645 sect = tf->lbal;
646
Tejun Heoac8672e2009-08-16 21:21:21 +0900647 if (!sect) {
Joe Perchesa9a79df2011-04-15 15:51:59 -0700648 ata_dev_warn(dev,
649 "device reported invalid CHS sector 0\n");
Hannes Reineckecffd1ee2016-04-04 11:43:57 +0200650 return U64_MAX;
Tejun Heoac8672e2009-08-16 21:21:21 +0900651 }
652
653 block = (cyl * dev->heads + head) * dev->sectors + sect - 1;
Tejun Heo35b649f2006-11-14 22:37:35 +0900654 }
655
656 return block;
657}
658
659/**
Tejun Heobd056d72006-11-14 22:47:10 +0900660 * ata_build_rw_tf - Build ATA taskfile for given read/write request
661 * @tf: Target ATA taskfile
662 * @dev: ATA device @tf belongs to
663 * @block: Block address
664 * @n_block: Number of blocks
665 * @tf_flags: RW/FUA etc...
666 * @tag: tag
Adam Manzanares8e061782016-10-17 11:27:29 -0700667 * @class: IO priority class
Tejun Heobd056d72006-11-14 22:47:10 +0900668 *
669 * LOCKING:
670 * None.
671 *
672 * Build ATA taskfile @tf for read/write request described by
673 * @block, @n_block, @tf_flags and @tag on @dev.
674 *
675 * RETURNS:
676 *
677 * 0 on success, -ERANGE if the request is too large for @dev,
678 * -EINVAL if the request is invalid.
679 */
680int ata_build_rw_tf(struct ata_taskfile *tf, struct ata_device *dev,
681 u64 block, u32 n_block, unsigned int tf_flags,
Adam Manzanares8e061782016-10-17 11:27:29 -0700682 unsigned int tag, int class)
Tejun Heobd056d72006-11-14 22:47:10 +0900683{
684 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
685 tf->flags |= tf_flags;
686
Jens Axboe2e2cc672018-05-11 12:51:06 -0600687 if (ata_ncq_enabled(dev) && !ata_tag_internal(tag)) {
Tejun Heobd056d72006-11-14 22:47:10 +0900688 /* yay, NCQ */
689 if (!lba_48_ok(block, n_block))
690 return -ERANGE;
691
692 tf->protocol = ATA_PROT_NCQ;
693 tf->flags |= ATA_TFLAG_LBA | ATA_TFLAG_LBA48;
694
695 if (tf->flags & ATA_TFLAG_WRITE)
696 tf->command = ATA_CMD_FPDMA_WRITE;
697 else
698 tf->command = ATA_CMD_FPDMA_READ;
699
700 tf->nsect = tag << 3;
701 tf->hob_feature = (n_block >> 8) & 0xff;
702 tf->feature = n_block & 0xff;
703
704 tf->hob_lbah = (block >> 40) & 0xff;
705 tf->hob_lbam = (block >> 32) & 0xff;
706 tf->hob_lbal = (block >> 24) & 0xff;
707 tf->lbah = (block >> 16) & 0xff;
708 tf->lbam = (block >> 8) & 0xff;
709 tf->lbal = block & 0xff;
710
Sergei Shtylyov9ca7cfa2012-08-21 22:18:50 +0400711 tf->device = ATA_LBA;
Tejun Heobd056d72006-11-14 22:47:10 +0900712 if (tf->flags & ATA_TFLAG_FUA)
713 tf->device |= 1 << 7;
Adam Manzanares8e061782016-10-17 11:27:29 -0700714
Damien Le Moal2360fa12021-08-16 10:44:51 +0900715 if (dev->flags & ATA_DFLAG_NCQ_PRIO_ENABLE &&
716 class == IOPRIO_CLASS_RT)
717 tf->hob_nsect |= ATA_PRIO_HIGH << ATA_SHIFT_PRIO;
Tejun Heobd056d72006-11-14 22:47:10 +0900718 } else if (dev->flags & ATA_DFLAG_LBA) {
719 tf->flags |= ATA_TFLAG_LBA;
720
721 if (lba_28_ok(block, n_block)) {
722 /* use LBA28 */
723 tf->device |= (block >> 24) & 0xf;
724 } else if (lba_48_ok(block, n_block)) {
725 if (!(dev->flags & ATA_DFLAG_LBA48))
726 return -ERANGE;
727
728 /* use LBA48 */
729 tf->flags |= ATA_TFLAG_LBA48;
730
731 tf->hob_nsect = (n_block >> 8) & 0xff;
732
733 tf->hob_lbah = (block >> 40) & 0xff;
734 tf->hob_lbam = (block >> 32) & 0xff;
735 tf->hob_lbal = (block >> 24) & 0xff;
736 } else
737 /* request too large even for LBA48 */
738 return -ERANGE;
739
740 if (unlikely(ata_rwcmd_protocol(tf, dev) < 0))
741 return -EINVAL;
742
743 tf->nsect = n_block & 0xff;
744
745 tf->lbah = (block >> 16) & 0xff;
746 tf->lbam = (block >> 8) & 0xff;
747 tf->lbal = block & 0xff;
748
749 tf->device |= ATA_LBA;
750 } else {
751 /* CHS */
752 u32 sect, head, cyl, track;
753
754 /* The request -may- be too large for CHS addressing. */
755 if (!lba_28_ok(block, n_block))
756 return -ERANGE;
757
758 if (unlikely(ata_rwcmd_protocol(tf, dev) < 0))
759 return -EINVAL;
760
761 /* Convert LBA to CHS */
762 track = (u32)block / dev->sectors;
763 cyl = track / dev->heads;
764 head = track % dev->heads;
765 sect = (u32)block % dev->sectors + 1;
766
767 DPRINTK("block %u track %u cyl %u head %u sect %u\n",
768 (u32)block, track, cyl, head, sect);
769
770 /* Check whether the converted CHS can fit.
771 Cylinder: 0-65535
772 Head: 0-15
773 Sector: 1-255*/
774 if ((cyl >> 16) || (head >> 4) || (sect >> 8) || (!sect))
775 return -ERANGE;
776
777 tf->nsect = n_block & 0xff; /* Sector count 0 means 256 sectors */
778 tf->lbal = sect;
779 tf->lbam = cyl;
780 tf->lbah = cyl >> 8;
781 tf->device |= head;
782 }
783
784 return 0;
785}
786
787/**
Tejun Heocb95d562006-03-06 04:31:56 +0900788 * ata_pack_xfermask - Pack pio, mwdma and udma masks into xfer_mask
789 * @pio_mask: pio_mask
790 * @mwdma_mask: mwdma_mask
791 * @udma_mask: udma_mask
792 *
793 * Pack @pio_mask, @mwdma_mask and @udma_mask into a single
794 * unsigned int xfer_mask.
795 *
796 * LOCKING:
797 * None.
798 *
799 * RETURNS:
800 * Packed xfer_mask.
801 */
Tejun Heo7dc951a2007-11-27 19:43:42 +0900802unsigned long ata_pack_xfermask(unsigned long pio_mask,
803 unsigned long mwdma_mask,
804 unsigned long udma_mask)
Tejun Heocb95d562006-03-06 04:31:56 +0900805{
806 return ((pio_mask << ATA_SHIFT_PIO) & ATA_MASK_PIO) |
807 ((mwdma_mask << ATA_SHIFT_MWDMA) & ATA_MASK_MWDMA) |
808 ((udma_mask << ATA_SHIFT_UDMA) & ATA_MASK_UDMA);
809}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +0100810EXPORT_SYMBOL_GPL(ata_pack_xfermask);
Tejun Heocb95d562006-03-06 04:31:56 +0900811
Tejun Heoc0489e42006-03-24 14:07:49 +0900812/**
813 * ata_unpack_xfermask - Unpack xfer_mask into pio, mwdma and udma masks
814 * @xfer_mask: xfer_mask to unpack
815 * @pio_mask: resulting pio_mask
816 * @mwdma_mask: resulting mwdma_mask
817 * @udma_mask: resulting udma_mask
818 *
819 * Unpack @xfer_mask into @pio_mask, @mwdma_mask and @udma_mask.
Masanari Iidac9b55602016-04-13 23:36:27 +0900820 * Any NULL destination masks will be ignored.
Tejun Heoc0489e42006-03-24 14:07:49 +0900821 */
Tejun Heo7dc951a2007-11-27 19:43:42 +0900822void ata_unpack_xfermask(unsigned long xfer_mask, unsigned long *pio_mask,
823 unsigned long *mwdma_mask, unsigned long *udma_mask)
Tejun Heoc0489e42006-03-24 14:07:49 +0900824{
825 if (pio_mask)
826 *pio_mask = (xfer_mask & ATA_MASK_PIO) >> ATA_SHIFT_PIO;
827 if (mwdma_mask)
828 *mwdma_mask = (xfer_mask & ATA_MASK_MWDMA) >> ATA_SHIFT_MWDMA;
829 if (udma_mask)
830 *udma_mask = (xfer_mask & ATA_MASK_UDMA) >> ATA_SHIFT_UDMA;
831}
832
Tejun Heocb95d562006-03-06 04:31:56 +0900833static const struct ata_xfer_ent {
Tejun Heobe9a50c2006-03-31 22:48:52 +0900834 int shift, bits;
Tejun Heocb95d562006-03-06 04:31:56 +0900835 u8 base;
836} ata_xfer_tbl[] = {
Tejun Heo70cd0712007-11-27 19:43:40 +0900837 { ATA_SHIFT_PIO, ATA_NR_PIO_MODES, XFER_PIO_0 },
838 { ATA_SHIFT_MWDMA, ATA_NR_MWDMA_MODES, XFER_MW_DMA_0 },
839 { ATA_SHIFT_UDMA, ATA_NR_UDMA_MODES, XFER_UDMA_0 },
Tejun Heocb95d562006-03-06 04:31:56 +0900840 { -1, },
841};
842
843/**
844 * ata_xfer_mask2mode - Find matching XFER_* for the given xfer_mask
845 * @xfer_mask: xfer_mask of interest
846 *
847 * Return matching XFER_* value for @xfer_mask. Only the highest
848 * bit of @xfer_mask is considered.
849 *
850 * LOCKING:
851 * None.
852 *
853 * RETURNS:
Tejun Heo70cd0712007-11-27 19:43:40 +0900854 * Matching XFER_* value, 0xff if no match found.
Tejun Heocb95d562006-03-06 04:31:56 +0900855 */
Tejun Heo7dc951a2007-11-27 19:43:42 +0900856u8 ata_xfer_mask2mode(unsigned long xfer_mask)
Tejun Heocb95d562006-03-06 04:31:56 +0900857{
858 int highbit = fls(xfer_mask) - 1;
859 const struct ata_xfer_ent *ent;
860
861 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
862 if (highbit >= ent->shift && highbit < ent->shift + ent->bits)
863 return ent->base + highbit - ent->shift;
Tejun Heo70cd0712007-11-27 19:43:40 +0900864 return 0xff;
Tejun Heocb95d562006-03-06 04:31:56 +0900865}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +0100866EXPORT_SYMBOL_GPL(ata_xfer_mask2mode);
Tejun Heocb95d562006-03-06 04:31:56 +0900867
868/**
869 * ata_xfer_mode2mask - Find matching xfer_mask for XFER_*
870 * @xfer_mode: XFER_* of interest
871 *
872 * Return matching xfer_mask for @xfer_mode.
873 *
874 * LOCKING:
875 * None.
876 *
877 * RETURNS:
878 * Matching xfer_mask, 0 if no match found.
879 */
Tejun Heo7dc951a2007-11-27 19:43:42 +0900880unsigned long ata_xfer_mode2mask(u8 xfer_mode)
Tejun Heocb95d562006-03-06 04:31:56 +0900881{
882 const struct ata_xfer_ent *ent;
883
884 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
885 if (xfer_mode >= ent->base && xfer_mode < ent->base + ent->bits)
Tejun Heo70cd0712007-11-27 19:43:40 +0900886 return ((2 << (ent->shift + xfer_mode - ent->base)) - 1)
887 & ~((1 << ent->shift) - 1);
Tejun Heocb95d562006-03-06 04:31:56 +0900888 return 0;
889}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +0100890EXPORT_SYMBOL_GPL(ata_xfer_mode2mask);
Tejun Heocb95d562006-03-06 04:31:56 +0900891
892/**
893 * ata_xfer_mode2shift - Find matching xfer_shift for XFER_*
894 * @xfer_mode: XFER_* of interest
895 *
896 * Return matching xfer_shift for @xfer_mode.
897 *
898 * LOCKING:
899 * None.
900 *
901 * RETURNS:
902 * Matching xfer_shift, -1 if no match found.
903 */
Tejun Heo7dc951a2007-11-27 19:43:42 +0900904int ata_xfer_mode2shift(unsigned long xfer_mode)
Tejun Heocb95d562006-03-06 04:31:56 +0900905{
906 const struct ata_xfer_ent *ent;
907
908 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
909 if (xfer_mode >= ent->base && xfer_mode < ent->base + ent->bits)
910 return ent->shift;
911 return -1;
912}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +0100913EXPORT_SYMBOL_GPL(ata_xfer_mode2shift);
Tejun Heocb95d562006-03-06 04:31:56 +0900914
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915/**
Tejun Heo1da7b0d2006-03-06 04:31:56 +0900916 * ata_mode_string - convert xfer_mask to string
917 * @xfer_mask: mask of bits supported; only highest bit counts.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 *
919 * Determine string which represents the highest speed
Tejun Heo1da7b0d2006-03-06 04:31:56 +0900920 * (highest bit in @modemask).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 *
922 * LOCKING:
923 * None.
924 *
925 * RETURNS:
926 * Constant C string representing highest speed listed in
Tejun Heo1da7b0d2006-03-06 04:31:56 +0900927 * @mode_mask, or the constant C string "<n/a>".
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 */
Tejun Heo7dc951a2007-11-27 19:43:42 +0900929const char *ata_mode_string(unsigned long xfer_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930{
Tejun Heo75f554b2006-03-06 04:31:57 +0900931 static const char * const xfer_mode_str[] = {
932 "PIO0",
933 "PIO1",
934 "PIO2",
935 "PIO3",
936 "PIO4",
Alan Coxb352e572006-08-10 18:52:12 +0100937 "PIO5",
938 "PIO6",
Tejun Heo75f554b2006-03-06 04:31:57 +0900939 "MWDMA0",
940 "MWDMA1",
941 "MWDMA2",
Alan Coxb352e572006-08-10 18:52:12 +0100942 "MWDMA3",
943 "MWDMA4",
Tejun Heo75f554b2006-03-06 04:31:57 +0900944 "UDMA/16",
945 "UDMA/25",
946 "UDMA/33",
947 "UDMA/44",
948 "UDMA/66",
949 "UDMA/100",
950 "UDMA/133",
951 "UDMA7",
952 };
Tejun Heo1da7b0d2006-03-06 04:31:56 +0900953 int highbit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954
Tejun Heo1da7b0d2006-03-06 04:31:56 +0900955 highbit = fls(xfer_mask) - 1;
956 if (highbit >= 0 && highbit < ARRAY_SIZE(xfer_mode_str))
957 return xfer_mode_str[highbit];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 return "<n/a>";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +0100960EXPORT_SYMBOL_GPL(ata_mode_string);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961
Gwendal Grignoud9027472010-05-25 12:31:38 -0700962const char *sata_spd_string(unsigned int spd)
Tejun Heo4c360c82006-04-01 01:38:17 +0900963{
964 static const char * const spd_str[] = {
965 "1.5 Gbps",
966 "3.0 Gbps",
Shane Huang8522ee22008-12-30 11:00:37 +0800967 "6.0 Gbps",
Tejun Heo4c360c82006-04-01 01:38:17 +0900968 };
969
970 if (spd == 0 || (spd - 1) >= ARRAY_SIZE(spd_str))
971 return "<unknown>";
972 return spd_str[spd - 1];
973}
974
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975/**
976 * ata_dev_classify - determine device type based on ATA-spec signature
977 * @tf: ATA taskfile register set for device to be identified
978 *
979 * Determine from taskfile register contents whether a device is
980 * ATA or ATAPI, as per "Signature and persistence" section
981 * of ATA/PI spec (volume 1, sect 5.14).
982 *
983 * LOCKING:
984 * None.
985 *
986 * RETURNS:
Hannes Reinecke9162c652014-11-05 13:08:21 +0100987 * Device type, %ATA_DEV_ATA, %ATA_DEV_ATAPI, %ATA_DEV_PMP,
988 * %ATA_DEV_ZAC, or %ATA_DEV_UNKNOWN the event of failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 */
Jeff Garzik057ace52005-10-22 14:27:05 -0400990unsigned int ata_dev_classify(const struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991{
992 /* Apple's open source Darwin code hints that some devices only
993 * put a proper signature into the LBA mid/high registers,
994 * So, we only check those. It's sufficient for uniqueness.
Tejun Heo633273a2007-09-23 13:19:54 +0900995 *
996 * ATA/ATAPI-7 (d1532v1r1: Feb. 19, 2003) specified separate
997 * signatures for ATA and ATAPI devices attached on SerialATA,
998 * 0x3c/0xc3 and 0x69/0x96 respectively. However, SerialATA
999 * spec has never mentioned about using different signatures
1000 * for ATA/ATAPI devices. Then, Serial ATA II: Port
1001 * Multiplier specification began to use 0x69/0x96 to identify
1002 * port multpliers and 0x3c/0xc3 to identify SEMB device.
1003 * ATA/ATAPI-7 dropped descriptions about 0x3c/0xc3 and
1004 * 0x69/0x96 shortly and described them as reserved for
1005 * SerialATA.
1006 *
1007 * We follow the current spec and consider that 0x69/0x96
1008 * identifies a port multiplier and 0x3c/0xc3 a SEMB device.
Tejun Heo79b42ba2009-04-15 06:21:10 +09001009 * Unfortunately, WDC WD1600JS-62MHB5 (a hard drive) reports
1010 * SEMB signature. This is worked around in
1011 * ata_dev_read_id().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 */
Tejun Heo633273a2007-09-23 13:19:54 +09001013 if ((tf->lbam == 0) && (tf->lbah == 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 DPRINTK("found ATA device by sig\n");
1015 return ATA_DEV_ATA;
1016 }
1017
Tejun Heo633273a2007-09-23 13:19:54 +09001018 if ((tf->lbam == 0x14) && (tf->lbah == 0xeb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 DPRINTK("found ATAPI device by sig\n");
1020 return ATA_DEV_ATAPI;
1021 }
1022
Tejun Heo633273a2007-09-23 13:19:54 +09001023 if ((tf->lbam == 0x69) && (tf->lbah == 0x96)) {
1024 DPRINTK("found PMP device by sig\n");
1025 return ATA_DEV_PMP;
1026 }
1027
1028 if ((tf->lbam == 0x3c) && (tf->lbah == 0xc3)) {
Tejun Heo79b42ba2009-04-15 06:21:10 +09001029 DPRINTK("found SEMB device by sig (could be ATA device)\n");
1030 return ATA_DEV_SEMB;
Tejun Heo633273a2007-09-23 13:19:54 +09001031 }
1032
Hannes Reinecke9162c652014-11-05 13:08:21 +01001033 if ((tf->lbam == 0xcd) && (tf->lbah == 0xab)) {
1034 DPRINTK("found ZAC device by sig\n");
1035 return ATA_DEV_ZAC;
1036 }
1037
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 DPRINTK("unknown device\n");
1039 return ATA_DEV_UNKNOWN;
1040}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01001041EXPORT_SYMBOL_GPL(ata_dev_classify);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042
1043/**
Tejun Heo6a62a042006-02-13 10:02:46 +09001044 * ata_id_string - Convert IDENTIFY DEVICE page into string
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 * @id: IDENTIFY DEVICE results we will examine
1046 * @s: string into which data is output
1047 * @ofs: offset into identify device page
1048 * @len: length of string to return. must be an even number.
1049 *
1050 * The strings in the IDENTIFY DEVICE page are broken up into
1051 * 16-bit chunks. Run through the string, and output each
1052 * 8-bit chunk linearly, regardless of platform.
1053 *
1054 * LOCKING:
1055 * caller.
1056 */
1057
Tejun Heo6a62a042006-02-13 10:02:46 +09001058void ata_id_string(const u16 *id, unsigned char *s,
1059 unsigned int ofs, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060{
1061 unsigned int c;
1062
Alan Cox963e4972008-07-24 17:16:06 +01001063 BUG_ON(len & 1);
1064
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 while (len > 0) {
1066 c = id[ofs] >> 8;
1067 *s = c;
1068 s++;
1069
1070 c = id[ofs] & 0xff;
1071 *s = c;
1072 s++;
1073
1074 ofs++;
1075 len -= 2;
1076 }
1077}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01001078EXPORT_SYMBOL_GPL(ata_id_string);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079
Tejun Heo0e949ff2006-02-12 22:47:04 +09001080/**
Tejun Heo6a62a042006-02-13 10:02:46 +09001081 * ata_id_c_string - Convert IDENTIFY DEVICE page into C string
Tejun Heo0e949ff2006-02-12 22:47:04 +09001082 * @id: IDENTIFY DEVICE results we will examine
1083 * @s: string into which data is output
1084 * @ofs: offset into identify device page
1085 * @len: length of string to return. must be an odd number.
1086 *
Tejun Heo6a62a042006-02-13 10:02:46 +09001087 * This function is identical to ata_id_string except that it
Tejun Heo0e949ff2006-02-12 22:47:04 +09001088 * trims trailing spaces and terminates the resulting string with
1089 * null. @len must be actual maximum length (even number) + 1.
1090 *
1091 * LOCKING:
1092 * caller.
1093 */
Tejun Heo6a62a042006-02-13 10:02:46 +09001094void ata_id_c_string(const u16 *id, unsigned char *s,
1095 unsigned int ofs, unsigned int len)
Tejun Heo0e949ff2006-02-12 22:47:04 +09001096{
1097 unsigned char *p;
1098
Tejun Heo6a62a042006-02-13 10:02:46 +09001099 ata_id_string(id, s, ofs, len - 1);
Tejun Heo0e949ff2006-02-12 22:47:04 +09001100
1101 p = s + strnlen(s, len - 1);
1102 while (p > s && p[-1] == ' ')
1103 p--;
1104 *p = '\0';
1105}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01001106EXPORT_SYMBOL_GPL(ata_id_c_string);
Edward Falk0baab862005-06-02 18:17:13 -04001107
Tejun Heodb6f8752007-09-03 12:31:58 +09001108static u64 ata_id_n_sectors(const u16 *id)
1109{
1110 if (ata_id_has_lba(id)) {
1111 if (ata_id_has_lba48(id))
Robert Hancock968e5942009-02-16 20:15:08 -06001112 return ata_id_u64(id, ATA_ID_LBA_CAPACITY_2);
Tejun Heodb6f8752007-09-03 12:31:58 +09001113 else
Robert Hancock968e5942009-02-16 20:15:08 -06001114 return ata_id_u32(id, ATA_ID_LBA_CAPACITY);
Tejun Heodb6f8752007-09-03 12:31:58 +09001115 } else {
1116 if (ata_id_current_chs_valid(id))
Robert Hancock968e5942009-02-16 20:15:08 -06001117 return id[ATA_ID_CUR_CYLS] * id[ATA_ID_CUR_HEADS] *
1118 id[ATA_ID_CUR_SECTORS];
Tejun Heodb6f8752007-09-03 12:31:58 +09001119 else
Robert Hancock968e5942009-02-16 20:15:08 -06001120 return id[ATA_ID_CYLS] * id[ATA_ID_HEADS] *
1121 id[ATA_ID_SECTORS];
Tejun Heodb6f8752007-09-03 12:31:58 +09001122 }
1123}
1124
Tejun Heoa5987e02008-03-27 19:14:23 +09001125u64 ata_tf_to_lba48(const struct ata_taskfile *tf)
Alan Cox1e999732007-04-11 00:23:13 +01001126{
1127 u64 sectors = 0;
1128
1129 sectors |= ((u64)(tf->hob_lbah & 0xff)) << 40;
1130 sectors |= ((u64)(tf->hob_lbam & 0xff)) << 32;
Roland Dreierba14a9c2008-10-28 16:52:20 -07001131 sectors |= ((u64)(tf->hob_lbal & 0xff)) << 24;
Alan Cox1e999732007-04-11 00:23:13 +01001132 sectors |= (tf->lbah & 0xff) << 16;
1133 sectors |= (tf->lbam & 0xff) << 8;
1134 sectors |= (tf->lbal & 0xff);
1135
Tejun Heoa5987e02008-03-27 19:14:23 +09001136 return sectors;
Alan Cox1e999732007-04-11 00:23:13 +01001137}
1138
Tejun Heoa5987e02008-03-27 19:14:23 +09001139u64 ata_tf_to_lba(const struct ata_taskfile *tf)
Alan Cox1e999732007-04-11 00:23:13 +01001140{
1141 u64 sectors = 0;
1142
1143 sectors |= (tf->device & 0x0f) << 24;
1144 sectors |= (tf->lbah & 0xff) << 16;
1145 sectors |= (tf->lbam & 0xff) << 8;
1146 sectors |= (tf->lbal & 0xff);
1147
Tejun Heoa5987e02008-03-27 19:14:23 +09001148 return sectors;
Alan Cox1e999732007-04-11 00:23:13 +01001149}
1150
1151/**
Tejun Heoc728a912007-09-03 12:32:30 +09001152 * ata_read_native_max_address - Read native max address
1153 * @dev: target device
1154 * @max_sectors: out parameter for the result native max address
Alan Cox1e999732007-04-11 00:23:13 +01001155 *
Tejun Heoc728a912007-09-03 12:32:30 +09001156 * Perform an LBA48 or LBA28 native size query upon the device in
1157 * question.
1158 *
1159 * RETURNS:
1160 * 0 on success, -EACCES if command is aborted by the drive.
1161 * -EIO on other errors.
Alan Cox1e999732007-04-11 00:23:13 +01001162 */
Tejun Heoc728a912007-09-03 12:32:30 +09001163static int ata_read_native_max_address(struct ata_device *dev, u64 *max_sectors)
Alan Cox1e999732007-04-11 00:23:13 +01001164{
Tejun Heoc728a912007-09-03 12:32:30 +09001165 unsigned int err_mask;
Alan Cox1e999732007-04-11 00:23:13 +01001166 struct ata_taskfile tf;
Tejun Heoc728a912007-09-03 12:32:30 +09001167 int lba48 = ata_id_has_lba48(dev->id);
Alan Cox1e999732007-04-11 00:23:13 +01001168
1169 ata_tf_init(dev, &tf);
1170
Tejun Heoc728a912007-09-03 12:32:30 +09001171 /* always clear all address registers */
Alan Cox1e999732007-04-11 00:23:13 +01001172 tf.flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
Tejun Heoc728a912007-09-03 12:32:30 +09001173
1174 if (lba48) {
1175 tf.command = ATA_CMD_READ_NATIVE_MAX_EXT;
1176 tf.flags |= ATA_TFLAG_LBA48;
1177 } else
1178 tf.command = ATA_CMD_READ_NATIVE_MAX;
1179
Hannes Reineckebd18bc02016-07-14 09:05:46 +09001180 tf.protocol = ATA_PROT_NODATA;
Tejun Heoc728a912007-09-03 12:32:30 +09001181 tf.device |= ATA_LBA;
Alan Cox1e999732007-04-11 00:23:13 +01001182
Tejun Heo2b789102007-10-09 15:05:44 +09001183 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
Tejun Heoc728a912007-09-03 12:32:30 +09001184 if (err_mask) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07001185 ata_dev_warn(dev,
1186 "failed to read native max address (err_mask=0x%x)\n",
1187 err_mask);
Tejun Heoc728a912007-09-03 12:32:30 +09001188 if (err_mask == AC_ERR_DEV && (tf.feature & ATA_ABORTED))
1189 return -EACCES;
1190 return -EIO;
1191 }
Alan Cox1e999732007-04-11 00:23:13 +01001192
Tejun Heoc728a912007-09-03 12:32:30 +09001193 if (lba48)
Tejun Heoa5987e02008-03-27 19:14:23 +09001194 *max_sectors = ata_tf_to_lba48(&tf) + 1;
Tejun Heoc728a912007-09-03 12:32:30 +09001195 else
Tejun Heoa5987e02008-03-27 19:14:23 +09001196 *max_sectors = ata_tf_to_lba(&tf) + 1;
Jeff Garzik2dcb4072007-10-19 06:42:56 -04001197 if (dev->horkage & ATA_HORKAGE_HPA_SIZE)
Alan Cox93328e112007-09-29 04:06:48 -04001198 (*max_sectors)--;
Tejun Heoc728a912007-09-03 12:32:30 +09001199 return 0;
Alan Cox1e999732007-04-11 00:23:13 +01001200}
1201
1202/**
Tejun Heoc728a912007-09-03 12:32:30 +09001203 * ata_set_max_sectors - Set max sectors
1204 * @dev: target device
Randy Dunlap6b38d1d2007-05-01 17:35:55 -07001205 * @new_sectors: new max sectors value to set for the device
Alan Cox1e999732007-04-11 00:23:13 +01001206 *
Tejun Heoc728a912007-09-03 12:32:30 +09001207 * Set max sectors of @dev to @new_sectors.
1208 *
1209 * RETURNS:
1210 * 0 on success, -EACCES if command is aborted or denied (due to
1211 * previous non-volatile SET_MAX) by the drive. -EIO on other
1212 * errors.
Alan Cox1e999732007-04-11 00:23:13 +01001213 */
Tejun Heo05027ad2007-09-03 12:32:57 +09001214static int ata_set_max_sectors(struct ata_device *dev, u64 new_sectors)
Alan Cox1e999732007-04-11 00:23:13 +01001215{
Tejun Heoc728a912007-09-03 12:32:30 +09001216 unsigned int err_mask;
Alan Cox1e999732007-04-11 00:23:13 +01001217 struct ata_taskfile tf;
Tejun Heoc728a912007-09-03 12:32:30 +09001218 int lba48 = ata_id_has_lba48(dev->id);
Alan Cox1e999732007-04-11 00:23:13 +01001219
1220 new_sectors--;
1221
1222 ata_tf_init(dev, &tf);
1223
Alan Cox1e999732007-04-11 00:23:13 +01001224 tf.flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
Tejun Heoc728a912007-09-03 12:32:30 +09001225
1226 if (lba48) {
1227 tf.command = ATA_CMD_SET_MAX_EXT;
1228 tf.flags |= ATA_TFLAG_LBA48;
1229
1230 tf.hob_lbal = (new_sectors >> 24) & 0xff;
1231 tf.hob_lbam = (new_sectors >> 32) & 0xff;
1232 tf.hob_lbah = (new_sectors >> 40) & 0xff;
Tejun Heo1e582ba2007-09-21 20:07:14 +09001233 } else {
Tejun Heoc728a912007-09-03 12:32:30 +09001234 tf.command = ATA_CMD_SET_MAX;
1235
Tejun Heo1e582ba2007-09-21 20:07:14 +09001236 tf.device |= (new_sectors >> 24) & 0xf;
1237 }
1238
Hannes Reineckebd18bc02016-07-14 09:05:46 +09001239 tf.protocol = ATA_PROT_NODATA;
Tejun Heoc728a912007-09-03 12:32:30 +09001240 tf.device |= ATA_LBA;
Alan Cox1e999732007-04-11 00:23:13 +01001241
1242 tf.lbal = (new_sectors >> 0) & 0xff;
1243 tf.lbam = (new_sectors >> 8) & 0xff;
1244 tf.lbah = (new_sectors >> 16) & 0xff;
Alan Cox1e999732007-04-11 00:23:13 +01001245
Tejun Heo2b789102007-10-09 15:05:44 +09001246 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
Tejun Heoc728a912007-09-03 12:32:30 +09001247 if (err_mask) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07001248 ata_dev_warn(dev,
1249 "failed to set max address (err_mask=0x%x)\n",
1250 err_mask);
Tejun Heoc728a912007-09-03 12:32:30 +09001251 if (err_mask == AC_ERR_DEV &&
1252 (tf.feature & (ATA_ABORTED | ATA_IDNF)))
1253 return -EACCES;
1254 return -EIO;
1255 }
Alan Cox1e999732007-04-11 00:23:13 +01001256
Tejun Heoc728a912007-09-03 12:32:30 +09001257 return 0;
Alan Cox1e999732007-04-11 00:23:13 +01001258}
1259
1260/**
1261 * ata_hpa_resize - Resize a device with an HPA set
1262 * @dev: Device to resize
1263 *
1264 * Read the size of an LBA28 or LBA48 disk with HPA features and resize
1265 * it if required to the full size of the media. The caller must check
1266 * the drive has the HPA feature set enabled.
Tejun Heo05027ad2007-09-03 12:32:57 +09001267 *
1268 * RETURNS:
1269 * 0 on success, -errno on failure.
Alan Cox1e999732007-04-11 00:23:13 +01001270 */
Tejun Heo05027ad2007-09-03 12:32:57 +09001271static int ata_hpa_resize(struct ata_device *dev)
Alan Cox1e999732007-04-11 00:23:13 +01001272{
Damien Le Moal891fd7c2021-08-16 10:44:50 +09001273 bool print_info = ata_dev_print_info(dev);
Tejun Heo445d2112010-04-05 10:33:13 +09001274 bool unlock_hpa = ata_ignore_hpa || dev->flags & ATA_DFLAG_UNLOCK_HPA;
Tejun Heo05027ad2007-09-03 12:32:57 +09001275 u64 sectors = ata_id_n_sectors(dev->id);
1276 u64 native_sectors;
Tejun Heoc728a912007-09-03 12:32:30 +09001277 int rc;
Jeff Garzika617c092007-05-21 20:14:23 -04001278
Tejun Heo05027ad2007-09-03 12:32:57 +09001279 /* do we need to do it? */
Hannes Reinecke9162c652014-11-05 13:08:21 +01001280 if ((dev->class != ATA_DEV_ATA && dev->class != ATA_DEV_ZAC) ||
Tejun Heo05027ad2007-09-03 12:32:57 +09001281 !ata_id_has_lba(dev->id) || !ata_id_hpa_enabled(dev->id) ||
1282 (dev->horkage & ATA_HORKAGE_BROKEN_HPA))
Tejun Heoc728a912007-09-03 12:32:30 +09001283 return 0;
Alan Cox1e999732007-04-11 00:23:13 +01001284
Tejun Heo05027ad2007-09-03 12:32:57 +09001285 /* read native max address */
1286 rc = ata_read_native_max_address(dev, &native_sectors);
1287 if (rc) {
Tejun Heodda7aba2008-03-23 21:05:15 +09001288 /* If device aborted the command or HPA isn't going to
1289 * be unlocked, skip HPA resizing.
Tejun Heo05027ad2007-09-03 12:32:57 +09001290 */
Tejun Heo445d2112010-04-05 10:33:13 +09001291 if (rc == -EACCES || !unlock_hpa) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07001292 ata_dev_warn(dev,
1293 "HPA support seems broken, skipping HPA handling\n");
Tejun Heo05027ad2007-09-03 12:32:57 +09001294 dev->horkage |= ATA_HORKAGE_BROKEN_HPA;
Alan Cox1e999732007-04-11 00:23:13 +01001295
Tejun Heo05027ad2007-09-03 12:32:57 +09001296 /* we can continue if device aborted the command */
1297 if (rc == -EACCES)
1298 rc = 0;
Alan Cox1e999732007-04-11 00:23:13 +01001299 }
Tejun Heo37301a52007-06-25 20:45:54 +09001300
Tejun Heo05027ad2007-09-03 12:32:57 +09001301 return rc;
1302 }
Tejun Heo5920dad2009-07-15 17:11:41 +09001303 dev->n_native_sectors = native_sectors;
Tejun Heo05027ad2007-09-03 12:32:57 +09001304
1305 /* nothing to do? */
Tejun Heo445d2112010-04-05 10:33:13 +09001306 if (native_sectors <= sectors || !unlock_hpa) {
Tejun Heo05027ad2007-09-03 12:32:57 +09001307 if (!print_info || native_sectors == sectors)
1308 return 0;
1309
1310 if (native_sectors > sectors)
Joe Perchesa9a79df2011-04-15 15:51:59 -07001311 ata_dev_info(dev,
Tejun Heo05027ad2007-09-03 12:32:57 +09001312 "HPA detected: current %llu, native %llu\n",
1313 (unsigned long long)sectors,
1314 (unsigned long long)native_sectors);
1315 else if (native_sectors < sectors)
Joe Perchesa9a79df2011-04-15 15:51:59 -07001316 ata_dev_warn(dev,
1317 "native sectors (%llu) is smaller than sectors (%llu)\n",
Tejun Heo05027ad2007-09-03 12:32:57 +09001318 (unsigned long long)native_sectors,
1319 (unsigned long long)sectors);
1320 return 0;
1321 }
1322
1323 /* let's unlock HPA */
1324 rc = ata_set_max_sectors(dev, native_sectors);
1325 if (rc == -EACCES) {
1326 /* if device aborted the command, skip HPA resizing */
Joe Perchesa9a79df2011-04-15 15:51:59 -07001327 ata_dev_warn(dev,
1328 "device aborted resize (%llu -> %llu), skipping HPA handling\n",
1329 (unsigned long long)sectors,
1330 (unsigned long long)native_sectors);
Tejun Heo05027ad2007-09-03 12:32:57 +09001331 dev->horkage |= ATA_HORKAGE_BROKEN_HPA;
1332 return 0;
1333 } else if (rc)
1334 return rc;
1335
1336 /* re-read IDENTIFY data */
1337 rc = ata_dev_reread_id(dev, 0);
1338 if (rc) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07001339 ata_dev_err(dev,
1340 "failed to re-read IDENTIFY data after HPA resizing\n");
Tejun Heo05027ad2007-09-03 12:32:57 +09001341 return rc;
1342 }
1343
1344 if (print_info) {
1345 u64 new_sectors = ata_id_n_sectors(dev->id);
Joe Perchesa9a79df2011-04-15 15:51:59 -07001346 ata_dev_info(dev,
Tejun Heo05027ad2007-09-03 12:32:57 +09001347 "HPA unlocked: %llu -> %llu, native %llu\n",
1348 (unsigned long long)sectors,
1349 (unsigned long long)new_sectors,
1350 (unsigned long long)native_sectors);
1351 }
1352
1353 return 0;
Alan Cox1e999732007-04-11 00:23:13 +01001354}
1355
Edward Falk0baab862005-06-02 18:17:13 -04001356/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 * ata_dump_id - IDENTIFY DEVICE info debugging output
Tejun Heo0bd33002006-02-12 22:47:05 +09001358 * @id: IDENTIFY DEVICE page to dump
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 *
Tejun Heo0bd33002006-02-12 22:47:05 +09001360 * Dump selected 16-bit words from the given IDENTIFY DEVICE
1361 * page.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 *
1363 * LOCKING:
1364 * caller.
1365 */
1366
Tejun Heo0bd33002006-02-12 22:47:05 +09001367static inline void ata_dump_id(const u16 *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368{
1369 DPRINTK("49==0x%04x "
1370 "53==0x%04x "
1371 "63==0x%04x "
1372 "64==0x%04x "
1373 "75==0x%04x \n",
Tejun Heo0bd33002006-02-12 22:47:05 +09001374 id[49],
1375 id[53],
1376 id[63],
1377 id[64],
1378 id[75]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 DPRINTK("80==0x%04x "
1380 "81==0x%04x "
1381 "82==0x%04x "
1382 "83==0x%04x "
1383 "84==0x%04x \n",
Tejun Heo0bd33002006-02-12 22:47:05 +09001384 id[80],
1385 id[81],
1386 id[82],
1387 id[83],
1388 id[84]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 DPRINTK("88==0x%04x "
1390 "93==0x%04x\n",
Tejun Heo0bd33002006-02-12 22:47:05 +09001391 id[88],
1392 id[93]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393}
1394
Tejun Heocb95d562006-03-06 04:31:56 +09001395/**
1396 * ata_id_xfermask - Compute xfermask from the given IDENTIFY data
1397 * @id: IDENTIFY data to compute xfer mask from
1398 *
1399 * Compute the xfermask for this device. This is not as trivial
1400 * as it seems if we must consider early devices correctly.
1401 *
1402 * FIXME: pre IDE drive timing (do we care ?).
1403 *
1404 * LOCKING:
1405 * None.
1406 *
1407 * RETURNS:
1408 * Computed xfermask
1409 */
Tejun Heo7dc951a2007-11-27 19:43:42 +09001410unsigned long ata_id_xfermask(const u16 *id)
Tejun Heocb95d562006-03-06 04:31:56 +09001411{
Tejun Heo7dc951a2007-11-27 19:43:42 +09001412 unsigned long pio_mask, mwdma_mask, udma_mask;
Tejun Heocb95d562006-03-06 04:31:56 +09001413
1414 /* Usual case. Word 53 indicates word 64 is valid */
1415 if (id[ATA_ID_FIELD_VALID] & (1 << 1)) {
1416 pio_mask = id[ATA_ID_PIO_MODES] & 0x03;
1417 pio_mask <<= 3;
1418 pio_mask |= 0x7;
1419 } else {
1420 /* If word 64 isn't valid then Word 51 high byte holds
1421 * the PIO timing number for the maximum. Turn it into
1422 * a mask.
1423 */
Lennert Buytenhek7a0f1c82007-01-29 13:28:47 +01001424 u8 mode = (id[ATA_ID_OLD_PIO_MODES] >> 8) & 0xFF;
Alan Cox46767aeb2006-09-29 18:26:47 +01001425 if (mode < 5) /* Valid PIO range */
Jeff Garzik2dcb4072007-10-19 06:42:56 -04001426 pio_mask = (2 << mode) - 1;
Alan Cox46767aeb2006-09-29 18:26:47 +01001427 else
1428 pio_mask = 1;
Tejun Heocb95d562006-03-06 04:31:56 +09001429
1430 /* But wait.. there's more. Design your standards by
1431 * committee and you too can get a free iordy field to
1432 * process. However its the speeds not the modes that
1433 * are supported... Note drivers using the timing API
1434 * will get this right anyway
1435 */
1436 }
1437
1438 mwdma_mask = id[ATA_ID_MWDMA_MODES] & 0x07;
Tejun Heofb21f0d2006-03-12 12:34:35 +09001439
Alan Coxb352e572006-08-10 18:52:12 +01001440 if (ata_id_is_cfa(id)) {
1441 /*
1442 * Process compact flash extended modes
1443 */
Sergei Shtylyov62afe5d2009-04-13 20:50:00 +04001444 int pio = (id[ATA_ID_CFA_MODES] >> 0) & 0x7;
1445 int dma = (id[ATA_ID_CFA_MODES] >> 3) & 0x7;
Alan Coxb352e572006-08-10 18:52:12 +01001446
1447 if (pio)
1448 pio_mask |= (1 << 5);
1449 if (pio > 1)
1450 pio_mask |= (1 << 6);
1451 if (dma)
1452 mwdma_mask |= (1 << 3);
1453 if (dma > 1)
1454 mwdma_mask |= (1 << 4);
1455 }
1456
Tejun Heofb21f0d2006-03-12 12:34:35 +09001457 udma_mask = 0;
1458 if (id[ATA_ID_FIELD_VALID] & (1 << 2))
1459 udma_mask = id[ATA_ID_UDMA_MODES] & 0xff;
Tejun Heocb95d562006-03-06 04:31:56 +09001460
1461 return ata_pack_xfermask(pio_mask, mwdma_mask, udma_mask);
1462}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01001463EXPORT_SYMBOL_GPL(ata_id_xfermask);
Tejun Heocb95d562006-03-06 04:31:56 +09001464
Adrian Bunk7102d232007-01-04 00:09:36 +01001465static void ata_qc_complete_internal(struct ata_queued_cmd *qc)
Tejun Heoa2a7a662005-12-13 14:48:31 +09001466{
Tejun Heo77853bf2006-01-23 13:09:36 +09001467 struct completion *waiting = qc->private_data;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001468
Tejun Heoa2a7a662005-12-13 14:48:31 +09001469 complete(waiting);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001470}
1471
1472/**
Tejun Heo24326972006-11-14 22:47:09 +09001473 * ata_exec_internal_sg - execute libata internal command
Tejun Heoa2a7a662005-12-13 14:48:31 +09001474 * @dev: Device to which the command is sent
1475 * @tf: Taskfile registers for the command and the result
Tejun Heod69cf372006-04-02 18:51:53 +09001476 * @cdb: CDB for packet command
Masanari Iidae2278672014-02-18 22:54:36 +09001477 * @dma_dir: Data transfer direction of the command
Randy Dunlap5c1ad8b2007-10-18 14:12:26 -07001478 * @sgl: sg list for the data buffer of the command
Tejun Heo24326972006-11-14 22:47:09 +09001479 * @n_elem: Number of sg entries
Tejun Heo2b789102007-10-09 15:05:44 +09001480 * @timeout: Timeout in msecs (0 for default)
Tejun Heoa2a7a662005-12-13 14:48:31 +09001481 *
1482 * Executes libata internal command with timeout. @tf contains
1483 * command on entry and result on return. Timeout and error
1484 * conditions are reported via return value. No recovery action
1485 * is taken after a command times out. It's caller's duty to
1486 * clean up after timeout.
1487 *
1488 * LOCKING:
1489 * None. Should be called with kernel context, might sleep.
Tejun Heo551e8882006-06-12 14:09:49 +09001490 *
1491 * RETURNS:
1492 * Zero on success, AC_ERR_* mask on failure
Tejun Heoa2a7a662005-12-13 14:48:31 +09001493 */
Tejun Heo24326972006-11-14 22:47:09 +09001494unsigned ata_exec_internal_sg(struct ata_device *dev,
1495 struct ata_taskfile *tf, const u8 *cdb,
Jens Axboe87260212007-10-16 11:14:12 +02001496 int dma_dir, struct scatterlist *sgl,
Tejun Heo2b789102007-10-09 15:05:44 +09001497 unsigned int n_elem, unsigned long timeout)
Tejun Heoa2a7a662005-12-13 14:48:31 +09001498{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001499 struct ata_link *link = dev->link;
1500 struct ata_port *ap = link->ap;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001501 u8 command = tf->command;
Tejun Heo87fbc5a2008-05-20 02:17:54 +09001502 int auto_timeout = 0;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001503 struct ata_queued_cmd *qc;
Jens Axboe28361c42018-05-11 12:51:09 -06001504 unsigned int preempted_tag;
Jens Axboee3ed89392018-05-11 12:51:05 -06001505 u32 preempted_sactive;
1506 u64 preempted_qc_active;
Tejun Heoda917d62007-09-23 13:14:12 +09001507 int preempted_nr_active_links;
Ingo Molnar60be6b92006-07-03 00:25:26 -07001508 DECLARE_COMPLETION_ONSTACK(wait);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001509 unsigned long flags;
Tejun Heo77853bf2006-01-23 13:09:36 +09001510 unsigned int err_mask;
Tejun Heod95a7172006-05-15 20:58:14 +09001511 int rc;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001512
Jeff Garzikba6a1302006-06-22 23:46:10 -04001513 spin_lock_irqsave(ap->lock, flags);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001514
Tejun Heoe3180492006-05-15 20:58:09 +09001515 /* no internal command while frozen */
Tejun Heob51e9e52006-06-29 01:29:30 +09001516 if (ap->pflags & ATA_PFLAG_FROZEN) {
Jeff Garzikba6a1302006-06-22 23:46:10 -04001517 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoe3180492006-05-15 20:58:09 +09001518 return AC_ERR_SYSTEM;
1519 }
1520
Tejun Heo2ab7db12006-05-15 20:58:02 +09001521 /* initialize internal qc */
Jens Axboe28361c42018-05-11 12:51:09 -06001522 qc = __ata_qc_from_tag(ap, ATA_TAG_INTERNAL);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001523
Jens Axboe28361c42018-05-11 12:51:09 -06001524 qc->tag = ATA_TAG_INTERNAL;
1525 qc->hw_tag = 0;
Tejun Heo2ab7db12006-05-15 20:58:02 +09001526 qc->scsicmd = NULL;
1527 qc->ap = ap;
1528 qc->dev = dev;
1529 ata_qc_reinit(qc);
1530
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001531 preempted_tag = link->active_tag;
1532 preempted_sactive = link->sactive;
Tejun Heodedaf2b2006-05-15 21:03:43 +09001533 preempted_qc_active = ap->qc_active;
Tejun Heoda917d62007-09-23 13:14:12 +09001534 preempted_nr_active_links = ap->nr_active_links;
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001535 link->active_tag = ATA_TAG_POISON;
1536 link->sactive = 0;
Tejun Heodedaf2b2006-05-15 21:03:43 +09001537 ap->qc_active = 0;
Tejun Heoda917d62007-09-23 13:14:12 +09001538 ap->nr_active_links = 0;
Tejun Heo2ab7db12006-05-15 20:58:02 +09001539
1540 /* prepare & issue qc */
Tejun Heoa2a7a662005-12-13 14:48:31 +09001541 qc->tf = *tf;
Tejun Heod69cf372006-04-02 18:51:53 +09001542 if (cdb)
1543 memcpy(qc->cdb, cdb, ATAPI_CDB_LEN);
Vincent Pelletiere7714512013-05-18 18:44:04 +02001544
1545 /* some SATA bridges need us to indicate data xfer direction */
1546 if (tf->protocol == ATAPI_PROT_DMA && (dev->flags & ATA_DFLAG_DMADIR) &&
1547 dma_dir == DMA_FROM_DEVICE)
1548 qc->tf.feature |= ATAPI_DMADIR;
1549
Tejun Heoe61e0672006-05-15 20:57:40 +09001550 qc->flags |= ATA_QCFLAG_RESULT_TF;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001551 qc->dma_dir = dma_dir;
1552 if (dma_dir != DMA_NONE) {
Tejun Heo24326972006-11-14 22:47:09 +09001553 unsigned int i, buflen = 0;
Jens Axboe87260212007-10-16 11:14:12 +02001554 struct scatterlist *sg;
Tejun Heo24326972006-11-14 22:47:09 +09001555
Jens Axboe87260212007-10-16 11:14:12 +02001556 for_each_sg(sgl, sg, n_elem, i)
1557 buflen += sg->length;
Tejun Heo24326972006-11-14 22:47:09 +09001558
Jens Axboe87260212007-10-16 11:14:12 +02001559 ata_sg_init(qc, sgl, n_elem);
Brian King49c80422007-01-30 11:32:26 -06001560 qc->nbytes = buflen;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001561 }
1562
Tejun Heo77853bf2006-01-23 13:09:36 +09001563 qc->private_data = &wait;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001564 qc->complete_fn = ata_qc_complete_internal;
1565
Tejun Heo8e0e6942006-03-31 20:41:11 +09001566 ata_qc_issue(qc);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001567
Jeff Garzikba6a1302006-06-22 23:46:10 -04001568 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001569
Tejun Heo87fbc5a2008-05-20 02:17:54 +09001570 if (!timeout) {
1571 if (ata_probe_timeout)
1572 timeout = ata_probe_timeout * 1000;
1573 else {
1574 timeout = ata_internal_cmd_timeout(dev, command);
1575 auto_timeout = 1;
1576 }
1577 }
Tejun Heo2b789102007-10-09 15:05:44 +09001578
Tejun Heoc0c362b2010-09-06 17:57:14 +02001579 if (ap->ops->error_handler)
1580 ata_eh_release(ap);
1581
Tejun Heo2b789102007-10-09 15:05:44 +09001582 rc = wait_for_completion_timeout(&wait, msecs_to_jiffies(timeout));
Albert Lee41ade502006-03-14 11:19:04 +08001583
Tejun Heoc0c362b2010-09-06 17:57:14 +02001584 if (ap->ops->error_handler)
1585 ata_eh_acquire(ap);
1586
Tejun Heoc4291372010-05-10 21:41:38 +02001587 ata_sff_flush_pio_task(ap);
Tejun Heod95a7172006-05-15 20:58:14 +09001588
1589 if (!rc) {
Jeff Garzikba6a1302006-06-22 23:46:10 -04001590 spin_lock_irqsave(ap->lock, flags);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001591
1592 /* We're racing with irq here. If we lose, the
1593 * following test prevents us from completing the qc
Tejun Heod95a7172006-05-15 20:58:14 +09001594 * twice. If we win, the port is frozen and will be
1595 * cleaned up by ->post_internal_cmd().
Tejun Heoa2a7a662005-12-13 14:48:31 +09001596 */
Tejun Heo77853bf2006-01-23 13:09:36 +09001597 if (qc->flags & ATA_QCFLAG_ACTIVE) {
Tejun Heod95a7172006-05-15 20:58:14 +09001598 qc->err_mask |= AC_ERR_TIMEOUT;
1599
1600 if (ap->ops->error_handler)
1601 ata_port_freeze(ap);
1602 else
1603 ata_qc_complete(qc);
Tejun Heof15a1da2006-05-15 20:57:56 +09001604
Borislav Petkov0dd4b212006-06-23 02:29:08 -04001605 if (ata_msg_warn(ap))
Joe Perchesa9a79df2011-04-15 15:51:59 -07001606 ata_dev_warn(dev, "qc timeout (cmd 0x%x)\n",
1607 command);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001608 }
1609
Jeff Garzikba6a1302006-06-22 23:46:10 -04001610 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001611 }
1612
Tejun Heod95a7172006-05-15 20:58:14 +09001613 /* do post_internal_cmd */
1614 if (ap->ops->post_internal_cmd)
1615 ap->ops->post_internal_cmd(qc);
1616
Tejun Heoa51d6442007-03-20 15:24:11 +09001617 /* perform minimal error analysis */
1618 if (qc->flags & ATA_QCFLAG_FAILED) {
1619 if (qc->result_tf.command & (ATA_ERR | ATA_DF))
1620 qc->err_mask |= AC_ERR_DEV;
1621
1622 if (!qc->err_mask)
1623 qc->err_mask |= AC_ERR_OTHER;
1624
1625 if (qc->err_mask & ~AC_ERR_OTHER)
1626 qc->err_mask &= ~AC_ERR_OTHER;
Damien Le Moal2dae9952016-12-19 10:17:40 +09001627 } else if (qc->tf.command == ATA_CMD_REQ_SENSE_DATA) {
1628 qc->result_tf.command |= ATA_SENSE;
Tejun Heod95a7172006-05-15 20:58:14 +09001629 }
1630
Tejun Heo15869302006-05-15 20:57:33 +09001631 /* finish up */
Jeff Garzikba6a1302006-06-22 23:46:10 -04001632 spin_lock_irqsave(ap->lock, flags);
Tejun Heo15869302006-05-15 20:57:33 +09001633
Tejun Heoe61e0672006-05-15 20:57:40 +09001634 *tf = qc->result_tf;
Tejun Heo77853bf2006-01-23 13:09:36 +09001635 err_mask = qc->err_mask;
1636
1637 ata_qc_free(qc);
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001638 link->active_tag = preempted_tag;
1639 link->sactive = preempted_sactive;
Tejun Heodedaf2b2006-05-15 21:03:43 +09001640 ap->qc_active = preempted_qc_active;
Tejun Heoda917d62007-09-23 13:14:12 +09001641 ap->nr_active_links = preempted_nr_active_links;
Tejun Heo77853bf2006-01-23 13:09:36 +09001642
Jeff Garzikba6a1302006-06-22 23:46:10 -04001643 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heo15869302006-05-15 20:57:33 +09001644
Tejun Heo87fbc5a2008-05-20 02:17:54 +09001645 if ((err_mask & AC_ERR_TIMEOUT) && auto_timeout)
1646 ata_internal_cmd_timed_out(dev, command);
1647
Tejun Heo77853bf2006-01-23 13:09:36 +09001648 return err_mask;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001649}
1650
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651/**
Tejun Heo33480a02006-12-12 02:15:31 +09001652 * ata_exec_internal - execute libata internal command
Tejun Heo24326972006-11-14 22:47:09 +09001653 * @dev: Device to which the command is sent
1654 * @tf: Taskfile registers for the command and the result
1655 * @cdb: CDB for packet command
Masanari Iidae2278672014-02-18 22:54:36 +09001656 * @dma_dir: Data transfer direction of the command
Tejun Heo24326972006-11-14 22:47:09 +09001657 * @buf: Data buffer of the command
1658 * @buflen: Length of data buffer
Tejun Heo2b789102007-10-09 15:05:44 +09001659 * @timeout: Timeout in msecs (0 for default)
Tejun Heo24326972006-11-14 22:47:09 +09001660 *
1661 * Wrapper around ata_exec_internal_sg() which takes simple
1662 * buffer instead of sg list.
1663 *
1664 * LOCKING:
1665 * None. Should be called with kernel context, might sleep.
1666 *
1667 * RETURNS:
1668 * Zero on success, AC_ERR_* mask on failure
1669 */
1670unsigned ata_exec_internal(struct ata_device *dev,
1671 struct ata_taskfile *tf, const u8 *cdb,
Tejun Heo2b789102007-10-09 15:05:44 +09001672 int dma_dir, void *buf, unsigned int buflen,
1673 unsigned long timeout)
Tejun Heo24326972006-11-14 22:47:09 +09001674{
Tejun Heo33480a02006-12-12 02:15:31 +09001675 struct scatterlist *psg = NULL, sg;
1676 unsigned int n_elem = 0;
Tejun Heo24326972006-11-14 22:47:09 +09001677
Tejun Heo33480a02006-12-12 02:15:31 +09001678 if (dma_dir != DMA_NONE) {
1679 WARN_ON(!buf);
1680 sg_init_one(&sg, buf, buflen);
1681 psg = &sg;
1682 n_elem++;
1683 }
Tejun Heo24326972006-11-14 22:47:09 +09001684
Tejun Heo2b789102007-10-09 15:05:44 +09001685 return ata_exec_internal_sg(dev, tf, cdb, dma_dir, psg, n_elem,
1686 timeout);
Tejun Heo24326972006-11-14 22:47:09 +09001687}
1688
1689/**
Alan Cox1bc4ccf2006-01-09 17:18:14 +00001690 * ata_pio_need_iordy - check if iordy needed
1691 * @adev: ATA device
1692 *
1693 * Check if the current speed of the device requires IORDY. Used
1694 * by various controllers for chip configuration.
1695 */
Alan Cox1bc4ccf2006-01-09 17:18:14 +00001696unsigned int ata_pio_need_iordy(const struct ata_device *adev)
1697{
Tejun Heo0d9e6652009-06-11 11:04:45 +09001698 /* Don't set IORDY if we're preparing for reset. IORDY may
1699 * lead to controller lock up on certain controllers if the
1700 * port is not occupied. See bko#11703 for details.
1701 */
1702 if (adev->link->ap->pflags & ATA_PFLAG_RESETTING)
1703 return 0;
1704 /* Controller doesn't support IORDY. Probably a pointless
1705 * check as the caller should know this.
1706 */
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001707 if (adev->link->ap->flags & ATA_FLAG_NO_IORDY)
Alan Cox1bc4ccf2006-01-09 17:18:14 +00001708 return 0;
David Daney5c18c4d2008-12-10 15:39:12 -08001709 /* CF spec. r4.1 Table 22 says no iordy on PIO5 and PIO6. */
1710 if (ata_id_is_cfa(adev->id)
1711 && (adev->pio_mode == XFER_PIO_5 || adev->pio_mode == XFER_PIO_6))
1712 return 0;
Alan Cox432729f2007-03-08 23:22:59 +00001713 /* PIO3 and higher it is mandatory */
1714 if (adev->pio_mode > XFER_PIO_2)
Alan Cox1bc4ccf2006-01-09 17:18:14 +00001715 return 1;
Alan Cox432729f2007-03-08 23:22:59 +00001716 /* We turn it on when possible */
1717 if (ata_id_has_iordy(adev->id))
1718 return 1;
1719 return 0;
1720}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01001721EXPORT_SYMBOL_GPL(ata_pio_need_iordy);
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05001722
Alan Cox432729f2007-03-08 23:22:59 +00001723/**
1724 * ata_pio_mask_no_iordy - Return the non IORDY mask
1725 * @adev: ATA device
1726 *
1727 * Compute the highest mode possible if we are not using iordy. Return
1728 * -1 if no iordy mode is available.
1729 */
Alan Cox432729f2007-03-08 23:22:59 +00001730static u32 ata_pio_mask_no_iordy(const struct ata_device *adev)
1731{
Alan Cox1bc4ccf2006-01-09 17:18:14 +00001732 /* If we have no drive specific rule, then PIO 2 is non IORDY */
Alan Cox1bc4ccf2006-01-09 17:18:14 +00001733 if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE */
Alan Cox432729f2007-03-08 23:22:59 +00001734 u16 pio = adev->id[ATA_ID_EIDE_PIO];
Alan Cox1bc4ccf2006-01-09 17:18:14 +00001735 /* Is the speed faster than the drive allows non IORDY ? */
1736 if (pio) {
1737 /* This is cycle times not frequency - watch the logic! */
1738 if (pio > 240) /* PIO2 is 240nS per cycle */
Alan Cox432729f2007-03-08 23:22:59 +00001739 return 3 << ATA_SHIFT_PIO;
1740 return 7 << ATA_SHIFT_PIO;
Alan Cox1bc4ccf2006-01-09 17:18:14 +00001741 }
1742 }
Alan Cox432729f2007-03-08 23:22:59 +00001743 return 3 << ATA_SHIFT_PIO;
Alan Cox1bc4ccf2006-01-09 17:18:14 +00001744}
1745
1746/**
Alan Cox963e4972008-07-24 17:16:06 +01001747 * ata_do_dev_read_id - default ID read method
1748 * @dev: device
1749 * @tf: proposed taskfile
1750 * @id: data buffer
1751 *
1752 * Issue the identify taskfile and hand back the buffer containing
1753 * identify data. For some RAID controllers and for pre ATA devices
1754 * this function is wrapped or replaced by the driver
1755 */
1756unsigned int ata_do_dev_read_id(struct ata_device *dev,
1757 struct ata_taskfile *tf, u16 *id)
1758{
1759 return ata_exec_internal(dev, tf, NULL, DMA_FROM_DEVICE,
1760 id, sizeof(id[0]) * ATA_ID_WORDS, 0);
1761}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01001762EXPORT_SYMBOL_GPL(ata_do_dev_read_id);
Alan Cox963e4972008-07-24 17:16:06 +01001763
1764/**
Tejun Heo49016ac2006-02-21 02:12:11 +09001765 * ata_dev_read_id - Read ID data from the specified device
Tejun Heo49016ac2006-02-21 02:12:11 +09001766 * @dev: target device
1767 * @p_class: pointer to class of the target device (may be changed)
Tejun Heobff04642006-11-10 18:08:10 +09001768 * @flags: ATA_READID_* flags
Tejun Heofe635c72006-05-15 20:57:35 +09001769 * @id: buffer to read IDENTIFY data into
Tejun Heo49016ac2006-02-21 02:12:11 +09001770 *
1771 * Read ID data from the specified device. ATA_CMD_ID_ATA is
1772 * performed on ATA devices and ATA_CMD_ID_ATAPI on ATAPI
Tejun Heoaec5c3c2006-03-25 01:33:34 +09001773 * devices. This function also issues ATA_CMD_INIT_DEV_PARAMS
1774 * for pre-ATA4 drives.
Tejun Heo49016ac2006-02-21 02:12:11 +09001775 *
Alan Cox50a99012007-08-08 14:27:00 +01001776 * FIXME: ATA_CMD_ID_ATA is optional for early drives and right
Jeff Garzik2dcb4072007-10-19 06:42:56 -04001777 * now we abort if we hit that case.
Alan Cox50a99012007-08-08 14:27:00 +01001778 *
Tejun Heo49016ac2006-02-21 02:12:11 +09001779 * LOCKING:
1780 * Kernel thread context (may sleep)
1781 *
1782 * RETURNS:
1783 * 0 on success, -errno otherwise.
1784 */
Tejun Heoa9beec92006-05-31 18:27:44 +09001785int ata_dev_read_id(struct ata_device *dev, unsigned int *p_class,
Tejun Heobff04642006-11-10 18:08:10 +09001786 unsigned int flags, u16 *id)
Tejun Heo49016ac2006-02-21 02:12:11 +09001787{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001788 struct ata_port *ap = dev->link->ap;
Tejun Heo49016ac2006-02-21 02:12:11 +09001789 unsigned int class = *p_class;
Tejun Heo49016ac2006-02-21 02:12:11 +09001790 struct ata_taskfile tf;
1791 unsigned int err_mask = 0;
1792 const char *reason;
Tejun Heo79b42ba2009-04-15 06:21:10 +09001793 bool is_semb = class == ATA_DEV_SEMB;
Tejun Heo54936f82007-05-11 14:35:29 +02001794 int may_fallback = 1, tried_spinup = 0;
Tejun Heo49016ac2006-02-21 02:12:11 +09001795 int rc;
1796
Borislav Petkov0dd4b212006-06-23 02:29:08 -04001797 if (ata_msg_ctl(ap))
Joe Perchesa9a79df2011-04-15 15:51:59 -07001798 ata_dev_dbg(dev, "%s: ENTER\n", __func__);
Tejun Heo49016ac2006-02-21 02:12:11 +09001799
Alan Cox963e4972008-07-24 17:16:06 +01001800retry:
Tejun Heo3373efd2006-05-15 20:57:53 +09001801 ata_tf_init(dev, &tf);
Tejun Heo49016ac2006-02-21 02:12:11 +09001802
1803 switch (class) {
Tejun Heo79b42ba2009-04-15 06:21:10 +09001804 case ATA_DEV_SEMB:
1805 class = ATA_DEV_ATA; /* some hard drives report SEMB sig */
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05001806 fallthrough;
Tejun Heo49016ac2006-02-21 02:12:11 +09001807 case ATA_DEV_ATA:
Hannes Reinecke9162c652014-11-05 13:08:21 +01001808 case ATA_DEV_ZAC:
Tejun Heo49016ac2006-02-21 02:12:11 +09001809 tf.command = ATA_CMD_ID_ATA;
1810 break;
1811 case ATA_DEV_ATAPI:
1812 tf.command = ATA_CMD_ID_ATAPI;
1813 break;
1814 default:
1815 rc = -ENODEV;
1816 reason = "unsupported class";
1817 goto err_out;
1818 }
1819
1820 tf.protocol = ATA_PROT_PIO;
Tejun Heo81afe892007-02-07 12:37:41 -08001821
1822 /* Some devices choke if TF registers contain garbage. Make
1823 * sure those are properly initialized.
1824 */
1825 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
1826
1827 /* Device presence detection is unreliable on some
1828 * controllers. Always poll IDENTIFY if available.
1829 */
1830 tf.flags |= ATA_TFLAG_POLLING;
Tejun Heo49016ac2006-02-21 02:12:11 +09001831
Alan Cox963e4972008-07-24 17:16:06 +01001832 if (ap->ops->read_id)
1833 err_mask = ap->ops->read_id(dev, &tf, id);
1834 else
1835 err_mask = ata_do_dev_read_id(dev, &tf, id);
1836
Tejun Heo49016ac2006-02-21 02:12:11 +09001837 if (err_mask) {
Tejun Heo800b3992006-12-03 21:34:13 +09001838 if (err_mask & AC_ERR_NODEV_HINT) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07001839 ata_dev_dbg(dev, "NODEV after polling detection\n");
Tejun Heo55a8e2c2006-11-10 18:08:10 +09001840 return -ENOENT;
1841 }
1842
Tejun Heo79b42ba2009-04-15 06:21:10 +09001843 if (is_semb) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07001844 ata_dev_info(dev,
1845 "IDENTIFY failed on device w/ SEMB sig, disabled\n");
Tejun Heo79b42ba2009-04-15 06:21:10 +09001846 /* SEMB is not supported yet */
1847 *p_class = ATA_DEV_SEMB_UNSUP;
1848 return 0;
1849 }
1850
Tejun Heo1ffc1512008-03-23 15:16:53 +09001851 if ((err_mask == AC_ERR_DEV) && (tf.feature & ATA_ABORTED)) {
1852 /* Device or controller might have reported
1853 * the wrong device class. Give a shot at the
1854 * other IDENTIFY if the current one is
1855 * aborted by the device.
1856 */
1857 if (may_fallback) {
1858 may_fallback = 0;
Tejun Heo54936f82007-05-11 14:35:29 +02001859
Tejun Heo1ffc1512008-03-23 15:16:53 +09001860 if (class == ATA_DEV_ATA)
1861 class = ATA_DEV_ATAPI;
1862 else
1863 class = ATA_DEV_ATA;
1864 goto retry;
1865 }
1866
1867 /* Control reaches here iff the device aborted
1868 * both flavors of IDENTIFYs which happens
1869 * sometimes with phantom devices.
1870 */
Joe Perchesa9a79df2011-04-15 15:51:59 -07001871 ata_dev_dbg(dev,
1872 "both IDENTIFYs aborted, assuming NODEV\n");
Tejun Heo1ffc1512008-03-23 15:16:53 +09001873 return -ENOENT;
Tejun Heo54936f82007-05-11 14:35:29 +02001874 }
1875
Tejun Heo49016ac2006-02-21 02:12:11 +09001876 rc = -EIO;
1877 reason = "I/O error";
Tejun Heo49016ac2006-02-21 02:12:11 +09001878 goto err_out;
1879 }
1880
Tejun Heo43c9c592010-05-23 12:59:11 +02001881 if (dev->horkage & ATA_HORKAGE_DUMP_ID) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07001882 ata_dev_dbg(dev, "dumping IDENTIFY data, "
1883 "class=%d may_fallback=%d tried_spinup=%d\n",
1884 class, may_fallback, tried_spinup);
Tejun Heo43c9c592010-05-23 12:59:11 +02001885 print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET,
1886 16, 2, id, ATA_ID_WORDS * sizeof(*id), true);
1887 }
1888
Tejun Heo54936f82007-05-11 14:35:29 +02001889 /* Falling back doesn't make sense if ID data was read
1890 * successfully at least once.
1891 */
1892 may_fallback = 0;
1893
Tejun Heo49016ac2006-02-21 02:12:11 +09001894 swap_buf_le16(id, ATA_ID_WORDS);
1895
Tejun Heo49016ac2006-02-21 02:12:11 +09001896 /* sanity check */
Tejun Heoa4f57492006-09-12 20:35:49 -07001897 rc = -EINVAL;
Alan Cox60700682007-06-07 16:13:55 +01001898 reason = "device reports invalid type";
Tejun Heoa4f57492006-09-12 20:35:49 -07001899
Hannes Reinecke9162c652014-11-05 13:08:21 +01001900 if (class == ATA_DEV_ATA || class == ATA_DEV_ZAC) {
Tejun Heoa4f57492006-09-12 20:35:49 -07001901 if (!ata_id_is_ata(id) && !ata_id_is_cfa(id))
1902 goto err_out;
Andy Whitcroftdb63a4c2012-05-04 22:15:10 +01001903 if (ap->host->flags & ATA_HOST_IGNORE_ATA &&
1904 ata_id_is_ata(id)) {
1905 ata_dev_dbg(dev,
1906 "host indicates ignore ATA devices, ignored\n");
1907 return -ENOENT;
1908 }
Tejun Heoa4f57492006-09-12 20:35:49 -07001909 } else {
1910 if (ata_id_is_ata(id))
1911 goto err_out;
Tejun Heo49016ac2006-02-21 02:12:11 +09001912 }
1913
Mark Lord169439c2007-04-17 18:26:07 -04001914 if (!tried_spinup && (id[2] == 0x37c8 || id[2] == 0x738c)) {
1915 tried_spinup = 1;
1916 /*
1917 * Drive powered-up in standby mode, and requires a specific
1918 * SET_FEATURES spin-up subcommand before it will accept
1919 * anything other than the original IDENTIFY command.
1920 */
Jeff Garzik218f3d32007-10-25 00:33:27 -04001921 err_mask = ata_dev_set_feature(dev, SETFEATURES_SPINUP, 0);
Ryan Powerfb0582f92007-08-10 13:59:35 -07001922 if (err_mask && id[2] != 0x738c) {
Mark Lord169439c2007-04-17 18:26:07 -04001923 rc = -EIO;
1924 reason = "SPINUP failed";
1925 goto err_out;
1926 }
1927 /*
1928 * If the drive initially returned incomplete IDENTIFY info,
1929 * we now must reissue the IDENTIFY command.
1930 */
1931 if (id[2] == 0x37c8)
1932 goto retry;
1933 }
1934
Hannes Reinecke9162c652014-11-05 13:08:21 +01001935 if ((flags & ATA_READID_POSTRESET) &&
1936 (class == ATA_DEV_ATA || class == ATA_DEV_ZAC)) {
Tejun Heo49016ac2006-02-21 02:12:11 +09001937 /*
1938 * The exact sequence expected by certain pre-ATA4 drives is:
1939 * SRST RESET
Alan Cox50a99012007-08-08 14:27:00 +01001940 * IDENTIFY (optional in early ATA)
1941 * INITIALIZE DEVICE PARAMETERS (later IDE and ATA)
Tejun Heo49016ac2006-02-21 02:12:11 +09001942 * anything else..
1943 * Some drives were very specific about that exact sequence.
Alan Cox50a99012007-08-08 14:27:00 +01001944 *
1945 * Note that ATA4 says lba is mandatory so the second check
Adam Buchbinderc9404c92009-12-18 15:40:42 -05001946 * should never trigger.
Tejun Heo49016ac2006-02-21 02:12:11 +09001947 */
1948 if (ata_id_major_version(id) < 4 || !ata_id_has_lba(id)) {
Tejun Heo3373efd2006-05-15 20:57:53 +09001949 err_mask = ata_dev_init_params(dev, id[3], id[6]);
Tejun Heo49016ac2006-02-21 02:12:11 +09001950 if (err_mask) {
1951 rc = -EIO;
1952 reason = "INIT_DEV_PARAMS failed";
1953 goto err_out;
1954 }
1955
1956 /* current CHS translation info (id[53-58]) might be
1957 * changed. reread the identify device info.
1958 */
Tejun Heobff04642006-11-10 18:08:10 +09001959 flags &= ~ATA_READID_POSTRESET;
Tejun Heo49016ac2006-02-21 02:12:11 +09001960 goto retry;
1961 }
1962 }
1963
1964 *p_class = class;
Tejun Heofe635c72006-05-15 20:57:35 +09001965
Tejun Heo49016ac2006-02-21 02:12:11 +09001966 return 0;
1967
1968 err_out:
Tejun Heo88574552006-06-25 20:00:35 +09001969 if (ata_msg_warn(ap))
Joe Perchesa9a79df2011-04-15 15:51:59 -07001970 ata_dev_warn(dev, "failed to IDENTIFY (%s, err_mask=0x%x)\n",
1971 reason, err_mask);
Tejun Heo49016ac2006-02-21 02:12:11 +09001972 return rc;
1973}
1974
Christoph Hellwigf01f62c2017-06-04 14:42:20 +02001975/**
1976 * ata_read_log_page - read a specific log page
1977 * @dev: target device
1978 * @log: log to read
1979 * @page: page to read
1980 * @buf: buffer to store read page
1981 * @sectors: number of sectors to read
1982 *
1983 * Read log page using READ_LOG_EXT command.
1984 *
1985 * LOCKING:
1986 * Kernel thread context (may sleep).
1987 *
1988 * RETURNS:
1989 * 0 on success, AC_ERR_* mask otherwise.
1990 */
1991unsigned int ata_read_log_page(struct ata_device *dev, u8 log,
1992 u8 page, void *buf, unsigned int sectors)
1993{
1994 unsigned long ap_flags = dev->link->ap->flags;
1995 struct ata_taskfile tf;
1996 unsigned int err_mask;
1997 bool dma = false;
1998
1999 DPRINTK("read log page - log 0x%x, page 0x%x\n", log, page);
2000
2001 /*
2002 * Return error without actually issuing the command on controllers
2003 * which e.g. lockup on a read log page.
2004 */
2005 if (ap_flags & ATA_FLAG_NO_LOG_PAGE)
2006 return AC_ERR_DEV;
2007
2008retry:
2009 ata_tf_init(dev, &tf);
Reimar Döffingerf971a852021-10-12 08:27:44 +02002010 if (ata_dma_enabled(dev) && ata_id_has_read_log_dma_ext(dev->id) &&
Damien Le Moal7cfdfdc2017-07-10 14:45:20 +09002011 !(dev->horkage & ATA_HORKAGE_NO_DMA_LOG)) {
Christoph Hellwigf01f62c2017-06-04 14:42:20 +02002012 tf.command = ATA_CMD_READ_LOG_DMA_EXT;
2013 tf.protocol = ATA_PROT_DMA;
2014 dma = true;
2015 } else {
2016 tf.command = ATA_CMD_READ_LOG_EXT;
2017 tf.protocol = ATA_PROT_PIO;
2018 dma = false;
2019 }
2020 tf.lbal = log;
2021 tf.lbam = page;
2022 tf.nsect = sectors;
2023 tf.hob_nsect = sectors >> 8;
2024 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_LBA48 | ATA_TFLAG_DEVICE;
2025
2026 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_FROM_DEVICE,
2027 buf, sectors * ATA_SECT_SIZE, 0);
2028
Damien Le Moalfc5c8aa2021-08-16 10:44:52 +09002029 if (err_mask) {
2030 if (dma) {
2031 dev->horkage |= ATA_HORKAGE_NO_DMA_LOG;
2032 goto retry;
2033 }
Damien Le Moal23ef63d2021-11-15 12:37:46 +09002034 ata_dev_err(dev,
2035 "Read log 0x%02x page 0x%02x failed, Emask 0x%x\n",
2036 (unsigned int)log, (unsigned int)page, err_mask);
Christoph Hellwigf01f62c2017-06-04 14:42:20 +02002037 }
2038
Christoph Hellwigf01f62c2017-06-04 14:42:20 +02002039 return err_mask;
2040}
2041
Christoph Hellwigefe205a2017-06-04 14:42:21 +02002042static bool ata_log_supported(struct ata_device *dev, u8 log)
2043{
2044 struct ata_port *ap = dev->link->ap;
2045
2046 if (ata_read_log_page(dev, ATA_LOG_DIRECTORY, 0, ap->sector_buf, 1))
2047 return false;
2048 return get_unaligned_le16(&ap->sector_buf[log * 2]) ? true : false;
2049}
2050
Christoph Hellwiga0fd2452017-06-04 14:42:23 +02002051static bool ata_identify_page_supported(struct ata_device *dev, u8 page)
2052{
2053 struct ata_port *ap = dev->link->ap;
2054 unsigned int err, i;
2055
Damien Le Moal636f6e22021-11-09 08:45:25 +09002056 if (dev->horkage & ATA_HORKAGE_NO_ID_DEV_LOG)
2057 return false;
2058
Christoph Hellwiga0fd2452017-06-04 14:42:23 +02002059 if (!ata_log_supported(dev, ATA_LOG_IDENTIFY_DEVICE)) {
Damien Le Moal636f6e22021-11-09 08:45:25 +09002060 /*
2061 * IDENTIFY DEVICE data log is defined as mandatory starting
2062 * with ACS-3 (ATA version 10). Warn about the missing log
2063 * for drives which implement this ATA level or above.
2064 */
2065 if (ata_id_major_version(dev->id) >= 10)
2066 ata_dev_warn(dev,
2067 "ATA Identify Device Log not supported\n");
2068 dev->horkage |= ATA_HORKAGE_NO_ID_DEV_LOG;
Christoph Hellwiga0fd2452017-06-04 14:42:23 +02002069 return false;
2070 }
2071
2072 /*
2073 * Read IDENTIFY DEVICE data log, page 0, to figure out if the page is
2074 * supported.
2075 */
2076 err = ata_read_log_page(dev, ATA_LOG_IDENTIFY_DEVICE, 0, ap->sector_buf,
2077 1);
Damien Le Moalfc5c8aa2021-08-16 10:44:52 +09002078 if (err)
Christoph Hellwiga0fd2452017-06-04 14:42:23 +02002079 return false;
Christoph Hellwiga0fd2452017-06-04 14:42:23 +02002080
2081 for (i = 0; i < ap->sector_buf[8]; i++) {
2082 if (ap->sector_buf[9 + i] == page)
2083 return true;
2084 }
2085
2086 return false;
2087}
2088
Tejun Heo90627122009-01-29 20:31:36 +09002089static int ata_do_link_spd_horkage(struct ata_device *dev)
2090{
2091 struct ata_link *plink = ata_dev_phys_link(dev);
2092 u32 target, target_limit;
2093
2094 if (!sata_scr_valid(plink))
2095 return 0;
2096
2097 if (dev->horkage & ATA_HORKAGE_1_5_GBPS)
2098 target = 1;
2099 else
2100 return 0;
2101
2102 target_limit = (1 << target) - 1;
2103
2104 /* if already on stricter limit, no need to push further */
2105 if (plink->sata_spd_limit <= target_limit)
2106 return 0;
2107
2108 plink->sata_spd_limit = target_limit;
2109
2110 /* Request another EH round by returning -EAGAIN if link is
2111 * going faster than the target speed. Forward progress is
2112 * guaranteed by setting sata_spd_limit to target_limit above.
2113 */
2114 if (plink->sata_spd > target) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07002115 ata_dev_info(dev, "applying link speed limit horkage to %s\n",
2116 sata_spd_string(target));
Tejun Heo90627122009-01-29 20:31:36 +09002117 return -EAGAIN;
2118 }
2119 return 0;
2120}
2121
Tejun Heo3373efd2006-05-15 20:57:53 +09002122static inline u8 ata_dev_knobble(struct ata_device *dev)
Tejun Heo4b2f3ed2006-03-01 16:09:36 +09002123{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09002124 struct ata_port *ap = dev->link->ap;
Jens Axboe9ce8e302008-08-27 15:23:18 +02002125
2126 if (ata_dev_blacklisted(dev) & ATA_HORKAGE_BRIDGE_OK)
2127 return 0;
2128
Tejun Heo9af5c9c2007-08-06 18:36:22 +09002129 return ((ap->cbl == ATA_CBL_SATA) && (!ata_id_is_sata(dev->id)));
Tejun Heo4b2f3ed2006-03-01 16:09:36 +09002130}
2131
Hannes Reinecke5a233552016-04-25 12:45:46 +02002132static void ata_dev_config_ncq_send_recv(struct ata_device *dev)
2133{
2134 struct ata_port *ap = dev->link->ap;
2135 unsigned int err_mask;
2136
Christoph Hellwigefe205a2017-06-04 14:42:21 +02002137 if (!ata_log_supported(dev, ATA_LOG_NCQ_SEND_RECV)) {
2138 ata_dev_warn(dev, "NCQ Send/Recv Log not supported\n");
Hannes Reineckefe5af0c2016-04-25 12:45:48 +02002139 return;
2140 }
Hannes Reinecke5a233552016-04-25 12:45:46 +02002141 err_mask = ata_read_log_page(dev, ATA_LOG_NCQ_SEND_RECV,
2142 0, ap->sector_buf, 1);
Damien Le Moalfc5c8aa2021-08-16 10:44:52 +09002143 if (!err_mask) {
Hannes Reinecke5a233552016-04-25 12:45:46 +02002144 u8 *cmds = dev->ncq_send_recv_cmds;
2145
2146 dev->flags |= ATA_DFLAG_NCQ_SEND_RECV;
2147 memcpy(cmds, ap->sector_buf, ATA_LOG_NCQ_SEND_RECV_SIZE);
2148
2149 if (dev->horkage & ATA_HORKAGE_NO_NCQ_TRIM) {
2150 ata_dev_dbg(dev, "disabling queued TRIM support\n");
2151 cmds[ATA_LOG_NCQ_SEND_RECV_DSM_OFFSET] &=
2152 ~ATA_LOG_NCQ_SEND_RECV_DSM_TRIM;
2153 }
2154 }
2155}
2156
Hannes Reinecke284b3b72016-04-25 12:45:54 +02002157static void ata_dev_config_ncq_non_data(struct ata_device *dev)
2158{
2159 struct ata_port *ap = dev->link->ap;
2160 unsigned int err_mask;
Hannes Reinecke284b3b72016-04-25 12:45:54 +02002161
Christoph Hellwigefe205a2017-06-04 14:42:21 +02002162 if (!ata_log_supported(dev, ATA_LOG_NCQ_NON_DATA)) {
Hannes Reinecke284b3b72016-04-25 12:45:54 +02002163 ata_dev_warn(dev,
2164 "NCQ Send/Recv Log not supported\n");
2165 return;
2166 }
2167 err_mask = ata_read_log_page(dev, ATA_LOG_NCQ_NON_DATA,
2168 0, ap->sector_buf, 1);
Damien Le Moalfc5c8aa2021-08-16 10:44:52 +09002169 if (!err_mask) {
Hannes Reinecke284b3b72016-04-25 12:45:54 +02002170 u8 *cmds = dev->ncq_non_data_cmds;
2171
2172 memcpy(cmds, ap->sector_buf, ATA_LOG_NCQ_NON_DATA_SIZE);
2173 }
2174}
2175
Adam Manzanares8e061782016-10-17 11:27:29 -07002176static void ata_dev_config_ncq_prio(struct ata_device *dev)
2177{
2178 struct ata_port *ap = dev->link->ap;
2179 unsigned int err_mask;
2180
Damien Le Moal06f6c4c2021-11-15 12:47:26 +09002181 if (!ata_identify_page_supported(dev, ATA_LOG_SATA_SETTINGS))
2182 return;
2183
Adam Manzanares8e061782016-10-17 11:27:29 -07002184 err_mask = ata_read_log_page(dev,
Christoph Hellwig1d51d5f2017-06-04 14:42:22 +02002185 ATA_LOG_IDENTIFY_DEVICE,
Adam Manzanares8e061782016-10-17 11:27:29 -07002186 ATA_LOG_SATA_SETTINGS,
2187 ap->sector_buf,
2188 1);
Damien Le Moalfc5c8aa2021-08-16 10:44:52 +09002189 if (err_mask)
Damien Le Moal2360fa12021-08-16 10:44:51 +09002190 goto not_supported;
Adam Manzanares8e061782016-10-17 11:27:29 -07002191
Damien Le Moal2360fa12021-08-16 10:44:51 +09002192 if (!(ap->sector_buf[ATA_LOG_NCQ_PRIO_OFFSET] & BIT(3)))
2193 goto not_supported;
Adam Manzanares8e061782016-10-17 11:27:29 -07002194
Damien Le Moal2360fa12021-08-16 10:44:51 +09002195 dev->flags |= ATA_DFLAG_NCQ_PRIO;
2196
2197 return;
2198
2199not_supported:
2200 dev->flags &= ~ATA_DFLAG_NCQ_PRIO_ENABLE;
2201 dev->flags &= ~ATA_DFLAG_NCQ_PRIO;
Adam Manzanares8e061782016-10-17 11:27:29 -07002202}
2203
Kate Hsuan7a8526a2021-09-03 17:44:11 +08002204static bool ata_dev_check_adapter(struct ata_device *dev,
2205 unsigned short vendor_id)
2206{
2207 struct pci_dev *pcidev = NULL;
2208 struct device *parent_dev = NULL;
2209
2210 for (parent_dev = dev->tdev.parent; parent_dev != NULL;
2211 parent_dev = parent_dev->parent) {
2212 if (dev_is_pci(parent_dev)) {
2213 pcidev = to_pci_dev(parent_dev);
2214 if (pcidev->vendor == vendor_id)
2215 return true;
2216 break;
2217 }
2218 }
2219
2220 return false;
2221}
2222
Shaohua Li388539f2009-07-27 09:24:35 +08002223static int ata_dev_config_ncq(struct ata_device *dev,
Tejun Heoa6e6ce82006-05-15 21:03:48 +09002224 char *desc, size_t desc_sz)
2225{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09002226 struct ata_port *ap = dev->link->ap;
Tejun Heoa6e6ce82006-05-15 21:03:48 +09002227 int hdepth = 0, ddepth = ata_id_queue_depth(dev->id);
Shaohua Li388539f2009-07-27 09:24:35 +08002228 unsigned int err_mask;
2229 char *aa_desc = "";
Tejun Heoa6e6ce82006-05-15 21:03:48 +09002230
2231 if (!ata_id_has_ncq(dev->id)) {
2232 desc[0] = '\0';
Shaohua Li388539f2009-07-27 09:24:35 +08002233 return 0;
Tejun Heoa6e6ce82006-05-15 21:03:48 +09002234 }
Bartlomiej Zolnierkiewiczcba97ea2020-03-26 16:58:09 +01002235 if (!IS_ENABLED(CONFIG_SATA_HOST))
2236 return 0;
Tejun Heo75683fe2007-07-05 13:31:27 +09002237 if (dev->horkage & ATA_HORKAGE_NONCQ) {
Alan Cox6919a0a2006-10-27 19:08:46 -07002238 snprintf(desc, desc_sz, "NCQ (not used)");
Shaohua Li388539f2009-07-27 09:24:35 +08002239 return 0;
Alan Cox6919a0a2006-10-27 19:08:46 -07002240 }
Kate Hsuan7a8526a2021-09-03 17:44:11 +08002241
2242 if (dev->horkage & ATA_HORKAGE_NO_NCQ_ON_ATI &&
2243 ata_dev_check_adapter(dev, PCI_VENDOR_ID_ATI)) {
2244 snprintf(desc, desc_sz, "NCQ (not used)");
2245 return 0;
2246 }
2247
Tejun Heoa6e6ce82006-05-15 21:03:48 +09002248 if (ap->flags & ATA_FLAG_NCQ) {
Jens Axboe69278f72018-05-11 12:51:10 -06002249 hdepth = min(ap->scsi_host->can_queue, ATA_MAX_QUEUE);
Tejun Heoa6e6ce82006-05-15 21:03:48 +09002250 dev->flags |= ATA_DFLAG_NCQ;
2251 }
2252
Shaohua Li388539f2009-07-27 09:24:35 +08002253 if (!(dev->horkage & ATA_HORKAGE_BROKEN_FPDMA_AA) &&
2254 (ap->flags & ATA_FLAG_FPDMA_AA) &&
2255 ata_id_has_fpdma_aa(dev->id)) {
2256 err_mask = ata_dev_set_feature(dev, SETFEATURES_SATA_ENABLE,
2257 SATA_FPDMA_AA);
2258 if (err_mask) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07002259 ata_dev_err(dev,
2260 "failed to enable AA (error_mask=0x%x)\n",
2261 err_mask);
Shaohua Li388539f2009-07-27 09:24:35 +08002262 if (err_mask != AC_ERR_DEV) {
2263 dev->horkage |= ATA_HORKAGE_BROKEN_FPDMA_AA;
2264 return -EIO;
2265 }
2266 } else
2267 aa_desc = ", AA";
2268 }
2269
Tejun Heoa6e6ce82006-05-15 21:03:48 +09002270 if (hdepth >= ddepth)
Shaohua Li388539f2009-07-27 09:24:35 +08002271 snprintf(desc, desc_sz, "NCQ (depth %d)%s", ddepth, aa_desc);
Tejun Heoa6e6ce82006-05-15 21:03:48 +09002272 else
Shaohua Li388539f2009-07-27 09:24:35 +08002273 snprintf(desc, desc_sz, "NCQ (depth %d/%d)%s", hdepth,
2274 ddepth, aa_desc);
Marc Carinoed369112013-08-24 23:22:50 -07002275
Hannes Reinecke284b3b72016-04-25 12:45:54 +02002276 if ((ap->flags & ATA_FLAG_FPDMA_AUX)) {
2277 if (ata_id_has_ncq_send_and_recv(dev->id))
2278 ata_dev_config_ncq_send_recv(dev);
2279 if (ata_id_has_ncq_non_data(dev->id))
2280 ata_dev_config_ncq_non_data(dev);
Adam Manzanares8e061782016-10-17 11:27:29 -07002281 if (ata_id_has_ncq_prio(dev->id))
2282 ata_dev_config_ncq_prio(dev);
Marc Carinoed369112013-08-24 23:22:50 -07002283 }
2284
Shaohua Li388539f2009-07-27 09:24:35 +08002285 return 0;
Tejun Heoa6e6ce82006-05-15 21:03:48 +09002286}
2287
Hannes Reineckee87fd282016-04-04 11:43:55 +02002288static void ata_dev_config_sense_reporting(struct ata_device *dev)
2289{
2290 unsigned int err_mask;
2291
2292 if (!ata_id_has_sense_reporting(dev->id))
2293 return;
2294
2295 if (ata_id_sense_reporting_enabled(dev->id))
2296 return;
2297
2298 err_mask = ata_dev_set_feature(dev, SETFEATURE_SENSE_DATA, 0x1);
2299 if (err_mask) {
2300 ata_dev_dbg(dev,
2301 "failed to enable Sense Data Reporting, Emask 0x%x\n",
2302 err_mask);
2303 }
2304}
2305
Hannes Reinecke6d1003a2016-04-25 12:45:56 +02002306static void ata_dev_config_zac(struct ata_device *dev)
2307{
2308 struct ata_port *ap = dev->link->ap;
2309 unsigned int err_mask;
2310 u8 *identify_buf = ap->sector_buf;
Hannes Reinecke6d1003a2016-04-25 12:45:56 +02002311
2312 dev->zac_zones_optimal_open = U32_MAX;
2313 dev->zac_zones_optimal_nonseq = U32_MAX;
2314 dev->zac_zones_max_open = U32_MAX;
2315
2316 /*
2317 * Always set the 'ZAC' flag for Host-managed devices.
2318 */
2319 if (dev->class == ATA_DEV_ZAC)
2320 dev->flags |= ATA_DFLAG_ZAC;
2321 else if (ata_id_zoned_cap(dev->id) == 0x01)
2322 /*
2323 * Check for host-aware devices.
2324 */
2325 dev->flags |= ATA_DFLAG_ZAC;
2326
2327 if (!(dev->flags & ATA_DFLAG_ZAC))
2328 return;
2329
Christoph Hellwiga0fd2452017-06-04 14:42:23 +02002330 if (!ata_identify_page_supported(dev, ATA_LOG_ZONED_INFORMATION)) {
Hannes Reinecke6d1003a2016-04-25 12:45:56 +02002331 ata_dev_warn(dev,
2332 "ATA Zoned Information Log not supported\n");
2333 return;
2334 }
2335
2336 /*
2337 * Read IDENTIFY DEVICE data log, page 9 (Zoned-device information)
2338 */
Christoph Hellwig1d51d5f2017-06-04 14:42:22 +02002339 err_mask = ata_read_log_page(dev, ATA_LOG_IDENTIFY_DEVICE,
Hannes Reinecke6d1003a2016-04-25 12:45:56 +02002340 ATA_LOG_ZONED_INFORMATION,
2341 identify_buf, 1);
2342 if (!err_mask) {
2343 u64 zoned_cap, opt_open, opt_nonseq, max_open;
2344
2345 zoned_cap = get_unaligned_le64(&identify_buf[8]);
2346 if ((zoned_cap >> 63))
2347 dev->zac_zoned_cap = (zoned_cap & 1);
2348 opt_open = get_unaligned_le64(&identify_buf[24]);
2349 if ((opt_open >> 63))
2350 dev->zac_zones_optimal_open = (u32)opt_open;
2351 opt_nonseq = get_unaligned_le64(&identify_buf[32]);
2352 if ((opt_nonseq >> 63))
2353 dev->zac_zones_optimal_nonseq = (u32)opt_nonseq;
2354 max_open = get_unaligned_le64(&identify_buf[40]);
2355 if ((max_open >> 63))
2356 dev->zac_zones_max_open = (u32)max_open;
2357 }
2358}
2359
Christoph Hellwig818831c2017-06-04 14:42:24 +02002360static void ata_dev_config_trusted(struct ata_device *dev)
2361{
2362 struct ata_port *ap = dev->link->ap;
2363 u64 trusted_cap;
2364 unsigned int err;
2365
Christoph Hellwige8f11db2017-08-29 14:42:06 +02002366 if (!ata_id_has_trusted(dev->id))
2367 return;
2368
Christoph Hellwig818831c2017-06-04 14:42:24 +02002369 if (!ata_identify_page_supported(dev, ATA_LOG_SECURITY)) {
2370 ata_dev_warn(dev,
2371 "Security Log not supported\n");
2372 return;
2373 }
2374
2375 err = ata_read_log_page(dev, ATA_LOG_IDENTIFY_DEVICE, ATA_LOG_SECURITY,
2376 ap->sector_buf, 1);
Damien Le Moalfc5c8aa2021-08-16 10:44:52 +09002377 if (err)
Christoph Hellwig818831c2017-06-04 14:42:24 +02002378 return;
Christoph Hellwig818831c2017-06-04 14:42:24 +02002379
2380 trusted_cap = get_unaligned_le64(&ap->sector_buf[40]);
2381 if (!(trusted_cap & (1ULL << 63))) {
2382 ata_dev_dbg(dev,
2383 "Trusted Computing capability qword not valid!\n");
2384 return;
2385 }
2386
2387 if (trusted_cap & (1 << 0))
2388 dev->flags |= ATA_DFLAG_TRUSTED;
2389}
2390
Damien Le Moal891fd7c2021-08-16 10:44:50 +09002391static int ata_dev_config_lba(struct ata_device *dev)
2392{
2393 struct ata_port *ap = dev->link->ap;
2394 const u16 *id = dev->id;
2395 const char *lba_desc;
2396 char ncq_desc[24];
2397 int ret;
2398
2399 dev->flags |= ATA_DFLAG_LBA;
2400
2401 if (ata_id_has_lba48(id)) {
2402 lba_desc = "LBA48";
2403 dev->flags |= ATA_DFLAG_LBA48;
2404 if (dev->n_sectors >= (1UL << 28) &&
2405 ata_id_has_flush_ext(id))
2406 dev->flags |= ATA_DFLAG_FLUSH_EXT;
2407 } else {
2408 lba_desc = "LBA";
2409 }
2410
2411 /* config NCQ */
2412 ret = ata_dev_config_ncq(dev, ncq_desc, sizeof(ncq_desc));
2413
2414 /* print device info to dmesg */
2415 if (ata_msg_drv(ap) && ata_dev_print_info(dev))
2416 ata_dev_info(dev,
2417 "%llu sectors, multi %u: %s %s\n",
2418 (unsigned long long)dev->n_sectors,
2419 dev->multi_count, lba_desc, ncq_desc);
2420
2421 return ret;
2422}
2423
2424static void ata_dev_config_chs(struct ata_device *dev)
2425{
2426 struct ata_port *ap = dev->link->ap;
2427 const u16 *id = dev->id;
2428
2429 if (ata_id_current_chs_valid(id)) {
2430 /* Current CHS translation is valid. */
2431 dev->cylinders = id[54];
2432 dev->heads = id[55];
2433 dev->sectors = id[56];
2434 } else {
2435 /* Default translation */
2436 dev->cylinders = id[1];
2437 dev->heads = id[3];
2438 dev->sectors = id[6];
2439 }
2440
2441 /* print device info to dmesg */
2442 if (ata_msg_drv(ap) && ata_dev_print_info(dev))
2443 ata_dev_info(dev,
2444 "%llu sectors, multi %u, CHS %u/%u/%u\n",
2445 (unsigned long long)dev->n_sectors,
2446 dev->multi_count, dev->cylinders,
2447 dev->heads, dev->sectors);
2448}
2449
Damien Le Moald8d87782021-08-16 10:44:49 +09002450static void ata_dev_config_devslp(struct ata_device *dev)
2451{
2452 u8 *sata_setting = dev->link->ap->sector_buf;
2453 unsigned int err_mask;
2454 int i, j;
2455
2456 /*
2457 * Check device sleep capability. Get DevSlp timing variables
2458 * from SATA Settings page of Identify Device Data Log.
2459 */
Damien Le Moal06f6c4c2021-11-15 12:47:26 +09002460 if (!ata_id_has_devslp(dev->id) ||
2461 !ata_identify_page_supported(dev, ATA_LOG_SATA_SETTINGS))
Damien Le Moald8d87782021-08-16 10:44:49 +09002462 return;
2463
2464 err_mask = ata_read_log_page(dev,
2465 ATA_LOG_IDENTIFY_DEVICE,
2466 ATA_LOG_SATA_SETTINGS,
2467 sata_setting, 1);
Damien Le Moalfc5c8aa2021-08-16 10:44:52 +09002468 if (err_mask)
Damien Le Moald8d87782021-08-16 10:44:49 +09002469 return;
Damien Le Moald8d87782021-08-16 10:44:49 +09002470
2471 dev->flags |= ATA_DFLAG_DEVSLP;
2472 for (i = 0; i < ATA_LOG_DEVSLP_SIZE; i++) {
2473 j = ATA_LOG_DEVSLP_OFFSET + i;
2474 dev->devslp_timing[i] = sata_setting[j];
2475 }
2476}
2477
Damien Le Moalfe22e1c2021-10-27 11:22:21 +09002478static void ata_dev_config_cpr(struct ata_device *dev)
2479{
2480 unsigned int err_mask;
2481 size_t buf_len;
2482 int i, nr_cpr = 0;
2483 struct ata_cpr_log *cpr_log = NULL;
2484 u8 *desc, *buf = NULL;
2485
2486 if (!ata_identify_page_supported(dev,
2487 ATA_LOG_CONCURRENT_POSITIONING_RANGES))
2488 goto out;
2489
2490 /*
2491 * Read IDENTIFY DEVICE data log, page 0x47
2492 * (concurrent positioning ranges). We can have at most 255 32B range
2493 * descriptors plus a 64B header.
2494 */
2495 buf_len = (64 + 255 * 32 + 511) & ~511;
2496 buf = kzalloc(buf_len, GFP_KERNEL);
2497 if (!buf)
2498 goto out;
2499
2500 err_mask = ata_read_log_page(dev, ATA_LOG_IDENTIFY_DEVICE,
2501 ATA_LOG_CONCURRENT_POSITIONING_RANGES,
2502 buf, buf_len >> 9);
2503 if (err_mask)
2504 goto out;
2505
2506 nr_cpr = buf[0];
2507 if (!nr_cpr)
2508 goto out;
2509
2510 cpr_log = kzalloc(struct_size(cpr_log, cpr, nr_cpr), GFP_KERNEL);
2511 if (!cpr_log)
2512 goto out;
2513
2514 cpr_log->nr_cpr = nr_cpr;
2515 desc = &buf[64];
2516 for (i = 0; i < nr_cpr; i++, desc += 32) {
2517 cpr_log->cpr[i].num = desc[0];
2518 cpr_log->cpr[i].num_storage_elements = desc[1];
2519 cpr_log->cpr[i].start_lba = get_unaligned_le64(&desc[8]);
2520 cpr_log->cpr[i].num_lbas = get_unaligned_le64(&desc[16]);
2521 }
2522
2523out:
2524 swap(dev->cpr_log, cpr_log);
2525 kfree(cpr_log);
2526 kfree(buf);
2527}
2528
Damien Le Moald633b8a2021-08-16 10:44:53 +09002529static void ata_dev_print_features(struct ata_device *dev)
2530{
2531 if (!(dev->flags & ATA_DFLAG_FEATURES_MASK))
2532 return;
2533
2534 ata_dev_info(dev,
Damien Le Moalfe22e1c2021-10-27 11:22:21 +09002535 "Features:%s%s%s%s%s%s\n",
Damien Le Moald633b8a2021-08-16 10:44:53 +09002536 dev->flags & ATA_DFLAG_TRUSTED ? " Trust" : "",
2537 dev->flags & ATA_DFLAG_DA ? " Dev-Attention" : "",
2538 dev->flags & ATA_DFLAG_DEVSLP ? " Dev-Sleep" : "",
2539 dev->flags & ATA_DFLAG_NCQ_SEND_RECV ? " NCQ-sndrcv" : "",
Damien Le Moalfe22e1c2021-10-27 11:22:21 +09002540 dev->flags & ATA_DFLAG_NCQ_PRIO ? " NCQ-prio" : "",
2541 dev->cpr_log ? " CPR" : "");
Damien Le Moald633b8a2021-08-16 10:44:53 +09002542}
2543
Tejun Heo49016ac2006-02-21 02:12:11 +09002544/**
Tejun Heoffeae412006-03-01 16:09:35 +09002545 * ata_dev_configure - Configure the specified ATA/ATAPI device
Tejun Heoffeae412006-03-01 16:09:35 +09002546 * @dev: Target device to configure
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547 *
Tejun Heoffeae412006-03-01 16:09:35 +09002548 * Configure @dev according to @dev->id. Generic and low-level
2549 * driver specific fixups are also applied.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550 *
2551 * LOCKING:
Tejun Heoffeae412006-03-01 16:09:35 +09002552 * Kernel thread context (may sleep)
2553 *
2554 * RETURNS:
2555 * 0 on success, -errno otherwise
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556 */
Tejun Heoefdaedc2006-11-01 18:38:52 +09002557int ata_dev_configure(struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09002559 struct ata_port *ap = dev->link->ap;
Damien Le Moal891fd7c2021-08-16 10:44:50 +09002560 bool print_info = ata_dev_print_info(dev);
Tejun Heo1148c3a2006-03-13 19:48:04 +09002561 const u16 *id = dev->id;
Tejun Heo7dc951a2007-11-27 19:43:42 +09002562 unsigned long xfer_mask;
Shane Huang65fe1f02012-09-07 22:40:01 +08002563 unsigned int err_mask;
Alan Coxb352e572006-08-10 18:52:12 +01002564 char revbuf[7]; /* XYZ-99\0 */
Eric D. Mudama3f64f562007-01-30 23:00:40 -07002565 char fwrevbuf[ATA_ID_FW_REV_LEN+1];
2566 char modelbuf[ATA_ID_PROD_LEN+1];
Brian Kinge6d902a2006-06-28 08:30:31 -05002567 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568
Borislav Petkov0dd4b212006-06-23 02:29:08 -04002569 if (!ata_dev_enabled(dev) && ata_msg_info(ap)) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07002570 ata_dev_info(dev, "%s: ENTER/EXIT -- nodev\n", __func__);
Tejun Heoffeae412006-03-01 16:09:35 +09002571 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572 }
2573
Borislav Petkov0dd4b212006-06-23 02:29:08 -04002574 if (ata_msg_probe(ap))
Joe Perchesa9a79df2011-04-15 15:51:59 -07002575 ata_dev_dbg(dev, "%s: ENTER\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576
Tejun Heo75683fe2007-07-05 13:31:27 +09002577 /* set horkage */
2578 dev->horkage |= ata_dev_blacklisted(dev);
Tejun Heo33267322008-02-13 09:15:09 +09002579 ata_force_horkage(dev);
Tejun Heo75683fe2007-07-05 13:31:27 +09002580
Tejun Heo50af2fa2008-05-19 01:15:14 +09002581 if (dev->horkage & ATA_HORKAGE_DISABLE) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07002582 ata_dev_info(dev, "unsupported device, disabling\n");
Tejun Heo50af2fa2008-05-19 01:15:14 +09002583 ata_dev_disable(dev);
2584 return 0;
2585 }
2586
Tejun Heo2486fa52008-07-31 07:52:40 +09002587 if ((!atapi_enabled || (ap->flags & ATA_FLAG_NO_ATAPI)) &&
2588 dev->class == ATA_DEV_ATAPI) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07002589 ata_dev_warn(dev, "WARNING: ATAPI is %s, device ignored\n",
2590 atapi_enabled ? "not supported with this driver"
2591 : "disabled");
Tejun Heo2486fa52008-07-31 07:52:40 +09002592 ata_dev_disable(dev);
2593 return 0;
2594 }
2595
Tejun Heo90627122009-01-29 20:31:36 +09002596 rc = ata_do_link_spd_horkage(dev);
2597 if (rc)
2598 return rc;
2599
Tejun Heoecd75ad2014-01-16 09:47:17 -05002600 /* some WD SATA-1 drives have issues with LPM, turn on NOLPM for them */
2601 if ((dev->horkage & ATA_HORKAGE_WD_BROKEN_LPM) &&
2602 (id[ATA_ID_SATA_CAPABILITY] & 0xe) == 0x2)
2603 dev->horkage |= ATA_HORKAGE_NOLPM;
2604
Hans de Goede240630e2018-07-01 12:15:46 +02002605 if (ap->flags & ATA_FLAG_NO_LPM)
2606 dev->horkage |= ATA_HORKAGE_NOLPM;
2607
Tejun Heoecd75ad2014-01-16 09:47:17 -05002608 if (dev->horkage & ATA_HORKAGE_NOLPM) {
2609 ata_dev_warn(dev, "LPM support broken, forcing max_power\n");
2610 dev->link->ap->target_lpm_policy = ATA_LPM_MAX_POWER;
2611 }
2612
Tejun Heo67465442007-05-15 03:28:16 +09002613 /* let ACPI work its magic */
2614 rc = ata_acpi_on_devcfg(dev);
2615 if (rc)
2616 return rc;
Kristen Carlson Accardi08573a82006-11-10 16:14:47 -08002617
Tejun Heo05027ad2007-09-03 12:32:57 +09002618 /* massage HPA, do it early as it might change IDENTIFY data */
2619 rc = ata_hpa_resize(dev);
2620 if (rc)
2621 return rc;
2622
Tejun Heoc39f5eb2006-03-13 19:51:19 +09002623 /* print device capabilities */
Borislav Petkov0dd4b212006-06-23 02:29:08 -04002624 if (ata_msg_probe(ap))
Joe Perchesa9a79df2011-04-15 15:51:59 -07002625 ata_dev_dbg(dev,
2626 "%s: cfg 49:%04x 82:%04x 83:%04x 84:%04x "
2627 "85:%04x 86:%04x 87:%04x 88:%04x\n",
2628 __func__,
2629 id[49], id[82], id[83], id[84],
2630 id[85], id[86], id[87], id[88]);
Tejun Heoc39f5eb2006-03-13 19:51:19 +09002631
Tejun Heo208a9932006-03-05 17:55:58 +09002632 /* initialize to-be-configured parameters */
Tejun Heoea1dd4e2006-04-02 18:51:53 +09002633 dev->flags &= ~ATA_DFLAG_CFG_MASK;
Tejun Heo208a9932006-03-05 17:55:58 +09002634 dev->max_sectors = 0;
2635 dev->cdb_len = 0;
2636 dev->n_sectors = 0;
2637 dev->cylinders = 0;
2638 dev->heads = 0;
2639 dev->sectors = 0;
Mark Lorde18086d2009-03-19 13:32:21 -04002640 dev->multi_count = 0;
Tejun Heo208a9932006-03-05 17:55:58 +09002641
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642 /*
2643 * common ATA, ATAPI feature tests
2644 */
2645
Tejun Heoff8854b2006-03-06 04:31:56 +09002646 /* find max transfer mode; for printk only */
Tejun Heo1148c3a2006-03-13 19:48:04 +09002647 xfer_mask = ata_id_xfermask(id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648
Borislav Petkov0dd4b212006-06-23 02:29:08 -04002649 if (ata_msg_probe(ap))
2650 ata_dump_id(id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651
Albert Leeef143d52007-06-05 13:01:33 +08002652 /* SCSI only uses 4-char revisions, dump full 8 chars from ATA */
2653 ata_id_c_string(dev->id, fwrevbuf, ATA_ID_FW_REV,
2654 sizeof(fwrevbuf));
2655
2656 ata_id_c_string(dev->id, modelbuf, ATA_ID_PROD,
2657 sizeof(modelbuf));
2658
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659 /* ATA-specific feature tests */
Hannes Reinecke9162c652014-11-05 13:08:21 +01002660 if (dev->class == ATA_DEV_ATA || dev->class == ATA_DEV_ZAC) {
Alan Coxb352e572006-08-10 18:52:12 +01002661 if (ata_id_is_cfa(id)) {
Sergei Shtylyov62afe5d2009-04-13 20:50:00 +04002662 /* CPRM may make this media unusable */
2663 if (id[ATA_ID_CFA_KEY_MGMT] & 1)
Joe Perchesa9a79df2011-04-15 15:51:59 -07002664 ata_dev_warn(dev,
2665 "supports DRM functions and may not be fully accessible\n");
Alan Coxb352e572006-08-10 18:52:12 +01002666 snprintf(revbuf, 7, "CFA");
Alan Coxae8d4ee2007-11-04 22:05:49 -05002667 } else {
Jeff Garzik2dcb4072007-10-19 06:42:56 -04002668 snprintf(revbuf, 7, "ATA-%d", ata_id_major_version(id));
Alan Coxae8d4ee2007-11-04 22:05:49 -05002669 /* Warn the user if the device has TPM extensions */
2670 if (ata_id_has_tpm(id))
Joe Perchesa9a79df2011-04-15 15:51:59 -07002671 ata_dev_warn(dev,
2672 "supports DRM functions and may not be fully accessible\n");
Alan Coxae8d4ee2007-11-04 22:05:49 -05002673 }
Alan Coxb352e572006-08-10 18:52:12 +01002674
Tejun Heo1148c3a2006-03-13 19:48:04 +09002675 dev->n_sectors = ata_id_n_sectors(id);
Tejun Heo29407402006-02-12 22:47:04 +09002676
Mark Lorde18086d2009-03-19 13:32:21 -04002677 /* get current R/W Multiple count setting */
2678 if ((dev->id[47] >> 8) == 0x80 && (dev->id[59] & 0x100)) {
2679 unsigned int max = dev->id[47] & 0xff;
2680 unsigned int cnt = dev->id[59] & 0xff;
2681 /* only recognize/allow powers of two here */
2682 if (is_power_of_2(max) && is_power_of_2(cnt))
2683 if (cnt <= max)
2684 dev->multi_count = cnt;
2685 }
Eric D. Mudama3f64f562007-01-30 23:00:40 -07002686
Damien Le Moal891fd7c2021-08-16 10:44:50 +09002687 /* print device info to dmesg */
2688 if (ata_msg_drv(ap) && print_info)
2689 ata_dev_info(dev, "%s: %s, %s, max %s\n",
2690 revbuf, modelbuf, fwrevbuf,
2691 ata_mode_string(xfer_mask));
2692
Tejun Heo1148c3a2006-03-13 19:48:04 +09002693 if (ata_id_has_lba(id)) {
Damien Le Moal891fd7c2021-08-16 10:44:50 +09002694 rc = ata_dev_config_lba(dev);
Shaohua Li388539f2009-07-27 09:24:35 +08002695 if (rc)
2696 return rc;
Tejun Heoffeae412006-03-01 16:09:35 +09002697 } else {
Damien Le Moal891fd7c2021-08-16 10:44:50 +09002698 ata_dev_config_chs(dev);
Albert Lee07f6f7d2005-11-01 19:33:20 +08002699 }
2700
Damien Le Moald8d87782021-08-16 10:44:49 +09002701 ata_dev_config_devslp(dev);
Hannes Reineckee87fd282016-04-04 11:43:55 +02002702 ata_dev_config_sense_reporting(dev);
Hannes Reinecke6d1003a2016-04-25 12:45:56 +02002703 ata_dev_config_zac(dev);
Christoph Hellwig818831c2017-06-04 14:42:24 +02002704 ata_dev_config_trusted(dev);
Damien Le Moalfe22e1c2021-10-27 11:22:21 +09002705 ata_dev_config_cpr(dev);
Minwoo Imb1ffbf82017-06-24 03:41:10 +09002706 dev->cdb_len = 32;
Damien Le Moald633b8a2021-08-16 10:44:53 +09002707
2708 if (ata_msg_drv(ap) && print_info)
2709 ata_dev_print_features(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710 }
2711
2712 /* ATAPI-specific feature tests */
Jeff Garzik2c13b7c2005-11-14 14:14:16 -05002713 else if (dev->class == ATA_DEV_ATAPI) {
Tejun Heo854c73a2007-09-23 13:14:11 +09002714 const char *cdb_intr_string = "";
2715 const char *atapi_an_string = "";
Tejun Heo91163002008-02-21 13:25:50 +09002716 const char *dma_dir_string = "";
Tejun Heo7d77b242007-09-23 13:14:13 +09002717 u32 sntf;
Albert Lee08a556d2006-03-31 13:29:04 +08002718
Tejun Heo1148c3a2006-03-13 19:48:04 +09002719 rc = atapi_cdb_len(id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720 if ((rc < 12) || (rc > ATAPI_CDB_LEN)) {
Borislav Petkov0dd4b212006-06-23 02:29:08 -04002721 if (ata_msg_warn(ap))
Joe Perchesa9a79df2011-04-15 15:51:59 -07002722 ata_dev_warn(dev, "unsupported CDB len\n");
Tejun Heoffeae412006-03-01 16:09:35 +09002723 rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724 goto err_out_nosup;
2725 }
Tejun Heo6e7846e2006-02-12 23:32:58 +09002726 dev->cdb_len = (unsigned int) rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727
Tejun Heo7d77b242007-09-23 13:14:13 +09002728 /* Enable ATAPI AN if both the host and device have
2729 * the support. If PMP is attached, SNTF is required
2730 * to enable ATAPI AN to discern between PHY status
2731 * changed notifications and ATAPI ANs.
Kristen Carlson Accardi9f45cbd2007-08-15 03:57:11 -04002732 */
Tejun Heoe7ecd432010-05-19 15:38:58 +02002733 if (atapi_an &&
2734 (ap->flags & ATA_FLAG_AN) && ata_id_has_atapi_AN(id) &&
Tejun Heo071f44b2008-04-07 22:47:22 +09002735 (!sata_pmp_attached(ap) ||
Tejun Heo7d77b242007-09-23 13:14:13 +09002736 sata_scr_read(&ap->link, SCR_NOTIFICATION, &sntf) == 0)) {
Kristen Carlson Accardi9f45cbd2007-08-15 03:57:11 -04002737 /* issue SET feature command to turn this on */
Jeff Garzik218f3d32007-10-25 00:33:27 -04002738 err_mask = ata_dev_set_feature(dev,
2739 SETFEATURES_SATA_ENABLE, SATA_AN);
Tejun Heo854c73a2007-09-23 13:14:11 +09002740 if (err_mask)
Joe Perchesa9a79df2011-04-15 15:51:59 -07002741 ata_dev_err(dev,
2742 "failed to enable ATAPI AN (err_mask=0x%x)\n",
2743 err_mask);
Tejun Heo854c73a2007-09-23 13:14:11 +09002744 else {
Kristen Carlson Accardi9f45cbd2007-08-15 03:57:11 -04002745 dev->flags |= ATA_DFLAG_AN;
Tejun Heo854c73a2007-09-23 13:14:11 +09002746 atapi_an_string = ", ATAPI AN";
2747 }
Kristen Carlson Accardi9f45cbd2007-08-15 03:57:11 -04002748 }
2749
Albert Lee08a556d2006-03-31 13:29:04 +08002750 if (ata_id_cdb_intr(dev->id)) {
Albert Lee312f7da2005-09-27 17:38:03 +08002751 dev->flags |= ATA_DFLAG_CDB_INTR;
Albert Lee08a556d2006-03-31 13:29:04 +08002752 cdb_intr_string = ", CDB intr";
2753 }
Albert Lee312f7da2005-09-27 17:38:03 +08002754
Vincent Pelletier966fbe12013-05-21 22:30:58 +02002755 if (atapi_dmadir || (dev->horkage & ATA_HORKAGE_ATAPI_DMADIR) || atapi_id_dmadir(dev->id)) {
Tejun Heo91163002008-02-21 13:25:50 +09002756 dev->flags |= ATA_DFLAG_DMADIR;
2757 dma_dir_string = ", DMADIR";
2758 }
2759
Aaron Luafe75952013-01-15 17:20:58 +08002760 if (ata_id_has_da(dev->id)) {
Lin Mingb1354cb2012-06-25 16:13:08 +08002761 dev->flags |= ATA_DFLAG_DA;
Aaron Luafe75952013-01-15 17:20:58 +08002762 zpodd_init(dev);
2763 }
Lin Mingb1354cb2012-06-25 16:13:08 +08002764
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765 /* print device info to dmesg */
Borislav Petkov5afc8142006-06-27 14:51:25 +02002766 if (ata_msg_drv(ap) && print_info)
Joe Perchesa9a79df2011-04-15 15:51:59 -07002767 ata_dev_info(dev,
2768 "ATAPI: %s, %s, max %s%s%s%s\n",
2769 modelbuf, fwrevbuf,
2770 ata_mode_string(xfer_mask),
2771 cdb_intr_string, atapi_an_string,
2772 dma_dir_string);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773 }
2774
Tejun Heo914ed352006-11-01 18:39:55 +09002775 /* determine max_sectors */
2776 dev->max_sectors = ATA_MAX_SECTORS;
2777 if (dev->flags & ATA_DFLAG_LBA48)
2778 dev->max_sectors = ATA_MAX_SECTORS_LBA48;
2779
Alan Coxc5038fc2007-10-25 14:21:16 +01002780 /* Limit PATA drive on SATA cable bridge transfers to udma5,
2781 200 sectors */
Tejun Heo3373efd2006-05-15 20:57:53 +09002782 if (ata_dev_knobble(dev)) {
Borislav Petkov5afc8142006-06-27 14:51:25 +02002783 if (ata_msg_drv(ap) && print_info)
Joe Perchesa9a79df2011-04-15 15:51:59 -07002784 ata_dev_info(dev, "applying bridge limits\n");
Tejun Heo5a529132006-03-24 14:07:50 +09002785 dev->udma_mask &= ATA_UDMA5;
Tejun Heo4b2f3ed2006-03-01 16:09:36 +09002786 dev->max_sectors = ATA_MAX_SECTORS;
2787 }
2788
Tony Battersbyf8d8e572007-10-30 11:44:35 -04002789 if ((dev->class == ATA_DEV_ATAPI) &&
Albert Leef442cd82007-11-15 10:35:47 +09002790 (atapi_command_packet_set(id) == TYPE_TAPE)) {
Tony Battersbyf8d8e572007-10-30 11:44:35 -04002791 dev->max_sectors = ATA_MAX_SECTORS_TAPE;
Albert Leef442cd82007-11-15 10:35:47 +09002792 dev->horkage |= ATA_HORKAGE_STUCK_ERR;
2793 }
Tony Battersbyf8d8e572007-10-30 11:44:35 -04002794
Tejun Heo75683fe2007-07-05 13:31:27 +09002795 if (dev->horkage & ATA_HORKAGE_MAX_SEC_128)
Tejun Heo03ec52d2007-04-12 13:38:11 +09002796 dev->max_sectors = min_t(unsigned int, ATA_MAX_SECTORS_128,
2797 dev->max_sectors);
Albert Lee18d6e9d2007-04-02 11:34:15 +08002798
David Milburnaf34d632015-07-13 11:48:23 -05002799 if (dev->horkage & ATA_HORKAGE_MAX_SEC_1024)
2800 dev->max_sectors = min_t(unsigned int, ATA_MAX_SECTORS_1024,
2801 dev->max_sectors);
2802
Shan Haia32450e2013-03-18 10:30:44 +08002803 if (dev->horkage & ATA_HORKAGE_MAX_SEC_LBA48)
2804 dev->max_sectors = ATA_MAX_SECTORS_LBA48;
2805
Tejun Heo4b2f3ed2006-03-01 16:09:36 +09002806 if (ap->ops->dev_config)
Alancd0d3bb2007-03-02 00:56:15 +00002807 ap->ops->dev_config(dev);
Tejun Heo4b2f3ed2006-03-01 16:09:36 +09002808
Alan Coxc5038fc2007-10-25 14:21:16 +01002809 if (dev->horkage & ATA_HORKAGE_DIAGNOSTIC) {
2810 /* Let the user know. We don't want to disallow opens for
2811 rescue purposes, or in case the vendor is just a blithering
2812 idiot. Do this after the dev_config call as some controllers
2813 with buggy firmware may want to avoid reporting false device
2814 bugs */
2815
2816 if (print_info) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07002817 ata_dev_warn(dev,
Alan Coxc5038fc2007-10-25 14:21:16 +01002818"Drive reports diagnostics failure. This may indicate a drive\n");
Joe Perchesa9a79df2011-04-15 15:51:59 -07002819 ata_dev_warn(dev,
Alan Coxc5038fc2007-10-25 14:21:16 +01002820"fault or invalid emulation. Contact drive vendor for information.\n");
2821 }
2822 }
2823
Tejun Heoac70a962008-11-27 13:36:48 +09002824 if ((dev->horkage & ATA_HORKAGE_FIRMWARE_WARN) && print_info) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07002825 ata_dev_warn(dev, "WARNING: device requires firmware update to be fully functional\n");
2826 ata_dev_warn(dev, " contact the vendor or visit http://ata.wiki.kernel.org\n");
Tejun Heoac70a962008-11-27 13:36:48 +09002827 }
2828
Tejun Heoffeae412006-03-01 16:09:35 +09002829 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830
2831err_out_nosup:
Borislav Petkov0dd4b212006-06-23 02:29:08 -04002832 if (ata_msg_probe(ap))
Joe Perchesa9a79df2011-04-15 15:51:59 -07002833 ata_dev_dbg(dev, "%s: EXIT, err\n", __func__);
Tejun Heoffeae412006-03-01 16:09:35 +09002834 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835}
2836
2837/**
Alan Cox2e41e8e2007-03-08 23:19:19 +00002838 * ata_cable_40wire - return 40 wire cable type
Alan Coxbe0d18d2007-03-06 02:37:56 -08002839 * @ap: port
2840 *
Alan Cox2e41e8e2007-03-08 23:19:19 +00002841 * Helper method for drivers which want to hardwire 40 wire cable
Alan Coxbe0d18d2007-03-06 02:37:56 -08002842 * detection.
2843 */
2844
2845int ata_cable_40wire(struct ata_port *ap)
2846{
2847 return ATA_CBL_PATA40;
2848}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01002849EXPORT_SYMBOL_GPL(ata_cable_40wire);
Alan Coxbe0d18d2007-03-06 02:37:56 -08002850
2851/**
Alan Cox2e41e8e2007-03-08 23:19:19 +00002852 * ata_cable_80wire - return 80 wire cable type
Alan Coxbe0d18d2007-03-06 02:37:56 -08002853 * @ap: port
2854 *
Alan Cox2e41e8e2007-03-08 23:19:19 +00002855 * Helper method for drivers which want to hardwire 80 wire cable
Alan Coxbe0d18d2007-03-06 02:37:56 -08002856 * detection.
2857 */
2858
2859int ata_cable_80wire(struct ata_port *ap)
2860{
2861 return ATA_CBL_PATA80;
2862}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01002863EXPORT_SYMBOL_GPL(ata_cable_80wire);
Alan Coxbe0d18d2007-03-06 02:37:56 -08002864
2865/**
2866 * ata_cable_unknown - return unknown PATA cable.
2867 * @ap: port
2868 *
2869 * Helper method for drivers which have no PATA cable detection.
2870 */
2871
2872int ata_cable_unknown(struct ata_port *ap)
2873{
2874 return ATA_CBL_PATA_UNK;
2875}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01002876EXPORT_SYMBOL_GPL(ata_cable_unknown);
Alan Coxbe0d18d2007-03-06 02:37:56 -08002877
2878/**
Tejun Heoc88f90c2007-11-27 19:43:48 +09002879 * ata_cable_ignore - return ignored PATA cable.
2880 * @ap: port
2881 *
2882 * Helper method for drivers which don't use cable type to limit
2883 * transfer mode.
2884 */
2885int ata_cable_ignore(struct ata_port *ap)
2886{
2887 return ATA_CBL_PATA_IGN;
2888}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01002889EXPORT_SYMBOL_GPL(ata_cable_ignore);
Tejun Heoc88f90c2007-11-27 19:43:48 +09002890
2891/**
Alan Coxbe0d18d2007-03-06 02:37:56 -08002892 * ata_cable_sata - return SATA cable type
2893 * @ap: port
2894 *
2895 * Helper method for drivers which have SATA cables
2896 */
2897
2898int ata_cable_sata(struct ata_port *ap)
2899{
2900 return ATA_CBL_SATA;
2901}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01002902EXPORT_SYMBOL_GPL(ata_cable_sata);
Alan Coxbe0d18d2007-03-06 02:37:56 -08002903
2904/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905 * ata_bus_probe - Reset and probe ATA bus
2906 * @ap: Bus to probe
2907 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04002908 * Master ATA bus probing function. Initiates a hardware-dependent
2909 * bus reset, then attempts to identify any devices found on
2910 * the bus.
2911 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002913 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914 *
2915 * RETURNS:
Tejun Heo96072e62006-04-01 01:38:17 +09002916 * Zero on success, negative errno otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917 */
2918
Brian King80289162006-08-07 14:27:31 -05002919int ata_bus_probe(struct ata_port *ap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920{
Tejun Heo28ca5c52006-03-01 16:09:36 +09002921 unsigned int classes[ATA_MAX_DEVICES];
Tejun Heo14d2bac2006-04-02 17:54:46 +09002922 int tries[ATA_MAX_DEVICES];
Tejun Heof58229f2007-08-06 18:36:23 +09002923 int rc;
Tejun Heoe82cbdb2006-04-01 01:38:18 +09002924 struct ata_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925
Tejun Heo1eca4362008-11-03 20:03:17 +09002926 ata_for_each_dev(dev, &ap->link, ALL)
Tejun Heof58229f2007-08-06 18:36:23 +09002927 tries[dev->devno] = ATA_PROBE_MAX_TRIES;
Tejun Heo14d2bac2006-04-02 17:54:46 +09002928
2929 retry:
Tejun Heo1eca4362008-11-03 20:03:17 +09002930 ata_for_each_dev(dev, &ap->link, ALL) {
Tejun Heocdeab112007-10-29 16:41:09 +09002931 /* If we issue an SRST then an ATA drive (not ATAPI)
2932 * may change configuration and be in PIO0 timing. If
2933 * we do a hard reset (or are coming from power on)
2934 * this is true for ATA or ATAPI. Until we've set a
2935 * suitable controller mode we should not touch the
2936 * bus as we may be talking too fast.
2937 */
2938 dev->pio_mode = XFER_PIO_0;
Aaron Lu54169122012-12-03 11:35:02 +08002939 dev->dma_mode = 0xff;
Tejun Heocdeab112007-10-29 16:41:09 +09002940
2941 /* If the controller has a pio mode setup function
2942 * then use it to set the chipset to rights. Don't
2943 * touch the DMA setup as that will be dealt with when
2944 * configuring devices.
2945 */
2946 if (ap->ops->set_piomode)
2947 ap->ops->set_piomode(ap, dev);
2948 }
2949
Tejun Heo20444702006-03-13 01:57:01 +09002950 /* reset and determine device classes */
Tejun Heo52783c52006-05-31 18:28:22 +09002951 ap->ops->phy_reset(ap);
Tejun Heo2061a472006-03-12 00:57:39 +09002952
Tejun Heo1eca4362008-11-03 20:03:17 +09002953 ata_for_each_dev(dev, &ap->link, ALL) {
Tejun Heo3e4ec342010-05-10 21:41:30 +02002954 if (dev->class != ATA_DEV_UNKNOWN)
Tejun Heo52783c52006-05-31 18:28:22 +09002955 classes[dev->devno] = dev->class;
2956 else
2957 classes[dev->devno] = ATA_DEV_NONE;
Tejun Heo20444702006-03-13 01:57:01 +09002958
Tejun Heo52783c52006-05-31 18:28:22 +09002959 dev->class = ATA_DEV_UNKNOWN;
Tejun Heo28ca5c52006-03-01 16:09:36 +09002960 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961
Jeff Garzikf31f0cc2007-03-02 17:47:28 -05002962 /* read IDENTIFY page and configure devices. We have to do the identify
2963 specific sequence bass-ackwards so that PDIAG- is released by
2964 the slave device */
2965
Tejun Heo1eca4362008-11-03 20:03:17 +09002966 ata_for_each_dev(dev, &ap->link, ALL_REVERSE) {
Tejun Heof58229f2007-08-06 18:36:23 +09002967 if (tries[dev->devno])
2968 dev->class = classes[dev->devno];
Tejun Heo14d2bac2006-04-02 17:54:46 +09002969
Tejun Heoe1211e32006-04-01 01:38:18 +09002970 if (!ata_dev_enabled(dev))
Tejun Heoffeae412006-03-01 16:09:35 +09002971 continue;
2972
Tejun Heobff04642006-11-10 18:08:10 +09002973 rc = ata_dev_read_id(dev, &dev->class, ATA_READID_POSTRESET,
2974 dev->id);
Tejun Heo14d2bac2006-04-02 17:54:46 +09002975 if (rc)
2976 goto fail;
Jeff Garzikf31f0cc2007-03-02 17:47:28 -05002977 }
2978
Alan Coxbe0d18d2007-03-06 02:37:56 -08002979 /* Now ask for the cable type as PDIAG- should have been released */
2980 if (ap->ops->cable_detect)
2981 ap->cbl = ap->ops->cable_detect(ap);
2982
Tejun Heo1eca4362008-11-03 20:03:17 +09002983 /* We may have SATA bridge glue hiding here irrespective of
2984 * the reported cable types and sensed types. When SATA
2985 * drives indicate we have a bridge, we don't know which end
2986 * of the link the bridge is which is a problem.
2987 */
2988 ata_for_each_dev(dev, &ap->link, ENABLED)
Alan Cox614fe292007-08-22 23:22:45 +01002989 if (ata_id_is_sata(dev->id))
2990 ap->cbl = ATA_CBL_SATA;
Alan Cox614fe292007-08-22 23:22:45 +01002991
Jeff Garzikf31f0cc2007-03-02 17:47:28 -05002992 /* After the identify sequence we can now set up the devices. We do
2993 this in the normal order so that the user doesn't get confused */
2994
Tejun Heo1eca4362008-11-03 20:03:17 +09002995 ata_for_each_dev(dev, &ap->link, ENABLED) {
Tejun Heo9af5c9c2007-08-06 18:36:22 +09002996 ap->link.eh_context.i.flags |= ATA_EHI_PRINTINFO;
Tejun Heoefdaedc2006-11-01 18:38:52 +09002997 rc = ata_dev_configure(dev);
Tejun Heo9af5c9c2007-08-06 18:36:22 +09002998 ap->link.eh_context.i.flags &= ~ATA_EHI_PRINTINFO;
Tejun Heo14d2bac2006-04-02 17:54:46 +09002999 if (rc)
3000 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001 }
3002
Tejun Heoe82cbdb2006-04-01 01:38:18 +09003003 /* configure transfer mode */
Tejun Heo02607312007-08-06 18:36:23 +09003004 rc = ata_set_mode(&ap->link, &dev);
Tejun Heo4ae72a12007-02-02 16:22:30 +09003005 if (rc)
Tejun Heo51713d32006-04-11 22:26:29 +09003006 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007
Tejun Heo1eca4362008-11-03 20:03:17 +09003008 ata_for_each_dev(dev, &ap->link, ENABLED)
3009 return 0;
Alan Coxe35a9e02006-03-27 18:46:37 +01003010
Tejun Heo96072e62006-04-01 01:38:17 +09003011 return -ENODEV;
Tejun Heo14d2bac2006-04-02 17:54:46 +09003012
3013 fail:
Tejun Heo4ae72a12007-02-02 16:22:30 +09003014 tries[dev->devno]--;
3015
Tejun Heo14d2bac2006-04-02 17:54:46 +09003016 switch (rc) {
3017 case -EINVAL:
Tejun Heo4ae72a12007-02-02 16:22:30 +09003018 /* eeek, something went very wrong, give up */
Tejun Heo14d2bac2006-04-02 17:54:46 +09003019 tries[dev->devno] = 0;
3020 break;
Tejun Heo4ae72a12007-02-02 16:22:30 +09003021
3022 case -ENODEV:
3023 /* give it just one more chance */
3024 tries[dev->devno] = min(tries[dev->devno], 1);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05003025 fallthrough;
Tejun Heo14d2bac2006-04-02 17:54:46 +09003026 case -EIO:
Tejun Heo4ae72a12007-02-02 16:22:30 +09003027 if (tries[dev->devno] == 1) {
3028 /* This is the last chance, better to slow
3029 * down than lose it.
3030 */
Tejun Heoa07d4992009-01-29 20:31:33 +09003031 sata_down_spd_limit(&ap->link, 0);
Tejun Heo4ae72a12007-02-02 16:22:30 +09003032 ata_down_xfermask_limit(dev, ATA_DNXFER_PIO);
3033 }
Tejun Heo14d2bac2006-04-02 17:54:46 +09003034 }
3035
Tejun Heo4ae72a12007-02-02 16:22:30 +09003036 if (!tries[dev->devno])
Tejun Heo3373efd2006-05-15 20:57:53 +09003037 ata_dev_disable(dev);
Tejun Heoec573752006-04-11 22:26:29 +09003038
Tejun Heo14d2bac2006-04-02 17:54:46 +09003039 goto retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040}
3041
3042/**
Tejun Heo3be680b2005-12-19 22:35:02 +09003043 * sata_print_link_status - Print SATA link status
Tejun Heo936fd732007-08-06 18:36:23 +09003044 * @link: SATA link to printk link status about
Tejun Heo3be680b2005-12-19 22:35:02 +09003045 *
3046 * This function prints link speed and status of a SATA link.
3047 *
3048 * LOCKING:
3049 * None.
3050 */
Adrian Bunk6bdb4fc2008-04-21 11:51:11 +03003051static void sata_print_link_status(struct ata_link *link)
Tejun Heo3be680b2005-12-19 22:35:02 +09003052{
Tejun Heo6d5f9732006-04-03 00:09:41 +09003053 u32 sstatus, scontrol, tmp;
Tejun Heo3be680b2005-12-19 22:35:02 +09003054
Tejun Heo936fd732007-08-06 18:36:23 +09003055 if (sata_scr_read(link, SCR_STATUS, &sstatus))
Tejun Heo3be680b2005-12-19 22:35:02 +09003056 return;
Tejun Heo936fd732007-08-06 18:36:23 +09003057 sata_scr_read(link, SCR_CONTROL, &scontrol);
Tejun Heo3be680b2005-12-19 22:35:02 +09003058
Tejun Heob1c72912008-07-31 17:02:43 +09003059 if (ata_phys_link_online(link)) {
Tejun Heo3be680b2005-12-19 22:35:02 +09003060 tmp = (sstatus >> 4) & 0xf;
Joe Perchesa9a79df2011-04-15 15:51:59 -07003061 ata_link_info(link, "SATA link up %s (SStatus %X SControl %X)\n",
3062 sata_spd_string(tmp), sstatus, scontrol);
Tejun Heo3be680b2005-12-19 22:35:02 +09003063 } else {
Joe Perchesa9a79df2011-04-15 15:51:59 -07003064 ata_link_info(link, "SATA link down (SStatus %X SControl %X)\n",
3065 sstatus, scontrol);
Tejun Heo3be680b2005-12-19 22:35:02 +09003066 }
3067}
3068
3069/**
Alan Coxebdfca6e2006-03-23 15:38:34 +00003070 * ata_dev_pair - return other device on cable
Alan Coxebdfca6e2006-03-23 15:38:34 +00003071 * @adev: device
3072 *
3073 * Obtain the other device on the same cable, or if none is
3074 * present NULL is returned
3075 */
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05003076
Tejun Heo3373efd2006-05-15 20:57:53 +09003077struct ata_device *ata_dev_pair(struct ata_device *adev)
Alan Coxebdfca6e2006-03-23 15:38:34 +00003078{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09003079 struct ata_link *link = adev->link;
3080 struct ata_device *pair = &link->device[1 - adev->devno];
Tejun Heoe1211e32006-04-01 01:38:18 +09003081 if (!ata_dev_enabled(pair))
Alan Coxebdfca6e2006-03-23 15:38:34 +00003082 return NULL;
3083 return pair;
3084}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01003085EXPORT_SYMBOL_GPL(ata_dev_pair);
Alan Coxebdfca6e2006-03-23 15:38:34 +00003086
3087/**
Tejun Heo3c567b72006-05-15 20:57:23 +09003088 * sata_down_spd_limit - adjust SATA spd limit downward
Tejun Heo936fd732007-08-06 18:36:23 +09003089 * @link: Link to adjust SATA spd limit for
Tejun Heoa07d4992009-01-29 20:31:33 +09003090 * @spd_limit: Additional limit
Tejun Heo1c3fae42006-04-02 20:53:28 +09003091 *
Tejun Heo936fd732007-08-06 18:36:23 +09003092 * Adjust SATA spd limit of @link downward. Note that this
Tejun Heo1c3fae42006-04-02 20:53:28 +09003093 * function only adjusts the limit. The change must be applied
Tejun Heo3c567b72006-05-15 20:57:23 +09003094 * using sata_set_spd().
Tejun Heo1c3fae42006-04-02 20:53:28 +09003095 *
Tejun Heoa07d4992009-01-29 20:31:33 +09003096 * If @spd_limit is non-zero, the speed is limited to equal to or
3097 * lower than @spd_limit if such speed is supported. If
3098 * @spd_limit is slower than any supported speed, only the lowest
3099 * supported speed is allowed.
3100 *
Tejun Heo1c3fae42006-04-02 20:53:28 +09003101 * LOCKING:
3102 * Inherited from caller.
3103 *
3104 * RETURNS:
3105 * 0 on success, negative errno on failure
3106 */
Tejun Heoa07d4992009-01-29 20:31:33 +09003107int sata_down_spd_limit(struct ata_link *link, u32 spd_limit)
Tejun Heo1c3fae42006-04-02 20:53:28 +09003108{
Tejun Heo81952c52006-05-15 20:57:47 +09003109 u32 sstatus, spd, mask;
Tejun Heoa07d4992009-01-29 20:31:33 +09003110 int rc, bit;
Tejun Heo1c3fae42006-04-02 20:53:28 +09003111
Tejun Heo936fd732007-08-06 18:36:23 +09003112 if (!sata_scr_valid(link))
Tejun Heo008a7892007-07-16 14:29:40 +09003113 return -EOPNOTSUPP;
3114
3115 /* If SCR can be read, use it to determine the current SPD.
Tejun Heo936fd732007-08-06 18:36:23 +09003116 * If not, use cached value in link->sata_spd.
Tejun Heo008a7892007-07-16 14:29:40 +09003117 */
Tejun Heo936fd732007-08-06 18:36:23 +09003118 rc = sata_scr_read(link, SCR_STATUS, &sstatus);
Tejun Heo9913ff8a2009-01-29 20:31:31 +09003119 if (rc == 0 && ata_sstatus_online(sstatus))
Tejun Heo008a7892007-07-16 14:29:40 +09003120 spd = (sstatus >> 4) & 0xf;
3121 else
Tejun Heo936fd732007-08-06 18:36:23 +09003122 spd = link->sata_spd;
Tejun Heo1c3fae42006-04-02 20:53:28 +09003123
Tejun Heo936fd732007-08-06 18:36:23 +09003124 mask = link->sata_spd_limit;
Tejun Heo1c3fae42006-04-02 20:53:28 +09003125 if (mask <= 1)
3126 return -EINVAL;
Tejun Heo008a7892007-07-16 14:29:40 +09003127
3128 /* unconditionally mask off the highest bit */
Tejun Heoa07d4992009-01-29 20:31:33 +09003129 bit = fls(mask) - 1;
3130 mask &= ~(1 << bit);
Tejun Heo1c3fae42006-04-02 20:53:28 +09003131
David Milburn2dc0b462017-11-14 16:17:25 -06003132 /*
3133 * Mask off all speeds higher than or equal to the current one. At
3134 * this point, if current SPD is not available and we previously
3135 * recorded the link speed from SStatus, the driver has already
3136 * masked off the highest bit so mask should already be 1 or 0.
3137 * Otherwise, we should not force 1.5Gbps on a link where we have
3138 * not previously recorded speed from SStatus. Just return in this
3139 * case.
Tejun Heo008a7892007-07-16 14:29:40 +09003140 */
3141 if (spd > 1)
3142 mask &= (1 << (spd - 1)) - 1;
3143 else
David Milburn2dc0b462017-11-14 16:17:25 -06003144 return -EINVAL;
Tejun Heo008a7892007-07-16 14:29:40 +09003145
3146 /* were we already at the bottom? */
Tejun Heo1c3fae42006-04-02 20:53:28 +09003147 if (!mask)
3148 return -EINVAL;
3149
Tejun Heoa07d4992009-01-29 20:31:33 +09003150 if (spd_limit) {
3151 if (mask & ((1 << spd_limit) - 1))
3152 mask &= (1 << spd_limit) - 1;
3153 else {
3154 bit = ffs(mask) - 1;
3155 mask = 1 << bit;
3156 }
3157 }
3158
Tejun Heo936fd732007-08-06 18:36:23 +09003159 link->sata_spd_limit = mask;
Tejun Heo1c3fae42006-04-02 20:53:28 +09003160
Joe Perchesa9a79df2011-04-15 15:51:59 -07003161 ata_link_warn(link, "limiting SATA link speed to %s\n",
3162 sata_spd_string(fls(mask)));
Tejun Heo1c3fae42006-04-02 20:53:28 +09003163
3164 return 0;
3165}
3166
Bartlomiej Zolnierkiewicza9b2c122020-03-26 16:58:07 +01003167#ifdef CONFIG_ATA_ACPI
Tejun Heocf176e12006-04-02 17:54:46 +09003168/**
Tejun Heoa0f79b92007-12-18 16:33:05 +09003169 * ata_timing_cycle2mode - find xfer mode for the specified cycle duration
3170 * @xfer_shift: ATA_SHIFT_* value for transfer type to examine.
3171 * @cycle: cycle duration in ns
3172 *
3173 * Return matching xfer mode for @cycle. The returned mode is of
3174 * the transfer type specified by @xfer_shift. If @cycle is too
3175 * slow for @xfer_shift, 0xff is returned. If @cycle is faster
3176 * than the fastest known mode, the fasted mode is returned.
3177 *
3178 * LOCKING:
3179 * None.
3180 *
3181 * RETURNS:
3182 * Matching xfer_mode, 0xff if no match found.
3183 */
3184u8 ata_timing_cycle2mode(unsigned int xfer_shift, int cycle)
3185{
3186 u8 base_mode = 0xff, last_mode = 0xff;
3187 const struct ata_xfer_ent *ent;
3188 const struct ata_timing *t;
3189
3190 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
3191 if (ent->shift == xfer_shift)
3192 base_mode = ent->base;
3193
3194 for (t = ata_timing_find_mode(base_mode);
3195 t && ata_xfer_mode2shift(t->mode) == xfer_shift; t++) {
3196 unsigned short this_cycle;
3197
3198 switch (xfer_shift) {
3199 case ATA_SHIFT_PIO:
3200 case ATA_SHIFT_MWDMA:
3201 this_cycle = t->cycle;
3202 break;
3203 case ATA_SHIFT_UDMA:
3204 this_cycle = t->udma;
3205 break;
3206 default:
3207 return 0xff;
3208 }
3209
3210 if (cycle > this_cycle)
3211 break;
3212
3213 last_mode = t->mode;
3214 }
3215
3216 return last_mode;
3217}
Bartlomiej Zolnierkiewicza9b2c122020-03-26 16:58:07 +01003218#endif
Tejun Heoa0f79b92007-12-18 16:33:05 +09003219
3220/**
Tejun Heocf176e12006-04-02 17:54:46 +09003221 * ata_down_xfermask_limit - adjust dev xfer masks downward
Tejun Heocf176e12006-04-02 17:54:46 +09003222 * @dev: Device to adjust xfer masks
Tejun Heo458337d2007-02-02 16:22:30 +09003223 * @sel: ATA_DNXFER_* selector
Tejun Heocf176e12006-04-02 17:54:46 +09003224 *
3225 * Adjust xfer masks of @dev downward. Note that this function
3226 * does not apply the change. Invoking ata_set_mode() afterwards
3227 * will apply the limit.
3228 *
3229 * LOCKING:
3230 * Inherited from caller.
3231 *
3232 * RETURNS:
3233 * 0 on success, negative errno on failure
3234 */
Tejun Heo458337d2007-02-02 16:22:30 +09003235int ata_down_xfermask_limit(struct ata_device *dev, unsigned int sel)
Tejun Heocf176e12006-04-02 17:54:46 +09003236{
Tejun Heo458337d2007-02-02 16:22:30 +09003237 char buf[32];
Tejun Heo7dc951a2007-11-27 19:43:42 +09003238 unsigned long orig_mask, xfer_mask;
3239 unsigned long pio_mask, mwdma_mask, udma_mask;
Tejun Heo458337d2007-02-02 16:22:30 +09003240 int quiet, highbit;
Tejun Heocf176e12006-04-02 17:54:46 +09003241
Tejun Heo458337d2007-02-02 16:22:30 +09003242 quiet = !!(sel & ATA_DNXFER_QUIET);
3243 sel &= ~ATA_DNXFER_QUIET;
Tejun Heocf176e12006-04-02 17:54:46 +09003244
Tejun Heo458337d2007-02-02 16:22:30 +09003245 xfer_mask = orig_mask = ata_pack_xfermask(dev->pio_mask,
3246 dev->mwdma_mask,
3247 dev->udma_mask);
3248 ata_unpack_xfermask(xfer_mask, &pio_mask, &mwdma_mask, &udma_mask);
Tejun Heocf176e12006-04-02 17:54:46 +09003249
Tejun Heo458337d2007-02-02 16:22:30 +09003250 switch (sel) {
3251 case ATA_DNXFER_PIO:
3252 highbit = fls(pio_mask) - 1;
3253 pio_mask &= ~(1 << highbit);
3254 break;
3255
3256 case ATA_DNXFER_DMA:
3257 if (udma_mask) {
3258 highbit = fls(udma_mask) - 1;
3259 udma_mask &= ~(1 << highbit);
3260 if (!udma_mask)
3261 return -ENOENT;
3262 } else if (mwdma_mask) {
3263 highbit = fls(mwdma_mask) - 1;
3264 mwdma_mask &= ~(1 << highbit);
3265 if (!mwdma_mask)
3266 return -ENOENT;
3267 }
3268 break;
3269
3270 case ATA_DNXFER_40C:
3271 udma_mask &= ATA_UDMA_MASK_40C;
3272 break;
3273
3274 case ATA_DNXFER_FORCE_PIO0:
3275 pio_mask &= 1;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05003276 fallthrough;
Tejun Heo458337d2007-02-02 16:22:30 +09003277 case ATA_DNXFER_FORCE_PIO:
3278 mwdma_mask = 0;
3279 udma_mask = 0;
3280 break;
3281
Tejun Heo458337d2007-02-02 16:22:30 +09003282 default:
3283 BUG();
3284 }
3285
3286 xfer_mask &= ata_pack_xfermask(pio_mask, mwdma_mask, udma_mask);
3287
3288 if (!(xfer_mask & ATA_MASK_PIO) || xfer_mask == orig_mask)
3289 return -ENOENT;
3290
3291 if (!quiet) {
3292 if (xfer_mask & (ATA_MASK_MWDMA | ATA_MASK_UDMA))
3293 snprintf(buf, sizeof(buf), "%s:%s",
3294 ata_mode_string(xfer_mask),
3295 ata_mode_string(xfer_mask & ATA_MASK_PIO));
3296 else
3297 snprintf(buf, sizeof(buf), "%s",
3298 ata_mode_string(xfer_mask));
3299
Joe Perchesa9a79df2011-04-15 15:51:59 -07003300 ata_dev_warn(dev, "limiting speed to %s\n", buf);
Tejun Heo458337d2007-02-02 16:22:30 +09003301 }
Tejun Heocf176e12006-04-02 17:54:46 +09003302
3303 ata_unpack_xfermask(xfer_mask, &dev->pio_mask, &dev->mwdma_mask,
3304 &dev->udma_mask);
3305
Tejun Heocf176e12006-04-02 17:54:46 +09003306 return 0;
Tejun Heocf176e12006-04-02 17:54:46 +09003307}
3308
Tejun Heo3373efd2006-05-15 20:57:53 +09003309static int ata_dev_set_mode(struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003310{
Tejun Heod0cb43b2009-07-09 09:27:50 +09003311 struct ata_port *ap = dev->link->ap;
Tejun Heo9af5c9c2007-08-06 18:36:22 +09003312 struct ata_eh_context *ehc = &dev->link->eh_context;
Tejun Heod0cb43b2009-07-09 09:27:50 +09003313 const bool nosetxfer = dev->horkage & ATA_HORKAGE_NOSETXFER;
Tejun Heo4055dee2008-02-07 10:34:08 +09003314 const char *dev_err_whine = "";
3315 int ign_dev_err = 0;
Tejun Heod0cb43b2009-07-09 09:27:50 +09003316 unsigned int err_mask = 0;
Tejun Heo83206a22006-03-24 15:25:31 +09003317 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003318
Tejun Heoe8384602006-04-02 18:51:53 +09003319 dev->flags &= ~ATA_DFLAG_PIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320 if (dev->xfer_shift == ATA_SHIFT_PIO)
3321 dev->flags |= ATA_DFLAG_PIO;
3322
Tejun Heod0cb43b2009-07-09 09:27:50 +09003323 if (nosetxfer && ap->flags & ATA_FLAG_SATA && ata_id_is_sata(dev->id))
3324 dev_err_whine = " (SET_XFERMODE skipped)";
3325 else {
3326 if (nosetxfer)
Joe Perchesa9a79df2011-04-15 15:51:59 -07003327 ata_dev_warn(dev,
3328 "NOSETXFER but PATA detected - can't "
3329 "skip SETXFER, might malfunction\n");
Tejun Heod0cb43b2009-07-09 09:27:50 +09003330 err_mask = ata_dev_set_xfermode(dev);
3331 }
Jeff Garzik2dcb4072007-10-19 06:42:56 -04003332
Tejun Heo4055dee2008-02-07 10:34:08 +09003333 if (err_mask & ~AC_ERR_DEV)
3334 goto fail;
Jeff Garzik2dcb4072007-10-19 06:42:56 -04003335
Tejun Heo4055dee2008-02-07 10:34:08 +09003336 /* revalidate */
Tejun Heobaa1e782006-11-01 18:39:27 +09003337 ehc->i.flags |= ATA_EHI_POST_SETMODE;
Tejun Heo422c9da2007-09-23 13:14:12 +09003338 rc = ata_dev_revalidate(dev, ATA_DEV_UNKNOWN, 0);
Tejun Heobaa1e782006-11-01 18:39:27 +09003339 ehc->i.flags &= ~ATA_EHI_POST_SETMODE;
Tejun Heo5eb45c02006-04-02 18:51:52 +09003340 if (rc)
Tejun Heo83206a22006-03-24 15:25:31 +09003341 return rc;
Tejun Heo48a8a142006-03-05 17:55:58 +09003342
Alan Coxb93fda12008-04-08 16:36:44 +01003343 if (dev->xfer_shift == ATA_SHIFT_PIO) {
3344 /* Old CFA may refuse this command, which is just fine */
3345 if (ata_id_is_cfa(dev->id))
3346 ign_dev_err = 1;
3347 /* Catch several broken garbage emulations plus some pre
3348 ATA devices */
3349 if (ata_id_major_version(dev->id) == 0 &&
3350 dev->pio_mode <= XFER_PIO_2)
3351 ign_dev_err = 1;
3352 /* Some very old devices and some bad newer ones fail
3353 any kind of SET_XFERMODE request but support PIO0-2
3354 timings and no IORDY */
3355 if (!ata_id_has_iordy(dev->id) && dev->pio_mode <= XFER_PIO_2)
3356 ign_dev_err = 1;
3357 }
Tejun Heo4055dee2008-02-07 10:34:08 +09003358 /* Early MWDMA devices do DMA but don't allow DMA mode setting.
3359 Don't fail an MWDMA0 set IFF the device indicates it is in MWDMA0 */
3360 if (dev->xfer_shift == ATA_SHIFT_MWDMA &&
3361 dev->dma_mode == XFER_MW_DMA_0 &&
3362 (dev->id[63] >> 8) & 1)
3363 ign_dev_err = 1;
3364
3365 /* if the device is actually configured correctly, ignore dev err */
3366 if (dev->xfer_mode == ata_xfer_mask2mode(ata_id_xfermask(dev->id)))
3367 ign_dev_err = 1;
3368
3369 if (err_mask & AC_ERR_DEV) {
3370 if (!ign_dev_err)
3371 goto fail;
3372 else
3373 dev_err_whine = " (device error ignored)";
3374 }
3375
Tejun Heo23e71c32006-03-06 04:31:57 +09003376 DPRINTK("xfer_shift=%u, xfer_mode=0x%x\n",
3377 dev->xfer_shift, (int)dev->xfer_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003378
Damien Le Moal07b9b6d2018-05-09 09:28:10 +09003379 if (!(ehc->i.flags & ATA_EHI_QUIET) ||
3380 ehc->i.flags & ATA_EHI_DID_HARDRESET)
3381 ata_dev_info(dev, "configured for %s%s\n",
3382 ata_mode_string(ata_xfer_mode2mask(dev->xfer_mode)),
3383 dev_err_whine);
Tejun Heo4055dee2008-02-07 10:34:08 +09003384
Tejun Heo83206a22006-03-24 15:25:31 +09003385 return 0;
Tejun Heo4055dee2008-02-07 10:34:08 +09003386
3387 fail:
Joe Perchesa9a79df2011-04-15 15:51:59 -07003388 ata_dev_err(dev, "failed to set xfermode (err_mask=0x%x)\n", err_mask);
Tejun Heo4055dee2008-02-07 10:34:08 +09003389 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003390}
3391
Linus Torvalds1da177e2005-04-16 15:20:36 -07003392/**
Alan04351822007-03-06 02:37:52 -08003393 * ata_do_set_mode - Program timings and issue SET FEATURES - XFER
Tejun Heo02607312007-08-06 18:36:23 +09003394 * @link: link on which timings will be programmed
Joe Perches1967b7f2008-02-03 17:08:11 +02003395 * @r_failed_dev: out parameter for failed device
Linus Torvalds1da177e2005-04-16 15:20:36 -07003396 *
Alan04351822007-03-06 02:37:52 -08003397 * Standard implementation of the function used to tune and set
3398 * ATA device disk transfer mode (PIO3, UDMA6, etc.). If
3399 * ata_dev_set_mode() fails, pointer to the failing device is
Tejun Heoe82cbdb2006-04-01 01:38:18 +09003400 * returned in @r_failed_dev.
Jeff Garzik780a87f2005-05-30 15:41:05 -04003401 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003402 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003403 * PCI/etc. bus probe sem.
Tejun Heoe82cbdb2006-04-01 01:38:18 +09003404 *
3405 * RETURNS:
3406 * 0 on success, negative errno otherwise
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407 */
Alan04351822007-03-06 02:37:52 -08003408
Tejun Heo02607312007-08-06 18:36:23 +09003409int ata_do_set_mode(struct ata_link *link, struct ata_device **r_failed_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410{
Tejun Heo02607312007-08-06 18:36:23 +09003411 struct ata_port *ap = link->ap;
Tejun Heoe8e06192006-04-01 01:38:18 +09003412 struct ata_device *dev;
Tejun Heof58229f2007-08-06 18:36:23 +09003413 int rc = 0, used_dma = 0, found = 0;
Tejun Heo3adcebb2006-05-15 20:57:37 +09003414
Tejun Heoa6d5a512006-03-06 04:31:57 +09003415 /* step 1: calculate xfer_mask */
Tejun Heo1eca4362008-11-03 20:03:17 +09003416 ata_for_each_dev(dev, link, ENABLED) {
Tejun Heo7dc951a2007-11-27 19:43:42 +09003417 unsigned long pio_mask, dma_mask;
Alan Coxb3a70602007-10-02 12:38:26 -04003418 unsigned int mode_mask;
Tejun Heoa6d5a512006-03-06 04:31:57 +09003419
Alan Coxb3a70602007-10-02 12:38:26 -04003420 mode_mask = ATA_DMA_MASK_ATA;
3421 if (dev->class == ATA_DEV_ATAPI)
3422 mode_mask = ATA_DMA_MASK_ATAPI;
3423 else if (ata_id_is_cfa(dev->id))
3424 mode_mask = ATA_DMA_MASK_CFA;
3425
Tejun Heo3373efd2006-05-15 20:57:53 +09003426 ata_dev_xfermask(dev);
Tejun Heo33267322008-02-13 09:15:09 +09003427 ata_force_xfermask(dev);
Tejun Heoa6d5a512006-03-06 04:31:57 +09003428
Tejun Heoacf356b2006-03-24 14:07:50 +09003429 pio_mask = ata_pack_xfermask(dev->pio_mask, 0, 0);
Alan Coxb3a70602007-10-02 12:38:26 -04003430
3431 if (libata_dma_mask & mode_mask)
Sergei Shtylyov80a9c432011-12-22 22:41:32 +03003432 dma_mask = ata_pack_xfermask(0, dev->mwdma_mask,
3433 dev->udma_mask);
Alan Coxb3a70602007-10-02 12:38:26 -04003434 else
3435 dma_mask = 0;
3436
Tejun Heoacf356b2006-03-24 14:07:50 +09003437 dev->pio_mode = ata_xfer_mask2mode(pio_mask);
3438 dev->dma_mode = ata_xfer_mask2mode(dma_mask);
Alan Cox5444a6f2006-03-27 18:58:20 +01003439
Tejun Heo4f659772006-04-01 01:38:18 +09003440 found = 1;
Alan Coxb15b3eb2008-08-01 09:18:34 +01003441 if (ata_dma_enabled(dev))
Alan Cox5444a6f2006-03-27 18:58:20 +01003442 used_dma = 1;
Tejun Heoa6d5a512006-03-06 04:31:57 +09003443 }
Tejun Heo4f659772006-04-01 01:38:18 +09003444 if (!found)
Tejun Heoe82cbdb2006-04-01 01:38:18 +09003445 goto out;
Tejun Heoa6d5a512006-03-06 04:31:57 +09003446
3447 /* step 2: always set host PIO timings */
Tejun Heo1eca4362008-11-03 20:03:17 +09003448 ata_for_each_dev(dev, link, ENABLED) {
Tejun Heo70cd0712007-11-27 19:43:40 +09003449 if (dev->pio_mode == 0xff) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07003450 ata_dev_warn(dev, "no PIO support\n");
Tejun Heoe8e06192006-04-01 01:38:18 +09003451 rc = -EINVAL;
Tejun Heoe82cbdb2006-04-01 01:38:18 +09003452 goto out;
Tejun Heoe8e06192006-04-01 01:38:18 +09003453 }
3454
3455 dev->xfer_mode = dev->pio_mode;
3456 dev->xfer_shift = ATA_SHIFT_PIO;
3457 if (ap->ops->set_piomode)
3458 ap->ops->set_piomode(ap, dev);
3459 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003460
Tejun Heoa6d5a512006-03-06 04:31:57 +09003461 /* step 3: set host DMA timings */
Tejun Heo1eca4362008-11-03 20:03:17 +09003462 ata_for_each_dev(dev, link, ENABLED) {
3463 if (!ata_dma_enabled(dev))
Tejun Heoe8e06192006-04-01 01:38:18 +09003464 continue;
3465
3466 dev->xfer_mode = dev->dma_mode;
3467 dev->xfer_shift = ata_xfer_mode2shift(dev->dma_mode);
3468 if (ap->ops->set_dmamode)
3469 ap->ops->set_dmamode(ap, dev);
3470 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003471
3472 /* step 4: update devices' xfer mode */
Tejun Heo1eca4362008-11-03 20:03:17 +09003473 ata_for_each_dev(dev, link, ENABLED) {
Tejun Heo3373efd2006-05-15 20:57:53 +09003474 rc = ata_dev_set_mode(dev);
Tejun Heo5bbc53f2006-04-01 01:38:17 +09003475 if (rc)
Tejun Heoe82cbdb2006-04-01 01:38:18 +09003476 goto out;
Tejun Heo83206a22006-03-24 15:25:31 +09003477 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003478
Tejun Heoe8e06192006-04-01 01:38:18 +09003479 /* Record simplex status. If we selected DMA then the other
3480 * host channels are not permitted to do so.
Alan Cox5444a6f2006-03-27 18:58:20 +01003481 */
Jeff Garzikcca39742006-08-24 03:19:22 -04003482 if (used_dma && (ap->host->flags & ATA_HOST_SIMPLEX))
Alan032af1c2007-03-01 17:36:46 +00003483 ap->host->simplex_claimed = ap;
Alan Cox5444a6f2006-03-27 18:58:20 +01003484
Tejun Heoe82cbdb2006-04-01 01:38:18 +09003485 out:
3486 if (rc)
3487 *r_failed_dev = dev;
3488 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003489}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01003490EXPORT_SYMBOL_GPL(ata_do_set_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003491
3492/**
Tejun Heoaa2731a2008-04-07 22:47:19 +09003493 * ata_wait_ready - wait for link to become ready
3494 * @link: link to be waited on
3495 * @deadline: deadline jiffies for the operation
3496 * @check_ready: callback to check link readiness
3497 *
3498 * Wait for @link to become ready. @check_ready should return
3499 * positive number if @link is ready, 0 if it isn't, -ENODEV if
3500 * link doesn't seem to be occupied, other errno for other error
3501 * conditions.
3502 *
3503 * Transient -ENODEV conditions are allowed for
3504 * ATA_TMOUT_FF_WAIT.
3505 *
3506 * LOCKING:
3507 * EH context.
3508 *
3509 * RETURNS:
Masanari Iidac9b55602016-04-13 23:36:27 +09003510 * 0 if @link is ready before @deadline; otherwise, -errno.
Tejun Heoaa2731a2008-04-07 22:47:19 +09003511 */
3512int ata_wait_ready(struct ata_link *link, unsigned long deadline,
3513 int (*check_ready)(struct ata_link *link))
3514{
3515 unsigned long start = jiffies;
Tejun Heob48d58f2010-04-09 19:46:38 +09003516 unsigned long nodev_deadline;
Tejun Heoaa2731a2008-04-07 22:47:19 +09003517 int warned = 0;
3518
Tejun Heob48d58f2010-04-09 19:46:38 +09003519 /* choose which 0xff timeout to use, read comment in libata.h */
3520 if (link->ap->host->flags & ATA_HOST_PARALLEL_SCAN)
3521 nodev_deadline = ata_deadline(start, ATA_TMOUT_FF_WAIT_LONG);
3522 else
3523 nodev_deadline = ata_deadline(start, ATA_TMOUT_FF_WAIT);
3524
Tejun Heob1c72912008-07-31 17:02:43 +09003525 /* Slave readiness can't be tested separately from master. On
3526 * M/S emulation configuration, this function should be called
3527 * only on the master and it will handle both master and slave.
3528 */
3529 WARN_ON(link == link->ap->slave_link);
3530
Tejun Heoaa2731a2008-04-07 22:47:19 +09003531 if (time_after(nodev_deadline, deadline))
3532 nodev_deadline = deadline;
3533
3534 while (1) {
3535 unsigned long now = jiffies;
3536 int ready, tmp;
3537
3538 ready = tmp = check_ready(link);
3539 if (ready > 0)
3540 return 0;
3541
Tejun Heob48d58f2010-04-09 19:46:38 +09003542 /*
3543 * -ENODEV could be transient. Ignore -ENODEV if link
Tejun Heoaa2731a2008-04-07 22:47:19 +09003544 * is online. Also, some SATA devices take a long
Tejun Heob48d58f2010-04-09 19:46:38 +09003545 * time to clear 0xff after reset. Wait for
3546 * ATA_TMOUT_FF_WAIT[_LONG] on -ENODEV if link isn't
3547 * offline.
Tejun Heoaa2731a2008-04-07 22:47:19 +09003548 *
3549 * Note that some PATA controllers (pata_ali) explode
3550 * if status register is read more than once when
3551 * there's no device attached.
3552 */
3553 if (ready == -ENODEV) {
3554 if (ata_link_online(link))
3555 ready = 0;
3556 else if ((link->ap->flags & ATA_FLAG_SATA) &&
3557 !ata_link_offline(link) &&
3558 time_before(now, nodev_deadline))
3559 ready = 0;
3560 }
3561
3562 if (ready)
3563 return ready;
3564 if (time_after(now, deadline))
3565 return -EBUSY;
3566
3567 if (!warned && time_after(now, start + 5 * HZ) &&
3568 (deadline - now > 3 * HZ)) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07003569 ata_link_warn(link,
Tejun Heoaa2731a2008-04-07 22:47:19 +09003570 "link is slow to respond, please be patient "
3571 "(ready=%d)\n", tmp);
3572 warned = 1;
3573 }
3574
Tejun Heo97750ce2010-09-06 17:56:29 +02003575 ata_msleep(link->ap, 50);
Tejun Heoaa2731a2008-04-07 22:47:19 +09003576 }
3577}
3578
3579/**
3580 * ata_wait_after_reset - wait for link to become ready after reset
3581 * @link: link to be waited on
3582 * @deadline: deadline jiffies for the operation
3583 * @check_ready: callback to check link readiness
3584 *
3585 * Wait for @link to become ready after reset.
3586 *
3587 * LOCKING:
3588 * EH context.
3589 *
3590 * RETURNS:
Masanari Iidac9b55602016-04-13 23:36:27 +09003591 * 0 if @link is ready before @deadline; otherwise, -errno.
Tejun Heoaa2731a2008-04-07 22:47:19 +09003592 */
Harvey Harrison2b4221b2008-04-24 18:37:34 -07003593int ata_wait_after_reset(struct ata_link *link, unsigned long deadline,
Tejun Heoaa2731a2008-04-07 22:47:19 +09003594 int (*check_ready)(struct ata_link *link))
3595{
Tejun Heo97750ce2010-09-06 17:56:29 +02003596 ata_msleep(link->ap, ATA_WAIT_AFTER_RESET);
Tejun Heoaa2731a2008-04-07 22:47:19 +09003597
3598 return ata_wait_ready(link, deadline, check_ready);
3599}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01003600EXPORT_SYMBOL_GPL(ata_wait_after_reset);
Tejun Heoaa2731a2008-04-07 22:47:19 +09003601
3602/**
Tejun Heo0aa11132008-04-07 22:47:18 +09003603 * ata_std_prereset - prepare for reset
Tejun Heocc0680a2007-08-06 18:36:23 +09003604 * @link: ATA link to be reset
Tejun Heod4b2bab2007-02-02 16:50:52 +09003605 * @deadline: deadline jiffies for the operation
Tejun Heof5914a42006-05-31 18:27:48 +09003606 *
Tejun Heocc0680a2007-08-06 18:36:23 +09003607 * @link is about to be reset. Initialize it. Failure from
Tejun Heob8cffc62007-02-02 16:50:52 +09003608 * prereset makes libata abort whole reset sequence and give up
3609 * that port, so prereset should be best-effort. It does its
3610 * best to prepare for reset sequence but if things go wrong, it
3611 * should just whine, not fail.
Tejun Heof5914a42006-05-31 18:27:48 +09003612 *
3613 * LOCKING:
3614 * Kernel thread context (may sleep)
3615 *
3616 * RETURNS:
3617 * 0 on success, -errno otherwise.
3618 */
Tejun Heo0aa11132008-04-07 22:47:18 +09003619int ata_std_prereset(struct ata_link *link, unsigned long deadline)
Tejun Heof5914a42006-05-31 18:27:48 +09003620{
Tejun Heocc0680a2007-08-06 18:36:23 +09003621 struct ata_port *ap = link->ap;
Tejun Heo936fd732007-08-06 18:36:23 +09003622 struct ata_eh_context *ehc = &link->eh_context;
Tejun Heoe9c83912006-07-03 16:07:26 +09003623 const unsigned long *timing = sata_ehc_deb_timing(ehc);
Tejun Heof5914a42006-05-31 18:27:48 +09003624 int rc;
3625
Tejun Heof5914a42006-05-31 18:27:48 +09003626 /* if we're about to do hardreset, nothing more to do */
3627 if (ehc->i.action & ATA_EH_HARDRESET)
3628 return 0;
3629
Tejun Heo936fd732007-08-06 18:36:23 +09003630 /* if SATA, resume link */
Tejun Heoa16abc02007-05-21 18:33:47 +02003631 if (ap->flags & ATA_FLAG_SATA) {
Tejun Heo936fd732007-08-06 18:36:23 +09003632 rc = sata_link_resume(link, timing, deadline);
Tejun Heob8cffc62007-02-02 16:50:52 +09003633 /* whine about phy resume failure but proceed */
3634 if (rc && rc != -EOPNOTSUPP)
Joe Perchesa9a79df2011-04-15 15:51:59 -07003635 ata_link_warn(link,
3636 "failed to resume link for reset (errno=%d)\n",
3637 rc);
Tejun Heof5914a42006-05-31 18:27:48 +09003638 }
3639
Tejun Heo45db2f62008-04-08 01:46:56 +09003640 /* no point in trying softreset on offline link */
Tejun Heob1c72912008-07-31 17:02:43 +09003641 if (ata_phys_link_offline(link))
Tejun Heo45db2f62008-04-08 01:46:56 +09003642 ehc->i.action &= ~ATA_EH_SOFTRESET;
3643
Tejun Heof5914a42006-05-31 18:27:48 +09003644 return 0;
3645}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01003646EXPORT_SYMBOL_GPL(ata_std_prereset);
Tejun Heof5914a42006-05-31 18:27:48 +09003647
Tejun Heoc2bd5802006-01-24 17:05:22 +09003648/**
Tejun Heo57c9efd2008-04-07 22:47:19 +09003649 * sata_std_hardreset - COMRESET w/o waiting or classification
3650 * @link: link to reset
3651 * @class: resulting class of attached device
3652 * @deadline: deadline jiffies for the operation
3653 *
3654 * Standard SATA COMRESET w/o waiting or classification.
3655 *
3656 * LOCKING:
3657 * Kernel thread context (may sleep)
3658 *
3659 * RETURNS:
3660 * 0 if link offline, -EAGAIN if link online, -errno on errors.
3661 */
3662int sata_std_hardreset(struct ata_link *link, unsigned int *class,
3663 unsigned long deadline)
3664{
3665 const unsigned long *timing = sata_ehc_deb_timing(&link->eh_context);
3666 bool online;
3667 int rc;
3668
3669 /* do hardreset */
3670 rc = sata_link_hardreset(link, timing, deadline, &online, NULL);
Tejun Heo57c9efd2008-04-07 22:47:19 +09003671 return online ? -EAGAIN : rc;
3672}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01003673EXPORT_SYMBOL_GPL(sata_std_hardreset);
Tejun Heo57c9efd2008-04-07 22:47:19 +09003674
3675/**
Tejun Heo203c75b2008-04-07 22:47:18 +09003676 * ata_std_postreset - standard postreset callback
Tejun Heocc0680a2007-08-06 18:36:23 +09003677 * @link: the target ata_link
Tejun Heoc2bd5802006-01-24 17:05:22 +09003678 * @classes: classes of attached devices
3679 *
3680 * This function is invoked after a successful reset. Note that
3681 * the device might have been reset more than once using
3682 * different reset methods before postreset is invoked.
Tejun Heoc2bd5802006-01-24 17:05:22 +09003683 *
Tejun Heoc2bd5802006-01-24 17:05:22 +09003684 * LOCKING:
3685 * Kernel thread context (may sleep)
3686 */
Tejun Heo203c75b2008-04-07 22:47:18 +09003687void ata_std_postreset(struct ata_link *link, unsigned int *classes)
Tejun Heoc2bd5802006-01-24 17:05:22 +09003688{
Tejun Heof0465192008-05-19 01:15:08 +09003689 u32 serror;
3690
Tejun Heoc2bd5802006-01-24 17:05:22 +09003691 DPRINTK("ENTER\n");
3692
Tejun Heof0465192008-05-19 01:15:08 +09003693 /* reset complete, clear SError */
3694 if (!sata_scr_read(link, SCR_ERROR, &serror))
3695 sata_scr_write(link, SCR_ERROR, serror);
3696
Tejun Heoc2bd5802006-01-24 17:05:22 +09003697 /* print link status */
Tejun Heo936fd732007-08-06 18:36:23 +09003698 sata_print_link_status(link);
Tejun Heoc2bd5802006-01-24 17:05:22 +09003699
Tejun Heoc2bd5802006-01-24 17:05:22 +09003700 DPRINTK("EXIT\n");
3701}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01003702EXPORT_SYMBOL_GPL(ata_std_postreset);
Tejun Heoc2bd5802006-01-24 17:05:22 +09003703
Tejun Heoa62c0fc2006-01-24 17:05:22 +09003704/**
Tejun Heo623a3122006-03-05 17:55:58 +09003705 * ata_dev_same_device - Determine whether new ID matches configured device
Tejun Heo623a3122006-03-05 17:55:58 +09003706 * @dev: device to compare against
3707 * @new_class: class of the new device
3708 * @new_id: IDENTIFY page of the new device
3709 *
3710 * Compare @new_class and @new_id against @dev and determine
3711 * whether @dev is the device indicated by @new_class and
3712 * @new_id.
3713 *
3714 * LOCKING:
3715 * None.
3716 *
3717 * RETURNS:
3718 * 1 if @dev matches @new_class and @new_id, 0 otherwise.
3719 */
Tejun Heo3373efd2006-05-15 20:57:53 +09003720static int ata_dev_same_device(struct ata_device *dev, unsigned int new_class,
3721 const u16 *new_id)
Tejun Heo623a3122006-03-05 17:55:58 +09003722{
3723 const u16 *old_id = dev->id;
Tejun Heoa0cf7332007-01-02 20:18:49 +09003724 unsigned char model[2][ATA_ID_PROD_LEN + 1];
3725 unsigned char serial[2][ATA_ID_SERNO_LEN + 1];
Tejun Heo623a3122006-03-05 17:55:58 +09003726
3727 if (dev->class != new_class) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07003728 ata_dev_info(dev, "class mismatch %d != %d\n",
3729 dev->class, new_class);
Tejun Heo623a3122006-03-05 17:55:58 +09003730 return 0;
3731 }
3732
Tejun Heoa0cf7332007-01-02 20:18:49 +09003733 ata_id_c_string(old_id, model[0], ATA_ID_PROD, sizeof(model[0]));
3734 ata_id_c_string(new_id, model[1], ATA_ID_PROD, sizeof(model[1]));
3735 ata_id_c_string(old_id, serial[0], ATA_ID_SERNO, sizeof(serial[0]));
3736 ata_id_c_string(new_id, serial[1], ATA_ID_SERNO, sizeof(serial[1]));
Tejun Heo623a3122006-03-05 17:55:58 +09003737
3738 if (strcmp(model[0], model[1])) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07003739 ata_dev_info(dev, "model number mismatch '%s' != '%s'\n",
3740 model[0], model[1]);
Tejun Heo623a3122006-03-05 17:55:58 +09003741 return 0;
3742 }
3743
3744 if (strcmp(serial[0], serial[1])) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07003745 ata_dev_info(dev, "serial number mismatch '%s' != '%s'\n",
3746 serial[0], serial[1]);
Tejun Heo623a3122006-03-05 17:55:58 +09003747 return 0;
3748 }
3749
Tejun Heo623a3122006-03-05 17:55:58 +09003750 return 1;
3751}
3752
3753/**
Tejun Heofe309112007-05-15 03:28:15 +09003754 * ata_dev_reread_id - Re-read IDENTIFY data
Henrik Kretzschmar3fae4502007-06-19 10:10:50 +02003755 * @dev: target ATA device
Tejun Heobff04642006-11-10 18:08:10 +09003756 * @readid_flags: read ID flags
Tejun Heo623a3122006-03-05 17:55:58 +09003757 *
3758 * Re-read IDENTIFY page and make sure @dev is still attached to
3759 * the port.
3760 *
3761 * LOCKING:
3762 * Kernel thread context (may sleep)
3763 *
3764 * RETURNS:
3765 * 0 on success, negative errno otherwise
3766 */
Tejun Heofe309112007-05-15 03:28:15 +09003767int ata_dev_reread_id(struct ata_device *dev, unsigned int readid_flags)
Tejun Heo623a3122006-03-05 17:55:58 +09003768{
Tejun Heo5eb45c02006-04-02 18:51:52 +09003769 unsigned int class = dev->class;
Tejun Heo9af5c9c2007-08-06 18:36:22 +09003770 u16 *id = (void *)dev->link->ap->sector_buf;
Tejun Heo623a3122006-03-05 17:55:58 +09003771 int rc;
3772
Tejun Heofe635c72006-05-15 20:57:35 +09003773 /* read ID data */
Tejun Heobff04642006-11-10 18:08:10 +09003774 rc = ata_dev_read_id(dev, &class, readid_flags, id);
Tejun Heo623a3122006-03-05 17:55:58 +09003775 if (rc)
Tejun Heofe309112007-05-15 03:28:15 +09003776 return rc;
Tejun Heo623a3122006-03-05 17:55:58 +09003777
3778 /* is the device still there? */
Tejun Heofe309112007-05-15 03:28:15 +09003779 if (!ata_dev_same_device(dev, class, id))
3780 return -ENODEV;
Tejun Heo623a3122006-03-05 17:55:58 +09003781
Tejun Heofe635c72006-05-15 20:57:35 +09003782 memcpy(dev->id, id, sizeof(id[0]) * ATA_ID_WORDS);
Tejun Heofe309112007-05-15 03:28:15 +09003783 return 0;
3784}
3785
3786/**
3787 * ata_dev_revalidate - Revalidate ATA device
3788 * @dev: device to revalidate
Tejun Heo422c9da2007-09-23 13:14:12 +09003789 * @new_class: new class code
Tejun Heofe309112007-05-15 03:28:15 +09003790 * @readid_flags: read ID flags
3791 *
3792 * Re-read IDENTIFY page, make sure @dev is still attached to the
3793 * port and reconfigure it according to the new IDENTIFY page.
3794 *
3795 * LOCKING:
3796 * Kernel thread context (may sleep)
3797 *
3798 * RETURNS:
3799 * 0 on success, negative errno otherwise
3800 */
Tejun Heo422c9da2007-09-23 13:14:12 +09003801int ata_dev_revalidate(struct ata_device *dev, unsigned int new_class,
3802 unsigned int readid_flags)
Tejun Heofe309112007-05-15 03:28:15 +09003803{
Tejun Heo6ddcd3b2007-05-15 03:28:15 +09003804 u64 n_sectors = dev->n_sectors;
Tejun Heo5920dad2009-07-15 17:11:41 +09003805 u64 n_native_sectors = dev->n_native_sectors;
Tejun Heofe309112007-05-15 03:28:15 +09003806 int rc;
3807
3808 if (!ata_dev_enabled(dev))
3809 return -ENODEV;
3810
Tejun Heo422c9da2007-09-23 13:14:12 +09003811 /* fail early if !ATA && !ATAPI to avoid issuing [P]IDENTIFY to PMP */
3812 if (ata_class_enabled(new_class) &&
Borislav Petkovf0d06132009-04-26 16:33:34 +02003813 new_class != ATA_DEV_ATA &&
3814 new_class != ATA_DEV_ATAPI &&
Hannes Reinecke9162c652014-11-05 13:08:21 +01003815 new_class != ATA_DEV_ZAC &&
Borislav Petkovf0d06132009-04-26 16:33:34 +02003816 new_class != ATA_DEV_SEMB) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07003817 ata_dev_info(dev, "class mismatch %u != %u\n",
3818 dev->class, new_class);
Tejun Heo422c9da2007-09-23 13:14:12 +09003819 rc = -ENODEV;
3820 goto fail;
3821 }
3822
Tejun Heofe309112007-05-15 03:28:15 +09003823 /* re-read ID */
3824 rc = ata_dev_reread_id(dev, readid_flags);
3825 if (rc)
3826 goto fail;
Tejun Heo623a3122006-03-05 17:55:58 +09003827
3828 /* configure device according to the new ID */
Tejun Heoefdaedc2006-11-01 18:38:52 +09003829 rc = ata_dev_configure(dev);
Tejun Heo6ddcd3b2007-05-15 03:28:15 +09003830 if (rc)
3831 goto fail;
3832
3833 /* verify n_sectors hasn't changed */
Tejun Heo445d2112010-04-05 10:33:13 +09003834 if (dev->class != ATA_DEV_ATA || !n_sectors ||
3835 dev->n_sectors == n_sectors)
3836 return 0;
3837
3838 /* n_sectors has changed */
Joe Perchesa9a79df2011-04-15 15:51:59 -07003839 ata_dev_warn(dev, "n_sectors mismatch %llu != %llu\n",
3840 (unsigned long long)n_sectors,
3841 (unsigned long long)dev->n_sectors);
Tejun Heo445d2112010-04-05 10:33:13 +09003842
3843 /*
3844 * Something could have caused HPA to be unlocked
3845 * involuntarily. If n_native_sectors hasn't changed and the
3846 * new size matches it, keep the device.
3847 */
3848 if (dev->n_native_sectors == n_native_sectors &&
3849 dev->n_sectors > n_sectors && dev->n_sectors == n_native_sectors) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07003850 ata_dev_warn(dev,
3851 "new n_sectors matches native, probably "
3852 "late HPA unlock, n_sectors updated\n");
Tejun Heo68939ce2010-05-15 20:09:33 +02003853 /* use the larger n_sectors */
Tejun Heo445d2112010-04-05 10:33:13 +09003854 return 0;
Tejun Heo6ddcd3b2007-05-15 03:28:15 +09003855 }
3856
Tejun Heo445d2112010-04-05 10:33:13 +09003857 /*
3858 * Some BIOSes boot w/o HPA but resume w/ HPA locked. Try
3859 * unlocking HPA in those cases.
3860 *
3861 * https://bugzilla.kernel.org/show_bug.cgi?id=15396
3862 */
3863 if (dev->n_native_sectors == n_native_sectors &&
3864 dev->n_sectors < n_sectors && n_sectors == n_native_sectors &&
3865 !(dev->horkage & ATA_HORKAGE_BROKEN_HPA)) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07003866 ata_dev_warn(dev,
3867 "old n_sectors matches native, probably "
3868 "late HPA lock, will try to unlock HPA\n");
Tejun Heo445d2112010-04-05 10:33:13 +09003869 /* try unlocking HPA */
3870 dev->flags |= ATA_DFLAG_UNLOCK_HPA;
3871 rc = -EIO;
3872 } else
3873 rc = -ENODEV;
Tejun Heo623a3122006-03-05 17:55:58 +09003874
Tejun Heo445d2112010-04-05 10:33:13 +09003875 /* restore original n_[native_]sectors and fail */
3876 dev->n_native_sectors = n_native_sectors;
3877 dev->n_sectors = n_sectors;
Tejun Heo623a3122006-03-05 17:55:58 +09003878 fail:
Joe Perchesa9a79df2011-04-15 15:51:59 -07003879 ata_dev_err(dev, "revalidation failed (errno=%d)\n", rc);
Tejun Heo623a3122006-03-05 17:55:58 +09003880 return rc;
3881}
3882
Alan Cox6919a0a2006-10-27 19:08:46 -07003883struct ata_blacklist_entry {
3884 const char *model_num;
3885 const char *model_rev;
3886 unsigned long horkage;
3887};
3888
3889static const struct ata_blacklist_entry ata_device_blacklist [] = {
3890 /* Devices with DMA related problems under Linux */
3891 { "WDC AC11000H", NULL, ATA_HORKAGE_NODMA },
3892 { "WDC AC22100H", NULL, ATA_HORKAGE_NODMA },
3893 { "WDC AC32500H", NULL, ATA_HORKAGE_NODMA },
3894 { "WDC AC33100H", NULL, ATA_HORKAGE_NODMA },
3895 { "WDC AC31600H", NULL, ATA_HORKAGE_NODMA },
3896 { "WDC AC32100H", "24.09P07", ATA_HORKAGE_NODMA },
3897 { "WDC AC23200L", "21.10N21", ATA_HORKAGE_NODMA },
3898 { "Compaq CRD-8241B", NULL, ATA_HORKAGE_NODMA },
3899 { "CRD-8400B", NULL, ATA_HORKAGE_NODMA },
Mark Lord7da4c932010-07-01 18:18:12 -04003900 { "CRD-848[02]B", NULL, ATA_HORKAGE_NODMA },
Alan Cox6919a0a2006-10-27 19:08:46 -07003901 { "CRD-84", NULL, ATA_HORKAGE_NODMA },
3902 { "SanDisk SDP3B", NULL, ATA_HORKAGE_NODMA },
3903 { "SanDisk SDP3B-64", NULL, ATA_HORKAGE_NODMA },
3904 { "SANYO CD-ROM CRD", NULL, ATA_HORKAGE_NODMA },
3905 { "HITACHI CDR-8", NULL, ATA_HORKAGE_NODMA },
Mark Lord7da4c932010-07-01 18:18:12 -04003906 { "HITACHI CDR-8[34]35",NULL, ATA_HORKAGE_NODMA },
Alan Cox6919a0a2006-10-27 19:08:46 -07003907 { "Toshiba CD-ROM XM-6202B", NULL, ATA_HORKAGE_NODMA },
3908 { "TOSHIBA CD-ROM XM-1702BC", NULL, ATA_HORKAGE_NODMA },
3909 { "CD-532E-A", NULL, ATA_HORKAGE_NODMA },
3910 { "E-IDE CD-ROM CR-840",NULL, ATA_HORKAGE_NODMA },
3911 { "CD-ROM Drive/F5A", NULL, ATA_HORKAGE_NODMA },
3912 { "WPI CDD-820", NULL, ATA_HORKAGE_NODMA },
3913 { "SAMSUNG CD-ROM SC-148C", NULL, ATA_HORKAGE_NODMA },
3914 { "SAMSUNG CD-ROM SC", NULL, ATA_HORKAGE_NODMA },
Alan Cox6919a0a2006-10-27 19:08:46 -07003915 { "ATAPI CD-ROM DRIVE 40X MAXIMUM",NULL,ATA_HORKAGE_NODMA },
3916 { "_NEC DV5800A", NULL, ATA_HORKAGE_NODMA },
Jeff Garzik2dcb4072007-10-19 06:42:56 -04003917 { "SAMSUNG CD-ROM SN-124", "N001", ATA_HORKAGE_NODMA },
Dave Jones39f19882007-05-21 14:31:03 -04003918 { "Seagate STT20000A", NULL, ATA_HORKAGE_NODMA },
Prarit Bhargavad17d7942012-08-23 15:11:52 -04003919 { " 2GB ATA Flash Disk", "ADMA428M", ATA_HORKAGE_NODMA },
Shikha Jainb00622f2016-01-11 14:55:45 -08003920 { "VRFDFC22048UCHC-TE*", NULL, ATA_HORKAGE_NODMA },
Tejun Heo3af9a772007-09-23 13:19:54 +09003921 /* Odd clown on sil3726/4726 PMPs */
Tejun Heo50af2fa2008-05-19 01:15:14 +09003922 { "Config Disk", NULL, ATA_HORKAGE_DISABLE },
Hannes Reineckea66307d2021-12-08 07:58:53 +01003923 /* Similar story with ASMedia 1092 */
3924 { "ASMT109x- Config", NULL, ATA_HORKAGE_DISABLE },
Alan Cox6919a0a2006-10-27 19:08:46 -07003925
Albert Lee18d6e9d2007-04-02 11:34:15 +08003926 /* Weird ATAPI devices */
Tejun Heo40a1d532007-06-27 02:49:38 +09003927 { "TORiSAN DVD-ROM DRD-N216", NULL, ATA_HORKAGE_MAX_SEC_128 },
Tejun Heo6a87e422008-11-03 19:01:09 +09003928 { "QUANTUM DAT DAT72-000", NULL, ATA_HORKAGE_ATAPI_MOD16_DMA },
Shan Haia32450e2013-03-18 10:30:44 +08003929 { "Slimtype DVD A DS8A8SH", NULL, ATA_HORKAGE_MAX_SEC_LBA48 },
Shan Hai0523f032013-10-28 16:08:01 +08003930 { "Slimtype DVD A DS8A9SH", NULL, ATA_HORKAGE_MAX_SEC_LBA48 },
Albert Lee18d6e9d2007-04-02 11:34:15 +08003931
David Milburnaf34d632015-07-13 11:48:23 -05003932 /*
3933 * Causes silent data corruption with higher max sects.
3934 * http://lkml.kernel.org/g/x49wpy40ysk.fsf@segfault.boston.devel.redhat.com
3935 */
3936 { "ST380013AS", "3.20", ATA_HORKAGE_MAX_SEC_1024 },
3937
Tejun Heo1488a1e2016-07-18 18:40:00 -04003938 /*
Tejun Heoe0edc8c2017-01-06 11:48:50 -05003939 * These devices time out with higher max sects.
Tejun Heo1488a1e2016-07-18 18:40:00 -04003940 * https://bugzilla.kernel.org/show_bug.cgi?id=121671
3941 */
Tejun Heoe0edc8c2017-01-06 11:48:50 -05003942 { "LITEON CX1-JB*-HP", NULL, ATA_HORKAGE_MAX_SEC_1024 },
Xinyu Lindb5ff902017-12-17 20:13:39 +08003943 { "LITEON EP1-*", NULL, ATA_HORKAGE_MAX_SEC_1024 },
Tejun Heo1488a1e2016-07-18 18:40:00 -04003944
Alan Cox6919a0a2006-10-27 19:08:46 -07003945 /* Devices we expect to fail diagnostics */
3946
3947 /* Devices where NCQ should be avoided */
3948 /* NCQ is slow */
Jeff Garzik2dcb4072007-10-19 06:42:56 -04003949 { "WDC WD740ADFD-00", NULL, ATA_HORKAGE_NONCQ },
Tejun Heo459ad682007-12-07 12:46:23 +09003950 { "WDC WD740ADFD-00NLR1", NULL, ATA_HORKAGE_NONCQ, },
Tejun Heo09125ea2007-02-28 15:21:23 +09003951 /* http://thread.gmane.org/gmane.linux.ide/14907 */
3952 { "FUJITSU MHT2060BH", NULL, ATA_HORKAGE_NONCQ },
Paul Rolland7acfaf32007-03-26 21:43:44 -08003953 /* NCQ is broken */
Jeff Garzik539cc7c2007-09-20 16:31:47 -04003954 { "Maxtor *", "BANC*", ATA_HORKAGE_NONCQ },
Alan Cox0e3dbc02007-09-20 15:22:47 +01003955 { "Maxtor 7V300F0", "VA111630", ATA_HORKAGE_NONCQ },
Paolo Ornatida6f0ec2007-10-04 11:06:56 +09003956 { "ST380817AS", "3.42", ATA_HORKAGE_NONCQ },
Tejun Heoe41bd3e2007-12-09 19:45:39 +09003957 { "ST3160023AS", "3.42", ATA_HORKAGE_NONCQ },
Lubomir Bulej5ccfca92008-12-22 11:35:22 +01003958 { "OCZ CORE_SSD", "02.10104", ATA_HORKAGE_NONCQ },
Jeff Garzik539cc7c2007-09-20 16:31:47 -04003959
Tejun Heoac70a962008-11-27 13:36:48 +09003960 /* Seagate NCQ + FLUSH CACHE firmware bug */
Mark Lord4d1f9082010-07-05 18:53:37 -04003961 { "ST31500341AS", "SD1[5-9]", ATA_HORKAGE_NONCQ |
Tejun Heoac70a962008-11-27 13:36:48 +09003962 ATA_HORKAGE_FIRMWARE_WARN },
Tejun Heod10d4912008-12-11 13:42:42 +09003963
Mark Lord4d1f9082010-07-05 18:53:37 -04003964 { "ST31000333AS", "SD1[5-9]", ATA_HORKAGE_NONCQ |
Tejun Heod10d4912008-12-11 13:42:42 +09003965 ATA_HORKAGE_FIRMWARE_WARN },
3966
Mark Lord4d1f9082010-07-05 18:53:37 -04003967 { "ST3640[36]23AS", "SD1[5-9]", ATA_HORKAGE_NONCQ |
Tejun Heod10d4912008-12-11 13:42:42 +09003968 ATA_HORKAGE_FIRMWARE_WARN },
3969
Mark Lord4d1f9082010-07-05 18:53:37 -04003970 { "ST3320[68]13AS", "SD1[5-9]", ATA_HORKAGE_NONCQ |
Tejun Heoac70a962008-11-27 13:36:48 +09003971 ATA_HORKAGE_FIRMWARE_WARN },
3972
Hans de Goede31f62642019-06-11 16:32:59 +02003973 /* drives which fail FPDMA_AA activation (some may freeze afterwards)
3974 the ST disks also have LPM issues */
Hans de Goede8756a252019-07-01 18:58:13 +02003975 { "ST1000LM024 HN-M101MBB", NULL, ATA_HORKAGE_BROKEN_FPDMA_AA |
Hans de Goede31f62642019-06-11 16:32:59 +02003976 ATA_HORKAGE_NOLPM, },
Aleksei Mamlin08c85d22015-07-01 13:48:30 +03003977 { "VB0250EAVER", "HPG7", ATA_HORKAGE_BROKEN_FPDMA_AA },
Michele Baldessari87809942013-11-25 19:00:14 +00003978
Robert Hancock36e337d2007-04-02 22:05:29 -06003979 /* Blacklist entries taken from Silicon Image 3124/3132
3980 Windows driver .inf file - also several Linux problem reports */
3981 { "HTS541060G9SA00", "MB3OC60D", ATA_HORKAGE_NONCQ, },
3982 { "HTS541080G9SA00", "MB4OC60D", ATA_HORKAGE_NONCQ, },
3983 { "HTS541010G9SA00", "MBZOC60D", ATA_HORKAGE_NONCQ, },
Alan Cox6919a0a2006-10-27 19:08:46 -07003984
Tejun Heo68b0ddb2010-04-05 10:51:26 +09003985 /* https://bugzilla.kernel.org/show_bug.cgi?id=15573 */
3986 { "C300-CTFDDAC128MAG", "0001", ATA_HORKAGE_NONCQ, },
3987
Tejun Heo3b545562020-09-02 12:32:45 -04003988 /* Sandisk SD7/8/9s lock up hard on large trims */
3989 { "SanDisk SD[789]*", NULL, ATA_HORKAGE_MAX_TRIM_128M, },
Tejun Heo322579d2018-05-08 14:21:56 -07003990
Tejun Heo16c55b02007-08-29 11:58:33 +09003991 /* devices which puke on READ_NATIVE_MAX */
3992 { "HDS724040KLSA80", "KFAOA20N", ATA_HORKAGE_BROKEN_HPA, },
3993 { "WDC WD3200JD-00KLB0", "WD-WCAMR1130137", ATA_HORKAGE_BROKEN_HPA },
3994 { "WDC WD2500JD-00HBB0", "WD-WMAL71490727", ATA_HORKAGE_BROKEN_HPA },
3995 { "MAXTOR 6L080L4", "A93.0500", ATA_HORKAGE_BROKEN_HPA },
Alan Cox6919a0a2006-10-27 19:08:46 -07003996
Tejun Heo7831387b2009-08-07 01:59:15 +09003997 /* this one allows HPA unlocking but fails IOs on the area */
3998 { "OCZ-VERTEX", "1.30", ATA_HORKAGE_BROKEN_HPA },
3999
Alan Cox93328e112007-09-29 04:06:48 -04004000 /* Devices which report 1 sector over size HPA */
4001 { "ST340823A", NULL, ATA_HORKAGE_HPA_SIZE, },
4002 { "ST320413A", NULL, ATA_HORKAGE_HPA_SIZE, },
Mikko Rapelib152fcd2008-02-19 01:41:25 +01004003 { "ST310211A", NULL, ATA_HORKAGE_HPA_SIZE, },
Alan Cox93328e112007-09-29 04:06:48 -04004004
Alan Cox6bbfd532007-11-05 22:58:58 +00004005 /* Devices which get the IVB wrong */
4006 { "QUANTUM FIREBALLlct10 05", "A03.0900", ATA_HORKAGE_IVB, },
Alan Coxa79067e2008-04-29 14:08:36 +01004007 /* Maybe we should just blacklist TSSTcorp... */
Mark Lord7da4c932010-07-01 18:18:12 -04004008 { "TSSTcorp CDDVDW SH-S202[HJN]", "SB0[01]", ATA_HORKAGE_IVB, },
Alan Cox6bbfd532007-11-05 22:58:58 +00004009
Jens Axboe9ce8e302008-08-27 15:23:18 +02004010 /* Devices that do not need bridging limits applied */
4011 { "MTRON MSP-SATA*", NULL, ATA_HORKAGE_BRIDGE_OK, },
Jeff Garzik04d0f1b82012-08-17 13:36:59 -04004012 { "BUFFALO HD-QSU2/R5", NULL, ATA_HORKAGE_BRIDGE_OK, },
Jens Axboe9ce8e302008-08-27 15:23:18 +02004013
Tejun Heo90627122009-01-29 20:31:36 +09004014 /* Devices which aren't very happy with higher link speeds */
4015 { "WD My Book", NULL, ATA_HORKAGE_1_5_GBPS, },
Daniel J Bluemanc5310772012-07-23 12:22:37 +08004016 { "Seagate FreeAgent GoFlex", NULL, ATA_HORKAGE_1_5_GBPS, },
Tejun Heo90627122009-01-29 20:31:36 +09004017
Tejun Heod0cb43b2009-07-09 09:27:50 +09004018 /*
4019 * Devices which choke on SETXFER. Applies only if both the
4020 * device and controller are SATA.
4021 */
Tejun Heocd691872011-06-22 12:13:10 +02004022 { "PIONEER DVD-RW DVRTD08", NULL, ATA_HORKAGE_NOSETXFER },
Vladimir LAVALLADE3a251792012-01-08 13:50:13 +01004023 { "PIONEER DVD-RW DVRTD08A", NULL, ATA_HORKAGE_NOSETXFER },
4024 { "PIONEER DVD-RW DVR-215", NULL, ATA_HORKAGE_NOSETXFER },
Tejun Heocd691872011-06-22 12:13:10 +02004025 { "PIONEER DVD-RW DVR-212D", NULL, ATA_HORKAGE_NOSETXFER },
4026 { "PIONEER DVD-RW DVR-216D", NULL, ATA_HORKAGE_NOSETXFER },
Tejun Heod0cb43b2009-07-09 09:27:50 +09004027
Kai-Heng Fengb17e57292018-02-18 22:17:09 +08004028 /* Crucial BX100 SSD 500GB has broken LPM support */
Hans de Goede3bf7b5d2018-03-19 16:33:59 +01004029 { "CT500BX100SSD1", NULL, ATA_HORKAGE_NOLPM },
Kai-Heng Fengb17e57292018-02-18 22:17:09 +08004030
Hans de Goeded418ff52018-03-19 16:34:00 +01004031 /* 512GB MX100 with MU01 firmware has both queued TRIM and LPM issues */
4032 { "Crucial_CT512MX100*", "MU01", ATA_HORKAGE_NO_NCQ_TRIM |
Hans de Goede9c7be59f2018-02-16 10:48:20 +01004033 ATA_HORKAGE_ZERO_AFTER_TRIM |
4034 ATA_HORKAGE_NOLPM, },
Hans de Goeded418ff52018-03-19 16:34:00 +01004035 /* 512GB MX100 with newer firmware has only LPM issues */
4036 { "Crucial_CT512MX100*", NULL, ATA_HORKAGE_ZERO_AFTER_TRIM |
4037 ATA_HORKAGE_NOLPM, },
Hans de Goede9c7be59f2018-02-16 10:48:20 +01004038
Hans de Goede62ac3f72018-03-19 16:33:58 +01004039 /* 480GB+ M500 SSDs have both queued TRIM and LPM issues */
4040 { "Crucial_CT480M500*", NULL, ATA_HORKAGE_NO_NCQ_TRIM |
4041 ATA_HORKAGE_ZERO_AFTER_TRIM |
4042 ATA_HORKAGE_NOLPM, },
4043 { "Crucial_CT960M500*", NULL, ATA_HORKAGE_NO_NCQ_TRIM |
4044 ATA_HORKAGE_ZERO_AFTER_TRIM |
4045 ATA_HORKAGE_NOLPM, },
4046
François Cami76936e92018-05-13 20:11:15 +02004047 /* These specific Samsung models/firmware-revs do not handle LPM well */
Hans de Goedeb5b4d3a2018-04-24 11:19:07 +02004048 { "SAMSUNG MZMPC128HBFU-000MV", "CXM14M1Q", ATA_HORKAGE_NOLPM, },
François Cami76936e92018-05-13 20:11:15 +02004049 { "SAMSUNG SSD PM830 mSATA *", "CXM13D1Q", ATA_HORKAGE_NOLPM, },
Diego Viola410b5c72018-11-12 17:22:52 -02004050 { "SAMSUNG MZ7TD256HAFV-000L9", NULL, ATA_HORKAGE_NOLPM, },
Hans de Goededd957492019-02-03 10:02:07 +01004051 { "SAMSUNG MZ7TE512HMHP-000L1", "EXT06L0Q", ATA_HORKAGE_NOLPM, },
Hans de Goedeb5b4d3a2018-04-24 11:19:07 +02004052
Marc Carinof78dea02013-12-16 18:15:53 -08004053 /* devices that don't properly handle queued TRIM commands */
Sudip Mukherjee136d7692018-05-19 22:29:36 +01004054 { "Micron_M500IT_*", "MU01", ATA_HORKAGE_NO_NCQ_TRIM |
4055 ATA_HORKAGE_ZERO_AFTER_TRIM, },
Martin K. Petersen243918b2015-06-18 14:50:18 -04004056 { "Micron_M500_*", NULL, ATA_HORKAGE_NO_NCQ_TRIM |
Martin K. Petersene61f7d12015-01-08 10:34:27 -05004057 ATA_HORKAGE_ZERO_AFTER_TRIM, },
Martin K. Petersenff7f53f2015-03-27 15:17:20 -04004058 { "Crucial_CT*M500*", NULL, ATA_HORKAGE_NO_NCQ_TRIM |
4059 ATA_HORKAGE_ZERO_AFTER_TRIM, },
Martin K. Petersen9051bd32015-07-15 21:03:23 -04004060 { "Micron_M5[15]0_*", "MU01", ATA_HORKAGE_NO_NCQ_TRIM |
Martin K. Petersenff7f53f2015-03-27 15:17:20 -04004061 ATA_HORKAGE_ZERO_AFTER_TRIM, },
4062 { "Crucial_CT*M550*", "MU01", ATA_HORKAGE_NO_NCQ_TRIM |
4063 ATA_HORKAGE_ZERO_AFTER_TRIM, },
4064 { "Crucial_CT*MX100*", "MU01", ATA_HORKAGE_NO_NCQ_TRIM |
4065 ATA_HORKAGE_ZERO_AFTER_TRIM, },
Ju Hyung Parkca6bfcb2018-03-11 02:28:35 +09004066 { "Samsung SSD 840*", NULL, ATA_HORKAGE_NO_NCQ_TRIM |
4067 ATA_HORKAGE_ZERO_AFTER_TRIM, },
4068 { "Samsung SSD 850*", NULL, ATA_HORKAGE_NO_NCQ_TRIM |
Martin K. Petersen6fc4d972015-03-27 15:17:21 -04004069 ATA_HORKAGE_ZERO_AFTER_TRIM, },
Hans de Goede8a6430a2021-08-23 11:52:20 +02004070 { "Samsung SSD 860*", NULL, ATA_HORKAGE_NO_NCQ_TRIM |
Kate Hsuan7a8526a2021-09-03 17:44:11 +08004071 ATA_HORKAGE_ZERO_AFTER_TRIM |
4072 ATA_HORKAGE_NO_NCQ_ON_ATI, },
Hans de Goede8a6430a2021-08-23 11:52:20 +02004073 { "Samsung SSD 870*", NULL, ATA_HORKAGE_NO_NCQ_TRIM |
Kate Hsuan7a8526a2021-09-03 17:44:11 +08004074 ATA_HORKAGE_ZERO_AFTER_TRIM |
4075 ATA_HORKAGE_NO_NCQ_ON_ATI, },
Guillermo A. Amaral7a7184b2015-08-25 23:29:13 -07004076 { "FCCT*M500*", NULL, ATA_HORKAGE_NO_NCQ_TRIM |
4077 ATA_HORKAGE_ZERO_AFTER_TRIM, },
Martin K. Petersene61f7d12015-01-08 10:34:27 -05004078
Arne Fitzenreitercda57b12015-07-15 13:54:37 +02004079 /* devices that don't properly handle TRIM commands */
4080 { "SuperSSpeed S238*", NULL, ATA_HORKAGE_NOTRIM, },
4081
Martin K. Petersene61f7d12015-01-08 10:34:27 -05004082 /*
4083 * As defined, the DRAT (Deterministic Read After Trim) and RZAT
4084 * (Return Zero After Trim) flags in the ATA Command Set are
4085 * unreliable in the sense that they only define what happens if
4086 * the device successfully executed the DSM TRIM command. TRIM
4087 * is only advisory, however, and the device is free to silently
4088 * ignore all or parts of the request.
4089 *
4090 * Whitelist drives that are known to reliably return zeroes
4091 * after TRIM.
4092 */
4093
4094 /*
4095 * The intel 510 drive has buggy DRAT/RZAT. Explicitly exclude
4096 * that model before whitelisting all other intel SSDs.
4097 */
4098 { "INTEL*SSDSC2MH*", NULL, 0, },
4099
Martin K. Petersenff7f53f2015-03-27 15:17:20 -04004100 { "Micron*", NULL, ATA_HORKAGE_ZERO_AFTER_TRIM, },
4101 { "Crucial*", NULL, ATA_HORKAGE_ZERO_AFTER_TRIM, },
Martin K. Petersene61f7d12015-01-08 10:34:27 -05004102 { "INTEL*SSD*", NULL, ATA_HORKAGE_ZERO_AFTER_TRIM, },
4103 { "SSD*INTEL*", NULL, ATA_HORKAGE_ZERO_AFTER_TRIM, },
4104 { "Samsung*SSD*", NULL, ATA_HORKAGE_ZERO_AFTER_TRIM, },
4105 { "SAMSUNG*SSD*", NULL, ATA_HORKAGE_ZERO_AFTER_TRIM, },
Juha-Matti Tillifd6f32f2018-12-02 12:47:08 +02004106 { "SAMSUNG*MZ7KM*", NULL, ATA_HORKAGE_ZERO_AFTER_TRIM, },
Martin K. Petersene61f7d12015-01-08 10:34:27 -05004107 { "ST[1248][0248]0[FH]*", NULL, ATA_HORKAGE_ZERO_AFTER_TRIM, },
Marc Carinof78dea02013-12-16 18:15:53 -08004108
Tejun Heoecd75ad2014-01-16 09:47:17 -05004109 /*
4110 * Some WD SATA-I drives spin up and down erratically when the link
4111 * is put into the slumber mode. We don't have full list of the
4112 * affected devices. Disable LPM if the device matches one of the
4113 * known prefixes and is SATA-1. As a side effect LPM partial is
4114 * lost too.
4115 *
4116 * https://bugzilla.kernel.org/show_bug.cgi?id=57211
4117 */
4118 { "WDC WD800JD-*", NULL, ATA_HORKAGE_WD_BROKEN_LPM },
4119 { "WDC WD1200JD-*", NULL, ATA_HORKAGE_WD_BROKEN_LPM },
4120 { "WDC WD1600JD-*", NULL, ATA_HORKAGE_WD_BROKEN_LPM },
4121 { "WDC WD2000JD-*", NULL, ATA_HORKAGE_WD_BROKEN_LPM },
4122 { "WDC WD2500JD-*", NULL, ATA_HORKAGE_WD_BROKEN_LPM },
4123 { "WDC WD3000JD-*", NULL, ATA_HORKAGE_WD_BROKEN_LPM },
4124 { "WDC WD3200JD-*", NULL, ATA_HORKAGE_WD_BROKEN_LPM },
4125
Alan Cox6919a0a2006-10-27 19:08:46 -07004126 /* End Marker */
4127 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004128};
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05004129
Tejun Heo75683fe2007-07-05 13:31:27 +09004130static unsigned long ata_dev_blacklisted(const struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004131{
Tejun Heo8bfa79f2007-01-02 20:19:40 +09004132 unsigned char model_num[ATA_ID_PROD_LEN + 1];
4133 unsigned char model_rev[ATA_ID_FW_REV_LEN + 1];
Alan Cox6919a0a2006-10-27 19:08:46 -07004134 const struct ata_blacklist_entry *ad = ata_device_blacklist;
Albert Lee3a778272006-06-22 13:00:25 +08004135
Tejun Heo8bfa79f2007-01-02 20:19:40 +09004136 ata_id_c_string(dev->id, model_num, ATA_ID_PROD, sizeof(model_num));
4137 ata_id_c_string(dev->id, model_rev, ATA_ID_FW_REV, sizeof(model_rev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004138
Alan Cox6919a0a2006-10-27 19:08:46 -07004139 while (ad->model_num) {
George Spelvin1c402792014-10-07 07:26:38 -04004140 if (glob_match(ad->model_num, model_num)) {
Alan Cox6919a0a2006-10-27 19:08:46 -07004141 if (ad->model_rev == NULL)
4142 return ad->horkage;
George Spelvin1c402792014-10-07 07:26:38 -04004143 if (glob_match(ad->model_rev, model_rev))
Alan Cox6919a0a2006-10-27 19:08:46 -07004144 return ad->horkage;
Alan Coxf4b15fe2006-03-22 15:54:04 +00004145 }
Alan Cox6919a0a2006-10-27 19:08:46 -07004146 ad++;
Alan Coxf4b15fe2006-03-22 15:54:04 +00004147 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004148 return 0;
4149}
4150
Alan Cox6919a0a2006-10-27 19:08:46 -07004151static int ata_dma_blacklisted(const struct ata_device *dev)
4152{
4153 /* We don't support polling DMA.
4154 * DMA blacklist those ATAPI devices with CDB-intr (and use PIO)
4155 * if the LLDD handles only interrupts in the HSM_ST_LAST state.
4156 */
Tejun Heo9af5c9c2007-08-06 18:36:22 +09004157 if ((dev->link->ap->flags & ATA_FLAG_PIO_POLLING) &&
Alan Cox6919a0a2006-10-27 19:08:46 -07004158 (dev->flags & ATA_DFLAG_CDB_INTR))
4159 return 1;
Tejun Heo75683fe2007-07-05 13:31:27 +09004160 return (dev->horkage & ATA_HORKAGE_NODMA) ? 1 : 0;
Alan Cox6919a0a2006-10-27 19:08:46 -07004161}
4162
Tejun Heoa6d5a512006-03-06 04:31:57 +09004163/**
Alan Cox6bbfd532007-11-05 22:58:58 +00004164 * ata_is_40wire - check drive side detection
4165 * @dev: device
4166 *
4167 * Perform drive side detection decoding, allowing for device vendors
4168 * who can't follow the documentation.
4169 */
4170
4171static int ata_is_40wire(struct ata_device *dev)
4172{
4173 if (dev->horkage & ATA_HORKAGE_IVB)
4174 return ata_drive_40wire_relaxed(dev->id);
4175 return ata_drive_40wire(dev->id);
4176}
4177
4178/**
Alan Cox15a55512008-03-28 14:33:46 -07004179 * cable_is_40wire - 40/80/SATA decider
4180 * @ap: port to consider
4181 *
4182 * This function encapsulates the policy for speed management
4183 * in one place. At the moment we don't cache the result but
4184 * there is a good case for setting ap->cbl to the result when
4185 * we are called with unknown cables (and figuring out if it
4186 * impacts hotplug at all).
4187 *
4188 * Return 1 if the cable appears to be 40 wire.
4189 */
4190
4191static int cable_is_40wire(struct ata_port *ap)
4192{
4193 struct ata_link *link;
4194 struct ata_device *dev;
4195
Tejun Heo4a9c7b32008-10-27 19:59:23 +09004196 /* If the controller thinks we are 40 wire, we are. */
Alan Cox15a55512008-03-28 14:33:46 -07004197 if (ap->cbl == ATA_CBL_PATA40)
4198 return 1;
Tejun Heo4a9c7b32008-10-27 19:59:23 +09004199
4200 /* If the controller thinks we are 80 wire, we are. */
Alan Cox15a55512008-03-28 14:33:46 -07004201 if (ap->cbl == ATA_CBL_PATA80 || ap->cbl == ATA_CBL_SATA)
4202 return 0;
Tejun Heo4a9c7b32008-10-27 19:59:23 +09004203
4204 /* If the system is known to be 40 wire short cable (eg
4205 * laptop), then we allow 80 wire modes even if the drive
4206 * isn't sure.
4207 */
Alan Coxf7920682008-04-08 16:37:50 +01004208 if (ap->cbl == ATA_CBL_PATA40_SHORT)
4209 return 0;
Alan Cox15a55512008-03-28 14:33:46 -07004210
Tejun Heo4a9c7b32008-10-27 19:59:23 +09004211 /* If the controller doesn't know, we scan.
4212 *
4213 * Note: We look for all 40 wire detects at this point. Any
4214 * 80 wire detect is taken to be 80 wire cable because
4215 * - in many setups only the one drive (slave if present) will
4216 * give a valid detect
4217 * - if you have a non detect capable drive you don't want it
4218 * to colour the choice
4219 */
Tejun Heo1eca4362008-11-03 20:03:17 +09004220 ata_for_each_link(link, ap, EDGE) {
4221 ata_for_each_dev(dev, link, ENABLED) {
4222 if (!ata_is_40wire(dev))
Alan Cox15a55512008-03-28 14:33:46 -07004223 return 0;
4224 }
4225 }
4226 return 1;
4227}
4228
4229/**
Tejun Heoa6d5a512006-03-06 04:31:57 +09004230 * ata_dev_xfermask - Compute supported xfermask of the given device
Tejun Heoa6d5a512006-03-06 04:31:57 +09004231 * @dev: Device to compute xfermask for
4232 *
Tejun Heoacf356b2006-03-24 14:07:50 +09004233 * Compute supported xfermask of @dev and store it in
4234 * dev->*_mask. This function is responsible for applying all
4235 * known limits including host controller limits, device
4236 * blacklist, etc...
Tejun Heoa6d5a512006-03-06 04:31:57 +09004237 *
4238 * LOCKING:
4239 * None.
Tejun Heoa6d5a512006-03-06 04:31:57 +09004240 */
Tejun Heo3373efd2006-05-15 20:57:53 +09004241static void ata_dev_xfermask(struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004242{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09004243 struct ata_link *link = dev->link;
4244 struct ata_port *ap = link->ap;
Jeff Garzikcca39742006-08-24 03:19:22 -04004245 struct ata_host *host = ap->host;
Tejun Heoa6d5a512006-03-06 04:31:57 +09004246 unsigned long xfer_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004247
Tejun Heo37deecb2006-08-10 16:59:07 +09004248 /* controller modes available */
Tejun Heo565083e2006-04-02 17:54:47 +09004249 xfer_mask = ata_pack_xfermask(ap->pio_mask,
4250 ap->mwdma_mask, ap->udma_mask);
4251
Robert Hancock8343f882007-03-06 02:37:51 -08004252 /* drive modes available */
Tejun Heo37deecb2006-08-10 16:59:07 +09004253 xfer_mask &= ata_pack_xfermask(dev->pio_mask,
4254 dev->mwdma_mask, dev->udma_mask);
4255 xfer_mask &= ata_id_xfermask(dev->id);
Tejun Heo565083e2006-04-02 17:54:47 +09004256
Alan Coxb352e572006-08-10 18:52:12 +01004257 /*
4258 * CFA Advanced TrueIDE timings are not allowed on a shared
4259 * cable
4260 */
4261 if (ata_dev_pair(dev)) {
4262 /* No PIO5 or PIO6 */
4263 xfer_mask &= ~(0x03 << (ATA_SHIFT_PIO + 5));
4264 /* No MWDMA3 or MWDMA 4 */
4265 xfer_mask &= ~(0x03 << (ATA_SHIFT_MWDMA + 3));
4266 }
4267
Tejun Heo37deecb2006-08-10 16:59:07 +09004268 if (ata_dma_blacklisted(dev)) {
4269 xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
Joe Perchesa9a79df2011-04-15 15:51:59 -07004270 ata_dev_warn(dev,
4271 "device is on DMA blacklist, disabling DMA\n");
Tejun Heo37deecb2006-08-10 16:59:07 +09004272 }
Tejun Heoa6d5a512006-03-06 04:31:57 +09004273
Petr Vandrovec14d66ab2007-03-08 10:12:12 +01004274 if ((host->flags & ATA_HOST_SIMPLEX) &&
Jeff Garzik2dcb4072007-10-19 06:42:56 -04004275 host->simplex_claimed && host->simplex_claimed != ap) {
Tejun Heo37deecb2006-08-10 16:59:07 +09004276 xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
Joe Perchesa9a79df2011-04-15 15:51:59 -07004277 ata_dev_warn(dev,
4278 "simplex DMA is claimed by other device, disabling DMA\n");
Alan Cox5444a6f2006-03-27 18:58:20 +01004279 }
Tejun Heo565083e2006-04-02 17:54:47 +09004280
Jeff Garzike4246752007-03-09 09:56:46 -05004281 if (ap->flags & ATA_FLAG_NO_IORDY)
4282 xfer_mask &= ata_pio_mask_no_iordy(dev);
4283
Alan Cox5444a6f2006-03-27 18:58:20 +01004284 if (ap->ops->mode_filter)
Alan Coxa76b62ca2007-03-09 09:34:07 -05004285 xfer_mask = ap->ops->mode_filter(dev, xfer_mask);
Alan Cox5444a6f2006-03-27 18:58:20 +01004286
Robert Hancock8343f882007-03-06 02:37:51 -08004287 /* Apply cable rule here. Don't apply it early because when
4288 * we handle hot plug the cable type can itself change.
4289 * Check this last so that we know if the transfer rate was
4290 * solely limited by the cable.
4291 * Unknown or 80 wire cables reported host side are checked
4292 * drive side as well. Cases where we know a 40wire cable
4293 * is used safely for 80 are not checked here.
4294 */
4295 if (xfer_mask & (0xF8 << ATA_SHIFT_UDMA))
4296 /* UDMA/44 or higher would be available */
Alan Cox15a55512008-03-28 14:33:46 -07004297 if (cable_is_40wire(ap)) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07004298 ata_dev_warn(dev,
4299 "limited to UDMA/33 due to 40-wire cable\n");
Robert Hancock8343f882007-03-06 02:37:51 -08004300 xfer_mask &= ~(0xF8 << ATA_SHIFT_UDMA);
4301 }
4302
Tejun Heo565083e2006-04-02 17:54:47 +09004303 ata_unpack_xfermask(xfer_mask, &dev->pio_mask,
4304 &dev->mwdma_mask, &dev->udma_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004305}
4306
Linus Torvalds1da177e2005-04-16 15:20:36 -07004307/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004308 * ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
Linus Torvalds1da177e2005-04-16 15:20:36 -07004309 * @dev: Device to which command will be sent
4310 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04004311 * Issue SET FEATURES - XFER MODE command to device @dev
4312 * on port @ap.
4313 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004314 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004315 * PCI/etc. bus probe sem.
Tejun Heo83206a22006-03-24 15:25:31 +09004316 *
4317 * RETURNS:
4318 * 0 on success, AC_ERR_* mask otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004319 */
4320
Tejun Heo3373efd2006-05-15 20:57:53 +09004321static unsigned int ata_dev_set_xfermode(struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004322{
Tejun Heoa0123702005-12-13 14:49:31 +09004323 struct ata_taskfile tf;
Tejun Heo83206a22006-03-24 15:25:31 +09004324 unsigned int err_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004325
4326 /* set up set-features taskfile */
4327 DPRINTK("set features - xfer mode\n");
4328
Tejun Heo464cf172007-05-27 15:10:40 +02004329 /* Some controllers and ATAPI devices show flaky interrupt
4330 * behavior after setting xfer mode. Use polling instead.
4331 */
Tejun Heo3373efd2006-05-15 20:57:53 +09004332 ata_tf_init(dev, &tf);
Tejun Heoa0123702005-12-13 14:49:31 +09004333 tf.command = ATA_CMD_SET_FEATURES;
4334 tf.feature = SETFEATURES_XFER;
Tejun Heo464cf172007-05-27 15:10:40 +02004335 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE | ATA_TFLAG_POLLING;
Tejun Heoa0123702005-12-13 14:49:31 +09004336 tf.protocol = ATA_PROT_NODATA;
Alan Coxb9f8ab22007-11-19 14:33:11 +00004337 /* If we are using IORDY we must send the mode setting command */
Jeff Garzik11b7bec2007-11-23 21:12:14 -05004338 if (ata_pio_need_iordy(dev))
4339 tf.nsect = dev->xfer_mode;
Alan Coxb9f8ab22007-11-19 14:33:11 +00004340 /* If the device has IORDY and the controller does not - turn it off */
4341 else if (ata_id_has_iordy(dev->id))
Jeff Garzik11b7bec2007-11-23 21:12:14 -05004342 tf.nsect = 0x01;
Alan Coxb9f8ab22007-11-19 14:33:11 +00004343 else /* In the ancient relic department - skip all of this */
4344 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004345
Mikulas Patockad531be22015-07-08 13:06:12 -04004346 /* On some disks, this command causes spin-up, so we need longer timeout */
4347 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 15000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004348
Tejun Heo83206a22006-03-24 15:25:31 +09004349 DPRINTK("EXIT, err_mask=%x\n", err_mask);
4350 return err_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004351}
Tejun Heo1152b262010-09-01 17:50:05 +02004352
Linus Torvalds1da177e2005-04-16 15:20:36 -07004353/**
Jeff Garzik218f3d32007-10-25 00:33:27 -04004354 * ata_dev_set_feature - Issue SET FEATURES - SATA FEATURES
Kristen Carlson Accardi9f45cbd2007-08-15 03:57:11 -04004355 * @dev: Device to which command will be sent
4356 * @enable: Whether to enable or disable the feature
Jeff Garzik218f3d32007-10-25 00:33:27 -04004357 * @feature: The sector count represents the feature to set
Kristen Carlson Accardi9f45cbd2007-08-15 03:57:11 -04004358 *
4359 * Issue SET FEATURES - SATA FEATURES command to device @dev
Jeff Garzik218f3d32007-10-25 00:33:27 -04004360 * on port @ap with sector count
Kristen Carlson Accardi9f45cbd2007-08-15 03:57:11 -04004361 *
4362 * LOCKING:
4363 * PCI/etc. bus probe sem.
4364 *
4365 * RETURNS:
4366 * 0 on success, AC_ERR_* mask otherwise.
4367 */
Tejun Heo1152b262010-09-01 17:50:05 +02004368unsigned int ata_dev_set_feature(struct ata_device *dev, u8 enable, u8 feature)
Kristen Carlson Accardi9f45cbd2007-08-15 03:57:11 -04004369{
4370 struct ata_taskfile tf;
4371 unsigned int err_mask;
Damien Le Moal974e0a42016-04-04 12:17:09 -04004372 unsigned long timeout = 0;
Kristen Carlson Accardi9f45cbd2007-08-15 03:57:11 -04004373
4374 /* set up set-features taskfile */
4375 DPRINTK("set features - SATA features\n");
4376
4377 ata_tf_init(dev, &tf);
4378 tf.command = ATA_CMD_SET_FEATURES;
4379 tf.feature = enable;
4380 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
4381 tf.protocol = ATA_PROT_NODATA;
Jeff Garzik218f3d32007-10-25 00:33:27 -04004382 tf.nsect = feature;
Kristen Carlson Accardi9f45cbd2007-08-15 03:57:11 -04004383
Damien Le Moal974e0a42016-04-04 12:17:09 -04004384 if (enable == SETFEATURES_SPINUP)
4385 timeout = ata_probe_timeout ?
4386 ata_probe_timeout * 1000 : SETFEATURES_SPINUP_TIMEOUT;
4387 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, timeout);
Kristen Carlson Accardi9f45cbd2007-08-15 03:57:11 -04004388
4389 DPRINTK("EXIT, err_mask=%x\n", err_mask);
4390 return err_mask;
4391}
Jeff Garzik633de4c2012-09-13 01:12:29 -04004392EXPORT_SYMBOL_GPL(ata_dev_set_feature);
Kristen Carlson Accardi9f45cbd2007-08-15 03:57:11 -04004393
4394/**
Albert Lee8bf62ece2005-05-12 15:29:42 -04004395 * ata_dev_init_params - Issue INIT DEV PARAMS command
Albert Lee8bf62ece2005-05-12 15:29:42 -04004396 * @dev: Device to which command will be sent
Randy Dunlape2a7f772006-05-18 10:50:18 -07004397 * @heads: Number of heads (taskfile parameter)
4398 * @sectors: Number of sectors (taskfile parameter)
Albert Lee8bf62ece2005-05-12 15:29:42 -04004399 *
4400 * LOCKING:
Tejun Heo6aff8f12006-02-15 18:24:09 +09004401 * Kernel thread context (may sleep)
4402 *
4403 * RETURNS:
4404 * 0 on success, AC_ERR_* mask otherwise.
Albert Lee8bf62ece2005-05-12 15:29:42 -04004405 */
Tejun Heo3373efd2006-05-15 20:57:53 +09004406static unsigned int ata_dev_init_params(struct ata_device *dev,
4407 u16 heads, u16 sectors)
Albert Lee8bf62ece2005-05-12 15:29:42 -04004408{
Tejun Heoa0123702005-12-13 14:49:31 +09004409 struct ata_taskfile tf;
Tejun Heo6aff8f12006-02-15 18:24:09 +09004410 unsigned int err_mask;
Albert Lee8bf62ece2005-05-12 15:29:42 -04004411
4412 /* Number of sectors per track 1-255. Number of heads 1-16 */
4413 if (sectors < 1 || sectors > 255 || heads < 1 || heads > 16)
Albert Lee00b6f5e2006-03-27 16:39:18 +08004414 return AC_ERR_INVALID;
Albert Lee8bf62ece2005-05-12 15:29:42 -04004415
4416 /* set up init dev params taskfile */
4417 DPRINTK("init dev params \n");
4418
Tejun Heo3373efd2006-05-15 20:57:53 +09004419 ata_tf_init(dev, &tf);
Tejun Heoa0123702005-12-13 14:49:31 +09004420 tf.command = ATA_CMD_INIT_DEV_PARAMS;
4421 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
4422 tf.protocol = ATA_PROT_NODATA;
4423 tf.nsect = sectors;
4424 tf.device |= (heads - 1) & 0x0f; /* max head = num. of heads - 1 */
Albert Lee8bf62ece2005-05-12 15:29:42 -04004425
Tejun Heo2b789102007-10-09 15:05:44 +09004426 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
Alan Cox18b24662007-08-08 14:28:49 +01004427 /* A clean abort indicates an original or just out of spec drive
4428 and we should continue as we issue the setup based on the
4429 drive reported working geometry */
4430 if (err_mask == AC_ERR_DEV && (tf.feature & ATA_ABORTED))
4431 err_mask = 0;
Albert Lee8bf62ece2005-05-12 15:29:42 -04004432
Tejun Heo6aff8f12006-02-15 18:24:09 +09004433 DPRINTK("EXIT, err_mask=%x\n", err_mask);
4434 return err_mask;
Albert Lee8bf62ece2005-05-12 15:29:42 -04004435}
4436
4437/**
Tejun Heo5895ef92008-06-17 12:36:26 +09004438 * atapi_check_dma - Check whether ATAPI DMA can be supported
Linus Torvalds1da177e2005-04-16 15:20:36 -07004439 * @qc: Metadata associated with taskfile to check
4440 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04004441 * Allow low-level driver to filter ATA PACKET commands, returning
4442 * a status indicating whether or not it is OK to use DMA for the
4443 * supplied PACKET command.
4444 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004445 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04004446 * spin_lock_irqsave(host lock)
Jeff Garzik0cba6322005-05-30 19:49:12 -04004447 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004448 * RETURNS: 0 when ATAPI DMA can be used
4449 * nonzero otherwise
4450 */
Tejun Heo5895ef92008-06-17 12:36:26 +09004451int atapi_check_dma(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004452{
4453 struct ata_port *ap = qc->ap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004454
Tejun Heob9a41972007-06-27 02:48:43 +09004455 /* Don't allow DMA if it isn't multiple of 16 bytes. Quite a
4456 * few ATAPI devices choke on such DMA requests.
4457 */
Tejun Heo6a87e422008-11-03 19:01:09 +09004458 if (!(qc->dev->horkage & ATA_HORKAGE_ATAPI_MOD16_DMA) &&
4459 unlikely(qc->nbytes & 15))
Tejun Heob9a41972007-06-27 02:48:43 +09004460 return 1;
Albert Lee6f23a312007-04-02 11:39:25 +08004461
Linus Torvalds1da177e2005-04-16 15:20:36 -07004462 if (ap->ops->check_atapi_dma)
Tejun Heob9a41972007-06-27 02:48:43 +09004463 return ap->ops->check_atapi_dma(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004464
Tejun Heob9a41972007-06-27 02:48:43 +09004465 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004466}
Tejun Heob9a41972007-06-27 02:48:43 +09004467
Linus Torvalds1da177e2005-04-16 15:20:36 -07004468/**
Tejun Heo31cc23b2007-09-23 13:14:12 +09004469 * ata_std_qc_defer - Check whether a qc needs to be deferred
4470 * @qc: ATA command in question
4471 *
4472 * Non-NCQ commands cannot run with any other command, NCQ or
4473 * not. As upper layer only knows the queue depth, we are
4474 * responsible for maintaining exclusion. This function checks
4475 * whether a new command @qc can be issued.
4476 *
4477 * LOCKING:
4478 * spin_lock_irqsave(host lock)
4479 *
4480 * RETURNS:
4481 * ATA_DEFER_* if deferring is needed, 0 otherwise.
4482 */
4483int ata_std_qc_defer(struct ata_queued_cmd *qc)
4484{
4485 struct ata_link *link = qc->dev->link;
4486
Hannes Reinecke179b3102016-07-14 09:05:43 +09004487 if (ata_is_ncq(qc->tf.protocol)) {
Tejun Heo31cc23b2007-09-23 13:14:12 +09004488 if (!ata_tag_valid(link->active_tag))
4489 return 0;
4490 } else {
4491 if (!ata_tag_valid(link->active_tag) && !link->sactive)
4492 return 0;
4493 }
4494
4495 return ATA_DEFER_LINK;
4496}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01004497EXPORT_SYMBOL_GPL(ata_std_qc_defer);
Tejun Heo31cc23b2007-09-23 13:14:12 +09004498
Jiri Slaby95364f32019-10-31 10:59:45 +01004499enum ata_completion_errors ata_noop_qc_prep(struct ata_queued_cmd *qc)
4500{
4501 return AC_ERR_OK;
4502}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01004503EXPORT_SYMBOL_GPL(ata_noop_qc_prep);
Brian Kinge46834c2006-03-17 17:04:03 -06004504
Jeff Garzik0cba6322005-05-30 19:49:12 -04004505/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04004506 * ata_sg_init - Associate command with scatter-gather table.
4507 * @qc: Command to be associated
4508 * @sg: Scatter-gather table.
4509 * @n_elem: Number of elements in s/g table.
4510 *
4511 * Initialize the data-related elements of queued_cmd @qc
4512 * to point to a scatter-gather table @sg, containing @n_elem
4513 * elements.
4514 *
4515 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04004516 * spin_lock_irqsave(host lock)
Jeff Garzik0cba6322005-05-30 19:49:12 -04004517 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004518void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
4519 unsigned int n_elem)
4520{
Tejun Heoff2aeb12007-12-05 16:43:11 +09004521 qc->sg = sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004522 qc->n_elem = n_elem;
Tejun Heoff2aeb12007-12-05 16:43:11 +09004523 qc->cursg = qc->sg;
4524}
4525
Geert Uytterhoeven2874d5e2017-01-03 19:09:49 +01004526#ifdef CONFIG_HAS_DMA
4527
4528/**
4529 * ata_sg_clean - Unmap DMA memory associated with command
4530 * @qc: Command containing DMA memory to be released
4531 *
4532 * Unmap all mapped DMA memory associated with this command.
4533 *
4534 * LOCKING:
4535 * spin_lock_irqsave(host lock)
4536 */
Jason Yanaf27e012017-03-10 10:05:40 +08004537static void ata_sg_clean(struct ata_queued_cmd *qc)
Geert Uytterhoeven2874d5e2017-01-03 19:09:49 +01004538{
4539 struct ata_port *ap = qc->ap;
4540 struct scatterlist *sg = qc->sg;
4541 int dir = qc->dma_dir;
4542
4543 WARN_ON_ONCE(sg == NULL);
4544
4545 VPRINTK("unmapping %u sg elements\n", qc->n_elem);
4546
4547 if (qc->n_elem)
4548 dma_unmap_sg(ap->dev, sg, qc->orig_n_elem, dir);
4549
4550 qc->flags &= ~ATA_QCFLAG_DMAMAP;
4551 qc->sg = NULL;
4552}
4553
Linus Torvalds1da177e2005-04-16 15:20:36 -07004554/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04004555 * ata_sg_setup - DMA-map the scatter-gather table associated with a command.
4556 * @qc: Command with scatter-gather table to be mapped.
4557 *
4558 * DMA-map the scatter-gather table associated with queued_cmd @qc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004559 *
4560 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04004561 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004562 *
4563 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004564 * Zero on success, negative on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004565 *
4566 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004567static int ata_sg_setup(struct ata_queued_cmd *qc)
4568{
4569 struct ata_port *ap = qc->ap;
James Bottomleydde20202008-02-19 11:36:56 +01004570 unsigned int n_elem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004571
Tejun Heo44877b42007-02-21 01:06:51 +09004572 VPRINTK("ENTER, ata%u\n", ap->print_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004573
James Bottomleydde20202008-02-19 11:36:56 +01004574 n_elem = dma_map_sg(ap->dev, qc->sg, qc->n_elem, qc->dma_dir);
4575 if (n_elem < 1)
4576 return -1;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004577
Paul Menzelbb376672018-07-08 09:18:21 +02004578 VPRINTK("%d sg elements mapped\n", n_elem);
FUJITA Tomonori58256272009-02-27 17:35:43 +09004579 qc->orig_n_elem = qc->n_elem;
James Bottomleydde20202008-02-19 11:36:56 +01004580 qc->n_elem = n_elem;
Tejun Heof92a2632007-12-05 16:43:10 +09004581 qc->flags |= ATA_QCFLAG_DMAMAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004582
4583 return 0;
4584}
4585
Geert Uytterhoeven2874d5e2017-01-03 19:09:49 +01004586#else /* !CONFIG_HAS_DMA */
4587
4588static inline void ata_sg_clean(struct ata_queued_cmd *qc) {}
4589static inline int ata_sg_setup(struct ata_queued_cmd *qc) { return -1; }
4590
4591#endif /* !CONFIG_HAS_DMA */
4592
Linus Torvalds1da177e2005-04-16 15:20:36 -07004593/**
Randy Dunlapc893a3a2006-01-28 13:15:32 -05004594 * swap_buf_le16 - swap halves of 16-bit words in place
Edward Falk0baab862005-06-02 18:17:13 -04004595 * @buf: Buffer to swap
4596 * @buf_words: Number of 16-bit words in buffer.
4597 *
4598 * Swap halves of 16-bit words if needed to convert from
4599 * little-endian byte order to native cpu byte order, or
4600 * vice-versa.
4601 *
4602 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04004603 * Inherited from caller.
Edward Falk0baab862005-06-02 18:17:13 -04004604 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004605void swap_buf_le16(u16 *buf, unsigned int buf_words)
4606{
4607#ifdef __BIG_ENDIAN
4608 unsigned int i;
4609
4610 for (i = 0; i < buf_words; i++)
4611 buf[i] = le16_to_cpu(buf[i]);
4612#endif /* __BIG_ENDIAN */
4613}
4614
Albert Lee6ae4cfb2005-08-12 14:15:34 +08004615/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004616 * ata_qc_new_init - Request an available ATA command, and initialize it
Linus Torvalds1da177e2005-04-16 15:20:36 -07004617 * @dev: Device from whom we request an available command structure
Randy Dunlap38755e82015-08-09 18:37:36 -07004618 * @tag: tag
Linus Torvalds1da177e2005-04-16 15:20:36 -07004619 *
4620 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004621 * None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004622 */
4623
Shaohua Li98bd4be2015-01-23 19:52:07 -08004624struct ata_queued_cmd *ata_qc_new_init(struct ata_device *dev, int tag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004625{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09004626 struct ata_port *ap = dev->link->ap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004627 struct ata_queued_cmd *qc;
4628
Shaohua Li98bd4be2015-01-23 19:52:07 -08004629 /* no command while frozen */
4630 if (unlikely(ap->pflags & ATA_PFLAG_FROZEN))
4631 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004632
Shaohua Li98bd4be2015-01-23 19:52:07 -08004633 /* libsas case */
Shaohua Li5067c042015-03-12 10:32:18 -07004634 if (ap->flags & ATA_FLAG_SAS_HOST) {
Shaohua Li98bd4be2015-01-23 19:52:07 -08004635 tag = ata_sas_allocate_tag(ap);
4636 if (tag < 0)
4637 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004638 }
4639
Shaohua Li98bd4be2015-01-23 19:52:07 -08004640 qc = __ata_qc_from_tag(ap, tag);
Jens Axboe5ac40792018-05-11 12:51:03 -06004641 qc->tag = qc->hw_tag = tag;
Shaohua Li98bd4be2015-01-23 19:52:07 -08004642 qc->scsicmd = NULL;
4643 qc->ap = ap;
4644 qc->dev = dev;
4645
4646 ata_qc_reinit(qc);
4647
Linus Torvalds1da177e2005-04-16 15:20:36 -07004648 return qc;
4649}
4650
Tejun Heo8a8bc222008-11-10 14:48:21 +09004651/**
4652 * ata_qc_free - free unused ata_queued_cmd
4653 * @qc: Command to complete
4654 *
4655 * Designed to free unused ata_queued_cmd object
4656 * in case something prevents using it.
4657 *
4658 * LOCKING:
4659 * spin_lock_irqsave(host lock)
4660 */
4661void ata_qc_free(struct ata_queued_cmd *qc)
4662{
Julia Lawalla1104012009-10-17 08:41:47 +02004663 struct ata_port *ap;
Tejun Heo8a8bc222008-11-10 14:48:21 +09004664 unsigned int tag;
4665
Tejun Heoefcb3cf2009-01-09 19:19:14 +09004666 WARN_ON_ONCE(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
Julia Lawalla1104012009-10-17 08:41:47 +02004667 ap = qc->ap;
Tejun Heo8a8bc222008-11-10 14:48:21 +09004668
4669 qc->flags = 0;
4670 tag = qc->tag;
Jens Axboe28361c42018-05-11 12:51:09 -06004671 if (ata_tag_valid(tag)) {
Tejun Heo8a8bc222008-11-10 14:48:21 +09004672 qc->tag = ATA_TAG_POISON;
Shaohua Li5067c042015-03-12 10:32:18 -07004673 if (ap->flags & ATA_FLAG_SAS_HOST)
Shaohua Li98bd4be2015-01-23 19:52:07 -08004674 ata_sas_free_tag(tag, ap);
Tejun Heo8a8bc222008-11-10 14:48:21 +09004675 }
4676}
4677
Tejun Heo76014422006-02-11 15:13:49 +09004678void __ata_qc_complete(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004679{
Julia Lawalla1104012009-10-17 08:41:47 +02004680 struct ata_port *ap;
4681 struct ata_link *link;
Tejun Heodedaf2b2006-05-15 21:03:43 +09004682
Tejun Heoefcb3cf2009-01-09 19:19:14 +09004683 WARN_ON_ONCE(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
4684 WARN_ON_ONCE(!(qc->flags & ATA_QCFLAG_ACTIVE));
Julia Lawalla1104012009-10-17 08:41:47 +02004685 ap = qc->ap;
4686 link = qc->dev->link;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004687
4688 if (likely(qc->flags & ATA_QCFLAG_DMAMAP))
4689 ata_sg_clean(qc);
4690
Tejun Heo7401abf2006-05-15 20:57:32 +09004691 /* command should be marked inactive atomically with qc completion */
Hannes Reinecke179b3102016-07-14 09:05:43 +09004692 if (ata_is_ncq(qc->tf.protocol)) {
Jens Axboe4e5b6262018-05-11 12:51:04 -06004693 link->sactive &= ~(1 << qc->hw_tag);
Tejun Heoda917d62007-09-23 13:14:12 +09004694 if (!link->sactive)
4695 ap->nr_active_links--;
4696 } else {
Tejun Heo9af5c9c2007-08-06 18:36:22 +09004697 link->active_tag = ATA_TAG_POISON;
Tejun Heoda917d62007-09-23 13:14:12 +09004698 ap->nr_active_links--;
4699 }
4700
4701 /* clear exclusive status */
4702 if (unlikely(qc->flags & ATA_QCFLAG_CLEAR_EXCL &&
4703 ap->excl_link == link))
4704 ap->excl_link = NULL;
Tejun Heo7401abf2006-05-15 20:57:32 +09004705
Albert Lee3f3791d2005-08-16 14:25:38 +08004706 /* atapi: mark qc as inactive to prevent the interrupt handler
4707 * from completing the command twice later, before the error handler
4708 * is called. (when rc != 0 and atapi request sense is needed)
4709 */
4710 qc->flags &= ~ATA_QCFLAG_ACTIVE;
Jens Axboee3ed89392018-05-11 12:51:05 -06004711 ap->qc_active &= ~(1ULL << qc->tag);
Albert Lee3f3791d2005-08-16 14:25:38 +08004712
Linus Torvalds1da177e2005-04-16 15:20:36 -07004713 /* call completion callback */
Tejun Heo77853bf2006-01-23 13:09:36 +09004714 qc->complete_fn(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004715}
4716
Tejun Heo39599a52006-11-14 22:37:35 +09004717static void fill_result_tf(struct ata_queued_cmd *qc)
4718{
4719 struct ata_port *ap = qc->ap;
4720
Tejun Heo39599a52006-11-14 22:37:35 +09004721 qc->result_tf.flags = qc->tf.flags;
Tejun Heo22183bf2008-04-07 22:47:20 +09004722 ap->ops->qc_fill_rtf(qc);
Tejun Heo39599a52006-11-14 22:37:35 +09004723}
4724
Tejun Heo00115e02007-11-27 19:28:58 +09004725static void ata_verify_xfer(struct ata_queued_cmd *qc)
4726{
4727 struct ata_device *dev = qc->dev;
4728
Christoph Hellwigeb0effd2016-07-16 22:16:41 +09004729 if (!ata_is_data(qc->tf.protocol))
Tejun Heo00115e02007-11-27 19:28:58 +09004730 return;
4731
4732 if ((dev->mwdma_mask || dev->udma_mask) && ata_is_pio(qc->tf.protocol))
4733 return;
4734
4735 dev->flags &= ~ATA_DFLAG_DUBIOUS_XFER;
4736}
4737
Tejun Heof686bcb2006-05-15 20:58:05 +09004738/**
4739 * ata_qc_complete - Complete an active ATA command
4740 * @qc: Command to complete
Tejun Heof686bcb2006-05-15 20:58:05 +09004741 *
Tejun Heo1aadf5c2010-06-25 15:03:34 +02004742 * Indicate to the mid and upper layers that an ATA command has
4743 * completed, with either an ok or not-ok status.
4744 *
4745 * Refrain from calling this function multiple times when
4746 * successfully completing multiple NCQ commands.
4747 * ata_qc_complete_multiple() should be used instead, which will
4748 * properly update IRQ expect state.
Tejun Heof686bcb2006-05-15 20:58:05 +09004749 *
4750 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04004751 * spin_lock_irqsave(host lock)
Tejun Heof686bcb2006-05-15 20:58:05 +09004752 */
4753void ata_qc_complete(struct ata_queued_cmd *qc)
4754{
4755 struct ata_port *ap = qc->ap;
4756
Stephan Linzeb25cb92016-06-10 07:59:56 +02004757 /* Trigger the LED (if available) */
Linus Walleijd1ed7c52018-02-24 23:45:56 +01004758 ledtrig_disk_activity(!!(qc->tf.flags & ATA_TFLAG_WRITE));
Stephan Linzeb25cb92016-06-10 07:59:56 +02004759
Tejun Heof686bcb2006-05-15 20:58:05 +09004760 /* XXX: New EH and old EH use different mechanisms to
4761 * synchronize EH with regular execution path.
4762 *
4763 * In new EH, a failed qc is marked with ATA_QCFLAG_FAILED.
4764 * Normal execution path is responsible for not accessing a
4765 * failed qc. libata core enforces the rule by returning NULL
4766 * from ata_qc_from_tag() for failed qcs.
4767 *
4768 * Old EH depends on ata_qc_complete() nullifying completion
4769 * requests if ATA_QCFLAG_EH_SCHEDULED is set. Old EH does
4770 * not synchronize with interrupt handler. Only PIO task is
4771 * taken care of.
4772 */
4773 if (ap->ops->error_handler) {
Tejun Heo4dbfa392007-10-25 18:22:44 +09004774 struct ata_device *dev = qc->dev;
4775 struct ata_eh_info *ehi = &dev->link->eh_info;
4776
Tejun Heof686bcb2006-05-15 20:58:05 +09004777 if (unlikely(qc->err_mask))
4778 qc->flags |= ATA_QCFLAG_FAILED;
4779
Tejun Heof08dc1a2010-12-09 15:59:32 +01004780 /*
4781 * Finish internal commands without any further processing
4782 * and always with the result TF filled.
4783 */
4784 if (unlikely(ata_tag_internal(qc->tag))) {
Tejun Heof4b31db92009-10-16 13:00:51 +09004785 fill_result_tf(qc);
Hannes Reinecke255c03d2015-03-27 16:46:38 +01004786 trace_ata_qc_complete_internal(qc);
Tejun Heof08dc1a2010-12-09 15:59:32 +01004787 __ata_qc_complete(qc);
4788 return;
4789 }
Tejun Heof4b31db92009-10-16 13:00:51 +09004790
Tejun Heof08dc1a2010-12-09 15:59:32 +01004791 /*
4792 * Non-internal qc has failed. Fill the result TF and
4793 * summon EH.
4794 */
4795 if (unlikely(qc->flags & ATA_QCFLAG_FAILED)) {
4796 fill_result_tf(qc);
Hannes Reinecke255c03d2015-03-27 16:46:38 +01004797 trace_ata_qc_complete_failed(qc);
Tejun Heof08dc1a2010-12-09 15:59:32 +01004798 ata_qc_schedule_eh(qc);
Tejun Heof4b31db92009-10-16 13:00:51 +09004799 return;
Tejun Heof686bcb2006-05-15 20:58:05 +09004800 }
4801
Tejun Heo4dc738e2009-09-17 18:45:27 +09004802 WARN_ON_ONCE(ap->pflags & ATA_PFLAG_FROZEN);
4803
Tejun Heof686bcb2006-05-15 20:58:05 +09004804 /* read result TF if requested */
4805 if (qc->flags & ATA_QCFLAG_RESULT_TF)
Tejun Heo39599a52006-11-14 22:37:35 +09004806 fill_result_tf(qc);
Tejun Heof686bcb2006-05-15 20:58:05 +09004807
Hannes Reinecke255c03d2015-03-27 16:46:38 +01004808 trace_ata_qc_complete_done(qc);
Tejun Heo4dbfa392007-10-25 18:22:44 +09004809 /* Some commands need post-processing after successful
4810 * completion.
4811 */
4812 switch (qc->tf.command) {
4813 case ATA_CMD_SET_FEATURES:
4814 if (qc->tf.feature != SETFEATURES_WC_ON &&
Tom Yan0c127352016-07-12 21:37:02 +08004815 qc->tf.feature != SETFEATURES_WC_OFF &&
4816 qc->tf.feature != SETFEATURES_RA_ON &&
4817 qc->tf.feature != SETFEATURES_RA_OFF)
Tejun Heo4dbfa392007-10-25 18:22:44 +09004818 break;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05004819 fallthrough;
Tejun Heo4dbfa392007-10-25 18:22:44 +09004820 case ATA_CMD_INIT_DEV_PARAMS: /* CHS translation changed */
4821 case ATA_CMD_SET_MULTI: /* multi_count changed */
4822 /* revalidate device */
4823 ehi->dev_action[dev->devno] |= ATA_EH_REVALIDATE;
4824 ata_port_schedule_eh(ap);
4825 break;
Tejun Heo054a5fb2007-10-25 18:30:36 +09004826
4827 case ATA_CMD_SLEEP:
4828 dev->flags |= ATA_DFLAG_SLEEPING;
4829 break;
Tejun Heo4dbfa392007-10-25 18:22:44 +09004830 }
4831
Tejun Heo00115e02007-11-27 19:28:58 +09004832 if (unlikely(dev->flags & ATA_DFLAG_DUBIOUS_XFER))
4833 ata_verify_xfer(qc);
4834
Tejun Heof686bcb2006-05-15 20:58:05 +09004835 __ata_qc_complete(qc);
4836 } else {
4837 if (qc->flags & ATA_QCFLAG_EH_SCHEDULED)
4838 return;
4839
4840 /* read result TF if failed or requested */
4841 if (qc->err_mask || qc->flags & ATA_QCFLAG_RESULT_TF)
Tejun Heo39599a52006-11-14 22:37:35 +09004842 fill_result_tf(qc);
Tejun Heof686bcb2006-05-15 20:58:05 +09004843
4844 __ata_qc_complete(qc);
4845 }
4846}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01004847EXPORT_SYMBOL_GPL(ata_qc_complete);
Tejun Heof686bcb2006-05-15 20:58:05 +09004848
Tejun Heodedaf2b2006-05-15 21:03:43 +09004849/**
Sascha Hauer8385d752019-12-13 09:04:08 +01004850 * ata_qc_get_active - get bitmask of active qcs
4851 * @ap: port in question
4852 *
4853 * LOCKING:
4854 * spin_lock_irqsave(host lock)
4855 *
4856 * RETURNS:
4857 * Bitmask of active qcs
4858 */
4859u64 ata_qc_get_active(struct ata_port *ap)
4860{
4861 u64 qc_active = ap->qc_active;
4862
4863 /* ATA_TAG_INTERNAL is sent to hw as tag 0 */
4864 if (qc_active & (1ULL << ATA_TAG_INTERNAL)) {
4865 qc_active |= (1 << 0);
4866 qc_active &= ~(1ULL << ATA_TAG_INTERNAL);
4867 }
4868
4869 return qc_active;
4870}
4871EXPORT_SYMBOL_GPL(ata_qc_get_active);
4872
4873/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004874 * ata_qc_issue - issue taskfile to device
4875 * @qc: command to issue to device
4876 *
4877 * Prepare an ATA command to submission to device.
4878 * This includes mapping the data into a DMA-able
4879 * area, filling in the S/G table, and finally
4880 * writing the taskfile to hardware, starting the command.
4881 *
4882 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04004883 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004884 */
Tejun Heo8e0e6942006-03-31 20:41:11 +09004885void ata_qc_issue(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004886{
4887 struct ata_port *ap = qc->ap;
Tejun Heo9af5c9c2007-08-06 18:36:22 +09004888 struct ata_link *link = qc->dev->link;
Tejun Heo405e66b2007-11-27 19:28:53 +09004889 u8 prot = qc->tf.protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004890
Tejun Heodedaf2b2006-05-15 21:03:43 +09004891 /* Make sure only one non-NCQ command is outstanding. The
4892 * check is skipped for old EH because it reuses active qc to
4893 * request ATAPI sense.
4894 */
Tejun Heoefcb3cf2009-01-09 19:19:14 +09004895 WARN_ON_ONCE(ap->ops->error_handler && ata_tag_valid(link->active_tag));
Tejun Heodedaf2b2006-05-15 21:03:43 +09004896
Tejun Heo1973a022007-12-05 10:36:13 +09004897 if (ata_is_ncq(prot)) {
Jens Axboe4e5b6262018-05-11 12:51:04 -06004898 WARN_ON_ONCE(link->sactive & (1 << qc->hw_tag));
Tejun Heoda917d62007-09-23 13:14:12 +09004899
4900 if (!link->sactive)
4901 ap->nr_active_links++;
Jens Axboe4e5b6262018-05-11 12:51:04 -06004902 link->sactive |= 1 << qc->hw_tag;
Tejun Heodedaf2b2006-05-15 21:03:43 +09004903 } else {
Tejun Heoefcb3cf2009-01-09 19:19:14 +09004904 WARN_ON_ONCE(link->sactive);
Tejun Heoda917d62007-09-23 13:14:12 +09004905
4906 ap->nr_active_links++;
Tejun Heo9af5c9c2007-08-06 18:36:22 +09004907 link->active_tag = qc->tag;
Tejun Heodedaf2b2006-05-15 21:03:43 +09004908 }
4909
Tejun Heoe4a70e72006-03-31 20:36:47 +09004910 qc->flags |= ATA_QCFLAG_ACTIVE;
Jens Axboee3ed89392018-05-11 12:51:05 -06004911 ap->qc_active |= 1ULL << qc->tag;
Tejun Heoe4a70e72006-03-31 20:36:47 +09004912
Tejun Heo60f5d6e2010-08-23 11:27:27 +02004913 /*
4914 * We guarantee to LLDs that they will have at least one
Tejun Heof92a2632007-12-05 16:43:10 +09004915 * non-zero sg if the command is a data command.
4916 */
Eric Biggers9173e5e2018-02-03 20:33:27 -08004917 if (ata_is_data(prot) && (!qc->sg || !qc->n_elem || !qc->nbytes))
Tejun Heo60f5d6e2010-08-23 11:27:27 +02004918 goto sys_err;
Tejun Heof92a2632007-12-05 16:43:10 +09004919
Tejun Heo405e66b2007-11-27 19:28:53 +09004920 if (ata_is_dma(prot) || (ata_is_pio(prot) &&
Tejun Heof92a2632007-12-05 16:43:10 +09004921 (ap->flags & ATA_FLAG_PIO_DMA)))
Tejun Heo001102d2007-12-05 16:43:09 +09004922 if (ata_sg_setup(qc))
Tejun Heo60f5d6e2010-08-23 11:27:27 +02004923 goto sys_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004924
Tejun Heocf480622008-01-24 00:05:14 +09004925 /* if device is sleeping, schedule reset and abort the link */
Tejun Heo054a5fb2007-10-25 18:30:36 +09004926 if (unlikely(qc->dev->flags & ATA_DFLAG_SLEEPING)) {
Tejun Heocf480622008-01-24 00:05:14 +09004927 link->eh_info.action |= ATA_EH_RESET;
Tejun Heo054a5fb2007-10-25 18:30:36 +09004928 ata_ehi_push_desc(&link->eh_info, "waking up from sleep");
4929 ata_link_abort(link);
4930 return;
4931 }
4932
Jiri Slaby95364f32019-10-31 10:59:45 +01004933 qc->err_mask |= ap->ops->qc_prep(qc);
4934 if (unlikely(qc->err_mask))
4935 goto err;
Hannes Reinecke255c03d2015-03-27 16:46:38 +01004936 trace_ata_qc_issue(qc);
Tejun Heo8e0e6942006-03-31 20:41:11 +09004937 qc->err_mask |= ap->ops->qc_issue(qc);
4938 if (unlikely(qc->err_mask))
4939 goto err;
4940 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004941
Tejun Heo60f5d6e2010-08-23 11:27:27 +02004942sys_err:
Tejun Heo8e0e6942006-03-31 20:41:11 +09004943 qc->err_mask |= AC_ERR_SYSTEM;
4944err:
4945 ata_qc_complete(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004946}
4947
4948/**
Tejun Heob1c72912008-07-31 17:02:43 +09004949 * ata_phys_link_online - test whether the given link is online
Tejun Heo936fd732007-08-06 18:36:23 +09004950 * @link: ATA link to test
Tejun Heo34bf2172006-05-15 20:57:46 +09004951 *
Tejun Heo936fd732007-08-06 18:36:23 +09004952 * Test whether @link is online. Note that this function returns
4953 * 0 if online status of @link cannot be obtained, so
4954 * ata_link_online(link) != !ata_link_offline(link).
Tejun Heo34bf2172006-05-15 20:57:46 +09004955 *
4956 * LOCKING:
4957 * None.
4958 *
4959 * RETURNS:
Tejun Heob5b3fa32008-07-31 17:02:42 +09004960 * True if the port online status is available and online.
Tejun Heo34bf2172006-05-15 20:57:46 +09004961 */
Tejun Heob1c72912008-07-31 17:02:43 +09004962bool ata_phys_link_online(struct ata_link *link)
Tejun Heo34bf2172006-05-15 20:57:46 +09004963{
4964 u32 sstatus;
4965
Tejun Heo936fd732007-08-06 18:36:23 +09004966 if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0 &&
Tejun Heo9913ff8a2009-01-29 20:31:31 +09004967 ata_sstatus_online(sstatus))
Tejun Heob5b3fa32008-07-31 17:02:42 +09004968 return true;
4969 return false;
Tejun Heo34bf2172006-05-15 20:57:46 +09004970}
4971
4972/**
Tejun Heob1c72912008-07-31 17:02:43 +09004973 * ata_phys_link_offline - test whether the given link is offline
Tejun Heo936fd732007-08-06 18:36:23 +09004974 * @link: ATA link to test
Tejun Heo34bf2172006-05-15 20:57:46 +09004975 *
Tejun Heo936fd732007-08-06 18:36:23 +09004976 * Test whether @link is offline. Note that this function
4977 * returns 0 if offline status of @link cannot be obtained, so
4978 * ata_link_online(link) != !ata_link_offline(link).
Tejun Heo34bf2172006-05-15 20:57:46 +09004979 *
4980 * LOCKING:
4981 * None.
4982 *
4983 * RETURNS:
Tejun Heob5b3fa32008-07-31 17:02:42 +09004984 * True if the port offline status is available and offline.
Tejun Heo34bf2172006-05-15 20:57:46 +09004985 */
Tejun Heob1c72912008-07-31 17:02:43 +09004986bool ata_phys_link_offline(struct ata_link *link)
Tejun Heo34bf2172006-05-15 20:57:46 +09004987{
4988 u32 sstatus;
4989
Tejun Heo936fd732007-08-06 18:36:23 +09004990 if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0 &&
Tejun Heo9913ff8a2009-01-29 20:31:31 +09004991 !ata_sstatus_online(sstatus))
Tejun Heob5b3fa32008-07-31 17:02:42 +09004992 return true;
4993 return false;
Tejun Heo34bf2172006-05-15 20:57:46 +09004994}
Edward Falk0baab862005-06-02 18:17:13 -04004995
Tejun Heob1c72912008-07-31 17:02:43 +09004996/**
4997 * ata_link_online - test whether the given link is online
4998 * @link: ATA link to test
4999 *
5000 * Test whether @link is online. This is identical to
5001 * ata_phys_link_online() when there's no slave link. When
5002 * there's a slave link, this function should only be called on
5003 * the master link and will return true if any of M/S links is
5004 * online.
5005 *
5006 * LOCKING:
5007 * None.
5008 *
5009 * RETURNS:
5010 * True if the port online status is available and online.
5011 */
5012bool ata_link_online(struct ata_link *link)
5013{
5014 struct ata_link *slave = link->ap->slave_link;
5015
5016 WARN_ON(link == slave); /* shouldn't be called on slave link */
5017
5018 return ata_phys_link_online(link) ||
5019 (slave && ata_phys_link_online(slave));
5020}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01005021EXPORT_SYMBOL_GPL(ata_link_online);
Tejun Heob1c72912008-07-31 17:02:43 +09005022
5023/**
5024 * ata_link_offline - test whether the given link is offline
5025 * @link: ATA link to test
5026 *
5027 * Test whether @link is offline. This is identical to
5028 * ata_phys_link_offline() when there's no slave link. When
5029 * there's a slave link, this function should only be called on
5030 * the master link and will return true if both M/S links are
5031 * offline.
5032 *
5033 * LOCKING:
5034 * None.
5035 *
5036 * RETURNS:
5037 * True if the port offline status is available and offline.
5038 */
5039bool ata_link_offline(struct ata_link *link)
5040{
5041 struct ata_link *slave = link->ap->slave_link;
5042
5043 WARN_ON(link == slave); /* shouldn't be called on slave link */
5044
5045 return ata_phys_link_offline(link) &&
5046 (!slave || ata_phys_link_offline(slave));
5047}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01005048EXPORT_SYMBOL_GPL(ata_link_offline);
Tejun Heob1c72912008-07-31 17:02:43 +09005049
Tejun Heo6ffa01d2007-03-02 17:32:47 +09005050#ifdef CONFIG_PM
Dan Williamsbc6e7c42014-03-14 13:52:48 -07005051static void ata_port_request_pm(struct ata_port *ap, pm_message_t mesg,
5052 unsigned int action, unsigned int ehi_flags,
5053 bool async)
Tejun Heo500530f2006-07-03 16:07:27 +09005054{
Lin Ming5ef41082011-12-05 09:20:27 +08005055 struct ata_link *link;
Tejun Heo500530f2006-07-03 16:07:27 +09005056 unsigned long flags;
Tejun Heo500530f2006-07-03 16:07:27 +09005057
Lin Ming5ef41082011-12-05 09:20:27 +08005058 /* Previous resume operation might still be in
5059 * progress. Wait for PM_PENDING to clear.
5060 */
5061 if (ap->pflags & ATA_PFLAG_PM_PENDING) {
5062 ata_port_wait_eh(ap);
5063 WARN_ON(ap->pflags & ATA_PFLAG_PM_PENDING);
Tejun Heo500530f2006-07-03 16:07:27 +09005064 }
5065
Lin Ming5ef41082011-12-05 09:20:27 +08005066 /* request PM ops to EH */
5067 spin_lock_irqsave(ap->lock, flags);
5068
5069 ap->pm_mesg = mesg;
Lin Ming5ef41082011-12-05 09:20:27 +08005070 ap->pflags |= ATA_PFLAG_PM_PENDING;
5071 ata_for_each_link(link, ap, HOST_FIRST) {
5072 link->eh_info.action |= action;
5073 link->eh_info.flags |= ehi_flags;
5074 }
5075
5076 ata_port_schedule_eh(ap);
5077
5078 spin_unlock_irqrestore(ap->lock, flags);
5079
Dan Williams2fcbdcb2012-06-21 23:41:46 -07005080 if (!async) {
Lin Ming5ef41082011-12-05 09:20:27 +08005081 ata_port_wait_eh(ap);
5082 WARN_ON(ap->pflags & ATA_PFLAG_PM_PENDING);
5083 }
Tejun Heo500530f2006-07-03 16:07:27 +09005084}
5085
Dan Williamsbc6e7c42014-03-14 13:52:48 -07005086/*
5087 * On some hardware, device fails to respond after spun down for suspend. As
5088 * the device won't be used before being resumed, we don't need to touch the
5089 * device. Ask EH to skip the usual stuff and proceed directly to suspend.
5090 *
5091 * http://thread.gmane.org/gmane.linux.ide/46764
5092 */
5093static const unsigned int ata_port_suspend_ehi = ATA_EHI_QUIET
5094 | ATA_EHI_NO_AUTOPSY
5095 | ATA_EHI_NO_RECOVERY;
5096
5097static void ata_port_suspend(struct ata_port *ap, pm_message_t mesg)
Lin Ming5ef41082011-12-05 09:20:27 +08005098{
Dan Williamsbc6e7c42014-03-14 13:52:48 -07005099 ata_port_request_pm(ap, mesg, 0, ata_port_suspend_ehi, false);
Lin Ming5ef41082011-12-05 09:20:27 +08005100}
5101
Dan Williamsbc6e7c42014-03-14 13:52:48 -07005102static void ata_port_suspend_async(struct ata_port *ap, pm_message_t mesg)
5103{
5104 ata_port_request_pm(ap, mesg, 0, ata_port_suspend_ehi, true);
5105}
5106
5107static int ata_port_pm_suspend(struct device *dev)
Dan Williams2fcbdcb2012-06-21 23:41:46 -07005108{
5109 struct ata_port *ap = to_ata_port(dev);
5110
Lin Ming5ef41082011-12-05 09:20:27 +08005111 if (pm_runtime_suspended(dev))
5112 return 0;
5113
Dan Williamsbc6e7c42014-03-14 13:52:48 -07005114 ata_port_suspend(ap, PMSG_SUSPEND);
5115 return 0;
Lin Ming33574d62011-12-22 14:50:49 +08005116}
5117
Dan Williamsbc6e7c42014-03-14 13:52:48 -07005118static int ata_port_pm_freeze(struct device *dev)
Dan Williams2fcbdcb2012-06-21 23:41:46 -07005119{
5120 struct ata_port *ap = to_ata_port(dev);
5121
Dan Williamsbc6e7c42014-03-14 13:52:48 -07005122 if (pm_runtime_suspended(dev))
5123 return 0;
5124
5125 ata_port_suspend(ap, PMSG_FREEZE);
5126 return 0;
Dan Williams2fcbdcb2012-06-21 23:41:46 -07005127}
5128
Dan Williamsbc6e7c42014-03-14 13:52:48 -07005129static int ata_port_pm_poweroff(struct device *dev)
Lin Minge90b1e52011-12-22 14:50:48 +08005130{
Dan Williamsbc6e7c42014-03-14 13:52:48 -07005131 ata_port_suspend(to_ata_port(dev), PMSG_HIBERNATE);
5132 return 0;
5133}
Lin Minge90b1e52011-12-22 14:50:48 +08005134
Dan Williamsbc6e7c42014-03-14 13:52:48 -07005135static const unsigned int ata_port_resume_ehi = ATA_EHI_NO_AUTOPSY
5136 | ATA_EHI_QUIET;
Lin Minge90b1e52011-12-22 14:50:48 +08005137
Dan Williamsbc6e7c42014-03-14 13:52:48 -07005138static void ata_port_resume(struct ata_port *ap, pm_message_t mesg)
5139{
5140 ata_port_request_pm(ap, mesg, ATA_EH_RESET, ata_port_resume_ehi, false);
5141}
5142
5143static void ata_port_resume_async(struct ata_port *ap, pm_message_t mesg)
5144{
5145 ata_port_request_pm(ap, mesg, ATA_EH_RESET, ata_port_resume_ehi, true);
5146}
5147
5148static int ata_port_pm_resume(struct device *dev)
5149{
Todd Brandt200421a2014-03-14 13:52:54 -07005150 ata_port_resume_async(to_ata_port(dev), PMSG_RESUME);
Dan Williamsbc6e7c42014-03-14 13:52:48 -07005151 pm_runtime_disable(dev);
5152 pm_runtime_set_active(dev);
5153 pm_runtime_enable(dev);
5154 return 0;
Lin Minge90b1e52011-12-22 14:50:48 +08005155}
5156
Aaron Lu7e15e9b2013-01-15 17:21:04 +08005157/*
5158 * For ODDs, the upper layer will poll for media change every few seconds,
5159 * which will make it enter and leave suspend state every few seconds. And
5160 * as each suspend will cause a hard/soft reset, the gain of runtime suspend
5161 * is very little and the ODD may malfunction after constantly being reset.
5162 * So the idle callback here will not proceed to suspend if a non-ZPODD capable
5163 * ODD is attached to the port.
5164 */
Lin Ming9ee4f392011-12-05 09:20:28 +08005165static int ata_port_runtime_idle(struct device *dev)
5166{
Aaron Lu7e15e9b2013-01-15 17:21:04 +08005167 struct ata_port *ap = to_ata_port(dev);
5168 struct ata_link *link;
5169 struct ata_device *adev;
5170
5171 ata_for_each_link(link, ap, HOST_FIRST) {
5172 ata_for_each_dev(adev, link, ENABLED)
5173 if (adev->class == ATA_DEV_ATAPI &&
5174 !zpodd_dev_enabled(adev))
5175 return -EBUSY;
5176 }
5177
Rafael J. Wysocki45f0a852013-06-03 21:49:52 +02005178 return 0;
Lin Ming9ee4f392011-12-05 09:20:28 +08005179}
5180
Aaron Lua7ff60d2013-01-25 14:29:35 +08005181static int ata_port_runtime_suspend(struct device *dev)
5182{
Dan Williamsbc6e7c42014-03-14 13:52:48 -07005183 ata_port_suspend(to_ata_port(dev), PMSG_AUTO_SUSPEND);
5184 return 0;
Aaron Lua7ff60d2013-01-25 14:29:35 +08005185}
5186
5187static int ata_port_runtime_resume(struct device *dev)
5188{
Dan Williamsbc6e7c42014-03-14 13:52:48 -07005189 ata_port_resume(to_ata_port(dev), PMSG_AUTO_RESUME);
5190 return 0;
Aaron Lua7ff60d2013-01-25 14:29:35 +08005191}
5192
Lin Ming5ef41082011-12-05 09:20:27 +08005193static const struct dev_pm_ops ata_port_pm_ops = {
Dan Williamsbc6e7c42014-03-14 13:52:48 -07005194 .suspend = ata_port_pm_suspend,
5195 .resume = ata_port_pm_resume,
5196 .freeze = ata_port_pm_freeze,
5197 .thaw = ata_port_pm_resume,
5198 .poweroff = ata_port_pm_poweroff,
5199 .restore = ata_port_pm_resume,
Lin Ming9ee4f392011-12-05 09:20:28 +08005200
Aaron Lua7ff60d2013-01-25 14:29:35 +08005201 .runtime_suspend = ata_port_runtime_suspend,
5202 .runtime_resume = ata_port_runtime_resume,
Lin Ming9ee4f392011-12-05 09:20:28 +08005203 .runtime_idle = ata_port_runtime_idle,
Lin Ming5ef41082011-12-05 09:20:27 +08005204};
5205
Dan Williams2fcbdcb2012-06-21 23:41:46 -07005206/* sas ports don't participate in pm runtime management of ata_ports,
5207 * and need to resume ata devices at the domain level, not the per-port
5208 * level. sas suspend/resume is async to allow parallel port recovery
5209 * since sas has multiple ata_port instances per Scsi_Host.
5210 */
Dan Williamsbc6e7c42014-03-14 13:52:48 -07005211void ata_sas_port_suspend(struct ata_port *ap)
Dan Williams2fcbdcb2012-06-21 23:41:46 -07005212{
Dan Williamsbc6e7c42014-03-14 13:52:48 -07005213 ata_port_suspend_async(ap, PMSG_SUSPEND);
Dan Williams2fcbdcb2012-06-21 23:41:46 -07005214}
Dan Williamsbc6e7c42014-03-14 13:52:48 -07005215EXPORT_SYMBOL_GPL(ata_sas_port_suspend);
Dan Williams2fcbdcb2012-06-21 23:41:46 -07005216
Dan Williamsbc6e7c42014-03-14 13:52:48 -07005217void ata_sas_port_resume(struct ata_port *ap)
Dan Williams2fcbdcb2012-06-21 23:41:46 -07005218{
Dan Williamsbc6e7c42014-03-14 13:52:48 -07005219 ata_port_resume_async(ap, PMSG_RESUME);
Dan Williams2fcbdcb2012-06-21 23:41:46 -07005220}
Dan Williamsbc6e7c42014-03-14 13:52:48 -07005221EXPORT_SYMBOL_GPL(ata_sas_port_resume);
Dan Williams2fcbdcb2012-06-21 23:41:46 -07005222
Tejun Heo500530f2006-07-03 16:07:27 +09005223/**
Jeff Garzikcca39742006-08-24 03:19:22 -04005224 * ata_host_suspend - suspend host
5225 * @host: host to suspend
Tejun Heo500530f2006-07-03 16:07:27 +09005226 * @mesg: PM message
5227 *
Lin Ming5ef41082011-12-05 09:20:27 +08005228 * Suspend @host. Actual operation is performed by port suspend.
Tejun Heo500530f2006-07-03 16:07:27 +09005229 */
Jeff Garzikcca39742006-08-24 03:19:22 -04005230int ata_host_suspend(struct ata_host *host, pm_message_t mesg)
Tejun Heo500530f2006-07-03 16:07:27 +09005231{
Lin Ming5ef41082011-12-05 09:20:27 +08005232 host->dev->power.power_state = mesg;
5233 return 0;
Tejun Heo500530f2006-07-03 16:07:27 +09005234}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01005235EXPORT_SYMBOL_GPL(ata_host_suspend);
Tejun Heo500530f2006-07-03 16:07:27 +09005236
5237/**
Jeff Garzikcca39742006-08-24 03:19:22 -04005238 * ata_host_resume - resume host
5239 * @host: host to resume
Tejun Heo500530f2006-07-03 16:07:27 +09005240 *
Lin Ming5ef41082011-12-05 09:20:27 +08005241 * Resume @host. Actual operation is performed by port resume.
Tejun Heo500530f2006-07-03 16:07:27 +09005242 */
Jeff Garzikcca39742006-08-24 03:19:22 -04005243void ata_host_resume(struct ata_host *host)
Tejun Heo500530f2006-07-03 16:07:27 +09005244{
Jeff Garzik72ad6ec2008-02-25 17:31:10 -05005245 host->dev->power.power_state = PMSG_ON;
Tejun Heo500530f2006-07-03 16:07:27 +09005246}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01005247EXPORT_SYMBOL_GPL(ata_host_resume);
Tejun Heo6ffa01d2007-03-02 17:32:47 +09005248#endif
Tejun Heo500530f2006-07-03 16:07:27 +09005249
Bhumika Goyal8df82c12017-09-30 22:10:40 +05305250const struct device_type ata_port_type = {
Lin Ming5ef41082011-12-05 09:20:27 +08005251 .name = "ata_port",
5252#ifdef CONFIG_PM
5253 .pm = &ata_port_pm_ops,
5254#endif
5255};
5256
Randy Dunlapc893a3a2006-01-28 13:15:32 -05005257/**
Tejun Heo3ef3b432006-05-31 18:27:30 +09005258 * ata_dev_init - Initialize an ata_device structure
5259 * @dev: Device structure to initialize
5260 *
5261 * Initialize @dev in preparation for probing.
5262 *
5263 * LOCKING:
5264 * Inherited from caller.
5265 */
5266void ata_dev_init(struct ata_device *dev)
5267{
Tejun Heob1c72912008-07-31 17:02:43 +09005268 struct ata_link *link = ata_dev_phys_link(dev);
Tejun Heo9af5c9c2007-08-06 18:36:22 +09005269 struct ata_port *ap = link->ap;
Tejun Heo72fa4b72006-05-31 18:27:32 +09005270 unsigned long flags;
Tejun Heo3ef3b432006-05-31 18:27:30 +09005271
Tejun Heob1c72912008-07-31 17:02:43 +09005272 /* SATA spd limit is bound to the attached device, reset together */
Tejun Heo9af5c9c2007-08-06 18:36:22 +09005273 link->sata_spd_limit = link->hw_sata_spd_limit;
5274 link->sata_spd = 0;
Tejun Heo5a04bf42006-05-31 18:27:38 +09005275
Tejun Heo72fa4b72006-05-31 18:27:32 +09005276 /* High bits of dev->flags are used to record warm plug
5277 * requests which occur asynchronously. Synchronize using
Jeff Garzikcca39742006-08-24 03:19:22 -04005278 * host lock.
Tejun Heo72fa4b72006-05-31 18:27:32 +09005279 */
Jeff Garzikba6a1302006-06-22 23:46:10 -04005280 spin_lock_irqsave(ap->lock, flags);
Tejun Heo72fa4b72006-05-31 18:27:32 +09005281 dev->flags &= ~ATA_DFLAG_INIT_MASK;
Tejun Heo3dcc3232007-09-03 12:20:11 +09005282 dev->horkage = 0;
Jeff Garzikba6a1302006-06-22 23:46:10 -04005283 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heo72fa4b72006-05-31 18:27:32 +09005284
Tejun Heo99cf6102009-01-29 20:31:32 +09005285 memset((void *)dev + ATA_DEVICE_CLEAR_BEGIN, 0,
5286 ATA_DEVICE_CLEAR_END - ATA_DEVICE_CLEAR_BEGIN);
Tejun Heo3ef3b432006-05-31 18:27:30 +09005287 dev->pio_mask = UINT_MAX;
5288 dev->mwdma_mask = UINT_MAX;
5289 dev->udma_mask = UINT_MAX;
5290}
5291
5292/**
Tejun Heo4fb37a22007-08-06 18:36:23 +09005293 * ata_link_init - Initialize an ata_link structure
5294 * @ap: ATA port link is attached to
5295 * @link: Link structure to initialize
Tejun Heo89898052007-08-06 18:36:23 +09005296 * @pmp: Port multiplier port number
Tejun Heo4fb37a22007-08-06 18:36:23 +09005297 *
5298 * Initialize @link.
5299 *
5300 * LOCKING:
5301 * Kernel thread context (may sleep)
5302 */
Tejun Heofb7fd612007-09-23 13:14:12 +09005303void ata_link_init(struct ata_port *ap, struct ata_link *link, int pmp)
Tejun Heo4fb37a22007-08-06 18:36:23 +09005304{
5305 int i;
5306
5307 /* clear everything except for devices */
Gwendal Grignoud9027472010-05-25 12:31:38 -07005308 memset((void *)link + ATA_LINK_CLEAR_BEGIN, 0,
5309 ATA_LINK_CLEAR_END - ATA_LINK_CLEAR_BEGIN);
Tejun Heo4fb37a22007-08-06 18:36:23 +09005310
5311 link->ap = ap;
Tejun Heo89898052007-08-06 18:36:23 +09005312 link->pmp = pmp;
Tejun Heo4fb37a22007-08-06 18:36:23 +09005313 link->active_tag = ATA_TAG_POISON;
5314 link->hw_sata_spd_limit = UINT_MAX;
5315
5316 /* can't use iterator, ap isn't initialized yet */
5317 for (i = 0; i < ATA_MAX_DEVICES; i++) {
5318 struct ata_device *dev = &link->device[i];
5319
5320 dev->link = link;
5321 dev->devno = dev - link->device;
Tejun Heo110f66d2009-09-16 04:17:28 +09005322#ifdef CONFIG_ATA_ACPI
5323 dev->gtf_filter = ata_acpi_gtf_filter;
5324#endif
Tejun Heo4fb37a22007-08-06 18:36:23 +09005325 ata_dev_init(dev);
5326 }
5327}
5328
5329/**
5330 * sata_link_init_spd - Initialize link->sata_spd_limit
5331 * @link: Link to configure sata_spd_limit for
5332 *
Mauro Carvalho Chehaba31a6992020-04-14 18:48:55 +02005333 * Initialize ``link->[hw_]sata_spd_limit`` to the currently
Tejun Heo4fb37a22007-08-06 18:36:23 +09005334 * configured value.
5335 *
5336 * LOCKING:
5337 * Kernel thread context (may sleep).
5338 *
5339 * RETURNS:
5340 * 0 on success, -errno on failure.
5341 */
Tejun Heofb7fd612007-09-23 13:14:12 +09005342int sata_link_init_spd(struct ata_link *link)
Tejun Heo4fb37a22007-08-06 18:36:23 +09005343{
Tejun Heo33267322008-02-13 09:15:09 +09005344 u8 spd;
Tejun Heo4fb37a22007-08-06 18:36:23 +09005345 int rc;
5346
Tejun Heod127ea72008-07-31 16:09:34 +09005347 rc = sata_scr_read(link, SCR_CONTROL, &link->saved_scontrol);
Tejun Heo4fb37a22007-08-06 18:36:23 +09005348 if (rc)
5349 return rc;
5350
Tejun Heod127ea72008-07-31 16:09:34 +09005351 spd = (link->saved_scontrol >> 4) & 0xf;
Tejun Heo4fb37a22007-08-06 18:36:23 +09005352 if (spd)
5353 link->hw_sata_spd_limit &= (1 << spd) - 1;
5354
Tejun Heo05944bd2008-08-13 20:19:09 +09005355 ata_force_link_limits(link);
Tejun Heo33267322008-02-13 09:15:09 +09005356
Tejun Heo4fb37a22007-08-06 18:36:23 +09005357 link->sata_spd_limit = link->hw_sata_spd_limit;
5358
5359 return 0;
5360}
5361
5362/**
Tejun Heof3187192007-04-17 23:44:07 +09005363 * ata_port_alloc - allocate and initialize basic ATA port resources
5364 * @host: ATA host this allocated port belongs to
Linus Torvalds1da177e2005-04-16 15:20:36 -07005365 *
Tejun Heof3187192007-04-17 23:44:07 +09005366 * Allocate and initialize basic ATA port resources.
5367 *
5368 * RETURNS:
5369 * Allocate ATA port on success, NULL on failure.
Jeff Garzik0cba6322005-05-30 19:49:12 -04005370 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07005371 * LOCKING:
Tejun Heof3187192007-04-17 23:44:07 +09005372 * Inherited from calling layer (may sleep).
Linus Torvalds1da177e2005-04-16 15:20:36 -07005373 */
Tejun Heof3187192007-04-17 23:44:07 +09005374struct ata_port *ata_port_alloc(struct ata_host *host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005375{
Tejun Heof3187192007-04-17 23:44:07 +09005376 struct ata_port *ap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005377
Tejun Heof3187192007-04-17 23:44:07 +09005378 DPRINTK("ENTER\n");
5379
5380 ap = kzalloc(sizeof(*ap), GFP_KERNEL);
5381 if (!ap)
5382 return NULL;
Jeff Garzik4fca3772011-02-15 01:13:24 -05005383
Maxime Bizon7b3a24c52011-03-16 14:58:32 +01005384 ap->pflags |= ATA_PFLAG_INITIALIZING | ATA_PFLAG_FROZEN;
Jeff Garzikcca39742006-08-24 03:19:22 -04005385 ap->lock = &host->lock;
Tejun Heof3187192007-04-17 23:44:07 +09005386 ap->print_id = -1;
David Milburne628dc92013-05-14 13:48:40 -05005387 ap->local_port_no = -1;
Jeff Garzikcca39742006-08-24 03:19:22 -04005388 ap->host = host;
Tejun Heof3187192007-04-17 23:44:07 +09005389 ap->dev = host->dev;
Borislav Petkovbd5d8252006-06-11 23:17:01 -04005390
5391#if defined(ATA_VERBOSE_DEBUG)
5392 /* turn on all debugging levels */
5393 ap->msg_enable = 0x00FF;
5394#elif defined(ATA_DEBUG)
5395 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 +09005396#else
Borislav Petkov0dd4b212006-06-23 02:29:08 -04005397 ap->msg_enable = ATA_MSG_DRV | ATA_MSG_ERR | ATA_MSG_WARN;
Borislav Petkovbd5d8252006-06-11 23:17:01 -04005398#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005399
Tejun Heoad72cf92010-07-02 10:03:52 +02005400 mutex_init(&ap->scsi_scan_mutex);
David Howells65f27f32006-11-22 14:55:48 +00005401 INIT_DELAYED_WORK(&ap->hotplug_task, ata_scsi_hotplug);
5402 INIT_WORK(&ap->scsi_rescan_task, ata_scsi_dev_rescan);
Tejun Heoa72ec4c2006-01-23 13:09:37 +09005403 INIT_LIST_HEAD(&ap->eh_done_q);
Tejun Heoc6cf9e92006-05-31 18:27:27 +09005404 init_waitqueue_head(&ap->eh_wait_q);
Elias Oltmanns45fabbb2008-09-21 11:54:08 +02005405 init_completion(&ap->park_req_pending);
Kees Cookb93ab332017-10-16 14:56:42 -07005406 timer_setup(&ap->fastdrain_timer, ata_eh_fastdrain_timerfn,
5407 TIMER_DEFERRABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005408
Tejun Heo838df622006-05-15 20:57:44 +09005409 ap->cbl = ATA_CBL_NONE;
Tejun Heo838df622006-05-15 20:57:44 +09005410
Tejun Heo89898052007-08-06 18:36:23 +09005411 ata_link_init(ap, &ap->link, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005412
5413#ifdef ATA_IRQ_TRAP
5414 ap->stats.unhandled_irq = 1;
5415 ap->stats.idle_irq = 1;
5416#endif
Tejun Heo270390e2010-05-10 21:41:35 +02005417 ata_sff_port_init(ap);
5418
Linus Torvalds1da177e2005-04-16 15:20:36 -07005419 return ap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005420}
5421
Taras Kondratiuk2623c7a2018-03-09 08:34:41 +00005422static void ata_devres_release(struct device *gendev, void *res)
Tejun Heof0d36ef2007-01-20 16:00:28 +09005423{
5424 struct ata_host *host = dev_get_drvdata(gendev);
5425 int i;
5426
5427 for (i = 0; i < host->n_ports; i++) {
5428 struct ata_port *ap = host->ports[i];
5429
Tejun Heoecef7252007-04-17 23:44:06 +09005430 if (!ap)
5431 continue;
5432
Tejun Heo49114872007-04-17 23:44:06 +09005433 if (ap->scsi_host)
Tejun Heo1aa506e42007-03-09 19:36:12 +09005434 scsi_host_put(ap->scsi_host);
5435
Taras Kondratiuk2623c7a2018-03-09 08:34:41 +00005436 }
5437
5438 dev_set_drvdata(gendev, NULL);
5439 ata_host_put(host);
5440}
5441
5442static void ata_host_release(struct kref *kref)
5443{
5444 struct ata_host *host = container_of(kref, struct ata_host, kref);
5445 int i;
5446
5447 for (i = 0; i < host->n_ports; i++) {
5448 struct ata_port *ap = host->ports[i];
5449
Tejun Heo633273a2007-09-23 13:19:54 +09005450 kfree(ap->pmp_link);
Tejun Heob1c72912008-07-31 17:02:43 +09005451 kfree(ap->slave_link);
Tejun Heo49114872007-04-17 23:44:06 +09005452 kfree(ap);
Tejun Heo1aa506e42007-03-09 19:36:12 +09005453 host->ports[i] = NULL;
5454 }
Taras Kondratiuk2623c7a2018-03-09 08:34:41 +00005455 kfree(host);
5456}
Tejun Heo1aa506e42007-03-09 19:36:12 +09005457
Taras Kondratiuk2623c7a2018-03-09 08:34:41 +00005458void ata_host_get(struct ata_host *host)
5459{
5460 kref_get(&host->kref);
5461}
5462
5463void ata_host_put(struct ata_host *host)
5464{
5465 kref_put(&host->kref, ata_host_release);
Tejun Heof0d36ef2007-01-20 16:00:28 +09005466}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01005467EXPORT_SYMBOL_GPL(ata_host_put);
Tejun Heof0d36ef2007-01-20 16:00:28 +09005468
Linus Torvalds1da177e2005-04-16 15:20:36 -07005469/**
Tejun Heof3187192007-04-17 23:44:07 +09005470 * ata_host_alloc - allocate and init basic ATA host resources
5471 * @dev: generic device this host is associated with
5472 * @max_ports: maximum number of ATA ports associated with this host
5473 *
5474 * Allocate and initialize basic ATA host resources. LLD calls
5475 * this function to allocate a host, initializes it fully and
5476 * attaches it using ata_host_register().
5477 *
5478 * @max_ports ports are allocated and host->n_ports is
5479 * initialized to @max_ports. The caller is allowed to decrease
5480 * host->n_ports before calling ata_host_register(). The unused
5481 * ports will be automatically freed on registration.
5482 *
5483 * RETURNS:
5484 * Allocate ATA host on success, NULL on failure.
5485 *
5486 * LOCKING:
5487 * Inherited from calling layer (may sleep).
5488 */
5489struct ata_host *ata_host_alloc(struct device *dev, int max_ports)
5490{
5491 struct ata_host *host;
5492 size_t sz;
5493 int i;
Taras Kondratiuk2623c7a2018-03-09 08:34:41 +00005494 void *dr;
Tejun Heof3187192007-04-17 23:44:07 +09005495
5496 DPRINTK("ENTER\n");
5497
Tejun Heof3187192007-04-17 23:44:07 +09005498 /* alloc a container for our list of ATA ports (buses) */
5499 sz = sizeof(struct ata_host) + (max_ports + 1) * sizeof(void *);
Taras Kondratiuk2623c7a2018-03-09 08:34:41 +00005500 host = kzalloc(sz, GFP_KERNEL);
Tejun Heof3187192007-04-17 23:44:07 +09005501 if (!host)
Taras Kondratiuk2623c7a2018-03-09 08:34:41 +00005502 return NULL;
5503
Tejun Heof3187192007-04-17 23:44:07 +09005504 if (!devres_open_group(dev, NULL, GFP_KERNEL))
Colin Ian Kingdafd6c42018-03-27 14:26:01 +01005505 goto err_free;
Tejun Heof3187192007-04-17 23:44:07 +09005506
Taras Kondratiuk2623c7a2018-03-09 08:34:41 +00005507 dr = devres_alloc(ata_devres_release, 0, GFP_KERNEL);
5508 if (!dr)
Tejun Heof3187192007-04-17 23:44:07 +09005509 goto err_out;
5510
Taras Kondratiuk2623c7a2018-03-09 08:34:41 +00005511 devres_add(dev, dr);
Tejun Heof3187192007-04-17 23:44:07 +09005512 dev_set_drvdata(dev, host);
5513
5514 spin_lock_init(&host->lock);
Tejun Heoc0c362b2010-09-06 17:57:14 +02005515 mutex_init(&host->eh_mutex);
Tejun Heof3187192007-04-17 23:44:07 +09005516 host->dev = dev;
5517 host->n_ports = max_ports;
Taras Kondratiuk2623c7a2018-03-09 08:34:41 +00005518 kref_init(&host->kref);
Tejun Heof3187192007-04-17 23:44:07 +09005519
5520 /* allocate ports bound to this host */
5521 for (i = 0; i < max_ports; i++) {
5522 struct ata_port *ap;
5523
5524 ap = ata_port_alloc(host);
5525 if (!ap)
5526 goto err_out;
5527
5528 ap->port_no = i;
5529 host->ports[i] = ap;
5530 }
5531
5532 devres_remove_group(dev, NULL);
5533 return host;
5534
5535 err_out:
5536 devres_release_group(dev, NULL);
Colin Ian Kingdafd6c42018-03-27 14:26:01 +01005537 err_free:
5538 kfree(host);
Tejun Heof3187192007-04-17 23:44:07 +09005539 return NULL;
5540}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01005541EXPORT_SYMBOL_GPL(ata_host_alloc);
Tejun Heof3187192007-04-17 23:44:07 +09005542
5543/**
Tejun Heof5cda252007-04-17 23:44:07 +09005544 * ata_host_alloc_pinfo - alloc host and init with port_info array
5545 * @dev: generic device this host is associated with
5546 * @ppi: array of ATA port_info to initialize host with
5547 * @n_ports: number of ATA ports attached to this host
5548 *
5549 * Allocate ATA host and initialize with info from @ppi. If NULL
5550 * terminated, @ppi may contain fewer entries than @n_ports. The
5551 * last entry will be used for the remaining ports.
5552 *
5553 * RETURNS:
5554 * Allocate ATA host on success, NULL on failure.
5555 *
5556 * LOCKING:
5557 * Inherited from calling layer (may sleep).
5558 */
5559struct ata_host *ata_host_alloc_pinfo(struct device *dev,
5560 const struct ata_port_info * const * ppi,
5561 int n_ports)
5562{
5563 const struct ata_port_info *pi;
5564 struct ata_host *host;
5565 int i, j;
5566
5567 host = ata_host_alloc(dev, n_ports);
5568 if (!host)
5569 return NULL;
5570
5571 for (i = 0, j = 0, pi = NULL; i < host->n_ports; i++) {
5572 struct ata_port *ap = host->ports[i];
5573
5574 if (ppi[j])
5575 pi = ppi[j++];
5576
5577 ap->pio_mask = pi->pio_mask;
5578 ap->mwdma_mask = pi->mwdma_mask;
5579 ap->udma_mask = pi->udma_mask;
5580 ap->flags |= pi->flags;
Tejun Heo0c887582007-08-06 18:36:23 +09005581 ap->link.flags |= pi->link_flags;
Tejun Heof5cda252007-04-17 23:44:07 +09005582 ap->ops = pi->port_ops;
5583
5584 if (!host->ops && (pi->port_ops != &ata_dummy_port_ops))
5585 host->ops = pi->port_ops;
Tejun Heof5cda252007-04-17 23:44:07 +09005586 }
5587
5588 return host;
5589}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01005590EXPORT_SYMBOL_GPL(ata_host_alloc_pinfo);
Tejun Heof5cda252007-04-17 23:44:07 +09005591
Tejun Heo32ebbc02007-11-08 13:09:00 +09005592static void ata_host_stop(struct device *gendev, void *res)
5593{
5594 struct ata_host *host = dev_get_drvdata(gendev);
5595 int i;
5596
5597 WARN_ON(!(host->flags & ATA_HOST_STARTED));
5598
5599 for (i = 0; i < host->n_ports; i++) {
5600 struct ata_port *ap = host->ports[i];
5601
5602 if (ap->ops->port_stop)
5603 ap->ops->port_stop(ap);
5604 }
5605
5606 if (host->ops->host_stop)
5607 host->ops->host_stop(host);
5608}
5609
Tejun Heof5cda252007-04-17 23:44:07 +09005610/**
Tejun Heo029cfd62008-03-25 12:22:49 +09005611 * ata_finalize_port_ops - finalize ata_port_operations
5612 * @ops: ata_port_operations to finalize
5613 *
5614 * An ata_port_operations can inherit from another ops and that
5615 * ops can again inherit from another. This can go on as many
5616 * times as necessary as long as there is no loop in the
5617 * inheritance chain.
5618 *
5619 * Ops tables are finalized when the host is started. NULL or
5620 * unspecified entries are inherited from the closet ancestor
5621 * which has the method and the entry is populated with it.
5622 * After finalization, the ops table directly points to all the
5623 * methods and ->inherits is no longer necessary and cleared.
5624 *
5625 * Using ATA_OP_NULL, inheriting ops can force a method to NULL.
5626 *
5627 * LOCKING:
5628 * None.
5629 */
5630static void ata_finalize_port_ops(struct ata_port_operations *ops)
5631{
Pradeep Singh Rautela2da67652008-05-29 23:28:14 +05305632 static DEFINE_SPINLOCK(lock);
Tejun Heo029cfd62008-03-25 12:22:49 +09005633 const struct ata_port_operations *cur;
5634 void **begin = (void **)ops;
5635 void **end = (void **)&ops->inherits;
5636 void **pp;
5637
5638 if (!ops || !ops->inherits)
5639 return;
5640
5641 spin_lock(&lock);
5642
5643 for (cur = ops->inherits; cur; cur = cur->inherits) {
5644 void **inherit = (void **)cur;
5645
5646 for (pp = begin; pp < end; pp++, inherit++)
5647 if (!*pp)
5648 *pp = *inherit;
5649 }
5650
5651 for (pp = begin; pp < end; pp++)
5652 if (IS_ERR(*pp))
5653 *pp = NULL;
5654
5655 ops->inherits = NULL;
5656
5657 spin_unlock(&lock);
5658}
5659
5660/**
Tejun Heoecef7252007-04-17 23:44:06 +09005661 * ata_host_start - start and freeze ports of an ATA host
5662 * @host: ATA host to start ports for
5663 *
5664 * Start and then freeze ports of @host. Started status is
5665 * recorded in host->flags, so this function can be called
5666 * multiple times. Ports are guaranteed to get started only
Tejun Heof3187192007-04-17 23:44:07 +09005667 * once. If host->ops isn't initialized yet, its set to the
5668 * first non-dummy port ops.
Tejun Heoecef7252007-04-17 23:44:06 +09005669 *
5670 * LOCKING:
5671 * Inherited from calling layer (may sleep).
5672 *
5673 * RETURNS:
5674 * 0 if all ports are started successfully, -errno otherwise.
5675 */
5676int ata_host_start(struct ata_host *host)
5677{
Tejun Heo32ebbc02007-11-08 13:09:00 +09005678 int have_stop = 0;
5679 void *start_dr = NULL;
Tejun Heoecef7252007-04-17 23:44:06 +09005680 int i, rc;
5681
5682 if (host->flags & ATA_HOST_STARTED)
5683 return 0;
5684
Tejun Heo029cfd62008-03-25 12:22:49 +09005685 ata_finalize_port_ops(host->ops);
5686
Tejun Heoecef7252007-04-17 23:44:06 +09005687 for (i = 0; i < host->n_ports; i++) {
5688 struct ata_port *ap = host->ports[i];
5689
Tejun Heo029cfd62008-03-25 12:22:49 +09005690 ata_finalize_port_ops(ap->ops);
5691
Tejun Heof3187192007-04-17 23:44:07 +09005692 if (!host->ops && !ata_port_is_dummy(ap))
5693 host->ops = ap->ops;
5694
Tejun Heo32ebbc02007-11-08 13:09:00 +09005695 if (ap->ops->port_stop)
5696 have_stop = 1;
5697 }
5698
Damien Le Moal355a8032021-08-16 10:44:47 +09005699 if (host->ops && host->ops->host_stop)
Tejun Heo32ebbc02007-11-08 13:09:00 +09005700 have_stop = 1;
5701
5702 if (have_stop) {
5703 start_dr = devres_alloc(ata_host_stop, 0, GFP_KERNEL);
5704 if (!start_dr)
5705 return -ENOMEM;
5706 }
5707
5708 for (i = 0; i < host->n_ports; i++) {
5709 struct ata_port *ap = host->ports[i];
5710
Tejun Heoecef7252007-04-17 23:44:06 +09005711 if (ap->ops->port_start) {
5712 rc = ap->ops->port_start(ap);
5713 if (rc) {
Alan Cox0f9fe9b2007-11-30 15:23:16 +00005714 if (rc != -ENODEV)
Joe Perchesa44fec12011-04-15 15:51:58 -07005715 dev_err(host->dev,
5716 "failed to start port %d (errno=%d)\n",
5717 i, rc);
Tejun Heoecef7252007-04-17 23:44:06 +09005718 goto err_out;
5719 }
5720 }
Tejun Heoecef7252007-04-17 23:44:06 +09005721 ata_eh_freeze_port(ap);
5722 }
5723
Tejun Heo32ebbc02007-11-08 13:09:00 +09005724 if (start_dr)
5725 devres_add(host->dev, start_dr);
Tejun Heoecef7252007-04-17 23:44:06 +09005726 host->flags |= ATA_HOST_STARTED;
5727 return 0;
5728
5729 err_out:
5730 while (--i >= 0) {
5731 struct ata_port *ap = host->ports[i];
5732
5733 if (ap->ops->port_stop)
5734 ap->ops->port_stop(ap);
5735 }
Tejun Heo32ebbc02007-11-08 13:09:00 +09005736 devres_free(start_dr);
Tejun Heoecef7252007-04-17 23:44:06 +09005737 return rc;
5738}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01005739EXPORT_SYMBOL_GPL(ata_host_start);
Tejun Heoecef7252007-04-17 23:44:06 +09005740
5741/**
Mauro Carvalho Chehab94bd5712020-10-23 18:32:55 +02005742 * ata_host_init - Initialize a host struct for sas (ipr, libsas)
Jeff Garzikcca39742006-08-24 03:19:22 -04005743 * @host: host to initialize
5744 * @dev: device host is attached to
Jeff Garzikcca39742006-08-24 03:19:22 -04005745 * @ops: port_ops
Brian Kingb03732f2006-08-07 14:27:10 -05005746 *
Brian Kingb03732f2006-08-07 14:27:10 -05005747 */
Jeff Garzikcca39742006-08-24 03:19:22 -04005748void ata_host_init(struct ata_host *host, struct device *dev,
Dan Williams8d8e7d12012-07-09 21:06:08 -07005749 struct ata_port_operations *ops)
Brian Kingb03732f2006-08-07 14:27:10 -05005750{
Jeff Garzikcca39742006-08-24 03:19:22 -04005751 spin_lock_init(&host->lock);
Tejun Heoc0c362b2010-09-06 17:57:14 +02005752 mutex_init(&host->eh_mutex);
Jens Axboe69278f72018-05-11 12:51:10 -06005753 host->n_tags = ATA_MAX_QUEUE;
Jeff Garzikcca39742006-08-24 03:19:22 -04005754 host->dev = dev;
Jeff Garzikcca39742006-08-24 03:19:22 -04005755 host->ops = ops;
Jason Yan2fa4a322018-05-10 11:05:16 +08005756 kref_init(&host->kref);
Brian Kingb03732f2006-08-07 14:27:10 -05005757}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01005758EXPORT_SYMBOL_GPL(ata_host_init);
Brian Kingb03732f2006-08-07 14:27:10 -05005759
Dan Williams9508a662012-01-18 20:47:01 -08005760void __ata_port_probe(struct ata_port *ap)
5761{
5762 struct ata_eh_info *ehi = &ap->link.eh_info;
5763 unsigned long flags;
5764
5765 /* kick EH for boot probing */
5766 spin_lock_irqsave(ap->lock, flags);
5767
5768 ehi->probe_mask |= ATA_ALL_DEVICES;
5769 ehi->action |= ATA_EH_RESET;
5770 ehi->flags |= ATA_EHI_NO_AUTOPSY | ATA_EHI_QUIET;
5771
5772 ap->pflags &= ~ATA_PFLAG_INITIALIZING;
5773 ap->pflags |= ATA_PFLAG_LOADING;
5774 ata_port_schedule_eh(ap);
5775
5776 spin_unlock_irqrestore(ap->lock, flags);
5777}
5778
James Bottomley238c9cf2011-01-23 08:28:33 -06005779int ata_port_probe(struct ata_port *ap)
Arjan van de Ven79318052009-01-04 05:32:28 -08005780{
James Bottomley238c9cf2011-01-23 08:28:33 -06005781 int rc = 0;
Arjan van de Ven886ad092009-01-09 15:54:07 -08005782
Arjan van de Ven79318052009-01-04 05:32:28 -08005783 if (ap->ops->error_handler) {
Dan Williams9508a662012-01-18 20:47:01 -08005784 __ata_port_probe(ap);
Arjan van de Ven79318052009-01-04 05:32:28 -08005785 ata_port_wait_eh(ap);
5786 } else {
5787 DPRINTK("ata%u: bus probe begin\n", ap->print_id);
5788 rc = ata_bus_probe(ap);
5789 DPRINTK("ata%u: bus probe end\n", ap->print_id);
Arjan van de Ven79318052009-01-04 05:32:28 -08005790 }
James Bottomley238c9cf2011-01-23 08:28:33 -06005791 return rc;
5792}
5793
5794
5795static void async_port_probe(void *data, async_cookie_t cookie)
5796{
5797 struct ata_port *ap = data;
Jeff Garzik4fca3772011-02-15 01:13:24 -05005798
James Bottomley238c9cf2011-01-23 08:28:33 -06005799 /*
5800 * If we're not allowed to scan this host in parallel,
5801 * we need to wait until all previous scans have completed
5802 * before going further.
5803 * Jeff Garzik says this is only within a controller, so we
5804 * don't need to wait for port 0, only for later ports.
5805 */
5806 if (!(ap->host->flags & ATA_HOST_PARALLEL_SCAN) && ap->port_no != 0)
5807 async_synchronize_cookie(cookie);
5808
5809 (void)ata_port_probe(ap);
Arjan van de Venf29d3b22009-01-05 15:07:07 -08005810
5811 /* in order to keep device order, we need to synchronize at this point */
5812 async_synchronize_cookie(cookie);
5813
5814 ata_scsi_scan_host(ap, 1);
Arjan van de Ven79318052009-01-04 05:32:28 -08005815}
James Bottomley238c9cf2011-01-23 08:28:33 -06005816
Brian Kingb03732f2006-08-07 14:27:10 -05005817/**
Tejun Heof3187192007-04-17 23:44:07 +09005818 * ata_host_register - register initialized ATA host
5819 * @host: ATA host to register
5820 * @sht: template for SCSI host
Jeff Garzik0cba6322005-05-30 19:49:12 -04005821 *
Tejun Heof3187192007-04-17 23:44:07 +09005822 * Register initialized ATA host. @host is allocated using
5823 * ata_host_alloc() and fully initialized by LLD. This function
5824 * starts ports, registers @host with ATA and SCSI layers and
5825 * probe registered devices.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005826 *
5827 * LOCKING:
Tejun Heof3187192007-04-17 23:44:07 +09005828 * Inherited from calling layer (may sleep).
Linus Torvalds1da177e2005-04-16 15:20:36 -07005829 *
5830 * RETURNS:
Tejun Heof3187192007-04-17 23:44:07 +09005831 * 0 on success, -errno otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005832 */
Tejun Heof3187192007-04-17 23:44:07 +09005833int ata_host_register(struct ata_host *host, struct scsi_host_template *sht)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005834{
Tejun Heof3187192007-04-17 23:44:07 +09005835 int i, rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005836
Jens Axboe69278f72018-05-11 12:51:10 -06005837 host->n_tags = clamp(sht->can_queue, 1, ATA_MAX_QUEUE);
Kevin Hao1871ee12014-07-12 12:08:24 +08005838
Tejun Heof3187192007-04-17 23:44:07 +09005839 /* host must have been started */
5840 if (!(host->flags & ATA_HOST_STARTED)) {
Joe Perchesa44fec12011-04-15 15:51:58 -07005841 dev_err(host->dev, "BUG: trying to register unstarted host\n");
Tejun Heof3187192007-04-17 23:44:07 +09005842 WARN_ON(1);
5843 return -EINVAL;
Alan Cox02f076a2006-09-26 17:35:32 +01005844 }
Tejun Heof0d36ef2007-01-20 16:00:28 +09005845
Tejun Heof3187192007-04-17 23:44:07 +09005846 /* Blow away unused ports. This happens when LLD can't
5847 * determine the exact number of ports to allocate at
5848 * allocation time.
5849 */
5850 for (i = host->n_ports; host->ports[i]; i++)
5851 kfree(host->ports[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005852
Tejun Heof3187192007-04-17 23:44:07 +09005853 /* give ports names and add SCSI hosts */
David Milburne628dc92013-05-14 13:48:40 -05005854 for (i = 0; i < host->n_ports; i++) {
Dan Williams85d67252012-03-10 23:28:46 -08005855 host->ports[i]->print_id = atomic_inc_return(&ata_print_id);
David Milburne628dc92013-05-14 13:48:40 -05005856 host->ports[i]->local_port_no = i + 1;
5857 }
Jeff Garzik4fca3772011-02-15 01:13:24 -05005858
Gwendal Grignoud9027472010-05-25 12:31:38 -07005859 /* Create associated sysfs transport objects */
5860 for (i = 0; i < host->n_ports; i++) {
5861 rc = ata_tport_add(host->dev,host->ports[i]);
5862 if (rc) {
5863 goto err_tadd;
5864 }
5865 }
5866
Tejun Heof3187192007-04-17 23:44:07 +09005867 rc = ata_scsi_add_hosts(host, sht);
Tejun Heoecef7252007-04-17 23:44:06 +09005868 if (rc)
Gwendal Grignoud9027472010-05-25 12:31:38 -07005869 goto err_tadd;
Tejun Heoecef7252007-04-17 23:44:06 +09005870
Tejun Heof3187192007-04-17 23:44:07 +09005871 /* set cable, sata_spd_limit and report */
Jeff Garzikcca39742006-08-24 03:19:22 -04005872 for (i = 0; i < host->n_ports; i++) {
5873 struct ata_port *ap = host->ports[i];
Tejun Heof3187192007-04-17 23:44:07 +09005874 unsigned long xfer_mask;
5875
5876 /* set SATA cable type if still unset */
5877 if (ap->cbl == ATA_CBL_NONE && (ap->flags & ATA_FLAG_SATA))
5878 ap->cbl = ATA_CBL_SATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005879
Tejun Heo5a04bf42006-05-31 18:27:38 +09005880 /* init sata_spd_limit to the current value */
Tejun Heo4fb37a22007-08-06 18:36:23 +09005881 sata_link_init_spd(&ap->link);
Tejun Heob1c72912008-07-31 17:02:43 +09005882 if (ap->slave_link)
5883 sata_link_init_spd(ap->slave_link);
Tejun Heo5a04bf42006-05-31 18:27:38 +09005884
Tejun Heocbcdd872007-08-18 13:14:55 +09005885 /* print per-port info to dmesg */
Tejun Heof3187192007-04-17 23:44:07 +09005886 xfer_mask = ata_pack_xfermask(ap->pio_mask, ap->mwdma_mask,
5887 ap->udma_mask);
5888
Tejun Heoabf6e8e2007-10-09 14:57:25 +09005889 if (!ata_port_is_dummy(ap)) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07005890 ata_port_info(ap, "%cATA max %s %s\n",
5891 (ap->flags & ATA_FLAG_SATA) ? 'S' : 'P',
5892 ata_mode_string(xfer_mask),
5893 ap->link.eh_info.desc);
Tejun Heoabf6e8e2007-10-09 14:57:25 +09005894 ata_ehi_clear_desc(&ap->link.eh_info);
5895 } else
Joe Perchesa9a79df2011-04-15 15:51:59 -07005896 ata_port_info(ap, "DUMMY\n");
Tejun Heof3187192007-04-17 23:44:07 +09005897 }
5898
Vegard Nossumf6005352009-04-08 18:19:39 +02005899 /* perform each probe asynchronously */
Tejun Heof3187192007-04-17 23:44:07 +09005900 for (i = 0; i < host->n_ports; i++) {
5901 struct ata_port *ap = host->ports[i];
Kai-Heng Fengb5292112020-06-03 15:48:19 +08005902 ap->cookie = async_schedule(async_port_probe, ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005903 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005904
Tejun Heof3187192007-04-17 23:44:07 +09005905 return 0;
Gwendal Grignoud9027472010-05-25 12:31:38 -07005906
5907 err_tadd:
5908 while (--i >= 0) {
5909 ata_tport_delete(host->ports[i]);
5910 }
5911 return rc;
5912
Tejun Heof3187192007-04-17 23:44:07 +09005913}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01005914EXPORT_SYMBOL_GPL(ata_host_register);
Tejun Heof3187192007-04-17 23:44:07 +09005915
5916/**
Tejun Heof5cda252007-04-17 23:44:07 +09005917 * ata_host_activate - start host, request IRQ and register it
5918 * @host: target ATA host
5919 * @irq: IRQ to request
5920 * @irq_handler: irq_handler used when requesting IRQ
5921 * @irq_flags: irq_flags used when requesting IRQ
5922 * @sht: scsi_host_template to use when registering the host
5923 *
5924 * After allocating an ATA host and initializing it, most libata
5925 * LLDs perform three steps to activate the host - start host,
Masanari Iidac9b55602016-04-13 23:36:27 +09005926 * request IRQ and register it. This helper takes necessary
Tejun Heof5cda252007-04-17 23:44:07 +09005927 * arguments and performs the three steps in one go.
5928 *
Paul Mundt3d46b2e2007-11-08 11:14:56 +09005929 * An invalid IRQ skips the IRQ registration and expects the host to
5930 * have set polling mode on the port. In this case, @irq_handler
5931 * should be NULL.
5932 *
Tejun Heof5cda252007-04-17 23:44:07 +09005933 * LOCKING:
5934 * Inherited from calling layer (may sleep).
5935 *
5936 * RETURNS:
5937 * 0 on success, -errno otherwise.
5938 */
5939int ata_host_activate(struct ata_host *host, int irq,
5940 irq_handler_t irq_handler, unsigned long irq_flags,
5941 struct scsi_host_template *sht)
5942{
Tejun Heocbcdd872007-08-18 13:14:55 +09005943 int i, rc;
Heiner Kallweit7e22c002015-12-06 21:56:33 +01005944 char *irq_desc;
Tejun Heof5cda252007-04-17 23:44:07 +09005945
5946 rc = ata_host_start(host);
5947 if (rc)
5948 return rc;
5949
Paul Mundt3d46b2e2007-11-08 11:14:56 +09005950 /* Special case for polling mode */
5951 if (!irq) {
5952 WARN_ON(irq_handler);
5953 return ata_host_register(host, sht);
5954 }
5955
Heiner Kallweit7e22c002015-12-06 21:56:33 +01005956 irq_desc = devm_kasprintf(host->dev, GFP_KERNEL, "%s[%s]",
5957 dev_driver_string(host->dev),
5958 dev_name(host->dev));
5959 if (!irq_desc)
5960 return -ENOMEM;
5961
Tejun Heof5cda252007-04-17 23:44:07 +09005962 rc = devm_request_irq(host->dev, irq, irq_handler, irq_flags,
Heiner Kallweit7e22c002015-12-06 21:56:33 +01005963 irq_desc, host);
Tejun Heof5cda252007-04-17 23:44:07 +09005964 if (rc)
5965 return rc;
5966
Tejun Heocbcdd872007-08-18 13:14:55 +09005967 for (i = 0; i < host->n_ports; i++)
5968 ata_port_desc(host->ports[i], "irq %d", irq);
Tejun Heo40318262007-07-03 01:38:47 +09005969
Tejun Heof5cda252007-04-17 23:44:07 +09005970 rc = ata_host_register(host, sht);
5971 /* if failed, just free the IRQ and leave ports alone */
5972 if (rc)
5973 devm_free_irq(host->dev, irq, host);
5974
5975 return rc;
5976}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01005977EXPORT_SYMBOL_GPL(ata_host_activate);
Tejun Heof5cda252007-04-17 23:44:07 +09005978
5979/**
Masanari Iidac9b55602016-04-13 23:36:27 +09005980 * ata_port_detach - Detach ATA port in preparation of device removal
Tejun Heo720ba122006-05-31 18:28:13 +09005981 * @ap: ATA port to be detached
5982 *
5983 * Detach all ATA devices and the associated SCSI devices of @ap;
5984 * then, remove the associated SCSI host. @ap is guaranteed to
5985 * be quiescent on return from this function.
5986 *
5987 * LOCKING:
5988 * Kernel thread context (may sleep).
5989 */
Adrian Bunk741b7762007-10-24 18:23:06 +02005990static void ata_port_detach(struct ata_port *ap)
Tejun Heo720ba122006-05-31 18:28:13 +09005991{
5992 unsigned long flags;
Levente Kurusaa6f9bf42014-05-06 15:57:48 +02005993 struct ata_link *link;
5994 struct ata_device *dev;
Tejun Heo720ba122006-05-31 18:28:13 +09005995
5996 if (!ap->ops->error_handler)
Tejun Heoc3cf30a2006-08-05 03:59:11 +09005997 goto skip_eh;
Tejun Heo720ba122006-05-31 18:28:13 +09005998
5999 /* tell EH we're leaving & flush EH */
Jeff Garzikba6a1302006-06-22 23:46:10 -04006000 spin_lock_irqsave(ap->lock, flags);
Tejun Heob51e9e52006-06-29 01:29:30 +09006001 ap->pflags |= ATA_PFLAG_UNLOADING;
Tejun Heoece180d2008-11-03 20:04:37 +09006002 ata_port_schedule_eh(ap);
Jeff Garzikba6a1302006-06-22 23:46:10 -04006003 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heo720ba122006-05-31 18:28:13 +09006004
Tejun Heoece180d2008-11-03 20:04:37 +09006005 /* wait till EH commits suicide */
Tejun Heo720ba122006-05-31 18:28:13 +09006006 ata_port_wait_eh(ap);
6007
Tejun Heoece180d2008-11-03 20:04:37 +09006008 /* it better be dead now */
6009 WARN_ON(!(ap->pflags & ATA_PFLAG_UNLOADED));
Tejun Heo720ba122006-05-31 18:28:13 +09006010
Tejun Heoafe2c512010-12-14 16:21:17 +01006011 cancel_delayed_work_sync(&ap->hotplug_task);
Tejun Heo720ba122006-05-31 18:28:13 +09006012
Tejun Heoc3cf30a2006-08-05 03:59:11 +09006013 skip_eh:
Levente Kurusaa6f9bf42014-05-06 15:57:48 +02006014 /* clean up zpodd on port removal */
6015 ata_for_each_link(link, ap, HOST_FIRST) {
6016 ata_for_each_dev(dev, link, ALL) {
6017 if (zpodd_dev_enabled(dev))
6018 zpodd_exit(dev);
6019 }
6020 }
Gwendal Grignoud9027472010-05-25 12:31:38 -07006021 if (ap->pmp_link) {
6022 int i;
6023 for (i = 0; i < SATA_PMP_MAX_PORTS; i++)
6024 ata_tlink_delete(&ap->pmp_link[i]);
6025 }
Tejun Heo720ba122006-05-31 18:28:13 +09006026 /* remove the associated SCSI host */
Jeff Garzikcca39742006-08-24 03:19:22 -04006027 scsi_remove_host(ap->scsi_host);
Rafael J. Wysockic5700762013-11-25 13:19:01 +01006028 ata_tport_delete(ap);
Tejun Heo720ba122006-05-31 18:28:13 +09006029}
6030
6031/**
Tejun Heo0529c1592007-01-20 16:00:26 +09006032 * ata_host_detach - Detach all ports of an ATA host
6033 * @host: Host to detach
6034 *
6035 * Detach all ports of @host.
6036 *
6037 * LOCKING:
6038 * Kernel thread context (may sleep).
6039 */
6040void ata_host_detach(struct ata_host *host)
6041{
6042 int i;
6043
Kai-Heng Fengb5292112020-06-03 15:48:19 +08006044 for (i = 0; i < host->n_ports; i++) {
6045 /* Ensure ata_port probe has completed */
6046 async_synchronize_cookie(host->ports[i]->cookie + 1);
Tejun Heo0529c1592007-01-20 16:00:26 +09006047 ata_port_detach(host->ports[i]);
Kai-Heng Fengb5292112020-06-03 15:48:19 +08006048 }
Tejun Heo562f0c22007-12-15 15:05:01 +09006049
6050 /* the host is dead now, dissociate ACPI */
6051 ata_acpi_dissociate(host);
Tejun Heo0529c1592007-01-20 16:00:26 +09006052}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01006053EXPORT_SYMBOL_GPL(ata_host_detach);
Tejun Heo0529c1592007-01-20 16:00:26 +09006054
Jeff Garzik374b1872005-08-30 05:42:52 -04006055#ifdef CONFIG_PCI
6056
Edward Falk0baab862005-06-02 18:17:13 -04006057/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07006058 * ata_pci_remove_one - PCI layer callback for device removal
6059 * @pdev: PCI device that was removed
6060 *
Tejun Heob878ca52007-01-20 16:00:28 +09006061 * PCI layer indicates to libata via this hook that hot-unplug or
6062 * module unload event has occurred. Detach all ports. Resource
6063 * release is handled via devres.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006064 *
6065 * LOCKING:
6066 * Inherited from PCI layer (may sleep).
6067 */
Tejun Heof0d36ef2007-01-20 16:00:28 +09006068void ata_pci_remove_one(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006069{
Brian Norris04a3f5b2012-12-03 10:34:41 -08006070 struct ata_host *host = pci_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006071
Tejun Heob878ca52007-01-20 16:00:28 +09006072 ata_host_detach(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006073}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01006074EXPORT_SYMBOL_GPL(ata_pci_remove_one);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006075
Prabhakar Kushwaha10a663a2020-01-25 03:37:29 +00006076void ata_pci_shutdown_one(struct pci_dev *pdev)
6077{
6078 struct ata_host *host = pci_get_drvdata(pdev);
6079 int i;
6080
6081 for (i = 0; i < host->n_ports; i++) {
6082 struct ata_port *ap = host->ports[i];
6083
6084 ap->pflags |= ATA_PFLAG_FROZEN;
6085
6086 /* Disable port interrupts */
6087 if (ap->ops->freeze)
6088 ap->ops->freeze(ap);
6089
6090 /* Stop the port DMA engines */
6091 if (ap->ops->port_stop)
6092 ap->ops->port_stop(ap);
6093 }
6094}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01006095EXPORT_SYMBOL_GPL(ata_pci_shutdown_one);
Prabhakar Kushwaha10a663a2020-01-25 03:37:29 +00006096
Linus Torvalds1da177e2005-04-16 15:20:36 -07006097/* move to PCI subsystem */
Jeff Garzik057ace52005-10-22 14:27:05 -04006098int pci_test_config_bits(struct pci_dev *pdev, const struct pci_bits *bits)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006099{
6100 unsigned long tmp = 0;
6101
6102 switch (bits->width) {
6103 case 1: {
6104 u8 tmp8 = 0;
6105 pci_read_config_byte(pdev, bits->reg, &tmp8);
6106 tmp = tmp8;
6107 break;
6108 }
6109 case 2: {
6110 u16 tmp16 = 0;
6111 pci_read_config_word(pdev, bits->reg, &tmp16);
6112 tmp = tmp16;
6113 break;
6114 }
6115 case 4: {
6116 u32 tmp32 = 0;
6117 pci_read_config_dword(pdev, bits->reg, &tmp32);
6118 tmp = tmp32;
6119 break;
6120 }
6121
6122 default:
6123 return -EINVAL;
6124 }
6125
6126 tmp &= bits->mask;
6127
6128 return (tmp == bits->val) ? 1 : 0;
6129}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01006130EXPORT_SYMBOL_GPL(pci_test_config_bits);
Jens Axboe9b847542006-01-06 09:28:07 +01006131
Tejun Heo6ffa01d2007-03-02 17:32:47 +09006132#ifdef CONFIG_PM
Tejun Heo3c5100c2006-07-26 16:58:33 +09006133void ata_pci_device_do_suspend(struct pci_dev *pdev, pm_message_t mesg)
Jens Axboe9b847542006-01-06 09:28:07 +01006134{
6135 pci_save_state(pdev);
Tejun Heo4c90d972007-02-20 18:14:48 +09006136 pci_disable_device(pdev);
Tejun Heo500530f2006-07-03 16:07:27 +09006137
Rafael J. Wysocki3a2d5b72008-02-23 19:13:25 +01006138 if (mesg.event & PM_EVENT_SLEEP)
Tejun Heo500530f2006-07-03 16:07:27 +09006139 pci_set_power_state(pdev, PCI_D3hot);
Jens Axboe9b847542006-01-06 09:28:07 +01006140}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01006141EXPORT_SYMBOL_GPL(ata_pci_device_do_suspend);
Jens Axboe9b847542006-01-06 09:28:07 +01006142
Tejun Heo553c4aa2006-12-26 19:39:50 +09006143int ata_pci_device_do_resume(struct pci_dev *pdev)
Jens Axboe9b847542006-01-06 09:28:07 +01006144{
Tejun Heo553c4aa2006-12-26 19:39:50 +09006145 int rc;
6146
Jens Axboe9b847542006-01-06 09:28:07 +01006147 pci_set_power_state(pdev, PCI_D0);
6148 pci_restore_state(pdev);
Tejun Heo553c4aa2006-12-26 19:39:50 +09006149
Tejun Heob878ca52007-01-20 16:00:28 +09006150 rc = pcim_enable_device(pdev);
Tejun Heo553c4aa2006-12-26 19:39:50 +09006151 if (rc) {
Joe Perchesa44fec12011-04-15 15:51:58 -07006152 dev_err(&pdev->dev,
6153 "failed to enable device after resume (%d)\n", rc);
Tejun Heo553c4aa2006-12-26 19:39:50 +09006154 return rc;
6155 }
6156
Jens Axboe9b847542006-01-06 09:28:07 +01006157 pci_set_master(pdev);
Tejun Heo553c4aa2006-12-26 19:39:50 +09006158 return 0;
Tejun Heo500530f2006-07-03 16:07:27 +09006159}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01006160EXPORT_SYMBOL_GPL(ata_pci_device_do_resume);
Tejun Heo500530f2006-07-03 16:07:27 +09006161
Tejun Heo3c5100c2006-07-26 16:58:33 +09006162int ata_pci_device_suspend(struct pci_dev *pdev, pm_message_t mesg)
Tejun Heo500530f2006-07-03 16:07:27 +09006163{
Brian Norris04a3f5b2012-12-03 10:34:41 -08006164 struct ata_host *host = pci_get_drvdata(pdev);
Tejun Heo500530f2006-07-03 16:07:27 +09006165 int rc = 0;
6166
Jeff Garzikcca39742006-08-24 03:19:22 -04006167 rc = ata_host_suspend(host, mesg);
Tejun Heo500530f2006-07-03 16:07:27 +09006168 if (rc)
6169 return rc;
6170
Tejun Heo3c5100c2006-07-26 16:58:33 +09006171 ata_pci_device_do_suspend(pdev, mesg);
Tejun Heo500530f2006-07-03 16:07:27 +09006172
6173 return 0;
6174}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01006175EXPORT_SYMBOL_GPL(ata_pci_device_suspend);
Tejun Heo500530f2006-07-03 16:07:27 +09006176
6177int ata_pci_device_resume(struct pci_dev *pdev)
6178{
Brian Norris04a3f5b2012-12-03 10:34:41 -08006179 struct ata_host *host = pci_get_drvdata(pdev);
Tejun Heo553c4aa2006-12-26 19:39:50 +09006180 int rc;
Tejun Heo500530f2006-07-03 16:07:27 +09006181
Tejun Heo553c4aa2006-12-26 19:39:50 +09006182 rc = ata_pci_device_do_resume(pdev);
6183 if (rc == 0)
6184 ata_host_resume(host);
6185 return rc;
Jens Axboe9b847542006-01-06 09:28:07 +01006186}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01006187EXPORT_SYMBOL_GPL(ata_pci_device_resume);
Tejun Heo6ffa01d2007-03-02 17:32:47 +09006188#endif /* CONFIG_PM */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006189#endif /* CONFIG_PCI */
6190
Brian Norrisb7db04d2012-11-02 12:29:32 -07006191/**
6192 * ata_platform_remove_one - Platform layer callback for device removal
6193 * @pdev: Platform device that was removed
6194 *
6195 * Platform layer indicates to libata via this hook that hot-unplug or
6196 * module unload event has occurred. Detach all ports. Resource
6197 * release is handled via devres.
6198 *
6199 * LOCKING:
6200 * Inherited from platform layer (may sleep).
6201 */
6202int ata_platform_remove_one(struct platform_device *pdev)
6203{
6204 struct ata_host *host = platform_get_drvdata(pdev);
6205
6206 ata_host_detach(host);
6207
6208 return 0;
6209}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01006210EXPORT_SYMBOL_GPL(ata_platform_remove_one);
Brian Norrisb7db04d2012-11-02 12:29:32 -07006211
Bartlomiej Zolnierkiewiczbf89b0b2020-03-26 16:58:22 +01006212#ifdef CONFIG_ATA_FORCE
Tejun Heo33267322008-02-13 09:15:09 +09006213static int __init ata_parse_force_one(char **cur,
6214 struct ata_force_ent *force_ent,
6215 const char **reason)
6216{
Rasmus Villemoes0f5f2642015-06-09 15:33:19 +02006217 static const struct ata_force_param force_tbl[] __initconst = {
Tejun Heo33267322008-02-13 09:15:09 +09006218 { "40c", .cbl = ATA_CBL_PATA40 },
6219 { "80c", .cbl = ATA_CBL_PATA80 },
6220 { "short40c", .cbl = ATA_CBL_PATA40_SHORT },
6221 { "unk", .cbl = ATA_CBL_PATA_UNK },
6222 { "ign", .cbl = ATA_CBL_PATA_IGN },
6223 { "sata", .cbl = ATA_CBL_SATA },
6224 { "1.5Gbps", .spd_limit = 1 },
6225 { "3.0Gbps", .spd_limit = 2 },
6226 { "noncq", .horkage_on = ATA_HORKAGE_NONCQ },
6227 { "ncq", .horkage_off = ATA_HORKAGE_NONCQ },
Martin K. Petersend7b16e42015-05-04 21:54:18 -04006228 { "noncqtrim", .horkage_on = ATA_HORKAGE_NO_NCQ_TRIM },
6229 { "ncqtrim", .horkage_off = ATA_HORKAGE_NO_NCQ_TRIM },
Kate Hsuan7a8526a2021-09-03 17:44:11 +08006230 { "noncqati", .horkage_on = ATA_HORKAGE_NO_NCQ_ON_ATI },
6231 { "ncqati", .horkage_off = ATA_HORKAGE_NO_NCQ_ON_ATI },
Tejun Heo43c9c592010-05-23 12:59:11 +02006232 { "dump_id", .horkage_on = ATA_HORKAGE_DUMP_ID },
Tejun Heo33267322008-02-13 09:15:09 +09006233 { "pio0", .xfer_mask = 1 << (ATA_SHIFT_PIO + 0) },
6234 { "pio1", .xfer_mask = 1 << (ATA_SHIFT_PIO + 1) },
6235 { "pio2", .xfer_mask = 1 << (ATA_SHIFT_PIO + 2) },
6236 { "pio3", .xfer_mask = 1 << (ATA_SHIFT_PIO + 3) },
6237 { "pio4", .xfer_mask = 1 << (ATA_SHIFT_PIO + 4) },
6238 { "pio5", .xfer_mask = 1 << (ATA_SHIFT_PIO + 5) },
6239 { "pio6", .xfer_mask = 1 << (ATA_SHIFT_PIO + 6) },
6240 { "mwdma0", .xfer_mask = 1 << (ATA_SHIFT_MWDMA + 0) },
6241 { "mwdma1", .xfer_mask = 1 << (ATA_SHIFT_MWDMA + 1) },
6242 { "mwdma2", .xfer_mask = 1 << (ATA_SHIFT_MWDMA + 2) },
6243 { "mwdma3", .xfer_mask = 1 << (ATA_SHIFT_MWDMA + 3) },
6244 { "mwdma4", .xfer_mask = 1 << (ATA_SHIFT_MWDMA + 4) },
6245 { "udma0", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 0) },
6246 { "udma16", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 0) },
6247 { "udma/16", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 0) },
6248 { "udma1", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 1) },
6249 { "udma25", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 1) },
6250 { "udma/25", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 1) },
6251 { "udma2", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 2) },
6252 { "udma33", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 2) },
6253 { "udma/33", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 2) },
6254 { "udma3", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 3) },
6255 { "udma44", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 3) },
6256 { "udma/44", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 3) },
6257 { "udma4", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 4) },
6258 { "udma66", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 4) },
6259 { "udma/66", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 4) },
6260 { "udma5", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 5) },
6261 { "udma100", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 5) },
6262 { "udma/100", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 5) },
6263 { "udma6", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 6) },
6264 { "udma133", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 6) },
6265 { "udma/133", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 6) },
6266 { "udma7", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 7) },
Tejun Heo05944bd2008-08-13 20:19:09 +09006267 { "nohrst", .lflags = ATA_LFLAG_NO_HRST },
6268 { "nosrst", .lflags = ATA_LFLAG_NO_SRST },
6269 { "norst", .lflags = ATA_LFLAG_NO_HRST | ATA_LFLAG_NO_SRST },
Dan Williamsca6d43b2012-06-21 23:41:41 -07006270 { "rstonce", .lflags = ATA_LFLAG_RST_ONCE },
Vincent Pelletier966fbe12013-05-21 22:30:58 +02006271 { "atapi_dmadir", .horkage_on = ATA_HORKAGE_ATAPI_DMADIR },
Robin H. Johnsonb8bd6dc2013-12-16 09:31:19 -08006272 { "disable", .horkage_on = ATA_HORKAGE_DISABLE },
Tejun Heo33267322008-02-13 09:15:09 +09006273 };
6274 char *start = *cur, *p = *cur;
6275 char *id, *val, *endp;
6276 const struct ata_force_param *match_fp = NULL;
6277 int nr_matches = 0, i;
6278
6279 /* find where this param ends and update *cur */
6280 while (*p != '\0' && *p != ',')
6281 p++;
6282
6283 if (*p == '\0')
6284 *cur = p;
6285 else
6286 *cur = p + 1;
6287
6288 *p = '\0';
6289
6290 /* parse */
6291 p = strchr(start, ':');
6292 if (!p) {
6293 val = strstrip(start);
6294 goto parse_val;
6295 }
6296 *p = '\0';
6297
6298 id = strstrip(start);
6299 val = strstrip(p + 1);
6300
6301 /* parse id */
6302 p = strchr(id, '.');
6303 if (p) {
6304 *p++ = '\0';
6305 force_ent->device = simple_strtoul(p, &endp, 10);
6306 if (p == endp || *endp != '\0') {
6307 *reason = "invalid device";
6308 return -EINVAL;
6309 }
6310 }
6311
6312 force_ent->port = simple_strtoul(id, &endp, 10);
Tejun Heof7cf69a2017-05-31 14:26:26 -04006313 if (id == endp || *endp != '\0') {
Tejun Heo33267322008-02-13 09:15:09 +09006314 *reason = "invalid port/link";
6315 return -EINVAL;
6316 }
6317
6318 parse_val:
6319 /* parse val, allow shortcuts so that both 1.5 and 1.5Gbps work */
6320 for (i = 0; i < ARRAY_SIZE(force_tbl); i++) {
6321 const struct ata_force_param *fp = &force_tbl[i];
6322
6323 if (strncasecmp(val, fp->name, strlen(val)))
6324 continue;
6325
6326 nr_matches++;
6327 match_fp = fp;
6328
6329 if (strcasecmp(val, fp->name) == 0) {
6330 nr_matches = 1;
6331 break;
6332 }
6333 }
6334
6335 if (!nr_matches) {
6336 *reason = "unknown value";
6337 return -EINVAL;
6338 }
6339 if (nr_matches > 1) {
Arvind Yadav9de55352017-10-28 23:51:47 +05306340 *reason = "ambiguous value";
Tejun Heo33267322008-02-13 09:15:09 +09006341 return -EINVAL;
6342 }
6343
6344 force_ent->param = *match_fp;
6345
6346 return 0;
6347}
6348
6349static void __init ata_parse_force_param(void)
6350{
6351 int idx = 0, size = 1;
6352 int last_port = -1, last_device = -1;
6353 char *p, *cur, *next;
6354
6355 /* calculate maximum number of params and allocate force_tbl */
6356 for (p = ata_force_param_buf; *p; p++)
6357 if (*p == ',')
6358 size++;
6359
Kees Cook6396bb22018-06-12 14:03:40 -07006360 ata_force_tbl = kcalloc(size, sizeof(ata_force_tbl[0]), GFP_KERNEL);
Tejun Heo33267322008-02-13 09:15:09 +09006361 if (!ata_force_tbl) {
6362 printk(KERN_WARNING "ata: failed to extend force table, "
6363 "libata.force ignored\n");
6364 return;
6365 }
6366
6367 /* parse and populate the table */
6368 for (cur = ata_force_param_buf; *cur != '\0'; cur = next) {
6369 const char *reason = "";
6370 struct ata_force_ent te = { .port = -1, .device = -1 };
6371
6372 next = cur;
6373 if (ata_parse_force_one(&next, &te, &reason)) {
6374 printk(KERN_WARNING "ata: failed to parse force "
6375 "parameter \"%s\" (%s)\n",
6376 cur, reason);
6377 continue;
6378 }
6379
6380 if (te.port == -1) {
6381 te.port = last_port;
6382 te.device = last_device;
6383 }
6384
6385 ata_force_tbl[idx++] = te;
6386
6387 last_port = te.port;
6388 last_device = te.device;
6389 }
6390
6391 ata_force_tbl_size = idx;
6392}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006393
Bartlomiej Zolnierkiewiczbf89b0b2020-03-26 16:58:22 +01006394static void ata_free_force_param(void)
6395{
6396 kfree(ata_force_tbl);
6397}
6398#else
6399static inline void ata_parse_force_param(void) { }
6400static inline void ata_free_force_param(void) { }
6401#endif
6402
Linus Torvalds1da177e2005-04-16 15:20:36 -07006403static int __init ata_init(void)
6404{
Gwendal Grignoud9027472010-05-25 12:31:38 -07006405 int rc;
Tejun Heo270390e2010-05-10 21:41:35 +02006406
Tejun Heo33267322008-02-13 09:15:09 +09006407 ata_parse_force_param();
6408
Tejun Heo270390e2010-05-10 21:41:35 +02006409 rc = ata_sff_init();
Tejun Heoad72cf92010-07-02 10:03:52 +02006410 if (rc) {
Bartlomiej Zolnierkiewiczbf89b0b2020-03-26 16:58:22 +01006411 ata_free_force_param();
Tejun Heoad72cf92010-07-02 10:03:52 +02006412 return rc;
6413 }
Tejun Heo453b07a2006-05-31 18:27:42 +09006414
Gwendal Grignoud9027472010-05-25 12:31:38 -07006415 libata_transport_init();
6416 ata_scsi_transport_template = ata_attach_transport();
6417 if (!ata_scsi_transport_template) {
6418 ata_sff_exit();
6419 rc = -ENOMEM;
6420 goto err_out;
Jeff Garzik4fca3772011-02-15 01:13:24 -05006421 }
Gwendal Grignoud9027472010-05-25 12:31:38 -07006422
Linus Torvalds1da177e2005-04-16 15:20:36 -07006423 printk(KERN_DEBUG "libata version " DRV_VERSION " loaded.\n");
6424 return 0;
Gwendal Grignoud9027472010-05-25 12:31:38 -07006425
6426err_out:
6427 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006428}
6429
6430static void __exit ata_exit(void)
6431{
Gwendal Grignoud9027472010-05-25 12:31:38 -07006432 ata_release_transport(ata_scsi_transport_template);
6433 libata_transport_exit();
Tejun Heo270390e2010-05-10 21:41:35 +02006434 ata_sff_exit();
Bartlomiej Zolnierkiewiczbf89b0b2020-03-26 16:58:22 +01006435 ata_free_force_param();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006436}
6437
Brian Kinga4625082006-11-13 16:32:36 -06006438subsys_initcall(ata_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006439module_exit(ata_exit);
6440
Akinobu Mita9990b6f2010-04-12 21:11:41 +09006441static DEFINE_RATELIMIT_STATE(ratelimit, HZ / 5, 1);
Jeff Garzik67846b32005-10-05 02:58:32 -04006442
6443int ata_ratelimit(void)
6444{
Akinobu Mita9990b6f2010-04-12 21:11:41 +09006445 return __ratelimit(&ratelimit);
Jeff Garzik67846b32005-10-05 02:58:32 -04006446}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01006447EXPORT_SYMBOL_GPL(ata_ratelimit);
Jeff Garzik67846b32005-10-05 02:58:32 -04006448
Tejun Heoc0c362b2010-09-06 17:57:14 +02006449/**
6450 * ata_msleep - ATA EH owner aware msleep
6451 * @ap: ATA port to attribute the sleep to
6452 * @msecs: duration to sleep in milliseconds
6453 *
6454 * Sleeps @msecs. If the current task is owner of @ap's EH, the
6455 * ownership is released before going to sleep and reacquired
6456 * after the sleep is complete. IOW, other ports sharing the
6457 * @ap->host will be allowed to own the EH while this task is
6458 * sleeping.
6459 *
6460 * LOCKING:
6461 * Might sleep.
6462 */
Tejun Heo97750ce2010-09-06 17:56:29 +02006463void ata_msleep(struct ata_port *ap, unsigned int msecs)
6464{
Tejun Heoc0c362b2010-09-06 17:57:14 +02006465 bool owns_eh = ap && ap->host->eh_owner == current;
6466
6467 if (owns_eh)
6468 ata_eh_release(ap);
6469
Anil Veliyankara Madam848c3922016-01-07 21:18:52 -08006470 if (msecs < 20) {
6471 unsigned long usecs = msecs * USEC_PER_MSEC;
6472 usleep_range(usecs, usecs + 50);
6473 } else {
6474 msleep(msecs);
6475 }
Tejun Heoc0c362b2010-09-06 17:57:14 +02006476
6477 if (owns_eh)
6478 ata_eh_acquire(ap);
Tejun Heo97750ce2010-09-06 17:56:29 +02006479}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01006480EXPORT_SYMBOL_GPL(ata_msleep);
Tejun Heo97750ce2010-09-06 17:56:29 +02006481
Tejun Heoc22daff2006-04-11 22:22:29 +09006482/**
6483 * ata_wait_register - wait until register value changes
Tejun Heo97750ce2010-09-06 17:56:29 +02006484 * @ap: ATA port to wait register for, can be NULL
Tejun Heoc22daff2006-04-11 22:22:29 +09006485 * @reg: IO-mapped register
6486 * @mask: Mask to apply to read register value
6487 * @val: Wait condition
Tejun Heo341c2c92008-05-20 02:17:51 +09006488 * @interval: polling interval in milliseconds
6489 * @timeout: timeout in milliseconds
Tejun Heoc22daff2006-04-11 22:22:29 +09006490 *
6491 * Waiting for some bits of register to change is a common
6492 * operation for ATA controllers. This function reads 32bit LE
6493 * IO-mapped register @reg and tests for the following condition.
6494 *
6495 * (*@reg & mask) != val
6496 *
6497 * If the condition is met, it returns; otherwise, the process is
6498 * repeated after @interval_msec until timeout.
6499 *
6500 * LOCKING:
6501 * Kernel thread context (may sleep)
6502 *
6503 * RETURNS:
6504 * The final register value.
6505 */
Tejun Heo97750ce2010-09-06 17:56:29 +02006506u32 ata_wait_register(struct ata_port *ap, void __iomem *reg, u32 mask, u32 val,
Tejun Heo341c2c92008-05-20 02:17:51 +09006507 unsigned long interval, unsigned long timeout)
Tejun Heoc22daff2006-04-11 22:22:29 +09006508{
Tejun Heo341c2c92008-05-20 02:17:51 +09006509 unsigned long deadline;
Tejun Heoc22daff2006-04-11 22:22:29 +09006510 u32 tmp;
6511
6512 tmp = ioread32(reg);
6513
6514 /* Calculate timeout _after_ the first read to make sure
6515 * preceding writes reach the controller before starting to
6516 * eat away the timeout.
6517 */
Tejun Heo341c2c92008-05-20 02:17:51 +09006518 deadline = ata_deadline(jiffies, timeout);
Tejun Heoc22daff2006-04-11 22:22:29 +09006519
Tejun Heo341c2c92008-05-20 02:17:51 +09006520 while ((tmp & mask) == val && time_before(jiffies, deadline)) {
Tejun Heo97750ce2010-09-06 17:56:29 +02006521 ata_msleep(ap, interval);
Tejun Heoc22daff2006-04-11 22:22:29 +09006522 tmp = ioread32(reg);
6523 }
6524
6525 return tmp;
6526}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01006527EXPORT_SYMBOL_GPL(ata_wait_register);
Tejun Heoc22daff2006-04-11 22:22:29 +09006528
Linus Torvalds1da177e2005-04-16 15:20:36 -07006529/*
Tejun Heodd5b06c2006-08-10 16:59:12 +09006530 * Dummy port_ops
6531 */
Tejun Heodd5b06c2006-08-10 16:59:12 +09006532static unsigned int ata_dummy_qc_issue(struct ata_queued_cmd *qc)
6533{
6534 return AC_ERR_SYSTEM;
6535}
6536
Tejun Heo182d7bb2008-04-07 22:47:21 +09006537static void ata_dummy_error_handler(struct ata_port *ap)
6538{
6539 /* truly dummy */
6540}
6541
Tejun Heo029cfd62008-03-25 12:22:49 +09006542struct ata_port_operations ata_dummy_port_ops = {
Tejun Heodd5b06c2006-08-10 16:59:12 +09006543 .qc_prep = ata_noop_qc_prep,
6544 .qc_issue = ata_dummy_qc_issue,
Tejun Heo182d7bb2008-04-07 22:47:21 +09006545 .error_handler = ata_dummy_error_handler,
Dan Williamse4a9c372012-06-21 23:25:27 -07006546 .sched_eh = ata_std_sched_eh,
6547 .end_eh = ata_std_end_eh,
Tejun Heodd5b06c2006-08-10 16:59:12 +09006548};
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01006549EXPORT_SYMBOL_GPL(ata_dummy_port_ops);
Tejun Heodd5b06c2006-08-10 16:59:12 +09006550
Tejun Heo21b0ad42007-04-17 23:44:07 +09006551const struct ata_port_info ata_dummy_port_info = {
6552 .port_ops = &ata_dummy_port_ops,
6553};
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01006554EXPORT_SYMBOL_GPL(ata_dummy_port_info);
Tejun Heo21b0ad42007-04-17 23:44:07 +09006555
Tejun Heodd5b06c2006-08-10 16:59:12 +09006556/*
Joe Perchesa9a79df2011-04-15 15:51:59 -07006557 * Utility print functions
6558 */
Joe Perchesd7bead12014-09-22 09:52:18 -07006559void ata_port_printk(const struct ata_port *ap, const char *level,
6560 const char *fmt, ...)
Joe Perchesa9a79df2011-04-15 15:51:59 -07006561{
6562 struct va_format vaf;
6563 va_list args;
Joe Perchesa9a79df2011-04-15 15:51:59 -07006564
6565 va_start(args, fmt);
6566
6567 vaf.fmt = fmt;
6568 vaf.va = &args;
6569
Joe Perchesd7bead12014-09-22 09:52:18 -07006570 printk("%sata%u: %pV", level, ap->print_id, &vaf);
Joe Perchesa9a79df2011-04-15 15:51:59 -07006571
6572 va_end(args);
Joe Perchesa9a79df2011-04-15 15:51:59 -07006573}
6574EXPORT_SYMBOL(ata_port_printk);
6575
Joe Perchesd7bead12014-09-22 09:52:18 -07006576void ata_link_printk(const struct ata_link *link, const char *level,
6577 const char *fmt, ...)
Joe Perchesa9a79df2011-04-15 15:51:59 -07006578{
6579 struct va_format vaf;
6580 va_list args;
Joe Perchesa9a79df2011-04-15 15:51:59 -07006581
6582 va_start(args, fmt);
6583
6584 vaf.fmt = fmt;
6585 vaf.va = &args;
6586
6587 if (sata_pmp_attached(link->ap) || link->ap->slave_link)
Joe Perchesd7bead12014-09-22 09:52:18 -07006588 printk("%sata%u.%02u: %pV",
6589 level, link->ap->print_id, link->pmp, &vaf);
Joe Perchesa9a79df2011-04-15 15:51:59 -07006590 else
Joe Perchesd7bead12014-09-22 09:52:18 -07006591 printk("%sata%u: %pV",
6592 level, link->ap->print_id, &vaf);
Joe Perchesa9a79df2011-04-15 15:51:59 -07006593
6594 va_end(args);
Joe Perchesa9a79df2011-04-15 15:51:59 -07006595}
6596EXPORT_SYMBOL(ata_link_printk);
6597
Joe Perchesd7bead12014-09-22 09:52:18 -07006598void ata_dev_printk(const struct ata_device *dev, const char *level,
Joe Perchesa9a79df2011-04-15 15:51:59 -07006599 const char *fmt, ...)
6600{
6601 struct va_format vaf;
6602 va_list args;
Joe Perchesa9a79df2011-04-15 15:51:59 -07006603
6604 va_start(args, fmt);
6605
6606 vaf.fmt = fmt;
6607 vaf.va = &args;
6608
Joe Perchesd7bead12014-09-22 09:52:18 -07006609 printk("%sata%u.%02u: %pV",
6610 level, dev->link->ap->print_id, dev->link->pmp + dev->devno,
6611 &vaf);
Joe Perchesa9a79df2011-04-15 15:51:59 -07006612
6613 va_end(args);
Joe Perchesa9a79df2011-04-15 15:51:59 -07006614}
6615EXPORT_SYMBOL(ata_dev_printk);
6616
Joe Perches06296a12011-04-15 15:52:00 -07006617void ata_print_version(const struct device *dev, const char *version)
6618{
6619 dev_printk(KERN_DEBUG, dev, "version %s\n", version);
6620}
6621EXPORT_SYMBOL(ata_print_version);