xref: /dpdk/drivers/bus/dpaa/base/fman/netcfg_layer.c (revision 7e5f49ae767da93486d28142ef53a8fd745f240b)
1 /* SPDX-License-Identifier: (BSD-3-Clause OR GPL-2.0)
2  *
3  * Copyright 2010-2016 Freescale Semiconductor Inc.
4  * Copyright 2017-2019,2023 NXP
5  *
6  */
7 #include <inttypes.h>
8 #include <dpaa_of.h>
9 #include <net/if.h>
10 #include <sys/ioctl.h>
11 #include <err.h>
12 #include <net/if_arp.h>
13 #include <assert.h>
14 #include <unistd.h>
15 
16 #include <rte_malloc.h>
17 
18 #include <rte_dpaa_logs.h>
19 #include <netcfg.h>
20 
21 /* This data structure contaings all configurations information
22  * related to usages of DPA devices.
23  */
24 static struct netcfg_info *netcfg;
25 /* fd to open a socket for making ioctl request to disable/enable shared
26  *  interfaces.
27  */
28 static int skfd = -1;
29 
30 #ifdef RTE_LIBRTE_DPAA_DEBUG_DRIVER
31 void
32 dump_netcfg(struct netcfg_info *cfg_ptr, FILE *f)
33 {
34 	int i;
35 
36 	fprintf(f, "..........  DPAA Configuration  ..........\n\n");
37 
38 	/* Network interfaces */
39 	fprintf(f, "Network interfaces: %d\n", cfg_ptr->num_ethports);
40 	for (i = 0; i < cfg_ptr->num_ethports; i++) {
41 		struct fman_if_bpool *bpool;
42 		struct fm_eth_port_cfg *p_cfg = &cfg_ptr->port_cfg[i];
43 		struct fman_if *__if = p_cfg->fman_if;
44 
45 		fprintf(f, "\n+ Fman %d, MAC %d (%s);\n",
46 		       __if->fman_idx, __if->mac_idx,
47 		       (__if->mac_type == fman_offline_internal) ? "OFFLINE" :
48 		       (__if->mac_type == fman_mac_1g) ? "1G" :
49 		       (__if->mac_type == fman_mac_2_5g) ? "2.5G" : "10G");
50 
51 		fprintf(f, "\tmac_addr: " RTE_ETHER_ADDR_PRT_FMT "\n",
52 		       RTE_ETHER_ADDR_BYTES(&__if->mac_addr));
53 
54 		fprintf(f, "\ttx_channel_id: 0x%02x\n",
55 		       __if->tx_channel_id);
56 
57 		fprintf(f, "\tfqid_rx_def: 0x%x\n", p_cfg->rx_def);
58 		fprintf(f, "\tfqid_rx_err: 0x%x\n", __if->fqid_rx_err);
59 
60 		if (__if->mac_type != fman_offline_internal) {
61 			fprintf(f, "\tfqid_tx_err: 0x%x\n", __if->fqid_tx_err);
62 			fprintf(f, "\tfqid_tx_confirm: 0x%x\n", __if->fqid_tx_confirm);
63 			fman_if_for_each_bpool(bpool, __if)
64 				fprintf(f, "\tbuffer pool: (bpid=%d, count=%"PRId64
65 				       " size=%"PRId64", addr=0x%"PRIx64")\n",
66 				       bpool->bpid, bpool->count, bpool->size,
67 				       bpool->addr);
68 		}
69 	}
70 }
71 #endif /* RTE_LIBRTE_DPAA_DEBUG_DRIVER */
72 
73 struct netcfg_info *
74 netcfg_acquire(void)
75 {
76 	struct fman_if *__if;
77 	int _errno, idx = 0;
78 	uint8_t num_ports = 0;
79 	uint8_t num_cfg_ports = 0;
80 	size_t size;
81 
82 	/* Extract dpa configuration from fman driver and FMC configuration
83 	 * for command-line interfaces.
84 	 */
85 
86 	/* Open a basic socket to enable/disable shared
87 	 * interfaces.
88 	 */
89 	skfd = socket(AF_PACKET, SOCK_RAW, 0);
90 	if (unlikely(skfd < 0)) {
91 		err(0, "%s(): open(SOCK_RAW)", __func__);
92 		return NULL;
93 	}
94 
95 	/* Initialise the Fman driver */
96 	_errno = fman_init();
97 	if (_errno) {
98 		DPAA_BUS_LOG(ERR, "FMAN driver init failed (%d)", errno);
99 		close(skfd);
100 		skfd = -1;
101 		return NULL;
102 	}
103 
104 	/* Number of MAC ports */
105 	list_for_each_entry(__if, fman_if_list, node)
106 		num_ports++;
107 
108 	if (!num_ports) {
109 		DPAA_BUS_LOG(ERR, "FMAN ports not available");
110 		return NULL;
111 	}
112 	/* Allocate space for all enabled mac ports */
113 	size = sizeof(*netcfg) +
114 		(num_ports * sizeof(struct fm_eth_port_cfg));
115 
116 	netcfg = rte_calloc(NULL, 1, size, 0);
117 	if (unlikely(netcfg == NULL)) {
118 		DPAA_BUS_LOG(ERR, "Unable to allocat mem for netcfg");
119 		goto error;
120 	}
121 
122 	netcfg->num_ethports = num_ports;
123 
124 	list_for_each_entry(__if, fman_if_list, node) {
125 		struct fm_eth_port_cfg *cfg = &netcfg->port_cfg[idx];
126 		/* Hook in the fman driver interface */
127 		cfg->fman_if = __if;
128 		cfg->rx_def = __if->fqid_rx_def;
129 		num_cfg_ports++;
130 		idx++;
131 	}
132 
133 	if (!num_cfg_ports) {
134 		DPAA_BUS_LOG(ERR, "No FMAN ports found");
135 		goto error;
136 	} else if (num_ports != num_cfg_ports)
137 		netcfg->num_ethports = num_cfg_ports;
138 
139 	return netcfg;
140 
141 error:
142 	if (netcfg) {
143 		rte_free(netcfg);
144 		netcfg = NULL;
145 	}
146 
147 	return NULL;
148 }
149 
150 void
151 netcfg_release(struct netcfg_info *cfg_ptr)
152 {
153 	rte_free(cfg_ptr);
154 	/* Close socket for shared interfaces */
155 	if (skfd >= 0) {
156 		close(skfd);
157 		skfd = -1;
158 	}
159 }
160