xref: /freebsd/sys/dev/vnic/thunder_bgx_fdt.c (revision 49b49cda41feabe3439f7318e8bf40e3896c7bf4)
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/mii/miivar.h>
57 
58 #include "thunder_bgx.h"
59 #include "thunder_bgx_var.h"
60 
61 #define	CONN_TYPE_MAXLEN	16
62 #define	CONN_TYPE_OFFSET	2
63 
64 int bgx_fdt_init_phy(struct bgx *);
65 
66 static void
67 bgx_fdt_get_macaddr(phandle_t phy, uint8_t *hwaddr)
68 {
69 	uint8_t addr[ETHER_ADDR_LEN];
70 
71 	if (OF_getprop(phy, "local-mac-address", addr, ETHER_ADDR_LEN) == -1) {
72 		/* Missing MAC address should be marked by clearing it */
73 		memset(hwaddr, 0, ETHER_ADDR_LEN);
74 	} else
75 		memcpy(hwaddr, addr, ETHER_ADDR_LEN);
76 }
77 
78 static boolean_t
79 bgx_fdt_phy_mode_match(struct bgx *bgx, char *qlm_mode, size_t size)
80 {
81 
82 	size -= CONN_TYPE_OFFSET;
83 
84 	switch (bgx->qlm_mode) {
85 	case QLM_MODE_SGMII:
86 		if (strncmp(&qlm_mode[CONN_TYPE_OFFSET], "sgmii", size) == 0)
87 			return (TRUE);
88 		break;
89 	case QLM_MODE_XAUI_1X4:
90 		if (strncmp(&qlm_mode[CONN_TYPE_OFFSET], "xaui", size) == 0)
91 			return (TRUE);
92 		if (strncmp(&qlm_mode[CONN_TYPE_OFFSET], "dxaui", size) == 0)
93 			return (TRUE);
94 		break;
95 	case QLM_MODE_RXAUI_2X2:
96 		if (strncmp(&qlm_mode[CONN_TYPE_OFFSET], "raui", size) == 0)
97 			return (TRUE);
98 		break;
99 	case QLM_MODE_XFI_4X1:
100 		if (strncmp(&qlm_mode[CONN_TYPE_OFFSET], "xfi", size) == 0)
101 			return (TRUE);
102 		break;
103 	case QLM_MODE_XLAUI_1X4:
104 		if (strncmp(&qlm_mode[CONN_TYPE_OFFSET], "xlaui", size) == 0)
105 			return (TRUE);
106 		break;
107 	case QLM_MODE_10G_KR_4X1:
108 		if (strncmp(&qlm_mode[CONN_TYPE_OFFSET], "xfi-10g-kr", size) == 0)
109 			return (TRUE);
110 		break;
111 	case QLM_MODE_40G_KR4_1X4:
112 		if (strncmp(&qlm_mode[CONN_TYPE_OFFSET], "xlaui-40g-kr", size) == 0)
113 			return (TRUE);
114 		break;
115 	default:
116 		return (FALSE);
117 	}
118 
119 	return (FALSE);
120 }
121 
122 int
123 bgx_fdt_init_phy(struct bgx *bgx)
124 {
125 	phandle_t node, child;
126 	phandle_t phy, mdio;
127 	uint8_t lmac;
128 	char bgx_sel[6];
129 	char qlm_mode[CONN_TYPE_MAXLEN];
130 	const char *mac;
131 
132 	(void)mac;
133 
134 	lmac = 0;
135 	/* Get BGX node from DT */
136 	snprintf(bgx_sel, 6, "/bgx%d", bgx->bgx_id);
137 	node = OF_finddevice(bgx_sel);
138 	if (node == 0 || node == -1) {
139 		device_printf(bgx->dev,
140 		    "Could not find %s node in FDT\n", bgx_sel);
141 		return (ENXIO);
142 	}
143 
144 	for (child = OF_child(node); child > 0; child = OF_peer(child)) {
145 		if (OF_getprop(child, "qlm-mode", qlm_mode,
146 		    sizeof(qlm_mode)) <= 0) {
147 			/* Missing qlm-mode, skipping */
148 			continue;
149 		}
150 
151 		if (!bgx_fdt_phy_mode_match(bgx, qlm_mode, sizeof(qlm_mode))) {
152 			/*
153 			 * Connection type not match with BGX mode.
154 			 */
155 			continue;
156 		}
157 
158 		if (OF_getencprop(child, "phy-handle", &phy,
159 		    sizeof(phy)) <= 0) {
160 			if (bootverbose) {
161 				device_printf(bgx->dev,
162 				    "No phy-handle in PHY node. Skipping...\n");
163 			}
164 			continue;
165 		}
166 
167 		/* Acquire PHY address */
168 		phy = OF_node_from_xref(phy);
169 		if (OF_getencprop(phy, "reg", &bgx->lmac[lmac].phyaddr,
170 		    sizeof(bgx->lmac[lmac].phyaddr)) <= 0) {
171 			if (bootverbose) {
172 				device_printf(bgx->dev,
173 				    "Could not retrieve PHY address\n");
174 			}
175 			bgx->lmac[lmac].phyaddr = MII_PHY_ANY;
176 		}
177 
178 		/*
179 		 * Get PHY interface (MDIO bus) device.
180 		 * Driver must be already attached.
181 		 */
182 		mdio = OF_parent(phy);
183 		bgx->lmac[lmac].phy_if_dev =
184 		    OF_device_from_xref(OF_xref_from_node(mdio));
185 		if (bgx->lmac[lmac].phy_if_dev == NULL) {
186 			if (bootverbose) {
187 				device_printf(bgx->dev,
188 				    "Could not find interface to PHY\n");
189 			}
190 			continue;
191 		}
192 
193 		/* Get mac address from FDT */
194 		bgx_fdt_get_macaddr(phy, bgx->lmac[lmac].mac);
195 
196 		bgx->lmac[lmac].lmacid = lmac;
197 		lmac++;
198 		if (lmac == MAX_LMAC_PER_BGX)
199 			break;
200 	}
201 	if (lmac == 0) {
202 		device_printf(bgx->dev, "Could not find matching PHY\n");
203 		return (ENXIO);
204 	}
205 
206 	return (0);
207 }
208