xref: /dpdk/drivers/bus/dpaa/base/fman/fman.c (revision 2d0c29a37a9c080c1cccb1ad7941aba2ccf5437e)
1 /* SPDX-License-Identifier: (BSD-3-Clause OR GPL-2.0)
2  *
3  * Copyright 2010-2016 Freescale Semiconductor Inc.
4  * Copyright 2017 NXP
5  *
6  */
7 
8 #include <sys/types.h>
9 #include <sys/ioctl.h>
10 #include <ifaddrs.h>
11 
12 /* This header declares the driver interface we implement */
13 #include <fman.h>
14 #include <of.h>
15 #include <rte_malloc.h>
16 #include <rte_dpaa_logs.h>
17 #include <rte_string_fns.h>
18 
19 #define QMI_PORT_REGS_OFFSET		0x400
20 
21 /* CCSR map address to access ccsr based register */
22 void *fman_ccsr_map;
23 /* fman version info */
24 u16 fman_ip_rev;
25 static int get_once;
26 u32 fman_dealloc_bufs_mask_hi;
27 u32 fman_dealloc_bufs_mask_lo;
28 
29 int fman_ccsr_map_fd = -1;
30 static COMPAT_LIST_HEAD(__ifs);
31 
32 /* This is the (const) global variable that callers have read-only access to.
33  * Internally, we have read-write access directly to __ifs.
34  */
35 const struct list_head *fman_if_list = &__ifs;
36 
37 static void
38 if_destructor(struct __fman_if *__if)
39 {
40 	struct fman_if_bpool *bp, *tmpbp;
41 
42 	if (!__if)
43 		return;
44 
45 	if (__if->__if.mac_type == fman_offline)
46 		goto cleanup;
47 
48 	list_for_each_entry_safe(bp, tmpbp, &__if->__if.bpool_list, node) {
49 		list_del(&bp->node);
50 		free(bp);
51 	}
52 cleanup:
53 	free(__if);
54 }
55 
56 static int
57 fman_get_ip_rev(const struct device_node *fman_node)
58 {
59 	const uint32_t *fman_addr;
60 	uint64_t phys_addr;
61 	uint64_t regs_size;
62 	uint32_t ip_rev_1;
63 	int _errno;
64 
65 	fman_addr = of_get_address(fman_node, 0, &regs_size, NULL);
66 	if (!fman_addr) {
67 		pr_err("of_get_address cannot return fman address\n");
68 		return -EINVAL;
69 	}
70 	phys_addr = of_translate_address(fman_node, fman_addr);
71 	if (!phys_addr) {
72 		pr_err("of_translate_address failed\n");
73 		return -EINVAL;
74 	}
75 	fman_ccsr_map = mmap(NULL, regs_size, PROT_READ | PROT_WRITE,
76 			     MAP_SHARED, fman_ccsr_map_fd, phys_addr);
77 	if (fman_ccsr_map == MAP_FAILED) {
78 		pr_err("Can not map FMan ccsr base");
79 		return -EINVAL;
80 	}
81 
82 	ip_rev_1 = in_be32(fman_ccsr_map + FMAN_IP_REV_1);
83 	fman_ip_rev = (ip_rev_1 & FMAN_IP_REV_1_MAJOR_MASK) >>
84 			FMAN_IP_REV_1_MAJOR_SHIFT;
85 
86 	_errno = munmap(fman_ccsr_map, regs_size);
87 	if (_errno)
88 		pr_err("munmap() of FMan ccsr failed");
89 
90 	return 0;
91 }
92 
93 static int
94 fman_get_mac_index(uint64_t regs_addr_host, uint8_t *mac_idx)
95 {
96 	int ret = 0;
97 
98 	/*
99 	 * MAC1 : E_0000h
100 	 * MAC2 : E_2000h
101 	 * MAC3 : E_4000h
102 	 * MAC4 : E_6000h
103 	 * MAC5 : E_8000h
104 	 * MAC6 : E_A000h
105 	 * MAC7 : E_C000h
106 	 * MAC8 : E_E000h
107 	 * MAC9 : F_0000h
108 	 * MAC10: F_2000h
109 	 */
110 	switch (regs_addr_host) {
111 	case 0xE0000:
112 		*mac_idx = 1;
113 		break;
114 	case 0xE2000:
115 		*mac_idx = 2;
116 		break;
117 	case 0xE4000:
118 		*mac_idx = 3;
119 		break;
120 	case 0xE6000:
121 		*mac_idx = 4;
122 		break;
123 	case 0xE8000:
124 		*mac_idx = 5;
125 		break;
126 	case 0xEA000:
127 		*mac_idx = 6;
128 		break;
129 	case 0xEC000:
130 		*mac_idx = 7;
131 		break;
132 	case 0xEE000:
133 		*mac_idx = 8;
134 		break;
135 	case 0xF0000:
136 		*mac_idx = 9;
137 		break;
138 	case 0xF2000:
139 		*mac_idx = 10;
140 		break;
141 	default:
142 		ret = -EINVAL;
143 	}
144 
145 	return ret;
146 }
147 
148 static int
149 fman_if_init(const struct device_node *dpa_node)
150 {
151 	const char *rprop, *mprop;
152 	uint64_t phys_addr;
153 	struct __fman_if *__if;
154 	struct fman_if_bpool *bpool;
155 
156 	const phandle *mac_phandle, *ports_phandle, *pools_phandle;
157 	const phandle *tx_channel_id = NULL, *mac_addr, *cell_idx;
158 	const phandle *rx_phandle, *tx_phandle;
159 	uint64_t tx_phandle_host[4] = {0};
160 	uint64_t rx_phandle_host[4] = {0};
161 	uint64_t regs_addr_host = 0;
162 	uint64_t cell_idx_host = 0;
163 
164 	const struct device_node *mac_node = NULL, *tx_node;
165 	const struct device_node *pool_node, *fman_node, *rx_node;
166 	const uint32_t *regs_addr = NULL;
167 	const char *mname, *fname;
168 	const char *dname = dpa_node->full_name;
169 	size_t lenp;
170 	int _errno;
171 	const char *char_prop;
172 	uint32_t na;
173 
174 	if (of_device_is_available(dpa_node) == false)
175 		return 0;
176 
177 	rprop = "fsl,qman-frame-queues-rx";
178 	mprop = "fsl,fman-mac";
179 
180 	/* Allocate an object for this network interface */
181 	__if = rte_malloc(NULL, sizeof(*__if), RTE_CACHE_LINE_SIZE);
182 	if (!__if) {
183 		FMAN_ERR(-ENOMEM, "malloc(%zu)\n", sizeof(*__if));
184 		goto err;
185 	}
186 	memset(__if, 0, sizeof(*__if));
187 	INIT_LIST_HEAD(&__if->__if.bpool_list);
188 	strlcpy(__if->node_path, dpa_node->full_name, PATH_MAX - 1);
189 	__if->node_path[PATH_MAX - 1] = '\0';
190 
191 	/* Obtain the MAC node used by this interface except macless */
192 	mac_phandle = of_get_property(dpa_node, mprop, &lenp);
193 	if (!mac_phandle) {
194 		FMAN_ERR(-EINVAL, "%s: no %s\n", dname, mprop);
195 		goto err;
196 	}
197 	assert(lenp == sizeof(phandle));
198 	mac_node = of_find_node_by_phandle(*mac_phandle);
199 	if (!mac_node) {
200 		FMAN_ERR(-ENXIO, "%s: bad 'fsl,fman-mac\n", dname);
201 		goto err;
202 	}
203 	mname = mac_node->full_name;
204 
205 	/* Map the CCSR regs for the MAC node */
206 	regs_addr = of_get_address(mac_node, 0, &__if->regs_size, NULL);
207 	if (!regs_addr) {
208 		FMAN_ERR(-EINVAL, "of_get_address(%s)\n", mname);
209 		goto err;
210 	}
211 	phys_addr = of_translate_address(mac_node, regs_addr);
212 	if (!phys_addr) {
213 		FMAN_ERR(-EINVAL, "of_translate_address(%s, %p)\n",
214 			 mname, regs_addr);
215 		goto err;
216 	}
217 	__if->ccsr_map = mmap(NULL, __if->regs_size,
218 			      PROT_READ | PROT_WRITE, MAP_SHARED,
219 			      fman_ccsr_map_fd, phys_addr);
220 	if (__if->ccsr_map == MAP_FAILED) {
221 		FMAN_ERR(-errno, "mmap(0x%"PRIx64")\n", phys_addr);
222 		goto err;
223 	}
224 	na = of_n_addr_cells(mac_node);
225 	/* Get rid of endianness (issues). Convert to host byte order */
226 	regs_addr_host = of_read_number(regs_addr, na);
227 
228 
229 	/* Get the index of the Fman this i/f belongs to */
230 	fman_node = of_get_parent(mac_node);
231 	na = of_n_addr_cells(mac_node);
232 	if (!fman_node) {
233 		FMAN_ERR(-ENXIO, "of_get_parent(%s)\n", mname);
234 		goto err;
235 	}
236 	fname = fman_node->full_name;
237 	cell_idx = of_get_property(fman_node, "cell-index", &lenp);
238 	if (!cell_idx) {
239 		FMAN_ERR(-ENXIO, "%s: no cell-index)\n", fname);
240 		goto err;
241 	}
242 	assert(lenp == sizeof(*cell_idx));
243 	cell_idx_host = of_read_number(cell_idx, lenp / sizeof(phandle));
244 	__if->__if.fman_idx = cell_idx_host;
245 	if (!get_once) {
246 		_errno = fman_get_ip_rev(fman_node);
247 		if (_errno) {
248 			FMAN_ERR(-ENXIO, "%s: ip_rev is not available\n",
249 				 fname);
250 			goto err;
251 		}
252 	}
253 
254 	if (fman_ip_rev >= FMAN_V3) {
255 		/*
256 		 * Set A2V, OVOM, EBD bits in contextA to allow external
257 		 * buffer deallocation by fman.
258 		 */
259 		fman_dealloc_bufs_mask_hi = FMAN_V3_CONTEXTA_EN_A2V |
260 						FMAN_V3_CONTEXTA_EN_OVOM;
261 		fman_dealloc_bufs_mask_lo = FMAN_V3_CONTEXTA_EN_EBD;
262 	} else {
263 		fman_dealloc_bufs_mask_hi = 0;
264 		fman_dealloc_bufs_mask_lo = 0;
265 	}
266 	/* Is the MAC node 1G, 10G? */
267 	__if->__if.is_memac = 0;
268 
269 	if (of_device_is_compatible(mac_node, "fsl,fman-1g-mac"))
270 		__if->__if.mac_type = fman_mac_1g;
271 	else if (of_device_is_compatible(mac_node, "fsl,fman-10g-mac"))
272 		__if->__if.mac_type = fman_mac_10g;
273 	else if (of_device_is_compatible(mac_node, "fsl,fman-memac")) {
274 		__if->__if.is_memac = 1;
275 		char_prop = of_get_property(mac_node, "phy-connection-type",
276 					    NULL);
277 		if (!char_prop) {
278 			printf("memac: unknown MII type assuming 1G\n");
279 			/* Right now forcing memac to 1g in case of error*/
280 			__if->__if.mac_type = fman_mac_1g;
281 		} else {
282 			if (strstr(char_prop, "sgmii"))
283 				__if->__if.mac_type = fman_mac_1g;
284 			else if (strstr(char_prop, "rgmii")) {
285 				__if->__if.mac_type = fman_mac_1g;
286 				__if->__if.is_rgmii = 1;
287 			} else if (strstr(char_prop, "xgmii"))
288 				__if->__if.mac_type = fman_mac_10g;
289 		}
290 	} else {
291 		FMAN_ERR(-EINVAL, "%s: unknown MAC type\n", mname);
292 		goto err;
293 	}
294 
295 	/*
296 	 * For MAC ports, we cannot rely on cell-index. In
297 	 * T2080, two of the 10G ports on single FMAN have same
298 	 * duplicate cell-indexes as the other two 10G ports on
299 	 * same FMAN. Hence, we now rely upon addresses of the
300 	 * ports from device tree to deduce the index.
301 	 */
302 
303 	_errno = fman_get_mac_index(regs_addr_host, &__if->__if.mac_idx);
304 	if (_errno) {
305 		FMAN_ERR(-EINVAL, "Invalid register address: %" PRIx64,
306 			 regs_addr_host);
307 		goto err;
308 	}
309 
310 	/* Extract the MAC address for private and shared interfaces */
311 	mac_addr = of_get_property(mac_node, "local-mac-address",
312 				   &lenp);
313 	if (!mac_addr) {
314 		FMAN_ERR(-EINVAL, "%s: no local-mac-address\n",
315 			 mname);
316 		goto err;
317 	}
318 	memcpy(&__if->__if.mac_addr, mac_addr, ETHER_ADDR_LEN);
319 
320 	/* Extract the Tx port (it's the second of the two port handles)
321 	 * and get its channel ID
322 	 */
323 	ports_phandle = of_get_property(mac_node, "fsl,port-handles",
324 					&lenp);
325 	if (!ports_phandle)
326 		ports_phandle = of_get_property(mac_node, "fsl,fman-ports",
327 						&lenp);
328 	if (!ports_phandle) {
329 		FMAN_ERR(-EINVAL, "%s: no fsl,port-handles\n",
330 			 mname);
331 		goto err;
332 	}
333 	assert(lenp == (2 * sizeof(phandle)));
334 	tx_node = of_find_node_by_phandle(ports_phandle[1]);
335 	if (!tx_node) {
336 		FMAN_ERR(-ENXIO, "%s: bad fsl,port-handle[1]\n", mname);
337 		goto err;
338 	}
339 	/* Extract the channel ID (from tx-port-handle) */
340 	tx_channel_id = of_get_property(tx_node, "fsl,qman-channel-id",
341 					&lenp);
342 	if (!tx_channel_id) {
343 		FMAN_ERR(-EINVAL, "%s: no fsl-qman-channel-id\n",
344 			 tx_node->full_name);
345 		goto err;
346 	}
347 
348 	rx_node = of_find_node_by_phandle(ports_phandle[0]);
349 	if (!rx_node) {
350 		FMAN_ERR(-ENXIO, "%s: bad fsl,port-handle[0]\n", mname);
351 		goto err;
352 	}
353 	regs_addr = of_get_address(rx_node, 0, &__if->regs_size, NULL);
354 	if (!regs_addr) {
355 		FMAN_ERR(-EINVAL, "of_get_address(%s)\n", mname);
356 		goto err;
357 	}
358 	phys_addr = of_translate_address(rx_node, regs_addr);
359 	if (!phys_addr) {
360 		FMAN_ERR(-EINVAL, "of_translate_address(%s, %p)\n",
361 			 mname, regs_addr);
362 		goto err;
363 	}
364 	__if->bmi_map = mmap(NULL, __if->regs_size,
365 				 PROT_READ | PROT_WRITE, MAP_SHARED,
366 				 fman_ccsr_map_fd, phys_addr);
367 	if (__if->bmi_map == MAP_FAILED) {
368 		FMAN_ERR(-errno, "mmap(0x%"PRIx64")\n", phys_addr);
369 		goto err;
370 	}
371 
372 	/* No channel ID for MAC-less */
373 	assert(lenp == sizeof(*tx_channel_id));
374 	na = of_n_addr_cells(mac_node);
375 	__if->__if.tx_channel_id = of_read_number(tx_channel_id, na);
376 
377 	/* Extract the Rx FQIDs. (Note, the device representation is silly,
378 	 * there are "counts" that must always be 1.)
379 	 */
380 	rx_phandle = of_get_property(dpa_node, rprop, &lenp);
381 	if (!rx_phandle) {
382 		FMAN_ERR(-EINVAL, "%s: no fsl,qman-frame-queues-rx\n", dname);
383 		goto err;
384 	}
385 
386 	assert(lenp == (4 * sizeof(phandle)));
387 
388 	na = of_n_addr_cells(mac_node);
389 	/* Get rid of endianness (issues). Convert to host byte order */
390 	rx_phandle_host[0] = of_read_number(&rx_phandle[0], na);
391 	rx_phandle_host[1] = of_read_number(&rx_phandle[1], na);
392 	rx_phandle_host[2] = of_read_number(&rx_phandle[2], na);
393 	rx_phandle_host[3] = of_read_number(&rx_phandle[3], na);
394 
395 	assert((rx_phandle_host[1] == 1) && (rx_phandle_host[3] == 1));
396 	__if->__if.fqid_rx_err = rx_phandle_host[0];
397 	__if->__if.fqid_rx_def = rx_phandle_host[2];
398 
399 	/* Extract the Tx FQIDs */
400 	tx_phandle = of_get_property(dpa_node,
401 				     "fsl,qman-frame-queues-tx", &lenp);
402 	if (!tx_phandle) {
403 		FMAN_ERR(-EINVAL, "%s: no fsl,qman-frame-queues-tx\n", dname);
404 		goto err;
405 	}
406 
407 	assert(lenp == (4 * sizeof(phandle)));
408 	/*TODO: Fix for other cases also */
409 	na = of_n_addr_cells(mac_node);
410 	/* Get rid of endianness (issues). Convert to host byte order */
411 	tx_phandle_host[0] = of_read_number(&tx_phandle[0], na);
412 	tx_phandle_host[1] = of_read_number(&tx_phandle[1], na);
413 	tx_phandle_host[2] = of_read_number(&tx_phandle[2], na);
414 	tx_phandle_host[3] = of_read_number(&tx_phandle[3], na);
415 	assert((tx_phandle_host[1] == 1) && (tx_phandle_host[3] == 1));
416 	__if->__if.fqid_tx_err = tx_phandle_host[0];
417 	__if->__if.fqid_tx_confirm = tx_phandle_host[2];
418 
419 	/* Obtain the buffer pool nodes used by this interface */
420 	pools_phandle = of_get_property(dpa_node, "fsl,bman-buffer-pools",
421 					&lenp);
422 	if (!pools_phandle) {
423 		FMAN_ERR(-EINVAL, "%s: no fsl,bman-buffer-pools\n", dname);
424 		goto err;
425 	}
426 	/* For each pool, parse the corresponding node and add a pool object
427 	 * to the interface's "bpool_list"
428 	 */
429 	assert(lenp && !(lenp % sizeof(phandle)));
430 	while (lenp) {
431 		size_t proplen;
432 		const phandle *prop;
433 		uint64_t bpid_host = 0;
434 		uint64_t bpool_host[6] = {0};
435 		const char *pname;
436 		/* Allocate an object for the pool */
437 		bpool = rte_malloc(NULL, sizeof(*bpool), RTE_CACHE_LINE_SIZE);
438 		if (!bpool) {
439 			FMAN_ERR(-ENOMEM, "malloc(%zu)\n", sizeof(*bpool));
440 			goto err;
441 		}
442 		/* Find the pool node */
443 		pool_node = of_find_node_by_phandle(*pools_phandle);
444 		if (!pool_node) {
445 			FMAN_ERR(-ENXIO, "%s: bad fsl,bman-buffer-pools\n",
446 				 dname);
447 			rte_free(bpool);
448 			goto err;
449 		}
450 		pname = pool_node->full_name;
451 		/* Extract the BPID property */
452 		prop = of_get_property(pool_node, "fsl,bpid", &proplen);
453 		if (!prop) {
454 			FMAN_ERR(-EINVAL, "%s: no fsl,bpid\n", pname);
455 			rte_free(bpool);
456 			goto err;
457 		}
458 		assert(proplen == sizeof(*prop));
459 		na = of_n_addr_cells(mac_node);
460 		/* Get rid of endianness (issues).
461 		 * Convert to host byte-order
462 		 */
463 		bpid_host = of_read_number(prop, na);
464 		bpool->bpid = bpid_host;
465 		/* Extract the cfg property (count/size/addr). "fsl,bpool-cfg"
466 		 * indicates for the Bman driver to seed the pool.
467 		 * "fsl,bpool-ethernet-cfg" is used by the network driver. The
468 		 * two are mutually exclusive, so check for either of them.
469 		 */
470 		prop = of_get_property(pool_node, "fsl,bpool-cfg",
471 				       &proplen);
472 		if (!prop)
473 			prop = of_get_property(pool_node,
474 					       "fsl,bpool-ethernet-cfg",
475 					       &proplen);
476 		if (!prop) {
477 			/* It's OK for there to be no bpool-cfg */
478 			bpool->count = bpool->size = bpool->addr = 0;
479 		} else {
480 			assert(proplen == (6 * sizeof(*prop)));
481 			na = of_n_addr_cells(mac_node);
482 			/* Get rid of endianness (issues).
483 			 * Convert to host byte order
484 			 */
485 			bpool_host[0] = of_read_number(&prop[0], na);
486 			bpool_host[1] = of_read_number(&prop[1], na);
487 			bpool_host[2] = of_read_number(&prop[2], na);
488 			bpool_host[3] = of_read_number(&prop[3], na);
489 			bpool_host[4] = of_read_number(&prop[4], na);
490 			bpool_host[5] = of_read_number(&prop[5], na);
491 
492 			bpool->count = ((uint64_t)bpool_host[0] << 32) |
493 					bpool_host[1];
494 			bpool->size = ((uint64_t)bpool_host[2] << 32) |
495 					bpool_host[3];
496 			bpool->addr = ((uint64_t)bpool_host[4] << 32) |
497 					bpool_host[5];
498 		}
499 		/* Parsing of the pool is complete, add it to the interface
500 		 * list.
501 		 */
502 		list_add_tail(&bpool->node, &__if->__if.bpool_list);
503 		lenp -= sizeof(phandle);
504 		pools_phandle++;
505 	}
506 
507 	/* Parsing of the network interface is complete, add it to the list */
508 	DPAA_BUS_LOG(DEBUG, "Found %s, Tx Channel = %x, FMAN = %x,"
509 		    "Port ID = %x",
510 		    dname, __if->__if.tx_channel_id, __if->__if.fman_idx,
511 		    __if->__if.mac_idx);
512 
513 	list_add_tail(&__if->__if.node, &__ifs);
514 	return 0;
515 err:
516 	if_destructor(__if);
517 	return _errno;
518 }
519 
520 int
521 fman_init(void)
522 {
523 	const struct device_node *dpa_node;
524 	int _errno;
525 
526 	/* If multiple dependencies try to initialise the Fman driver, don't
527 	 * panic.
528 	 */
529 	if (fman_ccsr_map_fd != -1)
530 		return 0;
531 
532 	fman_ccsr_map_fd = open(FMAN_DEVICE_PATH, O_RDWR);
533 	if (unlikely(fman_ccsr_map_fd < 0)) {
534 		DPAA_BUS_LOG(ERR, "Unable to open (/dev/mem)");
535 		return fman_ccsr_map_fd;
536 	}
537 
538 	for_each_compatible_node(dpa_node, NULL, "fsl,dpa-ethernet-init") {
539 		_errno = fman_if_init(dpa_node);
540 		if (_errno) {
541 			FMAN_ERR(_errno, "if_init(%s)\n", dpa_node->full_name);
542 			goto err;
543 		}
544 	}
545 
546 	return 0;
547 err:
548 	fman_finish();
549 	return _errno;
550 }
551 
552 void
553 fman_finish(void)
554 {
555 	struct __fman_if *__if, *tmpif;
556 
557 	assert(fman_ccsr_map_fd != -1);
558 
559 	list_for_each_entry_safe(__if, tmpif, &__ifs, __if.node) {
560 		int _errno;
561 
562 		/* disable Rx and Tx */
563 		if ((__if->__if.mac_type == fman_mac_1g) &&
564 		    (!__if->__if.is_memac))
565 			out_be32(__if->ccsr_map + 0x100,
566 				 in_be32(__if->ccsr_map + 0x100) & ~(u32)0x5);
567 		else
568 			out_be32(__if->ccsr_map + 8,
569 				 in_be32(__if->ccsr_map + 8) & ~(u32)3);
570 		/* release the mapping */
571 		_errno = munmap(__if->ccsr_map, __if->regs_size);
572 		if (unlikely(_errno < 0))
573 			fprintf(stderr, "%s:%d:%s(): munmap() = %d (%s)\n",
574 				__FILE__, __LINE__, __func__,
575 				-errno, strerror(errno));
576 		printf("Tearing down %s\n", __if->node_path);
577 		list_del(&__if->__if.node);
578 		rte_free(__if);
579 	}
580 
581 	close(fman_ccsr_map_fd);
582 	fman_ccsr_map_fd = -1;
583 }
584