xref: /freebsd/sys/dev/vnic/thunder_bgx_fdt.c (revision 1f4bcc459a76b7aa664f3fd557684cd0ba6da352)
1 /*-
2  * Copyright (c) 2015 The FreeBSD Foundation
3  * All rights reserved.
4  *
5  * This software was developed by Semihalf under
6  * the sponsorship of the FreeBSD Foundation.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
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.
16  *
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
27  * SUCH DAMAGE.
28  */
29 
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32 
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/bitset.h>
36 #include <sys/bitstring.h>
37 #include <sys/bus.h>
38 #include <sys/endian.h>
39 #include <sys/kernel.h>
40 #include <sys/malloc.h>
41 #include <sys/module.h>
42 #include <sys/rman.h>
43 #include <sys/pciio.h>
44 #include <sys/pcpu.h>
45 #include <sys/proc.h>
46 #include <sys/socket.h>
47 #include <sys/cpuset.h>
48 #include <sys/lock.h>
49 #include <sys/mutex.h>
50 
51 #include <net/ethernet.h>
52 #include <net/if.h>
53 #include <net/if_media.h>
54 
55 #include <dev/ofw/openfirm.h>
56 #include <dev/ofw/ofw_bus.h>
57 #include <dev/mii/miivar.h>
58 
59 #include "thunder_bgx.h"
60 #include "thunder_bgx_var.h"
61 
62 #define	CONN_TYPE_MAXLEN	16
63 #define	CONN_TYPE_OFFSET	2
64 
65 #define	BGX_NODE_NAME		"bgx"
66 #define	BGX_MAXID		9
67 
68 #define	FDT_NAME_MAXLEN		31
69 
70 int bgx_fdt_init_phy(struct bgx *);
71 
72 static void
73 bgx_fdt_get_macaddr(phandle_t phy, uint8_t *hwaddr)
74 {
75 	uint8_t addr[ETHER_ADDR_LEN];
76 
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);
80 	} else
81 		memcpy(hwaddr, addr, ETHER_ADDR_LEN);
82 }
83 
84 static boolean_t
85 bgx_fdt_phy_mode_match(struct bgx *bgx, char *qlm_mode, size_t size)
86 {
87 
88 	size -= CONN_TYPE_OFFSET;
89 
90 	switch (bgx->qlm_mode) {
91 	case QLM_MODE_SGMII:
92 		if (strncmp(&qlm_mode[CONN_TYPE_OFFSET], "sgmii", size) == 0)
93 			return (TRUE);
94 		break;
95 	case QLM_MODE_XAUI_1X4:
96 		if (strncmp(&qlm_mode[CONN_TYPE_OFFSET], "xaui", size) == 0)
97 			return (TRUE);
98 		if (strncmp(&qlm_mode[CONN_TYPE_OFFSET], "dxaui", size) == 0)
99 			return (TRUE);
100 		break;
101 	case QLM_MODE_RXAUI_2X2:
102 		if (strncmp(&qlm_mode[CONN_TYPE_OFFSET], "raui", size) == 0)
103 			return (TRUE);
104 		break;
105 	case QLM_MODE_XFI_4X1:
106 		if (strncmp(&qlm_mode[CONN_TYPE_OFFSET], "xfi", size) == 0)
107 			return (TRUE);
108 		break;
109 	case QLM_MODE_XLAUI_1X4:
110 		if (strncmp(&qlm_mode[CONN_TYPE_OFFSET], "xlaui", size) == 0)
111 			return (TRUE);
112 		break;
113 	case QLM_MODE_10G_KR_4X1:
114 		if (strncmp(&qlm_mode[CONN_TYPE_OFFSET], "xfi-10g-kr", size) == 0)
115 			return (TRUE);
116 		break;
117 	case QLM_MODE_40G_KR4_1X4:
118 		if (strncmp(&qlm_mode[CONN_TYPE_OFFSET], "xlaui-40g-kr", size) == 0)
119 			return (TRUE);
120 		break;
121 	default:
122 		return (FALSE);
123 	}
124 
125 	return (FALSE);
126 }
127 
128 static phandle_t
129 bgx_fdt_traverse_nodes(phandle_t start, char *name, size_t len)
130 {
131 	phandle_t node, ret;
132 	size_t buf_size;
133 	char *node_name;
134 	int err;
135 
136 	buf_size = sizeof(*node_name) * FDT_NAME_MAXLEN;
137 	if (len > buf_size) {
138 		/*
139 		 * This is an erroneous situation since the string
140 		 * to compare cannot be longer than FDT_NAME_MAXLEN.
141 		 */
142 		return (0);
143 	}
144 
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);
152 			if (ret != 0) {
153 				free(node_name, M_BGX);
154 				return (ret);
155 			}
156 		}
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);
160 			return (node);
161 		}
162 	}
163 	free(node_name, M_BGX);
164 
165 	return (0);
166 }
167 
168 /*
169  * Similar functionality to pci_find_pcie_root_port()
170  * but this one works for ThunderX.
171  */
172 static device_t
173 bgx_find_root_pcib(device_t dev)
174 {
175 	devclass_t pci_class;
176 	device_t pcib, bus;
177 
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)));
181 
182 	/* Walk the bridge hierarchy until we find a non-PCI device */
183 	for (;;) {
184 		bus = device_get_parent(dev);
185 		KASSERT(bus != NULL, ("%s: null parent of %s", __func__,
186 		    device_get_nameunit(dev)));
187 
188 		if (device_get_devclass(bus) != pci_class)
189 			return (NULL);
190 
191 		pcib = device_get_parent(bus);
192 		KASSERT(pcib != NULL, ("%s: null bridge of %s", __func__,
193 		    device_get_nameunit(bus)));
194 
195 		/*
196 		 * If the parent of this PCIB is not PCI
197 		 * then we found our root PCIB.
198 		 */
199 		if (device_get_devclass(device_get_parent(pcib)) != pci_class)
200 			return (pcib);
201 
202 		dev = pcib;
203 	}
204 }
205 
206 static __inline phandle_t
207 bgx_fdt_find_node(struct bgx *bgx)
208 {
209 	device_t root_pcib;
210 	phandle_t node;
211 	char *bgx_sel;
212 	size_t len;
213 
214 	KASSERT(bgx->bgx_id <= BGX_MAXID,
215 	    ("Invalid BGX ID: %d, max: %d", bgx->bgx_id, BGX_MAXID));
216 
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,
220 	    M_ZERO | M_WAITOK);
221 
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);
226 	if ((int)node > 0) {
227 		/* Found relevant node */
228 		goto out;
229 	}
230 	/*
231 	 * Clean-up and try to find BGX in DT
232 	 * starting from the parent PCI bridge node.
233 	 */
234 	memset(bgx_sel, 0, sizeof(*bgx_sel) * (len + 1));
235 	snprintf(bgx_sel, len, BGX_NODE_NAME"%d", bgx->bgx_id);
236 
237 	/* Find PCI bridge that we are connected to */
238 
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");
242 		node = 0;
243 		goto out;
244 	}
245 
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");
249 		goto out;
250 	}
251 
252 	node = bgx_fdt_traverse_nodes(node, bgx_sel, len);
253 out:
254 	free(bgx_sel, M_BGX);
255 	return (node);
256 }
257 
258 int
259 bgx_fdt_init_phy(struct bgx *bgx)
260 {
261 	phandle_t node, child;
262 	phandle_t phy, mdio;
263 	uint8_t lmac;
264 	char qlm_mode[CONN_TYPE_MAXLEN];
265 
266 	node = bgx_fdt_find_node(bgx);
267 	if (node == 0) {
268 		device_printf(bgx->dev,
269 		    "Could not find bgx%d node in FDT\n", bgx->bgx_id);
270 		return (ENXIO);
271 	}
272 
273 	lmac = 0;
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 */
278 			continue;
279 		}
280 
281 		if (!bgx_fdt_phy_mode_match(bgx, qlm_mode, sizeof(qlm_mode))) {
282 			/*
283 			 * Connection type not match with BGX mode.
284 			 */
285 			continue;
286 		}
287 
288 		if (OF_getencprop(child, "phy-handle", &phy,
289 		    sizeof(phy)) <= 0) {
290 			if (bootverbose) {
291 				device_printf(bgx->dev,
292 				    "No phy-handle in PHY node. Skipping...\n");
293 			}
294 			continue;
295 		}
296 
297 		/* Acquire PHY address */
298 		phy = OF_node_from_xref(phy);
299 		if (OF_getencprop(phy, "reg", &bgx->lmac[lmac].phyaddr,
300 		    sizeof(bgx->lmac[lmac].phyaddr)) <= 0) {
301 			if (bootverbose) {
302 				device_printf(bgx->dev,
303 				    "Could not retrieve PHY address\n");
304 			}
305 			bgx->lmac[lmac].phyaddr = MII_PHY_ANY;
306 		}
307 
308 		/*
309 		 * Get PHY interface (MDIO bus) device.
310 		 * Driver must be already attached.
311 		 */
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) {
316 			if (bootverbose) {
317 				device_printf(bgx->dev,
318 				    "Could not find interface to PHY\n");
319 			}
320 			continue;
321 		}
322 
323 		/* Get mac address from FDT */
324 		bgx_fdt_get_macaddr(phy, bgx->lmac[lmac].mac);
325 
326 		bgx->lmac[lmac].lmacid = lmac;
327 		lmac++;
328 		if (lmac == MAX_LMAC_PER_BGX)
329 			break;
330 	}
331 	if (lmac == 0) {
332 		device_printf(bgx->dev, "Could not find matching PHY\n");
333 		return (ENXIO);
334 	}
335 
336 	return (0);
337 }
338