Merge tag 'pci-v6.16-fixes-3' of git://git.kernel.org/pub/scm/linux/kernel/git/pci/pci
[linux-block.git] / drivers / cxl / port.c
CommitLineData
54cdbf84
BW
1// SPDX-License-Identifier: GPL-2.0-only
2/* Copyright(c) 2022 Intel Corporation. All rights reserved. */
3#include <linux/device.h>
4#include <linux/module.h>
5#include <linux/slab.h>
6
7#include "cxlmem.h"
8#include "cxlpci.h"
9
10/**
11 * DOC: cxl port
12 *
13 * The port driver enumerates dport via PCI and scans for HDM
14 * (Host-managed-Device-Memory) decoder resources via the
15 * @component_reg_phys value passed in by the agent that registered the
16 * port. All descendant ports of a CXL root port (described by platform
17 * firmware) are managed in this drivers context. Each driver instance
18 * is responsible for tearing down the driver context of immediate
19 * descendant ports. The locking for this is validated by
20 * CONFIG_PROVE_CXL_LOCKING.
21 *
22 * The primary service this driver provides is presenting APIs to other
23 * drivers to utilize the decoders, and indicating to userspace (via bind
24 * status) the connectivity of the CXL.mem protocol throughout the
25 * PCIe topology.
26 */
27
8dd2bc0f
BW
28static void schedule_detach(void *cxlmd)
29{
30 schedule_cxl_memdev_detach(cxlmd);
31}
32
5ed826fc 33static int discover_region(struct device *dev, void *unused)
a32320b7
DW
34{
35 struct cxl_endpoint_decoder *cxled;
36 int rc;
37
38 if (!is_endpoint_decoder(dev))
39 return 0;
40
41 cxled = to_cxl_endpoint_decoder(dev);
42 if ((cxled->cxld.flags & CXL_DECODER_F_ENABLE) == 0)
43 return 0;
44
45 if (cxled->state != CXL_DECODER_STATE_AUTO)
46 return 0;
47
48 /*
49 * Region enumeration is opportunistic, if this add-event fails,
50 * continue to the next endpoint decoder.
51 */
5ed826fc 52 rc = cxl_add_to_region(cxled);
a32320b7
DW
53 if (rc)
54 dev_dbg(dev, "failed to add to region: %#llx-%#llx\n",
55 cxled->cxld.hpa_range.start, cxled->cxld.hpa_range.end);
56
57 return 0;
58}
59
32ce3f18 60static int cxl_switch_port_probe(struct cxl_port *port)
54cdbf84 61{
54cdbf84 62 struct cxl_hdm *cxlhdm;
8f0220af 63 int rc;
fcfbc93c 64
8358e8f1
DJ
65 /* Cache the data early to ensure is_visible() works */
66 read_cdat_data(port);
67
8f0220af
DW
68 rc = devm_cxl_port_enumerate_dports(port);
69 if (rc < 0)
eb0764b8
DW
70 return rc;
71
80aa780d
DJ
72 cxl_switch_parse_cdat(port);
73
8f0220af 74 cxlhdm = devm_cxl_setup_hdm(port, NULL);
7bba261e
DW
75 if (!IS_ERR(cxlhdm))
76 return devm_cxl_enumerate_decoders(cxlhdm, NULL);
77
78 if (PTR_ERR(cxlhdm) != -ENODEV) {
79 dev_err(&port->dev, "Failed to map HDM decoder capability\n");
fcfbc93c 80 return PTR_ERR(cxlhdm);
7bba261e
DW
81 }
82
8f0220af 83 if (rc == 1) {
7bba261e
DW
84 dev_dbg(&port->dev, "Fallback to passthrough decoder\n");
85 return devm_cxl_add_passthrough_decoder(port);
86 }
fcfbc93c 87
7bba261e
DW
88 dev_err(&port->dev, "HDM decoder capability not found\n");
89 return -ENXIO;
32ce3f18 90}
8dd2bc0f 91
32ce3f18
DW
92static int cxl_endpoint_port_probe(struct cxl_port *port)
93{
b70c2cf9 94 struct cxl_endpoint_dvsec_info info = { .port = port };
7481653d 95 struct cxl_memdev *cxlmd = to_cxl_memdev(port->uport_dev);
32ce3f18
DW
96 struct cxl_dev_state *cxlds = cxlmd->cxlds;
97 struct cxl_hdm *cxlhdm;
98 int rc;
c9700604 99
2f84d072 100 rc = cxl_dvsec_rr_decode(cxlds, &info);
a5fcd228
DW
101 if (rc < 0)
102 return rc;
fcfbc93c 103
4474ce56 104 cxlhdm = devm_cxl_setup_hdm(port, &info);
f1d0525e
RR
105 if (IS_ERR(cxlhdm)) {
106 if (PTR_ERR(cxlhdm) == -ENODEV)
107 dev_err(&port->dev, "HDM decoder registers not found\n");
32ce3f18 108 return PTR_ERR(cxlhdm);
f1d0525e 109 }
5e5f4ad5 110
32ce3f18
DW
111 /* Cache the data early to ensure is_visible() works */
112 read_cdat_data(port);
ad6f04c0 113 cxl_endpoint_parse_cdat(port);
5e5f4ad5 114
32ce3f18
DW
115 get_device(&cxlmd->dev);
116 rc = devm_add_action_or_reset(&port->dev, schedule_detach, cxlmd);
117 if (rc)
118 return rc;
8dd2bc0f 119
a5fcd228 120 rc = cxl_hdm_decode_init(cxlds, cxlhdm, &info);
32ce3f18
DW
121 if (rc)
122 return rc;
123
b777e9be 124 rc = devm_cxl_enumerate_decoders(cxlhdm, &info);
a32320b7
DW
125 if (rc)
126 return rc;
127
a32320b7
DW
128 /*
129 * Now that all endpoint decoders are successfully enumerated, try to
130 * assemble regions from committed decoders
131 */
5ed826fc 132 device_for_each_child(&port->dev, NULL, discover_region);
a32320b7
DW
133
134 return 0;
32ce3f18
DW
135}
136
137static int cxl_port_probe(struct device *dev)
138{
139 struct cxl_port *port = to_cxl_port(dev);
140
141 if (is_cxl_endpoint(port))
142 return cxl_endpoint_port_probe(port);
143 return cxl_switch_port_probe(port);
54cdbf84
BW
144}
145
c9700604 146static ssize_t CDAT_read(struct file *filp, struct kobject *kobj,
7de24e20 147 const struct bin_attribute *bin_attr, char *buf,
c9700604
IW
148 loff_t offset, size_t count)
149{
150 struct device *dev = kobj_to_dev(kobj);
151 struct cxl_port *port = to_cxl_port(dev);
152
153 if (!port->cdat_available)
154 return -ENXIO;
155
156 if (!port->cdat.table)
157 return 0;
158
159 return memory_read_from_buffer(buf, count, &offset,
160 port->cdat.table,
161 port->cdat.length);
162}
163
7de24e20 164static const BIN_ATTR_ADMIN_RO(CDAT, 0);
c9700604
IW
165
166static umode_t cxl_port_bin_attr_is_visible(struct kobject *kobj,
b626816f 167 const struct bin_attribute *attr, int i)
c9700604
IW
168{
169 struct device *dev = kobj_to_dev(kobj);
170 struct cxl_port *port = to_cxl_port(dev);
171
172 if ((attr == &bin_attr_CDAT) && port->cdat_available)
173 return attr->attr.mode;
174
175 return 0;
176}
177
7de24e20 178static const struct bin_attribute *const cxl_cdat_bin_attributes[] = {
c9700604
IW
179 &bin_attr_CDAT,
180 NULL,
181};
182
7de24e20
TW
183static const struct attribute_group cxl_cdat_attribute_group = {
184 .bin_attrs_new = cxl_cdat_bin_attributes,
c9700604
IW
185 .is_bin_visible = cxl_port_bin_attr_is_visible,
186};
187
188static const struct attribute_group *cxl_port_attribute_groups[] = {
189 &cxl_cdat_attribute_group,
190 NULL,
191};
192
54cdbf84
BW
193static struct cxl_driver cxl_port_driver = {
194 .name = "cxl_port",
195 .probe = cxl_port_probe,
196 .id = CXL_DEVICE_PORT,
c9700604
IW
197 .drv = {
198 .dev_groups = cxl_port_attribute_groups,
199 },
54cdbf84
BW
200};
201
6575b268
DW
202static int __init cxl_port_init(void)
203{
204 return cxl_driver_register(&cxl_port_driver);
205}
206/*
207 * Be ready to immediately enable ports emitted by the platform CXL root
208 * (e.g. cxl_acpi) when CONFIG_CXL_PORT=y.
209 */
210subsys_initcall(cxl_port_init);
211
212static void __exit cxl_port_exit(void)
213{
214 cxl_driver_unregister(&cxl_port_driver);
215}
216module_exit(cxl_port_exit);
217
a0caa197 218MODULE_DESCRIPTION("CXL: Port enumeration and services");
54cdbf84 219MODULE_LICENSE("GPL v2");
cdd30ebb 220MODULE_IMPORT_NS("CXL");
54cdbf84 221MODULE_ALIAS_CXL(CXL_DEVICE_PORT);