xref: /freebsd/sys/dev/vnic/thunder_bgx_fdt.c (revision ce6a89e27cd190313be39bb479880aeda4778436)
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/ctype.h>
39 #include <sys/endian.h>
40 #include <sys/kernel.h>
41 #include <sys/malloc.h>
42 #include <sys/module.h>
43 #include <sys/rman.h>
44 #include <sys/pciio.h>
45 #include <sys/pcpu.h>
46 #include <sys/proc.h>
47 #include <sys/socket.h>
48 #include <sys/cpuset.h>
49 #include <sys/lock.h>
50 #include <sys/mutex.h>
51 
52 #include <net/ethernet.h>
53 #include <net/if.h>
54 #include <net/if_media.h>
55 
56 #include <dev/ofw/openfirm.h>
57 #include <dev/ofw/ofw_bus.h>
58 #include <dev/mii/miivar.h>
59 
60 #include "thunder_bgx.h"
61 #include "thunder_bgx_var.h"
62 
63 #define	CONN_TYPE_MAXLEN	16
64 #define	CONN_TYPE_OFFSET	2
65 
66 #define	BGX_NODE_NAME		"bgx"
67 #define	BGX_MAXID		9
68 /* BGX func. 0, i.e.: reg = <0x8000 0 0 0 0>; DEVFN = 0x80 */
69 #define	BGX_DEVFN_0		0x80
70 
71 #define	FDT_NAME_MAXLEN		31
72 
73 int bgx_fdt_init_phy(struct bgx *);
74 
75 static void
76 bgx_fdt_get_macaddr(phandle_t phy, uint8_t *hwaddr)
77 {
78 	uint8_t addr[ETHER_ADDR_LEN];
79 
80 	if (OF_getprop(phy, "local-mac-address", addr, ETHER_ADDR_LEN) == -1) {
81 		/* Missing MAC address should be marked by clearing it */
82 		memset(hwaddr, 0, ETHER_ADDR_LEN);
83 	} else
84 		memcpy(hwaddr, addr, ETHER_ADDR_LEN);
85 }
86 
87 static boolean_t
88 bgx_fdt_phy_mode_match(struct bgx *bgx, char *qlm_mode, ssize_t size)
89 {
90 	const char *type;
91 	ssize_t sz;
92 	ssize_t offset;
93 
94 	switch (bgx->qlm_mode) {
95 	case QLM_MODE_SGMII:
96 		type = "sgmii";
97 		sz = sizeof("sgmii");
98 		offset = size - sz;
99 		break;
100 	case QLM_MODE_XAUI_1X4:
101 		type = "xaui";
102 		sz = sizeof("xaui");
103 		offset = size - sz;
104 		if (offset < 0)
105 			return (FALSE);
106 		if (strncmp(&qlm_mode[offset], type, sz) == 0)
107 			return (TRUE);
108 		type = "dxaui";
109 		sz = sizeof("dxaui");
110 		offset = size - sz;
111 		break;
112 	case QLM_MODE_RXAUI_2X2:
113 		type = "raui";
114 		sz = sizeof("raui");
115 		offset = size - sz;
116 		break;
117 	case QLM_MODE_XFI_4X1:
118 		type = "xfi";
119 		sz = sizeof("xfi");
120 		offset = size - sz;
121 		break;
122 	case QLM_MODE_XLAUI_1X4:
123 		type = "xlaui";
124 		sz = sizeof("xlaui");
125 		offset = size - sz;
126 		break;
127 	case QLM_MODE_10G_KR_4X1:
128 		type = "xfi-10g-kr";
129 		sz = sizeof("xfi-10g-kr");
130 		offset = size - sz;
131 		break;
132 	case QLM_MODE_40G_KR4_1X4:
133 		type = "xlaui-40g-kr";
134 		sz = sizeof("xlaui-40g-kr");
135 		offset = size - sz;
136 		break;
137 	default:
138 		return (FALSE);
139 	}
140 
141 	if (offset < 0)
142 		return (FALSE);
143 
144 	if (strncmp(&qlm_mode[offset], type, sz) == 0)
145 		return (TRUE);
146 
147 	return (FALSE);
148 }
149 
150 static boolean_t
151 bgx_fdt_phy_name_match(struct bgx *bgx, char *phy_name, ssize_t size)
152 {
153 	const char *type;
154 	ssize_t sz;
155 	char last;
156 
157 	switch (bgx->qlm_mode) {
158 	case QLM_MODE_SGMII:
159 		type = "sgmii";
160 		sz = sizeof("sgmii");
161 		break;
162 	case QLM_MODE_XAUI_1X4:
163 		type = "xaui";
164 		sz = sizeof("xaui");
165 		if (sz < size)
166 			return (FALSE);
167 		if (strncmp(phy_name, type, sz) == 0)
168 			return (TRUE);
169 		type = "dxaui";
170 		sz = sizeof("dxaui");
171 		break;
172 	case QLM_MODE_RXAUI_2X2:
173 		type = "raui";
174 		sz = sizeof("raui");
175 		break;
176 	case QLM_MODE_XFI_4X1:
177 		type = "xfi";
178 		sz = sizeof("xfi");
179 		break;
180 	case QLM_MODE_XLAUI_1X4:
181 		type = "xlaui";
182 		sz = sizeof("xlaui");
183 		break;
184 	case QLM_MODE_10G_KR_4X1:
185 		type = "xfi-10g-kr";
186 		sz = sizeof("xfi-10g-kr");
187 		break;
188 	case QLM_MODE_40G_KR4_1X4:
189 		type = "xlaui-40g-kr";
190 		sz = sizeof("xlaui-40g-kr");
191 		break;
192 	default:
193 		return (FALSE);
194 	}
195 
196 	if (sz > size)
197 		return (FALSE);
198 	if (strncmp(phy_name, type, sz - 1) == 0) {
199 		last = phy_name[sz - 1];
200 		if (last == '\0' || last == '@' || isdigit(last))
201 			return (TRUE);
202 	}
203 	return (FALSE);
204 }
205 
206 static phandle_t
207 bgx_fdt_traverse_nodes(uint8_t unit, phandle_t start, char *name,
208     size_t len)
209 {
210 	phandle_t node, ret;
211 	uint32_t *reg;
212 	size_t buf_size;
213 	ssize_t proplen;
214 	char *node_name;
215 	int err;
216 
217 	/*
218 	 * Traverse all subordinate nodes of 'start' to find BGX instance.
219 	 * This supports both old (by name) and new (by reg) methods.
220 	 */
221 	buf_size = sizeof(*node_name) * FDT_NAME_MAXLEN;
222 	if (len > buf_size) {
223 		/*
224 		 * This is an erroneous situation since the string
225 		 * to compare cannot be longer than FDT_NAME_MAXLEN.
226 		 */
227 		return (0);
228 	}
229 
230 	node_name = malloc(buf_size, M_BGX, M_WAITOK);
231 	for (node = OF_child(start); node != 0; node = OF_peer(node)) {
232 		/* Clean-up the buffer */
233 		memset(node_name, 0, buf_size);
234 		/* Recurse to children */
235 		if (OF_child(node) != 0) {
236 			ret = bgx_fdt_traverse_nodes(unit, node, name, len);
237 			if (ret != 0) {
238 				free(node_name, M_BGX);
239 				return (ret);
240 			}
241 		}
242 		/*
243 		 * Old way - by name
244 		 */
245 		proplen = OF_getproplen(node, "name");
246 		if ((proplen <= 0) || (proplen < len))
247 			continue;
248 
249 		err = OF_getprop(node, "name", node_name, proplen);
250 		if (err <= 0)
251 			continue;
252 
253 		if (strncmp(node_name, name, len) == 0) {
254 			free(node_name, M_BGX);
255 			return (node);
256 		}
257 		/*
258 		 * New way - by reg
259 		 */
260 		/* Check if even BGX */
261 		if (strncmp(node_name,
262 		    BGX_NODE_NAME, sizeof(BGX_NODE_NAME) - 1) != 0)
263 			continue;
264 		/* Get reg */
265 		err = OF_getencprop_alloc_multi(node, "reg", sizeof(*reg),
266 		    (void **)&reg);
267 		if (err == -1) {
268 			free(reg, M_OFWPROP);
269 			continue;
270 		}
271 
272 		/* Match BGX device function */
273 		if ((BGX_DEVFN_0 + unit) == (reg[0] >> 8)) {
274 			free(reg, M_OFWPROP);
275 			free(node_name, M_BGX);
276 			return (node);
277 		}
278 		free(reg, M_OFWPROP);
279 	}
280 	free(node_name, M_BGX);
281 
282 	return (0);
283 }
284 
285 /*
286  * Similar functionality to pci_find_pcie_root_port()
287  * but this one works for ThunderX.
288  */
289 static device_t
290 bgx_find_root_pcib(device_t dev)
291 {
292 	devclass_t pci_class;
293 	device_t pcib, bus;
294 
295 	pci_class = devclass_find("pci");
296 	KASSERT(device_get_devclass(device_get_parent(dev)) == pci_class,
297 	    ("%s: non-pci device %s", __func__, device_get_nameunit(dev)));
298 
299 	/* Walk the bridge hierarchy until we find a non-PCI device */
300 	for (;;) {
301 		bus = device_get_parent(dev);
302 		KASSERT(bus != NULL, ("%s: null parent of %s", __func__,
303 		    device_get_nameunit(dev)));
304 
305 		if (device_get_devclass(bus) != pci_class)
306 			return (NULL);
307 
308 		pcib = device_get_parent(bus);
309 		KASSERT(pcib != NULL, ("%s: null bridge of %s", __func__,
310 		    device_get_nameunit(bus)));
311 
312 		/*
313 		 * If the parent of this PCIB is not PCI
314 		 * then we found our root PCIB.
315 		 */
316 		if (device_get_devclass(device_get_parent(pcib)) != pci_class)
317 			return (pcib);
318 
319 		dev = pcib;
320 	}
321 }
322 
323 static __inline phandle_t
324 bgx_fdt_find_node(struct bgx *bgx)
325 {
326 	device_t root_pcib;
327 	phandle_t node;
328 	char *bgx_sel;
329 	size_t len;
330 
331 	KASSERT(bgx->bgx_id <= BGX_MAXID,
332 	    ("Invalid BGX ID: %d, max: %d", bgx->bgx_id, BGX_MAXID));
333 
334 	len = sizeof(BGX_NODE_NAME) + 1; /* <bgx_name>+<digit>+<\0> */
335 	/* Allocate memory for BGX node name + "/" character */
336 	bgx_sel = malloc(sizeof(*bgx_sel) * (len + 1), M_BGX,
337 	    M_ZERO | M_WAITOK);
338 
339 	/* Prepare node's name */
340 	snprintf(bgx_sel, len + 1, "/"BGX_NODE_NAME"%d", bgx->bgx_id);
341 	/* First try the root node */
342 	node =  OF_finddevice(bgx_sel);
343 	if (node != -1) {
344 		/* Found relevant node */
345 		goto out;
346 	}
347 	/*
348 	 * Clean-up and try to find BGX in DT
349 	 * starting from the parent PCI bridge node.
350 	 */
351 	memset(bgx_sel, 0, sizeof(*bgx_sel) * (len + 1));
352 	snprintf(bgx_sel, len, BGX_NODE_NAME"%d", bgx->bgx_id);
353 
354 	/* Find PCI bridge that we are connected to */
355 
356 	root_pcib = bgx_find_root_pcib(bgx->dev);
357 	if (root_pcib == NULL) {
358 		device_printf(bgx->dev, "Unable to find BGX root bridge\n");
359 		node = 0;
360 		goto out;
361 	}
362 
363 	node = ofw_bus_get_node(root_pcib);
364 	if ((int)node <= 0) {
365 		device_printf(bgx->dev, "No parent FDT node for BGX\n");
366 		goto out;
367 	}
368 
369 	node = bgx_fdt_traverse_nodes(bgx->bgx_id, node, bgx_sel, len);
370 out:
371 	free(bgx_sel, M_BGX);
372 	return (node);
373 }
374 
375 int
376 bgx_fdt_init_phy(struct bgx *bgx)
377 {
378 	char *node_name;
379 	phandle_t node, child;
380 	phandle_t phy, mdio;
381 	ssize_t len;
382 	uint8_t lmac;
383 	char qlm_mode[CONN_TYPE_MAXLEN];
384 
385 	node = bgx_fdt_find_node(bgx);
386 	if (node == 0) {
387 		device_printf(bgx->dev,
388 		    "Could not find bgx%d node in FDT\n", bgx->bgx_id);
389 		return (ENXIO);
390 	}
391 
392 	lmac = 0;
393 	for (child = OF_child(node); child > 0; child = OF_peer(child)) {
394 		len = OF_getprop(child, "qlm-mode", qlm_mode, sizeof(qlm_mode));
395 		if (len > 0) {
396 			if (!bgx_fdt_phy_mode_match(bgx, qlm_mode, len)) {
397 				/*
398 				 * Connection type not match with BGX mode.
399 				 */
400 				continue;
401 			}
402 		} else {
403 			len = OF_getprop_alloc(child, "name",
404 			    (void **)&node_name);
405 			if (len <= 0) {
406 				continue;
407 			}
408 
409 			if (!bgx_fdt_phy_name_match(bgx, node_name, len)) {
410 				free(node_name, M_OFWPROP);
411 				continue;
412 			}
413 			free(node_name, M_OFWPROP);
414 		}
415 
416 		/* Acquire PHY address */
417 		if (OF_getencprop(child, "reg", &bgx->lmac[lmac].phyaddr,
418 		    sizeof(bgx->lmac[lmac].phyaddr)) <= 0) {
419 			if (bootverbose) {
420 				device_printf(bgx->dev,
421 				    "Could not retrieve PHY address\n");
422 			}
423 			bgx->lmac[lmac].phyaddr = MII_PHY_ANY;
424 		}
425 
426 		if (OF_getencprop(child, "phy-handle", &phy,
427 		    sizeof(phy)) <= 0) {
428 			if (bootverbose) {
429 				device_printf(bgx->dev,
430 				    "No phy-handle in PHY node. Skipping...\n");
431 			}
432 			continue;
433 		}
434 		phy = OF_instance_to_package(phy);
435 		/*
436 		 * Get PHY interface (MDIO bus) device.
437 		 * Driver must be already attached.
438 		 */
439 		mdio = OF_parent(phy);
440 		bgx->lmac[lmac].phy_if_dev =
441 		    OF_device_from_xref(OF_xref_from_node(mdio));
442 		if (bgx->lmac[lmac].phy_if_dev == NULL) {
443 			if (bootverbose) {
444 				device_printf(bgx->dev,
445 				    "Could not find interface to PHY\n");
446 			}
447 			continue;
448 		}
449 
450 		/* Get mac address from FDT */
451 		bgx_fdt_get_macaddr(child, bgx->lmac[lmac].mac);
452 
453 		bgx->lmac[lmac].lmacid = lmac;
454 		lmac++;
455 		if (lmac == MAX_LMAC_PER_BGX)
456 			break;
457 	}
458 	if (lmac == 0) {
459 		device_printf(bgx->dev, "Could not find matching PHY\n");
460 		return (ENXIO);
461 	}
462 
463 	return (0);
464 }
465