xref: /dpdk/drivers/net/octeontx/base/octeontx_bgx.c (revision c6b97d678699d194b54e1833e5e1c5e01ebdeca6)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2017 Cavium, Inc
3  */
4 
5 #include <string.h>
6 
7 #include "octeontx_bgx.h"
8 
9 int
10 octeontx_bgx_port_open(int port, octeontx_mbox_bgx_port_conf_t *conf)
11 {
12 	struct octeontx_mbox_hdr hdr;
13 	octeontx_mbox_bgx_port_conf_t bgx_conf;
14 	int len = sizeof(octeontx_mbox_bgx_port_conf_t);
15 	int res;
16 
17 	memset(&bgx_conf, 0, sizeof(octeontx_mbox_bgx_port_conf_t));
18 	hdr.coproc = OCTEONTX_BGX_COPROC;
19 	hdr.msg = MBOX_BGX_PORT_OPEN;
20 	hdr.vfid = port;
21 
22 	res = octeontx_mbox_send(&hdr, NULL, 0, &bgx_conf, len);
23 	if (res < 0)
24 		return -EACCES;
25 
26 	conf->enable = bgx_conf.enable;
27 	conf->promisc = bgx_conf.promisc;
28 	conf->bpen = bgx_conf.bpen;
29 	conf->node = bgx_conf.node;
30 	conf->base_chan = bgx_conf.base_chan;
31 	conf->num_chans = bgx_conf.num_chans;
32 	conf->mtu = bgx_conf.mtu;
33 	conf->bgx = bgx_conf.bgx;
34 	conf->lmac = bgx_conf.lmac;
35 	conf->mode = bgx_conf.mode;
36 	conf->pkind = bgx_conf.pkind;
37 	memcpy(conf->macaddr, bgx_conf.macaddr, 6);
38 
39 	return res;
40 }
41 
42 int
43 octeontx_bgx_port_close(int port)
44 {
45 	struct octeontx_mbox_hdr hdr;
46 	int res;
47 
48 	hdr.coproc = OCTEONTX_BGX_COPROC;
49 	hdr.msg = MBOX_BGX_PORT_CLOSE;
50 	hdr.vfid = port;
51 
52 	res = octeontx_mbox_send(&hdr, NULL, 0, NULL, 0);
53 	if (res < 0)
54 		return -EACCES;
55 
56 	return res;
57 }
58 
59 int
60 octeontx_bgx_port_start(int port)
61 {
62 	struct octeontx_mbox_hdr hdr;
63 	int res;
64 
65 	hdr.coproc = OCTEONTX_BGX_COPROC;
66 	hdr.msg = MBOX_BGX_PORT_START;
67 	hdr.vfid = port;
68 
69 	res = octeontx_mbox_send(&hdr, NULL, 0, NULL, 0);
70 	if (res < 0)
71 		return -EACCES;
72 
73 	return res;
74 }
75 
76 int
77 octeontx_bgx_port_stop(int port)
78 {
79 	struct octeontx_mbox_hdr hdr;
80 	int res;
81 
82 	hdr.coproc = OCTEONTX_BGX_COPROC;
83 	hdr.msg = MBOX_BGX_PORT_STOP;
84 	hdr.vfid = port;
85 
86 	res = octeontx_mbox_send(&hdr, NULL, 0, NULL, 0);
87 	if (res < 0)
88 		return -EACCES;
89 
90 	return res;
91 }
92 
93 int
94 octeontx_bgx_port_get_config(int port, octeontx_mbox_bgx_port_conf_t *conf)
95 {
96 	struct octeontx_mbox_hdr hdr;
97 	octeontx_mbox_bgx_port_conf_t bgx_conf;
98 	int len = sizeof(octeontx_mbox_bgx_port_conf_t);
99 	int res;
100 
101 	hdr.coproc = OCTEONTX_BGX_COPROC;
102 	hdr.msg = MBOX_BGX_PORT_GET_CONFIG;
103 	hdr.vfid = port;
104 
105 	memset(&bgx_conf, 0, sizeof(octeontx_mbox_bgx_port_conf_t));
106 	res = octeontx_mbox_send(&hdr, NULL, 0, &bgx_conf, len);
107 	if (res < 0)
108 		return -EACCES;
109 
110 	conf->enable = bgx_conf.enable;
111 	conf->promisc = bgx_conf.promisc;
112 	conf->bpen = bgx_conf.bpen;
113 	conf->node = bgx_conf.node;
114 	conf->base_chan = bgx_conf.base_chan;
115 	conf->num_chans = bgx_conf.num_chans;
116 	conf->mtu = bgx_conf.mtu;
117 	conf->bgx = bgx_conf.bgx;
118 	conf->lmac = bgx_conf.lmac;
119 	conf->mode = bgx_conf.mode;
120 	conf->pkind = bgx_conf.pkind;
121 	memcpy(conf->macaddr, bgx_conf.macaddr, 6);
122 
123 	return res;
124 }
125 
126 int
127 octeontx_bgx_port_status(int port, octeontx_mbox_bgx_port_status_t *stat)
128 {
129 	struct octeontx_mbox_hdr hdr;
130 	octeontx_mbox_bgx_port_status_t bgx_stat;
131 	int len = sizeof(octeontx_mbox_bgx_port_status_t);
132 	int res;
133 
134 	hdr.coproc = OCTEONTX_BGX_COPROC;
135 	hdr.msg = MBOX_BGX_PORT_GET_STATUS;
136 	hdr.vfid = port;
137 
138 	res = octeontx_mbox_send(&hdr, NULL, 0, &bgx_stat, len);
139 	if (res < 0)
140 		return -EACCES;
141 
142 	stat->link_up = bgx_stat.link_up;
143 
144 	return res;
145 }
146 
147 int
148 octeontx_bgx_port_xstats(int port, octeontx_mbox_bgx_port_stats_t *stats)
149 {
150 	struct octeontx_mbox_hdr hdr;
151 	int len = sizeof(octeontx_mbox_bgx_port_stats_t);
152 	int res;
153 
154 	hdr.coproc = OCTEONTX_BGX_COPROC;
155 	hdr.msg = MBOX_BGX_PORT_GET_STATS;
156 	hdr.vfid = port;
157 
158 	res = octeontx_mbox_send(&hdr, NULL, 0, stats, len);
159 	if (res < 0)
160 		return -EACCES;
161 	return res;
162 }
163 
164 int
165 octeontx_bgx_port_stats(int port, octeontx_mbox_bgx_port_stats_t *stats)
166 {
167 	struct octeontx_mbox_hdr hdr;
168 	octeontx_mbox_bgx_port_stats_t bgx_stats;
169 	int len = sizeof(octeontx_mbox_bgx_port_stats_t);
170 	int res;
171 
172 	hdr.coproc = OCTEONTX_BGX_COPROC;
173 	hdr.msg = MBOX_BGX_PORT_GET_STATS;
174 	hdr.vfid = port;
175 
176 	res = octeontx_mbox_send(&hdr, NULL, 0, &bgx_stats, len);
177 	if (res < 0)
178 		return -EACCES;
179 
180 	stats->rx_packets = bgx_stats.rx_packets;
181 	stats->rx_bytes = bgx_stats.rx_bytes;
182 	stats->rx_dropped = bgx_stats.rx_dropped;
183 	stats->rx_errors = bgx_stats.rx_errors;
184 	stats->tx_packets = bgx_stats.tx_packets;
185 	stats->tx_bytes = bgx_stats.tx_bytes;
186 	stats->tx_dropped = bgx_stats.tx_dropped;
187 	stats->tx_errors = bgx_stats.tx_errors;
188 	return res;
189 }
190 
191 int
192 octeontx_bgx_port_stats_clr(int port)
193 {
194 	struct octeontx_mbox_hdr hdr;
195 	int res;
196 
197 	hdr.coproc = OCTEONTX_BGX_COPROC;
198 	hdr.msg = MBOX_BGX_PORT_CLR_STATS;
199 	hdr.vfid = port;
200 
201 	res = octeontx_mbox_send(&hdr, NULL, 0, NULL, 0);
202 	if (res < 0)
203 		return -EACCES;
204 
205 	return res;
206 }
207 
208 int
209 octeontx_bgx_port_link_status(int port)
210 {
211 	struct octeontx_mbox_hdr hdr;
212 	uint8_t link;
213 	int len = sizeof(uint8_t);
214 	int res;
215 
216 	hdr.coproc = OCTEONTX_BGX_COPROC;
217 	hdr.msg = MBOX_BGX_PORT_GET_LINK_STATUS;
218 	hdr.vfid = port;
219 
220 	res = octeontx_mbox_send(&hdr, NULL, 0, &link, len);
221 	if (res < 0)
222 		return -EACCES;
223 
224 	return link;
225 }
226 
227 int
228 octeontx_bgx_port_set_link_state(int port, bool enable)
229 {
230 	struct octeontx_mbox_hdr hdr;
231 
232 	hdr.coproc = OCTEONTX_BGX_COPROC;
233 	hdr.msg = MBOX_BGX_PORT_SET_LINK_STATE;
234 	hdr.vfid = port;
235 
236 	return octeontx_mbox_send(&hdr, &enable, sizeof(bool), NULL, 0);
237 }
238 
239 int
240 octeontx_bgx_port_promisc_set(int port, int en)
241 {
242 	struct octeontx_mbox_hdr hdr;
243 	uint8_t	prom;
244 	int res;
245 
246 	hdr.coproc = OCTEONTX_BGX_COPROC;
247 	hdr.msg = MBOX_BGX_PORT_SET_PROMISC;
248 	hdr.vfid = port;
249 	prom = en ? 1 : 0;
250 
251 	res = octeontx_mbox_send(&hdr, &prom, sizeof(prom), NULL, 0);
252 	if (res < 0)
253 		return -EACCES;
254 
255 	return res;
256 }
257 
258 int
259 octeontx_bgx_port_mtu_set(int port, int mtu)
260 {
261 	struct octeontx_mbox_hdr hdr;
262 	int res;
263 
264 	hdr.coproc = OCTEONTX_BGX_COPROC;
265 	hdr.msg = MBOX_BGX_PORT_SET_MTU;
266 	hdr.vfid = port;
267 
268 	res = octeontx_mbox_send(&hdr, &mtu, sizeof(mtu), NULL, 0);
269 	if (res < 0)
270 		return -EACCES;
271 
272 	return res;
273 }
274 
275 int
276 octeontx_bgx_port_mac_set(int port, uint8_t *mac_addr)
277 {
278 	struct octeontx_mbox_hdr hdr;
279 	int len = 6;
280 	int res = 0;
281 
282 	hdr.coproc = OCTEONTX_BGX_COPROC;
283 	hdr.msg = MBOX_BGX_PORT_SET_MACADDR;
284 	hdr.vfid = port;
285 
286 	res = octeontx_mbox_send(&hdr, mac_addr, len, NULL, 0);
287 	if (res < 0)
288 		return -EACCES;
289 
290 	return res;
291 }
292 
293 int
294 octeontx_bgx_port_mac_add(int port, uint8_t *mac_addr, int index)
295 {
296 	struct octeontx_mbox_bgx_port_mac_filter filter;
297 	struct octeontx_mbox_hdr hdr;
298 	int len = 6;
299 
300 	hdr.coproc = OCTEONTX_BGX_COPROC;
301 	hdr.msg = MBOX_BGX_PORT_ADD_MACADDR;
302 	hdr.vfid = port;
303 
304 	memcpy(filter.mac_addr, mac_addr, len);
305 	filter.index = index;
306 	len = sizeof(struct octeontx_mbox_bgx_port_mac_filter);
307 
308 	return octeontx_mbox_send(&hdr, &filter, len, NULL, 0);
309 }
310 
311 int
312 octeontx_bgx_port_mac_del(int port, uint32_t index)
313 {
314 	struct octeontx_mbox_hdr hdr;
315 	int len = sizeof(uint32_t);
316 	int res = 0;
317 
318 	hdr.coproc = OCTEONTX_BGX_COPROC;
319 	hdr.msg = MBOX_BGX_PORT_DEL_MACADDR;
320 	hdr.vfid = port;
321 
322 	res = octeontx_mbox_send(&hdr, &index, len, NULL, 0);
323 	if (res < 0)
324 		return -EACCES;
325 
326 	return res;
327 }
328 
329 int
330 octeontx_bgx_port_mac_entries_get(int port)
331 {
332 	struct octeontx_mbox_hdr hdr;
333 	int resp = 6;
334 	int res = 0;
335 
336 	hdr.coproc = OCTEONTX_BGX_COPROC;
337 	hdr.msg = MBOX_BGX_PORT_GET_MACADDR_ENTRIES;
338 	hdr.vfid = port;
339 
340 	res = octeontx_mbox_send(&hdr, NULL, 0, &resp, sizeof(int));
341 	if (res < 0)
342 		return -EACCES;
343 
344 	return resp;
345 }
346 
347 int octeontx_bgx_port_get_fifo_cfg(int port,
348 				   octeontx_mbox_bgx_port_fifo_cfg_t *cfg)
349 {
350 	int len = sizeof(octeontx_mbox_bgx_port_fifo_cfg_t);
351 	octeontx_mbox_bgx_port_fifo_cfg_t conf;
352 	struct octeontx_mbox_hdr hdr;
353 
354 	hdr.coproc = OCTEONTX_BGX_COPROC;
355 	hdr.msg = MBOX_BGX_PORT_GET_FIFO_CFG;
356 	hdr.vfid = port;
357 
358 	if (octeontx_mbox_send(&hdr, NULL, 0, &conf, len) < 0)
359 		return -EACCES;
360 
361 	cfg->rx_fifosz = conf.rx_fifosz;
362 
363 	return 0;
364 }
365 
366 int octeontx_bgx_port_flow_ctrl_cfg(int port,
367 				    octeontx_mbox_bgx_port_fc_cfg_t *cfg)
368 {
369 	int len = sizeof(octeontx_mbox_bgx_port_fc_cfg_t);
370 	octeontx_mbox_bgx_port_fc_cfg_t conf;
371 	struct octeontx_mbox_hdr hdr;
372 
373 	hdr.coproc = OCTEONTX_BGX_COPROC;
374 	hdr.msg = MBOX_BGX_PORT_FLOW_CTRL_CFG;
375 	hdr.vfid = port;
376 
377 	if (cfg->fc_cfg == BGX_PORT_FC_CFG_SET)
378 		memcpy(&conf, cfg, len);
379 	else
380 		memset(&conf, 0, len);
381 
382 	if (octeontx_mbox_send(&hdr, &conf, len, &conf, len) < 0)
383 		return -EACCES;
384 
385 	if (cfg->fc_cfg == BGX_PORT_FC_CFG_SET)
386 		goto done;
387 
388 	cfg->rx_pause = conf.rx_pause;
389 	cfg->tx_pause = conf.tx_pause;
390 	cfg->low_water = conf.low_water;
391 	cfg->high_water = conf.high_water;
392 
393 done:
394 	return 0;
395 }
396 
397 int octeontx_bgx_port_change_mode(int port,
398 				  octeontx_mbox_bgx_port_change_mode_t *cfg)
399 {
400 	int len = sizeof(octeontx_mbox_bgx_port_change_mode_t), res;
401 	octeontx_mbox_bgx_port_change_mode_t conf;
402 	struct octeontx_mbox_hdr hdr;
403 
404 	hdr.coproc = OCTEONTX_BGX_COPROC;
405 	hdr.msg = MBOX_BGX_PORT_CHANGE_MODE;
406 	hdr.vfid = port;
407 
408 	memcpy(&conf, cfg, len);
409 	res = octeontx_mbox_send(&hdr, &conf, len, NULL, 0);
410 	if (res < 0)
411 		return -EACCES;
412 
413 	return res;
414 }
415