xref: /linux/drivers/ata/ahci.c (revision 20bed0d6ff62d985fe8222958825504b8aad4e88)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *  ahci.c - AHCI SATA support
4  *
5  *  Maintained by:  Tejun Heo <tj@kernel.org>
6  *    		    Please ALWAYS copy linux-ide@vger.kernel.org
7  *		    on emails.
8  *
9  *  Copyright 2004-2005 Red Hat, Inc.
10  *
11  * libata documentation is available via 'make {ps|pdf}docs',
12  * as Documentation/driver-api/libata.rst
13  *
14  * AHCI hardware documentation:
15  * http://www.intel.com/technology/serialata/pdf/rev1_0.pdf
16  * http://www.intel.com/technology/serialata/pdf/rev1_1.pdf
17  */
18 
19 #include <linux/kernel.h>
20 #include <linux/module.h>
21 #include <linux/pci.h>
22 #include <linux/blkdev.h>
23 #include <linux/delay.h>
24 #include <linux/interrupt.h>
25 #include <linux/dma-mapping.h>
26 #include <linux/device.h>
27 #include <linux/dmi.h>
28 #include <linux/gfp.h>
29 #include <linux/pm_runtime.h>
30 #include <scsi/scsi_host.h>
31 #include <scsi/scsi_cmnd.h>
32 #include <linux/libata.h>
33 #include <linux/ahci-remap.h>
34 #include <linux/io-64-nonatomic-lo-hi.h>
35 #include "ahci.h"
36 
37 #define DRV_NAME	"ahci"
38 #define DRV_VERSION	"3.0"
39 
40 enum {
41 	AHCI_PCI_BAR_STA2X11	= 0,
42 	AHCI_PCI_BAR_CAVIUM	= 0,
43 	AHCI_PCI_BAR_LOONGSON	= 0,
44 	AHCI_PCI_BAR_ENMOTUS	= 2,
45 	AHCI_PCI_BAR_CAVIUM_GEN5	= 4,
46 	AHCI_PCI_BAR_STANDARD	= 5,
47 };
48 
49 enum board_ids {
50 	/* board IDs by feature in alphabetical order */
51 	board_ahci,
52 	board_ahci_43bit_dma,
53 	board_ahci_ign_iferr,
54 	board_ahci_no_debounce_delay,
55 	board_ahci_no_msi,
56 	/*
57 	 * board_ahci_pcs_quirk is for legacy Intel platforms.
58 	 * Modern Intel platforms should use board_ahci instead.
59 	 * (Some modern Intel platforms might have been added with
60 	 * board_ahci_pcs_quirk, however, we cannot change them to board_ahci
61 	 * without testing that the platform actually works without the quirk.)
62 	 */
63 	board_ahci_pcs_quirk,
64 	board_ahci_pcs_quirk_no_devslp,
65 	board_ahci_pcs_quirk_no_sntf,
66 	board_ahci_yes_fbs,
67 	board_ahci_yes_fbs_atapi_dma,
68 
69 	/* board IDs for specific chipsets in alphabetical order */
70 	board_ahci_al,
71 	board_ahci_avn,
72 	board_ahci_mcp65,
73 	board_ahci_mcp77,
74 	board_ahci_mcp89,
75 	board_ahci_mv,
76 	board_ahci_sb600,
77 	board_ahci_sb700,	/* for SB700 and SB800 */
78 	board_ahci_vt8251,
79 
80 	/* aliases */
81 	board_ahci_mcp_linux	= board_ahci_mcp65,
82 	board_ahci_mcp67	= board_ahci_mcp65,
83 	board_ahci_mcp73	= board_ahci_mcp65,
84 	board_ahci_mcp79	= board_ahci_mcp77,
85 };
86 
87 static int ahci_init_one(struct pci_dev *pdev, const struct pci_device_id *ent);
88 static void ahci_remove_one(struct pci_dev *dev);
89 static void ahci_shutdown_one(struct pci_dev *dev);
90 static void ahci_intel_pcs_quirk(struct pci_dev *pdev, struct ahci_host_priv *hpriv);
91 static int ahci_vt8251_hardreset(struct ata_link *link, unsigned int *class,
92 				 unsigned long deadline);
93 static int ahci_avn_hardreset(struct ata_link *link, unsigned int *class,
94 			      unsigned long deadline);
95 static void ahci_mcp89_apple_enable(struct pci_dev *pdev);
96 static bool is_mcp89_apple(struct pci_dev *pdev);
97 static int ahci_p5wdh_hardreset(struct ata_link *link, unsigned int *class,
98 				unsigned long deadline);
99 #ifdef CONFIG_PM
100 static int ahci_pci_device_runtime_suspend(struct device *dev);
101 static int ahci_pci_device_runtime_resume(struct device *dev);
102 #ifdef CONFIG_PM_SLEEP
103 static int ahci_pci_device_suspend(struct device *dev);
104 static int ahci_pci_device_resume(struct device *dev);
105 #endif
106 #endif /* CONFIG_PM */
107 
108 static const struct scsi_host_template ahci_sht = {
109 	AHCI_SHT("ahci"),
110 };
111 
112 static struct ata_port_operations ahci_vt8251_ops = {
113 	.inherits		= &ahci_ops,
114 	.reset.hardreset	= ahci_vt8251_hardreset,
115 };
116 
117 static struct ata_port_operations ahci_p5wdh_ops = {
118 	.inherits		= &ahci_ops,
119 	.reset.hardreset	= ahci_p5wdh_hardreset,
120 };
121 
122 static struct ata_port_operations ahci_avn_ops = {
123 	.inherits		= &ahci_ops,
124 	.reset.hardreset	= ahci_avn_hardreset,
125 };
126 
127 static const struct ata_port_info ahci_port_info[] = {
128 	/* by features */
129 	[board_ahci] = {
130 		.flags		= AHCI_FLAG_COMMON,
131 		.pio_mask	= ATA_PIO4,
132 		.udma_mask	= ATA_UDMA6,
133 		.port_ops	= &ahci_ops,
134 	},
135 	[board_ahci_43bit_dma] = {
136 		AHCI_HFLAGS	(AHCI_HFLAG_43BIT_ONLY),
137 		.flags		= AHCI_FLAG_COMMON,
138 		.pio_mask	= ATA_PIO4,
139 		.udma_mask	= ATA_UDMA6,
140 		.port_ops	= &ahci_ops,
141 	},
142 	[board_ahci_ign_iferr] = {
143 		AHCI_HFLAGS	(AHCI_HFLAG_IGN_IRQ_IF_ERR),
144 		.flags		= AHCI_FLAG_COMMON,
145 		.pio_mask	= ATA_PIO4,
146 		.udma_mask	= ATA_UDMA6,
147 		.port_ops	= &ahci_ops,
148 	},
149 	[board_ahci_no_debounce_delay] = {
150 		.flags		= AHCI_FLAG_COMMON,
151 		.link_flags	= ATA_LFLAG_NO_DEBOUNCE_DELAY,
152 		.pio_mask	= ATA_PIO4,
153 		.udma_mask	= ATA_UDMA6,
154 		.port_ops	= &ahci_ops,
155 	},
156 	[board_ahci_no_msi] = {
157 		AHCI_HFLAGS	(AHCI_HFLAG_NO_MSI),
158 		.flags		= AHCI_FLAG_COMMON,
159 		.pio_mask	= ATA_PIO4,
160 		.udma_mask	= ATA_UDMA6,
161 		.port_ops	= &ahci_ops,
162 	},
163 	[board_ahci_pcs_quirk] = {
164 		AHCI_HFLAGS	(AHCI_HFLAG_INTEL_PCS_QUIRK),
165 		.flags		= AHCI_FLAG_COMMON,
166 		.pio_mask	= ATA_PIO4,
167 		.udma_mask	= ATA_UDMA6,
168 		.port_ops	= &ahci_ops,
169 	},
170 	[board_ahci_pcs_quirk_no_devslp] = {
171 		AHCI_HFLAGS	(AHCI_HFLAG_INTEL_PCS_QUIRK |
172 				 AHCI_HFLAG_NO_DEVSLP),
173 		.flags		= AHCI_FLAG_COMMON,
174 		.pio_mask	= ATA_PIO4,
175 		.udma_mask	= ATA_UDMA6,
176 		.port_ops	= &ahci_ops,
177 	},
178 	[board_ahci_pcs_quirk_no_sntf] = {
179 		AHCI_HFLAGS	(AHCI_HFLAG_INTEL_PCS_QUIRK |
180 				 AHCI_HFLAG_NO_SNTF),
181 		.flags		= AHCI_FLAG_COMMON,
182 		.pio_mask	= ATA_PIO4,
183 		.udma_mask	= ATA_UDMA6,
184 		.port_ops	= &ahci_ops,
185 	},
186 	[board_ahci_yes_fbs] = {
187 		AHCI_HFLAGS	(AHCI_HFLAG_YES_FBS),
188 		.flags		= AHCI_FLAG_COMMON,
189 		.pio_mask	= ATA_PIO4,
190 		.udma_mask	= ATA_UDMA6,
191 		.port_ops	= &ahci_ops,
192 	},
193 	[board_ahci_yes_fbs_atapi_dma] = {
194 		AHCI_HFLAGS	(AHCI_HFLAG_YES_FBS |
195 				 AHCI_HFLAG_ATAPI_DMA_QUIRK),
196 		.flags		= AHCI_FLAG_COMMON,
197 		.pio_mask	= ATA_PIO4,
198 		.udma_mask	= ATA_UDMA6,
199 		.port_ops	= &ahci_ops,
200 	},
201 	/* by chipsets */
202 	[board_ahci_al] = {
203 		AHCI_HFLAGS	(AHCI_HFLAG_NO_PMP | AHCI_HFLAG_NO_MSI),
204 		.flags		= AHCI_FLAG_COMMON,
205 		.pio_mask	= ATA_PIO4,
206 		.udma_mask	= ATA_UDMA6,
207 		.port_ops	= &ahci_ops,
208 	},
209 	[board_ahci_avn] = {
210 		AHCI_HFLAGS	(AHCI_HFLAG_INTEL_PCS_QUIRK),
211 		.flags		= AHCI_FLAG_COMMON,
212 		.pio_mask	= ATA_PIO4,
213 		.udma_mask	= ATA_UDMA6,
214 		.port_ops	= &ahci_avn_ops,
215 	},
216 	[board_ahci_mcp65] = {
217 		AHCI_HFLAGS	(AHCI_HFLAG_NO_FPDMA_AA | AHCI_HFLAG_NO_PMP |
218 				 AHCI_HFLAG_YES_NCQ),
219 		.flags		= AHCI_FLAG_COMMON | ATA_FLAG_NO_DIPM,
220 		.pio_mask	= ATA_PIO4,
221 		.udma_mask	= ATA_UDMA6,
222 		.port_ops	= &ahci_ops,
223 	},
224 	[board_ahci_mcp77] = {
225 		AHCI_HFLAGS	(AHCI_HFLAG_NO_FPDMA_AA | AHCI_HFLAG_NO_PMP),
226 		.flags		= AHCI_FLAG_COMMON,
227 		.pio_mask	= ATA_PIO4,
228 		.udma_mask	= ATA_UDMA6,
229 		.port_ops	= &ahci_ops,
230 	},
231 	[board_ahci_mcp89] = {
232 		AHCI_HFLAGS	(AHCI_HFLAG_NO_FPDMA_AA),
233 		.flags		= AHCI_FLAG_COMMON,
234 		.pio_mask	= ATA_PIO4,
235 		.udma_mask	= ATA_UDMA6,
236 		.port_ops	= &ahci_ops,
237 	},
238 	[board_ahci_mv] = {
239 		AHCI_HFLAGS	(AHCI_HFLAG_NO_NCQ | AHCI_HFLAG_NO_MSI |
240 				 AHCI_HFLAG_MV_PATA | AHCI_HFLAG_NO_PMP),
241 		.flags		= ATA_FLAG_SATA | ATA_FLAG_PIO_DMA,
242 		.pio_mask	= ATA_PIO4,
243 		.udma_mask	= ATA_UDMA6,
244 		.port_ops	= &ahci_ops,
245 	},
246 	[board_ahci_sb600] = {
247 		AHCI_HFLAGS	(AHCI_HFLAG_IGN_SERR_INTERNAL |
248 				 AHCI_HFLAG_NO_MSI | AHCI_HFLAG_SECT255 |
249 				 AHCI_HFLAG_32BIT_ONLY),
250 		.flags		= AHCI_FLAG_COMMON,
251 		.pio_mask	= ATA_PIO4,
252 		.udma_mask	= ATA_UDMA6,
253 		.port_ops	= &ahci_pmp_retry_srst_ops,
254 	},
255 	[board_ahci_sb700] = {	/* for SB700 and SB800 */
256 		AHCI_HFLAGS	(AHCI_HFLAG_IGN_SERR_INTERNAL),
257 		.flags		= AHCI_FLAG_COMMON,
258 		.pio_mask	= ATA_PIO4,
259 		.udma_mask	= ATA_UDMA6,
260 		.port_ops	= &ahci_pmp_retry_srst_ops,
261 	},
262 	[board_ahci_vt8251] = {
263 		AHCI_HFLAGS	(AHCI_HFLAG_NO_NCQ | AHCI_HFLAG_NO_PMP),
264 		.flags		= AHCI_FLAG_COMMON,
265 		.pio_mask	= ATA_PIO4,
266 		.udma_mask	= ATA_UDMA6,
267 		.port_ops	= &ahci_vt8251_ops,
268 	},
269 };
270 
271 static const struct pci_device_id ahci_pci_tbl[] = {
272 	/* Intel */
273 	{
274 		/* Comet Lake PCH-H RAID */
275 		PCI_VDEVICE(INTEL, 0x06d6),
276 		.driver_data = board_ahci_pcs_quirk,
277 	}, {
278 		/* ICH6 */
279 		PCI_VDEVICE(INTEL, 0x2652),
280 		.driver_data = board_ahci_pcs_quirk,
281 	}, {
282 		/* ICH6M */
283 		PCI_VDEVICE(INTEL, 0x2653),
284 		.driver_data = board_ahci_pcs_quirk,
285 	}, {
286 		/* ICH7 */
287 		PCI_VDEVICE(INTEL, 0x27c1),
288 		.driver_data = board_ahci_pcs_quirk,
289 	}, {
290 		/* ICH7M */
291 		PCI_VDEVICE(INTEL, 0x27c5),
292 		.driver_data = board_ahci_pcs_quirk,
293 	}, {
294 		/* ICH7R */
295 		PCI_VDEVICE(INTEL, 0x27c3),
296 		.driver_data = board_ahci_pcs_quirk,
297 	}, {
298 		/* ULi M5288 */
299 		PCI_VDEVICE(AL, 0x5288),
300 		.driver_data = board_ahci_ign_iferr,
301 	}, {
302 		/* ESB2 */
303 		PCI_VDEVICE(INTEL, 0x2681),
304 		.driver_data = board_ahci_pcs_quirk,
305 	}, {
306 		/* ESB2 */
307 		PCI_VDEVICE(INTEL, 0x2682),
308 		.driver_data = board_ahci_pcs_quirk,
309 	}, {
310 		/* ESB2 */
311 		PCI_VDEVICE(INTEL, 0x2683),
312 		.driver_data = board_ahci_pcs_quirk,
313 	}, {
314 		/* ICH7-M DH */
315 		PCI_VDEVICE(INTEL, 0x27c6),
316 		.driver_data = board_ahci_pcs_quirk,
317 	}, {
318 		/* ICH8 */
319 		PCI_VDEVICE(INTEL, 0x2821),
320 			.driver_data = board_ahci_pcs_quirk,
321 	}, {
322 		/* ICH8/Lewisburg RAID*/
323 		PCI_VDEVICE(INTEL, 0x2822),
324 			.driver_data = board_ahci_pcs_quirk_no_sntf,
325 	}, {
326 		/* ICH8 */
327 		PCI_VDEVICE(INTEL, 0x2824),
328 			.driver_data = board_ahci_pcs_quirk,
329 	}, {
330 		/* ICH8M */
331 		PCI_VDEVICE(INTEL, 0x2829),
332 			.driver_data = board_ahci_pcs_quirk,
333 	}, {
334 		/* ICH8M */
335 		PCI_VDEVICE(INTEL, 0x282a),
336 			.driver_data = board_ahci_pcs_quirk,
337 	}, {
338 		/* ICH9 */
339 		PCI_VDEVICE(INTEL, 0x2922),
340 		.driver_data = board_ahci_pcs_quirk,
341 	}, {
342 		/* ICH9 */
343 		PCI_VDEVICE(INTEL, 0x2923),
344 		.driver_data = board_ahci_pcs_quirk,
345 	}, {
346 		/* ICH9 */
347 		PCI_VDEVICE(INTEL, 0x2924),
348 		.driver_data = board_ahci_pcs_quirk,
349 	}, {
350 		/* ICH9 */
351 		PCI_VDEVICE(INTEL, 0x2925),
352 		.driver_data = board_ahci_pcs_quirk,
353 	}, {
354 		/* ICH9 */
355 		PCI_VDEVICE(INTEL, 0x2927),
356 		.driver_data = board_ahci_pcs_quirk,
357 	}, {
358 		/* ICH9M */
359 		PCI_VDEVICE(INTEL, 0x2929),
360 		.driver_data = board_ahci_pcs_quirk,
361 	}, {
362 		/* ICH9M */
363 		PCI_VDEVICE(INTEL, 0x292a),
364 		.driver_data = board_ahci_pcs_quirk,
365 	}, {
366 		/* ICH9M */
367 		PCI_VDEVICE(INTEL, 0x292b),
368 		.driver_data = board_ahci_pcs_quirk,
369 	}, {
370 		/* ICH9M */
371 		PCI_VDEVICE(INTEL, 0x292c),
372 		.driver_data = board_ahci_pcs_quirk,
373 	}, {
374 		/* ICH9M */
375 		PCI_VDEVICE(INTEL, 0x292f),
376 		.driver_data = board_ahci_pcs_quirk,
377 	}, {
378 		/* ICH9 */
379 		PCI_VDEVICE(INTEL, 0x294d),
380 		.driver_data = board_ahci_pcs_quirk,
381 	}, {
382 		/* ICH9M */
383 		PCI_VDEVICE(INTEL, 0x294e),
384 		.driver_data = board_ahci_pcs_quirk,
385 	}, {
386 		/* Tolapai */
387 		PCI_VDEVICE(INTEL, 0x502a),
388 		.driver_data = board_ahci_pcs_quirk,
389 	}, {
390 		/* Tolapai */
391 		PCI_VDEVICE(INTEL, 0x502b),
392 		.driver_data = board_ahci_pcs_quirk,
393 	}, {
394 		/* ICH10 */
395 		PCI_VDEVICE(INTEL, 0x3a05),
396 		.driver_data = board_ahci_pcs_quirk,
397 	}, {
398 		/* ICH10 */
399 		PCI_VDEVICE(INTEL, 0x3a22),
400 		.driver_data = board_ahci_pcs_quirk,
401 	}, {
402 		/* ICH10 */
403 		PCI_VDEVICE(INTEL, 0x3a25),
404 		.driver_data = board_ahci_pcs_quirk,
405 	}, {
406 		/* PCH AHCI */
407 		PCI_VDEVICE(INTEL, 0x3b22),
408 		.driver_data = board_ahci_pcs_quirk,
409 	}, {
410 		/* PCH AHCI */
411 		PCI_VDEVICE(INTEL, 0x3b23),
412 		.driver_data = board_ahci_pcs_quirk,
413 	}, {
414 		/* PCH RAID */
415 		PCI_VDEVICE(INTEL, 0x3b24),
416 		.driver_data = board_ahci_pcs_quirk,
417 	}, {
418 		/* PCH RAID */
419 		PCI_VDEVICE(INTEL, 0x3b25),
420 		.driver_data = board_ahci_pcs_quirk,
421 	}, {
422 		/* PCH M AHCI */
423 		PCI_VDEVICE(INTEL, 0x3b29),
424 		.driver_data = board_ahci_pcs_quirk,
425 	}, {
426 		/* PCH RAID */
427 		PCI_VDEVICE(INTEL, 0x3b2b),
428 		.driver_data = board_ahci_pcs_quirk,
429 	}, {
430 		/* PCH M RAID */
431 		PCI_VDEVICE(INTEL, 0x3b2c),
432 		.driver_data = board_ahci_pcs_quirk,
433 	}, {
434 		/* PCH AHCI */
435 		PCI_VDEVICE(INTEL, 0x3b2f),
436 		.driver_data = board_ahci_pcs_quirk,
437 	}, {
438 		/* DNV AHCI */
439 		PCI_VDEVICE(INTEL, 0x19b0),
440 		.driver_data = board_ahci,
441 	}, {
442 		/* DNV AHCI */
443 		PCI_VDEVICE(INTEL, 0x19b1),
444 		.driver_data = board_ahci,
445 	}, {
446 		/* DNV AHCI */
447 		PCI_VDEVICE(INTEL, 0x19b2),
448 		.driver_data = board_ahci,
449 	}, {
450 		/* DNV AHCI */
451 		PCI_VDEVICE(INTEL, 0x19b3),
452 		.driver_data = board_ahci,
453 	}, {
454 		/* DNV AHCI */
455 		PCI_VDEVICE(INTEL, 0x19b4),
456 		.driver_data = board_ahci,
457 	}, {
458 		/* DNV AHCI */
459 		PCI_VDEVICE(INTEL, 0x19b5),
460 		.driver_data = board_ahci,
461 	}, {
462 		/* DNV AHCI */
463 		PCI_VDEVICE(INTEL, 0x19b6),
464 		.driver_data = board_ahci,
465 	}, {
466 		/* DNV AHCI */
467 		PCI_VDEVICE(INTEL, 0x19b7),
468 		.driver_data = board_ahci,
469 	}, {
470 		/* DNV AHCI */
471 		PCI_VDEVICE(INTEL, 0x19bE),
472 		.driver_data = board_ahci,
473 	}, {
474 		/* DNV AHCI */
475 		PCI_VDEVICE(INTEL, 0x19bF),
476 		.driver_data = board_ahci,
477 	}, {
478 		/* DNV AHCI */
479 		PCI_VDEVICE(INTEL, 0x19c0),
480 		.driver_data = board_ahci,
481 	}, {
482 		/* DNV AHCI */
483 		PCI_VDEVICE(INTEL, 0x19c1),
484 		.driver_data = board_ahci,
485 	}, {
486 		/* DNV AHCI */
487 		PCI_VDEVICE(INTEL, 0x19c2),
488 		.driver_data = board_ahci,
489 	}, {
490 		/* DNV AHCI */
491 		PCI_VDEVICE(INTEL, 0x19c3),
492 		.driver_data = board_ahci,
493 	}, {
494 		/* DNV AHCI */
495 		PCI_VDEVICE(INTEL, 0x19c4),
496 		.driver_data = board_ahci,
497 	}, {
498 		/* DNV AHCI */
499 		PCI_VDEVICE(INTEL, 0x19c5),
500 		.driver_data = board_ahci,
501 	}, {
502 		/* DNV AHCI */
503 		PCI_VDEVICE(INTEL, 0x19c6),
504 		.driver_data = board_ahci,
505 	}, {
506 		/* DNV AHCI */
507 		PCI_VDEVICE(INTEL, 0x19c7),
508 		.driver_data = board_ahci,
509 	}, {
510 		/* DNV AHCI */
511 		PCI_VDEVICE(INTEL, 0x19cE),
512 		.driver_data = board_ahci,
513 	}, {
514 		/* DNV AHCI */
515 		PCI_VDEVICE(INTEL, 0x19cF),
516 		.driver_data = board_ahci,
517 	}, {
518 		/* CPT AHCI */
519 		PCI_VDEVICE(INTEL, 0x1c02),
520 		.driver_data = board_ahci_pcs_quirk,
521 	}, {
522 		/* CPT M AHCI */
523 		PCI_VDEVICE(INTEL, 0x1c03),
524 		.driver_data = board_ahci_pcs_quirk,
525 	}, {
526 		/* CPT RAID */
527 		PCI_VDEVICE(INTEL, 0x1c04),
528 		.driver_data = board_ahci_pcs_quirk,
529 	}, {
530 		/* CPT M RAID */
531 		PCI_VDEVICE(INTEL, 0x1c05),
532 		.driver_data = board_ahci_pcs_quirk,
533 	}, {
534 		/* CPT RAID */
535 		PCI_VDEVICE(INTEL, 0x1c06),
536 		.driver_data = board_ahci_pcs_quirk,
537 	}, {
538 		/* CPT RAID */
539 		PCI_VDEVICE(INTEL, 0x1c07),
540 		.driver_data = board_ahci_pcs_quirk,
541 	}, {
542 		/* PBG AHCI */
543 		PCI_VDEVICE(INTEL, 0x1d02),
544 		.driver_data = board_ahci_pcs_quirk,
545 	}, {
546 		/* PBG RAID */
547 		PCI_VDEVICE(INTEL, 0x1d04),
548 		.driver_data = board_ahci_pcs_quirk,
549 	}, {
550 		/* PBG RAID */
551 		PCI_VDEVICE(INTEL, 0x1d06),
552 		.driver_data = board_ahci_pcs_quirk,
553 	}, {
554 		/* DH89xxCC AHCI */
555 		PCI_VDEVICE(INTEL, 0x2323),
556 		.driver_data = board_ahci_pcs_quirk,
557 	}, {
558 		/* Panther Point AHCI */
559 		PCI_VDEVICE(INTEL, 0x1e02),
560 		.driver_data = board_ahci_pcs_quirk,
561 	}, {
562 		/* Panther M AHCI */
563 		PCI_VDEVICE(INTEL, 0x1e03),
564 		.driver_data = board_ahci_pcs_quirk,
565 	}, {
566 		/* Panther Point RAID */
567 		PCI_VDEVICE(INTEL, 0x1e04),
568 		.driver_data = board_ahci_pcs_quirk,
569 	}, {
570 		/* Panther Point RAID */
571 		PCI_VDEVICE(INTEL, 0x1e05),
572 		.driver_data = board_ahci_pcs_quirk,
573 	}, {
574 		/* Panther Point RAID */
575 		PCI_VDEVICE(INTEL, 0x1e06),
576 		.driver_data = board_ahci_pcs_quirk,
577 	}, {
578 		/* Panther M RAID */
579 		PCI_VDEVICE(INTEL, 0x1e07),
580 		.driver_data = board_ahci_pcs_quirk,
581 	}, {
582 		/* Panther Point RAID */
583 		PCI_VDEVICE(INTEL, 0x1e0e),
584 		.driver_data = board_ahci_pcs_quirk,
585 	}, {
586 		/* Lynx Point AHCI */
587 		PCI_VDEVICE(INTEL, 0x8c02),
588 		.driver_data = board_ahci_pcs_quirk,
589 	}, {
590 		/* Lynx M AHCI */
591 		PCI_VDEVICE(INTEL, 0x8c03),
592 		.driver_data = board_ahci_pcs_quirk,
593 	}, {
594 		/* Lynx Point RAID */
595 		PCI_VDEVICE(INTEL, 0x8c04),
596 		.driver_data = board_ahci_pcs_quirk,
597 	}, {
598 		/* Lynx M RAID */
599 		PCI_VDEVICE(INTEL, 0x8c05),
600 		.driver_data = board_ahci_pcs_quirk,
601 	}, {
602 		/* Lynx Point RAID */
603 		PCI_VDEVICE(INTEL, 0x8c06),
604 		.driver_data = board_ahci_pcs_quirk,
605 	}, {
606 		/* Lynx M RAID */
607 		PCI_VDEVICE(INTEL, 0x8c07),
608 		.driver_data = board_ahci_pcs_quirk,
609 	}, {
610 		/* Lynx Point RAID */
611 		PCI_VDEVICE(INTEL, 0x8c0e),
612 		.driver_data = board_ahci_pcs_quirk,
613 	}, {
614 		/* Lynx M RAID */
615 		PCI_VDEVICE(INTEL, 0x8c0f),
616 		.driver_data = board_ahci_pcs_quirk,
617 	}, {
618 		/* Lynx LP AHCI */
619 		PCI_VDEVICE(INTEL, 0x9c02),
620 		.driver_data = board_ahci_pcs_quirk,
621 	}, {
622 		/* Lynx LP AHCI */
623 		PCI_VDEVICE(INTEL, 0x9c03),
624 		.driver_data = board_ahci_pcs_quirk,
625 	}, {
626 		/* Lynx LP RAID */
627 		PCI_VDEVICE(INTEL, 0x9c04),
628 		.driver_data = board_ahci_pcs_quirk,
629 	}, {
630 		/* Lynx LP RAID */
631 		PCI_VDEVICE(INTEL, 0x9c05),
632 		.driver_data = board_ahci_pcs_quirk,
633 	}, {
634 		/* Lynx LP RAID */
635 		PCI_VDEVICE(INTEL, 0x9c06),
636 		.driver_data = board_ahci_pcs_quirk,
637 	}, {
638 		/* Lynx LP RAID */
639 		PCI_VDEVICE(INTEL, 0x9c07),
640 		.driver_data = board_ahci_pcs_quirk,
641 	}, {
642 		/* Lynx LP RAID */
643 		PCI_VDEVICE(INTEL, 0x9c0e),
644 		.driver_data = board_ahci_pcs_quirk,
645 	}, {
646 		/* Lynx LP RAID */
647 		PCI_VDEVICE(INTEL, 0x9c0f),
648 		.driver_data = board_ahci_pcs_quirk,
649 	}, {
650 		/* Cannon Lake PCH-LP AHCI */
651 		PCI_VDEVICE(INTEL, 0x9dd3),
652 		.driver_data = board_ahci_pcs_quirk,
653 	}, {
654 		/* Avoton AHCI */
655 		PCI_VDEVICE(INTEL, 0x1f22),
656 		.driver_data = board_ahci_pcs_quirk,
657 	}, {
658 		/* Avoton AHCI */
659 		PCI_VDEVICE(INTEL, 0x1f23),
660 		.driver_data = board_ahci_pcs_quirk,
661 	}, {
662 		/* Avoton RAID */
663 		PCI_VDEVICE(INTEL, 0x1f24),
664 		.driver_data = board_ahci_pcs_quirk,
665 	}, {
666 		/* Avoton RAID */
667 		PCI_VDEVICE(INTEL, 0x1f25),
668 		.driver_data = board_ahci_pcs_quirk,
669 	}, {
670 		/* Avoton RAID */
671 		PCI_VDEVICE(INTEL, 0x1f26),
672 		.driver_data = board_ahci_pcs_quirk,
673 	}, {
674 		/* Avoton RAID */
675 		PCI_VDEVICE(INTEL, 0x1f27),
676 		.driver_data = board_ahci_pcs_quirk,
677 	}, {
678 		/* Avoton RAID */
679 		PCI_VDEVICE(INTEL, 0x1f2e),
680 		.driver_data = board_ahci_pcs_quirk,
681 	}, {
682 		/* Avoton RAID */
683 		PCI_VDEVICE(INTEL, 0x1f2f),
684 		.driver_data = board_ahci_pcs_quirk,
685 	}, {
686 		/* Avoton AHCI */
687 		PCI_VDEVICE(INTEL, 0x1f32),
688 		.driver_data = board_ahci_avn,
689 	}, {
690 		/* Avoton AHCI */
691 		PCI_VDEVICE(INTEL, 0x1f33),
692 		.driver_data = board_ahci_avn,
693 	}, {
694 		/* Avoton RAID */
695 		PCI_VDEVICE(INTEL, 0x1f34),
696 		.driver_data = board_ahci_avn,
697 	}, {
698 		/* Avoton RAID */
699 		PCI_VDEVICE(INTEL, 0x1f35),
700 		.driver_data = board_ahci_avn,
701 	}, {
702 		/* Avoton RAID */
703 		PCI_VDEVICE(INTEL, 0x1f36),
704 		.driver_data = board_ahci_avn,
705 	}, {
706 		/* Avoton RAID */
707 		PCI_VDEVICE(INTEL, 0x1f37),
708 		.driver_data = board_ahci_avn,
709 	}, {
710 		/* Avoton RAID */
711 		PCI_VDEVICE(INTEL, 0x1f3e),
712 		.driver_data = board_ahci_avn,
713 	}, {
714 		/* Avoton RAID */
715 		PCI_VDEVICE(INTEL, 0x1f3f),
716 		.driver_data = board_ahci_avn,
717 	}, {
718 		/* Wellsburg/Lewisburg AHCI*/
719 		PCI_VDEVICE(INTEL, 0x2823),
720 		.driver_data = board_ahci_pcs_quirk,
721 	}, {
722 		/* *burg SATA0 'RAID' */
723 		PCI_VDEVICE(INTEL, 0x2826),
724 		.driver_data = board_ahci_pcs_quirk,
725 	}, {
726 		/* *burg SATA1 'RAID' */
727 		PCI_VDEVICE(INTEL, 0x2827),
728 		.driver_data = board_ahci_pcs_quirk,
729 	}, {
730 		/* *burg SATA2 'RAID' */
731 		PCI_VDEVICE(INTEL, 0x282f),
732 		.driver_data = board_ahci_pcs_quirk,
733 	}, {
734 		/* Rocket Lake PCH-H RAID */
735 		PCI_VDEVICE(INTEL, 0x43d4),
736 		.driver_data = board_ahci_pcs_quirk,
737 	}, {
738 		/* Rocket Lake PCH-H RAID */
739 		PCI_VDEVICE(INTEL, 0x43d5),
740 		.driver_data = board_ahci_pcs_quirk,
741 	}, {
742 		/* Rocket Lake PCH-H RAID */
743 		PCI_VDEVICE(INTEL, 0x43d6),
744 		.driver_data = board_ahci_pcs_quirk,
745 	}, {
746 		/* Rocket Lake PCH-H RAID */
747 		PCI_VDEVICE(INTEL, 0x43d7),
748 		.driver_data = board_ahci_pcs_quirk,
749 	}, {
750 		/* Wellsburg AHCI */
751 		PCI_VDEVICE(INTEL, 0x8d02),
752 		.driver_data = board_ahci_pcs_quirk,
753 	}, {
754 		/* Wellsburg RAID */
755 		PCI_VDEVICE(INTEL, 0x8d04),
756 		.driver_data = board_ahci_pcs_quirk,
757 	}, {
758 		/* Wellsburg RAID */
759 		PCI_VDEVICE(INTEL, 0x8d06),
760 		.driver_data = board_ahci_pcs_quirk,
761 	}, {
762 		/* Wellsburg RAID */
763 		PCI_VDEVICE(INTEL, 0x8d0e),
764 		.driver_data = board_ahci_pcs_quirk,
765 	}, {
766 		/* Wellsburg AHCI */
767 		PCI_VDEVICE(INTEL, 0x8d62),
768 		.driver_data = board_ahci_pcs_quirk,
769 	}, {
770 		/* Wellsburg RAID */
771 		PCI_VDEVICE(INTEL, 0x8d64),
772 		.driver_data = board_ahci_pcs_quirk,
773 	}, {
774 		/* Wellsburg RAID */
775 		PCI_VDEVICE(INTEL, 0x8d66),
776 		.driver_data = board_ahci_pcs_quirk,
777 	}, {
778 		/* Wellsburg RAID */
779 		PCI_VDEVICE(INTEL, 0x8d6e),
780 		.driver_data = board_ahci_pcs_quirk,
781 	}, {
782 		/* Coleto Creek AHCI */
783 		PCI_VDEVICE(INTEL, 0x23a3),
784 		.driver_data = board_ahci_pcs_quirk,
785 	}, {
786 		/* Wildcat LP AHCI */
787 		PCI_VDEVICE(INTEL, 0x9c83),
788 		.driver_data = board_ahci_pcs_quirk,
789 	}, {
790 		/* Wildcat LP RAID */
791 		PCI_VDEVICE(INTEL, 0x9c85),
792 		.driver_data = board_ahci_pcs_quirk,
793 	}, {
794 		/* Wildcat LP RAID */
795 		PCI_VDEVICE(INTEL, 0x9c87),
796 		.driver_data = board_ahci_pcs_quirk,
797 	}, {
798 		/* Wildcat LP RAID */
799 		PCI_VDEVICE(INTEL, 0x9c8f),
800 		.driver_data = board_ahci_pcs_quirk,
801 	}, {
802 		/* 9 Series AHCI */
803 		PCI_VDEVICE(INTEL, 0x8c82),
804 		.driver_data = board_ahci_pcs_quirk,
805 	}, {
806 		/* 9 Series M AHCI */
807 		PCI_VDEVICE(INTEL, 0x8c83),
808 		.driver_data = board_ahci_pcs_quirk,
809 	}, {
810 		/* 9 Series RAID */
811 		PCI_VDEVICE(INTEL, 0x8c84),
812 		.driver_data = board_ahci_pcs_quirk,
813 	}, {
814 		/* 9 Series M RAID */
815 		PCI_VDEVICE(INTEL, 0x8c85),
816 		.driver_data = board_ahci_pcs_quirk,
817 	}, {
818 		/* 9 Series RAID */
819 		PCI_VDEVICE(INTEL, 0x8c86),
820 		.driver_data = board_ahci_pcs_quirk,
821 	}, {
822 		/* 9 Series M RAID */
823 		PCI_VDEVICE(INTEL, 0x8c87),
824 		.driver_data = board_ahci_pcs_quirk,
825 	}, {
826 		/* 9 Series RAID */
827 		PCI_VDEVICE(INTEL, 0x8c8e),
828 		.driver_data = board_ahci_pcs_quirk,
829 	}, {
830 		/* 9 Series M RAID */
831 		PCI_VDEVICE(INTEL, 0x8c8f),
832 		.driver_data = board_ahci_pcs_quirk,
833 	}, {
834 		/* Sunrise LP AHCI */
835 		PCI_VDEVICE(INTEL, 0x9d03),
836 		.driver_data = board_ahci_pcs_quirk,
837 	}, {
838 		/* Sunrise LP RAID */
839 		PCI_VDEVICE(INTEL, 0x9d05),
840 		.driver_data = board_ahci_pcs_quirk,
841 	}, {
842 		/* Sunrise LP RAID */
843 		PCI_VDEVICE(INTEL, 0x9d07),
844 		.driver_data = board_ahci_pcs_quirk,
845 	}, {
846 		/* Sunrise Point-H AHCI */
847 		PCI_VDEVICE(INTEL, 0xa102),
848 		.driver_data = board_ahci_pcs_quirk,
849 	}, {
850 		/* Sunrise M AHCI */
851 		PCI_VDEVICE(INTEL, 0xa103),
852 		.driver_data = board_ahci_pcs_quirk,
853 	}, {
854 		/* Sunrise Point-H RAID */
855 		PCI_VDEVICE(INTEL, 0xa105),
856 		.driver_data = board_ahci_pcs_quirk,
857 	}, {
858 		/* Sunrise Point-H RAID */
859 		PCI_VDEVICE(INTEL, 0xa106),
860 		.driver_data = board_ahci_pcs_quirk,
861 	}, {
862 		/* Sunrise M RAID */
863 		PCI_VDEVICE(INTEL, 0xa107),
864 		.driver_data = board_ahci_pcs_quirk,
865 	}, {
866 		/* Sunrise Point-H RAID */
867 		PCI_VDEVICE(INTEL, 0xa10f),
868 		.driver_data = board_ahci_pcs_quirk,
869 	}, {
870 		/* Lewisburg AHCI*/
871 		PCI_VDEVICE(INTEL, 0xa182),
872 		.driver_data = board_ahci_pcs_quirk,
873 	}, {
874 		/* Lewisburg RAID*/
875 		PCI_VDEVICE(INTEL, 0xa186),
876 		.driver_data = board_ahci_pcs_quirk,
877 	}, {
878 		/* Lewisburg RAID*/
879 		PCI_VDEVICE(INTEL, 0xa1d2),
880 		.driver_data = board_ahci_pcs_quirk,
881 	}, {
882 		/* Lewisburg RAID*/
883 		PCI_VDEVICE(INTEL, 0xa1d6),
884 		.driver_data = board_ahci_pcs_quirk,
885 	}, {
886 		/* Lewisburg AHCI*/
887 		PCI_VDEVICE(INTEL, 0xa202),
888 		.driver_data = board_ahci_pcs_quirk,
889 	}, {
890 		/* Lewisburg RAID*/
891 		PCI_VDEVICE(INTEL, 0xa206),
892 		.driver_data = board_ahci_pcs_quirk,
893 	}, {
894 		/* Lewisburg RAID*/
895 		PCI_VDEVICE(INTEL, 0xa252),
896 		.driver_data = board_ahci_pcs_quirk,
897 	}, {
898 		/* Lewisburg RAID*/
899 		PCI_VDEVICE(INTEL, 0xa256),
900 		.driver_data = board_ahci_pcs_quirk,
901 	}, {
902 		/* Cannon Lake PCH-H RAID */
903 		PCI_VDEVICE(INTEL, 0xa356),
904 		.driver_data = board_ahci_pcs_quirk,
905 	}, {
906 		/* Comet Lake-H RAID */
907 		PCI_VDEVICE(INTEL, 0x06d7),
908 		.driver_data = board_ahci_pcs_quirk,
909 	}, {
910 		/* Comet Lake PCH-V RAID */
911 		PCI_VDEVICE(INTEL, 0xa386),
912 		.driver_data = board_ahci_pcs_quirk,
913 	}, {
914 		/* Bay Trail AHCI */
915 		PCI_VDEVICE(INTEL, 0x0f22),
916 		.driver_data = board_ahci_pcs_quirk,
917 	}, {
918 		/* Bay Trail AHCI */
919 		PCI_VDEVICE(INTEL, 0x0f23),
920 		.driver_data = board_ahci_pcs_quirk_no_devslp,
921 	}, {
922 		/* Cherry Tr. AHCI */
923 		PCI_VDEVICE(INTEL, 0x22a3),
924 		.driver_data = board_ahci_pcs_quirk,
925 	}, {
926 		/* ApolloLake AHCI */
927 		PCI_VDEVICE(INTEL, 0x5ae3),
928 		.driver_data = board_ahci_pcs_quirk,
929 	}, {
930 		/* Ice Lake LP AHCI */
931 		PCI_VDEVICE(INTEL, 0x34d3),
932 		.driver_data = board_ahci_pcs_quirk,
933 	}, {
934 		/* Comet Lake PCH-U AHCI */
935 		PCI_VDEVICE(INTEL, 0x02d3),
936 		.driver_data = board_ahci_pcs_quirk,
937 	}, {
938 		/* Comet Lake PCH RAID */
939 		PCI_VDEVICE(INTEL, 0x02d7),
940 		.driver_data = board_ahci_pcs_quirk,
941 	},
942 
943 	/* Elkhart Lake IDs 0x4b60 & 0x4b62 https://sata-io.org/product/8803 not tested yet */
944 	{
945 		/* Elkhart Lake AHCI */
946 		PCI_VDEVICE(INTEL, 0x4b63),
947 		.driver_data = board_ahci_pcs_quirk,
948 	}, {
949 		/* JMicron 360/1/3/5/6, match class to avoid IDE function */
950 		PCI_DEVICE(PCI_VENDOR_ID_JMICRON, PCI_ANY_ID),
951 		.class = PCI_CLASS_STORAGE_SATA_AHCI,
952 		.class_mask = 0xffffff,
953 		.driver_data = board_ahci_ign_iferr,
954 
955 	},
956 	/* JMicron 362B and 362C have an AHCI function with IDE class code */
957 	{
958 		PCI_VDEVICE(JMICRON, 0x2362),
959 		.driver_data = board_ahci_ign_iferr,
960 
961 	}, {
962 		PCI_VDEVICE(JMICRON, 0x236f),
963 		.driver_data = board_ahci_ign_iferr,
964 
965 	},
966 	/* May need to update quirk_jmicron_async_suspend() for additions */
967 
968 	/* ATI */
969 	{
970 		/* ATI SB600 */
971 		PCI_VDEVICE(ATI, 0x4380),
972 		.driver_data = board_ahci_sb600,
973 	}, {
974 		/* ATI SB700/800 */
975 		PCI_VDEVICE(ATI, 0x4390),
976 		.driver_data = board_ahci_sb700,
977 	}, {
978 		/* ATI SB700/800 */
979 		PCI_VDEVICE(ATI, 0x4391),
980 		.driver_data = board_ahci_sb700,
981 	}, {
982 		/* ATI SB700/800 */
983 		PCI_VDEVICE(ATI, 0x4392),
984 		.driver_data = board_ahci_sb700,
985 	}, {
986 		/* ATI SB700/800 */
987 		PCI_VDEVICE(ATI, 0x4393),
988 		.driver_data = board_ahci_sb700,
989 	}, {
990 		/* ATI SB700/800 */
991 		PCI_VDEVICE(ATI, 0x4394),
992 		.driver_data = board_ahci_sb700,
993 	}, {
994 		/* ATI SB700/800 */
995 		PCI_VDEVICE(ATI, 0x4395),
996 		.driver_data = board_ahci_sb700,
997 	},
998 
999 	/* Amazon's Annapurna Labs support */
1000 	{
1001 		PCI_DEVICE(PCI_VENDOR_ID_AMAZON_ANNAPURNA_LABS, 0x0031),
1002 			.class = PCI_CLASS_STORAGE_SATA_AHCI,
1003 			.class_mask = 0xffffff,
1004 		.driver_data = board_ahci_al,
1005 
1006 	},
1007 	/* AMD */
1008 	{
1009 		/* AMD Hudson-2 */
1010 		PCI_VDEVICE(AMD, 0x7800),
1011 		.driver_data = board_ahci,
1012 	}, {
1013 		/* AMD Hudson-2 (AHCI mode) */
1014 		PCI_VDEVICE(AMD, 0x7801),
1015 		.driver_data = board_ahci_no_debounce_delay,
1016 	}, {
1017 		/* AMD CZ */
1018 		PCI_VDEVICE(AMD, 0x7900),
1019 		.driver_data = board_ahci,
1020 	}, {
1021 		/* AMD Green Sardine */
1022 		PCI_VDEVICE(AMD, 0x7901),
1023 		.driver_data = board_ahci,
1024 	},
1025 	/* AMD is using RAID class only for ahci controllers */
1026 	{
1027 		PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_ANY_ID),
1028 		.class = PCI_CLASS_STORAGE_RAID << 8,
1029 		.class_mask = 0xffffff,
1030 		.driver_data = board_ahci,
1031 	},
1032 
1033 	/* Dell S140/S150 */
1034 	{
1035 		PCI_DEVICE_SUB(PCI_VENDOR_ID_INTEL, PCI_ANY_ID,
1036 			       PCI_SUBVENDOR_ID_DELL, PCI_ANY_ID),
1037 		.class = PCI_CLASS_STORAGE_RAID << 8,
1038 		.class_mask = 0xffffff,
1039 		.driver_data = board_ahci_pcs_quirk,
1040 
1041 	},
1042 
1043 	/* VIA */
1044 	{
1045 		/* VIA VT8251 */
1046 		PCI_VDEVICE(VIA, 0x3349),
1047 		.driver_data = board_ahci_vt8251,
1048 	}, {
1049 		/* VIA VT8251 */
1050 		PCI_VDEVICE(VIA, 0x6287),
1051 		.driver_data = board_ahci_vt8251,
1052 	},
1053 
1054 	/* NVIDIA */
1055 	{
1056 		/* MCP65 */
1057 		PCI_VDEVICE(NVIDIA, 0x044c),
1058 		.driver_data = board_ahci_mcp65,
1059 	}, {
1060 		/* MCP65 */
1061 		PCI_VDEVICE(NVIDIA, 0x044d),
1062 		.driver_data = board_ahci_mcp65,
1063 	}, {
1064 		/* MCP65 */
1065 		PCI_VDEVICE(NVIDIA, 0x044e),
1066 		.driver_data = board_ahci_mcp65,
1067 	}, {
1068 		/* MCP65 */
1069 		PCI_VDEVICE(NVIDIA, 0x044f),
1070 		.driver_data = board_ahci_mcp65,
1071 	}, {
1072 		/* MCP65 */
1073 		PCI_VDEVICE(NVIDIA, 0x045c),
1074 		.driver_data = board_ahci_mcp65,
1075 	}, {
1076 		/* MCP65 */
1077 		PCI_VDEVICE(NVIDIA, 0x045d),
1078 		.driver_data = board_ahci_mcp65,
1079 	}, {
1080 		/* MCP65 */
1081 		PCI_VDEVICE(NVIDIA, 0x045e),
1082 		.driver_data = board_ahci_mcp65,
1083 	}, {
1084 		/* MCP65 */
1085 		PCI_VDEVICE(NVIDIA, 0x045f),
1086 		.driver_data = board_ahci_mcp65,
1087 	}, {
1088 		/* MCP67 */
1089 		PCI_VDEVICE(NVIDIA, 0x0550),
1090 		.driver_data = board_ahci_mcp67,
1091 	}, {
1092 		/* MCP67 */
1093 		PCI_VDEVICE(NVIDIA, 0x0551),
1094 		.driver_data = board_ahci_mcp67,
1095 	}, {
1096 		/* MCP67 */
1097 		PCI_VDEVICE(NVIDIA, 0x0552),
1098 		.driver_data = board_ahci_mcp67,
1099 	}, {
1100 		/* MCP67 */
1101 		PCI_VDEVICE(NVIDIA, 0x0553),
1102 		.driver_data = board_ahci_mcp67,
1103 	}, {
1104 		/* MCP67 */
1105 		PCI_VDEVICE(NVIDIA, 0x0554),
1106 		.driver_data = board_ahci_mcp67,
1107 	}, {
1108 		/* MCP67 */
1109 		PCI_VDEVICE(NVIDIA, 0x0555),
1110 		.driver_data = board_ahci_mcp67,
1111 	}, {
1112 		/* MCP67 */
1113 		PCI_VDEVICE(NVIDIA, 0x0556),
1114 		.driver_data = board_ahci_mcp67,
1115 	}, {
1116 		/* MCP67 */
1117 		PCI_VDEVICE(NVIDIA, 0x0557),
1118 		.driver_data = board_ahci_mcp67,
1119 	}, {
1120 		/* MCP67 */
1121 		PCI_VDEVICE(NVIDIA, 0x0558),
1122 		.driver_data = board_ahci_mcp67,
1123 	}, {
1124 		/* MCP67 */
1125 		PCI_VDEVICE(NVIDIA, 0x0559),
1126 		.driver_data = board_ahci_mcp67,
1127 	}, {
1128 		/* MCP67 */
1129 		PCI_VDEVICE(NVIDIA, 0x055a),
1130 		.driver_data = board_ahci_mcp67,
1131 	}, {
1132 		/* MCP67 */
1133 		PCI_VDEVICE(NVIDIA, 0x055b),
1134 		.driver_data = board_ahci_mcp67,
1135 	}, {
1136 		/* Linux ID */
1137 		PCI_VDEVICE(NVIDIA, 0x0580),
1138 		.driver_data = board_ahci_mcp_linux,
1139 	}, {
1140 		/* Linux ID */
1141 		PCI_VDEVICE(NVIDIA, 0x0581),
1142 		.driver_data = board_ahci_mcp_linux,
1143 	}, {
1144 		/* Linux ID */
1145 		PCI_VDEVICE(NVIDIA, 0x0582),
1146 		.driver_data = board_ahci_mcp_linux,
1147 	}, {
1148 		/* Linux ID */
1149 		PCI_VDEVICE(NVIDIA, 0x0583),
1150 		.driver_data = board_ahci_mcp_linux,
1151 	}, {
1152 		/* Linux ID */
1153 		PCI_VDEVICE(NVIDIA, 0x0584),
1154 		.driver_data = board_ahci_mcp_linux,
1155 	}, {
1156 		/* Linux ID */
1157 		PCI_VDEVICE(NVIDIA, 0x0585),
1158 		.driver_data = board_ahci_mcp_linux,
1159 	}, {
1160 		/* Linux ID */
1161 		PCI_VDEVICE(NVIDIA, 0x0586),
1162 		.driver_data = board_ahci_mcp_linux,
1163 	}, {
1164 		/* Linux ID */
1165 		PCI_VDEVICE(NVIDIA, 0x0587),
1166 		.driver_data = board_ahci_mcp_linux,
1167 	}, {
1168 		/* Linux ID */
1169 		PCI_VDEVICE(NVIDIA, 0x0588),
1170 		.driver_data = board_ahci_mcp_linux,
1171 	}, {
1172 		/* Linux ID */
1173 		PCI_VDEVICE(NVIDIA, 0x0589),
1174 		.driver_data = board_ahci_mcp_linux,
1175 	}, {
1176 		/* Linux ID */
1177 		PCI_VDEVICE(NVIDIA, 0x058a),
1178 		.driver_data = board_ahci_mcp_linux,
1179 	}, {
1180 		/* Linux ID */
1181 		PCI_VDEVICE(NVIDIA, 0x058b),
1182 		.driver_data = board_ahci_mcp_linux,
1183 	}, {
1184 		/* Linux ID */
1185 		PCI_VDEVICE(NVIDIA, 0x058c),
1186 		.driver_data = board_ahci_mcp_linux,
1187 	}, {
1188 		/* Linux ID */
1189 		PCI_VDEVICE(NVIDIA, 0x058d),
1190 		.driver_data = board_ahci_mcp_linux,
1191 	}, {
1192 		/* Linux ID */
1193 		PCI_VDEVICE(NVIDIA, 0x058e),
1194 		.driver_data = board_ahci_mcp_linux,
1195 	}, {
1196 		/* Linux ID */
1197 		PCI_VDEVICE(NVIDIA, 0x058f),
1198 		.driver_data = board_ahci_mcp_linux,
1199 	}, {
1200 		/* MCP73 */
1201 		PCI_VDEVICE(NVIDIA, 0x07f0),
1202 		.driver_data = board_ahci_mcp73,
1203 	}, {
1204 		/* MCP73 */
1205 		PCI_VDEVICE(NVIDIA, 0x07f1),
1206 		.driver_data = board_ahci_mcp73,
1207 	}, {
1208 		/* MCP73 */
1209 		PCI_VDEVICE(NVIDIA, 0x07f2),
1210 		.driver_data = board_ahci_mcp73,
1211 	}, {
1212 		/* MCP73 */
1213 		PCI_VDEVICE(NVIDIA, 0x07f3),
1214 		.driver_data = board_ahci_mcp73,
1215 	}, {
1216 		/* MCP73 */
1217 		PCI_VDEVICE(NVIDIA, 0x07f4),
1218 		.driver_data = board_ahci_mcp73,
1219 	}, {
1220 		/* MCP73 */
1221 		PCI_VDEVICE(NVIDIA, 0x07f5),
1222 		.driver_data = board_ahci_mcp73,
1223 	}, {
1224 		/* MCP73 */
1225 		PCI_VDEVICE(NVIDIA, 0x07f6),
1226 		.driver_data = board_ahci_mcp73,
1227 	}, {
1228 		/* MCP73 */
1229 		PCI_VDEVICE(NVIDIA, 0x07f7),
1230 		.driver_data = board_ahci_mcp73,
1231 	}, {
1232 		/* MCP73 */
1233 		PCI_VDEVICE(NVIDIA, 0x07f8),
1234 		.driver_data = board_ahci_mcp73,
1235 	}, {
1236 		/* MCP73 */
1237 		PCI_VDEVICE(NVIDIA, 0x07f9),
1238 		.driver_data = board_ahci_mcp73,
1239 	}, {
1240 		/* MCP73 */
1241 		PCI_VDEVICE(NVIDIA, 0x07fa),
1242 		.driver_data = board_ahci_mcp73,
1243 	}, {
1244 		/* MCP73 */
1245 		PCI_VDEVICE(NVIDIA, 0x07fb),
1246 		.driver_data = board_ahci_mcp73,
1247 	}, {
1248 		/* MCP77 */
1249 		PCI_VDEVICE(NVIDIA, 0x0ad0),
1250 		.driver_data = board_ahci_mcp77,
1251 	}, {
1252 		/* MCP77 */
1253 		PCI_VDEVICE(NVIDIA, 0x0ad1),
1254 		.driver_data = board_ahci_mcp77,
1255 	}, {
1256 		/* MCP77 */
1257 		PCI_VDEVICE(NVIDIA, 0x0ad2),
1258 		.driver_data = board_ahci_mcp77,
1259 	}, {
1260 		/* MCP77 */
1261 		PCI_VDEVICE(NVIDIA, 0x0ad3),
1262 		.driver_data = board_ahci_mcp77,
1263 	}, {
1264 		/* MCP77 */
1265 		PCI_VDEVICE(NVIDIA, 0x0ad4),
1266 		.driver_data = board_ahci_mcp77,
1267 	}, {
1268 		/* MCP77 */
1269 		PCI_VDEVICE(NVIDIA, 0x0ad5),
1270 		.driver_data = board_ahci_mcp77,
1271 	}, {
1272 		/* MCP77 */
1273 		PCI_VDEVICE(NVIDIA, 0x0ad6),
1274 		.driver_data = board_ahci_mcp77,
1275 	}, {
1276 		/* MCP77 */
1277 		PCI_VDEVICE(NVIDIA, 0x0ad7),
1278 		.driver_data = board_ahci_mcp77,
1279 	}, {
1280 		/* MCP77 */
1281 		PCI_VDEVICE(NVIDIA, 0x0ad8),
1282 		.driver_data = board_ahci_mcp77,
1283 	}, {
1284 		/* MCP77 */
1285 		PCI_VDEVICE(NVIDIA, 0x0ad9),
1286 		.driver_data = board_ahci_mcp77,
1287 	}, {
1288 		/* MCP77 */
1289 		PCI_VDEVICE(NVIDIA, 0x0ada),
1290 		.driver_data = board_ahci_mcp77,
1291 	}, {
1292 		/* MCP77 */
1293 		PCI_VDEVICE(NVIDIA, 0x0adb),
1294 		.driver_data = board_ahci_mcp77,
1295 	}, {
1296 		/* MCP79 */
1297 		PCI_VDEVICE(NVIDIA, 0x0ab4),
1298 		.driver_data = board_ahci_mcp79,
1299 	}, {
1300 		/* MCP79 */
1301 		PCI_VDEVICE(NVIDIA, 0x0ab5),
1302 		.driver_data = board_ahci_mcp79,
1303 	}, {
1304 		/* MCP79 */
1305 		PCI_VDEVICE(NVIDIA, 0x0ab6),
1306 		.driver_data = board_ahci_mcp79,
1307 	}, {
1308 		/* MCP79 */
1309 		PCI_VDEVICE(NVIDIA, 0x0ab7),
1310 		.driver_data = board_ahci_mcp79,
1311 	}, {
1312 		/* MCP79 */
1313 		PCI_VDEVICE(NVIDIA, 0x0ab8),
1314 		.driver_data = board_ahci_mcp79,
1315 	}, {
1316 		/* MCP79 */
1317 		PCI_VDEVICE(NVIDIA, 0x0ab9),
1318 		.driver_data = board_ahci_mcp79,
1319 	}, {
1320 		/* MCP79 */
1321 		PCI_VDEVICE(NVIDIA, 0x0aba),
1322 		.driver_data = board_ahci_mcp79,
1323 	}, {
1324 		/* MCP79 */
1325 		PCI_VDEVICE(NVIDIA, 0x0abb),
1326 		.driver_data = board_ahci_mcp79,
1327 	}, {
1328 		/* MCP79 */
1329 		PCI_VDEVICE(NVIDIA, 0x0abc),
1330 		.driver_data = board_ahci_mcp79,
1331 	}, {
1332 		/* MCP79 */
1333 		PCI_VDEVICE(NVIDIA, 0x0abd),
1334 		.driver_data = board_ahci_mcp79,
1335 	}, {
1336 		/* MCP79 */
1337 		PCI_VDEVICE(NVIDIA, 0x0abe),
1338 		.driver_data = board_ahci_mcp79,
1339 	}, {
1340 		/* MCP79 */
1341 		PCI_VDEVICE(NVIDIA, 0x0abf),
1342 		.driver_data = board_ahci_mcp79,
1343 	}, {
1344 		/* MCP89 */
1345 		PCI_VDEVICE(NVIDIA, 0x0d84),
1346 		.driver_data = board_ahci_mcp89,
1347 	}, {
1348 		/* MCP89 */
1349 		PCI_VDEVICE(NVIDIA, 0x0d85),
1350 		.driver_data = board_ahci_mcp89,
1351 	}, {
1352 		/* MCP89 */
1353 		PCI_VDEVICE(NVIDIA, 0x0d86),
1354 		.driver_data = board_ahci_mcp89,
1355 	}, {
1356 		/* MCP89 */
1357 		PCI_VDEVICE(NVIDIA, 0x0d87),
1358 		.driver_data = board_ahci_mcp89,
1359 	}, {
1360 		/* MCP89 */
1361 		PCI_VDEVICE(NVIDIA, 0x0d88),
1362 		.driver_data = board_ahci_mcp89,
1363 	}, {
1364 		/* MCP89 */
1365 		PCI_VDEVICE(NVIDIA, 0x0d89),
1366 		.driver_data = board_ahci_mcp89,
1367 	}, {
1368 		/* MCP89 */
1369 		PCI_VDEVICE(NVIDIA, 0x0d8a),
1370 		.driver_data = board_ahci_mcp89,
1371 	}, {
1372 		/* MCP89 */
1373 		PCI_VDEVICE(NVIDIA, 0x0d8b),
1374 		.driver_data = board_ahci_mcp89,
1375 	}, {
1376 		/* MCP89 */
1377 		PCI_VDEVICE(NVIDIA, 0x0d8c),
1378 		.driver_data = board_ahci_mcp89,
1379 	}, {
1380 		/* MCP89 */
1381 		PCI_VDEVICE(NVIDIA, 0x0d8d),
1382 		.driver_data = board_ahci_mcp89,
1383 	}, {
1384 		/* MCP89 */
1385 		PCI_VDEVICE(NVIDIA, 0x0d8e),
1386 		.driver_data = board_ahci_mcp89,
1387 	}, {
1388 		/* MCP89 */
1389 		PCI_VDEVICE(NVIDIA, 0x0d8f),
1390 		.driver_data = board_ahci_mcp89,
1391 	},
1392 
1393 	/* SiS */
1394 	{
1395 		/* SiS 966 */
1396 		PCI_VDEVICE(SI, 0x1184),
1397 		.driver_data = board_ahci,
1398 	}, {
1399 		/* SiS 968 */
1400 		PCI_VDEVICE(SI, 0x1185),
1401 		.driver_data = board_ahci,
1402 	}, {
1403 		/* SiS 968 */
1404 		PCI_VDEVICE(SI, 0x0186),
1405 		.driver_data = board_ahci,
1406 	},
1407 
1408 	/* ST Microelectronics */
1409 	{
1410 		/* ST ConneXt */
1411 		PCI_VDEVICE(STMICRO, 0xCC06),
1412 		.driver_data = board_ahci,
1413 	},
1414 
1415 	/* Marvell */
1416 	{
1417 		/* 6145 */
1418 		PCI_VDEVICE(MARVELL, 0x6145),
1419 		.driver_data = board_ahci_mv,
1420 	}, {
1421 		/* 6121 */
1422 		PCI_VDEVICE(MARVELL, 0x6121),
1423 		.driver_data = board_ahci_mv,
1424 	}, {
1425 		/* 88se9128 */
1426 		PCI_DEVICE(PCI_VENDOR_ID_MARVELL_EXT, 0x9123),
1427 			.class = PCI_CLASS_STORAGE_SATA_AHCI,
1428 			.class_mask = 0xffffff,
1429 		.driver_data = board_ahci_yes_fbs,
1430 	}, {
1431 		/* 88se9125 */
1432 		PCI_DEVICE(PCI_VENDOR_ID_MARVELL_EXT, 0x9125),
1433 		.driver_data = board_ahci_yes_fbs,
1434 	}, {
1435 		/* 88se9170 */
1436 		PCI_DEVICE_SUB(PCI_VENDOR_ID_MARVELL_EXT, 0x9178,
1437 			       PCI_VENDOR_ID_MARVELL_EXT, 0x9170),
1438 		.driver_data = board_ahci_yes_fbs,
1439 	}, {
1440 		/* 88se9172 */
1441 		PCI_DEVICE(PCI_VENDOR_ID_MARVELL_EXT, 0x917a),
1442 		.driver_data = board_ahci_yes_fbs,
1443 	}, {
1444 		/* 88se9182 */
1445 		PCI_DEVICE(PCI_VENDOR_ID_MARVELL_EXT, 0x9172),
1446 		.driver_data = board_ahci_yes_fbs,
1447 	}, {
1448 		/* 88se9172 */
1449 		PCI_DEVICE(PCI_VENDOR_ID_MARVELL_EXT, 0x9182),
1450 		.driver_data = board_ahci_yes_fbs,
1451 	}, {
1452 		/* 88se9172 on some Gigabyte */
1453 		PCI_DEVICE(PCI_VENDOR_ID_MARVELL_EXT, 0x9192),
1454 		.driver_data = board_ahci_yes_fbs,
1455 	}, {
1456 		PCI_DEVICE(PCI_VENDOR_ID_MARVELL_EXT, 0x91a0),
1457 		.driver_data = board_ahci_yes_fbs,
1458 	}, {
1459 		PCI_DEVICE(PCI_VENDOR_ID_MARVELL_EXT, 0x91a2), 	/* 88se91a2 */
1460 		.driver_data = board_ahci_yes_fbs,
1461 	}, {
1462 		PCI_DEVICE(PCI_VENDOR_ID_MARVELL_EXT, 0x91a3),
1463 		.driver_data = board_ahci_yes_fbs,
1464 	}, {
1465 		PCI_DEVICE(PCI_VENDOR_ID_MARVELL_EXT, 0x9215),
1466 		.driver_data = board_ahci_yes_fbs_atapi_dma,
1467 	}, {
1468 		PCI_DEVICE(PCI_VENDOR_ID_MARVELL_EXT, 0x9230),
1469 		.driver_data = board_ahci_yes_fbs,
1470 	}, {
1471 		PCI_DEVICE(PCI_VENDOR_ID_MARVELL_EXT, 0x9235),
1472 		.driver_data = board_ahci_no_debounce_delay,
1473 	},
1474 
1475 	/* TTI */
1476 	{
1477 		/* highpoint rocketraid 642L */
1478 		PCI_DEVICE(PCI_VENDOR_ID_TTI, 0x0642),
1479 		.driver_data = board_ahci_yes_fbs,
1480 	}, {
1481 		/* highpoint rocketraid 644L */
1482 		PCI_DEVICE(PCI_VENDOR_ID_TTI, 0x0645),
1483 		.driver_data = board_ahci_yes_fbs,
1484 	},
1485 
1486 	/* Promise */
1487 	{
1488 		/* PDC42819 */
1489 		PCI_VDEVICE(PROMISE, 0x3f20),
1490 		.driver_data = board_ahci,
1491 	}, {
1492 		/* FastTrak TX8660 ahci-mode */
1493 		PCI_VDEVICE(PROMISE, 0x3781),
1494 		.driver_data = board_ahci,
1495 	},
1496 
1497 	/* ASMedia */
1498 	{
1499 		/* ASM1060 */
1500 		PCI_VDEVICE(ASMEDIA, 0x0601),
1501 		.driver_data = board_ahci_43bit_dma,
1502 	}, {
1503 		/* ASM1060 */
1504 		PCI_VDEVICE(ASMEDIA, 0x0602),
1505 		.driver_data = board_ahci_43bit_dma,
1506 	}, {
1507 		/* ASM1061 */
1508 		PCI_VDEVICE(ASMEDIA, 0x0611),
1509 		.driver_data = board_ahci_43bit_dma,
1510 	}, {
1511 		/* ASM1061/1062 */
1512 		PCI_VDEVICE(ASMEDIA, 0x0612),
1513 		.driver_data = board_ahci_43bit_dma,
1514 	}, {
1515 		/* ASM1061R */
1516 		PCI_VDEVICE(ASMEDIA, 0x0621),
1517 		.driver_data = board_ahci_43bit_dma,
1518 	}, {
1519 		/* ASM1062R */
1520 		PCI_VDEVICE(ASMEDIA, 0x0622),
1521 		.driver_data = board_ahci_43bit_dma,
1522 	}, {
1523 		/* ASM1062+JMB575 */
1524 		PCI_VDEVICE(ASMEDIA, 0x0624),
1525 		.driver_data = board_ahci_43bit_dma,
1526 	}, {
1527 		/* ASM1062A */
1528 		PCI_VDEVICE(ASMEDIA, 0x1062),
1529 		.driver_data = board_ahci,
1530 	}, {
1531 		/* ASM1064 */
1532 		PCI_VDEVICE(ASMEDIA, 0x1064),
1533 		.driver_data = board_ahci,
1534 	}, {
1535 		/* ASM1164 */
1536 		PCI_VDEVICE(ASMEDIA, 0x1164),
1537 		.driver_data = board_ahci,
1538 	}, {
1539 		/* ASM1165 */
1540 		PCI_VDEVICE(ASMEDIA, 0x1165),
1541 		.driver_data = board_ahci,
1542 	}, {
1543 		/* ASM1166 */
1544 		PCI_VDEVICE(ASMEDIA, 0x1166),
1545 		.driver_data = board_ahci,
1546 	}, {
1547 		/*
1548 		 * Samsung SSDs found on some macbooks.  NCQ times out if MSI is
1549 		 * enabled.  https://bugzilla.kernel.org/show_bug.cgi?id=60731
1550 		 */
1551 		PCI_VDEVICE(SAMSUNG, 0x1600),
1552 		.driver_data = board_ahci_no_msi,
1553 
1554 	}, {
1555 		PCI_VDEVICE(SAMSUNG, 0xa800),
1556 		.driver_data = board_ahci_no_msi,
1557 	}, {
1558 		/* Enmotus */
1559 		PCI_DEVICE(0x1c44, 0x8000),
1560 		.driver_data = board_ahci,
1561 	}, {
1562 		/* Loongson */
1563 		PCI_VDEVICE(LOONGSON, 0x7a08),
1564 		.driver_data = board_ahci,
1565 
1566 	}, {
1567 		/* Generic, PCI class code for AHCI */
1568 		PCI_DEVICE_CLASS(PCI_CLASS_STORAGE_SATA_AHCI, 0xffffff),
1569 		.driver_data = board_ahci,
1570 	},
1571 
1572 	{ }	/* terminate list */
1573 };
1574 
1575 static const struct dev_pm_ops ahci_pci_pm_ops = {
1576 	SET_SYSTEM_SLEEP_PM_OPS(ahci_pci_device_suspend, ahci_pci_device_resume)
1577 	SET_RUNTIME_PM_OPS(ahci_pci_device_runtime_suspend,
1578 			   ahci_pci_device_runtime_resume, NULL)
1579 };
1580 
1581 static struct pci_driver ahci_pci_driver = {
1582 	.name			= DRV_NAME,
1583 	.id_table		= ahci_pci_tbl,
1584 	.probe			= ahci_init_one,
1585 	.remove			= ahci_remove_one,
1586 	.shutdown		= ahci_shutdown_one,
1587 	.driver = {
1588 		.pm		= &ahci_pci_pm_ops,
1589 	},
1590 };
1591 
1592 #if IS_ENABLED(CONFIG_PATA_MARVELL)
1593 static int marvell_enable;
1594 #else
1595 static int marvell_enable = 1;
1596 #endif
1597 module_param(marvell_enable, int, 0644);
1598 MODULE_PARM_DESC(marvell_enable, "Marvell SATA via AHCI (1 = enabled)");
1599 
1600 static int mobile_lpm_policy = -1;
1601 module_param(mobile_lpm_policy, int, 0644);
1602 MODULE_PARM_DESC(mobile_lpm_policy,
1603 		 "Default LPM policy. Despite its name, this parameter applies "
1604 		 "to all chipsets, including desktop and server chipsets");
1605 
1606 static char *ahci_mask_port_map;
1607 module_param_named(mask_port_map, ahci_mask_port_map, charp, 0444);
1608 MODULE_PARM_DESC(mask_port_map,
1609 		 "32-bits port map masks to ignore controllers ports. "
1610 		 "Valid values are: "
1611 		 "\"<mask>\" to apply the same mask to all AHCI controller "
1612 		 "devices, and \"<pci_dev>=<mask>,<pci_dev>=<mask>,...\" to "
1613 		 "specify different masks for the controllers specified, "
1614 		 "where <pci_dev> is the PCI ID of an AHCI controller in the "
1615 		 "form \"domain:bus:dev.func\"");
1616 
1617 static char *ahci_mask_port_ext;
1618 module_param_named(mask_port_ext, ahci_mask_port_ext, charp, 0444);
1619 MODULE_PARM_DESC(mask_port_ext,
1620 		 "32-bits mask to ignore the external/hotplug capability of ports. "
1621 		 "Valid values are: "
1622 		 "\"<mask>\" to apply the same mask to all AHCI controller "
1623 		 "devices, and \"<pci_dev>=<mask>,<pci_dev>=<mask>,...\" to "
1624 		 "specify different masks for the controllers specified, "
1625 		 "where <pci_dev> is the PCI ID of an AHCI controller in the "
1626 		 "form \"domain:bus:dev.func\"");
1627 
1628 static void ahci_port_mask(struct device *dev, char *mask_s, u32 *mask)
1629 {
1630 	if (kstrtou32(mask_s, 0, mask))
1631 		dev_err(dev, "Invalid port map mask\n");
1632 }
1633 
1634 static void ahci_get_port_mask(struct device *dev, char *mask_p, u32 *mask)
1635 {
1636 	char *param, *end, *str, *mask_s;
1637 	char *name;
1638 
1639 	if (!mask_p || !strlen(mask_p))
1640 		return;
1641 
1642 	str = kstrdup(mask_p, GFP_KERNEL);
1643 	if (!str)
1644 		return;
1645 
1646 	/* Handle single mask case */
1647 	if (!strchr(str, '=')) {
1648 		ahci_port_mask(dev, str, mask);
1649 		goto free;
1650 	}
1651 
1652 	/*
1653 	 * Mask list case: parse the parameter to get the mask only if
1654 	 * the device name matches.
1655 	 */
1656 	param = str;
1657 	end = param + strlen(param);
1658 	while (param && param < end && *param) {
1659 		name = param;
1660 		param = strchr(name, '=');
1661 		if (!param)
1662 			break;
1663 
1664 		*param = '\0';
1665 		param++;
1666 		if (param >= end)
1667 			break;
1668 
1669 		if (strcmp(dev_name(dev), name) != 0) {
1670 			param = strchr(param, ',');
1671 			if (param)
1672 				param++;
1673 			continue;
1674 		}
1675 
1676 		mask_s = param;
1677 		param = strchr(mask_s, ',');
1678 		if (param) {
1679 			*param = '\0';
1680 			param++;
1681 		}
1682 
1683 		ahci_port_mask(dev, mask_s, mask);
1684 	}
1685 
1686 free:
1687 	kfree(str);
1688 }
1689 
1690 static void ahci_pci_save_initial_config(struct pci_dev *pdev,
1691 					 struct ahci_host_priv *hpriv)
1692 {
1693 	if (pdev->vendor == PCI_VENDOR_ID_JMICRON && pdev->device == 0x2361) {
1694 		dev_info(&pdev->dev, "JMB361 has only one port\n");
1695 		hpriv->saved_port_map = 1;
1696 	}
1697 
1698 	/*
1699 	 * Temporary Marvell 6145 hack: PATA port presence
1700 	 * is asserted through the standard AHCI port
1701 	 * presence register, as bit 4 (counting from 0)
1702 	 */
1703 	if (hpriv->flags & AHCI_HFLAG_MV_PATA) {
1704 		if (pdev->device == 0x6121)
1705 			hpriv->mask_port_map = 0x3;
1706 		else
1707 			hpriv->mask_port_map = 0xf;
1708 		dev_info(&pdev->dev,
1709 			  "Disabling your PATA port. Use the boot option 'ahci.marvell_enable=0' to avoid this.\n");
1710 	}
1711 
1712 	/* Handle port map masks passed as module parameter. */
1713 	ahci_get_port_mask(&pdev->dev, ahci_mask_port_map,
1714 			   &hpriv->mask_port_map);
1715 	ahci_get_port_mask(&pdev->dev, ahci_mask_port_ext,
1716 			   &hpriv->mask_port_ext);
1717 
1718 	ahci_save_initial_config(&pdev->dev, hpriv);
1719 }
1720 
1721 static int ahci_pci_reset_controller(struct ata_host *host)
1722 {
1723 	struct pci_dev *pdev = to_pci_dev(host->dev);
1724 	struct ahci_host_priv *hpriv = host->private_data;
1725 	int rc;
1726 
1727 	rc = ahci_reset_controller(host);
1728 	if (rc)
1729 		return rc;
1730 
1731 	/*
1732 	 * If platform firmware failed to enable ports, try to enable
1733 	 * them here.
1734 	 */
1735 	ahci_intel_pcs_quirk(pdev, hpriv);
1736 
1737 	return 0;
1738 }
1739 
1740 static void ahci_pci_init_controller(struct ata_host *host)
1741 {
1742 	struct ahci_host_priv *hpriv = host->private_data;
1743 	struct pci_dev *pdev = to_pci_dev(host->dev);
1744 	void __iomem *port_mmio;
1745 	u32 tmp;
1746 	int mv;
1747 
1748 	if (hpriv->flags & AHCI_HFLAG_MV_PATA) {
1749 		if (pdev->device == 0x6121)
1750 			mv = 2;
1751 		else
1752 			mv = 4;
1753 		port_mmio = __ahci_port_base(hpriv, mv);
1754 
1755 		writel(0, port_mmio + PORT_IRQ_MASK);
1756 
1757 		/* clear port IRQ */
1758 		tmp = readl(port_mmio + PORT_IRQ_STAT);
1759 		dev_dbg(&pdev->dev, "PORT_IRQ_STAT 0x%x\n", tmp);
1760 		if (tmp)
1761 			writel(tmp, port_mmio + PORT_IRQ_STAT);
1762 	}
1763 
1764 	ahci_init_controller(host);
1765 }
1766 
1767 static int ahci_vt8251_hardreset(struct ata_link *link, unsigned int *class,
1768 				 unsigned long deadline)
1769 {
1770 	struct ata_port *ap = link->ap;
1771 	struct ahci_host_priv *hpriv = ap->host->private_data;
1772 	bool online;
1773 	int rc;
1774 
1775 	hpriv->stop_engine(ap);
1776 
1777 	rc = sata_link_hardreset(link, sata_ehc_deb_timing(&link->eh_context),
1778 				 deadline, &online, NULL);
1779 
1780 	hpriv->start_engine(ap);
1781 
1782 	/* vt8251 doesn't clear BSY on signature FIS reception,
1783 	 * request follow-up softreset.
1784 	 */
1785 	return online ? -EAGAIN : rc;
1786 }
1787 
1788 static int ahci_p5wdh_hardreset(struct ata_link *link, unsigned int *class,
1789 				unsigned long deadline)
1790 {
1791 	struct ata_port *ap = link->ap;
1792 	struct ahci_port_priv *pp = ap->private_data;
1793 	struct ahci_host_priv *hpriv = ap->host->private_data;
1794 	u8 *d2h_fis = pp->rx_fis + RX_FIS_D2H_REG;
1795 	struct ata_taskfile tf;
1796 	bool online;
1797 	int rc;
1798 
1799 	hpriv->stop_engine(ap);
1800 
1801 	/* clear D2H reception area to properly wait for D2H FIS */
1802 	ata_tf_init(link->device, &tf);
1803 	tf.status = ATA_BUSY;
1804 	ata_tf_to_fis(&tf, 0, 0, d2h_fis);
1805 
1806 	rc = sata_link_hardreset(link, sata_ehc_deb_timing(&link->eh_context),
1807 				 deadline, &online, NULL);
1808 
1809 	hpriv->start_engine(ap);
1810 
1811 	/* The pseudo configuration device on SIMG4726 attached to
1812 	 * ASUS P5W-DH Deluxe doesn't send signature FIS after
1813 	 * hardreset if no device is attached to the first downstream
1814 	 * port && the pseudo device locks up on SRST w/ PMP==0.  To
1815 	 * work around this, wait for !BSY only briefly.  If BSY isn't
1816 	 * cleared, perform CLO and proceed to IDENTIFY (achieved by
1817 	 * ATA_LFLAG_NO_SRST and ATA_LFLAG_ASSUME_ATA).
1818 	 *
1819 	 * Wait for two seconds.  Devices attached to downstream port
1820 	 * which can't process the following IDENTIFY after this will
1821 	 * have to be reset again.  For most cases, this should
1822 	 * suffice while making probing snappish enough.
1823 	 */
1824 	if (online) {
1825 		rc = ata_wait_after_reset(link, jiffies + 2 * HZ,
1826 					  ahci_check_ready);
1827 		if (rc)
1828 			ahci_kick_engine(ap);
1829 	}
1830 	return rc;
1831 }
1832 
1833 /*
1834  * ahci_avn_hardreset - attempt more aggressive recovery of Avoton ports.
1835  *
1836  * It has been observed with some SSDs that the timing of events in the
1837  * link synchronization phase can leave the port in a state that can not
1838  * be recovered by a SATA-hard-reset alone.  The failing signature is
1839  * SStatus.DET stuck at 1 ("Device presence detected but Phy
1840  * communication not established").  It was found that unloading and
1841  * reloading the driver when this problem occurs allows the drive
1842  * connection to be recovered (DET advanced to 0x3).  The critical
1843  * component of reloading the driver is that the port state machines are
1844  * reset by bouncing "port enable" in the AHCI PCS configuration
1845  * register.  So, reproduce that effect by bouncing a port whenever we
1846  * see DET==1 after a reset.
1847  */
1848 static int ahci_avn_hardreset(struct ata_link *link, unsigned int *class,
1849 			      unsigned long deadline)
1850 {
1851 	const unsigned int *timing = sata_ehc_deb_timing(&link->eh_context);
1852 	struct ata_port *ap = link->ap;
1853 	struct ahci_port_priv *pp = ap->private_data;
1854 	struct ahci_host_priv *hpriv = ap->host->private_data;
1855 	u8 *d2h_fis = pp->rx_fis + RX_FIS_D2H_REG;
1856 	unsigned long tmo = deadline - jiffies;
1857 	struct ata_taskfile tf;
1858 	bool online;
1859 	int rc, i;
1860 
1861 	hpriv->stop_engine(ap);
1862 
1863 	for (i = 0; i < 2; i++) {
1864 		u16 val;
1865 		u32 sstatus;
1866 		int port = ap->port_no;
1867 		struct ata_host *host = ap->host;
1868 		struct pci_dev *pdev = to_pci_dev(host->dev);
1869 
1870 		/* clear D2H reception area to properly wait for D2H FIS */
1871 		ata_tf_init(link->device, &tf);
1872 		tf.status = ATA_BUSY;
1873 		ata_tf_to_fis(&tf, 0, 0, d2h_fis);
1874 
1875 		rc = sata_link_hardreset(link, timing, deadline, &online,
1876 				ahci_check_ready);
1877 
1878 		if (sata_scr_read(link, SCR_STATUS, &sstatus) != 0 ||
1879 				(sstatus & 0xf) != 1)
1880 			break;
1881 
1882 		ata_link_info(link,  "avn bounce port%d\n", port);
1883 
1884 		pci_read_config_word(pdev, 0x92, &val);
1885 		val &= ~(1 << port);
1886 		pci_write_config_word(pdev, 0x92, val);
1887 		ata_msleep(ap, 1000);
1888 		val |= 1 << port;
1889 		pci_write_config_word(pdev, 0x92, val);
1890 		deadline += tmo;
1891 	}
1892 
1893 	hpriv->start_engine(ap);
1894 
1895 	if (online)
1896 		*class = ahci_dev_classify(ap);
1897 
1898 	return rc;
1899 }
1900 
1901 
1902 #ifdef CONFIG_PM
1903 static void ahci_pci_disable_interrupts(struct ata_host *host)
1904 {
1905 	struct ahci_host_priv *hpriv = host->private_data;
1906 	void __iomem *mmio = hpriv->mmio;
1907 	u32 ctl;
1908 
1909 	/* AHCI spec rev1.1 section 8.3.3:
1910 	 * Software must disable interrupts prior to requesting a
1911 	 * transition of the HBA to D3 state.
1912 	 */
1913 	ctl = readl(mmio + HOST_CTL);
1914 	ctl &= ~HOST_IRQ_EN;
1915 	writel(ctl, mmio + HOST_CTL);
1916 	readl(mmio + HOST_CTL); /* flush */
1917 }
1918 
1919 static int ahci_pci_device_runtime_suspend(struct device *dev)
1920 {
1921 	struct pci_dev *pdev = to_pci_dev(dev);
1922 	struct ata_host *host = pci_get_drvdata(pdev);
1923 
1924 	ahci_pci_disable_interrupts(host);
1925 	return 0;
1926 }
1927 
1928 static int ahci_pci_device_runtime_resume(struct device *dev)
1929 {
1930 	struct pci_dev *pdev = to_pci_dev(dev);
1931 	struct ata_host *host = pci_get_drvdata(pdev);
1932 	int rc;
1933 
1934 	rc = ahci_pci_reset_controller(host);
1935 	if (rc)
1936 		return rc;
1937 	ahci_pci_init_controller(host);
1938 	return 0;
1939 }
1940 
1941 #ifdef CONFIG_PM_SLEEP
1942 static int ahci_pci_device_suspend(struct device *dev)
1943 {
1944 	struct pci_dev *pdev = to_pci_dev(dev);
1945 	struct ata_host *host = pci_get_drvdata(pdev);
1946 	struct ahci_host_priv *hpriv = host->private_data;
1947 
1948 	if (hpriv->flags & AHCI_HFLAG_NO_SUSPEND) {
1949 		dev_err(&pdev->dev,
1950 			"BIOS update required for suspend/resume\n");
1951 		return -EIO;
1952 	}
1953 
1954 	ahci_pci_disable_interrupts(host);
1955 	ata_host_suspend(host, PMSG_SUSPEND);
1956 	return 0;
1957 }
1958 
1959 static int ahci_pci_device_resume(struct device *dev)
1960 {
1961 	struct pci_dev *pdev = to_pci_dev(dev);
1962 	struct ata_host *host = pci_get_drvdata(pdev);
1963 	int rc;
1964 
1965 	/* Apple BIOS helpfully mangles the registers on resume */
1966 	if (is_mcp89_apple(pdev))
1967 		ahci_mcp89_apple_enable(pdev);
1968 
1969 	if (pdev->dev.power.power_state.event == PM_EVENT_SUSPEND) {
1970 		rc = ahci_pci_reset_controller(host);
1971 		if (rc)
1972 			return rc;
1973 
1974 		ahci_pci_init_controller(host);
1975 	}
1976 
1977 	ata_host_resume(host);
1978 
1979 	return 0;
1980 }
1981 #endif
1982 
1983 #endif /* CONFIG_PM */
1984 
1985 static int ahci_configure_dma_masks(struct pci_dev *pdev,
1986 				    struct ahci_host_priv *hpriv)
1987 {
1988 	int dma_bits;
1989 	int rc;
1990 
1991 	if (hpriv->cap & HOST_CAP_64) {
1992 		dma_bits = 64;
1993 		if (hpriv->flags & AHCI_HFLAG_43BIT_ONLY)
1994 			dma_bits = 43;
1995 	} else {
1996 		dma_bits = 32;
1997 	}
1998 
1999 	/*
2000 	 * If the device fixup already set the dma_mask to some non-standard
2001 	 * value, don't extend it here. This happens on STA2X11, for example.
2002 	 *
2003 	 * XXX: manipulating the DMA mask from platform code is completely
2004 	 * bogus, platform code should use dev->bus_dma_limit instead..
2005 	 */
2006 	if (pdev->dma_mask && pdev->dma_mask < DMA_BIT_MASK(32))
2007 		return 0;
2008 
2009 	rc = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(dma_bits));
2010 	if (rc)
2011 		dev_err(&pdev->dev, "DMA enable failed\n");
2012 	return rc;
2013 }
2014 
2015 static void ahci_pci_print_info(struct ata_host *host)
2016 {
2017 	struct pci_dev *pdev = to_pci_dev(host->dev);
2018 	u16 cc;
2019 	const char *scc_s;
2020 
2021 	pci_read_config_word(pdev, 0x0a, &cc);
2022 	if (cc == PCI_CLASS_STORAGE_IDE)
2023 		scc_s = "IDE";
2024 	else if (cc == PCI_CLASS_STORAGE_SATA)
2025 		scc_s = "SATA";
2026 	else if (cc == PCI_CLASS_STORAGE_RAID)
2027 		scc_s = "RAID";
2028 	else
2029 		scc_s = "unknown";
2030 
2031 	ahci_print_info(host, scc_s);
2032 }
2033 
2034 /* On ASUS P5W DH Deluxe, the second port of PCI device 00:1f.2 is
2035  * hardwired to on-board SIMG 4726.  The chipset is ICH8 and doesn't
2036  * support PMP and the 4726 either directly exports the device
2037  * attached to the first downstream port or acts as a hardware storage
2038  * controller and emulate a single ATA device (can be RAID 0/1 or some
2039  * other configuration).
2040  *
2041  * When there's no device attached to the first downstream port of the
2042  * 4726, "Config Disk" appears, which is a pseudo ATA device to
2043  * configure the 4726.  However, ATA emulation of the device is very
2044  * lame.  It doesn't send signature D2H Reg FIS after the initial
2045  * hardreset, pukes on SRST w/ PMP==0 and has bunch of other issues.
2046  *
2047  * The following function works around the problem by always using
2048  * hardreset on the port and not depending on receiving signature FIS
2049  * afterward.  If signature FIS isn't received soon, ATA class is
2050  * assumed without follow-up softreset.
2051  */
2052 static void ahci_p5wdh_workaround(struct ata_host *host)
2053 {
2054 	static const struct dmi_system_id sysids[] = {
2055 		{
2056 			.ident = "P5W DH Deluxe",
2057 			.matches = {
2058 				DMI_MATCH(DMI_SYS_VENDOR,
2059 					  "ASUSTEK COMPUTER INC"),
2060 				DMI_MATCH(DMI_PRODUCT_NAME, "P5W DH Deluxe"),
2061 			},
2062 		},
2063 		{ }
2064 	};
2065 	struct pci_dev *pdev = to_pci_dev(host->dev);
2066 
2067 	if (pdev->bus->number == 0 && pdev->devfn == PCI_DEVFN(0x1f, 2) &&
2068 	    dmi_check_system(sysids)) {
2069 		struct ata_port *ap = host->ports[1];
2070 
2071 		dev_info(&pdev->dev,
2072 			 "enabling ASUS P5W DH Deluxe on-board SIMG4726 workaround\n");
2073 
2074 		ap->ops = &ahci_p5wdh_ops;
2075 		ap->link.flags |= ATA_LFLAG_NO_SRST | ATA_LFLAG_ASSUME_ATA;
2076 	}
2077 }
2078 
2079 /*
2080  * Macbook7,1 firmware forcibly disables MCP89 AHCI and changes PCI ID when
2081  * booting in BIOS compatibility mode.  We restore the registers but not ID.
2082  */
2083 static void ahci_mcp89_apple_enable(struct pci_dev *pdev)
2084 {
2085 	u32 val;
2086 
2087 	printk(KERN_INFO "ahci: enabling MCP89 AHCI mode\n");
2088 
2089 	pci_read_config_dword(pdev, 0xf8, &val);
2090 	val |= 1 << 0x1b;
2091 	/* the following changes the device ID, but appears not to affect function */
2092 	/* val = (val & ~0xf0000000) | 0x80000000; */
2093 	pci_write_config_dword(pdev, 0xf8, val);
2094 
2095 	pci_read_config_dword(pdev, 0x54c, &val);
2096 	val |= 1 << 0xc;
2097 	pci_write_config_dword(pdev, 0x54c, val);
2098 
2099 	pci_read_config_dword(pdev, 0x4a4, &val);
2100 	val &= 0xff;
2101 	val |= 0x01060100;
2102 	pci_write_config_dword(pdev, 0x4a4, val);
2103 
2104 	pci_read_config_dword(pdev, 0x54c, &val);
2105 	val &= ~(1 << 0xc);
2106 	pci_write_config_dword(pdev, 0x54c, val);
2107 
2108 	pci_read_config_dword(pdev, 0xf8, &val);
2109 	val &= ~(1 << 0x1b);
2110 	pci_write_config_dword(pdev, 0xf8, val);
2111 }
2112 
2113 static bool is_mcp89_apple(struct pci_dev *pdev)
2114 {
2115 	return pdev->vendor == PCI_VENDOR_ID_NVIDIA &&
2116 		pdev->device == PCI_DEVICE_ID_NVIDIA_NFORCE_MCP89_SATA &&
2117 		pdev->subsystem_vendor == PCI_VENDOR_ID_APPLE &&
2118 		pdev->subsystem_device == 0xcb89;
2119 }
2120 
2121 /* only some SB600 ahci controllers can do 64bit DMA */
2122 static bool ahci_sb600_enable_64bit(struct pci_dev *pdev)
2123 {
2124 	static const struct dmi_system_id sysids[] = {
2125 		/*
2126 		 * The oldest version known to be broken is 0901 and
2127 		 * working is 1501 which was released on 2007-10-26.
2128 		 * Enable 64bit DMA on 1501 and anything newer.
2129 		 *
2130 		 * Please read bko#9412 for more info.
2131 		 */
2132 		{
2133 			.ident = "ASUS M2A-VM",
2134 			.matches = {
2135 				DMI_MATCH(DMI_BOARD_VENDOR,
2136 					  "ASUSTeK Computer INC."),
2137 				DMI_MATCH(DMI_BOARD_NAME, "M2A-VM"),
2138 			},
2139 			.driver_data = "20071026",	/* yyyymmdd */
2140 		},
2141 		/*
2142 		 * All BIOS versions for the MSI K9A2 Platinum (MS-7376)
2143 		 * support 64bit DMA.
2144 		 *
2145 		 * BIOS versions earlier than 1.5 had the Manufacturer DMI
2146 		 * fields as "MICRO-STAR INTERANTIONAL CO.,LTD".
2147 		 * This spelling mistake was fixed in BIOS version 1.5, so
2148 		 * 1.5 and later have the Manufacturer as
2149 		 * "MICRO-STAR INTERNATIONAL CO.,LTD".
2150 		 * So try to match on DMI_BOARD_VENDOR of "MICRO-STAR INTER".
2151 		 *
2152 		 * BIOS versions earlier than 1.9 had a Board Product Name
2153 		 * DMI field of "MS-7376". This was changed to be
2154 		 * "K9A2 Platinum (MS-7376)" in version 1.9, but we can still
2155 		 * match on DMI_BOARD_NAME of "MS-7376".
2156 		 */
2157 		{
2158 			.ident = "MSI K9A2 Platinum",
2159 			.matches = {
2160 				DMI_MATCH(DMI_BOARD_VENDOR,
2161 					  "MICRO-STAR INTER"),
2162 				DMI_MATCH(DMI_BOARD_NAME, "MS-7376"),
2163 			},
2164 		},
2165 		/*
2166 		 * All BIOS versions for the MSI K9AGM2 (MS-7327) support
2167 		 * 64bit DMA.
2168 		 *
2169 		 * This board also had the typo mentioned above in the
2170 		 * Manufacturer DMI field (fixed in BIOS version 1.5), so
2171 		 * match on DMI_BOARD_VENDOR of "MICRO-STAR INTER" again.
2172 		 */
2173 		{
2174 			.ident = "MSI K9AGM2",
2175 			.matches = {
2176 				DMI_MATCH(DMI_BOARD_VENDOR,
2177 					  "MICRO-STAR INTER"),
2178 				DMI_MATCH(DMI_BOARD_NAME, "MS-7327"),
2179 			},
2180 		},
2181 		/*
2182 		 * All BIOS versions for the Asus M3A support 64bit DMA.
2183 		 * (all release versions from 0301 to 1206 were tested)
2184 		 */
2185 		{
2186 			.ident = "ASUS M3A",
2187 			.matches = {
2188 				DMI_MATCH(DMI_BOARD_VENDOR,
2189 					  "ASUSTeK Computer INC."),
2190 				DMI_MATCH(DMI_BOARD_NAME, "M3A"),
2191 			},
2192 		},
2193 		{ }
2194 	};
2195 	const struct dmi_system_id *match;
2196 	int year, month, date;
2197 	char buf[9];
2198 
2199 	match = dmi_first_match(sysids);
2200 	if (pdev->bus->number != 0 || pdev->devfn != PCI_DEVFN(0x12, 0) ||
2201 	    !match)
2202 		return false;
2203 
2204 	if (!match->driver_data)
2205 		goto enable_64bit;
2206 
2207 	dmi_get_date(DMI_BIOS_DATE, &year, &month, &date);
2208 	snprintf(buf, sizeof(buf), "%04d%02d%02d", year, month, date);
2209 
2210 	if (strcmp(buf, match->driver_data) >= 0)
2211 		goto enable_64bit;
2212 	else {
2213 		dev_warn(&pdev->dev,
2214 			 "%s: BIOS too old, forcing 32bit DMA, update BIOS\n",
2215 			 match->ident);
2216 		return false;
2217 	}
2218 
2219 enable_64bit:
2220 	dev_warn(&pdev->dev, "%s: enabling 64bit DMA\n", match->ident);
2221 	return true;
2222 }
2223 
2224 static bool ahci_broken_system_poweroff(struct pci_dev *pdev)
2225 {
2226 	static const struct dmi_system_id broken_systems[] = {
2227 		{
2228 			.ident = "HP Compaq nx6310",
2229 			.matches = {
2230 				DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
2231 				DMI_MATCH(DMI_PRODUCT_NAME, "HP Compaq nx6310"),
2232 			},
2233 			/* PCI slot number of the controller */
2234 			.driver_data = (void *)0x1FUL,
2235 		},
2236 		{
2237 			.ident = "HP Compaq 6720s",
2238 			.matches = {
2239 				DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
2240 				DMI_MATCH(DMI_PRODUCT_NAME, "HP Compaq 6720s"),
2241 			},
2242 			/* PCI slot number of the controller */
2243 			.driver_data = (void *)0x1FUL,
2244 		},
2245 
2246 		{ }	/* terminate list */
2247 	};
2248 	const struct dmi_system_id *dmi = dmi_first_match(broken_systems);
2249 
2250 	if (dmi) {
2251 		unsigned long slot = (unsigned long)dmi->driver_data;
2252 		/* apply the quirk only to on-board controllers */
2253 		return slot == PCI_SLOT(pdev->devfn);
2254 	}
2255 
2256 	return false;
2257 }
2258 
2259 static bool ahci_broken_suspend(struct pci_dev *pdev)
2260 {
2261 	static const struct dmi_system_id sysids[] = {
2262 		/*
2263 		 * On HP dv[4-6] and HDX18 with earlier BIOSen, link
2264 		 * to the harddisk doesn't become online after
2265 		 * resuming from STR.  Warn and fail suspend.
2266 		 *
2267 		 * http://bugzilla.kernel.org/show_bug.cgi?id=12276
2268 		 *
2269 		 * Use dates instead of versions to match as HP is
2270 		 * apparently recycling both product and version
2271 		 * strings.
2272 		 *
2273 		 * http://bugzilla.kernel.org/show_bug.cgi?id=15462
2274 		 */
2275 		{
2276 			.ident = "dv4",
2277 			.matches = {
2278 				DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
2279 				DMI_MATCH(DMI_PRODUCT_NAME,
2280 					  "HP Pavilion dv4 Notebook PC"),
2281 			},
2282 			.driver_data = "20090105",	/* F.30 */
2283 		},
2284 		{
2285 			.ident = "dv5",
2286 			.matches = {
2287 				DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
2288 				DMI_MATCH(DMI_PRODUCT_NAME,
2289 					  "HP Pavilion dv5 Notebook PC"),
2290 			},
2291 			.driver_data = "20090506",	/* F.16 */
2292 		},
2293 		{
2294 			.ident = "dv6",
2295 			.matches = {
2296 				DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
2297 				DMI_MATCH(DMI_PRODUCT_NAME,
2298 					  "HP Pavilion dv6 Notebook PC"),
2299 			},
2300 			.driver_data = "20090423",	/* F.21 */
2301 		},
2302 		{
2303 			.ident = "HDX18",
2304 			.matches = {
2305 				DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
2306 				DMI_MATCH(DMI_PRODUCT_NAME,
2307 					  "HP HDX18 Notebook PC"),
2308 			},
2309 			.driver_data = "20090430",	/* F.23 */
2310 		},
2311 		/*
2312 		 * Acer eMachines G725 has the same problem.  BIOS
2313 		 * V1.03 is known to be broken.  V3.04 is known to
2314 		 * work.  Between, there are V1.06, V2.06 and V3.03
2315 		 * that we don't have much idea about.  For now,
2316 		 * assume that anything older than V3.04 is broken.
2317 		 *
2318 		 * http://bugzilla.kernel.org/show_bug.cgi?id=15104
2319 		 */
2320 		{
2321 			.ident = "G725",
2322 			.matches = {
2323 				DMI_MATCH(DMI_SYS_VENDOR, "eMachines"),
2324 				DMI_MATCH(DMI_PRODUCT_NAME, "eMachines G725"),
2325 			},
2326 			.driver_data = "20091216",	/* V3.04 */
2327 		},
2328 		{ }	/* terminate list */
2329 	};
2330 	const struct dmi_system_id *dmi = dmi_first_match(sysids);
2331 	int year, month, date;
2332 	char buf[9];
2333 
2334 	if (!dmi || pdev->bus->number || pdev->devfn != PCI_DEVFN(0x1f, 2))
2335 		return false;
2336 
2337 	dmi_get_date(DMI_BIOS_DATE, &year, &month, &date);
2338 	snprintf(buf, sizeof(buf), "%04d%02d%02d", year, month, date);
2339 
2340 	return strcmp(buf, dmi->driver_data) < 0;
2341 }
2342 
2343 static bool ahci_broken_lpm(struct pci_dev *pdev)
2344 {
2345 	/*
2346 	 * Platforms with LPM problems.
2347 	 * If driver_data is NULL, there is no existing BIOS version with
2348 	 * functioning LPM.
2349 	 * If driver_data is non-NULL, then driver_data contains the DMI BIOS
2350 	 * build date of the first BIOS version with functioning LPM (i.e. older
2351 	 * BIOS versions have broken LPM).
2352 	 */
2353 	static const struct dmi_system_id sysids[] = {
2354 		{
2355 			.matches = {
2356 				DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
2357 				DMI_MATCH(DMI_PRODUCT_VERSION, "ThinkPad X250"),
2358 			},
2359 			.driver_data = "20180406", /* 1.31 */
2360 		},
2361 		{
2362 			.matches = {
2363 				DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
2364 				DMI_MATCH(DMI_PRODUCT_VERSION, "ThinkPad L450"),
2365 			},
2366 			.driver_data = "20180420", /* 1.28 */
2367 		},
2368 		{
2369 			.matches = {
2370 				DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
2371 				DMI_MATCH(DMI_PRODUCT_VERSION, "ThinkPad T450s"),
2372 			},
2373 			.driver_data = "20180315", /* 1.33 */
2374 		},
2375 		{
2376 			.matches = {
2377 				DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
2378 				DMI_MATCH(DMI_PRODUCT_VERSION, "ThinkPad W541"),
2379 			},
2380 			.driver_data = "20180409", /* 2.35 */
2381 		},
2382 		{
2383 			.matches = {
2384 				DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK COMPUTER INC."),
2385 				DMI_MATCH(DMI_PRODUCT_NAME, "ASUSPRO D840MB_M840SA"),
2386 			},
2387 			/* 320 is broken, there is no known good version. */
2388 		},
2389 		{
2390 			/*
2391 			 * AMD 500 Series Chipset SATA Controller [1022:43eb]
2392 			 * on this motherboard timeouts on ports 5 and 6 when
2393 			 * LPM is enabled, at least with WDC WD20EFAX-68FB5N0
2394 			 * hard drives. LPM with the same drive works fine on
2395 			 * all other ports on the same controller.
2396 			 */
2397 			.matches = {
2398 				DMI_MATCH(DMI_BOARD_VENDOR,
2399 					  "ASUSTeK COMPUTER INC."),
2400 				DMI_MATCH(DMI_BOARD_NAME,
2401 					  "ROG STRIX B550-F GAMING (WI-FI)"),
2402 			},
2403 			/* 3621 is broken, there is no known good version. */
2404 		},
2405 		{ }	/* terminate list */
2406 	};
2407 	const struct dmi_system_id *dmi = dmi_first_match(sysids);
2408 	int year, month, date;
2409 	char buf[9];
2410 
2411 	if (!dmi)
2412 		return false;
2413 
2414 	if (!dmi->driver_data)
2415 		return true;
2416 
2417 	dmi_get_date(DMI_BIOS_DATE, &year, &month, &date);
2418 	snprintf(buf, sizeof(buf), "%04d%02d%02d", year, month, date);
2419 
2420 	return strcmp(buf, dmi->driver_data) < 0;
2421 }
2422 
2423 static bool ahci_broken_online(struct pci_dev *pdev)
2424 {
2425 #define ENCODE_BUSDEVFN(bus, slot, func)			\
2426 	(void *)(unsigned long)(((bus) << 8) | PCI_DEVFN((slot), (func)))
2427 	static const struct dmi_system_id sysids[] = {
2428 		/*
2429 		 * There are several gigabyte boards which use
2430 		 * SIMG5723s configured as hardware RAID.  Certain
2431 		 * 5723 firmware revisions shipped there keep the link
2432 		 * online but fail to answer properly to SRST or
2433 		 * IDENTIFY when no device is attached downstream
2434 		 * causing libata to retry quite a few times leading
2435 		 * to excessive detection delay.
2436 		 *
2437 		 * As these firmwares respond to the second reset try
2438 		 * with invalid device signature, considering unknown
2439 		 * sig as offline works around the problem acceptably.
2440 		 */
2441 		{
2442 			.ident = "EP45-DQ6",
2443 			.matches = {
2444 				DMI_MATCH(DMI_BOARD_VENDOR,
2445 					  "Gigabyte Technology Co., Ltd."),
2446 				DMI_MATCH(DMI_BOARD_NAME, "EP45-DQ6"),
2447 			},
2448 			.driver_data = ENCODE_BUSDEVFN(0x0a, 0x00, 0),
2449 		},
2450 		{
2451 			.ident = "EP45-DS5",
2452 			.matches = {
2453 				DMI_MATCH(DMI_BOARD_VENDOR,
2454 					  "Gigabyte Technology Co., Ltd."),
2455 				DMI_MATCH(DMI_BOARD_NAME, "EP45-DS5"),
2456 			},
2457 			.driver_data = ENCODE_BUSDEVFN(0x03, 0x00, 0),
2458 		},
2459 		{ }	/* terminate list */
2460 	};
2461 #undef ENCODE_BUSDEVFN
2462 	const struct dmi_system_id *dmi = dmi_first_match(sysids);
2463 	unsigned int val;
2464 
2465 	if (!dmi)
2466 		return false;
2467 
2468 	val = (unsigned long)dmi->driver_data;
2469 
2470 	return pdev->bus->number == (val >> 8) && pdev->devfn == (val & 0xff);
2471 }
2472 
2473 #ifdef CONFIG_ATA_ACPI
2474 static void ahci_gtf_filter_workaround(struct ata_host *host)
2475 {
2476 	static const struct dmi_system_id sysids[] = {
2477 		/*
2478 		 * Aspire 3810T issues a bunch of SATA enable commands
2479 		 * via _GTF including an invalid one and one which is
2480 		 * rejected by the device.  Among the successful ones
2481 		 * is FPDMA non-zero offset enable which when enabled
2482 		 * only on the drive side leads to NCQ command
2483 		 * failures.  Filter it out.
2484 		 */
2485 		{
2486 			.ident = "Aspire 3810T",
2487 			.matches = {
2488 				DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
2489 				DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 3810T"),
2490 			},
2491 			.driver_data = (void *)ATA_ACPI_FILTER_FPDMA_OFFSET,
2492 		},
2493 		{ }
2494 	};
2495 	const struct dmi_system_id *dmi = dmi_first_match(sysids);
2496 	unsigned int filter;
2497 	int i;
2498 
2499 	if (!dmi)
2500 		return;
2501 
2502 	filter = (unsigned long)dmi->driver_data;
2503 	dev_info(host->dev, "applying extra ACPI _GTF filter 0x%x for %s\n",
2504 		 filter, dmi->ident);
2505 
2506 	for (i = 0; i < host->n_ports; i++) {
2507 		struct ata_port *ap = host->ports[i];
2508 		struct ata_link *link;
2509 		struct ata_device *dev;
2510 
2511 		ata_for_each_link(link, ap, EDGE)
2512 			ata_for_each_dev(dev, link, ALL)
2513 				dev->gtf_filter |= filter;
2514 	}
2515 }
2516 #else
2517 static inline void ahci_gtf_filter_workaround(struct ata_host *host)
2518 {}
2519 #endif
2520 
2521 /*
2522  * On the Acer Aspire Switch Alpha 12, sometimes all SATA ports are detected
2523  * as DUMMY, or detected but eventually get a "link down" and never get up
2524  * again. When this happens, CAP.NP may hold a value of 0x00 or 0x01, and the
2525  * port_map may hold a value of 0x00.
2526  *
2527  * Overriding CAP.NP to 0x02 and the port_map to 0x7 will reveal all 3 ports
2528  * and can significantly reduce the occurrence of the problem.
2529  *
2530  * https://bugzilla.kernel.org/show_bug.cgi?id=189471
2531  */
2532 static void acer_sa5_271_workaround(struct ahci_host_priv *hpriv,
2533 				    struct pci_dev *pdev)
2534 {
2535 	static const struct dmi_system_id sysids[] = {
2536 		{
2537 			.ident = "Acer Switch Alpha 12",
2538 			.matches = {
2539 				DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
2540 				DMI_MATCH(DMI_PRODUCT_NAME, "Switch SA5-271")
2541 			},
2542 		},
2543 		{ }
2544 	};
2545 
2546 	if (dmi_check_system(sysids)) {
2547 		dev_info(&pdev->dev, "enabling Acer Switch Alpha 12 workaround\n");
2548 		if ((hpriv->saved_cap & 0xC734FF00) == 0xC734FF00) {
2549 			hpriv->port_map = 0x7;
2550 			hpriv->cap = 0xC734FF02;
2551 		}
2552 	}
2553 }
2554 
2555 #ifdef CONFIG_ARM64
2556 /*
2557  * Due to ERRATA#22536, ThunderX needs to handle HOST_IRQ_STAT differently.
2558  * Workaround is to make sure all pending IRQs are served before leaving
2559  * handler.
2560  */
2561 static irqreturn_t ahci_thunderx_irq_handler(int irq, void *dev_instance)
2562 {
2563 	struct ata_host *host = dev_instance;
2564 	struct ahci_host_priv *hpriv;
2565 	unsigned int rc = 0;
2566 	void __iomem *mmio;
2567 	u32 irq_stat, irq_masked;
2568 	unsigned int handled = 1;
2569 
2570 	hpriv = host->private_data;
2571 	mmio = hpriv->mmio;
2572 	irq_stat = readl(mmio + HOST_IRQ_STAT);
2573 	if (!irq_stat)
2574 		return IRQ_NONE;
2575 
2576 	do {
2577 		irq_masked = irq_stat & hpriv->port_map;
2578 		spin_lock(&host->lock);
2579 		rc = ahci_handle_port_intr(host, irq_masked);
2580 		if (!rc)
2581 			handled = 0;
2582 		writel(irq_stat, mmio + HOST_IRQ_STAT);
2583 		irq_stat = readl(mmio + HOST_IRQ_STAT);
2584 		spin_unlock(&host->lock);
2585 	} while (irq_stat);
2586 
2587 	return IRQ_RETVAL(handled);
2588 }
2589 #endif
2590 
2591 /*
2592  * The Marvell 88SE6111/6121/6145 ("Thor") family stops reporting interrupts
2593  * for a port when HOST_IRQ_STAT is cleared while PxIS still holds bits: PxIS
2594  * keeps its content, HOST_IRQ_STAT reads back as 0, the port is never looked
2595  * at again and the command in flight only ends in a timeout.  On a 88SE6121
2596  * this makes every SATA-2 or SATA-3 disk fail to IDENTIFY, while SATA-1 disks
2597  * happen to win the race often enough to work.
2598  *
2599  * Clearing the host status before servicing the ports avoids it.  Marvell's
2600  * own driver for these chips does the same and says so ("clear global before
2601  * channel"), and ahci_xgene handles its broken edge latch the same way.  The
2602  * price is at most one spurious interrupt per valid one, which is why this is
2603  * not the generic behaviour - see AHCI 1.1 section 10.6.2.
2604  *
2605  * Link: https://bugzilla.kernel.org/show_bug.cgi?id=216094
2606  */
2607 static irqreturn_t ahci_mv_irq_handler(int irq, void *dev_instance)
2608 {
2609 	struct ata_host *host = dev_instance;
2610 	struct ahci_host_priv *hpriv = host->private_data;
2611 	void __iomem *mmio = hpriv->mmio;
2612 	unsigned int rc;
2613 	u32 irq_stat, irq_masked;
2614 
2615 	irq_stat = readl(mmio + HOST_IRQ_STAT);
2616 	if (!irq_stat)
2617 		return IRQ_NONE;
2618 
2619 	irq_masked = irq_stat & hpriv->port_map;
2620 
2621 	spin_lock(&host->lock);
2622 
2623 	/*
2624 	 * Use the unmasked value to clear the interrupt, as a spurious pending
2625 	 * event on a dummy port might cause a screaming IRQ.
2626 	 */
2627 	writel(irq_stat, mmio + HOST_IRQ_STAT);
2628 
2629 	rc = ahci_handle_port_intr(host, irq_masked);
2630 
2631 	spin_unlock(&host->lock);
2632 
2633 	return IRQ_RETVAL(rc);
2634 }
2635 
2636 static void ahci_remap_check(struct pci_dev *pdev, int bar,
2637 		struct ahci_host_priv *hpriv)
2638 {
2639 	int i;
2640 	u32 cap;
2641 
2642 	/*
2643 	 * Check if this device might have remapped nvme devices.
2644 	 */
2645 	if (pdev->vendor != PCI_VENDOR_ID_INTEL ||
2646 	    pci_resource_len(pdev, bar) < SZ_512K ||
2647 	    bar != AHCI_PCI_BAR_STANDARD ||
2648 	    !(readl(hpriv->mmio + AHCI_VSCAP) & 1))
2649 		return;
2650 
2651 	cap = readq(hpriv->mmio + AHCI_REMAP_CAP);
2652 	for (i = 0; i < AHCI_MAX_REMAP; i++) {
2653 		if ((cap & (1 << i)) == 0)
2654 			continue;
2655 		if (readl(hpriv->mmio + ahci_remap_dcc(i))
2656 				!= PCI_CLASS_STORAGE_EXPRESS)
2657 			continue;
2658 
2659 		/* We've found a remapped device */
2660 		hpriv->remapped_nvme++;
2661 	}
2662 
2663 	if (!hpriv->remapped_nvme)
2664 		return;
2665 
2666 	dev_warn(&pdev->dev, "Found %u remapped NVMe devices.\n",
2667 		 hpriv->remapped_nvme);
2668 	dev_warn(&pdev->dev,
2669 		 "Switch your BIOS from RAID to AHCI mode to use them.\n");
2670 
2671 	/*
2672 	 * Don't rely on the msi-x capability in the remap case,
2673 	 * share the legacy interrupt across ahci and remapped devices.
2674 	 */
2675 	hpriv->flags |= AHCI_HFLAG_NO_MSI;
2676 }
2677 
2678 static int ahci_get_irq_vector(struct ata_host *host, int port)
2679 {
2680 	return pci_irq_vector(to_pci_dev(host->dev), port);
2681 }
2682 
2683 static void ahci_init_irq(struct pci_dev *pdev, unsigned int n_ports,
2684 			struct ahci_host_priv *hpriv)
2685 {
2686 	int nvec;
2687 
2688 	if (hpriv->flags & AHCI_HFLAG_NO_MSI) {
2689 		pci_alloc_irq_vectors(pdev, 1, 1, PCI_IRQ_INTX);
2690 		return;
2691 	}
2692 
2693 	/*
2694 	 * If number of MSIs is less than number of ports then Sharing Last
2695 	 * Message mode could be enforced. In this case assume that advantage
2696 	 * of multiple MSIs is negated and use single MSI mode instead.
2697 	 */
2698 	if (n_ports > 1) {
2699 		nvec = pci_alloc_irq_vectors(pdev, n_ports, INT_MAX,
2700 				PCI_IRQ_MSIX | PCI_IRQ_MSI);
2701 		if (nvec > 0) {
2702 			if (!(readl(hpriv->mmio + HOST_CTL) & HOST_MRSM)) {
2703 				hpriv->get_irq_vector = ahci_get_irq_vector;
2704 				hpriv->flags |= AHCI_HFLAG_MULTI_MSI;
2705 				return;
2706 			}
2707 
2708 			/*
2709 			 * Fallback to single MSI mode if the controller
2710 			 * enforced MRSM mode.
2711 			 */
2712 			printk(KERN_INFO
2713 				"ahci: MRSM is on, fallback to single MSI\n");
2714 			pci_free_irq_vectors(pdev);
2715 		}
2716 	}
2717 
2718 	/*
2719 	 * If the host is not capable of supporting per-port vectors, fall
2720 	 * back to single MSI before finally attempting single MSI-X or
2721 	 * a legacy INTx.
2722 	 */
2723 	nvec = pci_alloc_irq_vectors(pdev, 1, 1, PCI_IRQ_MSI);
2724 	if (nvec == 1)
2725 		return;
2726 	pci_alloc_irq_vectors(pdev, 1, 1, PCI_IRQ_MSIX | PCI_IRQ_INTX);
2727 }
2728 
2729 static void ahci_mark_external_port(struct ata_port *ap)
2730 {
2731 	struct ahci_host_priv *hpriv = ap->host->private_data;
2732 	void __iomem *port_mmio = ahci_port_base(ap);
2733 	u32 tmp;
2734 
2735 	/*
2736 	 * Mark external ports (hotplug-capable, eSATA), unless we were asked to
2737 	 * ignore this feature.
2738 	 */
2739 	tmp = readl(port_mmio + PORT_CMD);
2740 	if (((tmp & PORT_CMD_ESP) && (hpriv->cap & HOST_CAP_SXS)) ||
2741 	    (tmp & PORT_CMD_HPCP)) {
2742 		if (hpriv->mask_port_ext & (1U << ap->port_no)) {
2743 			ata_port_info(ap,
2744 				"Ignoring external/hotplug capability\n");
2745 			return;
2746 		}
2747 		ap->pflags |= ATA_PFLAG_EXTERNAL;
2748 	}
2749 }
2750 
2751 static void ahci_update_initial_lpm_policy(struct ata_port *ap)
2752 {
2753 	struct ahci_host_priv *hpriv = ap->host->private_data;
2754 	int policy = CONFIG_SATA_MOBILE_LPM_POLICY;
2755 
2756 	/*
2757 	 * AHCI contains a known incompatibility between LPM and hot-plug
2758 	 * removal events, see 7.3.1 Hot Plug Removal Detection and Power
2759 	 * Management Interaction in AHCI 1.3.1. Therefore, do not enable
2760 	 * LPM if the port advertises itself as an external port.
2761 	 */
2762 	if (ap->pflags & ATA_PFLAG_EXTERNAL) {
2763 		ap->flags |= ATA_FLAG_NO_LPM;
2764 		ap->target_lpm_policy = ATA_LPM_MAX_POWER;
2765 		return;
2766 	}
2767 
2768 	/* If no Partial or no Slumber, we cannot support DIPM. */
2769 	if ((ap->host->flags & ATA_HOST_NO_PART) ||
2770 	    (ap->host->flags & ATA_HOST_NO_SSC)) {
2771 		ata_port_dbg(ap, "Host does not support DIPM\n");
2772 		ap->flags |= ATA_FLAG_NO_DIPM;
2773 	}
2774 
2775 	/* If no LPM states are supported by the HBA, do not bother with LPM */
2776 	if ((ap->host->flags & ATA_HOST_NO_PART) &&
2777 	    (ap->host->flags & ATA_HOST_NO_SSC) &&
2778 	    (ap->host->flags & ATA_HOST_NO_DEVSLP)) {
2779 		ata_port_dbg(ap,
2780 			"No LPM states supported, forcing LPM max_power\n");
2781 		ap->flags |= ATA_FLAG_NO_LPM;
2782 		ap->target_lpm_policy = ATA_LPM_MAX_POWER;
2783 		return;
2784 	}
2785 
2786 	/* user modified policy via module param */
2787 	if (mobile_lpm_policy != -1) {
2788 		policy = mobile_lpm_policy;
2789 		goto update_policy;
2790 	}
2791 
2792 	if (policy > ATA_LPM_MED_POWER && pm_suspend_default_s2idle()) {
2793 		if (hpriv->cap & HOST_CAP_PART)
2794 			policy = ATA_LPM_MIN_POWER_WITH_PARTIAL;
2795 		else if (hpriv->cap & HOST_CAP_SSC)
2796 			policy = ATA_LPM_MIN_POWER;
2797 	}
2798 
2799 update_policy:
2800 	if (policy >= ATA_LPM_UNKNOWN && policy <= ATA_LPM_MIN_POWER)
2801 		ap->target_lpm_policy = policy;
2802 }
2803 
2804 static void ahci_intel_pcs_quirk(struct pci_dev *pdev, struct ahci_host_priv *hpriv)
2805 {
2806 	u16 tmp16;
2807 
2808 	if (!(hpriv->flags & AHCI_HFLAG_INTEL_PCS_QUIRK))
2809 		return;
2810 
2811 	/*
2812 	 * port_map is determined from PORTS_IMPL PCI register which is
2813 	 * implemented as write or write-once register.  If the register
2814 	 * isn't programmed, ahci automatically generates it from number
2815 	 * of ports, which is good enough for PCS programming. It is
2816 	 * otherwise expected that platform firmware enables the ports
2817 	 * before the OS boots.
2818 	 */
2819 	pci_read_config_word(pdev, PCS_6, &tmp16);
2820 	if ((tmp16 & hpriv->port_map) != hpriv->port_map) {
2821 		tmp16 |= hpriv->port_map;
2822 		pci_write_config_word(pdev, PCS_6, tmp16);
2823 	}
2824 }
2825 
2826 static ssize_t remapped_nvme_show(struct device *dev,
2827 				  struct device_attribute *attr,
2828 				  char *buf)
2829 {
2830 	struct ata_host *host = dev_get_drvdata(dev);
2831 	struct ahci_host_priv *hpriv = host->private_data;
2832 
2833 	return sysfs_emit(buf, "%u\n", hpriv->remapped_nvme);
2834 }
2835 
2836 static DEVICE_ATTR_RO(remapped_nvme);
2837 
2838 static int ahci_validate_bar_size(struct pci_dev *pdev, int bar,
2839 				  struct ahci_host_priv *hpriv)
2840 {
2841 	u32 cap = readl(hpriv->mmio + HOST_CAP);
2842 	unsigned int max_ports = ahci_nr_ports(cap);
2843 	u32 last_port_end = 0x100 + (max_ports * 0x80);
2844 	resource_size_t bar_size = pci_resource_len(pdev, bar);
2845 
2846 	if (last_port_end > bar_size) {
2847 		dev_warn(&pdev->dev,
2848 			 "BAR%d too small for %u ports (last port ends at %#x, BAR %pa)\n",
2849 			 bar, max_ports, last_port_end, &bar_size);
2850 		return -ENODEV;
2851 	}
2852 
2853 	return 0;
2854 }
2855 
2856 static int ahci_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
2857 {
2858 	unsigned int board_id = ent->driver_data;
2859 	struct ata_port_info pi = ahci_port_info[board_id];
2860 	const struct ata_port_info *ppi[] = { &pi, NULL };
2861 	struct device *dev = &pdev->dev;
2862 	struct ahci_host_priv *hpriv;
2863 	struct ata_host *host;
2864 	int n_ports, i, rc;
2865 	int ahci_pci_bar = AHCI_PCI_BAR_STANDARD;
2866 
2867 	WARN_ON((int)ATA_MAX_QUEUE > AHCI_MAX_CMDS);
2868 
2869 	ata_print_version_once(&pdev->dev, DRV_VERSION);
2870 
2871 	/* The AHCI driver can only drive the SATA ports, the PATA driver
2872 	   can drive them all so if both drivers are selected make sure
2873 	   AHCI stays out of the way */
2874 	if (pdev->vendor == PCI_VENDOR_ID_MARVELL && !marvell_enable)
2875 		return -ENODEV;
2876 
2877 	/* Apple BIOS on MCP89 prevents us using AHCI */
2878 	if (is_mcp89_apple(pdev))
2879 		ahci_mcp89_apple_enable(pdev);
2880 
2881 	/* Promise's PDC42819 is a SAS/SATA controller that has an AHCI mode.
2882 	 * At the moment, we can only use the AHCI mode. Let the users know
2883 	 * that for SAS drives they're out of luck.
2884 	 */
2885 	if (pdev->vendor == PCI_VENDOR_ID_PROMISE)
2886 		dev_info(&pdev->dev,
2887 			 "PDC42819 can only drive SATA devices with this driver\n");
2888 
2889 	/* Some devices use non-standard BARs */
2890 	if (pdev->vendor == PCI_VENDOR_ID_STMICRO && pdev->device == 0xCC06)
2891 		ahci_pci_bar = AHCI_PCI_BAR_STA2X11;
2892 	else if (pdev->vendor == 0x1c44 && pdev->device == 0x8000)
2893 		ahci_pci_bar = AHCI_PCI_BAR_ENMOTUS;
2894 	else if (pdev->vendor == PCI_VENDOR_ID_CAVIUM) {
2895 		if (pdev->device == 0xa01c)
2896 			ahci_pci_bar = AHCI_PCI_BAR_CAVIUM;
2897 		if (pdev->device == 0xa084)
2898 			ahci_pci_bar = AHCI_PCI_BAR_CAVIUM_GEN5;
2899 	} else if (pdev->vendor == PCI_VENDOR_ID_LOONGSON) {
2900 		if (pdev->device == 0x7a08)
2901 			ahci_pci_bar = AHCI_PCI_BAR_LOONGSON;
2902 	}
2903 
2904 	/* acquire resources */
2905 	rc = pcim_enable_device(pdev);
2906 	if (rc)
2907 		return rc;
2908 
2909 	if (pdev->vendor == PCI_VENDOR_ID_INTEL &&
2910 	    (pdev->device == 0x2652 || pdev->device == 0x2653)) {
2911 		u8 map;
2912 
2913 		/* ICH6s share the same PCI ID for both piix and ahci
2914 		 * modes.  Enabling ahci mode while MAP indicates
2915 		 * combined mode is a bad idea.  Yield to ata_piix.
2916 		 */
2917 		pci_read_config_byte(pdev, ICH_MAP, &map);
2918 		if (map & 0x3) {
2919 			dev_info(&pdev->dev,
2920 				 "controller is in combined mode, can't enable AHCI mode\n");
2921 			return -ENODEV;
2922 		}
2923 	}
2924 
2925 	/* AHCI controllers often implement SFF compatible interface.
2926 	 * Grab all PCI BARs just in case.
2927 	 */
2928 	rc = pcim_request_all_regions(pdev, DRV_NAME);
2929 	if (rc == -EBUSY)
2930 		pcim_pin_device(pdev);
2931 	if (rc)
2932 		return rc;
2933 
2934 	hpriv = devm_kzalloc(dev, sizeof(*hpriv), GFP_KERNEL);
2935 	if (!hpriv)
2936 		return -ENOMEM;
2937 	hpriv->flags |= (unsigned long)pi.private_data;
2938 
2939 	/* the Marvell "Thor" family needs HOST_IRQ_STAT cleared first */
2940 	if (board_id == board_ahci_mv)
2941 		hpriv->irq_handler = ahci_mv_irq_handler;
2942 
2943 	/* MCP65 revision A1 and A2 can't do MSI */
2944 	if (board_id == board_ahci_mcp65 &&
2945 	    (pdev->revision == 0xa1 || pdev->revision == 0xa2))
2946 		hpriv->flags |= AHCI_HFLAG_NO_MSI;
2947 
2948 	/* SB800 does NOT need the workaround to ignore SERR_INTERNAL */
2949 	if (board_id == board_ahci_sb700 && pdev->revision >= 0x40)
2950 		hpriv->flags &= ~AHCI_HFLAG_IGN_SERR_INTERNAL;
2951 
2952 	/* only some SB600s can do 64bit DMA */
2953 	if (ahci_sb600_enable_64bit(pdev))
2954 		hpriv->flags &= ~AHCI_HFLAG_32BIT_ONLY;
2955 
2956 	hpriv->mmio = pcim_iomap(pdev, ahci_pci_bar, 0);
2957 	if (!hpriv->mmio)
2958 		return -ENOMEM;
2959 
2960 	rc = ahci_validate_bar_size(pdev, ahci_pci_bar, hpriv);
2961 	if (rc)
2962 		return rc;
2963 
2964 	/* detect remapped nvme devices */
2965 	ahci_remap_check(pdev, ahci_pci_bar, hpriv);
2966 
2967 	sysfs_add_file_to_group(&pdev->dev.kobj,
2968 				&dev_attr_remapped_nvme.attr,
2969 				NULL);
2970 
2971 #ifdef CONFIG_ARM64
2972 	if (pdev->vendor == PCI_VENDOR_ID_HUAWEI &&
2973 	    pdev->device == 0xa235 &&
2974 	    pdev->revision < 0x30)
2975 		hpriv->flags |= AHCI_HFLAG_NO_SXS;
2976 
2977 	if (pdev->vendor == 0x177d && pdev->device == 0xa01c)
2978 		hpriv->irq_handler = ahci_thunderx_irq_handler;
2979 #endif
2980 
2981 	/* save initial config */
2982 	ahci_pci_save_initial_config(pdev, hpriv);
2983 
2984 	/* prepare host */
2985 	if (hpriv->cap & HOST_CAP_NCQ) {
2986 		pi.flags |= ATA_FLAG_NCQ;
2987 		/*
2988 		 * Auto-activate optimization is supposed to be
2989 		 * supported on all AHCI controllers indicating NCQ
2990 		 * capability, but it seems to be broken on some
2991 		 * chipsets including NVIDIAs.
2992 		 */
2993 		if (!(hpriv->flags & AHCI_HFLAG_NO_FPDMA_AA))
2994 			pi.flags |= ATA_FLAG_FPDMA_AA;
2995 
2996 		/*
2997 		 * All AHCI controllers should be forward-compatible
2998 		 * with the new auxiliary field. This code should be
2999 		 * conditionalized if any buggy AHCI controllers are
3000 		 * encountered.
3001 		 */
3002 		pi.flags |= ATA_FLAG_FPDMA_AUX;
3003 	}
3004 
3005 	if (hpriv->cap & HOST_CAP_PMP)
3006 		pi.flags |= ATA_FLAG_PMP;
3007 
3008 	ahci_set_em_messages(hpriv, &pi);
3009 
3010 	if (ahci_broken_system_poweroff(pdev)) {
3011 		pi.flags |= ATA_FLAG_NO_POWEROFF_SPINDOWN;
3012 		dev_info(&pdev->dev,
3013 			"quirky BIOS, skipping spindown on poweroff\n");
3014 	}
3015 
3016 	if (ahci_broken_lpm(pdev)) {
3017 		pi.flags |= ATA_FLAG_NO_LPM;
3018 		dev_warn(&pdev->dev,
3019 			 "BIOS update required for Link Power Management support\n");
3020 	}
3021 
3022 	if (ahci_broken_suspend(pdev)) {
3023 		hpriv->flags |= AHCI_HFLAG_NO_SUSPEND;
3024 		dev_warn(&pdev->dev,
3025 			 "BIOS update required for suspend/resume\n");
3026 	}
3027 
3028 	if (ahci_broken_online(pdev)) {
3029 		hpriv->flags |= AHCI_HFLAG_SRST_TOUT_IS_OFFLINE;
3030 		dev_info(&pdev->dev,
3031 			 "online status unreliable, applying workaround\n");
3032 	}
3033 
3034 
3035 	/* Acer SA5-271 workaround modifies private_data */
3036 	acer_sa5_271_workaround(hpriv, pdev);
3037 
3038 	/* CAP.NP sometimes indicate the index of the last enabled
3039 	 * port, at other times, that of the last possible port, so
3040 	 * determining the maximum port number requires looking at
3041 	 * both CAP.NP and port_map.
3042 	 */
3043 	n_ports = max(ahci_nr_ports(hpriv->cap), fls(hpriv->port_map));
3044 
3045 	host = ata_host_alloc_pinfo(&pdev->dev, ppi, n_ports);
3046 	if (!host) {
3047 		rc = -ENOMEM;
3048 		goto err_rm_sysfs_file;
3049 	}
3050 	host->private_data = hpriv;
3051 
3052 	ahci_init_irq(pdev, n_ports, hpriv);
3053 
3054 	hpriv->irq = pci_irq_vector(pdev, 0);
3055 
3056 	if (!(hpriv->cap & HOST_CAP_SSS) || ahci_ignore_sss)
3057 		host->flags |= ATA_HOST_PARALLEL_SCAN;
3058 	else
3059 		dev_info(&pdev->dev, "SSS flag set, parallel bus scan disabled\n");
3060 
3061 	if (!(hpriv->cap & HOST_CAP_PART))
3062 		host->flags |= ATA_HOST_NO_PART;
3063 
3064 	if (!(hpriv->cap & HOST_CAP_SSC))
3065 		host->flags |= ATA_HOST_NO_SSC;
3066 
3067 	if (!(hpriv->cap2 & HOST_CAP2_SDS))
3068 		host->flags |= ATA_HOST_NO_DEVSLP;
3069 
3070 	if (pi.flags & ATA_FLAG_EM)
3071 		ahci_reset_em(host);
3072 
3073 	for (i = 0; i < host->n_ports; i++) {
3074 		struct ata_port *ap = host->ports[i];
3075 
3076 		ata_port_pbar_desc(ap, ahci_pci_bar, -1, "abar");
3077 		ata_port_pbar_desc(ap, ahci_pci_bar,
3078 				   0x100 + ap->port_no * 0x80, "port");
3079 
3080 		/* set enclosure management message type */
3081 		if (ap->flags & ATA_FLAG_EM)
3082 			ap->em_message_type = hpriv->em_msg_type;
3083 
3084 		/* disabled/not-implemented port */
3085 		if (!(hpriv->port_map & (1 << i))) {
3086 			ap->ops = &ata_dummy_port_ops;
3087 		} else {
3088 			ahci_mark_external_port(ap);
3089 			ahci_update_initial_lpm_policy(ap);
3090 		}
3091 	}
3092 
3093 	/* apply workaround for ASUS P5W DH Deluxe mainboard */
3094 	ahci_p5wdh_workaround(host);
3095 
3096 	/* apply gtf filter quirk */
3097 	ahci_gtf_filter_workaround(host);
3098 
3099 	/* initialize adapter */
3100 	rc = ahci_configure_dma_masks(pdev, hpriv);
3101 	if (rc)
3102 		goto err_rm_sysfs_file;
3103 
3104 	rc = ahci_pci_reset_controller(host);
3105 	if (rc)
3106 		goto err_rm_sysfs_file;
3107 
3108 	ahci_pci_init_controller(host);
3109 	ahci_pci_print_info(host);
3110 
3111 	pci_set_master(pdev);
3112 
3113 	rc = ahci_host_activate(host, &ahci_sht);
3114 	if (rc)
3115 		goto err_rm_sysfs_file;
3116 
3117 	pm_runtime_put_noidle(&pdev->dev);
3118 	return 0;
3119 
3120 err_rm_sysfs_file:
3121 	sysfs_remove_file_from_group(&pdev->dev.kobj,
3122 				     &dev_attr_remapped_nvme.attr, NULL);
3123 	return rc;
3124 }
3125 
3126 static void ahci_shutdown_one(struct pci_dev *pdev)
3127 {
3128 	ata_pci_shutdown_one(pdev);
3129 }
3130 
3131 static void ahci_remove_one(struct pci_dev *pdev)
3132 {
3133 	sysfs_remove_file_from_group(&pdev->dev.kobj,
3134 				     &dev_attr_remapped_nvme.attr,
3135 				     NULL);
3136 	pm_runtime_get_noresume(&pdev->dev);
3137 	ata_pci_remove_one(pdev);
3138 }
3139 
3140 module_pci_driver(ahci_pci_driver);
3141 
3142 MODULE_AUTHOR("Jeff Garzik");
3143 MODULE_DESCRIPTION("AHCI SATA low-level driver");
3144 MODULE_LICENSE("GPL");
3145 MODULE_DEVICE_TABLE(pci, ahci_pci_tbl);
3146 MODULE_VERSION(DRV_VERSION);
3147