xref: /linux/drivers/net/ethernet/marvell/octeontx2/af/rvu_cgx.c (revision 4b660dbd9ee2059850fd30e0df420ca7a38a1856)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Marvell RVU Admin Function driver
3  *
4  * Copyright (C) 2018 Marvell.
5  *
6  */
7 
8 #include <linux/types.h>
9 #include <linux/module.h>
10 #include <linux/pci.h>
11 
12 #include "rvu.h"
13 #include "cgx.h"
14 #include "lmac_common.h"
15 #include "rvu_reg.h"
16 #include "rvu_trace.h"
17 #include "rvu_npc_hash.h"
18 
19 struct cgx_evq_entry {
20 	struct list_head evq_node;
21 	struct cgx_link_event link_event;
22 };
23 
24 #define M(_name, _id, _fn_name, _req_type, _rsp_type)			\
25 static struct _req_type __maybe_unused					\
26 *otx2_mbox_alloc_msg_ ## _fn_name(struct rvu *rvu, int devid)		\
27 {									\
28 	struct _req_type *req;						\
29 									\
30 	req = (struct _req_type *)otx2_mbox_alloc_msg_rsp(		\
31 		&rvu->afpf_wq_info.mbox_up, devid, sizeof(struct _req_type), \
32 		sizeof(struct _rsp_type));				\
33 	if (!req)							\
34 		return NULL;						\
35 	req->hdr.sig = OTX2_MBOX_REQ_SIG;				\
36 	req->hdr.id = _id;						\
37 	trace_otx2_msg_alloc(rvu->pdev, _id, sizeof(*req));		\
38 	return req;							\
39 }
40 
41 MBOX_UP_CGX_MESSAGES
42 #undef M
43 
44 bool is_mac_feature_supported(struct rvu *rvu, int pf, int feature)
45 {
46 	u8 cgx_id, lmac_id;
47 	void *cgxd;
48 
49 	if (!is_pf_cgxmapped(rvu, pf))
50 		return 0;
51 
52 	rvu_get_cgx_lmac_id(rvu->pf2cgxlmac_map[pf], &cgx_id, &lmac_id);
53 	cgxd = rvu_cgx_pdata(cgx_id, rvu);
54 
55 	return  (cgx_features_get(cgxd) & feature);
56 }
57 
58 #define CGX_OFFSET(x)			((x) * rvu->hw->lmac_per_cgx)
59 /* Returns bitmap of mapped PFs */
60 static u64 cgxlmac_to_pfmap(struct rvu *rvu, u8 cgx_id, u8 lmac_id)
61 {
62 	return rvu->cgxlmac2pf_map[CGX_OFFSET(cgx_id) + lmac_id];
63 }
64 
65 int cgxlmac_to_pf(struct rvu *rvu, int cgx_id, int lmac_id)
66 {
67 	unsigned long pfmap;
68 
69 	pfmap = cgxlmac_to_pfmap(rvu, cgx_id, lmac_id);
70 
71 	/* Assumes only one pf mapped to a cgx lmac port */
72 	if (!pfmap)
73 		return -ENODEV;
74 	else
75 		return find_first_bit(&pfmap,
76 				      rvu->cgx_cnt_max * rvu->hw->lmac_per_cgx);
77 }
78 
79 static u8 cgxlmac_id_to_bmap(u8 cgx_id, u8 lmac_id)
80 {
81 	return ((cgx_id & 0xF) << 4) | (lmac_id & 0xF);
82 }
83 
84 void *rvu_cgx_pdata(u8 cgx_id, struct rvu *rvu)
85 {
86 	if (cgx_id >= rvu->cgx_cnt_max)
87 		return NULL;
88 
89 	return rvu->cgx_idmap[cgx_id];
90 }
91 
92 /* Return first enabled CGX instance if none are enabled then return NULL */
93 void *rvu_first_cgx_pdata(struct rvu *rvu)
94 {
95 	int first_enabled_cgx = 0;
96 	void *cgxd = NULL;
97 
98 	for (; first_enabled_cgx < rvu->cgx_cnt_max; first_enabled_cgx++) {
99 		cgxd = rvu_cgx_pdata(first_enabled_cgx, rvu);
100 		if (cgxd)
101 			break;
102 	}
103 
104 	return cgxd;
105 }
106 
107 /* Based on P2X connectivity find mapped NIX block for a PF */
108 static void rvu_map_cgx_nix_block(struct rvu *rvu, int pf,
109 				  int cgx_id, int lmac_id)
110 {
111 	struct rvu_pfvf *pfvf = &rvu->pf[pf];
112 	u8 p2x;
113 
114 	p2x = cgx_lmac_get_p2x(cgx_id, lmac_id);
115 	/* Firmware sets P2X_SELECT as either NIX0 or NIX1 */
116 	pfvf->nix_blkaddr = BLKADDR_NIX0;
117 	if (is_rvu_supports_nix1(rvu) && p2x == CMR_P2X_SEL_NIX1)
118 		pfvf->nix_blkaddr = BLKADDR_NIX1;
119 }
120 
121 static int rvu_map_cgx_lmac_pf(struct rvu *rvu)
122 {
123 	struct npc_pkind *pkind = &rvu->hw->pkind;
124 	int cgx_cnt_max = rvu->cgx_cnt_max;
125 	int pf = PF_CGXMAP_BASE;
126 	unsigned long lmac_bmap;
127 	int size, free_pkind;
128 	int cgx, lmac, iter;
129 	int numvfs, hwvfs;
130 
131 	if (!cgx_cnt_max)
132 		return 0;
133 
134 	if (cgx_cnt_max > 0xF || rvu->hw->lmac_per_cgx > 0xF)
135 		return -EINVAL;
136 
137 	/* Alloc map table
138 	 * An additional entry is required since PF id starts from 1 and
139 	 * hence entry at offset 0 is invalid.
140 	 */
141 	size = (cgx_cnt_max * rvu->hw->lmac_per_cgx + 1) * sizeof(u8);
142 	rvu->pf2cgxlmac_map = devm_kmalloc(rvu->dev, size, GFP_KERNEL);
143 	if (!rvu->pf2cgxlmac_map)
144 		return -ENOMEM;
145 
146 	/* Initialize all entries with an invalid cgx and lmac id */
147 	memset(rvu->pf2cgxlmac_map, 0xFF, size);
148 
149 	/* Reverse map table */
150 	rvu->cgxlmac2pf_map =
151 		devm_kzalloc(rvu->dev,
152 			     cgx_cnt_max * rvu->hw->lmac_per_cgx * sizeof(u64),
153 			     GFP_KERNEL);
154 	if (!rvu->cgxlmac2pf_map)
155 		return -ENOMEM;
156 
157 	rvu->cgx_mapped_pfs = 0;
158 	for (cgx = 0; cgx < cgx_cnt_max; cgx++) {
159 		if (!rvu_cgx_pdata(cgx, rvu))
160 			continue;
161 		lmac_bmap = cgx_get_lmac_bmap(rvu_cgx_pdata(cgx, rvu));
162 		for_each_set_bit(iter, &lmac_bmap, rvu->hw->lmac_per_cgx) {
163 			lmac = cgx_get_lmacid(rvu_cgx_pdata(cgx, rvu),
164 					      iter);
165 			rvu->pf2cgxlmac_map[pf] = cgxlmac_id_to_bmap(cgx, lmac);
166 			rvu->cgxlmac2pf_map[CGX_OFFSET(cgx) + lmac] = 1 << pf;
167 			free_pkind = rvu_alloc_rsrc(&pkind->rsrc);
168 			pkind->pfchan_map[free_pkind] = ((pf) & 0x3F) << 16;
169 			rvu_map_cgx_nix_block(rvu, pf, cgx, lmac);
170 			rvu->cgx_mapped_pfs++;
171 			rvu_get_pf_numvfs(rvu, pf, &numvfs, &hwvfs);
172 			rvu->cgx_mapped_vfs += numvfs;
173 			pf++;
174 		}
175 	}
176 	return 0;
177 }
178 
179 static int rvu_cgx_send_link_info(int cgx_id, int lmac_id, struct rvu *rvu)
180 {
181 	struct cgx_evq_entry *qentry;
182 	unsigned long flags;
183 	int err;
184 
185 	qentry = kmalloc(sizeof(*qentry), GFP_KERNEL);
186 	if (!qentry)
187 		return -ENOMEM;
188 
189 	/* Lock the event queue before we read the local link status */
190 	spin_lock_irqsave(&rvu->cgx_evq_lock, flags);
191 	err = cgx_get_link_info(rvu_cgx_pdata(cgx_id, rvu), lmac_id,
192 				&qentry->link_event.link_uinfo);
193 	qentry->link_event.cgx_id = cgx_id;
194 	qentry->link_event.lmac_id = lmac_id;
195 	if (err) {
196 		kfree(qentry);
197 		goto skip_add;
198 	}
199 	list_add_tail(&qentry->evq_node, &rvu->cgx_evq_head);
200 skip_add:
201 	spin_unlock_irqrestore(&rvu->cgx_evq_lock, flags);
202 
203 	/* start worker to process the events */
204 	queue_work(rvu->cgx_evh_wq, &rvu->cgx_evh_work);
205 
206 	return 0;
207 }
208 
209 /* This is called from interrupt context and is expected to be atomic */
210 static int cgx_lmac_postevent(struct cgx_link_event *event, void *data)
211 {
212 	struct cgx_evq_entry *qentry;
213 	struct rvu *rvu = data;
214 
215 	/* post event to the event queue */
216 	qentry = kmalloc(sizeof(*qentry), GFP_ATOMIC);
217 	if (!qentry)
218 		return -ENOMEM;
219 	qentry->link_event = *event;
220 	spin_lock(&rvu->cgx_evq_lock);
221 	list_add_tail(&qentry->evq_node, &rvu->cgx_evq_head);
222 	spin_unlock(&rvu->cgx_evq_lock);
223 
224 	/* start worker to process the events */
225 	queue_work(rvu->cgx_evh_wq, &rvu->cgx_evh_work);
226 
227 	return 0;
228 }
229 
230 static void cgx_notify_pfs(struct cgx_link_event *event, struct rvu *rvu)
231 {
232 	struct cgx_link_user_info *linfo;
233 	struct cgx_link_info_msg *msg;
234 	unsigned long pfmap;
235 	int pfid;
236 
237 	linfo = &event->link_uinfo;
238 	pfmap = cgxlmac_to_pfmap(rvu, event->cgx_id, event->lmac_id);
239 	if (!pfmap) {
240 		dev_err(rvu->dev, "CGX port%d:%d not mapped with PF\n",
241 			event->cgx_id, event->lmac_id);
242 		return;
243 	}
244 
245 	do {
246 		pfid = find_first_bit(&pfmap,
247 				      rvu->cgx_cnt_max * rvu->hw->lmac_per_cgx);
248 		clear_bit(pfid, &pfmap);
249 
250 		/* check if notification is enabled */
251 		if (!test_bit(pfid, &rvu->pf_notify_bmap)) {
252 			dev_info(rvu->dev, "cgx %d: lmac %d Link status %s\n",
253 				 event->cgx_id, event->lmac_id,
254 				 linfo->link_up ? "UP" : "DOWN");
255 			continue;
256 		}
257 
258 		mutex_lock(&rvu->mbox_lock);
259 
260 		/* Send mbox message to PF */
261 		msg = otx2_mbox_alloc_msg_cgx_link_event(rvu, pfid);
262 		if (!msg) {
263 			mutex_unlock(&rvu->mbox_lock);
264 			continue;
265 		}
266 
267 		msg->link_info = *linfo;
268 
269 		otx2_mbox_wait_for_zero(&rvu->afpf_wq_info.mbox_up, pfid);
270 
271 		otx2_mbox_msg_send_up(&rvu->afpf_wq_info.mbox_up, pfid);
272 
273 		mutex_unlock(&rvu->mbox_lock);
274 	} while (pfmap);
275 }
276 
277 static void cgx_evhandler_task(struct work_struct *work)
278 {
279 	struct rvu *rvu = container_of(work, struct rvu, cgx_evh_work);
280 	struct cgx_evq_entry *qentry;
281 	struct cgx_link_event *event;
282 	unsigned long flags;
283 
284 	do {
285 		/* Dequeue an event */
286 		spin_lock_irqsave(&rvu->cgx_evq_lock, flags);
287 		qentry = list_first_entry_or_null(&rvu->cgx_evq_head,
288 						  struct cgx_evq_entry,
289 						  evq_node);
290 		if (qentry)
291 			list_del(&qentry->evq_node);
292 		spin_unlock_irqrestore(&rvu->cgx_evq_lock, flags);
293 		if (!qentry)
294 			break; /* nothing more to process */
295 
296 		event = &qentry->link_event;
297 
298 		/* process event */
299 		cgx_notify_pfs(event, rvu);
300 		kfree(qentry);
301 	} while (1);
302 }
303 
304 static int cgx_lmac_event_handler_init(struct rvu *rvu)
305 {
306 	unsigned long lmac_bmap;
307 	struct cgx_event_cb cb;
308 	int cgx, lmac, err;
309 	void *cgxd;
310 
311 	spin_lock_init(&rvu->cgx_evq_lock);
312 	INIT_LIST_HEAD(&rvu->cgx_evq_head);
313 	INIT_WORK(&rvu->cgx_evh_work, cgx_evhandler_task);
314 	rvu->cgx_evh_wq = alloc_workqueue("rvu_evh_wq", 0, 0);
315 	if (!rvu->cgx_evh_wq) {
316 		dev_err(rvu->dev, "alloc workqueue failed");
317 		return -ENOMEM;
318 	}
319 
320 	cb.notify_link_chg = cgx_lmac_postevent; /* link change call back */
321 	cb.data = rvu;
322 
323 	for (cgx = 0; cgx <= rvu->cgx_cnt_max; cgx++) {
324 		cgxd = rvu_cgx_pdata(cgx, rvu);
325 		if (!cgxd)
326 			continue;
327 		lmac_bmap = cgx_get_lmac_bmap(cgxd);
328 		for_each_set_bit(lmac, &lmac_bmap, rvu->hw->lmac_per_cgx) {
329 			err = cgx_lmac_evh_register(&cb, cgxd, lmac);
330 			if (err)
331 				dev_err(rvu->dev,
332 					"%d:%d handler register failed\n",
333 					cgx, lmac);
334 		}
335 	}
336 
337 	return 0;
338 }
339 
340 static void rvu_cgx_wq_destroy(struct rvu *rvu)
341 {
342 	if (rvu->cgx_evh_wq) {
343 		destroy_workqueue(rvu->cgx_evh_wq);
344 		rvu->cgx_evh_wq = NULL;
345 	}
346 }
347 
348 int rvu_cgx_init(struct rvu *rvu)
349 {
350 	int cgx, err;
351 	void *cgxd;
352 
353 	/* CGX port id starts from 0 and are not necessarily contiguous
354 	 * Hence we allocate resources based on the maximum port id value.
355 	 */
356 	rvu->cgx_cnt_max = cgx_get_cgxcnt_max();
357 	if (!rvu->cgx_cnt_max) {
358 		dev_info(rvu->dev, "No CGX devices found!\n");
359 		return 0;
360 	}
361 
362 	rvu->cgx_idmap = devm_kzalloc(rvu->dev, rvu->cgx_cnt_max *
363 				      sizeof(void *), GFP_KERNEL);
364 	if (!rvu->cgx_idmap)
365 		return -ENOMEM;
366 
367 	/* Initialize the cgxdata table */
368 	for (cgx = 0; cgx < rvu->cgx_cnt_max; cgx++)
369 		rvu->cgx_idmap[cgx] = cgx_get_pdata(cgx);
370 
371 	/* Map CGX LMAC interfaces to RVU PFs */
372 	err = rvu_map_cgx_lmac_pf(rvu);
373 	if (err)
374 		return err;
375 
376 	/* Register for CGX events */
377 	err = cgx_lmac_event_handler_init(rvu);
378 	if (err)
379 		return err;
380 
381 	mutex_init(&rvu->cgx_cfg_lock);
382 
383 	/* Ensure event handler registration is completed, before
384 	 * we turn on the links
385 	 */
386 	mb();
387 
388 	/* Do link up for all CGX ports */
389 	for (cgx = 0; cgx <= rvu->cgx_cnt_max; cgx++) {
390 		cgxd = rvu_cgx_pdata(cgx, rvu);
391 		if (!cgxd)
392 			continue;
393 		err = cgx_lmac_linkup_start(cgxd);
394 		if (err)
395 			dev_err(rvu->dev,
396 				"Link up process failed to start on cgx %d\n",
397 				cgx);
398 	}
399 
400 	return 0;
401 }
402 
403 int rvu_cgx_exit(struct rvu *rvu)
404 {
405 	unsigned long lmac_bmap;
406 	int cgx, lmac;
407 	void *cgxd;
408 
409 	for (cgx = 0; cgx <= rvu->cgx_cnt_max; cgx++) {
410 		cgxd = rvu_cgx_pdata(cgx, rvu);
411 		if (!cgxd)
412 			continue;
413 		lmac_bmap = cgx_get_lmac_bmap(cgxd);
414 		for_each_set_bit(lmac, &lmac_bmap, rvu->hw->lmac_per_cgx)
415 			cgx_lmac_evh_unregister(cgxd, lmac);
416 	}
417 
418 	/* Ensure event handler unregister is completed */
419 	mb();
420 
421 	rvu_cgx_wq_destroy(rvu);
422 	return 0;
423 }
424 
425 /* Most of the CGX configuration is restricted to the mapped PF only,
426  * VF's of mapped PF and other PFs are not allowed. This fn() checks
427  * whether a PFFUNC is permitted to do the config or not.
428  */
429 inline bool is_cgx_config_permitted(struct rvu *rvu, u16 pcifunc)
430 {
431 	if ((pcifunc & RVU_PFVF_FUNC_MASK) ||
432 	    !is_pf_cgxmapped(rvu, rvu_get_pf(pcifunc)))
433 		return false;
434 	return true;
435 }
436 
437 void rvu_cgx_enadis_rx_bp(struct rvu *rvu, int pf, bool enable)
438 {
439 	struct mac_ops *mac_ops;
440 	u8 cgx_id, lmac_id;
441 	void *cgxd;
442 
443 	if (!is_pf_cgxmapped(rvu, pf))
444 		return;
445 
446 	rvu_get_cgx_lmac_id(rvu->pf2cgxlmac_map[pf], &cgx_id, &lmac_id);
447 	cgxd = rvu_cgx_pdata(cgx_id, rvu);
448 
449 	mac_ops = get_mac_ops(cgxd);
450 	/* Set / clear CTL_BCK to control pause frame forwarding to NIX */
451 	if (enable)
452 		mac_ops->mac_enadis_rx_pause_fwding(cgxd, lmac_id, true);
453 	else
454 		mac_ops->mac_enadis_rx_pause_fwding(cgxd, lmac_id, false);
455 }
456 
457 int rvu_cgx_config_rxtx(struct rvu *rvu, u16 pcifunc, bool start)
458 {
459 	int pf = rvu_get_pf(pcifunc);
460 	struct mac_ops *mac_ops;
461 	u8 cgx_id, lmac_id;
462 	void *cgxd;
463 
464 	if (!is_cgx_config_permitted(rvu, pcifunc))
465 		return LMAC_AF_ERR_PERM_DENIED;
466 
467 	rvu_get_cgx_lmac_id(rvu->pf2cgxlmac_map[pf], &cgx_id, &lmac_id);
468 	cgxd = rvu_cgx_pdata(cgx_id, rvu);
469 	mac_ops = get_mac_ops(cgxd);
470 
471 	return mac_ops->mac_rx_tx_enable(cgxd, lmac_id, start);
472 }
473 
474 int rvu_cgx_tx_enable(struct rvu *rvu, u16 pcifunc, bool enable)
475 {
476 	int pf = rvu_get_pf(pcifunc);
477 	struct mac_ops *mac_ops;
478 	u8 cgx_id, lmac_id;
479 	void *cgxd;
480 
481 	if (!is_cgx_config_permitted(rvu, pcifunc))
482 		return LMAC_AF_ERR_PERM_DENIED;
483 
484 	rvu_get_cgx_lmac_id(rvu->pf2cgxlmac_map[pf], &cgx_id, &lmac_id);
485 	cgxd = rvu_cgx_pdata(cgx_id, rvu);
486 	mac_ops = get_mac_ops(cgxd);
487 
488 	return mac_ops->mac_tx_enable(cgxd, lmac_id, enable);
489 }
490 
491 int rvu_cgx_config_tx(void *cgxd, int lmac_id, bool enable)
492 {
493 	struct mac_ops *mac_ops;
494 
495 	mac_ops = get_mac_ops(cgxd);
496 	return mac_ops->mac_tx_enable(cgxd, lmac_id, enable);
497 }
498 
499 void rvu_cgx_disable_dmac_entries(struct rvu *rvu, u16 pcifunc)
500 {
501 	int pf = rvu_get_pf(pcifunc);
502 	int i = 0, lmac_count = 0;
503 	struct mac_ops *mac_ops;
504 	u8 max_dmac_filters;
505 	u8 cgx_id, lmac_id;
506 	void *cgx_dev;
507 
508 	if (!is_cgx_config_permitted(rvu, pcifunc))
509 		return;
510 
511 	if (rvu_npc_exact_has_match_table(rvu)) {
512 		rvu_npc_exact_reset(rvu, pcifunc);
513 		return;
514 	}
515 
516 	rvu_get_cgx_lmac_id(rvu->pf2cgxlmac_map[pf], &cgx_id, &lmac_id);
517 	cgx_dev = cgx_get_pdata(cgx_id);
518 	lmac_count = cgx_get_lmac_cnt(cgx_dev);
519 
520 	mac_ops = get_mac_ops(cgx_dev);
521 	if (!mac_ops)
522 		return;
523 
524 	max_dmac_filters = mac_ops->dmac_filter_count / lmac_count;
525 
526 	for (i = 0; i < max_dmac_filters; i++)
527 		cgx_lmac_addr_del(cgx_id, lmac_id, i);
528 
529 	/* As cgx_lmac_addr_del does not clear entry for index 0
530 	 * so it needs to be done explicitly
531 	 */
532 	cgx_lmac_addr_reset(cgx_id, lmac_id);
533 }
534 
535 int rvu_mbox_handler_cgx_start_rxtx(struct rvu *rvu, struct msg_req *req,
536 				    struct msg_rsp *rsp)
537 {
538 	rvu_cgx_config_rxtx(rvu, req->hdr.pcifunc, true);
539 	return 0;
540 }
541 
542 int rvu_mbox_handler_cgx_stop_rxtx(struct rvu *rvu, struct msg_req *req,
543 				   struct msg_rsp *rsp)
544 {
545 	rvu_cgx_config_rxtx(rvu, req->hdr.pcifunc, false);
546 	return 0;
547 }
548 
549 static int rvu_lmac_get_stats(struct rvu *rvu, struct msg_req *req,
550 			      void *rsp)
551 {
552 	int pf = rvu_get_pf(req->hdr.pcifunc);
553 	struct mac_ops *mac_ops;
554 	int stat = 0, err = 0;
555 	u64 tx_stat, rx_stat;
556 	u8 cgx_idx, lmac;
557 	void *cgxd;
558 
559 	if (!is_cgx_config_permitted(rvu, req->hdr.pcifunc))
560 		return LMAC_AF_ERR_PERM_DENIED;
561 
562 	rvu_get_cgx_lmac_id(rvu->pf2cgxlmac_map[pf], &cgx_idx, &lmac);
563 	cgxd = rvu_cgx_pdata(cgx_idx, rvu);
564 	mac_ops = get_mac_ops(cgxd);
565 
566 	/* Rx stats */
567 	while (stat < mac_ops->rx_stats_cnt) {
568 		err = mac_ops->mac_get_rx_stats(cgxd, lmac, stat, &rx_stat);
569 		if (err)
570 			return err;
571 		if (mac_ops->rx_stats_cnt == RPM_RX_STATS_COUNT)
572 			((struct rpm_stats_rsp *)rsp)->rx_stats[stat] = rx_stat;
573 		else
574 			((struct cgx_stats_rsp *)rsp)->rx_stats[stat] = rx_stat;
575 		stat++;
576 	}
577 
578 	/* Tx stats */
579 	stat = 0;
580 	while (stat < mac_ops->tx_stats_cnt) {
581 		err = mac_ops->mac_get_tx_stats(cgxd, lmac, stat, &tx_stat);
582 		if (err)
583 			return err;
584 		if (mac_ops->tx_stats_cnt == RPM_TX_STATS_COUNT)
585 			((struct rpm_stats_rsp *)rsp)->tx_stats[stat] = tx_stat;
586 		else
587 			((struct cgx_stats_rsp *)rsp)->tx_stats[stat] = tx_stat;
588 		stat++;
589 	}
590 	return 0;
591 }
592 
593 int rvu_mbox_handler_cgx_stats(struct rvu *rvu, struct msg_req *req,
594 			       struct cgx_stats_rsp *rsp)
595 {
596 	return rvu_lmac_get_stats(rvu, req, (void *)rsp);
597 }
598 
599 int rvu_mbox_handler_rpm_stats(struct rvu *rvu, struct msg_req *req,
600 			       struct rpm_stats_rsp *rsp)
601 {
602 	return rvu_lmac_get_stats(rvu, req, (void *)rsp);
603 }
604 
605 int rvu_mbox_handler_cgx_fec_stats(struct rvu *rvu,
606 				   struct msg_req *req,
607 				   struct cgx_fec_stats_rsp *rsp)
608 {
609 	int pf = rvu_get_pf(req->hdr.pcifunc);
610 	struct mac_ops *mac_ops;
611 	u8 cgx_idx, lmac;
612 	void *cgxd;
613 
614 	if (!is_cgx_config_permitted(rvu, req->hdr.pcifunc))
615 		return LMAC_AF_ERR_PERM_DENIED;
616 	rvu_get_cgx_lmac_id(rvu->pf2cgxlmac_map[pf], &cgx_idx, &lmac);
617 
618 	cgxd = rvu_cgx_pdata(cgx_idx, rvu);
619 	mac_ops = get_mac_ops(cgxd);
620 	return  mac_ops->get_fec_stats(cgxd, lmac, rsp);
621 }
622 
623 int rvu_mbox_handler_cgx_mac_addr_set(struct rvu *rvu,
624 				      struct cgx_mac_addr_set_or_get *req,
625 				      struct cgx_mac_addr_set_or_get *rsp)
626 {
627 	int pf = rvu_get_pf(req->hdr.pcifunc);
628 	u8 cgx_id, lmac_id;
629 
630 	if (!is_cgx_config_permitted(rvu, req->hdr.pcifunc))
631 		return -EPERM;
632 
633 	if (rvu_npc_exact_has_match_table(rvu))
634 		return rvu_npc_exact_mac_addr_set(rvu, req, rsp);
635 
636 	rvu_get_cgx_lmac_id(rvu->pf2cgxlmac_map[pf], &cgx_id, &lmac_id);
637 
638 	cgx_lmac_addr_set(cgx_id, lmac_id, req->mac_addr);
639 
640 	return 0;
641 }
642 
643 int rvu_mbox_handler_cgx_mac_addr_add(struct rvu *rvu,
644 				      struct cgx_mac_addr_add_req *req,
645 				      struct cgx_mac_addr_add_rsp *rsp)
646 {
647 	int pf = rvu_get_pf(req->hdr.pcifunc);
648 	u8 cgx_id, lmac_id;
649 	int rc = 0;
650 
651 	if (!is_cgx_config_permitted(rvu, req->hdr.pcifunc))
652 		return -EPERM;
653 
654 	if (rvu_npc_exact_has_match_table(rvu))
655 		return rvu_npc_exact_mac_addr_add(rvu, req, rsp);
656 
657 	rvu_get_cgx_lmac_id(rvu->pf2cgxlmac_map[pf], &cgx_id, &lmac_id);
658 	rc = cgx_lmac_addr_add(cgx_id, lmac_id, req->mac_addr);
659 	if (rc >= 0) {
660 		rsp->index = rc;
661 		return 0;
662 	}
663 
664 	return rc;
665 }
666 
667 int rvu_mbox_handler_cgx_mac_addr_del(struct rvu *rvu,
668 				      struct cgx_mac_addr_del_req *req,
669 				      struct msg_rsp *rsp)
670 {
671 	int pf = rvu_get_pf(req->hdr.pcifunc);
672 	u8 cgx_id, lmac_id;
673 
674 	if (!is_cgx_config_permitted(rvu, req->hdr.pcifunc))
675 		return -EPERM;
676 
677 	if (rvu_npc_exact_has_match_table(rvu))
678 		return rvu_npc_exact_mac_addr_del(rvu, req, rsp);
679 
680 	rvu_get_cgx_lmac_id(rvu->pf2cgxlmac_map[pf], &cgx_id, &lmac_id);
681 	return cgx_lmac_addr_del(cgx_id, lmac_id, req->index);
682 }
683 
684 int rvu_mbox_handler_cgx_mac_max_entries_get(struct rvu *rvu,
685 					     struct msg_req *req,
686 					     struct cgx_max_dmac_entries_get_rsp
687 					     *rsp)
688 {
689 	int pf = rvu_get_pf(req->hdr.pcifunc);
690 	u8 cgx_id, lmac_id;
691 
692 	/* If msg is received from PFs(which are not mapped to CGX LMACs)
693 	 * or VF then no entries are allocated for DMAC filters at CGX level.
694 	 * So returning zero.
695 	 */
696 	if (!is_cgx_config_permitted(rvu, req->hdr.pcifunc)) {
697 		rsp->max_dmac_filters = 0;
698 		return 0;
699 	}
700 
701 	if (rvu_npc_exact_has_match_table(rvu)) {
702 		rsp->max_dmac_filters = rvu_npc_exact_get_max_entries(rvu);
703 		return 0;
704 	}
705 
706 	rvu_get_cgx_lmac_id(rvu->pf2cgxlmac_map[pf], &cgx_id, &lmac_id);
707 	rsp->max_dmac_filters = cgx_lmac_addr_max_entries_get(cgx_id, lmac_id);
708 	return 0;
709 }
710 
711 int rvu_mbox_handler_cgx_mac_addr_get(struct rvu *rvu,
712 				      struct cgx_mac_addr_set_or_get *req,
713 				      struct cgx_mac_addr_set_or_get *rsp)
714 {
715 	int pf = rvu_get_pf(req->hdr.pcifunc);
716 	u8 cgx_id, lmac_id;
717 	int rc = 0;
718 	u64 cfg;
719 
720 	if (!is_cgx_config_permitted(rvu, req->hdr.pcifunc))
721 		return -EPERM;
722 
723 	rvu_get_cgx_lmac_id(rvu->pf2cgxlmac_map[pf], &cgx_id, &lmac_id);
724 
725 	rsp->hdr.rc = rc;
726 	cfg = cgx_lmac_addr_get(cgx_id, lmac_id);
727 	/* copy 48 bit mac address to req->mac_addr */
728 	u64_to_ether_addr(cfg, rsp->mac_addr);
729 	return 0;
730 }
731 
732 int rvu_mbox_handler_cgx_promisc_enable(struct rvu *rvu, struct msg_req *req,
733 					struct msg_rsp *rsp)
734 {
735 	u16 pcifunc = req->hdr.pcifunc;
736 	int pf = rvu_get_pf(pcifunc);
737 	u8 cgx_id, lmac_id;
738 
739 	if (!is_cgx_config_permitted(rvu, req->hdr.pcifunc))
740 		return -EPERM;
741 
742 	/* Disable drop on non hit rule */
743 	if (rvu_npc_exact_has_match_table(rvu))
744 		return rvu_npc_exact_promisc_enable(rvu, req->hdr.pcifunc);
745 
746 	rvu_get_cgx_lmac_id(rvu->pf2cgxlmac_map[pf], &cgx_id, &lmac_id);
747 
748 	cgx_lmac_promisc_config(cgx_id, lmac_id, true);
749 	return 0;
750 }
751 
752 int rvu_mbox_handler_cgx_promisc_disable(struct rvu *rvu, struct msg_req *req,
753 					 struct msg_rsp *rsp)
754 {
755 	int pf = rvu_get_pf(req->hdr.pcifunc);
756 	u8 cgx_id, lmac_id;
757 
758 	if (!is_cgx_config_permitted(rvu, req->hdr.pcifunc))
759 		return -EPERM;
760 
761 	/* Disable drop on non hit rule */
762 	if (rvu_npc_exact_has_match_table(rvu))
763 		return rvu_npc_exact_promisc_disable(rvu, req->hdr.pcifunc);
764 
765 	rvu_get_cgx_lmac_id(rvu->pf2cgxlmac_map[pf], &cgx_id, &lmac_id);
766 
767 	cgx_lmac_promisc_config(cgx_id, lmac_id, false);
768 	return 0;
769 }
770 
771 static int rvu_cgx_ptp_rx_cfg(struct rvu *rvu, u16 pcifunc, bool enable)
772 {
773 	struct rvu_pfvf *pfvf = rvu_get_pfvf(rvu, pcifunc);
774 	int pf = rvu_get_pf(pcifunc);
775 	struct mac_ops *mac_ops;
776 	u8 cgx_id, lmac_id;
777 	void *cgxd;
778 
779 	if (!is_mac_feature_supported(rvu, pf, RVU_LMAC_FEAT_PTP))
780 		return 0;
781 
782 	/* This msg is expected only from PF/VFs that are mapped to CGX/RPM LMACs,
783 	 * if received from other PF/VF simply ACK, nothing to do.
784 	 */
785 	if (!is_pf_cgxmapped(rvu, pf))
786 		return -EPERM;
787 
788 	rvu_get_cgx_lmac_id(rvu->pf2cgxlmac_map[pf], &cgx_id, &lmac_id);
789 	cgxd = rvu_cgx_pdata(cgx_id, rvu);
790 
791 	mac_ops = get_mac_ops(cgxd);
792 	mac_ops->mac_enadis_ptp_config(cgxd, lmac_id, enable);
793 	/* If PTP is enabled then inform NPC that packets to be
794 	 * parsed by this PF will have their data shifted by 8 bytes
795 	 * and if PTP is disabled then no shift is required
796 	 */
797 	if (npc_config_ts_kpuaction(rvu, pf, pcifunc, enable))
798 		return -EINVAL;
799 	/* This flag is required to clean up CGX conf if app gets killed */
800 	pfvf->hw_rx_tstamp_en = enable;
801 
802 	/* Inform MCS about 8B RX header */
803 	rvu_mcs_ptp_cfg(rvu, cgx_id, lmac_id, enable);
804 	return 0;
805 }
806 
807 int rvu_mbox_handler_cgx_ptp_rx_enable(struct rvu *rvu, struct msg_req *req,
808 				       struct msg_rsp *rsp)
809 {
810 	if (!is_pf_cgxmapped(rvu, rvu_get_pf(req->hdr.pcifunc)))
811 		return -EPERM;
812 
813 	return rvu_cgx_ptp_rx_cfg(rvu, req->hdr.pcifunc, true);
814 }
815 
816 int rvu_mbox_handler_cgx_ptp_rx_disable(struct rvu *rvu, struct msg_req *req,
817 					struct msg_rsp *rsp)
818 {
819 	return rvu_cgx_ptp_rx_cfg(rvu, req->hdr.pcifunc, false);
820 }
821 
822 static int rvu_cgx_config_linkevents(struct rvu *rvu, u16 pcifunc, bool en)
823 {
824 	int pf = rvu_get_pf(pcifunc);
825 	u8 cgx_id, lmac_id;
826 
827 	if (!is_cgx_config_permitted(rvu, pcifunc))
828 		return -EPERM;
829 
830 	rvu_get_cgx_lmac_id(rvu->pf2cgxlmac_map[pf], &cgx_id, &lmac_id);
831 
832 	if (en) {
833 		set_bit(pf, &rvu->pf_notify_bmap);
834 		/* Send the current link status to PF */
835 		rvu_cgx_send_link_info(cgx_id, lmac_id, rvu);
836 	} else {
837 		clear_bit(pf, &rvu->pf_notify_bmap);
838 	}
839 
840 	return 0;
841 }
842 
843 int rvu_mbox_handler_cgx_start_linkevents(struct rvu *rvu, struct msg_req *req,
844 					  struct msg_rsp *rsp)
845 {
846 	rvu_cgx_config_linkevents(rvu, req->hdr.pcifunc, true);
847 	return 0;
848 }
849 
850 int rvu_mbox_handler_cgx_stop_linkevents(struct rvu *rvu, struct msg_req *req,
851 					 struct msg_rsp *rsp)
852 {
853 	rvu_cgx_config_linkevents(rvu, req->hdr.pcifunc, false);
854 	return 0;
855 }
856 
857 int rvu_mbox_handler_cgx_get_linkinfo(struct rvu *rvu, struct msg_req *req,
858 				      struct cgx_link_info_msg *rsp)
859 {
860 	u8 cgx_id, lmac_id;
861 	int pf, err;
862 
863 	pf = rvu_get_pf(req->hdr.pcifunc);
864 
865 	if (!is_pf_cgxmapped(rvu, pf))
866 		return -ENODEV;
867 
868 	rvu_get_cgx_lmac_id(rvu->pf2cgxlmac_map[pf], &cgx_id, &lmac_id);
869 
870 	err = cgx_get_link_info(rvu_cgx_pdata(cgx_id, rvu), lmac_id,
871 				&rsp->link_info);
872 	return err;
873 }
874 
875 int rvu_mbox_handler_cgx_features_get(struct rvu *rvu,
876 				      struct msg_req *req,
877 				      struct cgx_features_info_msg *rsp)
878 {
879 	int pf = rvu_get_pf(req->hdr.pcifunc);
880 	u8 cgx_idx, lmac;
881 	void *cgxd;
882 
883 	if (!is_pf_cgxmapped(rvu, pf))
884 		return 0;
885 
886 	rvu_get_cgx_lmac_id(rvu->pf2cgxlmac_map[pf], &cgx_idx, &lmac);
887 	cgxd = rvu_cgx_pdata(cgx_idx, rvu);
888 	rsp->lmac_features = cgx_features_get(cgxd);
889 
890 	return 0;
891 }
892 
893 u32 rvu_cgx_get_fifolen(struct rvu *rvu)
894 {
895 	struct mac_ops *mac_ops;
896 	u32 fifo_len;
897 
898 	mac_ops = get_mac_ops(rvu_first_cgx_pdata(rvu));
899 	fifo_len = mac_ops ? mac_ops->fifo_len : 0;
900 
901 	return fifo_len;
902 }
903 
904 u32 rvu_cgx_get_lmac_fifolen(struct rvu *rvu, int cgx, int lmac)
905 {
906 	struct mac_ops *mac_ops;
907 	void *cgxd;
908 
909 	cgxd = rvu_cgx_pdata(cgx, rvu);
910 	if (!cgxd)
911 		return 0;
912 
913 	mac_ops = get_mac_ops(cgxd);
914 	if (!mac_ops->lmac_fifo_len)
915 		return 0;
916 
917 	return mac_ops->lmac_fifo_len(cgxd, lmac);
918 }
919 
920 static int rvu_cgx_config_intlbk(struct rvu *rvu, u16 pcifunc, bool en)
921 {
922 	int pf = rvu_get_pf(pcifunc);
923 	struct mac_ops *mac_ops;
924 	u8 cgx_id, lmac_id;
925 
926 	if (!is_cgx_config_permitted(rvu, pcifunc))
927 		return -EPERM;
928 
929 	rvu_get_cgx_lmac_id(rvu->pf2cgxlmac_map[pf], &cgx_id, &lmac_id);
930 	mac_ops = get_mac_ops(rvu_cgx_pdata(cgx_id, rvu));
931 
932 	return mac_ops->mac_lmac_intl_lbk(rvu_cgx_pdata(cgx_id, rvu),
933 					  lmac_id, en);
934 }
935 
936 int rvu_mbox_handler_cgx_intlbk_enable(struct rvu *rvu, struct msg_req *req,
937 				       struct msg_rsp *rsp)
938 {
939 	rvu_cgx_config_intlbk(rvu, req->hdr.pcifunc, true);
940 	return 0;
941 }
942 
943 int rvu_mbox_handler_cgx_intlbk_disable(struct rvu *rvu, struct msg_req *req,
944 					struct msg_rsp *rsp)
945 {
946 	rvu_cgx_config_intlbk(rvu, req->hdr.pcifunc, false);
947 	return 0;
948 }
949 
950 int rvu_cgx_cfg_pause_frm(struct rvu *rvu, u16 pcifunc, u8 tx_pause, u8 rx_pause)
951 {
952 	int pf = rvu_get_pf(pcifunc);
953 	u8 rx_pfc = 0, tx_pfc = 0;
954 	struct mac_ops *mac_ops;
955 	u8 cgx_id, lmac_id;
956 	void *cgxd;
957 
958 	if (!is_mac_feature_supported(rvu, pf, RVU_LMAC_FEAT_FC))
959 		return 0;
960 
961 	/* This msg is expected only from PF/VFs that are mapped to CGX LMACs,
962 	 * if received from other PF/VF simply ACK, nothing to do.
963 	 */
964 	if (!is_pf_cgxmapped(rvu, pf))
965 		return LMAC_AF_ERR_PF_NOT_MAPPED;
966 
967 	rvu_get_cgx_lmac_id(rvu->pf2cgxlmac_map[pf], &cgx_id, &lmac_id);
968 	cgxd = rvu_cgx_pdata(cgx_id, rvu);
969 	mac_ops = get_mac_ops(cgxd);
970 
971 	mac_ops->mac_get_pfc_frm_cfg(cgxd, lmac_id, &tx_pfc, &rx_pfc);
972 	if (tx_pfc || rx_pfc) {
973 		dev_warn(rvu->dev,
974 			 "Can not configure 802.3X flow control as PFC frames are enabled");
975 		return LMAC_AF_ERR_8023PAUSE_ENADIS_PERM_DENIED;
976 	}
977 
978 	mutex_lock(&rvu->rsrc_lock);
979 	if (verify_lmac_fc_cfg(cgxd, lmac_id, tx_pause, rx_pause,
980 			       pcifunc & RVU_PFVF_FUNC_MASK)) {
981 		mutex_unlock(&rvu->rsrc_lock);
982 		return LMAC_AF_ERR_PERM_DENIED;
983 	}
984 	mutex_unlock(&rvu->rsrc_lock);
985 
986 	return mac_ops->mac_enadis_pause_frm(cgxd, lmac_id, tx_pause, rx_pause);
987 }
988 
989 int rvu_mbox_handler_cgx_cfg_pause_frm(struct rvu *rvu,
990 				       struct cgx_pause_frm_cfg *req,
991 				       struct cgx_pause_frm_cfg *rsp)
992 {
993 	int pf = rvu_get_pf(req->hdr.pcifunc);
994 	struct mac_ops *mac_ops;
995 	u8 cgx_id, lmac_id;
996 	int err = 0;
997 	void *cgxd;
998 
999 	/* This msg is expected only from PF/VFs that are mapped to CGX LMACs,
1000 	 * if received from other PF/VF simply ACK, nothing to do.
1001 	 */
1002 	if (!is_pf_cgxmapped(rvu, pf))
1003 		return -ENODEV;
1004 
1005 	rvu_get_cgx_lmac_id(rvu->pf2cgxlmac_map[pf], &cgx_id, &lmac_id);
1006 	cgxd = rvu_cgx_pdata(cgx_id, rvu);
1007 	mac_ops = get_mac_ops(cgxd);
1008 
1009 	if (req->set)
1010 		err = rvu_cgx_cfg_pause_frm(rvu, req->hdr.pcifunc, req->tx_pause, req->rx_pause);
1011 	else
1012 		mac_ops->mac_get_pause_frm_status(cgxd, lmac_id, &rsp->tx_pause, &rsp->rx_pause);
1013 
1014 	return err;
1015 }
1016 
1017 int rvu_mbox_handler_cgx_get_phy_fec_stats(struct rvu *rvu, struct msg_req *req,
1018 					   struct msg_rsp *rsp)
1019 {
1020 	int pf = rvu_get_pf(req->hdr.pcifunc);
1021 	u8 cgx_id, lmac_id;
1022 
1023 	if (!is_pf_cgxmapped(rvu, pf))
1024 		return LMAC_AF_ERR_PF_NOT_MAPPED;
1025 
1026 	rvu_get_cgx_lmac_id(rvu->pf2cgxlmac_map[pf], &cgx_id, &lmac_id);
1027 	return cgx_get_phy_fec_stats(rvu_cgx_pdata(cgx_id, rvu), lmac_id);
1028 }
1029 
1030 /* Finds cumulative status of NIX rx/tx counters from LF of a PF and those
1031  * from its VFs as well. ie. NIX rx/tx counters at the CGX port level
1032  */
1033 int rvu_cgx_nix_cuml_stats(struct rvu *rvu, void *cgxd, int lmac_id,
1034 			   int index, int rxtxflag, u64 *stat)
1035 {
1036 	struct rvu_block *block;
1037 	int blkaddr;
1038 	u16 pcifunc;
1039 	int pf, lf;
1040 
1041 	*stat = 0;
1042 
1043 	if (!cgxd || !rvu)
1044 		return -EINVAL;
1045 
1046 	pf = cgxlmac_to_pf(rvu, cgx_get_cgxid(cgxd), lmac_id);
1047 	if (pf < 0)
1048 		return pf;
1049 
1050 	/* Assumes LF of a PF and all of its VF belongs to the same
1051 	 * NIX block
1052 	 */
1053 	pcifunc = pf << RVU_PFVF_PF_SHIFT;
1054 	blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NIX, pcifunc);
1055 	if (blkaddr < 0)
1056 		return 0;
1057 	block = &rvu->hw->block[blkaddr];
1058 
1059 	for (lf = 0; lf < block->lf.max; lf++) {
1060 		/* Check if a lf is attached to this PF or one of its VFs */
1061 		if (!((block->fn_map[lf] & ~RVU_PFVF_FUNC_MASK) == (pcifunc &
1062 			 ~RVU_PFVF_FUNC_MASK)))
1063 			continue;
1064 		if (rxtxflag == NIX_STATS_RX)
1065 			*stat += rvu_read64(rvu, blkaddr,
1066 					    NIX_AF_LFX_RX_STATX(lf, index));
1067 		else
1068 			*stat += rvu_read64(rvu, blkaddr,
1069 					    NIX_AF_LFX_TX_STATX(lf, index));
1070 	}
1071 
1072 	return 0;
1073 }
1074 
1075 int rvu_cgx_start_stop_io(struct rvu *rvu, u16 pcifunc, bool start)
1076 {
1077 	struct rvu_pfvf *parent_pf, *pfvf;
1078 	int cgx_users, err = 0;
1079 
1080 	if (!is_pf_cgxmapped(rvu, rvu_get_pf(pcifunc)))
1081 		return 0;
1082 
1083 	parent_pf = &rvu->pf[rvu_get_pf(pcifunc)];
1084 	pfvf = rvu_get_pfvf(rvu, pcifunc);
1085 
1086 	mutex_lock(&rvu->cgx_cfg_lock);
1087 
1088 	if (start && pfvf->cgx_in_use)
1089 		goto exit;  /* CGX is already started hence nothing to do */
1090 	if (!start && !pfvf->cgx_in_use)
1091 		goto exit; /* CGX is already stopped hence nothing to do */
1092 
1093 	if (start) {
1094 		cgx_users = parent_pf->cgx_users;
1095 		parent_pf->cgx_users++;
1096 	} else {
1097 		parent_pf->cgx_users--;
1098 		cgx_users = parent_pf->cgx_users;
1099 	}
1100 
1101 	/* Start CGX when first of all NIXLFs is started.
1102 	 * Stop CGX when last of all NIXLFs is stopped.
1103 	 */
1104 	if (!cgx_users) {
1105 		err = rvu_cgx_config_rxtx(rvu, pcifunc & ~RVU_PFVF_FUNC_MASK,
1106 					  start);
1107 		if (err) {
1108 			dev_err(rvu->dev, "Unable to %s CGX\n",
1109 				start ? "start" : "stop");
1110 			/* Revert the usage count in case of error */
1111 			parent_pf->cgx_users = start ? parent_pf->cgx_users  - 1
1112 					       : parent_pf->cgx_users  + 1;
1113 			goto exit;
1114 		}
1115 	}
1116 	pfvf->cgx_in_use = start;
1117 exit:
1118 	mutex_unlock(&rvu->cgx_cfg_lock);
1119 	return err;
1120 }
1121 
1122 int rvu_mbox_handler_cgx_set_fec_param(struct rvu *rvu,
1123 				       struct fec_mode *req,
1124 				       struct fec_mode *rsp)
1125 {
1126 	int pf = rvu_get_pf(req->hdr.pcifunc);
1127 	u8 cgx_id, lmac_id;
1128 
1129 	if (!is_pf_cgxmapped(rvu, pf))
1130 		return -EPERM;
1131 
1132 	if (req->fec == OTX2_FEC_OFF)
1133 		req->fec = OTX2_FEC_NONE;
1134 	rvu_get_cgx_lmac_id(rvu->pf2cgxlmac_map[pf], &cgx_id, &lmac_id);
1135 	rsp->fec = cgx_set_fec(req->fec, cgx_id, lmac_id);
1136 	return 0;
1137 }
1138 
1139 int rvu_mbox_handler_cgx_get_aux_link_info(struct rvu *rvu, struct msg_req *req,
1140 					   struct cgx_fw_data *rsp)
1141 {
1142 	int pf = rvu_get_pf(req->hdr.pcifunc);
1143 	u8 cgx_id, lmac_id;
1144 
1145 	if (!rvu->fwdata)
1146 		return LMAC_AF_ERR_FIRMWARE_DATA_NOT_MAPPED;
1147 
1148 	if (!is_pf_cgxmapped(rvu, pf))
1149 		return -EPERM;
1150 
1151 	rvu_get_cgx_lmac_id(rvu->pf2cgxlmac_map[pf], &cgx_id, &lmac_id);
1152 
1153 	if (rvu->hw->lmac_per_cgx == CGX_LMACS_USX)
1154 		memcpy(&rsp->fwdata,
1155 		       &rvu->fwdata->cgx_fw_data_usx[cgx_id][lmac_id],
1156 		       sizeof(struct cgx_lmac_fwdata_s));
1157 	else
1158 		memcpy(&rsp->fwdata,
1159 		       &rvu->fwdata->cgx_fw_data[cgx_id][lmac_id],
1160 		       sizeof(struct cgx_lmac_fwdata_s));
1161 
1162 	return 0;
1163 }
1164 
1165 int rvu_mbox_handler_cgx_set_link_mode(struct rvu *rvu,
1166 				       struct cgx_set_link_mode_req *req,
1167 				       struct cgx_set_link_mode_rsp *rsp)
1168 {
1169 	int pf = rvu_get_pf(req->hdr.pcifunc);
1170 	u8 cgx_idx, lmac;
1171 	void *cgxd;
1172 
1173 	if (!is_cgx_config_permitted(rvu, req->hdr.pcifunc))
1174 		return -EPERM;
1175 
1176 	rvu_get_cgx_lmac_id(rvu->pf2cgxlmac_map[pf], &cgx_idx, &lmac);
1177 	cgxd = rvu_cgx_pdata(cgx_idx, rvu);
1178 	rsp->status = cgx_set_link_mode(cgxd, req->args, cgx_idx, lmac);
1179 	return 0;
1180 }
1181 
1182 int rvu_mbox_handler_cgx_mac_addr_reset(struct rvu *rvu, struct cgx_mac_addr_reset_req *req,
1183 					struct msg_rsp *rsp)
1184 {
1185 	int pf = rvu_get_pf(req->hdr.pcifunc);
1186 	u8 cgx_id, lmac_id;
1187 
1188 	if (!is_cgx_config_permitted(rvu, req->hdr.pcifunc))
1189 		return LMAC_AF_ERR_PERM_DENIED;
1190 
1191 	rvu_get_cgx_lmac_id(rvu->pf2cgxlmac_map[pf], &cgx_id, &lmac_id);
1192 
1193 	if (rvu_npc_exact_has_match_table(rvu))
1194 		return rvu_npc_exact_mac_addr_reset(rvu, req, rsp);
1195 
1196 	return cgx_lmac_addr_reset(cgx_id, lmac_id);
1197 }
1198 
1199 int rvu_mbox_handler_cgx_mac_addr_update(struct rvu *rvu,
1200 					 struct cgx_mac_addr_update_req *req,
1201 					 struct cgx_mac_addr_update_rsp *rsp)
1202 {
1203 	int pf = rvu_get_pf(req->hdr.pcifunc);
1204 	u8 cgx_id, lmac_id;
1205 
1206 	if (!is_cgx_config_permitted(rvu, req->hdr.pcifunc))
1207 		return LMAC_AF_ERR_PERM_DENIED;
1208 
1209 	if (rvu_npc_exact_has_match_table(rvu))
1210 		return rvu_npc_exact_mac_addr_update(rvu, req, rsp);
1211 
1212 	rvu_get_cgx_lmac_id(rvu->pf2cgxlmac_map[pf], &cgx_id, &lmac_id);
1213 	return cgx_lmac_addr_update(cgx_id, lmac_id, req->mac_addr, req->index);
1214 }
1215 
1216 int rvu_cgx_prio_flow_ctrl_cfg(struct rvu *rvu, u16 pcifunc, u8 tx_pause,
1217 			       u8 rx_pause, u16 pfc_en)
1218 {
1219 	int pf = rvu_get_pf(pcifunc);
1220 	u8 rx_8023 = 0, tx_8023 = 0;
1221 	struct mac_ops *mac_ops;
1222 	u8 cgx_id, lmac_id;
1223 	void *cgxd;
1224 
1225 	/* This msg is expected only from PF/VFs that are mapped to CGX LMACs,
1226 	 * if received from other PF/VF simply ACK, nothing to do.
1227 	 */
1228 	if (!is_pf_cgxmapped(rvu, pf))
1229 		return -ENODEV;
1230 
1231 	rvu_get_cgx_lmac_id(rvu->pf2cgxlmac_map[pf], &cgx_id, &lmac_id);
1232 	cgxd = rvu_cgx_pdata(cgx_id, rvu);
1233 	mac_ops = get_mac_ops(cgxd);
1234 
1235 	mac_ops->mac_get_pause_frm_status(cgxd, lmac_id, &tx_8023, &rx_8023);
1236 	if (tx_8023 || rx_8023) {
1237 		dev_warn(rvu->dev,
1238 			 "Can not configure PFC as 802.3X pause frames are enabled");
1239 		return LMAC_AF_ERR_PFC_ENADIS_PERM_DENIED;
1240 	}
1241 
1242 	mutex_lock(&rvu->rsrc_lock);
1243 	if (verify_lmac_fc_cfg(cgxd, lmac_id, tx_pause, rx_pause,
1244 			       pcifunc & RVU_PFVF_FUNC_MASK)) {
1245 		mutex_unlock(&rvu->rsrc_lock);
1246 		return LMAC_AF_ERR_PERM_DENIED;
1247 	}
1248 	mutex_unlock(&rvu->rsrc_lock);
1249 
1250 	return mac_ops->pfc_config(cgxd, lmac_id, tx_pause, rx_pause, pfc_en);
1251 }
1252 
1253 int rvu_mbox_handler_cgx_prio_flow_ctrl_cfg(struct rvu *rvu,
1254 					    struct cgx_pfc_cfg *req,
1255 					    struct cgx_pfc_rsp *rsp)
1256 {
1257 	int pf = rvu_get_pf(req->hdr.pcifunc);
1258 	struct mac_ops *mac_ops;
1259 	u8 cgx_id, lmac_id;
1260 	void *cgxd;
1261 	int err;
1262 
1263 	/* This msg is expected only from PF/VFs that are mapped to CGX LMACs,
1264 	 * if received from other PF/VF simply ACK, nothing to do.
1265 	 */
1266 	if (!is_pf_cgxmapped(rvu, pf))
1267 		return -ENODEV;
1268 
1269 	rvu_get_cgx_lmac_id(rvu->pf2cgxlmac_map[pf], &cgx_id, &lmac_id);
1270 	cgxd = rvu_cgx_pdata(cgx_id, rvu);
1271 	mac_ops = get_mac_ops(cgxd);
1272 
1273 	err = rvu_cgx_prio_flow_ctrl_cfg(rvu, req->hdr.pcifunc, req->tx_pause,
1274 					 req->rx_pause, req->pfc_en);
1275 
1276 	mac_ops->mac_get_pfc_frm_cfg(cgxd, lmac_id, &rsp->tx_pause, &rsp->rx_pause);
1277 	return err;
1278 }
1279 
1280 void rvu_mac_reset(struct rvu *rvu, u16 pcifunc)
1281 {
1282 	int pf = rvu_get_pf(pcifunc);
1283 	struct mac_ops *mac_ops;
1284 	struct cgx *cgxd;
1285 	u8 cgx, lmac;
1286 
1287 	if (!is_pf_cgxmapped(rvu, pf))
1288 		return;
1289 
1290 	rvu_get_cgx_lmac_id(rvu->pf2cgxlmac_map[pf], &cgx, &lmac);
1291 	cgxd = rvu_cgx_pdata(cgx, rvu);
1292 	mac_ops = get_mac_ops(cgxd);
1293 
1294 	if (mac_ops->mac_reset(cgxd, lmac, !is_vf(pcifunc)))
1295 		dev_err(rvu->dev, "Failed to reset MAC\n");
1296 }
1297