xref: /linux/include/linux/libata.h (revision fb2c3f44a7e14627b9dca8e6b568e6cdc234b40b)
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_AND_AMD, /* Disable LPM on ATI and AMD chipsets */
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_AND_AMD	= BIT_ULL(__ATA_QUIRK_NO_LPM_ON_ATI_AND_AMD),
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 
1095 static inline bool ata_is_atapi(u8 prot)
1096 {
1097 	return prot & ATA_PROT_FLAG_ATAPI;
1098 }
1099 
1100 static inline bool ata_is_pio(u8 prot)
1101 {
1102 	return prot & ATA_PROT_FLAG_PIO;
1103 }
1104 
1105 static inline bool ata_is_dma(u8 prot)
1106 {
1107 	return prot & ATA_PROT_FLAG_DMA;
1108 }
1109 
1110 static inline bool ata_is_ncq(u8 prot)
1111 {
1112 	return prot & ATA_PROT_FLAG_NCQ;
1113 }
1114 
1115 static inline bool ata_is_data(u8 prot)
1116 {
1117 	return prot & (ATA_PROT_FLAG_PIO | ATA_PROT_FLAG_DMA);
1118 }
1119 
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 
1129 static inline int ata_port_is_dummy(struct ata_port *ap)
1130 {
1131 	return ap->ops == &ata_dummy_port_ops;
1132 }
1133 
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 void ata_host_undo_start(struct ata_host *host);
1151 extern int ata_host_register(struct ata_host *host,
1152 			     const struct scsi_host_template *sht);
1153 extern int ata_host_activate(struct ata_host *host, int irq,
1154 			     irq_handler_t irq_handler, unsigned long irq_flags,
1155 			     const struct scsi_host_template *sht);
1156 extern void ata_host_detach(struct ata_host *host);
1157 extern void ata_host_init(struct ata_host *, struct device *, struct ata_port_operations *);
1158 extern int ata_scsi_ioctl(struct scsi_device *dev, unsigned int cmd,
1159 			  void __user *arg);
1160 #ifdef CONFIG_COMPAT
1161 #define ATA_SCSI_COMPAT_IOCTL .compat_ioctl = ata_scsi_ioctl,
1162 #else
1163 #define ATA_SCSI_COMPAT_IOCTL /* empty */
1164 #endif
1165 extern enum scsi_qc_status ata_scsi_queuecmd(struct Scsi_Host *h,
1166 					     struct scsi_cmnd *cmd);
1167 enum scsi_timeout_action ata_scsi_retry_deferred_qc(struct ata_port *ap,
1168 						    struct scsi_cmnd *scmd);
1169 enum scsi_timeout_action ata_scsi_eh_timed_out(struct scsi_cmnd *cmd);
1170 #if IS_REACHABLE(CONFIG_ATA)
1171 bool ata_scsi_dma_need_drain(struct request *rq);
1172 #else
1173 #define ata_scsi_dma_need_drain NULL
1174 #endif
1175 extern int ata_sas_scsi_ioctl(struct ata_port *ap, struct scsi_device *dev,
1176 			    unsigned int cmd, void __user *arg);
1177 extern bool ata_link_online(struct ata_link *link);
1178 extern bool ata_link_offline(struct ata_link *link);
1179 #ifdef CONFIG_PM
1180 extern void ata_host_suspend(struct ata_host *host, pm_message_t mesg);
1181 extern void ata_host_resume(struct ata_host *host);
1182 extern void ata_sas_port_suspend(struct ata_port *ap);
1183 extern void ata_sas_port_resume(struct ata_port *ap);
1184 #else
1185 static inline void ata_sas_port_suspend(struct ata_port *ap)
1186 {
1187 }
1188 static inline void ata_sas_port_resume(struct ata_port *ap)
1189 {
1190 }
1191 #endif
1192 extern int ata_ratelimit(void);
1193 extern void ata_msleep(struct ata_port *ap, unsigned int msecs);
1194 extern u32 ata_wait_register(struct ata_port *ap, void __iomem *reg, u32 mask,
1195 			     u32 val, unsigned int interval, unsigned int timeout);
1196 extern int atapi_cmd_type(u8 opcode);
1197 extern unsigned int ata_pack_xfermask(unsigned int pio_mask,
1198 				      unsigned int mwdma_mask,
1199 				      unsigned int udma_mask);
1200 extern void ata_unpack_xfermask(unsigned int xfer_mask,
1201 				unsigned int *pio_mask,
1202 				unsigned int *mwdma_mask,
1203 				unsigned int *udma_mask);
1204 extern u8 ata_xfer_mask2mode(unsigned int xfer_mask);
1205 extern unsigned int ata_xfer_mode2mask(u8 xfer_mode);
1206 extern int ata_xfer_mode2shift(u8 xfer_mode);
1207 extern const char *ata_mode_string(unsigned int xfer_mask);
1208 extern unsigned int ata_id_xfermask(const u16 *id);
1209 extern int ata_std_qc_defer(struct ata_queued_cmd *qc);
1210 extern void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
1211 		 unsigned int n_elem);
1212 extern unsigned int ata_dev_classify(const struct ata_taskfile *tf);
1213 extern unsigned int ata_port_classify(struct ata_port *ap,
1214 				      const struct ata_taskfile *tf);
1215 extern void ata_dev_disable(struct ata_device *adev);
1216 extern void ata_id_string(const u16 *id, unsigned char *s,
1217 			  unsigned int ofs, unsigned int len);
1218 extern void ata_id_c_string(const u16 *id, unsigned char *s,
1219 			    unsigned int ofs, unsigned int len);
1220 extern unsigned int ata_do_dev_read_id(struct ata_device *dev,
1221 				       struct ata_taskfile *tf, __le16 *id);
1222 extern void ata_qc_complete(struct ata_queued_cmd *qc);
1223 extern u64 ata_qc_get_active(struct ata_port *ap);
1224 extern int ata_std_bios_param(struct scsi_device *sdev,
1225 			      struct gendisk *unused,
1226 			      sector_t capacity, int geom[]);
1227 extern void ata_scsi_unlock_native_capacity(struct scsi_device *sdev);
1228 extern int ata_scsi_sdev_init(struct scsi_device *sdev);
1229 int ata_scsi_sdev_configure(struct scsi_device *sdev, struct queue_limits *lim);
1230 extern void ata_scsi_sdev_destroy(struct scsi_device *sdev);
1231 extern int ata_scsi_change_queue_depth(struct scsi_device *sdev,
1232 				       int queue_depth);
1233 extern int ata_change_queue_depth(struct ata_port *ap, struct scsi_device *sdev,
1234 				  int queue_depth);
1235 extern int ata_ncq_prio_supported(struct ata_port *ap, struct scsi_device *sdev,
1236 				  bool *supported);
1237 extern int ata_ncq_prio_enabled(struct ata_port *ap, struct scsi_device *sdev,
1238 				bool *enabled);
1239 extern int ata_ncq_prio_enable(struct ata_port *ap, struct scsi_device *sdev,
1240 			       bool enable);
1241 extern struct ata_device *ata_dev_pair(struct ata_device *adev);
1242 int ata_set_mode(struct ata_link *link, struct ata_device **r_failed_dev);
1243 extern void ata_scsi_port_error_handler(struct Scsi_Host *host, struct ata_port *ap);
1244 int ata_scsi_cmd_error_handler(struct Scsi_Host *host, struct ata_port *ap,
1245 			       struct list_head *eh_q);
1246 
1247 /*
1248  * SATA specific code - drivers/ata/libata-sata.c
1249  */
1250 #ifdef CONFIG_SATA_HOST
1251 extern const unsigned int sata_deb_timing_normal[];
1252 extern const unsigned int sata_deb_timing_hotplug[];
1253 extern const unsigned int sata_deb_timing_long[];
1254 
1255 static inline const unsigned int *
1256 sata_ehc_deb_timing(struct ata_eh_context *ehc)
1257 {
1258 	if (ehc->i.flags & ATA_EHI_HOTPLUGGED)
1259 		return sata_deb_timing_hotplug;
1260 	else
1261 		return sata_deb_timing_normal;
1262 }
1263 
1264 extern int sata_scr_valid(struct ata_link *link);
1265 extern int sata_scr_read(struct ata_link *link, int reg, u32 *val);
1266 extern int sata_scr_write(struct ata_link *link, int reg, u32 val);
1267 extern int sata_scr_write_flush(struct ata_link *link, int reg, u32 val);
1268 extern int sata_set_spd(struct ata_link *link);
1269 int sata_std_hardreset(struct ata_link *link, unsigned int *class,
1270 		       unsigned long deadline);
1271 extern int sata_link_hardreset(struct ata_link *link,
1272 			const unsigned int *timing, unsigned long deadline,
1273 			bool *online, int (*check_ready)(struct ata_link *));
1274 extern int sata_link_resume(struct ata_link *link, const unsigned int *params,
1275 			    unsigned long deadline);
1276 extern void ata_eh_analyze_ncq_error(struct ata_link *link);
1277 #else
1278 static inline const unsigned int *
1279 sata_ehc_deb_timing(struct ata_eh_context *ehc)
1280 {
1281 	return NULL;
1282 }
1283 static inline int sata_scr_valid(struct ata_link *link) { return 0; }
1284 static inline int sata_scr_read(struct ata_link *link, int reg, u32 *val)
1285 {
1286 	return -EOPNOTSUPP;
1287 }
1288 static inline int sata_scr_write(struct ata_link *link, int reg, u32 val)
1289 {
1290 	return -EOPNOTSUPP;
1291 }
1292 static inline int sata_scr_write_flush(struct ata_link *link, int reg, u32 val)
1293 {
1294 	return -EOPNOTSUPP;
1295 }
1296 static inline int sata_set_spd(struct ata_link *link) { return -EOPNOTSUPP; }
1297 static inline int sata_std_hardreset(struct ata_link *link, unsigned int *class,
1298 				     unsigned long deadline)
1299 {
1300 	return -EOPNOTSUPP;
1301 }
1302 static inline int sata_link_hardreset(struct ata_link *link,
1303 				      const unsigned int *timing,
1304 				      unsigned long deadline,
1305 				      bool *online,
1306 				      int (*check_ready)(struct ata_link *))
1307 {
1308 	if (online)
1309 		*online = false;
1310 	return -EOPNOTSUPP;
1311 }
1312 static inline int sata_link_resume(struct ata_link *link,
1313 				   const unsigned int *params,
1314 				   unsigned long deadline)
1315 {
1316 	return -EOPNOTSUPP;
1317 }
1318 static inline void ata_eh_analyze_ncq_error(struct ata_link *link) { }
1319 #endif
1320 extern int sata_link_debounce(struct ata_link *link,
1321 			      const unsigned int *params, unsigned long deadline);
1322 extern int sata_link_scr_lpm(struct ata_link *link, enum ata_lpm_policy policy,
1323 			     bool spm_wakeup);
1324 extern int ata_slave_link_init(struct ata_port *ap);
1325 extern void ata_port_probe(struct ata_port *ap);
1326 extern struct ata_port *ata_port_alloc(struct ata_host *host);
1327 extern void ata_port_free(struct ata_port *ap);
1328 extern int ata_tport_add(struct device *parent, struct ata_port *ap);
1329 extern void ata_tport_delete(struct ata_port *ap);
1330 int ata_sas_sdev_configure(struct scsi_device *sdev, struct queue_limits *lim,
1331 			   struct ata_port *ap);
1332 extern int ata_sas_queuecmd(struct scsi_cmnd *cmd, struct ata_port *ap)
1333 	__must_hold(ap->lock);
1334 extern void ata_tf_to_fis(const struct ata_taskfile *tf,
1335 			  u8 pmp, int is_cmd, u8 *fis);
1336 extern void ata_tf_from_fis(const u8 *fis, struct ata_taskfile *tf);
1337 extern int ata_qc_complete_multiple(struct ata_port *ap, u64 qc_active);
1338 extern bool sata_lpm_ignore_phy_events(struct ata_link *link);
1339 extern int sata_async_notification(struct ata_port *ap);
1340 
1341 extern int ata_cable_40wire(struct ata_port *ap);
1342 extern int ata_cable_80wire(struct ata_port *ap);
1343 extern int ata_cable_sata(struct ata_port *ap);
1344 extern int ata_cable_ignore(struct ata_port *ap);
1345 extern int ata_cable_unknown(struct ata_port *ap);
1346 
1347 /* Timing helpers */
1348 extern unsigned int ata_pio_need_iordy(const struct ata_device *);
1349 extern u8 ata_timing_cycle2mode(unsigned int xfer_shift, int cycle);
1350 
1351 /* PCI */
1352 #ifdef CONFIG_PCI
1353 struct pci_dev;
1354 
1355 struct pci_bits {
1356 	unsigned int		reg;	/* PCI config register to read */
1357 	unsigned int		width;	/* 1 (8 bit), 2 (16 bit), 4 (32 bit) */
1358 	unsigned long		mask;
1359 	unsigned long		val;
1360 };
1361 
1362 extern int pci_test_config_bits(struct pci_dev *pdev, const struct pci_bits *bits);
1363 extern void ata_pci_shutdown_one(struct pci_dev *pdev);
1364 extern void ata_pci_remove_one(struct pci_dev *pdev);
1365 
1366 #ifdef CONFIG_PM
1367 extern void ata_pci_device_do_suspend(struct pci_dev *pdev, pm_message_t mesg);
1368 extern int __must_check ata_pci_device_do_resume(struct pci_dev *pdev);
1369 extern int ata_pci_device_suspend(struct pci_dev *pdev, pm_message_t mesg);
1370 extern int ata_pci_device_resume(struct pci_dev *pdev);
1371 #endif /* CONFIG_PM */
1372 #endif /* CONFIG_PCI */
1373 
1374 struct platform_device;
1375 
1376 extern void ata_platform_remove_one(struct platform_device *pdev);
1377 
1378 /*
1379  * ACPI - drivers/ata/libata-acpi.c
1380  */
1381 #ifdef CONFIG_ATA_ACPI
1382 static inline const struct ata_acpi_gtm *ata_acpi_init_gtm(struct ata_port *ap)
1383 {
1384 	if (ap->pflags & ATA_PFLAG_INIT_GTM_VALID)
1385 		return &ap->__acpi_init_gtm;
1386 	return NULL;
1387 }
1388 int ata_acpi_stm(struct ata_port *ap, const struct ata_acpi_gtm *stm);
1389 int ata_acpi_gtm(struct ata_port *ap, struct ata_acpi_gtm *stm);
1390 unsigned int ata_acpi_gtm_xfermask(struct ata_device *dev,
1391 				   const struct ata_acpi_gtm *gtm);
1392 int ata_acpi_cbl_pata_type(struct ata_port *ap);
1393 #else
1394 static inline const struct ata_acpi_gtm *ata_acpi_init_gtm(struct ata_port *ap)
1395 {
1396 	return NULL;
1397 }
1398 
1399 static inline int ata_acpi_stm(const struct ata_port *ap,
1400 			       struct ata_acpi_gtm *stm)
1401 {
1402 	return -ENOSYS;
1403 }
1404 
1405 static inline int ata_acpi_gtm(const struct ata_port *ap,
1406 			       struct ata_acpi_gtm *stm)
1407 {
1408 	return -ENOSYS;
1409 }
1410 
1411 static inline unsigned int ata_acpi_gtm_xfermask(struct ata_device *dev,
1412 					const struct ata_acpi_gtm *gtm)
1413 {
1414 	return 0;
1415 }
1416 
1417 static inline int ata_acpi_cbl_pata_type(struct ata_port *ap)
1418 {
1419 	return ATA_CBL_PATA40;
1420 }
1421 #endif
1422 
1423 /*
1424  * EH - drivers/ata/libata-eh.c
1425  */
1426 extern void ata_port_schedule_eh(struct ata_port *ap);
1427 extern void ata_port_wait_eh(struct ata_port *ap);
1428 extern int ata_link_abort(struct ata_link *link);
1429 extern int ata_port_abort(struct ata_port *ap);
1430 extern int ata_port_freeze(struct ata_port *ap);
1431 
1432 extern void ata_eh_freeze_port(struct ata_port *ap);
1433 extern void ata_eh_thaw_port(struct ata_port *ap);
1434 
1435 extern void ata_std_error_handler(struct ata_port *ap)
1436 	__must_hold(&ap->host->eh_mutex);
1437 extern void ata_std_sched_eh(struct ata_port *ap);
1438 extern void ata_std_end_eh(struct ata_port *ap);
1439 extern int ata_link_nr_enabled(struct ata_link *link);
1440 
1441 /*
1442  * Base operations to inherit from and initializers for sht
1443  *
1444  * Operations
1445  *
1446  * base  : Common to all libata drivers.
1447  * sata  : SATA controllers w/ native interface.
1448  * pmp   : SATA controllers w/ PMP support.
1449  * sff   : SFF ATA controllers w/o BMDMA support.
1450  * bmdma : SFF ATA controllers w/ BMDMA support.
1451  *
1452  * sht initializers
1453  *
1454  * BASE  : Common to all libata drivers.  The user must set
1455  *	   sg_tablesize and dma_boundary.
1456  * PIO   : SFF ATA controllers w/ only PIO support.
1457  * BMDMA : SFF ATA controllers w/ BMDMA support.  sg_tablesize and
1458  *	   dma_boundary are set to BMDMA limits.
1459  * NCQ   : SATA controllers supporting NCQ.  The user must set
1460  *	   sg_tablesize, dma_boundary and can_queue.
1461  */
1462 extern const struct ata_port_operations ata_base_port_ops;
1463 extern const struct ata_port_operations sata_port_ops;
1464 extern const struct attribute_group *ata_common_sdev_groups[];
1465 
1466 /*
1467  * All sht initializers (BASE, PIO, BMDMA, NCQ) must be instantiated
1468  * by the edge drivers.  Because the 'module' field of sht must be the
1469  * edge driver's module reference, otherwise the driver can be unloaded
1470  * even if the scsi_device is being accessed.
1471  */
1472 #define __ATA_BASE_SHT(drv_name)				\
1473 	.module			= THIS_MODULE,			\
1474 	.name			= drv_name,			\
1475 	.ioctl			= ata_scsi_ioctl,		\
1476 	ATA_SCSI_COMPAT_IOCTL					\
1477 	.queuecommand		= ata_scsi_queuecmd,		\
1478 	.eh_timed_out		= ata_scsi_eh_timed_out,	\
1479 	.dma_need_drain		= ata_scsi_dma_need_drain,	\
1480 	.this_id		= ATA_SHT_THIS_ID,		\
1481 	.emulated		= ATA_SHT_EMULATED,		\
1482 	.proc_name		= drv_name,			\
1483 	.sdev_init		= ata_scsi_sdev_init,		\
1484 	.sdev_destroy		= ata_scsi_sdev_destroy,	\
1485 	.bios_param		= ata_std_bios_param,		\
1486 	.unlock_native_capacity	= ata_scsi_unlock_native_capacity,\
1487 	.max_sectors		= ATA_MAX_SECTORS_LBA48
1488 
1489 #define ATA_SUBBASE_SHT(drv_name)				\
1490 	__ATA_BASE_SHT(drv_name),				\
1491 	.can_queue		= ATA_DEF_QUEUE,		\
1492 	.tag_alloc_policy_rr	= true,				\
1493 	.sdev_configure		= ata_scsi_sdev_configure
1494 
1495 #define ATA_SUBBASE_SHT_QD(drv_name, drv_qd)			\
1496 	__ATA_BASE_SHT(drv_name),				\
1497 	.can_queue		= drv_qd,			\
1498 	.tag_alloc_policy_rr	= true,				\
1499 	.sdev_configure		= ata_scsi_sdev_configure
1500 
1501 #define ATA_BASE_SHT(drv_name)					\
1502 	ATA_SUBBASE_SHT(drv_name),				\
1503 	.sdev_groups		= ata_common_sdev_groups
1504 
1505 #ifdef CONFIG_SATA_HOST
1506 extern const struct attribute_group *ata_ncq_sdev_groups[];
1507 
1508 #define ATA_NCQ_SHT(drv_name)					\
1509 	ATA_SUBBASE_SHT(drv_name),				\
1510 	.sdev_groups		= ata_ncq_sdev_groups,		\
1511 	.change_queue_depth	= ata_scsi_change_queue_depth
1512 
1513 #define ATA_NCQ_SHT_QD(drv_name, drv_qd)			\
1514 	ATA_SUBBASE_SHT_QD(drv_name, drv_qd),			\
1515 	.sdev_groups		= ata_ncq_sdev_groups,		\
1516 	.change_queue_depth	= ata_scsi_change_queue_depth
1517 #endif
1518 
1519 /*
1520  * PMP helpers
1521  */
1522 #ifdef CONFIG_SATA_PMP
1523 static inline bool sata_pmp_supported(struct ata_port *ap)
1524 {
1525 	return ap->flags & ATA_FLAG_PMP;
1526 }
1527 
1528 static inline bool sata_pmp_attached(struct ata_port *ap)
1529 {
1530 	return ap->nr_pmp_links != 0;
1531 }
1532 
1533 static inline bool ata_is_host_link(const struct ata_link *link)
1534 {
1535 	return link == &link->ap->link || link == link->ap->slave_link;
1536 }
1537 #else /* CONFIG_SATA_PMP */
1538 static inline bool sata_pmp_supported(struct ata_port *ap)
1539 {
1540 	return false;
1541 }
1542 
1543 static inline bool sata_pmp_attached(struct ata_port *ap)
1544 {
1545 	return false;
1546 }
1547 
1548 static inline bool ata_is_host_link(const struct ata_link *link)
1549 {
1550 	return true;
1551 }
1552 #endif /* CONFIG_SATA_PMP */
1553 
1554 static inline int sata_srst_pmp(struct ata_link *link)
1555 {
1556 	if (sata_pmp_supported(link->ap) && ata_is_host_link(link))
1557 		return SATA_PMP_CTRL_PORT;
1558 	return link->pmp;
1559 }
1560 
1561 #define ata_port_printk(level, ap, fmt, ...)			\
1562 	pr_ ## level ("ata%u: " fmt, (ap)->print_id, ##__VA_ARGS__)
1563 
1564 #define ata_port_err(ap, fmt, ...)				\
1565 	ata_port_printk(err, ap, fmt, ##__VA_ARGS__)
1566 #define ata_port_warn(ap, fmt, ...)				\
1567 	ata_port_printk(warn, ap, fmt, ##__VA_ARGS__)
1568 #define ata_port_notice(ap, fmt, ...)				\
1569 	ata_port_printk(notice, ap, fmt, ##__VA_ARGS__)
1570 #define ata_port_info(ap, fmt, ...)				\
1571 	ata_port_printk(info, ap, fmt, ##__VA_ARGS__)
1572 #define ata_port_dbg(ap, fmt, ...)				\
1573 	ata_port_printk(debug, ap, fmt, ##__VA_ARGS__)
1574 
1575 #define ata_link_printk(level, link, fmt, ...)			\
1576 do {								\
1577 	if (sata_pmp_attached((link)->ap) ||			\
1578 	    (link)->ap->slave_link)				\
1579 		pr_ ## level ("ata%u.%02u: " fmt,		\
1580 			      (link)->ap->print_id,		\
1581 			      (link)->pmp,			\
1582 			      ##__VA_ARGS__);			\
1583         else							\
1584 		pr_ ## level ("ata%u: " fmt,			\
1585 			      (link)->ap->print_id,		\
1586 			      ##__VA_ARGS__);			\
1587 } while (0)
1588 
1589 #define ata_link_err(link, fmt, ...)				\
1590 	ata_link_printk(err, link, fmt, ##__VA_ARGS__)
1591 #define ata_link_warn(link, fmt, ...)				\
1592 	ata_link_printk(warn, link, fmt, ##__VA_ARGS__)
1593 #define ata_link_notice(link, fmt, ...)				\
1594 	ata_link_printk(notice, link, fmt, ##__VA_ARGS__)
1595 #define ata_link_info(link, fmt, ...)				\
1596 	ata_link_printk(info, link, fmt, ##__VA_ARGS__)
1597 #define ata_link_dbg(link, fmt, ...)				\
1598 	ata_link_printk(debug, link, fmt, ##__VA_ARGS__)
1599 
1600 #define ata_dev_printk(level, dev, fmt, ...)			\
1601         pr_ ## level("ata%u.%02u: " fmt,			\
1602                (dev)->link->ap->print_id,			\
1603 	       (dev)->link->pmp + (dev)->devno,			\
1604 	       ##__VA_ARGS__)
1605 
1606 #define ata_dev_err(dev, fmt, ...)				\
1607 	ata_dev_printk(err, dev, fmt, ##__VA_ARGS__)
1608 #define ata_dev_warn(dev, fmt, ...)				\
1609 	ata_dev_printk(warn, dev, fmt, ##__VA_ARGS__)
1610 #define ata_dev_notice(dev, fmt, ...)				\
1611 	ata_dev_printk(notice, dev, fmt, ##__VA_ARGS__)
1612 #define ata_dev_info(dev, fmt, ...)				\
1613 	ata_dev_printk(info, dev, fmt, ##__VA_ARGS__)
1614 #define ata_dev_dbg(dev, fmt, ...)				\
1615 	ata_dev_printk(debug, dev, fmt, ##__VA_ARGS__)
1616 
1617 #define ata_dev_warn_once(dev, fmt, ...)			\
1618 	pr_warn_once("ata%u.%02u: " fmt,			\
1619 		     (dev)->link->ap->print_id,			\
1620 		     (dev)->link->pmp + (dev)->devno,		\
1621 		     ##__VA_ARGS__)
1622 
1623 static inline void ata_print_version_once(const struct device *dev,
1624 					  const char *version)
1625 {
1626 	dev_dbg_once(dev, "version %s\n", version);
1627 }
1628 
1629 /*
1630  * ata_eh_info helpers
1631  */
1632 extern __printf(2, 3)
1633 void __ata_ehi_push_desc(struct ata_eh_info *ehi, const char *fmt, ...);
1634 extern __printf(2, 3)
1635 void ata_ehi_push_desc(struct ata_eh_info *ehi, const char *fmt, ...);
1636 extern void ata_ehi_clear_desc(struct ata_eh_info *ehi);
1637 
1638 static inline void ata_ehi_hotplugged(struct ata_eh_info *ehi)
1639 {
1640 	ehi->probe_mask |= (1 << ATA_MAX_DEVICES) - 1;
1641 	ehi->flags |= ATA_EHI_HOTPLUGGED;
1642 	ehi->action |= ATA_EH_RESET | ATA_EH_ENABLE_LINK;
1643 	ehi->err_mask |= AC_ERR_ATA_BUS;
1644 }
1645 
1646 /*
1647  * port description helpers
1648  */
1649 extern __printf(2, 3)
1650 void ata_port_desc(struct ata_port *ap, const char *fmt, ...);
1651 #ifdef CONFIG_PCI
1652 extern void ata_port_pbar_desc(struct ata_port *ap, int bar, ssize_t offset,
1653 			       const char *name);
1654 #endif
1655 static inline void ata_port_desc_misc(struct ata_port *ap, int irq)
1656 {
1657 	ata_port_desc(ap, "irq %d", irq);
1658 	ata_port_desc(ap, "lpm-pol %d", ap->target_lpm_policy);
1659 	if (ap->pflags & ATA_PFLAG_EXTERNAL)
1660 		ata_port_desc(ap, "ext");
1661 }
1662 
1663 static inline bool ata_tag_internal(unsigned int tag)
1664 {
1665 	return tag == ATA_TAG_INTERNAL;
1666 }
1667 
1668 static inline bool ata_tag_valid(unsigned int tag)
1669 {
1670 	return tag < ATA_MAX_QUEUE || ata_tag_internal(tag);
1671 }
1672 
1673 #define __ata_qc_for_each(ap, qc, tag, max_tag, fn)		\
1674 	for ((tag) = 0; (tag) < (max_tag) &&			\
1675 	     ({ qc = fn((ap), (tag)); 1; }); (tag)++)		\
1676 
1677 /*
1678  * Internal use only, iterate commands ignoring error handling and
1679  * status of 'qc'.
1680  */
1681 #define ata_qc_for_each_raw(ap, qc, tag)					\
1682 	__ata_qc_for_each(ap, qc, tag, ATA_MAX_QUEUE, __ata_qc_from_tag)
1683 
1684 /*
1685  * Iterate all potential commands that can be queued
1686  */
1687 #define ata_qc_for_each(ap, qc, tag)					\
1688 	__ata_qc_for_each(ap, qc, tag, ATA_MAX_QUEUE, ata_qc_from_tag)
1689 
1690 /*
1691  * Like ata_qc_for_each, but with the internal tag included
1692  */
1693 #define ata_qc_for_each_with_internal(ap, qc, tag)			\
1694 	__ata_qc_for_each(ap, qc, tag, ATA_MAX_QUEUE + 1, ata_qc_from_tag)
1695 
1696 /*
1697  * device helpers
1698  */
1699 static inline unsigned int ata_class_enabled(unsigned int class)
1700 {
1701 	return class == ATA_DEV_ATA || class == ATA_DEV_ATAPI ||
1702 		class == ATA_DEV_PMP || class == ATA_DEV_SEMB ||
1703 		class == ATA_DEV_ZAC;
1704 }
1705 
1706 static inline unsigned int ata_class_disabled(unsigned int class)
1707 {
1708 	return class == ATA_DEV_ATA_UNSUP || class == ATA_DEV_ATAPI_UNSUP ||
1709 		class == ATA_DEV_PMP_UNSUP || class == ATA_DEV_SEMB_UNSUP ||
1710 		class == ATA_DEV_ZAC_UNSUP;
1711 }
1712 
1713 static inline unsigned int ata_class_absent(unsigned int class)
1714 {
1715 	return !ata_class_enabled(class) && !ata_class_disabled(class);
1716 }
1717 
1718 static inline unsigned int ata_dev_enabled(const struct ata_device *dev)
1719 {
1720 	return ata_class_enabled(dev->class);
1721 }
1722 
1723 static inline unsigned int ata_dev_disabled(const struct ata_device *dev)
1724 {
1725 	return ata_class_disabled(dev->class);
1726 }
1727 
1728 static inline unsigned int ata_dev_absent(const struct ata_device *dev)
1729 {
1730 	return ata_class_absent(dev->class);
1731 }
1732 
1733 /*
1734  * link helpers
1735  */
1736 static inline int ata_link_max_devices(const struct ata_link *link)
1737 {
1738 	if (ata_is_host_link(link) && link->ap->flags & ATA_FLAG_SLAVE_POSS)
1739 		return 2;
1740 	return 1;
1741 }
1742 
1743 static inline int ata_link_active(struct ata_link *link)
1744 {
1745 	return ata_tag_valid(link->active_tag) || link->sactive;
1746 }
1747 
1748 /*
1749  * Iterators
1750  *
1751  * ATA_LITER_* constants are used to select link iteration mode and
1752  * ATA_DITER_* device iteration mode.
1753  *
1754  * For a custom iteration directly using ata_{link|dev}_next(), if
1755  * @link or @dev, respectively, is NULL, the first element is
1756  * returned.  @dev and @link can be any valid device or link and the
1757  * next element according to the iteration mode will be returned.
1758  * After the last element, NULL is returned.
1759  */
1760 enum ata_link_iter_mode {
1761 	ATA_LITER_EDGE,		/* if present, PMP links only; otherwise,
1762 				 * host link.  no slave link */
1763 	ATA_LITER_HOST_FIRST,	/* host link followed by PMP or slave links */
1764 	ATA_LITER_PMP_FIRST,	/* PMP links followed by host link,
1765 				 * slave link still comes after host link */
1766 };
1767 
1768 enum ata_dev_iter_mode {
1769 	ATA_DITER_ENABLED,
1770 	ATA_DITER_ENABLED_REVERSE,
1771 	ATA_DITER_ALL,
1772 	ATA_DITER_ALL_REVERSE,
1773 };
1774 
1775 extern struct ata_link *ata_link_next(struct ata_link *link,
1776 				      struct ata_port *ap,
1777 				      enum ata_link_iter_mode mode);
1778 
1779 extern struct ata_device *ata_dev_next(struct ata_device *dev,
1780 				       struct ata_link *link,
1781 				       enum ata_dev_iter_mode mode);
1782 
1783 /*
1784  * Shortcut notation for iterations
1785  *
1786  * ata_for_each_link() iterates over each link of @ap according to
1787  * @mode.  @link points to the current link in the loop.  @link is
1788  * NULL after loop termination.  ata_for_each_dev() works the same way
1789  * except that it iterates over each device of @link.
1790  *
1791  * Note that the mode prefixes ATA_{L|D}ITER_ shouldn't need to be
1792  * specified when using the following shorthand notations.  Only the
1793  * mode itself (EDGE, HOST_FIRST, ENABLED, etc...) should be
1794  * specified.  This not only increases brevity but also makes it
1795  * impossible to use ATA_LITER_* for device iteration or vice-versa.
1796  */
1797 #define ata_for_each_link(link, ap, mode) \
1798 	for ((link) = ata_link_next(NULL, (ap), ATA_LITER_##mode); (link); \
1799 	     (link) = ata_link_next((link), (ap), ATA_LITER_##mode))
1800 
1801 #define ata_for_each_dev(dev, link, mode) \
1802 	for ((dev) = ata_dev_next(NULL, (link), ATA_DITER_##mode); (dev); \
1803 	     (dev) = ata_dev_next((dev), (link), ATA_DITER_##mode))
1804 
1805 /**
1806  *	ata_ncq_supported - Test whether NCQ is supported
1807  *	@dev: ATA device to test
1808  *
1809  *	LOCKING:
1810  *	spin_lock_irqsave(host lock)
1811  *
1812  *	RETURNS:
1813  *	true if @dev supports NCQ, false otherwise.
1814  */
1815 static inline bool ata_ncq_supported(struct ata_device *dev)
1816 {
1817 	if (!IS_ENABLED(CONFIG_SATA_HOST))
1818 		return false;
1819 	return (dev->flags & (ATA_DFLAG_PIO | ATA_DFLAG_NCQ)) == ATA_DFLAG_NCQ;
1820 }
1821 
1822 /**
1823  *	ata_ncq_enabled - Test whether NCQ is enabled
1824  *	@dev: ATA device to test
1825  *
1826  *	LOCKING:
1827  *	spin_lock_irqsave(host lock)
1828  *
1829  *	RETURNS:
1830  *	true if NCQ is enabled for @dev, false otherwise.
1831  */
1832 static inline bool ata_ncq_enabled(struct ata_device *dev)
1833 {
1834 	return ata_ncq_supported(dev) && !(dev->flags & ATA_DFLAG_NCQ_OFF);
1835 }
1836 
1837 static inline bool ata_fpdma_dsm_supported(struct ata_device *dev)
1838 {
1839 	return (dev->flags & ATA_DFLAG_NCQ_SEND_RECV) &&
1840 		(dev->ncq_send_recv_cmds[ATA_LOG_NCQ_SEND_RECV_DSM_OFFSET] &
1841 		 ATA_LOG_NCQ_SEND_RECV_DSM_TRIM);
1842 }
1843 
1844 static inline bool ata_fpdma_read_log_supported(struct ata_device *dev)
1845 {
1846 	return (dev->flags & ATA_DFLAG_NCQ_SEND_RECV) &&
1847 		(dev->ncq_send_recv_cmds[ATA_LOG_NCQ_SEND_RECV_RD_LOG_OFFSET] &
1848 		 ATA_LOG_NCQ_SEND_RECV_RD_LOG_SUPPORTED);
1849 }
1850 
1851 static inline bool ata_fpdma_zac_mgmt_in_supported(struct ata_device *dev)
1852 {
1853 	return (dev->flags & ATA_DFLAG_NCQ_SEND_RECV) &&
1854 		(dev->ncq_send_recv_cmds[ATA_LOG_NCQ_SEND_RECV_ZAC_MGMT_OFFSET] &
1855 		ATA_LOG_NCQ_SEND_RECV_ZAC_MGMT_IN_SUPPORTED);
1856 }
1857 
1858 static inline bool ata_fpdma_zac_mgmt_out_supported(struct ata_device *dev)
1859 {
1860 	return (dev->ncq_non_data_cmds[ATA_LOG_NCQ_NON_DATA_ZAC_MGMT_OFFSET] &
1861 		ATA_LOG_NCQ_NON_DATA_ZAC_MGMT_OUT);
1862 }
1863 
1864 static inline void ata_qc_set_polling(struct ata_queued_cmd *qc)
1865 {
1866 	qc->tf.ctl |= ATA_NIEN;
1867 }
1868 
1869 static inline struct ata_queued_cmd *__ata_qc_from_tag(struct ata_port *ap,
1870 						       unsigned int tag)
1871 {
1872 	if (ata_tag_valid(tag))
1873 		return &ap->qcmd[tag];
1874 	return NULL;
1875 }
1876 
1877 static inline struct ata_queued_cmd *ata_qc_from_tag(struct ata_port *ap,
1878 						     unsigned int tag)
1879 {
1880 	struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
1881 
1882 	if (unlikely(!qc))
1883 		return qc;
1884 
1885 	if ((qc->flags & (ATA_QCFLAG_ACTIVE |
1886 			  ATA_QCFLAG_EH)) == ATA_QCFLAG_ACTIVE)
1887 		return qc;
1888 
1889 	return NULL;
1890 }
1891 
1892 static inline unsigned int ata_qc_raw_nbytes(struct ata_queued_cmd *qc)
1893 {
1894 	return qc->nbytes - min(qc->extrabytes, qc->nbytes);
1895 }
1896 
1897 static inline void ata_tf_init(struct ata_device *dev, struct ata_taskfile *tf)
1898 {
1899 	memset(tf, 0, sizeof(*tf));
1900 
1901 #ifdef CONFIG_ATA_SFF
1902 	tf->ctl = dev->link->ap->ctl;
1903 #else
1904 	tf->ctl = ATA_DEVCTL_OBS;
1905 #endif
1906 	if (dev->devno == 0)
1907 		tf->device = ATA_DEVICE_OBS;
1908 	else
1909 		tf->device = ATA_DEVICE_OBS | ATA_DEV1;
1910 }
1911 
1912 static inline void ata_qc_reinit(struct ata_queued_cmd *qc)
1913 {
1914 	qc->dma_dir = DMA_NONE;
1915 	qc->sg = NULL;
1916 	qc->flags = 0;
1917 	qc->cursg = NULL;
1918 	qc->cursg_ofs = 0;
1919 	qc->nbytes = qc->extrabytes = qc->curbytes = 0;
1920 	qc->n_elem = 0;
1921 	qc->err_mask = 0;
1922 	qc->sect_size = ATA_SECT_SIZE;
1923 
1924 	ata_tf_init(qc->dev, &qc->tf);
1925 
1926 	/* init result_tf such that it indicates normal completion */
1927 	qc->result_tf.command = ATA_DRDY;
1928 	qc->result_tf.feature = 0;
1929 }
1930 
1931 static inline int ata_try_flush_cache(const struct ata_device *dev)
1932 {
1933 	return ata_id_wcache_enabled(dev->id) ||
1934 	       ata_id_has_flush(dev->id) ||
1935 	       ata_id_has_flush_ext(dev->id);
1936 }
1937 
1938 static inline unsigned int ac_err_mask(u8 status)
1939 {
1940 	if (status & (ATA_BUSY | ATA_DRQ))
1941 		return AC_ERR_HSM;
1942 	if (status & (ATA_ERR | ATA_DF))
1943 		return AC_ERR_DEV;
1944 	return 0;
1945 }
1946 
1947 static inline unsigned int __ac_err_mask(u8 status)
1948 {
1949 	unsigned int mask = ac_err_mask(status);
1950 	if (mask == 0)
1951 		return AC_ERR_OTHER;
1952 	return mask;
1953 }
1954 
1955 static inline struct ata_port *ata_shost_to_port(struct Scsi_Host *host)
1956 {
1957 	return *(struct ata_port **)&host->hostdata[0];
1958 }
1959 
1960 static inline int ata_check_ready(u8 status)
1961 {
1962 	if (!(status & ATA_BUSY))
1963 		return 1;
1964 
1965 	/* 0xff indicates either no device or device not ready */
1966 	if (status == 0xff)
1967 		return -ENODEV;
1968 
1969 	return 0;
1970 }
1971 
1972 static inline unsigned long ata_deadline(unsigned long from_jiffies,
1973 					 unsigned int timeout_msecs)
1974 {
1975 	return from_jiffies + msecs_to_jiffies(timeout_msecs);
1976 }
1977 
1978 /* Don't open code these in drivers as there are traps. Firstly the range may
1979    change in future hardware and specs, secondly 0xFF means 'no DMA' but is
1980    > UDMA_0. Dyma ddreigiau */
1981 
1982 static inline bool ata_using_mwdma(struct ata_device *adev)
1983 {
1984 	return adev->dma_mode >= XFER_MW_DMA_0 &&
1985 		adev->dma_mode <= XFER_MW_DMA_4;
1986 }
1987 
1988 static inline bool ata_using_udma(struct ata_device *adev)
1989 {
1990 	return adev->dma_mode >= XFER_UDMA_0 &&
1991 		adev->dma_mode <= XFER_UDMA_7;
1992 }
1993 
1994 static inline bool ata_dma_enabled(struct ata_device *adev)
1995 {
1996 	return adev->dma_mode != 0xFF;
1997 }
1998 
1999 /**************************************************************************
2000  * PATA timings - drivers/ata/libata-pata-timings.c
2001  */
2002 extern const struct ata_timing *ata_timing_find_mode(u8 xfer_mode);
2003 extern int ata_timing_compute(struct ata_device *, unsigned short,
2004 			      struct ata_timing *, int, int);
2005 extern void ata_timing_merge(const struct ata_timing *,
2006 			     const struct ata_timing *, struct ata_timing *,
2007 			     unsigned int);
2008 
2009 /**************************************************************************
2010  * PMP - drivers/ata/libata-pmp.c
2011  */
2012 #ifdef CONFIG_SATA_PMP
2013 
2014 extern const struct ata_port_operations sata_pmp_port_ops;
2015 
2016 extern int sata_pmp_qc_defer_cmd_switch(struct ata_queued_cmd *qc);
2017 extern void sata_pmp_error_handler(struct ata_port *ap)
2018 	__must_hold(&ap->host->eh_mutex);
2019 
2020 #else /* CONFIG_SATA_PMP */
2021 
2022 #define sata_pmp_port_ops		sata_port_ops
2023 #define sata_pmp_qc_defer_cmd_switch	ata_std_qc_defer
2024 #define sata_pmp_error_handler		ata_std_error_handler
2025 
2026 #endif /* CONFIG_SATA_PMP */
2027 
2028 
2029 /**************************************************************************
2030  * SFF - drivers/ata/libata-sff.c
2031  */
2032 #ifdef CONFIG_ATA_SFF
2033 
2034 extern const struct ata_port_operations ata_sff_port_ops;
2035 extern const struct ata_port_operations ata_bmdma32_port_ops;
2036 
2037 /* PIO only, sg_tablesize and dma_boundary limits can be removed */
2038 #define ATA_PIO_SHT(drv_name)					\
2039 	ATA_BASE_SHT(drv_name),					\
2040 	.sg_tablesize		= LIBATA_MAX_PRD,		\
2041 	.dma_boundary		= ATA_DMA_BOUNDARY
2042 
2043 extern void ata_sff_dev_select(struct ata_port *ap, unsigned int device);
2044 extern u8 ata_sff_check_status(struct ata_port *ap);
2045 extern void ata_sff_pause(struct ata_port *ap);
2046 extern void ata_sff_dma_pause(struct ata_port *ap);
2047 extern int ata_sff_wait_ready(struct ata_link *link, unsigned long deadline);
2048 extern void ata_sff_tf_load(struct ata_port *ap, const struct ata_taskfile *tf);
2049 extern void ata_sff_tf_read(struct ata_port *ap, struct ata_taskfile *tf);
2050 extern void ata_sff_exec_command(struct ata_port *ap,
2051 				 const struct ata_taskfile *tf);
2052 extern unsigned int ata_sff_data_xfer(struct ata_queued_cmd *qc,
2053 			unsigned char *buf, unsigned int buflen, int rw);
2054 extern unsigned int ata_sff_data_xfer32(struct ata_queued_cmd *qc,
2055 			unsigned char *buf, unsigned int buflen, int rw);
2056 extern void ata_sff_irq_on(struct ata_port *ap);
2057 extern int ata_sff_hsm_move(struct ata_port *ap, struct ata_queued_cmd *qc,
2058 			    u8 status, int in_wq);
2059 extern void ata_sff_queue_work(struct work_struct *work);
2060 extern void ata_sff_queue_delayed_work(struct delayed_work *dwork,
2061 		unsigned long delay);
2062 extern void ata_sff_queue_pio_task(struct ata_link *link, unsigned long delay);
2063 extern unsigned int ata_sff_qc_issue(struct ata_queued_cmd *qc);
2064 extern void ata_sff_qc_fill_rtf(struct ata_queued_cmd *qc);
2065 extern unsigned int ata_sff_port_intr(struct ata_port *ap,
2066 				      struct ata_queued_cmd *qc);
2067 extern irqreturn_t ata_sff_interrupt(int irq, void *dev_instance);
2068 extern void ata_sff_lost_interrupt(struct ata_port *ap);
2069 extern void ata_sff_freeze(struct ata_port *ap);
2070 extern void ata_sff_thaw(struct ata_port *ap);
2071 extern int ata_sff_prereset(struct ata_link *link, unsigned long deadline);
2072 extern unsigned int ata_sff_dev_classify(struct ata_device *dev, int present,
2073 					  u8 *r_err);
2074 extern int ata_sff_wait_after_reset(struct ata_link *link, unsigned int devmask,
2075 				    unsigned long deadline);
2076 extern int ata_sff_softreset(struct ata_link *link, unsigned int *classes,
2077 			     unsigned long deadline);
2078 extern int sata_sff_hardreset(struct ata_link *link, unsigned int *class,
2079 			       unsigned long deadline);
2080 extern void ata_sff_postreset(struct ata_link *link, unsigned int *classes);
2081 extern void ata_sff_drain_fifo(struct ata_queued_cmd *qc);
2082 extern void ata_sff_error_handler(struct ata_port *ap)
2083 	__must_hold(&ap->host->eh_mutex);
2084 extern void ata_sff_std_ports(struct ata_ioports *ioaddr);
2085 #ifdef CONFIG_PCI
2086 extern int ata_pci_sff_init_host(struct ata_host *host);
2087 extern int ata_pci_sff_prepare_host(struct pci_dev *pdev,
2088 				    const struct ata_port_info * const * ppi,
2089 				    struct ata_host **r_host);
2090 extern int ata_pci_sff_activate_host(struct ata_host *host,
2091 				     irq_handler_t irq_handler,
2092 				     const struct scsi_host_template *sht);
2093 extern int ata_pci_sff_init_one(struct pci_dev *pdev,
2094 		const struct ata_port_info * const * ppi,
2095 		const struct scsi_host_template *sht, void *host_priv, int hflags);
2096 #endif /* CONFIG_PCI */
2097 
2098 #ifdef CONFIG_ATA_BMDMA
2099 
2100 extern const struct ata_port_operations ata_bmdma_port_ops;
2101 
2102 #define ATA_BMDMA_SHT(drv_name)					\
2103 	ATA_BASE_SHT(drv_name),					\
2104 	.sg_tablesize		= LIBATA_MAX_PRD,		\
2105 	.dma_boundary		= ATA_DMA_BOUNDARY
2106 
2107 extern enum ata_completion_errors ata_bmdma_qc_prep(struct ata_queued_cmd *qc);
2108 extern unsigned int ata_bmdma_qc_issue(struct ata_queued_cmd *qc);
2109 extern enum ata_completion_errors ata_bmdma_dumb_qc_prep(struct ata_queued_cmd *qc);
2110 extern unsigned int ata_bmdma_port_intr(struct ata_port *ap,
2111 				      struct ata_queued_cmd *qc);
2112 extern irqreturn_t ata_bmdma_interrupt(int irq, void *dev_instance);
2113 extern void ata_bmdma_error_handler(struct ata_port *ap)
2114 	__must_hold(&ap->host->eh_mutex);
2115 extern void ata_bmdma_post_internal_cmd(struct ata_queued_cmd *qc);
2116 extern void ata_bmdma_irq_clear(struct ata_port *ap);
2117 extern void ata_bmdma_setup(struct ata_queued_cmd *qc);
2118 extern void ata_bmdma_start(struct ata_queued_cmd *qc);
2119 extern void ata_bmdma_stop(struct ata_queued_cmd *qc);
2120 extern u8 ata_bmdma_status(struct ata_port *ap);
2121 extern int ata_bmdma_port_start(struct ata_port *ap);
2122 extern int ata_bmdma_port_start32(struct ata_port *ap);
2123 
2124 #ifdef CONFIG_PCI
2125 extern int ata_pci_bmdma_clear_simplex(struct pci_dev *pdev);
2126 extern void ata_pci_bmdma_init(struct ata_host *host);
2127 extern int ata_pci_bmdma_prepare_host(struct pci_dev *pdev,
2128 				      const struct ata_port_info * const * ppi,
2129 				      struct ata_host **r_host);
2130 extern int ata_pci_bmdma_init_one(struct pci_dev *pdev,
2131 				  const struct ata_port_info * const * ppi,
2132 				  const struct scsi_host_template *sht,
2133 				  void *host_priv, int hflags);
2134 #endif /* CONFIG_PCI */
2135 #endif /* CONFIG_ATA_BMDMA */
2136 
2137 /**
2138  *	ata_sff_busy_wait - Wait for a port status register
2139  *	@ap: Port to wait for.
2140  *	@bits: bits that must be clear
2141  *	@max: number of 10uS waits to perform
2142  *
2143  *	Waits up to max*10 microseconds for the selected bits in the port's
2144  *	status register to be cleared.
2145  *	Returns final value of status register.
2146  *
2147  *	LOCKING:
2148  *	Inherited from caller.
2149  */
2150 static inline u8 ata_sff_busy_wait(struct ata_port *ap, unsigned int bits,
2151 				   unsigned int max)
2152 {
2153 	u8 status;
2154 
2155 	do {
2156 		udelay(10);
2157 		status = ap->ops->sff_check_status(ap);
2158 		max--;
2159 	} while (status != 0xff && (status & bits) && (max > 0));
2160 
2161 	return status;
2162 }
2163 
2164 /**
2165  *	ata_wait_idle - Wait for a port to be idle.
2166  *	@ap: Port to wait for.
2167  *
2168  *	Waits up to 10ms for port's BUSY and DRQ signals to clear.
2169  *	Returns final value of status register.
2170  *
2171  *	LOCKING:
2172  *	Inherited from caller.
2173  */
2174 static inline u8 ata_wait_idle(struct ata_port *ap)
2175 {
2176 	u8 status = ata_sff_busy_wait(ap, ATA_BUSY | ATA_DRQ, 1000);
2177 
2178 	if (status != 0xff && (status & (ATA_BUSY | ATA_DRQ)))
2179 		ata_port_dbg(ap, "abnormal Status 0x%X\n", status);
2180 
2181 	return status;
2182 }
2183 #else /* CONFIG_ATA_SFF */
2184 static inline int sata_sff_hardreset(struct ata_link *link, unsigned int *class,
2185 				     unsigned long deadline)
2186 {
2187 	return -EOPNOTSUPP;
2188 }
2189 #endif /* CONFIG_ATA_SFF */
2190 
2191 #endif /* __LINUX_LIBATA_H__ */
2192