2 * Copyright (c) 2015 The FreeBSD Foundation
5 * This software was developed by Semihalf under
6 * the sponsorship of the FreeBSD Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/bitset.h>
36 #include <sys/bitstring.h>
38 #include <sys/endian.h>
39 #include <sys/kernel.h>
40 #include <sys/malloc.h>
41 #include <sys/module.h>
43 #include <sys/pciio.h>
46 #include <sys/socket.h>
47 #include <sys/cpuset.h>
49 #include <sys/mutex.h>
51 #include <net/ethernet.h>
53 #include <net/if_media.h>
55 #include <dev/ofw/openfirm.h>
56 #include <dev/ofw/ofw_bus.h>
57 #include <dev/mii/miivar.h>
59 #include "thunder_bgx.h"
60 #include "thunder_bgx_var.h"
62 #define CONN_TYPE_MAXLEN 16
63 #define CONN_TYPE_OFFSET 2
65 #define BGX_NODE_NAME "bgx"
68 #define FDT_NAME_MAXLEN 31
70 int bgx_fdt_init_phy(struct bgx *);
73 bgx_fdt_get_macaddr(phandle_t phy, uint8_t *hwaddr)
75 uint8_t addr[ETHER_ADDR_LEN];
77 if (OF_getprop(phy, "local-mac-address", addr, ETHER_ADDR_LEN) == -1) {
78 /* Missing MAC address should be marked by clearing it */
79 memset(hwaddr, 0, ETHER_ADDR_LEN);
81 memcpy(hwaddr, addr, ETHER_ADDR_LEN);
85 bgx_fdt_phy_mode_match(struct bgx *bgx, char *qlm_mode, size_t size)
88 size -= CONN_TYPE_OFFSET;
90 switch (bgx->qlm_mode) {
92 if (strncmp(&qlm_mode[CONN_TYPE_OFFSET], "sgmii", size) == 0)
95 case QLM_MODE_XAUI_1X4:
96 if (strncmp(&qlm_mode[CONN_TYPE_OFFSET], "xaui", size) == 0)
98 if (strncmp(&qlm_mode[CONN_TYPE_OFFSET], "dxaui", size) == 0)
101 case QLM_MODE_RXAUI_2X2:
102 if (strncmp(&qlm_mode[CONN_TYPE_OFFSET], "raui", size) == 0)
105 case QLM_MODE_XFI_4X1:
106 if (strncmp(&qlm_mode[CONN_TYPE_OFFSET], "xfi", size) == 0)
109 case QLM_MODE_XLAUI_1X4:
110 if (strncmp(&qlm_mode[CONN_TYPE_OFFSET], "xlaui", size) == 0)
113 case QLM_MODE_10G_KR_4X1:
114 if (strncmp(&qlm_mode[CONN_TYPE_OFFSET], "xfi-10g-kr", size) == 0)
117 case QLM_MODE_40G_KR4_1X4:
118 if (strncmp(&qlm_mode[CONN_TYPE_OFFSET], "xlaui-40g-kr", size) == 0)
129 bgx_fdt_traverse_nodes(phandle_t start, char *name, size_t len)
136 buf_size = sizeof(*node_name) * FDT_NAME_MAXLEN;
137 if (len > buf_size) {
139 * This is an erroneous situation since the string
140 * to compare cannot be longer than FDT_NAME_MAXLEN.
145 node_name = malloc(buf_size, M_BGX, M_WAITOK);
146 for (node = OF_child(start); node != 0; node = OF_peer(node)) {
147 /* Clean-up the buffer */
148 memset(node_name, 0, buf_size);
149 /* Recurse to children */
150 if (OF_child(node) != 0) {
151 ret = bgx_fdt_traverse_nodes(node, name, len);
153 free(node_name, M_BGX);
157 err = OF_getprop(node, "name", node_name, FDT_NAME_MAXLEN);
158 if ((err > 0) && (strncmp(node_name, name, len) == 0)) {
159 free(node_name, M_BGX);
163 free(node_name, M_BGX);
169 * Similar functionality to pci_find_pcie_root_port()
170 * but this one works for ThunderX.
173 bgx_find_root_pcib(device_t dev)
175 devclass_t pci_class;
178 pci_class = devclass_find("pci");
179 KASSERT(device_get_devclass(device_get_parent(dev)) == pci_class,
180 ("%s: non-pci device %s", __func__, device_get_nameunit(dev)));
182 /* Walk the bridge hierarchy until we find a non-PCI device */
184 bus = device_get_parent(dev);
185 KASSERT(bus != NULL, ("%s: null parent of %s", __func__,
186 device_get_nameunit(dev)));
188 if (device_get_devclass(bus) != pci_class)
191 pcib = device_get_parent(bus);
192 KASSERT(pcib != NULL, ("%s: null bridge of %s", __func__,
193 device_get_nameunit(bus)));
196 * If the parent of this PCIB is not PCI
197 * then we found our root PCIB.
199 if (device_get_devclass(device_get_parent(pcib)) != pci_class)
206 static __inline phandle_t
207 bgx_fdt_find_node(struct bgx *bgx)
214 KASSERT(bgx->bgx_id <= BGX_MAXID,
215 ("Invalid BGX ID: %d, max: %d", bgx->bgx_id, BGX_MAXID));
217 len = sizeof(BGX_NODE_NAME) + 1; /* <bgx_name>+<digit>+<\0> */
218 /* Allocate memory for BGX node name + "/" character */
219 bgx_sel = malloc(sizeof(*bgx_sel) * (len + 1), M_BGX,
222 /* Prepare node's name */
223 snprintf(bgx_sel, len + 1, "/"BGX_NODE_NAME"%d", bgx->bgx_id);
224 /* First try the root node */
225 node = OF_finddevice(bgx_sel);
227 /* Found relevant node */
231 * Clean-up and try to find BGX in DT
232 * starting from the parent PCI bridge node.
234 memset(bgx_sel, 0, sizeof(*bgx_sel) * (len + 1));
235 snprintf(bgx_sel, len, BGX_NODE_NAME"%d", bgx->bgx_id);
237 /* Find PCI bridge that we are connected to */
239 root_pcib = bgx_find_root_pcib(bgx->dev);
240 if (root_pcib == NULL) {
241 device_printf(bgx->dev, "Unable to find BGX root bridge\n");
246 node = ofw_bus_get_node(root_pcib);
247 if ((int)node <= 0) {
248 device_printf(bgx->dev, "No parent FDT node for BGX\n");
252 node = bgx_fdt_traverse_nodes(node, bgx_sel, len);
254 free(bgx_sel, M_BGX);
259 bgx_fdt_init_phy(struct bgx *bgx)
261 phandle_t node, child;
264 char qlm_mode[CONN_TYPE_MAXLEN];
266 node = bgx_fdt_find_node(bgx);
268 device_printf(bgx->dev,
269 "Could not find bgx%d node in FDT\n", bgx->bgx_id);
274 for (child = OF_child(node); child > 0; child = OF_peer(child)) {
275 if (OF_getprop(child, "qlm-mode", qlm_mode,
276 sizeof(qlm_mode)) <= 0) {
277 /* Missing qlm-mode, skipping */
281 if (!bgx_fdt_phy_mode_match(bgx, qlm_mode, sizeof(qlm_mode))) {
283 * Connection type not match with BGX mode.
289 /* Acquire PHY address */
290 if (OF_getencprop(child, "reg", &bgx->lmac[lmac].phyaddr,
291 sizeof(bgx->lmac[lmac].phyaddr)) <= 0) {
293 device_printf(bgx->dev,
294 "Could not retrieve PHY address\n");
296 bgx->lmac[lmac].phyaddr = MII_PHY_ANY;
299 if (OF_getencprop(child, "phy-handle", &phy,
302 device_printf(bgx->dev,
303 "No phy-handle in PHY node. Skipping...\n");
307 phy = OF_instance_to_package(phy);
309 * Get PHY interface (MDIO bus) device.
310 * Driver must be already attached.
312 mdio = OF_parent(phy);
313 bgx->lmac[lmac].phy_if_dev =
314 OF_device_from_xref(OF_xref_from_node(mdio));
315 if (bgx->lmac[lmac].phy_if_dev == NULL) {
317 device_printf(bgx->dev,
318 "Could not find interface to PHY\n");
323 /* Get mac address from FDT */
324 bgx_fdt_get_macaddr(child, bgx->lmac[lmac].mac);
326 bgx->lmac[lmac].lmacid = lmac;
328 if (lmac == MAX_LMAC_PER_BGX)
332 device_printf(bgx->dev, "Could not find matching PHY\n");