• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0
2 /* Marvell OcteonTx2 RVU Admin Function driver
3  *
4  * Copyright (C) 2018 Marvell International Ltd.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  */
10 
11 #include <linux/types.h>
12 #include <linux/module.h>
13 #include <linux/pci.h>
14 
15 #include "rvu.h"
16 #include "cgx.h"
17 #include "rvu_reg.h"
18 #include "rvu_trace.h"
19 
20 struct cgx_evq_entry {
21 	struct list_head evq_node;
22 	struct cgx_link_event link_event;
23 };
24 
25 #define M(_name, _id, _fn_name, _req_type, _rsp_type)			\
26 static struct _req_type __maybe_unused					\
27 *otx2_mbox_alloc_msg_ ## _fn_name(struct rvu *rvu, int devid)		\
28 {									\
29 	struct _req_type *req;						\
30 									\
31 	req = (struct _req_type *)otx2_mbox_alloc_msg_rsp(		\
32 		&rvu->afpf_wq_info.mbox_up, devid, sizeof(struct _req_type), \
33 		sizeof(struct _rsp_type));				\
34 	if (!req)							\
35 		return NULL;						\
36 	req->hdr.sig = OTX2_MBOX_REQ_SIG;				\
37 	req->hdr.id = _id;						\
38 	trace_otx2_msg_alloc(rvu->pdev, _id, sizeof(*req));		\
39 	return req;							\
40 }
41 
42 MBOX_UP_CGX_MESSAGES
43 #undef M
44 
45 /* Returns bitmap of mapped PFs */
cgxlmac_to_pfmap(struct rvu * rvu,u8 cgx_id,u8 lmac_id)46 static u16 cgxlmac_to_pfmap(struct rvu *rvu, u8 cgx_id, u8 lmac_id)
47 {
48 	return rvu->cgxlmac2pf_map[CGX_OFFSET(cgx_id) + lmac_id];
49 }
50 
cgxlmac_to_pf(struct rvu * rvu,int cgx_id,int lmac_id)51 static int cgxlmac_to_pf(struct rvu *rvu, int cgx_id, int lmac_id)
52 {
53 	unsigned long pfmap;
54 
55 	pfmap = cgxlmac_to_pfmap(rvu, cgx_id, lmac_id);
56 
57 	/* Assumes only one pf mapped to a cgx lmac port */
58 	if (!pfmap)
59 		return -ENODEV;
60 	else
61 		return find_first_bit(&pfmap, 16);
62 }
63 
cgxlmac_id_to_bmap(u8 cgx_id,u8 lmac_id)64 static u8 cgxlmac_id_to_bmap(u8 cgx_id, u8 lmac_id)
65 {
66 	return ((cgx_id & 0xF) << 4) | (lmac_id & 0xF);
67 }
68 
rvu_cgx_pdata(u8 cgx_id,struct rvu * rvu)69 void *rvu_cgx_pdata(u8 cgx_id, struct rvu *rvu)
70 {
71 	if (cgx_id >= rvu->cgx_cnt_max)
72 		return NULL;
73 
74 	return rvu->cgx_idmap[cgx_id];
75 }
76 
77 /* Based on P2X connectivity find mapped NIX block for a PF */
rvu_map_cgx_nix_block(struct rvu * rvu,int pf,int cgx_id,int lmac_id)78 static void rvu_map_cgx_nix_block(struct rvu *rvu, int pf,
79 				  int cgx_id, int lmac_id)
80 {
81 	struct rvu_pfvf *pfvf = &rvu->pf[pf];
82 	u8 p2x;
83 
84 	p2x = cgx_lmac_get_p2x(cgx_id, lmac_id);
85 	/* Firmware sets P2X_SELECT as either NIX0 or NIX1 */
86 	pfvf->nix_blkaddr = BLKADDR_NIX0;
87 	if (is_rvu_supports_nix1(rvu) && p2x == CMR_P2X_SEL_NIX1)
88 		pfvf->nix_blkaddr = BLKADDR_NIX1;
89 }
90 
rvu_map_cgx_lmac_pf(struct rvu * rvu)91 static int rvu_map_cgx_lmac_pf(struct rvu *rvu)
92 {
93 	struct npc_pkind *pkind = &rvu->hw->pkind;
94 	int cgx_cnt_max = rvu->cgx_cnt_max;
95 	int cgx, lmac_cnt, lmac;
96 	int pf = PF_CGXMAP_BASE;
97 	int size, free_pkind;
98 
99 	if (!cgx_cnt_max)
100 		return 0;
101 
102 	if (cgx_cnt_max > 0xF || MAX_LMAC_PER_CGX > 0xF)
103 		return -EINVAL;
104 
105 	/* Alloc map table
106 	 * An additional entry is required since PF id starts from 1 and
107 	 * hence entry at offset 0 is invalid.
108 	 */
109 	size = (cgx_cnt_max * MAX_LMAC_PER_CGX + 1) * sizeof(u8);
110 	rvu->pf2cgxlmac_map = devm_kmalloc(rvu->dev, size, GFP_KERNEL);
111 	if (!rvu->pf2cgxlmac_map)
112 		return -ENOMEM;
113 
114 	/* Initialize all entries with an invalid cgx and lmac id */
115 	memset(rvu->pf2cgxlmac_map, 0xFF, size);
116 
117 	/* Reverse map table */
118 	rvu->cgxlmac2pf_map = devm_kzalloc(rvu->dev,
119 				  cgx_cnt_max * MAX_LMAC_PER_CGX * sizeof(u16),
120 				  GFP_KERNEL);
121 	if (!rvu->cgxlmac2pf_map)
122 		return -ENOMEM;
123 
124 	rvu->cgx_mapped_pfs = 0;
125 	for (cgx = 0; cgx < cgx_cnt_max; cgx++) {
126 		if (!rvu_cgx_pdata(cgx, rvu))
127 			continue;
128 		lmac_cnt = cgx_get_lmac_cnt(rvu_cgx_pdata(cgx, rvu));
129 		for (lmac = 0; lmac < lmac_cnt; lmac++, pf++) {
130 			rvu->pf2cgxlmac_map[pf] = cgxlmac_id_to_bmap(cgx, lmac);
131 			rvu->cgxlmac2pf_map[CGX_OFFSET(cgx) + lmac] = 1 << pf;
132 			free_pkind = rvu_alloc_rsrc(&pkind->rsrc);
133 			pkind->pfchan_map[free_pkind] = ((pf) & 0x3F) << 16;
134 			rvu_map_cgx_nix_block(rvu, pf, cgx, lmac);
135 			rvu->cgx_mapped_pfs++;
136 		}
137 	}
138 	return 0;
139 }
140 
rvu_cgx_send_link_info(int cgx_id,int lmac_id,struct rvu * rvu)141 static int rvu_cgx_send_link_info(int cgx_id, int lmac_id, struct rvu *rvu)
142 {
143 	struct cgx_evq_entry *qentry;
144 	unsigned long flags;
145 	int err;
146 
147 	qentry = kmalloc(sizeof(*qentry), GFP_KERNEL);
148 	if (!qentry)
149 		return -ENOMEM;
150 
151 	/* Lock the event queue before we read the local link status */
152 	spin_lock_irqsave(&rvu->cgx_evq_lock, flags);
153 	err = cgx_get_link_info(rvu_cgx_pdata(cgx_id, rvu), lmac_id,
154 				&qentry->link_event.link_uinfo);
155 	qentry->link_event.cgx_id = cgx_id;
156 	qentry->link_event.lmac_id = lmac_id;
157 	if (err)
158 		goto skip_add;
159 	list_add_tail(&qentry->evq_node, &rvu->cgx_evq_head);
160 skip_add:
161 	spin_unlock_irqrestore(&rvu->cgx_evq_lock, flags);
162 
163 	/* start worker to process the events */
164 	queue_work(rvu->cgx_evh_wq, &rvu->cgx_evh_work);
165 
166 	return 0;
167 }
168 
169 /* This is called from interrupt context and is expected to be atomic */
cgx_lmac_postevent(struct cgx_link_event * event,void * data)170 static int cgx_lmac_postevent(struct cgx_link_event *event, void *data)
171 {
172 	struct cgx_evq_entry *qentry;
173 	struct rvu *rvu = data;
174 
175 	/* post event to the event queue */
176 	qentry = kmalloc(sizeof(*qentry), GFP_ATOMIC);
177 	if (!qentry)
178 		return -ENOMEM;
179 	qentry->link_event = *event;
180 	spin_lock(&rvu->cgx_evq_lock);
181 	list_add_tail(&qentry->evq_node, &rvu->cgx_evq_head);
182 	spin_unlock(&rvu->cgx_evq_lock);
183 
184 	/* start worker to process the events */
185 	queue_work(rvu->cgx_evh_wq, &rvu->cgx_evh_work);
186 
187 	return 0;
188 }
189 
cgx_notify_pfs(struct cgx_link_event * event,struct rvu * rvu)190 static void cgx_notify_pfs(struct cgx_link_event *event, struct rvu *rvu)
191 {
192 	struct cgx_link_user_info *linfo;
193 	struct cgx_link_info_msg *msg;
194 	unsigned long pfmap;
195 	int err, pfid;
196 
197 	linfo = &event->link_uinfo;
198 	pfmap = cgxlmac_to_pfmap(rvu, event->cgx_id, event->lmac_id);
199 
200 	do {
201 		pfid = find_first_bit(&pfmap, 16);
202 		clear_bit(pfid, &pfmap);
203 
204 		/* check if notification is enabled */
205 		if (!test_bit(pfid, &rvu->pf_notify_bmap)) {
206 			dev_info(rvu->dev, "cgx %d: lmac %d Link status %s\n",
207 				 event->cgx_id, event->lmac_id,
208 				 linfo->link_up ? "UP" : "DOWN");
209 			continue;
210 		}
211 
212 		/* Send mbox message to PF */
213 		msg = otx2_mbox_alloc_msg_cgx_link_event(rvu, pfid);
214 		if (!msg)
215 			continue;
216 		msg->link_info = *linfo;
217 		otx2_mbox_msg_send(&rvu->afpf_wq_info.mbox_up, pfid);
218 		err = otx2_mbox_wait_for_rsp(&rvu->afpf_wq_info.mbox_up, pfid);
219 		if (err)
220 			dev_warn(rvu->dev, "notification to pf %d failed\n",
221 				 pfid);
222 	} while (pfmap);
223 }
224 
cgx_evhandler_task(struct work_struct * work)225 static void cgx_evhandler_task(struct work_struct *work)
226 {
227 	struct rvu *rvu = container_of(work, struct rvu, cgx_evh_work);
228 	struct cgx_evq_entry *qentry;
229 	struct cgx_link_event *event;
230 	unsigned long flags;
231 
232 	do {
233 		/* Dequeue an event */
234 		spin_lock_irqsave(&rvu->cgx_evq_lock, flags);
235 		qentry = list_first_entry_or_null(&rvu->cgx_evq_head,
236 						  struct cgx_evq_entry,
237 						  evq_node);
238 		if (qentry)
239 			list_del(&qentry->evq_node);
240 		spin_unlock_irqrestore(&rvu->cgx_evq_lock, flags);
241 		if (!qentry)
242 			break; /* nothing more to process */
243 
244 		event = &qentry->link_event;
245 
246 		/* process event */
247 		cgx_notify_pfs(event, rvu);
248 		kfree(qentry);
249 	} while (1);
250 }
251 
cgx_lmac_event_handler_init(struct rvu * rvu)252 static int cgx_lmac_event_handler_init(struct rvu *rvu)
253 {
254 	struct cgx_event_cb cb;
255 	int cgx, lmac, err;
256 	void *cgxd;
257 
258 	spin_lock_init(&rvu->cgx_evq_lock);
259 	INIT_LIST_HEAD(&rvu->cgx_evq_head);
260 	INIT_WORK(&rvu->cgx_evh_work, cgx_evhandler_task);
261 	rvu->cgx_evh_wq = alloc_workqueue("rvu_evh_wq", 0, 0);
262 	if (!rvu->cgx_evh_wq) {
263 		dev_err(rvu->dev, "alloc workqueue failed");
264 		return -ENOMEM;
265 	}
266 
267 	cb.notify_link_chg = cgx_lmac_postevent; /* link change call back */
268 	cb.data = rvu;
269 
270 	for (cgx = 0; cgx <= rvu->cgx_cnt_max; cgx++) {
271 		cgxd = rvu_cgx_pdata(cgx, rvu);
272 		if (!cgxd)
273 			continue;
274 		for (lmac = 0; lmac < cgx_get_lmac_cnt(cgxd); lmac++) {
275 			err = cgx_lmac_evh_register(&cb, cgxd, lmac);
276 			if (err)
277 				dev_err(rvu->dev,
278 					"%d:%d handler register failed\n",
279 					cgx, lmac);
280 		}
281 	}
282 
283 	return 0;
284 }
285 
rvu_cgx_wq_destroy(struct rvu * rvu)286 static void rvu_cgx_wq_destroy(struct rvu *rvu)
287 {
288 	if (rvu->cgx_evh_wq) {
289 		flush_workqueue(rvu->cgx_evh_wq);
290 		destroy_workqueue(rvu->cgx_evh_wq);
291 		rvu->cgx_evh_wq = NULL;
292 	}
293 }
294 
rvu_cgx_init(struct rvu * rvu)295 int rvu_cgx_init(struct rvu *rvu)
296 {
297 	int cgx, err;
298 	void *cgxd;
299 
300 	/* CGX port id starts from 0 and are not necessarily contiguous
301 	 * Hence we allocate resources based on the maximum port id value.
302 	 */
303 	rvu->cgx_cnt_max = cgx_get_cgxcnt_max();
304 	if (!rvu->cgx_cnt_max) {
305 		dev_info(rvu->dev, "No CGX devices found!\n");
306 		return -ENODEV;
307 	}
308 
309 	rvu->cgx_idmap = devm_kzalloc(rvu->dev, rvu->cgx_cnt_max *
310 				      sizeof(void *), GFP_KERNEL);
311 	if (!rvu->cgx_idmap)
312 		return -ENOMEM;
313 
314 	/* Initialize the cgxdata table */
315 	for (cgx = 0; cgx < rvu->cgx_cnt_max; cgx++)
316 		rvu->cgx_idmap[cgx] = cgx_get_pdata(cgx);
317 
318 	/* Map CGX LMAC interfaces to RVU PFs */
319 	err = rvu_map_cgx_lmac_pf(rvu);
320 	if (err)
321 		return err;
322 
323 	/* Register for CGX events */
324 	err = cgx_lmac_event_handler_init(rvu);
325 	if (err)
326 		return err;
327 
328 	mutex_init(&rvu->cgx_cfg_lock);
329 
330 	/* Ensure event handler registration is completed, before
331 	 * we turn on the links
332 	 */
333 	mb();
334 
335 	/* Do link up for all CGX ports */
336 	for (cgx = 0; cgx <= rvu->cgx_cnt_max; cgx++) {
337 		cgxd = rvu_cgx_pdata(cgx, rvu);
338 		if (!cgxd)
339 			continue;
340 		err = cgx_lmac_linkup_start(cgxd);
341 		if (err)
342 			dev_err(rvu->dev,
343 				"Link up process failed to start on cgx %d\n",
344 				cgx);
345 	}
346 
347 	return 0;
348 }
349 
rvu_cgx_exit(struct rvu * rvu)350 int rvu_cgx_exit(struct rvu *rvu)
351 {
352 	int cgx, lmac;
353 	void *cgxd;
354 
355 	for (cgx = 0; cgx <= rvu->cgx_cnt_max; cgx++) {
356 		cgxd = rvu_cgx_pdata(cgx, rvu);
357 		if (!cgxd)
358 			continue;
359 		for (lmac = 0; lmac < cgx_get_lmac_cnt(cgxd); lmac++)
360 			cgx_lmac_evh_unregister(cgxd, lmac);
361 	}
362 
363 	/* Ensure event handler unregister is completed */
364 	mb();
365 
366 	rvu_cgx_wq_destroy(rvu);
367 	return 0;
368 }
369 
370 /* Most of the CGX configuration is restricted to the mapped PF only,
371  * VF's of mapped PF and other PFs are not allowed. This fn() checks
372  * whether a PFFUNC is permitted to do the config or not.
373  */
is_cgx_config_permitted(struct rvu * rvu,u16 pcifunc)374 static bool is_cgx_config_permitted(struct rvu *rvu, u16 pcifunc)
375 {
376 	if ((pcifunc & RVU_PFVF_FUNC_MASK) ||
377 	    !is_pf_cgxmapped(rvu, rvu_get_pf(pcifunc)))
378 		return false;
379 	return true;
380 }
381 
rvu_cgx_enadis_rx_bp(struct rvu * rvu,int pf,bool enable)382 void rvu_cgx_enadis_rx_bp(struct rvu *rvu, int pf, bool enable)
383 {
384 	u8 cgx_id, lmac_id;
385 	void *cgxd;
386 
387 	if (!is_pf_cgxmapped(rvu, pf))
388 		return;
389 
390 	rvu_get_cgx_lmac_id(rvu->pf2cgxlmac_map[pf], &cgx_id, &lmac_id);
391 	cgxd = rvu_cgx_pdata(cgx_id, rvu);
392 
393 	/* Set / clear CTL_BCK to control pause frame forwarding to NIX */
394 	if (enable)
395 		cgx_lmac_enadis_rx_pause_fwding(cgxd, lmac_id, true);
396 	else
397 		cgx_lmac_enadis_rx_pause_fwding(cgxd, lmac_id, false);
398 }
399 
rvu_cgx_config_rxtx(struct rvu * rvu,u16 pcifunc,bool start)400 int rvu_cgx_config_rxtx(struct rvu *rvu, u16 pcifunc, bool start)
401 {
402 	int pf = rvu_get_pf(pcifunc);
403 	u8 cgx_id, lmac_id;
404 
405 	if (!is_cgx_config_permitted(rvu, pcifunc))
406 		return -EPERM;
407 
408 	rvu_get_cgx_lmac_id(rvu->pf2cgxlmac_map[pf], &cgx_id, &lmac_id);
409 
410 	cgx_lmac_rx_tx_enable(rvu_cgx_pdata(cgx_id, rvu), lmac_id, start);
411 
412 	return 0;
413 }
414 
rvu_mbox_handler_cgx_start_rxtx(struct rvu * rvu,struct msg_req * req,struct msg_rsp * rsp)415 int rvu_mbox_handler_cgx_start_rxtx(struct rvu *rvu, struct msg_req *req,
416 				    struct msg_rsp *rsp)
417 {
418 	rvu_cgx_config_rxtx(rvu, req->hdr.pcifunc, true);
419 	return 0;
420 }
421 
rvu_mbox_handler_cgx_stop_rxtx(struct rvu * rvu,struct msg_req * req,struct msg_rsp * rsp)422 int rvu_mbox_handler_cgx_stop_rxtx(struct rvu *rvu, struct msg_req *req,
423 				   struct msg_rsp *rsp)
424 {
425 	rvu_cgx_config_rxtx(rvu, req->hdr.pcifunc, false);
426 	return 0;
427 }
428 
rvu_mbox_handler_cgx_stats(struct rvu * rvu,struct msg_req * req,struct cgx_stats_rsp * rsp)429 int rvu_mbox_handler_cgx_stats(struct rvu *rvu, struct msg_req *req,
430 			       struct cgx_stats_rsp *rsp)
431 {
432 	int pf = rvu_get_pf(req->hdr.pcifunc);
433 	int stat = 0, err = 0;
434 	u64 tx_stat, rx_stat;
435 	u8 cgx_idx, lmac;
436 	void *cgxd;
437 
438 	if (!is_cgx_config_permitted(rvu, req->hdr.pcifunc))
439 		return -ENODEV;
440 
441 	rvu_get_cgx_lmac_id(rvu->pf2cgxlmac_map[pf], &cgx_idx, &lmac);
442 	cgxd = rvu_cgx_pdata(cgx_idx, rvu);
443 
444 	/* Rx stats */
445 	while (stat < CGX_RX_STATS_COUNT) {
446 		err = cgx_get_rx_stats(cgxd, lmac, stat, &rx_stat);
447 		if (err)
448 			return err;
449 		rsp->rx_stats[stat] = rx_stat;
450 		stat++;
451 	}
452 
453 	/* Tx stats */
454 	stat = 0;
455 	while (stat < CGX_TX_STATS_COUNT) {
456 		err = cgx_get_tx_stats(cgxd, lmac, stat, &tx_stat);
457 		if (err)
458 			return err;
459 		rsp->tx_stats[stat] = tx_stat;
460 		stat++;
461 	}
462 	return 0;
463 }
464 
rvu_mbox_handler_cgx_mac_addr_set(struct rvu * rvu,struct cgx_mac_addr_set_or_get * req,struct cgx_mac_addr_set_or_get * rsp)465 int rvu_mbox_handler_cgx_mac_addr_set(struct rvu *rvu,
466 				      struct cgx_mac_addr_set_or_get *req,
467 				      struct cgx_mac_addr_set_or_get *rsp)
468 {
469 	int pf = rvu_get_pf(req->hdr.pcifunc);
470 	u8 cgx_id, lmac_id;
471 
472 	if (!is_cgx_config_permitted(rvu, req->hdr.pcifunc))
473 		return -EPERM;
474 
475 	rvu_get_cgx_lmac_id(rvu->pf2cgxlmac_map[pf], &cgx_id, &lmac_id);
476 
477 	cgx_lmac_addr_set(cgx_id, lmac_id, req->mac_addr);
478 
479 	return 0;
480 }
481 
rvu_mbox_handler_cgx_mac_addr_get(struct rvu * rvu,struct cgx_mac_addr_set_or_get * req,struct cgx_mac_addr_set_or_get * rsp)482 int rvu_mbox_handler_cgx_mac_addr_get(struct rvu *rvu,
483 				      struct cgx_mac_addr_set_or_get *req,
484 				      struct cgx_mac_addr_set_or_get *rsp)
485 {
486 	int pf = rvu_get_pf(req->hdr.pcifunc);
487 	u8 cgx_id, lmac_id;
488 	int rc = 0, i;
489 	u64 cfg;
490 
491 	if (!is_cgx_config_permitted(rvu, req->hdr.pcifunc))
492 		return -EPERM;
493 
494 	rvu_get_cgx_lmac_id(rvu->pf2cgxlmac_map[pf], &cgx_id, &lmac_id);
495 
496 	rsp->hdr.rc = rc;
497 	cfg = cgx_lmac_addr_get(cgx_id, lmac_id);
498 	/* copy 48 bit mac address to req->mac_addr */
499 	for (i = 0; i < ETH_ALEN; i++)
500 		rsp->mac_addr[i] = cfg >> (ETH_ALEN - 1 - i) * 8;
501 	return 0;
502 }
503 
rvu_mbox_handler_cgx_promisc_enable(struct rvu * rvu,struct msg_req * req,struct msg_rsp * rsp)504 int rvu_mbox_handler_cgx_promisc_enable(struct rvu *rvu, struct msg_req *req,
505 					struct msg_rsp *rsp)
506 {
507 	u16 pcifunc = req->hdr.pcifunc;
508 	int pf = rvu_get_pf(pcifunc);
509 	u8 cgx_id, lmac_id;
510 
511 	if (!is_cgx_config_permitted(rvu, req->hdr.pcifunc))
512 		return -EPERM;
513 
514 	rvu_get_cgx_lmac_id(rvu->pf2cgxlmac_map[pf], &cgx_id, &lmac_id);
515 
516 	cgx_lmac_promisc_config(cgx_id, lmac_id, true);
517 	return 0;
518 }
519 
rvu_mbox_handler_cgx_promisc_disable(struct rvu * rvu,struct msg_req * req,struct msg_rsp * rsp)520 int rvu_mbox_handler_cgx_promisc_disable(struct rvu *rvu, struct msg_req *req,
521 					 struct msg_rsp *rsp)
522 {
523 	int pf = rvu_get_pf(req->hdr.pcifunc);
524 	u8 cgx_id, lmac_id;
525 
526 	if (!is_cgx_config_permitted(rvu, req->hdr.pcifunc))
527 		return -EPERM;
528 
529 	rvu_get_cgx_lmac_id(rvu->pf2cgxlmac_map[pf], &cgx_id, &lmac_id);
530 
531 	cgx_lmac_promisc_config(cgx_id, lmac_id, false);
532 	return 0;
533 }
534 
rvu_cgx_ptp_rx_cfg(struct rvu * rvu,u16 pcifunc,bool enable)535 static int rvu_cgx_ptp_rx_cfg(struct rvu *rvu, u16 pcifunc, bool enable)
536 {
537 	int pf = rvu_get_pf(pcifunc);
538 	u8 cgx_id, lmac_id;
539 	void *cgxd;
540 
541 	/* This msg is expected only from PFs that are mapped to CGX LMACs,
542 	 * if received from other PF/VF simply ACK, nothing to do.
543 	 */
544 	if ((pcifunc & RVU_PFVF_FUNC_MASK) ||
545 	    !is_pf_cgxmapped(rvu, pf))
546 		return -ENODEV;
547 
548 	rvu_get_cgx_lmac_id(rvu->pf2cgxlmac_map[pf], &cgx_id, &lmac_id);
549 	cgxd = rvu_cgx_pdata(cgx_id, rvu);
550 
551 	cgx_lmac_ptp_config(cgxd, lmac_id, enable);
552 	/* If PTP is enabled then inform NPC that packets to be
553 	 * parsed by this PF will have their data shifted by 8 bytes
554 	 * and if PTP is disabled then no shift is required
555 	 */
556 	if (npc_config_ts_kpuaction(rvu, pf, pcifunc, enable))
557 		return -EINVAL;
558 
559 	return 0;
560 }
561 
rvu_mbox_handler_cgx_ptp_rx_enable(struct rvu * rvu,struct msg_req * req,struct msg_rsp * rsp)562 int rvu_mbox_handler_cgx_ptp_rx_enable(struct rvu *rvu, struct msg_req *req,
563 				       struct msg_rsp *rsp)
564 {
565 	return rvu_cgx_ptp_rx_cfg(rvu, req->hdr.pcifunc, true);
566 }
567 
rvu_mbox_handler_cgx_ptp_rx_disable(struct rvu * rvu,struct msg_req * req,struct msg_rsp * rsp)568 int rvu_mbox_handler_cgx_ptp_rx_disable(struct rvu *rvu, struct msg_req *req,
569 					struct msg_rsp *rsp)
570 {
571 	return rvu_cgx_ptp_rx_cfg(rvu, req->hdr.pcifunc, false);
572 }
573 
rvu_cgx_config_linkevents(struct rvu * rvu,u16 pcifunc,bool en)574 static int rvu_cgx_config_linkevents(struct rvu *rvu, u16 pcifunc, bool en)
575 {
576 	int pf = rvu_get_pf(pcifunc);
577 	u8 cgx_id, lmac_id;
578 
579 	if (!is_cgx_config_permitted(rvu, pcifunc))
580 		return -EPERM;
581 
582 	rvu_get_cgx_lmac_id(rvu->pf2cgxlmac_map[pf], &cgx_id, &lmac_id);
583 
584 	if (en) {
585 		set_bit(pf, &rvu->pf_notify_bmap);
586 		/* Send the current link status to PF */
587 		rvu_cgx_send_link_info(cgx_id, lmac_id, rvu);
588 	} else {
589 		clear_bit(pf, &rvu->pf_notify_bmap);
590 	}
591 
592 	return 0;
593 }
594 
rvu_mbox_handler_cgx_start_linkevents(struct rvu * rvu,struct msg_req * req,struct msg_rsp * rsp)595 int rvu_mbox_handler_cgx_start_linkevents(struct rvu *rvu, struct msg_req *req,
596 					  struct msg_rsp *rsp)
597 {
598 	rvu_cgx_config_linkevents(rvu, req->hdr.pcifunc, true);
599 	return 0;
600 }
601 
rvu_mbox_handler_cgx_stop_linkevents(struct rvu * rvu,struct msg_req * req,struct msg_rsp * rsp)602 int rvu_mbox_handler_cgx_stop_linkevents(struct rvu *rvu, struct msg_req *req,
603 					 struct msg_rsp *rsp)
604 {
605 	rvu_cgx_config_linkevents(rvu, req->hdr.pcifunc, false);
606 	return 0;
607 }
608 
rvu_mbox_handler_cgx_get_linkinfo(struct rvu * rvu,struct msg_req * req,struct cgx_link_info_msg * rsp)609 int rvu_mbox_handler_cgx_get_linkinfo(struct rvu *rvu, struct msg_req *req,
610 				      struct cgx_link_info_msg *rsp)
611 {
612 	u8 cgx_id, lmac_id;
613 	int pf, err;
614 
615 	pf = rvu_get_pf(req->hdr.pcifunc);
616 
617 	if (!is_pf_cgxmapped(rvu, pf))
618 		return -ENODEV;
619 
620 	rvu_get_cgx_lmac_id(rvu->pf2cgxlmac_map[pf], &cgx_id, &lmac_id);
621 
622 	err = cgx_get_link_info(rvu_cgx_pdata(cgx_id, rvu), lmac_id,
623 				&rsp->link_info);
624 	return err;
625 }
626 
rvu_cgx_config_intlbk(struct rvu * rvu,u16 pcifunc,bool en)627 static int rvu_cgx_config_intlbk(struct rvu *rvu, u16 pcifunc, bool en)
628 {
629 	int pf = rvu_get_pf(pcifunc);
630 	u8 cgx_id, lmac_id;
631 
632 	if (!is_cgx_config_permitted(rvu, pcifunc))
633 		return -EPERM;
634 
635 	rvu_get_cgx_lmac_id(rvu->pf2cgxlmac_map[pf], &cgx_id, &lmac_id);
636 
637 	return cgx_lmac_internal_loopback(rvu_cgx_pdata(cgx_id, rvu),
638 					  lmac_id, en);
639 }
640 
rvu_mbox_handler_cgx_intlbk_enable(struct rvu * rvu,struct msg_req * req,struct msg_rsp * rsp)641 int rvu_mbox_handler_cgx_intlbk_enable(struct rvu *rvu, struct msg_req *req,
642 				       struct msg_rsp *rsp)
643 {
644 	rvu_cgx_config_intlbk(rvu, req->hdr.pcifunc, true);
645 	return 0;
646 }
647 
rvu_mbox_handler_cgx_intlbk_disable(struct rvu * rvu,struct msg_req * req,struct msg_rsp * rsp)648 int rvu_mbox_handler_cgx_intlbk_disable(struct rvu *rvu, struct msg_req *req,
649 					struct msg_rsp *rsp)
650 {
651 	rvu_cgx_config_intlbk(rvu, req->hdr.pcifunc, false);
652 	return 0;
653 }
654 
rvu_mbox_handler_cgx_cfg_pause_frm(struct rvu * rvu,struct cgx_pause_frm_cfg * req,struct cgx_pause_frm_cfg * rsp)655 int rvu_mbox_handler_cgx_cfg_pause_frm(struct rvu *rvu,
656 				       struct cgx_pause_frm_cfg *req,
657 				       struct cgx_pause_frm_cfg *rsp)
658 {
659 	int pf = rvu_get_pf(req->hdr.pcifunc);
660 	u8 cgx_id, lmac_id;
661 
662 	/* This msg is expected only from PF/VFs that are mapped to CGX LMACs,
663 	 * if received from other PF/VF simply ACK, nothing to do.
664 	 */
665 	if (!is_pf_cgxmapped(rvu, pf))
666 		return -ENODEV;
667 
668 	rvu_get_cgx_lmac_id(rvu->pf2cgxlmac_map[pf], &cgx_id, &lmac_id);
669 
670 	if (req->set)
671 		cgx_lmac_set_pause_frm(rvu_cgx_pdata(cgx_id, rvu), lmac_id,
672 				       req->tx_pause, req->rx_pause);
673 	else
674 		cgx_lmac_get_pause_frm(rvu_cgx_pdata(cgx_id, rvu), lmac_id,
675 				       &rsp->tx_pause, &rsp->rx_pause);
676 	return 0;
677 }
678 
679 /* Finds cumulative status of NIX rx/tx counters from LF of a PF and those
680  * from its VFs as well. ie. NIX rx/tx counters at the CGX port level
681  */
rvu_cgx_nix_cuml_stats(struct rvu * rvu,void * cgxd,int lmac_id,int index,int rxtxflag,u64 * stat)682 int rvu_cgx_nix_cuml_stats(struct rvu *rvu, void *cgxd, int lmac_id,
683 			   int index, int rxtxflag, u64 *stat)
684 {
685 	struct rvu_block *block;
686 	int blkaddr;
687 	u16 pcifunc;
688 	int pf, lf;
689 
690 	*stat = 0;
691 
692 	if (!cgxd || !rvu)
693 		return -EINVAL;
694 
695 	pf = cgxlmac_to_pf(rvu, cgx_get_cgxid(cgxd), lmac_id);
696 	if (pf < 0)
697 		return pf;
698 
699 	/* Assumes LF of a PF and all of its VF belongs to the same
700 	 * NIX block
701 	 */
702 	pcifunc = pf << RVU_PFVF_PF_SHIFT;
703 	blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NIX, pcifunc);
704 	if (blkaddr < 0)
705 		return 0;
706 	block = &rvu->hw->block[blkaddr];
707 
708 	for (lf = 0; lf < block->lf.max; lf++) {
709 		/* Check if a lf is attached to this PF or one of its VFs */
710 		if (!((block->fn_map[lf] & ~RVU_PFVF_FUNC_MASK) == (pcifunc &
711 			 ~RVU_PFVF_FUNC_MASK)))
712 			continue;
713 		if (rxtxflag == NIX_STATS_RX)
714 			*stat += rvu_read64(rvu, blkaddr,
715 					    NIX_AF_LFX_RX_STATX(lf, index));
716 		else
717 			*stat += rvu_read64(rvu, blkaddr,
718 					    NIX_AF_LFX_TX_STATX(lf, index));
719 	}
720 
721 	return 0;
722 }
723 
rvu_cgx_start_stop_io(struct rvu * rvu,u16 pcifunc,bool start)724 int rvu_cgx_start_stop_io(struct rvu *rvu, u16 pcifunc, bool start)
725 {
726 	struct rvu_pfvf *parent_pf, *pfvf;
727 	int cgx_users, err = 0;
728 
729 	if (!is_pf_cgxmapped(rvu, rvu_get_pf(pcifunc)))
730 		return 0;
731 
732 	parent_pf = &rvu->pf[rvu_get_pf(pcifunc)];
733 	pfvf = rvu_get_pfvf(rvu, pcifunc);
734 
735 	mutex_lock(&rvu->cgx_cfg_lock);
736 
737 	if (start && pfvf->cgx_in_use)
738 		goto exit;  /* CGX is already started hence nothing to do */
739 	if (!start && !pfvf->cgx_in_use)
740 		goto exit; /* CGX is already stopped hence nothing to do */
741 
742 	if (start) {
743 		cgx_users = parent_pf->cgx_users;
744 		parent_pf->cgx_users++;
745 	} else {
746 		parent_pf->cgx_users--;
747 		cgx_users = parent_pf->cgx_users;
748 	}
749 
750 	/* Start CGX when first of all NIXLFs is started.
751 	 * Stop CGX when last of all NIXLFs is stopped.
752 	 */
753 	if (!cgx_users) {
754 		err = rvu_cgx_config_rxtx(rvu, pcifunc & ~RVU_PFVF_FUNC_MASK,
755 					  start);
756 		if (err) {
757 			dev_err(rvu->dev, "Unable to %s CGX\n",
758 				start ? "start" : "stop");
759 			/* Revert the usage count in case of error */
760 			parent_pf->cgx_users = start ? parent_pf->cgx_users  - 1
761 					       : parent_pf->cgx_users  + 1;
762 			goto exit;
763 		}
764 	}
765 	pfvf->cgx_in_use = start;
766 exit:
767 	mutex_unlock(&rvu->cgx_cfg_lock);
768 	return err;
769 }
770