xref: /openbmc/linux/drivers/cxl/core/port.c (revision 128f20c0)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright(c) 2020 Intel Corporation. All rights reserved. */
3 #include <linux/memregion.h>
4 #include <linux/workqueue.h>
5 #include <linux/debugfs.h>
6 #include <linux/device.h>
7 #include <linux/module.h>
8 #include <linux/pci.h>
9 #include <linux/slab.h>
10 #include <linux/idr.h>
11 #include <cxlmem.h>
12 #include <cxlpci.h>
13 #include <cxl.h>
14 #include "core.h"
15 
16 /**
17  * DOC: cxl core
18  *
19  * The CXL core provides a set of interfaces that can be consumed by CXL aware
20  * drivers. The interfaces allow for creation, modification, and destruction of
21  * regions, memory devices, ports, and decoders. CXL aware drivers must register
22  * with the CXL core via these interfaces in order to be able to participate in
23  * cross-device interleave coordination. The CXL core also establishes and
24  * maintains the bridge to the nvdimm subsystem.
25  *
26  * CXL core introduces sysfs hierarchy to control the devices that are
27  * instantiated by the core.
28  */
29 
30 static DEFINE_IDA(cxl_port_ida);
31 static DEFINE_XARRAY(cxl_root_buses);
32 
33 static ssize_t devtype_show(struct device *dev, struct device_attribute *attr,
34 			    char *buf)
35 {
36 	return sysfs_emit(buf, "%s\n", dev->type->name);
37 }
38 static DEVICE_ATTR_RO(devtype);
39 
40 static int cxl_device_id(const struct device *dev)
41 {
42 	if (dev->type == &cxl_nvdimm_bridge_type)
43 		return CXL_DEVICE_NVDIMM_BRIDGE;
44 	if (dev->type == &cxl_nvdimm_type)
45 		return CXL_DEVICE_NVDIMM;
46 	if (dev->type == CXL_PMEM_REGION_TYPE())
47 		return CXL_DEVICE_PMEM_REGION;
48 	if (dev->type == CXL_DAX_REGION_TYPE())
49 		return CXL_DEVICE_DAX_REGION;
50 	if (is_cxl_port(dev)) {
51 		if (is_cxl_root(to_cxl_port(dev)))
52 			return CXL_DEVICE_ROOT;
53 		return CXL_DEVICE_PORT;
54 	}
55 	if (is_cxl_memdev(dev))
56 		return CXL_DEVICE_MEMORY_EXPANDER;
57 	if (dev->type == CXL_REGION_TYPE())
58 		return CXL_DEVICE_REGION;
59 	return 0;
60 }
61 
62 static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
63 			     char *buf)
64 {
65 	return sysfs_emit(buf, CXL_MODALIAS_FMT "\n", cxl_device_id(dev));
66 }
67 static DEVICE_ATTR_RO(modalias);
68 
69 static struct attribute *cxl_base_attributes[] = {
70 	&dev_attr_devtype.attr,
71 	&dev_attr_modalias.attr,
72 	NULL,
73 };
74 
75 struct attribute_group cxl_base_attribute_group = {
76 	.attrs = cxl_base_attributes,
77 };
78 
79 static ssize_t start_show(struct device *dev, struct device_attribute *attr,
80 			  char *buf)
81 {
82 	struct cxl_decoder *cxld = to_cxl_decoder(dev);
83 
84 	return sysfs_emit(buf, "%#llx\n", cxld->hpa_range.start);
85 }
86 static DEVICE_ATTR_ADMIN_RO(start);
87 
88 static ssize_t size_show(struct device *dev, struct device_attribute *attr,
89 			char *buf)
90 {
91 	struct cxl_decoder *cxld = to_cxl_decoder(dev);
92 
93 	return sysfs_emit(buf, "%#llx\n", range_len(&cxld->hpa_range));
94 }
95 static DEVICE_ATTR_RO(size);
96 
97 #define CXL_DECODER_FLAG_ATTR(name, flag)                            \
98 static ssize_t name##_show(struct device *dev,                       \
99 			   struct device_attribute *attr, char *buf) \
100 {                                                                    \
101 	struct cxl_decoder *cxld = to_cxl_decoder(dev);              \
102                                                                      \
103 	return sysfs_emit(buf, "%s\n",                               \
104 			  (cxld->flags & (flag)) ? "1" : "0");       \
105 }                                                                    \
106 static DEVICE_ATTR_RO(name)
107 
108 CXL_DECODER_FLAG_ATTR(cap_pmem, CXL_DECODER_F_PMEM);
109 CXL_DECODER_FLAG_ATTR(cap_ram, CXL_DECODER_F_RAM);
110 CXL_DECODER_FLAG_ATTR(cap_type2, CXL_DECODER_F_TYPE2);
111 CXL_DECODER_FLAG_ATTR(cap_type3, CXL_DECODER_F_TYPE3);
112 CXL_DECODER_FLAG_ATTR(locked, CXL_DECODER_F_LOCK);
113 
114 static ssize_t target_type_show(struct device *dev,
115 				struct device_attribute *attr, char *buf)
116 {
117 	struct cxl_decoder *cxld = to_cxl_decoder(dev);
118 
119 	switch (cxld->target_type) {
120 	case CXL_DECODER_ACCELERATOR:
121 		return sysfs_emit(buf, "accelerator\n");
122 	case CXL_DECODER_EXPANDER:
123 		return sysfs_emit(buf, "expander\n");
124 	}
125 	return -ENXIO;
126 }
127 static DEVICE_ATTR_RO(target_type);
128 
129 static ssize_t emit_target_list(struct cxl_switch_decoder *cxlsd, char *buf)
130 {
131 	struct cxl_decoder *cxld = &cxlsd->cxld;
132 	ssize_t offset = 0;
133 	int i, rc = 0;
134 
135 	for (i = 0; i < cxld->interleave_ways; i++) {
136 		struct cxl_dport *dport = cxlsd->target[i];
137 		struct cxl_dport *next = NULL;
138 
139 		if (!dport)
140 			break;
141 
142 		if (i + 1 < cxld->interleave_ways)
143 			next = cxlsd->target[i + 1];
144 		rc = sysfs_emit_at(buf, offset, "%d%s", dport->port_id,
145 				   next ? "," : "");
146 		if (rc < 0)
147 			return rc;
148 		offset += rc;
149 	}
150 
151 	return offset;
152 }
153 
154 static ssize_t target_list_show(struct device *dev,
155 				struct device_attribute *attr, char *buf)
156 {
157 	struct cxl_switch_decoder *cxlsd = to_cxl_switch_decoder(dev);
158 	ssize_t offset;
159 	unsigned int seq;
160 	int rc;
161 
162 	do {
163 		seq = read_seqbegin(&cxlsd->target_lock);
164 		rc = emit_target_list(cxlsd, buf);
165 	} while (read_seqretry(&cxlsd->target_lock, seq));
166 
167 	if (rc < 0)
168 		return rc;
169 	offset = rc;
170 
171 	rc = sysfs_emit_at(buf, offset, "\n");
172 	if (rc < 0)
173 		return rc;
174 
175 	return offset + rc;
176 }
177 static DEVICE_ATTR_RO(target_list);
178 
179 static ssize_t mode_show(struct device *dev, struct device_attribute *attr,
180 			 char *buf)
181 {
182 	struct cxl_endpoint_decoder *cxled = to_cxl_endpoint_decoder(dev);
183 
184 	return sysfs_emit(buf, "%s\n", cxl_decoder_mode_name(cxled->mode));
185 }
186 
187 static ssize_t mode_store(struct device *dev, struct device_attribute *attr,
188 			  const char *buf, size_t len)
189 {
190 	struct cxl_endpoint_decoder *cxled = to_cxl_endpoint_decoder(dev);
191 	enum cxl_decoder_mode mode;
192 	ssize_t rc;
193 
194 	if (sysfs_streq(buf, "pmem"))
195 		mode = CXL_DECODER_PMEM;
196 	else if (sysfs_streq(buf, "ram"))
197 		mode = CXL_DECODER_RAM;
198 	else
199 		return -EINVAL;
200 
201 	rc = cxl_dpa_set_mode(cxled, mode);
202 	if (rc)
203 		return rc;
204 
205 	return len;
206 }
207 static DEVICE_ATTR_RW(mode);
208 
209 static ssize_t dpa_resource_show(struct device *dev, struct device_attribute *attr,
210 			    char *buf)
211 {
212 	struct cxl_endpoint_decoder *cxled = to_cxl_endpoint_decoder(dev);
213 	u64 base = cxl_dpa_resource_start(cxled);
214 
215 	return sysfs_emit(buf, "%#llx\n", base);
216 }
217 static DEVICE_ATTR_RO(dpa_resource);
218 
219 static ssize_t dpa_size_show(struct device *dev, struct device_attribute *attr,
220 			     char *buf)
221 {
222 	struct cxl_endpoint_decoder *cxled = to_cxl_endpoint_decoder(dev);
223 	resource_size_t size = cxl_dpa_size(cxled);
224 
225 	return sysfs_emit(buf, "%pa\n", &size);
226 }
227 
228 static ssize_t dpa_size_store(struct device *dev, struct device_attribute *attr,
229 			      const char *buf, size_t len)
230 {
231 	struct cxl_endpoint_decoder *cxled = to_cxl_endpoint_decoder(dev);
232 	unsigned long long size;
233 	ssize_t rc;
234 
235 	rc = kstrtoull(buf, 0, &size);
236 	if (rc)
237 		return rc;
238 
239 	if (!IS_ALIGNED(size, SZ_256M))
240 		return -EINVAL;
241 
242 	rc = cxl_dpa_free(cxled);
243 	if (rc)
244 		return rc;
245 
246 	if (size == 0)
247 		return len;
248 
249 	rc = cxl_dpa_alloc(cxled, size);
250 	if (rc)
251 		return rc;
252 
253 	return len;
254 }
255 static DEVICE_ATTR_RW(dpa_size);
256 
257 static ssize_t interleave_granularity_show(struct device *dev,
258 					   struct device_attribute *attr,
259 					   char *buf)
260 {
261 	struct cxl_decoder *cxld = to_cxl_decoder(dev);
262 
263 	return sysfs_emit(buf, "%d\n", cxld->interleave_granularity);
264 }
265 
266 static DEVICE_ATTR_RO(interleave_granularity);
267 
268 static ssize_t interleave_ways_show(struct device *dev,
269 				    struct device_attribute *attr, char *buf)
270 {
271 	struct cxl_decoder *cxld = to_cxl_decoder(dev);
272 
273 	return sysfs_emit(buf, "%d\n", cxld->interleave_ways);
274 }
275 
276 static DEVICE_ATTR_RO(interleave_ways);
277 
278 static struct attribute *cxl_decoder_base_attrs[] = {
279 	&dev_attr_start.attr,
280 	&dev_attr_size.attr,
281 	&dev_attr_locked.attr,
282 	&dev_attr_interleave_granularity.attr,
283 	&dev_attr_interleave_ways.attr,
284 	NULL,
285 };
286 
287 static struct attribute_group cxl_decoder_base_attribute_group = {
288 	.attrs = cxl_decoder_base_attrs,
289 };
290 
291 static struct attribute *cxl_decoder_root_attrs[] = {
292 	&dev_attr_cap_pmem.attr,
293 	&dev_attr_cap_ram.attr,
294 	&dev_attr_cap_type2.attr,
295 	&dev_attr_cap_type3.attr,
296 	&dev_attr_target_list.attr,
297 	SET_CXL_REGION_ATTR(create_pmem_region)
298 	SET_CXL_REGION_ATTR(create_ram_region)
299 	SET_CXL_REGION_ATTR(delete_region)
300 	NULL,
301 };
302 
303 static bool can_create_pmem(struct cxl_root_decoder *cxlrd)
304 {
305 	unsigned long flags = CXL_DECODER_F_TYPE3 | CXL_DECODER_F_PMEM;
306 
307 	return (cxlrd->cxlsd.cxld.flags & flags) == flags;
308 }
309 
310 static bool can_create_ram(struct cxl_root_decoder *cxlrd)
311 {
312 	unsigned long flags = CXL_DECODER_F_TYPE3 | CXL_DECODER_F_RAM;
313 
314 	return (cxlrd->cxlsd.cxld.flags & flags) == flags;
315 }
316 
317 static umode_t cxl_root_decoder_visible(struct kobject *kobj, struct attribute *a, int n)
318 {
319 	struct device *dev = kobj_to_dev(kobj);
320 	struct cxl_root_decoder *cxlrd = to_cxl_root_decoder(dev);
321 
322 	if (a == CXL_REGION_ATTR(create_pmem_region) && !can_create_pmem(cxlrd))
323 		return 0;
324 
325 	if (a == CXL_REGION_ATTR(create_ram_region) && !can_create_ram(cxlrd))
326 		return 0;
327 
328 	if (a == CXL_REGION_ATTR(delete_region) &&
329 	    !(can_create_pmem(cxlrd) || can_create_ram(cxlrd)))
330 		return 0;
331 
332 	return a->mode;
333 }
334 
335 static struct attribute_group cxl_decoder_root_attribute_group = {
336 	.attrs = cxl_decoder_root_attrs,
337 	.is_visible = cxl_root_decoder_visible,
338 };
339 
340 static const struct attribute_group *cxl_decoder_root_attribute_groups[] = {
341 	&cxl_decoder_root_attribute_group,
342 	&cxl_decoder_base_attribute_group,
343 	&cxl_base_attribute_group,
344 	NULL,
345 };
346 
347 static struct attribute *cxl_decoder_switch_attrs[] = {
348 	&dev_attr_target_type.attr,
349 	&dev_attr_target_list.attr,
350 	SET_CXL_REGION_ATTR(region)
351 	NULL,
352 };
353 
354 static struct attribute_group cxl_decoder_switch_attribute_group = {
355 	.attrs = cxl_decoder_switch_attrs,
356 };
357 
358 static const struct attribute_group *cxl_decoder_switch_attribute_groups[] = {
359 	&cxl_decoder_switch_attribute_group,
360 	&cxl_decoder_base_attribute_group,
361 	&cxl_base_attribute_group,
362 	NULL,
363 };
364 
365 static struct attribute *cxl_decoder_endpoint_attrs[] = {
366 	&dev_attr_target_type.attr,
367 	&dev_attr_mode.attr,
368 	&dev_attr_dpa_size.attr,
369 	&dev_attr_dpa_resource.attr,
370 	SET_CXL_REGION_ATTR(region)
371 	NULL,
372 };
373 
374 static struct attribute_group cxl_decoder_endpoint_attribute_group = {
375 	.attrs = cxl_decoder_endpoint_attrs,
376 };
377 
378 static const struct attribute_group *cxl_decoder_endpoint_attribute_groups[] = {
379 	&cxl_decoder_base_attribute_group,
380 	&cxl_decoder_endpoint_attribute_group,
381 	&cxl_base_attribute_group,
382 	NULL,
383 };
384 
385 static void __cxl_decoder_release(struct cxl_decoder *cxld)
386 {
387 	struct cxl_port *port = to_cxl_port(cxld->dev.parent);
388 
389 	ida_free(&port->decoder_ida, cxld->id);
390 	put_device(&port->dev);
391 }
392 
393 static void cxl_endpoint_decoder_release(struct device *dev)
394 {
395 	struct cxl_endpoint_decoder *cxled = to_cxl_endpoint_decoder(dev);
396 
397 	__cxl_decoder_release(&cxled->cxld);
398 	kfree(cxled);
399 }
400 
401 static void cxl_switch_decoder_release(struct device *dev)
402 {
403 	struct cxl_switch_decoder *cxlsd = to_cxl_switch_decoder(dev);
404 
405 	__cxl_decoder_release(&cxlsd->cxld);
406 	kfree(cxlsd);
407 }
408 
409 struct cxl_root_decoder *to_cxl_root_decoder(struct device *dev)
410 {
411 	if (dev_WARN_ONCE(dev, !is_root_decoder(dev),
412 			  "not a cxl_root_decoder device\n"))
413 		return NULL;
414 	return container_of(dev, struct cxl_root_decoder, cxlsd.cxld.dev);
415 }
416 EXPORT_SYMBOL_NS_GPL(to_cxl_root_decoder, CXL);
417 
418 static void cxl_root_decoder_release(struct device *dev)
419 {
420 	struct cxl_root_decoder *cxlrd = to_cxl_root_decoder(dev);
421 
422 	if (atomic_read(&cxlrd->region_id) >= 0)
423 		memregion_free(atomic_read(&cxlrd->region_id));
424 	__cxl_decoder_release(&cxlrd->cxlsd.cxld);
425 	kfree(cxlrd);
426 }
427 
428 static const struct device_type cxl_decoder_endpoint_type = {
429 	.name = "cxl_decoder_endpoint",
430 	.release = cxl_endpoint_decoder_release,
431 	.groups = cxl_decoder_endpoint_attribute_groups,
432 };
433 
434 static const struct device_type cxl_decoder_switch_type = {
435 	.name = "cxl_decoder_switch",
436 	.release = cxl_switch_decoder_release,
437 	.groups = cxl_decoder_switch_attribute_groups,
438 };
439 
440 static const struct device_type cxl_decoder_root_type = {
441 	.name = "cxl_decoder_root",
442 	.release = cxl_root_decoder_release,
443 	.groups = cxl_decoder_root_attribute_groups,
444 };
445 
446 bool is_endpoint_decoder(struct device *dev)
447 {
448 	return dev->type == &cxl_decoder_endpoint_type;
449 }
450 EXPORT_SYMBOL_NS_GPL(is_endpoint_decoder, CXL);
451 
452 bool is_root_decoder(struct device *dev)
453 {
454 	return dev->type == &cxl_decoder_root_type;
455 }
456 EXPORT_SYMBOL_NS_GPL(is_root_decoder, CXL);
457 
458 bool is_switch_decoder(struct device *dev)
459 {
460 	return is_root_decoder(dev) || dev->type == &cxl_decoder_switch_type;
461 }
462 EXPORT_SYMBOL_NS_GPL(is_switch_decoder, CXL);
463 
464 struct cxl_decoder *to_cxl_decoder(struct device *dev)
465 {
466 	if (dev_WARN_ONCE(dev,
467 			  !is_switch_decoder(dev) && !is_endpoint_decoder(dev),
468 			  "not a cxl_decoder device\n"))
469 		return NULL;
470 	return container_of(dev, struct cxl_decoder, dev);
471 }
472 EXPORT_SYMBOL_NS_GPL(to_cxl_decoder, CXL);
473 
474 struct cxl_endpoint_decoder *to_cxl_endpoint_decoder(struct device *dev)
475 {
476 	if (dev_WARN_ONCE(dev, !is_endpoint_decoder(dev),
477 			  "not a cxl_endpoint_decoder device\n"))
478 		return NULL;
479 	return container_of(dev, struct cxl_endpoint_decoder, cxld.dev);
480 }
481 EXPORT_SYMBOL_NS_GPL(to_cxl_endpoint_decoder, CXL);
482 
483 struct cxl_switch_decoder *to_cxl_switch_decoder(struct device *dev)
484 {
485 	if (dev_WARN_ONCE(dev, !is_switch_decoder(dev),
486 			  "not a cxl_switch_decoder device\n"))
487 		return NULL;
488 	return container_of(dev, struct cxl_switch_decoder, cxld.dev);
489 }
490 EXPORT_SYMBOL_NS_GPL(to_cxl_switch_decoder, CXL);
491 
492 static void cxl_ep_release(struct cxl_ep *ep)
493 {
494 	put_device(ep->ep);
495 	kfree(ep);
496 }
497 
498 static void cxl_ep_remove(struct cxl_port *port, struct cxl_ep *ep)
499 {
500 	if (!ep)
501 		return;
502 	xa_erase(&port->endpoints, (unsigned long) ep->ep);
503 	cxl_ep_release(ep);
504 }
505 
506 static void cxl_port_release(struct device *dev)
507 {
508 	struct cxl_port *port = to_cxl_port(dev);
509 	unsigned long index;
510 	struct cxl_ep *ep;
511 
512 	xa_for_each(&port->endpoints, index, ep)
513 		cxl_ep_remove(port, ep);
514 	xa_destroy(&port->endpoints);
515 	xa_destroy(&port->dports);
516 	xa_destroy(&port->regions);
517 	ida_free(&cxl_port_ida, port->id);
518 	kfree(port);
519 }
520 
521 static const struct attribute_group *cxl_port_attribute_groups[] = {
522 	&cxl_base_attribute_group,
523 	NULL,
524 };
525 
526 static const struct device_type cxl_port_type = {
527 	.name = "cxl_port",
528 	.release = cxl_port_release,
529 	.groups = cxl_port_attribute_groups,
530 };
531 
532 bool is_cxl_port(const struct device *dev)
533 {
534 	return dev->type == &cxl_port_type;
535 }
536 EXPORT_SYMBOL_NS_GPL(is_cxl_port, CXL);
537 
538 struct cxl_port *to_cxl_port(const struct device *dev)
539 {
540 	if (dev_WARN_ONCE(dev, dev->type != &cxl_port_type,
541 			  "not a cxl_port device\n"))
542 		return NULL;
543 	return container_of(dev, struct cxl_port, dev);
544 }
545 EXPORT_SYMBOL_NS_GPL(to_cxl_port, CXL);
546 
547 static void unregister_port(void *_port)
548 {
549 	struct cxl_port *port = _port;
550 	struct cxl_port *parent;
551 	struct device *lock_dev;
552 
553 	if (is_cxl_root(port))
554 		parent = NULL;
555 	else
556 		parent = to_cxl_port(port->dev.parent);
557 
558 	/*
559 	 * CXL root port's and the first level of ports are unregistered
560 	 * under the platform firmware device lock, all other ports are
561 	 * unregistered while holding their parent port lock.
562 	 */
563 	if (!parent)
564 		lock_dev = port->uport;
565 	else if (is_cxl_root(parent))
566 		lock_dev = parent->uport;
567 	else
568 		lock_dev = &parent->dev;
569 
570 	device_lock_assert(lock_dev);
571 	port->dead = true;
572 	device_unregister(&port->dev);
573 }
574 
575 static void cxl_unlink_uport(void *_port)
576 {
577 	struct cxl_port *port = _port;
578 
579 	sysfs_remove_link(&port->dev.kobj, "uport");
580 }
581 
582 static int devm_cxl_link_uport(struct device *host, struct cxl_port *port)
583 {
584 	int rc;
585 
586 	rc = sysfs_create_link(&port->dev.kobj, &port->uport->kobj, "uport");
587 	if (rc)
588 		return rc;
589 	return devm_add_action_or_reset(host, cxl_unlink_uport, port);
590 }
591 
592 static void cxl_unlink_parent_dport(void *_port)
593 {
594 	struct cxl_port *port = _port;
595 
596 	sysfs_remove_link(&port->dev.kobj, "parent_dport");
597 }
598 
599 static int devm_cxl_link_parent_dport(struct device *host,
600 				      struct cxl_port *port,
601 				      struct cxl_dport *parent_dport)
602 {
603 	int rc;
604 
605 	if (!parent_dport)
606 		return 0;
607 
608 	rc = sysfs_create_link(&port->dev.kobj, &parent_dport->dport->kobj,
609 			       "parent_dport");
610 	if (rc)
611 		return rc;
612 	return devm_add_action_or_reset(host, cxl_unlink_parent_dport, port);
613 }
614 
615 static struct lock_class_key cxl_port_key;
616 
617 static struct cxl_port *cxl_port_alloc(struct device *uport,
618 				       resource_size_t component_reg_phys,
619 				       struct cxl_dport *parent_dport)
620 {
621 	struct cxl_port *port;
622 	struct device *dev;
623 	int rc;
624 
625 	port = kzalloc(sizeof(*port), GFP_KERNEL);
626 	if (!port)
627 		return ERR_PTR(-ENOMEM);
628 
629 	rc = ida_alloc(&cxl_port_ida, GFP_KERNEL);
630 	if (rc < 0)
631 		goto err;
632 	port->id = rc;
633 	port->uport = uport;
634 
635 	/*
636 	 * The top-level cxl_port "cxl_root" does not have a cxl_port as
637 	 * its parent and it does not have any corresponding component
638 	 * registers as its decode is described by a fixed platform
639 	 * description.
640 	 */
641 	dev = &port->dev;
642 	if (parent_dport) {
643 		struct cxl_port *parent_port = parent_dport->port;
644 		struct cxl_port *iter;
645 
646 		dev->parent = &parent_port->dev;
647 		port->depth = parent_port->depth + 1;
648 		port->parent_dport = parent_dport;
649 
650 		/*
651 		 * walk to the host bridge, or the first ancestor that knows
652 		 * the host bridge
653 		 */
654 		iter = port;
655 		while (!iter->host_bridge &&
656 		       !is_cxl_root(to_cxl_port(iter->dev.parent)))
657 			iter = to_cxl_port(iter->dev.parent);
658 		if (iter->host_bridge)
659 			port->host_bridge = iter->host_bridge;
660 		else if (parent_dport->rch)
661 			port->host_bridge = parent_dport->dport;
662 		else
663 			port->host_bridge = iter->uport;
664 		dev_dbg(uport, "host-bridge: %s\n", dev_name(port->host_bridge));
665 	} else
666 		dev->parent = uport;
667 
668 	port->component_reg_phys = component_reg_phys;
669 	ida_init(&port->decoder_ida);
670 	port->hdm_end = -1;
671 	port->commit_end = -1;
672 	xa_init(&port->dports);
673 	xa_init(&port->endpoints);
674 	xa_init(&port->regions);
675 
676 	device_initialize(dev);
677 	lockdep_set_class_and_subclass(&dev->mutex, &cxl_port_key, port->depth);
678 	device_set_pm_not_required(dev);
679 	dev->bus = &cxl_bus_type;
680 	dev->type = &cxl_port_type;
681 
682 	return port;
683 
684 err:
685 	kfree(port);
686 	return ERR_PTR(rc);
687 }
688 
689 static struct cxl_port *__devm_cxl_add_port(struct device *host,
690 					    struct device *uport,
691 					    resource_size_t component_reg_phys,
692 					    struct cxl_dport *parent_dport)
693 {
694 	struct cxl_port *port;
695 	struct device *dev;
696 	int rc;
697 
698 	port = cxl_port_alloc(uport, component_reg_phys, parent_dport);
699 	if (IS_ERR(port))
700 		return port;
701 
702 	dev = &port->dev;
703 	if (is_cxl_memdev(uport))
704 		rc = dev_set_name(dev, "endpoint%d", port->id);
705 	else if (parent_dport)
706 		rc = dev_set_name(dev, "port%d", port->id);
707 	else
708 		rc = dev_set_name(dev, "root%d", port->id);
709 	if (rc)
710 		goto err;
711 
712 	rc = device_add(dev);
713 	if (rc)
714 		goto err;
715 
716 	rc = devm_add_action_or_reset(host, unregister_port, port);
717 	if (rc)
718 		return ERR_PTR(rc);
719 
720 	rc = devm_cxl_link_uport(host, port);
721 	if (rc)
722 		return ERR_PTR(rc);
723 
724 	rc = devm_cxl_link_parent_dport(host, port, parent_dport);
725 	if (rc)
726 		return ERR_PTR(rc);
727 
728 	return port;
729 
730 err:
731 	put_device(dev);
732 	return ERR_PTR(rc);
733 }
734 
735 /**
736  * devm_cxl_add_port - register a cxl_port in CXL memory decode hierarchy
737  * @host: host device for devm operations
738  * @uport: "physical" device implementing this upstream port
739  * @component_reg_phys: (optional) for configurable cxl_port instances
740  * @parent_dport: next hop up in the CXL memory decode hierarchy
741  */
742 struct cxl_port *devm_cxl_add_port(struct device *host, struct device *uport,
743 				   resource_size_t component_reg_phys,
744 				   struct cxl_dport *parent_dport)
745 {
746 	struct cxl_port *port, *parent_port;
747 
748 	port = __devm_cxl_add_port(host, uport, component_reg_phys,
749 				   parent_dport);
750 
751 	parent_port = parent_dport ? parent_dport->port : NULL;
752 	if (IS_ERR(port)) {
753 		dev_dbg(uport, "Failed to add%s%s%s: %ld\n",
754 			parent_port ? " port to " : "",
755 			parent_port ? dev_name(&parent_port->dev) : "",
756 			parent_port ? "" : " root port",
757 			PTR_ERR(port));
758 	} else {
759 		dev_dbg(uport, "%s added%s%s%s\n",
760 			dev_name(&port->dev),
761 			parent_port ? " to " : "",
762 			parent_port ? dev_name(&parent_port->dev) : "",
763 			parent_port ? "" : " (root port)");
764 	}
765 
766 	return port;
767 }
768 EXPORT_SYMBOL_NS_GPL(devm_cxl_add_port, CXL);
769 
770 struct pci_bus *cxl_port_to_pci_bus(struct cxl_port *port)
771 {
772 	/* There is no pci_bus associated with a CXL platform-root port */
773 	if (is_cxl_root(port))
774 		return NULL;
775 
776 	if (dev_is_pci(port->uport)) {
777 		struct pci_dev *pdev = to_pci_dev(port->uport);
778 
779 		return pdev->subordinate;
780 	}
781 
782 	return xa_load(&cxl_root_buses, (unsigned long)port->uport);
783 }
784 EXPORT_SYMBOL_NS_GPL(cxl_port_to_pci_bus, CXL);
785 
786 static void unregister_pci_bus(void *uport)
787 {
788 	xa_erase(&cxl_root_buses, (unsigned long)uport);
789 }
790 
791 int devm_cxl_register_pci_bus(struct device *host, struct device *uport,
792 			      struct pci_bus *bus)
793 {
794 	int rc;
795 
796 	if (dev_is_pci(uport))
797 		return -EINVAL;
798 
799 	rc = xa_insert(&cxl_root_buses, (unsigned long)uport, bus, GFP_KERNEL);
800 	if (rc)
801 		return rc;
802 	return devm_add_action_or_reset(host, unregister_pci_bus, uport);
803 }
804 EXPORT_SYMBOL_NS_GPL(devm_cxl_register_pci_bus, CXL);
805 
806 static bool dev_is_cxl_root_child(struct device *dev)
807 {
808 	struct cxl_port *port, *parent;
809 
810 	if (!is_cxl_port(dev))
811 		return false;
812 
813 	port = to_cxl_port(dev);
814 	if (is_cxl_root(port))
815 		return false;
816 
817 	parent = to_cxl_port(port->dev.parent);
818 	if (is_cxl_root(parent))
819 		return true;
820 
821 	return false;
822 }
823 
824 struct cxl_port *find_cxl_root(struct cxl_port *port)
825 {
826 	struct cxl_port *iter = port;
827 
828 	while (iter && !is_cxl_root(iter))
829 		iter = to_cxl_port(iter->dev.parent);
830 
831 	if (!iter)
832 		return NULL;
833 	get_device(&iter->dev);
834 	return iter;
835 }
836 EXPORT_SYMBOL_NS_GPL(find_cxl_root, CXL);
837 
838 static struct cxl_dport *find_dport(struct cxl_port *port, int id)
839 {
840 	struct cxl_dport *dport;
841 	unsigned long index;
842 
843 	device_lock_assert(&port->dev);
844 	xa_for_each(&port->dports, index, dport)
845 		if (dport->port_id == id)
846 			return dport;
847 	return NULL;
848 }
849 
850 static int add_dport(struct cxl_port *port, struct cxl_dport *new)
851 {
852 	struct cxl_dport *dup;
853 	int rc;
854 
855 	device_lock_assert(&port->dev);
856 	dup = find_dport(port, new->port_id);
857 	if (dup) {
858 		dev_err(&port->dev,
859 			"unable to add dport%d-%s non-unique port id (%s)\n",
860 			new->port_id, dev_name(new->dport),
861 			dev_name(dup->dport));
862 		return -EBUSY;
863 	}
864 
865 	rc = xa_insert(&port->dports, (unsigned long)new->dport, new,
866 		       GFP_KERNEL);
867 	if (rc)
868 		return rc;
869 
870 	port->nr_dports++;
871 	return 0;
872 }
873 
874 /*
875  * Since root-level CXL dports cannot be enumerated by PCI they are not
876  * enumerated by the common port driver that acquires the port lock over
877  * dport add/remove. Instead, root dports are manually added by a
878  * platform driver and cond_cxl_root_lock() is used to take the missing
879  * port lock in that case.
880  */
881 static void cond_cxl_root_lock(struct cxl_port *port)
882 {
883 	if (is_cxl_root(port))
884 		device_lock(&port->dev);
885 }
886 
887 static void cond_cxl_root_unlock(struct cxl_port *port)
888 {
889 	if (is_cxl_root(port))
890 		device_unlock(&port->dev);
891 }
892 
893 static void cxl_dport_remove(void *data)
894 {
895 	struct cxl_dport *dport = data;
896 	struct cxl_port *port = dport->port;
897 
898 	xa_erase(&port->dports, (unsigned long) dport->dport);
899 	put_device(dport->dport);
900 }
901 
902 static void cxl_dport_unlink(void *data)
903 {
904 	struct cxl_dport *dport = data;
905 	struct cxl_port *port = dport->port;
906 	char link_name[CXL_TARGET_STRLEN];
907 
908 	sprintf(link_name, "dport%d", dport->port_id);
909 	sysfs_remove_link(&port->dev.kobj, link_name);
910 }
911 
912 static struct cxl_dport *
913 __devm_cxl_add_dport(struct cxl_port *port, struct device *dport_dev,
914 		     int port_id, resource_size_t component_reg_phys,
915 		     resource_size_t rcrb)
916 {
917 	char link_name[CXL_TARGET_STRLEN];
918 	struct cxl_dport *dport;
919 	struct device *host;
920 	int rc;
921 
922 	if (is_cxl_root(port))
923 		host = port->uport;
924 	else
925 		host = &port->dev;
926 
927 	if (!host->driver) {
928 		dev_WARN_ONCE(&port->dev, 1, "dport:%s bad devm context\n",
929 			      dev_name(dport_dev));
930 		return ERR_PTR(-ENXIO);
931 	}
932 
933 	if (snprintf(link_name, CXL_TARGET_STRLEN, "dport%d", port_id) >=
934 	    CXL_TARGET_STRLEN)
935 		return ERR_PTR(-EINVAL);
936 
937 	dport = devm_kzalloc(host, sizeof(*dport), GFP_KERNEL);
938 	if (!dport)
939 		return ERR_PTR(-ENOMEM);
940 
941 	dport->dport = dport_dev;
942 	dport->port_id = port_id;
943 	dport->component_reg_phys = component_reg_phys;
944 	dport->port = port;
945 	if (rcrb != CXL_RESOURCE_NONE)
946 		dport->rch = true;
947 	dport->rcrb = rcrb;
948 
949 	cond_cxl_root_lock(port);
950 	rc = add_dport(port, dport);
951 	cond_cxl_root_unlock(port);
952 	if (rc)
953 		return ERR_PTR(rc);
954 
955 	get_device(dport_dev);
956 	rc = devm_add_action_or_reset(host, cxl_dport_remove, dport);
957 	if (rc)
958 		return ERR_PTR(rc);
959 
960 	rc = sysfs_create_link(&port->dev.kobj, &dport_dev->kobj, link_name);
961 	if (rc)
962 		return ERR_PTR(rc);
963 
964 	rc = devm_add_action_or_reset(host, cxl_dport_unlink, dport);
965 	if (rc)
966 		return ERR_PTR(rc);
967 
968 	return dport;
969 }
970 
971 /**
972  * devm_cxl_add_dport - append VH downstream port data to a cxl_port
973  * @port: the cxl_port that references this dport
974  * @dport_dev: firmware or PCI device representing the dport
975  * @port_id: identifier for this dport in a decoder's target list
976  * @component_reg_phys: optional location of CXL component registers
977  *
978  * Note that dports are appended to the devm release action's of the
979  * either the port's host (for root ports), or the port itself (for
980  * switch ports)
981  */
982 struct cxl_dport *devm_cxl_add_dport(struct cxl_port *port,
983 				     struct device *dport_dev, int port_id,
984 				     resource_size_t component_reg_phys)
985 {
986 	struct cxl_dport *dport;
987 
988 	dport = __devm_cxl_add_dport(port, dport_dev, port_id,
989 				     component_reg_phys, CXL_RESOURCE_NONE);
990 	if (IS_ERR(dport)) {
991 		dev_dbg(dport_dev, "failed to add dport to %s: %ld\n",
992 			dev_name(&port->dev), PTR_ERR(dport));
993 	} else {
994 		dev_dbg(dport_dev, "dport added to %s\n",
995 			dev_name(&port->dev));
996 	}
997 
998 	return dport;
999 }
1000 EXPORT_SYMBOL_NS_GPL(devm_cxl_add_dport, CXL);
1001 
1002 /**
1003  * devm_cxl_add_rch_dport - append RCH downstream port data to a cxl_port
1004  * @port: the cxl_port that references this dport
1005  * @dport_dev: firmware or PCI device representing the dport
1006  * @port_id: identifier for this dport in a decoder's target list
1007  * @component_reg_phys: optional location of CXL component registers
1008  * @rcrb: mandatory location of a Root Complex Register Block
1009  *
1010  * See CXL 3.0 9.11.8 CXL Devices Attached to an RCH
1011  */
1012 struct cxl_dport *devm_cxl_add_rch_dport(struct cxl_port *port,
1013 					 struct device *dport_dev, int port_id,
1014 					 resource_size_t component_reg_phys,
1015 					 resource_size_t rcrb)
1016 {
1017 	struct cxl_dport *dport;
1018 
1019 	if (rcrb == CXL_RESOURCE_NONE) {
1020 		dev_dbg(&port->dev, "failed to add RCH dport, missing RCRB\n");
1021 		return ERR_PTR(-EINVAL);
1022 	}
1023 
1024 	dport = __devm_cxl_add_dport(port, dport_dev, port_id,
1025 				     component_reg_phys, rcrb);
1026 	if (IS_ERR(dport)) {
1027 		dev_dbg(dport_dev, "failed to add RCH dport to %s: %ld\n",
1028 			dev_name(&port->dev), PTR_ERR(dport));
1029 	} else {
1030 		dev_dbg(dport_dev, "RCH dport added to %s\n",
1031 			dev_name(&port->dev));
1032 	}
1033 
1034 	return dport;
1035 }
1036 EXPORT_SYMBOL_NS_GPL(devm_cxl_add_rch_dport, CXL);
1037 
1038 static int add_ep(struct cxl_ep *new)
1039 {
1040 	struct cxl_port *port = new->dport->port;
1041 	int rc;
1042 
1043 	device_lock(&port->dev);
1044 	if (port->dead) {
1045 		device_unlock(&port->dev);
1046 		return -ENXIO;
1047 	}
1048 	rc = xa_insert(&port->endpoints, (unsigned long)new->ep, new,
1049 		       GFP_KERNEL);
1050 	device_unlock(&port->dev);
1051 
1052 	return rc;
1053 }
1054 
1055 /**
1056  * cxl_add_ep - register an endpoint's interest in a port
1057  * @dport: the dport that routes to @ep_dev
1058  * @ep_dev: device representing the endpoint
1059  *
1060  * Intermediate CXL ports are scanned based on the arrival of endpoints.
1061  * When those endpoints depart the port can be destroyed once all
1062  * endpoints that care about that port have been removed.
1063  */
1064 static int cxl_add_ep(struct cxl_dport *dport, struct device *ep_dev)
1065 {
1066 	struct cxl_ep *ep;
1067 	int rc;
1068 
1069 	ep = kzalloc(sizeof(*ep), GFP_KERNEL);
1070 	if (!ep)
1071 		return -ENOMEM;
1072 
1073 	ep->ep = get_device(ep_dev);
1074 	ep->dport = dport;
1075 
1076 	rc = add_ep(ep);
1077 	if (rc)
1078 		cxl_ep_release(ep);
1079 	return rc;
1080 }
1081 
1082 struct cxl_find_port_ctx {
1083 	const struct device *dport_dev;
1084 	const struct cxl_port *parent_port;
1085 	struct cxl_dport **dport;
1086 };
1087 
1088 static int match_port_by_dport(struct device *dev, const void *data)
1089 {
1090 	const struct cxl_find_port_ctx *ctx = data;
1091 	struct cxl_dport *dport;
1092 	struct cxl_port *port;
1093 
1094 	if (!is_cxl_port(dev))
1095 		return 0;
1096 	if (ctx->parent_port && dev->parent != &ctx->parent_port->dev)
1097 		return 0;
1098 
1099 	port = to_cxl_port(dev);
1100 	dport = cxl_find_dport_by_dev(port, ctx->dport_dev);
1101 	if (ctx->dport)
1102 		*ctx->dport = dport;
1103 	return dport != NULL;
1104 }
1105 
1106 static struct cxl_port *__find_cxl_port(struct cxl_find_port_ctx *ctx)
1107 {
1108 	struct device *dev;
1109 
1110 	if (!ctx->dport_dev)
1111 		return NULL;
1112 
1113 	dev = bus_find_device(&cxl_bus_type, NULL, ctx, match_port_by_dport);
1114 	if (dev)
1115 		return to_cxl_port(dev);
1116 	return NULL;
1117 }
1118 
1119 static struct cxl_port *find_cxl_port(struct device *dport_dev,
1120 				      struct cxl_dport **dport)
1121 {
1122 	struct cxl_find_port_ctx ctx = {
1123 		.dport_dev = dport_dev,
1124 		.dport = dport,
1125 	};
1126 	struct cxl_port *port;
1127 
1128 	port = __find_cxl_port(&ctx);
1129 	return port;
1130 }
1131 
1132 static struct cxl_port *find_cxl_port_at(struct cxl_port *parent_port,
1133 					 struct device *dport_dev,
1134 					 struct cxl_dport **dport)
1135 {
1136 	struct cxl_find_port_ctx ctx = {
1137 		.dport_dev = dport_dev,
1138 		.parent_port = parent_port,
1139 		.dport = dport,
1140 	};
1141 	struct cxl_port *port;
1142 
1143 	port = __find_cxl_port(&ctx);
1144 	return port;
1145 }
1146 
1147 /*
1148  * All users of grandparent() are using it to walk PCIe-like switch port
1149  * hierarchy. A PCIe switch is comprised of a bridge device representing the
1150  * upstream switch port and N bridges representing downstream switch ports. When
1151  * bridges stack the grand-parent of a downstream switch port is another
1152  * downstream switch port in the immediate ancestor switch.
1153  */
1154 static struct device *grandparent(struct device *dev)
1155 {
1156 	if (dev && dev->parent)
1157 		return dev->parent->parent;
1158 	return NULL;
1159 }
1160 
1161 static void delete_endpoint(void *data)
1162 {
1163 	struct cxl_memdev *cxlmd = data;
1164 	struct cxl_port *endpoint = dev_get_drvdata(&cxlmd->dev);
1165 	struct cxl_port *parent_port;
1166 	struct device *parent;
1167 
1168 	parent_port = cxl_mem_find_port(cxlmd, NULL);
1169 	if (!parent_port)
1170 		goto out;
1171 	parent = &parent_port->dev;
1172 
1173 	device_lock(parent);
1174 	if (parent->driver && !endpoint->dead) {
1175 		devm_release_action(parent, cxl_unlink_parent_dport, endpoint);
1176 		devm_release_action(parent, cxl_unlink_uport, endpoint);
1177 		devm_release_action(parent, unregister_port, endpoint);
1178 	}
1179 	device_unlock(parent);
1180 	put_device(parent);
1181 out:
1182 	put_device(&endpoint->dev);
1183 }
1184 
1185 int cxl_endpoint_autoremove(struct cxl_memdev *cxlmd, struct cxl_port *endpoint)
1186 {
1187 	struct device *dev = &cxlmd->dev;
1188 
1189 	get_device(&endpoint->dev);
1190 	dev_set_drvdata(dev, endpoint);
1191 	cxlmd->depth = endpoint->depth;
1192 	return devm_add_action_or_reset(dev, delete_endpoint, cxlmd);
1193 }
1194 EXPORT_SYMBOL_NS_GPL(cxl_endpoint_autoremove, CXL);
1195 
1196 /*
1197  * The natural end of life of a non-root 'cxl_port' is when its parent port goes
1198  * through a ->remove() event ("top-down" unregistration). The unnatural trigger
1199  * for a port to be unregistered is when all memdevs beneath that port have gone
1200  * through ->remove(). This "bottom-up" removal selectively removes individual
1201  * child ports manually. This depends on devm_cxl_add_port() to not change is
1202  * devm action registration order, and for dports to have already been
1203  * destroyed by reap_dports().
1204  */
1205 static void delete_switch_port(struct cxl_port *port)
1206 {
1207 	devm_release_action(port->dev.parent, cxl_unlink_parent_dport, port);
1208 	devm_release_action(port->dev.parent, cxl_unlink_uport, port);
1209 	devm_release_action(port->dev.parent, unregister_port, port);
1210 }
1211 
1212 static void reap_dports(struct cxl_port *port)
1213 {
1214 	struct cxl_dport *dport;
1215 	unsigned long index;
1216 
1217 	device_lock_assert(&port->dev);
1218 
1219 	xa_for_each(&port->dports, index, dport) {
1220 		devm_release_action(&port->dev, cxl_dport_unlink, dport);
1221 		devm_release_action(&port->dev, cxl_dport_remove, dport);
1222 		devm_kfree(&port->dev, dport);
1223 	}
1224 }
1225 
1226 struct detach_ctx {
1227 	struct cxl_memdev *cxlmd;
1228 	int depth;
1229 };
1230 
1231 static int port_has_memdev(struct device *dev, const void *data)
1232 {
1233 	const struct detach_ctx *ctx = data;
1234 	struct cxl_port *port;
1235 
1236 	if (!is_cxl_port(dev))
1237 		return 0;
1238 
1239 	port = to_cxl_port(dev);
1240 	if (port->depth != ctx->depth)
1241 		return 0;
1242 
1243 	return !!cxl_ep_load(port, ctx->cxlmd);
1244 }
1245 
1246 static void cxl_detach_ep(void *data)
1247 {
1248 	struct cxl_memdev *cxlmd = data;
1249 
1250 	for (int i = cxlmd->depth - 1; i >= 1; i--) {
1251 		struct cxl_port *port, *parent_port;
1252 		struct detach_ctx ctx = {
1253 			.cxlmd = cxlmd,
1254 			.depth = i,
1255 		};
1256 		struct device *dev;
1257 		struct cxl_ep *ep;
1258 		bool died = false;
1259 
1260 		dev = bus_find_device(&cxl_bus_type, NULL, &ctx,
1261 				      port_has_memdev);
1262 		if (!dev)
1263 			continue;
1264 		port = to_cxl_port(dev);
1265 
1266 		parent_port = to_cxl_port(port->dev.parent);
1267 		device_lock(&parent_port->dev);
1268 		device_lock(&port->dev);
1269 		ep = cxl_ep_load(port, cxlmd);
1270 		dev_dbg(&cxlmd->dev, "disconnect %s from %s\n",
1271 			ep ? dev_name(ep->ep) : "", dev_name(&port->dev));
1272 		cxl_ep_remove(port, ep);
1273 		if (ep && !port->dead && xa_empty(&port->endpoints) &&
1274 		    !is_cxl_root(parent_port) && parent_port->dev.driver) {
1275 			/*
1276 			 * This was the last ep attached to a dynamically
1277 			 * enumerated port. Block new cxl_add_ep() and garbage
1278 			 * collect the port.
1279 			 */
1280 			died = true;
1281 			port->dead = true;
1282 			reap_dports(port);
1283 		}
1284 		device_unlock(&port->dev);
1285 
1286 		if (died) {
1287 			dev_dbg(&cxlmd->dev, "delete %s\n",
1288 				dev_name(&port->dev));
1289 			delete_switch_port(port);
1290 		}
1291 		put_device(&port->dev);
1292 		device_unlock(&parent_port->dev);
1293 	}
1294 }
1295 
1296 static resource_size_t find_component_registers(struct device *dev)
1297 {
1298 	struct cxl_register_map map;
1299 	struct pci_dev *pdev;
1300 
1301 	/*
1302 	 * Theoretically, CXL component registers can be hosted on a
1303 	 * non-PCI device, in practice, only cxl_test hits this case.
1304 	 */
1305 	if (!dev_is_pci(dev))
1306 		return CXL_RESOURCE_NONE;
1307 
1308 	pdev = to_pci_dev(dev);
1309 
1310 	cxl_find_regblock(pdev, CXL_REGLOC_RBI_COMPONENT, &map);
1311 	return map.resource;
1312 }
1313 
1314 static int add_port_attach_ep(struct cxl_memdev *cxlmd,
1315 			      struct device *uport_dev,
1316 			      struct device *dport_dev)
1317 {
1318 	struct device *dparent = grandparent(dport_dev);
1319 	struct cxl_port *port, *parent_port = NULL;
1320 	struct cxl_dport *dport, *parent_dport;
1321 	resource_size_t component_reg_phys;
1322 	int rc;
1323 
1324 	if (!dparent) {
1325 		/*
1326 		 * The iteration reached the topology root without finding the
1327 		 * CXL-root 'cxl_port' on a previous iteration, fail for now to
1328 		 * be re-probed after platform driver attaches.
1329 		 */
1330 		dev_dbg(&cxlmd->dev, "%s is a root dport\n",
1331 			dev_name(dport_dev));
1332 		return -ENXIO;
1333 	}
1334 
1335 	parent_port = find_cxl_port(dparent, &parent_dport);
1336 	if (!parent_port) {
1337 		/* iterate to create this parent_port */
1338 		return -EAGAIN;
1339 	}
1340 
1341 	device_lock(&parent_port->dev);
1342 	if (!parent_port->dev.driver) {
1343 		dev_warn(&cxlmd->dev,
1344 			 "port %s:%s disabled, failed to enumerate CXL.mem\n",
1345 			 dev_name(&parent_port->dev), dev_name(uport_dev));
1346 		port = ERR_PTR(-ENXIO);
1347 		goto out;
1348 	}
1349 
1350 	port = find_cxl_port_at(parent_port, dport_dev, &dport);
1351 	if (!port) {
1352 		component_reg_phys = find_component_registers(uport_dev);
1353 		port = devm_cxl_add_port(&parent_port->dev, uport_dev,
1354 					 component_reg_phys, parent_dport);
1355 		/* retry find to pick up the new dport information */
1356 		if (!IS_ERR(port))
1357 			port = find_cxl_port_at(parent_port, dport_dev, &dport);
1358 	}
1359 out:
1360 	device_unlock(&parent_port->dev);
1361 
1362 	if (IS_ERR(port))
1363 		rc = PTR_ERR(port);
1364 	else {
1365 		dev_dbg(&cxlmd->dev, "add to new port %s:%s\n",
1366 			dev_name(&port->dev), dev_name(port->uport));
1367 		rc = cxl_add_ep(dport, &cxlmd->dev);
1368 		if (rc == -EBUSY) {
1369 			/*
1370 			 * "can't" happen, but this error code means
1371 			 * something to the caller, so translate it.
1372 			 */
1373 			rc = -ENXIO;
1374 		}
1375 		put_device(&port->dev);
1376 	}
1377 
1378 	put_device(&parent_port->dev);
1379 	return rc;
1380 }
1381 
1382 int devm_cxl_enumerate_ports(struct cxl_memdev *cxlmd)
1383 {
1384 	struct device *dev = &cxlmd->dev;
1385 	struct device *iter;
1386 	int rc;
1387 
1388 	/*
1389 	 * Skip intermediate port enumeration in the RCH case, there
1390 	 * are no ports in between a host bridge and an endpoint.
1391 	 */
1392 	if (cxlmd->cxlds->rcd)
1393 		return 0;
1394 
1395 	rc = devm_add_action_or_reset(&cxlmd->dev, cxl_detach_ep, cxlmd);
1396 	if (rc)
1397 		return rc;
1398 
1399 	/*
1400 	 * Scan for and add all cxl_ports in this device's ancestry.
1401 	 * Repeat until no more ports are added. Abort if a port add
1402 	 * attempt fails.
1403 	 */
1404 retry:
1405 	for (iter = dev; iter; iter = grandparent(iter)) {
1406 		struct device *dport_dev = grandparent(iter);
1407 		struct device *uport_dev;
1408 		struct cxl_dport *dport;
1409 		struct cxl_port *port;
1410 
1411 		if (!dport_dev)
1412 			return 0;
1413 
1414 		uport_dev = dport_dev->parent;
1415 		if (!uport_dev) {
1416 			dev_warn(dev, "at %s no parent for dport: %s\n",
1417 				 dev_name(iter), dev_name(dport_dev));
1418 			return -ENXIO;
1419 		}
1420 
1421 		dev_dbg(dev, "scan: iter: %s dport_dev: %s parent: %s\n",
1422 			dev_name(iter), dev_name(dport_dev),
1423 			dev_name(uport_dev));
1424 		port = find_cxl_port(dport_dev, &dport);
1425 		if (port) {
1426 			dev_dbg(&cxlmd->dev,
1427 				"found already registered port %s:%s\n",
1428 				dev_name(&port->dev), dev_name(port->uport));
1429 			rc = cxl_add_ep(dport, &cxlmd->dev);
1430 
1431 			/*
1432 			 * If the endpoint already exists in the port's list,
1433 			 * that's ok, it was added on a previous pass.
1434 			 * Otherwise, retry in add_port_attach_ep() after taking
1435 			 * the parent_port lock as the current port may be being
1436 			 * reaped.
1437 			 */
1438 			if (rc && rc != -EBUSY) {
1439 				put_device(&port->dev);
1440 				return rc;
1441 			}
1442 
1443 			/* Any more ports to add between this one and the root? */
1444 			if (!dev_is_cxl_root_child(&port->dev)) {
1445 				put_device(&port->dev);
1446 				continue;
1447 			}
1448 
1449 			put_device(&port->dev);
1450 			return 0;
1451 		}
1452 
1453 		rc = add_port_attach_ep(cxlmd, uport_dev, dport_dev);
1454 		/* port missing, try to add parent */
1455 		if (rc == -EAGAIN)
1456 			continue;
1457 		/* failed to add ep or port */
1458 		if (rc)
1459 			return rc;
1460 		/* port added, new descendants possible, start over */
1461 		goto retry;
1462 	}
1463 
1464 	return 0;
1465 }
1466 EXPORT_SYMBOL_NS_GPL(devm_cxl_enumerate_ports, CXL);
1467 
1468 struct cxl_port *cxl_mem_find_port(struct cxl_memdev *cxlmd,
1469 				   struct cxl_dport **dport)
1470 {
1471 	return find_cxl_port(grandparent(&cxlmd->dev), dport);
1472 }
1473 EXPORT_SYMBOL_NS_GPL(cxl_mem_find_port, CXL);
1474 
1475 static int decoder_populate_targets(struct cxl_switch_decoder *cxlsd,
1476 				    struct cxl_port *port, int *target_map)
1477 {
1478 	int i, rc = 0;
1479 
1480 	if (!target_map)
1481 		return 0;
1482 
1483 	device_lock_assert(&port->dev);
1484 
1485 	if (xa_empty(&port->dports))
1486 		return -EINVAL;
1487 
1488 	write_seqlock(&cxlsd->target_lock);
1489 	for (i = 0; i < cxlsd->nr_targets; i++) {
1490 		struct cxl_dport *dport = find_dport(port, target_map[i]);
1491 
1492 		if (!dport) {
1493 			rc = -ENXIO;
1494 			break;
1495 		}
1496 		cxlsd->target[i] = dport;
1497 	}
1498 	write_sequnlock(&cxlsd->target_lock);
1499 
1500 	return rc;
1501 }
1502 
1503 struct cxl_dport *cxl_hb_modulo(struct cxl_root_decoder *cxlrd, int pos)
1504 {
1505 	struct cxl_switch_decoder *cxlsd = &cxlrd->cxlsd;
1506 	struct cxl_decoder *cxld = &cxlsd->cxld;
1507 	int iw;
1508 
1509 	iw = cxld->interleave_ways;
1510 	if (dev_WARN_ONCE(&cxld->dev, iw != cxlsd->nr_targets,
1511 			  "misconfigured root decoder\n"))
1512 		return NULL;
1513 
1514 	return cxlrd->cxlsd.target[pos % iw];
1515 }
1516 EXPORT_SYMBOL_NS_GPL(cxl_hb_modulo, CXL);
1517 
1518 static struct lock_class_key cxl_decoder_key;
1519 
1520 /**
1521  * cxl_decoder_init - Common decoder setup / initialization
1522  * @port: owning port of this decoder
1523  * @cxld: common decoder properties to initialize
1524  *
1525  * A port may contain one or more decoders. Each of those decoders
1526  * enable some address space for CXL.mem utilization. A decoder is
1527  * expected to be configured by the caller before registering via
1528  * cxl_decoder_add()
1529  */
1530 static int cxl_decoder_init(struct cxl_port *port, struct cxl_decoder *cxld)
1531 {
1532 	struct device *dev;
1533 	int rc;
1534 
1535 	rc = ida_alloc(&port->decoder_ida, GFP_KERNEL);
1536 	if (rc < 0)
1537 		return rc;
1538 
1539 	/* need parent to stick around to release the id */
1540 	get_device(&port->dev);
1541 	cxld->id = rc;
1542 
1543 	dev = &cxld->dev;
1544 	device_initialize(dev);
1545 	lockdep_set_class(&dev->mutex, &cxl_decoder_key);
1546 	device_set_pm_not_required(dev);
1547 	dev->parent = &port->dev;
1548 	dev->bus = &cxl_bus_type;
1549 
1550 	/* Pre initialize an "empty" decoder */
1551 	cxld->interleave_ways = 1;
1552 	cxld->interleave_granularity = PAGE_SIZE;
1553 	cxld->target_type = CXL_DECODER_EXPANDER;
1554 	cxld->hpa_range = (struct range) {
1555 		.start = 0,
1556 		.end = -1,
1557 	};
1558 
1559 	return 0;
1560 }
1561 
1562 static int cxl_switch_decoder_init(struct cxl_port *port,
1563 				   struct cxl_switch_decoder *cxlsd,
1564 				   int nr_targets)
1565 {
1566 	if (nr_targets > CXL_DECODER_MAX_INTERLEAVE)
1567 		return -EINVAL;
1568 
1569 	cxlsd->nr_targets = nr_targets;
1570 	seqlock_init(&cxlsd->target_lock);
1571 	return cxl_decoder_init(port, &cxlsd->cxld);
1572 }
1573 
1574 /**
1575  * cxl_root_decoder_alloc - Allocate a root level decoder
1576  * @port: owning CXL root of this decoder
1577  * @nr_targets: static number of downstream targets
1578  * @calc_hb: which host bridge covers the n'th position by granularity
1579  *
1580  * Return: A new cxl decoder to be registered by cxl_decoder_add(). A
1581  * 'CXL root' decoder is one that decodes from a top-level / static platform
1582  * firmware description of CXL resources into a CXL standard decode
1583  * topology.
1584  */
1585 struct cxl_root_decoder *cxl_root_decoder_alloc(struct cxl_port *port,
1586 						unsigned int nr_targets,
1587 						cxl_calc_hb_fn calc_hb)
1588 {
1589 	struct cxl_root_decoder *cxlrd;
1590 	struct cxl_switch_decoder *cxlsd;
1591 	struct cxl_decoder *cxld;
1592 	int rc;
1593 
1594 	if (!is_cxl_root(port))
1595 		return ERR_PTR(-EINVAL);
1596 
1597 	cxlrd = kzalloc(struct_size(cxlrd, cxlsd.target, nr_targets),
1598 			GFP_KERNEL);
1599 	if (!cxlrd)
1600 		return ERR_PTR(-ENOMEM);
1601 
1602 	cxlsd = &cxlrd->cxlsd;
1603 	rc = cxl_switch_decoder_init(port, cxlsd, nr_targets);
1604 	if (rc) {
1605 		kfree(cxlrd);
1606 		return ERR_PTR(rc);
1607 	}
1608 
1609 	cxlrd->calc_hb = calc_hb;
1610 	mutex_init(&cxlrd->range_lock);
1611 
1612 	cxld = &cxlsd->cxld;
1613 	cxld->dev.type = &cxl_decoder_root_type;
1614 	/*
1615 	 * cxl_root_decoder_release() special cases negative ids to
1616 	 * detect memregion_alloc() failures.
1617 	 */
1618 	atomic_set(&cxlrd->region_id, -1);
1619 	rc = memregion_alloc(GFP_KERNEL);
1620 	if (rc < 0) {
1621 		put_device(&cxld->dev);
1622 		return ERR_PTR(rc);
1623 	}
1624 
1625 	atomic_set(&cxlrd->region_id, rc);
1626 	return cxlrd;
1627 }
1628 EXPORT_SYMBOL_NS_GPL(cxl_root_decoder_alloc, CXL);
1629 
1630 /**
1631  * cxl_switch_decoder_alloc - Allocate a switch level decoder
1632  * @port: owning CXL switch port of this decoder
1633  * @nr_targets: max number of dynamically addressable downstream targets
1634  *
1635  * Return: A new cxl decoder to be registered by cxl_decoder_add(). A
1636  * 'switch' decoder is any decoder that can be enumerated by PCIe
1637  * topology and the HDM Decoder Capability. This includes the decoders
1638  * that sit between Switch Upstream Ports / Switch Downstream Ports and
1639  * Host Bridges / Root Ports.
1640  */
1641 struct cxl_switch_decoder *cxl_switch_decoder_alloc(struct cxl_port *port,
1642 						    unsigned int nr_targets)
1643 {
1644 	struct cxl_switch_decoder *cxlsd;
1645 	struct cxl_decoder *cxld;
1646 	int rc;
1647 
1648 	if (is_cxl_root(port) || is_cxl_endpoint(port))
1649 		return ERR_PTR(-EINVAL);
1650 
1651 	cxlsd = kzalloc(struct_size(cxlsd, target, nr_targets), GFP_KERNEL);
1652 	if (!cxlsd)
1653 		return ERR_PTR(-ENOMEM);
1654 
1655 	rc = cxl_switch_decoder_init(port, cxlsd, nr_targets);
1656 	if (rc) {
1657 		kfree(cxlsd);
1658 		return ERR_PTR(rc);
1659 	}
1660 
1661 	cxld = &cxlsd->cxld;
1662 	cxld->dev.type = &cxl_decoder_switch_type;
1663 	return cxlsd;
1664 }
1665 EXPORT_SYMBOL_NS_GPL(cxl_switch_decoder_alloc, CXL);
1666 
1667 /**
1668  * cxl_endpoint_decoder_alloc - Allocate an endpoint decoder
1669  * @port: owning port of this decoder
1670  *
1671  * Return: A new cxl decoder to be registered by cxl_decoder_add()
1672  */
1673 struct cxl_endpoint_decoder *cxl_endpoint_decoder_alloc(struct cxl_port *port)
1674 {
1675 	struct cxl_endpoint_decoder *cxled;
1676 	struct cxl_decoder *cxld;
1677 	int rc;
1678 
1679 	if (!is_cxl_endpoint(port))
1680 		return ERR_PTR(-EINVAL);
1681 
1682 	cxled = kzalloc(sizeof(*cxled), GFP_KERNEL);
1683 	if (!cxled)
1684 		return ERR_PTR(-ENOMEM);
1685 
1686 	cxled->pos = -1;
1687 	cxld = &cxled->cxld;
1688 	rc = cxl_decoder_init(port, cxld);
1689 	if (rc)	 {
1690 		kfree(cxled);
1691 		return ERR_PTR(rc);
1692 	}
1693 
1694 	cxld->dev.type = &cxl_decoder_endpoint_type;
1695 	return cxled;
1696 }
1697 EXPORT_SYMBOL_NS_GPL(cxl_endpoint_decoder_alloc, CXL);
1698 
1699 /**
1700  * cxl_decoder_add_locked - Add a decoder with targets
1701  * @cxld: The cxl decoder allocated by cxl_<type>_decoder_alloc()
1702  * @target_map: A list of downstream ports that this decoder can direct memory
1703  *              traffic to. These numbers should correspond with the port number
1704  *              in the PCIe Link Capabilities structure.
1705  *
1706  * Certain types of decoders may not have any targets. The main example of this
1707  * is an endpoint device. A more awkward example is a hostbridge whose root
1708  * ports get hot added (technically possible, though unlikely).
1709  *
1710  * This is the locked variant of cxl_decoder_add().
1711  *
1712  * Context: Process context. Expects the device lock of the port that owns the
1713  *	    @cxld to be held.
1714  *
1715  * Return: Negative error code if the decoder wasn't properly configured; else
1716  *	   returns 0.
1717  */
1718 int cxl_decoder_add_locked(struct cxl_decoder *cxld, int *target_map)
1719 {
1720 	struct cxl_port *port;
1721 	struct device *dev;
1722 	int rc;
1723 
1724 	if (WARN_ON_ONCE(!cxld))
1725 		return -EINVAL;
1726 
1727 	if (WARN_ON_ONCE(IS_ERR(cxld)))
1728 		return PTR_ERR(cxld);
1729 
1730 	if (cxld->interleave_ways < 1)
1731 		return -EINVAL;
1732 
1733 	dev = &cxld->dev;
1734 
1735 	port = to_cxl_port(cxld->dev.parent);
1736 	if (!is_endpoint_decoder(dev)) {
1737 		struct cxl_switch_decoder *cxlsd = to_cxl_switch_decoder(dev);
1738 
1739 		rc = decoder_populate_targets(cxlsd, port, target_map);
1740 		if (rc && (cxld->flags & CXL_DECODER_F_ENABLE)) {
1741 			dev_err(&port->dev,
1742 				"Failed to populate active decoder targets\n");
1743 			return rc;
1744 		}
1745 	}
1746 
1747 	rc = dev_set_name(dev, "decoder%d.%d", port->id, cxld->id);
1748 	if (rc)
1749 		return rc;
1750 
1751 	return device_add(dev);
1752 }
1753 EXPORT_SYMBOL_NS_GPL(cxl_decoder_add_locked, CXL);
1754 
1755 /**
1756  * cxl_decoder_add - Add a decoder with targets
1757  * @cxld: The cxl decoder allocated by cxl_<type>_decoder_alloc()
1758  * @target_map: A list of downstream ports that this decoder can direct memory
1759  *              traffic to. These numbers should correspond with the port number
1760  *              in the PCIe Link Capabilities structure.
1761  *
1762  * This is the unlocked variant of cxl_decoder_add_locked().
1763  * See cxl_decoder_add_locked().
1764  *
1765  * Context: Process context. Takes and releases the device lock of the port that
1766  *	    owns the @cxld.
1767  */
1768 int cxl_decoder_add(struct cxl_decoder *cxld, int *target_map)
1769 {
1770 	struct cxl_port *port;
1771 	int rc;
1772 
1773 	if (WARN_ON_ONCE(!cxld))
1774 		return -EINVAL;
1775 
1776 	if (WARN_ON_ONCE(IS_ERR(cxld)))
1777 		return PTR_ERR(cxld);
1778 
1779 	port = to_cxl_port(cxld->dev.parent);
1780 
1781 	device_lock(&port->dev);
1782 	rc = cxl_decoder_add_locked(cxld, target_map);
1783 	device_unlock(&port->dev);
1784 
1785 	return rc;
1786 }
1787 EXPORT_SYMBOL_NS_GPL(cxl_decoder_add, CXL);
1788 
1789 static void cxld_unregister(void *dev)
1790 {
1791 	struct cxl_endpoint_decoder *cxled;
1792 
1793 	if (is_endpoint_decoder(dev)) {
1794 		cxled = to_cxl_endpoint_decoder(dev);
1795 		cxl_decoder_kill_region(cxled);
1796 	}
1797 
1798 	device_unregister(dev);
1799 }
1800 
1801 int cxl_decoder_autoremove(struct device *host, struct cxl_decoder *cxld)
1802 {
1803 	return devm_add_action_or_reset(host, cxld_unregister, &cxld->dev);
1804 }
1805 EXPORT_SYMBOL_NS_GPL(cxl_decoder_autoremove, CXL);
1806 
1807 /**
1808  * __cxl_driver_register - register a driver for the cxl bus
1809  * @cxl_drv: cxl driver structure to attach
1810  * @owner: owning module/driver
1811  * @modname: KBUILD_MODNAME for parent driver
1812  */
1813 int __cxl_driver_register(struct cxl_driver *cxl_drv, struct module *owner,
1814 			  const char *modname)
1815 {
1816 	if (!cxl_drv->probe) {
1817 		pr_debug("%s ->probe() must be specified\n", modname);
1818 		return -EINVAL;
1819 	}
1820 
1821 	if (!cxl_drv->name) {
1822 		pr_debug("%s ->name must be specified\n", modname);
1823 		return -EINVAL;
1824 	}
1825 
1826 	if (!cxl_drv->id) {
1827 		pr_debug("%s ->id must be specified\n", modname);
1828 		return -EINVAL;
1829 	}
1830 
1831 	cxl_drv->drv.bus = &cxl_bus_type;
1832 	cxl_drv->drv.owner = owner;
1833 	cxl_drv->drv.mod_name = modname;
1834 	cxl_drv->drv.name = cxl_drv->name;
1835 
1836 	return driver_register(&cxl_drv->drv);
1837 }
1838 EXPORT_SYMBOL_NS_GPL(__cxl_driver_register, CXL);
1839 
1840 void cxl_driver_unregister(struct cxl_driver *cxl_drv)
1841 {
1842 	driver_unregister(&cxl_drv->drv);
1843 }
1844 EXPORT_SYMBOL_NS_GPL(cxl_driver_unregister, CXL);
1845 
1846 static int cxl_bus_uevent(const struct device *dev, struct kobj_uevent_env *env)
1847 {
1848 	return add_uevent_var(env, "MODALIAS=" CXL_MODALIAS_FMT,
1849 			      cxl_device_id(dev));
1850 }
1851 
1852 static int cxl_bus_match(struct device *dev, struct device_driver *drv)
1853 {
1854 	return cxl_device_id(dev) == to_cxl_drv(drv)->id;
1855 }
1856 
1857 static int cxl_bus_probe(struct device *dev)
1858 {
1859 	int rc;
1860 
1861 	rc = to_cxl_drv(dev->driver)->probe(dev);
1862 	dev_dbg(dev, "probe: %d\n", rc);
1863 	return rc;
1864 }
1865 
1866 static void cxl_bus_remove(struct device *dev)
1867 {
1868 	struct cxl_driver *cxl_drv = to_cxl_drv(dev->driver);
1869 
1870 	if (cxl_drv->remove)
1871 		cxl_drv->remove(dev);
1872 }
1873 
1874 static struct workqueue_struct *cxl_bus_wq;
1875 
1876 static void cxl_bus_rescan_queue(struct work_struct *w)
1877 {
1878 	int rc = bus_rescan_devices(&cxl_bus_type);
1879 
1880 	pr_debug("CXL bus rescan result: %d\n", rc);
1881 }
1882 
1883 void cxl_bus_rescan(void)
1884 {
1885 	static DECLARE_WORK(rescan_work, cxl_bus_rescan_queue);
1886 
1887 	queue_work(cxl_bus_wq, &rescan_work);
1888 }
1889 EXPORT_SYMBOL_NS_GPL(cxl_bus_rescan, CXL);
1890 
1891 void cxl_bus_drain(void)
1892 {
1893 	drain_workqueue(cxl_bus_wq);
1894 }
1895 EXPORT_SYMBOL_NS_GPL(cxl_bus_drain, CXL);
1896 
1897 bool schedule_cxl_memdev_detach(struct cxl_memdev *cxlmd)
1898 {
1899 	return queue_work(cxl_bus_wq, &cxlmd->detach_work);
1900 }
1901 EXPORT_SYMBOL_NS_GPL(schedule_cxl_memdev_detach, CXL);
1902 
1903 /* for user tooling to ensure port disable work has completed */
1904 static ssize_t flush_store(const struct bus_type *bus, const char *buf, size_t count)
1905 {
1906 	if (sysfs_streq(buf, "1")) {
1907 		flush_workqueue(cxl_bus_wq);
1908 		return count;
1909 	}
1910 
1911 	return -EINVAL;
1912 }
1913 
1914 static BUS_ATTR_WO(flush);
1915 
1916 static struct attribute *cxl_bus_attributes[] = {
1917 	&bus_attr_flush.attr,
1918 	NULL,
1919 };
1920 
1921 static struct attribute_group cxl_bus_attribute_group = {
1922 	.attrs = cxl_bus_attributes,
1923 };
1924 
1925 static const struct attribute_group *cxl_bus_attribute_groups[] = {
1926 	&cxl_bus_attribute_group,
1927 	NULL,
1928 };
1929 
1930 struct bus_type cxl_bus_type = {
1931 	.name = "cxl",
1932 	.uevent = cxl_bus_uevent,
1933 	.match = cxl_bus_match,
1934 	.probe = cxl_bus_probe,
1935 	.remove = cxl_bus_remove,
1936 	.bus_groups = cxl_bus_attribute_groups,
1937 };
1938 EXPORT_SYMBOL_NS_GPL(cxl_bus_type, CXL);
1939 
1940 static struct dentry *cxl_debugfs;
1941 
1942 struct dentry *cxl_debugfs_create_dir(const char *dir)
1943 {
1944 	return debugfs_create_dir(dir, cxl_debugfs);
1945 }
1946 EXPORT_SYMBOL_NS_GPL(cxl_debugfs_create_dir, CXL);
1947 
1948 static __init int cxl_core_init(void)
1949 {
1950 	int rc;
1951 
1952 	cxl_debugfs = debugfs_create_dir("cxl", NULL);
1953 
1954 	cxl_mbox_init();
1955 
1956 	rc = cxl_memdev_init();
1957 	if (rc)
1958 		return rc;
1959 
1960 	cxl_bus_wq = alloc_ordered_workqueue("cxl_port", 0);
1961 	if (!cxl_bus_wq) {
1962 		rc = -ENOMEM;
1963 		goto err_wq;
1964 	}
1965 
1966 	rc = bus_register(&cxl_bus_type);
1967 	if (rc)
1968 		goto err_bus;
1969 
1970 	rc = cxl_region_init();
1971 	if (rc)
1972 		goto err_region;
1973 
1974 	return 0;
1975 
1976 err_region:
1977 	bus_unregister(&cxl_bus_type);
1978 err_bus:
1979 	destroy_workqueue(cxl_bus_wq);
1980 err_wq:
1981 	cxl_memdev_exit();
1982 	return rc;
1983 }
1984 
1985 static void cxl_core_exit(void)
1986 {
1987 	cxl_region_exit();
1988 	bus_unregister(&cxl_bus_type);
1989 	destroy_workqueue(cxl_bus_wq);
1990 	cxl_memdev_exit();
1991 	debugfs_remove_recursive(cxl_debugfs);
1992 }
1993 
1994 subsys_initcall(cxl_core_init);
1995 module_exit(cxl_core_exit);
1996 MODULE_LICENSE("GPL v2");
1997