xref: /linux/include/linux/libata.h (revision fab183d632628381b466a41479489541ac0e29a0)
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3  *  Copyright 2003-2005 Red Hat, Inc.  All rights reserved.
4  *  Copyright 2003-2005 Jeff Garzik
5  *
6  *  libata documentation is available via 'make {ps|pdf}docs',
7  *  as Documentation/driver-api/libata.rst
8  */
9 
10 #ifndef __LINUX_LIBATA_H__
11 #define __LINUX_LIBATA_H__
12 
13 #include <linux/delay.h>
14 #include <linux/jiffies.h>
15 #include <linux/interrupt.h>
16 #include <linux/dma-mapping.h>
17 #include <linux/scatterlist.h>
18 #include <linux/io.h>
19 #include <linux/ata.h>
20 #include <linux/workqueue.h>
21 #include <scsi/scsi_host.h>
22 #include <linux/acpi.h>
23 #include <linux/cdrom.h>
24 #include <linux/sched.h>
25 #include <linux/async.h>
26 
27 /*
28  * Define if arch has non-standard setup.  This is a _PCI_ standard
29  * not a legacy or ISA standard.
30  */
31 #ifdef CONFIG_ATA_NONSTANDARD
32 #include <asm/libata-portmap.h>
33 #else
34 #define ATA_PRIMARY_IRQ(dev)	14
35 #define ATA_SECONDARY_IRQ(dev)	15
36 #endif
37 
38 /*
39  * compile-time options: to be removed as soon as all the drivers are
40  * converted to the new debugging mechanism
41  */
42 #undef ATA_IRQ_TRAP		/* define to ack screaming irqs */
43 
44 /* defines only for the constants which don't work well as enums */
45 #define ATA_TAG_POISON		0xfafbfcfdU
46 
47 /*
48  * Quirk flags bits.
49  * ata_device->quirks is an u64, so __ATA_QUIRK_MAX must not exceed 64.
50  */
51 enum ata_quirks {
52 	__ATA_QUIRK_DIAGNOSTIC,		/* Failed boot diag */
53 	__ATA_QUIRK_NODMA,		/* DMA problems */
54 	__ATA_QUIRK_NONCQ,		/* Don't use NCQ */
55 	__ATA_QUIRK_BROKEN_HPA,		/* Broken HPA */
56 	__ATA_QUIRK_DISABLE,		/* Disable it */
57 	__ATA_QUIRK_HPA_SIZE,		/* Native size off by one */
58 	__ATA_QUIRK_IVB,		/* cbl det validity bit bugs */
59 	__ATA_QUIRK_STUCK_ERR,		/* Stuck ERR on next PACKET */
60 	__ATA_QUIRK_BRIDGE_OK,		/* No bridge limits */
61 	__ATA_QUIRK_ATAPI_MOD16_DMA,	/* Use ATAPI DMA for commands that */
62 					/* are not a multiple of 16 bytes */
63 	__ATA_QUIRK_FIRMWARE_WARN,	/* Firmware update warning */
64 	__ATA_QUIRK_1_5_GBPS,		/* Force 1.5 Gbps */
65 	__ATA_QUIRK_NOSETXFER,		/* Skip SETXFER, SATA only */
66 	__ATA_QUIRK_BROKEN_FPDMA_AA,	/* Skip AA */
67 	__ATA_QUIRK_DUMP_ID,		/* Dump IDENTIFY data */
68 	__ATA_QUIRK_MAX_SEC_LBA48,	/* Set max sects to 65535 */
69 	__ATA_QUIRK_ATAPI_DMADIR,	/* Device requires dmadir */
70 	__ATA_QUIRK_NO_NCQ_TRIM,	/* Do not use queued TRIM */
71 	__ATA_QUIRK_NOLPM,		/* Do not use LPM */
72 	__ATA_QUIRK_WD_BROKEN_LPM,	/* Some WDs have broken LPM */
73 	__ATA_QUIRK_ZERO_AFTER_TRIM,	/* Guarantees zero after trim */
74 	__ATA_QUIRK_NO_DMA_LOG,		/* Do not use DMA for log read */
75 	__ATA_QUIRK_NOTRIM,		/* Do not use TRIM */
76 	__ATA_QUIRK_MAX_SEC,		/* Limit max sectors */
77 	__ATA_QUIRK_MAX_TRIM_128M,	/* Limit max trim size to 128M */
78 	__ATA_QUIRK_NO_NCQ_ON_ATI,	/* Disable NCQ on ATI chipset */
79 	__ATA_QUIRK_NO_LPM_ON_ATI,	/* Disable LPM on ATI chipset */
80 	__ATA_QUIRK_NO_ID_DEV_LOG,	/* Identify device log missing */
81 	__ATA_QUIRK_NO_LOG_DIR,		/* Do not read log directory */
82 	__ATA_QUIRK_NO_FUA,		/* Do not use FUA */
83 
84 	__ATA_QUIRK_MAX,
85 };
86 
87 /*
88  * Quirk flags: may be set by libata or controller drivers on drives.
89  * Some quirks may be drive/controller pair dependent.
90  */
91 enum {
92 	ATA_QUIRK_DIAGNOSTIC		= BIT_ULL(__ATA_QUIRK_DIAGNOSTIC),
93 	ATA_QUIRK_NODMA			= BIT_ULL(__ATA_QUIRK_NODMA),
94 	ATA_QUIRK_NONCQ			= BIT_ULL(__ATA_QUIRK_NONCQ),
95 	ATA_QUIRK_BROKEN_HPA		= BIT_ULL(__ATA_QUIRK_BROKEN_HPA),
96 	ATA_QUIRK_DISABLE		= BIT_ULL(__ATA_QUIRK_DISABLE),
97 	ATA_QUIRK_HPA_SIZE		= BIT_ULL(__ATA_QUIRK_HPA_SIZE),
98 	ATA_QUIRK_IVB			= BIT_ULL(__ATA_QUIRK_IVB),
99 	ATA_QUIRK_STUCK_ERR		= BIT_ULL(__ATA_QUIRK_STUCK_ERR),
100 	ATA_QUIRK_BRIDGE_OK		= BIT_ULL(__ATA_QUIRK_BRIDGE_OK),
101 	ATA_QUIRK_ATAPI_MOD16_DMA	= BIT_ULL(__ATA_QUIRK_ATAPI_MOD16_DMA),
102 	ATA_QUIRK_FIRMWARE_WARN		= BIT_ULL(__ATA_QUIRK_FIRMWARE_WARN),
103 	ATA_QUIRK_1_5_GBPS		= BIT_ULL(__ATA_QUIRK_1_5_GBPS),
104 	ATA_QUIRK_NOSETXFER		= BIT_ULL(__ATA_QUIRK_NOSETXFER),
105 	ATA_QUIRK_BROKEN_FPDMA_AA	= BIT_ULL(__ATA_QUIRK_BROKEN_FPDMA_AA),
106 	ATA_QUIRK_DUMP_ID		= BIT_ULL(__ATA_QUIRK_DUMP_ID),
107 	ATA_QUIRK_MAX_SEC_LBA48		= BIT_ULL(__ATA_QUIRK_MAX_SEC_LBA48),
108 	ATA_QUIRK_ATAPI_DMADIR		= BIT_ULL(__ATA_QUIRK_ATAPI_DMADIR),
109 	ATA_QUIRK_NO_NCQ_TRIM		= BIT_ULL(__ATA_QUIRK_NO_NCQ_TRIM),
110 	ATA_QUIRK_NOLPM			= BIT_ULL(__ATA_QUIRK_NOLPM),
111 	ATA_QUIRK_WD_BROKEN_LPM		= BIT_ULL(__ATA_QUIRK_WD_BROKEN_LPM),
112 	ATA_QUIRK_ZERO_AFTER_TRIM	= BIT_ULL(__ATA_QUIRK_ZERO_AFTER_TRIM),
113 	ATA_QUIRK_NO_DMA_LOG		= BIT_ULL(__ATA_QUIRK_NO_DMA_LOG),
114 	ATA_QUIRK_NOTRIM		= BIT_ULL(__ATA_QUIRK_NOTRIM),
115 	ATA_QUIRK_MAX_SEC		= BIT_ULL(__ATA_QUIRK_MAX_SEC),
116 	ATA_QUIRK_MAX_TRIM_128M		= BIT_ULL(__ATA_QUIRK_MAX_TRIM_128M),
117 	ATA_QUIRK_NO_NCQ_ON_ATI		= BIT_ULL(__ATA_QUIRK_NO_NCQ_ON_ATI),
118 	ATA_QUIRK_NO_LPM_ON_ATI		= BIT_ULL(__ATA_QUIRK_NO_LPM_ON_ATI),
119 	ATA_QUIRK_NO_ID_DEV_LOG		= BIT_ULL(__ATA_QUIRK_NO_ID_DEV_LOG),
120 	ATA_QUIRK_NO_LOG_DIR		= BIT_ULL(__ATA_QUIRK_NO_LOG_DIR),
121 	ATA_QUIRK_NO_FUA		= BIT_ULL(__ATA_QUIRK_NO_FUA),
122 };
123 
124 /*
125  * struct ata_device flags
126  */
127 enum {
128 	ATA_DFLAG_LBA		= (1UL << 0), /* device supports LBA */
129 	ATA_DFLAG_LBA48		= (1UL << 1), /* device supports LBA48 */
130 	ATA_DFLAG_CDB_INTR	= (1UL << 2), /* device asserts INTRQ when ready for CDB */
131 	ATA_DFLAG_NCQ		= (1UL << 3), /* device supports NCQ */
132 	ATA_DFLAG_FLUSH_EXT	= (1UL << 4), /* do FLUSH_EXT instead of FLUSH */
133 	ATA_DFLAG_ACPI_PENDING	= (1UL << 5), /* ACPI resume action pending */
134 	ATA_DFLAG_ACPI_FAILED	= (1UL << 6), /* ACPI on devcfg has failed */
135 	ATA_DFLAG_AN		= (1UL << 7), /* AN configured */
136 	ATA_DFLAG_TRUSTED	= (1UL << 8), /* device supports trusted send/recv */
137 	ATA_DFLAG_FUA		= (1UL << 9), /* device supports FUA */
138 	ATA_DFLAG_DMADIR	= (1UL << 10), /* device requires DMADIR */
139 	ATA_DFLAG_NCQ_SEND_RECV = (1UL << 11), /* device supports NCQ SEND and RECV */
140 	ATA_DFLAG_NCQ_PRIO	= (1UL << 12), /* device supports NCQ priority */
141 	ATA_DFLAG_CDL		= (1UL << 13), /* supports cmd duration limits */
142 	ATA_DFLAG_DEPOP		= (1UL << 14), /* supports depopulation capability */
143 	ATA_DFLAG_DEPOP_RESTORE	= (1UL << 15), /* supports depopulation restoration */
144 	ATA_DFLAG_DEPOP_MODIFY	= (1UL << 16), /* supports zoned depopulation */
145 	ATA_DFLAG_CFG_MASK	= (1UL << 17) - 1,
146 
147 	ATA_DFLAG_PIO		= (1UL << 17), /* device limited to PIO mode */
148 	ATA_DFLAG_NCQ_OFF	= (1UL << 18), /* device limited to non-NCQ mode */
149 	ATA_DFLAG_SLEEPING	= (1UL << 19), /* device is sleeping */
150 	ATA_DFLAG_DUBIOUS_XFER	= (1UL << 20), /* data transfer not verified */
151 	ATA_DFLAG_NO_UNLOAD	= (1UL << 21), /* device doesn't support unload */
152 	ATA_DFLAG_UNLOCK_HPA	= (1UL << 22), /* unlock HPA */
153 	ATA_DFLAG_INIT_MASK	= (1UL << 23) - 1,
154 
155 	ATA_DFLAG_NCQ_PRIO_ENABLED = (1UL << 23), /* Priority cmds sent to dev */
156 	ATA_DFLAG_CDL_ENABLED	= (1UL << 24), /* cmd duration limits is enabled */
157 	ATA_DFLAG_RESUMING	= (1UL << 25),  /* Device is resuming */
158 	ATA_DFLAG_DETACH	= (1UL << 26),
159 	ATA_DFLAG_DETACHED	= (1UL << 27),
160 	ATA_DFLAG_DA		= (1UL << 28), /* device supports Device Attention */
161 	ATA_DFLAG_DEVSLP	= (1UL << 29), /* device supports Device Sleep */
162 	ATA_DFLAG_ACPI_DISABLED = (1UL << 30), /* ACPI for the device is disabled */
163 	ATA_DFLAG_D_SENSE	= (1UL << 31), /* Descriptor sense requested */
164 
165 	ATA_DFLAG_FEATURES_MASK	= (ATA_DFLAG_TRUSTED | ATA_DFLAG_DA |	\
166 				   ATA_DFLAG_DEVSLP | ATA_DFLAG_NCQ_SEND_RECV | \
167 				   ATA_DFLAG_NCQ_PRIO | ATA_DFLAG_FUA | \
168 				   ATA_DFLAG_CDL | ATA_DFLAG_DEPOP | \
169 				   ATA_DFLAG_DEPOP_RESTORE |
170 				   ATA_DFLAG_DEPOP_MODIFY)
171 };
172 
173 enum {
174 	/* various global constants */
175 	LIBATA_MAX_PRD		= ATA_MAX_PRD / 2,
176 	LIBATA_DUMB_MAX_PRD	= ATA_MAX_PRD / 4,	/* Worst case */
177 	ATA_DEF_QUEUE		= 1,
178 	ATA_MAX_QUEUE		= 32,
179 	ATA_TAG_INTERNAL	= ATA_MAX_QUEUE,
180 	ATA_SHORT_PAUSE		= 16,
181 
182 	ATAPI_MAX_DRAIN		= 16 << 10,
183 
184 	ATA_ALL_DEVICES		= (1 << ATA_MAX_DEVICES) - 1,
185 
186 	ATA_SHT_EMULATED	= 1,
187 	ATA_SHT_THIS_ID		= -1,
188 
189 	/* struct ata_taskfile flags */
190 	ATA_TFLAG_LBA48		= (1 << 0), /* enable 48-bit LBA and "HOB" */
191 	ATA_TFLAG_ISADDR	= (1 << 1), /* enable r/w to nsect/lba regs */
192 	ATA_TFLAG_DEVICE	= (1 << 2), /* enable r/w to device reg */
193 	ATA_TFLAG_WRITE		= (1 << 3), /* data dir: host->dev==1 (write) */
194 	ATA_TFLAG_LBA		= (1 << 4), /* enable LBA */
195 	ATA_TFLAG_FUA		= (1 << 5), /* enable FUA */
196 	ATA_TFLAG_POLLING	= (1 << 6), /* set nIEN to 1 and use polling */
197 
198 	/* sturct ata_device class. */
199 	ATA_DEV_UNKNOWN		= 0,	/* unknown device */
200 	ATA_DEV_ATA		= 1,	/* ATA device */
201 	ATA_DEV_ATA_UNSUP	= 2,	/* ATA device (unsupported) */
202 	ATA_DEV_ATAPI		= 3,	/* ATAPI device */
203 	ATA_DEV_ATAPI_UNSUP	= 4,	/* ATAPI device (unsupported) */
204 	ATA_DEV_PMP		= 5,	/* SATA port multiplier */
205 	ATA_DEV_PMP_UNSUP	= 6,	/* SATA port multiplier (unsupported) */
206 	ATA_DEV_SEMB		= 7,	/* SEMB */
207 	ATA_DEV_SEMB_UNSUP	= 8,	/* SEMB (unsupported) */
208 	ATA_DEV_ZAC		= 9,	/* ZAC device */
209 	ATA_DEV_ZAC_UNSUP	= 10,	/* ZAC device (unsupported) */
210 	ATA_DEV_NONE		= 11,	/* no device */
211 
212 	/* struct ata_link flags */
213 	/* NOTE: struct ata_force_param currently stores lflags in u16 */
214 	ATA_LFLAG_NO_HRST	= (1 << 1), /* avoid hardreset */
215 	ATA_LFLAG_NO_SRST	= (1 << 2), /* avoid softreset */
216 	ATA_LFLAG_ASSUME_ATA	= (1 << 3), /* assume ATA class */
217 	ATA_LFLAG_ASSUME_SEMB	= (1 << 4), /* assume SEMB class */
218 	ATA_LFLAG_ASSUME_CLASS	= ATA_LFLAG_ASSUME_ATA | ATA_LFLAG_ASSUME_SEMB,
219 	ATA_LFLAG_NO_RETRY	= (1 << 5), /* don't retry this link */
220 	ATA_LFLAG_DISABLED	= (1 << 6), /* link is disabled */
221 	ATA_LFLAG_SW_ACTIVITY	= (1 << 7), /* keep activity stats */
222 	ATA_LFLAG_NO_LPM	= (1 << 8), /* disable LPM on this link */
223 	ATA_LFLAG_RST_ONCE	= (1 << 9), /* limit recovery to one reset */
224 	ATA_LFLAG_CHANGED	= (1 << 10), /* LPM state changed on this link */
225 	ATA_LFLAG_NO_DEBOUNCE_DELAY = (1 << 11), /* no debounce delay on link resume */
226 
227 	/* struct ata_port flags */
228 	ATA_FLAG_SLAVE_POSS	= (1 << 0), /* host supports slave dev */
229 					    /* (doesn't imply presence) */
230 	ATA_FLAG_SATA		= (1 << 1),
231 	ATA_FLAG_NO_LPM		= (1 << 2), /* host not happy with LPM */
232 	ATA_FLAG_NO_LOG_PAGE	= (1 << 5), /* do not issue log page read */
233 	ATA_FLAG_NO_ATAPI	= (1 << 6), /* No ATAPI support */
234 	ATA_FLAG_PIO_DMA	= (1 << 7), /* PIO cmds via DMA */
235 	ATA_FLAG_PIO_LBA48	= (1 << 8), /* Host DMA engine is LBA28 only */
236 	ATA_FLAG_PIO_POLLING	= (1 << 9), /* use polling PIO if LLD
237 					     * doesn't handle PIO interrupts */
238 	ATA_FLAG_NCQ		= (1 << 10), /* host supports NCQ */
239 	ATA_FLAG_NO_POWEROFF_SPINDOWN = (1 << 11), /* don't spindown before poweroff */
240 	ATA_FLAG_NO_HIBERNATE_SPINDOWN = (1 << 12), /* don't spindown before hibernation */
241 	ATA_FLAG_DEBUGMSG	= (1 << 13),
242 	ATA_FLAG_FPDMA_AA		= (1 << 14), /* driver supports Auto-Activate */
243 	ATA_FLAG_IGN_SIMPLEX	= (1 << 15), /* ignore SIMPLEX */
244 	ATA_FLAG_NO_IORDY	= (1 << 16), /* controller lacks iordy */
245 	ATA_FLAG_ACPI_SATA	= (1 << 17), /* need native SATA ACPI layout */
246 	ATA_FLAG_AN		= (1 << 18), /* controller supports AN */
247 	ATA_FLAG_PMP		= (1 << 19), /* controller supports PMP */
248 	ATA_FLAG_FPDMA_AUX	= (1 << 20), /* controller supports H2DFIS aux field */
249 	ATA_FLAG_EM		= (1 << 21), /* driver supports enclosure
250 					      * management */
251 	ATA_FLAG_SW_ACTIVITY	= (1 << 22), /* driver supports sw activity
252 					      * led */
253 	ATA_FLAG_NO_DIPM	= (1 << 23), /* host not happy with DIPM */
254 	ATA_FLAG_SAS_HOST	= (1 << 24), /* SAS host */
255 
256 	/* bits 24:31 of ap->flags are reserved for LLD specific flags */
257 
258 
259 	/* struct ata_port pflags */
260 	ATA_PFLAG_EH_PENDING	= (1 << 0), /* EH pending */
261 	ATA_PFLAG_EH_IN_PROGRESS = (1 << 1), /* EH in progress */
262 	ATA_PFLAG_FROZEN	= (1 << 2), /* port is frozen */
263 	ATA_PFLAG_RECOVERED	= (1 << 3), /* recovery action performed */
264 	ATA_PFLAG_LOADING	= (1 << 4), /* boot/loading probe */
265 	ATA_PFLAG_SCSI_HOTPLUG	= (1 << 6), /* SCSI hotplug scheduled */
266 	ATA_PFLAG_INITIALIZING	= (1 << 7), /* being initialized, don't touch */
267 	ATA_PFLAG_RESETTING	= (1 << 8), /* reset in progress */
268 	ATA_PFLAG_UNLOADING	= (1 << 9), /* driver is being unloaded */
269 	ATA_PFLAG_UNLOADED	= (1 << 10), /* driver is unloaded */
270 
271 	ATA_PFLAG_RESUMING	= (1 << 16),  /* port is being resumed */
272 	ATA_PFLAG_SUSPENDED	= (1 << 17), /* port is suspended (power) */
273 	ATA_PFLAG_PM_PENDING	= (1 << 18), /* PM operation pending */
274 	ATA_PFLAG_INIT_GTM_VALID = (1 << 19), /* initial gtm data valid */
275 
276 	ATA_PFLAG_PIO32		= (1 << 20),  /* 32bit PIO */
277 	ATA_PFLAG_PIO32CHANGE	= (1 << 21),  /* 32bit PIO can be turned on/off */
278 	ATA_PFLAG_EXTERNAL	= (1 << 22),  /* eSATA/external port */
279 
280 	/* struct ata_queued_cmd flags */
281 	ATA_QCFLAG_ACTIVE	= (1 << 0), /* cmd not yet ack'd to scsi lyer */
282 	ATA_QCFLAG_DMAMAP	= (1 << 1), /* SG table is DMA mapped */
283 	ATA_QCFLAG_RTF_FILLED	= (1 << 2), /* result TF has been filled */
284 	ATA_QCFLAG_IO		= (1 << 3), /* standard IO command */
285 	ATA_QCFLAG_RESULT_TF	= (1 << 4), /* result TF requested */
286 	ATA_QCFLAG_CLEAR_EXCL	= (1 << 5), /* clear excl_link on completion */
287 	ATA_QCFLAG_QUIET	= (1 << 6), /* don't report device error */
288 	ATA_QCFLAG_RETRY	= (1 << 7), /* retry after failure */
289 	ATA_QCFLAG_HAS_CDL	= (1 << 8), /* qc has CDL a descriptor set */
290 
291 	ATA_QCFLAG_EH		= (1 << 16), /* cmd aborted and owned by EH */
292 	ATA_QCFLAG_SENSE_VALID	= (1 << 17), /* sense data valid */
293 	ATA_QCFLAG_EH_SCHEDULED = (1 << 18), /* EH scheduled (obsolete) */
294 	ATA_QCFLAG_EH_SUCCESS_CMD = (1 << 19), /* EH should fetch sense for this successful cmd */
295 
296 	/* host set flags */
297 	ATA_HOST_SIMPLEX	= (1 << 0),	/* Host is simplex, one DMA channel per host only */
298 	ATA_HOST_STARTED	= (1 << 1),	/* Host started */
299 	ATA_HOST_PARALLEL_SCAN	= (1 << 2),	/* Ports on this host can be scanned in parallel */
300 	ATA_HOST_IGNORE_ATA	= (1 << 3),	/* Ignore ATA devices on this host. */
301 
302 	ATA_HOST_NO_PART	= (1 << 4), /* Host does not support partial */
303 	ATA_HOST_NO_SSC		= (1 << 5), /* Host does not support slumber */
304 	ATA_HOST_NO_DEVSLP	= (1 << 6), /* Host does not support devslp */
305 
306 	/* bits 24:31 of host->flags are reserved for LLD specific flags */
307 
308 	/* Various lengths of time */
309 	ATA_TMOUT_INTERNAL_QUICK = 5000,
310 	ATA_TMOUT_MAX_PARK	= 30000,
311 
312 	/*
313 	 * GoVault needs 2s and iVDR disk HHD424020F7SV00 800ms.  2s
314 	 * is too much without parallel probing.  Use 2s if parallel
315 	 * probing is available, 800ms otherwise.
316 	 */
317 	ATA_TMOUT_FF_WAIT_LONG	=  2000,
318 	ATA_TMOUT_FF_WAIT	=   800,
319 
320 	/* Spec mandates to wait for ">= 2ms" before checking status
321 	 * after reset.  We wait 150ms, because that was the magic
322 	 * delay used for ATAPI devices in Hale Landis's ATADRVR, for
323 	 * the period of time between when the ATA command register is
324 	 * written, and then status is checked.  Because waiting for
325 	 * "a while" before checking status is fine, post SRST, we
326 	 * perform this magic delay here as well.
327 	 *
328 	 * Old drivers/ide uses the 2mS rule and then waits for ready.
329 	 */
330 	ATA_WAIT_AFTER_RESET	=  150,
331 
332 	/* If PMP is supported, we have to do follow-up SRST.  As some
333 	 * PMPs don't send D2H Reg FIS after hardreset, LLDs are
334 	 * advised to wait only for the following duration before
335 	 * doing SRST.
336 	 */
337 	ATA_TMOUT_PMP_SRST_WAIT	= 10000,
338 
339 	/* When the LPM policy is set to ATA_LPM_MAX_POWER, there might
340 	 * be a spurious PHY event, so ignore the first PHY event that
341 	 * occurs within 10s after the policy change.
342 	 */
343 	ATA_TMOUT_SPURIOUS_PHY	= 10000,
344 
345 	/* ATA bus states */
346 	BUS_UNKNOWN		= 0,
347 	BUS_DMA			= 1,
348 	BUS_IDLE		= 2,
349 	BUS_NOINTR		= 3,
350 	BUS_NODATA		= 4,
351 	BUS_TIMER		= 5,
352 	BUS_PIO			= 6,
353 	BUS_EDD			= 7,
354 	BUS_IDENTIFY		= 8,
355 	BUS_PACKET		= 9,
356 
357 	/* SATA port states */
358 	PORT_UNKNOWN		= 0,
359 	PORT_ENABLED		= 1,
360 	PORT_DISABLED		= 2,
361 
362 	/* encoding various smaller bitmaps into a single
363 	 * unsigned int bitmap
364 	 */
365 	ATA_NR_PIO_MODES	= 7,
366 	ATA_NR_MWDMA_MODES	= 5,
367 	ATA_NR_UDMA_MODES	= 8,
368 
369 	ATA_SHIFT_PIO		= 0,
370 	ATA_SHIFT_MWDMA		= ATA_SHIFT_PIO + ATA_NR_PIO_MODES,
371 	ATA_SHIFT_UDMA		= ATA_SHIFT_MWDMA + ATA_NR_MWDMA_MODES,
372 	ATA_SHIFT_PRIO		= 6,
373 
374 	ATA_PRIO_HIGH		= 2,
375 	/* size of buffer to pad xfers ending on unaligned boundaries */
376 	ATA_DMA_PAD_SZ		= 4,
377 
378 	/* ering size */
379 	ATA_ERING_SIZE		= 32,
380 
381 	/* return values for ->qc_defer */
382 	ATA_DEFER_LINK		= 1,
383 	ATA_DEFER_PORT		= 2,
384 	ATA_DEFER_LINK_EXCL	= 3,
385 
386 	/* desc_len for ata_eh_info and context */
387 	ATA_EH_DESC_LEN		= 80,
388 
389 	/* reset / recovery action types */
390 	ATA_EH_REVALIDATE	= (1 << 0),
391 	ATA_EH_SOFTRESET	= (1 << 1), /* meaningful only in ->prereset */
392 	ATA_EH_HARDRESET	= (1 << 2), /* meaningful only in ->prereset */
393 	ATA_EH_RESET		= ATA_EH_SOFTRESET | ATA_EH_HARDRESET,
394 	ATA_EH_ENABLE_LINK	= (1 << 3),
395 	ATA_EH_PARK		= (1 << 5), /* unload heads and stop I/O */
396 	ATA_EH_GET_SUCCESS_SENSE = (1 << 6), /* Get sense data for successful cmd */
397 	ATA_EH_SET_ACTIVE	= (1 << 7), /* Set a device to active power mode */
398 
399 	ATA_EH_PERDEV_MASK	= ATA_EH_REVALIDATE | ATA_EH_PARK |
400 				  ATA_EH_GET_SUCCESS_SENSE | ATA_EH_SET_ACTIVE,
401 	ATA_EH_ALL_ACTIONS	= ATA_EH_REVALIDATE | ATA_EH_RESET |
402 				  ATA_EH_ENABLE_LINK,
403 
404 	/* ata_eh_info->flags */
405 	ATA_EHI_HOTPLUGGED	= (1 << 0),  /* could have been hotplugged */
406 	ATA_EHI_NO_AUTOPSY	= (1 << 2),  /* no autopsy */
407 	ATA_EHI_QUIET		= (1 << 3),  /* be quiet */
408 	ATA_EHI_NO_RECOVERY	= (1 << 4),  /* no recovery */
409 
410 	ATA_EHI_DID_SOFTRESET	= (1 << 16), /* already soft-reset this port */
411 	ATA_EHI_DID_HARDRESET	= (1 << 17), /* already soft-reset this port */
412 	ATA_EHI_PRINTINFO	= (1 << 18), /* print configuration info */
413 	ATA_EHI_SETMODE		= (1 << 19), /* configure transfer mode */
414 	ATA_EHI_POST_SETMODE	= (1 << 20), /* revalidating after setmode */
415 	ATA_EHI_DID_PRINT_QUIRKS = (1 << 21), /* already printed quirks info */
416 
417 	ATA_EHI_DID_RESET	= ATA_EHI_DID_SOFTRESET | ATA_EHI_DID_HARDRESET,
418 
419 	/* mask of flags to transfer *to* the slave link */
420 	ATA_EHI_TO_SLAVE_MASK	= ATA_EHI_NO_AUTOPSY | ATA_EHI_QUIET,
421 
422 	/* max tries if error condition is still set after ->error_handler */
423 	ATA_EH_MAX_TRIES	= 5,
424 
425 	/* sometimes resuming a link requires several retries */
426 	ATA_LINK_RESUME_TRIES	= 5,
427 
428 	/* how hard are we gonna try to probe/recover devices */
429 	ATA_EH_DEV_TRIES	= 3,
430 	ATA_EH_PMP_TRIES	= 5,
431 	ATA_EH_PMP_LINK_TRIES	= 3,
432 
433 	SATA_PMP_RW_TIMEOUT	= 3000,		/* PMP read/write timeout */
434 
435 	/* This should match the actual table size of
436 	 * ata_eh_cmd_timeout_table in libata-eh.c.
437 	 */
438 	ATA_EH_CMD_TIMEOUT_TABLE_SIZE = 9,
439 
440 	/* User visible DMA mask for DMA control. DO NOT renumber. */
441 	ATA_DMA_MASK_ATA	= (1 << 0),	/* DMA on ATA Disk */
442 	ATA_DMA_MASK_ATAPI	= (1 << 1),	/* DMA on ATAPI */
443 	ATA_DMA_MASK_CFA	= (1 << 2),	/* DMA on CF Card */
444 
445 	/* ATAPI command types */
446 	ATAPI_READ		= 0,		/* READs */
447 	ATAPI_WRITE		= 1,		/* WRITEs */
448 	ATAPI_READ_CD		= 2,		/* READ CD [MSF] */
449 	ATAPI_PASS_THRU		= 3,		/* SAT pass-thru */
450 	ATAPI_MISC		= 4,		/* the rest */
451 
452 	/* Timing constants */
453 	ATA_TIMING_SETUP	= (1 << 0),
454 	ATA_TIMING_ACT8B	= (1 << 1),
455 	ATA_TIMING_REC8B	= (1 << 2),
456 	ATA_TIMING_CYC8B	= (1 << 3),
457 	ATA_TIMING_8BIT		= ATA_TIMING_ACT8B | ATA_TIMING_REC8B |
458 				  ATA_TIMING_CYC8B,
459 	ATA_TIMING_ACTIVE	= (1 << 4),
460 	ATA_TIMING_RECOVER	= (1 << 5),
461 	ATA_TIMING_DMACK_HOLD	= (1 << 6),
462 	ATA_TIMING_CYCLE	= (1 << 7),
463 	ATA_TIMING_UDMA		= (1 << 8),
464 	ATA_TIMING_ALL		= ATA_TIMING_SETUP | ATA_TIMING_ACT8B |
465 				  ATA_TIMING_REC8B | ATA_TIMING_CYC8B |
466 				  ATA_TIMING_ACTIVE | ATA_TIMING_RECOVER |
467 				  ATA_TIMING_DMACK_HOLD | ATA_TIMING_CYCLE |
468 				  ATA_TIMING_UDMA,
469 
470 	/* ACPI constants */
471 	ATA_ACPI_FILTER_SETXFER	= 1 << 0,
472 	ATA_ACPI_FILTER_LOCK	= 1 << 1,
473 	ATA_ACPI_FILTER_DIPM	= 1 << 2,
474 	ATA_ACPI_FILTER_FPDMA_OFFSET = 1 << 3,	/* FPDMA non-zero offset */
475 	ATA_ACPI_FILTER_FPDMA_AA = 1 << 4,	/* FPDMA auto activate */
476 
477 	ATA_ACPI_FILTER_DEFAULT	= ATA_ACPI_FILTER_SETXFER |
478 				  ATA_ACPI_FILTER_LOCK |
479 				  ATA_ACPI_FILTER_DIPM,
480 };
481 
482 enum ata_xfer_mask {
483 	ATA_MASK_PIO		= ((1U << ATA_NR_PIO_MODES) - 1) << ATA_SHIFT_PIO,
484 	ATA_MASK_MWDMA		= ((1U << ATA_NR_MWDMA_MODES) - 1) << ATA_SHIFT_MWDMA,
485 	ATA_MASK_UDMA		= ((1U << ATA_NR_UDMA_MODES) - 1) << ATA_SHIFT_UDMA,
486 };
487 
488 enum hsm_task_states {
489 	HSM_ST_IDLE,		/* no command on going */
490 	HSM_ST_FIRST,		/* (waiting the device to)
491 				   write CDB or first data block */
492 	HSM_ST,			/* (waiting the device to) transfer data */
493 	HSM_ST_LAST,		/* (waiting the device to) complete command */
494 	HSM_ST_ERR,		/* error */
495 };
496 
497 enum ata_completion_errors {
498 	AC_ERR_OK		= 0,	    /* no error */
499 	AC_ERR_DEV		= (1 << 0), /* device reported error */
500 	AC_ERR_HSM		= (1 << 1), /* host state machine violation */
501 	AC_ERR_TIMEOUT		= (1 << 2), /* timeout */
502 	AC_ERR_MEDIA		= (1 << 3), /* media error */
503 	AC_ERR_ATA_BUS		= (1 << 4), /* ATA bus error */
504 	AC_ERR_HOST_BUS		= (1 << 5), /* host bus error */
505 	AC_ERR_SYSTEM		= (1 << 6), /* system error */
506 	AC_ERR_INVALID		= (1 << 7), /* invalid argument */
507 	AC_ERR_OTHER		= (1 << 8), /* unknown */
508 	AC_ERR_NODEV_HINT	= (1 << 9), /* polling device detection hint */
509 	AC_ERR_NCQ		= (1 << 10), /* marker for offending NCQ qc */
510 };
511 
512 /*
513  * Link Power Management (LPM) policies.
514  *
515  * The default LPM policy to use for a device link is defined using these values
516  * with the CONFIG_SATA_MOBILE_LPM_POLICY config option and applied through the
517  * target_lpm_policy field of struct ata_port.
518  *
519  * If you alter this, you also need to alter the policy names used with the
520  * sysfs attribute link_power_management_policy defined in libata-sata.c.
521  */
522 enum ata_lpm_policy {
523 	/* Keep firmware settings */
524 	ATA_LPM_UNKNOWN,
525 	/* No power savings (maximum performance) */
526 	ATA_LPM_MAX_POWER,
527 	/* HIPM (Partial) */
528 	ATA_LPM_MED_POWER,
529 	/* HIPM (Partial) and DIPM (Partial and Slumber) */
530 	ATA_LPM_MED_POWER_WITH_DIPM,
531 	/* HIPM (Partial and DevSleep) and DIPM (Partial and Slumber) */
532 	ATA_LPM_MIN_POWER_WITH_PARTIAL,
533 	/* HIPM (Slumber and DevSleep) and DIPM (Partial and Slumber) */
534 	ATA_LPM_MIN_POWER,
535 };
536 
537 enum ata_lpm_hints {
538 	ATA_LPM_EMPTY		= (1 << 0), /* port empty/probing */
539 	ATA_LPM_HIPM		= (1 << 1), /* may use HIPM */
540 	ATA_LPM_WAKE_ONLY	= (1 << 2), /* only wake up link */
541 };
542 
543 /* forward declarations */
544 struct scsi_device;
545 struct ata_port_operations;
546 struct ata_port;
547 struct ata_link;
548 struct ata_queued_cmd;
549 
550 /* typedefs */
551 typedef void (*ata_qc_cb_t) (struct ata_queued_cmd *qc);
552 typedef int (*ata_prereset_fn_t)(struct ata_link *link, unsigned long deadline);
553 typedef int (*ata_reset_fn_t)(struct ata_link *link, unsigned int *classes,
554 			      unsigned long deadline);
555 typedef void (*ata_postreset_fn_t)(struct ata_link *link, unsigned int *classes);
556 
557 extern struct device_attribute dev_attr_unload_heads;
558 #ifdef CONFIG_SATA_HOST
559 extern struct device_attribute dev_attr_link_power_management_supported;
560 extern struct device_attribute dev_attr_link_power_management_policy;
561 extern struct device_attribute dev_attr_ncq_prio_supported;
562 extern struct device_attribute dev_attr_ncq_prio_enable;
563 extern struct device_attribute dev_attr_em_message_type;
564 extern struct device_attribute dev_attr_em_message;
565 extern struct device_attribute dev_attr_sw_activity;
566 #endif
567 
568 enum sw_activity {
569 	OFF,
570 	BLINK_ON,
571 	BLINK_OFF,
572 };
573 
574 struct ata_taskfile {
575 	unsigned long		flags;		/* ATA_TFLAG_xxx */
576 	u8			protocol;	/* ATA_PROT_xxx */
577 
578 	u8			ctl;		/* control reg */
579 
580 	u8			hob_feature;	/* additional data */
581 	u8			hob_nsect;	/* to support LBA48 */
582 	u8			hob_lbal;
583 	u8			hob_lbam;
584 	u8			hob_lbah;
585 
586 	union {
587 		u8		error;
588 		u8		feature;
589 	};
590 	u8			nsect;
591 	u8			lbal;
592 	u8			lbam;
593 	u8			lbah;
594 
595 	u8			device;
596 
597 	union {
598 		u8		status;
599 		u8		command;
600 	};
601 
602 	u32			auxiliary;	/* auxiliary field */
603 						/* from SATA 3.1 and */
604 						/* ATA-8 ACS-3 */
605 };
606 
607 #ifdef CONFIG_ATA_SFF
608 struct ata_ioports {
609 	void __iomem		*cmd_addr;
610 	void __iomem		*data_addr;
611 	void __iomem		*error_addr;
612 	void __iomem		*feature_addr;
613 	void __iomem		*nsect_addr;
614 	void __iomem		*lbal_addr;
615 	void __iomem		*lbam_addr;
616 	void __iomem		*lbah_addr;
617 	void __iomem		*device_addr;
618 	void __iomem		*status_addr;
619 	void __iomem		*command_addr;
620 	void __iomem		*altstatus_addr;
621 	void __iomem		*ctl_addr;
622 #ifdef CONFIG_ATA_BMDMA
623 	void __iomem		*bmdma_addr;
624 #endif /* CONFIG_ATA_BMDMA */
625 	void __iomem		*scr_addr;
626 };
627 #endif /* CONFIG_ATA_SFF */
628 
629 struct ata_host {
630 	spinlock_t		lock;
631 	struct device 		*dev;
632 	void __iomem * const	*iomap;
633 	unsigned int		n_ports;
634 	unsigned int		n_tags;			/* nr of NCQ tags */
635 	void			*private_data;
636 	struct ata_port_operations *ops;
637 	unsigned long		flags;
638 	struct kref		kref;
639 
640 	struct mutex		eh_mutex;
641 	struct task_struct	*eh_owner;
642 
643 	struct ata_port		*simplex_claimed;	/* channel owning the DMA */
644 	struct ata_port		*ports[];
645 };
646 
647 struct ata_queued_cmd {
648 	struct ata_port		*ap;
649 	struct ata_device	*dev;
650 
651 	struct scsi_cmnd	*scsicmd;
652 	void			(*scsidone)(struct scsi_cmnd *);
653 
654 	struct ata_taskfile	tf;
655 	u8			cdb[ATAPI_CDB_LEN];
656 
657 	unsigned long		flags;		/* ATA_QCFLAG_xxx */
658 	unsigned int		tag;		/* libata core tag */
659 	unsigned int		hw_tag;		/* driver tag */
660 	unsigned int		n_elem;
661 	unsigned int		orig_n_elem;
662 
663 	int			dma_dir;
664 
665 	unsigned int		sect_size;
666 
667 	unsigned int		nbytes;
668 	unsigned int		extrabytes;
669 	unsigned int		curbytes;
670 
671 	struct scatterlist	sgent;
672 
673 	struct scatterlist	*sg;
674 
675 	struct scatterlist	*cursg;
676 	unsigned int		cursg_ofs;
677 
678 	unsigned int		err_mask;
679 	struct ata_taskfile	result_tf;
680 	ata_qc_cb_t		complete_fn;
681 
682 	void			*private_data;
683 	void			*lldd_task;
684 };
685 
686 struct ata_port_stats {
687 	unsigned long		unhandled_irq;
688 	unsigned long		idle_irq;
689 	unsigned long		rw_reqbuf;
690 };
691 
692 struct ata_ering_entry {
693 	unsigned int		eflags;
694 	unsigned int		err_mask;
695 	u64			timestamp;
696 };
697 
698 struct ata_ering {
699 	int			cursor;
700 	struct ata_ering_entry	ring[ATA_ERING_SIZE];
701 };
702 
703 struct ata_cpr {
704 	u8			num;
705 	u8			num_storage_elements;
706 	u64			start_lba;
707 	u64			num_lbas;
708 };
709 
710 struct ata_cpr_log {
711 	u8			nr_cpr;
712 	struct ata_cpr		cpr[] __counted_by(nr_cpr);
713 };
714 
715 struct ata_cdl {
716 	/*
717 	 * Buffer to cache the CDL log page 18h (command duration descriptors)
718 	 * for SCSI-ATA translation.
719 	 */
720 	u8			desc_log_buf[ATA_LOG_CDL_SIZE];
721 
722 	/*
723 	 * Buffer to handle reading the sense data for successful NCQ Commands
724 	 * log page for commands using a CDL with one of the limits policy set
725 	 * to 0xD (successful completion with sense data available bit set).
726 	 */
727 	u8			ncq_sense_log_buf[ATA_LOG_SENSE_NCQ_SIZE];
728 };
729 
730 struct ata_device {
731 	struct ata_link		*link;
732 	unsigned int		devno;		/* 0 or 1 */
733 	u64			quirks;		/* List of broken features */
734 	unsigned long		flags;		/* ATA_DFLAG_xxx */
735 	struct scsi_device	*sdev;		/* attached SCSI device */
736 	void			*private_data;
737 #ifdef CONFIG_ATA_ACPI
738 	union acpi_object	*gtf_cache;
739 	unsigned int		gtf_filter;
740 #endif
741 #ifdef CONFIG_SATA_ZPODD
742 	void			*zpodd;
743 #endif
744 	struct device		tdev;
745 	/* n_sector is CLEAR_BEGIN, read comment above CLEAR_BEGIN */
746 	u64			n_sectors;	/* size of device, if ATA */
747 	u64			n_native_sectors; /* native size, if ATA */
748 	unsigned int		class;		/* ATA_DEV_xxx */
749 	unsigned long		unpark_deadline;
750 
751 	u8			pio_mode;
752 	u8			dma_mode;
753 	u8			xfer_mode;
754 	unsigned int		xfer_shift;	/* ATA_SHIFT_xxx */
755 
756 	unsigned int		multi_count;	/* sectors count for
757 						   READ/WRITE MULTIPLE */
758 	unsigned int		max_sectors;	/* per-device max sectors */
759 	unsigned int		cdb_len;
760 
761 	/* per-dev xfer mask */
762 	unsigned int		pio_mask;
763 	unsigned int		mwdma_mask;
764 	unsigned int		udma_mask;
765 
766 	/* for CHS addressing */
767 	u16			cylinders;	/* Number of cylinders */
768 	u16			heads;		/* Number of heads */
769 	u16			sectors;	/* Number of sectors per track */
770 
771 	union {
772 		u16		id[ATA_ID_WORDS]; /* IDENTIFY xxx DEVICE data */
773 		u32		gscr[SATA_PMP_GSCR_DWORDS]; /* PMP GSCR block */
774 	} ____cacheline_aligned;
775 
776 	/* General Purpose Log Directory log page */
777 	u8			gp_log_dir[ATA_SECT_SIZE] ____cacheline_aligned;
778 
779 	/* DEVSLP Timing Variables from Identify Device Data Log */
780 	u8			devslp_timing[ATA_LOG_DEVSLP_SIZE];
781 
782 	/* NCQ send and receive log subcommand support */
783 	u8			ncq_send_recv_cmds[ATA_LOG_NCQ_SEND_RECV_SIZE];
784 	u8			ncq_non_data_cmds[ATA_LOG_NCQ_NON_DATA_SIZE];
785 
786 	/* ZAC zone configuration */
787 	u32			zac_zoned_cap;
788 	u32			zac_zones_optimal_open;
789 	u32			zac_zones_optimal_nonseq;
790 	u32			zac_zones_max_open;
791 
792 	/* Concurrent positioning ranges */
793 	struct ata_cpr_log	*cpr_log;
794 
795 	/* Command Duration Limits support */
796 	struct ata_cdl		*cdl;
797 
798 	/* error history */
799 	int			spdn_cnt;
800 	/* ering is CLEAR_END, read comment above CLEAR_END */
801 	struct ata_ering	ering;
802 
803 	/* For EH */
804 	u8			sector_buf[ATA_SECT_SIZE] ____cacheline_aligned;
805 };
806 
807 /* Fields between ATA_DEVICE_CLEAR_BEGIN and ATA_DEVICE_CLEAR_END are
808  * cleared to zero on ata_dev_init().
809  */
810 #define ATA_DEVICE_CLEAR_BEGIN		offsetof(struct ata_device, n_sectors)
811 #define ATA_DEVICE_CLEAR_END		offsetof(struct ata_device, ering)
812 
813 struct ata_eh_info {
814 	struct ata_device	*dev;		/* offending device */
815 	u32			serror;		/* SError from LLDD */
816 	unsigned int		err_mask;	/* port-wide err_mask */
817 	unsigned int		action;		/* ATA_EH_* action mask */
818 	unsigned int		dev_action[ATA_MAX_DEVICES]; /* dev EH action */
819 	unsigned int		flags;		/* ATA_EHI_* flags */
820 
821 	unsigned int		probe_mask;
822 
823 	char			desc[ATA_EH_DESC_LEN];
824 	int			desc_len;
825 };
826 
827 struct ata_eh_context {
828 	struct ata_eh_info	i;
829 	int			tries[ATA_MAX_DEVICES];
830 	int			cmd_timeout_idx[ATA_MAX_DEVICES]
831 					       [ATA_EH_CMD_TIMEOUT_TABLE_SIZE];
832 	unsigned int		classes[ATA_MAX_DEVICES];
833 	unsigned int		did_probe_mask;
834 	unsigned int		unloaded_mask;
835 	unsigned int		saved_ncq_enabled;
836 	u8			saved_xfer_mode[ATA_MAX_DEVICES];
837 	/* timestamp for the last reset attempt or success */
838 	unsigned long		last_reset;
839 };
840 
841 struct ata_acpi_drive
842 {
843 	u32 pio;
844 	u32 dma;
845 } __packed;
846 
847 struct ata_acpi_gtm {
848 	struct ata_acpi_drive drive[2];
849 	u32 flags;
850 } __packed;
851 
852 struct ata_link {
853 	struct ata_port		*ap;
854 	int			pmp;		/* port multiplier port # */
855 
856 	struct device		tdev;
857 	unsigned int		active_tag;	/* active tag on this link */
858 	u32			sactive;	/* active NCQ commands */
859 
860 	unsigned int		flags;		/* ATA_LFLAG_xxx */
861 
862 	u32			saved_scontrol;	/* SControl on probe */
863 	unsigned int		hw_sata_spd_limit;
864 	unsigned int		sata_spd_limit;
865 	unsigned int		sata_spd;	/* current SATA PHY speed */
866 	enum ata_lpm_policy	lpm_policy;
867 
868 	struct work_struct	deferred_qc_work;
869 	struct ata_queued_cmd	*deferred_qc;
870 
871 	/* record runtime error info, protected by host_set lock */
872 	struct ata_eh_info	eh_info;
873 	/* EH context */
874 	struct ata_eh_context	eh_context;
875 
876 	struct ata_device	device[ATA_MAX_DEVICES];
877 
878 	unsigned long		last_lpm_change; /* when last LPM change happened */
879 };
880 #define ATA_LINK_CLEAR_BEGIN		offsetof(struct ata_link, active_tag)
881 #define ATA_LINK_CLEAR_END		offsetof(struct ata_link, device[0])
882 
883 struct ata_port {
884 	struct Scsi_Host	*scsi_host; /* our co-allocated scsi host */
885 	struct ata_port_operations *ops;
886 	spinlock_t		*lock;
887 	/* Flags owned by the EH context. Only EH should touch these once the
888 	   port is active */
889 	unsigned long		flags;	/* ATA_FLAG_xxx */
890 	/* Flags that change dynamically, protected by ap->lock */
891 	unsigned int		pflags; /* ATA_PFLAG_xxx */
892 	unsigned int		print_id; /* user visible unique port ID */
893 	unsigned int		port_no; /* 0 based port no. inside the host */
894 
895 #ifdef CONFIG_ATA_SFF
896 	struct ata_ioports	ioaddr;	/* ATA cmd/ctl/dma register blocks */
897 	u8			ctl;	/* cache of ATA control register */
898 	u8			last_ctl;	/* Cache last written value */
899 	struct ata_link*	sff_pio_task_link; /* link currently used */
900 	struct delayed_work	sff_pio_task;
901 #ifdef CONFIG_ATA_BMDMA
902 	struct ata_bmdma_prd	*bmdma_prd;	/* BMDMA SG list */
903 	dma_addr_t		bmdma_prd_dma;	/* and its DMA mapping */
904 #endif /* CONFIG_ATA_BMDMA */
905 #endif /* CONFIG_ATA_SFF */
906 
907 	unsigned int		pio_mask;
908 	unsigned int		mwdma_mask;
909 	unsigned int		udma_mask;
910 	unsigned int		cbl;	/* cable type; ATA_CBL_xxx */
911 
912 	struct ata_queued_cmd	qcmd[ATA_MAX_QUEUE + 1];
913 	u64			qc_active;
914 	int			nr_active_links; /* #links with active qcs */
915 
916 	struct ata_link		link;		/* host default link */
917 	struct ata_link		*slave_link;	/* see ata_slave_link_init() */
918 
919 	int			nr_pmp_links;	/* nr of available PMP links */
920 	struct ata_link		*pmp_link;	/* array of PMP links */
921 	struct ata_link		*excl_link;	/* for PMP qc exclusion */
922 
923 	struct ata_port_stats	stats;
924 	struct ata_host		*host;
925 	struct device 		*dev;
926 	struct device		tdev;
927 
928 	struct mutex		scsi_scan_mutex;
929 	struct delayed_work	hotplug_task;
930 	struct delayed_work	scsi_rescan_task;
931 
932 	unsigned int		hsm_task_state;
933 
934 	struct list_head	eh_done_q;
935 	wait_queue_head_t	eh_wait_q;
936 	int			eh_tries;
937 	struct completion	park_req_pending;
938 
939 	pm_message_t		pm_mesg;
940 	enum ata_lpm_policy	target_lpm_policy;
941 
942 	struct timer_list	fastdrain_timer;
943 	unsigned int		fastdrain_cnt;
944 
945 	async_cookie_t		cookie;
946 
947 	int			em_message_type;
948 	void			*private_data;
949 
950 #ifdef CONFIG_ATA_ACPI
951 	struct ata_acpi_gtm	__acpi_init_gtm; /* use ata_acpi_init_gtm() */
952 #endif
953 };
954 
955 /* The following initializer overrides a method to NULL whether one of
956  * its parent has the method defined or not.  This is equivalent to
957  * ERR_PTR(-ENOENT).  Unfortunately, ERR_PTR doesn't render a constant
958  * expression and thus can't be used as an initializer.
959  */
960 #define ATA_OP_NULL		(void *)(unsigned long)(-ENOENT)
961 
962 struct ata_reset_operations {
963 	ata_prereset_fn_t	prereset;
964 	ata_reset_fn_t		softreset;
965 	ata_reset_fn_t		hardreset;
966 	ata_postreset_fn_t	postreset;
967 };
968 
969 struct ata_port_operations {
970 	/*
971 	 * Command execution
972 	 */
973 	int (*qc_defer)(struct ata_queued_cmd *qc);
974 	int (*check_atapi_dma)(struct ata_queued_cmd *qc);
975 	enum ata_completion_errors (*qc_prep)(struct ata_queued_cmd *qc);
976 	unsigned int (*qc_issue)(struct ata_queued_cmd *qc);
977 	void (*qc_fill_rtf)(struct ata_queued_cmd *qc);
978 	void (*qc_ncq_fill_rtf)(struct ata_port *ap, u64 done_mask);
979 
980 	/*
981 	 * Configuration and exception handling
982 	 */
983 	int  (*cable_detect)(struct ata_port *ap);
984 	unsigned int (*mode_filter)(struct ata_device *dev, unsigned int xfer_mask);
985 	void (*set_piomode)(struct ata_port *ap, struct ata_device *dev);
986 	void (*set_dmamode)(struct ata_port *ap, struct ata_device *dev);
987 	int  (*set_mode)(struct ata_link *link, struct ata_device **r_failed_dev);
988 	unsigned int (*read_id)(struct ata_device *dev, struct ata_taskfile *tf,
989 				__le16 *id);
990 
991 	void (*dev_config)(struct ata_device *dev);
992 
993 	void (*freeze)(struct ata_port *ap);
994 	void (*thaw)(struct ata_port *ap);
995 	struct ata_reset_operations reset;
996 	struct ata_reset_operations pmp_reset;
997 	void (*error_handler)(struct ata_port *ap)
998 		__must_hold(&ap->host->eh_mutex);
999 	void (*lost_interrupt)(struct ata_port *ap);
1000 	void (*post_internal_cmd)(struct ata_queued_cmd *qc);
1001 	void (*sched_eh)(struct ata_port *ap);
1002 	void (*end_eh)(struct ata_port *ap);
1003 
1004 	/*
1005 	 * Optional features
1006 	 */
1007 	int  (*scr_read)(struct ata_link *link, unsigned int sc_reg, u32 *val);
1008 	int  (*scr_write)(struct ata_link *link, unsigned int sc_reg, u32 val);
1009 	void (*pmp_attach)(struct ata_port *ap);
1010 	void (*pmp_detach)(struct ata_port *ap);
1011 	int  (*set_lpm)(struct ata_link *link, enum ata_lpm_policy policy,
1012 			unsigned hints);
1013 
1014 	/*
1015 	 * Start, stop, suspend and resume
1016 	 */
1017 	int  (*port_suspend)(struct ata_port *ap, pm_message_t mesg);
1018 	int  (*port_resume)(struct ata_port *ap);
1019 	int  (*port_start)(struct ata_port *ap);
1020 	void (*port_stop)(struct ata_port *ap);
1021 	void (*host_stop)(struct ata_host *host);
1022 
1023 #ifdef CONFIG_ATA_SFF
1024 	/*
1025 	 * SFF / taskfile oriented ops
1026 	 */
1027 	void (*sff_dev_select)(struct ata_port *ap, unsigned int device);
1028 	void (*sff_set_devctl)(struct ata_port *ap, u8 ctl);
1029 	u8   (*sff_check_status)(struct ata_port *ap);
1030 	u8   (*sff_check_altstatus)(struct ata_port *ap);
1031 	void (*sff_tf_load)(struct ata_port *ap, const struct ata_taskfile *tf);
1032 	void (*sff_tf_read)(struct ata_port *ap, struct ata_taskfile *tf);
1033 	void (*sff_exec_command)(struct ata_port *ap,
1034 				 const struct ata_taskfile *tf);
1035 	unsigned int (*sff_data_xfer)(struct ata_queued_cmd *qc,
1036 			unsigned char *buf, unsigned int buflen, int rw);
1037 	void (*sff_irq_on)(struct ata_port *);
1038 	bool (*sff_irq_check)(struct ata_port *);
1039 	void (*sff_irq_clear)(struct ata_port *);
1040 	void (*sff_drain_fifo)(struct ata_queued_cmd *qc);
1041 
1042 #ifdef CONFIG_ATA_BMDMA
1043 	void (*bmdma_setup)(struct ata_queued_cmd *qc);
1044 	void (*bmdma_start)(struct ata_queued_cmd *qc);
1045 	void (*bmdma_stop)(struct ata_queued_cmd *qc);
1046 	u8   (*bmdma_status)(struct ata_port *ap);
1047 #endif /* CONFIG_ATA_BMDMA */
1048 #endif /* CONFIG_ATA_SFF */
1049 
1050 	ssize_t (*em_show)(struct ata_port *ap, char *buf);
1051 	ssize_t (*em_store)(struct ata_port *ap, const char *message,
1052 			    size_t size);
1053 	ssize_t (*sw_activity_show)(struct ata_device *dev, char *buf);
1054 	ssize_t (*sw_activity_store)(struct ata_device *dev,
1055 				     enum sw_activity val);
1056 	ssize_t (*transmit_led_message)(struct ata_port *ap, u32 state,
1057 					ssize_t size);
1058 
1059 	/*
1060 	 * ->inherits must be the last field and all the preceding
1061 	 * fields must be pointers.
1062 	 */
1063 	const struct ata_port_operations	*inherits;
1064 };
1065 
1066 struct ata_port_info {
1067 	unsigned long		flags;
1068 	unsigned long		link_flags;
1069 	unsigned int		pio_mask;
1070 	unsigned int		mwdma_mask;
1071 	unsigned int		udma_mask;
1072 	struct ata_port_operations *port_ops;
1073 	void 			*private_data;
1074 };
1075 
1076 struct ata_timing {
1077 	unsigned short mode;		/* ATA mode */
1078 	unsigned short setup;		/* t1 */
1079 	unsigned short act8b;		/* t2 for 8-bit I/O */
1080 	unsigned short rec8b;		/* t2i for 8-bit I/O */
1081 	unsigned short cyc8b;		/* t0 for 8-bit I/O */
1082 	unsigned short active;		/* t2 or tD */
1083 	unsigned short recover;		/* t2i or tK */
1084 	unsigned short dmack_hold;	/* tj */
1085 	unsigned short cycle;		/* t0 */
1086 	unsigned short udma;		/* t2CYCTYP/2 */
1087 };
1088 
1089 /*
1090  * Core layer - drivers/ata/libata-core.c
1091  */
1092 extern struct ata_port_operations ata_dummy_port_ops;
1093 extern const struct ata_port_info ata_dummy_port_info;
1094 
ata_is_atapi(u8 prot)1095 static inline bool ata_is_atapi(u8 prot)
1096 {
1097 	return prot & ATA_PROT_FLAG_ATAPI;
1098 }
1099 
ata_is_pio(u8 prot)1100 static inline bool ata_is_pio(u8 prot)
1101 {
1102 	return prot & ATA_PROT_FLAG_PIO;
1103 }
1104 
ata_is_dma(u8 prot)1105 static inline bool ata_is_dma(u8 prot)
1106 {
1107 	return prot & ATA_PROT_FLAG_DMA;
1108 }
1109 
ata_is_ncq(u8 prot)1110 static inline bool ata_is_ncq(u8 prot)
1111 {
1112 	return prot & ATA_PROT_FLAG_NCQ;
1113 }
1114 
ata_is_data(u8 prot)1115 static inline bool ata_is_data(u8 prot)
1116 {
1117 	return prot & (ATA_PROT_FLAG_PIO | ATA_PROT_FLAG_DMA);
1118 }
1119 
is_multi_taskfile(struct ata_taskfile * tf)1120 static inline int is_multi_taskfile(struct ata_taskfile *tf)
1121 {
1122 	return (tf->command == ATA_CMD_READ_MULTI) ||
1123 	       (tf->command == ATA_CMD_WRITE_MULTI) ||
1124 	       (tf->command == ATA_CMD_READ_MULTI_EXT) ||
1125 	       (tf->command == ATA_CMD_WRITE_MULTI_EXT) ||
1126 	       (tf->command == ATA_CMD_WRITE_MULTI_FUA_EXT);
1127 }
1128 
ata_port_is_dummy(struct ata_port * ap)1129 static inline int ata_port_is_dummy(struct ata_port *ap)
1130 {
1131 	return ap->ops == &ata_dummy_port_ops;
1132 }
1133 
ata_port_is_frozen(const struct ata_port * ap)1134 static inline bool ata_port_is_frozen(const struct ata_port *ap)
1135 {
1136 	return ap->pflags & ATA_PFLAG_FROZEN;
1137 }
1138 
1139 extern int ata_std_prereset(struct ata_link *link, unsigned long deadline);
1140 extern int ata_wait_after_reset(struct ata_link *link, unsigned long deadline,
1141 				int (*check_ready)(struct ata_link *link));
1142 extern void ata_std_postreset(struct ata_link *link, unsigned int *classes);
1143 
1144 extern struct ata_host *ata_host_alloc(struct device *dev, int n_ports);
1145 extern struct ata_host *ata_host_alloc_pinfo(struct device *dev,
1146 			const struct ata_port_info * const * ppi, int n_ports);
1147 extern void ata_host_get(struct ata_host *host);
1148 extern void ata_host_put(struct ata_host *host);
1149 extern int ata_host_start(struct ata_host *host);
1150 extern int ata_host_register(struct ata_host *host,
1151 			     const struct scsi_host_template *sht);
1152 extern int ata_host_activate(struct ata_host *host, int irq,
1153 			     irq_handler_t irq_handler, unsigned long irq_flags,
1154 			     const struct scsi_host_template *sht);
1155 extern void ata_host_detach(struct ata_host *host);
1156 extern void ata_host_init(struct ata_host *, struct device *, struct ata_port_operations *);
1157 extern int ata_scsi_ioctl(struct scsi_device *dev, unsigned int cmd,
1158 			  void __user *arg);
1159 #ifdef CONFIG_COMPAT
1160 #define ATA_SCSI_COMPAT_IOCTL .compat_ioctl = ata_scsi_ioctl,
1161 #else
1162 #define ATA_SCSI_COMPAT_IOCTL /* empty */
1163 #endif
1164 extern enum scsi_qc_status ata_scsi_queuecmd(struct Scsi_Host *h,
1165 					     struct scsi_cmnd *cmd);
1166 enum scsi_timeout_action ata_scsi_retry_deferred_qc(struct ata_port *ap,
1167 						    struct scsi_cmnd *scmd);
1168 enum scsi_timeout_action ata_scsi_eh_timed_out(struct scsi_cmnd *cmd);
1169 #if IS_REACHABLE(CONFIG_ATA)
1170 bool ata_scsi_dma_need_drain(struct request *rq);
1171 #else
1172 #define ata_scsi_dma_need_drain NULL
1173 #endif
1174 extern int ata_sas_scsi_ioctl(struct ata_port *ap, struct scsi_device *dev,
1175 			    unsigned int cmd, void __user *arg);
1176 extern bool ata_link_online(struct ata_link *link);
1177 extern bool ata_link_offline(struct ata_link *link);
1178 #ifdef CONFIG_PM
1179 extern void ata_host_suspend(struct ata_host *host, pm_message_t mesg);
1180 extern void ata_host_resume(struct ata_host *host);
1181 extern void ata_sas_port_suspend(struct ata_port *ap);
1182 extern void ata_sas_port_resume(struct ata_port *ap);
1183 #else
ata_sas_port_suspend(struct ata_port * ap)1184 static inline void ata_sas_port_suspend(struct ata_port *ap)
1185 {
1186 }
ata_sas_port_resume(struct ata_port * ap)1187 static inline void ata_sas_port_resume(struct ata_port *ap)
1188 {
1189 }
1190 #endif
1191 extern int ata_ratelimit(void);
1192 extern void ata_msleep(struct ata_port *ap, unsigned int msecs);
1193 extern u32 ata_wait_register(struct ata_port *ap, void __iomem *reg, u32 mask,
1194 			     u32 val, unsigned int interval, unsigned int timeout);
1195 extern int atapi_cmd_type(u8 opcode);
1196 extern unsigned int ata_pack_xfermask(unsigned int pio_mask,
1197 				      unsigned int mwdma_mask,
1198 				      unsigned int udma_mask);
1199 extern void ata_unpack_xfermask(unsigned int xfer_mask,
1200 				unsigned int *pio_mask,
1201 				unsigned int *mwdma_mask,
1202 				unsigned int *udma_mask);
1203 extern u8 ata_xfer_mask2mode(unsigned int xfer_mask);
1204 extern unsigned int ata_xfer_mode2mask(u8 xfer_mode);
1205 extern int ata_xfer_mode2shift(u8 xfer_mode);
1206 extern const char *ata_mode_string(unsigned int xfer_mask);
1207 extern unsigned int ata_id_xfermask(const u16 *id);
1208 extern int ata_std_qc_defer(struct ata_queued_cmd *qc);
1209 extern void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
1210 		 unsigned int n_elem);
1211 extern unsigned int ata_dev_classify(const struct ata_taskfile *tf);
1212 extern unsigned int ata_port_classify(struct ata_port *ap,
1213 				      const struct ata_taskfile *tf);
1214 extern void ata_dev_disable(struct ata_device *adev);
1215 extern void ata_id_string(const u16 *id, unsigned char *s,
1216 			  unsigned int ofs, unsigned int len);
1217 extern void ata_id_c_string(const u16 *id, unsigned char *s,
1218 			    unsigned int ofs, unsigned int len);
1219 extern unsigned int ata_do_dev_read_id(struct ata_device *dev,
1220 				       struct ata_taskfile *tf, __le16 *id);
1221 extern void ata_qc_complete(struct ata_queued_cmd *qc);
1222 extern u64 ata_qc_get_active(struct ata_port *ap);
1223 extern int ata_std_bios_param(struct scsi_device *sdev,
1224 			      struct gendisk *unused,
1225 			      sector_t capacity, int geom[]);
1226 extern void ata_scsi_unlock_native_capacity(struct scsi_device *sdev);
1227 extern int ata_scsi_sdev_init(struct scsi_device *sdev);
1228 int ata_scsi_sdev_configure(struct scsi_device *sdev, struct queue_limits *lim);
1229 extern void ata_scsi_sdev_destroy(struct scsi_device *sdev);
1230 extern int ata_scsi_change_queue_depth(struct scsi_device *sdev,
1231 				       int queue_depth);
1232 extern int ata_change_queue_depth(struct ata_port *ap, struct scsi_device *sdev,
1233 				  int queue_depth);
1234 extern int ata_ncq_prio_supported(struct ata_port *ap, struct scsi_device *sdev,
1235 				  bool *supported);
1236 extern int ata_ncq_prio_enabled(struct ata_port *ap, struct scsi_device *sdev,
1237 				bool *enabled);
1238 extern int ata_ncq_prio_enable(struct ata_port *ap, struct scsi_device *sdev,
1239 			       bool enable);
1240 extern struct ata_device *ata_dev_pair(struct ata_device *adev);
1241 int ata_set_mode(struct ata_link *link, struct ata_device **r_failed_dev);
1242 extern void ata_scsi_port_error_handler(struct Scsi_Host *host, struct ata_port *ap);
1243 int ata_scsi_cmd_error_handler(struct Scsi_Host *host, struct ata_port *ap,
1244 			       struct list_head *eh_q);
1245 
1246 /*
1247  * SATA specific code - drivers/ata/libata-sata.c
1248  */
1249 #ifdef CONFIG_SATA_HOST
1250 extern const unsigned int sata_deb_timing_normal[];
1251 extern const unsigned int sata_deb_timing_hotplug[];
1252 extern const unsigned int sata_deb_timing_long[];
1253 
1254 static inline const unsigned int *
sata_ehc_deb_timing(struct ata_eh_context * ehc)1255 sata_ehc_deb_timing(struct ata_eh_context *ehc)
1256 {
1257 	if (ehc->i.flags & ATA_EHI_HOTPLUGGED)
1258 		return sata_deb_timing_hotplug;
1259 	else
1260 		return sata_deb_timing_normal;
1261 }
1262 
1263 extern int sata_scr_valid(struct ata_link *link);
1264 extern int sata_scr_read(struct ata_link *link, int reg, u32 *val);
1265 extern int sata_scr_write(struct ata_link *link, int reg, u32 val);
1266 extern int sata_scr_write_flush(struct ata_link *link, int reg, u32 val);
1267 extern int sata_set_spd(struct ata_link *link);
1268 int sata_std_hardreset(struct ata_link *link, unsigned int *class,
1269 		       unsigned long deadline);
1270 extern int sata_link_hardreset(struct ata_link *link,
1271 			const unsigned int *timing, unsigned long deadline,
1272 			bool *online, int (*check_ready)(struct ata_link *));
1273 extern int sata_link_resume(struct ata_link *link, const unsigned int *params,
1274 			    unsigned long deadline);
1275 extern void ata_eh_analyze_ncq_error(struct ata_link *link);
1276 #else
1277 static inline const unsigned int *
sata_ehc_deb_timing(struct ata_eh_context * ehc)1278 sata_ehc_deb_timing(struct ata_eh_context *ehc)
1279 {
1280 	return NULL;
1281 }
sata_scr_valid(struct ata_link * link)1282 static inline int sata_scr_valid(struct ata_link *link) { return 0; }
sata_scr_read(struct ata_link * link,int reg,u32 * val)1283 static inline int sata_scr_read(struct ata_link *link, int reg, u32 *val)
1284 {
1285 	return -EOPNOTSUPP;
1286 }
sata_scr_write(struct ata_link * link,int reg,u32 val)1287 static inline int sata_scr_write(struct ata_link *link, int reg, u32 val)
1288 {
1289 	return -EOPNOTSUPP;
1290 }
sata_scr_write_flush(struct ata_link * link,int reg,u32 val)1291 static inline int sata_scr_write_flush(struct ata_link *link, int reg, u32 val)
1292 {
1293 	return -EOPNOTSUPP;
1294 }
sata_set_spd(struct ata_link * link)1295 static inline int sata_set_spd(struct ata_link *link) { return -EOPNOTSUPP; }
sata_std_hardreset(struct ata_link * link,unsigned int * class,unsigned long deadline)1296 static inline int sata_std_hardreset(struct ata_link *link, unsigned int *class,
1297 				     unsigned long deadline)
1298 {
1299 	return -EOPNOTSUPP;
1300 }
sata_link_hardreset(struct ata_link * link,const unsigned int * timing,unsigned long deadline,bool * online,int (* check_ready)(struct ata_link *))1301 static inline int sata_link_hardreset(struct ata_link *link,
1302 				      const unsigned int *timing,
1303 				      unsigned long deadline,
1304 				      bool *online,
1305 				      int (*check_ready)(struct ata_link *))
1306 {
1307 	if (online)
1308 		*online = false;
1309 	return -EOPNOTSUPP;
1310 }
sata_link_resume(struct ata_link * link,const unsigned int * params,unsigned long deadline)1311 static inline int sata_link_resume(struct ata_link *link,
1312 				   const unsigned int *params,
1313 				   unsigned long deadline)
1314 {
1315 	return -EOPNOTSUPP;
1316 }
ata_eh_analyze_ncq_error(struct ata_link * link)1317 static inline void ata_eh_analyze_ncq_error(struct ata_link *link) { }
1318 #endif
1319 extern int sata_link_debounce(struct ata_link *link,
1320 			      const unsigned int *params, unsigned long deadline);
1321 extern int sata_link_scr_lpm(struct ata_link *link, enum ata_lpm_policy policy,
1322 			     bool spm_wakeup);
1323 extern int ata_slave_link_init(struct ata_port *ap);
1324 extern void ata_port_probe(struct ata_port *ap);
1325 extern struct ata_port *ata_port_alloc(struct ata_host *host);
1326 extern void ata_port_free(struct ata_port *ap);
1327 extern int ata_tport_add(struct device *parent, struct ata_port *ap);
1328 extern void ata_tport_delete(struct ata_port *ap);
1329 int ata_sas_sdev_configure(struct scsi_device *sdev, struct queue_limits *lim,
1330 			   struct ata_port *ap);
1331 extern int ata_sas_queuecmd(struct scsi_cmnd *cmd, struct ata_port *ap)
1332 	__must_hold(ap->lock);
1333 extern void ata_tf_to_fis(const struct ata_taskfile *tf,
1334 			  u8 pmp, int is_cmd, u8 *fis);
1335 extern void ata_tf_from_fis(const u8 *fis, struct ata_taskfile *tf);
1336 extern int ata_qc_complete_multiple(struct ata_port *ap, u64 qc_active);
1337 extern bool sata_lpm_ignore_phy_events(struct ata_link *link);
1338 extern int sata_async_notification(struct ata_port *ap);
1339 
1340 extern int ata_cable_40wire(struct ata_port *ap);
1341 extern int ata_cable_80wire(struct ata_port *ap);
1342 extern int ata_cable_sata(struct ata_port *ap);
1343 extern int ata_cable_ignore(struct ata_port *ap);
1344 extern int ata_cable_unknown(struct ata_port *ap);
1345 
1346 /* Timing helpers */
1347 extern unsigned int ata_pio_need_iordy(const struct ata_device *);
1348 extern u8 ata_timing_cycle2mode(unsigned int xfer_shift, int cycle);
1349 
1350 /* PCI */
1351 #ifdef CONFIG_PCI
1352 struct pci_dev;
1353 
1354 struct pci_bits {
1355 	unsigned int		reg;	/* PCI config register to read */
1356 	unsigned int		width;	/* 1 (8 bit), 2 (16 bit), 4 (32 bit) */
1357 	unsigned long		mask;
1358 	unsigned long		val;
1359 };
1360 
1361 extern int pci_test_config_bits(struct pci_dev *pdev, const struct pci_bits *bits);
1362 extern void ata_pci_shutdown_one(struct pci_dev *pdev);
1363 extern void ata_pci_remove_one(struct pci_dev *pdev);
1364 
1365 #ifdef CONFIG_PM
1366 extern void ata_pci_device_do_suspend(struct pci_dev *pdev, pm_message_t mesg);
1367 extern int __must_check ata_pci_device_do_resume(struct pci_dev *pdev);
1368 extern int ata_pci_device_suspend(struct pci_dev *pdev, pm_message_t mesg);
1369 extern int ata_pci_device_resume(struct pci_dev *pdev);
1370 #endif /* CONFIG_PM */
1371 #endif /* CONFIG_PCI */
1372 
1373 struct platform_device;
1374 
1375 extern void ata_platform_remove_one(struct platform_device *pdev);
1376 
1377 /*
1378  * ACPI - drivers/ata/libata-acpi.c
1379  */
1380 #ifdef CONFIG_ATA_ACPI
ata_acpi_init_gtm(struct ata_port * ap)1381 static inline const struct ata_acpi_gtm *ata_acpi_init_gtm(struct ata_port *ap)
1382 {
1383 	if (ap->pflags & ATA_PFLAG_INIT_GTM_VALID)
1384 		return &ap->__acpi_init_gtm;
1385 	return NULL;
1386 }
1387 int ata_acpi_stm(struct ata_port *ap, const struct ata_acpi_gtm *stm);
1388 int ata_acpi_gtm(struct ata_port *ap, struct ata_acpi_gtm *stm);
1389 unsigned int ata_acpi_gtm_xfermask(struct ata_device *dev,
1390 				   const struct ata_acpi_gtm *gtm);
1391 int ata_acpi_cbl_pata_type(struct ata_port *ap);
1392 #else
ata_acpi_init_gtm(struct ata_port * ap)1393 static inline const struct ata_acpi_gtm *ata_acpi_init_gtm(struct ata_port *ap)
1394 {
1395 	return NULL;
1396 }
1397 
ata_acpi_stm(const struct ata_port * ap,struct ata_acpi_gtm * stm)1398 static inline int ata_acpi_stm(const struct ata_port *ap,
1399 			       struct ata_acpi_gtm *stm)
1400 {
1401 	return -ENOSYS;
1402 }
1403 
ata_acpi_gtm(const struct ata_port * ap,struct ata_acpi_gtm * stm)1404 static inline int ata_acpi_gtm(const struct ata_port *ap,
1405 			       struct ata_acpi_gtm *stm)
1406 {
1407 	return -ENOSYS;
1408 }
1409 
ata_acpi_gtm_xfermask(struct ata_device * dev,const struct ata_acpi_gtm * gtm)1410 static inline unsigned int ata_acpi_gtm_xfermask(struct ata_device *dev,
1411 					const struct ata_acpi_gtm *gtm)
1412 {
1413 	return 0;
1414 }
1415 
ata_acpi_cbl_pata_type(struct ata_port * ap)1416 static inline int ata_acpi_cbl_pata_type(struct ata_port *ap)
1417 {
1418 	return ATA_CBL_PATA40;
1419 }
1420 #endif
1421 
1422 /*
1423  * EH - drivers/ata/libata-eh.c
1424  */
1425 extern void ata_port_schedule_eh(struct ata_port *ap);
1426 extern void ata_port_wait_eh(struct ata_port *ap);
1427 extern int ata_link_abort(struct ata_link *link);
1428 extern int ata_port_abort(struct ata_port *ap);
1429 extern int ata_port_freeze(struct ata_port *ap);
1430 
1431 extern void ata_eh_freeze_port(struct ata_port *ap);
1432 extern void ata_eh_thaw_port(struct ata_port *ap);
1433 
1434 extern void ata_std_error_handler(struct ata_port *ap)
1435 	__must_hold(&ap->host->eh_mutex);
1436 extern void ata_std_sched_eh(struct ata_port *ap);
1437 extern void ata_std_end_eh(struct ata_port *ap);
1438 extern int ata_link_nr_enabled(struct ata_link *link);
1439 
1440 /*
1441  * Base operations to inherit from and initializers for sht
1442  *
1443  * Operations
1444  *
1445  * base  : Common to all libata drivers.
1446  * sata  : SATA controllers w/ native interface.
1447  * pmp   : SATA controllers w/ PMP support.
1448  * sff   : SFF ATA controllers w/o BMDMA support.
1449  * bmdma : SFF ATA controllers w/ BMDMA support.
1450  *
1451  * sht initializers
1452  *
1453  * BASE  : Common to all libata drivers.  The user must set
1454  *	   sg_tablesize and dma_boundary.
1455  * PIO   : SFF ATA controllers w/ only PIO support.
1456  * BMDMA : SFF ATA controllers w/ BMDMA support.  sg_tablesize and
1457  *	   dma_boundary are set to BMDMA limits.
1458  * NCQ   : SATA controllers supporting NCQ.  The user must set
1459  *	   sg_tablesize, dma_boundary and can_queue.
1460  */
1461 extern const struct ata_port_operations ata_base_port_ops;
1462 extern const struct ata_port_operations sata_port_ops;
1463 extern const struct attribute_group *ata_common_sdev_groups[];
1464 
1465 /*
1466  * All sht initializers (BASE, PIO, BMDMA, NCQ) must be instantiated
1467  * by the edge drivers.  Because the 'module' field of sht must be the
1468  * edge driver's module reference, otherwise the driver can be unloaded
1469  * even if the scsi_device is being accessed.
1470  */
1471 #define __ATA_BASE_SHT(drv_name)				\
1472 	.module			= THIS_MODULE,			\
1473 	.name			= drv_name,			\
1474 	.ioctl			= ata_scsi_ioctl,		\
1475 	ATA_SCSI_COMPAT_IOCTL					\
1476 	.queuecommand		= ata_scsi_queuecmd,		\
1477 	.eh_timed_out		= ata_scsi_eh_timed_out,	\
1478 	.dma_need_drain		= ata_scsi_dma_need_drain,	\
1479 	.this_id		= ATA_SHT_THIS_ID,		\
1480 	.emulated		= ATA_SHT_EMULATED,		\
1481 	.proc_name		= drv_name,			\
1482 	.sdev_init		= ata_scsi_sdev_init,		\
1483 	.sdev_destroy		= ata_scsi_sdev_destroy,	\
1484 	.bios_param		= ata_std_bios_param,		\
1485 	.unlock_native_capacity	= ata_scsi_unlock_native_capacity,\
1486 	.max_sectors		= ATA_MAX_SECTORS_LBA48
1487 
1488 #define ATA_SUBBASE_SHT(drv_name)				\
1489 	__ATA_BASE_SHT(drv_name),				\
1490 	.can_queue		= ATA_DEF_QUEUE,		\
1491 	.tag_alloc_policy_rr	= true,				\
1492 	.sdev_configure		= ata_scsi_sdev_configure
1493 
1494 #define ATA_SUBBASE_SHT_QD(drv_name, drv_qd)			\
1495 	__ATA_BASE_SHT(drv_name),				\
1496 	.can_queue		= drv_qd,			\
1497 	.tag_alloc_policy_rr	= true,				\
1498 	.sdev_configure		= ata_scsi_sdev_configure
1499 
1500 #define ATA_BASE_SHT(drv_name)					\
1501 	ATA_SUBBASE_SHT(drv_name),				\
1502 	.sdev_groups		= ata_common_sdev_groups
1503 
1504 #ifdef CONFIG_SATA_HOST
1505 extern const struct attribute_group *ata_ncq_sdev_groups[];
1506 
1507 #define ATA_NCQ_SHT(drv_name)					\
1508 	ATA_SUBBASE_SHT(drv_name),				\
1509 	.sdev_groups		= ata_ncq_sdev_groups,		\
1510 	.change_queue_depth	= ata_scsi_change_queue_depth
1511 
1512 #define ATA_NCQ_SHT_QD(drv_name, drv_qd)			\
1513 	ATA_SUBBASE_SHT_QD(drv_name, drv_qd),			\
1514 	.sdev_groups		= ata_ncq_sdev_groups,		\
1515 	.change_queue_depth	= ata_scsi_change_queue_depth
1516 #endif
1517 
1518 /*
1519  * PMP helpers
1520  */
1521 #ifdef CONFIG_SATA_PMP
sata_pmp_supported(struct ata_port * ap)1522 static inline bool sata_pmp_supported(struct ata_port *ap)
1523 {
1524 	return ap->flags & ATA_FLAG_PMP;
1525 }
1526 
sata_pmp_attached(struct ata_port * ap)1527 static inline bool sata_pmp_attached(struct ata_port *ap)
1528 {
1529 	return ap->nr_pmp_links != 0;
1530 }
1531 
ata_is_host_link(const struct ata_link * link)1532 static inline bool ata_is_host_link(const struct ata_link *link)
1533 {
1534 	return link == &link->ap->link || link == link->ap->slave_link;
1535 }
1536 #else /* CONFIG_SATA_PMP */
sata_pmp_supported(struct ata_port * ap)1537 static inline bool sata_pmp_supported(struct ata_port *ap)
1538 {
1539 	return false;
1540 }
1541 
sata_pmp_attached(struct ata_port * ap)1542 static inline bool sata_pmp_attached(struct ata_port *ap)
1543 {
1544 	return false;
1545 }
1546 
ata_is_host_link(const struct ata_link * link)1547 static inline bool ata_is_host_link(const struct ata_link *link)
1548 {
1549 	return true;
1550 }
1551 #endif /* CONFIG_SATA_PMP */
1552 
sata_srst_pmp(struct ata_link * link)1553 static inline int sata_srst_pmp(struct ata_link *link)
1554 {
1555 	if (sata_pmp_supported(link->ap) && ata_is_host_link(link))
1556 		return SATA_PMP_CTRL_PORT;
1557 	return link->pmp;
1558 }
1559 
1560 #define ata_port_printk(level, ap, fmt, ...)			\
1561 	pr_ ## level ("ata%u: " fmt, (ap)->print_id, ##__VA_ARGS__)
1562 
1563 #define ata_port_err(ap, fmt, ...)				\
1564 	ata_port_printk(err, ap, fmt, ##__VA_ARGS__)
1565 #define ata_port_warn(ap, fmt, ...)				\
1566 	ata_port_printk(warn, ap, fmt, ##__VA_ARGS__)
1567 #define ata_port_notice(ap, fmt, ...)				\
1568 	ata_port_printk(notice, ap, fmt, ##__VA_ARGS__)
1569 #define ata_port_info(ap, fmt, ...)				\
1570 	ata_port_printk(info, ap, fmt, ##__VA_ARGS__)
1571 #define ata_port_dbg(ap, fmt, ...)				\
1572 	ata_port_printk(debug, ap, fmt, ##__VA_ARGS__)
1573 
1574 #define ata_link_printk(level, link, fmt, ...)			\
1575 do {								\
1576 	if (sata_pmp_attached((link)->ap) ||			\
1577 	    (link)->ap->slave_link)				\
1578 		pr_ ## level ("ata%u.%02u: " fmt,		\
1579 			      (link)->ap->print_id,		\
1580 			      (link)->pmp,			\
1581 			      ##__VA_ARGS__);			\
1582         else							\
1583 		pr_ ## level ("ata%u: " fmt,			\
1584 			      (link)->ap->print_id,		\
1585 			      ##__VA_ARGS__);			\
1586 } while (0)
1587 
1588 #define ata_link_err(link, fmt, ...)				\
1589 	ata_link_printk(err, link, fmt, ##__VA_ARGS__)
1590 #define ata_link_warn(link, fmt, ...)				\
1591 	ata_link_printk(warn, link, fmt, ##__VA_ARGS__)
1592 #define ata_link_notice(link, fmt, ...)				\
1593 	ata_link_printk(notice, link, fmt, ##__VA_ARGS__)
1594 #define ata_link_info(link, fmt, ...)				\
1595 	ata_link_printk(info, link, fmt, ##__VA_ARGS__)
1596 #define ata_link_dbg(link, fmt, ...)				\
1597 	ata_link_printk(debug, link, fmt, ##__VA_ARGS__)
1598 
1599 #define ata_dev_printk(level, dev, fmt, ...)			\
1600         pr_ ## level("ata%u.%02u: " fmt,			\
1601                (dev)->link->ap->print_id,			\
1602 	       (dev)->link->pmp + (dev)->devno,			\
1603 	       ##__VA_ARGS__)
1604 
1605 #define ata_dev_err(dev, fmt, ...)				\
1606 	ata_dev_printk(err, dev, fmt, ##__VA_ARGS__)
1607 #define ata_dev_warn(dev, fmt, ...)				\
1608 	ata_dev_printk(warn, dev, fmt, ##__VA_ARGS__)
1609 #define ata_dev_notice(dev, fmt, ...)				\
1610 	ata_dev_printk(notice, dev, fmt, ##__VA_ARGS__)
1611 #define ata_dev_info(dev, fmt, ...)				\
1612 	ata_dev_printk(info, dev, fmt, ##__VA_ARGS__)
1613 #define ata_dev_dbg(dev, fmt, ...)				\
1614 	ata_dev_printk(debug, dev, fmt, ##__VA_ARGS__)
1615 
1616 #define ata_dev_warn_once(dev, fmt, ...)			\
1617 	pr_warn_once("ata%u.%02u: " fmt,			\
1618 		     (dev)->link->ap->print_id,			\
1619 		     (dev)->link->pmp + (dev)->devno,		\
1620 		     ##__VA_ARGS__)
1621 
ata_print_version_once(const struct device * dev,const char * version)1622 static inline void ata_print_version_once(const struct device *dev,
1623 					  const char *version)
1624 {
1625 	dev_dbg_once(dev, "version %s\n", version);
1626 }
1627 
1628 /*
1629  * ata_eh_info helpers
1630  */
1631 extern __printf(2, 3)
1632 void __ata_ehi_push_desc(struct ata_eh_info *ehi, const char *fmt, ...);
1633 extern __printf(2, 3)
1634 void ata_ehi_push_desc(struct ata_eh_info *ehi, const char *fmt, ...);
1635 extern void ata_ehi_clear_desc(struct ata_eh_info *ehi);
1636 
ata_ehi_hotplugged(struct ata_eh_info * ehi)1637 static inline void ata_ehi_hotplugged(struct ata_eh_info *ehi)
1638 {
1639 	ehi->probe_mask |= (1 << ATA_MAX_DEVICES) - 1;
1640 	ehi->flags |= ATA_EHI_HOTPLUGGED;
1641 	ehi->action |= ATA_EH_RESET | ATA_EH_ENABLE_LINK;
1642 	ehi->err_mask |= AC_ERR_ATA_BUS;
1643 }
1644 
1645 /*
1646  * port description helpers
1647  */
1648 extern __printf(2, 3)
1649 void ata_port_desc(struct ata_port *ap, const char *fmt, ...);
1650 #ifdef CONFIG_PCI
1651 extern void ata_port_pbar_desc(struct ata_port *ap, int bar, ssize_t offset,
1652 			       const char *name);
1653 #endif
ata_port_desc_misc(struct ata_port * ap,int irq)1654 static inline void ata_port_desc_misc(struct ata_port *ap, int irq)
1655 {
1656 	ata_port_desc(ap, "irq %d", irq);
1657 	ata_port_desc(ap, "lpm-pol %d", ap->target_lpm_policy);
1658 	if (ap->pflags & ATA_PFLAG_EXTERNAL)
1659 		ata_port_desc(ap, "ext");
1660 }
1661 
ata_tag_internal(unsigned int tag)1662 static inline bool ata_tag_internal(unsigned int tag)
1663 {
1664 	return tag == ATA_TAG_INTERNAL;
1665 }
1666 
ata_tag_valid(unsigned int tag)1667 static inline bool ata_tag_valid(unsigned int tag)
1668 {
1669 	return tag < ATA_MAX_QUEUE || ata_tag_internal(tag);
1670 }
1671 
1672 #define __ata_qc_for_each(ap, qc, tag, max_tag, fn)		\
1673 	for ((tag) = 0; (tag) < (max_tag) &&			\
1674 	     ({ qc = fn((ap), (tag)); 1; }); (tag)++)		\
1675 
1676 /*
1677  * Internal use only, iterate commands ignoring error handling and
1678  * status of 'qc'.
1679  */
1680 #define ata_qc_for_each_raw(ap, qc, tag)					\
1681 	__ata_qc_for_each(ap, qc, tag, ATA_MAX_QUEUE, __ata_qc_from_tag)
1682 
1683 /*
1684  * Iterate all potential commands that can be queued
1685  */
1686 #define ata_qc_for_each(ap, qc, tag)					\
1687 	__ata_qc_for_each(ap, qc, tag, ATA_MAX_QUEUE, ata_qc_from_tag)
1688 
1689 /*
1690  * Like ata_qc_for_each, but with the internal tag included
1691  */
1692 #define ata_qc_for_each_with_internal(ap, qc, tag)			\
1693 	__ata_qc_for_each(ap, qc, tag, ATA_MAX_QUEUE + 1, ata_qc_from_tag)
1694 
1695 /*
1696  * device helpers
1697  */
ata_class_enabled(unsigned int class)1698 static inline unsigned int ata_class_enabled(unsigned int class)
1699 {
1700 	return class == ATA_DEV_ATA || class == ATA_DEV_ATAPI ||
1701 		class == ATA_DEV_PMP || class == ATA_DEV_SEMB ||
1702 		class == ATA_DEV_ZAC;
1703 }
1704 
ata_class_disabled(unsigned int class)1705 static inline unsigned int ata_class_disabled(unsigned int class)
1706 {
1707 	return class == ATA_DEV_ATA_UNSUP || class == ATA_DEV_ATAPI_UNSUP ||
1708 		class == ATA_DEV_PMP_UNSUP || class == ATA_DEV_SEMB_UNSUP ||
1709 		class == ATA_DEV_ZAC_UNSUP;
1710 }
1711 
ata_class_absent(unsigned int class)1712 static inline unsigned int ata_class_absent(unsigned int class)
1713 {
1714 	return !ata_class_enabled(class) && !ata_class_disabled(class);
1715 }
1716 
ata_dev_enabled(const struct ata_device * dev)1717 static inline unsigned int ata_dev_enabled(const struct ata_device *dev)
1718 {
1719 	return ata_class_enabled(dev->class);
1720 }
1721 
ata_dev_disabled(const struct ata_device * dev)1722 static inline unsigned int ata_dev_disabled(const struct ata_device *dev)
1723 {
1724 	return ata_class_disabled(dev->class);
1725 }
1726 
ata_dev_absent(const struct ata_device * dev)1727 static inline unsigned int ata_dev_absent(const struct ata_device *dev)
1728 {
1729 	return ata_class_absent(dev->class);
1730 }
1731 
1732 /*
1733  * link helpers
1734  */
ata_link_max_devices(const struct ata_link * link)1735 static inline int ata_link_max_devices(const struct ata_link *link)
1736 {
1737 	if (ata_is_host_link(link) && link->ap->flags & ATA_FLAG_SLAVE_POSS)
1738 		return 2;
1739 	return 1;
1740 }
1741 
ata_link_active(struct ata_link * link)1742 static inline int ata_link_active(struct ata_link *link)
1743 {
1744 	return ata_tag_valid(link->active_tag) || link->sactive;
1745 }
1746 
1747 /*
1748  * Iterators
1749  *
1750  * ATA_LITER_* constants are used to select link iteration mode and
1751  * ATA_DITER_* device iteration mode.
1752  *
1753  * For a custom iteration directly using ata_{link|dev}_next(), if
1754  * @link or @dev, respectively, is NULL, the first element is
1755  * returned.  @dev and @link can be any valid device or link and the
1756  * next element according to the iteration mode will be returned.
1757  * After the last element, NULL is returned.
1758  */
1759 enum ata_link_iter_mode {
1760 	ATA_LITER_EDGE,		/* if present, PMP links only; otherwise,
1761 				 * host link.  no slave link */
1762 	ATA_LITER_HOST_FIRST,	/* host link followed by PMP or slave links */
1763 	ATA_LITER_PMP_FIRST,	/* PMP links followed by host link,
1764 				 * slave link still comes after host link */
1765 };
1766 
1767 enum ata_dev_iter_mode {
1768 	ATA_DITER_ENABLED,
1769 	ATA_DITER_ENABLED_REVERSE,
1770 	ATA_DITER_ALL,
1771 	ATA_DITER_ALL_REVERSE,
1772 };
1773 
1774 extern struct ata_link *ata_link_next(struct ata_link *link,
1775 				      struct ata_port *ap,
1776 				      enum ata_link_iter_mode mode);
1777 
1778 extern struct ata_device *ata_dev_next(struct ata_device *dev,
1779 				       struct ata_link *link,
1780 				       enum ata_dev_iter_mode mode);
1781 
1782 /*
1783  * Shortcut notation for iterations
1784  *
1785  * ata_for_each_link() iterates over each link of @ap according to
1786  * @mode.  @link points to the current link in the loop.  @link is
1787  * NULL after loop termination.  ata_for_each_dev() works the same way
1788  * except that it iterates over each device of @link.
1789  *
1790  * Note that the mode prefixes ATA_{L|D}ITER_ shouldn't need to be
1791  * specified when using the following shorthand notations.  Only the
1792  * mode itself (EDGE, HOST_FIRST, ENABLED, etc...) should be
1793  * specified.  This not only increases brevity but also makes it
1794  * impossible to use ATA_LITER_* for device iteration or vice-versa.
1795  */
1796 #define ata_for_each_link(link, ap, mode) \
1797 	for ((link) = ata_link_next(NULL, (ap), ATA_LITER_##mode); (link); \
1798 	     (link) = ata_link_next((link), (ap), ATA_LITER_##mode))
1799 
1800 #define ata_for_each_dev(dev, link, mode) \
1801 	for ((dev) = ata_dev_next(NULL, (link), ATA_DITER_##mode); (dev); \
1802 	     (dev) = ata_dev_next((dev), (link), ATA_DITER_##mode))
1803 
1804 /**
1805  *	ata_ncq_supported - Test whether NCQ is supported
1806  *	@dev: ATA device to test
1807  *
1808  *	LOCKING:
1809  *	spin_lock_irqsave(host lock)
1810  *
1811  *	RETURNS:
1812  *	true if @dev supports NCQ, false otherwise.
1813  */
ata_ncq_supported(struct ata_device * dev)1814 static inline bool ata_ncq_supported(struct ata_device *dev)
1815 {
1816 	if (!IS_ENABLED(CONFIG_SATA_HOST))
1817 		return false;
1818 	return (dev->flags & (ATA_DFLAG_PIO | ATA_DFLAG_NCQ)) == ATA_DFLAG_NCQ;
1819 }
1820 
1821 /**
1822  *	ata_ncq_enabled - Test whether NCQ is enabled
1823  *	@dev: ATA device to test
1824  *
1825  *	LOCKING:
1826  *	spin_lock_irqsave(host lock)
1827  *
1828  *	RETURNS:
1829  *	true if NCQ is enabled for @dev, false otherwise.
1830  */
ata_ncq_enabled(struct ata_device * dev)1831 static inline bool ata_ncq_enabled(struct ata_device *dev)
1832 {
1833 	return ata_ncq_supported(dev) && !(dev->flags & ATA_DFLAG_NCQ_OFF);
1834 }
1835 
ata_fpdma_dsm_supported(struct ata_device * dev)1836 static inline bool ata_fpdma_dsm_supported(struct ata_device *dev)
1837 {
1838 	return (dev->flags & ATA_DFLAG_NCQ_SEND_RECV) &&
1839 		(dev->ncq_send_recv_cmds[ATA_LOG_NCQ_SEND_RECV_DSM_OFFSET] &
1840 		 ATA_LOG_NCQ_SEND_RECV_DSM_TRIM);
1841 }
1842 
ata_fpdma_read_log_supported(struct ata_device * dev)1843 static inline bool ata_fpdma_read_log_supported(struct ata_device *dev)
1844 {
1845 	return (dev->flags & ATA_DFLAG_NCQ_SEND_RECV) &&
1846 		(dev->ncq_send_recv_cmds[ATA_LOG_NCQ_SEND_RECV_RD_LOG_OFFSET] &
1847 		 ATA_LOG_NCQ_SEND_RECV_RD_LOG_SUPPORTED);
1848 }
1849 
ata_fpdma_zac_mgmt_in_supported(struct ata_device * dev)1850 static inline bool ata_fpdma_zac_mgmt_in_supported(struct ata_device *dev)
1851 {
1852 	return (dev->flags & ATA_DFLAG_NCQ_SEND_RECV) &&
1853 		(dev->ncq_send_recv_cmds[ATA_LOG_NCQ_SEND_RECV_ZAC_MGMT_OFFSET] &
1854 		ATA_LOG_NCQ_SEND_RECV_ZAC_MGMT_IN_SUPPORTED);
1855 }
1856 
ata_fpdma_zac_mgmt_out_supported(struct ata_device * dev)1857 static inline bool ata_fpdma_zac_mgmt_out_supported(struct ata_device *dev)
1858 {
1859 	return (dev->ncq_non_data_cmds[ATA_LOG_NCQ_NON_DATA_ZAC_MGMT_OFFSET] &
1860 		ATA_LOG_NCQ_NON_DATA_ZAC_MGMT_OUT);
1861 }
1862 
ata_qc_set_polling(struct ata_queued_cmd * qc)1863 static inline void ata_qc_set_polling(struct ata_queued_cmd *qc)
1864 {
1865 	qc->tf.ctl |= ATA_NIEN;
1866 }
1867 
__ata_qc_from_tag(struct ata_port * ap,unsigned int tag)1868 static inline struct ata_queued_cmd *__ata_qc_from_tag(struct ata_port *ap,
1869 						       unsigned int tag)
1870 {
1871 	if (ata_tag_valid(tag))
1872 		return &ap->qcmd[tag];
1873 	return NULL;
1874 }
1875 
ata_qc_from_tag(struct ata_port * ap,unsigned int tag)1876 static inline struct ata_queued_cmd *ata_qc_from_tag(struct ata_port *ap,
1877 						     unsigned int tag)
1878 {
1879 	struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
1880 
1881 	if (unlikely(!qc))
1882 		return qc;
1883 
1884 	if ((qc->flags & (ATA_QCFLAG_ACTIVE |
1885 			  ATA_QCFLAG_EH)) == ATA_QCFLAG_ACTIVE)
1886 		return qc;
1887 
1888 	return NULL;
1889 }
1890 
ata_qc_raw_nbytes(struct ata_queued_cmd * qc)1891 static inline unsigned int ata_qc_raw_nbytes(struct ata_queued_cmd *qc)
1892 {
1893 	return qc->nbytes - min(qc->extrabytes, qc->nbytes);
1894 }
1895 
ata_tf_init(struct ata_device * dev,struct ata_taskfile * tf)1896 static inline void ata_tf_init(struct ata_device *dev, struct ata_taskfile *tf)
1897 {
1898 	memset(tf, 0, sizeof(*tf));
1899 
1900 #ifdef CONFIG_ATA_SFF
1901 	tf->ctl = dev->link->ap->ctl;
1902 #else
1903 	tf->ctl = ATA_DEVCTL_OBS;
1904 #endif
1905 	if (dev->devno == 0)
1906 		tf->device = ATA_DEVICE_OBS;
1907 	else
1908 		tf->device = ATA_DEVICE_OBS | ATA_DEV1;
1909 }
1910 
ata_qc_reinit(struct ata_queued_cmd * qc)1911 static inline void ata_qc_reinit(struct ata_queued_cmd *qc)
1912 {
1913 	qc->dma_dir = DMA_NONE;
1914 	qc->sg = NULL;
1915 	qc->flags = 0;
1916 	qc->cursg = NULL;
1917 	qc->cursg_ofs = 0;
1918 	qc->nbytes = qc->extrabytes = qc->curbytes = 0;
1919 	qc->n_elem = 0;
1920 	qc->err_mask = 0;
1921 	qc->sect_size = ATA_SECT_SIZE;
1922 
1923 	ata_tf_init(qc->dev, &qc->tf);
1924 
1925 	/* init result_tf such that it indicates normal completion */
1926 	qc->result_tf.command = ATA_DRDY;
1927 	qc->result_tf.feature = 0;
1928 }
1929 
ata_try_flush_cache(const struct ata_device * dev)1930 static inline int ata_try_flush_cache(const struct ata_device *dev)
1931 {
1932 	return ata_id_wcache_enabled(dev->id) ||
1933 	       ata_id_has_flush(dev->id) ||
1934 	       ata_id_has_flush_ext(dev->id);
1935 }
1936 
ac_err_mask(u8 status)1937 static inline unsigned int ac_err_mask(u8 status)
1938 {
1939 	if (status & (ATA_BUSY | ATA_DRQ))
1940 		return AC_ERR_HSM;
1941 	if (status & (ATA_ERR | ATA_DF))
1942 		return AC_ERR_DEV;
1943 	return 0;
1944 }
1945 
__ac_err_mask(u8 status)1946 static inline unsigned int __ac_err_mask(u8 status)
1947 {
1948 	unsigned int mask = ac_err_mask(status);
1949 	if (mask == 0)
1950 		return AC_ERR_OTHER;
1951 	return mask;
1952 }
1953 
ata_shost_to_port(struct Scsi_Host * host)1954 static inline struct ata_port *ata_shost_to_port(struct Scsi_Host *host)
1955 {
1956 	return *(struct ata_port **)&host->hostdata[0];
1957 }
1958 
ata_check_ready(u8 status)1959 static inline int ata_check_ready(u8 status)
1960 {
1961 	if (!(status & ATA_BUSY))
1962 		return 1;
1963 
1964 	/* 0xff indicates either no device or device not ready */
1965 	if (status == 0xff)
1966 		return -ENODEV;
1967 
1968 	return 0;
1969 }
1970 
ata_deadline(unsigned long from_jiffies,unsigned int timeout_msecs)1971 static inline unsigned long ata_deadline(unsigned long from_jiffies,
1972 					 unsigned int timeout_msecs)
1973 {
1974 	return from_jiffies + msecs_to_jiffies(timeout_msecs);
1975 }
1976 
1977 /* Don't open code these in drivers as there are traps. Firstly the range may
1978    change in future hardware and specs, secondly 0xFF means 'no DMA' but is
1979    > UDMA_0. Dyma ddreigiau */
1980 
ata_using_mwdma(struct ata_device * adev)1981 static inline bool ata_using_mwdma(struct ata_device *adev)
1982 {
1983 	return adev->dma_mode >= XFER_MW_DMA_0 &&
1984 		adev->dma_mode <= XFER_MW_DMA_4;
1985 }
1986 
ata_using_udma(struct ata_device * adev)1987 static inline bool ata_using_udma(struct ata_device *adev)
1988 {
1989 	return adev->dma_mode >= XFER_UDMA_0 &&
1990 		adev->dma_mode <= XFER_UDMA_7;
1991 }
1992 
ata_dma_enabled(struct ata_device * adev)1993 static inline bool ata_dma_enabled(struct ata_device *adev)
1994 {
1995 	return adev->dma_mode != 0xFF;
1996 }
1997 
1998 /**************************************************************************
1999  * PATA timings - drivers/ata/libata-pata-timings.c
2000  */
2001 extern const struct ata_timing *ata_timing_find_mode(u8 xfer_mode);
2002 extern int ata_timing_compute(struct ata_device *, unsigned short,
2003 			      struct ata_timing *, int, int);
2004 extern void ata_timing_merge(const struct ata_timing *,
2005 			     const struct ata_timing *, struct ata_timing *,
2006 			     unsigned int);
2007 
2008 /**************************************************************************
2009  * PMP - drivers/ata/libata-pmp.c
2010  */
2011 #ifdef CONFIG_SATA_PMP
2012 
2013 extern const struct ata_port_operations sata_pmp_port_ops;
2014 
2015 extern int sata_pmp_qc_defer_cmd_switch(struct ata_queued_cmd *qc);
2016 extern void sata_pmp_error_handler(struct ata_port *ap)
2017 	__must_hold(&ap->host->eh_mutex);
2018 
2019 #else /* CONFIG_SATA_PMP */
2020 
2021 #define sata_pmp_port_ops		sata_port_ops
2022 #define sata_pmp_qc_defer_cmd_switch	ata_std_qc_defer
2023 #define sata_pmp_error_handler		ata_std_error_handler
2024 
2025 #endif /* CONFIG_SATA_PMP */
2026 
2027 
2028 /**************************************************************************
2029  * SFF - drivers/ata/libata-sff.c
2030  */
2031 #ifdef CONFIG_ATA_SFF
2032 
2033 extern const struct ata_port_operations ata_sff_port_ops;
2034 extern const struct ata_port_operations ata_bmdma32_port_ops;
2035 
2036 /* PIO only, sg_tablesize and dma_boundary limits can be removed */
2037 #define ATA_PIO_SHT(drv_name)					\
2038 	ATA_BASE_SHT(drv_name),					\
2039 	.sg_tablesize		= LIBATA_MAX_PRD,		\
2040 	.dma_boundary		= ATA_DMA_BOUNDARY
2041 
2042 extern void ata_sff_dev_select(struct ata_port *ap, unsigned int device);
2043 extern u8 ata_sff_check_status(struct ata_port *ap);
2044 extern void ata_sff_pause(struct ata_port *ap);
2045 extern void ata_sff_dma_pause(struct ata_port *ap);
2046 extern int ata_sff_wait_ready(struct ata_link *link, unsigned long deadline);
2047 extern void ata_sff_tf_load(struct ata_port *ap, const struct ata_taskfile *tf);
2048 extern void ata_sff_tf_read(struct ata_port *ap, struct ata_taskfile *tf);
2049 extern void ata_sff_exec_command(struct ata_port *ap,
2050 				 const struct ata_taskfile *tf);
2051 extern unsigned int ata_sff_data_xfer(struct ata_queued_cmd *qc,
2052 			unsigned char *buf, unsigned int buflen, int rw);
2053 extern unsigned int ata_sff_data_xfer32(struct ata_queued_cmd *qc,
2054 			unsigned char *buf, unsigned int buflen, int rw);
2055 extern void ata_sff_irq_on(struct ata_port *ap);
2056 extern int ata_sff_hsm_move(struct ata_port *ap, struct ata_queued_cmd *qc,
2057 			    u8 status, int in_wq);
2058 extern void ata_sff_queue_work(struct work_struct *work);
2059 extern void ata_sff_queue_delayed_work(struct delayed_work *dwork,
2060 		unsigned long delay);
2061 extern void ata_sff_queue_pio_task(struct ata_link *link, unsigned long delay);
2062 extern unsigned int ata_sff_qc_issue(struct ata_queued_cmd *qc);
2063 extern void ata_sff_qc_fill_rtf(struct ata_queued_cmd *qc);
2064 extern unsigned int ata_sff_port_intr(struct ata_port *ap,
2065 				      struct ata_queued_cmd *qc);
2066 extern irqreturn_t ata_sff_interrupt(int irq, void *dev_instance);
2067 extern void ata_sff_lost_interrupt(struct ata_port *ap);
2068 extern void ata_sff_freeze(struct ata_port *ap);
2069 extern void ata_sff_thaw(struct ata_port *ap);
2070 extern int ata_sff_prereset(struct ata_link *link, unsigned long deadline);
2071 extern unsigned int ata_sff_dev_classify(struct ata_device *dev, int present,
2072 					  u8 *r_err);
2073 extern int ata_sff_wait_after_reset(struct ata_link *link, unsigned int devmask,
2074 				    unsigned long deadline);
2075 extern int ata_sff_softreset(struct ata_link *link, unsigned int *classes,
2076 			     unsigned long deadline);
2077 extern int sata_sff_hardreset(struct ata_link *link, unsigned int *class,
2078 			       unsigned long deadline);
2079 extern void ata_sff_postreset(struct ata_link *link, unsigned int *classes);
2080 extern void ata_sff_drain_fifo(struct ata_queued_cmd *qc);
2081 extern void ata_sff_error_handler(struct ata_port *ap)
2082 	__must_hold(&ap->host->eh_mutex);
2083 extern void ata_sff_std_ports(struct ata_ioports *ioaddr);
2084 #ifdef CONFIG_PCI
2085 extern int ata_pci_sff_init_host(struct ata_host *host);
2086 extern int ata_pci_sff_prepare_host(struct pci_dev *pdev,
2087 				    const struct ata_port_info * const * ppi,
2088 				    struct ata_host **r_host);
2089 extern int ata_pci_sff_activate_host(struct ata_host *host,
2090 				     irq_handler_t irq_handler,
2091 				     const struct scsi_host_template *sht);
2092 extern int ata_pci_sff_init_one(struct pci_dev *pdev,
2093 		const struct ata_port_info * const * ppi,
2094 		const struct scsi_host_template *sht, void *host_priv, int hflags);
2095 #endif /* CONFIG_PCI */
2096 
2097 #ifdef CONFIG_ATA_BMDMA
2098 
2099 extern const struct ata_port_operations ata_bmdma_port_ops;
2100 
2101 #define ATA_BMDMA_SHT(drv_name)					\
2102 	ATA_BASE_SHT(drv_name),					\
2103 	.sg_tablesize		= LIBATA_MAX_PRD,		\
2104 	.dma_boundary		= ATA_DMA_BOUNDARY
2105 
2106 extern enum ata_completion_errors ata_bmdma_qc_prep(struct ata_queued_cmd *qc);
2107 extern unsigned int ata_bmdma_qc_issue(struct ata_queued_cmd *qc);
2108 extern enum ata_completion_errors ata_bmdma_dumb_qc_prep(struct ata_queued_cmd *qc);
2109 extern unsigned int ata_bmdma_port_intr(struct ata_port *ap,
2110 				      struct ata_queued_cmd *qc);
2111 extern irqreturn_t ata_bmdma_interrupt(int irq, void *dev_instance);
2112 extern void ata_bmdma_error_handler(struct ata_port *ap)
2113 	__must_hold(&ap->host->eh_mutex);
2114 extern void ata_bmdma_post_internal_cmd(struct ata_queued_cmd *qc);
2115 extern void ata_bmdma_irq_clear(struct ata_port *ap);
2116 extern void ata_bmdma_setup(struct ata_queued_cmd *qc);
2117 extern void ata_bmdma_start(struct ata_queued_cmd *qc);
2118 extern void ata_bmdma_stop(struct ata_queued_cmd *qc);
2119 extern u8 ata_bmdma_status(struct ata_port *ap);
2120 extern int ata_bmdma_port_start(struct ata_port *ap);
2121 extern int ata_bmdma_port_start32(struct ata_port *ap);
2122 
2123 #ifdef CONFIG_PCI
2124 extern int ata_pci_bmdma_clear_simplex(struct pci_dev *pdev);
2125 extern void ata_pci_bmdma_init(struct ata_host *host);
2126 extern int ata_pci_bmdma_prepare_host(struct pci_dev *pdev,
2127 				      const struct ata_port_info * const * ppi,
2128 				      struct ata_host **r_host);
2129 extern int ata_pci_bmdma_init_one(struct pci_dev *pdev,
2130 				  const struct ata_port_info * const * ppi,
2131 				  const struct scsi_host_template *sht,
2132 				  void *host_priv, int hflags);
2133 #endif /* CONFIG_PCI */
2134 #endif /* CONFIG_ATA_BMDMA */
2135 
2136 /**
2137  *	ata_sff_busy_wait - Wait for a port status register
2138  *	@ap: Port to wait for.
2139  *	@bits: bits that must be clear
2140  *	@max: number of 10uS waits to perform
2141  *
2142  *	Waits up to max*10 microseconds for the selected bits in the port's
2143  *	status register to be cleared.
2144  *	Returns final value of status register.
2145  *
2146  *	LOCKING:
2147  *	Inherited from caller.
2148  */
ata_sff_busy_wait(struct ata_port * ap,unsigned int bits,unsigned int max)2149 static inline u8 ata_sff_busy_wait(struct ata_port *ap, unsigned int bits,
2150 				   unsigned int max)
2151 {
2152 	u8 status;
2153 
2154 	do {
2155 		udelay(10);
2156 		status = ap->ops->sff_check_status(ap);
2157 		max--;
2158 	} while (status != 0xff && (status & bits) && (max > 0));
2159 
2160 	return status;
2161 }
2162 
2163 /**
2164  *	ata_wait_idle - Wait for a port to be idle.
2165  *	@ap: Port to wait for.
2166  *
2167  *	Waits up to 10ms for port's BUSY and DRQ signals to clear.
2168  *	Returns final value of status register.
2169  *
2170  *	LOCKING:
2171  *	Inherited from caller.
2172  */
ata_wait_idle(struct ata_port * ap)2173 static inline u8 ata_wait_idle(struct ata_port *ap)
2174 {
2175 	u8 status = ata_sff_busy_wait(ap, ATA_BUSY | ATA_DRQ, 1000);
2176 
2177 	if (status != 0xff && (status & (ATA_BUSY | ATA_DRQ)))
2178 		ata_port_dbg(ap, "abnormal Status 0x%X\n", status);
2179 
2180 	return status;
2181 }
2182 #else /* CONFIG_ATA_SFF */
sata_sff_hardreset(struct ata_link * link,unsigned int * class,unsigned long deadline)2183 static inline int sata_sff_hardreset(struct ata_link *link, unsigned int *class,
2184 				     unsigned long deadline)
2185 {
2186 	return -EOPNOTSUPP;
2187 }
2188 #endif /* CONFIG_ATA_SFF */
2189 
2190 #endif /* __LINUX_LIBATA_H__ */
2191