aboutsummaryrefslogtreecommitdiff
path: root/sys/dev/vnic/thunder_bgx_fdt.c
blob: 37ebcad37b4ddb1f36b524f9ee46feaaccfa286b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
/*-
 * Copyright (c) 2015 The FreeBSD Foundation
 * All rights reserved.
 *
 * This software was developed by Semihalf under
 * the sponsorship of the FreeBSD Foundation.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/bitset.h>
#include <sys/bitstring.h>
#include <sys/bus.h>
#include <sys/ctype.h>
#include <sys/endian.h>
#include <sys/kernel.h>
#include <sys/malloc.h>
#include <sys/module.h>
#include <sys/rman.h>
#include <sys/pciio.h>
#include <sys/pcpu.h>
#include <sys/proc.h>
#include <sys/socket.h>
#include <sys/cpuset.h>
#include <sys/lock.h>
#include <sys/mutex.h>

#include <net/ethernet.h>
#include <net/if.h>
#include <net/if_media.h>

#include <dev/ofw/openfirm.h>
#include <dev/ofw/ofw_bus.h>
#include <dev/mii/miivar.h>

#include "thunder_bgx.h"
#include "thunder_bgx_var.h"

#define	CONN_TYPE_MAXLEN	16
#define	CONN_TYPE_OFFSET	2

#define	BGX_NODE_NAME		"bgx"
#define	BGX_MAXID		9
/* BGX func. 0, i.e.: reg = <0x8000 0 0 0 0>; DEVFN = 0x80 */
#define	BGX_DEVFN_0		0x80

#define	FDT_NAME_MAXLEN		31

int bgx_fdt_init_phy(struct bgx *);

static void
bgx_fdt_get_macaddr(phandle_t phy, uint8_t *hwaddr)
{
	uint8_t addr[ETHER_ADDR_LEN];

	if (OF_getprop(phy, "local-mac-address", addr, ETHER_ADDR_LEN) == -1) {
		/* Missing MAC address should be marked by clearing it */
		memset(hwaddr, 0, ETHER_ADDR_LEN);
	} else
		memcpy(hwaddr, addr, ETHER_ADDR_LEN);
}

static boolean_t
bgx_fdt_phy_mode_match(struct bgx *bgx, char *qlm_mode, ssize_t size)
{
	const char *type;
	ssize_t sz;
	ssize_t offset;

	switch (bgx->qlm_mode) {
	case QLM_MODE_SGMII:
		type = "sgmii";
		sz = sizeof("sgmii");
		offset = size - sz;
		break;
	case QLM_MODE_XAUI_1X4:
		type = "xaui";
		sz = sizeof("xaui");
		offset = size - sz;
		if (offset < 0)
			return (FALSE);
		if (strncmp(&qlm_mode[offset], type, sz) == 0)
			return (TRUE);
		type = "dxaui";
		sz = sizeof("dxaui");
		offset = size - sz;
		break;
	case QLM_MODE_RXAUI_2X2:
		type = "raui";
		sz = sizeof("raui");
		offset = size - sz;
		break;
	case QLM_MODE_XFI_4X1:
		type = "xfi";
		sz = sizeof("xfi");
		offset = size - sz;
		break;
	case QLM_MODE_XLAUI_1X4:
		type = "xlaui";
		sz = sizeof("xlaui");
		offset = size - sz;
		break;
	case QLM_MODE_10G_KR_4X1:
		type = "xfi-10g-kr";
		sz = sizeof("xfi-10g-kr");
		offset = size - sz;
		break;
	case QLM_MODE_40G_KR4_1X4:
		type = "xlaui-40g-kr";
		sz = sizeof("xlaui-40g-kr");
		offset = size - sz;
		break;
	default:
		return (FALSE);
	}

	if (offset < 0)
		return (FALSE);

	if (strncmp(&qlm_mode[offset], type, sz) == 0)
		return (TRUE);

	return (FALSE);
}

static boolean_t
bgx_fdt_phy_name_match(struct bgx *bgx, char *phy_name, ssize_t size)
{
	const char *type;
	ssize_t sz;
	char last;

	switch (bgx->qlm_mode) {
	case QLM_MODE_SGMII:
		type = "sgmii";
		sz = sizeof("sgmii");
		break;
	case QLM_MODE_XAUI_1X4:
		type = "xaui";
		sz = sizeof("xaui");
		if (sz < size)
			return (FALSE);
		if (strncmp(phy_name, type, sz) == 0)
			return (TRUE);
		type = "dxaui";
		sz = sizeof("dxaui");
		break;
	case QLM_MODE_RXAUI_2X2:
		type = "raui";
		sz = sizeof("raui");
		break;
	case QLM_MODE_XFI_4X1:
		type = "xfi";
		sz = sizeof("xfi");
		break;
	case QLM_MODE_XLAUI_1X4:
		type = "xlaui";
		sz = sizeof("xlaui");
		break;
	case QLM_MODE_10G_KR_4X1:
		type = "xfi-10g-kr";
		sz = sizeof("xfi-10g-kr");
		break;
	case QLM_MODE_40G_KR4_1X4:
		type = "xlaui-40g-kr";
		sz = sizeof("xlaui-40g-kr");
		break;
	default:
		return (FALSE);
	}

	if (sz > size)
		return (FALSE);
	if (strncmp(phy_name, type, sz - 1) == 0) {
		last = phy_name[sz - 1];
		if (last == '\0' || last == '@' || isdigit(last))
			return (TRUE);
	}
	return (FALSE);
}

static phandle_t
bgx_fdt_traverse_nodes(uint8_t unit, phandle_t start, char *name,
    size_t len)
{
	phandle_t node, ret;
	uint32_t *reg;
	size_t buf_size;
	ssize_t proplen;
	char *node_name;
	int err;

	/*
	 * Traverse all subordinate nodes of 'start' to find BGX instance.
	 * This supports both old (by name) and new (by reg) methods.
	 */
	buf_size = sizeof(*node_name) * FDT_NAME_MAXLEN;
	if (len > buf_size) {
		/*
		 * This is an erroneous situation since the string
		 * to compare cannot be longer than FDT_NAME_MAXLEN.
		 */
		return (0);
	}

	node_name = malloc(buf_size, M_BGX, M_WAITOK);
	for (node = OF_child(start); node != 0; node = OF_peer(node)) {
		/* Clean-up the buffer */
		memset(node_name, 0, buf_size);
		/* Recurse to children */
		if (OF_child(node) != 0) {
			ret = bgx_fdt_traverse_nodes(unit, node, name, len);
			if (ret != 0) {
				free(node_name, M_BGX);
				return (ret);
			}
		}
		/*
		 * Old way - by name
		 */
		proplen = OF_getproplen(node, "name");
		if ((proplen <= 0) || (proplen < len))
			continue;

		err = OF_getprop(node, "name", node_name, proplen);
		if (err <= 0)
			continue;

		if (strncmp(node_name, name, len) == 0) {
			free(node_name, M_BGX);
			return (node);
		}
		/*
		 * New way - by reg
		 */
		/* Check if even BGX */
		if (strncmp(node_name,
		    BGX_NODE_NAME, sizeof(BGX_NODE_NAME) - 1) != 0)
			continue;
		/* Get reg */
		err = OF_getencprop_alloc_multi(node, "reg", sizeof(*reg),
		    (void **)&reg);
		if (err == -1) {
			free(reg, M_OFWPROP);
			continue;
		}

		/* Match BGX device function */
		if ((BGX_DEVFN_0 + unit) == (reg[0] >> 8)) {
			free(reg, M_OFWPROP);
			free(node_name, M_BGX);
			return (node);
		}
		free(reg, M_OFWPROP);
	}
	free(node_name, M_BGX);

	return (0);
}

/*
 * Similar functionality to pci_find_pcie_root_port()
 * but this one works for ThunderX.
 */
static device_t
bgx_find_root_pcib(device_t dev)
{
	devclass_t pci_class;
	device_t pcib, bus;

	pci_class = devclass_find("pci");
	KASSERT(device_get_devclass(device_get_parent(dev)) == pci_class,
	    ("%s: non-pci device %s", __func__, device_get_nameunit(dev)));

	/* Walk the bridge hierarchy until we find a non-PCI device */
	for (;;) {
		bus = device_get_parent(dev);
		KASSERT(bus != NULL, ("%s: null parent of %s", __func__,
		    device_get_nameunit(dev)));

		if (device_get_devclass(bus) != pci_class)
			return (NULL);

		pcib = device_get_parent(bus);
		KASSERT(pcib != NULL, ("%s: null bridge of %s", __func__,
		    device_get_nameunit(bus)));

		/*
		 * If the parent of this PCIB is not PCI
		 * then we found our root PCIB.
		 */
		if (device_get_devclass(device_get_parent(pcib)) != pci_class)
			return (pcib);

		dev = pcib;
	}
}

static __inline phandle_t
bgx_fdt_find_node(struct bgx *bgx)
{
	device_t root_pcib;
	phandle_t node;
	char *bgx_sel;
	size_t len;

	KASSERT(bgx->bgx_id <= BGX_MAXID,
	    ("Invalid BGX ID: %d, max: %d", bgx->bgx_id, BGX_MAXID));

	len = sizeof(BGX_NODE_NAME) + 1; /* <bgx_name>+<digit>+<\0> */
	/* Allocate memory for BGX node name + "/" character */
	bgx_sel = malloc(sizeof(*bgx_sel) * (len + 1), M_BGX,
	    M_ZERO | M_WAITOK);

	/* Prepare node's name */
	snprintf(bgx_sel, len + 1, "/"BGX_NODE_NAME"%d", bgx->bgx_id);
	/* First try the root node */
	node =  OF_finddevice(bgx_sel);
	if (node != -1) {
		/* Found relevant node */
		goto out;
	}
	/*
	 * Clean-up and try to find BGX in DT
	 * starting from the parent PCI bridge node.
	 */
	memset(bgx_sel, 0, sizeof(*bgx_sel) * (len + 1));
	snprintf(bgx_sel, len, BGX_NODE_NAME"%d", bgx->bgx_id);

	/* Find PCI bridge that we are connected to */

	root_pcib = bgx_find_root_pcib(bgx->dev);
	if (root_pcib == NULL) {
		device_printf(bgx->dev, "Unable to find BGX root bridge\n");
		node = 0;
		goto out;
	}

	node = ofw_bus_get_node(root_pcib);
	if ((int)node <= 0) {
		device_printf(bgx->dev, "No parent FDT node for BGX\n");
		goto out;
	}

	node = bgx_fdt_traverse_nodes(bgx->bgx_id, node, bgx_sel, len);
out:
	free(bgx_sel, M_BGX);
	return (node);
}

int
bgx_fdt_init_phy(struct bgx *bgx)
{
	char *node_name;
	phandle_t node, child;
	phandle_t phy, mdio;
	ssize_t len;
	uint8_t lmac;
	char qlm_mode[CONN_TYPE_MAXLEN];

	node = bgx_fdt_find_node(bgx);
	if (node == 0) {
		device_printf(bgx->dev,
		    "Could not find bgx%d node in FDT\n", bgx->bgx_id);
		return (ENXIO);
	}

	lmac = 0;
	for (child = OF_child(node); child > 0; child = OF_peer(child)) {
		len = OF_getprop(child, "qlm-mode", qlm_mode, sizeof(qlm_mode));
		if (len > 0) {
			if (!bgx_fdt_phy_mode_match(bgx, qlm_mode, len)) {
				/*
				 * Connection type not match with BGX mode.
				 */
				continue;
			}
		} else {
			len = OF_getprop_alloc(child, "name",
			    (void **)&node_name);
			if (len <= 0) {
				continue;
			}

			if (!bgx_fdt_phy_name_match(bgx, node_name, len)) {
				free(node_name, M_OFWPROP);
				continue;
			}
			free(node_name, M_OFWPROP);
		}

		/* Acquire PHY address */
		if (OF_getencprop(child, "reg", &bgx->lmac[lmac].phyaddr,
		    sizeof(bgx->lmac[lmac].phyaddr)) <= 0) {
			if (bootverbose) {
				device_printf(bgx->dev,
				    "Could not retrieve PHY address\n");
			}
			bgx->lmac[lmac].phyaddr = MII_PHY_ANY;
		}

		if (OF_getencprop(child, "phy-handle", &phy,
		    sizeof(phy)) <= 0) {
			if (bootverbose) {
				device_printf(bgx->dev,
				    "No phy-handle in PHY node. Skipping...\n");
			}
			continue;
		}
		phy = OF_instance_to_package(phy);
		/*
		 * Get PHY interface (MDIO bus) device.
		 * Driver must be already attached.
		 */
		mdio = OF_parent(phy);
		bgx->lmac[lmac].phy_if_dev =
		    OF_device_from_xref(OF_xref_from_node(mdio));
		if (bgx->lmac[lmac].phy_if_dev == NULL) {
			if (bootverbose) {
				device_printf(bgx->dev,
				    "Could not find interface to PHY\n");
			}
			continue;
		}

		/* Get mac address from FDT */
		bgx_fdt_get_macaddr(child, bgx->lmac[lmac].mac);

		bgx->lmac[lmac].lmacid = lmac;
		lmac++;
		if (lmac == MAX_LMAC_PER_BGX)
			break;
	}
	if (lmac == 0) {
		device_printf(bgx->dev, "Could not find matching PHY\n");
		return (ENXIO);
	}

	return (0);
}