1 // SPDX-License-Identifier: GPL-2.0-or-later
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>
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>
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>
66 #include "libata-transport.h"
68 const struct ata_port_operations ata_base_port_ops
= {
69 .prereset
= ata_std_prereset
,
70 .postreset
= ata_std_postreset
,
71 .error_handler
= ata_std_error_handler
,
72 .sched_eh
= ata_std_sched_eh
,
73 .end_eh
= ata_std_end_eh
,
76 const struct ata_port_operations sata_port_ops
= {
77 .inherits
= &ata_base_port_ops
,
79 .qc_defer
= ata_std_qc_defer
,
80 .hardreset
= sata_std_hardreset
,
82 EXPORT_SYMBOL_GPL(sata_port_ops
);
84 static unsigned int ata_dev_init_params(struct ata_device
*dev
,
85 u16 heads
, u16 sectors
);
86 static unsigned int ata_dev_set_xfermode(struct ata_device
*dev
);
87 static void ata_dev_xfermask(struct ata_device
*dev
);
88 static unsigned long ata_dev_blacklisted(const struct ata_device
*dev
);
90 atomic_t ata_print_id
= ATOMIC_INIT(0);
92 #ifdef CONFIG_ATA_FORCE
93 struct ata_force_param
{
97 unsigned long xfer_mask
;
98 unsigned int horkage_on
;
99 unsigned int horkage_off
;
103 struct ata_force_ent
{
106 struct ata_force_param param
;
109 static struct ata_force_ent
*ata_force_tbl
;
110 static int ata_force_tbl_size
;
112 static char ata_force_param_buf
[COMMAND_LINE_SIZE
] __initdata
;
113 /* param_buf is thrown away after initialization, disallow read */
114 module_param_string(force
, ata_force_param_buf
, sizeof(ata_force_param_buf
), 0);
115 MODULE_PARM_DESC(force
, "Force ATA configurations including cable type, link speed and transfer mode (see Documentation/admin-guide/kernel-parameters.rst for details)");
118 static int atapi_enabled
= 1;
119 module_param(atapi_enabled
, int, 0444);
120 MODULE_PARM_DESC(atapi_enabled
, "Enable discovery of ATAPI devices (0=off, 1=on [default])");
122 static int atapi_dmadir
= 0;
123 module_param(atapi_dmadir
, int, 0444);
124 MODULE_PARM_DESC(atapi_dmadir
, "Enable ATAPI DMADIR bridge support (0=off [default], 1=on)");
126 int atapi_passthru16
= 1;
127 module_param(atapi_passthru16
, int, 0444);
128 MODULE_PARM_DESC(atapi_passthru16
, "Enable ATA_16 passthru for ATAPI devices (0=off, 1=on [default])");
131 module_param_named(fua
, libata_fua
, int, 0444);
132 MODULE_PARM_DESC(fua
, "FUA support (0=off [default], 1=on)");
134 static int ata_ignore_hpa
;
135 module_param_named(ignore_hpa
, ata_ignore_hpa
, int, 0644);
136 MODULE_PARM_DESC(ignore_hpa
, "Ignore HPA limit (0=keep BIOS limits, 1=ignore limits, using full disk)");
138 static int libata_dma_mask
= ATA_DMA_MASK_ATA
|ATA_DMA_MASK_ATAPI
|ATA_DMA_MASK_CFA
;
139 module_param_named(dma
, libata_dma_mask
, int, 0444);
140 MODULE_PARM_DESC(dma
, "DMA enable/disable (0x1==ATA, 0x2==ATAPI, 0x4==CF)");
142 static int ata_probe_timeout
;
143 module_param(ata_probe_timeout
, int, 0444);
144 MODULE_PARM_DESC(ata_probe_timeout
, "Set ATA probing timeout (seconds)");
146 int libata_noacpi
= 0;
147 module_param_named(noacpi
, libata_noacpi
, int, 0444);
148 MODULE_PARM_DESC(noacpi
, "Disable the use of ACPI in probe/suspend/resume (0=off [default], 1=on)");
150 int libata_allow_tpm
= 0;
151 module_param_named(allow_tpm
, libata_allow_tpm
, int, 0444);
152 MODULE_PARM_DESC(allow_tpm
, "Permit the use of TPM commands (0=off [default], 1=on)");
155 module_param(atapi_an
, int, 0444);
156 MODULE_PARM_DESC(atapi_an
, "Enable ATAPI AN media presence notification (0=0ff [default], 1=on)");
158 MODULE_AUTHOR("Jeff Garzik");
159 MODULE_DESCRIPTION("Library module for ATA devices");
160 MODULE_LICENSE("GPL");
161 MODULE_VERSION(DRV_VERSION
);
164 static bool ata_sstatus_online(u32 sstatus
)
166 return (sstatus
& 0xf) == 0x3;
170 * ata_link_next - link iteration helper
171 * @link: the previous link, NULL to start
172 * @ap: ATA port containing links to iterate
173 * @mode: iteration mode, one of ATA_LITER_*
176 * Host lock or EH context.
179 * Pointer to the next link.
181 struct ata_link
*ata_link_next(struct ata_link
*link
, struct ata_port
*ap
,
182 enum ata_link_iter_mode mode
)
184 BUG_ON(mode
!= ATA_LITER_EDGE
&&
185 mode
!= ATA_LITER_PMP_FIRST
&& mode
!= ATA_LITER_HOST_FIRST
);
187 /* NULL link indicates start of iteration */
191 case ATA_LITER_PMP_FIRST
:
192 if (sata_pmp_attached(ap
))
195 case ATA_LITER_HOST_FIRST
:
199 /* we just iterated over the host link, what's next? */
200 if (link
== &ap
->link
)
202 case ATA_LITER_HOST_FIRST
:
203 if (sata_pmp_attached(ap
))
206 case ATA_LITER_PMP_FIRST
:
207 if (unlikely(ap
->slave_link
))
208 return ap
->slave_link
;
214 /* slave_link excludes PMP */
215 if (unlikely(link
== ap
->slave_link
))
218 /* we were over a PMP link */
219 if (++link
< ap
->pmp_link
+ ap
->nr_pmp_links
)
222 if (mode
== ATA_LITER_PMP_FIRST
)
227 EXPORT_SYMBOL_GPL(ata_link_next
);
230 * ata_dev_next - device iteration helper
231 * @dev: the previous device, NULL to start
232 * @link: ATA link containing devices to iterate
233 * @mode: iteration mode, one of ATA_DITER_*
236 * Host lock or EH context.
239 * Pointer to the next device.
241 struct ata_device
*ata_dev_next(struct ata_device
*dev
, struct ata_link
*link
,
242 enum ata_dev_iter_mode mode
)
244 BUG_ON(mode
!= ATA_DITER_ENABLED
&& mode
!= ATA_DITER_ENABLED_REVERSE
&&
245 mode
!= ATA_DITER_ALL
&& mode
!= ATA_DITER_ALL_REVERSE
);
247 /* NULL dev indicates start of iteration */
250 case ATA_DITER_ENABLED
:
254 case ATA_DITER_ENABLED_REVERSE
:
255 case ATA_DITER_ALL_REVERSE
:
256 dev
= link
->device
+ ata_link_max_devices(link
) - 1;
261 /* move to the next one */
263 case ATA_DITER_ENABLED
:
265 if (++dev
< link
->device
+ ata_link_max_devices(link
))
268 case ATA_DITER_ENABLED_REVERSE
:
269 case ATA_DITER_ALL_REVERSE
:
270 if (--dev
>= link
->device
)
276 if ((mode
== ATA_DITER_ENABLED
|| mode
== ATA_DITER_ENABLED_REVERSE
) &&
277 !ata_dev_enabled(dev
))
281 EXPORT_SYMBOL_GPL(ata_dev_next
);
284 * ata_dev_phys_link - find physical link for a device
285 * @dev: ATA device to look up physical link for
287 * Look up physical link which @dev is attached to. Note that
288 * this is different from @dev->link only when @dev is on slave
289 * link. For all other cases, it's the same as @dev->link.
295 * Pointer to the found physical link.
297 struct ata_link
*ata_dev_phys_link(struct ata_device
*dev
)
299 struct ata_port
*ap
= dev
->link
->ap
;
305 return ap
->slave_link
;
308 #ifdef CONFIG_ATA_FORCE
310 * ata_force_cbl - force cable type according to libata.force
311 * @ap: ATA port of interest
313 * Force cable type according to libata.force and whine about it.
314 * The last entry which has matching port number is used, so it
315 * can be specified as part of device force parameters. For
316 * example, both "a:40c,1.00:udma4" and "1.00:40c,udma4" have the
322 void ata_force_cbl(struct ata_port
*ap
)
326 for (i
= ata_force_tbl_size
- 1; i
>= 0; i
--) {
327 const struct ata_force_ent
*fe
= &ata_force_tbl
[i
];
329 if (fe
->port
!= -1 && fe
->port
!= ap
->print_id
)
332 if (fe
->param
.cbl
== ATA_CBL_NONE
)
335 ap
->cbl
= fe
->param
.cbl
;
336 ata_port_notice(ap
, "FORCE: cable set to %s\n", fe
->param
.name
);
342 * ata_force_link_limits - force link limits according to libata.force
343 * @link: ATA link of interest
345 * Force link flags and SATA spd limit according to libata.force
346 * and whine about it. When only the port part is specified
347 * (e.g. 1:), the limit applies to all links connected to both
348 * the host link and all fan-out ports connected via PMP. If the
349 * device part is specified as 0 (e.g. 1.00:), it specifies the
350 * first fan-out link not the host link. Device number 15 always
351 * points to the host link whether PMP is attached or not. If the
352 * controller has slave link, device number 16 points to it.
357 static void ata_force_link_limits(struct ata_link
*link
)
359 bool did_spd
= false;
360 int linkno
= link
->pmp
;
363 if (ata_is_host_link(link
))
366 for (i
= ata_force_tbl_size
- 1; i
>= 0; i
--) {
367 const struct ata_force_ent
*fe
= &ata_force_tbl
[i
];
369 if (fe
->port
!= -1 && fe
->port
!= link
->ap
->print_id
)
372 if (fe
->device
!= -1 && fe
->device
!= linkno
)
375 /* only honor the first spd limit */
376 if (!did_spd
&& fe
->param
.spd_limit
) {
377 link
->hw_sata_spd_limit
= (1 << fe
->param
.spd_limit
) - 1;
378 ata_link_notice(link
, "FORCE: PHY spd limit set to %s\n",
383 /* let lflags stack */
384 if (fe
->param
.lflags
) {
385 link
->flags
|= fe
->param
.lflags
;
386 ata_link_notice(link
,
387 "FORCE: link flag 0x%x forced -> 0x%x\n",
388 fe
->param
.lflags
, link
->flags
);
394 * ata_force_xfermask - force xfermask according to libata.force
395 * @dev: ATA device of interest
397 * Force xfer_mask according to libata.force and whine about it.
398 * For consistency with link selection, device number 15 selects
399 * the first device connected to the host link.
404 static void ata_force_xfermask(struct ata_device
*dev
)
406 int devno
= dev
->link
->pmp
+ dev
->devno
;
407 int alt_devno
= devno
;
410 /* allow n.15/16 for devices attached to host port */
411 if (ata_is_host_link(dev
->link
))
414 for (i
= ata_force_tbl_size
- 1; i
>= 0; i
--) {
415 const struct ata_force_ent
*fe
= &ata_force_tbl
[i
];
416 unsigned long pio_mask
, mwdma_mask
, udma_mask
;
418 if (fe
->port
!= -1 && fe
->port
!= dev
->link
->ap
->print_id
)
421 if (fe
->device
!= -1 && fe
->device
!= devno
&&
422 fe
->device
!= alt_devno
)
425 if (!fe
->param
.xfer_mask
)
428 ata_unpack_xfermask(fe
->param
.xfer_mask
,
429 &pio_mask
, &mwdma_mask
, &udma_mask
);
431 dev
->udma_mask
= udma_mask
;
432 else if (mwdma_mask
) {
434 dev
->mwdma_mask
= mwdma_mask
;
438 dev
->pio_mask
= pio_mask
;
441 ata_dev_notice(dev
, "FORCE: xfer_mask set to %s\n",
448 * ata_force_horkage - force horkage according to libata.force
449 * @dev: ATA device of interest
451 * Force horkage according to libata.force and whine about it.
452 * For consistency with link selection, device number 15 selects
453 * the first device connected to the host link.
458 static void ata_force_horkage(struct ata_device
*dev
)
460 int devno
= dev
->link
->pmp
+ dev
->devno
;
461 int alt_devno
= devno
;
464 /* allow n.15/16 for devices attached to host port */
465 if (ata_is_host_link(dev
->link
))
468 for (i
= 0; i
< ata_force_tbl_size
; i
++) {
469 const struct ata_force_ent
*fe
= &ata_force_tbl
[i
];
471 if (fe
->port
!= -1 && fe
->port
!= dev
->link
->ap
->print_id
)
474 if (fe
->device
!= -1 && fe
->device
!= devno
&&
475 fe
->device
!= alt_devno
)
478 if (!(~dev
->horkage
& fe
->param
.horkage_on
) &&
479 !(dev
->horkage
& fe
->param
.horkage_off
))
482 dev
->horkage
|= fe
->param
.horkage_on
;
483 dev
->horkage
&= ~fe
->param
.horkage_off
;
485 ata_dev_notice(dev
, "FORCE: horkage modified (%s)\n",
490 static inline void ata_force_link_limits(struct ata_link
*link
) { }
491 static inline void ata_force_xfermask(struct ata_device
*dev
) { }
492 static inline void ata_force_horkage(struct ata_device
*dev
) { }
496 * atapi_cmd_type - Determine ATAPI command type from SCSI opcode
497 * @opcode: SCSI opcode
499 * Determine ATAPI command type from @opcode.
505 * ATAPI_{READ|WRITE|READ_CD|PASS_THRU|MISC}
507 int atapi_cmd_type(u8 opcode
)
516 case GPCMD_WRITE_AND_VERIFY_10
:
520 case GPCMD_READ_CD_MSF
:
521 return ATAPI_READ_CD
;
525 if (atapi_passthru16
)
526 return ATAPI_PASS_THRU
;
532 EXPORT_SYMBOL_GPL(atapi_cmd_type
);
534 static const u8 ata_rw_cmds
[] = {
538 ATA_CMD_READ_MULTI_EXT
,
539 ATA_CMD_WRITE_MULTI_EXT
,
543 ATA_CMD_WRITE_MULTI_FUA_EXT
,
547 ATA_CMD_PIO_READ_EXT
,
548 ATA_CMD_PIO_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.
575 static int ata_rwcmd_protocol(struct ata_taskfile
*tf
, struct ata_device
*dev
)
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;
593 tf
->protocol
= ATA_PROT_DMA
;
597 cmd
= ata_rw_cmds
[index
+ fua
+ lba48
+ write
];
606 * ata_tf_read_block - Read block address from ATA taskfile
607 * @tf: ATA taskfile of interest
608 * @dev: ATA device @tf belongs to
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.
618 * Block address read from @tf.
620 u64
ata_tf_read_block(const struct ata_taskfile
*tf
, struct ata_device
*dev
)
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;
630 block
|= (tf
->device
& 0xf) << 24;
632 block
|= tf
->lbah
<< 16;
633 block
|= tf
->lbam
<< 8;
638 cyl
= tf
->lbam
| (tf
->lbah
<< 8);
639 head
= tf
->device
& 0xf;
644 "device reported invalid CHS sector 0\n");
648 block
= (cyl
* dev
->heads
+ head
) * dev
->sectors
+ sect
- 1;
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...
662 * @class: IO priority class
667 * Build ATA taskfile @tf for read/write request described by
668 * @block, @n_block, @tf_flags and @tag on @dev.
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
)) {
684 if (!lba_48_ok(block
, n_block
))
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
;
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
<<
715 } else if (dev
->flags
& ATA_DFLAG_LBA
) {
716 tf
->flags
|= ATA_TFLAG_LBA
;
718 if (lba_28_ok(block
, n_block
)) {
720 tf
->device
|= (block
>> 24) & 0xf;
721 } else if (lba_48_ok(block
, n_block
)) {
722 if (!(dev
->flags
& ATA_DFLAG_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;
734 /* request too large even for LBA48 */
737 if (unlikely(ata_rwcmd_protocol(tf
, dev
) < 0))
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
;
749 u32 sect
, head
, cyl
, track
;
751 /* The request -may- be too large for CHS addressing. */
752 if (!lba_28_ok(block
, n_block
))
755 if (unlikely(ata_rwcmd_protocol(tf
, dev
) < 0))
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.
771 if ((cyl
>> 16) || (head
>> 4) || (sect
>> 8) || (!sect
))
774 tf
->nsect
= n_block
& 0xff; /* Sector count 0 means 256 sectors */
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.
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
)
823 *pio_mask
= (xfer_mask
& ATA_MASK_PIO
) >> ATA_SHIFT_PIO
;
825 *mwdma_mask
= (xfer_mask
& ATA_MASK_MWDMA
) >> ATA_SHIFT_MWDMA
;
827 *udma_mask
= (xfer_mask
& ATA_MASK_UDMA
) >> ATA_SHIFT_UDMA
;
830 static const struct ata_xfer_ent
{
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
},
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.
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
;
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.
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);
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.
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
)
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).
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
[] = {
952 highbit
= fls(xfer_mask
) - 1;
953 if (highbit
>= 0 && highbit
< ARRAY_SIZE(xfer_mode_str
))
954 return xfer_mode_str
[highbit
];
957 EXPORT_SYMBOL_GPL(ata_mode_string
);
959 const char *sata_spd_string(unsigned int spd
)
961 static const char * const spd_str
[] = {
967 if (spd
== 0 || (spd
- 1) >= ARRAY_SIZE(spd_str
))
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).
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
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");
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");
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");
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.
1055 void ata_id_string(const u16
*id
, unsigned char *s
,
1056 unsigned int ofs
, unsigned int len
)
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.
1091 void ata_id_c_string(const u16
*id
, unsigned char *s
,
1092 unsigned int ofs
, unsigned int len
)
1096 ata_id_string(id
, s
, ofs
, len
- 1);
1098 p
= s
+ strnlen(s
, len
- 1);
1099 while (p
> s
&& p
[-1] == ' ')
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
);
1111 return ata_id_u32(id
, ATA_ID_LBA_CAPACITY
);
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
];
1117 return id
[ATA_ID_CYLS
] * id
[ATA_ID_HEADS
] *
1122 u64
ata_tf_to_lba48(const struct ata_taskfile
*tf
)
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);
1136 u64
ata_tf_to_lba(const struct ata_taskfile
*tf
)
1140 sectors
|= (tf
->device
& 0x0f) << 24;
1141 sectors
|= (tf
->lbah
& 0xff) << 16;
1142 sectors
|= (tf
->lbam
& 0xff) << 8;
1143 sectors
|= (tf
->lbal
& 0xff);
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
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
;
1172 tf
.command
= ATA_CMD_READ_NATIVE_MAX_EXT
;
1173 tf
.flags
|= ATA_TFLAG_LBA48
;
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);
1183 "failed to read native max address (err_mask=0x%x)\n",
1185 if (err_mask
== AC_ERR_DEV
&& (tf
.feature
& ATA_ABORTED
))
1191 *max_sectors
= ata_tf_to_lba48(&tf
) + 1;
1193 *max_sectors
= ata_tf_to_lba(&tf
) + 1;
1194 if (dev
->horkage
& ATA_HORKAGE_HPA_SIZE
)
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.
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
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
);
1219 ata_tf_init(dev
, &tf
);
1221 tf
.flags
|= ATA_TFLAG_DEVICE
| ATA_TFLAG_ISADDR
;
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;
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);
1246 "failed to set max address (err_mask=0x%x)\n",
1248 if (err_mask
== AC_ERR_DEV
&&
1249 (tf
.feature
& (ATA_ABORTED
| ATA_IDNF
)))
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.
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
);
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
))
1283 /* read native max address */
1284 rc
= ata_read_native_max_address(dev
, &native_sectors
);
1286 /* If device aborted the command or HPA isn't going to
1287 * be unlocked, skip HPA resizing.
1289 if (rc
== -EACCES
|| !unlock_hpa
) {
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 */
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
)
1308 if (native_sectors
> sectors
)
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
)
1315 "native sectors (%llu) is smaller than sectors (%llu)\n",
1316 (unsigned long long)native_sectors
,
1317 (unsigned long long)sectors
);
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 */
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
;
1334 /* re-read IDENTIFY data */
1335 rc
= ata_dev_reread_id(dev
, 0);
1338 "failed to re-read IDENTIFY data after HPA resizing\n");
1343 u64 new_sectors
= ata_id_n_sectors(dev
->id
);
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
);
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
1365 static inline void ata_dump_id(const u16
*id
)
1367 DPRINTK("49==0x%04x "
1377 DPRINTK("80==0x%04x "
1387 DPRINTK("88==0x%04x "
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 ?).
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;
1418 /* If word 64 isn't valid then Word 51 high byte holds
1419 * the PIO timing number for the maximum. Turn it into
1422 u8 mode
= (id
[ATA_ID_OLD_PIO_MODES
] >> 8) & 0xFF;
1423 if (mode
< 5) /* Valid PIO range */
1424 pio_mask
= (2 << mode
) - 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;
1446 pio_mask
|= (1 << 5);
1448 pio_mask
|= (1 << 6);
1450 mwdma_mask
|= (1 << 3);
1452 mwdma_mask
|= (1 << 4);
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
;
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.
1487 * None. Should be called with kernel context, might sleep.
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
;
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
;
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
;
1536 ap
->nr_active_links
= 0;
1538 /* prepare & issue qc */
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
;
1566 spin_unlock_irqrestore(ap
->lock
, flags
);
1569 if (ata_probe_timeout
)
1570 timeout
= ata_probe_timeout
* 1000;
1572 timeout
= ata_internal_cmd_timeout(dev
, command
);
1577 if (ap
->ops
->error_handler
)
1580 rc
= wait_for_completion_timeout(&wait
, msecs_to_jiffies(timeout
));
1582 if (ap
->ops
->error_handler
)
1585 ata_sff_flush_pio_task(ap
);
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
);
1601 ata_qc_complete(qc
);
1603 if (ata_msg_warn(ap
))
1604 ata_dev_warn(dev
, "qc timeout (cmd 0x%x)\n",
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
;
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
;
1630 spin_lock_irqsave(ap
->lock
, flags
);
1632 *tf
= qc
->result_tf
;
1633 err_mask
= qc
->err_mask
;
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
);
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.
1663 * None. Should be called with kernel context, might sleep.
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
) {
1678 sg_init_one(&sg
, buf
, buflen
);
1683 return ata_exec_internal_sg(dev
, tf
, cdb
, dma_dir
, psg
, n_elem
,
1688 * ata_pio_need_iordy - check if iordy needed
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
)
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
)
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
))
1711 /* PIO3 and higher it is mandatory */
1712 if (adev
->pio_mode
> XFER_PIO_2
)
1714 /* We turn it on when possible */
1715 if (ata_id_has_iordy(adev
->id
))
1719 EXPORT_SYMBOL_GPL(ata_pio_need_iordy
);
1722 * ata_pio_mask_no_iordy - Return the non IORDY mask
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 ? */
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
1747 * @tf: proposed taskfile
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.
1778 * Kernel thread context (may sleep)
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;
1791 bool is_semb
= class == ATA_DEV_SEMB
;
1792 int may_fallback
= 1, tried_spinup
= 0;
1795 if (ata_msg_ctl(ap
))
1796 ata_dev_dbg(dev
, "%s: ENTER\n", __func__
);
1799 ata_tf_init(dev
, &tf
);
1803 class = ATA_DEV_ATA
; /* some hard drives report SEMB sig */
1807 tf
.command
= ATA_CMD_ID_ATA
;
1810 tf
.command
= ATA_CMD_ID_ATAPI
;
1814 reason
= "unsupported class";
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
);
1833 err_mask
= ata_do_dev_read_id(dev
, &tf
, id
);
1836 if (err_mask
& AC_ERR_NODEV_HINT
) {
1837 ata_dev_dbg(dev
, "NODEV after polling detection\n");
1843 "IDENTIFY failed on device w/ SEMB sig, disabled\n");
1844 /* SEMB is not supported yet */
1845 *p_class
= ATA_DEV_SEMB_UNSUP
;
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.
1858 if (class == ATA_DEV_ATA
)
1859 class = ATA_DEV_ATAPI
;
1861 class = ATA_DEV_ATA
;
1865 /* Control reaches here iff the device aborted
1866 * both flavors of IDENTIFYs which happens
1867 * sometimes with phantom devices.
1870 "both IDENTIFYs aborted, assuming NODEV\n");
1875 reason
= "I/O error";
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.
1892 swap_buf_le16(id
, ATA_ID_WORDS
);
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
))
1901 if (ap
->host
->flags
& ATA_HOST_IGNORE_ATA
&&
1902 ata_id_is_ata(id
)) {
1904 "host indicates ignore ATA devices, ignored\n");
1908 if (ata_id_is_ata(id
))
1912 if (!tried_spinup
&& (id
[2] == 0x37c8 || id
[2] == 0x738c)) {
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) {
1922 reason
= "SPINUP failed";
1926 * If the drive initially returned incomplete IDENTIFY info,
1927 * we now must reissue the IDENTIFY command.
1929 if (id
[2] == 0x37c8)
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:
1938 * IDENTIFY (optional in early ATA)
1939 * INITIALIZE DEVICE PARAMETERS (later IDE and ATA)
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]);
1950 reason
= "INIT_DEV_PARAMS failed";
1954 /* current CHS translation info (id[53-58]) might be
1955 * changed. reread the identify device info.
1957 flags
&= ~ATA_READID_POSTRESET
;
1967 if (ata_msg_warn(ap
))
1968 ata_dev_warn(dev
, "failed to IDENTIFY (%s, err_mask=0x%x)\n",
1974 * ata_read_log_page - read a specific log page
1975 * @dev: target device
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.
1984 * Kernel thread context (may sleep).
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
;
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
)
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
;
2014 tf
.command
= ATA_CMD_READ_LOG_EXT
;
2015 tf
.protocol
= ATA_PROT_PIO
;
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");
2033 DPRINTK("EXIT, err_mask=%x\n", 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))
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");
2057 * Read IDENTIFY DEVICE data log, page 0, to figure out if the page is
2060 err
= ata_read_log_page(dev
, ATA_LOG_IDENTIFY_DEVICE
, 0, ap
->sector_buf
,
2064 "failed to get Device Identify Log Emask 0x%x\n",
2069 for (i
= 0; i
< ap
->sector_buf
[8]; i
++) {
2070 if (ap
->sector_buf
[9 + i
] == page
)
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
))
2085 if (dev
->horkage
& ATA_HORKAGE_1_5_GBPS
)
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
)
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
));
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
)
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");
2129 err_mask
= ata_read_log_page(dev
, ATA_LOG_NCQ_SEND_RECV
,
2130 0, ap
->sector_buf
, 1);
2133 "failed to get NCQ Send/Recv Log Emask 0x%x\n",
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
)) {
2156 "NCQ Send/Recv Log not supported\n");
2159 err_mask
= ata_read_log_page(dev
, ATA_LOG_NCQ_NON_DATA
,
2160 0, ap
->sector_buf
, 1);
2163 "failed to get NCQ Non-Data Log Emask 0x%x\n",
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
;
2182 err_mask
= ata_read_log_page(dev
,
2183 ATA_LOG_IDENTIFY_DEVICE
,
2184 ATA_LOG_SATA_SETTINGS
,
2189 "failed to get Identify Device data, Emask 0x%x\n",
2194 if (ap
->sector_buf
[ATA_LOG_NCQ_PRIO_OFFSET
] & BIT(3)) {
2195 dev
->flags
|= ATA_DFLAG_NCQ_PRIO
;
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
;
2211 if (!ata_id_has_ncq(dev
->id
)) {
2215 if (!IS_ENABLED(CONFIG_SATA_HOST
))
2217 if (dev
->horkage
& ATA_HORKAGE_NONCQ
) {
2218 snprintf(desc
, desc_sz
, "NCQ (not used)");
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
,
2233 "failed to enable AA (error_mask=0x%x)\n",
2235 if (err_mask
!= AC_ERR_DEV
) {
2236 dev
->horkage
|= ATA_HORKAGE_BROKEN_FPDMA_AA
;
2243 if (hdepth
>= ddepth
)
2244 snprintf(desc
, desc_sz
, "NCQ (depth %d)%s", ddepth
, aa_desc
);
2246 snprintf(desc
, desc_sz
, "NCQ (depth %d/%d)%s", hdepth
,
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
);
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
))
2268 if (ata_id_sense_reporting_enabled(dev
->id
))
2271 err_mask
= ata_dev_set_feature(dev
, SETFEATURE_SENSE_DATA
, 0x1);
2274 "failed to enable Sense Data Reporting, Emask 0x%x\n",
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
))
2303 if (!ata_identify_page_supported(dev
, ATA_LOG_ZONED_INFORMATION
)) {
2305 "ATA Zoned Information Log not supported\n");
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
,
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
;
2339 if (!ata_id_has_trusted(dev
->id
))
2342 if (!ata_identify_page_supported(dev
, ATA_LOG_SECURITY
)) {
2344 "Security Log not supported\n");
2348 err
= ata_read_log_page(dev
, ATA_LOG_IDENTIFY_DEVICE
, ATA_LOG_SECURITY
,
2352 "failed to read Security Log, Emask 0x%x\n", err
);
2356 trusted_cap
= get_unaligned_le64(&ap
->sector_buf
[40]);
2357 if (!(trusted_cap
& (1ULL << 63))) {
2359 "Trusted Computing capability qword not valid!\n");
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.
2375 * Kernel thread context (may sleep)
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];
2393 if (!ata_dev_enabled(dev
) && ata_msg_info(ap
)) {
2394 ata_dev_info(dev
, "%s: ENTER/EXIT -- nodev\n", __func__
);
2398 if (ata_msg_probe(ap
))
2399 ata_dev_dbg(dev
, "%s: ENTER\n", __func__
);
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
);
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"
2416 ata_dev_disable(dev
);
2420 rc
= ata_do_link_spd_horkage(dev
);
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
);
2442 /* massage HPA, do it early as it might change IDENTIFY data */
2443 rc
= ata_hpa_resize(dev
);
2447 /* print device capabilities */
2448 if (ata_msg_probe(ap
))
2450 "%s: cfg 49:%04x 82:%04x 83:%04x 84:%04x "
2451 "85:%04x 86:%04x 87:%04x 88:%04x\n",
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;
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
))
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
,
2480 ata_id_c_string(dev
->id
, modelbuf
, ATA_ID_PROD
,
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)
2489 "supports DRM functions and may not be fully accessible\n");
2490 snprintf(revbuf
, 7, "CFA");
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
))
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
))
2508 dev
->multi_count
= cnt
;
2511 if (ata_id_has_lba(id
)) {
2512 const char *lba_desc
;
2516 dev
->flags
|= ATA_DFLAG_LBA
;
2517 if (ata_id_has_lba48(id
)) {
2518 dev
->flags
|= ATA_DFLAG_LBA48
;
2521 if (dev
->n_sectors
>= (1UL << 28) &&
2522 ata_id_has_flush_ext(id
))
2523 dev
->flags
|= ATA_DFLAG_FLUSH_EXT
;
2527 rc
= ata_dev_config_ncq(dev
, ncq_desc
, sizeof(ncq_desc
));
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
));
2537 "%llu sectors, multi %u: %s %s\n",
2538 (unsigned long long)dev
->n_sectors
,
2539 dev
->multi_count
, lba_desc
, ncq_desc
);
2544 /* Default translation */
2545 dev
->cylinders
= id
[1];
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
));
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
;
2576 dev
->flags
|= ATA_DFLAG_DEVSLP
;
2577 err_mask
= ata_read_log_page(dev
,
2578 ATA_LOG_IDENTIFY_DEVICE
,
2579 ATA_LOG_SATA_SETTINGS
,
2584 "failed to get Identify Device Data, Emask 0x%x\n",
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
);
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
= "";
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");
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.
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
);
2628 "failed to enable ATAPI AN (err_mask=0x%x)\n",
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
;
2651 /* print device info to dmesg */
2652 if (ata_msg_drv(ap
) && print_info
)
2654 "ATAPI: %s, %s, max %s%s%s%s\n",
2656 ata_mode_string(xfer_mask
),
2657 cdb_intr_string
, atapi_an_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,
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
,
2685 if (dev
->horkage
& ATA_HORKAGE_MAX_SEC_1024
)
2686 dev
->max_sectors
= min_t(unsigned int, ATA_MAX_SECTORS_1024
,
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
2704 "Drive reports diagnostics failure. This may indicate a drive\n");
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");
2718 if (ata_msg_probe(ap
))
2719 ata_dev_dbg(dev
, "%s: EXIT, err\n", __func__
);
2724 * ata_cable_40wire - return 40 wire cable type
2727 * Helper method for drivers which want to hardwire 40 wire cable
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
2741 * Helper method for drivers which want to hardwire 80 wire cable
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.
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.
2768 * Helper method for drivers which don't use cable type to limit
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
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
2794 * Master ATA bus probing function. Initiates a hardware-dependent
2795 * bus reset, then attempts to identify any devices found on
2799 * PCI/etc. bus probe sem.
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
];
2810 struct ata_device
*dev
;
2812 ata_for_each_dev(dev
, &ap
->link
, ALL
)
2813 tries
[dev
->devno
] = ATA_PROBE_MAX_TRIES
;
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;
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
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
))
2859 rc
= ata_dev_read_id(dev
, &dev
->class, ATA_READID_POSTRESET
,
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
;
2889 /* configure transfer mode */
2890 rc
= ata_set_mode(&ap
->link
, &dev
);
2894 ata_for_each_dev(dev
, &ap
->link
, ENABLED
)
2900 tries
[dev
->devno
]--;
2904 /* eeek, something went very wrong, give up */
2905 tries
[dev
->devno
] = 0;
2909 /* give it just one more chance */
2910 tries
[dev
->devno
] = min(tries
[dev
->devno
], 1);
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
);
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.
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
))
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
);
2950 ata_link_info(link
, "SATA link down (SStatus %X SControl %X)\n",
2956 * ata_dev_pair - return other device on cable
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
))
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.
2988 * Inherited from caller.
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
;
2998 if (!sata_scr_valid(link
))
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;
3008 spd
= link
->sata_spd
;
3010 mask
= link
->sata_spd_limit
;
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
3028 mask
&= (1 << (spd
- 1)) - 1;
3032 /* were we already at the bottom? */
3037 if (mask
& ((1 << spd_limit
) - 1))
3038 mask
&= (1 << spd_limit
) - 1;
3040 bit
= ffs(mask
) - 1;
3045 link
->sata_spd_limit
= mask
;
3047 ata_link_warn(link
, "limiting SATA link speed to %s\n",
3048 sata_spd_string(fls(mask
)));
3053 #ifdef CONFIG_ATA_ACPI
3055 * ata_timing_cycle2mode - find xfer mode for the specified cycle duration
3056 * @xfer_shift: ATA_SHIFT_* value for transfer type to examine.
3057 * @cycle: cycle duration in ns
3059 * Return matching xfer mode for @cycle. The returned mode is of
3060 * the transfer type specified by @xfer_shift. If @cycle is too
3061 * slow for @xfer_shift, 0xff is returned. If @cycle is faster
3062 * than the fastest known mode, the fasted mode is returned.
3068 * Matching xfer_mode, 0xff if no match found.
3070 u8
ata_timing_cycle2mode(unsigned int xfer_shift
, int cycle
)
3072 u8 base_mode
= 0xff, last_mode
= 0xff;
3073 const struct ata_xfer_ent
*ent
;
3074 const struct ata_timing
*t
;
3076 for (ent
= ata_xfer_tbl
; ent
->shift
>= 0; ent
++)
3077 if (ent
->shift
== xfer_shift
)
3078 base_mode
= ent
->base
;
3080 for (t
= ata_timing_find_mode(base_mode
);
3081 t
&& ata_xfer_mode2shift(t
->mode
) == xfer_shift
; t
++) {
3082 unsigned short this_cycle
;
3084 switch (xfer_shift
) {
3086 case ATA_SHIFT_MWDMA
:
3087 this_cycle
= t
->cycle
;
3089 case ATA_SHIFT_UDMA
:
3090 this_cycle
= t
->udma
;
3096 if (cycle
> this_cycle
)
3099 last_mode
= t
->mode
;
3107 * ata_down_xfermask_limit - adjust dev xfer masks downward
3108 * @dev: Device to adjust xfer masks
3109 * @sel: ATA_DNXFER_* selector
3111 * Adjust xfer masks of @dev downward. Note that this function
3112 * does not apply the change. Invoking ata_set_mode() afterwards
3113 * will apply the limit.
3116 * Inherited from caller.
3119 * 0 on success, negative errno on failure
3121 int ata_down_xfermask_limit(struct ata_device
*dev
, unsigned int sel
)
3124 unsigned long orig_mask
, xfer_mask
;
3125 unsigned long pio_mask
, mwdma_mask
, udma_mask
;
3128 quiet
= !!(sel
& ATA_DNXFER_QUIET
);
3129 sel
&= ~ATA_DNXFER_QUIET
;
3131 xfer_mask
= orig_mask
= ata_pack_xfermask(dev
->pio_mask
,
3134 ata_unpack_xfermask(xfer_mask
, &pio_mask
, &mwdma_mask
, &udma_mask
);
3137 case ATA_DNXFER_PIO
:
3138 highbit
= fls(pio_mask
) - 1;
3139 pio_mask
&= ~(1 << highbit
);
3142 case ATA_DNXFER_DMA
:
3144 highbit
= fls(udma_mask
) - 1;
3145 udma_mask
&= ~(1 << highbit
);
3148 } else if (mwdma_mask
) {
3149 highbit
= fls(mwdma_mask
) - 1;
3150 mwdma_mask
&= ~(1 << highbit
);
3156 case ATA_DNXFER_40C
:
3157 udma_mask
&= ATA_UDMA_MASK_40C
;
3160 case ATA_DNXFER_FORCE_PIO0
:
3163 case ATA_DNXFER_FORCE_PIO
:
3172 xfer_mask
&= ata_pack_xfermask(pio_mask
, mwdma_mask
, udma_mask
);
3174 if (!(xfer_mask
& ATA_MASK_PIO
) || xfer_mask
== orig_mask
)
3178 if (xfer_mask
& (ATA_MASK_MWDMA
| ATA_MASK_UDMA
))
3179 snprintf(buf
, sizeof(buf
), "%s:%s",
3180 ata_mode_string(xfer_mask
),
3181 ata_mode_string(xfer_mask
& ATA_MASK_PIO
));
3183 snprintf(buf
, sizeof(buf
), "%s",
3184 ata_mode_string(xfer_mask
));
3186 ata_dev_warn(dev
, "limiting speed to %s\n", buf
);
3189 ata_unpack_xfermask(xfer_mask
, &dev
->pio_mask
, &dev
->mwdma_mask
,
3195 static int ata_dev_set_mode(struct ata_device
*dev
)
3197 struct ata_port
*ap
= dev
->link
->ap
;
3198 struct ata_eh_context
*ehc
= &dev
->link
->eh_context
;
3199 const bool nosetxfer
= dev
->horkage
& ATA_HORKAGE_NOSETXFER
;
3200 const char *dev_err_whine
= "";
3201 int ign_dev_err
= 0;
3202 unsigned int err_mask
= 0;
3205 dev
->flags
&= ~ATA_DFLAG_PIO
;
3206 if (dev
->xfer_shift
== ATA_SHIFT_PIO
)
3207 dev
->flags
|= ATA_DFLAG_PIO
;
3209 if (nosetxfer
&& ap
->flags
& ATA_FLAG_SATA
&& ata_id_is_sata(dev
->id
))
3210 dev_err_whine
= " (SET_XFERMODE skipped)";
3214 "NOSETXFER but PATA detected - can't "
3215 "skip SETXFER, might malfunction\n");
3216 err_mask
= ata_dev_set_xfermode(dev
);
3219 if (err_mask
& ~AC_ERR_DEV
)
3223 ehc
->i
.flags
|= ATA_EHI_POST_SETMODE
;
3224 rc
= ata_dev_revalidate(dev
, ATA_DEV_UNKNOWN
, 0);
3225 ehc
->i
.flags
&= ~ATA_EHI_POST_SETMODE
;
3229 if (dev
->xfer_shift
== ATA_SHIFT_PIO
) {
3230 /* Old CFA may refuse this command, which is just fine */
3231 if (ata_id_is_cfa(dev
->id
))
3233 /* Catch several broken garbage emulations plus some pre
3235 if (ata_id_major_version(dev
->id
) == 0 &&
3236 dev
->pio_mode
<= XFER_PIO_2
)
3238 /* Some very old devices and some bad newer ones fail
3239 any kind of SET_XFERMODE request but support PIO0-2
3240 timings and no IORDY */
3241 if (!ata_id_has_iordy(dev
->id
) && dev
->pio_mode
<= XFER_PIO_2
)
3244 /* Early MWDMA devices do DMA but don't allow DMA mode setting.
3245 Don't fail an MWDMA0 set IFF the device indicates it is in MWDMA0 */
3246 if (dev
->xfer_shift
== ATA_SHIFT_MWDMA
&&
3247 dev
->dma_mode
== XFER_MW_DMA_0
&&
3248 (dev
->id
[63] >> 8) & 1)
3251 /* if the device is actually configured correctly, ignore dev err */
3252 if (dev
->xfer_mode
== ata_xfer_mask2mode(ata_id_xfermask(dev
->id
)))
3255 if (err_mask
& AC_ERR_DEV
) {
3259 dev_err_whine
= " (device error ignored)";
3262 DPRINTK("xfer_shift=%u, xfer_mode=0x%x\n",
3263 dev
->xfer_shift
, (int)dev
->xfer_mode
);
3265 if (!(ehc
->i
.flags
& ATA_EHI_QUIET
) ||
3266 ehc
->i
.flags
& ATA_EHI_DID_HARDRESET
)
3267 ata_dev_info(dev
, "configured for %s%s\n",
3268 ata_mode_string(ata_xfer_mode2mask(dev
->xfer_mode
)),
3274 ata_dev_err(dev
, "failed to set xfermode (err_mask=0x%x)\n", err_mask
);
3279 * ata_do_set_mode - Program timings and issue SET FEATURES - XFER
3280 * @link: link on which timings will be programmed
3281 * @r_failed_dev: out parameter for failed device
3283 * Standard implementation of the function used to tune and set
3284 * ATA device disk transfer mode (PIO3, UDMA6, etc.). If
3285 * ata_dev_set_mode() fails, pointer to the failing device is
3286 * returned in @r_failed_dev.
3289 * PCI/etc. bus probe sem.
3292 * 0 on success, negative errno otherwise
3295 int ata_do_set_mode(struct ata_link
*link
, struct ata_device
**r_failed_dev
)
3297 struct ata_port
*ap
= link
->ap
;
3298 struct ata_device
*dev
;
3299 int rc
= 0, used_dma
= 0, found
= 0;
3301 /* step 1: calculate xfer_mask */
3302 ata_for_each_dev(dev
, link
, ENABLED
) {
3303 unsigned long pio_mask
, dma_mask
;
3304 unsigned int mode_mask
;
3306 mode_mask
= ATA_DMA_MASK_ATA
;
3307 if (dev
->class == ATA_DEV_ATAPI
)
3308 mode_mask
= ATA_DMA_MASK_ATAPI
;
3309 else if (ata_id_is_cfa(dev
->id
))
3310 mode_mask
= ATA_DMA_MASK_CFA
;
3312 ata_dev_xfermask(dev
);
3313 ata_force_xfermask(dev
);
3315 pio_mask
= ata_pack_xfermask(dev
->pio_mask
, 0, 0);
3317 if (libata_dma_mask
& mode_mask
)
3318 dma_mask
= ata_pack_xfermask(0, dev
->mwdma_mask
,
3323 dev
->pio_mode
= ata_xfer_mask2mode(pio_mask
);
3324 dev
->dma_mode
= ata_xfer_mask2mode(dma_mask
);
3327 if (ata_dma_enabled(dev
))
3333 /* step 2: always set host PIO timings */
3334 ata_for_each_dev(dev
, link
, ENABLED
) {
3335 if (dev
->pio_mode
== 0xff) {
3336 ata_dev_warn(dev
, "no PIO support\n");
3341 dev
->xfer_mode
= dev
->pio_mode
;
3342 dev
->xfer_shift
= ATA_SHIFT_PIO
;
3343 if (ap
->ops
->set_piomode
)
3344 ap
->ops
->set_piomode(ap
, dev
);
3347 /* step 3: set host DMA timings */
3348 ata_for_each_dev(dev
, link
, ENABLED
) {
3349 if (!ata_dma_enabled(dev
))
3352 dev
->xfer_mode
= dev
->dma_mode
;
3353 dev
->xfer_shift
= ata_xfer_mode2shift(dev
->dma_mode
);
3354 if (ap
->ops
->set_dmamode
)
3355 ap
->ops
->set_dmamode(ap
, dev
);
3358 /* step 4: update devices' xfer mode */
3359 ata_for_each_dev(dev
, link
, ENABLED
) {
3360 rc
= ata_dev_set_mode(dev
);
3365 /* Record simplex status. If we selected DMA then the other
3366 * host channels are not permitted to do so.
3368 if (used_dma
&& (ap
->host
->flags
& ATA_HOST_SIMPLEX
))
3369 ap
->host
->simplex_claimed
= ap
;
3373 *r_failed_dev
= dev
;
3376 EXPORT_SYMBOL_GPL(ata_do_set_mode
);
3379 * ata_wait_ready - wait for link to become ready
3380 * @link: link to be waited on
3381 * @deadline: deadline jiffies for the operation
3382 * @check_ready: callback to check link readiness
3384 * Wait for @link to become ready. @check_ready should return
3385 * positive number if @link is ready, 0 if it isn't, -ENODEV if
3386 * link doesn't seem to be occupied, other errno for other error
3389 * Transient -ENODEV conditions are allowed for
3390 * ATA_TMOUT_FF_WAIT.
3396 * 0 if @link is ready before @deadline; otherwise, -errno.
3398 int ata_wait_ready(struct ata_link
*link
, unsigned long deadline
,
3399 int (*check_ready
)(struct ata_link
*link
))
3401 unsigned long start
= jiffies
;
3402 unsigned long nodev_deadline
;
3405 /* choose which 0xff timeout to use, read comment in libata.h */
3406 if (link
->ap
->host
->flags
& ATA_HOST_PARALLEL_SCAN
)
3407 nodev_deadline
= ata_deadline(start
, ATA_TMOUT_FF_WAIT_LONG
);
3409 nodev_deadline
= ata_deadline(start
, ATA_TMOUT_FF_WAIT
);
3411 /* Slave readiness can't be tested separately from master. On
3412 * M/S emulation configuration, this function should be called
3413 * only on the master and it will handle both master and slave.
3415 WARN_ON(link
== link
->ap
->slave_link
);
3417 if (time_after(nodev_deadline
, deadline
))
3418 nodev_deadline
= deadline
;
3421 unsigned long now
= jiffies
;
3424 ready
= tmp
= check_ready(link
);
3429 * -ENODEV could be transient. Ignore -ENODEV if link
3430 * is online. Also, some SATA devices take a long
3431 * time to clear 0xff after reset. Wait for
3432 * ATA_TMOUT_FF_WAIT[_LONG] on -ENODEV if link isn't
3435 * Note that some PATA controllers (pata_ali) explode
3436 * if status register is read more than once when
3437 * there's no device attached.
3439 if (ready
== -ENODEV
) {
3440 if (ata_link_online(link
))
3442 else if ((link
->ap
->flags
& ATA_FLAG_SATA
) &&
3443 !ata_link_offline(link
) &&
3444 time_before(now
, nodev_deadline
))
3450 if (time_after(now
, deadline
))
3453 if (!warned
&& time_after(now
, start
+ 5 * HZ
) &&
3454 (deadline
- now
> 3 * HZ
)) {
3456 "link is slow to respond, please be patient "
3457 "(ready=%d)\n", tmp
);
3461 ata_msleep(link
->ap
, 50);
3466 * ata_wait_after_reset - wait for link to become ready after reset
3467 * @link: link to be waited on
3468 * @deadline: deadline jiffies for the operation
3469 * @check_ready: callback to check link readiness
3471 * Wait for @link to become ready after reset.
3477 * 0 if @link is ready before @deadline; otherwise, -errno.
3479 int ata_wait_after_reset(struct ata_link
*link
, unsigned long deadline
,
3480 int (*check_ready
)(struct ata_link
*link
))
3482 ata_msleep(link
->ap
, ATA_WAIT_AFTER_RESET
);
3484 return ata_wait_ready(link
, deadline
, check_ready
);
3486 EXPORT_SYMBOL_GPL(ata_wait_after_reset
);
3489 * ata_std_prereset - prepare for reset
3490 * @link: ATA link to be reset
3491 * @deadline: deadline jiffies for the operation
3493 * @link is about to be reset. Initialize it. Failure from
3494 * prereset makes libata abort whole reset sequence and give up
3495 * that port, so prereset should be best-effort. It does its
3496 * best to prepare for reset sequence but if things go wrong, it
3497 * should just whine, not fail.
3500 * Kernel thread context (may sleep)
3503 * 0 on success, -errno otherwise.
3505 int ata_std_prereset(struct ata_link
*link
, unsigned long deadline
)
3507 struct ata_port
*ap
= link
->ap
;
3508 struct ata_eh_context
*ehc
= &link
->eh_context
;
3509 const unsigned long *timing
= sata_ehc_deb_timing(ehc
);
3512 /* if we're about to do hardreset, nothing more to do */
3513 if (ehc
->i
.action
& ATA_EH_HARDRESET
)
3516 /* if SATA, resume link */
3517 if (ap
->flags
& ATA_FLAG_SATA
) {
3518 rc
= sata_link_resume(link
, timing
, deadline
);
3519 /* whine about phy resume failure but proceed */
3520 if (rc
&& rc
!= -EOPNOTSUPP
)
3522 "failed to resume link for reset (errno=%d)\n",
3526 /* no point in trying softreset on offline link */
3527 if (ata_phys_link_offline(link
))
3528 ehc
->i
.action
&= ~ATA_EH_SOFTRESET
;
3532 EXPORT_SYMBOL_GPL(ata_std_prereset
);
3535 * sata_std_hardreset - COMRESET w/o waiting or classification
3536 * @link: link to reset
3537 * @class: resulting class of attached device
3538 * @deadline: deadline jiffies for the operation
3540 * Standard SATA COMRESET w/o waiting or classification.
3543 * Kernel thread context (may sleep)
3546 * 0 if link offline, -EAGAIN if link online, -errno on errors.
3548 int sata_std_hardreset(struct ata_link
*link
, unsigned int *class,
3549 unsigned long deadline
)
3551 const unsigned long *timing
= sata_ehc_deb_timing(&link
->eh_context
);
3556 rc
= sata_link_hardreset(link
, timing
, deadline
, &online
, NULL
);
3557 return online
? -EAGAIN
: rc
;
3559 EXPORT_SYMBOL_GPL(sata_std_hardreset
);
3562 * ata_std_postreset - standard postreset callback
3563 * @link: the target ata_link
3564 * @classes: classes of attached devices
3566 * This function is invoked after a successful reset. Note that
3567 * the device might have been reset more than once using
3568 * different reset methods before postreset is invoked.
3571 * Kernel thread context (may sleep)
3573 void ata_std_postreset(struct ata_link
*link
, unsigned int *classes
)
3579 /* reset complete, clear SError */
3580 if (!sata_scr_read(link
, SCR_ERROR
, &serror
))
3581 sata_scr_write(link
, SCR_ERROR
, serror
);
3583 /* print link status */
3584 sata_print_link_status(link
);
3588 EXPORT_SYMBOL_GPL(ata_std_postreset
);
3591 * ata_dev_same_device - Determine whether new ID matches configured device
3592 * @dev: device to compare against
3593 * @new_class: class of the new device
3594 * @new_id: IDENTIFY page of the new device
3596 * Compare @new_class and @new_id against @dev and determine
3597 * whether @dev is the device indicated by @new_class and
3604 * 1 if @dev matches @new_class and @new_id, 0 otherwise.
3606 static int ata_dev_same_device(struct ata_device
*dev
, unsigned int new_class
,
3609 const u16
*old_id
= dev
->id
;
3610 unsigned char model
[2][ATA_ID_PROD_LEN
+ 1];
3611 unsigned char serial
[2][ATA_ID_SERNO_LEN
+ 1];
3613 if (dev
->class != new_class
) {
3614 ata_dev_info(dev
, "class mismatch %d != %d\n",
3615 dev
->class, new_class
);
3619 ata_id_c_string(old_id
, model
[0], ATA_ID_PROD
, sizeof(model
[0]));
3620 ata_id_c_string(new_id
, model
[1], ATA_ID_PROD
, sizeof(model
[1]));
3621 ata_id_c_string(old_id
, serial
[0], ATA_ID_SERNO
, sizeof(serial
[0]));
3622 ata_id_c_string(new_id
, serial
[1], ATA_ID_SERNO
, sizeof(serial
[1]));
3624 if (strcmp(model
[0], model
[1])) {
3625 ata_dev_info(dev
, "model number mismatch '%s' != '%s'\n",
3626 model
[0], model
[1]);
3630 if (strcmp(serial
[0], serial
[1])) {
3631 ata_dev_info(dev
, "serial number mismatch '%s' != '%s'\n",
3632 serial
[0], serial
[1]);
3640 * ata_dev_reread_id - Re-read IDENTIFY data
3641 * @dev: target ATA device
3642 * @readid_flags: read ID flags
3644 * Re-read IDENTIFY page and make sure @dev is still attached to
3648 * Kernel thread context (may sleep)
3651 * 0 on success, negative errno otherwise
3653 int ata_dev_reread_id(struct ata_device
*dev
, unsigned int readid_flags
)
3655 unsigned int class = dev
->class;
3656 u16
*id
= (void *)dev
->link
->ap
->sector_buf
;
3660 rc
= ata_dev_read_id(dev
, &class, readid_flags
, id
);
3664 /* is the device still there? */
3665 if (!ata_dev_same_device(dev
, class, id
))
3668 memcpy(dev
->id
, id
, sizeof(id
[0]) * ATA_ID_WORDS
);
3673 * ata_dev_revalidate - Revalidate ATA device
3674 * @dev: device to revalidate
3675 * @new_class: new class code
3676 * @readid_flags: read ID flags
3678 * Re-read IDENTIFY page, make sure @dev is still attached to the
3679 * port and reconfigure it according to the new IDENTIFY page.
3682 * Kernel thread context (may sleep)
3685 * 0 on success, negative errno otherwise
3687 int ata_dev_revalidate(struct ata_device
*dev
, unsigned int new_class
,
3688 unsigned int readid_flags
)
3690 u64 n_sectors
= dev
->n_sectors
;
3691 u64 n_native_sectors
= dev
->n_native_sectors
;
3694 if (!ata_dev_enabled(dev
))
3697 /* fail early if !ATA && !ATAPI to avoid issuing [P]IDENTIFY to PMP */
3698 if (ata_class_enabled(new_class
) &&
3699 new_class
!= ATA_DEV_ATA
&&
3700 new_class
!= ATA_DEV_ATAPI
&&
3701 new_class
!= ATA_DEV_ZAC
&&
3702 new_class
!= ATA_DEV_SEMB
) {
3703 ata_dev_info(dev
, "class mismatch %u != %u\n",
3704 dev
->class, new_class
);
3710 rc
= ata_dev_reread_id(dev
, readid_flags
);
3714 /* configure device according to the new ID */
3715 rc
= ata_dev_configure(dev
);
3719 /* verify n_sectors hasn't changed */
3720 if (dev
->class != ATA_DEV_ATA
|| !n_sectors
||
3721 dev
->n_sectors
== n_sectors
)
3724 /* n_sectors has changed */
3725 ata_dev_warn(dev
, "n_sectors mismatch %llu != %llu\n",
3726 (unsigned long long)n_sectors
,
3727 (unsigned long long)dev
->n_sectors
);
3730 * Something could have caused HPA to be unlocked
3731 * involuntarily. If n_native_sectors hasn't changed and the
3732 * new size matches it, keep the device.
3734 if (dev
->n_native_sectors
== n_native_sectors
&&
3735 dev
->n_sectors
> n_sectors
&& dev
->n_sectors
== n_native_sectors
) {
3737 "new n_sectors matches native, probably "
3738 "late HPA unlock, n_sectors updated\n");
3739 /* use the larger n_sectors */
3744 * Some BIOSes boot w/o HPA but resume w/ HPA locked. Try
3745 * unlocking HPA in those cases.
3747 * https://bugzilla.kernel.org/show_bug.cgi?id=15396
3749 if (dev
->n_native_sectors
== n_native_sectors
&&
3750 dev
->n_sectors
< n_sectors
&& n_sectors
== n_native_sectors
&&
3751 !(dev
->horkage
& ATA_HORKAGE_BROKEN_HPA
)) {
3753 "old n_sectors matches native, probably "
3754 "late HPA lock, will try to unlock HPA\n");
3755 /* try unlocking HPA */
3756 dev
->flags
|= ATA_DFLAG_UNLOCK_HPA
;
3761 /* restore original n_[native_]sectors and fail */
3762 dev
->n_native_sectors
= n_native_sectors
;
3763 dev
->n_sectors
= n_sectors
;
3765 ata_dev_err(dev
, "revalidation failed (errno=%d)\n", rc
);
3769 struct ata_blacklist_entry
{
3770 const char *model_num
;
3771 const char *model_rev
;
3772 unsigned long horkage
;
3775 static const struct ata_blacklist_entry ata_device_blacklist
[] = {
3776 /* Devices with DMA related problems under Linux */
3777 { "WDC AC11000H", NULL
, ATA_HORKAGE_NODMA
},
3778 { "WDC AC22100H", NULL
, ATA_HORKAGE_NODMA
},
3779 { "WDC AC32500H", NULL
, ATA_HORKAGE_NODMA
},
3780 { "WDC AC33100H", NULL
, ATA_HORKAGE_NODMA
},
3781 { "WDC AC31600H", NULL
, ATA_HORKAGE_NODMA
},
3782 { "WDC AC32100H", "24.09P07", ATA_HORKAGE_NODMA
},
3783 { "WDC AC23200L", "21.10N21", ATA_HORKAGE_NODMA
},
3784 { "Compaq CRD-8241B", NULL
, ATA_HORKAGE_NODMA
},
3785 { "CRD-8400B", NULL
, ATA_HORKAGE_NODMA
},
3786 { "CRD-848[02]B", NULL
, ATA_HORKAGE_NODMA
},
3787 { "CRD-84", NULL
, ATA_HORKAGE_NODMA
},
3788 { "SanDisk SDP3B", NULL
, ATA_HORKAGE_NODMA
},
3789 { "SanDisk SDP3B-64", NULL
, ATA_HORKAGE_NODMA
},
3790 { "SANYO CD-ROM CRD", NULL
, ATA_HORKAGE_NODMA
},
3791 { "HITACHI CDR-8", NULL
, ATA_HORKAGE_NODMA
},
3792 { "HITACHI CDR-8[34]35",NULL
, ATA_HORKAGE_NODMA
},
3793 { "Toshiba CD-ROM XM-6202B", NULL
, ATA_HORKAGE_NODMA
},
3794 { "TOSHIBA CD-ROM XM-1702BC", NULL
, ATA_HORKAGE_NODMA
},
3795 { "CD-532E-A", NULL
, ATA_HORKAGE_NODMA
},
3796 { "E-IDE CD-ROM CR-840",NULL
, ATA_HORKAGE_NODMA
},
3797 { "CD-ROM Drive/F5A", NULL
, ATA_HORKAGE_NODMA
},
3798 { "WPI CDD-820", NULL
, ATA_HORKAGE_NODMA
},
3799 { "SAMSUNG CD-ROM SC-148C", NULL
, ATA_HORKAGE_NODMA
},
3800 { "SAMSUNG CD-ROM SC", NULL
, ATA_HORKAGE_NODMA
},
3801 { "ATAPI CD-ROM DRIVE 40X MAXIMUM",NULL
,ATA_HORKAGE_NODMA
},
3802 { "_NEC DV5800A", NULL
, ATA_HORKAGE_NODMA
},
3803 { "SAMSUNG CD-ROM SN-124", "N001", ATA_HORKAGE_NODMA
},
3804 { "Seagate STT20000A", NULL
, ATA_HORKAGE_NODMA
},
3805 { " 2GB ATA Flash Disk", "ADMA428M", ATA_HORKAGE_NODMA
},
3806 { "VRFDFC22048UCHC-TE*", NULL
, ATA_HORKAGE_NODMA
},
3807 /* Odd clown on sil3726/4726 PMPs */
3808 { "Config Disk", NULL
, ATA_HORKAGE_DISABLE
},
3810 /* Weird ATAPI devices */
3811 { "TORiSAN DVD-ROM DRD-N216", NULL
, ATA_HORKAGE_MAX_SEC_128
},
3812 { "QUANTUM DAT DAT72-000", NULL
, ATA_HORKAGE_ATAPI_MOD16_DMA
},
3813 { "Slimtype DVD A DS8A8SH", NULL
, ATA_HORKAGE_MAX_SEC_LBA48
},
3814 { "Slimtype DVD A DS8A9SH", NULL
, ATA_HORKAGE_MAX_SEC_LBA48
},
3817 * Causes silent data corruption with higher max sects.
3818 * http://lkml.kernel.org/g/x49wpy40ysk.fsf@segfault.boston.devel.redhat.com
3820 { "ST380013AS", "3.20", ATA_HORKAGE_MAX_SEC_1024
},
3823 * These devices time out with higher max sects.
3824 * https://bugzilla.kernel.org/show_bug.cgi?id=121671
3826 { "LITEON CX1-JB*-HP", NULL
, ATA_HORKAGE_MAX_SEC_1024
},
3827 { "LITEON EP1-*", NULL
, ATA_HORKAGE_MAX_SEC_1024
},
3829 /* Devices we expect to fail diagnostics */
3831 /* Devices where NCQ should be avoided */
3833 { "WDC WD740ADFD-00", NULL
, ATA_HORKAGE_NONCQ
},
3834 { "WDC WD740ADFD-00NLR1", NULL
, ATA_HORKAGE_NONCQ
, },
3835 /* http://thread.gmane.org/gmane.linux.ide/14907 */
3836 { "FUJITSU MHT2060BH", NULL
, ATA_HORKAGE_NONCQ
},
3838 { "Maxtor *", "BANC*", ATA_HORKAGE_NONCQ
},
3839 { "Maxtor 7V300F0", "VA111630", ATA_HORKAGE_NONCQ
},
3840 { "ST380817AS", "3.42", ATA_HORKAGE_NONCQ
},
3841 { "ST3160023AS", "3.42", ATA_HORKAGE_NONCQ
},
3842 { "OCZ CORE_SSD", "02.10104", ATA_HORKAGE_NONCQ
},
3844 /* Seagate NCQ + FLUSH CACHE firmware bug */
3845 { "ST31500341AS", "SD1[5-9]", ATA_HORKAGE_NONCQ
|
3846 ATA_HORKAGE_FIRMWARE_WARN
},
3848 { "ST31000333AS", "SD1[5-9]", ATA_HORKAGE_NONCQ
|
3849 ATA_HORKAGE_FIRMWARE_WARN
},
3851 { "ST3640[36]23AS", "SD1[5-9]", ATA_HORKAGE_NONCQ
|
3852 ATA_HORKAGE_FIRMWARE_WARN
},
3854 { "ST3320[68]13AS", "SD1[5-9]", ATA_HORKAGE_NONCQ
|
3855 ATA_HORKAGE_FIRMWARE_WARN
},
3857 /* drives which fail FPDMA_AA activation (some may freeze afterwards)
3858 the ST disks also have LPM issues */
3859 { "ST1000LM024 HN-M101MBB", NULL
, ATA_HORKAGE_BROKEN_FPDMA_AA
|
3860 ATA_HORKAGE_NOLPM
, },
3861 { "VB0250EAVER", "HPG7", ATA_HORKAGE_BROKEN_FPDMA_AA
},
3863 /* Blacklist entries taken from Silicon Image 3124/3132
3864 Windows driver .inf file - also several Linux problem reports */
3865 { "HTS541060G9SA00", "MB3OC60D", ATA_HORKAGE_NONCQ
, },
3866 { "HTS541080G9SA00", "MB4OC60D", ATA_HORKAGE_NONCQ
, },
3867 { "HTS541010G9SA00", "MBZOC60D", ATA_HORKAGE_NONCQ
, },
3869 /* https://bugzilla.kernel.org/show_bug.cgi?id=15573 */
3870 { "C300-CTFDDAC128MAG", "0001", ATA_HORKAGE_NONCQ
, },
3872 /* Some Sandisk SSDs lock up hard with NCQ enabled. Reported on
3873 SD7SN6S256G and SD8SN8U256G */
3874 { "SanDisk SD[78]SN*G", NULL
, ATA_HORKAGE_NONCQ
, },
3876 /* devices which puke on READ_NATIVE_MAX */
3877 { "HDS724040KLSA80", "KFAOA20N", ATA_HORKAGE_BROKEN_HPA
, },
3878 { "WDC WD3200JD-00KLB0", "WD-WCAMR1130137", ATA_HORKAGE_BROKEN_HPA
},
3879 { "WDC WD2500JD-00HBB0", "WD-WMAL71490727", ATA_HORKAGE_BROKEN_HPA
},
3880 { "MAXTOR 6L080L4", "A93.0500", ATA_HORKAGE_BROKEN_HPA
},
3882 /* this one allows HPA unlocking but fails IOs on the area */
3883 { "OCZ-VERTEX", "1.30", ATA_HORKAGE_BROKEN_HPA
},
3885 /* Devices which report 1 sector over size HPA */
3886 { "ST340823A", NULL
, ATA_HORKAGE_HPA_SIZE
, },
3887 { "ST320413A", NULL
, ATA_HORKAGE_HPA_SIZE
, },
3888 { "ST310211A", NULL
, ATA_HORKAGE_HPA_SIZE
, },
3890 /* Devices which get the IVB wrong */
3891 { "QUANTUM FIREBALLlct10 05", "A03.0900", ATA_HORKAGE_IVB
, },
3892 /* Maybe we should just blacklist TSSTcorp... */
3893 { "TSSTcorp CDDVDW SH-S202[HJN]", "SB0[01]", ATA_HORKAGE_IVB
, },
3895 /* Devices that do not need bridging limits applied */
3896 { "MTRON MSP-SATA*", NULL
, ATA_HORKAGE_BRIDGE_OK
, },
3897 { "BUFFALO HD-QSU2/R5", NULL
, ATA_HORKAGE_BRIDGE_OK
, },
3899 /* Devices which aren't very happy with higher link speeds */
3900 { "WD My Book", NULL
, ATA_HORKAGE_1_5_GBPS
, },
3901 { "Seagate FreeAgent GoFlex", NULL
, ATA_HORKAGE_1_5_GBPS
, },
3904 * Devices which choke on SETXFER. Applies only if both the
3905 * device and controller are SATA.
3907 { "PIONEER DVD-RW DVRTD08", NULL
, ATA_HORKAGE_NOSETXFER
},
3908 { "PIONEER DVD-RW DVRTD08A", NULL
, ATA_HORKAGE_NOSETXFER
},
3909 { "PIONEER DVD-RW DVR-215", NULL
, ATA_HORKAGE_NOSETXFER
},
3910 { "PIONEER DVD-RW DVR-212D", NULL
, ATA_HORKAGE_NOSETXFER
},
3911 { "PIONEER DVD-RW DVR-216D", NULL
, ATA_HORKAGE_NOSETXFER
},
3913 /* Crucial BX100 SSD 500GB has broken LPM support */
3914 { "CT500BX100SSD1", NULL
, ATA_HORKAGE_NOLPM
},
3916 /* 512GB MX100 with MU01 firmware has both queued TRIM and LPM issues */
3917 { "Crucial_CT512MX100*", "MU01", ATA_HORKAGE_NO_NCQ_TRIM
|
3918 ATA_HORKAGE_ZERO_AFTER_TRIM
|
3919 ATA_HORKAGE_NOLPM
, },
3920 /* 512GB MX100 with newer firmware has only LPM issues */
3921 { "Crucial_CT512MX100*", NULL
, ATA_HORKAGE_ZERO_AFTER_TRIM
|
3922 ATA_HORKAGE_NOLPM
, },
3924 /* 480GB+ M500 SSDs have both queued TRIM and LPM issues */
3925 { "Crucial_CT480M500*", NULL
, ATA_HORKAGE_NO_NCQ_TRIM
|
3926 ATA_HORKAGE_ZERO_AFTER_TRIM
|
3927 ATA_HORKAGE_NOLPM
, },
3928 { "Crucial_CT960M500*", NULL
, ATA_HORKAGE_NO_NCQ_TRIM
|
3929 ATA_HORKAGE_ZERO_AFTER_TRIM
|
3930 ATA_HORKAGE_NOLPM
, },
3932 /* These specific Samsung models/firmware-revs do not handle LPM well */
3933 { "SAMSUNG MZMPC128HBFU-000MV", "CXM14M1Q", ATA_HORKAGE_NOLPM
, },
3934 { "SAMSUNG SSD PM830 mSATA *", "CXM13D1Q", ATA_HORKAGE_NOLPM
, },
3935 { "SAMSUNG MZ7TD256HAFV-000L9", NULL
, ATA_HORKAGE_NOLPM
, },
3936 { "SAMSUNG MZ7TE512HMHP-000L1", "EXT06L0Q", ATA_HORKAGE_NOLPM
, },
3938 /* devices that don't properly handle queued TRIM commands */
3939 { "Micron_M500IT_*", "MU01", ATA_HORKAGE_NO_NCQ_TRIM
|
3940 ATA_HORKAGE_ZERO_AFTER_TRIM
, },
3941 { "Micron_M500_*", NULL
, ATA_HORKAGE_NO_NCQ_TRIM
|
3942 ATA_HORKAGE_ZERO_AFTER_TRIM
, },
3943 { "Crucial_CT*M500*", NULL
, ATA_HORKAGE_NO_NCQ_TRIM
|
3944 ATA_HORKAGE_ZERO_AFTER_TRIM
, },
3945 { "Micron_M5[15]0_*", "MU01", ATA_HORKAGE_NO_NCQ_TRIM
|
3946 ATA_HORKAGE_ZERO_AFTER_TRIM
, },
3947 { "Crucial_CT*M550*", "MU01", ATA_HORKAGE_NO_NCQ_TRIM
|
3948 ATA_HORKAGE_ZERO_AFTER_TRIM
, },
3949 { "Crucial_CT*MX100*", "MU01", ATA_HORKAGE_NO_NCQ_TRIM
|
3950 ATA_HORKAGE_ZERO_AFTER_TRIM
, },
3951 { "Samsung SSD 840*", NULL
, ATA_HORKAGE_NO_NCQ_TRIM
|
3952 ATA_HORKAGE_ZERO_AFTER_TRIM
, },
3953 { "Samsung SSD 850*", NULL
, ATA_HORKAGE_NO_NCQ_TRIM
|
3954 ATA_HORKAGE_ZERO_AFTER_TRIM
, },
3955 { "FCCT*M500*", NULL
, ATA_HORKAGE_NO_NCQ_TRIM
|
3956 ATA_HORKAGE_ZERO_AFTER_TRIM
, },
3958 /* devices that don't properly handle TRIM commands */
3959 { "SuperSSpeed S238*", NULL
, ATA_HORKAGE_NOTRIM
, },
3962 * As defined, the DRAT (Deterministic Read After Trim) and RZAT
3963 * (Return Zero After Trim) flags in the ATA Command Set are
3964 * unreliable in the sense that they only define what happens if
3965 * the device successfully executed the DSM TRIM command. TRIM
3966 * is only advisory, however, and the device is free to silently
3967 * ignore all or parts of the request.
3969 * Whitelist drives that are known to reliably return zeroes
3974 * The intel 510 drive has buggy DRAT/RZAT. Explicitly exclude
3975 * that model before whitelisting all other intel SSDs.
3977 { "INTEL*SSDSC2MH*", NULL
, 0, },
3979 { "Micron*", NULL
, ATA_HORKAGE_ZERO_AFTER_TRIM
, },
3980 { "Crucial*", NULL
, ATA_HORKAGE_ZERO_AFTER_TRIM
, },
3981 { "INTEL*SSD*", NULL
, ATA_HORKAGE_ZERO_AFTER_TRIM
, },
3982 { "SSD*INTEL*", NULL
, ATA_HORKAGE_ZERO_AFTER_TRIM
, },
3983 { "Samsung*SSD*", NULL
, ATA_HORKAGE_ZERO_AFTER_TRIM
, },
3984 { "SAMSUNG*SSD*", NULL
, ATA_HORKAGE_ZERO_AFTER_TRIM
, },
3985 { "SAMSUNG*MZ7KM*", NULL
, ATA_HORKAGE_ZERO_AFTER_TRIM
, },
3986 { "ST[1248][0248]0[FH]*", NULL
, ATA_HORKAGE_ZERO_AFTER_TRIM
, },
3989 * Some WD SATA-I drives spin up and down erratically when the link
3990 * is put into the slumber mode. We don't have full list of the
3991 * affected devices. Disable LPM if the device matches one of the
3992 * known prefixes and is SATA-1. As a side effect LPM partial is
3995 * https://bugzilla.kernel.org/show_bug.cgi?id=57211
3997 { "WDC WD800JD-*", NULL
, ATA_HORKAGE_WD_BROKEN_LPM
},
3998 { "WDC WD1200JD-*", NULL
, ATA_HORKAGE_WD_BROKEN_LPM
},
3999 { "WDC WD1600JD-*", NULL
, ATA_HORKAGE_WD_BROKEN_LPM
},
4000 { "WDC WD2000JD-*", NULL
, ATA_HORKAGE_WD_BROKEN_LPM
},
4001 { "WDC WD2500JD-*", NULL
, ATA_HORKAGE_WD_BROKEN_LPM
},
4002 { "WDC WD3000JD-*", NULL
, ATA_HORKAGE_WD_BROKEN_LPM
},
4003 { "WDC WD3200JD-*", NULL
, ATA_HORKAGE_WD_BROKEN_LPM
},
4009 static unsigned long ata_dev_blacklisted(const struct ata_device
*dev
)
4011 unsigned char model_num
[ATA_ID_PROD_LEN
+ 1];
4012 unsigned char model_rev
[ATA_ID_FW_REV_LEN
+ 1];
4013 const struct ata_blacklist_entry
*ad
= ata_device_blacklist
;
4015 ata_id_c_string(dev
->id
, model_num
, ATA_ID_PROD
, sizeof(model_num
));
4016 ata_id_c_string(dev
->id
, model_rev
, ATA_ID_FW_REV
, sizeof(model_rev
));
4018 while (ad
->model_num
) {
4019 if (glob_match(ad
->model_num
, model_num
)) {
4020 if (ad
->model_rev
== NULL
)
4022 if (glob_match(ad
->model_rev
, model_rev
))
4030 static int ata_dma_blacklisted(const struct ata_device
*dev
)
4032 /* We don't support polling DMA.
4033 * DMA blacklist those ATAPI devices with CDB-intr (and use PIO)
4034 * if the LLDD handles only interrupts in the HSM_ST_LAST state.
4036 if ((dev
->link
->ap
->flags
& ATA_FLAG_PIO_POLLING
) &&
4037 (dev
->flags
& ATA_DFLAG_CDB_INTR
))
4039 return (dev
->horkage
& ATA_HORKAGE_NODMA
) ? 1 : 0;
4043 * ata_is_40wire - check drive side detection
4046 * Perform drive side detection decoding, allowing for device vendors
4047 * who can't follow the documentation.
4050 static int ata_is_40wire(struct ata_device
*dev
)
4052 if (dev
->horkage
& ATA_HORKAGE_IVB
)
4053 return ata_drive_40wire_relaxed(dev
->id
);
4054 return ata_drive_40wire(dev
->id
);
4058 * cable_is_40wire - 40/80/SATA decider
4059 * @ap: port to consider
4061 * This function encapsulates the policy for speed management
4062 * in one place. At the moment we don't cache the result but
4063 * there is a good case for setting ap->cbl to the result when
4064 * we are called with unknown cables (and figuring out if it
4065 * impacts hotplug at all).
4067 * Return 1 if the cable appears to be 40 wire.
4070 static int cable_is_40wire(struct ata_port
*ap
)
4072 struct ata_link
*link
;
4073 struct ata_device
*dev
;
4075 /* If the controller thinks we are 40 wire, we are. */
4076 if (ap
->cbl
== ATA_CBL_PATA40
)
4079 /* If the controller thinks we are 80 wire, we are. */
4080 if (ap
->cbl
== ATA_CBL_PATA80
|| ap
->cbl
== ATA_CBL_SATA
)
4083 /* If the system is known to be 40 wire short cable (eg
4084 * laptop), then we allow 80 wire modes even if the drive
4087 if (ap
->cbl
== ATA_CBL_PATA40_SHORT
)
4090 /* If the controller doesn't know, we scan.
4092 * Note: We look for all 40 wire detects at this point. Any
4093 * 80 wire detect is taken to be 80 wire cable because
4094 * - in many setups only the one drive (slave if present) will
4095 * give a valid detect
4096 * - if you have a non detect capable drive you don't want it
4097 * to colour the choice
4099 ata_for_each_link(link
, ap
, EDGE
) {
4100 ata_for_each_dev(dev
, link
, ENABLED
) {
4101 if (!ata_is_40wire(dev
))
4109 * ata_dev_xfermask - Compute supported xfermask of the given device
4110 * @dev: Device to compute xfermask for
4112 * Compute supported xfermask of @dev and store it in
4113 * dev->*_mask. This function is responsible for applying all
4114 * known limits including host controller limits, device
4120 static void ata_dev_xfermask(struct ata_device
*dev
)
4122 struct ata_link
*link
= dev
->link
;
4123 struct ata_port
*ap
= link
->ap
;
4124 struct ata_host
*host
= ap
->host
;
4125 unsigned long xfer_mask
;
4127 /* controller modes available */
4128 xfer_mask
= ata_pack_xfermask(ap
->pio_mask
,
4129 ap
->mwdma_mask
, ap
->udma_mask
);
4131 /* drive modes available */
4132 xfer_mask
&= ata_pack_xfermask(dev
->pio_mask
,
4133 dev
->mwdma_mask
, dev
->udma_mask
);
4134 xfer_mask
&= ata_id_xfermask(dev
->id
);
4137 * CFA Advanced TrueIDE timings are not allowed on a shared
4140 if (ata_dev_pair(dev
)) {
4141 /* No PIO5 or PIO6 */
4142 xfer_mask
&= ~(0x03 << (ATA_SHIFT_PIO
+ 5));
4143 /* No MWDMA3 or MWDMA 4 */
4144 xfer_mask
&= ~(0x03 << (ATA_SHIFT_MWDMA
+ 3));
4147 if (ata_dma_blacklisted(dev
)) {
4148 xfer_mask
&= ~(ATA_MASK_MWDMA
| ATA_MASK_UDMA
);
4150 "device is on DMA blacklist, disabling DMA\n");
4153 if ((host
->flags
& ATA_HOST_SIMPLEX
) &&
4154 host
->simplex_claimed
&& host
->simplex_claimed
!= ap
) {
4155 xfer_mask
&= ~(ATA_MASK_MWDMA
| ATA_MASK_UDMA
);
4157 "simplex DMA is claimed by other device, disabling DMA\n");
4160 if (ap
->flags
& ATA_FLAG_NO_IORDY
)
4161 xfer_mask
&= ata_pio_mask_no_iordy(dev
);
4163 if (ap
->ops
->mode_filter
)
4164 xfer_mask
= ap
->ops
->mode_filter(dev
, xfer_mask
);
4166 /* Apply cable rule here. Don't apply it early because when
4167 * we handle hot plug the cable type can itself change.
4168 * Check this last so that we know if the transfer rate was
4169 * solely limited by the cable.
4170 * Unknown or 80 wire cables reported host side are checked
4171 * drive side as well. Cases where we know a 40wire cable
4172 * is used safely for 80 are not checked here.
4174 if (xfer_mask
& (0xF8 << ATA_SHIFT_UDMA
))
4175 /* UDMA/44 or higher would be available */
4176 if (cable_is_40wire(ap
)) {
4178 "limited to UDMA/33 due to 40-wire cable\n");
4179 xfer_mask
&= ~(0xF8 << ATA_SHIFT_UDMA
);
4182 ata_unpack_xfermask(xfer_mask
, &dev
->pio_mask
,
4183 &dev
->mwdma_mask
, &dev
->udma_mask
);
4187 * ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
4188 * @dev: Device to which command will be sent
4190 * Issue SET FEATURES - XFER MODE command to device @dev
4194 * PCI/etc. bus probe sem.
4197 * 0 on success, AC_ERR_* mask otherwise.
4200 static unsigned int ata_dev_set_xfermode(struct ata_device
*dev
)
4202 struct ata_taskfile tf
;
4203 unsigned int err_mask
;
4205 /* set up set-features taskfile */
4206 DPRINTK("set features - xfer mode\n");
4208 /* Some controllers and ATAPI devices show flaky interrupt
4209 * behavior after setting xfer mode. Use polling instead.
4211 ata_tf_init(dev
, &tf
);
4212 tf
.command
= ATA_CMD_SET_FEATURES
;
4213 tf
.feature
= SETFEATURES_XFER
;
4214 tf
.flags
|= ATA_TFLAG_ISADDR
| ATA_TFLAG_DEVICE
| ATA_TFLAG_POLLING
;
4215 tf
.protocol
= ATA_PROT_NODATA
;
4216 /* If we are using IORDY we must send the mode setting command */
4217 if (ata_pio_need_iordy(dev
))
4218 tf
.nsect
= dev
->xfer_mode
;
4219 /* If the device has IORDY and the controller does not - turn it off */
4220 else if (ata_id_has_iordy(dev
->id
))
4222 else /* In the ancient relic department - skip all of this */
4225 /* On some disks, this command causes spin-up, so we need longer timeout */
4226 err_mask
= ata_exec_internal(dev
, &tf
, NULL
, DMA_NONE
, NULL
, 0, 15000);
4228 DPRINTK("EXIT, err_mask=%x\n", err_mask
);
4233 * ata_dev_set_feature - Issue SET FEATURES - SATA FEATURES
4234 * @dev: Device to which command will be sent
4235 * @enable: Whether to enable or disable the feature
4236 * @feature: The sector count represents the feature to set
4238 * Issue SET FEATURES - SATA FEATURES command to device @dev
4239 * on port @ap with sector count
4242 * PCI/etc. bus probe sem.
4245 * 0 on success, AC_ERR_* mask otherwise.
4247 unsigned int ata_dev_set_feature(struct ata_device
*dev
, u8 enable
, u8 feature
)
4249 struct ata_taskfile tf
;
4250 unsigned int err_mask
;
4251 unsigned long timeout
= 0;
4253 /* set up set-features taskfile */
4254 DPRINTK("set features - SATA features\n");
4256 ata_tf_init(dev
, &tf
);
4257 tf
.command
= ATA_CMD_SET_FEATURES
;
4258 tf
.feature
= enable
;
4259 tf
.flags
|= ATA_TFLAG_ISADDR
| ATA_TFLAG_DEVICE
;
4260 tf
.protocol
= ATA_PROT_NODATA
;
4263 if (enable
== SETFEATURES_SPINUP
)
4264 timeout
= ata_probe_timeout
?
4265 ata_probe_timeout
* 1000 : SETFEATURES_SPINUP_TIMEOUT
;
4266 err_mask
= ata_exec_internal(dev
, &tf
, NULL
, DMA_NONE
, NULL
, 0, timeout
);
4268 DPRINTK("EXIT, err_mask=%x\n", err_mask
);
4271 EXPORT_SYMBOL_GPL(ata_dev_set_feature
);
4274 * ata_dev_init_params - Issue INIT DEV PARAMS command
4275 * @dev: Device to which command will be sent
4276 * @heads: Number of heads (taskfile parameter)
4277 * @sectors: Number of sectors (taskfile parameter)
4280 * Kernel thread context (may sleep)
4283 * 0 on success, AC_ERR_* mask otherwise.
4285 static unsigned int ata_dev_init_params(struct ata_device
*dev
,
4286 u16 heads
, u16 sectors
)
4288 struct ata_taskfile tf
;
4289 unsigned int err_mask
;
4291 /* Number of sectors per track 1-255. Number of heads 1-16 */
4292 if (sectors
< 1 || sectors
> 255 || heads
< 1 || heads
> 16)
4293 return AC_ERR_INVALID
;
4295 /* set up init dev params taskfile */
4296 DPRINTK("init dev params \n");
4298 ata_tf_init(dev
, &tf
);
4299 tf
.command
= ATA_CMD_INIT_DEV_PARAMS
;
4300 tf
.flags
|= ATA_TFLAG_ISADDR
| ATA_TFLAG_DEVICE
;
4301 tf
.protocol
= ATA_PROT_NODATA
;
4303 tf
.device
|= (heads
- 1) & 0x0f; /* max head = num. of heads - 1 */
4305 err_mask
= ata_exec_internal(dev
, &tf
, NULL
, DMA_NONE
, NULL
, 0, 0);
4306 /* A clean abort indicates an original or just out of spec drive
4307 and we should continue as we issue the setup based on the
4308 drive reported working geometry */
4309 if (err_mask
== AC_ERR_DEV
&& (tf
.feature
& ATA_ABORTED
))
4312 DPRINTK("EXIT, err_mask=%x\n", err_mask
);
4317 * atapi_check_dma - Check whether ATAPI DMA can be supported
4318 * @qc: Metadata associated with taskfile to check
4320 * Allow low-level driver to filter ATA PACKET commands, returning
4321 * a status indicating whether or not it is OK to use DMA for the
4322 * supplied PACKET command.
4325 * spin_lock_irqsave(host lock)
4327 * RETURNS: 0 when ATAPI DMA can be used
4330 int atapi_check_dma(struct ata_queued_cmd
*qc
)
4332 struct ata_port
*ap
= qc
->ap
;
4334 /* Don't allow DMA if it isn't multiple of 16 bytes. Quite a
4335 * few ATAPI devices choke on such DMA requests.
4337 if (!(qc
->dev
->horkage
& ATA_HORKAGE_ATAPI_MOD16_DMA
) &&
4338 unlikely(qc
->nbytes
& 15))
4341 if (ap
->ops
->check_atapi_dma
)
4342 return ap
->ops
->check_atapi_dma(qc
);
4348 * ata_std_qc_defer - Check whether a qc needs to be deferred
4349 * @qc: ATA command in question
4351 * Non-NCQ commands cannot run with any other command, NCQ or
4352 * not. As upper layer only knows the queue depth, we are
4353 * responsible for maintaining exclusion. This function checks
4354 * whether a new command @qc can be issued.
4357 * spin_lock_irqsave(host lock)
4360 * ATA_DEFER_* if deferring is needed, 0 otherwise.
4362 int ata_std_qc_defer(struct ata_queued_cmd
*qc
)
4364 struct ata_link
*link
= qc
->dev
->link
;
4366 if (ata_is_ncq(qc
->tf
.protocol
)) {
4367 if (!ata_tag_valid(link
->active_tag
))
4370 if (!ata_tag_valid(link
->active_tag
) && !link
->sactive
)
4374 return ATA_DEFER_LINK
;
4376 EXPORT_SYMBOL_GPL(ata_std_qc_defer
);
4378 enum ata_completion_errors
ata_noop_qc_prep(struct ata_queued_cmd
*qc
)
4382 EXPORT_SYMBOL_GPL(ata_noop_qc_prep
);
4385 * ata_sg_init - Associate command with scatter-gather table.
4386 * @qc: Command to be associated
4387 * @sg: Scatter-gather table.
4388 * @n_elem: Number of elements in s/g table.
4390 * Initialize the data-related elements of queued_cmd @qc
4391 * to point to a scatter-gather table @sg, containing @n_elem
4395 * spin_lock_irqsave(host lock)
4397 void ata_sg_init(struct ata_queued_cmd
*qc
, struct scatterlist
*sg
,
4398 unsigned int n_elem
)
4401 qc
->n_elem
= n_elem
;
4405 #ifdef CONFIG_HAS_DMA
4408 * ata_sg_clean - Unmap DMA memory associated with command
4409 * @qc: Command containing DMA memory to be released
4411 * Unmap all mapped DMA memory associated with this command.
4414 * spin_lock_irqsave(host lock)
4416 static void ata_sg_clean(struct ata_queued_cmd
*qc
)
4418 struct ata_port
*ap
= qc
->ap
;
4419 struct scatterlist
*sg
= qc
->sg
;
4420 int dir
= qc
->dma_dir
;
4422 WARN_ON_ONCE(sg
== NULL
);
4424 VPRINTK("unmapping %u sg elements\n", qc
->n_elem
);
4427 dma_unmap_sg(ap
->dev
, sg
, qc
->orig_n_elem
, dir
);
4429 qc
->flags
&= ~ATA_QCFLAG_DMAMAP
;
4434 * ata_sg_setup - DMA-map the scatter-gather table associated with a command.
4435 * @qc: Command with scatter-gather table to be mapped.
4437 * DMA-map the scatter-gather table associated with queued_cmd @qc.
4440 * spin_lock_irqsave(host lock)
4443 * Zero on success, negative on error.
4446 static int ata_sg_setup(struct ata_queued_cmd
*qc
)
4448 struct ata_port
*ap
= qc
->ap
;
4449 unsigned int n_elem
;
4451 VPRINTK("ENTER, ata%u\n", ap
->print_id
);
4453 n_elem
= dma_map_sg(ap
->dev
, qc
->sg
, qc
->n_elem
, qc
->dma_dir
);
4457 VPRINTK("%d sg elements mapped\n", n_elem
);
4458 qc
->orig_n_elem
= qc
->n_elem
;
4459 qc
->n_elem
= n_elem
;
4460 qc
->flags
|= ATA_QCFLAG_DMAMAP
;
4465 #else /* !CONFIG_HAS_DMA */
4467 static inline void ata_sg_clean(struct ata_queued_cmd
*qc
) {}
4468 static inline int ata_sg_setup(struct ata_queued_cmd
*qc
) { return -1; }
4470 #endif /* !CONFIG_HAS_DMA */
4473 * swap_buf_le16 - swap halves of 16-bit words in place
4474 * @buf: Buffer to swap
4475 * @buf_words: Number of 16-bit words in buffer.
4477 * Swap halves of 16-bit words if needed to convert from
4478 * little-endian byte order to native cpu byte order, or
4482 * Inherited from caller.
4484 void swap_buf_le16(u16
*buf
, unsigned int buf_words
)
4489 for (i
= 0; i
< buf_words
; i
++)
4490 buf
[i
] = le16_to_cpu(buf
[i
]);
4491 #endif /* __BIG_ENDIAN */
4495 * ata_qc_new_init - Request an available ATA command, and initialize it
4496 * @dev: Device from whom we request an available command structure
4503 struct ata_queued_cmd
*ata_qc_new_init(struct ata_device
*dev
, int tag
)
4505 struct ata_port
*ap
= dev
->link
->ap
;
4506 struct ata_queued_cmd
*qc
;
4508 /* no command while frozen */
4509 if (unlikely(ap
->pflags
& ATA_PFLAG_FROZEN
))
4513 if (ap
->flags
& ATA_FLAG_SAS_HOST
) {
4514 tag
= ata_sas_allocate_tag(ap
);
4519 qc
= __ata_qc_from_tag(ap
, tag
);
4520 qc
->tag
= qc
->hw_tag
= tag
;
4531 * ata_qc_free - free unused ata_queued_cmd
4532 * @qc: Command to complete
4534 * Designed to free unused ata_queued_cmd object
4535 * in case something prevents using it.
4538 * spin_lock_irqsave(host lock)
4540 void ata_qc_free(struct ata_queued_cmd
*qc
)
4542 struct ata_port
*ap
;
4545 WARN_ON_ONCE(qc
== NULL
); /* ata_qc_from_tag _might_ return NULL */
4550 if (ata_tag_valid(tag
)) {
4551 qc
->tag
= ATA_TAG_POISON
;
4552 if (ap
->flags
& ATA_FLAG_SAS_HOST
)
4553 ata_sas_free_tag(tag
, ap
);
4557 void __ata_qc_complete(struct ata_queued_cmd
*qc
)
4559 struct ata_port
*ap
;
4560 struct ata_link
*link
;
4562 WARN_ON_ONCE(qc
== NULL
); /* ata_qc_from_tag _might_ return NULL */
4563 WARN_ON_ONCE(!(qc
->flags
& ATA_QCFLAG_ACTIVE
));
4565 link
= qc
->dev
->link
;
4567 if (likely(qc
->flags
& ATA_QCFLAG_DMAMAP
))
4570 /* command should be marked inactive atomically with qc completion */
4571 if (ata_is_ncq(qc
->tf
.protocol
)) {
4572 link
->sactive
&= ~(1 << qc
->hw_tag
);
4574 ap
->nr_active_links
--;
4576 link
->active_tag
= ATA_TAG_POISON
;
4577 ap
->nr_active_links
--;
4580 /* clear exclusive status */
4581 if (unlikely(qc
->flags
& ATA_QCFLAG_CLEAR_EXCL
&&
4582 ap
->excl_link
== link
))
4583 ap
->excl_link
= NULL
;
4585 /* atapi: mark qc as inactive to prevent the interrupt handler
4586 * from completing the command twice later, before the error handler
4587 * is called. (when rc != 0 and atapi request sense is needed)
4589 qc
->flags
&= ~ATA_QCFLAG_ACTIVE
;
4590 ap
->qc_active
&= ~(1ULL << qc
->tag
);
4592 /* call completion callback */
4593 qc
->complete_fn(qc
);
4596 static void fill_result_tf(struct ata_queued_cmd
*qc
)
4598 struct ata_port
*ap
= qc
->ap
;
4600 qc
->result_tf
.flags
= qc
->tf
.flags
;
4601 ap
->ops
->qc_fill_rtf(qc
);
4604 static void ata_verify_xfer(struct ata_queued_cmd
*qc
)
4606 struct ata_device
*dev
= qc
->dev
;
4608 if (!ata_is_data(qc
->tf
.protocol
))
4611 if ((dev
->mwdma_mask
|| dev
->udma_mask
) && ata_is_pio(qc
->tf
.protocol
))
4614 dev
->flags
&= ~ATA_DFLAG_DUBIOUS_XFER
;
4618 * ata_qc_complete - Complete an active ATA command
4619 * @qc: Command to complete
4621 * Indicate to the mid and upper layers that an ATA command has
4622 * completed, with either an ok or not-ok status.
4624 * Refrain from calling this function multiple times when
4625 * successfully completing multiple NCQ commands.
4626 * ata_qc_complete_multiple() should be used instead, which will
4627 * properly update IRQ expect state.
4630 * spin_lock_irqsave(host lock)
4632 void ata_qc_complete(struct ata_queued_cmd
*qc
)
4634 struct ata_port
*ap
= qc
->ap
;
4636 /* Trigger the LED (if available) */
4637 ledtrig_disk_activity(!!(qc
->tf
.flags
& ATA_TFLAG_WRITE
));
4639 /* XXX: New EH and old EH use different mechanisms to
4640 * synchronize EH with regular execution path.
4642 * In new EH, a failed qc is marked with ATA_QCFLAG_FAILED.
4643 * Normal execution path is responsible for not accessing a
4644 * failed qc. libata core enforces the rule by returning NULL
4645 * from ata_qc_from_tag() for failed qcs.
4647 * Old EH depends on ata_qc_complete() nullifying completion
4648 * requests if ATA_QCFLAG_EH_SCHEDULED is set. Old EH does
4649 * not synchronize with interrupt handler. Only PIO task is
4652 if (ap
->ops
->error_handler
) {
4653 struct ata_device
*dev
= qc
->dev
;
4654 struct ata_eh_info
*ehi
= &dev
->link
->eh_info
;
4656 if (unlikely(qc
->err_mask
))
4657 qc
->flags
|= ATA_QCFLAG_FAILED
;
4660 * Finish internal commands without any further processing
4661 * and always with the result TF filled.
4663 if (unlikely(ata_tag_internal(qc
->tag
))) {
4665 trace_ata_qc_complete_internal(qc
);
4666 __ata_qc_complete(qc
);
4671 * Non-internal qc has failed. Fill the result TF and
4674 if (unlikely(qc
->flags
& ATA_QCFLAG_FAILED
)) {
4676 trace_ata_qc_complete_failed(qc
);
4677 ata_qc_schedule_eh(qc
);
4681 WARN_ON_ONCE(ap
->pflags
& ATA_PFLAG_FROZEN
);
4683 /* read result TF if requested */
4684 if (qc
->flags
& ATA_QCFLAG_RESULT_TF
)
4687 trace_ata_qc_complete_done(qc
);
4688 /* Some commands need post-processing after successful
4691 switch (qc
->tf
.command
) {
4692 case ATA_CMD_SET_FEATURES
:
4693 if (qc
->tf
.feature
!= SETFEATURES_WC_ON
&&
4694 qc
->tf
.feature
!= SETFEATURES_WC_OFF
&&
4695 qc
->tf
.feature
!= SETFEATURES_RA_ON
&&
4696 qc
->tf
.feature
!= SETFEATURES_RA_OFF
)
4699 case ATA_CMD_INIT_DEV_PARAMS
: /* CHS translation changed */
4700 case ATA_CMD_SET_MULTI
: /* multi_count changed */
4701 /* revalidate device */
4702 ehi
->dev_action
[dev
->devno
] |= ATA_EH_REVALIDATE
;
4703 ata_port_schedule_eh(ap
);
4707 dev
->flags
|= ATA_DFLAG_SLEEPING
;
4711 if (unlikely(dev
->flags
& ATA_DFLAG_DUBIOUS_XFER
))
4712 ata_verify_xfer(qc
);
4714 __ata_qc_complete(qc
);
4716 if (qc
->flags
& ATA_QCFLAG_EH_SCHEDULED
)
4719 /* read result TF if failed or requested */
4720 if (qc
->err_mask
|| qc
->flags
& ATA_QCFLAG_RESULT_TF
)
4723 __ata_qc_complete(qc
);
4726 EXPORT_SYMBOL_GPL(ata_qc_complete
);
4729 * ata_qc_get_active - get bitmask of active qcs
4730 * @ap: port in question
4733 * spin_lock_irqsave(host lock)
4736 * Bitmask of active qcs
4738 u64
ata_qc_get_active(struct ata_port
*ap
)
4740 u64 qc_active
= ap
->qc_active
;
4742 /* ATA_TAG_INTERNAL is sent to hw as tag 0 */
4743 if (qc_active
& (1ULL << ATA_TAG_INTERNAL
)) {
4744 qc_active
|= (1 << 0);
4745 qc_active
&= ~(1ULL << ATA_TAG_INTERNAL
);
4750 EXPORT_SYMBOL_GPL(ata_qc_get_active
);
4753 * ata_qc_issue - issue taskfile to device
4754 * @qc: command to issue to device
4756 * Prepare an ATA command to submission to device.
4757 * This includes mapping the data into a DMA-able
4758 * area, filling in the S/G table, and finally
4759 * writing the taskfile to hardware, starting the command.
4762 * spin_lock_irqsave(host lock)
4764 void ata_qc_issue(struct ata_queued_cmd
*qc
)
4766 struct ata_port
*ap
= qc
->ap
;
4767 struct ata_link
*link
= qc
->dev
->link
;
4768 u8 prot
= qc
->tf
.protocol
;
4770 /* Make sure only one non-NCQ command is outstanding. The
4771 * check is skipped for old EH because it reuses active qc to
4772 * request ATAPI sense.
4774 WARN_ON_ONCE(ap
->ops
->error_handler
&& ata_tag_valid(link
->active_tag
));
4776 if (ata_is_ncq(prot
)) {
4777 WARN_ON_ONCE(link
->sactive
& (1 << qc
->hw_tag
));
4780 ap
->nr_active_links
++;
4781 link
->sactive
|= 1 << qc
->hw_tag
;
4783 WARN_ON_ONCE(link
->sactive
);
4785 ap
->nr_active_links
++;
4786 link
->active_tag
= qc
->tag
;
4789 qc
->flags
|= ATA_QCFLAG_ACTIVE
;
4790 ap
->qc_active
|= 1ULL << qc
->tag
;
4793 * We guarantee to LLDs that they will have at least one
4794 * non-zero sg if the command is a data command.
4796 if (ata_is_data(prot
) && (!qc
->sg
|| !qc
->n_elem
|| !qc
->nbytes
))
4799 if (ata_is_dma(prot
) || (ata_is_pio(prot
) &&
4800 (ap
->flags
& ATA_FLAG_PIO_DMA
)))
4801 if (ata_sg_setup(qc
))
4804 /* if device is sleeping, schedule reset and abort the link */
4805 if (unlikely(qc
->dev
->flags
& ATA_DFLAG_SLEEPING
)) {
4806 link
->eh_info
.action
|= ATA_EH_RESET
;
4807 ata_ehi_push_desc(&link
->eh_info
, "waking up from sleep");
4808 ata_link_abort(link
);
4812 qc
->err_mask
|= ap
->ops
->qc_prep(qc
);
4813 if (unlikely(qc
->err_mask
))
4815 trace_ata_qc_issue(qc
);
4816 qc
->err_mask
|= ap
->ops
->qc_issue(qc
);
4817 if (unlikely(qc
->err_mask
))
4822 qc
->err_mask
|= AC_ERR_SYSTEM
;
4824 ata_qc_complete(qc
);
4828 * ata_phys_link_online - test whether the given link is online
4829 * @link: ATA link to test
4831 * Test whether @link is online. Note that this function returns
4832 * 0 if online status of @link cannot be obtained, so
4833 * ata_link_online(link) != !ata_link_offline(link).
4839 * True if the port online status is available and online.
4841 bool ata_phys_link_online(struct ata_link
*link
)
4845 if (sata_scr_read(link
, SCR_STATUS
, &sstatus
) == 0 &&
4846 ata_sstatus_online(sstatus
))
4852 * ata_phys_link_offline - test whether the given link is offline
4853 * @link: ATA link to test
4855 * Test whether @link is offline. Note that this function
4856 * returns 0 if offline status of @link cannot be obtained, so
4857 * ata_link_online(link) != !ata_link_offline(link).
4863 * True if the port offline status is available and offline.
4865 bool ata_phys_link_offline(struct ata_link
*link
)
4869 if (sata_scr_read(link
, SCR_STATUS
, &sstatus
) == 0 &&
4870 !ata_sstatus_online(sstatus
))
4876 * ata_link_online - test whether the given link is online
4877 * @link: ATA link to test
4879 * Test whether @link is online. This is identical to
4880 * ata_phys_link_online() when there's no slave link. When
4881 * there's a slave link, this function should only be called on
4882 * the master link and will return true if any of M/S links is
4889 * True if the port online status is available and online.
4891 bool ata_link_online(struct ata_link
*link
)
4893 struct ata_link
*slave
= link
->ap
->slave_link
;
4895 WARN_ON(link
== slave
); /* shouldn't be called on slave link */
4897 return ata_phys_link_online(link
) ||
4898 (slave
&& ata_phys_link_online(slave
));
4900 EXPORT_SYMBOL_GPL(ata_link_online
);
4903 * ata_link_offline - test whether the given link is offline
4904 * @link: ATA link to test
4906 * Test whether @link is offline. This is identical to
4907 * ata_phys_link_offline() when there's no slave link. When
4908 * there's a slave link, this function should only be called on
4909 * the master link and will return true if both M/S links are
4916 * True if the port offline status is available and offline.
4918 bool ata_link_offline(struct ata_link
*link
)
4920 struct ata_link
*slave
= link
->ap
->slave_link
;
4922 WARN_ON(link
== slave
); /* shouldn't be called on slave link */
4924 return ata_phys_link_offline(link
) &&
4925 (!slave
|| ata_phys_link_offline(slave
));
4927 EXPORT_SYMBOL_GPL(ata_link_offline
);
4930 static void ata_port_request_pm(struct ata_port
*ap
, pm_message_t mesg
,
4931 unsigned int action
, unsigned int ehi_flags
,
4934 struct ata_link
*link
;
4935 unsigned long flags
;
4937 /* Previous resume operation might still be in
4938 * progress. Wait for PM_PENDING to clear.
4940 if (ap
->pflags
& ATA_PFLAG_PM_PENDING
) {
4941 ata_port_wait_eh(ap
);
4942 WARN_ON(ap
->pflags
& ATA_PFLAG_PM_PENDING
);
4945 /* request PM ops to EH */
4946 spin_lock_irqsave(ap
->lock
, flags
);
4949 ap
->pflags
|= ATA_PFLAG_PM_PENDING
;
4950 ata_for_each_link(link
, ap
, HOST_FIRST
) {
4951 link
->eh_info
.action
|= action
;
4952 link
->eh_info
.flags
|= ehi_flags
;
4955 ata_port_schedule_eh(ap
);
4957 spin_unlock_irqrestore(ap
->lock
, flags
);
4960 ata_port_wait_eh(ap
);
4961 WARN_ON(ap
->pflags
& ATA_PFLAG_PM_PENDING
);
4966 * On some hardware, device fails to respond after spun down for suspend. As
4967 * the device won't be used before being resumed, we don't need to touch the
4968 * device. Ask EH to skip the usual stuff and proceed directly to suspend.
4970 * http://thread.gmane.org/gmane.linux.ide/46764
4972 static const unsigned int ata_port_suspend_ehi
= ATA_EHI_QUIET
4973 | ATA_EHI_NO_AUTOPSY
4974 | ATA_EHI_NO_RECOVERY
;
4976 static void ata_port_suspend(struct ata_port
*ap
, pm_message_t mesg
)
4978 ata_port_request_pm(ap
, mesg
, 0, ata_port_suspend_ehi
, false);
4981 static void ata_port_suspend_async(struct ata_port
*ap
, pm_message_t mesg
)
4983 ata_port_request_pm(ap
, mesg
, 0, ata_port_suspend_ehi
, true);
4986 static int ata_port_pm_suspend(struct device
*dev
)
4988 struct ata_port
*ap
= to_ata_port(dev
);
4990 if (pm_runtime_suspended(dev
))
4993 ata_port_suspend(ap
, PMSG_SUSPEND
);
4997 static int ata_port_pm_freeze(struct device
*dev
)
4999 struct ata_port
*ap
= to_ata_port(dev
);
5001 if (pm_runtime_suspended(dev
))
5004 ata_port_suspend(ap
, PMSG_FREEZE
);
5008 static int ata_port_pm_poweroff(struct device
*dev
)
5010 ata_port_suspend(to_ata_port(dev
), PMSG_HIBERNATE
);
5014 static const unsigned int ata_port_resume_ehi
= ATA_EHI_NO_AUTOPSY
5017 static void ata_port_resume(struct ata_port
*ap
, pm_message_t mesg
)
5019 ata_port_request_pm(ap
, mesg
, ATA_EH_RESET
, ata_port_resume_ehi
, false);
5022 static void ata_port_resume_async(struct ata_port
*ap
, pm_message_t mesg
)
5024 ata_port_request_pm(ap
, mesg
, ATA_EH_RESET
, ata_port_resume_ehi
, true);
5027 static int ata_port_pm_resume(struct device
*dev
)
5029 ata_port_resume_async(to_ata_port(dev
), PMSG_RESUME
);
5030 pm_runtime_disable(dev
);
5031 pm_runtime_set_active(dev
);
5032 pm_runtime_enable(dev
);
5037 * For ODDs, the upper layer will poll for media change every few seconds,
5038 * which will make it enter and leave suspend state every few seconds. And
5039 * as each suspend will cause a hard/soft reset, the gain of runtime suspend
5040 * is very little and the ODD may malfunction after constantly being reset.
5041 * So the idle callback here will not proceed to suspend if a non-ZPODD capable
5042 * ODD is attached to the port.
5044 static int ata_port_runtime_idle(struct device
*dev
)
5046 struct ata_port
*ap
= to_ata_port(dev
);
5047 struct ata_link
*link
;
5048 struct ata_device
*adev
;
5050 ata_for_each_link(link
, ap
, HOST_FIRST
) {
5051 ata_for_each_dev(adev
, link
, ENABLED
)
5052 if (adev
->class == ATA_DEV_ATAPI
&&
5053 !zpodd_dev_enabled(adev
))
5060 static int ata_port_runtime_suspend(struct device
*dev
)
5062 ata_port_suspend(to_ata_port(dev
), PMSG_AUTO_SUSPEND
);
5066 static int ata_port_runtime_resume(struct device
*dev
)
5068 ata_port_resume(to_ata_port(dev
), PMSG_AUTO_RESUME
);
5072 static const struct dev_pm_ops ata_port_pm_ops
= {
5073 .suspend
= ata_port_pm_suspend
,
5074 .resume
= ata_port_pm_resume
,
5075 .freeze
= ata_port_pm_freeze
,
5076 .thaw
= ata_port_pm_resume
,
5077 .poweroff
= ata_port_pm_poweroff
,
5078 .restore
= ata_port_pm_resume
,
5080 .runtime_suspend
= ata_port_runtime_suspend
,
5081 .runtime_resume
= ata_port_runtime_resume
,
5082 .runtime_idle
= ata_port_runtime_idle
,
5085 /* sas ports don't participate in pm runtime management of ata_ports,
5086 * and need to resume ata devices at the domain level, not the per-port
5087 * level. sas suspend/resume is async to allow parallel port recovery
5088 * since sas has multiple ata_port instances per Scsi_Host.
5090 void ata_sas_port_suspend(struct ata_port
*ap
)
5092 ata_port_suspend_async(ap
, PMSG_SUSPEND
);
5094 EXPORT_SYMBOL_GPL(ata_sas_port_suspend
);
5096 void ata_sas_port_resume(struct ata_port
*ap
)
5098 ata_port_resume_async(ap
, PMSG_RESUME
);
5100 EXPORT_SYMBOL_GPL(ata_sas_port_resume
);
5103 * ata_host_suspend - suspend host
5104 * @host: host to suspend
5107 * Suspend @host. Actual operation is performed by port suspend.
5109 int ata_host_suspend(struct ata_host
*host
, pm_message_t mesg
)
5111 host
->dev
->power
.power_state
= mesg
;
5114 EXPORT_SYMBOL_GPL(ata_host_suspend
);
5117 * ata_host_resume - resume host
5118 * @host: host to resume
5120 * Resume @host. Actual operation is performed by port resume.
5122 void ata_host_resume(struct ata_host
*host
)
5124 host
->dev
->power
.power_state
= PMSG_ON
;
5126 EXPORT_SYMBOL_GPL(ata_host_resume
);
5129 const struct device_type ata_port_type
= {
5132 .pm
= &ata_port_pm_ops
,
5137 * ata_dev_init - Initialize an ata_device structure
5138 * @dev: Device structure to initialize
5140 * Initialize @dev in preparation for probing.
5143 * Inherited from caller.
5145 void ata_dev_init(struct ata_device
*dev
)
5147 struct ata_link
*link
= ata_dev_phys_link(dev
);
5148 struct ata_port
*ap
= link
->ap
;
5149 unsigned long flags
;
5151 /* SATA spd limit is bound to the attached device, reset together */
5152 link
->sata_spd_limit
= link
->hw_sata_spd_limit
;
5155 /* High bits of dev->flags are used to record warm plug
5156 * requests which occur asynchronously. Synchronize using
5159 spin_lock_irqsave(ap
->lock
, flags
);
5160 dev
->flags
&= ~ATA_DFLAG_INIT_MASK
;
5162 spin_unlock_irqrestore(ap
->lock
, flags
);
5164 memset((void *)dev
+ ATA_DEVICE_CLEAR_BEGIN
, 0,
5165 ATA_DEVICE_CLEAR_END
- ATA_DEVICE_CLEAR_BEGIN
);
5166 dev
->pio_mask
= UINT_MAX
;
5167 dev
->mwdma_mask
= UINT_MAX
;
5168 dev
->udma_mask
= UINT_MAX
;
5172 * ata_link_init - Initialize an ata_link structure
5173 * @ap: ATA port link is attached to
5174 * @link: Link structure to initialize
5175 * @pmp: Port multiplier port number
5180 * Kernel thread context (may sleep)
5182 void ata_link_init(struct ata_port
*ap
, struct ata_link
*link
, int pmp
)
5186 /* clear everything except for devices */
5187 memset((void *)link
+ ATA_LINK_CLEAR_BEGIN
, 0,
5188 ATA_LINK_CLEAR_END
- ATA_LINK_CLEAR_BEGIN
);
5192 link
->active_tag
= ATA_TAG_POISON
;
5193 link
->hw_sata_spd_limit
= UINT_MAX
;
5195 /* can't use iterator, ap isn't initialized yet */
5196 for (i
= 0; i
< ATA_MAX_DEVICES
; i
++) {
5197 struct ata_device
*dev
= &link
->device
[i
];
5200 dev
->devno
= dev
- link
->device
;
5201 #ifdef CONFIG_ATA_ACPI
5202 dev
->gtf_filter
= ata_acpi_gtf_filter
;
5209 * sata_link_init_spd - Initialize link->sata_spd_limit
5210 * @link: Link to configure sata_spd_limit for
5212 * Initialize @link->[hw_]sata_spd_limit to the currently
5216 * Kernel thread context (may sleep).
5219 * 0 on success, -errno on failure.
5221 int sata_link_init_spd(struct ata_link
*link
)
5226 rc
= sata_scr_read(link
, SCR_CONTROL
, &link
->saved_scontrol
);
5230 spd
= (link
->saved_scontrol
>> 4) & 0xf;
5232 link
->hw_sata_spd_limit
&= (1 << spd
) - 1;
5234 ata_force_link_limits(link
);
5236 link
->sata_spd_limit
= link
->hw_sata_spd_limit
;
5242 * ata_port_alloc - allocate and initialize basic ATA port resources
5243 * @host: ATA host this allocated port belongs to
5245 * Allocate and initialize basic ATA port resources.
5248 * Allocate ATA port on success, NULL on failure.
5251 * Inherited from calling layer (may sleep).
5253 struct ata_port
*ata_port_alloc(struct ata_host
*host
)
5255 struct ata_port
*ap
;
5259 ap
= kzalloc(sizeof(*ap
), GFP_KERNEL
);
5263 ap
->pflags
|= ATA_PFLAG_INITIALIZING
| ATA_PFLAG_FROZEN
;
5264 ap
->lock
= &host
->lock
;
5266 ap
->local_port_no
= -1;
5268 ap
->dev
= host
->dev
;
5270 #if defined(ATA_VERBOSE_DEBUG)
5271 /* turn on all debugging levels */
5272 ap
->msg_enable
= 0x00FF;
5273 #elif defined(ATA_DEBUG)
5274 ap
->msg_enable
= ATA_MSG_DRV
| ATA_MSG_INFO
| ATA_MSG_CTL
| ATA_MSG_WARN
| ATA_MSG_ERR
;
5276 ap
->msg_enable
= ATA_MSG_DRV
| ATA_MSG_ERR
| ATA_MSG_WARN
;
5279 mutex_init(&ap
->scsi_scan_mutex
);
5280 INIT_DELAYED_WORK(&ap
->hotplug_task
, ata_scsi_hotplug
);
5281 INIT_WORK(&ap
->scsi_rescan_task
, ata_scsi_dev_rescan
);
5282 INIT_LIST_HEAD(&ap
->eh_done_q
);
5283 init_waitqueue_head(&ap
->eh_wait_q
);
5284 init_completion(&ap
->park_req_pending
);
5285 timer_setup(&ap
->fastdrain_timer
, ata_eh_fastdrain_timerfn
,
5288 ap
->cbl
= ATA_CBL_NONE
;
5290 ata_link_init(ap
, &ap
->link
, 0);
5293 ap
->stats
.unhandled_irq
= 1;
5294 ap
->stats
.idle_irq
= 1;
5296 ata_sff_port_init(ap
);
5301 static void ata_devres_release(struct device
*gendev
, void *res
)
5303 struct ata_host
*host
= dev_get_drvdata(gendev
);
5306 for (i
= 0; i
< host
->n_ports
; i
++) {
5307 struct ata_port
*ap
= host
->ports
[i
];
5313 scsi_host_put(ap
->scsi_host
);
5317 dev_set_drvdata(gendev
, NULL
);
5321 static void ata_host_release(struct kref
*kref
)
5323 struct ata_host
*host
= container_of(kref
, struct ata_host
, kref
);
5326 for (i
= 0; i
< host
->n_ports
; i
++) {
5327 struct ata_port
*ap
= host
->ports
[i
];
5329 kfree(ap
->pmp_link
);
5330 kfree(ap
->slave_link
);
5332 host
->ports
[i
] = NULL
;
5337 void ata_host_get(struct ata_host
*host
)
5339 kref_get(&host
->kref
);
5342 void ata_host_put(struct ata_host
*host
)
5344 kref_put(&host
->kref
, ata_host_release
);
5346 EXPORT_SYMBOL_GPL(ata_host_put
);
5349 * ata_host_alloc - allocate and init basic ATA host resources
5350 * @dev: generic device this host is associated with
5351 * @max_ports: maximum number of ATA ports associated with this host
5353 * Allocate and initialize basic ATA host resources. LLD calls
5354 * this function to allocate a host, initializes it fully and
5355 * attaches it using ata_host_register().
5357 * @max_ports ports are allocated and host->n_ports is
5358 * initialized to @max_ports. The caller is allowed to decrease
5359 * host->n_ports before calling ata_host_register(). The unused
5360 * ports will be automatically freed on registration.
5363 * Allocate ATA host on success, NULL on failure.
5366 * Inherited from calling layer (may sleep).
5368 struct ata_host
*ata_host_alloc(struct device
*dev
, int max_ports
)
5370 struct ata_host
*host
;
5377 /* alloc a container for our list of ATA ports (buses) */
5378 sz
= sizeof(struct ata_host
) + (max_ports
+ 1) * sizeof(void *);
5379 host
= kzalloc(sz
, GFP_KERNEL
);
5383 if (!devres_open_group(dev
, NULL
, GFP_KERNEL
))
5386 dr
= devres_alloc(ata_devres_release
, 0, GFP_KERNEL
);
5390 devres_add(dev
, dr
);
5391 dev_set_drvdata(dev
, host
);
5393 spin_lock_init(&host
->lock
);
5394 mutex_init(&host
->eh_mutex
);
5396 host
->n_ports
= max_ports
;
5397 kref_init(&host
->kref
);
5399 /* allocate ports bound to this host */
5400 for (i
= 0; i
< max_ports
; i
++) {
5401 struct ata_port
*ap
;
5403 ap
= ata_port_alloc(host
);
5408 host
->ports
[i
] = ap
;
5411 devres_remove_group(dev
, NULL
);
5415 devres_release_group(dev
, NULL
);
5420 EXPORT_SYMBOL_GPL(ata_host_alloc
);
5423 * ata_host_alloc_pinfo - alloc host and init with port_info array
5424 * @dev: generic device this host is associated with
5425 * @ppi: array of ATA port_info to initialize host with
5426 * @n_ports: number of ATA ports attached to this host
5428 * Allocate ATA host and initialize with info from @ppi. If NULL
5429 * terminated, @ppi may contain fewer entries than @n_ports. The
5430 * last entry will be used for the remaining ports.
5433 * Allocate ATA host on success, NULL on failure.
5436 * Inherited from calling layer (may sleep).
5438 struct ata_host
*ata_host_alloc_pinfo(struct device
*dev
,
5439 const struct ata_port_info
* const * ppi
,
5442 const struct ata_port_info
*pi
;
5443 struct ata_host
*host
;
5446 host
= ata_host_alloc(dev
, n_ports
);
5450 for (i
= 0, j
= 0, pi
= NULL
; i
< host
->n_ports
; i
++) {
5451 struct ata_port
*ap
= host
->ports
[i
];
5456 ap
->pio_mask
= pi
->pio_mask
;
5457 ap
->mwdma_mask
= pi
->mwdma_mask
;
5458 ap
->udma_mask
= pi
->udma_mask
;
5459 ap
->flags
|= pi
->flags
;
5460 ap
->link
.flags
|= pi
->link_flags
;
5461 ap
->ops
= pi
->port_ops
;
5463 if (!host
->ops
&& (pi
->port_ops
!= &ata_dummy_port_ops
))
5464 host
->ops
= pi
->port_ops
;
5469 EXPORT_SYMBOL_GPL(ata_host_alloc_pinfo
);
5471 static void ata_host_stop(struct device
*gendev
, void *res
)
5473 struct ata_host
*host
= dev_get_drvdata(gendev
);
5476 WARN_ON(!(host
->flags
& ATA_HOST_STARTED
));
5478 for (i
= 0; i
< host
->n_ports
; i
++) {
5479 struct ata_port
*ap
= host
->ports
[i
];
5481 if (ap
->ops
->port_stop
)
5482 ap
->ops
->port_stop(ap
);
5485 if (host
->ops
->host_stop
)
5486 host
->ops
->host_stop(host
);
5490 * ata_finalize_port_ops - finalize ata_port_operations
5491 * @ops: ata_port_operations to finalize
5493 * An ata_port_operations can inherit from another ops and that
5494 * ops can again inherit from another. This can go on as many
5495 * times as necessary as long as there is no loop in the
5496 * inheritance chain.
5498 * Ops tables are finalized when the host is started. NULL or
5499 * unspecified entries are inherited from the closet ancestor
5500 * which has the method and the entry is populated with it.
5501 * After finalization, the ops table directly points to all the
5502 * methods and ->inherits is no longer necessary and cleared.
5504 * Using ATA_OP_NULL, inheriting ops can force a method to NULL.
5509 static void ata_finalize_port_ops(struct ata_port_operations
*ops
)
5511 static DEFINE_SPINLOCK(lock
);
5512 const struct ata_port_operations
*cur
;
5513 void **begin
= (void **)ops
;
5514 void **end
= (void **)&ops
->inherits
;
5517 if (!ops
|| !ops
->inherits
)
5522 for (cur
= ops
->inherits
; cur
; cur
= cur
->inherits
) {
5523 void **inherit
= (void **)cur
;
5525 for (pp
= begin
; pp
< end
; pp
++, inherit
++)
5530 for (pp
= begin
; pp
< end
; pp
++)
5534 ops
->inherits
= NULL
;
5540 * ata_host_start - start and freeze ports of an ATA host
5541 * @host: ATA host to start ports for
5543 * Start and then freeze ports of @host. Started status is
5544 * recorded in host->flags, so this function can be called
5545 * multiple times. Ports are guaranteed to get started only
5546 * once. If host->ops isn't initialized yet, its set to the
5547 * first non-dummy port ops.
5550 * Inherited from calling layer (may sleep).
5553 * 0 if all ports are started successfully, -errno otherwise.
5555 int ata_host_start(struct ata_host
*host
)
5558 void *start_dr
= NULL
;
5561 if (host
->flags
& ATA_HOST_STARTED
)
5564 ata_finalize_port_ops(host
->ops
);
5566 for (i
= 0; i
< host
->n_ports
; i
++) {
5567 struct ata_port
*ap
= host
->ports
[i
];
5569 ata_finalize_port_ops(ap
->ops
);
5571 if (!host
->ops
&& !ata_port_is_dummy(ap
))
5572 host
->ops
= ap
->ops
;
5574 if (ap
->ops
->port_stop
)
5578 if (host
->ops
->host_stop
)
5582 start_dr
= devres_alloc(ata_host_stop
, 0, GFP_KERNEL
);
5587 for (i
= 0; i
< host
->n_ports
; i
++) {
5588 struct ata_port
*ap
= host
->ports
[i
];
5590 if (ap
->ops
->port_start
) {
5591 rc
= ap
->ops
->port_start(ap
);
5595 "failed to start port %d (errno=%d)\n",
5600 ata_eh_freeze_port(ap
);
5604 devres_add(host
->dev
, start_dr
);
5605 host
->flags
|= ATA_HOST_STARTED
;
5610 struct ata_port
*ap
= host
->ports
[i
];
5612 if (ap
->ops
->port_stop
)
5613 ap
->ops
->port_stop(ap
);
5615 devres_free(start_dr
);
5618 EXPORT_SYMBOL_GPL(ata_host_start
);
5621 * ata_sas_host_init - Initialize a host struct for sas (ipr, libsas)
5622 * @host: host to initialize
5623 * @dev: device host is attached to
5627 void ata_host_init(struct ata_host
*host
, struct device
*dev
,
5628 struct ata_port_operations
*ops
)
5630 spin_lock_init(&host
->lock
);
5631 mutex_init(&host
->eh_mutex
);
5632 host
->n_tags
= ATA_MAX_QUEUE
;
5635 kref_init(&host
->kref
);
5637 EXPORT_SYMBOL_GPL(ata_host_init
);
5639 void __ata_port_probe(struct ata_port
*ap
)
5641 struct ata_eh_info
*ehi
= &ap
->link
.eh_info
;
5642 unsigned long flags
;
5644 /* kick EH for boot probing */
5645 spin_lock_irqsave(ap
->lock
, flags
);
5647 ehi
->probe_mask
|= ATA_ALL_DEVICES
;
5648 ehi
->action
|= ATA_EH_RESET
;
5649 ehi
->flags
|= ATA_EHI_NO_AUTOPSY
| ATA_EHI_QUIET
;
5651 ap
->pflags
&= ~ATA_PFLAG_INITIALIZING
;
5652 ap
->pflags
|= ATA_PFLAG_LOADING
;
5653 ata_port_schedule_eh(ap
);
5655 spin_unlock_irqrestore(ap
->lock
, flags
);
5658 int ata_port_probe(struct ata_port
*ap
)
5662 if (ap
->ops
->error_handler
) {
5663 __ata_port_probe(ap
);
5664 ata_port_wait_eh(ap
);
5666 DPRINTK("ata%u: bus probe begin\n", ap
->print_id
);
5667 rc
= ata_bus_probe(ap
);
5668 DPRINTK("ata%u: bus probe end\n", ap
->print_id
);
5674 static void async_port_probe(void *data
, async_cookie_t cookie
)
5676 struct ata_port
*ap
= data
;
5679 * If we're not allowed to scan this host in parallel,
5680 * we need to wait until all previous scans have completed
5681 * before going further.
5682 * Jeff Garzik says this is only within a controller, so we
5683 * don't need to wait for port 0, only for later ports.
5685 if (!(ap
->host
->flags
& ATA_HOST_PARALLEL_SCAN
) && ap
->port_no
!= 0)
5686 async_synchronize_cookie(cookie
);
5688 (void)ata_port_probe(ap
);
5690 /* in order to keep device order, we need to synchronize at this point */
5691 async_synchronize_cookie(cookie
);
5693 ata_scsi_scan_host(ap
, 1);
5697 * ata_host_register - register initialized ATA host
5698 * @host: ATA host to register
5699 * @sht: template for SCSI host
5701 * Register initialized ATA host. @host is allocated using
5702 * ata_host_alloc() and fully initialized by LLD. This function
5703 * starts ports, registers @host with ATA and SCSI layers and
5704 * probe registered devices.
5707 * Inherited from calling layer (may sleep).
5710 * 0 on success, -errno otherwise.
5712 int ata_host_register(struct ata_host
*host
, struct scsi_host_template
*sht
)
5716 host
->n_tags
= clamp(sht
->can_queue
, 1, ATA_MAX_QUEUE
);
5718 /* host must have been started */
5719 if (!(host
->flags
& ATA_HOST_STARTED
)) {
5720 dev_err(host
->dev
, "BUG: trying to register unstarted host\n");
5725 /* Blow away unused ports. This happens when LLD can't
5726 * determine the exact number of ports to allocate at
5729 for (i
= host
->n_ports
; host
->ports
[i
]; i
++)
5730 kfree(host
->ports
[i
]);
5732 /* give ports names and add SCSI hosts */
5733 for (i
= 0; i
< host
->n_ports
; i
++) {
5734 host
->ports
[i
]->print_id
= atomic_inc_return(&ata_print_id
);
5735 host
->ports
[i
]->local_port_no
= i
+ 1;
5738 /* Create associated sysfs transport objects */
5739 for (i
= 0; i
< host
->n_ports
; i
++) {
5740 rc
= ata_tport_add(host
->dev
,host
->ports
[i
]);
5746 rc
= ata_scsi_add_hosts(host
, sht
);
5750 /* set cable, sata_spd_limit and report */
5751 for (i
= 0; i
< host
->n_ports
; i
++) {
5752 struct ata_port
*ap
= host
->ports
[i
];
5753 unsigned long xfer_mask
;
5755 /* set SATA cable type if still unset */
5756 if (ap
->cbl
== ATA_CBL_NONE
&& (ap
->flags
& ATA_FLAG_SATA
))
5757 ap
->cbl
= ATA_CBL_SATA
;
5759 /* init sata_spd_limit to the current value */
5760 sata_link_init_spd(&ap
->link
);
5762 sata_link_init_spd(ap
->slave_link
);
5764 /* print per-port info to dmesg */
5765 xfer_mask
= ata_pack_xfermask(ap
->pio_mask
, ap
->mwdma_mask
,
5768 if (!ata_port_is_dummy(ap
)) {
5769 ata_port_info(ap
, "%cATA max %s %s\n",
5770 (ap
->flags
& ATA_FLAG_SATA
) ? 'S' : 'P',
5771 ata_mode_string(xfer_mask
),
5772 ap
->link
.eh_info
.desc
);
5773 ata_ehi_clear_desc(&ap
->link
.eh_info
);
5775 ata_port_info(ap
, "DUMMY\n");
5778 /* perform each probe asynchronously */
5779 for (i
= 0; i
< host
->n_ports
; i
++) {
5780 struct ata_port
*ap
= host
->ports
[i
];
5781 async_schedule(async_port_probe
, ap
);
5788 ata_tport_delete(host
->ports
[i
]);
5793 EXPORT_SYMBOL_GPL(ata_host_register
);
5796 * ata_host_activate - start host, request IRQ and register it
5797 * @host: target ATA host
5798 * @irq: IRQ to request
5799 * @irq_handler: irq_handler used when requesting IRQ
5800 * @irq_flags: irq_flags used when requesting IRQ
5801 * @sht: scsi_host_template to use when registering the host
5803 * After allocating an ATA host and initializing it, most libata
5804 * LLDs perform three steps to activate the host - start host,
5805 * request IRQ and register it. This helper takes necessary
5806 * arguments and performs the three steps in one go.
5808 * An invalid IRQ skips the IRQ registration and expects the host to
5809 * have set polling mode on the port. In this case, @irq_handler
5813 * Inherited from calling layer (may sleep).
5816 * 0 on success, -errno otherwise.
5818 int ata_host_activate(struct ata_host
*host
, int irq
,
5819 irq_handler_t irq_handler
, unsigned long irq_flags
,
5820 struct scsi_host_template
*sht
)
5825 rc
= ata_host_start(host
);
5829 /* Special case for polling mode */
5831 WARN_ON(irq_handler
);
5832 return ata_host_register(host
, sht
);
5835 irq_desc
= devm_kasprintf(host
->dev
, GFP_KERNEL
, "%s[%s]",
5836 dev_driver_string(host
->dev
),
5837 dev_name(host
->dev
));
5841 rc
= devm_request_irq(host
->dev
, irq
, irq_handler
, irq_flags
,
5846 for (i
= 0; i
< host
->n_ports
; i
++)
5847 ata_port_desc(host
->ports
[i
], "irq %d", irq
);
5849 rc
= ata_host_register(host
, sht
);
5850 /* if failed, just free the IRQ and leave ports alone */
5852 devm_free_irq(host
->dev
, irq
, host
);
5856 EXPORT_SYMBOL_GPL(ata_host_activate
);
5859 * ata_port_detach - Detach ATA port in preparation of device removal
5860 * @ap: ATA port to be detached
5862 * Detach all ATA devices and the associated SCSI devices of @ap;
5863 * then, remove the associated SCSI host. @ap is guaranteed to
5864 * be quiescent on return from this function.
5867 * Kernel thread context (may sleep).
5869 static void ata_port_detach(struct ata_port
*ap
)
5871 unsigned long flags
;
5872 struct ata_link
*link
;
5873 struct ata_device
*dev
;
5875 if (!ap
->ops
->error_handler
)
5878 /* tell EH we're leaving & flush EH */
5879 spin_lock_irqsave(ap
->lock
, flags
);
5880 ap
->pflags
|= ATA_PFLAG_UNLOADING
;
5881 ata_port_schedule_eh(ap
);
5882 spin_unlock_irqrestore(ap
->lock
, flags
);
5884 /* wait till EH commits suicide */
5885 ata_port_wait_eh(ap
);
5887 /* it better be dead now */
5888 WARN_ON(!(ap
->pflags
& ATA_PFLAG_UNLOADED
));
5890 cancel_delayed_work_sync(&ap
->hotplug_task
);
5893 /* clean up zpodd on port removal */
5894 ata_for_each_link(link
, ap
, HOST_FIRST
) {
5895 ata_for_each_dev(dev
, link
, ALL
) {
5896 if (zpodd_dev_enabled(dev
))
5902 for (i
= 0; i
< SATA_PMP_MAX_PORTS
; i
++)
5903 ata_tlink_delete(&ap
->pmp_link
[i
]);
5905 /* remove the associated SCSI host */
5906 scsi_remove_host(ap
->scsi_host
);
5907 ata_tport_delete(ap
);
5911 * ata_host_detach - Detach all ports of an ATA host
5912 * @host: Host to detach
5914 * Detach all ports of @host.
5917 * Kernel thread context (may sleep).
5919 void ata_host_detach(struct ata_host
*host
)
5923 /* Ensure ata_port probe has completed */
5924 async_synchronize_full();
5926 for (i
= 0; i
< host
->n_ports
; i
++)
5927 ata_port_detach(host
->ports
[i
]);
5929 /* the host is dead now, dissociate ACPI */
5930 ata_acpi_dissociate(host
);
5932 EXPORT_SYMBOL_GPL(ata_host_detach
);
5937 * ata_pci_remove_one - PCI layer callback for device removal
5938 * @pdev: PCI device that was removed
5940 * PCI layer indicates to libata via this hook that hot-unplug or
5941 * module unload event has occurred. Detach all ports. Resource
5942 * release is handled via devres.
5945 * Inherited from PCI layer (may sleep).
5947 void ata_pci_remove_one(struct pci_dev
*pdev
)
5949 struct ata_host
*host
= pci_get_drvdata(pdev
);
5951 ata_host_detach(host
);
5953 EXPORT_SYMBOL_GPL(ata_pci_remove_one
);
5955 void ata_pci_shutdown_one(struct pci_dev
*pdev
)
5957 struct ata_host
*host
= pci_get_drvdata(pdev
);
5960 for (i
= 0; i
< host
->n_ports
; i
++) {
5961 struct ata_port
*ap
= host
->ports
[i
];
5963 ap
->pflags
|= ATA_PFLAG_FROZEN
;
5965 /* Disable port interrupts */
5966 if (ap
->ops
->freeze
)
5967 ap
->ops
->freeze(ap
);
5969 /* Stop the port DMA engines */
5970 if (ap
->ops
->port_stop
)
5971 ap
->ops
->port_stop(ap
);
5974 EXPORT_SYMBOL_GPL(ata_pci_shutdown_one
);
5976 /* move to PCI subsystem */
5977 int pci_test_config_bits(struct pci_dev
*pdev
, const struct pci_bits
*bits
)
5979 unsigned long tmp
= 0;
5981 switch (bits
->width
) {
5984 pci_read_config_byte(pdev
, bits
->reg
, &tmp8
);
5990 pci_read_config_word(pdev
, bits
->reg
, &tmp16
);
5996 pci_read_config_dword(pdev
, bits
->reg
, &tmp32
);
6007 return (tmp
== bits
->val
) ? 1 : 0;
6009 EXPORT_SYMBOL_GPL(pci_test_config_bits
);
6012 void ata_pci_device_do_suspend(struct pci_dev
*pdev
, pm_message_t mesg
)
6014 pci_save_state(pdev
);
6015 pci_disable_device(pdev
);
6017 if (mesg
.event
& PM_EVENT_SLEEP
)
6018 pci_set_power_state(pdev
, PCI_D3hot
);
6020 EXPORT_SYMBOL_GPL(ata_pci_device_do_suspend
);
6022 int ata_pci_device_do_resume(struct pci_dev
*pdev
)
6026 pci_set_power_state(pdev
, PCI_D0
);
6027 pci_restore_state(pdev
);
6029 rc
= pcim_enable_device(pdev
);
6032 "failed to enable device after resume (%d)\n", rc
);
6036 pci_set_master(pdev
);
6039 EXPORT_SYMBOL_GPL(ata_pci_device_do_resume
);
6041 int ata_pci_device_suspend(struct pci_dev
*pdev
, pm_message_t mesg
)
6043 struct ata_host
*host
= pci_get_drvdata(pdev
);
6046 rc
= ata_host_suspend(host
, mesg
);
6050 ata_pci_device_do_suspend(pdev
, mesg
);
6054 EXPORT_SYMBOL_GPL(ata_pci_device_suspend
);
6056 int ata_pci_device_resume(struct pci_dev
*pdev
)
6058 struct ata_host
*host
= pci_get_drvdata(pdev
);
6061 rc
= ata_pci_device_do_resume(pdev
);
6063 ata_host_resume(host
);
6066 EXPORT_SYMBOL_GPL(ata_pci_device_resume
);
6067 #endif /* CONFIG_PM */
6068 #endif /* CONFIG_PCI */
6071 * ata_platform_remove_one - Platform layer callback for device removal
6072 * @pdev: Platform device that was removed
6074 * Platform layer indicates to libata via this hook that hot-unplug or
6075 * module unload event has occurred. Detach all ports. Resource
6076 * release is handled via devres.
6079 * Inherited from platform layer (may sleep).
6081 int ata_platform_remove_one(struct platform_device
*pdev
)
6083 struct ata_host
*host
= platform_get_drvdata(pdev
);
6085 ata_host_detach(host
);
6089 EXPORT_SYMBOL_GPL(ata_platform_remove_one
);
6091 #ifdef CONFIG_ATA_FORCE
6092 static int __init
ata_parse_force_one(char **cur
,
6093 struct ata_force_ent
*force_ent
,
6094 const char **reason
)
6096 static const struct ata_force_param force_tbl
[] __initconst
= {
6097 { "40c", .cbl
= ATA_CBL_PATA40
},
6098 { "80c", .cbl
= ATA_CBL_PATA80
},
6099 { "short40c", .cbl
= ATA_CBL_PATA40_SHORT
},
6100 { "unk", .cbl
= ATA_CBL_PATA_UNK
},
6101 { "ign", .cbl
= ATA_CBL_PATA_IGN
},
6102 { "sata", .cbl
= ATA_CBL_SATA
},
6103 { "1.5Gbps", .spd_limit
= 1 },
6104 { "3.0Gbps", .spd_limit
= 2 },
6105 { "noncq", .horkage_on
= ATA_HORKAGE_NONCQ
},
6106 { "ncq", .horkage_off
= ATA_HORKAGE_NONCQ
},
6107 { "noncqtrim", .horkage_on
= ATA_HORKAGE_NO_NCQ_TRIM
},
6108 { "ncqtrim", .horkage_off
= ATA_HORKAGE_NO_NCQ_TRIM
},
6109 { "dump_id", .horkage_on
= ATA_HORKAGE_DUMP_ID
},
6110 { "pio0", .xfer_mask
= 1 << (ATA_SHIFT_PIO
+ 0) },
6111 { "pio1", .xfer_mask
= 1 << (ATA_SHIFT_PIO
+ 1) },
6112 { "pio2", .xfer_mask
= 1 << (ATA_SHIFT_PIO
+ 2) },
6113 { "pio3", .xfer_mask
= 1 << (ATA_SHIFT_PIO
+ 3) },
6114 { "pio4", .xfer_mask
= 1 << (ATA_SHIFT_PIO
+ 4) },
6115 { "pio5", .xfer_mask
= 1 << (ATA_SHIFT_PIO
+ 5) },
6116 { "pio6", .xfer_mask
= 1 << (ATA_SHIFT_PIO
+ 6) },
6117 { "mwdma0", .xfer_mask
= 1 << (ATA_SHIFT_MWDMA
+ 0) },
6118 { "mwdma1", .xfer_mask
= 1 << (ATA_SHIFT_MWDMA
+ 1) },
6119 { "mwdma2", .xfer_mask
= 1 << (ATA_SHIFT_MWDMA
+ 2) },
6120 { "mwdma3", .xfer_mask
= 1 << (ATA_SHIFT_MWDMA
+ 3) },
6121 { "mwdma4", .xfer_mask
= 1 << (ATA_SHIFT_MWDMA
+ 4) },
6122 { "udma0", .xfer_mask
= 1 << (ATA_SHIFT_UDMA
+ 0) },
6123 { "udma16", .xfer_mask
= 1 << (ATA_SHIFT_UDMA
+ 0) },
6124 { "udma/16", .xfer_mask
= 1 << (ATA_SHIFT_UDMA
+ 0) },
6125 { "udma1", .xfer_mask
= 1 << (ATA_SHIFT_UDMA
+ 1) },
6126 { "udma25", .xfer_mask
= 1 << (ATA_SHIFT_UDMA
+ 1) },
6127 { "udma/25", .xfer_mask
= 1 << (ATA_SHIFT_UDMA
+ 1) },
6128 { "udma2", .xfer_mask
= 1 << (ATA_SHIFT_UDMA
+ 2) },
6129 { "udma33", .xfer_mask
= 1 << (ATA_SHIFT_UDMA
+ 2) },
6130 { "udma/33", .xfer_mask
= 1 << (ATA_SHIFT_UDMA
+ 2) },
6131 { "udma3", .xfer_mask
= 1 << (ATA_SHIFT_UDMA
+ 3) },
6132 { "udma44", .xfer_mask
= 1 << (ATA_SHIFT_UDMA
+ 3) },
6133 { "udma/44", .xfer_mask
= 1 << (ATA_SHIFT_UDMA
+ 3) },
6134 { "udma4", .xfer_mask
= 1 << (ATA_SHIFT_UDMA
+ 4) },
6135 { "udma66", .xfer_mask
= 1 << (ATA_SHIFT_UDMA
+ 4) },
6136 { "udma/66", .xfer_mask
= 1 << (ATA_SHIFT_UDMA
+ 4) },
6137 { "udma5", .xfer_mask
= 1 << (ATA_SHIFT_UDMA
+ 5) },
6138 { "udma100", .xfer_mask
= 1 << (ATA_SHIFT_UDMA
+ 5) },
6139 { "udma/100", .xfer_mask
= 1 << (ATA_SHIFT_UDMA
+ 5) },
6140 { "udma6", .xfer_mask
= 1 << (ATA_SHIFT_UDMA
+ 6) },
6141 { "udma133", .xfer_mask
= 1 << (ATA_SHIFT_UDMA
+ 6) },
6142 { "udma/133", .xfer_mask
= 1 << (ATA_SHIFT_UDMA
+ 6) },
6143 { "udma7", .xfer_mask
= 1 << (ATA_SHIFT_UDMA
+ 7) },
6144 { "nohrst", .lflags
= ATA_LFLAG_NO_HRST
},
6145 { "nosrst", .lflags
= ATA_LFLAG_NO_SRST
},
6146 { "norst", .lflags
= ATA_LFLAG_NO_HRST
| ATA_LFLAG_NO_SRST
},
6147 { "rstonce", .lflags
= ATA_LFLAG_RST_ONCE
},
6148 { "atapi_dmadir", .horkage_on
= ATA_HORKAGE_ATAPI_DMADIR
},
6149 { "disable", .horkage_on
= ATA_HORKAGE_DISABLE
},
6151 char *start
= *cur
, *p
= *cur
;
6152 char *id
, *val
, *endp
;
6153 const struct ata_force_param
*match_fp
= NULL
;
6154 int nr_matches
= 0, i
;
6156 /* find where this param ends and update *cur */
6157 while (*p
!= '\0' && *p
!= ',')
6168 p
= strchr(start
, ':');
6170 val
= strstrip(start
);
6175 id
= strstrip(start
);
6176 val
= strstrip(p
+ 1);
6179 p
= strchr(id
, '.');
6182 force_ent
->device
= simple_strtoul(p
, &endp
, 10);
6183 if (p
== endp
|| *endp
!= '\0') {
6184 *reason
= "invalid device";
6189 force_ent
->port
= simple_strtoul(id
, &endp
, 10);
6190 if (id
== endp
|| *endp
!= '\0') {
6191 *reason
= "invalid port/link";
6196 /* parse val, allow shortcuts so that both 1.5 and 1.5Gbps work */
6197 for (i
= 0; i
< ARRAY_SIZE(force_tbl
); i
++) {
6198 const struct ata_force_param
*fp
= &force_tbl
[i
];
6200 if (strncasecmp(val
, fp
->name
, strlen(val
)))
6206 if (strcasecmp(val
, fp
->name
) == 0) {
6213 *reason
= "unknown value";
6216 if (nr_matches
> 1) {
6217 *reason
= "ambiguous value";
6221 force_ent
->param
= *match_fp
;
6226 static void __init
ata_parse_force_param(void)
6228 int idx
= 0, size
= 1;
6229 int last_port
= -1, last_device
= -1;
6230 char *p
, *cur
, *next
;
6232 /* calculate maximum number of params and allocate force_tbl */
6233 for (p
= ata_force_param_buf
; *p
; p
++)
6237 ata_force_tbl
= kcalloc(size
, sizeof(ata_force_tbl
[0]), GFP_KERNEL
);
6238 if (!ata_force_tbl
) {
6239 printk(KERN_WARNING
"ata: failed to extend force table, "
6240 "libata.force ignored\n");
6244 /* parse and populate the table */
6245 for (cur
= ata_force_param_buf
; *cur
!= '\0'; cur
= next
) {
6246 const char *reason
= "";
6247 struct ata_force_ent te
= { .port
= -1, .device
= -1 };
6250 if (ata_parse_force_one(&next
, &te
, &reason
)) {
6251 printk(KERN_WARNING
"ata: failed to parse force "
6252 "parameter \"%s\" (%s)\n",
6257 if (te
.port
== -1) {
6258 te
.port
= last_port
;
6259 te
.device
= last_device
;
6262 ata_force_tbl
[idx
++] = te
;
6264 last_port
= te
.port
;
6265 last_device
= te
.device
;
6268 ata_force_tbl_size
= idx
;
6271 static void ata_free_force_param(void)
6273 kfree(ata_force_tbl
);
6276 static inline void ata_parse_force_param(void) { }
6277 static inline void ata_free_force_param(void) { }
6280 static int __init
ata_init(void)
6284 ata_parse_force_param();
6286 rc
= ata_sff_init();
6288 ata_free_force_param();
6292 libata_transport_init();
6293 ata_scsi_transport_template
= ata_attach_transport();
6294 if (!ata_scsi_transport_template
) {
6300 printk(KERN_DEBUG
"libata version " DRV_VERSION
" loaded.\n");
6307 static void __exit
ata_exit(void)
6309 ata_release_transport(ata_scsi_transport_template
);
6310 libata_transport_exit();
6312 ata_free_force_param();
6315 subsys_initcall(ata_init
);
6316 module_exit(ata_exit
);
6318 static DEFINE_RATELIMIT_STATE(ratelimit
, HZ
/ 5, 1);
6320 int ata_ratelimit(void)
6322 return __ratelimit(&ratelimit
);
6324 EXPORT_SYMBOL_GPL(ata_ratelimit
);
6327 * ata_msleep - ATA EH owner aware msleep
6328 * @ap: ATA port to attribute the sleep to
6329 * @msecs: duration to sleep in milliseconds
6331 * Sleeps @msecs. If the current task is owner of @ap's EH, the
6332 * ownership is released before going to sleep and reacquired
6333 * after the sleep is complete. IOW, other ports sharing the
6334 * @ap->host will be allowed to own the EH while this task is
6340 void ata_msleep(struct ata_port
*ap
, unsigned int msecs
)
6342 bool owns_eh
= ap
&& ap
->host
->eh_owner
== current
;
6348 unsigned long usecs
= msecs
* USEC_PER_MSEC
;
6349 usleep_range(usecs
, usecs
+ 50);
6357 EXPORT_SYMBOL_GPL(ata_msleep
);
6360 * ata_wait_register - wait until register value changes
6361 * @ap: ATA port to wait register for, can be NULL
6362 * @reg: IO-mapped register
6363 * @mask: Mask to apply to read register value
6364 * @val: Wait condition
6365 * @interval: polling interval in milliseconds
6366 * @timeout: timeout in milliseconds
6368 * Waiting for some bits of register to change is a common
6369 * operation for ATA controllers. This function reads 32bit LE
6370 * IO-mapped register @reg and tests for the following condition.
6372 * (*@reg & mask) != val
6374 * If the condition is met, it returns; otherwise, the process is
6375 * repeated after @interval_msec until timeout.
6378 * Kernel thread context (may sleep)
6381 * The final register value.
6383 u32
ata_wait_register(struct ata_port
*ap
, void __iomem
*reg
, u32 mask
, u32 val
,
6384 unsigned long interval
, unsigned long timeout
)
6386 unsigned long deadline
;
6389 tmp
= ioread32(reg
);
6391 /* Calculate timeout _after_ the first read to make sure
6392 * preceding writes reach the controller before starting to
6393 * eat away the timeout.
6395 deadline
= ata_deadline(jiffies
, timeout
);
6397 while ((tmp
& mask
) == val
&& time_before(jiffies
, deadline
)) {
6398 ata_msleep(ap
, interval
);
6399 tmp
= ioread32(reg
);
6404 EXPORT_SYMBOL_GPL(ata_wait_register
);
6409 static unsigned int ata_dummy_qc_issue(struct ata_queued_cmd
*qc
)
6411 return AC_ERR_SYSTEM
;
6414 static void ata_dummy_error_handler(struct ata_port
*ap
)
6419 struct ata_port_operations ata_dummy_port_ops
= {
6420 .qc_prep
= ata_noop_qc_prep
,
6421 .qc_issue
= ata_dummy_qc_issue
,
6422 .error_handler
= ata_dummy_error_handler
,
6423 .sched_eh
= ata_std_sched_eh
,
6424 .end_eh
= ata_std_end_eh
,
6426 EXPORT_SYMBOL_GPL(ata_dummy_port_ops
);
6428 const struct ata_port_info ata_dummy_port_info
= {
6429 .port_ops
= &ata_dummy_port_ops
,
6431 EXPORT_SYMBOL_GPL(ata_dummy_port_info
);
6434 * Utility print functions
6436 void ata_port_printk(const struct ata_port
*ap
, const char *level
,
6437 const char *fmt
, ...)
6439 struct va_format vaf
;
6442 va_start(args
, fmt
);
6447 printk("%sata%u: %pV", level
, ap
->print_id
, &vaf
);
6451 EXPORT_SYMBOL(ata_port_printk
);
6453 void ata_link_printk(const struct ata_link
*link
, const char *level
,
6454 const char *fmt
, ...)
6456 struct va_format vaf
;
6459 va_start(args
, fmt
);
6464 if (sata_pmp_attached(link
->ap
) || link
->ap
->slave_link
)
6465 printk("%sata%u.%02u: %pV",
6466 level
, link
->ap
->print_id
, link
->pmp
, &vaf
);
6468 printk("%sata%u: %pV",
6469 level
, link
->ap
->print_id
, &vaf
);
6473 EXPORT_SYMBOL(ata_link_printk
);
6475 void ata_dev_printk(const struct ata_device
*dev
, const char *level
,
6476 const char *fmt
, ...)
6478 struct va_format vaf
;
6481 va_start(args
, fmt
);
6486 printk("%sata%u.%02u: %pV",
6487 level
, dev
->link
->ap
->print_id
, dev
->link
->pmp
+ dev
->devno
,
6492 EXPORT_SYMBOL(ata_dev_printk
);
6494 void ata_print_version(const struct device
*dev
, const char *version
)
6496 dev_printk(KERN_DEBUG
, dev
, "version %s\n", version
);
6498 EXPORT_SYMBOL(ata_print_version
);