ata: start separating SATA specific code from libata-core.c
[linux/fpc-iii.git] / drivers / ata / libata-core.c
blobb4a952dce7ab0891e4a51442628fe6ad43988a78
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * libata-core.c - helper library for ATA
5 * Copyright 2003-2004 Red Hat, Inc. All rights reserved.
6 * Copyright 2003-2004 Jeff Garzik
8 * libata documentation is available via 'make {ps|pdf}docs',
9 * as Documentation/driver-api/libata.rst
11 * Hardware documentation available from http://www.t13.org/ and
12 * http://www.sata-io.org/
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)
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.
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>
34 #include <linux/spinlock.h>
35 #include <linux/blkdev.h>
36 #include <linux/delay.h>
37 #include <linux/timer.h>
38 #include <linux/time.h>
39 #include <linux/interrupt.h>
40 #include <linux/completion.h>
41 #include <linux/suspend.h>
42 #include <linux/workqueue.h>
43 #include <linux/scatterlist.h>
44 #include <linux/io.h>
45 #include <linux/async.h>
46 #include <linux/log2.h>
47 #include <linux/slab.h>
48 #include <linux/glob.h>
49 #include <scsi/scsi.h>
50 #include <scsi/scsi_cmnd.h>
51 #include <scsi/scsi_host.h>
52 #include <linux/libata.h>
53 #include <asm/byteorder.h>
54 #include <asm/unaligned.h>
55 #include <linux/cdrom.h>
56 #include <linux/ratelimit.h>
57 #include <linux/leds.h>
58 #include <linux/pm_runtime.h>
59 #include <linux/platform_device.h>
60 #include <asm/setup.h>
62 #define CREATE_TRACE_POINTS
63 #include <trace/events/libata.h>
65 #include "libata.h"
66 #include "libata-transport.h"
68 /* debounce timing parameters in msecs { interval, duration, timeout } */
69 const unsigned long sata_deb_timing_normal[] = { 5, 100, 2000 };
70 EXPORT_SYMBOL_GPL(sata_deb_timing_normal);
71 const unsigned long sata_deb_timing_hotplug[] = { 25, 500, 2000 };
72 EXPORT_SYMBOL_GPL(sata_deb_timing_hotplug);
73 const unsigned long sata_deb_timing_long[] = { 100, 2000, 5000 };
74 EXPORT_SYMBOL_GPL(sata_deb_timing_long);
76 const struct ata_port_operations ata_base_port_ops = {
77 .prereset = ata_std_prereset,
78 .postreset = ata_std_postreset,
79 .error_handler = ata_std_error_handler,
80 .sched_eh = ata_std_sched_eh,
81 .end_eh = ata_std_end_eh,
84 const struct ata_port_operations sata_port_ops = {
85 .inherits = &ata_base_port_ops,
87 .qc_defer = ata_std_qc_defer,
88 .hardreset = sata_std_hardreset,
90 EXPORT_SYMBOL_GPL(sata_port_ops);
92 static unsigned int ata_dev_init_params(struct ata_device *dev,
93 u16 heads, u16 sectors);
94 static unsigned int ata_dev_set_xfermode(struct ata_device *dev);
95 static void ata_dev_xfermask(struct ata_device *dev);
96 static unsigned long ata_dev_blacklisted(const struct ata_device *dev);
98 atomic_t ata_print_id = ATOMIC_INIT(0);
100 struct ata_force_param {
101 const char *name;
102 u8 cbl;
103 u8 spd_limit;
104 unsigned long xfer_mask;
105 unsigned int horkage_on;
106 unsigned int horkage_off;
107 u16 lflags;
110 struct ata_force_ent {
111 int port;
112 int device;
113 struct ata_force_param param;
116 static struct ata_force_ent *ata_force_tbl;
117 static int ata_force_tbl_size;
119 static char ata_force_param_buf[COMMAND_LINE_SIZE] __initdata;
120 /* param_buf is thrown away after initialization, disallow read */
121 module_param_string(force, ata_force_param_buf, sizeof(ata_force_param_buf), 0);
122 MODULE_PARM_DESC(force, "Force ATA configurations including cable type, link speed and transfer mode (see Documentation/admin-guide/kernel-parameters.rst for details)");
124 static int atapi_enabled = 1;
125 module_param(atapi_enabled, int, 0444);
126 MODULE_PARM_DESC(atapi_enabled, "Enable discovery of ATAPI devices (0=off, 1=on [default])");
128 static int atapi_dmadir = 0;
129 module_param(atapi_dmadir, int, 0444);
130 MODULE_PARM_DESC(atapi_dmadir, "Enable ATAPI DMADIR bridge support (0=off [default], 1=on)");
132 int atapi_passthru16 = 1;
133 module_param(atapi_passthru16, int, 0444);
134 MODULE_PARM_DESC(atapi_passthru16, "Enable ATA_16 passthru for ATAPI devices (0=off, 1=on [default])");
136 int libata_fua = 0;
137 module_param_named(fua, libata_fua, int, 0444);
138 MODULE_PARM_DESC(fua, "FUA support (0=off [default], 1=on)");
140 static int ata_ignore_hpa;
141 module_param_named(ignore_hpa, ata_ignore_hpa, int, 0644);
142 MODULE_PARM_DESC(ignore_hpa, "Ignore HPA limit (0=keep BIOS limits, 1=ignore limits, using full disk)");
144 static int libata_dma_mask = ATA_DMA_MASK_ATA|ATA_DMA_MASK_ATAPI|ATA_DMA_MASK_CFA;
145 module_param_named(dma, libata_dma_mask, int, 0444);
146 MODULE_PARM_DESC(dma, "DMA enable/disable (0x1==ATA, 0x2==ATAPI, 0x4==CF)");
148 static int ata_probe_timeout;
149 module_param(ata_probe_timeout, int, 0444);
150 MODULE_PARM_DESC(ata_probe_timeout, "Set ATA probing timeout (seconds)");
152 int libata_noacpi = 0;
153 module_param_named(noacpi, libata_noacpi, int, 0444);
154 MODULE_PARM_DESC(noacpi, "Disable the use of ACPI in probe/suspend/resume (0=off [default], 1=on)");
156 int libata_allow_tpm = 0;
157 module_param_named(allow_tpm, libata_allow_tpm, int, 0444);
158 MODULE_PARM_DESC(allow_tpm, "Permit the use of TPM commands (0=off [default], 1=on)");
160 static int atapi_an;
161 module_param(atapi_an, int, 0444);
162 MODULE_PARM_DESC(atapi_an, "Enable ATAPI AN media presence notification (0=0ff [default], 1=on)");
164 MODULE_AUTHOR("Jeff Garzik");
165 MODULE_DESCRIPTION("Library module for ATA devices");
166 MODULE_LICENSE("GPL");
167 MODULE_VERSION(DRV_VERSION);
170 static bool ata_sstatus_online(u32 sstatus)
172 return (sstatus & 0xf) == 0x3;
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_*
181 * LOCKING:
182 * Host lock or EH context.
184 * RETURNS:
185 * Pointer to the next link.
187 struct ata_link *ata_link_next(struct ata_link *link, struct ata_port *ap,
188 enum ata_link_iter_mode mode)
190 BUG_ON(mode != ATA_LITER_EDGE &&
191 mode != ATA_LITER_PMP_FIRST && mode != ATA_LITER_HOST_FIRST);
193 /* NULL link indicates start of iteration */
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;
200 /* fall through */
201 case ATA_LITER_HOST_FIRST:
202 return &ap->link;
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;
211 /* fall through */
212 case ATA_LITER_PMP_FIRST:
213 if (unlikely(ap->slave_link))
214 return ap->slave_link;
215 /* fall through */
216 case ATA_LITER_EDGE:
217 return NULL;
220 /* slave_link excludes PMP */
221 if (unlikely(link == ap->slave_link))
222 return NULL;
224 /* we were over a PMP link */
225 if (++link < ap->pmp_link + ap->nr_pmp_links)
226 return link;
228 if (mode == ATA_LITER_PMP_FIRST)
229 return &ap->link;
231 return NULL;
233 EXPORT_SYMBOL_GPL(ata_link_next);
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_*
241 * LOCKING:
242 * Host lock or EH context.
244 * RETURNS:
245 * Pointer to the next device.
247 struct ata_device *ata_dev_next(struct ata_device *dev, struct ata_link *link,
248 enum ata_dev_iter_mode mode)
250 BUG_ON(mode != ATA_DITER_ENABLED && mode != ATA_DITER_ENABLED_REVERSE &&
251 mode != ATA_DITER_ALL && mode != ATA_DITER_ALL_REVERSE);
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;
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;
281 check:
282 if ((mode == ATA_DITER_ENABLED || mode == ATA_DITER_ENABLED_REVERSE) &&
283 !ata_dev_enabled(dev))
284 goto next;
285 return dev;
287 EXPORT_SYMBOL_GPL(ata_dev_next);
290 * ata_dev_phys_link - find physical link for a device
291 * @dev: ATA device to look up physical link for
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.
297 * LOCKING:
298 * Don't care.
300 * RETURNS:
301 * Pointer to the found physical link.
303 struct ata_link *ata_dev_phys_link(struct ata_device *dev)
305 struct ata_port *ap = dev->link->ap;
307 if (!ap->slave_link)
308 return dev->link;
309 if (!dev->devno)
310 return &ap->link;
311 return ap->slave_link;
315 * ata_force_cbl - force cable type according to libata.force
316 * @ap: ATA port of interest
318 * Force cable type according to libata.force and whine about it.
319 * The last entry which has matching port number is used, so it
320 * can be specified as part of device force parameters. For
321 * example, both "a:40c,1.00:udma4" and "1.00:40c,udma4" have the
322 * same effect.
324 * LOCKING:
325 * EH context.
327 void ata_force_cbl(struct ata_port *ap)
329 int i;
331 for (i = ata_force_tbl_size - 1; i >= 0; i--) {
332 const struct ata_force_ent *fe = &ata_force_tbl[i];
334 if (fe->port != -1 && fe->port != ap->print_id)
335 continue;
337 if (fe->param.cbl == ATA_CBL_NONE)
338 continue;
340 ap->cbl = fe->param.cbl;
341 ata_port_notice(ap, "FORCE: cable set to %s\n", fe->param.name);
342 return;
347 * ata_force_link_limits - force link limits according to libata.force
348 * @link: ATA link of interest
350 * Force link flags and SATA spd limit according to libata.force
351 * and whine about it. When only the port part is specified
352 * (e.g. 1:), the limit applies to all links connected to both
353 * the host link and all fan-out ports connected via PMP. If the
354 * device part is specified as 0 (e.g. 1.00:), it specifies the
355 * first fan-out link not the host link. Device number 15 always
356 * points to the host link whether PMP is attached or not. If the
357 * controller has slave link, device number 16 points to it.
359 * LOCKING:
360 * EH context.
362 static void ata_force_link_limits(struct ata_link *link)
364 bool did_spd = false;
365 int linkno = link->pmp;
366 int i;
368 if (ata_is_host_link(link))
369 linkno += 15;
371 for (i = ata_force_tbl_size - 1; i >= 0; i--) {
372 const struct ata_force_ent *fe = &ata_force_tbl[i];
374 if (fe->port != -1 && fe->port != link->ap->print_id)
375 continue;
377 if (fe->device != -1 && fe->device != linkno)
378 continue;
380 /* only honor the first spd limit */
381 if (!did_spd && fe->param.spd_limit) {
382 link->hw_sata_spd_limit = (1 << fe->param.spd_limit) - 1;
383 ata_link_notice(link, "FORCE: PHY spd limit set to %s\n",
384 fe->param.name);
385 did_spd = true;
388 /* let lflags stack */
389 if (fe->param.lflags) {
390 link->flags |= fe->param.lflags;
391 ata_link_notice(link,
392 "FORCE: link flag 0x%x forced -> 0x%x\n",
393 fe->param.lflags, link->flags);
399 * ata_force_xfermask - force xfermask according to libata.force
400 * @dev: ATA device of interest
402 * Force xfer_mask according to libata.force and whine about it.
403 * For consistency with link selection, device number 15 selects
404 * the first device connected to the host link.
406 * LOCKING:
407 * EH context.
409 static void ata_force_xfermask(struct ata_device *dev)
411 int devno = dev->link->pmp + dev->devno;
412 int alt_devno = devno;
413 int i;
415 /* allow n.15/16 for devices attached to host port */
416 if (ata_is_host_link(dev->link))
417 alt_devno += 15;
419 for (i = ata_force_tbl_size - 1; i >= 0; i--) {
420 const struct ata_force_ent *fe = &ata_force_tbl[i];
421 unsigned long pio_mask, mwdma_mask, udma_mask;
423 if (fe->port != -1 && fe->port != dev->link->ap->print_id)
424 continue;
426 if (fe->device != -1 && fe->device != devno &&
427 fe->device != alt_devno)
428 continue;
430 if (!fe->param.xfer_mask)
431 continue;
433 ata_unpack_xfermask(fe->param.xfer_mask,
434 &pio_mask, &mwdma_mask, &udma_mask);
435 if (udma_mask)
436 dev->udma_mask = udma_mask;
437 else if (mwdma_mask) {
438 dev->udma_mask = 0;
439 dev->mwdma_mask = mwdma_mask;
440 } else {
441 dev->udma_mask = 0;
442 dev->mwdma_mask = 0;
443 dev->pio_mask = pio_mask;
446 ata_dev_notice(dev, "FORCE: xfer_mask set to %s\n",
447 fe->param.name);
448 return;
453 * ata_force_horkage - force horkage according to libata.force
454 * @dev: ATA device of interest
456 * Force horkage according to libata.force and whine about it.
457 * For consistency with link selection, device number 15 selects
458 * the first device connected to the host link.
460 * LOCKING:
461 * EH context.
463 static void ata_force_horkage(struct ata_device *dev)
465 int devno = dev->link->pmp + dev->devno;
466 int alt_devno = devno;
467 int i;
469 /* allow n.15/16 for devices attached to host port */
470 if (ata_is_host_link(dev->link))
471 alt_devno += 15;
473 for (i = 0; i < ata_force_tbl_size; i++) {
474 const struct ata_force_ent *fe = &ata_force_tbl[i];
476 if (fe->port != -1 && fe->port != dev->link->ap->print_id)
477 continue;
479 if (fe->device != -1 && fe->device != devno &&
480 fe->device != alt_devno)
481 continue;
483 if (!(~dev->horkage & fe->param.horkage_on) &&
484 !(dev->horkage & fe->param.horkage_off))
485 continue;
487 dev->horkage |= fe->param.horkage_on;
488 dev->horkage &= ~fe->param.horkage_off;
490 ata_dev_notice(dev, "FORCE: horkage modified (%s)\n",
491 fe->param.name);
496 * atapi_cmd_type - Determine ATAPI command type from SCSI opcode
497 * @opcode: SCSI opcode
499 * Determine ATAPI command type from @opcode.
501 * LOCKING:
502 * None.
504 * RETURNS:
505 * ATAPI_{READ|WRITE|READ_CD|PASS_THRU|MISC}
507 int atapi_cmd_type(u8 opcode)
509 switch (opcode) {
510 case GPCMD_READ_10:
511 case GPCMD_READ_12:
512 return ATAPI_READ;
514 case GPCMD_WRITE_10:
515 case GPCMD_WRITE_12:
516 case GPCMD_WRITE_AND_VERIFY_10:
517 return ATAPI_WRITE;
519 case GPCMD_READ_CD:
520 case GPCMD_READ_CD_MSF:
521 return ATAPI_READ_CD;
523 case ATA_16:
524 case ATA_12:
525 if (atapi_passthru16)
526 return ATAPI_PASS_THRU;
527 /* fall thru */
528 default:
529 return ATAPI_MISC;
532 EXPORT_SYMBOL_GPL(atapi_cmd_type);
534 static const u8 ata_rw_cmds[] = {
535 /* pio multi */
536 ATA_CMD_READ_MULTI,
537 ATA_CMD_WRITE_MULTI,
538 ATA_CMD_READ_MULTI_EXT,
539 ATA_CMD_WRITE_MULTI_EXT,
543 ATA_CMD_WRITE_MULTI_FUA_EXT,
544 /* pio */
545 ATA_CMD_PIO_READ,
546 ATA_CMD_PIO_WRITE,
547 ATA_CMD_PIO_READ_EXT,
548 ATA_CMD_PIO_WRITE_EXT,
553 /* dma */
554 ATA_CMD_READ,
555 ATA_CMD_WRITE,
556 ATA_CMD_READ_EXT,
557 ATA_CMD_WRITE_EXT,
561 ATA_CMD_WRITE_FUA_EXT
565 * ata_rwcmd_protocol - set taskfile r/w commands and protocol
566 * @tf: command to examine and configure
567 * @dev: device tf belongs to
569 * Examine the device configuration and tf->flags to calculate
570 * the proper read/write commands and protocol to use.
572 * LOCKING:
573 * caller.
575 static int ata_rwcmd_protocol(struct ata_taskfile *tf, struct ata_device *dev)
577 u8 cmd;
579 int index, fua, lba48, write;
581 fua = (tf->flags & ATA_TFLAG_FUA) ? 4 : 0;
582 lba48 = (tf->flags & ATA_TFLAG_LBA48) ? 2 : 0;
583 write = (tf->flags & ATA_TFLAG_WRITE) ? 1 : 0;
585 if (dev->flags & ATA_DFLAG_PIO) {
586 tf->protocol = ATA_PROT_PIO;
587 index = dev->multi_count ? 0 : 8;
588 } else if (lba48 && (dev->link->ap->flags & ATA_FLAG_PIO_LBA48)) {
589 /* Unable to use DMA due to host limitation */
590 tf->protocol = ATA_PROT_PIO;
591 index = dev->multi_count ? 0 : 8;
592 } else {
593 tf->protocol = ATA_PROT_DMA;
594 index = 16;
597 cmd = ata_rw_cmds[index + fua + lba48 + write];
598 if (cmd) {
599 tf->command = cmd;
600 return 0;
602 return -1;
606 * ata_tf_read_block - Read block address from ATA taskfile
607 * @tf: ATA taskfile of interest
608 * @dev: ATA device @tf belongs to
610 * LOCKING:
611 * None.
613 * Read block address from @tf. This function can handle all
614 * three address formats - LBA, LBA48 and CHS. tf->protocol and
615 * flags select the address format to use.
617 * RETURNS:
618 * Block address read from @tf.
620 u64 ata_tf_read_block(const struct ata_taskfile *tf, struct ata_device *dev)
622 u64 block = 0;
624 if (tf->flags & ATA_TFLAG_LBA) {
625 if (tf->flags & ATA_TFLAG_LBA48) {
626 block |= (u64)tf->hob_lbah << 40;
627 block |= (u64)tf->hob_lbam << 32;
628 block |= (u64)tf->hob_lbal << 24;
629 } else
630 block |= (tf->device & 0xf) << 24;
632 block |= tf->lbah << 16;
633 block |= tf->lbam << 8;
634 block |= tf->lbal;
635 } else {
636 u32 cyl, head, sect;
638 cyl = tf->lbam | (tf->lbah << 8);
639 head = tf->device & 0xf;
640 sect = tf->lbal;
642 if (!sect) {
643 ata_dev_warn(dev,
644 "device reported invalid CHS sector 0\n");
645 return U64_MAX;
648 block = (cyl * dev->heads + head) * dev->sectors + sect - 1;
651 return block;
655 * ata_build_rw_tf - Build ATA taskfile for given read/write request
656 * @tf: Target ATA taskfile
657 * @dev: ATA device @tf belongs to
658 * @block: Block address
659 * @n_block: Number of blocks
660 * @tf_flags: RW/FUA etc...
661 * @tag: tag
662 * @class: IO priority class
664 * LOCKING:
665 * None.
667 * Build ATA taskfile @tf for read/write request described by
668 * @block, @n_block, @tf_flags and @tag on @dev.
670 * RETURNS:
672 * 0 on success, -ERANGE if the request is too large for @dev,
673 * -EINVAL if the request is invalid.
675 int ata_build_rw_tf(struct ata_taskfile *tf, struct ata_device *dev,
676 u64 block, u32 n_block, unsigned int tf_flags,
677 unsigned int tag, int class)
679 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
680 tf->flags |= tf_flags;
682 if (ata_ncq_enabled(dev) && !ata_tag_internal(tag)) {
683 /* yay, NCQ */
684 if (!lba_48_ok(block, n_block))
685 return -ERANGE;
687 tf->protocol = ATA_PROT_NCQ;
688 tf->flags |= ATA_TFLAG_LBA | ATA_TFLAG_LBA48;
690 if (tf->flags & ATA_TFLAG_WRITE)
691 tf->command = ATA_CMD_FPDMA_WRITE;
692 else
693 tf->command = ATA_CMD_FPDMA_READ;
695 tf->nsect = tag << 3;
696 tf->hob_feature = (n_block >> 8) & 0xff;
697 tf->feature = n_block & 0xff;
699 tf->hob_lbah = (block >> 40) & 0xff;
700 tf->hob_lbam = (block >> 32) & 0xff;
701 tf->hob_lbal = (block >> 24) & 0xff;
702 tf->lbah = (block >> 16) & 0xff;
703 tf->lbam = (block >> 8) & 0xff;
704 tf->lbal = block & 0xff;
706 tf->device = ATA_LBA;
707 if (tf->flags & ATA_TFLAG_FUA)
708 tf->device |= 1 << 7;
710 if (dev->flags & ATA_DFLAG_NCQ_PRIO) {
711 if (class == IOPRIO_CLASS_RT)
712 tf->hob_nsect |= ATA_PRIO_HIGH <<
713 ATA_SHIFT_PRIO;
715 } else if (dev->flags & ATA_DFLAG_LBA) {
716 tf->flags |= ATA_TFLAG_LBA;
718 if (lba_28_ok(block, n_block)) {
719 /* use LBA28 */
720 tf->device |= (block >> 24) & 0xf;
721 } else if (lba_48_ok(block, n_block)) {
722 if (!(dev->flags & ATA_DFLAG_LBA48))
723 return -ERANGE;
725 /* use LBA48 */
726 tf->flags |= ATA_TFLAG_LBA48;
728 tf->hob_nsect = (n_block >> 8) & 0xff;
730 tf->hob_lbah = (block >> 40) & 0xff;
731 tf->hob_lbam = (block >> 32) & 0xff;
732 tf->hob_lbal = (block >> 24) & 0xff;
733 } else
734 /* request too large even for LBA48 */
735 return -ERANGE;
737 if (unlikely(ata_rwcmd_protocol(tf, dev) < 0))
738 return -EINVAL;
740 tf->nsect = n_block & 0xff;
742 tf->lbah = (block >> 16) & 0xff;
743 tf->lbam = (block >> 8) & 0xff;
744 tf->lbal = block & 0xff;
746 tf->device |= ATA_LBA;
747 } else {
748 /* CHS */
749 u32 sect, head, cyl, track;
751 /* The request -may- be too large for CHS addressing. */
752 if (!lba_28_ok(block, n_block))
753 return -ERANGE;
755 if (unlikely(ata_rwcmd_protocol(tf, dev) < 0))
756 return -EINVAL;
758 /* Convert LBA to CHS */
759 track = (u32)block / dev->sectors;
760 cyl = track / dev->heads;
761 head = track % dev->heads;
762 sect = (u32)block % dev->sectors + 1;
764 DPRINTK("block %u track %u cyl %u head %u sect %u\n",
765 (u32)block, track, cyl, head, sect);
767 /* Check whether the converted CHS can fit.
768 Cylinder: 0-65535
769 Head: 0-15
770 Sector: 1-255*/
771 if ((cyl >> 16) || (head >> 4) || (sect >> 8) || (!sect))
772 return -ERANGE;
774 tf->nsect = n_block & 0xff; /* Sector count 0 means 256 sectors */
775 tf->lbal = sect;
776 tf->lbam = cyl;
777 tf->lbah = cyl >> 8;
778 tf->device |= head;
781 return 0;
785 * ata_pack_xfermask - Pack pio, mwdma and udma masks into xfer_mask
786 * @pio_mask: pio_mask
787 * @mwdma_mask: mwdma_mask
788 * @udma_mask: udma_mask
790 * Pack @pio_mask, @mwdma_mask and @udma_mask into a single
791 * unsigned int xfer_mask.
793 * LOCKING:
794 * None.
796 * RETURNS:
797 * Packed xfer_mask.
799 unsigned long ata_pack_xfermask(unsigned long pio_mask,
800 unsigned long mwdma_mask,
801 unsigned long udma_mask)
803 return ((pio_mask << ATA_SHIFT_PIO) & ATA_MASK_PIO) |
804 ((mwdma_mask << ATA_SHIFT_MWDMA) & ATA_MASK_MWDMA) |
805 ((udma_mask << ATA_SHIFT_UDMA) & ATA_MASK_UDMA);
807 EXPORT_SYMBOL_GPL(ata_pack_xfermask);
810 * ata_unpack_xfermask - Unpack xfer_mask into pio, mwdma and udma masks
811 * @xfer_mask: xfer_mask to unpack
812 * @pio_mask: resulting pio_mask
813 * @mwdma_mask: resulting mwdma_mask
814 * @udma_mask: resulting udma_mask
816 * Unpack @xfer_mask into @pio_mask, @mwdma_mask and @udma_mask.
817 * Any NULL destination masks will be ignored.
819 void ata_unpack_xfermask(unsigned long xfer_mask, unsigned long *pio_mask,
820 unsigned long *mwdma_mask, unsigned long *udma_mask)
822 if (pio_mask)
823 *pio_mask = (xfer_mask & ATA_MASK_PIO) >> ATA_SHIFT_PIO;
824 if (mwdma_mask)
825 *mwdma_mask = (xfer_mask & ATA_MASK_MWDMA) >> ATA_SHIFT_MWDMA;
826 if (udma_mask)
827 *udma_mask = (xfer_mask & ATA_MASK_UDMA) >> ATA_SHIFT_UDMA;
830 static const struct ata_xfer_ent {
831 int shift, bits;
832 u8 base;
833 } ata_xfer_tbl[] = {
834 { ATA_SHIFT_PIO, ATA_NR_PIO_MODES, XFER_PIO_0 },
835 { ATA_SHIFT_MWDMA, ATA_NR_MWDMA_MODES, XFER_MW_DMA_0 },
836 { ATA_SHIFT_UDMA, ATA_NR_UDMA_MODES, XFER_UDMA_0 },
837 { -1, },
841 * ata_xfer_mask2mode - Find matching XFER_* for the given xfer_mask
842 * @xfer_mask: xfer_mask of interest
844 * Return matching XFER_* value for @xfer_mask. Only the highest
845 * bit of @xfer_mask is considered.
847 * LOCKING:
848 * None.
850 * RETURNS:
851 * Matching XFER_* value, 0xff if no match found.
853 u8 ata_xfer_mask2mode(unsigned long xfer_mask)
855 int highbit = fls(xfer_mask) - 1;
856 const struct ata_xfer_ent *ent;
858 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
859 if (highbit >= ent->shift && highbit < ent->shift + ent->bits)
860 return ent->base + highbit - ent->shift;
861 return 0xff;
863 EXPORT_SYMBOL_GPL(ata_xfer_mask2mode);
866 * ata_xfer_mode2mask - Find matching xfer_mask for XFER_*
867 * @xfer_mode: XFER_* of interest
869 * Return matching xfer_mask for @xfer_mode.
871 * LOCKING:
872 * None.
874 * RETURNS:
875 * Matching xfer_mask, 0 if no match found.
877 unsigned long ata_xfer_mode2mask(u8 xfer_mode)
879 const struct ata_xfer_ent *ent;
881 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
882 if (xfer_mode >= ent->base && xfer_mode < ent->base + ent->bits)
883 return ((2 << (ent->shift + xfer_mode - ent->base)) - 1)
884 & ~((1 << ent->shift) - 1);
885 return 0;
887 EXPORT_SYMBOL_GPL(ata_xfer_mode2mask);
890 * ata_xfer_mode2shift - Find matching xfer_shift for XFER_*
891 * @xfer_mode: XFER_* of interest
893 * Return matching xfer_shift for @xfer_mode.
895 * LOCKING:
896 * None.
898 * RETURNS:
899 * Matching xfer_shift, -1 if no match found.
901 int ata_xfer_mode2shift(unsigned long xfer_mode)
903 const struct ata_xfer_ent *ent;
905 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
906 if (xfer_mode >= ent->base && xfer_mode < ent->base + ent->bits)
907 return ent->shift;
908 return -1;
910 EXPORT_SYMBOL_GPL(ata_xfer_mode2shift);
913 * ata_mode_string - convert xfer_mask to string
914 * @xfer_mask: mask of bits supported; only highest bit counts.
916 * Determine string which represents the highest speed
917 * (highest bit in @modemask).
919 * LOCKING:
920 * None.
922 * RETURNS:
923 * Constant C string representing highest speed listed in
924 * @mode_mask, or the constant C string "<n/a>".
926 const char *ata_mode_string(unsigned long xfer_mask)
928 static const char * const xfer_mode_str[] = {
929 "PIO0",
930 "PIO1",
931 "PIO2",
932 "PIO3",
933 "PIO4",
934 "PIO5",
935 "PIO6",
936 "MWDMA0",
937 "MWDMA1",
938 "MWDMA2",
939 "MWDMA3",
940 "MWDMA4",
941 "UDMA/16",
942 "UDMA/25",
943 "UDMA/33",
944 "UDMA/44",
945 "UDMA/66",
946 "UDMA/100",
947 "UDMA/133",
948 "UDMA7",
950 int highbit;
952 highbit = fls(xfer_mask) - 1;
953 if (highbit >= 0 && highbit < ARRAY_SIZE(xfer_mode_str))
954 return xfer_mode_str[highbit];
955 return "<n/a>";
957 EXPORT_SYMBOL_GPL(ata_mode_string);
959 const char *sata_spd_string(unsigned int spd)
961 static const char * const spd_str[] = {
962 "1.5 Gbps",
963 "3.0 Gbps",
964 "6.0 Gbps",
967 if (spd == 0 || (spd - 1) >= ARRAY_SIZE(spd_str))
968 return "<unknown>";
969 return spd_str[spd - 1];
973 * ata_dev_classify - determine device type based on ATA-spec signature
974 * @tf: ATA taskfile register set for device to be identified
976 * Determine from taskfile register contents whether a device is
977 * ATA or ATAPI, as per "Signature and persistence" section
978 * of ATA/PI spec (volume 1, sect 5.14).
980 * LOCKING:
981 * None.
983 * RETURNS:
984 * Device type, %ATA_DEV_ATA, %ATA_DEV_ATAPI, %ATA_DEV_PMP,
985 * %ATA_DEV_ZAC, or %ATA_DEV_UNKNOWN the event of failure.
987 unsigned int ata_dev_classify(const struct ata_taskfile *tf)
989 /* Apple's open source Darwin code hints that some devices only
990 * put a proper signature into the LBA mid/high registers,
991 * So, we only check those. It's sufficient for uniqueness.
993 * ATA/ATAPI-7 (d1532v1r1: Feb. 19, 2003) specified separate
994 * signatures for ATA and ATAPI devices attached on SerialATA,
995 * 0x3c/0xc3 and 0x69/0x96 respectively. However, SerialATA
996 * spec has never mentioned about using different signatures
997 * for ATA/ATAPI devices. Then, Serial ATA II: Port
998 * Multiplier specification began to use 0x69/0x96 to identify
999 * port multpliers and 0x3c/0xc3 to identify SEMB device.
1000 * ATA/ATAPI-7 dropped descriptions about 0x3c/0xc3 and
1001 * 0x69/0x96 shortly and described them as reserved for
1002 * SerialATA.
1004 * We follow the current spec and consider that 0x69/0x96
1005 * identifies a port multiplier and 0x3c/0xc3 a SEMB device.
1006 * Unfortunately, WDC WD1600JS-62MHB5 (a hard drive) reports
1007 * SEMB signature. This is worked around in
1008 * ata_dev_read_id().
1010 if ((tf->lbam == 0) && (tf->lbah == 0)) {
1011 DPRINTK("found ATA device by sig\n");
1012 return ATA_DEV_ATA;
1015 if ((tf->lbam == 0x14) && (tf->lbah == 0xeb)) {
1016 DPRINTK("found ATAPI device by sig\n");
1017 return ATA_DEV_ATAPI;
1020 if ((tf->lbam == 0x69) && (tf->lbah == 0x96)) {
1021 DPRINTK("found PMP device by sig\n");
1022 return ATA_DEV_PMP;
1025 if ((tf->lbam == 0x3c) && (tf->lbah == 0xc3)) {
1026 DPRINTK("found SEMB device by sig (could be ATA device)\n");
1027 return ATA_DEV_SEMB;
1030 if ((tf->lbam == 0xcd) && (tf->lbah == 0xab)) {
1031 DPRINTK("found ZAC device by sig\n");
1032 return ATA_DEV_ZAC;
1035 DPRINTK("unknown device\n");
1036 return ATA_DEV_UNKNOWN;
1038 EXPORT_SYMBOL_GPL(ata_dev_classify);
1041 * ata_id_string - Convert IDENTIFY DEVICE page into string
1042 * @id: IDENTIFY DEVICE results we will examine
1043 * @s: string into which data is output
1044 * @ofs: offset into identify device page
1045 * @len: length of string to return. must be an even number.
1047 * The strings in the IDENTIFY DEVICE page are broken up into
1048 * 16-bit chunks. Run through the string, and output each
1049 * 8-bit chunk linearly, regardless of platform.
1051 * LOCKING:
1052 * caller.
1055 void ata_id_string(const u16 *id, unsigned char *s,
1056 unsigned int ofs, unsigned int len)
1058 unsigned int c;
1060 BUG_ON(len & 1);
1062 while (len > 0) {
1063 c = id[ofs] >> 8;
1064 *s = c;
1065 s++;
1067 c = id[ofs] & 0xff;
1068 *s = c;
1069 s++;
1071 ofs++;
1072 len -= 2;
1075 EXPORT_SYMBOL_GPL(ata_id_string);
1078 * ata_id_c_string - Convert IDENTIFY DEVICE page into C string
1079 * @id: IDENTIFY DEVICE results we will examine
1080 * @s: string into which data is output
1081 * @ofs: offset into identify device page
1082 * @len: length of string to return. must be an odd number.
1084 * This function is identical to ata_id_string except that it
1085 * trims trailing spaces and terminates the resulting string with
1086 * null. @len must be actual maximum length (even number) + 1.
1088 * LOCKING:
1089 * caller.
1091 void ata_id_c_string(const u16 *id, unsigned char *s,
1092 unsigned int ofs, unsigned int len)
1094 unsigned char *p;
1096 ata_id_string(id, s, ofs, len - 1);
1098 p = s + strnlen(s, len - 1);
1099 while (p > s && p[-1] == ' ')
1100 p--;
1101 *p = '\0';
1103 EXPORT_SYMBOL_GPL(ata_id_c_string);
1105 static u64 ata_id_n_sectors(const u16 *id)
1107 if (ata_id_has_lba(id)) {
1108 if (ata_id_has_lba48(id))
1109 return ata_id_u64(id, ATA_ID_LBA_CAPACITY_2);
1110 else
1111 return ata_id_u32(id, ATA_ID_LBA_CAPACITY);
1112 } else {
1113 if (ata_id_current_chs_valid(id))
1114 return id[ATA_ID_CUR_CYLS] * id[ATA_ID_CUR_HEADS] *
1115 id[ATA_ID_CUR_SECTORS];
1116 else
1117 return id[ATA_ID_CYLS] * id[ATA_ID_HEADS] *
1118 id[ATA_ID_SECTORS];
1122 u64 ata_tf_to_lba48(const struct ata_taskfile *tf)
1124 u64 sectors = 0;
1126 sectors |= ((u64)(tf->hob_lbah & 0xff)) << 40;
1127 sectors |= ((u64)(tf->hob_lbam & 0xff)) << 32;
1128 sectors |= ((u64)(tf->hob_lbal & 0xff)) << 24;
1129 sectors |= (tf->lbah & 0xff) << 16;
1130 sectors |= (tf->lbam & 0xff) << 8;
1131 sectors |= (tf->lbal & 0xff);
1133 return sectors;
1136 u64 ata_tf_to_lba(const struct ata_taskfile *tf)
1138 u64 sectors = 0;
1140 sectors |= (tf->device & 0x0f) << 24;
1141 sectors |= (tf->lbah & 0xff) << 16;
1142 sectors |= (tf->lbam & 0xff) << 8;
1143 sectors |= (tf->lbal & 0xff);
1145 return sectors;
1149 * ata_read_native_max_address - Read native max address
1150 * @dev: target device
1151 * @max_sectors: out parameter for the result native max address
1153 * Perform an LBA48 or LBA28 native size query upon the device in
1154 * question.
1156 * RETURNS:
1157 * 0 on success, -EACCES if command is aborted by the drive.
1158 * -EIO on other errors.
1160 static int ata_read_native_max_address(struct ata_device *dev, u64 *max_sectors)
1162 unsigned int err_mask;
1163 struct ata_taskfile tf;
1164 int lba48 = ata_id_has_lba48(dev->id);
1166 ata_tf_init(dev, &tf);
1168 /* always clear all address registers */
1169 tf.flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
1171 if (lba48) {
1172 tf.command = ATA_CMD_READ_NATIVE_MAX_EXT;
1173 tf.flags |= ATA_TFLAG_LBA48;
1174 } else
1175 tf.command = ATA_CMD_READ_NATIVE_MAX;
1177 tf.protocol = ATA_PROT_NODATA;
1178 tf.device |= ATA_LBA;
1180 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
1181 if (err_mask) {
1182 ata_dev_warn(dev,
1183 "failed to read native max address (err_mask=0x%x)\n",
1184 err_mask);
1185 if (err_mask == AC_ERR_DEV && (tf.feature & ATA_ABORTED))
1186 return -EACCES;
1187 return -EIO;
1190 if (lba48)
1191 *max_sectors = ata_tf_to_lba48(&tf) + 1;
1192 else
1193 *max_sectors = ata_tf_to_lba(&tf) + 1;
1194 if (dev->horkage & ATA_HORKAGE_HPA_SIZE)
1195 (*max_sectors)--;
1196 return 0;
1200 * ata_set_max_sectors - Set max sectors
1201 * @dev: target device
1202 * @new_sectors: new max sectors value to set for the device
1204 * Set max sectors of @dev to @new_sectors.
1206 * RETURNS:
1207 * 0 on success, -EACCES if command is aborted or denied (due to
1208 * previous non-volatile SET_MAX) by the drive. -EIO on other
1209 * errors.
1211 static int ata_set_max_sectors(struct ata_device *dev, u64 new_sectors)
1213 unsigned int err_mask;
1214 struct ata_taskfile tf;
1215 int lba48 = ata_id_has_lba48(dev->id);
1217 new_sectors--;
1219 ata_tf_init(dev, &tf);
1221 tf.flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
1223 if (lba48) {
1224 tf.command = ATA_CMD_SET_MAX_EXT;
1225 tf.flags |= ATA_TFLAG_LBA48;
1227 tf.hob_lbal = (new_sectors >> 24) & 0xff;
1228 tf.hob_lbam = (new_sectors >> 32) & 0xff;
1229 tf.hob_lbah = (new_sectors >> 40) & 0xff;
1230 } else {
1231 tf.command = ATA_CMD_SET_MAX;
1233 tf.device |= (new_sectors >> 24) & 0xf;
1236 tf.protocol = ATA_PROT_NODATA;
1237 tf.device |= ATA_LBA;
1239 tf.lbal = (new_sectors >> 0) & 0xff;
1240 tf.lbam = (new_sectors >> 8) & 0xff;
1241 tf.lbah = (new_sectors >> 16) & 0xff;
1243 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
1244 if (err_mask) {
1245 ata_dev_warn(dev,
1246 "failed to set max address (err_mask=0x%x)\n",
1247 err_mask);
1248 if (err_mask == AC_ERR_DEV &&
1249 (tf.feature & (ATA_ABORTED | ATA_IDNF)))
1250 return -EACCES;
1251 return -EIO;
1254 return 0;
1258 * ata_hpa_resize - Resize a device with an HPA set
1259 * @dev: Device to resize
1261 * Read the size of an LBA28 or LBA48 disk with HPA features and resize
1262 * it if required to the full size of the media. The caller must check
1263 * the drive has the HPA feature set enabled.
1265 * RETURNS:
1266 * 0 on success, -errno on failure.
1268 static int ata_hpa_resize(struct ata_device *dev)
1270 struct ata_eh_context *ehc = &dev->link->eh_context;
1271 int print_info = ehc->i.flags & ATA_EHI_PRINTINFO;
1272 bool unlock_hpa = ata_ignore_hpa || dev->flags & ATA_DFLAG_UNLOCK_HPA;
1273 u64 sectors = ata_id_n_sectors(dev->id);
1274 u64 native_sectors;
1275 int rc;
1277 /* do we need to do it? */
1278 if ((dev->class != ATA_DEV_ATA && dev->class != ATA_DEV_ZAC) ||
1279 !ata_id_has_lba(dev->id) || !ata_id_hpa_enabled(dev->id) ||
1280 (dev->horkage & ATA_HORKAGE_BROKEN_HPA))
1281 return 0;
1283 /* read native max address */
1284 rc = ata_read_native_max_address(dev, &native_sectors);
1285 if (rc) {
1286 /* If device aborted the command or HPA isn't going to
1287 * be unlocked, skip HPA resizing.
1289 if (rc == -EACCES || !unlock_hpa) {
1290 ata_dev_warn(dev,
1291 "HPA support seems broken, skipping HPA handling\n");
1292 dev->horkage |= ATA_HORKAGE_BROKEN_HPA;
1294 /* we can continue if device aborted the command */
1295 if (rc == -EACCES)
1296 rc = 0;
1299 return rc;
1301 dev->n_native_sectors = native_sectors;
1303 /* nothing to do? */
1304 if (native_sectors <= sectors || !unlock_hpa) {
1305 if (!print_info || native_sectors == sectors)
1306 return 0;
1308 if (native_sectors > sectors)
1309 ata_dev_info(dev,
1310 "HPA detected: current %llu, native %llu\n",
1311 (unsigned long long)sectors,
1312 (unsigned long long)native_sectors);
1313 else if (native_sectors < sectors)
1314 ata_dev_warn(dev,
1315 "native sectors (%llu) is smaller than sectors (%llu)\n",
1316 (unsigned long long)native_sectors,
1317 (unsigned long long)sectors);
1318 return 0;
1321 /* let's unlock HPA */
1322 rc = ata_set_max_sectors(dev, native_sectors);
1323 if (rc == -EACCES) {
1324 /* if device aborted the command, skip HPA resizing */
1325 ata_dev_warn(dev,
1326 "device aborted resize (%llu -> %llu), skipping HPA handling\n",
1327 (unsigned long long)sectors,
1328 (unsigned long long)native_sectors);
1329 dev->horkage |= ATA_HORKAGE_BROKEN_HPA;
1330 return 0;
1331 } else if (rc)
1332 return rc;
1334 /* re-read IDENTIFY data */
1335 rc = ata_dev_reread_id(dev, 0);
1336 if (rc) {
1337 ata_dev_err(dev,
1338 "failed to re-read IDENTIFY data after HPA resizing\n");
1339 return rc;
1342 if (print_info) {
1343 u64 new_sectors = ata_id_n_sectors(dev->id);
1344 ata_dev_info(dev,
1345 "HPA unlocked: %llu -> %llu, native %llu\n",
1346 (unsigned long long)sectors,
1347 (unsigned long long)new_sectors,
1348 (unsigned long long)native_sectors);
1351 return 0;
1355 * ata_dump_id - IDENTIFY DEVICE info debugging output
1356 * @id: IDENTIFY DEVICE page to dump
1358 * Dump selected 16-bit words from the given IDENTIFY DEVICE
1359 * page.
1361 * LOCKING:
1362 * caller.
1365 static inline void ata_dump_id(const u16 *id)
1367 DPRINTK("49==0x%04x "
1368 "53==0x%04x "
1369 "63==0x%04x "
1370 "64==0x%04x "
1371 "75==0x%04x \n",
1372 id[49],
1373 id[53],
1374 id[63],
1375 id[64],
1376 id[75]);
1377 DPRINTK("80==0x%04x "
1378 "81==0x%04x "
1379 "82==0x%04x "
1380 "83==0x%04x "
1381 "84==0x%04x \n",
1382 id[80],
1383 id[81],
1384 id[82],
1385 id[83],
1386 id[84]);
1387 DPRINTK("88==0x%04x "
1388 "93==0x%04x\n",
1389 id[88],
1390 id[93]);
1394 * ata_id_xfermask - Compute xfermask from the given IDENTIFY data
1395 * @id: IDENTIFY data to compute xfer mask from
1397 * Compute the xfermask for this device. This is not as trivial
1398 * as it seems if we must consider early devices correctly.
1400 * FIXME: pre IDE drive timing (do we care ?).
1402 * LOCKING:
1403 * None.
1405 * RETURNS:
1406 * Computed xfermask
1408 unsigned long ata_id_xfermask(const u16 *id)
1410 unsigned long pio_mask, mwdma_mask, udma_mask;
1412 /* Usual case. Word 53 indicates word 64 is valid */
1413 if (id[ATA_ID_FIELD_VALID] & (1 << 1)) {
1414 pio_mask = id[ATA_ID_PIO_MODES] & 0x03;
1415 pio_mask <<= 3;
1416 pio_mask |= 0x7;
1417 } else {
1418 /* If word 64 isn't valid then Word 51 high byte holds
1419 * the PIO timing number for the maximum. Turn it into
1420 * a mask.
1422 u8 mode = (id[ATA_ID_OLD_PIO_MODES] >> 8) & 0xFF;
1423 if (mode < 5) /* Valid PIO range */
1424 pio_mask = (2 << mode) - 1;
1425 else
1426 pio_mask = 1;
1428 /* But wait.. there's more. Design your standards by
1429 * committee and you too can get a free iordy field to
1430 * process. However its the speeds not the modes that
1431 * are supported... Note drivers using the timing API
1432 * will get this right anyway
1436 mwdma_mask = id[ATA_ID_MWDMA_MODES] & 0x07;
1438 if (ata_id_is_cfa(id)) {
1440 * Process compact flash extended modes
1442 int pio = (id[ATA_ID_CFA_MODES] >> 0) & 0x7;
1443 int dma = (id[ATA_ID_CFA_MODES] >> 3) & 0x7;
1445 if (pio)
1446 pio_mask |= (1 << 5);
1447 if (pio > 1)
1448 pio_mask |= (1 << 6);
1449 if (dma)
1450 mwdma_mask |= (1 << 3);
1451 if (dma > 1)
1452 mwdma_mask |= (1 << 4);
1455 udma_mask = 0;
1456 if (id[ATA_ID_FIELD_VALID] & (1 << 2))
1457 udma_mask = id[ATA_ID_UDMA_MODES] & 0xff;
1459 return ata_pack_xfermask(pio_mask, mwdma_mask, udma_mask);
1461 EXPORT_SYMBOL_GPL(ata_id_xfermask);
1463 static void ata_qc_complete_internal(struct ata_queued_cmd *qc)
1465 struct completion *waiting = qc->private_data;
1467 complete(waiting);
1471 * ata_exec_internal_sg - execute libata internal command
1472 * @dev: Device to which the command is sent
1473 * @tf: Taskfile registers for the command and the result
1474 * @cdb: CDB for packet command
1475 * @dma_dir: Data transfer direction of the command
1476 * @sgl: sg list for the data buffer of the command
1477 * @n_elem: Number of sg entries
1478 * @timeout: Timeout in msecs (0 for default)
1480 * Executes libata internal command with timeout. @tf contains
1481 * command on entry and result on return. Timeout and error
1482 * conditions are reported via return value. No recovery action
1483 * is taken after a command times out. It's caller's duty to
1484 * clean up after timeout.
1486 * LOCKING:
1487 * None. Should be called with kernel context, might sleep.
1489 * RETURNS:
1490 * Zero on success, AC_ERR_* mask on failure
1492 unsigned ata_exec_internal_sg(struct ata_device *dev,
1493 struct ata_taskfile *tf, const u8 *cdb,
1494 int dma_dir, struct scatterlist *sgl,
1495 unsigned int n_elem, unsigned long timeout)
1497 struct ata_link *link = dev->link;
1498 struct ata_port *ap = link->ap;
1499 u8 command = tf->command;
1500 int auto_timeout = 0;
1501 struct ata_queued_cmd *qc;
1502 unsigned int preempted_tag;
1503 u32 preempted_sactive;
1504 u64 preempted_qc_active;
1505 int preempted_nr_active_links;
1506 DECLARE_COMPLETION_ONSTACK(wait);
1507 unsigned long flags;
1508 unsigned int err_mask;
1509 int rc;
1511 spin_lock_irqsave(ap->lock, flags);
1513 /* no internal command while frozen */
1514 if (ap->pflags & ATA_PFLAG_FROZEN) {
1515 spin_unlock_irqrestore(ap->lock, flags);
1516 return AC_ERR_SYSTEM;
1519 /* initialize internal qc */
1520 qc = __ata_qc_from_tag(ap, ATA_TAG_INTERNAL);
1522 qc->tag = ATA_TAG_INTERNAL;
1523 qc->hw_tag = 0;
1524 qc->scsicmd = NULL;
1525 qc->ap = ap;
1526 qc->dev = dev;
1527 ata_qc_reinit(qc);
1529 preempted_tag = link->active_tag;
1530 preempted_sactive = link->sactive;
1531 preempted_qc_active = ap->qc_active;
1532 preempted_nr_active_links = ap->nr_active_links;
1533 link->active_tag = ATA_TAG_POISON;
1534 link->sactive = 0;
1535 ap->qc_active = 0;
1536 ap->nr_active_links = 0;
1538 /* prepare & issue qc */
1539 qc->tf = *tf;
1540 if (cdb)
1541 memcpy(qc->cdb, cdb, ATAPI_CDB_LEN);
1543 /* some SATA bridges need us to indicate data xfer direction */
1544 if (tf->protocol == ATAPI_PROT_DMA && (dev->flags & ATA_DFLAG_DMADIR) &&
1545 dma_dir == DMA_FROM_DEVICE)
1546 qc->tf.feature |= ATAPI_DMADIR;
1548 qc->flags |= ATA_QCFLAG_RESULT_TF;
1549 qc->dma_dir = dma_dir;
1550 if (dma_dir != DMA_NONE) {
1551 unsigned int i, buflen = 0;
1552 struct scatterlist *sg;
1554 for_each_sg(sgl, sg, n_elem, i)
1555 buflen += sg->length;
1557 ata_sg_init(qc, sgl, n_elem);
1558 qc->nbytes = buflen;
1561 qc->private_data = &wait;
1562 qc->complete_fn = ata_qc_complete_internal;
1564 ata_qc_issue(qc);
1566 spin_unlock_irqrestore(ap->lock, flags);
1568 if (!timeout) {
1569 if (ata_probe_timeout)
1570 timeout = ata_probe_timeout * 1000;
1571 else {
1572 timeout = ata_internal_cmd_timeout(dev, command);
1573 auto_timeout = 1;
1577 if (ap->ops->error_handler)
1578 ata_eh_release(ap);
1580 rc = wait_for_completion_timeout(&wait, msecs_to_jiffies(timeout));
1582 if (ap->ops->error_handler)
1583 ata_eh_acquire(ap);
1585 ata_sff_flush_pio_task(ap);
1587 if (!rc) {
1588 spin_lock_irqsave(ap->lock, flags);
1590 /* We're racing with irq here. If we lose, the
1591 * following test prevents us from completing the qc
1592 * twice. If we win, the port is frozen and will be
1593 * cleaned up by ->post_internal_cmd().
1595 if (qc->flags & ATA_QCFLAG_ACTIVE) {
1596 qc->err_mask |= AC_ERR_TIMEOUT;
1598 if (ap->ops->error_handler)
1599 ata_port_freeze(ap);
1600 else
1601 ata_qc_complete(qc);
1603 if (ata_msg_warn(ap))
1604 ata_dev_warn(dev, "qc timeout (cmd 0x%x)\n",
1605 command);
1608 spin_unlock_irqrestore(ap->lock, flags);
1611 /* do post_internal_cmd */
1612 if (ap->ops->post_internal_cmd)
1613 ap->ops->post_internal_cmd(qc);
1615 /* perform minimal error analysis */
1616 if (qc->flags & ATA_QCFLAG_FAILED) {
1617 if (qc->result_tf.command & (ATA_ERR | ATA_DF))
1618 qc->err_mask |= AC_ERR_DEV;
1620 if (!qc->err_mask)
1621 qc->err_mask |= AC_ERR_OTHER;
1623 if (qc->err_mask & ~AC_ERR_OTHER)
1624 qc->err_mask &= ~AC_ERR_OTHER;
1625 } else if (qc->tf.command == ATA_CMD_REQ_SENSE_DATA) {
1626 qc->result_tf.command |= ATA_SENSE;
1629 /* finish up */
1630 spin_lock_irqsave(ap->lock, flags);
1632 *tf = qc->result_tf;
1633 err_mask = qc->err_mask;
1635 ata_qc_free(qc);
1636 link->active_tag = preempted_tag;
1637 link->sactive = preempted_sactive;
1638 ap->qc_active = preempted_qc_active;
1639 ap->nr_active_links = preempted_nr_active_links;
1641 spin_unlock_irqrestore(ap->lock, flags);
1643 if ((err_mask & AC_ERR_TIMEOUT) && auto_timeout)
1644 ata_internal_cmd_timed_out(dev, command);
1646 return err_mask;
1650 * ata_exec_internal - execute libata internal command
1651 * @dev: Device to which the command is sent
1652 * @tf: Taskfile registers for the command and the result
1653 * @cdb: CDB for packet command
1654 * @dma_dir: Data transfer direction of the command
1655 * @buf: Data buffer of the command
1656 * @buflen: Length of data buffer
1657 * @timeout: Timeout in msecs (0 for default)
1659 * Wrapper around ata_exec_internal_sg() which takes simple
1660 * buffer instead of sg list.
1662 * LOCKING:
1663 * None. Should be called with kernel context, might sleep.
1665 * RETURNS:
1666 * Zero on success, AC_ERR_* mask on failure
1668 unsigned ata_exec_internal(struct ata_device *dev,
1669 struct ata_taskfile *tf, const u8 *cdb,
1670 int dma_dir, void *buf, unsigned int buflen,
1671 unsigned long timeout)
1673 struct scatterlist *psg = NULL, sg;
1674 unsigned int n_elem = 0;
1676 if (dma_dir != DMA_NONE) {
1677 WARN_ON(!buf);
1678 sg_init_one(&sg, buf, buflen);
1679 psg = &sg;
1680 n_elem++;
1683 return ata_exec_internal_sg(dev, tf, cdb, dma_dir, psg, n_elem,
1684 timeout);
1688 * ata_pio_need_iordy - check if iordy needed
1689 * @adev: ATA device
1691 * Check if the current speed of the device requires IORDY. Used
1692 * by various controllers for chip configuration.
1694 unsigned int ata_pio_need_iordy(const struct ata_device *adev)
1696 /* Don't set IORDY if we're preparing for reset. IORDY may
1697 * lead to controller lock up on certain controllers if the
1698 * port is not occupied. See bko#11703 for details.
1700 if (adev->link->ap->pflags & ATA_PFLAG_RESETTING)
1701 return 0;
1702 /* Controller doesn't support IORDY. Probably a pointless
1703 * check as the caller should know this.
1705 if (adev->link->ap->flags & ATA_FLAG_NO_IORDY)
1706 return 0;
1707 /* CF spec. r4.1 Table 22 says no iordy on PIO5 and PIO6. */
1708 if (ata_id_is_cfa(adev->id)
1709 && (adev->pio_mode == XFER_PIO_5 || adev->pio_mode == XFER_PIO_6))
1710 return 0;
1711 /* PIO3 and higher it is mandatory */
1712 if (adev->pio_mode > XFER_PIO_2)
1713 return 1;
1714 /* We turn it on when possible */
1715 if (ata_id_has_iordy(adev->id))
1716 return 1;
1717 return 0;
1719 EXPORT_SYMBOL_GPL(ata_pio_need_iordy);
1722 * ata_pio_mask_no_iordy - Return the non IORDY mask
1723 * @adev: ATA device
1725 * Compute the highest mode possible if we are not using iordy. Return
1726 * -1 if no iordy mode is available.
1728 static u32 ata_pio_mask_no_iordy(const struct ata_device *adev)
1730 /* If we have no drive specific rule, then PIO 2 is non IORDY */
1731 if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE */
1732 u16 pio = adev->id[ATA_ID_EIDE_PIO];
1733 /* Is the speed faster than the drive allows non IORDY ? */
1734 if (pio) {
1735 /* This is cycle times not frequency - watch the logic! */
1736 if (pio > 240) /* PIO2 is 240nS per cycle */
1737 return 3 << ATA_SHIFT_PIO;
1738 return 7 << ATA_SHIFT_PIO;
1741 return 3 << ATA_SHIFT_PIO;
1745 * ata_do_dev_read_id - default ID read method
1746 * @dev: device
1747 * @tf: proposed taskfile
1748 * @id: data buffer
1750 * Issue the identify taskfile and hand back the buffer containing
1751 * identify data. For some RAID controllers and for pre ATA devices
1752 * this function is wrapped or replaced by the driver
1754 unsigned int ata_do_dev_read_id(struct ata_device *dev,
1755 struct ata_taskfile *tf, u16 *id)
1757 return ata_exec_internal(dev, tf, NULL, DMA_FROM_DEVICE,
1758 id, sizeof(id[0]) * ATA_ID_WORDS, 0);
1760 EXPORT_SYMBOL_GPL(ata_do_dev_read_id);
1763 * ata_dev_read_id - Read ID data from the specified device
1764 * @dev: target device
1765 * @p_class: pointer to class of the target device (may be changed)
1766 * @flags: ATA_READID_* flags
1767 * @id: buffer to read IDENTIFY data into
1769 * Read ID data from the specified device. ATA_CMD_ID_ATA is
1770 * performed on ATA devices and ATA_CMD_ID_ATAPI on ATAPI
1771 * devices. This function also issues ATA_CMD_INIT_DEV_PARAMS
1772 * for pre-ATA4 drives.
1774 * FIXME: ATA_CMD_ID_ATA is optional for early drives and right
1775 * now we abort if we hit that case.
1777 * LOCKING:
1778 * Kernel thread context (may sleep)
1780 * RETURNS:
1781 * 0 on success, -errno otherwise.
1783 int ata_dev_read_id(struct ata_device *dev, unsigned int *p_class,
1784 unsigned int flags, u16 *id)
1786 struct ata_port *ap = dev->link->ap;
1787 unsigned int class = *p_class;
1788 struct ata_taskfile tf;
1789 unsigned int err_mask = 0;
1790 const char *reason;
1791 bool is_semb = class == ATA_DEV_SEMB;
1792 int may_fallback = 1, tried_spinup = 0;
1793 int rc;
1795 if (ata_msg_ctl(ap))
1796 ata_dev_dbg(dev, "%s: ENTER\n", __func__);
1798 retry:
1799 ata_tf_init(dev, &tf);
1801 switch (class) {
1802 case ATA_DEV_SEMB:
1803 class = ATA_DEV_ATA; /* some hard drives report SEMB sig */
1804 /* fall through */
1805 case ATA_DEV_ATA:
1806 case ATA_DEV_ZAC:
1807 tf.command = ATA_CMD_ID_ATA;
1808 break;
1809 case ATA_DEV_ATAPI:
1810 tf.command = ATA_CMD_ID_ATAPI;
1811 break;
1812 default:
1813 rc = -ENODEV;
1814 reason = "unsupported class";
1815 goto err_out;
1818 tf.protocol = ATA_PROT_PIO;
1820 /* Some devices choke if TF registers contain garbage. Make
1821 * sure those are properly initialized.
1823 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
1825 /* Device presence detection is unreliable on some
1826 * controllers. Always poll IDENTIFY if available.
1828 tf.flags |= ATA_TFLAG_POLLING;
1830 if (ap->ops->read_id)
1831 err_mask = ap->ops->read_id(dev, &tf, id);
1832 else
1833 err_mask = ata_do_dev_read_id(dev, &tf, id);
1835 if (err_mask) {
1836 if (err_mask & AC_ERR_NODEV_HINT) {
1837 ata_dev_dbg(dev, "NODEV after polling detection\n");
1838 return -ENOENT;
1841 if (is_semb) {
1842 ata_dev_info(dev,
1843 "IDENTIFY failed on device w/ SEMB sig, disabled\n");
1844 /* SEMB is not supported yet */
1845 *p_class = ATA_DEV_SEMB_UNSUP;
1846 return 0;
1849 if ((err_mask == AC_ERR_DEV) && (tf.feature & ATA_ABORTED)) {
1850 /* Device or controller might have reported
1851 * the wrong device class. Give a shot at the
1852 * other IDENTIFY if the current one is
1853 * aborted by the device.
1855 if (may_fallback) {
1856 may_fallback = 0;
1858 if (class == ATA_DEV_ATA)
1859 class = ATA_DEV_ATAPI;
1860 else
1861 class = ATA_DEV_ATA;
1862 goto retry;
1865 /* Control reaches here iff the device aborted
1866 * both flavors of IDENTIFYs which happens
1867 * sometimes with phantom devices.
1869 ata_dev_dbg(dev,
1870 "both IDENTIFYs aborted, assuming NODEV\n");
1871 return -ENOENT;
1874 rc = -EIO;
1875 reason = "I/O error";
1876 goto err_out;
1879 if (dev->horkage & ATA_HORKAGE_DUMP_ID) {
1880 ata_dev_dbg(dev, "dumping IDENTIFY data, "
1881 "class=%d may_fallback=%d tried_spinup=%d\n",
1882 class, may_fallback, tried_spinup);
1883 print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET,
1884 16, 2, id, ATA_ID_WORDS * sizeof(*id), true);
1887 /* Falling back doesn't make sense if ID data was read
1888 * successfully at least once.
1890 may_fallback = 0;
1892 swap_buf_le16(id, ATA_ID_WORDS);
1894 /* sanity check */
1895 rc = -EINVAL;
1896 reason = "device reports invalid type";
1898 if (class == ATA_DEV_ATA || class == ATA_DEV_ZAC) {
1899 if (!ata_id_is_ata(id) && !ata_id_is_cfa(id))
1900 goto err_out;
1901 if (ap->host->flags & ATA_HOST_IGNORE_ATA &&
1902 ata_id_is_ata(id)) {
1903 ata_dev_dbg(dev,
1904 "host indicates ignore ATA devices, ignored\n");
1905 return -ENOENT;
1907 } else {
1908 if (ata_id_is_ata(id))
1909 goto err_out;
1912 if (!tried_spinup && (id[2] == 0x37c8 || id[2] == 0x738c)) {
1913 tried_spinup = 1;
1915 * Drive powered-up in standby mode, and requires a specific
1916 * SET_FEATURES spin-up subcommand before it will accept
1917 * anything other than the original IDENTIFY command.
1919 err_mask = ata_dev_set_feature(dev, SETFEATURES_SPINUP, 0);
1920 if (err_mask && id[2] != 0x738c) {
1921 rc = -EIO;
1922 reason = "SPINUP failed";
1923 goto err_out;
1926 * If the drive initially returned incomplete IDENTIFY info,
1927 * we now must reissue the IDENTIFY command.
1929 if (id[2] == 0x37c8)
1930 goto retry;
1933 if ((flags & ATA_READID_POSTRESET) &&
1934 (class == ATA_DEV_ATA || class == ATA_DEV_ZAC)) {
1936 * The exact sequence expected by certain pre-ATA4 drives is:
1937 * SRST RESET
1938 * IDENTIFY (optional in early ATA)
1939 * INITIALIZE DEVICE PARAMETERS (later IDE and ATA)
1940 * anything else..
1941 * Some drives were very specific about that exact sequence.
1943 * Note that ATA4 says lba is mandatory so the second check
1944 * should never trigger.
1946 if (ata_id_major_version(id) < 4 || !ata_id_has_lba(id)) {
1947 err_mask = ata_dev_init_params(dev, id[3], id[6]);
1948 if (err_mask) {
1949 rc = -EIO;
1950 reason = "INIT_DEV_PARAMS failed";
1951 goto err_out;
1954 /* current CHS translation info (id[53-58]) might be
1955 * changed. reread the identify device info.
1957 flags &= ~ATA_READID_POSTRESET;
1958 goto retry;
1962 *p_class = class;
1964 return 0;
1966 err_out:
1967 if (ata_msg_warn(ap))
1968 ata_dev_warn(dev, "failed to IDENTIFY (%s, err_mask=0x%x)\n",
1969 reason, err_mask);
1970 return rc;
1974 * ata_read_log_page - read a specific log page
1975 * @dev: target device
1976 * @log: log to read
1977 * @page: page to read
1978 * @buf: buffer to store read page
1979 * @sectors: number of sectors to read
1981 * Read log page using READ_LOG_EXT command.
1983 * LOCKING:
1984 * Kernel thread context (may sleep).
1986 * RETURNS:
1987 * 0 on success, AC_ERR_* mask otherwise.
1989 unsigned int ata_read_log_page(struct ata_device *dev, u8 log,
1990 u8 page, void *buf, unsigned int sectors)
1992 unsigned long ap_flags = dev->link->ap->flags;
1993 struct ata_taskfile tf;
1994 unsigned int err_mask;
1995 bool dma = false;
1997 DPRINTK("read log page - log 0x%x, page 0x%x\n", log, page);
2000 * Return error without actually issuing the command on controllers
2001 * which e.g. lockup on a read log page.
2003 if (ap_flags & ATA_FLAG_NO_LOG_PAGE)
2004 return AC_ERR_DEV;
2006 retry:
2007 ata_tf_init(dev, &tf);
2008 if (dev->dma_mode && ata_id_has_read_log_dma_ext(dev->id) &&
2009 !(dev->horkage & ATA_HORKAGE_NO_DMA_LOG)) {
2010 tf.command = ATA_CMD_READ_LOG_DMA_EXT;
2011 tf.protocol = ATA_PROT_DMA;
2012 dma = true;
2013 } else {
2014 tf.command = ATA_CMD_READ_LOG_EXT;
2015 tf.protocol = ATA_PROT_PIO;
2016 dma = false;
2018 tf.lbal = log;
2019 tf.lbam = page;
2020 tf.nsect = sectors;
2021 tf.hob_nsect = sectors >> 8;
2022 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_LBA48 | ATA_TFLAG_DEVICE;
2024 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_FROM_DEVICE,
2025 buf, sectors * ATA_SECT_SIZE, 0);
2027 if (err_mask && dma) {
2028 dev->horkage |= ATA_HORKAGE_NO_DMA_LOG;
2029 ata_dev_warn(dev, "READ LOG DMA EXT failed, trying PIO\n");
2030 goto retry;
2033 DPRINTK("EXIT, err_mask=%x\n", err_mask);
2034 return err_mask;
2037 static bool ata_log_supported(struct ata_device *dev, u8 log)
2039 struct ata_port *ap = dev->link->ap;
2041 if (ata_read_log_page(dev, ATA_LOG_DIRECTORY, 0, ap->sector_buf, 1))
2042 return false;
2043 return get_unaligned_le16(&ap->sector_buf[log * 2]) ? true : false;
2046 static bool ata_identify_page_supported(struct ata_device *dev, u8 page)
2048 struct ata_port *ap = dev->link->ap;
2049 unsigned int err, i;
2051 if (!ata_log_supported(dev, ATA_LOG_IDENTIFY_DEVICE)) {
2052 ata_dev_warn(dev, "ATA Identify Device Log not supported\n");
2053 return false;
2057 * Read IDENTIFY DEVICE data log, page 0, to figure out if the page is
2058 * supported.
2060 err = ata_read_log_page(dev, ATA_LOG_IDENTIFY_DEVICE, 0, ap->sector_buf,
2062 if (err) {
2063 ata_dev_info(dev,
2064 "failed to get Device Identify Log Emask 0x%x\n",
2065 err);
2066 return false;
2069 for (i = 0; i < ap->sector_buf[8]; i++) {
2070 if (ap->sector_buf[9 + i] == page)
2071 return true;
2074 return false;
2077 static int ata_do_link_spd_horkage(struct ata_device *dev)
2079 struct ata_link *plink = ata_dev_phys_link(dev);
2080 u32 target, target_limit;
2082 if (!sata_scr_valid(plink))
2083 return 0;
2085 if (dev->horkage & ATA_HORKAGE_1_5_GBPS)
2086 target = 1;
2087 else
2088 return 0;
2090 target_limit = (1 << target) - 1;
2092 /* if already on stricter limit, no need to push further */
2093 if (plink->sata_spd_limit <= target_limit)
2094 return 0;
2096 plink->sata_spd_limit = target_limit;
2098 /* Request another EH round by returning -EAGAIN if link is
2099 * going faster than the target speed. Forward progress is
2100 * guaranteed by setting sata_spd_limit to target_limit above.
2102 if (plink->sata_spd > target) {
2103 ata_dev_info(dev, "applying link speed limit horkage to %s\n",
2104 sata_spd_string(target));
2105 return -EAGAIN;
2107 return 0;
2110 static inline u8 ata_dev_knobble(struct ata_device *dev)
2112 struct ata_port *ap = dev->link->ap;
2114 if (ata_dev_blacklisted(dev) & ATA_HORKAGE_BRIDGE_OK)
2115 return 0;
2117 return ((ap->cbl == ATA_CBL_SATA) && (!ata_id_is_sata(dev->id)));
2120 static void ata_dev_config_ncq_send_recv(struct ata_device *dev)
2122 struct ata_port *ap = dev->link->ap;
2123 unsigned int err_mask;
2125 if (!ata_log_supported(dev, ATA_LOG_NCQ_SEND_RECV)) {
2126 ata_dev_warn(dev, "NCQ Send/Recv Log not supported\n");
2127 return;
2129 err_mask = ata_read_log_page(dev, ATA_LOG_NCQ_SEND_RECV,
2130 0, ap->sector_buf, 1);
2131 if (err_mask) {
2132 ata_dev_dbg(dev,
2133 "failed to get NCQ Send/Recv Log Emask 0x%x\n",
2134 err_mask);
2135 } else {
2136 u8 *cmds = dev->ncq_send_recv_cmds;
2138 dev->flags |= ATA_DFLAG_NCQ_SEND_RECV;
2139 memcpy(cmds, ap->sector_buf, ATA_LOG_NCQ_SEND_RECV_SIZE);
2141 if (dev->horkage & ATA_HORKAGE_NO_NCQ_TRIM) {
2142 ata_dev_dbg(dev, "disabling queued TRIM support\n");
2143 cmds[ATA_LOG_NCQ_SEND_RECV_DSM_OFFSET] &=
2144 ~ATA_LOG_NCQ_SEND_RECV_DSM_TRIM;
2149 static void ata_dev_config_ncq_non_data(struct ata_device *dev)
2151 struct ata_port *ap = dev->link->ap;
2152 unsigned int err_mask;
2154 if (!ata_log_supported(dev, ATA_LOG_NCQ_NON_DATA)) {
2155 ata_dev_warn(dev,
2156 "NCQ Send/Recv Log not supported\n");
2157 return;
2159 err_mask = ata_read_log_page(dev, ATA_LOG_NCQ_NON_DATA,
2160 0, ap->sector_buf, 1);
2161 if (err_mask) {
2162 ata_dev_dbg(dev,
2163 "failed to get NCQ Non-Data Log Emask 0x%x\n",
2164 err_mask);
2165 } else {
2166 u8 *cmds = dev->ncq_non_data_cmds;
2168 memcpy(cmds, ap->sector_buf, ATA_LOG_NCQ_NON_DATA_SIZE);
2172 static void ata_dev_config_ncq_prio(struct ata_device *dev)
2174 struct ata_port *ap = dev->link->ap;
2175 unsigned int err_mask;
2177 if (!(dev->flags & ATA_DFLAG_NCQ_PRIO_ENABLE)) {
2178 dev->flags &= ~ATA_DFLAG_NCQ_PRIO;
2179 return;
2182 err_mask = ata_read_log_page(dev,
2183 ATA_LOG_IDENTIFY_DEVICE,
2184 ATA_LOG_SATA_SETTINGS,
2185 ap->sector_buf,
2187 if (err_mask) {
2188 ata_dev_dbg(dev,
2189 "failed to get Identify Device data, Emask 0x%x\n",
2190 err_mask);
2191 return;
2194 if (ap->sector_buf[ATA_LOG_NCQ_PRIO_OFFSET] & BIT(3)) {
2195 dev->flags |= ATA_DFLAG_NCQ_PRIO;
2196 } else {
2197 dev->flags &= ~ATA_DFLAG_NCQ_PRIO;
2198 ata_dev_dbg(dev, "SATA page does not support priority\n");
2203 static int ata_dev_config_ncq(struct ata_device *dev,
2204 char *desc, size_t desc_sz)
2206 struct ata_port *ap = dev->link->ap;
2207 int hdepth = 0, ddepth = ata_id_queue_depth(dev->id);
2208 unsigned int err_mask;
2209 char *aa_desc = "";
2211 if (!ata_id_has_ncq(dev->id)) {
2212 desc[0] = '\0';
2213 return 0;
2215 if (!IS_ENABLED(CONFIG_SATA_HOST))
2216 return 0;
2217 if (dev->horkage & ATA_HORKAGE_NONCQ) {
2218 snprintf(desc, desc_sz, "NCQ (not used)");
2219 return 0;
2221 if (ap->flags & ATA_FLAG_NCQ) {
2222 hdepth = min(ap->scsi_host->can_queue, ATA_MAX_QUEUE);
2223 dev->flags |= ATA_DFLAG_NCQ;
2226 if (!(dev->horkage & ATA_HORKAGE_BROKEN_FPDMA_AA) &&
2227 (ap->flags & ATA_FLAG_FPDMA_AA) &&
2228 ata_id_has_fpdma_aa(dev->id)) {
2229 err_mask = ata_dev_set_feature(dev, SETFEATURES_SATA_ENABLE,
2230 SATA_FPDMA_AA);
2231 if (err_mask) {
2232 ata_dev_err(dev,
2233 "failed to enable AA (error_mask=0x%x)\n",
2234 err_mask);
2235 if (err_mask != AC_ERR_DEV) {
2236 dev->horkage |= ATA_HORKAGE_BROKEN_FPDMA_AA;
2237 return -EIO;
2239 } else
2240 aa_desc = ", AA";
2243 if (hdepth >= ddepth)
2244 snprintf(desc, desc_sz, "NCQ (depth %d)%s", ddepth, aa_desc);
2245 else
2246 snprintf(desc, desc_sz, "NCQ (depth %d/%d)%s", hdepth,
2247 ddepth, aa_desc);
2249 if ((ap->flags & ATA_FLAG_FPDMA_AUX)) {
2250 if (ata_id_has_ncq_send_and_recv(dev->id))
2251 ata_dev_config_ncq_send_recv(dev);
2252 if (ata_id_has_ncq_non_data(dev->id))
2253 ata_dev_config_ncq_non_data(dev);
2254 if (ata_id_has_ncq_prio(dev->id))
2255 ata_dev_config_ncq_prio(dev);
2258 return 0;
2261 static void ata_dev_config_sense_reporting(struct ata_device *dev)
2263 unsigned int err_mask;
2265 if (!ata_id_has_sense_reporting(dev->id))
2266 return;
2268 if (ata_id_sense_reporting_enabled(dev->id))
2269 return;
2271 err_mask = ata_dev_set_feature(dev, SETFEATURE_SENSE_DATA, 0x1);
2272 if (err_mask) {
2273 ata_dev_dbg(dev,
2274 "failed to enable Sense Data Reporting, Emask 0x%x\n",
2275 err_mask);
2279 static void ata_dev_config_zac(struct ata_device *dev)
2281 struct ata_port *ap = dev->link->ap;
2282 unsigned int err_mask;
2283 u8 *identify_buf = ap->sector_buf;
2285 dev->zac_zones_optimal_open = U32_MAX;
2286 dev->zac_zones_optimal_nonseq = U32_MAX;
2287 dev->zac_zones_max_open = U32_MAX;
2290 * Always set the 'ZAC' flag for Host-managed devices.
2292 if (dev->class == ATA_DEV_ZAC)
2293 dev->flags |= ATA_DFLAG_ZAC;
2294 else if (ata_id_zoned_cap(dev->id) == 0x01)
2296 * Check for host-aware devices.
2298 dev->flags |= ATA_DFLAG_ZAC;
2300 if (!(dev->flags & ATA_DFLAG_ZAC))
2301 return;
2303 if (!ata_identify_page_supported(dev, ATA_LOG_ZONED_INFORMATION)) {
2304 ata_dev_warn(dev,
2305 "ATA Zoned Information Log not supported\n");
2306 return;
2310 * Read IDENTIFY DEVICE data log, page 9 (Zoned-device information)
2312 err_mask = ata_read_log_page(dev, ATA_LOG_IDENTIFY_DEVICE,
2313 ATA_LOG_ZONED_INFORMATION,
2314 identify_buf, 1);
2315 if (!err_mask) {
2316 u64 zoned_cap, opt_open, opt_nonseq, max_open;
2318 zoned_cap = get_unaligned_le64(&identify_buf[8]);
2319 if ((zoned_cap >> 63))
2320 dev->zac_zoned_cap = (zoned_cap & 1);
2321 opt_open = get_unaligned_le64(&identify_buf[24]);
2322 if ((opt_open >> 63))
2323 dev->zac_zones_optimal_open = (u32)opt_open;
2324 opt_nonseq = get_unaligned_le64(&identify_buf[32]);
2325 if ((opt_nonseq >> 63))
2326 dev->zac_zones_optimal_nonseq = (u32)opt_nonseq;
2327 max_open = get_unaligned_le64(&identify_buf[40]);
2328 if ((max_open >> 63))
2329 dev->zac_zones_max_open = (u32)max_open;
2333 static void ata_dev_config_trusted(struct ata_device *dev)
2335 struct ata_port *ap = dev->link->ap;
2336 u64 trusted_cap;
2337 unsigned int err;
2339 if (!ata_id_has_trusted(dev->id))
2340 return;
2342 if (!ata_identify_page_supported(dev, ATA_LOG_SECURITY)) {
2343 ata_dev_warn(dev,
2344 "Security Log not supported\n");
2345 return;
2348 err = ata_read_log_page(dev, ATA_LOG_IDENTIFY_DEVICE, ATA_LOG_SECURITY,
2349 ap->sector_buf, 1);
2350 if (err) {
2351 ata_dev_dbg(dev,
2352 "failed to read Security Log, Emask 0x%x\n", err);
2353 return;
2356 trusted_cap = get_unaligned_le64(&ap->sector_buf[40]);
2357 if (!(trusted_cap & (1ULL << 63))) {
2358 ata_dev_dbg(dev,
2359 "Trusted Computing capability qword not valid!\n");
2360 return;
2363 if (trusted_cap & (1 << 0))
2364 dev->flags |= ATA_DFLAG_TRUSTED;
2368 * ata_dev_configure - Configure the specified ATA/ATAPI device
2369 * @dev: Target device to configure
2371 * Configure @dev according to @dev->id. Generic and low-level
2372 * driver specific fixups are also applied.
2374 * LOCKING:
2375 * Kernel thread context (may sleep)
2377 * RETURNS:
2378 * 0 on success, -errno otherwise
2380 int ata_dev_configure(struct ata_device *dev)
2382 struct ata_port *ap = dev->link->ap;
2383 struct ata_eh_context *ehc = &dev->link->eh_context;
2384 int print_info = ehc->i.flags & ATA_EHI_PRINTINFO;
2385 const u16 *id = dev->id;
2386 unsigned long xfer_mask;
2387 unsigned int err_mask;
2388 char revbuf[7]; /* XYZ-99\0 */
2389 char fwrevbuf[ATA_ID_FW_REV_LEN+1];
2390 char modelbuf[ATA_ID_PROD_LEN+1];
2391 int rc;
2393 if (!ata_dev_enabled(dev) && ata_msg_info(ap)) {
2394 ata_dev_info(dev, "%s: ENTER/EXIT -- nodev\n", __func__);
2395 return 0;
2398 if (ata_msg_probe(ap))
2399 ata_dev_dbg(dev, "%s: ENTER\n", __func__);
2401 /* set horkage */
2402 dev->horkage |= ata_dev_blacklisted(dev);
2403 ata_force_horkage(dev);
2405 if (dev->horkage & ATA_HORKAGE_DISABLE) {
2406 ata_dev_info(dev, "unsupported device, disabling\n");
2407 ata_dev_disable(dev);
2408 return 0;
2411 if ((!atapi_enabled || (ap->flags & ATA_FLAG_NO_ATAPI)) &&
2412 dev->class == ATA_DEV_ATAPI) {
2413 ata_dev_warn(dev, "WARNING: ATAPI is %s, device ignored\n",
2414 atapi_enabled ? "not supported with this driver"
2415 : "disabled");
2416 ata_dev_disable(dev);
2417 return 0;
2420 rc = ata_do_link_spd_horkage(dev);
2421 if (rc)
2422 return rc;
2424 /* some WD SATA-1 drives have issues with LPM, turn on NOLPM for them */
2425 if ((dev->horkage & ATA_HORKAGE_WD_BROKEN_LPM) &&
2426 (id[ATA_ID_SATA_CAPABILITY] & 0xe) == 0x2)
2427 dev->horkage |= ATA_HORKAGE_NOLPM;
2429 if (ap->flags & ATA_FLAG_NO_LPM)
2430 dev->horkage |= ATA_HORKAGE_NOLPM;
2432 if (dev->horkage & ATA_HORKAGE_NOLPM) {
2433 ata_dev_warn(dev, "LPM support broken, forcing max_power\n");
2434 dev->link->ap->target_lpm_policy = ATA_LPM_MAX_POWER;
2437 /* let ACPI work its magic */
2438 rc = ata_acpi_on_devcfg(dev);
2439 if (rc)
2440 return rc;
2442 /* massage HPA, do it early as it might change IDENTIFY data */
2443 rc = ata_hpa_resize(dev);
2444 if (rc)
2445 return rc;
2447 /* print device capabilities */
2448 if (ata_msg_probe(ap))
2449 ata_dev_dbg(dev,
2450 "%s: cfg 49:%04x 82:%04x 83:%04x 84:%04x "
2451 "85:%04x 86:%04x 87:%04x 88:%04x\n",
2452 __func__,
2453 id[49], id[82], id[83], id[84],
2454 id[85], id[86], id[87], id[88]);
2456 /* initialize to-be-configured parameters */
2457 dev->flags &= ~ATA_DFLAG_CFG_MASK;
2458 dev->max_sectors = 0;
2459 dev->cdb_len = 0;
2460 dev->n_sectors = 0;
2461 dev->cylinders = 0;
2462 dev->heads = 0;
2463 dev->sectors = 0;
2464 dev->multi_count = 0;
2467 * common ATA, ATAPI feature tests
2470 /* find max transfer mode; for printk only */
2471 xfer_mask = ata_id_xfermask(id);
2473 if (ata_msg_probe(ap))
2474 ata_dump_id(id);
2476 /* SCSI only uses 4-char revisions, dump full 8 chars from ATA */
2477 ata_id_c_string(dev->id, fwrevbuf, ATA_ID_FW_REV,
2478 sizeof(fwrevbuf));
2480 ata_id_c_string(dev->id, modelbuf, ATA_ID_PROD,
2481 sizeof(modelbuf));
2483 /* ATA-specific feature tests */
2484 if (dev->class == ATA_DEV_ATA || dev->class == ATA_DEV_ZAC) {
2485 if (ata_id_is_cfa(id)) {
2486 /* CPRM may make this media unusable */
2487 if (id[ATA_ID_CFA_KEY_MGMT] & 1)
2488 ata_dev_warn(dev,
2489 "supports DRM functions and may not be fully accessible\n");
2490 snprintf(revbuf, 7, "CFA");
2491 } else {
2492 snprintf(revbuf, 7, "ATA-%d", ata_id_major_version(id));
2493 /* Warn the user if the device has TPM extensions */
2494 if (ata_id_has_tpm(id))
2495 ata_dev_warn(dev,
2496 "supports DRM functions and may not be fully accessible\n");
2499 dev->n_sectors = ata_id_n_sectors(id);
2501 /* get current R/W Multiple count setting */
2502 if ((dev->id[47] >> 8) == 0x80 && (dev->id[59] & 0x100)) {
2503 unsigned int max = dev->id[47] & 0xff;
2504 unsigned int cnt = dev->id[59] & 0xff;
2505 /* only recognize/allow powers of two here */
2506 if (is_power_of_2(max) && is_power_of_2(cnt))
2507 if (cnt <= max)
2508 dev->multi_count = cnt;
2511 if (ata_id_has_lba(id)) {
2512 const char *lba_desc;
2513 char ncq_desc[24];
2515 lba_desc = "LBA";
2516 dev->flags |= ATA_DFLAG_LBA;
2517 if (ata_id_has_lba48(id)) {
2518 dev->flags |= ATA_DFLAG_LBA48;
2519 lba_desc = "LBA48";
2521 if (dev->n_sectors >= (1UL << 28) &&
2522 ata_id_has_flush_ext(id))
2523 dev->flags |= ATA_DFLAG_FLUSH_EXT;
2526 /* config NCQ */
2527 rc = ata_dev_config_ncq(dev, ncq_desc, sizeof(ncq_desc));
2528 if (rc)
2529 return rc;
2531 /* print device info to dmesg */
2532 if (ata_msg_drv(ap) && print_info) {
2533 ata_dev_info(dev, "%s: %s, %s, max %s\n",
2534 revbuf, modelbuf, fwrevbuf,
2535 ata_mode_string(xfer_mask));
2536 ata_dev_info(dev,
2537 "%llu sectors, multi %u: %s %s\n",
2538 (unsigned long long)dev->n_sectors,
2539 dev->multi_count, lba_desc, ncq_desc);
2541 } else {
2542 /* CHS */
2544 /* Default translation */
2545 dev->cylinders = id[1];
2546 dev->heads = id[3];
2547 dev->sectors = id[6];
2549 if (ata_id_current_chs_valid(id)) {
2550 /* Current CHS translation is valid. */
2551 dev->cylinders = id[54];
2552 dev->heads = id[55];
2553 dev->sectors = id[56];
2556 /* print device info to dmesg */
2557 if (ata_msg_drv(ap) && print_info) {
2558 ata_dev_info(dev, "%s: %s, %s, max %s\n",
2559 revbuf, modelbuf, fwrevbuf,
2560 ata_mode_string(xfer_mask));
2561 ata_dev_info(dev,
2562 "%llu sectors, multi %u, CHS %u/%u/%u\n",
2563 (unsigned long long)dev->n_sectors,
2564 dev->multi_count, dev->cylinders,
2565 dev->heads, dev->sectors);
2569 /* Check and mark DevSlp capability. Get DevSlp timing variables
2570 * from SATA Settings page of Identify Device Data Log.
2572 if (ata_id_has_devslp(dev->id)) {
2573 u8 *sata_setting = ap->sector_buf;
2574 int i, j;
2576 dev->flags |= ATA_DFLAG_DEVSLP;
2577 err_mask = ata_read_log_page(dev,
2578 ATA_LOG_IDENTIFY_DEVICE,
2579 ATA_LOG_SATA_SETTINGS,
2580 sata_setting,
2582 if (err_mask)
2583 ata_dev_dbg(dev,
2584 "failed to get Identify Device Data, Emask 0x%x\n",
2585 err_mask);
2586 else
2587 for (i = 0; i < ATA_LOG_DEVSLP_SIZE; i++) {
2588 j = ATA_LOG_DEVSLP_OFFSET + i;
2589 dev->devslp_timing[i] = sata_setting[j];
2592 ata_dev_config_sense_reporting(dev);
2593 ata_dev_config_zac(dev);
2594 ata_dev_config_trusted(dev);
2595 dev->cdb_len = 32;
2598 /* ATAPI-specific feature tests */
2599 else if (dev->class == ATA_DEV_ATAPI) {
2600 const char *cdb_intr_string = "";
2601 const char *atapi_an_string = "";
2602 const char *dma_dir_string = "";
2603 u32 sntf;
2605 rc = atapi_cdb_len(id);
2606 if ((rc < 12) || (rc > ATAPI_CDB_LEN)) {
2607 if (ata_msg_warn(ap))
2608 ata_dev_warn(dev, "unsupported CDB len\n");
2609 rc = -EINVAL;
2610 goto err_out_nosup;
2612 dev->cdb_len = (unsigned int) rc;
2614 /* Enable ATAPI AN if both the host and device have
2615 * the support. If PMP is attached, SNTF is required
2616 * to enable ATAPI AN to discern between PHY status
2617 * changed notifications and ATAPI ANs.
2619 if (atapi_an &&
2620 (ap->flags & ATA_FLAG_AN) && ata_id_has_atapi_AN(id) &&
2621 (!sata_pmp_attached(ap) ||
2622 sata_scr_read(&ap->link, SCR_NOTIFICATION, &sntf) == 0)) {
2623 /* issue SET feature command to turn this on */
2624 err_mask = ata_dev_set_feature(dev,
2625 SETFEATURES_SATA_ENABLE, SATA_AN);
2626 if (err_mask)
2627 ata_dev_err(dev,
2628 "failed to enable ATAPI AN (err_mask=0x%x)\n",
2629 err_mask);
2630 else {
2631 dev->flags |= ATA_DFLAG_AN;
2632 atapi_an_string = ", ATAPI AN";
2636 if (ata_id_cdb_intr(dev->id)) {
2637 dev->flags |= ATA_DFLAG_CDB_INTR;
2638 cdb_intr_string = ", CDB intr";
2641 if (atapi_dmadir || (dev->horkage & ATA_HORKAGE_ATAPI_DMADIR) || atapi_id_dmadir(dev->id)) {
2642 dev->flags |= ATA_DFLAG_DMADIR;
2643 dma_dir_string = ", DMADIR";
2646 if (ata_id_has_da(dev->id)) {
2647 dev->flags |= ATA_DFLAG_DA;
2648 zpodd_init(dev);
2651 /* print device info to dmesg */
2652 if (ata_msg_drv(ap) && print_info)
2653 ata_dev_info(dev,
2654 "ATAPI: %s, %s, max %s%s%s%s\n",
2655 modelbuf, fwrevbuf,
2656 ata_mode_string(xfer_mask),
2657 cdb_intr_string, atapi_an_string,
2658 dma_dir_string);
2661 /* determine max_sectors */
2662 dev->max_sectors = ATA_MAX_SECTORS;
2663 if (dev->flags & ATA_DFLAG_LBA48)
2664 dev->max_sectors = ATA_MAX_SECTORS_LBA48;
2666 /* Limit PATA drive on SATA cable bridge transfers to udma5,
2667 200 sectors */
2668 if (ata_dev_knobble(dev)) {
2669 if (ata_msg_drv(ap) && print_info)
2670 ata_dev_info(dev, "applying bridge limits\n");
2671 dev->udma_mask &= ATA_UDMA5;
2672 dev->max_sectors = ATA_MAX_SECTORS;
2675 if ((dev->class == ATA_DEV_ATAPI) &&
2676 (atapi_command_packet_set(id) == TYPE_TAPE)) {
2677 dev->max_sectors = ATA_MAX_SECTORS_TAPE;
2678 dev->horkage |= ATA_HORKAGE_STUCK_ERR;
2681 if (dev->horkage & ATA_HORKAGE_MAX_SEC_128)
2682 dev->max_sectors = min_t(unsigned int, ATA_MAX_SECTORS_128,
2683 dev->max_sectors);
2685 if (dev->horkage & ATA_HORKAGE_MAX_SEC_1024)
2686 dev->max_sectors = min_t(unsigned int, ATA_MAX_SECTORS_1024,
2687 dev->max_sectors);
2689 if (dev->horkage & ATA_HORKAGE_MAX_SEC_LBA48)
2690 dev->max_sectors = ATA_MAX_SECTORS_LBA48;
2692 if (ap->ops->dev_config)
2693 ap->ops->dev_config(dev);
2695 if (dev->horkage & ATA_HORKAGE_DIAGNOSTIC) {
2696 /* Let the user know. We don't want to disallow opens for
2697 rescue purposes, or in case the vendor is just a blithering
2698 idiot. Do this after the dev_config call as some controllers
2699 with buggy firmware may want to avoid reporting false device
2700 bugs */
2702 if (print_info) {
2703 ata_dev_warn(dev,
2704 "Drive reports diagnostics failure. This may indicate a drive\n");
2705 ata_dev_warn(dev,
2706 "fault or invalid emulation. Contact drive vendor for information.\n");
2710 if ((dev->horkage & ATA_HORKAGE_FIRMWARE_WARN) && print_info) {
2711 ata_dev_warn(dev, "WARNING: device requires firmware update to be fully functional\n");
2712 ata_dev_warn(dev, " contact the vendor or visit http://ata.wiki.kernel.org\n");
2715 return 0;
2717 err_out_nosup:
2718 if (ata_msg_probe(ap))
2719 ata_dev_dbg(dev, "%s: EXIT, err\n", __func__);
2720 return rc;
2724 * ata_cable_40wire - return 40 wire cable type
2725 * @ap: port
2727 * Helper method for drivers which want to hardwire 40 wire cable
2728 * detection.
2731 int ata_cable_40wire(struct ata_port *ap)
2733 return ATA_CBL_PATA40;
2735 EXPORT_SYMBOL_GPL(ata_cable_40wire);
2738 * ata_cable_80wire - return 80 wire cable type
2739 * @ap: port
2741 * Helper method for drivers which want to hardwire 80 wire cable
2742 * detection.
2745 int ata_cable_80wire(struct ata_port *ap)
2747 return ATA_CBL_PATA80;
2749 EXPORT_SYMBOL_GPL(ata_cable_80wire);
2752 * ata_cable_unknown - return unknown PATA cable.
2753 * @ap: port
2755 * Helper method for drivers which have no PATA cable detection.
2758 int ata_cable_unknown(struct ata_port *ap)
2760 return ATA_CBL_PATA_UNK;
2762 EXPORT_SYMBOL_GPL(ata_cable_unknown);
2765 * ata_cable_ignore - return ignored PATA cable.
2766 * @ap: port
2768 * Helper method for drivers which don't use cable type to limit
2769 * transfer mode.
2771 int ata_cable_ignore(struct ata_port *ap)
2773 return ATA_CBL_PATA_IGN;
2775 EXPORT_SYMBOL_GPL(ata_cable_ignore);
2778 * ata_cable_sata - return SATA cable type
2779 * @ap: port
2781 * Helper method for drivers which have SATA cables
2784 int ata_cable_sata(struct ata_port *ap)
2786 return ATA_CBL_SATA;
2788 EXPORT_SYMBOL_GPL(ata_cable_sata);
2791 * ata_bus_probe - Reset and probe ATA bus
2792 * @ap: Bus to probe
2794 * Master ATA bus probing function. Initiates a hardware-dependent
2795 * bus reset, then attempts to identify any devices found on
2796 * the bus.
2798 * LOCKING:
2799 * PCI/etc. bus probe sem.
2801 * RETURNS:
2802 * Zero on success, negative errno otherwise.
2805 int ata_bus_probe(struct ata_port *ap)
2807 unsigned int classes[ATA_MAX_DEVICES];
2808 int tries[ATA_MAX_DEVICES];
2809 int rc;
2810 struct ata_device *dev;
2812 ata_for_each_dev(dev, &ap->link, ALL)
2813 tries[dev->devno] = ATA_PROBE_MAX_TRIES;
2815 retry:
2816 ata_for_each_dev(dev, &ap->link, ALL) {
2817 /* If we issue an SRST then an ATA drive (not ATAPI)
2818 * may change configuration and be in PIO0 timing. If
2819 * we do a hard reset (or are coming from power on)
2820 * this is true for ATA or ATAPI. Until we've set a
2821 * suitable controller mode we should not touch the
2822 * bus as we may be talking too fast.
2824 dev->pio_mode = XFER_PIO_0;
2825 dev->dma_mode = 0xff;
2827 /* If the controller has a pio mode setup function
2828 * then use it to set the chipset to rights. Don't
2829 * touch the DMA setup as that will be dealt with when
2830 * configuring devices.
2832 if (ap->ops->set_piomode)
2833 ap->ops->set_piomode(ap, dev);
2836 /* reset and determine device classes */
2837 ap->ops->phy_reset(ap);
2839 ata_for_each_dev(dev, &ap->link, ALL) {
2840 if (dev->class != ATA_DEV_UNKNOWN)
2841 classes[dev->devno] = dev->class;
2842 else
2843 classes[dev->devno] = ATA_DEV_NONE;
2845 dev->class = ATA_DEV_UNKNOWN;
2848 /* read IDENTIFY page and configure devices. We have to do the identify
2849 specific sequence bass-ackwards so that PDIAG- is released by
2850 the slave device */
2852 ata_for_each_dev(dev, &ap->link, ALL_REVERSE) {
2853 if (tries[dev->devno])
2854 dev->class = classes[dev->devno];
2856 if (!ata_dev_enabled(dev))
2857 continue;
2859 rc = ata_dev_read_id(dev, &dev->class, ATA_READID_POSTRESET,
2860 dev->id);
2861 if (rc)
2862 goto fail;
2865 /* Now ask for the cable type as PDIAG- should have been released */
2866 if (ap->ops->cable_detect)
2867 ap->cbl = ap->ops->cable_detect(ap);
2869 /* We may have SATA bridge glue hiding here irrespective of
2870 * the reported cable types and sensed types. When SATA
2871 * drives indicate we have a bridge, we don't know which end
2872 * of the link the bridge is which is a problem.
2874 ata_for_each_dev(dev, &ap->link, ENABLED)
2875 if (ata_id_is_sata(dev->id))
2876 ap->cbl = ATA_CBL_SATA;
2878 /* After the identify sequence we can now set up the devices. We do
2879 this in the normal order so that the user doesn't get confused */
2881 ata_for_each_dev(dev, &ap->link, ENABLED) {
2882 ap->link.eh_context.i.flags |= ATA_EHI_PRINTINFO;
2883 rc = ata_dev_configure(dev);
2884 ap->link.eh_context.i.flags &= ~ATA_EHI_PRINTINFO;
2885 if (rc)
2886 goto fail;
2889 /* configure transfer mode */
2890 rc = ata_set_mode(&ap->link, &dev);
2891 if (rc)
2892 goto fail;
2894 ata_for_each_dev(dev, &ap->link, ENABLED)
2895 return 0;
2897 return -ENODEV;
2899 fail:
2900 tries[dev->devno]--;
2902 switch (rc) {
2903 case -EINVAL:
2904 /* eeek, something went very wrong, give up */
2905 tries[dev->devno] = 0;
2906 break;
2908 case -ENODEV:
2909 /* give it just one more chance */
2910 tries[dev->devno] = min(tries[dev->devno], 1);
2911 /* fall through */
2912 case -EIO:
2913 if (tries[dev->devno] == 1) {
2914 /* This is the last chance, better to slow
2915 * down than lose it.
2917 sata_down_spd_limit(&ap->link, 0);
2918 ata_down_xfermask_limit(dev, ATA_DNXFER_PIO);
2922 if (!tries[dev->devno])
2923 ata_dev_disable(dev);
2925 goto retry;
2929 * sata_print_link_status - Print SATA link status
2930 * @link: SATA link to printk link status about
2932 * This function prints link speed and status of a SATA link.
2934 * LOCKING:
2935 * None.
2937 static void sata_print_link_status(struct ata_link *link)
2939 u32 sstatus, scontrol, tmp;
2941 if (sata_scr_read(link, SCR_STATUS, &sstatus))
2942 return;
2943 sata_scr_read(link, SCR_CONTROL, &scontrol);
2945 if (ata_phys_link_online(link)) {
2946 tmp = (sstatus >> 4) & 0xf;
2947 ata_link_info(link, "SATA link up %s (SStatus %X SControl %X)\n",
2948 sata_spd_string(tmp), sstatus, scontrol);
2949 } else {
2950 ata_link_info(link, "SATA link down (SStatus %X SControl %X)\n",
2951 sstatus, scontrol);
2956 * ata_dev_pair - return other device on cable
2957 * @adev: device
2959 * Obtain the other device on the same cable, or if none is
2960 * present NULL is returned
2963 struct ata_device *ata_dev_pair(struct ata_device *adev)
2965 struct ata_link *link = adev->link;
2966 struct ata_device *pair = &link->device[1 - adev->devno];
2967 if (!ata_dev_enabled(pair))
2968 return NULL;
2969 return pair;
2971 EXPORT_SYMBOL_GPL(ata_dev_pair);
2974 * sata_down_spd_limit - adjust SATA spd limit downward
2975 * @link: Link to adjust SATA spd limit for
2976 * @spd_limit: Additional limit
2978 * Adjust SATA spd limit of @link downward. Note that this
2979 * function only adjusts the limit. The change must be applied
2980 * using sata_set_spd().
2982 * If @spd_limit is non-zero, the speed is limited to equal to or
2983 * lower than @spd_limit if such speed is supported. If
2984 * @spd_limit is slower than any supported speed, only the lowest
2985 * supported speed is allowed.
2987 * LOCKING:
2988 * Inherited from caller.
2990 * RETURNS:
2991 * 0 on success, negative errno on failure
2993 int sata_down_spd_limit(struct ata_link *link, u32 spd_limit)
2995 u32 sstatus, spd, mask;
2996 int rc, bit;
2998 if (!sata_scr_valid(link))
2999 return -EOPNOTSUPP;
3001 /* If SCR can be read, use it to determine the current SPD.
3002 * If not, use cached value in link->sata_spd.
3004 rc = sata_scr_read(link, SCR_STATUS, &sstatus);
3005 if (rc == 0 && ata_sstatus_online(sstatus))
3006 spd = (sstatus >> 4) & 0xf;
3007 else
3008 spd = link->sata_spd;
3010 mask = link->sata_spd_limit;
3011 if (mask <= 1)
3012 return -EINVAL;
3014 /* unconditionally mask off the highest bit */
3015 bit = fls(mask) - 1;
3016 mask &= ~(1 << bit);
3019 * Mask off all speeds higher than or equal to the current one. At
3020 * this point, if current SPD is not available and we previously
3021 * recorded the link speed from SStatus, the driver has already
3022 * masked off the highest bit so mask should already be 1 or 0.
3023 * Otherwise, we should not force 1.5Gbps on a link where we have
3024 * not previously recorded speed from SStatus. Just return in this
3025 * case.
3027 if (spd > 1)
3028 mask &= (1 << (spd - 1)) - 1;
3029 else
3030 return -EINVAL;
3032 /* were we already at the bottom? */
3033 if (!mask)
3034 return -EINVAL;
3036 if (spd_limit) {
3037 if (mask & ((1 << spd_limit) - 1))
3038 mask &= (1 << spd_limit) - 1;
3039 else {
3040 bit = ffs(mask) - 1;
3041 mask = 1 << bit;
3045 link->sata_spd_limit = mask;
3047 ata_link_warn(link, "limiting SATA link speed to %s\n",
3048 sata_spd_string(fls(mask)));
3050 return 0;
3053 static int __sata_set_spd_needed(struct ata_link *link, u32 *scontrol)
3055 struct ata_link *host_link = &link->ap->link;
3056 u32 limit, target, spd;
3058 limit = link->sata_spd_limit;
3060 /* Don't configure downstream link faster than upstream link.
3061 * It doesn't speed up anything and some PMPs choke on such
3062 * configuration.
3064 if (!ata_is_host_link(link) && host_link->sata_spd)
3065 limit &= (1 << host_link->sata_spd) - 1;
3067 if (limit == UINT_MAX)
3068 target = 0;
3069 else
3070 target = fls(limit);
3072 spd = (*scontrol >> 4) & 0xf;
3073 *scontrol = (*scontrol & ~0xf0) | ((target & 0xf) << 4);
3075 return spd != target;
3079 * sata_set_spd_needed - is SATA spd configuration needed
3080 * @link: Link in question
3082 * Test whether the spd limit in SControl matches
3083 * @link->sata_spd_limit. This function is used to determine
3084 * whether hardreset is necessary to apply SATA spd
3085 * configuration.
3087 * LOCKING:
3088 * Inherited from caller.
3090 * RETURNS:
3091 * 1 if SATA spd configuration is needed, 0 otherwise.
3093 static int sata_set_spd_needed(struct ata_link *link)
3095 u32 scontrol;
3097 if (sata_scr_read(link, SCR_CONTROL, &scontrol))
3098 return 1;
3100 return __sata_set_spd_needed(link, &scontrol);
3104 * sata_set_spd - set SATA spd according to spd limit
3105 * @link: Link to set SATA spd for
3107 * Set SATA spd of @link according to sata_spd_limit.
3109 * LOCKING:
3110 * Inherited from caller.
3112 * RETURNS:
3113 * 0 if spd doesn't need to be changed, 1 if spd has been
3114 * changed. Negative errno if SCR registers are inaccessible.
3116 int sata_set_spd(struct ata_link *link)
3118 u32 scontrol;
3119 int rc;
3121 if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
3122 return rc;
3124 if (!__sata_set_spd_needed(link, &scontrol))
3125 return 0;
3127 if ((rc = sata_scr_write(link, SCR_CONTROL, scontrol)))
3128 return rc;
3130 return 1;
3132 EXPORT_SYMBOL_GPL(sata_set_spd);
3134 #ifdef CONFIG_ATA_ACPI
3136 * ata_timing_cycle2mode - find xfer mode for the specified cycle duration
3137 * @xfer_shift: ATA_SHIFT_* value for transfer type to examine.
3138 * @cycle: cycle duration in ns
3140 * Return matching xfer mode for @cycle. The returned mode is of
3141 * the transfer type specified by @xfer_shift. If @cycle is too
3142 * slow for @xfer_shift, 0xff is returned. If @cycle is faster
3143 * than the fastest known mode, the fasted mode is returned.
3145 * LOCKING:
3146 * None.
3148 * RETURNS:
3149 * Matching xfer_mode, 0xff if no match found.
3151 u8 ata_timing_cycle2mode(unsigned int xfer_shift, int cycle)
3153 u8 base_mode = 0xff, last_mode = 0xff;
3154 const struct ata_xfer_ent *ent;
3155 const struct ata_timing *t;
3157 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
3158 if (ent->shift == xfer_shift)
3159 base_mode = ent->base;
3161 for (t = ata_timing_find_mode(base_mode);
3162 t && ata_xfer_mode2shift(t->mode) == xfer_shift; t++) {
3163 unsigned short this_cycle;
3165 switch (xfer_shift) {
3166 case ATA_SHIFT_PIO:
3167 case ATA_SHIFT_MWDMA:
3168 this_cycle = t->cycle;
3169 break;
3170 case ATA_SHIFT_UDMA:
3171 this_cycle = t->udma;
3172 break;
3173 default:
3174 return 0xff;
3177 if (cycle > this_cycle)
3178 break;
3180 last_mode = t->mode;
3183 return last_mode;
3185 #endif
3188 * ata_down_xfermask_limit - adjust dev xfer masks downward
3189 * @dev: Device to adjust xfer masks
3190 * @sel: ATA_DNXFER_* selector
3192 * Adjust xfer masks of @dev downward. Note that this function
3193 * does not apply the change. Invoking ata_set_mode() afterwards
3194 * will apply the limit.
3196 * LOCKING:
3197 * Inherited from caller.
3199 * RETURNS:
3200 * 0 on success, negative errno on failure
3202 int ata_down_xfermask_limit(struct ata_device *dev, unsigned int sel)
3204 char buf[32];
3205 unsigned long orig_mask, xfer_mask;
3206 unsigned long pio_mask, mwdma_mask, udma_mask;
3207 int quiet, highbit;
3209 quiet = !!(sel & ATA_DNXFER_QUIET);
3210 sel &= ~ATA_DNXFER_QUIET;
3212 xfer_mask = orig_mask = ata_pack_xfermask(dev->pio_mask,
3213 dev->mwdma_mask,
3214 dev->udma_mask);
3215 ata_unpack_xfermask(xfer_mask, &pio_mask, &mwdma_mask, &udma_mask);
3217 switch (sel) {
3218 case ATA_DNXFER_PIO:
3219 highbit = fls(pio_mask) - 1;
3220 pio_mask &= ~(1 << highbit);
3221 break;
3223 case ATA_DNXFER_DMA:
3224 if (udma_mask) {
3225 highbit = fls(udma_mask) - 1;
3226 udma_mask &= ~(1 << highbit);
3227 if (!udma_mask)
3228 return -ENOENT;
3229 } else if (mwdma_mask) {
3230 highbit = fls(mwdma_mask) - 1;
3231 mwdma_mask &= ~(1 << highbit);
3232 if (!mwdma_mask)
3233 return -ENOENT;
3235 break;
3237 case ATA_DNXFER_40C:
3238 udma_mask &= ATA_UDMA_MASK_40C;
3239 break;
3241 case ATA_DNXFER_FORCE_PIO0:
3242 pio_mask &= 1;
3243 /* fall through */
3244 case ATA_DNXFER_FORCE_PIO:
3245 mwdma_mask = 0;
3246 udma_mask = 0;
3247 break;
3249 default:
3250 BUG();
3253 xfer_mask &= ata_pack_xfermask(pio_mask, mwdma_mask, udma_mask);
3255 if (!(xfer_mask & ATA_MASK_PIO) || xfer_mask == orig_mask)
3256 return -ENOENT;
3258 if (!quiet) {
3259 if (xfer_mask & (ATA_MASK_MWDMA | ATA_MASK_UDMA))
3260 snprintf(buf, sizeof(buf), "%s:%s",
3261 ata_mode_string(xfer_mask),
3262 ata_mode_string(xfer_mask & ATA_MASK_PIO));
3263 else
3264 snprintf(buf, sizeof(buf), "%s",
3265 ata_mode_string(xfer_mask));
3267 ata_dev_warn(dev, "limiting speed to %s\n", buf);
3270 ata_unpack_xfermask(xfer_mask, &dev->pio_mask, &dev->mwdma_mask,
3271 &dev->udma_mask);
3273 return 0;
3276 static int ata_dev_set_mode(struct ata_device *dev)
3278 struct ata_port *ap = dev->link->ap;
3279 struct ata_eh_context *ehc = &dev->link->eh_context;
3280 const bool nosetxfer = dev->horkage & ATA_HORKAGE_NOSETXFER;
3281 const char *dev_err_whine = "";
3282 int ign_dev_err = 0;
3283 unsigned int err_mask = 0;
3284 int rc;
3286 dev->flags &= ~ATA_DFLAG_PIO;
3287 if (dev->xfer_shift == ATA_SHIFT_PIO)
3288 dev->flags |= ATA_DFLAG_PIO;
3290 if (nosetxfer && ap->flags & ATA_FLAG_SATA && ata_id_is_sata(dev->id))
3291 dev_err_whine = " (SET_XFERMODE skipped)";
3292 else {
3293 if (nosetxfer)
3294 ata_dev_warn(dev,
3295 "NOSETXFER but PATA detected - can't "
3296 "skip SETXFER, might malfunction\n");
3297 err_mask = ata_dev_set_xfermode(dev);
3300 if (err_mask & ~AC_ERR_DEV)
3301 goto fail;
3303 /* revalidate */
3304 ehc->i.flags |= ATA_EHI_POST_SETMODE;
3305 rc = ata_dev_revalidate(dev, ATA_DEV_UNKNOWN, 0);
3306 ehc->i.flags &= ~ATA_EHI_POST_SETMODE;
3307 if (rc)
3308 return rc;
3310 if (dev->xfer_shift == ATA_SHIFT_PIO) {
3311 /* Old CFA may refuse this command, which is just fine */
3312 if (ata_id_is_cfa(dev->id))
3313 ign_dev_err = 1;
3314 /* Catch several broken garbage emulations plus some pre
3315 ATA devices */
3316 if (ata_id_major_version(dev->id) == 0 &&
3317 dev->pio_mode <= XFER_PIO_2)
3318 ign_dev_err = 1;
3319 /* Some very old devices and some bad newer ones fail
3320 any kind of SET_XFERMODE request but support PIO0-2
3321 timings and no IORDY */
3322 if (!ata_id_has_iordy(dev->id) && dev->pio_mode <= XFER_PIO_2)
3323 ign_dev_err = 1;
3325 /* Early MWDMA devices do DMA but don't allow DMA mode setting.
3326 Don't fail an MWDMA0 set IFF the device indicates it is in MWDMA0 */
3327 if (dev->xfer_shift == ATA_SHIFT_MWDMA &&
3328 dev->dma_mode == XFER_MW_DMA_0 &&
3329 (dev->id[63] >> 8) & 1)
3330 ign_dev_err = 1;
3332 /* if the device is actually configured correctly, ignore dev err */
3333 if (dev->xfer_mode == ata_xfer_mask2mode(ata_id_xfermask(dev->id)))
3334 ign_dev_err = 1;
3336 if (err_mask & AC_ERR_DEV) {
3337 if (!ign_dev_err)
3338 goto fail;
3339 else
3340 dev_err_whine = " (device error ignored)";
3343 DPRINTK("xfer_shift=%u, xfer_mode=0x%x\n",
3344 dev->xfer_shift, (int)dev->xfer_mode);
3346 if (!(ehc->i.flags & ATA_EHI_QUIET) ||
3347 ehc->i.flags & ATA_EHI_DID_HARDRESET)
3348 ata_dev_info(dev, "configured for %s%s\n",
3349 ata_mode_string(ata_xfer_mode2mask(dev->xfer_mode)),
3350 dev_err_whine);
3352 return 0;
3354 fail:
3355 ata_dev_err(dev, "failed to set xfermode (err_mask=0x%x)\n", err_mask);
3356 return -EIO;
3360 * ata_do_set_mode - Program timings and issue SET FEATURES - XFER
3361 * @link: link on which timings will be programmed
3362 * @r_failed_dev: out parameter for failed device
3364 * Standard implementation of the function used to tune and set
3365 * ATA device disk transfer mode (PIO3, UDMA6, etc.). If
3366 * ata_dev_set_mode() fails, pointer to the failing device is
3367 * returned in @r_failed_dev.
3369 * LOCKING:
3370 * PCI/etc. bus probe sem.
3372 * RETURNS:
3373 * 0 on success, negative errno otherwise
3376 int ata_do_set_mode(struct ata_link *link, struct ata_device **r_failed_dev)
3378 struct ata_port *ap = link->ap;
3379 struct ata_device *dev;
3380 int rc = 0, used_dma = 0, found = 0;
3382 /* step 1: calculate xfer_mask */
3383 ata_for_each_dev(dev, link, ENABLED) {
3384 unsigned long pio_mask, dma_mask;
3385 unsigned int mode_mask;
3387 mode_mask = ATA_DMA_MASK_ATA;
3388 if (dev->class == ATA_DEV_ATAPI)
3389 mode_mask = ATA_DMA_MASK_ATAPI;
3390 else if (ata_id_is_cfa(dev->id))
3391 mode_mask = ATA_DMA_MASK_CFA;
3393 ata_dev_xfermask(dev);
3394 ata_force_xfermask(dev);
3396 pio_mask = ata_pack_xfermask(dev->pio_mask, 0, 0);
3398 if (libata_dma_mask & mode_mask)
3399 dma_mask = ata_pack_xfermask(0, dev->mwdma_mask,
3400 dev->udma_mask);
3401 else
3402 dma_mask = 0;
3404 dev->pio_mode = ata_xfer_mask2mode(pio_mask);
3405 dev->dma_mode = ata_xfer_mask2mode(dma_mask);
3407 found = 1;
3408 if (ata_dma_enabled(dev))
3409 used_dma = 1;
3411 if (!found)
3412 goto out;
3414 /* step 2: always set host PIO timings */
3415 ata_for_each_dev(dev, link, ENABLED) {
3416 if (dev->pio_mode == 0xff) {
3417 ata_dev_warn(dev, "no PIO support\n");
3418 rc = -EINVAL;
3419 goto out;
3422 dev->xfer_mode = dev->pio_mode;
3423 dev->xfer_shift = ATA_SHIFT_PIO;
3424 if (ap->ops->set_piomode)
3425 ap->ops->set_piomode(ap, dev);
3428 /* step 3: set host DMA timings */
3429 ata_for_each_dev(dev, link, ENABLED) {
3430 if (!ata_dma_enabled(dev))
3431 continue;
3433 dev->xfer_mode = dev->dma_mode;
3434 dev->xfer_shift = ata_xfer_mode2shift(dev->dma_mode);
3435 if (ap->ops->set_dmamode)
3436 ap->ops->set_dmamode(ap, dev);
3439 /* step 4: update devices' xfer mode */
3440 ata_for_each_dev(dev, link, ENABLED) {
3441 rc = ata_dev_set_mode(dev);
3442 if (rc)
3443 goto out;
3446 /* Record simplex status. If we selected DMA then the other
3447 * host channels are not permitted to do so.
3449 if (used_dma && (ap->host->flags & ATA_HOST_SIMPLEX))
3450 ap->host->simplex_claimed = ap;
3452 out:
3453 if (rc)
3454 *r_failed_dev = dev;
3455 return rc;
3457 EXPORT_SYMBOL_GPL(ata_do_set_mode);
3460 * ata_wait_ready - wait for link to become ready
3461 * @link: link to be waited on
3462 * @deadline: deadline jiffies for the operation
3463 * @check_ready: callback to check link readiness
3465 * Wait for @link to become ready. @check_ready should return
3466 * positive number if @link is ready, 0 if it isn't, -ENODEV if
3467 * link doesn't seem to be occupied, other errno for other error
3468 * conditions.
3470 * Transient -ENODEV conditions are allowed for
3471 * ATA_TMOUT_FF_WAIT.
3473 * LOCKING:
3474 * EH context.
3476 * RETURNS:
3477 * 0 if @link is ready before @deadline; otherwise, -errno.
3479 int ata_wait_ready(struct ata_link *link, unsigned long deadline,
3480 int (*check_ready)(struct ata_link *link))
3482 unsigned long start = jiffies;
3483 unsigned long nodev_deadline;
3484 int warned = 0;
3486 /* choose which 0xff timeout to use, read comment in libata.h */
3487 if (link->ap->host->flags & ATA_HOST_PARALLEL_SCAN)
3488 nodev_deadline = ata_deadline(start, ATA_TMOUT_FF_WAIT_LONG);
3489 else
3490 nodev_deadline = ata_deadline(start, ATA_TMOUT_FF_WAIT);
3492 /* Slave readiness can't be tested separately from master. On
3493 * M/S emulation configuration, this function should be called
3494 * only on the master and it will handle both master and slave.
3496 WARN_ON(link == link->ap->slave_link);
3498 if (time_after(nodev_deadline, deadline))
3499 nodev_deadline = deadline;
3501 while (1) {
3502 unsigned long now = jiffies;
3503 int ready, tmp;
3505 ready = tmp = check_ready(link);
3506 if (ready > 0)
3507 return 0;
3510 * -ENODEV could be transient. Ignore -ENODEV if link
3511 * is online. Also, some SATA devices take a long
3512 * time to clear 0xff after reset. Wait for
3513 * ATA_TMOUT_FF_WAIT[_LONG] on -ENODEV if link isn't
3514 * offline.
3516 * Note that some PATA controllers (pata_ali) explode
3517 * if status register is read more than once when
3518 * there's no device attached.
3520 if (ready == -ENODEV) {
3521 if (ata_link_online(link))
3522 ready = 0;
3523 else if ((link->ap->flags & ATA_FLAG_SATA) &&
3524 !ata_link_offline(link) &&
3525 time_before(now, nodev_deadline))
3526 ready = 0;
3529 if (ready)
3530 return ready;
3531 if (time_after(now, deadline))
3532 return -EBUSY;
3534 if (!warned && time_after(now, start + 5 * HZ) &&
3535 (deadline - now > 3 * HZ)) {
3536 ata_link_warn(link,
3537 "link is slow to respond, please be patient "
3538 "(ready=%d)\n", tmp);
3539 warned = 1;
3542 ata_msleep(link->ap, 50);
3547 * ata_wait_after_reset - wait for link to become ready after reset
3548 * @link: link to be waited on
3549 * @deadline: deadline jiffies for the operation
3550 * @check_ready: callback to check link readiness
3552 * Wait for @link to become ready after reset.
3554 * LOCKING:
3555 * EH context.
3557 * RETURNS:
3558 * 0 if @link is ready before @deadline; otherwise, -errno.
3560 int ata_wait_after_reset(struct ata_link *link, unsigned long deadline,
3561 int (*check_ready)(struct ata_link *link))
3563 ata_msleep(link->ap, ATA_WAIT_AFTER_RESET);
3565 return ata_wait_ready(link, deadline, check_ready);
3567 EXPORT_SYMBOL_GPL(ata_wait_after_reset);
3570 * sata_link_debounce - debounce SATA phy status
3571 * @link: ATA link to debounce SATA phy status for
3572 * @params: timing parameters { interval, duration, timeout } in msec
3573 * @deadline: deadline jiffies for the operation
3575 * Make sure SStatus of @link reaches stable state, determined by
3576 * holding the same value where DET is not 1 for @duration polled
3577 * every @interval, before @timeout. Timeout constraints the
3578 * beginning of the stable state. Because DET gets stuck at 1 on
3579 * some controllers after hot unplugging, this functions waits
3580 * until timeout then returns 0 if DET is stable at 1.
3582 * @timeout is further limited by @deadline. The sooner of the
3583 * two is used.
3585 * LOCKING:
3586 * Kernel thread context (may sleep)
3588 * RETURNS:
3589 * 0 on success, -errno on failure.
3591 int sata_link_debounce(struct ata_link *link, const unsigned long *params,
3592 unsigned long deadline)
3594 unsigned long interval = params[0];
3595 unsigned long duration = params[1];
3596 unsigned long last_jiffies, t;
3597 u32 last, cur;
3598 int rc;
3600 t = ata_deadline(jiffies, params[2]);
3601 if (time_before(t, deadline))
3602 deadline = t;
3604 if ((rc = sata_scr_read(link, SCR_STATUS, &cur)))
3605 return rc;
3606 cur &= 0xf;
3608 last = cur;
3609 last_jiffies = jiffies;
3611 while (1) {
3612 ata_msleep(link->ap, interval);
3613 if ((rc = sata_scr_read(link, SCR_STATUS, &cur)))
3614 return rc;
3615 cur &= 0xf;
3617 /* DET stable? */
3618 if (cur == last) {
3619 if (cur == 1 && time_before(jiffies, deadline))
3620 continue;
3621 if (time_after(jiffies,
3622 ata_deadline(last_jiffies, duration)))
3623 return 0;
3624 continue;
3627 /* unstable, start over */
3628 last = cur;
3629 last_jiffies = jiffies;
3631 /* Check deadline. If debouncing failed, return
3632 * -EPIPE to tell upper layer to lower link speed.
3634 if (time_after(jiffies, deadline))
3635 return -EPIPE;
3638 EXPORT_SYMBOL_GPL(sata_link_debounce);
3641 * sata_link_resume - resume SATA link
3642 * @link: ATA link to resume SATA
3643 * @params: timing parameters { interval, duration, timeout } in msec
3644 * @deadline: deadline jiffies for the operation
3646 * Resume SATA phy @link and debounce it.
3648 * LOCKING:
3649 * Kernel thread context (may sleep)
3651 * RETURNS:
3652 * 0 on success, -errno on failure.
3654 int sata_link_resume(struct ata_link *link, const unsigned long *params,
3655 unsigned long deadline)
3657 int tries = ATA_LINK_RESUME_TRIES;
3658 u32 scontrol, serror;
3659 int rc;
3661 if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
3662 return rc;
3665 * Writes to SControl sometimes get ignored under certain
3666 * controllers (ata_piix SIDPR). Make sure DET actually is
3667 * cleared.
3669 do {
3670 scontrol = (scontrol & 0x0f0) | 0x300;
3671 if ((rc = sata_scr_write(link, SCR_CONTROL, scontrol)))
3672 return rc;
3674 * Some PHYs react badly if SStatus is pounded
3675 * immediately after resuming. Delay 200ms before
3676 * debouncing.
3678 if (!(link->flags & ATA_LFLAG_NO_DB_DELAY))
3679 ata_msleep(link->ap, 200);
3681 /* is SControl restored correctly? */
3682 if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
3683 return rc;
3684 } while ((scontrol & 0xf0f) != 0x300 && --tries);
3686 if ((scontrol & 0xf0f) != 0x300) {
3687 ata_link_warn(link, "failed to resume link (SControl %X)\n",
3688 scontrol);
3689 return 0;
3692 if (tries < ATA_LINK_RESUME_TRIES)
3693 ata_link_warn(link, "link resume succeeded after %d retries\n",
3694 ATA_LINK_RESUME_TRIES - tries);
3696 if ((rc = sata_link_debounce(link, params, deadline)))
3697 return rc;
3699 /* clear SError, some PHYs require this even for SRST to work */
3700 if (!(rc = sata_scr_read(link, SCR_ERROR, &serror)))
3701 rc = sata_scr_write(link, SCR_ERROR, serror);
3703 return rc != -EINVAL ? rc : 0;
3705 EXPORT_SYMBOL_GPL(sata_link_resume);
3708 * ata_std_prereset - prepare for reset
3709 * @link: ATA link to be reset
3710 * @deadline: deadline jiffies for the operation
3712 * @link is about to be reset. Initialize it. Failure from
3713 * prereset makes libata abort whole reset sequence and give up
3714 * that port, so prereset should be best-effort. It does its
3715 * best to prepare for reset sequence but if things go wrong, it
3716 * should just whine, not fail.
3718 * LOCKING:
3719 * Kernel thread context (may sleep)
3721 * RETURNS:
3722 * 0 on success, -errno otherwise.
3724 int ata_std_prereset(struct ata_link *link, unsigned long deadline)
3726 struct ata_port *ap = link->ap;
3727 struct ata_eh_context *ehc = &link->eh_context;
3728 const unsigned long *timing = sata_ehc_deb_timing(ehc);
3729 int rc;
3731 /* if we're about to do hardreset, nothing more to do */
3732 if (ehc->i.action & ATA_EH_HARDRESET)
3733 return 0;
3735 /* if SATA, resume link */
3736 if (ap->flags & ATA_FLAG_SATA) {
3737 rc = sata_link_resume(link, timing, deadline);
3738 /* whine about phy resume failure but proceed */
3739 if (rc && rc != -EOPNOTSUPP)
3740 ata_link_warn(link,
3741 "failed to resume link for reset (errno=%d)\n",
3742 rc);
3745 /* no point in trying softreset on offline link */
3746 if (ata_phys_link_offline(link))
3747 ehc->i.action &= ~ATA_EH_SOFTRESET;
3749 return 0;
3751 EXPORT_SYMBOL_GPL(ata_std_prereset);
3754 * sata_link_hardreset - reset link via SATA phy reset
3755 * @link: link to reset
3756 * @timing: timing parameters { interval, duration, timeout } in msec
3757 * @deadline: deadline jiffies for the operation
3758 * @online: optional out parameter indicating link onlineness
3759 * @check_ready: optional callback to check link readiness
3761 * SATA phy-reset @link using DET bits of SControl register.
3762 * After hardreset, link readiness is waited upon using
3763 * ata_wait_ready() if @check_ready is specified. LLDs are
3764 * allowed to not specify @check_ready and wait itself after this
3765 * function returns. Device classification is LLD's
3766 * responsibility.
3768 * *@online is set to one iff reset succeeded and @link is online
3769 * after reset.
3771 * LOCKING:
3772 * Kernel thread context (may sleep)
3774 * RETURNS:
3775 * 0 on success, -errno otherwise.
3777 int sata_link_hardreset(struct ata_link *link, const unsigned long *timing,
3778 unsigned long deadline,
3779 bool *online, int (*check_ready)(struct ata_link *))
3781 u32 scontrol;
3782 int rc;
3784 DPRINTK("ENTER\n");
3786 if (online)
3787 *online = false;
3789 if (sata_set_spd_needed(link)) {
3790 /* SATA spec says nothing about how to reconfigure
3791 * spd. To be on the safe side, turn off phy during
3792 * reconfiguration. This works for at least ICH7 AHCI
3793 * and Sil3124.
3795 if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
3796 goto out;
3798 scontrol = (scontrol & 0x0f0) | 0x304;
3800 if ((rc = sata_scr_write(link, SCR_CONTROL, scontrol)))
3801 goto out;
3803 sata_set_spd(link);
3806 /* issue phy wake/reset */
3807 if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
3808 goto out;
3810 scontrol = (scontrol & 0x0f0) | 0x301;
3812 if ((rc = sata_scr_write_flush(link, SCR_CONTROL, scontrol)))
3813 goto out;
3815 /* Couldn't find anything in SATA I/II specs, but AHCI-1.1
3816 * 10.4.2 says at least 1 ms.
3818 ata_msleep(link->ap, 1);
3820 /* bring link back */
3821 rc = sata_link_resume(link, timing, deadline);
3822 if (rc)
3823 goto out;
3824 /* if link is offline nothing more to do */
3825 if (ata_phys_link_offline(link))
3826 goto out;
3828 /* Link is online. From this point, -ENODEV too is an error. */
3829 if (online)
3830 *online = true;
3832 if (sata_pmp_supported(link->ap) && ata_is_host_link(link)) {
3833 /* If PMP is supported, we have to do follow-up SRST.
3834 * Some PMPs don't send D2H Reg FIS after hardreset if
3835 * the first port is empty. Wait only for
3836 * ATA_TMOUT_PMP_SRST_WAIT.
3838 if (check_ready) {
3839 unsigned long pmp_deadline;
3841 pmp_deadline = ata_deadline(jiffies,
3842 ATA_TMOUT_PMP_SRST_WAIT);
3843 if (time_after(pmp_deadline, deadline))
3844 pmp_deadline = deadline;
3845 ata_wait_ready(link, pmp_deadline, check_ready);
3847 rc = -EAGAIN;
3848 goto out;
3851 rc = 0;
3852 if (check_ready)
3853 rc = ata_wait_ready(link, deadline, check_ready);
3854 out:
3855 if (rc && rc != -EAGAIN) {
3856 /* online is set iff link is online && reset succeeded */
3857 if (online)
3858 *online = false;
3859 ata_link_err(link, "COMRESET failed (errno=%d)\n", rc);
3861 DPRINTK("EXIT, rc=%d\n", rc);
3862 return rc;
3864 EXPORT_SYMBOL_GPL(sata_link_hardreset);
3867 * sata_std_hardreset - COMRESET w/o waiting or classification
3868 * @link: link to reset
3869 * @class: resulting class of attached device
3870 * @deadline: deadline jiffies for the operation
3872 * Standard SATA COMRESET w/o waiting or classification.
3874 * LOCKING:
3875 * Kernel thread context (may sleep)
3877 * RETURNS:
3878 * 0 if link offline, -EAGAIN if link online, -errno on errors.
3880 int sata_std_hardreset(struct ata_link *link, unsigned int *class,
3881 unsigned long deadline)
3883 const unsigned long *timing = sata_ehc_deb_timing(&link->eh_context);
3884 bool online;
3885 int rc;
3887 /* do hardreset */
3888 rc = sata_link_hardreset(link, timing, deadline, &online, NULL);
3889 return online ? -EAGAIN : rc;
3891 EXPORT_SYMBOL_GPL(sata_std_hardreset);
3894 * ata_std_postreset - standard postreset callback
3895 * @link: the target ata_link
3896 * @classes: classes of attached devices
3898 * This function is invoked after a successful reset. Note that
3899 * the device might have been reset more than once using
3900 * different reset methods before postreset is invoked.
3902 * LOCKING:
3903 * Kernel thread context (may sleep)
3905 void ata_std_postreset(struct ata_link *link, unsigned int *classes)
3907 u32 serror;
3909 DPRINTK("ENTER\n");
3911 /* reset complete, clear SError */
3912 if (!sata_scr_read(link, SCR_ERROR, &serror))
3913 sata_scr_write(link, SCR_ERROR, serror);
3915 /* print link status */
3916 sata_print_link_status(link);
3918 DPRINTK("EXIT\n");
3920 EXPORT_SYMBOL_GPL(ata_std_postreset);
3923 * ata_dev_same_device - Determine whether new ID matches configured device
3924 * @dev: device to compare against
3925 * @new_class: class of the new device
3926 * @new_id: IDENTIFY page of the new device
3928 * Compare @new_class and @new_id against @dev and determine
3929 * whether @dev is the device indicated by @new_class and
3930 * @new_id.
3932 * LOCKING:
3933 * None.
3935 * RETURNS:
3936 * 1 if @dev matches @new_class and @new_id, 0 otherwise.
3938 static int ata_dev_same_device(struct ata_device *dev, unsigned int new_class,
3939 const u16 *new_id)
3941 const u16 *old_id = dev->id;
3942 unsigned char model[2][ATA_ID_PROD_LEN + 1];
3943 unsigned char serial[2][ATA_ID_SERNO_LEN + 1];
3945 if (dev->class != new_class) {
3946 ata_dev_info(dev, "class mismatch %d != %d\n",
3947 dev->class, new_class);
3948 return 0;
3951 ata_id_c_string(old_id, model[0], ATA_ID_PROD, sizeof(model[0]));
3952 ata_id_c_string(new_id, model[1], ATA_ID_PROD, sizeof(model[1]));
3953 ata_id_c_string(old_id, serial[0], ATA_ID_SERNO, sizeof(serial[0]));
3954 ata_id_c_string(new_id, serial[1], ATA_ID_SERNO, sizeof(serial[1]));
3956 if (strcmp(model[0], model[1])) {
3957 ata_dev_info(dev, "model number mismatch '%s' != '%s'\n",
3958 model[0], model[1]);
3959 return 0;
3962 if (strcmp(serial[0], serial[1])) {
3963 ata_dev_info(dev, "serial number mismatch '%s' != '%s'\n",
3964 serial[0], serial[1]);
3965 return 0;
3968 return 1;
3972 * ata_dev_reread_id - Re-read IDENTIFY data
3973 * @dev: target ATA device
3974 * @readid_flags: read ID flags
3976 * Re-read IDENTIFY page and make sure @dev is still attached to
3977 * the port.
3979 * LOCKING:
3980 * Kernel thread context (may sleep)
3982 * RETURNS:
3983 * 0 on success, negative errno otherwise
3985 int ata_dev_reread_id(struct ata_device *dev, unsigned int readid_flags)
3987 unsigned int class = dev->class;
3988 u16 *id = (void *)dev->link->ap->sector_buf;
3989 int rc;
3991 /* read ID data */
3992 rc = ata_dev_read_id(dev, &class, readid_flags, id);
3993 if (rc)
3994 return rc;
3996 /* is the device still there? */
3997 if (!ata_dev_same_device(dev, class, id))
3998 return -ENODEV;
4000 memcpy(dev->id, id, sizeof(id[0]) * ATA_ID_WORDS);
4001 return 0;
4005 * ata_dev_revalidate - Revalidate ATA device
4006 * @dev: device to revalidate
4007 * @new_class: new class code
4008 * @readid_flags: read ID flags
4010 * Re-read IDENTIFY page, make sure @dev is still attached to the
4011 * port and reconfigure it according to the new IDENTIFY page.
4013 * LOCKING:
4014 * Kernel thread context (may sleep)
4016 * RETURNS:
4017 * 0 on success, negative errno otherwise
4019 int ata_dev_revalidate(struct ata_device *dev, unsigned int new_class,
4020 unsigned int readid_flags)
4022 u64 n_sectors = dev->n_sectors;
4023 u64 n_native_sectors = dev->n_native_sectors;
4024 int rc;
4026 if (!ata_dev_enabled(dev))
4027 return -ENODEV;
4029 /* fail early if !ATA && !ATAPI to avoid issuing [P]IDENTIFY to PMP */
4030 if (ata_class_enabled(new_class) &&
4031 new_class != ATA_DEV_ATA &&
4032 new_class != ATA_DEV_ATAPI &&
4033 new_class != ATA_DEV_ZAC &&
4034 new_class != ATA_DEV_SEMB) {
4035 ata_dev_info(dev, "class mismatch %u != %u\n",
4036 dev->class, new_class);
4037 rc = -ENODEV;
4038 goto fail;
4041 /* re-read ID */
4042 rc = ata_dev_reread_id(dev, readid_flags);
4043 if (rc)
4044 goto fail;
4046 /* configure device according to the new ID */
4047 rc = ata_dev_configure(dev);
4048 if (rc)
4049 goto fail;
4051 /* verify n_sectors hasn't changed */
4052 if (dev->class != ATA_DEV_ATA || !n_sectors ||
4053 dev->n_sectors == n_sectors)
4054 return 0;
4056 /* n_sectors has changed */
4057 ata_dev_warn(dev, "n_sectors mismatch %llu != %llu\n",
4058 (unsigned long long)n_sectors,
4059 (unsigned long long)dev->n_sectors);
4062 * Something could have caused HPA to be unlocked
4063 * involuntarily. If n_native_sectors hasn't changed and the
4064 * new size matches it, keep the device.
4066 if (dev->n_native_sectors == n_native_sectors &&
4067 dev->n_sectors > n_sectors && dev->n_sectors == n_native_sectors) {
4068 ata_dev_warn(dev,
4069 "new n_sectors matches native, probably "
4070 "late HPA unlock, n_sectors updated\n");
4071 /* use the larger n_sectors */
4072 return 0;
4076 * Some BIOSes boot w/o HPA but resume w/ HPA locked. Try
4077 * unlocking HPA in those cases.
4079 * https://bugzilla.kernel.org/show_bug.cgi?id=15396
4081 if (dev->n_native_sectors == n_native_sectors &&
4082 dev->n_sectors < n_sectors && n_sectors == n_native_sectors &&
4083 !(dev->horkage & ATA_HORKAGE_BROKEN_HPA)) {
4084 ata_dev_warn(dev,
4085 "old n_sectors matches native, probably "
4086 "late HPA lock, will try to unlock HPA\n");
4087 /* try unlocking HPA */
4088 dev->flags |= ATA_DFLAG_UNLOCK_HPA;
4089 rc = -EIO;
4090 } else
4091 rc = -ENODEV;
4093 /* restore original n_[native_]sectors and fail */
4094 dev->n_native_sectors = n_native_sectors;
4095 dev->n_sectors = n_sectors;
4096 fail:
4097 ata_dev_err(dev, "revalidation failed (errno=%d)\n", rc);
4098 return rc;
4101 struct ata_blacklist_entry {
4102 const char *model_num;
4103 const char *model_rev;
4104 unsigned long horkage;
4107 static const struct ata_blacklist_entry ata_device_blacklist [] = {
4108 /* Devices with DMA related problems under Linux */
4109 { "WDC AC11000H", NULL, ATA_HORKAGE_NODMA },
4110 { "WDC AC22100H", NULL, ATA_HORKAGE_NODMA },
4111 { "WDC AC32500H", NULL, ATA_HORKAGE_NODMA },
4112 { "WDC AC33100H", NULL, ATA_HORKAGE_NODMA },
4113 { "WDC AC31600H", NULL, ATA_HORKAGE_NODMA },
4114 { "WDC AC32100H", "24.09P07", ATA_HORKAGE_NODMA },
4115 { "WDC AC23200L", "21.10N21", ATA_HORKAGE_NODMA },
4116 { "Compaq CRD-8241B", NULL, ATA_HORKAGE_NODMA },
4117 { "CRD-8400B", NULL, ATA_HORKAGE_NODMA },
4118 { "CRD-848[02]B", NULL, ATA_HORKAGE_NODMA },
4119 { "CRD-84", NULL, ATA_HORKAGE_NODMA },
4120 { "SanDisk SDP3B", NULL, ATA_HORKAGE_NODMA },
4121 { "SanDisk SDP3B-64", NULL, ATA_HORKAGE_NODMA },
4122 { "SANYO CD-ROM CRD", NULL, ATA_HORKAGE_NODMA },
4123 { "HITACHI CDR-8", NULL, ATA_HORKAGE_NODMA },
4124 { "HITACHI CDR-8[34]35",NULL, ATA_HORKAGE_NODMA },
4125 { "Toshiba CD-ROM XM-6202B", NULL, ATA_HORKAGE_NODMA },
4126 { "TOSHIBA CD-ROM XM-1702BC", NULL, ATA_HORKAGE_NODMA },
4127 { "CD-532E-A", NULL, ATA_HORKAGE_NODMA },
4128 { "E-IDE CD-ROM CR-840",NULL, ATA_HORKAGE_NODMA },
4129 { "CD-ROM Drive/F5A", NULL, ATA_HORKAGE_NODMA },
4130 { "WPI CDD-820", NULL, ATA_HORKAGE_NODMA },
4131 { "SAMSUNG CD-ROM SC-148C", NULL, ATA_HORKAGE_NODMA },
4132 { "SAMSUNG CD-ROM SC", NULL, ATA_HORKAGE_NODMA },
4133 { "ATAPI CD-ROM DRIVE 40X MAXIMUM",NULL,ATA_HORKAGE_NODMA },
4134 { "_NEC DV5800A", NULL, ATA_HORKAGE_NODMA },
4135 { "SAMSUNG CD-ROM SN-124", "N001", ATA_HORKAGE_NODMA },
4136 { "Seagate STT20000A", NULL, ATA_HORKAGE_NODMA },
4137 { " 2GB ATA Flash Disk", "ADMA428M", ATA_HORKAGE_NODMA },
4138 { "VRFDFC22048UCHC-TE*", NULL, ATA_HORKAGE_NODMA },
4139 /* Odd clown on sil3726/4726 PMPs */
4140 { "Config Disk", NULL, ATA_HORKAGE_DISABLE },
4142 /* Weird ATAPI devices */
4143 { "TORiSAN DVD-ROM DRD-N216", NULL, ATA_HORKAGE_MAX_SEC_128 },
4144 { "QUANTUM DAT DAT72-000", NULL, ATA_HORKAGE_ATAPI_MOD16_DMA },
4145 { "Slimtype DVD A DS8A8SH", NULL, ATA_HORKAGE_MAX_SEC_LBA48 },
4146 { "Slimtype DVD A DS8A9SH", NULL, ATA_HORKAGE_MAX_SEC_LBA48 },
4149 * Causes silent data corruption with higher max sects.
4150 * http://lkml.kernel.org/g/x49wpy40ysk.fsf@segfault.boston.devel.redhat.com
4152 { "ST380013AS", "3.20", ATA_HORKAGE_MAX_SEC_1024 },
4155 * These devices time out with higher max sects.
4156 * https://bugzilla.kernel.org/show_bug.cgi?id=121671
4158 { "LITEON CX1-JB*-HP", NULL, ATA_HORKAGE_MAX_SEC_1024 },
4159 { "LITEON EP1-*", NULL, ATA_HORKAGE_MAX_SEC_1024 },
4161 /* Devices we expect to fail diagnostics */
4163 /* Devices where NCQ should be avoided */
4164 /* NCQ is slow */
4165 { "WDC WD740ADFD-00", NULL, ATA_HORKAGE_NONCQ },
4166 { "WDC WD740ADFD-00NLR1", NULL, ATA_HORKAGE_NONCQ, },
4167 /* http://thread.gmane.org/gmane.linux.ide/14907 */
4168 { "FUJITSU MHT2060BH", NULL, ATA_HORKAGE_NONCQ },
4169 /* NCQ is broken */
4170 { "Maxtor *", "BANC*", ATA_HORKAGE_NONCQ },
4171 { "Maxtor 7V300F0", "VA111630", ATA_HORKAGE_NONCQ },
4172 { "ST380817AS", "3.42", ATA_HORKAGE_NONCQ },
4173 { "ST3160023AS", "3.42", ATA_HORKAGE_NONCQ },
4174 { "OCZ CORE_SSD", "02.10104", ATA_HORKAGE_NONCQ },
4176 /* Seagate NCQ + FLUSH CACHE firmware bug */
4177 { "ST31500341AS", "SD1[5-9]", ATA_HORKAGE_NONCQ |
4178 ATA_HORKAGE_FIRMWARE_WARN },
4180 { "ST31000333AS", "SD1[5-9]", ATA_HORKAGE_NONCQ |
4181 ATA_HORKAGE_FIRMWARE_WARN },
4183 { "ST3640[36]23AS", "SD1[5-9]", ATA_HORKAGE_NONCQ |
4184 ATA_HORKAGE_FIRMWARE_WARN },
4186 { "ST3320[68]13AS", "SD1[5-9]", ATA_HORKAGE_NONCQ |
4187 ATA_HORKAGE_FIRMWARE_WARN },
4189 /* drives which fail FPDMA_AA activation (some may freeze afterwards)
4190 the ST disks also have LPM issues */
4191 { "ST1000LM024 HN-M101MBB", NULL, ATA_HORKAGE_BROKEN_FPDMA_AA |
4192 ATA_HORKAGE_NOLPM, },
4193 { "VB0250EAVER", "HPG7", ATA_HORKAGE_BROKEN_FPDMA_AA },
4195 /* Blacklist entries taken from Silicon Image 3124/3132
4196 Windows driver .inf file - also several Linux problem reports */
4197 { "HTS541060G9SA00", "MB3OC60D", ATA_HORKAGE_NONCQ, },
4198 { "HTS541080G9SA00", "MB4OC60D", ATA_HORKAGE_NONCQ, },
4199 { "HTS541010G9SA00", "MBZOC60D", ATA_HORKAGE_NONCQ, },
4201 /* https://bugzilla.kernel.org/show_bug.cgi?id=15573 */
4202 { "C300-CTFDDAC128MAG", "0001", ATA_HORKAGE_NONCQ, },
4204 /* Some Sandisk SSDs lock up hard with NCQ enabled. Reported on
4205 SD7SN6S256G and SD8SN8U256G */
4206 { "SanDisk SD[78]SN*G", NULL, ATA_HORKAGE_NONCQ, },
4208 /* devices which puke on READ_NATIVE_MAX */
4209 { "HDS724040KLSA80", "KFAOA20N", ATA_HORKAGE_BROKEN_HPA, },
4210 { "WDC WD3200JD-00KLB0", "WD-WCAMR1130137", ATA_HORKAGE_BROKEN_HPA },
4211 { "WDC WD2500JD-00HBB0", "WD-WMAL71490727", ATA_HORKAGE_BROKEN_HPA },
4212 { "MAXTOR 6L080L4", "A93.0500", ATA_HORKAGE_BROKEN_HPA },
4214 /* this one allows HPA unlocking but fails IOs on the area */
4215 { "OCZ-VERTEX", "1.30", ATA_HORKAGE_BROKEN_HPA },
4217 /* Devices which report 1 sector over size HPA */
4218 { "ST340823A", NULL, ATA_HORKAGE_HPA_SIZE, },
4219 { "ST320413A", NULL, ATA_HORKAGE_HPA_SIZE, },
4220 { "ST310211A", NULL, ATA_HORKAGE_HPA_SIZE, },
4222 /* Devices which get the IVB wrong */
4223 { "QUANTUM FIREBALLlct10 05", "A03.0900", ATA_HORKAGE_IVB, },
4224 /* Maybe we should just blacklist TSSTcorp... */
4225 { "TSSTcorp CDDVDW SH-S202[HJN]", "SB0[01]", ATA_HORKAGE_IVB, },
4227 /* Devices that do not need bridging limits applied */
4228 { "MTRON MSP-SATA*", NULL, ATA_HORKAGE_BRIDGE_OK, },
4229 { "BUFFALO HD-QSU2/R5", NULL, ATA_HORKAGE_BRIDGE_OK, },
4231 /* Devices which aren't very happy with higher link speeds */
4232 { "WD My Book", NULL, ATA_HORKAGE_1_5_GBPS, },
4233 { "Seagate FreeAgent GoFlex", NULL, ATA_HORKAGE_1_5_GBPS, },
4236 * Devices which choke on SETXFER. Applies only if both the
4237 * device and controller are SATA.
4239 { "PIONEER DVD-RW DVRTD08", NULL, ATA_HORKAGE_NOSETXFER },
4240 { "PIONEER DVD-RW DVRTD08A", NULL, ATA_HORKAGE_NOSETXFER },
4241 { "PIONEER DVD-RW DVR-215", NULL, ATA_HORKAGE_NOSETXFER },
4242 { "PIONEER DVD-RW DVR-212D", NULL, ATA_HORKAGE_NOSETXFER },
4243 { "PIONEER DVD-RW DVR-216D", NULL, ATA_HORKAGE_NOSETXFER },
4245 /* Crucial BX100 SSD 500GB has broken LPM support */
4246 { "CT500BX100SSD1", NULL, ATA_HORKAGE_NOLPM },
4248 /* 512GB MX100 with MU01 firmware has both queued TRIM and LPM issues */
4249 { "Crucial_CT512MX100*", "MU01", ATA_HORKAGE_NO_NCQ_TRIM |
4250 ATA_HORKAGE_ZERO_AFTER_TRIM |
4251 ATA_HORKAGE_NOLPM, },
4252 /* 512GB MX100 with newer firmware has only LPM issues */
4253 { "Crucial_CT512MX100*", NULL, ATA_HORKAGE_ZERO_AFTER_TRIM |
4254 ATA_HORKAGE_NOLPM, },
4256 /* 480GB+ M500 SSDs have both queued TRIM and LPM issues */
4257 { "Crucial_CT480M500*", NULL, ATA_HORKAGE_NO_NCQ_TRIM |
4258 ATA_HORKAGE_ZERO_AFTER_TRIM |
4259 ATA_HORKAGE_NOLPM, },
4260 { "Crucial_CT960M500*", NULL, ATA_HORKAGE_NO_NCQ_TRIM |
4261 ATA_HORKAGE_ZERO_AFTER_TRIM |
4262 ATA_HORKAGE_NOLPM, },
4264 /* These specific Samsung models/firmware-revs do not handle LPM well */
4265 { "SAMSUNG MZMPC128HBFU-000MV", "CXM14M1Q", ATA_HORKAGE_NOLPM, },
4266 { "SAMSUNG SSD PM830 mSATA *", "CXM13D1Q", ATA_HORKAGE_NOLPM, },
4267 { "SAMSUNG MZ7TD256HAFV-000L9", NULL, ATA_HORKAGE_NOLPM, },
4268 { "SAMSUNG MZ7TE512HMHP-000L1", "EXT06L0Q", ATA_HORKAGE_NOLPM, },
4270 /* devices that don't properly handle queued TRIM commands */
4271 { "Micron_M500IT_*", "MU01", ATA_HORKAGE_NO_NCQ_TRIM |
4272 ATA_HORKAGE_ZERO_AFTER_TRIM, },
4273 { "Micron_M500_*", NULL, ATA_HORKAGE_NO_NCQ_TRIM |
4274 ATA_HORKAGE_ZERO_AFTER_TRIM, },
4275 { "Crucial_CT*M500*", NULL, ATA_HORKAGE_NO_NCQ_TRIM |
4276 ATA_HORKAGE_ZERO_AFTER_TRIM, },
4277 { "Micron_M5[15]0_*", "MU01", ATA_HORKAGE_NO_NCQ_TRIM |
4278 ATA_HORKAGE_ZERO_AFTER_TRIM, },
4279 { "Crucial_CT*M550*", "MU01", ATA_HORKAGE_NO_NCQ_TRIM |
4280 ATA_HORKAGE_ZERO_AFTER_TRIM, },
4281 { "Crucial_CT*MX100*", "MU01", ATA_HORKAGE_NO_NCQ_TRIM |
4282 ATA_HORKAGE_ZERO_AFTER_TRIM, },
4283 { "Samsung SSD 840*", NULL, ATA_HORKAGE_NO_NCQ_TRIM |
4284 ATA_HORKAGE_ZERO_AFTER_TRIM, },
4285 { "Samsung SSD 850*", NULL, ATA_HORKAGE_NO_NCQ_TRIM |
4286 ATA_HORKAGE_ZERO_AFTER_TRIM, },
4287 { "FCCT*M500*", NULL, ATA_HORKAGE_NO_NCQ_TRIM |
4288 ATA_HORKAGE_ZERO_AFTER_TRIM, },
4290 /* devices that don't properly handle TRIM commands */
4291 { "SuperSSpeed S238*", NULL, ATA_HORKAGE_NOTRIM, },
4294 * As defined, the DRAT (Deterministic Read After Trim) and RZAT
4295 * (Return Zero After Trim) flags in the ATA Command Set are
4296 * unreliable in the sense that they only define what happens if
4297 * the device successfully executed the DSM TRIM command. TRIM
4298 * is only advisory, however, and the device is free to silently
4299 * ignore all or parts of the request.
4301 * Whitelist drives that are known to reliably return zeroes
4302 * after TRIM.
4306 * The intel 510 drive has buggy DRAT/RZAT. Explicitly exclude
4307 * that model before whitelisting all other intel SSDs.
4309 { "INTEL*SSDSC2MH*", NULL, 0, },
4311 { "Micron*", NULL, ATA_HORKAGE_ZERO_AFTER_TRIM, },
4312 { "Crucial*", NULL, ATA_HORKAGE_ZERO_AFTER_TRIM, },
4313 { "INTEL*SSD*", NULL, ATA_HORKAGE_ZERO_AFTER_TRIM, },
4314 { "SSD*INTEL*", NULL, ATA_HORKAGE_ZERO_AFTER_TRIM, },
4315 { "Samsung*SSD*", NULL, ATA_HORKAGE_ZERO_AFTER_TRIM, },
4316 { "SAMSUNG*SSD*", NULL, ATA_HORKAGE_ZERO_AFTER_TRIM, },
4317 { "SAMSUNG*MZ7KM*", NULL, ATA_HORKAGE_ZERO_AFTER_TRIM, },
4318 { "ST[1248][0248]0[FH]*", NULL, ATA_HORKAGE_ZERO_AFTER_TRIM, },
4321 * Some WD SATA-I drives spin up and down erratically when the link
4322 * is put into the slumber mode. We don't have full list of the
4323 * affected devices. Disable LPM if the device matches one of the
4324 * known prefixes and is SATA-1. As a side effect LPM partial is
4325 * lost too.
4327 * https://bugzilla.kernel.org/show_bug.cgi?id=57211
4329 { "WDC WD800JD-*", NULL, ATA_HORKAGE_WD_BROKEN_LPM },
4330 { "WDC WD1200JD-*", NULL, ATA_HORKAGE_WD_BROKEN_LPM },
4331 { "WDC WD1600JD-*", NULL, ATA_HORKAGE_WD_BROKEN_LPM },
4332 { "WDC WD2000JD-*", NULL, ATA_HORKAGE_WD_BROKEN_LPM },
4333 { "WDC WD2500JD-*", NULL, ATA_HORKAGE_WD_BROKEN_LPM },
4334 { "WDC WD3000JD-*", NULL, ATA_HORKAGE_WD_BROKEN_LPM },
4335 { "WDC WD3200JD-*", NULL, ATA_HORKAGE_WD_BROKEN_LPM },
4337 /* End Marker */
4341 static unsigned long ata_dev_blacklisted(const struct ata_device *dev)
4343 unsigned char model_num[ATA_ID_PROD_LEN + 1];
4344 unsigned char model_rev[ATA_ID_FW_REV_LEN + 1];
4345 const struct ata_blacklist_entry *ad = ata_device_blacklist;
4347 ata_id_c_string(dev->id, model_num, ATA_ID_PROD, sizeof(model_num));
4348 ata_id_c_string(dev->id, model_rev, ATA_ID_FW_REV, sizeof(model_rev));
4350 while (ad->model_num) {
4351 if (glob_match(ad->model_num, model_num)) {
4352 if (ad->model_rev == NULL)
4353 return ad->horkage;
4354 if (glob_match(ad->model_rev, model_rev))
4355 return ad->horkage;
4357 ad++;
4359 return 0;
4362 static int ata_dma_blacklisted(const struct ata_device *dev)
4364 /* We don't support polling DMA.
4365 * DMA blacklist those ATAPI devices with CDB-intr (and use PIO)
4366 * if the LLDD handles only interrupts in the HSM_ST_LAST state.
4368 if ((dev->link->ap->flags & ATA_FLAG_PIO_POLLING) &&
4369 (dev->flags & ATA_DFLAG_CDB_INTR))
4370 return 1;
4371 return (dev->horkage & ATA_HORKAGE_NODMA) ? 1 : 0;
4375 * ata_is_40wire - check drive side detection
4376 * @dev: device
4378 * Perform drive side detection decoding, allowing for device vendors
4379 * who can't follow the documentation.
4382 static int ata_is_40wire(struct ata_device *dev)
4384 if (dev->horkage & ATA_HORKAGE_IVB)
4385 return ata_drive_40wire_relaxed(dev->id);
4386 return ata_drive_40wire(dev->id);
4390 * cable_is_40wire - 40/80/SATA decider
4391 * @ap: port to consider
4393 * This function encapsulates the policy for speed management
4394 * in one place. At the moment we don't cache the result but
4395 * there is a good case for setting ap->cbl to the result when
4396 * we are called with unknown cables (and figuring out if it
4397 * impacts hotplug at all).
4399 * Return 1 if the cable appears to be 40 wire.
4402 static int cable_is_40wire(struct ata_port *ap)
4404 struct ata_link *link;
4405 struct ata_device *dev;
4407 /* If the controller thinks we are 40 wire, we are. */
4408 if (ap->cbl == ATA_CBL_PATA40)
4409 return 1;
4411 /* If the controller thinks we are 80 wire, we are. */
4412 if (ap->cbl == ATA_CBL_PATA80 || ap->cbl == ATA_CBL_SATA)
4413 return 0;
4415 /* If the system is known to be 40 wire short cable (eg
4416 * laptop), then we allow 80 wire modes even if the drive
4417 * isn't sure.
4419 if (ap->cbl == ATA_CBL_PATA40_SHORT)
4420 return 0;
4422 /* If the controller doesn't know, we scan.
4424 * Note: We look for all 40 wire detects at this point. Any
4425 * 80 wire detect is taken to be 80 wire cable because
4426 * - in many setups only the one drive (slave if present) will
4427 * give a valid detect
4428 * - if you have a non detect capable drive you don't want it
4429 * to colour the choice
4431 ata_for_each_link(link, ap, EDGE) {
4432 ata_for_each_dev(dev, link, ENABLED) {
4433 if (!ata_is_40wire(dev))
4434 return 0;
4437 return 1;
4441 * ata_dev_xfermask - Compute supported xfermask of the given device
4442 * @dev: Device to compute xfermask for
4444 * Compute supported xfermask of @dev and store it in
4445 * dev->*_mask. This function is responsible for applying all
4446 * known limits including host controller limits, device
4447 * blacklist, etc...
4449 * LOCKING:
4450 * None.
4452 static void ata_dev_xfermask(struct ata_device *dev)
4454 struct ata_link *link = dev->link;
4455 struct ata_port *ap = link->ap;
4456 struct ata_host *host = ap->host;
4457 unsigned long xfer_mask;
4459 /* controller modes available */
4460 xfer_mask = ata_pack_xfermask(ap->pio_mask,
4461 ap->mwdma_mask, ap->udma_mask);
4463 /* drive modes available */
4464 xfer_mask &= ata_pack_xfermask(dev->pio_mask,
4465 dev->mwdma_mask, dev->udma_mask);
4466 xfer_mask &= ata_id_xfermask(dev->id);
4469 * CFA Advanced TrueIDE timings are not allowed on a shared
4470 * cable
4472 if (ata_dev_pair(dev)) {
4473 /* No PIO5 or PIO6 */
4474 xfer_mask &= ~(0x03 << (ATA_SHIFT_PIO + 5));
4475 /* No MWDMA3 or MWDMA 4 */
4476 xfer_mask &= ~(0x03 << (ATA_SHIFT_MWDMA + 3));
4479 if (ata_dma_blacklisted(dev)) {
4480 xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
4481 ata_dev_warn(dev,
4482 "device is on DMA blacklist, disabling DMA\n");
4485 if ((host->flags & ATA_HOST_SIMPLEX) &&
4486 host->simplex_claimed && host->simplex_claimed != ap) {
4487 xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
4488 ata_dev_warn(dev,
4489 "simplex DMA is claimed by other device, disabling DMA\n");
4492 if (ap->flags & ATA_FLAG_NO_IORDY)
4493 xfer_mask &= ata_pio_mask_no_iordy(dev);
4495 if (ap->ops->mode_filter)
4496 xfer_mask = ap->ops->mode_filter(dev, xfer_mask);
4498 /* Apply cable rule here. Don't apply it early because when
4499 * we handle hot plug the cable type can itself change.
4500 * Check this last so that we know if the transfer rate was
4501 * solely limited by the cable.
4502 * Unknown or 80 wire cables reported host side are checked
4503 * drive side as well. Cases where we know a 40wire cable
4504 * is used safely for 80 are not checked here.
4506 if (xfer_mask & (0xF8 << ATA_SHIFT_UDMA))
4507 /* UDMA/44 or higher would be available */
4508 if (cable_is_40wire(ap)) {
4509 ata_dev_warn(dev,
4510 "limited to UDMA/33 due to 40-wire cable\n");
4511 xfer_mask &= ~(0xF8 << ATA_SHIFT_UDMA);
4514 ata_unpack_xfermask(xfer_mask, &dev->pio_mask,
4515 &dev->mwdma_mask, &dev->udma_mask);
4519 * ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
4520 * @dev: Device to which command will be sent
4522 * Issue SET FEATURES - XFER MODE command to device @dev
4523 * on port @ap.
4525 * LOCKING:
4526 * PCI/etc. bus probe sem.
4528 * RETURNS:
4529 * 0 on success, AC_ERR_* mask otherwise.
4532 static unsigned int ata_dev_set_xfermode(struct ata_device *dev)
4534 struct ata_taskfile tf;
4535 unsigned int err_mask;
4537 /* set up set-features taskfile */
4538 DPRINTK("set features - xfer mode\n");
4540 /* Some controllers and ATAPI devices show flaky interrupt
4541 * behavior after setting xfer mode. Use polling instead.
4543 ata_tf_init(dev, &tf);
4544 tf.command = ATA_CMD_SET_FEATURES;
4545 tf.feature = SETFEATURES_XFER;
4546 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE | ATA_TFLAG_POLLING;
4547 tf.protocol = ATA_PROT_NODATA;
4548 /* If we are using IORDY we must send the mode setting command */
4549 if (ata_pio_need_iordy(dev))
4550 tf.nsect = dev->xfer_mode;
4551 /* If the device has IORDY and the controller does not - turn it off */
4552 else if (ata_id_has_iordy(dev->id))
4553 tf.nsect = 0x01;
4554 else /* In the ancient relic department - skip all of this */
4555 return 0;
4557 /* On some disks, this command causes spin-up, so we need longer timeout */
4558 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 15000);
4560 DPRINTK("EXIT, err_mask=%x\n", err_mask);
4561 return err_mask;
4565 * ata_dev_set_feature - Issue SET FEATURES - SATA FEATURES
4566 * @dev: Device to which command will be sent
4567 * @enable: Whether to enable or disable the feature
4568 * @feature: The sector count represents the feature to set
4570 * Issue SET FEATURES - SATA FEATURES command to device @dev
4571 * on port @ap with sector count
4573 * LOCKING:
4574 * PCI/etc. bus probe sem.
4576 * RETURNS:
4577 * 0 on success, AC_ERR_* mask otherwise.
4579 unsigned int ata_dev_set_feature(struct ata_device *dev, u8 enable, u8 feature)
4581 struct ata_taskfile tf;
4582 unsigned int err_mask;
4583 unsigned long timeout = 0;
4585 /* set up set-features taskfile */
4586 DPRINTK("set features - SATA features\n");
4588 ata_tf_init(dev, &tf);
4589 tf.command = ATA_CMD_SET_FEATURES;
4590 tf.feature = enable;
4591 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
4592 tf.protocol = ATA_PROT_NODATA;
4593 tf.nsect = feature;
4595 if (enable == SETFEATURES_SPINUP)
4596 timeout = ata_probe_timeout ?
4597 ata_probe_timeout * 1000 : SETFEATURES_SPINUP_TIMEOUT;
4598 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, timeout);
4600 DPRINTK("EXIT, err_mask=%x\n", err_mask);
4601 return err_mask;
4603 EXPORT_SYMBOL_GPL(ata_dev_set_feature);
4606 * ata_dev_init_params - Issue INIT DEV PARAMS command
4607 * @dev: Device to which command will be sent
4608 * @heads: Number of heads (taskfile parameter)
4609 * @sectors: Number of sectors (taskfile parameter)
4611 * LOCKING:
4612 * Kernel thread context (may sleep)
4614 * RETURNS:
4615 * 0 on success, AC_ERR_* mask otherwise.
4617 static unsigned int ata_dev_init_params(struct ata_device *dev,
4618 u16 heads, u16 sectors)
4620 struct ata_taskfile tf;
4621 unsigned int err_mask;
4623 /* Number of sectors per track 1-255. Number of heads 1-16 */
4624 if (sectors < 1 || sectors > 255 || heads < 1 || heads > 16)
4625 return AC_ERR_INVALID;
4627 /* set up init dev params taskfile */
4628 DPRINTK("init dev params \n");
4630 ata_tf_init(dev, &tf);
4631 tf.command = ATA_CMD_INIT_DEV_PARAMS;
4632 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
4633 tf.protocol = ATA_PROT_NODATA;
4634 tf.nsect = sectors;
4635 tf.device |= (heads - 1) & 0x0f; /* max head = num. of heads - 1 */
4637 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
4638 /* A clean abort indicates an original or just out of spec drive
4639 and we should continue as we issue the setup based on the
4640 drive reported working geometry */
4641 if (err_mask == AC_ERR_DEV && (tf.feature & ATA_ABORTED))
4642 err_mask = 0;
4644 DPRINTK("EXIT, err_mask=%x\n", err_mask);
4645 return err_mask;
4649 * atapi_check_dma - Check whether ATAPI DMA can be supported
4650 * @qc: Metadata associated with taskfile to check
4652 * Allow low-level driver to filter ATA PACKET commands, returning
4653 * a status indicating whether or not it is OK to use DMA for the
4654 * supplied PACKET command.
4656 * LOCKING:
4657 * spin_lock_irqsave(host lock)
4659 * RETURNS: 0 when ATAPI DMA can be used
4660 * nonzero otherwise
4662 int atapi_check_dma(struct ata_queued_cmd *qc)
4664 struct ata_port *ap = qc->ap;
4666 /* Don't allow DMA if it isn't multiple of 16 bytes. Quite a
4667 * few ATAPI devices choke on such DMA requests.
4669 if (!(qc->dev->horkage & ATA_HORKAGE_ATAPI_MOD16_DMA) &&
4670 unlikely(qc->nbytes & 15))
4671 return 1;
4673 if (ap->ops->check_atapi_dma)
4674 return ap->ops->check_atapi_dma(qc);
4676 return 0;
4680 * ata_std_qc_defer - Check whether a qc needs to be deferred
4681 * @qc: ATA command in question
4683 * Non-NCQ commands cannot run with any other command, NCQ or
4684 * not. As upper layer only knows the queue depth, we are
4685 * responsible for maintaining exclusion. This function checks
4686 * whether a new command @qc can be issued.
4688 * LOCKING:
4689 * spin_lock_irqsave(host lock)
4691 * RETURNS:
4692 * ATA_DEFER_* if deferring is needed, 0 otherwise.
4694 int ata_std_qc_defer(struct ata_queued_cmd *qc)
4696 struct ata_link *link = qc->dev->link;
4698 if (ata_is_ncq(qc->tf.protocol)) {
4699 if (!ata_tag_valid(link->active_tag))
4700 return 0;
4701 } else {
4702 if (!ata_tag_valid(link->active_tag) && !link->sactive)
4703 return 0;
4706 return ATA_DEFER_LINK;
4708 EXPORT_SYMBOL_GPL(ata_std_qc_defer);
4710 enum ata_completion_errors ata_noop_qc_prep(struct ata_queued_cmd *qc)
4712 return AC_ERR_OK;
4714 EXPORT_SYMBOL_GPL(ata_noop_qc_prep);
4717 * ata_sg_init - Associate command with scatter-gather table.
4718 * @qc: Command to be associated
4719 * @sg: Scatter-gather table.
4720 * @n_elem: Number of elements in s/g table.
4722 * Initialize the data-related elements of queued_cmd @qc
4723 * to point to a scatter-gather table @sg, containing @n_elem
4724 * elements.
4726 * LOCKING:
4727 * spin_lock_irqsave(host lock)
4729 void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
4730 unsigned int n_elem)
4732 qc->sg = sg;
4733 qc->n_elem = n_elem;
4734 qc->cursg = qc->sg;
4737 #ifdef CONFIG_HAS_DMA
4740 * ata_sg_clean - Unmap DMA memory associated with command
4741 * @qc: Command containing DMA memory to be released
4743 * Unmap all mapped DMA memory associated with this command.
4745 * LOCKING:
4746 * spin_lock_irqsave(host lock)
4748 static void ata_sg_clean(struct ata_queued_cmd *qc)
4750 struct ata_port *ap = qc->ap;
4751 struct scatterlist *sg = qc->sg;
4752 int dir = qc->dma_dir;
4754 WARN_ON_ONCE(sg == NULL);
4756 VPRINTK("unmapping %u sg elements\n", qc->n_elem);
4758 if (qc->n_elem)
4759 dma_unmap_sg(ap->dev, sg, qc->orig_n_elem, dir);
4761 qc->flags &= ~ATA_QCFLAG_DMAMAP;
4762 qc->sg = NULL;
4766 * ata_sg_setup - DMA-map the scatter-gather table associated with a command.
4767 * @qc: Command with scatter-gather table to be mapped.
4769 * DMA-map the scatter-gather table associated with queued_cmd @qc.
4771 * LOCKING:
4772 * spin_lock_irqsave(host lock)
4774 * RETURNS:
4775 * Zero on success, negative on error.
4778 static int ata_sg_setup(struct ata_queued_cmd *qc)
4780 struct ata_port *ap = qc->ap;
4781 unsigned int n_elem;
4783 VPRINTK("ENTER, ata%u\n", ap->print_id);
4785 n_elem = dma_map_sg(ap->dev, qc->sg, qc->n_elem, qc->dma_dir);
4786 if (n_elem < 1)
4787 return -1;
4789 VPRINTK("%d sg elements mapped\n", n_elem);
4790 qc->orig_n_elem = qc->n_elem;
4791 qc->n_elem = n_elem;
4792 qc->flags |= ATA_QCFLAG_DMAMAP;
4794 return 0;
4797 #else /* !CONFIG_HAS_DMA */
4799 static inline void ata_sg_clean(struct ata_queued_cmd *qc) {}
4800 static inline int ata_sg_setup(struct ata_queued_cmd *qc) { return -1; }
4802 #endif /* !CONFIG_HAS_DMA */
4805 * swap_buf_le16 - swap halves of 16-bit words in place
4806 * @buf: Buffer to swap
4807 * @buf_words: Number of 16-bit words in buffer.
4809 * Swap halves of 16-bit words if needed to convert from
4810 * little-endian byte order to native cpu byte order, or
4811 * vice-versa.
4813 * LOCKING:
4814 * Inherited from caller.
4816 void swap_buf_le16(u16 *buf, unsigned int buf_words)
4818 #ifdef __BIG_ENDIAN
4819 unsigned int i;
4821 for (i = 0; i < buf_words; i++)
4822 buf[i] = le16_to_cpu(buf[i]);
4823 #endif /* __BIG_ENDIAN */
4827 * ata_qc_new_init - Request an available ATA command, and initialize it
4828 * @dev: Device from whom we request an available command structure
4829 * @tag: tag
4831 * LOCKING:
4832 * None.
4835 struct ata_queued_cmd *ata_qc_new_init(struct ata_device *dev, int tag)
4837 struct ata_port *ap = dev->link->ap;
4838 struct ata_queued_cmd *qc;
4840 /* no command while frozen */
4841 if (unlikely(ap->pflags & ATA_PFLAG_FROZEN))
4842 return NULL;
4844 /* libsas case */
4845 if (ap->flags & ATA_FLAG_SAS_HOST) {
4846 tag = ata_sas_allocate_tag(ap);
4847 if (tag < 0)
4848 return NULL;
4851 qc = __ata_qc_from_tag(ap, tag);
4852 qc->tag = qc->hw_tag = tag;
4853 qc->scsicmd = NULL;
4854 qc->ap = ap;
4855 qc->dev = dev;
4857 ata_qc_reinit(qc);
4859 return qc;
4863 * ata_qc_free - free unused ata_queued_cmd
4864 * @qc: Command to complete
4866 * Designed to free unused ata_queued_cmd object
4867 * in case something prevents using it.
4869 * LOCKING:
4870 * spin_lock_irqsave(host lock)
4872 void ata_qc_free(struct ata_queued_cmd *qc)
4874 struct ata_port *ap;
4875 unsigned int tag;
4877 WARN_ON_ONCE(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
4878 ap = qc->ap;
4880 qc->flags = 0;
4881 tag = qc->tag;
4882 if (ata_tag_valid(tag)) {
4883 qc->tag = ATA_TAG_POISON;
4884 if (ap->flags & ATA_FLAG_SAS_HOST)
4885 ata_sas_free_tag(tag, ap);
4889 void __ata_qc_complete(struct ata_queued_cmd *qc)
4891 struct ata_port *ap;
4892 struct ata_link *link;
4894 WARN_ON_ONCE(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
4895 WARN_ON_ONCE(!(qc->flags & ATA_QCFLAG_ACTIVE));
4896 ap = qc->ap;
4897 link = qc->dev->link;
4899 if (likely(qc->flags & ATA_QCFLAG_DMAMAP))
4900 ata_sg_clean(qc);
4902 /* command should be marked inactive atomically with qc completion */
4903 if (ata_is_ncq(qc->tf.protocol)) {
4904 link->sactive &= ~(1 << qc->hw_tag);
4905 if (!link->sactive)
4906 ap->nr_active_links--;
4907 } else {
4908 link->active_tag = ATA_TAG_POISON;
4909 ap->nr_active_links--;
4912 /* clear exclusive status */
4913 if (unlikely(qc->flags & ATA_QCFLAG_CLEAR_EXCL &&
4914 ap->excl_link == link))
4915 ap->excl_link = NULL;
4917 /* atapi: mark qc as inactive to prevent the interrupt handler
4918 * from completing the command twice later, before the error handler
4919 * is called. (when rc != 0 and atapi request sense is needed)
4921 qc->flags &= ~ATA_QCFLAG_ACTIVE;
4922 ap->qc_active &= ~(1ULL << qc->tag);
4924 /* call completion callback */
4925 qc->complete_fn(qc);
4928 static void fill_result_tf(struct ata_queued_cmd *qc)
4930 struct ata_port *ap = qc->ap;
4932 qc->result_tf.flags = qc->tf.flags;
4933 ap->ops->qc_fill_rtf(qc);
4936 static void ata_verify_xfer(struct ata_queued_cmd *qc)
4938 struct ata_device *dev = qc->dev;
4940 if (!ata_is_data(qc->tf.protocol))
4941 return;
4943 if ((dev->mwdma_mask || dev->udma_mask) && ata_is_pio(qc->tf.protocol))
4944 return;
4946 dev->flags &= ~ATA_DFLAG_DUBIOUS_XFER;
4950 * ata_qc_complete - Complete an active ATA command
4951 * @qc: Command to complete
4953 * Indicate to the mid and upper layers that an ATA command has
4954 * completed, with either an ok or not-ok status.
4956 * Refrain from calling this function multiple times when
4957 * successfully completing multiple NCQ commands.
4958 * ata_qc_complete_multiple() should be used instead, which will
4959 * properly update IRQ expect state.
4961 * LOCKING:
4962 * spin_lock_irqsave(host lock)
4964 void ata_qc_complete(struct ata_queued_cmd *qc)
4966 struct ata_port *ap = qc->ap;
4968 /* Trigger the LED (if available) */
4969 ledtrig_disk_activity(!!(qc->tf.flags & ATA_TFLAG_WRITE));
4971 /* XXX: New EH and old EH use different mechanisms to
4972 * synchronize EH with regular execution path.
4974 * In new EH, a failed qc is marked with ATA_QCFLAG_FAILED.
4975 * Normal execution path is responsible for not accessing a
4976 * failed qc. libata core enforces the rule by returning NULL
4977 * from ata_qc_from_tag() for failed qcs.
4979 * Old EH depends on ata_qc_complete() nullifying completion
4980 * requests if ATA_QCFLAG_EH_SCHEDULED is set. Old EH does
4981 * not synchronize with interrupt handler. Only PIO task is
4982 * taken care of.
4984 if (ap->ops->error_handler) {
4985 struct ata_device *dev = qc->dev;
4986 struct ata_eh_info *ehi = &dev->link->eh_info;
4988 if (unlikely(qc->err_mask))
4989 qc->flags |= ATA_QCFLAG_FAILED;
4992 * Finish internal commands without any further processing
4993 * and always with the result TF filled.
4995 if (unlikely(ata_tag_internal(qc->tag))) {
4996 fill_result_tf(qc);
4997 trace_ata_qc_complete_internal(qc);
4998 __ata_qc_complete(qc);
4999 return;
5003 * Non-internal qc has failed. Fill the result TF and
5004 * summon EH.
5006 if (unlikely(qc->flags & ATA_QCFLAG_FAILED)) {
5007 fill_result_tf(qc);
5008 trace_ata_qc_complete_failed(qc);
5009 ata_qc_schedule_eh(qc);
5010 return;
5013 WARN_ON_ONCE(ap->pflags & ATA_PFLAG_FROZEN);
5015 /* read result TF if requested */
5016 if (qc->flags & ATA_QCFLAG_RESULT_TF)
5017 fill_result_tf(qc);
5019 trace_ata_qc_complete_done(qc);
5020 /* Some commands need post-processing after successful
5021 * completion.
5023 switch (qc->tf.command) {
5024 case ATA_CMD_SET_FEATURES:
5025 if (qc->tf.feature != SETFEATURES_WC_ON &&
5026 qc->tf.feature != SETFEATURES_WC_OFF &&
5027 qc->tf.feature != SETFEATURES_RA_ON &&
5028 qc->tf.feature != SETFEATURES_RA_OFF)
5029 break;
5030 /* fall through */
5031 case ATA_CMD_INIT_DEV_PARAMS: /* CHS translation changed */
5032 case ATA_CMD_SET_MULTI: /* multi_count changed */
5033 /* revalidate device */
5034 ehi->dev_action[dev->devno] |= ATA_EH_REVALIDATE;
5035 ata_port_schedule_eh(ap);
5036 break;
5038 case ATA_CMD_SLEEP:
5039 dev->flags |= ATA_DFLAG_SLEEPING;
5040 break;
5043 if (unlikely(dev->flags & ATA_DFLAG_DUBIOUS_XFER))
5044 ata_verify_xfer(qc);
5046 __ata_qc_complete(qc);
5047 } else {
5048 if (qc->flags & ATA_QCFLAG_EH_SCHEDULED)
5049 return;
5051 /* read result TF if failed or requested */
5052 if (qc->err_mask || qc->flags & ATA_QCFLAG_RESULT_TF)
5053 fill_result_tf(qc);
5055 __ata_qc_complete(qc);
5058 EXPORT_SYMBOL_GPL(ata_qc_complete);
5061 * ata_qc_get_active - get bitmask of active qcs
5062 * @ap: port in question
5064 * LOCKING:
5065 * spin_lock_irqsave(host lock)
5067 * RETURNS:
5068 * Bitmask of active qcs
5070 u64 ata_qc_get_active(struct ata_port *ap)
5072 u64 qc_active = ap->qc_active;
5074 /* ATA_TAG_INTERNAL is sent to hw as tag 0 */
5075 if (qc_active & (1ULL << ATA_TAG_INTERNAL)) {
5076 qc_active |= (1 << 0);
5077 qc_active &= ~(1ULL << ATA_TAG_INTERNAL);
5080 return qc_active;
5082 EXPORT_SYMBOL_GPL(ata_qc_get_active);
5085 * ata_qc_complete_multiple - Complete multiple qcs successfully
5086 * @ap: port in question
5087 * @qc_active: new qc_active mask
5089 * Complete in-flight commands. This functions is meant to be
5090 * called from low-level driver's interrupt routine to complete
5091 * requests normally. ap->qc_active and @qc_active is compared
5092 * and commands are completed accordingly.
5094 * Always use this function when completing multiple NCQ commands
5095 * from IRQ handlers instead of calling ata_qc_complete()
5096 * multiple times to keep IRQ expect status properly in sync.
5098 * LOCKING:
5099 * spin_lock_irqsave(host lock)
5101 * RETURNS:
5102 * Number of completed commands on success, -errno otherwise.
5104 int ata_qc_complete_multiple(struct ata_port *ap, u64 qc_active)
5106 u64 done_mask, ap_qc_active = ap->qc_active;
5107 int nr_done = 0;
5110 * If the internal tag is set on ap->qc_active, then we care about
5111 * bit0 on the passed in qc_active mask. Move that bit up to match
5112 * the internal tag.
5114 if (ap_qc_active & (1ULL << ATA_TAG_INTERNAL)) {
5115 qc_active |= (qc_active & 0x01) << ATA_TAG_INTERNAL;
5116 qc_active ^= qc_active & 0x01;
5119 done_mask = ap_qc_active ^ qc_active;
5121 if (unlikely(done_mask & qc_active)) {
5122 ata_port_err(ap, "illegal qc_active transition (%08llx->%08llx)\n",
5123 ap->qc_active, qc_active);
5124 return -EINVAL;
5127 while (done_mask) {
5128 struct ata_queued_cmd *qc;
5129 unsigned int tag = __ffs64(done_mask);
5131 qc = ata_qc_from_tag(ap, tag);
5132 if (qc) {
5133 ata_qc_complete(qc);
5134 nr_done++;
5136 done_mask &= ~(1ULL << tag);
5139 return nr_done;
5141 EXPORT_SYMBOL_GPL(ata_qc_complete_multiple);
5144 * ata_qc_issue - issue taskfile to device
5145 * @qc: command to issue to device
5147 * Prepare an ATA command to submission to device.
5148 * This includes mapping the data into a DMA-able
5149 * area, filling in the S/G table, and finally
5150 * writing the taskfile to hardware, starting the command.
5152 * LOCKING:
5153 * spin_lock_irqsave(host lock)
5155 void ata_qc_issue(struct ata_queued_cmd *qc)
5157 struct ata_port *ap = qc->ap;
5158 struct ata_link *link = qc->dev->link;
5159 u8 prot = qc->tf.protocol;
5161 /* Make sure only one non-NCQ command is outstanding. The
5162 * check is skipped for old EH because it reuses active qc to
5163 * request ATAPI sense.
5165 WARN_ON_ONCE(ap->ops->error_handler && ata_tag_valid(link->active_tag));
5167 if (ata_is_ncq(prot)) {
5168 WARN_ON_ONCE(link->sactive & (1 << qc->hw_tag));
5170 if (!link->sactive)
5171 ap->nr_active_links++;
5172 link->sactive |= 1 << qc->hw_tag;
5173 } else {
5174 WARN_ON_ONCE(link->sactive);
5176 ap->nr_active_links++;
5177 link->active_tag = qc->tag;
5180 qc->flags |= ATA_QCFLAG_ACTIVE;
5181 ap->qc_active |= 1ULL << qc->tag;
5184 * We guarantee to LLDs that they will have at least one
5185 * non-zero sg if the command is a data command.
5187 if (ata_is_data(prot) && (!qc->sg || !qc->n_elem || !qc->nbytes))
5188 goto sys_err;
5190 if (ata_is_dma(prot) || (ata_is_pio(prot) &&
5191 (ap->flags & ATA_FLAG_PIO_DMA)))
5192 if (ata_sg_setup(qc))
5193 goto sys_err;
5195 /* if device is sleeping, schedule reset and abort the link */
5196 if (unlikely(qc->dev->flags & ATA_DFLAG_SLEEPING)) {
5197 link->eh_info.action |= ATA_EH_RESET;
5198 ata_ehi_push_desc(&link->eh_info, "waking up from sleep");
5199 ata_link_abort(link);
5200 return;
5203 qc->err_mask |= ap->ops->qc_prep(qc);
5204 if (unlikely(qc->err_mask))
5205 goto err;
5206 trace_ata_qc_issue(qc);
5207 qc->err_mask |= ap->ops->qc_issue(qc);
5208 if (unlikely(qc->err_mask))
5209 goto err;
5210 return;
5212 sys_err:
5213 qc->err_mask |= AC_ERR_SYSTEM;
5214 err:
5215 ata_qc_complete(qc);
5219 * sata_scr_valid - test whether SCRs are accessible
5220 * @link: ATA link to test SCR accessibility for
5222 * Test whether SCRs are accessible for @link.
5224 * LOCKING:
5225 * None.
5227 * RETURNS:
5228 * 1 if SCRs are accessible, 0 otherwise.
5230 int sata_scr_valid(struct ata_link *link)
5232 struct ata_port *ap = link->ap;
5234 return (ap->flags & ATA_FLAG_SATA) && ap->ops->scr_read;
5236 EXPORT_SYMBOL_GPL(sata_scr_valid);
5239 * sata_scr_read - read SCR register of the specified port
5240 * @link: ATA link to read SCR for
5241 * @reg: SCR to read
5242 * @val: Place to store read value
5244 * Read SCR register @reg of @link into *@val. This function is
5245 * guaranteed to succeed if @link is ap->link, the cable type of
5246 * the port is SATA and the port implements ->scr_read.
5248 * LOCKING:
5249 * None if @link is ap->link. Kernel thread context otherwise.
5251 * RETURNS:
5252 * 0 on success, negative errno on failure.
5254 int sata_scr_read(struct ata_link *link, int reg, u32 *val)
5256 if (ata_is_host_link(link)) {
5257 if (sata_scr_valid(link))
5258 return link->ap->ops->scr_read(link, reg, val);
5259 return -EOPNOTSUPP;
5262 return sata_pmp_scr_read(link, reg, val);
5264 EXPORT_SYMBOL_GPL(sata_scr_read);
5267 * sata_scr_write - write SCR register of the specified port
5268 * @link: ATA link to write SCR for
5269 * @reg: SCR to write
5270 * @val: value to write
5272 * Write @val to SCR register @reg of @link. This function is
5273 * guaranteed to succeed if @link is ap->link, the cable type of
5274 * the port is SATA and the port implements ->scr_read.
5276 * LOCKING:
5277 * None if @link is ap->link. Kernel thread context otherwise.
5279 * RETURNS:
5280 * 0 on success, negative errno on failure.
5282 int sata_scr_write(struct ata_link *link, int reg, u32 val)
5284 if (ata_is_host_link(link)) {
5285 if (sata_scr_valid(link))
5286 return link->ap->ops->scr_write(link, reg, val);
5287 return -EOPNOTSUPP;
5290 return sata_pmp_scr_write(link, reg, val);
5292 EXPORT_SYMBOL_GPL(sata_scr_write);
5295 * sata_scr_write_flush - write SCR register of the specified port and flush
5296 * @link: ATA link to write SCR for
5297 * @reg: SCR to write
5298 * @val: value to write
5300 * This function is identical to sata_scr_write() except that this
5301 * function performs flush after writing to the register.
5303 * LOCKING:
5304 * None if @link is ap->link. Kernel thread context otherwise.
5306 * RETURNS:
5307 * 0 on success, negative errno on failure.
5309 int sata_scr_write_flush(struct ata_link *link, int reg, u32 val)
5311 if (ata_is_host_link(link)) {
5312 int rc;
5314 if (sata_scr_valid(link)) {
5315 rc = link->ap->ops->scr_write(link, reg, val);
5316 if (rc == 0)
5317 rc = link->ap->ops->scr_read(link, reg, &val);
5318 return rc;
5320 return -EOPNOTSUPP;
5323 return sata_pmp_scr_write(link, reg, val);
5325 EXPORT_SYMBOL_GPL(sata_scr_write_flush);
5328 * ata_phys_link_online - test whether the given link is online
5329 * @link: ATA link to test
5331 * Test whether @link is online. Note that this function returns
5332 * 0 if online status of @link cannot be obtained, so
5333 * ata_link_online(link) != !ata_link_offline(link).
5335 * LOCKING:
5336 * None.
5338 * RETURNS:
5339 * True if the port online status is available and online.
5341 bool ata_phys_link_online(struct ata_link *link)
5343 u32 sstatus;
5345 if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0 &&
5346 ata_sstatus_online(sstatus))
5347 return true;
5348 return false;
5352 * ata_phys_link_offline - test whether the given link is offline
5353 * @link: ATA link to test
5355 * Test whether @link is offline. Note that this function
5356 * returns 0 if offline status of @link cannot be obtained, so
5357 * ata_link_online(link) != !ata_link_offline(link).
5359 * LOCKING:
5360 * None.
5362 * RETURNS:
5363 * True if the port offline status is available and offline.
5365 bool ata_phys_link_offline(struct ata_link *link)
5367 u32 sstatus;
5369 if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0 &&
5370 !ata_sstatus_online(sstatus))
5371 return true;
5372 return false;
5376 * ata_link_online - test whether the given link is online
5377 * @link: ATA link to test
5379 * Test whether @link is online. This is identical to
5380 * ata_phys_link_online() when there's no slave link. When
5381 * there's a slave link, this function should only be called on
5382 * the master link and will return true if any of M/S links is
5383 * online.
5385 * LOCKING:
5386 * None.
5388 * RETURNS:
5389 * True if the port online status is available and online.
5391 bool ata_link_online(struct ata_link *link)
5393 struct ata_link *slave = link->ap->slave_link;
5395 WARN_ON(link == slave); /* shouldn't be called on slave link */
5397 return ata_phys_link_online(link) ||
5398 (slave && ata_phys_link_online(slave));
5400 EXPORT_SYMBOL_GPL(ata_link_online);
5403 * ata_link_offline - test whether the given link is offline
5404 * @link: ATA link to test
5406 * Test whether @link is offline. This is identical to
5407 * ata_phys_link_offline() when there's no slave link. When
5408 * there's a slave link, this function should only be called on
5409 * the master link and will return true if both M/S links are
5410 * offline.
5412 * LOCKING:
5413 * None.
5415 * RETURNS:
5416 * True if the port offline status is available and offline.
5418 bool ata_link_offline(struct ata_link *link)
5420 struct ata_link *slave = link->ap->slave_link;
5422 WARN_ON(link == slave); /* shouldn't be called on slave link */
5424 return ata_phys_link_offline(link) &&
5425 (!slave || ata_phys_link_offline(slave));
5427 EXPORT_SYMBOL_GPL(ata_link_offline);
5429 #ifdef CONFIG_PM
5430 static void ata_port_request_pm(struct ata_port *ap, pm_message_t mesg,
5431 unsigned int action, unsigned int ehi_flags,
5432 bool async)
5434 struct ata_link *link;
5435 unsigned long flags;
5437 /* Previous resume operation might still be in
5438 * progress. Wait for PM_PENDING to clear.
5440 if (ap->pflags & ATA_PFLAG_PM_PENDING) {
5441 ata_port_wait_eh(ap);
5442 WARN_ON(ap->pflags & ATA_PFLAG_PM_PENDING);
5445 /* request PM ops to EH */
5446 spin_lock_irqsave(ap->lock, flags);
5448 ap->pm_mesg = mesg;
5449 ap->pflags |= ATA_PFLAG_PM_PENDING;
5450 ata_for_each_link(link, ap, HOST_FIRST) {
5451 link->eh_info.action |= action;
5452 link->eh_info.flags |= ehi_flags;
5455 ata_port_schedule_eh(ap);
5457 spin_unlock_irqrestore(ap->lock, flags);
5459 if (!async) {
5460 ata_port_wait_eh(ap);
5461 WARN_ON(ap->pflags & ATA_PFLAG_PM_PENDING);
5466 * On some hardware, device fails to respond after spun down for suspend. As
5467 * the device won't be used before being resumed, we don't need to touch the
5468 * device. Ask EH to skip the usual stuff and proceed directly to suspend.
5470 * http://thread.gmane.org/gmane.linux.ide/46764
5472 static const unsigned int ata_port_suspend_ehi = ATA_EHI_QUIET
5473 | ATA_EHI_NO_AUTOPSY
5474 | ATA_EHI_NO_RECOVERY;
5476 static void ata_port_suspend(struct ata_port *ap, pm_message_t mesg)
5478 ata_port_request_pm(ap, mesg, 0, ata_port_suspend_ehi, false);
5481 static void ata_port_suspend_async(struct ata_port *ap, pm_message_t mesg)
5483 ata_port_request_pm(ap, mesg, 0, ata_port_suspend_ehi, true);
5486 static int ata_port_pm_suspend(struct device *dev)
5488 struct ata_port *ap = to_ata_port(dev);
5490 if (pm_runtime_suspended(dev))
5491 return 0;
5493 ata_port_suspend(ap, PMSG_SUSPEND);
5494 return 0;
5497 static int ata_port_pm_freeze(struct device *dev)
5499 struct ata_port *ap = to_ata_port(dev);
5501 if (pm_runtime_suspended(dev))
5502 return 0;
5504 ata_port_suspend(ap, PMSG_FREEZE);
5505 return 0;
5508 static int ata_port_pm_poweroff(struct device *dev)
5510 ata_port_suspend(to_ata_port(dev), PMSG_HIBERNATE);
5511 return 0;
5514 static const unsigned int ata_port_resume_ehi = ATA_EHI_NO_AUTOPSY
5515 | ATA_EHI_QUIET;
5517 static void ata_port_resume(struct ata_port *ap, pm_message_t mesg)
5519 ata_port_request_pm(ap, mesg, ATA_EH_RESET, ata_port_resume_ehi, false);
5522 static void ata_port_resume_async(struct ata_port *ap, pm_message_t mesg)
5524 ata_port_request_pm(ap, mesg, ATA_EH_RESET, ata_port_resume_ehi, true);
5527 static int ata_port_pm_resume(struct device *dev)
5529 ata_port_resume_async(to_ata_port(dev), PMSG_RESUME);
5530 pm_runtime_disable(dev);
5531 pm_runtime_set_active(dev);
5532 pm_runtime_enable(dev);
5533 return 0;
5537 * For ODDs, the upper layer will poll for media change every few seconds,
5538 * which will make it enter and leave suspend state every few seconds. And
5539 * as each suspend will cause a hard/soft reset, the gain of runtime suspend
5540 * is very little and the ODD may malfunction after constantly being reset.
5541 * So the idle callback here will not proceed to suspend if a non-ZPODD capable
5542 * ODD is attached to the port.
5544 static int ata_port_runtime_idle(struct device *dev)
5546 struct ata_port *ap = to_ata_port(dev);
5547 struct ata_link *link;
5548 struct ata_device *adev;
5550 ata_for_each_link(link, ap, HOST_FIRST) {
5551 ata_for_each_dev(adev, link, ENABLED)
5552 if (adev->class == ATA_DEV_ATAPI &&
5553 !zpodd_dev_enabled(adev))
5554 return -EBUSY;
5557 return 0;
5560 static int ata_port_runtime_suspend(struct device *dev)
5562 ata_port_suspend(to_ata_port(dev), PMSG_AUTO_SUSPEND);
5563 return 0;
5566 static int ata_port_runtime_resume(struct device *dev)
5568 ata_port_resume(to_ata_port(dev), PMSG_AUTO_RESUME);
5569 return 0;
5572 static const struct dev_pm_ops ata_port_pm_ops = {
5573 .suspend = ata_port_pm_suspend,
5574 .resume = ata_port_pm_resume,
5575 .freeze = ata_port_pm_freeze,
5576 .thaw = ata_port_pm_resume,
5577 .poweroff = ata_port_pm_poweroff,
5578 .restore = ata_port_pm_resume,
5580 .runtime_suspend = ata_port_runtime_suspend,
5581 .runtime_resume = ata_port_runtime_resume,
5582 .runtime_idle = ata_port_runtime_idle,
5585 /* sas ports don't participate in pm runtime management of ata_ports,
5586 * and need to resume ata devices at the domain level, not the per-port
5587 * level. sas suspend/resume is async to allow parallel port recovery
5588 * since sas has multiple ata_port instances per Scsi_Host.
5590 void ata_sas_port_suspend(struct ata_port *ap)
5592 ata_port_suspend_async(ap, PMSG_SUSPEND);
5594 EXPORT_SYMBOL_GPL(ata_sas_port_suspend);
5596 void ata_sas_port_resume(struct ata_port *ap)
5598 ata_port_resume_async(ap, PMSG_RESUME);
5600 EXPORT_SYMBOL_GPL(ata_sas_port_resume);
5603 * ata_host_suspend - suspend host
5604 * @host: host to suspend
5605 * @mesg: PM message
5607 * Suspend @host. Actual operation is performed by port suspend.
5609 int ata_host_suspend(struct ata_host *host, pm_message_t mesg)
5611 host->dev->power.power_state = mesg;
5612 return 0;
5614 EXPORT_SYMBOL_GPL(ata_host_suspend);
5617 * ata_host_resume - resume host
5618 * @host: host to resume
5620 * Resume @host. Actual operation is performed by port resume.
5622 void ata_host_resume(struct ata_host *host)
5624 host->dev->power.power_state = PMSG_ON;
5626 EXPORT_SYMBOL_GPL(ata_host_resume);
5627 #endif
5629 const struct device_type ata_port_type = {
5630 .name = "ata_port",
5631 #ifdef CONFIG_PM
5632 .pm = &ata_port_pm_ops,
5633 #endif
5637 * ata_dev_init - Initialize an ata_device structure
5638 * @dev: Device structure to initialize
5640 * Initialize @dev in preparation for probing.
5642 * LOCKING:
5643 * Inherited from caller.
5645 void ata_dev_init(struct ata_device *dev)
5647 struct ata_link *link = ata_dev_phys_link(dev);
5648 struct ata_port *ap = link->ap;
5649 unsigned long flags;
5651 /* SATA spd limit is bound to the attached device, reset together */
5652 link->sata_spd_limit = link->hw_sata_spd_limit;
5653 link->sata_spd = 0;
5655 /* High bits of dev->flags are used to record warm plug
5656 * requests which occur asynchronously. Synchronize using
5657 * host lock.
5659 spin_lock_irqsave(ap->lock, flags);
5660 dev->flags &= ~ATA_DFLAG_INIT_MASK;
5661 dev->horkage = 0;
5662 spin_unlock_irqrestore(ap->lock, flags);
5664 memset((void *)dev + ATA_DEVICE_CLEAR_BEGIN, 0,
5665 ATA_DEVICE_CLEAR_END - ATA_DEVICE_CLEAR_BEGIN);
5666 dev->pio_mask = UINT_MAX;
5667 dev->mwdma_mask = UINT_MAX;
5668 dev->udma_mask = UINT_MAX;
5672 * ata_link_init - Initialize an ata_link structure
5673 * @ap: ATA port link is attached to
5674 * @link: Link structure to initialize
5675 * @pmp: Port multiplier port number
5677 * Initialize @link.
5679 * LOCKING:
5680 * Kernel thread context (may sleep)
5682 void ata_link_init(struct ata_port *ap, struct ata_link *link, int pmp)
5684 int i;
5686 /* clear everything except for devices */
5687 memset((void *)link + ATA_LINK_CLEAR_BEGIN, 0,
5688 ATA_LINK_CLEAR_END - ATA_LINK_CLEAR_BEGIN);
5690 link->ap = ap;
5691 link->pmp = pmp;
5692 link->active_tag = ATA_TAG_POISON;
5693 link->hw_sata_spd_limit = UINT_MAX;
5695 /* can't use iterator, ap isn't initialized yet */
5696 for (i = 0; i < ATA_MAX_DEVICES; i++) {
5697 struct ata_device *dev = &link->device[i];
5699 dev->link = link;
5700 dev->devno = dev - link->device;
5701 #ifdef CONFIG_ATA_ACPI
5702 dev->gtf_filter = ata_acpi_gtf_filter;
5703 #endif
5704 ata_dev_init(dev);
5709 * sata_link_init_spd - Initialize link->sata_spd_limit
5710 * @link: Link to configure sata_spd_limit for
5712 * Initialize @link->[hw_]sata_spd_limit to the currently
5713 * configured value.
5715 * LOCKING:
5716 * Kernel thread context (may sleep).
5718 * RETURNS:
5719 * 0 on success, -errno on failure.
5721 int sata_link_init_spd(struct ata_link *link)
5723 u8 spd;
5724 int rc;
5726 rc = sata_scr_read(link, SCR_CONTROL, &link->saved_scontrol);
5727 if (rc)
5728 return rc;
5730 spd = (link->saved_scontrol >> 4) & 0xf;
5731 if (spd)
5732 link->hw_sata_spd_limit &= (1 << spd) - 1;
5734 ata_force_link_limits(link);
5736 link->sata_spd_limit = link->hw_sata_spd_limit;
5738 return 0;
5742 * ata_port_alloc - allocate and initialize basic ATA port resources
5743 * @host: ATA host this allocated port belongs to
5745 * Allocate and initialize basic ATA port resources.
5747 * RETURNS:
5748 * Allocate ATA port on success, NULL on failure.
5750 * LOCKING:
5751 * Inherited from calling layer (may sleep).
5753 struct ata_port *ata_port_alloc(struct ata_host *host)
5755 struct ata_port *ap;
5757 DPRINTK("ENTER\n");
5759 ap = kzalloc(sizeof(*ap), GFP_KERNEL);
5760 if (!ap)
5761 return NULL;
5763 ap->pflags |= ATA_PFLAG_INITIALIZING | ATA_PFLAG_FROZEN;
5764 ap->lock = &host->lock;
5765 ap->print_id = -1;
5766 ap->local_port_no = -1;
5767 ap->host = host;
5768 ap->dev = host->dev;
5770 #if defined(ATA_VERBOSE_DEBUG)
5771 /* turn on all debugging levels */
5772 ap->msg_enable = 0x00FF;
5773 #elif defined(ATA_DEBUG)
5774 ap->msg_enable = ATA_MSG_DRV | ATA_MSG_INFO | ATA_MSG_CTL | ATA_MSG_WARN | ATA_MSG_ERR;
5775 #else
5776 ap->msg_enable = ATA_MSG_DRV | ATA_MSG_ERR | ATA_MSG_WARN;
5777 #endif
5779 mutex_init(&ap->scsi_scan_mutex);
5780 INIT_DELAYED_WORK(&ap->hotplug_task, ata_scsi_hotplug);
5781 INIT_WORK(&ap->scsi_rescan_task, ata_scsi_dev_rescan);
5782 INIT_LIST_HEAD(&ap->eh_done_q);
5783 init_waitqueue_head(&ap->eh_wait_q);
5784 init_completion(&ap->park_req_pending);
5785 timer_setup(&ap->fastdrain_timer, ata_eh_fastdrain_timerfn,
5786 TIMER_DEFERRABLE);
5788 ap->cbl = ATA_CBL_NONE;
5790 ata_link_init(ap, &ap->link, 0);
5792 #ifdef ATA_IRQ_TRAP
5793 ap->stats.unhandled_irq = 1;
5794 ap->stats.idle_irq = 1;
5795 #endif
5796 ata_sff_port_init(ap);
5798 return ap;
5801 static void ata_devres_release(struct device *gendev, void *res)
5803 struct ata_host *host = dev_get_drvdata(gendev);
5804 int i;
5806 for (i = 0; i < host->n_ports; i++) {
5807 struct ata_port *ap = host->ports[i];
5809 if (!ap)
5810 continue;
5812 if (ap->scsi_host)
5813 scsi_host_put(ap->scsi_host);
5817 dev_set_drvdata(gendev, NULL);
5818 ata_host_put(host);
5821 static void ata_host_release(struct kref *kref)
5823 struct ata_host *host = container_of(kref, struct ata_host, kref);
5824 int i;
5826 for (i = 0; i < host->n_ports; i++) {
5827 struct ata_port *ap = host->ports[i];
5829 kfree(ap->pmp_link);
5830 kfree(ap->slave_link);
5831 kfree(ap);
5832 host->ports[i] = NULL;
5834 kfree(host);
5837 void ata_host_get(struct ata_host *host)
5839 kref_get(&host->kref);
5842 void ata_host_put(struct ata_host *host)
5844 kref_put(&host->kref, ata_host_release);
5846 EXPORT_SYMBOL_GPL(ata_host_put);
5849 * ata_host_alloc - allocate and init basic ATA host resources
5850 * @dev: generic device this host is associated with
5851 * @max_ports: maximum number of ATA ports associated with this host
5853 * Allocate and initialize basic ATA host resources. LLD calls
5854 * this function to allocate a host, initializes it fully and
5855 * attaches it using ata_host_register().
5857 * @max_ports ports are allocated and host->n_ports is
5858 * initialized to @max_ports. The caller is allowed to decrease
5859 * host->n_ports before calling ata_host_register(). The unused
5860 * ports will be automatically freed on registration.
5862 * RETURNS:
5863 * Allocate ATA host on success, NULL on failure.
5865 * LOCKING:
5866 * Inherited from calling layer (may sleep).
5868 struct ata_host *ata_host_alloc(struct device *dev, int max_ports)
5870 struct ata_host *host;
5871 size_t sz;
5872 int i;
5873 void *dr;
5875 DPRINTK("ENTER\n");
5877 /* alloc a container for our list of ATA ports (buses) */
5878 sz = sizeof(struct ata_host) + (max_ports + 1) * sizeof(void *);
5879 host = kzalloc(sz, GFP_KERNEL);
5880 if (!host)
5881 return NULL;
5883 if (!devres_open_group(dev, NULL, GFP_KERNEL))
5884 goto err_free;
5886 dr = devres_alloc(ata_devres_release, 0, GFP_KERNEL);
5887 if (!dr)
5888 goto err_out;
5890 devres_add(dev, dr);
5891 dev_set_drvdata(dev, host);
5893 spin_lock_init(&host->lock);
5894 mutex_init(&host->eh_mutex);
5895 host->dev = dev;
5896 host->n_ports = max_ports;
5897 kref_init(&host->kref);
5899 /* allocate ports bound to this host */
5900 for (i = 0; i < max_ports; i++) {
5901 struct ata_port *ap;
5903 ap = ata_port_alloc(host);
5904 if (!ap)
5905 goto err_out;
5907 ap->port_no = i;
5908 host->ports[i] = ap;
5911 devres_remove_group(dev, NULL);
5912 return host;
5914 err_out:
5915 devres_release_group(dev, NULL);
5916 err_free:
5917 kfree(host);
5918 return NULL;
5920 EXPORT_SYMBOL_GPL(ata_host_alloc);
5923 * ata_host_alloc_pinfo - alloc host and init with port_info array
5924 * @dev: generic device this host is associated with
5925 * @ppi: array of ATA port_info to initialize host with
5926 * @n_ports: number of ATA ports attached to this host
5928 * Allocate ATA host and initialize with info from @ppi. If NULL
5929 * terminated, @ppi may contain fewer entries than @n_ports. The
5930 * last entry will be used for the remaining ports.
5932 * RETURNS:
5933 * Allocate ATA host on success, NULL on failure.
5935 * LOCKING:
5936 * Inherited from calling layer (may sleep).
5938 struct ata_host *ata_host_alloc_pinfo(struct device *dev,
5939 const struct ata_port_info * const * ppi,
5940 int n_ports)
5942 const struct ata_port_info *pi;
5943 struct ata_host *host;
5944 int i, j;
5946 host = ata_host_alloc(dev, n_ports);
5947 if (!host)
5948 return NULL;
5950 for (i = 0, j = 0, pi = NULL; i < host->n_ports; i++) {
5951 struct ata_port *ap = host->ports[i];
5953 if (ppi[j])
5954 pi = ppi[j++];
5956 ap->pio_mask = pi->pio_mask;
5957 ap->mwdma_mask = pi->mwdma_mask;
5958 ap->udma_mask = pi->udma_mask;
5959 ap->flags |= pi->flags;
5960 ap->link.flags |= pi->link_flags;
5961 ap->ops = pi->port_ops;
5963 if (!host->ops && (pi->port_ops != &ata_dummy_port_ops))
5964 host->ops = pi->port_ops;
5967 return host;
5969 EXPORT_SYMBOL_GPL(ata_host_alloc_pinfo);
5971 static void ata_host_stop(struct device *gendev, void *res)
5973 struct ata_host *host = dev_get_drvdata(gendev);
5974 int i;
5976 WARN_ON(!(host->flags & ATA_HOST_STARTED));
5978 for (i = 0; i < host->n_ports; i++) {
5979 struct ata_port *ap = host->ports[i];
5981 if (ap->ops->port_stop)
5982 ap->ops->port_stop(ap);
5985 if (host->ops->host_stop)
5986 host->ops->host_stop(host);
5990 * ata_finalize_port_ops - finalize ata_port_operations
5991 * @ops: ata_port_operations to finalize
5993 * An ata_port_operations can inherit from another ops and that
5994 * ops can again inherit from another. This can go on as many
5995 * times as necessary as long as there is no loop in the
5996 * inheritance chain.
5998 * Ops tables are finalized when the host is started. NULL or
5999 * unspecified entries are inherited from the closet ancestor
6000 * which has the method and the entry is populated with it.
6001 * After finalization, the ops table directly points to all the
6002 * methods and ->inherits is no longer necessary and cleared.
6004 * Using ATA_OP_NULL, inheriting ops can force a method to NULL.
6006 * LOCKING:
6007 * None.
6009 static void ata_finalize_port_ops(struct ata_port_operations *ops)
6011 static DEFINE_SPINLOCK(lock);
6012 const struct ata_port_operations *cur;
6013 void **begin = (void **)ops;
6014 void **end = (void **)&ops->inherits;
6015 void **pp;
6017 if (!ops || !ops->inherits)
6018 return;
6020 spin_lock(&lock);
6022 for (cur = ops->inherits; cur; cur = cur->inherits) {
6023 void **inherit = (void **)cur;
6025 for (pp = begin; pp < end; pp++, inherit++)
6026 if (!*pp)
6027 *pp = *inherit;
6030 for (pp = begin; pp < end; pp++)
6031 if (IS_ERR(*pp))
6032 *pp = NULL;
6034 ops->inherits = NULL;
6036 spin_unlock(&lock);
6040 * ata_host_start - start and freeze ports of an ATA host
6041 * @host: ATA host to start ports for
6043 * Start and then freeze ports of @host. Started status is
6044 * recorded in host->flags, so this function can be called
6045 * multiple times. Ports are guaranteed to get started only
6046 * once. If host->ops isn't initialized yet, its set to the
6047 * first non-dummy port ops.
6049 * LOCKING:
6050 * Inherited from calling layer (may sleep).
6052 * RETURNS:
6053 * 0 if all ports are started successfully, -errno otherwise.
6055 int ata_host_start(struct ata_host *host)
6057 int have_stop = 0;
6058 void *start_dr = NULL;
6059 int i, rc;
6061 if (host->flags & ATA_HOST_STARTED)
6062 return 0;
6064 ata_finalize_port_ops(host->ops);
6066 for (i = 0; i < host->n_ports; i++) {
6067 struct ata_port *ap = host->ports[i];
6069 ata_finalize_port_ops(ap->ops);
6071 if (!host->ops && !ata_port_is_dummy(ap))
6072 host->ops = ap->ops;
6074 if (ap->ops->port_stop)
6075 have_stop = 1;
6078 if (host->ops->host_stop)
6079 have_stop = 1;
6081 if (have_stop) {
6082 start_dr = devres_alloc(ata_host_stop, 0, GFP_KERNEL);
6083 if (!start_dr)
6084 return -ENOMEM;
6087 for (i = 0; i < host->n_ports; i++) {
6088 struct ata_port *ap = host->ports[i];
6090 if (ap->ops->port_start) {
6091 rc = ap->ops->port_start(ap);
6092 if (rc) {
6093 if (rc != -ENODEV)
6094 dev_err(host->dev,
6095 "failed to start port %d (errno=%d)\n",
6096 i, rc);
6097 goto err_out;
6100 ata_eh_freeze_port(ap);
6103 if (start_dr)
6104 devres_add(host->dev, start_dr);
6105 host->flags |= ATA_HOST_STARTED;
6106 return 0;
6108 err_out:
6109 while (--i >= 0) {
6110 struct ata_port *ap = host->ports[i];
6112 if (ap->ops->port_stop)
6113 ap->ops->port_stop(ap);
6115 devres_free(start_dr);
6116 return rc;
6118 EXPORT_SYMBOL_GPL(ata_host_start);
6121 * ata_sas_host_init - Initialize a host struct for sas (ipr, libsas)
6122 * @host: host to initialize
6123 * @dev: device host is attached to
6124 * @ops: port_ops
6127 void ata_host_init(struct ata_host *host, struct device *dev,
6128 struct ata_port_operations *ops)
6130 spin_lock_init(&host->lock);
6131 mutex_init(&host->eh_mutex);
6132 host->n_tags = ATA_MAX_QUEUE;
6133 host->dev = dev;
6134 host->ops = ops;
6135 kref_init(&host->kref);
6137 EXPORT_SYMBOL_GPL(ata_host_init);
6139 void __ata_port_probe(struct ata_port *ap)
6141 struct ata_eh_info *ehi = &ap->link.eh_info;
6142 unsigned long flags;
6144 /* kick EH for boot probing */
6145 spin_lock_irqsave(ap->lock, flags);
6147 ehi->probe_mask |= ATA_ALL_DEVICES;
6148 ehi->action |= ATA_EH_RESET;
6149 ehi->flags |= ATA_EHI_NO_AUTOPSY | ATA_EHI_QUIET;
6151 ap->pflags &= ~ATA_PFLAG_INITIALIZING;
6152 ap->pflags |= ATA_PFLAG_LOADING;
6153 ata_port_schedule_eh(ap);
6155 spin_unlock_irqrestore(ap->lock, flags);
6158 int ata_port_probe(struct ata_port *ap)
6160 int rc = 0;
6162 if (ap->ops->error_handler) {
6163 __ata_port_probe(ap);
6164 ata_port_wait_eh(ap);
6165 } else {
6166 DPRINTK("ata%u: bus probe begin\n", ap->print_id);
6167 rc = ata_bus_probe(ap);
6168 DPRINTK("ata%u: bus probe end\n", ap->print_id);
6170 return rc;
6174 static void async_port_probe(void *data, async_cookie_t cookie)
6176 struct ata_port *ap = data;
6179 * If we're not allowed to scan this host in parallel,
6180 * we need to wait until all previous scans have completed
6181 * before going further.
6182 * Jeff Garzik says this is only within a controller, so we
6183 * don't need to wait for port 0, only for later ports.
6185 if (!(ap->host->flags & ATA_HOST_PARALLEL_SCAN) && ap->port_no != 0)
6186 async_synchronize_cookie(cookie);
6188 (void)ata_port_probe(ap);
6190 /* in order to keep device order, we need to synchronize at this point */
6191 async_synchronize_cookie(cookie);
6193 ata_scsi_scan_host(ap, 1);
6197 * ata_host_register - register initialized ATA host
6198 * @host: ATA host to register
6199 * @sht: template for SCSI host
6201 * Register initialized ATA host. @host is allocated using
6202 * ata_host_alloc() and fully initialized by LLD. This function
6203 * starts ports, registers @host with ATA and SCSI layers and
6204 * probe registered devices.
6206 * LOCKING:
6207 * Inherited from calling layer (may sleep).
6209 * RETURNS:
6210 * 0 on success, -errno otherwise.
6212 int ata_host_register(struct ata_host *host, struct scsi_host_template *sht)
6214 int i, rc;
6216 host->n_tags = clamp(sht->can_queue, 1, ATA_MAX_QUEUE);
6218 /* host must have been started */
6219 if (!(host->flags & ATA_HOST_STARTED)) {
6220 dev_err(host->dev, "BUG: trying to register unstarted host\n");
6221 WARN_ON(1);
6222 return -EINVAL;
6225 /* Blow away unused ports. This happens when LLD can't
6226 * determine the exact number of ports to allocate at
6227 * allocation time.
6229 for (i = host->n_ports; host->ports[i]; i++)
6230 kfree(host->ports[i]);
6232 /* give ports names and add SCSI hosts */
6233 for (i = 0; i < host->n_ports; i++) {
6234 host->ports[i]->print_id = atomic_inc_return(&ata_print_id);
6235 host->ports[i]->local_port_no = i + 1;
6238 /* Create associated sysfs transport objects */
6239 for (i = 0; i < host->n_ports; i++) {
6240 rc = ata_tport_add(host->dev,host->ports[i]);
6241 if (rc) {
6242 goto err_tadd;
6246 rc = ata_scsi_add_hosts(host, sht);
6247 if (rc)
6248 goto err_tadd;
6250 /* set cable, sata_spd_limit and report */
6251 for (i = 0; i < host->n_ports; i++) {
6252 struct ata_port *ap = host->ports[i];
6253 unsigned long xfer_mask;
6255 /* set SATA cable type if still unset */
6256 if (ap->cbl == ATA_CBL_NONE && (ap->flags & ATA_FLAG_SATA))
6257 ap->cbl = ATA_CBL_SATA;
6259 /* init sata_spd_limit to the current value */
6260 sata_link_init_spd(&ap->link);
6261 if (ap->slave_link)
6262 sata_link_init_spd(ap->slave_link);
6264 /* print per-port info to dmesg */
6265 xfer_mask = ata_pack_xfermask(ap->pio_mask, ap->mwdma_mask,
6266 ap->udma_mask);
6268 if (!ata_port_is_dummy(ap)) {
6269 ata_port_info(ap, "%cATA max %s %s\n",
6270 (ap->flags & ATA_FLAG_SATA) ? 'S' : 'P',
6271 ata_mode_string(xfer_mask),
6272 ap->link.eh_info.desc);
6273 ata_ehi_clear_desc(&ap->link.eh_info);
6274 } else
6275 ata_port_info(ap, "DUMMY\n");
6278 /* perform each probe asynchronously */
6279 for (i = 0; i < host->n_ports; i++) {
6280 struct ata_port *ap = host->ports[i];
6281 async_schedule(async_port_probe, ap);
6284 return 0;
6286 err_tadd:
6287 while (--i >= 0) {
6288 ata_tport_delete(host->ports[i]);
6290 return rc;
6293 EXPORT_SYMBOL_GPL(ata_host_register);
6296 * ata_host_activate - start host, request IRQ and register it
6297 * @host: target ATA host
6298 * @irq: IRQ to request
6299 * @irq_handler: irq_handler used when requesting IRQ
6300 * @irq_flags: irq_flags used when requesting IRQ
6301 * @sht: scsi_host_template to use when registering the host
6303 * After allocating an ATA host and initializing it, most libata
6304 * LLDs perform three steps to activate the host - start host,
6305 * request IRQ and register it. This helper takes necessary
6306 * arguments and performs the three steps in one go.
6308 * An invalid IRQ skips the IRQ registration and expects the host to
6309 * have set polling mode on the port. In this case, @irq_handler
6310 * should be NULL.
6312 * LOCKING:
6313 * Inherited from calling layer (may sleep).
6315 * RETURNS:
6316 * 0 on success, -errno otherwise.
6318 int ata_host_activate(struct ata_host *host, int irq,
6319 irq_handler_t irq_handler, unsigned long irq_flags,
6320 struct scsi_host_template *sht)
6322 int i, rc;
6323 char *irq_desc;
6325 rc = ata_host_start(host);
6326 if (rc)
6327 return rc;
6329 /* Special case for polling mode */
6330 if (!irq) {
6331 WARN_ON(irq_handler);
6332 return ata_host_register(host, sht);
6335 irq_desc = devm_kasprintf(host->dev, GFP_KERNEL, "%s[%s]",
6336 dev_driver_string(host->dev),
6337 dev_name(host->dev));
6338 if (!irq_desc)
6339 return -ENOMEM;
6341 rc = devm_request_irq(host->dev, irq, irq_handler, irq_flags,
6342 irq_desc, host);
6343 if (rc)
6344 return rc;
6346 for (i = 0; i < host->n_ports; i++)
6347 ata_port_desc(host->ports[i], "irq %d", irq);
6349 rc = ata_host_register(host, sht);
6350 /* if failed, just free the IRQ and leave ports alone */
6351 if (rc)
6352 devm_free_irq(host->dev, irq, host);
6354 return rc;
6356 EXPORT_SYMBOL_GPL(ata_host_activate);
6359 * ata_port_detach - Detach ATA port in preparation of device removal
6360 * @ap: ATA port to be detached
6362 * Detach all ATA devices and the associated SCSI devices of @ap;
6363 * then, remove the associated SCSI host. @ap is guaranteed to
6364 * be quiescent on return from this function.
6366 * LOCKING:
6367 * Kernel thread context (may sleep).
6369 static void ata_port_detach(struct ata_port *ap)
6371 unsigned long flags;
6372 struct ata_link *link;
6373 struct ata_device *dev;
6375 if (!ap->ops->error_handler)
6376 goto skip_eh;
6378 /* tell EH we're leaving & flush EH */
6379 spin_lock_irqsave(ap->lock, flags);
6380 ap->pflags |= ATA_PFLAG_UNLOADING;
6381 ata_port_schedule_eh(ap);
6382 spin_unlock_irqrestore(ap->lock, flags);
6384 /* wait till EH commits suicide */
6385 ata_port_wait_eh(ap);
6387 /* it better be dead now */
6388 WARN_ON(!(ap->pflags & ATA_PFLAG_UNLOADED));
6390 cancel_delayed_work_sync(&ap->hotplug_task);
6392 skip_eh:
6393 /* clean up zpodd on port removal */
6394 ata_for_each_link(link, ap, HOST_FIRST) {
6395 ata_for_each_dev(dev, link, ALL) {
6396 if (zpodd_dev_enabled(dev))
6397 zpodd_exit(dev);
6400 if (ap->pmp_link) {
6401 int i;
6402 for (i = 0; i < SATA_PMP_MAX_PORTS; i++)
6403 ata_tlink_delete(&ap->pmp_link[i]);
6405 /* remove the associated SCSI host */
6406 scsi_remove_host(ap->scsi_host);
6407 ata_tport_delete(ap);
6411 * ata_host_detach - Detach all ports of an ATA host
6412 * @host: Host to detach
6414 * Detach all ports of @host.
6416 * LOCKING:
6417 * Kernel thread context (may sleep).
6419 void ata_host_detach(struct ata_host *host)
6421 int i;
6423 /* Ensure ata_port probe has completed */
6424 async_synchronize_full();
6426 for (i = 0; i < host->n_ports; i++)
6427 ata_port_detach(host->ports[i]);
6429 /* the host is dead now, dissociate ACPI */
6430 ata_acpi_dissociate(host);
6432 EXPORT_SYMBOL_GPL(ata_host_detach);
6434 #ifdef CONFIG_PCI
6437 * ata_pci_remove_one - PCI layer callback for device removal
6438 * @pdev: PCI device that was removed
6440 * PCI layer indicates to libata via this hook that hot-unplug or
6441 * module unload event has occurred. Detach all ports. Resource
6442 * release is handled via devres.
6444 * LOCKING:
6445 * Inherited from PCI layer (may sleep).
6447 void ata_pci_remove_one(struct pci_dev *pdev)
6449 struct ata_host *host = pci_get_drvdata(pdev);
6451 ata_host_detach(host);
6453 EXPORT_SYMBOL_GPL(ata_pci_remove_one);
6455 void ata_pci_shutdown_one(struct pci_dev *pdev)
6457 struct ata_host *host = pci_get_drvdata(pdev);
6458 int i;
6460 for (i = 0; i < host->n_ports; i++) {
6461 struct ata_port *ap = host->ports[i];
6463 ap->pflags |= ATA_PFLAG_FROZEN;
6465 /* Disable port interrupts */
6466 if (ap->ops->freeze)
6467 ap->ops->freeze(ap);
6469 /* Stop the port DMA engines */
6470 if (ap->ops->port_stop)
6471 ap->ops->port_stop(ap);
6474 EXPORT_SYMBOL_GPL(ata_pci_shutdown_one);
6476 /* move to PCI subsystem */
6477 int pci_test_config_bits(struct pci_dev *pdev, const struct pci_bits *bits)
6479 unsigned long tmp = 0;
6481 switch (bits->width) {
6482 case 1: {
6483 u8 tmp8 = 0;
6484 pci_read_config_byte(pdev, bits->reg, &tmp8);
6485 tmp = tmp8;
6486 break;
6488 case 2: {
6489 u16 tmp16 = 0;
6490 pci_read_config_word(pdev, bits->reg, &tmp16);
6491 tmp = tmp16;
6492 break;
6494 case 4: {
6495 u32 tmp32 = 0;
6496 pci_read_config_dword(pdev, bits->reg, &tmp32);
6497 tmp = tmp32;
6498 break;
6501 default:
6502 return -EINVAL;
6505 tmp &= bits->mask;
6507 return (tmp == bits->val) ? 1 : 0;
6509 EXPORT_SYMBOL_GPL(pci_test_config_bits);
6511 #ifdef CONFIG_PM
6512 void ata_pci_device_do_suspend(struct pci_dev *pdev, pm_message_t mesg)
6514 pci_save_state(pdev);
6515 pci_disable_device(pdev);
6517 if (mesg.event & PM_EVENT_SLEEP)
6518 pci_set_power_state(pdev, PCI_D3hot);
6520 EXPORT_SYMBOL_GPL(ata_pci_device_do_suspend);
6522 int ata_pci_device_do_resume(struct pci_dev *pdev)
6524 int rc;
6526 pci_set_power_state(pdev, PCI_D0);
6527 pci_restore_state(pdev);
6529 rc = pcim_enable_device(pdev);
6530 if (rc) {
6531 dev_err(&pdev->dev,
6532 "failed to enable device after resume (%d)\n", rc);
6533 return rc;
6536 pci_set_master(pdev);
6537 return 0;
6539 EXPORT_SYMBOL_GPL(ata_pci_device_do_resume);
6541 int ata_pci_device_suspend(struct pci_dev *pdev, pm_message_t mesg)
6543 struct ata_host *host = pci_get_drvdata(pdev);
6544 int rc = 0;
6546 rc = ata_host_suspend(host, mesg);
6547 if (rc)
6548 return rc;
6550 ata_pci_device_do_suspend(pdev, mesg);
6552 return 0;
6554 EXPORT_SYMBOL_GPL(ata_pci_device_suspend);
6556 int ata_pci_device_resume(struct pci_dev *pdev)
6558 struct ata_host *host = pci_get_drvdata(pdev);
6559 int rc;
6561 rc = ata_pci_device_do_resume(pdev);
6562 if (rc == 0)
6563 ata_host_resume(host);
6564 return rc;
6566 EXPORT_SYMBOL_GPL(ata_pci_device_resume);
6567 #endif /* CONFIG_PM */
6568 #endif /* CONFIG_PCI */
6571 * ata_platform_remove_one - Platform layer callback for device removal
6572 * @pdev: Platform device that was removed
6574 * Platform layer indicates to libata via this hook that hot-unplug or
6575 * module unload event has occurred. Detach all ports. Resource
6576 * release is handled via devres.
6578 * LOCKING:
6579 * Inherited from platform layer (may sleep).
6581 int ata_platform_remove_one(struct platform_device *pdev)
6583 struct ata_host *host = platform_get_drvdata(pdev);
6585 ata_host_detach(host);
6587 return 0;
6589 EXPORT_SYMBOL_GPL(ata_platform_remove_one);
6591 static int __init ata_parse_force_one(char **cur,
6592 struct ata_force_ent *force_ent,
6593 const char **reason)
6595 static const struct ata_force_param force_tbl[] __initconst = {
6596 { "40c", .cbl = ATA_CBL_PATA40 },
6597 { "80c", .cbl = ATA_CBL_PATA80 },
6598 { "short40c", .cbl = ATA_CBL_PATA40_SHORT },
6599 { "unk", .cbl = ATA_CBL_PATA_UNK },
6600 { "ign", .cbl = ATA_CBL_PATA_IGN },
6601 { "sata", .cbl = ATA_CBL_SATA },
6602 { "1.5Gbps", .spd_limit = 1 },
6603 { "3.0Gbps", .spd_limit = 2 },
6604 { "noncq", .horkage_on = ATA_HORKAGE_NONCQ },
6605 { "ncq", .horkage_off = ATA_HORKAGE_NONCQ },
6606 { "noncqtrim", .horkage_on = ATA_HORKAGE_NO_NCQ_TRIM },
6607 { "ncqtrim", .horkage_off = ATA_HORKAGE_NO_NCQ_TRIM },
6608 { "dump_id", .horkage_on = ATA_HORKAGE_DUMP_ID },
6609 { "pio0", .xfer_mask = 1 << (ATA_SHIFT_PIO + 0) },
6610 { "pio1", .xfer_mask = 1 << (ATA_SHIFT_PIO + 1) },
6611 { "pio2", .xfer_mask = 1 << (ATA_SHIFT_PIO + 2) },
6612 { "pio3", .xfer_mask = 1 << (ATA_SHIFT_PIO + 3) },
6613 { "pio4", .xfer_mask = 1 << (ATA_SHIFT_PIO + 4) },
6614 { "pio5", .xfer_mask = 1 << (ATA_SHIFT_PIO + 5) },
6615 { "pio6", .xfer_mask = 1 << (ATA_SHIFT_PIO + 6) },
6616 { "mwdma0", .xfer_mask = 1 << (ATA_SHIFT_MWDMA + 0) },
6617 { "mwdma1", .xfer_mask = 1 << (ATA_SHIFT_MWDMA + 1) },
6618 { "mwdma2", .xfer_mask = 1 << (ATA_SHIFT_MWDMA + 2) },
6619 { "mwdma3", .xfer_mask = 1 << (ATA_SHIFT_MWDMA + 3) },
6620 { "mwdma4", .xfer_mask = 1 << (ATA_SHIFT_MWDMA + 4) },
6621 { "udma0", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 0) },
6622 { "udma16", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 0) },
6623 { "udma/16", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 0) },
6624 { "udma1", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 1) },
6625 { "udma25", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 1) },
6626 { "udma/25", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 1) },
6627 { "udma2", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 2) },
6628 { "udma33", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 2) },
6629 { "udma/33", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 2) },
6630 { "udma3", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 3) },
6631 { "udma44", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 3) },
6632 { "udma/44", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 3) },
6633 { "udma4", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 4) },
6634 { "udma66", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 4) },
6635 { "udma/66", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 4) },
6636 { "udma5", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 5) },
6637 { "udma100", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 5) },
6638 { "udma/100", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 5) },
6639 { "udma6", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 6) },
6640 { "udma133", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 6) },
6641 { "udma/133", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 6) },
6642 { "udma7", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 7) },
6643 { "nohrst", .lflags = ATA_LFLAG_NO_HRST },
6644 { "nosrst", .lflags = ATA_LFLAG_NO_SRST },
6645 { "norst", .lflags = ATA_LFLAG_NO_HRST | ATA_LFLAG_NO_SRST },
6646 { "rstonce", .lflags = ATA_LFLAG_RST_ONCE },
6647 { "atapi_dmadir", .horkage_on = ATA_HORKAGE_ATAPI_DMADIR },
6648 { "disable", .horkage_on = ATA_HORKAGE_DISABLE },
6650 char *start = *cur, *p = *cur;
6651 char *id, *val, *endp;
6652 const struct ata_force_param *match_fp = NULL;
6653 int nr_matches = 0, i;
6655 /* find where this param ends and update *cur */
6656 while (*p != '\0' && *p != ',')
6657 p++;
6659 if (*p == '\0')
6660 *cur = p;
6661 else
6662 *cur = p + 1;
6664 *p = '\0';
6666 /* parse */
6667 p = strchr(start, ':');
6668 if (!p) {
6669 val = strstrip(start);
6670 goto parse_val;
6672 *p = '\0';
6674 id = strstrip(start);
6675 val = strstrip(p + 1);
6677 /* parse id */
6678 p = strchr(id, '.');
6679 if (p) {
6680 *p++ = '\0';
6681 force_ent->device = simple_strtoul(p, &endp, 10);
6682 if (p == endp || *endp != '\0') {
6683 *reason = "invalid device";
6684 return -EINVAL;
6688 force_ent->port = simple_strtoul(id, &endp, 10);
6689 if (id == endp || *endp != '\0') {
6690 *reason = "invalid port/link";
6691 return -EINVAL;
6694 parse_val:
6695 /* parse val, allow shortcuts so that both 1.5 and 1.5Gbps work */
6696 for (i = 0; i < ARRAY_SIZE(force_tbl); i++) {
6697 const struct ata_force_param *fp = &force_tbl[i];
6699 if (strncasecmp(val, fp->name, strlen(val)))
6700 continue;
6702 nr_matches++;
6703 match_fp = fp;
6705 if (strcasecmp(val, fp->name) == 0) {
6706 nr_matches = 1;
6707 break;
6711 if (!nr_matches) {
6712 *reason = "unknown value";
6713 return -EINVAL;
6715 if (nr_matches > 1) {
6716 *reason = "ambiguous value";
6717 return -EINVAL;
6720 force_ent->param = *match_fp;
6722 return 0;
6725 static void __init ata_parse_force_param(void)
6727 int idx = 0, size = 1;
6728 int last_port = -1, last_device = -1;
6729 char *p, *cur, *next;
6731 /* calculate maximum number of params and allocate force_tbl */
6732 for (p = ata_force_param_buf; *p; p++)
6733 if (*p == ',')
6734 size++;
6736 ata_force_tbl = kcalloc(size, sizeof(ata_force_tbl[0]), GFP_KERNEL);
6737 if (!ata_force_tbl) {
6738 printk(KERN_WARNING "ata: failed to extend force table, "
6739 "libata.force ignored\n");
6740 return;
6743 /* parse and populate the table */
6744 for (cur = ata_force_param_buf; *cur != '\0'; cur = next) {
6745 const char *reason = "";
6746 struct ata_force_ent te = { .port = -1, .device = -1 };
6748 next = cur;
6749 if (ata_parse_force_one(&next, &te, &reason)) {
6750 printk(KERN_WARNING "ata: failed to parse force "
6751 "parameter \"%s\" (%s)\n",
6752 cur, reason);
6753 continue;
6756 if (te.port == -1) {
6757 te.port = last_port;
6758 te.device = last_device;
6761 ata_force_tbl[idx++] = te;
6763 last_port = te.port;
6764 last_device = te.device;
6767 ata_force_tbl_size = idx;
6770 static int __init ata_init(void)
6772 int rc;
6774 ata_parse_force_param();
6776 rc = ata_sff_init();
6777 if (rc) {
6778 kfree(ata_force_tbl);
6779 return rc;
6782 libata_transport_init();
6783 ata_scsi_transport_template = ata_attach_transport();
6784 if (!ata_scsi_transport_template) {
6785 ata_sff_exit();
6786 rc = -ENOMEM;
6787 goto err_out;
6790 printk(KERN_DEBUG "libata version " DRV_VERSION " loaded.\n");
6791 return 0;
6793 err_out:
6794 return rc;
6797 static void __exit ata_exit(void)
6799 ata_release_transport(ata_scsi_transport_template);
6800 libata_transport_exit();
6801 ata_sff_exit();
6802 kfree(ata_force_tbl);
6805 subsys_initcall(ata_init);
6806 module_exit(ata_exit);
6808 static DEFINE_RATELIMIT_STATE(ratelimit, HZ / 5, 1);
6810 int ata_ratelimit(void)
6812 return __ratelimit(&ratelimit);
6814 EXPORT_SYMBOL_GPL(ata_ratelimit);
6817 * ata_msleep - ATA EH owner aware msleep
6818 * @ap: ATA port to attribute the sleep to
6819 * @msecs: duration to sleep in milliseconds
6821 * Sleeps @msecs. If the current task is owner of @ap's EH, the
6822 * ownership is released before going to sleep and reacquired
6823 * after the sleep is complete. IOW, other ports sharing the
6824 * @ap->host will be allowed to own the EH while this task is
6825 * sleeping.
6827 * LOCKING:
6828 * Might sleep.
6830 void ata_msleep(struct ata_port *ap, unsigned int msecs)
6832 bool owns_eh = ap && ap->host->eh_owner == current;
6834 if (owns_eh)
6835 ata_eh_release(ap);
6837 if (msecs < 20) {
6838 unsigned long usecs = msecs * USEC_PER_MSEC;
6839 usleep_range(usecs, usecs + 50);
6840 } else {
6841 msleep(msecs);
6844 if (owns_eh)
6845 ata_eh_acquire(ap);
6847 EXPORT_SYMBOL_GPL(ata_msleep);
6850 * ata_wait_register - wait until register value changes
6851 * @ap: ATA port to wait register for, can be NULL
6852 * @reg: IO-mapped register
6853 * @mask: Mask to apply to read register value
6854 * @val: Wait condition
6855 * @interval: polling interval in milliseconds
6856 * @timeout: timeout in milliseconds
6858 * Waiting for some bits of register to change is a common
6859 * operation for ATA controllers. This function reads 32bit LE
6860 * IO-mapped register @reg and tests for the following condition.
6862 * (*@reg & mask) != val
6864 * If the condition is met, it returns; otherwise, the process is
6865 * repeated after @interval_msec until timeout.
6867 * LOCKING:
6868 * Kernel thread context (may sleep)
6870 * RETURNS:
6871 * The final register value.
6873 u32 ata_wait_register(struct ata_port *ap, void __iomem *reg, u32 mask, u32 val,
6874 unsigned long interval, unsigned long timeout)
6876 unsigned long deadline;
6877 u32 tmp;
6879 tmp = ioread32(reg);
6881 /* Calculate timeout _after_ the first read to make sure
6882 * preceding writes reach the controller before starting to
6883 * eat away the timeout.
6885 deadline = ata_deadline(jiffies, timeout);
6887 while ((tmp & mask) == val && time_before(jiffies, deadline)) {
6888 ata_msleep(ap, interval);
6889 tmp = ioread32(reg);
6892 return tmp;
6894 EXPORT_SYMBOL_GPL(ata_wait_register);
6897 * Dummy port_ops
6899 static unsigned int ata_dummy_qc_issue(struct ata_queued_cmd *qc)
6901 return AC_ERR_SYSTEM;
6904 static void ata_dummy_error_handler(struct ata_port *ap)
6906 /* truly dummy */
6909 struct ata_port_operations ata_dummy_port_ops = {
6910 .qc_prep = ata_noop_qc_prep,
6911 .qc_issue = ata_dummy_qc_issue,
6912 .error_handler = ata_dummy_error_handler,
6913 .sched_eh = ata_std_sched_eh,
6914 .end_eh = ata_std_end_eh,
6916 EXPORT_SYMBOL_GPL(ata_dummy_port_ops);
6918 const struct ata_port_info ata_dummy_port_info = {
6919 .port_ops = &ata_dummy_port_ops,
6921 EXPORT_SYMBOL_GPL(ata_dummy_port_info);
6924 * Utility print functions
6926 void ata_port_printk(const struct ata_port *ap, const char *level,
6927 const char *fmt, ...)
6929 struct va_format vaf;
6930 va_list args;
6932 va_start(args, fmt);
6934 vaf.fmt = fmt;
6935 vaf.va = &args;
6937 printk("%sata%u: %pV", level, ap->print_id, &vaf);
6939 va_end(args);
6941 EXPORT_SYMBOL(ata_port_printk);
6943 void ata_link_printk(const struct ata_link *link, const char *level,
6944 const char *fmt, ...)
6946 struct va_format vaf;
6947 va_list args;
6949 va_start(args, fmt);
6951 vaf.fmt = fmt;
6952 vaf.va = &args;
6954 if (sata_pmp_attached(link->ap) || link->ap->slave_link)
6955 printk("%sata%u.%02u: %pV",
6956 level, link->ap->print_id, link->pmp, &vaf);
6957 else
6958 printk("%sata%u: %pV",
6959 level, link->ap->print_id, &vaf);
6961 va_end(args);
6963 EXPORT_SYMBOL(ata_link_printk);
6965 void ata_dev_printk(const struct ata_device *dev, const char *level,
6966 const char *fmt, ...)
6968 struct va_format vaf;
6969 va_list args;
6971 va_start(args, fmt);
6973 vaf.fmt = fmt;
6974 vaf.va = &args;
6976 printk("%sata%u.%02u: %pV",
6977 level, dev->link->ap->print_id, dev->link->pmp + dev->devno,
6978 &vaf);
6980 va_end(args);
6982 EXPORT_SYMBOL(ata_dev_printk);
6984 void ata_print_version(const struct device *dev, const char *version)
6986 dev_printk(KERN_DEBUG, dev, "version %s\n", version);
6988 EXPORT_SYMBOL(ata_print_version);