xref: /linux/drivers/cxl/pci.c (revision 87807f77a03d0271211b75f84b2a8b88f4e8e5d4)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright(c) 2020 Intel Corporation. All rights reserved. */
3 #include <linux/io-64-nonatomic-lo-hi.h>
4 #include <linux/moduleparam.h>
5 #include <linux/module.h>
6 #include <linux/delay.h>
7 #include <linux/sizes.h>
8 #include <linux/mutex.h>
9 #include <linux/list.h>
10 #include <linux/pci.h>
11 #include <linux/aer.h>
12 #include <linux/io.h>
13 #include "cxlmem.h"
14 #include "cxlpci.h"
15 #include "cxl.h"
16 
17 /**
18  * DOC: cxl pci
19  *
20  * This implements the PCI exclusive functionality for a CXL device as it is
21  * defined by the Compute Express Link specification. CXL devices may surface
22  * certain functionality even if it isn't CXL enabled. While this driver is
23  * focused around the PCI specific aspects of a CXL device, it binds to the
24  * specific CXL memory device class code, and therefore the implementation of
25  * cxl_pci is focused around CXL memory devices.
26  *
27  * The driver has several responsibilities, mainly:
28  *  - Create the memX device and register on the CXL bus.
29  *  - Enumerate device's register interface and map them.
30  *  - Registers nvdimm bridge device with cxl_core.
31  *  - Registers a CXL mailbox with cxl_core.
32  */
33 
34 #define cxl_doorbell_busy(cxlds)                                                \
35 	(readl((cxlds)->regs.mbox + CXLDEV_MBOX_CTRL_OFFSET) &                  \
36 	 CXLDEV_MBOX_CTRL_DOORBELL)
37 
38 /* CXL 2.0 - 8.2.8.4 */
39 #define CXL_MAILBOX_TIMEOUT_MS (2 * HZ)
40 
41 /*
42  * CXL 2.0 ECN "Add Mailbox Ready Time" defines a capability field to
43  * dictate how long to wait for the mailbox to become ready. The new
44  * field allows the device to tell software the amount of time to wait
45  * before mailbox ready. This field per the spec theoretically allows
46  * for up to 255 seconds. 255 seconds is unreasonably long, its longer
47  * than the maximum SATA port link recovery wait. Default to 60 seconds
48  * until someone builds a CXL device that needs more time in practice.
49  */
50 static unsigned short mbox_ready_timeout = 60;
51 module_param(mbox_ready_timeout, ushort, 0644);
52 MODULE_PARM_DESC(mbox_ready_timeout, "seconds to wait for mailbox ready");
53 
54 static int cxl_pci_mbox_wait_for_doorbell(struct cxl_dev_state *cxlds)
55 {
56 	const unsigned long start = jiffies;
57 	unsigned long end = start;
58 
59 	while (cxl_doorbell_busy(cxlds)) {
60 		end = jiffies;
61 
62 		if (time_after(end, start + CXL_MAILBOX_TIMEOUT_MS)) {
63 			/* Check again in case preempted before timeout test */
64 			if (!cxl_doorbell_busy(cxlds))
65 				break;
66 			return -ETIMEDOUT;
67 		}
68 		cpu_relax();
69 	}
70 
71 	dev_dbg(cxlds->dev, "Doorbell wait took %dms",
72 		jiffies_to_msecs(end) - jiffies_to_msecs(start));
73 	return 0;
74 }
75 
76 #define cxl_err(dev, status, msg)                                        \
77 	dev_err_ratelimited(dev, msg ", device state %s%s\n",                  \
78 			    status & CXLMDEV_DEV_FATAL ? " fatal" : "",        \
79 			    status & CXLMDEV_FW_HALT ? " firmware-halt" : "")
80 
81 #define cxl_cmd_err(dev, cmd, status, msg)                               \
82 	dev_err_ratelimited(dev, msg " (opcode: %#x), device state %s%s\n",    \
83 			    (cmd)->opcode,                                     \
84 			    status & CXLMDEV_DEV_FATAL ? " fatal" : "",        \
85 			    status & CXLMDEV_FW_HALT ? " firmware-halt" : "")
86 
87 /**
88  * __cxl_pci_mbox_send_cmd() - Execute a mailbox command
89  * @cxlds: The device state to communicate with.
90  * @mbox_cmd: Command to send to the memory device.
91  *
92  * Context: Any context. Expects mbox_mutex to be held.
93  * Return: -ETIMEDOUT if timeout occurred waiting for completion. 0 on success.
94  *         Caller should check the return code in @mbox_cmd to make sure it
95  *         succeeded.
96  *
97  * This is a generic form of the CXL mailbox send command thus only using the
98  * registers defined by the mailbox capability ID - CXL 2.0 8.2.8.4. Memory
99  * devices, and perhaps other types of CXL devices may have further information
100  * available upon error conditions. Driver facilities wishing to send mailbox
101  * commands should use the wrapper command.
102  *
103  * The CXL spec allows for up to two mailboxes. The intention is for the primary
104  * mailbox to be OS controlled and the secondary mailbox to be used by system
105  * firmware. This allows the OS and firmware to communicate with the device and
106  * not need to coordinate with each other. The driver only uses the primary
107  * mailbox.
108  */
109 static int __cxl_pci_mbox_send_cmd(struct cxl_dev_state *cxlds,
110 				   struct cxl_mbox_cmd *mbox_cmd)
111 {
112 	void __iomem *payload = cxlds->regs.mbox + CXLDEV_MBOX_PAYLOAD_OFFSET;
113 	struct device *dev = cxlds->dev;
114 	u64 cmd_reg, status_reg;
115 	size_t out_len;
116 	int rc;
117 
118 	lockdep_assert_held(&cxlds->mbox_mutex);
119 
120 	/*
121 	 * Here are the steps from 8.2.8.4 of the CXL 2.0 spec.
122 	 *   1. Caller reads MB Control Register to verify doorbell is clear
123 	 *   2. Caller writes Command Register
124 	 *   3. Caller writes Command Payload Registers if input payload is non-empty
125 	 *   4. Caller writes MB Control Register to set doorbell
126 	 *   5. Caller either polls for doorbell to be clear or waits for interrupt if configured
127 	 *   6. Caller reads MB Status Register to fetch Return code
128 	 *   7. If command successful, Caller reads Command Register to get Payload Length
129 	 *   8. If output payload is non-empty, host reads Command Payload Registers
130 	 *
131 	 * Hardware is free to do whatever it wants before the doorbell is rung,
132 	 * and isn't allowed to change anything after it clears the doorbell. As
133 	 * such, steps 2 and 3 can happen in any order, and steps 6, 7, 8 can
134 	 * also happen in any order (though some orders might not make sense).
135 	 */
136 
137 	/* #1 */
138 	if (cxl_doorbell_busy(cxlds)) {
139 		u64 md_status =
140 			readq(cxlds->regs.memdev + CXLMDEV_STATUS_OFFSET);
141 
142 		cxl_cmd_err(cxlds->dev, mbox_cmd, md_status,
143 			    "mailbox queue busy");
144 		return -EBUSY;
145 	}
146 
147 	cmd_reg = FIELD_PREP(CXLDEV_MBOX_CMD_COMMAND_OPCODE_MASK,
148 			     mbox_cmd->opcode);
149 	if (mbox_cmd->size_in) {
150 		if (WARN_ON(!mbox_cmd->payload_in))
151 			return -EINVAL;
152 
153 		cmd_reg |= FIELD_PREP(CXLDEV_MBOX_CMD_PAYLOAD_LENGTH_MASK,
154 				      mbox_cmd->size_in);
155 		memcpy_toio(payload, mbox_cmd->payload_in, mbox_cmd->size_in);
156 	}
157 
158 	/* #2, #3 */
159 	writeq(cmd_reg, cxlds->regs.mbox + CXLDEV_MBOX_CMD_OFFSET);
160 
161 	/* #4 */
162 	dev_dbg(dev, "Sending command: 0x%04x\n", mbox_cmd->opcode);
163 	writel(CXLDEV_MBOX_CTRL_DOORBELL,
164 	       cxlds->regs.mbox + CXLDEV_MBOX_CTRL_OFFSET);
165 
166 	/* #5 */
167 	rc = cxl_pci_mbox_wait_for_doorbell(cxlds);
168 	if (rc == -ETIMEDOUT) {
169 		u64 md_status = readq(cxlds->regs.memdev + CXLMDEV_STATUS_OFFSET);
170 
171 		cxl_cmd_err(cxlds->dev, mbox_cmd, md_status, "mailbox timeout");
172 		return rc;
173 	}
174 
175 	/* #6 */
176 	status_reg = readq(cxlds->regs.mbox + CXLDEV_MBOX_STATUS_OFFSET);
177 	mbox_cmd->return_code =
178 		FIELD_GET(CXLDEV_MBOX_STATUS_RET_CODE_MASK, status_reg);
179 
180 	if (mbox_cmd->return_code != CXL_MBOX_CMD_RC_SUCCESS) {
181 		dev_dbg(dev, "Mailbox operation had an error: %s\n",
182 			cxl_mbox_cmd_rc2str(mbox_cmd));
183 		return 0; /* completed but caller must check return_code */
184 	}
185 
186 	/* #7 */
187 	cmd_reg = readq(cxlds->regs.mbox + CXLDEV_MBOX_CMD_OFFSET);
188 	out_len = FIELD_GET(CXLDEV_MBOX_CMD_PAYLOAD_LENGTH_MASK, cmd_reg);
189 
190 	/* #8 */
191 	if (out_len && mbox_cmd->payload_out) {
192 		/*
193 		 * Sanitize the copy. If hardware misbehaves, out_len per the
194 		 * spec can actually be greater than the max allowed size (21
195 		 * bits available but spec defined 1M max). The caller also may
196 		 * have requested less data than the hardware supplied even
197 		 * within spec.
198 		 */
199 		size_t n = min3(mbox_cmd->size_out, cxlds->payload_size, out_len);
200 
201 		memcpy_fromio(mbox_cmd->payload_out, payload, n);
202 		mbox_cmd->size_out = n;
203 	} else {
204 		mbox_cmd->size_out = 0;
205 	}
206 
207 	return 0;
208 }
209 
210 static int cxl_pci_mbox_send(struct cxl_dev_state *cxlds, struct cxl_mbox_cmd *cmd)
211 {
212 	int rc;
213 
214 	mutex_lock_io(&cxlds->mbox_mutex);
215 	rc = __cxl_pci_mbox_send_cmd(cxlds, cmd);
216 	mutex_unlock(&cxlds->mbox_mutex);
217 
218 	return rc;
219 }
220 
221 static int cxl_pci_setup_mailbox(struct cxl_dev_state *cxlds)
222 {
223 	const int cap = readl(cxlds->regs.mbox + CXLDEV_MBOX_CAPS_OFFSET);
224 	unsigned long timeout;
225 	u64 md_status;
226 
227 	timeout = jiffies + mbox_ready_timeout * HZ;
228 	do {
229 		md_status = readq(cxlds->regs.memdev + CXLMDEV_STATUS_OFFSET);
230 		if (md_status & CXLMDEV_MBOX_IF_READY)
231 			break;
232 		if (msleep_interruptible(100))
233 			break;
234 	} while (!time_after(jiffies, timeout));
235 
236 	if (!(md_status & CXLMDEV_MBOX_IF_READY)) {
237 		cxl_err(cxlds->dev, md_status,
238 			"timeout awaiting mailbox ready");
239 		return -ETIMEDOUT;
240 	}
241 
242 	/*
243 	 * A command may be in flight from a previous driver instance,
244 	 * think kexec, do one doorbell wait so that
245 	 * __cxl_pci_mbox_send_cmd() can assume that it is the only
246 	 * source for future doorbell busy events.
247 	 */
248 	if (cxl_pci_mbox_wait_for_doorbell(cxlds) != 0) {
249 		cxl_err(cxlds->dev, md_status, "timeout awaiting mailbox idle");
250 		return -ETIMEDOUT;
251 	}
252 
253 	cxlds->mbox_send = cxl_pci_mbox_send;
254 	cxlds->payload_size =
255 		1 << FIELD_GET(CXLDEV_MBOX_CAP_PAYLOAD_SIZE_MASK, cap);
256 
257 	/*
258 	 * CXL 2.0 8.2.8.4.3 Mailbox Capabilities Register
259 	 *
260 	 * If the size is too small, mandatory commands will not work and so
261 	 * there's no point in going forward. If the size is too large, there's
262 	 * no harm is soft limiting it.
263 	 */
264 	cxlds->payload_size = min_t(size_t, cxlds->payload_size, SZ_1M);
265 	if (cxlds->payload_size < 256) {
266 		dev_err(cxlds->dev, "Mailbox is too small (%zub)",
267 			cxlds->payload_size);
268 		return -ENXIO;
269 	}
270 
271 	dev_dbg(cxlds->dev, "Mailbox payload sized %zu",
272 		cxlds->payload_size);
273 
274 	return 0;
275 }
276 
277 static int cxl_map_regblock(struct pci_dev *pdev, struct cxl_register_map *map)
278 {
279 	struct device *dev = &pdev->dev;
280 
281 	map->base = ioremap(map->resource, map->max_size);
282 	if (!map->base) {
283 		dev_err(dev, "failed to map registers\n");
284 		return -ENOMEM;
285 	}
286 
287 	dev_dbg(dev, "Mapped CXL Memory Device resource %pa\n", &map->resource);
288 	return 0;
289 }
290 
291 static void cxl_unmap_regblock(struct pci_dev *pdev,
292 			       struct cxl_register_map *map)
293 {
294 	iounmap(map->base);
295 	map->base = NULL;
296 }
297 
298 static int cxl_probe_regs(struct pci_dev *pdev, struct cxl_register_map *map)
299 {
300 	struct cxl_component_reg_map *comp_map;
301 	struct cxl_device_reg_map *dev_map;
302 	struct device *dev = &pdev->dev;
303 	void __iomem *base = map->base;
304 
305 	switch (map->reg_type) {
306 	case CXL_REGLOC_RBI_COMPONENT:
307 		comp_map = &map->component_map;
308 		cxl_probe_component_regs(dev, base, comp_map);
309 		if (!comp_map->hdm_decoder.valid) {
310 			dev_err(dev, "HDM decoder registers not found\n");
311 			return -ENXIO;
312 		}
313 
314 		if (!comp_map->ras.valid)
315 			dev_dbg(dev, "RAS registers not found\n");
316 
317 		dev_dbg(dev, "Set up component registers\n");
318 		break;
319 	case CXL_REGLOC_RBI_MEMDEV:
320 		dev_map = &map->device_map;
321 		cxl_probe_device_regs(dev, base, dev_map);
322 		if (!dev_map->status.valid || !dev_map->mbox.valid ||
323 		    !dev_map->memdev.valid) {
324 			dev_err(dev, "registers not found: %s%s%s\n",
325 				!dev_map->status.valid ? "status " : "",
326 				!dev_map->mbox.valid ? "mbox " : "",
327 				!dev_map->memdev.valid ? "memdev " : "");
328 			return -ENXIO;
329 		}
330 
331 		dev_dbg(dev, "Probing device registers...\n");
332 		break;
333 	default:
334 		break;
335 	}
336 
337 	return 0;
338 }
339 
340 static int cxl_setup_regs(struct pci_dev *pdev, enum cxl_regloc_type type,
341 			  struct cxl_register_map *map)
342 {
343 	int rc;
344 
345 	rc = cxl_find_regblock(pdev, type, map);
346 	if (rc)
347 		return rc;
348 
349 	rc = cxl_map_regblock(pdev, map);
350 	if (rc)
351 		return rc;
352 
353 	rc = cxl_probe_regs(pdev, map);
354 	cxl_unmap_regblock(pdev, map);
355 
356 	return rc;
357 }
358 
359 /*
360  * Assume that any RCIEP that emits the CXL memory expander class code
361  * is an RCD
362  */
363 static bool is_cxl_restricted(struct pci_dev *pdev)
364 {
365 	return pci_pcie_type(pdev) == PCI_EXP_TYPE_RC_END;
366 }
367 
368 /*
369  * CXL v3.0 6.2.3 Table 6-4
370  * The table indicates that if PCIe Flit Mode is set, then CXL is in 256B flits
371  * mode, otherwise it's 68B flits mode.
372  */
373 static bool cxl_pci_flit_256(struct pci_dev *pdev)
374 {
375 	u16 lnksta2;
376 
377 	pcie_capability_read_word(pdev, PCI_EXP_LNKSTA2, &lnksta2);
378 	return lnksta2 & PCI_EXP_LNKSTA2_FLIT;
379 }
380 
381 static int cxl_pci_ras_unmask(struct pci_dev *pdev)
382 {
383 	struct pci_host_bridge *host_bridge = pci_find_host_bridge(pdev->bus);
384 	struct cxl_dev_state *cxlds = pci_get_drvdata(pdev);
385 	void __iomem *addr;
386 	u32 orig_val, val, mask;
387 	u16 cap;
388 	int rc;
389 
390 	if (!cxlds->regs.ras) {
391 		dev_dbg(&pdev->dev, "No RAS registers.\n");
392 		return 0;
393 	}
394 
395 	/* BIOS has CXL error control */
396 	if (!host_bridge->native_cxl_error)
397 		return -ENXIO;
398 
399 	rc = pcie_capability_read_word(pdev, PCI_EXP_DEVCTL, &cap);
400 	if (rc)
401 		return rc;
402 
403 	if (cap & PCI_EXP_DEVCTL_URRE) {
404 		addr = cxlds->regs.ras + CXL_RAS_UNCORRECTABLE_MASK_OFFSET;
405 		orig_val = readl(addr);
406 
407 		mask = CXL_RAS_UNCORRECTABLE_MASK_MASK;
408 		if (!cxl_pci_flit_256(pdev))
409 			mask &= ~CXL_RAS_UNCORRECTABLE_MASK_F256B_MASK;
410 		val = orig_val & ~mask;
411 		writel(val, addr);
412 		dev_dbg(&pdev->dev,
413 			"Uncorrectable RAS Errors Mask: %#x -> %#x\n",
414 			orig_val, val);
415 	}
416 
417 	if (cap & PCI_EXP_DEVCTL_CERE) {
418 		addr = cxlds->regs.ras + CXL_RAS_CORRECTABLE_MASK_OFFSET;
419 		orig_val = readl(addr);
420 		val = orig_val & ~CXL_RAS_CORRECTABLE_MASK_MASK;
421 		writel(val, addr);
422 		dev_dbg(&pdev->dev, "Correctable RAS Errors Mask: %#x -> %#x\n",
423 			orig_val, val);
424 	}
425 
426 	return 0;
427 }
428 
429 static void free_event_buf(void *buf)
430 {
431 	kvfree(buf);
432 }
433 
434 /*
435  * There is a single buffer for reading event logs from the mailbox.  All logs
436  * share this buffer protected by the cxlds->event_log_lock.
437  */
438 static int cxl_mem_alloc_event_buf(struct cxl_dev_state *cxlds)
439 {
440 	struct cxl_get_event_payload *buf;
441 
442 	buf = kvmalloc(cxlds->payload_size, GFP_KERNEL);
443 	if (!buf)
444 		return -ENOMEM;
445 	cxlds->event.buf = buf;
446 
447 	return devm_add_action_or_reset(cxlds->dev, free_event_buf, buf);
448 }
449 
450 static int cxl_alloc_irq_vectors(struct pci_dev *pdev)
451 {
452 	int nvecs;
453 
454 	/*
455 	 * Per CXL 3.0 3.1.1 CXL.io Endpoint a function on a CXL device must
456 	 * not generate INTx messages if that function participates in
457 	 * CXL.cache or CXL.mem.
458 	 *
459 	 * Additionally pci_alloc_irq_vectors() handles calling
460 	 * pci_free_irq_vectors() automatically despite not being called
461 	 * pcim_*.  See pci_setup_msi_context().
462 	 */
463 	nvecs = pci_alloc_irq_vectors(pdev, 1, CXL_PCI_DEFAULT_MAX_VECTORS,
464 				      PCI_IRQ_MSIX | PCI_IRQ_MSI);
465 	if (nvecs < 1) {
466 		dev_dbg(&pdev->dev, "Failed to alloc irq vectors: %d\n", nvecs);
467 		return -ENXIO;
468 	}
469 	return 0;
470 }
471 
472 struct cxl_dev_id {
473 	struct cxl_dev_state *cxlds;
474 };
475 
476 static irqreturn_t cxl_event_thread(int irq, void *id)
477 {
478 	struct cxl_dev_id *dev_id = id;
479 	struct cxl_dev_state *cxlds = dev_id->cxlds;
480 	u32 status;
481 
482 	do {
483 		/*
484 		 * CXL 3.0 8.2.8.3.1: The lower 32 bits are the status;
485 		 * ignore the reserved upper 32 bits
486 		 */
487 		status = readl(cxlds->regs.status + CXLDEV_DEV_EVENT_STATUS_OFFSET);
488 		/* Ignore logs unknown to the driver */
489 		status &= CXLDEV_EVENT_STATUS_ALL;
490 		if (!status)
491 			break;
492 		cxl_mem_get_event_records(cxlds, status);
493 		cond_resched();
494 	} while (status);
495 
496 	return IRQ_HANDLED;
497 }
498 
499 static int cxl_event_req_irq(struct cxl_dev_state *cxlds, u8 setting)
500 {
501 	struct device *dev = cxlds->dev;
502 	struct pci_dev *pdev = to_pci_dev(dev);
503 	struct cxl_dev_id *dev_id;
504 	int irq;
505 
506 	if (FIELD_GET(CXLDEV_EVENT_INT_MODE_MASK, setting) != CXL_INT_MSI_MSIX)
507 		return -ENXIO;
508 
509 	/* dev_id must be globally unique and must contain the cxlds */
510 	dev_id = devm_kzalloc(dev, sizeof(*dev_id), GFP_KERNEL);
511 	if (!dev_id)
512 		return -ENOMEM;
513 	dev_id->cxlds = cxlds;
514 
515 	irq =  pci_irq_vector(pdev,
516 			      FIELD_GET(CXLDEV_EVENT_INT_MSGNUM_MASK, setting));
517 	if (irq < 0)
518 		return irq;
519 
520 	return devm_request_threaded_irq(dev, irq, NULL, cxl_event_thread,
521 					 IRQF_SHARED | IRQF_ONESHOT, NULL,
522 					 dev_id);
523 }
524 
525 static int cxl_event_get_int_policy(struct cxl_dev_state *cxlds,
526 				    struct cxl_event_interrupt_policy *policy)
527 {
528 	struct cxl_mbox_cmd mbox_cmd = {
529 		.opcode = CXL_MBOX_OP_GET_EVT_INT_POLICY,
530 		.payload_out = policy,
531 		.size_out = sizeof(*policy),
532 	};
533 	int rc;
534 
535 	rc = cxl_internal_send_cmd(cxlds, &mbox_cmd);
536 	if (rc < 0)
537 		dev_err(cxlds->dev, "Failed to get event interrupt policy : %d",
538 			rc);
539 
540 	return rc;
541 }
542 
543 static int cxl_event_config_msgnums(struct cxl_dev_state *cxlds,
544 				    struct cxl_event_interrupt_policy *policy)
545 {
546 	struct cxl_mbox_cmd mbox_cmd;
547 	int rc;
548 
549 	*policy = (struct cxl_event_interrupt_policy) {
550 		.info_settings = CXL_INT_MSI_MSIX,
551 		.warn_settings = CXL_INT_MSI_MSIX,
552 		.failure_settings = CXL_INT_MSI_MSIX,
553 		.fatal_settings = CXL_INT_MSI_MSIX,
554 	};
555 
556 	mbox_cmd = (struct cxl_mbox_cmd) {
557 		.opcode = CXL_MBOX_OP_SET_EVT_INT_POLICY,
558 		.payload_in = policy,
559 		.size_in = sizeof(*policy),
560 	};
561 
562 	rc = cxl_internal_send_cmd(cxlds, &mbox_cmd);
563 	if (rc < 0) {
564 		dev_err(cxlds->dev, "Failed to set event interrupt policy : %d",
565 			rc);
566 		return rc;
567 	}
568 
569 	/* Retrieve final interrupt settings */
570 	return cxl_event_get_int_policy(cxlds, policy);
571 }
572 
573 static int cxl_event_irqsetup(struct cxl_dev_state *cxlds)
574 {
575 	struct cxl_event_interrupt_policy policy;
576 	int rc;
577 
578 	rc = cxl_event_config_msgnums(cxlds, &policy);
579 	if (rc)
580 		return rc;
581 
582 	rc = cxl_event_req_irq(cxlds, policy.info_settings);
583 	if (rc) {
584 		dev_err(cxlds->dev, "Failed to get interrupt for event Info log\n");
585 		return rc;
586 	}
587 
588 	rc = cxl_event_req_irq(cxlds, policy.warn_settings);
589 	if (rc) {
590 		dev_err(cxlds->dev, "Failed to get interrupt for event Warn log\n");
591 		return rc;
592 	}
593 
594 	rc = cxl_event_req_irq(cxlds, policy.failure_settings);
595 	if (rc) {
596 		dev_err(cxlds->dev, "Failed to get interrupt for event Failure log\n");
597 		return rc;
598 	}
599 
600 	rc = cxl_event_req_irq(cxlds, policy.fatal_settings);
601 	if (rc) {
602 		dev_err(cxlds->dev, "Failed to get interrupt for event Fatal log\n");
603 		return rc;
604 	}
605 
606 	return 0;
607 }
608 
609 static bool cxl_event_int_is_fw(u8 setting)
610 {
611 	u8 mode = FIELD_GET(CXLDEV_EVENT_INT_MODE_MASK, setting);
612 
613 	return mode == CXL_INT_FW;
614 }
615 
616 static int cxl_event_config(struct pci_host_bridge *host_bridge,
617 			    struct cxl_dev_state *cxlds)
618 {
619 	struct cxl_event_interrupt_policy policy;
620 	int rc;
621 
622 	/*
623 	 * When BIOS maintains CXL error reporting control, it will process
624 	 * event records.  Only one agent can do so.
625 	 */
626 	if (!host_bridge->native_cxl_error)
627 		return 0;
628 
629 	rc = cxl_mem_alloc_event_buf(cxlds);
630 	if (rc)
631 		return rc;
632 
633 	rc = cxl_event_get_int_policy(cxlds, &policy);
634 	if (rc)
635 		return rc;
636 
637 	if (cxl_event_int_is_fw(policy.info_settings) ||
638 	    cxl_event_int_is_fw(policy.warn_settings) ||
639 	    cxl_event_int_is_fw(policy.failure_settings) ||
640 	    cxl_event_int_is_fw(policy.fatal_settings)) {
641 		dev_err(cxlds->dev, "FW still in control of Event Logs despite _OSC settings\n");
642 		return -EBUSY;
643 	}
644 
645 	rc = cxl_event_irqsetup(cxlds);
646 	if (rc)
647 		return rc;
648 
649 	cxl_mem_get_event_records(cxlds, CXLDEV_EVENT_STATUS_ALL);
650 
651 	return 0;
652 }
653 
654 static int cxl_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
655 {
656 	struct pci_host_bridge *host_bridge = pci_find_host_bridge(pdev->bus);
657 	struct cxl_register_map map;
658 	struct cxl_memdev *cxlmd;
659 	struct cxl_dev_state *cxlds;
660 	int rc;
661 
662 	/*
663 	 * Double check the anonymous union trickery in struct cxl_regs
664 	 * FIXME switch to struct_group()
665 	 */
666 	BUILD_BUG_ON(offsetof(struct cxl_regs, memdev) !=
667 		     offsetof(struct cxl_regs, device_regs.memdev));
668 
669 	rc = pcim_enable_device(pdev);
670 	if (rc)
671 		return rc;
672 	pci_set_master(pdev);
673 
674 	cxlds = cxl_dev_state_create(&pdev->dev);
675 	if (IS_ERR(cxlds))
676 		return PTR_ERR(cxlds);
677 	pci_set_drvdata(pdev, cxlds);
678 
679 	cxlds->rcd = is_cxl_restricted(pdev);
680 	cxlds->serial = pci_get_dsn(pdev);
681 	cxlds->cxl_dvsec = pci_find_dvsec_capability(
682 		pdev, PCI_DVSEC_VENDOR_ID_CXL, CXL_DVSEC_PCIE_DEVICE);
683 	if (!cxlds->cxl_dvsec)
684 		dev_warn(&pdev->dev,
685 			 "Device DVSEC not present, skip CXL.mem init\n");
686 
687 	rc = cxl_setup_regs(pdev, CXL_REGLOC_RBI_MEMDEV, &map);
688 	if (rc)
689 		return rc;
690 
691 	rc = cxl_map_device_regs(&pdev->dev, &cxlds->regs.device_regs, &map);
692 	if (rc)
693 		return rc;
694 
695 	/*
696 	 * If the component registers can't be found, the cxl_pci driver may
697 	 * still be useful for management functions so don't return an error.
698 	 */
699 	cxlds->component_reg_phys = CXL_RESOURCE_NONE;
700 	rc = cxl_setup_regs(pdev, CXL_REGLOC_RBI_COMPONENT, &map);
701 	if (rc)
702 		dev_warn(&pdev->dev, "No component registers (%d)\n", rc);
703 
704 	cxlds->component_reg_phys = map.resource;
705 
706 	rc = cxl_map_component_regs(&pdev->dev, &cxlds->regs.component,
707 				    &map, BIT(CXL_CM_CAP_CAP_ID_RAS));
708 	if (rc)
709 		dev_dbg(&pdev->dev, "Failed to map RAS capability.\n");
710 
711 	rc = cxl_pci_setup_mailbox(cxlds);
712 	if (rc)
713 		return rc;
714 
715 	rc = cxl_enumerate_cmds(cxlds);
716 	if (rc)
717 		return rc;
718 
719 	rc = cxl_set_timestamp(cxlds);
720 	if (rc)
721 		return rc;
722 
723 	rc = cxl_poison_state_init(cxlds);
724 	if (rc)
725 		return rc;
726 
727 	rc = cxl_dev_state_identify(cxlds);
728 	if (rc)
729 		return rc;
730 
731 	rc = cxl_mem_create_range_info(cxlds);
732 	if (rc)
733 		return rc;
734 
735 	rc = cxl_alloc_irq_vectors(pdev);
736 	if (rc)
737 		return rc;
738 
739 	cxlmd = devm_cxl_add_memdev(cxlds);
740 	if (IS_ERR(cxlmd))
741 		return PTR_ERR(cxlmd);
742 
743 	rc = cxl_event_config(host_bridge, cxlds);
744 	if (rc)
745 		return rc;
746 
747 	rc = cxl_pci_ras_unmask(pdev);
748 	if (rc)
749 		dev_dbg(&pdev->dev, "No RAS reporting unmasked\n");
750 
751 	pci_save_state(pdev);
752 
753 	return rc;
754 }
755 
756 static const struct pci_device_id cxl_mem_pci_tbl[] = {
757 	/* PCI class code for CXL.mem Type-3 Devices */
758 	{ PCI_DEVICE_CLASS((PCI_CLASS_MEMORY_CXL << 8 | CXL_MEMORY_PROGIF), ~0)},
759 	{ /* terminate list */ },
760 };
761 MODULE_DEVICE_TABLE(pci, cxl_mem_pci_tbl);
762 
763 static pci_ers_result_t cxl_slot_reset(struct pci_dev *pdev)
764 {
765 	struct cxl_dev_state *cxlds = pci_get_drvdata(pdev);
766 	struct cxl_memdev *cxlmd = cxlds->cxlmd;
767 	struct device *dev = &cxlmd->dev;
768 
769 	dev_info(&pdev->dev, "%s: restart CXL.mem after slot reset\n",
770 		 dev_name(dev));
771 	pci_restore_state(pdev);
772 	if (device_attach(dev) <= 0)
773 		return PCI_ERS_RESULT_DISCONNECT;
774 	return PCI_ERS_RESULT_RECOVERED;
775 }
776 
777 static void cxl_error_resume(struct pci_dev *pdev)
778 {
779 	struct cxl_dev_state *cxlds = pci_get_drvdata(pdev);
780 	struct cxl_memdev *cxlmd = cxlds->cxlmd;
781 	struct device *dev = &cxlmd->dev;
782 
783 	dev_info(&pdev->dev, "%s: error resume %s\n", dev_name(dev),
784 		 dev->driver ? "successful" : "failed");
785 }
786 
787 static const struct pci_error_handlers cxl_error_handlers = {
788 	.error_detected	= cxl_error_detected,
789 	.slot_reset	= cxl_slot_reset,
790 	.resume		= cxl_error_resume,
791 	.cor_error_detected	= cxl_cor_error_detected,
792 };
793 
794 static struct pci_driver cxl_pci_driver = {
795 	.name			= KBUILD_MODNAME,
796 	.id_table		= cxl_mem_pci_tbl,
797 	.probe			= cxl_pci_probe,
798 	.err_handler		= &cxl_error_handlers,
799 	.driver	= {
800 		.probe_type	= PROBE_PREFER_ASYNCHRONOUS,
801 	},
802 };
803 
804 MODULE_LICENSE("GPL v2");
805 module_pci_driver(cxl_pci_driver);
806 MODULE_IMPORT_NS(CXL);
807