ata: pata_cs5535: Don't build on UML
[linux-2.6-block.git] / drivers / ata / libata-core.c
CommitLineData
c82ee6d3 1// SPDX-License-Identifier: GPL-2.0-or-later
1da177e4 2/*
af36d7f0
JG
3 * libata-core.c - helper library for ATA
4 *
af36d7f0
JG
5 * Copyright 2003-2004 Red Hat, Inc. All rights reserved.
6 * Copyright 2003-2004 Jeff Garzik
7 *
af36d7f0 8 * libata documentation is available via 'make {ps|pdf}docs',
19285f3c 9 * as Documentation/driver-api/libata.rst
af36d7f0
JG
10 *
11 * Hardware documentation available from http://www.t13.org/ and
12 * http://www.sata-io.org/
13 *
92c52c52
AC
14 * 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)
a52fbcfc
BZ
21 *
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.
1da177e4
LT
26 */
27
1da177e4
LT
28#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>
1da177e4
LT
34#include <linux/spinlock.h>
35#include <linux/blkdev.h>
36#include <linux/delay.h>
37#include <linux/timer.h>
848c3920 38#include <linux/time.h>
1da177e4
LT
39#include <linux/interrupt.h>
40#include <linux/completion.h>
41#include <linux/suspend.h>
42#include <linux/workqueue.h>
378f058c 43#include <linux/scatterlist.h>
2dcb407e 44#include <linux/io.h>
e18086d6 45#include <linux/log2.h>
5a0e3ad6 46#include <linux/slab.h>
428ac5fc 47#include <linux/glob.h>
1da177e4 48#include <scsi/scsi.h>
193515d5 49#include <scsi/scsi_cmnd.h>
1da177e4
LT
50#include <scsi/scsi_host.h>
51#include <linux/libata.h>
1da177e4 52#include <asm/byteorder.h>
fe5af0cc 53#include <asm/unaligned.h>
140b5e59 54#include <linux/cdrom.h>
9990b6f3 55#include <linux/ratelimit.h>
eb25cb99 56#include <linux/leds.h>
9ee4f393 57#include <linux/pm_runtime.h>
b7db04d9 58#include <linux/platform_device.h>
bbf5a097 59#include <asm/setup.h>
1da177e4 60
255c03d1
HR
61#define CREATE_TRACE_POINTS
62#include <trace/events/libata.h>
63
1da177e4 64#include "libata.h"
d9027470 65#include "libata-transport.h"
fda0efc5 66
029cfd6b 67const struct ata_port_operations ata_base_port_ops = {
0aa1113d 68 .prereset = ata_std_prereset,
203c75b8 69 .postreset = ata_std_postreset,
a1efdaba 70 .error_handler = ata_std_error_handler,
e4a9c373
DW
71 .sched_eh = ata_std_sched_eh,
72 .end_eh = ata_std_end_eh,
029cfd6b
TH
73};
74
75const struct ata_port_operations sata_port_ops = {
76 .inherits = &ata_base_port_ops,
77
78 .qc_defer = ata_std_qc_defer,
57c9efdf 79 .hardreset = sata_std_hardreset,
029cfd6b 80};
a52fbcfc 81EXPORT_SYMBOL_GPL(sata_port_ops);
029cfd6b 82
3373efd8
TH
83static unsigned int ata_dev_init_params(struct ata_device *dev,
84 u16 heads, u16 sectors);
85static unsigned int ata_dev_set_xfermode(struct ata_device *dev);
86static void ata_dev_xfermask(struct ata_device *dev);
75683fe7 87static unsigned long ata_dev_blacklisted(const struct ata_device *dev);
1da177e4 88
a78f57af 89atomic_t ata_print_id = ATOMIC_INIT(0);
1da177e4 90
bf89b0bf 91#ifdef CONFIG_ATA_FORCE
33267325
TH
92struct ata_force_param {
93 const char *name;
8ba5a45c
BZ
94 u8 cbl;
95 u8 spd_limit;
f0a6d77b 96 unsigned int xfer_mask;
33267325
TH
97 unsigned int horkage_on;
98 unsigned int horkage_off;
3af9ca4d
DLM
99 u16 lflags_on;
100 u16 lflags_off;
33267325
TH
101};
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
bbf5a097 112static char ata_force_param_buf[COMMAND_LINE_SIZE] __initdata;
7afb4222
TH
113/* param_buf is thrown away after initialization, disallow read */
114module_param_string(force, ata_force_param_buf, sizeof(ata_force_param_buf), 0);
8c27ceff 115MODULE_PARM_DESC(force, "Force ATA configurations including cable type, link speed and transfer mode (see Documentation/admin-guide/kernel-parameters.rst for details)");
bf89b0bf 116#endif
33267325 117
2486fa56 118static int atapi_enabled = 1;
1623c81e 119module_param(atapi_enabled, int, 0444);
ad5d8eac 120MODULE_PARM_DESC(atapi_enabled, "Enable discovery of ATAPI devices (0=off, 1=on [default])");
1623c81e 121
c5c61bda 122static int atapi_dmadir = 0;
95de719a 123module_param(atapi_dmadir, int, 0444);
ad5d8eac 124MODULE_PARM_DESC(atapi_dmadir, "Enable ATAPI DMADIR bridge support (0=off [default], 1=on)");
95de719a 125
baf4fdfa
ML
126int atapi_passthru16 = 1;
127module_param(atapi_passthru16, int, 0444);
ad5d8eac 128MODULE_PARM_DESC(atapi_passthru16, "Enable ATA_16 passthru for ATAPI devices (0=off, 1=on [default])");
baf4fdfa 129
c3c013a2
JG
130int libata_fua = 0;
131module_param_named(fua, libata_fua, int, 0444);
ad5d8eac 132MODULE_PARM_DESC(fua, "FUA support (0=off [default], 1=on)");
c3c013a2 133
2dcb407e 134static int ata_ignore_hpa;
1e999736
AC
135module_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
b3a70601
AC
138static 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
87fbc5a0 142static int ata_probe_timeout;
a8601e5f
AM
143module_param(ata_probe_timeout, int, 0444);
144MODULE_PARM_DESC(ata_probe_timeout, "Set ATA probing timeout (seconds)");
145
6ebe9d86 146int libata_noacpi = 0;
d7d0dad6 147module_param_named(noacpi, libata_noacpi, int, 0444);
ad5d8eac 148MODULE_PARM_DESC(noacpi, "Disable the use of ACPI in probe/suspend/resume (0=off [default], 1=on)");
11ef697b 149
ae8d4ee7
AC
150int libata_allow_tpm = 0;
151module_param_named(allow_tpm, libata_allow_tpm, int, 0444);
ad5d8eac 152MODULE_PARM_DESC(allow_tpm, "Permit the use of TPM commands (0=off [default], 1=on)");
ae8d4ee7 153
e7ecd435
TH
154static 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
1da177e4
LT
158MODULE_AUTHOR("Jeff Garzik");
159MODULE_DESCRIPTION("Library module for ATA devices");
160MODULE_LICENSE("GPL");
161MODULE_VERSION(DRV_VERSION);
162
891fd7c6
DLM
163static inline bool ata_dev_print_info(struct ata_device *dev)
164{
165 struct ata_eh_context *ehc = &dev->link->eh_context;
166
167 return ehc->i.flags & ATA_EHI_PRINTINFO;
168}
0baab86b 169
9913ff8a
TH
170static bool ata_sstatus_online(u32 sstatus)
171{
172 return (sstatus & 0xf) == 0x3;
173}
174
1eca4365
TH
175/**
176 * ata_link_next - link iteration helper
177 * @link: the previous link, NULL to start
178 * @ap: ATA port containing links to iterate
179 * @mode: iteration mode, one of ATA_LITER_*
180 *
181 * LOCKING:
182 * Host lock or EH context.
aadffb68 183 *
1eca4365
TH
184 * RETURNS:
185 * Pointer to the next link.
aadffb68 186 */
1eca4365
TH
187struct ata_link *ata_link_next(struct ata_link *link, struct ata_port *ap,
188 enum ata_link_iter_mode mode)
aadffb68 189{
1eca4365
TH
190 BUG_ON(mode != ATA_LITER_EDGE &&
191 mode != ATA_LITER_PMP_FIRST && mode != ATA_LITER_HOST_FIRST);
192
aadffb68 193 /* NULL link indicates start of iteration */
1eca4365
TH
194 if (!link)
195 switch (mode) {
196 case ATA_LITER_EDGE:
197 case ATA_LITER_PMP_FIRST:
198 if (sata_pmp_attached(ap))
199 return ap->pmp_link;
df561f66 200 fallthrough;
1eca4365
TH
201 case ATA_LITER_HOST_FIRST:
202 return &ap->link;
203 }
aadffb68 204
1eca4365
TH
205 /* we just iterated over the host link, what's next? */
206 if (link == &ap->link)
207 switch (mode) {
208 case ATA_LITER_HOST_FIRST:
209 if (sata_pmp_attached(ap))
210 return ap->pmp_link;
df561f66 211 fallthrough;
1eca4365
TH
212 case ATA_LITER_PMP_FIRST:
213 if (unlikely(ap->slave_link))
b1c72916 214 return ap->slave_link;
df561f66 215 fallthrough;
1eca4365 216 case ATA_LITER_EDGE:
aadffb68 217 return NULL;
b1c72916 218 }
aadffb68 219
b1c72916
TH
220 /* slave_link excludes PMP */
221 if (unlikely(link == ap->slave_link))
222 return NULL;
223
1eca4365 224 /* we were over a PMP link */
aadffb68
TH
225 if (++link < ap->pmp_link + ap->nr_pmp_links)
226 return link;
1eca4365
TH
227
228 if (mode == ATA_LITER_PMP_FIRST)
229 return &ap->link;
230
aadffb68
TH
231 return NULL;
232}
a52fbcfc 233EXPORT_SYMBOL_GPL(ata_link_next);
aadffb68 234
1eca4365
TH
235/**
236 * ata_dev_next - device iteration helper
237 * @dev: the previous device, NULL to start
238 * @link: ATA link containing devices to iterate
239 * @mode: iteration mode, one of ATA_DITER_*
240 *
241 * LOCKING:
242 * Host lock or EH context.
243 *
244 * RETURNS:
245 * Pointer to the next device.
246 */
247struct ata_device *ata_dev_next(struct ata_device *dev, struct ata_link *link,
248 enum ata_dev_iter_mode mode)
249{
250 BUG_ON(mode != ATA_DITER_ENABLED && mode != ATA_DITER_ENABLED_REVERSE &&
251 mode != ATA_DITER_ALL && mode != ATA_DITER_ALL_REVERSE);
252
253 /* NULL dev indicates start of iteration */
254 if (!dev)
255 switch (mode) {
256 case ATA_DITER_ENABLED:
257 case ATA_DITER_ALL:
258 dev = link->device;
259 goto check;
260 case ATA_DITER_ENABLED_REVERSE:
261 case ATA_DITER_ALL_REVERSE:
262 dev = link->device + ata_link_max_devices(link) - 1;
263 goto check;
264 }
265
266 next:
267 /* move to the next one */
268 switch (mode) {
269 case ATA_DITER_ENABLED:
270 case ATA_DITER_ALL:
271 if (++dev < link->device + ata_link_max_devices(link))
272 goto check;
273 return NULL;
274 case ATA_DITER_ENABLED_REVERSE:
275 case ATA_DITER_ALL_REVERSE:
276 if (--dev >= link->device)
277 goto check;
278 return NULL;
279 }
280
281 check:
282 if ((mode == ATA_DITER_ENABLED || mode == ATA_DITER_ENABLED_REVERSE) &&
283 !ata_dev_enabled(dev))
284 goto next;
285 return dev;
286}
a52fbcfc 287EXPORT_SYMBOL_GPL(ata_dev_next);
1eca4365 288
b1c72916
TH
289/**
290 * ata_dev_phys_link - find physical link for a device
291 * @dev: ATA device to look up physical link for
292 *
293 * Look up physical link which @dev is attached to. Note that
294 * this is different from @dev->link only when @dev is on slave
295 * link. For all other cases, it's the same as @dev->link.
296 *
297 * LOCKING:
298 * Don't care.
299 *
300 * RETURNS:
301 * Pointer to the found physical link.
302 */
303struct ata_link *ata_dev_phys_link(struct ata_device *dev)
304{
305 struct ata_port *ap = dev->link->ap;
306
307 if (!ap->slave_link)
308 return dev->link;
309 if (!dev->devno)
310 return &ap->link;
311 return ap->slave_link;
312}
313
bf89b0bf 314#ifdef CONFIG_ATA_FORCE
33267325
TH
315/**
316 * ata_force_cbl - force cable type according to libata.force
4cdfa1b3 317 * @ap: ATA port of interest
33267325
TH
318 *
319 * Force cable type according to libata.force and whine about it.
320 * The last entry which has matching port number is used, so it
321 * can be specified as part of device force parameters. For
322 * example, both "a:40c,1.00:udma4" and "1.00:40c,udma4" have the
323 * same effect.
324 *
325 * LOCKING:
326 * EH context.
327 */
328void ata_force_cbl(struct ata_port *ap)
329{
330 int i;
331
332 for (i = ata_force_tbl_size - 1; i >= 0; i--) {
333 const struct ata_force_ent *fe = &ata_force_tbl[i];
334
335 if (fe->port != -1 && fe->port != ap->print_id)
336 continue;
337
338 if (fe->param.cbl == ATA_CBL_NONE)
339 continue;
340
341 ap->cbl = fe->param.cbl;
a9a79dfe 342 ata_port_notice(ap, "FORCE: cable set to %s\n", fe->param.name);
33267325
TH
343 return;
344 }
345}
346
347/**
05944bdf 348 * ata_force_link_limits - force link limits according to libata.force
33267325
TH
349 * @link: ATA link of interest
350 *
05944bdf
TH
351 * Force link flags and SATA spd limit according to libata.force
352 * and whine about it. When only the port part is specified
353 * (e.g. 1:), the limit applies to all links connected to both
354 * the host link and all fan-out ports connected via PMP. If the
355 * device part is specified as 0 (e.g. 1.00:), it specifies the
356 * first fan-out link not the host link. Device number 15 always
b1c72916
TH
357 * points to the host link whether PMP is attached or not. If the
358 * controller has slave link, device number 16 points to it.
33267325
TH
359 *
360 * LOCKING:
361 * EH context.
362 */
05944bdf 363static void ata_force_link_limits(struct ata_link *link)
33267325 364{
05944bdf 365 bool did_spd = false;
b1c72916
TH
366 int linkno = link->pmp;
367 int i;
33267325
TH
368
369 if (ata_is_host_link(link))
b1c72916 370 linkno += 15;
33267325
TH
371
372 for (i = ata_force_tbl_size - 1; i >= 0; i--) {
373 const struct ata_force_ent *fe = &ata_force_tbl[i];
374
375 if (fe->port != -1 && fe->port != link->ap->print_id)
376 continue;
377
378 if (fe->device != -1 && fe->device != linkno)
379 continue;
380
05944bdf
TH
381 /* only honor the first spd limit */
382 if (!did_spd && fe->param.spd_limit) {
383 link->hw_sata_spd_limit = (1 << fe->param.spd_limit) - 1;
a9a79dfe 384 ata_link_notice(link, "FORCE: PHY spd limit set to %s\n",
05944bdf
TH
385 fe->param.name);
386 did_spd = true;
387 }
33267325 388
05944bdf 389 /* let lflags stack */
3af9ca4d
DLM
390 if (fe->param.lflags_on) {
391 link->flags |= fe->param.lflags_on;
a9a79dfe 392 ata_link_notice(link,
05944bdf 393 "FORCE: link flag 0x%x forced -> 0x%x\n",
3af9ca4d
DLM
394 fe->param.lflags_on, link->flags);
395 }
396 if (fe->param.lflags_off) {
397 link->flags &= ~fe->param.lflags_off;
398 ata_link_notice(link,
399 "FORCE: link flag 0x%x cleared -> 0x%x\n",
400 fe->param.lflags_off, link->flags);
05944bdf 401 }
33267325
TH
402 }
403}
404
405/**
406 * ata_force_xfermask - force xfermask according to libata.force
407 * @dev: ATA device of interest
408 *
409 * Force xfer_mask according to libata.force and whine about it.
410 * For consistency with link selection, device number 15 selects
411 * the first device connected to the host link.
412 *
413 * LOCKING:
414 * EH context.
415 */
416static void ata_force_xfermask(struct ata_device *dev)
417{
418 int devno = dev->link->pmp + dev->devno;
419 int alt_devno = devno;
420 int i;
421
b1c72916
TH
422 /* allow n.15/16 for devices attached to host port */
423 if (ata_is_host_link(dev->link))
424 alt_devno += 15;
33267325
TH
425
426 for (i = ata_force_tbl_size - 1; i >= 0; i--) {
427 const struct ata_force_ent *fe = &ata_force_tbl[i];
f0a6d77b 428 unsigned int pio_mask, mwdma_mask, udma_mask;
33267325
TH
429
430 if (fe->port != -1 && fe->port != dev->link->ap->print_id)
431 continue;
432
433 if (fe->device != -1 && fe->device != devno &&
434 fe->device != alt_devno)
435 continue;
436
437 if (!fe->param.xfer_mask)
438 continue;
439
440 ata_unpack_xfermask(fe->param.xfer_mask,
441 &pio_mask, &mwdma_mask, &udma_mask);
442 if (udma_mask)
443 dev->udma_mask = udma_mask;
444 else if (mwdma_mask) {
445 dev->udma_mask = 0;
446 dev->mwdma_mask = mwdma_mask;
447 } else {
448 dev->udma_mask = 0;
449 dev->mwdma_mask = 0;
450 dev->pio_mask = pio_mask;
451 }
452
a9a79dfe
JP
453 ata_dev_notice(dev, "FORCE: xfer_mask set to %s\n",
454 fe->param.name);
33267325
TH
455 return;
456 }
457}
458
459/**
460 * ata_force_horkage - force horkage according to libata.force
461 * @dev: ATA device of interest
462 *
463 * Force horkage according to libata.force and whine about it.
464 * For consistency with link selection, device number 15 selects
465 * the first device connected to the host link.
466 *
467 * LOCKING:
468 * EH context.
469 */
470static void ata_force_horkage(struct ata_device *dev)
471{
472 int devno = dev->link->pmp + dev->devno;
473 int alt_devno = devno;
474 int i;
475
b1c72916
TH
476 /* allow n.15/16 for devices attached to host port */
477 if (ata_is_host_link(dev->link))
478 alt_devno += 15;
33267325
TH
479
480 for (i = 0; i < ata_force_tbl_size; i++) {
481 const struct ata_force_ent *fe = &ata_force_tbl[i];
482
483 if (fe->port != -1 && fe->port != dev->link->ap->print_id)
484 continue;
485
486 if (fe->device != -1 && fe->device != devno &&
487 fe->device != alt_devno)
488 continue;
489
490 if (!(~dev->horkage & fe->param.horkage_on) &&
491 !(dev->horkage & fe->param.horkage_off))
492 continue;
493
494 dev->horkage |= fe->param.horkage_on;
495 dev->horkage &= ~fe->param.horkage_off;
496
a9a79dfe
JP
497 ata_dev_notice(dev, "FORCE: horkage modified (%s)\n",
498 fe->param.name);
33267325
TH
499 }
500}
bf89b0bf
BZ
501#else
502static inline void ata_force_link_limits(struct ata_link *link) { }
503static inline void ata_force_xfermask(struct ata_device *dev) { }
504static inline void ata_force_horkage(struct ata_device *dev) { }
505#endif
33267325 506
436d34b3
TH
507/**
508 * atapi_cmd_type - Determine ATAPI command type from SCSI opcode
509 * @opcode: SCSI opcode
510 *
511 * Determine ATAPI command type from @opcode.
512 *
513 * LOCKING:
514 * None.
515 *
516 * RETURNS:
517 * ATAPI_{READ|WRITE|READ_CD|PASS_THRU|MISC}
518 */
519int atapi_cmd_type(u8 opcode)
520{
521 switch (opcode) {
522 case GPCMD_READ_10:
523 case GPCMD_READ_12:
524 return ATAPI_READ;
525
526 case GPCMD_WRITE_10:
527 case GPCMD_WRITE_12:
528 case GPCMD_WRITE_AND_VERIFY_10:
529 return ATAPI_WRITE;
530
531 case GPCMD_READ_CD:
532 case GPCMD_READ_CD_MSF:
533 return ATAPI_READ_CD;
534
e52dcc48
TH
535 case ATA_16:
536 case ATA_12:
537 if (atapi_passthru16)
538 return ATAPI_PASS_THRU;
df561f66 539 fallthrough;
436d34b3
TH
540 default:
541 return ATAPI_MISC;
542 }
543}
a52fbcfc 544EXPORT_SYMBOL_GPL(atapi_cmd_type);
436d34b3 545
8cbd6df1
AL
546static const u8 ata_rw_cmds[] = {
547 /* pio multi */
548 ATA_CMD_READ_MULTI,
549 ATA_CMD_WRITE_MULTI,
550 ATA_CMD_READ_MULTI_EXT,
551 ATA_CMD_WRITE_MULTI_EXT,
9a3dccc4
TH
552 0,
553 0,
554 0,
555 ATA_CMD_WRITE_MULTI_FUA_EXT,
8cbd6df1
AL
556 /* pio */
557 ATA_CMD_PIO_READ,
558 ATA_CMD_PIO_WRITE,
559 ATA_CMD_PIO_READ_EXT,
560 ATA_CMD_PIO_WRITE_EXT,
9a3dccc4
TH
561 0,
562 0,
563 0,
564 0,
8cbd6df1
AL
565 /* dma */
566 ATA_CMD_READ,
567 ATA_CMD_WRITE,
568 ATA_CMD_READ_EXT,
9a3dccc4
TH
569 ATA_CMD_WRITE_EXT,
570 0,
571 0,
572 0,
573 ATA_CMD_WRITE_FUA_EXT
8cbd6df1 574};
1da177e4
LT
575
576/**
8cbd6df1 577 * ata_rwcmd_protocol - set taskfile r/w commands and protocol
bd056d7e
TH
578 * @tf: command to examine and configure
579 * @dev: device tf belongs to
1da177e4 580 *
2e9edbf8 581 * Examine the device configuration and tf->flags to calculate
8cbd6df1 582 * the proper read/write commands and protocol to use.
1da177e4
LT
583 *
584 * LOCKING:
585 * caller.
586 */
bd056d7e 587static int ata_rwcmd_protocol(struct ata_taskfile *tf, struct ata_device *dev)
1da177e4 588{
9a3dccc4 589 u8 cmd;
1da177e4 590
9a3dccc4 591 int index, fua, lba48, write;
2e9edbf8 592
9a3dccc4 593 fua = (tf->flags & ATA_TFLAG_FUA) ? 4 : 0;
8cbd6df1
AL
594 lba48 = (tf->flags & ATA_TFLAG_LBA48) ? 2 : 0;
595 write = (tf->flags & ATA_TFLAG_WRITE) ? 1 : 0;
1da177e4 596
8cbd6df1
AL
597 if (dev->flags & ATA_DFLAG_PIO) {
598 tf->protocol = ATA_PROT_PIO;
9a3dccc4 599 index = dev->multi_count ? 0 : 8;
9af5c9c9 600 } else if (lba48 && (dev->link->ap->flags & ATA_FLAG_PIO_LBA48)) {
8d238e01
AC
601 /* Unable to use DMA due to host limitation */
602 tf->protocol = ATA_PROT_PIO;
0565c26d 603 index = dev->multi_count ? 0 : 8;
8cbd6df1
AL
604 } else {
605 tf->protocol = ATA_PROT_DMA;
9a3dccc4 606 index = 16;
8cbd6df1 607 }
1da177e4 608
9a3dccc4
TH
609 cmd = ata_rw_cmds[index + fua + lba48 + write];
610 if (cmd) {
611 tf->command = cmd;
612 return 0;
613 }
614 return -1;
1da177e4
LT
615}
616
35b649fe
TH
617/**
618 * ata_tf_read_block - Read block address from ATA taskfile
619 * @tf: ATA taskfile of interest
620 * @dev: ATA device @tf belongs to
621 *
622 * LOCKING:
623 * None.
624 *
625 * Read block address from @tf. This function can handle all
626 * three address formats - LBA, LBA48 and CHS. tf->protocol and
627 * flags select the address format to use.
628 *
629 * RETURNS:
630 * Block address read from @tf.
631 */
cffd1ee9 632u64 ata_tf_read_block(const struct ata_taskfile *tf, struct ata_device *dev)
35b649fe
TH
633{
634 u64 block = 0;
635
fe16d4f2 636 if (tf->flags & ATA_TFLAG_LBA) {
35b649fe
TH
637 if (tf->flags & ATA_TFLAG_LBA48) {
638 block |= (u64)tf->hob_lbah << 40;
639 block |= (u64)tf->hob_lbam << 32;
44901a96 640 block |= (u64)tf->hob_lbal << 24;
35b649fe
TH
641 } else
642 block |= (tf->device & 0xf) << 24;
643
644 block |= tf->lbah << 16;
645 block |= tf->lbam << 8;
646 block |= tf->lbal;
647 } else {
648 u32 cyl, head, sect;
649
650 cyl = tf->lbam | (tf->lbah << 8);
651 head = tf->device & 0xf;
652 sect = tf->lbal;
653
ac8672ea 654 if (!sect) {
a9a79dfe
JP
655 ata_dev_warn(dev,
656 "device reported invalid CHS sector 0\n");
cffd1ee9 657 return U64_MAX;
ac8672ea
TH
658 }
659
660 block = (cyl * dev->heads + head) * dev->sectors + sect - 1;
35b649fe
TH
661 }
662
663 return block;
664}
665
bd056d7e
TH
666/**
667 * ata_build_rw_tf - Build ATA taskfile for given read/write request
066de3b9 668 * @qc: Metadata associated with the taskfile to build
bd056d7e
TH
669 * @block: Block address
670 * @n_block: Number of blocks
671 * @tf_flags: RW/FUA etc...
8e061784 672 * @class: IO priority class
bd056d7e
TH
673 *
674 * LOCKING:
675 * None.
676 *
066de3b9
DLM
677 * Build ATA taskfile for the command @qc for read/write request described
678 * by @block, @n_block, @tf_flags and @class.
bd056d7e
TH
679 *
680 * RETURNS:
681 *
682 * 0 on success, -ERANGE if the request is too large for @dev,
683 * -EINVAL if the request is invalid.
684 */
066de3b9
DLM
685int ata_build_rw_tf(struct ata_queued_cmd *qc, u64 block, u32 n_block,
686 unsigned int tf_flags, int class)
bd056d7e 687{
066de3b9
DLM
688 struct ata_taskfile *tf = &qc->tf;
689 struct ata_device *dev = qc->dev;
690
bd056d7e
TH
691 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
692 tf->flags |= tf_flags;
693
066de3b9 694 if (ata_ncq_enabled(dev)) {
bd056d7e
TH
695 /* yay, NCQ */
696 if (!lba_48_ok(block, n_block))
697 return -ERANGE;
698
699 tf->protocol = ATA_PROT_NCQ;
700 tf->flags |= ATA_TFLAG_LBA | ATA_TFLAG_LBA48;
701
702 if (tf->flags & ATA_TFLAG_WRITE)
703 tf->command = ATA_CMD_FPDMA_WRITE;
704 else
705 tf->command = ATA_CMD_FPDMA_READ;
706
066de3b9 707 tf->nsect = qc->hw_tag << 3;
bd056d7e
TH
708 tf->hob_feature = (n_block >> 8) & 0xff;
709 tf->feature = n_block & 0xff;
710
711 tf->hob_lbah = (block >> 40) & 0xff;
712 tf->hob_lbam = (block >> 32) & 0xff;
713 tf->hob_lbal = (block >> 24) & 0xff;
714 tf->lbah = (block >> 16) & 0xff;
715 tf->lbam = (block >> 8) & 0xff;
716 tf->lbal = block & 0xff;
717
9ca7cfa4 718 tf->device = ATA_LBA;
bd056d7e
TH
719 if (tf->flags & ATA_TFLAG_FUA)
720 tf->device |= 1 << 7;
8e061784 721
e00923c5 722 if (dev->flags & ATA_DFLAG_NCQ_PRIO_ENABLED &&
2360fa18
DLM
723 class == IOPRIO_CLASS_RT)
724 tf->hob_nsect |= ATA_PRIO_HIGH << ATA_SHIFT_PRIO;
bd056d7e
TH
725 } else if (dev->flags & ATA_DFLAG_LBA) {
726 tf->flags |= ATA_TFLAG_LBA;
727
728 if (lba_28_ok(block, n_block)) {
729 /* use LBA28 */
730 tf->device |= (block >> 24) & 0xf;
731 } else if (lba_48_ok(block, n_block)) {
732 if (!(dev->flags & ATA_DFLAG_LBA48))
733 return -ERANGE;
734
735 /* use LBA48 */
736 tf->flags |= ATA_TFLAG_LBA48;
737
738 tf->hob_nsect = (n_block >> 8) & 0xff;
739
740 tf->hob_lbah = (block >> 40) & 0xff;
741 tf->hob_lbam = (block >> 32) & 0xff;
742 tf->hob_lbal = (block >> 24) & 0xff;
743 } else
744 /* request too large even for LBA48 */
745 return -ERANGE;
746
747 if (unlikely(ata_rwcmd_protocol(tf, dev) < 0))
748 return -EINVAL;
749
750 tf->nsect = n_block & 0xff;
751
752 tf->lbah = (block >> 16) & 0xff;
753 tf->lbam = (block >> 8) & 0xff;
754 tf->lbal = block & 0xff;
755
756 tf->device |= ATA_LBA;
757 } else {
758 /* CHS */
759 u32 sect, head, cyl, track;
760
761 /* The request -may- be too large for CHS addressing. */
762 if (!lba_28_ok(block, n_block))
763 return -ERANGE;
764
765 if (unlikely(ata_rwcmd_protocol(tf, dev) < 0))
766 return -EINVAL;
767
768 /* Convert LBA to CHS */
769 track = (u32)block / dev->sectors;
770 cyl = track / dev->heads;
771 head = track % dev->heads;
772 sect = (u32)block % dev->sectors + 1;
773
bd056d7e
TH
774 /* Check whether the converted CHS can fit.
775 Cylinder: 0-65535
776 Head: 0-15
777 Sector: 1-255*/
778 if ((cyl >> 16) || (head >> 4) || (sect >> 8) || (!sect))
779 return -ERANGE;
780
781 tf->nsect = n_block & 0xff; /* Sector count 0 means 256 sectors */
782 tf->lbal = sect;
783 tf->lbam = cyl;
784 tf->lbah = cyl >> 8;
785 tf->device |= head;
786 }
787
788 return 0;
789}
790
cb95d562
TH
791/**
792 * ata_pack_xfermask - Pack pio, mwdma and udma masks into xfer_mask
793 * @pio_mask: pio_mask
794 * @mwdma_mask: mwdma_mask
795 * @udma_mask: udma_mask
796 *
797 * Pack @pio_mask, @mwdma_mask and @udma_mask into a single
798 * unsigned int xfer_mask.
799 *
800 * LOCKING:
801 * None.
802 *
803 * RETURNS:
804 * Packed xfer_mask.
805 */
f0a6d77b
SS
806unsigned int ata_pack_xfermask(unsigned int pio_mask,
807 unsigned int mwdma_mask,
808 unsigned int udma_mask)
cb95d562 809{
f0a6d77b 810 return ((pio_mask << ATA_SHIFT_PIO) & ATA_MASK_PIO) |
cb95d562
TH
811 ((mwdma_mask << ATA_SHIFT_MWDMA) & ATA_MASK_MWDMA) |
812 ((udma_mask << ATA_SHIFT_UDMA) & ATA_MASK_UDMA);
813}
a52fbcfc 814EXPORT_SYMBOL_GPL(ata_pack_xfermask);
cb95d562 815
c0489e4e
TH
816/**
817 * ata_unpack_xfermask - Unpack xfer_mask into pio, mwdma and udma masks
818 * @xfer_mask: xfer_mask to unpack
819 * @pio_mask: resulting pio_mask
820 * @mwdma_mask: resulting mwdma_mask
821 * @udma_mask: resulting udma_mask
822 *
823 * Unpack @xfer_mask into @pio_mask, @mwdma_mask and @udma_mask.
c9b5560a 824 * Any NULL destination masks will be ignored.
c0489e4e 825 */
f0a6d77b
SS
826void ata_unpack_xfermask(unsigned int xfer_mask, unsigned int *pio_mask,
827 unsigned int *mwdma_mask, unsigned int *udma_mask)
c0489e4e
TH
828{
829 if (pio_mask)
830 *pio_mask = (xfer_mask & ATA_MASK_PIO) >> ATA_SHIFT_PIO;
831 if (mwdma_mask)
832 *mwdma_mask = (xfer_mask & ATA_MASK_MWDMA) >> ATA_SHIFT_MWDMA;
833 if (udma_mask)
834 *udma_mask = (xfer_mask & ATA_MASK_UDMA) >> ATA_SHIFT_UDMA;
835}
836
cb95d562 837static const struct ata_xfer_ent {
be9a50c8 838 int shift, bits;
cb95d562
TH
839 u8 base;
840} ata_xfer_tbl[] = {
70cd071e
TH
841 { ATA_SHIFT_PIO, ATA_NR_PIO_MODES, XFER_PIO_0 },
842 { ATA_SHIFT_MWDMA, ATA_NR_MWDMA_MODES, XFER_MW_DMA_0 },
843 { ATA_SHIFT_UDMA, ATA_NR_UDMA_MODES, XFER_UDMA_0 },
cb95d562
TH
844 { -1, },
845};
846
847/**
848 * ata_xfer_mask2mode - Find matching XFER_* for the given xfer_mask
849 * @xfer_mask: xfer_mask of interest
850 *
851 * Return matching XFER_* value for @xfer_mask. Only the highest
852 * bit of @xfer_mask is considered.
853 *
854 * LOCKING:
855 * None.
856 *
857 * RETURNS:
70cd071e 858 * Matching XFER_* value, 0xff if no match found.
cb95d562 859 */
f0a6d77b 860u8 ata_xfer_mask2mode(unsigned int xfer_mask)
cb95d562
TH
861{
862 int highbit = fls(xfer_mask) - 1;
863 const struct ata_xfer_ent *ent;
864
865 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
866 if (highbit >= ent->shift && highbit < ent->shift + ent->bits)
867 return ent->base + highbit - ent->shift;
70cd071e 868 return 0xff;
cb95d562 869}
a52fbcfc 870EXPORT_SYMBOL_GPL(ata_xfer_mask2mode);
cb95d562
TH
871
872/**
873 * ata_xfer_mode2mask - Find matching xfer_mask for XFER_*
874 * @xfer_mode: XFER_* of interest
875 *
876 * Return matching xfer_mask for @xfer_mode.
877 *
878 * LOCKING:
879 * None.
880 *
881 * RETURNS:
882 * Matching xfer_mask, 0 if no match found.
883 */
f0a6d77b 884unsigned int ata_xfer_mode2mask(u8 xfer_mode)
cb95d562
TH
885{
886 const struct ata_xfer_ent *ent;
887
888 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
889 if (xfer_mode >= ent->base && xfer_mode < ent->base + ent->bits)
70cd071e
TH
890 return ((2 << (ent->shift + xfer_mode - ent->base)) - 1)
891 & ~((1 << ent->shift) - 1);
cb95d562
TH
892 return 0;
893}
a52fbcfc 894EXPORT_SYMBOL_GPL(ata_xfer_mode2mask);
cb95d562
TH
895
896/**
897 * ata_xfer_mode2shift - Find matching xfer_shift for XFER_*
898 * @xfer_mode: XFER_* of interest
899 *
900 * Return matching xfer_shift for @xfer_mode.
901 *
902 * LOCKING:
903 * None.
904 *
905 * RETURNS:
906 * Matching xfer_shift, -1 if no match found.
907 */
a28c1ab3 908int ata_xfer_mode2shift(u8 xfer_mode)
cb95d562
TH
909{
910 const struct ata_xfer_ent *ent;
911
912 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
913 if (xfer_mode >= ent->base && xfer_mode < ent->base + ent->bits)
914 return ent->shift;
915 return -1;
916}
a52fbcfc 917EXPORT_SYMBOL_GPL(ata_xfer_mode2shift);
cb95d562 918
1da177e4 919/**
1da7b0d0
TH
920 * ata_mode_string - convert xfer_mask to string
921 * @xfer_mask: mask of bits supported; only highest bit counts.
1da177e4
LT
922 *
923 * Determine string which represents the highest speed
1da7b0d0 924 * (highest bit in @modemask).
1da177e4
LT
925 *
926 * LOCKING:
927 * None.
928 *
929 * RETURNS:
930 * Constant C string representing highest speed listed in
1da7b0d0 931 * @mode_mask, or the constant C string "<n/a>".
1da177e4 932 */
f0a6d77b 933const char *ata_mode_string(unsigned int xfer_mask)
1da177e4 934{
75f554bc
TH
935 static const char * const xfer_mode_str[] = {
936 "PIO0",
937 "PIO1",
938 "PIO2",
939 "PIO3",
940 "PIO4",
b352e57d
AC
941 "PIO5",
942 "PIO6",
75f554bc
TH
943 "MWDMA0",
944 "MWDMA1",
945 "MWDMA2",
b352e57d
AC
946 "MWDMA3",
947 "MWDMA4",
75f554bc
TH
948 "UDMA/16",
949 "UDMA/25",
950 "UDMA/33",
951 "UDMA/44",
952 "UDMA/66",
953 "UDMA/100",
954 "UDMA/133",
955 "UDMA7",
956 };
1da7b0d0 957 int highbit;
1da177e4 958
1da7b0d0
TH
959 highbit = fls(xfer_mask) - 1;
960 if (highbit >= 0 && highbit < ARRAY_SIZE(xfer_mode_str))
961 return xfer_mode_str[highbit];
1da177e4 962 return "<n/a>";
1da177e4 963}
a52fbcfc 964EXPORT_SYMBOL_GPL(ata_mode_string);
1da177e4 965
d9027470 966const char *sata_spd_string(unsigned int spd)
4c360c81
TH
967{
968 static const char * const spd_str[] = {
969 "1.5 Gbps",
970 "3.0 Gbps",
8522ee25 971 "6.0 Gbps",
4c360c81
TH
972 };
973
974 if (spd == 0 || (spd - 1) >= ARRAY_SIZE(spd_str))
975 return "<unknown>";
976 return spd_str[spd - 1];
977}
978
1da177e4
LT
979/**
980 * ata_dev_classify - determine device type based on ATA-spec signature
981 * @tf: ATA taskfile register set for device to be identified
982 *
983 * Determine from taskfile register contents whether a device is
984 * ATA or ATAPI, as per "Signature and persistence" section
985 * of ATA/PI spec (volume 1, sect 5.14).
986 *
987 * LOCKING:
988 * None.
989 *
990 * RETURNS:
9162c657
HR
991 * Device type, %ATA_DEV_ATA, %ATA_DEV_ATAPI, %ATA_DEV_PMP,
992 * %ATA_DEV_ZAC, or %ATA_DEV_UNKNOWN the event of failure.
1da177e4 993 */
057ace5e 994unsigned int ata_dev_classify(const struct ata_taskfile *tf)
1da177e4
LT
995{
996 /* Apple's open source Darwin code hints that some devices only
997 * put a proper signature into the LBA mid/high registers,
998 * So, we only check those. It's sufficient for uniqueness.
633273a3
TH
999 *
1000 * ATA/ATAPI-7 (d1532v1r1: Feb. 19, 2003) specified separate
1001 * signatures for ATA and ATAPI devices attached on SerialATA,
1002 * 0x3c/0xc3 and 0x69/0x96 respectively. However, SerialATA
1003 * spec has never mentioned about using different signatures
1004 * for ATA/ATAPI devices. Then, Serial ATA II: Port
1005 * Multiplier specification began to use 0x69/0x96 to identify
1006 * port multpliers and 0x3c/0xc3 to identify SEMB device.
1007 * ATA/ATAPI-7 dropped descriptions about 0x3c/0xc3 and
1008 * 0x69/0x96 shortly and described them as reserved for
1009 * SerialATA.
1010 *
1011 * We follow the current spec and consider that 0x69/0x96
1012 * identifies a port multiplier and 0x3c/0xc3 a SEMB device.
79b42bab
TH
1013 * Unfortunately, WDC WD1600JS-62MHB5 (a hard drive) reports
1014 * SEMB signature. This is worked around in
1015 * ata_dev_read_id().
1da177e4 1016 */
6c952a0d 1017 if (tf->lbam == 0 && tf->lbah == 0)
1da177e4 1018 return ATA_DEV_ATA;
1da177e4 1019
6c952a0d 1020 if (tf->lbam == 0x14 && tf->lbah == 0xeb)
1da177e4 1021 return ATA_DEV_ATAPI;
1da177e4 1022
6c952a0d 1023 if (tf->lbam == 0x69 && tf->lbah == 0x96)
633273a3 1024 return ATA_DEV_PMP;
633273a3 1025
6c952a0d 1026 if (tf->lbam == 0x3c && tf->lbah == 0xc3)
79b42bab 1027 return ATA_DEV_SEMB;
633273a3 1028
6c952a0d 1029 if (tf->lbam == 0xcd && tf->lbah == 0xab)
9162c657 1030 return ATA_DEV_ZAC;
9162c657 1031
1da177e4
LT
1032 return ATA_DEV_UNKNOWN;
1033}
a52fbcfc 1034EXPORT_SYMBOL_GPL(ata_dev_classify);
1da177e4 1035
1da177e4 1036/**
6a62a04d 1037 * ata_id_string - Convert IDENTIFY DEVICE page into string
1da177e4
LT
1038 * @id: IDENTIFY DEVICE results we will examine
1039 * @s: string into which data is output
1040 * @ofs: offset into identify device page
1041 * @len: length of string to return. must be an even number.
1042 *
1043 * The strings in the IDENTIFY DEVICE page are broken up into
1044 * 16-bit chunks. Run through the string, and output each
1045 * 8-bit chunk linearly, regardless of platform.
1046 *
1047 * LOCKING:
1048 * caller.
1049 */
1050
6a62a04d
TH
1051void ata_id_string(const u16 *id, unsigned char *s,
1052 unsigned int ofs, unsigned int len)
1da177e4
LT
1053{
1054 unsigned int c;
1055
963e4975
AC
1056 BUG_ON(len & 1);
1057
1da177e4
LT
1058 while (len > 0) {
1059 c = id[ofs] >> 8;
1060 *s = c;
1061 s++;
1062
1063 c = id[ofs] & 0xff;
1064 *s = c;
1065 s++;
1066
1067 ofs++;
1068 len -= 2;
1069 }
1070}
a52fbcfc 1071EXPORT_SYMBOL_GPL(ata_id_string);
1da177e4 1072
0e949ff3 1073/**
6a62a04d 1074 * ata_id_c_string - Convert IDENTIFY DEVICE page into C string
0e949ff3
TH
1075 * @id: IDENTIFY DEVICE results we will examine
1076 * @s: string into which data is output
1077 * @ofs: offset into identify device page
1078 * @len: length of string to return. must be an odd number.
1079 *
6a62a04d 1080 * This function is identical to ata_id_string except that it
0e949ff3
TH
1081 * trims trailing spaces and terminates the resulting string with
1082 * null. @len must be actual maximum length (even number) + 1.
1083 *
1084 * LOCKING:
1085 * caller.
1086 */
6a62a04d
TH
1087void ata_id_c_string(const u16 *id, unsigned char *s,
1088 unsigned int ofs, unsigned int len)
0e949ff3
TH
1089{
1090 unsigned char *p;
1091
6a62a04d 1092 ata_id_string(id, s, ofs, len - 1);
0e949ff3
TH
1093
1094 p = s + strnlen(s, len - 1);
1095 while (p > s && p[-1] == ' ')
1096 p--;
1097 *p = '\0';
1098}
a52fbcfc 1099EXPORT_SYMBOL_GPL(ata_id_c_string);
0baab86b 1100
db6f8759
TH
1101static u64 ata_id_n_sectors(const u16 *id)
1102{
1103 if (ata_id_has_lba(id)) {
1104 if (ata_id_has_lba48(id))
968e594a 1105 return ata_id_u64(id, ATA_ID_LBA_CAPACITY_2);
5eb8deb4
SS
1106
1107 return ata_id_u32(id, ATA_ID_LBA_CAPACITY);
db6f8759 1108 }
5eb8deb4
SS
1109
1110 if (ata_id_current_chs_valid(id))
1111 return (u32)id[ATA_ID_CUR_CYLS] * (u32)id[ATA_ID_CUR_HEADS] *
1112 (u32)id[ATA_ID_CUR_SECTORS];
1113
1114 return (u32)id[ATA_ID_CYLS] * (u32)id[ATA_ID_HEADS] *
1115 (u32)id[ATA_ID_SECTORS];
db6f8759
TH
1116}
1117
a5987e0a 1118u64 ata_tf_to_lba48(const struct ata_taskfile *tf)
1e999736
AC
1119{
1120 u64 sectors = 0;
1121
1122 sectors |= ((u64)(tf->hob_lbah & 0xff)) << 40;
1123 sectors |= ((u64)(tf->hob_lbam & 0xff)) << 32;
ba14a9c2 1124 sectors |= ((u64)(tf->hob_lbal & 0xff)) << 24;
1e999736
AC
1125 sectors |= (tf->lbah & 0xff) << 16;
1126 sectors |= (tf->lbam & 0xff) << 8;
1127 sectors |= (tf->lbal & 0xff);
1128
a5987e0a 1129 return sectors;
1e999736
AC
1130}
1131
a5987e0a 1132u64 ata_tf_to_lba(const struct ata_taskfile *tf)
1e999736
AC
1133{
1134 u64 sectors = 0;
1135
1136 sectors |= (tf->device & 0x0f) << 24;
1137 sectors |= (tf->lbah & 0xff) << 16;
1138 sectors |= (tf->lbam & 0xff) << 8;
1139 sectors |= (tf->lbal & 0xff);
1140
a5987e0a 1141 return sectors;
1e999736
AC
1142}
1143
1144/**
c728a914
TH
1145 * ata_read_native_max_address - Read native max address
1146 * @dev: target device
1147 * @max_sectors: out parameter for the result native max address
1e999736 1148 *
c728a914
TH
1149 * Perform an LBA48 or LBA28 native size query upon the device in
1150 * question.
1e999736 1151 *
c728a914
TH
1152 * RETURNS:
1153 * 0 on success, -EACCES if command is aborted by the drive.
1154 * -EIO on other errors.
1e999736 1155 */
c728a914 1156static int ata_read_native_max_address(struct ata_device *dev, u64 *max_sectors)
1e999736 1157{
c728a914 1158 unsigned int err_mask;
1e999736 1159 struct ata_taskfile tf;
c728a914 1160 int lba48 = ata_id_has_lba48(dev->id);
1e999736
AC
1161
1162 ata_tf_init(dev, &tf);
1163
c728a914 1164 /* always clear all address registers */
1e999736 1165 tf.flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
1e999736 1166
c728a914
TH
1167 if (lba48) {
1168 tf.command = ATA_CMD_READ_NATIVE_MAX_EXT;
1169 tf.flags |= ATA_TFLAG_LBA48;
1170 } else
1171 tf.command = ATA_CMD_READ_NATIVE_MAX;
1e999736 1172
bd18bc04 1173 tf.protocol = ATA_PROT_NODATA;
c728a914
TH
1174 tf.device |= ATA_LBA;
1175
2b789108 1176 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
c728a914 1177 if (err_mask) {
a9a79dfe
JP
1178 ata_dev_warn(dev,
1179 "failed to read native max address (err_mask=0x%x)\n",
1180 err_mask);
efcef265 1181 if (err_mask == AC_ERR_DEV && (tf.error & ATA_ABORTED))
c728a914
TH
1182 return -EACCES;
1183 return -EIO;
1184 }
1e999736 1185
c728a914 1186 if (lba48)
a5987e0a 1187 *max_sectors = ata_tf_to_lba48(&tf) + 1;
c728a914 1188 else
a5987e0a 1189 *max_sectors = ata_tf_to_lba(&tf) + 1;
2dcb407e 1190 if (dev->horkage & ATA_HORKAGE_HPA_SIZE)
93328e11 1191 (*max_sectors)--;
c728a914 1192 return 0;
1e999736
AC
1193}
1194
1195/**
c728a914
TH
1196 * ata_set_max_sectors - Set max sectors
1197 * @dev: target device
6b38d1d1 1198 * @new_sectors: new max sectors value to set for the device
1e999736 1199 *
c728a914
TH
1200 * Set max sectors of @dev to @new_sectors.
1201 *
1202 * RETURNS:
1203 * 0 on success, -EACCES if command is aborted or denied (due to
1204 * previous non-volatile SET_MAX) by the drive. -EIO on other
1205 * errors.
1e999736 1206 */
05027adc 1207static int ata_set_max_sectors(struct ata_device *dev, u64 new_sectors)
1e999736 1208{
c728a914 1209 unsigned int err_mask;
1e999736 1210 struct ata_taskfile tf;
c728a914 1211 int lba48 = ata_id_has_lba48(dev->id);
1e999736
AC
1212
1213 new_sectors--;
1214
1215 ata_tf_init(dev, &tf);
1216
1e999736 1217 tf.flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
c728a914
TH
1218
1219 if (lba48) {
1220 tf.command = ATA_CMD_SET_MAX_EXT;
1221 tf.flags |= ATA_TFLAG_LBA48;
1222
1223 tf.hob_lbal = (new_sectors >> 24) & 0xff;
1224 tf.hob_lbam = (new_sectors >> 32) & 0xff;
1225 tf.hob_lbah = (new_sectors >> 40) & 0xff;
1e582ba4 1226 } else {
c728a914
TH
1227 tf.command = ATA_CMD_SET_MAX;
1228
1e582ba4
TH
1229 tf.device |= (new_sectors >> 24) & 0xf;
1230 }
1231
bd18bc04 1232 tf.protocol = ATA_PROT_NODATA;
c728a914 1233 tf.device |= ATA_LBA;
1e999736
AC
1234
1235 tf.lbal = (new_sectors >> 0) & 0xff;
1236 tf.lbam = (new_sectors >> 8) & 0xff;
1237 tf.lbah = (new_sectors >> 16) & 0xff;
1e999736 1238
2b789108 1239 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
c728a914 1240 if (err_mask) {
a9a79dfe
JP
1241 ata_dev_warn(dev,
1242 "failed to set max address (err_mask=0x%x)\n",
1243 err_mask);
c728a914 1244 if (err_mask == AC_ERR_DEV &&
efcef265 1245 (tf.error & (ATA_ABORTED | ATA_IDNF)))
c728a914
TH
1246 return -EACCES;
1247 return -EIO;
1248 }
1249
c728a914 1250 return 0;
1e999736
AC
1251}
1252
1253/**
1254 * ata_hpa_resize - Resize a device with an HPA set
1255 * @dev: Device to resize
1256 *
1257 * Read the size of an LBA28 or LBA48 disk with HPA features and resize
1258 * it if required to the full size of the media. The caller must check
1259 * the drive has the HPA feature set enabled.
05027adc
TH
1260 *
1261 * RETURNS:
1262 * 0 on success, -errno on failure.
1e999736 1263 */
05027adc 1264static int ata_hpa_resize(struct ata_device *dev)
1e999736 1265{
891fd7c6 1266 bool print_info = ata_dev_print_info(dev);
445d211b 1267 bool unlock_hpa = ata_ignore_hpa || dev->flags & ATA_DFLAG_UNLOCK_HPA;
05027adc
TH
1268 u64 sectors = ata_id_n_sectors(dev->id);
1269 u64 native_sectors;
c728a914 1270 int rc;
a617c09f 1271
05027adc 1272 /* do we need to do it? */
9162c657 1273 if ((dev->class != ATA_DEV_ATA && dev->class != ATA_DEV_ZAC) ||
05027adc
TH
1274 !ata_id_has_lba(dev->id) || !ata_id_hpa_enabled(dev->id) ||
1275 (dev->horkage & ATA_HORKAGE_BROKEN_HPA))
c728a914 1276 return 0;
1e999736 1277
05027adc
TH
1278 /* read native max address */
1279 rc = ata_read_native_max_address(dev, &native_sectors);
1280 if (rc) {
dda7aba1
TH
1281 /* If device aborted the command or HPA isn't going to
1282 * be unlocked, skip HPA resizing.
05027adc 1283 */
445d211b 1284 if (rc == -EACCES || !unlock_hpa) {
a9a79dfe
JP
1285 ata_dev_warn(dev,
1286 "HPA support seems broken, skipping HPA handling\n");
05027adc
TH
1287 dev->horkage |= ATA_HORKAGE_BROKEN_HPA;
1288
1289 /* we can continue if device aborted the command */
1290 if (rc == -EACCES)
1291 rc = 0;
1e999736 1292 }
37301a55 1293
05027adc
TH
1294 return rc;
1295 }
5920dadf 1296 dev->n_native_sectors = native_sectors;
05027adc
TH
1297
1298 /* nothing to do? */
445d211b 1299 if (native_sectors <= sectors || !unlock_hpa) {
05027adc
TH
1300 if (!print_info || native_sectors == sectors)
1301 return 0;
1302
1303 if (native_sectors > sectors)
a9a79dfe 1304 ata_dev_info(dev,
05027adc
TH
1305 "HPA detected: current %llu, native %llu\n",
1306 (unsigned long long)sectors,
1307 (unsigned long long)native_sectors);
1308 else if (native_sectors < sectors)
a9a79dfe
JP
1309 ata_dev_warn(dev,
1310 "native sectors (%llu) is smaller than sectors (%llu)\n",
05027adc
TH
1311 (unsigned long long)native_sectors,
1312 (unsigned long long)sectors);
1313 return 0;
1314 }
1315
1316 /* let's unlock HPA */
1317 rc = ata_set_max_sectors(dev, native_sectors);
1318 if (rc == -EACCES) {
1319 /* if device aborted the command, skip HPA resizing */
a9a79dfe
JP
1320 ata_dev_warn(dev,
1321 "device aborted resize (%llu -> %llu), skipping HPA handling\n",
1322 (unsigned long long)sectors,
1323 (unsigned long long)native_sectors);
05027adc
TH
1324 dev->horkage |= ATA_HORKAGE_BROKEN_HPA;
1325 return 0;
1326 } else if (rc)
1327 return rc;
1328
1329 /* re-read IDENTIFY data */
1330 rc = ata_dev_reread_id(dev, 0);
1331 if (rc) {
a9a79dfe
JP
1332 ata_dev_err(dev,
1333 "failed to re-read IDENTIFY data after HPA resizing\n");
05027adc
TH
1334 return rc;
1335 }
1336
1337 if (print_info) {
1338 u64 new_sectors = ata_id_n_sectors(dev->id);
a9a79dfe 1339 ata_dev_info(dev,
05027adc
TH
1340 "HPA unlocked: %llu -> %llu, native %llu\n",
1341 (unsigned long long)sectors,
1342 (unsigned long long)new_sectors,
1343 (unsigned long long)native_sectors);
1344 }
1345
1346 return 0;
1e999736
AC
1347}
1348
1da177e4
LT
1349/**
1350 * ata_dump_id - IDENTIFY DEVICE info debugging output
6044f3c4 1351 * @dev: device from which the information is fetched
0bd3300a 1352 * @id: IDENTIFY DEVICE page to dump
1da177e4 1353 *
0bd3300a
TH
1354 * Dump selected 16-bit words from the given IDENTIFY DEVICE
1355 * page.
1da177e4
LT
1356 *
1357 * LOCKING:
1358 * caller.
1359 */
1360
6044f3c4 1361static inline void ata_dump_id(struct ata_device *dev, const u16 *id)
1da177e4 1362{
6044f3c4
HR
1363 ata_dev_dbg(dev,
1364 "49==0x%04x 53==0x%04x 63==0x%04x 64==0x%04x 75==0x%04x\n"
1365 "80==0x%04x 81==0x%04x 82==0x%04x 83==0x%04x 84==0x%04x\n"
1366 "88==0x%04x 93==0x%04x\n",
1367 id[49], id[53], id[63], id[64], id[75], id[80],
1368 id[81], id[82], id[83], id[84], id[88], id[93]);
1da177e4
LT
1369}
1370
cb95d562
TH
1371/**
1372 * ata_id_xfermask - Compute xfermask from the given IDENTIFY data
1373 * @id: IDENTIFY data to compute xfer mask from
1374 *
1375 * Compute the xfermask for this device. This is not as trivial
1376 * as it seems if we must consider early devices correctly.
1377 *
1378 * FIXME: pre IDE drive timing (do we care ?).
1379 *
1380 * LOCKING:
1381 * None.
1382 *
1383 * RETURNS:
1384 * Computed xfermask
1385 */
f0a6d77b 1386unsigned int ata_id_xfermask(const u16 *id)
cb95d562 1387{
f0a6d77b 1388 unsigned int pio_mask, mwdma_mask, udma_mask;
cb95d562
TH
1389
1390 /* Usual case. Word 53 indicates word 64 is valid */
1391 if (id[ATA_ID_FIELD_VALID] & (1 << 1)) {
1392 pio_mask = id[ATA_ID_PIO_MODES] & 0x03;
1393 pio_mask <<= 3;
1394 pio_mask |= 0x7;
1395 } else {
1396 /* If word 64 isn't valid then Word 51 high byte holds
1397 * the PIO timing number for the maximum. Turn it into
1398 * a mask.
1399 */
7a0f1c8a 1400 u8 mode = (id[ATA_ID_OLD_PIO_MODES] >> 8) & 0xFF;
46767aeb 1401 if (mode < 5) /* Valid PIO range */
2dcb407e 1402 pio_mask = (2 << mode) - 1;
46767aeb
AC
1403 else
1404 pio_mask = 1;
cb95d562
TH
1405
1406 /* But wait.. there's more. Design your standards by
1407 * committee and you too can get a free iordy field to
e0af10ac 1408 * process. However it is the speeds not the modes that
cb95d562
TH
1409 * are supported... Note drivers using the timing API
1410 * will get this right anyway
1411 */
1412 }
1413
1414 mwdma_mask = id[ATA_ID_MWDMA_MODES] & 0x07;
fb21f0d0 1415
b352e57d
AC
1416 if (ata_id_is_cfa(id)) {
1417 /*
1418 * Process compact flash extended modes
1419 */
62afe5d7
SS
1420 int pio = (id[ATA_ID_CFA_MODES] >> 0) & 0x7;
1421 int dma = (id[ATA_ID_CFA_MODES] >> 3) & 0x7;
b352e57d
AC
1422
1423 if (pio)
1424 pio_mask |= (1 << 5);
1425 if (pio > 1)
1426 pio_mask |= (1 << 6);
1427 if (dma)
1428 mwdma_mask |= (1 << 3);
1429 if (dma > 1)
1430 mwdma_mask |= (1 << 4);
1431 }
1432
fb21f0d0
TH
1433 udma_mask = 0;
1434 if (id[ATA_ID_FIELD_VALID] & (1 << 2))
1435 udma_mask = id[ATA_ID_UDMA_MODES] & 0xff;
cb95d562
TH
1436
1437 return ata_pack_xfermask(pio_mask, mwdma_mask, udma_mask);
1438}
a52fbcfc 1439EXPORT_SYMBOL_GPL(ata_id_xfermask);
cb95d562 1440
7102d230 1441static void ata_qc_complete_internal(struct ata_queued_cmd *qc)
a2a7a662 1442{
77853bf2 1443 struct completion *waiting = qc->private_data;
a2a7a662 1444
a2a7a662 1445 complete(waiting);
a2a7a662
TH
1446}
1447
1448/**
2432697b 1449 * ata_exec_internal_sg - execute libata internal command
a2a7a662
TH
1450 * @dev: Device to which the command is sent
1451 * @tf: Taskfile registers for the command and the result
d69cf37d 1452 * @cdb: CDB for packet command
e227867f 1453 * @dma_dir: Data transfer direction of the command
5c1ad8b3 1454 * @sgl: sg list for the data buffer of the command
2432697b 1455 * @n_elem: Number of sg entries
2b789108 1456 * @timeout: Timeout in msecs (0 for default)
a2a7a662
TH
1457 *
1458 * Executes libata internal command with timeout. @tf contains
1459 * command on entry and result on return. Timeout and error
1460 * conditions are reported via return value. No recovery action
1461 * is taken after a command times out. It's caller's duty to
1462 * clean up after timeout.
1463 *
1464 * LOCKING:
1465 * None. Should be called with kernel context, might sleep.
551e8889
TH
1466 *
1467 * RETURNS:
1468 * Zero on success, AC_ERR_* mask on failure
a2a7a662 1469 */
4d6119f0
SS
1470static unsigned ata_exec_internal_sg(struct ata_device *dev,
1471 struct ata_taskfile *tf, const u8 *cdb,
1472 int dma_dir, struct scatterlist *sgl,
61176eed 1473 unsigned int n_elem, unsigned int timeout)
a2a7a662 1474{
9af5c9c9
TH
1475 struct ata_link *link = dev->link;
1476 struct ata_port *ap = link->ap;
a2a7a662 1477 u8 command = tf->command;
87fbc5a0 1478 int auto_timeout = 0;
a2a7a662 1479 struct ata_queued_cmd *qc;
28361c40 1480 unsigned int preempted_tag;
e3ed8939
JA
1481 u32 preempted_sactive;
1482 u64 preempted_qc_active;
da917d69 1483 int preempted_nr_active_links;
60be6b9a 1484 DECLARE_COMPLETION_ONSTACK(wait);
a2a7a662 1485 unsigned long flags;
77853bf2 1486 unsigned int err_mask;
d95a717f 1487 int rc;
a2a7a662 1488
ba6a1308 1489 spin_lock_irqsave(ap->lock, flags);
a2a7a662 1490
e3180499 1491 /* no internal command while frozen */
4cb7c6f1 1492 if (ata_port_is_frozen(ap)) {
ba6a1308 1493 spin_unlock_irqrestore(ap->lock, flags);
e3180499
TH
1494 return AC_ERR_SYSTEM;
1495 }
1496
2ab7db1f 1497 /* initialize internal qc */
28361c40 1498 qc = __ata_qc_from_tag(ap, ATA_TAG_INTERNAL);
a2a7a662 1499
28361c40
JA
1500 qc->tag = ATA_TAG_INTERNAL;
1501 qc->hw_tag = 0;
2ab7db1f
TH
1502 qc->scsicmd = NULL;
1503 qc->ap = ap;
1504 qc->dev = dev;
1505 ata_qc_reinit(qc);
1506
9af5c9c9
TH
1507 preempted_tag = link->active_tag;
1508 preempted_sactive = link->sactive;
dedaf2b0 1509 preempted_qc_active = ap->qc_active;
da917d69 1510 preempted_nr_active_links = ap->nr_active_links;
9af5c9c9
TH
1511 link->active_tag = ATA_TAG_POISON;
1512 link->sactive = 0;
dedaf2b0 1513 ap->qc_active = 0;
da917d69 1514 ap->nr_active_links = 0;
2ab7db1f
TH
1515
1516 /* prepare & issue qc */
a2a7a662 1517 qc->tf = *tf;
d69cf37d
TH
1518 if (cdb)
1519 memcpy(qc->cdb, cdb, ATAPI_CDB_LEN);
e771451c
VP
1520
1521 /* some SATA bridges need us to indicate data xfer direction */
1522 if (tf->protocol == ATAPI_PROT_DMA && (dev->flags & ATA_DFLAG_DMADIR) &&
1523 dma_dir == DMA_FROM_DEVICE)
1524 qc->tf.feature |= ATAPI_DMADIR;
1525
e61e0672 1526 qc->flags |= ATA_QCFLAG_RESULT_TF;
a2a7a662
TH
1527 qc->dma_dir = dma_dir;
1528 if (dma_dir != DMA_NONE) {
2432697b 1529 unsigned int i, buflen = 0;
87260216 1530 struct scatterlist *sg;
2432697b 1531
87260216
JA
1532 for_each_sg(sgl, sg, n_elem, i)
1533 buflen += sg->length;
2432697b 1534
87260216 1535 ata_sg_init(qc, sgl, n_elem);
49c80429 1536 qc->nbytes = buflen;
a2a7a662
TH
1537 }
1538
77853bf2 1539 qc->private_data = &wait;
a2a7a662
TH
1540 qc->complete_fn = ata_qc_complete_internal;
1541
8e0e694a 1542 ata_qc_issue(qc);
a2a7a662 1543
ba6a1308 1544 spin_unlock_irqrestore(ap->lock, flags);
a2a7a662 1545
87fbc5a0
TH
1546 if (!timeout) {
1547 if (ata_probe_timeout)
1548 timeout = ata_probe_timeout * 1000;
1549 else {
1550 timeout = ata_internal_cmd_timeout(dev, command);
1551 auto_timeout = 1;
1552 }
1553 }
2b789108 1554
c0c362b6
TH
1555 if (ap->ops->error_handler)
1556 ata_eh_release(ap);
1557
2b789108 1558 rc = wait_for_completion_timeout(&wait, msecs_to_jiffies(timeout));
d95a717f 1559
c0c362b6
TH
1560 if (ap->ops->error_handler)
1561 ata_eh_acquire(ap);
1562
c429137a 1563 ata_sff_flush_pio_task(ap);
41ade50c 1564
d95a717f 1565 if (!rc) {
ba6a1308 1566 spin_lock_irqsave(ap->lock, flags);
a2a7a662
TH
1567
1568 /* We're racing with irq here. If we lose, the
1569 * following test prevents us from completing the qc
d95a717f
TH
1570 * twice. If we win, the port is frozen and will be
1571 * cleaned up by ->post_internal_cmd().
a2a7a662 1572 */
77853bf2 1573 if (qc->flags & ATA_QCFLAG_ACTIVE) {
d95a717f
TH
1574 qc->err_mask |= AC_ERR_TIMEOUT;
1575
1576 if (ap->ops->error_handler)
1577 ata_port_freeze(ap);
1578 else
1579 ata_qc_complete(qc);
f15a1daf 1580
16169fb7
TH
1581 ata_dev_warn(dev, "qc timeout after %u msecs (cmd 0x%x)\n",
1582 timeout, command);
a2a7a662
TH
1583 }
1584
ba6a1308 1585 spin_unlock_irqrestore(ap->lock, flags);
a2a7a662
TH
1586 }
1587
d95a717f
TH
1588 /* do post_internal_cmd */
1589 if (ap->ops->post_internal_cmd)
1590 ap->ops->post_internal_cmd(qc);
1591
a51d644a
TH
1592 /* perform minimal error analysis */
1593 if (qc->flags & ATA_QCFLAG_FAILED) {
efcef265 1594 if (qc->result_tf.status & (ATA_ERR | ATA_DF))
a51d644a
TH
1595 qc->err_mask |= AC_ERR_DEV;
1596
1597 if (!qc->err_mask)
1598 qc->err_mask |= AC_ERR_OTHER;
1599
1600 if (qc->err_mask & ~AC_ERR_OTHER)
1601 qc->err_mask &= ~AC_ERR_OTHER;
2dae9955 1602 } else if (qc->tf.command == ATA_CMD_REQ_SENSE_DATA) {
efcef265 1603 qc->result_tf.status |= ATA_SENSE;
d95a717f
TH
1604 }
1605
15869303 1606 /* finish up */
ba6a1308 1607 spin_lock_irqsave(ap->lock, flags);
15869303 1608
e61e0672 1609 *tf = qc->result_tf;
77853bf2
TH
1610 err_mask = qc->err_mask;
1611
1612 ata_qc_free(qc);
9af5c9c9
TH
1613 link->active_tag = preempted_tag;
1614 link->sactive = preempted_sactive;
dedaf2b0 1615 ap->qc_active = preempted_qc_active;
da917d69 1616 ap->nr_active_links = preempted_nr_active_links;
77853bf2 1617
ba6a1308 1618 spin_unlock_irqrestore(ap->lock, flags);
15869303 1619
87fbc5a0
TH
1620 if ((err_mask & AC_ERR_TIMEOUT) && auto_timeout)
1621 ata_internal_cmd_timed_out(dev, command);
1622
77853bf2 1623 return err_mask;
a2a7a662
TH
1624}
1625
2432697b 1626/**
33480a0e 1627 * ata_exec_internal - execute libata internal command
2432697b
TH
1628 * @dev: Device to which the command is sent
1629 * @tf: Taskfile registers for the command and the result
1630 * @cdb: CDB for packet command
e227867f 1631 * @dma_dir: Data transfer direction of the command
2432697b
TH
1632 * @buf: Data buffer of the command
1633 * @buflen: Length of data buffer
2b789108 1634 * @timeout: Timeout in msecs (0 for default)
2432697b
TH
1635 *
1636 * Wrapper around ata_exec_internal_sg() which takes simple
1637 * buffer instead of sg list.
1638 *
1639 * LOCKING:
1640 * None. Should be called with kernel context, might sleep.
1641 *
1642 * RETURNS:
1643 * Zero on success, AC_ERR_* mask on failure
1644 */
1645unsigned ata_exec_internal(struct ata_device *dev,
1646 struct ata_taskfile *tf, const u8 *cdb,
2b789108 1647 int dma_dir, void *buf, unsigned int buflen,
61176eed 1648 unsigned int timeout)
2432697b 1649{
33480a0e
TH
1650 struct scatterlist *psg = NULL, sg;
1651 unsigned int n_elem = 0;
2432697b 1652
33480a0e
TH
1653 if (dma_dir != DMA_NONE) {
1654 WARN_ON(!buf);
1655 sg_init_one(&sg, buf, buflen);
1656 psg = &sg;
1657 n_elem++;
1658 }
2432697b 1659
2b789108
TH
1660 return ata_exec_internal_sg(dev, tf, cdb, dma_dir, psg, n_elem,
1661 timeout);
2432697b
TH
1662}
1663
1bc4ccff
AC
1664/**
1665 * ata_pio_need_iordy - check if iordy needed
1666 * @adev: ATA device
1667 *
1668 * Check if the current speed of the device requires IORDY. Used
1669 * by various controllers for chip configuration.
1670 */
1bc4ccff
AC
1671unsigned int ata_pio_need_iordy(const struct ata_device *adev)
1672{
0d9e6659
TH
1673 /* Don't set IORDY if we're preparing for reset. IORDY may
1674 * lead to controller lock up on certain controllers if the
1675 * port is not occupied. See bko#11703 for details.
1676 */
1677 if (adev->link->ap->pflags & ATA_PFLAG_RESETTING)
1678 return 0;
1679 /* Controller doesn't support IORDY. Probably a pointless
1680 * check as the caller should know this.
1681 */
9af5c9c9 1682 if (adev->link->ap->flags & ATA_FLAG_NO_IORDY)
1bc4ccff 1683 return 0;
5c18c4d2
DD
1684 /* CF spec. r4.1 Table 22 says no iordy on PIO5 and PIO6. */
1685 if (ata_id_is_cfa(adev->id)
1686 && (adev->pio_mode == XFER_PIO_5 || adev->pio_mode == XFER_PIO_6))
1687 return 0;
432729f0
AC
1688 /* PIO3 and higher it is mandatory */
1689 if (adev->pio_mode > XFER_PIO_2)
1690 return 1;
1691 /* We turn it on when possible */
1692 if (ata_id_has_iordy(adev->id))
1bc4ccff 1693 return 1;
432729f0
AC
1694 return 0;
1695}
a52fbcfc 1696EXPORT_SYMBOL_GPL(ata_pio_need_iordy);
2e9edbf8 1697
432729f0
AC
1698/**
1699 * ata_pio_mask_no_iordy - Return the non IORDY mask
1700 * @adev: ATA device
1701 *
1702 * Compute the highest mode possible if we are not using iordy. Return
1703 * -1 if no iordy mode is available.
1704 */
432729f0
AC
1705static u32 ata_pio_mask_no_iordy(const struct ata_device *adev)
1706{
1bc4ccff 1707 /* If we have no drive specific rule, then PIO 2 is non IORDY */
1bc4ccff 1708 if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE */
432729f0 1709 u16 pio = adev->id[ATA_ID_EIDE_PIO];
1bc4ccff
AC
1710 /* Is the speed faster than the drive allows non IORDY ? */
1711 if (pio) {
1712 /* This is cycle times not frequency - watch the logic! */
1713 if (pio > 240) /* PIO2 is 240nS per cycle */
432729f0
AC
1714 return 3 << ATA_SHIFT_PIO;
1715 return 7 << ATA_SHIFT_PIO;
1bc4ccff
AC
1716 }
1717 }
432729f0 1718 return 3 << ATA_SHIFT_PIO;
1bc4ccff
AC
1719}
1720
963e4975
AC
1721/**
1722 * ata_do_dev_read_id - default ID read method
1723 * @dev: device
1724 * @tf: proposed taskfile
1725 * @id: data buffer
1726 *
1727 * Issue the identify taskfile and hand back the buffer containing
1728 * identify data. For some RAID controllers and for pre ATA devices
1729 * this function is wrapped or replaced by the driver
1730 */
1731unsigned int ata_do_dev_read_id(struct ata_device *dev,
0561e514 1732 struct ata_taskfile *tf, __le16 *id)
963e4975
AC
1733{
1734 return ata_exec_internal(dev, tf, NULL, DMA_FROM_DEVICE,
1735 id, sizeof(id[0]) * ATA_ID_WORDS, 0);
1736}
a52fbcfc 1737EXPORT_SYMBOL_GPL(ata_do_dev_read_id);
963e4975 1738
1da177e4 1739/**
49016aca 1740 * ata_dev_read_id - Read ID data from the specified device
49016aca
TH
1741 * @dev: target device
1742 * @p_class: pointer to class of the target device (may be changed)
bff04647 1743 * @flags: ATA_READID_* flags
fe635c7e 1744 * @id: buffer to read IDENTIFY data into
1da177e4 1745 *
49016aca
TH
1746 * Read ID data from the specified device. ATA_CMD_ID_ATA is
1747 * performed on ATA devices and ATA_CMD_ID_ATAPI on ATAPI
aec5c3c1
TH
1748 * devices. This function also issues ATA_CMD_INIT_DEV_PARAMS
1749 * for pre-ATA4 drives.
1da177e4 1750 *
50a99018 1751 * FIXME: ATA_CMD_ID_ATA is optional for early drives and right
2dcb407e 1752 * now we abort if we hit that case.
50a99018 1753 *
1da177e4 1754 * LOCKING:
49016aca
TH
1755 * Kernel thread context (may sleep)
1756 *
1757 * RETURNS:
1758 * 0 on success, -errno otherwise.
1da177e4 1759 */
a9beec95 1760int ata_dev_read_id(struct ata_device *dev, unsigned int *p_class,
bff04647 1761 unsigned int flags, u16 *id)
1da177e4 1762{
9af5c9c9 1763 struct ata_port *ap = dev->link->ap;
49016aca 1764 unsigned int class = *p_class;
a0123703 1765 struct ata_taskfile tf;
49016aca
TH
1766 unsigned int err_mask = 0;
1767 const char *reason;
79b42bab 1768 bool is_semb = class == ATA_DEV_SEMB;
54936f8b 1769 int may_fallback = 1, tried_spinup = 0;
49016aca 1770 int rc;
1da177e4 1771
963e4975 1772retry:
3373efd8 1773 ata_tf_init(dev, &tf);
a0123703 1774
49016aca 1775 switch (class) {
79b42bab
TH
1776 case ATA_DEV_SEMB:
1777 class = ATA_DEV_ATA; /* some hard drives report SEMB sig */
df561f66 1778 fallthrough;
49016aca 1779 case ATA_DEV_ATA:
9162c657 1780 case ATA_DEV_ZAC:
a0123703 1781 tf.command = ATA_CMD_ID_ATA;
49016aca
TH
1782 break;
1783 case ATA_DEV_ATAPI:
a0123703 1784 tf.command = ATA_CMD_ID_ATAPI;
49016aca
TH
1785 break;
1786 default:
1787 rc = -ENODEV;
1788 reason = "unsupported class";
1789 goto err_out;
1da177e4
LT
1790 }
1791
a0123703 1792 tf.protocol = ATA_PROT_PIO;
81afe893
TH
1793
1794 /* Some devices choke if TF registers contain garbage. Make
1795 * sure those are properly initialized.
1796 */
1797 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
1798
1799 /* Device presence detection is unreliable on some
1800 * controllers. Always poll IDENTIFY if available.
1801 */
1802 tf.flags |= ATA_TFLAG_POLLING;
1da177e4 1803
963e4975 1804 if (ap->ops->read_id)
0561e514 1805 err_mask = ap->ops->read_id(dev, &tf, (__le16 *)id);
963e4975 1806 else
0561e514 1807 err_mask = ata_do_dev_read_id(dev, &tf, (__le16 *)id);
963e4975 1808
a0123703 1809 if (err_mask) {
800b3996 1810 if (err_mask & AC_ERR_NODEV_HINT) {
a9a79dfe 1811 ata_dev_dbg(dev, "NODEV after polling detection\n");
55a8e2c8
TH
1812 return -ENOENT;
1813 }
1814
79b42bab 1815 if (is_semb) {
a9a79dfe
JP
1816 ata_dev_info(dev,
1817 "IDENTIFY failed on device w/ SEMB sig, disabled\n");
79b42bab
TH
1818 /* SEMB is not supported yet */
1819 *p_class = ATA_DEV_SEMB_UNSUP;
1820 return 0;
1821 }
1822
efcef265 1823 if ((err_mask == AC_ERR_DEV) && (tf.error & ATA_ABORTED)) {
1ffc151f
TH
1824 /* Device or controller might have reported
1825 * the wrong device class. Give a shot at the
1826 * other IDENTIFY if the current one is
1827 * aborted by the device.
1828 */
1829 if (may_fallback) {
1830 may_fallback = 0;
1831
1832 if (class == ATA_DEV_ATA)
1833 class = ATA_DEV_ATAPI;
1834 else
1835 class = ATA_DEV_ATA;
1836 goto retry;
1837 }
1838
1839 /* Control reaches here iff the device aborted
1840 * both flavors of IDENTIFYs which happens
1841 * sometimes with phantom devices.
1842 */
a9a79dfe
JP
1843 ata_dev_dbg(dev,
1844 "both IDENTIFYs aborted, assuming NODEV\n");
1ffc151f 1845 return -ENOENT;
54936f8b
TH
1846 }
1847
49016aca
TH
1848 rc = -EIO;
1849 reason = "I/O error";
1da177e4
LT
1850 goto err_out;
1851 }
1852
43c9c591 1853 if (dev->horkage & ATA_HORKAGE_DUMP_ID) {
4baa5745 1854 ata_dev_info(dev, "dumping IDENTIFY data, "
a9a79dfe
JP
1855 "class=%d may_fallback=%d tried_spinup=%d\n",
1856 class, may_fallback, tried_spinup);
4baa5745 1857 print_hex_dump(KERN_INFO, "", DUMP_PREFIX_OFFSET,
43c9c591
TH
1858 16, 2, id, ATA_ID_WORDS * sizeof(*id), true);
1859 }
1860
54936f8b
TH
1861 /* Falling back doesn't make sense if ID data was read
1862 * successfully at least once.
1863 */
1864 may_fallback = 0;
1865
49016aca 1866 swap_buf_le16(id, ATA_ID_WORDS);
1da177e4 1867
49016aca 1868 /* sanity check */
a4f5749b 1869 rc = -EINVAL;
6070068b 1870 reason = "device reports invalid type";
a4f5749b 1871
9162c657 1872 if (class == ATA_DEV_ATA || class == ATA_DEV_ZAC) {
a4f5749b
TH
1873 if (!ata_id_is_ata(id) && !ata_id_is_cfa(id))
1874 goto err_out;
db63a4c8
AW
1875 if (ap->host->flags & ATA_HOST_IGNORE_ATA &&
1876 ata_id_is_ata(id)) {
1877 ata_dev_dbg(dev,
1878 "host indicates ignore ATA devices, ignored\n");
1879 return -ENOENT;
1880 }
a4f5749b
TH
1881 } else {
1882 if (ata_id_is_ata(id))
1883 goto err_out;
49016aca
TH
1884 }
1885
169439c2
ML
1886 if (!tried_spinup && (id[2] == 0x37c8 || id[2] == 0x738c)) {
1887 tried_spinup = 1;
1888 /*
1889 * Drive powered-up in standby mode, and requires a specific
1890 * SET_FEATURES spin-up subcommand before it will accept
1891 * anything other than the original IDENTIFY command.
1892 */
218f3d30 1893 err_mask = ata_dev_set_feature(dev, SETFEATURES_SPINUP, 0);
fb0582f9 1894 if (err_mask && id[2] != 0x738c) {
169439c2
ML
1895 rc = -EIO;
1896 reason = "SPINUP failed";
1897 goto err_out;
1898 }
1899 /*
1900 * If the drive initially returned incomplete IDENTIFY info,
1901 * we now must reissue the IDENTIFY command.
1902 */
1903 if (id[2] == 0x37c8)
1904 goto retry;
1905 }
1906
9162c657
HR
1907 if ((flags & ATA_READID_POSTRESET) &&
1908 (class == ATA_DEV_ATA || class == ATA_DEV_ZAC)) {
49016aca
TH
1909 /*
1910 * The exact sequence expected by certain pre-ATA4 drives is:
1911 * SRST RESET
50a99018
AC
1912 * IDENTIFY (optional in early ATA)
1913 * INITIALIZE DEVICE PARAMETERS (later IDE and ATA)
49016aca
TH
1914 * anything else..
1915 * Some drives were very specific about that exact sequence.
50a99018
AC
1916 *
1917 * Note that ATA4 says lba is mandatory so the second check
c9404c9c 1918 * should never trigger.
49016aca
TH
1919 */
1920 if (ata_id_major_version(id) < 4 || !ata_id_has_lba(id)) {
3373efd8 1921 err_mask = ata_dev_init_params(dev, id[3], id[6]);
49016aca
TH
1922 if (err_mask) {
1923 rc = -EIO;
1924 reason = "INIT_DEV_PARAMS failed";
1925 goto err_out;
1926 }
1927
1928 /* current CHS translation info (id[53-58]) might be
1929 * changed. reread the identify device info.
1930 */
bff04647 1931 flags &= ~ATA_READID_POSTRESET;
49016aca
TH
1932 goto retry;
1933 }
1934 }
1935
1936 *p_class = class;
fe635c7e 1937
49016aca
TH
1938 return 0;
1939
1940 err_out:
16d42467
HR
1941 ata_dev_warn(dev, "failed to IDENTIFY (%s, err_mask=0x%x)\n",
1942 reason, err_mask);
49016aca
TH
1943 return rc;
1944}
1945
f01f62c2
CH
1946/**
1947 * ata_read_log_page - read a specific log page
1948 * @dev: target device
1949 * @log: log to read
1950 * @page: page to read
1951 * @buf: buffer to store read page
1952 * @sectors: number of sectors to read
1953 *
1954 * Read log page using READ_LOG_EXT command.
1955 *
1956 * LOCKING:
1957 * Kernel thread context (may sleep).
1958 *
1959 * RETURNS:
1960 * 0 on success, AC_ERR_* mask otherwise.
1961 */
1962unsigned int ata_read_log_page(struct ata_device *dev, u8 log,
1963 u8 page, void *buf, unsigned int sectors)
1964{
1965 unsigned long ap_flags = dev->link->ap->flags;
1966 struct ata_taskfile tf;
1967 unsigned int err_mask;
1968 bool dma = false;
1969
4633778b 1970 ata_dev_dbg(dev, "read log page - log 0x%x, page 0x%x\n", log, page);
f01f62c2
CH
1971
1972 /*
1973 * Return error without actually issuing the command on controllers
1974 * which e.g. lockup on a read log page.
1975 */
1976 if (ap_flags & ATA_FLAG_NO_LOG_PAGE)
1977 return AC_ERR_DEV;
1978
1979retry:
1980 ata_tf_init(dev, &tf);
f971a854 1981 if (ata_dma_enabled(dev) && ata_id_has_read_log_dma_ext(dev->id) &&
7cfdfdc8 1982 !(dev->horkage & ATA_HORKAGE_NO_DMA_LOG)) {
f01f62c2
CH
1983 tf.command = ATA_CMD_READ_LOG_DMA_EXT;
1984 tf.protocol = ATA_PROT_DMA;
1985 dma = true;
1986 } else {
1987 tf.command = ATA_CMD_READ_LOG_EXT;
1988 tf.protocol = ATA_PROT_PIO;
1989 dma = false;
1990 }
1991 tf.lbal = log;
1992 tf.lbam = page;
1993 tf.nsect = sectors;
1994 tf.hob_nsect = sectors >> 8;
1995 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_LBA48 | ATA_TFLAG_DEVICE;
1996
1997 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_FROM_DEVICE,
1998 buf, sectors * ATA_SECT_SIZE, 0);
1999
fc5c8aa7
DLM
2000 if (err_mask) {
2001 if (dma) {
2002 dev->horkage |= ATA_HORKAGE_NO_DMA_LOG;
5122e53e
NC
2003 if (!ata_port_is_frozen(dev->link->ap))
2004 goto retry;
fc5c8aa7 2005 }
23ef63d5
DLM
2006 ata_dev_err(dev,
2007 "Read log 0x%02x page 0x%02x failed, Emask 0x%x\n",
2008 (unsigned int)log, (unsigned int)page, err_mask);
f01f62c2
CH
2009 }
2010
f01f62c2
CH
2011 return err_mask;
2012}
2013
c745dfc5 2014static int ata_log_supported(struct ata_device *dev, u8 log)
efe205a3
CH
2015{
2016 struct ata_port *ap = dev->link->ap;
2017
ac9f0c81 2018 if (dev->horkage & ATA_HORKAGE_NO_LOG_DIR)
c745dfc5 2019 return 0;
ac9f0c81 2020
efe205a3 2021 if (ata_read_log_page(dev, ATA_LOG_DIRECTORY, 0, ap->sector_buf, 1))
c745dfc5
TE
2022 return 0;
2023 return get_unaligned_le16(&ap->sector_buf[log * 2]);
efe205a3
CH
2024}
2025
a0fd2454
CH
2026static bool ata_identify_page_supported(struct ata_device *dev, u8 page)
2027{
2028 struct ata_port *ap = dev->link->ap;
2029 unsigned int err, i;
2030
636f6e2a
DLM
2031 if (dev->horkage & ATA_HORKAGE_NO_ID_DEV_LOG)
2032 return false;
2033
a0fd2454 2034 if (!ata_log_supported(dev, ATA_LOG_IDENTIFY_DEVICE)) {
636f6e2a
DLM
2035 /*
2036 * IDENTIFY DEVICE data log is defined as mandatory starting
2037 * with ACS-3 (ATA version 10). Warn about the missing log
2038 * for drives which implement this ATA level or above.
2039 */
2040 if (ata_id_major_version(dev->id) >= 10)
2041 ata_dev_warn(dev,
2042 "ATA Identify Device Log not supported\n");
2043 dev->horkage |= ATA_HORKAGE_NO_ID_DEV_LOG;
a0fd2454
CH
2044 return false;
2045 }
2046
2047 /*
2048 * Read IDENTIFY DEVICE data log, page 0, to figure out if the page is
2049 * supported.
2050 */
2051 err = ata_read_log_page(dev, ATA_LOG_IDENTIFY_DEVICE, 0, ap->sector_buf,
2052 1);
fc5c8aa7 2053 if (err)
a0fd2454 2054 return false;
a0fd2454
CH
2055
2056 for (i = 0; i < ap->sector_buf[8]; i++) {
2057 if (ap->sector_buf[9 + i] == page)
2058 return true;
2059 }
2060
2061 return false;
2062}
2063
9062712f
TH
2064static int ata_do_link_spd_horkage(struct ata_device *dev)
2065{
2066 struct ata_link *plink = ata_dev_phys_link(dev);
2067 u32 target, target_limit;
2068
2069 if (!sata_scr_valid(plink))
2070 return 0;
2071
2072 if (dev->horkage & ATA_HORKAGE_1_5_GBPS)
2073 target = 1;
2074 else
2075 return 0;
2076
2077 target_limit = (1 << target) - 1;
2078
2079 /* if already on stricter limit, no need to push further */
2080 if (plink->sata_spd_limit <= target_limit)
2081 return 0;
2082
2083 plink->sata_spd_limit = target_limit;
2084
2085 /* Request another EH round by returning -EAGAIN if link is
2086 * going faster than the target speed. Forward progress is
2087 * guaranteed by setting sata_spd_limit to target_limit above.
2088 */
2089 if (plink->sata_spd > target) {
a9a79dfe
JP
2090 ata_dev_info(dev, "applying link speed limit horkage to %s\n",
2091 sata_spd_string(target));
9062712f
TH
2092 return -EAGAIN;
2093 }
2094 return 0;
2095}
2096
3373efd8 2097static inline u8 ata_dev_knobble(struct ata_device *dev)
4b2f3ede 2098{
9af5c9c9 2099 struct ata_port *ap = dev->link->ap;
9ce8e307
JA
2100
2101 if (ata_dev_blacklisted(dev) & ATA_HORKAGE_BRIDGE_OK)
2102 return 0;
2103
9af5c9c9 2104 return ((ap->cbl == ATA_CBL_SATA) && (!ata_id_is_sata(dev->id)));
4b2f3ede
TH
2105}
2106
5a233551
HR
2107static void ata_dev_config_ncq_send_recv(struct ata_device *dev)
2108{
2109 struct ata_port *ap = dev->link->ap;
2110 unsigned int err_mask;
2111
efe205a3
CH
2112 if (!ata_log_supported(dev, ATA_LOG_NCQ_SEND_RECV)) {
2113 ata_dev_warn(dev, "NCQ Send/Recv Log not supported\n");
fe5af0cc
HR
2114 return;
2115 }
5a233551
HR
2116 err_mask = ata_read_log_page(dev, ATA_LOG_NCQ_SEND_RECV,
2117 0, ap->sector_buf, 1);
fc5c8aa7 2118 if (!err_mask) {
5a233551
HR
2119 u8 *cmds = dev->ncq_send_recv_cmds;
2120
2121 dev->flags |= ATA_DFLAG_NCQ_SEND_RECV;
2122 memcpy(cmds, ap->sector_buf, ATA_LOG_NCQ_SEND_RECV_SIZE);
2123
2124 if (dev->horkage & ATA_HORKAGE_NO_NCQ_TRIM) {
2125 ata_dev_dbg(dev, "disabling queued TRIM support\n");
2126 cmds[ATA_LOG_NCQ_SEND_RECV_DSM_OFFSET] &=
2127 ~ATA_LOG_NCQ_SEND_RECV_DSM_TRIM;
2128 }
2129 }
2130}
2131
284b3b77
HR
2132static void ata_dev_config_ncq_non_data(struct ata_device *dev)
2133{
2134 struct ata_port *ap = dev->link->ap;
2135 unsigned int err_mask;
284b3b77 2136
efe205a3 2137 if (!ata_log_supported(dev, ATA_LOG_NCQ_NON_DATA)) {
284b3b77
HR
2138 ata_dev_warn(dev,
2139 "NCQ Send/Recv Log not supported\n");
2140 return;
2141 }
2142 err_mask = ata_read_log_page(dev, ATA_LOG_NCQ_NON_DATA,
2143 0, ap->sector_buf, 1);
fc5c8aa7 2144 if (!err_mask) {
284b3b77
HR
2145 u8 *cmds = dev->ncq_non_data_cmds;
2146
2147 memcpy(cmds, ap->sector_buf, ATA_LOG_NCQ_NON_DATA_SIZE);
2148 }
2149}
2150
8e061784
AM
2151static void ata_dev_config_ncq_prio(struct ata_device *dev)
2152{
2153 struct ata_port *ap = dev->link->ap;
2154 unsigned int err_mask;
2155
06f6c4c6
DLM
2156 if (!ata_identify_page_supported(dev, ATA_LOG_SATA_SETTINGS))
2157 return;
2158
8e061784 2159 err_mask = ata_read_log_page(dev,
1d51d5f3 2160 ATA_LOG_IDENTIFY_DEVICE,
8e061784
AM
2161 ATA_LOG_SATA_SETTINGS,
2162 ap->sector_buf,
2163 1);
fc5c8aa7 2164 if (err_mask)
2360fa18 2165 goto not_supported;
8e061784 2166
2360fa18
DLM
2167 if (!(ap->sector_buf[ATA_LOG_NCQ_PRIO_OFFSET] & BIT(3)))
2168 goto not_supported;
2169
2170 dev->flags |= ATA_DFLAG_NCQ_PRIO;
2171
2172 return;
8e061784 2173
2360fa18 2174not_supported:
e00923c5 2175 dev->flags &= ~ATA_DFLAG_NCQ_PRIO_ENABLED;
2360fa18 2176 dev->flags &= ~ATA_DFLAG_NCQ_PRIO;
8e061784
AM
2177}
2178
7a8526a5
KH
2179static bool ata_dev_check_adapter(struct ata_device *dev,
2180 unsigned short vendor_id)
2181{
2182 struct pci_dev *pcidev = NULL;
2183 struct device *parent_dev = NULL;
2184
2185 for (parent_dev = dev->tdev.parent; parent_dev != NULL;
2186 parent_dev = parent_dev->parent) {
2187 if (dev_is_pci(parent_dev)) {
2188 pcidev = to_pci_dev(parent_dev);
2189 if (pcidev->vendor == vendor_id)
2190 return true;
2191 break;
2192 }
2193 }
2194
2195 return false;
2196}
2197
388539f3 2198static int ata_dev_config_ncq(struct ata_device *dev,
a6e6ce8e
TH
2199 char *desc, size_t desc_sz)
2200{
9af5c9c9 2201 struct ata_port *ap = dev->link->ap;
a6e6ce8e 2202 int hdepth = 0, ddepth = ata_id_queue_depth(dev->id);
388539f3
SL
2203 unsigned int err_mask;
2204 char *aa_desc = "";
a6e6ce8e
TH
2205
2206 if (!ata_id_has_ncq(dev->id)) {
2207 desc[0] = '\0';
388539f3 2208 return 0;
a6e6ce8e 2209 }
cba97ea1
BZ
2210 if (!IS_ENABLED(CONFIG_SATA_HOST))
2211 return 0;
75683fe7 2212 if (dev->horkage & ATA_HORKAGE_NONCQ) {
6919a0a6 2213 snprintf(desc, desc_sz, "NCQ (not used)");
388539f3 2214 return 0;
6919a0a6 2215 }
7a8526a5
KH
2216
2217 if (dev->horkage & ATA_HORKAGE_NO_NCQ_ON_ATI &&
2218 ata_dev_check_adapter(dev, PCI_VENDOR_ID_ATI)) {
2219 snprintf(desc, desc_sz, "NCQ (not used)");
2220 return 0;
2221 }
2222
a6e6ce8e 2223 if (ap->flags & ATA_FLAG_NCQ) {
69278f79 2224 hdepth = min(ap->scsi_host->can_queue, ATA_MAX_QUEUE);
a6e6ce8e
TH
2225 dev->flags |= ATA_DFLAG_NCQ;
2226 }
2227
388539f3
SL
2228 if (!(dev->horkage & ATA_HORKAGE_BROKEN_FPDMA_AA) &&
2229 (ap->flags & ATA_FLAG_FPDMA_AA) &&
2230 ata_id_has_fpdma_aa(dev->id)) {
2231 err_mask = ata_dev_set_feature(dev, SETFEATURES_SATA_ENABLE,
2232 SATA_FPDMA_AA);
2233 if (err_mask) {
a9a79dfe
JP
2234 ata_dev_err(dev,
2235 "failed to enable AA (error_mask=0x%x)\n",
2236 err_mask);
388539f3
SL
2237 if (err_mask != AC_ERR_DEV) {
2238 dev->horkage |= ATA_HORKAGE_BROKEN_FPDMA_AA;
2239 return -EIO;
2240 }
2241 } else
2242 aa_desc = ", AA";
2243 }
2244
a6e6ce8e 2245 if (hdepth >= ddepth)
388539f3 2246 snprintf(desc, desc_sz, "NCQ (depth %d)%s", ddepth, aa_desc);
a6e6ce8e 2247 else
388539f3
SL
2248 snprintf(desc, desc_sz, "NCQ (depth %d/%d)%s", hdepth,
2249 ddepth, aa_desc);
ed36911c 2250
284b3b77
HR
2251 if ((ap->flags & ATA_FLAG_FPDMA_AUX)) {
2252 if (ata_id_has_ncq_send_and_recv(dev->id))
2253 ata_dev_config_ncq_send_recv(dev);
2254 if (ata_id_has_ncq_non_data(dev->id))
2255 ata_dev_config_ncq_non_data(dev);
8e061784
AM
2256 if (ata_id_has_ncq_prio(dev->id))
2257 ata_dev_config_ncq_prio(dev);
284b3b77 2258 }
f78dea06 2259
388539f3 2260 return 0;
a6e6ce8e 2261}
f78dea06 2262
e87fd28c
HR
2263static void ata_dev_config_sense_reporting(struct ata_device *dev)
2264{
2265 unsigned int err_mask;
2266
2267 if (!ata_id_has_sense_reporting(dev->id))
2268 return;
2269
2270 if (ata_id_sense_reporting_enabled(dev->id))
2271 return;
2272
2273 err_mask = ata_dev_set_feature(dev, SETFEATURE_SENSE_DATA, 0x1);
2274 if (err_mask) {
2275 ata_dev_dbg(dev,
2276 "failed to enable Sense Data Reporting, Emask 0x%x\n",
2277 err_mask);
2278 }
2279}
2280
6d1003ae
HR
2281static void ata_dev_config_zac(struct ata_device *dev)
2282{
2283 struct ata_port *ap = dev->link->ap;
2284 unsigned int err_mask;
2285 u8 *identify_buf = ap->sector_buf;
6d1003ae
HR
2286
2287 dev->zac_zones_optimal_open = U32_MAX;
2288 dev->zac_zones_optimal_nonseq = U32_MAX;
2289 dev->zac_zones_max_open = U32_MAX;
2290
2291 /*
2292 * Always set the 'ZAC' flag for Host-managed devices.
2293 */
2294 if (dev->class == ATA_DEV_ZAC)
2295 dev->flags |= ATA_DFLAG_ZAC;
2296 else if (ata_id_zoned_cap(dev->id) == 0x01)
2297 /*
2298 * Check for host-aware devices.
2299 */
2300 dev->flags |= ATA_DFLAG_ZAC;
2301
2302 if (!(dev->flags & ATA_DFLAG_ZAC))
2303 return;
2304
a0fd2454 2305 if (!ata_identify_page_supported(dev, ATA_LOG_ZONED_INFORMATION)) {
6d1003ae
HR
2306 ata_dev_warn(dev,
2307 "ATA Zoned Information Log not supported\n");
2308 return;
2309 }
ed36911c 2310
6d1003ae
HR
2311 /*
2312 * Read IDENTIFY DEVICE data log, page 9 (Zoned-device information)
2313 */
1d51d5f3 2314 err_mask = ata_read_log_page(dev, ATA_LOG_IDENTIFY_DEVICE,
6d1003ae
HR
2315 ATA_LOG_ZONED_INFORMATION,
2316 identify_buf, 1);
2317 if (!err_mask) {
2318 u64 zoned_cap, opt_open, opt_nonseq, max_open;
2319
2320 zoned_cap = get_unaligned_le64(&identify_buf[8]);
2321 if ((zoned_cap >> 63))
2322 dev->zac_zoned_cap = (zoned_cap & 1);
2323 opt_open = get_unaligned_le64(&identify_buf[24]);
2324 if ((opt_open >> 63))
2325 dev->zac_zones_optimal_open = (u32)opt_open;
2326 opt_nonseq = get_unaligned_le64(&identify_buf[32]);
2327 if ((opt_nonseq >> 63))
2328 dev->zac_zones_optimal_nonseq = (u32)opt_nonseq;
2329 max_open = get_unaligned_le64(&identify_buf[40]);
2330 if ((max_open >> 63))
2331 dev->zac_zones_max_open = (u32)max_open;
2332 }
a6e6ce8e
TH
2333}
2334
818831c8
CH
2335static void ata_dev_config_trusted(struct ata_device *dev)
2336{
2337 struct ata_port *ap = dev->link->ap;
2338 u64 trusted_cap;
2339 unsigned int err;
2340
e8f11db9
CH
2341 if (!ata_id_has_trusted(dev->id))
2342 return;
2343
818831c8
CH
2344 if (!ata_identify_page_supported(dev, ATA_LOG_SECURITY)) {
2345 ata_dev_warn(dev,
2346 "Security Log not supported\n");
2347 return;
2348 }
2349
2350 err = ata_read_log_page(dev, ATA_LOG_IDENTIFY_DEVICE, ATA_LOG_SECURITY,
2351 ap->sector_buf, 1);
fc5c8aa7 2352 if (err)
818831c8 2353 return;
818831c8
CH
2354
2355 trusted_cap = get_unaligned_le64(&ap->sector_buf[40]);
2356 if (!(trusted_cap & (1ULL << 63))) {
2357 ata_dev_dbg(dev,
2358 "Trusted Computing capability qword not valid!\n");
2359 return;
2360 }
2361
2362 if (trusted_cap & (1 << 0))
2363 dev->flags |= ATA_DFLAG_TRUSTED;
2364}
2365
891fd7c6
DLM
2366static int ata_dev_config_lba(struct ata_device *dev)
2367{
891fd7c6
DLM
2368 const u16 *id = dev->id;
2369 const char *lba_desc;
2370 char ncq_desc[24];
2371 int ret;
2372
2373 dev->flags |= ATA_DFLAG_LBA;
2374
2375 if (ata_id_has_lba48(id)) {
2376 lba_desc = "LBA48";
2377 dev->flags |= ATA_DFLAG_LBA48;
2378 if (dev->n_sectors >= (1UL << 28) &&
2379 ata_id_has_flush_ext(id))
2380 dev->flags |= ATA_DFLAG_FLUSH_EXT;
2381 } else {
2382 lba_desc = "LBA";
2383 }
2384
2385 /* config NCQ */
2386 ret = ata_dev_config_ncq(dev, ncq_desc, sizeof(ncq_desc));
2387
2388 /* print device info to dmesg */
1c95a27c 2389 if (ata_dev_print_info(dev))
891fd7c6
DLM
2390 ata_dev_info(dev,
2391 "%llu sectors, multi %u: %s %s\n",
2392 (unsigned long long)dev->n_sectors,
2393 dev->multi_count, lba_desc, ncq_desc);
2394
2395 return ret;
2396}
2397
2398static void ata_dev_config_chs(struct ata_device *dev)
2399{
891fd7c6
DLM
2400 const u16 *id = dev->id;
2401
2402 if (ata_id_current_chs_valid(id)) {
2403 /* Current CHS translation is valid. */
2404 dev->cylinders = id[54];
2405 dev->heads = id[55];
2406 dev->sectors = id[56];
2407 } else {
2408 /* Default translation */
2409 dev->cylinders = id[1];
2410 dev->heads = id[3];
2411 dev->sectors = id[6];
2412 }
2413
2414 /* print device info to dmesg */
1c95a27c 2415 if (ata_dev_print_info(dev))
891fd7c6
DLM
2416 ata_dev_info(dev,
2417 "%llu sectors, multi %u, CHS %u/%u/%u\n",
2418 (unsigned long long)dev->n_sectors,
2419 dev->multi_count, dev->cylinders,
2420 dev->heads, dev->sectors);
2421}
2422
d8d8778c
DLM
2423static void ata_dev_config_devslp(struct ata_device *dev)
2424{
2425 u8 *sata_setting = dev->link->ap->sector_buf;
2426 unsigned int err_mask;
2427 int i, j;
2428
2429 /*
2430 * Check device sleep capability. Get DevSlp timing variables
2431 * from SATA Settings page of Identify Device Data Log.
2432 */
06f6c4c6
DLM
2433 if (!ata_id_has_devslp(dev->id) ||
2434 !ata_identify_page_supported(dev, ATA_LOG_SATA_SETTINGS))
d8d8778c
DLM
2435 return;
2436
2437 err_mask = ata_read_log_page(dev,
2438 ATA_LOG_IDENTIFY_DEVICE,
2439 ATA_LOG_SATA_SETTINGS,
2440 sata_setting, 1);
fc5c8aa7 2441 if (err_mask)
d8d8778c 2442 return;
d8d8778c
DLM
2443
2444 dev->flags |= ATA_DFLAG_DEVSLP;
2445 for (i = 0; i < ATA_LOG_DEVSLP_SIZE; i++) {
2446 j = ATA_LOG_DEVSLP_OFFSET + i;
2447 dev->devslp_timing[i] = sata_setting[j];
2448 }
2449}
2450
fe22e1c2
DLM
2451static void ata_dev_config_cpr(struct ata_device *dev)
2452{
2453 unsigned int err_mask;
2454 size_t buf_len;
2455 int i, nr_cpr = 0;
2456 struct ata_cpr_log *cpr_log = NULL;
2457 u8 *desc, *buf = NULL;
2458
c745dfc5
TE
2459 if (ata_id_major_version(dev->id) < 11)
2460 goto out;
2461
2462 buf_len = ata_log_supported(dev, ATA_LOG_CONCURRENT_POSITIONING_RANGES);
2463 if (buf_len == 0)
fe22e1c2
DLM
2464 goto out;
2465
2466 /*
fda17afc 2467 * Read the concurrent positioning ranges log (0x47). We can have at
c745dfc5
TE
2468 * most 255 32B range descriptors plus a 64B header. This log varies in
2469 * size, so use the size reported in the GPL directory. Reading beyond
2470 * the supported length will result in an error.
fe22e1c2 2471 */
c745dfc5 2472 buf_len <<= 9;
fe22e1c2
DLM
2473 buf = kzalloc(buf_len, GFP_KERNEL);
2474 if (!buf)
2475 goto out;
2476
fda17afc
DLM
2477 err_mask = ata_read_log_page(dev, ATA_LOG_CONCURRENT_POSITIONING_RANGES,
2478 0, buf, buf_len >> 9);
fe22e1c2
DLM
2479 if (err_mask)
2480 goto out;
2481
2482 nr_cpr = buf[0];
2483 if (!nr_cpr)
2484 goto out;
2485
2486 cpr_log = kzalloc(struct_size(cpr_log, cpr, nr_cpr), GFP_KERNEL);
2487 if (!cpr_log)
2488 goto out;
2489
2490 cpr_log->nr_cpr = nr_cpr;
2491 desc = &buf[64];
2492 for (i = 0; i < nr_cpr; i++, desc += 32) {
2493 cpr_log->cpr[i].num = desc[0];
2494 cpr_log->cpr[i].num_storage_elements = desc[1];
2495 cpr_log->cpr[i].start_lba = get_unaligned_le64(&desc[8]);
2496 cpr_log->cpr[i].num_lbas = get_unaligned_le64(&desc[16]);
2497 }
2498
2499out:
2500 swap(dev->cpr_log, cpr_log);
2501 kfree(cpr_log);
2502 kfree(buf);
2503}
2504
d633b8a7
DLM
2505static void ata_dev_print_features(struct ata_device *dev)
2506{
2507 if (!(dev->flags & ATA_DFLAG_FEATURES_MASK))
2508 return;
2509
2510 ata_dev_info(dev,
fe22e1c2 2511 "Features:%s%s%s%s%s%s\n",
d633b8a7
DLM
2512 dev->flags & ATA_DFLAG_TRUSTED ? " Trust" : "",
2513 dev->flags & ATA_DFLAG_DA ? " Dev-Attention" : "",
2514 dev->flags & ATA_DFLAG_DEVSLP ? " Dev-Sleep" : "",
2515 dev->flags & ATA_DFLAG_NCQ_SEND_RECV ? " NCQ-sndrcv" : "",
fe22e1c2
DLM
2516 dev->flags & ATA_DFLAG_NCQ_PRIO ? " NCQ-prio" : "",
2517 dev->cpr_log ? " CPR" : "");
d633b8a7
DLM
2518}
2519
49016aca 2520/**
ffeae418 2521 * ata_dev_configure - Configure the specified ATA/ATAPI device
ffeae418
TH
2522 * @dev: Target device to configure
2523 *
2524 * Configure @dev according to @dev->id. Generic and low-level
2525 * driver specific fixups are also applied.
49016aca
TH
2526 *
2527 * LOCKING:
ffeae418
TH
2528 * Kernel thread context (may sleep)
2529 *
2530 * RETURNS:
2531 * 0 on success, -errno otherwise
49016aca 2532 */
efdaedc4 2533int ata_dev_configure(struct ata_device *dev)
49016aca 2534{
9af5c9c9 2535 struct ata_port *ap = dev->link->ap;
891fd7c6 2536 bool print_info = ata_dev_print_info(dev);
1148c3a7 2537 const u16 *id = dev->id;
f0a6d77b 2538 unsigned int xfer_mask;
65fe1f0f 2539 unsigned int err_mask;
b352e57d 2540 char revbuf[7]; /* XYZ-99\0 */
3f64f565
EM
2541 char fwrevbuf[ATA_ID_FW_REV_LEN+1];
2542 char modelbuf[ATA_ID_PROD_LEN+1];
e6d902a3 2543 int rc;
49016aca 2544
96c810f2
HR
2545 if (!ata_dev_enabled(dev)) {
2546 ata_dev_dbg(dev, "no device\n");
ffeae418 2547 return 0;
49016aca
TH
2548 }
2549
75683fe7
TH
2550 /* set horkage */
2551 dev->horkage |= ata_dev_blacklisted(dev);
33267325 2552 ata_force_horkage(dev);
75683fe7 2553
50af2fa1 2554 if (dev->horkage & ATA_HORKAGE_DISABLE) {
a9a79dfe 2555 ata_dev_info(dev, "unsupported device, disabling\n");
50af2fa1
TH
2556 ata_dev_disable(dev);
2557 return 0;
2558 }
2559
2486fa56
TH
2560 if ((!atapi_enabled || (ap->flags & ATA_FLAG_NO_ATAPI)) &&
2561 dev->class == ATA_DEV_ATAPI) {
a9a79dfe
JP
2562 ata_dev_warn(dev, "WARNING: ATAPI is %s, device ignored\n",
2563 atapi_enabled ? "not supported with this driver"
2564 : "disabled");
2486fa56
TH
2565 ata_dev_disable(dev);
2566 return 0;
2567 }
2568
9062712f
TH
2569 rc = ata_do_link_spd_horkage(dev);
2570 if (rc)
2571 return rc;
2572
ecd75ad5
TH
2573 /* some WD SATA-1 drives have issues with LPM, turn on NOLPM for them */
2574 if ((dev->horkage & ATA_HORKAGE_WD_BROKEN_LPM) &&
2575 (id[ATA_ID_SATA_CAPABILITY] & 0xe) == 0x2)
2576 dev->horkage |= ATA_HORKAGE_NOLPM;
2577
240630e6
HG
2578 if (ap->flags & ATA_FLAG_NO_LPM)
2579 dev->horkage |= ATA_HORKAGE_NOLPM;
2580
ecd75ad5
TH
2581 if (dev->horkage & ATA_HORKAGE_NOLPM) {
2582 ata_dev_warn(dev, "LPM support broken, forcing max_power\n");
2583 dev->link->ap->target_lpm_policy = ATA_LPM_MAX_POWER;
2584 }
2585
6746544c
TH
2586 /* let ACPI work its magic */
2587 rc = ata_acpi_on_devcfg(dev);
2588 if (rc)
2589 return rc;
08573a86 2590
05027adc
TH
2591 /* massage HPA, do it early as it might change IDENTIFY data */
2592 rc = ata_hpa_resize(dev);
2593 if (rc)
2594 return rc;
2595
c39f5ebe 2596 /* print device capabilities */
17a1e1be
HR
2597 ata_dev_dbg(dev,
2598 "%s: cfg 49:%04x 82:%04x 83:%04x 84:%04x "
2599 "85:%04x 86:%04x 87:%04x 88:%04x\n",
2600 __func__,
2601 id[49], id[82], id[83], id[84],
2602 id[85], id[86], id[87], id[88]);
c39f5ebe 2603
208a9933 2604 /* initialize to-be-configured parameters */
ea1dd4e1 2605 dev->flags &= ~ATA_DFLAG_CFG_MASK;
208a9933
TH
2606 dev->max_sectors = 0;
2607 dev->cdb_len = 0;
2608 dev->n_sectors = 0;
2609 dev->cylinders = 0;
2610 dev->heads = 0;
2611 dev->sectors = 0;
e18086d6 2612 dev->multi_count = 0;
208a9933 2613
1da177e4
LT
2614 /*
2615 * common ATA, ATAPI feature tests
2616 */
2617
ff8854b2 2618 /* find max transfer mode; for printk only */
1148c3a7 2619 xfer_mask = ata_id_xfermask(id);
1da177e4 2620
6044f3c4 2621 ata_dump_id(dev, id);
1da177e4 2622
ef143d57
AL
2623 /* SCSI only uses 4-char revisions, dump full 8 chars from ATA */
2624 ata_id_c_string(dev->id, fwrevbuf, ATA_ID_FW_REV,
2625 sizeof(fwrevbuf));
2626
2627 ata_id_c_string(dev->id, modelbuf, ATA_ID_PROD,
2628 sizeof(modelbuf));
2629
1da177e4 2630 /* ATA-specific feature tests */
9162c657 2631 if (dev->class == ATA_DEV_ATA || dev->class == ATA_DEV_ZAC) {
b352e57d 2632 if (ata_id_is_cfa(id)) {
62afe5d7
SS
2633 /* CPRM may make this media unusable */
2634 if (id[ATA_ID_CFA_KEY_MGMT] & 1)
a9a79dfe
JP
2635 ata_dev_warn(dev,
2636 "supports DRM functions and may not be fully accessible\n");
b352e57d 2637 snprintf(revbuf, 7, "CFA");
ae8d4ee7 2638 } else {
2dcb407e 2639 snprintf(revbuf, 7, "ATA-%d", ata_id_major_version(id));
ae8d4ee7
AC
2640 /* Warn the user if the device has TPM extensions */
2641 if (ata_id_has_tpm(id))
a9a79dfe
JP
2642 ata_dev_warn(dev,
2643 "supports DRM functions and may not be fully accessible\n");
ae8d4ee7 2644 }
b352e57d 2645
1148c3a7 2646 dev->n_sectors = ata_id_n_sectors(id);
2940740b 2647
e18086d6
ML
2648 /* get current R/W Multiple count setting */
2649 if ((dev->id[47] >> 8) == 0x80 && (dev->id[59] & 0x100)) {
2650 unsigned int max = dev->id[47] & 0xff;
2651 unsigned int cnt = dev->id[59] & 0xff;
2652 /* only recognize/allow powers of two here */
2653 if (is_power_of_2(max) && is_power_of_2(cnt))
2654 if (cnt <= max)
2655 dev->multi_count = cnt;
2656 }
3f64f565 2657
891fd7c6 2658 /* print device info to dmesg */
1c95a27c 2659 if (print_info)
891fd7c6
DLM
2660 ata_dev_info(dev, "%s: %s, %s, max %s\n",
2661 revbuf, modelbuf, fwrevbuf,
2662 ata_mode_string(xfer_mask));
8bf62ece 2663
891fd7c6
DLM
2664 if (ata_id_has_lba(id)) {
2665 rc = ata_dev_config_lba(dev);
388539f3
SL
2666 if (rc)
2667 return rc;
ffeae418 2668 } else {
891fd7c6 2669 ata_dev_config_chs(dev);
07f6f7d0
AL
2670 }
2671
d8d8778c 2672 ata_dev_config_devslp(dev);
e87fd28c 2673 ata_dev_config_sense_reporting(dev);
6d1003ae 2674 ata_dev_config_zac(dev);
818831c8 2675 ata_dev_config_trusted(dev);
fe22e1c2 2676 ata_dev_config_cpr(dev);
b1ffbf85 2677 dev->cdb_len = 32;
d633b8a7 2678
1c95a27c 2679 if (print_info)
d633b8a7 2680 ata_dev_print_features(dev);
1da177e4
LT
2681 }
2682
2683 /* ATAPI-specific feature tests */
2c13b7ce 2684 else if (dev->class == ATA_DEV_ATAPI) {
854c73a2
TH
2685 const char *cdb_intr_string = "";
2686 const char *atapi_an_string = "";
91163006 2687 const char *dma_dir_string = "";
7d77b247 2688 u32 sntf;
08a556db 2689
1148c3a7 2690 rc = atapi_cdb_len(id);
1da177e4 2691 if ((rc < 12) || (rc > ATAPI_CDB_LEN)) {
16d42467 2692 ata_dev_warn(dev, "unsupported CDB len %d\n", rc);
ffeae418 2693 rc = -EINVAL;
1da177e4
LT
2694 goto err_out_nosup;
2695 }
6e7846e9 2696 dev->cdb_len = (unsigned int) rc;
1da177e4 2697
7d77b247
TH
2698 /* Enable ATAPI AN if both the host and device have
2699 * the support. If PMP is attached, SNTF is required
2700 * to enable ATAPI AN to discern between PHY status
2701 * changed notifications and ATAPI ANs.
9f45cbd3 2702 */
e7ecd435
TH
2703 if (atapi_an &&
2704 (ap->flags & ATA_FLAG_AN) && ata_id_has_atapi_AN(id) &&
071f44b1 2705 (!sata_pmp_attached(ap) ||
7d77b247 2706 sata_scr_read(&ap->link, SCR_NOTIFICATION, &sntf) == 0)) {
9f45cbd3 2707 /* issue SET feature command to turn this on */
218f3d30
JG
2708 err_mask = ata_dev_set_feature(dev,
2709 SETFEATURES_SATA_ENABLE, SATA_AN);
854c73a2 2710 if (err_mask)
a9a79dfe
JP
2711 ata_dev_err(dev,
2712 "failed to enable ATAPI AN (err_mask=0x%x)\n",
2713 err_mask);
854c73a2 2714 else {
9f45cbd3 2715 dev->flags |= ATA_DFLAG_AN;
854c73a2
TH
2716 atapi_an_string = ", ATAPI AN";
2717 }
9f45cbd3
KCA
2718 }
2719
08a556db 2720 if (ata_id_cdb_intr(dev->id)) {
312f7da2 2721 dev->flags |= ATA_DFLAG_CDB_INTR;
08a556db
AL
2722 cdb_intr_string = ", CDB intr";
2723 }
312f7da2 2724
966fbe19 2725 if (atapi_dmadir || (dev->horkage & ATA_HORKAGE_ATAPI_DMADIR) || atapi_id_dmadir(dev->id)) {
91163006
TH
2726 dev->flags |= ATA_DFLAG_DMADIR;
2727 dma_dir_string = ", DMADIR";
2728 }
2729
afe75951 2730 if (ata_id_has_da(dev->id)) {
b1354cbb 2731 dev->flags |= ATA_DFLAG_DA;
afe75951
AL
2732 zpodd_init(dev);
2733 }
b1354cbb 2734
1da177e4 2735 /* print device info to dmesg */
1c95a27c 2736 if (print_info)
a9a79dfe
JP
2737 ata_dev_info(dev,
2738 "ATAPI: %s, %s, max %s%s%s%s\n",
2739 modelbuf, fwrevbuf,
2740 ata_mode_string(xfer_mask),
2741 cdb_intr_string, atapi_an_string,
2742 dma_dir_string);
1da177e4
LT
2743 }
2744
914ed354
TH
2745 /* determine max_sectors */
2746 dev->max_sectors = ATA_MAX_SECTORS;
2747 if (dev->flags & ATA_DFLAG_LBA48)
2748 dev->max_sectors = ATA_MAX_SECTORS_LBA48;
2749
c5038fc0
AC
2750 /* Limit PATA drive on SATA cable bridge transfers to udma5,
2751 200 sectors */
3373efd8 2752 if (ata_dev_knobble(dev)) {
1c95a27c 2753 if (print_info)
a9a79dfe 2754 ata_dev_info(dev, "applying bridge limits\n");
5a529139 2755 dev->udma_mask &= ATA_UDMA5;
4b2f3ede
TH
2756 dev->max_sectors = ATA_MAX_SECTORS;
2757 }
2758
f8d8e579 2759 if ((dev->class == ATA_DEV_ATAPI) &&
f442cd86 2760 (atapi_command_packet_set(id) == TYPE_TAPE)) {
f8d8e579 2761 dev->max_sectors = ATA_MAX_SECTORS_TAPE;
f442cd86
AL
2762 dev->horkage |= ATA_HORKAGE_STUCK_ERR;
2763 }
f8d8e579 2764
75683fe7 2765 if (dev->horkage & ATA_HORKAGE_MAX_SEC_128)
03ec52de
TH
2766 dev->max_sectors = min_t(unsigned int, ATA_MAX_SECTORS_128,
2767 dev->max_sectors);
18d6e9d5 2768
af34d637
DM
2769 if (dev->horkage & ATA_HORKAGE_MAX_SEC_1024)
2770 dev->max_sectors = min_t(unsigned int, ATA_MAX_SECTORS_1024,
2771 dev->max_sectors);
2772
a32450e1
SH
2773 if (dev->horkage & ATA_HORKAGE_MAX_SEC_LBA48)
2774 dev->max_sectors = ATA_MAX_SECTORS_LBA48;
2775
4b2f3ede 2776 if (ap->ops->dev_config)
cd0d3bbc 2777 ap->ops->dev_config(dev);
4b2f3ede 2778
c5038fc0
AC
2779 if (dev->horkage & ATA_HORKAGE_DIAGNOSTIC) {
2780 /* Let the user know. We don't want to disallow opens for
2781 rescue purposes, or in case the vendor is just a blithering
2782 idiot. Do this after the dev_config call as some controllers
2783 with buggy firmware may want to avoid reporting false device
2784 bugs */
2785
2786 if (print_info) {
a9a79dfe 2787 ata_dev_warn(dev,
c5038fc0 2788"Drive reports diagnostics failure. This may indicate a drive\n");
a9a79dfe 2789 ata_dev_warn(dev,
c5038fc0
AC
2790"fault or invalid emulation. Contact drive vendor for information.\n");
2791 }
2792 }
2793
ac70a964 2794 if ((dev->horkage & ATA_HORKAGE_FIRMWARE_WARN) && print_info) {
a9a79dfe
JP
2795 ata_dev_warn(dev, "WARNING: device requires firmware update to be fully functional\n");
2796 ata_dev_warn(dev, " contact the vendor or visit http://ata.wiki.kernel.org\n");
ac70a964
TH
2797 }
2798
ffeae418 2799 return 0;
1da177e4
LT
2800
2801err_out_nosup:
ffeae418 2802 return rc;
1da177e4
LT
2803}
2804
be0d18df 2805/**
2e41e8e6 2806 * ata_cable_40wire - return 40 wire cable type
be0d18df
AC
2807 * @ap: port
2808 *
2e41e8e6 2809 * Helper method for drivers which want to hardwire 40 wire cable
be0d18df
AC
2810 * detection.
2811 */
2812
2813int ata_cable_40wire(struct ata_port *ap)
2814{
2815 return ATA_CBL_PATA40;
2816}
a52fbcfc 2817EXPORT_SYMBOL_GPL(ata_cable_40wire);
be0d18df
AC
2818
2819/**
2e41e8e6 2820 * ata_cable_80wire - return 80 wire cable type
be0d18df
AC
2821 * @ap: port
2822 *
2e41e8e6 2823 * Helper method for drivers which want to hardwire 80 wire cable
be0d18df
AC
2824 * detection.
2825 */
2826
2827int ata_cable_80wire(struct ata_port *ap)
2828{
2829 return ATA_CBL_PATA80;
2830}
a52fbcfc 2831EXPORT_SYMBOL_GPL(ata_cable_80wire);
be0d18df
AC
2832
2833/**
2834 * ata_cable_unknown - return unknown PATA cable.
2835 * @ap: port
2836 *
2837 * Helper method for drivers which have no PATA cable detection.
2838 */
2839
2840int ata_cable_unknown(struct ata_port *ap)
2841{
2842 return ATA_CBL_PATA_UNK;
2843}
a52fbcfc 2844EXPORT_SYMBOL_GPL(ata_cable_unknown);
be0d18df 2845
c88f90c3
TH
2846/**
2847 * ata_cable_ignore - return ignored PATA cable.
2848 * @ap: port
2849 *
2850 * Helper method for drivers which don't use cable type to limit
2851 * transfer mode.
2852 */
2853int ata_cable_ignore(struct ata_port *ap)
2854{
2855 return ATA_CBL_PATA_IGN;
2856}
a52fbcfc 2857EXPORT_SYMBOL_GPL(ata_cable_ignore);
c88f90c3 2858
be0d18df
AC
2859/**
2860 * ata_cable_sata - return SATA cable type
2861 * @ap: port
2862 *
2863 * Helper method for drivers which have SATA cables
2864 */
2865
2866int ata_cable_sata(struct ata_port *ap)
2867{
2868 return ATA_CBL_SATA;
2869}
a52fbcfc 2870EXPORT_SYMBOL_GPL(ata_cable_sata);
be0d18df 2871
1da177e4
LT
2872/**
2873 * ata_bus_probe - Reset and probe ATA bus
2874 * @ap: Bus to probe
2875 *
0cba632b
JG
2876 * Master ATA bus probing function. Initiates a hardware-dependent
2877 * bus reset, then attempts to identify any devices found on
2878 * the bus.
2879 *
1da177e4 2880 * LOCKING:
0cba632b 2881 * PCI/etc. bus probe sem.
1da177e4
LT
2882 *
2883 * RETURNS:
96072e69 2884 * Zero on success, negative errno otherwise.
1da177e4
LT
2885 */
2886
80289167 2887int ata_bus_probe(struct ata_port *ap)
1da177e4 2888{
28ca5c57 2889 unsigned int classes[ATA_MAX_DEVICES];
14d2bac1 2890 int tries[ATA_MAX_DEVICES];
f58229f8 2891 int rc;
e82cbdb9 2892 struct ata_device *dev;
1da177e4 2893
1eca4365 2894 ata_for_each_dev(dev, &ap->link, ALL)
f58229f8 2895 tries[dev->devno] = ATA_PROBE_MAX_TRIES;
14d2bac1
TH
2896
2897 retry:
1eca4365 2898 ata_for_each_dev(dev, &ap->link, ALL) {
cdeab114
TH
2899 /* If we issue an SRST then an ATA drive (not ATAPI)
2900 * may change configuration and be in PIO0 timing. If
2901 * we do a hard reset (or are coming from power on)
2902 * this is true for ATA or ATAPI. Until we've set a
2903 * suitable controller mode we should not touch the
2904 * bus as we may be talking too fast.
2905 */
2906 dev->pio_mode = XFER_PIO_0;
5416912a 2907 dev->dma_mode = 0xff;
cdeab114
TH
2908
2909 /* If the controller has a pio mode setup function
2910 * then use it to set the chipset to rights. Don't
2911 * touch the DMA setup as that will be dealt with when
2912 * configuring devices.
2913 */
2914 if (ap->ops->set_piomode)
2915 ap->ops->set_piomode(ap, dev);
2916 }
2917
2044470c 2918 /* reset and determine device classes */
52783c5d 2919 ap->ops->phy_reset(ap);
2061a47a 2920
1eca4365 2921 ata_for_each_dev(dev, &ap->link, ALL) {
3e4ec344 2922 if (dev->class != ATA_DEV_UNKNOWN)
52783c5d
TH
2923 classes[dev->devno] = dev->class;
2924 else
2925 classes[dev->devno] = ATA_DEV_NONE;
2044470c 2926
52783c5d 2927 dev->class = ATA_DEV_UNKNOWN;
28ca5c57 2928 }
1da177e4 2929
f31f0cc2
JG
2930 /* read IDENTIFY page and configure devices. We have to do the identify
2931 specific sequence bass-ackwards so that PDIAG- is released by
2932 the slave device */
2933
1eca4365 2934 ata_for_each_dev(dev, &ap->link, ALL_REVERSE) {
f58229f8
TH
2935 if (tries[dev->devno])
2936 dev->class = classes[dev->devno];
ffeae418 2937
14d2bac1 2938 if (!ata_dev_enabled(dev))
ffeae418 2939 continue;
ffeae418 2940
bff04647
TH
2941 rc = ata_dev_read_id(dev, &dev->class, ATA_READID_POSTRESET,
2942 dev->id);
14d2bac1
TH
2943 if (rc)
2944 goto fail;
f31f0cc2
JG
2945 }
2946
be0d18df
AC
2947 /* Now ask for the cable type as PDIAG- should have been released */
2948 if (ap->ops->cable_detect)
2949 ap->cbl = ap->ops->cable_detect(ap);
2950
1eca4365
TH
2951 /* We may have SATA bridge glue hiding here irrespective of
2952 * the reported cable types and sensed types. When SATA
2953 * drives indicate we have a bridge, we don't know which end
2954 * of the link the bridge is which is a problem.
2955 */
2956 ata_for_each_dev(dev, &ap->link, ENABLED)
614fe29b
AC
2957 if (ata_id_is_sata(dev->id))
2958 ap->cbl = ATA_CBL_SATA;
614fe29b 2959
f31f0cc2
JG
2960 /* After the identify sequence we can now set up the devices. We do
2961 this in the normal order so that the user doesn't get confused */
2962
1eca4365 2963 ata_for_each_dev(dev, &ap->link, ENABLED) {
9af5c9c9 2964 ap->link.eh_context.i.flags |= ATA_EHI_PRINTINFO;
efdaedc4 2965 rc = ata_dev_configure(dev);
9af5c9c9 2966 ap->link.eh_context.i.flags &= ~ATA_EHI_PRINTINFO;
14d2bac1
TH
2967 if (rc)
2968 goto fail;
1da177e4
LT
2969 }
2970
e82cbdb9 2971 /* configure transfer mode */
0260731f 2972 rc = ata_set_mode(&ap->link, &dev);
4ae72a1e 2973 if (rc)
51713d35 2974 goto fail;
1da177e4 2975
1eca4365
TH
2976 ata_for_each_dev(dev, &ap->link, ENABLED)
2977 return 0;
1da177e4 2978
96072e69 2979 return -ENODEV;
14d2bac1
TH
2980
2981 fail:
4ae72a1e
TH
2982 tries[dev->devno]--;
2983
14d2bac1
TH
2984 switch (rc) {
2985 case -EINVAL:
4ae72a1e 2986 /* eeek, something went very wrong, give up */
14d2bac1
TH
2987 tries[dev->devno] = 0;
2988 break;
4ae72a1e
TH
2989
2990 case -ENODEV:
2991 /* give it just one more chance */
2992 tries[dev->devno] = min(tries[dev->devno], 1);
df561f66 2993 fallthrough;
14d2bac1 2994 case -EIO:
4ae72a1e
TH
2995 if (tries[dev->devno] == 1) {
2996 /* This is the last chance, better to slow
2997 * down than lose it.
2998 */
a07d499b 2999 sata_down_spd_limit(&ap->link, 0);
4ae72a1e
TH
3000 ata_down_xfermask_limit(dev, ATA_DNXFER_PIO);
3001 }
14d2bac1
TH
3002 }
3003
4ae72a1e 3004 if (!tries[dev->devno])
3373efd8 3005 ata_dev_disable(dev);
ec573755 3006
14d2bac1 3007 goto retry;
1da177e4
LT
3008}
3009
3be680b7
TH
3010/**
3011 * sata_print_link_status - Print SATA link status
936fd732 3012 * @link: SATA link to printk link status about
3be680b7
TH
3013 *
3014 * This function prints link speed and status of a SATA link.
3015 *
3016 * LOCKING:
3017 * None.
3018 */
6bdb4fc9 3019static void sata_print_link_status(struct ata_link *link)
3be680b7 3020{
6d5f9732 3021 u32 sstatus, scontrol, tmp;
3be680b7 3022
936fd732 3023 if (sata_scr_read(link, SCR_STATUS, &sstatus))
3be680b7 3024 return;
55d5ba55
LZ
3025 if (sata_scr_read(link, SCR_CONTROL, &scontrol))
3026 return;
3be680b7 3027
b1c72916 3028 if (ata_phys_link_online(link)) {
3be680b7 3029 tmp = (sstatus >> 4) & 0xf;
a9a79dfe
JP
3030 ata_link_info(link, "SATA link up %s (SStatus %X SControl %X)\n",
3031 sata_spd_string(tmp), sstatus, scontrol);
3be680b7 3032 } else {
a9a79dfe
JP
3033 ata_link_info(link, "SATA link down (SStatus %X SControl %X)\n",
3034 sstatus, scontrol);
3be680b7
TH
3035 }
3036}
3037
ebdfca6e
AC
3038/**
3039 * ata_dev_pair - return other device on cable
ebdfca6e
AC
3040 * @adev: device
3041 *
3042 * Obtain the other device on the same cable, or if none is
3043 * present NULL is returned
3044 */
2e9edbf8 3045
3373efd8 3046struct ata_device *ata_dev_pair(struct ata_device *adev)
ebdfca6e 3047{
9af5c9c9
TH
3048 struct ata_link *link = adev->link;
3049 struct ata_device *pair = &link->device[1 - adev->devno];
e1211e3f 3050 if (!ata_dev_enabled(pair))
ebdfca6e
AC
3051 return NULL;
3052 return pair;
3053}
a52fbcfc 3054EXPORT_SYMBOL_GPL(ata_dev_pair);
ebdfca6e 3055
1c3fae4d 3056/**
3c567b7d 3057 * sata_down_spd_limit - adjust SATA spd limit downward
936fd732 3058 * @link: Link to adjust SATA spd limit for
a07d499b 3059 * @spd_limit: Additional limit
1c3fae4d 3060 *
936fd732 3061 * Adjust SATA spd limit of @link downward. Note that this
1c3fae4d 3062 * function only adjusts the limit. The change must be applied
3c567b7d 3063 * using sata_set_spd().
1c3fae4d 3064 *
a07d499b
TH
3065 * If @spd_limit is non-zero, the speed is limited to equal to or
3066 * lower than @spd_limit if such speed is supported. If
3067 * @spd_limit is slower than any supported speed, only the lowest
3068 * supported speed is allowed.
3069 *
1c3fae4d
TH
3070 * LOCKING:
3071 * Inherited from caller.
3072 *
3073 * RETURNS:
3074 * 0 on success, negative errno on failure
3075 */
a07d499b 3076int sata_down_spd_limit(struct ata_link *link, u32 spd_limit)
1c3fae4d 3077{
81952c54 3078 u32 sstatus, spd, mask;
a07d499b 3079 int rc, bit;
1c3fae4d 3080
936fd732 3081 if (!sata_scr_valid(link))
008a7896
TH
3082 return -EOPNOTSUPP;
3083
3084 /* If SCR can be read, use it to determine the current SPD.
936fd732 3085 * If not, use cached value in link->sata_spd.
008a7896 3086 */
936fd732 3087 rc = sata_scr_read(link, SCR_STATUS, &sstatus);
9913ff8a 3088 if (rc == 0 && ata_sstatus_online(sstatus))
008a7896
TH
3089 spd = (sstatus >> 4) & 0xf;
3090 else
936fd732 3091 spd = link->sata_spd;
1c3fae4d 3092
936fd732 3093 mask = link->sata_spd_limit;
1c3fae4d
TH
3094 if (mask <= 1)
3095 return -EINVAL;
008a7896
TH
3096
3097 /* unconditionally mask off the highest bit */
a07d499b
TH
3098 bit = fls(mask) - 1;
3099 mask &= ~(1 << bit);
1c3fae4d 3100
2dc0b46b
DM
3101 /*
3102 * Mask off all speeds higher than or equal to the current one. At
3103 * this point, if current SPD is not available and we previously
3104 * recorded the link speed from SStatus, the driver has already
3105 * masked off the highest bit so mask should already be 1 or 0.
3106 * Otherwise, we should not force 1.5Gbps on a link where we have
3107 * not previously recorded speed from SStatus. Just return in this
3108 * case.
008a7896
TH
3109 */
3110 if (spd > 1)
3111 mask &= (1 << (spd - 1)) - 1;
3112 else
2dc0b46b 3113 return -EINVAL;
008a7896
TH
3114
3115 /* were we already at the bottom? */
1c3fae4d
TH
3116 if (!mask)
3117 return -EINVAL;
3118
a07d499b
TH
3119 if (spd_limit) {
3120 if (mask & ((1 << spd_limit) - 1))
3121 mask &= (1 << spd_limit) - 1;
3122 else {
3123 bit = ffs(mask) - 1;
3124 mask = 1 << bit;
3125 }
3126 }
3127
936fd732 3128 link->sata_spd_limit = mask;
1c3fae4d 3129
a9a79dfe
JP
3130 ata_link_warn(link, "limiting SATA link speed to %s\n",
3131 sata_spd_string(fls(mask)));
1c3fae4d
TH
3132
3133 return 0;
3134}
3135
a9b2c120 3136#ifdef CONFIG_ATA_ACPI
a0f79b92
TH
3137/**
3138 * ata_timing_cycle2mode - find xfer mode for the specified cycle duration
3139 * @xfer_shift: ATA_SHIFT_* value for transfer type to examine.
3140 * @cycle: cycle duration in ns
3141 *
3142 * Return matching xfer mode for @cycle. The returned mode is of
3143 * the transfer type specified by @xfer_shift. If @cycle is too
3144 * slow for @xfer_shift, 0xff is returned. If @cycle is faster
3145 * than the fastest known mode, the fasted mode is returned.
3146 *
3147 * LOCKING:
3148 * None.
3149 *
3150 * RETURNS:
3151 * Matching xfer_mode, 0xff if no match found.
3152 */
3153u8 ata_timing_cycle2mode(unsigned int xfer_shift, int cycle)
3154{
3155 u8 base_mode = 0xff, last_mode = 0xff;
3156 const struct ata_xfer_ent *ent;
3157 const struct ata_timing *t;
3158
3159 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
3160 if (ent->shift == xfer_shift)
3161 base_mode = ent->base;
3162
3163 for (t = ata_timing_find_mode(base_mode);
3164 t && ata_xfer_mode2shift(t->mode) == xfer_shift; t++) {
3165 unsigned short this_cycle;
3166
3167 switch (xfer_shift) {
3168 case ATA_SHIFT_PIO:
3169 case ATA_SHIFT_MWDMA:
3170 this_cycle = t->cycle;
3171 break;
3172 case ATA_SHIFT_UDMA:
3173 this_cycle = t->udma;
3174 break;
3175 default:
3176 return 0xff;
3177 }
3178
3179 if (cycle > this_cycle)
3180 break;
3181
3182 last_mode = t->mode;
3183 }
3184
3185 return last_mode;
3186}
a9b2c120 3187#endif
a0f79b92 3188
cf176e1a
TH
3189/**
3190 * ata_down_xfermask_limit - adjust dev xfer masks downward
cf176e1a 3191 * @dev: Device to adjust xfer masks
458337db 3192 * @sel: ATA_DNXFER_* selector
cf176e1a
TH
3193 *
3194 * Adjust xfer masks of @dev downward. Note that this function
3195 * does not apply the change. Invoking ata_set_mode() afterwards
3196 * will apply the limit.
3197 *
3198 * LOCKING:
3199 * Inherited from caller.
3200 *
3201 * RETURNS:
3202 * 0 on success, negative errno on failure
3203 */
458337db 3204int ata_down_xfermask_limit(struct ata_device *dev, unsigned int sel)
cf176e1a 3205{
458337db 3206 char buf[32];
f0a6d77b
SS
3207 unsigned int orig_mask, xfer_mask;
3208 unsigned int pio_mask, mwdma_mask, udma_mask;
458337db 3209 int quiet, highbit;
cf176e1a 3210
458337db
TH
3211 quiet = !!(sel & ATA_DNXFER_QUIET);
3212 sel &= ~ATA_DNXFER_QUIET;
cf176e1a 3213
458337db
TH
3214 xfer_mask = orig_mask = ata_pack_xfermask(dev->pio_mask,
3215 dev->mwdma_mask,
3216 dev->udma_mask);
3217 ata_unpack_xfermask(xfer_mask, &pio_mask, &mwdma_mask, &udma_mask);
cf176e1a 3218
458337db
TH
3219 switch (sel) {
3220 case ATA_DNXFER_PIO:
3221 highbit = fls(pio_mask) - 1;
3222 pio_mask &= ~(1 << highbit);
3223 break;
3224
3225 case ATA_DNXFER_DMA:
3226 if (udma_mask) {
3227 highbit = fls(udma_mask) - 1;
3228 udma_mask &= ~(1 << highbit);
3229 if (!udma_mask)
3230 return -ENOENT;
3231 } else if (mwdma_mask) {
3232 highbit = fls(mwdma_mask) - 1;
3233 mwdma_mask &= ~(1 << highbit);
3234 if (!mwdma_mask)
3235 return -ENOENT;
3236 }
3237 break;
3238
3239 case ATA_DNXFER_40C:
3240 udma_mask &= ATA_UDMA_MASK_40C;
3241 break;
3242
3243 case ATA_DNXFER_FORCE_PIO0:
3244 pio_mask &= 1;
df561f66 3245 fallthrough;
458337db
TH
3246 case ATA_DNXFER_FORCE_PIO:
3247 mwdma_mask = 0;
3248 udma_mask = 0;
3249 break;
3250
458337db
TH
3251 default:
3252 BUG();
3253 }
3254
3255 xfer_mask &= ata_pack_xfermask(pio_mask, mwdma_mask, udma_mask);
3256
3257 if (!(xfer_mask & ATA_MASK_PIO) || xfer_mask == orig_mask)
3258 return -ENOENT;
3259
3260 if (!quiet) {
3261 if (xfer_mask & (ATA_MASK_MWDMA | ATA_MASK_UDMA))
3262 snprintf(buf, sizeof(buf), "%s:%s",
3263 ata_mode_string(xfer_mask),
3264 ata_mode_string(xfer_mask & ATA_MASK_PIO));
3265 else
3266 snprintf(buf, sizeof(buf), "%s",
3267 ata_mode_string(xfer_mask));
3268
a9a79dfe 3269 ata_dev_warn(dev, "limiting speed to %s\n", buf);
458337db 3270 }
cf176e1a
TH
3271
3272 ata_unpack_xfermask(xfer_mask, &dev->pio_mask, &dev->mwdma_mask,
3273 &dev->udma_mask);
3274
cf176e1a 3275 return 0;
cf176e1a
TH
3276}
3277
3373efd8 3278static int ata_dev_set_mode(struct ata_device *dev)
1da177e4 3279{
d0cb43b3 3280 struct ata_port *ap = dev->link->ap;
9af5c9c9 3281 struct ata_eh_context *ehc = &dev->link->eh_context;
d0cb43b3 3282 const bool nosetxfer = dev->horkage & ATA_HORKAGE_NOSETXFER;
4055dee7
TH
3283 const char *dev_err_whine = "";
3284 int ign_dev_err = 0;
d0cb43b3 3285 unsigned int err_mask = 0;
83206a29 3286 int rc;
1da177e4 3287
e8384607 3288 dev->flags &= ~ATA_DFLAG_PIO;
1da177e4
LT
3289 if (dev->xfer_shift == ATA_SHIFT_PIO)
3290 dev->flags |= ATA_DFLAG_PIO;
3291
d0cb43b3
TH
3292 if (nosetxfer && ap->flags & ATA_FLAG_SATA && ata_id_is_sata(dev->id))
3293 dev_err_whine = " (SET_XFERMODE skipped)";
3294 else {
3295 if (nosetxfer)
a9a79dfe
JP
3296 ata_dev_warn(dev,
3297 "NOSETXFER but PATA detected - can't "
3298 "skip SETXFER, might malfunction\n");
d0cb43b3
TH
3299 err_mask = ata_dev_set_xfermode(dev);
3300 }
2dcb407e 3301
4055dee7
TH
3302 if (err_mask & ~AC_ERR_DEV)
3303 goto fail;
3304
3305 /* revalidate */
3306 ehc->i.flags |= ATA_EHI_POST_SETMODE;
3307 rc = ata_dev_revalidate(dev, ATA_DEV_UNKNOWN, 0);
3308 ehc->i.flags &= ~ATA_EHI_POST_SETMODE;
3309 if (rc)
3310 return rc;
3311
b93fda12
AC
3312 if (dev->xfer_shift == ATA_SHIFT_PIO) {
3313 /* Old CFA may refuse this command, which is just fine */
3314 if (ata_id_is_cfa(dev->id))
3315 ign_dev_err = 1;
3316 /* Catch several broken garbage emulations plus some pre
3317 ATA devices */
3318 if (ata_id_major_version(dev->id) == 0 &&
3319 dev->pio_mode <= XFER_PIO_2)
3320 ign_dev_err = 1;
3321 /* Some very old devices and some bad newer ones fail
3322 any kind of SET_XFERMODE request but support PIO0-2
3323 timings and no IORDY */
3324 if (!ata_id_has_iordy(dev->id) && dev->pio_mode <= XFER_PIO_2)
3325 ign_dev_err = 1;
3326 }
3acaf94b
AC
3327 /* Early MWDMA devices do DMA but don't allow DMA mode setting.
3328 Don't fail an MWDMA0 set IFF the device indicates it is in MWDMA0 */
c5038fc0 3329 if (dev->xfer_shift == ATA_SHIFT_MWDMA &&
3acaf94b
AC
3330 dev->dma_mode == XFER_MW_DMA_0 &&
3331 (dev->id[63] >> 8) & 1)
4055dee7 3332 ign_dev_err = 1;
3acaf94b 3333
4055dee7
TH
3334 /* if the device is actually configured correctly, ignore dev err */
3335 if (dev->xfer_mode == ata_xfer_mask2mode(ata_id_xfermask(dev->id)))
3336 ign_dev_err = 1;
1da177e4 3337
4055dee7
TH
3338 if (err_mask & AC_ERR_DEV) {
3339 if (!ign_dev_err)
3340 goto fail;
3341 else
3342 dev_err_whine = " (device error ignored)";
3343 }
48a8a14f 3344
4633778b
HR
3345 ata_dev_dbg(dev, "xfer_shift=%u, xfer_mode=0x%x\n",
3346 dev->xfer_shift, (int)dev->xfer_mode);
1da177e4 3347
07b9b6d6
DLM
3348 if (!(ehc->i.flags & ATA_EHI_QUIET) ||
3349 ehc->i.flags & ATA_EHI_DID_HARDRESET)
3350 ata_dev_info(dev, "configured for %s%s\n",
3351 ata_mode_string(ata_xfer_mode2mask(dev->xfer_mode)),
3352 dev_err_whine);
4055dee7 3353
83206a29 3354 return 0;
4055dee7
TH
3355
3356 fail:
a9a79dfe 3357 ata_dev_err(dev, "failed to set xfermode (err_mask=0x%x)\n", err_mask);
4055dee7 3358 return -EIO;
1da177e4
LT
3359}
3360
1da177e4 3361/**
04351821 3362 * ata_do_set_mode - Program timings and issue SET FEATURES - XFER
0260731f 3363 * @link: link on which timings will be programmed
1967b7ff 3364 * @r_failed_dev: out parameter for failed device
1da177e4 3365 *
04351821
A
3366 * Standard implementation of the function used to tune and set
3367 * ATA device disk transfer mode (PIO3, UDMA6, etc.). If
3368 * ata_dev_set_mode() fails, pointer to the failing device is
e82cbdb9 3369 * returned in @r_failed_dev.
780a87f7 3370 *
1da177e4 3371 * LOCKING:
0cba632b 3372 * PCI/etc. bus probe sem.
e82cbdb9
TH
3373 *
3374 * RETURNS:
3375 * 0 on success, negative errno otherwise
1da177e4 3376 */
04351821 3377
0260731f 3378int ata_do_set_mode(struct ata_link *link, struct ata_device **r_failed_dev)
1da177e4 3379{
0260731f 3380 struct ata_port *ap = link->ap;
e8e0619f 3381 struct ata_device *dev;
f58229f8 3382 int rc = 0, used_dma = 0, found = 0;
3adcebb2 3383
a6d5a51c 3384 /* step 1: calculate xfer_mask */
1eca4365 3385 ata_for_each_dev(dev, link, ENABLED) {
f0a6d77b 3386 unsigned int pio_mask, dma_mask;
b3a70601 3387 unsigned int mode_mask;
a6d5a51c 3388
b3a70601
AC
3389 mode_mask = ATA_DMA_MASK_ATA;
3390 if (dev->class == ATA_DEV_ATAPI)
3391 mode_mask = ATA_DMA_MASK_ATAPI;
3392 else if (ata_id_is_cfa(dev->id))
3393 mode_mask = ATA_DMA_MASK_CFA;
3394
3373efd8 3395 ata_dev_xfermask(dev);
33267325 3396 ata_force_xfermask(dev);
1da177e4 3397
acf356b1 3398 pio_mask = ata_pack_xfermask(dev->pio_mask, 0, 0);
b3a70601
AC
3399
3400 if (libata_dma_mask & mode_mask)
80a9c430
SS
3401 dma_mask = ata_pack_xfermask(0, dev->mwdma_mask,
3402 dev->udma_mask);
b3a70601
AC
3403 else
3404 dma_mask = 0;
3405
acf356b1
TH
3406 dev->pio_mode = ata_xfer_mask2mode(pio_mask);
3407 dev->dma_mode = ata_xfer_mask2mode(dma_mask);
5444a6f4 3408
4f65977d 3409 found = 1;
b15b3eba 3410 if (ata_dma_enabled(dev))
5444a6f4 3411 used_dma = 1;
a6d5a51c 3412 }
4f65977d 3413 if (!found)
e82cbdb9 3414 goto out;
a6d5a51c
TH
3415
3416 /* step 2: always set host PIO timings */
1eca4365 3417 ata_for_each_dev(dev, link, ENABLED) {
70cd071e 3418 if (dev->pio_mode == 0xff) {
a9a79dfe 3419 ata_dev_warn(dev, "no PIO support\n");
e8e0619f 3420 rc = -EINVAL;
e82cbdb9 3421 goto out;
e8e0619f
TH
3422 }
3423
3424 dev->xfer_mode = dev->pio_mode;
3425 dev->xfer_shift = ATA_SHIFT_PIO;
3426 if (ap->ops->set_piomode)
3427 ap->ops->set_piomode(ap, dev);
3428 }
1da177e4 3429
a6d5a51c 3430 /* step 3: set host DMA timings */
1eca4365
TH
3431 ata_for_each_dev(dev, link, ENABLED) {
3432 if (!ata_dma_enabled(dev))
e8e0619f
TH
3433 continue;
3434
3435 dev->xfer_mode = dev->dma_mode;
3436 dev->xfer_shift = ata_xfer_mode2shift(dev->dma_mode);
3437 if (ap->ops->set_dmamode)
3438 ap->ops->set_dmamode(ap, dev);
3439 }
1da177e4
LT
3440
3441 /* step 4: update devices' xfer mode */
1eca4365 3442 ata_for_each_dev(dev, link, ENABLED) {
3373efd8 3443 rc = ata_dev_set_mode(dev);
5bbc53f4 3444 if (rc)
e82cbdb9 3445 goto out;
83206a29 3446 }
1da177e4 3447
e8e0619f
TH
3448 /* Record simplex status. If we selected DMA then the other
3449 * host channels are not permitted to do so.
5444a6f4 3450 */
cca3974e 3451 if (used_dma && (ap->host->flags & ATA_HOST_SIMPLEX))
032af1ce 3452 ap->host->simplex_claimed = ap;
5444a6f4 3453
e82cbdb9
TH
3454 out:
3455 if (rc)
3456 *r_failed_dev = dev;
3457 return rc;
1da177e4 3458}
a52fbcfc 3459EXPORT_SYMBOL_GPL(ata_do_set_mode);
1da177e4 3460
aa2731ad
TH
3461/**
3462 * ata_wait_ready - wait for link to become ready
3463 * @link: link to be waited on
3464 * @deadline: deadline jiffies for the operation
3465 * @check_ready: callback to check link readiness
3466 *
3467 * Wait for @link to become ready. @check_ready should return
3468 * positive number if @link is ready, 0 if it isn't, -ENODEV if
3469 * link doesn't seem to be occupied, other errno for other error
3470 * conditions.
3471 *
3472 * Transient -ENODEV conditions are allowed for
3473 * ATA_TMOUT_FF_WAIT.
3474 *
3475 * LOCKING:
3476 * EH context.
3477 *
3478 * RETURNS:
c9b5560a 3479 * 0 if @link is ready before @deadline; otherwise, -errno.
aa2731ad
TH
3480 */
3481int ata_wait_ready(struct ata_link *link, unsigned long deadline,
3482 int (*check_ready)(struct ata_link *link))
3483{
3484 unsigned long start = jiffies;
b48d58f5 3485 unsigned long nodev_deadline;
aa2731ad
TH
3486 int warned = 0;
3487
b48d58f5
TH
3488 /* choose which 0xff timeout to use, read comment in libata.h */
3489 if (link->ap->host->flags & ATA_HOST_PARALLEL_SCAN)
3490 nodev_deadline = ata_deadline(start, ATA_TMOUT_FF_WAIT_LONG);
3491 else
3492 nodev_deadline = ata_deadline(start, ATA_TMOUT_FF_WAIT);
3493
b1c72916
TH
3494 /* Slave readiness can't be tested separately from master. On
3495 * M/S emulation configuration, this function should be called
3496 * only on the master and it will handle both master and slave.
3497 */
3498 WARN_ON(link == link->ap->slave_link);
3499
aa2731ad
TH
3500 if (time_after(nodev_deadline, deadline))
3501 nodev_deadline = deadline;
3502
3503 while (1) {
3504 unsigned long now = jiffies;
3505 int ready, tmp;
3506
3507 ready = tmp = check_ready(link);
3508 if (ready > 0)
3509 return 0;
3510
b48d58f5
TH
3511 /*
3512 * -ENODEV could be transient. Ignore -ENODEV if link
aa2731ad 3513 * is online. Also, some SATA devices take a long
b48d58f5
TH
3514 * time to clear 0xff after reset. Wait for
3515 * ATA_TMOUT_FF_WAIT[_LONG] on -ENODEV if link isn't
3516 * offline.
aa2731ad
TH
3517 *
3518 * Note that some PATA controllers (pata_ali) explode
3519 * if status register is read more than once when
3520 * there's no device attached.
3521 */
3522 if (ready == -ENODEV) {
3523 if (ata_link_online(link))
3524 ready = 0;
3525 else if ((link->ap->flags & ATA_FLAG_SATA) &&
3526 !ata_link_offline(link) &&
3527 time_before(now, nodev_deadline))
3528 ready = 0;
3529 }
3530
3531 if (ready)
3532 return ready;
3533 if (time_after(now, deadline))
3534 return -EBUSY;
3535
3536 if (!warned && time_after(now, start + 5 * HZ) &&
3537 (deadline - now > 3 * HZ)) {
a9a79dfe 3538 ata_link_warn(link,
aa2731ad
TH
3539 "link is slow to respond, please be patient "
3540 "(ready=%d)\n", tmp);
3541 warned = 1;
3542 }
3543
97750ceb 3544 ata_msleep(link->ap, 50);
aa2731ad
TH
3545 }
3546}
3547
3548/**
3549 * ata_wait_after_reset - wait for link to become ready after reset
3550 * @link: link to be waited on
3551 * @deadline: deadline jiffies for the operation
3552 * @check_ready: callback to check link readiness
3553 *
3554 * Wait for @link to become ready after reset.
3555 *
3556 * LOCKING:
3557 * EH context.
3558 *
3559 * RETURNS:
c9b5560a 3560 * 0 if @link is ready before @deadline; otherwise, -errno.
aa2731ad 3561 */
2b4221bb 3562int ata_wait_after_reset(struct ata_link *link, unsigned long deadline,
aa2731ad
TH
3563 int (*check_ready)(struct ata_link *link))
3564{
97750ceb 3565 ata_msleep(link->ap, ATA_WAIT_AFTER_RESET);
aa2731ad
TH
3566
3567 return ata_wait_ready(link, deadline, check_ready);
3568}
a52fbcfc 3569EXPORT_SYMBOL_GPL(ata_wait_after_reset);
aa2731ad 3570
f5914a46 3571/**
0aa1113d 3572 * ata_std_prereset - prepare for reset
cc0680a5 3573 * @link: ATA link to be reset
d4b2bab4 3574 * @deadline: deadline jiffies for the operation
f5914a46 3575 *
cc0680a5 3576 * @link is about to be reset. Initialize it. Failure from
b8cffc6a
TH
3577 * prereset makes libata abort whole reset sequence and give up
3578 * that port, so prereset should be best-effort. It does its
3579 * best to prepare for reset sequence but if things go wrong, it
3580 * should just whine, not fail.
f5914a46
TH
3581 *
3582 * LOCKING:
3583 * Kernel thread context (may sleep)
3584 *
3585 * RETURNS:
ac1eb665 3586 * Always 0.
f5914a46 3587 */
0aa1113d 3588int ata_std_prereset(struct ata_link *link, unsigned long deadline)
f5914a46 3589{
cc0680a5 3590 struct ata_port *ap = link->ap;
936fd732 3591 struct ata_eh_context *ehc = &link->eh_context;
e9c83914 3592 const unsigned long *timing = sata_ehc_deb_timing(ehc);
f5914a46
TH
3593 int rc;
3594
f5914a46
TH
3595 /* if we're about to do hardreset, nothing more to do */
3596 if (ehc->i.action & ATA_EH_HARDRESET)
3597 return 0;
3598
936fd732 3599 /* if SATA, resume link */
a16abc0b 3600 if (ap->flags & ATA_FLAG_SATA) {
936fd732 3601 rc = sata_link_resume(link, timing, deadline);
b8cffc6a
TH
3602 /* whine about phy resume failure but proceed */
3603 if (rc && rc != -EOPNOTSUPP)
a9a79dfe
JP
3604 ata_link_warn(link,
3605 "failed to resume link for reset (errno=%d)\n",
3606 rc);
f5914a46
TH
3607 }
3608
45db2f6c 3609 /* no point in trying softreset on offline link */
b1c72916 3610 if (ata_phys_link_offline(link))
45db2f6c
TH
3611 ehc->i.action &= ~ATA_EH_SOFTRESET;
3612
f5914a46
TH
3613 return 0;
3614}
a52fbcfc 3615EXPORT_SYMBOL_GPL(ata_std_prereset);
f5914a46 3616
57c9efdf
TH
3617/**
3618 * sata_std_hardreset - COMRESET w/o waiting or classification
3619 * @link: link to reset
3620 * @class: resulting class of attached device
3621 * @deadline: deadline jiffies for the operation
3622 *
3623 * Standard SATA COMRESET w/o waiting or classification.
3624 *
3625 * LOCKING:
3626 * Kernel thread context (may sleep)
3627 *
3628 * RETURNS:
3629 * 0 if link offline, -EAGAIN if link online, -errno on errors.
3630 */
3631int sata_std_hardreset(struct ata_link *link, unsigned int *class,
3632 unsigned long deadline)
3633{
3634 const unsigned long *timing = sata_ehc_deb_timing(&link->eh_context);
3635 bool online;
3636 int rc;
3637
3638 /* do hardreset */
3639 rc = sata_link_hardreset(link, timing, deadline, &online, NULL);
57c9efdf
TH
3640 return online ? -EAGAIN : rc;
3641}
a52fbcfc 3642EXPORT_SYMBOL_GPL(sata_std_hardreset);
57c9efdf 3643
c2bd5804 3644/**
203c75b8 3645 * ata_std_postreset - standard postreset callback
cc0680a5 3646 * @link: the target ata_link
c2bd5804
TH
3647 * @classes: classes of attached devices
3648 *
3649 * This function is invoked after a successful reset. Note that
3650 * the device might have been reset more than once using
3651 * different reset methods before postreset is invoked.
c2bd5804 3652 *
c2bd5804
TH
3653 * LOCKING:
3654 * Kernel thread context (may sleep)
3655 */
203c75b8 3656void ata_std_postreset(struct ata_link *link, unsigned int *classes)
c2bd5804 3657{
f046519f
TH
3658 u32 serror;
3659
f046519f
TH
3660 /* reset complete, clear SError */
3661 if (!sata_scr_read(link, SCR_ERROR, &serror))
3662 sata_scr_write(link, SCR_ERROR, serror);
3663
c2bd5804 3664 /* print link status */
936fd732 3665 sata_print_link_status(link);
c2bd5804 3666}
a52fbcfc 3667EXPORT_SYMBOL_GPL(ata_std_postreset);
c2bd5804 3668
623a3128
TH
3669/**
3670 * ata_dev_same_device - Determine whether new ID matches configured device
623a3128
TH
3671 * @dev: device to compare against
3672 * @new_class: class of the new device
3673 * @new_id: IDENTIFY page of the new device
3674 *
3675 * Compare @new_class and @new_id against @dev and determine
3676 * whether @dev is the device indicated by @new_class and
3677 * @new_id.
3678 *
3679 * LOCKING:
3680 * None.
3681 *
3682 * RETURNS:
3683 * 1 if @dev matches @new_class and @new_id, 0 otherwise.
3684 */
3373efd8
TH
3685static int ata_dev_same_device(struct ata_device *dev, unsigned int new_class,
3686 const u16 *new_id)
623a3128
TH
3687{
3688 const u16 *old_id = dev->id;
a0cf733b
TH
3689 unsigned char model[2][ATA_ID_PROD_LEN + 1];
3690 unsigned char serial[2][ATA_ID_SERNO_LEN + 1];
623a3128
TH
3691
3692 if (dev->class != new_class) {
a9a79dfe
JP
3693 ata_dev_info(dev, "class mismatch %d != %d\n",
3694 dev->class, new_class);
623a3128
TH
3695 return 0;
3696 }
3697
a0cf733b
TH
3698 ata_id_c_string(old_id, model[0], ATA_ID_PROD, sizeof(model[0]));
3699 ata_id_c_string(new_id, model[1], ATA_ID_PROD, sizeof(model[1]));
3700 ata_id_c_string(old_id, serial[0], ATA_ID_SERNO, sizeof(serial[0]));
3701 ata_id_c_string(new_id, serial[1], ATA_ID_SERNO, sizeof(serial[1]));
623a3128
TH
3702
3703 if (strcmp(model[0], model[1])) {
a9a79dfe
JP
3704 ata_dev_info(dev, "model number mismatch '%s' != '%s'\n",
3705 model[0], model[1]);
623a3128
TH
3706 return 0;
3707 }
3708
3709 if (strcmp(serial[0], serial[1])) {
a9a79dfe
JP
3710 ata_dev_info(dev, "serial number mismatch '%s' != '%s'\n",
3711 serial[0], serial[1]);
623a3128
TH
3712 return 0;
3713 }
3714
623a3128
TH
3715 return 1;
3716}
3717
3718/**
fe30911b 3719 * ata_dev_reread_id - Re-read IDENTIFY data
3fae450c 3720 * @dev: target ATA device
bff04647 3721 * @readid_flags: read ID flags
623a3128
TH
3722 *
3723 * Re-read IDENTIFY page and make sure @dev is still attached to
3724 * the port.
3725 *
3726 * LOCKING:
3727 * Kernel thread context (may sleep)
3728 *
3729 * RETURNS:
3730 * 0 on success, negative errno otherwise
3731 */
fe30911b 3732int ata_dev_reread_id(struct ata_device *dev, unsigned int readid_flags)
623a3128 3733{
5eb45c02 3734 unsigned int class = dev->class;
9af5c9c9 3735 u16 *id = (void *)dev->link->ap->sector_buf;
623a3128
TH
3736 int rc;
3737
fe635c7e 3738 /* read ID data */
bff04647 3739 rc = ata_dev_read_id(dev, &class, readid_flags, id);
623a3128 3740 if (rc)
fe30911b 3741 return rc;
623a3128
TH
3742
3743 /* is the device still there? */
fe30911b
TH
3744 if (!ata_dev_same_device(dev, class, id))
3745 return -ENODEV;
623a3128 3746
fe635c7e 3747 memcpy(dev->id, id, sizeof(id[0]) * ATA_ID_WORDS);
fe30911b
TH
3748 return 0;
3749}
3750
3751/**
3752 * ata_dev_revalidate - Revalidate ATA device
3753 * @dev: device to revalidate
422c9daa 3754 * @new_class: new class code
fe30911b
TH
3755 * @readid_flags: read ID flags
3756 *
3757 * Re-read IDENTIFY page, make sure @dev is still attached to the
3758 * port and reconfigure it according to the new IDENTIFY page.
3759 *
3760 * LOCKING:
3761 * Kernel thread context (may sleep)
3762 *
3763 * RETURNS:
3764 * 0 on success, negative errno otherwise
3765 */
422c9daa
TH
3766int ata_dev_revalidate(struct ata_device *dev, unsigned int new_class,
3767 unsigned int readid_flags)
fe30911b 3768{
6ddcd3b0 3769 u64 n_sectors = dev->n_sectors;
5920dadf 3770 u64 n_native_sectors = dev->n_native_sectors;
fe30911b
TH
3771 int rc;
3772
3773 if (!ata_dev_enabled(dev))
3774 return -ENODEV;
3775
422c9daa
TH
3776 /* fail early if !ATA && !ATAPI to avoid issuing [P]IDENTIFY to PMP */
3777 if (ata_class_enabled(new_class) &&
f0d0613d
BP
3778 new_class != ATA_DEV_ATA &&
3779 new_class != ATA_DEV_ATAPI &&
9162c657 3780 new_class != ATA_DEV_ZAC &&
f0d0613d 3781 new_class != ATA_DEV_SEMB) {
a9a79dfe
JP
3782 ata_dev_info(dev, "class mismatch %u != %u\n",
3783 dev->class, new_class);
422c9daa
TH
3784 rc = -ENODEV;
3785 goto fail;
3786 }
3787
fe30911b
TH
3788 /* re-read ID */
3789 rc = ata_dev_reread_id(dev, readid_flags);
3790 if (rc)
3791 goto fail;
623a3128
TH
3792
3793 /* configure device according to the new ID */
efdaedc4 3794 rc = ata_dev_configure(dev);
6ddcd3b0
TH
3795 if (rc)
3796 goto fail;
3797
3798 /* verify n_sectors hasn't changed */
445d211b
TH
3799 if (dev->class != ATA_DEV_ATA || !n_sectors ||
3800 dev->n_sectors == n_sectors)
3801 return 0;
3802
3803 /* n_sectors has changed */
a9a79dfe
JP
3804 ata_dev_warn(dev, "n_sectors mismatch %llu != %llu\n",
3805 (unsigned long long)n_sectors,
3806 (unsigned long long)dev->n_sectors);
445d211b
TH
3807
3808 /*
3809 * Something could have caused HPA to be unlocked
3810 * involuntarily. If n_native_sectors hasn't changed and the
3811 * new size matches it, keep the device.
3812 */
3813 if (dev->n_native_sectors == n_native_sectors &&
3814 dev->n_sectors > n_sectors && dev->n_sectors == n_native_sectors) {
a9a79dfe
JP
3815 ata_dev_warn(dev,
3816 "new n_sectors matches native, probably "
3817 "late HPA unlock, n_sectors updated\n");
68939ce5 3818 /* use the larger n_sectors */
445d211b 3819 return 0;
6ddcd3b0
TH
3820 }
3821
445d211b
TH
3822 /*
3823 * Some BIOSes boot w/o HPA but resume w/ HPA locked. Try
3824 * unlocking HPA in those cases.
3825 *
3826 * https://bugzilla.kernel.org/show_bug.cgi?id=15396
3827 */
3828 if (dev->n_native_sectors == n_native_sectors &&
3829 dev->n_sectors < n_sectors && n_sectors == n_native_sectors &&
3830 !(dev->horkage & ATA_HORKAGE_BROKEN_HPA)) {
a9a79dfe
JP
3831 ata_dev_warn(dev,
3832 "old n_sectors matches native, probably "
3833 "late HPA lock, will try to unlock HPA\n");
445d211b
TH
3834 /* try unlocking HPA */
3835 dev->flags |= ATA_DFLAG_UNLOCK_HPA;
3836 rc = -EIO;
3837 } else
3838 rc = -ENODEV;
623a3128 3839
445d211b
TH
3840 /* restore original n_[native_]sectors and fail */
3841 dev->n_native_sectors = n_native_sectors;
3842 dev->n_sectors = n_sectors;
623a3128 3843 fail:
a9a79dfe 3844 ata_dev_err(dev, "revalidation failed (errno=%d)\n", rc);
623a3128
TH
3845 return rc;
3846}
3847
6919a0a6
AC
3848struct ata_blacklist_entry {
3849 const char *model_num;
3850 const char *model_rev;
3851 unsigned long horkage;
3852};
3853
3854static const struct ata_blacklist_entry ata_device_blacklist [] = {
3855 /* Devices with DMA related problems under Linux */
3856 { "WDC AC11000H", NULL, ATA_HORKAGE_NODMA },
3857 { "WDC AC22100H", NULL, ATA_HORKAGE_NODMA },
3858 { "WDC AC32500H", NULL, ATA_HORKAGE_NODMA },
3859 { "WDC AC33100H", NULL, ATA_HORKAGE_NODMA },
3860 { "WDC AC31600H", NULL, ATA_HORKAGE_NODMA },
3861 { "WDC AC32100H", "24.09P07", ATA_HORKAGE_NODMA },
3862 { "WDC AC23200L", "21.10N21", ATA_HORKAGE_NODMA },
3863 { "Compaq CRD-8241B", NULL, ATA_HORKAGE_NODMA },
3864 { "CRD-8400B", NULL, ATA_HORKAGE_NODMA },
7da4c935 3865 { "CRD-848[02]B", NULL, ATA_HORKAGE_NODMA },
6919a0a6
AC
3866 { "CRD-84", NULL, ATA_HORKAGE_NODMA },
3867 { "SanDisk SDP3B", NULL, ATA_HORKAGE_NODMA },
3868 { "SanDisk SDP3B-64", NULL, ATA_HORKAGE_NODMA },
3869 { "SANYO CD-ROM CRD", NULL, ATA_HORKAGE_NODMA },
3870 { "HITACHI CDR-8", NULL, ATA_HORKAGE_NODMA },
7da4c935 3871 { "HITACHI CDR-8[34]35",NULL, ATA_HORKAGE_NODMA },
6919a0a6
AC
3872 { "Toshiba CD-ROM XM-6202B", NULL, ATA_HORKAGE_NODMA },
3873 { "TOSHIBA CD-ROM XM-1702BC", NULL, ATA_HORKAGE_NODMA },
3874 { "CD-532E-A", NULL, ATA_HORKAGE_NODMA },
3875 { "E-IDE CD-ROM CR-840",NULL, ATA_HORKAGE_NODMA },
3876 { "CD-ROM Drive/F5A", NULL, ATA_HORKAGE_NODMA },
3877 { "WPI CDD-820", NULL, ATA_HORKAGE_NODMA },
3878 { "SAMSUNG CD-ROM SC-148C", NULL, ATA_HORKAGE_NODMA },
3879 { "SAMSUNG CD-ROM SC", NULL, ATA_HORKAGE_NODMA },
6919a0a6
AC
3880 { "ATAPI CD-ROM DRIVE 40X MAXIMUM",NULL,ATA_HORKAGE_NODMA },
3881 { "_NEC DV5800A", NULL, ATA_HORKAGE_NODMA },
2dcb407e 3882 { "SAMSUNG CD-ROM SN-124", "N001", ATA_HORKAGE_NODMA },
39f19886 3883 { "Seagate STT20000A", NULL, ATA_HORKAGE_NODMA },
d17d794c 3884 { " 2GB ATA Flash Disk", "ADMA428M", ATA_HORKAGE_NODMA },
b00622fc 3885 { "VRFDFC22048UCHC-TE*", NULL, ATA_HORKAGE_NODMA },
3af9a77a 3886 /* Odd clown on sil3726/4726 PMPs */
50af2fa1 3887 { "Config Disk", NULL, ATA_HORKAGE_DISABLE },
a66307d4
HR
3888 /* Similar story with ASMedia 1092 */
3889 { "ASMT109x- Config", NULL, ATA_HORKAGE_DISABLE },
6919a0a6 3890
18d6e9d5 3891 /* Weird ATAPI devices */
40a1d531 3892 { "TORiSAN DVD-ROM DRD-N216", NULL, ATA_HORKAGE_MAX_SEC_128 },
6a87e42e 3893 { "QUANTUM DAT DAT72-000", NULL, ATA_HORKAGE_ATAPI_MOD16_DMA },
a32450e1 3894 { "Slimtype DVD A DS8A8SH", NULL, ATA_HORKAGE_MAX_SEC_LBA48 },
0523f037 3895 { "Slimtype DVD A DS8A9SH", NULL, ATA_HORKAGE_MAX_SEC_LBA48 },
18d6e9d5 3896
af34d637
DM
3897 /*
3898 * Causes silent data corruption with higher max sects.
3899 * http://lkml.kernel.org/g/x49wpy40ysk.fsf@segfault.boston.devel.redhat.com
3900 */
3901 { "ST380013AS", "3.20", ATA_HORKAGE_MAX_SEC_1024 },
1488a1e3
TH
3902
3903 /*
e0edc8c5 3904 * These devices time out with higher max sects.
1488a1e3
TH
3905 * https://bugzilla.kernel.org/show_bug.cgi?id=121671
3906 */
e0edc8c5 3907 { "LITEON CX1-JB*-HP", NULL, ATA_HORKAGE_MAX_SEC_1024 },
db5ff909 3908 { "LITEON EP1-*", NULL, ATA_HORKAGE_MAX_SEC_1024 },
af34d637 3909
6919a0a6
AC
3910 /* Devices we expect to fail diagnostics */
3911
3912 /* Devices where NCQ should be avoided */
3913 /* NCQ is slow */
2dcb407e 3914 { "WDC WD740ADFD-00", NULL, ATA_HORKAGE_NONCQ },
ef1429c0 3915 { "WDC WD740ADFD-00NLR1", NULL, ATA_HORKAGE_NONCQ },
09125ea6
TH
3916 /* http://thread.gmane.org/gmane.linux.ide/14907 */
3917 { "FUJITSU MHT2060BH", NULL, ATA_HORKAGE_NONCQ },
7acfaf30 3918 /* NCQ is broken */
539cc7c7 3919 { "Maxtor *", "BANC*", ATA_HORKAGE_NONCQ },
0e3dbc01 3920 { "Maxtor 7V300F0", "VA111630", ATA_HORKAGE_NONCQ },
da6f0ec2 3921 { "ST380817AS", "3.42", ATA_HORKAGE_NONCQ },
e41bd3e8 3922 { "ST3160023AS", "3.42", ATA_HORKAGE_NONCQ },
5ccfca97 3923 { "OCZ CORE_SSD", "02.10104", ATA_HORKAGE_NONCQ },
539cc7c7 3924
ac70a964 3925 /* Seagate NCQ + FLUSH CACHE firmware bug */
4d1f9082 3926 { "ST31500341AS", "SD1[5-9]", ATA_HORKAGE_NONCQ |
ac70a964 3927 ATA_HORKAGE_FIRMWARE_WARN },
d10d491f 3928
4d1f9082 3929 { "ST31000333AS", "SD1[5-9]", ATA_HORKAGE_NONCQ |
d10d491f
TH
3930 ATA_HORKAGE_FIRMWARE_WARN },
3931
4d1f9082 3932 { "ST3640[36]23AS", "SD1[5-9]", ATA_HORKAGE_NONCQ |
d10d491f
TH
3933 ATA_HORKAGE_FIRMWARE_WARN },
3934
4d1f9082 3935 { "ST3320[68]13AS", "SD1[5-9]", ATA_HORKAGE_NONCQ |
ac70a964
TH
3936 ATA_HORKAGE_FIRMWARE_WARN },
3937
31f6264e
HG
3938 /* drives which fail FPDMA_AA activation (some may freeze afterwards)
3939 the ST disks also have LPM issues */
8756a25b 3940 { "ST1000LM024 HN-M101MBB", NULL, ATA_HORKAGE_BROKEN_FPDMA_AA |
ef1429c0 3941 ATA_HORKAGE_NOLPM },
08c85d2a 3942 { "VB0250EAVER", "HPG7", ATA_HORKAGE_BROKEN_FPDMA_AA },
87809942 3943
36e337d0
RH
3944 /* Blacklist entries taken from Silicon Image 3124/3132
3945 Windows driver .inf file - also several Linux problem reports */
ef1429c0
DLM
3946 { "HTS541060G9SA00", "MB3OC60D", ATA_HORKAGE_NONCQ },
3947 { "HTS541080G9SA00", "MB4OC60D", ATA_HORKAGE_NONCQ },
3948 { "HTS541010G9SA00", "MBZOC60D", ATA_HORKAGE_NONCQ },
6919a0a6 3949
68b0ddb2 3950 /* https://bugzilla.kernel.org/show_bug.cgi?id=15573 */
ef1429c0 3951 { "C300-CTFDDAC128MAG", "0001", ATA_HORKAGE_NONCQ },
68b0ddb2 3952
3b545563 3953 /* Sandisk SD7/8/9s lock up hard on large trims */
ef1429c0 3954 { "SanDisk SD[789]*", NULL, ATA_HORKAGE_MAX_TRIM_128M },
322579dc 3955
16c55b03 3956 /* devices which puke on READ_NATIVE_MAX */
ef1429c0 3957 { "HDS724040KLSA80", "KFAOA20N", ATA_HORKAGE_BROKEN_HPA },
16c55b03
TH
3958 { "WDC WD3200JD-00KLB0", "WD-WCAMR1130137", ATA_HORKAGE_BROKEN_HPA },
3959 { "WDC WD2500JD-00HBB0", "WD-WMAL71490727", ATA_HORKAGE_BROKEN_HPA },
3960 { "MAXTOR 6L080L4", "A93.0500", ATA_HORKAGE_BROKEN_HPA },
6919a0a6 3961
7831387b
TH
3962 /* this one allows HPA unlocking but fails IOs on the area */
3963 { "OCZ-VERTEX", "1.30", ATA_HORKAGE_BROKEN_HPA },
3964
93328e11 3965 /* Devices which report 1 sector over size HPA */
ef1429c0
DLM
3966 { "ST340823A", NULL, ATA_HORKAGE_HPA_SIZE },
3967 { "ST320413A", NULL, ATA_HORKAGE_HPA_SIZE },
3968 { "ST310211A", NULL, ATA_HORKAGE_HPA_SIZE },
93328e11 3969
6bbfd53d 3970 /* Devices which get the IVB wrong */
ef1429c0 3971 { "QUANTUM FIREBALLlct10 05", "A03.0900", ATA_HORKAGE_IVB },
a79067e5 3972 /* Maybe we should just blacklist TSSTcorp... */
ef1429c0 3973 { "TSSTcorp CDDVDW SH-S202[HJN]", "SB0[01]", ATA_HORKAGE_IVB },
6bbfd53d 3974
9ce8e307 3975 /* Devices that do not need bridging limits applied */
ef1429c0
DLM
3976 { "MTRON MSP-SATA*", NULL, ATA_HORKAGE_BRIDGE_OK },
3977 { "BUFFALO HD-QSU2/R5", NULL, ATA_HORKAGE_BRIDGE_OK },
9ce8e307 3978
9062712f 3979 /* Devices which aren't very happy with higher link speeds */
ef1429c0
DLM
3980 { "WD My Book", NULL, ATA_HORKAGE_1_5_GBPS },
3981 { "Seagate FreeAgent GoFlex", NULL, ATA_HORKAGE_1_5_GBPS },
9062712f 3982
d0cb43b3
TH
3983 /*
3984 * Devices which choke on SETXFER. Applies only if both the
3985 * device and controller are SATA.
3986 */
cd691876 3987 { "PIONEER DVD-RW DVRTD08", NULL, ATA_HORKAGE_NOSETXFER },
3a25179e
VL
3988 { "PIONEER DVD-RW DVRTD08A", NULL, ATA_HORKAGE_NOSETXFER },
3989 { "PIONEER DVD-RW DVR-215", NULL, ATA_HORKAGE_NOSETXFER },
cd691876
TH
3990 { "PIONEER DVD-RW DVR-212D", NULL, ATA_HORKAGE_NOSETXFER },
3991 { "PIONEER DVD-RW DVR-216D", NULL, ATA_HORKAGE_NOSETXFER },
d0cb43b3 3992
ea08aec7
NC
3993 /* These specific Pioneer models have LPM issues */
3994 { "PIONEER BD-RW BDR-207M", NULL, ATA_HORKAGE_NOLPM },
3995 { "PIONEER BD-RW BDR-205", NULL, ATA_HORKAGE_NOLPM },
3996
b17e5729 3997 /* Crucial BX100 SSD 500GB has broken LPM support */
3bf7b5d6 3998 { "CT500BX100SSD1", NULL, ATA_HORKAGE_NOLPM },
b17e5729 3999
d418ff56
HG
4000 /* 512GB MX100 with MU01 firmware has both queued TRIM and LPM issues */
4001 { "Crucial_CT512MX100*", "MU01", ATA_HORKAGE_NO_NCQ_TRIM |
9c7be59f 4002 ATA_HORKAGE_ZERO_AFTER_TRIM |
ef1429c0 4003 ATA_HORKAGE_NOLPM },
d418ff56
HG
4004 /* 512GB MX100 with newer firmware has only LPM issues */
4005 { "Crucial_CT512MX100*", NULL, ATA_HORKAGE_ZERO_AFTER_TRIM |
ef1429c0 4006 ATA_HORKAGE_NOLPM },
9c7be59f 4007
62ac3f73
HG
4008 /* 480GB+ M500 SSDs have both queued TRIM and LPM issues */
4009 { "Crucial_CT480M500*", NULL, ATA_HORKAGE_NO_NCQ_TRIM |
4010 ATA_HORKAGE_ZERO_AFTER_TRIM |
ef1429c0 4011 ATA_HORKAGE_NOLPM },
62ac3f73
HG
4012 { "Crucial_CT960M500*", NULL, ATA_HORKAGE_NO_NCQ_TRIM |
4013 ATA_HORKAGE_ZERO_AFTER_TRIM |
ef1429c0 4014 ATA_HORKAGE_NOLPM },
62ac3f73 4015
76936e9a 4016 /* These specific Samsung models/firmware-revs do not handle LPM well */
ef1429c0
DLM
4017 { "SAMSUNG MZMPC128HBFU-000MV", "CXM14M1Q", ATA_HORKAGE_NOLPM },
4018 { "SAMSUNG SSD PM830 mSATA *", "CXM13D1Q", ATA_HORKAGE_NOLPM },
4019 { "SAMSUNG MZ7TD256HAFV-000L9", NULL, ATA_HORKAGE_NOLPM },
4020 { "SAMSUNG MZ7TE512HMHP-000L1", "EXT06L0Q", ATA_HORKAGE_NOLPM },
b5b4d3a5 4021
f78dea06 4022 /* devices that don't properly handle queued TRIM commands */
136d769e 4023 { "Micron_M500IT_*", "MU01", ATA_HORKAGE_NO_NCQ_TRIM |
ef1429c0 4024 ATA_HORKAGE_ZERO_AFTER_TRIM },
243918be 4025 { "Micron_M500_*", NULL, ATA_HORKAGE_NO_NCQ_TRIM |
ef1429c0 4026 ATA_HORKAGE_ZERO_AFTER_TRIM },
ff7f53fb 4027 { "Crucial_CT*M500*", NULL, ATA_HORKAGE_NO_NCQ_TRIM |
ef1429c0 4028 ATA_HORKAGE_ZERO_AFTER_TRIM },
9051bd39 4029 { "Micron_M5[15]0_*", "MU01", ATA_HORKAGE_NO_NCQ_TRIM |
ef1429c0 4030 ATA_HORKAGE_ZERO_AFTER_TRIM },
ff7f53fb 4031 { "Crucial_CT*M550*", "MU01", ATA_HORKAGE_NO_NCQ_TRIM |
ef1429c0 4032 ATA_HORKAGE_ZERO_AFTER_TRIM },
ff7f53fb 4033 { "Crucial_CT*MX100*", "MU01", ATA_HORKAGE_NO_NCQ_TRIM |
ef1429c0 4034 ATA_HORKAGE_ZERO_AFTER_TRIM },
53997522
CL
4035 { "Samsung SSD 840 EVO*", NULL, ATA_HORKAGE_NO_NCQ_TRIM |
4036 ATA_HORKAGE_NO_DMA_LOG |
ef1429c0 4037 ATA_HORKAGE_ZERO_AFTER_TRIM },
ca6bfcb2 4038 { "Samsung SSD 840*", NULL, ATA_HORKAGE_NO_NCQ_TRIM |
ef1429c0 4039 ATA_HORKAGE_ZERO_AFTER_TRIM },
ca6bfcb2 4040 { "Samsung SSD 850*", NULL, ATA_HORKAGE_NO_NCQ_TRIM |
ef1429c0 4041 ATA_HORKAGE_ZERO_AFTER_TRIM },
8a6430ab 4042 { "Samsung SSD 860*", NULL, ATA_HORKAGE_NO_NCQ_TRIM |
7a8526a5 4043 ATA_HORKAGE_ZERO_AFTER_TRIM |
ef1429c0 4044 ATA_HORKAGE_NO_NCQ_ON_ATI },
8a6430ab 4045 { "Samsung SSD 870*", NULL, ATA_HORKAGE_NO_NCQ_TRIM |
7a8526a5 4046 ATA_HORKAGE_ZERO_AFTER_TRIM |
ef1429c0 4047 ATA_HORKAGE_NO_NCQ_ON_ATI },
7a7184b0 4048 { "FCCT*M500*", NULL, ATA_HORKAGE_NO_NCQ_TRIM |
ef1429c0 4049 ATA_HORKAGE_ZERO_AFTER_TRIM },
e61f7d1c 4050
cda57b1b 4051 /* devices that don't properly handle TRIM commands */
ef1429c0
DLM
4052 { "SuperSSpeed S238*", NULL, ATA_HORKAGE_NOTRIM },
4053 { "M88V29*", NULL, ATA_HORKAGE_NOTRIM },
cda57b1b 4054
e61f7d1c
MP
4055 /*
4056 * As defined, the DRAT (Deterministic Read After Trim) and RZAT
4057 * (Return Zero After Trim) flags in the ATA Command Set are
4058 * unreliable in the sense that they only define what happens if
4059 * the device successfully executed the DSM TRIM command. TRIM
4060 * is only advisory, however, and the device is free to silently
4061 * ignore all or parts of the request.
4062 *
4063 * Whitelist drives that are known to reliably return zeroes
4064 * after TRIM.
4065 */
4066
4067 /*
4068 * The intel 510 drive has buggy DRAT/RZAT. Explicitly exclude
4069 * that model before whitelisting all other intel SSDs.
4070 */
ef1429c0
DLM
4071 { "INTEL*SSDSC2MH*", NULL, 0 },
4072
4073 { "Micron*", NULL, ATA_HORKAGE_ZERO_AFTER_TRIM },
4074 { "Crucial*", NULL, ATA_HORKAGE_ZERO_AFTER_TRIM },
4075 { "INTEL*SSD*", NULL, ATA_HORKAGE_ZERO_AFTER_TRIM },
4076 { "SSD*INTEL*", NULL, ATA_HORKAGE_ZERO_AFTER_TRIM },
4077 { "Samsung*SSD*", NULL, ATA_HORKAGE_ZERO_AFTER_TRIM },
4078 { "SAMSUNG*SSD*", NULL, ATA_HORKAGE_ZERO_AFTER_TRIM },
4079 { "SAMSUNG*MZ7KM*", NULL, ATA_HORKAGE_ZERO_AFTER_TRIM },
4080 { "ST[1248][0248]0[FH]*", NULL, ATA_HORKAGE_ZERO_AFTER_TRIM },
f78dea06 4081
ecd75ad5
TH
4082 /*
4083 * Some WD SATA-I drives spin up and down erratically when the link
4084 * is put into the slumber mode. We don't have full list of the
4085 * affected devices. Disable LPM if the device matches one of the
4086 * known prefixes and is SATA-1. As a side effect LPM partial is
4087 * lost too.
4088 *
4089 * https://bugzilla.kernel.org/show_bug.cgi?id=57211
4090 */
4091 { "WDC WD800JD-*", NULL, ATA_HORKAGE_WD_BROKEN_LPM },
4092 { "WDC WD1200JD-*", NULL, ATA_HORKAGE_WD_BROKEN_LPM },
4093 { "WDC WD1600JD-*", NULL, ATA_HORKAGE_WD_BROKEN_LPM },
4094 { "WDC WD2000JD-*", NULL, ATA_HORKAGE_WD_BROKEN_LPM },
4095 { "WDC WD2500JD-*", NULL, ATA_HORKAGE_WD_BROKEN_LPM },
4096 { "WDC WD3000JD-*", NULL, ATA_HORKAGE_WD_BROKEN_LPM },
4097 { "WDC WD3200JD-*", NULL, ATA_HORKAGE_WD_BROKEN_LPM },
4098
ac9f0c81
AL
4099 /*
4100 * This sata dom device goes on a walkabout when the ATA_LOG_DIRECTORY
4101 * log page is accessed. Ensure we never ask for this log page with
4102 * these devices.
4103 */
4104 { "SATADOM-ML 3ME", NULL, ATA_HORKAGE_NO_LOG_DIR },
4105
6919a0a6
AC
4106 /* End Marker */
4107 { }
1da177e4 4108};
2e9edbf8 4109
75683fe7 4110static unsigned long ata_dev_blacklisted(const struct ata_device *dev)
1da177e4 4111{
8bfa79fc
TH
4112 unsigned char model_num[ATA_ID_PROD_LEN + 1];
4113 unsigned char model_rev[ATA_ID_FW_REV_LEN + 1];
6919a0a6 4114 const struct ata_blacklist_entry *ad = ata_device_blacklist;
3a778275 4115
8bfa79fc
TH
4116 ata_id_c_string(dev->id, model_num, ATA_ID_PROD, sizeof(model_num));
4117 ata_id_c_string(dev->id, model_rev, ATA_ID_FW_REV, sizeof(model_rev));
1da177e4 4118
6919a0a6 4119 while (ad->model_num) {
1c402799 4120 if (glob_match(ad->model_num, model_num)) {
6919a0a6
AC
4121 if (ad->model_rev == NULL)
4122 return ad->horkage;
1c402799 4123 if (glob_match(ad->model_rev, model_rev))
6919a0a6 4124 return ad->horkage;
f4b15fef 4125 }
6919a0a6 4126 ad++;
f4b15fef 4127 }
1da177e4
LT
4128 return 0;
4129}
4130
6919a0a6
AC
4131static int ata_dma_blacklisted(const struct ata_device *dev)
4132{
4133 /* We don't support polling DMA.
4134 * DMA blacklist those ATAPI devices with CDB-intr (and use PIO)
4135 * if the LLDD handles only interrupts in the HSM_ST_LAST state.
4136 */
9af5c9c9 4137 if ((dev->link->ap->flags & ATA_FLAG_PIO_POLLING) &&
6919a0a6
AC
4138 (dev->flags & ATA_DFLAG_CDB_INTR))
4139 return 1;
75683fe7 4140 return (dev->horkage & ATA_HORKAGE_NODMA) ? 1 : 0;
6919a0a6
AC
4141}
4142
6bbfd53d
AC
4143/**
4144 * ata_is_40wire - check drive side detection
4145 * @dev: device
4146 *
4147 * Perform drive side detection decoding, allowing for device vendors
4148 * who can't follow the documentation.
4149 */
4150
4151static int ata_is_40wire(struct ata_device *dev)
4152{
4153 if (dev->horkage & ATA_HORKAGE_IVB)
4154 return ata_drive_40wire_relaxed(dev->id);
4155 return ata_drive_40wire(dev->id);
4156}
4157
15a5551c
AC
4158/**
4159 * cable_is_40wire - 40/80/SATA decider
4160 * @ap: port to consider
4161 *
4162 * This function encapsulates the policy for speed management
4163 * in one place. At the moment we don't cache the result but
4164 * there is a good case for setting ap->cbl to the result when
4165 * we are called with unknown cables (and figuring out if it
4166 * impacts hotplug at all).
4167 *
4168 * Return 1 if the cable appears to be 40 wire.
4169 */
4170
4171static int cable_is_40wire(struct ata_port *ap)
4172{
4173 struct ata_link *link;
4174 struct ata_device *dev;
4175
4a9c7b33 4176 /* If the controller thinks we are 40 wire, we are. */
15a5551c
AC
4177 if (ap->cbl == ATA_CBL_PATA40)
4178 return 1;
4a9c7b33
TH
4179
4180 /* If the controller thinks we are 80 wire, we are. */
15a5551c
AC
4181 if (ap->cbl == ATA_CBL_PATA80 || ap->cbl == ATA_CBL_SATA)
4182 return 0;
4a9c7b33
TH
4183
4184 /* If the system is known to be 40 wire short cable (eg
4185 * laptop), then we allow 80 wire modes even if the drive
4186 * isn't sure.
4187 */
f792068e
AC
4188 if (ap->cbl == ATA_CBL_PATA40_SHORT)
4189 return 0;
4a9c7b33
TH
4190
4191 /* If the controller doesn't know, we scan.
4192 *
4193 * Note: We look for all 40 wire detects at this point. Any
4194 * 80 wire detect is taken to be 80 wire cable because
4195 * - in many setups only the one drive (slave if present) will
4196 * give a valid detect
4197 * - if you have a non detect capable drive you don't want it
4198 * to colour the choice
4199 */
1eca4365
TH
4200 ata_for_each_link(link, ap, EDGE) {
4201 ata_for_each_dev(dev, link, ENABLED) {
4202 if (!ata_is_40wire(dev))
15a5551c
AC
4203 return 0;
4204 }
4205 }
4206 return 1;
4207}
4208
a6d5a51c
TH
4209/**
4210 * ata_dev_xfermask - Compute supported xfermask of the given device
a6d5a51c
TH
4211 * @dev: Device to compute xfermask for
4212 *
acf356b1
TH
4213 * Compute supported xfermask of @dev and store it in
4214 * dev->*_mask. This function is responsible for applying all
4215 * known limits including host controller limits, device
4216 * blacklist, etc...
a6d5a51c
TH
4217 *
4218 * LOCKING:
4219 * None.
a6d5a51c 4220 */
3373efd8 4221static void ata_dev_xfermask(struct ata_device *dev)
1da177e4 4222{
9af5c9c9
TH
4223 struct ata_link *link = dev->link;
4224 struct ata_port *ap = link->ap;
cca3974e 4225 struct ata_host *host = ap->host;
f0a6d77b 4226 unsigned int xfer_mask;
1da177e4 4227
37deecb5 4228 /* controller modes available */
565083e1
TH
4229 xfer_mask = ata_pack_xfermask(ap->pio_mask,
4230 ap->mwdma_mask, ap->udma_mask);
4231
8343f889 4232 /* drive modes available */
37deecb5
TH
4233 xfer_mask &= ata_pack_xfermask(dev->pio_mask,
4234 dev->mwdma_mask, dev->udma_mask);
4235 xfer_mask &= ata_id_xfermask(dev->id);
565083e1 4236
b352e57d
AC
4237 /*
4238 * CFA Advanced TrueIDE timings are not allowed on a shared
4239 * cable
4240 */
4241 if (ata_dev_pair(dev)) {
4242 /* No PIO5 or PIO6 */
4243 xfer_mask &= ~(0x03 << (ATA_SHIFT_PIO + 5));
4244 /* No MWDMA3 or MWDMA 4 */
4245 xfer_mask &= ~(0x03 << (ATA_SHIFT_MWDMA + 3));
4246 }
4247
37deecb5
TH
4248 if (ata_dma_blacklisted(dev)) {
4249 xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
a9a79dfe
JP
4250 ata_dev_warn(dev,
4251 "device is on DMA blacklist, disabling DMA\n");
37deecb5 4252 }
a6d5a51c 4253
14d66ab7 4254 if ((host->flags & ATA_HOST_SIMPLEX) &&
2dcb407e 4255 host->simplex_claimed && host->simplex_claimed != ap) {
37deecb5 4256 xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
a9a79dfe
JP
4257 ata_dev_warn(dev,
4258 "simplex DMA is claimed by other device, disabling DMA\n");
5444a6f4 4259 }
565083e1 4260
e424675f
JG
4261 if (ap->flags & ATA_FLAG_NO_IORDY)
4262 xfer_mask &= ata_pio_mask_no_iordy(dev);
4263
5444a6f4 4264 if (ap->ops->mode_filter)
a76b62ca 4265 xfer_mask = ap->ops->mode_filter(dev, xfer_mask);
5444a6f4 4266
8343f889
RH
4267 /* Apply cable rule here. Don't apply it early because when
4268 * we handle hot plug the cable type can itself change.
4269 * Check this last so that we know if the transfer rate was
4270 * solely limited by the cable.
4271 * Unknown or 80 wire cables reported host side are checked
4272 * drive side as well. Cases where we know a 40wire cable
4273 * is used safely for 80 are not checked here.
4274 */
4275 if (xfer_mask & (0xF8 << ATA_SHIFT_UDMA))
4276 /* UDMA/44 or higher would be available */
15a5551c 4277 if (cable_is_40wire(ap)) {
a9a79dfe
JP
4278 ata_dev_warn(dev,
4279 "limited to UDMA/33 due to 40-wire cable\n");
8343f889
RH
4280 xfer_mask &= ~(0xF8 << ATA_SHIFT_UDMA);
4281 }
4282
565083e1
TH
4283 ata_unpack_xfermask(xfer_mask, &dev->pio_mask,
4284 &dev->mwdma_mask, &dev->udma_mask);
1da177e4
LT
4285}
4286
1da177e4
LT
4287/**
4288 * ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
1da177e4
LT
4289 * @dev: Device to which command will be sent
4290 *
780a87f7
JG
4291 * Issue SET FEATURES - XFER MODE command to device @dev
4292 * on port @ap.
4293 *
1da177e4 4294 * LOCKING:
0cba632b 4295 * PCI/etc. bus probe sem.
83206a29
TH
4296 *
4297 * RETURNS:
4298 * 0 on success, AC_ERR_* mask otherwise.
1da177e4
LT
4299 */
4300
3373efd8 4301static unsigned int ata_dev_set_xfermode(struct ata_device *dev)
1da177e4 4302{
a0123703 4303 struct ata_taskfile tf;
1da177e4
LT
4304
4305 /* set up set-features taskfile */
4633778b 4306 ata_dev_dbg(dev, "set features - xfer mode\n");
1da177e4 4307
464cf177
TH
4308 /* Some controllers and ATAPI devices show flaky interrupt
4309 * behavior after setting xfer mode. Use polling instead.
4310 */
3373efd8 4311 ata_tf_init(dev, &tf);
a0123703
TH
4312 tf.command = ATA_CMD_SET_FEATURES;
4313 tf.feature = SETFEATURES_XFER;
464cf177 4314 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE | ATA_TFLAG_POLLING;
a0123703 4315 tf.protocol = ATA_PROT_NODATA;
b9f8ab2d 4316 /* If we are using IORDY we must send the mode setting command */
11b7becc
JG
4317 if (ata_pio_need_iordy(dev))
4318 tf.nsect = dev->xfer_mode;
b9f8ab2d
AC
4319 /* If the device has IORDY and the controller does not - turn it off */
4320 else if (ata_id_has_iordy(dev->id))
11b7becc 4321 tf.nsect = 0x01;
b9f8ab2d
AC
4322 else /* In the ancient relic department - skip all of this */
4323 return 0;
1da177e4 4324
024811a2
DLM
4325 /*
4326 * On some disks, this command causes spin-up, so we need longer
4327 * timeout.
4328 */
4329 return ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 15000);
9f45cbd3 4330}
1152b261 4331
9f45cbd3 4332/**
99ad3f9f 4333 * ata_dev_set_feature - Issue SET FEATURES
9f45cbd3 4334 * @dev: Device to which command will be sent
99ad3f9f
NC
4335 * @subcmd: The SET FEATURES subcommand to be sent
4336 * @action: The sector count represents a subcommand specific action
9f45cbd3 4337 *
99ad3f9f 4338 * Issue SET FEATURES command to device @dev on port @ap with sector count
9f45cbd3
KCA
4339 *
4340 * LOCKING:
4341 * PCI/etc. bus probe sem.
4342 *
4343 * RETURNS:
4344 * 0 on success, AC_ERR_* mask otherwise.
4345 */
99ad3f9f 4346unsigned int ata_dev_set_feature(struct ata_device *dev, u8 subcmd, u8 action)
9f45cbd3
KCA
4347{
4348 struct ata_taskfile tf;
61176eed 4349 unsigned int timeout = 0;
9f45cbd3
KCA
4350
4351 /* set up set-features taskfile */
99ad3f9f 4352 ata_dev_dbg(dev, "set features\n");
9f45cbd3
KCA
4353
4354 ata_tf_init(dev, &tf);
4355 tf.command = ATA_CMD_SET_FEATURES;
99ad3f9f 4356 tf.feature = subcmd;
9f45cbd3
KCA
4357 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
4358 tf.protocol = ATA_PROT_NODATA;
99ad3f9f 4359 tf.nsect = action;
9f45cbd3 4360
99ad3f9f 4361 if (subcmd == SETFEATURES_SPINUP)
974e0a45
DLM
4362 timeout = ata_probe_timeout ?
4363 ata_probe_timeout * 1000 : SETFEATURES_SPINUP_TIMEOUT;
1da177e4 4364
614065ab 4365 return ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, timeout);
1da177e4 4366}
633de4cc 4367EXPORT_SYMBOL_GPL(ata_dev_set_feature);
1da177e4 4368
8bf62ece
AL
4369/**
4370 * ata_dev_init_params - Issue INIT DEV PARAMS command
8bf62ece 4371 * @dev: Device to which command will be sent
e2a7f77a
RD
4372 * @heads: Number of heads (taskfile parameter)
4373 * @sectors: Number of sectors (taskfile parameter)
8bf62ece
AL
4374 *
4375 * LOCKING:
6aff8f1f
TH
4376 * Kernel thread context (may sleep)
4377 *
4378 * RETURNS:
4379 * 0 on success, AC_ERR_* mask otherwise.
8bf62ece 4380 */
3373efd8
TH
4381static unsigned int ata_dev_init_params(struct ata_device *dev,
4382 u16 heads, u16 sectors)
8bf62ece 4383{
a0123703 4384 struct ata_taskfile tf;
6aff8f1f 4385 unsigned int err_mask;
8bf62ece
AL
4386
4387 /* Number of sectors per track 1-255. Number of heads 1-16 */
4388 if (sectors < 1 || sectors > 255 || heads < 1 || heads > 16)
00b6f5e9 4389 return AC_ERR_INVALID;
8bf62ece
AL
4390
4391 /* set up init dev params taskfile */
4633778b 4392 ata_dev_dbg(dev, "init dev params \n");
8bf62ece 4393
3373efd8 4394 ata_tf_init(dev, &tf);
a0123703
TH
4395 tf.command = ATA_CMD_INIT_DEV_PARAMS;
4396 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
4397 tf.protocol = ATA_PROT_NODATA;
4398 tf.nsect = sectors;
4399 tf.device |= (heads - 1) & 0x0f; /* max head = num. of heads - 1 */
8bf62ece 4400
2b789108 4401 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
18b2466c
AC
4402 /* A clean abort indicates an original or just out of spec drive
4403 and we should continue as we issue the setup based on the
4404 drive reported working geometry */
efcef265 4405 if (err_mask == AC_ERR_DEV && (tf.error & ATA_ABORTED))
18b2466c 4406 err_mask = 0;
8bf62ece 4407
6aff8f1f 4408 return err_mask;
8bf62ece
AL
4409}
4410
1da177e4 4411/**
5895ef9a 4412 * atapi_check_dma - Check whether ATAPI DMA can be supported
1da177e4
LT
4413 * @qc: Metadata associated with taskfile to check
4414 *
780a87f7
JG
4415 * Allow low-level driver to filter ATA PACKET commands, returning
4416 * a status indicating whether or not it is OK to use DMA for the
4417 * supplied PACKET command.
4418 *
1da177e4 4419 * LOCKING:
624d5c51
TH
4420 * spin_lock_irqsave(host lock)
4421 *
4422 * RETURNS: 0 when ATAPI DMA can be used
4423 * nonzero otherwise
4424 */
5895ef9a 4425int atapi_check_dma(struct ata_queued_cmd *qc)
624d5c51
TH
4426{
4427 struct ata_port *ap = qc->ap;
71601958 4428
624d5c51
TH
4429 /* Don't allow DMA if it isn't multiple of 16 bytes. Quite a
4430 * few ATAPI devices choke on such DMA requests.
4431 */
6a87e42e
TH
4432 if (!(qc->dev->horkage & ATA_HORKAGE_ATAPI_MOD16_DMA) &&
4433 unlikely(qc->nbytes & 15))
624d5c51 4434 return 1;
e2cec771 4435
624d5c51
TH
4436 if (ap->ops->check_atapi_dma)
4437 return ap->ops->check_atapi_dma(qc);
e2cec771 4438
624d5c51
TH
4439 return 0;
4440}
1da177e4 4441
624d5c51
TH
4442/**
4443 * ata_std_qc_defer - Check whether a qc needs to be deferred
4444 * @qc: ATA command in question
4445 *
4446 * Non-NCQ commands cannot run with any other command, NCQ or
4447 * not. As upper layer only knows the queue depth, we are
4448 * responsible for maintaining exclusion. This function checks
4449 * whether a new command @qc can be issued.
4450 *
4451 * LOCKING:
4452 * spin_lock_irqsave(host lock)
4453 *
4454 * RETURNS:
4455 * ATA_DEFER_* if deferring is needed, 0 otherwise.
4456 */
4457int ata_std_qc_defer(struct ata_queued_cmd *qc)
4458{
4459 struct ata_link *link = qc->dev->link;
e2cec771 4460
179b310a 4461 if (ata_is_ncq(qc->tf.protocol)) {
624d5c51
TH
4462 if (!ata_tag_valid(link->active_tag))
4463 return 0;
4464 } else {
4465 if (!ata_tag_valid(link->active_tag) && !link->sactive)
4466 return 0;
4467 }
e2cec771 4468
624d5c51
TH
4469 return ATA_DEFER_LINK;
4470}
a52fbcfc 4471EXPORT_SYMBOL_GPL(ata_std_qc_defer);
6912ccd5 4472
95364f36
JS
4473enum ata_completion_errors ata_noop_qc_prep(struct ata_queued_cmd *qc)
4474{
4475 return AC_ERR_OK;
4476}
a52fbcfc 4477EXPORT_SYMBOL_GPL(ata_noop_qc_prep);
1da177e4 4478
624d5c51
TH
4479/**
4480 * ata_sg_init - Associate command with scatter-gather table.
4481 * @qc: Command to be associated
4482 * @sg: Scatter-gather table.
4483 * @n_elem: Number of elements in s/g table.
4484 *
4485 * Initialize the data-related elements of queued_cmd @qc
4486 * to point to a scatter-gather table @sg, containing @n_elem
4487 * elements.
4488 *
4489 * LOCKING:
4490 * spin_lock_irqsave(host lock)
4491 */
4492void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
4493 unsigned int n_elem)
4494{
4495 qc->sg = sg;
4496 qc->n_elem = n_elem;
4497 qc->cursg = qc->sg;
4498}
bb5cb290 4499
2874d5ee
GU
4500#ifdef CONFIG_HAS_DMA
4501
4502/**
4503 * ata_sg_clean - Unmap DMA memory associated with command
4504 * @qc: Command containing DMA memory to be released
4505 *
4506 * Unmap all mapped DMA memory associated with this command.
4507 *
4508 * LOCKING:
4509 * spin_lock_irqsave(host lock)
4510 */
af27e01c 4511static void ata_sg_clean(struct ata_queued_cmd *qc)
2874d5ee
GU
4512{
4513 struct ata_port *ap = qc->ap;
4514 struct scatterlist *sg = qc->sg;
4515 int dir = qc->dma_dir;
4516
4517 WARN_ON_ONCE(sg == NULL);
4518
2874d5ee
GU
4519 if (qc->n_elem)
4520 dma_unmap_sg(ap->dev, sg, qc->orig_n_elem, dir);
4521
4522 qc->flags &= ~ATA_QCFLAG_DMAMAP;
4523 qc->sg = NULL;
4524}
4525
624d5c51
TH
4526/**
4527 * ata_sg_setup - DMA-map the scatter-gather table associated with a command.
4528 * @qc: Command with scatter-gather table to be mapped.
4529 *
4530 * DMA-map the scatter-gather table associated with queued_cmd @qc.
4531 *
4532 * LOCKING:
4533 * spin_lock_irqsave(host lock)
4534 *
4535 * RETURNS:
4536 * Zero on success, negative on error.
4537 *
4538 */
4539static int ata_sg_setup(struct ata_queued_cmd *qc)
4540{
4541 struct ata_port *ap = qc->ap;
4542 unsigned int n_elem;
1da177e4 4543
624d5c51
TH
4544 n_elem = dma_map_sg(ap->dev, qc->sg, qc->n_elem, qc->dma_dir);
4545 if (n_elem < 1)
4546 return -1;
bb5cb290 4547
5825627c 4548 qc->orig_n_elem = qc->n_elem;
624d5c51
TH
4549 qc->n_elem = n_elem;
4550 qc->flags |= ATA_QCFLAG_DMAMAP;
1da177e4 4551
624d5c51 4552 return 0;
1da177e4
LT
4553}
4554
2874d5ee
GU
4555#else /* !CONFIG_HAS_DMA */
4556
4557static inline void ata_sg_clean(struct ata_queued_cmd *qc) {}
4558static inline int ata_sg_setup(struct ata_queued_cmd *qc) { return -1; }
4559
4560#endif /* !CONFIG_HAS_DMA */
4561
624d5c51
TH
4562/**
4563 * swap_buf_le16 - swap halves of 16-bit words in place
4564 * @buf: Buffer to swap
4565 * @buf_words: Number of 16-bit words in buffer.
4566 *
4567 * Swap halves of 16-bit words if needed to convert from
4568 * little-endian byte order to native cpu byte order, or
4569 * vice-versa.
4570 *
4571 * LOCKING:
4572 * Inherited from caller.
4573 */
4574void swap_buf_le16(u16 *buf, unsigned int buf_words)
8061f5f0 4575{
624d5c51
TH
4576#ifdef __BIG_ENDIAN
4577 unsigned int i;
8061f5f0 4578
624d5c51
TH
4579 for (i = 0; i < buf_words; i++)
4580 buf[i] = le16_to_cpu(buf[i]);
4581#endif /* __BIG_ENDIAN */
8061f5f0
TH
4582}
4583
8a8bc223
TH
4584/**
4585 * ata_qc_free - free unused ata_queued_cmd
4586 * @qc: Command to complete
4587 *
4588 * Designed to free unused ata_queued_cmd object
4589 * in case something prevents using it.
4590 *
4591 * LOCKING:
4592 * spin_lock_irqsave(host lock)
4593 */
4594void ata_qc_free(struct ata_queued_cmd *qc)
4595{
8a8bc223 4596 qc->flags = 0;
4f1a22ee 4597 if (ata_tag_valid(qc->tag))
8a8bc223 4598 qc->tag = ATA_TAG_POISON;
8a8bc223
TH
4599}
4600
76014427 4601void __ata_qc_complete(struct ata_queued_cmd *qc)
1da177e4 4602{
a1104016
JL
4603 struct ata_port *ap;
4604 struct ata_link *link;
dedaf2b0 4605
efcb3cf7
TH
4606 WARN_ON_ONCE(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
4607 WARN_ON_ONCE(!(qc->flags & ATA_QCFLAG_ACTIVE));
a1104016
JL
4608 ap = qc->ap;
4609 link = qc->dev->link;
1da177e4
LT
4610
4611 if (likely(qc->flags & ATA_QCFLAG_DMAMAP))
4612 ata_sg_clean(qc);
4613
7401abf2 4614 /* command should be marked inactive atomically with qc completion */
179b310a 4615 if (ata_is_ncq(qc->tf.protocol)) {
4e5b6260 4616 link->sactive &= ~(1 << qc->hw_tag);
da917d69
TH
4617 if (!link->sactive)
4618 ap->nr_active_links--;
4619 } else {
9af5c9c9 4620 link->active_tag = ATA_TAG_POISON;
da917d69
TH
4621 ap->nr_active_links--;
4622 }
4623
4624 /* clear exclusive status */
4625 if (unlikely(qc->flags & ATA_QCFLAG_CLEAR_EXCL &&
4626 ap->excl_link == link))
4627 ap->excl_link = NULL;
7401abf2 4628
3f3791d3
AL
4629 /* atapi: mark qc as inactive to prevent the interrupt handler
4630 * from completing the command twice later, before the error handler
4631 * is called. (when rc != 0 and atapi request sense is needed)
4632 */
4633 qc->flags &= ~ATA_QCFLAG_ACTIVE;
e3ed8939 4634 ap->qc_active &= ~(1ULL << qc->tag);
3f3791d3 4635
1da177e4 4636 /* call completion callback */
77853bf2 4637 qc->complete_fn(qc);
1da177e4
LT
4638}
4639
39599a53
TH
4640static void fill_result_tf(struct ata_queued_cmd *qc)
4641{
4642 struct ata_port *ap = qc->ap;
4643
39599a53 4644 qc->result_tf.flags = qc->tf.flags;
22183bf5 4645 ap->ops->qc_fill_rtf(qc);
39599a53
TH
4646}
4647
00115e0f
TH
4648static void ata_verify_xfer(struct ata_queued_cmd *qc)
4649{
4650 struct ata_device *dev = qc->dev;
4651
eb0effdf 4652 if (!ata_is_data(qc->tf.protocol))
00115e0f
TH
4653 return;
4654
4655 if ((dev->mwdma_mask || dev->udma_mask) && ata_is_pio(qc->tf.protocol))
4656 return;
4657
4658 dev->flags &= ~ATA_DFLAG_DUBIOUS_XFER;
4659}
4660
f686bcb8
TH
4661/**
4662 * ata_qc_complete - Complete an active ATA command
4663 * @qc: Command to complete
f686bcb8 4664 *
1aadf5c3
TH
4665 * Indicate to the mid and upper layers that an ATA command has
4666 * completed, with either an ok or not-ok status.
4667 *
4668 * Refrain from calling this function multiple times when
4669 * successfully completing multiple NCQ commands.
4670 * ata_qc_complete_multiple() should be used instead, which will
4671 * properly update IRQ expect state.
f686bcb8
TH
4672 *
4673 * LOCKING:
cca3974e 4674 * spin_lock_irqsave(host lock)
f686bcb8
TH
4675 */
4676void ata_qc_complete(struct ata_queued_cmd *qc)
4677{
4678 struct ata_port *ap = qc->ap;
4679
eb25cb99 4680 /* Trigger the LED (if available) */
d1ed7c55 4681 ledtrig_disk_activity(!!(qc->tf.flags & ATA_TFLAG_WRITE));
eb25cb99 4682
f686bcb8
TH
4683 /* XXX: New EH and old EH use different mechanisms to
4684 * synchronize EH with regular execution path.
4685 *
4686 * In new EH, a failed qc is marked with ATA_QCFLAG_FAILED.
4687 * Normal execution path is responsible for not accessing a
4688 * failed qc. libata core enforces the rule by returning NULL
4689 * from ata_qc_from_tag() for failed qcs.
4690 *
4691 * Old EH depends on ata_qc_complete() nullifying completion
4692 * requests if ATA_QCFLAG_EH_SCHEDULED is set. Old EH does
4693 * not synchronize with interrupt handler. Only PIO task is
4694 * taken care of.
4695 */
4696 if (ap->ops->error_handler) {
4dbfa39b
TH
4697 struct ata_device *dev = qc->dev;
4698 struct ata_eh_info *ehi = &dev->link->eh_info;
4699
f686bcb8
TH
4700 if (unlikely(qc->err_mask))
4701 qc->flags |= ATA_QCFLAG_FAILED;
4702
f08dc1ac
TH
4703 /*
4704 * Finish internal commands without any further processing
4705 * and always with the result TF filled.
4706 */
4707 if (unlikely(ata_tag_internal(qc->tag))) {
f4b31db9 4708 fill_result_tf(qc);
255c03d1 4709 trace_ata_qc_complete_internal(qc);
f08dc1ac
TH
4710 __ata_qc_complete(qc);
4711 return;
4712 }
f4b31db9 4713
f08dc1ac
TH
4714 /*
4715 * Non-internal qc has failed. Fill the result TF and
4716 * summon EH.
4717 */
4718 if (unlikely(qc->flags & ATA_QCFLAG_FAILED)) {
4719 fill_result_tf(qc);
255c03d1 4720 trace_ata_qc_complete_failed(qc);
f08dc1ac 4721 ata_qc_schedule_eh(qc);
f4b31db9 4722 return;
f686bcb8
TH
4723 }
4724
4cb7c6f1 4725 WARN_ON_ONCE(ata_port_is_frozen(ap));
4dc738ed 4726
f686bcb8
TH
4727 /* read result TF if requested */
4728 if (qc->flags & ATA_QCFLAG_RESULT_TF)
39599a53 4729 fill_result_tf(qc);
f686bcb8 4730
255c03d1 4731 trace_ata_qc_complete_done(qc);
4dbfa39b
TH
4732 /* Some commands need post-processing after successful
4733 * completion.
4734 */
4735 switch (qc->tf.command) {
4736 case ATA_CMD_SET_FEATURES:
4737 if (qc->tf.feature != SETFEATURES_WC_ON &&
0c12735e
TY
4738 qc->tf.feature != SETFEATURES_WC_OFF &&
4739 qc->tf.feature != SETFEATURES_RA_ON &&
4740 qc->tf.feature != SETFEATURES_RA_OFF)
4dbfa39b 4741 break;
df561f66 4742 fallthrough;
4dbfa39b
TH
4743 case ATA_CMD_INIT_DEV_PARAMS: /* CHS translation changed */
4744 case ATA_CMD_SET_MULTI: /* multi_count changed */
4745 /* revalidate device */
4746 ehi->dev_action[dev->devno] |= ATA_EH_REVALIDATE;
4747 ata_port_schedule_eh(ap);
4748 break;
054a5fba
TH
4749
4750 case ATA_CMD_SLEEP:
4751 dev->flags |= ATA_DFLAG_SLEEPING;
4752 break;
4dbfa39b
TH
4753 }
4754
00115e0f
TH
4755 if (unlikely(dev->flags & ATA_DFLAG_DUBIOUS_XFER))
4756 ata_verify_xfer(qc);
4757
f686bcb8
TH
4758 __ata_qc_complete(qc);
4759 } else {
4760 if (qc->flags & ATA_QCFLAG_EH_SCHEDULED)
4761 return;
4762
4763 /* read result TF if failed or requested */
4764 if (qc->err_mask || qc->flags & ATA_QCFLAG_RESULT_TF)
39599a53 4765 fill_result_tf(qc);
f686bcb8
TH
4766
4767 __ata_qc_complete(qc);
4768 }
4769}
a52fbcfc 4770EXPORT_SYMBOL_GPL(ata_qc_complete);
f686bcb8 4771
8385d756
SH
4772/**
4773 * ata_qc_get_active - get bitmask of active qcs
4774 * @ap: port in question
4775 *
4776 * LOCKING:
4777 * spin_lock_irqsave(host lock)
4778 *
4779 * RETURNS:
4780 * Bitmask of active qcs
4781 */
4782u64 ata_qc_get_active(struct ata_port *ap)
4783{
4784 u64 qc_active = ap->qc_active;
4785
4786 /* ATA_TAG_INTERNAL is sent to hw as tag 0 */
4787 if (qc_active & (1ULL << ATA_TAG_INTERNAL)) {
4788 qc_active |= (1 << 0);
4789 qc_active &= ~(1ULL << ATA_TAG_INTERNAL);
4790 }
4791
4792 return qc_active;
4793}
4794EXPORT_SYMBOL_GPL(ata_qc_get_active);
4795
1da177e4
LT
4796/**
4797 * ata_qc_issue - issue taskfile to device
4798 * @qc: command to issue to device
4799 *
4800 * Prepare an ATA command to submission to device.
4801 * This includes mapping the data into a DMA-able
4802 * area, filling in the S/G table, and finally
4803 * writing the taskfile to hardware, starting the command.
4804 *
4805 * LOCKING:
cca3974e 4806 * spin_lock_irqsave(host lock)
1da177e4 4807 */
8e0e694a 4808void ata_qc_issue(struct ata_queued_cmd *qc)
1da177e4
LT
4809{
4810 struct ata_port *ap = qc->ap;
9af5c9c9 4811 struct ata_link *link = qc->dev->link;
405e66b3 4812 u8 prot = qc->tf.protocol;
1da177e4 4813
dedaf2b0
TH
4814 /* Make sure only one non-NCQ command is outstanding. The
4815 * check is skipped for old EH because it reuses active qc to
4816 * request ATAPI sense.
4817 */
efcb3cf7 4818 WARN_ON_ONCE(ap->ops->error_handler && ata_tag_valid(link->active_tag));
dedaf2b0 4819
1973a023 4820 if (ata_is_ncq(prot)) {
4e5b6260 4821 WARN_ON_ONCE(link->sactive & (1 << qc->hw_tag));
da917d69
TH
4822
4823 if (!link->sactive)
4824 ap->nr_active_links++;
4e5b6260 4825 link->sactive |= 1 << qc->hw_tag;
dedaf2b0 4826 } else {
efcb3cf7 4827 WARN_ON_ONCE(link->sactive);
da917d69
TH
4828
4829 ap->nr_active_links++;
9af5c9c9 4830 link->active_tag = qc->tag;
dedaf2b0
TH
4831 }
4832
e4a70e76 4833 qc->flags |= ATA_QCFLAG_ACTIVE;
e3ed8939 4834 ap->qc_active |= 1ULL << qc->tag;
e4a70e76 4835
60f5d6ef
TH
4836 /*
4837 * We guarantee to LLDs that they will have at least one
f92a2636
TH
4838 * non-zero sg if the command is a data command.
4839 */
9173e5e8 4840 if (ata_is_data(prot) && (!qc->sg || !qc->n_elem || !qc->nbytes))
60f5d6ef 4841 goto sys_err;
f92a2636 4842
405e66b3 4843 if (ata_is_dma(prot) || (ata_is_pio(prot) &&
f92a2636 4844 (ap->flags & ATA_FLAG_PIO_DMA)))
001102d7 4845 if (ata_sg_setup(qc))
60f5d6ef 4846 goto sys_err;
1da177e4 4847
cf480626 4848 /* if device is sleeping, schedule reset and abort the link */
054a5fba 4849 if (unlikely(qc->dev->flags & ATA_DFLAG_SLEEPING)) {
cf480626 4850 link->eh_info.action |= ATA_EH_RESET;
054a5fba
TH
4851 ata_ehi_push_desc(&link->eh_info, "waking up from sleep");
4852 ata_link_abort(link);
4853 return;
4854 }
4855
fc914faa 4856 trace_ata_qc_prep(qc);
95364f36
JS
4857 qc->err_mask |= ap->ops->qc_prep(qc);
4858 if (unlikely(qc->err_mask))
4859 goto err;
255c03d1 4860 trace_ata_qc_issue(qc);
8e0e694a
TH
4861 qc->err_mask |= ap->ops->qc_issue(qc);
4862 if (unlikely(qc->err_mask))
4863 goto err;
4864 return;
1da177e4 4865
60f5d6ef 4866sys_err:
8e0e694a
TH
4867 qc->err_mask |= AC_ERR_SYSTEM;
4868err:
4869 ata_qc_complete(qc);
1da177e4
LT
4870}
4871
34bf2170 4872/**
b1c72916 4873 * ata_phys_link_online - test whether the given link is online
936fd732 4874 * @link: ATA link to test
34bf2170 4875 *
936fd732
TH
4876 * Test whether @link is online. Note that this function returns
4877 * 0 if online status of @link cannot be obtained, so
4878 * ata_link_online(link) != !ata_link_offline(link).
34bf2170
TH
4879 *
4880 * LOCKING:
4881 * None.
4882 *
4883 * RETURNS:
b5b3fa38 4884 * True if the port online status is available and online.
34bf2170 4885 */
b1c72916 4886bool ata_phys_link_online(struct ata_link *link)
34bf2170
TH
4887{
4888 u32 sstatus;
4889
936fd732 4890 if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0 &&
9913ff8a 4891 ata_sstatus_online(sstatus))
b5b3fa38
TH
4892 return true;
4893 return false;
34bf2170
TH
4894}
4895
4896/**
b1c72916 4897 * ata_phys_link_offline - test whether the given link is offline
936fd732 4898 * @link: ATA link to test
34bf2170 4899 *
936fd732
TH
4900 * Test whether @link is offline. Note that this function
4901 * returns 0 if offline status of @link cannot be obtained, so
4902 * ata_link_online(link) != !ata_link_offline(link).
34bf2170
TH
4903 *
4904 * LOCKING:
4905 * None.
4906 *
4907 * RETURNS:
b5b3fa38 4908 * True if the port offline status is available and offline.
34bf2170 4909 */
b1c72916 4910bool ata_phys_link_offline(struct ata_link *link)
34bf2170
TH
4911{
4912 u32 sstatus;
4913
936fd732 4914 if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0 &&
9913ff8a 4915 !ata_sstatus_online(sstatus))
b5b3fa38
TH
4916 return true;
4917 return false;
34bf2170 4918}
0baab86b 4919
b1c72916
TH
4920/**
4921 * ata_link_online - test whether the given link is online
4922 * @link: ATA link to test
4923 *
4924 * Test whether @link is online. This is identical to
4925 * ata_phys_link_online() when there's no slave link. When
4926 * there's a slave link, this function should only be called on
4927 * the master link and will return true if any of M/S links is
4928 * online.
4929 *
4930 * LOCKING:
4931 * None.
4932 *
4933 * RETURNS:
4934 * True if the port online status is available and online.
4935 */
4936bool ata_link_online(struct ata_link *link)
4937{
4938 struct ata_link *slave = link->ap->slave_link;
4939
4940 WARN_ON(link == slave); /* shouldn't be called on slave link */
4941
4942 return ata_phys_link_online(link) ||
4943 (slave && ata_phys_link_online(slave));
4944}
a52fbcfc 4945EXPORT_SYMBOL_GPL(ata_link_online);
b1c72916
TH
4946
4947/**
4948 * ata_link_offline - test whether the given link is offline
4949 * @link: ATA link to test
4950 *
4951 * Test whether @link is offline. This is identical to
4952 * ata_phys_link_offline() when there's no slave link. When
4953 * there's a slave link, this function should only be called on
4954 * the master link and will return true if both M/S links are
4955 * offline.
4956 *
4957 * LOCKING:
4958 * None.
4959 *
4960 * RETURNS:
4961 * True if the port offline status is available and offline.
4962 */
4963bool ata_link_offline(struct ata_link *link)
4964{
4965 struct ata_link *slave = link->ap->slave_link;
4966
4967 WARN_ON(link == slave); /* shouldn't be called on slave link */
4968
4969 return ata_phys_link_offline(link) &&
4970 (!slave || ata_phys_link_offline(slave));
4971}
a52fbcfc 4972EXPORT_SYMBOL_GPL(ata_link_offline);
b1c72916 4973
6ffa01d8 4974#ifdef CONFIG_PM
bc6e7c4b
DW
4975static void ata_port_request_pm(struct ata_port *ap, pm_message_t mesg,
4976 unsigned int action, unsigned int ehi_flags,
4977 bool async)
500530f6 4978{
5ef41082 4979 struct ata_link *link;
500530f6 4980 unsigned long flags;
500530f6 4981
5ef41082
LM
4982 /* Previous resume operation might still be in
4983 * progress. Wait for PM_PENDING to clear.
4984 */
4985 if (ap->pflags & ATA_PFLAG_PM_PENDING) {
4986 ata_port_wait_eh(ap);
4987 WARN_ON(ap->pflags & ATA_PFLAG_PM_PENDING);
4988 }
500530f6 4989
5ef41082
LM
4990 /* request PM ops to EH */
4991 spin_lock_irqsave(ap->lock, flags);
500530f6 4992
5ef41082 4993 ap->pm_mesg = mesg;
5ef41082
LM
4994 ap->pflags |= ATA_PFLAG_PM_PENDING;
4995 ata_for_each_link(link, ap, HOST_FIRST) {
4996 link->eh_info.action |= action;
4997 link->eh_info.flags |= ehi_flags;
4998 }
500530f6 4999
5ef41082 5000 ata_port_schedule_eh(ap);
500530f6 5001
5ef41082 5002 spin_unlock_irqrestore(ap->lock, flags);
500530f6 5003
2fcbdcb4 5004 if (!async) {
5ef41082
LM
5005 ata_port_wait_eh(ap);
5006 WARN_ON(ap->pflags & ATA_PFLAG_PM_PENDING);
500530f6 5007 }
500530f6
TH
5008}
5009
bc6e7c4b
DW
5010/*
5011 * On some hardware, device fails to respond after spun down for suspend. As
5012 * the device won't be used before being resumed, we don't need to touch the
5013 * device. Ask EH to skip the usual stuff and proceed directly to suspend.
5014 *
5015 * http://thread.gmane.org/gmane.linux.ide/46764
5016 */
5017static const unsigned int ata_port_suspend_ehi = ATA_EHI_QUIET
5018 | ATA_EHI_NO_AUTOPSY
5019 | ATA_EHI_NO_RECOVERY;
5020
5021static void ata_port_suspend(struct ata_port *ap, pm_message_t mesg)
5ef41082 5022{
bc6e7c4b 5023 ata_port_request_pm(ap, mesg, 0, ata_port_suspend_ehi, false);
5ef41082
LM
5024}
5025
bc6e7c4b 5026static void ata_port_suspend_async(struct ata_port *ap, pm_message_t mesg)
2fcbdcb4 5027{
bc6e7c4b 5028 ata_port_request_pm(ap, mesg, 0, ata_port_suspend_ehi, true);
2fcbdcb4
DW
5029}
5030
bc6e7c4b 5031static int ata_port_pm_suspend(struct device *dev)
5ef41082 5032{
bc6e7c4b
DW
5033 struct ata_port *ap = to_ata_port(dev);
5034
5ef41082
LM
5035 if (pm_runtime_suspended(dev))
5036 return 0;
5037
bc6e7c4b
DW
5038 ata_port_suspend(ap, PMSG_SUSPEND);
5039 return 0;
33574d68
LM
5040}
5041
bc6e7c4b 5042static int ata_port_pm_freeze(struct device *dev)
33574d68 5043{
bc6e7c4b
DW
5044 struct ata_port *ap = to_ata_port(dev);
5045
33574d68 5046 if (pm_runtime_suspended(dev))
f5e6d0d0 5047 return 0;
33574d68 5048
bc6e7c4b
DW
5049 ata_port_suspend(ap, PMSG_FREEZE);
5050 return 0;
33574d68
LM
5051}
5052
bc6e7c4b 5053static int ata_port_pm_poweroff(struct device *dev)
33574d68 5054{
bc6e7c4b
DW
5055 ata_port_suspend(to_ata_port(dev), PMSG_HIBERNATE);
5056 return 0;
5ef41082
LM
5057}
5058
bc6e7c4b
DW
5059static const unsigned int ata_port_resume_ehi = ATA_EHI_NO_AUTOPSY
5060 | ATA_EHI_QUIET;
5ef41082 5061
bc6e7c4b
DW
5062static void ata_port_resume(struct ata_port *ap, pm_message_t mesg)
5063{
5064 ata_port_request_pm(ap, mesg, ATA_EH_RESET, ata_port_resume_ehi, false);
5ef41082
LM
5065}
5066
bc6e7c4b 5067static void ata_port_resume_async(struct ata_port *ap, pm_message_t mesg)
2fcbdcb4 5068{
bc6e7c4b 5069 ata_port_request_pm(ap, mesg, ATA_EH_RESET, ata_port_resume_ehi, true);
2fcbdcb4
DW
5070}
5071
bc6e7c4b 5072static int ata_port_pm_resume(struct device *dev)
e90b1e5a 5073{
200421a8 5074 ata_port_resume_async(to_ata_port(dev), PMSG_RESUME);
bc6e7c4b
DW
5075 pm_runtime_disable(dev);
5076 pm_runtime_set_active(dev);
5077 pm_runtime_enable(dev);
5078 return 0;
e90b1e5a
LM
5079}
5080
7e15e9be
AL
5081/*
5082 * For ODDs, the upper layer will poll for media change every few seconds,
5083 * which will make it enter and leave suspend state every few seconds. And
5084 * as each suspend will cause a hard/soft reset, the gain of runtime suspend
5085 * is very little and the ODD may malfunction after constantly being reset.
5086 * So the idle callback here will not proceed to suspend if a non-ZPODD capable
5087 * ODD is attached to the port.
5088 */
9ee4f393
LM
5089static int ata_port_runtime_idle(struct device *dev)
5090{
7e15e9be
AL
5091 struct ata_port *ap = to_ata_port(dev);
5092 struct ata_link *link;
5093 struct ata_device *adev;
5094
5095 ata_for_each_link(link, ap, HOST_FIRST) {
5096 ata_for_each_dev(adev, link, ENABLED)
5097 if (adev->class == ATA_DEV_ATAPI &&
5098 !zpodd_dev_enabled(adev))
5099 return -EBUSY;
5100 }
5101
45f0a85c 5102 return 0;
9ee4f393
LM
5103}
5104
a7ff60db
AL
5105static int ata_port_runtime_suspend(struct device *dev)
5106{
bc6e7c4b
DW
5107 ata_port_suspend(to_ata_port(dev), PMSG_AUTO_SUSPEND);
5108 return 0;
a7ff60db
AL
5109}
5110
5111static int ata_port_runtime_resume(struct device *dev)
5112{
bc6e7c4b
DW
5113 ata_port_resume(to_ata_port(dev), PMSG_AUTO_RESUME);
5114 return 0;
a7ff60db
AL
5115}
5116
5ef41082 5117static const struct dev_pm_ops ata_port_pm_ops = {
bc6e7c4b
DW
5118 .suspend = ata_port_pm_suspend,
5119 .resume = ata_port_pm_resume,
5120 .freeze = ata_port_pm_freeze,
5121 .thaw = ata_port_pm_resume,
5122 .poweroff = ata_port_pm_poweroff,
5123 .restore = ata_port_pm_resume,
9ee4f393 5124
a7ff60db
AL
5125 .runtime_suspend = ata_port_runtime_suspend,
5126 .runtime_resume = ata_port_runtime_resume,
9ee4f393 5127 .runtime_idle = ata_port_runtime_idle,
5ef41082
LM
5128};
5129
2fcbdcb4
DW
5130/* sas ports don't participate in pm runtime management of ata_ports,
5131 * and need to resume ata devices at the domain level, not the per-port
5132 * level. sas suspend/resume is async to allow parallel port recovery
5133 * since sas has multiple ata_port instances per Scsi_Host.
5134 */
bc6e7c4b 5135void ata_sas_port_suspend(struct ata_port *ap)
2fcbdcb4 5136{
bc6e7c4b 5137 ata_port_suspend_async(ap, PMSG_SUSPEND);
2fcbdcb4 5138}
bc6e7c4b 5139EXPORT_SYMBOL_GPL(ata_sas_port_suspend);
2fcbdcb4 5140
bc6e7c4b 5141void ata_sas_port_resume(struct ata_port *ap)
2fcbdcb4 5142{
bc6e7c4b 5143 ata_port_resume_async(ap, PMSG_RESUME);
2fcbdcb4 5144}
bc6e7c4b 5145EXPORT_SYMBOL_GPL(ata_sas_port_resume);
2fcbdcb4 5146
500530f6 5147/**
cca3974e
JG
5148 * ata_host_suspend - suspend host
5149 * @host: host to suspend
500530f6
TH
5150 * @mesg: PM message
5151 *
5ef41082 5152 * Suspend @host. Actual operation is performed by port suspend.
500530f6 5153 */
ec87cf37 5154void ata_host_suspend(struct ata_host *host, pm_message_t mesg)
500530f6 5155{
5ef41082 5156 host->dev->power.power_state = mesg;
500530f6 5157}
a52fbcfc 5158EXPORT_SYMBOL_GPL(ata_host_suspend);
500530f6
TH
5159
5160/**
cca3974e
JG
5161 * ata_host_resume - resume host
5162 * @host: host to resume
500530f6 5163 *
5ef41082 5164 * Resume @host. Actual operation is performed by port resume.
500530f6 5165 */
cca3974e 5166void ata_host_resume(struct ata_host *host)
500530f6 5167{
72ad6ec4 5168 host->dev->power.power_state = PMSG_ON;
500530f6 5169}
a52fbcfc 5170EXPORT_SYMBOL_GPL(ata_host_resume);
6ffa01d8 5171#endif
500530f6 5172
8df82c13 5173const struct device_type ata_port_type = {
5ef41082
LM
5174 .name = "ata_port",
5175#ifdef CONFIG_PM
5176 .pm = &ata_port_pm_ops,
5177#endif
5178};
5179
3ef3b43d
TH
5180/**
5181 * ata_dev_init - Initialize an ata_device structure
5182 * @dev: Device structure to initialize
5183 *
5184 * Initialize @dev in preparation for probing.
5185 *
5186 * LOCKING:
5187 * Inherited from caller.
5188 */
5189void ata_dev_init(struct ata_device *dev)
5190{
b1c72916 5191 struct ata_link *link = ata_dev_phys_link(dev);
9af5c9c9 5192 struct ata_port *ap = link->ap;
72fa4b74
TH
5193 unsigned long flags;
5194
b1c72916 5195 /* SATA spd limit is bound to the attached device, reset together */
9af5c9c9
TH
5196 link->sata_spd_limit = link->hw_sata_spd_limit;
5197 link->sata_spd = 0;
5a04bf4b 5198
72fa4b74
TH
5199 /* High bits of dev->flags are used to record warm plug
5200 * requests which occur asynchronously. Synchronize using
cca3974e 5201 * host lock.
72fa4b74 5202 */
ba6a1308 5203 spin_lock_irqsave(ap->lock, flags);
72fa4b74 5204 dev->flags &= ~ATA_DFLAG_INIT_MASK;
3dcc323f 5205 dev->horkage = 0;
ba6a1308 5206 spin_unlock_irqrestore(ap->lock, flags);
3ef3b43d 5207
99cf610a
TH
5208 memset((void *)dev + ATA_DEVICE_CLEAR_BEGIN, 0,
5209 ATA_DEVICE_CLEAR_END - ATA_DEVICE_CLEAR_BEGIN);
3ef3b43d
TH
5210 dev->pio_mask = UINT_MAX;
5211 dev->mwdma_mask = UINT_MAX;
5212 dev->udma_mask = UINT_MAX;
5213}
5214
4fb37a25
TH
5215/**
5216 * ata_link_init - Initialize an ata_link structure
5217 * @ap: ATA port link is attached to
5218 * @link: Link structure to initialize
8989805d 5219 * @pmp: Port multiplier port number
4fb37a25
TH
5220 *
5221 * Initialize @link.
5222 *
5223 * LOCKING:
5224 * Kernel thread context (may sleep)
5225 */
fb7fd614 5226void ata_link_init(struct ata_port *ap, struct ata_link *link, int pmp)
4fb37a25
TH
5227{
5228 int i;
5229
5230 /* clear everything except for devices */
d9027470
GG
5231 memset((void *)link + ATA_LINK_CLEAR_BEGIN, 0,
5232 ATA_LINK_CLEAR_END - ATA_LINK_CLEAR_BEGIN);
4fb37a25
TH
5233
5234 link->ap = ap;
8989805d 5235 link->pmp = pmp;
4fb37a25
TH
5236 link->active_tag = ATA_TAG_POISON;
5237 link->hw_sata_spd_limit = UINT_MAX;
5238
5239 /* can't use iterator, ap isn't initialized yet */
5240 for (i = 0; i < ATA_MAX_DEVICES; i++) {
5241 struct ata_device *dev = &link->device[i];
5242
5243 dev->link = link;
5244 dev->devno = dev - link->device;
110f66d2
TH
5245#ifdef CONFIG_ATA_ACPI
5246 dev->gtf_filter = ata_acpi_gtf_filter;
5247#endif
4fb37a25
TH
5248 ata_dev_init(dev);
5249 }
5250}
5251
5252/**
5253 * sata_link_init_spd - Initialize link->sata_spd_limit
5254 * @link: Link to configure sata_spd_limit for
5255 *
a31a6997 5256 * Initialize ``link->[hw_]sata_spd_limit`` to the currently
4fb37a25
TH
5257 * configured value.
5258 *
5259 * LOCKING:
5260 * Kernel thread context (may sleep).
5261 *
5262 * RETURNS:
5263 * 0 on success, -errno on failure.
5264 */
fb7fd614 5265int sata_link_init_spd(struct ata_link *link)
4fb37a25 5266{
33267325 5267 u8 spd;
4fb37a25
TH
5268 int rc;
5269
d127ea7b 5270 rc = sata_scr_read(link, SCR_CONTROL, &link->saved_scontrol);
4fb37a25
TH
5271 if (rc)
5272 return rc;
5273
d127ea7b 5274 spd = (link->saved_scontrol >> 4) & 0xf;
4fb37a25
TH
5275 if (spd)
5276 link->hw_sata_spd_limit &= (1 << spd) - 1;
5277
05944bdf 5278 ata_force_link_limits(link);
33267325 5279
4fb37a25
TH
5280 link->sata_spd_limit = link->hw_sata_spd_limit;
5281
5282 return 0;
5283}
5284
1da177e4 5285/**
f3187195
TH
5286 * ata_port_alloc - allocate and initialize basic ATA port resources
5287 * @host: ATA host this allocated port belongs to
1da177e4 5288 *
f3187195
TH
5289 * Allocate and initialize basic ATA port resources.
5290 *
5291 * RETURNS:
5292 * Allocate ATA port on success, NULL on failure.
0cba632b 5293 *
1da177e4 5294 * LOCKING:
f3187195 5295 * Inherited from calling layer (may sleep).
1da177e4 5296 */
f3187195 5297struct ata_port *ata_port_alloc(struct ata_host *host)
1da177e4 5298{
f3187195 5299 struct ata_port *ap;
1da177e4 5300
f3187195
TH
5301 ap = kzalloc(sizeof(*ap), GFP_KERNEL);
5302 if (!ap)
5303 return NULL;
4fca377f 5304
7b3a24c5 5305 ap->pflags |= ATA_PFLAG_INITIALIZING | ATA_PFLAG_FROZEN;
cca3974e 5306 ap->lock = &host->lock;
f3187195 5307 ap->print_id = -1;
e628dc99 5308 ap->local_port_no = -1;
cca3974e 5309 ap->host = host;
f3187195 5310 ap->dev = host->dev;
bd5d825c 5311
ad72cf98 5312 mutex_init(&ap->scsi_scan_mutex);
65f27f38
DH
5313 INIT_DELAYED_WORK(&ap->hotplug_task, ata_scsi_hotplug);
5314 INIT_WORK(&ap->scsi_rescan_task, ata_scsi_dev_rescan);
a72ec4ce 5315 INIT_LIST_HEAD(&ap->eh_done_q);
c6cf9e99 5316 init_waitqueue_head(&ap->eh_wait_q);
45fabbb7 5317 init_completion(&ap->park_req_pending);
b93ab338
KC
5318 timer_setup(&ap->fastdrain_timer, ata_eh_fastdrain_timerfn,
5319 TIMER_DEFERRABLE);
1da177e4 5320
838df628 5321 ap->cbl = ATA_CBL_NONE;
838df628 5322
8989805d 5323 ata_link_init(ap, &ap->link, 0);
1da177e4
LT
5324
5325#ifdef ATA_IRQ_TRAP
5326 ap->stats.unhandled_irq = 1;
5327 ap->stats.idle_irq = 1;
5328#endif
270390e1
TH
5329 ata_sff_port_init(ap);
5330
1da177e4 5331 return ap;
1da177e4
LT
5332}
5333
2623c7a5 5334static void ata_devres_release(struct device *gendev, void *res)
f0d36efd
TH
5335{
5336 struct ata_host *host = dev_get_drvdata(gendev);
5337 int i;
5338
1aa506e4
TH
5339 for (i = 0; i < host->n_ports; i++) {
5340 struct ata_port *ap = host->ports[i];
5341
4911487a
TH
5342 if (!ap)
5343 continue;
5344
5345 if (ap->scsi_host)
1aa506e4
TH
5346 scsi_host_put(ap->scsi_host);
5347
2623c7a5
TK
5348 }
5349
5350 dev_set_drvdata(gendev, NULL);
5351 ata_host_put(host);
5352}
5353
5354static void ata_host_release(struct kref *kref)
5355{
5356 struct ata_host *host = container_of(kref, struct ata_host, kref);
5357 int i;
5358
5359 for (i = 0; i < host->n_ports; i++) {
5360 struct ata_port *ap = host->ports[i];
5361
633273a3 5362 kfree(ap->pmp_link);
b1c72916 5363 kfree(ap->slave_link);
4911487a 5364 kfree(ap);
1aa506e4
TH
5365 host->ports[i] = NULL;
5366 }
2623c7a5
TK
5367 kfree(host);
5368}
1aa506e4 5369
2623c7a5
TK
5370void ata_host_get(struct ata_host *host)
5371{
5372 kref_get(&host->kref);
5373}
5374
5375void ata_host_put(struct ata_host *host)
5376{
5377 kref_put(&host->kref, ata_host_release);
f0d36efd 5378}
a52fbcfc 5379EXPORT_SYMBOL_GPL(ata_host_put);
f0d36efd 5380
f3187195
TH
5381/**
5382 * ata_host_alloc - allocate and init basic ATA host resources
5383 * @dev: generic device this host is associated with
5384 * @max_ports: maximum number of ATA ports associated with this host
5385 *
5386 * Allocate and initialize basic ATA host resources. LLD calls
5387 * this function to allocate a host, initializes it fully and
5388 * attaches it using ata_host_register().
5389 *
5390 * @max_ports ports are allocated and host->n_ports is
5391 * initialized to @max_ports. The caller is allowed to decrease
5392 * host->n_ports before calling ata_host_register(). The unused
5393 * ports will be automatically freed on registration.
5394 *
5395 * RETURNS:
5396 * Allocate ATA host on success, NULL on failure.
5397 *
5398 * LOCKING:
5399 * Inherited from calling layer (may sleep).
5400 */
5401struct ata_host *ata_host_alloc(struct device *dev, int max_ports)
5402{
5403 struct ata_host *host;
5404 size_t sz;
5405 int i;
2623c7a5 5406 void *dr;
f3187195 5407
f3187195
TH
5408 /* alloc a container for our list of ATA ports (buses) */
5409 sz = sizeof(struct ata_host) + (max_ports + 1) * sizeof(void *);
2623c7a5 5410 host = kzalloc(sz, GFP_KERNEL);
f3187195 5411 if (!host)
2623c7a5
TK
5412 return NULL;
5413
5414 if (!devres_open_group(dev, NULL, GFP_KERNEL))
dafd6c49 5415 goto err_free;
2623c7a5
TK
5416
5417 dr = devres_alloc(ata_devres_release, 0, GFP_KERNEL);
5418 if (!dr)
f3187195
TH
5419 goto err_out;
5420
2623c7a5 5421 devres_add(dev, dr);
f3187195
TH
5422 dev_set_drvdata(dev, host);
5423
5424 spin_lock_init(&host->lock);
c0c362b6 5425 mutex_init(&host->eh_mutex);
f3187195
TH
5426 host->dev = dev;
5427 host->n_ports = max_ports;
2623c7a5 5428 kref_init(&host->kref);
f3187195
TH
5429
5430 /* allocate ports bound to this host */
5431 for (i = 0; i < max_ports; i++) {
5432 struct ata_port *ap;
5433
5434 ap = ata_port_alloc(host);
5435 if (!ap)
5436 goto err_out;
5437
5438 ap->port_no = i;
5439 host->ports[i] = ap;
5440 }
5441
5442 devres_remove_group(dev, NULL);
5443 return host;
5444
5445 err_out:
5446 devres_release_group(dev, NULL);
dafd6c49
CIK
5447 err_free:
5448 kfree(host);
f3187195
TH
5449 return NULL;
5450}
a52fbcfc 5451EXPORT_SYMBOL_GPL(ata_host_alloc);
f3187195 5452
f5cda257
TH
5453/**
5454 * ata_host_alloc_pinfo - alloc host and init with port_info array
5455 * @dev: generic device this host is associated with
5456 * @ppi: array of ATA port_info to initialize host with
5457 * @n_ports: number of ATA ports attached to this host
5458 *
5459 * Allocate ATA host and initialize with info from @ppi. If NULL
5460 * terminated, @ppi may contain fewer entries than @n_ports. The
5461 * last entry will be used for the remaining ports.
5462 *
5463 * RETURNS:
5464 * Allocate ATA host on success, NULL on failure.
5465 *
5466 * LOCKING:
5467 * Inherited from calling layer (may sleep).
5468 */
5469struct ata_host *ata_host_alloc_pinfo(struct device *dev,
5470 const struct ata_port_info * const * ppi,
5471 int n_ports)
5472{
bf476fe2 5473 const struct ata_port_info *pi = &ata_dummy_port_info;
f5cda257
TH
5474 struct ata_host *host;
5475 int i, j;
5476
5477 host = ata_host_alloc(dev, n_ports);
5478 if (!host)
5479 return NULL;
5480
bf476fe2 5481 for (i = 0, j = 0; i < host->n_ports; i++) {
f5cda257
TH
5482 struct ata_port *ap = host->ports[i];
5483
5484 if (ppi[j])
5485 pi = ppi[j++];
5486
5487 ap->pio_mask = pi->pio_mask;
5488 ap->mwdma_mask = pi->mwdma_mask;
5489 ap->udma_mask = pi->udma_mask;
5490 ap->flags |= pi->flags;
0c88758b 5491 ap->link.flags |= pi->link_flags;
f5cda257
TH
5492 ap->ops = pi->port_ops;
5493
5494 if (!host->ops && (pi->port_ops != &ata_dummy_port_ops))
5495 host->ops = pi->port_ops;
f5cda257
TH
5496 }
5497
5498 return host;
5499}
a52fbcfc 5500EXPORT_SYMBOL_GPL(ata_host_alloc_pinfo);
f5cda257 5501
32ebbc0c
TH
5502static void ata_host_stop(struct device *gendev, void *res)
5503{
5504 struct ata_host *host = dev_get_drvdata(gendev);
5505 int i;
5506
5507 WARN_ON(!(host->flags & ATA_HOST_STARTED));
5508
5509 for (i = 0; i < host->n_ports; i++) {
5510 struct ata_port *ap = host->ports[i];
5511
5512 if (ap->ops->port_stop)
5513 ap->ops->port_stop(ap);
5514 }
5515
5516 if (host->ops->host_stop)
5517 host->ops->host_stop(host);
5518}
5519
029cfd6b
TH
5520/**
5521 * ata_finalize_port_ops - finalize ata_port_operations
5522 * @ops: ata_port_operations to finalize
5523 *
5524 * An ata_port_operations can inherit from another ops and that
5525 * ops can again inherit from another. This can go on as many
5526 * times as necessary as long as there is no loop in the
5527 * inheritance chain.
5528 *
5529 * Ops tables are finalized when the host is started. NULL or
5530 * unspecified entries are inherited from the closet ancestor
5531 * which has the method and the entry is populated with it.
5532 * After finalization, the ops table directly points to all the
5533 * methods and ->inherits is no longer necessary and cleared.
5534 *
5535 * Using ATA_OP_NULL, inheriting ops can force a method to NULL.
5536 *
5537 * LOCKING:
5538 * None.
5539 */
5540static void ata_finalize_port_ops(struct ata_port_operations *ops)
5541{
2da67659 5542 static DEFINE_SPINLOCK(lock);
029cfd6b
TH
5543 const struct ata_port_operations *cur;
5544 void **begin = (void **)ops;
5545 void **end = (void **)&ops->inherits;
5546 void **pp;
5547
5548 if (!ops || !ops->inherits)
5549 return;
5550
5551 spin_lock(&lock);
5552
5553 for (cur = ops->inherits; cur; cur = cur->inherits) {
5554 void **inherit = (void **)cur;
5555
5556 for (pp = begin; pp < end; pp++, inherit++)
5557 if (!*pp)
5558 *pp = *inherit;
5559 }
5560
5561 for (pp = begin; pp < end; pp++)
5562 if (IS_ERR(*pp))
5563 *pp = NULL;
5564
5565 ops->inherits = NULL;
5566
5567 spin_unlock(&lock);
5568}
5569
ecef7253
TH
5570/**
5571 * ata_host_start - start and freeze ports of an ATA host
5572 * @host: ATA host to start ports for
5573 *
5574 * Start and then freeze ports of @host. Started status is
5575 * recorded in host->flags, so this function can be called
5576 * multiple times. Ports are guaranteed to get started only
e0af10ac 5577 * once. If host->ops is not initialized yet, it is set to the
f3187195 5578 * first non-dummy port ops.
ecef7253
TH
5579 *
5580 * LOCKING:
5581 * Inherited from calling layer (may sleep).
5582 *
5583 * RETURNS:
5584 * 0 if all ports are started successfully, -errno otherwise.
5585 */
5586int ata_host_start(struct ata_host *host)
5587{
32ebbc0c
TH
5588 int have_stop = 0;
5589 void *start_dr = NULL;
ecef7253
TH
5590 int i, rc;
5591
5592 if (host->flags & ATA_HOST_STARTED)
5593 return 0;
5594
029cfd6b
TH
5595 ata_finalize_port_ops(host->ops);
5596
ecef7253
TH
5597 for (i = 0; i < host->n_ports; i++) {
5598 struct ata_port *ap = host->ports[i];
5599
029cfd6b
TH
5600 ata_finalize_port_ops(ap->ops);
5601
f3187195
TH
5602 if (!host->ops && !ata_port_is_dummy(ap))
5603 host->ops = ap->ops;
5604
32ebbc0c
TH
5605 if (ap->ops->port_stop)
5606 have_stop = 1;
5607 }
5608
355a8031 5609 if (host->ops && host->ops->host_stop)
32ebbc0c
TH
5610 have_stop = 1;
5611
5612 if (have_stop) {
5613 start_dr = devres_alloc(ata_host_stop, 0, GFP_KERNEL);
5614 if (!start_dr)
5615 return -ENOMEM;
5616 }
5617
5618 for (i = 0; i < host->n_ports; i++) {
5619 struct ata_port *ap = host->ports[i];
5620
ecef7253
TH
5621 if (ap->ops->port_start) {
5622 rc = ap->ops->port_start(ap);
5623 if (rc) {
0f9fe9b7 5624 if (rc != -ENODEV)
a44fec1f
JP
5625 dev_err(host->dev,
5626 "failed to start port %d (errno=%d)\n",
5627 i, rc);
ecef7253
TH
5628 goto err_out;
5629 }
5630 }
ecef7253
TH
5631 ata_eh_freeze_port(ap);
5632 }
5633
32ebbc0c
TH
5634 if (start_dr)
5635 devres_add(host->dev, start_dr);
ecef7253
TH
5636 host->flags |= ATA_HOST_STARTED;
5637 return 0;
5638
5639 err_out:
5640 while (--i >= 0) {
5641 struct ata_port *ap = host->ports[i];
5642
5643 if (ap->ops->port_stop)
5644 ap->ops->port_stop(ap);
5645 }
32ebbc0c 5646 devres_free(start_dr);
ecef7253
TH
5647 return rc;
5648}
a52fbcfc 5649EXPORT_SYMBOL_GPL(ata_host_start);
ecef7253 5650
b03732f0 5651/**
94bd5719 5652 * ata_host_init - Initialize a host struct for sas (ipr, libsas)
cca3974e
JG
5653 * @host: host to initialize
5654 * @dev: device host is attached to
cca3974e 5655 * @ops: port_ops
b03732f0 5656 *
b03732f0 5657 */
cca3974e 5658void ata_host_init(struct ata_host *host, struct device *dev,
8d8e7d13 5659 struct ata_port_operations *ops)
b03732f0 5660{
cca3974e 5661 spin_lock_init(&host->lock);
c0c362b6 5662 mutex_init(&host->eh_mutex);
69278f79 5663 host->n_tags = ATA_MAX_QUEUE;
cca3974e 5664 host->dev = dev;
cca3974e 5665 host->ops = ops;
2fa4a326 5666 kref_init(&host->kref);
b03732f0 5667}
a52fbcfc 5668EXPORT_SYMBOL_GPL(ata_host_init);
b03732f0 5669
9508a66f 5670void __ata_port_probe(struct ata_port *ap)
79318057 5671{
9508a66f
DW
5672 struct ata_eh_info *ehi = &ap->link.eh_info;
5673 unsigned long flags;
886ad09f 5674
9508a66f
DW
5675 /* kick EH for boot probing */
5676 spin_lock_irqsave(ap->lock, flags);
79318057 5677
9508a66f
DW
5678 ehi->probe_mask |= ATA_ALL_DEVICES;
5679 ehi->action |= ATA_EH_RESET;
5680 ehi->flags |= ATA_EHI_NO_AUTOPSY | ATA_EHI_QUIET;
79318057 5681
9508a66f
DW
5682 ap->pflags &= ~ATA_PFLAG_INITIALIZING;
5683 ap->pflags |= ATA_PFLAG_LOADING;
5684 ata_port_schedule_eh(ap);
79318057 5685
9508a66f
DW
5686 spin_unlock_irqrestore(ap->lock, flags);
5687}
79318057 5688
9508a66f
DW
5689int ata_port_probe(struct ata_port *ap)
5690{
5691 int rc = 0;
79318057 5692
9508a66f
DW
5693 if (ap->ops->error_handler) {
5694 __ata_port_probe(ap);
79318057
AV
5695 ata_port_wait_eh(ap);
5696 } else {
79318057 5697 rc = ata_bus_probe(ap);
79318057 5698 }
238c9cf9
JB
5699 return rc;
5700}
5701
5702
5703static void async_port_probe(void *data, async_cookie_t cookie)
5704{
5705 struct ata_port *ap = data;
4fca377f 5706
238c9cf9
JB
5707 /*
5708 * If we're not allowed to scan this host in parallel,
5709 * we need to wait until all previous scans have completed
5710 * before going further.
5711 * Jeff Garzik says this is only within a controller, so we
5712 * don't need to wait for port 0, only for later ports.
5713 */
5714 if (!(ap->host->flags & ATA_HOST_PARALLEL_SCAN) && ap->port_no != 0)
5715 async_synchronize_cookie(cookie);
5716
5717 (void)ata_port_probe(ap);
f29d3b23
AV
5718
5719 /* in order to keep device order, we need to synchronize at this point */
5720 async_synchronize_cookie(cookie);
5721
5722 ata_scsi_scan_host(ap, 1);
79318057 5723}
238c9cf9 5724
f3187195
TH
5725/**
5726 * ata_host_register - register initialized ATA host
5727 * @host: ATA host to register
5728 * @sht: template for SCSI host
5729 *
5730 * Register initialized ATA host. @host is allocated using
5731 * ata_host_alloc() and fully initialized by LLD. This function
5732 * starts ports, registers @host with ATA and SCSI layers and
5733 * probe registered devices.
5734 *
5735 * LOCKING:
5736 * Inherited from calling layer (may sleep).
5737 *
5738 * RETURNS:
5739 * 0 on success, -errno otherwise.
5740 */
5741int ata_host_register(struct ata_host *host, struct scsi_host_template *sht)
5742{
5743 int i, rc;
5744
69278f79 5745 host->n_tags = clamp(sht->can_queue, 1, ATA_MAX_QUEUE);
1871ee13 5746
f3187195
TH
5747 /* host must have been started */
5748 if (!(host->flags & ATA_HOST_STARTED)) {
a44fec1f 5749 dev_err(host->dev, "BUG: trying to register unstarted host\n");
f3187195
TH
5750 WARN_ON(1);
5751 return -EINVAL;
5752 }
5753
5754 /* Blow away unused ports. This happens when LLD can't
5755 * determine the exact number of ports to allocate at
5756 * allocation time.
5757 */
5758 for (i = host->n_ports; host->ports[i]; i++)
5759 kfree(host->ports[i]);
5760
5761 /* give ports names and add SCSI hosts */
e628dc99 5762 for (i = 0; i < host->n_ports; i++) {
85d6725b 5763 host->ports[i]->print_id = atomic_inc_return(&ata_print_id);
e628dc99
DM
5764 host->ports[i]->local_port_no = i + 1;
5765 }
4fca377f 5766
d9027470
GG
5767 /* Create associated sysfs transport objects */
5768 for (i = 0; i < host->n_ports; i++) {
5769 rc = ata_tport_add(host->dev,host->ports[i]);
5770 if (rc) {
5771 goto err_tadd;
5772 }
5773 }
5774
f3187195
TH
5775 rc = ata_scsi_add_hosts(host, sht);
5776 if (rc)
d9027470 5777 goto err_tadd;
f3187195
TH
5778
5779 /* set cable, sata_spd_limit and report */
5780 for (i = 0; i < host->n_ports; i++) {
5781 struct ata_port *ap = host->ports[i];
f0a6d77b 5782 unsigned int xfer_mask;
f3187195
TH
5783
5784 /* set SATA cable type if still unset */
5785 if (ap->cbl == ATA_CBL_NONE && (ap->flags & ATA_FLAG_SATA))
5786 ap->cbl = ATA_CBL_SATA;
5787
5788 /* init sata_spd_limit to the current value */
4fb37a25 5789 sata_link_init_spd(&ap->link);
b1c72916
TH
5790 if (ap->slave_link)
5791 sata_link_init_spd(ap->slave_link);
f3187195 5792
cbcdd875 5793 /* print per-port info to dmesg */
f3187195
TH
5794 xfer_mask = ata_pack_xfermask(ap->pio_mask, ap->mwdma_mask,
5795 ap->udma_mask);
5796
abf6e8ed 5797 if (!ata_port_is_dummy(ap)) {
a9a79dfe
JP
5798 ata_port_info(ap, "%cATA max %s %s\n",
5799 (ap->flags & ATA_FLAG_SATA) ? 'S' : 'P',
5800 ata_mode_string(xfer_mask),
5801 ap->link.eh_info.desc);
abf6e8ed
TH
5802 ata_ehi_clear_desc(&ap->link.eh_info);
5803 } else
a9a79dfe 5804 ata_port_info(ap, "DUMMY\n");
f3187195
TH
5805 }
5806
f6005354 5807 /* perform each probe asynchronously */
f3187195
TH
5808 for (i = 0; i < host->n_ports; i++) {
5809 struct ata_port *ap = host->ports[i];
b5292111 5810 ap->cookie = async_schedule(async_port_probe, ap);
f3187195 5811 }
f3187195
TH
5812
5813 return 0;
d9027470
GG
5814
5815 err_tadd:
5816 while (--i >= 0) {
5817 ata_tport_delete(host->ports[i]);
5818 }
5819 return rc;
5820
f3187195 5821}
a52fbcfc 5822EXPORT_SYMBOL_GPL(ata_host_register);
f3187195 5823
f5cda257
TH
5824/**
5825 * ata_host_activate - start host, request IRQ and register it
5826 * @host: target ATA host
5827 * @irq: IRQ to request
5828 * @irq_handler: irq_handler used when requesting IRQ
5829 * @irq_flags: irq_flags used when requesting IRQ
5830 * @sht: scsi_host_template to use when registering the host
5831 *
5832 * After allocating an ATA host and initializing it, most libata
5833 * LLDs perform three steps to activate the host - start host,
c9b5560a 5834 * request IRQ and register it. This helper takes necessary
f5cda257
TH
5835 * arguments and performs the three steps in one go.
5836 *
3d46b2e2
PM
5837 * An invalid IRQ skips the IRQ registration and expects the host to
5838 * have set polling mode on the port. In this case, @irq_handler
5839 * should be NULL.
5840 *
f5cda257
TH
5841 * LOCKING:
5842 * Inherited from calling layer (may sleep).
5843 *
5844 * RETURNS:
5845 * 0 on success, -errno otherwise.
5846 */
5847int ata_host_activate(struct ata_host *host, int irq,
5848 irq_handler_t irq_handler, unsigned long irq_flags,
5849 struct scsi_host_template *sht)
5850{
cbcdd875 5851 int i, rc;
7e22c002 5852 char *irq_desc;
f5cda257
TH
5853
5854 rc = ata_host_start(host);
5855 if (rc)
5856 return rc;
5857
3d46b2e2
PM
5858 /* Special case for polling mode */
5859 if (!irq) {
5860 WARN_ON(irq_handler);
5861 return ata_host_register(host, sht);
5862 }
5863
7e22c002
HK
5864 irq_desc = devm_kasprintf(host->dev, GFP_KERNEL, "%s[%s]",
5865 dev_driver_string(host->dev),
5866 dev_name(host->dev));
5867 if (!irq_desc)
5868 return -ENOMEM;
5869
f5cda257 5870 rc = devm_request_irq(host->dev, irq, irq_handler, irq_flags,
7e22c002 5871 irq_desc, host);
f5cda257
TH
5872 if (rc)
5873 return rc;
5874
cbcdd875
TH
5875 for (i = 0; i < host->n_ports; i++)
5876 ata_port_desc(host->ports[i], "irq %d", irq);
4031826b 5877
f5cda257
TH
5878 rc = ata_host_register(host, sht);
5879 /* if failed, just free the IRQ and leave ports alone */
5880 if (rc)
5881 devm_free_irq(host->dev, irq, host);
5882
5883 return rc;
5884}
a52fbcfc 5885EXPORT_SYMBOL_GPL(ata_host_activate);
f5cda257 5886
720ba126 5887/**
c9b5560a 5888 * ata_port_detach - Detach ATA port in preparation of device removal
720ba126
TH
5889 * @ap: ATA port to be detached
5890 *
5891 * Detach all ATA devices and the associated SCSI devices of @ap;
5892 * then, remove the associated SCSI host. @ap is guaranteed to
5893 * be quiescent on return from this function.
5894 *
5895 * LOCKING:
5896 * Kernel thread context (may sleep).
5897 */
741b7763 5898static void ata_port_detach(struct ata_port *ap)
720ba126
TH
5899{
5900 unsigned long flags;
a6f9bf4d
LK
5901 struct ata_link *link;
5902 struct ata_device *dev;
720ba126
TH
5903
5904 if (!ap->ops->error_handler)
c3cf30a9 5905 goto skip_eh;
720ba126
TH
5906
5907 /* tell EH we're leaving & flush EH */
ba6a1308 5908 spin_lock_irqsave(ap->lock, flags);
b51e9e5d 5909 ap->pflags |= ATA_PFLAG_UNLOADING;
ece180d1 5910 ata_port_schedule_eh(ap);
ba6a1308 5911 spin_unlock_irqrestore(ap->lock, flags);
720ba126 5912
ece180d1 5913 /* wait till EH commits suicide */
720ba126
TH
5914 ata_port_wait_eh(ap);
5915
ece180d1
TH
5916 /* it better be dead now */
5917 WARN_ON(!(ap->pflags & ATA_PFLAG_UNLOADED));
720ba126 5918
afe2c511 5919 cancel_delayed_work_sync(&ap->hotplug_task);
720ba126 5920
c3cf30a9 5921 skip_eh:
a6f9bf4d
LK
5922 /* clean up zpodd on port removal */
5923 ata_for_each_link(link, ap, HOST_FIRST) {
5924 ata_for_each_dev(dev, link, ALL) {
5925 if (zpodd_dev_enabled(dev))
5926 zpodd_exit(dev);
5927 }
5928 }
d9027470
GG
5929 if (ap->pmp_link) {
5930 int i;
5931 for (i = 0; i < SATA_PMP_MAX_PORTS; i++)
5932 ata_tlink_delete(&ap->pmp_link[i]);
5933 }
720ba126 5934 /* remove the associated SCSI host */
cca3974e 5935 scsi_remove_host(ap->scsi_host);
c5700766 5936 ata_tport_delete(ap);
720ba126
TH
5937}
5938
0529c159
TH
5939/**
5940 * ata_host_detach - Detach all ports of an ATA host
5941 * @host: Host to detach
5942 *
5943 * Detach all ports of @host.
5944 *
5945 * LOCKING:
5946 * Kernel thread context (may sleep).
5947 */
5948void ata_host_detach(struct ata_host *host)
5949{
5950 int i;
5951
b5292111
KHF
5952 for (i = 0; i < host->n_ports; i++) {
5953 /* Ensure ata_port probe has completed */
5954 async_synchronize_cookie(host->ports[i]->cookie + 1);
0529c159 5955 ata_port_detach(host->ports[i]);
b5292111 5956 }
562f0c2d
TH
5957
5958 /* the host is dead now, dissociate ACPI */
5959 ata_acpi_dissociate(host);
0529c159 5960}
a52fbcfc 5961EXPORT_SYMBOL_GPL(ata_host_detach);
0529c159 5962
374b1873
JG
5963#ifdef CONFIG_PCI
5964
1da177e4
LT
5965/**
5966 * ata_pci_remove_one - PCI layer callback for device removal
5967 * @pdev: PCI device that was removed
5968 *
b878ca5d
TH
5969 * PCI layer indicates to libata via this hook that hot-unplug or
5970 * module unload event has occurred. Detach all ports. Resource
5971 * release is handled via devres.
1da177e4
LT
5972 *
5973 * LOCKING:
5974 * Inherited from PCI layer (may sleep).
5975 */
f0d36efd 5976void ata_pci_remove_one(struct pci_dev *pdev)
1da177e4 5977{
04a3f5b7 5978 struct ata_host *host = pci_get_drvdata(pdev);
1da177e4 5979
b878ca5d 5980 ata_host_detach(host);
1da177e4 5981}
a52fbcfc 5982EXPORT_SYMBOL_GPL(ata_pci_remove_one);
1da177e4 5983
10a663a1
PK
5984void ata_pci_shutdown_one(struct pci_dev *pdev)
5985{
5986 struct ata_host *host = pci_get_drvdata(pdev);
5987 int i;
5988
5989 for (i = 0; i < host->n_ports; i++) {
5990 struct ata_port *ap = host->ports[i];
5991
5992 ap->pflags |= ATA_PFLAG_FROZEN;
5993
5994 /* Disable port interrupts */
5995 if (ap->ops->freeze)
5996 ap->ops->freeze(ap);
5997
5998 /* Stop the port DMA engines */
5999 if (ap->ops->port_stop)
6000 ap->ops->port_stop(ap);
6001 }
6002}
a52fbcfc 6003EXPORT_SYMBOL_GPL(ata_pci_shutdown_one);
10a663a1 6004
1da177e4 6005/* move to PCI subsystem */
057ace5e 6006int pci_test_config_bits(struct pci_dev *pdev, const struct pci_bits *bits)
1da177e4
LT
6007{
6008 unsigned long tmp = 0;
6009
6010 switch (bits->width) {
6011 case 1: {
6012 u8 tmp8 = 0;
6013 pci_read_config_byte(pdev, bits->reg, &tmp8);
6014 tmp = tmp8;
6015 break;
6016 }
6017 case 2: {
6018 u16 tmp16 = 0;
6019 pci_read_config_word(pdev, bits->reg, &tmp16);
6020 tmp = tmp16;
6021 break;
6022 }
6023 case 4: {
6024 u32 tmp32 = 0;
6025 pci_read_config_dword(pdev, bits->reg, &tmp32);
6026 tmp = tmp32;
6027 break;
6028 }
6029
6030 default:
6031 return -EINVAL;
6032 }
6033
6034 tmp &= bits->mask;
6035
6036 return (tmp == bits->val) ? 1 : 0;
6037}
a52fbcfc 6038EXPORT_SYMBOL_GPL(pci_test_config_bits);
9b847548 6039
6ffa01d8 6040#ifdef CONFIG_PM
3c5100c1 6041void ata_pci_device_do_suspend(struct pci_dev *pdev, pm_message_t mesg)
9b847548
JA
6042{
6043 pci_save_state(pdev);
4c90d971 6044 pci_disable_device(pdev);
500530f6 6045
3a2d5b70 6046 if (mesg.event & PM_EVENT_SLEEP)
500530f6 6047 pci_set_power_state(pdev, PCI_D3hot);
9b847548 6048}
a52fbcfc 6049EXPORT_SYMBOL_GPL(ata_pci_device_do_suspend);
9b847548 6050
553c4aa6 6051int ata_pci_device_do_resume(struct pci_dev *pdev)
9b847548 6052{
553c4aa6
TH
6053 int rc;
6054
9b847548
JA
6055 pci_set_power_state(pdev, PCI_D0);
6056 pci_restore_state(pdev);
553c4aa6 6057
b878ca5d 6058 rc = pcim_enable_device(pdev);
553c4aa6 6059 if (rc) {
a44fec1f
JP
6060 dev_err(&pdev->dev,
6061 "failed to enable device after resume (%d)\n", rc);
553c4aa6
TH
6062 return rc;
6063 }
6064
9b847548 6065 pci_set_master(pdev);
553c4aa6 6066 return 0;
500530f6 6067}
a52fbcfc 6068EXPORT_SYMBOL_GPL(ata_pci_device_do_resume);
500530f6 6069
3c5100c1 6070int ata_pci_device_suspend(struct pci_dev *pdev, pm_message_t mesg)
500530f6 6071{
04a3f5b7 6072 struct ata_host *host = pci_get_drvdata(pdev);
500530f6 6073
ec87cf37 6074 ata_host_suspend(host, mesg);
500530f6 6075
3c5100c1 6076 ata_pci_device_do_suspend(pdev, mesg);
500530f6
TH
6077
6078 return 0;
6079}
a52fbcfc 6080EXPORT_SYMBOL_GPL(ata_pci_device_suspend);
500530f6
TH
6081
6082int ata_pci_device_resume(struct pci_dev *pdev)
6083{
04a3f5b7 6084 struct ata_host *host = pci_get_drvdata(pdev);
553c4aa6 6085 int rc;
500530f6 6086
553c4aa6
TH
6087 rc = ata_pci_device_do_resume(pdev);
6088 if (rc == 0)
6089 ata_host_resume(host);
6090 return rc;
9b847548 6091}
a52fbcfc 6092EXPORT_SYMBOL_GPL(ata_pci_device_resume);
6ffa01d8 6093#endif /* CONFIG_PM */
1da177e4
LT
6094#endif /* CONFIG_PCI */
6095
b7db04d9
BN
6096/**
6097 * ata_platform_remove_one - Platform layer callback for device removal
6098 * @pdev: Platform device that was removed
6099 *
6100 * Platform layer indicates to libata via this hook that hot-unplug or
6101 * module unload event has occurred. Detach all ports. Resource
6102 * release is handled via devres.
6103 *
6104 * LOCKING:
6105 * Inherited from platform layer (may sleep).
6106 */
6107int ata_platform_remove_one(struct platform_device *pdev)
6108{
6109 struct ata_host *host = platform_get_drvdata(pdev);
6110
6111 ata_host_detach(host);
6112
6113 return 0;
6114}
a52fbcfc 6115EXPORT_SYMBOL_GPL(ata_platform_remove_one);
b7db04d9 6116
bf89b0bf 6117#ifdef CONFIG_ATA_FORCE
168af4af
DLM
6118
6119#define force_cbl(name, flag) \
6120 { #name, .cbl = (flag) }
6121
6122#define force_spd_limit(spd, val) \
6123 { #spd, .spd_limit = (val) }
6124
6125#define force_xfer(mode, shift) \
6126 { #mode, .xfer_mask = (1UL << (shift)) }
6127
3af9ca4d
DLM
6128#define force_lflag_on(name, flags) \
6129 { #name, .lflags_on = (flags) }
6130
6131#define force_lflag_onoff(name, flags) \
6132 { "no" #name, .lflags_on = (flags) }, \
6133 { #name, .lflags_off = (flags) }
168af4af
DLM
6134
6135#define force_horkage_on(name, flag) \
6136 { #name, .horkage_on = (flag) }
6137
6138#define force_horkage_onoff(name, flag) \
6139 { "no" #name, .horkage_on = (flag) }, \
6140 { #name, .horkage_off = (flag) }
6141
6142static const struct ata_force_param force_tbl[] __initconst = {
6143 force_cbl(40c, ATA_CBL_PATA40),
6144 force_cbl(80c, ATA_CBL_PATA80),
6145 force_cbl(short40c, ATA_CBL_PATA40_SHORT),
6146 force_cbl(unk, ATA_CBL_PATA_UNK),
6147 force_cbl(ign, ATA_CBL_PATA_IGN),
6148 force_cbl(sata, ATA_CBL_SATA),
6149
6150 force_spd_limit(1.5Gbps, 1),
6151 force_spd_limit(3.0Gbps, 2),
6152
6153 force_xfer(pio0, ATA_SHIFT_PIO + 0),
6154 force_xfer(pio1, ATA_SHIFT_PIO + 1),
6155 force_xfer(pio2, ATA_SHIFT_PIO + 2),
6156 force_xfer(pio3, ATA_SHIFT_PIO + 3),
6157 force_xfer(pio4, ATA_SHIFT_PIO + 4),
6158 force_xfer(pio5, ATA_SHIFT_PIO + 5),
6159 force_xfer(pio6, ATA_SHIFT_PIO + 6),
6160 force_xfer(mwdma0, ATA_SHIFT_MWDMA + 0),
6161 force_xfer(mwdma1, ATA_SHIFT_MWDMA + 1),
6162 force_xfer(mwdma2, ATA_SHIFT_MWDMA + 2),
6163 force_xfer(mwdma3, ATA_SHIFT_MWDMA + 3),
6164 force_xfer(mwdma4, ATA_SHIFT_MWDMA + 4),
6165 force_xfer(udma0, ATA_SHIFT_UDMA + 0),
6166 force_xfer(udma16, ATA_SHIFT_UDMA + 0),
6167 force_xfer(udma/16, ATA_SHIFT_UDMA + 0),
6168 force_xfer(udma1, ATA_SHIFT_UDMA + 1),
6169 force_xfer(udma25, ATA_SHIFT_UDMA + 1),
6170 force_xfer(udma/25, ATA_SHIFT_UDMA + 1),
6171 force_xfer(udma2, ATA_SHIFT_UDMA + 2),
6172 force_xfer(udma33, ATA_SHIFT_UDMA + 2),
6173 force_xfer(udma/33, ATA_SHIFT_UDMA + 2),
6174 force_xfer(udma3, ATA_SHIFT_UDMA + 3),
6175 force_xfer(udma44, ATA_SHIFT_UDMA + 3),
6176 force_xfer(udma/44, ATA_SHIFT_UDMA + 3),
6177 force_xfer(udma4, ATA_SHIFT_UDMA + 4),
6178 force_xfer(udma66, ATA_SHIFT_UDMA + 4),
6179 force_xfer(udma/66, ATA_SHIFT_UDMA + 4),
6180 force_xfer(udma5, ATA_SHIFT_UDMA + 5),
6181 force_xfer(udma100, ATA_SHIFT_UDMA + 5),
6182 force_xfer(udma/100, ATA_SHIFT_UDMA + 5),
6183 force_xfer(udma6, ATA_SHIFT_UDMA + 6),
6184 force_xfer(udma133, ATA_SHIFT_UDMA + 6),
6185 force_xfer(udma/133, ATA_SHIFT_UDMA + 6),
6186 force_xfer(udma7, ATA_SHIFT_UDMA + 7),
6187
3af9ca4d
DLM
6188 force_lflag_on(nohrst, ATA_LFLAG_NO_HRST),
6189 force_lflag_on(nosrst, ATA_LFLAG_NO_SRST),
6190 force_lflag_on(norst, ATA_LFLAG_NO_HRST | ATA_LFLAG_NO_SRST),
6191 force_lflag_on(rstonce, ATA_LFLAG_RST_ONCE),
6192 force_lflag_onoff(dbdelay, ATA_LFLAG_NO_DEBOUNCE_DELAY),
168af4af
DLM
6193
6194 force_horkage_onoff(ncq, ATA_HORKAGE_NONCQ),
6195 force_horkage_onoff(ncqtrim, ATA_HORKAGE_NO_NCQ_TRIM),
6196 force_horkage_onoff(ncqati, ATA_HORKAGE_NO_NCQ_ON_ATI),
6197
2c33bbda
DLM
6198 force_horkage_onoff(trim, ATA_HORKAGE_NOTRIM),
6199 force_horkage_on(trim_zero, ATA_HORKAGE_ZERO_AFTER_TRIM),
6200 force_horkage_on(max_trim_128m, ATA_HORKAGE_MAX_TRIM_128M),
6201
6202 force_horkage_onoff(dma, ATA_HORKAGE_NODMA),
168af4af 6203 force_horkage_on(atapi_dmadir, ATA_HORKAGE_ATAPI_DMADIR),
2c33bbda
DLM
6204 force_horkage_on(atapi_mod16_dma, ATA_HORKAGE_ATAPI_MOD16_DMA),
6205
6206 force_horkage_onoff(dmalog, ATA_HORKAGE_NO_DMA_LOG),
6207 force_horkage_onoff(iddevlog, ATA_HORKAGE_NO_ID_DEV_LOG),
6208 force_horkage_onoff(logdir, ATA_HORKAGE_NO_LOG_DIR),
6209
6210 force_horkage_on(max_sec_128, ATA_HORKAGE_MAX_SEC_128),
6211 force_horkage_on(max_sec_1024, ATA_HORKAGE_MAX_SEC_1024),
6212 force_horkage_on(max_sec_lba48, ATA_HORKAGE_MAX_SEC_LBA48),
6213
6214 force_horkage_onoff(lpm, ATA_HORKAGE_NOLPM),
6215 force_horkage_onoff(setxfer, ATA_HORKAGE_NOSETXFER),
6216 force_horkage_on(dump_id, ATA_HORKAGE_DUMP_ID),
6217
6218 force_horkage_on(disable, ATA_HORKAGE_DISABLE),
168af4af
DLM
6219};
6220
33267325
TH
6221static int __init ata_parse_force_one(char **cur,
6222 struct ata_force_ent *force_ent,
6223 const char **reason)
6224{
33267325
TH
6225 char *start = *cur, *p = *cur;
6226 char *id, *val, *endp;
6227 const struct ata_force_param *match_fp = NULL;
6228 int nr_matches = 0, i;
6229
6230 /* find where this param ends and update *cur */
6231 while (*p != '\0' && *p != ',')
6232 p++;
6233
6234 if (*p == '\0')
6235 *cur = p;
6236 else
6237 *cur = p + 1;
6238
6239 *p = '\0';
6240
6241 /* parse */
6242 p = strchr(start, ':');
6243 if (!p) {
6244 val = strstrip(start);
6245 goto parse_val;
6246 }
6247 *p = '\0';
6248
6249 id = strstrip(start);
6250 val = strstrip(p + 1);
6251
6252 /* parse id */
6253 p = strchr(id, '.');
6254 if (p) {
6255 *p++ = '\0';
6256 force_ent->device = simple_strtoul(p, &endp, 10);
6257 if (p == endp || *endp != '\0') {
6258 *reason = "invalid device";
6259 return -EINVAL;
6260 }
6261 }
6262
6263 force_ent->port = simple_strtoul(id, &endp, 10);
f7cf69ae 6264 if (id == endp || *endp != '\0') {
33267325
TH
6265 *reason = "invalid port/link";
6266 return -EINVAL;
6267 }
6268
6269 parse_val:
6270 /* parse val, allow shortcuts so that both 1.5 and 1.5Gbps work */
6271 for (i = 0; i < ARRAY_SIZE(force_tbl); i++) {
6272 const struct ata_force_param *fp = &force_tbl[i];
6273
6274 if (strncasecmp(val, fp->name, strlen(val)))
6275 continue;
6276
6277 nr_matches++;
6278 match_fp = fp;
6279
6280 if (strcasecmp(val, fp->name) == 0) {
6281 nr_matches = 1;
6282 break;
6283 }
6284 }
6285
6286 if (!nr_matches) {
6287 *reason = "unknown value";
6288 return -EINVAL;
6289 }
6290 if (nr_matches > 1) {
9de55351 6291 *reason = "ambiguous value";
33267325
TH
6292 return -EINVAL;
6293 }
6294
6295 force_ent->param = *match_fp;
6296
6297 return 0;
6298}
6299
6300static void __init ata_parse_force_param(void)
6301{
6302 int idx = 0, size = 1;
6303 int last_port = -1, last_device = -1;
6304 char *p, *cur, *next;
6305
168af4af 6306 /* Calculate maximum number of params and allocate ata_force_tbl */
33267325
TH
6307 for (p = ata_force_param_buf; *p; p++)
6308 if (*p == ',')
6309 size++;
6310
6396bb22 6311 ata_force_tbl = kcalloc(size, sizeof(ata_force_tbl[0]), GFP_KERNEL);
33267325
TH
6312 if (!ata_force_tbl) {
6313 printk(KERN_WARNING "ata: failed to extend force table, "
6314 "libata.force ignored\n");
6315 return;
6316 }
6317
6318 /* parse and populate the table */
6319 for (cur = ata_force_param_buf; *cur != '\0'; cur = next) {
6320 const char *reason = "";
6321 struct ata_force_ent te = { .port = -1, .device = -1 };
6322
6323 next = cur;
6324 if (ata_parse_force_one(&next, &te, &reason)) {
6325 printk(KERN_WARNING "ata: failed to parse force "
6326 "parameter \"%s\" (%s)\n",
6327 cur, reason);
6328 continue;
6329 }
6330
6331 if (te.port == -1) {
6332 te.port = last_port;
6333 te.device = last_device;
6334 }
6335
6336 ata_force_tbl[idx++] = te;
6337
6338 last_port = te.port;
6339 last_device = te.device;
6340 }
6341
6342 ata_force_tbl_size = idx;
6343}
1da177e4 6344
bf89b0bf
BZ
6345static void ata_free_force_param(void)
6346{
6347 kfree(ata_force_tbl);
6348}
6349#else
6350static inline void ata_parse_force_param(void) { }
6351static inline void ata_free_force_param(void) { }
6352#endif
6353
1da177e4
LT
6354static int __init ata_init(void)
6355{
d9027470 6356 int rc;
270390e1 6357
33267325
TH
6358 ata_parse_force_param();
6359
270390e1 6360 rc = ata_sff_init();
ad72cf98 6361 if (rc) {
bf89b0bf 6362 ata_free_force_param();
ad72cf98
TH
6363 return rc;
6364 }
453b07ac 6365
d9027470
GG
6366 libata_transport_init();
6367 ata_scsi_transport_template = ata_attach_transport();
6368 if (!ata_scsi_transport_template) {
6369 ata_sff_exit();
6370 rc = -ENOMEM;
6371 goto err_out;
4fca377f 6372 }
d9027470 6373
1da177e4
LT
6374 printk(KERN_DEBUG "libata version " DRV_VERSION " loaded.\n");
6375 return 0;
d9027470
GG
6376
6377err_out:
6378 return rc;
1da177e4
LT
6379}
6380
6381static void __exit ata_exit(void)
6382{
d9027470
GG
6383 ata_release_transport(ata_scsi_transport_template);
6384 libata_transport_exit();
270390e1 6385 ata_sff_exit();
bf89b0bf 6386 ata_free_force_param();
1da177e4
LT
6387}
6388
a4625085 6389subsys_initcall(ata_init);
1da177e4
LT
6390module_exit(ata_exit);
6391
9990b6f3 6392static DEFINE_RATELIMIT_STATE(ratelimit, HZ / 5, 1);
67846b30
JG
6393
6394int ata_ratelimit(void)
6395{
9990b6f3 6396 return __ratelimit(&ratelimit);
67846b30 6397}
a52fbcfc 6398EXPORT_SYMBOL_GPL(ata_ratelimit);
67846b30 6399
c0c362b6
TH
6400/**
6401 * ata_msleep - ATA EH owner aware msleep
6402 * @ap: ATA port to attribute the sleep to
6403 * @msecs: duration to sleep in milliseconds
6404 *
6405 * Sleeps @msecs. If the current task is owner of @ap's EH, the
6406 * ownership is released before going to sleep and reacquired
6407 * after the sleep is complete. IOW, other ports sharing the
6408 * @ap->host will be allowed to own the EH while this task is
6409 * sleeping.
6410 *
6411 * LOCKING:
6412 * Might sleep.
6413 */
97750ceb
TH
6414void ata_msleep(struct ata_port *ap, unsigned int msecs)
6415{
c0c362b6
TH
6416 bool owns_eh = ap && ap->host->eh_owner == current;
6417
6418 if (owns_eh)
6419 ata_eh_release(ap);
6420
848c3920
AVM
6421 if (msecs < 20) {
6422 unsigned long usecs = msecs * USEC_PER_MSEC;
6423 usleep_range(usecs, usecs + 50);
6424 } else {
6425 msleep(msecs);
6426 }
c0c362b6
TH
6427
6428 if (owns_eh)
6429 ata_eh_acquire(ap);
97750ceb 6430}
a52fbcfc 6431EXPORT_SYMBOL_GPL(ata_msleep);
97750ceb 6432
c22daff4
TH
6433/**
6434 * ata_wait_register - wait until register value changes
97750ceb 6435 * @ap: ATA port to wait register for, can be NULL
c22daff4
TH
6436 * @reg: IO-mapped register
6437 * @mask: Mask to apply to read register value
6438 * @val: Wait condition
341c2c95
TH
6439 * @interval: polling interval in milliseconds
6440 * @timeout: timeout in milliseconds
c22daff4
TH
6441 *
6442 * Waiting for some bits of register to change is a common
6443 * operation for ATA controllers. This function reads 32bit LE
6444 * IO-mapped register @reg and tests for the following condition.
6445 *
6446 * (*@reg & mask) != val
6447 *
6448 * If the condition is met, it returns; otherwise, the process is
6449 * repeated after @interval_msec until timeout.
6450 *
6451 * LOCKING:
6452 * Kernel thread context (may sleep)
6453 *
6454 * RETURNS:
6455 * The final register value.
6456 */
97750ceb 6457u32 ata_wait_register(struct ata_port *ap, void __iomem *reg, u32 mask, u32 val,
341c2c95 6458 unsigned long interval, unsigned long timeout)
c22daff4 6459{
341c2c95 6460 unsigned long deadline;
c22daff4
TH
6461 u32 tmp;
6462
6463 tmp = ioread32(reg);
6464
6465 /* Calculate timeout _after_ the first read to make sure
6466 * preceding writes reach the controller before starting to
6467 * eat away the timeout.
6468 */
341c2c95 6469 deadline = ata_deadline(jiffies, timeout);
c22daff4 6470
341c2c95 6471 while ((tmp & mask) == val && time_before(jiffies, deadline)) {
97750ceb 6472 ata_msleep(ap, interval);
c22daff4
TH
6473 tmp = ioread32(reg);
6474 }
6475
6476 return tmp;
6477}
a52fbcfc 6478EXPORT_SYMBOL_GPL(ata_wait_register);
c22daff4 6479
dd5b06c4
TH
6480/*
6481 * Dummy port_ops
6482 */
182d7bba 6483static unsigned int ata_dummy_qc_issue(struct ata_queued_cmd *qc)
dd5b06c4 6484{
182d7bba 6485 return AC_ERR_SYSTEM;
dd5b06c4
TH
6486}
6487
182d7bba 6488static void ata_dummy_error_handler(struct ata_port *ap)
dd5b06c4 6489{
182d7bba 6490 /* truly dummy */
dd5b06c4
TH
6491}
6492
029cfd6b 6493struct ata_port_operations ata_dummy_port_ops = {
dd5b06c4
TH
6494 .qc_prep = ata_noop_qc_prep,
6495 .qc_issue = ata_dummy_qc_issue,
182d7bba 6496 .error_handler = ata_dummy_error_handler,
e4a9c373
DW
6497 .sched_eh = ata_std_sched_eh,
6498 .end_eh = ata_std_end_eh,
dd5b06c4 6499};
a52fbcfc 6500EXPORT_SYMBOL_GPL(ata_dummy_port_ops);
dd5b06c4 6501
21b0ad4f
TH
6502const struct ata_port_info ata_dummy_port_info = {
6503 .port_ops = &ata_dummy_port_ops,
6504};
a52fbcfc 6505EXPORT_SYMBOL_GPL(ata_dummy_port_info);
21b0ad4f 6506
06296a1e
JP
6507void ata_print_version(const struct device *dev, const char *version)
6508{
6509 dev_printk(KERN_DEBUG, dev, "version %s\n", version);
6510}
6511EXPORT_SYMBOL(ata_print_version);
c206a389
HR
6512
6513EXPORT_TRACEPOINT_SYMBOL_GPL(ata_tf_load);
6514EXPORT_TRACEPOINT_SYMBOL_GPL(ata_exec_command);
6515EXPORT_TRACEPOINT_SYMBOL_GPL(ata_bmdma_setup);
6516EXPORT_TRACEPOINT_SYMBOL_GPL(ata_bmdma_start);
6517EXPORT_TRACEPOINT_SYMBOL_GPL(ata_bmdma_status);