blob: dc48e983ccdba4f145dc8339d177804f197802c2 [file] [log] [blame]
Thomas Gleixnerc82ee6d2019-05-19 15:51:48 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Jeff Garzikaf36d7f2005-08-28 20:18:39 -04003 * libata-core.c - helper library for ATA
4 *
Jeff Garzikaf36d7f2005-08-28 20:18:39 -04005 * Copyright 2003-2004 Red Hat, Inc. All rights reserved.
6 * Copyright 2003-2004 Jeff Garzik
7 *
Jeff Garzikaf36d7f2005-08-28 20:18:39 -04008 * libata documentation is available via 'make {ps|pdf}docs',
Mauro Carvalho Chehab19285f32017-05-14 11:52:56 -03009 * as Documentation/driver-api/libata.rst
Jeff Garzikaf36d7f2005-08-28 20:18:39 -040010 *
11 * Hardware documentation available from http://www.t13.org/ and
12 * http://www.sata-io.org/
13 *
Alan Cox92c52c52007-11-19 14:30:16 +000014 * Standards documents from:
15 * http://www.t13.org (ATA standards, PCI DMA IDE spec)
16 * http://www.t10.org (SCSI MMC - for ATAPI MMC)
17 * http://www.sata-io.org (SATA)
18 * http://www.compactflash.org (CF)
19 * http://www.qic.org (QIC157 - Tape and DSC)
20 * http://www.ce-ata.org (CE-ATA: not supported)
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +010021 *
22 * libata is essentially a library of internal helper functions for
23 * low-level ATA host controller drivers. As such, the API/ABI is
24 * likely to change as new drivers are added and updated.
25 * Do not depend on ABI/API stability.
Linus Torvalds1da177e2005-04-16 15:20:36 -070026 */
27
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/kernel.h>
29#include <linux/module.h>
30#include <linux/pci.h>
31#include <linux/init.h>
32#include <linux/list.h>
33#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <linux/spinlock.h>
35#include <linux/blkdev.h>
36#include <linux/delay.h>
37#include <linux/timer.h>
Anil Veliyankara Madam848c3922016-01-07 21:18:52 -080038#include <linux/time.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/interrupt.h>
40#include <linux/completion.h>
41#include <linux/suspend.h>
42#include <linux/workqueue.h>
David Hardeman378f0582005-09-17 17:55:31 +100043#include <linux/scatterlist.h>
Jeff Garzik2dcb4072007-10-19 06:42:56 -040044#include <linux/io.h>
Arjan van de Ven79318052009-01-04 05:32:28 -080045#include <linux/async.h>
Mark Lorde18086d2009-03-19 13:32:21 -040046#include <linux/log2.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090047#include <linux/slab.h>
George Spelvin428ac5f2014-08-06 16:09:27 -070048#include <linux/glob.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include <scsi/scsi.h>
Jeff Garzik193515d2005-11-07 00:59:37 -050050#include <scsi/scsi_cmnd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <scsi/scsi_host.h>
52#include <linux/libata.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#include <asm/byteorder.h>
Hannes Reineckefe5af0c2016-04-25 12:45:48 +020054#include <asm/unaligned.h>
Tejun Heo140b5e592007-12-12 12:21:52 +090055#include <linux/cdrom.h>
Akinobu Mita9990b6f2010-04-12 21:11:41 +090056#include <linux/ratelimit.h>
Stephan Linzeb25cb92016-06-10 07:59:56 +020057#include <linux/leds.h>
Lin Ming9ee4f392011-12-05 09:20:28 +080058#include <linux/pm_runtime.h>
Brian Norrisb7db04d2012-11-02 12:29:32 -070059#include <linux/platform_device.h>
Bartlomiej Zolnierkiewiczbbf5a092020-03-26 16:58:01 +010060#include <asm/setup.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Hannes Reinecke255c03d2015-03-27 16:46:38 +010062#define CREATE_TRACE_POINTS
63#include <trace/events/libata.h>
64
Linus Torvalds1da177e2005-04-16 15:20:36 -070065#include "libata.h"
Gwendal Grignoud9027472010-05-25 12:31:38 -070066#include "libata-transport.h"
Jeff Garzikfda0efc2007-01-31 07:43:15 -050067
Tejun Heod7bb4cc2006-05-31 18:27:46 +090068/* debounce timing parameters in msecs { interval, duration, timeout } */
Tejun Heoe9c83912006-07-03 16:07:26 +090069const unsigned long sata_deb_timing_normal[] = { 5, 100, 2000 };
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +010070EXPORT_SYMBOL_GPL(sata_deb_timing_normal);
Tejun Heoe9c83912006-07-03 16:07:26 +090071const unsigned long sata_deb_timing_hotplug[] = { 25, 500, 2000 };
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +010072EXPORT_SYMBOL_GPL(sata_deb_timing_hotplug);
Tejun Heoe9c83912006-07-03 16:07:26 +090073const unsigned long sata_deb_timing_long[] = { 100, 2000, 5000 };
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +010074EXPORT_SYMBOL_GPL(sata_deb_timing_long);
Tejun Heod7bb4cc2006-05-31 18:27:46 +090075
Tejun Heo029cfd62008-03-25 12:22:49 +090076const struct ata_port_operations ata_base_port_ops = {
Tejun Heo0aa11132008-04-07 22:47:18 +090077 .prereset = ata_std_prereset,
Tejun Heo203c75b2008-04-07 22:47:18 +090078 .postreset = ata_std_postreset,
Tejun Heoa1efdab2008-03-25 12:22:50 +090079 .error_handler = ata_std_error_handler,
Dan Williamse4a9c372012-06-21 23:25:27 -070080 .sched_eh = ata_std_sched_eh,
81 .end_eh = ata_std_end_eh,
Tejun Heo029cfd62008-03-25 12:22:49 +090082};
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +010083EXPORT_SYMBOL_GPL(ata_base_port_ops);
Tejun Heo029cfd62008-03-25 12:22:49 +090084
85const struct ata_port_operations sata_port_ops = {
86 .inherits = &ata_base_port_ops,
87
88 .qc_defer = ata_std_qc_defer,
Tejun Heo57c9efd2008-04-07 22:47:19 +090089 .hardreset = sata_std_hardreset,
Tejun Heo029cfd62008-03-25 12:22:49 +090090};
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +010091EXPORT_SYMBOL_GPL(sata_port_ops);
Tejun Heo029cfd62008-03-25 12:22:49 +090092
Tejun Heo3373efd2006-05-15 20:57:53 +090093static unsigned int ata_dev_init_params(struct ata_device *dev,
94 u16 heads, u16 sectors);
95static unsigned int ata_dev_set_xfermode(struct ata_device *dev);
96static void ata_dev_xfermask(struct ata_device *dev);
Tejun Heo75683fe2007-07-05 13:31:27 +090097static unsigned long ata_dev_blacklisted(const struct ata_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
Tero Roponena78f57a2012-04-22 11:38:00 +030099atomic_t ata_print_id = ATOMIC_INIT(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
Tejun Heo33267322008-02-13 09:15:09 +0900101struct ata_force_param {
102 const char *name;
Bartlomiej Zolnierkiewicz8ba5a452020-03-26 16:58:02 +0100103 u8 cbl;
104 u8 spd_limit;
Tejun Heo33267322008-02-13 09:15:09 +0900105 unsigned long xfer_mask;
106 unsigned int horkage_on;
107 unsigned int horkage_off;
Bartlomiej Zolnierkiewicz8ba5a452020-03-26 16:58:02 +0100108 u16 lflags;
Tejun Heo33267322008-02-13 09:15:09 +0900109};
110
111struct ata_force_ent {
112 int port;
113 int device;
114 struct ata_force_param param;
115};
116
117static struct ata_force_ent *ata_force_tbl;
118static int ata_force_tbl_size;
119
Bartlomiej Zolnierkiewiczbbf5a092020-03-26 16:58:01 +0100120static char ata_force_param_buf[COMMAND_LINE_SIZE] __initdata;
Tejun Heo7afb4222008-03-09 20:21:53 +0900121/* param_buf is thrown away after initialization, disallow read */
122module_param_string(force, ata_force_param_buf, sizeof(ata_force_param_buf), 0);
Mauro Carvalho Chehab8c27ceff32016-10-18 10:12:27 -0200123MODULE_PARM_DESC(force, "Force ATA configurations including cable type, link speed and transfer mode (see Documentation/admin-guide/kernel-parameters.rst for details)");
Tejun Heo33267322008-02-13 09:15:09 +0900124
Tejun Heo2486fa52008-07-31 07:52:40 +0900125static int atapi_enabled = 1;
Jeff Garzik1623c812005-08-30 03:37:42 -0400126module_param(atapi_enabled, int, 0444);
Evgeni Golovad5d8ea2009-06-22 09:46:20 +0200127MODULE_PARM_DESC(atapi_enabled, "Enable discovery of ATAPI devices (0=off, 1=on [default])");
Jeff Garzik1623c812005-08-30 03:37:42 -0400128
Adrian Bunkc5c61bd2008-02-25 02:07:25 +0200129static int atapi_dmadir = 0;
Albert Lee95de7192006-04-04 10:57:18 +0800130module_param(atapi_dmadir, int, 0444);
Evgeni Golovad5d8ea2009-06-22 09:46:20 +0200131MODULE_PARM_DESC(atapi_dmadir, "Enable ATAPI DMADIR bridge support (0=off [default], 1=on)");
Albert Lee95de7192006-04-04 10:57:18 +0800132
Mark Lordbaf4fdf2007-08-08 01:08:45 +0900133int atapi_passthru16 = 1;
134module_param(atapi_passthru16, int, 0444);
Evgeni Golovad5d8ea2009-06-22 09:46:20 +0200135MODULE_PARM_DESC(atapi_passthru16, "Enable ATA_16 passthru for ATAPI devices (0=off, 1=on [default])");
Mark Lordbaf4fdf2007-08-08 01:08:45 +0900136
Jeff Garzikc3c013a2006-02-27 22:31:19 -0500137int libata_fua = 0;
138module_param_named(fua, libata_fua, int, 0444);
Evgeni Golovad5d8ea2009-06-22 09:46:20 +0200139MODULE_PARM_DESC(fua, "FUA support (0=off [default], 1=on)");
Jeff Garzikc3c013a2006-02-27 22:31:19 -0500140
Jeff Garzik2dcb4072007-10-19 06:42:56 -0400141static int ata_ignore_hpa;
Alan Cox1e999732007-04-11 00:23:13 +0100142module_param_named(ignore_hpa, ata_ignore_hpa, int, 0644);
143MODULE_PARM_DESC(ignore_hpa, "Ignore HPA limit (0=keep BIOS limits, 1=ignore limits, using full disk)");
144
Alan Coxb3a70602007-10-02 12:38:26 -0400145static int libata_dma_mask = ATA_DMA_MASK_ATA|ATA_DMA_MASK_ATAPI|ATA_DMA_MASK_CFA;
146module_param_named(dma, libata_dma_mask, int, 0444);
147MODULE_PARM_DESC(dma, "DMA enable/disable (0x1==ATA, 0x2==ATAPI, 0x4==CF)");
148
Tejun Heo87fbc5a2008-05-20 02:17:54 +0900149static int ata_probe_timeout;
Andrew Mortona8601e52006-06-25 01:36:52 -0700150module_param(ata_probe_timeout, int, 0444);
151MODULE_PARM_DESC(ata_probe_timeout, "Set ATA probing timeout (seconds)");
152
Jeff Garzik6ebe9d82007-10-05 16:28:36 -0400153int libata_noacpi = 0;
Jeff Garzikd7d0dad2007-03-28 01:57:37 -0400154module_param_named(noacpi, libata_noacpi, int, 0444);
Evgeni Golovad5d8ea2009-06-22 09:46:20 +0200155MODULE_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 -0700156
Alan Coxae8d4ee2007-11-04 22:05:49 -0500157int libata_allow_tpm = 0;
158module_param_named(allow_tpm, libata_allow_tpm, int, 0444);
Evgeni Golovad5d8ea2009-06-22 09:46:20 +0200159MODULE_PARM_DESC(allow_tpm, "Permit the use of TPM commands (0=off [default], 1=on)");
Alan Coxae8d4ee2007-11-04 22:05:49 -0500160
Tejun Heoe7ecd432010-05-19 15:38:58 +0200161static int atapi_an;
162module_param(atapi_an, int, 0444);
163MODULE_PARM_DESC(atapi_an, "Enable ATAPI AN media presence notification (0=0ff [default], 1=on)");
164
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165MODULE_AUTHOR("Jeff Garzik");
166MODULE_DESCRIPTION("Library module for ATA devices");
167MODULE_LICENSE("GPL");
168MODULE_VERSION(DRV_VERSION);
169
Edward Falk0baab862005-06-02 18:17:13 -0400170
Tejun Heo9913ff8a2009-01-29 20:31:31 +0900171static bool ata_sstatus_online(u32 sstatus)
172{
173 return (sstatus & 0xf) == 0x3;
174}
175
Tejun Heo1eca4362008-11-03 20:03:17 +0900176/**
177 * ata_link_next - link iteration helper
178 * @link: the previous link, NULL to start
179 * @ap: ATA port containing links to iterate
180 * @mode: iteration mode, one of ATA_LITER_*
Tejun Heoaadffb62008-07-31 17:02:41 +0900181 *
Tejun Heo1eca4362008-11-03 20:03:17 +0900182 * LOCKING:
183 * Host lock or EH context.
184 *
185 * RETURNS:
186 * Pointer to the next link.
Tejun Heoaadffb62008-07-31 17:02:41 +0900187 */
Tejun Heo1eca4362008-11-03 20:03:17 +0900188struct ata_link *ata_link_next(struct ata_link *link, struct ata_port *ap,
189 enum ata_link_iter_mode mode)
Tejun Heoaadffb62008-07-31 17:02:41 +0900190{
Tejun Heo1eca4362008-11-03 20:03:17 +0900191 BUG_ON(mode != ATA_LITER_EDGE &&
192 mode != ATA_LITER_PMP_FIRST && mode != ATA_LITER_HOST_FIRST);
Tejun Heoaadffb62008-07-31 17:02:41 +0900193
Tejun Heo1eca4362008-11-03 20:03:17 +0900194 /* NULL link indicates start of iteration */
195 if (!link)
196 switch (mode) {
197 case ATA_LITER_EDGE:
198 case ATA_LITER_PMP_FIRST:
199 if (sata_pmp_attached(ap))
200 return ap->pmp_link;
201 /* fall through */
202 case ATA_LITER_HOST_FIRST:
203 return &ap->link;
204 }
205
206 /* we just iterated over the host link, what's next? */
207 if (link == &ap->link)
208 switch (mode) {
209 case ATA_LITER_HOST_FIRST:
210 if (sata_pmp_attached(ap))
211 return ap->pmp_link;
212 /* fall through */
213 case ATA_LITER_PMP_FIRST:
214 if (unlikely(ap->slave_link))
Tejun Heob1c72912008-07-31 17:02:43 +0900215 return ap->slave_link;
Tejun Heo1eca4362008-11-03 20:03:17 +0900216 /* fall through */
217 case ATA_LITER_EDGE:
Tejun Heoaadffb62008-07-31 17:02:41 +0900218 return NULL;
Tejun Heob1c72912008-07-31 17:02:43 +0900219 }
Tejun Heoaadffb62008-07-31 17:02:41 +0900220
Tejun Heob1c72912008-07-31 17:02:43 +0900221 /* slave_link excludes PMP */
222 if (unlikely(link == ap->slave_link))
223 return NULL;
224
Tejun Heo1eca4362008-11-03 20:03:17 +0900225 /* we were over a PMP link */
Tejun Heoaadffb62008-07-31 17:02:41 +0900226 if (++link < ap->pmp_link + ap->nr_pmp_links)
227 return link;
Tejun Heo1eca4362008-11-03 20:03:17 +0900228
229 if (mode == ATA_LITER_PMP_FIRST)
230 return &ap->link;
231
Tejun Heoaadffb62008-07-31 17:02:41 +0900232 return NULL;
233}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +0100234EXPORT_SYMBOL_GPL(ata_link_next);
Tejun Heoaadffb62008-07-31 17:02:41 +0900235
Edward Falk0baab862005-06-02 18:17:13 -0400236/**
Tejun Heo1eca4362008-11-03 20:03:17 +0900237 * ata_dev_next - device iteration helper
238 * @dev: the previous device, NULL to start
239 * @link: ATA link containing devices to iterate
240 * @mode: iteration mode, one of ATA_DITER_*
241 *
242 * LOCKING:
243 * Host lock or EH context.
244 *
245 * RETURNS:
246 * Pointer to the next device.
247 */
248struct ata_device *ata_dev_next(struct ata_device *dev, struct ata_link *link,
249 enum ata_dev_iter_mode mode)
250{
251 BUG_ON(mode != ATA_DITER_ENABLED && mode != ATA_DITER_ENABLED_REVERSE &&
252 mode != ATA_DITER_ALL && mode != ATA_DITER_ALL_REVERSE);
253
254 /* NULL dev indicates start of iteration */
255 if (!dev)
256 switch (mode) {
257 case ATA_DITER_ENABLED:
258 case ATA_DITER_ALL:
259 dev = link->device;
260 goto check;
261 case ATA_DITER_ENABLED_REVERSE:
262 case ATA_DITER_ALL_REVERSE:
263 dev = link->device + ata_link_max_devices(link) - 1;
264 goto check;
265 }
266
267 next:
268 /* move to the next one */
269 switch (mode) {
270 case ATA_DITER_ENABLED:
271 case ATA_DITER_ALL:
272 if (++dev < link->device + ata_link_max_devices(link))
273 goto check;
274 return NULL;
275 case ATA_DITER_ENABLED_REVERSE:
276 case ATA_DITER_ALL_REVERSE:
277 if (--dev >= link->device)
278 goto check;
279 return NULL;
280 }
281
282 check:
283 if ((mode == ATA_DITER_ENABLED || mode == ATA_DITER_ENABLED_REVERSE) &&
284 !ata_dev_enabled(dev))
285 goto next;
286 return dev;
287}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +0100288EXPORT_SYMBOL_GPL(ata_dev_next);
Tejun Heo1eca4362008-11-03 20:03:17 +0900289
290/**
Tejun Heob1c72912008-07-31 17:02:43 +0900291 * ata_dev_phys_link - find physical link for a device
292 * @dev: ATA device to look up physical link for
293 *
294 * Look up physical link which @dev is attached to. Note that
295 * this is different from @dev->link only when @dev is on slave
296 * link. For all other cases, it's the same as @dev->link.
297 *
298 * LOCKING:
299 * Don't care.
300 *
301 * RETURNS:
302 * Pointer to the found physical link.
303 */
304struct ata_link *ata_dev_phys_link(struct ata_device *dev)
305{
306 struct ata_port *ap = dev->link->ap;
307
308 if (!ap->slave_link)
309 return dev->link;
310 if (!dev->devno)
311 return &ap->link;
312 return ap->slave_link;
313}
314
315/**
Tejun Heo33267322008-02-13 09:15:09 +0900316 * ata_force_cbl - force cable type according to libata.force
Randy Dunlap4cdfa1b2008-02-22 12:21:37 -0800317 * @ap: ATA port of interest
Tejun Heo33267322008-02-13 09:15:09 +0900318 *
319 * Force cable type according to libata.force and whine about it.
320 * The last entry which has matching port number is used, so it
321 * can be specified as part of device force parameters. For
322 * example, both "a:40c,1.00:udma4" and "1.00:40c,udma4" have the
323 * same effect.
324 *
325 * LOCKING:
326 * EH context.
327 */
328void ata_force_cbl(struct ata_port *ap)
329{
330 int i;
331
332 for (i = ata_force_tbl_size - 1; i >= 0; i--) {
333 const struct ata_force_ent *fe = &ata_force_tbl[i];
334
335 if (fe->port != -1 && fe->port != ap->print_id)
336 continue;
337
338 if (fe->param.cbl == ATA_CBL_NONE)
339 continue;
340
341 ap->cbl = fe->param.cbl;
Joe Perchesa9a79df2011-04-15 15:51:59 -0700342 ata_port_notice(ap, "FORCE: cable set to %s\n", fe->param.name);
Tejun Heo33267322008-02-13 09:15:09 +0900343 return;
344 }
345}
346
347/**
Tejun Heo05944bd2008-08-13 20:19:09 +0900348 * ata_force_link_limits - force link limits according to libata.force
Tejun Heo33267322008-02-13 09:15:09 +0900349 * @link: ATA link of interest
350 *
Tejun Heo05944bd2008-08-13 20:19:09 +0900351 * Force link flags and SATA spd limit according to libata.force
352 * and whine about it. When only the port part is specified
353 * (e.g. 1:), the limit applies to all links connected to both
354 * the host link and all fan-out ports connected via PMP. If the
355 * device part is specified as 0 (e.g. 1.00:), it specifies the
356 * first fan-out link not the host link. Device number 15 always
Tejun Heob1c72912008-07-31 17:02:43 +0900357 * points to the host link whether PMP is attached or not. If the
358 * controller has slave link, device number 16 points to it.
Tejun Heo33267322008-02-13 09:15:09 +0900359 *
360 * LOCKING:
361 * EH context.
362 */
Tejun Heo05944bd2008-08-13 20:19:09 +0900363static void ata_force_link_limits(struct ata_link *link)
Tejun Heo33267322008-02-13 09:15:09 +0900364{
Tejun Heo05944bd2008-08-13 20:19:09 +0900365 bool did_spd = false;
Tejun Heob1c72912008-07-31 17:02:43 +0900366 int linkno = link->pmp;
367 int i;
Tejun Heo33267322008-02-13 09:15:09 +0900368
369 if (ata_is_host_link(link))
Tejun Heob1c72912008-07-31 17:02:43 +0900370 linkno += 15;
Tejun Heo33267322008-02-13 09:15:09 +0900371
372 for (i = ata_force_tbl_size - 1; i >= 0; i--) {
373 const struct ata_force_ent *fe = &ata_force_tbl[i];
374
375 if (fe->port != -1 && fe->port != link->ap->print_id)
376 continue;
377
378 if (fe->device != -1 && fe->device != linkno)
379 continue;
380
Tejun Heo05944bd2008-08-13 20:19:09 +0900381 /* only honor the first spd limit */
382 if (!did_spd && fe->param.spd_limit) {
383 link->hw_sata_spd_limit = (1 << fe->param.spd_limit) - 1;
Joe Perchesa9a79df2011-04-15 15:51:59 -0700384 ata_link_notice(link, "FORCE: PHY spd limit set to %s\n",
Tejun Heo05944bd2008-08-13 20:19:09 +0900385 fe->param.name);
386 did_spd = true;
387 }
Tejun Heo33267322008-02-13 09:15:09 +0900388
Tejun Heo05944bd2008-08-13 20:19:09 +0900389 /* let lflags stack */
390 if (fe->param.lflags) {
391 link->flags |= fe->param.lflags;
Joe Perchesa9a79df2011-04-15 15:51:59 -0700392 ata_link_notice(link,
Tejun Heo05944bd2008-08-13 20:19:09 +0900393 "FORCE: link flag 0x%x forced -> 0x%x\n",
394 fe->param.lflags, link->flags);
395 }
Tejun Heo33267322008-02-13 09:15:09 +0900396 }
397}
398
399/**
400 * ata_force_xfermask - force xfermask according to libata.force
401 * @dev: ATA device of interest
402 *
403 * Force xfer_mask according to libata.force and whine about it.
404 * For consistency with link selection, device number 15 selects
405 * the first device connected to the host link.
406 *
407 * LOCKING:
408 * EH context.
409 */
410static void ata_force_xfermask(struct ata_device *dev)
411{
412 int devno = dev->link->pmp + dev->devno;
413 int alt_devno = devno;
414 int i;
415
Tejun Heob1c72912008-07-31 17:02:43 +0900416 /* allow n.15/16 for devices attached to host port */
417 if (ata_is_host_link(dev->link))
418 alt_devno += 15;
Tejun Heo33267322008-02-13 09:15:09 +0900419
420 for (i = ata_force_tbl_size - 1; i >= 0; i--) {
421 const struct ata_force_ent *fe = &ata_force_tbl[i];
422 unsigned long pio_mask, mwdma_mask, udma_mask;
423
424 if (fe->port != -1 && fe->port != dev->link->ap->print_id)
425 continue;
426
427 if (fe->device != -1 && fe->device != devno &&
428 fe->device != alt_devno)
429 continue;
430
431 if (!fe->param.xfer_mask)
432 continue;
433
434 ata_unpack_xfermask(fe->param.xfer_mask,
435 &pio_mask, &mwdma_mask, &udma_mask);
436 if (udma_mask)
437 dev->udma_mask = udma_mask;
438 else if (mwdma_mask) {
439 dev->udma_mask = 0;
440 dev->mwdma_mask = mwdma_mask;
441 } else {
442 dev->udma_mask = 0;
443 dev->mwdma_mask = 0;
444 dev->pio_mask = pio_mask;
445 }
446
Joe Perchesa9a79df2011-04-15 15:51:59 -0700447 ata_dev_notice(dev, "FORCE: xfer_mask set to %s\n",
448 fe->param.name);
Tejun Heo33267322008-02-13 09:15:09 +0900449 return;
450 }
451}
452
453/**
454 * ata_force_horkage - force horkage according to libata.force
455 * @dev: ATA device of interest
456 *
457 * Force horkage according to libata.force and whine about it.
458 * For consistency with link selection, device number 15 selects
459 * the first device connected to the host link.
460 *
461 * LOCKING:
462 * EH context.
463 */
464static void ata_force_horkage(struct ata_device *dev)
465{
466 int devno = dev->link->pmp + dev->devno;
467 int alt_devno = devno;
468 int i;
469
Tejun Heob1c72912008-07-31 17:02:43 +0900470 /* allow n.15/16 for devices attached to host port */
471 if (ata_is_host_link(dev->link))
472 alt_devno += 15;
Tejun Heo33267322008-02-13 09:15:09 +0900473
474 for (i = 0; i < ata_force_tbl_size; i++) {
475 const struct ata_force_ent *fe = &ata_force_tbl[i];
476
477 if (fe->port != -1 && fe->port != dev->link->ap->print_id)
478 continue;
479
480 if (fe->device != -1 && fe->device != devno &&
481 fe->device != alt_devno)
482 continue;
483
484 if (!(~dev->horkage & fe->param.horkage_on) &&
485 !(dev->horkage & fe->param.horkage_off))
486 continue;
487
488 dev->horkage |= fe->param.horkage_on;
489 dev->horkage &= ~fe->param.horkage_off;
490
Joe Perchesa9a79df2011-04-15 15:51:59 -0700491 ata_dev_notice(dev, "FORCE: horkage modified (%s)\n",
492 fe->param.name);
Tejun Heo33267322008-02-13 09:15:09 +0900493 }
494}
495
496/**
Tejun Heo436d34b2008-04-02 17:28:46 +0900497 * atapi_cmd_type - Determine ATAPI command type from SCSI opcode
498 * @opcode: SCSI opcode
499 *
500 * Determine ATAPI command type from @opcode.
501 *
502 * LOCKING:
503 * None.
504 *
505 * RETURNS:
506 * ATAPI_{READ|WRITE|READ_CD|PASS_THRU|MISC}
507 */
508int atapi_cmd_type(u8 opcode)
509{
510 switch (opcode) {
511 case GPCMD_READ_10:
512 case GPCMD_READ_12:
513 return ATAPI_READ;
514
515 case GPCMD_WRITE_10:
516 case GPCMD_WRITE_12:
517 case GPCMD_WRITE_AND_VERIFY_10:
518 return ATAPI_WRITE;
519
520 case GPCMD_READ_CD:
521 case GPCMD_READ_CD_MSF:
522 return ATAPI_READ_CD;
523
Tejun Heoe52dcc42008-04-02 17:35:19 +0900524 case ATA_16:
525 case ATA_12:
526 if (atapi_passthru16)
527 return ATAPI_PASS_THRU;
528 /* fall thru */
Tejun Heo436d34b2008-04-02 17:28:46 +0900529 default:
530 return ATAPI_MISC;
531 }
532}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +0100533EXPORT_SYMBOL_GPL(atapi_cmd_type);
Tejun Heo436d34b2008-04-02 17:28:46 +0900534
535/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 * ata_tf_to_fis - Convert ATA taskfile to SATA FIS structure
537 * @tf: Taskfile to convert
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 * @pmp: Port multiplier port
Tejun Heo99771262007-07-16 14:29:38 +0900539 * @is_cmd: This FIS is for command
540 * @fis: Buffer into which data will output
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 *
542 * Converts a standard ATA taskfile to a Serial ATA
543 * FIS structure (Register - Host to Device).
544 *
545 * LOCKING:
546 * Inherited from caller.
547 */
Tejun Heo99771262007-07-16 14:29:38 +0900548void ata_tf_to_fis(const struct ata_taskfile *tf, u8 pmp, int is_cmd, u8 *fis)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549{
Tejun Heo99771262007-07-16 14:29:38 +0900550 fis[0] = 0x27; /* Register - Host to Device FIS */
551 fis[1] = pmp & 0xf; /* Port multiplier number*/
552 if (is_cmd)
553 fis[1] |= (1 << 7); /* bit 7 indicates Command FIS */
554
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 fis[2] = tf->command;
556 fis[3] = tf->feature;
557
558 fis[4] = tf->lbal;
559 fis[5] = tf->lbam;
560 fis[6] = tf->lbah;
561 fis[7] = tf->device;
562
563 fis[8] = tf->hob_lbal;
564 fis[9] = tf->hob_lbam;
565 fis[10] = tf->hob_lbah;
566 fis[11] = tf->hob_feature;
567
568 fis[12] = tf->nsect;
569 fis[13] = tf->hob_nsect;
570 fis[14] = 0;
571 fis[15] = tf->ctl;
572
Marc Carino86a565e2013-09-01 08:59:01 -0700573 fis[16] = tf->auxiliary & 0xff;
574 fis[17] = (tf->auxiliary >> 8) & 0xff;
575 fis[18] = (tf->auxiliary >> 16) & 0xff;
576 fis[19] = (tf->auxiliary >> 24) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +0100578EXPORT_SYMBOL_GPL(ata_tf_to_fis);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
580/**
581 * ata_tf_from_fis - Convert SATA FIS to ATA taskfile
582 * @fis: Buffer from which data will be input
583 * @tf: Taskfile to output
584 *
Mark Lorde12a1be2005-11-12 18:55:45 -0500585 * Converts a serial ATA FIS structure to a standard ATA taskfile.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 *
587 * LOCKING:
588 * Inherited from caller.
589 */
590
Jeff Garzik057ace52005-10-22 14:27:05 -0400591void ata_tf_from_fis(const u8 *fis, struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592{
593 tf->command = fis[2]; /* status */
594 tf->feature = fis[3]; /* error */
595
596 tf->lbal = fis[4];
597 tf->lbam = fis[5];
598 tf->lbah = fis[6];
599 tf->device = fis[7];
600
601 tf->hob_lbal = fis[8];
602 tf->hob_lbam = fis[9];
603 tf->hob_lbah = fis[10];
604
605 tf->nsect = fis[12];
606 tf->hob_nsect = fis[13];
607}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +0100608EXPORT_SYMBOL_GPL(ata_tf_from_fis);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
Albert Lee8cbd6df2005-10-12 15:06:27 +0800610static const u8 ata_rw_cmds[] = {
611 /* pio multi */
612 ATA_CMD_READ_MULTI,
613 ATA_CMD_WRITE_MULTI,
614 ATA_CMD_READ_MULTI_EXT,
615 ATA_CMD_WRITE_MULTI_EXT,
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100616 0,
617 0,
618 0,
619 ATA_CMD_WRITE_MULTI_FUA_EXT,
Albert Lee8cbd6df2005-10-12 15:06:27 +0800620 /* pio */
621 ATA_CMD_PIO_READ,
622 ATA_CMD_PIO_WRITE,
623 ATA_CMD_PIO_READ_EXT,
624 ATA_CMD_PIO_WRITE_EXT,
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100625 0,
626 0,
627 0,
628 0,
Albert Lee8cbd6df2005-10-12 15:06:27 +0800629 /* dma */
630 ATA_CMD_READ,
631 ATA_CMD_WRITE,
632 ATA_CMD_READ_EXT,
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100633 ATA_CMD_WRITE_EXT,
634 0,
635 0,
636 0,
637 ATA_CMD_WRITE_FUA_EXT
Albert Lee8cbd6df2005-10-12 15:06:27 +0800638};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
640/**
Albert Lee8cbd6df2005-10-12 15:06:27 +0800641 * ata_rwcmd_protocol - set taskfile r/w commands and protocol
Tejun Heobd056d72006-11-14 22:47:10 +0900642 * @tf: command to examine and configure
643 * @dev: device tf belongs to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 *
Jeff Garzik2e9edbf2006-03-24 09:56:57 -0500645 * Examine the device configuration and tf->flags to calculate
Albert Lee8cbd6df2005-10-12 15:06:27 +0800646 * the proper read/write commands and protocol to use.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 *
648 * LOCKING:
649 * caller.
650 */
Tejun Heobd056d72006-11-14 22:47:10 +0900651static int ata_rwcmd_protocol(struct ata_taskfile *tf, struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652{
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100653 u8 cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100655 int index, fua, lba48, write;
Jeff Garzik2e9edbf2006-03-24 09:56:57 -0500656
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100657 fua = (tf->flags & ATA_TFLAG_FUA) ? 4 : 0;
Albert Lee8cbd6df2005-10-12 15:06:27 +0800658 lba48 = (tf->flags & ATA_TFLAG_LBA48) ? 2 : 0;
659 write = (tf->flags & ATA_TFLAG_WRITE) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
Albert Lee8cbd6df2005-10-12 15:06:27 +0800661 if (dev->flags & ATA_DFLAG_PIO) {
662 tf->protocol = ATA_PROT_PIO;
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100663 index = dev->multi_count ? 0 : 8;
Tejun Heo9af5c9c2007-08-06 18:36:22 +0900664 } else if (lba48 && (dev->link->ap->flags & ATA_FLAG_PIO_LBA48)) {
Alan Cox8d238e02006-01-17 20:50:31 +0000665 /* Unable to use DMA due to host limitation */
666 tf->protocol = ATA_PROT_PIO;
Albert Lee0565c262006-02-13 18:55:25 +0800667 index = dev->multi_count ? 0 : 8;
Albert Lee8cbd6df2005-10-12 15:06:27 +0800668 } else {
669 tf->protocol = ATA_PROT_DMA;
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100670 index = 16;
Albert Lee8cbd6df2005-10-12 15:06:27 +0800671 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100673 cmd = ata_rw_cmds[index + fua + lba48 + write];
674 if (cmd) {
675 tf->command = cmd;
676 return 0;
677 }
678 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679}
680
Tejun Heocb95d562006-03-06 04:31:56 +0900681/**
Tejun Heo35b649f2006-11-14 22:37:35 +0900682 * ata_tf_read_block - Read block address from ATA taskfile
683 * @tf: ATA taskfile of interest
684 * @dev: ATA device @tf belongs to
685 *
686 * LOCKING:
687 * None.
688 *
689 * Read block address from @tf. This function can handle all
690 * three address formats - LBA, LBA48 and CHS. tf->protocol and
691 * flags select the address format to use.
692 *
693 * RETURNS:
694 * Block address read from @tf.
695 */
Hannes Reineckecffd1ee2016-04-04 11:43:57 +0200696u64 ata_tf_read_block(const struct ata_taskfile *tf, struct ata_device *dev)
Tejun Heo35b649f2006-11-14 22:37:35 +0900697{
698 u64 block = 0;
699
Tejun Heofe16d4f2015-08-03 11:41:33 -0400700 if (tf->flags & ATA_TFLAG_LBA) {
Tejun Heo35b649f2006-11-14 22:37:35 +0900701 if (tf->flags & ATA_TFLAG_LBA48) {
702 block |= (u64)tf->hob_lbah << 40;
703 block |= (u64)tf->hob_lbam << 32;
Roland Dreier44901a92008-11-04 10:34:48 -0800704 block |= (u64)tf->hob_lbal << 24;
Tejun Heo35b649f2006-11-14 22:37:35 +0900705 } else
706 block |= (tf->device & 0xf) << 24;
707
708 block |= tf->lbah << 16;
709 block |= tf->lbam << 8;
710 block |= tf->lbal;
711 } else {
712 u32 cyl, head, sect;
713
714 cyl = tf->lbam | (tf->lbah << 8);
715 head = tf->device & 0xf;
716 sect = tf->lbal;
717
Tejun Heoac8672e2009-08-16 21:21:21 +0900718 if (!sect) {
Joe Perchesa9a79df2011-04-15 15:51:59 -0700719 ata_dev_warn(dev,
720 "device reported invalid CHS sector 0\n");
Hannes Reineckecffd1ee2016-04-04 11:43:57 +0200721 return U64_MAX;
Tejun Heoac8672e2009-08-16 21:21:21 +0900722 }
723
724 block = (cyl * dev->heads + head) * dev->sectors + sect - 1;
Tejun Heo35b649f2006-11-14 22:37:35 +0900725 }
726
727 return block;
728}
729
730/**
Tejun Heobd056d72006-11-14 22:47:10 +0900731 * ata_build_rw_tf - Build ATA taskfile for given read/write request
732 * @tf: Target ATA taskfile
733 * @dev: ATA device @tf belongs to
734 * @block: Block address
735 * @n_block: Number of blocks
736 * @tf_flags: RW/FUA etc...
737 * @tag: tag
Adam Manzanares8e061782016-10-17 11:27:29 -0700738 * @class: IO priority class
Tejun Heobd056d72006-11-14 22:47:10 +0900739 *
740 * LOCKING:
741 * None.
742 *
743 * Build ATA taskfile @tf for read/write request described by
744 * @block, @n_block, @tf_flags and @tag on @dev.
745 *
746 * RETURNS:
747 *
748 * 0 on success, -ERANGE if the request is too large for @dev,
749 * -EINVAL if the request is invalid.
750 */
751int ata_build_rw_tf(struct ata_taskfile *tf, struct ata_device *dev,
752 u64 block, u32 n_block, unsigned int tf_flags,
Adam Manzanares8e061782016-10-17 11:27:29 -0700753 unsigned int tag, int class)
Tejun Heobd056d72006-11-14 22:47:10 +0900754{
755 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
756 tf->flags |= tf_flags;
757
Jens Axboe2e2cc672018-05-11 12:51:06 -0600758 if (ata_ncq_enabled(dev) && !ata_tag_internal(tag)) {
Tejun Heobd056d72006-11-14 22:47:10 +0900759 /* yay, NCQ */
760 if (!lba_48_ok(block, n_block))
761 return -ERANGE;
762
763 tf->protocol = ATA_PROT_NCQ;
764 tf->flags |= ATA_TFLAG_LBA | ATA_TFLAG_LBA48;
765
766 if (tf->flags & ATA_TFLAG_WRITE)
767 tf->command = ATA_CMD_FPDMA_WRITE;
768 else
769 tf->command = ATA_CMD_FPDMA_READ;
770
771 tf->nsect = tag << 3;
772 tf->hob_feature = (n_block >> 8) & 0xff;
773 tf->feature = n_block & 0xff;
774
775 tf->hob_lbah = (block >> 40) & 0xff;
776 tf->hob_lbam = (block >> 32) & 0xff;
777 tf->hob_lbal = (block >> 24) & 0xff;
778 tf->lbah = (block >> 16) & 0xff;
779 tf->lbam = (block >> 8) & 0xff;
780 tf->lbal = block & 0xff;
781
Sergei Shtylyov9ca7cfa2012-08-21 22:18:50 +0400782 tf->device = ATA_LBA;
Tejun Heobd056d72006-11-14 22:47:10 +0900783 if (tf->flags & ATA_TFLAG_FUA)
784 tf->device |= 1 << 7;
Adam Manzanares8e061782016-10-17 11:27:29 -0700785
Adam Manzanares9f56eca2016-12-13 12:00:05 -0800786 if (dev->flags & ATA_DFLAG_NCQ_PRIO) {
Adam Manzanares8e061782016-10-17 11:27:29 -0700787 if (class == IOPRIO_CLASS_RT)
788 tf->hob_nsect |= ATA_PRIO_HIGH <<
789 ATA_SHIFT_PRIO;
790 }
Tejun Heobd056d72006-11-14 22:47:10 +0900791 } else if (dev->flags & ATA_DFLAG_LBA) {
792 tf->flags |= ATA_TFLAG_LBA;
793
794 if (lba_28_ok(block, n_block)) {
795 /* use LBA28 */
796 tf->device |= (block >> 24) & 0xf;
797 } else if (lba_48_ok(block, n_block)) {
798 if (!(dev->flags & ATA_DFLAG_LBA48))
799 return -ERANGE;
800
801 /* use LBA48 */
802 tf->flags |= ATA_TFLAG_LBA48;
803
804 tf->hob_nsect = (n_block >> 8) & 0xff;
805
806 tf->hob_lbah = (block >> 40) & 0xff;
807 tf->hob_lbam = (block >> 32) & 0xff;
808 tf->hob_lbal = (block >> 24) & 0xff;
809 } else
810 /* request too large even for LBA48 */
811 return -ERANGE;
812
813 if (unlikely(ata_rwcmd_protocol(tf, dev) < 0))
814 return -EINVAL;
815
816 tf->nsect = n_block & 0xff;
817
818 tf->lbah = (block >> 16) & 0xff;
819 tf->lbam = (block >> 8) & 0xff;
820 tf->lbal = block & 0xff;
821
822 tf->device |= ATA_LBA;
823 } else {
824 /* CHS */
825 u32 sect, head, cyl, track;
826
827 /* The request -may- be too large for CHS addressing. */
828 if (!lba_28_ok(block, n_block))
829 return -ERANGE;
830
831 if (unlikely(ata_rwcmd_protocol(tf, dev) < 0))
832 return -EINVAL;
833
834 /* Convert LBA to CHS */
835 track = (u32)block / dev->sectors;
836 cyl = track / dev->heads;
837 head = track % dev->heads;
838 sect = (u32)block % dev->sectors + 1;
839
840 DPRINTK("block %u track %u cyl %u head %u sect %u\n",
841 (u32)block, track, cyl, head, sect);
842
843 /* Check whether the converted CHS can fit.
844 Cylinder: 0-65535
845 Head: 0-15
846 Sector: 1-255*/
847 if ((cyl >> 16) || (head >> 4) || (sect >> 8) || (!sect))
848 return -ERANGE;
849
850 tf->nsect = n_block & 0xff; /* Sector count 0 means 256 sectors */
851 tf->lbal = sect;
852 tf->lbam = cyl;
853 tf->lbah = cyl >> 8;
854 tf->device |= head;
855 }
856
857 return 0;
858}
859
860/**
Tejun Heocb95d562006-03-06 04:31:56 +0900861 * ata_pack_xfermask - Pack pio, mwdma and udma masks into xfer_mask
862 * @pio_mask: pio_mask
863 * @mwdma_mask: mwdma_mask
864 * @udma_mask: udma_mask
865 *
866 * Pack @pio_mask, @mwdma_mask and @udma_mask into a single
867 * unsigned int xfer_mask.
868 *
869 * LOCKING:
870 * None.
871 *
872 * RETURNS:
873 * Packed xfer_mask.
874 */
Tejun Heo7dc951a2007-11-27 19:43:42 +0900875unsigned long ata_pack_xfermask(unsigned long pio_mask,
876 unsigned long mwdma_mask,
877 unsigned long udma_mask)
Tejun Heocb95d562006-03-06 04:31:56 +0900878{
879 return ((pio_mask << ATA_SHIFT_PIO) & ATA_MASK_PIO) |
880 ((mwdma_mask << ATA_SHIFT_MWDMA) & ATA_MASK_MWDMA) |
881 ((udma_mask << ATA_SHIFT_UDMA) & ATA_MASK_UDMA);
882}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +0100883EXPORT_SYMBOL_GPL(ata_pack_xfermask);
Tejun Heocb95d562006-03-06 04:31:56 +0900884
Tejun Heoc0489e42006-03-24 14:07:49 +0900885/**
886 * ata_unpack_xfermask - Unpack xfer_mask into pio, mwdma and udma masks
887 * @xfer_mask: xfer_mask to unpack
888 * @pio_mask: resulting pio_mask
889 * @mwdma_mask: resulting mwdma_mask
890 * @udma_mask: resulting udma_mask
891 *
892 * Unpack @xfer_mask into @pio_mask, @mwdma_mask and @udma_mask.
Masanari Iidac9b55602016-04-13 23:36:27 +0900893 * Any NULL destination masks will be ignored.
Tejun Heoc0489e42006-03-24 14:07:49 +0900894 */
Tejun Heo7dc951a2007-11-27 19:43:42 +0900895void ata_unpack_xfermask(unsigned long xfer_mask, unsigned long *pio_mask,
896 unsigned long *mwdma_mask, unsigned long *udma_mask)
Tejun Heoc0489e42006-03-24 14:07:49 +0900897{
898 if (pio_mask)
899 *pio_mask = (xfer_mask & ATA_MASK_PIO) >> ATA_SHIFT_PIO;
900 if (mwdma_mask)
901 *mwdma_mask = (xfer_mask & ATA_MASK_MWDMA) >> ATA_SHIFT_MWDMA;
902 if (udma_mask)
903 *udma_mask = (xfer_mask & ATA_MASK_UDMA) >> ATA_SHIFT_UDMA;
904}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +0100905EXPORT_SYMBOL_GPL(ata_unpack_xfermask);
Tejun Heoc0489e42006-03-24 14:07:49 +0900906
Tejun Heocb95d562006-03-06 04:31:56 +0900907static const struct ata_xfer_ent {
Tejun Heobe9a50c2006-03-31 22:48:52 +0900908 int shift, bits;
Tejun Heocb95d562006-03-06 04:31:56 +0900909 u8 base;
910} ata_xfer_tbl[] = {
Tejun Heo70cd0712007-11-27 19:43:40 +0900911 { ATA_SHIFT_PIO, ATA_NR_PIO_MODES, XFER_PIO_0 },
912 { ATA_SHIFT_MWDMA, ATA_NR_MWDMA_MODES, XFER_MW_DMA_0 },
913 { ATA_SHIFT_UDMA, ATA_NR_UDMA_MODES, XFER_UDMA_0 },
Tejun Heocb95d562006-03-06 04:31:56 +0900914 { -1, },
915};
916
917/**
918 * ata_xfer_mask2mode - Find matching XFER_* for the given xfer_mask
919 * @xfer_mask: xfer_mask of interest
920 *
921 * Return matching XFER_* value for @xfer_mask. Only the highest
922 * bit of @xfer_mask is considered.
923 *
924 * LOCKING:
925 * None.
926 *
927 * RETURNS:
Tejun Heo70cd0712007-11-27 19:43:40 +0900928 * Matching XFER_* value, 0xff if no match found.
Tejun Heocb95d562006-03-06 04:31:56 +0900929 */
Tejun Heo7dc951a2007-11-27 19:43:42 +0900930u8 ata_xfer_mask2mode(unsigned long xfer_mask)
Tejun Heocb95d562006-03-06 04:31:56 +0900931{
932 int highbit = fls(xfer_mask) - 1;
933 const struct ata_xfer_ent *ent;
934
935 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
936 if (highbit >= ent->shift && highbit < ent->shift + ent->bits)
937 return ent->base + highbit - ent->shift;
Tejun Heo70cd0712007-11-27 19:43:40 +0900938 return 0xff;
Tejun Heocb95d562006-03-06 04:31:56 +0900939}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +0100940EXPORT_SYMBOL_GPL(ata_xfer_mask2mode);
Tejun Heocb95d562006-03-06 04:31:56 +0900941
942/**
943 * ata_xfer_mode2mask - Find matching xfer_mask for XFER_*
944 * @xfer_mode: XFER_* of interest
945 *
946 * Return matching xfer_mask for @xfer_mode.
947 *
948 * LOCKING:
949 * None.
950 *
951 * RETURNS:
952 * Matching xfer_mask, 0 if no match found.
953 */
Tejun Heo7dc951a2007-11-27 19:43:42 +0900954unsigned long ata_xfer_mode2mask(u8 xfer_mode)
Tejun Heocb95d562006-03-06 04:31:56 +0900955{
956 const struct ata_xfer_ent *ent;
957
958 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
959 if (xfer_mode >= ent->base && xfer_mode < ent->base + ent->bits)
Tejun Heo70cd0712007-11-27 19:43:40 +0900960 return ((2 << (ent->shift + xfer_mode - ent->base)) - 1)
961 & ~((1 << ent->shift) - 1);
Tejun Heocb95d562006-03-06 04:31:56 +0900962 return 0;
963}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +0100964EXPORT_SYMBOL_GPL(ata_xfer_mode2mask);
Tejun Heocb95d562006-03-06 04:31:56 +0900965
966/**
967 * ata_xfer_mode2shift - Find matching xfer_shift for XFER_*
968 * @xfer_mode: XFER_* of interest
969 *
970 * Return matching xfer_shift for @xfer_mode.
971 *
972 * LOCKING:
973 * None.
974 *
975 * RETURNS:
976 * Matching xfer_shift, -1 if no match found.
977 */
Tejun Heo7dc951a2007-11-27 19:43:42 +0900978int ata_xfer_mode2shift(unsigned long xfer_mode)
Tejun Heocb95d562006-03-06 04:31:56 +0900979{
980 const struct ata_xfer_ent *ent;
981
982 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
983 if (xfer_mode >= ent->base && xfer_mode < ent->base + ent->bits)
984 return ent->shift;
985 return -1;
986}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +0100987EXPORT_SYMBOL_GPL(ata_xfer_mode2shift);
Tejun Heocb95d562006-03-06 04:31:56 +0900988
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989/**
Tejun Heo1da7b0d2006-03-06 04:31:56 +0900990 * ata_mode_string - convert xfer_mask to string
991 * @xfer_mask: mask of bits supported; only highest bit counts.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 *
993 * Determine string which represents the highest speed
Tejun Heo1da7b0d2006-03-06 04:31:56 +0900994 * (highest bit in @modemask).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 *
996 * LOCKING:
997 * None.
998 *
999 * RETURNS:
1000 * Constant C string representing highest speed listed in
Tejun Heo1da7b0d2006-03-06 04:31:56 +09001001 * @mode_mask, or the constant C string "<n/a>".
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 */
Tejun Heo7dc951a2007-11-27 19:43:42 +09001003const char *ata_mode_string(unsigned long xfer_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004{
Tejun Heo75f554b2006-03-06 04:31:57 +09001005 static const char * const xfer_mode_str[] = {
1006 "PIO0",
1007 "PIO1",
1008 "PIO2",
1009 "PIO3",
1010 "PIO4",
Alan Coxb352e572006-08-10 18:52:12 +01001011 "PIO5",
1012 "PIO6",
Tejun Heo75f554b2006-03-06 04:31:57 +09001013 "MWDMA0",
1014 "MWDMA1",
1015 "MWDMA2",
Alan Coxb352e572006-08-10 18:52:12 +01001016 "MWDMA3",
1017 "MWDMA4",
Tejun Heo75f554b2006-03-06 04:31:57 +09001018 "UDMA/16",
1019 "UDMA/25",
1020 "UDMA/33",
1021 "UDMA/44",
1022 "UDMA/66",
1023 "UDMA/100",
1024 "UDMA/133",
1025 "UDMA7",
1026 };
Tejun Heo1da7b0d2006-03-06 04:31:56 +09001027 int highbit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028
Tejun Heo1da7b0d2006-03-06 04:31:56 +09001029 highbit = fls(xfer_mask) - 1;
1030 if (highbit >= 0 && highbit < ARRAY_SIZE(xfer_mode_str))
1031 return xfer_mode_str[highbit];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 return "<n/a>";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01001034EXPORT_SYMBOL_GPL(ata_mode_string);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035
Gwendal Grignoud9027472010-05-25 12:31:38 -07001036const char *sata_spd_string(unsigned int spd)
Tejun Heo4c360c82006-04-01 01:38:17 +09001037{
1038 static const char * const spd_str[] = {
1039 "1.5 Gbps",
1040 "3.0 Gbps",
Shane Huang8522ee22008-12-30 11:00:37 +08001041 "6.0 Gbps",
Tejun Heo4c360c82006-04-01 01:38:17 +09001042 };
1043
1044 if (spd == 0 || (spd - 1) >= ARRAY_SIZE(spd_str))
1045 return "<unknown>";
1046 return spd_str[spd - 1];
1047}
1048
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049/**
1050 * ata_dev_classify - determine device type based on ATA-spec signature
1051 * @tf: ATA taskfile register set for device to be identified
1052 *
1053 * Determine from taskfile register contents whether a device is
1054 * ATA or ATAPI, as per "Signature and persistence" section
1055 * of ATA/PI spec (volume 1, sect 5.14).
1056 *
1057 * LOCKING:
1058 * None.
1059 *
1060 * RETURNS:
Hannes Reinecke9162c652014-11-05 13:08:21 +01001061 * Device type, %ATA_DEV_ATA, %ATA_DEV_ATAPI, %ATA_DEV_PMP,
1062 * %ATA_DEV_ZAC, or %ATA_DEV_UNKNOWN the event of failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 */
Jeff Garzik057ace52005-10-22 14:27:05 -04001064unsigned int ata_dev_classify(const struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065{
1066 /* Apple's open source Darwin code hints that some devices only
1067 * put a proper signature into the LBA mid/high registers,
1068 * So, we only check those. It's sufficient for uniqueness.
Tejun Heo633273a2007-09-23 13:19:54 +09001069 *
1070 * ATA/ATAPI-7 (d1532v1r1: Feb. 19, 2003) specified separate
1071 * signatures for ATA and ATAPI devices attached on SerialATA,
1072 * 0x3c/0xc3 and 0x69/0x96 respectively. However, SerialATA
1073 * spec has never mentioned about using different signatures
1074 * for ATA/ATAPI devices. Then, Serial ATA II: Port
1075 * Multiplier specification began to use 0x69/0x96 to identify
1076 * port multpliers and 0x3c/0xc3 to identify SEMB device.
1077 * ATA/ATAPI-7 dropped descriptions about 0x3c/0xc3 and
1078 * 0x69/0x96 shortly and described them as reserved for
1079 * SerialATA.
1080 *
1081 * We follow the current spec and consider that 0x69/0x96
1082 * identifies a port multiplier and 0x3c/0xc3 a SEMB device.
Tejun Heo79b42ba2009-04-15 06:21:10 +09001083 * Unfortunately, WDC WD1600JS-62MHB5 (a hard drive) reports
1084 * SEMB signature. This is worked around in
1085 * ata_dev_read_id().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 */
Tejun Heo633273a2007-09-23 13:19:54 +09001087 if ((tf->lbam == 0) && (tf->lbah == 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 DPRINTK("found ATA device by sig\n");
1089 return ATA_DEV_ATA;
1090 }
1091
Tejun Heo633273a2007-09-23 13:19:54 +09001092 if ((tf->lbam == 0x14) && (tf->lbah == 0xeb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 DPRINTK("found ATAPI device by sig\n");
1094 return ATA_DEV_ATAPI;
1095 }
1096
Tejun Heo633273a2007-09-23 13:19:54 +09001097 if ((tf->lbam == 0x69) && (tf->lbah == 0x96)) {
1098 DPRINTK("found PMP device by sig\n");
1099 return ATA_DEV_PMP;
1100 }
1101
1102 if ((tf->lbam == 0x3c) && (tf->lbah == 0xc3)) {
Tejun Heo79b42ba2009-04-15 06:21:10 +09001103 DPRINTK("found SEMB device by sig (could be ATA device)\n");
1104 return ATA_DEV_SEMB;
Tejun Heo633273a2007-09-23 13:19:54 +09001105 }
1106
Hannes Reinecke9162c652014-11-05 13:08:21 +01001107 if ((tf->lbam == 0xcd) && (tf->lbah == 0xab)) {
1108 DPRINTK("found ZAC device by sig\n");
1109 return ATA_DEV_ZAC;
1110 }
1111
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 DPRINTK("unknown device\n");
1113 return ATA_DEV_UNKNOWN;
1114}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01001115EXPORT_SYMBOL_GPL(ata_dev_classify);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116
1117/**
Tejun Heo6a62a042006-02-13 10:02:46 +09001118 * ata_id_string - Convert IDENTIFY DEVICE page into string
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 * @id: IDENTIFY DEVICE results we will examine
1120 * @s: string into which data is output
1121 * @ofs: offset into identify device page
1122 * @len: length of string to return. must be an even number.
1123 *
1124 * The strings in the IDENTIFY DEVICE page are broken up into
1125 * 16-bit chunks. Run through the string, and output each
1126 * 8-bit chunk linearly, regardless of platform.
1127 *
1128 * LOCKING:
1129 * caller.
1130 */
1131
Tejun Heo6a62a042006-02-13 10:02:46 +09001132void ata_id_string(const u16 *id, unsigned char *s,
1133 unsigned int ofs, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134{
1135 unsigned int c;
1136
Alan Cox963e4972008-07-24 17:16:06 +01001137 BUG_ON(len & 1);
1138
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 while (len > 0) {
1140 c = id[ofs] >> 8;
1141 *s = c;
1142 s++;
1143
1144 c = id[ofs] & 0xff;
1145 *s = c;
1146 s++;
1147
1148 ofs++;
1149 len -= 2;
1150 }
1151}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01001152EXPORT_SYMBOL_GPL(ata_id_string);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153
Tejun Heo0e949ff2006-02-12 22:47:04 +09001154/**
Tejun Heo6a62a042006-02-13 10:02:46 +09001155 * ata_id_c_string - Convert IDENTIFY DEVICE page into C string
Tejun Heo0e949ff2006-02-12 22:47:04 +09001156 * @id: IDENTIFY DEVICE results we will examine
1157 * @s: string into which data is output
1158 * @ofs: offset into identify device page
1159 * @len: length of string to return. must be an odd number.
1160 *
Tejun Heo6a62a042006-02-13 10:02:46 +09001161 * This function is identical to ata_id_string except that it
Tejun Heo0e949ff2006-02-12 22:47:04 +09001162 * trims trailing spaces and terminates the resulting string with
1163 * null. @len must be actual maximum length (even number) + 1.
1164 *
1165 * LOCKING:
1166 * caller.
1167 */
Tejun Heo6a62a042006-02-13 10:02:46 +09001168void ata_id_c_string(const u16 *id, unsigned char *s,
1169 unsigned int ofs, unsigned int len)
Tejun Heo0e949ff2006-02-12 22:47:04 +09001170{
1171 unsigned char *p;
1172
Tejun Heo6a62a042006-02-13 10:02:46 +09001173 ata_id_string(id, s, ofs, len - 1);
Tejun Heo0e949ff2006-02-12 22:47:04 +09001174
1175 p = s + strnlen(s, len - 1);
1176 while (p > s && p[-1] == ' ')
1177 p--;
1178 *p = '\0';
1179}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01001180EXPORT_SYMBOL_GPL(ata_id_c_string);
Edward Falk0baab862005-06-02 18:17:13 -04001181
Tejun Heodb6f8752007-09-03 12:31:58 +09001182static u64 ata_id_n_sectors(const u16 *id)
1183{
1184 if (ata_id_has_lba(id)) {
1185 if (ata_id_has_lba48(id))
Robert Hancock968e5942009-02-16 20:15:08 -06001186 return ata_id_u64(id, ATA_ID_LBA_CAPACITY_2);
Tejun Heodb6f8752007-09-03 12:31:58 +09001187 else
Robert Hancock968e5942009-02-16 20:15:08 -06001188 return ata_id_u32(id, ATA_ID_LBA_CAPACITY);
Tejun Heodb6f8752007-09-03 12:31:58 +09001189 } else {
1190 if (ata_id_current_chs_valid(id))
Robert Hancock968e5942009-02-16 20:15:08 -06001191 return id[ATA_ID_CUR_CYLS] * id[ATA_ID_CUR_HEADS] *
1192 id[ATA_ID_CUR_SECTORS];
Tejun Heodb6f8752007-09-03 12:31:58 +09001193 else
Robert Hancock968e5942009-02-16 20:15:08 -06001194 return id[ATA_ID_CYLS] * id[ATA_ID_HEADS] *
1195 id[ATA_ID_SECTORS];
Tejun Heodb6f8752007-09-03 12:31:58 +09001196 }
1197}
1198
Tejun Heoa5987e02008-03-27 19:14:23 +09001199u64 ata_tf_to_lba48(const struct ata_taskfile *tf)
Alan Cox1e999732007-04-11 00:23:13 +01001200{
1201 u64 sectors = 0;
1202
1203 sectors |= ((u64)(tf->hob_lbah & 0xff)) << 40;
1204 sectors |= ((u64)(tf->hob_lbam & 0xff)) << 32;
Roland Dreierba14a9c2008-10-28 16:52:20 -07001205 sectors |= ((u64)(tf->hob_lbal & 0xff)) << 24;
Alan Cox1e999732007-04-11 00:23:13 +01001206 sectors |= (tf->lbah & 0xff) << 16;
1207 sectors |= (tf->lbam & 0xff) << 8;
1208 sectors |= (tf->lbal & 0xff);
1209
Tejun Heoa5987e02008-03-27 19:14:23 +09001210 return sectors;
Alan Cox1e999732007-04-11 00:23:13 +01001211}
1212
Tejun Heoa5987e02008-03-27 19:14:23 +09001213u64 ata_tf_to_lba(const struct ata_taskfile *tf)
Alan Cox1e999732007-04-11 00:23:13 +01001214{
1215 u64 sectors = 0;
1216
1217 sectors |= (tf->device & 0x0f) << 24;
1218 sectors |= (tf->lbah & 0xff) << 16;
1219 sectors |= (tf->lbam & 0xff) << 8;
1220 sectors |= (tf->lbal & 0xff);
1221
Tejun Heoa5987e02008-03-27 19:14:23 +09001222 return sectors;
Alan Cox1e999732007-04-11 00:23:13 +01001223}
1224
1225/**
Tejun Heoc728a912007-09-03 12:32:30 +09001226 * ata_read_native_max_address - Read native max address
1227 * @dev: target device
1228 * @max_sectors: out parameter for the result native max address
Alan Cox1e999732007-04-11 00:23:13 +01001229 *
Tejun Heoc728a912007-09-03 12:32:30 +09001230 * Perform an LBA48 or LBA28 native size query upon the device in
1231 * question.
1232 *
1233 * RETURNS:
1234 * 0 on success, -EACCES if command is aborted by the drive.
1235 * -EIO on other errors.
Alan Cox1e999732007-04-11 00:23:13 +01001236 */
Tejun Heoc728a912007-09-03 12:32:30 +09001237static int ata_read_native_max_address(struct ata_device *dev, u64 *max_sectors)
Alan Cox1e999732007-04-11 00:23:13 +01001238{
Tejun Heoc728a912007-09-03 12:32:30 +09001239 unsigned int err_mask;
Alan Cox1e999732007-04-11 00:23:13 +01001240 struct ata_taskfile tf;
Tejun Heoc728a912007-09-03 12:32:30 +09001241 int lba48 = ata_id_has_lba48(dev->id);
Alan Cox1e999732007-04-11 00:23:13 +01001242
1243 ata_tf_init(dev, &tf);
1244
Tejun Heoc728a912007-09-03 12:32:30 +09001245 /* always clear all address registers */
Alan Cox1e999732007-04-11 00:23:13 +01001246 tf.flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
Tejun Heoc728a912007-09-03 12:32:30 +09001247
1248 if (lba48) {
1249 tf.command = ATA_CMD_READ_NATIVE_MAX_EXT;
1250 tf.flags |= ATA_TFLAG_LBA48;
1251 } else
1252 tf.command = ATA_CMD_READ_NATIVE_MAX;
1253
Hannes Reineckebd18bc02016-07-14 09:05:46 +09001254 tf.protocol = ATA_PROT_NODATA;
Tejun Heoc728a912007-09-03 12:32:30 +09001255 tf.device |= ATA_LBA;
Alan Cox1e999732007-04-11 00:23:13 +01001256
Tejun Heo2b789102007-10-09 15:05:44 +09001257 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
Tejun Heoc728a912007-09-03 12:32:30 +09001258 if (err_mask) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07001259 ata_dev_warn(dev,
1260 "failed to read native max address (err_mask=0x%x)\n",
1261 err_mask);
Tejun Heoc728a912007-09-03 12:32:30 +09001262 if (err_mask == AC_ERR_DEV && (tf.feature & ATA_ABORTED))
1263 return -EACCES;
1264 return -EIO;
1265 }
Alan Cox1e999732007-04-11 00:23:13 +01001266
Tejun Heoc728a912007-09-03 12:32:30 +09001267 if (lba48)
Tejun Heoa5987e02008-03-27 19:14:23 +09001268 *max_sectors = ata_tf_to_lba48(&tf) + 1;
Tejun Heoc728a912007-09-03 12:32:30 +09001269 else
Tejun Heoa5987e02008-03-27 19:14:23 +09001270 *max_sectors = ata_tf_to_lba(&tf) + 1;
Jeff Garzik2dcb4072007-10-19 06:42:56 -04001271 if (dev->horkage & ATA_HORKAGE_HPA_SIZE)
Alan Cox93328e112007-09-29 04:06:48 -04001272 (*max_sectors)--;
Tejun Heoc728a912007-09-03 12:32:30 +09001273 return 0;
Alan Cox1e999732007-04-11 00:23:13 +01001274}
1275
1276/**
Tejun Heoc728a912007-09-03 12:32:30 +09001277 * ata_set_max_sectors - Set max sectors
1278 * @dev: target device
Randy Dunlap6b38d1d2007-05-01 17:35:55 -07001279 * @new_sectors: new max sectors value to set for the device
Alan Cox1e999732007-04-11 00:23:13 +01001280 *
Tejun Heoc728a912007-09-03 12:32:30 +09001281 * Set max sectors of @dev to @new_sectors.
1282 *
1283 * RETURNS:
1284 * 0 on success, -EACCES if command is aborted or denied (due to
1285 * previous non-volatile SET_MAX) by the drive. -EIO on other
1286 * errors.
Alan Cox1e999732007-04-11 00:23:13 +01001287 */
Tejun Heo05027ad2007-09-03 12:32:57 +09001288static int ata_set_max_sectors(struct ata_device *dev, u64 new_sectors)
Alan Cox1e999732007-04-11 00:23:13 +01001289{
Tejun Heoc728a912007-09-03 12:32:30 +09001290 unsigned int err_mask;
Alan Cox1e999732007-04-11 00:23:13 +01001291 struct ata_taskfile tf;
Tejun Heoc728a912007-09-03 12:32:30 +09001292 int lba48 = ata_id_has_lba48(dev->id);
Alan Cox1e999732007-04-11 00:23:13 +01001293
1294 new_sectors--;
1295
1296 ata_tf_init(dev, &tf);
1297
Alan Cox1e999732007-04-11 00:23:13 +01001298 tf.flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
Tejun Heoc728a912007-09-03 12:32:30 +09001299
1300 if (lba48) {
1301 tf.command = ATA_CMD_SET_MAX_EXT;
1302 tf.flags |= ATA_TFLAG_LBA48;
1303
1304 tf.hob_lbal = (new_sectors >> 24) & 0xff;
1305 tf.hob_lbam = (new_sectors >> 32) & 0xff;
1306 tf.hob_lbah = (new_sectors >> 40) & 0xff;
Tejun Heo1e582ba2007-09-21 20:07:14 +09001307 } else {
Tejun Heoc728a912007-09-03 12:32:30 +09001308 tf.command = ATA_CMD_SET_MAX;
1309
Tejun Heo1e582ba2007-09-21 20:07:14 +09001310 tf.device |= (new_sectors >> 24) & 0xf;
1311 }
1312
Hannes Reineckebd18bc02016-07-14 09:05:46 +09001313 tf.protocol = ATA_PROT_NODATA;
Tejun Heoc728a912007-09-03 12:32:30 +09001314 tf.device |= ATA_LBA;
Alan Cox1e999732007-04-11 00:23:13 +01001315
1316 tf.lbal = (new_sectors >> 0) & 0xff;
1317 tf.lbam = (new_sectors >> 8) & 0xff;
1318 tf.lbah = (new_sectors >> 16) & 0xff;
Alan Cox1e999732007-04-11 00:23:13 +01001319
Tejun Heo2b789102007-10-09 15:05:44 +09001320 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
Tejun Heoc728a912007-09-03 12:32:30 +09001321 if (err_mask) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07001322 ata_dev_warn(dev,
1323 "failed to set max address (err_mask=0x%x)\n",
1324 err_mask);
Tejun Heoc728a912007-09-03 12:32:30 +09001325 if (err_mask == AC_ERR_DEV &&
1326 (tf.feature & (ATA_ABORTED | ATA_IDNF)))
1327 return -EACCES;
1328 return -EIO;
1329 }
Alan Cox1e999732007-04-11 00:23:13 +01001330
Tejun Heoc728a912007-09-03 12:32:30 +09001331 return 0;
Alan Cox1e999732007-04-11 00:23:13 +01001332}
1333
1334/**
1335 * ata_hpa_resize - Resize a device with an HPA set
1336 * @dev: Device to resize
1337 *
1338 * Read the size of an LBA28 or LBA48 disk with HPA features and resize
1339 * it if required to the full size of the media. The caller must check
1340 * the drive has the HPA feature set enabled.
Tejun Heo05027ad2007-09-03 12:32:57 +09001341 *
1342 * RETURNS:
1343 * 0 on success, -errno on failure.
Alan Cox1e999732007-04-11 00:23:13 +01001344 */
Tejun Heo05027ad2007-09-03 12:32:57 +09001345static int ata_hpa_resize(struct ata_device *dev)
Alan Cox1e999732007-04-11 00:23:13 +01001346{
Tejun Heo05027ad2007-09-03 12:32:57 +09001347 struct ata_eh_context *ehc = &dev->link->eh_context;
1348 int print_info = ehc->i.flags & ATA_EHI_PRINTINFO;
Tejun Heo445d2112010-04-05 10:33:13 +09001349 bool unlock_hpa = ata_ignore_hpa || dev->flags & ATA_DFLAG_UNLOCK_HPA;
Tejun Heo05027ad2007-09-03 12:32:57 +09001350 u64 sectors = ata_id_n_sectors(dev->id);
1351 u64 native_sectors;
Tejun Heoc728a912007-09-03 12:32:30 +09001352 int rc;
Jeff Garzika617c092007-05-21 20:14:23 -04001353
Tejun Heo05027ad2007-09-03 12:32:57 +09001354 /* do we need to do it? */
Hannes Reinecke9162c652014-11-05 13:08:21 +01001355 if ((dev->class != ATA_DEV_ATA && dev->class != ATA_DEV_ZAC) ||
Tejun Heo05027ad2007-09-03 12:32:57 +09001356 !ata_id_has_lba(dev->id) || !ata_id_hpa_enabled(dev->id) ||
1357 (dev->horkage & ATA_HORKAGE_BROKEN_HPA))
Tejun Heoc728a912007-09-03 12:32:30 +09001358 return 0;
Alan Cox1e999732007-04-11 00:23:13 +01001359
Tejun Heo05027ad2007-09-03 12:32:57 +09001360 /* read native max address */
1361 rc = ata_read_native_max_address(dev, &native_sectors);
1362 if (rc) {
Tejun Heodda7aba2008-03-23 21:05:15 +09001363 /* If device aborted the command or HPA isn't going to
1364 * be unlocked, skip HPA resizing.
Tejun Heo05027ad2007-09-03 12:32:57 +09001365 */
Tejun Heo445d2112010-04-05 10:33:13 +09001366 if (rc == -EACCES || !unlock_hpa) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07001367 ata_dev_warn(dev,
1368 "HPA support seems broken, skipping HPA handling\n");
Tejun Heo05027ad2007-09-03 12:32:57 +09001369 dev->horkage |= ATA_HORKAGE_BROKEN_HPA;
Alan Cox1e999732007-04-11 00:23:13 +01001370
Tejun Heo05027ad2007-09-03 12:32:57 +09001371 /* we can continue if device aborted the command */
1372 if (rc == -EACCES)
1373 rc = 0;
Alan Cox1e999732007-04-11 00:23:13 +01001374 }
Tejun Heo37301a52007-06-25 20:45:54 +09001375
Tejun Heo05027ad2007-09-03 12:32:57 +09001376 return rc;
1377 }
Tejun Heo5920dad2009-07-15 17:11:41 +09001378 dev->n_native_sectors = native_sectors;
Tejun Heo05027ad2007-09-03 12:32:57 +09001379
1380 /* nothing to do? */
Tejun Heo445d2112010-04-05 10:33:13 +09001381 if (native_sectors <= sectors || !unlock_hpa) {
Tejun Heo05027ad2007-09-03 12:32:57 +09001382 if (!print_info || native_sectors == sectors)
1383 return 0;
1384
1385 if (native_sectors > sectors)
Joe Perchesa9a79df2011-04-15 15:51:59 -07001386 ata_dev_info(dev,
Tejun Heo05027ad2007-09-03 12:32:57 +09001387 "HPA detected: current %llu, native %llu\n",
1388 (unsigned long long)sectors,
1389 (unsigned long long)native_sectors);
1390 else if (native_sectors < sectors)
Joe Perchesa9a79df2011-04-15 15:51:59 -07001391 ata_dev_warn(dev,
1392 "native sectors (%llu) is smaller than sectors (%llu)\n",
Tejun Heo05027ad2007-09-03 12:32:57 +09001393 (unsigned long long)native_sectors,
1394 (unsigned long long)sectors);
1395 return 0;
1396 }
1397
1398 /* let's unlock HPA */
1399 rc = ata_set_max_sectors(dev, native_sectors);
1400 if (rc == -EACCES) {
1401 /* if device aborted the command, skip HPA resizing */
Joe Perchesa9a79df2011-04-15 15:51:59 -07001402 ata_dev_warn(dev,
1403 "device aborted resize (%llu -> %llu), skipping HPA handling\n",
1404 (unsigned long long)sectors,
1405 (unsigned long long)native_sectors);
Tejun Heo05027ad2007-09-03 12:32:57 +09001406 dev->horkage |= ATA_HORKAGE_BROKEN_HPA;
1407 return 0;
1408 } else if (rc)
1409 return rc;
1410
1411 /* re-read IDENTIFY data */
1412 rc = ata_dev_reread_id(dev, 0);
1413 if (rc) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07001414 ata_dev_err(dev,
1415 "failed to re-read IDENTIFY data after HPA resizing\n");
Tejun Heo05027ad2007-09-03 12:32:57 +09001416 return rc;
1417 }
1418
1419 if (print_info) {
1420 u64 new_sectors = ata_id_n_sectors(dev->id);
Joe Perchesa9a79df2011-04-15 15:51:59 -07001421 ata_dev_info(dev,
Tejun Heo05027ad2007-09-03 12:32:57 +09001422 "HPA unlocked: %llu -> %llu, native %llu\n",
1423 (unsigned long long)sectors,
1424 (unsigned long long)new_sectors,
1425 (unsigned long long)native_sectors);
1426 }
1427
1428 return 0;
Alan Cox1e999732007-04-11 00:23:13 +01001429}
1430
Edward Falk0baab862005-06-02 18:17:13 -04001431/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 * ata_dump_id - IDENTIFY DEVICE info debugging output
Tejun Heo0bd33002006-02-12 22:47:05 +09001433 * @id: IDENTIFY DEVICE page to dump
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 *
Tejun Heo0bd33002006-02-12 22:47:05 +09001435 * Dump selected 16-bit words from the given IDENTIFY DEVICE
1436 * page.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 *
1438 * LOCKING:
1439 * caller.
1440 */
1441
Tejun Heo0bd33002006-02-12 22:47:05 +09001442static inline void ata_dump_id(const u16 *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443{
1444 DPRINTK("49==0x%04x "
1445 "53==0x%04x "
1446 "63==0x%04x "
1447 "64==0x%04x "
1448 "75==0x%04x \n",
Tejun Heo0bd33002006-02-12 22:47:05 +09001449 id[49],
1450 id[53],
1451 id[63],
1452 id[64],
1453 id[75]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 DPRINTK("80==0x%04x "
1455 "81==0x%04x "
1456 "82==0x%04x "
1457 "83==0x%04x "
1458 "84==0x%04x \n",
Tejun Heo0bd33002006-02-12 22:47:05 +09001459 id[80],
1460 id[81],
1461 id[82],
1462 id[83],
1463 id[84]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 DPRINTK("88==0x%04x "
1465 "93==0x%04x\n",
Tejun Heo0bd33002006-02-12 22:47:05 +09001466 id[88],
1467 id[93]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468}
1469
Tejun Heocb95d562006-03-06 04:31:56 +09001470/**
1471 * ata_id_xfermask - Compute xfermask from the given IDENTIFY data
1472 * @id: IDENTIFY data to compute xfer mask from
1473 *
1474 * Compute the xfermask for this device. This is not as trivial
1475 * as it seems if we must consider early devices correctly.
1476 *
1477 * FIXME: pre IDE drive timing (do we care ?).
1478 *
1479 * LOCKING:
1480 * None.
1481 *
1482 * RETURNS:
1483 * Computed xfermask
1484 */
Tejun Heo7dc951a2007-11-27 19:43:42 +09001485unsigned long ata_id_xfermask(const u16 *id)
Tejun Heocb95d562006-03-06 04:31:56 +09001486{
Tejun Heo7dc951a2007-11-27 19:43:42 +09001487 unsigned long pio_mask, mwdma_mask, udma_mask;
Tejun Heocb95d562006-03-06 04:31:56 +09001488
1489 /* Usual case. Word 53 indicates word 64 is valid */
1490 if (id[ATA_ID_FIELD_VALID] & (1 << 1)) {
1491 pio_mask = id[ATA_ID_PIO_MODES] & 0x03;
1492 pio_mask <<= 3;
1493 pio_mask |= 0x7;
1494 } else {
1495 /* If word 64 isn't valid then Word 51 high byte holds
1496 * the PIO timing number for the maximum. Turn it into
1497 * a mask.
1498 */
Lennert Buytenhek7a0f1c82007-01-29 13:28:47 +01001499 u8 mode = (id[ATA_ID_OLD_PIO_MODES] >> 8) & 0xFF;
Alan Cox46767aeb2006-09-29 18:26:47 +01001500 if (mode < 5) /* Valid PIO range */
Jeff Garzik2dcb4072007-10-19 06:42:56 -04001501 pio_mask = (2 << mode) - 1;
Alan Cox46767aeb2006-09-29 18:26:47 +01001502 else
1503 pio_mask = 1;
Tejun Heocb95d562006-03-06 04:31:56 +09001504
1505 /* But wait.. there's more. Design your standards by
1506 * committee and you too can get a free iordy field to
1507 * process. However its the speeds not the modes that
1508 * are supported... Note drivers using the timing API
1509 * will get this right anyway
1510 */
1511 }
1512
1513 mwdma_mask = id[ATA_ID_MWDMA_MODES] & 0x07;
Tejun Heofb21f0d2006-03-12 12:34:35 +09001514
Alan Coxb352e572006-08-10 18:52:12 +01001515 if (ata_id_is_cfa(id)) {
1516 /*
1517 * Process compact flash extended modes
1518 */
Sergei Shtylyov62afe5d2009-04-13 20:50:00 +04001519 int pio = (id[ATA_ID_CFA_MODES] >> 0) & 0x7;
1520 int dma = (id[ATA_ID_CFA_MODES] >> 3) & 0x7;
Alan Coxb352e572006-08-10 18:52:12 +01001521
1522 if (pio)
1523 pio_mask |= (1 << 5);
1524 if (pio > 1)
1525 pio_mask |= (1 << 6);
1526 if (dma)
1527 mwdma_mask |= (1 << 3);
1528 if (dma > 1)
1529 mwdma_mask |= (1 << 4);
1530 }
1531
Tejun Heofb21f0d2006-03-12 12:34:35 +09001532 udma_mask = 0;
1533 if (id[ATA_ID_FIELD_VALID] & (1 << 2))
1534 udma_mask = id[ATA_ID_UDMA_MODES] & 0xff;
Tejun Heocb95d562006-03-06 04:31:56 +09001535
1536 return ata_pack_xfermask(pio_mask, mwdma_mask, udma_mask);
1537}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01001538EXPORT_SYMBOL_GPL(ata_id_xfermask);
Tejun Heocb95d562006-03-06 04:31:56 +09001539
Adrian Bunk7102d232007-01-04 00:09:36 +01001540static void ata_qc_complete_internal(struct ata_queued_cmd *qc)
Tejun Heoa2a7a662005-12-13 14:48:31 +09001541{
Tejun Heo77853bf2006-01-23 13:09:36 +09001542 struct completion *waiting = qc->private_data;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001543
Tejun Heoa2a7a662005-12-13 14:48:31 +09001544 complete(waiting);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001545}
1546
1547/**
Tejun Heo24326972006-11-14 22:47:09 +09001548 * ata_exec_internal_sg - execute libata internal command
Tejun Heoa2a7a662005-12-13 14:48:31 +09001549 * @dev: Device to which the command is sent
1550 * @tf: Taskfile registers for the command and the result
Tejun Heod69cf372006-04-02 18:51:53 +09001551 * @cdb: CDB for packet command
Masanari Iidae2278672014-02-18 22:54:36 +09001552 * @dma_dir: Data transfer direction of the command
Randy Dunlap5c1ad8b2007-10-18 14:12:26 -07001553 * @sgl: sg list for the data buffer of the command
Tejun Heo24326972006-11-14 22:47:09 +09001554 * @n_elem: Number of sg entries
Tejun Heo2b789102007-10-09 15:05:44 +09001555 * @timeout: Timeout in msecs (0 for default)
Tejun Heoa2a7a662005-12-13 14:48:31 +09001556 *
1557 * Executes libata internal command with timeout. @tf contains
1558 * command on entry and result on return. Timeout and error
1559 * conditions are reported via return value. No recovery action
1560 * is taken after a command times out. It's caller's duty to
1561 * clean up after timeout.
1562 *
1563 * LOCKING:
1564 * None. Should be called with kernel context, might sleep.
Tejun Heo551e8882006-06-12 14:09:49 +09001565 *
1566 * RETURNS:
1567 * Zero on success, AC_ERR_* mask on failure
Tejun Heoa2a7a662005-12-13 14:48:31 +09001568 */
Tejun Heo24326972006-11-14 22:47:09 +09001569unsigned ata_exec_internal_sg(struct ata_device *dev,
1570 struct ata_taskfile *tf, const u8 *cdb,
Jens Axboe87260212007-10-16 11:14:12 +02001571 int dma_dir, struct scatterlist *sgl,
Tejun Heo2b789102007-10-09 15:05:44 +09001572 unsigned int n_elem, unsigned long timeout)
Tejun Heoa2a7a662005-12-13 14:48:31 +09001573{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001574 struct ata_link *link = dev->link;
1575 struct ata_port *ap = link->ap;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001576 u8 command = tf->command;
Tejun Heo87fbc5a2008-05-20 02:17:54 +09001577 int auto_timeout = 0;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001578 struct ata_queued_cmd *qc;
Jens Axboe28361c42018-05-11 12:51:09 -06001579 unsigned int preempted_tag;
Jens Axboee3ed89392018-05-11 12:51:05 -06001580 u32 preempted_sactive;
1581 u64 preempted_qc_active;
Tejun Heoda917d62007-09-23 13:14:12 +09001582 int preempted_nr_active_links;
Ingo Molnar60be6b92006-07-03 00:25:26 -07001583 DECLARE_COMPLETION_ONSTACK(wait);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001584 unsigned long flags;
Tejun Heo77853bf2006-01-23 13:09:36 +09001585 unsigned int err_mask;
Tejun Heod95a7172006-05-15 20:58:14 +09001586 int rc;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001587
Jeff Garzikba6a1302006-06-22 23:46:10 -04001588 spin_lock_irqsave(ap->lock, flags);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001589
Tejun Heoe3180492006-05-15 20:58:09 +09001590 /* no internal command while frozen */
Tejun Heob51e9e52006-06-29 01:29:30 +09001591 if (ap->pflags & ATA_PFLAG_FROZEN) {
Jeff Garzikba6a1302006-06-22 23:46:10 -04001592 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoe3180492006-05-15 20:58:09 +09001593 return AC_ERR_SYSTEM;
1594 }
1595
Tejun Heo2ab7db12006-05-15 20:58:02 +09001596 /* initialize internal qc */
Jens Axboe28361c42018-05-11 12:51:09 -06001597 qc = __ata_qc_from_tag(ap, ATA_TAG_INTERNAL);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001598
Jens Axboe28361c42018-05-11 12:51:09 -06001599 qc->tag = ATA_TAG_INTERNAL;
1600 qc->hw_tag = 0;
Tejun Heo2ab7db12006-05-15 20:58:02 +09001601 qc->scsicmd = NULL;
1602 qc->ap = ap;
1603 qc->dev = dev;
1604 ata_qc_reinit(qc);
1605
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001606 preempted_tag = link->active_tag;
1607 preempted_sactive = link->sactive;
Tejun Heodedaf2b2006-05-15 21:03:43 +09001608 preempted_qc_active = ap->qc_active;
Tejun Heoda917d62007-09-23 13:14:12 +09001609 preempted_nr_active_links = ap->nr_active_links;
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001610 link->active_tag = ATA_TAG_POISON;
1611 link->sactive = 0;
Tejun Heodedaf2b2006-05-15 21:03:43 +09001612 ap->qc_active = 0;
Tejun Heoda917d62007-09-23 13:14:12 +09001613 ap->nr_active_links = 0;
Tejun Heo2ab7db12006-05-15 20:58:02 +09001614
1615 /* prepare & issue qc */
Tejun Heoa2a7a662005-12-13 14:48:31 +09001616 qc->tf = *tf;
Tejun Heod69cf372006-04-02 18:51:53 +09001617 if (cdb)
1618 memcpy(qc->cdb, cdb, ATAPI_CDB_LEN);
Vincent Pelletiere7714512013-05-18 18:44:04 +02001619
1620 /* some SATA bridges need us to indicate data xfer direction */
1621 if (tf->protocol == ATAPI_PROT_DMA && (dev->flags & ATA_DFLAG_DMADIR) &&
1622 dma_dir == DMA_FROM_DEVICE)
1623 qc->tf.feature |= ATAPI_DMADIR;
1624
Tejun Heoe61e0672006-05-15 20:57:40 +09001625 qc->flags |= ATA_QCFLAG_RESULT_TF;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001626 qc->dma_dir = dma_dir;
1627 if (dma_dir != DMA_NONE) {
Tejun Heo24326972006-11-14 22:47:09 +09001628 unsigned int i, buflen = 0;
Jens Axboe87260212007-10-16 11:14:12 +02001629 struct scatterlist *sg;
Tejun Heo24326972006-11-14 22:47:09 +09001630
Jens Axboe87260212007-10-16 11:14:12 +02001631 for_each_sg(sgl, sg, n_elem, i)
1632 buflen += sg->length;
Tejun Heo24326972006-11-14 22:47:09 +09001633
Jens Axboe87260212007-10-16 11:14:12 +02001634 ata_sg_init(qc, sgl, n_elem);
Brian King49c80422007-01-30 11:32:26 -06001635 qc->nbytes = buflen;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001636 }
1637
Tejun Heo77853bf2006-01-23 13:09:36 +09001638 qc->private_data = &wait;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001639 qc->complete_fn = ata_qc_complete_internal;
1640
Tejun Heo8e0e6942006-03-31 20:41:11 +09001641 ata_qc_issue(qc);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001642
Jeff Garzikba6a1302006-06-22 23:46:10 -04001643 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001644
Tejun Heo87fbc5a2008-05-20 02:17:54 +09001645 if (!timeout) {
1646 if (ata_probe_timeout)
1647 timeout = ata_probe_timeout * 1000;
1648 else {
1649 timeout = ata_internal_cmd_timeout(dev, command);
1650 auto_timeout = 1;
1651 }
1652 }
Tejun Heo2b789102007-10-09 15:05:44 +09001653
Tejun Heoc0c362b2010-09-06 17:57:14 +02001654 if (ap->ops->error_handler)
1655 ata_eh_release(ap);
1656
Tejun Heo2b789102007-10-09 15:05:44 +09001657 rc = wait_for_completion_timeout(&wait, msecs_to_jiffies(timeout));
Albert Lee41ade502006-03-14 11:19:04 +08001658
Tejun Heoc0c362b2010-09-06 17:57:14 +02001659 if (ap->ops->error_handler)
1660 ata_eh_acquire(ap);
1661
Tejun Heoc4291372010-05-10 21:41:38 +02001662 ata_sff_flush_pio_task(ap);
Tejun Heod95a7172006-05-15 20:58:14 +09001663
1664 if (!rc) {
Jeff Garzikba6a1302006-06-22 23:46:10 -04001665 spin_lock_irqsave(ap->lock, flags);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001666
1667 /* We're racing with irq here. If we lose, the
1668 * following test prevents us from completing the qc
Tejun Heod95a7172006-05-15 20:58:14 +09001669 * twice. If we win, the port is frozen and will be
1670 * cleaned up by ->post_internal_cmd().
Tejun Heoa2a7a662005-12-13 14:48:31 +09001671 */
Tejun Heo77853bf2006-01-23 13:09:36 +09001672 if (qc->flags & ATA_QCFLAG_ACTIVE) {
Tejun Heod95a7172006-05-15 20:58:14 +09001673 qc->err_mask |= AC_ERR_TIMEOUT;
1674
1675 if (ap->ops->error_handler)
1676 ata_port_freeze(ap);
1677 else
1678 ata_qc_complete(qc);
Tejun Heof15a1da2006-05-15 20:57:56 +09001679
Borislav Petkov0dd4b212006-06-23 02:29:08 -04001680 if (ata_msg_warn(ap))
Joe Perchesa9a79df2011-04-15 15:51:59 -07001681 ata_dev_warn(dev, "qc timeout (cmd 0x%x)\n",
1682 command);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001683 }
1684
Jeff Garzikba6a1302006-06-22 23:46:10 -04001685 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001686 }
1687
Tejun Heod95a7172006-05-15 20:58:14 +09001688 /* do post_internal_cmd */
1689 if (ap->ops->post_internal_cmd)
1690 ap->ops->post_internal_cmd(qc);
1691
Tejun Heoa51d6442007-03-20 15:24:11 +09001692 /* perform minimal error analysis */
1693 if (qc->flags & ATA_QCFLAG_FAILED) {
1694 if (qc->result_tf.command & (ATA_ERR | ATA_DF))
1695 qc->err_mask |= AC_ERR_DEV;
1696
1697 if (!qc->err_mask)
1698 qc->err_mask |= AC_ERR_OTHER;
1699
1700 if (qc->err_mask & ~AC_ERR_OTHER)
1701 qc->err_mask &= ~AC_ERR_OTHER;
Damien Le Moal2dae9952016-12-19 10:17:40 +09001702 } else if (qc->tf.command == ATA_CMD_REQ_SENSE_DATA) {
1703 qc->result_tf.command |= ATA_SENSE;
Tejun Heod95a7172006-05-15 20:58:14 +09001704 }
1705
Tejun Heo15869302006-05-15 20:57:33 +09001706 /* finish up */
Jeff Garzikba6a1302006-06-22 23:46:10 -04001707 spin_lock_irqsave(ap->lock, flags);
Tejun Heo15869302006-05-15 20:57:33 +09001708
Tejun Heoe61e0672006-05-15 20:57:40 +09001709 *tf = qc->result_tf;
Tejun Heo77853bf2006-01-23 13:09:36 +09001710 err_mask = qc->err_mask;
1711
1712 ata_qc_free(qc);
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001713 link->active_tag = preempted_tag;
1714 link->sactive = preempted_sactive;
Tejun Heodedaf2b2006-05-15 21:03:43 +09001715 ap->qc_active = preempted_qc_active;
Tejun Heoda917d62007-09-23 13:14:12 +09001716 ap->nr_active_links = preempted_nr_active_links;
Tejun Heo77853bf2006-01-23 13:09:36 +09001717
Jeff Garzikba6a1302006-06-22 23:46:10 -04001718 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heo15869302006-05-15 20:57:33 +09001719
Tejun Heo87fbc5a2008-05-20 02:17:54 +09001720 if ((err_mask & AC_ERR_TIMEOUT) && auto_timeout)
1721 ata_internal_cmd_timed_out(dev, command);
1722
Tejun Heo77853bf2006-01-23 13:09:36 +09001723 return err_mask;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001724}
1725
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726/**
Tejun Heo33480a02006-12-12 02:15:31 +09001727 * ata_exec_internal - execute libata internal command
Tejun Heo24326972006-11-14 22:47:09 +09001728 * @dev: Device to which the command is sent
1729 * @tf: Taskfile registers for the command and the result
1730 * @cdb: CDB for packet command
Masanari Iidae2278672014-02-18 22:54:36 +09001731 * @dma_dir: Data transfer direction of the command
Tejun Heo24326972006-11-14 22:47:09 +09001732 * @buf: Data buffer of the command
1733 * @buflen: Length of data buffer
Tejun Heo2b789102007-10-09 15:05:44 +09001734 * @timeout: Timeout in msecs (0 for default)
Tejun Heo24326972006-11-14 22:47:09 +09001735 *
1736 * Wrapper around ata_exec_internal_sg() which takes simple
1737 * buffer instead of sg list.
1738 *
1739 * LOCKING:
1740 * None. Should be called with kernel context, might sleep.
1741 *
1742 * RETURNS:
1743 * Zero on success, AC_ERR_* mask on failure
1744 */
1745unsigned ata_exec_internal(struct ata_device *dev,
1746 struct ata_taskfile *tf, const u8 *cdb,
Tejun Heo2b789102007-10-09 15:05:44 +09001747 int dma_dir, void *buf, unsigned int buflen,
1748 unsigned long timeout)
Tejun Heo24326972006-11-14 22:47:09 +09001749{
Tejun Heo33480a02006-12-12 02:15:31 +09001750 struct scatterlist *psg = NULL, sg;
1751 unsigned int n_elem = 0;
Tejun Heo24326972006-11-14 22:47:09 +09001752
Tejun Heo33480a02006-12-12 02:15:31 +09001753 if (dma_dir != DMA_NONE) {
1754 WARN_ON(!buf);
1755 sg_init_one(&sg, buf, buflen);
1756 psg = &sg;
1757 n_elem++;
1758 }
Tejun Heo24326972006-11-14 22:47:09 +09001759
Tejun Heo2b789102007-10-09 15:05:44 +09001760 return ata_exec_internal_sg(dev, tf, cdb, dma_dir, psg, n_elem,
1761 timeout);
Tejun Heo24326972006-11-14 22:47:09 +09001762}
1763
1764/**
Alan Cox1bc4ccf2006-01-09 17:18:14 +00001765 * ata_pio_need_iordy - check if iordy needed
1766 * @adev: ATA device
1767 *
1768 * Check if the current speed of the device requires IORDY. Used
1769 * by various controllers for chip configuration.
1770 */
Alan Cox1bc4ccf2006-01-09 17:18:14 +00001771unsigned int ata_pio_need_iordy(const struct ata_device *adev)
1772{
Tejun Heo0d9e6652009-06-11 11:04:45 +09001773 /* Don't set IORDY if we're preparing for reset. IORDY may
1774 * lead to controller lock up on certain controllers if the
1775 * port is not occupied. See bko#11703 for details.
1776 */
1777 if (adev->link->ap->pflags & ATA_PFLAG_RESETTING)
1778 return 0;
1779 /* Controller doesn't support IORDY. Probably a pointless
1780 * check as the caller should know this.
1781 */
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001782 if (adev->link->ap->flags & ATA_FLAG_NO_IORDY)
Alan Cox1bc4ccf2006-01-09 17:18:14 +00001783 return 0;
David Daney5c18c4d2008-12-10 15:39:12 -08001784 /* CF spec. r4.1 Table 22 says no iordy on PIO5 and PIO6. */
1785 if (ata_id_is_cfa(adev->id)
1786 && (adev->pio_mode == XFER_PIO_5 || adev->pio_mode == XFER_PIO_6))
1787 return 0;
Alan Cox432729f2007-03-08 23:22:59 +00001788 /* PIO3 and higher it is mandatory */
1789 if (adev->pio_mode > XFER_PIO_2)
Alan Cox1bc4ccf2006-01-09 17:18:14 +00001790 return 1;
Alan Cox432729f2007-03-08 23:22:59 +00001791 /* We turn it on when possible */
1792 if (ata_id_has_iordy(adev->id))
1793 return 1;
1794 return 0;
1795}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01001796EXPORT_SYMBOL_GPL(ata_pio_need_iordy);
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05001797
Alan Cox432729f2007-03-08 23:22:59 +00001798/**
1799 * ata_pio_mask_no_iordy - Return the non IORDY mask
1800 * @adev: ATA device
1801 *
1802 * Compute the highest mode possible if we are not using iordy. Return
1803 * -1 if no iordy mode is available.
1804 */
Alan Cox432729f2007-03-08 23:22:59 +00001805static u32 ata_pio_mask_no_iordy(const struct ata_device *adev)
1806{
Alan Cox1bc4ccf2006-01-09 17:18:14 +00001807 /* If we have no drive specific rule, then PIO 2 is non IORDY */
Alan Cox1bc4ccf2006-01-09 17:18:14 +00001808 if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE */
Alan Cox432729f2007-03-08 23:22:59 +00001809 u16 pio = adev->id[ATA_ID_EIDE_PIO];
Alan Cox1bc4ccf2006-01-09 17:18:14 +00001810 /* Is the speed faster than the drive allows non IORDY ? */
1811 if (pio) {
1812 /* This is cycle times not frequency - watch the logic! */
1813 if (pio > 240) /* PIO2 is 240nS per cycle */
Alan Cox432729f2007-03-08 23:22:59 +00001814 return 3 << ATA_SHIFT_PIO;
1815 return 7 << ATA_SHIFT_PIO;
Alan Cox1bc4ccf2006-01-09 17:18:14 +00001816 }
1817 }
Alan Cox432729f2007-03-08 23:22:59 +00001818 return 3 << ATA_SHIFT_PIO;
Alan Cox1bc4ccf2006-01-09 17:18:14 +00001819}
1820
1821/**
Alan Cox963e4972008-07-24 17:16:06 +01001822 * ata_do_dev_read_id - default ID read method
1823 * @dev: device
1824 * @tf: proposed taskfile
1825 * @id: data buffer
1826 *
1827 * Issue the identify taskfile and hand back the buffer containing
1828 * identify data. For some RAID controllers and for pre ATA devices
1829 * this function is wrapped or replaced by the driver
1830 */
1831unsigned int ata_do_dev_read_id(struct ata_device *dev,
1832 struct ata_taskfile *tf, u16 *id)
1833{
1834 return ata_exec_internal(dev, tf, NULL, DMA_FROM_DEVICE,
1835 id, sizeof(id[0]) * ATA_ID_WORDS, 0);
1836}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01001837EXPORT_SYMBOL_GPL(ata_do_dev_read_id);
Alan Cox963e4972008-07-24 17:16:06 +01001838
1839/**
Tejun Heo49016ac2006-02-21 02:12:11 +09001840 * ata_dev_read_id - Read ID data from the specified device
Tejun Heo49016ac2006-02-21 02:12:11 +09001841 * @dev: target device
1842 * @p_class: pointer to class of the target device (may be changed)
Tejun Heobff04642006-11-10 18:08:10 +09001843 * @flags: ATA_READID_* flags
Tejun Heofe635c72006-05-15 20:57:35 +09001844 * @id: buffer to read IDENTIFY data into
Tejun Heo49016ac2006-02-21 02:12:11 +09001845 *
1846 * Read ID data from the specified device. ATA_CMD_ID_ATA is
1847 * performed on ATA devices and ATA_CMD_ID_ATAPI on ATAPI
Tejun Heoaec5c3c2006-03-25 01:33:34 +09001848 * devices. This function also issues ATA_CMD_INIT_DEV_PARAMS
1849 * for pre-ATA4 drives.
Tejun Heo49016ac2006-02-21 02:12:11 +09001850 *
Alan Cox50a99012007-08-08 14:27:00 +01001851 * FIXME: ATA_CMD_ID_ATA is optional for early drives and right
Jeff Garzik2dcb4072007-10-19 06:42:56 -04001852 * now we abort if we hit that case.
Alan Cox50a99012007-08-08 14:27:00 +01001853 *
Tejun Heo49016ac2006-02-21 02:12:11 +09001854 * LOCKING:
1855 * Kernel thread context (may sleep)
1856 *
1857 * RETURNS:
1858 * 0 on success, -errno otherwise.
1859 */
Tejun Heoa9beec92006-05-31 18:27:44 +09001860int ata_dev_read_id(struct ata_device *dev, unsigned int *p_class,
Tejun Heobff04642006-11-10 18:08:10 +09001861 unsigned int flags, u16 *id)
Tejun Heo49016ac2006-02-21 02:12:11 +09001862{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001863 struct ata_port *ap = dev->link->ap;
Tejun Heo49016ac2006-02-21 02:12:11 +09001864 unsigned int class = *p_class;
Tejun Heo49016ac2006-02-21 02:12:11 +09001865 struct ata_taskfile tf;
1866 unsigned int err_mask = 0;
1867 const char *reason;
Tejun Heo79b42ba2009-04-15 06:21:10 +09001868 bool is_semb = class == ATA_DEV_SEMB;
Tejun Heo54936f82007-05-11 14:35:29 +02001869 int may_fallback = 1, tried_spinup = 0;
Tejun Heo49016ac2006-02-21 02:12:11 +09001870 int rc;
1871
Borislav Petkov0dd4b212006-06-23 02:29:08 -04001872 if (ata_msg_ctl(ap))
Joe Perchesa9a79df2011-04-15 15:51:59 -07001873 ata_dev_dbg(dev, "%s: ENTER\n", __func__);
Tejun Heo49016ac2006-02-21 02:12:11 +09001874
Alan Cox963e4972008-07-24 17:16:06 +01001875retry:
Tejun Heo3373efd2006-05-15 20:57:53 +09001876 ata_tf_init(dev, &tf);
Tejun Heo49016ac2006-02-21 02:12:11 +09001877
1878 switch (class) {
Tejun Heo79b42ba2009-04-15 06:21:10 +09001879 case ATA_DEV_SEMB:
1880 class = ATA_DEV_ATA; /* some hard drives report SEMB sig */
Gustavo A. R. Silva05b83602017-10-12 14:19:16 -05001881 /* fall through */
Tejun Heo49016ac2006-02-21 02:12:11 +09001882 case ATA_DEV_ATA:
Hannes Reinecke9162c652014-11-05 13:08:21 +01001883 case ATA_DEV_ZAC:
Tejun Heo49016ac2006-02-21 02:12:11 +09001884 tf.command = ATA_CMD_ID_ATA;
1885 break;
1886 case ATA_DEV_ATAPI:
1887 tf.command = ATA_CMD_ID_ATAPI;
1888 break;
1889 default:
1890 rc = -ENODEV;
1891 reason = "unsupported class";
1892 goto err_out;
1893 }
1894
1895 tf.protocol = ATA_PROT_PIO;
Tejun Heo81afe892007-02-07 12:37:41 -08001896
1897 /* Some devices choke if TF registers contain garbage. Make
1898 * sure those are properly initialized.
1899 */
1900 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
1901
1902 /* Device presence detection is unreliable on some
1903 * controllers. Always poll IDENTIFY if available.
1904 */
1905 tf.flags |= ATA_TFLAG_POLLING;
Tejun Heo49016ac2006-02-21 02:12:11 +09001906
Alan Cox963e4972008-07-24 17:16:06 +01001907 if (ap->ops->read_id)
1908 err_mask = ap->ops->read_id(dev, &tf, id);
1909 else
1910 err_mask = ata_do_dev_read_id(dev, &tf, id);
1911
Tejun Heo49016ac2006-02-21 02:12:11 +09001912 if (err_mask) {
Tejun Heo800b3992006-12-03 21:34:13 +09001913 if (err_mask & AC_ERR_NODEV_HINT) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07001914 ata_dev_dbg(dev, "NODEV after polling detection\n");
Tejun Heo55a8e2c2006-11-10 18:08:10 +09001915 return -ENOENT;
1916 }
1917
Tejun Heo79b42ba2009-04-15 06:21:10 +09001918 if (is_semb) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07001919 ata_dev_info(dev,
1920 "IDENTIFY failed on device w/ SEMB sig, disabled\n");
Tejun Heo79b42ba2009-04-15 06:21:10 +09001921 /* SEMB is not supported yet */
1922 *p_class = ATA_DEV_SEMB_UNSUP;
1923 return 0;
1924 }
1925
Tejun Heo1ffc1512008-03-23 15:16:53 +09001926 if ((err_mask == AC_ERR_DEV) && (tf.feature & ATA_ABORTED)) {
1927 /* Device or controller might have reported
1928 * the wrong device class. Give a shot at the
1929 * other IDENTIFY if the current one is
1930 * aborted by the device.
1931 */
1932 if (may_fallback) {
1933 may_fallback = 0;
Tejun Heo54936f82007-05-11 14:35:29 +02001934
Tejun Heo1ffc1512008-03-23 15:16:53 +09001935 if (class == ATA_DEV_ATA)
1936 class = ATA_DEV_ATAPI;
1937 else
1938 class = ATA_DEV_ATA;
1939 goto retry;
1940 }
1941
1942 /* Control reaches here iff the device aborted
1943 * both flavors of IDENTIFYs which happens
1944 * sometimes with phantom devices.
1945 */
Joe Perchesa9a79df2011-04-15 15:51:59 -07001946 ata_dev_dbg(dev,
1947 "both IDENTIFYs aborted, assuming NODEV\n");
Tejun Heo1ffc1512008-03-23 15:16:53 +09001948 return -ENOENT;
Tejun Heo54936f82007-05-11 14:35:29 +02001949 }
1950
Tejun Heo49016ac2006-02-21 02:12:11 +09001951 rc = -EIO;
1952 reason = "I/O error";
Tejun Heo49016ac2006-02-21 02:12:11 +09001953 goto err_out;
1954 }
1955
Tejun Heo43c9c592010-05-23 12:59:11 +02001956 if (dev->horkage & ATA_HORKAGE_DUMP_ID) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07001957 ata_dev_dbg(dev, "dumping IDENTIFY data, "
1958 "class=%d may_fallback=%d tried_spinup=%d\n",
1959 class, may_fallback, tried_spinup);
Tejun Heo43c9c592010-05-23 12:59:11 +02001960 print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET,
1961 16, 2, id, ATA_ID_WORDS * sizeof(*id), true);
1962 }
1963
Tejun Heo54936f82007-05-11 14:35:29 +02001964 /* Falling back doesn't make sense if ID data was read
1965 * successfully at least once.
1966 */
1967 may_fallback = 0;
1968
Tejun Heo49016ac2006-02-21 02:12:11 +09001969 swap_buf_le16(id, ATA_ID_WORDS);
1970
Tejun Heo49016ac2006-02-21 02:12:11 +09001971 /* sanity check */
Tejun Heoa4f57492006-09-12 20:35:49 -07001972 rc = -EINVAL;
Alan Cox60700682007-06-07 16:13:55 +01001973 reason = "device reports invalid type";
Tejun Heoa4f57492006-09-12 20:35:49 -07001974
Hannes Reinecke9162c652014-11-05 13:08:21 +01001975 if (class == ATA_DEV_ATA || class == ATA_DEV_ZAC) {
Tejun Heoa4f57492006-09-12 20:35:49 -07001976 if (!ata_id_is_ata(id) && !ata_id_is_cfa(id))
1977 goto err_out;
Andy Whitcroftdb63a4c2012-05-04 22:15:10 +01001978 if (ap->host->flags & ATA_HOST_IGNORE_ATA &&
1979 ata_id_is_ata(id)) {
1980 ata_dev_dbg(dev,
1981 "host indicates ignore ATA devices, ignored\n");
1982 return -ENOENT;
1983 }
Tejun Heoa4f57492006-09-12 20:35:49 -07001984 } else {
1985 if (ata_id_is_ata(id))
1986 goto err_out;
Tejun Heo49016ac2006-02-21 02:12:11 +09001987 }
1988
Mark Lord169439c2007-04-17 18:26:07 -04001989 if (!tried_spinup && (id[2] == 0x37c8 || id[2] == 0x738c)) {
1990 tried_spinup = 1;
1991 /*
1992 * Drive powered-up in standby mode, and requires a specific
1993 * SET_FEATURES spin-up subcommand before it will accept
1994 * anything other than the original IDENTIFY command.
1995 */
Jeff Garzik218f3d32007-10-25 00:33:27 -04001996 err_mask = ata_dev_set_feature(dev, SETFEATURES_SPINUP, 0);
Ryan Powerfb0582f92007-08-10 13:59:35 -07001997 if (err_mask && id[2] != 0x738c) {
Mark Lord169439c2007-04-17 18:26:07 -04001998 rc = -EIO;
1999 reason = "SPINUP failed";
2000 goto err_out;
2001 }
2002 /*
2003 * If the drive initially returned incomplete IDENTIFY info,
2004 * we now must reissue the IDENTIFY command.
2005 */
2006 if (id[2] == 0x37c8)
2007 goto retry;
2008 }
2009
Hannes Reinecke9162c652014-11-05 13:08:21 +01002010 if ((flags & ATA_READID_POSTRESET) &&
2011 (class == ATA_DEV_ATA || class == ATA_DEV_ZAC)) {
Tejun Heo49016ac2006-02-21 02:12:11 +09002012 /*
2013 * The exact sequence expected by certain pre-ATA4 drives is:
2014 * SRST RESET
Alan Cox50a99012007-08-08 14:27:00 +01002015 * IDENTIFY (optional in early ATA)
2016 * INITIALIZE DEVICE PARAMETERS (later IDE and ATA)
Tejun Heo49016ac2006-02-21 02:12:11 +09002017 * anything else..
2018 * Some drives were very specific about that exact sequence.
Alan Cox50a99012007-08-08 14:27:00 +01002019 *
2020 * Note that ATA4 says lba is mandatory so the second check
Adam Buchbinderc9404c92009-12-18 15:40:42 -05002021 * should never trigger.
Tejun Heo49016ac2006-02-21 02:12:11 +09002022 */
2023 if (ata_id_major_version(id) < 4 || !ata_id_has_lba(id)) {
Tejun Heo3373efd2006-05-15 20:57:53 +09002024 err_mask = ata_dev_init_params(dev, id[3], id[6]);
Tejun Heo49016ac2006-02-21 02:12:11 +09002025 if (err_mask) {
2026 rc = -EIO;
2027 reason = "INIT_DEV_PARAMS failed";
2028 goto err_out;
2029 }
2030
2031 /* current CHS translation info (id[53-58]) might be
2032 * changed. reread the identify device info.
2033 */
Tejun Heobff04642006-11-10 18:08:10 +09002034 flags &= ~ATA_READID_POSTRESET;
Tejun Heo49016ac2006-02-21 02:12:11 +09002035 goto retry;
2036 }
2037 }
2038
2039 *p_class = class;
Tejun Heofe635c72006-05-15 20:57:35 +09002040
Tejun Heo49016ac2006-02-21 02:12:11 +09002041 return 0;
2042
2043 err_out:
Tejun Heo88574552006-06-25 20:00:35 +09002044 if (ata_msg_warn(ap))
Joe Perchesa9a79df2011-04-15 15:51:59 -07002045 ata_dev_warn(dev, "failed to IDENTIFY (%s, err_mask=0x%x)\n",
2046 reason, err_mask);
Tejun Heo49016ac2006-02-21 02:12:11 +09002047 return rc;
2048}
2049
Christoph Hellwigf01f62c2017-06-04 14:42:20 +02002050/**
2051 * ata_read_log_page - read a specific log page
2052 * @dev: target device
2053 * @log: log to read
2054 * @page: page to read
2055 * @buf: buffer to store read page
2056 * @sectors: number of sectors to read
2057 *
2058 * Read log page using READ_LOG_EXT command.
2059 *
2060 * LOCKING:
2061 * Kernel thread context (may sleep).
2062 *
2063 * RETURNS:
2064 * 0 on success, AC_ERR_* mask otherwise.
2065 */
2066unsigned int ata_read_log_page(struct ata_device *dev, u8 log,
2067 u8 page, void *buf, unsigned int sectors)
2068{
2069 unsigned long ap_flags = dev->link->ap->flags;
2070 struct ata_taskfile tf;
2071 unsigned int err_mask;
2072 bool dma = false;
2073
2074 DPRINTK("read log page - log 0x%x, page 0x%x\n", log, page);
2075
2076 /*
2077 * Return error without actually issuing the command on controllers
2078 * which e.g. lockup on a read log page.
2079 */
2080 if (ap_flags & ATA_FLAG_NO_LOG_PAGE)
2081 return AC_ERR_DEV;
2082
2083retry:
2084 ata_tf_init(dev, &tf);
2085 if (dev->dma_mode && ata_id_has_read_log_dma_ext(dev->id) &&
Damien Le Moal7cfdfdc2017-07-10 14:45:20 +09002086 !(dev->horkage & ATA_HORKAGE_NO_DMA_LOG)) {
Christoph Hellwigf01f62c2017-06-04 14:42:20 +02002087 tf.command = ATA_CMD_READ_LOG_DMA_EXT;
2088 tf.protocol = ATA_PROT_DMA;
2089 dma = true;
2090 } else {
2091 tf.command = ATA_CMD_READ_LOG_EXT;
2092 tf.protocol = ATA_PROT_PIO;
2093 dma = false;
2094 }
2095 tf.lbal = log;
2096 tf.lbam = page;
2097 tf.nsect = sectors;
2098 tf.hob_nsect = sectors >> 8;
2099 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_LBA48 | ATA_TFLAG_DEVICE;
2100
2101 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_FROM_DEVICE,
2102 buf, sectors * ATA_SECT_SIZE, 0);
2103
2104 if (err_mask && dma) {
Damien Le Moal7cfdfdc2017-07-10 14:45:20 +09002105 dev->horkage |= ATA_HORKAGE_NO_DMA_LOG;
2106 ata_dev_warn(dev, "READ LOG DMA EXT failed, trying PIO\n");
Christoph Hellwigf01f62c2017-06-04 14:42:20 +02002107 goto retry;
2108 }
2109
2110 DPRINTK("EXIT, err_mask=%x\n", err_mask);
2111 return err_mask;
2112}
2113
Christoph Hellwigefe205a2017-06-04 14:42:21 +02002114static bool ata_log_supported(struct ata_device *dev, u8 log)
2115{
2116 struct ata_port *ap = dev->link->ap;
2117
2118 if (ata_read_log_page(dev, ATA_LOG_DIRECTORY, 0, ap->sector_buf, 1))
2119 return false;
2120 return get_unaligned_le16(&ap->sector_buf[log * 2]) ? true : false;
2121}
2122
Christoph Hellwiga0fd2452017-06-04 14:42:23 +02002123static bool ata_identify_page_supported(struct ata_device *dev, u8 page)
2124{
2125 struct ata_port *ap = dev->link->ap;
2126 unsigned int err, i;
2127
2128 if (!ata_log_supported(dev, ATA_LOG_IDENTIFY_DEVICE)) {
2129 ata_dev_warn(dev, "ATA Identify Device Log not supported\n");
2130 return false;
2131 }
2132
2133 /*
2134 * Read IDENTIFY DEVICE data log, page 0, to figure out if the page is
2135 * supported.
2136 */
2137 err = ata_read_log_page(dev, ATA_LOG_IDENTIFY_DEVICE, 0, ap->sector_buf,
2138 1);
2139 if (err) {
2140 ata_dev_info(dev,
2141 "failed to get Device Identify Log Emask 0x%x\n",
2142 err);
2143 return false;
2144 }
2145
2146 for (i = 0; i < ap->sector_buf[8]; i++) {
2147 if (ap->sector_buf[9 + i] == page)
2148 return true;
2149 }
2150
2151 return false;
2152}
2153
Tejun Heo90627122009-01-29 20:31:36 +09002154static int ata_do_link_spd_horkage(struct ata_device *dev)
2155{
2156 struct ata_link *plink = ata_dev_phys_link(dev);
2157 u32 target, target_limit;
2158
2159 if (!sata_scr_valid(plink))
2160 return 0;
2161
2162 if (dev->horkage & ATA_HORKAGE_1_5_GBPS)
2163 target = 1;
2164 else
2165 return 0;
2166
2167 target_limit = (1 << target) - 1;
2168
2169 /* if already on stricter limit, no need to push further */
2170 if (plink->sata_spd_limit <= target_limit)
2171 return 0;
2172
2173 plink->sata_spd_limit = target_limit;
2174
2175 /* Request another EH round by returning -EAGAIN if link is
2176 * going faster than the target speed. Forward progress is
2177 * guaranteed by setting sata_spd_limit to target_limit above.
2178 */
2179 if (plink->sata_spd > target) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07002180 ata_dev_info(dev, "applying link speed limit horkage to %s\n",
2181 sata_spd_string(target));
Tejun Heo90627122009-01-29 20:31:36 +09002182 return -EAGAIN;
2183 }
2184 return 0;
2185}
2186
Tejun Heo3373efd2006-05-15 20:57:53 +09002187static inline u8 ata_dev_knobble(struct ata_device *dev)
Tejun Heo4b2f3ed2006-03-01 16:09:36 +09002188{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09002189 struct ata_port *ap = dev->link->ap;
Jens Axboe9ce8e302008-08-27 15:23:18 +02002190
2191 if (ata_dev_blacklisted(dev) & ATA_HORKAGE_BRIDGE_OK)
2192 return 0;
2193
Tejun Heo9af5c9c2007-08-06 18:36:22 +09002194 return ((ap->cbl == ATA_CBL_SATA) && (!ata_id_is_sata(dev->id)));
Tejun Heo4b2f3ed2006-03-01 16:09:36 +09002195}
2196
Hannes Reinecke5a233552016-04-25 12:45:46 +02002197static void ata_dev_config_ncq_send_recv(struct ata_device *dev)
2198{
2199 struct ata_port *ap = dev->link->ap;
2200 unsigned int err_mask;
2201
Christoph Hellwigefe205a2017-06-04 14:42:21 +02002202 if (!ata_log_supported(dev, ATA_LOG_NCQ_SEND_RECV)) {
2203 ata_dev_warn(dev, "NCQ Send/Recv Log not supported\n");
Hannes Reineckefe5af0c2016-04-25 12:45:48 +02002204 return;
2205 }
Hannes Reinecke5a233552016-04-25 12:45:46 +02002206 err_mask = ata_read_log_page(dev, ATA_LOG_NCQ_SEND_RECV,
2207 0, ap->sector_buf, 1);
2208 if (err_mask) {
2209 ata_dev_dbg(dev,
2210 "failed to get NCQ Send/Recv Log Emask 0x%x\n",
2211 err_mask);
2212 } else {
2213 u8 *cmds = dev->ncq_send_recv_cmds;
2214
2215 dev->flags |= ATA_DFLAG_NCQ_SEND_RECV;
2216 memcpy(cmds, ap->sector_buf, ATA_LOG_NCQ_SEND_RECV_SIZE);
2217
2218 if (dev->horkage & ATA_HORKAGE_NO_NCQ_TRIM) {
2219 ata_dev_dbg(dev, "disabling queued TRIM support\n");
2220 cmds[ATA_LOG_NCQ_SEND_RECV_DSM_OFFSET] &=
2221 ~ATA_LOG_NCQ_SEND_RECV_DSM_TRIM;
2222 }
2223 }
2224}
2225
Hannes Reinecke284b3b72016-04-25 12:45:54 +02002226static void ata_dev_config_ncq_non_data(struct ata_device *dev)
2227{
2228 struct ata_port *ap = dev->link->ap;
2229 unsigned int err_mask;
Hannes Reinecke284b3b72016-04-25 12:45:54 +02002230
Christoph Hellwigefe205a2017-06-04 14:42:21 +02002231 if (!ata_log_supported(dev, ATA_LOG_NCQ_NON_DATA)) {
Hannes Reinecke284b3b72016-04-25 12:45:54 +02002232 ata_dev_warn(dev,
2233 "NCQ Send/Recv Log not supported\n");
2234 return;
2235 }
2236 err_mask = ata_read_log_page(dev, ATA_LOG_NCQ_NON_DATA,
2237 0, ap->sector_buf, 1);
2238 if (err_mask) {
2239 ata_dev_dbg(dev,
2240 "failed to get NCQ Non-Data Log Emask 0x%x\n",
2241 err_mask);
2242 } else {
2243 u8 *cmds = dev->ncq_non_data_cmds;
2244
2245 memcpy(cmds, ap->sector_buf, ATA_LOG_NCQ_NON_DATA_SIZE);
2246 }
2247}
2248
Adam Manzanares8e061782016-10-17 11:27:29 -07002249static void ata_dev_config_ncq_prio(struct ata_device *dev)
2250{
2251 struct ata_port *ap = dev->link->ap;
2252 unsigned int err_mask;
2253
Adam Manzanares9f56eca2016-12-13 12:00:05 -08002254 if (!(dev->flags & ATA_DFLAG_NCQ_PRIO_ENABLE)) {
2255 dev->flags &= ~ATA_DFLAG_NCQ_PRIO;
2256 return;
2257 }
2258
Adam Manzanares8e061782016-10-17 11:27:29 -07002259 err_mask = ata_read_log_page(dev,
Christoph Hellwig1d51d5f2017-06-04 14:42:22 +02002260 ATA_LOG_IDENTIFY_DEVICE,
Adam Manzanares8e061782016-10-17 11:27:29 -07002261 ATA_LOG_SATA_SETTINGS,
2262 ap->sector_buf,
2263 1);
2264 if (err_mask) {
2265 ata_dev_dbg(dev,
2266 "failed to get Identify Device data, Emask 0x%x\n",
2267 err_mask);
2268 return;
2269 }
2270
Adam Manzanares9f56eca2016-12-13 12:00:05 -08002271 if (ap->sector_buf[ATA_LOG_NCQ_PRIO_OFFSET] & BIT(3)) {
Adam Manzanares8e061782016-10-17 11:27:29 -07002272 dev->flags |= ATA_DFLAG_NCQ_PRIO;
Adam Manzanares9f56eca2016-12-13 12:00:05 -08002273 } else {
2274 dev->flags &= ~ATA_DFLAG_NCQ_PRIO;
Adam Manzanares8e061782016-10-17 11:27:29 -07002275 ata_dev_dbg(dev, "SATA page does not support priority\n");
Adam Manzanares9f56eca2016-12-13 12:00:05 -08002276 }
Adam Manzanares8e061782016-10-17 11:27:29 -07002277
2278}
2279
Shaohua Li388539f2009-07-27 09:24:35 +08002280static int ata_dev_config_ncq(struct ata_device *dev,
Tejun Heoa6e6ce82006-05-15 21:03:48 +09002281 char *desc, size_t desc_sz)
2282{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09002283 struct ata_port *ap = dev->link->ap;
Tejun Heoa6e6ce82006-05-15 21:03:48 +09002284 int hdepth = 0, ddepth = ata_id_queue_depth(dev->id);
Shaohua Li388539f2009-07-27 09:24:35 +08002285 unsigned int err_mask;
2286 char *aa_desc = "";
Tejun Heoa6e6ce82006-05-15 21:03:48 +09002287
2288 if (!ata_id_has_ncq(dev->id)) {
2289 desc[0] = '\0';
Shaohua Li388539f2009-07-27 09:24:35 +08002290 return 0;
Tejun Heoa6e6ce82006-05-15 21:03:48 +09002291 }
Tejun Heo75683fe2007-07-05 13:31:27 +09002292 if (dev->horkage & ATA_HORKAGE_NONCQ) {
Alan Cox6919a0a2006-10-27 19:08:46 -07002293 snprintf(desc, desc_sz, "NCQ (not used)");
Shaohua Li388539f2009-07-27 09:24:35 +08002294 return 0;
Alan Cox6919a0a2006-10-27 19:08:46 -07002295 }
Tejun Heoa6e6ce82006-05-15 21:03:48 +09002296 if (ap->flags & ATA_FLAG_NCQ) {
Jens Axboe69278f72018-05-11 12:51:10 -06002297 hdepth = min(ap->scsi_host->can_queue, ATA_MAX_QUEUE);
Tejun Heoa6e6ce82006-05-15 21:03:48 +09002298 dev->flags |= ATA_DFLAG_NCQ;
2299 }
2300
Shaohua Li388539f2009-07-27 09:24:35 +08002301 if (!(dev->horkage & ATA_HORKAGE_BROKEN_FPDMA_AA) &&
2302 (ap->flags & ATA_FLAG_FPDMA_AA) &&
2303 ata_id_has_fpdma_aa(dev->id)) {
2304 err_mask = ata_dev_set_feature(dev, SETFEATURES_SATA_ENABLE,
2305 SATA_FPDMA_AA);
2306 if (err_mask) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07002307 ata_dev_err(dev,
2308 "failed to enable AA (error_mask=0x%x)\n",
2309 err_mask);
Shaohua Li388539f2009-07-27 09:24:35 +08002310 if (err_mask != AC_ERR_DEV) {
2311 dev->horkage |= ATA_HORKAGE_BROKEN_FPDMA_AA;
2312 return -EIO;
2313 }
2314 } else
2315 aa_desc = ", AA";
2316 }
2317
Tejun Heoa6e6ce82006-05-15 21:03:48 +09002318 if (hdepth >= ddepth)
Shaohua Li388539f2009-07-27 09:24:35 +08002319 snprintf(desc, desc_sz, "NCQ (depth %d)%s", ddepth, aa_desc);
Tejun Heoa6e6ce82006-05-15 21:03:48 +09002320 else
Shaohua Li388539f2009-07-27 09:24:35 +08002321 snprintf(desc, desc_sz, "NCQ (depth %d/%d)%s", hdepth,
2322 ddepth, aa_desc);
Marc Carinoed369112013-08-24 23:22:50 -07002323
Hannes Reinecke284b3b72016-04-25 12:45:54 +02002324 if ((ap->flags & ATA_FLAG_FPDMA_AUX)) {
2325 if (ata_id_has_ncq_send_and_recv(dev->id))
2326 ata_dev_config_ncq_send_recv(dev);
2327 if (ata_id_has_ncq_non_data(dev->id))
2328 ata_dev_config_ncq_non_data(dev);
Adam Manzanares8e061782016-10-17 11:27:29 -07002329 if (ata_id_has_ncq_prio(dev->id))
2330 ata_dev_config_ncq_prio(dev);
Marc Carinoed369112013-08-24 23:22:50 -07002331 }
2332
Shaohua Li388539f2009-07-27 09:24:35 +08002333 return 0;
Tejun Heoa6e6ce82006-05-15 21:03:48 +09002334}
2335
Hannes Reineckee87fd282016-04-04 11:43:55 +02002336static void ata_dev_config_sense_reporting(struct ata_device *dev)
2337{
2338 unsigned int err_mask;
2339
2340 if (!ata_id_has_sense_reporting(dev->id))
2341 return;
2342
2343 if (ata_id_sense_reporting_enabled(dev->id))
2344 return;
2345
2346 err_mask = ata_dev_set_feature(dev, SETFEATURE_SENSE_DATA, 0x1);
2347 if (err_mask) {
2348 ata_dev_dbg(dev,
2349 "failed to enable Sense Data Reporting, Emask 0x%x\n",
2350 err_mask);
2351 }
2352}
2353
Hannes Reinecke6d1003a2016-04-25 12:45:56 +02002354static void ata_dev_config_zac(struct ata_device *dev)
2355{
2356 struct ata_port *ap = dev->link->ap;
2357 unsigned int err_mask;
2358 u8 *identify_buf = ap->sector_buf;
Hannes Reinecke6d1003a2016-04-25 12:45:56 +02002359
2360 dev->zac_zones_optimal_open = U32_MAX;
2361 dev->zac_zones_optimal_nonseq = U32_MAX;
2362 dev->zac_zones_max_open = U32_MAX;
2363
2364 /*
2365 * Always set the 'ZAC' flag for Host-managed devices.
2366 */
2367 if (dev->class == ATA_DEV_ZAC)
2368 dev->flags |= ATA_DFLAG_ZAC;
2369 else if (ata_id_zoned_cap(dev->id) == 0x01)
2370 /*
2371 * Check for host-aware devices.
2372 */
2373 dev->flags |= ATA_DFLAG_ZAC;
2374
2375 if (!(dev->flags & ATA_DFLAG_ZAC))
2376 return;
2377
Christoph Hellwiga0fd2452017-06-04 14:42:23 +02002378 if (!ata_identify_page_supported(dev, ATA_LOG_ZONED_INFORMATION)) {
Hannes Reinecke6d1003a2016-04-25 12:45:56 +02002379 ata_dev_warn(dev,
2380 "ATA Zoned Information Log not supported\n");
2381 return;
2382 }
2383
2384 /*
2385 * Read IDENTIFY DEVICE data log, page 9 (Zoned-device information)
2386 */
Christoph Hellwig1d51d5f2017-06-04 14:42:22 +02002387 err_mask = ata_read_log_page(dev, ATA_LOG_IDENTIFY_DEVICE,
Hannes Reinecke6d1003a2016-04-25 12:45:56 +02002388 ATA_LOG_ZONED_INFORMATION,
2389 identify_buf, 1);
2390 if (!err_mask) {
2391 u64 zoned_cap, opt_open, opt_nonseq, max_open;
2392
2393 zoned_cap = get_unaligned_le64(&identify_buf[8]);
2394 if ((zoned_cap >> 63))
2395 dev->zac_zoned_cap = (zoned_cap & 1);
2396 opt_open = get_unaligned_le64(&identify_buf[24]);
2397 if ((opt_open >> 63))
2398 dev->zac_zones_optimal_open = (u32)opt_open;
2399 opt_nonseq = get_unaligned_le64(&identify_buf[32]);
2400 if ((opt_nonseq >> 63))
2401 dev->zac_zones_optimal_nonseq = (u32)opt_nonseq;
2402 max_open = get_unaligned_le64(&identify_buf[40]);
2403 if ((max_open >> 63))
2404 dev->zac_zones_max_open = (u32)max_open;
2405 }
2406}
2407
Christoph Hellwig818831c2017-06-04 14:42:24 +02002408static void ata_dev_config_trusted(struct ata_device *dev)
2409{
2410 struct ata_port *ap = dev->link->ap;
2411 u64 trusted_cap;
2412 unsigned int err;
2413
Christoph Hellwige8f11db2017-08-29 14:42:06 +02002414 if (!ata_id_has_trusted(dev->id))
2415 return;
2416
Christoph Hellwig818831c2017-06-04 14:42:24 +02002417 if (!ata_identify_page_supported(dev, ATA_LOG_SECURITY)) {
2418 ata_dev_warn(dev,
2419 "Security Log not supported\n");
2420 return;
2421 }
2422
2423 err = ata_read_log_page(dev, ATA_LOG_IDENTIFY_DEVICE, ATA_LOG_SECURITY,
2424 ap->sector_buf, 1);
2425 if (err) {
2426 ata_dev_dbg(dev,
2427 "failed to read Security Log, Emask 0x%x\n", err);
2428 return;
2429 }
2430
2431 trusted_cap = get_unaligned_le64(&ap->sector_buf[40]);
2432 if (!(trusted_cap & (1ULL << 63))) {
2433 ata_dev_dbg(dev,
2434 "Trusted Computing capability qword not valid!\n");
2435 return;
2436 }
2437
2438 if (trusted_cap & (1 << 0))
2439 dev->flags |= ATA_DFLAG_TRUSTED;
2440}
2441
Tejun Heo49016ac2006-02-21 02:12:11 +09002442/**
Tejun Heoffeae412006-03-01 16:09:35 +09002443 * ata_dev_configure - Configure the specified ATA/ATAPI device
Tejun Heoffeae412006-03-01 16:09:35 +09002444 * @dev: Target device to configure
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445 *
Tejun Heoffeae412006-03-01 16:09:35 +09002446 * Configure @dev according to @dev->id. Generic and low-level
2447 * driver specific fixups are also applied.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 *
2449 * LOCKING:
Tejun Heoffeae412006-03-01 16:09:35 +09002450 * Kernel thread context (may sleep)
2451 *
2452 * RETURNS:
2453 * 0 on success, -errno otherwise
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 */
Tejun Heoefdaedc2006-11-01 18:38:52 +09002455int ata_dev_configure(struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09002457 struct ata_port *ap = dev->link->ap;
2458 struct ata_eh_context *ehc = &dev->link->eh_context;
Tejun Heo67465442007-05-15 03:28:16 +09002459 int print_info = ehc->i.flags & ATA_EHI_PRINTINFO;
Tejun Heo1148c3a2006-03-13 19:48:04 +09002460 const u16 *id = dev->id;
Tejun Heo7dc951a2007-11-27 19:43:42 +09002461 unsigned long xfer_mask;
Shane Huang65fe1f02012-09-07 22:40:01 +08002462 unsigned int err_mask;
Alan Coxb352e572006-08-10 18:52:12 +01002463 char revbuf[7]; /* XYZ-99\0 */
Eric D. Mudama3f64f562007-01-30 23:00:40 -07002464 char fwrevbuf[ATA_ID_FW_REV_LEN+1];
2465 char modelbuf[ATA_ID_PROD_LEN+1];
Brian Kinge6d902a2006-06-28 08:30:31 -05002466 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467
Borislav Petkov0dd4b212006-06-23 02:29:08 -04002468 if (!ata_dev_enabled(dev) && ata_msg_info(ap)) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07002469 ata_dev_info(dev, "%s: ENTER/EXIT -- nodev\n", __func__);
Tejun Heoffeae412006-03-01 16:09:35 +09002470 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471 }
2472
Borislav Petkov0dd4b212006-06-23 02:29:08 -04002473 if (ata_msg_probe(ap))
Joe Perchesa9a79df2011-04-15 15:51:59 -07002474 ata_dev_dbg(dev, "%s: ENTER\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475
Tejun Heo75683fe2007-07-05 13:31:27 +09002476 /* set horkage */
2477 dev->horkage |= ata_dev_blacklisted(dev);
Tejun Heo33267322008-02-13 09:15:09 +09002478 ata_force_horkage(dev);
Tejun Heo75683fe2007-07-05 13:31:27 +09002479
Tejun Heo50af2fa2008-05-19 01:15:14 +09002480 if (dev->horkage & ATA_HORKAGE_DISABLE) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07002481 ata_dev_info(dev, "unsupported device, disabling\n");
Tejun Heo50af2fa2008-05-19 01:15:14 +09002482 ata_dev_disable(dev);
2483 return 0;
2484 }
2485
Tejun Heo2486fa52008-07-31 07:52:40 +09002486 if ((!atapi_enabled || (ap->flags & ATA_FLAG_NO_ATAPI)) &&
2487 dev->class == ATA_DEV_ATAPI) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07002488 ata_dev_warn(dev, "WARNING: ATAPI is %s, device ignored\n",
2489 atapi_enabled ? "not supported with this driver"
2490 : "disabled");
Tejun Heo2486fa52008-07-31 07:52:40 +09002491 ata_dev_disable(dev);
2492 return 0;
2493 }
2494
Tejun Heo90627122009-01-29 20:31:36 +09002495 rc = ata_do_link_spd_horkage(dev);
2496 if (rc)
2497 return rc;
2498
Tejun Heoecd75ad2014-01-16 09:47:17 -05002499 /* some WD SATA-1 drives have issues with LPM, turn on NOLPM for them */
2500 if ((dev->horkage & ATA_HORKAGE_WD_BROKEN_LPM) &&
2501 (id[ATA_ID_SATA_CAPABILITY] & 0xe) == 0x2)
2502 dev->horkage |= ATA_HORKAGE_NOLPM;
2503
Hans de Goede240630e2018-07-01 12:15:46 +02002504 if (ap->flags & ATA_FLAG_NO_LPM)
2505 dev->horkage |= ATA_HORKAGE_NOLPM;
2506
Tejun Heoecd75ad2014-01-16 09:47:17 -05002507 if (dev->horkage & ATA_HORKAGE_NOLPM) {
2508 ata_dev_warn(dev, "LPM support broken, forcing max_power\n");
2509 dev->link->ap->target_lpm_policy = ATA_LPM_MAX_POWER;
2510 }
2511
Tejun Heo67465442007-05-15 03:28:16 +09002512 /* let ACPI work its magic */
2513 rc = ata_acpi_on_devcfg(dev);
2514 if (rc)
2515 return rc;
Kristen Carlson Accardi08573a82006-11-10 16:14:47 -08002516
Tejun Heo05027ad2007-09-03 12:32:57 +09002517 /* massage HPA, do it early as it might change IDENTIFY data */
2518 rc = ata_hpa_resize(dev);
2519 if (rc)
2520 return rc;
2521
Tejun Heoc39f5eb2006-03-13 19:51:19 +09002522 /* print device capabilities */
Borislav Petkov0dd4b212006-06-23 02:29:08 -04002523 if (ata_msg_probe(ap))
Joe Perchesa9a79df2011-04-15 15:51:59 -07002524 ata_dev_dbg(dev,
2525 "%s: cfg 49:%04x 82:%04x 83:%04x 84:%04x "
2526 "85:%04x 86:%04x 87:%04x 88:%04x\n",
2527 __func__,
2528 id[49], id[82], id[83], id[84],
2529 id[85], id[86], id[87], id[88]);
Tejun Heoc39f5eb2006-03-13 19:51:19 +09002530
Tejun Heo208a9932006-03-05 17:55:58 +09002531 /* initialize to-be-configured parameters */
Tejun Heoea1dd4e2006-04-02 18:51:53 +09002532 dev->flags &= ~ATA_DFLAG_CFG_MASK;
Tejun Heo208a9932006-03-05 17:55:58 +09002533 dev->max_sectors = 0;
2534 dev->cdb_len = 0;
2535 dev->n_sectors = 0;
2536 dev->cylinders = 0;
2537 dev->heads = 0;
2538 dev->sectors = 0;
Mark Lorde18086d2009-03-19 13:32:21 -04002539 dev->multi_count = 0;
Tejun Heo208a9932006-03-05 17:55:58 +09002540
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541 /*
2542 * common ATA, ATAPI feature tests
2543 */
2544
Tejun Heoff8854b2006-03-06 04:31:56 +09002545 /* find max transfer mode; for printk only */
Tejun Heo1148c3a2006-03-13 19:48:04 +09002546 xfer_mask = ata_id_xfermask(id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547
Borislav Petkov0dd4b212006-06-23 02:29:08 -04002548 if (ata_msg_probe(ap))
2549 ata_dump_id(id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550
Albert Leeef143d52007-06-05 13:01:33 +08002551 /* SCSI only uses 4-char revisions, dump full 8 chars from ATA */
2552 ata_id_c_string(dev->id, fwrevbuf, ATA_ID_FW_REV,
2553 sizeof(fwrevbuf));
2554
2555 ata_id_c_string(dev->id, modelbuf, ATA_ID_PROD,
2556 sizeof(modelbuf));
2557
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558 /* ATA-specific feature tests */
Hannes Reinecke9162c652014-11-05 13:08:21 +01002559 if (dev->class == ATA_DEV_ATA || dev->class == ATA_DEV_ZAC) {
Alan Coxb352e572006-08-10 18:52:12 +01002560 if (ata_id_is_cfa(id)) {
Sergei Shtylyov62afe5d2009-04-13 20:50:00 +04002561 /* CPRM may make this media unusable */
2562 if (id[ATA_ID_CFA_KEY_MGMT] & 1)
Joe Perchesa9a79df2011-04-15 15:51:59 -07002563 ata_dev_warn(dev,
2564 "supports DRM functions and may not be fully accessible\n");
Alan Coxb352e572006-08-10 18:52:12 +01002565 snprintf(revbuf, 7, "CFA");
Alan Coxae8d4ee2007-11-04 22:05:49 -05002566 } else {
Jeff Garzik2dcb4072007-10-19 06:42:56 -04002567 snprintf(revbuf, 7, "ATA-%d", ata_id_major_version(id));
Alan Coxae8d4ee2007-11-04 22:05:49 -05002568 /* Warn the user if the device has TPM extensions */
2569 if (ata_id_has_tpm(id))
Joe Perchesa9a79df2011-04-15 15:51:59 -07002570 ata_dev_warn(dev,
2571 "supports DRM functions and may not be fully accessible\n");
Alan Coxae8d4ee2007-11-04 22:05:49 -05002572 }
Alan Coxb352e572006-08-10 18:52:12 +01002573
Tejun Heo1148c3a2006-03-13 19:48:04 +09002574 dev->n_sectors = ata_id_n_sectors(id);
Tejun Heo29407402006-02-12 22:47:04 +09002575
Mark Lorde18086d2009-03-19 13:32:21 -04002576 /* get current R/W Multiple count setting */
2577 if ((dev->id[47] >> 8) == 0x80 && (dev->id[59] & 0x100)) {
2578 unsigned int max = dev->id[47] & 0xff;
2579 unsigned int cnt = dev->id[59] & 0xff;
2580 /* only recognize/allow powers of two here */
2581 if (is_power_of_2(max) && is_power_of_2(cnt))
2582 if (cnt <= max)
2583 dev->multi_count = cnt;
2584 }
Eric D. Mudama3f64f562007-01-30 23:00:40 -07002585
Tejun Heo1148c3a2006-03-13 19:48:04 +09002586 if (ata_id_has_lba(id)) {
Tejun Heo4c2d7212006-03-05 17:55:58 +09002587 const char *lba_desc;
Shaohua Li388539f2009-07-27 09:24:35 +08002588 char ncq_desc[24];
Albert Lee8bf62ece2005-05-12 15:29:42 -04002589
Tejun Heo4c2d7212006-03-05 17:55:58 +09002590 lba_desc = "LBA";
2591 dev->flags |= ATA_DFLAG_LBA;
Tejun Heo1148c3a2006-03-13 19:48:04 +09002592 if (ata_id_has_lba48(id)) {
Albert Lee8bf62ece2005-05-12 15:29:42 -04002593 dev->flags |= ATA_DFLAG_LBA48;
Tejun Heo4c2d7212006-03-05 17:55:58 +09002594 lba_desc = "LBA48";
Tejun Heo6fc49ad2006-11-11 20:10:45 +09002595
2596 if (dev->n_sectors >= (1UL << 28) &&
2597 ata_id_has_flush_ext(id))
2598 dev->flags |= ATA_DFLAG_FLUSH_EXT;
Tejun Heo4c2d7212006-03-05 17:55:58 +09002599 }
Albert Lee8bf62ece2005-05-12 15:29:42 -04002600
Tejun Heoa6e6ce82006-05-15 21:03:48 +09002601 /* config NCQ */
Shaohua Li388539f2009-07-27 09:24:35 +08002602 rc = ata_dev_config_ncq(dev, ncq_desc, sizeof(ncq_desc));
2603 if (rc)
2604 return rc;
Tejun Heoa6e6ce82006-05-15 21:03:48 +09002605
Albert Lee8bf62ece2005-05-12 15:29:42 -04002606 /* print device info to dmesg */
Eric D. Mudama3f64f562007-01-30 23:00:40 -07002607 if (ata_msg_drv(ap) && print_info) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07002608 ata_dev_info(dev, "%s: %s, %s, max %s\n",
2609 revbuf, modelbuf, fwrevbuf,
2610 ata_mode_string(xfer_mask));
2611 ata_dev_info(dev,
2612 "%llu sectors, multi %u: %s %s\n",
Tejun Heof15a1da2006-05-15 20:57:56 +09002613 (unsigned long long)dev->n_sectors,
Eric D. Mudama3f64f562007-01-30 23:00:40 -07002614 dev->multi_count, lba_desc, ncq_desc);
2615 }
Tejun Heoffeae412006-03-01 16:09:35 +09002616 } else {
Albert Lee8bf62ece2005-05-12 15:29:42 -04002617 /* CHS */
2618
2619 /* Default translation */
Tejun Heo1148c3a2006-03-13 19:48:04 +09002620 dev->cylinders = id[1];
2621 dev->heads = id[3];
2622 dev->sectors = id[6];
Albert Lee8bf62ece2005-05-12 15:29:42 -04002623
Tejun Heo1148c3a2006-03-13 19:48:04 +09002624 if (ata_id_current_chs_valid(id)) {
Albert Lee8bf62ece2005-05-12 15:29:42 -04002625 /* Current CHS translation is valid. */
Tejun Heo1148c3a2006-03-13 19:48:04 +09002626 dev->cylinders = id[54];
2627 dev->heads = id[55];
2628 dev->sectors = id[56];
Albert Lee8bf62ece2005-05-12 15:29:42 -04002629 }
2630
2631 /* print device info to dmesg */
Eric D. Mudama3f64f562007-01-30 23:00:40 -07002632 if (ata_msg_drv(ap) && print_info) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07002633 ata_dev_info(dev, "%s: %s, %s, max %s\n",
2634 revbuf, modelbuf, fwrevbuf,
2635 ata_mode_string(xfer_mask));
2636 ata_dev_info(dev,
2637 "%llu sectors, multi %u, CHS %u/%u/%u\n",
2638 (unsigned long long)dev->n_sectors,
2639 dev->multi_count, dev->cylinders,
2640 dev->heads, dev->sectors);
Eric D. Mudama3f64f562007-01-30 23:00:40 -07002641 }
Albert Lee07f6f7d2005-11-01 19:33:20 +08002642 }
2643
Shane Huang803739d2012-12-17 23:18:59 +08002644 /* Check and mark DevSlp capability. Get DevSlp timing variables
2645 * from SATA Settings page of Identify Device Data Log.
Shane Huang65fe1f02012-09-07 22:40:01 +08002646 */
Shane Huang803739d2012-12-17 23:18:59 +08002647 if (ata_id_has_devslp(dev->id)) {
David Woodhouse8e725c72013-03-29 11:54:55 +00002648 u8 *sata_setting = ap->sector_buf;
Shane Huang803739d2012-12-17 23:18:59 +08002649 int i, j;
2650
2651 dev->flags |= ATA_DFLAG_DEVSLP;
Shane Huang65fe1f02012-09-07 22:40:01 +08002652 err_mask = ata_read_log_page(dev,
Christoph Hellwig1d51d5f2017-06-04 14:42:22 +02002653 ATA_LOG_IDENTIFY_DEVICE,
Shane Huang65fe1f02012-09-07 22:40:01 +08002654 ATA_LOG_SATA_SETTINGS,
Shane Huang803739d2012-12-17 23:18:59 +08002655 sata_setting,
Shane Huang65fe1f02012-09-07 22:40:01 +08002656 1);
2657 if (err_mask)
2658 ata_dev_dbg(dev,
2659 "failed to get Identify Device Data, Emask 0x%x\n",
2660 err_mask);
Shane Huang803739d2012-12-17 23:18:59 +08002661 else
2662 for (i = 0; i < ATA_LOG_DEVSLP_SIZE; i++) {
2663 j = ATA_LOG_DEVSLP_OFFSET + i;
2664 dev->devslp_timing[i] = sata_setting[j];
2665 }
Shane Huang65fe1f02012-09-07 22:40:01 +08002666 }
Hannes Reineckee87fd282016-04-04 11:43:55 +02002667 ata_dev_config_sense_reporting(dev);
Hannes Reinecke6d1003a2016-04-25 12:45:56 +02002668 ata_dev_config_zac(dev);
Christoph Hellwig818831c2017-06-04 14:42:24 +02002669 ata_dev_config_trusted(dev);
Minwoo Imb1ffbf82017-06-24 03:41:10 +09002670 dev->cdb_len = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671 }
2672
2673 /* ATAPI-specific feature tests */
Jeff Garzik2c13b7c2005-11-14 14:14:16 -05002674 else if (dev->class == ATA_DEV_ATAPI) {
Tejun Heo854c73a2007-09-23 13:14:11 +09002675 const char *cdb_intr_string = "";
2676 const char *atapi_an_string = "";
Tejun Heo91163002008-02-21 13:25:50 +09002677 const char *dma_dir_string = "";
Tejun Heo7d77b242007-09-23 13:14:13 +09002678 u32 sntf;
Albert Lee08a556d2006-03-31 13:29:04 +08002679
Tejun Heo1148c3a2006-03-13 19:48:04 +09002680 rc = atapi_cdb_len(id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681 if ((rc < 12) || (rc > ATAPI_CDB_LEN)) {
Borislav Petkov0dd4b212006-06-23 02:29:08 -04002682 if (ata_msg_warn(ap))
Joe Perchesa9a79df2011-04-15 15:51:59 -07002683 ata_dev_warn(dev, "unsupported CDB len\n");
Tejun Heoffeae412006-03-01 16:09:35 +09002684 rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685 goto err_out_nosup;
2686 }
Tejun Heo6e7846e2006-02-12 23:32:58 +09002687 dev->cdb_len = (unsigned int) rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688
Tejun Heo7d77b242007-09-23 13:14:13 +09002689 /* Enable ATAPI AN if both the host and device have
2690 * the support. If PMP is attached, SNTF is required
2691 * to enable ATAPI AN to discern between PHY status
2692 * changed notifications and ATAPI ANs.
Kristen Carlson Accardi9f45cbd2007-08-15 03:57:11 -04002693 */
Tejun Heoe7ecd432010-05-19 15:38:58 +02002694 if (atapi_an &&
2695 (ap->flags & ATA_FLAG_AN) && ata_id_has_atapi_AN(id) &&
Tejun Heo071f44b2008-04-07 22:47:22 +09002696 (!sata_pmp_attached(ap) ||
Tejun Heo7d77b242007-09-23 13:14:13 +09002697 sata_scr_read(&ap->link, SCR_NOTIFICATION, &sntf) == 0)) {
Kristen Carlson Accardi9f45cbd2007-08-15 03:57:11 -04002698 /* issue SET feature command to turn this on */
Jeff Garzik218f3d32007-10-25 00:33:27 -04002699 err_mask = ata_dev_set_feature(dev,
2700 SETFEATURES_SATA_ENABLE, SATA_AN);
Tejun Heo854c73a2007-09-23 13:14:11 +09002701 if (err_mask)
Joe Perchesa9a79df2011-04-15 15:51:59 -07002702 ata_dev_err(dev,
2703 "failed to enable ATAPI AN (err_mask=0x%x)\n",
2704 err_mask);
Tejun Heo854c73a2007-09-23 13:14:11 +09002705 else {
Kristen Carlson Accardi9f45cbd2007-08-15 03:57:11 -04002706 dev->flags |= ATA_DFLAG_AN;
Tejun Heo854c73a2007-09-23 13:14:11 +09002707 atapi_an_string = ", ATAPI AN";
2708 }
Kristen Carlson Accardi9f45cbd2007-08-15 03:57:11 -04002709 }
2710
Albert Lee08a556d2006-03-31 13:29:04 +08002711 if (ata_id_cdb_intr(dev->id)) {
Albert Lee312f7da2005-09-27 17:38:03 +08002712 dev->flags |= ATA_DFLAG_CDB_INTR;
Albert Lee08a556d2006-03-31 13:29:04 +08002713 cdb_intr_string = ", CDB intr";
2714 }
Albert Lee312f7da2005-09-27 17:38:03 +08002715
Vincent Pelletier966fbe12013-05-21 22:30:58 +02002716 if (atapi_dmadir || (dev->horkage & ATA_HORKAGE_ATAPI_DMADIR) || atapi_id_dmadir(dev->id)) {
Tejun Heo91163002008-02-21 13:25:50 +09002717 dev->flags |= ATA_DFLAG_DMADIR;
2718 dma_dir_string = ", DMADIR";
2719 }
2720
Aaron Luafe75952013-01-15 17:20:58 +08002721 if (ata_id_has_da(dev->id)) {
Lin Mingb1354cb2012-06-25 16:13:08 +08002722 dev->flags |= ATA_DFLAG_DA;
Aaron Luafe75952013-01-15 17:20:58 +08002723 zpodd_init(dev);
2724 }
Lin Mingb1354cb2012-06-25 16:13:08 +08002725
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726 /* print device info to dmesg */
Borislav Petkov5afc8142006-06-27 14:51:25 +02002727 if (ata_msg_drv(ap) && print_info)
Joe Perchesa9a79df2011-04-15 15:51:59 -07002728 ata_dev_info(dev,
2729 "ATAPI: %s, %s, max %s%s%s%s\n",
2730 modelbuf, fwrevbuf,
2731 ata_mode_string(xfer_mask),
2732 cdb_intr_string, atapi_an_string,
2733 dma_dir_string);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734 }
2735
Tejun Heo914ed352006-11-01 18:39:55 +09002736 /* determine max_sectors */
2737 dev->max_sectors = ATA_MAX_SECTORS;
2738 if (dev->flags & ATA_DFLAG_LBA48)
2739 dev->max_sectors = ATA_MAX_SECTORS_LBA48;
2740
Alan Coxc5038fc2007-10-25 14:21:16 +01002741 /* Limit PATA drive on SATA cable bridge transfers to udma5,
2742 200 sectors */
Tejun Heo3373efd2006-05-15 20:57:53 +09002743 if (ata_dev_knobble(dev)) {
Borislav Petkov5afc8142006-06-27 14:51:25 +02002744 if (ata_msg_drv(ap) && print_info)
Joe Perchesa9a79df2011-04-15 15:51:59 -07002745 ata_dev_info(dev, "applying bridge limits\n");
Tejun Heo5a529132006-03-24 14:07:50 +09002746 dev->udma_mask &= ATA_UDMA5;
Tejun Heo4b2f3ed2006-03-01 16:09:36 +09002747 dev->max_sectors = ATA_MAX_SECTORS;
2748 }
2749
Tony Battersbyf8d8e572007-10-30 11:44:35 -04002750 if ((dev->class == ATA_DEV_ATAPI) &&
Albert Leef442cd82007-11-15 10:35:47 +09002751 (atapi_command_packet_set(id) == TYPE_TAPE)) {
Tony Battersbyf8d8e572007-10-30 11:44:35 -04002752 dev->max_sectors = ATA_MAX_SECTORS_TAPE;
Albert Leef442cd82007-11-15 10:35:47 +09002753 dev->horkage |= ATA_HORKAGE_STUCK_ERR;
2754 }
Tony Battersbyf8d8e572007-10-30 11:44:35 -04002755
Tejun Heo75683fe2007-07-05 13:31:27 +09002756 if (dev->horkage & ATA_HORKAGE_MAX_SEC_128)
Tejun Heo03ec52d2007-04-12 13:38:11 +09002757 dev->max_sectors = min_t(unsigned int, ATA_MAX_SECTORS_128,
2758 dev->max_sectors);
Albert Lee18d6e9d2007-04-02 11:34:15 +08002759
David Milburnaf34d632015-07-13 11:48:23 -05002760 if (dev->horkage & ATA_HORKAGE_MAX_SEC_1024)
2761 dev->max_sectors = min_t(unsigned int, ATA_MAX_SECTORS_1024,
2762 dev->max_sectors);
2763
Shan Haia32450e2013-03-18 10:30:44 +08002764 if (dev->horkage & ATA_HORKAGE_MAX_SEC_LBA48)
2765 dev->max_sectors = ATA_MAX_SECTORS_LBA48;
2766
Tejun Heo4b2f3ed2006-03-01 16:09:36 +09002767 if (ap->ops->dev_config)
Alancd0d3bb2007-03-02 00:56:15 +00002768 ap->ops->dev_config(dev);
Tejun Heo4b2f3ed2006-03-01 16:09:36 +09002769
Alan Coxc5038fc2007-10-25 14:21:16 +01002770 if (dev->horkage & ATA_HORKAGE_DIAGNOSTIC) {
2771 /* Let the user know. We don't want to disallow opens for
2772 rescue purposes, or in case the vendor is just a blithering
2773 idiot. Do this after the dev_config call as some controllers
2774 with buggy firmware may want to avoid reporting false device
2775 bugs */
2776
2777 if (print_info) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07002778 ata_dev_warn(dev,
Alan Coxc5038fc2007-10-25 14:21:16 +01002779"Drive reports diagnostics failure. This may indicate a drive\n");
Joe Perchesa9a79df2011-04-15 15:51:59 -07002780 ata_dev_warn(dev,
Alan Coxc5038fc2007-10-25 14:21:16 +01002781"fault or invalid emulation. Contact drive vendor for information.\n");
2782 }
2783 }
2784
Tejun Heoac70a962008-11-27 13:36:48 +09002785 if ((dev->horkage & ATA_HORKAGE_FIRMWARE_WARN) && print_info) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07002786 ata_dev_warn(dev, "WARNING: device requires firmware update to be fully functional\n");
2787 ata_dev_warn(dev, " contact the vendor or visit http://ata.wiki.kernel.org\n");
Tejun Heoac70a962008-11-27 13:36:48 +09002788 }
2789
Tejun Heoffeae412006-03-01 16:09:35 +09002790 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791
2792err_out_nosup:
Borislav Petkov0dd4b212006-06-23 02:29:08 -04002793 if (ata_msg_probe(ap))
Joe Perchesa9a79df2011-04-15 15:51:59 -07002794 ata_dev_dbg(dev, "%s: EXIT, err\n", __func__);
Tejun Heoffeae412006-03-01 16:09:35 +09002795 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796}
2797
2798/**
Alan Cox2e41e8e2007-03-08 23:19:19 +00002799 * ata_cable_40wire - return 40 wire cable type
Alan Coxbe0d18d2007-03-06 02:37:56 -08002800 * @ap: port
2801 *
Alan Cox2e41e8e2007-03-08 23:19:19 +00002802 * Helper method for drivers which want to hardwire 40 wire cable
Alan Coxbe0d18d2007-03-06 02:37:56 -08002803 * detection.
2804 */
2805
2806int ata_cable_40wire(struct ata_port *ap)
2807{
2808 return ATA_CBL_PATA40;
2809}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01002810EXPORT_SYMBOL_GPL(ata_cable_40wire);
Alan Coxbe0d18d2007-03-06 02:37:56 -08002811
2812/**
Alan Cox2e41e8e2007-03-08 23:19:19 +00002813 * ata_cable_80wire - return 80 wire cable type
Alan Coxbe0d18d2007-03-06 02:37:56 -08002814 * @ap: port
2815 *
Alan Cox2e41e8e2007-03-08 23:19:19 +00002816 * Helper method for drivers which want to hardwire 80 wire cable
Alan Coxbe0d18d2007-03-06 02:37:56 -08002817 * detection.
2818 */
2819
2820int ata_cable_80wire(struct ata_port *ap)
2821{
2822 return ATA_CBL_PATA80;
2823}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01002824EXPORT_SYMBOL_GPL(ata_cable_80wire);
Alan Coxbe0d18d2007-03-06 02:37:56 -08002825
2826/**
2827 * ata_cable_unknown - return unknown PATA cable.
2828 * @ap: port
2829 *
2830 * Helper method for drivers which have no PATA cable detection.
2831 */
2832
2833int ata_cable_unknown(struct ata_port *ap)
2834{
2835 return ATA_CBL_PATA_UNK;
2836}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01002837EXPORT_SYMBOL_GPL(ata_cable_unknown);
Alan Coxbe0d18d2007-03-06 02:37:56 -08002838
2839/**
Tejun Heoc88f90c2007-11-27 19:43:48 +09002840 * ata_cable_ignore - return ignored PATA cable.
2841 * @ap: port
2842 *
2843 * Helper method for drivers which don't use cable type to limit
2844 * transfer mode.
2845 */
2846int ata_cable_ignore(struct ata_port *ap)
2847{
2848 return ATA_CBL_PATA_IGN;
2849}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01002850EXPORT_SYMBOL_GPL(ata_cable_ignore);
Tejun Heoc88f90c2007-11-27 19:43:48 +09002851
2852/**
Alan Coxbe0d18d2007-03-06 02:37:56 -08002853 * ata_cable_sata - return SATA cable type
2854 * @ap: port
2855 *
2856 * Helper method for drivers which have SATA cables
2857 */
2858
2859int ata_cable_sata(struct ata_port *ap)
2860{
2861 return ATA_CBL_SATA;
2862}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01002863EXPORT_SYMBOL_GPL(ata_cable_sata);
Alan Coxbe0d18d2007-03-06 02:37:56 -08002864
2865/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866 * ata_bus_probe - Reset and probe ATA bus
2867 * @ap: Bus to probe
2868 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04002869 * Master ATA bus probing function. Initiates a hardware-dependent
2870 * bus reset, then attempts to identify any devices found on
2871 * the bus.
2872 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002874 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875 *
2876 * RETURNS:
Tejun Heo96072e62006-04-01 01:38:17 +09002877 * Zero on success, negative errno otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878 */
2879
Brian King80289162006-08-07 14:27:31 -05002880int ata_bus_probe(struct ata_port *ap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881{
Tejun Heo28ca5c52006-03-01 16:09:36 +09002882 unsigned int classes[ATA_MAX_DEVICES];
Tejun Heo14d2bac2006-04-02 17:54:46 +09002883 int tries[ATA_MAX_DEVICES];
Tejun Heof58229f2007-08-06 18:36:23 +09002884 int rc;
Tejun Heoe82cbdb2006-04-01 01:38:18 +09002885 struct ata_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886
Tejun Heo1eca4362008-11-03 20:03:17 +09002887 ata_for_each_dev(dev, &ap->link, ALL)
Tejun Heof58229f2007-08-06 18:36:23 +09002888 tries[dev->devno] = ATA_PROBE_MAX_TRIES;
Tejun Heo14d2bac2006-04-02 17:54:46 +09002889
2890 retry:
Tejun Heo1eca4362008-11-03 20:03:17 +09002891 ata_for_each_dev(dev, &ap->link, ALL) {
Tejun Heocdeab112007-10-29 16:41:09 +09002892 /* If we issue an SRST then an ATA drive (not ATAPI)
2893 * may change configuration and be in PIO0 timing. If
2894 * we do a hard reset (or are coming from power on)
2895 * this is true for ATA or ATAPI. Until we've set a
2896 * suitable controller mode we should not touch the
2897 * bus as we may be talking too fast.
2898 */
2899 dev->pio_mode = XFER_PIO_0;
Aaron Lu54169122012-12-03 11:35:02 +08002900 dev->dma_mode = 0xff;
Tejun Heocdeab112007-10-29 16:41:09 +09002901
2902 /* If the controller has a pio mode setup function
2903 * then use it to set the chipset to rights. Don't
2904 * touch the DMA setup as that will be dealt with when
2905 * configuring devices.
2906 */
2907 if (ap->ops->set_piomode)
2908 ap->ops->set_piomode(ap, dev);
2909 }
2910
Tejun Heo20444702006-03-13 01:57:01 +09002911 /* reset and determine device classes */
Tejun Heo52783c52006-05-31 18:28:22 +09002912 ap->ops->phy_reset(ap);
Tejun Heo2061a472006-03-12 00:57:39 +09002913
Tejun Heo1eca4362008-11-03 20:03:17 +09002914 ata_for_each_dev(dev, &ap->link, ALL) {
Tejun Heo3e4ec342010-05-10 21:41:30 +02002915 if (dev->class != ATA_DEV_UNKNOWN)
Tejun Heo52783c52006-05-31 18:28:22 +09002916 classes[dev->devno] = dev->class;
2917 else
2918 classes[dev->devno] = ATA_DEV_NONE;
Tejun Heo20444702006-03-13 01:57:01 +09002919
Tejun Heo52783c52006-05-31 18:28:22 +09002920 dev->class = ATA_DEV_UNKNOWN;
Tejun Heo28ca5c52006-03-01 16:09:36 +09002921 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922
Jeff Garzikf31f0cc2007-03-02 17:47:28 -05002923 /* read IDENTIFY page and configure devices. We have to do the identify
2924 specific sequence bass-ackwards so that PDIAG- is released by
2925 the slave device */
2926
Tejun Heo1eca4362008-11-03 20:03:17 +09002927 ata_for_each_dev(dev, &ap->link, ALL_REVERSE) {
Tejun Heof58229f2007-08-06 18:36:23 +09002928 if (tries[dev->devno])
2929 dev->class = classes[dev->devno];
Tejun Heo14d2bac2006-04-02 17:54:46 +09002930
Tejun Heoe1211e32006-04-01 01:38:18 +09002931 if (!ata_dev_enabled(dev))
Tejun Heoffeae412006-03-01 16:09:35 +09002932 continue;
2933
Tejun Heobff04642006-11-10 18:08:10 +09002934 rc = ata_dev_read_id(dev, &dev->class, ATA_READID_POSTRESET,
2935 dev->id);
Tejun Heo14d2bac2006-04-02 17:54:46 +09002936 if (rc)
2937 goto fail;
Jeff Garzikf31f0cc2007-03-02 17:47:28 -05002938 }
2939
Alan Coxbe0d18d2007-03-06 02:37:56 -08002940 /* Now ask for the cable type as PDIAG- should have been released */
2941 if (ap->ops->cable_detect)
2942 ap->cbl = ap->ops->cable_detect(ap);
2943
Tejun Heo1eca4362008-11-03 20:03:17 +09002944 /* We may have SATA bridge glue hiding here irrespective of
2945 * the reported cable types and sensed types. When SATA
2946 * drives indicate we have a bridge, we don't know which end
2947 * of the link the bridge is which is a problem.
2948 */
2949 ata_for_each_dev(dev, &ap->link, ENABLED)
Alan Cox614fe292007-08-22 23:22:45 +01002950 if (ata_id_is_sata(dev->id))
2951 ap->cbl = ATA_CBL_SATA;
Alan Cox614fe292007-08-22 23:22:45 +01002952
Jeff Garzikf31f0cc2007-03-02 17:47:28 -05002953 /* After the identify sequence we can now set up the devices. We do
2954 this in the normal order so that the user doesn't get confused */
2955
Tejun Heo1eca4362008-11-03 20:03:17 +09002956 ata_for_each_dev(dev, &ap->link, ENABLED) {
Tejun Heo9af5c9c2007-08-06 18:36:22 +09002957 ap->link.eh_context.i.flags |= ATA_EHI_PRINTINFO;
Tejun Heoefdaedc2006-11-01 18:38:52 +09002958 rc = ata_dev_configure(dev);
Tejun Heo9af5c9c2007-08-06 18:36:22 +09002959 ap->link.eh_context.i.flags &= ~ATA_EHI_PRINTINFO;
Tejun Heo14d2bac2006-04-02 17:54:46 +09002960 if (rc)
2961 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962 }
2963
Tejun Heoe82cbdb2006-04-01 01:38:18 +09002964 /* configure transfer mode */
Tejun Heo02607312007-08-06 18:36:23 +09002965 rc = ata_set_mode(&ap->link, &dev);
Tejun Heo4ae72a12007-02-02 16:22:30 +09002966 if (rc)
Tejun Heo51713d32006-04-11 22:26:29 +09002967 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968
Tejun Heo1eca4362008-11-03 20:03:17 +09002969 ata_for_each_dev(dev, &ap->link, ENABLED)
2970 return 0;
Alan Coxe35a9e02006-03-27 18:46:37 +01002971
Tejun Heo96072e62006-04-01 01:38:17 +09002972 return -ENODEV;
Tejun Heo14d2bac2006-04-02 17:54:46 +09002973
2974 fail:
Tejun Heo4ae72a12007-02-02 16:22:30 +09002975 tries[dev->devno]--;
2976
Tejun Heo14d2bac2006-04-02 17:54:46 +09002977 switch (rc) {
2978 case -EINVAL:
Tejun Heo4ae72a12007-02-02 16:22:30 +09002979 /* eeek, something went very wrong, give up */
Tejun Heo14d2bac2006-04-02 17:54:46 +09002980 tries[dev->devno] = 0;
2981 break;
Tejun Heo4ae72a12007-02-02 16:22:30 +09002982
2983 case -ENODEV:
2984 /* give it just one more chance */
2985 tries[dev->devno] = min(tries[dev->devno], 1);
Gustavo A. R. Silva05b83602017-10-12 14:19:16 -05002986 /* fall through */
Tejun Heo14d2bac2006-04-02 17:54:46 +09002987 case -EIO:
Tejun Heo4ae72a12007-02-02 16:22:30 +09002988 if (tries[dev->devno] == 1) {
2989 /* This is the last chance, better to slow
2990 * down than lose it.
2991 */
Tejun Heoa07d4992009-01-29 20:31:33 +09002992 sata_down_spd_limit(&ap->link, 0);
Tejun Heo4ae72a12007-02-02 16:22:30 +09002993 ata_down_xfermask_limit(dev, ATA_DNXFER_PIO);
2994 }
Tejun Heo14d2bac2006-04-02 17:54:46 +09002995 }
2996
Tejun Heo4ae72a12007-02-02 16:22:30 +09002997 if (!tries[dev->devno])
Tejun Heo3373efd2006-05-15 20:57:53 +09002998 ata_dev_disable(dev);
Tejun Heoec573752006-04-11 22:26:29 +09002999
Tejun Heo14d2bac2006-04-02 17:54:46 +09003000 goto retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001}
3002
3003/**
Tejun Heo3be680b2005-12-19 22:35:02 +09003004 * sata_print_link_status - Print SATA link status
Tejun Heo936fd732007-08-06 18:36:23 +09003005 * @link: SATA link to printk link status about
Tejun Heo3be680b2005-12-19 22:35:02 +09003006 *
3007 * This function prints link speed and status of a SATA link.
3008 *
3009 * LOCKING:
3010 * None.
3011 */
Adrian Bunk6bdb4fc2008-04-21 11:51:11 +03003012static void sata_print_link_status(struct ata_link *link)
Tejun Heo3be680b2005-12-19 22:35:02 +09003013{
Tejun Heo6d5f9732006-04-03 00:09:41 +09003014 u32 sstatus, scontrol, tmp;
Tejun Heo3be680b2005-12-19 22:35:02 +09003015
Tejun Heo936fd732007-08-06 18:36:23 +09003016 if (sata_scr_read(link, SCR_STATUS, &sstatus))
Tejun Heo3be680b2005-12-19 22:35:02 +09003017 return;
Tejun Heo936fd732007-08-06 18:36:23 +09003018 sata_scr_read(link, SCR_CONTROL, &scontrol);
Tejun Heo3be680b2005-12-19 22:35:02 +09003019
Tejun Heob1c72912008-07-31 17:02:43 +09003020 if (ata_phys_link_online(link)) {
Tejun Heo3be680b2005-12-19 22:35:02 +09003021 tmp = (sstatus >> 4) & 0xf;
Joe Perchesa9a79df2011-04-15 15:51:59 -07003022 ata_link_info(link, "SATA link up %s (SStatus %X SControl %X)\n",
3023 sata_spd_string(tmp), sstatus, scontrol);
Tejun Heo3be680b2005-12-19 22:35:02 +09003024 } else {
Joe Perchesa9a79df2011-04-15 15:51:59 -07003025 ata_link_info(link, "SATA link down (SStatus %X SControl %X)\n",
3026 sstatus, scontrol);
Tejun Heo3be680b2005-12-19 22:35:02 +09003027 }
3028}
3029
3030/**
Alan Coxebdfca6e2006-03-23 15:38:34 +00003031 * ata_dev_pair - return other device on cable
Alan Coxebdfca6e2006-03-23 15:38:34 +00003032 * @adev: device
3033 *
3034 * Obtain the other device on the same cable, or if none is
3035 * present NULL is returned
3036 */
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05003037
Tejun Heo3373efd2006-05-15 20:57:53 +09003038struct ata_device *ata_dev_pair(struct ata_device *adev)
Alan Coxebdfca6e2006-03-23 15:38:34 +00003039{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09003040 struct ata_link *link = adev->link;
3041 struct ata_device *pair = &link->device[1 - adev->devno];
Tejun Heoe1211e32006-04-01 01:38:18 +09003042 if (!ata_dev_enabled(pair))
Alan Coxebdfca6e2006-03-23 15:38:34 +00003043 return NULL;
3044 return pair;
3045}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01003046EXPORT_SYMBOL_GPL(ata_dev_pair);
Alan Coxebdfca6e2006-03-23 15:38:34 +00003047
3048/**
Tejun Heo3c567b72006-05-15 20:57:23 +09003049 * sata_down_spd_limit - adjust SATA spd limit downward
Tejun Heo936fd732007-08-06 18:36:23 +09003050 * @link: Link to adjust SATA spd limit for
Tejun Heoa07d4992009-01-29 20:31:33 +09003051 * @spd_limit: Additional limit
Tejun Heo1c3fae42006-04-02 20:53:28 +09003052 *
Tejun Heo936fd732007-08-06 18:36:23 +09003053 * Adjust SATA spd limit of @link downward. Note that this
Tejun Heo1c3fae42006-04-02 20:53:28 +09003054 * function only adjusts the limit. The change must be applied
Tejun Heo3c567b72006-05-15 20:57:23 +09003055 * using sata_set_spd().
Tejun Heo1c3fae42006-04-02 20:53:28 +09003056 *
Tejun Heoa07d4992009-01-29 20:31:33 +09003057 * If @spd_limit is non-zero, the speed is limited to equal to or
3058 * lower than @spd_limit if such speed is supported. If
3059 * @spd_limit is slower than any supported speed, only the lowest
3060 * supported speed is allowed.
3061 *
Tejun Heo1c3fae42006-04-02 20:53:28 +09003062 * LOCKING:
3063 * Inherited from caller.
3064 *
3065 * RETURNS:
3066 * 0 on success, negative errno on failure
3067 */
Tejun Heoa07d4992009-01-29 20:31:33 +09003068int sata_down_spd_limit(struct ata_link *link, u32 spd_limit)
Tejun Heo1c3fae42006-04-02 20:53:28 +09003069{
Tejun Heo81952c52006-05-15 20:57:47 +09003070 u32 sstatus, spd, mask;
Tejun Heoa07d4992009-01-29 20:31:33 +09003071 int rc, bit;
Tejun Heo1c3fae42006-04-02 20:53:28 +09003072
Tejun Heo936fd732007-08-06 18:36:23 +09003073 if (!sata_scr_valid(link))
Tejun Heo008a7892007-07-16 14:29:40 +09003074 return -EOPNOTSUPP;
3075
3076 /* If SCR can be read, use it to determine the current SPD.
Tejun Heo936fd732007-08-06 18:36:23 +09003077 * If not, use cached value in link->sata_spd.
Tejun Heo008a7892007-07-16 14:29:40 +09003078 */
Tejun Heo936fd732007-08-06 18:36:23 +09003079 rc = sata_scr_read(link, SCR_STATUS, &sstatus);
Tejun Heo9913ff8a2009-01-29 20:31:31 +09003080 if (rc == 0 && ata_sstatus_online(sstatus))
Tejun Heo008a7892007-07-16 14:29:40 +09003081 spd = (sstatus >> 4) & 0xf;
3082 else
Tejun Heo936fd732007-08-06 18:36:23 +09003083 spd = link->sata_spd;
Tejun Heo1c3fae42006-04-02 20:53:28 +09003084
Tejun Heo936fd732007-08-06 18:36:23 +09003085 mask = link->sata_spd_limit;
Tejun Heo1c3fae42006-04-02 20:53:28 +09003086 if (mask <= 1)
3087 return -EINVAL;
Tejun Heo008a7892007-07-16 14:29:40 +09003088
3089 /* unconditionally mask off the highest bit */
Tejun Heoa07d4992009-01-29 20:31:33 +09003090 bit = fls(mask) - 1;
3091 mask &= ~(1 << bit);
Tejun Heo1c3fae42006-04-02 20:53:28 +09003092
David Milburn2dc0b462017-11-14 16:17:25 -06003093 /*
3094 * Mask off all speeds higher than or equal to the current one. At
3095 * this point, if current SPD is not available and we previously
3096 * recorded the link speed from SStatus, the driver has already
3097 * masked off the highest bit so mask should already be 1 or 0.
3098 * Otherwise, we should not force 1.5Gbps on a link where we have
3099 * not previously recorded speed from SStatus. Just return in this
3100 * case.
Tejun Heo008a7892007-07-16 14:29:40 +09003101 */
3102 if (spd > 1)
3103 mask &= (1 << (spd - 1)) - 1;
3104 else
David Milburn2dc0b462017-11-14 16:17:25 -06003105 return -EINVAL;
Tejun Heo008a7892007-07-16 14:29:40 +09003106
3107 /* were we already at the bottom? */
Tejun Heo1c3fae42006-04-02 20:53:28 +09003108 if (!mask)
3109 return -EINVAL;
3110
Tejun Heoa07d4992009-01-29 20:31:33 +09003111 if (spd_limit) {
3112 if (mask & ((1 << spd_limit) - 1))
3113 mask &= (1 << spd_limit) - 1;
3114 else {
3115 bit = ffs(mask) - 1;
3116 mask = 1 << bit;
3117 }
3118 }
3119
Tejun Heo936fd732007-08-06 18:36:23 +09003120 link->sata_spd_limit = mask;
Tejun Heo1c3fae42006-04-02 20:53:28 +09003121
Joe Perchesa9a79df2011-04-15 15:51:59 -07003122 ata_link_warn(link, "limiting SATA link speed to %s\n",
3123 sata_spd_string(fls(mask)));
Tejun Heo1c3fae42006-04-02 20:53:28 +09003124
3125 return 0;
3126}
3127
Tejun Heo936fd732007-08-06 18:36:23 +09003128static int __sata_set_spd_needed(struct ata_link *link, u32 *scontrol)
Tejun Heo1c3fae42006-04-02 20:53:28 +09003129{
Tejun Heo52702222007-10-31 10:17:07 +09003130 struct ata_link *host_link = &link->ap->link;
3131 u32 limit, target, spd;
Tejun Heo1c3fae42006-04-02 20:53:28 +09003132
Tejun Heo52702222007-10-31 10:17:07 +09003133 limit = link->sata_spd_limit;
3134
3135 /* Don't configure downstream link faster than upstream link.
3136 * It doesn't speed up anything and some PMPs choke on such
3137 * configuration.
3138 */
3139 if (!ata_is_host_link(link) && host_link->sata_spd)
3140 limit &= (1 << host_link->sata_spd) - 1;
3141
3142 if (limit == UINT_MAX)
3143 target = 0;
Tejun Heo1c3fae42006-04-02 20:53:28 +09003144 else
Tejun Heo52702222007-10-31 10:17:07 +09003145 target = fls(limit);
Tejun Heo1c3fae42006-04-02 20:53:28 +09003146
3147 spd = (*scontrol >> 4) & 0xf;
Tejun Heo52702222007-10-31 10:17:07 +09003148 *scontrol = (*scontrol & ~0xf0) | ((target & 0xf) << 4);
Tejun Heo1c3fae42006-04-02 20:53:28 +09003149
Tejun Heo52702222007-10-31 10:17:07 +09003150 return spd != target;
Tejun Heo1c3fae42006-04-02 20:53:28 +09003151}
3152
3153/**
Tejun Heo3c567b72006-05-15 20:57:23 +09003154 * sata_set_spd_needed - is SATA spd configuration needed
Tejun Heo936fd732007-08-06 18:36:23 +09003155 * @link: Link in question
Tejun Heo1c3fae42006-04-02 20:53:28 +09003156 *
3157 * Test whether the spd limit in SControl matches
Tejun Heo936fd732007-08-06 18:36:23 +09003158 * @link->sata_spd_limit. This function is used to determine
Tejun Heo1c3fae42006-04-02 20:53:28 +09003159 * whether hardreset is necessary to apply SATA spd
3160 * configuration.
3161 *
3162 * LOCKING:
3163 * Inherited from caller.
3164 *
3165 * RETURNS:
3166 * 1 if SATA spd configuration is needed, 0 otherwise.
3167 */
Adrian Bunk1dc55e82008-04-21 11:51:17 +03003168static int sata_set_spd_needed(struct ata_link *link)
Tejun Heo1c3fae42006-04-02 20:53:28 +09003169{
3170 u32 scontrol;
3171
Tejun Heo936fd732007-08-06 18:36:23 +09003172 if (sata_scr_read(link, SCR_CONTROL, &scontrol))
Tejun Heodb64bcf2007-10-31 10:17:06 +09003173 return 1;
Tejun Heo1c3fae42006-04-02 20:53:28 +09003174
Tejun Heo936fd732007-08-06 18:36:23 +09003175 return __sata_set_spd_needed(link, &scontrol);
Tejun Heo1c3fae42006-04-02 20:53:28 +09003176}
3177
3178/**
Tejun Heo3c567b72006-05-15 20:57:23 +09003179 * sata_set_spd - set SATA spd according to spd limit
Tejun Heo936fd732007-08-06 18:36:23 +09003180 * @link: Link to set SATA spd for
Tejun Heo1c3fae42006-04-02 20:53:28 +09003181 *
Tejun Heo936fd732007-08-06 18:36:23 +09003182 * Set SATA spd of @link according to sata_spd_limit.
Tejun Heo1c3fae42006-04-02 20:53:28 +09003183 *
3184 * LOCKING:
3185 * Inherited from caller.
3186 *
3187 * RETURNS:
3188 * 0 if spd doesn't need to be changed, 1 if spd has been
Tejun Heo81952c52006-05-15 20:57:47 +09003189 * changed. Negative errno if SCR registers are inaccessible.
Tejun Heo1c3fae42006-04-02 20:53:28 +09003190 */
Tejun Heo936fd732007-08-06 18:36:23 +09003191int sata_set_spd(struct ata_link *link)
Tejun Heo1c3fae42006-04-02 20:53:28 +09003192{
3193 u32 scontrol;
Tejun Heo81952c52006-05-15 20:57:47 +09003194 int rc;
Tejun Heo1c3fae42006-04-02 20:53:28 +09003195
Tejun Heo936fd732007-08-06 18:36:23 +09003196 if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
Tejun Heo81952c52006-05-15 20:57:47 +09003197 return rc;
Tejun Heo1c3fae42006-04-02 20:53:28 +09003198
Tejun Heo936fd732007-08-06 18:36:23 +09003199 if (!__sata_set_spd_needed(link, &scontrol))
Tejun Heo1c3fae42006-04-02 20:53:28 +09003200 return 0;
3201
Tejun Heo936fd732007-08-06 18:36:23 +09003202 if ((rc = sata_scr_write(link, SCR_CONTROL, scontrol)))
Tejun Heo81952c52006-05-15 20:57:47 +09003203 return rc;
3204
Tejun Heo1c3fae42006-04-02 20:53:28 +09003205 return 1;
3206}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01003207EXPORT_SYMBOL_GPL(sata_set_spd);
Tejun Heo1c3fae42006-04-02 20:53:28 +09003208
Alan Cox452503f2005-10-21 19:01:32 -04003209/*
3210 * This mode timing computation functionality is ported over from
3211 * drivers/ide/ide-timing.h and was originally written by Vojtech Pavlik
3212 */
3213/*
Alan Coxb352e572006-08-10 18:52:12 +01003214 * PIO 0-4, MWDMA 0-2 and UDMA 0-6 timings (in nanoseconds).
Alan Cox452503f2005-10-21 19:01:32 -04003215 * These were taken from ATA/ATAPI-6 standard, rev 0a, except
Alan Coxb352e572006-08-10 18:52:12 +01003216 * for UDMA6, which is currently supported only by Maxtor drives.
3217 *
3218 * For PIO 5/6 MWDMA 3/4 see the CFA specification 3.0.
Alan Cox452503f2005-10-21 19:01:32 -04003219 */
3220
3221static const struct ata_timing ata_timing[] = {
David Daney3ada9c12009-01-15 17:45:31 -08003222/* { XFER_PIO_SLOW, 120, 290, 240, 960, 290, 240, 0, 960, 0 }, */
3223 { XFER_PIO_0, 70, 290, 240, 600, 165, 150, 0, 600, 0 },
3224 { XFER_PIO_1, 50, 290, 93, 383, 125, 100, 0, 383, 0 },
3225 { XFER_PIO_2, 30, 290, 40, 330, 100, 90, 0, 240, 0 },
3226 { XFER_PIO_3, 30, 80, 70, 180, 80, 70, 0, 180, 0 },
3227 { XFER_PIO_4, 25, 70, 25, 120, 70, 25, 0, 120, 0 },
3228 { XFER_PIO_5, 15, 65, 25, 100, 65, 25, 0, 100, 0 },
3229 { XFER_PIO_6, 10, 55, 20, 80, 55, 20, 0, 80, 0 },
Alan Cox452503f2005-10-21 19:01:32 -04003230
David Daney3ada9c12009-01-15 17:45:31 -08003231 { XFER_SW_DMA_0, 120, 0, 0, 0, 480, 480, 50, 960, 0 },
3232 { XFER_SW_DMA_1, 90, 0, 0, 0, 240, 240, 30, 480, 0 },
3233 { XFER_SW_DMA_2, 60, 0, 0, 0, 120, 120, 20, 240, 0 },
Alan Cox452503f2005-10-21 19:01:32 -04003234
David Daney3ada9c12009-01-15 17:45:31 -08003235 { XFER_MW_DMA_0, 60, 0, 0, 0, 215, 215, 20, 480, 0 },
3236 { XFER_MW_DMA_1, 45, 0, 0, 0, 80, 50, 5, 150, 0 },
3237 { XFER_MW_DMA_2, 25, 0, 0, 0, 70, 25, 5, 120, 0 },
3238 { XFER_MW_DMA_3, 25, 0, 0, 0, 65, 25, 5, 100, 0 },
3239 { XFER_MW_DMA_4, 25, 0, 0, 0, 55, 20, 5, 80, 0 },
Alan Cox452503f2005-10-21 19:01:32 -04003240
David Daney3ada9c12009-01-15 17:45:31 -08003241/* { XFER_UDMA_SLOW, 0, 0, 0, 0, 0, 0, 0, 0, 150 }, */
3242 { XFER_UDMA_0, 0, 0, 0, 0, 0, 0, 0, 0, 120 },
3243 { XFER_UDMA_1, 0, 0, 0, 0, 0, 0, 0, 0, 80 },
3244 { XFER_UDMA_2, 0, 0, 0, 0, 0, 0, 0, 0, 60 },
3245 { XFER_UDMA_3, 0, 0, 0, 0, 0, 0, 0, 0, 45 },
3246 { XFER_UDMA_4, 0, 0, 0, 0, 0, 0, 0, 0, 30 },
3247 { XFER_UDMA_5, 0, 0, 0, 0, 0, 0, 0, 0, 20 },
3248 { XFER_UDMA_6, 0, 0, 0, 0, 0, 0, 0, 0, 15 },
Alan Cox452503f2005-10-21 19:01:32 -04003249
3250 { 0xFF }
3251};
3252
Jeff Garzik2dcb4072007-10-19 06:42:56 -04003253#define ENOUGH(v, unit) (((v)-1)/(unit)+1)
Arnd Bergmann23e4c672017-09-06 23:45:34 +02003254#define EZ(v, unit) ((v)?ENOUGH(((v) * 1000), unit):0)
Alan Cox452503f2005-10-21 19:01:32 -04003255
3256static void ata_timing_quantize(const struct ata_timing *t, struct ata_timing *q, int T, int UT)
3257{
Arnd Bergmann23e4c672017-09-06 23:45:34 +02003258 q->setup = EZ(t->setup, T);
3259 q->act8b = EZ(t->act8b, T);
3260 q->rec8b = EZ(t->rec8b, T);
3261 q->cyc8b = EZ(t->cyc8b, T);
3262 q->active = EZ(t->active, T);
3263 q->recover = EZ(t->recover, T);
3264 q->dmack_hold = EZ(t->dmack_hold, T);
3265 q->cycle = EZ(t->cycle, T);
3266 q->udma = EZ(t->udma, UT);
Alan Cox452503f2005-10-21 19:01:32 -04003267}
3268
3269void ata_timing_merge(const struct ata_timing *a, const struct ata_timing *b,
3270 struct ata_timing *m, unsigned int what)
3271{
3272 if (what & ATA_TIMING_SETUP ) m->setup = max(a->setup, b->setup);
3273 if (what & ATA_TIMING_ACT8B ) m->act8b = max(a->act8b, b->act8b);
3274 if (what & ATA_TIMING_REC8B ) m->rec8b = max(a->rec8b, b->rec8b);
3275 if (what & ATA_TIMING_CYC8B ) m->cyc8b = max(a->cyc8b, b->cyc8b);
3276 if (what & ATA_TIMING_ACTIVE ) m->active = max(a->active, b->active);
3277 if (what & ATA_TIMING_RECOVER) m->recover = max(a->recover, b->recover);
David Daney3ada9c12009-01-15 17:45:31 -08003278 if (what & ATA_TIMING_DMACK_HOLD) m->dmack_hold = max(a->dmack_hold, b->dmack_hold);
Alan Cox452503f2005-10-21 19:01:32 -04003279 if (what & ATA_TIMING_CYCLE ) m->cycle = max(a->cycle, b->cycle);
3280 if (what & ATA_TIMING_UDMA ) m->udma = max(a->udma, b->udma);
3281}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01003282EXPORT_SYMBOL_GPL(ata_timing_merge);
Alan Cox452503f2005-10-21 19:01:32 -04003283
Tejun Heo63573572007-11-27 19:43:39 +09003284const struct ata_timing *ata_timing_find_mode(u8 xfer_mode)
Alan Cox452503f2005-10-21 19:01:32 -04003285{
Tejun Heo70cd0712007-11-27 19:43:40 +09003286 const struct ata_timing *t = ata_timing;
Alan Cox452503f2005-10-21 19:01:32 -04003287
Tejun Heo70cd0712007-11-27 19:43:40 +09003288 while (xfer_mode > t->mode)
3289 t++;
3290
3291 if (xfer_mode == t->mode)
3292 return t;
Borislav Petkovcd705d52012-10-21 18:57:56 +02003293
3294 WARN_ONCE(true, "%s: unable to find timing for xfer_mode 0x%x\n",
3295 __func__, xfer_mode);
3296
Tejun Heo70cd0712007-11-27 19:43:40 +09003297 return NULL;
Alan Cox452503f2005-10-21 19:01:32 -04003298}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01003299EXPORT_SYMBOL_GPL(ata_timing_find_mode);
Alan Cox452503f2005-10-21 19:01:32 -04003300
3301int ata_timing_compute(struct ata_device *adev, unsigned short speed,
3302 struct ata_timing *t, int T, int UT)
3303{
Bartlomiej Zolnierkiewicz9e8808a2010-01-18 18:13:57 +01003304 const u16 *id = adev->id;
Alan Cox452503f2005-10-21 19:01:32 -04003305 const struct ata_timing *s;
3306 struct ata_timing p;
3307
3308 /*
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05003309 * Find the mode.
Albert Lee75b1f2f2005-11-16 17:06:18 +08003310 */
Alan Cox452503f2005-10-21 19:01:32 -04003311
3312 if (!(s = ata_timing_find_mode(speed)))
3313 return -EINVAL;
3314
Albert Lee75b1f2f2005-11-16 17:06:18 +08003315 memcpy(t, s, sizeof(*s));
3316
Alan Cox452503f2005-10-21 19:01:32 -04003317 /*
3318 * If the drive is an EIDE drive, it can tell us it needs extended
3319 * PIO/MW_DMA cycle timing.
3320 */
3321
Bartlomiej Zolnierkiewicz9e8808a2010-01-18 18:13:57 +01003322 if (id[ATA_ID_FIELD_VALID] & 2) { /* EIDE drive */
Alan Cox452503f2005-10-21 19:01:32 -04003323 memset(&p, 0, sizeof(p));
Bartlomiej Zolnierkiewicz9e8808a2010-01-18 18:13:57 +01003324
Bartlomiej Zolnierkiewiczbff00252011-10-13 15:17:32 +02003325 if (speed >= XFER_PIO_0 && speed < XFER_SW_DMA_0) {
Bartlomiej Zolnierkiewicz9e8808a2010-01-18 18:13:57 +01003326 if (speed <= XFER_PIO_2)
3327 p.cycle = p.cyc8b = id[ATA_ID_EIDE_PIO];
3328 else if ((speed <= XFER_PIO_4) ||
3329 (speed == XFER_PIO_5 && !ata_id_is_cfa(id)))
3330 p.cycle = p.cyc8b = id[ATA_ID_EIDE_PIO_IORDY];
3331 } else if (speed >= XFER_MW_DMA_0 && speed <= XFER_MW_DMA_2)
3332 p.cycle = id[ATA_ID_EIDE_DMA_MIN];
3333
Alan Cox452503f2005-10-21 19:01:32 -04003334 ata_timing_merge(&p, t, t, ATA_TIMING_CYCLE | ATA_TIMING_CYC8B);
3335 }
3336
3337 /*
3338 * Convert the timing to bus clock counts.
3339 */
3340
Albert Lee75b1f2f2005-11-16 17:06:18 +08003341 ata_timing_quantize(t, t, T, UT);
Alan Cox452503f2005-10-21 19:01:32 -04003342
3343 /*
Randy Dunlapc893a3a2006-01-28 13:15:32 -05003344 * Even in DMA/UDMA modes we still use PIO access for IDENTIFY,
3345 * S.M.A.R.T * and some other commands. We have to ensure that the
3346 * DMA cycle timing is slower/equal than the fastest PIO timing.
Alan Cox452503f2005-10-21 19:01:32 -04003347 */
3348
Alanfd3367af2006-12-07 12:41:18 +00003349 if (speed > XFER_PIO_6) {
Alan Cox452503f2005-10-21 19:01:32 -04003350 ata_timing_compute(adev, adev->pio_mode, &p, T, UT);
3351 ata_timing_merge(&p, t, t, ATA_TIMING_ALL);
3352 }
3353
3354 /*
Randy Dunlapc893a3a2006-01-28 13:15:32 -05003355 * Lengthen active & recovery time so that cycle time is correct.
Alan Cox452503f2005-10-21 19:01:32 -04003356 */
3357
3358 if (t->act8b + t->rec8b < t->cyc8b) {
3359 t->act8b += (t->cyc8b - (t->act8b + t->rec8b)) / 2;
3360 t->rec8b = t->cyc8b - t->act8b;
3361 }
3362
3363 if (t->active + t->recover < t->cycle) {
3364 t->active += (t->cycle - (t->active + t->recover)) / 2;
3365 t->recover = t->cycle - t->active;
3366 }
Jeff Garzika617c092007-05-21 20:14:23 -04003367
Alan Cox4f701d12007-04-23 11:55:36 +01003368 /* In a few cases quantisation may produce enough errors to
3369 leave t->cycle too low for the sum of active and recovery
3370 if so we must correct this */
3371 if (t->active + t->recover > t->cycle)
3372 t->cycle = t->active + t->recover;
Alan Cox452503f2005-10-21 19:01:32 -04003373
3374 return 0;
3375}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01003376EXPORT_SYMBOL_GPL(ata_timing_compute);
Alan Cox452503f2005-10-21 19:01:32 -04003377
Tejun Heocf176e12006-04-02 17:54:46 +09003378/**
Tejun Heoa0f79b92007-12-18 16:33:05 +09003379 * ata_timing_cycle2mode - find xfer mode for the specified cycle duration
3380 * @xfer_shift: ATA_SHIFT_* value for transfer type to examine.
3381 * @cycle: cycle duration in ns
3382 *
3383 * Return matching xfer mode for @cycle. The returned mode is of
3384 * the transfer type specified by @xfer_shift. If @cycle is too
3385 * slow for @xfer_shift, 0xff is returned. If @cycle is faster
3386 * than the fastest known mode, the fasted mode is returned.
3387 *
3388 * LOCKING:
3389 * None.
3390 *
3391 * RETURNS:
3392 * Matching xfer_mode, 0xff if no match found.
3393 */
3394u8 ata_timing_cycle2mode(unsigned int xfer_shift, int cycle)
3395{
3396 u8 base_mode = 0xff, last_mode = 0xff;
3397 const struct ata_xfer_ent *ent;
3398 const struct ata_timing *t;
3399
3400 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
3401 if (ent->shift == xfer_shift)
3402 base_mode = ent->base;
3403
3404 for (t = ata_timing_find_mode(base_mode);
3405 t && ata_xfer_mode2shift(t->mode) == xfer_shift; t++) {
3406 unsigned short this_cycle;
3407
3408 switch (xfer_shift) {
3409 case ATA_SHIFT_PIO:
3410 case ATA_SHIFT_MWDMA:
3411 this_cycle = t->cycle;
3412 break;
3413 case ATA_SHIFT_UDMA:
3414 this_cycle = t->udma;
3415 break;
3416 default:
3417 return 0xff;
3418 }
3419
3420 if (cycle > this_cycle)
3421 break;
3422
3423 last_mode = t->mode;
3424 }
3425
3426 return last_mode;
3427}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01003428EXPORT_SYMBOL_GPL(ata_timing_cycle2mode);
Tejun Heoa0f79b92007-12-18 16:33:05 +09003429
3430/**
Tejun Heocf176e12006-04-02 17:54:46 +09003431 * ata_down_xfermask_limit - adjust dev xfer masks downward
Tejun Heocf176e12006-04-02 17:54:46 +09003432 * @dev: Device to adjust xfer masks
Tejun Heo458337d2007-02-02 16:22:30 +09003433 * @sel: ATA_DNXFER_* selector
Tejun Heocf176e12006-04-02 17:54:46 +09003434 *
3435 * Adjust xfer masks of @dev downward. Note that this function
3436 * does not apply the change. Invoking ata_set_mode() afterwards
3437 * will apply the limit.
3438 *
3439 * LOCKING:
3440 * Inherited from caller.
3441 *
3442 * RETURNS:
3443 * 0 on success, negative errno on failure
3444 */
Tejun Heo458337d2007-02-02 16:22:30 +09003445int ata_down_xfermask_limit(struct ata_device *dev, unsigned int sel)
Tejun Heocf176e12006-04-02 17:54:46 +09003446{
Tejun Heo458337d2007-02-02 16:22:30 +09003447 char buf[32];
Tejun Heo7dc951a2007-11-27 19:43:42 +09003448 unsigned long orig_mask, xfer_mask;
3449 unsigned long pio_mask, mwdma_mask, udma_mask;
Tejun Heo458337d2007-02-02 16:22:30 +09003450 int quiet, highbit;
Tejun Heocf176e12006-04-02 17:54:46 +09003451
Tejun Heo458337d2007-02-02 16:22:30 +09003452 quiet = !!(sel & ATA_DNXFER_QUIET);
3453 sel &= ~ATA_DNXFER_QUIET;
Tejun Heocf176e12006-04-02 17:54:46 +09003454
Tejun Heo458337d2007-02-02 16:22:30 +09003455 xfer_mask = orig_mask = ata_pack_xfermask(dev->pio_mask,
3456 dev->mwdma_mask,
3457 dev->udma_mask);
3458 ata_unpack_xfermask(xfer_mask, &pio_mask, &mwdma_mask, &udma_mask);
Tejun Heocf176e12006-04-02 17:54:46 +09003459
Tejun Heo458337d2007-02-02 16:22:30 +09003460 switch (sel) {
3461 case ATA_DNXFER_PIO:
3462 highbit = fls(pio_mask) - 1;
3463 pio_mask &= ~(1 << highbit);
3464 break;
3465
3466 case ATA_DNXFER_DMA:
3467 if (udma_mask) {
3468 highbit = fls(udma_mask) - 1;
3469 udma_mask &= ~(1 << highbit);
3470 if (!udma_mask)
3471 return -ENOENT;
3472 } else if (mwdma_mask) {
3473 highbit = fls(mwdma_mask) - 1;
3474 mwdma_mask &= ~(1 << highbit);
3475 if (!mwdma_mask)
3476 return -ENOENT;
3477 }
3478 break;
3479
3480 case ATA_DNXFER_40C:
3481 udma_mask &= ATA_UDMA_MASK_40C;
3482 break;
3483
3484 case ATA_DNXFER_FORCE_PIO0:
3485 pio_mask &= 1;
Gustavo A. R. Silva05b83602017-10-12 14:19:16 -05003486 /* fall through */
Tejun Heo458337d2007-02-02 16:22:30 +09003487 case ATA_DNXFER_FORCE_PIO:
3488 mwdma_mask = 0;
3489 udma_mask = 0;
3490 break;
3491
Tejun Heo458337d2007-02-02 16:22:30 +09003492 default:
3493 BUG();
3494 }
3495
3496 xfer_mask &= ata_pack_xfermask(pio_mask, mwdma_mask, udma_mask);
3497
3498 if (!(xfer_mask & ATA_MASK_PIO) || xfer_mask == orig_mask)
3499 return -ENOENT;
3500
3501 if (!quiet) {
3502 if (xfer_mask & (ATA_MASK_MWDMA | ATA_MASK_UDMA))
3503 snprintf(buf, sizeof(buf), "%s:%s",
3504 ata_mode_string(xfer_mask),
3505 ata_mode_string(xfer_mask & ATA_MASK_PIO));
3506 else
3507 snprintf(buf, sizeof(buf), "%s",
3508 ata_mode_string(xfer_mask));
3509
Joe Perchesa9a79df2011-04-15 15:51:59 -07003510 ata_dev_warn(dev, "limiting speed to %s\n", buf);
Tejun Heo458337d2007-02-02 16:22:30 +09003511 }
Tejun Heocf176e12006-04-02 17:54:46 +09003512
3513 ata_unpack_xfermask(xfer_mask, &dev->pio_mask, &dev->mwdma_mask,
3514 &dev->udma_mask);
3515
Tejun Heocf176e12006-04-02 17:54:46 +09003516 return 0;
Tejun Heocf176e12006-04-02 17:54:46 +09003517}
3518
Tejun Heo3373efd2006-05-15 20:57:53 +09003519static int ata_dev_set_mode(struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003520{
Tejun Heod0cb43b2009-07-09 09:27:50 +09003521 struct ata_port *ap = dev->link->ap;
Tejun Heo9af5c9c2007-08-06 18:36:22 +09003522 struct ata_eh_context *ehc = &dev->link->eh_context;
Tejun Heod0cb43b2009-07-09 09:27:50 +09003523 const bool nosetxfer = dev->horkage & ATA_HORKAGE_NOSETXFER;
Tejun Heo4055dee2008-02-07 10:34:08 +09003524 const char *dev_err_whine = "";
3525 int ign_dev_err = 0;
Tejun Heod0cb43b2009-07-09 09:27:50 +09003526 unsigned int err_mask = 0;
Tejun Heo83206a22006-03-24 15:25:31 +09003527 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003528
Tejun Heoe8384602006-04-02 18:51:53 +09003529 dev->flags &= ~ATA_DFLAG_PIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003530 if (dev->xfer_shift == ATA_SHIFT_PIO)
3531 dev->flags |= ATA_DFLAG_PIO;
3532
Tejun Heod0cb43b2009-07-09 09:27:50 +09003533 if (nosetxfer && ap->flags & ATA_FLAG_SATA && ata_id_is_sata(dev->id))
3534 dev_err_whine = " (SET_XFERMODE skipped)";
3535 else {
3536 if (nosetxfer)
Joe Perchesa9a79df2011-04-15 15:51:59 -07003537 ata_dev_warn(dev,
3538 "NOSETXFER but PATA detected - can't "
3539 "skip SETXFER, might malfunction\n");
Tejun Heod0cb43b2009-07-09 09:27:50 +09003540 err_mask = ata_dev_set_xfermode(dev);
3541 }
Jeff Garzik2dcb4072007-10-19 06:42:56 -04003542
Tejun Heo4055dee2008-02-07 10:34:08 +09003543 if (err_mask & ~AC_ERR_DEV)
3544 goto fail;
Jeff Garzik2dcb4072007-10-19 06:42:56 -04003545
Tejun Heo4055dee2008-02-07 10:34:08 +09003546 /* revalidate */
Tejun Heobaa1e782006-11-01 18:39:27 +09003547 ehc->i.flags |= ATA_EHI_POST_SETMODE;
Tejun Heo422c9da2007-09-23 13:14:12 +09003548 rc = ata_dev_revalidate(dev, ATA_DEV_UNKNOWN, 0);
Tejun Heobaa1e782006-11-01 18:39:27 +09003549 ehc->i.flags &= ~ATA_EHI_POST_SETMODE;
Tejun Heo5eb45c02006-04-02 18:51:52 +09003550 if (rc)
Tejun Heo83206a22006-03-24 15:25:31 +09003551 return rc;
Tejun Heo48a8a142006-03-05 17:55:58 +09003552
Alan Coxb93fda12008-04-08 16:36:44 +01003553 if (dev->xfer_shift == ATA_SHIFT_PIO) {
3554 /* Old CFA may refuse this command, which is just fine */
3555 if (ata_id_is_cfa(dev->id))
3556 ign_dev_err = 1;
3557 /* Catch several broken garbage emulations plus some pre
3558 ATA devices */
3559 if (ata_id_major_version(dev->id) == 0 &&
3560 dev->pio_mode <= XFER_PIO_2)
3561 ign_dev_err = 1;
3562 /* Some very old devices and some bad newer ones fail
3563 any kind of SET_XFERMODE request but support PIO0-2
3564 timings and no IORDY */
3565 if (!ata_id_has_iordy(dev->id) && dev->pio_mode <= XFER_PIO_2)
3566 ign_dev_err = 1;
3567 }
Tejun Heo4055dee2008-02-07 10:34:08 +09003568 /* Early MWDMA devices do DMA but don't allow DMA mode setting.
3569 Don't fail an MWDMA0 set IFF the device indicates it is in MWDMA0 */
3570 if (dev->xfer_shift == ATA_SHIFT_MWDMA &&
3571 dev->dma_mode == XFER_MW_DMA_0 &&
3572 (dev->id[63] >> 8) & 1)
3573 ign_dev_err = 1;
3574
3575 /* if the device is actually configured correctly, ignore dev err */
3576 if (dev->xfer_mode == ata_xfer_mask2mode(ata_id_xfermask(dev->id)))
3577 ign_dev_err = 1;
3578
3579 if (err_mask & AC_ERR_DEV) {
3580 if (!ign_dev_err)
3581 goto fail;
3582 else
3583 dev_err_whine = " (device error ignored)";
3584 }
3585
Tejun Heo23e71c32006-03-06 04:31:57 +09003586 DPRINTK("xfer_shift=%u, xfer_mode=0x%x\n",
3587 dev->xfer_shift, (int)dev->xfer_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003588
Damien Le Moal07b9b6d2018-05-09 09:28:10 +09003589 if (!(ehc->i.flags & ATA_EHI_QUIET) ||
3590 ehc->i.flags & ATA_EHI_DID_HARDRESET)
3591 ata_dev_info(dev, "configured for %s%s\n",
3592 ata_mode_string(ata_xfer_mode2mask(dev->xfer_mode)),
3593 dev_err_whine);
Tejun Heo4055dee2008-02-07 10:34:08 +09003594
Tejun Heo83206a22006-03-24 15:25:31 +09003595 return 0;
Tejun Heo4055dee2008-02-07 10:34:08 +09003596
3597 fail:
Joe Perchesa9a79df2011-04-15 15:51:59 -07003598 ata_dev_err(dev, "failed to set xfermode (err_mask=0x%x)\n", err_mask);
Tejun Heo4055dee2008-02-07 10:34:08 +09003599 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003600}
3601
Linus Torvalds1da177e2005-04-16 15:20:36 -07003602/**
Alan04351822007-03-06 02:37:52 -08003603 * ata_do_set_mode - Program timings and issue SET FEATURES - XFER
Tejun Heo02607312007-08-06 18:36:23 +09003604 * @link: link on which timings will be programmed
Joe Perches1967b7f2008-02-03 17:08:11 +02003605 * @r_failed_dev: out parameter for failed device
Linus Torvalds1da177e2005-04-16 15:20:36 -07003606 *
Alan04351822007-03-06 02:37:52 -08003607 * Standard implementation of the function used to tune and set
3608 * ATA device disk transfer mode (PIO3, UDMA6, etc.). If
3609 * ata_dev_set_mode() fails, pointer to the failing device is
Tejun Heoe82cbdb2006-04-01 01:38:18 +09003610 * returned in @r_failed_dev.
Jeff Garzik780a87f2005-05-30 15:41:05 -04003611 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003612 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003613 * PCI/etc. bus probe sem.
Tejun Heoe82cbdb2006-04-01 01:38:18 +09003614 *
3615 * RETURNS:
3616 * 0 on success, negative errno otherwise
Linus Torvalds1da177e2005-04-16 15:20:36 -07003617 */
Alan04351822007-03-06 02:37:52 -08003618
Tejun Heo02607312007-08-06 18:36:23 +09003619int ata_do_set_mode(struct ata_link *link, struct ata_device **r_failed_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003620{
Tejun Heo02607312007-08-06 18:36:23 +09003621 struct ata_port *ap = link->ap;
Tejun Heoe8e06192006-04-01 01:38:18 +09003622 struct ata_device *dev;
Tejun Heof58229f2007-08-06 18:36:23 +09003623 int rc = 0, used_dma = 0, found = 0;
Tejun Heo3adcebb2006-05-15 20:57:37 +09003624
Tejun Heoa6d5a512006-03-06 04:31:57 +09003625 /* step 1: calculate xfer_mask */
Tejun Heo1eca4362008-11-03 20:03:17 +09003626 ata_for_each_dev(dev, link, ENABLED) {
Tejun Heo7dc951a2007-11-27 19:43:42 +09003627 unsigned long pio_mask, dma_mask;
Alan Coxb3a70602007-10-02 12:38:26 -04003628 unsigned int mode_mask;
Tejun Heoa6d5a512006-03-06 04:31:57 +09003629
Alan Coxb3a70602007-10-02 12:38:26 -04003630 mode_mask = ATA_DMA_MASK_ATA;
3631 if (dev->class == ATA_DEV_ATAPI)
3632 mode_mask = ATA_DMA_MASK_ATAPI;
3633 else if (ata_id_is_cfa(dev->id))
3634 mode_mask = ATA_DMA_MASK_CFA;
3635
Tejun Heo3373efd2006-05-15 20:57:53 +09003636 ata_dev_xfermask(dev);
Tejun Heo33267322008-02-13 09:15:09 +09003637 ata_force_xfermask(dev);
Tejun Heoa6d5a512006-03-06 04:31:57 +09003638
Tejun Heoacf356b2006-03-24 14:07:50 +09003639 pio_mask = ata_pack_xfermask(dev->pio_mask, 0, 0);
Alan Coxb3a70602007-10-02 12:38:26 -04003640
3641 if (libata_dma_mask & mode_mask)
Sergei Shtylyov80a9c432011-12-22 22:41:32 +03003642 dma_mask = ata_pack_xfermask(0, dev->mwdma_mask,
3643 dev->udma_mask);
Alan Coxb3a70602007-10-02 12:38:26 -04003644 else
3645 dma_mask = 0;
3646
Tejun Heoacf356b2006-03-24 14:07:50 +09003647 dev->pio_mode = ata_xfer_mask2mode(pio_mask);
3648 dev->dma_mode = ata_xfer_mask2mode(dma_mask);
Alan Cox5444a6f2006-03-27 18:58:20 +01003649
Tejun Heo4f659772006-04-01 01:38:18 +09003650 found = 1;
Alan Coxb15b3eb2008-08-01 09:18:34 +01003651 if (ata_dma_enabled(dev))
Alan Cox5444a6f2006-03-27 18:58:20 +01003652 used_dma = 1;
Tejun Heoa6d5a512006-03-06 04:31:57 +09003653 }
Tejun Heo4f659772006-04-01 01:38:18 +09003654 if (!found)
Tejun Heoe82cbdb2006-04-01 01:38:18 +09003655 goto out;
Tejun Heoa6d5a512006-03-06 04:31:57 +09003656
3657 /* step 2: always set host PIO timings */
Tejun Heo1eca4362008-11-03 20:03:17 +09003658 ata_for_each_dev(dev, link, ENABLED) {
Tejun Heo70cd0712007-11-27 19:43:40 +09003659 if (dev->pio_mode == 0xff) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07003660 ata_dev_warn(dev, "no PIO support\n");
Tejun Heoe8e06192006-04-01 01:38:18 +09003661 rc = -EINVAL;
Tejun Heoe82cbdb2006-04-01 01:38:18 +09003662 goto out;
Tejun Heoe8e06192006-04-01 01:38:18 +09003663 }
3664
3665 dev->xfer_mode = dev->pio_mode;
3666 dev->xfer_shift = ATA_SHIFT_PIO;
3667 if (ap->ops->set_piomode)
3668 ap->ops->set_piomode(ap, dev);
3669 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003670
Tejun Heoa6d5a512006-03-06 04:31:57 +09003671 /* step 3: set host DMA timings */
Tejun Heo1eca4362008-11-03 20:03:17 +09003672 ata_for_each_dev(dev, link, ENABLED) {
3673 if (!ata_dma_enabled(dev))
Tejun Heoe8e06192006-04-01 01:38:18 +09003674 continue;
3675
3676 dev->xfer_mode = dev->dma_mode;
3677 dev->xfer_shift = ata_xfer_mode2shift(dev->dma_mode);
3678 if (ap->ops->set_dmamode)
3679 ap->ops->set_dmamode(ap, dev);
3680 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003681
3682 /* step 4: update devices' xfer mode */
Tejun Heo1eca4362008-11-03 20:03:17 +09003683 ata_for_each_dev(dev, link, ENABLED) {
Tejun Heo3373efd2006-05-15 20:57:53 +09003684 rc = ata_dev_set_mode(dev);
Tejun Heo5bbc53f2006-04-01 01:38:17 +09003685 if (rc)
Tejun Heoe82cbdb2006-04-01 01:38:18 +09003686 goto out;
Tejun Heo83206a22006-03-24 15:25:31 +09003687 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003688
Tejun Heoe8e06192006-04-01 01:38:18 +09003689 /* Record simplex status. If we selected DMA then the other
3690 * host channels are not permitted to do so.
Alan Cox5444a6f2006-03-27 18:58:20 +01003691 */
Jeff Garzikcca39742006-08-24 03:19:22 -04003692 if (used_dma && (ap->host->flags & ATA_HOST_SIMPLEX))
Alan032af1c2007-03-01 17:36:46 +00003693 ap->host->simplex_claimed = ap;
Alan Cox5444a6f2006-03-27 18:58:20 +01003694
Tejun Heoe82cbdb2006-04-01 01:38:18 +09003695 out:
3696 if (rc)
3697 *r_failed_dev = dev;
3698 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003699}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01003700EXPORT_SYMBOL_GPL(ata_do_set_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003701
3702/**
Tejun Heoaa2731a2008-04-07 22:47:19 +09003703 * ata_wait_ready - wait for link to become ready
3704 * @link: link to be waited on
3705 * @deadline: deadline jiffies for the operation
3706 * @check_ready: callback to check link readiness
3707 *
3708 * Wait for @link to become ready. @check_ready should return
3709 * positive number if @link is ready, 0 if it isn't, -ENODEV if
3710 * link doesn't seem to be occupied, other errno for other error
3711 * conditions.
3712 *
3713 * Transient -ENODEV conditions are allowed for
3714 * ATA_TMOUT_FF_WAIT.
3715 *
3716 * LOCKING:
3717 * EH context.
3718 *
3719 * RETURNS:
Masanari Iidac9b55602016-04-13 23:36:27 +09003720 * 0 if @link is ready before @deadline; otherwise, -errno.
Tejun Heoaa2731a2008-04-07 22:47:19 +09003721 */
3722int ata_wait_ready(struct ata_link *link, unsigned long deadline,
3723 int (*check_ready)(struct ata_link *link))
3724{
3725 unsigned long start = jiffies;
Tejun Heob48d58f2010-04-09 19:46:38 +09003726 unsigned long nodev_deadline;
Tejun Heoaa2731a2008-04-07 22:47:19 +09003727 int warned = 0;
3728
Tejun Heob48d58f2010-04-09 19:46:38 +09003729 /* choose which 0xff timeout to use, read comment in libata.h */
3730 if (link->ap->host->flags & ATA_HOST_PARALLEL_SCAN)
3731 nodev_deadline = ata_deadline(start, ATA_TMOUT_FF_WAIT_LONG);
3732 else
3733 nodev_deadline = ata_deadline(start, ATA_TMOUT_FF_WAIT);
3734
Tejun Heob1c72912008-07-31 17:02:43 +09003735 /* Slave readiness can't be tested separately from master. On
3736 * M/S emulation configuration, this function should be called
3737 * only on the master and it will handle both master and slave.
3738 */
3739 WARN_ON(link == link->ap->slave_link);
3740
Tejun Heoaa2731a2008-04-07 22:47:19 +09003741 if (time_after(nodev_deadline, deadline))
3742 nodev_deadline = deadline;
3743
3744 while (1) {
3745 unsigned long now = jiffies;
3746 int ready, tmp;
3747
3748 ready = tmp = check_ready(link);
3749 if (ready > 0)
3750 return 0;
3751
Tejun Heob48d58f2010-04-09 19:46:38 +09003752 /*
3753 * -ENODEV could be transient. Ignore -ENODEV if link
Tejun Heoaa2731a2008-04-07 22:47:19 +09003754 * is online. Also, some SATA devices take a long
Tejun Heob48d58f2010-04-09 19:46:38 +09003755 * time to clear 0xff after reset. Wait for
3756 * ATA_TMOUT_FF_WAIT[_LONG] on -ENODEV if link isn't
3757 * offline.
Tejun Heoaa2731a2008-04-07 22:47:19 +09003758 *
3759 * Note that some PATA controllers (pata_ali) explode
3760 * if status register is read more than once when
3761 * there's no device attached.
3762 */
3763 if (ready == -ENODEV) {
3764 if (ata_link_online(link))
3765 ready = 0;
3766 else if ((link->ap->flags & ATA_FLAG_SATA) &&
3767 !ata_link_offline(link) &&
3768 time_before(now, nodev_deadline))
3769 ready = 0;
3770 }
3771
3772 if (ready)
3773 return ready;
3774 if (time_after(now, deadline))
3775 return -EBUSY;
3776
3777 if (!warned && time_after(now, start + 5 * HZ) &&
3778 (deadline - now > 3 * HZ)) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07003779 ata_link_warn(link,
Tejun Heoaa2731a2008-04-07 22:47:19 +09003780 "link is slow to respond, please be patient "
3781 "(ready=%d)\n", tmp);
3782 warned = 1;
3783 }
3784
Tejun Heo97750ce2010-09-06 17:56:29 +02003785 ata_msleep(link->ap, 50);
Tejun Heoaa2731a2008-04-07 22:47:19 +09003786 }
3787}
3788
3789/**
3790 * ata_wait_after_reset - wait for link to become ready after reset
3791 * @link: link to be waited on
3792 * @deadline: deadline jiffies for the operation
3793 * @check_ready: callback to check link readiness
3794 *
3795 * Wait for @link to become ready after reset.
3796 *
3797 * LOCKING:
3798 * EH context.
3799 *
3800 * RETURNS:
Masanari Iidac9b55602016-04-13 23:36:27 +09003801 * 0 if @link is ready before @deadline; otherwise, -errno.
Tejun Heoaa2731a2008-04-07 22:47:19 +09003802 */
Harvey Harrison2b4221b2008-04-24 18:37:34 -07003803int ata_wait_after_reset(struct ata_link *link, unsigned long deadline,
Tejun Heoaa2731a2008-04-07 22:47:19 +09003804 int (*check_ready)(struct ata_link *link))
3805{
Tejun Heo97750ce2010-09-06 17:56:29 +02003806 ata_msleep(link->ap, ATA_WAIT_AFTER_RESET);
Tejun Heoaa2731a2008-04-07 22:47:19 +09003807
3808 return ata_wait_ready(link, deadline, check_ready);
3809}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01003810EXPORT_SYMBOL_GPL(ata_wait_after_reset);
Tejun Heoaa2731a2008-04-07 22:47:19 +09003811
3812/**
Tejun Heo936fd732007-08-06 18:36:23 +09003813 * sata_link_debounce - debounce SATA phy status
3814 * @link: ATA link to debounce SATA phy status for
Masanari Iidac9b55602016-04-13 23:36:27 +09003815 * @params: timing parameters { interval, duration, timeout } in msec
Tejun Heod4b2bab2007-02-02 16:50:52 +09003816 * @deadline: deadline jiffies for the operation
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003817 *
Tejun Heo1152b262010-09-01 17:50:05 +02003818 * Make sure SStatus of @link reaches stable state, determined by
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003819 * holding the same value where DET is not 1 for @duration polled
3820 * every @interval, before @timeout. Timeout constraints the
Tejun Heod4b2bab2007-02-02 16:50:52 +09003821 * beginning of the stable state. Because DET gets stuck at 1 on
3822 * some controllers after hot unplugging, this functions waits
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003823 * until timeout then returns 0 if DET is stable at 1.
3824 *
Tejun Heod4b2bab2007-02-02 16:50:52 +09003825 * @timeout is further limited by @deadline. The sooner of the
3826 * two is used.
3827 *
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003828 * LOCKING:
3829 * Kernel thread context (may sleep)
3830 *
3831 * RETURNS:
3832 * 0 on success, -errno on failure.
3833 */
Tejun Heo936fd732007-08-06 18:36:23 +09003834int sata_link_debounce(struct ata_link *link, const unsigned long *params,
3835 unsigned long deadline)
Tejun Heo7a7921e2006-02-02 18:20:00 +09003836{
Tejun Heo341c2c92008-05-20 02:17:51 +09003837 unsigned long interval = params[0];
3838 unsigned long duration = params[1];
Tejun Heod4b2bab2007-02-02 16:50:52 +09003839 unsigned long last_jiffies, t;
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003840 u32 last, cur;
3841 int rc;
3842
Tejun Heo341c2c92008-05-20 02:17:51 +09003843 t = ata_deadline(jiffies, params[2]);
Tejun Heod4b2bab2007-02-02 16:50:52 +09003844 if (time_before(t, deadline))
3845 deadline = t;
3846
Tejun Heo936fd732007-08-06 18:36:23 +09003847 if ((rc = sata_scr_read(link, SCR_STATUS, &cur)))
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003848 return rc;
3849 cur &= 0xf;
3850
3851 last = cur;
3852 last_jiffies = jiffies;
3853
3854 while (1) {
Tejun Heo97750ce2010-09-06 17:56:29 +02003855 ata_msleep(link->ap, interval);
Tejun Heo936fd732007-08-06 18:36:23 +09003856 if ((rc = sata_scr_read(link, SCR_STATUS, &cur)))
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003857 return rc;
3858 cur &= 0xf;
3859
3860 /* DET stable? */
3861 if (cur == last) {
Tejun Heod4b2bab2007-02-02 16:50:52 +09003862 if (cur == 1 && time_before(jiffies, deadline))
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003863 continue;
Tejun Heo341c2c92008-05-20 02:17:51 +09003864 if (time_after(jiffies,
3865 ata_deadline(last_jiffies, duration)))
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003866 return 0;
3867 continue;
3868 }
3869
3870 /* unstable, start over */
3871 last = cur;
3872 last_jiffies = jiffies;
3873
Tejun Heof1545152007-07-16 14:29:40 +09003874 /* Check deadline. If debouncing failed, return
3875 * -EPIPE to tell upper layer to lower link speed.
3876 */
Tejun Heod4b2bab2007-02-02 16:50:52 +09003877 if (time_after(jiffies, deadline))
Tejun Heof1545152007-07-16 14:29:40 +09003878 return -EPIPE;
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003879 }
3880}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01003881EXPORT_SYMBOL_GPL(sata_link_debounce);
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003882
3883/**
Tejun Heo936fd732007-08-06 18:36:23 +09003884 * sata_link_resume - resume SATA link
3885 * @link: ATA link to resume SATA
Masanari Iidac9b55602016-04-13 23:36:27 +09003886 * @params: timing parameters { interval, duration, timeout } in msec
Tejun Heod4b2bab2007-02-02 16:50:52 +09003887 * @deadline: deadline jiffies for the operation
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003888 *
Tejun Heo936fd732007-08-06 18:36:23 +09003889 * Resume SATA phy @link and debounce it.
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003890 *
3891 * LOCKING:
3892 * Kernel thread context (may sleep)
3893 *
3894 * RETURNS:
3895 * 0 on success, -errno on failure.
3896 */
Tejun Heo936fd732007-08-06 18:36:23 +09003897int sata_link_resume(struct ata_link *link, const unsigned long *params,
3898 unsigned long deadline)
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003899{
Tejun Heo5040ab62010-01-11 11:14:44 +09003900 int tries = ATA_LINK_RESUME_TRIES;
Tejun Heoac371982008-04-07 22:47:19 +09003901 u32 scontrol, serror;
Tejun Heo81952c52006-05-15 20:57:47 +09003902 int rc;
Tejun Heo7a7921e2006-02-02 18:20:00 +09003903
Tejun Heo936fd732007-08-06 18:36:23 +09003904 if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
Tejun Heo81952c52006-05-15 20:57:47 +09003905 return rc;
3906
Tejun Heo5040ab62010-01-11 11:14:44 +09003907 /*
3908 * Writes to SControl sometimes get ignored under certain
3909 * controllers (ata_piix SIDPR). Make sure DET actually is
3910 * cleared.
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003911 */
Tejun Heo5040ab62010-01-11 11:14:44 +09003912 do {
3913 scontrol = (scontrol & 0x0f0) | 0x300;
3914 if ((rc = sata_scr_write(link, SCR_CONTROL, scontrol)))
3915 return rc;
3916 /*
3917 * Some PHYs react badly if SStatus is pounded
3918 * immediately after resuming. Delay 200ms before
3919 * debouncing.
3920 */
Danesh Petigarae39b2bb2016-01-07 16:03:33 -08003921 if (!(link->flags & ATA_LFLAG_NO_DB_DELAY))
3922 ata_msleep(link->ap, 200);
Tejun Heo5040ab62010-01-11 11:14:44 +09003923
3924 /* is SControl restored correctly? */
3925 if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
3926 return rc;
3927 } while ((scontrol & 0xf0f) != 0x300 && --tries);
3928
3929 if ((scontrol & 0xf0f) != 0x300) {
Tejun Heo38941c92011-07-01 15:12:09 +02003930 ata_link_warn(link, "failed to resume link (SControl %X)\n",
Joe Perchesa9a79df2011-04-15 15:51:59 -07003931 scontrol);
Tejun Heo5040ab62010-01-11 11:14:44 +09003932 return 0;
3933 }
3934
3935 if (tries < ATA_LINK_RESUME_TRIES)
Joe Perchesa9a79df2011-04-15 15:51:59 -07003936 ata_link_warn(link, "link resume succeeded after %d retries\n",
3937 ATA_LINK_RESUME_TRIES - tries);
Tejun Heo7a7921e2006-02-02 18:20:00 +09003938
Tejun Heoac371982008-04-07 22:47:19 +09003939 if ((rc = sata_link_debounce(link, params, deadline)))
3940 return rc;
3941
Tejun Heof0465192008-05-19 01:15:08 +09003942 /* clear SError, some PHYs require this even for SRST to work */
Tejun Heoac371982008-04-07 22:47:19 +09003943 if (!(rc = sata_scr_read(link, SCR_ERROR, &serror)))
3944 rc = sata_scr_write(link, SCR_ERROR, serror);
Tejun Heoac371982008-04-07 22:47:19 +09003945
Tejun Heof0465192008-05-19 01:15:08 +09003946 return rc != -EINVAL ? rc : 0;
Tejun Heo7a7921e2006-02-02 18:20:00 +09003947}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01003948EXPORT_SYMBOL_GPL(sata_link_resume);
Tejun Heo7a7921e2006-02-02 18:20:00 +09003949
Tejun Heof5914a42006-05-31 18:27:48 +09003950/**
Tejun Heo1152b262010-09-01 17:50:05 +02003951 * sata_link_scr_lpm - manipulate SControl IPM and SPM fields
3952 * @link: ATA link to manipulate SControl for
3953 * @policy: LPM policy to configure
3954 * @spm_wakeup: initiate LPM transition to active state
3955 *
3956 * Manipulate the IPM field of the SControl register of @link
3957 * according to @policy. If @policy is ATA_LPM_MAX_POWER and
3958 * @spm_wakeup is %true, the SPM field is manipulated to wake up
3959 * the link. This function also clears PHYRDY_CHG before
3960 * returning.
3961 *
3962 * LOCKING:
3963 * EH context.
3964 *
3965 * RETURNS:
Masanari Iida84851872015-05-29 23:11:51 +09003966 * 0 on success, -errno otherwise.
Tejun Heo1152b262010-09-01 17:50:05 +02003967 */
3968int sata_link_scr_lpm(struct ata_link *link, enum ata_lpm_policy policy,
3969 bool spm_wakeup)
3970{
3971 struct ata_eh_context *ehc = &link->eh_context;
3972 bool woken_up = false;
3973 u32 scontrol;
3974 int rc;
3975
3976 rc = sata_scr_read(link, SCR_CONTROL, &scontrol);
3977 if (rc)
3978 return rc;
3979
3980 switch (policy) {
3981 case ATA_LPM_MAX_POWER:
3982 /* disable all LPM transitions */
Shane Huang65fe1f02012-09-07 22:40:01 +08003983 scontrol |= (0x7 << 8);
Tejun Heo1152b262010-09-01 17:50:05 +02003984 /* initiate transition to active state */
3985 if (spm_wakeup) {
3986 scontrol |= (0x4 << 12);
3987 woken_up = true;
3988 }
3989 break;
3990 case ATA_LPM_MED_POWER:
3991 /* allow LPM to PARTIAL */
3992 scontrol &= ~(0x1 << 8);
Shane Huang65fe1f02012-09-07 22:40:01 +08003993 scontrol |= (0x6 << 8);
Tejun Heo1152b262010-09-01 17:50:05 +02003994 break;
Hans de Goedef4ac6472017-09-14 12:35:36 +02003995 case ATA_LPM_MED_POWER_WITH_DIPM:
Srinivas Pandruvadaa5ec5a7b2018-07-27 13:47:02 -07003996 case ATA_LPM_MIN_POWER_WITH_PARTIAL:
Tejun Heo1152b262010-09-01 17:50:05 +02003997 case ATA_LPM_MIN_POWER:
Kristen Carlson Accardi8a745f12011-03-04 10:24:11 -08003998 if (ata_link_nr_enabled(link) > 0)
3999 /* no restrictions on LPM transitions */
Shane Huang65fe1f02012-09-07 22:40:01 +08004000 scontrol &= ~(0x7 << 8);
Kristen Carlson Accardi8a745f12011-03-04 10:24:11 -08004001 else {
4002 /* empty port, power off */
4003 scontrol &= ~0xf;
4004 scontrol |= (0x1 << 2);
4005 }
Tejun Heo1152b262010-09-01 17:50:05 +02004006 break;
4007 default:
4008 WARN_ON(1);
4009 }
4010
4011 rc = sata_scr_write(link, SCR_CONTROL, scontrol);
4012 if (rc)
4013 return rc;
4014
4015 /* give the link time to transit out of LPM state */
4016 if (woken_up)
4017 msleep(10);
4018
4019 /* clear PHYRDY_CHG from SError */
4020 ehc->i.serror &= ~SERR_PHYRDY_CHG;
4021 return sata_scr_write(link, SCR_ERROR, SERR_PHYRDY_CHG);
4022}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01004023EXPORT_SYMBOL_GPL(sata_link_scr_lpm);
Tejun Heo1152b262010-09-01 17:50:05 +02004024
4025/**
Tejun Heo0aa11132008-04-07 22:47:18 +09004026 * ata_std_prereset - prepare for reset
Tejun Heocc0680a2007-08-06 18:36:23 +09004027 * @link: ATA link to be reset
Tejun Heod4b2bab2007-02-02 16:50:52 +09004028 * @deadline: deadline jiffies for the operation
Tejun Heof5914a42006-05-31 18:27:48 +09004029 *
Tejun Heocc0680a2007-08-06 18:36:23 +09004030 * @link is about to be reset. Initialize it. Failure from
Tejun Heob8cffc62007-02-02 16:50:52 +09004031 * prereset makes libata abort whole reset sequence and give up
4032 * that port, so prereset should be best-effort. It does its
4033 * best to prepare for reset sequence but if things go wrong, it
4034 * should just whine, not fail.
Tejun Heof5914a42006-05-31 18:27:48 +09004035 *
4036 * LOCKING:
4037 * Kernel thread context (may sleep)
4038 *
4039 * RETURNS:
4040 * 0 on success, -errno otherwise.
4041 */
Tejun Heo0aa11132008-04-07 22:47:18 +09004042int ata_std_prereset(struct ata_link *link, unsigned long deadline)
Tejun Heof5914a42006-05-31 18:27:48 +09004043{
Tejun Heocc0680a2007-08-06 18:36:23 +09004044 struct ata_port *ap = link->ap;
Tejun Heo936fd732007-08-06 18:36:23 +09004045 struct ata_eh_context *ehc = &link->eh_context;
Tejun Heoe9c83912006-07-03 16:07:26 +09004046 const unsigned long *timing = sata_ehc_deb_timing(ehc);
Tejun Heof5914a42006-05-31 18:27:48 +09004047 int rc;
4048
Tejun Heof5914a42006-05-31 18:27:48 +09004049 /* if we're about to do hardreset, nothing more to do */
4050 if (ehc->i.action & ATA_EH_HARDRESET)
4051 return 0;
4052
Tejun Heo936fd732007-08-06 18:36:23 +09004053 /* if SATA, resume link */
Tejun Heoa16abc02007-05-21 18:33:47 +02004054 if (ap->flags & ATA_FLAG_SATA) {
Tejun Heo936fd732007-08-06 18:36:23 +09004055 rc = sata_link_resume(link, timing, deadline);
Tejun Heob8cffc62007-02-02 16:50:52 +09004056 /* whine about phy resume failure but proceed */
4057 if (rc && rc != -EOPNOTSUPP)
Joe Perchesa9a79df2011-04-15 15:51:59 -07004058 ata_link_warn(link,
4059 "failed to resume link for reset (errno=%d)\n",
4060 rc);
Tejun Heof5914a42006-05-31 18:27:48 +09004061 }
4062
Tejun Heo45db2f62008-04-08 01:46:56 +09004063 /* no point in trying softreset on offline link */
Tejun Heob1c72912008-07-31 17:02:43 +09004064 if (ata_phys_link_offline(link))
Tejun Heo45db2f62008-04-08 01:46:56 +09004065 ehc->i.action &= ~ATA_EH_SOFTRESET;
4066
Tejun Heof5914a42006-05-31 18:27:48 +09004067 return 0;
4068}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01004069EXPORT_SYMBOL_GPL(ata_std_prereset);
Tejun Heof5914a42006-05-31 18:27:48 +09004070
Tejun Heoc2bd5802006-01-24 17:05:22 +09004071/**
Tejun Heocc0680a2007-08-06 18:36:23 +09004072 * sata_link_hardreset - reset link via SATA phy reset
4073 * @link: link to reset
Masanari Iidac9b55602016-04-13 23:36:27 +09004074 * @timing: timing parameters { interval, duration, timeout } in msec
Tejun Heod4b2bab2007-02-02 16:50:52 +09004075 * @deadline: deadline jiffies for the operation
Tejun Heo9dadd452008-04-07 22:47:19 +09004076 * @online: optional out parameter indicating link onlineness
4077 * @check_ready: optional callback to check link readiness
Tejun Heoc2bd5802006-01-24 17:05:22 +09004078 *
Tejun Heocc0680a2007-08-06 18:36:23 +09004079 * SATA phy-reset @link using DET bits of SControl register.
Tejun Heo9dadd452008-04-07 22:47:19 +09004080 * After hardreset, link readiness is waited upon using
4081 * ata_wait_ready() if @check_ready is specified. LLDs are
4082 * allowed to not specify @check_ready and wait itself after this
4083 * function returns. Device classification is LLD's
4084 * responsibility.
4085 *
4086 * *@online is set to one iff reset succeeded and @link is online
4087 * after reset.
Tejun Heoc2bd5802006-01-24 17:05:22 +09004088 *
4089 * LOCKING:
4090 * Kernel thread context (may sleep)
4091 *
4092 * RETURNS:
4093 * 0 on success, -errno otherwise.
4094 */
Tejun Heocc0680a2007-08-06 18:36:23 +09004095int sata_link_hardreset(struct ata_link *link, const unsigned long *timing,
Tejun Heo9dadd452008-04-07 22:47:19 +09004096 unsigned long deadline,
4097 bool *online, int (*check_ready)(struct ata_link *))
Tejun Heoc2bd5802006-01-24 17:05:22 +09004098{
Tejun Heo852ee162006-04-01 01:38:18 +09004099 u32 scontrol;
Tejun Heo81952c52006-05-15 20:57:47 +09004100 int rc;
Tejun Heo852ee162006-04-01 01:38:18 +09004101
Tejun Heoc2bd5802006-01-24 17:05:22 +09004102 DPRINTK("ENTER\n");
4103
Tejun Heo9dadd452008-04-07 22:47:19 +09004104 if (online)
4105 *online = false;
4106
Tejun Heo936fd732007-08-06 18:36:23 +09004107 if (sata_set_spd_needed(link)) {
Tejun Heo1c3fae42006-04-02 20:53:28 +09004108 /* SATA spec says nothing about how to reconfigure
4109 * spd. To be on the safe side, turn off phy during
4110 * reconfiguration. This works for at least ICH7 AHCI
4111 * and Sil3124.
4112 */
Tejun Heo936fd732007-08-06 18:36:23 +09004113 if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
Tejun Heob6103f62006-11-01 17:59:53 +09004114 goto out;
Tejun Heo81952c52006-05-15 20:57:47 +09004115
Martin Hicksa34b6fc2006-07-05 15:06:13 -04004116 scontrol = (scontrol & 0x0f0) | 0x304;
Tejun Heo81952c52006-05-15 20:57:47 +09004117
Tejun Heo936fd732007-08-06 18:36:23 +09004118 if ((rc = sata_scr_write(link, SCR_CONTROL, scontrol)))
Tejun Heob6103f62006-11-01 17:59:53 +09004119 goto out;
Tejun Heo1c3fae42006-04-02 20:53:28 +09004120
Tejun Heo936fd732007-08-06 18:36:23 +09004121 sata_set_spd(link);
Tejun Heo1c3fae42006-04-02 20:53:28 +09004122 }
4123
4124 /* issue phy wake/reset */
Tejun Heo936fd732007-08-06 18:36:23 +09004125 if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
Tejun Heob6103f62006-11-01 17:59:53 +09004126 goto out;
Tejun Heo81952c52006-05-15 20:57:47 +09004127
Tejun Heo852ee162006-04-01 01:38:18 +09004128 scontrol = (scontrol & 0x0f0) | 0x301;
Tejun Heo81952c52006-05-15 20:57:47 +09004129
Tejun Heo936fd732007-08-06 18:36:23 +09004130 if ((rc = sata_scr_write_flush(link, SCR_CONTROL, scontrol)))
Tejun Heob6103f62006-11-01 17:59:53 +09004131 goto out;
Tejun Heoc2bd5802006-01-24 17:05:22 +09004132
Tejun Heo1c3fae42006-04-02 20:53:28 +09004133 /* Couldn't find anything in SATA I/II specs, but AHCI-1.1
Tejun Heoc2bd5802006-01-24 17:05:22 +09004134 * 10.4.2 says at least 1 ms.
4135 */
Tejun Heo97750ce2010-09-06 17:56:29 +02004136 ata_msleep(link->ap, 1);
Tejun Heoc2bd5802006-01-24 17:05:22 +09004137
Tejun Heo936fd732007-08-06 18:36:23 +09004138 /* bring link back */
4139 rc = sata_link_resume(link, timing, deadline);
Tejun Heo9dadd452008-04-07 22:47:19 +09004140 if (rc)
4141 goto out;
4142 /* if link is offline nothing more to do */
Tejun Heob1c72912008-07-31 17:02:43 +09004143 if (ata_phys_link_offline(link))
Tejun Heo9dadd452008-04-07 22:47:19 +09004144 goto out;
4145
4146 /* Link is online. From this point, -ENODEV too is an error. */
4147 if (online)
4148 *online = true;
4149
Tejun Heo071f44b2008-04-07 22:47:22 +09004150 if (sata_pmp_supported(link->ap) && ata_is_host_link(link)) {
Tejun Heo9dadd452008-04-07 22:47:19 +09004151 /* If PMP is supported, we have to do follow-up SRST.
4152 * Some PMPs don't send D2H Reg FIS after hardreset if
4153 * the first port is empty. Wait only for
4154 * ATA_TMOUT_PMP_SRST_WAIT.
4155 */
4156 if (check_ready) {
4157 unsigned long pmp_deadline;
4158
Tejun Heo341c2c92008-05-20 02:17:51 +09004159 pmp_deadline = ata_deadline(jiffies,
4160 ATA_TMOUT_PMP_SRST_WAIT);
Tejun Heo9dadd452008-04-07 22:47:19 +09004161 if (time_after(pmp_deadline, deadline))
4162 pmp_deadline = deadline;
4163 ata_wait_ready(link, pmp_deadline, check_ready);
4164 }
4165 rc = -EAGAIN;
4166 goto out;
4167 }
4168
4169 rc = 0;
4170 if (check_ready)
4171 rc = ata_wait_ready(link, deadline, check_ready);
Tejun Heob6103f62006-11-01 17:59:53 +09004172 out:
Tejun Heo0cbf0712008-05-19 01:15:05 +09004173 if (rc && rc != -EAGAIN) {
4174 /* online is set iff link is online && reset succeeded */
4175 if (online)
4176 *online = false;
Joe Perchesa9a79df2011-04-15 15:51:59 -07004177 ata_link_err(link, "COMRESET failed (errno=%d)\n", rc);
Tejun Heo0cbf0712008-05-19 01:15:05 +09004178 }
Tejun Heob6103f62006-11-01 17:59:53 +09004179 DPRINTK("EXIT, rc=%d\n", rc);
4180 return rc;
4181}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01004182EXPORT_SYMBOL_GPL(sata_link_hardreset);
Tejun Heob6103f62006-11-01 17:59:53 +09004183
4184/**
Tejun Heo57c9efd2008-04-07 22:47:19 +09004185 * sata_std_hardreset - COMRESET w/o waiting or classification
4186 * @link: link to reset
4187 * @class: resulting class of attached device
4188 * @deadline: deadline jiffies for the operation
4189 *
4190 * Standard SATA COMRESET w/o waiting or classification.
4191 *
4192 * LOCKING:
4193 * Kernel thread context (may sleep)
4194 *
4195 * RETURNS:
4196 * 0 if link offline, -EAGAIN if link online, -errno on errors.
4197 */
4198int sata_std_hardreset(struct ata_link *link, unsigned int *class,
4199 unsigned long deadline)
4200{
4201 const unsigned long *timing = sata_ehc_deb_timing(&link->eh_context);
4202 bool online;
4203 int rc;
4204
4205 /* do hardreset */
4206 rc = sata_link_hardreset(link, timing, deadline, &online, NULL);
Tejun Heo57c9efd2008-04-07 22:47:19 +09004207 return online ? -EAGAIN : rc;
4208}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01004209EXPORT_SYMBOL_GPL(sata_std_hardreset);
Tejun Heo57c9efd2008-04-07 22:47:19 +09004210
4211/**
Tejun Heo203c75b2008-04-07 22:47:18 +09004212 * ata_std_postreset - standard postreset callback
Tejun Heocc0680a2007-08-06 18:36:23 +09004213 * @link: the target ata_link
Tejun Heoc2bd5802006-01-24 17:05:22 +09004214 * @classes: classes of attached devices
4215 *
4216 * This function is invoked after a successful reset. Note that
4217 * the device might have been reset more than once using
4218 * different reset methods before postreset is invoked.
Tejun Heoc2bd5802006-01-24 17:05:22 +09004219 *
Tejun Heoc2bd5802006-01-24 17:05:22 +09004220 * LOCKING:
4221 * Kernel thread context (may sleep)
4222 */
Tejun Heo203c75b2008-04-07 22:47:18 +09004223void ata_std_postreset(struct ata_link *link, unsigned int *classes)
Tejun Heoc2bd5802006-01-24 17:05:22 +09004224{
Tejun Heof0465192008-05-19 01:15:08 +09004225 u32 serror;
4226
Tejun Heoc2bd5802006-01-24 17:05:22 +09004227 DPRINTK("ENTER\n");
4228
Tejun Heof0465192008-05-19 01:15:08 +09004229 /* reset complete, clear SError */
4230 if (!sata_scr_read(link, SCR_ERROR, &serror))
4231 sata_scr_write(link, SCR_ERROR, serror);
4232
Tejun Heoc2bd5802006-01-24 17:05:22 +09004233 /* print link status */
Tejun Heo936fd732007-08-06 18:36:23 +09004234 sata_print_link_status(link);
Tejun Heoc2bd5802006-01-24 17:05:22 +09004235
Tejun Heoc2bd5802006-01-24 17:05:22 +09004236 DPRINTK("EXIT\n");
4237}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01004238EXPORT_SYMBOL_GPL(ata_std_postreset);
Tejun Heoc2bd5802006-01-24 17:05:22 +09004239
Tejun Heoa62c0fc2006-01-24 17:05:22 +09004240/**
Tejun Heo623a3122006-03-05 17:55:58 +09004241 * ata_dev_same_device - Determine whether new ID matches configured device
Tejun Heo623a3122006-03-05 17:55:58 +09004242 * @dev: device to compare against
4243 * @new_class: class of the new device
4244 * @new_id: IDENTIFY page of the new device
4245 *
4246 * Compare @new_class and @new_id against @dev and determine
4247 * whether @dev is the device indicated by @new_class and
4248 * @new_id.
4249 *
4250 * LOCKING:
4251 * None.
4252 *
4253 * RETURNS:
4254 * 1 if @dev matches @new_class and @new_id, 0 otherwise.
4255 */
Tejun Heo3373efd2006-05-15 20:57:53 +09004256static int ata_dev_same_device(struct ata_device *dev, unsigned int new_class,
4257 const u16 *new_id)
Tejun Heo623a3122006-03-05 17:55:58 +09004258{
4259 const u16 *old_id = dev->id;
Tejun Heoa0cf7332007-01-02 20:18:49 +09004260 unsigned char model[2][ATA_ID_PROD_LEN + 1];
4261 unsigned char serial[2][ATA_ID_SERNO_LEN + 1];
Tejun Heo623a3122006-03-05 17:55:58 +09004262
4263 if (dev->class != new_class) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07004264 ata_dev_info(dev, "class mismatch %d != %d\n",
4265 dev->class, new_class);
Tejun Heo623a3122006-03-05 17:55:58 +09004266 return 0;
4267 }
4268
Tejun Heoa0cf7332007-01-02 20:18:49 +09004269 ata_id_c_string(old_id, model[0], ATA_ID_PROD, sizeof(model[0]));
4270 ata_id_c_string(new_id, model[1], ATA_ID_PROD, sizeof(model[1]));
4271 ata_id_c_string(old_id, serial[0], ATA_ID_SERNO, sizeof(serial[0]));
4272 ata_id_c_string(new_id, serial[1], ATA_ID_SERNO, sizeof(serial[1]));
Tejun Heo623a3122006-03-05 17:55:58 +09004273
4274 if (strcmp(model[0], model[1])) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07004275 ata_dev_info(dev, "model number mismatch '%s' != '%s'\n",
4276 model[0], model[1]);
Tejun Heo623a3122006-03-05 17:55:58 +09004277 return 0;
4278 }
4279
4280 if (strcmp(serial[0], serial[1])) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07004281 ata_dev_info(dev, "serial number mismatch '%s' != '%s'\n",
4282 serial[0], serial[1]);
Tejun Heo623a3122006-03-05 17:55:58 +09004283 return 0;
4284 }
4285
Tejun Heo623a3122006-03-05 17:55:58 +09004286 return 1;
4287}
4288
4289/**
Tejun Heofe309112007-05-15 03:28:15 +09004290 * ata_dev_reread_id - Re-read IDENTIFY data
Henrik Kretzschmar3fae4502007-06-19 10:10:50 +02004291 * @dev: target ATA device
Tejun Heobff04642006-11-10 18:08:10 +09004292 * @readid_flags: read ID flags
Tejun Heo623a3122006-03-05 17:55:58 +09004293 *
4294 * Re-read IDENTIFY page and make sure @dev is still attached to
4295 * the port.
4296 *
4297 * LOCKING:
4298 * Kernel thread context (may sleep)
4299 *
4300 * RETURNS:
4301 * 0 on success, negative errno otherwise
4302 */
Tejun Heofe309112007-05-15 03:28:15 +09004303int ata_dev_reread_id(struct ata_device *dev, unsigned int readid_flags)
Tejun Heo623a3122006-03-05 17:55:58 +09004304{
Tejun Heo5eb45c02006-04-02 18:51:52 +09004305 unsigned int class = dev->class;
Tejun Heo9af5c9c2007-08-06 18:36:22 +09004306 u16 *id = (void *)dev->link->ap->sector_buf;
Tejun Heo623a3122006-03-05 17:55:58 +09004307 int rc;
4308
Tejun Heofe635c72006-05-15 20:57:35 +09004309 /* read ID data */
Tejun Heobff04642006-11-10 18:08:10 +09004310 rc = ata_dev_read_id(dev, &class, readid_flags, id);
Tejun Heo623a3122006-03-05 17:55:58 +09004311 if (rc)
Tejun Heofe309112007-05-15 03:28:15 +09004312 return rc;
Tejun Heo623a3122006-03-05 17:55:58 +09004313
4314 /* is the device still there? */
Tejun Heofe309112007-05-15 03:28:15 +09004315 if (!ata_dev_same_device(dev, class, id))
4316 return -ENODEV;
Tejun Heo623a3122006-03-05 17:55:58 +09004317
Tejun Heofe635c72006-05-15 20:57:35 +09004318 memcpy(dev->id, id, sizeof(id[0]) * ATA_ID_WORDS);
Tejun Heofe309112007-05-15 03:28:15 +09004319 return 0;
4320}
4321
4322/**
4323 * ata_dev_revalidate - Revalidate ATA device
4324 * @dev: device to revalidate
Tejun Heo422c9da2007-09-23 13:14:12 +09004325 * @new_class: new class code
Tejun Heofe309112007-05-15 03:28:15 +09004326 * @readid_flags: read ID flags
4327 *
4328 * Re-read IDENTIFY page, make sure @dev is still attached to the
4329 * port and reconfigure it according to the new IDENTIFY page.
4330 *
4331 * LOCKING:
4332 * Kernel thread context (may sleep)
4333 *
4334 * RETURNS:
4335 * 0 on success, negative errno otherwise
4336 */
Tejun Heo422c9da2007-09-23 13:14:12 +09004337int ata_dev_revalidate(struct ata_device *dev, unsigned int new_class,
4338 unsigned int readid_flags)
Tejun Heofe309112007-05-15 03:28:15 +09004339{
Tejun Heo6ddcd3b2007-05-15 03:28:15 +09004340 u64 n_sectors = dev->n_sectors;
Tejun Heo5920dad2009-07-15 17:11:41 +09004341 u64 n_native_sectors = dev->n_native_sectors;
Tejun Heofe309112007-05-15 03:28:15 +09004342 int rc;
4343
4344 if (!ata_dev_enabled(dev))
4345 return -ENODEV;
4346
Tejun Heo422c9da2007-09-23 13:14:12 +09004347 /* fail early if !ATA && !ATAPI to avoid issuing [P]IDENTIFY to PMP */
4348 if (ata_class_enabled(new_class) &&
Borislav Petkovf0d06132009-04-26 16:33:34 +02004349 new_class != ATA_DEV_ATA &&
4350 new_class != ATA_DEV_ATAPI &&
Hannes Reinecke9162c652014-11-05 13:08:21 +01004351 new_class != ATA_DEV_ZAC &&
Borislav Petkovf0d06132009-04-26 16:33:34 +02004352 new_class != ATA_DEV_SEMB) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07004353 ata_dev_info(dev, "class mismatch %u != %u\n",
4354 dev->class, new_class);
Tejun Heo422c9da2007-09-23 13:14:12 +09004355 rc = -ENODEV;
4356 goto fail;
4357 }
4358
Tejun Heofe309112007-05-15 03:28:15 +09004359 /* re-read ID */
4360 rc = ata_dev_reread_id(dev, readid_flags);
4361 if (rc)
4362 goto fail;
Tejun Heo623a3122006-03-05 17:55:58 +09004363
4364 /* configure device according to the new ID */
Tejun Heoefdaedc2006-11-01 18:38:52 +09004365 rc = ata_dev_configure(dev);
Tejun Heo6ddcd3b2007-05-15 03:28:15 +09004366 if (rc)
4367 goto fail;
4368
4369 /* verify n_sectors hasn't changed */
Tejun Heo445d2112010-04-05 10:33:13 +09004370 if (dev->class != ATA_DEV_ATA || !n_sectors ||
4371 dev->n_sectors == n_sectors)
4372 return 0;
4373
4374 /* n_sectors has changed */
Joe Perchesa9a79df2011-04-15 15:51:59 -07004375 ata_dev_warn(dev, "n_sectors mismatch %llu != %llu\n",
4376 (unsigned long long)n_sectors,
4377 (unsigned long long)dev->n_sectors);
Tejun Heo445d2112010-04-05 10:33:13 +09004378
4379 /*
4380 * Something could have caused HPA to be unlocked
4381 * involuntarily. If n_native_sectors hasn't changed and the
4382 * new size matches it, keep the device.
4383 */
4384 if (dev->n_native_sectors == n_native_sectors &&
4385 dev->n_sectors > n_sectors && dev->n_sectors == n_native_sectors) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07004386 ata_dev_warn(dev,
4387 "new n_sectors matches native, probably "
4388 "late HPA unlock, n_sectors updated\n");
Tejun Heo68939ce2010-05-15 20:09:33 +02004389 /* use the larger n_sectors */
Tejun Heo445d2112010-04-05 10:33:13 +09004390 return 0;
Tejun Heo6ddcd3b2007-05-15 03:28:15 +09004391 }
4392
Tejun Heo445d2112010-04-05 10:33:13 +09004393 /*
4394 * Some BIOSes boot w/o HPA but resume w/ HPA locked. Try
4395 * unlocking HPA in those cases.
4396 *
4397 * https://bugzilla.kernel.org/show_bug.cgi?id=15396
4398 */
4399 if (dev->n_native_sectors == n_native_sectors &&
4400 dev->n_sectors < n_sectors && n_sectors == n_native_sectors &&
4401 !(dev->horkage & ATA_HORKAGE_BROKEN_HPA)) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07004402 ata_dev_warn(dev,
4403 "old n_sectors matches native, probably "
4404 "late HPA lock, will try to unlock HPA\n");
Tejun Heo445d2112010-04-05 10:33:13 +09004405 /* try unlocking HPA */
4406 dev->flags |= ATA_DFLAG_UNLOCK_HPA;
4407 rc = -EIO;
4408 } else
4409 rc = -ENODEV;
Tejun Heo623a3122006-03-05 17:55:58 +09004410
Tejun Heo445d2112010-04-05 10:33:13 +09004411 /* restore original n_[native_]sectors and fail */
4412 dev->n_native_sectors = n_native_sectors;
4413 dev->n_sectors = n_sectors;
Tejun Heo623a3122006-03-05 17:55:58 +09004414 fail:
Joe Perchesa9a79df2011-04-15 15:51:59 -07004415 ata_dev_err(dev, "revalidation failed (errno=%d)\n", rc);
Tejun Heo623a3122006-03-05 17:55:58 +09004416 return rc;
4417}
4418
Alan Cox6919a0a2006-10-27 19:08:46 -07004419struct ata_blacklist_entry {
4420 const char *model_num;
4421 const char *model_rev;
4422 unsigned long horkage;
4423};
4424
4425static const struct ata_blacklist_entry ata_device_blacklist [] = {
4426 /* Devices with DMA related problems under Linux */
4427 { "WDC AC11000H", NULL, ATA_HORKAGE_NODMA },
4428 { "WDC AC22100H", NULL, ATA_HORKAGE_NODMA },
4429 { "WDC AC32500H", NULL, ATA_HORKAGE_NODMA },
4430 { "WDC AC33100H", NULL, ATA_HORKAGE_NODMA },
4431 { "WDC AC31600H", NULL, ATA_HORKAGE_NODMA },
4432 { "WDC AC32100H", "24.09P07", ATA_HORKAGE_NODMA },
4433 { "WDC AC23200L", "21.10N21", ATA_HORKAGE_NODMA },
4434 { "Compaq CRD-8241B", NULL, ATA_HORKAGE_NODMA },
4435 { "CRD-8400B", NULL, ATA_HORKAGE_NODMA },
Mark Lord7da4c932010-07-01 18:18:12 -04004436 { "CRD-848[02]B", NULL, ATA_HORKAGE_NODMA },
Alan Cox6919a0a2006-10-27 19:08:46 -07004437 { "CRD-84", NULL, ATA_HORKAGE_NODMA },
4438 { "SanDisk SDP3B", NULL, ATA_HORKAGE_NODMA },
4439 { "SanDisk SDP3B-64", NULL, ATA_HORKAGE_NODMA },
4440 { "SANYO CD-ROM CRD", NULL, ATA_HORKAGE_NODMA },
4441 { "HITACHI CDR-8", NULL, ATA_HORKAGE_NODMA },
Mark Lord7da4c932010-07-01 18:18:12 -04004442 { "HITACHI CDR-8[34]35",NULL, ATA_HORKAGE_NODMA },
Alan Cox6919a0a2006-10-27 19:08:46 -07004443 { "Toshiba CD-ROM XM-6202B", NULL, ATA_HORKAGE_NODMA },
4444 { "TOSHIBA CD-ROM XM-1702BC", NULL, ATA_HORKAGE_NODMA },
4445 { "CD-532E-A", NULL, ATA_HORKAGE_NODMA },
4446 { "E-IDE CD-ROM CR-840",NULL, ATA_HORKAGE_NODMA },
4447 { "CD-ROM Drive/F5A", NULL, ATA_HORKAGE_NODMA },
4448 { "WPI CDD-820", NULL, ATA_HORKAGE_NODMA },
4449 { "SAMSUNG CD-ROM SC-148C", NULL, ATA_HORKAGE_NODMA },
4450 { "SAMSUNG CD-ROM SC", NULL, ATA_HORKAGE_NODMA },
Alan Cox6919a0a2006-10-27 19:08:46 -07004451 { "ATAPI CD-ROM DRIVE 40X MAXIMUM",NULL,ATA_HORKAGE_NODMA },
4452 { "_NEC DV5800A", NULL, ATA_HORKAGE_NODMA },
Jeff Garzik2dcb4072007-10-19 06:42:56 -04004453 { "SAMSUNG CD-ROM SN-124", "N001", ATA_HORKAGE_NODMA },
Dave Jones39f19882007-05-21 14:31:03 -04004454 { "Seagate STT20000A", NULL, ATA_HORKAGE_NODMA },
Prarit Bhargavad17d7942012-08-23 15:11:52 -04004455 { " 2GB ATA Flash Disk", "ADMA428M", ATA_HORKAGE_NODMA },
Shikha Jainb00622f2016-01-11 14:55:45 -08004456 { "VRFDFC22048UCHC-TE*", NULL, ATA_HORKAGE_NODMA },
Tejun Heo3af9a772007-09-23 13:19:54 +09004457 /* Odd clown on sil3726/4726 PMPs */
Tejun Heo50af2fa2008-05-19 01:15:14 +09004458 { "Config Disk", NULL, ATA_HORKAGE_DISABLE },
Alan Cox6919a0a2006-10-27 19:08:46 -07004459
Albert Lee18d6e9d2007-04-02 11:34:15 +08004460 /* Weird ATAPI devices */
Tejun Heo40a1d532007-06-27 02:49:38 +09004461 { "TORiSAN DVD-ROM DRD-N216", NULL, ATA_HORKAGE_MAX_SEC_128 },
Tejun Heo6a87e422008-11-03 19:01:09 +09004462 { "QUANTUM DAT DAT72-000", NULL, ATA_HORKAGE_ATAPI_MOD16_DMA },
Shan Haia32450e2013-03-18 10:30:44 +08004463 { "Slimtype DVD A DS8A8SH", NULL, ATA_HORKAGE_MAX_SEC_LBA48 },
Shan Hai0523f032013-10-28 16:08:01 +08004464 { "Slimtype DVD A DS8A9SH", NULL, ATA_HORKAGE_MAX_SEC_LBA48 },
Albert Lee18d6e9d2007-04-02 11:34:15 +08004465
David Milburnaf34d632015-07-13 11:48:23 -05004466 /*
4467 * Causes silent data corruption with higher max sects.
4468 * http://lkml.kernel.org/g/x49wpy40ysk.fsf@segfault.boston.devel.redhat.com
4469 */
4470 { "ST380013AS", "3.20", ATA_HORKAGE_MAX_SEC_1024 },
4471
Tejun Heo1488a1e2016-07-18 18:40:00 -04004472 /*
Tejun Heoe0edc8c2017-01-06 11:48:50 -05004473 * These devices time out with higher max sects.
Tejun Heo1488a1e2016-07-18 18:40:00 -04004474 * https://bugzilla.kernel.org/show_bug.cgi?id=121671
4475 */
Tejun Heoe0edc8c2017-01-06 11:48:50 -05004476 { "LITEON CX1-JB*-HP", NULL, ATA_HORKAGE_MAX_SEC_1024 },
Xinyu Lindb5ff902017-12-17 20:13:39 +08004477 { "LITEON EP1-*", NULL, ATA_HORKAGE_MAX_SEC_1024 },
Tejun Heo1488a1e2016-07-18 18:40:00 -04004478
Alan Cox6919a0a2006-10-27 19:08:46 -07004479 /* Devices we expect to fail diagnostics */
4480
4481 /* Devices where NCQ should be avoided */
4482 /* NCQ is slow */
Jeff Garzik2dcb4072007-10-19 06:42:56 -04004483 { "WDC WD740ADFD-00", NULL, ATA_HORKAGE_NONCQ },
Tejun Heo459ad682007-12-07 12:46:23 +09004484 { "WDC WD740ADFD-00NLR1", NULL, ATA_HORKAGE_NONCQ, },
Tejun Heo09125ea2007-02-28 15:21:23 +09004485 /* http://thread.gmane.org/gmane.linux.ide/14907 */
4486 { "FUJITSU MHT2060BH", NULL, ATA_HORKAGE_NONCQ },
Paul Rolland7acfaf32007-03-26 21:43:44 -08004487 /* NCQ is broken */
Jeff Garzik539cc7c2007-09-20 16:31:47 -04004488 { "Maxtor *", "BANC*", ATA_HORKAGE_NONCQ },
Alan Cox0e3dbc02007-09-20 15:22:47 +01004489 { "Maxtor 7V300F0", "VA111630", ATA_HORKAGE_NONCQ },
Paolo Ornatida6f0ec2007-10-04 11:06:56 +09004490 { "ST380817AS", "3.42", ATA_HORKAGE_NONCQ },
Tejun Heoe41bd3e2007-12-09 19:45:39 +09004491 { "ST3160023AS", "3.42", ATA_HORKAGE_NONCQ },
Lubomir Bulej5ccfca92008-12-22 11:35:22 +01004492 { "OCZ CORE_SSD", "02.10104", ATA_HORKAGE_NONCQ },
Jeff Garzik539cc7c2007-09-20 16:31:47 -04004493
Tejun Heoac70a962008-11-27 13:36:48 +09004494 /* Seagate NCQ + FLUSH CACHE firmware bug */
Mark Lord4d1f9082010-07-05 18:53:37 -04004495 { "ST31500341AS", "SD1[5-9]", ATA_HORKAGE_NONCQ |
Tejun Heoac70a962008-11-27 13:36:48 +09004496 ATA_HORKAGE_FIRMWARE_WARN },
Tejun Heod10d4912008-12-11 13:42:42 +09004497
Mark Lord4d1f9082010-07-05 18:53:37 -04004498 { "ST31000333AS", "SD1[5-9]", ATA_HORKAGE_NONCQ |
Tejun Heod10d4912008-12-11 13:42:42 +09004499 ATA_HORKAGE_FIRMWARE_WARN },
4500
Mark Lord4d1f9082010-07-05 18:53:37 -04004501 { "ST3640[36]23AS", "SD1[5-9]", ATA_HORKAGE_NONCQ |
Tejun Heod10d4912008-12-11 13:42:42 +09004502 ATA_HORKAGE_FIRMWARE_WARN },
4503
Mark Lord4d1f9082010-07-05 18:53:37 -04004504 { "ST3320[68]13AS", "SD1[5-9]", ATA_HORKAGE_NONCQ |
Tejun Heoac70a962008-11-27 13:36:48 +09004505 ATA_HORKAGE_FIRMWARE_WARN },
4506
Hans de Goede31f62642019-06-11 16:32:59 +02004507 /* drives which fail FPDMA_AA activation (some may freeze afterwards)
4508 the ST disks also have LPM issues */
Hans de Goede8756a252019-07-01 18:58:13 +02004509 { "ST1000LM024 HN-M101MBB", NULL, ATA_HORKAGE_BROKEN_FPDMA_AA |
Hans de Goede31f62642019-06-11 16:32:59 +02004510 ATA_HORKAGE_NOLPM, },
Aleksei Mamlin08c85d22015-07-01 13:48:30 +03004511 { "VB0250EAVER", "HPG7", ATA_HORKAGE_BROKEN_FPDMA_AA },
Michele Baldessari87809942013-11-25 19:00:14 +00004512
Robert Hancock36e337d2007-04-02 22:05:29 -06004513 /* Blacklist entries taken from Silicon Image 3124/3132
4514 Windows driver .inf file - also several Linux problem reports */
4515 { "HTS541060G9SA00", "MB3OC60D", ATA_HORKAGE_NONCQ, },
4516 { "HTS541080G9SA00", "MB4OC60D", ATA_HORKAGE_NONCQ, },
4517 { "HTS541010G9SA00", "MBZOC60D", ATA_HORKAGE_NONCQ, },
Alan Cox6919a0a2006-10-27 19:08:46 -07004518
Tejun Heo68b0ddb2010-04-05 10:51:26 +09004519 /* https://bugzilla.kernel.org/show_bug.cgi?id=15573 */
4520 { "C300-CTFDDAC128MAG", "0001", ATA_HORKAGE_NONCQ, },
4521
Tejun Heo322579d2018-05-08 14:21:56 -07004522 /* Some Sandisk SSDs lock up hard with NCQ enabled. Reported on
4523 SD7SN6S256G and SD8SN8U256G */
4524 { "SanDisk SD[78]SN*G", NULL, ATA_HORKAGE_NONCQ, },
4525
Tejun Heo16c55b02007-08-29 11:58:33 +09004526 /* devices which puke on READ_NATIVE_MAX */
4527 { "HDS724040KLSA80", "KFAOA20N", ATA_HORKAGE_BROKEN_HPA, },
4528 { "WDC WD3200JD-00KLB0", "WD-WCAMR1130137", ATA_HORKAGE_BROKEN_HPA },
4529 { "WDC WD2500JD-00HBB0", "WD-WMAL71490727", ATA_HORKAGE_BROKEN_HPA },
4530 { "MAXTOR 6L080L4", "A93.0500", ATA_HORKAGE_BROKEN_HPA },
Alan Cox6919a0a2006-10-27 19:08:46 -07004531
Tejun Heo7831387b2009-08-07 01:59:15 +09004532 /* this one allows HPA unlocking but fails IOs on the area */
4533 { "OCZ-VERTEX", "1.30", ATA_HORKAGE_BROKEN_HPA },
4534
Alan Cox93328e112007-09-29 04:06:48 -04004535 /* Devices which report 1 sector over size HPA */
4536 { "ST340823A", NULL, ATA_HORKAGE_HPA_SIZE, },
4537 { "ST320413A", NULL, ATA_HORKAGE_HPA_SIZE, },
Mikko Rapelib152fcd2008-02-19 01:41:25 +01004538 { "ST310211A", NULL, ATA_HORKAGE_HPA_SIZE, },
Alan Cox93328e112007-09-29 04:06:48 -04004539
Alan Cox6bbfd532007-11-05 22:58:58 +00004540 /* Devices which get the IVB wrong */
4541 { "QUANTUM FIREBALLlct10 05", "A03.0900", ATA_HORKAGE_IVB, },
Alan Coxa79067e2008-04-29 14:08:36 +01004542 /* Maybe we should just blacklist TSSTcorp... */
Mark Lord7da4c932010-07-01 18:18:12 -04004543 { "TSSTcorp CDDVDW SH-S202[HJN]", "SB0[01]", ATA_HORKAGE_IVB, },
Alan Cox6bbfd532007-11-05 22:58:58 +00004544
Jens Axboe9ce8e302008-08-27 15:23:18 +02004545 /* Devices that do not need bridging limits applied */
4546 { "MTRON MSP-SATA*", NULL, ATA_HORKAGE_BRIDGE_OK, },
Jeff Garzik04d0f1b82012-08-17 13:36:59 -04004547 { "BUFFALO HD-QSU2/R5", NULL, ATA_HORKAGE_BRIDGE_OK, },
Jens Axboe9ce8e302008-08-27 15:23:18 +02004548
Tejun Heo90627122009-01-29 20:31:36 +09004549 /* Devices which aren't very happy with higher link speeds */
4550 { "WD My Book", NULL, ATA_HORKAGE_1_5_GBPS, },
Daniel J Bluemanc5310772012-07-23 12:22:37 +08004551 { "Seagate FreeAgent GoFlex", NULL, ATA_HORKAGE_1_5_GBPS, },
Tejun Heo90627122009-01-29 20:31:36 +09004552
Tejun Heod0cb43b2009-07-09 09:27:50 +09004553 /*
4554 * Devices which choke on SETXFER. Applies only if both the
4555 * device and controller are SATA.
4556 */
Tejun Heocd691872011-06-22 12:13:10 +02004557 { "PIONEER DVD-RW DVRTD08", NULL, ATA_HORKAGE_NOSETXFER },
Vladimir LAVALLADE3a251792012-01-08 13:50:13 +01004558 { "PIONEER DVD-RW DVRTD08A", NULL, ATA_HORKAGE_NOSETXFER },
4559 { "PIONEER DVD-RW DVR-215", NULL, ATA_HORKAGE_NOSETXFER },
Tejun Heocd691872011-06-22 12:13:10 +02004560 { "PIONEER DVD-RW DVR-212D", NULL, ATA_HORKAGE_NOSETXFER },
4561 { "PIONEER DVD-RW DVR-216D", NULL, ATA_HORKAGE_NOSETXFER },
Tejun Heod0cb43b2009-07-09 09:27:50 +09004562
Kai-Heng Fengb17e57292018-02-18 22:17:09 +08004563 /* Crucial BX100 SSD 500GB has broken LPM support */
Hans de Goede3bf7b5d2018-03-19 16:33:59 +01004564 { "CT500BX100SSD1", NULL, ATA_HORKAGE_NOLPM },
Kai-Heng Fengb17e57292018-02-18 22:17:09 +08004565
Hans de Goeded418ff52018-03-19 16:34:00 +01004566 /* 512GB MX100 with MU01 firmware has both queued TRIM and LPM issues */
4567 { "Crucial_CT512MX100*", "MU01", ATA_HORKAGE_NO_NCQ_TRIM |
Hans de Goede9c7be59f2018-02-16 10:48:20 +01004568 ATA_HORKAGE_ZERO_AFTER_TRIM |
4569 ATA_HORKAGE_NOLPM, },
Hans de Goeded418ff52018-03-19 16:34:00 +01004570 /* 512GB MX100 with newer firmware has only LPM issues */
4571 { "Crucial_CT512MX100*", NULL, ATA_HORKAGE_ZERO_AFTER_TRIM |
4572 ATA_HORKAGE_NOLPM, },
Hans de Goede9c7be59f2018-02-16 10:48:20 +01004573
Hans de Goede62ac3f72018-03-19 16:33:58 +01004574 /* 480GB+ M500 SSDs have both queued TRIM and LPM issues */
4575 { "Crucial_CT480M500*", NULL, ATA_HORKAGE_NO_NCQ_TRIM |
4576 ATA_HORKAGE_ZERO_AFTER_TRIM |
4577 ATA_HORKAGE_NOLPM, },
4578 { "Crucial_CT960M500*", NULL, ATA_HORKAGE_NO_NCQ_TRIM |
4579 ATA_HORKAGE_ZERO_AFTER_TRIM |
4580 ATA_HORKAGE_NOLPM, },
4581
François Cami76936e92018-05-13 20:11:15 +02004582 /* These specific Samsung models/firmware-revs do not handle LPM well */
Hans de Goedeb5b4d3a2018-04-24 11:19:07 +02004583 { "SAMSUNG MZMPC128HBFU-000MV", "CXM14M1Q", ATA_HORKAGE_NOLPM, },
François Cami76936e92018-05-13 20:11:15 +02004584 { "SAMSUNG SSD PM830 mSATA *", "CXM13D1Q", ATA_HORKAGE_NOLPM, },
Diego Viola410b5c72018-11-12 17:22:52 -02004585 { "SAMSUNG MZ7TD256HAFV-000L9", NULL, ATA_HORKAGE_NOLPM, },
Hans de Goededd957492019-02-03 10:02:07 +01004586 { "SAMSUNG MZ7TE512HMHP-000L1", "EXT06L0Q", ATA_HORKAGE_NOLPM, },
Hans de Goedeb5b4d3a2018-04-24 11:19:07 +02004587
Marc Carinof78dea02013-12-16 18:15:53 -08004588 /* devices that don't properly handle queued TRIM commands */
Sudip Mukherjee136d7692018-05-19 22:29:36 +01004589 { "Micron_M500IT_*", "MU01", ATA_HORKAGE_NO_NCQ_TRIM |
4590 ATA_HORKAGE_ZERO_AFTER_TRIM, },
Martin K. Petersen243918b2015-06-18 14:50:18 -04004591 { "Micron_M500_*", NULL, ATA_HORKAGE_NO_NCQ_TRIM |
Martin K. Petersene61f7d12015-01-08 10:34:27 -05004592 ATA_HORKAGE_ZERO_AFTER_TRIM, },
Martin K. Petersenff7f53f2015-03-27 15:17:20 -04004593 { "Crucial_CT*M500*", NULL, ATA_HORKAGE_NO_NCQ_TRIM |
4594 ATA_HORKAGE_ZERO_AFTER_TRIM, },
Martin K. Petersen9051bd32015-07-15 21:03:23 -04004595 { "Micron_M5[15]0_*", "MU01", ATA_HORKAGE_NO_NCQ_TRIM |
Martin K. Petersenff7f53f2015-03-27 15:17:20 -04004596 ATA_HORKAGE_ZERO_AFTER_TRIM, },
4597 { "Crucial_CT*M550*", "MU01", ATA_HORKAGE_NO_NCQ_TRIM |
4598 ATA_HORKAGE_ZERO_AFTER_TRIM, },
4599 { "Crucial_CT*MX100*", "MU01", ATA_HORKAGE_NO_NCQ_TRIM |
4600 ATA_HORKAGE_ZERO_AFTER_TRIM, },
Ju Hyung Parkca6bfcb2018-03-11 02:28:35 +09004601 { "Samsung SSD 840*", NULL, ATA_HORKAGE_NO_NCQ_TRIM |
4602 ATA_HORKAGE_ZERO_AFTER_TRIM, },
4603 { "Samsung SSD 850*", NULL, ATA_HORKAGE_NO_NCQ_TRIM |
Martin K. Petersen6fc4d972015-03-27 15:17:21 -04004604 ATA_HORKAGE_ZERO_AFTER_TRIM, },
Guillermo A. Amaral7a7184b2015-08-25 23:29:13 -07004605 { "FCCT*M500*", NULL, ATA_HORKAGE_NO_NCQ_TRIM |
4606 ATA_HORKAGE_ZERO_AFTER_TRIM, },
Martin K. Petersene61f7d12015-01-08 10:34:27 -05004607
Arne Fitzenreitercda57b12015-07-15 13:54:37 +02004608 /* devices that don't properly handle TRIM commands */
4609 { "SuperSSpeed S238*", NULL, ATA_HORKAGE_NOTRIM, },
4610
Martin K. Petersene61f7d12015-01-08 10:34:27 -05004611 /*
4612 * As defined, the DRAT (Deterministic Read After Trim) and RZAT
4613 * (Return Zero After Trim) flags in the ATA Command Set are
4614 * unreliable in the sense that they only define what happens if
4615 * the device successfully executed the DSM TRIM command. TRIM
4616 * is only advisory, however, and the device is free to silently
4617 * ignore all or parts of the request.
4618 *
4619 * Whitelist drives that are known to reliably return zeroes
4620 * after TRIM.
4621 */
4622
4623 /*
4624 * The intel 510 drive has buggy DRAT/RZAT. Explicitly exclude
4625 * that model before whitelisting all other intel SSDs.
4626 */
4627 { "INTEL*SSDSC2MH*", NULL, 0, },
4628
Martin K. Petersenff7f53f2015-03-27 15:17:20 -04004629 { "Micron*", NULL, ATA_HORKAGE_ZERO_AFTER_TRIM, },
4630 { "Crucial*", NULL, ATA_HORKAGE_ZERO_AFTER_TRIM, },
Martin K. Petersene61f7d12015-01-08 10:34:27 -05004631 { "INTEL*SSD*", NULL, ATA_HORKAGE_ZERO_AFTER_TRIM, },
4632 { "SSD*INTEL*", NULL, ATA_HORKAGE_ZERO_AFTER_TRIM, },
4633 { "Samsung*SSD*", NULL, ATA_HORKAGE_ZERO_AFTER_TRIM, },
4634 { "SAMSUNG*SSD*", NULL, ATA_HORKAGE_ZERO_AFTER_TRIM, },
Juha-Matti Tillifd6f32f2018-12-02 12:47:08 +02004635 { "SAMSUNG*MZ7KM*", NULL, ATA_HORKAGE_ZERO_AFTER_TRIM, },
Martin K. Petersene61f7d12015-01-08 10:34:27 -05004636 { "ST[1248][0248]0[FH]*", NULL, ATA_HORKAGE_ZERO_AFTER_TRIM, },
Marc Carinof78dea02013-12-16 18:15:53 -08004637
Tejun Heoecd75ad2014-01-16 09:47:17 -05004638 /*
4639 * Some WD SATA-I drives spin up and down erratically when the link
4640 * is put into the slumber mode. We don't have full list of the
4641 * affected devices. Disable LPM if the device matches one of the
4642 * known prefixes and is SATA-1. As a side effect LPM partial is
4643 * lost too.
4644 *
4645 * https://bugzilla.kernel.org/show_bug.cgi?id=57211
4646 */
4647 { "WDC WD800JD-*", NULL, ATA_HORKAGE_WD_BROKEN_LPM },
4648 { "WDC WD1200JD-*", NULL, ATA_HORKAGE_WD_BROKEN_LPM },
4649 { "WDC WD1600JD-*", NULL, ATA_HORKAGE_WD_BROKEN_LPM },
4650 { "WDC WD2000JD-*", NULL, ATA_HORKAGE_WD_BROKEN_LPM },
4651 { "WDC WD2500JD-*", NULL, ATA_HORKAGE_WD_BROKEN_LPM },
4652 { "WDC WD3000JD-*", NULL, ATA_HORKAGE_WD_BROKEN_LPM },
4653 { "WDC WD3200JD-*", NULL, ATA_HORKAGE_WD_BROKEN_LPM },
4654
Alan Cox6919a0a2006-10-27 19:08:46 -07004655 /* End Marker */
4656 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004657};
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05004658
Tejun Heo75683fe2007-07-05 13:31:27 +09004659static unsigned long ata_dev_blacklisted(const struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004660{
Tejun Heo8bfa79f2007-01-02 20:19:40 +09004661 unsigned char model_num[ATA_ID_PROD_LEN + 1];
4662 unsigned char model_rev[ATA_ID_FW_REV_LEN + 1];
Alan Cox6919a0a2006-10-27 19:08:46 -07004663 const struct ata_blacklist_entry *ad = ata_device_blacklist;
Albert Lee3a778272006-06-22 13:00:25 +08004664
Tejun Heo8bfa79f2007-01-02 20:19:40 +09004665 ata_id_c_string(dev->id, model_num, ATA_ID_PROD, sizeof(model_num));
4666 ata_id_c_string(dev->id, model_rev, ATA_ID_FW_REV, sizeof(model_rev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004667
Alan Cox6919a0a2006-10-27 19:08:46 -07004668 while (ad->model_num) {
George Spelvin1c402792014-10-07 07:26:38 -04004669 if (glob_match(ad->model_num, model_num)) {
Alan Cox6919a0a2006-10-27 19:08:46 -07004670 if (ad->model_rev == NULL)
4671 return ad->horkage;
George Spelvin1c402792014-10-07 07:26:38 -04004672 if (glob_match(ad->model_rev, model_rev))
Alan Cox6919a0a2006-10-27 19:08:46 -07004673 return ad->horkage;
Alan Coxf4b15fe2006-03-22 15:54:04 +00004674 }
Alan Cox6919a0a2006-10-27 19:08:46 -07004675 ad++;
Alan Coxf4b15fe2006-03-22 15:54:04 +00004676 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004677 return 0;
4678}
4679
Alan Cox6919a0a2006-10-27 19:08:46 -07004680static int ata_dma_blacklisted(const struct ata_device *dev)
4681{
4682 /* We don't support polling DMA.
4683 * DMA blacklist those ATAPI devices with CDB-intr (and use PIO)
4684 * if the LLDD handles only interrupts in the HSM_ST_LAST state.
4685 */
Tejun Heo9af5c9c2007-08-06 18:36:22 +09004686 if ((dev->link->ap->flags & ATA_FLAG_PIO_POLLING) &&
Alan Cox6919a0a2006-10-27 19:08:46 -07004687 (dev->flags & ATA_DFLAG_CDB_INTR))
4688 return 1;
Tejun Heo75683fe2007-07-05 13:31:27 +09004689 return (dev->horkage & ATA_HORKAGE_NODMA) ? 1 : 0;
Alan Cox6919a0a2006-10-27 19:08:46 -07004690}
4691
Tejun Heoa6d5a512006-03-06 04:31:57 +09004692/**
Alan Cox6bbfd532007-11-05 22:58:58 +00004693 * ata_is_40wire - check drive side detection
4694 * @dev: device
4695 *
4696 * Perform drive side detection decoding, allowing for device vendors
4697 * who can't follow the documentation.
4698 */
4699
4700static int ata_is_40wire(struct ata_device *dev)
4701{
4702 if (dev->horkage & ATA_HORKAGE_IVB)
4703 return ata_drive_40wire_relaxed(dev->id);
4704 return ata_drive_40wire(dev->id);
4705}
4706
4707/**
Alan Cox15a55512008-03-28 14:33:46 -07004708 * cable_is_40wire - 40/80/SATA decider
4709 * @ap: port to consider
4710 *
4711 * This function encapsulates the policy for speed management
4712 * in one place. At the moment we don't cache the result but
4713 * there is a good case for setting ap->cbl to the result when
4714 * we are called with unknown cables (and figuring out if it
4715 * impacts hotplug at all).
4716 *
4717 * Return 1 if the cable appears to be 40 wire.
4718 */
4719
4720static int cable_is_40wire(struct ata_port *ap)
4721{
4722 struct ata_link *link;
4723 struct ata_device *dev;
4724
Tejun Heo4a9c7b32008-10-27 19:59:23 +09004725 /* If the controller thinks we are 40 wire, we are. */
Alan Cox15a55512008-03-28 14:33:46 -07004726 if (ap->cbl == ATA_CBL_PATA40)
4727 return 1;
Tejun Heo4a9c7b32008-10-27 19:59:23 +09004728
4729 /* If the controller thinks we are 80 wire, we are. */
Alan Cox15a55512008-03-28 14:33:46 -07004730 if (ap->cbl == ATA_CBL_PATA80 || ap->cbl == ATA_CBL_SATA)
4731 return 0;
Tejun Heo4a9c7b32008-10-27 19:59:23 +09004732
4733 /* If the system is known to be 40 wire short cable (eg
4734 * laptop), then we allow 80 wire modes even if the drive
4735 * isn't sure.
4736 */
Alan Coxf7920682008-04-08 16:37:50 +01004737 if (ap->cbl == ATA_CBL_PATA40_SHORT)
4738 return 0;
Alan Cox15a55512008-03-28 14:33:46 -07004739
Tejun Heo4a9c7b32008-10-27 19:59:23 +09004740 /* If the controller doesn't know, we scan.
4741 *
4742 * Note: We look for all 40 wire detects at this point. Any
4743 * 80 wire detect is taken to be 80 wire cable because
4744 * - in many setups only the one drive (slave if present) will
4745 * give a valid detect
4746 * - if you have a non detect capable drive you don't want it
4747 * to colour the choice
4748 */
Tejun Heo1eca4362008-11-03 20:03:17 +09004749 ata_for_each_link(link, ap, EDGE) {
4750 ata_for_each_dev(dev, link, ENABLED) {
4751 if (!ata_is_40wire(dev))
Alan Cox15a55512008-03-28 14:33:46 -07004752 return 0;
4753 }
4754 }
4755 return 1;
4756}
4757
4758/**
Tejun Heoa6d5a512006-03-06 04:31:57 +09004759 * ata_dev_xfermask - Compute supported xfermask of the given device
Tejun Heoa6d5a512006-03-06 04:31:57 +09004760 * @dev: Device to compute xfermask for
4761 *
Tejun Heoacf356b2006-03-24 14:07:50 +09004762 * Compute supported xfermask of @dev and store it in
4763 * dev->*_mask. This function is responsible for applying all
4764 * known limits including host controller limits, device
4765 * blacklist, etc...
Tejun Heoa6d5a512006-03-06 04:31:57 +09004766 *
4767 * LOCKING:
4768 * None.
Tejun Heoa6d5a512006-03-06 04:31:57 +09004769 */
Tejun Heo3373efd2006-05-15 20:57:53 +09004770static void ata_dev_xfermask(struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004771{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09004772 struct ata_link *link = dev->link;
4773 struct ata_port *ap = link->ap;
Jeff Garzikcca39742006-08-24 03:19:22 -04004774 struct ata_host *host = ap->host;
Tejun Heoa6d5a512006-03-06 04:31:57 +09004775 unsigned long xfer_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004776
Tejun Heo37deecb2006-08-10 16:59:07 +09004777 /* controller modes available */
Tejun Heo565083e2006-04-02 17:54:47 +09004778 xfer_mask = ata_pack_xfermask(ap->pio_mask,
4779 ap->mwdma_mask, ap->udma_mask);
4780
Robert Hancock8343f882007-03-06 02:37:51 -08004781 /* drive modes available */
Tejun Heo37deecb2006-08-10 16:59:07 +09004782 xfer_mask &= ata_pack_xfermask(dev->pio_mask,
4783 dev->mwdma_mask, dev->udma_mask);
4784 xfer_mask &= ata_id_xfermask(dev->id);
Tejun Heo565083e2006-04-02 17:54:47 +09004785
Alan Coxb352e572006-08-10 18:52:12 +01004786 /*
4787 * CFA Advanced TrueIDE timings are not allowed on a shared
4788 * cable
4789 */
4790 if (ata_dev_pair(dev)) {
4791 /* No PIO5 or PIO6 */
4792 xfer_mask &= ~(0x03 << (ATA_SHIFT_PIO + 5));
4793 /* No MWDMA3 or MWDMA 4 */
4794 xfer_mask &= ~(0x03 << (ATA_SHIFT_MWDMA + 3));
4795 }
4796
Tejun Heo37deecb2006-08-10 16:59:07 +09004797 if (ata_dma_blacklisted(dev)) {
4798 xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
Joe Perchesa9a79df2011-04-15 15:51:59 -07004799 ata_dev_warn(dev,
4800 "device is on DMA blacklist, disabling DMA\n");
Tejun Heo37deecb2006-08-10 16:59:07 +09004801 }
Tejun Heoa6d5a512006-03-06 04:31:57 +09004802
Petr Vandrovec14d66ab2007-03-08 10:12:12 +01004803 if ((host->flags & ATA_HOST_SIMPLEX) &&
Jeff Garzik2dcb4072007-10-19 06:42:56 -04004804 host->simplex_claimed && host->simplex_claimed != ap) {
Tejun Heo37deecb2006-08-10 16:59:07 +09004805 xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
Joe Perchesa9a79df2011-04-15 15:51:59 -07004806 ata_dev_warn(dev,
4807 "simplex DMA is claimed by other device, disabling DMA\n");
Alan Cox5444a6f2006-03-27 18:58:20 +01004808 }
Tejun Heo565083e2006-04-02 17:54:47 +09004809
Jeff Garzike4246752007-03-09 09:56:46 -05004810 if (ap->flags & ATA_FLAG_NO_IORDY)
4811 xfer_mask &= ata_pio_mask_no_iordy(dev);
4812
Alan Cox5444a6f2006-03-27 18:58:20 +01004813 if (ap->ops->mode_filter)
Alan Coxa76b62ca2007-03-09 09:34:07 -05004814 xfer_mask = ap->ops->mode_filter(dev, xfer_mask);
Alan Cox5444a6f2006-03-27 18:58:20 +01004815
Robert Hancock8343f882007-03-06 02:37:51 -08004816 /* Apply cable rule here. Don't apply it early because when
4817 * we handle hot plug the cable type can itself change.
4818 * Check this last so that we know if the transfer rate was
4819 * solely limited by the cable.
4820 * Unknown or 80 wire cables reported host side are checked
4821 * drive side as well. Cases where we know a 40wire cable
4822 * is used safely for 80 are not checked here.
4823 */
4824 if (xfer_mask & (0xF8 << ATA_SHIFT_UDMA))
4825 /* UDMA/44 or higher would be available */
Alan Cox15a55512008-03-28 14:33:46 -07004826 if (cable_is_40wire(ap)) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07004827 ata_dev_warn(dev,
4828 "limited to UDMA/33 due to 40-wire cable\n");
Robert Hancock8343f882007-03-06 02:37:51 -08004829 xfer_mask &= ~(0xF8 << ATA_SHIFT_UDMA);
4830 }
4831
Tejun Heo565083e2006-04-02 17:54:47 +09004832 ata_unpack_xfermask(xfer_mask, &dev->pio_mask,
4833 &dev->mwdma_mask, &dev->udma_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004834}
4835
Linus Torvalds1da177e2005-04-16 15:20:36 -07004836/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004837 * ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
Linus Torvalds1da177e2005-04-16 15:20:36 -07004838 * @dev: Device to which command will be sent
4839 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04004840 * Issue SET FEATURES - XFER MODE command to device @dev
4841 * on port @ap.
4842 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004843 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004844 * PCI/etc. bus probe sem.
Tejun Heo83206a22006-03-24 15:25:31 +09004845 *
4846 * RETURNS:
4847 * 0 on success, AC_ERR_* mask otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004848 */
4849
Tejun Heo3373efd2006-05-15 20:57:53 +09004850static unsigned int ata_dev_set_xfermode(struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004851{
Tejun Heoa0123702005-12-13 14:49:31 +09004852 struct ata_taskfile tf;
Tejun Heo83206a22006-03-24 15:25:31 +09004853 unsigned int err_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004854
4855 /* set up set-features taskfile */
4856 DPRINTK("set features - xfer mode\n");
4857
Tejun Heo464cf172007-05-27 15:10:40 +02004858 /* Some controllers and ATAPI devices show flaky interrupt
4859 * behavior after setting xfer mode. Use polling instead.
4860 */
Tejun Heo3373efd2006-05-15 20:57:53 +09004861 ata_tf_init(dev, &tf);
Tejun Heoa0123702005-12-13 14:49:31 +09004862 tf.command = ATA_CMD_SET_FEATURES;
4863 tf.feature = SETFEATURES_XFER;
Tejun Heo464cf172007-05-27 15:10:40 +02004864 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE | ATA_TFLAG_POLLING;
Tejun Heoa0123702005-12-13 14:49:31 +09004865 tf.protocol = ATA_PROT_NODATA;
Alan Coxb9f8ab22007-11-19 14:33:11 +00004866 /* If we are using IORDY we must send the mode setting command */
Jeff Garzik11b7bec2007-11-23 21:12:14 -05004867 if (ata_pio_need_iordy(dev))
4868 tf.nsect = dev->xfer_mode;
Alan Coxb9f8ab22007-11-19 14:33:11 +00004869 /* If the device has IORDY and the controller does not - turn it off */
4870 else if (ata_id_has_iordy(dev->id))
Jeff Garzik11b7bec2007-11-23 21:12:14 -05004871 tf.nsect = 0x01;
Alan Coxb9f8ab22007-11-19 14:33:11 +00004872 else /* In the ancient relic department - skip all of this */
4873 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004874
Mikulas Patockad531be22015-07-08 13:06:12 -04004875 /* On some disks, this command causes spin-up, so we need longer timeout */
4876 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 15000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004877
Tejun Heo83206a22006-03-24 15:25:31 +09004878 DPRINTK("EXIT, err_mask=%x\n", err_mask);
4879 return err_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004880}
Tejun Heo1152b262010-09-01 17:50:05 +02004881
Linus Torvalds1da177e2005-04-16 15:20:36 -07004882/**
Jeff Garzik218f3d32007-10-25 00:33:27 -04004883 * ata_dev_set_feature - Issue SET FEATURES - SATA FEATURES
Kristen Carlson Accardi9f45cbd2007-08-15 03:57:11 -04004884 * @dev: Device to which command will be sent
4885 * @enable: Whether to enable or disable the feature
Jeff Garzik218f3d32007-10-25 00:33:27 -04004886 * @feature: The sector count represents the feature to set
Kristen Carlson Accardi9f45cbd2007-08-15 03:57:11 -04004887 *
4888 * Issue SET FEATURES - SATA FEATURES command to device @dev
Jeff Garzik218f3d32007-10-25 00:33:27 -04004889 * on port @ap with sector count
Kristen Carlson Accardi9f45cbd2007-08-15 03:57:11 -04004890 *
4891 * LOCKING:
4892 * PCI/etc. bus probe sem.
4893 *
4894 * RETURNS:
4895 * 0 on success, AC_ERR_* mask otherwise.
4896 */
Tejun Heo1152b262010-09-01 17:50:05 +02004897unsigned int ata_dev_set_feature(struct ata_device *dev, u8 enable, u8 feature)
Kristen Carlson Accardi9f45cbd2007-08-15 03:57:11 -04004898{
4899 struct ata_taskfile tf;
4900 unsigned int err_mask;
Damien Le Moal974e0a42016-04-04 12:17:09 -04004901 unsigned long timeout = 0;
Kristen Carlson Accardi9f45cbd2007-08-15 03:57:11 -04004902
4903 /* set up set-features taskfile */
4904 DPRINTK("set features - SATA features\n");
4905
4906 ata_tf_init(dev, &tf);
4907 tf.command = ATA_CMD_SET_FEATURES;
4908 tf.feature = enable;
4909 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
4910 tf.protocol = ATA_PROT_NODATA;
Jeff Garzik218f3d32007-10-25 00:33:27 -04004911 tf.nsect = feature;
Kristen Carlson Accardi9f45cbd2007-08-15 03:57:11 -04004912
Damien Le Moal974e0a42016-04-04 12:17:09 -04004913 if (enable == SETFEATURES_SPINUP)
4914 timeout = ata_probe_timeout ?
4915 ata_probe_timeout * 1000 : SETFEATURES_SPINUP_TIMEOUT;
4916 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, timeout);
Kristen Carlson Accardi9f45cbd2007-08-15 03:57:11 -04004917
4918 DPRINTK("EXIT, err_mask=%x\n", err_mask);
4919 return err_mask;
4920}
Jeff Garzik633de4c2012-09-13 01:12:29 -04004921EXPORT_SYMBOL_GPL(ata_dev_set_feature);
Kristen Carlson Accardi9f45cbd2007-08-15 03:57:11 -04004922
4923/**
Albert Lee8bf62ece2005-05-12 15:29:42 -04004924 * ata_dev_init_params - Issue INIT DEV PARAMS command
Albert Lee8bf62ece2005-05-12 15:29:42 -04004925 * @dev: Device to which command will be sent
Randy Dunlape2a7f772006-05-18 10:50:18 -07004926 * @heads: Number of heads (taskfile parameter)
4927 * @sectors: Number of sectors (taskfile parameter)
Albert Lee8bf62ece2005-05-12 15:29:42 -04004928 *
4929 * LOCKING:
Tejun Heo6aff8f12006-02-15 18:24:09 +09004930 * Kernel thread context (may sleep)
4931 *
4932 * RETURNS:
4933 * 0 on success, AC_ERR_* mask otherwise.
Albert Lee8bf62ece2005-05-12 15:29:42 -04004934 */
Tejun Heo3373efd2006-05-15 20:57:53 +09004935static unsigned int ata_dev_init_params(struct ata_device *dev,
4936 u16 heads, u16 sectors)
Albert Lee8bf62ece2005-05-12 15:29:42 -04004937{
Tejun Heoa0123702005-12-13 14:49:31 +09004938 struct ata_taskfile tf;
Tejun Heo6aff8f12006-02-15 18:24:09 +09004939 unsigned int err_mask;
Albert Lee8bf62ece2005-05-12 15:29:42 -04004940
4941 /* Number of sectors per track 1-255. Number of heads 1-16 */
4942 if (sectors < 1 || sectors > 255 || heads < 1 || heads > 16)
Albert Lee00b6f5e2006-03-27 16:39:18 +08004943 return AC_ERR_INVALID;
Albert Lee8bf62ece2005-05-12 15:29:42 -04004944
4945 /* set up init dev params taskfile */
4946 DPRINTK("init dev params \n");
4947
Tejun Heo3373efd2006-05-15 20:57:53 +09004948 ata_tf_init(dev, &tf);
Tejun Heoa0123702005-12-13 14:49:31 +09004949 tf.command = ATA_CMD_INIT_DEV_PARAMS;
4950 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
4951 tf.protocol = ATA_PROT_NODATA;
4952 tf.nsect = sectors;
4953 tf.device |= (heads - 1) & 0x0f; /* max head = num. of heads - 1 */
Albert Lee8bf62ece2005-05-12 15:29:42 -04004954
Tejun Heo2b789102007-10-09 15:05:44 +09004955 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
Alan Cox18b24662007-08-08 14:28:49 +01004956 /* A clean abort indicates an original or just out of spec drive
4957 and we should continue as we issue the setup based on the
4958 drive reported working geometry */
4959 if (err_mask == AC_ERR_DEV && (tf.feature & ATA_ABORTED))
4960 err_mask = 0;
Albert Lee8bf62ece2005-05-12 15:29:42 -04004961
Tejun Heo6aff8f12006-02-15 18:24:09 +09004962 DPRINTK("EXIT, err_mask=%x\n", err_mask);
4963 return err_mask;
Albert Lee8bf62ece2005-05-12 15:29:42 -04004964}
4965
4966/**
Tejun Heo5895ef92008-06-17 12:36:26 +09004967 * atapi_check_dma - Check whether ATAPI DMA can be supported
Linus Torvalds1da177e2005-04-16 15:20:36 -07004968 * @qc: Metadata associated with taskfile to check
4969 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04004970 * Allow low-level driver to filter ATA PACKET commands, returning
4971 * a status indicating whether or not it is OK to use DMA for the
4972 * supplied PACKET command.
4973 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004974 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04004975 * spin_lock_irqsave(host lock)
Jeff Garzik0cba6322005-05-30 19:49:12 -04004976 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004977 * RETURNS: 0 when ATAPI DMA can be used
4978 * nonzero otherwise
4979 */
Tejun Heo5895ef92008-06-17 12:36:26 +09004980int atapi_check_dma(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004981{
4982 struct ata_port *ap = qc->ap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004983
Tejun Heob9a41972007-06-27 02:48:43 +09004984 /* Don't allow DMA if it isn't multiple of 16 bytes. Quite a
4985 * few ATAPI devices choke on such DMA requests.
4986 */
Tejun Heo6a87e422008-11-03 19:01:09 +09004987 if (!(qc->dev->horkage & ATA_HORKAGE_ATAPI_MOD16_DMA) &&
4988 unlikely(qc->nbytes & 15))
Tejun Heob9a41972007-06-27 02:48:43 +09004989 return 1;
Albert Lee6f23a312007-04-02 11:39:25 +08004990
Linus Torvalds1da177e2005-04-16 15:20:36 -07004991 if (ap->ops->check_atapi_dma)
Tejun Heob9a41972007-06-27 02:48:43 +09004992 return ap->ops->check_atapi_dma(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004993
Tejun Heob9a41972007-06-27 02:48:43 +09004994 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004995}
Tejun Heob9a41972007-06-27 02:48:43 +09004996
Linus Torvalds1da177e2005-04-16 15:20:36 -07004997/**
Tejun Heo31cc23b2007-09-23 13:14:12 +09004998 * ata_std_qc_defer - Check whether a qc needs to be deferred
4999 * @qc: ATA command in question
5000 *
5001 * Non-NCQ commands cannot run with any other command, NCQ or
5002 * not. As upper layer only knows the queue depth, we are
5003 * responsible for maintaining exclusion. This function checks
5004 * whether a new command @qc can be issued.
5005 *
5006 * LOCKING:
5007 * spin_lock_irqsave(host lock)
5008 *
5009 * RETURNS:
5010 * ATA_DEFER_* if deferring is needed, 0 otherwise.
5011 */
5012int ata_std_qc_defer(struct ata_queued_cmd *qc)
5013{
5014 struct ata_link *link = qc->dev->link;
5015
Hannes Reinecke179b3102016-07-14 09:05:43 +09005016 if (ata_is_ncq(qc->tf.protocol)) {
Tejun Heo31cc23b2007-09-23 13:14:12 +09005017 if (!ata_tag_valid(link->active_tag))
5018 return 0;
5019 } else {
5020 if (!ata_tag_valid(link->active_tag) && !link->sactive)
5021 return 0;
5022 }
5023
5024 return ATA_DEFER_LINK;
5025}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01005026EXPORT_SYMBOL_GPL(ata_std_qc_defer);
Tejun Heo31cc23b2007-09-23 13:14:12 +09005027
Jiri Slaby95364f32019-10-31 10:59:45 +01005028enum ata_completion_errors ata_noop_qc_prep(struct ata_queued_cmd *qc)
5029{
5030 return AC_ERR_OK;
5031}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01005032EXPORT_SYMBOL_GPL(ata_noop_qc_prep);
Brian Kinge46834c2006-03-17 17:04:03 -06005033
Jeff Garzik0cba6322005-05-30 19:49:12 -04005034/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04005035 * ata_sg_init - Associate command with scatter-gather table.
5036 * @qc: Command to be associated
5037 * @sg: Scatter-gather table.
5038 * @n_elem: Number of elements in s/g table.
5039 *
5040 * Initialize the data-related elements of queued_cmd @qc
5041 * to point to a scatter-gather table @sg, containing @n_elem
5042 * elements.
5043 *
5044 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04005045 * spin_lock_irqsave(host lock)
Jeff Garzik0cba6322005-05-30 19:49:12 -04005046 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005047void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
5048 unsigned int n_elem)
5049{
Tejun Heoff2aeb12007-12-05 16:43:11 +09005050 qc->sg = sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005051 qc->n_elem = n_elem;
Tejun Heoff2aeb12007-12-05 16:43:11 +09005052 qc->cursg = qc->sg;
5053}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01005054EXPORT_SYMBOL_GPL(ata_sg_init);
Tejun Heoff2aeb12007-12-05 16:43:11 +09005055
Geert Uytterhoeven2874d5e2017-01-03 19:09:49 +01005056#ifdef CONFIG_HAS_DMA
5057
5058/**
5059 * ata_sg_clean - Unmap DMA memory associated with command
5060 * @qc: Command containing DMA memory to be released
5061 *
5062 * Unmap all mapped DMA memory associated with this command.
5063 *
5064 * LOCKING:
5065 * spin_lock_irqsave(host lock)
5066 */
Jason Yanaf27e012017-03-10 10:05:40 +08005067static void ata_sg_clean(struct ata_queued_cmd *qc)
Geert Uytterhoeven2874d5e2017-01-03 19:09:49 +01005068{
5069 struct ata_port *ap = qc->ap;
5070 struct scatterlist *sg = qc->sg;
5071 int dir = qc->dma_dir;
5072
5073 WARN_ON_ONCE(sg == NULL);
5074
5075 VPRINTK("unmapping %u sg elements\n", qc->n_elem);
5076
5077 if (qc->n_elem)
5078 dma_unmap_sg(ap->dev, sg, qc->orig_n_elem, dir);
5079
5080 qc->flags &= ~ATA_QCFLAG_DMAMAP;
5081 qc->sg = NULL;
5082}
5083
Linus Torvalds1da177e2005-04-16 15:20:36 -07005084/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04005085 * ata_sg_setup - DMA-map the scatter-gather table associated with a command.
5086 * @qc: Command with scatter-gather table to be mapped.
5087 *
5088 * DMA-map the scatter-gather table associated with queued_cmd @qc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005089 *
5090 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04005091 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005092 *
5093 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04005094 * Zero on success, negative on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005095 *
5096 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005097static int ata_sg_setup(struct ata_queued_cmd *qc)
5098{
5099 struct ata_port *ap = qc->ap;
James Bottomleydde20202008-02-19 11:36:56 +01005100 unsigned int n_elem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005101
Tejun Heo44877b42007-02-21 01:06:51 +09005102 VPRINTK("ENTER, ata%u\n", ap->print_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005103
James Bottomleydde20202008-02-19 11:36:56 +01005104 n_elem = dma_map_sg(ap->dev, qc->sg, qc->n_elem, qc->dma_dir);
5105 if (n_elem < 1)
5106 return -1;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04005107
Paul Menzelbb376672018-07-08 09:18:21 +02005108 VPRINTK("%d sg elements mapped\n", n_elem);
FUJITA Tomonori58256272009-02-27 17:35:43 +09005109 qc->orig_n_elem = qc->n_elem;
James Bottomleydde20202008-02-19 11:36:56 +01005110 qc->n_elem = n_elem;
Tejun Heof92a2632007-12-05 16:43:10 +09005111 qc->flags |= ATA_QCFLAG_DMAMAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005112
5113 return 0;
5114}
5115
Geert Uytterhoeven2874d5e2017-01-03 19:09:49 +01005116#else /* !CONFIG_HAS_DMA */
5117
5118static inline void ata_sg_clean(struct ata_queued_cmd *qc) {}
5119static inline int ata_sg_setup(struct ata_queued_cmd *qc) { return -1; }
5120
5121#endif /* !CONFIG_HAS_DMA */
5122
Linus Torvalds1da177e2005-04-16 15:20:36 -07005123/**
Randy Dunlapc893a3a2006-01-28 13:15:32 -05005124 * swap_buf_le16 - swap halves of 16-bit words in place
Edward Falk0baab862005-06-02 18:17:13 -04005125 * @buf: Buffer to swap
5126 * @buf_words: Number of 16-bit words in buffer.
5127 *
5128 * Swap halves of 16-bit words if needed to convert from
5129 * little-endian byte order to native cpu byte order, or
5130 * vice-versa.
5131 *
5132 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04005133 * Inherited from caller.
Edward Falk0baab862005-06-02 18:17:13 -04005134 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005135void swap_buf_le16(u16 *buf, unsigned int buf_words)
5136{
5137#ifdef __BIG_ENDIAN
5138 unsigned int i;
5139
5140 for (i = 0; i < buf_words; i++)
5141 buf[i] = le16_to_cpu(buf[i]);
5142#endif /* __BIG_ENDIAN */
5143}
5144
Albert Lee6ae4cfb2005-08-12 14:15:34 +08005145/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07005146 * ata_qc_new_init - Request an available ATA command, and initialize it
Linus Torvalds1da177e2005-04-16 15:20:36 -07005147 * @dev: Device from whom we request an available command structure
Randy Dunlap38755e82015-08-09 18:37:36 -07005148 * @tag: tag
Linus Torvalds1da177e2005-04-16 15:20:36 -07005149 *
5150 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04005151 * None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005152 */
5153
Shaohua Li98bd4be2015-01-23 19:52:07 -08005154struct ata_queued_cmd *ata_qc_new_init(struct ata_device *dev, int tag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005155{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09005156 struct ata_port *ap = dev->link->ap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005157 struct ata_queued_cmd *qc;
5158
Shaohua Li98bd4be2015-01-23 19:52:07 -08005159 /* no command while frozen */
5160 if (unlikely(ap->pflags & ATA_PFLAG_FROZEN))
5161 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005162
Shaohua Li98bd4be2015-01-23 19:52:07 -08005163 /* libsas case */
Shaohua Li5067c042015-03-12 10:32:18 -07005164 if (ap->flags & ATA_FLAG_SAS_HOST) {
Shaohua Li98bd4be2015-01-23 19:52:07 -08005165 tag = ata_sas_allocate_tag(ap);
5166 if (tag < 0)
5167 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005168 }
5169
Shaohua Li98bd4be2015-01-23 19:52:07 -08005170 qc = __ata_qc_from_tag(ap, tag);
Jens Axboe5ac40792018-05-11 12:51:03 -06005171 qc->tag = qc->hw_tag = tag;
Shaohua Li98bd4be2015-01-23 19:52:07 -08005172 qc->scsicmd = NULL;
5173 qc->ap = ap;
5174 qc->dev = dev;
5175
5176 ata_qc_reinit(qc);
5177
Linus Torvalds1da177e2005-04-16 15:20:36 -07005178 return qc;
5179}
5180
Tejun Heo8a8bc222008-11-10 14:48:21 +09005181/**
5182 * ata_qc_free - free unused ata_queued_cmd
5183 * @qc: Command to complete
5184 *
5185 * Designed to free unused ata_queued_cmd object
5186 * in case something prevents using it.
5187 *
5188 * LOCKING:
5189 * spin_lock_irqsave(host lock)
5190 */
5191void ata_qc_free(struct ata_queued_cmd *qc)
5192{
Julia Lawalla1104012009-10-17 08:41:47 +02005193 struct ata_port *ap;
Tejun Heo8a8bc222008-11-10 14:48:21 +09005194 unsigned int tag;
5195
Tejun Heoefcb3cf2009-01-09 19:19:14 +09005196 WARN_ON_ONCE(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
Julia Lawalla1104012009-10-17 08:41:47 +02005197 ap = qc->ap;
Tejun Heo8a8bc222008-11-10 14:48:21 +09005198
5199 qc->flags = 0;
5200 tag = qc->tag;
Jens Axboe28361c42018-05-11 12:51:09 -06005201 if (ata_tag_valid(tag)) {
Tejun Heo8a8bc222008-11-10 14:48:21 +09005202 qc->tag = ATA_TAG_POISON;
Shaohua Li5067c042015-03-12 10:32:18 -07005203 if (ap->flags & ATA_FLAG_SAS_HOST)
Shaohua Li98bd4be2015-01-23 19:52:07 -08005204 ata_sas_free_tag(tag, ap);
Tejun Heo8a8bc222008-11-10 14:48:21 +09005205 }
5206}
5207
Tejun Heo76014422006-02-11 15:13:49 +09005208void __ata_qc_complete(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005209{
Julia Lawalla1104012009-10-17 08:41:47 +02005210 struct ata_port *ap;
5211 struct ata_link *link;
Tejun Heodedaf2b2006-05-15 21:03:43 +09005212
Tejun Heoefcb3cf2009-01-09 19:19:14 +09005213 WARN_ON_ONCE(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
5214 WARN_ON_ONCE(!(qc->flags & ATA_QCFLAG_ACTIVE));
Julia Lawalla1104012009-10-17 08:41:47 +02005215 ap = qc->ap;
5216 link = qc->dev->link;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005217
5218 if (likely(qc->flags & ATA_QCFLAG_DMAMAP))
5219 ata_sg_clean(qc);
5220
Tejun Heo7401abf2006-05-15 20:57:32 +09005221 /* command should be marked inactive atomically with qc completion */
Hannes Reinecke179b3102016-07-14 09:05:43 +09005222 if (ata_is_ncq(qc->tf.protocol)) {
Jens Axboe4e5b6262018-05-11 12:51:04 -06005223 link->sactive &= ~(1 << qc->hw_tag);
Tejun Heoda917d62007-09-23 13:14:12 +09005224 if (!link->sactive)
5225 ap->nr_active_links--;
5226 } else {
Tejun Heo9af5c9c2007-08-06 18:36:22 +09005227 link->active_tag = ATA_TAG_POISON;
Tejun Heoda917d62007-09-23 13:14:12 +09005228 ap->nr_active_links--;
5229 }
5230
5231 /* clear exclusive status */
5232 if (unlikely(qc->flags & ATA_QCFLAG_CLEAR_EXCL &&
5233 ap->excl_link == link))
5234 ap->excl_link = NULL;
Tejun Heo7401abf2006-05-15 20:57:32 +09005235
Albert Lee3f3791d2005-08-16 14:25:38 +08005236 /* atapi: mark qc as inactive to prevent the interrupt handler
5237 * from completing the command twice later, before the error handler
5238 * is called. (when rc != 0 and atapi request sense is needed)
5239 */
5240 qc->flags &= ~ATA_QCFLAG_ACTIVE;
Jens Axboee3ed89392018-05-11 12:51:05 -06005241 ap->qc_active &= ~(1ULL << qc->tag);
Albert Lee3f3791d2005-08-16 14:25:38 +08005242
Linus Torvalds1da177e2005-04-16 15:20:36 -07005243 /* call completion callback */
Tejun Heo77853bf2006-01-23 13:09:36 +09005244 qc->complete_fn(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005245}
5246
Tejun Heo39599a52006-11-14 22:37:35 +09005247static void fill_result_tf(struct ata_queued_cmd *qc)
5248{
5249 struct ata_port *ap = qc->ap;
5250
Tejun Heo39599a52006-11-14 22:37:35 +09005251 qc->result_tf.flags = qc->tf.flags;
Tejun Heo22183bf2008-04-07 22:47:20 +09005252 ap->ops->qc_fill_rtf(qc);
Tejun Heo39599a52006-11-14 22:37:35 +09005253}
5254
Tejun Heo00115e02007-11-27 19:28:58 +09005255static void ata_verify_xfer(struct ata_queued_cmd *qc)
5256{
5257 struct ata_device *dev = qc->dev;
5258
Christoph Hellwigeb0effd2016-07-16 22:16:41 +09005259 if (!ata_is_data(qc->tf.protocol))
Tejun Heo00115e02007-11-27 19:28:58 +09005260 return;
5261
5262 if ((dev->mwdma_mask || dev->udma_mask) && ata_is_pio(qc->tf.protocol))
5263 return;
5264
5265 dev->flags &= ~ATA_DFLAG_DUBIOUS_XFER;
5266}
5267
Tejun Heof686bcb2006-05-15 20:58:05 +09005268/**
5269 * ata_qc_complete - Complete an active ATA command
5270 * @qc: Command to complete
Tejun Heof686bcb2006-05-15 20:58:05 +09005271 *
Tejun Heo1aadf5c2010-06-25 15:03:34 +02005272 * Indicate to the mid and upper layers that an ATA command has
5273 * completed, with either an ok or not-ok status.
5274 *
5275 * Refrain from calling this function multiple times when
5276 * successfully completing multiple NCQ commands.
5277 * ata_qc_complete_multiple() should be used instead, which will
5278 * properly update IRQ expect state.
Tejun Heof686bcb2006-05-15 20:58:05 +09005279 *
5280 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04005281 * spin_lock_irqsave(host lock)
Tejun Heof686bcb2006-05-15 20:58:05 +09005282 */
5283void ata_qc_complete(struct ata_queued_cmd *qc)
5284{
5285 struct ata_port *ap = qc->ap;
5286
Stephan Linzeb25cb92016-06-10 07:59:56 +02005287 /* Trigger the LED (if available) */
Linus Walleijd1ed7c52018-02-24 23:45:56 +01005288 ledtrig_disk_activity(!!(qc->tf.flags & ATA_TFLAG_WRITE));
Stephan Linzeb25cb92016-06-10 07:59:56 +02005289
Tejun Heof686bcb2006-05-15 20:58:05 +09005290 /* XXX: New EH and old EH use different mechanisms to
5291 * synchronize EH with regular execution path.
5292 *
5293 * In new EH, a failed qc is marked with ATA_QCFLAG_FAILED.
5294 * Normal execution path is responsible for not accessing a
5295 * failed qc. libata core enforces the rule by returning NULL
5296 * from ata_qc_from_tag() for failed qcs.
5297 *
5298 * Old EH depends on ata_qc_complete() nullifying completion
5299 * requests if ATA_QCFLAG_EH_SCHEDULED is set. Old EH does
5300 * not synchronize with interrupt handler. Only PIO task is
5301 * taken care of.
5302 */
5303 if (ap->ops->error_handler) {
Tejun Heo4dbfa392007-10-25 18:22:44 +09005304 struct ata_device *dev = qc->dev;
5305 struct ata_eh_info *ehi = &dev->link->eh_info;
5306
Tejun Heof686bcb2006-05-15 20:58:05 +09005307 if (unlikely(qc->err_mask))
5308 qc->flags |= ATA_QCFLAG_FAILED;
5309
Tejun Heof08dc1a2010-12-09 15:59:32 +01005310 /*
5311 * Finish internal commands without any further processing
5312 * and always with the result TF filled.
5313 */
5314 if (unlikely(ata_tag_internal(qc->tag))) {
Tejun Heof4b31db92009-10-16 13:00:51 +09005315 fill_result_tf(qc);
Hannes Reinecke255c03d2015-03-27 16:46:38 +01005316 trace_ata_qc_complete_internal(qc);
Tejun Heof08dc1a2010-12-09 15:59:32 +01005317 __ata_qc_complete(qc);
5318 return;
5319 }
Tejun Heof4b31db92009-10-16 13:00:51 +09005320
Tejun Heof08dc1a2010-12-09 15:59:32 +01005321 /*
5322 * Non-internal qc has failed. Fill the result TF and
5323 * summon EH.
5324 */
5325 if (unlikely(qc->flags & ATA_QCFLAG_FAILED)) {
5326 fill_result_tf(qc);
Hannes Reinecke255c03d2015-03-27 16:46:38 +01005327 trace_ata_qc_complete_failed(qc);
Tejun Heof08dc1a2010-12-09 15:59:32 +01005328 ata_qc_schedule_eh(qc);
Tejun Heof4b31db92009-10-16 13:00:51 +09005329 return;
Tejun Heof686bcb2006-05-15 20:58:05 +09005330 }
5331
Tejun Heo4dc738e2009-09-17 18:45:27 +09005332 WARN_ON_ONCE(ap->pflags & ATA_PFLAG_FROZEN);
5333
Tejun Heof686bcb2006-05-15 20:58:05 +09005334 /* read result TF if requested */
5335 if (qc->flags & ATA_QCFLAG_RESULT_TF)
Tejun Heo39599a52006-11-14 22:37:35 +09005336 fill_result_tf(qc);
Tejun Heof686bcb2006-05-15 20:58:05 +09005337
Hannes Reinecke255c03d2015-03-27 16:46:38 +01005338 trace_ata_qc_complete_done(qc);
Tejun Heo4dbfa392007-10-25 18:22:44 +09005339 /* Some commands need post-processing after successful
5340 * completion.
5341 */
5342 switch (qc->tf.command) {
5343 case ATA_CMD_SET_FEATURES:
5344 if (qc->tf.feature != SETFEATURES_WC_ON &&
Tom Yan0c127352016-07-12 21:37:02 +08005345 qc->tf.feature != SETFEATURES_WC_OFF &&
5346 qc->tf.feature != SETFEATURES_RA_ON &&
5347 qc->tf.feature != SETFEATURES_RA_OFF)
Tejun Heo4dbfa392007-10-25 18:22:44 +09005348 break;
5349 /* fall through */
5350 case ATA_CMD_INIT_DEV_PARAMS: /* CHS translation changed */
5351 case ATA_CMD_SET_MULTI: /* multi_count changed */
5352 /* revalidate device */
5353 ehi->dev_action[dev->devno] |= ATA_EH_REVALIDATE;
5354 ata_port_schedule_eh(ap);
5355 break;
Tejun Heo054a5fb2007-10-25 18:30:36 +09005356
5357 case ATA_CMD_SLEEP:
5358 dev->flags |= ATA_DFLAG_SLEEPING;
5359 break;
Tejun Heo4dbfa392007-10-25 18:22:44 +09005360 }
5361
Tejun Heo00115e02007-11-27 19:28:58 +09005362 if (unlikely(dev->flags & ATA_DFLAG_DUBIOUS_XFER))
5363 ata_verify_xfer(qc);
5364
Tejun Heof686bcb2006-05-15 20:58:05 +09005365 __ata_qc_complete(qc);
5366 } else {
5367 if (qc->flags & ATA_QCFLAG_EH_SCHEDULED)
5368 return;
5369
5370 /* read result TF if failed or requested */
5371 if (qc->err_mask || qc->flags & ATA_QCFLAG_RESULT_TF)
Tejun Heo39599a52006-11-14 22:37:35 +09005372 fill_result_tf(qc);
Tejun Heof686bcb2006-05-15 20:58:05 +09005373
5374 __ata_qc_complete(qc);
5375 }
5376}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01005377EXPORT_SYMBOL_GPL(ata_qc_complete);
Tejun Heof686bcb2006-05-15 20:58:05 +09005378
Tejun Heodedaf2b2006-05-15 21:03:43 +09005379/**
Sascha Hauer8385d752019-12-13 09:04:08 +01005380 * ata_qc_get_active - get bitmask of active qcs
5381 * @ap: port in question
5382 *
5383 * LOCKING:
5384 * spin_lock_irqsave(host lock)
5385 *
5386 * RETURNS:
5387 * Bitmask of active qcs
5388 */
5389u64 ata_qc_get_active(struct ata_port *ap)
5390{
5391 u64 qc_active = ap->qc_active;
5392
5393 /* ATA_TAG_INTERNAL is sent to hw as tag 0 */
5394 if (qc_active & (1ULL << ATA_TAG_INTERNAL)) {
5395 qc_active |= (1 << 0);
5396 qc_active &= ~(1ULL << ATA_TAG_INTERNAL);
5397 }
5398
5399 return qc_active;
5400}
5401EXPORT_SYMBOL_GPL(ata_qc_get_active);
5402
5403/**
Tejun Heodedaf2b2006-05-15 21:03:43 +09005404 * ata_qc_complete_multiple - Complete multiple qcs successfully
5405 * @ap: port in question
5406 * @qc_active: new qc_active mask
Tejun Heodedaf2b2006-05-15 21:03:43 +09005407 *
5408 * Complete in-flight commands. This functions is meant to be
5409 * called from low-level driver's interrupt routine to complete
5410 * requests normally. ap->qc_active and @qc_active is compared
5411 * and commands are completed accordingly.
5412 *
Tejun Heo1aadf5c2010-06-25 15:03:34 +02005413 * Always use this function when completing multiple NCQ commands
5414 * from IRQ handlers instead of calling ata_qc_complete()
5415 * multiple times to keep IRQ expect status properly in sync.
5416 *
Tejun Heodedaf2b2006-05-15 21:03:43 +09005417 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04005418 * spin_lock_irqsave(host lock)
Tejun Heodedaf2b2006-05-15 21:03:43 +09005419 *
5420 * RETURNS:
5421 * Number of completed commands on success, -errno otherwise.
5422 */
Jens Axboee3ed89392018-05-11 12:51:05 -06005423int ata_qc_complete_multiple(struct ata_port *ap, u64 qc_active)
Tejun Heodedaf2b2006-05-15 21:03:43 +09005424{
Jens Axboe7ce5c8c2018-09-20 08:30:55 -06005425 u64 done_mask, ap_qc_active = ap->qc_active;
Tejun Heodedaf2b2006-05-15 21:03:43 +09005426 int nr_done = 0;
Tejun Heodedaf2b2006-05-15 21:03:43 +09005427
Jens Axboe7ce5c8c2018-09-20 08:30:55 -06005428 /*
5429 * If the internal tag is set on ap->qc_active, then we care about
5430 * bit0 on the passed in qc_active mask. Move that bit up to match
5431 * the internal tag.
5432 */
5433 if (ap_qc_active & (1ULL << ATA_TAG_INTERNAL)) {
5434 qc_active |= (qc_active & 0x01) << ATA_TAG_INTERNAL;
5435 qc_active ^= qc_active & 0x01;
5436 }
5437
5438 done_mask = ap_qc_active ^ qc_active;
Tejun Heodedaf2b2006-05-15 21:03:43 +09005439
5440 if (unlikely(done_mask & qc_active)) {
Jens Axboee3ed89392018-05-11 12:51:05 -06005441 ata_port_err(ap, "illegal qc_active transition (%08llx->%08llx)\n",
Joe Perchesa9a79df2011-04-15 15:51:59 -07005442 ap->qc_active, qc_active);
Tejun Heodedaf2b2006-05-15 21:03:43 +09005443 return -EINVAL;
5444 }
5445
Jens Axboe43768182009-05-20 09:44:39 +02005446 while (done_mask) {
Tejun Heodedaf2b2006-05-15 21:03:43 +09005447 struct ata_queued_cmd *qc;
Jens Axboee3ed89392018-05-11 12:51:05 -06005448 unsigned int tag = __ffs64(done_mask);
Tejun Heodedaf2b2006-05-15 21:03:43 +09005449
Jens Axboe43768182009-05-20 09:44:39 +02005450 qc = ata_qc_from_tag(ap, tag);
5451 if (qc) {
Tejun Heodedaf2b2006-05-15 21:03:43 +09005452 ata_qc_complete(qc);
5453 nr_done++;
5454 }
Jens Axboee3ed89392018-05-11 12:51:05 -06005455 done_mask &= ~(1ULL << tag);
Tejun Heodedaf2b2006-05-15 21:03:43 +09005456 }
5457
5458 return nr_done;
5459}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01005460EXPORT_SYMBOL_GPL(ata_qc_complete_multiple);
Tejun Heodedaf2b2006-05-15 21:03:43 +09005461
Linus Torvalds1da177e2005-04-16 15:20:36 -07005462/**
5463 * ata_qc_issue - issue taskfile to device
5464 * @qc: command to issue to device
5465 *
5466 * Prepare an ATA command to submission to device.
5467 * This includes mapping the data into a DMA-able
5468 * area, filling in the S/G table, and finally
5469 * writing the taskfile to hardware, starting the command.
5470 *
5471 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04005472 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005473 */
Tejun Heo8e0e6942006-03-31 20:41:11 +09005474void ata_qc_issue(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005475{
5476 struct ata_port *ap = qc->ap;
Tejun Heo9af5c9c2007-08-06 18:36:22 +09005477 struct ata_link *link = qc->dev->link;
Tejun Heo405e66b2007-11-27 19:28:53 +09005478 u8 prot = qc->tf.protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005479
Tejun Heodedaf2b2006-05-15 21:03:43 +09005480 /* Make sure only one non-NCQ command is outstanding. The
5481 * check is skipped for old EH because it reuses active qc to
5482 * request ATAPI sense.
5483 */
Tejun Heoefcb3cf2009-01-09 19:19:14 +09005484 WARN_ON_ONCE(ap->ops->error_handler && ata_tag_valid(link->active_tag));
Tejun Heodedaf2b2006-05-15 21:03:43 +09005485
Tejun Heo1973a022007-12-05 10:36:13 +09005486 if (ata_is_ncq(prot)) {
Jens Axboe4e5b6262018-05-11 12:51:04 -06005487 WARN_ON_ONCE(link->sactive & (1 << qc->hw_tag));
Tejun Heoda917d62007-09-23 13:14:12 +09005488
5489 if (!link->sactive)
5490 ap->nr_active_links++;
Jens Axboe4e5b6262018-05-11 12:51:04 -06005491 link->sactive |= 1 << qc->hw_tag;
Tejun Heodedaf2b2006-05-15 21:03:43 +09005492 } else {
Tejun Heoefcb3cf2009-01-09 19:19:14 +09005493 WARN_ON_ONCE(link->sactive);
Tejun Heoda917d62007-09-23 13:14:12 +09005494
5495 ap->nr_active_links++;
Tejun Heo9af5c9c2007-08-06 18:36:22 +09005496 link->active_tag = qc->tag;
Tejun Heodedaf2b2006-05-15 21:03:43 +09005497 }
5498
Tejun Heoe4a70e72006-03-31 20:36:47 +09005499 qc->flags |= ATA_QCFLAG_ACTIVE;
Jens Axboee3ed89392018-05-11 12:51:05 -06005500 ap->qc_active |= 1ULL << qc->tag;
Tejun Heoe4a70e72006-03-31 20:36:47 +09005501
Tejun Heo60f5d6e2010-08-23 11:27:27 +02005502 /*
5503 * We guarantee to LLDs that they will have at least one
Tejun Heof92a2632007-12-05 16:43:10 +09005504 * non-zero sg if the command is a data command.
5505 */
Eric Biggers9173e5e2018-02-03 20:33:27 -08005506 if (ata_is_data(prot) && (!qc->sg || !qc->n_elem || !qc->nbytes))
Tejun Heo60f5d6e2010-08-23 11:27:27 +02005507 goto sys_err;
Tejun Heof92a2632007-12-05 16:43:10 +09005508
Tejun Heo405e66b2007-11-27 19:28:53 +09005509 if (ata_is_dma(prot) || (ata_is_pio(prot) &&
Tejun Heof92a2632007-12-05 16:43:10 +09005510 (ap->flags & ATA_FLAG_PIO_DMA)))
Tejun Heo001102d2007-12-05 16:43:09 +09005511 if (ata_sg_setup(qc))
Tejun Heo60f5d6e2010-08-23 11:27:27 +02005512 goto sys_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005513
Tejun Heocf480622008-01-24 00:05:14 +09005514 /* if device is sleeping, schedule reset and abort the link */
Tejun Heo054a5fb2007-10-25 18:30:36 +09005515 if (unlikely(qc->dev->flags & ATA_DFLAG_SLEEPING)) {
Tejun Heocf480622008-01-24 00:05:14 +09005516 link->eh_info.action |= ATA_EH_RESET;
Tejun Heo054a5fb2007-10-25 18:30:36 +09005517 ata_ehi_push_desc(&link->eh_info, "waking up from sleep");
5518 ata_link_abort(link);
5519 return;
5520 }
5521
Jiri Slaby95364f32019-10-31 10:59:45 +01005522 qc->err_mask |= ap->ops->qc_prep(qc);
5523 if (unlikely(qc->err_mask))
5524 goto err;
Hannes Reinecke255c03d2015-03-27 16:46:38 +01005525 trace_ata_qc_issue(qc);
Tejun Heo8e0e6942006-03-31 20:41:11 +09005526 qc->err_mask |= ap->ops->qc_issue(qc);
5527 if (unlikely(qc->err_mask))
5528 goto err;
5529 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005530
Tejun Heo60f5d6e2010-08-23 11:27:27 +02005531sys_err:
Tejun Heo8e0e6942006-03-31 20:41:11 +09005532 qc->err_mask |= AC_ERR_SYSTEM;
5533err:
5534 ata_qc_complete(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005535}
5536
5537/**
Tejun Heo34bf2172006-05-15 20:57:46 +09005538 * sata_scr_valid - test whether SCRs are accessible
Tejun Heo936fd732007-08-06 18:36:23 +09005539 * @link: ATA link to test SCR accessibility for
Tejun Heo34bf2172006-05-15 20:57:46 +09005540 *
Tejun Heo936fd732007-08-06 18:36:23 +09005541 * Test whether SCRs are accessible for @link.
Tejun Heo34bf2172006-05-15 20:57:46 +09005542 *
5543 * LOCKING:
5544 * None.
5545 *
5546 * RETURNS:
5547 * 1 if SCRs are accessible, 0 otherwise.
5548 */
Tejun Heo936fd732007-08-06 18:36:23 +09005549int sata_scr_valid(struct ata_link *link)
Tejun Heo34bf2172006-05-15 20:57:46 +09005550{
Tejun Heo936fd732007-08-06 18:36:23 +09005551 struct ata_port *ap = link->ap;
5552
Tejun Heoa16abc02007-05-21 18:33:47 +02005553 return (ap->flags & ATA_FLAG_SATA) && ap->ops->scr_read;
Tejun Heo34bf2172006-05-15 20:57:46 +09005554}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01005555EXPORT_SYMBOL_GPL(sata_scr_valid);
Tejun Heo34bf2172006-05-15 20:57:46 +09005556
5557/**
5558 * sata_scr_read - read SCR register of the specified port
Tejun Heo936fd732007-08-06 18:36:23 +09005559 * @link: ATA link to read SCR for
Tejun Heo34bf2172006-05-15 20:57:46 +09005560 * @reg: SCR to read
5561 * @val: Place to store read value
5562 *
Tejun Heo936fd732007-08-06 18:36:23 +09005563 * Read SCR register @reg of @link into *@val. This function is
Tejun Heo633273a2007-09-23 13:19:54 +09005564 * guaranteed to succeed if @link is ap->link, the cable type of
5565 * the port is SATA and the port implements ->scr_read.
Tejun Heo34bf2172006-05-15 20:57:46 +09005566 *
5567 * LOCKING:
Tejun Heo633273a2007-09-23 13:19:54 +09005568 * None if @link is ap->link. Kernel thread context otherwise.
Tejun Heo34bf2172006-05-15 20:57:46 +09005569 *
5570 * RETURNS:
5571 * 0 on success, negative errno on failure.
5572 */
Tejun Heo936fd732007-08-06 18:36:23 +09005573int sata_scr_read(struct ata_link *link, int reg, u32 *val)
Tejun Heo34bf2172006-05-15 20:57:46 +09005574{
Tejun Heo633273a2007-09-23 13:19:54 +09005575 if (ata_is_host_link(link)) {
Tejun Heo633273a2007-09-23 13:19:54 +09005576 if (sata_scr_valid(link))
Tejun Heo82ef04f2008-07-31 17:02:40 +09005577 return link->ap->ops->scr_read(link, reg, val);
Tejun Heo633273a2007-09-23 13:19:54 +09005578 return -EOPNOTSUPP;
5579 }
5580
5581 return sata_pmp_scr_read(link, reg, val);
Tejun Heo34bf2172006-05-15 20:57:46 +09005582}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01005583EXPORT_SYMBOL_GPL(sata_scr_read);
Tejun Heo34bf2172006-05-15 20:57:46 +09005584
5585/**
5586 * sata_scr_write - write SCR register of the specified port
Tejun Heo936fd732007-08-06 18:36:23 +09005587 * @link: ATA link to write SCR for
Tejun Heo34bf2172006-05-15 20:57:46 +09005588 * @reg: SCR to write
5589 * @val: value to write
5590 *
Tejun Heo936fd732007-08-06 18:36:23 +09005591 * Write @val to SCR register @reg of @link. This function is
Tejun Heo633273a2007-09-23 13:19:54 +09005592 * guaranteed to succeed if @link is ap->link, the cable type of
5593 * the port is SATA and the port implements ->scr_read.
Tejun Heo34bf2172006-05-15 20:57:46 +09005594 *
5595 * LOCKING:
Tejun Heo633273a2007-09-23 13:19:54 +09005596 * None if @link is ap->link. Kernel thread context otherwise.
Tejun Heo34bf2172006-05-15 20:57:46 +09005597 *
5598 * RETURNS:
5599 * 0 on success, negative errno on failure.
5600 */
Tejun Heo936fd732007-08-06 18:36:23 +09005601int sata_scr_write(struct ata_link *link, int reg, u32 val)
Tejun Heo34bf2172006-05-15 20:57:46 +09005602{
Tejun Heo633273a2007-09-23 13:19:54 +09005603 if (ata_is_host_link(link)) {
Tejun Heo633273a2007-09-23 13:19:54 +09005604 if (sata_scr_valid(link))
Tejun Heo82ef04f2008-07-31 17:02:40 +09005605 return link->ap->ops->scr_write(link, reg, val);
Tejun Heo633273a2007-09-23 13:19:54 +09005606 return -EOPNOTSUPP;
5607 }
5608
5609 return sata_pmp_scr_write(link, reg, val);
Tejun Heo34bf2172006-05-15 20:57:46 +09005610}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01005611EXPORT_SYMBOL_GPL(sata_scr_write);
Tejun Heo34bf2172006-05-15 20:57:46 +09005612
5613/**
5614 * sata_scr_write_flush - write SCR register of the specified port and flush
Tejun Heo936fd732007-08-06 18:36:23 +09005615 * @link: ATA link to write SCR for
Tejun Heo34bf2172006-05-15 20:57:46 +09005616 * @reg: SCR to write
5617 * @val: value to write
5618 *
5619 * This function is identical to sata_scr_write() except that this
5620 * function performs flush after writing to the register.
5621 *
5622 * LOCKING:
Tejun Heo633273a2007-09-23 13:19:54 +09005623 * None if @link is ap->link. Kernel thread context otherwise.
Tejun Heo34bf2172006-05-15 20:57:46 +09005624 *
5625 * RETURNS:
5626 * 0 on success, negative errno on failure.
5627 */
Tejun Heo936fd732007-08-06 18:36:23 +09005628int sata_scr_write_flush(struct ata_link *link, int reg, u32 val)
Tejun Heo34bf2172006-05-15 20:57:46 +09005629{
Tejun Heo633273a2007-09-23 13:19:54 +09005630 if (ata_is_host_link(link)) {
Tejun Heo633273a2007-09-23 13:19:54 +09005631 int rc;
Tejun Heoda3dbb12007-07-16 14:29:40 +09005632
Tejun Heo633273a2007-09-23 13:19:54 +09005633 if (sata_scr_valid(link)) {
Tejun Heo82ef04f2008-07-31 17:02:40 +09005634 rc = link->ap->ops->scr_write(link, reg, val);
Tejun Heo633273a2007-09-23 13:19:54 +09005635 if (rc == 0)
Tejun Heo82ef04f2008-07-31 17:02:40 +09005636 rc = link->ap->ops->scr_read(link, reg, &val);
Tejun Heo633273a2007-09-23 13:19:54 +09005637 return rc;
5638 }
5639 return -EOPNOTSUPP;
Tejun Heo34bf2172006-05-15 20:57:46 +09005640 }
Tejun Heo633273a2007-09-23 13:19:54 +09005641
5642 return sata_pmp_scr_write(link, reg, val);
Tejun Heo34bf2172006-05-15 20:57:46 +09005643}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01005644EXPORT_SYMBOL_GPL(sata_scr_write_flush);
Tejun Heo34bf2172006-05-15 20:57:46 +09005645
5646/**
Tejun Heob1c72912008-07-31 17:02:43 +09005647 * ata_phys_link_online - test whether the given link is online
Tejun Heo936fd732007-08-06 18:36:23 +09005648 * @link: ATA link to test
Tejun Heo34bf2172006-05-15 20:57:46 +09005649 *
Tejun Heo936fd732007-08-06 18:36:23 +09005650 * Test whether @link is online. Note that this function returns
5651 * 0 if online status of @link cannot be obtained, so
5652 * ata_link_online(link) != !ata_link_offline(link).
Tejun Heo34bf2172006-05-15 20:57:46 +09005653 *
5654 * LOCKING:
5655 * None.
5656 *
5657 * RETURNS:
Tejun Heob5b3fa32008-07-31 17:02:42 +09005658 * True if the port online status is available and online.
Tejun Heo34bf2172006-05-15 20:57:46 +09005659 */
Tejun Heob1c72912008-07-31 17:02:43 +09005660bool ata_phys_link_online(struct ata_link *link)
Tejun Heo34bf2172006-05-15 20:57:46 +09005661{
5662 u32 sstatus;
5663
Tejun Heo936fd732007-08-06 18:36:23 +09005664 if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0 &&
Tejun Heo9913ff8a2009-01-29 20:31:31 +09005665 ata_sstatus_online(sstatus))
Tejun Heob5b3fa32008-07-31 17:02:42 +09005666 return true;
5667 return false;
Tejun Heo34bf2172006-05-15 20:57:46 +09005668}
5669
5670/**
Tejun Heob1c72912008-07-31 17:02:43 +09005671 * ata_phys_link_offline - test whether the given link is offline
Tejun Heo936fd732007-08-06 18:36:23 +09005672 * @link: ATA link to test
Tejun Heo34bf2172006-05-15 20:57:46 +09005673 *
Tejun Heo936fd732007-08-06 18:36:23 +09005674 * Test whether @link is offline. Note that this function
5675 * returns 0 if offline status of @link cannot be obtained, so
5676 * ata_link_online(link) != !ata_link_offline(link).
Tejun Heo34bf2172006-05-15 20:57:46 +09005677 *
5678 * LOCKING:
5679 * None.
5680 *
5681 * RETURNS:
Tejun Heob5b3fa32008-07-31 17:02:42 +09005682 * True if the port offline status is available and offline.
Tejun Heo34bf2172006-05-15 20:57:46 +09005683 */
Tejun Heob1c72912008-07-31 17:02:43 +09005684bool ata_phys_link_offline(struct ata_link *link)
Tejun Heo34bf2172006-05-15 20:57:46 +09005685{
5686 u32 sstatus;
5687
Tejun Heo936fd732007-08-06 18:36:23 +09005688 if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0 &&
Tejun Heo9913ff8a2009-01-29 20:31:31 +09005689 !ata_sstatus_online(sstatus))
Tejun Heob5b3fa32008-07-31 17:02:42 +09005690 return true;
5691 return false;
Tejun Heo34bf2172006-05-15 20:57:46 +09005692}
Edward Falk0baab862005-06-02 18:17:13 -04005693
Tejun Heob1c72912008-07-31 17:02:43 +09005694/**
5695 * ata_link_online - test whether the given link is online
5696 * @link: ATA link to test
5697 *
5698 * Test whether @link is online. This is identical to
5699 * ata_phys_link_online() when there's no slave link. When
5700 * there's a slave link, this function should only be called on
5701 * the master link and will return true if any of M/S links is
5702 * online.
5703 *
5704 * LOCKING:
5705 * None.
5706 *
5707 * RETURNS:
5708 * True if the port online status is available and online.
5709 */
5710bool ata_link_online(struct ata_link *link)
5711{
5712 struct ata_link *slave = link->ap->slave_link;
5713
5714 WARN_ON(link == slave); /* shouldn't be called on slave link */
5715
5716 return ata_phys_link_online(link) ||
5717 (slave && ata_phys_link_online(slave));
5718}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01005719EXPORT_SYMBOL_GPL(ata_link_online);
Tejun Heob1c72912008-07-31 17:02:43 +09005720
5721/**
5722 * ata_link_offline - test whether the given link is offline
5723 * @link: ATA link to test
5724 *
5725 * Test whether @link is offline. This is identical to
5726 * ata_phys_link_offline() when there's no slave link. When
5727 * there's a slave link, this function should only be called on
5728 * the master link and will return true if both M/S links are
5729 * offline.
5730 *
5731 * LOCKING:
5732 * None.
5733 *
5734 * RETURNS:
5735 * True if the port offline status is available and offline.
5736 */
5737bool ata_link_offline(struct ata_link *link)
5738{
5739 struct ata_link *slave = link->ap->slave_link;
5740
5741 WARN_ON(link == slave); /* shouldn't be called on slave link */
5742
5743 return ata_phys_link_offline(link) &&
5744 (!slave || ata_phys_link_offline(slave));
5745}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01005746EXPORT_SYMBOL_GPL(ata_link_offline);
Tejun Heob1c72912008-07-31 17:02:43 +09005747
Tejun Heo6ffa01d2007-03-02 17:32:47 +09005748#ifdef CONFIG_PM
Dan Williamsbc6e7c42014-03-14 13:52:48 -07005749static void ata_port_request_pm(struct ata_port *ap, pm_message_t mesg,
5750 unsigned int action, unsigned int ehi_flags,
5751 bool async)
Tejun Heo500530f2006-07-03 16:07:27 +09005752{
Lin Ming5ef41082011-12-05 09:20:27 +08005753 struct ata_link *link;
Tejun Heo500530f2006-07-03 16:07:27 +09005754 unsigned long flags;
Tejun Heo500530f2006-07-03 16:07:27 +09005755
Lin Ming5ef41082011-12-05 09:20:27 +08005756 /* Previous resume operation might still be in
5757 * progress. Wait for PM_PENDING to clear.
5758 */
5759 if (ap->pflags & ATA_PFLAG_PM_PENDING) {
5760 ata_port_wait_eh(ap);
5761 WARN_ON(ap->pflags & ATA_PFLAG_PM_PENDING);
Tejun Heo500530f2006-07-03 16:07:27 +09005762 }
5763
Lin Ming5ef41082011-12-05 09:20:27 +08005764 /* request PM ops to EH */
5765 spin_lock_irqsave(ap->lock, flags);
5766
5767 ap->pm_mesg = mesg;
Lin Ming5ef41082011-12-05 09:20:27 +08005768 ap->pflags |= ATA_PFLAG_PM_PENDING;
5769 ata_for_each_link(link, ap, HOST_FIRST) {
5770 link->eh_info.action |= action;
5771 link->eh_info.flags |= ehi_flags;
5772 }
5773
5774 ata_port_schedule_eh(ap);
5775
5776 spin_unlock_irqrestore(ap->lock, flags);
5777
Dan Williams2fcbdcb2012-06-21 23:41:46 -07005778 if (!async) {
Lin Ming5ef41082011-12-05 09:20:27 +08005779 ata_port_wait_eh(ap);
5780 WARN_ON(ap->pflags & ATA_PFLAG_PM_PENDING);
5781 }
Tejun Heo500530f2006-07-03 16:07:27 +09005782}
5783
Dan Williamsbc6e7c42014-03-14 13:52:48 -07005784/*
5785 * On some hardware, device fails to respond after spun down for suspend. As
5786 * the device won't be used before being resumed, we don't need to touch the
5787 * device. Ask EH to skip the usual stuff and proceed directly to suspend.
5788 *
5789 * http://thread.gmane.org/gmane.linux.ide/46764
5790 */
5791static const unsigned int ata_port_suspend_ehi = ATA_EHI_QUIET
5792 | ATA_EHI_NO_AUTOPSY
5793 | ATA_EHI_NO_RECOVERY;
5794
5795static void ata_port_suspend(struct ata_port *ap, pm_message_t mesg)
Lin Ming5ef41082011-12-05 09:20:27 +08005796{
Dan Williamsbc6e7c42014-03-14 13:52:48 -07005797 ata_port_request_pm(ap, mesg, 0, ata_port_suspend_ehi, false);
Lin Ming5ef41082011-12-05 09:20:27 +08005798}
5799
Dan Williamsbc6e7c42014-03-14 13:52:48 -07005800static void ata_port_suspend_async(struct ata_port *ap, pm_message_t mesg)
5801{
5802 ata_port_request_pm(ap, mesg, 0, ata_port_suspend_ehi, true);
5803}
5804
5805static int ata_port_pm_suspend(struct device *dev)
Dan Williams2fcbdcb2012-06-21 23:41:46 -07005806{
5807 struct ata_port *ap = to_ata_port(dev);
5808
Lin Ming5ef41082011-12-05 09:20:27 +08005809 if (pm_runtime_suspended(dev))
5810 return 0;
5811
Dan Williamsbc6e7c42014-03-14 13:52:48 -07005812 ata_port_suspend(ap, PMSG_SUSPEND);
5813 return 0;
Lin Ming33574d62011-12-22 14:50:49 +08005814}
5815
Dan Williamsbc6e7c42014-03-14 13:52:48 -07005816static int ata_port_pm_freeze(struct device *dev)
Dan Williams2fcbdcb2012-06-21 23:41:46 -07005817{
5818 struct ata_port *ap = to_ata_port(dev);
5819
Dan Williamsbc6e7c42014-03-14 13:52:48 -07005820 if (pm_runtime_suspended(dev))
5821 return 0;
5822
5823 ata_port_suspend(ap, PMSG_FREEZE);
5824 return 0;
Dan Williams2fcbdcb2012-06-21 23:41:46 -07005825}
5826
Dan Williamsbc6e7c42014-03-14 13:52:48 -07005827static int ata_port_pm_poweroff(struct device *dev)
Lin Minge90b1e52011-12-22 14:50:48 +08005828{
Dan Williamsbc6e7c42014-03-14 13:52:48 -07005829 ata_port_suspend(to_ata_port(dev), PMSG_HIBERNATE);
5830 return 0;
5831}
Lin Minge90b1e52011-12-22 14:50:48 +08005832
Dan Williamsbc6e7c42014-03-14 13:52:48 -07005833static const unsigned int ata_port_resume_ehi = ATA_EHI_NO_AUTOPSY
5834 | ATA_EHI_QUIET;
Lin Minge90b1e52011-12-22 14:50:48 +08005835
Dan Williamsbc6e7c42014-03-14 13:52:48 -07005836static void ata_port_resume(struct ata_port *ap, pm_message_t mesg)
5837{
5838 ata_port_request_pm(ap, mesg, ATA_EH_RESET, ata_port_resume_ehi, false);
5839}
5840
5841static void ata_port_resume_async(struct ata_port *ap, pm_message_t mesg)
5842{
5843 ata_port_request_pm(ap, mesg, ATA_EH_RESET, ata_port_resume_ehi, true);
5844}
5845
5846static int ata_port_pm_resume(struct device *dev)
5847{
Todd Brandt200421a2014-03-14 13:52:54 -07005848 ata_port_resume_async(to_ata_port(dev), PMSG_RESUME);
Dan Williamsbc6e7c42014-03-14 13:52:48 -07005849 pm_runtime_disable(dev);
5850 pm_runtime_set_active(dev);
5851 pm_runtime_enable(dev);
5852 return 0;
Lin Minge90b1e52011-12-22 14:50:48 +08005853}
5854
Aaron Lu7e15e9b2013-01-15 17:21:04 +08005855/*
5856 * For ODDs, the upper layer will poll for media change every few seconds,
5857 * which will make it enter and leave suspend state every few seconds. And
5858 * as each suspend will cause a hard/soft reset, the gain of runtime suspend
5859 * is very little and the ODD may malfunction after constantly being reset.
5860 * So the idle callback here will not proceed to suspend if a non-ZPODD capable
5861 * ODD is attached to the port.
5862 */
Lin Ming9ee4f392011-12-05 09:20:28 +08005863static int ata_port_runtime_idle(struct device *dev)
5864{
Aaron Lu7e15e9b2013-01-15 17:21:04 +08005865 struct ata_port *ap = to_ata_port(dev);
5866 struct ata_link *link;
5867 struct ata_device *adev;
5868
5869 ata_for_each_link(link, ap, HOST_FIRST) {
5870 ata_for_each_dev(adev, link, ENABLED)
5871 if (adev->class == ATA_DEV_ATAPI &&
5872 !zpodd_dev_enabled(adev))
5873 return -EBUSY;
5874 }
5875
Rafael J. Wysocki45f0a852013-06-03 21:49:52 +02005876 return 0;
Lin Ming9ee4f392011-12-05 09:20:28 +08005877}
5878
Aaron Lua7ff60d2013-01-25 14:29:35 +08005879static int ata_port_runtime_suspend(struct device *dev)
5880{
Dan Williamsbc6e7c42014-03-14 13:52:48 -07005881 ata_port_suspend(to_ata_port(dev), PMSG_AUTO_SUSPEND);
5882 return 0;
Aaron Lua7ff60d2013-01-25 14:29:35 +08005883}
5884
5885static int ata_port_runtime_resume(struct device *dev)
5886{
Dan Williamsbc6e7c42014-03-14 13:52:48 -07005887 ata_port_resume(to_ata_port(dev), PMSG_AUTO_RESUME);
5888 return 0;
Aaron Lua7ff60d2013-01-25 14:29:35 +08005889}
5890
Lin Ming5ef41082011-12-05 09:20:27 +08005891static const struct dev_pm_ops ata_port_pm_ops = {
Dan Williamsbc6e7c42014-03-14 13:52:48 -07005892 .suspend = ata_port_pm_suspend,
5893 .resume = ata_port_pm_resume,
5894 .freeze = ata_port_pm_freeze,
5895 .thaw = ata_port_pm_resume,
5896 .poweroff = ata_port_pm_poweroff,
5897 .restore = ata_port_pm_resume,
Lin Ming9ee4f392011-12-05 09:20:28 +08005898
Aaron Lua7ff60d2013-01-25 14:29:35 +08005899 .runtime_suspend = ata_port_runtime_suspend,
5900 .runtime_resume = ata_port_runtime_resume,
Lin Ming9ee4f392011-12-05 09:20:28 +08005901 .runtime_idle = ata_port_runtime_idle,
Lin Ming5ef41082011-12-05 09:20:27 +08005902};
5903
Dan Williams2fcbdcb2012-06-21 23:41:46 -07005904/* sas ports don't participate in pm runtime management of ata_ports,
5905 * and need to resume ata devices at the domain level, not the per-port
5906 * level. sas suspend/resume is async to allow parallel port recovery
5907 * since sas has multiple ata_port instances per Scsi_Host.
5908 */
Dan Williamsbc6e7c42014-03-14 13:52:48 -07005909void ata_sas_port_suspend(struct ata_port *ap)
Dan Williams2fcbdcb2012-06-21 23:41:46 -07005910{
Dan Williamsbc6e7c42014-03-14 13:52:48 -07005911 ata_port_suspend_async(ap, PMSG_SUSPEND);
Dan Williams2fcbdcb2012-06-21 23:41:46 -07005912}
Dan Williamsbc6e7c42014-03-14 13:52:48 -07005913EXPORT_SYMBOL_GPL(ata_sas_port_suspend);
Dan Williams2fcbdcb2012-06-21 23:41:46 -07005914
Dan Williamsbc6e7c42014-03-14 13:52:48 -07005915void ata_sas_port_resume(struct ata_port *ap)
Dan Williams2fcbdcb2012-06-21 23:41:46 -07005916{
Dan Williamsbc6e7c42014-03-14 13:52:48 -07005917 ata_port_resume_async(ap, PMSG_RESUME);
Dan Williams2fcbdcb2012-06-21 23:41:46 -07005918}
Dan Williamsbc6e7c42014-03-14 13:52:48 -07005919EXPORT_SYMBOL_GPL(ata_sas_port_resume);
Dan Williams2fcbdcb2012-06-21 23:41:46 -07005920
Tejun Heo500530f2006-07-03 16:07:27 +09005921/**
Jeff Garzikcca39742006-08-24 03:19:22 -04005922 * ata_host_suspend - suspend host
5923 * @host: host to suspend
Tejun Heo500530f2006-07-03 16:07:27 +09005924 * @mesg: PM message
5925 *
Lin Ming5ef41082011-12-05 09:20:27 +08005926 * Suspend @host. Actual operation is performed by port suspend.
Tejun Heo500530f2006-07-03 16:07:27 +09005927 */
Jeff Garzikcca39742006-08-24 03:19:22 -04005928int ata_host_suspend(struct ata_host *host, pm_message_t mesg)
Tejun Heo500530f2006-07-03 16:07:27 +09005929{
Lin Ming5ef41082011-12-05 09:20:27 +08005930 host->dev->power.power_state = mesg;
5931 return 0;
Tejun Heo500530f2006-07-03 16:07:27 +09005932}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01005933EXPORT_SYMBOL_GPL(ata_host_suspend);
Tejun Heo500530f2006-07-03 16:07:27 +09005934
5935/**
Jeff Garzikcca39742006-08-24 03:19:22 -04005936 * ata_host_resume - resume host
5937 * @host: host to resume
Tejun Heo500530f2006-07-03 16:07:27 +09005938 *
Lin Ming5ef41082011-12-05 09:20:27 +08005939 * Resume @host. Actual operation is performed by port resume.
Tejun Heo500530f2006-07-03 16:07:27 +09005940 */
Jeff Garzikcca39742006-08-24 03:19:22 -04005941void ata_host_resume(struct ata_host *host)
Tejun Heo500530f2006-07-03 16:07:27 +09005942{
Jeff Garzik72ad6ec2008-02-25 17:31:10 -05005943 host->dev->power.power_state = PMSG_ON;
Tejun Heo500530f2006-07-03 16:07:27 +09005944}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01005945EXPORT_SYMBOL_GPL(ata_host_resume);
Tejun Heo6ffa01d2007-03-02 17:32:47 +09005946#endif
Tejun Heo500530f2006-07-03 16:07:27 +09005947
Bhumika Goyal8df82c12017-09-30 22:10:40 +05305948const struct device_type ata_port_type = {
Lin Ming5ef41082011-12-05 09:20:27 +08005949 .name = "ata_port",
5950#ifdef CONFIG_PM
5951 .pm = &ata_port_pm_ops,
5952#endif
5953};
5954
Randy Dunlapc893a3a2006-01-28 13:15:32 -05005955/**
Tejun Heo3ef3b432006-05-31 18:27:30 +09005956 * ata_dev_init - Initialize an ata_device structure
5957 * @dev: Device structure to initialize
5958 *
5959 * Initialize @dev in preparation for probing.
5960 *
5961 * LOCKING:
5962 * Inherited from caller.
5963 */
5964void ata_dev_init(struct ata_device *dev)
5965{
Tejun Heob1c72912008-07-31 17:02:43 +09005966 struct ata_link *link = ata_dev_phys_link(dev);
Tejun Heo9af5c9c2007-08-06 18:36:22 +09005967 struct ata_port *ap = link->ap;
Tejun Heo72fa4b72006-05-31 18:27:32 +09005968 unsigned long flags;
Tejun Heo3ef3b432006-05-31 18:27:30 +09005969
Tejun Heob1c72912008-07-31 17:02:43 +09005970 /* SATA spd limit is bound to the attached device, reset together */
Tejun Heo9af5c9c2007-08-06 18:36:22 +09005971 link->sata_spd_limit = link->hw_sata_spd_limit;
5972 link->sata_spd = 0;
Tejun Heo5a04bf42006-05-31 18:27:38 +09005973
Tejun Heo72fa4b72006-05-31 18:27:32 +09005974 /* High bits of dev->flags are used to record warm plug
5975 * requests which occur asynchronously. Synchronize using
Jeff Garzikcca39742006-08-24 03:19:22 -04005976 * host lock.
Tejun Heo72fa4b72006-05-31 18:27:32 +09005977 */
Jeff Garzikba6a1302006-06-22 23:46:10 -04005978 spin_lock_irqsave(ap->lock, flags);
Tejun Heo72fa4b72006-05-31 18:27:32 +09005979 dev->flags &= ~ATA_DFLAG_INIT_MASK;
Tejun Heo3dcc3232007-09-03 12:20:11 +09005980 dev->horkage = 0;
Jeff Garzikba6a1302006-06-22 23:46:10 -04005981 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heo72fa4b72006-05-31 18:27:32 +09005982
Tejun Heo99cf6102009-01-29 20:31:32 +09005983 memset((void *)dev + ATA_DEVICE_CLEAR_BEGIN, 0,
5984 ATA_DEVICE_CLEAR_END - ATA_DEVICE_CLEAR_BEGIN);
Tejun Heo3ef3b432006-05-31 18:27:30 +09005985 dev->pio_mask = UINT_MAX;
5986 dev->mwdma_mask = UINT_MAX;
5987 dev->udma_mask = UINT_MAX;
5988}
5989
5990/**
Tejun Heo4fb37a22007-08-06 18:36:23 +09005991 * ata_link_init - Initialize an ata_link structure
5992 * @ap: ATA port link is attached to
5993 * @link: Link structure to initialize
Tejun Heo89898052007-08-06 18:36:23 +09005994 * @pmp: Port multiplier port number
Tejun Heo4fb37a22007-08-06 18:36:23 +09005995 *
5996 * Initialize @link.
5997 *
5998 * LOCKING:
5999 * Kernel thread context (may sleep)
6000 */
Tejun Heofb7fd612007-09-23 13:14:12 +09006001void ata_link_init(struct ata_port *ap, struct ata_link *link, int pmp)
Tejun Heo4fb37a22007-08-06 18:36:23 +09006002{
6003 int i;
6004
6005 /* clear everything except for devices */
Gwendal Grignoud9027472010-05-25 12:31:38 -07006006 memset((void *)link + ATA_LINK_CLEAR_BEGIN, 0,
6007 ATA_LINK_CLEAR_END - ATA_LINK_CLEAR_BEGIN);
Tejun Heo4fb37a22007-08-06 18:36:23 +09006008
6009 link->ap = ap;
Tejun Heo89898052007-08-06 18:36:23 +09006010 link->pmp = pmp;
Tejun Heo4fb37a22007-08-06 18:36:23 +09006011 link->active_tag = ATA_TAG_POISON;
6012 link->hw_sata_spd_limit = UINT_MAX;
6013
6014 /* can't use iterator, ap isn't initialized yet */
6015 for (i = 0; i < ATA_MAX_DEVICES; i++) {
6016 struct ata_device *dev = &link->device[i];
6017
6018 dev->link = link;
6019 dev->devno = dev - link->device;
Tejun Heo110f66d2009-09-16 04:17:28 +09006020#ifdef CONFIG_ATA_ACPI
6021 dev->gtf_filter = ata_acpi_gtf_filter;
6022#endif
Tejun Heo4fb37a22007-08-06 18:36:23 +09006023 ata_dev_init(dev);
6024 }
6025}
6026
6027/**
6028 * sata_link_init_spd - Initialize link->sata_spd_limit
6029 * @link: Link to configure sata_spd_limit for
6030 *
6031 * Initialize @link->[hw_]sata_spd_limit to the currently
6032 * configured value.
6033 *
6034 * LOCKING:
6035 * Kernel thread context (may sleep).
6036 *
6037 * RETURNS:
6038 * 0 on success, -errno on failure.
6039 */
Tejun Heofb7fd612007-09-23 13:14:12 +09006040int sata_link_init_spd(struct ata_link *link)
Tejun Heo4fb37a22007-08-06 18:36:23 +09006041{
Tejun Heo33267322008-02-13 09:15:09 +09006042 u8 spd;
Tejun Heo4fb37a22007-08-06 18:36:23 +09006043 int rc;
6044
Tejun Heod127ea72008-07-31 16:09:34 +09006045 rc = sata_scr_read(link, SCR_CONTROL, &link->saved_scontrol);
Tejun Heo4fb37a22007-08-06 18:36:23 +09006046 if (rc)
6047 return rc;
6048
Tejun Heod127ea72008-07-31 16:09:34 +09006049 spd = (link->saved_scontrol >> 4) & 0xf;
Tejun Heo4fb37a22007-08-06 18:36:23 +09006050 if (spd)
6051 link->hw_sata_spd_limit &= (1 << spd) - 1;
6052
Tejun Heo05944bd2008-08-13 20:19:09 +09006053 ata_force_link_limits(link);
Tejun Heo33267322008-02-13 09:15:09 +09006054
Tejun Heo4fb37a22007-08-06 18:36:23 +09006055 link->sata_spd_limit = link->hw_sata_spd_limit;
6056
6057 return 0;
6058}
6059
6060/**
Tejun Heof3187192007-04-17 23:44:07 +09006061 * ata_port_alloc - allocate and initialize basic ATA port resources
6062 * @host: ATA host this allocated port belongs to
Linus Torvalds1da177e2005-04-16 15:20:36 -07006063 *
Tejun Heof3187192007-04-17 23:44:07 +09006064 * Allocate and initialize basic ATA port resources.
6065 *
6066 * RETURNS:
6067 * Allocate ATA port on success, NULL on failure.
Jeff Garzik0cba6322005-05-30 19:49:12 -04006068 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07006069 * LOCKING:
Tejun Heof3187192007-04-17 23:44:07 +09006070 * Inherited from calling layer (may sleep).
Linus Torvalds1da177e2005-04-16 15:20:36 -07006071 */
Tejun Heof3187192007-04-17 23:44:07 +09006072struct ata_port *ata_port_alloc(struct ata_host *host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006073{
Tejun Heof3187192007-04-17 23:44:07 +09006074 struct ata_port *ap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006075
Tejun Heof3187192007-04-17 23:44:07 +09006076 DPRINTK("ENTER\n");
6077
6078 ap = kzalloc(sizeof(*ap), GFP_KERNEL);
6079 if (!ap)
6080 return NULL;
Jeff Garzik4fca3772011-02-15 01:13:24 -05006081
Maxime Bizon7b3a24c52011-03-16 14:58:32 +01006082 ap->pflags |= ATA_PFLAG_INITIALIZING | ATA_PFLAG_FROZEN;
Jeff Garzikcca39742006-08-24 03:19:22 -04006083 ap->lock = &host->lock;
Tejun Heof3187192007-04-17 23:44:07 +09006084 ap->print_id = -1;
David Milburne628dc92013-05-14 13:48:40 -05006085 ap->local_port_no = -1;
Jeff Garzikcca39742006-08-24 03:19:22 -04006086 ap->host = host;
Tejun Heof3187192007-04-17 23:44:07 +09006087 ap->dev = host->dev;
Borislav Petkovbd5d8252006-06-11 23:17:01 -04006088
6089#if defined(ATA_VERBOSE_DEBUG)
6090 /* turn on all debugging levels */
6091 ap->msg_enable = 0x00FF;
6092#elif defined(ATA_DEBUG)
6093 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 +09006094#else
Borislav Petkov0dd4b212006-06-23 02:29:08 -04006095 ap->msg_enable = ATA_MSG_DRV | ATA_MSG_ERR | ATA_MSG_WARN;
Borislav Petkovbd5d8252006-06-11 23:17:01 -04006096#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07006097
Tejun Heoad72cf92010-07-02 10:03:52 +02006098 mutex_init(&ap->scsi_scan_mutex);
David Howells65f27f32006-11-22 14:55:48 +00006099 INIT_DELAYED_WORK(&ap->hotplug_task, ata_scsi_hotplug);
6100 INIT_WORK(&ap->scsi_rescan_task, ata_scsi_dev_rescan);
Tejun Heoa72ec4c2006-01-23 13:09:37 +09006101 INIT_LIST_HEAD(&ap->eh_done_q);
Tejun Heoc6cf9e92006-05-31 18:27:27 +09006102 init_waitqueue_head(&ap->eh_wait_q);
Elias Oltmanns45fabbb2008-09-21 11:54:08 +02006103 init_completion(&ap->park_req_pending);
Kees Cookb93ab332017-10-16 14:56:42 -07006104 timer_setup(&ap->fastdrain_timer, ata_eh_fastdrain_timerfn,
6105 TIMER_DEFERRABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006106
Tejun Heo838df622006-05-15 20:57:44 +09006107 ap->cbl = ATA_CBL_NONE;
Tejun Heo838df622006-05-15 20:57:44 +09006108
Tejun Heo89898052007-08-06 18:36:23 +09006109 ata_link_init(ap, &ap->link, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006110
6111#ifdef ATA_IRQ_TRAP
6112 ap->stats.unhandled_irq = 1;
6113 ap->stats.idle_irq = 1;
6114#endif
Tejun Heo270390e2010-05-10 21:41:35 +02006115 ata_sff_port_init(ap);
6116
Linus Torvalds1da177e2005-04-16 15:20:36 -07006117 return ap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006118}
6119
Taras Kondratiuk2623c7a2018-03-09 08:34:41 +00006120static void ata_devres_release(struct device *gendev, void *res)
Tejun Heof0d36ef2007-01-20 16:00:28 +09006121{
6122 struct ata_host *host = dev_get_drvdata(gendev);
6123 int i;
6124
6125 for (i = 0; i < host->n_ports; i++) {
6126 struct ata_port *ap = host->ports[i];
6127
Tejun Heoecef7252007-04-17 23:44:06 +09006128 if (!ap)
6129 continue;
6130
Tejun Heo49114872007-04-17 23:44:06 +09006131 if (ap->scsi_host)
Tejun Heo1aa506e42007-03-09 19:36:12 +09006132 scsi_host_put(ap->scsi_host);
6133
Taras Kondratiuk2623c7a2018-03-09 08:34:41 +00006134 }
6135
6136 dev_set_drvdata(gendev, NULL);
6137 ata_host_put(host);
6138}
6139
6140static void ata_host_release(struct kref *kref)
6141{
6142 struct ata_host *host = container_of(kref, struct ata_host, kref);
6143 int i;
6144
6145 for (i = 0; i < host->n_ports; i++) {
6146 struct ata_port *ap = host->ports[i];
6147
Tejun Heo633273a2007-09-23 13:19:54 +09006148 kfree(ap->pmp_link);
Tejun Heob1c72912008-07-31 17:02:43 +09006149 kfree(ap->slave_link);
Tejun Heo49114872007-04-17 23:44:06 +09006150 kfree(ap);
Tejun Heo1aa506e42007-03-09 19:36:12 +09006151 host->ports[i] = NULL;
6152 }
Taras Kondratiuk2623c7a2018-03-09 08:34:41 +00006153 kfree(host);
6154}
Tejun Heo1aa506e42007-03-09 19:36:12 +09006155
Taras Kondratiuk2623c7a2018-03-09 08:34:41 +00006156void ata_host_get(struct ata_host *host)
6157{
6158 kref_get(&host->kref);
6159}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01006160EXPORT_SYMBOL_GPL(ata_host_get);
Taras Kondratiuk2623c7a2018-03-09 08:34:41 +00006161
6162void ata_host_put(struct ata_host *host)
6163{
6164 kref_put(&host->kref, ata_host_release);
Tejun Heof0d36ef2007-01-20 16:00:28 +09006165}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01006166EXPORT_SYMBOL_GPL(ata_host_put);
Tejun Heof0d36ef2007-01-20 16:00:28 +09006167
Linus Torvalds1da177e2005-04-16 15:20:36 -07006168/**
Tejun Heof3187192007-04-17 23:44:07 +09006169 * ata_host_alloc - allocate and init basic ATA host resources
6170 * @dev: generic device this host is associated with
6171 * @max_ports: maximum number of ATA ports associated with this host
6172 *
6173 * Allocate and initialize basic ATA host resources. LLD calls
6174 * this function to allocate a host, initializes it fully and
6175 * attaches it using ata_host_register().
6176 *
6177 * @max_ports ports are allocated and host->n_ports is
6178 * initialized to @max_ports. The caller is allowed to decrease
6179 * host->n_ports before calling ata_host_register(). The unused
6180 * ports will be automatically freed on registration.
6181 *
6182 * RETURNS:
6183 * Allocate ATA host on success, NULL on failure.
6184 *
6185 * LOCKING:
6186 * Inherited from calling layer (may sleep).
6187 */
6188struct ata_host *ata_host_alloc(struct device *dev, int max_ports)
6189{
6190 struct ata_host *host;
6191 size_t sz;
6192 int i;
Taras Kondratiuk2623c7a2018-03-09 08:34:41 +00006193 void *dr;
Tejun Heof3187192007-04-17 23:44:07 +09006194
6195 DPRINTK("ENTER\n");
6196
Tejun Heof3187192007-04-17 23:44:07 +09006197 /* alloc a container for our list of ATA ports (buses) */
6198 sz = sizeof(struct ata_host) + (max_ports + 1) * sizeof(void *);
Taras Kondratiuk2623c7a2018-03-09 08:34:41 +00006199 host = kzalloc(sz, GFP_KERNEL);
Tejun Heof3187192007-04-17 23:44:07 +09006200 if (!host)
Taras Kondratiuk2623c7a2018-03-09 08:34:41 +00006201 return NULL;
6202
Tejun Heof3187192007-04-17 23:44:07 +09006203 if (!devres_open_group(dev, NULL, GFP_KERNEL))
Colin Ian Kingdafd6c42018-03-27 14:26:01 +01006204 goto err_free;
Tejun Heof3187192007-04-17 23:44:07 +09006205
Taras Kondratiuk2623c7a2018-03-09 08:34:41 +00006206 dr = devres_alloc(ata_devres_release, 0, GFP_KERNEL);
6207 if (!dr)
Tejun Heof3187192007-04-17 23:44:07 +09006208 goto err_out;
6209
Taras Kondratiuk2623c7a2018-03-09 08:34:41 +00006210 devres_add(dev, dr);
Tejun Heof3187192007-04-17 23:44:07 +09006211 dev_set_drvdata(dev, host);
6212
6213 spin_lock_init(&host->lock);
Tejun Heoc0c362b2010-09-06 17:57:14 +02006214 mutex_init(&host->eh_mutex);
Tejun Heof3187192007-04-17 23:44:07 +09006215 host->dev = dev;
6216 host->n_ports = max_ports;
Taras Kondratiuk2623c7a2018-03-09 08:34:41 +00006217 kref_init(&host->kref);
Tejun Heof3187192007-04-17 23:44:07 +09006218
6219 /* allocate ports bound to this host */
6220 for (i = 0; i < max_ports; i++) {
6221 struct ata_port *ap;
6222
6223 ap = ata_port_alloc(host);
6224 if (!ap)
6225 goto err_out;
6226
6227 ap->port_no = i;
6228 host->ports[i] = ap;
6229 }
6230
6231 devres_remove_group(dev, NULL);
6232 return host;
6233
6234 err_out:
6235 devres_release_group(dev, NULL);
Colin Ian Kingdafd6c42018-03-27 14:26:01 +01006236 err_free:
6237 kfree(host);
Tejun Heof3187192007-04-17 23:44:07 +09006238 return NULL;
6239}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01006240EXPORT_SYMBOL_GPL(ata_host_alloc);
Tejun Heof3187192007-04-17 23:44:07 +09006241
6242/**
Tejun Heof5cda252007-04-17 23:44:07 +09006243 * ata_host_alloc_pinfo - alloc host and init with port_info array
6244 * @dev: generic device this host is associated with
6245 * @ppi: array of ATA port_info to initialize host with
6246 * @n_ports: number of ATA ports attached to this host
6247 *
6248 * Allocate ATA host and initialize with info from @ppi. If NULL
6249 * terminated, @ppi may contain fewer entries than @n_ports. The
6250 * last entry will be used for the remaining ports.
6251 *
6252 * RETURNS:
6253 * Allocate ATA host on success, NULL on failure.
6254 *
6255 * LOCKING:
6256 * Inherited from calling layer (may sleep).
6257 */
6258struct ata_host *ata_host_alloc_pinfo(struct device *dev,
6259 const struct ata_port_info * const * ppi,
6260 int n_ports)
6261{
6262 const struct ata_port_info *pi;
6263 struct ata_host *host;
6264 int i, j;
6265
6266 host = ata_host_alloc(dev, n_ports);
6267 if (!host)
6268 return NULL;
6269
6270 for (i = 0, j = 0, pi = NULL; i < host->n_ports; i++) {
6271 struct ata_port *ap = host->ports[i];
6272
6273 if (ppi[j])
6274 pi = ppi[j++];
6275
6276 ap->pio_mask = pi->pio_mask;
6277 ap->mwdma_mask = pi->mwdma_mask;
6278 ap->udma_mask = pi->udma_mask;
6279 ap->flags |= pi->flags;
Tejun Heo0c887582007-08-06 18:36:23 +09006280 ap->link.flags |= pi->link_flags;
Tejun Heof5cda252007-04-17 23:44:07 +09006281 ap->ops = pi->port_ops;
6282
6283 if (!host->ops && (pi->port_ops != &ata_dummy_port_ops))
6284 host->ops = pi->port_ops;
Tejun Heof5cda252007-04-17 23:44:07 +09006285 }
6286
6287 return host;
6288}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01006289EXPORT_SYMBOL_GPL(ata_host_alloc_pinfo);
Tejun Heof5cda252007-04-17 23:44:07 +09006290
Tejun Heob1c72912008-07-31 17:02:43 +09006291/**
6292 * ata_slave_link_init - initialize slave link
6293 * @ap: port to initialize slave link for
6294 *
6295 * Create and initialize slave link for @ap. This enables slave
6296 * link handling on the port.
6297 *
6298 * In libata, a port contains links and a link contains devices.
6299 * There is single host link but if a PMP is attached to it,
6300 * there can be multiple fan-out links. On SATA, there's usually
6301 * a single device connected to a link but PATA and SATA
6302 * controllers emulating TF based interface can have two - master
6303 * and slave.
6304 *
6305 * However, there are a few controllers which don't fit into this
6306 * abstraction too well - SATA controllers which emulate TF
6307 * interface with both master and slave devices but also have
6308 * separate SCR register sets for each device. These controllers
6309 * need separate links for physical link handling
6310 * (e.g. onlineness, link speed) but should be treated like a
6311 * traditional M/S controller for everything else (e.g. command
6312 * issue, softreset).
6313 *
6314 * slave_link is libata's way of handling this class of
6315 * controllers without impacting core layer too much. For
6316 * anything other than physical link handling, the default host
6317 * link is used for both master and slave. For physical link
6318 * handling, separate @ap->slave_link is used. All dirty details
6319 * are implemented inside libata core layer. From LLD's POV, the
6320 * only difference is that prereset, hardreset and postreset are
6321 * called once more for the slave link, so the reset sequence
6322 * looks like the following.
6323 *
6324 * prereset(M) -> prereset(S) -> hardreset(M) -> hardreset(S) ->
6325 * softreset(M) -> postreset(M) -> postreset(S)
6326 *
6327 * Note that softreset is called only for the master. Softreset
6328 * resets both M/S by definition, so SRST on master should handle
6329 * both (the standard method will work just fine).
6330 *
6331 * LOCKING:
6332 * Should be called before host is registered.
6333 *
6334 * RETURNS:
6335 * 0 on success, -errno on failure.
6336 */
6337int ata_slave_link_init(struct ata_port *ap)
6338{
6339 struct ata_link *link;
6340
6341 WARN_ON(ap->slave_link);
6342 WARN_ON(ap->flags & ATA_FLAG_PMP);
6343
6344 link = kzalloc(sizeof(*link), GFP_KERNEL);
6345 if (!link)
6346 return -ENOMEM;
6347
6348 ata_link_init(ap, link, 1);
6349 ap->slave_link = link;
6350 return 0;
6351}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01006352EXPORT_SYMBOL_GPL(ata_slave_link_init);
Tejun Heob1c72912008-07-31 17:02:43 +09006353
Tejun Heo32ebbc02007-11-08 13:09:00 +09006354static void ata_host_stop(struct device *gendev, void *res)
6355{
6356 struct ata_host *host = dev_get_drvdata(gendev);
6357 int i;
6358
6359 WARN_ON(!(host->flags & ATA_HOST_STARTED));
6360
6361 for (i = 0; i < host->n_ports; i++) {
6362 struct ata_port *ap = host->ports[i];
6363
6364 if (ap->ops->port_stop)
6365 ap->ops->port_stop(ap);
6366 }
6367
6368 if (host->ops->host_stop)
6369 host->ops->host_stop(host);
6370}
6371
Tejun Heof5cda252007-04-17 23:44:07 +09006372/**
Tejun Heo029cfd62008-03-25 12:22:49 +09006373 * ata_finalize_port_ops - finalize ata_port_operations
6374 * @ops: ata_port_operations to finalize
6375 *
6376 * An ata_port_operations can inherit from another ops and that
6377 * ops can again inherit from another. This can go on as many
6378 * times as necessary as long as there is no loop in the
6379 * inheritance chain.
6380 *
6381 * Ops tables are finalized when the host is started. NULL or
6382 * unspecified entries are inherited from the closet ancestor
6383 * which has the method and the entry is populated with it.
6384 * After finalization, the ops table directly points to all the
6385 * methods and ->inherits is no longer necessary and cleared.
6386 *
6387 * Using ATA_OP_NULL, inheriting ops can force a method to NULL.
6388 *
6389 * LOCKING:
6390 * None.
6391 */
6392static void ata_finalize_port_ops(struct ata_port_operations *ops)
6393{
Pradeep Singh Rautela2da67652008-05-29 23:28:14 +05306394 static DEFINE_SPINLOCK(lock);
Tejun Heo029cfd62008-03-25 12:22:49 +09006395 const struct ata_port_operations *cur;
6396 void **begin = (void **)ops;
6397 void **end = (void **)&ops->inherits;
6398 void **pp;
6399
6400 if (!ops || !ops->inherits)
6401 return;
6402
6403 spin_lock(&lock);
6404
6405 for (cur = ops->inherits; cur; cur = cur->inherits) {
6406 void **inherit = (void **)cur;
6407
6408 for (pp = begin; pp < end; pp++, inherit++)
6409 if (!*pp)
6410 *pp = *inherit;
6411 }
6412
6413 for (pp = begin; pp < end; pp++)
6414 if (IS_ERR(*pp))
6415 *pp = NULL;
6416
6417 ops->inherits = NULL;
6418
6419 spin_unlock(&lock);
6420}
6421
6422/**
Tejun Heoecef7252007-04-17 23:44:06 +09006423 * ata_host_start - start and freeze ports of an ATA host
6424 * @host: ATA host to start ports for
6425 *
6426 * Start and then freeze ports of @host. Started status is
6427 * recorded in host->flags, so this function can be called
6428 * multiple times. Ports are guaranteed to get started only
Tejun Heof3187192007-04-17 23:44:07 +09006429 * once. If host->ops isn't initialized yet, its set to the
6430 * first non-dummy port ops.
Tejun Heoecef7252007-04-17 23:44:06 +09006431 *
6432 * LOCKING:
6433 * Inherited from calling layer (may sleep).
6434 *
6435 * RETURNS:
6436 * 0 if all ports are started successfully, -errno otherwise.
6437 */
6438int ata_host_start(struct ata_host *host)
6439{
Tejun Heo32ebbc02007-11-08 13:09:00 +09006440 int have_stop = 0;
6441 void *start_dr = NULL;
Tejun Heoecef7252007-04-17 23:44:06 +09006442 int i, rc;
6443
6444 if (host->flags & ATA_HOST_STARTED)
6445 return 0;
6446
Tejun Heo029cfd62008-03-25 12:22:49 +09006447 ata_finalize_port_ops(host->ops);
6448
Tejun Heoecef7252007-04-17 23:44:06 +09006449 for (i = 0; i < host->n_ports; i++) {
6450 struct ata_port *ap = host->ports[i];
6451
Tejun Heo029cfd62008-03-25 12:22:49 +09006452 ata_finalize_port_ops(ap->ops);
6453
Tejun Heof3187192007-04-17 23:44:07 +09006454 if (!host->ops && !ata_port_is_dummy(ap))
6455 host->ops = ap->ops;
6456
Tejun Heo32ebbc02007-11-08 13:09:00 +09006457 if (ap->ops->port_stop)
6458 have_stop = 1;
6459 }
6460
6461 if (host->ops->host_stop)
6462 have_stop = 1;
6463
6464 if (have_stop) {
6465 start_dr = devres_alloc(ata_host_stop, 0, GFP_KERNEL);
6466 if (!start_dr)
6467 return -ENOMEM;
6468 }
6469
6470 for (i = 0; i < host->n_ports; i++) {
6471 struct ata_port *ap = host->ports[i];
6472
Tejun Heoecef7252007-04-17 23:44:06 +09006473 if (ap->ops->port_start) {
6474 rc = ap->ops->port_start(ap);
6475 if (rc) {
Alan Cox0f9fe9b2007-11-30 15:23:16 +00006476 if (rc != -ENODEV)
Joe Perchesa44fec12011-04-15 15:51:58 -07006477 dev_err(host->dev,
6478 "failed to start port %d (errno=%d)\n",
6479 i, rc);
Tejun Heoecef7252007-04-17 23:44:06 +09006480 goto err_out;
6481 }
6482 }
Tejun Heoecef7252007-04-17 23:44:06 +09006483 ata_eh_freeze_port(ap);
6484 }
6485
Tejun Heo32ebbc02007-11-08 13:09:00 +09006486 if (start_dr)
6487 devres_add(host->dev, start_dr);
Tejun Heoecef7252007-04-17 23:44:06 +09006488 host->flags |= ATA_HOST_STARTED;
6489 return 0;
6490
6491 err_out:
6492 while (--i >= 0) {
6493 struct ata_port *ap = host->ports[i];
6494
6495 if (ap->ops->port_stop)
6496 ap->ops->port_stop(ap);
6497 }
Tejun Heo32ebbc02007-11-08 13:09:00 +09006498 devres_free(start_dr);
Tejun Heoecef7252007-04-17 23:44:06 +09006499 return rc;
6500}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01006501EXPORT_SYMBOL_GPL(ata_host_start);
Tejun Heoecef7252007-04-17 23:44:06 +09006502
6503/**
Dan Williams8d8e7d12012-07-09 21:06:08 -07006504 * ata_sas_host_init - Initialize a host struct for sas (ipr, libsas)
Jeff Garzikcca39742006-08-24 03:19:22 -04006505 * @host: host to initialize
6506 * @dev: device host is attached to
Jeff Garzikcca39742006-08-24 03:19:22 -04006507 * @ops: port_ops
Brian Kingb03732f2006-08-07 14:27:10 -05006508 *
Brian Kingb03732f2006-08-07 14:27:10 -05006509 */
Jeff Garzikcca39742006-08-24 03:19:22 -04006510void ata_host_init(struct ata_host *host, struct device *dev,
Dan Williams8d8e7d12012-07-09 21:06:08 -07006511 struct ata_port_operations *ops)
Brian Kingb03732f2006-08-07 14:27:10 -05006512{
Jeff Garzikcca39742006-08-24 03:19:22 -04006513 spin_lock_init(&host->lock);
Tejun Heoc0c362b2010-09-06 17:57:14 +02006514 mutex_init(&host->eh_mutex);
Jens Axboe69278f72018-05-11 12:51:10 -06006515 host->n_tags = ATA_MAX_QUEUE;
Jeff Garzikcca39742006-08-24 03:19:22 -04006516 host->dev = dev;
Jeff Garzikcca39742006-08-24 03:19:22 -04006517 host->ops = ops;
Jason Yan2fa4a322018-05-10 11:05:16 +08006518 kref_init(&host->kref);
Brian Kingb03732f2006-08-07 14:27:10 -05006519}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01006520EXPORT_SYMBOL_GPL(ata_host_init);
Brian Kingb03732f2006-08-07 14:27:10 -05006521
Dan Williams9508a662012-01-18 20:47:01 -08006522void __ata_port_probe(struct ata_port *ap)
6523{
6524 struct ata_eh_info *ehi = &ap->link.eh_info;
6525 unsigned long flags;
6526
6527 /* kick EH for boot probing */
6528 spin_lock_irqsave(ap->lock, flags);
6529
6530 ehi->probe_mask |= ATA_ALL_DEVICES;
6531 ehi->action |= ATA_EH_RESET;
6532 ehi->flags |= ATA_EHI_NO_AUTOPSY | ATA_EHI_QUIET;
6533
6534 ap->pflags &= ~ATA_PFLAG_INITIALIZING;
6535 ap->pflags |= ATA_PFLAG_LOADING;
6536 ata_port_schedule_eh(ap);
6537
6538 spin_unlock_irqrestore(ap->lock, flags);
6539}
6540
James Bottomley238c9cf2011-01-23 08:28:33 -06006541int ata_port_probe(struct ata_port *ap)
Arjan van de Ven79318052009-01-04 05:32:28 -08006542{
James Bottomley238c9cf2011-01-23 08:28:33 -06006543 int rc = 0;
Arjan van de Ven886ad092009-01-09 15:54:07 -08006544
Arjan van de Ven79318052009-01-04 05:32:28 -08006545 if (ap->ops->error_handler) {
Dan Williams9508a662012-01-18 20:47:01 -08006546 __ata_port_probe(ap);
Arjan van de Ven79318052009-01-04 05:32:28 -08006547 ata_port_wait_eh(ap);
6548 } else {
6549 DPRINTK("ata%u: bus probe begin\n", ap->print_id);
6550 rc = ata_bus_probe(ap);
6551 DPRINTK("ata%u: bus probe end\n", ap->print_id);
Arjan van de Ven79318052009-01-04 05:32:28 -08006552 }
James Bottomley238c9cf2011-01-23 08:28:33 -06006553 return rc;
6554}
6555
6556
6557static void async_port_probe(void *data, async_cookie_t cookie)
6558{
6559 struct ata_port *ap = data;
Jeff Garzik4fca3772011-02-15 01:13:24 -05006560
James Bottomley238c9cf2011-01-23 08:28:33 -06006561 /*
6562 * If we're not allowed to scan this host in parallel,
6563 * we need to wait until all previous scans have completed
6564 * before going further.
6565 * Jeff Garzik says this is only within a controller, so we
6566 * don't need to wait for port 0, only for later ports.
6567 */
6568 if (!(ap->host->flags & ATA_HOST_PARALLEL_SCAN) && ap->port_no != 0)
6569 async_synchronize_cookie(cookie);
6570
6571 (void)ata_port_probe(ap);
Arjan van de Venf29d3b22009-01-05 15:07:07 -08006572
6573 /* in order to keep device order, we need to synchronize at this point */
6574 async_synchronize_cookie(cookie);
6575
6576 ata_scsi_scan_host(ap, 1);
Arjan van de Ven79318052009-01-04 05:32:28 -08006577}
James Bottomley238c9cf2011-01-23 08:28:33 -06006578
Brian Kingb03732f2006-08-07 14:27:10 -05006579/**
Tejun Heof3187192007-04-17 23:44:07 +09006580 * ata_host_register - register initialized ATA host
6581 * @host: ATA host to register
6582 * @sht: template for SCSI host
Jeff Garzik0cba6322005-05-30 19:49:12 -04006583 *
Tejun Heof3187192007-04-17 23:44:07 +09006584 * Register initialized ATA host. @host is allocated using
6585 * ata_host_alloc() and fully initialized by LLD. This function
6586 * starts ports, registers @host with ATA and SCSI layers and
6587 * probe registered devices.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006588 *
6589 * LOCKING:
Tejun Heof3187192007-04-17 23:44:07 +09006590 * Inherited from calling layer (may sleep).
Linus Torvalds1da177e2005-04-16 15:20:36 -07006591 *
6592 * RETURNS:
Tejun Heof3187192007-04-17 23:44:07 +09006593 * 0 on success, -errno otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006594 */
Tejun Heof3187192007-04-17 23:44:07 +09006595int ata_host_register(struct ata_host *host, struct scsi_host_template *sht)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006596{
Tejun Heof3187192007-04-17 23:44:07 +09006597 int i, rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006598
Jens Axboe69278f72018-05-11 12:51:10 -06006599 host->n_tags = clamp(sht->can_queue, 1, ATA_MAX_QUEUE);
Kevin Hao1871ee12014-07-12 12:08:24 +08006600
Tejun Heof3187192007-04-17 23:44:07 +09006601 /* host must have been started */
6602 if (!(host->flags & ATA_HOST_STARTED)) {
Joe Perchesa44fec12011-04-15 15:51:58 -07006603 dev_err(host->dev, "BUG: trying to register unstarted host\n");
Tejun Heof3187192007-04-17 23:44:07 +09006604 WARN_ON(1);
6605 return -EINVAL;
Alan Cox02f076a2006-09-26 17:35:32 +01006606 }
Tejun Heof0d36ef2007-01-20 16:00:28 +09006607
Tejun Heof3187192007-04-17 23:44:07 +09006608 /* Blow away unused ports. This happens when LLD can't
6609 * determine the exact number of ports to allocate at
6610 * allocation time.
6611 */
6612 for (i = host->n_ports; host->ports[i]; i++)
6613 kfree(host->ports[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006614
Tejun Heof3187192007-04-17 23:44:07 +09006615 /* give ports names and add SCSI hosts */
David Milburne628dc92013-05-14 13:48:40 -05006616 for (i = 0; i < host->n_ports; i++) {
Dan Williams85d67252012-03-10 23:28:46 -08006617 host->ports[i]->print_id = atomic_inc_return(&ata_print_id);
David Milburne628dc92013-05-14 13:48:40 -05006618 host->ports[i]->local_port_no = i + 1;
6619 }
Jeff Garzik4fca3772011-02-15 01:13:24 -05006620
Gwendal Grignoud9027472010-05-25 12:31:38 -07006621 /* Create associated sysfs transport objects */
6622 for (i = 0; i < host->n_ports; i++) {
6623 rc = ata_tport_add(host->dev,host->ports[i]);
6624 if (rc) {
6625 goto err_tadd;
6626 }
6627 }
6628
Tejun Heof3187192007-04-17 23:44:07 +09006629 rc = ata_scsi_add_hosts(host, sht);
Tejun Heoecef7252007-04-17 23:44:06 +09006630 if (rc)
Gwendal Grignoud9027472010-05-25 12:31:38 -07006631 goto err_tadd;
Tejun Heoecef7252007-04-17 23:44:06 +09006632
Tejun Heof3187192007-04-17 23:44:07 +09006633 /* set cable, sata_spd_limit and report */
Jeff Garzikcca39742006-08-24 03:19:22 -04006634 for (i = 0; i < host->n_ports; i++) {
6635 struct ata_port *ap = host->ports[i];
Tejun Heof3187192007-04-17 23:44:07 +09006636 unsigned long xfer_mask;
6637
6638 /* set SATA cable type if still unset */
6639 if (ap->cbl == ATA_CBL_NONE && (ap->flags & ATA_FLAG_SATA))
6640 ap->cbl = ATA_CBL_SATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006641
Tejun Heo5a04bf42006-05-31 18:27:38 +09006642 /* init sata_spd_limit to the current value */
Tejun Heo4fb37a22007-08-06 18:36:23 +09006643 sata_link_init_spd(&ap->link);
Tejun Heob1c72912008-07-31 17:02:43 +09006644 if (ap->slave_link)
6645 sata_link_init_spd(ap->slave_link);
Tejun Heo5a04bf42006-05-31 18:27:38 +09006646
Tejun Heocbcdd872007-08-18 13:14:55 +09006647 /* print per-port info to dmesg */
Tejun Heof3187192007-04-17 23:44:07 +09006648 xfer_mask = ata_pack_xfermask(ap->pio_mask, ap->mwdma_mask,
6649 ap->udma_mask);
6650
Tejun Heoabf6e8e2007-10-09 14:57:25 +09006651 if (!ata_port_is_dummy(ap)) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07006652 ata_port_info(ap, "%cATA max %s %s\n",
6653 (ap->flags & ATA_FLAG_SATA) ? 'S' : 'P',
6654 ata_mode_string(xfer_mask),
6655 ap->link.eh_info.desc);
Tejun Heoabf6e8e2007-10-09 14:57:25 +09006656 ata_ehi_clear_desc(&ap->link.eh_info);
6657 } else
Joe Perchesa9a79df2011-04-15 15:51:59 -07006658 ata_port_info(ap, "DUMMY\n");
Tejun Heof3187192007-04-17 23:44:07 +09006659 }
6660
Vegard Nossumf6005352009-04-08 18:19:39 +02006661 /* perform each probe asynchronously */
Tejun Heof3187192007-04-17 23:44:07 +09006662 for (i = 0; i < host->n_ports; i++) {
6663 struct ata_port *ap = host->ports[i];
Arjan van de Ven79318052009-01-04 05:32:28 -08006664 async_schedule(async_port_probe, ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006665 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006666
Tejun Heof3187192007-04-17 23:44:07 +09006667 return 0;
Gwendal Grignoud9027472010-05-25 12:31:38 -07006668
6669 err_tadd:
6670 while (--i >= 0) {
6671 ata_tport_delete(host->ports[i]);
6672 }
6673 return rc;
6674
Tejun Heof3187192007-04-17 23:44:07 +09006675}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01006676EXPORT_SYMBOL_GPL(ata_host_register);
Tejun Heof3187192007-04-17 23:44:07 +09006677
6678/**
Tejun Heof5cda252007-04-17 23:44:07 +09006679 * ata_host_activate - start host, request IRQ and register it
6680 * @host: target ATA host
6681 * @irq: IRQ to request
6682 * @irq_handler: irq_handler used when requesting IRQ
6683 * @irq_flags: irq_flags used when requesting IRQ
6684 * @sht: scsi_host_template to use when registering the host
6685 *
6686 * After allocating an ATA host and initializing it, most libata
6687 * LLDs perform three steps to activate the host - start host,
Masanari Iidac9b55602016-04-13 23:36:27 +09006688 * request IRQ and register it. This helper takes necessary
Tejun Heof5cda252007-04-17 23:44:07 +09006689 * arguments and performs the three steps in one go.
6690 *
Paul Mundt3d46b2e2007-11-08 11:14:56 +09006691 * An invalid IRQ skips the IRQ registration and expects the host to
6692 * have set polling mode on the port. In this case, @irq_handler
6693 * should be NULL.
6694 *
Tejun Heof5cda252007-04-17 23:44:07 +09006695 * LOCKING:
6696 * Inherited from calling layer (may sleep).
6697 *
6698 * RETURNS:
6699 * 0 on success, -errno otherwise.
6700 */
6701int ata_host_activate(struct ata_host *host, int irq,
6702 irq_handler_t irq_handler, unsigned long irq_flags,
6703 struct scsi_host_template *sht)
6704{
Tejun Heocbcdd872007-08-18 13:14:55 +09006705 int i, rc;
Heiner Kallweit7e22c002015-12-06 21:56:33 +01006706 char *irq_desc;
Tejun Heof5cda252007-04-17 23:44:07 +09006707
6708 rc = ata_host_start(host);
6709 if (rc)
6710 return rc;
6711
Paul Mundt3d46b2e2007-11-08 11:14:56 +09006712 /* Special case for polling mode */
6713 if (!irq) {
6714 WARN_ON(irq_handler);
6715 return ata_host_register(host, sht);
6716 }
6717
Heiner Kallweit7e22c002015-12-06 21:56:33 +01006718 irq_desc = devm_kasprintf(host->dev, GFP_KERNEL, "%s[%s]",
6719 dev_driver_string(host->dev),
6720 dev_name(host->dev));
6721 if (!irq_desc)
6722 return -ENOMEM;
6723
Tejun Heof5cda252007-04-17 23:44:07 +09006724 rc = devm_request_irq(host->dev, irq, irq_handler, irq_flags,
Heiner Kallweit7e22c002015-12-06 21:56:33 +01006725 irq_desc, host);
Tejun Heof5cda252007-04-17 23:44:07 +09006726 if (rc)
6727 return rc;
6728
Tejun Heocbcdd872007-08-18 13:14:55 +09006729 for (i = 0; i < host->n_ports; i++)
6730 ata_port_desc(host->ports[i], "irq %d", irq);
Tejun Heo40318262007-07-03 01:38:47 +09006731
Tejun Heof5cda252007-04-17 23:44:07 +09006732 rc = ata_host_register(host, sht);
6733 /* if failed, just free the IRQ and leave ports alone */
6734 if (rc)
6735 devm_free_irq(host->dev, irq, host);
6736
6737 return rc;
6738}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01006739EXPORT_SYMBOL_GPL(ata_host_activate);
Tejun Heof5cda252007-04-17 23:44:07 +09006740
6741/**
Masanari Iidac9b55602016-04-13 23:36:27 +09006742 * ata_port_detach - Detach ATA port in preparation of device removal
Tejun Heo720ba122006-05-31 18:28:13 +09006743 * @ap: ATA port to be detached
6744 *
6745 * Detach all ATA devices and the associated SCSI devices of @ap;
6746 * then, remove the associated SCSI host. @ap is guaranteed to
6747 * be quiescent on return from this function.
6748 *
6749 * LOCKING:
6750 * Kernel thread context (may sleep).
6751 */
Adrian Bunk741b7762007-10-24 18:23:06 +02006752static void ata_port_detach(struct ata_port *ap)
Tejun Heo720ba122006-05-31 18:28:13 +09006753{
6754 unsigned long flags;
Levente Kurusaa6f9bf42014-05-06 15:57:48 +02006755 struct ata_link *link;
6756 struct ata_device *dev;
Tejun Heo720ba122006-05-31 18:28:13 +09006757
6758 if (!ap->ops->error_handler)
Tejun Heoc3cf30a2006-08-05 03:59:11 +09006759 goto skip_eh;
Tejun Heo720ba122006-05-31 18:28:13 +09006760
6761 /* tell EH we're leaving & flush EH */
Jeff Garzikba6a1302006-06-22 23:46:10 -04006762 spin_lock_irqsave(ap->lock, flags);
Tejun Heob51e9e52006-06-29 01:29:30 +09006763 ap->pflags |= ATA_PFLAG_UNLOADING;
Tejun Heoece180d2008-11-03 20:04:37 +09006764 ata_port_schedule_eh(ap);
Jeff Garzikba6a1302006-06-22 23:46:10 -04006765 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heo720ba122006-05-31 18:28:13 +09006766
Tejun Heoece180d2008-11-03 20:04:37 +09006767 /* wait till EH commits suicide */
Tejun Heo720ba122006-05-31 18:28:13 +09006768 ata_port_wait_eh(ap);
6769
Tejun Heoece180d2008-11-03 20:04:37 +09006770 /* it better be dead now */
6771 WARN_ON(!(ap->pflags & ATA_PFLAG_UNLOADED));
Tejun Heo720ba122006-05-31 18:28:13 +09006772
Tejun Heoafe2c512010-12-14 16:21:17 +01006773 cancel_delayed_work_sync(&ap->hotplug_task);
Tejun Heo720ba122006-05-31 18:28:13 +09006774
Tejun Heoc3cf30a2006-08-05 03:59:11 +09006775 skip_eh:
Levente Kurusaa6f9bf42014-05-06 15:57:48 +02006776 /* clean up zpodd on port removal */
6777 ata_for_each_link(link, ap, HOST_FIRST) {
6778 ata_for_each_dev(dev, link, ALL) {
6779 if (zpodd_dev_enabled(dev))
6780 zpodd_exit(dev);
6781 }
6782 }
Gwendal Grignoud9027472010-05-25 12:31:38 -07006783 if (ap->pmp_link) {
6784 int i;
6785 for (i = 0; i < SATA_PMP_MAX_PORTS; i++)
6786 ata_tlink_delete(&ap->pmp_link[i]);
6787 }
Tejun Heo720ba122006-05-31 18:28:13 +09006788 /* remove the associated SCSI host */
Jeff Garzikcca39742006-08-24 03:19:22 -04006789 scsi_remove_host(ap->scsi_host);
Rafael J. Wysockic5700762013-11-25 13:19:01 +01006790 ata_tport_delete(ap);
Tejun Heo720ba122006-05-31 18:28:13 +09006791}
6792
6793/**
Tejun Heo0529c1592007-01-20 16:00:26 +09006794 * ata_host_detach - Detach all ports of an ATA host
6795 * @host: Host to detach
6796 *
6797 * Detach all ports of @host.
6798 *
6799 * LOCKING:
6800 * Kernel thread context (may sleep).
6801 */
6802void ata_host_detach(struct ata_host *host)
6803{
6804 int i;
6805
John Garry130f4ca2019-10-16 18:19:52 +08006806 /* Ensure ata_port probe has completed */
6807 async_synchronize_full();
6808
Tejun Heo0529c1592007-01-20 16:00:26 +09006809 for (i = 0; i < host->n_ports; i++)
6810 ata_port_detach(host->ports[i]);
Tejun Heo562f0c22007-12-15 15:05:01 +09006811
6812 /* the host is dead now, dissociate ACPI */
6813 ata_acpi_dissociate(host);
Tejun Heo0529c1592007-01-20 16:00:26 +09006814}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01006815EXPORT_SYMBOL_GPL(ata_host_detach);
Tejun Heo0529c1592007-01-20 16:00:26 +09006816
Jeff Garzik374b1872005-08-30 05:42:52 -04006817#ifdef CONFIG_PCI
6818
Edward Falk0baab862005-06-02 18:17:13 -04006819/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07006820 * ata_pci_remove_one - PCI layer callback for device removal
6821 * @pdev: PCI device that was removed
6822 *
Tejun Heob878ca52007-01-20 16:00:28 +09006823 * PCI layer indicates to libata via this hook that hot-unplug or
6824 * module unload event has occurred. Detach all ports. Resource
6825 * release is handled via devres.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006826 *
6827 * LOCKING:
6828 * Inherited from PCI layer (may sleep).
6829 */
Tejun Heof0d36ef2007-01-20 16:00:28 +09006830void ata_pci_remove_one(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006831{
Brian Norris04a3f5b2012-12-03 10:34:41 -08006832 struct ata_host *host = pci_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006833
Tejun Heob878ca52007-01-20 16:00:28 +09006834 ata_host_detach(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006835}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01006836EXPORT_SYMBOL_GPL(ata_pci_remove_one);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006837
Prabhakar Kushwaha10a663a2020-01-25 03:37:29 +00006838void ata_pci_shutdown_one(struct pci_dev *pdev)
6839{
6840 struct ata_host *host = pci_get_drvdata(pdev);
6841 int i;
6842
6843 for (i = 0; i < host->n_ports; i++) {
6844 struct ata_port *ap = host->ports[i];
6845
6846 ap->pflags |= ATA_PFLAG_FROZEN;
6847
6848 /* Disable port interrupts */
6849 if (ap->ops->freeze)
6850 ap->ops->freeze(ap);
6851
6852 /* Stop the port DMA engines */
6853 if (ap->ops->port_stop)
6854 ap->ops->port_stop(ap);
6855 }
6856}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01006857EXPORT_SYMBOL_GPL(ata_pci_shutdown_one);
Prabhakar Kushwaha10a663a2020-01-25 03:37:29 +00006858
Linus Torvalds1da177e2005-04-16 15:20:36 -07006859/* move to PCI subsystem */
Jeff Garzik057ace52005-10-22 14:27:05 -04006860int pci_test_config_bits(struct pci_dev *pdev, const struct pci_bits *bits)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006861{
6862 unsigned long tmp = 0;
6863
6864 switch (bits->width) {
6865 case 1: {
6866 u8 tmp8 = 0;
6867 pci_read_config_byte(pdev, bits->reg, &tmp8);
6868 tmp = tmp8;
6869 break;
6870 }
6871 case 2: {
6872 u16 tmp16 = 0;
6873 pci_read_config_word(pdev, bits->reg, &tmp16);
6874 tmp = tmp16;
6875 break;
6876 }
6877 case 4: {
6878 u32 tmp32 = 0;
6879 pci_read_config_dword(pdev, bits->reg, &tmp32);
6880 tmp = tmp32;
6881 break;
6882 }
6883
6884 default:
6885 return -EINVAL;
6886 }
6887
6888 tmp &= bits->mask;
6889
6890 return (tmp == bits->val) ? 1 : 0;
6891}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01006892EXPORT_SYMBOL_GPL(pci_test_config_bits);
Jens Axboe9b847542006-01-06 09:28:07 +01006893
Tejun Heo6ffa01d2007-03-02 17:32:47 +09006894#ifdef CONFIG_PM
Tejun Heo3c5100c2006-07-26 16:58:33 +09006895void ata_pci_device_do_suspend(struct pci_dev *pdev, pm_message_t mesg)
Jens Axboe9b847542006-01-06 09:28:07 +01006896{
6897 pci_save_state(pdev);
Tejun Heo4c90d972007-02-20 18:14:48 +09006898 pci_disable_device(pdev);
Tejun Heo500530f2006-07-03 16:07:27 +09006899
Rafael J. Wysocki3a2d5b72008-02-23 19:13:25 +01006900 if (mesg.event & PM_EVENT_SLEEP)
Tejun Heo500530f2006-07-03 16:07:27 +09006901 pci_set_power_state(pdev, PCI_D3hot);
Jens Axboe9b847542006-01-06 09:28:07 +01006902}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01006903EXPORT_SYMBOL_GPL(ata_pci_device_do_suspend);
Jens Axboe9b847542006-01-06 09:28:07 +01006904
Tejun Heo553c4aa2006-12-26 19:39:50 +09006905int ata_pci_device_do_resume(struct pci_dev *pdev)
Jens Axboe9b847542006-01-06 09:28:07 +01006906{
Tejun Heo553c4aa2006-12-26 19:39:50 +09006907 int rc;
6908
Jens Axboe9b847542006-01-06 09:28:07 +01006909 pci_set_power_state(pdev, PCI_D0);
6910 pci_restore_state(pdev);
Tejun Heo553c4aa2006-12-26 19:39:50 +09006911
Tejun Heob878ca52007-01-20 16:00:28 +09006912 rc = pcim_enable_device(pdev);
Tejun Heo553c4aa2006-12-26 19:39:50 +09006913 if (rc) {
Joe Perchesa44fec12011-04-15 15:51:58 -07006914 dev_err(&pdev->dev,
6915 "failed to enable device after resume (%d)\n", rc);
Tejun Heo553c4aa2006-12-26 19:39:50 +09006916 return rc;
6917 }
6918
Jens Axboe9b847542006-01-06 09:28:07 +01006919 pci_set_master(pdev);
Tejun Heo553c4aa2006-12-26 19:39:50 +09006920 return 0;
Tejun Heo500530f2006-07-03 16:07:27 +09006921}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01006922EXPORT_SYMBOL_GPL(ata_pci_device_do_resume);
Tejun Heo500530f2006-07-03 16:07:27 +09006923
Tejun Heo3c5100c2006-07-26 16:58:33 +09006924int ata_pci_device_suspend(struct pci_dev *pdev, pm_message_t mesg)
Tejun Heo500530f2006-07-03 16:07:27 +09006925{
Brian Norris04a3f5b2012-12-03 10:34:41 -08006926 struct ata_host *host = pci_get_drvdata(pdev);
Tejun Heo500530f2006-07-03 16:07:27 +09006927 int rc = 0;
6928
Jeff Garzikcca39742006-08-24 03:19:22 -04006929 rc = ata_host_suspend(host, mesg);
Tejun Heo500530f2006-07-03 16:07:27 +09006930 if (rc)
6931 return rc;
6932
Tejun Heo3c5100c2006-07-26 16:58:33 +09006933 ata_pci_device_do_suspend(pdev, mesg);
Tejun Heo500530f2006-07-03 16:07:27 +09006934
6935 return 0;
6936}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01006937EXPORT_SYMBOL_GPL(ata_pci_device_suspend);
Tejun Heo500530f2006-07-03 16:07:27 +09006938
6939int ata_pci_device_resume(struct pci_dev *pdev)
6940{
Brian Norris04a3f5b2012-12-03 10:34:41 -08006941 struct ata_host *host = pci_get_drvdata(pdev);
Tejun Heo553c4aa2006-12-26 19:39:50 +09006942 int rc;
Tejun Heo500530f2006-07-03 16:07:27 +09006943
Tejun Heo553c4aa2006-12-26 19:39:50 +09006944 rc = ata_pci_device_do_resume(pdev);
6945 if (rc == 0)
6946 ata_host_resume(host);
6947 return rc;
Jens Axboe9b847542006-01-06 09:28:07 +01006948}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01006949EXPORT_SYMBOL_GPL(ata_pci_device_resume);
Tejun Heo6ffa01d2007-03-02 17:32:47 +09006950#endif /* CONFIG_PM */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006951#endif /* CONFIG_PCI */
6952
Brian Norrisb7db04d2012-11-02 12:29:32 -07006953/**
6954 * ata_platform_remove_one - Platform layer callback for device removal
6955 * @pdev: Platform device that was removed
6956 *
6957 * Platform layer indicates to libata via this hook that hot-unplug or
6958 * module unload event has occurred. Detach all ports. Resource
6959 * release is handled via devres.
6960 *
6961 * LOCKING:
6962 * Inherited from platform layer (may sleep).
6963 */
6964int ata_platform_remove_one(struct platform_device *pdev)
6965{
6966 struct ata_host *host = platform_get_drvdata(pdev);
6967
6968 ata_host_detach(host);
6969
6970 return 0;
6971}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01006972EXPORT_SYMBOL_GPL(ata_platform_remove_one);
Brian Norrisb7db04d2012-11-02 12:29:32 -07006973
Tejun Heo33267322008-02-13 09:15:09 +09006974static int __init ata_parse_force_one(char **cur,
6975 struct ata_force_ent *force_ent,
6976 const char **reason)
6977{
Rasmus Villemoes0f5f2642015-06-09 15:33:19 +02006978 static const struct ata_force_param force_tbl[] __initconst = {
Tejun Heo33267322008-02-13 09:15:09 +09006979 { "40c", .cbl = ATA_CBL_PATA40 },
6980 { "80c", .cbl = ATA_CBL_PATA80 },
6981 { "short40c", .cbl = ATA_CBL_PATA40_SHORT },
6982 { "unk", .cbl = ATA_CBL_PATA_UNK },
6983 { "ign", .cbl = ATA_CBL_PATA_IGN },
6984 { "sata", .cbl = ATA_CBL_SATA },
6985 { "1.5Gbps", .spd_limit = 1 },
6986 { "3.0Gbps", .spd_limit = 2 },
6987 { "noncq", .horkage_on = ATA_HORKAGE_NONCQ },
6988 { "ncq", .horkage_off = ATA_HORKAGE_NONCQ },
Martin K. Petersend7b16e42015-05-04 21:54:18 -04006989 { "noncqtrim", .horkage_on = ATA_HORKAGE_NO_NCQ_TRIM },
6990 { "ncqtrim", .horkage_off = ATA_HORKAGE_NO_NCQ_TRIM },
Tejun Heo43c9c592010-05-23 12:59:11 +02006991 { "dump_id", .horkage_on = ATA_HORKAGE_DUMP_ID },
Tejun Heo33267322008-02-13 09:15:09 +09006992 { "pio0", .xfer_mask = 1 << (ATA_SHIFT_PIO + 0) },
6993 { "pio1", .xfer_mask = 1 << (ATA_SHIFT_PIO + 1) },
6994 { "pio2", .xfer_mask = 1 << (ATA_SHIFT_PIO + 2) },
6995 { "pio3", .xfer_mask = 1 << (ATA_SHIFT_PIO + 3) },
6996 { "pio4", .xfer_mask = 1 << (ATA_SHIFT_PIO + 4) },
6997 { "pio5", .xfer_mask = 1 << (ATA_SHIFT_PIO + 5) },
6998 { "pio6", .xfer_mask = 1 << (ATA_SHIFT_PIO + 6) },
6999 { "mwdma0", .xfer_mask = 1 << (ATA_SHIFT_MWDMA + 0) },
7000 { "mwdma1", .xfer_mask = 1 << (ATA_SHIFT_MWDMA + 1) },
7001 { "mwdma2", .xfer_mask = 1 << (ATA_SHIFT_MWDMA + 2) },
7002 { "mwdma3", .xfer_mask = 1 << (ATA_SHIFT_MWDMA + 3) },
7003 { "mwdma4", .xfer_mask = 1 << (ATA_SHIFT_MWDMA + 4) },
7004 { "udma0", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 0) },
7005 { "udma16", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 0) },
7006 { "udma/16", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 0) },
7007 { "udma1", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 1) },
7008 { "udma25", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 1) },
7009 { "udma/25", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 1) },
7010 { "udma2", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 2) },
7011 { "udma33", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 2) },
7012 { "udma/33", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 2) },
7013 { "udma3", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 3) },
7014 { "udma44", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 3) },
7015 { "udma/44", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 3) },
7016 { "udma4", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 4) },
7017 { "udma66", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 4) },
7018 { "udma/66", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 4) },
7019 { "udma5", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 5) },
7020 { "udma100", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 5) },
7021 { "udma/100", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 5) },
7022 { "udma6", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 6) },
7023 { "udma133", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 6) },
7024 { "udma/133", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 6) },
7025 { "udma7", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 7) },
Tejun Heo05944bd2008-08-13 20:19:09 +09007026 { "nohrst", .lflags = ATA_LFLAG_NO_HRST },
7027 { "nosrst", .lflags = ATA_LFLAG_NO_SRST },
7028 { "norst", .lflags = ATA_LFLAG_NO_HRST | ATA_LFLAG_NO_SRST },
Dan Williamsca6d43b2012-06-21 23:41:41 -07007029 { "rstonce", .lflags = ATA_LFLAG_RST_ONCE },
Vincent Pelletier966fbe12013-05-21 22:30:58 +02007030 { "atapi_dmadir", .horkage_on = ATA_HORKAGE_ATAPI_DMADIR },
Robin H. Johnsonb8bd6dc2013-12-16 09:31:19 -08007031 { "disable", .horkage_on = ATA_HORKAGE_DISABLE },
Tejun Heo33267322008-02-13 09:15:09 +09007032 };
7033 char *start = *cur, *p = *cur;
7034 char *id, *val, *endp;
7035 const struct ata_force_param *match_fp = NULL;
7036 int nr_matches = 0, i;
7037
7038 /* find where this param ends and update *cur */
7039 while (*p != '\0' && *p != ',')
7040 p++;
7041
7042 if (*p == '\0')
7043 *cur = p;
7044 else
7045 *cur = p + 1;
7046
7047 *p = '\0';
7048
7049 /* parse */
7050 p = strchr(start, ':');
7051 if (!p) {
7052 val = strstrip(start);
7053 goto parse_val;
7054 }
7055 *p = '\0';
7056
7057 id = strstrip(start);
7058 val = strstrip(p + 1);
7059
7060 /* parse id */
7061 p = strchr(id, '.');
7062 if (p) {
7063 *p++ = '\0';
7064 force_ent->device = simple_strtoul(p, &endp, 10);
7065 if (p == endp || *endp != '\0') {
7066 *reason = "invalid device";
7067 return -EINVAL;
7068 }
7069 }
7070
7071 force_ent->port = simple_strtoul(id, &endp, 10);
Tejun Heof7cf69a2017-05-31 14:26:26 -04007072 if (id == endp || *endp != '\0') {
Tejun Heo33267322008-02-13 09:15:09 +09007073 *reason = "invalid port/link";
7074 return -EINVAL;
7075 }
7076
7077 parse_val:
7078 /* parse val, allow shortcuts so that both 1.5 and 1.5Gbps work */
7079 for (i = 0; i < ARRAY_SIZE(force_tbl); i++) {
7080 const struct ata_force_param *fp = &force_tbl[i];
7081
7082 if (strncasecmp(val, fp->name, strlen(val)))
7083 continue;
7084
7085 nr_matches++;
7086 match_fp = fp;
7087
7088 if (strcasecmp(val, fp->name) == 0) {
7089 nr_matches = 1;
7090 break;
7091 }
7092 }
7093
7094 if (!nr_matches) {
7095 *reason = "unknown value";
7096 return -EINVAL;
7097 }
7098 if (nr_matches > 1) {
Arvind Yadav9de55352017-10-28 23:51:47 +05307099 *reason = "ambiguous value";
Tejun Heo33267322008-02-13 09:15:09 +09007100 return -EINVAL;
7101 }
7102
7103 force_ent->param = *match_fp;
7104
7105 return 0;
7106}
7107
7108static void __init ata_parse_force_param(void)
7109{
7110 int idx = 0, size = 1;
7111 int last_port = -1, last_device = -1;
7112 char *p, *cur, *next;
7113
7114 /* calculate maximum number of params and allocate force_tbl */
7115 for (p = ata_force_param_buf; *p; p++)
7116 if (*p == ',')
7117 size++;
7118
Kees Cook6396bb22018-06-12 14:03:40 -07007119 ata_force_tbl = kcalloc(size, sizeof(ata_force_tbl[0]), GFP_KERNEL);
Tejun Heo33267322008-02-13 09:15:09 +09007120 if (!ata_force_tbl) {
7121 printk(KERN_WARNING "ata: failed to extend force table, "
7122 "libata.force ignored\n");
7123 return;
7124 }
7125
7126 /* parse and populate the table */
7127 for (cur = ata_force_param_buf; *cur != '\0'; cur = next) {
7128 const char *reason = "";
7129 struct ata_force_ent te = { .port = -1, .device = -1 };
7130
7131 next = cur;
7132 if (ata_parse_force_one(&next, &te, &reason)) {
7133 printk(KERN_WARNING "ata: failed to parse force "
7134 "parameter \"%s\" (%s)\n",
7135 cur, reason);
7136 continue;
7137 }
7138
7139 if (te.port == -1) {
7140 te.port = last_port;
7141 te.device = last_device;
7142 }
7143
7144 ata_force_tbl[idx++] = te;
7145
7146 last_port = te.port;
7147 last_device = te.device;
7148 }
7149
7150 ata_force_tbl_size = idx;
7151}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007152
Linus Torvalds1da177e2005-04-16 15:20:36 -07007153static int __init ata_init(void)
7154{
Gwendal Grignoud9027472010-05-25 12:31:38 -07007155 int rc;
Tejun Heo270390e2010-05-10 21:41:35 +02007156
Tejun Heo33267322008-02-13 09:15:09 +09007157 ata_parse_force_param();
7158
Tejun Heo270390e2010-05-10 21:41:35 +02007159 rc = ata_sff_init();
Tejun Heoad72cf92010-07-02 10:03:52 +02007160 if (rc) {
7161 kfree(ata_force_tbl);
7162 return rc;
7163 }
Tejun Heo453b07a2006-05-31 18:27:42 +09007164
Gwendal Grignoud9027472010-05-25 12:31:38 -07007165 libata_transport_init();
7166 ata_scsi_transport_template = ata_attach_transport();
7167 if (!ata_scsi_transport_template) {
7168 ata_sff_exit();
7169 rc = -ENOMEM;
7170 goto err_out;
Jeff Garzik4fca3772011-02-15 01:13:24 -05007171 }
Gwendal Grignoud9027472010-05-25 12:31:38 -07007172
Linus Torvalds1da177e2005-04-16 15:20:36 -07007173 printk(KERN_DEBUG "libata version " DRV_VERSION " loaded.\n");
7174 return 0;
Gwendal Grignoud9027472010-05-25 12:31:38 -07007175
7176err_out:
7177 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007178}
7179
7180static void __exit ata_exit(void)
7181{
Gwendal Grignoud9027472010-05-25 12:31:38 -07007182 ata_release_transport(ata_scsi_transport_template);
7183 libata_transport_exit();
Tejun Heo270390e2010-05-10 21:41:35 +02007184 ata_sff_exit();
Tejun Heo33267322008-02-13 09:15:09 +09007185 kfree(ata_force_tbl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007186}
7187
Brian Kinga4625082006-11-13 16:32:36 -06007188subsys_initcall(ata_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007189module_exit(ata_exit);
7190
Akinobu Mita9990b6f2010-04-12 21:11:41 +09007191static DEFINE_RATELIMIT_STATE(ratelimit, HZ / 5, 1);
Jeff Garzik67846b32005-10-05 02:58:32 -04007192
7193int ata_ratelimit(void)
7194{
Akinobu Mita9990b6f2010-04-12 21:11:41 +09007195 return __ratelimit(&ratelimit);
Jeff Garzik67846b32005-10-05 02:58:32 -04007196}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01007197EXPORT_SYMBOL_GPL(ata_ratelimit);
Jeff Garzik67846b32005-10-05 02:58:32 -04007198
Tejun Heoc0c362b2010-09-06 17:57:14 +02007199/**
7200 * ata_msleep - ATA EH owner aware msleep
7201 * @ap: ATA port to attribute the sleep to
7202 * @msecs: duration to sleep in milliseconds
7203 *
7204 * Sleeps @msecs. If the current task is owner of @ap's EH, the
7205 * ownership is released before going to sleep and reacquired
7206 * after the sleep is complete. IOW, other ports sharing the
7207 * @ap->host will be allowed to own the EH while this task is
7208 * sleeping.
7209 *
7210 * LOCKING:
7211 * Might sleep.
7212 */
Tejun Heo97750ce2010-09-06 17:56:29 +02007213void ata_msleep(struct ata_port *ap, unsigned int msecs)
7214{
Tejun Heoc0c362b2010-09-06 17:57:14 +02007215 bool owns_eh = ap && ap->host->eh_owner == current;
7216
7217 if (owns_eh)
7218 ata_eh_release(ap);
7219
Anil Veliyankara Madam848c3922016-01-07 21:18:52 -08007220 if (msecs < 20) {
7221 unsigned long usecs = msecs * USEC_PER_MSEC;
7222 usleep_range(usecs, usecs + 50);
7223 } else {
7224 msleep(msecs);
7225 }
Tejun Heoc0c362b2010-09-06 17:57:14 +02007226
7227 if (owns_eh)
7228 ata_eh_acquire(ap);
Tejun Heo97750ce2010-09-06 17:56:29 +02007229}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01007230EXPORT_SYMBOL_GPL(ata_msleep);
Tejun Heo97750ce2010-09-06 17:56:29 +02007231
Tejun Heoc22daff2006-04-11 22:22:29 +09007232/**
7233 * ata_wait_register - wait until register value changes
Tejun Heo97750ce2010-09-06 17:56:29 +02007234 * @ap: ATA port to wait register for, can be NULL
Tejun Heoc22daff2006-04-11 22:22:29 +09007235 * @reg: IO-mapped register
7236 * @mask: Mask to apply to read register value
7237 * @val: Wait condition
Tejun Heo341c2c92008-05-20 02:17:51 +09007238 * @interval: polling interval in milliseconds
7239 * @timeout: timeout in milliseconds
Tejun Heoc22daff2006-04-11 22:22:29 +09007240 *
7241 * Waiting for some bits of register to change is a common
7242 * operation for ATA controllers. This function reads 32bit LE
7243 * IO-mapped register @reg and tests for the following condition.
7244 *
7245 * (*@reg & mask) != val
7246 *
7247 * If the condition is met, it returns; otherwise, the process is
7248 * repeated after @interval_msec until timeout.
7249 *
7250 * LOCKING:
7251 * Kernel thread context (may sleep)
7252 *
7253 * RETURNS:
7254 * The final register value.
7255 */
Tejun Heo97750ce2010-09-06 17:56:29 +02007256u32 ata_wait_register(struct ata_port *ap, void __iomem *reg, u32 mask, u32 val,
Tejun Heo341c2c92008-05-20 02:17:51 +09007257 unsigned long interval, unsigned long timeout)
Tejun Heoc22daff2006-04-11 22:22:29 +09007258{
Tejun Heo341c2c92008-05-20 02:17:51 +09007259 unsigned long deadline;
Tejun Heoc22daff2006-04-11 22:22:29 +09007260 u32 tmp;
7261
7262 tmp = ioread32(reg);
7263
7264 /* Calculate timeout _after_ the first read to make sure
7265 * preceding writes reach the controller before starting to
7266 * eat away the timeout.
7267 */
Tejun Heo341c2c92008-05-20 02:17:51 +09007268 deadline = ata_deadline(jiffies, timeout);
Tejun Heoc22daff2006-04-11 22:22:29 +09007269
Tejun Heo341c2c92008-05-20 02:17:51 +09007270 while ((tmp & mask) == val && time_before(jiffies, deadline)) {
Tejun Heo97750ce2010-09-06 17:56:29 +02007271 ata_msleep(ap, interval);
Tejun Heoc22daff2006-04-11 22:22:29 +09007272 tmp = ioread32(reg);
7273 }
7274
7275 return tmp;
7276}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01007277EXPORT_SYMBOL_GPL(ata_wait_register);
Tejun Heoc22daff2006-04-11 22:22:29 +09007278
Gabriele Mazzotta8393b812015-04-25 19:52:36 +02007279/**
7280 * sata_lpm_ignore_phy_events - test if PHY event should be ignored
7281 * @link: Link receiving the event
7282 *
7283 * Test whether the received PHY event has to be ignored or not.
7284 *
7285 * LOCKING:
7286 * None:
7287 *
7288 * RETURNS:
7289 * True if the event has to be ignored.
7290 */
7291bool sata_lpm_ignore_phy_events(struct ata_link *link)
7292{
Gabriele Mazzotta09c5b482015-04-25 19:52:37 +02007293 unsigned long lpm_timeout = link->last_lpm_change +
7294 msecs_to_jiffies(ATA_TMOUT_SPURIOUS_PHY);
7295
Gabriele Mazzotta8393b812015-04-25 19:52:36 +02007296 /* if LPM is enabled, PHYRDY doesn't mean anything */
Gabriele Mazzotta09c5b482015-04-25 19:52:37 +02007297 if (link->lpm_policy > ATA_LPM_MAX_POWER)
7298 return true;
7299
7300 /* ignore the first PHY event after the LPM policy changed
7301 * as it is might be spurious
7302 */
7303 if ((link->flags & ATA_LFLAG_CHANGED) &&
7304 time_before(jiffies, lpm_timeout))
7305 return true;
7306
7307 return false;
Gabriele Mazzotta8393b812015-04-25 19:52:36 +02007308}
7309EXPORT_SYMBOL_GPL(sata_lpm_ignore_phy_events);
7310
Linus Torvalds1da177e2005-04-16 15:20:36 -07007311/*
Tejun Heodd5b06c2006-08-10 16:59:12 +09007312 * Dummy port_ops
7313 */
Tejun Heodd5b06c2006-08-10 16:59:12 +09007314static unsigned int ata_dummy_qc_issue(struct ata_queued_cmd *qc)
7315{
7316 return AC_ERR_SYSTEM;
7317}
7318
Tejun Heo182d7bb2008-04-07 22:47:21 +09007319static void ata_dummy_error_handler(struct ata_port *ap)
7320{
7321 /* truly dummy */
7322}
7323
Tejun Heo029cfd62008-03-25 12:22:49 +09007324struct ata_port_operations ata_dummy_port_ops = {
Tejun Heodd5b06c2006-08-10 16:59:12 +09007325 .qc_prep = ata_noop_qc_prep,
7326 .qc_issue = ata_dummy_qc_issue,
Tejun Heo182d7bb2008-04-07 22:47:21 +09007327 .error_handler = ata_dummy_error_handler,
Dan Williamse4a9c372012-06-21 23:25:27 -07007328 .sched_eh = ata_std_sched_eh,
7329 .end_eh = ata_std_end_eh,
Tejun Heodd5b06c2006-08-10 16:59:12 +09007330};
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01007331EXPORT_SYMBOL_GPL(ata_dummy_port_ops);
Tejun Heodd5b06c2006-08-10 16:59:12 +09007332
Tejun Heo21b0ad42007-04-17 23:44:07 +09007333const struct ata_port_info ata_dummy_port_info = {
7334 .port_ops = &ata_dummy_port_ops,
7335};
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01007336EXPORT_SYMBOL_GPL(ata_dummy_port_info);
Tejun Heo21b0ad42007-04-17 23:44:07 +09007337
Tejun Heodd5b06c2006-08-10 16:59:12 +09007338/*
Joe Perchesa9a79df2011-04-15 15:51:59 -07007339 * Utility print functions
7340 */
Joe Perchesd7bead12014-09-22 09:52:18 -07007341void ata_port_printk(const struct ata_port *ap, const char *level,
7342 const char *fmt, ...)
Joe Perchesa9a79df2011-04-15 15:51:59 -07007343{
7344 struct va_format vaf;
7345 va_list args;
Joe Perchesa9a79df2011-04-15 15:51:59 -07007346
7347 va_start(args, fmt);
7348
7349 vaf.fmt = fmt;
7350 vaf.va = &args;
7351
Joe Perchesd7bead12014-09-22 09:52:18 -07007352 printk("%sata%u: %pV", level, ap->print_id, &vaf);
Joe Perchesa9a79df2011-04-15 15:51:59 -07007353
7354 va_end(args);
Joe Perchesa9a79df2011-04-15 15:51:59 -07007355}
7356EXPORT_SYMBOL(ata_port_printk);
7357
Joe Perchesd7bead12014-09-22 09:52:18 -07007358void ata_link_printk(const struct ata_link *link, const char *level,
7359 const char *fmt, ...)
Joe Perchesa9a79df2011-04-15 15:51:59 -07007360{
7361 struct va_format vaf;
7362 va_list args;
Joe Perchesa9a79df2011-04-15 15:51:59 -07007363
7364 va_start(args, fmt);
7365
7366 vaf.fmt = fmt;
7367 vaf.va = &args;
7368
7369 if (sata_pmp_attached(link->ap) || link->ap->slave_link)
Joe Perchesd7bead12014-09-22 09:52:18 -07007370 printk("%sata%u.%02u: %pV",
7371 level, link->ap->print_id, link->pmp, &vaf);
Joe Perchesa9a79df2011-04-15 15:51:59 -07007372 else
Joe Perchesd7bead12014-09-22 09:52:18 -07007373 printk("%sata%u: %pV",
7374 level, link->ap->print_id, &vaf);
Joe Perchesa9a79df2011-04-15 15:51:59 -07007375
7376 va_end(args);
Joe Perchesa9a79df2011-04-15 15:51:59 -07007377}
7378EXPORT_SYMBOL(ata_link_printk);
7379
Joe Perchesd7bead12014-09-22 09:52:18 -07007380void ata_dev_printk(const struct ata_device *dev, const char *level,
Joe Perchesa9a79df2011-04-15 15:51:59 -07007381 const char *fmt, ...)
7382{
7383 struct va_format vaf;
7384 va_list args;
Joe Perchesa9a79df2011-04-15 15:51:59 -07007385
7386 va_start(args, fmt);
7387
7388 vaf.fmt = fmt;
7389 vaf.va = &args;
7390
Joe Perchesd7bead12014-09-22 09:52:18 -07007391 printk("%sata%u.%02u: %pV",
7392 level, dev->link->ap->print_id, dev->link->pmp + dev->devno,
7393 &vaf);
Joe Perchesa9a79df2011-04-15 15:51:59 -07007394
7395 va_end(args);
Joe Perchesa9a79df2011-04-15 15:51:59 -07007396}
7397EXPORT_SYMBOL(ata_dev_printk);
7398
Joe Perches06296a12011-04-15 15:52:00 -07007399void ata_print_version(const struct device *dev, const char *version)
7400{
7401 dev_printk(KERN_DEBUG, dev, "version %s\n", version);
7402}
7403EXPORT_SYMBOL(ata_print_version);