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