Index: head/sys/arm64/cavium/thunder_pcie_fdt.c =================================================================== --- head/sys/arm64/cavium/thunder_pcie_fdt.c (revision 294997) +++ head/sys/arm64/cavium/thunder_pcie_fdt.c (revision 294998) @@ -1,382 +1,382 @@ /* * Copyright (C) 2016 Cavium Inc. * All rights reserved. * * Developed by Semihalf. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #include "opt_platform.h" #include __FBSDID("$FreeBSD$"); #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "thunder_pcie_common.h" #define OFW_CELL_TO_UINT64(cell) \ (((uint64_t)(*(cell)) << 32) | (uint64_t)(*((cell) + 1))) #define SPACE_CODE_SHIFT 24 #define SPACE_CODE_MASK 0x3 #define SPACE_CODE_IO_SPACE 0x1 #define PROPS_CELL_SIZE 1 #define PCI_ADDR_CELL_SIZE 2 static int thunder_pcie_fdt_probe(device_t); static int thunder_pcie_fdt_attach(device_t); static struct resource * thunder_pcie_ofw_bus_alloc_res(device_t, device_t, int, int *, rman_res_t, rman_res_t, rman_res_t, u_int); static int thunder_pcie_ofw_bus_rel_res(device_t, device_t, int, int, struct resource *); static const struct ofw_bus_devinfo *thunder_pcie_ofw_get_devinfo(device_t, device_t); static device_method_t thunder_pcie_fdt_methods[] = { /* Device interface */ DEVMETHOD(device_probe, thunder_pcie_fdt_probe), DEVMETHOD(device_attach, thunder_pcie_fdt_attach), /* Bus interface */ DEVMETHOD(bus_alloc_resource, thunder_pcie_ofw_bus_alloc_res), DEVMETHOD(bus_release_resource, thunder_pcie_ofw_bus_rel_res), DEVMETHOD(bus_activate_resource, bus_generic_activate_resource), /* ofw_bus interface */ DEVMETHOD(ofw_bus_get_devinfo, thunder_pcie_ofw_get_devinfo), DEVMETHOD(ofw_bus_get_compat, ofw_bus_gen_get_compat), DEVMETHOD(ofw_bus_get_model, ofw_bus_gen_get_model), DEVMETHOD(ofw_bus_get_name, ofw_bus_gen_get_name), DEVMETHOD(ofw_bus_get_node, ofw_bus_gen_get_node), DEVMETHOD(ofw_bus_get_type, ofw_bus_gen_get_type), /* End */ DEVMETHOD_END }; DEFINE_CLASS_1(pcib, thunder_pcie_fdt_driver, thunder_pcie_fdt_methods, sizeof(struct thunder_pcie_softc), thunder_pcie_driver); static devclass_t thunder_pcie_fdt_devclass; DRIVER_MODULE(thunder_pcib, simplebus, thunder_pcie_fdt_driver, thunder_pcie_fdt_devclass, 0, 0); DRIVER_MODULE(thunder_pcib, ofwbus, thunder_pcie_fdt_driver, thunder_pcie_fdt_devclass, 0, 0); static int thunder_pcie_fdt_ranges(device_t); static int thunder_pcie_ofw_bus_attach(device_t); static int thunder_pcie_fdt_probe(device_t dev) { if (!ofw_bus_status_okay(dev)) return (ENXIO); if (ofw_bus_is_compatible(dev, "cavium,thunder-pcie") || ofw_bus_is_compatible(dev, "cavium,pci-host-thunder-ecam")) { device_set_desc(dev, "Cavium Integrated PCI/PCI-E Controller"); return (BUS_PROBE_DEFAULT); } return (ENXIO); } static int thunder_pcie_fdt_attach(device_t dev) { int err; /* Retrieve 'ranges' property from FDT */ if (thunder_pcie_fdt_ranges(dev) != 0) return (ENXIO); err = thunder_pcie_ofw_bus_attach(dev); if (err != 0) return (err); return (thunder_pcie_attach(dev)); } static __inline void get_addr_size_cells(phandle_t node, pcell_t *addr_cells, pcell_t *size_cells) { *addr_cells = 2; /* Find address cells if present */ OF_getencprop(node, "#address-cells", addr_cells, sizeof(*addr_cells)); *size_cells = 2; /* Find size cells if present */ OF_getencprop(node, "#size-cells", size_cells, sizeof(*size_cells)); } static int thunder_pcie_fdt_ranges(device_t dev) { struct thunder_pcie_softc *sc; phandle_t node; pcell_t pci_addr_cells, parent_addr_cells, size_cells; pcell_t attributes; pcell_t *ranges_buf, *cell_ptr; int cells_count, tuples_count; int tuple; int rv; sc = device_get_softc(dev); node = ofw_bus_get_node(dev); get_addr_size_cells(node, &pci_addr_cells, &size_cells); /* Find parent address cells if present */ if (OF_getencprop(OF_parent(node), "#address-cells", &parent_addr_cells, sizeof(parent_addr_cells)) < sizeof(parent_addr_cells)) parent_addr_cells = 2; /* Check if FDT format matches driver requirements */ if ((parent_addr_cells != 2) || (pci_addr_cells != 3) || (size_cells != 2)) { device_printf(dev, "Unexpected number of address or size cells in FDT " " %d:%d:%d\n", parent_addr_cells, pci_addr_cells, size_cells); return (ENXIO); } cells_count = OF_getencprop_alloc(node, "ranges", sizeof(pcell_t), (void **)&ranges_buf); if (cells_count == -1) { device_printf(dev, "Error parsing FDT 'ranges' property\n"); return (ENXIO); } tuples_count = cells_count / (pci_addr_cells + parent_addr_cells + size_cells); if (tuples_count > RANGES_TUPLES_MAX) { device_printf(dev, "Unexpected number of 'ranges' tuples in FDT\n"); rv = ENXIO; goto out; } cell_ptr = ranges_buf; for (tuple = 0; tuple < tuples_count; tuple++) { /* * TUPLE FORMAT: * attributes - 32-bit attributes field * PCI address - bus address combined of two cells in * a following format: * * PA address - physical address combined of two cells in * a following format: * * size - range size combined of two cells in * a following format: * */ attributes = *cell_ptr; attributes = (attributes >> SPACE_CODE_SHIFT) & SPACE_CODE_MASK; if (attributes == SPACE_CODE_IO_SPACE) { /* Internal PCIe does not support IO space, ignore. */ sc->ranges[tuple].phys_base = 0; sc->ranges[tuple].size = 0; cell_ptr += (pci_addr_cells + parent_addr_cells + size_cells); continue; } cell_ptr += PROPS_CELL_SIZE; sc->ranges[tuple].pci_base = OFW_CELL_TO_UINT64(cell_ptr); cell_ptr += PCI_ADDR_CELL_SIZE; sc->ranges[tuple].phys_base = OFW_CELL_TO_UINT64(cell_ptr); cell_ptr += parent_addr_cells; sc->ranges[tuple].size = OFW_CELL_TO_UINT64(cell_ptr); cell_ptr += size_cells; if (bootverbose) { device_printf(dev, "\tPCI addr: 0x%jx, CPU addr: 0x%jx, Size: 0x%jx\n", sc->ranges[tuple].pci_base, sc->ranges[tuple].phys_base, sc->ranges[tuple].size); } } for (; tuple < RANGES_TUPLES_MAX; tuple++) { /* zero-fill remaining tuples to mark empty elements in array */ sc->ranges[tuple].phys_base = 0; sc->ranges[tuple].size = 0; } rv = 0; out: free(ranges_buf, M_OFWPROP); return (rv); } /* OFW bus interface */ struct thunder_pcie_ofw_devinfo { struct ofw_bus_devinfo di_dinfo; struct resource_list di_rl; }; static const struct ofw_bus_devinfo * thunder_pcie_ofw_get_devinfo(device_t bus __unused, device_t child) { struct thunder_pcie_ofw_devinfo *di; di = device_get_ivars(child); return (&di->di_dinfo); } static struct resource * thunder_pcie_ofw_bus_alloc_res(device_t bus, device_t child, int type, int *rid, rman_res_t start, rman_res_t end, rman_res_t count, u_int flags) { struct thunder_pcie_softc *sc; struct thunder_pcie_ofw_devinfo *di; struct resource_list_entry *rle; int i; /* For PCIe devices that do not have FDT nodes, use PCIB method */ - if (ofw_bus_get_node(child) == 0) { + if ((int)ofw_bus_get_node(child) <= 0) { return (thunder_pcie_alloc_resource(bus, child, type, rid, start, end, count, flags)); } sc = device_get_softc(bus); if ((start == 0UL) && (end == ~0UL)) { if ((di = device_get_ivars(child)) == NULL) return (NULL); if (type == SYS_RES_IOPORT) type = SYS_RES_MEMORY; /* Find defaults for this rid */ rle = resource_list_find(&di->di_rl, type, *rid); if (rle == NULL) return (NULL); start = rle->start; end = rle->end; count = rle->count; } if (type == SYS_RES_MEMORY) { /* Remap through ranges property */ for (i = 0; i < RANGES_TUPLES_MAX; i++) { if (start >= sc->ranges[i].phys_base && end < sc->ranges[i].pci_base + sc->ranges[i].size) { start -= sc->ranges[i].phys_base; start += sc->ranges[i].pci_base; end -= sc->ranges[i].phys_base; end += sc->ranges[i].pci_base; break; } } if (i == RANGES_TUPLES_MAX) { device_printf(bus, "Could not map resource " "%#lx-%#lx\n", start, end); return (NULL); } } return (bus_generic_alloc_resource(bus, child, type, rid, start, end, count, flags)); } static int thunder_pcie_ofw_bus_rel_res(device_t bus, device_t child, int type, int rid, struct resource *res) { /* For PCIe devices that do not have FDT nodes, use PCIB method */ - if (ofw_bus_get_node(child) == 0) { + if ((int)ofw_bus_get_node(child) <= 0) { return (thunder_pcie_release_resource(bus, child, type, rid, res)); } return (bus_generic_release_resource(bus, child, type, rid, res)); } /* Helper functions */ static int thunder_pcie_ofw_bus_attach(device_t dev) { struct thunder_pcie_ofw_devinfo *di; device_t child; phandle_t parent, node; pcell_t addr_cells, size_cells; parent = ofw_bus_get_node(dev); if (parent > 0) { get_addr_size_cells(parent, &addr_cells, &size_cells); /* Iterate through all bus subordinates */ for (node = OF_child(parent); node > 0; node = OF_peer(node)) { /* Allocate and populate devinfo. */ di = malloc(sizeof(*di), M_THUNDER_PCIE, M_WAITOK | M_ZERO); if (ofw_bus_gen_setup_devinfo(&di->di_dinfo, node) != 0) { free(di, M_THUNDER_PCIE); continue; } /* Initialize and populate resource list. */ resource_list_init(&di->di_rl); ofw_bus_reg_to_rl(dev, node, addr_cells, size_cells, &di->di_rl); ofw_bus_intr_to_rl(dev, node, &di->di_rl, NULL); /* Add newbus device for this FDT node */ child = device_add_child(dev, NULL, -1); if (child == NULL) { resource_list_free(&di->di_rl); ofw_bus_gen_destroy_devinfo(&di->di_dinfo); free(di, M_THUNDER_PCIE); continue; } device_set_ivars(child, di); } } return (0); } Index: head/sys/dev/vnic/thunder_bgx_fdt.c =================================================================== --- head/sys/dev/vnic/thunder_bgx_fdt.c (revision 294997) +++ head/sys/dev/vnic/thunder_bgx_fdt.c (revision 294998) @@ -1,337 +1,337 @@ /*- * Copyright (c) 2015 The FreeBSD Foundation * All rights reserved. * * This software was developed by Semihalf under * the sponsorship of the FreeBSD Foundation. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #include __FBSDID("$FreeBSD$"); #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "thunder_bgx.h" #include "thunder_bgx_var.h" #define CONN_TYPE_MAXLEN 16 #define CONN_TYPE_OFFSET 2 #define BGX_NODE_NAME "bgx" #define BGX_MAXID 9 #define FDT_NAME_MAXLEN 31 int bgx_fdt_init_phy(struct bgx *); static void bgx_fdt_get_macaddr(phandle_t phy, uint8_t *hwaddr) { uint8_t addr[ETHER_ADDR_LEN]; if (OF_getprop(phy, "local-mac-address", addr, ETHER_ADDR_LEN) == -1) { /* Missing MAC address should be marked by clearing it */ memset(hwaddr, 0, ETHER_ADDR_LEN); } else memcpy(hwaddr, addr, ETHER_ADDR_LEN); } static boolean_t bgx_fdt_phy_mode_match(struct bgx *bgx, char *qlm_mode, size_t size) { size -= CONN_TYPE_OFFSET; switch (bgx->qlm_mode) { case QLM_MODE_SGMII: if (strncmp(&qlm_mode[CONN_TYPE_OFFSET], "sgmii", size) == 0) return (TRUE); break; case QLM_MODE_XAUI_1X4: if (strncmp(&qlm_mode[CONN_TYPE_OFFSET], "xaui", size) == 0) return (TRUE); if (strncmp(&qlm_mode[CONN_TYPE_OFFSET], "dxaui", size) == 0) return (TRUE); break; case QLM_MODE_RXAUI_2X2: if (strncmp(&qlm_mode[CONN_TYPE_OFFSET], "raui", size) == 0) return (TRUE); break; case QLM_MODE_XFI_4X1: if (strncmp(&qlm_mode[CONN_TYPE_OFFSET], "xfi", size) == 0) return (TRUE); break; case QLM_MODE_XLAUI_1X4: if (strncmp(&qlm_mode[CONN_TYPE_OFFSET], "xlaui", size) == 0) return (TRUE); break; case QLM_MODE_10G_KR_4X1: if (strncmp(&qlm_mode[CONN_TYPE_OFFSET], "xfi-10g-kr", size) == 0) return (TRUE); break; case QLM_MODE_40G_KR4_1X4: if (strncmp(&qlm_mode[CONN_TYPE_OFFSET], "xlaui-40g-kr", size) == 0) return (TRUE); break; default: return (FALSE); } return (FALSE); } static phandle_t bgx_fdt_traverse_nodes(phandle_t start, char *name, size_t len) { phandle_t node, ret; size_t buf_size; char *node_name; int err; buf_size = sizeof(*node_name) * FDT_NAME_MAXLEN; if (len > buf_size) { /* * This is an erroneous situation since the string * to compare cannot be longer than FDT_NAME_MAXLEN. */ return (0); } node_name = malloc(buf_size, M_BGX, M_WAITOK); for (node = OF_child(start); node != 0; node = OF_peer(node)) { /* Clean-up the buffer */ memset(node_name, 0, buf_size); /* Recurse to children */ if (OF_child(node) != 0) { ret = bgx_fdt_traverse_nodes(node, name, len); if (ret != 0) { free(node_name, M_BGX); return (ret); } } err = OF_getprop(node, "name", node_name, FDT_NAME_MAXLEN); if ((err > 0) && (strncmp(node_name, name, len) == 0)) { free(node_name, M_BGX); return (node); } } free(node_name, M_BGX); return (0); } /* * Similar functionality to pci_find_pcie_root_port() * but this one works for ThunderX. */ static device_t bgx_find_root_pcib(device_t dev) { devclass_t pci_class; device_t pcib, bus; pci_class = devclass_find("pci"); KASSERT(device_get_devclass(device_get_parent(dev)) == pci_class, ("%s: non-pci device %s", __func__, device_get_nameunit(dev))); /* Walk the bridge hierarchy until we find a non-PCI device */ for (;;) { bus = device_get_parent(dev); KASSERT(bus != NULL, ("%s: null parent of %s", __func__, device_get_nameunit(dev))); if (device_get_devclass(bus) != pci_class) return (NULL); pcib = device_get_parent(bus); KASSERT(pcib != NULL, ("%s: null bridge of %s", __func__, device_get_nameunit(bus))); /* * If the parent of this PCIB is not PCI * then we found our root PCIB. */ if (device_get_devclass(device_get_parent(pcib)) != pci_class) return (pcib); dev = pcib; } } static __inline phandle_t bgx_fdt_find_node(struct bgx *bgx) { device_t root_pcib; phandle_t node; char *bgx_sel; size_t len; KASSERT(bgx->bgx_id <= BGX_MAXID, ("Invalid BGX ID: %d, max: %d", bgx->bgx_id, BGX_MAXID)); len = sizeof(BGX_NODE_NAME) + 1; /* ++<\0> */ /* Allocate memory for BGX node name + "/" character */ bgx_sel = malloc(sizeof(*bgx_sel) * (len + 1), M_BGX, M_ZERO | M_WAITOK); /* Prepare node's name */ snprintf(bgx_sel, len + 1, "/"BGX_NODE_NAME"%d", bgx->bgx_id); /* First try the root node */ node = OF_finddevice(bgx_sel); if ((int)node > 0) { /* Found relevant node */ goto out; } /* * Clean-up and try to find BGX in DT * starting from the parent PCI bridge node. */ memset(bgx_sel, 0, sizeof(*bgx_sel) * (len + 1)); snprintf(bgx_sel, len, BGX_NODE_NAME"%d", bgx->bgx_id); /* Find PCI bridge that we are connected to */ root_pcib = bgx_find_root_pcib(bgx->dev); if (root_pcib == NULL) { device_printf(bgx->dev, "Unable to find BGX root bridge\n"); node = 0; goto out; } node = ofw_bus_get_node(root_pcib); - if (node == 0) { + if ((int)node <= 0) { device_printf(bgx->dev, "No parent FDT node for BGX\n"); goto out; } node = bgx_fdt_traverse_nodes(node, bgx_sel, len); out: free(bgx_sel, M_BGX); return (node); } int bgx_fdt_init_phy(struct bgx *bgx) { phandle_t node, child; phandle_t phy, mdio; uint8_t lmac; char qlm_mode[CONN_TYPE_MAXLEN]; node = bgx_fdt_find_node(bgx); if (node == 0) { device_printf(bgx->dev, "Could not find bgx%d node in FDT\n", bgx->bgx_id); return (ENXIO); } lmac = 0; for (child = OF_child(node); child > 0; child = OF_peer(child)) { if (OF_getprop(child, "qlm-mode", qlm_mode, sizeof(qlm_mode)) <= 0) { /* Missing qlm-mode, skipping */ continue; } if (!bgx_fdt_phy_mode_match(bgx, qlm_mode, sizeof(qlm_mode))) { /* * Connection type not match with BGX mode. */ continue; } if (OF_getencprop(child, "phy-handle", &phy, sizeof(phy)) <= 0) { if (bootverbose) { device_printf(bgx->dev, "No phy-handle in PHY node. Skipping...\n"); } continue; } /* Acquire PHY address */ phy = OF_node_from_xref(phy); if (OF_getencprop(phy, "reg", &bgx->lmac[lmac].phyaddr, sizeof(bgx->lmac[lmac].phyaddr)) <= 0) { if (bootverbose) { device_printf(bgx->dev, "Could not retrieve PHY address\n"); } bgx->lmac[lmac].phyaddr = MII_PHY_ANY; } /* * Get PHY interface (MDIO bus) device. * Driver must be already attached. */ mdio = OF_parent(phy); bgx->lmac[lmac].phy_if_dev = OF_device_from_xref(OF_xref_from_node(mdio)); if (bgx->lmac[lmac].phy_if_dev == NULL) { if (bootverbose) { device_printf(bgx->dev, "Could not find interface to PHY\n"); } continue; } /* Get mac address from FDT */ bgx_fdt_get_macaddr(phy, bgx->lmac[lmac].mac); bgx->lmac[lmac].lmacid = lmac; lmac++; if (lmac == MAX_LMAC_PER_BGX) break; } if (lmac == 0) { device_printf(bgx->dev, "Could not find matching PHY\n"); return (ENXIO); } return (0); }