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