xref: /freebsd/sys/dev/vnic/thunder_bgx_fdt.c (revision 55141f2c8991b2a6adbf30bb0fe3e6cbc303f06d)
1 /*-
2  * Copyright (c) 2015 The FreeBSD Foundation
3  *
4  * This software was developed by Semihalf under
5  * the sponsorship of the FreeBSD Foundation.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/bitset.h>
33 #include <sys/bitstring.h>
34 #include <sys/bus.h>
35 #include <sys/ctype.h>
36 #include <sys/endian.h>
37 #include <sys/kernel.h>
38 #include <sys/malloc.h>
39 #include <sys/module.h>
40 #include <sys/rman.h>
41 #include <sys/pciio.h>
42 #include <sys/pcpu.h>
43 #include <sys/proc.h>
44 #include <sys/socket.h>
45 #include <sys/cpuset.h>
46 #include <sys/lock.h>
47 #include <sys/mutex.h>
48 
49 #include <net/ethernet.h>
50 #include <net/if.h>
51 #include <net/if_media.h>
52 
53 #include <dev/ofw/openfirm.h>
54 #include <dev/ofw/ofw_bus.h>
55 #include <dev/mii/miivar.h>
56 
57 #include "thunder_bgx.h"
58 #include "thunder_bgx_var.h"
59 
60 #define	CONN_TYPE_MAXLEN	16
61 #define	CONN_TYPE_OFFSET	2
62 
63 #define	BGX_NODE_NAME		"bgx"
64 #define	BGX_MAXID		9
65 /* BGX func. 0, i.e.: reg = <0x8000 0 0 0 0>; DEVFN = 0x80 */
66 #define	BGX_DEVFN_0		0x80
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, ssize_t size)
86 {
87 	const char *type;
88 	ssize_t sz;
89 	ssize_t offset;
90 
91 	switch (bgx->qlm_mode) {
92 	case QLM_MODE_SGMII:
93 		type = "sgmii";
94 		sz = sizeof("sgmii");
95 		offset = size - sz;
96 		break;
97 	case QLM_MODE_XAUI_1X4:
98 		type = "xaui";
99 		sz = sizeof("xaui");
100 		offset = size - sz;
101 		if (offset < 0)
102 			return (FALSE);
103 		if (strncmp(&qlm_mode[offset], type, sz) == 0)
104 			return (TRUE);
105 		type = "dxaui";
106 		sz = sizeof("dxaui");
107 		offset = size - sz;
108 		break;
109 	case QLM_MODE_RXAUI_2X2:
110 		type = "raui";
111 		sz = sizeof("raui");
112 		offset = size - sz;
113 		break;
114 	case QLM_MODE_XFI_4X1:
115 		type = "xfi";
116 		sz = sizeof("xfi");
117 		offset = size - sz;
118 		break;
119 	case QLM_MODE_XLAUI_1X4:
120 		type = "xlaui";
121 		sz = sizeof("xlaui");
122 		offset = size - sz;
123 		break;
124 	case QLM_MODE_10G_KR_4X1:
125 		type = "xfi-10g-kr";
126 		sz = sizeof("xfi-10g-kr");
127 		offset = size - sz;
128 		break;
129 	case QLM_MODE_40G_KR4_1X4:
130 		type = "xlaui-40g-kr";
131 		sz = sizeof("xlaui-40g-kr");
132 		offset = size - sz;
133 		break;
134 	default:
135 		return (FALSE);
136 	}
137 
138 	if (offset < 0)
139 		return (FALSE);
140 
141 	if (strncmp(&qlm_mode[offset], type, sz) == 0)
142 		return (TRUE);
143 
144 	return (FALSE);
145 }
146 
147 static boolean_t
148 bgx_fdt_phy_name_match(struct bgx *bgx, char *phy_name, ssize_t size)
149 {
150 	const char *type;
151 	ssize_t sz;
152 	char last;
153 
154 	switch (bgx->qlm_mode) {
155 	case QLM_MODE_SGMII:
156 		type = "sgmii";
157 		sz = sizeof("sgmii");
158 		break;
159 	case QLM_MODE_XAUI_1X4:
160 		type = "xaui";
161 		sz = sizeof("xaui");
162 		if (sz < size)
163 			return (FALSE);
164 		if (strncmp(phy_name, type, sz) == 0)
165 			return (TRUE);
166 		type = "dxaui";
167 		sz = sizeof("dxaui");
168 		break;
169 	case QLM_MODE_RXAUI_2X2:
170 		type = "raui";
171 		sz = sizeof("raui");
172 		break;
173 	case QLM_MODE_XFI_4X1:
174 		type = "xfi";
175 		sz = sizeof("xfi");
176 		break;
177 	case QLM_MODE_XLAUI_1X4:
178 		type = "xlaui";
179 		sz = sizeof("xlaui");
180 		break;
181 	case QLM_MODE_10G_KR_4X1:
182 		type = "xfi-10g-kr";
183 		sz = sizeof("xfi-10g-kr");
184 		break;
185 	case QLM_MODE_40G_KR4_1X4:
186 		type = "xlaui-40g-kr";
187 		sz = sizeof("xlaui-40g-kr");
188 		break;
189 	default:
190 		return (FALSE);
191 	}
192 
193 	if (sz > size)
194 		return (FALSE);
195 	if (strncmp(phy_name, type, sz - 1) == 0) {
196 		last = phy_name[sz - 1];
197 		if (last == '\0' || last == '@' || isdigit(last))
198 			return (TRUE);
199 	}
200 	return (FALSE);
201 }
202 
203 static phandle_t
204 bgx_fdt_traverse_nodes(uint8_t unit, phandle_t start, char *name,
205     size_t len)
206 {
207 	phandle_t node, ret;
208 	uint32_t *reg;
209 	size_t buf_size;
210 	ssize_t proplen;
211 	char *node_name;
212 	int err;
213 
214 	/*
215 	 * Traverse all subordinate nodes of 'start' to find BGX instance.
216 	 * This supports both old (by name) and new (by reg) methods.
217 	 */
218 	buf_size = sizeof(*node_name) * FDT_NAME_MAXLEN;
219 	if (len > buf_size) {
220 		/*
221 		 * This is an erroneous situation since the string
222 		 * to compare cannot be longer than FDT_NAME_MAXLEN.
223 		 */
224 		return (0);
225 	}
226 
227 	node_name = malloc(buf_size, M_BGX, M_WAITOK);
228 	for (node = OF_child(start); node != 0; node = OF_peer(node)) {
229 		/* Clean-up the buffer */
230 		memset(node_name, 0, buf_size);
231 		/* Recurse to children */
232 		if (OF_child(node) != 0) {
233 			ret = bgx_fdt_traverse_nodes(unit, node, name, len);
234 			if (ret != 0) {
235 				free(node_name, M_BGX);
236 				return (ret);
237 			}
238 		}
239 		/*
240 		 * Old way - by name
241 		 */
242 		proplen = OF_getproplen(node, "name");
243 		if ((proplen <= 0) || (proplen < len))
244 			continue;
245 
246 		err = OF_getprop(node, "name", node_name, proplen);
247 		if (err <= 0)
248 			continue;
249 
250 		if (strncmp(node_name, name, len) == 0) {
251 			free(node_name, M_BGX);
252 			return (node);
253 		}
254 		/*
255 		 * New way - by reg
256 		 */
257 		/* Check if even BGX */
258 		if (strncmp(node_name,
259 		    BGX_NODE_NAME, sizeof(BGX_NODE_NAME) - 1) != 0)
260 			continue;
261 		/* Get reg */
262 		err = OF_getencprop_alloc_multi(node, "reg", sizeof(*reg),
263 		    (void **)&reg);
264 		if (err == -1) {
265 			free(reg, M_OFWPROP);
266 			continue;
267 		}
268 
269 		/* Match BGX device function */
270 		if ((BGX_DEVFN_0 + unit) == (reg[0] >> 8)) {
271 			free(reg, M_OFWPROP);
272 			free(node_name, M_BGX);
273 			return (node);
274 		}
275 		free(reg, M_OFWPROP);
276 	}
277 	free(node_name, M_BGX);
278 
279 	return (0);
280 }
281 
282 /*
283  * Similar functionality to pci_find_pcie_root_port()
284  * but this one works for ThunderX.
285  */
286 static device_t
287 bgx_find_root_pcib(device_t dev)
288 {
289 	devclass_t pci_class;
290 	device_t pcib, bus;
291 
292 	pci_class = devclass_find("pci");
293 	KASSERT(device_get_devclass(device_get_parent(dev)) == pci_class,
294 	    ("%s: non-pci device %s", __func__, device_get_nameunit(dev)));
295 
296 	/* Walk the bridge hierarchy until we find a non-PCI device */
297 	for (;;) {
298 		bus = device_get_parent(dev);
299 		KASSERT(bus != NULL, ("%s: null parent of %s", __func__,
300 		    device_get_nameunit(dev)));
301 
302 		if (device_get_devclass(bus) != pci_class)
303 			return (NULL);
304 
305 		pcib = device_get_parent(bus);
306 		KASSERT(pcib != NULL, ("%s: null bridge of %s", __func__,
307 		    device_get_nameunit(bus)));
308 
309 		/*
310 		 * If the parent of this PCIB is not PCI
311 		 * then we found our root PCIB.
312 		 */
313 		if (device_get_devclass(device_get_parent(pcib)) != pci_class)
314 			return (pcib);
315 
316 		dev = pcib;
317 	}
318 }
319 
320 static __inline phandle_t
321 bgx_fdt_find_node(struct bgx *bgx)
322 {
323 	device_t root_pcib;
324 	phandle_t node;
325 	char *bgx_sel;
326 	size_t len;
327 
328 	KASSERT(bgx->bgx_id <= BGX_MAXID,
329 	    ("Invalid BGX ID: %d, max: %d", bgx->bgx_id, BGX_MAXID));
330 
331 	len = sizeof(BGX_NODE_NAME) + 1; /* <bgx_name>+<digit>+<\0> */
332 	/* Allocate memory for BGX node name + "/" character */
333 	bgx_sel = malloc(sizeof(*bgx_sel) * (len + 1), M_BGX,
334 	    M_ZERO | M_WAITOK);
335 
336 	/* Prepare node's name */
337 	snprintf(bgx_sel, len + 1, "/"BGX_NODE_NAME"%d", bgx->bgx_id);
338 	/* First try the root node */
339 	node =  OF_finddevice(bgx_sel);
340 	if (node != -1) {
341 		/* Found relevant node */
342 		goto out;
343 	}
344 	/*
345 	 * Clean-up and try to find BGX in DT
346 	 * starting from the parent PCI bridge node.
347 	 */
348 	memset(bgx_sel, 0, sizeof(*bgx_sel) * (len + 1));
349 	snprintf(bgx_sel, len, BGX_NODE_NAME"%d", bgx->bgx_id);
350 
351 	/* Find PCI bridge that we are connected to */
352 
353 	root_pcib = bgx_find_root_pcib(bgx->dev);
354 	if (root_pcib == NULL) {
355 		device_printf(bgx->dev, "Unable to find BGX root bridge\n");
356 		node = 0;
357 		goto out;
358 	}
359 
360 	node = ofw_bus_get_node(root_pcib);
361 	if ((int)node <= 0) {
362 		device_printf(bgx->dev, "No parent FDT node for BGX\n");
363 		goto out;
364 	}
365 
366 	node = bgx_fdt_traverse_nodes(bgx->bgx_id, node, bgx_sel, len);
367 out:
368 	free(bgx_sel, M_BGX);
369 	return (node);
370 }
371 
372 int
373 bgx_fdt_init_phy(struct bgx *bgx)
374 {
375 	char *node_name;
376 	phandle_t node, child;
377 	phandle_t phy, mdio;
378 	ssize_t len;
379 	uint8_t lmac;
380 	char qlm_mode[CONN_TYPE_MAXLEN];
381 
382 	node = bgx_fdt_find_node(bgx);
383 	if (node == 0) {
384 		device_printf(bgx->dev,
385 		    "Could not find bgx%d node in FDT\n", bgx->bgx_id);
386 		return (ENXIO);
387 	}
388 
389 	lmac = 0;
390 	for (child = OF_child(node); child > 0; child = OF_peer(child)) {
391 		len = OF_getprop(child, "qlm-mode", qlm_mode, sizeof(qlm_mode));
392 		if (len > 0) {
393 			if (!bgx_fdt_phy_mode_match(bgx, qlm_mode, len)) {
394 				/*
395 				 * Connection type not match with BGX mode.
396 				 */
397 				continue;
398 			}
399 		} else {
400 			len = OF_getprop_alloc(child, "name",
401 			    (void **)&node_name);
402 			if (len <= 0) {
403 				continue;
404 			}
405 
406 			if (!bgx_fdt_phy_name_match(bgx, node_name, len)) {
407 				free(node_name, M_OFWPROP);
408 				continue;
409 			}
410 			free(node_name, M_OFWPROP);
411 		}
412 
413 		/* Acquire PHY address */
414 		if (OF_getencprop(child, "reg", &bgx->lmac[lmac].phyaddr,
415 		    sizeof(bgx->lmac[lmac].phyaddr)) <= 0) {
416 			if (bootverbose) {
417 				device_printf(bgx->dev,
418 				    "Could not retrieve PHY address\n");
419 			}
420 			bgx->lmac[lmac].phyaddr = MII_PHY_ANY;
421 		}
422 
423 		if (OF_getencprop(child, "phy-handle", &phy,
424 		    sizeof(phy)) <= 0) {
425 			if (bootverbose) {
426 				device_printf(bgx->dev,
427 				    "No phy-handle in PHY node. Skipping...\n");
428 			}
429 			continue;
430 		}
431 		phy = OF_instance_to_package(phy);
432 		/*
433 		 * Get PHY interface (MDIO bus) device.
434 		 * Driver must be already attached.
435 		 */
436 		mdio = OF_parent(phy);
437 		bgx->lmac[lmac].phy_if_dev =
438 		    OF_device_from_xref(OF_xref_from_node(mdio));
439 		if (bgx->lmac[lmac].phy_if_dev == NULL) {
440 			if (bootverbose) {
441 				device_printf(bgx->dev,
442 				    "Could not find interface to PHY\n");
443 			}
444 			continue;
445 		}
446 
447 		/* Get mac address from FDT */
448 		bgx_fdt_get_macaddr(child, bgx->lmac[lmac].mac);
449 
450 		bgx->lmac[lmac].lmacid = lmac;
451 		lmac++;
452 		if (lmac == MAX_LMAC_PER_BGX)
453 			break;
454 	}
455 	if (lmac == 0) {
456 		device_printf(bgx->dev, "Could not find matching PHY\n");
457 		return (ENXIO);
458 	}
459 
460 	return (0);
461 }
462