]> git.itanic.dy.fi Git - linux-stable/blobdiff - drivers/cxl/port.c
Merge branch 'for-6.3/cxl-rr-emu' into cxl/next
[linux-stable] / drivers / cxl / port.c
index 9f9cc268b5978b840a9e98e204d37942e730c413..1049bb5ea496129177e3db06fae3b64198548483 100644 (file)
@@ -30,63 +30,116 @@ static void schedule_detach(void *cxlmd)
        schedule_cxl_memdev_detach(cxlmd);
 }
 
-static int cxl_port_probe(struct device *dev)
+static int discover_region(struct device *dev, void *root)
+{
+       struct cxl_endpoint_decoder *cxled;
+       int rc;
+
+       if (!is_endpoint_decoder(dev))
+               return 0;
+
+       cxled = to_cxl_endpoint_decoder(dev);
+       if ((cxled->cxld.flags & CXL_DECODER_F_ENABLE) == 0)
+               return 0;
+
+       if (cxled->state != CXL_DECODER_STATE_AUTO)
+               return 0;
+
+       /*
+        * Region enumeration is opportunistic, if this add-event fails,
+        * continue to the next endpoint decoder.
+        */
+       rc = cxl_add_to_region(root, cxled);
+       if (rc)
+               dev_dbg(dev, "failed to add to region: %#llx-%#llx\n",
+                       cxled->cxld.hpa_range.start, cxled->cxld.hpa_range.end);
+
+       return 0;
+}
+
+static int cxl_switch_port_probe(struct cxl_port *port)
 {
+       struct cxl_hdm *cxlhdm;
+       int rc;
+
+       rc = devm_cxl_port_enumerate_dports(port);
+       if (rc < 0)
+               return rc;
+
+       if (rc == 1)
+               return devm_cxl_add_passthrough_decoder(port);
+
+       cxlhdm = devm_cxl_setup_hdm(port, NULL);
+       if (IS_ERR(cxlhdm))
+               return PTR_ERR(cxlhdm);
+
+       return devm_cxl_enumerate_decoders(cxlhdm, NULL);
+}
+
+static int cxl_endpoint_port_probe(struct cxl_port *port)
+{
+       struct cxl_memdev *cxlmd = to_cxl_memdev(port->uport);
        struct cxl_endpoint_dvsec_info info = { 0 };
-       struct cxl_port *port = to_cxl_port(dev);
-       bool is_ep = is_cxl_endpoint(port);
-       struct cxl_dev_state *cxlds;
-       struct cxl_memdev *cxlmd;
+       struct cxl_dev_state *cxlds = cxlmd->cxlds;
        struct cxl_hdm *cxlhdm;
+       struct cxl_port *root;
        int rc;
 
-       if (is_ep) {
-               cxlmd = to_cxl_memdev(port->uport);
-               cxlds = cxlmd->cxlds;
-               rc = cxl_dvsec_rr_decode(cxlds->dev, cxlds->cxl_dvsec, &info);
-               if (rc < 0)
-                       return rc;
-       } else {
-               rc = devm_cxl_port_enumerate_dports(port);
-               if (rc < 0)
-                       return rc;
-               if (rc == 1)
-                       return devm_cxl_add_passthrough_decoder(port);
-       }
+       rc = cxl_dvsec_rr_decode(cxlds->dev, cxlds->cxl_dvsec, &info);
+       if (rc < 0)
+               return rc;
 
        cxlhdm = devm_cxl_setup_hdm(port, &info);
        if (IS_ERR(cxlhdm))
                return PTR_ERR(cxlhdm);
 
-       if (is_ep) {
-               /* Cache the data early to ensure is_visible() works */
-               read_cdat_data(port);
+       /* Cache the data early to ensure is_visible() works */
+       read_cdat_data(port);
 
-               get_device(&cxlmd->dev);
-               rc = devm_add_action_or_reset(dev, schedule_detach, cxlmd);
-               if (rc)
-                       return rc;
+       get_device(&cxlmd->dev);
+       rc = devm_add_action_or_reset(&port->dev, schedule_detach, cxlmd);
+       if (rc)
+               return rc;
 
-               rc = cxl_hdm_decode_init(cxlds, cxlhdm, &info);
-               if (rc)
-                       return rc;
+       rc = cxl_hdm_decode_init(cxlds, cxlhdm, &info);
+       if (rc)
+               return rc;
 
-               rc = cxl_await_media_ready(cxlds);
-               if (rc) {
-                       dev_err(dev, "Media not active (%d)\n", rc);
-                       return rc;
-               }
+       rc = cxl_await_media_ready(cxlds);
+       if (rc) {
+               dev_err(&port->dev, "Media not active (%d)\n", rc);
+               return rc;
        }
 
        rc = devm_cxl_enumerate_decoders(cxlhdm, &info);
-       if (rc) {
-               dev_err(dev, "Couldn't enumerate decoders (%d)\n", rc);
+       if (rc)
                return rc;
-       }
+
+       /*
+        * This can't fail in practice as CXL root exit unregisters all
+        * descendant ports and that in turn synchronizes with cxl_port_probe()
+        */
+       root = find_cxl_root(&cxlmd->dev);
+
+       /*
+        * Now that all endpoint decoders are successfully enumerated, try to
+        * assemble regions from committed decoders
+        */
+       device_for_each_child(&port->dev, root, discover_region);
+       put_device(&root->dev);
 
        return 0;
 }
 
+static int cxl_port_probe(struct device *dev)
+{
+       struct cxl_port *port = to_cxl_port(dev);
+
+       if (is_cxl_endpoint(port))
+               return cxl_endpoint_port_probe(port);
+       return cxl_switch_port_probe(port);
+}
+
 static ssize_t CDAT_read(struct file *filp, struct kobject *kobj,
                         struct bin_attribute *bin_attr, char *buf,
                         loff_t offset, size_t count)