cxl/region: Fix 'distance' calculation with passthrough ports

When programming port decode targets, the algorithm wants to ensure that
two devices are compatible to be programmed as peers beneath a given
port. A compatible peer is a target that shares the same dport, and
where that target's interleave position also routes it to the same
dport. Compatibility is determined by the device's interleave position
being >= to distance. For example, if a given dport can only map every
Nth position then positions less than N away from the last target
programmed are incompatible.

The @distance for the host-bridge's cxl_port in a simple dual-ported
host-bridge configuration with 2 direct-attached devices is 1, i.e. An
x2 region divided by 2 dports to reach 2 region targets.

An x4 region under an x2 host-bridge would need 2 intervening switches
where the @distance at the host bridge level is 2 (x4 region divided by
2 switches to reach 4 devices).

However, the distance between peers underneath a single ported
host-bridge is always zero because there is no limit to the number of
devices that can be mapped. In other words, there are no decoders to
program in a passthrough, all descendants are mapped and distance only
starts matters for the intervening descendant ports of the passthrough
port.

Add tracking for the number of dports mapped to a port, and use that to
detect the passthrough case for calculating @distance.

Cc: <stable@vger.kernel.org>
Reported-by: Bobo WL <lmw.bobo@gmail.com>
Reported-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
Link: http://lore.kernel.org/r/20221010172057.00001559@huawei.com
Fixes: 27b3f8d13830 ("cxl/region: Program target lists")
Reviewed-by: Vishal Verma <vishal.l.verma@intel.com>
Link: https://lore.kernel.org/r/166752185440.947915.6617495912508299445.stgit@dwillia2-xfh.jf.intel.com
Signed-off-by: Dan Williams <dan.j.williams@intel.com>

Changed files
+19 -3
drivers
cxl
+9 -2
drivers/cxl/core/port.c
··· 811 811 static int add_dport(struct cxl_port *port, struct cxl_dport *new) 812 812 { 813 813 struct cxl_dport *dup; 814 + int rc; 814 815 815 816 device_lock_assert(&port->dev); 816 817 dup = find_dport(port, new->port_id); ··· 822 821 dev_name(dup->dport)); 823 822 return -EBUSY; 824 823 } 825 - return xa_insert(&port->dports, (unsigned long)new->dport, new, 826 - GFP_KERNEL); 824 + 825 + rc = xa_insert(&port->dports, (unsigned long)new->dport, new, 826 + GFP_KERNEL); 827 + if (rc) 828 + return rc; 829 + 830 + port->nr_dports++; 831 + return 0; 827 832 } 828 833 829 834 /*
+8 -1
drivers/cxl/core/region.c
··· 990 990 if (cxl_rr->nr_targets_set) { 991 991 int i, distance; 992 992 993 - distance = p->nr_targets / cxl_rr->nr_targets; 993 + /* 994 + * Passthrough ports impose no distance requirements between 995 + * peers 996 + */ 997 + if (port->nr_dports == 1) 998 + distance = 0; 999 + else 1000 + distance = p->nr_targets / cxl_rr->nr_targets; 994 1001 for (i = 0; i < cxl_rr->nr_targets_set; i++) 995 1002 if (ep->dport == cxlsd->target[i]) { 996 1003 rc = check_last_peer(cxled, ep, cxl_rr,
+2
drivers/cxl/cxl.h
··· 457 457 * @regions: cxl_region_ref instances, regions mapped by this port 458 458 * @parent_dport: dport that points to this port in the parent 459 459 * @decoder_ida: allocator for decoder ids 460 + * @nr_dports: number of entries in @dports 460 461 * @hdm_end: track last allocated HDM decoder instance for allocation ordering 461 462 * @commit_end: cursor to track highest committed decoder for commit ordering 462 463 * @component_reg_phys: component register capability base address (optional) ··· 476 475 struct xarray regions; 477 476 struct cxl_dport *parent_dport; 478 477 struct ida decoder_ida; 478 + int nr_dports; 479 479 int hdm_end; 480 480 int commit_end; 481 481 resource_size_t component_reg_phys;