blob: d316fbf13309153e4098310bf321794530cb6049 [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
Tejun Heobd056d72006-11-14 22:47:10 +0900767 /* Check whether the converted CHS can fit.
768 Cylinder: 0-65535
769 Head: 0-15
770 Sector: 1-255*/
771 if ((cyl >> 16) || (head >> 4) || (sect >> 8) || (!sect))
772 return -ERANGE;
773
774 tf->nsect = n_block & 0xff; /* Sector count 0 means 256 sectors */
775 tf->lbal = sect;
776 tf->lbam = cyl;
777 tf->lbah = cyl >> 8;
778 tf->device |= head;
779 }
780
781 return 0;
782}
783
784/**
Tejun Heocb95d562006-03-06 04:31:56 +0900785 * ata_pack_xfermask - Pack pio, mwdma and udma masks into xfer_mask
786 * @pio_mask: pio_mask
787 * @mwdma_mask: mwdma_mask
788 * @udma_mask: udma_mask
789 *
790 * Pack @pio_mask, @mwdma_mask and @udma_mask into a single
791 * unsigned int xfer_mask.
792 *
793 * LOCKING:
794 * None.
795 *
796 * RETURNS:
797 * Packed xfer_mask.
798 */
Tejun Heo7dc951a2007-11-27 19:43:42 +0900799unsigned long ata_pack_xfermask(unsigned long pio_mask,
800 unsigned long mwdma_mask,
801 unsigned long udma_mask)
Tejun Heocb95d562006-03-06 04:31:56 +0900802{
803 return ((pio_mask << ATA_SHIFT_PIO) & ATA_MASK_PIO) |
804 ((mwdma_mask << ATA_SHIFT_MWDMA) & ATA_MASK_MWDMA) |
805 ((udma_mask << ATA_SHIFT_UDMA) & ATA_MASK_UDMA);
806}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +0100807EXPORT_SYMBOL_GPL(ata_pack_xfermask);
Tejun Heocb95d562006-03-06 04:31:56 +0900808
Tejun Heoc0489e42006-03-24 14:07:49 +0900809/**
810 * ata_unpack_xfermask - Unpack xfer_mask into pio, mwdma and udma masks
811 * @xfer_mask: xfer_mask to unpack
812 * @pio_mask: resulting pio_mask
813 * @mwdma_mask: resulting mwdma_mask
814 * @udma_mask: resulting udma_mask
815 *
816 * Unpack @xfer_mask into @pio_mask, @mwdma_mask and @udma_mask.
Masanari Iidac9b55602016-04-13 23:36:27 +0900817 * Any NULL destination masks will be ignored.
Tejun Heoc0489e42006-03-24 14:07:49 +0900818 */
Tejun Heo7dc951a2007-11-27 19:43:42 +0900819void ata_unpack_xfermask(unsigned long xfer_mask, unsigned long *pio_mask,
820 unsigned long *mwdma_mask, unsigned long *udma_mask)
Tejun Heoc0489e42006-03-24 14:07:49 +0900821{
822 if (pio_mask)
823 *pio_mask = (xfer_mask & ATA_MASK_PIO) >> ATA_SHIFT_PIO;
824 if (mwdma_mask)
825 *mwdma_mask = (xfer_mask & ATA_MASK_MWDMA) >> ATA_SHIFT_MWDMA;
826 if (udma_mask)
827 *udma_mask = (xfer_mask & ATA_MASK_UDMA) >> ATA_SHIFT_UDMA;
828}
829
Tejun Heocb95d562006-03-06 04:31:56 +0900830static const struct ata_xfer_ent {
Tejun Heobe9a50c2006-03-31 22:48:52 +0900831 int shift, bits;
Tejun Heocb95d562006-03-06 04:31:56 +0900832 u8 base;
833} ata_xfer_tbl[] = {
Tejun Heo70cd0712007-11-27 19:43:40 +0900834 { ATA_SHIFT_PIO, ATA_NR_PIO_MODES, XFER_PIO_0 },
835 { ATA_SHIFT_MWDMA, ATA_NR_MWDMA_MODES, XFER_MW_DMA_0 },
836 { ATA_SHIFT_UDMA, ATA_NR_UDMA_MODES, XFER_UDMA_0 },
Tejun Heocb95d562006-03-06 04:31:56 +0900837 { -1, },
838};
839
840/**
841 * ata_xfer_mask2mode - Find matching XFER_* for the given xfer_mask
842 * @xfer_mask: xfer_mask of interest
843 *
844 * Return matching XFER_* value for @xfer_mask. Only the highest
845 * bit of @xfer_mask is considered.
846 *
847 * LOCKING:
848 * None.
849 *
850 * RETURNS:
Tejun Heo70cd0712007-11-27 19:43:40 +0900851 * Matching XFER_* value, 0xff if no match found.
Tejun Heocb95d562006-03-06 04:31:56 +0900852 */
Tejun Heo7dc951a2007-11-27 19:43:42 +0900853u8 ata_xfer_mask2mode(unsigned long xfer_mask)
Tejun Heocb95d562006-03-06 04:31:56 +0900854{
855 int highbit = fls(xfer_mask) - 1;
856 const struct ata_xfer_ent *ent;
857
858 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
859 if (highbit >= ent->shift && highbit < ent->shift + ent->bits)
860 return ent->base + highbit - ent->shift;
Tejun Heo70cd0712007-11-27 19:43:40 +0900861 return 0xff;
Tejun Heocb95d562006-03-06 04:31:56 +0900862}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +0100863EXPORT_SYMBOL_GPL(ata_xfer_mask2mode);
Tejun Heocb95d562006-03-06 04:31:56 +0900864
865/**
866 * ata_xfer_mode2mask - Find matching xfer_mask for XFER_*
867 * @xfer_mode: XFER_* of interest
868 *
869 * Return matching xfer_mask for @xfer_mode.
870 *
871 * LOCKING:
872 * None.
873 *
874 * RETURNS:
875 * Matching xfer_mask, 0 if no match found.
876 */
Tejun Heo7dc951a2007-11-27 19:43:42 +0900877unsigned long ata_xfer_mode2mask(u8 xfer_mode)
Tejun Heocb95d562006-03-06 04:31:56 +0900878{
879 const struct ata_xfer_ent *ent;
880
881 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
882 if (xfer_mode >= ent->base && xfer_mode < ent->base + ent->bits)
Tejun Heo70cd0712007-11-27 19:43:40 +0900883 return ((2 << (ent->shift + xfer_mode - ent->base)) - 1)
884 & ~((1 << ent->shift) - 1);
Tejun Heocb95d562006-03-06 04:31:56 +0900885 return 0;
886}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +0100887EXPORT_SYMBOL_GPL(ata_xfer_mode2mask);
Tejun Heocb95d562006-03-06 04:31:56 +0900888
889/**
890 * ata_xfer_mode2shift - Find matching xfer_shift for XFER_*
891 * @xfer_mode: XFER_* of interest
892 *
893 * Return matching xfer_shift for @xfer_mode.
894 *
895 * LOCKING:
896 * None.
897 *
898 * RETURNS:
899 * Matching xfer_shift, -1 if no match found.
900 */
Tejun Heo7dc951a2007-11-27 19:43:42 +0900901int ata_xfer_mode2shift(unsigned long xfer_mode)
Tejun Heocb95d562006-03-06 04:31:56 +0900902{
903 const struct ata_xfer_ent *ent;
904
905 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
906 if (xfer_mode >= ent->base && xfer_mode < ent->base + ent->bits)
907 return ent->shift;
908 return -1;
909}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +0100910EXPORT_SYMBOL_GPL(ata_xfer_mode2shift);
Tejun Heocb95d562006-03-06 04:31:56 +0900911
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912/**
Tejun Heo1da7b0d2006-03-06 04:31:56 +0900913 * ata_mode_string - convert xfer_mask to string
914 * @xfer_mask: mask of bits supported; only highest bit counts.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 *
916 * Determine string which represents the highest speed
Tejun Heo1da7b0d2006-03-06 04:31:56 +0900917 * (highest bit in @modemask).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 *
919 * LOCKING:
920 * None.
921 *
922 * RETURNS:
923 * Constant C string representing highest speed listed in
Tejun Heo1da7b0d2006-03-06 04:31:56 +0900924 * @mode_mask, or the constant C string "<n/a>".
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 */
Tejun Heo7dc951a2007-11-27 19:43:42 +0900926const char *ata_mode_string(unsigned long xfer_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927{
Tejun Heo75f554b2006-03-06 04:31:57 +0900928 static const char * const xfer_mode_str[] = {
929 "PIO0",
930 "PIO1",
931 "PIO2",
932 "PIO3",
933 "PIO4",
Alan Coxb352e572006-08-10 18:52:12 +0100934 "PIO5",
935 "PIO6",
Tejun Heo75f554b2006-03-06 04:31:57 +0900936 "MWDMA0",
937 "MWDMA1",
938 "MWDMA2",
Alan Coxb352e572006-08-10 18:52:12 +0100939 "MWDMA3",
940 "MWDMA4",
Tejun Heo75f554b2006-03-06 04:31:57 +0900941 "UDMA/16",
942 "UDMA/25",
943 "UDMA/33",
944 "UDMA/44",
945 "UDMA/66",
946 "UDMA/100",
947 "UDMA/133",
948 "UDMA7",
949 };
Tejun Heo1da7b0d2006-03-06 04:31:56 +0900950 int highbit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951
Tejun Heo1da7b0d2006-03-06 04:31:56 +0900952 highbit = fls(xfer_mask) - 1;
953 if (highbit >= 0 && highbit < ARRAY_SIZE(xfer_mode_str))
954 return xfer_mode_str[highbit];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 return "<n/a>";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +0100957EXPORT_SYMBOL_GPL(ata_mode_string);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958
Gwendal Grignoud9027472010-05-25 12:31:38 -0700959const char *sata_spd_string(unsigned int spd)
Tejun Heo4c360c82006-04-01 01:38:17 +0900960{
961 static const char * const spd_str[] = {
962 "1.5 Gbps",
963 "3.0 Gbps",
Shane Huang8522ee22008-12-30 11:00:37 +0800964 "6.0 Gbps",
Tejun Heo4c360c82006-04-01 01:38:17 +0900965 };
966
967 if (spd == 0 || (spd - 1) >= ARRAY_SIZE(spd_str))
968 return "<unknown>";
969 return spd_str[spd - 1];
970}
971
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972/**
973 * ata_dev_classify - determine device type based on ATA-spec signature
974 * @tf: ATA taskfile register set for device to be identified
975 *
976 * Determine from taskfile register contents whether a device is
977 * ATA or ATAPI, as per "Signature and persistence" section
978 * of ATA/PI spec (volume 1, sect 5.14).
979 *
980 * LOCKING:
981 * None.
982 *
983 * RETURNS:
Hannes Reinecke9162c652014-11-05 13:08:21 +0100984 * Device type, %ATA_DEV_ATA, %ATA_DEV_ATAPI, %ATA_DEV_PMP,
985 * %ATA_DEV_ZAC, or %ATA_DEV_UNKNOWN the event of failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 */
Jeff Garzik057ace52005-10-22 14:27:05 -0400987unsigned int ata_dev_classify(const struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988{
989 /* Apple's open source Darwin code hints that some devices only
990 * put a proper signature into the LBA mid/high registers,
991 * So, we only check those. It's sufficient for uniqueness.
Tejun Heo633273a2007-09-23 13:19:54 +0900992 *
993 * ATA/ATAPI-7 (d1532v1r1: Feb. 19, 2003) specified separate
994 * signatures for ATA and ATAPI devices attached on SerialATA,
995 * 0x3c/0xc3 and 0x69/0x96 respectively. However, SerialATA
996 * spec has never mentioned about using different signatures
997 * for ATA/ATAPI devices. Then, Serial ATA II: Port
998 * Multiplier specification began to use 0x69/0x96 to identify
999 * port multpliers and 0x3c/0xc3 to identify SEMB device.
1000 * ATA/ATAPI-7 dropped descriptions about 0x3c/0xc3 and
1001 * 0x69/0x96 shortly and described them as reserved for
1002 * SerialATA.
1003 *
1004 * We follow the current spec and consider that 0x69/0x96
1005 * identifies a port multiplier and 0x3c/0xc3 a SEMB device.
Tejun Heo79b42ba2009-04-15 06:21:10 +09001006 * Unfortunately, WDC WD1600JS-62MHB5 (a hard drive) reports
1007 * SEMB signature. This is worked around in
1008 * ata_dev_read_id().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 */
Hannes Reinecke6c952a02021-12-21 08:20:26 +01001010 if (tf->lbam == 0 && tf->lbah == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 return ATA_DEV_ATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012
Hannes Reinecke6c952a02021-12-21 08:20:26 +01001013 if (tf->lbam == 0x14 && tf->lbah == 0xeb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 return ATA_DEV_ATAPI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015
Hannes Reinecke6c952a02021-12-21 08:20:26 +01001016 if (tf->lbam == 0x69 && tf->lbah == 0x96)
Tejun Heo633273a2007-09-23 13:19:54 +09001017 return ATA_DEV_PMP;
Tejun Heo633273a2007-09-23 13:19:54 +09001018
Hannes Reinecke6c952a02021-12-21 08:20:26 +01001019 if (tf->lbam == 0x3c && tf->lbah == 0xc3)
Tejun Heo79b42ba2009-04-15 06:21:10 +09001020 return ATA_DEV_SEMB;
Tejun Heo633273a2007-09-23 13:19:54 +09001021
Hannes Reinecke6c952a02021-12-21 08:20:26 +01001022 if (tf->lbam == 0xcd && tf->lbah == 0xab)
Hannes Reinecke9162c652014-11-05 13:08:21 +01001023 return ATA_DEV_ZAC;
Hannes Reinecke9162c652014-11-05 13:08:21 +01001024
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 return ATA_DEV_UNKNOWN;
1026}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01001027EXPORT_SYMBOL_GPL(ata_dev_classify);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028
1029/**
Tejun Heo6a62a042006-02-13 10:02:46 +09001030 * ata_id_string - Convert IDENTIFY DEVICE page into string
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 * @id: IDENTIFY DEVICE results we will examine
1032 * @s: string into which data is output
1033 * @ofs: offset into identify device page
1034 * @len: length of string to return. must be an even number.
1035 *
1036 * The strings in the IDENTIFY DEVICE page are broken up into
1037 * 16-bit chunks. Run through the string, and output each
1038 * 8-bit chunk linearly, regardless of platform.
1039 *
1040 * LOCKING:
1041 * caller.
1042 */
1043
Tejun Heo6a62a042006-02-13 10:02:46 +09001044void ata_id_string(const u16 *id, unsigned char *s,
1045 unsigned int ofs, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046{
1047 unsigned int c;
1048
Alan Cox963e4972008-07-24 17:16:06 +01001049 BUG_ON(len & 1);
1050
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 while (len > 0) {
1052 c = id[ofs] >> 8;
1053 *s = c;
1054 s++;
1055
1056 c = id[ofs] & 0xff;
1057 *s = c;
1058 s++;
1059
1060 ofs++;
1061 len -= 2;
1062 }
1063}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01001064EXPORT_SYMBOL_GPL(ata_id_string);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065
Tejun Heo0e949ff2006-02-12 22:47:04 +09001066/**
Tejun Heo6a62a042006-02-13 10:02:46 +09001067 * ata_id_c_string - Convert IDENTIFY DEVICE page into C string
Tejun Heo0e949ff2006-02-12 22:47:04 +09001068 * @id: IDENTIFY DEVICE results we will examine
1069 * @s: string into which data is output
1070 * @ofs: offset into identify device page
1071 * @len: length of string to return. must be an odd number.
1072 *
Tejun Heo6a62a042006-02-13 10:02:46 +09001073 * This function is identical to ata_id_string except that it
Tejun Heo0e949ff2006-02-12 22:47:04 +09001074 * trims trailing spaces and terminates the resulting string with
1075 * null. @len must be actual maximum length (even number) + 1.
1076 *
1077 * LOCKING:
1078 * caller.
1079 */
Tejun Heo6a62a042006-02-13 10:02:46 +09001080void ata_id_c_string(const u16 *id, unsigned char *s,
1081 unsigned int ofs, unsigned int len)
Tejun Heo0e949ff2006-02-12 22:47:04 +09001082{
1083 unsigned char *p;
1084
Tejun Heo6a62a042006-02-13 10:02:46 +09001085 ata_id_string(id, s, ofs, len - 1);
Tejun Heo0e949ff2006-02-12 22:47:04 +09001086
1087 p = s + strnlen(s, len - 1);
1088 while (p > s && p[-1] == ' ')
1089 p--;
1090 *p = '\0';
1091}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01001092EXPORT_SYMBOL_GPL(ata_id_c_string);
Edward Falk0baab862005-06-02 18:17:13 -04001093
Tejun Heodb6f8752007-09-03 12:31:58 +09001094static u64 ata_id_n_sectors(const u16 *id)
1095{
1096 if (ata_id_has_lba(id)) {
1097 if (ata_id_has_lba48(id))
Robert Hancock968e5942009-02-16 20:15:08 -06001098 return ata_id_u64(id, ATA_ID_LBA_CAPACITY_2);
Tejun Heodb6f8752007-09-03 12:31:58 +09001099 else
Robert Hancock968e5942009-02-16 20:15:08 -06001100 return ata_id_u32(id, ATA_ID_LBA_CAPACITY);
Tejun Heodb6f8752007-09-03 12:31:58 +09001101 } else {
1102 if (ata_id_current_chs_valid(id))
Robert Hancock968e5942009-02-16 20:15:08 -06001103 return id[ATA_ID_CUR_CYLS] * id[ATA_ID_CUR_HEADS] *
1104 id[ATA_ID_CUR_SECTORS];
Tejun Heodb6f8752007-09-03 12:31:58 +09001105 else
Robert Hancock968e5942009-02-16 20:15:08 -06001106 return id[ATA_ID_CYLS] * id[ATA_ID_HEADS] *
1107 id[ATA_ID_SECTORS];
Tejun Heodb6f8752007-09-03 12:31:58 +09001108 }
1109}
1110
Tejun Heoa5987e02008-03-27 19:14:23 +09001111u64 ata_tf_to_lba48(const struct ata_taskfile *tf)
Alan Cox1e999732007-04-11 00:23:13 +01001112{
1113 u64 sectors = 0;
1114
1115 sectors |= ((u64)(tf->hob_lbah & 0xff)) << 40;
1116 sectors |= ((u64)(tf->hob_lbam & 0xff)) << 32;
Roland Dreierba14a9c2008-10-28 16:52:20 -07001117 sectors |= ((u64)(tf->hob_lbal & 0xff)) << 24;
Alan Cox1e999732007-04-11 00:23:13 +01001118 sectors |= (tf->lbah & 0xff) << 16;
1119 sectors |= (tf->lbam & 0xff) << 8;
1120 sectors |= (tf->lbal & 0xff);
1121
Tejun Heoa5987e02008-03-27 19:14:23 +09001122 return sectors;
Alan Cox1e999732007-04-11 00:23:13 +01001123}
1124
Tejun Heoa5987e02008-03-27 19:14:23 +09001125u64 ata_tf_to_lba(const struct ata_taskfile *tf)
Alan Cox1e999732007-04-11 00:23:13 +01001126{
1127 u64 sectors = 0;
1128
1129 sectors |= (tf->device & 0x0f) << 24;
1130 sectors |= (tf->lbah & 0xff) << 16;
1131 sectors |= (tf->lbam & 0xff) << 8;
1132 sectors |= (tf->lbal & 0xff);
1133
Tejun Heoa5987e02008-03-27 19:14:23 +09001134 return sectors;
Alan Cox1e999732007-04-11 00:23:13 +01001135}
1136
1137/**
Tejun Heoc728a912007-09-03 12:32:30 +09001138 * ata_read_native_max_address - Read native max address
1139 * @dev: target device
1140 * @max_sectors: out parameter for the result native max address
Alan Cox1e999732007-04-11 00:23:13 +01001141 *
Tejun Heoc728a912007-09-03 12:32:30 +09001142 * Perform an LBA48 or LBA28 native size query upon the device in
1143 * question.
1144 *
1145 * RETURNS:
1146 * 0 on success, -EACCES if command is aborted by the drive.
1147 * -EIO on other errors.
Alan Cox1e999732007-04-11 00:23:13 +01001148 */
Tejun Heoc728a912007-09-03 12:32:30 +09001149static int ata_read_native_max_address(struct ata_device *dev, u64 *max_sectors)
Alan Cox1e999732007-04-11 00:23:13 +01001150{
Tejun Heoc728a912007-09-03 12:32:30 +09001151 unsigned int err_mask;
Alan Cox1e999732007-04-11 00:23:13 +01001152 struct ata_taskfile tf;
Tejun Heoc728a912007-09-03 12:32:30 +09001153 int lba48 = ata_id_has_lba48(dev->id);
Alan Cox1e999732007-04-11 00:23:13 +01001154
1155 ata_tf_init(dev, &tf);
1156
Tejun Heoc728a912007-09-03 12:32:30 +09001157 /* always clear all address registers */
Alan Cox1e999732007-04-11 00:23:13 +01001158 tf.flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
Tejun Heoc728a912007-09-03 12:32:30 +09001159
1160 if (lba48) {
1161 tf.command = ATA_CMD_READ_NATIVE_MAX_EXT;
1162 tf.flags |= ATA_TFLAG_LBA48;
1163 } else
1164 tf.command = ATA_CMD_READ_NATIVE_MAX;
1165
Hannes Reineckebd18bc02016-07-14 09:05:46 +09001166 tf.protocol = ATA_PROT_NODATA;
Tejun Heoc728a912007-09-03 12:32:30 +09001167 tf.device |= ATA_LBA;
Alan Cox1e999732007-04-11 00:23:13 +01001168
Tejun Heo2b789102007-10-09 15:05:44 +09001169 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
Tejun Heoc728a912007-09-03 12:32:30 +09001170 if (err_mask) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07001171 ata_dev_warn(dev,
1172 "failed to read native max address (err_mask=0x%x)\n",
1173 err_mask);
Tejun Heoc728a912007-09-03 12:32:30 +09001174 if (err_mask == AC_ERR_DEV && (tf.feature & ATA_ABORTED))
1175 return -EACCES;
1176 return -EIO;
1177 }
Alan Cox1e999732007-04-11 00:23:13 +01001178
Tejun Heoc728a912007-09-03 12:32:30 +09001179 if (lba48)
Tejun Heoa5987e02008-03-27 19:14:23 +09001180 *max_sectors = ata_tf_to_lba48(&tf) + 1;
Tejun Heoc728a912007-09-03 12:32:30 +09001181 else
Tejun Heoa5987e02008-03-27 19:14:23 +09001182 *max_sectors = ata_tf_to_lba(&tf) + 1;
Jeff Garzik2dcb4072007-10-19 06:42:56 -04001183 if (dev->horkage & ATA_HORKAGE_HPA_SIZE)
Alan Cox93328e112007-09-29 04:06:48 -04001184 (*max_sectors)--;
Tejun Heoc728a912007-09-03 12:32:30 +09001185 return 0;
Alan Cox1e999732007-04-11 00:23:13 +01001186}
1187
1188/**
Tejun Heoc728a912007-09-03 12:32:30 +09001189 * ata_set_max_sectors - Set max sectors
1190 * @dev: target device
Randy Dunlap6b38d1d2007-05-01 17:35:55 -07001191 * @new_sectors: new max sectors value to set for the device
Alan Cox1e999732007-04-11 00:23:13 +01001192 *
Tejun Heoc728a912007-09-03 12:32:30 +09001193 * Set max sectors of @dev to @new_sectors.
1194 *
1195 * RETURNS:
1196 * 0 on success, -EACCES if command is aborted or denied (due to
1197 * previous non-volatile SET_MAX) by the drive. -EIO on other
1198 * errors.
Alan Cox1e999732007-04-11 00:23:13 +01001199 */
Tejun Heo05027ad2007-09-03 12:32:57 +09001200static int ata_set_max_sectors(struct ata_device *dev, u64 new_sectors)
Alan Cox1e999732007-04-11 00:23:13 +01001201{
Tejun Heoc728a912007-09-03 12:32:30 +09001202 unsigned int err_mask;
Alan Cox1e999732007-04-11 00:23:13 +01001203 struct ata_taskfile tf;
Tejun Heoc728a912007-09-03 12:32:30 +09001204 int lba48 = ata_id_has_lba48(dev->id);
Alan Cox1e999732007-04-11 00:23:13 +01001205
1206 new_sectors--;
1207
1208 ata_tf_init(dev, &tf);
1209
Alan Cox1e999732007-04-11 00:23:13 +01001210 tf.flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
Tejun Heoc728a912007-09-03 12:32:30 +09001211
1212 if (lba48) {
1213 tf.command = ATA_CMD_SET_MAX_EXT;
1214 tf.flags |= ATA_TFLAG_LBA48;
1215
1216 tf.hob_lbal = (new_sectors >> 24) & 0xff;
1217 tf.hob_lbam = (new_sectors >> 32) & 0xff;
1218 tf.hob_lbah = (new_sectors >> 40) & 0xff;
Tejun Heo1e582ba2007-09-21 20:07:14 +09001219 } else {
Tejun Heoc728a912007-09-03 12:32:30 +09001220 tf.command = ATA_CMD_SET_MAX;
1221
Tejun Heo1e582ba2007-09-21 20:07:14 +09001222 tf.device |= (new_sectors >> 24) & 0xf;
1223 }
1224
Hannes Reineckebd18bc02016-07-14 09:05:46 +09001225 tf.protocol = ATA_PROT_NODATA;
Tejun Heoc728a912007-09-03 12:32:30 +09001226 tf.device |= ATA_LBA;
Alan Cox1e999732007-04-11 00:23:13 +01001227
1228 tf.lbal = (new_sectors >> 0) & 0xff;
1229 tf.lbam = (new_sectors >> 8) & 0xff;
1230 tf.lbah = (new_sectors >> 16) & 0xff;
Alan Cox1e999732007-04-11 00:23:13 +01001231
Tejun Heo2b789102007-10-09 15:05:44 +09001232 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
Tejun Heoc728a912007-09-03 12:32:30 +09001233 if (err_mask) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07001234 ata_dev_warn(dev,
1235 "failed to set max address (err_mask=0x%x)\n",
1236 err_mask);
Tejun Heoc728a912007-09-03 12:32:30 +09001237 if (err_mask == AC_ERR_DEV &&
1238 (tf.feature & (ATA_ABORTED | ATA_IDNF)))
1239 return -EACCES;
1240 return -EIO;
1241 }
Alan Cox1e999732007-04-11 00:23:13 +01001242
Tejun Heoc728a912007-09-03 12:32:30 +09001243 return 0;
Alan Cox1e999732007-04-11 00:23:13 +01001244}
1245
1246/**
1247 * ata_hpa_resize - Resize a device with an HPA set
1248 * @dev: Device to resize
1249 *
1250 * Read the size of an LBA28 or LBA48 disk with HPA features and resize
1251 * it if required to the full size of the media. The caller must check
1252 * the drive has the HPA feature set enabled.
Tejun Heo05027ad2007-09-03 12:32:57 +09001253 *
1254 * RETURNS:
1255 * 0 on success, -errno on failure.
Alan Cox1e999732007-04-11 00:23:13 +01001256 */
Tejun Heo05027ad2007-09-03 12:32:57 +09001257static int ata_hpa_resize(struct ata_device *dev)
Alan Cox1e999732007-04-11 00:23:13 +01001258{
Damien Le Moal891fd7c2021-08-16 10:44:50 +09001259 bool print_info = ata_dev_print_info(dev);
Tejun Heo445d2112010-04-05 10:33:13 +09001260 bool unlock_hpa = ata_ignore_hpa || dev->flags & ATA_DFLAG_UNLOCK_HPA;
Tejun Heo05027ad2007-09-03 12:32:57 +09001261 u64 sectors = ata_id_n_sectors(dev->id);
1262 u64 native_sectors;
Tejun Heoc728a912007-09-03 12:32:30 +09001263 int rc;
Jeff Garzika617c092007-05-21 20:14:23 -04001264
Tejun Heo05027ad2007-09-03 12:32:57 +09001265 /* do we need to do it? */
Hannes Reinecke9162c652014-11-05 13:08:21 +01001266 if ((dev->class != ATA_DEV_ATA && dev->class != ATA_DEV_ZAC) ||
Tejun Heo05027ad2007-09-03 12:32:57 +09001267 !ata_id_has_lba(dev->id) || !ata_id_hpa_enabled(dev->id) ||
1268 (dev->horkage & ATA_HORKAGE_BROKEN_HPA))
Tejun Heoc728a912007-09-03 12:32:30 +09001269 return 0;
Alan Cox1e999732007-04-11 00:23:13 +01001270
Tejun Heo05027ad2007-09-03 12:32:57 +09001271 /* read native max address */
1272 rc = ata_read_native_max_address(dev, &native_sectors);
1273 if (rc) {
Tejun Heodda7aba2008-03-23 21:05:15 +09001274 /* If device aborted the command or HPA isn't going to
1275 * be unlocked, skip HPA resizing.
Tejun Heo05027ad2007-09-03 12:32:57 +09001276 */
Tejun Heo445d2112010-04-05 10:33:13 +09001277 if (rc == -EACCES || !unlock_hpa) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07001278 ata_dev_warn(dev,
1279 "HPA support seems broken, skipping HPA handling\n");
Tejun Heo05027ad2007-09-03 12:32:57 +09001280 dev->horkage |= ATA_HORKAGE_BROKEN_HPA;
Alan Cox1e999732007-04-11 00:23:13 +01001281
Tejun Heo05027ad2007-09-03 12:32:57 +09001282 /* we can continue if device aborted the command */
1283 if (rc == -EACCES)
1284 rc = 0;
Alan Cox1e999732007-04-11 00:23:13 +01001285 }
Tejun Heo37301a52007-06-25 20:45:54 +09001286
Tejun Heo05027ad2007-09-03 12:32:57 +09001287 return rc;
1288 }
Tejun Heo5920dad2009-07-15 17:11:41 +09001289 dev->n_native_sectors = native_sectors;
Tejun Heo05027ad2007-09-03 12:32:57 +09001290
1291 /* nothing to do? */
Tejun Heo445d2112010-04-05 10:33:13 +09001292 if (native_sectors <= sectors || !unlock_hpa) {
Tejun Heo05027ad2007-09-03 12:32:57 +09001293 if (!print_info || native_sectors == sectors)
1294 return 0;
1295
1296 if (native_sectors > sectors)
Joe Perchesa9a79df2011-04-15 15:51:59 -07001297 ata_dev_info(dev,
Tejun Heo05027ad2007-09-03 12:32:57 +09001298 "HPA detected: current %llu, native %llu\n",
1299 (unsigned long long)sectors,
1300 (unsigned long long)native_sectors);
1301 else if (native_sectors < sectors)
Joe Perchesa9a79df2011-04-15 15:51:59 -07001302 ata_dev_warn(dev,
1303 "native sectors (%llu) is smaller than sectors (%llu)\n",
Tejun Heo05027ad2007-09-03 12:32:57 +09001304 (unsigned long long)native_sectors,
1305 (unsigned long long)sectors);
1306 return 0;
1307 }
1308
1309 /* let's unlock HPA */
1310 rc = ata_set_max_sectors(dev, native_sectors);
1311 if (rc == -EACCES) {
1312 /* if device aborted the command, skip HPA resizing */
Joe Perchesa9a79df2011-04-15 15:51:59 -07001313 ata_dev_warn(dev,
1314 "device aborted resize (%llu -> %llu), skipping HPA handling\n",
1315 (unsigned long long)sectors,
1316 (unsigned long long)native_sectors);
Tejun Heo05027ad2007-09-03 12:32:57 +09001317 dev->horkage |= ATA_HORKAGE_BROKEN_HPA;
1318 return 0;
1319 } else if (rc)
1320 return rc;
1321
1322 /* re-read IDENTIFY data */
1323 rc = ata_dev_reread_id(dev, 0);
1324 if (rc) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07001325 ata_dev_err(dev,
1326 "failed to re-read IDENTIFY data after HPA resizing\n");
Tejun Heo05027ad2007-09-03 12:32:57 +09001327 return rc;
1328 }
1329
1330 if (print_info) {
1331 u64 new_sectors = ata_id_n_sectors(dev->id);
Joe Perchesa9a79df2011-04-15 15:51:59 -07001332 ata_dev_info(dev,
Tejun Heo05027ad2007-09-03 12:32:57 +09001333 "HPA unlocked: %llu -> %llu, native %llu\n",
1334 (unsigned long long)sectors,
1335 (unsigned long long)new_sectors,
1336 (unsigned long long)native_sectors);
1337 }
1338
1339 return 0;
Alan Cox1e999732007-04-11 00:23:13 +01001340}
1341
Edward Falk0baab862005-06-02 18:17:13 -04001342/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 * ata_dump_id - IDENTIFY DEVICE info debugging output
Hannes Reinecke6044f3c2021-12-21 08:20:27 +01001344 * @dev: device from which the information is fetched
Tejun Heo0bd33002006-02-12 22:47:05 +09001345 * @id: IDENTIFY DEVICE page to dump
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 *
Tejun Heo0bd33002006-02-12 22:47:05 +09001347 * Dump selected 16-bit words from the given IDENTIFY DEVICE
1348 * page.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 *
1350 * LOCKING:
1351 * caller.
1352 */
1353
Hannes Reinecke6044f3c2021-12-21 08:20:27 +01001354static inline void ata_dump_id(struct ata_device *dev, const u16 *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355{
Hannes Reinecke6044f3c2021-12-21 08:20:27 +01001356 ata_dev_dbg(dev,
1357 "49==0x%04x 53==0x%04x 63==0x%04x 64==0x%04x 75==0x%04x\n"
1358 "80==0x%04x 81==0x%04x 82==0x%04x 83==0x%04x 84==0x%04x\n"
1359 "88==0x%04x 93==0x%04x\n",
1360 id[49], id[53], id[63], id[64], id[75], id[80],
1361 id[81], id[82], id[83], id[84], id[88], id[93]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362}
1363
Tejun Heocb95d562006-03-06 04:31:56 +09001364/**
1365 * ata_id_xfermask - Compute xfermask from the given IDENTIFY data
1366 * @id: IDENTIFY data to compute xfer mask from
1367 *
1368 * Compute the xfermask for this device. This is not as trivial
1369 * as it seems if we must consider early devices correctly.
1370 *
1371 * FIXME: pre IDE drive timing (do we care ?).
1372 *
1373 * LOCKING:
1374 * None.
1375 *
1376 * RETURNS:
1377 * Computed xfermask
1378 */
Tejun Heo7dc951a2007-11-27 19:43:42 +09001379unsigned long ata_id_xfermask(const u16 *id)
Tejun Heocb95d562006-03-06 04:31:56 +09001380{
Tejun Heo7dc951a2007-11-27 19:43:42 +09001381 unsigned long pio_mask, mwdma_mask, udma_mask;
Tejun Heocb95d562006-03-06 04:31:56 +09001382
1383 /* Usual case. Word 53 indicates word 64 is valid */
1384 if (id[ATA_ID_FIELD_VALID] & (1 << 1)) {
1385 pio_mask = id[ATA_ID_PIO_MODES] & 0x03;
1386 pio_mask <<= 3;
1387 pio_mask |= 0x7;
1388 } else {
1389 /* If word 64 isn't valid then Word 51 high byte holds
1390 * the PIO timing number for the maximum. Turn it into
1391 * a mask.
1392 */
Lennert Buytenhek7a0f1c82007-01-29 13:28:47 +01001393 u8 mode = (id[ATA_ID_OLD_PIO_MODES] >> 8) & 0xFF;
Alan Cox46767aeb2006-09-29 18:26:47 +01001394 if (mode < 5) /* Valid PIO range */
Jeff Garzik2dcb4072007-10-19 06:42:56 -04001395 pio_mask = (2 << mode) - 1;
Alan Cox46767aeb2006-09-29 18:26:47 +01001396 else
1397 pio_mask = 1;
Tejun Heocb95d562006-03-06 04:31:56 +09001398
1399 /* But wait.. there's more. Design your standards by
1400 * committee and you too can get a free iordy field to
1401 * process. However its the speeds not the modes that
1402 * are supported... Note drivers using the timing API
1403 * will get this right anyway
1404 */
1405 }
1406
1407 mwdma_mask = id[ATA_ID_MWDMA_MODES] & 0x07;
Tejun Heofb21f0d2006-03-12 12:34:35 +09001408
Alan Coxb352e572006-08-10 18:52:12 +01001409 if (ata_id_is_cfa(id)) {
1410 /*
1411 * Process compact flash extended modes
1412 */
Sergei Shtylyov62afe5d2009-04-13 20:50:00 +04001413 int pio = (id[ATA_ID_CFA_MODES] >> 0) & 0x7;
1414 int dma = (id[ATA_ID_CFA_MODES] >> 3) & 0x7;
Alan Coxb352e572006-08-10 18:52:12 +01001415
1416 if (pio)
1417 pio_mask |= (1 << 5);
1418 if (pio > 1)
1419 pio_mask |= (1 << 6);
1420 if (dma)
1421 mwdma_mask |= (1 << 3);
1422 if (dma > 1)
1423 mwdma_mask |= (1 << 4);
1424 }
1425
Tejun Heofb21f0d2006-03-12 12:34:35 +09001426 udma_mask = 0;
1427 if (id[ATA_ID_FIELD_VALID] & (1 << 2))
1428 udma_mask = id[ATA_ID_UDMA_MODES] & 0xff;
Tejun Heocb95d562006-03-06 04:31:56 +09001429
1430 return ata_pack_xfermask(pio_mask, mwdma_mask, udma_mask);
1431}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01001432EXPORT_SYMBOL_GPL(ata_id_xfermask);
Tejun Heocb95d562006-03-06 04:31:56 +09001433
Adrian Bunk7102d232007-01-04 00:09:36 +01001434static void ata_qc_complete_internal(struct ata_queued_cmd *qc)
Tejun Heoa2a7a662005-12-13 14:48:31 +09001435{
Tejun Heo77853bf2006-01-23 13:09:36 +09001436 struct completion *waiting = qc->private_data;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001437
Tejun Heoa2a7a662005-12-13 14:48:31 +09001438 complete(waiting);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001439}
1440
1441/**
Tejun Heo24326972006-11-14 22:47:09 +09001442 * ata_exec_internal_sg - execute libata internal command
Tejun Heoa2a7a662005-12-13 14:48:31 +09001443 * @dev: Device to which the command is sent
1444 * @tf: Taskfile registers for the command and the result
Tejun Heod69cf372006-04-02 18:51:53 +09001445 * @cdb: CDB for packet command
Masanari Iidae2278672014-02-18 22:54:36 +09001446 * @dma_dir: Data transfer direction of the command
Randy Dunlap5c1ad8b2007-10-18 14:12:26 -07001447 * @sgl: sg list for the data buffer of the command
Tejun Heo24326972006-11-14 22:47:09 +09001448 * @n_elem: Number of sg entries
Tejun Heo2b789102007-10-09 15:05:44 +09001449 * @timeout: Timeout in msecs (0 for default)
Tejun Heoa2a7a662005-12-13 14:48:31 +09001450 *
1451 * Executes libata internal command with timeout. @tf contains
1452 * command on entry and result on return. Timeout and error
1453 * conditions are reported via return value. No recovery action
1454 * is taken after a command times out. It's caller's duty to
1455 * clean up after timeout.
1456 *
1457 * LOCKING:
1458 * None. Should be called with kernel context, might sleep.
Tejun Heo551e8882006-06-12 14:09:49 +09001459 *
1460 * RETURNS:
1461 * Zero on success, AC_ERR_* mask on failure
Tejun Heoa2a7a662005-12-13 14:48:31 +09001462 */
Tejun Heo24326972006-11-14 22:47:09 +09001463unsigned ata_exec_internal_sg(struct ata_device *dev,
1464 struct ata_taskfile *tf, const u8 *cdb,
Jens Axboe87260212007-10-16 11:14:12 +02001465 int dma_dir, struct scatterlist *sgl,
Tejun Heo2b789102007-10-09 15:05:44 +09001466 unsigned int n_elem, unsigned long timeout)
Tejun Heoa2a7a662005-12-13 14:48:31 +09001467{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001468 struct ata_link *link = dev->link;
1469 struct ata_port *ap = link->ap;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001470 u8 command = tf->command;
Tejun Heo87fbc5a2008-05-20 02:17:54 +09001471 int auto_timeout = 0;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001472 struct ata_queued_cmd *qc;
Jens Axboe28361c42018-05-11 12:51:09 -06001473 unsigned int preempted_tag;
Jens Axboee3ed89392018-05-11 12:51:05 -06001474 u32 preempted_sactive;
1475 u64 preempted_qc_active;
Tejun Heoda917d62007-09-23 13:14:12 +09001476 int preempted_nr_active_links;
Ingo Molnar60be6b92006-07-03 00:25:26 -07001477 DECLARE_COMPLETION_ONSTACK(wait);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001478 unsigned long flags;
Tejun Heo77853bf2006-01-23 13:09:36 +09001479 unsigned int err_mask;
Tejun Heod95a7172006-05-15 20:58:14 +09001480 int rc;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001481
Jeff Garzikba6a1302006-06-22 23:46:10 -04001482 spin_lock_irqsave(ap->lock, flags);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001483
Tejun Heoe3180492006-05-15 20:58:09 +09001484 /* no internal command while frozen */
Tejun Heob51e9e52006-06-29 01:29:30 +09001485 if (ap->pflags & ATA_PFLAG_FROZEN) {
Jeff Garzikba6a1302006-06-22 23:46:10 -04001486 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoe3180492006-05-15 20:58:09 +09001487 return AC_ERR_SYSTEM;
1488 }
1489
Tejun Heo2ab7db12006-05-15 20:58:02 +09001490 /* initialize internal qc */
Jens Axboe28361c42018-05-11 12:51:09 -06001491 qc = __ata_qc_from_tag(ap, ATA_TAG_INTERNAL);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001492
Jens Axboe28361c42018-05-11 12:51:09 -06001493 qc->tag = ATA_TAG_INTERNAL;
1494 qc->hw_tag = 0;
Tejun Heo2ab7db12006-05-15 20:58:02 +09001495 qc->scsicmd = NULL;
1496 qc->ap = ap;
1497 qc->dev = dev;
1498 ata_qc_reinit(qc);
1499
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001500 preempted_tag = link->active_tag;
1501 preempted_sactive = link->sactive;
Tejun Heodedaf2b2006-05-15 21:03:43 +09001502 preempted_qc_active = ap->qc_active;
Tejun Heoda917d62007-09-23 13:14:12 +09001503 preempted_nr_active_links = ap->nr_active_links;
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001504 link->active_tag = ATA_TAG_POISON;
1505 link->sactive = 0;
Tejun Heodedaf2b2006-05-15 21:03:43 +09001506 ap->qc_active = 0;
Tejun Heoda917d62007-09-23 13:14:12 +09001507 ap->nr_active_links = 0;
Tejun Heo2ab7db12006-05-15 20:58:02 +09001508
1509 /* prepare & issue qc */
Tejun Heoa2a7a662005-12-13 14:48:31 +09001510 qc->tf = *tf;
Tejun Heod69cf372006-04-02 18:51:53 +09001511 if (cdb)
1512 memcpy(qc->cdb, cdb, ATAPI_CDB_LEN);
Vincent Pelletiere7714512013-05-18 18:44:04 +02001513
1514 /* some SATA bridges need us to indicate data xfer direction */
1515 if (tf->protocol == ATAPI_PROT_DMA && (dev->flags & ATA_DFLAG_DMADIR) &&
1516 dma_dir == DMA_FROM_DEVICE)
1517 qc->tf.feature |= ATAPI_DMADIR;
1518
Tejun Heoe61e0672006-05-15 20:57:40 +09001519 qc->flags |= ATA_QCFLAG_RESULT_TF;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001520 qc->dma_dir = dma_dir;
1521 if (dma_dir != DMA_NONE) {
Tejun Heo24326972006-11-14 22:47:09 +09001522 unsigned int i, buflen = 0;
Jens Axboe87260212007-10-16 11:14:12 +02001523 struct scatterlist *sg;
Tejun Heo24326972006-11-14 22:47:09 +09001524
Jens Axboe87260212007-10-16 11:14:12 +02001525 for_each_sg(sgl, sg, n_elem, i)
1526 buflen += sg->length;
Tejun Heo24326972006-11-14 22:47:09 +09001527
Jens Axboe87260212007-10-16 11:14:12 +02001528 ata_sg_init(qc, sgl, n_elem);
Brian King49c80422007-01-30 11:32:26 -06001529 qc->nbytes = buflen;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001530 }
1531
Tejun Heo77853bf2006-01-23 13:09:36 +09001532 qc->private_data = &wait;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001533 qc->complete_fn = ata_qc_complete_internal;
1534
Tejun Heo8e0e6942006-03-31 20:41:11 +09001535 ata_qc_issue(qc);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001536
Jeff Garzikba6a1302006-06-22 23:46:10 -04001537 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001538
Tejun Heo87fbc5a2008-05-20 02:17:54 +09001539 if (!timeout) {
1540 if (ata_probe_timeout)
1541 timeout = ata_probe_timeout * 1000;
1542 else {
1543 timeout = ata_internal_cmd_timeout(dev, command);
1544 auto_timeout = 1;
1545 }
1546 }
Tejun Heo2b789102007-10-09 15:05:44 +09001547
Tejun Heoc0c362b2010-09-06 17:57:14 +02001548 if (ap->ops->error_handler)
1549 ata_eh_release(ap);
1550
Tejun Heo2b789102007-10-09 15:05:44 +09001551 rc = wait_for_completion_timeout(&wait, msecs_to_jiffies(timeout));
Albert Lee41ade502006-03-14 11:19:04 +08001552
Tejun Heoc0c362b2010-09-06 17:57:14 +02001553 if (ap->ops->error_handler)
1554 ata_eh_acquire(ap);
1555
Tejun Heoc4291372010-05-10 21:41:38 +02001556 ata_sff_flush_pio_task(ap);
Tejun Heod95a7172006-05-15 20:58:14 +09001557
1558 if (!rc) {
Jeff Garzikba6a1302006-06-22 23:46:10 -04001559 spin_lock_irqsave(ap->lock, flags);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001560
1561 /* We're racing with irq here. If we lose, the
1562 * following test prevents us from completing the qc
Tejun Heod95a7172006-05-15 20:58:14 +09001563 * twice. If we win, the port is frozen and will be
1564 * cleaned up by ->post_internal_cmd().
Tejun Heoa2a7a662005-12-13 14:48:31 +09001565 */
Tejun Heo77853bf2006-01-23 13:09:36 +09001566 if (qc->flags & ATA_QCFLAG_ACTIVE) {
Tejun Heod95a7172006-05-15 20:58:14 +09001567 qc->err_mask |= AC_ERR_TIMEOUT;
1568
1569 if (ap->ops->error_handler)
1570 ata_port_freeze(ap);
1571 else
1572 ata_qc_complete(qc);
Tejun Heof15a1da2006-05-15 20:57:56 +09001573
Borislav Petkov0dd4b212006-06-23 02:29:08 -04001574 if (ata_msg_warn(ap))
Joe Perchesa9a79df2011-04-15 15:51:59 -07001575 ata_dev_warn(dev, "qc timeout (cmd 0x%x)\n",
1576 command);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001577 }
1578
Jeff Garzikba6a1302006-06-22 23:46:10 -04001579 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001580 }
1581
Tejun Heod95a7172006-05-15 20:58:14 +09001582 /* do post_internal_cmd */
1583 if (ap->ops->post_internal_cmd)
1584 ap->ops->post_internal_cmd(qc);
1585
Tejun Heoa51d6442007-03-20 15:24:11 +09001586 /* perform minimal error analysis */
1587 if (qc->flags & ATA_QCFLAG_FAILED) {
1588 if (qc->result_tf.command & (ATA_ERR | ATA_DF))
1589 qc->err_mask |= AC_ERR_DEV;
1590
1591 if (!qc->err_mask)
1592 qc->err_mask |= AC_ERR_OTHER;
1593
1594 if (qc->err_mask & ~AC_ERR_OTHER)
1595 qc->err_mask &= ~AC_ERR_OTHER;
Damien Le Moal2dae9952016-12-19 10:17:40 +09001596 } else if (qc->tf.command == ATA_CMD_REQ_SENSE_DATA) {
1597 qc->result_tf.command |= ATA_SENSE;
Tejun Heod95a7172006-05-15 20:58:14 +09001598 }
1599
Tejun Heo15869302006-05-15 20:57:33 +09001600 /* finish up */
Jeff Garzikba6a1302006-06-22 23:46:10 -04001601 spin_lock_irqsave(ap->lock, flags);
Tejun Heo15869302006-05-15 20:57:33 +09001602
Tejun Heoe61e0672006-05-15 20:57:40 +09001603 *tf = qc->result_tf;
Tejun Heo77853bf2006-01-23 13:09:36 +09001604 err_mask = qc->err_mask;
1605
1606 ata_qc_free(qc);
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001607 link->active_tag = preempted_tag;
1608 link->sactive = preempted_sactive;
Tejun Heodedaf2b2006-05-15 21:03:43 +09001609 ap->qc_active = preempted_qc_active;
Tejun Heoda917d62007-09-23 13:14:12 +09001610 ap->nr_active_links = preempted_nr_active_links;
Tejun Heo77853bf2006-01-23 13:09:36 +09001611
Jeff Garzikba6a1302006-06-22 23:46:10 -04001612 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heo15869302006-05-15 20:57:33 +09001613
Tejun Heo87fbc5a2008-05-20 02:17:54 +09001614 if ((err_mask & AC_ERR_TIMEOUT) && auto_timeout)
1615 ata_internal_cmd_timed_out(dev, command);
1616
Tejun Heo77853bf2006-01-23 13:09:36 +09001617 return err_mask;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001618}
1619
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620/**
Tejun Heo33480a02006-12-12 02:15:31 +09001621 * ata_exec_internal - execute libata internal command
Tejun Heo24326972006-11-14 22:47:09 +09001622 * @dev: Device to which the command is sent
1623 * @tf: Taskfile registers for the command and the result
1624 * @cdb: CDB for packet command
Masanari Iidae2278672014-02-18 22:54:36 +09001625 * @dma_dir: Data transfer direction of the command
Tejun Heo24326972006-11-14 22:47:09 +09001626 * @buf: Data buffer of the command
1627 * @buflen: Length of data buffer
Tejun Heo2b789102007-10-09 15:05:44 +09001628 * @timeout: Timeout in msecs (0 for default)
Tejun Heo24326972006-11-14 22:47:09 +09001629 *
1630 * Wrapper around ata_exec_internal_sg() which takes simple
1631 * buffer instead of sg list.
1632 *
1633 * LOCKING:
1634 * None. Should be called with kernel context, might sleep.
1635 *
1636 * RETURNS:
1637 * Zero on success, AC_ERR_* mask on failure
1638 */
1639unsigned ata_exec_internal(struct ata_device *dev,
1640 struct ata_taskfile *tf, const u8 *cdb,
Tejun Heo2b789102007-10-09 15:05:44 +09001641 int dma_dir, void *buf, unsigned int buflen,
1642 unsigned long timeout)
Tejun Heo24326972006-11-14 22:47:09 +09001643{
Tejun Heo33480a02006-12-12 02:15:31 +09001644 struct scatterlist *psg = NULL, sg;
1645 unsigned int n_elem = 0;
Tejun Heo24326972006-11-14 22:47:09 +09001646
Tejun Heo33480a02006-12-12 02:15:31 +09001647 if (dma_dir != DMA_NONE) {
1648 WARN_ON(!buf);
1649 sg_init_one(&sg, buf, buflen);
1650 psg = &sg;
1651 n_elem++;
1652 }
Tejun Heo24326972006-11-14 22:47:09 +09001653
Tejun Heo2b789102007-10-09 15:05:44 +09001654 return ata_exec_internal_sg(dev, tf, cdb, dma_dir, psg, n_elem,
1655 timeout);
Tejun Heo24326972006-11-14 22:47:09 +09001656}
1657
1658/**
Alan Cox1bc4ccf2006-01-09 17:18:14 +00001659 * ata_pio_need_iordy - check if iordy needed
1660 * @adev: ATA device
1661 *
1662 * Check if the current speed of the device requires IORDY. Used
1663 * by various controllers for chip configuration.
1664 */
Alan Cox1bc4ccf2006-01-09 17:18:14 +00001665unsigned int ata_pio_need_iordy(const struct ata_device *adev)
1666{
Tejun Heo0d9e6652009-06-11 11:04:45 +09001667 /* Don't set IORDY if we're preparing for reset. IORDY may
1668 * lead to controller lock up on certain controllers if the
1669 * port is not occupied. See bko#11703 for details.
1670 */
1671 if (adev->link->ap->pflags & ATA_PFLAG_RESETTING)
1672 return 0;
1673 /* Controller doesn't support IORDY. Probably a pointless
1674 * check as the caller should know this.
1675 */
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001676 if (adev->link->ap->flags & ATA_FLAG_NO_IORDY)
Alan Cox1bc4ccf2006-01-09 17:18:14 +00001677 return 0;
David Daney5c18c4d2008-12-10 15:39:12 -08001678 /* CF spec. r4.1 Table 22 says no iordy on PIO5 and PIO6. */
1679 if (ata_id_is_cfa(adev->id)
1680 && (adev->pio_mode == XFER_PIO_5 || adev->pio_mode == XFER_PIO_6))
1681 return 0;
Alan Cox432729f2007-03-08 23:22:59 +00001682 /* PIO3 and higher it is mandatory */
1683 if (adev->pio_mode > XFER_PIO_2)
Alan Cox1bc4ccf2006-01-09 17:18:14 +00001684 return 1;
Alan Cox432729f2007-03-08 23:22:59 +00001685 /* We turn it on when possible */
1686 if (ata_id_has_iordy(adev->id))
1687 return 1;
1688 return 0;
1689}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01001690EXPORT_SYMBOL_GPL(ata_pio_need_iordy);
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05001691
Alan Cox432729f2007-03-08 23:22:59 +00001692/**
1693 * ata_pio_mask_no_iordy - Return the non IORDY mask
1694 * @adev: ATA device
1695 *
1696 * Compute the highest mode possible if we are not using iordy. Return
1697 * -1 if no iordy mode is available.
1698 */
Alan Cox432729f2007-03-08 23:22:59 +00001699static u32 ata_pio_mask_no_iordy(const struct ata_device *adev)
1700{
Alan Cox1bc4ccf2006-01-09 17:18:14 +00001701 /* If we have no drive specific rule, then PIO 2 is non IORDY */
Alan Cox1bc4ccf2006-01-09 17:18:14 +00001702 if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE */
Alan Cox432729f2007-03-08 23:22:59 +00001703 u16 pio = adev->id[ATA_ID_EIDE_PIO];
Alan Cox1bc4ccf2006-01-09 17:18:14 +00001704 /* Is the speed faster than the drive allows non IORDY ? */
1705 if (pio) {
1706 /* This is cycle times not frequency - watch the logic! */
1707 if (pio > 240) /* PIO2 is 240nS per cycle */
Alan Cox432729f2007-03-08 23:22:59 +00001708 return 3 << ATA_SHIFT_PIO;
1709 return 7 << ATA_SHIFT_PIO;
Alan Cox1bc4ccf2006-01-09 17:18:14 +00001710 }
1711 }
Alan Cox432729f2007-03-08 23:22:59 +00001712 return 3 << ATA_SHIFT_PIO;
Alan Cox1bc4ccf2006-01-09 17:18:14 +00001713}
1714
1715/**
Alan Cox963e4972008-07-24 17:16:06 +01001716 * ata_do_dev_read_id - default ID read method
1717 * @dev: device
1718 * @tf: proposed taskfile
1719 * @id: data buffer
1720 *
1721 * Issue the identify taskfile and hand back the buffer containing
1722 * identify data. For some RAID controllers and for pre ATA devices
1723 * this function is wrapped or replaced by the driver
1724 */
1725unsigned int ata_do_dev_read_id(struct ata_device *dev,
1726 struct ata_taskfile *tf, u16 *id)
1727{
1728 return ata_exec_internal(dev, tf, NULL, DMA_FROM_DEVICE,
1729 id, sizeof(id[0]) * ATA_ID_WORDS, 0);
1730}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01001731EXPORT_SYMBOL_GPL(ata_do_dev_read_id);
Alan Cox963e4972008-07-24 17:16:06 +01001732
1733/**
Tejun Heo49016ac2006-02-21 02:12:11 +09001734 * ata_dev_read_id - Read ID data from the specified device
Tejun Heo49016ac2006-02-21 02:12:11 +09001735 * @dev: target device
1736 * @p_class: pointer to class of the target device (may be changed)
Tejun Heobff04642006-11-10 18:08:10 +09001737 * @flags: ATA_READID_* flags
Tejun Heofe635c72006-05-15 20:57:35 +09001738 * @id: buffer to read IDENTIFY data into
Tejun Heo49016ac2006-02-21 02:12:11 +09001739 *
1740 * Read ID data from the specified device. ATA_CMD_ID_ATA is
1741 * performed on ATA devices and ATA_CMD_ID_ATAPI on ATAPI
Tejun Heoaec5c3c2006-03-25 01:33:34 +09001742 * devices. This function also issues ATA_CMD_INIT_DEV_PARAMS
1743 * for pre-ATA4 drives.
Tejun Heo49016ac2006-02-21 02:12:11 +09001744 *
Alan Cox50a99012007-08-08 14:27:00 +01001745 * FIXME: ATA_CMD_ID_ATA is optional for early drives and right
Jeff Garzik2dcb4072007-10-19 06:42:56 -04001746 * now we abort if we hit that case.
Alan Cox50a99012007-08-08 14:27:00 +01001747 *
Tejun Heo49016ac2006-02-21 02:12:11 +09001748 * LOCKING:
1749 * Kernel thread context (may sleep)
1750 *
1751 * RETURNS:
1752 * 0 on success, -errno otherwise.
1753 */
Tejun Heoa9beec92006-05-31 18:27:44 +09001754int ata_dev_read_id(struct ata_device *dev, unsigned int *p_class,
Tejun Heobff04642006-11-10 18:08:10 +09001755 unsigned int flags, u16 *id)
Tejun Heo49016ac2006-02-21 02:12:11 +09001756{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001757 struct ata_port *ap = dev->link->ap;
Tejun Heo49016ac2006-02-21 02:12:11 +09001758 unsigned int class = *p_class;
Tejun Heo49016ac2006-02-21 02:12:11 +09001759 struct ata_taskfile tf;
1760 unsigned int err_mask = 0;
1761 const char *reason;
Tejun Heo79b42ba2009-04-15 06:21:10 +09001762 bool is_semb = class == ATA_DEV_SEMB;
Tejun Heo54936f82007-05-11 14:35:29 +02001763 int may_fallback = 1, tried_spinup = 0;
Tejun Heo49016ac2006-02-21 02:12:11 +09001764 int rc;
1765
Borislav Petkov0dd4b212006-06-23 02:29:08 -04001766 if (ata_msg_ctl(ap))
Joe Perchesa9a79df2011-04-15 15:51:59 -07001767 ata_dev_dbg(dev, "%s: ENTER\n", __func__);
Tejun Heo49016ac2006-02-21 02:12:11 +09001768
Alan Cox963e4972008-07-24 17:16:06 +01001769retry:
Tejun Heo3373efd2006-05-15 20:57:53 +09001770 ata_tf_init(dev, &tf);
Tejun Heo49016ac2006-02-21 02:12:11 +09001771
1772 switch (class) {
Tejun Heo79b42ba2009-04-15 06:21:10 +09001773 case ATA_DEV_SEMB:
1774 class = ATA_DEV_ATA; /* some hard drives report SEMB sig */
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05001775 fallthrough;
Tejun Heo49016ac2006-02-21 02:12:11 +09001776 case ATA_DEV_ATA:
Hannes Reinecke9162c652014-11-05 13:08:21 +01001777 case ATA_DEV_ZAC:
Tejun Heo49016ac2006-02-21 02:12:11 +09001778 tf.command = ATA_CMD_ID_ATA;
1779 break;
1780 case ATA_DEV_ATAPI:
1781 tf.command = ATA_CMD_ID_ATAPI;
1782 break;
1783 default:
1784 rc = -ENODEV;
1785 reason = "unsupported class";
1786 goto err_out;
1787 }
1788
1789 tf.protocol = ATA_PROT_PIO;
Tejun Heo81afe892007-02-07 12:37:41 -08001790
1791 /* Some devices choke if TF registers contain garbage. Make
1792 * sure those are properly initialized.
1793 */
1794 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
1795
1796 /* Device presence detection is unreliable on some
1797 * controllers. Always poll IDENTIFY if available.
1798 */
1799 tf.flags |= ATA_TFLAG_POLLING;
Tejun Heo49016ac2006-02-21 02:12:11 +09001800
Alan Cox963e4972008-07-24 17:16:06 +01001801 if (ap->ops->read_id)
1802 err_mask = ap->ops->read_id(dev, &tf, id);
1803 else
1804 err_mask = ata_do_dev_read_id(dev, &tf, id);
1805
Tejun Heo49016ac2006-02-21 02:12:11 +09001806 if (err_mask) {
Tejun Heo800b3992006-12-03 21:34:13 +09001807 if (err_mask & AC_ERR_NODEV_HINT) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07001808 ata_dev_dbg(dev, "NODEV after polling detection\n");
Tejun Heo55a8e2c2006-11-10 18:08:10 +09001809 return -ENOENT;
1810 }
1811
Tejun Heo79b42ba2009-04-15 06:21:10 +09001812 if (is_semb) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07001813 ata_dev_info(dev,
1814 "IDENTIFY failed on device w/ SEMB sig, disabled\n");
Tejun Heo79b42ba2009-04-15 06:21:10 +09001815 /* SEMB is not supported yet */
1816 *p_class = ATA_DEV_SEMB_UNSUP;
1817 return 0;
1818 }
1819
Tejun Heo1ffc1512008-03-23 15:16:53 +09001820 if ((err_mask == AC_ERR_DEV) && (tf.feature & ATA_ABORTED)) {
1821 /* Device or controller might have reported
1822 * the wrong device class. Give a shot at the
1823 * other IDENTIFY if the current one is
1824 * aborted by the device.
1825 */
1826 if (may_fallback) {
1827 may_fallback = 0;
Tejun Heo54936f82007-05-11 14:35:29 +02001828
Tejun Heo1ffc1512008-03-23 15:16:53 +09001829 if (class == ATA_DEV_ATA)
1830 class = ATA_DEV_ATAPI;
1831 else
1832 class = ATA_DEV_ATA;
1833 goto retry;
1834 }
1835
1836 /* Control reaches here iff the device aborted
1837 * both flavors of IDENTIFYs which happens
1838 * sometimes with phantom devices.
1839 */
Joe Perchesa9a79df2011-04-15 15:51:59 -07001840 ata_dev_dbg(dev,
1841 "both IDENTIFYs aborted, assuming NODEV\n");
Tejun Heo1ffc1512008-03-23 15:16:53 +09001842 return -ENOENT;
Tejun Heo54936f82007-05-11 14:35:29 +02001843 }
1844
Tejun Heo49016ac2006-02-21 02:12:11 +09001845 rc = -EIO;
1846 reason = "I/O error";
Tejun Heo49016ac2006-02-21 02:12:11 +09001847 goto err_out;
1848 }
1849
Tejun Heo43c9c592010-05-23 12:59:11 +02001850 if (dev->horkage & ATA_HORKAGE_DUMP_ID) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07001851 ata_dev_dbg(dev, "dumping IDENTIFY data, "
1852 "class=%d may_fallback=%d tried_spinup=%d\n",
1853 class, may_fallback, tried_spinup);
Tejun Heo43c9c592010-05-23 12:59:11 +02001854 print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET,
1855 16, 2, id, ATA_ID_WORDS * sizeof(*id), true);
1856 }
1857
Tejun Heo54936f82007-05-11 14:35:29 +02001858 /* Falling back doesn't make sense if ID data was read
1859 * successfully at least once.
1860 */
1861 may_fallback = 0;
1862
Tejun Heo49016ac2006-02-21 02:12:11 +09001863 swap_buf_le16(id, ATA_ID_WORDS);
1864
Tejun Heo49016ac2006-02-21 02:12:11 +09001865 /* sanity check */
Tejun Heoa4f57492006-09-12 20:35:49 -07001866 rc = -EINVAL;
Alan Cox60700682007-06-07 16:13:55 +01001867 reason = "device reports invalid type";
Tejun Heoa4f57492006-09-12 20:35:49 -07001868
Hannes Reinecke9162c652014-11-05 13:08:21 +01001869 if (class == ATA_DEV_ATA || class == ATA_DEV_ZAC) {
Tejun Heoa4f57492006-09-12 20:35:49 -07001870 if (!ata_id_is_ata(id) && !ata_id_is_cfa(id))
1871 goto err_out;
Andy Whitcroftdb63a4c2012-05-04 22:15:10 +01001872 if (ap->host->flags & ATA_HOST_IGNORE_ATA &&
1873 ata_id_is_ata(id)) {
1874 ata_dev_dbg(dev,
1875 "host indicates ignore ATA devices, ignored\n");
1876 return -ENOENT;
1877 }
Tejun Heoa4f57492006-09-12 20:35:49 -07001878 } else {
1879 if (ata_id_is_ata(id))
1880 goto err_out;
Tejun Heo49016ac2006-02-21 02:12:11 +09001881 }
1882
Mark Lord169439c2007-04-17 18:26:07 -04001883 if (!tried_spinup && (id[2] == 0x37c8 || id[2] == 0x738c)) {
1884 tried_spinup = 1;
1885 /*
1886 * Drive powered-up in standby mode, and requires a specific
1887 * SET_FEATURES spin-up subcommand before it will accept
1888 * anything other than the original IDENTIFY command.
1889 */
Jeff Garzik218f3d32007-10-25 00:33:27 -04001890 err_mask = ata_dev_set_feature(dev, SETFEATURES_SPINUP, 0);
Ryan Powerfb0582f92007-08-10 13:59:35 -07001891 if (err_mask && id[2] != 0x738c) {
Mark Lord169439c2007-04-17 18:26:07 -04001892 rc = -EIO;
1893 reason = "SPINUP failed";
1894 goto err_out;
1895 }
1896 /*
1897 * If the drive initially returned incomplete IDENTIFY info,
1898 * we now must reissue the IDENTIFY command.
1899 */
1900 if (id[2] == 0x37c8)
1901 goto retry;
1902 }
1903
Hannes Reinecke9162c652014-11-05 13:08:21 +01001904 if ((flags & ATA_READID_POSTRESET) &&
1905 (class == ATA_DEV_ATA || class == ATA_DEV_ZAC)) {
Tejun Heo49016ac2006-02-21 02:12:11 +09001906 /*
1907 * The exact sequence expected by certain pre-ATA4 drives is:
1908 * SRST RESET
Alan Cox50a99012007-08-08 14:27:00 +01001909 * IDENTIFY (optional in early ATA)
1910 * INITIALIZE DEVICE PARAMETERS (later IDE and ATA)
Tejun Heo49016ac2006-02-21 02:12:11 +09001911 * anything else..
1912 * Some drives were very specific about that exact sequence.
Alan Cox50a99012007-08-08 14:27:00 +01001913 *
1914 * Note that ATA4 says lba is mandatory so the second check
Adam Buchbinderc9404c92009-12-18 15:40:42 -05001915 * should never trigger.
Tejun Heo49016ac2006-02-21 02:12:11 +09001916 */
1917 if (ata_id_major_version(id) < 4 || !ata_id_has_lba(id)) {
Tejun Heo3373efd2006-05-15 20:57:53 +09001918 err_mask = ata_dev_init_params(dev, id[3], id[6]);
Tejun Heo49016ac2006-02-21 02:12:11 +09001919 if (err_mask) {
1920 rc = -EIO;
1921 reason = "INIT_DEV_PARAMS failed";
1922 goto err_out;
1923 }
1924
1925 /* current CHS translation info (id[53-58]) might be
1926 * changed. reread the identify device info.
1927 */
Tejun Heobff04642006-11-10 18:08:10 +09001928 flags &= ~ATA_READID_POSTRESET;
Tejun Heo49016ac2006-02-21 02:12:11 +09001929 goto retry;
1930 }
1931 }
1932
1933 *p_class = class;
Tejun Heofe635c72006-05-15 20:57:35 +09001934
Tejun Heo49016ac2006-02-21 02:12:11 +09001935 return 0;
1936
1937 err_out:
Tejun Heo88574552006-06-25 20:00:35 +09001938 if (ata_msg_warn(ap))
Joe Perchesa9a79df2011-04-15 15:51:59 -07001939 ata_dev_warn(dev, "failed to IDENTIFY (%s, err_mask=0x%x)\n",
1940 reason, err_mask);
Tejun Heo49016ac2006-02-21 02:12:11 +09001941 return rc;
1942}
1943
Christoph Hellwigf01f62c2017-06-04 14:42:20 +02001944/**
1945 * ata_read_log_page - read a specific log page
1946 * @dev: target device
1947 * @log: log to read
1948 * @page: page to read
1949 * @buf: buffer to store read page
1950 * @sectors: number of sectors to read
1951 *
1952 * Read log page using READ_LOG_EXT command.
1953 *
1954 * LOCKING:
1955 * Kernel thread context (may sleep).
1956 *
1957 * RETURNS:
1958 * 0 on success, AC_ERR_* mask otherwise.
1959 */
1960unsigned int ata_read_log_page(struct ata_device *dev, u8 log,
1961 u8 page, void *buf, unsigned int sectors)
1962{
1963 unsigned long ap_flags = dev->link->ap->flags;
1964 struct ata_taskfile tf;
1965 unsigned int err_mask;
1966 bool dma = false;
1967
1968 DPRINTK("read log page - log 0x%x, page 0x%x\n", log, page);
1969
1970 /*
1971 * Return error without actually issuing the command on controllers
1972 * which e.g. lockup on a read log page.
1973 */
1974 if (ap_flags & ATA_FLAG_NO_LOG_PAGE)
1975 return AC_ERR_DEV;
1976
1977retry:
1978 ata_tf_init(dev, &tf);
Reimar Döffingerf971a852021-10-12 08:27:44 +02001979 if (ata_dma_enabled(dev) && ata_id_has_read_log_dma_ext(dev->id) &&
Damien Le Moal7cfdfdc2017-07-10 14:45:20 +09001980 !(dev->horkage & ATA_HORKAGE_NO_DMA_LOG)) {
Christoph Hellwigf01f62c2017-06-04 14:42:20 +02001981 tf.command = ATA_CMD_READ_LOG_DMA_EXT;
1982 tf.protocol = ATA_PROT_DMA;
1983 dma = true;
1984 } else {
1985 tf.command = ATA_CMD_READ_LOG_EXT;
1986 tf.protocol = ATA_PROT_PIO;
1987 dma = false;
1988 }
1989 tf.lbal = log;
1990 tf.lbam = page;
1991 tf.nsect = sectors;
1992 tf.hob_nsect = sectors >> 8;
1993 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_LBA48 | ATA_TFLAG_DEVICE;
1994
1995 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_FROM_DEVICE,
1996 buf, sectors * ATA_SECT_SIZE, 0);
1997
Damien Le Moalfc5c8aa2021-08-16 10:44:52 +09001998 if (err_mask) {
1999 if (dma) {
2000 dev->horkage |= ATA_HORKAGE_NO_DMA_LOG;
2001 goto retry;
2002 }
Damien Le Moal23ef63d2021-11-15 12:37:46 +09002003 ata_dev_err(dev,
2004 "Read log 0x%02x page 0x%02x failed, Emask 0x%x\n",
2005 (unsigned int)log, (unsigned int)page, err_mask);
Christoph Hellwigf01f62c2017-06-04 14:42:20 +02002006 }
2007
Christoph Hellwigf01f62c2017-06-04 14:42:20 +02002008 return err_mask;
2009}
2010
Christoph Hellwigefe205a2017-06-04 14:42:21 +02002011static bool ata_log_supported(struct ata_device *dev, u8 log)
2012{
2013 struct ata_port *ap = dev->link->ap;
2014
2015 if (ata_read_log_page(dev, ATA_LOG_DIRECTORY, 0, ap->sector_buf, 1))
2016 return false;
2017 return get_unaligned_le16(&ap->sector_buf[log * 2]) ? true : false;
2018}
2019
Christoph Hellwiga0fd2452017-06-04 14:42:23 +02002020static bool ata_identify_page_supported(struct ata_device *dev, u8 page)
2021{
2022 struct ata_port *ap = dev->link->ap;
2023 unsigned int err, i;
2024
Damien Le Moal636f6e22021-11-09 08:45:25 +09002025 if (dev->horkage & ATA_HORKAGE_NO_ID_DEV_LOG)
2026 return false;
2027
Christoph Hellwiga0fd2452017-06-04 14:42:23 +02002028 if (!ata_log_supported(dev, ATA_LOG_IDENTIFY_DEVICE)) {
Damien Le Moal636f6e22021-11-09 08:45:25 +09002029 /*
2030 * IDENTIFY DEVICE data log is defined as mandatory starting
2031 * with ACS-3 (ATA version 10). Warn about the missing log
2032 * for drives which implement this ATA level or above.
2033 */
2034 if (ata_id_major_version(dev->id) >= 10)
2035 ata_dev_warn(dev,
2036 "ATA Identify Device Log not supported\n");
2037 dev->horkage |= ATA_HORKAGE_NO_ID_DEV_LOG;
Christoph Hellwiga0fd2452017-06-04 14:42:23 +02002038 return false;
2039 }
2040
2041 /*
2042 * Read IDENTIFY DEVICE data log, page 0, to figure out if the page is
2043 * supported.
2044 */
2045 err = ata_read_log_page(dev, ATA_LOG_IDENTIFY_DEVICE, 0, ap->sector_buf,
2046 1);
Damien Le Moalfc5c8aa2021-08-16 10:44:52 +09002047 if (err)
Christoph Hellwiga0fd2452017-06-04 14:42:23 +02002048 return false;
Christoph Hellwiga0fd2452017-06-04 14:42:23 +02002049
2050 for (i = 0; i < ap->sector_buf[8]; i++) {
2051 if (ap->sector_buf[9 + i] == page)
2052 return true;
2053 }
2054
2055 return false;
2056}
2057
Tejun Heo90627122009-01-29 20:31:36 +09002058static int ata_do_link_spd_horkage(struct ata_device *dev)
2059{
2060 struct ata_link *plink = ata_dev_phys_link(dev);
2061 u32 target, target_limit;
2062
2063 if (!sata_scr_valid(plink))
2064 return 0;
2065
2066 if (dev->horkage & ATA_HORKAGE_1_5_GBPS)
2067 target = 1;
2068 else
2069 return 0;
2070
2071 target_limit = (1 << target) - 1;
2072
2073 /* if already on stricter limit, no need to push further */
2074 if (plink->sata_spd_limit <= target_limit)
2075 return 0;
2076
2077 plink->sata_spd_limit = target_limit;
2078
2079 /* Request another EH round by returning -EAGAIN if link is
2080 * going faster than the target speed. Forward progress is
2081 * guaranteed by setting sata_spd_limit to target_limit above.
2082 */
2083 if (plink->sata_spd > target) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07002084 ata_dev_info(dev, "applying link speed limit horkage to %s\n",
2085 sata_spd_string(target));
Tejun Heo90627122009-01-29 20:31:36 +09002086 return -EAGAIN;
2087 }
2088 return 0;
2089}
2090
Tejun Heo3373efd2006-05-15 20:57:53 +09002091static inline u8 ata_dev_knobble(struct ata_device *dev)
Tejun Heo4b2f3ed2006-03-01 16:09:36 +09002092{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09002093 struct ata_port *ap = dev->link->ap;
Jens Axboe9ce8e302008-08-27 15:23:18 +02002094
2095 if (ata_dev_blacklisted(dev) & ATA_HORKAGE_BRIDGE_OK)
2096 return 0;
2097
Tejun Heo9af5c9c2007-08-06 18:36:22 +09002098 return ((ap->cbl == ATA_CBL_SATA) && (!ata_id_is_sata(dev->id)));
Tejun Heo4b2f3ed2006-03-01 16:09:36 +09002099}
2100
Hannes Reinecke5a233552016-04-25 12:45:46 +02002101static void ata_dev_config_ncq_send_recv(struct ata_device *dev)
2102{
2103 struct ata_port *ap = dev->link->ap;
2104 unsigned int err_mask;
2105
Christoph Hellwigefe205a2017-06-04 14:42:21 +02002106 if (!ata_log_supported(dev, ATA_LOG_NCQ_SEND_RECV)) {
2107 ata_dev_warn(dev, "NCQ Send/Recv Log not supported\n");
Hannes Reineckefe5af0c2016-04-25 12:45:48 +02002108 return;
2109 }
Hannes Reinecke5a233552016-04-25 12:45:46 +02002110 err_mask = ata_read_log_page(dev, ATA_LOG_NCQ_SEND_RECV,
2111 0, ap->sector_buf, 1);
Damien Le Moalfc5c8aa2021-08-16 10:44:52 +09002112 if (!err_mask) {
Hannes Reinecke5a233552016-04-25 12:45:46 +02002113 u8 *cmds = dev->ncq_send_recv_cmds;
2114
2115 dev->flags |= ATA_DFLAG_NCQ_SEND_RECV;
2116 memcpy(cmds, ap->sector_buf, ATA_LOG_NCQ_SEND_RECV_SIZE);
2117
2118 if (dev->horkage & ATA_HORKAGE_NO_NCQ_TRIM) {
2119 ata_dev_dbg(dev, "disabling queued TRIM support\n");
2120 cmds[ATA_LOG_NCQ_SEND_RECV_DSM_OFFSET] &=
2121 ~ATA_LOG_NCQ_SEND_RECV_DSM_TRIM;
2122 }
2123 }
2124}
2125
Hannes Reinecke284b3b72016-04-25 12:45:54 +02002126static void ata_dev_config_ncq_non_data(struct ata_device *dev)
2127{
2128 struct ata_port *ap = dev->link->ap;
2129 unsigned int err_mask;
Hannes Reinecke284b3b72016-04-25 12:45:54 +02002130
Christoph Hellwigefe205a2017-06-04 14:42:21 +02002131 if (!ata_log_supported(dev, ATA_LOG_NCQ_NON_DATA)) {
Hannes Reinecke284b3b72016-04-25 12:45:54 +02002132 ata_dev_warn(dev,
2133 "NCQ Send/Recv Log not supported\n");
2134 return;
2135 }
2136 err_mask = ata_read_log_page(dev, ATA_LOG_NCQ_NON_DATA,
2137 0, ap->sector_buf, 1);
Damien Le Moalfc5c8aa2021-08-16 10:44:52 +09002138 if (!err_mask) {
Hannes Reinecke284b3b72016-04-25 12:45:54 +02002139 u8 *cmds = dev->ncq_non_data_cmds;
2140
2141 memcpy(cmds, ap->sector_buf, ATA_LOG_NCQ_NON_DATA_SIZE);
2142 }
2143}
2144
Adam Manzanares8e061782016-10-17 11:27:29 -07002145static void ata_dev_config_ncq_prio(struct ata_device *dev)
2146{
2147 struct ata_port *ap = dev->link->ap;
2148 unsigned int err_mask;
2149
Damien Le Moal06f6c4c2021-11-15 12:47:26 +09002150 if (!ata_identify_page_supported(dev, ATA_LOG_SATA_SETTINGS))
2151 return;
2152
Adam Manzanares8e061782016-10-17 11:27:29 -07002153 err_mask = ata_read_log_page(dev,
Christoph Hellwig1d51d5f2017-06-04 14:42:22 +02002154 ATA_LOG_IDENTIFY_DEVICE,
Adam Manzanares8e061782016-10-17 11:27:29 -07002155 ATA_LOG_SATA_SETTINGS,
2156 ap->sector_buf,
2157 1);
Damien Le Moalfc5c8aa2021-08-16 10:44:52 +09002158 if (err_mask)
Damien Le Moal2360fa12021-08-16 10:44:51 +09002159 goto not_supported;
Adam Manzanares8e061782016-10-17 11:27:29 -07002160
Damien Le Moal2360fa12021-08-16 10:44:51 +09002161 if (!(ap->sector_buf[ATA_LOG_NCQ_PRIO_OFFSET] & BIT(3)))
2162 goto not_supported;
Adam Manzanares8e061782016-10-17 11:27:29 -07002163
Damien Le Moal2360fa12021-08-16 10:44:51 +09002164 dev->flags |= ATA_DFLAG_NCQ_PRIO;
2165
2166 return;
2167
2168not_supported:
2169 dev->flags &= ~ATA_DFLAG_NCQ_PRIO_ENABLE;
2170 dev->flags &= ~ATA_DFLAG_NCQ_PRIO;
Adam Manzanares8e061782016-10-17 11:27:29 -07002171}
2172
Kate Hsuan7a8526a2021-09-03 17:44:11 +08002173static bool ata_dev_check_adapter(struct ata_device *dev,
2174 unsigned short vendor_id)
2175{
2176 struct pci_dev *pcidev = NULL;
2177 struct device *parent_dev = NULL;
2178
2179 for (parent_dev = dev->tdev.parent; parent_dev != NULL;
2180 parent_dev = parent_dev->parent) {
2181 if (dev_is_pci(parent_dev)) {
2182 pcidev = to_pci_dev(parent_dev);
2183 if (pcidev->vendor == vendor_id)
2184 return true;
2185 break;
2186 }
2187 }
2188
2189 return false;
2190}
2191
Shaohua Li388539f2009-07-27 09:24:35 +08002192static int ata_dev_config_ncq(struct ata_device *dev,
Tejun Heoa6e6ce82006-05-15 21:03:48 +09002193 char *desc, size_t desc_sz)
2194{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09002195 struct ata_port *ap = dev->link->ap;
Tejun Heoa6e6ce82006-05-15 21:03:48 +09002196 int hdepth = 0, ddepth = ata_id_queue_depth(dev->id);
Shaohua Li388539f2009-07-27 09:24:35 +08002197 unsigned int err_mask;
2198 char *aa_desc = "";
Tejun Heoa6e6ce82006-05-15 21:03:48 +09002199
2200 if (!ata_id_has_ncq(dev->id)) {
2201 desc[0] = '\0';
Shaohua Li388539f2009-07-27 09:24:35 +08002202 return 0;
Tejun Heoa6e6ce82006-05-15 21:03:48 +09002203 }
Bartlomiej Zolnierkiewiczcba97ea2020-03-26 16:58:09 +01002204 if (!IS_ENABLED(CONFIG_SATA_HOST))
2205 return 0;
Tejun Heo75683fe2007-07-05 13:31:27 +09002206 if (dev->horkage & ATA_HORKAGE_NONCQ) {
Alan Cox6919a0a2006-10-27 19:08:46 -07002207 snprintf(desc, desc_sz, "NCQ (not used)");
Shaohua Li388539f2009-07-27 09:24:35 +08002208 return 0;
Alan Cox6919a0a2006-10-27 19:08:46 -07002209 }
Kate Hsuan7a8526a2021-09-03 17:44:11 +08002210
2211 if (dev->horkage & ATA_HORKAGE_NO_NCQ_ON_ATI &&
2212 ata_dev_check_adapter(dev, PCI_VENDOR_ID_ATI)) {
2213 snprintf(desc, desc_sz, "NCQ (not used)");
2214 return 0;
2215 }
2216
Tejun Heoa6e6ce82006-05-15 21:03:48 +09002217 if (ap->flags & ATA_FLAG_NCQ) {
Jens Axboe69278f72018-05-11 12:51:10 -06002218 hdepth = min(ap->scsi_host->can_queue, ATA_MAX_QUEUE);
Tejun Heoa6e6ce82006-05-15 21:03:48 +09002219 dev->flags |= ATA_DFLAG_NCQ;
2220 }
2221
Shaohua Li388539f2009-07-27 09:24:35 +08002222 if (!(dev->horkage & ATA_HORKAGE_BROKEN_FPDMA_AA) &&
2223 (ap->flags & ATA_FLAG_FPDMA_AA) &&
2224 ata_id_has_fpdma_aa(dev->id)) {
2225 err_mask = ata_dev_set_feature(dev, SETFEATURES_SATA_ENABLE,
2226 SATA_FPDMA_AA);
2227 if (err_mask) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07002228 ata_dev_err(dev,
2229 "failed to enable AA (error_mask=0x%x)\n",
2230 err_mask);
Shaohua Li388539f2009-07-27 09:24:35 +08002231 if (err_mask != AC_ERR_DEV) {
2232 dev->horkage |= ATA_HORKAGE_BROKEN_FPDMA_AA;
2233 return -EIO;
2234 }
2235 } else
2236 aa_desc = ", AA";
2237 }
2238
Tejun Heoa6e6ce82006-05-15 21:03:48 +09002239 if (hdepth >= ddepth)
Shaohua Li388539f2009-07-27 09:24:35 +08002240 snprintf(desc, desc_sz, "NCQ (depth %d)%s", ddepth, aa_desc);
Tejun Heoa6e6ce82006-05-15 21:03:48 +09002241 else
Shaohua Li388539f2009-07-27 09:24:35 +08002242 snprintf(desc, desc_sz, "NCQ (depth %d/%d)%s", hdepth,
2243 ddepth, aa_desc);
Marc Carinoed369112013-08-24 23:22:50 -07002244
Hannes Reinecke284b3b72016-04-25 12:45:54 +02002245 if ((ap->flags & ATA_FLAG_FPDMA_AUX)) {
2246 if (ata_id_has_ncq_send_and_recv(dev->id))
2247 ata_dev_config_ncq_send_recv(dev);
2248 if (ata_id_has_ncq_non_data(dev->id))
2249 ata_dev_config_ncq_non_data(dev);
Adam Manzanares8e061782016-10-17 11:27:29 -07002250 if (ata_id_has_ncq_prio(dev->id))
2251 ata_dev_config_ncq_prio(dev);
Marc Carinoed369112013-08-24 23:22:50 -07002252 }
2253
Shaohua Li388539f2009-07-27 09:24:35 +08002254 return 0;
Tejun Heoa6e6ce82006-05-15 21:03:48 +09002255}
2256
Hannes Reineckee87fd282016-04-04 11:43:55 +02002257static void ata_dev_config_sense_reporting(struct ata_device *dev)
2258{
2259 unsigned int err_mask;
2260
2261 if (!ata_id_has_sense_reporting(dev->id))
2262 return;
2263
2264 if (ata_id_sense_reporting_enabled(dev->id))
2265 return;
2266
2267 err_mask = ata_dev_set_feature(dev, SETFEATURE_SENSE_DATA, 0x1);
2268 if (err_mask) {
2269 ata_dev_dbg(dev,
2270 "failed to enable Sense Data Reporting, Emask 0x%x\n",
2271 err_mask);
2272 }
2273}
2274
Hannes Reinecke6d1003a2016-04-25 12:45:56 +02002275static void ata_dev_config_zac(struct ata_device *dev)
2276{
2277 struct ata_port *ap = dev->link->ap;
2278 unsigned int err_mask;
2279 u8 *identify_buf = ap->sector_buf;
Hannes Reinecke6d1003a2016-04-25 12:45:56 +02002280
2281 dev->zac_zones_optimal_open = U32_MAX;
2282 dev->zac_zones_optimal_nonseq = U32_MAX;
2283 dev->zac_zones_max_open = U32_MAX;
2284
2285 /*
2286 * Always set the 'ZAC' flag for Host-managed devices.
2287 */
2288 if (dev->class == ATA_DEV_ZAC)
2289 dev->flags |= ATA_DFLAG_ZAC;
2290 else if (ata_id_zoned_cap(dev->id) == 0x01)
2291 /*
2292 * Check for host-aware devices.
2293 */
2294 dev->flags |= ATA_DFLAG_ZAC;
2295
2296 if (!(dev->flags & ATA_DFLAG_ZAC))
2297 return;
2298
Christoph Hellwiga0fd2452017-06-04 14:42:23 +02002299 if (!ata_identify_page_supported(dev, ATA_LOG_ZONED_INFORMATION)) {
Hannes Reinecke6d1003a2016-04-25 12:45:56 +02002300 ata_dev_warn(dev,
2301 "ATA Zoned Information Log not supported\n");
2302 return;
2303 }
2304
2305 /*
2306 * Read IDENTIFY DEVICE data log, page 9 (Zoned-device information)
2307 */
Christoph Hellwig1d51d5f2017-06-04 14:42:22 +02002308 err_mask = ata_read_log_page(dev, ATA_LOG_IDENTIFY_DEVICE,
Hannes Reinecke6d1003a2016-04-25 12:45:56 +02002309 ATA_LOG_ZONED_INFORMATION,
2310 identify_buf, 1);
2311 if (!err_mask) {
2312 u64 zoned_cap, opt_open, opt_nonseq, max_open;
2313
2314 zoned_cap = get_unaligned_le64(&identify_buf[8]);
2315 if ((zoned_cap >> 63))
2316 dev->zac_zoned_cap = (zoned_cap & 1);
2317 opt_open = get_unaligned_le64(&identify_buf[24]);
2318 if ((opt_open >> 63))
2319 dev->zac_zones_optimal_open = (u32)opt_open;
2320 opt_nonseq = get_unaligned_le64(&identify_buf[32]);
2321 if ((opt_nonseq >> 63))
2322 dev->zac_zones_optimal_nonseq = (u32)opt_nonseq;
2323 max_open = get_unaligned_le64(&identify_buf[40]);
2324 if ((max_open >> 63))
2325 dev->zac_zones_max_open = (u32)max_open;
2326 }
2327}
2328
Christoph Hellwig818831c2017-06-04 14:42:24 +02002329static void ata_dev_config_trusted(struct ata_device *dev)
2330{
2331 struct ata_port *ap = dev->link->ap;
2332 u64 trusted_cap;
2333 unsigned int err;
2334
Christoph Hellwige8f11db2017-08-29 14:42:06 +02002335 if (!ata_id_has_trusted(dev->id))
2336 return;
2337
Christoph Hellwig818831c2017-06-04 14:42:24 +02002338 if (!ata_identify_page_supported(dev, ATA_LOG_SECURITY)) {
2339 ata_dev_warn(dev,
2340 "Security Log not supported\n");
2341 return;
2342 }
2343
2344 err = ata_read_log_page(dev, ATA_LOG_IDENTIFY_DEVICE, ATA_LOG_SECURITY,
2345 ap->sector_buf, 1);
Damien Le Moalfc5c8aa2021-08-16 10:44:52 +09002346 if (err)
Christoph Hellwig818831c2017-06-04 14:42:24 +02002347 return;
Christoph Hellwig818831c2017-06-04 14:42:24 +02002348
2349 trusted_cap = get_unaligned_le64(&ap->sector_buf[40]);
2350 if (!(trusted_cap & (1ULL << 63))) {
2351 ata_dev_dbg(dev,
2352 "Trusted Computing capability qword not valid!\n");
2353 return;
2354 }
2355
2356 if (trusted_cap & (1 << 0))
2357 dev->flags |= ATA_DFLAG_TRUSTED;
2358}
2359
Damien Le Moal891fd7c2021-08-16 10:44:50 +09002360static int ata_dev_config_lba(struct ata_device *dev)
2361{
2362 struct ata_port *ap = dev->link->ap;
2363 const u16 *id = dev->id;
2364 const char *lba_desc;
2365 char ncq_desc[24];
2366 int ret;
2367
2368 dev->flags |= ATA_DFLAG_LBA;
2369
2370 if (ata_id_has_lba48(id)) {
2371 lba_desc = "LBA48";
2372 dev->flags |= ATA_DFLAG_LBA48;
2373 if (dev->n_sectors >= (1UL << 28) &&
2374 ata_id_has_flush_ext(id))
2375 dev->flags |= ATA_DFLAG_FLUSH_EXT;
2376 } else {
2377 lba_desc = "LBA";
2378 }
2379
2380 /* config NCQ */
2381 ret = ata_dev_config_ncq(dev, ncq_desc, sizeof(ncq_desc));
2382
2383 /* print device info to dmesg */
2384 if (ata_msg_drv(ap) && ata_dev_print_info(dev))
2385 ata_dev_info(dev,
2386 "%llu sectors, multi %u: %s %s\n",
2387 (unsigned long long)dev->n_sectors,
2388 dev->multi_count, lba_desc, ncq_desc);
2389
2390 return ret;
2391}
2392
2393static void ata_dev_config_chs(struct ata_device *dev)
2394{
2395 struct ata_port *ap = dev->link->ap;
2396 const u16 *id = dev->id;
2397
2398 if (ata_id_current_chs_valid(id)) {
2399 /* Current CHS translation is valid. */
2400 dev->cylinders = id[54];
2401 dev->heads = id[55];
2402 dev->sectors = id[56];
2403 } else {
2404 /* Default translation */
2405 dev->cylinders = id[1];
2406 dev->heads = id[3];
2407 dev->sectors = id[6];
2408 }
2409
2410 /* print device info to dmesg */
2411 if (ata_msg_drv(ap) && ata_dev_print_info(dev))
2412 ata_dev_info(dev,
2413 "%llu sectors, multi %u, CHS %u/%u/%u\n",
2414 (unsigned long long)dev->n_sectors,
2415 dev->multi_count, dev->cylinders,
2416 dev->heads, dev->sectors);
2417}
2418
Damien Le Moald8d87782021-08-16 10:44:49 +09002419static void ata_dev_config_devslp(struct ata_device *dev)
2420{
2421 u8 *sata_setting = dev->link->ap->sector_buf;
2422 unsigned int err_mask;
2423 int i, j;
2424
2425 /*
2426 * Check device sleep capability. Get DevSlp timing variables
2427 * from SATA Settings page of Identify Device Data Log.
2428 */
Damien Le Moal06f6c4c2021-11-15 12:47:26 +09002429 if (!ata_id_has_devslp(dev->id) ||
2430 !ata_identify_page_supported(dev, ATA_LOG_SATA_SETTINGS))
Damien Le Moald8d87782021-08-16 10:44:49 +09002431 return;
2432
2433 err_mask = ata_read_log_page(dev,
2434 ATA_LOG_IDENTIFY_DEVICE,
2435 ATA_LOG_SATA_SETTINGS,
2436 sata_setting, 1);
Damien Le Moalfc5c8aa2021-08-16 10:44:52 +09002437 if (err_mask)
Damien Le Moald8d87782021-08-16 10:44:49 +09002438 return;
Damien Le Moald8d87782021-08-16 10:44:49 +09002439
2440 dev->flags |= ATA_DFLAG_DEVSLP;
2441 for (i = 0; i < ATA_LOG_DEVSLP_SIZE; i++) {
2442 j = ATA_LOG_DEVSLP_OFFSET + i;
2443 dev->devslp_timing[i] = sata_setting[j];
2444 }
2445}
2446
Damien Le Moalfe22e1c2021-10-27 11:22:21 +09002447static void ata_dev_config_cpr(struct ata_device *dev)
2448{
2449 unsigned int err_mask;
2450 size_t buf_len;
2451 int i, nr_cpr = 0;
2452 struct ata_cpr_log *cpr_log = NULL;
2453 u8 *desc, *buf = NULL;
2454
2455 if (!ata_identify_page_supported(dev,
2456 ATA_LOG_CONCURRENT_POSITIONING_RANGES))
2457 goto out;
2458
2459 /*
2460 * Read IDENTIFY DEVICE data log, page 0x47
2461 * (concurrent positioning ranges). We can have at most 255 32B range
2462 * descriptors plus a 64B header.
2463 */
2464 buf_len = (64 + 255 * 32 + 511) & ~511;
2465 buf = kzalloc(buf_len, GFP_KERNEL);
2466 if (!buf)
2467 goto out;
2468
2469 err_mask = ata_read_log_page(dev, ATA_LOG_IDENTIFY_DEVICE,
2470 ATA_LOG_CONCURRENT_POSITIONING_RANGES,
2471 buf, buf_len >> 9);
2472 if (err_mask)
2473 goto out;
2474
2475 nr_cpr = buf[0];
2476 if (!nr_cpr)
2477 goto out;
2478
2479 cpr_log = kzalloc(struct_size(cpr_log, cpr, nr_cpr), GFP_KERNEL);
2480 if (!cpr_log)
2481 goto out;
2482
2483 cpr_log->nr_cpr = nr_cpr;
2484 desc = &buf[64];
2485 for (i = 0; i < nr_cpr; i++, desc += 32) {
2486 cpr_log->cpr[i].num = desc[0];
2487 cpr_log->cpr[i].num_storage_elements = desc[1];
2488 cpr_log->cpr[i].start_lba = get_unaligned_le64(&desc[8]);
2489 cpr_log->cpr[i].num_lbas = get_unaligned_le64(&desc[16]);
2490 }
2491
2492out:
2493 swap(dev->cpr_log, cpr_log);
2494 kfree(cpr_log);
2495 kfree(buf);
2496}
2497
Damien Le Moald633b8a2021-08-16 10:44:53 +09002498static void ata_dev_print_features(struct ata_device *dev)
2499{
2500 if (!(dev->flags & ATA_DFLAG_FEATURES_MASK))
2501 return;
2502
2503 ata_dev_info(dev,
Damien Le Moalfe22e1c2021-10-27 11:22:21 +09002504 "Features:%s%s%s%s%s%s\n",
Damien Le Moald633b8a2021-08-16 10:44:53 +09002505 dev->flags & ATA_DFLAG_TRUSTED ? " Trust" : "",
2506 dev->flags & ATA_DFLAG_DA ? " Dev-Attention" : "",
2507 dev->flags & ATA_DFLAG_DEVSLP ? " Dev-Sleep" : "",
2508 dev->flags & ATA_DFLAG_NCQ_SEND_RECV ? " NCQ-sndrcv" : "",
Damien Le Moalfe22e1c2021-10-27 11:22:21 +09002509 dev->flags & ATA_DFLAG_NCQ_PRIO ? " NCQ-prio" : "",
2510 dev->cpr_log ? " CPR" : "");
Damien Le Moald633b8a2021-08-16 10:44:53 +09002511}
2512
Tejun Heo49016ac2006-02-21 02:12:11 +09002513/**
Tejun Heoffeae412006-03-01 16:09:35 +09002514 * ata_dev_configure - Configure the specified ATA/ATAPI device
Tejun Heoffeae412006-03-01 16:09:35 +09002515 * @dev: Target device to configure
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516 *
Tejun Heoffeae412006-03-01 16:09:35 +09002517 * Configure @dev according to @dev->id. Generic and low-level
2518 * driver specific fixups are also applied.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519 *
2520 * LOCKING:
Tejun Heoffeae412006-03-01 16:09:35 +09002521 * Kernel thread context (may sleep)
2522 *
2523 * RETURNS:
2524 * 0 on success, -errno otherwise
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525 */
Tejun Heoefdaedc2006-11-01 18:38:52 +09002526int ata_dev_configure(struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09002528 struct ata_port *ap = dev->link->ap;
Damien Le Moal891fd7c2021-08-16 10:44:50 +09002529 bool print_info = ata_dev_print_info(dev);
Tejun Heo1148c3a2006-03-13 19:48:04 +09002530 const u16 *id = dev->id;
Tejun Heo7dc951a2007-11-27 19:43:42 +09002531 unsigned long xfer_mask;
Shane Huang65fe1f02012-09-07 22:40:01 +08002532 unsigned int err_mask;
Alan Coxb352e572006-08-10 18:52:12 +01002533 char revbuf[7]; /* XYZ-99\0 */
Eric D. Mudama3f64f562007-01-30 23:00:40 -07002534 char fwrevbuf[ATA_ID_FW_REV_LEN+1];
2535 char modelbuf[ATA_ID_PROD_LEN+1];
Brian Kinge6d902a2006-06-28 08:30:31 -05002536 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537
Borislav Petkov0dd4b212006-06-23 02:29:08 -04002538 if (!ata_dev_enabled(dev) && ata_msg_info(ap)) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07002539 ata_dev_info(dev, "%s: ENTER/EXIT -- nodev\n", __func__);
Tejun Heoffeae412006-03-01 16:09:35 +09002540 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541 }
2542
Borislav Petkov0dd4b212006-06-23 02:29:08 -04002543 if (ata_msg_probe(ap))
Joe Perchesa9a79df2011-04-15 15:51:59 -07002544 ata_dev_dbg(dev, "%s: ENTER\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545
Tejun Heo75683fe2007-07-05 13:31:27 +09002546 /* set horkage */
2547 dev->horkage |= ata_dev_blacklisted(dev);
Tejun Heo33267322008-02-13 09:15:09 +09002548 ata_force_horkage(dev);
Tejun Heo75683fe2007-07-05 13:31:27 +09002549
Tejun Heo50af2fa2008-05-19 01:15:14 +09002550 if (dev->horkage & ATA_HORKAGE_DISABLE) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07002551 ata_dev_info(dev, "unsupported device, disabling\n");
Tejun Heo50af2fa2008-05-19 01:15:14 +09002552 ata_dev_disable(dev);
2553 return 0;
2554 }
2555
Tejun Heo2486fa52008-07-31 07:52:40 +09002556 if ((!atapi_enabled || (ap->flags & ATA_FLAG_NO_ATAPI)) &&
2557 dev->class == ATA_DEV_ATAPI) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07002558 ata_dev_warn(dev, "WARNING: ATAPI is %s, device ignored\n",
2559 atapi_enabled ? "not supported with this driver"
2560 : "disabled");
Tejun Heo2486fa52008-07-31 07:52:40 +09002561 ata_dev_disable(dev);
2562 return 0;
2563 }
2564
Tejun Heo90627122009-01-29 20:31:36 +09002565 rc = ata_do_link_spd_horkage(dev);
2566 if (rc)
2567 return rc;
2568
Tejun Heoecd75ad2014-01-16 09:47:17 -05002569 /* some WD SATA-1 drives have issues with LPM, turn on NOLPM for them */
2570 if ((dev->horkage & ATA_HORKAGE_WD_BROKEN_LPM) &&
2571 (id[ATA_ID_SATA_CAPABILITY] & 0xe) == 0x2)
2572 dev->horkage |= ATA_HORKAGE_NOLPM;
2573
Hans de Goede240630e2018-07-01 12:15:46 +02002574 if (ap->flags & ATA_FLAG_NO_LPM)
2575 dev->horkage |= ATA_HORKAGE_NOLPM;
2576
Tejun Heoecd75ad2014-01-16 09:47:17 -05002577 if (dev->horkage & ATA_HORKAGE_NOLPM) {
2578 ata_dev_warn(dev, "LPM support broken, forcing max_power\n");
2579 dev->link->ap->target_lpm_policy = ATA_LPM_MAX_POWER;
2580 }
2581
Tejun Heo67465442007-05-15 03:28:16 +09002582 /* let ACPI work its magic */
2583 rc = ata_acpi_on_devcfg(dev);
2584 if (rc)
2585 return rc;
Kristen Carlson Accardi08573a82006-11-10 16:14:47 -08002586
Tejun Heo05027ad2007-09-03 12:32:57 +09002587 /* massage HPA, do it early as it might change IDENTIFY data */
2588 rc = ata_hpa_resize(dev);
2589 if (rc)
2590 return rc;
2591
Tejun Heoc39f5eb2006-03-13 19:51:19 +09002592 /* print device capabilities */
Borislav Petkov0dd4b212006-06-23 02:29:08 -04002593 if (ata_msg_probe(ap))
Joe Perchesa9a79df2011-04-15 15:51:59 -07002594 ata_dev_dbg(dev,
2595 "%s: cfg 49:%04x 82:%04x 83:%04x 84:%04x "
2596 "85:%04x 86:%04x 87:%04x 88:%04x\n",
2597 __func__,
2598 id[49], id[82], id[83], id[84],
2599 id[85], id[86], id[87], id[88]);
Tejun Heoc39f5eb2006-03-13 19:51:19 +09002600
Tejun Heo208a9932006-03-05 17:55:58 +09002601 /* initialize to-be-configured parameters */
Tejun Heoea1dd4e2006-04-02 18:51:53 +09002602 dev->flags &= ~ATA_DFLAG_CFG_MASK;
Tejun Heo208a9932006-03-05 17:55:58 +09002603 dev->max_sectors = 0;
2604 dev->cdb_len = 0;
2605 dev->n_sectors = 0;
2606 dev->cylinders = 0;
2607 dev->heads = 0;
2608 dev->sectors = 0;
Mark Lorde18086d2009-03-19 13:32:21 -04002609 dev->multi_count = 0;
Tejun Heo208a9932006-03-05 17:55:58 +09002610
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611 /*
2612 * common ATA, ATAPI feature tests
2613 */
2614
Tejun Heoff8854b2006-03-06 04:31:56 +09002615 /* find max transfer mode; for printk only */
Tejun Heo1148c3a2006-03-13 19:48:04 +09002616 xfer_mask = ata_id_xfermask(id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617
Hannes Reinecke6044f3c2021-12-21 08:20:27 +01002618 ata_dump_id(dev, id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619
Albert Leeef143d52007-06-05 13:01:33 +08002620 /* SCSI only uses 4-char revisions, dump full 8 chars from ATA */
2621 ata_id_c_string(dev->id, fwrevbuf, ATA_ID_FW_REV,
2622 sizeof(fwrevbuf));
2623
2624 ata_id_c_string(dev->id, modelbuf, ATA_ID_PROD,
2625 sizeof(modelbuf));
2626
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627 /* ATA-specific feature tests */
Hannes Reinecke9162c652014-11-05 13:08:21 +01002628 if (dev->class == ATA_DEV_ATA || dev->class == ATA_DEV_ZAC) {
Alan Coxb352e572006-08-10 18:52:12 +01002629 if (ata_id_is_cfa(id)) {
Sergei Shtylyov62afe5d2009-04-13 20:50:00 +04002630 /* CPRM may make this media unusable */
2631 if (id[ATA_ID_CFA_KEY_MGMT] & 1)
Joe Perchesa9a79df2011-04-15 15:51:59 -07002632 ata_dev_warn(dev,
2633 "supports DRM functions and may not be fully accessible\n");
Alan Coxb352e572006-08-10 18:52:12 +01002634 snprintf(revbuf, 7, "CFA");
Alan Coxae8d4ee2007-11-04 22:05:49 -05002635 } else {
Jeff Garzik2dcb4072007-10-19 06:42:56 -04002636 snprintf(revbuf, 7, "ATA-%d", ata_id_major_version(id));
Alan Coxae8d4ee2007-11-04 22:05:49 -05002637 /* Warn the user if the device has TPM extensions */
2638 if (ata_id_has_tpm(id))
Joe Perchesa9a79df2011-04-15 15:51:59 -07002639 ata_dev_warn(dev,
2640 "supports DRM functions and may not be fully accessible\n");
Alan Coxae8d4ee2007-11-04 22:05:49 -05002641 }
Alan Coxb352e572006-08-10 18:52:12 +01002642
Tejun Heo1148c3a2006-03-13 19:48:04 +09002643 dev->n_sectors = ata_id_n_sectors(id);
Tejun Heo29407402006-02-12 22:47:04 +09002644
Mark Lorde18086d2009-03-19 13:32:21 -04002645 /* get current R/W Multiple count setting */
2646 if ((dev->id[47] >> 8) == 0x80 && (dev->id[59] & 0x100)) {
2647 unsigned int max = dev->id[47] & 0xff;
2648 unsigned int cnt = dev->id[59] & 0xff;
2649 /* only recognize/allow powers of two here */
2650 if (is_power_of_2(max) && is_power_of_2(cnt))
2651 if (cnt <= max)
2652 dev->multi_count = cnt;
2653 }
Eric D. Mudama3f64f562007-01-30 23:00:40 -07002654
Damien Le Moal891fd7c2021-08-16 10:44:50 +09002655 /* print device info to dmesg */
2656 if (ata_msg_drv(ap) && print_info)
2657 ata_dev_info(dev, "%s: %s, %s, max %s\n",
2658 revbuf, modelbuf, fwrevbuf,
2659 ata_mode_string(xfer_mask));
2660
Tejun Heo1148c3a2006-03-13 19:48:04 +09002661 if (ata_id_has_lba(id)) {
Damien Le Moal891fd7c2021-08-16 10:44:50 +09002662 rc = ata_dev_config_lba(dev);
Shaohua Li388539f2009-07-27 09:24:35 +08002663 if (rc)
2664 return rc;
Tejun Heoffeae412006-03-01 16:09:35 +09002665 } else {
Damien Le Moal891fd7c2021-08-16 10:44:50 +09002666 ata_dev_config_chs(dev);
Albert Lee07f6f7d2005-11-01 19:33:20 +08002667 }
2668
Damien Le Moald8d87782021-08-16 10:44:49 +09002669 ata_dev_config_devslp(dev);
Hannes Reineckee87fd282016-04-04 11:43:55 +02002670 ata_dev_config_sense_reporting(dev);
Hannes Reinecke6d1003a2016-04-25 12:45:56 +02002671 ata_dev_config_zac(dev);
Christoph Hellwig818831c2017-06-04 14:42:24 +02002672 ata_dev_config_trusted(dev);
Damien Le Moalfe22e1c2021-10-27 11:22:21 +09002673 ata_dev_config_cpr(dev);
Minwoo Imb1ffbf82017-06-24 03:41:10 +09002674 dev->cdb_len = 32;
Damien Le Moald633b8a2021-08-16 10:44:53 +09002675
2676 if (ata_msg_drv(ap) && print_info)
2677 ata_dev_print_features(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678 }
2679
2680 /* ATAPI-specific feature tests */
Jeff Garzik2c13b7c2005-11-14 14:14:16 -05002681 else if (dev->class == ATA_DEV_ATAPI) {
Tejun Heo854c73a2007-09-23 13:14:11 +09002682 const char *cdb_intr_string = "";
2683 const char *atapi_an_string = "";
Tejun Heo91163002008-02-21 13:25:50 +09002684 const char *dma_dir_string = "";
Tejun Heo7d77b242007-09-23 13:14:13 +09002685 u32 sntf;
Albert Lee08a556d2006-03-31 13:29:04 +08002686
Tejun Heo1148c3a2006-03-13 19:48:04 +09002687 rc = atapi_cdb_len(id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688 if ((rc < 12) || (rc > ATAPI_CDB_LEN)) {
Borislav Petkov0dd4b212006-06-23 02:29:08 -04002689 if (ata_msg_warn(ap))
Joe Perchesa9a79df2011-04-15 15:51:59 -07002690 ata_dev_warn(dev, "unsupported CDB len\n");
Tejun Heoffeae412006-03-01 16:09:35 +09002691 rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 goto err_out_nosup;
2693 }
Tejun Heo6e7846e2006-02-12 23:32:58 +09002694 dev->cdb_len = (unsigned int) rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695
Tejun Heo7d77b242007-09-23 13:14:13 +09002696 /* Enable ATAPI AN if both the host and device have
2697 * the support. If PMP is attached, SNTF is required
2698 * to enable ATAPI AN to discern between PHY status
2699 * changed notifications and ATAPI ANs.
Kristen Carlson Accardi9f45cbd2007-08-15 03:57:11 -04002700 */
Tejun Heoe7ecd432010-05-19 15:38:58 +02002701 if (atapi_an &&
2702 (ap->flags & ATA_FLAG_AN) && ata_id_has_atapi_AN(id) &&
Tejun Heo071f44b2008-04-07 22:47:22 +09002703 (!sata_pmp_attached(ap) ||
Tejun Heo7d77b242007-09-23 13:14:13 +09002704 sata_scr_read(&ap->link, SCR_NOTIFICATION, &sntf) == 0)) {
Kristen Carlson Accardi9f45cbd2007-08-15 03:57:11 -04002705 /* issue SET feature command to turn this on */
Jeff Garzik218f3d32007-10-25 00:33:27 -04002706 err_mask = ata_dev_set_feature(dev,
2707 SETFEATURES_SATA_ENABLE, SATA_AN);
Tejun Heo854c73a2007-09-23 13:14:11 +09002708 if (err_mask)
Joe Perchesa9a79df2011-04-15 15:51:59 -07002709 ata_dev_err(dev,
2710 "failed to enable ATAPI AN (err_mask=0x%x)\n",
2711 err_mask);
Tejun Heo854c73a2007-09-23 13:14:11 +09002712 else {
Kristen Carlson Accardi9f45cbd2007-08-15 03:57:11 -04002713 dev->flags |= ATA_DFLAG_AN;
Tejun Heo854c73a2007-09-23 13:14:11 +09002714 atapi_an_string = ", ATAPI AN";
2715 }
Kristen Carlson Accardi9f45cbd2007-08-15 03:57:11 -04002716 }
2717
Albert Lee08a556d2006-03-31 13:29:04 +08002718 if (ata_id_cdb_intr(dev->id)) {
Albert Lee312f7da2005-09-27 17:38:03 +08002719 dev->flags |= ATA_DFLAG_CDB_INTR;
Albert Lee08a556d2006-03-31 13:29:04 +08002720 cdb_intr_string = ", CDB intr";
2721 }
Albert Lee312f7da2005-09-27 17:38:03 +08002722
Vincent Pelletier966fbe12013-05-21 22:30:58 +02002723 if (atapi_dmadir || (dev->horkage & ATA_HORKAGE_ATAPI_DMADIR) || atapi_id_dmadir(dev->id)) {
Tejun Heo91163002008-02-21 13:25:50 +09002724 dev->flags |= ATA_DFLAG_DMADIR;
2725 dma_dir_string = ", DMADIR";
2726 }
2727
Aaron Luafe75952013-01-15 17:20:58 +08002728 if (ata_id_has_da(dev->id)) {
Lin Mingb1354cb2012-06-25 16:13:08 +08002729 dev->flags |= ATA_DFLAG_DA;
Aaron Luafe75952013-01-15 17:20:58 +08002730 zpodd_init(dev);
2731 }
Lin Mingb1354cb2012-06-25 16:13:08 +08002732
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733 /* print device info to dmesg */
Borislav Petkov5afc8142006-06-27 14:51:25 +02002734 if (ata_msg_drv(ap) && print_info)
Joe Perchesa9a79df2011-04-15 15:51:59 -07002735 ata_dev_info(dev,
2736 "ATAPI: %s, %s, max %s%s%s%s\n",
2737 modelbuf, fwrevbuf,
2738 ata_mode_string(xfer_mask),
2739 cdb_intr_string, atapi_an_string,
2740 dma_dir_string);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741 }
2742
Tejun Heo914ed352006-11-01 18:39:55 +09002743 /* determine max_sectors */
2744 dev->max_sectors = ATA_MAX_SECTORS;
2745 if (dev->flags & ATA_DFLAG_LBA48)
2746 dev->max_sectors = ATA_MAX_SECTORS_LBA48;
2747
Alan Coxc5038fc2007-10-25 14:21:16 +01002748 /* Limit PATA drive on SATA cable bridge transfers to udma5,
2749 200 sectors */
Tejun Heo3373efd2006-05-15 20:57:53 +09002750 if (ata_dev_knobble(dev)) {
Borislav Petkov5afc8142006-06-27 14:51:25 +02002751 if (ata_msg_drv(ap) && print_info)
Joe Perchesa9a79df2011-04-15 15:51:59 -07002752 ata_dev_info(dev, "applying bridge limits\n");
Tejun Heo5a529132006-03-24 14:07:50 +09002753 dev->udma_mask &= ATA_UDMA5;
Tejun Heo4b2f3ed2006-03-01 16:09:36 +09002754 dev->max_sectors = ATA_MAX_SECTORS;
2755 }
2756
Tony Battersbyf8d8e572007-10-30 11:44:35 -04002757 if ((dev->class == ATA_DEV_ATAPI) &&
Albert Leef442cd82007-11-15 10:35:47 +09002758 (atapi_command_packet_set(id) == TYPE_TAPE)) {
Tony Battersbyf8d8e572007-10-30 11:44:35 -04002759 dev->max_sectors = ATA_MAX_SECTORS_TAPE;
Albert Leef442cd82007-11-15 10:35:47 +09002760 dev->horkage |= ATA_HORKAGE_STUCK_ERR;
2761 }
Tony Battersbyf8d8e572007-10-30 11:44:35 -04002762
Tejun Heo75683fe2007-07-05 13:31:27 +09002763 if (dev->horkage & ATA_HORKAGE_MAX_SEC_128)
Tejun Heo03ec52d2007-04-12 13:38:11 +09002764 dev->max_sectors = min_t(unsigned int, ATA_MAX_SECTORS_128,
2765 dev->max_sectors);
Albert Lee18d6e9d2007-04-02 11:34:15 +08002766
David Milburnaf34d632015-07-13 11:48:23 -05002767 if (dev->horkage & ATA_HORKAGE_MAX_SEC_1024)
2768 dev->max_sectors = min_t(unsigned int, ATA_MAX_SECTORS_1024,
2769 dev->max_sectors);
2770
Shan Haia32450e2013-03-18 10:30:44 +08002771 if (dev->horkage & ATA_HORKAGE_MAX_SEC_LBA48)
2772 dev->max_sectors = ATA_MAX_SECTORS_LBA48;
2773
Tejun Heo4b2f3ed2006-03-01 16:09:36 +09002774 if (ap->ops->dev_config)
Alancd0d3bb2007-03-02 00:56:15 +00002775 ap->ops->dev_config(dev);
Tejun Heo4b2f3ed2006-03-01 16:09:36 +09002776
Alan Coxc5038fc2007-10-25 14:21:16 +01002777 if (dev->horkage & ATA_HORKAGE_DIAGNOSTIC) {
2778 /* Let the user know. We don't want to disallow opens for
2779 rescue purposes, or in case the vendor is just a blithering
2780 idiot. Do this after the dev_config call as some controllers
2781 with buggy firmware may want to avoid reporting false device
2782 bugs */
2783
2784 if (print_info) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07002785 ata_dev_warn(dev,
Alan Coxc5038fc2007-10-25 14:21:16 +01002786"Drive reports diagnostics failure. This may indicate a drive\n");
Joe Perchesa9a79df2011-04-15 15:51:59 -07002787 ata_dev_warn(dev,
Alan Coxc5038fc2007-10-25 14:21:16 +01002788"fault or invalid emulation. Contact drive vendor for information.\n");
2789 }
2790 }
2791
Tejun Heoac70a962008-11-27 13:36:48 +09002792 if ((dev->horkage & ATA_HORKAGE_FIRMWARE_WARN) && print_info) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07002793 ata_dev_warn(dev, "WARNING: device requires firmware update to be fully functional\n");
2794 ata_dev_warn(dev, " contact the vendor or visit http://ata.wiki.kernel.org\n");
Tejun Heoac70a962008-11-27 13:36:48 +09002795 }
2796
Tejun Heoffeae412006-03-01 16:09:35 +09002797 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798
2799err_out_nosup:
Borislav Petkov0dd4b212006-06-23 02:29:08 -04002800 if (ata_msg_probe(ap))
Joe Perchesa9a79df2011-04-15 15:51:59 -07002801 ata_dev_dbg(dev, "%s: EXIT, err\n", __func__);
Tejun Heoffeae412006-03-01 16:09:35 +09002802 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803}
2804
2805/**
Alan Cox2e41e8e2007-03-08 23:19:19 +00002806 * ata_cable_40wire - return 40 wire cable type
Alan Coxbe0d18d2007-03-06 02:37:56 -08002807 * @ap: port
2808 *
Alan Cox2e41e8e2007-03-08 23:19:19 +00002809 * Helper method for drivers which want to hardwire 40 wire cable
Alan Coxbe0d18d2007-03-06 02:37:56 -08002810 * detection.
2811 */
2812
2813int ata_cable_40wire(struct ata_port *ap)
2814{
2815 return ATA_CBL_PATA40;
2816}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01002817EXPORT_SYMBOL_GPL(ata_cable_40wire);
Alan Coxbe0d18d2007-03-06 02:37:56 -08002818
2819/**
Alan Cox2e41e8e2007-03-08 23:19:19 +00002820 * ata_cable_80wire - return 80 wire cable type
Alan Coxbe0d18d2007-03-06 02:37:56 -08002821 * @ap: port
2822 *
Alan Cox2e41e8e2007-03-08 23:19:19 +00002823 * Helper method for drivers which want to hardwire 80 wire cable
Alan Coxbe0d18d2007-03-06 02:37:56 -08002824 * detection.
2825 */
2826
2827int ata_cable_80wire(struct ata_port *ap)
2828{
2829 return ATA_CBL_PATA80;
2830}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01002831EXPORT_SYMBOL_GPL(ata_cable_80wire);
Alan Coxbe0d18d2007-03-06 02:37:56 -08002832
2833/**
2834 * ata_cable_unknown - return unknown PATA cable.
2835 * @ap: port
2836 *
2837 * Helper method for drivers which have no PATA cable detection.
2838 */
2839
2840int ata_cable_unknown(struct ata_port *ap)
2841{
2842 return ATA_CBL_PATA_UNK;
2843}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01002844EXPORT_SYMBOL_GPL(ata_cable_unknown);
Alan Coxbe0d18d2007-03-06 02:37:56 -08002845
2846/**
Tejun Heoc88f90c2007-11-27 19:43:48 +09002847 * ata_cable_ignore - return ignored PATA cable.
2848 * @ap: port
2849 *
2850 * Helper method for drivers which don't use cable type to limit
2851 * transfer mode.
2852 */
2853int ata_cable_ignore(struct ata_port *ap)
2854{
2855 return ATA_CBL_PATA_IGN;
2856}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01002857EXPORT_SYMBOL_GPL(ata_cable_ignore);
Tejun Heoc88f90c2007-11-27 19:43:48 +09002858
2859/**
Alan Coxbe0d18d2007-03-06 02:37:56 -08002860 * ata_cable_sata - return SATA cable type
2861 * @ap: port
2862 *
2863 * Helper method for drivers which have SATA cables
2864 */
2865
2866int ata_cable_sata(struct ata_port *ap)
2867{
2868 return ATA_CBL_SATA;
2869}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01002870EXPORT_SYMBOL_GPL(ata_cable_sata);
Alan Coxbe0d18d2007-03-06 02:37:56 -08002871
2872/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873 * ata_bus_probe - Reset and probe ATA bus
2874 * @ap: Bus to probe
2875 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04002876 * Master ATA bus probing function. Initiates a hardware-dependent
2877 * bus reset, then attempts to identify any devices found on
2878 * the bus.
2879 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002881 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882 *
2883 * RETURNS:
Tejun Heo96072e62006-04-01 01:38:17 +09002884 * Zero on success, negative errno otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885 */
2886
Brian King80289162006-08-07 14:27:31 -05002887int ata_bus_probe(struct ata_port *ap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888{
Tejun Heo28ca5c52006-03-01 16:09:36 +09002889 unsigned int classes[ATA_MAX_DEVICES];
Tejun Heo14d2bac2006-04-02 17:54:46 +09002890 int tries[ATA_MAX_DEVICES];
Tejun Heof58229f2007-08-06 18:36:23 +09002891 int rc;
Tejun Heoe82cbdb2006-04-01 01:38:18 +09002892 struct ata_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893
Tejun Heo1eca4362008-11-03 20:03:17 +09002894 ata_for_each_dev(dev, &ap->link, ALL)
Tejun Heof58229f2007-08-06 18:36:23 +09002895 tries[dev->devno] = ATA_PROBE_MAX_TRIES;
Tejun Heo14d2bac2006-04-02 17:54:46 +09002896
2897 retry:
Tejun Heo1eca4362008-11-03 20:03:17 +09002898 ata_for_each_dev(dev, &ap->link, ALL) {
Tejun Heocdeab112007-10-29 16:41:09 +09002899 /* If we issue an SRST then an ATA drive (not ATAPI)
2900 * may change configuration and be in PIO0 timing. If
2901 * we do a hard reset (or are coming from power on)
2902 * this is true for ATA or ATAPI. Until we've set a
2903 * suitable controller mode we should not touch the
2904 * bus as we may be talking too fast.
2905 */
2906 dev->pio_mode = XFER_PIO_0;
Aaron Lu54169122012-12-03 11:35:02 +08002907 dev->dma_mode = 0xff;
Tejun Heocdeab112007-10-29 16:41:09 +09002908
2909 /* If the controller has a pio mode setup function
2910 * then use it to set the chipset to rights. Don't
2911 * touch the DMA setup as that will be dealt with when
2912 * configuring devices.
2913 */
2914 if (ap->ops->set_piomode)
2915 ap->ops->set_piomode(ap, dev);
2916 }
2917
Tejun Heo20444702006-03-13 01:57:01 +09002918 /* reset and determine device classes */
Tejun Heo52783c52006-05-31 18:28:22 +09002919 ap->ops->phy_reset(ap);
Tejun Heo2061a472006-03-12 00:57:39 +09002920
Tejun Heo1eca4362008-11-03 20:03:17 +09002921 ata_for_each_dev(dev, &ap->link, ALL) {
Tejun Heo3e4ec342010-05-10 21:41:30 +02002922 if (dev->class != ATA_DEV_UNKNOWN)
Tejun Heo52783c52006-05-31 18:28:22 +09002923 classes[dev->devno] = dev->class;
2924 else
2925 classes[dev->devno] = ATA_DEV_NONE;
Tejun Heo20444702006-03-13 01:57:01 +09002926
Tejun Heo52783c52006-05-31 18:28:22 +09002927 dev->class = ATA_DEV_UNKNOWN;
Tejun Heo28ca5c52006-03-01 16:09:36 +09002928 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929
Jeff Garzikf31f0cc2007-03-02 17:47:28 -05002930 /* read IDENTIFY page and configure devices. We have to do the identify
2931 specific sequence bass-ackwards so that PDIAG- is released by
2932 the slave device */
2933
Tejun Heo1eca4362008-11-03 20:03:17 +09002934 ata_for_each_dev(dev, &ap->link, ALL_REVERSE) {
Tejun Heof58229f2007-08-06 18:36:23 +09002935 if (tries[dev->devno])
2936 dev->class = classes[dev->devno];
Tejun Heo14d2bac2006-04-02 17:54:46 +09002937
Tejun Heoe1211e32006-04-01 01:38:18 +09002938 if (!ata_dev_enabled(dev))
Tejun Heoffeae412006-03-01 16:09:35 +09002939 continue;
2940
Tejun Heobff04642006-11-10 18:08:10 +09002941 rc = ata_dev_read_id(dev, &dev->class, ATA_READID_POSTRESET,
2942 dev->id);
Tejun Heo14d2bac2006-04-02 17:54:46 +09002943 if (rc)
2944 goto fail;
Jeff Garzikf31f0cc2007-03-02 17:47:28 -05002945 }
2946
Alan Coxbe0d18d2007-03-06 02:37:56 -08002947 /* Now ask for the cable type as PDIAG- should have been released */
2948 if (ap->ops->cable_detect)
2949 ap->cbl = ap->ops->cable_detect(ap);
2950
Tejun Heo1eca4362008-11-03 20:03:17 +09002951 /* We may have SATA bridge glue hiding here irrespective of
2952 * the reported cable types and sensed types. When SATA
2953 * drives indicate we have a bridge, we don't know which end
2954 * of the link the bridge is which is a problem.
2955 */
2956 ata_for_each_dev(dev, &ap->link, ENABLED)
Alan Cox614fe292007-08-22 23:22:45 +01002957 if (ata_id_is_sata(dev->id))
2958 ap->cbl = ATA_CBL_SATA;
Alan Cox614fe292007-08-22 23:22:45 +01002959
Jeff Garzikf31f0cc2007-03-02 17:47:28 -05002960 /* After the identify sequence we can now set up the devices. We do
2961 this in the normal order so that the user doesn't get confused */
2962
Tejun Heo1eca4362008-11-03 20:03:17 +09002963 ata_for_each_dev(dev, &ap->link, ENABLED) {
Tejun Heo9af5c9c2007-08-06 18:36:22 +09002964 ap->link.eh_context.i.flags |= ATA_EHI_PRINTINFO;
Tejun Heoefdaedc2006-11-01 18:38:52 +09002965 rc = ata_dev_configure(dev);
Tejun Heo9af5c9c2007-08-06 18:36:22 +09002966 ap->link.eh_context.i.flags &= ~ATA_EHI_PRINTINFO;
Tejun Heo14d2bac2006-04-02 17:54:46 +09002967 if (rc)
2968 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969 }
2970
Tejun Heoe82cbdb2006-04-01 01:38:18 +09002971 /* configure transfer mode */
Tejun Heo02607312007-08-06 18:36:23 +09002972 rc = ata_set_mode(&ap->link, &dev);
Tejun Heo4ae72a12007-02-02 16:22:30 +09002973 if (rc)
Tejun Heo51713d32006-04-11 22:26:29 +09002974 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975
Tejun Heo1eca4362008-11-03 20:03:17 +09002976 ata_for_each_dev(dev, &ap->link, ENABLED)
2977 return 0;
Alan Coxe35a9e02006-03-27 18:46:37 +01002978
Tejun Heo96072e62006-04-01 01:38:17 +09002979 return -ENODEV;
Tejun Heo14d2bac2006-04-02 17:54:46 +09002980
2981 fail:
Tejun Heo4ae72a12007-02-02 16:22:30 +09002982 tries[dev->devno]--;
2983
Tejun Heo14d2bac2006-04-02 17:54:46 +09002984 switch (rc) {
2985 case -EINVAL:
Tejun Heo4ae72a12007-02-02 16:22:30 +09002986 /* eeek, something went very wrong, give up */
Tejun Heo14d2bac2006-04-02 17:54:46 +09002987 tries[dev->devno] = 0;
2988 break;
Tejun Heo4ae72a12007-02-02 16:22:30 +09002989
2990 case -ENODEV:
2991 /* give it just one more chance */
2992 tries[dev->devno] = min(tries[dev->devno], 1);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05002993 fallthrough;
Tejun Heo14d2bac2006-04-02 17:54:46 +09002994 case -EIO:
Tejun Heo4ae72a12007-02-02 16:22:30 +09002995 if (tries[dev->devno] == 1) {
2996 /* This is the last chance, better to slow
2997 * down than lose it.
2998 */
Tejun Heoa07d4992009-01-29 20:31:33 +09002999 sata_down_spd_limit(&ap->link, 0);
Tejun Heo4ae72a12007-02-02 16:22:30 +09003000 ata_down_xfermask_limit(dev, ATA_DNXFER_PIO);
3001 }
Tejun Heo14d2bac2006-04-02 17:54:46 +09003002 }
3003
Tejun Heo4ae72a12007-02-02 16:22:30 +09003004 if (!tries[dev->devno])
Tejun Heo3373efd2006-05-15 20:57:53 +09003005 ata_dev_disable(dev);
Tejun Heoec573752006-04-11 22:26:29 +09003006
Tejun Heo14d2bac2006-04-02 17:54:46 +09003007 goto retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008}
3009
3010/**
Tejun Heo3be680b2005-12-19 22:35:02 +09003011 * sata_print_link_status - Print SATA link status
Tejun Heo936fd732007-08-06 18:36:23 +09003012 * @link: SATA link to printk link status about
Tejun Heo3be680b2005-12-19 22:35:02 +09003013 *
3014 * This function prints link speed and status of a SATA link.
3015 *
3016 * LOCKING:
3017 * None.
3018 */
Adrian Bunk6bdb4fc2008-04-21 11:51:11 +03003019static void sata_print_link_status(struct ata_link *link)
Tejun Heo3be680b2005-12-19 22:35:02 +09003020{
Tejun Heo6d5f9732006-04-03 00:09:41 +09003021 u32 sstatus, scontrol, tmp;
Tejun Heo3be680b2005-12-19 22:35:02 +09003022
Tejun Heo936fd732007-08-06 18:36:23 +09003023 if (sata_scr_read(link, SCR_STATUS, &sstatus))
Tejun Heo3be680b2005-12-19 22:35:02 +09003024 return;
Tejun Heo936fd732007-08-06 18:36:23 +09003025 sata_scr_read(link, SCR_CONTROL, &scontrol);
Tejun Heo3be680b2005-12-19 22:35:02 +09003026
Tejun Heob1c72912008-07-31 17:02:43 +09003027 if (ata_phys_link_online(link)) {
Tejun Heo3be680b2005-12-19 22:35:02 +09003028 tmp = (sstatus >> 4) & 0xf;
Joe Perchesa9a79df2011-04-15 15:51:59 -07003029 ata_link_info(link, "SATA link up %s (SStatus %X SControl %X)\n",
3030 sata_spd_string(tmp), sstatus, scontrol);
Tejun Heo3be680b2005-12-19 22:35:02 +09003031 } else {
Joe Perchesa9a79df2011-04-15 15:51:59 -07003032 ata_link_info(link, "SATA link down (SStatus %X SControl %X)\n",
3033 sstatus, scontrol);
Tejun Heo3be680b2005-12-19 22:35:02 +09003034 }
3035}
3036
3037/**
Alan Coxebdfca6e2006-03-23 15:38:34 +00003038 * ata_dev_pair - return other device on cable
Alan Coxebdfca6e2006-03-23 15:38:34 +00003039 * @adev: device
3040 *
3041 * Obtain the other device on the same cable, or if none is
3042 * present NULL is returned
3043 */
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05003044
Tejun Heo3373efd2006-05-15 20:57:53 +09003045struct ata_device *ata_dev_pair(struct ata_device *adev)
Alan Coxebdfca6e2006-03-23 15:38:34 +00003046{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09003047 struct ata_link *link = adev->link;
3048 struct ata_device *pair = &link->device[1 - adev->devno];
Tejun Heoe1211e32006-04-01 01:38:18 +09003049 if (!ata_dev_enabled(pair))
Alan Coxebdfca6e2006-03-23 15:38:34 +00003050 return NULL;
3051 return pair;
3052}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01003053EXPORT_SYMBOL_GPL(ata_dev_pair);
Alan Coxebdfca6e2006-03-23 15:38:34 +00003054
3055/**
Tejun Heo3c567b72006-05-15 20:57:23 +09003056 * sata_down_spd_limit - adjust SATA spd limit downward
Tejun Heo936fd732007-08-06 18:36:23 +09003057 * @link: Link to adjust SATA spd limit for
Tejun Heoa07d4992009-01-29 20:31:33 +09003058 * @spd_limit: Additional limit
Tejun Heo1c3fae42006-04-02 20:53:28 +09003059 *
Tejun Heo936fd732007-08-06 18:36:23 +09003060 * Adjust SATA spd limit of @link downward. Note that this
Tejun Heo1c3fae42006-04-02 20:53:28 +09003061 * function only adjusts the limit. The change must be applied
Tejun Heo3c567b72006-05-15 20:57:23 +09003062 * using sata_set_spd().
Tejun Heo1c3fae42006-04-02 20:53:28 +09003063 *
Tejun Heoa07d4992009-01-29 20:31:33 +09003064 * If @spd_limit is non-zero, the speed is limited to equal to or
3065 * lower than @spd_limit if such speed is supported. If
3066 * @spd_limit is slower than any supported speed, only the lowest
3067 * supported speed is allowed.
3068 *
Tejun Heo1c3fae42006-04-02 20:53:28 +09003069 * LOCKING:
3070 * Inherited from caller.
3071 *
3072 * RETURNS:
3073 * 0 on success, negative errno on failure
3074 */
Tejun Heoa07d4992009-01-29 20:31:33 +09003075int sata_down_spd_limit(struct ata_link *link, u32 spd_limit)
Tejun Heo1c3fae42006-04-02 20:53:28 +09003076{
Tejun Heo81952c52006-05-15 20:57:47 +09003077 u32 sstatus, spd, mask;
Tejun Heoa07d4992009-01-29 20:31:33 +09003078 int rc, bit;
Tejun Heo1c3fae42006-04-02 20:53:28 +09003079
Tejun Heo936fd732007-08-06 18:36:23 +09003080 if (!sata_scr_valid(link))
Tejun Heo008a7892007-07-16 14:29:40 +09003081 return -EOPNOTSUPP;
3082
3083 /* If SCR can be read, use it to determine the current SPD.
Tejun Heo936fd732007-08-06 18:36:23 +09003084 * If not, use cached value in link->sata_spd.
Tejun Heo008a7892007-07-16 14:29:40 +09003085 */
Tejun Heo936fd732007-08-06 18:36:23 +09003086 rc = sata_scr_read(link, SCR_STATUS, &sstatus);
Tejun Heo9913ff8a2009-01-29 20:31:31 +09003087 if (rc == 0 && ata_sstatus_online(sstatus))
Tejun Heo008a7892007-07-16 14:29:40 +09003088 spd = (sstatus >> 4) & 0xf;
3089 else
Tejun Heo936fd732007-08-06 18:36:23 +09003090 spd = link->sata_spd;
Tejun Heo1c3fae42006-04-02 20:53:28 +09003091
Tejun Heo936fd732007-08-06 18:36:23 +09003092 mask = link->sata_spd_limit;
Tejun Heo1c3fae42006-04-02 20:53:28 +09003093 if (mask <= 1)
3094 return -EINVAL;
Tejun Heo008a7892007-07-16 14:29:40 +09003095
3096 /* unconditionally mask off the highest bit */
Tejun Heoa07d4992009-01-29 20:31:33 +09003097 bit = fls(mask) - 1;
3098 mask &= ~(1 << bit);
Tejun Heo1c3fae42006-04-02 20:53:28 +09003099
David Milburn2dc0b462017-11-14 16:17:25 -06003100 /*
3101 * Mask off all speeds higher than or equal to the current one. At
3102 * this point, if current SPD is not available and we previously
3103 * recorded the link speed from SStatus, the driver has already
3104 * masked off the highest bit so mask should already be 1 or 0.
3105 * Otherwise, we should not force 1.5Gbps on a link where we have
3106 * not previously recorded speed from SStatus. Just return in this
3107 * case.
Tejun Heo008a7892007-07-16 14:29:40 +09003108 */
3109 if (spd > 1)
3110 mask &= (1 << (spd - 1)) - 1;
3111 else
David Milburn2dc0b462017-11-14 16:17:25 -06003112 return -EINVAL;
Tejun Heo008a7892007-07-16 14:29:40 +09003113
3114 /* were we already at the bottom? */
Tejun Heo1c3fae42006-04-02 20:53:28 +09003115 if (!mask)
3116 return -EINVAL;
3117
Tejun Heoa07d4992009-01-29 20:31:33 +09003118 if (spd_limit) {
3119 if (mask & ((1 << spd_limit) - 1))
3120 mask &= (1 << spd_limit) - 1;
3121 else {
3122 bit = ffs(mask) - 1;
3123 mask = 1 << bit;
3124 }
3125 }
3126
Tejun Heo936fd732007-08-06 18:36:23 +09003127 link->sata_spd_limit = mask;
Tejun Heo1c3fae42006-04-02 20:53:28 +09003128
Joe Perchesa9a79df2011-04-15 15:51:59 -07003129 ata_link_warn(link, "limiting SATA link speed to %s\n",
3130 sata_spd_string(fls(mask)));
Tejun Heo1c3fae42006-04-02 20:53:28 +09003131
3132 return 0;
3133}
3134
Bartlomiej Zolnierkiewicza9b2c122020-03-26 16:58:07 +01003135#ifdef CONFIG_ATA_ACPI
Tejun Heocf176e12006-04-02 17:54:46 +09003136/**
Tejun Heoa0f79b92007-12-18 16:33:05 +09003137 * ata_timing_cycle2mode - find xfer mode for the specified cycle duration
3138 * @xfer_shift: ATA_SHIFT_* value for transfer type to examine.
3139 * @cycle: cycle duration in ns
3140 *
3141 * Return matching xfer mode for @cycle. The returned mode is of
3142 * the transfer type specified by @xfer_shift. If @cycle is too
3143 * slow for @xfer_shift, 0xff is returned. If @cycle is faster
3144 * than the fastest known mode, the fasted mode is returned.
3145 *
3146 * LOCKING:
3147 * None.
3148 *
3149 * RETURNS:
3150 * Matching xfer_mode, 0xff if no match found.
3151 */
3152u8 ata_timing_cycle2mode(unsigned int xfer_shift, int cycle)
3153{
3154 u8 base_mode = 0xff, last_mode = 0xff;
3155 const struct ata_xfer_ent *ent;
3156 const struct ata_timing *t;
3157
3158 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
3159 if (ent->shift == xfer_shift)
3160 base_mode = ent->base;
3161
3162 for (t = ata_timing_find_mode(base_mode);
3163 t && ata_xfer_mode2shift(t->mode) == xfer_shift; t++) {
3164 unsigned short this_cycle;
3165
3166 switch (xfer_shift) {
3167 case ATA_SHIFT_PIO:
3168 case ATA_SHIFT_MWDMA:
3169 this_cycle = t->cycle;
3170 break;
3171 case ATA_SHIFT_UDMA:
3172 this_cycle = t->udma;
3173 break;
3174 default:
3175 return 0xff;
3176 }
3177
3178 if (cycle > this_cycle)
3179 break;
3180
3181 last_mode = t->mode;
3182 }
3183
3184 return last_mode;
3185}
Bartlomiej Zolnierkiewicza9b2c122020-03-26 16:58:07 +01003186#endif
Tejun Heoa0f79b92007-12-18 16:33:05 +09003187
3188/**
Tejun Heocf176e12006-04-02 17:54:46 +09003189 * ata_down_xfermask_limit - adjust dev xfer masks downward
Tejun Heocf176e12006-04-02 17:54:46 +09003190 * @dev: Device to adjust xfer masks
Tejun Heo458337d2007-02-02 16:22:30 +09003191 * @sel: ATA_DNXFER_* selector
Tejun Heocf176e12006-04-02 17:54:46 +09003192 *
3193 * Adjust xfer masks of @dev downward. Note that this function
3194 * does not apply the change. Invoking ata_set_mode() afterwards
3195 * will apply the limit.
3196 *
3197 * LOCKING:
3198 * Inherited from caller.
3199 *
3200 * RETURNS:
3201 * 0 on success, negative errno on failure
3202 */
Tejun Heo458337d2007-02-02 16:22:30 +09003203int ata_down_xfermask_limit(struct ata_device *dev, unsigned int sel)
Tejun Heocf176e12006-04-02 17:54:46 +09003204{
Tejun Heo458337d2007-02-02 16:22:30 +09003205 char buf[32];
Tejun Heo7dc951a2007-11-27 19:43:42 +09003206 unsigned long orig_mask, xfer_mask;
3207 unsigned long pio_mask, mwdma_mask, udma_mask;
Tejun Heo458337d2007-02-02 16:22:30 +09003208 int quiet, highbit;
Tejun Heocf176e12006-04-02 17:54:46 +09003209
Tejun Heo458337d2007-02-02 16:22:30 +09003210 quiet = !!(sel & ATA_DNXFER_QUIET);
3211 sel &= ~ATA_DNXFER_QUIET;
Tejun Heocf176e12006-04-02 17:54:46 +09003212
Tejun Heo458337d2007-02-02 16:22:30 +09003213 xfer_mask = orig_mask = ata_pack_xfermask(dev->pio_mask,
3214 dev->mwdma_mask,
3215 dev->udma_mask);
3216 ata_unpack_xfermask(xfer_mask, &pio_mask, &mwdma_mask, &udma_mask);
Tejun Heocf176e12006-04-02 17:54:46 +09003217
Tejun Heo458337d2007-02-02 16:22:30 +09003218 switch (sel) {
3219 case ATA_DNXFER_PIO:
3220 highbit = fls(pio_mask) - 1;
3221 pio_mask &= ~(1 << highbit);
3222 break;
3223
3224 case ATA_DNXFER_DMA:
3225 if (udma_mask) {
3226 highbit = fls(udma_mask) - 1;
3227 udma_mask &= ~(1 << highbit);
3228 if (!udma_mask)
3229 return -ENOENT;
3230 } else if (mwdma_mask) {
3231 highbit = fls(mwdma_mask) - 1;
3232 mwdma_mask &= ~(1 << highbit);
3233 if (!mwdma_mask)
3234 return -ENOENT;
3235 }
3236 break;
3237
3238 case ATA_DNXFER_40C:
3239 udma_mask &= ATA_UDMA_MASK_40C;
3240 break;
3241
3242 case ATA_DNXFER_FORCE_PIO0:
3243 pio_mask &= 1;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05003244 fallthrough;
Tejun Heo458337d2007-02-02 16:22:30 +09003245 case ATA_DNXFER_FORCE_PIO:
3246 mwdma_mask = 0;
3247 udma_mask = 0;
3248 break;
3249
Tejun Heo458337d2007-02-02 16:22:30 +09003250 default:
3251 BUG();
3252 }
3253
3254 xfer_mask &= ata_pack_xfermask(pio_mask, mwdma_mask, udma_mask);
3255
3256 if (!(xfer_mask & ATA_MASK_PIO) || xfer_mask == orig_mask)
3257 return -ENOENT;
3258
3259 if (!quiet) {
3260 if (xfer_mask & (ATA_MASK_MWDMA | ATA_MASK_UDMA))
3261 snprintf(buf, sizeof(buf), "%s:%s",
3262 ata_mode_string(xfer_mask),
3263 ata_mode_string(xfer_mask & ATA_MASK_PIO));
3264 else
3265 snprintf(buf, sizeof(buf), "%s",
3266 ata_mode_string(xfer_mask));
3267
Joe Perchesa9a79df2011-04-15 15:51:59 -07003268 ata_dev_warn(dev, "limiting speed to %s\n", buf);
Tejun Heo458337d2007-02-02 16:22:30 +09003269 }
Tejun Heocf176e12006-04-02 17:54:46 +09003270
3271 ata_unpack_xfermask(xfer_mask, &dev->pio_mask, &dev->mwdma_mask,
3272 &dev->udma_mask);
3273
Tejun Heocf176e12006-04-02 17:54:46 +09003274 return 0;
Tejun Heocf176e12006-04-02 17:54:46 +09003275}
3276
Tejun Heo3373efd2006-05-15 20:57:53 +09003277static int ata_dev_set_mode(struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278{
Tejun Heod0cb43b2009-07-09 09:27:50 +09003279 struct ata_port *ap = dev->link->ap;
Tejun Heo9af5c9c2007-08-06 18:36:22 +09003280 struct ata_eh_context *ehc = &dev->link->eh_context;
Tejun Heod0cb43b2009-07-09 09:27:50 +09003281 const bool nosetxfer = dev->horkage & ATA_HORKAGE_NOSETXFER;
Tejun Heo4055dee2008-02-07 10:34:08 +09003282 const char *dev_err_whine = "";
3283 int ign_dev_err = 0;
Tejun Heod0cb43b2009-07-09 09:27:50 +09003284 unsigned int err_mask = 0;
Tejun Heo83206a22006-03-24 15:25:31 +09003285 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003286
Tejun Heoe8384602006-04-02 18:51:53 +09003287 dev->flags &= ~ATA_DFLAG_PIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003288 if (dev->xfer_shift == ATA_SHIFT_PIO)
3289 dev->flags |= ATA_DFLAG_PIO;
3290
Tejun Heod0cb43b2009-07-09 09:27:50 +09003291 if (nosetxfer && ap->flags & ATA_FLAG_SATA && ata_id_is_sata(dev->id))
3292 dev_err_whine = " (SET_XFERMODE skipped)";
3293 else {
3294 if (nosetxfer)
Joe Perchesa9a79df2011-04-15 15:51:59 -07003295 ata_dev_warn(dev,
3296 "NOSETXFER but PATA detected - can't "
3297 "skip SETXFER, might malfunction\n");
Tejun Heod0cb43b2009-07-09 09:27:50 +09003298 err_mask = ata_dev_set_xfermode(dev);
3299 }
Jeff Garzik2dcb4072007-10-19 06:42:56 -04003300
Tejun Heo4055dee2008-02-07 10:34:08 +09003301 if (err_mask & ~AC_ERR_DEV)
3302 goto fail;
Jeff Garzik2dcb4072007-10-19 06:42:56 -04003303
Tejun Heo4055dee2008-02-07 10:34:08 +09003304 /* revalidate */
Tejun Heobaa1e782006-11-01 18:39:27 +09003305 ehc->i.flags |= ATA_EHI_POST_SETMODE;
Tejun Heo422c9da2007-09-23 13:14:12 +09003306 rc = ata_dev_revalidate(dev, ATA_DEV_UNKNOWN, 0);
Tejun Heobaa1e782006-11-01 18:39:27 +09003307 ehc->i.flags &= ~ATA_EHI_POST_SETMODE;
Tejun Heo5eb45c02006-04-02 18:51:52 +09003308 if (rc)
Tejun Heo83206a22006-03-24 15:25:31 +09003309 return rc;
Tejun Heo48a8a142006-03-05 17:55:58 +09003310
Alan Coxb93fda12008-04-08 16:36:44 +01003311 if (dev->xfer_shift == ATA_SHIFT_PIO) {
3312 /* Old CFA may refuse this command, which is just fine */
3313 if (ata_id_is_cfa(dev->id))
3314 ign_dev_err = 1;
3315 /* Catch several broken garbage emulations plus some pre
3316 ATA devices */
3317 if (ata_id_major_version(dev->id) == 0 &&
3318 dev->pio_mode <= XFER_PIO_2)
3319 ign_dev_err = 1;
3320 /* Some very old devices and some bad newer ones fail
3321 any kind of SET_XFERMODE request but support PIO0-2
3322 timings and no IORDY */
3323 if (!ata_id_has_iordy(dev->id) && dev->pio_mode <= XFER_PIO_2)
3324 ign_dev_err = 1;
3325 }
Tejun Heo4055dee2008-02-07 10:34:08 +09003326 /* Early MWDMA devices do DMA but don't allow DMA mode setting.
3327 Don't fail an MWDMA0 set IFF the device indicates it is in MWDMA0 */
3328 if (dev->xfer_shift == ATA_SHIFT_MWDMA &&
3329 dev->dma_mode == XFER_MW_DMA_0 &&
3330 (dev->id[63] >> 8) & 1)
3331 ign_dev_err = 1;
3332
3333 /* if the device is actually configured correctly, ignore dev err */
3334 if (dev->xfer_mode == ata_xfer_mask2mode(ata_id_xfermask(dev->id)))
3335 ign_dev_err = 1;
3336
3337 if (err_mask & AC_ERR_DEV) {
3338 if (!ign_dev_err)
3339 goto fail;
3340 else
3341 dev_err_whine = " (device error ignored)";
3342 }
3343
Tejun Heo23e71c32006-03-06 04:31:57 +09003344 DPRINTK("xfer_shift=%u, xfer_mode=0x%x\n",
3345 dev->xfer_shift, (int)dev->xfer_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346
Damien Le Moal07b9b6d2018-05-09 09:28:10 +09003347 if (!(ehc->i.flags & ATA_EHI_QUIET) ||
3348 ehc->i.flags & ATA_EHI_DID_HARDRESET)
3349 ata_dev_info(dev, "configured for %s%s\n",
3350 ata_mode_string(ata_xfer_mode2mask(dev->xfer_mode)),
3351 dev_err_whine);
Tejun Heo4055dee2008-02-07 10:34:08 +09003352
Tejun Heo83206a22006-03-24 15:25:31 +09003353 return 0;
Tejun Heo4055dee2008-02-07 10:34:08 +09003354
3355 fail:
Joe Perchesa9a79df2011-04-15 15:51:59 -07003356 ata_dev_err(dev, "failed to set xfermode (err_mask=0x%x)\n", err_mask);
Tejun Heo4055dee2008-02-07 10:34:08 +09003357 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358}
3359
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360/**
Alan04351822007-03-06 02:37:52 -08003361 * ata_do_set_mode - Program timings and issue SET FEATURES - XFER
Tejun Heo02607312007-08-06 18:36:23 +09003362 * @link: link on which timings will be programmed
Joe Perches1967b7f2008-02-03 17:08:11 +02003363 * @r_failed_dev: out parameter for failed device
Linus Torvalds1da177e2005-04-16 15:20:36 -07003364 *
Alan04351822007-03-06 02:37:52 -08003365 * Standard implementation of the function used to tune and set
3366 * ATA device disk transfer mode (PIO3, UDMA6, etc.). If
3367 * ata_dev_set_mode() fails, pointer to the failing device is
Tejun Heoe82cbdb2006-04-01 01:38:18 +09003368 * returned in @r_failed_dev.
Jeff Garzik780a87f2005-05-30 15:41:05 -04003369 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003371 * PCI/etc. bus probe sem.
Tejun Heoe82cbdb2006-04-01 01:38:18 +09003372 *
3373 * RETURNS:
3374 * 0 on success, negative errno otherwise
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375 */
Alan04351822007-03-06 02:37:52 -08003376
Tejun Heo02607312007-08-06 18:36:23 +09003377int ata_do_set_mode(struct ata_link *link, struct ata_device **r_failed_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003378{
Tejun Heo02607312007-08-06 18:36:23 +09003379 struct ata_port *ap = link->ap;
Tejun Heoe8e06192006-04-01 01:38:18 +09003380 struct ata_device *dev;
Tejun Heof58229f2007-08-06 18:36:23 +09003381 int rc = 0, used_dma = 0, found = 0;
Tejun Heo3adcebb2006-05-15 20:57:37 +09003382
Tejun Heoa6d5a512006-03-06 04:31:57 +09003383 /* step 1: calculate xfer_mask */
Tejun Heo1eca4362008-11-03 20:03:17 +09003384 ata_for_each_dev(dev, link, ENABLED) {
Tejun Heo7dc951a2007-11-27 19:43:42 +09003385 unsigned long pio_mask, dma_mask;
Alan Coxb3a70602007-10-02 12:38:26 -04003386 unsigned int mode_mask;
Tejun Heoa6d5a512006-03-06 04:31:57 +09003387
Alan Coxb3a70602007-10-02 12:38:26 -04003388 mode_mask = ATA_DMA_MASK_ATA;
3389 if (dev->class == ATA_DEV_ATAPI)
3390 mode_mask = ATA_DMA_MASK_ATAPI;
3391 else if (ata_id_is_cfa(dev->id))
3392 mode_mask = ATA_DMA_MASK_CFA;
3393
Tejun Heo3373efd2006-05-15 20:57:53 +09003394 ata_dev_xfermask(dev);
Tejun Heo33267322008-02-13 09:15:09 +09003395 ata_force_xfermask(dev);
Tejun Heoa6d5a512006-03-06 04:31:57 +09003396
Tejun Heoacf356b2006-03-24 14:07:50 +09003397 pio_mask = ata_pack_xfermask(dev->pio_mask, 0, 0);
Alan Coxb3a70602007-10-02 12:38:26 -04003398
3399 if (libata_dma_mask & mode_mask)
Sergei Shtylyov80a9c432011-12-22 22:41:32 +03003400 dma_mask = ata_pack_xfermask(0, dev->mwdma_mask,
3401 dev->udma_mask);
Alan Coxb3a70602007-10-02 12:38:26 -04003402 else
3403 dma_mask = 0;
3404
Tejun Heoacf356b2006-03-24 14:07:50 +09003405 dev->pio_mode = ata_xfer_mask2mode(pio_mask);
3406 dev->dma_mode = ata_xfer_mask2mode(dma_mask);
Alan Cox5444a6f2006-03-27 18:58:20 +01003407
Tejun Heo4f659772006-04-01 01:38:18 +09003408 found = 1;
Alan Coxb15b3eb2008-08-01 09:18:34 +01003409 if (ata_dma_enabled(dev))
Alan Cox5444a6f2006-03-27 18:58:20 +01003410 used_dma = 1;
Tejun Heoa6d5a512006-03-06 04:31:57 +09003411 }
Tejun Heo4f659772006-04-01 01:38:18 +09003412 if (!found)
Tejun Heoe82cbdb2006-04-01 01:38:18 +09003413 goto out;
Tejun Heoa6d5a512006-03-06 04:31:57 +09003414
3415 /* step 2: always set host PIO timings */
Tejun Heo1eca4362008-11-03 20:03:17 +09003416 ata_for_each_dev(dev, link, ENABLED) {
Tejun Heo70cd0712007-11-27 19:43:40 +09003417 if (dev->pio_mode == 0xff) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07003418 ata_dev_warn(dev, "no PIO support\n");
Tejun Heoe8e06192006-04-01 01:38:18 +09003419 rc = -EINVAL;
Tejun Heoe82cbdb2006-04-01 01:38:18 +09003420 goto out;
Tejun Heoe8e06192006-04-01 01:38:18 +09003421 }
3422
3423 dev->xfer_mode = dev->pio_mode;
3424 dev->xfer_shift = ATA_SHIFT_PIO;
3425 if (ap->ops->set_piomode)
3426 ap->ops->set_piomode(ap, dev);
3427 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003428
Tejun Heoa6d5a512006-03-06 04:31:57 +09003429 /* step 3: set host DMA timings */
Tejun Heo1eca4362008-11-03 20:03:17 +09003430 ata_for_each_dev(dev, link, ENABLED) {
3431 if (!ata_dma_enabled(dev))
Tejun Heoe8e06192006-04-01 01:38:18 +09003432 continue;
3433
3434 dev->xfer_mode = dev->dma_mode;
3435 dev->xfer_shift = ata_xfer_mode2shift(dev->dma_mode);
3436 if (ap->ops->set_dmamode)
3437 ap->ops->set_dmamode(ap, dev);
3438 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003439
3440 /* step 4: update devices' xfer mode */
Tejun Heo1eca4362008-11-03 20:03:17 +09003441 ata_for_each_dev(dev, link, ENABLED) {
Tejun Heo3373efd2006-05-15 20:57:53 +09003442 rc = ata_dev_set_mode(dev);
Tejun Heo5bbc53f2006-04-01 01:38:17 +09003443 if (rc)
Tejun Heoe82cbdb2006-04-01 01:38:18 +09003444 goto out;
Tejun Heo83206a22006-03-24 15:25:31 +09003445 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003446
Tejun Heoe8e06192006-04-01 01:38:18 +09003447 /* Record simplex status. If we selected DMA then the other
3448 * host channels are not permitted to do so.
Alan Cox5444a6f2006-03-27 18:58:20 +01003449 */
Jeff Garzikcca39742006-08-24 03:19:22 -04003450 if (used_dma && (ap->host->flags & ATA_HOST_SIMPLEX))
Alan032af1c2007-03-01 17:36:46 +00003451 ap->host->simplex_claimed = ap;
Alan Cox5444a6f2006-03-27 18:58:20 +01003452
Tejun Heoe82cbdb2006-04-01 01:38:18 +09003453 out:
3454 if (rc)
3455 *r_failed_dev = dev;
3456 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01003458EXPORT_SYMBOL_GPL(ata_do_set_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003459
3460/**
Tejun Heoaa2731a2008-04-07 22:47:19 +09003461 * ata_wait_ready - wait for link to become ready
3462 * @link: link to be waited on
3463 * @deadline: deadline jiffies for the operation
3464 * @check_ready: callback to check link readiness
3465 *
3466 * Wait for @link to become ready. @check_ready should return
3467 * positive number if @link is ready, 0 if it isn't, -ENODEV if
3468 * link doesn't seem to be occupied, other errno for other error
3469 * conditions.
3470 *
3471 * Transient -ENODEV conditions are allowed for
3472 * ATA_TMOUT_FF_WAIT.
3473 *
3474 * LOCKING:
3475 * EH context.
3476 *
3477 * RETURNS:
Masanari Iidac9b55602016-04-13 23:36:27 +09003478 * 0 if @link is ready before @deadline; otherwise, -errno.
Tejun Heoaa2731a2008-04-07 22:47:19 +09003479 */
3480int ata_wait_ready(struct ata_link *link, unsigned long deadline,
3481 int (*check_ready)(struct ata_link *link))
3482{
3483 unsigned long start = jiffies;
Tejun Heob48d58f2010-04-09 19:46:38 +09003484 unsigned long nodev_deadline;
Tejun Heoaa2731a2008-04-07 22:47:19 +09003485 int warned = 0;
3486
Tejun Heob48d58f2010-04-09 19:46:38 +09003487 /* choose which 0xff timeout to use, read comment in libata.h */
3488 if (link->ap->host->flags & ATA_HOST_PARALLEL_SCAN)
3489 nodev_deadline = ata_deadline(start, ATA_TMOUT_FF_WAIT_LONG);
3490 else
3491 nodev_deadline = ata_deadline(start, ATA_TMOUT_FF_WAIT);
3492
Tejun Heob1c72912008-07-31 17:02:43 +09003493 /* Slave readiness can't be tested separately from master. On
3494 * M/S emulation configuration, this function should be called
3495 * only on the master and it will handle both master and slave.
3496 */
3497 WARN_ON(link == link->ap->slave_link);
3498
Tejun Heoaa2731a2008-04-07 22:47:19 +09003499 if (time_after(nodev_deadline, deadline))
3500 nodev_deadline = deadline;
3501
3502 while (1) {
3503 unsigned long now = jiffies;
3504 int ready, tmp;
3505
3506 ready = tmp = check_ready(link);
3507 if (ready > 0)
3508 return 0;
3509
Tejun Heob48d58f2010-04-09 19:46:38 +09003510 /*
3511 * -ENODEV could be transient. Ignore -ENODEV if link
Tejun Heoaa2731a2008-04-07 22:47:19 +09003512 * is online. Also, some SATA devices take a long
Tejun Heob48d58f2010-04-09 19:46:38 +09003513 * time to clear 0xff after reset. Wait for
3514 * ATA_TMOUT_FF_WAIT[_LONG] on -ENODEV if link isn't
3515 * offline.
Tejun Heoaa2731a2008-04-07 22:47:19 +09003516 *
3517 * Note that some PATA controllers (pata_ali) explode
3518 * if status register is read more than once when
3519 * there's no device attached.
3520 */
3521 if (ready == -ENODEV) {
3522 if (ata_link_online(link))
3523 ready = 0;
3524 else if ((link->ap->flags & ATA_FLAG_SATA) &&
3525 !ata_link_offline(link) &&
3526 time_before(now, nodev_deadline))
3527 ready = 0;
3528 }
3529
3530 if (ready)
3531 return ready;
3532 if (time_after(now, deadline))
3533 return -EBUSY;
3534
3535 if (!warned && time_after(now, start + 5 * HZ) &&
3536 (deadline - now > 3 * HZ)) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07003537 ata_link_warn(link,
Tejun Heoaa2731a2008-04-07 22:47:19 +09003538 "link is slow to respond, please be patient "
3539 "(ready=%d)\n", tmp);
3540 warned = 1;
3541 }
3542
Tejun Heo97750ce2010-09-06 17:56:29 +02003543 ata_msleep(link->ap, 50);
Tejun Heoaa2731a2008-04-07 22:47:19 +09003544 }
3545}
3546
3547/**
3548 * ata_wait_after_reset - wait for link to become ready after reset
3549 * @link: link to be waited on
3550 * @deadline: deadline jiffies for the operation
3551 * @check_ready: callback to check link readiness
3552 *
3553 * Wait for @link to become ready after reset.
3554 *
3555 * LOCKING:
3556 * EH context.
3557 *
3558 * RETURNS:
Masanari Iidac9b55602016-04-13 23:36:27 +09003559 * 0 if @link is ready before @deadline; otherwise, -errno.
Tejun Heoaa2731a2008-04-07 22:47:19 +09003560 */
Harvey Harrison2b4221b2008-04-24 18:37:34 -07003561int ata_wait_after_reset(struct ata_link *link, unsigned long deadline,
Tejun Heoaa2731a2008-04-07 22:47:19 +09003562 int (*check_ready)(struct ata_link *link))
3563{
Tejun Heo97750ce2010-09-06 17:56:29 +02003564 ata_msleep(link->ap, ATA_WAIT_AFTER_RESET);
Tejun Heoaa2731a2008-04-07 22:47:19 +09003565
3566 return ata_wait_ready(link, deadline, check_ready);
3567}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01003568EXPORT_SYMBOL_GPL(ata_wait_after_reset);
Tejun Heoaa2731a2008-04-07 22:47:19 +09003569
3570/**
Tejun Heo0aa11132008-04-07 22:47:18 +09003571 * ata_std_prereset - prepare for reset
Tejun Heocc0680a2007-08-06 18:36:23 +09003572 * @link: ATA link to be reset
Tejun Heod4b2bab2007-02-02 16:50:52 +09003573 * @deadline: deadline jiffies for the operation
Tejun Heof5914a42006-05-31 18:27:48 +09003574 *
Tejun Heocc0680a2007-08-06 18:36:23 +09003575 * @link is about to be reset. Initialize it. Failure from
Tejun Heob8cffc62007-02-02 16:50:52 +09003576 * prereset makes libata abort whole reset sequence and give up
3577 * that port, so prereset should be best-effort. It does its
3578 * best to prepare for reset sequence but if things go wrong, it
3579 * should just whine, not fail.
Tejun Heof5914a42006-05-31 18:27:48 +09003580 *
3581 * LOCKING:
3582 * Kernel thread context (may sleep)
3583 *
3584 * RETURNS:
3585 * 0 on success, -errno otherwise.
3586 */
Tejun Heo0aa11132008-04-07 22:47:18 +09003587int ata_std_prereset(struct ata_link *link, unsigned long deadline)
Tejun Heof5914a42006-05-31 18:27:48 +09003588{
Tejun Heocc0680a2007-08-06 18:36:23 +09003589 struct ata_port *ap = link->ap;
Tejun Heo936fd732007-08-06 18:36:23 +09003590 struct ata_eh_context *ehc = &link->eh_context;
Tejun Heoe9c83912006-07-03 16:07:26 +09003591 const unsigned long *timing = sata_ehc_deb_timing(ehc);
Tejun Heof5914a42006-05-31 18:27:48 +09003592 int rc;
3593
Tejun Heof5914a42006-05-31 18:27:48 +09003594 /* if we're about to do hardreset, nothing more to do */
3595 if (ehc->i.action & ATA_EH_HARDRESET)
3596 return 0;
3597
Tejun Heo936fd732007-08-06 18:36:23 +09003598 /* if SATA, resume link */
Tejun Heoa16abc02007-05-21 18:33:47 +02003599 if (ap->flags & ATA_FLAG_SATA) {
Tejun Heo936fd732007-08-06 18:36:23 +09003600 rc = sata_link_resume(link, timing, deadline);
Tejun Heob8cffc62007-02-02 16:50:52 +09003601 /* whine about phy resume failure but proceed */
3602 if (rc && rc != -EOPNOTSUPP)
Joe Perchesa9a79df2011-04-15 15:51:59 -07003603 ata_link_warn(link,
3604 "failed to resume link for reset (errno=%d)\n",
3605 rc);
Tejun Heof5914a42006-05-31 18:27:48 +09003606 }
3607
Tejun Heo45db2f62008-04-08 01:46:56 +09003608 /* no point in trying softreset on offline link */
Tejun Heob1c72912008-07-31 17:02:43 +09003609 if (ata_phys_link_offline(link))
Tejun Heo45db2f62008-04-08 01:46:56 +09003610 ehc->i.action &= ~ATA_EH_SOFTRESET;
3611
Tejun Heof5914a42006-05-31 18:27:48 +09003612 return 0;
3613}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01003614EXPORT_SYMBOL_GPL(ata_std_prereset);
Tejun Heof5914a42006-05-31 18:27:48 +09003615
Tejun Heoc2bd5802006-01-24 17:05:22 +09003616/**
Tejun Heo57c9efd2008-04-07 22:47:19 +09003617 * sata_std_hardreset - COMRESET w/o waiting or classification
3618 * @link: link to reset
3619 * @class: resulting class of attached device
3620 * @deadline: deadline jiffies for the operation
3621 *
3622 * Standard SATA COMRESET w/o waiting or classification.
3623 *
3624 * LOCKING:
3625 * Kernel thread context (may sleep)
3626 *
3627 * RETURNS:
3628 * 0 if link offline, -EAGAIN if link online, -errno on errors.
3629 */
3630int sata_std_hardreset(struct ata_link *link, unsigned int *class,
3631 unsigned long deadline)
3632{
3633 const unsigned long *timing = sata_ehc_deb_timing(&link->eh_context);
3634 bool online;
3635 int rc;
3636
3637 /* do hardreset */
3638 rc = sata_link_hardreset(link, timing, deadline, &online, NULL);
Tejun Heo57c9efd2008-04-07 22:47:19 +09003639 return online ? -EAGAIN : rc;
3640}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01003641EXPORT_SYMBOL_GPL(sata_std_hardreset);
Tejun Heo57c9efd2008-04-07 22:47:19 +09003642
3643/**
Tejun Heo203c75b2008-04-07 22:47:18 +09003644 * ata_std_postreset - standard postreset callback
Tejun Heocc0680a2007-08-06 18:36:23 +09003645 * @link: the target ata_link
Tejun Heoc2bd5802006-01-24 17:05:22 +09003646 * @classes: classes of attached devices
3647 *
3648 * This function is invoked after a successful reset. Note that
3649 * the device might have been reset more than once using
3650 * different reset methods before postreset is invoked.
Tejun Heoc2bd5802006-01-24 17:05:22 +09003651 *
Tejun Heoc2bd5802006-01-24 17:05:22 +09003652 * LOCKING:
3653 * Kernel thread context (may sleep)
3654 */
Tejun Heo203c75b2008-04-07 22:47:18 +09003655void ata_std_postreset(struct ata_link *link, unsigned int *classes)
Tejun Heoc2bd5802006-01-24 17:05:22 +09003656{
Tejun Heof0465192008-05-19 01:15:08 +09003657 u32 serror;
3658
Tejun Heoc2bd5802006-01-24 17:05:22 +09003659 DPRINTK("ENTER\n");
3660
Tejun Heof0465192008-05-19 01:15:08 +09003661 /* reset complete, clear SError */
3662 if (!sata_scr_read(link, SCR_ERROR, &serror))
3663 sata_scr_write(link, SCR_ERROR, serror);
3664
Tejun Heoc2bd5802006-01-24 17:05:22 +09003665 /* print link status */
Tejun Heo936fd732007-08-06 18:36:23 +09003666 sata_print_link_status(link);
Tejun Heoc2bd5802006-01-24 17:05:22 +09003667
Tejun Heoc2bd5802006-01-24 17:05:22 +09003668 DPRINTK("EXIT\n");
3669}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01003670EXPORT_SYMBOL_GPL(ata_std_postreset);
Tejun Heoc2bd5802006-01-24 17:05:22 +09003671
Tejun Heoa62c0fc2006-01-24 17:05:22 +09003672/**
Tejun Heo623a3122006-03-05 17:55:58 +09003673 * ata_dev_same_device - Determine whether new ID matches configured device
Tejun Heo623a3122006-03-05 17:55:58 +09003674 * @dev: device to compare against
3675 * @new_class: class of the new device
3676 * @new_id: IDENTIFY page of the new device
3677 *
3678 * Compare @new_class and @new_id against @dev and determine
3679 * whether @dev is the device indicated by @new_class and
3680 * @new_id.
3681 *
3682 * LOCKING:
3683 * None.
3684 *
3685 * RETURNS:
3686 * 1 if @dev matches @new_class and @new_id, 0 otherwise.
3687 */
Tejun Heo3373efd2006-05-15 20:57:53 +09003688static int ata_dev_same_device(struct ata_device *dev, unsigned int new_class,
3689 const u16 *new_id)
Tejun Heo623a3122006-03-05 17:55:58 +09003690{
3691 const u16 *old_id = dev->id;
Tejun Heoa0cf7332007-01-02 20:18:49 +09003692 unsigned char model[2][ATA_ID_PROD_LEN + 1];
3693 unsigned char serial[2][ATA_ID_SERNO_LEN + 1];
Tejun Heo623a3122006-03-05 17:55:58 +09003694
3695 if (dev->class != new_class) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07003696 ata_dev_info(dev, "class mismatch %d != %d\n",
3697 dev->class, new_class);
Tejun Heo623a3122006-03-05 17:55:58 +09003698 return 0;
3699 }
3700
Tejun Heoa0cf7332007-01-02 20:18:49 +09003701 ata_id_c_string(old_id, model[0], ATA_ID_PROD, sizeof(model[0]));
3702 ata_id_c_string(new_id, model[1], ATA_ID_PROD, sizeof(model[1]));
3703 ata_id_c_string(old_id, serial[0], ATA_ID_SERNO, sizeof(serial[0]));
3704 ata_id_c_string(new_id, serial[1], ATA_ID_SERNO, sizeof(serial[1]));
Tejun Heo623a3122006-03-05 17:55:58 +09003705
3706 if (strcmp(model[0], model[1])) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07003707 ata_dev_info(dev, "model number mismatch '%s' != '%s'\n",
3708 model[0], model[1]);
Tejun Heo623a3122006-03-05 17:55:58 +09003709 return 0;
3710 }
3711
3712 if (strcmp(serial[0], serial[1])) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07003713 ata_dev_info(dev, "serial number mismatch '%s' != '%s'\n",
3714 serial[0], serial[1]);
Tejun Heo623a3122006-03-05 17:55:58 +09003715 return 0;
3716 }
3717
Tejun Heo623a3122006-03-05 17:55:58 +09003718 return 1;
3719}
3720
3721/**
Tejun Heofe309112007-05-15 03:28:15 +09003722 * ata_dev_reread_id - Re-read IDENTIFY data
Henrik Kretzschmar3fae4502007-06-19 10:10:50 +02003723 * @dev: target ATA device
Tejun Heobff04642006-11-10 18:08:10 +09003724 * @readid_flags: read ID flags
Tejun Heo623a3122006-03-05 17:55:58 +09003725 *
3726 * Re-read IDENTIFY page and make sure @dev is still attached to
3727 * the port.
3728 *
3729 * LOCKING:
3730 * Kernel thread context (may sleep)
3731 *
3732 * RETURNS:
3733 * 0 on success, negative errno otherwise
3734 */
Tejun Heofe309112007-05-15 03:28:15 +09003735int ata_dev_reread_id(struct ata_device *dev, unsigned int readid_flags)
Tejun Heo623a3122006-03-05 17:55:58 +09003736{
Tejun Heo5eb45c02006-04-02 18:51:52 +09003737 unsigned int class = dev->class;
Tejun Heo9af5c9c2007-08-06 18:36:22 +09003738 u16 *id = (void *)dev->link->ap->sector_buf;
Tejun Heo623a3122006-03-05 17:55:58 +09003739 int rc;
3740
Tejun Heofe635c72006-05-15 20:57:35 +09003741 /* read ID data */
Tejun Heobff04642006-11-10 18:08:10 +09003742 rc = ata_dev_read_id(dev, &class, readid_flags, id);
Tejun Heo623a3122006-03-05 17:55:58 +09003743 if (rc)
Tejun Heofe309112007-05-15 03:28:15 +09003744 return rc;
Tejun Heo623a3122006-03-05 17:55:58 +09003745
3746 /* is the device still there? */
Tejun Heofe309112007-05-15 03:28:15 +09003747 if (!ata_dev_same_device(dev, class, id))
3748 return -ENODEV;
Tejun Heo623a3122006-03-05 17:55:58 +09003749
Tejun Heofe635c72006-05-15 20:57:35 +09003750 memcpy(dev->id, id, sizeof(id[0]) * ATA_ID_WORDS);
Tejun Heofe309112007-05-15 03:28:15 +09003751 return 0;
3752}
3753
3754/**
3755 * ata_dev_revalidate - Revalidate ATA device
3756 * @dev: device to revalidate
Tejun Heo422c9da2007-09-23 13:14:12 +09003757 * @new_class: new class code
Tejun Heofe309112007-05-15 03:28:15 +09003758 * @readid_flags: read ID flags
3759 *
3760 * Re-read IDENTIFY page, make sure @dev is still attached to the
3761 * port and reconfigure it according to the new IDENTIFY page.
3762 *
3763 * LOCKING:
3764 * Kernel thread context (may sleep)
3765 *
3766 * RETURNS:
3767 * 0 on success, negative errno otherwise
3768 */
Tejun Heo422c9da2007-09-23 13:14:12 +09003769int ata_dev_revalidate(struct ata_device *dev, unsigned int new_class,
3770 unsigned int readid_flags)
Tejun Heofe309112007-05-15 03:28:15 +09003771{
Tejun Heo6ddcd3b2007-05-15 03:28:15 +09003772 u64 n_sectors = dev->n_sectors;
Tejun Heo5920dad2009-07-15 17:11:41 +09003773 u64 n_native_sectors = dev->n_native_sectors;
Tejun Heofe309112007-05-15 03:28:15 +09003774 int rc;
3775
3776 if (!ata_dev_enabled(dev))
3777 return -ENODEV;
3778
Tejun Heo422c9da2007-09-23 13:14:12 +09003779 /* fail early if !ATA && !ATAPI to avoid issuing [P]IDENTIFY to PMP */
3780 if (ata_class_enabled(new_class) &&
Borislav Petkovf0d06132009-04-26 16:33:34 +02003781 new_class != ATA_DEV_ATA &&
3782 new_class != ATA_DEV_ATAPI &&
Hannes Reinecke9162c652014-11-05 13:08:21 +01003783 new_class != ATA_DEV_ZAC &&
Borislav Petkovf0d06132009-04-26 16:33:34 +02003784 new_class != ATA_DEV_SEMB) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07003785 ata_dev_info(dev, "class mismatch %u != %u\n",
3786 dev->class, new_class);
Tejun Heo422c9da2007-09-23 13:14:12 +09003787 rc = -ENODEV;
3788 goto fail;
3789 }
3790
Tejun Heofe309112007-05-15 03:28:15 +09003791 /* re-read ID */
3792 rc = ata_dev_reread_id(dev, readid_flags);
3793 if (rc)
3794 goto fail;
Tejun Heo623a3122006-03-05 17:55:58 +09003795
3796 /* configure device according to the new ID */
Tejun Heoefdaedc2006-11-01 18:38:52 +09003797 rc = ata_dev_configure(dev);
Tejun Heo6ddcd3b2007-05-15 03:28:15 +09003798 if (rc)
3799 goto fail;
3800
3801 /* verify n_sectors hasn't changed */
Tejun Heo445d2112010-04-05 10:33:13 +09003802 if (dev->class != ATA_DEV_ATA || !n_sectors ||
3803 dev->n_sectors == n_sectors)
3804 return 0;
3805
3806 /* n_sectors has changed */
Joe Perchesa9a79df2011-04-15 15:51:59 -07003807 ata_dev_warn(dev, "n_sectors mismatch %llu != %llu\n",
3808 (unsigned long long)n_sectors,
3809 (unsigned long long)dev->n_sectors);
Tejun Heo445d2112010-04-05 10:33:13 +09003810
3811 /*
3812 * Something could have caused HPA to be unlocked
3813 * involuntarily. If n_native_sectors hasn't changed and the
3814 * new size matches it, keep the device.
3815 */
3816 if (dev->n_native_sectors == n_native_sectors &&
3817 dev->n_sectors > n_sectors && dev->n_sectors == n_native_sectors) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07003818 ata_dev_warn(dev,
3819 "new n_sectors matches native, probably "
3820 "late HPA unlock, n_sectors updated\n");
Tejun Heo68939ce2010-05-15 20:09:33 +02003821 /* use the larger n_sectors */
Tejun Heo445d2112010-04-05 10:33:13 +09003822 return 0;
Tejun Heo6ddcd3b2007-05-15 03:28:15 +09003823 }
3824
Tejun Heo445d2112010-04-05 10:33:13 +09003825 /*
3826 * Some BIOSes boot w/o HPA but resume w/ HPA locked. Try
3827 * unlocking HPA in those cases.
3828 *
3829 * https://bugzilla.kernel.org/show_bug.cgi?id=15396
3830 */
3831 if (dev->n_native_sectors == n_native_sectors &&
3832 dev->n_sectors < n_sectors && n_sectors == n_native_sectors &&
3833 !(dev->horkage & ATA_HORKAGE_BROKEN_HPA)) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07003834 ata_dev_warn(dev,
3835 "old n_sectors matches native, probably "
3836 "late HPA lock, will try to unlock HPA\n");
Tejun Heo445d2112010-04-05 10:33:13 +09003837 /* try unlocking HPA */
3838 dev->flags |= ATA_DFLAG_UNLOCK_HPA;
3839 rc = -EIO;
3840 } else
3841 rc = -ENODEV;
Tejun Heo623a3122006-03-05 17:55:58 +09003842
Tejun Heo445d2112010-04-05 10:33:13 +09003843 /* restore original n_[native_]sectors and fail */
3844 dev->n_native_sectors = n_native_sectors;
3845 dev->n_sectors = n_sectors;
Tejun Heo623a3122006-03-05 17:55:58 +09003846 fail:
Joe Perchesa9a79df2011-04-15 15:51:59 -07003847 ata_dev_err(dev, "revalidation failed (errno=%d)\n", rc);
Tejun Heo623a3122006-03-05 17:55:58 +09003848 return rc;
3849}
3850
Alan Cox6919a0a2006-10-27 19:08:46 -07003851struct ata_blacklist_entry {
3852 const char *model_num;
3853 const char *model_rev;
3854 unsigned long horkage;
3855};
3856
3857static const struct ata_blacklist_entry ata_device_blacklist [] = {
3858 /* Devices with DMA related problems under Linux */
3859 { "WDC AC11000H", NULL, ATA_HORKAGE_NODMA },
3860 { "WDC AC22100H", NULL, ATA_HORKAGE_NODMA },
3861 { "WDC AC32500H", NULL, ATA_HORKAGE_NODMA },
3862 { "WDC AC33100H", NULL, ATA_HORKAGE_NODMA },
3863 { "WDC AC31600H", NULL, ATA_HORKAGE_NODMA },
3864 { "WDC AC32100H", "24.09P07", ATA_HORKAGE_NODMA },
3865 { "WDC AC23200L", "21.10N21", ATA_HORKAGE_NODMA },
3866 { "Compaq CRD-8241B", NULL, ATA_HORKAGE_NODMA },
3867 { "CRD-8400B", NULL, ATA_HORKAGE_NODMA },
Mark Lord7da4c932010-07-01 18:18:12 -04003868 { "CRD-848[02]B", NULL, ATA_HORKAGE_NODMA },
Alan Cox6919a0a2006-10-27 19:08:46 -07003869 { "CRD-84", NULL, ATA_HORKAGE_NODMA },
3870 { "SanDisk SDP3B", NULL, ATA_HORKAGE_NODMA },
3871 { "SanDisk SDP3B-64", NULL, ATA_HORKAGE_NODMA },
3872 { "SANYO CD-ROM CRD", NULL, ATA_HORKAGE_NODMA },
3873 { "HITACHI CDR-8", NULL, ATA_HORKAGE_NODMA },
Mark Lord7da4c932010-07-01 18:18:12 -04003874 { "HITACHI CDR-8[34]35",NULL, ATA_HORKAGE_NODMA },
Alan Cox6919a0a2006-10-27 19:08:46 -07003875 { "Toshiba CD-ROM XM-6202B", NULL, ATA_HORKAGE_NODMA },
3876 { "TOSHIBA CD-ROM XM-1702BC", NULL, ATA_HORKAGE_NODMA },
3877 { "CD-532E-A", NULL, ATA_HORKAGE_NODMA },
3878 { "E-IDE CD-ROM CR-840",NULL, ATA_HORKAGE_NODMA },
3879 { "CD-ROM Drive/F5A", NULL, ATA_HORKAGE_NODMA },
3880 { "WPI CDD-820", NULL, ATA_HORKAGE_NODMA },
3881 { "SAMSUNG CD-ROM SC-148C", NULL, ATA_HORKAGE_NODMA },
3882 { "SAMSUNG CD-ROM SC", NULL, ATA_HORKAGE_NODMA },
Alan Cox6919a0a2006-10-27 19:08:46 -07003883 { "ATAPI CD-ROM DRIVE 40X MAXIMUM",NULL,ATA_HORKAGE_NODMA },
3884 { "_NEC DV5800A", NULL, ATA_HORKAGE_NODMA },
Jeff Garzik2dcb4072007-10-19 06:42:56 -04003885 { "SAMSUNG CD-ROM SN-124", "N001", ATA_HORKAGE_NODMA },
Dave Jones39f19882007-05-21 14:31:03 -04003886 { "Seagate STT20000A", NULL, ATA_HORKAGE_NODMA },
Prarit Bhargavad17d7942012-08-23 15:11:52 -04003887 { " 2GB ATA Flash Disk", "ADMA428M", ATA_HORKAGE_NODMA },
Shikha Jainb00622f2016-01-11 14:55:45 -08003888 { "VRFDFC22048UCHC-TE*", NULL, ATA_HORKAGE_NODMA },
Tejun Heo3af9a772007-09-23 13:19:54 +09003889 /* Odd clown on sil3726/4726 PMPs */
Tejun Heo50af2fa2008-05-19 01:15:14 +09003890 { "Config Disk", NULL, ATA_HORKAGE_DISABLE },
Hannes Reineckea66307d2021-12-08 07:58:53 +01003891 /* Similar story with ASMedia 1092 */
3892 { "ASMT109x- Config", NULL, ATA_HORKAGE_DISABLE },
Alan Cox6919a0a2006-10-27 19:08:46 -07003893
Albert Lee18d6e9d2007-04-02 11:34:15 +08003894 /* Weird ATAPI devices */
Tejun Heo40a1d532007-06-27 02:49:38 +09003895 { "TORiSAN DVD-ROM DRD-N216", NULL, ATA_HORKAGE_MAX_SEC_128 },
Tejun Heo6a87e422008-11-03 19:01:09 +09003896 { "QUANTUM DAT DAT72-000", NULL, ATA_HORKAGE_ATAPI_MOD16_DMA },
Shan Haia32450e2013-03-18 10:30:44 +08003897 { "Slimtype DVD A DS8A8SH", NULL, ATA_HORKAGE_MAX_SEC_LBA48 },
Shan Hai0523f032013-10-28 16:08:01 +08003898 { "Slimtype DVD A DS8A9SH", NULL, ATA_HORKAGE_MAX_SEC_LBA48 },
Albert Lee18d6e9d2007-04-02 11:34:15 +08003899
David Milburnaf34d632015-07-13 11:48:23 -05003900 /*
3901 * Causes silent data corruption with higher max sects.
3902 * http://lkml.kernel.org/g/x49wpy40ysk.fsf@segfault.boston.devel.redhat.com
3903 */
3904 { "ST380013AS", "3.20", ATA_HORKAGE_MAX_SEC_1024 },
3905
Tejun Heo1488a1e2016-07-18 18:40:00 -04003906 /*
Tejun Heoe0edc8c2017-01-06 11:48:50 -05003907 * These devices time out with higher max sects.
Tejun Heo1488a1e2016-07-18 18:40:00 -04003908 * https://bugzilla.kernel.org/show_bug.cgi?id=121671
3909 */
Tejun Heoe0edc8c2017-01-06 11:48:50 -05003910 { "LITEON CX1-JB*-HP", NULL, ATA_HORKAGE_MAX_SEC_1024 },
Xinyu Lindb5ff902017-12-17 20:13:39 +08003911 { "LITEON EP1-*", NULL, ATA_HORKAGE_MAX_SEC_1024 },
Tejun Heo1488a1e2016-07-18 18:40:00 -04003912
Alan Cox6919a0a2006-10-27 19:08:46 -07003913 /* Devices we expect to fail diagnostics */
3914
3915 /* Devices where NCQ should be avoided */
3916 /* NCQ is slow */
Jeff Garzik2dcb4072007-10-19 06:42:56 -04003917 { "WDC WD740ADFD-00", NULL, ATA_HORKAGE_NONCQ },
Tejun Heo459ad682007-12-07 12:46:23 +09003918 { "WDC WD740ADFD-00NLR1", NULL, ATA_HORKAGE_NONCQ, },
Tejun Heo09125ea2007-02-28 15:21:23 +09003919 /* http://thread.gmane.org/gmane.linux.ide/14907 */
3920 { "FUJITSU MHT2060BH", NULL, ATA_HORKAGE_NONCQ },
Paul Rolland7acfaf32007-03-26 21:43:44 -08003921 /* NCQ is broken */
Jeff Garzik539cc7c2007-09-20 16:31:47 -04003922 { "Maxtor *", "BANC*", ATA_HORKAGE_NONCQ },
Alan Cox0e3dbc02007-09-20 15:22:47 +01003923 { "Maxtor 7V300F0", "VA111630", ATA_HORKAGE_NONCQ },
Paolo Ornatida6f0ec2007-10-04 11:06:56 +09003924 { "ST380817AS", "3.42", ATA_HORKAGE_NONCQ },
Tejun Heoe41bd3e2007-12-09 19:45:39 +09003925 { "ST3160023AS", "3.42", ATA_HORKAGE_NONCQ },
Lubomir Bulej5ccfca92008-12-22 11:35:22 +01003926 { "OCZ CORE_SSD", "02.10104", ATA_HORKAGE_NONCQ },
Jeff Garzik539cc7c2007-09-20 16:31:47 -04003927
Tejun Heoac70a962008-11-27 13:36:48 +09003928 /* Seagate NCQ + FLUSH CACHE firmware bug */
Mark Lord4d1f9082010-07-05 18:53:37 -04003929 { "ST31500341AS", "SD1[5-9]", ATA_HORKAGE_NONCQ |
Tejun Heoac70a962008-11-27 13:36:48 +09003930 ATA_HORKAGE_FIRMWARE_WARN },
Tejun Heod10d4912008-12-11 13:42:42 +09003931
Mark Lord4d1f9082010-07-05 18:53:37 -04003932 { "ST31000333AS", "SD1[5-9]", ATA_HORKAGE_NONCQ |
Tejun Heod10d4912008-12-11 13:42:42 +09003933 ATA_HORKAGE_FIRMWARE_WARN },
3934
Mark Lord4d1f9082010-07-05 18:53:37 -04003935 { "ST3640[36]23AS", "SD1[5-9]", ATA_HORKAGE_NONCQ |
Tejun Heod10d4912008-12-11 13:42:42 +09003936 ATA_HORKAGE_FIRMWARE_WARN },
3937
Mark Lord4d1f9082010-07-05 18:53:37 -04003938 { "ST3320[68]13AS", "SD1[5-9]", ATA_HORKAGE_NONCQ |
Tejun Heoac70a962008-11-27 13:36:48 +09003939 ATA_HORKAGE_FIRMWARE_WARN },
3940
Hans de Goede31f62642019-06-11 16:32:59 +02003941 /* drives which fail FPDMA_AA activation (some may freeze afterwards)
3942 the ST disks also have LPM issues */
Hans de Goede8756a252019-07-01 18:58:13 +02003943 { "ST1000LM024 HN-M101MBB", NULL, ATA_HORKAGE_BROKEN_FPDMA_AA |
Hans de Goede31f62642019-06-11 16:32:59 +02003944 ATA_HORKAGE_NOLPM, },
Aleksei Mamlin08c85d22015-07-01 13:48:30 +03003945 { "VB0250EAVER", "HPG7", ATA_HORKAGE_BROKEN_FPDMA_AA },
Michele Baldessari87809942013-11-25 19:00:14 +00003946
Robert Hancock36e337d2007-04-02 22:05:29 -06003947 /* Blacklist entries taken from Silicon Image 3124/3132
3948 Windows driver .inf file - also several Linux problem reports */
3949 { "HTS541060G9SA00", "MB3OC60D", ATA_HORKAGE_NONCQ, },
3950 { "HTS541080G9SA00", "MB4OC60D", ATA_HORKAGE_NONCQ, },
3951 { "HTS541010G9SA00", "MBZOC60D", ATA_HORKAGE_NONCQ, },
Alan Cox6919a0a2006-10-27 19:08:46 -07003952
Tejun Heo68b0ddb2010-04-05 10:51:26 +09003953 /* https://bugzilla.kernel.org/show_bug.cgi?id=15573 */
3954 { "C300-CTFDDAC128MAG", "0001", ATA_HORKAGE_NONCQ, },
3955
Tejun Heo3b545562020-09-02 12:32:45 -04003956 /* Sandisk SD7/8/9s lock up hard on large trims */
3957 { "SanDisk SD[789]*", NULL, ATA_HORKAGE_MAX_TRIM_128M, },
Tejun Heo322579d2018-05-08 14:21:56 -07003958
Tejun Heo16c55b02007-08-29 11:58:33 +09003959 /* devices which puke on READ_NATIVE_MAX */
3960 { "HDS724040KLSA80", "KFAOA20N", ATA_HORKAGE_BROKEN_HPA, },
3961 { "WDC WD3200JD-00KLB0", "WD-WCAMR1130137", ATA_HORKAGE_BROKEN_HPA },
3962 { "WDC WD2500JD-00HBB0", "WD-WMAL71490727", ATA_HORKAGE_BROKEN_HPA },
3963 { "MAXTOR 6L080L4", "A93.0500", ATA_HORKAGE_BROKEN_HPA },
Alan Cox6919a0a2006-10-27 19:08:46 -07003964
Tejun Heo7831387b2009-08-07 01:59:15 +09003965 /* this one allows HPA unlocking but fails IOs on the area */
3966 { "OCZ-VERTEX", "1.30", ATA_HORKAGE_BROKEN_HPA },
3967
Alan Cox93328e112007-09-29 04:06:48 -04003968 /* Devices which report 1 sector over size HPA */
3969 { "ST340823A", NULL, ATA_HORKAGE_HPA_SIZE, },
3970 { "ST320413A", NULL, ATA_HORKAGE_HPA_SIZE, },
Mikko Rapelib152fcd2008-02-19 01:41:25 +01003971 { "ST310211A", NULL, ATA_HORKAGE_HPA_SIZE, },
Alan Cox93328e112007-09-29 04:06:48 -04003972
Alan Cox6bbfd532007-11-05 22:58:58 +00003973 /* Devices which get the IVB wrong */
3974 { "QUANTUM FIREBALLlct10 05", "A03.0900", ATA_HORKAGE_IVB, },
Alan Coxa79067e2008-04-29 14:08:36 +01003975 /* Maybe we should just blacklist TSSTcorp... */
Mark Lord7da4c932010-07-01 18:18:12 -04003976 { "TSSTcorp CDDVDW SH-S202[HJN]", "SB0[01]", ATA_HORKAGE_IVB, },
Alan Cox6bbfd532007-11-05 22:58:58 +00003977
Jens Axboe9ce8e302008-08-27 15:23:18 +02003978 /* Devices that do not need bridging limits applied */
3979 { "MTRON MSP-SATA*", NULL, ATA_HORKAGE_BRIDGE_OK, },
Jeff Garzik04d0f1b82012-08-17 13:36:59 -04003980 { "BUFFALO HD-QSU2/R5", NULL, ATA_HORKAGE_BRIDGE_OK, },
Jens Axboe9ce8e302008-08-27 15:23:18 +02003981
Tejun Heo90627122009-01-29 20:31:36 +09003982 /* Devices which aren't very happy with higher link speeds */
3983 { "WD My Book", NULL, ATA_HORKAGE_1_5_GBPS, },
Daniel J Bluemanc5310772012-07-23 12:22:37 +08003984 { "Seagate FreeAgent GoFlex", NULL, ATA_HORKAGE_1_5_GBPS, },
Tejun Heo90627122009-01-29 20:31:36 +09003985
Tejun Heod0cb43b2009-07-09 09:27:50 +09003986 /*
3987 * Devices which choke on SETXFER. Applies only if both the
3988 * device and controller are SATA.
3989 */
Tejun Heocd691872011-06-22 12:13:10 +02003990 { "PIONEER DVD-RW DVRTD08", NULL, ATA_HORKAGE_NOSETXFER },
Vladimir LAVALLADE3a251792012-01-08 13:50:13 +01003991 { "PIONEER DVD-RW DVRTD08A", NULL, ATA_HORKAGE_NOSETXFER },
3992 { "PIONEER DVD-RW DVR-215", NULL, ATA_HORKAGE_NOSETXFER },
Tejun Heocd691872011-06-22 12:13:10 +02003993 { "PIONEER DVD-RW DVR-212D", NULL, ATA_HORKAGE_NOSETXFER },
3994 { "PIONEER DVD-RW DVR-216D", NULL, ATA_HORKAGE_NOSETXFER },
Tejun Heod0cb43b2009-07-09 09:27:50 +09003995
Kai-Heng Fengb17e57292018-02-18 22:17:09 +08003996 /* Crucial BX100 SSD 500GB has broken LPM support */
Hans de Goede3bf7b5d2018-03-19 16:33:59 +01003997 { "CT500BX100SSD1", NULL, ATA_HORKAGE_NOLPM },
Kai-Heng Fengb17e57292018-02-18 22:17:09 +08003998
Hans de Goeded418ff52018-03-19 16:34:00 +01003999 /* 512GB MX100 with MU01 firmware has both queued TRIM and LPM issues */
4000 { "Crucial_CT512MX100*", "MU01", ATA_HORKAGE_NO_NCQ_TRIM |
Hans de Goede9c7be59f2018-02-16 10:48:20 +01004001 ATA_HORKAGE_ZERO_AFTER_TRIM |
4002 ATA_HORKAGE_NOLPM, },
Hans de Goeded418ff52018-03-19 16:34:00 +01004003 /* 512GB MX100 with newer firmware has only LPM issues */
4004 { "Crucial_CT512MX100*", NULL, ATA_HORKAGE_ZERO_AFTER_TRIM |
4005 ATA_HORKAGE_NOLPM, },
Hans de Goede9c7be59f2018-02-16 10:48:20 +01004006
Hans de Goede62ac3f72018-03-19 16:33:58 +01004007 /* 480GB+ M500 SSDs have both queued TRIM and LPM issues */
4008 { "Crucial_CT480M500*", NULL, ATA_HORKAGE_NO_NCQ_TRIM |
4009 ATA_HORKAGE_ZERO_AFTER_TRIM |
4010 ATA_HORKAGE_NOLPM, },
4011 { "Crucial_CT960M500*", NULL, ATA_HORKAGE_NO_NCQ_TRIM |
4012 ATA_HORKAGE_ZERO_AFTER_TRIM |
4013 ATA_HORKAGE_NOLPM, },
4014
François Cami76936e92018-05-13 20:11:15 +02004015 /* These specific Samsung models/firmware-revs do not handle LPM well */
Hans de Goedeb5b4d3a2018-04-24 11:19:07 +02004016 { "SAMSUNG MZMPC128HBFU-000MV", "CXM14M1Q", ATA_HORKAGE_NOLPM, },
François Cami76936e92018-05-13 20:11:15 +02004017 { "SAMSUNG SSD PM830 mSATA *", "CXM13D1Q", ATA_HORKAGE_NOLPM, },
Diego Viola410b5c72018-11-12 17:22:52 -02004018 { "SAMSUNG MZ7TD256HAFV-000L9", NULL, ATA_HORKAGE_NOLPM, },
Hans de Goededd957492019-02-03 10:02:07 +01004019 { "SAMSUNG MZ7TE512HMHP-000L1", "EXT06L0Q", ATA_HORKAGE_NOLPM, },
Hans de Goedeb5b4d3a2018-04-24 11:19:07 +02004020
Marc Carinof78dea02013-12-16 18:15:53 -08004021 /* devices that don't properly handle queued TRIM commands */
Sudip Mukherjee136d7692018-05-19 22:29:36 +01004022 { "Micron_M500IT_*", "MU01", ATA_HORKAGE_NO_NCQ_TRIM |
4023 ATA_HORKAGE_ZERO_AFTER_TRIM, },
Martin K. Petersen243918b2015-06-18 14:50:18 -04004024 { "Micron_M500_*", NULL, ATA_HORKAGE_NO_NCQ_TRIM |
Martin K. Petersene61f7d12015-01-08 10:34:27 -05004025 ATA_HORKAGE_ZERO_AFTER_TRIM, },
Martin K. Petersenff7f53f2015-03-27 15:17:20 -04004026 { "Crucial_CT*M500*", NULL, ATA_HORKAGE_NO_NCQ_TRIM |
4027 ATA_HORKAGE_ZERO_AFTER_TRIM, },
Martin K. Petersen9051bd32015-07-15 21:03:23 -04004028 { "Micron_M5[15]0_*", "MU01", ATA_HORKAGE_NO_NCQ_TRIM |
Martin K. Petersenff7f53f2015-03-27 15:17:20 -04004029 ATA_HORKAGE_ZERO_AFTER_TRIM, },
4030 { "Crucial_CT*M550*", "MU01", ATA_HORKAGE_NO_NCQ_TRIM |
4031 ATA_HORKAGE_ZERO_AFTER_TRIM, },
4032 { "Crucial_CT*MX100*", "MU01", ATA_HORKAGE_NO_NCQ_TRIM |
4033 ATA_HORKAGE_ZERO_AFTER_TRIM, },
Ju Hyung Parkca6bfcb2018-03-11 02:28:35 +09004034 { "Samsung SSD 840*", NULL, ATA_HORKAGE_NO_NCQ_TRIM |
4035 ATA_HORKAGE_ZERO_AFTER_TRIM, },
4036 { "Samsung SSD 850*", NULL, ATA_HORKAGE_NO_NCQ_TRIM |
Martin K. Petersen6fc4d972015-03-27 15:17:21 -04004037 ATA_HORKAGE_ZERO_AFTER_TRIM, },
Hans de Goede8a6430a2021-08-23 11:52:20 +02004038 { "Samsung SSD 860*", NULL, ATA_HORKAGE_NO_NCQ_TRIM |
Kate Hsuan7a8526a2021-09-03 17:44:11 +08004039 ATA_HORKAGE_ZERO_AFTER_TRIM |
4040 ATA_HORKAGE_NO_NCQ_ON_ATI, },
Hans de Goede8a6430a2021-08-23 11:52:20 +02004041 { "Samsung SSD 870*", NULL, ATA_HORKAGE_NO_NCQ_TRIM |
Kate Hsuan7a8526a2021-09-03 17:44:11 +08004042 ATA_HORKAGE_ZERO_AFTER_TRIM |
4043 ATA_HORKAGE_NO_NCQ_ON_ATI, },
Guillermo A. Amaral7a7184b2015-08-25 23:29:13 -07004044 { "FCCT*M500*", NULL, ATA_HORKAGE_NO_NCQ_TRIM |
4045 ATA_HORKAGE_ZERO_AFTER_TRIM, },
Martin K. Petersene61f7d12015-01-08 10:34:27 -05004046
Arne Fitzenreitercda57b12015-07-15 13:54:37 +02004047 /* devices that don't properly handle TRIM commands */
4048 { "SuperSSpeed S238*", NULL, ATA_HORKAGE_NOTRIM, },
4049
Martin K. Petersene61f7d12015-01-08 10:34:27 -05004050 /*
4051 * As defined, the DRAT (Deterministic Read After Trim) and RZAT
4052 * (Return Zero After Trim) flags in the ATA Command Set are
4053 * unreliable in the sense that they only define what happens if
4054 * the device successfully executed the DSM TRIM command. TRIM
4055 * is only advisory, however, and the device is free to silently
4056 * ignore all or parts of the request.
4057 *
4058 * Whitelist drives that are known to reliably return zeroes
4059 * after TRIM.
4060 */
4061
4062 /*
4063 * The intel 510 drive has buggy DRAT/RZAT. Explicitly exclude
4064 * that model before whitelisting all other intel SSDs.
4065 */
4066 { "INTEL*SSDSC2MH*", NULL, 0, },
4067
Martin K. Petersenff7f53f2015-03-27 15:17:20 -04004068 { "Micron*", NULL, ATA_HORKAGE_ZERO_AFTER_TRIM, },
4069 { "Crucial*", NULL, ATA_HORKAGE_ZERO_AFTER_TRIM, },
Martin K. Petersene61f7d12015-01-08 10:34:27 -05004070 { "INTEL*SSD*", NULL, ATA_HORKAGE_ZERO_AFTER_TRIM, },
4071 { "SSD*INTEL*", NULL, ATA_HORKAGE_ZERO_AFTER_TRIM, },
4072 { "Samsung*SSD*", NULL, ATA_HORKAGE_ZERO_AFTER_TRIM, },
4073 { "SAMSUNG*SSD*", NULL, ATA_HORKAGE_ZERO_AFTER_TRIM, },
Juha-Matti Tillifd6f32f2018-12-02 12:47:08 +02004074 { "SAMSUNG*MZ7KM*", NULL, ATA_HORKAGE_ZERO_AFTER_TRIM, },
Martin K. Petersene61f7d12015-01-08 10:34:27 -05004075 { "ST[1248][0248]0[FH]*", NULL, ATA_HORKAGE_ZERO_AFTER_TRIM, },
Marc Carinof78dea02013-12-16 18:15:53 -08004076
Tejun Heoecd75ad2014-01-16 09:47:17 -05004077 /*
4078 * Some WD SATA-I drives spin up and down erratically when the link
4079 * is put into the slumber mode. We don't have full list of the
4080 * affected devices. Disable LPM if the device matches one of the
4081 * known prefixes and is SATA-1. As a side effect LPM partial is
4082 * lost too.
4083 *
4084 * https://bugzilla.kernel.org/show_bug.cgi?id=57211
4085 */
4086 { "WDC WD800JD-*", NULL, ATA_HORKAGE_WD_BROKEN_LPM },
4087 { "WDC WD1200JD-*", NULL, ATA_HORKAGE_WD_BROKEN_LPM },
4088 { "WDC WD1600JD-*", NULL, ATA_HORKAGE_WD_BROKEN_LPM },
4089 { "WDC WD2000JD-*", NULL, ATA_HORKAGE_WD_BROKEN_LPM },
4090 { "WDC WD2500JD-*", NULL, ATA_HORKAGE_WD_BROKEN_LPM },
4091 { "WDC WD3000JD-*", NULL, ATA_HORKAGE_WD_BROKEN_LPM },
4092 { "WDC WD3200JD-*", NULL, ATA_HORKAGE_WD_BROKEN_LPM },
4093
Alan Cox6919a0a2006-10-27 19:08:46 -07004094 /* End Marker */
4095 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004096};
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05004097
Tejun Heo75683fe2007-07-05 13:31:27 +09004098static unsigned long ata_dev_blacklisted(const struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004099{
Tejun Heo8bfa79f2007-01-02 20:19:40 +09004100 unsigned char model_num[ATA_ID_PROD_LEN + 1];
4101 unsigned char model_rev[ATA_ID_FW_REV_LEN + 1];
Alan Cox6919a0a2006-10-27 19:08:46 -07004102 const struct ata_blacklist_entry *ad = ata_device_blacklist;
Albert Lee3a778272006-06-22 13:00:25 +08004103
Tejun Heo8bfa79f2007-01-02 20:19:40 +09004104 ata_id_c_string(dev->id, model_num, ATA_ID_PROD, sizeof(model_num));
4105 ata_id_c_string(dev->id, model_rev, ATA_ID_FW_REV, sizeof(model_rev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004106
Alan Cox6919a0a2006-10-27 19:08:46 -07004107 while (ad->model_num) {
George Spelvin1c402792014-10-07 07:26:38 -04004108 if (glob_match(ad->model_num, model_num)) {
Alan Cox6919a0a2006-10-27 19:08:46 -07004109 if (ad->model_rev == NULL)
4110 return ad->horkage;
George Spelvin1c402792014-10-07 07:26:38 -04004111 if (glob_match(ad->model_rev, model_rev))
Alan Cox6919a0a2006-10-27 19:08:46 -07004112 return ad->horkage;
Alan Coxf4b15fe2006-03-22 15:54:04 +00004113 }
Alan Cox6919a0a2006-10-27 19:08:46 -07004114 ad++;
Alan Coxf4b15fe2006-03-22 15:54:04 +00004115 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004116 return 0;
4117}
4118
Alan Cox6919a0a2006-10-27 19:08:46 -07004119static int ata_dma_blacklisted(const struct ata_device *dev)
4120{
4121 /* We don't support polling DMA.
4122 * DMA blacklist those ATAPI devices with CDB-intr (and use PIO)
4123 * if the LLDD handles only interrupts in the HSM_ST_LAST state.
4124 */
Tejun Heo9af5c9c2007-08-06 18:36:22 +09004125 if ((dev->link->ap->flags & ATA_FLAG_PIO_POLLING) &&
Alan Cox6919a0a2006-10-27 19:08:46 -07004126 (dev->flags & ATA_DFLAG_CDB_INTR))
4127 return 1;
Tejun Heo75683fe2007-07-05 13:31:27 +09004128 return (dev->horkage & ATA_HORKAGE_NODMA) ? 1 : 0;
Alan Cox6919a0a2006-10-27 19:08:46 -07004129}
4130
Tejun Heoa6d5a512006-03-06 04:31:57 +09004131/**
Alan Cox6bbfd532007-11-05 22:58:58 +00004132 * ata_is_40wire - check drive side detection
4133 * @dev: device
4134 *
4135 * Perform drive side detection decoding, allowing for device vendors
4136 * who can't follow the documentation.
4137 */
4138
4139static int ata_is_40wire(struct ata_device *dev)
4140{
4141 if (dev->horkage & ATA_HORKAGE_IVB)
4142 return ata_drive_40wire_relaxed(dev->id);
4143 return ata_drive_40wire(dev->id);
4144}
4145
4146/**
Alan Cox15a55512008-03-28 14:33:46 -07004147 * cable_is_40wire - 40/80/SATA decider
4148 * @ap: port to consider
4149 *
4150 * This function encapsulates the policy for speed management
4151 * in one place. At the moment we don't cache the result but
4152 * there is a good case for setting ap->cbl to the result when
4153 * we are called with unknown cables (and figuring out if it
4154 * impacts hotplug at all).
4155 *
4156 * Return 1 if the cable appears to be 40 wire.
4157 */
4158
4159static int cable_is_40wire(struct ata_port *ap)
4160{
4161 struct ata_link *link;
4162 struct ata_device *dev;
4163
Tejun Heo4a9c7b32008-10-27 19:59:23 +09004164 /* If the controller thinks we are 40 wire, we are. */
Alan Cox15a55512008-03-28 14:33:46 -07004165 if (ap->cbl == ATA_CBL_PATA40)
4166 return 1;
Tejun Heo4a9c7b32008-10-27 19:59:23 +09004167
4168 /* If the controller thinks we are 80 wire, we are. */
Alan Cox15a55512008-03-28 14:33:46 -07004169 if (ap->cbl == ATA_CBL_PATA80 || ap->cbl == ATA_CBL_SATA)
4170 return 0;
Tejun Heo4a9c7b32008-10-27 19:59:23 +09004171
4172 /* If the system is known to be 40 wire short cable (eg
4173 * laptop), then we allow 80 wire modes even if the drive
4174 * isn't sure.
4175 */
Alan Coxf7920682008-04-08 16:37:50 +01004176 if (ap->cbl == ATA_CBL_PATA40_SHORT)
4177 return 0;
Alan Cox15a55512008-03-28 14:33:46 -07004178
Tejun Heo4a9c7b32008-10-27 19:59:23 +09004179 /* If the controller doesn't know, we scan.
4180 *
4181 * Note: We look for all 40 wire detects at this point. Any
4182 * 80 wire detect is taken to be 80 wire cable because
4183 * - in many setups only the one drive (slave if present) will
4184 * give a valid detect
4185 * - if you have a non detect capable drive you don't want it
4186 * to colour the choice
4187 */
Tejun Heo1eca4362008-11-03 20:03:17 +09004188 ata_for_each_link(link, ap, EDGE) {
4189 ata_for_each_dev(dev, link, ENABLED) {
4190 if (!ata_is_40wire(dev))
Alan Cox15a55512008-03-28 14:33:46 -07004191 return 0;
4192 }
4193 }
4194 return 1;
4195}
4196
4197/**
Tejun Heoa6d5a512006-03-06 04:31:57 +09004198 * ata_dev_xfermask - Compute supported xfermask of the given device
Tejun Heoa6d5a512006-03-06 04:31:57 +09004199 * @dev: Device to compute xfermask for
4200 *
Tejun Heoacf356b2006-03-24 14:07:50 +09004201 * Compute supported xfermask of @dev and store it in
4202 * dev->*_mask. This function is responsible for applying all
4203 * known limits including host controller limits, device
4204 * blacklist, etc...
Tejun Heoa6d5a512006-03-06 04:31:57 +09004205 *
4206 * LOCKING:
4207 * None.
Tejun Heoa6d5a512006-03-06 04:31:57 +09004208 */
Tejun Heo3373efd2006-05-15 20:57:53 +09004209static void ata_dev_xfermask(struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004210{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09004211 struct ata_link *link = dev->link;
4212 struct ata_port *ap = link->ap;
Jeff Garzikcca39742006-08-24 03:19:22 -04004213 struct ata_host *host = ap->host;
Tejun Heoa6d5a512006-03-06 04:31:57 +09004214 unsigned long xfer_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004215
Tejun Heo37deecb2006-08-10 16:59:07 +09004216 /* controller modes available */
Tejun Heo565083e2006-04-02 17:54:47 +09004217 xfer_mask = ata_pack_xfermask(ap->pio_mask,
4218 ap->mwdma_mask, ap->udma_mask);
4219
Robert Hancock8343f882007-03-06 02:37:51 -08004220 /* drive modes available */
Tejun Heo37deecb2006-08-10 16:59:07 +09004221 xfer_mask &= ata_pack_xfermask(dev->pio_mask,
4222 dev->mwdma_mask, dev->udma_mask);
4223 xfer_mask &= ata_id_xfermask(dev->id);
Tejun Heo565083e2006-04-02 17:54:47 +09004224
Alan Coxb352e572006-08-10 18:52:12 +01004225 /*
4226 * CFA Advanced TrueIDE timings are not allowed on a shared
4227 * cable
4228 */
4229 if (ata_dev_pair(dev)) {
4230 /* No PIO5 or PIO6 */
4231 xfer_mask &= ~(0x03 << (ATA_SHIFT_PIO + 5));
4232 /* No MWDMA3 or MWDMA 4 */
4233 xfer_mask &= ~(0x03 << (ATA_SHIFT_MWDMA + 3));
4234 }
4235
Tejun Heo37deecb2006-08-10 16:59:07 +09004236 if (ata_dma_blacklisted(dev)) {
4237 xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
Joe Perchesa9a79df2011-04-15 15:51:59 -07004238 ata_dev_warn(dev,
4239 "device is on DMA blacklist, disabling DMA\n");
Tejun Heo37deecb2006-08-10 16:59:07 +09004240 }
Tejun Heoa6d5a512006-03-06 04:31:57 +09004241
Petr Vandrovec14d66ab2007-03-08 10:12:12 +01004242 if ((host->flags & ATA_HOST_SIMPLEX) &&
Jeff Garzik2dcb4072007-10-19 06:42:56 -04004243 host->simplex_claimed && host->simplex_claimed != ap) {
Tejun Heo37deecb2006-08-10 16:59:07 +09004244 xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
Joe Perchesa9a79df2011-04-15 15:51:59 -07004245 ata_dev_warn(dev,
4246 "simplex DMA is claimed by other device, disabling DMA\n");
Alan Cox5444a6f2006-03-27 18:58:20 +01004247 }
Tejun Heo565083e2006-04-02 17:54:47 +09004248
Jeff Garzike4246752007-03-09 09:56:46 -05004249 if (ap->flags & ATA_FLAG_NO_IORDY)
4250 xfer_mask &= ata_pio_mask_no_iordy(dev);
4251
Alan Cox5444a6f2006-03-27 18:58:20 +01004252 if (ap->ops->mode_filter)
Alan Coxa76b62ca2007-03-09 09:34:07 -05004253 xfer_mask = ap->ops->mode_filter(dev, xfer_mask);
Alan Cox5444a6f2006-03-27 18:58:20 +01004254
Robert Hancock8343f882007-03-06 02:37:51 -08004255 /* Apply cable rule here. Don't apply it early because when
4256 * we handle hot plug the cable type can itself change.
4257 * Check this last so that we know if the transfer rate was
4258 * solely limited by the cable.
4259 * Unknown or 80 wire cables reported host side are checked
4260 * drive side as well. Cases where we know a 40wire cable
4261 * is used safely for 80 are not checked here.
4262 */
4263 if (xfer_mask & (0xF8 << ATA_SHIFT_UDMA))
4264 /* UDMA/44 or higher would be available */
Alan Cox15a55512008-03-28 14:33:46 -07004265 if (cable_is_40wire(ap)) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07004266 ata_dev_warn(dev,
4267 "limited to UDMA/33 due to 40-wire cable\n");
Robert Hancock8343f882007-03-06 02:37:51 -08004268 xfer_mask &= ~(0xF8 << ATA_SHIFT_UDMA);
4269 }
4270
Tejun Heo565083e2006-04-02 17:54:47 +09004271 ata_unpack_xfermask(xfer_mask, &dev->pio_mask,
4272 &dev->mwdma_mask, &dev->udma_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004273}
4274
Linus Torvalds1da177e2005-04-16 15:20:36 -07004275/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004276 * ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
Linus Torvalds1da177e2005-04-16 15:20:36 -07004277 * @dev: Device to which command will be sent
4278 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04004279 * Issue SET FEATURES - XFER MODE command to device @dev
4280 * on port @ap.
4281 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004282 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004283 * PCI/etc. bus probe sem.
Tejun Heo83206a22006-03-24 15:25:31 +09004284 *
4285 * RETURNS:
4286 * 0 on success, AC_ERR_* mask otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004287 */
4288
Tejun Heo3373efd2006-05-15 20:57:53 +09004289static unsigned int ata_dev_set_xfermode(struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004290{
Tejun Heoa0123702005-12-13 14:49:31 +09004291 struct ata_taskfile tf;
Tejun Heo83206a22006-03-24 15:25:31 +09004292 unsigned int err_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004293
4294 /* set up set-features taskfile */
4295 DPRINTK("set features - xfer mode\n");
4296
Tejun Heo464cf172007-05-27 15:10:40 +02004297 /* Some controllers and ATAPI devices show flaky interrupt
4298 * behavior after setting xfer mode. Use polling instead.
4299 */
Tejun Heo3373efd2006-05-15 20:57:53 +09004300 ata_tf_init(dev, &tf);
Tejun Heoa0123702005-12-13 14:49:31 +09004301 tf.command = ATA_CMD_SET_FEATURES;
4302 tf.feature = SETFEATURES_XFER;
Tejun Heo464cf172007-05-27 15:10:40 +02004303 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE | ATA_TFLAG_POLLING;
Tejun Heoa0123702005-12-13 14:49:31 +09004304 tf.protocol = ATA_PROT_NODATA;
Alan Coxb9f8ab22007-11-19 14:33:11 +00004305 /* If we are using IORDY we must send the mode setting command */
Jeff Garzik11b7bec2007-11-23 21:12:14 -05004306 if (ata_pio_need_iordy(dev))
4307 tf.nsect = dev->xfer_mode;
Alan Coxb9f8ab22007-11-19 14:33:11 +00004308 /* If the device has IORDY and the controller does not - turn it off */
4309 else if (ata_id_has_iordy(dev->id))
Jeff Garzik11b7bec2007-11-23 21:12:14 -05004310 tf.nsect = 0x01;
Alan Coxb9f8ab22007-11-19 14:33:11 +00004311 else /* In the ancient relic department - skip all of this */
4312 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004313
Mikulas Patockad531be22015-07-08 13:06:12 -04004314 /* On some disks, this command causes spin-up, so we need longer timeout */
4315 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 15000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004316
Tejun Heo83206a22006-03-24 15:25:31 +09004317 DPRINTK("EXIT, err_mask=%x\n", err_mask);
4318 return err_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004319}
Tejun Heo1152b262010-09-01 17:50:05 +02004320
Linus Torvalds1da177e2005-04-16 15:20:36 -07004321/**
Jeff Garzik218f3d32007-10-25 00:33:27 -04004322 * ata_dev_set_feature - Issue SET FEATURES - SATA FEATURES
Kristen Carlson Accardi9f45cbd2007-08-15 03:57:11 -04004323 * @dev: Device to which command will be sent
4324 * @enable: Whether to enable or disable the feature
Jeff Garzik218f3d32007-10-25 00:33:27 -04004325 * @feature: The sector count represents the feature to set
Kristen Carlson Accardi9f45cbd2007-08-15 03:57:11 -04004326 *
4327 * Issue SET FEATURES - SATA FEATURES command to device @dev
Jeff Garzik218f3d32007-10-25 00:33:27 -04004328 * on port @ap with sector count
Kristen Carlson Accardi9f45cbd2007-08-15 03:57:11 -04004329 *
4330 * LOCKING:
4331 * PCI/etc. bus probe sem.
4332 *
4333 * RETURNS:
4334 * 0 on success, AC_ERR_* mask otherwise.
4335 */
Tejun Heo1152b262010-09-01 17:50:05 +02004336unsigned int ata_dev_set_feature(struct ata_device *dev, u8 enable, u8 feature)
Kristen Carlson Accardi9f45cbd2007-08-15 03:57:11 -04004337{
4338 struct ata_taskfile tf;
4339 unsigned int err_mask;
Damien Le Moal974e0a42016-04-04 12:17:09 -04004340 unsigned long timeout = 0;
Kristen Carlson Accardi9f45cbd2007-08-15 03:57:11 -04004341
4342 /* set up set-features taskfile */
4343 DPRINTK("set features - SATA features\n");
4344
4345 ata_tf_init(dev, &tf);
4346 tf.command = ATA_CMD_SET_FEATURES;
4347 tf.feature = enable;
4348 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
4349 tf.protocol = ATA_PROT_NODATA;
Jeff Garzik218f3d32007-10-25 00:33:27 -04004350 tf.nsect = feature;
Kristen Carlson Accardi9f45cbd2007-08-15 03:57:11 -04004351
Damien Le Moal974e0a42016-04-04 12:17:09 -04004352 if (enable == SETFEATURES_SPINUP)
4353 timeout = ata_probe_timeout ?
4354 ata_probe_timeout * 1000 : SETFEATURES_SPINUP_TIMEOUT;
4355 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, timeout);
Kristen Carlson Accardi9f45cbd2007-08-15 03:57:11 -04004356
4357 DPRINTK("EXIT, err_mask=%x\n", err_mask);
4358 return err_mask;
4359}
Jeff Garzik633de4c2012-09-13 01:12:29 -04004360EXPORT_SYMBOL_GPL(ata_dev_set_feature);
Kristen Carlson Accardi9f45cbd2007-08-15 03:57:11 -04004361
4362/**
Albert Lee8bf62ece2005-05-12 15:29:42 -04004363 * ata_dev_init_params - Issue INIT DEV PARAMS command
Albert Lee8bf62ece2005-05-12 15:29:42 -04004364 * @dev: Device to which command will be sent
Randy Dunlape2a7f772006-05-18 10:50:18 -07004365 * @heads: Number of heads (taskfile parameter)
4366 * @sectors: Number of sectors (taskfile parameter)
Albert Lee8bf62ece2005-05-12 15:29:42 -04004367 *
4368 * LOCKING:
Tejun Heo6aff8f12006-02-15 18:24:09 +09004369 * Kernel thread context (may sleep)
4370 *
4371 * RETURNS:
4372 * 0 on success, AC_ERR_* mask otherwise.
Albert Lee8bf62ece2005-05-12 15:29:42 -04004373 */
Tejun Heo3373efd2006-05-15 20:57:53 +09004374static unsigned int ata_dev_init_params(struct ata_device *dev,
4375 u16 heads, u16 sectors)
Albert Lee8bf62ece2005-05-12 15:29:42 -04004376{
Tejun Heoa0123702005-12-13 14:49:31 +09004377 struct ata_taskfile tf;
Tejun Heo6aff8f12006-02-15 18:24:09 +09004378 unsigned int err_mask;
Albert Lee8bf62ece2005-05-12 15:29:42 -04004379
4380 /* Number of sectors per track 1-255. Number of heads 1-16 */
4381 if (sectors < 1 || sectors > 255 || heads < 1 || heads > 16)
Albert Lee00b6f5e2006-03-27 16:39:18 +08004382 return AC_ERR_INVALID;
Albert Lee8bf62ece2005-05-12 15:29:42 -04004383
4384 /* set up init dev params taskfile */
4385 DPRINTK("init dev params \n");
4386
Tejun Heo3373efd2006-05-15 20:57:53 +09004387 ata_tf_init(dev, &tf);
Tejun Heoa0123702005-12-13 14:49:31 +09004388 tf.command = ATA_CMD_INIT_DEV_PARAMS;
4389 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
4390 tf.protocol = ATA_PROT_NODATA;
4391 tf.nsect = sectors;
4392 tf.device |= (heads - 1) & 0x0f; /* max head = num. of heads - 1 */
Albert Lee8bf62ece2005-05-12 15:29:42 -04004393
Tejun Heo2b789102007-10-09 15:05:44 +09004394 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
Alan Cox18b24662007-08-08 14:28:49 +01004395 /* A clean abort indicates an original or just out of spec drive
4396 and we should continue as we issue the setup based on the
4397 drive reported working geometry */
4398 if (err_mask == AC_ERR_DEV && (tf.feature & ATA_ABORTED))
4399 err_mask = 0;
Albert Lee8bf62ece2005-05-12 15:29:42 -04004400
Tejun Heo6aff8f12006-02-15 18:24:09 +09004401 DPRINTK("EXIT, err_mask=%x\n", err_mask);
4402 return err_mask;
Albert Lee8bf62ece2005-05-12 15:29:42 -04004403}
4404
4405/**
Tejun Heo5895ef92008-06-17 12:36:26 +09004406 * atapi_check_dma - Check whether ATAPI DMA can be supported
Linus Torvalds1da177e2005-04-16 15:20:36 -07004407 * @qc: Metadata associated with taskfile to check
4408 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04004409 * Allow low-level driver to filter ATA PACKET commands, returning
4410 * a status indicating whether or not it is OK to use DMA for the
4411 * supplied PACKET command.
4412 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004413 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04004414 * spin_lock_irqsave(host lock)
Jeff Garzik0cba6322005-05-30 19:49:12 -04004415 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004416 * RETURNS: 0 when ATAPI DMA can be used
4417 * nonzero otherwise
4418 */
Tejun Heo5895ef92008-06-17 12:36:26 +09004419int atapi_check_dma(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004420{
4421 struct ata_port *ap = qc->ap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004422
Tejun Heob9a41972007-06-27 02:48:43 +09004423 /* Don't allow DMA if it isn't multiple of 16 bytes. Quite a
4424 * few ATAPI devices choke on such DMA requests.
4425 */
Tejun Heo6a87e422008-11-03 19:01:09 +09004426 if (!(qc->dev->horkage & ATA_HORKAGE_ATAPI_MOD16_DMA) &&
4427 unlikely(qc->nbytes & 15))
Tejun Heob9a41972007-06-27 02:48:43 +09004428 return 1;
Albert Lee6f23a312007-04-02 11:39:25 +08004429
Linus Torvalds1da177e2005-04-16 15:20:36 -07004430 if (ap->ops->check_atapi_dma)
Tejun Heob9a41972007-06-27 02:48:43 +09004431 return ap->ops->check_atapi_dma(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004432
Tejun Heob9a41972007-06-27 02:48:43 +09004433 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004434}
Tejun Heob9a41972007-06-27 02:48:43 +09004435
Linus Torvalds1da177e2005-04-16 15:20:36 -07004436/**
Tejun Heo31cc23b2007-09-23 13:14:12 +09004437 * ata_std_qc_defer - Check whether a qc needs to be deferred
4438 * @qc: ATA command in question
4439 *
4440 * Non-NCQ commands cannot run with any other command, NCQ or
4441 * not. As upper layer only knows the queue depth, we are
4442 * responsible for maintaining exclusion. This function checks
4443 * whether a new command @qc can be issued.
4444 *
4445 * LOCKING:
4446 * spin_lock_irqsave(host lock)
4447 *
4448 * RETURNS:
4449 * ATA_DEFER_* if deferring is needed, 0 otherwise.
4450 */
4451int ata_std_qc_defer(struct ata_queued_cmd *qc)
4452{
4453 struct ata_link *link = qc->dev->link;
4454
Hannes Reinecke179b3102016-07-14 09:05:43 +09004455 if (ata_is_ncq(qc->tf.protocol)) {
Tejun Heo31cc23b2007-09-23 13:14:12 +09004456 if (!ata_tag_valid(link->active_tag))
4457 return 0;
4458 } else {
4459 if (!ata_tag_valid(link->active_tag) && !link->sactive)
4460 return 0;
4461 }
4462
4463 return ATA_DEFER_LINK;
4464}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01004465EXPORT_SYMBOL_GPL(ata_std_qc_defer);
Tejun Heo31cc23b2007-09-23 13:14:12 +09004466
Jiri Slaby95364f32019-10-31 10:59:45 +01004467enum ata_completion_errors ata_noop_qc_prep(struct ata_queued_cmd *qc)
4468{
4469 return AC_ERR_OK;
4470}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01004471EXPORT_SYMBOL_GPL(ata_noop_qc_prep);
Brian Kinge46834c2006-03-17 17:04:03 -06004472
Jeff Garzik0cba6322005-05-30 19:49:12 -04004473/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04004474 * ata_sg_init - Associate command with scatter-gather table.
4475 * @qc: Command to be associated
4476 * @sg: Scatter-gather table.
4477 * @n_elem: Number of elements in s/g table.
4478 *
4479 * Initialize the data-related elements of queued_cmd @qc
4480 * to point to a scatter-gather table @sg, containing @n_elem
4481 * elements.
4482 *
4483 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04004484 * spin_lock_irqsave(host lock)
Jeff Garzik0cba6322005-05-30 19:49:12 -04004485 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004486void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
4487 unsigned int n_elem)
4488{
Tejun Heoff2aeb12007-12-05 16:43:11 +09004489 qc->sg = sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004490 qc->n_elem = n_elem;
Tejun Heoff2aeb12007-12-05 16:43:11 +09004491 qc->cursg = qc->sg;
4492}
4493
Geert Uytterhoeven2874d5e2017-01-03 19:09:49 +01004494#ifdef CONFIG_HAS_DMA
4495
4496/**
4497 * ata_sg_clean - Unmap DMA memory associated with command
4498 * @qc: Command containing DMA memory to be released
4499 *
4500 * Unmap all mapped DMA memory associated with this command.
4501 *
4502 * LOCKING:
4503 * spin_lock_irqsave(host lock)
4504 */
Jason Yanaf27e012017-03-10 10:05:40 +08004505static void ata_sg_clean(struct ata_queued_cmd *qc)
Geert Uytterhoeven2874d5e2017-01-03 19:09:49 +01004506{
4507 struct ata_port *ap = qc->ap;
4508 struct scatterlist *sg = qc->sg;
4509 int dir = qc->dma_dir;
4510
4511 WARN_ON_ONCE(sg == NULL);
4512
4513 VPRINTK("unmapping %u sg elements\n", qc->n_elem);
4514
4515 if (qc->n_elem)
4516 dma_unmap_sg(ap->dev, sg, qc->orig_n_elem, dir);
4517
4518 qc->flags &= ~ATA_QCFLAG_DMAMAP;
4519 qc->sg = NULL;
4520}
4521
Linus Torvalds1da177e2005-04-16 15:20:36 -07004522/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04004523 * ata_sg_setup - DMA-map the scatter-gather table associated with a command.
4524 * @qc: Command with scatter-gather table to be mapped.
4525 *
4526 * DMA-map the scatter-gather table associated with queued_cmd @qc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004527 *
4528 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04004529 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004530 *
4531 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004532 * Zero on success, negative on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004533 *
4534 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004535static int ata_sg_setup(struct ata_queued_cmd *qc)
4536{
4537 struct ata_port *ap = qc->ap;
James Bottomleydde20202008-02-19 11:36:56 +01004538 unsigned int n_elem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004539
James Bottomleydde20202008-02-19 11:36:56 +01004540 n_elem = dma_map_sg(ap->dev, qc->sg, qc->n_elem, qc->dma_dir);
4541 if (n_elem < 1)
4542 return -1;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004543
Paul Menzelbb376672018-07-08 09:18:21 +02004544 VPRINTK("%d sg elements mapped\n", n_elem);
FUJITA Tomonori58256272009-02-27 17:35:43 +09004545 qc->orig_n_elem = qc->n_elem;
James Bottomleydde20202008-02-19 11:36:56 +01004546 qc->n_elem = n_elem;
Tejun Heof92a2632007-12-05 16:43:10 +09004547 qc->flags |= ATA_QCFLAG_DMAMAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004548
4549 return 0;
4550}
4551
Geert Uytterhoeven2874d5e2017-01-03 19:09:49 +01004552#else /* !CONFIG_HAS_DMA */
4553
4554static inline void ata_sg_clean(struct ata_queued_cmd *qc) {}
4555static inline int ata_sg_setup(struct ata_queued_cmd *qc) { return -1; }
4556
4557#endif /* !CONFIG_HAS_DMA */
4558
Linus Torvalds1da177e2005-04-16 15:20:36 -07004559/**
Randy Dunlapc893a3a2006-01-28 13:15:32 -05004560 * swap_buf_le16 - swap halves of 16-bit words in place
Edward Falk0baab862005-06-02 18:17:13 -04004561 * @buf: Buffer to swap
4562 * @buf_words: Number of 16-bit words in buffer.
4563 *
4564 * Swap halves of 16-bit words if needed to convert from
4565 * little-endian byte order to native cpu byte order, or
4566 * vice-versa.
4567 *
4568 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04004569 * Inherited from caller.
Edward Falk0baab862005-06-02 18:17:13 -04004570 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004571void swap_buf_le16(u16 *buf, unsigned int buf_words)
4572{
4573#ifdef __BIG_ENDIAN
4574 unsigned int i;
4575
4576 for (i = 0; i < buf_words; i++)
4577 buf[i] = le16_to_cpu(buf[i]);
4578#endif /* __BIG_ENDIAN */
4579}
4580
Albert Lee6ae4cfb2005-08-12 14:15:34 +08004581/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004582 * ata_qc_new_init - Request an available ATA command, and initialize it
Linus Torvalds1da177e2005-04-16 15:20:36 -07004583 * @dev: Device from whom we request an available command structure
Randy Dunlap38755e82015-08-09 18:37:36 -07004584 * @tag: tag
Linus Torvalds1da177e2005-04-16 15:20:36 -07004585 *
4586 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004587 * None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004588 */
4589
Shaohua Li98bd4be2015-01-23 19:52:07 -08004590struct ata_queued_cmd *ata_qc_new_init(struct ata_device *dev, int tag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004591{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09004592 struct ata_port *ap = dev->link->ap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004593 struct ata_queued_cmd *qc;
4594
Shaohua Li98bd4be2015-01-23 19:52:07 -08004595 /* no command while frozen */
4596 if (unlikely(ap->pflags & ATA_PFLAG_FROZEN))
4597 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004598
Shaohua Li98bd4be2015-01-23 19:52:07 -08004599 /* libsas case */
Shaohua Li5067c042015-03-12 10:32:18 -07004600 if (ap->flags & ATA_FLAG_SAS_HOST) {
Shaohua Li98bd4be2015-01-23 19:52:07 -08004601 tag = ata_sas_allocate_tag(ap);
4602 if (tag < 0)
4603 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004604 }
4605
Shaohua Li98bd4be2015-01-23 19:52:07 -08004606 qc = __ata_qc_from_tag(ap, tag);
Jens Axboe5ac40792018-05-11 12:51:03 -06004607 qc->tag = qc->hw_tag = tag;
Shaohua Li98bd4be2015-01-23 19:52:07 -08004608 qc->scsicmd = NULL;
4609 qc->ap = ap;
4610 qc->dev = dev;
4611
4612 ata_qc_reinit(qc);
4613
Linus Torvalds1da177e2005-04-16 15:20:36 -07004614 return qc;
4615}
4616
Tejun Heo8a8bc222008-11-10 14:48:21 +09004617/**
4618 * ata_qc_free - free unused ata_queued_cmd
4619 * @qc: Command to complete
4620 *
4621 * Designed to free unused ata_queued_cmd object
4622 * in case something prevents using it.
4623 *
4624 * LOCKING:
4625 * spin_lock_irqsave(host lock)
4626 */
4627void ata_qc_free(struct ata_queued_cmd *qc)
4628{
Julia Lawalla1104012009-10-17 08:41:47 +02004629 struct ata_port *ap;
Tejun Heo8a8bc222008-11-10 14:48:21 +09004630 unsigned int tag;
4631
Tejun Heoefcb3cf2009-01-09 19:19:14 +09004632 WARN_ON_ONCE(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
Julia Lawalla1104012009-10-17 08:41:47 +02004633 ap = qc->ap;
Tejun Heo8a8bc222008-11-10 14:48:21 +09004634
4635 qc->flags = 0;
4636 tag = qc->tag;
Jens Axboe28361c42018-05-11 12:51:09 -06004637 if (ata_tag_valid(tag)) {
Tejun Heo8a8bc222008-11-10 14:48:21 +09004638 qc->tag = ATA_TAG_POISON;
Shaohua Li5067c042015-03-12 10:32:18 -07004639 if (ap->flags & ATA_FLAG_SAS_HOST)
Shaohua Li98bd4be2015-01-23 19:52:07 -08004640 ata_sas_free_tag(tag, ap);
Tejun Heo8a8bc222008-11-10 14:48:21 +09004641 }
4642}
4643
Tejun Heo76014422006-02-11 15:13:49 +09004644void __ata_qc_complete(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004645{
Julia Lawalla1104012009-10-17 08:41:47 +02004646 struct ata_port *ap;
4647 struct ata_link *link;
Tejun Heodedaf2b2006-05-15 21:03:43 +09004648
Tejun Heoefcb3cf2009-01-09 19:19:14 +09004649 WARN_ON_ONCE(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
4650 WARN_ON_ONCE(!(qc->flags & ATA_QCFLAG_ACTIVE));
Julia Lawalla1104012009-10-17 08:41:47 +02004651 ap = qc->ap;
4652 link = qc->dev->link;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004653
4654 if (likely(qc->flags & ATA_QCFLAG_DMAMAP))
4655 ata_sg_clean(qc);
4656
Tejun Heo7401abf2006-05-15 20:57:32 +09004657 /* command should be marked inactive atomically with qc completion */
Hannes Reinecke179b3102016-07-14 09:05:43 +09004658 if (ata_is_ncq(qc->tf.protocol)) {
Jens Axboe4e5b6262018-05-11 12:51:04 -06004659 link->sactive &= ~(1 << qc->hw_tag);
Tejun Heoda917d62007-09-23 13:14:12 +09004660 if (!link->sactive)
4661 ap->nr_active_links--;
4662 } else {
Tejun Heo9af5c9c2007-08-06 18:36:22 +09004663 link->active_tag = ATA_TAG_POISON;
Tejun Heoda917d62007-09-23 13:14:12 +09004664 ap->nr_active_links--;
4665 }
4666
4667 /* clear exclusive status */
4668 if (unlikely(qc->flags & ATA_QCFLAG_CLEAR_EXCL &&
4669 ap->excl_link == link))
4670 ap->excl_link = NULL;
Tejun Heo7401abf2006-05-15 20:57:32 +09004671
Albert Lee3f3791d2005-08-16 14:25:38 +08004672 /* atapi: mark qc as inactive to prevent the interrupt handler
4673 * from completing the command twice later, before the error handler
4674 * is called. (when rc != 0 and atapi request sense is needed)
4675 */
4676 qc->flags &= ~ATA_QCFLAG_ACTIVE;
Jens Axboee3ed89392018-05-11 12:51:05 -06004677 ap->qc_active &= ~(1ULL << qc->tag);
Albert Lee3f3791d2005-08-16 14:25:38 +08004678
Linus Torvalds1da177e2005-04-16 15:20:36 -07004679 /* call completion callback */
Tejun Heo77853bf2006-01-23 13:09:36 +09004680 qc->complete_fn(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004681}
4682
Tejun Heo39599a52006-11-14 22:37:35 +09004683static void fill_result_tf(struct ata_queued_cmd *qc)
4684{
4685 struct ata_port *ap = qc->ap;
4686
Tejun Heo39599a52006-11-14 22:37:35 +09004687 qc->result_tf.flags = qc->tf.flags;
Tejun Heo22183bf2008-04-07 22:47:20 +09004688 ap->ops->qc_fill_rtf(qc);
Tejun Heo39599a52006-11-14 22:37:35 +09004689}
4690
Tejun Heo00115e02007-11-27 19:28:58 +09004691static void ata_verify_xfer(struct ata_queued_cmd *qc)
4692{
4693 struct ata_device *dev = qc->dev;
4694
Christoph Hellwigeb0effd2016-07-16 22:16:41 +09004695 if (!ata_is_data(qc->tf.protocol))
Tejun Heo00115e02007-11-27 19:28:58 +09004696 return;
4697
4698 if ((dev->mwdma_mask || dev->udma_mask) && ata_is_pio(qc->tf.protocol))
4699 return;
4700
4701 dev->flags &= ~ATA_DFLAG_DUBIOUS_XFER;
4702}
4703
Tejun Heof686bcb2006-05-15 20:58:05 +09004704/**
4705 * ata_qc_complete - Complete an active ATA command
4706 * @qc: Command to complete
Tejun Heof686bcb2006-05-15 20:58:05 +09004707 *
Tejun Heo1aadf5c2010-06-25 15:03:34 +02004708 * Indicate to the mid and upper layers that an ATA command has
4709 * completed, with either an ok or not-ok status.
4710 *
4711 * Refrain from calling this function multiple times when
4712 * successfully completing multiple NCQ commands.
4713 * ata_qc_complete_multiple() should be used instead, which will
4714 * properly update IRQ expect state.
Tejun Heof686bcb2006-05-15 20:58:05 +09004715 *
4716 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04004717 * spin_lock_irqsave(host lock)
Tejun Heof686bcb2006-05-15 20:58:05 +09004718 */
4719void ata_qc_complete(struct ata_queued_cmd *qc)
4720{
4721 struct ata_port *ap = qc->ap;
4722
Stephan Linzeb25cb92016-06-10 07:59:56 +02004723 /* Trigger the LED (if available) */
Linus Walleijd1ed7c52018-02-24 23:45:56 +01004724 ledtrig_disk_activity(!!(qc->tf.flags & ATA_TFLAG_WRITE));
Stephan Linzeb25cb92016-06-10 07:59:56 +02004725
Tejun Heof686bcb2006-05-15 20:58:05 +09004726 /* XXX: New EH and old EH use different mechanisms to
4727 * synchronize EH with regular execution path.
4728 *
4729 * In new EH, a failed qc is marked with ATA_QCFLAG_FAILED.
4730 * Normal execution path is responsible for not accessing a
4731 * failed qc. libata core enforces the rule by returning NULL
4732 * from ata_qc_from_tag() for failed qcs.
4733 *
4734 * Old EH depends on ata_qc_complete() nullifying completion
4735 * requests if ATA_QCFLAG_EH_SCHEDULED is set. Old EH does
4736 * not synchronize with interrupt handler. Only PIO task is
4737 * taken care of.
4738 */
4739 if (ap->ops->error_handler) {
Tejun Heo4dbfa392007-10-25 18:22:44 +09004740 struct ata_device *dev = qc->dev;
4741 struct ata_eh_info *ehi = &dev->link->eh_info;
4742
Tejun Heof686bcb2006-05-15 20:58:05 +09004743 if (unlikely(qc->err_mask))
4744 qc->flags |= ATA_QCFLAG_FAILED;
4745
Tejun Heof08dc1a2010-12-09 15:59:32 +01004746 /*
4747 * Finish internal commands without any further processing
4748 * and always with the result TF filled.
4749 */
4750 if (unlikely(ata_tag_internal(qc->tag))) {
Tejun Heof4b31db92009-10-16 13:00:51 +09004751 fill_result_tf(qc);
Hannes Reinecke255c03d2015-03-27 16:46:38 +01004752 trace_ata_qc_complete_internal(qc);
Tejun Heof08dc1a2010-12-09 15:59:32 +01004753 __ata_qc_complete(qc);
4754 return;
4755 }
Tejun Heof4b31db92009-10-16 13:00:51 +09004756
Tejun Heof08dc1a2010-12-09 15:59:32 +01004757 /*
4758 * Non-internal qc has failed. Fill the result TF and
4759 * summon EH.
4760 */
4761 if (unlikely(qc->flags & ATA_QCFLAG_FAILED)) {
4762 fill_result_tf(qc);
Hannes Reinecke255c03d2015-03-27 16:46:38 +01004763 trace_ata_qc_complete_failed(qc);
Tejun Heof08dc1a2010-12-09 15:59:32 +01004764 ata_qc_schedule_eh(qc);
Tejun Heof4b31db92009-10-16 13:00:51 +09004765 return;
Tejun Heof686bcb2006-05-15 20:58:05 +09004766 }
4767
Tejun Heo4dc738e2009-09-17 18:45:27 +09004768 WARN_ON_ONCE(ap->pflags & ATA_PFLAG_FROZEN);
4769
Tejun Heof686bcb2006-05-15 20:58:05 +09004770 /* read result TF if requested */
4771 if (qc->flags & ATA_QCFLAG_RESULT_TF)
Tejun Heo39599a52006-11-14 22:37:35 +09004772 fill_result_tf(qc);
Tejun Heof686bcb2006-05-15 20:58:05 +09004773
Hannes Reinecke255c03d2015-03-27 16:46:38 +01004774 trace_ata_qc_complete_done(qc);
Tejun Heo4dbfa392007-10-25 18:22:44 +09004775 /* Some commands need post-processing after successful
4776 * completion.
4777 */
4778 switch (qc->tf.command) {
4779 case ATA_CMD_SET_FEATURES:
4780 if (qc->tf.feature != SETFEATURES_WC_ON &&
Tom Yan0c127352016-07-12 21:37:02 +08004781 qc->tf.feature != SETFEATURES_WC_OFF &&
4782 qc->tf.feature != SETFEATURES_RA_ON &&
4783 qc->tf.feature != SETFEATURES_RA_OFF)
Tejun Heo4dbfa392007-10-25 18:22:44 +09004784 break;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05004785 fallthrough;
Tejun Heo4dbfa392007-10-25 18:22:44 +09004786 case ATA_CMD_INIT_DEV_PARAMS: /* CHS translation changed */
4787 case ATA_CMD_SET_MULTI: /* multi_count changed */
4788 /* revalidate device */
4789 ehi->dev_action[dev->devno] |= ATA_EH_REVALIDATE;
4790 ata_port_schedule_eh(ap);
4791 break;
Tejun Heo054a5fb2007-10-25 18:30:36 +09004792
4793 case ATA_CMD_SLEEP:
4794 dev->flags |= ATA_DFLAG_SLEEPING;
4795 break;
Tejun Heo4dbfa392007-10-25 18:22:44 +09004796 }
4797
Tejun Heo00115e02007-11-27 19:28:58 +09004798 if (unlikely(dev->flags & ATA_DFLAG_DUBIOUS_XFER))
4799 ata_verify_xfer(qc);
4800
Tejun Heof686bcb2006-05-15 20:58:05 +09004801 __ata_qc_complete(qc);
4802 } else {
4803 if (qc->flags & ATA_QCFLAG_EH_SCHEDULED)
4804 return;
4805
4806 /* read result TF if failed or requested */
4807 if (qc->err_mask || qc->flags & ATA_QCFLAG_RESULT_TF)
Tejun Heo39599a52006-11-14 22:37:35 +09004808 fill_result_tf(qc);
Tejun Heof686bcb2006-05-15 20:58:05 +09004809
4810 __ata_qc_complete(qc);
4811 }
4812}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01004813EXPORT_SYMBOL_GPL(ata_qc_complete);
Tejun Heof686bcb2006-05-15 20:58:05 +09004814
Tejun Heodedaf2b2006-05-15 21:03:43 +09004815/**
Sascha Hauer8385d752019-12-13 09:04:08 +01004816 * ata_qc_get_active - get bitmask of active qcs
4817 * @ap: port in question
4818 *
4819 * LOCKING:
4820 * spin_lock_irqsave(host lock)
4821 *
4822 * RETURNS:
4823 * Bitmask of active qcs
4824 */
4825u64 ata_qc_get_active(struct ata_port *ap)
4826{
4827 u64 qc_active = ap->qc_active;
4828
4829 /* ATA_TAG_INTERNAL is sent to hw as tag 0 */
4830 if (qc_active & (1ULL << ATA_TAG_INTERNAL)) {
4831 qc_active |= (1 << 0);
4832 qc_active &= ~(1ULL << ATA_TAG_INTERNAL);
4833 }
4834
4835 return qc_active;
4836}
4837EXPORT_SYMBOL_GPL(ata_qc_get_active);
4838
4839/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004840 * ata_qc_issue - issue taskfile to device
4841 * @qc: command to issue to device
4842 *
4843 * Prepare an ATA command to submission to device.
4844 * This includes mapping the data into a DMA-able
4845 * area, filling in the S/G table, and finally
4846 * writing the taskfile to hardware, starting the command.
4847 *
4848 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04004849 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004850 */
Tejun Heo8e0e6942006-03-31 20:41:11 +09004851void ata_qc_issue(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004852{
4853 struct ata_port *ap = qc->ap;
Tejun Heo9af5c9c2007-08-06 18:36:22 +09004854 struct ata_link *link = qc->dev->link;
Tejun Heo405e66b2007-11-27 19:28:53 +09004855 u8 prot = qc->tf.protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004856
Tejun Heodedaf2b2006-05-15 21:03:43 +09004857 /* Make sure only one non-NCQ command is outstanding. The
4858 * check is skipped for old EH because it reuses active qc to
4859 * request ATAPI sense.
4860 */
Tejun Heoefcb3cf2009-01-09 19:19:14 +09004861 WARN_ON_ONCE(ap->ops->error_handler && ata_tag_valid(link->active_tag));
Tejun Heodedaf2b2006-05-15 21:03:43 +09004862
Tejun Heo1973a022007-12-05 10:36:13 +09004863 if (ata_is_ncq(prot)) {
Jens Axboe4e5b6262018-05-11 12:51:04 -06004864 WARN_ON_ONCE(link->sactive & (1 << qc->hw_tag));
Tejun Heoda917d62007-09-23 13:14:12 +09004865
4866 if (!link->sactive)
4867 ap->nr_active_links++;
Jens Axboe4e5b6262018-05-11 12:51:04 -06004868 link->sactive |= 1 << qc->hw_tag;
Tejun Heodedaf2b2006-05-15 21:03:43 +09004869 } else {
Tejun Heoefcb3cf2009-01-09 19:19:14 +09004870 WARN_ON_ONCE(link->sactive);
Tejun Heoda917d62007-09-23 13:14:12 +09004871
4872 ap->nr_active_links++;
Tejun Heo9af5c9c2007-08-06 18:36:22 +09004873 link->active_tag = qc->tag;
Tejun Heodedaf2b2006-05-15 21:03:43 +09004874 }
4875
Tejun Heoe4a70e72006-03-31 20:36:47 +09004876 qc->flags |= ATA_QCFLAG_ACTIVE;
Jens Axboee3ed89392018-05-11 12:51:05 -06004877 ap->qc_active |= 1ULL << qc->tag;
Tejun Heoe4a70e72006-03-31 20:36:47 +09004878
Tejun Heo60f5d6e2010-08-23 11:27:27 +02004879 /*
4880 * We guarantee to LLDs that they will have at least one
Tejun Heof92a2632007-12-05 16:43:10 +09004881 * non-zero sg if the command is a data command.
4882 */
Eric Biggers9173e5e2018-02-03 20:33:27 -08004883 if (ata_is_data(prot) && (!qc->sg || !qc->n_elem || !qc->nbytes))
Tejun Heo60f5d6e2010-08-23 11:27:27 +02004884 goto sys_err;
Tejun Heof92a2632007-12-05 16:43:10 +09004885
Tejun Heo405e66b2007-11-27 19:28:53 +09004886 if (ata_is_dma(prot) || (ata_is_pio(prot) &&
Tejun Heof92a2632007-12-05 16:43:10 +09004887 (ap->flags & ATA_FLAG_PIO_DMA)))
Tejun Heo001102d2007-12-05 16:43:09 +09004888 if (ata_sg_setup(qc))
Tejun Heo60f5d6e2010-08-23 11:27:27 +02004889 goto sys_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004890
Tejun Heocf480622008-01-24 00:05:14 +09004891 /* if device is sleeping, schedule reset and abort the link */
Tejun Heo054a5fb2007-10-25 18:30:36 +09004892 if (unlikely(qc->dev->flags & ATA_DFLAG_SLEEPING)) {
Tejun Heocf480622008-01-24 00:05:14 +09004893 link->eh_info.action |= ATA_EH_RESET;
Tejun Heo054a5fb2007-10-25 18:30:36 +09004894 ata_ehi_push_desc(&link->eh_info, "waking up from sleep");
4895 ata_link_abort(link);
4896 return;
4897 }
4898
Jiri Slaby95364f32019-10-31 10:59:45 +01004899 qc->err_mask |= ap->ops->qc_prep(qc);
4900 if (unlikely(qc->err_mask))
4901 goto err;
Hannes Reinecke255c03d2015-03-27 16:46:38 +01004902 trace_ata_qc_issue(qc);
Tejun Heo8e0e6942006-03-31 20:41:11 +09004903 qc->err_mask |= ap->ops->qc_issue(qc);
4904 if (unlikely(qc->err_mask))
4905 goto err;
4906 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004907
Tejun Heo60f5d6e2010-08-23 11:27:27 +02004908sys_err:
Tejun Heo8e0e6942006-03-31 20:41:11 +09004909 qc->err_mask |= AC_ERR_SYSTEM;
4910err:
4911 ata_qc_complete(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004912}
4913
4914/**
Tejun Heob1c72912008-07-31 17:02:43 +09004915 * ata_phys_link_online - test whether the given link is online
Tejun Heo936fd732007-08-06 18:36:23 +09004916 * @link: ATA link to test
Tejun Heo34bf2172006-05-15 20:57:46 +09004917 *
Tejun Heo936fd732007-08-06 18:36:23 +09004918 * Test whether @link is online. Note that this function returns
4919 * 0 if online status of @link cannot be obtained, so
4920 * ata_link_online(link) != !ata_link_offline(link).
Tejun Heo34bf2172006-05-15 20:57:46 +09004921 *
4922 * LOCKING:
4923 * None.
4924 *
4925 * RETURNS:
Tejun Heob5b3fa32008-07-31 17:02:42 +09004926 * True if the port online status is available and online.
Tejun Heo34bf2172006-05-15 20:57:46 +09004927 */
Tejun Heob1c72912008-07-31 17:02:43 +09004928bool ata_phys_link_online(struct ata_link *link)
Tejun Heo34bf2172006-05-15 20:57:46 +09004929{
4930 u32 sstatus;
4931
Tejun Heo936fd732007-08-06 18:36:23 +09004932 if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0 &&
Tejun Heo9913ff8a2009-01-29 20:31:31 +09004933 ata_sstatus_online(sstatus))
Tejun Heob5b3fa32008-07-31 17:02:42 +09004934 return true;
4935 return false;
Tejun Heo34bf2172006-05-15 20:57:46 +09004936}
4937
4938/**
Tejun Heob1c72912008-07-31 17:02:43 +09004939 * ata_phys_link_offline - test whether the given link is offline
Tejun Heo936fd732007-08-06 18:36:23 +09004940 * @link: ATA link to test
Tejun Heo34bf2172006-05-15 20:57:46 +09004941 *
Tejun Heo936fd732007-08-06 18:36:23 +09004942 * Test whether @link is offline. Note that this function
4943 * returns 0 if offline status of @link cannot be obtained, so
4944 * ata_link_online(link) != !ata_link_offline(link).
Tejun Heo34bf2172006-05-15 20:57:46 +09004945 *
4946 * LOCKING:
4947 * None.
4948 *
4949 * RETURNS:
Tejun Heob5b3fa32008-07-31 17:02:42 +09004950 * True if the port offline status is available and offline.
Tejun Heo34bf2172006-05-15 20:57:46 +09004951 */
Tejun Heob1c72912008-07-31 17:02:43 +09004952bool ata_phys_link_offline(struct ata_link *link)
Tejun Heo34bf2172006-05-15 20:57:46 +09004953{
4954 u32 sstatus;
4955
Tejun Heo936fd732007-08-06 18:36:23 +09004956 if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0 &&
Tejun Heo9913ff8a2009-01-29 20:31:31 +09004957 !ata_sstatus_online(sstatus))
Tejun Heob5b3fa32008-07-31 17:02:42 +09004958 return true;
4959 return false;
Tejun Heo34bf2172006-05-15 20:57:46 +09004960}
Edward Falk0baab862005-06-02 18:17:13 -04004961
Tejun Heob1c72912008-07-31 17:02:43 +09004962/**
4963 * ata_link_online - test whether the given link is online
4964 * @link: ATA link to test
4965 *
4966 * Test whether @link is online. This is identical to
4967 * ata_phys_link_online() when there's no slave link. When
4968 * there's a slave link, this function should only be called on
4969 * the master link and will return true if any of M/S links is
4970 * online.
4971 *
4972 * LOCKING:
4973 * None.
4974 *
4975 * RETURNS:
4976 * True if the port online status is available and online.
4977 */
4978bool ata_link_online(struct ata_link *link)
4979{
4980 struct ata_link *slave = link->ap->slave_link;
4981
4982 WARN_ON(link == slave); /* shouldn't be called on slave link */
4983
4984 return ata_phys_link_online(link) ||
4985 (slave && ata_phys_link_online(slave));
4986}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01004987EXPORT_SYMBOL_GPL(ata_link_online);
Tejun Heob1c72912008-07-31 17:02:43 +09004988
4989/**
4990 * ata_link_offline - test whether the given link is offline
4991 * @link: ATA link to test
4992 *
4993 * Test whether @link is offline. This is identical to
4994 * ata_phys_link_offline() when there's no slave link. When
4995 * there's a slave link, this function should only be called on
4996 * the master link and will return true if both M/S links are
4997 * offline.
4998 *
4999 * LOCKING:
5000 * None.
5001 *
5002 * RETURNS:
5003 * True if the port offline status is available and offline.
5004 */
5005bool ata_link_offline(struct ata_link *link)
5006{
5007 struct ata_link *slave = link->ap->slave_link;
5008
5009 WARN_ON(link == slave); /* shouldn't be called on slave link */
5010
5011 return ata_phys_link_offline(link) &&
5012 (!slave || ata_phys_link_offline(slave));
5013}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01005014EXPORT_SYMBOL_GPL(ata_link_offline);
Tejun Heob1c72912008-07-31 17:02:43 +09005015
Tejun Heo6ffa01d2007-03-02 17:32:47 +09005016#ifdef CONFIG_PM
Dan Williamsbc6e7c42014-03-14 13:52:48 -07005017static void ata_port_request_pm(struct ata_port *ap, pm_message_t mesg,
5018 unsigned int action, unsigned int ehi_flags,
5019 bool async)
Tejun Heo500530f2006-07-03 16:07:27 +09005020{
Lin Ming5ef41082011-12-05 09:20:27 +08005021 struct ata_link *link;
Tejun Heo500530f2006-07-03 16:07:27 +09005022 unsigned long flags;
Tejun Heo500530f2006-07-03 16:07:27 +09005023
Lin Ming5ef41082011-12-05 09:20:27 +08005024 /* Previous resume operation might still be in
5025 * progress. Wait for PM_PENDING to clear.
5026 */
5027 if (ap->pflags & ATA_PFLAG_PM_PENDING) {
5028 ata_port_wait_eh(ap);
5029 WARN_ON(ap->pflags & ATA_PFLAG_PM_PENDING);
Tejun Heo500530f2006-07-03 16:07:27 +09005030 }
5031
Lin Ming5ef41082011-12-05 09:20:27 +08005032 /* request PM ops to EH */
5033 spin_lock_irqsave(ap->lock, flags);
5034
5035 ap->pm_mesg = mesg;
Lin Ming5ef41082011-12-05 09:20:27 +08005036 ap->pflags |= ATA_PFLAG_PM_PENDING;
5037 ata_for_each_link(link, ap, HOST_FIRST) {
5038 link->eh_info.action |= action;
5039 link->eh_info.flags |= ehi_flags;
5040 }
5041
5042 ata_port_schedule_eh(ap);
5043
5044 spin_unlock_irqrestore(ap->lock, flags);
5045
Dan Williams2fcbdcb2012-06-21 23:41:46 -07005046 if (!async) {
Lin Ming5ef41082011-12-05 09:20:27 +08005047 ata_port_wait_eh(ap);
5048 WARN_ON(ap->pflags & ATA_PFLAG_PM_PENDING);
5049 }
Tejun Heo500530f2006-07-03 16:07:27 +09005050}
5051
Dan Williamsbc6e7c42014-03-14 13:52:48 -07005052/*
5053 * On some hardware, device fails to respond after spun down for suspend. As
5054 * the device won't be used before being resumed, we don't need to touch the
5055 * device. Ask EH to skip the usual stuff and proceed directly to suspend.
5056 *
5057 * http://thread.gmane.org/gmane.linux.ide/46764
5058 */
5059static const unsigned int ata_port_suspend_ehi = ATA_EHI_QUIET
5060 | ATA_EHI_NO_AUTOPSY
5061 | ATA_EHI_NO_RECOVERY;
5062
5063static void ata_port_suspend(struct ata_port *ap, pm_message_t mesg)
Lin Ming5ef41082011-12-05 09:20:27 +08005064{
Dan Williamsbc6e7c42014-03-14 13:52:48 -07005065 ata_port_request_pm(ap, mesg, 0, ata_port_suspend_ehi, false);
Lin Ming5ef41082011-12-05 09:20:27 +08005066}
5067
Dan Williamsbc6e7c42014-03-14 13:52:48 -07005068static void ata_port_suspend_async(struct ata_port *ap, pm_message_t mesg)
5069{
5070 ata_port_request_pm(ap, mesg, 0, ata_port_suspend_ehi, true);
5071}
5072
5073static int ata_port_pm_suspend(struct device *dev)
Dan Williams2fcbdcb2012-06-21 23:41:46 -07005074{
5075 struct ata_port *ap = to_ata_port(dev);
5076
Lin Ming5ef41082011-12-05 09:20:27 +08005077 if (pm_runtime_suspended(dev))
5078 return 0;
5079
Dan Williamsbc6e7c42014-03-14 13:52:48 -07005080 ata_port_suspend(ap, PMSG_SUSPEND);
5081 return 0;
Lin Ming33574d62011-12-22 14:50:49 +08005082}
5083
Dan Williamsbc6e7c42014-03-14 13:52:48 -07005084static int ata_port_pm_freeze(struct device *dev)
Dan Williams2fcbdcb2012-06-21 23:41:46 -07005085{
5086 struct ata_port *ap = to_ata_port(dev);
5087
Dan Williamsbc6e7c42014-03-14 13:52:48 -07005088 if (pm_runtime_suspended(dev))
5089 return 0;
5090
5091 ata_port_suspend(ap, PMSG_FREEZE);
5092 return 0;
Dan Williams2fcbdcb2012-06-21 23:41:46 -07005093}
5094
Dan Williamsbc6e7c42014-03-14 13:52:48 -07005095static int ata_port_pm_poweroff(struct device *dev)
Lin Minge90b1e52011-12-22 14:50:48 +08005096{
Dan Williamsbc6e7c42014-03-14 13:52:48 -07005097 ata_port_suspend(to_ata_port(dev), PMSG_HIBERNATE);
5098 return 0;
5099}
Lin Minge90b1e52011-12-22 14:50:48 +08005100
Dan Williamsbc6e7c42014-03-14 13:52:48 -07005101static const unsigned int ata_port_resume_ehi = ATA_EHI_NO_AUTOPSY
5102 | ATA_EHI_QUIET;
Lin Minge90b1e52011-12-22 14:50:48 +08005103
Dan Williamsbc6e7c42014-03-14 13:52:48 -07005104static void ata_port_resume(struct ata_port *ap, pm_message_t mesg)
5105{
5106 ata_port_request_pm(ap, mesg, ATA_EH_RESET, ata_port_resume_ehi, false);
5107}
5108
5109static void ata_port_resume_async(struct ata_port *ap, pm_message_t mesg)
5110{
5111 ata_port_request_pm(ap, mesg, ATA_EH_RESET, ata_port_resume_ehi, true);
5112}
5113
5114static int ata_port_pm_resume(struct device *dev)
5115{
Todd Brandt200421a2014-03-14 13:52:54 -07005116 ata_port_resume_async(to_ata_port(dev), PMSG_RESUME);
Dan Williamsbc6e7c42014-03-14 13:52:48 -07005117 pm_runtime_disable(dev);
5118 pm_runtime_set_active(dev);
5119 pm_runtime_enable(dev);
5120 return 0;
Lin Minge90b1e52011-12-22 14:50:48 +08005121}
5122
Aaron Lu7e15e9b2013-01-15 17:21:04 +08005123/*
5124 * For ODDs, the upper layer will poll for media change every few seconds,
5125 * which will make it enter and leave suspend state every few seconds. And
5126 * as each suspend will cause a hard/soft reset, the gain of runtime suspend
5127 * is very little and the ODD may malfunction after constantly being reset.
5128 * So the idle callback here will not proceed to suspend if a non-ZPODD capable
5129 * ODD is attached to the port.
5130 */
Lin Ming9ee4f392011-12-05 09:20:28 +08005131static int ata_port_runtime_idle(struct device *dev)
5132{
Aaron Lu7e15e9b2013-01-15 17:21:04 +08005133 struct ata_port *ap = to_ata_port(dev);
5134 struct ata_link *link;
5135 struct ata_device *adev;
5136
5137 ata_for_each_link(link, ap, HOST_FIRST) {
5138 ata_for_each_dev(adev, link, ENABLED)
5139 if (adev->class == ATA_DEV_ATAPI &&
5140 !zpodd_dev_enabled(adev))
5141 return -EBUSY;
5142 }
5143
Rafael J. Wysocki45f0a852013-06-03 21:49:52 +02005144 return 0;
Lin Ming9ee4f392011-12-05 09:20:28 +08005145}
5146
Aaron Lua7ff60d2013-01-25 14:29:35 +08005147static int ata_port_runtime_suspend(struct device *dev)
5148{
Dan Williamsbc6e7c42014-03-14 13:52:48 -07005149 ata_port_suspend(to_ata_port(dev), PMSG_AUTO_SUSPEND);
5150 return 0;
Aaron Lua7ff60d2013-01-25 14:29:35 +08005151}
5152
5153static int ata_port_runtime_resume(struct device *dev)
5154{
Dan Williamsbc6e7c42014-03-14 13:52:48 -07005155 ata_port_resume(to_ata_port(dev), PMSG_AUTO_RESUME);
5156 return 0;
Aaron Lua7ff60d2013-01-25 14:29:35 +08005157}
5158
Lin Ming5ef41082011-12-05 09:20:27 +08005159static const struct dev_pm_ops ata_port_pm_ops = {
Dan Williamsbc6e7c42014-03-14 13:52:48 -07005160 .suspend = ata_port_pm_suspend,
5161 .resume = ata_port_pm_resume,
5162 .freeze = ata_port_pm_freeze,
5163 .thaw = ata_port_pm_resume,
5164 .poweroff = ata_port_pm_poweroff,
5165 .restore = ata_port_pm_resume,
Lin Ming9ee4f392011-12-05 09:20:28 +08005166
Aaron Lua7ff60d2013-01-25 14:29:35 +08005167 .runtime_suspend = ata_port_runtime_suspend,
5168 .runtime_resume = ata_port_runtime_resume,
Lin Ming9ee4f392011-12-05 09:20:28 +08005169 .runtime_idle = ata_port_runtime_idle,
Lin Ming5ef41082011-12-05 09:20:27 +08005170};
5171
Dan Williams2fcbdcb2012-06-21 23:41:46 -07005172/* sas ports don't participate in pm runtime management of ata_ports,
5173 * and need to resume ata devices at the domain level, not the per-port
5174 * level. sas suspend/resume is async to allow parallel port recovery
5175 * since sas has multiple ata_port instances per Scsi_Host.
5176 */
Dan Williamsbc6e7c42014-03-14 13:52:48 -07005177void ata_sas_port_suspend(struct ata_port *ap)
Dan Williams2fcbdcb2012-06-21 23:41:46 -07005178{
Dan Williamsbc6e7c42014-03-14 13:52:48 -07005179 ata_port_suspend_async(ap, PMSG_SUSPEND);
Dan Williams2fcbdcb2012-06-21 23:41:46 -07005180}
Dan Williamsbc6e7c42014-03-14 13:52:48 -07005181EXPORT_SYMBOL_GPL(ata_sas_port_suspend);
Dan Williams2fcbdcb2012-06-21 23:41:46 -07005182
Dan Williamsbc6e7c42014-03-14 13:52:48 -07005183void ata_sas_port_resume(struct ata_port *ap)
Dan Williams2fcbdcb2012-06-21 23:41:46 -07005184{
Dan Williamsbc6e7c42014-03-14 13:52:48 -07005185 ata_port_resume_async(ap, PMSG_RESUME);
Dan Williams2fcbdcb2012-06-21 23:41:46 -07005186}
Dan Williamsbc6e7c42014-03-14 13:52:48 -07005187EXPORT_SYMBOL_GPL(ata_sas_port_resume);
Dan Williams2fcbdcb2012-06-21 23:41:46 -07005188
Tejun Heo500530f2006-07-03 16:07:27 +09005189/**
Jeff Garzikcca39742006-08-24 03:19:22 -04005190 * ata_host_suspend - suspend host
5191 * @host: host to suspend
Tejun Heo500530f2006-07-03 16:07:27 +09005192 * @mesg: PM message
5193 *
Lin Ming5ef41082011-12-05 09:20:27 +08005194 * Suspend @host. Actual operation is performed by port suspend.
Tejun Heo500530f2006-07-03 16:07:27 +09005195 */
Jeff Garzikcca39742006-08-24 03:19:22 -04005196int ata_host_suspend(struct ata_host *host, pm_message_t mesg)
Tejun Heo500530f2006-07-03 16:07:27 +09005197{
Lin Ming5ef41082011-12-05 09:20:27 +08005198 host->dev->power.power_state = mesg;
5199 return 0;
Tejun Heo500530f2006-07-03 16:07:27 +09005200}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01005201EXPORT_SYMBOL_GPL(ata_host_suspend);
Tejun Heo500530f2006-07-03 16:07:27 +09005202
5203/**
Jeff Garzikcca39742006-08-24 03:19:22 -04005204 * ata_host_resume - resume host
5205 * @host: host to resume
Tejun Heo500530f2006-07-03 16:07:27 +09005206 *
Lin Ming5ef41082011-12-05 09:20:27 +08005207 * Resume @host. Actual operation is performed by port resume.
Tejun Heo500530f2006-07-03 16:07:27 +09005208 */
Jeff Garzikcca39742006-08-24 03:19:22 -04005209void ata_host_resume(struct ata_host *host)
Tejun Heo500530f2006-07-03 16:07:27 +09005210{
Jeff Garzik72ad6ec2008-02-25 17:31:10 -05005211 host->dev->power.power_state = PMSG_ON;
Tejun Heo500530f2006-07-03 16:07:27 +09005212}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01005213EXPORT_SYMBOL_GPL(ata_host_resume);
Tejun Heo6ffa01d2007-03-02 17:32:47 +09005214#endif
Tejun Heo500530f2006-07-03 16:07:27 +09005215
Bhumika Goyal8df82c12017-09-30 22:10:40 +05305216const struct device_type ata_port_type = {
Lin Ming5ef41082011-12-05 09:20:27 +08005217 .name = "ata_port",
5218#ifdef CONFIG_PM
5219 .pm = &ata_port_pm_ops,
5220#endif
5221};
5222
Randy Dunlapc893a3a2006-01-28 13:15:32 -05005223/**
Tejun Heo3ef3b432006-05-31 18:27:30 +09005224 * ata_dev_init - Initialize an ata_device structure
5225 * @dev: Device structure to initialize
5226 *
5227 * Initialize @dev in preparation for probing.
5228 *
5229 * LOCKING:
5230 * Inherited from caller.
5231 */
5232void ata_dev_init(struct ata_device *dev)
5233{
Tejun Heob1c72912008-07-31 17:02:43 +09005234 struct ata_link *link = ata_dev_phys_link(dev);
Tejun Heo9af5c9c2007-08-06 18:36:22 +09005235 struct ata_port *ap = link->ap;
Tejun Heo72fa4b72006-05-31 18:27:32 +09005236 unsigned long flags;
Tejun Heo3ef3b432006-05-31 18:27:30 +09005237
Tejun Heob1c72912008-07-31 17:02:43 +09005238 /* SATA spd limit is bound to the attached device, reset together */
Tejun Heo9af5c9c2007-08-06 18:36:22 +09005239 link->sata_spd_limit = link->hw_sata_spd_limit;
5240 link->sata_spd = 0;
Tejun Heo5a04bf42006-05-31 18:27:38 +09005241
Tejun Heo72fa4b72006-05-31 18:27:32 +09005242 /* High bits of dev->flags are used to record warm plug
5243 * requests which occur asynchronously. Synchronize using
Jeff Garzikcca39742006-08-24 03:19:22 -04005244 * host lock.
Tejun Heo72fa4b72006-05-31 18:27:32 +09005245 */
Jeff Garzikba6a1302006-06-22 23:46:10 -04005246 spin_lock_irqsave(ap->lock, flags);
Tejun Heo72fa4b72006-05-31 18:27:32 +09005247 dev->flags &= ~ATA_DFLAG_INIT_MASK;
Tejun Heo3dcc3232007-09-03 12:20:11 +09005248 dev->horkage = 0;
Jeff Garzikba6a1302006-06-22 23:46:10 -04005249 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heo72fa4b72006-05-31 18:27:32 +09005250
Tejun Heo99cf6102009-01-29 20:31:32 +09005251 memset((void *)dev + ATA_DEVICE_CLEAR_BEGIN, 0,
5252 ATA_DEVICE_CLEAR_END - ATA_DEVICE_CLEAR_BEGIN);
Tejun Heo3ef3b432006-05-31 18:27:30 +09005253 dev->pio_mask = UINT_MAX;
5254 dev->mwdma_mask = UINT_MAX;
5255 dev->udma_mask = UINT_MAX;
5256}
5257
5258/**
Tejun Heo4fb37a22007-08-06 18:36:23 +09005259 * ata_link_init - Initialize an ata_link structure
5260 * @ap: ATA port link is attached to
5261 * @link: Link structure to initialize
Tejun Heo89898052007-08-06 18:36:23 +09005262 * @pmp: Port multiplier port number
Tejun Heo4fb37a22007-08-06 18:36:23 +09005263 *
5264 * Initialize @link.
5265 *
5266 * LOCKING:
5267 * Kernel thread context (may sleep)
5268 */
Tejun Heofb7fd612007-09-23 13:14:12 +09005269void ata_link_init(struct ata_port *ap, struct ata_link *link, int pmp)
Tejun Heo4fb37a22007-08-06 18:36:23 +09005270{
5271 int i;
5272
5273 /* clear everything except for devices */
Gwendal Grignoud9027472010-05-25 12:31:38 -07005274 memset((void *)link + ATA_LINK_CLEAR_BEGIN, 0,
5275 ATA_LINK_CLEAR_END - ATA_LINK_CLEAR_BEGIN);
Tejun Heo4fb37a22007-08-06 18:36:23 +09005276
5277 link->ap = ap;
Tejun Heo89898052007-08-06 18:36:23 +09005278 link->pmp = pmp;
Tejun Heo4fb37a22007-08-06 18:36:23 +09005279 link->active_tag = ATA_TAG_POISON;
5280 link->hw_sata_spd_limit = UINT_MAX;
5281
5282 /* can't use iterator, ap isn't initialized yet */
5283 for (i = 0; i < ATA_MAX_DEVICES; i++) {
5284 struct ata_device *dev = &link->device[i];
5285
5286 dev->link = link;
5287 dev->devno = dev - link->device;
Tejun Heo110f66d2009-09-16 04:17:28 +09005288#ifdef CONFIG_ATA_ACPI
5289 dev->gtf_filter = ata_acpi_gtf_filter;
5290#endif
Tejun Heo4fb37a22007-08-06 18:36:23 +09005291 ata_dev_init(dev);
5292 }
5293}
5294
5295/**
5296 * sata_link_init_spd - Initialize link->sata_spd_limit
5297 * @link: Link to configure sata_spd_limit for
5298 *
Mauro Carvalho Chehaba31a6992020-04-14 18:48:55 +02005299 * Initialize ``link->[hw_]sata_spd_limit`` to the currently
Tejun Heo4fb37a22007-08-06 18:36:23 +09005300 * configured value.
5301 *
5302 * LOCKING:
5303 * Kernel thread context (may sleep).
5304 *
5305 * RETURNS:
5306 * 0 on success, -errno on failure.
5307 */
Tejun Heofb7fd612007-09-23 13:14:12 +09005308int sata_link_init_spd(struct ata_link *link)
Tejun Heo4fb37a22007-08-06 18:36:23 +09005309{
Tejun Heo33267322008-02-13 09:15:09 +09005310 u8 spd;
Tejun Heo4fb37a22007-08-06 18:36:23 +09005311 int rc;
5312
Tejun Heod127ea72008-07-31 16:09:34 +09005313 rc = sata_scr_read(link, SCR_CONTROL, &link->saved_scontrol);
Tejun Heo4fb37a22007-08-06 18:36:23 +09005314 if (rc)
5315 return rc;
5316
Tejun Heod127ea72008-07-31 16:09:34 +09005317 spd = (link->saved_scontrol >> 4) & 0xf;
Tejun Heo4fb37a22007-08-06 18:36:23 +09005318 if (spd)
5319 link->hw_sata_spd_limit &= (1 << spd) - 1;
5320
Tejun Heo05944bd2008-08-13 20:19:09 +09005321 ata_force_link_limits(link);
Tejun Heo33267322008-02-13 09:15:09 +09005322
Tejun Heo4fb37a22007-08-06 18:36:23 +09005323 link->sata_spd_limit = link->hw_sata_spd_limit;
5324
5325 return 0;
5326}
5327
5328/**
Tejun Heof3187192007-04-17 23:44:07 +09005329 * ata_port_alloc - allocate and initialize basic ATA port resources
5330 * @host: ATA host this allocated port belongs to
Linus Torvalds1da177e2005-04-16 15:20:36 -07005331 *
Tejun Heof3187192007-04-17 23:44:07 +09005332 * Allocate and initialize basic ATA port resources.
5333 *
5334 * RETURNS:
5335 * Allocate ATA port on success, NULL on failure.
Jeff Garzik0cba6322005-05-30 19:49:12 -04005336 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07005337 * LOCKING:
Tejun Heof3187192007-04-17 23:44:07 +09005338 * Inherited from calling layer (may sleep).
Linus Torvalds1da177e2005-04-16 15:20:36 -07005339 */
Tejun Heof3187192007-04-17 23:44:07 +09005340struct ata_port *ata_port_alloc(struct ata_host *host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005341{
Tejun Heof3187192007-04-17 23:44:07 +09005342 struct ata_port *ap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005343
Tejun Heof3187192007-04-17 23:44:07 +09005344 ap = kzalloc(sizeof(*ap), GFP_KERNEL);
5345 if (!ap)
5346 return NULL;
Jeff Garzik4fca3772011-02-15 01:13:24 -05005347
Maxime Bizon7b3a24c52011-03-16 14:58:32 +01005348 ap->pflags |= ATA_PFLAG_INITIALIZING | ATA_PFLAG_FROZEN;
Jeff Garzikcca39742006-08-24 03:19:22 -04005349 ap->lock = &host->lock;
Tejun Heof3187192007-04-17 23:44:07 +09005350 ap->print_id = -1;
David Milburne628dc92013-05-14 13:48:40 -05005351 ap->local_port_no = -1;
Jeff Garzikcca39742006-08-24 03:19:22 -04005352 ap->host = host;
Tejun Heof3187192007-04-17 23:44:07 +09005353 ap->dev = host->dev;
Borislav Petkovbd5d8252006-06-11 23:17:01 -04005354
5355#if defined(ATA_VERBOSE_DEBUG)
5356 /* turn on all debugging levels */
5357 ap->msg_enable = 0x00FF;
5358#elif defined(ATA_DEBUG)
5359 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 +09005360#else
Borislav Petkov0dd4b212006-06-23 02:29:08 -04005361 ap->msg_enable = ATA_MSG_DRV | ATA_MSG_ERR | ATA_MSG_WARN;
Borislav Petkovbd5d8252006-06-11 23:17:01 -04005362#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005363
Tejun Heoad72cf92010-07-02 10:03:52 +02005364 mutex_init(&ap->scsi_scan_mutex);
David Howells65f27f32006-11-22 14:55:48 +00005365 INIT_DELAYED_WORK(&ap->hotplug_task, ata_scsi_hotplug);
5366 INIT_WORK(&ap->scsi_rescan_task, ata_scsi_dev_rescan);
Tejun Heoa72ec4c2006-01-23 13:09:37 +09005367 INIT_LIST_HEAD(&ap->eh_done_q);
Tejun Heoc6cf9e92006-05-31 18:27:27 +09005368 init_waitqueue_head(&ap->eh_wait_q);
Elias Oltmanns45fabbb2008-09-21 11:54:08 +02005369 init_completion(&ap->park_req_pending);
Kees Cookb93ab332017-10-16 14:56:42 -07005370 timer_setup(&ap->fastdrain_timer, ata_eh_fastdrain_timerfn,
5371 TIMER_DEFERRABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005372
Tejun Heo838df622006-05-15 20:57:44 +09005373 ap->cbl = ATA_CBL_NONE;
Tejun Heo838df622006-05-15 20:57:44 +09005374
Tejun Heo89898052007-08-06 18:36:23 +09005375 ata_link_init(ap, &ap->link, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005376
5377#ifdef ATA_IRQ_TRAP
5378 ap->stats.unhandled_irq = 1;
5379 ap->stats.idle_irq = 1;
5380#endif
Tejun Heo270390e2010-05-10 21:41:35 +02005381 ata_sff_port_init(ap);
5382
Linus Torvalds1da177e2005-04-16 15:20:36 -07005383 return ap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005384}
5385
Taras Kondratiuk2623c7a2018-03-09 08:34:41 +00005386static void ata_devres_release(struct device *gendev, void *res)
Tejun Heof0d36ef2007-01-20 16:00:28 +09005387{
5388 struct ata_host *host = dev_get_drvdata(gendev);
5389 int i;
5390
5391 for (i = 0; i < host->n_ports; i++) {
5392 struct ata_port *ap = host->ports[i];
5393
Tejun Heoecef7252007-04-17 23:44:06 +09005394 if (!ap)
5395 continue;
5396
Tejun Heo49114872007-04-17 23:44:06 +09005397 if (ap->scsi_host)
Tejun Heo1aa506e42007-03-09 19:36:12 +09005398 scsi_host_put(ap->scsi_host);
5399
Taras Kondratiuk2623c7a2018-03-09 08:34:41 +00005400 }
5401
5402 dev_set_drvdata(gendev, NULL);
5403 ata_host_put(host);
5404}
5405
5406static void ata_host_release(struct kref *kref)
5407{
5408 struct ata_host *host = container_of(kref, struct ata_host, kref);
5409 int i;
5410
5411 for (i = 0; i < host->n_ports; i++) {
5412 struct ata_port *ap = host->ports[i];
5413
Tejun Heo633273a2007-09-23 13:19:54 +09005414 kfree(ap->pmp_link);
Tejun Heob1c72912008-07-31 17:02:43 +09005415 kfree(ap->slave_link);
Tejun Heo49114872007-04-17 23:44:06 +09005416 kfree(ap);
Tejun Heo1aa506e42007-03-09 19:36:12 +09005417 host->ports[i] = NULL;
5418 }
Taras Kondratiuk2623c7a2018-03-09 08:34:41 +00005419 kfree(host);
5420}
Tejun Heo1aa506e42007-03-09 19:36:12 +09005421
Taras Kondratiuk2623c7a2018-03-09 08:34:41 +00005422void ata_host_get(struct ata_host *host)
5423{
5424 kref_get(&host->kref);
5425}
5426
5427void ata_host_put(struct ata_host *host)
5428{
5429 kref_put(&host->kref, ata_host_release);
Tejun Heof0d36ef2007-01-20 16:00:28 +09005430}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01005431EXPORT_SYMBOL_GPL(ata_host_put);
Tejun Heof0d36ef2007-01-20 16:00:28 +09005432
Linus Torvalds1da177e2005-04-16 15:20:36 -07005433/**
Tejun Heof3187192007-04-17 23:44:07 +09005434 * ata_host_alloc - allocate and init basic ATA host resources
5435 * @dev: generic device this host is associated with
5436 * @max_ports: maximum number of ATA ports associated with this host
5437 *
5438 * Allocate and initialize basic ATA host resources. LLD calls
5439 * this function to allocate a host, initializes it fully and
5440 * attaches it using ata_host_register().
5441 *
5442 * @max_ports ports are allocated and host->n_ports is
5443 * initialized to @max_ports. The caller is allowed to decrease
5444 * host->n_ports before calling ata_host_register(). The unused
5445 * ports will be automatically freed on registration.
5446 *
5447 * RETURNS:
5448 * Allocate ATA host on success, NULL on failure.
5449 *
5450 * LOCKING:
5451 * Inherited from calling layer (may sleep).
5452 */
5453struct ata_host *ata_host_alloc(struct device *dev, int max_ports)
5454{
5455 struct ata_host *host;
5456 size_t sz;
5457 int i;
Taras Kondratiuk2623c7a2018-03-09 08:34:41 +00005458 void *dr;
Tejun Heof3187192007-04-17 23:44:07 +09005459
Tejun Heof3187192007-04-17 23:44:07 +09005460 /* alloc a container for our list of ATA ports (buses) */
5461 sz = sizeof(struct ata_host) + (max_ports + 1) * sizeof(void *);
Taras Kondratiuk2623c7a2018-03-09 08:34:41 +00005462 host = kzalloc(sz, GFP_KERNEL);
Tejun Heof3187192007-04-17 23:44:07 +09005463 if (!host)
Taras Kondratiuk2623c7a2018-03-09 08:34:41 +00005464 return NULL;
5465
Tejun Heof3187192007-04-17 23:44:07 +09005466 if (!devres_open_group(dev, NULL, GFP_KERNEL))
Colin Ian Kingdafd6c42018-03-27 14:26:01 +01005467 goto err_free;
Tejun Heof3187192007-04-17 23:44:07 +09005468
Taras Kondratiuk2623c7a2018-03-09 08:34:41 +00005469 dr = devres_alloc(ata_devres_release, 0, GFP_KERNEL);
5470 if (!dr)
Tejun Heof3187192007-04-17 23:44:07 +09005471 goto err_out;
5472
Taras Kondratiuk2623c7a2018-03-09 08:34:41 +00005473 devres_add(dev, dr);
Tejun Heof3187192007-04-17 23:44:07 +09005474 dev_set_drvdata(dev, host);
5475
5476 spin_lock_init(&host->lock);
Tejun Heoc0c362b2010-09-06 17:57:14 +02005477 mutex_init(&host->eh_mutex);
Tejun Heof3187192007-04-17 23:44:07 +09005478 host->dev = dev;
5479 host->n_ports = max_ports;
Taras Kondratiuk2623c7a2018-03-09 08:34:41 +00005480 kref_init(&host->kref);
Tejun Heof3187192007-04-17 23:44:07 +09005481
5482 /* allocate ports bound to this host */
5483 for (i = 0; i < max_ports; i++) {
5484 struct ata_port *ap;
5485
5486 ap = ata_port_alloc(host);
5487 if (!ap)
5488 goto err_out;
5489
5490 ap->port_no = i;
5491 host->ports[i] = ap;
5492 }
5493
5494 devres_remove_group(dev, NULL);
5495 return host;
5496
5497 err_out:
5498 devres_release_group(dev, NULL);
Colin Ian Kingdafd6c42018-03-27 14:26:01 +01005499 err_free:
5500 kfree(host);
Tejun Heof3187192007-04-17 23:44:07 +09005501 return NULL;
5502}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01005503EXPORT_SYMBOL_GPL(ata_host_alloc);
Tejun Heof3187192007-04-17 23:44:07 +09005504
5505/**
Tejun Heof5cda252007-04-17 23:44:07 +09005506 * ata_host_alloc_pinfo - alloc host and init with port_info array
5507 * @dev: generic device this host is associated with
5508 * @ppi: array of ATA port_info to initialize host with
5509 * @n_ports: number of ATA ports attached to this host
5510 *
5511 * Allocate ATA host and initialize with info from @ppi. If NULL
5512 * terminated, @ppi may contain fewer entries than @n_ports. The
5513 * last entry will be used for the remaining ports.
5514 *
5515 * RETURNS:
5516 * Allocate ATA host on success, NULL on failure.
5517 *
5518 * LOCKING:
5519 * Inherited from calling layer (may sleep).
5520 */
5521struct ata_host *ata_host_alloc_pinfo(struct device *dev,
5522 const struct ata_port_info * const * ppi,
5523 int n_ports)
5524{
5525 const struct ata_port_info *pi;
5526 struct ata_host *host;
5527 int i, j;
5528
5529 host = ata_host_alloc(dev, n_ports);
5530 if (!host)
5531 return NULL;
5532
5533 for (i = 0, j = 0, pi = NULL; i < host->n_ports; i++) {
5534 struct ata_port *ap = host->ports[i];
5535
5536 if (ppi[j])
5537 pi = ppi[j++];
5538
5539 ap->pio_mask = pi->pio_mask;
5540 ap->mwdma_mask = pi->mwdma_mask;
5541 ap->udma_mask = pi->udma_mask;
5542 ap->flags |= pi->flags;
Tejun Heo0c887582007-08-06 18:36:23 +09005543 ap->link.flags |= pi->link_flags;
Tejun Heof5cda252007-04-17 23:44:07 +09005544 ap->ops = pi->port_ops;
5545
5546 if (!host->ops && (pi->port_ops != &ata_dummy_port_ops))
5547 host->ops = pi->port_ops;
Tejun Heof5cda252007-04-17 23:44:07 +09005548 }
5549
5550 return host;
5551}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01005552EXPORT_SYMBOL_GPL(ata_host_alloc_pinfo);
Tejun Heof5cda252007-04-17 23:44:07 +09005553
Tejun Heo32ebbc02007-11-08 13:09:00 +09005554static void ata_host_stop(struct device *gendev, void *res)
5555{
5556 struct ata_host *host = dev_get_drvdata(gendev);
5557 int i;
5558
5559 WARN_ON(!(host->flags & ATA_HOST_STARTED));
5560
5561 for (i = 0; i < host->n_ports; i++) {
5562 struct ata_port *ap = host->ports[i];
5563
5564 if (ap->ops->port_stop)
5565 ap->ops->port_stop(ap);
5566 }
5567
5568 if (host->ops->host_stop)
5569 host->ops->host_stop(host);
5570}
5571
Tejun Heof5cda252007-04-17 23:44:07 +09005572/**
Tejun Heo029cfd62008-03-25 12:22:49 +09005573 * ata_finalize_port_ops - finalize ata_port_operations
5574 * @ops: ata_port_operations to finalize
5575 *
5576 * An ata_port_operations can inherit from another ops and that
5577 * ops can again inherit from another. This can go on as many
5578 * times as necessary as long as there is no loop in the
5579 * inheritance chain.
5580 *
5581 * Ops tables are finalized when the host is started. NULL or
5582 * unspecified entries are inherited from the closet ancestor
5583 * which has the method and the entry is populated with it.
5584 * After finalization, the ops table directly points to all the
5585 * methods and ->inherits is no longer necessary and cleared.
5586 *
5587 * Using ATA_OP_NULL, inheriting ops can force a method to NULL.
5588 *
5589 * LOCKING:
5590 * None.
5591 */
5592static void ata_finalize_port_ops(struct ata_port_operations *ops)
5593{
Pradeep Singh Rautela2da67652008-05-29 23:28:14 +05305594 static DEFINE_SPINLOCK(lock);
Tejun Heo029cfd62008-03-25 12:22:49 +09005595 const struct ata_port_operations *cur;
5596 void **begin = (void **)ops;
5597 void **end = (void **)&ops->inherits;
5598 void **pp;
5599
5600 if (!ops || !ops->inherits)
5601 return;
5602
5603 spin_lock(&lock);
5604
5605 for (cur = ops->inherits; cur; cur = cur->inherits) {
5606 void **inherit = (void **)cur;
5607
5608 for (pp = begin; pp < end; pp++, inherit++)
5609 if (!*pp)
5610 *pp = *inherit;
5611 }
5612
5613 for (pp = begin; pp < end; pp++)
5614 if (IS_ERR(*pp))
5615 *pp = NULL;
5616
5617 ops->inherits = NULL;
5618
5619 spin_unlock(&lock);
5620}
5621
5622/**
Tejun Heoecef7252007-04-17 23:44:06 +09005623 * ata_host_start - start and freeze ports of an ATA host
5624 * @host: ATA host to start ports for
5625 *
5626 * Start and then freeze ports of @host. Started status is
5627 * recorded in host->flags, so this function can be called
5628 * multiple times. Ports are guaranteed to get started only
Tejun Heof3187192007-04-17 23:44:07 +09005629 * once. If host->ops isn't initialized yet, its set to the
5630 * first non-dummy port ops.
Tejun Heoecef7252007-04-17 23:44:06 +09005631 *
5632 * LOCKING:
5633 * Inherited from calling layer (may sleep).
5634 *
5635 * RETURNS:
5636 * 0 if all ports are started successfully, -errno otherwise.
5637 */
5638int ata_host_start(struct ata_host *host)
5639{
Tejun Heo32ebbc02007-11-08 13:09:00 +09005640 int have_stop = 0;
5641 void *start_dr = NULL;
Tejun Heoecef7252007-04-17 23:44:06 +09005642 int i, rc;
5643
5644 if (host->flags & ATA_HOST_STARTED)
5645 return 0;
5646
Tejun Heo029cfd62008-03-25 12:22:49 +09005647 ata_finalize_port_ops(host->ops);
5648
Tejun Heoecef7252007-04-17 23:44:06 +09005649 for (i = 0; i < host->n_ports; i++) {
5650 struct ata_port *ap = host->ports[i];
5651
Tejun Heo029cfd62008-03-25 12:22:49 +09005652 ata_finalize_port_ops(ap->ops);
5653
Tejun Heof3187192007-04-17 23:44:07 +09005654 if (!host->ops && !ata_port_is_dummy(ap))
5655 host->ops = ap->ops;
5656
Tejun Heo32ebbc02007-11-08 13:09:00 +09005657 if (ap->ops->port_stop)
5658 have_stop = 1;
5659 }
5660
Damien Le Moal355a8032021-08-16 10:44:47 +09005661 if (host->ops && host->ops->host_stop)
Tejun Heo32ebbc02007-11-08 13:09:00 +09005662 have_stop = 1;
5663
5664 if (have_stop) {
5665 start_dr = devres_alloc(ata_host_stop, 0, GFP_KERNEL);
5666 if (!start_dr)
5667 return -ENOMEM;
5668 }
5669
5670 for (i = 0; i < host->n_ports; i++) {
5671 struct ata_port *ap = host->ports[i];
5672
Tejun Heoecef7252007-04-17 23:44:06 +09005673 if (ap->ops->port_start) {
5674 rc = ap->ops->port_start(ap);
5675 if (rc) {
Alan Cox0f9fe9b2007-11-30 15:23:16 +00005676 if (rc != -ENODEV)
Joe Perchesa44fec12011-04-15 15:51:58 -07005677 dev_err(host->dev,
5678 "failed to start port %d (errno=%d)\n",
5679 i, rc);
Tejun Heoecef7252007-04-17 23:44:06 +09005680 goto err_out;
5681 }
5682 }
Tejun Heoecef7252007-04-17 23:44:06 +09005683 ata_eh_freeze_port(ap);
5684 }
5685
Tejun Heo32ebbc02007-11-08 13:09:00 +09005686 if (start_dr)
5687 devres_add(host->dev, start_dr);
Tejun Heoecef7252007-04-17 23:44:06 +09005688 host->flags |= ATA_HOST_STARTED;
5689 return 0;
5690
5691 err_out:
5692 while (--i >= 0) {
5693 struct ata_port *ap = host->ports[i];
5694
5695 if (ap->ops->port_stop)
5696 ap->ops->port_stop(ap);
5697 }
Tejun Heo32ebbc02007-11-08 13:09:00 +09005698 devres_free(start_dr);
Tejun Heoecef7252007-04-17 23:44:06 +09005699 return rc;
5700}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01005701EXPORT_SYMBOL_GPL(ata_host_start);
Tejun Heoecef7252007-04-17 23:44:06 +09005702
5703/**
Mauro Carvalho Chehab94bd5712020-10-23 18:32:55 +02005704 * ata_host_init - Initialize a host struct for sas (ipr, libsas)
Jeff Garzikcca39742006-08-24 03:19:22 -04005705 * @host: host to initialize
5706 * @dev: device host is attached to
Jeff Garzikcca39742006-08-24 03:19:22 -04005707 * @ops: port_ops
Brian Kingb03732f2006-08-07 14:27:10 -05005708 *
Brian Kingb03732f2006-08-07 14:27:10 -05005709 */
Jeff Garzikcca39742006-08-24 03:19:22 -04005710void ata_host_init(struct ata_host *host, struct device *dev,
Dan Williams8d8e7d12012-07-09 21:06:08 -07005711 struct ata_port_operations *ops)
Brian Kingb03732f2006-08-07 14:27:10 -05005712{
Jeff Garzikcca39742006-08-24 03:19:22 -04005713 spin_lock_init(&host->lock);
Tejun Heoc0c362b2010-09-06 17:57:14 +02005714 mutex_init(&host->eh_mutex);
Jens Axboe69278f72018-05-11 12:51:10 -06005715 host->n_tags = ATA_MAX_QUEUE;
Jeff Garzikcca39742006-08-24 03:19:22 -04005716 host->dev = dev;
Jeff Garzikcca39742006-08-24 03:19:22 -04005717 host->ops = ops;
Jason Yan2fa4a322018-05-10 11:05:16 +08005718 kref_init(&host->kref);
Brian Kingb03732f2006-08-07 14:27:10 -05005719}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01005720EXPORT_SYMBOL_GPL(ata_host_init);
Brian Kingb03732f2006-08-07 14:27:10 -05005721
Dan Williams9508a662012-01-18 20:47:01 -08005722void __ata_port_probe(struct ata_port *ap)
5723{
5724 struct ata_eh_info *ehi = &ap->link.eh_info;
5725 unsigned long flags;
5726
5727 /* kick EH for boot probing */
5728 spin_lock_irqsave(ap->lock, flags);
5729
5730 ehi->probe_mask |= ATA_ALL_DEVICES;
5731 ehi->action |= ATA_EH_RESET;
5732 ehi->flags |= ATA_EHI_NO_AUTOPSY | ATA_EHI_QUIET;
5733
5734 ap->pflags &= ~ATA_PFLAG_INITIALIZING;
5735 ap->pflags |= ATA_PFLAG_LOADING;
5736 ata_port_schedule_eh(ap);
5737
5738 spin_unlock_irqrestore(ap->lock, flags);
5739}
5740
James Bottomley238c9cf2011-01-23 08:28:33 -06005741int ata_port_probe(struct ata_port *ap)
Arjan van de Ven79318052009-01-04 05:32:28 -08005742{
James Bottomley238c9cf2011-01-23 08:28:33 -06005743 int rc = 0;
Arjan van de Ven886ad092009-01-09 15:54:07 -08005744
Arjan van de Ven79318052009-01-04 05:32:28 -08005745 if (ap->ops->error_handler) {
Dan Williams9508a662012-01-18 20:47:01 -08005746 __ata_port_probe(ap);
Arjan van de Ven79318052009-01-04 05:32:28 -08005747 ata_port_wait_eh(ap);
5748 } else {
Arjan van de Ven79318052009-01-04 05:32:28 -08005749 rc = ata_bus_probe(ap);
Arjan van de Ven79318052009-01-04 05:32:28 -08005750 }
James Bottomley238c9cf2011-01-23 08:28:33 -06005751 return rc;
5752}
5753
5754
5755static void async_port_probe(void *data, async_cookie_t cookie)
5756{
5757 struct ata_port *ap = data;
Jeff Garzik4fca3772011-02-15 01:13:24 -05005758
James Bottomley238c9cf2011-01-23 08:28:33 -06005759 /*
5760 * If we're not allowed to scan this host in parallel,
5761 * we need to wait until all previous scans have completed
5762 * before going further.
5763 * Jeff Garzik says this is only within a controller, so we
5764 * don't need to wait for port 0, only for later ports.
5765 */
5766 if (!(ap->host->flags & ATA_HOST_PARALLEL_SCAN) && ap->port_no != 0)
5767 async_synchronize_cookie(cookie);
5768
5769 (void)ata_port_probe(ap);
Arjan van de Venf29d3b22009-01-05 15:07:07 -08005770
5771 /* in order to keep device order, we need to synchronize at this point */
5772 async_synchronize_cookie(cookie);
5773
5774 ata_scsi_scan_host(ap, 1);
Arjan van de Ven79318052009-01-04 05:32:28 -08005775}
James Bottomley238c9cf2011-01-23 08:28:33 -06005776
Brian Kingb03732f2006-08-07 14:27:10 -05005777/**
Tejun Heof3187192007-04-17 23:44:07 +09005778 * ata_host_register - register initialized ATA host
5779 * @host: ATA host to register
5780 * @sht: template for SCSI host
Jeff Garzik0cba6322005-05-30 19:49:12 -04005781 *
Tejun Heof3187192007-04-17 23:44:07 +09005782 * Register initialized ATA host. @host is allocated using
5783 * ata_host_alloc() and fully initialized by LLD. This function
5784 * starts ports, registers @host with ATA and SCSI layers and
5785 * probe registered devices.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005786 *
5787 * LOCKING:
Tejun Heof3187192007-04-17 23:44:07 +09005788 * Inherited from calling layer (may sleep).
Linus Torvalds1da177e2005-04-16 15:20:36 -07005789 *
5790 * RETURNS:
Tejun Heof3187192007-04-17 23:44:07 +09005791 * 0 on success, -errno otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005792 */
Tejun Heof3187192007-04-17 23:44:07 +09005793int ata_host_register(struct ata_host *host, struct scsi_host_template *sht)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005794{
Tejun Heof3187192007-04-17 23:44:07 +09005795 int i, rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005796
Jens Axboe69278f72018-05-11 12:51:10 -06005797 host->n_tags = clamp(sht->can_queue, 1, ATA_MAX_QUEUE);
Kevin Hao1871ee12014-07-12 12:08:24 +08005798
Tejun Heof3187192007-04-17 23:44:07 +09005799 /* host must have been started */
5800 if (!(host->flags & ATA_HOST_STARTED)) {
Joe Perchesa44fec12011-04-15 15:51:58 -07005801 dev_err(host->dev, "BUG: trying to register unstarted host\n");
Tejun Heof3187192007-04-17 23:44:07 +09005802 WARN_ON(1);
5803 return -EINVAL;
Alan Cox02f076a2006-09-26 17:35:32 +01005804 }
Tejun Heof0d36ef2007-01-20 16:00:28 +09005805
Tejun Heof3187192007-04-17 23:44:07 +09005806 /* Blow away unused ports. This happens when LLD can't
5807 * determine the exact number of ports to allocate at
5808 * allocation time.
5809 */
5810 for (i = host->n_ports; host->ports[i]; i++)
5811 kfree(host->ports[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005812
Tejun Heof3187192007-04-17 23:44:07 +09005813 /* give ports names and add SCSI hosts */
David Milburne628dc92013-05-14 13:48:40 -05005814 for (i = 0; i < host->n_ports; i++) {
Dan Williams85d67252012-03-10 23:28:46 -08005815 host->ports[i]->print_id = atomic_inc_return(&ata_print_id);
David Milburne628dc92013-05-14 13:48:40 -05005816 host->ports[i]->local_port_no = i + 1;
5817 }
Jeff Garzik4fca3772011-02-15 01:13:24 -05005818
Gwendal Grignoud9027472010-05-25 12:31:38 -07005819 /* Create associated sysfs transport objects */
5820 for (i = 0; i < host->n_ports; i++) {
5821 rc = ata_tport_add(host->dev,host->ports[i]);
5822 if (rc) {
5823 goto err_tadd;
5824 }
5825 }
5826
Tejun Heof3187192007-04-17 23:44:07 +09005827 rc = ata_scsi_add_hosts(host, sht);
Tejun Heoecef7252007-04-17 23:44:06 +09005828 if (rc)
Gwendal Grignoud9027472010-05-25 12:31:38 -07005829 goto err_tadd;
Tejun Heoecef7252007-04-17 23:44:06 +09005830
Tejun Heof3187192007-04-17 23:44:07 +09005831 /* set cable, sata_spd_limit and report */
Jeff Garzikcca39742006-08-24 03:19:22 -04005832 for (i = 0; i < host->n_ports; i++) {
5833 struct ata_port *ap = host->ports[i];
Tejun Heof3187192007-04-17 23:44:07 +09005834 unsigned long xfer_mask;
5835
5836 /* set SATA cable type if still unset */
5837 if (ap->cbl == ATA_CBL_NONE && (ap->flags & ATA_FLAG_SATA))
5838 ap->cbl = ATA_CBL_SATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005839
Tejun Heo5a04bf42006-05-31 18:27:38 +09005840 /* init sata_spd_limit to the current value */
Tejun Heo4fb37a22007-08-06 18:36:23 +09005841 sata_link_init_spd(&ap->link);
Tejun Heob1c72912008-07-31 17:02:43 +09005842 if (ap->slave_link)
5843 sata_link_init_spd(ap->slave_link);
Tejun Heo5a04bf42006-05-31 18:27:38 +09005844
Tejun Heocbcdd872007-08-18 13:14:55 +09005845 /* print per-port info to dmesg */
Tejun Heof3187192007-04-17 23:44:07 +09005846 xfer_mask = ata_pack_xfermask(ap->pio_mask, ap->mwdma_mask,
5847 ap->udma_mask);
5848
Tejun Heoabf6e8e2007-10-09 14:57:25 +09005849 if (!ata_port_is_dummy(ap)) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07005850 ata_port_info(ap, "%cATA max %s %s\n",
5851 (ap->flags & ATA_FLAG_SATA) ? 'S' : 'P',
5852 ata_mode_string(xfer_mask),
5853 ap->link.eh_info.desc);
Tejun Heoabf6e8e2007-10-09 14:57:25 +09005854 ata_ehi_clear_desc(&ap->link.eh_info);
5855 } else
Joe Perchesa9a79df2011-04-15 15:51:59 -07005856 ata_port_info(ap, "DUMMY\n");
Tejun Heof3187192007-04-17 23:44:07 +09005857 }
5858
Vegard Nossumf6005352009-04-08 18:19:39 +02005859 /* perform each probe asynchronously */
Tejun Heof3187192007-04-17 23:44:07 +09005860 for (i = 0; i < host->n_ports; i++) {
5861 struct ata_port *ap = host->ports[i];
Kai-Heng Fengb5292112020-06-03 15:48:19 +08005862 ap->cookie = async_schedule(async_port_probe, ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005863 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005864
Tejun Heof3187192007-04-17 23:44:07 +09005865 return 0;
Gwendal Grignoud9027472010-05-25 12:31:38 -07005866
5867 err_tadd:
5868 while (--i >= 0) {
5869 ata_tport_delete(host->ports[i]);
5870 }
5871 return rc;
5872
Tejun Heof3187192007-04-17 23:44:07 +09005873}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01005874EXPORT_SYMBOL_GPL(ata_host_register);
Tejun Heof3187192007-04-17 23:44:07 +09005875
5876/**
Tejun Heof5cda252007-04-17 23:44:07 +09005877 * ata_host_activate - start host, request IRQ and register it
5878 * @host: target ATA host
5879 * @irq: IRQ to request
5880 * @irq_handler: irq_handler used when requesting IRQ
5881 * @irq_flags: irq_flags used when requesting IRQ
5882 * @sht: scsi_host_template to use when registering the host
5883 *
5884 * After allocating an ATA host and initializing it, most libata
5885 * LLDs perform three steps to activate the host - start host,
Masanari Iidac9b55602016-04-13 23:36:27 +09005886 * request IRQ and register it. This helper takes necessary
Tejun Heof5cda252007-04-17 23:44:07 +09005887 * arguments and performs the three steps in one go.
5888 *
Paul Mundt3d46b2e2007-11-08 11:14:56 +09005889 * An invalid IRQ skips the IRQ registration and expects the host to
5890 * have set polling mode on the port. In this case, @irq_handler
5891 * should be NULL.
5892 *
Tejun Heof5cda252007-04-17 23:44:07 +09005893 * LOCKING:
5894 * Inherited from calling layer (may sleep).
5895 *
5896 * RETURNS:
5897 * 0 on success, -errno otherwise.
5898 */
5899int ata_host_activate(struct ata_host *host, int irq,
5900 irq_handler_t irq_handler, unsigned long irq_flags,
5901 struct scsi_host_template *sht)
5902{
Tejun Heocbcdd872007-08-18 13:14:55 +09005903 int i, rc;
Heiner Kallweit7e22c002015-12-06 21:56:33 +01005904 char *irq_desc;
Tejun Heof5cda252007-04-17 23:44:07 +09005905
5906 rc = ata_host_start(host);
5907 if (rc)
5908 return rc;
5909
Paul Mundt3d46b2e2007-11-08 11:14:56 +09005910 /* Special case for polling mode */
5911 if (!irq) {
5912 WARN_ON(irq_handler);
5913 return ata_host_register(host, sht);
5914 }
5915
Heiner Kallweit7e22c002015-12-06 21:56:33 +01005916 irq_desc = devm_kasprintf(host->dev, GFP_KERNEL, "%s[%s]",
5917 dev_driver_string(host->dev),
5918 dev_name(host->dev));
5919 if (!irq_desc)
5920 return -ENOMEM;
5921
Tejun Heof5cda252007-04-17 23:44:07 +09005922 rc = devm_request_irq(host->dev, irq, irq_handler, irq_flags,
Heiner Kallweit7e22c002015-12-06 21:56:33 +01005923 irq_desc, host);
Tejun Heof5cda252007-04-17 23:44:07 +09005924 if (rc)
5925 return rc;
5926
Tejun Heocbcdd872007-08-18 13:14:55 +09005927 for (i = 0; i < host->n_ports; i++)
5928 ata_port_desc(host->ports[i], "irq %d", irq);
Tejun Heo40318262007-07-03 01:38:47 +09005929
Tejun Heof5cda252007-04-17 23:44:07 +09005930 rc = ata_host_register(host, sht);
5931 /* if failed, just free the IRQ and leave ports alone */
5932 if (rc)
5933 devm_free_irq(host->dev, irq, host);
5934
5935 return rc;
5936}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01005937EXPORT_SYMBOL_GPL(ata_host_activate);
Tejun Heof5cda252007-04-17 23:44:07 +09005938
5939/**
Masanari Iidac9b55602016-04-13 23:36:27 +09005940 * ata_port_detach - Detach ATA port in preparation of device removal
Tejun Heo720ba122006-05-31 18:28:13 +09005941 * @ap: ATA port to be detached
5942 *
5943 * Detach all ATA devices and the associated SCSI devices of @ap;
5944 * then, remove the associated SCSI host. @ap is guaranteed to
5945 * be quiescent on return from this function.
5946 *
5947 * LOCKING:
5948 * Kernel thread context (may sleep).
5949 */
Adrian Bunk741b7762007-10-24 18:23:06 +02005950static void ata_port_detach(struct ata_port *ap)
Tejun Heo720ba122006-05-31 18:28:13 +09005951{
5952 unsigned long flags;
Levente Kurusaa6f9bf42014-05-06 15:57:48 +02005953 struct ata_link *link;
5954 struct ata_device *dev;
Tejun Heo720ba122006-05-31 18:28:13 +09005955
5956 if (!ap->ops->error_handler)
Tejun Heoc3cf30a2006-08-05 03:59:11 +09005957 goto skip_eh;
Tejun Heo720ba122006-05-31 18:28:13 +09005958
5959 /* tell EH we're leaving & flush EH */
Jeff Garzikba6a1302006-06-22 23:46:10 -04005960 spin_lock_irqsave(ap->lock, flags);
Tejun Heob51e9e52006-06-29 01:29:30 +09005961 ap->pflags |= ATA_PFLAG_UNLOADING;
Tejun Heoece180d2008-11-03 20:04:37 +09005962 ata_port_schedule_eh(ap);
Jeff Garzikba6a1302006-06-22 23:46:10 -04005963 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heo720ba122006-05-31 18:28:13 +09005964
Tejun Heoece180d2008-11-03 20:04:37 +09005965 /* wait till EH commits suicide */
Tejun Heo720ba122006-05-31 18:28:13 +09005966 ata_port_wait_eh(ap);
5967
Tejun Heoece180d2008-11-03 20:04:37 +09005968 /* it better be dead now */
5969 WARN_ON(!(ap->pflags & ATA_PFLAG_UNLOADED));
Tejun Heo720ba122006-05-31 18:28:13 +09005970
Tejun Heoafe2c512010-12-14 16:21:17 +01005971 cancel_delayed_work_sync(&ap->hotplug_task);
Tejun Heo720ba122006-05-31 18:28:13 +09005972
Tejun Heoc3cf30a2006-08-05 03:59:11 +09005973 skip_eh:
Levente Kurusaa6f9bf42014-05-06 15:57:48 +02005974 /* clean up zpodd on port removal */
5975 ata_for_each_link(link, ap, HOST_FIRST) {
5976 ata_for_each_dev(dev, link, ALL) {
5977 if (zpodd_dev_enabled(dev))
5978 zpodd_exit(dev);
5979 }
5980 }
Gwendal Grignoud9027472010-05-25 12:31:38 -07005981 if (ap->pmp_link) {
5982 int i;
5983 for (i = 0; i < SATA_PMP_MAX_PORTS; i++)
5984 ata_tlink_delete(&ap->pmp_link[i]);
5985 }
Tejun Heo720ba122006-05-31 18:28:13 +09005986 /* remove the associated SCSI host */
Jeff Garzikcca39742006-08-24 03:19:22 -04005987 scsi_remove_host(ap->scsi_host);
Rafael J. Wysockic5700762013-11-25 13:19:01 +01005988 ata_tport_delete(ap);
Tejun Heo720ba122006-05-31 18:28:13 +09005989}
5990
5991/**
Tejun Heo0529c1592007-01-20 16:00:26 +09005992 * ata_host_detach - Detach all ports of an ATA host
5993 * @host: Host to detach
5994 *
5995 * Detach all ports of @host.
5996 *
5997 * LOCKING:
5998 * Kernel thread context (may sleep).
5999 */
6000void ata_host_detach(struct ata_host *host)
6001{
6002 int i;
6003
Kai-Heng Fengb5292112020-06-03 15:48:19 +08006004 for (i = 0; i < host->n_ports; i++) {
6005 /* Ensure ata_port probe has completed */
6006 async_synchronize_cookie(host->ports[i]->cookie + 1);
Tejun Heo0529c1592007-01-20 16:00:26 +09006007 ata_port_detach(host->ports[i]);
Kai-Heng Fengb5292112020-06-03 15:48:19 +08006008 }
Tejun Heo562f0c22007-12-15 15:05:01 +09006009
6010 /* the host is dead now, dissociate ACPI */
6011 ata_acpi_dissociate(host);
Tejun Heo0529c1592007-01-20 16:00:26 +09006012}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01006013EXPORT_SYMBOL_GPL(ata_host_detach);
Tejun Heo0529c1592007-01-20 16:00:26 +09006014
Jeff Garzik374b1872005-08-30 05:42:52 -04006015#ifdef CONFIG_PCI
6016
Edward Falk0baab862005-06-02 18:17:13 -04006017/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07006018 * ata_pci_remove_one - PCI layer callback for device removal
6019 * @pdev: PCI device that was removed
6020 *
Tejun Heob878ca52007-01-20 16:00:28 +09006021 * PCI layer indicates to libata via this hook that hot-unplug or
6022 * module unload event has occurred. Detach all ports. Resource
6023 * release is handled via devres.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006024 *
6025 * LOCKING:
6026 * Inherited from PCI layer (may sleep).
6027 */
Tejun Heof0d36ef2007-01-20 16:00:28 +09006028void ata_pci_remove_one(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006029{
Brian Norris04a3f5b2012-12-03 10:34:41 -08006030 struct ata_host *host = pci_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006031
Tejun Heob878ca52007-01-20 16:00:28 +09006032 ata_host_detach(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006033}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01006034EXPORT_SYMBOL_GPL(ata_pci_remove_one);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006035
Prabhakar Kushwaha10a663a2020-01-25 03:37:29 +00006036void ata_pci_shutdown_one(struct pci_dev *pdev)
6037{
6038 struct ata_host *host = pci_get_drvdata(pdev);
6039 int i;
6040
6041 for (i = 0; i < host->n_ports; i++) {
6042 struct ata_port *ap = host->ports[i];
6043
6044 ap->pflags |= ATA_PFLAG_FROZEN;
6045
6046 /* Disable port interrupts */
6047 if (ap->ops->freeze)
6048 ap->ops->freeze(ap);
6049
6050 /* Stop the port DMA engines */
6051 if (ap->ops->port_stop)
6052 ap->ops->port_stop(ap);
6053 }
6054}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01006055EXPORT_SYMBOL_GPL(ata_pci_shutdown_one);
Prabhakar Kushwaha10a663a2020-01-25 03:37:29 +00006056
Linus Torvalds1da177e2005-04-16 15:20:36 -07006057/* move to PCI subsystem */
Jeff Garzik057ace52005-10-22 14:27:05 -04006058int pci_test_config_bits(struct pci_dev *pdev, const struct pci_bits *bits)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006059{
6060 unsigned long tmp = 0;
6061
6062 switch (bits->width) {
6063 case 1: {
6064 u8 tmp8 = 0;
6065 pci_read_config_byte(pdev, bits->reg, &tmp8);
6066 tmp = tmp8;
6067 break;
6068 }
6069 case 2: {
6070 u16 tmp16 = 0;
6071 pci_read_config_word(pdev, bits->reg, &tmp16);
6072 tmp = tmp16;
6073 break;
6074 }
6075 case 4: {
6076 u32 tmp32 = 0;
6077 pci_read_config_dword(pdev, bits->reg, &tmp32);
6078 tmp = tmp32;
6079 break;
6080 }
6081
6082 default:
6083 return -EINVAL;
6084 }
6085
6086 tmp &= bits->mask;
6087
6088 return (tmp == bits->val) ? 1 : 0;
6089}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01006090EXPORT_SYMBOL_GPL(pci_test_config_bits);
Jens Axboe9b847542006-01-06 09:28:07 +01006091
Tejun Heo6ffa01d2007-03-02 17:32:47 +09006092#ifdef CONFIG_PM
Tejun Heo3c5100c2006-07-26 16:58:33 +09006093void ata_pci_device_do_suspend(struct pci_dev *pdev, pm_message_t mesg)
Jens Axboe9b847542006-01-06 09:28:07 +01006094{
6095 pci_save_state(pdev);
Tejun Heo4c90d972007-02-20 18:14:48 +09006096 pci_disable_device(pdev);
Tejun Heo500530f2006-07-03 16:07:27 +09006097
Rafael J. Wysocki3a2d5b72008-02-23 19:13:25 +01006098 if (mesg.event & PM_EVENT_SLEEP)
Tejun Heo500530f2006-07-03 16:07:27 +09006099 pci_set_power_state(pdev, PCI_D3hot);
Jens Axboe9b847542006-01-06 09:28:07 +01006100}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01006101EXPORT_SYMBOL_GPL(ata_pci_device_do_suspend);
Jens Axboe9b847542006-01-06 09:28:07 +01006102
Tejun Heo553c4aa2006-12-26 19:39:50 +09006103int ata_pci_device_do_resume(struct pci_dev *pdev)
Jens Axboe9b847542006-01-06 09:28:07 +01006104{
Tejun Heo553c4aa2006-12-26 19:39:50 +09006105 int rc;
6106
Jens Axboe9b847542006-01-06 09:28:07 +01006107 pci_set_power_state(pdev, PCI_D0);
6108 pci_restore_state(pdev);
Tejun Heo553c4aa2006-12-26 19:39:50 +09006109
Tejun Heob878ca52007-01-20 16:00:28 +09006110 rc = pcim_enable_device(pdev);
Tejun Heo553c4aa2006-12-26 19:39:50 +09006111 if (rc) {
Joe Perchesa44fec12011-04-15 15:51:58 -07006112 dev_err(&pdev->dev,
6113 "failed to enable device after resume (%d)\n", rc);
Tejun Heo553c4aa2006-12-26 19:39:50 +09006114 return rc;
6115 }
6116
Jens Axboe9b847542006-01-06 09:28:07 +01006117 pci_set_master(pdev);
Tejun Heo553c4aa2006-12-26 19:39:50 +09006118 return 0;
Tejun Heo500530f2006-07-03 16:07:27 +09006119}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01006120EXPORT_SYMBOL_GPL(ata_pci_device_do_resume);
Tejun Heo500530f2006-07-03 16:07:27 +09006121
Tejun Heo3c5100c2006-07-26 16:58:33 +09006122int ata_pci_device_suspend(struct pci_dev *pdev, pm_message_t mesg)
Tejun Heo500530f2006-07-03 16:07:27 +09006123{
Brian Norris04a3f5b2012-12-03 10:34:41 -08006124 struct ata_host *host = pci_get_drvdata(pdev);
Tejun Heo500530f2006-07-03 16:07:27 +09006125 int rc = 0;
6126
Jeff Garzikcca39742006-08-24 03:19:22 -04006127 rc = ata_host_suspend(host, mesg);
Tejun Heo500530f2006-07-03 16:07:27 +09006128 if (rc)
6129 return rc;
6130
Tejun Heo3c5100c2006-07-26 16:58:33 +09006131 ata_pci_device_do_suspend(pdev, mesg);
Tejun Heo500530f2006-07-03 16:07:27 +09006132
6133 return 0;
6134}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01006135EXPORT_SYMBOL_GPL(ata_pci_device_suspend);
Tejun Heo500530f2006-07-03 16:07:27 +09006136
6137int ata_pci_device_resume(struct pci_dev *pdev)
6138{
Brian Norris04a3f5b2012-12-03 10:34:41 -08006139 struct ata_host *host = pci_get_drvdata(pdev);
Tejun Heo553c4aa2006-12-26 19:39:50 +09006140 int rc;
Tejun Heo500530f2006-07-03 16:07:27 +09006141
Tejun Heo553c4aa2006-12-26 19:39:50 +09006142 rc = ata_pci_device_do_resume(pdev);
6143 if (rc == 0)
6144 ata_host_resume(host);
6145 return rc;
Jens Axboe9b847542006-01-06 09:28:07 +01006146}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01006147EXPORT_SYMBOL_GPL(ata_pci_device_resume);
Tejun Heo6ffa01d2007-03-02 17:32:47 +09006148#endif /* CONFIG_PM */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006149#endif /* CONFIG_PCI */
6150
Brian Norrisb7db04d2012-11-02 12:29:32 -07006151/**
6152 * ata_platform_remove_one - Platform layer callback for device removal
6153 * @pdev: Platform device that was removed
6154 *
6155 * Platform layer indicates to libata via this hook that hot-unplug or
6156 * module unload event has occurred. Detach all ports. Resource
6157 * release is handled via devres.
6158 *
6159 * LOCKING:
6160 * Inherited from platform layer (may sleep).
6161 */
6162int ata_platform_remove_one(struct platform_device *pdev)
6163{
6164 struct ata_host *host = platform_get_drvdata(pdev);
6165
6166 ata_host_detach(host);
6167
6168 return 0;
6169}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01006170EXPORT_SYMBOL_GPL(ata_platform_remove_one);
Brian Norrisb7db04d2012-11-02 12:29:32 -07006171
Bartlomiej Zolnierkiewiczbf89b0b2020-03-26 16:58:22 +01006172#ifdef CONFIG_ATA_FORCE
Tejun Heo33267322008-02-13 09:15:09 +09006173static int __init ata_parse_force_one(char **cur,
6174 struct ata_force_ent *force_ent,
6175 const char **reason)
6176{
Rasmus Villemoes0f5f2642015-06-09 15:33:19 +02006177 static const struct ata_force_param force_tbl[] __initconst = {
Tejun Heo33267322008-02-13 09:15:09 +09006178 { "40c", .cbl = ATA_CBL_PATA40 },
6179 { "80c", .cbl = ATA_CBL_PATA80 },
6180 { "short40c", .cbl = ATA_CBL_PATA40_SHORT },
6181 { "unk", .cbl = ATA_CBL_PATA_UNK },
6182 { "ign", .cbl = ATA_CBL_PATA_IGN },
6183 { "sata", .cbl = ATA_CBL_SATA },
6184 { "1.5Gbps", .spd_limit = 1 },
6185 { "3.0Gbps", .spd_limit = 2 },
6186 { "noncq", .horkage_on = ATA_HORKAGE_NONCQ },
6187 { "ncq", .horkage_off = ATA_HORKAGE_NONCQ },
Martin K. Petersend7b16e42015-05-04 21:54:18 -04006188 { "noncqtrim", .horkage_on = ATA_HORKAGE_NO_NCQ_TRIM },
6189 { "ncqtrim", .horkage_off = ATA_HORKAGE_NO_NCQ_TRIM },
Kate Hsuan7a8526a2021-09-03 17:44:11 +08006190 { "noncqati", .horkage_on = ATA_HORKAGE_NO_NCQ_ON_ATI },
6191 { "ncqati", .horkage_off = ATA_HORKAGE_NO_NCQ_ON_ATI },
Tejun Heo43c9c592010-05-23 12:59:11 +02006192 { "dump_id", .horkage_on = ATA_HORKAGE_DUMP_ID },
Tejun Heo33267322008-02-13 09:15:09 +09006193 { "pio0", .xfer_mask = 1 << (ATA_SHIFT_PIO + 0) },
6194 { "pio1", .xfer_mask = 1 << (ATA_SHIFT_PIO + 1) },
6195 { "pio2", .xfer_mask = 1 << (ATA_SHIFT_PIO + 2) },
6196 { "pio3", .xfer_mask = 1 << (ATA_SHIFT_PIO + 3) },
6197 { "pio4", .xfer_mask = 1 << (ATA_SHIFT_PIO + 4) },
6198 { "pio5", .xfer_mask = 1 << (ATA_SHIFT_PIO + 5) },
6199 { "pio6", .xfer_mask = 1 << (ATA_SHIFT_PIO + 6) },
6200 { "mwdma0", .xfer_mask = 1 << (ATA_SHIFT_MWDMA + 0) },
6201 { "mwdma1", .xfer_mask = 1 << (ATA_SHIFT_MWDMA + 1) },
6202 { "mwdma2", .xfer_mask = 1 << (ATA_SHIFT_MWDMA + 2) },
6203 { "mwdma3", .xfer_mask = 1 << (ATA_SHIFT_MWDMA + 3) },
6204 { "mwdma4", .xfer_mask = 1 << (ATA_SHIFT_MWDMA + 4) },
6205 { "udma0", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 0) },
6206 { "udma16", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 0) },
6207 { "udma/16", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 0) },
6208 { "udma1", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 1) },
6209 { "udma25", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 1) },
6210 { "udma/25", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 1) },
6211 { "udma2", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 2) },
6212 { "udma33", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 2) },
6213 { "udma/33", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 2) },
6214 { "udma3", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 3) },
6215 { "udma44", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 3) },
6216 { "udma/44", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 3) },
6217 { "udma4", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 4) },
6218 { "udma66", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 4) },
6219 { "udma/66", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 4) },
6220 { "udma5", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 5) },
6221 { "udma100", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 5) },
6222 { "udma/100", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 5) },
6223 { "udma6", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 6) },
6224 { "udma133", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 6) },
6225 { "udma/133", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 6) },
6226 { "udma7", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 7) },
Tejun Heo05944bd2008-08-13 20:19:09 +09006227 { "nohrst", .lflags = ATA_LFLAG_NO_HRST },
6228 { "nosrst", .lflags = ATA_LFLAG_NO_SRST },
6229 { "norst", .lflags = ATA_LFLAG_NO_HRST | ATA_LFLAG_NO_SRST },
Dan Williamsca6d43b2012-06-21 23:41:41 -07006230 { "rstonce", .lflags = ATA_LFLAG_RST_ONCE },
Vincent Pelletier966fbe12013-05-21 22:30:58 +02006231 { "atapi_dmadir", .horkage_on = ATA_HORKAGE_ATAPI_DMADIR },
Robin H. Johnsonb8bd6dc2013-12-16 09:31:19 -08006232 { "disable", .horkage_on = ATA_HORKAGE_DISABLE },
Tejun Heo33267322008-02-13 09:15:09 +09006233 };
6234 char *start = *cur, *p = *cur;
6235 char *id, *val, *endp;
6236 const struct ata_force_param *match_fp = NULL;
6237 int nr_matches = 0, i;
6238
6239 /* find where this param ends and update *cur */
6240 while (*p != '\0' && *p != ',')
6241 p++;
6242
6243 if (*p == '\0')
6244 *cur = p;
6245 else
6246 *cur = p + 1;
6247
6248 *p = '\0';
6249
6250 /* parse */
6251 p = strchr(start, ':');
6252 if (!p) {
6253 val = strstrip(start);
6254 goto parse_val;
6255 }
6256 *p = '\0';
6257
6258 id = strstrip(start);
6259 val = strstrip(p + 1);
6260
6261 /* parse id */
6262 p = strchr(id, '.');
6263 if (p) {
6264 *p++ = '\0';
6265 force_ent->device = simple_strtoul(p, &endp, 10);
6266 if (p == endp || *endp != '\0') {
6267 *reason = "invalid device";
6268 return -EINVAL;
6269 }
6270 }
6271
6272 force_ent->port = simple_strtoul(id, &endp, 10);
Tejun Heof7cf69a2017-05-31 14:26:26 -04006273 if (id == endp || *endp != '\0') {
Tejun Heo33267322008-02-13 09:15:09 +09006274 *reason = "invalid port/link";
6275 return -EINVAL;
6276 }
6277
6278 parse_val:
6279 /* parse val, allow shortcuts so that both 1.5 and 1.5Gbps work */
6280 for (i = 0; i < ARRAY_SIZE(force_tbl); i++) {
6281 const struct ata_force_param *fp = &force_tbl[i];
6282
6283 if (strncasecmp(val, fp->name, strlen(val)))
6284 continue;
6285
6286 nr_matches++;
6287 match_fp = fp;
6288
6289 if (strcasecmp(val, fp->name) == 0) {
6290 nr_matches = 1;
6291 break;
6292 }
6293 }
6294
6295 if (!nr_matches) {
6296 *reason = "unknown value";
6297 return -EINVAL;
6298 }
6299 if (nr_matches > 1) {
Arvind Yadav9de55352017-10-28 23:51:47 +05306300 *reason = "ambiguous value";
Tejun Heo33267322008-02-13 09:15:09 +09006301 return -EINVAL;
6302 }
6303
6304 force_ent->param = *match_fp;
6305
6306 return 0;
6307}
6308
6309static void __init ata_parse_force_param(void)
6310{
6311 int idx = 0, size = 1;
6312 int last_port = -1, last_device = -1;
6313 char *p, *cur, *next;
6314
6315 /* calculate maximum number of params and allocate force_tbl */
6316 for (p = ata_force_param_buf; *p; p++)
6317 if (*p == ',')
6318 size++;
6319
Kees Cook6396bb22018-06-12 14:03:40 -07006320 ata_force_tbl = kcalloc(size, sizeof(ata_force_tbl[0]), GFP_KERNEL);
Tejun Heo33267322008-02-13 09:15:09 +09006321 if (!ata_force_tbl) {
6322 printk(KERN_WARNING "ata: failed to extend force table, "
6323 "libata.force ignored\n");
6324 return;
6325 }
6326
6327 /* parse and populate the table */
6328 for (cur = ata_force_param_buf; *cur != '\0'; cur = next) {
6329 const char *reason = "";
6330 struct ata_force_ent te = { .port = -1, .device = -1 };
6331
6332 next = cur;
6333 if (ata_parse_force_one(&next, &te, &reason)) {
6334 printk(KERN_WARNING "ata: failed to parse force "
6335 "parameter \"%s\" (%s)\n",
6336 cur, reason);
6337 continue;
6338 }
6339
6340 if (te.port == -1) {
6341 te.port = last_port;
6342 te.device = last_device;
6343 }
6344
6345 ata_force_tbl[idx++] = te;
6346
6347 last_port = te.port;
6348 last_device = te.device;
6349 }
6350
6351 ata_force_tbl_size = idx;
6352}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006353
Bartlomiej Zolnierkiewiczbf89b0b2020-03-26 16:58:22 +01006354static void ata_free_force_param(void)
6355{
6356 kfree(ata_force_tbl);
6357}
6358#else
6359static inline void ata_parse_force_param(void) { }
6360static inline void ata_free_force_param(void) { }
6361#endif
6362
Linus Torvalds1da177e2005-04-16 15:20:36 -07006363static int __init ata_init(void)
6364{
Gwendal Grignoud9027472010-05-25 12:31:38 -07006365 int rc;
Tejun Heo270390e2010-05-10 21:41:35 +02006366
Tejun Heo33267322008-02-13 09:15:09 +09006367 ata_parse_force_param();
6368
Tejun Heo270390e2010-05-10 21:41:35 +02006369 rc = ata_sff_init();
Tejun Heoad72cf92010-07-02 10:03:52 +02006370 if (rc) {
Bartlomiej Zolnierkiewiczbf89b0b2020-03-26 16:58:22 +01006371 ata_free_force_param();
Tejun Heoad72cf92010-07-02 10:03:52 +02006372 return rc;
6373 }
Tejun Heo453b07a2006-05-31 18:27:42 +09006374
Gwendal Grignoud9027472010-05-25 12:31:38 -07006375 libata_transport_init();
6376 ata_scsi_transport_template = ata_attach_transport();
6377 if (!ata_scsi_transport_template) {
6378 ata_sff_exit();
6379 rc = -ENOMEM;
6380 goto err_out;
Jeff Garzik4fca3772011-02-15 01:13:24 -05006381 }
Gwendal Grignoud9027472010-05-25 12:31:38 -07006382
Linus Torvalds1da177e2005-04-16 15:20:36 -07006383 printk(KERN_DEBUG "libata version " DRV_VERSION " loaded.\n");
6384 return 0;
Gwendal Grignoud9027472010-05-25 12:31:38 -07006385
6386err_out:
6387 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006388}
6389
6390static void __exit ata_exit(void)
6391{
Gwendal Grignoud9027472010-05-25 12:31:38 -07006392 ata_release_transport(ata_scsi_transport_template);
6393 libata_transport_exit();
Tejun Heo270390e2010-05-10 21:41:35 +02006394 ata_sff_exit();
Bartlomiej Zolnierkiewiczbf89b0b2020-03-26 16:58:22 +01006395 ata_free_force_param();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006396}
6397
Brian Kinga4625082006-11-13 16:32:36 -06006398subsys_initcall(ata_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006399module_exit(ata_exit);
6400
Akinobu Mita9990b6f2010-04-12 21:11:41 +09006401static DEFINE_RATELIMIT_STATE(ratelimit, HZ / 5, 1);
Jeff Garzik67846b32005-10-05 02:58:32 -04006402
6403int ata_ratelimit(void)
6404{
Akinobu Mita9990b6f2010-04-12 21:11:41 +09006405 return __ratelimit(&ratelimit);
Jeff Garzik67846b32005-10-05 02:58:32 -04006406}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01006407EXPORT_SYMBOL_GPL(ata_ratelimit);
Jeff Garzik67846b32005-10-05 02:58:32 -04006408
Tejun Heoc0c362b2010-09-06 17:57:14 +02006409/**
6410 * ata_msleep - ATA EH owner aware msleep
6411 * @ap: ATA port to attribute the sleep to
6412 * @msecs: duration to sleep in milliseconds
6413 *
6414 * Sleeps @msecs. If the current task is owner of @ap's EH, the
6415 * ownership is released before going to sleep and reacquired
6416 * after the sleep is complete. IOW, other ports sharing the
6417 * @ap->host will be allowed to own the EH while this task is
6418 * sleeping.
6419 *
6420 * LOCKING:
6421 * Might sleep.
6422 */
Tejun Heo97750ce2010-09-06 17:56:29 +02006423void ata_msleep(struct ata_port *ap, unsigned int msecs)
6424{
Tejun Heoc0c362b2010-09-06 17:57:14 +02006425 bool owns_eh = ap && ap->host->eh_owner == current;
6426
6427 if (owns_eh)
6428 ata_eh_release(ap);
6429
Anil Veliyankara Madam848c3922016-01-07 21:18:52 -08006430 if (msecs < 20) {
6431 unsigned long usecs = msecs * USEC_PER_MSEC;
6432 usleep_range(usecs, usecs + 50);
6433 } else {
6434 msleep(msecs);
6435 }
Tejun Heoc0c362b2010-09-06 17:57:14 +02006436
6437 if (owns_eh)
6438 ata_eh_acquire(ap);
Tejun Heo97750ce2010-09-06 17:56:29 +02006439}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01006440EXPORT_SYMBOL_GPL(ata_msleep);
Tejun Heo97750ce2010-09-06 17:56:29 +02006441
Tejun Heoc22daff2006-04-11 22:22:29 +09006442/**
6443 * ata_wait_register - wait until register value changes
Tejun Heo97750ce2010-09-06 17:56:29 +02006444 * @ap: ATA port to wait register for, can be NULL
Tejun Heoc22daff2006-04-11 22:22:29 +09006445 * @reg: IO-mapped register
6446 * @mask: Mask to apply to read register value
6447 * @val: Wait condition
Tejun Heo341c2c92008-05-20 02:17:51 +09006448 * @interval: polling interval in milliseconds
6449 * @timeout: timeout in milliseconds
Tejun Heoc22daff2006-04-11 22:22:29 +09006450 *
6451 * Waiting for some bits of register to change is a common
6452 * operation for ATA controllers. This function reads 32bit LE
6453 * IO-mapped register @reg and tests for the following condition.
6454 *
6455 * (*@reg & mask) != val
6456 *
6457 * If the condition is met, it returns; otherwise, the process is
6458 * repeated after @interval_msec until timeout.
6459 *
6460 * LOCKING:
6461 * Kernel thread context (may sleep)
6462 *
6463 * RETURNS:
6464 * The final register value.
6465 */
Tejun Heo97750ce2010-09-06 17:56:29 +02006466u32 ata_wait_register(struct ata_port *ap, void __iomem *reg, u32 mask, u32 val,
Tejun Heo341c2c92008-05-20 02:17:51 +09006467 unsigned long interval, unsigned long timeout)
Tejun Heoc22daff2006-04-11 22:22:29 +09006468{
Tejun Heo341c2c92008-05-20 02:17:51 +09006469 unsigned long deadline;
Tejun Heoc22daff2006-04-11 22:22:29 +09006470 u32 tmp;
6471
6472 tmp = ioread32(reg);
6473
6474 /* Calculate timeout _after_ the first read to make sure
6475 * preceding writes reach the controller before starting to
6476 * eat away the timeout.
6477 */
Tejun Heo341c2c92008-05-20 02:17:51 +09006478 deadline = ata_deadline(jiffies, timeout);
Tejun Heoc22daff2006-04-11 22:22:29 +09006479
Tejun Heo341c2c92008-05-20 02:17:51 +09006480 while ((tmp & mask) == val && time_before(jiffies, deadline)) {
Tejun Heo97750ce2010-09-06 17:56:29 +02006481 ata_msleep(ap, interval);
Tejun Heoc22daff2006-04-11 22:22:29 +09006482 tmp = ioread32(reg);
6483 }
6484
6485 return tmp;
6486}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01006487EXPORT_SYMBOL_GPL(ata_wait_register);
Tejun Heoc22daff2006-04-11 22:22:29 +09006488
Linus Torvalds1da177e2005-04-16 15:20:36 -07006489/*
Tejun Heodd5b06c2006-08-10 16:59:12 +09006490 * Dummy port_ops
6491 */
Tejun Heodd5b06c2006-08-10 16:59:12 +09006492static unsigned int ata_dummy_qc_issue(struct ata_queued_cmd *qc)
6493{
6494 return AC_ERR_SYSTEM;
6495}
6496
Tejun Heo182d7bb2008-04-07 22:47:21 +09006497static void ata_dummy_error_handler(struct ata_port *ap)
6498{
6499 /* truly dummy */
6500}
6501
Tejun Heo029cfd62008-03-25 12:22:49 +09006502struct ata_port_operations ata_dummy_port_ops = {
Tejun Heodd5b06c2006-08-10 16:59:12 +09006503 .qc_prep = ata_noop_qc_prep,
6504 .qc_issue = ata_dummy_qc_issue,
Tejun Heo182d7bb2008-04-07 22:47:21 +09006505 .error_handler = ata_dummy_error_handler,
Dan Williamse4a9c372012-06-21 23:25:27 -07006506 .sched_eh = ata_std_sched_eh,
6507 .end_eh = ata_std_end_eh,
Tejun Heodd5b06c2006-08-10 16:59:12 +09006508};
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01006509EXPORT_SYMBOL_GPL(ata_dummy_port_ops);
Tejun Heodd5b06c2006-08-10 16:59:12 +09006510
Tejun Heo21b0ad42007-04-17 23:44:07 +09006511const struct ata_port_info ata_dummy_port_info = {
6512 .port_ops = &ata_dummy_port_ops,
6513};
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01006514EXPORT_SYMBOL_GPL(ata_dummy_port_info);
Tejun Heo21b0ad42007-04-17 23:44:07 +09006515
Tejun Heodd5b06c2006-08-10 16:59:12 +09006516/*
Joe Perchesa9a79df2011-04-15 15:51:59 -07006517 * Utility print functions
6518 */
Joe Perchesd7bead12014-09-22 09:52:18 -07006519void ata_port_printk(const struct ata_port *ap, const char *level,
6520 const char *fmt, ...)
Joe Perchesa9a79df2011-04-15 15:51:59 -07006521{
6522 struct va_format vaf;
6523 va_list args;
Joe Perchesa9a79df2011-04-15 15:51:59 -07006524
6525 va_start(args, fmt);
6526
6527 vaf.fmt = fmt;
6528 vaf.va = &args;
6529
Joe Perchesd7bead12014-09-22 09:52:18 -07006530 printk("%sata%u: %pV", level, ap->print_id, &vaf);
Joe Perchesa9a79df2011-04-15 15:51:59 -07006531
6532 va_end(args);
Joe Perchesa9a79df2011-04-15 15:51:59 -07006533}
6534EXPORT_SYMBOL(ata_port_printk);
6535
Joe Perchesd7bead12014-09-22 09:52:18 -07006536void ata_link_printk(const struct ata_link *link, const char *level,
6537 const char *fmt, ...)
Joe Perchesa9a79df2011-04-15 15:51:59 -07006538{
6539 struct va_format vaf;
6540 va_list args;
Joe Perchesa9a79df2011-04-15 15:51:59 -07006541
6542 va_start(args, fmt);
6543
6544 vaf.fmt = fmt;
6545 vaf.va = &args;
6546
6547 if (sata_pmp_attached(link->ap) || link->ap->slave_link)
Joe Perchesd7bead12014-09-22 09:52:18 -07006548 printk("%sata%u.%02u: %pV",
6549 level, link->ap->print_id, link->pmp, &vaf);
Joe Perchesa9a79df2011-04-15 15:51:59 -07006550 else
Joe Perchesd7bead12014-09-22 09:52:18 -07006551 printk("%sata%u: %pV",
6552 level, link->ap->print_id, &vaf);
Joe Perchesa9a79df2011-04-15 15:51:59 -07006553
6554 va_end(args);
Joe Perchesa9a79df2011-04-15 15:51:59 -07006555}
6556EXPORT_SYMBOL(ata_link_printk);
6557
Joe Perchesd7bead12014-09-22 09:52:18 -07006558void ata_dev_printk(const struct ata_device *dev, const char *level,
Joe Perchesa9a79df2011-04-15 15:51:59 -07006559 const char *fmt, ...)
6560{
6561 struct va_format vaf;
6562 va_list args;
Joe Perchesa9a79df2011-04-15 15:51:59 -07006563
6564 va_start(args, fmt);
6565
6566 vaf.fmt = fmt;
6567 vaf.va = &args;
6568
Joe Perchesd7bead12014-09-22 09:52:18 -07006569 printk("%sata%u.%02u: %pV",
6570 level, dev->link->ap->print_id, dev->link->pmp + dev->devno,
6571 &vaf);
Joe Perchesa9a79df2011-04-15 15:51:59 -07006572
6573 va_end(args);
Joe Perchesa9a79df2011-04-15 15:51:59 -07006574}
6575EXPORT_SYMBOL(ata_dev_printk);
6576
Joe Perches06296a12011-04-15 15:52:00 -07006577void ata_print_version(const struct device *dev, const char *version)
6578{
6579 dev_printk(KERN_DEBUG, dev, "version %s\n", version);
6580}
6581EXPORT_SYMBOL(ata_print_version);