blob: beca5f91bb4c7c8bed8a40c118717363981da495 [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 Heo029cfd62008-03-25 12:22:49 +090068const struct ata_port_operations ata_base_port_ops = {
Tejun Heo0aa11132008-04-07 22:47:18 +090069 .prereset = ata_std_prereset,
Tejun Heo203c75b2008-04-07 22:47:18 +090070 .postreset = ata_std_postreset,
Tejun Heoa1efdab2008-03-25 12:22:50 +090071 .error_handler = ata_std_error_handler,
Dan Williamse4a9c372012-06-21 23:25:27 -070072 .sched_eh = ata_std_sched_eh,
73 .end_eh = ata_std_end_eh,
Tejun Heo029cfd62008-03-25 12:22:49 +090074};
75
76const struct ata_port_operations sata_port_ops = {
77 .inherits = &ata_base_port_ops,
78
79 .qc_defer = ata_std_qc_defer,
Tejun Heo57c9efd2008-04-07 22:47:19 +090080 .hardreset = sata_std_hardreset,
Tejun Heo029cfd62008-03-25 12:22:49 +090081};
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +010082EXPORT_SYMBOL_GPL(sata_port_ops);
Tejun Heo029cfd62008-03-25 12:22:49 +090083
Tejun Heo3373efd2006-05-15 20:57:53 +090084static unsigned int ata_dev_init_params(struct ata_device *dev,
85 u16 heads, u16 sectors);
86static unsigned int ata_dev_set_xfermode(struct ata_device *dev);
87static void ata_dev_xfermask(struct ata_device *dev);
Tejun Heo75683fe2007-07-05 13:31:27 +090088static unsigned long ata_dev_blacklisted(const struct ata_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
Tero Roponena78f57a2012-04-22 11:38:00 +030090atomic_t ata_print_id = ATOMIC_INIT(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Bartlomiej Zolnierkiewiczbf89b0b2020-03-26 16:58:22 +010092#ifdef CONFIG_ATA_FORCE
Tejun Heo33267322008-02-13 09:15:09 +090093struct ata_force_param {
94 const char *name;
Bartlomiej Zolnierkiewicz8ba5a452020-03-26 16:58:02 +010095 u8 cbl;
96 u8 spd_limit;
Tejun Heo33267322008-02-13 09:15:09 +090097 unsigned long xfer_mask;
98 unsigned int horkage_on;
99 unsigned int horkage_off;
Bartlomiej Zolnierkiewicz8ba5a452020-03-26 16:58:02 +0100100 u16 lflags;
Tejun Heo33267322008-02-13 09:15:09 +0900101};
102
103struct ata_force_ent {
104 int port;
105 int device;
106 struct ata_force_param param;
107};
108
109static struct ata_force_ent *ata_force_tbl;
110static int ata_force_tbl_size;
111
Bartlomiej Zolnierkiewiczbbf5a092020-03-26 16:58:01 +0100112static char ata_force_param_buf[COMMAND_LINE_SIZE] __initdata;
Tejun Heo7afb4222008-03-09 20:21:53 +0900113/* param_buf is thrown away after initialization, disallow read */
114module_param_string(force, ata_force_param_buf, sizeof(ata_force_param_buf), 0);
Mauro Carvalho Chehab8c27ceff32016-10-18 10:12:27 -0200115MODULE_PARM_DESC(force, "Force ATA configurations including cable type, link speed and transfer mode (see Documentation/admin-guide/kernel-parameters.rst for details)");
Bartlomiej Zolnierkiewiczbf89b0b2020-03-26 16:58:22 +0100116#endif
Tejun Heo33267322008-02-13 09:15:09 +0900117
Tejun Heo2486fa52008-07-31 07:52:40 +0900118static int atapi_enabled = 1;
Jeff Garzik1623c812005-08-30 03:37:42 -0400119module_param(atapi_enabled, int, 0444);
Evgeni Golovad5d8ea2009-06-22 09:46:20 +0200120MODULE_PARM_DESC(atapi_enabled, "Enable discovery of ATAPI devices (0=off, 1=on [default])");
Jeff Garzik1623c812005-08-30 03:37:42 -0400121
Adrian Bunkc5c61bd2008-02-25 02:07:25 +0200122static int atapi_dmadir = 0;
Albert Lee95de7192006-04-04 10:57:18 +0800123module_param(atapi_dmadir, int, 0444);
Evgeni Golovad5d8ea2009-06-22 09:46:20 +0200124MODULE_PARM_DESC(atapi_dmadir, "Enable ATAPI DMADIR bridge support (0=off [default], 1=on)");
Albert Lee95de7192006-04-04 10:57:18 +0800125
Mark Lordbaf4fdf2007-08-08 01:08:45 +0900126int atapi_passthru16 = 1;
127module_param(atapi_passthru16, int, 0444);
Evgeni Golovad5d8ea2009-06-22 09:46:20 +0200128MODULE_PARM_DESC(atapi_passthru16, "Enable ATA_16 passthru for ATAPI devices (0=off, 1=on [default])");
Mark Lordbaf4fdf2007-08-08 01:08:45 +0900129
Jeff Garzikc3c013a2006-02-27 22:31:19 -0500130int libata_fua = 0;
131module_param_named(fua, libata_fua, int, 0444);
Evgeni Golovad5d8ea2009-06-22 09:46:20 +0200132MODULE_PARM_DESC(fua, "FUA support (0=off [default], 1=on)");
Jeff Garzikc3c013a2006-02-27 22:31:19 -0500133
Jeff Garzik2dcb4072007-10-19 06:42:56 -0400134static int ata_ignore_hpa;
Alan Cox1e999732007-04-11 00:23:13 +0100135module_param_named(ignore_hpa, ata_ignore_hpa, int, 0644);
136MODULE_PARM_DESC(ignore_hpa, "Ignore HPA limit (0=keep BIOS limits, 1=ignore limits, using full disk)");
137
Alan Coxb3a70602007-10-02 12:38:26 -0400138static int libata_dma_mask = ATA_DMA_MASK_ATA|ATA_DMA_MASK_ATAPI|ATA_DMA_MASK_CFA;
139module_param_named(dma, libata_dma_mask, int, 0444);
140MODULE_PARM_DESC(dma, "DMA enable/disable (0x1==ATA, 0x2==ATAPI, 0x4==CF)");
141
Tejun Heo87fbc5a2008-05-20 02:17:54 +0900142static int ata_probe_timeout;
Andrew Mortona8601e52006-06-25 01:36:52 -0700143module_param(ata_probe_timeout, int, 0444);
144MODULE_PARM_DESC(ata_probe_timeout, "Set ATA probing timeout (seconds)");
145
Jeff Garzik6ebe9d82007-10-05 16:28:36 -0400146int libata_noacpi = 0;
Jeff Garzikd7d0dad2007-03-28 01:57:37 -0400147module_param_named(noacpi, libata_noacpi, int, 0444);
Evgeni Golovad5d8ea2009-06-22 09:46:20 +0200148MODULE_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 -0700149
Alan Coxae8d4ee2007-11-04 22:05:49 -0500150int libata_allow_tpm = 0;
151module_param_named(allow_tpm, libata_allow_tpm, int, 0444);
Evgeni Golovad5d8ea2009-06-22 09:46:20 +0200152MODULE_PARM_DESC(allow_tpm, "Permit the use of TPM commands (0=off [default], 1=on)");
Alan Coxae8d4ee2007-11-04 22:05:49 -0500153
Tejun Heoe7ecd432010-05-19 15:38:58 +0200154static int atapi_an;
155module_param(atapi_an, int, 0444);
156MODULE_PARM_DESC(atapi_an, "Enable ATAPI AN media presence notification (0=0ff [default], 1=on)");
157
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158MODULE_AUTHOR("Jeff Garzik");
159MODULE_DESCRIPTION("Library module for ATA devices");
160MODULE_LICENSE("GPL");
161MODULE_VERSION(DRV_VERSION);
162
Edward Falk0baab862005-06-02 18:17:13 -0400163
Tejun Heo9913ff8a2009-01-29 20:31:31 +0900164static bool ata_sstatus_online(u32 sstatus)
165{
166 return (sstatus & 0xf) == 0x3;
167}
168
Tejun Heo1eca4362008-11-03 20:03:17 +0900169/**
170 * ata_link_next - link iteration helper
171 * @link: the previous link, NULL to start
172 * @ap: ATA port containing links to iterate
173 * @mode: iteration mode, one of ATA_LITER_*
Tejun Heoaadffb62008-07-31 17:02:41 +0900174 *
Tejun Heo1eca4362008-11-03 20:03:17 +0900175 * LOCKING:
176 * Host lock or EH context.
177 *
178 * RETURNS:
179 * Pointer to the next link.
Tejun Heoaadffb62008-07-31 17:02:41 +0900180 */
Tejun Heo1eca4362008-11-03 20:03:17 +0900181struct ata_link *ata_link_next(struct ata_link *link, struct ata_port *ap,
182 enum ata_link_iter_mode mode)
Tejun Heoaadffb62008-07-31 17:02:41 +0900183{
Tejun Heo1eca4362008-11-03 20:03:17 +0900184 BUG_ON(mode != ATA_LITER_EDGE &&
185 mode != ATA_LITER_PMP_FIRST && mode != ATA_LITER_HOST_FIRST);
Tejun Heoaadffb62008-07-31 17:02:41 +0900186
Tejun Heo1eca4362008-11-03 20:03:17 +0900187 /* NULL link indicates start of iteration */
188 if (!link)
189 switch (mode) {
190 case ATA_LITER_EDGE:
191 case ATA_LITER_PMP_FIRST:
192 if (sata_pmp_attached(ap))
193 return ap->pmp_link;
194 /* fall through */
195 case ATA_LITER_HOST_FIRST:
196 return &ap->link;
197 }
198
199 /* we just iterated over the host link, what's next? */
200 if (link == &ap->link)
201 switch (mode) {
202 case ATA_LITER_HOST_FIRST:
203 if (sata_pmp_attached(ap))
204 return ap->pmp_link;
205 /* fall through */
206 case ATA_LITER_PMP_FIRST:
207 if (unlikely(ap->slave_link))
Tejun Heob1c72912008-07-31 17:02:43 +0900208 return ap->slave_link;
Tejun Heo1eca4362008-11-03 20:03:17 +0900209 /* fall through */
210 case ATA_LITER_EDGE:
Tejun Heoaadffb62008-07-31 17:02:41 +0900211 return NULL;
Tejun Heob1c72912008-07-31 17:02:43 +0900212 }
Tejun Heoaadffb62008-07-31 17:02:41 +0900213
Tejun Heob1c72912008-07-31 17:02:43 +0900214 /* slave_link excludes PMP */
215 if (unlikely(link == ap->slave_link))
216 return NULL;
217
Tejun Heo1eca4362008-11-03 20:03:17 +0900218 /* we were over a PMP link */
Tejun Heoaadffb62008-07-31 17:02:41 +0900219 if (++link < ap->pmp_link + ap->nr_pmp_links)
220 return link;
Tejun Heo1eca4362008-11-03 20:03:17 +0900221
222 if (mode == ATA_LITER_PMP_FIRST)
223 return &ap->link;
224
Tejun Heoaadffb62008-07-31 17:02:41 +0900225 return NULL;
226}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +0100227EXPORT_SYMBOL_GPL(ata_link_next);
Tejun Heoaadffb62008-07-31 17:02:41 +0900228
Edward Falk0baab862005-06-02 18:17:13 -0400229/**
Tejun Heo1eca4362008-11-03 20:03:17 +0900230 * ata_dev_next - device iteration helper
231 * @dev: the previous device, NULL to start
232 * @link: ATA link containing devices to iterate
233 * @mode: iteration mode, one of ATA_DITER_*
234 *
235 * LOCKING:
236 * Host lock or EH context.
237 *
238 * RETURNS:
239 * Pointer to the next device.
240 */
241struct ata_device *ata_dev_next(struct ata_device *dev, struct ata_link *link,
242 enum ata_dev_iter_mode mode)
243{
244 BUG_ON(mode != ATA_DITER_ENABLED && mode != ATA_DITER_ENABLED_REVERSE &&
245 mode != ATA_DITER_ALL && mode != ATA_DITER_ALL_REVERSE);
246
247 /* NULL dev indicates start of iteration */
248 if (!dev)
249 switch (mode) {
250 case ATA_DITER_ENABLED:
251 case ATA_DITER_ALL:
252 dev = link->device;
253 goto check;
254 case ATA_DITER_ENABLED_REVERSE:
255 case ATA_DITER_ALL_REVERSE:
256 dev = link->device + ata_link_max_devices(link) - 1;
257 goto check;
258 }
259
260 next:
261 /* move to the next one */
262 switch (mode) {
263 case ATA_DITER_ENABLED:
264 case ATA_DITER_ALL:
265 if (++dev < link->device + ata_link_max_devices(link))
266 goto check;
267 return NULL;
268 case ATA_DITER_ENABLED_REVERSE:
269 case ATA_DITER_ALL_REVERSE:
270 if (--dev >= link->device)
271 goto check;
272 return NULL;
273 }
274
275 check:
276 if ((mode == ATA_DITER_ENABLED || mode == ATA_DITER_ENABLED_REVERSE) &&
277 !ata_dev_enabled(dev))
278 goto next;
279 return dev;
280}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +0100281EXPORT_SYMBOL_GPL(ata_dev_next);
Tejun Heo1eca4362008-11-03 20:03:17 +0900282
283/**
Tejun Heob1c72912008-07-31 17:02:43 +0900284 * ata_dev_phys_link - find physical link for a device
285 * @dev: ATA device to look up physical link for
286 *
287 * Look up physical link which @dev is attached to. Note that
288 * this is different from @dev->link only when @dev is on slave
289 * link. For all other cases, it's the same as @dev->link.
290 *
291 * LOCKING:
292 * Don't care.
293 *
294 * RETURNS:
295 * Pointer to the found physical link.
296 */
297struct ata_link *ata_dev_phys_link(struct ata_device *dev)
298{
299 struct ata_port *ap = dev->link->ap;
300
301 if (!ap->slave_link)
302 return dev->link;
303 if (!dev->devno)
304 return &ap->link;
305 return ap->slave_link;
306}
307
Bartlomiej Zolnierkiewiczbf89b0b2020-03-26 16:58:22 +0100308#ifdef CONFIG_ATA_FORCE
Tejun Heob1c72912008-07-31 17:02:43 +0900309/**
Tejun Heo33267322008-02-13 09:15:09 +0900310 * ata_force_cbl - force cable type according to libata.force
Randy Dunlap4cdfa1b2008-02-22 12:21:37 -0800311 * @ap: ATA port of interest
Tejun Heo33267322008-02-13 09:15:09 +0900312 *
313 * Force cable type according to libata.force and whine about it.
314 * The last entry which has matching port number is used, so it
315 * can be specified as part of device force parameters. For
316 * example, both "a:40c,1.00:udma4" and "1.00:40c,udma4" have the
317 * same effect.
318 *
319 * LOCKING:
320 * EH context.
321 */
322void ata_force_cbl(struct ata_port *ap)
323{
324 int i;
325
326 for (i = ata_force_tbl_size - 1; i >= 0; i--) {
327 const struct ata_force_ent *fe = &ata_force_tbl[i];
328
329 if (fe->port != -1 && fe->port != ap->print_id)
330 continue;
331
332 if (fe->param.cbl == ATA_CBL_NONE)
333 continue;
334
335 ap->cbl = fe->param.cbl;
Joe Perchesa9a79df2011-04-15 15:51:59 -0700336 ata_port_notice(ap, "FORCE: cable set to %s\n", fe->param.name);
Tejun Heo33267322008-02-13 09:15:09 +0900337 return;
338 }
339}
340
341/**
Tejun Heo05944bd2008-08-13 20:19:09 +0900342 * ata_force_link_limits - force link limits according to libata.force
Tejun Heo33267322008-02-13 09:15:09 +0900343 * @link: ATA link of interest
344 *
Tejun Heo05944bd2008-08-13 20:19:09 +0900345 * Force link flags and SATA spd limit according to libata.force
346 * and whine about it. When only the port part is specified
347 * (e.g. 1:), the limit applies to all links connected to both
348 * the host link and all fan-out ports connected via PMP. If the
349 * device part is specified as 0 (e.g. 1.00:), it specifies the
350 * first fan-out link not the host link. Device number 15 always
Tejun Heob1c72912008-07-31 17:02:43 +0900351 * points to the host link whether PMP is attached or not. If the
352 * controller has slave link, device number 16 points to it.
Tejun Heo33267322008-02-13 09:15:09 +0900353 *
354 * LOCKING:
355 * EH context.
356 */
Tejun Heo05944bd2008-08-13 20:19:09 +0900357static void ata_force_link_limits(struct ata_link *link)
Tejun Heo33267322008-02-13 09:15:09 +0900358{
Tejun Heo05944bd2008-08-13 20:19:09 +0900359 bool did_spd = false;
Tejun Heob1c72912008-07-31 17:02:43 +0900360 int linkno = link->pmp;
361 int i;
Tejun Heo33267322008-02-13 09:15:09 +0900362
363 if (ata_is_host_link(link))
Tejun Heob1c72912008-07-31 17:02:43 +0900364 linkno += 15;
Tejun Heo33267322008-02-13 09:15:09 +0900365
366 for (i = ata_force_tbl_size - 1; i >= 0; i--) {
367 const struct ata_force_ent *fe = &ata_force_tbl[i];
368
369 if (fe->port != -1 && fe->port != link->ap->print_id)
370 continue;
371
372 if (fe->device != -1 && fe->device != linkno)
373 continue;
374
Tejun Heo05944bd2008-08-13 20:19:09 +0900375 /* only honor the first spd limit */
376 if (!did_spd && fe->param.spd_limit) {
377 link->hw_sata_spd_limit = (1 << fe->param.spd_limit) - 1;
Joe Perchesa9a79df2011-04-15 15:51:59 -0700378 ata_link_notice(link, "FORCE: PHY spd limit set to %s\n",
Tejun Heo05944bd2008-08-13 20:19:09 +0900379 fe->param.name);
380 did_spd = true;
381 }
Tejun Heo33267322008-02-13 09:15:09 +0900382
Tejun Heo05944bd2008-08-13 20:19:09 +0900383 /* let lflags stack */
384 if (fe->param.lflags) {
385 link->flags |= fe->param.lflags;
Joe Perchesa9a79df2011-04-15 15:51:59 -0700386 ata_link_notice(link,
Tejun Heo05944bd2008-08-13 20:19:09 +0900387 "FORCE: link flag 0x%x forced -> 0x%x\n",
388 fe->param.lflags, link->flags);
389 }
Tejun Heo33267322008-02-13 09:15:09 +0900390 }
391}
392
393/**
394 * ata_force_xfermask - force xfermask according to libata.force
395 * @dev: ATA device of interest
396 *
397 * Force xfer_mask according to libata.force and whine about it.
398 * For consistency with link selection, device number 15 selects
399 * the first device connected to the host link.
400 *
401 * LOCKING:
402 * EH context.
403 */
404static void ata_force_xfermask(struct ata_device *dev)
405{
406 int devno = dev->link->pmp + dev->devno;
407 int alt_devno = devno;
408 int i;
409
Tejun Heob1c72912008-07-31 17:02:43 +0900410 /* allow n.15/16 for devices attached to host port */
411 if (ata_is_host_link(dev->link))
412 alt_devno += 15;
Tejun Heo33267322008-02-13 09:15:09 +0900413
414 for (i = ata_force_tbl_size - 1; i >= 0; i--) {
415 const struct ata_force_ent *fe = &ata_force_tbl[i];
416 unsigned long pio_mask, mwdma_mask, udma_mask;
417
418 if (fe->port != -1 && fe->port != dev->link->ap->print_id)
419 continue;
420
421 if (fe->device != -1 && fe->device != devno &&
422 fe->device != alt_devno)
423 continue;
424
425 if (!fe->param.xfer_mask)
426 continue;
427
428 ata_unpack_xfermask(fe->param.xfer_mask,
429 &pio_mask, &mwdma_mask, &udma_mask);
430 if (udma_mask)
431 dev->udma_mask = udma_mask;
432 else if (mwdma_mask) {
433 dev->udma_mask = 0;
434 dev->mwdma_mask = mwdma_mask;
435 } else {
436 dev->udma_mask = 0;
437 dev->mwdma_mask = 0;
438 dev->pio_mask = pio_mask;
439 }
440
Joe Perchesa9a79df2011-04-15 15:51:59 -0700441 ata_dev_notice(dev, "FORCE: xfer_mask set to %s\n",
442 fe->param.name);
Tejun Heo33267322008-02-13 09:15:09 +0900443 return;
444 }
445}
446
447/**
448 * ata_force_horkage - force horkage according to libata.force
449 * @dev: ATA device of interest
450 *
451 * Force horkage according to libata.force and whine about it.
452 * For consistency with link selection, device number 15 selects
453 * the first device connected to the host link.
454 *
455 * LOCKING:
456 * EH context.
457 */
458static void ata_force_horkage(struct ata_device *dev)
459{
460 int devno = dev->link->pmp + dev->devno;
461 int alt_devno = devno;
462 int i;
463
Tejun Heob1c72912008-07-31 17:02:43 +0900464 /* allow n.15/16 for devices attached to host port */
465 if (ata_is_host_link(dev->link))
466 alt_devno += 15;
Tejun Heo33267322008-02-13 09:15:09 +0900467
468 for (i = 0; i < ata_force_tbl_size; i++) {
469 const struct ata_force_ent *fe = &ata_force_tbl[i];
470
471 if (fe->port != -1 && fe->port != dev->link->ap->print_id)
472 continue;
473
474 if (fe->device != -1 && fe->device != devno &&
475 fe->device != alt_devno)
476 continue;
477
478 if (!(~dev->horkage & fe->param.horkage_on) &&
479 !(dev->horkage & fe->param.horkage_off))
480 continue;
481
482 dev->horkage |= fe->param.horkage_on;
483 dev->horkage &= ~fe->param.horkage_off;
484
Joe Perchesa9a79df2011-04-15 15:51:59 -0700485 ata_dev_notice(dev, "FORCE: horkage modified (%s)\n",
486 fe->param.name);
Tejun Heo33267322008-02-13 09:15:09 +0900487 }
488}
Bartlomiej Zolnierkiewiczbf89b0b2020-03-26 16:58:22 +0100489#else
490static inline void ata_force_link_limits(struct ata_link *link) { }
491static inline void ata_force_xfermask(struct ata_device *dev) { }
492static inline void ata_force_horkage(struct ata_device *dev) { }
493#endif
Tejun Heo33267322008-02-13 09:15:09 +0900494
495/**
Tejun Heo436d34b2008-04-02 17:28:46 +0900496 * atapi_cmd_type - Determine ATAPI command type from SCSI opcode
497 * @opcode: SCSI opcode
498 *
499 * Determine ATAPI command type from @opcode.
500 *
501 * LOCKING:
502 * None.
503 *
504 * RETURNS:
505 * ATAPI_{READ|WRITE|READ_CD|PASS_THRU|MISC}
506 */
507int atapi_cmd_type(u8 opcode)
508{
509 switch (opcode) {
510 case GPCMD_READ_10:
511 case GPCMD_READ_12:
512 return ATAPI_READ;
513
514 case GPCMD_WRITE_10:
515 case GPCMD_WRITE_12:
516 case GPCMD_WRITE_AND_VERIFY_10:
517 return ATAPI_WRITE;
518
519 case GPCMD_READ_CD:
520 case GPCMD_READ_CD_MSF:
521 return ATAPI_READ_CD;
522
Tejun Heoe52dcc42008-04-02 17:35:19 +0900523 case ATA_16:
524 case ATA_12:
525 if (atapi_passthru16)
526 return ATAPI_PASS_THRU;
527 /* fall thru */
Tejun Heo436d34b2008-04-02 17:28:46 +0900528 default:
529 return ATAPI_MISC;
530 }
531}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +0100532EXPORT_SYMBOL_GPL(atapi_cmd_type);
Tejun Heo436d34b2008-04-02 17:28:46 +0900533
Albert Lee8cbd6df2005-10-12 15:06:27 +0800534static const u8 ata_rw_cmds[] = {
535 /* pio multi */
536 ATA_CMD_READ_MULTI,
537 ATA_CMD_WRITE_MULTI,
538 ATA_CMD_READ_MULTI_EXT,
539 ATA_CMD_WRITE_MULTI_EXT,
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100540 0,
541 0,
542 0,
543 ATA_CMD_WRITE_MULTI_FUA_EXT,
Albert Lee8cbd6df2005-10-12 15:06:27 +0800544 /* pio */
545 ATA_CMD_PIO_READ,
546 ATA_CMD_PIO_WRITE,
547 ATA_CMD_PIO_READ_EXT,
548 ATA_CMD_PIO_WRITE_EXT,
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100549 0,
550 0,
551 0,
552 0,
Albert Lee8cbd6df2005-10-12 15:06:27 +0800553 /* dma */
554 ATA_CMD_READ,
555 ATA_CMD_WRITE,
556 ATA_CMD_READ_EXT,
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100557 ATA_CMD_WRITE_EXT,
558 0,
559 0,
560 0,
561 ATA_CMD_WRITE_FUA_EXT
Albert Lee8cbd6df2005-10-12 15:06:27 +0800562};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
564/**
Albert Lee8cbd6df2005-10-12 15:06:27 +0800565 * ata_rwcmd_protocol - set taskfile r/w commands and protocol
Tejun Heobd056d72006-11-14 22:47:10 +0900566 * @tf: command to examine and configure
567 * @dev: device tf belongs to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 *
Jeff Garzik2e9edbf2006-03-24 09:56:57 -0500569 * Examine the device configuration and tf->flags to calculate
Albert Lee8cbd6df2005-10-12 15:06:27 +0800570 * the proper read/write commands and protocol to use.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 *
572 * LOCKING:
573 * caller.
574 */
Tejun Heobd056d72006-11-14 22:47:10 +0900575static int ata_rwcmd_protocol(struct ata_taskfile *tf, struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576{
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100577 u8 cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100579 int index, fua, lba48, write;
Jeff Garzik2e9edbf2006-03-24 09:56:57 -0500580
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100581 fua = (tf->flags & ATA_TFLAG_FUA) ? 4 : 0;
Albert Lee8cbd6df2005-10-12 15:06:27 +0800582 lba48 = (tf->flags & ATA_TFLAG_LBA48) ? 2 : 0;
583 write = (tf->flags & ATA_TFLAG_WRITE) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
Albert Lee8cbd6df2005-10-12 15:06:27 +0800585 if (dev->flags & ATA_DFLAG_PIO) {
586 tf->protocol = ATA_PROT_PIO;
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100587 index = dev->multi_count ? 0 : 8;
Tejun Heo9af5c9c2007-08-06 18:36:22 +0900588 } else if (lba48 && (dev->link->ap->flags & ATA_FLAG_PIO_LBA48)) {
Alan Cox8d238e02006-01-17 20:50:31 +0000589 /* Unable to use DMA due to host limitation */
590 tf->protocol = ATA_PROT_PIO;
Albert Lee0565c262006-02-13 18:55:25 +0800591 index = dev->multi_count ? 0 : 8;
Albert Lee8cbd6df2005-10-12 15:06:27 +0800592 } else {
593 tf->protocol = ATA_PROT_DMA;
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100594 index = 16;
Albert Lee8cbd6df2005-10-12 15:06:27 +0800595 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100597 cmd = ata_rw_cmds[index + fua + lba48 + write];
598 if (cmd) {
599 tf->command = cmd;
600 return 0;
601 }
602 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603}
604
Tejun Heocb95d562006-03-06 04:31:56 +0900605/**
Tejun Heo35b649f2006-11-14 22:37:35 +0900606 * ata_tf_read_block - Read block address from ATA taskfile
607 * @tf: ATA taskfile of interest
608 * @dev: ATA device @tf belongs to
609 *
610 * LOCKING:
611 * None.
612 *
613 * Read block address from @tf. This function can handle all
614 * three address formats - LBA, LBA48 and CHS. tf->protocol and
615 * flags select the address format to use.
616 *
617 * RETURNS:
618 * Block address read from @tf.
619 */
Hannes Reineckecffd1ee2016-04-04 11:43:57 +0200620u64 ata_tf_read_block(const struct ata_taskfile *tf, struct ata_device *dev)
Tejun Heo35b649f2006-11-14 22:37:35 +0900621{
622 u64 block = 0;
623
Tejun Heofe16d4f2015-08-03 11:41:33 -0400624 if (tf->flags & ATA_TFLAG_LBA) {
Tejun Heo35b649f2006-11-14 22:37:35 +0900625 if (tf->flags & ATA_TFLAG_LBA48) {
626 block |= (u64)tf->hob_lbah << 40;
627 block |= (u64)tf->hob_lbam << 32;
Roland Dreier44901a92008-11-04 10:34:48 -0800628 block |= (u64)tf->hob_lbal << 24;
Tejun Heo35b649f2006-11-14 22:37:35 +0900629 } else
630 block |= (tf->device & 0xf) << 24;
631
632 block |= tf->lbah << 16;
633 block |= tf->lbam << 8;
634 block |= tf->lbal;
635 } else {
636 u32 cyl, head, sect;
637
638 cyl = tf->lbam | (tf->lbah << 8);
639 head = tf->device & 0xf;
640 sect = tf->lbal;
641
Tejun Heoac8672e2009-08-16 21:21:21 +0900642 if (!sect) {
Joe Perchesa9a79df2011-04-15 15:51:59 -0700643 ata_dev_warn(dev,
644 "device reported invalid CHS sector 0\n");
Hannes Reineckecffd1ee2016-04-04 11:43:57 +0200645 return U64_MAX;
Tejun Heoac8672e2009-08-16 21:21:21 +0900646 }
647
648 block = (cyl * dev->heads + head) * dev->sectors + sect - 1;
Tejun Heo35b649f2006-11-14 22:37:35 +0900649 }
650
651 return block;
652}
653
654/**
Tejun Heobd056d72006-11-14 22:47:10 +0900655 * ata_build_rw_tf - Build ATA taskfile for given read/write request
656 * @tf: Target ATA taskfile
657 * @dev: ATA device @tf belongs to
658 * @block: Block address
659 * @n_block: Number of blocks
660 * @tf_flags: RW/FUA etc...
661 * @tag: tag
Adam Manzanares8e061782016-10-17 11:27:29 -0700662 * @class: IO priority class
Tejun Heobd056d72006-11-14 22:47:10 +0900663 *
664 * LOCKING:
665 * None.
666 *
667 * Build ATA taskfile @tf for read/write request described by
668 * @block, @n_block, @tf_flags and @tag on @dev.
669 *
670 * RETURNS:
671 *
672 * 0 on success, -ERANGE if the request is too large for @dev,
673 * -EINVAL if the request is invalid.
674 */
675int ata_build_rw_tf(struct ata_taskfile *tf, struct ata_device *dev,
676 u64 block, u32 n_block, unsigned int tf_flags,
Adam Manzanares8e061782016-10-17 11:27:29 -0700677 unsigned int tag, int class)
Tejun Heobd056d72006-11-14 22:47:10 +0900678{
679 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
680 tf->flags |= tf_flags;
681
Jens Axboe2e2cc672018-05-11 12:51:06 -0600682 if (ata_ncq_enabled(dev) && !ata_tag_internal(tag)) {
Tejun Heobd056d72006-11-14 22:47:10 +0900683 /* yay, NCQ */
684 if (!lba_48_ok(block, n_block))
685 return -ERANGE;
686
687 tf->protocol = ATA_PROT_NCQ;
688 tf->flags |= ATA_TFLAG_LBA | ATA_TFLAG_LBA48;
689
690 if (tf->flags & ATA_TFLAG_WRITE)
691 tf->command = ATA_CMD_FPDMA_WRITE;
692 else
693 tf->command = ATA_CMD_FPDMA_READ;
694
695 tf->nsect = tag << 3;
696 tf->hob_feature = (n_block >> 8) & 0xff;
697 tf->feature = n_block & 0xff;
698
699 tf->hob_lbah = (block >> 40) & 0xff;
700 tf->hob_lbam = (block >> 32) & 0xff;
701 tf->hob_lbal = (block >> 24) & 0xff;
702 tf->lbah = (block >> 16) & 0xff;
703 tf->lbam = (block >> 8) & 0xff;
704 tf->lbal = block & 0xff;
705
Sergei Shtylyov9ca7cfa2012-08-21 22:18:50 +0400706 tf->device = ATA_LBA;
Tejun Heobd056d72006-11-14 22:47:10 +0900707 if (tf->flags & ATA_TFLAG_FUA)
708 tf->device |= 1 << 7;
Adam Manzanares8e061782016-10-17 11:27:29 -0700709
Adam Manzanares9f56eca2016-12-13 12:00:05 -0800710 if (dev->flags & ATA_DFLAG_NCQ_PRIO) {
Adam Manzanares8e061782016-10-17 11:27:29 -0700711 if (class == IOPRIO_CLASS_RT)
712 tf->hob_nsect |= ATA_PRIO_HIGH <<
713 ATA_SHIFT_PRIO;
714 }
Tejun Heobd056d72006-11-14 22:47:10 +0900715 } else if (dev->flags & ATA_DFLAG_LBA) {
716 tf->flags |= ATA_TFLAG_LBA;
717
718 if (lba_28_ok(block, n_block)) {
719 /* use LBA28 */
720 tf->device |= (block >> 24) & 0xf;
721 } else if (lba_48_ok(block, n_block)) {
722 if (!(dev->flags & ATA_DFLAG_LBA48))
723 return -ERANGE;
724
725 /* use LBA48 */
726 tf->flags |= ATA_TFLAG_LBA48;
727
728 tf->hob_nsect = (n_block >> 8) & 0xff;
729
730 tf->hob_lbah = (block >> 40) & 0xff;
731 tf->hob_lbam = (block >> 32) & 0xff;
732 tf->hob_lbal = (block >> 24) & 0xff;
733 } else
734 /* request too large even for LBA48 */
735 return -ERANGE;
736
737 if (unlikely(ata_rwcmd_protocol(tf, dev) < 0))
738 return -EINVAL;
739
740 tf->nsect = n_block & 0xff;
741
742 tf->lbah = (block >> 16) & 0xff;
743 tf->lbam = (block >> 8) & 0xff;
744 tf->lbal = block & 0xff;
745
746 tf->device |= ATA_LBA;
747 } else {
748 /* CHS */
749 u32 sect, head, cyl, track;
750
751 /* The request -may- be too large for CHS addressing. */
752 if (!lba_28_ok(block, n_block))
753 return -ERANGE;
754
755 if (unlikely(ata_rwcmd_protocol(tf, dev) < 0))
756 return -EINVAL;
757
758 /* Convert LBA to CHS */
759 track = (u32)block / dev->sectors;
760 cyl = track / dev->heads;
761 head = track % dev->heads;
762 sect = (u32)block % dev->sectors + 1;
763
764 DPRINTK("block %u track %u cyl %u head %u sect %u\n",
765 (u32)block, track, cyl, head, sect);
766
767 /* Check whether the converted CHS can fit.
768 Cylinder: 0-65535
769 Head: 0-15
770 Sector: 1-255*/
771 if ((cyl >> 16) || (head >> 4) || (sect >> 8) || (!sect))
772 return -ERANGE;
773
774 tf->nsect = n_block & 0xff; /* Sector count 0 means 256 sectors */
775 tf->lbal = sect;
776 tf->lbam = cyl;
777 tf->lbah = cyl >> 8;
778 tf->device |= head;
779 }
780
781 return 0;
782}
783
784/**
Tejun Heocb95d562006-03-06 04:31:56 +0900785 * ata_pack_xfermask - Pack pio, mwdma and udma masks into xfer_mask
786 * @pio_mask: pio_mask
787 * @mwdma_mask: mwdma_mask
788 * @udma_mask: udma_mask
789 *
790 * Pack @pio_mask, @mwdma_mask and @udma_mask into a single
791 * unsigned int xfer_mask.
792 *
793 * LOCKING:
794 * None.
795 *
796 * RETURNS:
797 * Packed xfer_mask.
798 */
Tejun Heo7dc951a2007-11-27 19:43:42 +0900799unsigned long ata_pack_xfermask(unsigned long pio_mask,
800 unsigned long mwdma_mask,
801 unsigned long udma_mask)
Tejun Heocb95d562006-03-06 04:31:56 +0900802{
803 return ((pio_mask << ATA_SHIFT_PIO) & ATA_MASK_PIO) |
804 ((mwdma_mask << ATA_SHIFT_MWDMA) & ATA_MASK_MWDMA) |
805 ((udma_mask << ATA_SHIFT_UDMA) & ATA_MASK_UDMA);
806}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +0100807EXPORT_SYMBOL_GPL(ata_pack_xfermask);
Tejun Heocb95d562006-03-06 04:31:56 +0900808
Tejun Heoc0489e42006-03-24 14:07:49 +0900809/**
810 * ata_unpack_xfermask - Unpack xfer_mask into pio, mwdma and udma masks
811 * @xfer_mask: xfer_mask to unpack
812 * @pio_mask: resulting pio_mask
813 * @mwdma_mask: resulting mwdma_mask
814 * @udma_mask: resulting udma_mask
815 *
816 * Unpack @xfer_mask into @pio_mask, @mwdma_mask and @udma_mask.
Masanari Iidac9b55602016-04-13 23:36:27 +0900817 * Any NULL destination masks will be ignored.
Tejun Heoc0489e42006-03-24 14:07:49 +0900818 */
Tejun Heo7dc951a2007-11-27 19:43:42 +0900819void ata_unpack_xfermask(unsigned long xfer_mask, unsigned long *pio_mask,
820 unsigned long *mwdma_mask, unsigned long *udma_mask)
Tejun Heoc0489e42006-03-24 14:07:49 +0900821{
822 if (pio_mask)
823 *pio_mask = (xfer_mask & ATA_MASK_PIO) >> ATA_SHIFT_PIO;
824 if (mwdma_mask)
825 *mwdma_mask = (xfer_mask & ATA_MASK_MWDMA) >> ATA_SHIFT_MWDMA;
826 if (udma_mask)
827 *udma_mask = (xfer_mask & ATA_MASK_UDMA) >> ATA_SHIFT_UDMA;
828}
829
Tejun Heocb95d562006-03-06 04:31:56 +0900830static const struct ata_xfer_ent {
Tejun Heobe9a50c2006-03-31 22:48:52 +0900831 int shift, bits;
Tejun Heocb95d562006-03-06 04:31:56 +0900832 u8 base;
833} ata_xfer_tbl[] = {
Tejun Heo70cd0712007-11-27 19:43:40 +0900834 { ATA_SHIFT_PIO, ATA_NR_PIO_MODES, XFER_PIO_0 },
835 { ATA_SHIFT_MWDMA, ATA_NR_MWDMA_MODES, XFER_MW_DMA_0 },
836 { ATA_SHIFT_UDMA, ATA_NR_UDMA_MODES, XFER_UDMA_0 },
Tejun Heocb95d562006-03-06 04:31:56 +0900837 { -1, },
838};
839
840/**
841 * ata_xfer_mask2mode - Find matching XFER_* for the given xfer_mask
842 * @xfer_mask: xfer_mask of interest
843 *
844 * Return matching XFER_* value for @xfer_mask. Only the highest
845 * bit of @xfer_mask is considered.
846 *
847 * LOCKING:
848 * None.
849 *
850 * RETURNS:
Tejun Heo70cd0712007-11-27 19:43:40 +0900851 * Matching XFER_* value, 0xff if no match found.
Tejun Heocb95d562006-03-06 04:31:56 +0900852 */
Tejun Heo7dc951a2007-11-27 19:43:42 +0900853u8 ata_xfer_mask2mode(unsigned long xfer_mask)
Tejun Heocb95d562006-03-06 04:31:56 +0900854{
855 int highbit = fls(xfer_mask) - 1;
856 const struct ata_xfer_ent *ent;
857
858 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
859 if (highbit >= ent->shift && highbit < ent->shift + ent->bits)
860 return ent->base + highbit - ent->shift;
Tejun Heo70cd0712007-11-27 19:43:40 +0900861 return 0xff;
Tejun Heocb95d562006-03-06 04:31:56 +0900862}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +0100863EXPORT_SYMBOL_GPL(ata_xfer_mask2mode);
Tejun Heocb95d562006-03-06 04:31:56 +0900864
865/**
866 * ata_xfer_mode2mask - Find matching xfer_mask for XFER_*
867 * @xfer_mode: XFER_* of interest
868 *
869 * Return matching xfer_mask for @xfer_mode.
870 *
871 * LOCKING:
872 * None.
873 *
874 * RETURNS:
875 * Matching xfer_mask, 0 if no match found.
876 */
Tejun Heo7dc951a2007-11-27 19:43:42 +0900877unsigned long ata_xfer_mode2mask(u8 xfer_mode)
Tejun Heocb95d562006-03-06 04:31:56 +0900878{
879 const struct ata_xfer_ent *ent;
880
881 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
882 if (xfer_mode >= ent->base && xfer_mode < ent->base + ent->bits)
Tejun Heo70cd0712007-11-27 19:43:40 +0900883 return ((2 << (ent->shift + xfer_mode - ent->base)) - 1)
884 & ~((1 << ent->shift) - 1);
Tejun Heocb95d562006-03-06 04:31:56 +0900885 return 0;
886}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +0100887EXPORT_SYMBOL_GPL(ata_xfer_mode2mask);
Tejun Heocb95d562006-03-06 04:31:56 +0900888
889/**
890 * ata_xfer_mode2shift - Find matching xfer_shift for XFER_*
891 * @xfer_mode: XFER_* of interest
892 *
893 * Return matching xfer_shift for @xfer_mode.
894 *
895 * LOCKING:
896 * None.
897 *
898 * RETURNS:
899 * Matching xfer_shift, -1 if no match found.
900 */
Tejun Heo7dc951a2007-11-27 19:43:42 +0900901int ata_xfer_mode2shift(unsigned long xfer_mode)
Tejun Heocb95d562006-03-06 04:31:56 +0900902{
903 const struct ata_xfer_ent *ent;
904
905 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
906 if (xfer_mode >= ent->base && xfer_mode < ent->base + ent->bits)
907 return ent->shift;
908 return -1;
909}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +0100910EXPORT_SYMBOL_GPL(ata_xfer_mode2shift);
Tejun Heocb95d562006-03-06 04:31:56 +0900911
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912/**
Tejun Heo1da7b0d2006-03-06 04:31:56 +0900913 * ata_mode_string - convert xfer_mask to string
914 * @xfer_mask: mask of bits supported; only highest bit counts.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 *
916 * Determine string which represents the highest speed
Tejun Heo1da7b0d2006-03-06 04:31:56 +0900917 * (highest bit in @modemask).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 *
919 * LOCKING:
920 * None.
921 *
922 * RETURNS:
923 * Constant C string representing highest speed listed in
Tejun Heo1da7b0d2006-03-06 04:31:56 +0900924 * @mode_mask, or the constant C string "<n/a>".
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 */
Tejun Heo7dc951a2007-11-27 19:43:42 +0900926const char *ata_mode_string(unsigned long xfer_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927{
Tejun Heo75f554b2006-03-06 04:31:57 +0900928 static const char * const xfer_mode_str[] = {
929 "PIO0",
930 "PIO1",
931 "PIO2",
932 "PIO3",
933 "PIO4",
Alan Coxb352e572006-08-10 18:52:12 +0100934 "PIO5",
935 "PIO6",
Tejun Heo75f554b2006-03-06 04:31:57 +0900936 "MWDMA0",
937 "MWDMA1",
938 "MWDMA2",
Alan Coxb352e572006-08-10 18:52:12 +0100939 "MWDMA3",
940 "MWDMA4",
Tejun Heo75f554b2006-03-06 04:31:57 +0900941 "UDMA/16",
942 "UDMA/25",
943 "UDMA/33",
944 "UDMA/44",
945 "UDMA/66",
946 "UDMA/100",
947 "UDMA/133",
948 "UDMA7",
949 };
Tejun Heo1da7b0d2006-03-06 04:31:56 +0900950 int highbit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951
Tejun Heo1da7b0d2006-03-06 04:31:56 +0900952 highbit = fls(xfer_mask) - 1;
953 if (highbit >= 0 && highbit < ARRAY_SIZE(xfer_mode_str))
954 return xfer_mode_str[highbit];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 return "<n/a>";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +0100957EXPORT_SYMBOL_GPL(ata_mode_string);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958
Gwendal Grignoud9027472010-05-25 12:31:38 -0700959const char *sata_spd_string(unsigned int spd)
Tejun Heo4c360c82006-04-01 01:38:17 +0900960{
961 static const char * const spd_str[] = {
962 "1.5 Gbps",
963 "3.0 Gbps",
Shane Huang8522ee22008-12-30 11:00:37 +0800964 "6.0 Gbps",
Tejun Heo4c360c82006-04-01 01:38:17 +0900965 };
966
967 if (spd == 0 || (spd - 1) >= ARRAY_SIZE(spd_str))
968 return "<unknown>";
969 return spd_str[spd - 1];
970}
971
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972/**
973 * ata_dev_classify - determine device type based on ATA-spec signature
974 * @tf: ATA taskfile register set for device to be identified
975 *
976 * Determine from taskfile register contents whether a device is
977 * ATA or ATAPI, as per "Signature and persistence" section
978 * of ATA/PI spec (volume 1, sect 5.14).
979 *
980 * LOCKING:
981 * None.
982 *
983 * RETURNS:
Hannes Reinecke9162c652014-11-05 13:08:21 +0100984 * Device type, %ATA_DEV_ATA, %ATA_DEV_ATAPI, %ATA_DEV_PMP,
985 * %ATA_DEV_ZAC, or %ATA_DEV_UNKNOWN the event of failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 */
Jeff Garzik057ace52005-10-22 14:27:05 -0400987unsigned int ata_dev_classify(const struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988{
989 /* Apple's open source Darwin code hints that some devices only
990 * put a proper signature into the LBA mid/high registers,
991 * So, we only check those. It's sufficient for uniqueness.
Tejun Heo633273a2007-09-23 13:19:54 +0900992 *
993 * ATA/ATAPI-7 (d1532v1r1: Feb. 19, 2003) specified separate
994 * signatures for ATA and ATAPI devices attached on SerialATA,
995 * 0x3c/0xc3 and 0x69/0x96 respectively. However, SerialATA
996 * spec has never mentioned about using different signatures
997 * for ATA/ATAPI devices. Then, Serial ATA II: Port
998 * Multiplier specification began to use 0x69/0x96 to identify
999 * port multpliers and 0x3c/0xc3 to identify SEMB device.
1000 * ATA/ATAPI-7 dropped descriptions about 0x3c/0xc3 and
1001 * 0x69/0x96 shortly and described them as reserved for
1002 * SerialATA.
1003 *
1004 * We follow the current spec and consider that 0x69/0x96
1005 * identifies a port multiplier and 0x3c/0xc3 a SEMB device.
Tejun Heo79b42ba2009-04-15 06:21:10 +09001006 * Unfortunately, WDC WD1600JS-62MHB5 (a hard drive) reports
1007 * SEMB signature. This is worked around in
1008 * ata_dev_read_id().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 */
Tejun Heo633273a2007-09-23 13:19:54 +09001010 if ((tf->lbam == 0) && (tf->lbah == 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 DPRINTK("found ATA device by sig\n");
1012 return ATA_DEV_ATA;
1013 }
1014
Tejun Heo633273a2007-09-23 13:19:54 +09001015 if ((tf->lbam == 0x14) && (tf->lbah == 0xeb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 DPRINTK("found ATAPI device by sig\n");
1017 return ATA_DEV_ATAPI;
1018 }
1019
Tejun Heo633273a2007-09-23 13:19:54 +09001020 if ((tf->lbam == 0x69) && (tf->lbah == 0x96)) {
1021 DPRINTK("found PMP device by sig\n");
1022 return ATA_DEV_PMP;
1023 }
1024
1025 if ((tf->lbam == 0x3c) && (tf->lbah == 0xc3)) {
Tejun Heo79b42ba2009-04-15 06:21:10 +09001026 DPRINTK("found SEMB device by sig (could be ATA device)\n");
1027 return ATA_DEV_SEMB;
Tejun Heo633273a2007-09-23 13:19:54 +09001028 }
1029
Hannes Reinecke9162c652014-11-05 13:08:21 +01001030 if ((tf->lbam == 0xcd) && (tf->lbah == 0xab)) {
1031 DPRINTK("found ZAC device by sig\n");
1032 return ATA_DEV_ZAC;
1033 }
1034
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 DPRINTK("unknown device\n");
1036 return ATA_DEV_UNKNOWN;
1037}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01001038EXPORT_SYMBOL_GPL(ata_dev_classify);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039
1040/**
Tejun Heo6a62a042006-02-13 10:02:46 +09001041 * ata_id_string - Convert IDENTIFY DEVICE page into string
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 * @id: IDENTIFY DEVICE results we will examine
1043 * @s: string into which data is output
1044 * @ofs: offset into identify device page
1045 * @len: length of string to return. must be an even number.
1046 *
1047 * The strings in the IDENTIFY DEVICE page are broken up into
1048 * 16-bit chunks. Run through the string, and output each
1049 * 8-bit chunk linearly, regardless of platform.
1050 *
1051 * LOCKING:
1052 * caller.
1053 */
1054
Tejun Heo6a62a042006-02-13 10:02:46 +09001055void ata_id_string(const u16 *id, unsigned char *s,
1056 unsigned int ofs, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057{
1058 unsigned int c;
1059
Alan Cox963e4972008-07-24 17:16:06 +01001060 BUG_ON(len & 1);
1061
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 while (len > 0) {
1063 c = id[ofs] >> 8;
1064 *s = c;
1065 s++;
1066
1067 c = id[ofs] & 0xff;
1068 *s = c;
1069 s++;
1070
1071 ofs++;
1072 len -= 2;
1073 }
1074}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01001075EXPORT_SYMBOL_GPL(ata_id_string);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076
Tejun Heo0e949ff2006-02-12 22:47:04 +09001077/**
Tejun Heo6a62a042006-02-13 10:02:46 +09001078 * ata_id_c_string - Convert IDENTIFY DEVICE page into C string
Tejun Heo0e949ff2006-02-12 22:47:04 +09001079 * @id: IDENTIFY DEVICE results we will examine
1080 * @s: string into which data is output
1081 * @ofs: offset into identify device page
1082 * @len: length of string to return. must be an odd number.
1083 *
Tejun Heo6a62a042006-02-13 10:02:46 +09001084 * This function is identical to ata_id_string except that it
Tejun Heo0e949ff2006-02-12 22:47:04 +09001085 * trims trailing spaces and terminates the resulting string with
1086 * null. @len must be actual maximum length (even number) + 1.
1087 *
1088 * LOCKING:
1089 * caller.
1090 */
Tejun Heo6a62a042006-02-13 10:02:46 +09001091void ata_id_c_string(const u16 *id, unsigned char *s,
1092 unsigned int ofs, unsigned int len)
Tejun Heo0e949ff2006-02-12 22:47:04 +09001093{
1094 unsigned char *p;
1095
Tejun Heo6a62a042006-02-13 10:02:46 +09001096 ata_id_string(id, s, ofs, len - 1);
Tejun Heo0e949ff2006-02-12 22:47:04 +09001097
1098 p = s + strnlen(s, len - 1);
1099 while (p > s && p[-1] == ' ')
1100 p--;
1101 *p = '\0';
1102}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01001103EXPORT_SYMBOL_GPL(ata_id_c_string);
Edward Falk0baab862005-06-02 18:17:13 -04001104
Tejun Heodb6f8752007-09-03 12:31:58 +09001105static u64 ata_id_n_sectors(const u16 *id)
1106{
1107 if (ata_id_has_lba(id)) {
1108 if (ata_id_has_lba48(id))
Robert Hancock968e5942009-02-16 20:15:08 -06001109 return ata_id_u64(id, ATA_ID_LBA_CAPACITY_2);
Tejun Heodb6f8752007-09-03 12:31:58 +09001110 else
Robert Hancock968e5942009-02-16 20:15:08 -06001111 return ata_id_u32(id, ATA_ID_LBA_CAPACITY);
Tejun Heodb6f8752007-09-03 12:31:58 +09001112 } else {
1113 if (ata_id_current_chs_valid(id))
Robert Hancock968e5942009-02-16 20:15:08 -06001114 return id[ATA_ID_CUR_CYLS] * id[ATA_ID_CUR_HEADS] *
1115 id[ATA_ID_CUR_SECTORS];
Tejun Heodb6f8752007-09-03 12:31:58 +09001116 else
Robert Hancock968e5942009-02-16 20:15:08 -06001117 return id[ATA_ID_CYLS] * id[ATA_ID_HEADS] *
1118 id[ATA_ID_SECTORS];
Tejun Heodb6f8752007-09-03 12:31:58 +09001119 }
1120}
1121
Tejun Heoa5987e02008-03-27 19:14:23 +09001122u64 ata_tf_to_lba48(const struct ata_taskfile *tf)
Alan Cox1e999732007-04-11 00:23:13 +01001123{
1124 u64 sectors = 0;
1125
1126 sectors |= ((u64)(tf->hob_lbah & 0xff)) << 40;
1127 sectors |= ((u64)(tf->hob_lbam & 0xff)) << 32;
Roland Dreierba14a9c2008-10-28 16:52:20 -07001128 sectors |= ((u64)(tf->hob_lbal & 0xff)) << 24;
Alan Cox1e999732007-04-11 00:23:13 +01001129 sectors |= (tf->lbah & 0xff) << 16;
1130 sectors |= (tf->lbam & 0xff) << 8;
1131 sectors |= (tf->lbal & 0xff);
1132
Tejun Heoa5987e02008-03-27 19:14:23 +09001133 return sectors;
Alan Cox1e999732007-04-11 00:23:13 +01001134}
1135
Tejun Heoa5987e02008-03-27 19:14:23 +09001136u64 ata_tf_to_lba(const struct ata_taskfile *tf)
Alan Cox1e999732007-04-11 00:23:13 +01001137{
1138 u64 sectors = 0;
1139
1140 sectors |= (tf->device & 0x0f) << 24;
1141 sectors |= (tf->lbah & 0xff) << 16;
1142 sectors |= (tf->lbam & 0xff) << 8;
1143 sectors |= (tf->lbal & 0xff);
1144
Tejun Heoa5987e02008-03-27 19:14:23 +09001145 return sectors;
Alan Cox1e999732007-04-11 00:23:13 +01001146}
1147
1148/**
Tejun Heoc728a912007-09-03 12:32:30 +09001149 * ata_read_native_max_address - Read native max address
1150 * @dev: target device
1151 * @max_sectors: out parameter for the result native max address
Alan Cox1e999732007-04-11 00:23:13 +01001152 *
Tejun Heoc728a912007-09-03 12:32:30 +09001153 * Perform an LBA48 or LBA28 native size query upon the device in
1154 * question.
1155 *
1156 * RETURNS:
1157 * 0 on success, -EACCES if command is aborted by the drive.
1158 * -EIO on other errors.
Alan Cox1e999732007-04-11 00:23:13 +01001159 */
Tejun Heoc728a912007-09-03 12:32:30 +09001160static int ata_read_native_max_address(struct ata_device *dev, u64 *max_sectors)
Alan Cox1e999732007-04-11 00:23:13 +01001161{
Tejun Heoc728a912007-09-03 12:32:30 +09001162 unsigned int err_mask;
Alan Cox1e999732007-04-11 00:23:13 +01001163 struct ata_taskfile tf;
Tejun Heoc728a912007-09-03 12:32:30 +09001164 int lba48 = ata_id_has_lba48(dev->id);
Alan Cox1e999732007-04-11 00:23:13 +01001165
1166 ata_tf_init(dev, &tf);
1167
Tejun Heoc728a912007-09-03 12:32:30 +09001168 /* always clear all address registers */
Alan Cox1e999732007-04-11 00:23:13 +01001169 tf.flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
Tejun Heoc728a912007-09-03 12:32:30 +09001170
1171 if (lba48) {
1172 tf.command = ATA_CMD_READ_NATIVE_MAX_EXT;
1173 tf.flags |= ATA_TFLAG_LBA48;
1174 } else
1175 tf.command = ATA_CMD_READ_NATIVE_MAX;
1176
Hannes Reineckebd18bc02016-07-14 09:05:46 +09001177 tf.protocol = ATA_PROT_NODATA;
Tejun Heoc728a912007-09-03 12:32:30 +09001178 tf.device |= ATA_LBA;
Alan Cox1e999732007-04-11 00:23:13 +01001179
Tejun Heo2b789102007-10-09 15:05:44 +09001180 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
Tejun Heoc728a912007-09-03 12:32:30 +09001181 if (err_mask) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07001182 ata_dev_warn(dev,
1183 "failed to read native max address (err_mask=0x%x)\n",
1184 err_mask);
Tejun Heoc728a912007-09-03 12:32:30 +09001185 if (err_mask == AC_ERR_DEV && (tf.feature & ATA_ABORTED))
1186 return -EACCES;
1187 return -EIO;
1188 }
Alan Cox1e999732007-04-11 00:23:13 +01001189
Tejun Heoc728a912007-09-03 12:32:30 +09001190 if (lba48)
Tejun Heoa5987e02008-03-27 19:14:23 +09001191 *max_sectors = ata_tf_to_lba48(&tf) + 1;
Tejun Heoc728a912007-09-03 12:32:30 +09001192 else
Tejun Heoa5987e02008-03-27 19:14:23 +09001193 *max_sectors = ata_tf_to_lba(&tf) + 1;
Jeff Garzik2dcb4072007-10-19 06:42:56 -04001194 if (dev->horkage & ATA_HORKAGE_HPA_SIZE)
Alan Cox93328e112007-09-29 04:06:48 -04001195 (*max_sectors)--;
Tejun Heoc728a912007-09-03 12:32:30 +09001196 return 0;
Alan Cox1e999732007-04-11 00:23:13 +01001197}
1198
1199/**
Tejun Heoc728a912007-09-03 12:32:30 +09001200 * ata_set_max_sectors - Set max sectors
1201 * @dev: target device
Randy Dunlap6b38d1d2007-05-01 17:35:55 -07001202 * @new_sectors: new max sectors value to set for the device
Alan Cox1e999732007-04-11 00:23:13 +01001203 *
Tejun Heoc728a912007-09-03 12:32:30 +09001204 * Set max sectors of @dev to @new_sectors.
1205 *
1206 * RETURNS:
1207 * 0 on success, -EACCES if command is aborted or denied (due to
1208 * previous non-volatile SET_MAX) by the drive. -EIO on other
1209 * errors.
Alan Cox1e999732007-04-11 00:23:13 +01001210 */
Tejun Heo05027ad2007-09-03 12:32:57 +09001211static int ata_set_max_sectors(struct ata_device *dev, u64 new_sectors)
Alan Cox1e999732007-04-11 00:23:13 +01001212{
Tejun Heoc728a912007-09-03 12:32:30 +09001213 unsigned int err_mask;
Alan Cox1e999732007-04-11 00:23:13 +01001214 struct ata_taskfile tf;
Tejun Heoc728a912007-09-03 12:32:30 +09001215 int lba48 = ata_id_has_lba48(dev->id);
Alan Cox1e999732007-04-11 00:23:13 +01001216
1217 new_sectors--;
1218
1219 ata_tf_init(dev, &tf);
1220
Alan Cox1e999732007-04-11 00:23:13 +01001221 tf.flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
Tejun Heoc728a912007-09-03 12:32:30 +09001222
1223 if (lba48) {
1224 tf.command = ATA_CMD_SET_MAX_EXT;
1225 tf.flags |= ATA_TFLAG_LBA48;
1226
1227 tf.hob_lbal = (new_sectors >> 24) & 0xff;
1228 tf.hob_lbam = (new_sectors >> 32) & 0xff;
1229 tf.hob_lbah = (new_sectors >> 40) & 0xff;
Tejun Heo1e582ba2007-09-21 20:07:14 +09001230 } else {
Tejun Heoc728a912007-09-03 12:32:30 +09001231 tf.command = ATA_CMD_SET_MAX;
1232
Tejun Heo1e582ba2007-09-21 20:07:14 +09001233 tf.device |= (new_sectors >> 24) & 0xf;
1234 }
1235
Hannes Reineckebd18bc02016-07-14 09:05:46 +09001236 tf.protocol = ATA_PROT_NODATA;
Tejun Heoc728a912007-09-03 12:32:30 +09001237 tf.device |= ATA_LBA;
Alan Cox1e999732007-04-11 00:23:13 +01001238
1239 tf.lbal = (new_sectors >> 0) & 0xff;
1240 tf.lbam = (new_sectors >> 8) & 0xff;
1241 tf.lbah = (new_sectors >> 16) & 0xff;
Alan Cox1e999732007-04-11 00:23:13 +01001242
Tejun Heo2b789102007-10-09 15:05:44 +09001243 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
Tejun Heoc728a912007-09-03 12:32:30 +09001244 if (err_mask) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07001245 ata_dev_warn(dev,
1246 "failed to set max address (err_mask=0x%x)\n",
1247 err_mask);
Tejun Heoc728a912007-09-03 12:32:30 +09001248 if (err_mask == AC_ERR_DEV &&
1249 (tf.feature & (ATA_ABORTED | ATA_IDNF)))
1250 return -EACCES;
1251 return -EIO;
1252 }
Alan Cox1e999732007-04-11 00:23:13 +01001253
Tejun Heoc728a912007-09-03 12:32:30 +09001254 return 0;
Alan Cox1e999732007-04-11 00:23:13 +01001255}
1256
1257/**
1258 * ata_hpa_resize - Resize a device with an HPA set
1259 * @dev: Device to resize
1260 *
1261 * Read the size of an LBA28 or LBA48 disk with HPA features and resize
1262 * it if required to the full size of the media. The caller must check
1263 * the drive has the HPA feature set enabled.
Tejun Heo05027ad2007-09-03 12:32:57 +09001264 *
1265 * RETURNS:
1266 * 0 on success, -errno on failure.
Alan Cox1e999732007-04-11 00:23:13 +01001267 */
Tejun Heo05027ad2007-09-03 12:32:57 +09001268static int ata_hpa_resize(struct ata_device *dev)
Alan Cox1e999732007-04-11 00:23:13 +01001269{
Tejun Heo05027ad2007-09-03 12:32:57 +09001270 struct ata_eh_context *ehc = &dev->link->eh_context;
1271 int print_info = ehc->i.flags & ATA_EHI_PRINTINFO;
Tejun Heo445d2112010-04-05 10:33:13 +09001272 bool unlock_hpa = ata_ignore_hpa || dev->flags & ATA_DFLAG_UNLOCK_HPA;
Tejun Heo05027ad2007-09-03 12:32:57 +09001273 u64 sectors = ata_id_n_sectors(dev->id);
1274 u64 native_sectors;
Tejun Heoc728a912007-09-03 12:32:30 +09001275 int rc;
Jeff Garzika617c092007-05-21 20:14:23 -04001276
Tejun Heo05027ad2007-09-03 12:32:57 +09001277 /* do we need to do it? */
Hannes Reinecke9162c652014-11-05 13:08:21 +01001278 if ((dev->class != ATA_DEV_ATA && dev->class != ATA_DEV_ZAC) ||
Tejun Heo05027ad2007-09-03 12:32:57 +09001279 !ata_id_has_lba(dev->id) || !ata_id_hpa_enabled(dev->id) ||
1280 (dev->horkage & ATA_HORKAGE_BROKEN_HPA))
Tejun Heoc728a912007-09-03 12:32:30 +09001281 return 0;
Alan Cox1e999732007-04-11 00:23:13 +01001282
Tejun Heo05027ad2007-09-03 12:32:57 +09001283 /* read native max address */
1284 rc = ata_read_native_max_address(dev, &native_sectors);
1285 if (rc) {
Tejun Heodda7aba2008-03-23 21:05:15 +09001286 /* If device aborted the command or HPA isn't going to
1287 * be unlocked, skip HPA resizing.
Tejun Heo05027ad2007-09-03 12:32:57 +09001288 */
Tejun Heo445d2112010-04-05 10:33:13 +09001289 if (rc == -EACCES || !unlock_hpa) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07001290 ata_dev_warn(dev,
1291 "HPA support seems broken, skipping HPA handling\n");
Tejun Heo05027ad2007-09-03 12:32:57 +09001292 dev->horkage |= ATA_HORKAGE_BROKEN_HPA;
Alan Cox1e999732007-04-11 00:23:13 +01001293
Tejun Heo05027ad2007-09-03 12:32:57 +09001294 /* we can continue if device aborted the command */
1295 if (rc == -EACCES)
1296 rc = 0;
Alan Cox1e999732007-04-11 00:23:13 +01001297 }
Tejun Heo37301a52007-06-25 20:45:54 +09001298
Tejun Heo05027ad2007-09-03 12:32:57 +09001299 return rc;
1300 }
Tejun Heo5920dad2009-07-15 17:11:41 +09001301 dev->n_native_sectors = native_sectors;
Tejun Heo05027ad2007-09-03 12:32:57 +09001302
1303 /* nothing to do? */
Tejun Heo445d2112010-04-05 10:33:13 +09001304 if (native_sectors <= sectors || !unlock_hpa) {
Tejun Heo05027ad2007-09-03 12:32:57 +09001305 if (!print_info || native_sectors == sectors)
1306 return 0;
1307
1308 if (native_sectors > sectors)
Joe Perchesa9a79df2011-04-15 15:51:59 -07001309 ata_dev_info(dev,
Tejun Heo05027ad2007-09-03 12:32:57 +09001310 "HPA detected: current %llu, native %llu\n",
1311 (unsigned long long)sectors,
1312 (unsigned long long)native_sectors);
1313 else if (native_sectors < sectors)
Joe Perchesa9a79df2011-04-15 15:51:59 -07001314 ata_dev_warn(dev,
1315 "native sectors (%llu) is smaller than sectors (%llu)\n",
Tejun Heo05027ad2007-09-03 12:32:57 +09001316 (unsigned long long)native_sectors,
1317 (unsigned long long)sectors);
1318 return 0;
1319 }
1320
1321 /* let's unlock HPA */
1322 rc = ata_set_max_sectors(dev, native_sectors);
1323 if (rc == -EACCES) {
1324 /* if device aborted the command, skip HPA resizing */
Joe Perchesa9a79df2011-04-15 15:51:59 -07001325 ata_dev_warn(dev,
1326 "device aborted resize (%llu -> %llu), skipping HPA handling\n",
1327 (unsigned long long)sectors,
1328 (unsigned long long)native_sectors);
Tejun Heo05027ad2007-09-03 12:32:57 +09001329 dev->horkage |= ATA_HORKAGE_BROKEN_HPA;
1330 return 0;
1331 } else if (rc)
1332 return rc;
1333
1334 /* re-read IDENTIFY data */
1335 rc = ata_dev_reread_id(dev, 0);
1336 if (rc) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07001337 ata_dev_err(dev,
1338 "failed to re-read IDENTIFY data after HPA resizing\n");
Tejun Heo05027ad2007-09-03 12:32:57 +09001339 return rc;
1340 }
1341
1342 if (print_info) {
1343 u64 new_sectors = ata_id_n_sectors(dev->id);
Joe Perchesa9a79df2011-04-15 15:51:59 -07001344 ata_dev_info(dev,
Tejun Heo05027ad2007-09-03 12:32:57 +09001345 "HPA unlocked: %llu -> %llu, native %llu\n",
1346 (unsigned long long)sectors,
1347 (unsigned long long)new_sectors,
1348 (unsigned long long)native_sectors);
1349 }
1350
1351 return 0;
Alan Cox1e999732007-04-11 00:23:13 +01001352}
1353
Edward Falk0baab862005-06-02 18:17:13 -04001354/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 * ata_dump_id - IDENTIFY DEVICE info debugging output
Tejun Heo0bd33002006-02-12 22:47:05 +09001356 * @id: IDENTIFY DEVICE page to dump
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 *
Tejun Heo0bd33002006-02-12 22:47:05 +09001358 * Dump selected 16-bit words from the given IDENTIFY DEVICE
1359 * page.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 *
1361 * LOCKING:
1362 * caller.
1363 */
1364
Tejun Heo0bd33002006-02-12 22:47:05 +09001365static inline void ata_dump_id(const u16 *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366{
1367 DPRINTK("49==0x%04x "
1368 "53==0x%04x "
1369 "63==0x%04x "
1370 "64==0x%04x "
1371 "75==0x%04x \n",
Tejun Heo0bd33002006-02-12 22:47:05 +09001372 id[49],
1373 id[53],
1374 id[63],
1375 id[64],
1376 id[75]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 DPRINTK("80==0x%04x "
1378 "81==0x%04x "
1379 "82==0x%04x "
1380 "83==0x%04x "
1381 "84==0x%04x \n",
Tejun Heo0bd33002006-02-12 22:47:05 +09001382 id[80],
1383 id[81],
1384 id[82],
1385 id[83],
1386 id[84]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 DPRINTK("88==0x%04x "
1388 "93==0x%04x\n",
Tejun Heo0bd33002006-02-12 22:47:05 +09001389 id[88],
1390 id[93]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391}
1392
Tejun Heocb95d562006-03-06 04:31:56 +09001393/**
1394 * ata_id_xfermask - Compute xfermask from the given IDENTIFY data
1395 * @id: IDENTIFY data to compute xfer mask from
1396 *
1397 * Compute the xfermask for this device. This is not as trivial
1398 * as it seems if we must consider early devices correctly.
1399 *
1400 * FIXME: pre IDE drive timing (do we care ?).
1401 *
1402 * LOCKING:
1403 * None.
1404 *
1405 * RETURNS:
1406 * Computed xfermask
1407 */
Tejun Heo7dc951a2007-11-27 19:43:42 +09001408unsigned long ata_id_xfermask(const u16 *id)
Tejun Heocb95d562006-03-06 04:31:56 +09001409{
Tejun Heo7dc951a2007-11-27 19:43:42 +09001410 unsigned long pio_mask, mwdma_mask, udma_mask;
Tejun Heocb95d562006-03-06 04:31:56 +09001411
1412 /* Usual case. Word 53 indicates word 64 is valid */
1413 if (id[ATA_ID_FIELD_VALID] & (1 << 1)) {
1414 pio_mask = id[ATA_ID_PIO_MODES] & 0x03;
1415 pio_mask <<= 3;
1416 pio_mask |= 0x7;
1417 } else {
1418 /* If word 64 isn't valid then Word 51 high byte holds
1419 * the PIO timing number for the maximum. Turn it into
1420 * a mask.
1421 */
Lennert Buytenhek7a0f1c82007-01-29 13:28:47 +01001422 u8 mode = (id[ATA_ID_OLD_PIO_MODES] >> 8) & 0xFF;
Alan Cox46767aeb2006-09-29 18:26:47 +01001423 if (mode < 5) /* Valid PIO range */
Jeff Garzik2dcb4072007-10-19 06:42:56 -04001424 pio_mask = (2 << mode) - 1;
Alan Cox46767aeb2006-09-29 18:26:47 +01001425 else
1426 pio_mask = 1;
Tejun Heocb95d562006-03-06 04:31:56 +09001427
1428 /* But wait.. there's more. Design your standards by
1429 * committee and you too can get a free iordy field to
1430 * process. However its the speeds not the modes that
1431 * are supported... Note drivers using the timing API
1432 * will get this right anyway
1433 */
1434 }
1435
1436 mwdma_mask = id[ATA_ID_MWDMA_MODES] & 0x07;
Tejun Heofb21f0d2006-03-12 12:34:35 +09001437
Alan Coxb352e572006-08-10 18:52:12 +01001438 if (ata_id_is_cfa(id)) {
1439 /*
1440 * Process compact flash extended modes
1441 */
Sergei Shtylyov62afe5d2009-04-13 20:50:00 +04001442 int pio = (id[ATA_ID_CFA_MODES] >> 0) & 0x7;
1443 int dma = (id[ATA_ID_CFA_MODES] >> 3) & 0x7;
Alan Coxb352e572006-08-10 18:52:12 +01001444
1445 if (pio)
1446 pio_mask |= (1 << 5);
1447 if (pio > 1)
1448 pio_mask |= (1 << 6);
1449 if (dma)
1450 mwdma_mask |= (1 << 3);
1451 if (dma > 1)
1452 mwdma_mask |= (1 << 4);
1453 }
1454
Tejun Heofb21f0d2006-03-12 12:34:35 +09001455 udma_mask = 0;
1456 if (id[ATA_ID_FIELD_VALID] & (1 << 2))
1457 udma_mask = id[ATA_ID_UDMA_MODES] & 0xff;
Tejun Heocb95d562006-03-06 04:31:56 +09001458
1459 return ata_pack_xfermask(pio_mask, mwdma_mask, udma_mask);
1460}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01001461EXPORT_SYMBOL_GPL(ata_id_xfermask);
Tejun Heocb95d562006-03-06 04:31:56 +09001462
Adrian Bunk7102d232007-01-04 00:09:36 +01001463static void ata_qc_complete_internal(struct ata_queued_cmd *qc)
Tejun Heoa2a7a662005-12-13 14:48:31 +09001464{
Tejun Heo77853bf2006-01-23 13:09:36 +09001465 struct completion *waiting = qc->private_data;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001466
Tejun Heoa2a7a662005-12-13 14:48:31 +09001467 complete(waiting);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001468}
1469
1470/**
Tejun Heo24326972006-11-14 22:47:09 +09001471 * ata_exec_internal_sg - execute libata internal command
Tejun Heoa2a7a662005-12-13 14:48:31 +09001472 * @dev: Device to which the command is sent
1473 * @tf: Taskfile registers for the command and the result
Tejun Heod69cf372006-04-02 18:51:53 +09001474 * @cdb: CDB for packet command
Masanari Iidae2278672014-02-18 22:54:36 +09001475 * @dma_dir: Data transfer direction of the command
Randy Dunlap5c1ad8b2007-10-18 14:12:26 -07001476 * @sgl: sg list for the data buffer of the command
Tejun Heo24326972006-11-14 22:47:09 +09001477 * @n_elem: Number of sg entries
Tejun Heo2b789102007-10-09 15:05:44 +09001478 * @timeout: Timeout in msecs (0 for default)
Tejun Heoa2a7a662005-12-13 14:48:31 +09001479 *
1480 * Executes libata internal command with timeout. @tf contains
1481 * command on entry and result on return. Timeout and error
1482 * conditions are reported via return value. No recovery action
1483 * is taken after a command times out. It's caller's duty to
1484 * clean up after timeout.
1485 *
1486 * LOCKING:
1487 * None. Should be called with kernel context, might sleep.
Tejun Heo551e8882006-06-12 14:09:49 +09001488 *
1489 * RETURNS:
1490 * Zero on success, AC_ERR_* mask on failure
Tejun Heoa2a7a662005-12-13 14:48:31 +09001491 */
Tejun Heo24326972006-11-14 22:47:09 +09001492unsigned ata_exec_internal_sg(struct ata_device *dev,
1493 struct ata_taskfile *tf, const u8 *cdb,
Jens Axboe87260212007-10-16 11:14:12 +02001494 int dma_dir, struct scatterlist *sgl,
Tejun Heo2b789102007-10-09 15:05:44 +09001495 unsigned int n_elem, unsigned long timeout)
Tejun Heoa2a7a662005-12-13 14:48:31 +09001496{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001497 struct ata_link *link = dev->link;
1498 struct ata_port *ap = link->ap;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001499 u8 command = tf->command;
Tejun Heo87fbc5a2008-05-20 02:17:54 +09001500 int auto_timeout = 0;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001501 struct ata_queued_cmd *qc;
Jens Axboe28361c42018-05-11 12:51:09 -06001502 unsigned int preempted_tag;
Jens Axboee3ed89392018-05-11 12:51:05 -06001503 u32 preempted_sactive;
1504 u64 preempted_qc_active;
Tejun Heoda917d62007-09-23 13:14:12 +09001505 int preempted_nr_active_links;
Ingo Molnar60be6b92006-07-03 00:25:26 -07001506 DECLARE_COMPLETION_ONSTACK(wait);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001507 unsigned long flags;
Tejun Heo77853bf2006-01-23 13:09:36 +09001508 unsigned int err_mask;
Tejun Heod95a7172006-05-15 20:58:14 +09001509 int rc;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001510
Jeff Garzikba6a1302006-06-22 23:46:10 -04001511 spin_lock_irqsave(ap->lock, flags);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001512
Tejun Heoe3180492006-05-15 20:58:09 +09001513 /* no internal command while frozen */
Tejun Heob51e9e52006-06-29 01:29:30 +09001514 if (ap->pflags & ATA_PFLAG_FROZEN) {
Jeff Garzikba6a1302006-06-22 23:46:10 -04001515 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoe3180492006-05-15 20:58:09 +09001516 return AC_ERR_SYSTEM;
1517 }
1518
Tejun Heo2ab7db12006-05-15 20:58:02 +09001519 /* initialize internal qc */
Jens Axboe28361c42018-05-11 12:51:09 -06001520 qc = __ata_qc_from_tag(ap, ATA_TAG_INTERNAL);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001521
Jens Axboe28361c42018-05-11 12:51:09 -06001522 qc->tag = ATA_TAG_INTERNAL;
1523 qc->hw_tag = 0;
Tejun Heo2ab7db12006-05-15 20:58:02 +09001524 qc->scsicmd = NULL;
1525 qc->ap = ap;
1526 qc->dev = dev;
1527 ata_qc_reinit(qc);
1528
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001529 preempted_tag = link->active_tag;
1530 preempted_sactive = link->sactive;
Tejun Heodedaf2b2006-05-15 21:03:43 +09001531 preempted_qc_active = ap->qc_active;
Tejun Heoda917d62007-09-23 13:14:12 +09001532 preempted_nr_active_links = ap->nr_active_links;
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001533 link->active_tag = ATA_TAG_POISON;
1534 link->sactive = 0;
Tejun Heodedaf2b2006-05-15 21:03:43 +09001535 ap->qc_active = 0;
Tejun Heoda917d62007-09-23 13:14:12 +09001536 ap->nr_active_links = 0;
Tejun Heo2ab7db12006-05-15 20:58:02 +09001537
1538 /* prepare & issue qc */
Tejun Heoa2a7a662005-12-13 14:48:31 +09001539 qc->tf = *tf;
Tejun Heod69cf372006-04-02 18:51:53 +09001540 if (cdb)
1541 memcpy(qc->cdb, cdb, ATAPI_CDB_LEN);
Vincent Pelletiere7714512013-05-18 18:44:04 +02001542
1543 /* some SATA bridges need us to indicate data xfer direction */
1544 if (tf->protocol == ATAPI_PROT_DMA && (dev->flags & ATA_DFLAG_DMADIR) &&
1545 dma_dir == DMA_FROM_DEVICE)
1546 qc->tf.feature |= ATAPI_DMADIR;
1547
Tejun Heoe61e0672006-05-15 20:57:40 +09001548 qc->flags |= ATA_QCFLAG_RESULT_TF;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001549 qc->dma_dir = dma_dir;
1550 if (dma_dir != DMA_NONE) {
Tejun Heo24326972006-11-14 22:47:09 +09001551 unsigned int i, buflen = 0;
Jens Axboe87260212007-10-16 11:14:12 +02001552 struct scatterlist *sg;
Tejun Heo24326972006-11-14 22:47:09 +09001553
Jens Axboe87260212007-10-16 11:14:12 +02001554 for_each_sg(sgl, sg, n_elem, i)
1555 buflen += sg->length;
Tejun Heo24326972006-11-14 22:47:09 +09001556
Jens Axboe87260212007-10-16 11:14:12 +02001557 ata_sg_init(qc, sgl, n_elem);
Brian King49c80422007-01-30 11:32:26 -06001558 qc->nbytes = buflen;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001559 }
1560
Tejun Heo77853bf2006-01-23 13:09:36 +09001561 qc->private_data = &wait;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001562 qc->complete_fn = ata_qc_complete_internal;
1563
Tejun Heo8e0e6942006-03-31 20:41:11 +09001564 ata_qc_issue(qc);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001565
Jeff Garzikba6a1302006-06-22 23:46:10 -04001566 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001567
Tejun Heo87fbc5a2008-05-20 02:17:54 +09001568 if (!timeout) {
1569 if (ata_probe_timeout)
1570 timeout = ata_probe_timeout * 1000;
1571 else {
1572 timeout = ata_internal_cmd_timeout(dev, command);
1573 auto_timeout = 1;
1574 }
1575 }
Tejun Heo2b789102007-10-09 15:05:44 +09001576
Tejun Heoc0c362b2010-09-06 17:57:14 +02001577 if (ap->ops->error_handler)
1578 ata_eh_release(ap);
1579
Tejun Heo2b789102007-10-09 15:05:44 +09001580 rc = wait_for_completion_timeout(&wait, msecs_to_jiffies(timeout));
Albert Lee41ade502006-03-14 11:19:04 +08001581
Tejun Heoc0c362b2010-09-06 17:57:14 +02001582 if (ap->ops->error_handler)
1583 ata_eh_acquire(ap);
1584
Tejun Heoc4291372010-05-10 21:41:38 +02001585 ata_sff_flush_pio_task(ap);
Tejun Heod95a7172006-05-15 20:58:14 +09001586
1587 if (!rc) {
Jeff Garzikba6a1302006-06-22 23:46:10 -04001588 spin_lock_irqsave(ap->lock, flags);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001589
1590 /* We're racing with irq here. If we lose, the
1591 * following test prevents us from completing the qc
Tejun Heod95a7172006-05-15 20:58:14 +09001592 * twice. If we win, the port is frozen and will be
1593 * cleaned up by ->post_internal_cmd().
Tejun Heoa2a7a662005-12-13 14:48:31 +09001594 */
Tejun Heo77853bf2006-01-23 13:09:36 +09001595 if (qc->flags & ATA_QCFLAG_ACTIVE) {
Tejun Heod95a7172006-05-15 20:58:14 +09001596 qc->err_mask |= AC_ERR_TIMEOUT;
1597
1598 if (ap->ops->error_handler)
1599 ata_port_freeze(ap);
1600 else
1601 ata_qc_complete(qc);
Tejun Heof15a1da2006-05-15 20:57:56 +09001602
Borislav Petkov0dd4b212006-06-23 02:29:08 -04001603 if (ata_msg_warn(ap))
Joe Perchesa9a79df2011-04-15 15:51:59 -07001604 ata_dev_warn(dev, "qc timeout (cmd 0x%x)\n",
1605 command);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001606 }
1607
Jeff Garzikba6a1302006-06-22 23:46:10 -04001608 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001609 }
1610
Tejun Heod95a7172006-05-15 20:58:14 +09001611 /* do post_internal_cmd */
1612 if (ap->ops->post_internal_cmd)
1613 ap->ops->post_internal_cmd(qc);
1614
Tejun Heoa51d6442007-03-20 15:24:11 +09001615 /* perform minimal error analysis */
1616 if (qc->flags & ATA_QCFLAG_FAILED) {
1617 if (qc->result_tf.command & (ATA_ERR | ATA_DF))
1618 qc->err_mask |= AC_ERR_DEV;
1619
1620 if (!qc->err_mask)
1621 qc->err_mask |= AC_ERR_OTHER;
1622
1623 if (qc->err_mask & ~AC_ERR_OTHER)
1624 qc->err_mask &= ~AC_ERR_OTHER;
Damien Le Moal2dae9952016-12-19 10:17:40 +09001625 } else if (qc->tf.command == ATA_CMD_REQ_SENSE_DATA) {
1626 qc->result_tf.command |= ATA_SENSE;
Tejun Heod95a7172006-05-15 20:58:14 +09001627 }
1628
Tejun Heo15869302006-05-15 20:57:33 +09001629 /* finish up */
Jeff Garzikba6a1302006-06-22 23:46:10 -04001630 spin_lock_irqsave(ap->lock, flags);
Tejun Heo15869302006-05-15 20:57:33 +09001631
Tejun Heoe61e0672006-05-15 20:57:40 +09001632 *tf = qc->result_tf;
Tejun Heo77853bf2006-01-23 13:09:36 +09001633 err_mask = qc->err_mask;
1634
1635 ata_qc_free(qc);
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001636 link->active_tag = preempted_tag;
1637 link->sactive = preempted_sactive;
Tejun Heodedaf2b2006-05-15 21:03:43 +09001638 ap->qc_active = preempted_qc_active;
Tejun Heoda917d62007-09-23 13:14:12 +09001639 ap->nr_active_links = preempted_nr_active_links;
Tejun Heo77853bf2006-01-23 13:09:36 +09001640
Jeff Garzikba6a1302006-06-22 23:46:10 -04001641 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heo15869302006-05-15 20:57:33 +09001642
Tejun Heo87fbc5a2008-05-20 02:17:54 +09001643 if ((err_mask & AC_ERR_TIMEOUT) && auto_timeout)
1644 ata_internal_cmd_timed_out(dev, command);
1645
Tejun Heo77853bf2006-01-23 13:09:36 +09001646 return err_mask;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001647}
1648
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649/**
Tejun Heo33480a02006-12-12 02:15:31 +09001650 * ata_exec_internal - execute libata internal command
Tejun Heo24326972006-11-14 22:47:09 +09001651 * @dev: Device to which the command is sent
1652 * @tf: Taskfile registers for the command and the result
1653 * @cdb: CDB for packet command
Masanari Iidae2278672014-02-18 22:54:36 +09001654 * @dma_dir: Data transfer direction of the command
Tejun Heo24326972006-11-14 22:47:09 +09001655 * @buf: Data buffer of the command
1656 * @buflen: Length of data buffer
Tejun Heo2b789102007-10-09 15:05:44 +09001657 * @timeout: Timeout in msecs (0 for default)
Tejun Heo24326972006-11-14 22:47:09 +09001658 *
1659 * Wrapper around ata_exec_internal_sg() which takes simple
1660 * buffer instead of sg list.
1661 *
1662 * LOCKING:
1663 * None. Should be called with kernel context, might sleep.
1664 *
1665 * RETURNS:
1666 * Zero on success, AC_ERR_* mask on failure
1667 */
1668unsigned ata_exec_internal(struct ata_device *dev,
1669 struct ata_taskfile *tf, const u8 *cdb,
Tejun Heo2b789102007-10-09 15:05:44 +09001670 int dma_dir, void *buf, unsigned int buflen,
1671 unsigned long timeout)
Tejun Heo24326972006-11-14 22:47:09 +09001672{
Tejun Heo33480a02006-12-12 02:15:31 +09001673 struct scatterlist *psg = NULL, sg;
1674 unsigned int n_elem = 0;
Tejun Heo24326972006-11-14 22:47:09 +09001675
Tejun Heo33480a02006-12-12 02:15:31 +09001676 if (dma_dir != DMA_NONE) {
1677 WARN_ON(!buf);
1678 sg_init_one(&sg, buf, buflen);
1679 psg = &sg;
1680 n_elem++;
1681 }
Tejun Heo24326972006-11-14 22:47:09 +09001682
Tejun Heo2b789102007-10-09 15:05:44 +09001683 return ata_exec_internal_sg(dev, tf, cdb, dma_dir, psg, n_elem,
1684 timeout);
Tejun Heo24326972006-11-14 22:47:09 +09001685}
1686
1687/**
Alan Cox1bc4ccf2006-01-09 17:18:14 +00001688 * ata_pio_need_iordy - check if iordy needed
1689 * @adev: ATA device
1690 *
1691 * Check if the current speed of the device requires IORDY. Used
1692 * by various controllers for chip configuration.
1693 */
Alan Cox1bc4ccf2006-01-09 17:18:14 +00001694unsigned int ata_pio_need_iordy(const struct ata_device *adev)
1695{
Tejun Heo0d9e6652009-06-11 11:04:45 +09001696 /* Don't set IORDY if we're preparing for reset. IORDY may
1697 * lead to controller lock up on certain controllers if the
1698 * port is not occupied. See bko#11703 for details.
1699 */
1700 if (adev->link->ap->pflags & ATA_PFLAG_RESETTING)
1701 return 0;
1702 /* Controller doesn't support IORDY. Probably a pointless
1703 * check as the caller should know this.
1704 */
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001705 if (adev->link->ap->flags & ATA_FLAG_NO_IORDY)
Alan Cox1bc4ccf2006-01-09 17:18:14 +00001706 return 0;
David Daney5c18c4d2008-12-10 15:39:12 -08001707 /* CF spec. r4.1 Table 22 says no iordy on PIO5 and PIO6. */
1708 if (ata_id_is_cfa(adev->id)
1709 && (adev->pio_mode == XFER_PIO_5 || adev->pio_mode == XFER_PIO_6))
1710 return 0;
Alan Cox432729f2007-03-08 23:22:59 +00001711 /* PIO3 and higher it is mandatory */
1712 if (adev->pio_mode > XFER_PIO_2)
Alan Cox1bc4ccf2006-01-09 17:18:14 +00001713 return 1;
Alan Cox432729f2007-03-08 23:22:59 +00001714 /* We turn it on when possible */
1715 if (ata_id_has_iordy(adev->id))
1716 return 1;
1717 return 0;
1718}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01001719EXPORT_SYMBOL_GPL(ata_pio_need_iordy);
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05001720
Alan Cox432729f2007-03-08 23:22:59 +00001721/**
1722 * ata_pio_mask_no_iordy - Return the non IORDY mask
1723 * @adev: ATA device
1724 *
1725 * Compute the highest mode possible if we are not using iordy. Return
1726 * -1 if no iordy mode is available.
1727 */
Alan Cox432729f2007-03-08 23:22:59 +00001728static u32 ata_pio_mask_no_iordy(const struct ata_device *adev)
1729{
Alan Cox1bc4ccf2006-01-09 17:18:14 +00001730 /* If we have no drive specific rule, then PIO 2 is non IORDY */
Alan Cox1bc4ccf2006-01-09 17:18:14 +00001731 if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE */
Alan Cox432729f2007-03-08 23:22:59 +00001732 u16 pio = adev->id[ATA_ID_EIDE_PIO];
Alan Cox1bc4ccf2006-01-09 17:18:14 +00001733 /* Is the speed faster than the drive allows non IORDY ? */
1734 if (pio) {
1735 /* This is cycle times not frequency - watch the logic! */
1736 if (pio > 240) /* PIO2 is 240nS per cycle */
Alan Cox432729f2007-03-08 23:22:59 +00001737 return 3 << ATA_SHIFT_PIO;
1738 return 7 << ATA_SHIFT_PIO;
Alan Cox1bc4ccf2006-01-09 17:18:14 +00001739 }
1740 }
Alan Cox432729f2007-03-08 23:22:59 +00001741 return 3 << ATA_SHIFT_PIO;
Alan Cox1bc4ccf2006-01-09 17:18:14 +00001742}
1743
1744/**
Alan Cox963e4972008-07-24 17:16:06 +01001745 * ata_do_dev_read_id - default ID read method
1746 * @dev: device
1747 * @tf: proposed taskfile
1748 * @id: data buffer
1749 *
1750 * Issue the identify taskfile and hand back the buffer containing
1751 * identify data. For some RAID controllers and for pre ATA devices
1752 * this function is wrapped or replaced by the driver
1753 */
1754unsigned int ata_do_dev_read_id(struct ata_device *dev,
1755 struct ata_taskfile *tf, u16 *id)
1756{
1757 return ata_exec_internal(dev, tf, NULL, DMA_FROM_DEVICE,
1758 id, sizeof(id[0]) * ATA_ID_WORDS, 0);
1759}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01001760EXPORT_SYMBOL_GPL(ata_do_dev_read_id);
Alan Cox963e4972008-07-24 17:16:06 +01001761
1762/**
Tejun Heo49016ac2006-02-21 02:12:11 +09001763 * ata_dev_read_id - Read ID data from the specified device
Tejun Heo49016ac2006-02-21 02:12:11 +09001764 * @dev: target device
1765 * @p_class: pointer to class of the target device (may be changed)
Tejun Heobff04642006-11-10 18:08:10 +09001766 * @flags: ATA_READID_* flags
Tejun Heofe635c72006-05-15 20:57:35 +09001767 * @id: buffer to read IDENTIFY data into
Tejun Heo49016ac2006-02-21 02:12:11 +09001768 *
1769 * Read ID data from the specified device. ATA_CMD_ID_ATA is
1770 * performed on ATA devices and ATA_CMD_ID_ATAPI on ATAPI
Tejun Heoaec5c3c2006-03-25 01:33:34 +09001771 * devices. This function also issues ATA_CMD_INIT_DEV_PARAMS
1772 * for pre-ATA4 drives.
Tejun Heo49016ac2006-02-21 02:12:11 +09001773 *
Alan Cox50a99012007-08-08 14:27:00 +01001774 * FIXME: ATA_CMD_ID_ATA is optional for early drives and right
Jeff Garzik2dcb4072007-10-19 06:42:56 -04001775 * now we abort if we hit that case.
Alan Cox50a99012007-08-08 14:27:00 +01001776 *
Tejun Heo49016ac2006-02-21 02:12:11 +09001777 * LOCKING:
1778 * Kernel thread context (may sleep)
1779 *
1780 * RETURNS:
1781 * 0 on success, -errno otherwise.
1782 */
Tejun Heoa9beec92006-05-31 18:27:44 +09001783int ata_dev_read_id(struct ata_device *dev, unsigned int *p_class,
Tejun Heobff04642006-11-10 18:08:10 +09001784 unsigned int flags, u16 *id)
Tejun Heo49016ac2006-02-21 02:12:11 +09001785{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001786 struct ata_port *ap = dev->link->ap;
Tejun Heo49016ac2006-02-21 02:12:11 +09001787 unsigned int class = *p_class;
Tejun Heo49016ac2006-02-21 02:12:11 +09001788 struct ata_taskfile tf;
1789 unsigned int err_mask = 0;
1790 const char *reason;
Tejun Heo79b42ba2009-04-15 06:21:10 +09001791 bool is_semb = class == ATA_DEV_SEMB;
Tejun Heo54936f82007-05-11 14:35:29 +02001792 int may_fallback = 1, tried_spinup = 0;
Tejun Heo49016ac2006-02-21 02:12:11 +09001793 int rc;
1794
Borislav Petkov0dd4b212006-06-23 02:29:08 -04001795 if (ata_msg_ctl(ap))
Joe Perchesa9a79df2011-04-15 15:51:59 -07001796 ata_dev_dbg(dev, "%s: ENTER\n", __func__);
Tejun Heo49016ac2006-02-21 02:12:11 +09001797
Alan Cox963e4972008-07-24 17:16:06 +01001798retry:
Tejun Heo3373efd2006-05-15 20:57:53 +09001799 ata_tf_init(dev, &tf);
Tejun Heo49016ac2006-02-21 02:12:11 +09001800
1801 switch (class) {
Tejun Heo79b42ba2009-04-15 06:21:10 +09001802 case ATA_DEV_SEMB:
1803 class = ATA_DEV_ATA; /* some hard drives report SEMB sig */
Gustavo A. R. Silva05b83602017-10-12 14:19:16 -05001804 /* fall through */
Tejun Heo49016ac2006-02-21 02:12:11 +09001805 case ATA_DEV_ATA:
Hannes Reinecke9162c652014-11-05 13:08:21 +01001806 case ATA_DEV_ZAC:
Tejun Heo49016ac2006-02-21 02:12:11 +09001807 tf.command = ATA_CMD_ID_ATA;
1808 break;
1809 case ATA_DEV_ATAPI:
1810 tf.command = ATA_CMD_ID_ATAPI;
1811 break;
1812 default:
1813 rc = -ENODEV;
1814 reason = "unsupported class";
1815 goto err_out;
1816 }
1817
1818 tf.protocol = ATA_PROT_PIO;
Tejun Heo81afe892007-02-07 12:37:41 -08001819
1820 /* Some devices choke if TF registers contain garbage. Make
1821 * sure those are properly initialized.
1822 */
1823 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
1824
1825 /* Device presence detection is unreliable on some
1826 * controllers. Always poll IDENTIFY if available.
1827 */
1828 tf.flags |= ATA_TFLAG_POLLING;
Tejun Heo49016ac2006-02-21 02:12:11 +09001829
Alan Cox963e4972008-07-24 17:16:06 +01001830 if (ap->ops->read_id)
1831 err_mask = ap->ops->read_id(dev, &tf, id);
1832 else
1833 err_mask = ata_do_dev_read_id(dev, &tf, id);
1834
Tejun Heo49016ac2006-02-21 02:12:11 +09001835 if (err_mask) {
Tejun Heo800b3992006-12-03 21:34:13 +09001836 if (err_mask & AC_ERR_NODEV_HINT) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07001837 ata_dev_dbg(dev, "NODEV after polling detection\n");
Tejun Heo55a8e2c2006-11-10 18:08:10 +09001838 return -ENOENT;
1839 }
1840
Tejun Heo79b42ba2009-04-15 06:21:10 +09001841 if (is_semb) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07001842 ata_dev_info(dev,
1843 "IDENTIFY failed on device w/ SEMB sig, disabled\n");
Tejun Heo79b42ba2009-04-15 06:21:10 +09001844 /* SEMB is not supported yet */
1845 *p_class = ATA_DEV_SEMB_UNSUP;
1846 return 0;
1847 }
1848
Tejun Heo1ffc1512008-03-23 15:16:53 +09001849 if ((err_mask == AC_ERR_DEV) && (tf.feature & ATA_ABORTED)) {
1850 /* Device or controller might have reported
1851 * the wrong device class. Give a shot at the
1852 * other IDENTIFY if the current one is
1853 * aborted by the device.
1854 */
1855 if (may_fallback) {
1856 may_fallback = 0;
Tejun Heo54936f82007-05-11 14:35:29 +02001857
Tejun Heo1ffc1512008-03-23 15:16:53 +09001858 if (class == ATA_DEV_ATA)
1859 class = ATA_DEV_ATAPI;
1860 else
1861 class = ATA_DEV_ATA;
1862 goto retry;
1863 }
1864
1865 /* Control reaches here iff the device aborted
1866 * both flavors of IDENTIFYs which happens
1867 * sometimes with phantom devices.
1868 */
Joe Perchesa9a79df2011-04-15 15:51:59 -07001869 ata_dev_dbg(dev,
1870 "both IDENTIFYs aborted, assuming NODEV\n");
Tejun Heo1ffc1512008-03-23 15:16:53 +09001871 return -ENOENT;
Tejun Heo54936f82007-05-11 14:35:29 +02001872 }
1873
Tejun Heo49016ac2006-02-21 02:12:11 +09001874 rc = -EIO;
1875 reason = "I/O error";
Tejun Heo49016ac2006-02-21 02:12:11 +09001876 goto err_out;
1877 }
1878
Tejun Heo43c9c592010-05-23 12:59:11 +02001879 if (dev->horkage & ATA_HORKAGE_DUMP_ID) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07001880 ata_dev_dbg(dev, "dumping IDENTIFY data, "
1881 "class=%d may_fallback=%d tried_spinup=%d\n",
1882 class, may_fallback, tried_spinup);
Tejun Heo43c9c592010-05-23 12:59:11 +02001883 print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET,
1884 16, 2, id, ATA_ID_WORDS * sizeof(*id), true);
1885 }
1886
Tejun Heo54936f82007-05-11 14:35:29 +02001887 /* Falling back doesn't make sense if ID data was read
1888 * successfully at least once.
1889 */
1890 may_fallback = 0;
1891
Tejun Heo49016ac2006-02-21 02:12:11 +09001892 swap_buf_le16(id, ATA_ID_WORDS);
1893
Tejun Heo49016ac2006-02-21 02:12:11 +09001894 /* sanity check */
Tejun Heoa4f57492006-09-12 20:35:49 -07001895 rc = -EINVAL;
Alan Cox60700682007-06-07 16:13:55 +01001896 reason = "device reports invalid type";
Tejun Heoa4f57492006-09-12 20:35:49 -07001897
Hannes Reinecke9162c652014-11-05 13:08:21 +01001898 if (class == ATA_DEV_ATA || class == ATA_DEV_ZAC) {
Tejun Heoa4f57492006-09-12 20:35:49 -07001899 if (!ata_id_is_ata(id) && !ata_id_is_cfa(id))
1900 goto err_out;
Andy Whitcroftdb63a4c2012-05-04 22:15:10 +01001901 if (ap->host->flags & ATA_HOST_IGNORE_ATA &&
1902 ata_id_is_ata(id)) {
1903 ata_dev_dbg(dev,
1904 "host indicates ignore ATA devices, ignored\n");
1905 return -ENOENT;
1906 }
Tejun Heoa4f57492006-09-12 20:35:49 -07001907 } else {
1908 if (ata_id_is_ata(id))
1909 goto err_out;
Tejun Heo49016ac2006-02-21 02:12:11 +09001910 }
1911
Mark Lord169439c2007-04-17 18:26:07 -04001912 if (!tried_spinup && (id[2] == 0x37c8 || id[2] == 0x738c)) {
1913 tried_spinup = 1;
1914 /*
1915 * Drive powered-up in standby mode, and requires a specific
1916 * SET_FEATURES spin-up subcommand before it will accept
1917 * anything other than the original IDENTIFY command.
1918 */
Jeff Garzik218f3d32007-10-25 00:33:27 -04001919 err_mask = ata_dev_set_feature(dev, SETFEATURES_SPINUP, 0);
Ryan Powerfb0582f92007-08-10 13:59:35 -07001920 if (err_mask && id[2] != 0x738c) {
Mark Lord169439c2007-04-17 18:26:07 -04001921 rc = -EIO;
1922 reason = "SPINUP failed";
1923 goto err_out;
1924 }
1925 /*
1926 * If the drive initially returned incomplete IDENTIFY info,
1927 * we now must reissue the IDENTIFY command.
1928 */
1929 if (id[2] == 0x37c8)
1930 goto retry;
1931 }
1932
Hannes Reinecke9162c652014-11-05 13:08:21 +01001933 if ((flags & ATA_READID_POSTRESET) &&
1934 (class == ATA_DEV_ATA || class == ATA_DEV_ZAC)) {
Tejun Heo49016ac2006-02-21 02:12:11 +09001935 /*
1936 * The exact sequence expected by certain pre-ATA4 drives is:
1937 * SRST RESET
Alan Cox50a99012007-08-08 14:27:00 +01001938 * IDENTIFY (optional in early ATA)
1939 * INITIALIZE DEVICE PARAMETERS (later IDE and ATA)
Tejun Heo49016ac2006-02-21 02:12:11 +09001940 * anything else..
1941 * Some drives were very specific about that exact sequence.
Alan Cox50a99012007-08-08 14:27:00 +01001942 *
1943 * Note that ATA4 says lba is mandatory so the second check
Adam Buchbinderc9404c92009-12-18 15:40:42 -05001944 * should never trigger.
Tejun Heo49016ac2006-02-21 02:12:11 +09001945 */
1946 if (ata_id_major_version(id) < 4 || !ata_id_has_lba(id)) {
Tejun Heo3373efd2006-05-15 20:57:53 +09001947 err_mask = ata_dev_init_params(dev, id[3], id[6]);
Tejun Heo49016ac2006-02-21 02:12:11 +09001948 if (err_mask) {
1949 rc = -EIO;
1950 reason = "INIT_DEV_PARAMS failed";
1951 goto err_out;
1952 }
1953
1954 /* current CHS translation info (id[53-58]) might be
1955 * changed. reread the identify device info.
1956 */
Tejun Heobff04642006-11-10 18:08:10 +09001957 flags &= ~ATA_READID_POSTRESET;
Tejun Heo49016ac2006-02-21 02:12:11 +09001958 goto retry;
1959 }
1960 }
1961
1962 *p_class = class;
Tejun Heofe635c72006-05-15 20:57:35 +09001963
Tejun Heo49016ac2006-02-21 02:12:11 +09001964 return 0;
1965
1966 err_out:
Tejun Heo88574552006-06-25 20:00:35 +09001967 if (ata_msg_warn(ap))
Joe Perchesa9a79df2011-04-15 15:51:59 -07001968 ata_dev_warn(dev, "failed to IDENTIFY (%s, err_mask=0x%x)\n",
1969 reason, err_mask);
Tejun Heo49016ac2006-02-21 02:12:11 +09001970 return rc;
1971}
1972
Christoph Hellwigf01f62c2017-06-04 14:42:20 +02001973/**
1974 * ata_read_log_page - read a specific log page
1975 * @dev: target device
1976 * @log: log to read
1977 * @page: page to read
1978 * @buf: buffer to store read page
1979 * @sectors: number of sectors to read
1980 *
1981 * Read log page using READ_LOG_EXT command.
1982 *
1983 * LOCKING:
1984 * Kernel thread context (may sleep).
1985 *
1986 * RETURNS:
1987 * 0 on success, AC_ERR_* mask otherwise.
1988 */
1989unsigned int ata_read_log_page(struct ata_device *dev, u8 log,
1990 u8 page, void *buf, unsigned int sectors)
1991{
1992 unsigned long ap_flags = dev->link->ap->flags;
1993 struct ata_taskfile tf;
1994 unsigned int err_mask;
1995 bool dma = false;
1996
1997 DPRINTK("read log page - log 0x%x, page 0x%x\n", log, page);
1998
1999 /*
2000 * Return error without actually issuing the command on controllers
2001 * which e.g. lockup on a read log page.
2002 */
2003 if (ap_flags & ATA_FLAG_NO_LOG_PAGE)
2004 return AC_ERR_DEV;
2005
2006retry:
2007 ata_tf_init(dev, &tf);
2008 if (dev->dma_mode && ata_id_has_read_log_dma_ext(dev->id) &&
Damien Le Moal7cfdfdc2017-07-10 14:45:20 +09002009 !(dev->horkage & ATA_HORKAGE_NO_DMA_LOG)) {
Christoph Hellwigf01f62c2017-06-04 14:42:20 +02002010 tf.command = ATA_CMD_READ_LOG_DMA_EXT;
2011 tf.protocol = ATA_PROT_DMA;
2012 dma = true;
2013 } else {
2014 tf.command = ATA_CMD_READ_LOG_EXT;
2015 tf.protocol = ATA_PROT_PIO;
2016 dma = false;
2017 }
2018 tf.lbal = log;
2019 tf.lbam = page;
2020 tf.nsect = sectors;
2021 tf.hob_nsect = sectors >> 8;
2022 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_LBA48 | ATA_TFLAG_DEVICE;
2023
2024 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_FROM_DEVICE,
2025 buf, sectors * ATA_SECT_SIZE, 0);
2026
2027 if (err_mask && dma) {
Damien Le Moal7cfdfdc2017-07-10 14:45:20 +09002028 dev->horkage |= ATA_HORKAGE_NO_DMA_LOG;
2029 ata_dev_warn(dev, "READ LOG DMA EXT failed, trying PIO\n");
Christoph Hellwigf01f62c2017-06-04 14:42:20 +02002030 goto retry;
2031 }
2032
2033 DPRINTK("EXIT, err_mask=%x\n", err_mask);
2034 return err_mask;
2035}
2036
Christoph Hellwigefe205a2017-06-04 14:42:21 +02002037static bool ata_log_supported(struct ata_device *dev, u8 log)
2038{
2039 struct ata_port *ap = dev->link->ap;
2040
2041 if (ata_read_log_page(dev, ATA_LOG_DIRECTORY, 0, ap->sector_buf, 1))
2042 return false;
2043 return get_unaligned_le16(&ap->sector_buf[log * 2]) ? true : false;
2044}
2045
Christoph Hellwiga0fd2452017-06-04 14:42:23 +02002046static bool ata_identify_page_supported(struct ata_device *dev, u8 page)
2047{
2048 struct ata_port *ap = dev->link->ap;
2049 unsigned int err, i;
2050
2051 if (!ata_log_supported(dev, ATA_LOG_IDENTIFY_DEVICE)) {
2052 ata_dev_warn(dev, "ATA Identify Device Log not supported\n");
2053 return false;
2054 }
2055
2056 /*
2057 * Read IDENTIFY DEVICE data log, page 0, to figure out if the page is
2058 * supported.
2059 */
2060 err = ata_read_log_page(dev, ATA_LOG_IDENTIFY_DEVICE, 0, ap->sector_buf,
2061 1);
2062 if (err) {
2063 ata_dev_info(dev,
2064 "failed to get Device Identify Log Emask 0x%x\n",
2065 err);
2066 return false;
2067 }
2068
2069 for (i = 0; i < ap->sector_buf[8]; i++) {
2070 if (ap->sector_buf[9 + i] == page)
2071 return true;
2072 }
2073
2074 return false;
2075}
2076
Tejun Heo90627122009-01-29 20:31:36 +09002077static int ata_do_link_spd_horkage(struct ata_device *dev)
2078{
2079 struct ata_link *plink = ata_dev_phys_link(dev);
2080 u32 target, target_limit;
2081
2082 if (!sata_scr_valid(plink))
2083 return 0;
2084
2085 if (dev->horkage & ATA_HORKAGE_1_5_GBPS)
2086 target = 1;
2087 else
2088 return 0;
2089
2090 target_limit = (1 << target) - 1;
2091
2092 /* if already on stricter limit, no need to push further */
2093 if (plink->sata_spd_limit <= target_limit)
2094 return 0;
2095
2096 plink->sata_spd_limit = target_limit;
2097
2098 /* Request another EH round by returning -EAGAIN if link is
2099 * going faster than the target speed. Forward progress is
2100 * guaranteed by setting sata_spd_limit to target_limit above.
2101 */
2102 if (plink->sata_spd > target) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07002103 ata_dev_info(dev, "applying link speed limit horkage to %s\n",
2104 sata_spd_string(target));
Tejun Heo90627122009-01-29 20:31:36 +09002105 return -EAGAIN;
2106 }
2107 return 0;
2108}
2109
Tejun Heo3373efd2006-05-15 20:57:53 +09002110static inline u8 ata_dev_knobble(struct ata_device *dev)
Tejun Heo4b2f3ed2006-03-01 16:09:36 +09002111{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09002112 struct ata_port *ap = dev->link->ap;
Jens Axboe9ce8e302008-08-27 15:23:18 +02002113
2114 if (ata_dev_blacklisted(dev) & ATA_HORKAGE_BRIDGE_OK)
2115 return 0;
2116
Tejun Heo9af5c9c2007-08-06 18:36:22 +09002117 return ((ap->cbl == ATA_CBL_SATA) && (!ata_id_is_sata(dev->id)));
Tejun Heo4b2f3ed2006-03-01 16:09:36 +09002118}
2119
Hannes Reinecke5a233552016-04-25 12:45:46 +02002120static void ata_dev_config_ncq_send_recv(struct ata_device *dev)
2121{
2122 struct ata_port *ap = dev->link->ap;
2123 unsigned int err_mask;
2124
Christoph Hellwigefe205a2017-06-04 14:42:21 +02002125 if (!ata_log_supported(dev, ATA_LOG_NCQ_SEND_RECV)) {
2126 ata_dev_warn(dev, "NCQ Send/Recv Log not supported\n");
Hannes Reineckefe5af0c2016-04-25 12:45:48 +02002127 return;
2128 }
Hannes Reinecke5a233552016-04-25 12:45:46 +02002129 err_mask = ata_read_log_page(dev, ATA_LOG_NCQ_SEND_RECV,
2130 0, ap->sector_buf, 1);
2131 if (err_mask) {
2132 ata_dev_dbg(dev,
2133 "failed to get NCQ Send/Recv Log Emask 0x%x\n",
2134 err_mask);
2135 } else {
2136 u8 *cmds = dev->ncq_send_recv_cmds;
2137
2138 dev->flags |= ATA_DFLAG_NCQ_SEND_RECV;
2139 memcpy(cmds, ap->sector_buf, ATA_LOG_NCQ_SEND_RECV_SIZE);
2140
2141 if (dev->horkage & ATA_HORKAGE_NO_NCQ_TRIM) {
2142 ata_dev_dbg(dev, "disabling queued TRIM support\n");
2143 cmds[ATA_LOG_NCQ_SEND_RECV_DSM_OFFSET] &=
2144 ~ATA_LOG_NCQ_SEND_RECV_DSM_TRIM;
2145 }
2146 }
2147}
2148
Hannes Reinecke284b3b72016-04-25 12:45:54 +02002149static void ata_dev_config_ncq_non_data(struct ata_device *dev)
2150{
2151 struct ata_port *ap = dev->link->ap;
2152 unsigned int err_mask;
Hannes Reinecke284b3b72016-04-25 12:45:54 +02002153
Christoph Hellwigefe205a2017-06-04 14:42:21 +02002154 if (!ata_log_supported(dev, ATA_LOG_NCQ_NON_DATA)) {
Hannes Reinecke284b3b72016-04-25 12:45:54 +02002155 ata_dev_warn(dev,
2156 "NCQ Send/Recv Log not supported\n");
2157 return;
2158 }
2159 err_mask = ata_read_log_page(dev, ATA_LOG_NCQ_NON_DATA,
2160 0, ap->sector_buf, 1);
2161 if (err_mask) {
2162 ata_dev_dbg(dev,
2163 "failed to get NCQ Non-Data Log Emask 0x%x\n",
2164 err_mask);
2165 } else {
2166 u8 *cmds = dev->ncq_non_data_cmds;
2167
2168 memcpy(cmds, ap->sector_buf, ATA_LOG_NCQ_NON_DATA_SIZE);
2169 }
2170}
2171
Adam Manzanares8e061782016-10-17 11:27:29 -07002172static void ata_dev_config_ncq_prio(struct ata_device *dev)
2173{
2174 struct ata_port *ap = dev->link->ap;
2175 unsigned int err_mask;
2176
Adam Manzanares9f56eca2016-12-13 12:00:05 -08002177 if (!(dev->flags & ATA_DFLAG_NCQ_PRIO_ENABLE)) {
2178 dev->flags &= ~ATA_DFLAG_NCQ_PRIO;
2179 return;
2180 }
2181
Adam Manzanares8e061782016-10-17 11:27:29 -07002182 err_mask = ata_read_log_page(dev,
Christoph Hellwig1d51d5f2017-06-04 14:42:22 +02002183 ATA_LOG_IDENTIFY_DEVICE,
Adam Manzanares8e061782016-10-17 11:27:29 -07002184 ATA_LOG_SATA_SETTINGS,
2185 ap->sector_buf,
2186 1);
2187 if (err_mask) {
2188 ata_dev_dbg(dev,
2189 "failed to get Identify Device data, Emask 0x%x\n",
2190 err_mask);
2191 return;
2192 }
2193
Adam Manzanares9f56eca2016-12-13 12:00:05 -08002194 if (ap->sector_buf[ATA_LOG_NCQ_PRIO_OFFSET] & BIT(3)) {
Adam Manzanares8e061782016-10-17 11:27:29 -07002195 dev->flags |= ATA_DFLAG_NCQ_PRIO;
Adam Manzanares9f56eca2016-12-13 12:00:05 -08002196 } else {
2197 dev->flags &= ~ATA_DFLAG_NCQ_PRIO;
Adam Manzanares8e061782016-10-17 11:27:29 -07002198 ata_dev_dbg(dev, "SATA page does not support priority\n");
Adam Manzanares9f56eca2016-12-13 12:00:05 -08002199 }
Adam Manzanares8e061782016-10-17 11:27:29 -07002200
2201}
2202
Shaohua Li388539f2009-07-27 09:24:35 +08002203static int ata_dev_config_ncq(struct ata_device *dev,
Tejun Heoa6e6ce82006-05-15 21:03:48 +09002204 char *desc, size_t desc_sz)
2205{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09002206 struct ata_port *ap = dev->link->ap;
Tejun Heoa6e6ce82006-05-15 21:03:48 +09002207 int hdepth = 0, ddepth = ata_id_queue_depth(dev->id);
Shaohua Li388539f2009-07-27 09:24:35 +08002208 unsigned int err_mask;
2209 char *aa_desc = "";
Tejun Heoa6e6ce82006-05-15 21:03:48 +09002210
2211 if (!ata_id_has_ncq(dev->id)) {
2212 desc[0] = '\0';
Shaohua Li388539f2009-07-27 09:24:35 +08002213 return 0;
Tejun Heoa6e6ce82006-05-15 21:03:48 +09002214 }
Bartlomiej Zolnierkiewiczcba97ea2020-03-26 16:58:09 +01002215 if (!IS_ENABLED(CONFIG_SATA_HOST))
2216 return 0;
Tejun Heo75683fe2007-07-05 13:31:27 +09002217 if (dev->horkage & ATA_HORKAGE_NONCQ) {
Alan Cox6919a0a2006-10-27 19:08:46 -07002218 snprintf(desc, desc_sz, "NCQ (not used)");
Shaohua Li388539f2009-07-27 09:24:35 +08002219 return 0;
Alan Cox6919a0a2006-10-27 19:08:46 -07002220 }
Tejun Heoa6e6ce82006-05-15 21:03:48 +09002221 if (ap->flags & ATA_FLAG_NCQ) {
Jens Axboe69278f72018-05-11 12:51:10 -06002222 hdepth = min(ap->scsi_host->can_queue, ATA_MAX_QUEUE);
Tejun Heoa6e6ce82006-05-15 21:03:48 +09002223 dev->flags |= ATA_DFLAG_NCQ;
2224 }
2225
Shaohua Li388539f2009-07-27 09:24:35 +08002226 if (!(dev->horkage & ATA_HORKAGE_BROKEN_FPDMA_AA) &&
2227 (ap->flags & ATA_FLAG_FPDMA_AA) &&
2228 ata_id_has_fpdma_aa(dev->id)) {
2229 err_mask = ata_dev_set_feature(dev, SETFEATURES_SATA_ENABLE,
2230 SATA_FPDMA_AA);
2231 if (err_mask) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07002232 ata_dev_err(dev,
2233 "failed to enable AA (error_mask=0x%x)\n",
2234 err_mask);
Shaohua Li388539f2009-07-27 09:24:35 +08002235 if (err_mask != AC_ERR_DEV) {
2236 dev->horkage |= ATA_HORKAGE_BROKEN_FPDMA_AA;
2237 return -EIO;
2238 }
2239 } else
2240 aa_desc = ", AA";
2241 }
2242
Tejun Heoa6e6ce82006-05-15 21:03:48 +09002243 if (hdepth >= ddepth)
Shaohua Li388539f2009-07-27 09:24:35 +08002244 snprintf(desc, desc_sz, "NCQ (depth %d)%s", ddepth, aa_desc);
Tejun Heoa6e6ce82006-05-15 21:03:48 +09002245 else
Shaohua Li388539f2009-07-27 09:24:35 +08002246 snprintf(desc, desc_sz, "NCQ (depth %d/%d)%s", hdepth,
2247 ddepth, aa_desc);
Marc Carinoed369112013-08-24 23:22:50 -07002248
Hannes Reinecke284b3b72016-04-25 12:45:54 +02002249 if ((ap->flags & ATA_FLAG_FPDMA_AUX)) {
2250 if (ata_id_has_ncq_send_and_recv(dev->id))
2251 ata_dev_config_ncq_send_recv(dev);
2252 if (ata_id_has_ncq_non_data(dev->id))
2253 ata_dev_config_ncq_non_data(dev);
Adam Manzanares8e061782016-10-17 11:27:29 -07002254 if (ata_id_has_ncq_prio(dev->id))
2255 ata_dev_config_ncq_prio(dev);
Marc Carinoed369112013-08-24 23:22:50 -07002256 }
2257
Shaohua Li388539f2009-07-27 09:24:35 +08002258 return 0;
Tejun Heoa6e6ce82006-05-15 21:03:48 +09002259}
2260
Hannes Reineckee87fd282016-04-04 11:43:55 +02002261static void ata_dev_config_sense_reporting(struct ata_device *dev)
2262{
2263 unsigned int err_mask;
2264
2265 if (!ata_id_has_sense_reporting(dev->id))
2266 return;
2267
2268 if (ata_id_sense_reporting_enabled(dev->id))
2269 return;
2270
2271 err_mask = ata_dev_set_feature(dev, SETFEATURE_SENSE_DATA, 0x1);
2272 if (err_mask) {
2273 ata_dev_dbg(dev,
2274 "failed to enable Sense Data Reporting, Emask 0x%x\n",
2275 err_mask);
2276 }
2277}
2278
Hannes Reinecke6d1003a2016-04-25 12:45:56 +02002279static void ata_dev_config_zac(struct ata_device *dev)
2280{
2281 struct ata_port *ap = dev->link->ap;
2282 unsigned int err_mask;
2283 u8 *identify_buf = ap->sector_buf;
Hannes Reinecke6d1003a2016-04-25 12:45:56 +02002284
2285 dev->zac_zones_optimal_open = U32_MAX;
2286 dev->zac_zones_optimal_nonseq = U32_MAX;
2287 dev->zac_zones_max_open = U32_MAX;
2288
2289 /*
2290 * Always set the 'ZAC' flag for Host-managed devices.
2291 */
2292 if (dev->class == ATA_DEV_ZAC)
2293 dev->flags |= ATA_DFLAG_ZAC;
2294 else if (ata_id_zoned_cap(dev->id) == 0x01)
2295 /*
2296 * Check for host-aware devices.
2297 */
2298 dev->flags |= ATA_DFLAG_ZAC;
2299
2300 if (!(dev->flags & ATA_DFLAG_ZAC))
2301 return;
2302
Christoph Hellwiga0fd2452017-06-04 14:42:23 +02002303 if (!ata_identify_page_supported(dev, ATA_LOG_ZONED_INFORMATION)) {
Hannes Reinecke6d1003a2016-04-25 12:45:56 +02002304 ata_dev_warn(dev,
2305 "ATA Zoned Information Log not supported\n");
2306 return;
2307 }
2308
2309 /*
2310 * Read IDENTIFY DEVICE data log, page 9 (Zoned-device information)
2311 */
Christoph Hellwig1d51d5f2017-06-04 14:42:22 +02002312 err_mask = ata_read_log_page(dev, ATA_LOG_IDENTIFY_DEVICE,
Hannes Reinecke6d1003a2016-04-25 12:45:56 +02002313 ATA_LOG_ZONED_INFORMATION,
2314 identify_buf, 1);
2315 if (!err_mask) {
2316 u64 zoned_cap, opt_open, opt_nonseq, max_open;
2317
2318 zoned_cap = get_unaligned_le64(&identify_buf[8]);
2319 if ((zoned_cap >> 63))
2320 dev->zac_zoned_cap = (zoned_cap & 1);
2321 opt_open = get_unaligned_le64(&identify_buf[24]);
2322 if ((opt_open >> 63))
2323 dev->zac_zones_optimal_open = (u32)opt_open;
2324 opt_nonseq = get_unaligned_le64(&identify_buf[32]);
2325 if ((opt_nonseq >> 63))
2326 dev->zac_zones_optimal_nonseq = (u32)opt_nonseq;
2327 max_open = get_unaligned_le64(&identify_buf[40]);
2328 if ((max_open >> 63))
2329 dev->zac_zones_max_open = (u32)max_open;
2330 }
2331}
2332
Christoph Hellwig818831c2017-06-04 14:42:24 +02002333static void ata_dev_config_trusted(struct ata_device *dev)
2334{
2335 struct ata_port *ap = dev->link->ap;
2336 u64 trusted_cap;
2337 unsigned int err;
2338
Christoph Hellwige8f11db2017-08-29 14:42:06 +02002339 if (!ata_id_has_trusted(dev->id))
2340 return;
2341
Christoph Hellwig818831c2017-06-04 14:42:24 +02002342 if (!ata_identify_page_supported(dev, ATA_LOG_SECURITY)) {
2343 ata_dev_warn(dev,
2344 "Security Log not supported\n");
2345 return;
2346 }
2347
2348 err = ata_read_log_page(dev, ATA_LOG_IDENTIFY_DEVICE, ATA_LOG_SECURITY,
2349 ap->sector_buf, 1);
2350 if (err) {
2351 ata_dev_dbg(dev,
2352 "failed to read Security Log, Emask 0x%x\n", err);
2353 return;
2354 }
2355
2356 trusted_cap = get_unaligned_le64(&ap->sector_buf[40]);
2357 if (!(trusted_cap & (1ULL << 63))) {
2358 ata_dev_dbg(dev,
2359 "Trusted Computing capability qword not valid!\n");
2360 return;
2361 }
2362
2363 if (trusted_cap & (1 << 0))
2364 dev->flags |= ATA_DFLAG_TRUSTED;
2365}
2366
Tejun Heo49016ac2006-02-21 02:12:11 +09002367/**
Tejun Heoffeae412006-03-01 16:09:35 +09002368 * ata_dev_configure - Configure the specified ATA/ATAPI device
Tejun Heoffeae412006-03-01 16:09:35 +09002369 * @dev: Target device to configure
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 *
Tejun Heoffeae412006-03-01 16:09:35 +09002371 * Configure @dev according to @dev->id. Generic and low-level
2372 * driver specific fixups are also applied.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 *
2374 * LOCKING:
Tejun Heoffeae412006-03-01 16:09:35 +09002375 * Kernel thread context (may sleep)
2376 *
2377 * RETURNS:
2378 * 0 on success, -errno otherwise
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379 */
Tejun Heoefdaedc2006-11-01 18:38:52 +09002380int ata_dev_configure(struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09002382 struct ata_port *ap = dev->link->ap;
2383 struct ata_eh_context *ehc = &dev->link->eh_context;
Tejun Heo67465442007-05-15 03:28:16 +09002384 int print_info = ehc->i.flags & ATA_EHI_PRINTINFO;
Tejun Heo1148c3a2006-03-13 19:48:04 +09002385 const u16 *id = dev->id;
Tejun Heo7dc951a2007-11-27 19:43:42 +09002386 unsigned long xfer_mask;
Shane Huang65fe1f02012-09-07 22:40:01 +08002387 unsigned int err_mask;
Alan Coxb352e572006-08-10 18:52:12 +01002388 char revbuf[7]; /* XYZ-99\0 */
Eric D. Mudama3f64f562007-01-30 23:00:40 -07002389 char fwrevbuf[ATA_ID_FW_REV_LEN+1];
2390 char modelbuf[ATA_ID_PROD_LEN+1];
Brian Kinge6d902a2006-06-28 08:30:31 -05002391 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392
Borislav Petkov0dd4b212006-06-23 02:29:08 -04002393 if (!ata_dev_enabled(dev) && ata_msg_info(ap)) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07002394 ata_dev_info(dev, "%s: ENTER/EXIT -- nodev\n", __func__);
Tejun Heoffeae412006-03-01 16:09:35 +09002395 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396 }
2397
Borislav Petkov0dd4b212006-06-23 02:29:08 -04002398 if (ata_msg_probe(ap))
Joe Perchesa9a79df2011-04-15 15:51:59 -07002399 ata_dev_dbg(dev, "%s: ENTER\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400
Tejun Heo75683fe2007-07-05 13:31:27 +09002401 /* set horkage */
2402 dev->horkage |= ata_dev_blacklisted(dev);
Tejun Heo33267322008-02-13 09:15:09 +09002403 ata_force_horkage(dev);
Tejun Heo75683fe2007-07-05 13:31:27 +09002404
Tejun Heo50af2fa2008-05-19 01:15:14 +09002405 if (dev->horkage & ATA_HORKAGE_DISABLE) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07002406 ata_dev_info(dev, "unsupported device, disabling\n");
Tejun Heo50af2fa2008-05-19 01:15:14 +09002407 ata_dev_disable(dev);
2408 return 0;
2409 }
2410
Tejun Heo2486fa52008-07-31 07:52:40 +09002411 if ((!atapi_enabled || (ap->flags & ATA_FLAG_NO_ATAPI)) &&
2412 dev->class == ATA_DEV_ATAPI) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07002413 ata_dev_warn(dev, "WARNING: ATAPI is %s, device ignored\n",
2414 atapi_enabled ? "not supported with this driver"
2415 : "disabled");
Tejun Heo2486fa52008-07-31 07:52:40 +09002416 ata_dev_disable(dev);
2417 return 0;
2418 }
2419
Tejun Heo90627122009-01-29 20:31:36 +09002420 rc = ata_do_link_spd_horkage(dev);
2421 if (rc)
2422 return rc;
2423
Tejun Heoecd75ad2014-01-16 09:47:17 -05002424 /* some WD SATA-1 drives have issues with LPM, turn on NOLPM for them */
2425 if ((dev->horkage & ATA_HORKAGE_WD_BROKEN_LPM) &&
2426 (id[ATA_ID_SATA_CAPABILITY] & 0xe) == 0x2)
2427 dev->horkage |= ATA_HORKAGE_NOLPM;
2428
Hans de Goede240630e2018-07-01 12:15:46 +02002429 if (ap->flags & ATA_FLAG_NO_LPM)
2430 dev->horkage |= ATA_HORKAGE_NOLPM;
2431
Tejun Heoecd75ad2014-01-16 09:47:17 -05002432 if (dev->horkage & ATA_HORKAGE_NOLPM) {
2433 ata_dev_warn(dev, "LPM support broken, forcing max_power\n");
2434 dev->link->ap->target_lpm_policy = ATA_LPM_MAX_POWER;
2435 }
2436
Tejun Heo67465442007-05-15 03:28:16 +09002437 /* let ACPI work its magic */
2438 rc = ata_acpi_on_devcfg(dev);
2439 if (rc)
2440 return rc;
Kristen Carlson Accardi08573a82006-11-10 16:14:47 -08002441
Tejun Heo05027ad2007-09-03 12:32:57 +09002442 /* massage HPA, do it early as it might change IDENTIFY data */
2443 rc = ata_hpa_resize(dev);
2444 if (rc)
2445 return rc;
2446
Tejun Heoc39f5eb2006-03-13 19:51:19 +09002447 /* print device capabilities */
Borislav Petkov0dd4b212006-06-23 02:29:08 -04002448 if (ata_msg_probe(ap))
Joe Perchesa9a79df2011-04-15 15:51:59 -07002449 ata_dev_dbg(dev,
2450 "%s: cfg 49:%04x 82:%04x 83:%04x 84:%04x "
2451 "85:%04x 86:%04x 87:%04x 88:%04x\n",
2452 __func__,
2453 id[49], id[82], id[83], id[84],
2454 id[85], id[86], id[87], id[88]);
Tejun Heoc39f5eb2006-03-13 19:51:19 +09002455
Tejun Heo208a9932006-03-05 17:55:58 +09002456 /* initialize to-be-configured parameters */
Tejun Heoea1dd4e2006-04-02 18:51:53 +09002457 dev->flags &= ~ATA_DFLAG_CFG_MASK;
Tejun Heo208a9932006-03-05 17:55:58 +09002458 dev->max_sectors = 0;
2459 dev->cdb_len = 0;
2460 dev->n_sectors = 0;
2461 dev->cylinders = 0;
2462 dev->heads = 0;
2463 dev->sectors = 0;
Mark Lorde18086d2009-03-19 13:32:21 -04002464 dev->multi_count = 0;
Tejun Heo208a9932006-03-05 17:55:58 +09002465
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466 /*
2467 * common ATA, ATAPI feature tests
2468 */
2469
Tejun Heoff8854b2006-03-06 04:31:56 +09002470 /* find max transfer mode; for printk only */
Tejun Heo1148c3a2006-03-13 19:48:04 +09002471 xfer_mask = ata_id_xfermask(id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472
Borislav Petkov0dd4b212006-06-23 02:29:08 -04002473 if (ata_msg_probe(ap))
2474 ata_dump_id(id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475
Albert Leeef143d52007-06-05 13:01:33 +08002476 /* SCSI only uses 4-char revisions, dump full 8 chars from ATA */
2477 ata_id_c_string(dev->id, fwrevbuf, ATA_ID_FW_REV,
2478 sizeof(fwrevbuf));
2479
2480 ata_id_c_string(dev->id, modelbuf, ATA_ID_PROD,
2481 sizeof(modelbuf));
2482
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483 /* ATA-specific feature tests */
Hannes Reinecke9162c652014-11-05 13:08:21 +01002484 if (dev->class == ATA_DEV_ATA || dev->class == ATA_DEV_ZAC) {
Alan Coxb352e572006-08-10 18:52:12 +01002485 if (ata_id_is_cfa(id)) {
Sergei Shtylyov62afe5d2009-04-13 20:50:00 +04002486 /* CPRM may make this media unusable */
2487 if (id[ATA_ID_CFA_KEY_MGMT] & 1)
Joe Perchesa9a79df2011-04-15 15:51:59 -07002488 ata_dev_warn(dev,
2489 "supports DRM functions and may not be fully accessible\n");
Alan Coxb352e572006-08-10 18:52:12 +01002490 snprintf(revbuf, 7, "CFA");
Alan Coxae8d4ee2007-11-04 22:05:49 -05002491 } else {
Jeff Garzik2dcb4072007-10-19 06:42:56 -04002492 snprintf(revbuf, 7, "ATA-%d", ata_id_major_version(id));
Alan Coxae8d4ee2007-11-04 22:05:49 -05002493 /* Warn the user if the device has TPM extensions */
2494 if (ata_id_has_tpm(id))
Joe Perchesa9a79df2011-04-15 15:51:59 -07002495 ata_dev_warn(dev,
2496 "supports DRM functions and may not be fully accessible\n");
Alan Coxae8d4ee2007-11-04 22:05:49 -05002497 }
Alan Coxb352e572006-08-10 18:52:12 +01002498
Tejun Heo1148c3a2006-03-13 19:48:04 +09002499 dev->n_sectors = ata_id_n_sectors(id);
Tejun Heo29407402006-02-12 22:47:04 +09002500
Mark Lorde18086d2009-03-19 13:32:21 -04002501 /* get current R/W Multiple count setting */
2502 if ((dev->id[47] >> 8) == 0x80 && (dev->id[59] & 0x100)) {
2503 unsigned int max = dev->id[47] & 0xff;
2504 unsigned int cnt = dev->id[59] & 0xff;
2505 /* only recognize/allow powers of two here */
2506 if (is_power_of_2(max) && is_power_of_2(cnt))
2507 if (cnt <= max)
2508 dev->multi_count = cnt;
2509 }
Eric D. Mudama3f64f562007-01-30 23:00:40 -07002510
Tejun Heo1148c3a2006-03-13 19:48:04 +09002511 if (ata_id_has_lba(id)) {
Tejun Heo4c2d7212006-03-05 17:55:58 +09002512 const char *lba_desc;
Shaohua Li388539f2009-07-27 09:24:35 +08002513 char ncq_desc[24];
Albert Lee8bf62ece2005-05-12 15:29:42 -04002514
Tejun Heo4c2d7212006-03-05 17:55:58 +09002515 lba_desc = "LBA";
2516 dev->flags |= ATA_DFLAG_LBA;
Tejun Heo1148c3a2006-03-13 19:48:04 +09002517 if (ata_id_has_lba48(id)) {
Albert Lee8bf62ece2005-05-12 15:29:42 -04002518 dev->flags |= ATA_DFLAG_LBA48;
Tejun Heo4c2d7212006-03-05 17:55:58 +09002519 lba_desc = "LBA48";
Tejun Heo6fc49ad2006-11-11 20:10:45 +09002520
2521 if (dev->n_sectors >= (1UL << 28) &&
2522 ata_id_has_flush_ext(id))
2523 dev->flags |= ATA_DFLAG_FLUSH_EXT;
Tejun Heo4c2d7212006-03-05 17:55:58 +09002524 }
Albert Lee8bf62ece2005-05-12 15:29:42 -04002525
Tejun Heoa6e6ce82006-05-15 21:03:48 +09002526 /* config NCQ */
Shaohua Li388539f2009-07-27 09:24:35 +08002527 rc = ata_dev_config_ncq(dev, ncq_desc, sizeof(ncq_desc));
2528 if (rc)
2529 return rc;
Tejun Heoa6e6ce82006-05-15 21:03:48 +09002530
Albert Lee8bf62ece2005-05-12 15:29:42 -04002531 /* print device info to dmesg */
Eric D. Mudama3f64f562007-01-30 23:00:40 -07002532 if (ata_msg_drv(ap) && print_info) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07002533 ata_dev_info(dev, "%s: %s, %s, max %s\n",
2534 revbuf, modelbuf, fwrevbuf,
2535 ata_mode_string(xfer_mask));
2536 ata_dev_info(dev,
2537 "%llu sectors, multi %u: %s %s\n",
Tejun Heof15a1da2006-05-15 20:57:56 +09002538 (unsigned long long)dev->n_sectors,
Eric D. Mudama3f64f562007-01-30 23:00:40 -07002539 dev->multi_count, lba_desc, ncq_desc);
2540 }
Tejun Heoffeae412006-03-01 16:09:35 +09002541 } else {
Albert Lee8bf62ece2005-05-12 15:29:42 -04002542 /* CHS */
2543
2544 /* Default translation */
Tejun Heo1148c3a2006-03-13 19:48:04 +09002545 dev->cylinders = id[1];
2546 dev->heads = id[3];
2547 dev->sectors = id[6];
Albert Lee8bf62ece2005-05-12 15:29:42 -04002548
Tejun Heo1148c3a2006-03-13 19:48:04 +09002549 if (ata_id_current_chs_valid(id)) {
Albert Lee8bf62ece2005-05-12 15:29:42 -04002550 /* Current CHS translation is valid. */
Tejun Heo1148c3a2006-03-13 19:48:04 +09002551 dev->cylinders = id[54];
2552 dev->heads = id[55];
2553 dev->sectors = id[56];
Albert Lee8bf62ece2005-05-12 15:29:42 -04002554 }
2555
2556 /* print device info to dmesg */
Eric D. Mudama3f64f562007-01-30 23:00:40 -07002557 if (ata_msg_drv(ap) && print_info) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07002558 ata_dev_info(dev, "%s: %s, %s, max %s\n",
2559 revbuf, modelbuf, fwrevbuf,
2560 ata_mode_string(xfer_mask));
2561 ata_dev_info(dev,
2562 "%llu sectors, multi %u, CHS %u/%u/%u\n",
2563 (unsigned long long)dev->n_sectors,
2564 dev->multi_count, dev->cylinders,
2565 dev->heads, dev->sectors);
Eric D. Mudama3f64f562007-01-30 23:00:40 -07002566 }
Albert Lee07f6f7d2005-11-01 19:33:20 +08002567 }
2568
Shane Huang803739d2012-12-17 23:18:59 +08002569 /* Check and mark DevSlp capability. Get DevSlp timing variables
2570 * from SATA Settings page of Identify Device Data Log.
Shane Huang65fe1f02012-09-07 22:40:01 +08002571 */
Shane Huang803739d2012-12-17 23:18:59 +08002572 if (ata_id_has_devslp(dev->id)) {
David Woodhouse8e725c72013-03-29 11:54:55 +00002573 u8 *sata_setting = ap->sector_buf;
Shane Huang803739d2012-12-17 23:18:59 +08002574 int i, j;
2575
2576 dev->flags |= ATA_DFLAG_DEVSLP;
Shane Huang65fe1f02012-09-07 22:40:01 +08002577 err_mask = ata_read_log_page(dev,
Christoph Hellwig1d51d5f2017-06-04 14:42:22 +02002578 ATA_LOG_IDENTIFY_DEVICE,
Shane Huang65fe1f02012-09-07 22:40:01 +08002579 ATA_LOG_SATA_SETTINGS,
Shane Huang803739d2012-12-17 23:18:59 +08002580 sata_setting,
Shane Huang65fe1f02012-09-07 22:40:01 +08002581 1);
2582 if (err_mask)
2583 ata_dev_dbg(dev,
2584 "failed to get Identify Device Data, Emask 0x%x\n",
2585 err_mask);
Shane Huang803739d2012-12-17 23:18:59 +08002586 else
2587 for (i = 0; i < ATA_LOG_DEVSLP_SIZE; i++) {
2588 j = ATA_LOG_DEVSLP_OFFSET + i;
2589 dev->devslp_timing[i] = sata_setting[j];
2590 }
Shane Huang65fe1f02012-09-07 22:40:01 +08002591 }
Hannes Reineckee87fd282016-04-04 11:43:55 +02002592 ata_dev_config_sense_reporting(dev);
Hannes Reinecke6d1003a2016-04-25 12:45:56 +02002593 ata_dev_config_zac(dev);
Christoph Hellwig818831c2017-06-04 14:42:24 +02002594 ata_dev_config_trusted(dev);
Minwoo Imb1ffbf82017-06-24 03:41:10 +09002595 dev->cdb_len = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596 }
2597
2598 /* ATAPI-specific feature tests */
Jeff Garzik2c13b7c2005-11-14 14:14:16 -05002599 else if (dev->class == ATA_DEV_ATAPI) {
Tejun Heo854c73a2007-09-23 13:14:11 +09002600 const char *cdb_intr_string = "";
2601 const char *atapi_an_string = "";
Tejun Heo91163002008-02-21 13:25:50 +09002602 const char *dma_dir_string = "";
Tejun Heo7d77b242007-09-23 13:14:13 +09002603 u32 sntf;
Albert Lee08a556d2006-03-31 13:29:04 +08002604
Tejun Heo1148c3a2006-03-13 19:48:04 +09002605 rc = atapi_cdb_len(id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 if ((rc < 12) || (rc > ATAPI_CDB_LEN)) {
Borislav Petkov0dd4b212006-06-23 02:29:08 -04002607 if (ata_msg_warn(ap))
Joe Perchesa9a79df2011-04-15 15:51:59 -07002608 ata_dev_warn(dev, "unsupported CDB len\n");
Tejun Heoffeae412006-03-01 16:09:35 +09002609 rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610 goto err_out_nosup;
2611 }
Tejun Heo6e7846e2006-02-12 23:32:58 +09002612 dev->cdb_len = (unsigned int) rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613
Tejun Heo7d77b242007-09-23 13:14:13 +09002614 /* Enable ATAPI AN if both the host and device have
2615 * the support. If PMP is attached, SNTF is required
2616 * to enable ATAPI AN to discern between PHY status
2617 * changed notifications and ATAPI ANs.
Kristen Carlson Accardi9f45cbd2007-08-15 03:57:11 -04002618 */
Tejun Heoe7ecd432010-05-19 15:38:58 +02002619 if (atapi_an &&
2620 (ap->flags & ATA_FLAG_AN) && ata_id_has_atapi_AN(id) &&
Tejun Heo071f44b2008-04-07 22:47:22 +09002621 (!sata_pmp_attached(ap) ||
Tejun Heo7d77b242007-09-23 13:14:13 +09002622 sata_scr_read(&ap->link, SCR_NOTIFICATION, &sntf) == 0)) {
Kristen Carlson Accardi9f45cbd2007-08-15 03:57:11 -04002623 /* issue SET feature command to turn this on */
Jeff Garzik218f3d32007-10-25 00:33:27 -04002624 err_mask = ata_dev_set_feature(dev,
2625 SETFEATURES_SATA_ENABLE, SATA_AN);
Tejun Heo854c73a2007-09-23 13:14:11 +09002626 if (err_mask)
Joe Perchesa9a79df2011-04-15 15:51:59 -07002627 ata_dev_err(dev,
2628 "failed to enable ATAPI AN (err_mask=0x%x)\n",
2629 err_mask);
Tejun Heo854c73a2007-09-23 13:14:11 +09002630 else {
Kristen Carlson Accardi9f45cbd2007-08-15 03:57:11 -04002631 dev->flags |= ATA_DFLAG_AN;
Tejun Heo854c73a2007-09-23 13:14:11 +09002632 atapi_an_string = ", ATAPI AN";
2633 }
Kristen Carlson Accardi9f45cbd2007-08-15 03:57:11 -04002634 }
2635
Albert Lee08a556d2006-03-31 13:29:04 +08002636 if (ata_id_cdb_intr(dev->id)) {
Albert Lee312f7da2005-09-27 17:38:03 +08002637 dev->flags |= ATA_DFLAG_CDB_INTR;
Albert Lee08a556d2006-03-31 13:29:04 +08002638 cdb_intr_string = ", CDB intr";
2639 }
Albert Lee312f7da2005-09-27 17:38:03 +08002640
Vincent Pelletier966fbe12013-05-21 22:30:58 +02002641 if (atapi_dmadir || (dev->horkage & ATA_HORKAGE_ATAPI_DMADIR) || atapi_id_dmadir(dev->id)) {
Tejun Heo91163002008-02-21 13:25:50 +09002642 dev->flags |= ATA_DFLAG_DMADIR;
2643 dma_dir_string = ", DMADIR";
2644 }
2645
Aaron Luafe75952013-01-15 17:20:58 +08002646 if (ata_id_has_da(dev->id)) {
Lin Mingb1354cb2012-06-25 16:13:08 +08002647 dev->flags |= ATA_DFLAG_DA;
Aaron Luafe75952013-01-15 17:20:58 +08002648 zpodd_init(dev);
2649 }
Lin Mingb1354cb2012-06-25 16:13:08 +08002650
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651 /* print device info to dmesg */
Borislav Petkov5afc8142006-06-27 14:51:25 +02002652 if (ata_msg_drv(ap) && print_info)
Joe Perchesa9a79df2011-04-15 15:51:59 -07002653 ata_dev_info(dev,
2654 "ATAPI: %s, %s, max %s%s%s%s\n",
2655 modelbuf, fwrevbuf,
2656 ata_mode_string(xfer_mask),
2657 cdb_intr_string, atapi_an_string,
2658 dma_dir_string);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659 }
2660
Tejun Heo914ed352006-11-01 18:39:55 +09002661 /* determine max_sectors */
2662 dev->max_sectors = ATA_MAX_SECTORS;
2663 if (dev->flags & ATA_DFLAG_LBA48)
2664 dev->max_sectors = ATA_MAX_SECTORS_LBA48;
2665
Alan Coxc5038fc2007-10-25 14:21:16 +01002666 /* Limit PATA drive on SATA cable bridge transfers to udma5,
2667 200 sectors */
Tejun Heo3373efd2006-05-15 20:57:53 +09002668 if (ata_dev_knobble(dev)) {
Borislav Petkov5afc8142006-06-27 14:51:25 +02002669 if (ata_msg_drv(ap) && print_info)
Joe Perchesa9a79df2011-04-15 15:51:59 -07002670 ata_dev_info(dev, "applying bridge limits\n");
Tejun Heo5a529132006-03-24 14:07:50 +09002671 dev->udma_mask &= ATA_UDMA5;
Tejun Heo4b2f3ed2006-03-01 16:09:36 +09002672 dev->max_sectors = ATA_MAX_SECTORS;
2673 }
2674
Tony Battersbyf8d8e572007-10-30 11:44:35 -04002675 if ((dev->class == ATA_DEV_ATAPI) &&
Albert Leef442cd82007-11-15 10:35:47 +09002676 (atapi_command_packet_set(id) == TYPE_TAPE)) {
Tony Battersbyf8d8e572007-10-30 11:44:35 -04002677 dev->max_sectors = ATA_MAX_SECTORS_TAPE;
Albert Leef442cd82007-11-15 10:35:47 +09002678 dev->horkage |= ATA_HORKAGE_STUCK_ERR;
2679 }
Tony Battersbyf8d8e572007-10-30 11:44:35 -04002680
Tejun Heo75683fe2007-07-05 13:31:27 +09002681 if (dev->horkage & ATA_HORKAGE_MAX_SEC_128)
Tejun Heo03ec52d2007-04-12 13:38:11 +09002682 dev->max_sectors = min_t(unsigned int, ATA_MAX_SECTORS_128,
2683 dev->max_sectors);
Albert Lee18d6e9d2007-04-02 11:34:15 +08002684
David Milburnaf34d632015-07-13 11:48:23 -05002685 if (dev->horkage & ATA_HORKAGE_MAX_SEC_1024)
2686 dev->max_sectors = min_t(unsigned int, ATA_MAX_SECTORS_1024,
2687 dev->max_sectors);
2688
Shan Haia32450e2013-03-18 10:30:44 +08002689 if (dev->horkage & ATA_HORKAGE_MAX_SEC_LBA48)
2690 dev->max_sectors = ATA_MAX_SECTORS_LBA48;
2691
Tejun Heo4b2f3ed2006-03-01 16:09:36 +09002692 if (ap->ops->dev_config)
Alancd0d3bb2007-03-02 00:56:15 +00002693 ap->ops->dev_config(dev);
Tejun Heo4b2f3ed2006-03-01 16:09:36 +09002694
Alan Coxc5038fc2007-10-25 14:21:16 +01002695 if (dev->horkage & ATA_HORKAGE_DIAGNOSTIC) {
2696 /* Let the user know. We don't want to disallow opens for
2697 rescue purposes, or in case the vendor is just a blithering
2698 idiot. Do this after the dev_config call as some controllers
2699 with buggy firmware may want to avoid reporting false device
2700 bugs */
2701
2702 if (print_info) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07002703 ata_dev_warn(dev,
Alan Coxc5038fc2007-10-25 14:21:16 +01002704"Drive reports diagnostics failure. This may indicate a drive\n");
Joe Perchesa9a79df2011-04-15 15:51:59 -07002705 ata_dev_warn(dev,
Alan Coxc5038fc2007-10-25 14:21:16 +01002706"fault or invalid emulation. Contact drive vendor for information.\n");
2707 }
2708 }
2709
Tejun Heoac70a962008-11-27 13:36:48 +09002710 if ((dev->horkage & ATA_HORKAGE_FIRMWARE_WARN) && print_info) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07002711 ata_dev_warn(dev, "WARNING: device requires firmware update to be fully functional\n");
2712 ata_dev_warn(dev, " contact the vendor or visit http://ata.wiki.kernel.org\n");
Tejun Heoac70a962008-11-27 13:36:48 +09002713 }
2714
Tejun Heoffeae412006-03-01 16:09:35 +09002715 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716
2717err_out_nosup:
Borislav Petkov0dd4b212006-06-23 02:29:08 -04002718 if (ata_msg_probe(ap))
Joe Perchesa9a79df2011-04-15 15:51:59 -07002719 ata_dev_dbg(dev, "%s: EXIT, err\n", __func__);
Tejun Heoffeae412006-03-01 16:09:35 +09002720 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721}
2722
2723/**
Alan Cox2e41e8e2007-03-08 23:19:19 +00002724 * ata_cable_40wire - return 40 wire cable type
Alan Coxbe0d18d2007-03-06 02:37:56 -08002725 * @ap: port
2726 *
Alan Cox2e41e8e2007-03-08 23:19:19 +00002727 * Helper method for drivers which want to hardwire 40 wire cable
Alan Coxbe0d18d2007-03-06 02:37:56 -08002728 * detection.
2729 */
2730
2731int ata_cable_40wire(struct ata_port *ap)
2732{
2733 return ATA_CBL_PATA40;
2734}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01002735EXPORT_SYMBOL_GPL(ata_cable_40wire);
Alan Coxbe0d18d2007-03-06 02:37:56 -08002736
2737/**
Alan Cox2e41e8e2007-03-08 23:19:19 +00002738 * ata_cable_80wire - return 80 wire cable type
Alan Coxbe0d18d2007-03-06 02:37:56 -08002739 * @ap: port
2740 *
Alan Cox2e41e8e2007-03-08 23:19:19 +00002741 * Helper method for drivers which want to hardwire 80 wire cable
Alan Coxbe0d18d2007-03-06 02:37:56 -08002742 * detection.
2743 */
2744
2745int ata_cable_80wire(struct ata_port *ap)
2746{
2747 return ATA_CBL_PATA80;
2748}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01002749EXPORT_SYMBOL_GPL(ata_cable_80wire);
Alan Coxbe0d18d2007-03-06 02:37:56 -08002750
2751/**
2752 * ata_cable_unknown - return unknown PATA cable.
2753 * @ap: port
2754 *
2755 * Helper method for drivers which have no PATA cable detection.
2756 */
2757
2758int ata_cable_unknown(struct ata_port *ap)
2759{
2760 return ATA_CBL_PATA_UNK;
2761}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01002762EXPORT_SYMBOL_GPL(ata_cable_unknown);
Alan Coxbe0d18d2007-03-06 02:37:56 -08002763
2764/**
Tejun Heoc88f90c2007-11-27 19:43:48 +09002765 * ata_cable_ignore - return ignored PATA cable.
2766 * @ap: port
2767 *
2768 * Helper method for drivers which don't use cable type to limit
2769 * transfer mode.
2770 */
2771int ata_cable_ignore(struct ata_port *ap)
2772{
2773 return ATA_CBL_PATA_IGN;
2774}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01002775EXPORT_SYMBOL_GPL(ata_cable_ignore);
Tejun Heoc88f90c2007-11-27 19:43:48 +09002776
2777/**
Alan Coxbe0d18d2007-03-06 02:37:56 -08002778 * ata_cable_sata - return SATA cable type
2779 * @ap: port
2780 *
2781 * Helper method for drivers which have SATA cables
2782 */
2783
2784int ata_cable_sata(struct ata_port *ap)
2785{
2786 return ATA_CBL_SATA;
2787}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01002788EXPORT_SYMBOL_GPL(ata_cable_sata);
Alan Coxbe0d18d2007-03-06 02:37:56 -08002789
2790/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791 * ata_bus_probe - Reset and probe ATA bus
2792 * @ap: Bus to probe
2793 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04002794 * Master ATA bus probing function. Initiates a hardware-dependent
2795 * bus reset, then attempts to identify any devices found on
2796 * the bus.
2797 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002799 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800 *
2801 * RETURNS:
Tejun Heo96072e62006-04-01 01:38:17 +09002802 * Zero on success, negative errno otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803 */
2804
Brian King80289162006-08-07 14:27:31 -05002805int ata_bus_probe(struct ata_port *ap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806{
Tejun Heo28ca5c52006-03-01 16:09:36 +09002807 unsigned int classes[ATA_MAX_DEVICES];
Tejun Heo14d2bac2006-04-02 17:54:46 +09002808 int tries[ATA_MAX_DEVICES];
Tejun Heof58229f2007-08-06 18:36:23 +09002809 int rc;
Tejun Heoe82cbdb2006-04-01 01:38:18 +09002810 struct ata_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811
Tejun Heo1eca4362008-11-03 20:03:17 +09002812 ata_for_each_dev(dev, &ap->link, ALL)
Tejun Heof58229f2007-08-06 18:36:23 +09002813 tries[dev->devno] = ATA_PROBE_MAX_TRIES;
Tejun Heo14d2bac2006-04-02 17:54:46 +09002814
2815 retry:
Tejun Heo1eca4362008-11-03 20:03:17 +09002816 ata_for_each_dev(dev, &ap->link, ALL) {
Tejun Heocdeab112007-10-29 16:41:09 +09002817 /* If we issue an SRST then an ATA drive (not ATAPI)
2818 * may change configuration and be in PIO0 timing. If
2819 * we do a hard reset (or are coming from power on)
2820 * this is true for ATA or ATAPI. Until we've set a
2821 * suitable controller mode we should not touch the
2822 * bus as we may be talking too fast.
2823 */
2824 dev->pio_mode = XFER_PIO_0;
Aaron Lu54169122012-12-03 11:35:02 +08002825 dev->dma_mode = 0xff;
Tejun Heocdeab112007-10-29 16:41:09 +09002826
2827 /* If the controller has a pio mode setup function
2828 * then use it to set the chipset to rights. Don't
2829 * touch the DMA setup as that will be dealt with when
2830 * configuring devices.
2831 */
2832 if (ap->ops->set_piomode)
2833 ap->ops->set_piomode(ap, dev);
2834 }
2835
Tejun Heo20444702006-03-13 01:57:01 +09002836 /* reset and determine device classes */
Tejun Heo52783c52006-05-31 18:28:22 +09002837 ap->ops->phy_reset(ap);
Tejun Heo2061a472006-03-12 00:57:39 +09002838
Tejun Heo1eca4362008-11-03 20:03:17 +09002839 ata_for_each_dev(dev, &ap->link, ALL) {
Tejun Heo3e4ec342010-05-10 21:41:30 +02002840 if (dev->class != ATA_DEV_UNKNOWN)
Tejun Heo52783c52006-05-31 18:28:22 +09002841 classes[dev->devno] = dev->class;
2842 else
2843 classes[dev->devno] = ATA_DEV_NONE;
Tejun Heo20444702006-03-13 01:57:01 +09002844
Tejun Heo52783c52006-05-31 18:28:22 +09002845 dev->class = ATA_DEV_UNKNOWN;
Tejun Heo28ca5c52006-03-01 16:09:36 +09002846 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847
Jeff Garzikf31f0cc2007-03-02 17:47:28 -05002848 /* read IDENTIFY page and configure devices. We have to do the identify
2849 specific sequence bass-ackwards so that PDIAG- is released by
2850 the slave device */
2851
Tejun Heo1eca4362008-11-03 20:03:17 +09002852 ata_for_each_dev(dev, &ap->link, ALL_REVERSE) {
Tejun Heof58229f2007-08-06 18:36:23 +09002853 if (tries[dev->devno])
2854 dev->class = classes[dev->devno];
Tejun Heo14d2bac2006-04-02 17:54:46 +09002855
Tejun Heoe1211e32006-04-01 01:38:18 +09002856 if (!ata_dev_enabled(dev))
Tejun Heoffeae412006-03-01 16:09:35 +09002857 continue;
2858
Tejun Heobff04642006-11-10 18:08:10 +09002859 rc = ata_dev_read_id(dev, &dev->class, ATA_READID_POSTRESET,
2860 dev->id);
Tejun Heo14d2bac2006-04-02 17:54:46 +09002861 if (rc)
2862 goto fail;
Jeff Garzikf31f0cc2007-03-02 17:47:28 -05002863 }
2864
Alan Coxbe0d18d2007-03-06 02:37:56 -08002865 /* Now ask for the cable type as PDIAG- should have been released */
2866 if (ap->ops->cable_detect)
2867 ap->cbl = ap->ops->cable_detect(ap);
2868
Tejun Heo1eca4362008-11-03 20:03:17 +09002869 /* We may have SATA bridge glue hiding here irrespective of
2870 * the reported cable types and sensed types. When SATA
2871 * drives indicate we have a bridge, we don't know which end
2872 * of the link the bridge is which is a problem.
2873 */
2874 ata_for_each_dev(dev, &ap->link, ENABLED)
Alan Cox614fe292007-08-22 23:22:45 +01002875 if (ata_id_is_sata(dev->id))
2876 ap->cbl = ATA_CBL_SATA;
Alan Cox614fe292007-08-22 23:22:45 +01002877
Jeff Garzikf31f0cc2007-03-02 17:47:28 -05002878 /* After the identify sequence we can now set up the devices. We do
2879 this in the normal order so that the user doesn't get confused */
2880
Tejun Heo1eca4362008-11-03 20:03:17 +09002881 ata_for_each_dev(dev, &ap->link, ENABLED) {
Tejun Heo9af5c9c2007-08-06 18:36:22 +09002882 ap->link.eh_context.i.flags |= ATA_EHI_PRINTINFO;
Tejun Heoefdaedc2006-11-01 18:38:52 +09002883 rc = ata_dev_configure(dev);
Tejun Heo9af5c9c2007-08-06 18:36:22 +09002884 ap->link.eh_context.i.flags &= ~ATA_EHI_PRINTINFO;
Tejun Heo14d2bac2006-04-02 17:54:46 +09002885 if (rc)
2886 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887 }
2888
Tejun Heoe82cbdb2006-04-01 01:38:18 +09002889 /* configure transfer mode */
Tejun Heo02607312007-08-06 18:36:23 +09002890 rc = ata_set_mode(&ap->link, &dev);
Tejun Heo4ae72a12007-02-02 16:22:30 +09002891 if (rc)
Tejun Heo51713d32006-04-11 22:26:29 +09002892 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893
Tejun Heo1eca4362008-11-03 20:03:17 +09002894 ata_for_each_dev(dev, &ap->link, ENABLED)
2895 return 0;
Alan Coxe35a9e02006-03-27 18:46:37 +01002896
Tejun Heo96072e62006-04-01 01:38:17 +09002897 return -ENODEV;
Tejun Heo14d2bac2006-04-02 17:54:46 +09002898
2899 fail:
Tejun Heo4ae72a12007-02-02 16:22:30 +09002900 tries[dev->devno]--;
2901
Tejun Heo14d2bac2006-04-02 17:54:46 +09002902 switch (rc) {
2903 case -EINVAL:
Tejun Heo4ae72a12007-02-02 16:22:30 +09002904 /* eeek, something went very wrong, give up */
Tejun Heo14d2bac2006-04-02 17:54:46 +09002905 tries[dev->devno] = 0;
2906 break;
Tejun Heo4ae72a12007-02-02 16:22:30 +09002907
2908 case -ENODEV:
2909 /* give it just one more chance */
2910 tries[dev->devno] = min(tries[dev->devno], 1);
Gustavo A. R. Silva05b83602017-10-12 14:19:16 -05002911 /* fall through */
Tejun Heo14d2bac2006-04-02 17:54:46 +09002912 case -EIO:
Tejun Heo4ae72a12007-02-02 16:22:30 +09002913 if (tries[dev->devno] == 1) {
2914 /* This is the last chance, better to slow
2915 * down than lose it.
2916 */
Tejun Heoa07d4992009-01-29 20:31:33 +09002917 sata_down_spd_limit(&ap->link, 0);
Tejun Heo4ae72a12007-02-02 16:22:30 +09002918 ata_down_xfermask_limit(dev, ATA_DNXFER_PIO);
2919 }
Tejun Heo14d2bac2006-04-02 17:54:46 +09002920 }
2921
Tejun Heo4ae72a12007-02-02 16:22:30 +09002922 if (!tries[dev->devno])
Tejun Heo3373efd2006-05-15 20:57:53 +09002923 ata_dev_disable(dev);
Tejun Heoec573752006-04-11 22:26:29 +09002924
Tejun Heo14d2bac2006-04-02 17:54:46 +09002925 goto retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926}
2927
2928/**
Tejun Heo3be680b2005-12-19 22:35:02 +09002929 * sata_print_link_status - Print SATA link status
Tejun Heo936fd732007-08-06 18:36:23 +09002930 * @link: SATA link to printk link status about
Tejun Heo3be680b2005-12-19 22:35:02 +09002931 *
2932 * This function prints link speed and status of a SATA link.
2933 *
2934 * LOCKING:
2935 * None.
2936 */
Adrian Bunk6bdb4fc2008-04-21 11:51:11 +03002937static void sata_print_link_status(struct ata_link *link)
Tejun Heo3be680b2005-12-19 22:35:02 +09002938{
Tejun Heo6d5f9732006-04-03 00:09:41 +09002939 u32 sstatus, scontrol, tmp;
Tejun Heo3be680b2005-12-19 22:35:02 +09002940
Tejun Heo936fd732007-08-06 18:36:23 +09002941 if (sata_scr_read(link, SCR_STATUS, &sstatus))
Tejun Heo3be680b2005-12-19 22:35:02 +09002942 return;
Tejun Heo936fd732007-08-06 18:36:23 +09002943 sata_scr_read(link, SCR_CONTROL, &scontrol);
Tejun Heo3be680b2005-12-19 22:35:02 +09002944
Tejun Heob1c72912008-07-31 17:02:43 +09002945 if (ata_phys_link_online(link)) {
Tejun Heo3be680b2005-12-19 22:35:02 +09002946 tmp = (sstatus >> 4) & 0xf;
Joe Perchesa9a79df2011-04-15 15:51:59 -07002947 ata_link_info(link, "SATA link up %s (SStatus %X SControl %X)\n",
2948 sata_spd_string(tmp), sstatus, scontrol);
Tejun Heo3be680b2005-12-19 22:35:02 +09002949 } else {
Joe Perchesa9a79df2011-04-15 15:51:59 -07002950 ata_link_info(link, "SATA link down (SStatus %X SControl %X)\n",
2951 sstatus, scontrol);
Tejun Heo3be680b2005-12-19 22:35:02 +09002952 }
2953}
2954
2955/**
Alan Coxebdfca6e2006-03-23 15:38:34 +00002956 * ata_dev_pair - return other device on cable
Alan Coxebdfca6e2006-03-23 15:38:34 +00002957 * @adev: device
2958 *
2959 * Obtain the other device on the same cable, or if none is
2960 * present NULL is returned
2961 */
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05002962
Tejun Heo3373efd2006-05-15 20:57:53 +09002963struct ata_device *ata_dev_pair(struct ata_device *adev)
Alan Coxebdfca6e2006-03-23 15:38:34 +00002964{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09002965 struct ata_link *link = adev->link;
2966 struct ata_device *pair = &link->device[1 - adev->devno];
Tejun Heoe1211e32006-04-01 01:38:18 +09002967 if (!ata_dev_enabled(pair))
Alan Coxebdfca6e2006-03-23 15:38:34 +00002968 return NULL;
2969 return pair;
2970}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01002971EXPORT_SYMBOL_GPL(ata_dev_pair);
Alan Coxebdfca6e2006-03-23 15:38:34 +00002972
2973/**
Tejun Heo3c567b72006-05-15 20:57:23 +09002974 * sata_down_spd_limit - adjust SATA spd limit downward
Tejun Heo936fd732007-08-06 18:36:23 +09002975 * @link: Link to adjust SATA spd limit for
Tejun Heoa07d4992009-01-29 20:31:33 +09002976 * @spd_limit: Additional limit
Tejun Heo1c3fae42006-04-02 20:53:28 +09002977 *
Tejun Heo936fd732007-08-06 18:36:23 +09002978 * Adjust SATA spd limit of @link downward. Note that this
Tejun Heo1c3fae42006-04-02 20:53:28 +09002979 * function only adjusts the limit. The change must be applied
Tejun Heo3c567b72006-05-15 20:57:23 +09002980 * using sata_set_spd().
Tejun Heo1c3fae42006-04-02 20:53:28 +09002981 *
Tejun Heoa07d4992009-01-29 20:31:33 +09002982 * If @spd_limit is non-zero, the speed is limited to equal to or
2983 * lower than @spd_limit if such speed is supported. If
2984 * @spd_limit is slower than any supported speed, only the lowest
2985 * supported speed is allowed.
2986 *
Tejun Heo1c3fae42006-04-02 20:53:28 +09002987 * LOCKING:
2988 * Inherited from caller.
2989 *
2990 * RETURNS:
2991 * 0 on success, negative errno on failure
2992 */
Tejun Heoa07d4992009-01-29 20:31:33 +09002993int sata_down_spd_limit(struct ata_link *link, u32 spd_limit)
Tejun Heo1c3fae42006-04-02 20:53:28 +09002994{
Tejun Heo81952c52006-05-15 20:57:47 +09002995 u32 sstatus, spd, mask;
Tejun Heoa07d4992009-01-29 20:31:33 +09002996 int rc, bit;
Tejun Heo1c3fae42006-04-02 20:53:28 +09002997
Tejun Heo936fd732007-08-06 18:36:23 +09002998 if (!sata_scr_valid(link))
Tejun Heo008a7892007-07-16 14:29:40 +09002999 return -EOPNOTSUPP;
3000
3001 /* If SCR can be read, use it to determine the current SPD.
Tejun Heo936fd732007-08-06 18:36:23 +09003002 * If not, use cached value in link->sata_spd.
Tejun Heo008a7892007-07-16 14:29:40 +09003003 */
Tejun Heo936fd732007-08-06 18:36:23 +09003004 rc = sata_scr_read(link, SCR_STATUS, &sstatus);
Tejun Heo9913ff8a2009-01-29 20:31:31 +09003005 if (rc == 0 && ata_sstatus_online(sstatus))
Tejun Heo008a7892007-07-16 14:29:40 +09003006 spd = (sstatus >> 4) & 0xf;
3007 else
Tejun Heo936fd732007-08-06 18:36:23 +09003008 spd = link->sata_spd;
Tejun Heo1c3fae42006-04-02 20:53:28 +09003009
Tejun Heo936fd732007-08-06 18:36:23 +09003010 mask = link->sata_spd_limit;
Tejun Heo1c3fae42006-04-02 20:53:28 +09003011 if (mask <= 1)
3012 return -EINVAL;
Tejun Heo008a7892007-07-16 14:29:40 +09003013
3014 /* unconditionally mask off the highest bit */
Tejun Heoa07d4992009-01-29 20:31:33 +09003015 bit = fls(mask) - 1;
3016 mask &= ~(1 << bit);
Tejun Heo1c3fae42006-04-02 20:53:28 +09003017
David Milburn2dc0b462017-11-14 16:17:25 -06003018 /*
3019 * Mask off all speeds higher than or equal to the current one. At
3020 * this point, if current SPD is not available and we previously
3021 * recorded the link speed from SStatus, the driver has already
3022 * masked off the highest bit so mask should already be 1 or 0.
3023 * Otherwise, we should not force 1.5Gbps on a link where we have
3024 * not previously recorded speed from SStatus. Just return in this
3025 * case.
Tejun Heo008a7892007-07-16 14:29:40 +09003026 */
3027 if (spd > 1)
3028 mask &= (1 << (spd - 1)) - 1;
3029 else
David Milburn2dc0b462017-11-14 16:17:25 -06003030 return -EINVAL;
Tejun Heo008a7892007-07-16 14:29:40 +09003031
3032 /* were we already at the bottom? */
Tejun Heo1c3fae42006-04-02 20:53:28 +09003033 if (!mask)
3034 return -EINVAL;
3035
Tejun Heoa07d4992009-01-29 20:31:33 +09003036 if (spd_limit) {
3037 if (mask & ((1 << spd_limit) - 1))
3038 mask &= (1 << spd_limit) - 1;
3039 else {
3040 bit = ffs(mask) - 1;
3041 mask = 1 << bit;
3042 }
3043 }
3044
Tejun Heo936fd732007-08-06 18:36:23 +09003045 link->sata_spd_limit = mask;
Tejun Heo1c3fae42006-04-02 20:53:28 +09003046
Joe Perchesa9a79df2011-04-15 15:51:59 -07003047 ata_link_warn(link, "limiting SATA link speed to %s\n",
3048 sata_spd_string(fls(mask)));
Tejun Heo1c3fae42006-04-02 20:53:28 +09003049
3050 return 0;
3051}
3052
Bartlomiej Zolnierkiewicza9b2c122020-03-26 16:58:07 +01003053#ifdef CONFIG_ATA_ACPI
Tejun Heocf176e12006-04-02 17:54:46 +09003054/**
Tejun Heoa0f79b92007-12-18 16:33:05 +09003055 * ata_timing_cycle2mode - find xfer mode for the specified cycle duration
3056 * @xfer_shift: ATA_SHIFT_* value for transfer type to examine.
3057 * @cycle: cycle duration in ns
3058 *
3059 * Return matching xfer mode for @cycle. The returned mode is of
3060 * the transfer type specified by @xfer_shift. If @cycle is too
3061 * slow for @xfer_shift, 0xff is returned. If @cycle is faster
3062 * than the fastest known mode, the fasted mode is returned.
3063 *
3064 * LOCKING:
3065 * None.
3066 *
3067 * RETURNS:
3068 * Matching xfer_mode, 0xff if no match found.
3069 */
3070u8 ata_timing_cycle2mode(unsigned int xfer_shift, int cycle)
3071{
3072 u8 base_mode = 0xff, last_mode = 0xff;
3073 const struct ata_xfer_ent *ent;
3074 const struct ata_timing *t;
3075
3076 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
3077 if (ent->shift == xfer_shift)
3078 base_mode = ent->base;
3079
3080 for (t = ata_timing_find_mode(base_mode);
3081 t && ata_xfer_mode2shift(t->mode) == xfer_shift; t++) {
3082 unsigned short this_cycle;
3083
3084 switch (xfer_shift) {
3085 case ATA_SHIFT_PIO:
3086 case ATA_SHIFT_MWDMA:
3087 this_cycle = t->cycle;
3088 break;
3089 case ATA_SHIFT_UDMA:
3090 this_cycle = t->udma;
3091 break;
3092 default:
3093 return 0xff;
3094 }
3095
3096 if (cycle > this_cycle)
3097 break;
3098
3099 last_mode = t->mode;
3100 }
3101
3102 return last_mode;
3103}
Bartlomiej Zolnierkiewicza9b2c122020-03-26 16:58:07 +01003104#endif
Tejun Heoa0f79b92007-12-18 16:33:05 +09003105
3106/**
Tejun Heocf176e12006-04-02 17:54:46 +09003107 * ata_down_xfermask_limit - adjust dev xfer masks downward
Tejun Heocf176e12006-04-02 17:54:46 +09003108 * @dev: Device to adjust xfer masks
Tejun Heo458337d2007-02-02 16:22:30 +09003109 * @sel: ATA_DNXFER_* selector
Tejun Heocf176e12006-04-02 17:54:46 +09003110 *
3111 * Adjust xfer masks of @dev downward. Note that this function
3112 * does not apply the change. Invoking ata_set_mode() afterwards
3113 * will apply the limit.
3114 *
3115 * LOCKING:
3116 * Inherited from caller.
3117 *
3118 * RETURNS:
3119 * 0 on success, negative errno on failure
3120 */
Tejun Heo458337d2007-02-02 16:22:30 +09003121int ata_down_xfermask_limit(struct ata_device *dev, unsigned int sel)
Tejun Heocf176e12006-04-02 17:54:46 +09003122{
Tejun Heo458337d2007-02-02 16:22:30 +09003123 char buf[32];
Tejun Heo7dc951a2007-11-27 19:43:42 +09003124 unsigned long orig_mask, xfer_mask;
3125 unsigned long pio_mask, mwdma_mask, udma_mask;
Tejun Heo458337d2007-02-02 16:22:30 +09003126 int quiet, highbit;
Tejun Heocf176e12006-04-02 17:54:46 +09003127
Tejun Heo458337d2007-02-02 16:22:30 +09003128 quiet = !!(sel & ATA_DNXFER_QUIET);
3129 sel &= ~ATA_DNXFER_QUIET;
Tejun Heocf176e12006-04-02 17:54:46 +09003130
Tejun Heo458337d2007-02-02 16:22:30 +09003131 xfer_mask = orig_mask = ata_pack_xfermask(dev->pio_mask,
3132 dev->mwdma_mask,
3133 dev->udma_mask);
3134 ata_unpack_xfermask(xfer_mask, &pio_mask, &mwdma_mask, &udma_mask);
Tejun Heocf176e12006-04-02 17:54:46 +09003135
Tejun Heo458337d2007-02-02 16:22:30 +09003136 switch (sel) {
3137 case ATA_DNXFER_PIO:
3138 highbit = fls(pio_mask) - 1;
3139 pio_mask &= ~(1 << highbit);
3140 break;
3141
3142 case ATA_DNXFER_DMA:
3143 if (udma_mask) {
3144 highbit = fls(udma_mask) - 1;
3145 udma_mask &= ~(1 << highbit);
3146 if (!udma_mask)
3147 return -ENOENT;
3148 } else if (mwdma_mask) {
3149 highbit = fls(mwdma_mask) - 1;
3150 mwdma_mask &= ~(1 << highbit);
3151 if (!mwdma_mask)
3152 return -ENOENT;
3153 }
3154 break;
3155
3156 case ATA_DNXFER_40C:
3157 udma_mask &= ATA_UDMA_MASK_40C;
3158 break;
3159
3160 case ATA_DNXFER_FORCE_PIO0:
3161 pio_mask &= 1;
Gustavo A. R. Silva05b83602017-10-12 14:19:16 -05003162 /* fall through */
Tejun Heo458337d2007-02-02 16:22:30 +09003163 case ATA_DNXFER_FORCE_PIO:
3164 mwdma_mask = 0;
3165 udma_mask = 0;
3166 break;
3167
Tejun Heo458337d2007-02-02 16:22:30 +09003168 default:
3169 BUG();
3170 }
3171
3172 xfer_mask &= ata_pack_xfermask(pio_mask, mwdma_mask, udma_mask);
3173
3174 if (!(xfer_mask & ATA_MASK_PIO) || xfer_mask == orig_mask)
3175 return -ENOENT;
3176
3177 if (!quiet) {
3178 if (xfer_mask & (ATA_MASK_MWDMA | ATA_MASK_UDMA))
3179 snprintf(buf, sizeof(buf), "%s:%s",
3180 ata_mode_string(xfer_mask),
3181 ata_mode_string(xfer_mask & ATA_MASK_PIO));
3182 else
3183 snprintf(buf, sizeof(buf), "%s",
3184 ata_mode_string(xfer_mask));
3185
Joe Perchesa9a79df2011-04-15 15:51:59 -07003186 ata_dev_warn(dev, "limiting speed to %s\n", buf);
Tejun Heo458337d2007-02-02 16:22:30 +09003187 }
Tejun Heocf176e12006-04-02 17:54:46 +09003188
3189 ata_unpack_xfermask(xfer_mask, &dev->pio_mask, &dev->mwdma_mask,
3190 &dev->udma_mask);
3191
Tejun Heocf176e12006-04-02 17:54:46 +09003192 return 0;
Tejun Heocf176e12006-04-02 17:54:46 +09003193}
3194
Tejun Heo3373efd2006-05-15 20:57:53 +09003195static int ata_dev_set_mode(struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196{
Tejun Heod0cb43b2009-07-09 09:27:50 +09003197 struct ata_port *ap = dev->link->ap;
Tejun Heo9af5c9c2007-08-06 18:36:22 +09003198 struct ata_eh_context *ehc = &dev->link->eh_context;
Tejun Heod0cb43b2009-07-09 09:27:50 +09003199 const bool nosetxfer = dev->horkage & ATA_HORKAGE_NOSETXFER;
Tejun Heo4055dee2008-02-07 10:34:08 +09003200 const char *dev_err_whine = "";
3201 int ign_dev_err = 0;
Tejun Heod0cb43b2009-07-09 09:27:50 +09003202 unsigned int err_mask = 0;
Tejun Heo83206a22006-03-24 15:25:31 +09003203 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204
Tejun Heoe8384602006-04-02 18:51:53 +09003205 dev->flags &= ~ATA_DFLAG_PIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206 if (dev->xfer_shift == ATA_SHIFT_PIO)
3207 dev->flags |= ATA_DFLAG_PIO;
3208
Tejun Heod0cb43b2009-07-09 09:27:50 +09003209 if (nosetxfer && ap->flags & ATA_FLAG_SATA && ata_id_is_sata(dev->id))
3210 dev_err_whine = " (SET_XFERMODE skipped)";
3211 else {
3212 if (nosetxfer)
Joe Perchesa9a79df2011-04-15 15:51:59 -07003213 ata_dev_warn(dev,
3214 "NOSETXFER but PATA detected - can't "
3215 "skip SETXFER, might malfunction\n");
Tejun Heod0cb43b2009-07-09 09:27:50 +09003216 err_mask = ata_dev_set_xfermode(dev);
3217 }
Jeff Garzik2dcb4072007-10-19 06:42:56 -04003218
Tejun Heo4055dee2008-02-07 10:34:08 +09003219 if (err_mask & ~AC_ERR_DEV)
3220 goto fail;
Jeff Garzik2dcb4072007-10-19 06:42:56 -04003221
Tejun Heo4055dee2008-02-07 10:34:08 +09003222 /* revalidate */
Tejun Heobaa1e782006-11-01 18:39:27 +09003223 ehc->i.flags |= ATA_EHI_POST_SETMODE;
Tejun Heo422c9da2007-09-23 13:14:12 +09003224 rc = ata_dev_revalidate(dev, ATA_DEV_UNKNOWN, 0);
Tejun Heobaa1e782006-11-01 18:39:27 +09003225 ehc->i.flags &= ~ATA_EHI_POST_SETMODE;
Tejun Heo5eb45c02006-04-02 18:51:52 +09003226 if (rc)
Tejun Heo83206a22006-03-24 15:25:31 +09003227 return rc;
Tejun Heo48a8a142006-03-05 17:55:58 +09003228
Alan Coxb93fda12008-04-08 16:36:44 +01003229 if (dev->xfer_shift == ATA_SHIFT_PIO) {
3230 /* Old CFA may refuse this command, which is just fine */
3231 if (ata_id_is_cfa(dev->id))
3232 ign_dev_err = 1;
3233 /* Catch several broken garbage emulations plus some pre
3234 ATA devices */
3235 if (ata_id_major_version(dev->id) == 0 &&
3236 dev->pio_mode <= XFER_PIO_2)
3237 ign_dev_err = 1;
3238 /* Some very old devices and some bad newer ones fail
3239 any kind of SET_XFERMODE request but support PIO0-2
3240 timings and no IORDY */
3241 if (!ata_id_has_iordy(dev->id) && dev->pio_mode <= XFER_PIO_2)
3242 ign_dev_err = 1;
3243 }
Tejun Heo4055dee2008-02-07 10:34:08 +09003244 /* Early MWDMA devices do DMA but don't allow DMA mode setting.
3245 Don't fail an MWDMA0 set IFF the device indicates it is in MWDMA0 */
3246 if (dev->xfer_shift == ATA_SHIFT_MWDMA &&
3247 dev->dma_mode == XFER_MW_DMA_0 &&
3248 (dev->id[63] >> 8) & 1)
3249 ign_dev_err = 1;
3250
3251 /* if the device is actually configured correctly, ignore dev err */
3252 if (dev->xfer_mode == ata_xfer_mask2mode(ata_id_xfermask(dev->id)))
3253 ign_dev_err = 1;
3254
3255 if (err_mask & AC_ERR_DEV) {
3256 if (!ign_dev_err)
3257 goto fail;
3258 else
3259 dev_err_whine = " (device error ignored)";
3260 }
3261
Tejun Heo23e71c32006-03-06 04:31:57 +09003262 DPRINTK("xfer_shift=%u, xfer_mode=0x%x\n",
3263 dev->xfer_shift, (int)dev->xfer_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264
Damien Le Moal07b9b6d2018-05-09 09:28:10 +09003265 if (!(ehc->i.flags & ATA_EHI_QUIET) ||
3266 ehc->i.flags & ATA_EHI_DID_HARDRESET)
3267 ata_dev_info(dev, "configured for %s%s\n",
3268 ata_mode_string(ata_xfer_mode2mask(dev->xfer_mode)),
3269 dev_err_whine);
Tejun Heo4055dee2008-02-07 10:34:08 +09003270
Tejun Heo83206a22006-03-24 15:25:31 +09003271 return 0;
Tejun Heo4055dee2008-02-07 10:34:08 +09003272
3273 fail:
Joe Perchesa9a79df2011-04-15 15:51:59 -07003274 ata_dev_err(dev, "failed to set xfermode (err_mask=0x%x)\n", err_mask);
Tejun Heo4055dee2008-02-07 10:34:08 +09003275 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276}
3277
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278/**
Alan04351822007-03-06 02:37:52 -08003279 * ata_do_set_mode - Program timings and issue SET FEATURES - XFER
Tejun Heo02607312007-08-06 18:36:23 +09003280 * @link: link on which timings will be programmed
Joe Perches1967b7f2008-02-03 17:08:11 +02003281 * @r_failed_dev: out parameter for failed device
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282 *
Alan04351822007-03-06 02:37:52 -08003283 * Standard implementation of the function used to tune and set
3284 * ATA device disk transfer mode (PIO3, UDMA6, etc.). If
3285 * ata_dev_set_mode() fails, pointer to the failing device is
Tejun Heoe82cbdb2006-04-01 01:38:18 +09003286 * returned in @r_failed_dev.
Jeff Garzik780a87f2005-05-30 15:41:05 -04003287 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003288 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003289 * PCI/etc. bus probe sem.
Tejun Heoe82cbdb2006-04-01 01:38:18 +09003290 *
3291 * RETURNS:
3292 * 0 on success, negative errno otherwise
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293 */
Alan04351822007-03-06 02:37:52 -08003294
Tejun Heo02607312007-08-06 18:36:23 +09003295int ata_do_set_mode(struct ata_link *link, struct ata_device **r_failed_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003296{
Tejun Heo02607312007-08-06 18:36:23 +09003297 struct ata_port *ap = link->ap;
Tejun Heoe8e06192006-04-01 01:38:18 +09003298 struct ata_device *dev;
Tejun Heof58229f2007-08-06 18:36:23 +09003299 int rc = 0, used_dma = 0, found = 0;
Tejun Heo3adcebb2006-05-15 20:57:37 +09003300
Tejun Heoa6d5a512006-03-06 04:31:57 +09003301 /* step 1: calculate xfer_mask */
Tejun Heo1eca4362008-11-03 20:03:17 +09003302 ata_for_each_dev(dev, link, ENABLED) {
Tejun Heo7dc951a2007-11-27 19:43:42 +09003303 unsigned long pio_mask, dma_mask;
Alan Coxb3a70602007-10-02 12:38:26 -04003304 unsigned int mode_mask;
Tejun Heoa6d5a512006-03-06 04:31:57 +09003305
Alan Coxb3a70602007-10-02 12:38:26 -04003306 mode_mask = ATA_DMA_MASK_ATA;
3307 if (dev->class == ATA_DEV_ATAPI)
3308 mode_mask = ATA_DMA_MASK_ATAPI;
3309 else if (ata_id_is_cfa(dev->id))
3310 mode_mask = ATA_DMA_MASK_CFA;
3311
Tejun Heo3373efd2006-05-15 20:57:53 +09003312 ata_dev_xfermask(dev);
Tejun Heo33267322008-02-13 09:15:09 +09003313 ata_force_xfermask(dev);
Tejun Heoa6d5a512006-03-06 04:31:57 +09003314
Tejun Heoacf356b2006-03-24 14:07:50 +09003315 pio_mask = ata_pack_xfermask(dev->pio_mask, 0, 0);
Alan Coxb3a70602007-10-02 12:38:26 -04003316
3317 if (libata_dma_mask & mode_mask)
Sergei Shtylyov80a9c432011-12-22 22:41:32 +03003318 dma_mask = ata_pack_xfermask(0, dev->mwdma_mask,
3319 dev->udma_mask);
Alan Coxb3a70602007-10-02 12:38:26 -04003320 else
3321 dma_mask = 0;
3322
Tejun Heoacf356b2006-03-24 14:07:50 +09003323 dev->pio_mode = ata_xfer_mask2mode(pio_mask);
3324 dev->dma_mode = ata_xfer_mask2mode(dma_mask);
Alan Cox5444a6f2006-03-27 18:58:20 +01003325
Tejun Heo4f659772006-04-01 01:38:18 +09003326 found = 1;
Alan Coxb15b3eb2008-08-01 09:18:34 +01003327 if (ata_dma_enabled(dev))
Alan Cox5444a6f2006-03-27 18:58:20 +01003328 used_dma = 1;
Tejun Heoa6d5a512006-03-06 04:31:57 +09003329 }
Tejun Heo4f659772006-04-01 01:38:18 +09003330 if (!found)
Tejun Heoe82cbdb2006-04-01 01:38:18 +09003331 goto out;
Tejun Heoa6d5a512006-03-06 04:31:57 +09003332
3333 /* step 2: always set host PIO timings */
Tejun Heo1eca4362008-11-03 20:03:17 +09003334 ata_for_each_dev(dev, link, ENABLED) {
Tejun Heo70cd0712007-11-27 19:43:40 +09003335 if (dev->pio_mode == 0xff) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07003336 ata_dev_warn(dev, "no PIO support\n");
Tejun Heoe8e06192006-04-01 01:38:18 +09003337 rc = -EINVAL;
Tejun Heoe82cbdb2006-04-01 01:38:18 +09003338 goto out;
Tejun Heoe8e06192006-04-01 01:38:18 +09003339 }
3340
3341 dev->xfer_mode = dev->pio_mode;
3342 dev->xfer_shift = ATA_SHIFT_PIO;
3343 if (ap->ops->set_piomode)
3344 ap->ops->set_piomode(ap, dev);
3345 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346
Tejun Heoa6d5a512006-03-06 04:31:57 +09003347 /* step 3: set host DMA timings */
Tejun Heo1eca4362008-11-03 20:03:17 +09003348 ata_for_each_dev(dev, link, ENABLED) {
3349 if (!ata_dma_enabled(dev))
Tejun Heoe8e06192006-04-01 01:38:18 +09003350 continue;
3351
3352 dev->xfer_mode = dev->dma_mode;
3353 dev->xfer_shift = ata_xfer_mode2shift(dev->dma_mode);
3354 if (ap->ops->set_dmamode)
3355 ap->ops->set_dmamode(ap, dev);
3356 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357
3358 /* step 4: update devices' xfer mode */
Tejun Heo1eca4362008-11-03 20:03:17 +09003359 ata_for_each_dev(dev, link, ENABLED) {
Tejun Heo3373efd2006-05-15 20:57:53 +09003360 rc = ata_dev_set_mode(dev);
Tejun Heo5bbc53f2006-04-01 01:38:17 +09003361 if (rc)
Tejun Heoe82cbdb2006-04-01 01:38:18 +09003362 goto out;
Tejun Heo83206a22006-03-24 15:25:31 +09003363 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003364
Tejun Heoe8e06192006-04-01 01:38:18 +09003365 /* Record simplex status. If we selected DMA then the other
3366 * host channels are not permitted to do so.
Alan Cox5444a6f2006-03-27 18:58:20 +01003367 */
Jeff Garzikcca39742006-08-24 03:19:22 -04003368 if (used_dma && (ap->host->flags & ATA_HOST_SIMPLEX))
Alan032af1c2007-03-01 17:36:46 +00003369 ap->host->simplex_claimed = ap;
Alan Cox5444a6f2006-03-27 18:58:20 +01003370
Tejun Heoe82cbdb2006-04-01 01:38:18 +09003371 out:
3372 if (rc)
3373 *r_failed_dev = dev;
3374 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01003376EXPORT_SYMBOL_GPL(ata_do_set_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377
3378/**
Tejun Heoaa2731a2008-04-07 22:47:19 +09003379 * ata_wait_ready - wait for link to become ready
3380 * @link: link to be waited on
3381 * @deadline: deadline jiffies for the operation
3382 * @check_ready: callback to check link readiness
3383 *
3384 * Wait for @link to become ready. @check_ready should return
3385 * positive number if @link is ready, 0 if it isn't, -ENODEV if
3386 * link doesn't seem to be occupied, other errno for other error
3387 * conditions.
3388 *
3389 * Transient -ENODEV conditions are allowed for
3390 * ATA_TMOUT_FF_WAIT.
3391 *
3392 * LOCKING:
3393 * EH context.
3394 *
3395 * RETURNS:
Masanari Iidac9b55602016-04-13 23:36:27 +09003396 * 0 if @link is ready before @deadline; otherwise, -errno.
Tejun Heoaa2731a2008-04-07 22:47:19 +09003397 */
3398int ata_wait_ready(struct ata_link *link, unsigned long deadline,
3399 int (*check_ready)(struct ata_link *link))
3400{
3401 unsigned long start = jiffies;
Tejun Heob48d58f2010-04-09 19:46:38 +09003402 unsigned long nodev_deadline;
Tejun Heoaa2731a2008-04-07 22:47:19 +09003403 int warned = 0;
3404
Tejun Heob48d58f2010-04-09 19:46:38 +09003405 /* choose which 0xff timeout to use, read comment in libata.h */
3406 if (link->ap->host->flags & ATA_HOST_PARALLEL_SCAN)
3407 nodev_deadline = ata_deadline(start, ATA_TMOUT_FF_WAIT_LONG);
3408 else
3409 nodev_deadline = ata_deadline(start, ATA_TMOUT_FF_WAIT);
3410
Tejun Heob1c72912008-07-31 17:02:43 +09003411 /* Slave readiness can't be tested separately from master. On
3412 * M/S emulation configuration, this function should be called
3413 * only on the master and it will handle both master and slave.
3414 */
3415 WARN_ON(link == link->ap->slave_link);
3416
Tejun Heoaa2731a2008-04-07 22:47:19 +09003417 if (time_after(nodev_deadline, deadline))
3418 nodev_deadline = deadline;
3419
3420 while (1) {
3421 unsigned long now = jiffies;
3422 int ready, tmp;
3423
3424 ready = tmp = check_ready(link);
3425 if (ready > 0)
3426 return 0;
3427
Tejun Heob48d58f2010-04-09 19:46:38 +09003428 /*
3429 * -ENODEV could be transient. Ignore -ENODEV if link
Tejun Heoaa2731a2008-04-07 22:47:19 +09003430 * is online. Also, some SATA devices take a long
Tejun Heob48d58f2010-04-09 19:46:38 +09003431 * time to clear 0xff after reset. Wait for
3432 * ATA_TMOUT_FF_WAIT[_LONG] on -ENODEV if link isn't
3433 * offline.
Tejun Heoaa2731a2008-04-07 22:47:19 +09003434 *
3435 * Note that some PATA controllers (pata_ali) explode
3436 * if status register is read more than once when
3437 * there's no device attached.
3438 */
3439 if (ready == -ENODEV) {
3440 if (ata_link_online(link))
3441 ready = 0;
3442 else if ((link->ap->flags & ATA_FLAG_SATA) &&
3443 !ata_link_offline(link) &&
3444 time_before(now, nodev_deadline))
3445 ready = 0;
3446 }
3447
3448 if (ready)
3449 return ready;
3450 if (time_after(now, deadline))
3451 return -EBUSY;
3452
3453 if (!warned && time_after(now, start + 5 * HZ) &&
3454 (deadline - now > 3 * HZ)) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07003455 ata_link_warn(link,
Tejun Heoaa2731a2008-04-07 22:47:19 +09003456 "link is slow to respond, please be patient "
3457 "(ready=%d)\n", tmp);
3458 warned = 1;
3459 }
3460
Tejun Heo97750ce2010-09-06 17:56:29 +02003461 ata_msleep(link->ap, 50);
Tejun Heoaa2731a2008-04-07 22:47:19 +09003462 }
3463}
3464
3465/**
3466 * ata_wait_after_reset - wait for link to become ready after reset
3467 * @link: link to be waited on
3468 * @deadline: deadline jiffies for the operation
3469 * @check_ready: callback to check link readiness
3470 *
3471 * Wait for @link to become ready after reset.
3472 *
3473 * LOCKING:
3474 * EH context.
3475 *
3476 * RETURNS:
Masanari Iidac9b55602016-04-13 23:36:27 +09003477 * 0 if @link is ready before @deadline; otherwise, -errno.
Tejun Heoaa2731a2008-04-07 22:47:19 +09003478 */
Harvey Harrison2b4221b2008-04-24 18:37:34 -07003479int ata_wait_after_reset(struct ata_link *link, unsigned long deadline,
Tejun Heoaa2731a2008-04-07 22:47:19 +09003480 int (*check_ready)(struct ata_link *link))
3481{
Tejun Heo97750ce2010-09-06 17:56:29 +02003482 ata_msleep(link->ap, ATA_WAIT_AFTER_RESET);
Tejun Heoaa2731a2008-04-07 22:47:19 +09003483
3484 return ata_wait_ready(link, deadline, check_ready);
3485}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01003486EXPORT_SYMBOL_GPL(ata_wait_after_reset);
Tejun Heoaa2731a2008-04-07 22:47:19 +09003487
3488/**
Tejun Heo0aa11132008-04-07 22:47:18 +09003489 * ata_std_prereset - prepare for reset
Tejun Heocc0680a2007-08-06 18:36:23 +09003490 * @link: ATA link to be reset
Tejun Heod4b2bab2007-02-02 16:50:52 +09003491 * @deadline: deadline jiffies for the operation
Tejun Heof5914a42006-05-31 18:27:48 +09003492 *
Tejun Heocc0680a2007-08-06 18:36:23 +09003493 * @link is about to be reset. Initialize it. Failure from
Tejun Heob8cffc62007-02-02 16:50:52 +09003494 * prereset makes libata abort whole reset sequence and give up
3495 * that port, so prereset should be best-effort. It does its
3496 * best to prepare for reset sequence but if things go wrong, it
3497 * should just whine, not fail.
Tejun Heof5914a42006-05-31 18:27:48 +09003498 *
3499 * LOCKING:
3500 * Kernel thread context (may sleep)
3501 *
3502 * RETURNS:
3503 * 0 on success, -errno otherwise.
3504 */
Tejun Heo0aa11132008-04-07 22:47:18 +09003505int ata_std_prereset(struct ata_link *link, unsigned long deadline)
Tejun Heof5914a42006-05-31 18:27:48 +09003506{
Tejun Heocc0680a2007-08-06 18:36:23 +09003507 struct ata_port *ap = link->ap;
Tejun Heo936fd732007-08-06 18:36:23 +09003508 struct ata_eh_context *ehc = &link->eh_context;
Tejun Heoe9c83912006-07-03 16:07:26 +09003509 const unsigned long *timing = sata_ehc_deb_timing(ehc);
Tejun Heof5914a42006-05-31 18:27:48 +09003510 int rc;
3511
Tejun Heof5914a42006-05-31 18:27:48 +09003512 /* if we're about to do hardreset, nothing more to do */
3513 if (ehc->i.action & ATA_EH_HARDRESET)
3514 return 0;
3515
Tejun Heo936fd732007-08-06 18:36:23 +09003516 /* if SATA, resume link */
Tejun Heoa16abc02007-05-21 18:33:47 +02003517 if (ap->flags & ATA_FLAG_SATA) {
Tejun Heo936fd732007-08-06 18:36:23 +09003518 rc = sata_link_resume(link, timing, deadline);
Tejun Heob8cffc62007-02-02 16:50:52 +09003519 /* whine about phy resume failure but proceed */
3520 if (rc && rc != -EOPNOTSUPP)
Joe Perchesa9a79df2011-04-15 15:51:59 -07003521 ata_link_warn(link,
3522 "failed to resume link for reset (errno=%d)\n",
3523 rc);
Tejun Heof5914a42006-05-31 18:27:48 +09003524 }
3525
Tejun Heo45db2f62008-04-08 01:46:56 +09003526 /* no point in trying softreset on offline link */
Tejun Heob1c72912008-07-31 17:02:43 +09003527 if (ata_phys_link_offline(link))
Tejun Heo45db2f62008-04-08 01:46:56 +09003528 ehc->i.action &= ~ATA_EH_SOFTRESET;
3529
Tejun Heof5914a42006-05-31 18:27:48 +09003530 return 0;
3531}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01003532EXPORT_SYMBOL_GPL(ata_std_prereset);
Tejun Heof5914a42006-05-31 18:27:48 +09003533
Tejun Heoc2bd5802006-01-24 17:05:22 +09003534/**
Tejun Heo57c9efd2008-04-07 22:47:19 +09003535 * sata_std_hardreset - COMRESET w/o waiting or classification
3536 * @link: link to reset
3537 * @class: resulting class of attached device
3538 * @deadline: deadline jiffies for the operation
3539 *
3540 * Standard SATA COMRESET w/o waiting or classification.
3541 *
3542 * LOCKING:
3543 * Kernel thread context (may sleep)
3544 *
3545 * RETURNS:
3546 * 0 if link offline, -EAGAIN if link online, -errno on errors.
3547 */
3548int sata_std_hardreset(struct ata_link *link, unsigned int *class,
3549 unsigned long deadline)
3550{
3551 const unsigned long *timing = sata_ehc_deb_timing(&link->eh_context);
3552 bool online;
3553 int rc;
3554
3555 /* do hardreset */
3556 rc = sata_link_hardreset(link, timing, deadline, &online, NULL);
Tejun Heo57c9efd2008-04-07 22:47:19 +09003557 return online ? -EAGAIN : rc;
3558}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01003559EXPORT_SYMBOL_GPL(sata_std_hardreset);
Tejun Heo57c9efd2008-04-07 22:47:19 +09003560
3561/**
Tejun Heo203c75b2008-04-07 22:47:18 +09003562 * ata_std_postreset - standard postreset callback
Tejun Heocc0680a2007-08-06 18:36:23 +09003563 * @link: the target ata_link
Tejun Heoc2bd5802006-01-24 17:05:22 +09003564 * @classes: classes of attached devices
3565 *
3566 * This function is invoked after a successful reset. Note that
3567 * the device might have been reset more than once using
3568 * different reset methods before postreset is invoked.
Tejun Heoc2bd5802006-01-24 17:05:22 +09003569 *
Tejun Heoc2bd5802006-01-24 17:05:22 +09003570 * LOCKING:
3571 * Kernel thread context (may sleep)
3572 */
Tejun Heo203c75b2008-04-07 22:47:18 +09003573void ata_std_postreset(struct ata_link *link, unsigned int *classes)
Tejun Heoc2bd5802006-01-24 17:05:22 +09003574{
Tejun Heof0465192008-05-19 01:15:08 +09003575 u32 serror;
3576
Tejun Heoc2bd5802006-01-24 17:05:22 +09003577 DPRINTK("ENTER\n");
3578
Tejun Heof0465192008-05-19 01:15:08 +09003579 /* reset complete, clear SError */
3580 if (!sata_scr_read(link, SCR_ERROR, &serror))
3581 sata_scr_write(link, SCR_ERROR, serror);
3582
Tejun Heoc2bd5802006-01-24 17:05:22 +09003583 /* print link status */
Tejun Heo936fd732007-08-06 18:36:23 +09003584 sata_print_link_status(link);
Tejun Heoc2bd5802006-01-24 17:05:22 +09003585
Tejun Heoc2bd5802006-01-24 17:05:22 +09003586 DPRINTK("EXIT\n");
3587}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01003588EXPORT_SYMBOL_GPL(ata_std_postreset);
Tejun Heoc2bd5802006-01-24 17:05:22 +09003589
Tejun Heoa62c0fc2006-01-24 17:05:22 +09003590/**
Tejun Heo623a3122006-03-05 17:55:58 +09003591 * ata_dev_same_device - Determine whether new ID matches configured device
Tejun Heo623a3122006-03-05 17:55:58 +09003592 * @dev: device to compare against
3593 * @new_class: class of the new device
3594 * @new_id: IDENTIFY page of the new device
3595 *
3596 * Compare @new_class and @new_id against @dev and determine
3597 * whether @dev is the device indicated by @new_class and
3598 * @new_id.
3599 *
3600 * LOCKING:
3601 * None.
3602 *
3603 * RETURNS:
3604 * 1 if @dev matches @new_class and @new_id, 0 otherwise.
3605 */
Tejun Heo3373efd2006-05-15 20:57:53 +09003606static int ata_dev_same_device(struct ata_device *dev, unsigned int new_class,
3607 const u16 *new_id)
Tejun Heo623a3122006-03-05 17:55:58 +09003608{
3609 const u16 *old_id = dev->id;
Tejun Heoa0cf7332007-01-02 20:18:49 +09003610 unsigned char model[2][ATA_ID_PROD_LEN + 1];
3611 unsigned char serial[2][ATA_ID_SERNO_LEN + 1];
Tejun Heo623a3122006-03-05 17:55:58 +09003612
3613 if (dev->class != new_class) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07003614 ata_dev_info(dev, "class mismatch %d != %d\n",
3615 dev->class, new_class);
Tejun Heo623a3122006-03-05 17:55:58 +09003616 return 0;
3617 }
3618
Tejun Heoa0cf7332007-01-02 20:18:49 +09003619 ata_id_c_string(old_id, model[0], ATA_ID_PROD, sizeof(model[0]));
3620 ata_id_c_string(new_id, model[1], ATA_ID_PROD, sizeof(model[1]));
3621 ata_id_c_string(old_id, serial[0], ATA_ID_SERNO, sizeof(serial[0]));
3622 ata_id_c_string(new_id, serial[1], ATA_ID_SERNO, sizeof(serial[1]));
Tejun Heo623a3122006-03-05 17:55:58 +09003623
3624 if (strcmp(model[0], model[1])) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07003625 ata_dev_info(dev, "model number mismatch '%s' != '%s'\n",
3626 model[0], model[1]);
Tejun Heo623a3122006-03-05 17:55:58 +09003627 return 0;
3628 }
3629
3630 if (strcmp(serial[0], serial[1])) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07003631 ata_dev_info(dev, "serial number mismatch '%s' != '%s'\n",
3632 serial[0], serial[1]);
Tejun Heo623a3122006-03-05 17:55:58 +09003633 return 0;
3634 }
3635
Tejun Heo623a3122006-03-05 17:55:58 +09003636 return 1;
3637}
3638
3639/**
Tejun Heofe309112007-05-15 03:28:15 +09003640 * ata_dev_reread_id - Re-read IDENTIFY data
Henrik Kretzschmar3fae4502007-06-19 10:10:50 +02003641 * @dev: target ATA device
Tejun Heobff04642006-11-10 18:08:10 +09003642 * @readid_flags: read ID flags
Tejun Heo623a3122006-03-05 17:55:58 +09003643 *
3644 * Re-read IDENTIFY page and make sure @dev is still attached to
3645 * the port.
3646 *
3647 * LOCKING:
3648 * Kernel thread context (may sleep)
3649 *
3650 * RETURNS:
3651 * 0 on success, negative errno otherwise
3652 */
Tejun Heofe309112007-05-15 03:28:15 +09003653int ata_dev_reread_id(struct ata_device *dev, unsigned int readid_flags)
Tejun Heo623a3122006-03-05 17:55:58 +09003654{
Tejun Heo5eb45c02006-04-02 18:51:52 +09003655 unsigned int class = dev->class;
Tejun Heo9af5c9c2007-08-06 18:36:22 +09003656 u16 *id = (void *)dev->link->ap->sector_buf;
Tejun Heo623a3122006-03-05 17:55:58 +09003657 int rc;
3658
Tejun Heofe635c72006-05-15 20:57:35 +09003659 /* read ID data */
Tejun Heobff04642006-11-10 18:08:10 +09003660 rc = ata_dev_read_id(dev, &class, readid_flags, id);
Tejun Heo623a3122006-03-05 17:55:58 +09003661 if (rc)
Tejun Heofe309112007-05-15 03:28:15 +09003662 return rc;
Tejun Heo623a3122006-03-05 17:55:58 +09003663
3664 /* is the device still there? */
Tejun Heofe309112007-05-15 03:28:15 +09003665 if (!ata_dev_same_device(dev, class, id))
3666 return -ENODEV;
Tejun Heo623a3122006-03-05 17:55:58 +09003667
Tejun Heofe635c72006-05-15 20:57:35 +09003668 memcpy(dev->id, id, sizeof(id[0]) * ATA_ID_WORDS);
Tejun Heofe309112007-05-15 03:28:15 +09003669 return 0;
3670}
3671
3672/**
3673 * ata_dev_revalidate - Revalidate ATA device
3674 * @dev: device to revalidate
Tejun Heo422c9da2007-09-23 13:14:12 +09003675 * @new_class: new class code
Tejun Heofe309112007-05-15 03:28:15 +09003676 * @readid_flags: read ID flags
3677 *
3678 * Re-read IDENTIFY page, make sure @dev is still attached to the
3679 * port and reconfigure it according to the new IDENTIFY page.
3680 *
3681 * LOCKING:
3682 * Kernel thread context (may sleep)
3683 *
3684 * RETURNS:
3685 * 0 on success, negative errno otherwise
3686 */
Tejun Heo422c9da2007-09-23 13:14:12 +09003687int ata_dev_revalidate(struct ata_device *dev, unsigned int new_class,
3688 unsigned int readid_flags)
Tejun Heofe309112007-05-15 03:28:15 +09003689{
Tejun Heo6ddcd3b2007-05-15 03:28:15 +09003690 u64 n_sectors = dev->n_sectors;
Tejun Heo5920dad2009-07-15 17:11:41 +09003691 u64 n_native_sectors = dev->n_native_sectors;
Tejun Heofe309112007-05-15 03:28:15 +09003692 int rc;
3693
3694 if (!ata_dev_enabled(dev))
3695 return -ENODEV;
3696
Tejun Heo422c9da2007-09-23 13:14:12 +09003697 /* fail early if !ATA && !ATAPI to avoid issuing [P]IDENTIFY to PMP */
3698 if (ata_class_enabled(new_class) &&
Borislav Petkovf0d06132009-04-26 16:33:34 +02003699 new_class != ATA_DEV_ATA &&
3700 new_class != ATA_DEV_ATAPI &&
Hannes Reinecke9162c652014-11-05 13:08:21 +01003701 new_class != ATA_DEV_ZAC &&
Borislav Petkovf0d06132009-04-26 16:33:34 +02003702 new_class != ATA_DEV_SEMB) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07003703 ata_dev_info(dev, "class mismatch %u != %u\n",
3704 dev->class, new_class);
Tejun Heo422c9da2007-09-23 13:14:12 +09003705 rc = -ENODEV;
3706 goto fail;
3707 }
3708
Tejun Heofe309112007-05-15 03:28:15 +09003709 /* re-read ID */
3710 rc = ata_dev_reread_id(dev, readid_flags);
3711 if (rc)
3712 goto fail;
Tejun Heo623a3122006-03-05 17:55:58 +09003713
3714 /* configure device according to the new ID */
Tejun Heoefdaedc2006-11-01 18:38:52 +09003715 rc = ata_dev_configure(dev);
Tejun Heo6ddcd3b2007-05-15 03:28:15 +09003716 if (rc)
3717 goto fail;
3718
3719 /* verify n_sectors hasn't changed */
Tejun Heo445d2112010-04-05 10:33:13 +09003720 if (dev->class != ATA_DEV_ATA || !n_sectors ||
3721 dev->n_sectors == n_sectors)
3722 return 0;
3723
3724 /* n_sectors has changed */
Joe Perchesa9a79df2011-04-15 15:51:59 -07003725 ata_dev_warn(dev, "n_sectors mismatch %llu != %llu\n",
3726 (unsigned long long)n_sectors,
3727 (unsigned long long)dev->n_sectors);
Tejun Heo445d2112010-04-05 10:33:13 +09003728
3729 /*
3730 * Something could have caused HPA to be unlocked
3731 * involuntarily. If n_native_sectors hasn't changed and the
3732 * new size matches it, keep the device.
3733 */
3734 if (dev->n_native_sectors == n_native_sectors &&
3735 dev->n_sectors > n_sectors && dev->n_sectors == n_native_sectors) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07003736 ata_dev_warn(dev,
3737 "new n_sectors matches native, probably "
3738 "late HPA unlock, n_sectors updated\n");
Tejun Heo68939ce2010-05-15 20:09:33 +02003739 /* use the larger n_sectors */
Tejun Heo445d2112010-04-05 10:33:13 +09003740 return 0;
Tejun Heo6ddcd3b2007-05-15 03:28:15 +09003741 }
3742
Tejun Heo445d2112010-04-05 10:33:13 +09003743 /*
3744 * Some BIOSes boot w/o HPA but resume w/ HPA locked. Try
3745 * unlocking HPA in those cases.
3746 *
3747 * https://bugzilla.kernel.org/show_bug.cgi?id=15396
3748 */
3749 if (dev->n_native_sectors == n_native_sectors &&
3750 dev->n_sectors < n_sectors && n_sectors == n_native_sectors &&
3751 !(dev->horkage & ATA_HORKAGE_BROKEN_HPA)) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07003752 ata_dev_warn(dev,
3753 "old n_sectors matches native, probably "
3754 "late HPA lock, will try to unlock HPA\n");
Tejun Heo445d2112010-04-05 10:33:13 +09003755 /* try unlocking HPA */
3756 dev->flags |= ATA_DFLAG_UNLOCK_HPA;
3757 rc = -EIO;
3758 } else
3759 rc = -ENODEV;
Tejun Heo623a3122006-03-05 17:55:58 +09003760
Tejun Heo445d2112010-04-05 10:33:13 +09003761 /* restore original n_[native_]sectors and fail */
3762 dev->n_native_sectors = n_native_sectors;
3763 dev->n_sectors = n_sectors;
Tejun Heo623a3122006-03-05 17:55:58 +09003764 fail:
Joe Perchesa9a79df2011-04-15 15:51:59 -07003765 ata_dev_err(dev, "revalidation failed (errno=%d)\n", rc);
Tejun Heo623a3122006-03-05 17:55:58 +09003766 return rc;
3767}
3768
Alan Cox6919a0a2006-10-27 19:08:46 -07003769struct ata_blacklist_entry {
3770 const char *model_num;
3771 const char *model_rev;
3772 unsigned long horkage;
3773};
3774
3775static const struct ata_blacklist_entry ata_device_blacklist [] = {
3776 /* Devices with DMA related problems under Linux */
3777 { "WDC AC11000H", NULL, ATA_HORKAGE_NODMA },
3778 { "WDC AC22100H", NULL, ATA_HORKAGE_NODMA },
3779 { "WDC AC32500H", NULL, ATA_HORKAGE_NODMA },
3780 { "WDC AC33100H", NULL, ATA_HORKAGE_NODMA },
3781 { "WDC AC31600H", NULL, ATA_HORKAGE_NODMA },
3782 { "WDC AC32100H", "24.09P07", ATA_HORKAGE_NODMA },
3783 { "WDC AC23200L", "21.10N21", ATA_HORKAGE_NODMA },
3784 { "Compaq CRD-8241B", NULL, ATA_HORKAGE_NODMA },
3785 { "CRD-8400B", NULL, ATA_HORKAGE_NODMA },
Mark Lord7da4c932010-07-01 18:18:12 -04003786 { "CRD-848[02]B", NULL, ATA_HORKAGE_NODMA },
Alan Cox6919a0a2006-10-27 19:08:46 -07003787 { "CRD-84", NULL, ATA_HORKAGE_NODMA },
3788 { "SanDisk SDP3B", NULL, ATA_HORKAGE_NODMA },
3789 { "SanDisk SDP3B-64", NULL, ATA_HORKAGE_NODMA },
3790 { "SANYO CD-ROM CRD", NULL, ATA_HORKAGE_NODMA },
3791 { "HITACHI CDR-8", NULL, ATA_HORKAGE_NODMA },
Mark Lord7da4c932010-07-01 18:18:12 -04003792 { "HITACHI CDR-8[34]35",NULL, ATA_HORKAGE_NODMA },
Alan Cox6919a0a2006-10-27 19:08:46 -07003793 { "Toshiba CD-ROM XM-6202B", NULL, ATA_HORKAGE_NODMA },
3794 { "TOSHIBA CD-ROM XM-1702BC", NULL, ATA_HORKAGE_NODMA },
3795 { "CD-532E-A", NULL, ATA_HORKAGE_NODMA },
3796 { "E-IDE CD-ROM CR-840",NULL, ATA_HORKAGE_NODMA },
3797 { "CD-ROM Drive/F5A", NULL, ATA_HORKAGE_NODMA },
3798 { "WPI CDD-820", NULL, ATA_HORKAGE_NODMA },
3799 { "SAMSUNG CD-ROM SC-148C", NULL, ATA_HORKAGE_NODMA },
3800 { "SAMSUNG CD-ROM SC", NULL, ATA_HORKAGE_NODMA },
Alan Cox6919a0a2006-10-27 19:08:46 -07003801 { "ATAPI CD-ROM DRIVE 40X MAXIMUM",NULL,ATA_HORKAGE_NODMA },
3802 { "_NEC DV5800A", NULL, ATA_HORKAGE_NODMA },
Jeff Garzik2dcb4072007-10-19 06:42:56 -04003803 { "SAMSUNG CD-ROM SN-124", "N001", ATA_HORKAGE_NODMA },
Dave Jones39f19882007-05-21 14:31:03 -04003804 { "Seagate STT20000A", NULL, ATA_HORKAGE_NODMA },
Prarit Bhargavad17d7942012-08-23 15:11:52 -04003805 { " 2GB ATA Flash Disk", "ADMA428M", ATA_HORKAGE_NODMA },
Shikha Jainb00622f2016-01-11 14:55:45 -08003806 { "VRFDFC22048UCHC-TE*", NULL, ATA_HORKAGE_NODMA },
Tejun Heo3af9a772007-09-23 13:19:54 +09003807 /* Odd clown on sil3726/4726 PMPs */
Tejun Heo50af2fa2008-05-19 01:15:14 +09003808 { "Config Disk", NULL, ATA_HORKAGE_DISABLE },
Alan Cox6919a0a2006-10-27 19:08:46 -07003809
Albert Lee18d6e9d2007-04-02 11:34:15 +08003810 /* Weird ATAPI devices */
Tejun Heo40a1d532007-06-27 02:49:38 +09003811 { "TORiSAN DVD-ROM DRD-N216", NULL, ATA_HORKAGE_MAX_SEC_128 },
Tejun Heo6a87e422008-11-03 19:01:09 +09003812 { "QUANTUM DAT DAT72-000", NULL, ATA_HORKAGE_ATAPI_MOD16_DMA },
Shan Haia32450e2013-03-18 10:30:44 +08003813 { "Slimtype DVD A DS8A8SH", NULL, ATA_HORKAGE_MAX_SEC_LBA48 },
Shan Hai0523f032013-10-28 16:08:01 +08003814 { "Slimtype DVD A DS8A9SH", NULL, ATA_HORKAGE_MAX_SEC_LBA48 },
Albert Lee18d6e9d2007-04-02 11:34:15 +08003815
David Milburnaf34d632015-07-13 11:48:23 -05003816 /*
3817 * Causes silent data corruption with higher max sects.
3818 * http://lkml.kernel.org/g/x49wpy40ysk.fsf@segfault.boston.devel.redhat.com
3819 */
3820 { "ST380013AS", "3.20", ATA_HORKAGE_MAX_SEC_1024 },
3821
Tejun Heo1488a1e2016-07-18 18:40:00 -04003822 /*
Tejun Heoe0edc8c2017-01-06 11:48:50 -05003823 * These devices time out with higher max sects.
Tejun Heo1488a1e2016-07-18 18:40:00 -04003824 * https://bugzilla.kernel.org/show_bug.cgi?id=121671
3825 */
Tejun Heoe0edc8c2017-01-06 11:48:50 -05003826 { "LITEON CX1-JB*-HP", NULL, ATA_HORKAGE_MAX_SEC_1024 },
Xinyu Lindb5ff902017-12-17 20:13:39 +08003827 { "LITEON EP1-*", NULL, ATA_HORKAGE_MAX_SEC_1024 },
Tejun Heo1488a1e2016-07-18 18:40:00 -04003828
Alan Cox6919a0a2006-10-27 19:08:46 -07003829 /* Devices we expect to fail diagnostics */
3830
3831 /* Devices where NCQ should be avoided */
3832 /* NCQ is slow */
Jeff Garzik2dcb4072007-10-19 06:42:56 -04003833 { "WDC WD740ADFD-00", NULL, ATA_HORKAGE_NONCQ },
Tejun Heo459ad682007-12-07 12:46:23 +09003834 { "WDC WD740ADFD-00NLR1", NULL, ATA_HORKAGE_NONCQ, },
Tejun Heo09125ea2007-02-28 15:21:23 +09003835 /* http://thread.gmane.org/gmane.linux.ide/14907 */
3836 { "FUJITSU MHT2060BH", NULL, ATA_HORKAGE_NONCQ },
Paul Rolland7acfaf32007-03-26 21:43:44 -08003837 /* NCQ is broken */
Jeff Garzik539cc7c2007-09-20 16:31:47 -04003838 { "Maxtor *", "BANC*", ATA_HORKAGE_NONCQ },
Alan Cox0e3dbc02007-09-20 15:22:47 +01003839 { "Maxtor 7V300F0", "VA111630", ATA_HORKAGE_NONCQ },
Paolo Ornatida6f0ec2007-10-04 11:06:56 +09003840 { "ST380817AS", "3.42", ATA_HORKAGE_NONCQ },
Tejun Heoe41bd3e2007-12-09 19:45:39 +09003841 { "ST3160023AS", "3.42", ATA_HORKAGE_NONCQ },
Lubomir Bulej5ccfca92008-12-22 11:35:22 +01003842 { "OCZ CORE_SSD", "02.10104", ATA_HORKAGE_NONCQ },
Jeff Garzik539cc7c2007-09-20 16:31:47 -04003843
Tejun Heoac70a962008-11-27 13:36:48 +09003844 /* Seagate NCQ + FLUSH CACHE firmware bug */
Mark Lord4d1f9082010-07-05 18:53:37 -04003845 { "ST31500341AS", "SD1[5-9]", ATA_HORKAGE_NONCQ |
Tejun Heoac70a962008-11-27 13:36:48 +09003846 ATA_HORKAGE_FIRMWARE_WARN },
Tejun Heod10d4912008-12-11 13:42:42 +09003847
Mark Lord4d1f9082010-07-05 18:53:37 -04003848 { "ST31000333AS", "SD1[5-9]", ATA_HORKAGE_NONCQ |
Tejun Heod10d4912008-12-11 13:42:42 +09003849 ATA_HORKAGE_FIRMWARE_WARN },
3850
Mark Lord4d1f9082010-07-05 18:53:37 -04003851 { "ST3640[36]23AS", "SD1[5-9]", ATA_HORKAGE_NONCQ |
Tejun Heod10d4912008-12-11 13:42:42 +09003852 ATA_HORKAGE_FIRMWARE_WARN },
3853
Mark Lord4d1f9082010-07-05 18:53:37 -04003854 { "ST3320[68]13AS", "SD1[5-9]", ATA_HORKAGE_NONCQ |
Tejun Heoac70a962008-11-27 13:36:48 +09003855 ATA_HORKAGE_FIRMWARE_WARN },
3856
Hans de Goede31f62642019-06-11 16:32:59 +02003857 /* drives which fail FPDMA_AA activation (some may freeze afterwards)
3858 the ST disks also have LPM issues */
Hans de Goede8756a252019-07-01 18:58:13 +02003859 { "ST1000LM024 HN-M101MBB", NULL, ATA_HORKAGE_BROKEN_FPDMA_AA |
Hans de Goede31f62642019-06-11 16:32:59 +02003860 ATA_HORKAGE_NOLPM, },
Aleksei Mamlin08c85d22015-07-01 13:48:30 +03003861 { "VB0250EAVER", "HPG7", ATA_HORKAGE_BROKEN_FPDMA_AA },
Michele Baldessari87809942013-11-25 19:00:14 +00003862
Robert Hancock36e337d2007-04-02 22:05:29 -06003863 /* Blacklist entries taken from Silicon Image 3124/3132
3864 Windows driver .inf file - also several Linux problem reports */
3865 { "HTS541060G9SA00", "MB3OC60D", ATA_HORKAGE_NONCQ, },
3866 { "HTS541080G9SA00", "MB4OC60D", ATA_HORKAGE_NONCQ, },
3867 { "HTS541010G9SA00", "MBZOC60D", ATA_HORKAGE_NONCQ, },
Alan Cox6919a0a2006-10-27 19:08:46 -07003868
Tejun Heo68b0ddb2010-04-05 10:51:26 +09003869 /* https://bugzilla.kernel.org/show_bug.cgi?id=15573 */
3870 { "C300-CTFDDAC128MAG", "0001", ATA_HORKAGE_NONCQ, },
3871
Tejun Heo322579d2018-05-08 14:21:56 -07003872 /* Some Sandisk SSDs lock up hard with NCQ enabled. Reported on
3873 SD7SN6S256G and SD8SN8U256G */
3874 { "SanDisk SD[78]SN*G", NULL, ATA_HORKAGE_NONCQ, },
3875
Tejun Heo16c55b02007-08-29 11:58:33 +09003876 /* devices which puke on READ_NATIVE_MAX */
3877 { "HDS724040KLSA80", "KFAOA20N", ATA_HORKAGE_BROKEN_HPA, },
3878 { "WDC WD3200JD-00KLB0", "WD-WCAMR1130137", ATA_HORKAGE_BROKEN_HPA },
3879 { "WDC WD2500JD-00HBB0", "WD-WMAL71490727", ATA_HORKAGE_BROKEN_HPA },
3880 { "MAXTOR 6L080L4", "A93.0500", ATA_HORKAGE_BROKEN_HPA },
Alan Cox6919a0a2006-10-27 19:08:46 -07003881
Tejun Heo7831387b2009-08-07 01:59:15 +09003882 /* this one allows HPA unlocking but fails IOs on the area */
3883 { "OCZ-VERTEX", "1.30", ATA_HORKAGE_BROKEN_HPA },
3884
Alan Cox93328e112007-09-29 04:06:48 -04003885 /* Devices which report 1 sector over size HPA */
3886 { "ST340823A", NULL, ATA_HORKAGE_HPA_SIZE, },
3887 { "ST320413A", NULL, ATA_HORKAGE_HPA_SIZE, },
Mikko Rapelib152fcd2008-02-19 01:41:25 +01003888 { "ST310211A", NULL, ATA_HORKAGE_HPA_SIZE, },
Alan Cox93328e112007-09-29 04:06:48 -04003889
Alan Cox6bbfd532007-11-05 22:58:58 +00003890 /* Devices which get the IVB wrong */
3891 { "QUANTUM FIREBALLlct10 05", "A03.0900", ATA_HORKAGE_IVB, },
Alan Coxa79067e2008-04-29 14:08:36 +01003892 /* Maybe we should just blacklist TSSTcorp... */
Mark Lord7da4c932010-07-01 18:18:12 -04003893 { "TSSTcorp CDDVDW SH-S202[HJN]", "SB0[01]", ATA_HORKAGE_IVB, },
Alan Cox6bbfd532007-11-05 22:58:58 +00003894
Jens Axboe9ce8e302008-08-27 15:23:18 +02003895 /* Devices that do not need bridging limits applied */
3896 { "MTRON MSP-SATA*", NULL, ATA_HORKAGE_BRIDGE_OK, },
Jeff Garzik04d0f1b82012-08-17 13:36:59 -04003897 { "BUFFALO HD-QSU2/R5", NULL, ATA_HORKAGE_BRIDGE_OK, },
Jens Axboe9ce8e302008-08-27 15:23:18 +02003898
Tejun Heo90627122009-01-29 20:31:36 +09003899 /* Devices which aren't very happy with higher link speeds */
3900 { "WD My Book", NULL, ATA_HORKAGE_1_5_GBPS, },
Daniel J Bluemanc5310772012-07-23 12:22:37 +08003901 { "Seagate FreeAgent GoFlex", NULL, ATA_HORKAGE_1_5_GBPS, },
Tejun Heo90627122009-01-29 20:31:36 +09003902
Tejun Heod0cb43b2009-07-09 09:27:50 +09003903 /*
3904 * Devices which choke on SETXFER. Applies only if both the
3905 * device and controller are SATA.
3906 */
Tejun Heocd691872011-06-22 12:13:10 +02003907 { "PIONEER DVD-RW DVRTD08", NULL, ATA_HORKAGE_NOSETXFER },
Vladimir LAVALLADE3a251792012-01-08 13:50:13 +01003908 { "PIONEER DVD-RW DVRTD08A", NULL, ATA_HORKAGE_NOSETXFER },
3909 { "PIONEER DVD-RW DVR-215", NULL, ATA_HORKAGE_NOSETXFER },
Tejun Heocd691872011-06-22 12:13:10 +02003910 { "PIONEER DVD-RW DVR-212D", NULL, ATA_HORKAGE_NOSETXFER },
3911 { "PIONEER DVD-RW DVR-216D", NULL, ATA_HORKAGE_NOSETXFER },
Tejun Heod0cb43b2009-07-09 09:27:50 +09003912
Kai-Heng Fengb17e57292018-02-18 22:17:09 +08003913 /* Crucial BX100 SSD 500GB has broken LPM support */
Hans de Goede3bf7b5d2018-03-19 16:33:59 +01003914 { "CT500BX100SSD1", NULL, ATA_HORKAGE_NOLPM },
Kai-Heng Fengb17e57292018-02-18 22:17:09 +08003915
Hans de Goeded418ff52018-03-19 16:34:00 +01003916 /* 512GB MX100 with MU01 firmware has both queued TRIM and LPM issues */
3917 { "Crucial_CT512MX100*", "MU01", ATA_HORKAGE_NO_NCQ_TRIM |
Hans de Goede9c7be59f2018-02-16 10:48:20 +01003918 ATA_HORKAGE_ZERO_AFTER_TRIM |
3919 ATA_HORKAGE_NOLPM, },
Hans de Goeded418ff52018-03-19 16:34:00 +01003920 /* 512GB MX100 with newer firmware has only LPM issues */
3921 { "Crucial_CT512MX100*", NULL, ATA_HORKAGE_ZERO_AFTER_TRIM |
3922 ATA_HORKAGE_NOLPM, },
Hans de Goede9c7be59f2018-02-16 10:48:20 +01003923
Hans de Goede62ac3f72018-03-19 16:33:58 +01003924 /* 480GB+ M500 SSDs have both queued TRIM and LPM issues */
3925 { "Crucial_CT480M500*", NULL, ATA_HORKAGE_NO_NCQ_TRIM |
3926 ATA_HORKAGE_ZERO_AFTER_TRIM |
3927 ATA_HORKAGE_NOLPM, },
3928 { "Crucial_CT960M500*", NULL, ATA_HORKAGE_NO_NCQ_TRIM |
3929 ATA_HORKAGE_ZERO_AFTER_TRIM |
3930 ATA_HORKAGE_NOLPM, },
3931
François Cami76936e92018-05-13 20:11:15 +02003932 /* These specific Samsung models/firmware-revs do not handle LPM well */
Hans de Goedeb5b4d3a2018-04-24 11:19:07 +02003933 { "SAMSUNG MZMPC128HBFU-000MV", "CXM14M1Q", ATA_HORKAGE_NOLPM, },
François Cami76936e92018-05-13 20:11:15 +02003934 { "SAMSUNG SSD PM830 mSATA *", "CXM13D1Q", ATA_HORKAGE_NOLPM, },
Diego Viola410b5c72018-11-12 17:22:52 -02003935 { "SAMSUNG MZ7TD256HAFV-000L9", NULL, ATA_HORKAGE_NOLPM, },
Hans de Goededd957492019-02-03 10:02:07 +01003936 { "SAMSUNG MZ7TE512HMHP-000L1", "EXT06L0Q", ATA_HORKAGE_NOLPM, },
Hans de Goedeb5b4d3a2018-04-24 11:19:07 +02003937
Marc Carinof78dea02013-12-16 18:15:53 -08003938 /* devices that don't properly handle queued TRIM commands */
Sudip Mukherjee136d7692018-05-19 22:29:36 +01003939 { "Micron_M500IT_*", "MU01", ATA_HORKAGE_NO_NCQ_TRIM |
3940 ATA_HORKAGE_ZERO_AFTER_TRIM, },
Martin K. Petersen243918b2015-06-18 14:50:18 -04003941 { "Micron_M500_*", NULL, ATA_HORKAGE_NO_NCQ_TRIM |
Martin K. Petersene61f7d12015-01-08 10:34:27 -05003942 ATA_HORKAGE_ZERO_AFTER_TRIM, },
Martin K. Petersenff7f53f2015-03-27 15:17:20 -04003943 { "Crucial_CT*M500*", NULL, ATA_HORKAGE_NO_NCQ_TRIM |
3944 ATA_HORKAGE_ZERO_AFTER_TRIM, },
Martin K. Petersen9051bd32015-07-15 21:03:23 -04003945 { "Micron_M5[15]0_*", "MU01", ATA_HORKAGE_NO_NCQ_TRIM |
Martin K. Petersenff7f53f2015-03-27 15:17:20 -04003946 ATA_HORKAGE_ZERO_AFTER_TRIM, },
3947 { "Crucial_CT*M550*", "MU01", ATA_HORKAGE_NO_NCQ_TRIM |
3948 ATA_HORKAGE_ZERO_AFTER_TRIM, },
3949 { "Crucial_CT*MX100*", "MU01", ATA_HORKAGE_NO_NCQ_TRIM |
3950 ATA_HORKAGE_ZERO_AFTER_TRIM, },
Ju Hyung Parkca6bfcb2018-03-11 02:28:35 +09003951 { "Samsung SSD 840*", NULL, ATA_HORKAGE_NO_NCQ_TRIM |
3952 ATA_HORKAGE_ZERO_AFTER_TRIM, },
3953 { "Samsung SSD 850*", NULL, ATA_HORKAGE_NO_NCQ_TRIM |
Martin K. Petersen6fc4d972015-03-27 15:17:21 -04003954 ATA_HORKAGE_ZERO_AFTER_TRIM, },
Guillermo A. Amaral7a7184b2015-08-25 23:29:13 -07003955 { "FCCT*M500*", NULL, ATA_HORKAGE_NO_NCQ_TRIM |
3956 ATA_HORKAGE_ZERO_AFTER_TRIM, },
Martin K. Petersene61f7d12015-01-08 10:34:27 -05003957
Arne Fitzenreitercda57b12015-07-15 13:54:37 +02003958 /* devices that don't properly handle TRIM commands */
3959 { "SuperSSpeed S238*", NULL, ATA_HORKAGE_NOTRIM, },
3960
Martin K. Petersene61f7d12015-01-08 10:34:27 -05003961 /*
3962 * As defined, the DRAT (Deterministic Read After Trim) and RZAT
3963 * (Return Zero After Trim) flags in the ATA Command Set are
3964 * unreliable in the sense that they only define what happens if
3965 * the device successfully executed the DSM TRIM command. TRIM
3966 * is only advisory, however, and the device is free to silently
3967 * ignore all or parts of the request.
3968 *
3969 * Whitelist drives that are known to reliably return zeroes
3970 * after TRIM.
3971 */
3972
3973 /*
3974 * The intel 510 drive has buggy DRAT/RZAT. Explicitly exclude
3975 * that model before whitelisting all other intel SSDs.
3976 */
3977 { "INTEL*SSDSC2MH*", NULL, 0, },
3978
Martin K. Petersenff7f53f2015-03-27 15:17:20 -04003979 { "Micron*", NULL, ATA_HORKAGE_ZERO_AFTER_TRIM, },
3980 { "Crucial*", NULL, ATA_HORKAGE_ZERO_AFTER_TRIM, },
Martin K. Petersene61f7d12015-01-08 10:34:27 -05003981 { "INTEL*SSD*", NULL, ATA_HORKAGE_ZERO_AFTER_TRIM, },
3982 { "SSD*INTEL*", NULL, ATA_HORKAGE_ZERO_AFTER_TRIM, },
3983 { "Samsung*SSD*", NULL, ATA_HORKAGE_ZERO_AFTER_TRIM, },
3984 { "SAMSUNG*SSD*", NULL, ATA_HORKAGE_ZERO_AFTER_TRIM, },
Juha-Matti Tillifd6f32f2018-12-02 12:47:08 +02003985 { "SAMSUNG*MZ7KM*", NULL, ATA_HORKAGE_ZERO_AFTER_TRIM, },
Martin K. Petersene61f7d12015-01-08 10:34:27 -05003986 { "ST[1248][0248]0[FH]*", NULL, ATA_HORKAGE_ZERO_AFTER_TRIM, },
Marc Carinof78dea02013-12-16 18:15:53 -08003987
Tejun Heoecd75ad2014-01-16 09:47:17 -05003988 /*
3989 * Some WD SATA-I drives spin up and down erratically when the link
3990 * is put into the slumber mode. We don't have full list of the
3991 * affected devices. Disable LPM if the device matches one of the
3992 * known prefixes and is SATA-1. As a side effect LPM partial is
3993 * lost too.
3994 *
3995 * https://bugzilla.kernel.org/show_bug.cgi?id=57211
3996 */
3997 { "WDC WD800JD-*", NULL, ATA_HORKAGE_WD_BROKEN_LPM },
3998 { "WDC WD1200JD-*", NULL, ATA_HORKAGE_WD_BROKEN_LPM },
3999 { "WDC WD1600JD-*", NULL, ATA_HORKAGE_WD_BROKEN_LPM },
4000 { "WDC WD2000JD-*", NULL, ATA_HORKAGE_WD_BROKEN_LPM },
4001 { "WDC WD2500JD-*", NULL, ATA_HORKAGE_WD_BROKEN_LPM },
4002 { "WDC WD3000JD-*", NULL, ATA_HORKAGE_WD_BROKEN_LPM },
4003 { "WDC WD3200JD-*", NULL, ATA_HORKAGE_WD_BROKEN_LPM },
4004
Alan Cox6919a0a2006-10-27 19:08:46 -07004005 /* End Marker */
4006 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004007};
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05004008
Tejun Heo75683fe2007-07-05 13:31:27 +09004009static unsigned long ata_dev_blacklisted(const struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004010{
Tejun Heo8bfa79f2007-01-02 20:19:40 +09004011 unsigned char model_num[ATA_ID_PROD_LEN + 1];
4012 unsigned char model_rev[ATA_ID_FW_REV_LEN + 1];
Alan Cox6919a0a2006-10-27 19:08:46 -07004013 const struct ata_blacklist_entry *ad = ata_device_blacklist;
Albert Lee3a778272006-06-22 13:00:25 +08004014
Tejun Heo8bfa79f2007-01-02 20:19:40 +09004015 ata_id_c_string(dev->id, model_num, ATA_ID_PROD, sizeof(model_num));
4016 ata_id_c_string(dev->id, model_rev, ATA_ID_FW_REV, sizeof(model_rev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004017
Alan Cox6919a0a2006-10-27 19:08:46 -07004018 while (ad->model_num) {
George Spelvin1c402792014-10-07 07:26:38 -04004019 if (glob_match(ad->model_num, model_num)) {
Alan Cox6919a0a2006-10-27 19:08:46 -07004020 if (ad->model_rev == NULL)
4021 return ad->horkage;
George Spelvin1c402792014-10-07 07:26:38 -04004022 if (glob_match(ad->model_rev, model_rev))
Alan Cox6919a0a2006-10-27 19:08:46 -07004023 return ad->horkage;
Alan Coxf4b15fe2006-03-22 15:54:04 +00004024 }
Alan Cox6919a0a2006-10-27 19:08:46 -07004025 ad++;
Alan Coxf4b15fe2006-03-22 15:54:04 +00004026 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004027 return 0;
4028}
4029
Alan Cox6919a0a2006-10-27 19:08:46 -07004030static int ata_dma_blacklisted(const struct ata_device *dev)
4031{
4032 /* We don't support polling DMA.
4033 * DMA blacklist those ATAPI devices with CDB-intr (and use PIO)
4034 * if the LLDD handles only interrupts in the HSM_ST_LAST state.
4035 */
Tejun Heo9af5c9c2007-08-06 18:36:22 +09004036 if ((dev->link->ap->flags & ATA_FLAG_PIO_POLLING) &&
Alan Cox6919a0a2006-10-27 19:08:46 -07004037 (dev->flags & ATA_DFLAG_CDB_INTR))
4038 return 1;
Tejun Heo75683fe2007-07-05 13:31:27 +09004039 return (dev->horkage & ATA_HORKAGE_NODMA) ? 1 : 0;
Alan Cox6919a0a2006-10-27 19:08:46 -07004040}
4041
Tejun Heoa6d5a512006-03-06 04:31:57 +09004042/**
Alan Cox6bbfd532007-11-05 22:58:58 +00004043 * ata_is_40wire - check drive side detection
4044 * @dev: device
4045 *
4046 * Perform drive side detection decoding, allowing for device vendors
4047 * who can't follow the documentation.
4048 */
4049
4050static int ata_is_40wire(struct ata_device *dev)
4051{
4052 if (dev->horkage & ATA_HORKAGE_IVB)
4053 return ata_drive_40wire_relaxed(dev->id);
4054 return ata_drive_40wire(dev->id);
4055}
4056
4057/**
Alan Cox15a55512008-03-28 14:33:46 -07004058 * cable_is_40wire - 40/80/SATA decider
4059 * @ap: port to consider
4060 *
4061 * This function encapsulates the policy for speed management
4062 * in one place. At the moment we don't cache the result but
4063 * there is a good case for setting ap->cbl to the result when
4064 * we are called with unknown cables (and figuring out if it
4065 * impacts hotplug at all).
4066 *
4067 * Return 1 if the cable appears to be 40 wire.
4068 */
4069
4070static int cable_is_40wire(struct ata_port *ap)
4071{
4072 struct ata_link *link;
4073 struct ata_device *dev;
4074
Tejun Heo4a9c7b32008-10-27 19:59:23 +09004075 /* If the controller thinks we are 40 wire, we are. */
Alan Cox15a55512008-03-28 14:33:46 -07004076 if (ap->cbl == ATA_CBL_PATA40)
4077 return 1;
Tejun Heo4a9c7b32008-10-27 19:59:23 +09004078
4079 /* If the controller thinks we are 80 wire, we are. */
Alan Cox15a55512008-03-28 14:33:46 -07004080 if (ap->cbl == ATA_CBL_PATA80 || ap->cbl == ATA_CBL_SATA)
4081 return 0;
Tejun Heo4a9c7b32008-10-27 19:59:23 +09004082
4083 /* If the system is known to be 40 wire short cable (eg
4084 * laptop), then we allow 80 wire modes even if the drive
4085 * isn't sure.
4086 */
Alan Coxf7920682008-04-08 16:37:50 +01004087 if (ap->cbl == ATA_CBL_PATA40_SHORT)
4088 return 0;
Alan Cox15a55512008-03-28 14:33:46 -07004089
Tejun Heo4a9c7b32008-10-27 19:59:23 +09004090 /* If the controller doesn't know, we scan.
4091 *
4092 * Note: We look for all 40 wire detects at this point. Any
4093 * 80 wire detect is taken to be 80 wire cable because
4094 * - in many setups only the one drive (slave if present) will
4095 * give a valid detect
4096 * - if you have a non detect capable drive you don't want it
4097 * to colour the choice
4098 */
Tejun Heo1eca4362008-11-03 20:03:17 +09004099 ata_for_each_link(link, ap, EDGE) {
4100 ata_for_each_dev(dev, link, ENABLED) {
4101 if (!ata_is_40wire(dev))
Alan Cox15a55512008-03-28 14:33:46 -07004102 return 0;
4103 }
4104 }
4105 return 1;
4106}
4107
4108/**
Tejun Heoa6d5a512006-03-06 04:31:57 +09004109 * ata_dev_xfermask - Compute supported xfermask of the given device
Tejun Heoa6d5a512006-03-06 04:31:57 +09004110 * @dev: Device to compute xfermask for
4111 *
Tejun Heoacf356b2006-03-24 14:07:50 +09004112 * Compute supported xfermask of @dev and store it in
4113 * dev->*_mask. This function is responsible for applying all
4114 * known limits including host controller limits, device
4115 * blacklist, etc...
Tejun Heoa6d5a512006-03-06 04:31:57 +09004116 *
4117 * LOCKING:
4118 * None.
Tejun Heoa6d5a512006-03-06 04:31:57 +09004119 */
Tejun Heo3373efd2006-05-15 20:57:53 +09004120static void ata_dev_xfermask(struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004121{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09004122 struct ata_link *link = dev->link;
4123 struct ata_port *ap = link->ap;
Jeff Garzikcca39742006-08-24 03:19:22 -04004124 struct ata_host *host = ap->host;
Tejun Heoa6d5a512006-03-06 04:31:57 +09004125 unsigned long xfer_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004126
Tejun Heo37deecb2006-08-10 16:59:07 +09004127 /* controller modes available */
Tejun Heo565083e2006-04-02 17:54:47 +09004128 xfer_mask = ata_pack_xfermask(ap->pio_mask,
4129 ap->mwdma_mask, ap->udma_mask);
4130
Robert Hancock8343f882007-03-06 02:37:51 -08004131 /* drive modes available */
Tejun Heo37deecb2006-08-10 16:59:07 +09004132 xfer_mask &= ata_pack_xfermask(dev->pio_mask,
4133 dev->mwdma_mask, dev->udma_mask);
4134 xfer_mask &= ata_id_xfermask(dev->id);
Tejun Heo565083e2006-04-02 17:54:47 +09004135
Alan Coxb352e572006-08-10 18:52:12 +01004136 /*
4137 * CFA Advanced TrueIDE timings are not allowed on a shared
4138 * cable
4139 */
4140 if (ata_dev_pair(dev)) {
4141 /* No PIO5 or PIO6 */
4142 xfer_mask &= ~(0x03 << (ATA_SHIFT_PIO + 5));
4143 /* No MWDMA3 or MWDMA 4 */
4144 xfer_mask &= ~(0x03 << (ATA_SHIFT_MWDMA + 3));
4145 }
4146
Tejun Heo37deecb2006-08-10 16:59:07 +09004147 if (ata_dma_blacklisted(dev)) {
4148 xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
Joe Perchesa9a79df2011-04-15 15:51:59 -07004149 ata_dev_warn(dev,
4150 "device is on DMA blacklist, disabling DMA\n");
Tejun Heo37deecb2006-08-10 16:59:07 +09004151 }
Tejun Heoa6d5a512006-03-06 04:31:57 +09004152
Petr Vandrovec14d66ab2007-03-08 10:12:12 +01004153 if ((host->flags & ATA_HOST_SIMPLEX) &&
Jeff Garzik2dcb4072007-10-19 06:42:56 -04004154 host->simplex_claimed && host->simplex_claimed != ap) {
Tejun Heo37deecb2006-08-10 16:59:07 +09004155 xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
Joe Perchesa9a79df2011-04-15 15:51:59 -07004156 ata_dev_warn(dev,
4157 "simplex DMA is claimed by other device, disabling DMA\n");
Alan Cox5444a6f2006-03-27 18:58:20 +01004158 }
Tejun Heo565083e2006-04-02 17:54:47 +09004159
Jeff Garzike4246752007-03-09 09:56:46 -05004160 if (ap->flags & ATA_FLAG_NO_IORDY)
4161 xfer_mask &= ata_pio_mask_no_iordy(dev);
4162
Alan Cox5444a6f2006-03-27 18:58:20 +01004163 if (ap->ops->mode_filter)
Alan Coxa76b62ca2007-03-09 09:34:07 -05004164 xfer_mask = ap->ops->mode_filter(dev, xfer_mask);
Alan Cox5444a6f2006-03-27 18:58:20 +01004165
Robert Hancock8343f882007-03-06 02:37:51 -08004166 /* Apply cable rule here. Don't apply it early because when
4167 * we handle hot plug the cable type can itself change.
4168 * Check this last so that we know if the transfer rate was
4169 * solely limited by the cable.
4170 * Unknown or 80 wire cables reported host side are checked
4171 * drive side as well. Cases where we know a 40wire cable
4172 * is used safely for 80 are not checked here.
4173 */
4174 if (xfer_mask & (0xF8 << ATA_SHIFT_UDMA))
4175 /* UDMA/44 or higher would be available */
Alan Cox15a55512008-03-28 14:33:46 -07004176 if (cable_is_40wire(ap)) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07004177 ata_dev_warn(dev,
4178 "limited to UDMA/33 due to 40-wire cable\n");
Robert Hancock8343f882007-03-06 02:37:51 -08004179 xfer_mask &= ~(0xF8 << ATA_SHIFT_UDMA);
4180 }
4181
Tejun Heo565083e2006-04-02 17:54:47 +09004182 ata_unpack_xfermask(xfer_mask, &dev->pio_mask,
4183 &dev->mwdma_mask, &dev->udma_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004184}
4185
Linus Torvalds1da177e2005-04-16 15:20:36 -07004186/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004187 * ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
Linus Torvalds1da177e2005-04-16 15:20:36 -07004188 * @dev: Device to which command will be sent
4189 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04004190 * Issue SET FEATURES - XFER MODE command to device @dev
4191 * on port @ap.
4192 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004193 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004194 * PCI/etc. bus probe sem.
Tejun Heo83206a22006-03-24 15:25:31 +09004195 *
4196 * RETURNS:
4197 * 0 on success, AC_ERR_* mask otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004198 */
4199
Tejun Heo3373efd2006-05-15 20:57:53 +09004200static unsigned int ata_dev_set_xfermode(struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004201{
Tejun Heoa0123702005-12-13 14:49:31 +09004202 struct ata_taskfile tf;
Tejun Heo83206a22006-03-24 15:25:31 +09004203 unsigned int err_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004204
4205 /* set up set-features taskfile */
4206 DPRINTK("set features - xfer mode\n");
4207
Tejun Heo464cf172007-05-27 15:10:40 +02004208 /* Some controllers and ATAPI devices show flaky interrupt
4209 * behavior after setting xfer mode. Use polling instead.
4210 */
Tejun Heo3373efd2006-05-15 20:57:53 +09004211 ata_tf_init(dev, &tf);
Tejun Heoa0123702005-12-13 14:49:31 +09004212 tf.command = ATA_CMD_SET_FEATURES;
4213 tf.feature = SETFEATURES_XFER;
Tejun Heo464cf172007-05-27 15:10:40 +02004214 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE | ATA_TFLAG_POLLING;
Tejun Heoa0123702005-12-13 14:49:31 +09004215 tf.protocol = ATA_PROT_NODATA;
Alan Coxb9f8ab22007-11-19 14:33:11 +00004216 /* If we are using IORDY we must send the mode setting command */
Jeff Garzik11b7bec2007-11-23 21:12:14 -05004217 if (ata_pio_need_iordy(dev))
4218 tf.nsect = dev->xfer_mode;
Alan Coxb9f8ab22007-11-19 14:33:11 +00004219 /* If the device has IORDY and the controller does not - turn it off */
4220 else if (ata_id_has_iordy(dev->id))
Jeff Garzik11b7bec2007-11-23 21:12:14 -05004221 tf.nsect = 0x01;
Alan Coxb9f8ab22007-11-19 14:33:11 +00004222 else /* In the ancient relic department - skip all of this */
4223 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004224
Mikulas Patockad531be22015-07-08 13:06:12 -04004225 /* On some disks, this command causes spin-up, so we need longer timeout */
4226 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 15000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004227
Tejun Heo83206a22006-03-24 15:25:31 +09004228 DPRINTK("EXIT, err_mask=%x\n", err_mask);
4229 return err_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004230}
Tejun Heo1152b262010-09-01 17:50:05 +02004231
Linus Torvalds1da177e2005-04-16 15:20:36 -07004232/**
Jeff Garzik218f3d32007-10-25 00:33:27 -04004233 * ata_dev_set_feature - Issue SET FEATURES - SATA FEATURES
Kristen Carlson Accardi9f45cbd2007-08-15 03:57:11 -04004234 * @dev: Device to which command will be sent
4235 * @enable: Whether to enable or disable the feature
Jeff Garzik218f3d32007-10-25 00:33:27 -04004236 * @feature: The sector count represents the feature to set
Kristen Carlson Accardi9f45cbd2007-08-15 03:57:11 -04004237 *
4238 * Issue SET FEATURES - SATA FEATURES command to device @dev
Jeff Garzik218f3d32007-10-25 00:33:27 -04004239 * on port @ap with sector count
Kristen Carlson Accardi9f45cbd2007-08-15 03:57:11 -04004240 *
4241 * LOCKING:
4242 * PCI/etc. bus probe sem.
4243 *
4244 * RETURNS:
4245 * 0 on success, AC_ERR_* mask otherwise.
4246 */
Tejun Heo1152b262010-09-01 17:50:05 +02004247unsigned int ata_dev_set_feature(struct ata_device *dev, u8 enable, u8 feature)
Kristen Carlson Accardi9f45cbd2007-08-15 03:57:11 -04004248{
4249 struct ata_taskfile tf;
4250 unsigned int err_mask;
Damien Le Moal974e0a42016-04-04 12:17:09 -04004251 unsigned long timeout = 0;
Kristen Carlson Accardi9f45cbd2007-08-15 03:57:11 -04004252
4253 /* set up set-features taskfile */
4254 DPRINTK("set features - SATA features\n");
4255
4256 ata_tf_init(dev, &tf);
4257 tf.command = ATA_CMD_SET_FEATURES;
4258 tf.feature = enable;
4259 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
4260 tf.protocol = ATA_PROT_NODATA;
Jeff Garzik218f3d32007-10-25 00:33:27 -04004261 tf.nsect = feature;
Kristen Carlson Accardi9f45cbd2007-08-15 03:57:11 -04004262
Damien Le Moal974e0a42016-04-04 12:17:09 -04004263 if (enable == SETFEATURES_SPINUP)
4264 timeout = ata_probe_timeout ?
4265 ata_probe_timeout * 1000 : SETFEATURES_SPINUP_TIMEOUT;
4266 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, timeout);
Kristen Carlson Accardi9f45cbd2007-08-15 03:57:11 -04004267
4268 DPRINTK("EXIT, err_mask=%x\n", err_mask);
4269 return err_mask;
4270}
Jeff Garzik633de4c2012-09-13 01:12:29 -04004271EXPORT_SYMBOL_GPL(ata_dev_set_feature);
Kristen Carlson Accardi9f45cbd2007-08-15 03:57:11 -04004272
4273/**
Albert Lee8bf62ece2005-05-12 15:29:42 -04004274 * ata_dev_init_params - Issue INIT DEV PARAMS command
Albert Lee8bf62ece2005-05-12 15:29:42 -04004275 * @dev: Device to which command will be sent
Randy Dunlape2a7f772006-05-18 10:50:18 -07004276 * @heads: Number of heads (taskfile parameter)
4277 * @sectors: Number of sectors (taskfile parameter)
Albert Lee8bf62ece2005-05-12 15:29:42 -04004278 *
4279 * LOCKING:
Tejun Heo6aff8f12006-02-15 18:24:09 +09004280 * Kernel thread context (may sleep)
4281 *
4282 * RETURNS:
4283 * 0 on success, AC_ERR_* mask otherwise.
Albert Lee8bf62ece2005-05-12 15:29:42 -04004284 */
Tejun Heo3373efd2006-05-15 20:57:53 +09004285static unsigned int ata_dev_init_params(struct ata_device *dev,
4286 u16 heads, u16 sectors)
Albert Lee8bf62ece2005-05-12 15:29:42 -04004287{
Tejun Heoa0123702005-12-13 14:49:31 +09004288 struct ata_taskfile tf;
Tejun Heo6aff8f12006-02-15 18:24:09 +09004289 unsigned int err_mask;
Albert Lee8bf62ece2005-05-12 15:29:42 -04004290
4291 /* Number of sectors per track 1-255. Number of heads 1-16 */
4292 if (sectors < 1 || sectors > 255 || heads < 1 || heads > 16)
Albert Lee00b6f5e2006-03-27 16:39:18 +08004293 return AC_ERR_INVALID;
Albert Lee8bf62ece2005-05-12 15:29:42 -04004294
4295 /* set up init dev params taskfile */
4296 DPRINTK("init dev params \n");
4297
Tejun Heo3373efd2006-05-15 20:57:53 +09004298 ata_tf_init(dev, &tf);
Tejun Heoa0123702005-12-13 14:49:31 +09004299 tf.command = ATA_CMD_INIT_DEV_PARAMS;
4300 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
4301 tf.protocol = ATA_PROT_NODATA;
4302 tf.nsect = sectors;
4303 tf.device |= (heads - 1) & 0x0f; /* max head = num. of heads - 1 */
Albert Lee8bf62ece2005-05-12 15:29:42 -04004304
Tejun Heo2b789102007-10-09 15:05:44 +09004305 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
Alan Cox18b24662007-08-08 14:28:49 +01004306 /* A clean abort indicates an original or just out of spec drive
4307 and we should continue as we issue the setup based on the
4308 drive reported working geometry */
4309 if (err_mask == AC_ERR_DEV && (tf.feature & ATA_ABORTED))
4310 err_mask = 0;
Albert Lee8bf62ece2005-05-12 15:29:42 -04004311
Tejun Heo6aff8f12006-02-15 18:24:09 +09004312 DPRINTK("EXIT, err_mask=%x\n", err_mask);
4313 return err_mask;
Albert Lee8bf62ece2005-05-12 15:29:42 -04004314}
4315
4316/**
Tejun Heo5895ef92008-06-17 12:36:26 +09004317 * atapi_check_dma - Check whether ATAPI DMA can be supported
Linus Torvalds1da177e2005-04-16 15:20:36 -07004318 * @qc: Metadata associated with taskfile to check
4319 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04004320 * Allow low-level driver to filter ATA PACKET commands, returning
4321 * a status indicating whether or not it is OK to use DMA for the
4322 * supplied PACKET command.
4323 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004324 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04004325 * spin_lock_irqsave(host lock)
Jeff Garzik0cba6322005-05-30 19:49:12 -04004326 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004327 * RETURNS: 0 when ATAPI DMA can be used
4328 * nonzero otherwise
4329 */
Tejun Heo5895ef92008-06-17 12:36:26 +09004330int atapi_check_dma(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004331{
4332 struct ata_port *ap = qc->ap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004333
Tejun Heob9a41972007-06-27 02:48:43 +09004334 /* Don't allow DMA if it isn't multiple of 16 bytes. Quite a
4335 * few ATAPI devices choke on such DMA requests.
4336 */
Tejun Heo6a87e422008-11-03 19:01:09 +09004337 if (!(qc->dev->horkage & ATA_HORKAGE_ATAPI_MOD16_DMA) &&
4338 unlikely(qc->nbytes & 15))
Tejun Heob9a41972007-06-27 02:48:43 +09004339 return 1;
Albert Lee6f23a312007-04-02 11:39:25 +08004340
Linus Torvalds1da177e2005-04-16 15:20:36 -07004341 if (ap->ops->check_atapi_dma)
Tejun Heob9a41972007-06-27 02:48:43 +09004342 return ap->ops->check_atapi_dma(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004343
Tejun Heob9a41972007-06-27 02:48:43 +09004344 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004345}
Tejun Heob9a41972007-06-27 02:48:43 +09004346
Linus Torvalds1da177e2005-04-16 15:20:36 -07004347/**
Tejun Heo31cc23b2007-09-23 13:14:12 +09004348 * ata_std_qc_defer - Check whether a qc needs to be deferred
4349 * @qc: ATA command in question
4350 *
4351 * Non-NCQ commands cannot run with any other command, NCQ or
4352 * not. As upper layer only knows the queue depth, we are
4353 * responsible for maintaining exclusion. This function checks
4354 * whether a new command @qc can be issued.
4355 *
4356 * LOCKING:
4357 * spin_lock_irqsave(host lock)
4358 *
4359 * RETURNS:
4360 * ATA_DEFER_* if deferring is needed, 0 otherwise.
4361 */
4362int ata_std_qc_defer(struct ata_queued_cmd *qc)
4363{
4364 struct ata_link *link = qc->dev->link;
4365
Hannes Reinecke179b3102016-07-14 09:05:43 +09004366 if (ata_is_ncq(qc->tf.protocol)) {
Tejun Heo31cc23b2007-09-23 13:14:12 +09004367 if (!ata_tag_valid(link->active_tag))
4368 return 0;
4369 } else {
4370 if (!ata_tag_valid(link->active_tag) && !link->sactive)
4371 return 0;
4372 }
4373
4374 return ATA_DEFER_LINK;
4375}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01004376EXPORT_SYMBOL_GPL(ata_std_qc_defer);
Tejun Heo31cc23b2007-09-23 13:14:12 +09004377
Jiri Slaby95364f32019-10-31 10:59:45 +01004378enum ata_completion_errors ata_noop_qc_prep(struct ata_queued_cmd *qc)
4379{
4380 return AC_ERR_OK;
4381}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01004382EXPORT_SYMBOL_GPL(ata_noop_qc_prep);
Brian Kinge46834c2006-03-17 17:04:03 -06004383
Jeff Garzik0cba6322005-05-30 19:49:12 -04004384/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04004385 * ata_sg_init - Associate command with scatter-gather table.
4386 * @qc: Command to be associated
4387 * @sg: Scatter-gather table.
4388 * @n_elem: Number of elements in s/g table.
4389 *
4390 * Initialize the data-related elements of queued_cmd @qc
4391 * to point to a scatter-gather table @sg, containing @n_elem
4392 * elements.
4393 *
4394 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04004395 * spin_lock_irqsave(host lock)
Jeff Garzik0cba6322005-05-30 19:49:12 -04004396 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004397void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
4398 unsigned int n_elem)
4399{
Tejun Heoff2aeb12007-12-05 16:43:11 +09004400 qc->sg = sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004401 qc->n_elem = n_elem;
Tejun Heoff2aeb12007-12-05 16:43:11 +09004402 qc->cursg = qc->sg;
4403}
4404
Geert Uytterhoeven2874d5e2017-01-03 19:09:49 +01004405#ifdef CONFIG_HAS_DMA
4406
4407/**
4408 * ata_sg_clean - Unmap DMA memory associated with command
4409 * @qc: Command containing DMA memory to be released
4410 *
4411 * Unmap all mapped DMA memory associated with this command.
4412 *
4413 * LOCKING:
4414 * spin_lock_irqsave(host lock)
4415 */
Jason Yanaf27e012017-03-10 10:05:40 +08004416static void ata_sg_clean(struct ata_queued_cmd *qc)
Geert Uytterhoeven2874d5e2017-01-03 19:09:49 +01004417{
4418 struct ata_port *ap = qc->ap;
4419 struct scatterlist *sg = qc->sg;
4420 int dir = qc->dma_dir;
4421
4422 WARN_ON_ONCE(sg == NULL);
4423
4424 VPRINTK("unmapping %u sg elements\n", qc->n_elem);
4425
4426 if (qc->n_elem)
4427 dma_unmap_sg(ap->dev, sg, qc->orig_n_elem, dir);
4428
4429 qc->flags &= ~ATA_QCFLAG_DMAMAP;
4430 qc->sg = NULL;
4431}
4432
Linus Torvalds1da177e2005-04-16 15:20:36 -07004433/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04004434 * ata_sg_setup - DMA-map the scatter-gather table associated with a command.
4435 * @qc: Command with scatter-gather table to be mapped.
4436 *
4437 * DMA-map the scatter-gather table associated with queued_cmd @qc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004438 *
4439 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04004440 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004441 *
4442 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004443 * Zero on success, negative on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004444 *
4445 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004446static int ata_sg_setup(struct ata_queued_cmd *qc)
4447{
4448 struct ata_port *ap = qc->ap;
James Bottomleydde20202008-02-19 11:36:56 +01004449 unsigned int n_elem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004450
Tejun Heo44877b42007-02-21 01:06:51 +09004451 VPRINTK("ENTER, ata%u\n", ap->print_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004452
James Bottomleydde20202008-02-19 11:36:56 +01004453 n_elem = dma_map_sg(ap->dev, qc->sg, qc->n_elem, qc->dma_dir);
4454 if (n_elem < 1)
4455 return -1;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004456
Paul Menzelbb376672018-07-08 09:18:21 +02004457 VPRINTK("%d sg elements mapped\n", n_elem);
FUJITA Tomonori58256272009-02-27 17:35:43 +09004458 qc->orig_n_elem = qc->n_elem;
James Bottomleydde20202008-02-19 11:36:56 +01004459 qc->n_elem = n_elem;
Tejun Heof92a2632007-12-05 16:43:10 +09004460 qc->flags |= ATA_QCFLAG_DMAMAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004461
4462 return 0;
4463}
4464
Geert Uytterhoeven2874d5e2017-01-03 19:09:49 +01004465#else /* !CONFIG_HAS_DMA */
4466
4467static inline void ata_sg_clean(struct ata_queued_cmd *qc) {}
4468static inline int ata_sg_setup(struct ata_queued_cmd *qc) { return -1; }
4469
4470#endif /* !CONFIG_HAS_DMA */
4471
Linus Torvalds1da177e2005-04-16 15:20:36 -07004472/**
Randy Dunlapc893a3a2006-01-28 13:15:32 -05004473 * swap_buf_le16 - swap halves of 16-bit words in place
Edward Falk0baab862005-06-02 18:17:13 -04004474 * @buf: Buffer to swap
4475 * @buf_words: Number of 16-bit words in buffer.
4476 *
4477 * Swap halves of 16-bit words if needed to convert from
4478 * little-endian byte order to native cpu byte order, or
4479 * vice-versa.
4480 *
4481 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04004482 * Inherited from caller.
Edward Falk0baab862005-06-02 18:17:13 -04004483 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004484void swap_buf_le16(u16 *buf, unsigned int buf_words)
4485{
4486#ifdef __BIG_ENDIAN
4487 unsigned int i;
4488
4489 for (i = 0; i < buf_words; i++)
4490 buf[i] = le16_to_cpu(buf[i]);
4491#endif /* __BIG_ENDIAN */
4492}
4493
Albert Lee6ae4cfb2005-08-12 14:15:34 +08004494/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004495 * ata_qc_new_init - Request an available ATA command, and initialize it
Linus Torvalds1da177e2005-04-16 15:20:36 -07004496 * @dev: Device from whom we request an available command structure
Randy Dunlap38755e82015-08-09 18:37:36 -07004497 * @tag: tag
Linus Torvalds1da177e2005-04-16 15:20:36 -07004498 *
4499 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004500 * None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004501 */
4502
Shaohua Li98bd4be2015-01-23 19:52:07 -08004503struct ata_queued_cmd *ata_qc_new_init(struct ata_device *dev, int tag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004504{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09004505 struct ata_port *ap = dev->link->ap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004506 struct ata_queued_cmd *qc;
4507
Shaohua Li98bd4be2015-01-23 19:52:07 -08004508 /* no command while frozen */
4509 if (unlikely(ap->pflags & ATA_PFLAG_FROZEN))
4510 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004511
Shaohua Li98bd4be2015-01-23 19:52:07 -08004512 /* libsas case */
Shaohua Li5067c042015-03-12 10:32:18 -07004513 if (ap->flags & ATA_FLAG_SAS_HOST) {
Shaohua Li98bd4be2015-01-23 19:52:07 -08004514 tag = ata_sas_allocate_tag(ap);
4515 if (tag < 0)
4516 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004517 }
4518
Shaohua Li98bd4be2015-01-23 19:52:07 -08004519 qc = __ata_qc_from_tag(ap, tag);
Jens Axboe5ac40792018-05-11 12:51:03 -06004520 qc->tag = qc->hw_tag = tag;
Shaohua Li98bd4be2015-01-23 19:52:07 -08004521 qc->scsicmd = NULL;
4522 qc->ap = ap;
4523 qc->dev = dev;
4524
4525 ata_qc_reinit(qc);
4526
Linus Torvalds1da177e2005-04-16 15:20:36 -07004527 return qc;
4528}
4529
Tejun Heo8a8bc222008-11-10 14:48:21 +09004530/**
4531 * ata_qc_free - free unused ata_queued_cmd
4532 * @qc: Command to complete
4533 *
4534 * Designed to free unused ata_queued_cmd object
4535 * in case something prevents using it.
4536 *
4537 * LOCKING:
4538 * spin_lock_irqsave(host lock)
4539 */
4540void ata_qc_free(struct ata_queued_cmd *qc)
4541{
Julia Lawalla1104012009-10-17 08:41:47 +02004542 struct ata_port *ap;
Tejun Heo8a8bc222008-11-10 14:48:21 +09004543 unsigned int tag;
4544
Tejun Heoefcb3cf2009-01-09 19:19:14 +09004545 WARN_ON_ONCE(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
Julia Lawalla1104012009-10-17 08:41:47 +02004546 ap = qc->ap;
Tejun Heo8a8bc222008-11-10 14:48:21 +09004547
4548 qc->flags = 0;
4549 tag = qc->tag;
Jens Axboe28361c42018-05-11 12:51:09 -06004550 if (ata_tag_valid(tag)) {
Tejun Heo8a8bc222008-11-10 14:48:21 +09004551 qc->tag = ATA_TAG_POISON;
Shaohua Li5067c042015-03-12 10:32:18 -07004552 if (ap->flags & ATA_FLAG_SAS_HOST)
Shaohua Li98bd4be2015-01-23 19:52:07 -08004553 ata_sas_free_tag(tag, ap);
Tejun Heo8a8bc222008-11-10 14:48:21 +09004554 }
4555}
4556
Tejun Heo76014422006-02-11 15:13:49 +09004557void __ata_qc_complete(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004558{
Julia Lawalla1104012009-10-17 08:41:47 +02004559 struct ata_port *ap;
4560 struct ata_link *link;
Tejun Heodedaf2b2006-05-15 21:03:43 +09004561
Tejun Heoefcb3cf2009-01-09 19:19:14 +09004562 WARN_ON_ONCE(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
4563 WARN_ON_ONCE(!(qc->flags & ATA_QCFLAG_ACTIVE));
Julia Lawalla1104012009-10-17 08:41:47 +02004564 ap = qc->ap;
4565 link = qc->dev->link;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004566
4567 if (likely(qc->flags & ATA_QCFLAG_DMAMAP))
4568 ata_sg_clean(qc);
4569
Tejun Heo7401abf2006-05-15 20:57:32 +09004570 /* command should be marked inactive atomically with qc completion */
Hannes Reinecke179b3102016-07-14 09:05:43 +09004571 if (ata_is_ncq(qc->tf.protocol)) {
Jens Axboe4e5b6262018-05-11 12:51:04 -06004572 link->sactive &= ~(1 << qc->hw_tag);
Tejun Heoda917d62007-09-23 13:14:12 +09004573 if (!link->sactive)
4574 ap->nr_active_links--;
4575 } else {
Tejun Heo9af5c9c2007-08-06 18:36:22 +09004576 link->active_tag = ATA_TAG_POISON;
Tejun Heoda917d62007-09-23 13:14:12 +09004577 ap->nr_active_links--;
4578 }
4579
4580 /* clear exclusive status */
4581 if (unlikely(qc->flags & ATA_QCFLAG_CLEAR_EXCL &&
4582 ap->excl_link == link))
4583 ap->excl_link = NULL;
Tejun Heo7401abf2006-05-15 20:57:32 +09004584
Albert Lee3f3791d2005-08-16 14:25:38 +08004585 /* atapi: mark qc as inactive to prevent the interrupt handler
4586 * from completing the command twice later, before the error handler
4587 * is called. (when rc != 0 and atapi request sense is needed)
4588 */
4589 qc->flags &= ~ATA_QCFLAG_ACTIVE;
Jens Axboee3ed89392018-05-11 12:51:05 -06004590 ap->qc_active &= ~(1ULL << qc->tag);
Albert Lee3f3791d2005-08-16 14:25:38 +08004591
Linus Torvalds1da177e2005-04-16 15:20:36 -07004592 /* call completion callback */
Tejun Heo77853bf2006-01-23 13:09:36 +09004593 qc->complete_fn(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004594}
4595
Tejun Heo39599a52006-11-14 22:37:35 +09004596static void fill_result_tf(struct ata_queued_cmd *qc)
4597{
4598 struct ata_port *ap = qc->ap;
4599
Tejun Heo39599a52006-11-14 22:37:35 +09004600 qc->result_tf.flags = qc->tf.flags;
Tejun Heo22183bf2008-04-07 22:47:20 +09004601 ap->ops->qc_fill_rtf(qc);
Tejun Heo39599a52006-11-14 22:37:35 +09004602}
4603
Tejun Heo00115e02007-11-27 19:28:58 +09004604static void ata_verify_xfer(struct ata_queued_cmd *qc)
4605{
4606 struct ata_device *dev = qc->dev;
4607
Christoph Hellwigeb0effd2016-07-16 22:16:41 +09004608 if (!ata_is_data(qc->tf.protocol))
Tejun Heo00115e02007-11-27 19:28:58 +09004609 return;
4610
4611 if ((dev->mwdma_mask || dev->udma_mask) && ata_is_pio(qc->tf.protocol))
4612 return;
4613
4614 dev->flags &= ~ATA_DFLAG_DUBIOUS_XFER;
4615}
4616
Tejun Heof686bcb2006-05-15 20:58:05 +09004617/**
4618 * ata_qc_complete - Complete an active ATA command
4619 * @qc: Command to complete
Tejun Heof686bcb2006-05-15 20:58:05 +09004620 *
Tejun Heo1aadf5c2010-06-25 15:03:34 +02004621 * Indicate to the mid and upper layers that an ATA command has
4622 * completed, with either an ok or not-ok status.
4623 *
4624 * Refrain from calling this function multiple times when
4625 * successfully completing multiple NCQ commands.
4626 * ata_qc_complete_multiple() should be used instead, which will
4627 * properly update IRQ expect state.
Tejun Heof686bcb2006-05-15 20:58:05 +09004628 *
4629 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04004630 * spin_lock_irqsave(host lock)
Tejun Heof686bcb2006-05-15 20:58:05 +09004631 */
4632void ata_qc_complete(struct ata_queued_cmd *qc)
4633{
4634 struct ata_port *ap = qc->ap;
4635
Stephan Linzeb25cb92016-06-10 07:59:56 +02004636 /* Trigger the LED (if available) */
Linus Walleijd1ed7c52018-02-24 23:45:56 +01004637 ledtrig_disk_activity(!!(qc->tf.flags & ATA_TFLAG_WRITE));
Stephan Linzeb25cb92016-06-10 07:59:56 +02004638
Tejun Heof686bcb2006-05-15 20:58:05 +09004639 /* XXX: New EH and old EH use different mechanisms to
4640 * synchronize EH with regular execution path.
4641 *
4642 * In new EH, a failed qc is marked with ATA_QCFLAG_FAILED.
4643 * Normal execution path is responsible for not accessing a
4644 * failed qc. libata core enforces the rule by returning NULL
4645 * from ata_qc_from_tag() for failed qcs.
4646 *
4647 * Old EH depends on ata_qc_complete() nullifying completion
4648 * requests if ATA_QCFLAG_EH_SCHEDULED is set. Old EH does
4649 * not synchronize with interrupt handler. Only PIO task is
4650 * taken care of.
4651 */
4652 if (ap->ops->error_handler) {
Tejun Heo4dbfa392007-10-25 18:22:44 +09004653 struct ata_device *dev = qc->dev;
4654 struct ata_eh_info *ehi = &dev->link->eh_info;
4655
Tejun Heof686bcb2006-05-15 20:58:05 +09004656 if (unlikely(qc->err_mask))
4657 qc->flags |= ATA_QCFLAG_FAILED;
4658
Tejun Heof08dc1a2010-12-09 15:59:32 +01004659 /*
4660 * Finish internal commands without any further processing
4661 * and always with the result TF filled.
4662 */
4663 if (unlikely(ata_tag_internal(qc->tag))) {
Tejun Heof4b31db92009-10-16 13:00:51 +09004664 fill_result_tf(qc);
Hannes Reinecke255c03d2015-03-27 16:46:38 +01004665 trace_ata_qc_complete_internal(qc);
Tejun Heof08dc1a2010-12-09 15:59:32 +01004666 __ata_qc_complete(qc);
4667 return;
4668 }
Tejun Heof4b31db92009-10-16 13:00:51 +09004669
Tejun Heof08dc1a2010-12-09 15:59:32 +01004670 /*
4671 * Non-internal qc has failed. Fill the result TF and
4672 * summon EH.
4673 */
4674 if (unlikely(qc->flags & ATA_QCFLAG_FAILED)) {
4675 fill_result_tf(qc);
Hannes Reinecke255c03d2015-03-27 16:46:38 +01004676 trace_ata_qc_complete_failed(qc);
Tejun Heof08dc1a2010-12-09 15:59:32 +01004677 ata_qc_schedule_eh(qc);
Tejun Heof4b31db92009-10-16 13:00:51 +09004678 return;
Tejun Heof686bcb2006-05-15 20:58:05 +09004679 }
4680
Tejun Heo4dc738e2009-09-17 18:45:27 +09004681 WARN_ON_ONCE(ap->pflags & ATA_PFLAG_FROZEN);
4682
Tejun Heof686bcb2006-05-15 20:58:05 +09004683 /* read result TF if requested */
4684 if (qc->flags & ATA_QCFLAG_RESULT_TF)
Tejun Heo39599a52006-11-14 22:37:35 +09004685 fill_result_tf(qc);
Tejun Heof686bcb2006-05-15 20:58:05 +09004686
Hannes Reinecke255c03d2015-03-27 16:46:38 +01004687 trace_ata_qc_complete_done(qc);
Tejun Heo4dbfa392007-10-25 18:22:44 +09004688 /* Some commands need post-processing after successful
4689 * completion.
4690 */
4691 switch (qc->tf.command) {
4692 case ATA_CMD_SET_FEATURES:
4693 if (qc->tf.feature != SETFEATURES_WC_ON &&
Tom Yan0c127352016-07-12 21:37:02 +08004694 qc->tf.feature != SETFEATURES_WC_OFF &&
4695 qc->tf.feature != SETFEATURES_RA_ON &&
4696 qc->tf.feature != SETFEATURES_RA_OFF)
Tejun Heo4dbfa392007-10-25 18:22:44 +09004697 break;
4698 /* fall through */
4699 case ATA_CMD_INIT_DEV_PARAMS: /* CHS translation changed */
4700 case ATA_CMD_SET_MULTI: /* multi_count changed */
4701 /* revalidate device */
4702 ehi->dev_action[dev->devno] |= ATA_EH_REVALIDATE;
4703 ata_port_schedule_eh(ap);
4704 break;
Tejun Heo054a5fb2007-10-25 18:30:36 +09004705
4706 case ATA_CMD_SLEEP:
4707 dev->flags |= ATA_DFLAG_SLEEPING;
4708 break;
Tejun Heo4dbfa392007-10-25 18:22:44 +09004709 }
4710
Tejun Heo00115e02007-11-27 19:28:58 +09004711 if (unlikely(dev->flags & ATA_DFLAG_DUBIOUS_XFER))
4712 ata_verify_xfer(qc);
4713
Tejun Heof686bcb2006-05-15 20:58:05 +09004714 __ata_qc_complete(qc);
4715 } else {
4716 if (qc->flags & ATA_QCFLAG_EH_SCHEDULED)
4717 return;
4718
4719 /* read result TF if failed or requested */
4720 if (qc->err_mask || qc->flags & ATA_QCFLAG_RESULT_TF)
Tejun Heo39599a52006-11-14 22:37:35 +09004721 fill_result_tf(qc);
Tejun Heof686bcb2006-05-15 20:58:05 +09004722
4723 __ata_qc_complete(qc);
4724 }
4725}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01004726EXPORT_SYMBOL_GPL(ata_qc_complete);
Tejun Heof686bcb2006-05-15 20:58:05 +09004727
Tejun Heodedaf2b2006-05-15 21:03:43 +09004728/**
Sascha Hauer8385d752019-12-13 09:04:08 +01004729 * ata_qc_get_active - get bitmask of active qcs
4730 * @ap: port in question
4731 *
4732 * LOCKING:
4733 * spin_lock_irqsave(host lock)
4734 *
4735 * RETURNS:
4736 * Bitmask of active qcs
4737 */
4738u64 ata_qc_get_active(struct ata_port *ap)
4739{
4740 u64 qc_active = ap->qc_active;
4741
4742 /* ATA_TAG_INTERNAL is sent to hw as tag 0 */
4743 if (qc_active & (1ULL << ATA_TAG_INTERNAL)) {
4744 qc_active |= (1 << 0);
4745 qc_active &= ~(1ULL << ATA_TAG_INTERNAL);
4746 }
4747
4748 return qc_active;
4749}
4750EXPORT_SYMBOL_GPL(ata_qc_get_active);
4751
4752/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004753 * ata_qc_issue - issue taskfile to device
4754 * @qc: command to issue to device
4755 *
4756 * Prepare an ATA command to submission to device.
4757 * This includes mapping the data into a DMA-able
4758 * area, filling in the S/G table, and finally
4759 * writing the taskfile to hardware, starting the command.
4760 *
4761 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04004762 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004763 */
Tejun Heo8e0e6942006-03-31 20:41:11 +09004764void ata_qc_issue(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004765{
4766 struct ata_port *ap = qc->ap;
Tejun Heo9af5c9c2007-08-06 18:36:22 +09004767 struct ata_link *link = qc->dev->link;
Tejun Heo405e66b2007-11-27 19:28:53 +09004768 u8 prot = qc->tf.protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004769
Tejun Heodedaf2b2006-05-15 21:03:43 +09004770 /* Make sure only one non-NCQ command is outstanding. The
4771 * check is skipped for old EH because it reuses active qc to
4772 * request ATAPI sense.
4773 */
Tejun Heoefcb3cf2009-01-09 19:19:14 +09004774 WARN_ON_ONCE(ap->ops->error_handler && ata_tag_valid(link->active_tag));
Tejun Heodedaf2b2006-05-15 21:03:43 +09004775
Tejun Heo1973a022007-12-05 10:36:13 +09004776 if (ata_is_ncq(prot)) {
Jens Axboe4e5b6262018-05-11 12:51:04 -06004777 WARN_ON_ONCE(link->sactive & (1 << qc->hw_tag));
Tejun Heoda917d62007-09-23 13:14:12 +09004778
4779 if (!link->sactive)
4780 ap->nr_active_links++;
Jens Axboe4e5b6262018-05-11 12:51:04 -06004781 link->sactive |= 1 << qc->hw_tag;
Tejun Heodedaf2b2006-05-15 21:03:43 +09004782 } else {
Tejun Heoefcb3cf2009-01-09 19:19:14 +09004783 WARN_ON_ONCE(link->sactive);
Tejun Heoda917d62007-09-23 13:14:12 +09004784
4785 ap->nr_active_links++;
Tejun Heo9af5c9c2007-08-06 18:36:22 +09004786 link->active_tag = qc->tag;
Tejun Heodedaf2b2006-05-15 21:03:43 +09004787 }
4788
Tejun Heoe4a70e72006-03-31 20:36:47 +09004789 qc->flags |= ATA_QCFLAG_ACTIVE;
Jens Axboee3ed89392018-05-11 12:51:05 -06004790 ap->qc_active |= 1ULL << qc->tag;
Tejun Heoe4a70e72006-03-31 20:36:47 +09004791
Tejun Heo60f5d6e2010-08-23 11:27:27 +02004792 /*
4793 * We guarantee to LLDs that they will have at least one
Tejun Heof92a2632007-12-05 16:43:10 +09004794 * non-zero sg if the command is a data command.
4795 */
Eric Biggers9173e5e2018-02-03 20:33:27 -08004796 if (ata_is_data(prot) && (!qc->sg || !qc->n_elem || !qc->nbytes))
Tejun Heo60f5d6e2010-08-23 11:27:27 +02004797 goto sys_err;
Tejun Heof92a2632007-12-05 16:43:10 +09004798
Tejun Heo405e66b2007-11-27 19:28:53 +09004799 if (ata_is_dma(prot) || (ata_is_pio(prot) &&
Tejun Heof92a2632007-12-05 16:43:10 +09004800 (ap->flags & ATA_FLAG_PIO_DMA)))
Tejun Heo001102d2007-12-05 16:43:09 +09004801 if (ata_sg_setup(qc))
Tejun Heo60f5d6e2010-08-23 11:27:27 +02004802 goto sys_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004803
Tejun Heocf480622008-01-24 00:05:14 +09004804 /* if device is sleeping, schedule reset and abort the link */
Tejun Heo054a5fb2007-10-25 18:30:36 +09004805 if (unlikely(qc->dev->flags & ATA_DFLAG_SLEEPING)) {
Tejun Heocf480622008-01-24 00:05:14 +09004806 link->eh_info.action |= ATA_EH_RESET;
Tejun Heo054a5fb2007-10-25 18:30:36 +09004807 ata_ehi_push_desc(&link->eh_info, "waking up from sleep");
4808 ata_link_abort(link);
4809 return;
4810 }
4811
Jiri Slaby95364f32019-10-31 10:59:45 +01004812 qc->err_mask |= ap->ops->qc_prep(qc);
4813 if (unlikely(qc->err_mask))
4814 goto err;
Hannes Reinecke255c03d2015-03-27 16:46:38 +01004815 trace_ata_qc_issue(qc);
Tejun Heo8e0e6942006-03-31 20:41:11 +09004816 qc->err_mask |= ap->ops->qc_issue(qc);
4817 if (unlikely(qc->err_mask))
4818 goto err;
4819 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004820
Tejun Heo60f5d6e2010-08-23 11:27:27 +02004821sys_err:
Tejun Heo8e0e6942006-03-31 20:41:11 +09004822 qc->err_mask |= AC_ERR_SYSTEM;
4823err:
4824 ata_qc_complete(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004825}
4826
4827/**
Tejun Heob1c72912008-07-31 17:02:43 +09004828 * ata_phys_link_online - test whether the given link is online
Tejun Heo936fd732007-08-06 18:36:23 +09004829 * @link: ATA link to test
Tejun Heo34bf2172006-05-15 20:57:46 +09004830 *
Tejun Heo936fd732007-08-06 18:36:23 +09004831 * Test whether @link is online. Note that this function returns
4832 * 0 if online status of @link cannot be obtained, so
4833 * ata_link_online(link) != !ata_link_offline(link).
Tejun Heo34bf2172006-05-15 20:57:46 +09004834 *
4835 * LOCKING:
4836 * None.
4837 *
4838 * RETURNS:
Tejun Heob5b3fa32008-07-31 17:02:42 +09004839 * True if the port online status is available and online.
Tejun Heo34bf2172006-05-15 20:57:46 +09004840 */
Tejun Heob1c72912008-07-31 17:02:43 +09004841bool ata_phys_link_online(struct ata_link *link)
Tejun Heo34bf2172006-05-15 20:57:46 +09004842{
4843 u32 sstatus;
4844
Tejun Heo936fd732007-08-06 18:36:23 +09004845 if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0 &&
Tejun Heo9913ff8a2009-01-29 20:31:31 +09004846 ata_sstatus_online(sstatus))
Tejun Heob5b3fa32008-07-31 17:02:42 +09004847 return true;
4848 return false;
Tejun Heo34bf2172006-05-15 20:57:46 +09004849}
4850
4851/**
Tejun Heob1c72912008-07-31 17:02:43 +09004852 * ata_phys_link_offline - test whether the given link is offline
Tejun Heo936fd732007-08-06 18:36:23 +09004853 * @link: ATA link to test
Tejun Heo34bf2172006-05-15 20:57:46 +09004854 *
Tejun Heo936fd732007-08-06 18:36:23 +09004855 * Test whether @link is offline. Note that this function
4856 * returns 0 if offline status of @link cannot be obtained, so
4857 * ata_link_online(link) != !ata_link_offline(link).
Tejun Heo34bf2172006-05-15 20:57:46 +09004858 *
4859 * LOCKING:
4860 * None.
4861 *
4862 * RETURNS:
Tejun Heob5b3fa32008-07-31 17:02:42 +09004863 * True if the port offline status is available and offline.
Tejun Heo34bf2172006-05-15 20:57:46 +09004864 */
Tejun Heob1c72912008-07-31 17:02:43 +09004865bool ata_phys_link_offline(struct ata_link *link)
Tejun Heo34bf2172006-05-15 20:57:46 +09004866{
4867 u32 sstatus;
4868
Tejun Heo936fd732007-08-06 18:36:23 +09004869 if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0 &&
Tejun Heo9913ff8a2009-01-29 20:31:31 +09004870 !ata_sstatus_online(sstatus))
Tejun Heob5b3fa32008-07-31 17:02:42 +09004871 return true;
4872 return false;
Tejun Heo34bf2172006-05-15 20:57:46 +09004873}
Edward Falk0baab862005-06-02 18:17:13 -04004874
Tejun Heob1c72912008-07-31 17:02:43 +09004875/**
4876 * ata_link_online - test whether the given link is online
4877 * @link: ATA link to test
4878 *
4879 * Test whether @link is online. This is identical to
4880 * ata_phys_link_online() when there's no slave link. When
4881 * there's a slave link, this function should only be called on
4882 * the master link and will return true if any of M/S links is
4883 * online.
4884 *
4885 * LOCKING:
4886 * None.
4887 *
4888 * RETURNS:
4889 * True if the port online status is available and online.
4890 */
4891bool ata_link_online(struct ata_link *link)
4892{
4893 struct ata_link *slave = link->ap->slave_link;
4894
4895 WARN_ON(link == slave); /* shouldn't be called on slave link */
4896
4897 return ata_phys_link_online(link) ||
4898 (slave && ata_phys_link_online(slave));
4899}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01004900EXPORT_SYMBOL_GPL(ata_link_online);
Tejun Heob1c72912008-07-31 17:02:43 +09004901
4902/**
4903 * ata_link_offline - test whether the given link is offline
4904 * @link: ATA link to test
4905 *
4906 * Test whether @link is offline. This is identical to
4907 * ata_phys_link_offline() when there's no slave link. When
4908 * there's a slave link, this function should only be called on
4909 * the master link and will return true if both M/S links are
4910 * offline.
4911 *
4912 * LOCKING:
4913 * None.
4914 *
4915 * RETURNS:
4916 * True if the port offline status is available and offline.
4917 */
4918bool ata_link_offline(struct ata_link *link)
4919{
4920 struct ata_link *slave = link->ap->slave_link;
4921
4922 WARN_ON(link == slave); /* shouldn't be called on slave link */
4923
4924 return ata_phys_link_offline(link) &&
4925 (!slave || ata_phys_link_offline(slave));
4926}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01004927EXPORT_SYMBOL_GPL(ata_link_offline);
Tejun Heob1c72912008-07-31 17:02:43 +09004928
Tejun Heo6ffa01d2007-03-02 17:32:47 +09004929#ifdef CONFIG_PM
Dan Williamsbc6e7c42014-03-14 13:52:48 -07004930static void ata_port_request_pm(struct ata_port *ap, pm_message_t mesg,
4931 unsigned int action, unsigned int ehi_flags,
4932 bool async)
Tejun Heo500530f2006-07-03 16:07:27 +09004933{
Lin Ming5ef41082011-12-05 09:20:27 +08004934 struct ata_link *link;
Tejun Heo500530f2006-07-03 16:07:27 +09004935 unsigned long flags;
Tejun Heo500530f2006-07-03 16:07:27 +09004936
Lin Ming5ef41082011-12-05 09:20:27 +08004937 /* Previous resume operation might still be in
4938 * progress. Wait for PM_PENDING to clear.
4939 */
4940 if (ap->pflags & ATA_PFLAG_PM_PENDING) {
4941 ata_port_wait_eh(ap);
4942 WARN_ON(ap->pflags & ATA_PFLAG_PM_PENDING);
Tejun Heo500530f2006-07-03 16:07:27 +09004943 }
4944
Lin Ming5ef41082011-12-05 09:20:27 +08004945 /* request PM ops to EH */
4946 spin_lock_irqsave(ap->lock, flags);
4947
4948 ap->pm_mesg = mesg;
Lin Ming5ef41082011-12-05 09:20:27 +08004949 ap->pflags |= ATA_PFLAG_PM_PENDING;
4950 ata_for_each_link(link, ap, HOST_FIRST) {
4951 link->eh_info.action |= action;
4952 link->eh_info.flags |= ehi_flags;
4953 }
4954
4955 ata_port_schedule_eh(ap);
4956
4957 spin_unlock_irqrestore(ap->lock, flags);
4958
Dan Williams2fcbdcb2012-06-21 23:41:46 -07004959 if (!async) {
Lin Ming5ef41082011-12-05 09:20:27 +08004960 ata_port_wait_eh(ap);
4961 WARN_ON(ap->pflags & ATA_PFLAG_PM_PENDING);
4962 }
Tejun Heo500530f2006-07-03 16:07:27 +09004963}
4964
Dan Williamsbc6e7c42014-03-14 13:52:48 -07004965/*
4966 * On some hardware, device fails to respond after spun down for suspend. As
4967 * the device won't be used before being resumed, we don't need to touch the
4968 * device. Ask EH to skip the usual stuff and proceed directly to suspend.
4969 *
4970 * http://thread.gmane.org/gmane.linux.ide/46764
4971 */
4972static const unsigned int ata_port_suspend_ehi = ATA_EHI_QUIET
4973 | ATA_EHI_NO_AUTOPSY
4974 | ATA_EHI_NO_RECOVERY;
4975
4976static void ata_port_suspend(struct ata_port *ap, pm_message_t mesg)
Lin Ming5ef41082011-12-05 09:20:27 +08004977{
Dan Williamsbc6e7c42014-03-14 13:52:48 -07004978 ata_port_request_pm(ap, mesg, 0, ata_port_suspend_ehi, false);
Lin Ming5ef41082011-12-05 09:20:27 +08004979}
4980
Dan Williamsbc6e7c42014-03-14 13:52:48 -07004981static void ata_port_suspend_async(struct ata_port *ap, pm_message_t mesg)
4982{
4983 ata_port_request_pm(ap, mesg, 0, ata_port_suspend_ehi, true);
4984}
4985
4986static int ata_port_pm_suspend(struct device *dev)
Dan Williams2fcbdcb2012-06-21 23:41:46 -07004987{
4988 struct ata_port *ap = to_ata_port(dev);
4989
Lin Ming5ef41082011-12-05 09:20:27 +08004990 if (pm_runtime_suspended(dev))
4991 return 0;
4992
Dan Williamsbc6e7c42014-03-14 13:52:48 -07004993 ata_port_suspend(ap, PMSG_SUSPEND);
4994 return 0;
Lin Ming33574d62011-12-22 14:50:49 +08004995}
4996
Dan Williamsbc6e7c42014-03-14 13:52:48 -07004997static int ata_port_pm_freeze(struct device *dev)
Dan Williams2fcbdcb2012-06-21 23:41:46 -07004998{
4999 struct ata_port *ap = to_ata_port(dev);
5000
Dan Williamsbc6e7c42014-03-14 13:52:48 -07005001 if (pm_runtime_suspended(dev))
5002 return 0;
5003
5004 ata_port_suspend(ap, PMSG_FREEZE);
5005 return 0;
Dan Williams2fcbdcb2012-06-21 23:41:46 -07005006}
5007
Dan Williamsbc6e7c42014-03-14 13:52:48 -07005008static int ata_port_pm_poweroff(struct device *dev)
Lin Minge90b1e52011-12-22 14:50:48 +08005009{
Dan Williamsbc6e7c42014-03-14 13:52:48 -07005010 ata_port_suspend(to_ata_port(dev), PMSG_HIBERNATE);
5011 return 0;
5012}
Lin Minge90b1e52011-12-22 14:50:48 +08005013
Dan Williamsbc6e7c42014-03-14 13:52:48 -07005014static const unsigned int ata_port_resume_ehi = ATA_EHI_NO_AUTOPSY
5015 | ATA_EHI_QUIET;
Lin Minge90b1e52011-12-22 14:50:48 +08005016
Dan Williamsbc6e7c42014-03-14 13:52:48 -07005017static void ata_port_resume(struct ata_port *ap, pm_message_t mesg)
5018{
5019 ata_port_request_pm(ap, mesg, ATA_EH_RESET, ata_port_resume_ehi, false);
5020}
5021
5022static void ata_port_resume_async(struct ata_port *ap, pm_message_t mesg)
5023{
5024 ata_port_request_pm(ap, mesg, ATA_EH_RESET, ata_port_resume_ehi, true);
5025}
5026
5027static int ata_port_pm_resume(struct device *dev)
5028{
Todd Brandt200421a2014-03-14 13:52:54 -07005029 ata_port_resume_async(to_ata_port(dev), PMSG_RESUME);
Dan Williamsbc6e7c42014-03-14 13:52:48 -07005030 pm_runtime_disable(dev);
5031 pm_runtime_set_active(dev);
5032 pm_runtime_enable(dev);
5033 return 0;
Lin Minge90b1e52011-12-22 14:50:48 +08005034}
5035
Aaron Lu7e15e9b2013-01-15 17:21:04 +08005036/*
5037 * For ODDs, the upper layer will poll for media change every few seconds,
5038 * which will make it enter and leave suspend state every few seconds. And
5039 * as each suspend will cause a hard/soft reset, the gain of runtime suspend
5040 * is very little and the ODD may malfunction after constantly being reset.
5041 * So the idle callback here will not proceed to suspend if a non-ZPODD capable
5042 * ODD is attached to the port.
5043 */
Lin Ming9ee4f392011-12-05 09:20:28 +08005044static int ata_port_runtime_idle(struct device *dev)
5045{
Aaron Lu7e15e9b2013-01-15 17:21:04 +08005046 struct ata_port *ap = to_ata_port(dev);
5047 struct ata_link *link;
5048 struct ata_device *adev;
5049
5050 ata_for_each_link(link, ap, HOST_FIRST) {
5051 ata_for_each_dev(adev, link, ENABLED)
5052 if (adev->class == ATA_DEV_ATAPI &&
5053 !zpodd_dev_enabled(adev))
5054 return -EBUSY;
5055 }
5056
Rafael J. Wysocki45f0a852013-06-03 21:49:52 +02005057 return 0;
Lin Ming9ee4f392011-12-05 09:20:28 +08005058}
5059
Aaron Lua7ff60d2013-01-25 14:29:35 +08005060static int ata_port_runtime_suspend(struct device *dev)
5061{
Dan Williamsbc6e7c42014-03-14 13:52:48 -07005062 ata_port_suspend(to_ata_port(dev), PMSG_AUTO_SUSPEND);
5063 return 0;
Aaron Lua7ff60d2013-01-25 14:29:35 +08005064}
5065
5066static int ata_port_runtime_resume(struct device *dev)
5067{
Dan Williamsbc6e7c42014-03-14 13:52:48 -07005068 ata_port_resume(to_ata_port(dev), PMSG_AUTO_RESUME);
5069 return 0;
Aaron Lua7ff60d2013-01-25 14:29:35 +08005070}
5071
Lin Ming5ef41082011-12-05 09:20:27 +08005072static const struct dev_pm_ops ata_port_pm_ops = {
Dan Williamsbc6e7c42014-03-14 13:52:48 -07005073 .suspend = ata_port_pm_suspend,
5074 .resume = ata_port_pm_resume,
5075 .freeze = ata_port_pm_freeze,
5076 .thaw = ata_port_pm_resume,
5077 .poweroff = ata_port_pm_poweroff,
5078 .restore = ata_port_pm_resume,
Lin Ming9ee4f392011-12-05 09:20:28 +08005079
Aaron Lua7ff60d2013-01-25 14:29:35 +08005080 .runtime_suspend = ata_port_runtime_suspend,
5081 .runtime_resume = ata_port_runtime_resume,
Lin Ming9ee4f392011-12-05 09:20:28 +08005082 .runtime_idle = ata_port_runtime_idle,
Lin Ming5ef41082011-12-05 09:20:27 +08005083};
5084
Dan Williams2fcbdcb2012-06-21 23:41:46 -07005085/* sas ports don't participate in pm runtime management of ata_ports,
5086 * and need to resume ata devices at the domain level, not the per-port
5087 * level. sas suspend/resume is async to allow parallel port recovery
5088 * since sas has multiple ata_port instances per Scsi_Host.
5089 */
Dan Williamsbc6e7c42014-03-14 13:52:48 -07005090void ata_sas_port_suspend(struct ata_port *ap)
Dan Williams2fcbdcb2012-06-21 23:41:46 -07005091{
Dan Williamsbc6e7c42014-03-14 13:52:48 -07005092 ata_port_suspend_async(ap, PMSG_SUSPEND);
Dan Williams2fcbdcb2012-06-21 23:41:46 -07005093}
Dan Williamsbc6e7c42014-03-14 13:52:48 -07005094EXPORT_SYMBOL_GPL(ata_sas_port_suspend);
Dan Williams2fcbdcb2012-06-21 23:41:46 -07005095
Dan Williamsbc6e7c42014-03-14 13:52:48 -07005096void ata_sas_port_resume(struct ata_port *ap)
Dan Williams2fcbdcb2012-06-21 23:41:46 -07005097{
Dan Williamsbc6e7c42014-03-14 13:52:48 -07005098 ata_port_resume_async(ap, PMSG_RESUME);
Dan Williams2fcbdcb2012-06-21 23:41:46 -07005099}
Dan Williamsbc6e7c42014-03-14 13:52:48 -07005100EXPORT_SYMBOL_GPL(ata_sas_port_resume);
Dan Williams2fcbdcb2012-06-21 23:41:46 -07005101
Tejun Heo500530f2006-07-03 16:07:27 +09005102/**
Jeff Garzikcca39742006-08-24 03:19:22 -04005103 * ata_host_suspend - suspend host
5104 * @host: host to suspend
Tejun Heo500530f2006-07-03 16:07:27 +09005105 * @mesg: PM message
5106 *
Lin Ming5ef41082011-12-05 09:20:27 +08005107 * Suspend @host. Actual operation is performed by port suspend.
Tejun Heo500530f2006-07-03 16:07:27 +09005108 */
Jeff Garzikcca39742006-08-24 03:19:22 -04005109int ata_host_suspend(struct ata_host *host, pm_message_t mesg)
Tejun Heo500530f2006-07-03 16:07:27 +09005110{
Lin Ming5ef41082011-12-05 09:20:27 +08005111 host->dev->power.power_state = mesg;
5112 return 0;
Tejun Heo500530f2006-07-03 16:07:27 +09005113}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01005114EXPORT_SYMBOL_GPL(ata_host_suspend);
Tejun Heo500530f2006-07-03 16:07:27 +09005115
5116/**
Jeff Garzikcca39742006-08-24 03:19:22 -04005117 * ata_host_resume - resume host
5118 * @host: host to resume
Tejun Heo500530f2006-07-03 16:07:27 +09005119 *
Lin Ming5ef41082011-12-05 09:20:27 +08005120 * Resume @host. Actual operation is performed by port resume.
Tejun Heo500530f2006-07-03 16:07:27 +09005121 */
Jeff Garzikcca39742006-08-24 03:19:22 -04005122void ata_host_resume(struct ata_host *host)
Tejun Heo500530f2006-07-03 16:07:27 +09005123{
Jeff Garzik72ad6ec2008-02-25 17:31:10 -05005124 host->dev->power.power_state = PMSG_ON;
Tejun Heo500530f2006-07-03 16:07:27 +09005125}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01005126EXPORT_SYMBOL_GPL(ata_host_resume);
Tejun Heo6ffa01d2007-03-02 17:32:47 +09005127#endif
Tejun Heo500530f2006-07-03 16:07:27 +09005128
Bhumika Goyal8df82c12017-09-30 22:10:40 +05305129const struct device_type ata_port_type = {
Lin Ming5ef41082011-12-05 09:20:27 +08005130 .name = "ata_port",
5131#ifdef CONFIG_PM
5132 .pm = &ata_port_pm_ops,
5133#endif
5134};
5135
Randy Dunlapc893a3a2006-01-28 13:15:32 -05005136/**
Tejun Heo3ef3b432006-05-31 18:27:30 +09005137 * ata_dev_init - Initialize an ata_device structure
5138 * @dev: Device structure to initialize
5139 *
5140 * Initialize @dev in preparation for probing.
5141 *
5142 * LOCKING:
5143 * Inherited from caller.
5144 */
5145void ata_dev_init(struct ata_device *dev)
5146{
Tejun Heob1c72912008-07-31 17:02:43 +09005147 struct ata_link *link = ata_dev_phys_link(dev);
Tejun Heo9af5c9c2007-08-06 18:36:22 +09005148 struct ata_port *ap = link->ap;
Tejun Heo72fa4b72006-05-31 18:27:32 +09005149 unsigned long flags;
Tejun Heo3ef3b432006-05-31 18:27:30 +09005150
Tejun Heob1c72912008-07-31 17:02:43 +09005151 /* SATA spd limit is bound to the attached device, reset together */
Tejun Heo9af5c9c2007-08-06 18:36:22 +09005152 link->sata_spd_limit = link->hw_sata_spd_limit;
5153 link->sata_spd = 0;
Tejun Heo5a04bf42006-05-31 18:27:38 +09005154
Tejun Heo72fa4b72006-05-31 18:27:32 +09005155 /* High bits of dev->flags are used to record warm plug
5156 * requests which occur asynchronously. Synchronize using
Jeff Garzikcca39742006-08-24 03:19:22 -04005157 * host lock.
Tejun Heo72fa4b72006-05-31 18:27:32 +09005158 */
Jeff Garzikba6a1302006-06-22 23:46:10 -04005159 spin_lock_irqsave(ap->lock, flags);
Tejun Heo72fa4b72006-05-31 18:27:32 +09005160 dev->flags &= ~ATA_DFLAG_INIT_MASK;
Tejun Heo3dcc3232007-09-03 12:20:11 +09005161 dev->horkage = 0;
Jeff Garzikba6a1302006-06-22 23:46:10 -04005162 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heo72fa4b72006-05-31 18:27:32 +09005163
Tejun Heo99cf6102009-01-29 20:31:32 +09005164 memset((void *)dev + ATA_DEVICE_CLEAR_BEGIN, 0,
5165 ATA_DEVICE_CLEAR_END - ATA_DEVICE_CLEAR_BEGIN);
Tejun Heo3ef3b432006-05-31 18:27:30 +09005166 dev->pio_mask = UINT_MAX;
5167 dev->mwdma_mask = UINT_MAX;
5168 dev->udma_mask = UINT_MAX;
5169}
5170
5171/**
Tejun Heo4fb37a22007-08-06 18:36:23 +09005172 * ata_link_init - Initialize an ata_link structure
5173 * @ap: ATA port link is attached to
5174 * @link: Link structure to initialize
Tejun Heo89898052007-08-06 18:36:23 +09005175 * @pmp: Port multiplier port number
Tejun Heo4fb37a22007-08-06 18:36:23 +09005176 *
5177 * Initialize @link.
5178 *
5179 * LOCKING:
5180 * Kernel thread context (may sleep)
5181 */
Tejun Heofb7fd612007-09-23 13:14:12 +09005182void ata_link_init(struct ata_port *ap, struct ata_link *link, int pmp)
Tejun Heo4fb37a22007-08-06 18:36:23 +09005183{
5184 int i;
5185
5186 /* clear everything except for devices */
Gwendal Grignoud9027472010-05-25 12:31:38 -07005187 memset((void *)link + ATA_LINK_CLEAR_BEGIN, 0,
5188 ATA_LINK_CLEAR_END - ATA_LINK_CLEAR_BEGIN);
Tejun Heo4fb37a22007-08-06 18:36:23 +09005189
5190 link->ap = ap;
Tejun Heo89898052007-08-06 18:36:23 +09005191 link->pmp = pmp;
Tejun Heo4fb37a22007-08-06 18:36:23 +09005192 link->active_tag = ATA_TAG_POISON;
5193 link->hw_sata_spd_limit = UINT_MAX;
5194
5195 /* can't use iterator, ap isn't initialized yet */
5196 for (i = 0; i < ATA_MAX_DEVICES; i++) {
5197 struct ata_device *dev = &link->device[i];
5198
5199 dev->link = link;
5200 dev->devno = dev - link->device;
Tejun Heo110f66d2009-09-16 04:17:28 +09005201#ifdef CONFIG_ATA_ACPI
5202 dev->gtf_filter = ata_acpi_gtf_filter;
5203#endif
Tejun Heo4fb37a22007-08-06 18:36:23 +09005204 ata_dev_init(dev);
5205 }
5206}
5207
5208/**
5209 * sata_link_init_spd - Initialize link->sata_spd_limit
5210 * @link: Link to configure sata_spd_limit for
5211 *
5212 * Initialize @link->[hw_]sata_spd_limit to the currently
5213 * configured value.
5214 *
5215 * LOCKING:
5216 * Kernel thread context (may sleep).
5217 *
5218 * RETURNS:
5219 * 0 on success, -errno on failure.
5220 */
Tejun Heofb7fd612007-09-23 13:14:12 +09005221int sata_link_init_spd(struct ata_link *link)
Tejun Heo4fb37a22007-08-06 18:36:23 +09005222{
Tejun Heo33267322008-02-13 09:15:09 +09005223 u8 spd;
Tejun Heo4fb37a22007-08-06 18:36:23 +09005224 int rc;
5225
Tejun Heod127ea72008-07-31 16:09:34 +09005226 rc = sata_scr_read(link, SCR_CONTROL, &link->saved_scontrol);
Tejun Heo4fb37a22007-08-06 18:36:23 +09005227 if (rc)
5228 return rc;
5229
Tejun Heod127ea72008-07-31 16:09:34 +09005230 spd = (link->saved_scontrol >> 4) & 0xf;
Tejun Heo4fb37a22007-08-06 18:36:23 +09005231 if (spd)
5232 link->hw_sata_spd_limit &= (1 << spd) - 1;
5233
Tejun Heo05944bd2008-08-13 20:19:09 +09005234 ata_force_link_limits(link);
Tejun Heo33267322008-02-13 09:15:09 +09005235
Tejun Heo4fb37a22007-08-06 18:36:23 +09005236 link->sata_spd_limit = link->hw_sata_spd_limit;
5237
5238 return 0;
5239}
5240
5241/**
Tejun Heof3187192007-04-17 23:44:07 +09005242 * ata_port_alloc - allocate and initialize basic ATA port resources
5243 * @host: ATA host this allocated port belongs to
Linus Torvalds1da177e2005-04-16 15:20:36 -07005244 *
Tejun Heof3187192007-04-17 23:44:07 +09005245 * Allocate and initialize basic ATA port resources.
5246 *
5247 * RETURNS:
5248 * Allocate ATA port on success, NULL on failure.
Jeff Garzik0cba6322005-05-30 19:49:12 -04005249 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07005250 * LOCKING:
Tejun Heof3187192007-04-17 23:44:07 +09005251 * Inherited from calling layer (may sleep).
Linus Torvalds1da177e2005-04-16 15:20:36 -07005252 */
Tejun Heof3187192007-04-17 23:44:07 +09005253struct ata_port *ata_port_alloc(struct ata_host *host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005254{
Tejun Heof3187192007-04-17 23:44:07 +09005255 struct ata_port *ap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005256
Tejun Heof3187192007-04-17 23:44:07 +09005257 DPRINTK("ENTER\n");
5258
5259 ap = kzalloc(sizeof(*ap), GFP_KERNEL);
5260 if (!ap)
5261 return NULL;
Jeff Garzik4fca3772011-02-15 01:13:24 -05005262
Maxime Bizon7b3a24c52011-03-16 14:58:32 +01005263 ap->pflags |= ATA_PFLAG_INITIALIZING | ATA_PFLAG_FROZEN;
Jeff Garzikcca39742006-08-24 03:19:22 -04005264 ap->lock = &host->lock;
Tejun Heof3187192007-04-17 23:44:07 +09005265 ap->print_id = -1;
David Milburne628dc92013-05-14 13:48:40 -05005266 ap->local_port_no = -1;
Jeff Garzikcca39742006-08-24 03:19:22 -04005267 ap->host = host;
Tejun Heof3187192007-04-17 23:44:07 +09005268 ap->dev = host->dev;
Borislav Petkovbd5d8252006-06-11 23:17:01 -04005269
5270#if defined(ATA_VERBOSE_DEBUG)
5271 /* turn on all debugging levels */
5272 ap->msg_enable = 0x00FF;
5273#elif defined(ATA_DEBUG)
5274 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 +09005275#else
Borislav Petkov0dd4b212006-06-23 02:29:08 -04005276 ap->msg_enable = ATA_MSG_DRV | ATA_MSG_ERR | ATA_MSG_WARN;
Borislav Petkovbd5d8252006-06-11 23:17:01 -04005277#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005278
Tejun Heoad72cf92010-07-02 10:03:52 +02005279 mutex_init(&ap->scsi_scan_mutex);
David Howells65f27f32006-11-22 14:55:48 +00005280 INIT_DELAYED_WORK(&ap->hotplug_task, ata_scsi_hotplug);
5281 INIT_WORK(&ap->scsi_rescan_task, ata_scsi_dev_rescan);
Tejun Heoa72ec4c2006-01-23 13:09:37 +09005282 INIT_LIST_HEAD(&ap->eh_done_q);
Tejun Heoc6cf9e92006-05-31 18:27:27 +09005283 init_waitqueue_head(&ap->eh_wait_q);
Elias Oltmanns45fabbb2008-09-21 11:54:08 +02005284 init_completion(&ap->park_req_pending);
Kees Cookb93ab332017-10-16 14:56:42 -07005285 timer_setup(&ap->fastdrain_timer, ata_eh_fastdrain_timerfn,
5286 TIMER_DEFERRABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005287
Tejun Heo838df622006-05-15 20:57:44 +09005288 ap->cbl = ATA_CBL_NONE;
Tejun Heo838df622006-05-15 20:57:44 +09005289
Tejun Heo89898052007-08-06 18:36:23 +09005290 ata_link_init(ap, &ap->link, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005291
5292#ifdef ATA_IRQ_TRAP
5293 ap->stats.unhandled_irq = 1;
5294 ap->stats.idle_irq = 1;
5295#endif
Tejun Heo270390e2010-05-10 21:41:35 +02005296 ata_sff_port_init(ap);
5297
Linus Torvalds1da177e2005-04-16 15:20:36 -07005298 return ap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005299}
5300
Taras Kondratiuk2623c7a2018-03-09 08:34:41 +00005301static void ata_devres_release(struct device *gendev, void *res)
Tejun Heof0d36ef2007-01-20 16:00:28 +09005302{
5303 struct ata_host *host = dev_get_drvdata(gendev);
5304 int i;
5305
5306 for (i = 0; i < host->n_ports; i++) {
5307 struct ata_port *ap = host->ports[i];
5308
Tejun Heoecef7252007-04-17 23:44:06 +09005309 if (!ap)
5310 continue;
5311
Tejun Heo49114872007-04-17 23:44:06 +09005312 if (ap->scsi_host)
Tejun Heo1aa506e42007-03-09 19:36:12 +09005313 scsi_host_put(ap->scsi_host);
5314
Taras Kondratiuk2623c7a2018-03-09 08:34:41 +00005315 }
5316
5317 dev_set_drvdata(gendev, NULL);
5318 ata_host_put(host);
5319}
5320
5321static void ata_host_release(struct kref *kref)
5322{
5323 struct ata_host *host = container_of(kref, struct ata_host, kref);
5324 int i;
5325
5326 for (i = 0; i < host->n_ports; i++) {
5327 struct ata_port *ap = host->ports[i];
5328
Tejun Heo633273a2007-09-23 13:19:54 +09005329 kfree(ap->pmp_link);
Tejun Heob1c72912008-07-31 17:02:43 +09005330 kfree(ap->slave_link);
Tejun Heo49114872007-04-17 23:44:06 +09005331 kfree(ap);
Tejun Heo1aa506e42007-03-09 19:36:12 +09005332 host->ports[i] = NULL;
5333 }
Taras Kondratiuk2623c7a2018-03-09 08:34:41 +00005334 kfree(host);
5335}
Tejun Heo1aa506e42007-03-09 19:36:12 +09005336
Taras Kondratiuk2623c7a2018-03-09 08:34:41 +00005337void ata_host_get(struct ata_host *host)
5338{
5339 kref_get(&host->kref);
5340}
5341
5342void ata_host_put(struct ata_host *host)
5343{
5344 kref_put(&host->kref, ata_host_release);
Tejun Heof0d36ef2007-01-20 16:00:28 +09005345}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01005346EXPORT_SYMBOL_GPL(ata_host_put);
Tejun Heof0d36ef2007-01-20 16:00:28 +09005347
Linus Torvalds1da177e2005-04-16 15:20:36 -07005348/**
Tejun Heof3187192007-04-17 23:44:07 +09005349 * ata_host_alloc - allocate and init basic ATA host resources
5350 * @dev: generic device this host is associated with
5351 * @max_ports: maximum number of ATA ports associated with this host
5352 *
5353 * Allocate and initialize basic ATA host resources. LLD calls
5354 * this function to allocate a host, initializes it fully and
5355 * attaches it using ata_host_register().
5356 *
5357 * @max_ports ports are allocated and host->n_ports is
5358 * initialized to @max_ports. The caller is allowed to decrease
5359 * host->n_ports before calling ata_host_register(). The unused
5360 * ports will be automatically freed on registration.
5361 *
5362 * RETURNS:
5363 * Allocate ATA host on success, NULL on failure.
5364 *
5365 * LOCKING:
5366 * Inherited from calling layer (may sleep).
5367 */
5368struct ata_host *ata_host_alloc(struct device *dev, int max_ports)
5369{
5370 struct ata_host *host;
5371 size_t sz;
5372 int i;
Taras Kondratiuk2623c7a2018-03-09 08:34:41 +00005373 void *dr;
Tejun Heof3187192007-04-17 23:44:07 +09005374
5375 DPRINTK("ENTER\n");
5376
Tejun Heof3187192007-04-17 23:44:07 +09005377 /* alloc a container for our list of ATA ports (buses) */
5378 sz = sizeof(struct ata_host) + (max_ports + 1) * sizeof(void *);
Taras Kondratiuk2623c7a2018-03-09 08:34:41 +00005379 host = kzalloc(sz, GFP_KERNEL);
Tejun Heof3187192007-04-17 23:44:07 +09005380 if (!host)
Taras Kondratiuk2623c7a2018-03-09 08:34:41 +00005381 return NULL;
5382
Tejun Heof3187192007-04-17 23:44:07 +09005383 if (!devres_open_group(dev, NULL, GFP_KERNEL))
Colin Ian Kingdafd6c42018-03-27 14:26:01 +01005384 goto err_free;
Tejun Heof3187192007-04-17 23:44:07 +09005385
Taras Kondratiuk2623c7a2018-03-09 08:34:41 +00005386 dr = devres_alloc(ata_devres_release, 0, GFP_KERNEL);
5387 if (!dr)
Tejun Heof3187192007-04-17 23:44:07 +09005388 goto err_out;
5389
Taras Kondratiuk2623c7a2018-03-09 08:34:41 +00005390 devres_add(dev, dr);
Tejun Heof3187192007-04-17 23:44:07 +09005391 dev_set_drvdata(dev, host);
5392
5393 spin_lock_init(&host->lock);
Tejun Heoc0c362b2010-09-06 17:57:14 +02005394 mutex_init(&host->eh_mutex);
Tejun Heof3187192007-04-17 23:44:07 +09005395 host->dev = dev;
5396 host->n_ports = max_ports;
Taras Kondratiuk2623c7a2018-03-09 08:34:41 +00005397 kref_init(&host->kref);
Tejun Heof3187192007-04-17 23:44:07 +09005398
5399 /* allocate ports bound to this host */
5400 for (i = 0; i < max_ports; i++) {
5401 struct ata_port *ap;
5402
5403 ap = ata_port_alloc(host);
5404 if (!ap)
5405 goto err_out;
5406
5407 ap->port_no = i;
5408 host->ports[i] = ap;
5409 }
5410
5411 devres_remove_group(dev, NULL);
5412 return host;
5413
5414 err_out:
5415 devres_release_group(dev, NULL);
Colin Ian Kingdafd6c42018-03-27 14:26:01 +01005416 err_free:
5417 kfree(host);
Tejun Heof3187192007-04-17 23:44:07 +09005418 return NULL;
5419}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01005420EXPORT_SYMBOL_GPL(ata_host_alloc);
Tejun Heof3187192007-04-17 23:44:07 +09005421
5422/**
Tejun Heof5cda252007-04-17 23:44:07 +09005423 * ata_host_alloc_pinfo - alloc host and init with port_info array
5424 * @dev: generic device this host is associated with
5425 * @ppi: array of ATA port_info to initialize host with
5426 * @n_ports: number of ATA ports attached to this host
5427 *
5428 * Allocate ATA host and initialize with info from @ppi. If NULL
5429 * terminated, @ppi may contain fewer entries than @n_ports. The
5430 * last entry will be used for the remaining ports.
5431 *
5432 * RETURNS:
5433 * Allocate ATA host on success, NULL on failure.
5434 *
5435 * LOCKING:
5436 * Inherited from calling layer (may sleep).
5437 */
5438struct ata_host *ata_host_alloc_pinfo(struct device *dev,
5439 const struct ata_port_info * const * ppi,
5440 int n_ports)
5441{
5442 const struct ata_port_info *pi;
5443 struct ata_host *host;
5444 int i, j;
5445
5446 host = ata_host_alloc(dev, n_ports);
5447 if (!host)
5448 return NULL;
5449
5450 for (i = 0, j = 0, pi = NULL; i < host->n_ports; i++) {
5451 struct ata_port *ap = host->ports[i];
5452
5453 if (ppi[j])
5454 pi = ppi[j++];
5455
5456 ap->pio_mask = pi->pio_mask;
5457 ap->mwdma_mask = pi->mwdma_mask;
5458 ap->udma_mask = pi->udma_mask;
5459 ap->flags |= pi->flags;
Tejun Heo0c887582007-08-06 18:36:23 +09005460 ap->link.flags |= pi->link_flags;
Tejun Heof5cda252007-04-17 23:44:07 +09005461 ap->ops = pi->port_ops;
5462
5463 if (!host->ops && (pi->port_ops != &ata_dummy_port_ops))
5464 host->ops = pi->port_ops;
Tejun Heof5cda252007-04-17 23:44:07 +09005465 }
5466
5467 return host;
5468}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01005469EXPORT_SYMBOL_GPL(ata_host_alloc_pinfo);
Tejun Heof5cda252007-04-17 23:44:07 +09005470
Tejun Heo32ebbc02007-11-08 13:09:00 +09005471static void ata_host_stop(struct device *gendev, void *res)
5472{
5473 struct ata_host *host = dev_get_drvdata(gendev);
5474 int i;
5475
5476 WARN_ON(!(host->flags & ATA_HOST_STARTED));
5477
5478 for (i = 0; i < host->n_ports; i++) {
5479 struct ata_port *ap = host->ports[i];
5480
5481 if (ap->ops->port_stop)
5482 ap->ops->port_stop(ap);
5483 }
5484
5485 if (host->ops->host_stop)
5486 host->ops->host_stop(host);
5487}
5488
Tejun Heof5cda252007-04-17 23:44:07 +09005489/**
Tejun Heo029cfd62008-03-25 12:22:49 +09005490 * ata_finalize_port_ops - finalize ata_port_operations
5491 * @ops: ata_port_operations to finalize
5492 *
5493 * An ata_port_operations can inherit from another ops and that
5494 * ops can again inherit from another. This can go on as many
5495 * times as necessary as long as there is no loop in the
5496 * inheritance chain.
5497 *
5498 * Ops tables are finalized when the host is started. NULL or
5499 * unspecified entries are inherited from the closet ancestor
5500 * which has the method and the entry is populated with it.
5501 * After finalization, the ops table directly points to all the
5502 * methods and ->inherits is no longer necessary and cleared.
5503 *
5504 * Using ATA_OP_NULL, inheriting ops can force a method to NULL.
5505 *
5506 * LOCKING:
5507 * None.
5508 */
5509static void ata_finalize_port_ops(struct ata_port_operations *ops)
5510{
Pradeep Singh Rautela2da67652008-05-29 23:28:14 +05305511 static DEFINE_SPINLOCK(lock);
Tejun Heo029cfd62008-03-25 12:22:49 +09005512 const struct ata_port_operations *cur;
5513 void **begin = (void **)ops;
5514 void **end = (void **)&ops->inherits;
5515 void **pp;
5516
5517 if (!ops || !ops->inherits)
5518 return;
5519
5520 spin_lock(&lock);
5521
5522 for (cur = ops->inherits; cur; cur = cur->inherits) {
5523 void **inherit = (void **)cur;
5524
5525 for (pp = begin; pp < end; pp++, inherit++)
5526 if (!*pp)
5527 *pp = *inherit;
5528 }
5529
5530 for (pp = begin; pp < end; pp++)
5531 if (IS_ERR(*pp))
5532 *pp = NULL;
5533
5534 ops->inherits = NULL;
5535
5536 spin_unlock(&lock);
5537}
5538
5539/**
Tejun Heoecef7252007-04-17 23:44:06 +09005540 * ata_host_start - start and freeze ports of an ATA host
5541 * @host: ATA host to start ports for
5542 *
5543 * Start and then freeze ports of @host. Started status is
5544 * recorded in host->flags, so this function can be called
5545 * multiple times. Ports are guaranteed to get started only
Tejun Heof3187192007-04-17 23:44:07 +09005546 * once. If host->ops isn't initialized yet, its set to the
5547 * first non-dummy port ops.
Tejun Heoecef7252007-04-17 23:44:06 +09005548 *
5549 * LOCKING:
5550 * Inherited from calling layer (may sleep).
5551 *
5552 * RETURNS:
5553 * 0 if all ports are started successfully, -errno otherwise.
5554 */
5555int ata_host_start(struct ata_host *host)
5556{
Tejun Heo32ebbc02007-11-08 13:09:00 +09005557 int have_stop = 0;
5558 void *start_dr = NULL;
Tejun Heoecef7252007-04-17 23:44:06 +09005559 int i, rc;
5560
5561 if (host->flags & ATA_HOST_STARTED)
5562 return 0;
5563
Tejun Heo029cfd62008-03-25 12:22:49 +09005564 ata_finalize_port_ops(host->ops);
5565
Tejun Heoecef7252007-04-17 23:44:06 +09005566 for (i = 0; i < host->n_ports; i++) {
5567 struct ata_port *ap = host->ports[i];
5568
Tejun Heo029cfd62008-03-25 12:22:49 +09005569 ata_finalize_port_ops(ap->ops);
5570
Tejun Heof3187192007-04-17 23:44:07 +09005571 if (!host->ops && !ata_port_is_dummy(ap))
5572 host->ops = ap->ops;
5573
Tejun Heo32ebbc02007-11-08 13:09:00 +09005574 if (ap->ops->port_stop)
5575 have_stop = 1;
5576 }
5577
5578 if (host->ops->host_stop)
5579 have_stop = 1;
5580
5581 if (have_stop) {
5582 start_dr = devres_alloc(ata_host_stop, 0, GFP_KERNEL);
5583 if (!start_dr)
5584 return -ENOMEM;
5585 }
5586
5587 for (i = 0; i < host->n_ports; i++) {
5588 struct ata_port *ap = host->ports[i];
5589
Tejun Heoecef7252007-04-17 23:44:06 +09005590 if (ap->ops->port_start) {
5591 rc = ap->ops->port_start(ap);
5592 if (rc) {
Alan Cox0f9fe9b2007-11-30 15:23:16 +00005593 if (rc != -ENODEV)
Joe Perchesa44fec12011-04-15 15:51:58 -07005594 dev_err(host->dev,
5595 "failed to start port %d (errno=%d)\n",
5596 i, rc);
Tejun Heoecef7252007-04-17 23:44:06 +09005597 goto err_out;
5598 }
5599 }
Tejun Heoecef7252007-04-17 23:44:06 +09005600 ata_eh_freeze_port(ap);
5601 }
5602
Tejun Heo32ebbc02007-11-08 13:09:00 +09005603 if (start_dr)
5604 devres_add(host->dev, start_dr);
Tejun Heoecef7252007-04-17 23:44:06 +09005605 host->flags |= ATA_HOST_STARTED;
5606 return 0;
5607
5608 err_out:
5609 while (--i >= 0) {
5610 struct ata_port *ap = host->ports[i];
5611
5612 if (ap->ops->port_stop)
5613 ap->ops->port_stop(ap);
5614 }
Tejun Heo32ebbc02007-11-08 13:09:00 +09005615 devres_free(start_dr);
Tejun Heoecef7252007-04-17 23:44:06 +09005616 return rc;
5617}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01005618EXPORT_SYMBOL_GPL(ata_host_start);
Tejun Heoecef7252007-04-17 23:44:06 +09005619
5620/**
Dan Williams8d8e7d12012-07-09 21:06:08 -07005621 * ata_sas_host_init - Initialize a host struct for sas (ipr, libsas)
Jeff Garzikcca39742006-08-24 03:19:22 -04005622 * @host: host to initialize
5623 * @dev: device host is attached to
Jeff Garzikcca39742006-08-24 03:19:22 -04005624 * @ops: port_ops
Brian Kingb03732f2006-08-07 14:27:10 -05005625 *
Brian Kingb03732f2006-08-07 14:27:10 -05005626 */
Jeff Garzikcca39742006-08-24 03:19:22 -04005627void ata_host_init(struct ata_host *host, struct device *dev,
Dan Williams8d8e7d12012-07-09 21:06:08 -07005628 struct ata_port_operations *ops)
Brian Kingb03732f2006-08-07 14:27:10 -05005629{
Jeff Garzikcca39742006-08-24 03:19:22 -04005630 spin_lock_init(&host->lock);
Tejun Heoc0c362b2010-09-06 17:57:14 +02005631 mutex_init(&host->eh_mutex);
Jens Axboe69278f72018-05-11 12:51:10 -06005632 host->n_tags = ATA_MAX_QUEUE;
Jeff Garzikcca39742006-08-24 03:19:22 -04005633 host->dev = dev;
Jeff Garzikcca39742006-08-24 03:19:22 -04005634 host->ops = ops;
Jason Yan2fa4a322018-05-10 11:05:16 +08005635 kref_init(&host->kref);
Brian Kingb03732f2006-08-07 14:27:10 -05005636}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01005637EXPORT_SYMBOL_GPL(ata_host_init);
Brian Kingb03732f2006-08-07 14:27:10 -05005638
Dan Williams9508a662012-01-18 20:47:01 -08005639void __ata_port_probe(struct ata_port *ap)
5640{
5641 struct ata_eh_info *ehi = &ap->link.eh_info;
5642 unsigned long flags;
5643
5644 /* kick EH for boot probing */
5645 spin_lock_irqsave(ap->lock, flags);
5646
5647 ehi->probe_mask |= ATA_ALL_DEVICES;
5648 ehi->action |= ATA_EH_RESET;
5649 ehi->flags |= ATA_EHI_NO_AUTOPSY | ATA_EHI_QUIET;
5650
5651 ap->pflags &= ~ATA_PFLAG_INITIALIZING;
5652 ap->pflags |= ATA_PFLAG_LOADING;
5653 ata_port_schedule_eh(ap);
5654
5655 spin_unlock_irqrestore(ap->lock, flags);
5656}
5657
James Bottomley238c9cf2011-01-23 08:28:33 -06005658int ata_port_probe(struct ata_port *ap)
Arjan van de Ven79318052009-01-04 05:32:28 -08005659{
James Bottomley238c9cf2011-01-23 08:28:33 -06005660 int rc = 0;
Arjan van de Ven886ad092009-01-09 15:54:07 -08005661
Arjan van de Ven79318052009-01-04 05:32:28 -08005662 if (ap->ops->error_handler) {
Dan Williams9508a662012-01-18 20:47:01 -08005663 __ata_port_probe(ap);
Arjan van de Ven79318052009-01-04 05:32:28 -08005664 ata_port_wait_eh(ap);
5665 } else {
5666 DPRINTK("ata%u: bus probe begin\n", ap->print_id);
5667 rc = ata_bus_probe(ap);
5668 DPRINTK("ata%u: bus probe end\n", ap->print_id);
Arjan van de Ven79318052009-01-04 05:32:28 -08005669 }
James Bottomley238c9cf2011-01-23 08:28:33 -06005670 return rc;
5671}
5672
5673
5674static void async_port_probe(void *data, async_cookie_t cookie)
5675{
5676 struct ata_port *ap = data;
Jeff Garzik4fca3772011-02-15 01:13:24 -05005677
James Bottomley238c9cf2011-01-23 08:28:33 -06005678 /*
5679 * If we're not allowed to scan this host in parallel,
5680 * we need to wait until all previous scans have completed
5681 * before going further.
5682 * Jeff Garzik says this is only within a controller, so we
5683 * don't need to wait for port 0, only for later ports.
5684 */
5685 if (!(ap->host->flags & ATA_HOST_PARALLEL_SCAN) && ap->port_no != 0)
5686 async_synchronize_cookie(cookie);
5687
5688 (void)ata_port_probe(ap);
Arjan van de Venf29d3b22009-01-05 15:07:07 -08005689
5690 /* in order to keep device order, we need to synchronize at this point */
5691 async_synchronize_cookie(cookie);
5692
5693 ata_scsi_scan_host(ap, 1);
Arjan van de Ven79318052009-01-04 05:32:28 -08005694}
James Bottomley238c9cf2011-01-23 08:28:33 -06005695
Brian Kingb03732f2006-08-07 14:27:10 -05005696/**
Tejun Heof3187192007-04-17 23:44:07 +09005697 * ata_host_register - register initialized ATA host
5698 * @host: ATA host to register
5699 * @sht: template for SCSI host
Jeff Garzik0cba6322005-05-30 19:49:12 -04005700 *
Tejun Heof3187192007-04-17 23:44:07 +09005701 * Register initialized ATA host. @host is allocated using
5702 * ata_host_alloc() and fully initialized by LLD. This function
5703 * starts ports, registers @host with ATA and SCSI layers and
5704 * probe registered devices.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005705 *
5706 * LOCKING:
Tejun Heof3187192007-04-17 23:44:07 +09005707 * Inherited from calling layer (may sleep).
Linus Torvalds1da177e2005-04-16 15:20:36 -07005708 *
5709 * RETURNS:
Tejun Heof3187192007-04-17 23:44:07 +09005710 * 0 on success, -errno otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005711 */
Tejun Heof3187192007-04-17 23:44:07 +09005712int ata_host_register(struct ata_host *host, struct scsi_host_template *sht)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005713{
Tejun Heof3187192007-04-17 23:44:07 +09005714 int i, rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005715
Jens Axboe69278f72018-05-11 12:51:10 -06005716 host->n_tags = clamp(sht->can_queue, 1, ATA_MAX_QUEUE);
Kevin Hao1871ee12014-07-12 12:08:24 +08005717
Tejun Heof3187192007-04-17 23:44:07 +09005718 /* host must have been started */
5719 if (!(host->flags & ATA_HOST_STARTED)) {
Joe Perchesa44fec12011-04-15 15:51:58 -07005720 dev_err(host->dev, "BUG: trying to register unstarted host\n");
Tejun Heof3187192007-04-17 23:44:07 +09005721 WARN_ON(1);
5722 return -EINVAL;
Alan Cox02f076a2006-09-26 17:35:32 +01005723 }
Tejun Heof0d36ef2007-01-20 16:00:28 +09005724
Tejun Heof3187192007-04-17 23:44:07 +09005725 /* Blow away unused ports. This happens when LLD can't
5726 * determine the exact number of ports to allocate at
5727 * allocation time.
5728 */
5729 for (i = host->n_ports; host->ports[i]; i++)
5730 kfree(host->ports[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005731
Tejun Heof3187192007-04-17 23:44:07 +09005732 /* give ports names and add SCSI hosts */
David Milburne628dc92013-05-14 13:48:40 -05005733 for (i = 0; i < host->n_ports; i++) {
Dan Williams85d67252012-03-10 23:28:46 -08005734 host->ports[i]->print_id = atomic_inc_return(&ata_print_id);
David Milburne628dc92013-05-14 13:48:40 -05005735 host->ports[i]->local_port_no = i + 1;
5736 }
Jeff Garzik4fca3772011-02-15 01:13:24 -05005737
Gwendal Grignoud9027472010-05-25 12:31:38 -07005738 /* Create associated sysfs transport objects */
5739 for (i = 0; i < host->n_ports; i++) {
5740 rc = ata_tport_add(host->dev,host->ports[i]);
5741 if (rc) {
5742 goto err_tadd;
5743 }
5744 }
5745
Tejun Heof3187192007-04-17 23:44:07 +09005746 rc = ata_scsi_add_hosts(host, sht);
Tejun Heoecef7252007-04-17 23:44:06 +09005747 if (rc)
Gwendal Grignoud9027472010-05-25 12:31:38 -07005748 goto err_tadd;
Tejun Heoecef7252007-04-17 23:44:06 +09005749
Tejun Heof3187192007-04-17 23:44:07 +09005750 /* set cable, sata_spd_limit and report */
Jeff Garzikcca39742006-08-24 03:19:22 -04005751 for (i = 0; i < host->n_ports; i++) {
5752 struct ata_port *ap = host->ports[i];
Tejun Heof3187192007-04-17 23:44:07 +09005753 unsigned long xfer_mask;
5754
5755 /* set SATA cable type if still unset */
5756 if (ap->cbl == ATA_CBL_NONE && (ap->flags & ATA_FLAG_SATA))
5757 ap->cbl = ATA_CBL_SATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005758
Tejun Heo5a04bf42006-05-31 18:27:38 +09005759 /* init sata_spd_limit to the current value */
Tejun Heo4fb37a22007-08-06 18:36:23 +09005760 sata_link_init_spd(&ap->link);
Tejun Heob1c72912008-07-31 17:02:43 +09005761 if (ap->slave_link)
5762 sata_link_init_spd(ap->slave_link);
Tejun Heo5a04bf42006-05-31 18:27:38 +09005763
Tejun Heocbcdd872007-08-18 13:14:55 +09005764 /* print per-port info to dmesg */
Tejun Heof3187192007-04-17 23:44:07 +09005765 xfer_mask = ata_pack_xfermask(ap->pio_mask, ap->mwdma_mask,
5766 ap->udma_mask);
5767
Tejun Heoabf6e8e2007-10-09 14:57:25 +09005768 if (!ata_port_is_dummy(ap)) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07005769 ata_port_info(ap, "%cATA max %s %s\n",
5770 (ap->flags & ATA_FLAG_SATA) ? 'S' : 'P',
5771 ata_mode_string(xfer_mask),
5772 ap->link.eh_info.desc);
Tejun Heoabf6e8e2007-10-09 14:57:25 +09005773 ata_ehi_clear_desc(&ap->link.eh_info);
5774 } else
Joe Perchesa9a79df2011-04-15 15:51:59 -07005775 ata_port_info(ap, "DUMMY\n");
Tejun Heof3187192007-04-17 23:44:07 +09005776 }
5777
Vegard Nossumf6005352009-04-08 18:19:39 +02005778 /* perform each probe asynchronously */
Tejun Heof3187192007-04-17 23:44:07 +09005779 for (i = 0; i < host->n_ports; i++) {
5780 struct ata_port *ap = host->ports[i];
Arjan van de Ven79318052009-01-04 05:32:28 -08005781 async_schedule(async_port_probe, ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005782 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005783
Tejun Heof3187192007-04-17 23:44:07 +09005784 return 0;
Gwendal Grignoud9027472010-05-25 12:31:38 -07005785
5786 err_tadd:
5787 while (--i >= 0) {
5788 ata_tport_delete(host->ports[i]);
5789 }
5790 return rc;
5791
Tejun Heof3187192007-04-17 23:44:07 +09005792}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01005793EXPORT_SYMBOL_GPL(ata_host_register);
Tejun Heof3187192007-04-17 23:44:07 +09005794
5795/**
Tejun Heof5cda252007-04-17 23:44:07 +09005796 * ata_host_activate - start host, request IRQ and register it
5797 * @host: target ATA host
5798 * @irq: IRQ to request
5799 * @irq_handler: irq_handler used when requesting IRQ
5800 * @irq_flags: irq_flags used when requesting IRQ
5801 * @sht: scsi_host_template to use when registering the host
5802 *
5803 * After allocating an ATA host and initializing it, most libata
5804 * LLDs perform three steps to activate the host - start host,
Masanari Iidac9b55602016-04-13 23:36:27 +09005805 * request IRQ and register it. This helper takes necessary
Tejun Heof5cda252007-04-17 23:44:07 +09005806 * arguments and performs the three steps in one go.
5807 *
Paul Mundt3d46b2e2007-11-08 11:14:56 +09005808 * An invalid IRQ skips the IRQ registration and expects the host to
5809 * have set polling mode on the port. In this case, @irq_handler
5810 * should be NULL.
5811 *
Tejun Heof5cda252007-04-17 23:44:07 +09005812 * LOCKING:
5813 * Inherited from calling layer (may sleep).
5814 *
5815 * RETURNS:
5816 * 0 on success, -errno otherwise.
5817 */
5818int ata_host_activate(struct ata_host *host, int irq,
5819 irq_handler_t irq_handler, unsigned long irq_flags,
5820 struct scsi_host_template *sht)
5821{
Tejun Heocbcdd872007-08-18 13:14:55 +09005822 int i, rc;
Heiner Kallweit7e22c002015-12-06 21:56:33 +01005823 char *irq_desc;
Tejun Heof5cda252007-04-17 23:44:07 +09005824
5825 rc = ata_host_start(host);
5826 if (rc)
5827 return rc;
5828
Paul Mundt3d46b2e2007-11-08 11:14:56 +09005829 /* Special case for polling mode */
5830 if (!irq) {
5831 WARN_ON(irq_handler);
5832 return ata_host_register(host, sht);
5833 }
5834
Heiner Kallweit7e22c002015-12-06 21:56:33 +01005835 irq_desc = devm_kasprintf(host->dev, GFP_KERNEL, "%s[%s]",
5836 dev_driver_string(host->dev),
5837 dev_name(host->dev));
5838 if (!irq_desc)
5839 return -ENOMEM;
5840
Tejun Heof5cda252007-04-17 23:44:07 +09005841 rc = devm_request_irq(host->dev, irq, irq_handler, irq_flags,
Heiner Kallweit7e22c002015-12-06 21:56:33 +01005842 irq_desc, host);
Tejun Heof5cda252007-04-17 23:44:07 +09005843 if (rc)
5844 return rc;
5845
Tejun Heocbcdd872007-08-18 13:14:55 +09005846 for (i = 0; i < host->n_ports; i++)
5847 ata_port_desc(host->ports[i], "irq %d", irq);
Tejun Heo40318262007-07-03 01:38:47 +09005848
Tejun Heof5cda252007-04-17 23:44:07 +09005849 rc = ata_host_register(host, sht);
5850 /* if failed, just free the IRQ and leave ports alone */
5851 if (rc)
5852 devm_free_irq(host->dev, irq, host);
5853
5854 return rc;
5855}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01005856EXPORT_SYMBOL_GPL(ata_host_activate);
Tejun Heof5cda252007-04-17 23:44:07 +09005857
5858/**
Masanari Iidac9b55602016-04-13 23:36:27 +09005859 * ata_port_detach - Detach ATA port in preparation of device removal
Tejun Heo720ba122006-05-31 18:28:13 +09005860 * @ap: ATA port to be detached
5861 *
5862 * Detach all ATA devices and the associated SCSI devices of @ap;
5863 * then, remove the associated SCSI host. @ap is guaranteed to
5864 * be quiescent on return from this function.
5865 *
5866 * LOCKING:
5867 * Kernel thread context (may sleep).
5868 */
Adrian Bunk741b7762007-10-24 18:23:06 +02005869static void ata_port_detach(struct ata_port *ap)
Tejun Heo720ba122006-05-31 18:28:13 +09005870{
5871 unsigned long flags;
Levente Kurusaa6f9bf42014-05-06 15:57:48 +02005872 struct ata_link *link;
5873 struct ata_device *dev;
Tejun Heo720ba122006-05-31 18:28:13 +09005874
5875 if (!ap->ops->error_handler)
Tejun Heoc3cf30a2006-08-05 03:59:11 +09005876 goto skip_eh;
Tejun Heo720ba122006-05-31 18:28:13 +09005877
5878 /* tell EH we're leaving & flush EH */
Jeff Garzikba6a1302006-06-22 23:46:10 -04005879 spin_lock_irqsave(ap->lock, flags);
Tejun Heob51e9e52006-06-29 01:29:30 +09005880 ap->pflags |= ATA_PFLAG_UNLOADING;
Tejun Heoece180d2008-11-03 20:04:37 +09005881 ata_port_schedule_eh(ap);
Jeff Garzikba6a1302006-06-22 23:46:10 -04005882 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heo720ba122006-05-31 18:28:13 +09005883
Tejun Heoece180d2008-11-03 20:04:37 +09005884 /* wait till EH commits suicide */
Tejun Heo720ba122006-05-31 18:28:13 +09005885 ata_port_wait_eh(ap);
5886
Tejun Heoece180d2008-11-03 20:04:37 +09005887 /* it better be dead now */
5888 WARN_ON(!(ap->pflags & ATA_PFLAG_UNLOADED));
Tejun Heo720ba122006-05-31 18:28:13 +09005889
Tejun Heoafe2c512010-12-14 16:21:17 +01005890 cancel_delayed_work_sync(&ap->hotplug_task);
Tejun Heo720ba122006-05-31 18:28:13 +09005891
Tejun Heoc3cf30a2006-08-05 03:59:11 +09005892 skip_eh:
Levente Kurusaa6f9bf42014-05-06 15:57:48 +02005893 /* clean up zpodd on port removal */
5894 ata_for_each_link(link, ap, HOST_FIRST) {
5895 ata_for_each_dev(dev, link, ALL) {
5896 if (zpodd_dev_enabled(dev))
5897 zpodd_exit(dev);
5898 }
5899 }
Gwendal Grignoud9027472010-05-25 12:31:38 -07005900 if (ap->pmp_link) {
5901 int i;
5902 for (i = 0; i < SATA_PMP_MAX_PORTS; i++)
5903 ata_tlink_delete(&ap->pmp_link[i]);
5904 }
Tejun Heo720ba122006-05-31 18:28:13 +09005905 /* remove the associated SCSI host */
Jeff Garzikcca39742006-08-24 03:19:22 -04005906 scsi_remove_host(ap->scsi_host);
Rafael J. Wysockic5700762013-11-25 13:19:01 +01005907 ata_tport_delete(ap);
Tejun Heo720ba122006-05-31 18:28:13 +09005908}
5909
5910/**
Tejun Heo0529c1592007-01-20 16:00:26 +09005911 * ata_host_detach - Detach all ports of an ATA host
5912 * @host: Host to detach
5913 *
5914 * Detach all ports of @host.
5915 *
5916 * LOCKING:
5917 * Kernel thread context (may sleep).
5918 */
5919void ata_host_detach(struct ata_host *host)
5920{
5921 int i;
5922
John Garry130f4ca2019-10-16 18:19:52 +08005923 /* Ensure ata_port probe has completed */
5924 async_synchronize_full();
5925
Tejun Heo0529c1592007-01-20 16:00:26 +09005926 for (i = 0; i < host->n_ports; i++)
5927 ata_port_detach(host->ports[i]);
Tejun Heo562f0c22007-12-15 15:05:01 +09005928
5929 /* the host is dead now, dissociate ACPI */
5930 ata_acpi_dissociate(host);
Tejun Heo0529c1592007-01-20 16:00:26 +09005931}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01005932EXPORT_SYMBOL_GPL(ata_host_detach);
Tejun Heo0529c1592007-01-20 16:00:26 +09005933
Jeff Garzik374b1872005-08-30 05:42:52 -04005934#ifdef CONFIG_PCI
5935
Edward Falk0baab862005-06-02 18:17:13 -04005936/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07005937 * ata_pci_remove_one - PCI layer callback for device removal
5938 * @pdev: PCI device that was removed
5939 *
Tejun Heob878ca52007-01-20 16:00:28 +09005940 * PCI layer indicates to libata via this hook that hot-unplug or
5941 * module unload event has occurred. Detach all ports. Resource
5942 * release is handled via devres.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005943 *
5944 * LOCKING:
5945 * Inherited from PCI layer (may sleep).
5946 */
Tejun Heof0d36ef2007-01-20 16:00:28 +09005947void ata_pci_remove_one(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005948{
Brian Norris04a3f5b2012-12-03 10:34:41 -08005949 struct ata_host *host = pci_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005950
Tejun Heob878ca52007-01-20 16:00:28 +09005951 ata_host_detach(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005952}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01005953EXPORT_SYMBOL_GPL(ata_pci_remove_one);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005954
Prabhakar Kushwaha10a663a2020-01-25 03:37:29 +00005955void ata_pci_shutdown_one(struct pci_dev *pdev)
5956{
5957 struct ata_host *host = pci_get_drvdata(pdev);
5958 int i;
5959
5960 for (i = 0; i < host->n_ports; i++) {
5961 struct ata_port *ap = host->ports[i];
5962
5963 ap->pflags |= ATA_PFLAG_FROZEN;
5964
5965 /* Disable port interrupts */
5966 if (ap->ops->freeze)
5967 ap->ops->freeze(ap);
5968
5969 /* Stop the port DMA engines */
5970 if (ap->ops->port_stop)
5971 ap->ops->port_stop(ap);
5972 }
5973}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01005974EXPORT_SYMBOL_GPL(ata_pci_shutdown_one);
Prabhakar Kushwaha10a663a2020-01-25 03:37:29 +00005975
Linus Torvalds1da177e2005-04-16 15:20:36 -07005976/* move to PCI subsystem */
Jeff Garzik057ace52005-10-22 14:27:05 -04005977int pci_test_config_bits(struct pci_dev *pdev, const struct pci_bits *bits)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005978{
5979 unsigned long tmp = 0;
5980
5981 switch (bits->width) {
5982 case 1: {
5983 u8 tmp8 = 0;
5984 pci_read_config_byte(pdev, bits->reg, &tmp8);
5985 tmp = tmp8;
5986 break;
5987 }
5988 case 2: {
5989 u16 tmp16 = 0;
5990 pci_read_config_word(pdev, bits->reg, &tmp16);
5991 tmp = tmp16;
5992 break;
5993 }
5994 case 4: {
5995 u32 tmp32 = 0;
5996 pci_read_config_dword(pdev, bits->reg, &tmp32);
5997 tmp = tmp32;
5998 break;
5999 }
6000
6001 default:
6002 return -EINVAL;
6003 }
6004
6005 tmp &= bits->mask;
6006
6007 return (tmp == bits->val) ? 1 : 0;
6008}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01006009EXPORT_SYMBOL_GPL(pci_test_config_bits);
Jens Axboe9b847542006-01-06 09:28:07 +01006010
Tejun Heo6ffa01d2007-03-02 17:32:47 +09006011#ifdef CONFIG_PM
Tejun Heo3c5100c2006-07-26 16:58:33 +09006012void ata_pci_device_do_suspend(struct pci_dev *pdev, pm_message_t mesg)
Jens Axboe9b847542006-01-06 09:28:07 +01006013{
6014 pci_save_state(pdev);
Tejun Heo4c90d972007-02-20 18:14:48 +09006015 pci_disable_device(pdev);
Tejun Heo500530f2006-07-03 16:07:27 +09006016
Rafael J. Wysocki3a2d5b72008-02-23 19:13:25 +01006017 if (mesg.event & PM_EVENT_SLEEP)
Tejun Heo500530f2006-07-03 16:07:27 +09006018 pci_set_power_state(pdev, PCI_D3hot);
Jens Axboe9b847542006-01-06 09:28:07 +01006019}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01006020EXPORT_SYMBOL_GPL(ata_pci_device_do_suspend);
Jens Axboe9b847542006-01-06 09:28:07 +01006021
Tejun Heo553c4aa2006-12-26 19:39:50 +09006022int ata_pci_device_do_resume(struct pci_dev *pdev)
Jens Axboe9b847542006-01-06 09:28:07 +01006023{
Tejun Heo553c4aa2006-12-26 19:39:50 +09006024 int rc;
6025
Jens Axboe9b847542006-01-06 09:28:07 +01006026 pci_set_power_state(pdev, PCI_D0);
6027 pci_restore_state(pdev);
Tejun Heo553c4aa2006-12-26 19:39:50 +09006028
Tejun Heob878ca52007-01-20 16:00:28 +09006029 rc = pcim_enable_device(pdev);
Tejun Heo553c4aa2006-12-26 19:39:50 +09006030 if (rc) {
Joe Perchesa44fec12011-04-15 15:51:58 -07006031 dev_err(&pdev->dev,
6032 "failed to enable device after resume (%d)\n", rc);
Tejun Heo553c4aa2006-12-26 19:39:50 +09006033 return rc;
6034 }
6035
Jens Axboe9b847542006-01-06 09:28:07 +01006036 pci_set_master(pdev);
Tejun Heo553c4aa2006-12-26 19:39:50 +09006037 return 0;
Tejun Heo500530f2006-07-03 16:07:27 +09006038}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01006039EXPORT_SYMBOL_GPL(ata_pci_device_do_resume);
Tejun Heo500530f2006-07-03 16:07:27 +09006040
Tejun Heo3c5100c2006-07-26 16:58:33 +09006041int ata_pci_device_suspend(struct pci_dev *pdev, pm_message_t mesg)
Tejun Heo500530f2006-07-03 16:07:27 +09006042{
Brian Norris04a3f5b2012-12-03 10:34:41 -08006043 struct ata_host *host = pci_get_drvdata(pdev);
Tejun Heo500530f2006-07-03 16:07:27 +09006044 int rc = 0;
6045
Jeff Garzikcca39742006-08-24 03:19:22 -04006046 rc = ata_host_suspend(host, mesg);
Tejun Heo500530f2006-07-03 16:07:27 +09006047 if (rc)
6048 return rc;
6049
Tejun Heo3c5100c2006-07-26 16:58:33 +09006050 ata_pci_device_do_suspend(pdev, mesg);
Tejun Heo500530f2006-07-03 16:07:27 +09006051
6052 return 0;
6053}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01006054EXPORT_SYMBOL_GPL(ata_pci_device_suspend);
Tejun Heo500530f2006-07-03 16:07:27 +09006055
6056int ata_pci_device_resume(struct pci_dev *pdev)
6057{
Brian Norris04a3f5b2012-12-03 10:34:41 -08006058 struct ata_host *host = pci_get_drvdata(pdev);
Tejun Heo553c4aa2006-12-26 19:39:50 +09006059 int rc;
Tejun Heo500530f2006-07-03 16:07:27 +09006060
Tejun Heo553c4aa2006-12-26 19:39:50 +09006061 rc = ata_pci_device_do_resume(pdev);
6062 if (rc == 0)
6063 ata_host_resume(host);
6064 return rc;
Jens Axboe9b847542006-01-06 09:28:07 +01006065}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01006066EXPORT_SYMBOL_GPL(ata_pci_device_resume);
Tejun Heo6ffa01d2007-03-02 17:32:47 +09006067#endif /* CONFIG_PM */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006068#endif /* CONFIG_PCI */
6069
Brian Norrisb7db04d2012-11-02 12:29:32 -07006070/**
6071 * ata_platform_remove_one - Platform layer callback for device removal
6072 * @pdev: Platform device that was removed
6073 *
6074 * Platform layer indicates to libata via this hook that hot-unplug or
6075 * module unload event has occurred. Detach all ports. Resource
6076 * release is handled via devres.
6077 *
6078 * LOCKING:
6079 * Inherited from platform layer (may sleep).
6080 */
6081int ata_platform_remove_one(struct platform_device *pdev)
6082{
6083 struct ata_host *host = platform_get_drvdata(pdev);
6084
6085 ata_host_detach(host);
6086
6087 return 0;
6088}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01006089EXPORT_SYMBOL_GPL(ata_platform_remove_one);
Brian Norrisb7db04d2012-11-02 12:29:32 -07006090
Bartlomiej Zolnierkiewiczbf89b0b2020-03-26 16:58:22 +01006091#ifdef CONFIG_ATA_FORCE
Tejun Heo33267322008-02-13 09:15:09 +09006092static int __init ata_parse_force_one(char **cur,
6093 struct ata_force_ent *force_ent,
6094 const char **reason)
6095{
Rasmus Villemoes0f5f2642015-06-09 15:33:19 +02006096 static const struct ata_force_param force_tbl[] __initconst = {
Tejun Heo33267322008-02-13 09:15:09 +09006097 { "40c", .cbl = ATA_CBL_PATA40 },
6098 { "80c", .cbl = ATA_CBL_PATA80 },
6099 { "short40c", .cbl = ATA_CBL_PATA40_SHORT },
6100 { "unk", .cbl = ATA_CBL_PATA_UNK },
6101 { "ign", .cbl = ATA_CBL_PATA_IGN },
6102 { "sata", .cbl = ATA_CBL_SATA },
6103 { "1.5Gbps", .spd_limit = 1 },
6104 { "3.0Gbps", .spd_limit = 2 },
6105 { "noncq", .horkage_on = ATA_HORKAGE_NONCQ },
6106 { "ncq", .horkage_off = ATA_HORKAGE_NONCQ },
Martin K. Petersend7b16e42015-05-04 21:54:18 -04006107 { "noncqtrim", .horkage_on = ATA_HORKAGE_NO_NCQ_TRIM },
6108 { "ncqtrim", .horkage_off = ATA_HORKAGE_NO_NCQ_TRIM },
Tejun Heo43c9c592010-05-23 12:59:11 +02006109 { "dump_id", .horkage_on = ATA_HORKAGE_DUMP_ID },
Tejun Heo33267322008-02-13 09:15:09 +09006110 { "pio0", .xfer_mask = 1 << (ATA_SHIFT_PIO + 0) },
6111 { "pio1", .xfer_mask = 1 << (ATA_SHIFT_PIO + 1) },
6112 { "pio2", .xfer_mask = 1 << (ATA_SHIFT_PIO + 2) },
6113 { "pio3", .xfer_mask = 1 << (ATA_SHIFT_PIO + 3) },
6114 { "pio4", .xfer_mask = 1 << (ATA_SHIFT_PIO + 4) },
6115 { "pio5", .xfer_mask = 1 << (ATA_SHIFT_PIO + 5) },
6116 { "pio6", .xfer_mask = 1 << (ATA_SHIFT_PIO + 6) },
6117 { "mwdma0", .xfer_mask = 1 << (ATA_SHIFT_MWDMA + 0) },
6118 { "mwdma1", .xfer_mask = 1 << (ATA_SHIFT_MWDMA + 1) },
6119 { "mwdma2", .xfer_mask = 1 << (ATA_SHIFT_MWDMA + 2) },
6120 { "mwdma3", .xfer_mask = 1 << (ATA_SHIFT_MWDMA + 3) },
6121 { "mwdma4", .xfer_mask = 1 << (ATA_SHIFT_MWDMA + 4) },
6122 { "udma0", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 0) },
6123 { "udma16", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 0) },
6124 { "udma/16", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 0) },
6125 { "udma1", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 1) },
6126 { "udma25", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 1) },
6127 { "udma/25", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 1) },
6128 { "udma2", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 2) },
6129 { "udma33", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 2) },
6130 { "udma/33", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 2) },
6131 { "udma3", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 3) },
6132 { "udma44", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 3) },
6133 { "udma/44", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 3) },
6134 { "udma4", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 4) },
6135 { "udma66", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 4) },
6136 { "udma/66", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 4) },
6137 { "udma5", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 5) },
6138 { "udma100", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 5) },
6139 { "udma/100", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 5) },
6140 { "udma6", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 6) },
6141 { "udma133", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 6) },
6142 { "udma/133", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 6) },
6143 { "udma7", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 7) },
Tejun Heo05944bd2008-08-13 20:19:09 +09006144 { "nohrst", .lflags = ATA_LFLAG_NO_HRST },
6145 { "nosrst", .lflags = ATA_LFLAG_NO_SRST },
6146 { "norst", .lflags = ATA_LFLAG_NO_HRST | ATA_LFLAG_NO_SRST },
Dan Williamsca6d43b2012-06-21 23:41:41 -07006147 { "rstonce", .lflags = ATA_LFLAG_RST_ONCE },
Vincent Pelletier966fbe12013-05-21 22:30:58 +02006148 { "atapi_dmadir", .horkage_on = ATA_HORKAGE_ATAPI_DMADIR },
Robin H. Johnsonb8bd6dc2013-12-16 09:31:19 -08006149 { "disable", .horkage_on = ATA_HORKAGE_DISABLE },
Tejun Heo33267322008-02-13 09:15:09 +09006150 };
6151 char *start = *cur, *p = *cur;
6152 char *id, *val, *endp;
6153 const struct ata_force_param *match_fp = NULL;
6154 int nr_matches = 0, i;
6155
6156 /* find where this param ends and update *cur */
6157 while (*p != '\0' && *p != ',')
6158 p++;
6159
6160 if (*p == '\0')
6161 *cur = p;
6162 else
6163 *cur = p + 1;
6164
6165 *p = '\0';
6166
6167 /* parse */
6168 p = strchr(start, ':');
6169 if (!p) {
6170 val = strstrip(start);
6171 goto parse_val;
6172 }
6173 *p = '\0';
6174
6175 id = strstrip(start);
6176 val = strstrip(p + 1);
6177
6178 /* parse id */
6179 p = strchr(id, '.');
6180 if (p) {
6181 *p++ = '\0';
6182 force_ent->device = simple_strtoul(p, &endp, 10);
6183 if (p == endp || *endp != '\0') {
6184 *reason = "invalid device";
6185 return -EINVAL;
6186 }
6187 }
6188
6189 force_ent->port = simple_strtoul(id, &endp, 10);
Tejun Heof7cf69a2017-05-31 14:26:26 -04006190 if (id == endp || *endp != '\0') {
Tejun Heo33267322008-02-13 09:15:09 +09006191 *reason = "invalid port/link";
6192 return -EINVAL;
6193 }
6194
6195 parse_val:
6196 /* parse val, allow shortcuts so that both 1.5 and 1.5Gbps work */
6197 for (i = 0; i < ARRAY_SIZE(force_tbl); i++) {
6198 const struct ata_force_param *fp = &force_tbl[i];
6199
6200 if (strncasecmp(val, fp->name, strlen(val)))
6201 continue;
6202
6203 nr_matches++;
6204 match_fp = fp;
6205
6206 if (strcasecmp(val, fp->name) == 0) {
6207 nr_matches = 1;
6208 break;
6209 }
6210 }
6211
6212 if (!nr_matches) {
6213 *reason = "unknown value";
6214 return -EINVAL;
6215 }
6216 if (nr_matches > 1) {
Arvind Yadav9de55352017-10-28 23:51:47 +05306217 *reason = "ambiguous value";
Tejun Heo33267322008-02-13 09:15:09 +09006218 return -EINVAL;
6219 }
6220
6221 force_ent->param = *match_fp;
6222
6223 return 0;
6224}
6225
6226static void __init ata_parse_force_param(void)
6227{
6228 int idx = 0, size = 1;
6229 int last_port = -1, last_device = -1;
6230 char *p, *cur, *next;
6231
6232 /* calculate maximum number of params and allocate force_tbl */
6233 for (p = ata_force_param_buf; *p; p++)
6234 if (*p == ',')
6235 size++;
6236
Kees Cook6396bb22018-06-12 14:03:40 -07006237 ata_force_tbl = kcalloc(size, sizeof(ata_force_tbl[0]), GFP_KERNEL);
Tejun Heo33267322008-02-13 09:15:09 +09006238 if (!ata_force_tbl) {
6239 printk(KERN_WARNING "ata: failed to extend force table, "
6240 "libata.force ignored\n");
6241 return;
6242 }
6243
6244 /* parse and populate the table */
6245 for (cur = ata_force_param_buf; *cur != '\0'; cur = next) {
6246 const char *reason = "";
6247 struct ata_force_ent te = { .port = -1, .device = -1 };
6248
6249 next = cur;
6250 if (ata_parse_force_one(&next, &te, &reason)) {
6251 printk(KERN_WARNING "ata: failed to parse force "
6252 "parameter \"%s\" (%s)\n",
6253 cur, reason);
6254 continue;
6255 }
6256
6257 if (te.port == -1) {
6258 te.port = last_port;
6259 te.device = last_device;
6260 }
6261
6262 ata_force_tbl[idx++] = te;
6263
6264 last_port = te.port;
6265 last_device = te.device;
6266 }
6267
6268 ata_force_tbl_size = idx;
6269}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006270
Bartlomiej Zolnierkiewiczbf89b0b2020-03-26 16:58:22 +01006271static void ata_free_force_param(void)
6272{
6273 kfree(ata_force_tbl);
6274}
6275#else
6276static inline void ata_parse_force_param(void) { }
6277static inline void ata_free_force_param(void) { }
6278#endif
6279
Linus Torvalds1da177e2005-04-16 15:20:36 -07006280static int __init ata_init(void)
6281{
Gwendal Grignoud9027472010-05-25 12:31:38 -07006282 int rc;
Tejun Heo270390e2010-05-10 21:41:35 +02006283
Tejun Heo33267322008-02-13 09:15:09 +09006284 ata_parse_force_param();
6285
Tejun Heo270390e2010-05-10 21:41:35 +02006286 rc = ata_sff_init();
Tejun Heoad72cf92010-07-02 10:03:52 +02006287 if (rc) {
Bartlomiej Zolnierkiewiczbf89b0b2020-03-26 16:58:22 +01006288 ata_free_force_param();
Tejun Heoad72cf92010-07-02 10:03:52 +02006289 return rc;
6290 }
Tejun Heo453b07a2006-05-31 18:27:42 +09006291
Gwendal Grignoud9027472010-05-25 12:31:38 -07006292 libata_transport_init();
6293 ata_scsi_transport_template = ata_attach_transport();
6294 if (!ata_scsi_transport_template) {
6295 ata_sff_exit();
6296 rc = -ENOMEM;
6297 goto err_out;
Jeff Garzik4fca3772011-02-15 01:13:24 -05006298 }
Gwendal Grignoud9027472010-05-25 12:31:38 -07006299
Linus Torvalds1da177e2005-04-16 15:20:36 -07006300 printk(KERN_DEBUG "libata version " DRV_VERSION " loaded.\n");
6301 return 0;
Gwendal Grignoud9027472010-05-25 12:31:38 -07006302
6303err_out:
6304 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006305}
6306
6307static void __exit ata_exit(void)
6308{
Gwendal Grignoud9027472010-05-25 12:31:38 -07006309 ata_release_transport(ata_scsi_transport_template);
6310 libata_transport_exit();
Tejun Heo270390e2010-05-10 21:41:35 +02006311 ata_sff_exit();
Bartlomiej Zolnierkiewiczbf89b0b2020-03-26 16:58:22 +01006312 ata_free_force_param();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006313}
6314
Brian Kinga4625082006-11-13 16:32:36 -06006315subsys_initcall(ata_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006316module_exit(ata_exit);
6317
Akinobu Mita9990b6f2010-04-12 21:11:41 +09006318static DEFINE_RATELIMIT_STATE(ratelimit, HZ / 5, 1);
Jeff Garzik67846b32005-10-05 02:58:32 -04006319
6320int ata_ratelimit(void)
6321{
Akinobu Mita9990b6f2010-04-12 21:11:41 +09006322 return __ratelimit(&ratelimit);
Jeff Garzik67846b32005-10-05 02:58:32 -04006323}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01006324EXPORT_SYMBOL_GPL(ata_ratelimit);
Jeff Garzik67846b32005-10-05 02:58:32 -04006325
Tejun Heoc0c362b2010-09-06 17:57:14 +02006326/**
6327 * ata_msleep - ATA EH owner aware msleep
6328 * @ap: ATA port to attribute the sleep to
6329 * @msecs: duration to sleep in milliseconds
6330 *
6331 * Sleeps @msecs. If the current task is owner of @ap's EH, the
6332 * ownership is released before going to sleep and reacquired
6333 * after the sleep is complete. IOW, other ports sharing the
6334 * @ap->host will be allowed to own the EH while this task is
6335 * sleeping.
6336 *
6337 * LOCKING:
6338 * Might sleep.
6339 */
Tejun Heo97750ce2010-09-06 17:56:29 +02006340void ata_msleep(struct ata_port *ap, unsigned int msecs)
6341{
Tejun Heoc0c362b2010-09-06 17:57:14 +02006342 bool owns_eh = ap && ap->host->eh_owner == current;
6343
6344 if (owns_eh)
6345 ata_eh_release(ap);
6346
Anil Veliyankara Madam848c3922016-01-07 21:18:52 -08006347 if (msecs < 20) {
6348 unsigned long usecs = msecs * USEC_PER_MSEC;
6349 usleep_range(usecs, usecs + 50);
6350 } else {
6351 msleep(msecs);
6352 }
Tejun Heoc0c362b2010-09-06 17:57:14 +02006353
6354 if (owns_eh)
6355 ata_eh_acquire(ap);
Tejun Heo97750ce2010-09-06 17:56:29 +02006356}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01006357EXPORT_SYMBOL_GPL(ata_msleep);
Tejun Heo97750ce2010-09-06 17:56:29 +02006358
Tejun Heoc22daff2006-04-11 22:22:29 +09006359/**
6360 * ata_wait_register - wait until register value changes
Tejun Heo97750ce2010-09-06 17:56:29 +02006361 * @ap: ATA port to wait register for, can be NULL
Tejun Heoc22daff2006-04-11 22:22:29 +09006362 * @reg: IO-mapped register
6363 * @mask: Mask to apply to read register value
6364 * @val: Wait condition
Tejun Heo341c2c92008-05-20 02:17:51 +09006365 * @interval: polling interval in milliseconds
6366 * @timeout: timeout in milliseconds
Tejun Heoc22daff2006-04-11 22:22:29 +09006367 *
6368 * Waiting for some bits of register to change is a common
6369 * operation for ATA controllers. This function reads 32bit LE
6370 * IO-mapped register @reg and tests for the following condition.
6371 *
6372 * (*@reg & mask) != val
6373 *
6374 * If the condition is met, it returns; otherwise, the process is
6375 * repeated after @interval_msec until timeout.
6376 *
6377 * LOCKING:
6378 * Kernel thread context (may sleep)
6379 *
6380 * RETURNS:
6381 * The final register value.
6382 */
Tejun Heo97750ce2010-09-06 17:56:29 +02006383u32 ata_wait_register(struct ata_port *ap, void __iomem *reg, u32 mask, u32 val,
Tejun Heo341c2c92008-05-20 02:17:51 +09006384 unsigned long interval, unsigned long timeout)
Tejun Heoc22daff2006-04-11 22:22:29 +09006385{
Tejun Heo341c2c92008-05-20 02:17:51 +09006386 unsigned long deadline;
Tejun Heoc22daff2006-04-11 22:22:29 +09006387 u32 tmp;
6388
6389 tmp = ioread32(reg);
6390
6391 /* Calculate timeout _after_ the first read to make sure
6392 * preceding writes reach the controller before starting to
6393 * eat away the timeout.
6394 */
Tejun Heo341c2c92008-05-20 02:17:51 +09006395 deadline = ata_deadline(jiffies, timeout);
Tejun Heoc22daff2006-04-11 22:22:29 +09006396
Tejun Heo341c2c92008-05-20 02:17:51 +09006397 while ((tmp & mask) == val && time_before(jiffies, deadline)) {
Tejun Heo97750ce2010-09-06 17:56:29 +02006398 ata_msleep(ap, interval);
Tejun Heoc22daff2006-04-11 22:22:29 +09006399 tmp = ioread32(reg);
6400 }
6401
6402 return tmp;
6403}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01006404EXPORT_SYMBOL_GPL(ata_wait_register);
Tejun Heoc22daff2006-04-11 22:22:29 +09006405
Linus Torvalds1da177e2005-04-16 15:20:36 -07006406/*
Tejun Heodd5b06c2006-08-10 16:59:12 +09006407 * Dummy port_ops
6408 */
Tejun Heodd5b06c2006-08-10 16:59:12 +09006409static unsigned int ata_dummy_qc_issue(struct ata_queued_cmd *qc)
6410{
6411 return AC_ERR_SYSTEM;
6412}
6413
Tejun Heo182d7bb2008-04-07 22:47:21 +09006414static void ata_dummy_error_handler(struct ata_port *ap)
6415{
6416 /* truly dummy */
6417}
6418
Tejun Heo029cfd62008-03-25 12:22:49 +09006419struct ata_port_operations ata_dummy_port_ops = {
Tejun Heodd5b06c2006-08-10 16:59:12 +09006420 .qc_prep = ata_noop_qc_prep,
6421 .qc_issue = ata_dummy_qc_issue,
Tejun Heo182d7bb2008-04-07 22:47:21 +09006422 .error_handler = ata_dummy_error_handler,
Dan Williamse4a9c372012-06-21 23:25:27 -07006423 .sched_eh = ata_std_sched_eh,
6424 .end_eh = ata_std_end_eh,
Tejun Heodd5b06c2006-08-10 16:59:12 +09006425};
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01006426EXPORT_SYMBOL_GPL(ata_dummy_port_ops);
Tejun Heodd5b06c2006-08-10 16:59:12 +09006427
Tejun Heo21b0ad42007-04-17 23:44:07 +09006428const struct ata_port_info ata_dummy_port_info = {
6429 .port_ops = &ata_dummy_port_ops,
6430};
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01006431EXPORT_SYMBOL_GPL(ata_dummy_port_info);
Tejun Heo21b0ad42007-04-17 23:44:07 +09006432
Tejun Heodd5b06c2006-08-10 16:59:12 +09006433/*
Joe Perchesa9a79df2011-04-15 15:51:59 -07006434 * Utility print functions
6435 */
Joe Perchesd7bead12014-09-22 09:52:18 -07006436void ata_port_printk(const struct ata_port *ap, const char *level,
6437 const char *fmt, ...)
Joe Perchesa9a79df2011-04-15 15:51:59 -07006438{
6439 struct va_format vaf;
6440 va_list args;
Joe Perchesa9a79df2011-04-15 15:51:59 -07006441
6442 va_start(args, fmt);
6443
6444 vaf.fmt = fmt;
6445 vaf.va = &args;
6446
Joe Perchesd7bead12014-09-22 09:52:18 -07006447 printk("%sata%u: %pV", level, ap->print_id, &vaf);
Joe Perchesa9a79df2011-04-15 15:51:59 -07006448
6449 va_end(args);
Joe Perchesa9a79df2011-04-15 15:51:59 -07006450}
6451EXPORT_SYMBOL(ata_port_printk);
6452
Joe Perchesd7bead12014-09-22 09:52:18 -07006453void ata_link_printk(const struct ata_link *link, const char *level,
6454 const char *fmt, ...)
Joe Perchesa9a79df2011-04-15 15:51:59 -07006455{
6456 struct va_format vaf;
6457 va_list args;
Joe Perchesa9a79df2011-04-15 15:51:59 -07006458
6459 va_start(args, fmt);
6460
6461 vaf.fmt = fmt;
6462 vaf.va = &args;
6463
6464 if (sata_pmp_attached(link->ap) || link->ap->slave_link)
Joe Perchesd7bead12014-09-22 09:52:18 -07006465 printk("%sata%u.%02u: %pV",
6466 level, link->ap->print_id, link->pmp, &vaf);
Joe Perchesa9a79df2011-04-15 15:51:59 -07006467 else
Joe Perchesd7bead12014-09-22 09:52:18 -07006468 printk("%sata%u: %pV",
6469 level, link->ap->print_id, &vaf);
Joe Perchesa9a79df2011-04-15 15:51:59 -07006470
6471 va_end(args);
Joe Perchesa9a79df2011-04-15 15:51:59 -07006472}
6473EXPORT_SYMBOL(ata_link_printk);
6474
Joe Perchesd7bead12014-09-22 09:52:18 -07006475void ata_dev_printk(const struct ata_device *dev, const char *level,
Joe Perchesa9a79df2011-04-15 15:51:59 -07006476 const char *fmt, ...)
6477{
6478 struct va_format vaf;
6479 va_list args;
Joe Perchesa9a79df2011-04-15 15:51:59 -07006480
6481 va_start(args, fmt);
6482
6483 vaf.fmt = fmt;
6484 vaf.va = &args;
6485
Joe Perchesd7bead12014-09-22 09:52:18 -07006486 printk("%sata%u.%02u: %pV",
6487 level, dev->link->ap->print_id, dev->link->pmp + dev->devno,
6488 &vaf);
Joe Perchesa9a79df2011-04-15 15:51:59 -07006489
6490 va_end(args);
Joe Perchesa9a79df2011-04-15 15:51:59 -07006491}
6492EXPORT_SYMBOL(ata_dev_printk);
6493
Joe Perches06296a12011-04-15 15:52:00 -07006494void ata_print_version(const struct device *dev, const char *version)
6495{
6496 dev_printk(KERN_DEBUG, dev, "version %s\n", version);
6497}
6498EXPORT_SYMBOL(ata_print_version);