Debian Codegraph @

drivers/net/ethernet/marvell/octeontx2/af/rvu_nix.c

raw symbols SCIP
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/module.h>
9 #include <linux/pci.h>
10
11 #include "rvu_struct.h"
12 #include "rvu_reg.h"
13 #include "rvu.h"
14 #include "npc.h"
15 #include "mcs.h"
16 #include "cgx.h"
17 #include "lmac_common.h"
18 #include "rvu_npc_hash.h"
19 #include "cn20k/npc.h"
20
21 static void nix_free_tx_vtag_entries(struct rvu *rvu, u16 pcifunc);
22 static int rvu_nix_get_bpid(struct rvu *rvu, struct nix_bp_cfg_req *req,
23 int type, int chan_id);
24 static int nix_update_mce_rule(struct rvu *rvu, u16 pcifunc,
25 int type, bool add);
26 static int nix_setup_ipolicers(struct rvu *rvu,
27 struct nix_hw *nix_hw, int blkaddr);
28 static void nix_ipolicer_freemem(struct rvu *rvu, struct nix_hw *nix_hw);
29 static int nix_verify_bandprof(struct nix_cn10k_aq_enq_req *req,
30 struct nix_hw *nix_hw, u16 pcifunc);
31 static int nix_free_all_bandprof(struct rvu *rvu, u16 pcifunc);
32 static void nix_clear_ratelimit_aggr(struct rvu *rvu, struct nix_hw *nix_hw,
33 u32 leaf_prof);
34 static const char *nix_get_ctx_name(int ctype);
35 static int nix_get_tx_link(struct rvu *rvu, u16 pcifunc);
36
37 enum mc_tbl_sz {
38 MC_TBL_SZ_256,
39 MC_TBL_SZ_512,
40 MC_TBL_SZ_1K,
41 MC_TBL_SZ_2K,
42 MC_TBL_SZ_4K,
43 MC_TBL_SZ_8K,
44 MC_TBL_SZ_16K,
45 MC_TBL_SZ_32K,
46 MC_TBL_SZ_64K,
47 };
48
49 enum mc_buf_cnt {
50 MC_BUF_CNT_8,
51 MC_BUF_CNT_16,
52 MC_BUF_CNT_32,
53 MC_BUF_CNT_64,
54 MC_BUF_CNT_128,
55 MC_BUF_CNT_256,
56 MC_BUF_CNT_512,
57 MC_BUF_CNT_1024,
58 MC_BUF_CNT_2048,
59 };
60
61 enum nix_makr_fmt_indexes {
62 NIX_MARK_CFG_IP_DSCP_RED,
63 NIX_MARK_CFG_IP_DSCP_YELLOW,
64 NIX_MARK_CFG_IP_DSCP_YELLOW_RED,
65 NIX_MARK_CFG_IP_ECN_RED,
66 NIX_MARK_CFG_IP_ECN_YELLOW,
67 NIX_MARK_CFG_IP_ECN_YELLOW_RED,
68 NIX_MARK_CFG_VLAN_DEI_RED,
69 NIX_MARK_CFG_VLAN_DEI_YELLOW,
70 NIX_MARK_CFG_VLAN_DEI_YELLOW_RED,
71 NIX_MARK_CFG_MAX,
72 };
73
74 /* For now considering MC resources needed for broadcast
75 * pkt replication only. i.e 256 HWVFs + 12 PFs.
76 */
77 #define MC_TBL_SIZE MC_TBL_SZ_2K
78 #define MC_BUF_CNT MC_BUF_CNT_1024
79
80 #define MC_TX_MAX 2048
81
82 struct mce {
83 struct hlist_node node;
84 u32 rq_rss_index;
85 u16 pcifunc;
86 u16 channel;
87 u8 dest_type;
88 u8 is_active;
89 u8 reserved[2];
90 };
91
92 int rvu_get_next_nix_blkaddr(struct rvu *rvu, int blkaddr)
93 {
94 int i = 0;
95
96 /*If blkaddr is 0, return the first nix block address*/
97 if (blkaddr == 0)
98 return rvu->nix_blkaddr[blkaddr];
99
100 while (i + 1 < MAX_NIX_BLKS) {
101 if (rvu->nix_blkaddr[i] == blkaddr)
102 return rvu->nix_blkaddr[i + 1];
103 i++;
104 }
105
106 return 0;
107 }
108
109 bool is_nixlf_attached(struct rvu *rvu, u16 pcifunc)
110 {
111 struct rvu_pfvf *pfvf = rvu_get_pfvf(rvu, pcifunc);
112 int blkaddr;
113
114 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NIX, pcifunc);
115 if (!pfvf->nixlf || blkaddr < 0)
116 return false;
117 return true;
118 }
119
120 int rvu_get_nixlf_count(struct rvu *rvu)
121 {
122 int blkaddr = 0, max = 0;
123 struct rvu_block *block;
124
125 blkaddr = rvu_get_next_nix_blkaddr(rvu, blkaddr);
126 while (blkaddr) {
127 block = &rvu->hw->block[blkaddr];
128 max += block->lf.max;
129 blkaddr = rvu_get_next_nix_blkaddr(rvu, blkaddr);
130 }
131 return max;
132 }
133
134 int nix_get_nixlf(struct rvu *rvu, u16 pcifunc, int *nixlf, int *nix_blkaddr)
135 {
136 struct rvu_pfvf *pfvf = rvu_get_pfvf(rvu, pcifunc);
137 struct rvu_hwinfo *hw = rvu->hw;
138 int blkaddr;
139
140 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NIX, pcifunc);
141 if (!pfvf->nixlf || blkaddr < 0)
142 return NIX_AF_ERR_AF_LF_INVALID;
143
144 *nixlf = rvu_get_lf(rvu, &hw->block[blkaddr], pcifunc, 0);
145 if (*nixlf < 0)
146 return NIX_AF_ERR_AF_LF_INVALID;
147
148 if (nix_blkaddr)
149 *nix_blkaddr = blkaddr;
150
151 return 0;
152 }
153
154 int nix_get_struct_ptrs(struct rvu *rvu, u16 pcifunc,
155 struct nix_hw **nix_hw, int *blkaddr)
156 {
157 struct rvu_pfvf *pfvf;
158
159 pfvf = rvu_get_pfvf(rvu, pcifunc);
160 *blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NIX, pcifunc);
161 if (!pfvf->nixlf || *blkaddr < 0)
162 return NIX_AF_ERR_AF_LF_INVALID;
163
164 *nix_hw = get_nix_hw(rvu->hw, *blkaddr);
165 if (!*nix_hw)
166 return NIX_AF_ERR_INVALID_NIXBLK;
167 return 0;
168 }
169
170 static void nix_mce_list_init(struct nix_mce_list *list, int max)
171 {
172 INIT_HLIST_HEAD(&list->head);
173 list->count = 0;
174 list->max = max;
175 }
176
177 static int nix_alloc_mce_list(struct nix_mcast *mcast, int count, u8 dir)
178 {
179 struct rsrc_bmap *mce_counter;
180 int idx;
181
182 if (!mcast)
183 return -EINVAL;
184
185 mce_counter = &mcast->mce_counter[dir];
186 if (!rvu_rsrc_check_contig(mce_counter, count))
187 return -ENOSPC;
188
189 idx = rvu_alloc_rsrc_contig(mce_counter, count);
190 return idx;
191 }
192
193 static void nix_free_mce_list(struct nix_mcast *mcast, int count, int start, u8 dir)
194 {
195 struct rsrc_bmap *mce_counter;
196
197 if (!mcast)
198 return;
199
200 mce_counter = &mcast->mce_counter[dir];
201 rvu_free_rsrc_contig(mce_counter, count, start);
202 }
203
204 struct nix_hw *get_nix_hw(struct rvu_hwinfo *hw, int blkaddr)
205 {
206 int nix_blkaddr = 0, i = 0;
207 struct rvu *rvu = hw->rvu;
208
209 nix_blkaddr = rvu_get_next_nix_blkaddr(rvu, nix_blkaddr);
210 while (nix_blkaddr) {
211 if (blkaddr == nix_blkaddr && hw->nix)
212 return &hw->nix[i];
213 nix_blkaddr = rvu_get_next_nix_blkaddr(rvu, nix_blkaddr);
214 i++;
215 }
216 return NULL;
217 }
218
219 int nix_get_dwrr_mtu_reg(struct rvu_hwinfo *hw, int smq_link_type)
220 {
221 if (hw->cap.nix_multiple_dwrr_mtu)
222 return NIX_AF_DWRR_MTUX(smq_link_type);
223
224 if (smq_link_type == SMQ_LINK_TYPE_SDP)
225 return NIX_AF_DWRR_SDP_MTU;
226
227 /* Here it's same reg for RPM and LBK */
228 return NIX_AF_DWRR_RPM_MTU;
229 }
230
231 u32 convert_dwrr_mtu_to_bytes(u8 dwrr_mtu)
232 {
233 dwrr_mtu &= 0x1FULL;
234
235 /* MTU used for DWRR calculation is in power of 2 up until 64K bytes.
236 * Value of 4 is reserved for MTU value of 9728 bytes.
237 * Value of 5 is reserved for MTU value of 10240 bytes.
238 */
239 switch (dwrr_mtu) {
240 case 4:
241 return 9728;
242 case 5:
243 return 10240;
244 default:
245 return BIT_ULL(dwrr_mtu);
246 }
247
248 return 0;
249 }
250
251 u32 convert_bytes_to_dwrr_mtu(u32 bytes)
252 {
253 /* MTU used for DWRR calculation is in power of 2 up until 64K bytes.
254 * Value of 4 is reserved for MTU value of 9728 bytes.
255 * Value of 5 is reserved for MTU value of 10240 bytes.
256 */
257 if (bytes > BIT_ULL(16))
258 return 0;
259
260 switch (bytes) {
261 case 9728:
262 return 4;
263 case 10240:
264 return 5;
265 default:
266 return ilog2(bytes);
267 }
268
269 return 0;
270 }
271
272 static void nix_rx_sync(struct rvu *rvu, int blkaddr)
273 {
274 int err;
275
276 /* Sync all in flight RX packets to LLC/DRAM */
277 rvu_write64(rvu, blkaddr, NIX_AF_RX_SW_SYNC, BIT_ULL(0));
278 err = rvu_poll_reg(rvu, blkaddr, NIX_AF_RX_SW_SYNC, BIT_ULL(0), true);
279 if (err)
280 dev_err(rvu->dev, "SYNC1: NIX RX software sync failed\n");
281
282 /* SW_SYNC ensures all existing transactions are finished and pkts
283 * are written to LLC/DRAM, queues should be teared down after
284 * successful SW_SYNC. Due to a HW errata, in some rare scenarios
285 * an existing transaction might end after SW_SYNC operation. To
286 * ensure operation is fully done, do the SW_SYNC twice.
287 */
288 rvu_write64(rvu, blkaddr, NIX_AF_RX_SW_SYNC, BIT_ULL(0));
289 err = rvu_poll_reg(rvu, blkaddr, NIX_AF_RX_SW_SYNC, BIT_ULL(0), true);
290 if (err)
291 dev_err(rvu->dev, "SYNC2: NIX RX software sync failed\n");
292 }
293
294 static bool is_valid_txschq(struct rvu *rvu, int blkaddr,
295 int lvl, u16 pcifunc, u16 schq)
296 {
297 struct rvu_hwinfo *hw = rvu->hw;
298 struct nix_txsch *txsch;
299 struct nix_hw *nix_hw;
300 u16 map_func;
301
302 nix_hw = get_nix_hw(rvu->hw, blkaddr);
303 if (!nix_hw)
304 return false;
305
306 txsch = &nix_hw->txsch[lvl];
307 /* Check out of bounds */
308 if (schq >= txsch->schq.max)
309 return false;
310
311 mutex_lock(&rvu->rsrc_lock);
312 map_func = TXSCH_MAP_FUNC(txsch->pfvf_map[schq]);
313 mutex_unlock(&rvu->rsrc_lock);
314
315 /* TLs aggegating traffic are shared across PF and VFs */
316 if (lvl >= hw->cap.nix_tx_aggr_lvl) {
317 if ((nix_get_tx_link(rvu, map_func) !=
318 nix_get_tx_link(rvu, pcifunc)) &&
319 (rvu_get_pf(rvu->pdev, map_func) !=
320 rvu_get_pf(rvu->pdev, pcifunc)))
321 return false;
322 else
323 return true;
324 }
325
326 if (map_func != pcifunc)
327 return false;
328
329 return true;
330 }
331
332 static int nix_interface_init(struct rvu *rvu, u16 pcifunc, int type, int nixlf,
333 struct nix_lf_alloc_rsp *rsp, bool loop)
334 {
335 struct rvu_pfvf *parent_pf, *pfvf = rvu_get_pfvf(rvu, pcifunc);
336 u16 req_chan_base, req_chan_end, req_chan_cnt;
337 struct rvu_hwinfo *hw = rvu->hw;
338 struct sdp_node_info *sdp_info;
339 int pkind, pf, vf, lbkid, vfid;
340 u8 cgx_id, lmac_id;
341 bool from_vf;
342 int err;
343
344 pf = rvu_get_pf(rvu->pdev, pcifunc);
345 if (!is_pf_cgxmapped(rvu, pf) && type != NIX_INTF_TYPE_LBK &&
346 type != NIX_INTF_TYPE_SDP)
347 return 0;
348
349 switch (type) {
350 case NIX_INTF_TYPE_CGX:
351 pfvf->cgx_lmac = rvu->pf2cgxlmac_map[pf];
352 rvu_get_cgx_lmac_id(pfvf->cgx_lmac, &cgx_id, &lmac_id);
353
354 pkind = rvu_npc_get_pkind(rvu, pf);
355 if (pkind < 0) {
356 dev_err(rvu->dev,
357 "PF_Func 0x%x: Invalid pkind\n", pcifunc);
358 return -EINVAL;
359 }
360 pfvf->rx_chan_base = rvu_nix_chan_cgx(rvu, cgx_id, lmac_id, 0);
361 pfvf->tx_chan_base = pfvf->rx_chan_base;
362 pfvf->rx_chan_cnt = 1;
363 pfvf->tx_chan_cnt = 1;
364 rsp->tx_link = cgx_id * hw->lmac_per_cgx + lmac_id;
365
366 if (rvu_cgx_check_permission_and_set_pkind(rvu, pcifunc, pkind))
367 rvu_npc_set_pkind(rvu, pkind, pfvf);
368 break;
369 case NIX_INTF_TYPE_LBK:
370 vf = (pcifunc & RVU_PFVF_FUNC_MASK) - 1;
371
372 /* If NIX1 block is present on the silicon then NIXes are
373 * assigned alternatively for lbk interfaces. NIX0 should
374 * send packets on lbk link 1 channels and NIX1 should send
375 * on lbk link 0 channels for the communication between
376 * NIX0 and NIX1.
377 */
378 lbkid = 0;
379 if (rvu->hw->lbk_links > 1)
380 lbkid = vf & 0x1 ? 0 : 1;
381
382 /* By default NIX0 is configured to send packet on lbk link 1
383 * (which corresponds to LBK1), same packet will receive on
384 * NIX1 over lbk link 0. If NIX1 sends packet on lbk link 0
385 * (which corresponds to LBK2) packet will receive on NIX0 lbk
386 * link 1.
387 * But if lbk links for NIX0 and NIX1 are negated, i.e NIX0
388 * transmits and receives on lbk link 0, whick corresponds
389 * to LBK1 block, back to back connectivity between NIX and
390 * LBK can be achieved (which is similar to 96xx)
391 *
392 * RX TX
393 * NIX0 lbk link 1 (LBK2) 1 (LBK1)
394 * NIX0 lbk link 0 (LBK0) 0 (LBK0)
395 * NIX1 lbk link 0 (LBK1) 0 (LBK2)
396 * NIX1 lbk link 1 (LBK3) 1 (LBK3)
397 */
398 if (loop)
399 lbkid = !lbkid;
400
401 /* Note that AF's VFs work in pairs and talk over consecutive
402 * loopback channels.Therefore if odd number of AF VFs are
403 * enabled then the last VF remains with no pair.
404 */
405 pfvf->rx_chan_base = rvu_nix_chan_lbk(rvu, lbkid, vf);
406 pfvf->tx_chan_base = vf & 0x1 ?
407 rvu_nix_chan_lbk(rvu, lbkid, vf - 1) :
408 rvu_nix_chan_lbk(rvu, lbkid, vf + 1);
409 pfvf->rx_chan_cnt = 1;
410 pfvf->tx_chan_cnt = 1;
411 rsp->tx_link = hw->cgx_links + lbkid;
412 pfvf->lbkid = lbkid;
413 rvu_npc_set_pkind(rvu, NPC_RX_LBK_PKIND, pfvf);
414 rvu_npc_install_promisc_entry(rvu, pcifunc, nixlf,
415 pfvf->rx_chan_base,
416 pfvf->rx_chan_cnt);
417
418 break;
419 case NIX_INTF_TYPE_SDP:
420 from_vf = !!(pcifunc & RVU_PFVF_FUNC_MASK);
421 parent_pf = &rvu->pf[rvu_get_pf(rvu->pdev, pcifunc)];
422 sdp_info = parent_pf->sdp_info;
423 if (!sdp_info) {
424 dev_err(rvu->dev, "Invalid sdp_info pointer\n");
425 return -EINVAL;
426 }
427 if (from_vf) {
428 req_chan_base = rvu_nix_chan_sdp(rvu, 0) + sdp_info->pf_srn +
429 sdp_info->num_pf_rings;
430 vf = (pcifunc & RVU_PFVF_FUNC_MASK) - 1;
431 for (vfid = 0; vfid < vf; vfid++)
432 req_chan_base += sdp_info->vf_rings[vfid];
433 req_chan_cnt = sdp_info->vf_rings[vf];
434 req_chan_end = req_chan_base + req_chan_cnt - 1;
435 if (req_chan_base < rvu_nix_chan_sdp(rvu, 0) ||
436 req_chan_end > rvu_nix_chan_sdp(rvu, 255)) {
437 dev_err(rvu->dev,
438 "PF_Func 0x%x: Invalid channel base and count\n",
439 pcifunc);
440 return -EINVAL;
441 }
442 } else {
443 req_chan_base = rvu_nix_chan_sdp(rvu, 0) + sdp_info->pf_srn;
444 req_chan_cnt = sdp_info->num_pf_rings;
445 }
446
447 pfvf->rx_chan_base = req_chan_base;
448 pfvf->rx_chan_cnt = req_chan_cnt;
449 pfvf->tx_chan_base = pfvf->rx_chan_base;
450 pfvf->tx_chan_cnt = pfvf->rx_chan_cnt;
451
452 rsp->tx_link = hw->cgx_links + hw->lbk_links;
453 rvu_npc_install_promisc_entry(rvu, pcifunc, nixlf,
454 pfvf->rx_chan_base,
455 pfvf->rx_chan_cnt);
456 break;
457 }
458
459 /* Add a UCAST forwarding rule in MCAM with this NIXLF attached
460 * RVU PF/VF's MAC address.
461 */
462 rvu_npc_install_ucast_entry(rvu, pcifunc, nixlf,
463 pfvf->rx_chan_base, pfvf->mac_addr);
464
465 /* Add this PF_FUNC to bcast pkt replication list */
466 err = nix_update_mce_rule(rvu, pcifunc, NIXLF_BCAST_ENTRY, true);
467 if (err) {
468 dev_err(rvu->dev,
469 "Bcast list, failed to enable PF_FUNC 0x%x\n",
470 pcifunc);
471 return err;
472 }
473 /* Install MCAM rule matching Ethernet broadcast mac address */
474 rvu_npc_install_bcast_match_entry(rvu, pcifunc,
475 nixlf, pfvf->rx_chan_base);
476
477 pfvf->maxlen = NIC_HW_MIN_FRS;
478 pfvf->minlen = NIC_HW_MIN_FRS;
479
480 return 0;
481 }
482
483 static void nix_interface_deinit(struct rvu *rvu, u16 pcifunc, u8 nixlf)
484 {
485 struct rvu_pfvf *pfvf = rvu_get_pfvf(rvu, pcifunc);
486 int err;
487
488 pfvf->maxlen = 0;
489 pfvf->minlen = 0;
490
491 /* Remove this PF_FUNC from bcast pkt replication list */
492 err = nix_update_mce_rule(rvu, pcifunc, NIXLF_BCAST_ENTRY, false);
493 if (err) {
494 dev_err(rvu->dev,
495 "Bcast list, failed to disable PF_FUNC 0x%x\n",
496 pcifunc);
497 }
498
499 /* Free and disable any MCAM entries used by this NIX LF */
500 rvu_npc_disable_mcam_entries(rvu, pcifunc, nixlf);
501
502 /* Disable DMAC filters used */
503 rvu_cgx_disable_dmac_entries(rvu, pcifunc);
504 }
505
506 #define NIX_BPIDS_PER_LMAC 8
507 #define NIX_BPIDS_PER_CPT 1
508 static int nix_setup_bpids(struct rvu *rvu, struct nix_hw *hw, int blkaddr)
509 {
510 struct nix_bp *bp = &hw->bp;
511 int err, max_bpids;
512 u64 cfg;
513
514 cfg = rvu_read64(rvu, blkaddr, NIX_AF_CONST1);
515 max_bpids = FIELD_GET(NIX_CONST_MAX_BPIDS, cfg);
516
517 /* Reserve the BPIds for CGX and SDP */
518 bp->cgx_bpid_cnt = rvu->hw->cgx_links * NIX_BPIDS_PER_LMAC;
519 bp->sdp_bpid_cnt = rvu->hw->sdp_links * FIELD_GET(NIX_CONST_SDP_CHANS, cfg);
520 bp->free_pool_base = bp->cgx_bpid_cnt + bp->sdp_bpid_cnt +
521 NIX_BPIDS_PER_CPT;
522 bp->bpids.max = max_bpids - bp->free_pool_base;
523
524 err = rvu_alloc_bitmap(&bp->bpids);
525 if (err)
526 return err;
527
528 bp->fn_map = devm_kcalloc(rvu->dev, bp->bpids.max,
529 sizeof(u16), GFP_KERNEL);
530 if (!bp->fn_map)
531 goto free_bpids;
532
533 bp->intf_map = devm_kcalloc(rvu->dev, bp->bpids.max,
534 sizeof(u8), GFP_KERNEL);
535 if (!bp->intf_map)
536 goto free_bpids;
537
538 bp->ref_cnt = devm_kcalloc(rvu->dev, bp->bpids.max,
539 sizeof(u8), GFP_KERNEL);
540 if (!bp->ref_cnt)
541 goto free_bpids;
542
543 return 0;
544
545 free_bpids:
546 rvu_free_bitmap(&bp->bpids);
547 bp->bpids.bmap = NULL;
548 return -ENOMEM;
549 }
550
551 void rvu_nix_flr_free_bpids(struct rvu *rvu, u16 pcifunc)
552 {
553 int blkaddr, bpid, err;
554 struct nix_hw *nix_hw;
555 struct nix_bp *bp;
556
557 if (!is_lbk_vf(rvu, pcifunc))
558 return;
559
560 err = nix_get_struct_ptrs(rvu, pcifunc, &nix_hw, &blkaddr);
561 if (err)
562 return;
563
564 bp = &nix_hw->bp;
565
566 mutex_lock(&rvu->rsrc_lock);
567 for (bpid = 0; bpid < bp->bpids.max; bpid++) {
568 if (bp->fn_map[bpid] == pcifunc) {
569 bp->ref_cnt[bpid]--;
570 if (bp->ref_cnt[bpid])
571 continue;
572 rvu_free_rsrc(&bp->bpids, bpid);
573 bp->fn_map[bpid] = 0;
574 }
575 }
576 mutex_unlock(&rvu->rsrc_lock);
577 }
578
579 static u16 nix_get_channel(u16 chan, bool cpt_link)
580 {
581 /* CPT channel for a given link channel is always
582 * assumed to be BIT(11) set in link channel.
583 */
584 return cpt_link ? chan | BIT(11) : chan;
585 }
586
587 static int nix_bp_disable(struct rvu *rvu,
588 struct nix_bp_cfg_req *req,
589 struct msg_rsp *rsp, bool cpt_link)
590 {
591 u16 pcifunc = req->hdr.pcifunc;
592 int blkaddr, pf, type, err;
593 u16 chan_base, chan, bpid;
594 struct rvu_pfvf *pfvf;
595 struct nix_hw *nix_hw;
596 struct nix_bp *bp;
597 u16 chan_v;
598 u64 cfg;
599
600 pf = rvu_get_pf(rvu->pdev, pcifunc);
601 type = is_lbk_vf(rvu, pcifunc) ? NIX_INTF_TYPE_LBK : NIX_INTF_TYPE_CGX;
602 if (!is_pf_cgxmapped(rvu, pf) && type != NIX_INTF_TYPE_LBK)
603 return 0;
604
605 if (is_sdp_pfvf(rvu, pcifunc))
606 type = NIX_INTF_TYPE_SDP;
607
608 if (cpt_link && !rvu->hw->cpt_links)
609 return 0;
610
611 pfvf = rvu_get_pfvf(rvu, pcifunc);
612 err = nix_get_struct_ptrs(rvu, pcifunc, &nix_hw, &blkaddr);
613 if (err)
614 return err;
615
616 bp = &nix_hw->bp;
617 chan_base = pfvf->rx_chan_base + req->chan_base;
618 for (chan = chan_base; chan < (chan_base + req->chan_cnt); chan++) {
619 chan_v = nix_get_channel(chan, cpt_link);
620 cfg = rvu_read64(rvu, blkaddr, NIX_AF_RX_CHANX_CFG(chan_v));
621 rvu_write64(rvu, blkaddr, NIX_AF_RX_CHANX_CFG(chan_v),
622 cfg & ~BIT_ULL(16));
623
624 if (type == NIX_INTF_TYPE_LBK) {
625 bpid = cfg & GENMASK(8, 0);
626 mutex_lock(&rvu->rsrc_lock);
627 rvu_free_rsrc(&bp->bpids, bpid - bp->free_pool_base);
628 for (bpid = 0; bpid < bp->bpids.max; bpid++) {
629 if (bp->fn_map[bpid] == pcifunc) {
630 bp->fn_map[bpid] = 0;
631 bp->ref_cnt[bpid] = 0;
632 }
633 }
634 mutex_unlock(&rvu->rsrc_lock);
635 }
636 }
637 return 0;
638 }
639
640 int rvu_mbox_handler_nix_bp_disable(struct rvu *rvu,
641 struct nix_bp_cfg_req *req,
642 struct msg_rsp *rsp)
643 {
644 return nix_bp_disable(rvu, req, rsp, false);
645 }
646
647 int rvu_mbox_handler_nix_cpt_bp_disable(struct rvu *rvu,
648 struct nix_bp_cfg_req *req,
649 struct msg_rsp *rsp)
650 {
651 return nix_bp_disable(rvu, req, rsp, true);
652 }
653
654 static int rvu_nix_get_bpid(struct rvu *rvu, struct nix_bp_cfg_req *req,
655 int type, int chan_id)
656 {
657 int bpid, blkaddr, sdp_chan_base, err;
658 struct rvu_hwinfo *hw = rvu->hw;
659 struct rvu_pfvf *pfvf;
660 struct nix_hw *nix_hw;
661 u8 cgx_id, lmac_id;
662 struct nix_bp *bp;
663
664 pfvf = rvu_get_pfvf(rvu, req->hdr.pcifunc);
665
666 err = nix_get_struct_ptrs(rvu, req->hdr.pcifunc, &nix_hw, &blkaddr);
667 if (err)
668 return err;
669
670 bp = &nix_hw->bp;
671
672 /* Backpressure IDs range division
673 * CGX channles are mapped to (0 - 191) BPIDs
674 * LBK channles are mapped to (192 - 255) BPIDs
675 * SDP channles are mapped to (256 - 511) BPIDs
676 *
677 * Lmac channles and bpids mapped as follows
678 * cgx(0)_lmac(0)_chan(0 - 15) = bpid(0 - 15)
679 * cgx(0)_lmac(1)_chan(0 - 15) = bpid(16 - 31) ....
680 * cgx(1)_lmac(0)_chan(0 - 15) = bpid(64 - 79) ....
681 */
682 switch (type) {
683 case NIX_INTF_TYPE_CGX:
684 if ((req->chan_base + req->chan_cnt) > NIX_BPIDS_PER_LMAC)
685 return NIX_AF_ERR_INVALID_BPID_REQ;
686 rvu_get_cgx_lmac_id(pfvf->cgx_lmac, &cgx_id, &lmac_id);
687 /* Assign bpid based on cgx, lmac and chan id */
688 bpid = (cgx_id * hw->lmac_per_cgx * NIX_BPIDS_PER_LMAC) +
689 (lmac_id * NIX_BPIDS_PER_LMAC) + req->chan_base;
690
691 if (req->bpid_per_chan)
692 bpid += chan_id;
693 if (bpid > bp->cgx_bpid_cnt)
694 return NIX_AF_ERR_INVALID_BPID;
695 break;
696
697 case NIX_INTF_TYPE_LBK:
698 /* Alloc bpid from the free pool */
699 mutex_lock(&rvu->rsrc_lock);
700 bpid = rvu_alloc_rsrc(&bp->bpids);
701 if (bpid < 0) {
702 mutex_unlock(&rvu->rsrc_lock);
703 return NIX_AF_ERR_INVALID_BPID;
704 }
705 bp->fn_map[bpid] = req->hdr.pcifunc;
706 bp->ref_cnt[bpid]++;
707 bpid += bp->free_pool_base;
708 mutex_unlock(&rvu->rsrc_lock);
709 break;
710 case NIX_INTF_TYPE_SDP:
711 if ((req->chan_base + req->chan_cnt) > bp->sdp_bpid_cnt)
712 return NIX_AF_ERR_INVALID_BPID_REQ;
713
714 /* Handle usecase of 2 SDP blocks */
715 if (!hw->cap.programmable_chans)
716 sdp_chan_base = pfvf->rx_chan_base - NIX_CHAN_SDP_CH_START;
717 else
718 sdp_chan_base = pfvf->rx_chan_base - hw->sdp_chan_base;
719
720 bpid = bp->cgx_bpid_cnt + req->chan_base + sdp_chan_base;
721 if (req->bpid_per_chan)
722 bpid += chan_id;
723
724 if (bpid > (bp->cgx_bpid_cnt + bp->sdp_bpid_cnt))
725 return NIX_AF_ERR_INVALID_BPID;
726 break;
727 default:
728 return -EINVAL;
729 }
730 return bpid;
731 }
732
733 static int nix_bp_enable(struct rvu *rvu,
734 struct nix_bp_cfg_req *req,
735 struct nix_bp_cfg_rsp *rsp,
736 bool cpt_link)
737 {
738 int blkaddr, pf, type, chan_id = 0;
739 u16 pcifunc = req->hdr.pcifunc;
740 struct rvu_pfvf *pfvf;
741 u16 chan_base, chan;
742 s16 bpid, bpid_base;
743 u16 chan_v;
744 u64 cfg;
745
746 pf = rvu_get_pf(rvu->pdev, pcifunc);
747 type = is_lbk_vf(rvu, pcifunc) ? NIX_INTF_TYPE_LBK : NIX_INTF_TYPE_CGX;
748 if (is_sdp_pfvf(rvu, pcifunc))
749 type = NIX_INTF_TYPE_SDP;
750
751 /* Enable backpressure only for CGX mapped PFs and LBK/SDP interface */
752 if (!is_pf_cgxmapped(rvu, pf) && type != NIX_INTF_TYPE_LBK &&
753 type != NIX_INTF_TYPE_SDP)
754 return 0;
755
756 if (cpt_link && !rvu->hw->cpt_links)
757 return 0;
758
759 pfvf = rvu_get_pfvf(rvu, pcifunc);
760 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NIX, pcifunc);
761
762 bpid_base = rvu_nix_get_bpid(rvu, req, type, chan_id);
763 chan_base = pfvf->rx_chan_base + req->chan_base;
764 bpid = bpid_base;
765
766 for (chan = chan_base; chan < (chan_base + req->chan_cnt); chan++) {
767 if (bpid < 0) {
768 dev_warn(rvu->dev, "Fail to enable backpressure\n");
769 return -EINVAL;
770 }
771
772 chan_v = nix_get_channel(chan, cpt_link);
773
774 cfg = rvu_read64(rvu, blkaddr, NIX_AF_RX_CHANX_CFG(chan_v));
775 cfg &= ~GENMASK_ULL(8, 0);
776 rvu_write64(rvu, blkaddr, NIX_AF_RX_CHANX_CFG(chan_v),
777 cfg | (bpid & GENMASK_ULL(8, 0)) | BIT_ULL(16));
778 chan_id++;
779 bpid = rvu_nix_get_bpid(rvu, req, type, chan_id);
780 }
781
782 for (chan = 0; chan < req->chan_cnt; chan++) {
783 /* Map channel and bpid assign to it */
784 rsp->chan_bpid[chan] = ((req->chan_base + chan) & 0x7F) << 10 |
785 (bpid_base & 0x3FF);
786 if (req->bpid_per_chan)
787 bpid_base++;
788 }
789 rsp->chan_cnt = req->chan_cnt;
790
791 return 0;
792 }
793
794 int rvu_mbox_handler_nix_bp_enable(struct rvu *rvu,
795 struct nix_bp_cfg_req *req,
796 struct nix_bp_cfg_rsp *rsp)
797 {
798 return nix_bp_enable(rvu, req, rsp, false);
799 }
800
801 int rvu_mbox_handler_nix_cpt_bp_enable(struct rvu *rvu,
802 struct nix_bp_cfg_req *req,
803 struct nix_bp_cfg_rsp *rsp)
804 {
805 return nix_bp_enable(rvu, req, rsp, true);
806 }
807
808 static void nix_setup_lso_tso_l3(struct rvu *rvu, int blkaddr,
809 u64 format, bool v4, u64 *fidx)
810 {
811 struct nix_lso_format field = {0};
812
813 /* IP's Length field */
814 field.layer = NIX_TXLAYER_OL3;
815 /* In ipv4, length field is at offset 2 bytes, for ipv6 it's 4 */
816 field.offset = v4 ? 2 : 4;
817 field.sizem1 = 1; /* i.e 2 bytes */
818 field.alg = NIX_LSOALG_ADD_PAYLEN;
819 rvu_write64(rvu, blkaddr,
820 NIX_AF_LSO_FORMATX_FIELDX(format, (*fidx)++),
821 *(u64 *)&field);
822
823 /* No ID field in IPv6 header */
824 if (!v4)
825 return;
826
827 /* IP's ID field */
828 field.layer = NIX_TXLAYER_OL3;
829 field.offset = 4;
830 field.sizem1 = 1; /* i.e 2 bytes */
831 field.alg = NIX_LSOALG_ADD_SEGNUM;
832 rvu_write64(rvu, blkaddr,
833 NIX_AF_LSO_FORMATX_FIELDX(format, (*fidx)++),
834 *(u64 *)&field);
835 }
836
837 static void nix_setup_lso_tso_l4(struct rvu *rvu, int blkaddr,
838 u64 format, u64 *fidx)
839 {
840 struct nix_lso_format field = {0};
841
842 /* TCP's sequence number field */
843 field.layer = NIX_TXLAYER_OL4;
844 field.offset = 4;
845 field.sizem1 = 3; /* i.e 4 bytes */
846 field.alg = NIX_LSOALG_ADD_OFFSET;
847 rvu_write64(rvu, blkaddr,
848 NIX_AF_LSO_FORMATX_FIELDX(format, (*fidx)++),
849 *(u64 *)&field);
850
851 /* TCP's flags field */
852 field.layer = NIX_TXLAYER_OL4;
853 field.offset = 12;
854 field.sizem1 = 1; /* 2 bytes */
855 field.alg = NIX_LSOALG_TCP_FLAGS;
856 rvu_write64(rvu, blkaddr,
857 NIX_AF_LSO_FORMATX_FIELDX(format, (*fidx)++),
858 *(u64 *)&field);
859 }
860
861 static void nix_setup_lso(struct rvu *rvu, struct nix_hw *nix_hw, int blkaddr)
862 {
863 u64 cfg, idx, fidx = 0;
864
865 /* Get max HW supported format indices */
866 cfg = (rvu_read64(rvu, blkaddr, NIX_AF_CONST1) >> 48) & 0xFF;
867 nix_hw->lso.total = cfg;
868
869 /* Enable LSO */
870 cfg = rvu_read64(rvu, blkaddr, NIX_AF_LSO_CFG);
871 /* For TSO, set first and middle segment flags to
872 * mask out PSH, RST & FIN flags in TCP packet
873 */
874 cfg &= ~((0xFFFFULL << 32) | (0xFFFFULL << 16));
875 cfg |= (0xFFF2ULL << 32) | (0xFFF2ULL << 16);
876 rvu_write64(rvu, blkaddr, NIX_AF_LSO_CFG, cfg | BIT_ULL(63));
877
878 /* Setup default static LSO formats
879 *
880 * Configure format fields for TCPv4 segmentation offload
881 */
882 idx = NIX_LSO_FORMAT_IDX_TSOV4;
883 nix_setup_lso_tso_l3(rvu, blkaddr, idx, true, &fidx);
884 nix_setup_lso_tso_l4(rvu, blkaddr, idx, &fidx);
885
886 /* Set rest of the fields to NOP */
887 for (; fidx < 8; fidx++) {
888 rvu_write64(rvu, blkaddr,
889 NIX_AF_LSO_FORMATX_FIELDX(idx, fidx), 0x0ULL);
890 }
891 nix_hw->lso.in_use++;
892
893 /* Configure format fields for TCPv6 segmentation offload */
894 idx = NIX_LSO_FORMAT_IDX_TSOV6;
895 fidx = 0;
896 nix_setup_lso_tso_l3(rvu, blkaddr, idx, false, &fidx);
897 nix_setup_lso_tso_l4(rvu, blkaddr, idx, &fidx);
898
899 /* Set rest of the fields to NOP */
900 for (; fidx < 8; fidx++) {
901 rvu_write64(rvu, blkaddr,
902 NIX_AF_LSO_FORMATX_FIELDX(idx, fidx), 0x0ULL);
903 }
904 nix_hw->lso.in_use++;
905 }
906
907 static void nix_ctx_free(struct rvu *rvu, struct rvu_pfvf *pfvf)
908 {
909 kfree(pfvf->rq_bmap);
910 kfree(pfvf->sq_bmap);
911 kfree(pfvf->cq_bmap);
912 if (pfvf->rq_ctx)
913 qmem_free(rvu->dev, pfvf->rq_ctx);
914 if (pfvf->sq_ctx)
915 qmem_free(rvu->dev, pfvf->sq_ctx);
916 if (pfvf->cq_ctx)
917 qmem_free(rvu->dev, pfvf->cq_ctx);
918 if (pfvf->rss_ctx)
919 qmem_free(rvu->dev, pfvf->rss_ctx);
920 if (pfvf->nix_qints_ctx)
921 qmem_free(rvu->dev, pfvf->nix_qints_ctx);
922 if (pfvf->cq_ints_ctx)
923 qmem_free(rvu->dev, pfvf->cq_ints_ctx);
924
925 pfvf->rq_bmap = NULL;
926 pfvf->cq_bmap = NULL;
927 pfvf->sq_bmap = NULL;
928 pfvf->rq_ctx = NULL;
929 pfvf->sq_ctx = NULL;
930 pfvf->cq_ctx = NULL;
931 pfvf->rss_ctx = NULL;
932 pfvf->nix_qints_ctx = NULL;
933 pfvf->cq_ints_ctx = NULL;
934 }
935
936 static int nixlf_rss_ctx_init(struct rvu *rvu, int blkaddr,
937 struct rvu_pfvf *pfvf, int nixlf,
938 int rss_sz, int rss_grps, int hwctx_size,
939 u64 way_mask, bool tag_lsb_as_adder)
940 {
941 int err, grp, num_indices;
942 u64 val;
943
944 /* RSS is not requested for this NIXLF */
945 if (!rss_sz)
946 return 0;
947 num_indices = rss_sz * rss_grps;
948
949 /* Alloc NIX RSS HW context memory and config the base */
950 err = qmem_alloc(rvu->dev, &pfvf->rss_ctx, num_indices, hwctx_size);
951 if (err)
952 return err;
953
954 rvu_write64(rvu, blkaddr, NIX_AF_LFX_RSS_BASE(nixlf),
955 (u64)pfvf->rss_ctx->iova);
956
957 /* Config full RSS table size, enable RSS and caching */
958 val = BIT_ULL(36) | BIT_ULL(4) | way_mask << 20 |
959 ilog2(num_indices / MAX_RSS_INDIR_TBL_SIZE);
960
961 if (tag_lsb_as_adder)
962 val |= BIT_ULL(5);
963
964 rvu_write64(rvu, blkaddr, NIX_AF_LFX_RSS_CFG(nixlf), val);
965 /* Config RSS group offset and sizes */
966 for (grp = 0; grp < rss_grps; grp++)
967 rvu_write64(rvu, blkaddr, NIX_AF_LFX_RSS_GRPX(nixlf, grp),
968 ((ilog2(rss_sz) - 1) << 16) | (rss_sz * grp));
969 return 0;
970 }
971
972 static int nix_aq_enqueue_wait(struct rvu *rvu, struct rvu_block *block,
973 struct nix_aq_inst_s *inst)
974 {
975 struct admin_queue *aq = block->aq;
976 struct nix_aq_res_s *result;
977 int timeout = 1000;
978 u64 reg, head;
979 int ret;
980
981 result = (struct nix_aq_res_s *)aq->res->base;
982
983 /* Get current head pointer where to append this instruction */
984 reg = rvu_read64(rvu, block->addr, NIX_AF_AQ_STATUS);
985 head = (reg >> 4) & AQ_PTR_MASK;
986
987 memcpy((void *)(aq->inst->base + (head * aq->inst->entry_sz)),
988 (void *)inst, aq->inst->entry_sz);
989 memset(result, 0, sizeof(*result));
990 /* sync into memory */
991 wmb();
992
993 /* Ring the doorbell and wait for result */
994 rvu_write64(rvu, block->addr, NIX_AF_AQ_DOOR, 1);
995 while (result->compcode == NIX_AQ_COMP_NOTDONE) {
996 cpu_relax();
997 udelay(1);
998 timeout--;
999 if (!timeout)
1000 return -EBUSY;
1001 }
1002
1003 if (result->compcode != NIX_AQ_COMP_GOOD) {
1004 /* TODO: Replace this with some error code */
1005 if (result->compcode == NIX_AQ_COMP_CTX_FAULT ||
1006 result->compcode == NIX_AQ_COMP_LOCKERR ||
1007 result->compcode == NIX_AQ_COMP_CTX_POISON) {
1008 ret = rvu_ndc_fix_locked_cacheline(rvu, BLKADDR_NDC_NIX0_RX);
1009 ret |= rvu_ndc_fix_locked_cacheline(rvu, BLKADDR_NDC_NIX0_TX);
1010 ret |= rvu_ndc_fix_locked_cacheline(rvu, BLKADDR_NDC_NIX1_RX);
1011 ret |= rvu_ndc_fix_locked_cacheline(rvu, BLKADDR_NDC_NIX1_TX);
1012 if (ret)
1013 dev_err(rvu->dev,
1014 "%s: Not able to unlock cachelines\n", __func__);
1015 }
1016
1017 return -EBUSY;
1018 }
1019
1020 return 0;
1021 }
1022
1023 static void nix_get_aq_req_smq(struct rvu *rvu, struct nix_aq_enq_req *req,
1024 u16 *smq, u16 *smq_mask)
1025 {
1026 struct nix_cn10k_aq_enq_req *aq_req;
1027
1028 if (is_cn20k(rvu->pdev)) {
1029 *smq = ((struct nix_cn20k_aq_enq_req *)req)->sq.smq;
1030 *smq_mask = ((struct nix_cn20k_aq_enq_req *)req)->sq_mask.smq;
1031 return;
1032 }
1033
1034 if (!is_rvu_otx2(rvu)) {
1035 aq_req = (struct nix_cn10k_aq_enq_req *)req;
1036 *smq = aq_req->sq.smq;
1037 *smq_mask = aq_req->sq_mask.smq;
1038 } else {
1039 *smq = req->sq.smq;
1040 *smq_mask = req->sq_mask.smq;
1041 }
1042 }
1043
1044 static int rvu_nix_blk_aq_enq_inst(struct rvu *rvu, struct nix_hw *nix_hw,
1045 struct nix_aq_enq_req *req,
1046 struct nix_aq_enq_rsp *rsp)
1047 {
1048 struct rvu_hwinfo *hw = rvu->hw;
1049 u16 pcifunc = req->hdr.pcifunc;
1050 int nixlf, blkaddr, rc = 0;
1051 struct nix_aq_inst_s inst;
1052 struct rvu_block *block;
1053 struct admin_queue *aq;
1054 struct rvu_pfvf *pfvf;
1055 u16 smq, smq_mask;
1056 void *ctx, *mask;
1057 bool ena;
1058 u64 cfg;
1059
1060 blkaddr = nix_hw->blkaddr;
1061 block = &hw->block[blkaddr];
1062 aq = block->aq;
1063 if (!aq) {
1064 dev_warn(rvu->dev, "%s: NIX AQ not initialized\n", __func__);
1065 return NIX_AF_ERR_AQ_ENQUEUE;
1066 }
1067
1068 pfvf = rvu_get_pfvf(rvu, pcifunc);
1069 nixlf = rvu_get_lf(rvu, block, pcifunc, 0);
1070
1071 /* Skip NIXLF check for broadcast MCE entry and bandwidth profile
1072 * operations done by AF itself.
1073 */
1074 if (!((!rsp && req->ctype == NIX_AQ_CTYPE_MCE) ||
1075 (req->ctype == NIX_AQ_CTYPE_BANDPROF && !pcifunc))) {
1076 if (!pfvf->nixlf || nixlf < 0)
1077 return NIX_AF_ERR_AF_LF_INVALID;
1078 }
1079
1080 switch (req->ctype) {
1081 case NIX_AQ_CTYPE_RQ:
1082 /* Check if index exceeds max no of queues */
1083 if (!pfvf->rq_ctx || req->qidx >= pfvf->rq_ctx->qsize)
1084 rc = NIX_AF_ERR_AQ_ENQUEUE;
1085 break;
1086 case NIX_AQ_CTYPE_SQ:
1087 if (!pfvf->sq_ctx || req->qidx >= pfvf->sq_ctx->qsize)
1088 rc = NIX_AF_ERR_AQ_ENQUEUE;
1089 break;
1090 case NIX_AQ_CTYPE_CQ:
1091 if (!pfvf->cq_ctx || req->qidx >= pfvf->cq_ctx->qsize)
1092 rc = NIX_AF_ERR_AQ_ENQUEUE;
1093 break;
1094 case NIX_AQ_CTYPE_RSS:
1095 /* Check if RSS is enabled and qidx is within range */
1096 cfg = rvu_read64(rvu, blkaddr, NIX_AF_LFX_RSS_CFG(nixlf));
1097 if (!(cfg & BIT_ULL(4)) || !pfvf->rss_ctx ||
1098 (req->qidx >= (256UL << (cfg & 0xF))))
1099 rc = NIX_AF_ERR_AQ_ENQUEUE;
1100 break;
1101 case NIX_AQ_CTYPE_MCE:
1102 cfg = rvu_read64(rvu, blkaddr, NIX_AF_RX_MCAST_CFG);
1103
1104 /* Check if index exceeds MCE list length */
1105 if (!nix_hw->mcast.mce_ctx ||
1106 (req->qidx >= (256UL << (cfg & 0xF))))
1107 rc = NIX_AF_ERR_AQ_ENQUEUE;
1108
1109 /* Adding multicast lists for requests from PF/VFs is not
1110 * yet supported, so ignore this.
1111 */
1112 if (rsp)
1113 rc = NIX_AF_ERR_AQ_ENQUEUE;
1114 break;
1115 case NIX_AQ_CTYPE_BANDPROF:
1116 if (nix_verify_bandprof((struct nix_cn10k_aq_enq_req *)req,
1117 nix_hw, pcifunc))
1118 rc = NIX_AF_ERR_INVALID_BANDPROF;
1119 break;
1120 default:
1121 rc = NIX_AF_ERR_AQ_ENQUEUE;
1122 }
1123
1124 if (rc)
1125 return rc;
1126
1127 nix_get_aq_req_smq(rvu, req, &smq, &smq_mask);
1128 /* Check if SQ pointed SMQ belongs to this PF/VF or not */
1129 if (req->ctype == NIX_AQ_CTYPE_SQ &&
1130 ((req->op == NIX_AQ_INSTOP_INIT && req->sq.ena) ||
1131 (req->op == NIX_AQ_INSTOP_WRITE &&
1132 req->sq_mask.ena && req->sq.ena && smq_mask))) {
1133 if (!is_valid_txschq(rvu, blkaddr, NIX_TXSCH_LVL_SMQ,
1134 pcifunc, smq))
1135 return NIX_AF_ERR_AQ_ENQUEUE;
1136 }
1137
1138 memset(&inst, 0, sizeof(struct nix_aq_inst_s));
1139 inst.lf = nixlf;
1140 inst.cindex = req->qidx;
1141 inst.ctype = req->ctype;
1142 inst.op = req->op;
1143 /* Currently we are not supporting enqueuing multiple instructions,
1144 * so always choose first entry in result memory.
1145 */
1146 inst.res_addr = (u64)aq->res->iova;
1147
1148 /* Hardware uses same aq->res->base for updating result of
1149 * previous instruction hence wait here till it is done.
1150 */
1151 spin_lock(&aq->lock);
1152
1153 /* Clean result + context memory */
1154 memset(aq->res->base, 0, aq->res->entry_sz);
1155 /* Context needs to be written at RES_ADDR + 128 */
1156 ctx = aq->res->base + 128;
1157 /* Mask needs to be written at RES_ADDR + 256 */
1158 mask = aq->res->base + 256;
1159
1160 switch (req->op) {
1161 case NIX_AQ_INSTOP_WRITE:
1162 if (req->ctype == NIX_AQ_CTYPE_RQ)
1163 memcpy(mask, &req->rq_mask,
1164 NIX_MAX_CTX_SIZE);
1165 else if (req->ctype == NIX_AQ_CTYPE_SQ)
1166 memcpy(mask, &req->sq_mask,
1167 NIX_MAX_CTX_SIZE);
1168 else if (req->ctype == NIX_AQ_CTYPE_CQ)
1169 memcpy(mask, &req->cq_mask,
1170 NIX_MAX_CTX_SIZE);
1171 else if (req->ctype == NIX_AQ_CTYPE_RSS)
1172 memcpy(mask, &req->rss_mask,
1173 NIX_MAX_CTX_SIZE);
1174 else if (req->ctype == NIX_AQ_CTYPE_MCE)
1175 memcpy(mask, &req->mce_mask,
1176 NIX_MAX_CTX_SIZE);
1177 else if (req->ctype == NIX_AQ_CTYPE_BANDPROF)
1178 memcpy(mask, &req->prof_mask,
1179 NIX_MAX_CTX_SIZE);
1180 fallthrough;
1181 case NIX_AQ_INSTOP_INIT:
1182 if (req->ctype == NIX_AQ_CTYPE_RQ)
1183 memcpy(ctx, &req->rq, NIX_MAX_CTX_SIZE);
1184 else if (req->ctype == NIX_AQ_CTYPE_SQ)
1185 memcpy(ctx, &req->sq, NIX_MAX_CTX_SIZE);
1186 else if (req->ctype == NIX_AQ_CTYPE_CQ)
1187 memcpy(ctx, &req->cq, NIX_MAX_CTX_SIZE);
1188 else if (req->ctype == NIX_AQ_CTYPE_RSS)
1189 memcpy(ctx, &req->rss, NIX_MAX_CTX_SIZE);
1190 else if (req->ctype == NIX_AQ_CTYPE_MCE)
1191 memcpy(ctx, &req->mce, NIX_MAX_CTX_SIZE);
1192 else if (req->ctype == NIX_AQ_CTYPE_BANDPROF)
1193 memcpy(ctx, &req->prof, NIX_MAX_CTX_SIZE);
1194 break;
1195 case NIX_AQ_INSTOP_NOP:
1196 case NIX_AQ_INSTOP_READ:
1197 case NIX_AQ_INSTOP_LOCK:
1198 case NIX_AQ_INSTOP_UNLOCK:
1199 break;
1200 default:
1201 rc = NIX_AF_ERR_AQ_ENQUEUE;
1202 spin_unlock(&aq->lock);
1203 return rc;
1204 }
1205
1206 /* Submit the instruction to AQ */
1207 rc = nix_aq_enqueue_wait(rvu, block, &inst);
1208 if (rc) {
1209 spin_unlock(&aq->lock);
1210 return rc;
1211 }
1212
1213 /* Set RQ/SQ/CQ bitmap if respective queue hw context is enabled */
1214 if (req->op == NIX_AQ_INSTOP_INIT) {
1215 if (req->ctype == NIX_AQ_CTYPE_RQ && req->rq.ena)
1216 __set_bit(req->qidx, pfvf->rq_bmap);
1217 if (req->ctype == NIX_AQ_CTYPE_SQ && req->sq.ena)
1218 __set_bit(req->qidx, pfvf->sq_bmap);
1219 if (req->ctype == NIX_AQ_CTYPE_CQ && req->cq.ena)
1220 __set_bit(req->qidx, pfvf->cq_bmap);
1221 }
1222
1223 if (req->op == NIX_AQ_INSTOP_WRITE) {
1224 if (req->ctype == NIX_AQ_CTYPE_RQ) {
1225 ena = (req->rq.ena & req->rq_mask.ena) |
1226 (test_bit(req->qidx, pfvf->rq_bmap) &
1227 ~req->rq_mask.ena);
1228 if (ena)
1229 __set_bit(req->qidx, pfvf->rq_bmap);
1230 else
1231 __clear_bit(req->qidx, pfvf->rq_bmap);
1232 }
1233 if (req->ctype == NIX_AQ_CTYPE_SQ) {
1234 ena = (req->rq.ena & req->sq_mask.ena) |
1235 (test_bit(req->qidx, pfvf->sq_bmap) &
1236 ~req->sq_mask.ena);
1237 if (ena)
1238 __set_bit(req->qidx, pfvf->sq_bmap);
1239 else
1240 __clear_bit(req->qidx, pfvf->sq_bmap);
1241 }
1242 if (req->ctype == NIX_AQ_CTYPE_CQ) {
1243 ena = (req->rq.ena & req->cq_mask.ena) |
1244 (test_bit(req->qidx, pfvf->cq_bmap) &
1245 ~req->cq_mask.ena);
1246 if (ena)
1247 __set_bit(req->qidx, pfvf->cq_bmap);
1248 else
1249 __clear_bit(req->qidx, pfvf->cq_bmap);
1250 }
1251 }
1252
1253 if (rsp) {
1254 /* Copy read context into mailbox */
1255 if (req->op == NIX_AQ_INSTOP_READ) {
1256 if (req->ctype == NIX_AQ_CTYPE_RQ)
1257 memcpy(&rsp->rq, ctx,
1258 NIX_MAX_CTX_SIZE);
1259 else if (req->ctype == NIX_AQ_CTYPE_SQ)
1260 memcpy(&rsp->sq, ctx,
1261 NIX_MAX_CTX_SIZE);
1262 else if (req->ctype == NIX_AQ_CTYPE_CQ)
1263 memcpy(&rsp->cq, ctx,
1264 NIX_MAX_CTX_SIZE);
1265 else if (req->ctype == NIX_AQ_CTYPE_RSS)
1266 memcpy(&rsp->rss, ctx,
1267 NIX_MAX_CTX_SIZE);
1268 else if (req->ctype == NIX_AQ_CTYPE_MCE)
1269 memcpy(&rsp->mce, ctx,
1270 NIX_MAX_CTX_SIZE);
1271 else if (req->ctype == NIX_AQ_CTYPE_BANDPROF)
1272 memcpy(&rsp->prof, ctx,
1273 NIX_MAX_CTX_SIZE);
1274 }
1275 }
1276
1277 spin_unlock(&aq->lock);
1278 return 0;
1279 }
1280
1281 static int rvu_nix_verify_aq_ctx(struct rvu *rvu, struct nix_hw *nix_hw,
1282 struct nix_aq_enq_req *req, u8 ctype)
1283 {
1284 struct nix_cn10k_aq_enq_req aq_req;
1285 struct nix_cn10k_aq_enq_rsp aq_rsp;
1286 int rc, word;
1287
1288 if (req->ctype != NIX_AQ_CTYPE_CQ)
1289 return 0;
1290
1291 rc = nix_aq_context_read(rvu, nix_hw, &aq_req, &aq_rsp,
1292 req->hdr.pcifunc, ctype, req->qidx);
1293 if (rc) {
1294 dev_err(rvu->dev,
1295 "%s: Failed to fetch %s%d context of PFFUNC 0x%x\n",
1296 __func__, nix_get_ctx_name(ctype), req->qidx,
1297 req->hdr.pcifunc);
1298 return rc;
1299 }
1300
1301 /* Make copy of original context & mask which are required
1302 * for resubmission
1303 */
1304 memcpy(&aq_req.cq_mask, &req->cq_mask, NIX_MAX_CTX_SIZE);
1305 memcpy(&aq_req.cq, &req->cq, NIX_MAX_CTX_SIZE);
1306
1307 /* exclude fields which HW can update */
1308 aq_req.cq_mask.cq_err = 0;
1309 aq_req.cq_mask.wrptr = 0;
1310 aq_req.cq_mask.tail = 0;
1311 aq_req.cq_mask.head = 0;
1312 aq_req.cq_mask.avg_level = 0;
1313 aq_req.cq_mask.update_time = 0;
1314 aq_req.cq_mask.substream = 0;
1315
1316 /* Context mask (cq_mask) holds mask value of fields which
1317 * are changed in AQ WRITE operation.
1318 * for example cq.drop = 0xa;
1319 * cq_mask.drop = 0xff;
1320 * Below logic performs '&' between cq and cq_mask so that non
1321 * updated fields are masked out for request and response
1322 * comparison
1323 */
1324 for (word = 0; word < NIX_MAX_CTX_SIZE / sizeof(u64);
1325 word++) {
1326 *(u64 *)((u8 *)&aq_rsp.cq + word * 8) &=
1327 (*(u64 *)((u8 *)&aq_req.cq_mask + word * 8));
1328 *(u64 *)((u8 *)&aq_req.cq + word * 8) &=
1329 (*(u64 *)((u8 *)&aq_req.cq_mask + word * 8));
1330 }
1331
1332 if (memcmp(&aq_req.cq, &aq_rsp.cq, NIX_MAX_CTX_SIZE))
1333 return NIX_AF_ERR_AQ_CTX_RETRY_WRITE;
1334
1335 return 0;
1336 }
1337
1338 int rvu_nix_aq_enq_inst(struct rvu *rvu, struct nix_aq_enq_req *req,
1339 struct nix_aq_enq_rsp *rsp)
1340 {
1341 struct nix_hw *nix_hw;
1342 int err, retries = 5;
1343 int blkaddr;
1344
1345 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NIX, req->hdr.pcifunc);
1346 if (blkaddr < 0)
1347 return NIX_AF_ERR_AF_LF_INVALID;
1348
1349 nix_hw = get_nix_hw(rvu->hw, blkaddr);
1350 if (!nix_hw)
1351 return NIX_AF_ERR_INVALID_NIXBLK;
1352
1353 retry:
1354 err = rvu_nix_blk_aq_enq_inst(rvu, nix_hw, req, rsp);
1355
1356 /* HW errata 'AQ Modification to CQ could be discarded on heavy traffic'
1357 * As a work around perfrom CQ context read after each AQ write. If AQ
1358 * read shows AQ write is not updated perform AQ write again.
1359 */
1360 if (!err && req->op == NIX_AQ_INSTOP_WRITE) {
1361 err = rvu_nix_verify_aq_ctx(rvu, nix_hw, req, NIX_AQ_CTYPE_CQ);
1362 if (err == NIX_AF_ERR_AQ_CTX_RETRY_WRITE) {
1363 if (retries--)
1364 goto retry;
1365 else
1366 return NIX_AF_ERR_CQ_CTX_WRITE_ERR;
1367 }
1368 }
1369
1370 return err;
1371 }
1372
1373 static const char *nix_get_ctx_name(int ctype)
1374 {
1375 switch (ctype) {
1376 case NIX_AQ_CTYPE_CQ:
1377 return "CQ";
1378 case NIX_AQ_CTYPE_SQ:
1379 return "SQ";
1380 case NIX_AQ_CTYPE_RQ:
1381 return "RQ";
1382 case NIX_AQ_CTYPE_RSS:
1383 return "RSS";
1384 }
1385 return "";
1386 }
1387
1388 static int nix_lf_hwctx_disable(struct rvu *rvu, struct hwctx_disable_req *req)
1389 {
1390 struct rvu_pfvf *pfvf = rvu_get_pfvf(rvu, req->hdr.pcifunc);
1391 struct nix_aq_enq_req aq_req;
1392 unsigned long *bmap;
1393 int qidx, q_cnt = 0;
1394 int err = 0, rc;
1395
1396 if (!pfvf->cq_ctx || !pfvf->sq_ctx || !pfvf->rq_ctx)
1397 return NIX_AF_ERR_AQ_ENQUEUE;
1398
1399 memset(&aq_req, 0, sizeof(struct nix_aq_enq_req));
1400 aq_req.hdr.pcifunc = req->hdr.pcifunc;
1401
1402 if (req->ctype == NIX_AQ_CTYPE_CQ) {
1403 aq_req.cq.ena = 0;
1404 aq_req.cq_mask.ena = 1;
1405 aq_req.cq.bp_ena = 0;
1406 aq_req.cq_mask.bp_ena = 1;
1407 q_cnt = pfvf->cq_ctx->qsize;
1408 bmap = pfvf->cq_bmap;
1409 }
1410 if (req->ctype == NIX_AQ_CTYPE_SQ) {
1411 aq_req.sq.ena = 0;
1412 aq_req.sq_mask.ena = 1;
1413 q_cnt = pfvf->sq_ctx->qsize;
1414 bmap = pfvf->sq_bmap;
1415 }
1416 if (req->ctype == NIX_AQ_CTYPE_RQ) {
1417 aq_req.rq.ena = 0;
1418 aq_req.rq_mask.ena = 1;
1419 q_cnt = pfvf->rq_ctx->qsize;
1420 bmap = pfvf->rq_bmap;
1421 }
1422
1423 aq_req.ctype = req->ctype;
1424 aq_req.op = NIX_AQ_INSTOP_WRITE;
1425
1426 for (qidx = 0; qidx < q_cnt; qidx++) {
1427 if (!test_bit(qidx, bmap))
1428 continue;
1429 aq_req.qidx = qidx;
1430 rc = rvu_nix_aq_enq_inst(rvu, &aq_req, NULL);
1431 if (rc) {
1432 err = rc;
1433 dev_err(rvu->dev, "Failed to disable %s:%d context\n",
1434 nix_get_ctx_name(req->ctype), qidx);
1435 }
1436 }
1437
1438 return err;
1439 }
1440
1441 #ifdef CONFIG_NDC_DIS_DYNAMIC_CACHING
1442 static int nix_lf_hwctx_lockdown(struct rvu *rvu, struct nix_aq_enq_req *req)
1443 {
1444 struct nix_aq_enq_req lock_ctx_req;
1445 int err;
1446
1447 if (req->op != NIX_AQ_INSTOP_INIT)
1448 return 0;
1449
1450 if (req->ctype == NIX_AQ_CTYPE_MCE ||
1451 req->ctype == NIX_AQ_CTYPE_DYNO)
1452 return 0;
1453
1454 memset(&lock_ctx_req, 0, sizeof(struct nix_aq_enq_req));
1455 lock_ctx_req.hdr.pcifunc = req->hdr.pcifunc;
1456 lock_ctx_req.ctype = req->ctype;
1457 lock_ctx_req.op = NIX_AQ_INSTOP_LOCK;
1458 lock_ctx_req.qidx = req->qidx;
1459 err = rvu_nix_aq_enq_inst(rvu, &lock_ctx_req, NULL);
1460 if (err)
1461 dev_err(rvu->dev,
1462 "PFUNC 0x%x: Failed to lock NIX %s:%d context\n",
1463 req->hdr.pcifunc,
1464 nix_get_ctx_name(req->ctype), req->qidx);
1465 return err;
1466 }
1467
1468 int rvu_mbox_handler_nix_aq_enq(struct rvu *rvu,
1469 struct nix_aq_enq_req *req,
1470 struct nix_aq_enq_rsp *rsp)
1471 {
1472 int err;
1473
1474 err = rvu_nix_aq_enq_inst(rvu, req, rsp);
1475 if (!err)
1476 err = nix_lf_hwctx_lockdown(rvu, req);
1477 return err;
1478 }
1479 #else
1480
1481 int rvu_mbox_handler_nix_aq_enq(struct rvu *rvu,
1482 struct nix_aq_enq_req *req,
1483 struct nix_aq_enq_rsp *rsp)
1484 {
1485 return rvu_nix_aq_enq_inst(rvu, req, rsp);
1486 }
1487 #endif
1488 /* CN10K mbox handler */
1489 int rvu_mbox_handler_nix_cn10k_aq_enq(struct rvu *rvu,
1490 struct nix_cn10k_aq_enq_req *req,
1491 struct nix_cn10k_aq_enq_rsp *rsp)
1492 {
1493 return rvu_nix_aq_enq_inst(rvu, (struct nix_aq_enq_req *)req,
1494 (struct nix_aq_enq_rsp *)rsp);
1495 }
1496
1497 int rvu_mbox_handler_nix_hwctx_disable(struct rvu *rvu,
1498 struct hwctx_disable_req *req,
1499 struct msg_rsp *rsp)
1500 {
1501 return nix_lf_hwctx_disable(rvu, req);
1502 }
1503
1504 int rvu_mbox_handler_nix_lf_alloc(struct rvu *rvu,
1505 struct nix_lf_alloc_req *req,
1506 struct nix_lf_alloc_rsp *rsp)
1507 {
1508 int nixlf, qints, hwctx_size, intf, rc = 0, pf;
1509 u16 bcast, mcast, promisc, ucast;
1510 struct rvu_hwinfo *hw = rvu->hw;
1511 u16 pcifunc = req->hdr.pcifunc;
1512 u8 cgx_id = 0, lmac_id = 0;
1513 bool rules_created = false;
1514 struct rvu_block *block;
1515 struct rvu_pfvf *pfvf;
1516 struct cgx *cgxd;
1517 u64 cfg, ctx_cfg;
1518 int blkaddr;
1519
1520 if (!req->rq_cnt || !req->sq_cnt || !req->cq_cnt)
1521 return NIX_AF_ERR_PARAM;
1522
1523 if (req->way_mask)
1524 req->way_mask &= 0xFFFF;
1525
1526 pfvf = rvu_get_pfvf(rvu, pcifunc);
1527 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NIX, pcifunc);
1528 if (!pfvf->nixlf || blkaddr < 0)
1529 return NIX_AF_ERR_AF_LF_INVALID;
1530
1531 block = &hw->block[blkaddr];
1532 nixlf = rvu_get_lf(rvu, block, pcifunc, 0);
1533 if (nixlf < 0)
1534 return NIX_AF_ERR_AF_LF_INVALID;
1535
1536 /* Check if requested 'NIXLF <=> NPALF' mapping is valid */
1537 if (req->npa_func) {
1538 /* If default, use 'this' NIXLF's PFFUNC */
1539 if (req->npa_func == RVU_DEFAULT_PF_FUNC)
1540 req->npa_func = pcifunc;
1541 if (!is_pffunc_map_valid(rvu, req->npa_func, BLKTYPE_NPA))
1542 return NIX_AF_INVAL_NPA_PF_FUNC;
1543 }
1544
1545 /* Check if requested 'NIXLF <=> SSOLF' mapping is valid */
1546 if (req->sso_func) {
1547 /* If default, use 'this' NIXLF's PFFUNC */
1548 if (req->sso_func == RVU_DEFAULT_PF_FUNC)
1549 req->sso_func = pcifunc;
1550 if (!is_pffunc_map_valid(rvu, req->sso_func, BLKTYPE_SSO))
1551 return NIX_AF_INVAL_SSO_PF_FUNC;
1552 }
1553
1554 /* If RSS is being enabled, check if requested config is valid.
1555 * RSS table size should be power of two, otherwise
1556 * RSS_GRP::OFFSET + adder might go beyond that group or
1557 * won't be able to use entire table.
1558 */
1559 if (req->rss_sz && (req->rss_sz > MAX_RSS_INDIR_TBL_SIZE ||
1560 !is_power_of_2(req->rss_sz)))
1561 return NIX_AF_ERR_RSS_SIZE_INVALID;
1562
1563 if (req->rss_sz &&
1564 (!req->rss_grps || req->rss_grps > MAX_RSS_GROUPS))
1565 return NIX_AF_ERR_RSS_GRPS_INVALID;
1566
1567 /* Reset this NIX LF */
1568 rc = rvu_lf_reset(rvu, block, nixlf);
1569 if (rc) {
1570 dev_err(rvu->dev, "Failed to reset NIX%d LF%d\n",
1571 block->addr - BLKADDR_NIX0, nixlf);
1572 return NIX_AF_ERR_LF_RESET;
1573 }
1574
1575 ctx_cfg = rvu_read64(rvu, blkaddr, NIX_AF_CONST3);
1576
1577 /* Alloc NIX RQ HW context memory and config the base */
1578 hwctx_size = 1UL << ((ctx_cfg >> 4) & 0xF);
1579 rc = qmem_alloc(rvu->dev, &pfvf->rq_ctx, req->rq_cnt, hwctx_size);
1580 if (rc)
1581 goto free_mem;
1582
1583 pfvf->rq_bmap = kcalloc(req->rq_cnt, sizeof(long), GFP_KERNEL);
1584 if (!pfvf->rq_bmap) {
1585 rc = -ENOMEM;
1586 goto free_mem;
1587 }
1588
1589 rvu_write64(rvu, blkaddr, NIX_AF_LFX_RQS_BASE(nixlf),
1590 (u64)pfvf->rq_ctx->iova);
1591
1592 /* Set caching and queue count in HW */
1593 cfg = BIT_ULL(36) | (req->rq_cnt - 1) | req->way_mask << 20;
1594 rvu_write64(rvu, blkaddr, NIX_AF_LFX_RQS_CFG(nixlf), cfg);
1595
1596 /* Alloc NIX SQ HW context memory and config the base */
1597 hwctx_size = 1UL << (ctx_cfg & 0xF);
1598 rc = qmem_alloc(rvu->dev, &pfvf->sq_ctx, req->sq_cnt, hwctx_size);
1599 if (rc)
1600 goto free_mem;
1601
1602 pfvf->sq_bmap = kcalloc(req->sq_cnt, sizeof(long), GFP_KERNEL);
1603 if (!pfvf->sq_bmap) {
1604 rc = -ENOMEM;
1605 goto free_mem;
1606 }
1607
1608 rvu_write64(rvu, blkaddr, NIX_AF_LFX_SQS_BASE(nixlf),
1609 (u64)pfvf->sq_ctx->iova);
1610
1611 cfg = BIT_ULL(36) | (req->sq_cnt - 1) | req->way_mask << 20;
1612 rvu_write64(rvu, blkaddr, NIX_AF_LFX_SQS_CFG(nixlf), cfg);
1613
1614 /* Alloc NIX CQ HW context memory and config the base */
1615 hwctx_size = 1UL << ((ctx_cfg >> 8) & 0xF);
1616 rc = qmem_alloc(rvu->dev, &pfvf->cq_ctx, req->cq_cnt, hwctx_size);
1617 if (rc)
1618 goto free_mem;
1619
1620 pfvf->cq_bmap = kcalloc(req->cq_cnt, sizeof(long), GFP_KERNEL);
1621 if (!pfvf->cq_bmap) {
1622 rc = -ENOMEM;
1623 goto free_mem;
1624 }
1625
1626 rvu_write64(rvu, blkaddr, NIX_AF_LFX_CQS_BASE(nixlf),
1627 (u64)pfvf->cq_ctx->iova);
1628
1629 cfg = BIT_ULL(36) | (req->cq_cnt - 1) | req->way_mask << 20;
1630 rvu_write64(rvu, blkaddr, NIX_AF_LFX_CQS_CFG(nixlf), cfg);
1631
1632 /* Initialize receive side scaling (RSS) */
1633 hwctx_size = 1UL << ((ctx_cfg >> 12) & 0xF);
1634 rc = nixlf_rss_ctx_init(rvu, blkaddr, pfvf, nixlf, req->rss_sz,
1635 req->rss_grps, hwctx_size, req->way_mask,
1636 !!(req->flags & NIX_LF_RSS_TAG_LSB_AS_ADDER));
1637 if (rc)
1638 goto free_mem;
1639
1640 /* Alloc memory for CQINT's HW contexts */
1641 cfg = rvu_read64(rvu, blkaddr, NIX_AF_CONST2);
1642 qints = (cfg >> 24) & 0xFFF;
1643 hwctx_size = 1UL << ((ctx_cfg >> 24) & 0xF);
1644 rc = qmem_alloc(rvu->dev, &pfvf->cq_ints_ctx, qints, hwctx_size);
1645 if (rc)
1646 goto free_mem;
1647
1648 rvu_write64(rvu, blkaddr, NIX_AF_LFX_CINTS_BASE(nixlf),
1649 (u64)pfvf->cq_ints_ctx->iova);
1650
1651 rvu_write64(rvu, blkaddr, NIX_AF_LFX_CINTS_CFG(nixlf),
1652 BIT_ULL(36) | req->way_mask << 20);
1653
1654 /* Alloc memory for QINT's HW contexts */
1655 cfg = rvu_read64(rvu, blkaddr, NIX_AF_CONST2);
1656 qints = (cfg >> 12) & 0xFFF;
1657 hwctx_size = 1UL << ((ctx_cfg >> 20) & 0xF);
1658 rc = qmem_alloc(rvu->dev, &pfvf->nix_qints_ctx, qints, hwctx_size);
1659 if (rc)
1660 goto free_mem;
1661
1662 rvu_write64(rvu, blkaddr, NIX_AF_LFX_QINTS_BASE(nixlf),
1663 (u64)pfvf->nix_qints_ctx->iova);
1664 rvu_write64(rvu, blkaddr, NIX_AF_LFX_QINTS_CFG(nixlf),
1665 BIT_ULL(36) | req->way_mask << 20);
1666
1667 /* Setup VLANX TPID's.
1668 * Use VLAN1 for 802.1Q
1669 * and VLAN0 for 802.1AD.
1670 */
1671 cfg = (0x8100ULL << 16) | 0x88A8ULL;
1672 rvu_write64(rvu, blkaddr, NIX_AF_LFX_TX_CFG(nixlf), cfg);
1673
1674 /* Enable LMTST for this NIX LF */
1675 rvu_write64(rvu, blkaddr, NIX_AF_LFX_TX_CFG2(nixlf), BIT_ULL(0));
1676
1677 /* Set CQE/WQE size, NPA_PF_FUNC for SQBs and also SSO_PF_FUNC */
1678 if (req->npa_func)
1679 cfg = req->npa_func;
1680 if (req->sso_func)
1681 cfg |= (u64)req->sso_func << 16;
1682
1683 cfg |= (u64)req->xqe_sz << 33;
1684 rvu_write64(rvu, blkaddr, NIX_AF_LFX_CFG(nixlf), cfg);
1685
1686 /* Config Rx pkt length, csum checks and apad enable / disable */
1687 rvu_write64(rvu, blkaddr, NIX_AF_LFX_RX_CFG(nixlf), req->rx_cfg);
1688
1689 /* Configure pkind for TX parse config */
1690
1691 pf = rvu_get_pf(rvu->pdev, pcifunc);
1692 cfg = NPC_TX_DEF_PKIND;
1693
1694 if (is_pf_cgxmapped(rvu, pf) && is_vf(pcifunc)) {
1695 rvu_get_cgx_lmac_id(rvu->pf2cgxlmac_map[pf], &cgx_id, &lmac_id);
1696 cgxd = rvu_cgx_pdata(cgx_id, rvu);
1697 mutex_lock(&cgxd->lock);
1698 if (rvu_cgx_is_pkind_config_permitted(rvu, pcifunc))
1699 rvu_write64(rvu, blkaddr, NIX_AF_LFX_TX_PARSE_CFG(nixlf), cfg);
1700 mutex_unlock(&cgxd->lock);
1701 } else {
1702 rvu_write64(rvu, blkaddr, NIX_AF_LFX_TX_PARSE_CFG(nixlf), cfg);
1703 }
1704
1705 if (is_rep_dev(rvu, pcifunc)) {
1706 pfvf->tx_chan_base = RVU_SWITCH_LBK_CHAN;
1707 pfvf->tx_chan_cnt = 1;
1708 goto exit;
1709 }
1710
1711 intf = is_lbk_vf(rvu, pcifunc) ? NIX_INTF_TYPE_LBK : NIX_INTF_TYPE_CGX;
1712 if (is_sdp_pfvf(rvu, pcifunc))
1713 intf = NIX_INTF_TYPE_SDP;
1714
1715 if (is_cn20k(rvu->pdev)) {
1716 rc = npc_cn20k_dft_rules_idx_get(rvu, pcifunc, &bcast, &mcast,
1717 &promisc, &ucast);
1718 if (rc) {
1719 rc = npc_cn20k_dft_rules_alloc(rvu, pcifunc);
1720 if (rc)
1721 goto free_mem;
1722
1723 rules_created = true;
1724 }
1725 }
1726
1727 rc = nix_interface_init(rvu, pcifunc, intf, nixlf, rsp,
1728 !!(req->flags & NIX_LF_LBK_BLK_SEL));
1729 if (rc)
1730 goto free_dft;
1731
1732 /* Disable NPC entries as NIXLF's contexts are not initialized yet */
1733 rvu_npc_disable_default_entries(rvu, pcifunc, nixlf);
1734
1735 /* Configure RX VTAG Type 7 (strip) for vf vlan */
1736 rvu_write64(rvu, blkaddr,
1737 NIX_AF_LFX_RX_VTAG_TYPEX(nixlf, NIX_AF_LFX_RX_VTAG_TYPE7),
1738 VTAGSIZE_T4 | VTAG_STRIP);
1739
1740 goto exit;
1741
1742 free_dft:
1743 if (is_cn20k(rvu->pdev) && rules_created)
1744 npc_cn20k_dft_rules_free(rvu, pcifunc);
1745
1746 free_mem:
1747 nix_ctx_free(rvu, pfvf);
1748
1749 exit:
1750 /* Set macaddr of this PF/VF */
1751 ether_addr_copy(rsp->mac_addr, pfvf->mac_addr);
1752
1753 /* set SQB size info */
1754 cfg = rvu_read64(rvu, blkaddr, NIX_AF_SQ_CONST);
1755 rsp->sqb_size = (cfg >> 34) & 0xFFFF;
1756 rsp->rx_chan_base = pfvf->rx_chan_base;
1757 rsp->tx_chan_base = pfvf->tx_chan_base;
1758 rsp->rx_chan_cnt = pfvf->rx_chan_cnt;
1759 rsp->tx_chan_cnt = pfvf->tx_chan_cnt;
1760 rsp->lso_tsov4_idx = NIX_LSO_FORMAT_IDX_TSOV4;
1761 rsp->lso_tsov6_idx = NIX_LSO_FORMAT_IDX_TSOV6;
1762 /* Get HW supported stat count */
1763 cfg = rvu_read64(rvu, blkaddr, NIX_AF_CONST1);
1764 rsp->lf_rx_stats = ((cfg >> 32) & 0xFF);
1765 rsp->lf_tx_stats = ((cfg >> 24) & 0xFF);
1766 /* Get count of CQ IRQs and error IRQs supported per LF */
1767 cfg = rvu_read64(rvu, blkaddr, NIX_AF_CONST2);
1768 rsp->qints = ((cfg >> 12) & 0xFFF);
1769 rsp->cints = ((cfg >> 24) & 0xFFF);
1770 rsp->cgx_links = hw->cgx_links;
1771 rsp->lbk_links = hw->lbk_links;
1772 rsp->sdp_links = hw->sdp_links;
1773
1774 return rc;
1775 }
1776
1777 int rvu_mbox_handler_nix_lf_free(struct rvu *rvu, struct nix_lf_free_req *req,
1778 struct msg_rsp *rsp)
1779 {
1780 struct rvu_hwinfo *hw = rvu->hw;
1781 u16 pcifunc = req->hdr.pcifunc;
1782 struct rvu_block *block;
1783 int blkaddr, nixlf, err;
1784 struct rvu_pfvf *pfvf;
1785
1786 pfvf = rvu_get_pfvf(rvu, pcifunc);
1787 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NIX, pcifunc);
1788 if (!pfvf->nixlf || blkaddr < 0)
1789 return NIX_AF_ERR_AF_LF_INVALID;
1790
1791 block = &hw->block[blkaddr];
1792 nixlf = rvu_get_lf(rvu, block, pcifunc, 0);
1793 if (nixlf < 0)
1794 return NIX_AF_ERR_AF_LF_INVALID;
1795
1796 if (is_rep_dev(rvu, pcifunc))
1797 goto free_lf;
1798
1799 if (req->flags & NIX_LF_DISABLE_FLOWS)
1800 rvu_npc_disable_mcam_entries(rvu, pcifunc, nixlf);
1801 else
1802 rvu_npc_free_mcam_entries(rvu, pcifunc, nixlf);
1803
1804 /* Free any tx vtag def entries used by this NIX LF */
1805 if (!(req->flags & NIX_LF_DONT_FREE_TX_VTAG))
1806 nix_free_tx_vtag_entries(rvu, pcifunc);
1807
1808 nix_interface_deinit(rvu, pcifunc, nixlf);
1809
1810 free_lf:
1811 /* Reset this NIX LF */
1812 err = rvu_lf_reset(rvu, block, nixlf);
1813 if (err) {
1814 dev_err(rvu->dev, "Failed to reset NIX%d LF%d\n",
1815 block->addr - BLKADDR_NIX0, nixlf);
1816 return NIX_AF_ERR_LF_RESET;
1817 }
1818
1819 nix_ctx_free(rvu, pfvf);
1820
1821 if (is_cn20k(rvu->pdev) && !(req->flags & NIX_LF_DONT_FREE_DFT_IDXS))
1822 npc_cn20k_dft_rules_free(rvu, pcifunc);
1823
1824 return 0;
1825 }
1826
1827 int rvu_mbox_handler_nix_mark_format_cfg(struct rvu *rvu,
1828 struct nix_mark_format_cfg *req,
1829 struct nix_mark_format_cfg_rsp *rsp)
1830 {
1831 u16 pcifunc = req->hdr.pcifunc;
1832 struct nix_hw *nix_hw;
1833 struct rvu_pfvf *pfvf;
1834 int blkaddr, rc;
1835 u32 cfg;
1836
1837 pfvf = rvu_get_pfvf(rvu, pcifunc);
1838 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NIX, pcifunc);
1839 if (!pfvf->nixlf || blkaddr < 0)
1840 return NIX_AF_ERR_AF_LF_INVALID;
1841
1842 nix_hw = get_nix_hw(rvu->hw, blkaddr);
1843 if (!nix_hw)
1844 return NIX_AF_ERR_INVALID_NIXBLK;
1845
1846 cfg = (((u32)req->offset & 0x7) << 16) |
1847 (((u32)req->y_mask & 0xF) << 12) |
1848 (((u32)req->y_val & 0xF) << 8) |
1849 (((u32)req->r_mask & 0xF) << 4) | ((u32)req->r_val & 0xF);
1850
1851 rc = rvu_nix_reserve_mark_format(rvu, nix_hw, blkaddr, cfg);
1852 if (rc < 0) {
1853 dev_err(rvu->dev, "No mark_format_ctl for (pf:%d, vf:%d)",
1854 rvu_get_pf(rvu->pdev, pcifunc),
1855 pcifunc & RVU_PFVF_FUNC_MASK);
1856 return NIX_AF_ERR_MARK_CFG_FAIL;
1857 }
1858
1859 rsp->mark_format_idx = rc;
1860 return 0;
1861 }
1862
1863 /* Handle shaper update specially for few revisions */
1864 static bool
1865 handle_txschq_shaper_update(struct rvu *rvu, int blkaddr, int nixlf,
1866 int lvl, u64 reg, u64 regval)
1867 {
1868 u64 regbase, oldval, sw_xoff = 0;
1869 u64 dbgval, md_debug0 = 0;
1870 unsigned long poll_tmo;
1871 bool rate_reg = 0;
1872 u32 schq;
1873
1874 regbase = reg & 0xFFFF;
1875 schq = TXSCHQ_IDX(reg, TXSCHQ_IDX_SHIFT);
1876
1877 /* Check for rate register */
1878 switch (lvl) {
1879 case NIX_TXSCH_LVL_TL1:
1880 md_debug0 = NIX_AF_TL1X_MD_DEBUG0(schq);
1881 sw_xoff = NIX_AF_TL1X_SW_XOFF(schq);
1882
1883 rate_reg = !!(regbase == NIX_AF_TL1X_CIR(0));
1884 break;
1885 case NIX_TXSCH_LVL_TL2:
1886 md_debug0 = NIX_AF_TL2X_MD_DEBUG0(schq);
1887 sw_xoff = NIX_AF_TL2X_SW_XOFF(schq);
1888
1889 rate_reg = (regbase == NIX_AF_TL2X_CIR(0) ||
1890 regbase == NIX_AF_TL2X_PIR(0));
1891 break;
1892 case NIX_TXSCH_LVL_TL3:
1893 md_debug0 = NIX_AF_TL3X_MD_DEBUG0(schq);
1894 sw_xoff = NIX_AF_TL3X_SW_XOFF(schq);
1895
1896 rate_reg = (regbase == NIX_AF_TL3X_CIR(0) ||
1897 regbase == NIX_AF_TL3X_PIR(0));
1898 break;
1899 case NIX_TXSCH_LVL_TL4:
1900 md_debug0 = NIX_AF_TL4X_MD_DEBUG0(schq);
1901 sw_xoff = NIX_AF_TL4X_SW_XOFF(schq);
1902
1903 rate_reg = (regbase == NIX_AF_TL4X_CIR(0) ||
1904 regbase == NIX_AF_TL4X_PIR(0));
1905 break;
1906 case NIX_TXSCH_LVL_MDQ:
1907 sw_xoff = NIX_AF_MDQX_SW_XOFF(schq);
1908 rate_reg = (regbase == NIX_AF_MDQX_CIR(0) ||
1909 regbase == NIX_AF_MDQX_PIR(0));
1910 break;
1911 }
1912
1913 if (!rate_reg)
1914 return false;
1915
1916 /* Nothing special to do when state is not toggled */
1917 oldval = rvu_read64(rvu, blkaddr, reg);
1918 if ((oldval & 0x1) == (regval & 0x1)) {
1919 rvu_write64(rvu, blkaddr, reg, regval);
1920 return true;
1921 }
1922
1923 /* PIR/CIR disable */
1924 if (!(regval & 0x1)) {
1925 rvu_write64(rvu, blkaddr, sw_xoff, 1);
1926 rvu_write64(rvu, blkaddr, reg, 0);
1927 udelay(4);
1928 rvu_write64(rvu, blkaddr, sw_xoff, 0);
1929 return true;
1930 }
1931
1932 /* PIR/CIR enable */
1933 rvu_write64(rvu, blkaddr, sw_xoff, 1);
1934 if (md_debug0) {
1935 poll_tmo = jiffies + usecs_to_jiffies(10000);
1936 /* Wait until VLD(bit32) == 1 or C_CON(bit48) == 0 */
1937 do {
1938 if (time_after(jiffies, poll_tmo)) {
1939 dev_err(rvu->dev,
1940 "NIXLF%d: TLX%u(lvl %u) CIR/PIR enable failed\n",
1941 nixlf, schq, lvl);
1942 goto exit;
1943 }
1944 usleep_range(1, 5);
1945 dbgval = rvu_read64(rvu, blkaddr, md_debug0);
1946 } while (!(dbgval & BIT_ULL(32)) && (dbgval & BIT_ULL(48)));
1947 }
1948 rvu_write64(rvu, blkaddr, reg, regval);
1949 exit:
1950 rvu_write64(rvu, blkaddr, sw_xoff, 0);
1951 return true;
1952 }
1953
1954 static void nix_reset_tx_schedule(struct rvu *rvu, int blkaddr,
1955 int lvl, int schq)
1956 {
1957 u64 tlx_parent = 0, tlx_schedule = 0;
1958
1959 switch (lvl) {
1960 case NIX_TXSCH_LVL_TL2:
1961 tlx_parent = NIX_AF_TL2X_PARENT(schq);
1962 tlx_schedule = NIX_AF_TL2X_SCHEDULE(schq);
1963 break;
1964 case NIX_TXSCH_LVL_TL3:
1965 tlx_parent = NIX_AF_TL3X_PARENT(schq);
1966 tlx_schedule = NIX_AF_TL3X_SCHEDULE(schq);
1967 break;
1968 case NIX_TXSCH_LVL_TL4:
1969 tlx_parent = NIX_AF_TL4X_PARENT(schq);
1970 tlx_schedule = NIX_AF_TL4X_SCHEDULE(schq);
1971 break;
1972 case NIX_TXSCH_LVL_MDQ:
1973 /* no need to reset SMQ_CFG as HW clears this CSR
1974 * on SMQ flush
1975 */
1976 tlx_parent = NIX_AF_MDQX_PARENT(schq);
1977 tlx_schedule = NIX_AF_MDQX_SCHEDULE(schq);
1978 break;
1979 default:
1980 return;
1981 }
1982
1983 if (tlx_parent)
1984 rvu_write64(rvu, blkaddr, tlx_parent, 0x0);
1985
1986 if (tlx_schedule)
1987 rvu_write64(rvu, blkaddr, tlx_schedule, 0x0);
1988 }
1989
1990 /* Disable shaping of pkts by a scheduler queue
1991 * at a given scheduler level.
1992 */
1993 static void nix_reset_tx_shaping(struct rvu *rvu, int blkaddr,
1994 int nixlf, int lvl, int schq)
1995 {
1996 struct rvu_hwinfo *hw = rvu->hw;
1997 u64 cir_reg = 0, pir_reg = 0;
1998 u64 cfg;
1999
2000 switch (lvl) {
2001 case NIX_TXSCH_LVL_TL1:
2002 cir_reg = NIX_AF_TL1X_CIR(schq);
2003 pir_reg = 0; /* PIR not available at TL1 */
2004 break;
2005 case NIX_TXSCH_LVL_TL2:
2006 cir_reg = NIX_AF_TL2X_CIR(schq);
2007 pir_reg = NIX_AF_TL2X_PIR(schq);
2008 break;
2009 case NIX_TXSCH_LVL_TL3:
2010 cir_reg = NIX_AF_TL3X_CIR(schq);
2011 pir_reg = NIX_AF_TL3X_PIR(schq);
2012 break;
2013 case NIX_TXSCH_LVL_TL4:
2014 cir_reg = NIX_AF_TL4X_CIR(schq);
2015 pir_reg = NIX_AF_TL4X_PIR(schq);
2016 break;
2017 case NIX_TXSCH_LVL_MDQ:
2018 cir_reg = NIX_AF_MDQX_CIR(schq);
2019 pir_reg = NIX_AF_MDQX_PIR(schq);
2020 break;
2021 }
2022
2023 /* Shaper state toggle needs wait/poll */
2024 if (hw->cap.nix_shaper_toggle_wait) {
2025 if (cir_reg)
2026 handle_txschq_shaper_update(rvu, blkaddr, nixlf,
2027 lvl, cir_reg, 0);
2028 if (pir_reg)
2029 handle_txschq_shaper_update(rvu, blkaddr, nixlf,
2030 lvl, pir_reg, 0);
2031 return;
2032 }
2033
2034 if (!cir_reg)
2035 return;
2036 cfg = rvu_read64(rvu, blkaddr, cir_reg);
2037 rvu_write64(rvu, blkaddr, cir_reg, cfg & ~BIT_ULL(0));
2038
2039 if (!pir_reg)
2040 return;
2041 cfg = rvu_read64(rvu, blkaddr, pir_reg);
2042 rvu_write64(rvu, blkaddr, pir_reg, cfg & ~BIT_ULL(0));
2043 }
2044
2045 static void nix_reset_tx_linkcfg(struct rvu *rvu, int blkaddr,
2046 int lvl, int schq)
2047 {
2048 struct rvu_hwinfo *hw = rvu->hw;
2049 int link_level;
2050 int link;
2051
2052 if (lvl >= hw->cap.nix_tx_aggr_lvl)
2053 return;
2054
2055 /* Reset TL4's SDP link config */
2056 if (lvl == NIX_TXSCH_LVL_TL4)
2057 rvu_write64(rvu, blkaddr, NIX_AF_TL4X_SDP_LINK_CFG(schq), 0x00);
2058
2059 link_level = rvu_read64(rvu, blkaddr, NIX_AF_PSE_CHANNEL_LEVEL) & 0x01 ?
2060 NIX_TXSCH_LVL_TL3 : NIX_TXSCH_LVL_TL2;
2061 if (lvl != link_level)
2062 return;
2063
2064 /* Reset TL2's CGX or LBK link config */
2065 for (link = 0; link < (hw->cgx_links + hw->lbk_links); link++)
2066 rvu_write64(rvu, blkaddr,
2067 NIX_AF_TL3_TL2X_LINKX_CFG(schq, link), 0x00);
2068 }
2069
2070 static void nix_clear_tx_xoff(struct rvu *rvu, int blkaddr,
2071 int lvl, int schq)
2072 {
2073 struct rvu_hwinfo *hw = rvu->hw;
2074 u64 reg;
2075
2076 /* Skip this if shaping is not supported */
2077 if (!hw->cap.nix_shaping)
2078 return;
2079
2080 /* Clear level specific SW_XOFF */
2081 switch (lvl) {
2082 case NIX_TXSCH_LVL_TL1:
2083 reg = NIX_AF_TL1X_SW_XOFF(schq);
2084 break;
2085 case NIX_TXSCH_LVL_TL2:
2086 reg = NIX_AF_TL2X_SW_XOFF(schq);
2087 break;
2088 case NIX_TXSCH_LVL_TL3:
2089 reg = NIX_AF_TL3X_SW_XOFF(schq);
2090 break;
2091 case NIX_TXSCH_LVL_TL4:
2092 reg = NIX_AF_TL4X_SW_XOFF(schq);
2093 break;
2094 case NIX_TXSCH_LVL_MDQ:
2095 reg = NIX_AF_MDQX_SW_XOFF(schq);
2096 break;
2097 default:
2098 return;
2099 }
2100
2101 rvu_write64(rvu, blkaddr, reg, 0x0);
2102 }
2103
2104 static int nix_get_tx_link(struct rvu *rvu, u16 pcifunc)
2105 {
2106 struct rvu_hwinfo *hw = rvu->hw;
2107 int pf = rvu_get_pf(rvu->pdev, pcifunc);
2108 u8 cgx_id = 0, lmac_id = 0;
2109
2110 if (is_lbk_vf(rvu, pcifunc)) {/* LBK links */
2111 return hw->cgx_links;
2112 } else if (is_pf_cgxmapped(rvu, pf)) {
2113 rvu_get_cgx_lmac_id(rvu->pf2cgxlmac_map[pf], &cgx_id, &lmac_id);
2114 return (cgx_id * hw->lmac_per_cgx) + lmac_id;
2115 }
2116
2117 /* SDP link */
2118 return hw->cgx_links + hw->lbk_links;
2119 }
2120
2121 static void nix_get_txschq_range(struct rvu *rvu, u16 pcifunc,
2122 int link, int *start, int *end)
2123 {
2124 struct rvu_hwinfo *hw = rvu->hw;
2125 int pf = rvu_get_pf(rvu->pdev, pcifunc);
2126
2127 /* LBK links */
2128 if (is_lbk_vf(rvu, pcifunc) || is_rep_dev(rvu, pcifunc)) {
2129 *start = hw->cap.nix_txsch_per_cgx_lmac * link;
2130 *end = *start + hw->cap.nix_txsch_per_lbk_lmac;
2131 } else if (is_pf_cgxmapped(rvu, pf)) { /* CGX links */
2132 *start = hw->cap.nix_txsch_per_cgx_lmac * link;
2133 *end = *start + hw->cap.nix_txsch_per_cgx_lmac;
2134 } else { /* SDP link */
2135 *start = (hw->cap.nix_txsch_per_cgx_lmac * hw->cgx_links) +
2136 (hw->cap.nix_txsch_per_lbk_lmac * hw->lbk_links);
2137 *end = *start + hw->cap.nix_txsch_per_sdp_lmac;
2138 }
2139 }
2140
2141 static int nix_check_txschq_alloc_req(struct rvu *rvu, int lvl, u16 pcifunc,
2142 struct nix_hw *nix_hw,
2143 struct nix_txsch_alloc_req *req)
2144 {
2145 struct rvu_hwinfo *hw = rvu->hw;
2146 int schq, req_schq, free_cnt;
2147 struct nix_txsch *txsch;
2148 int link, start, end;
2149
2150 txsch = &nix_hw->txsch[lvl];
2151 req_schq = req->schq_contig[lvl] + req->schq[lvl];
2152
2153 if (!req_schq)
2154 return 0;
2155
2156 link = nix_get_tx_link(rvu, pcifunc);
2157
2158 /* For traffic aggregating scheduler level, one queue is enough */
2159 if (lvl >= hw->cap.nix_tx_aggr_lvl) {
2160 if (req_schq != 1)
2161 return NIX_AF_ERR_TLX_ALLOC_FAIL;
2162 return 0;
2163 }
2164
2165 /* Get free SCHQ count and check if request can be accomodated */
2166 if (hw->cap.nix_fixed_txschq_mapping) {
2167 nix_get_txschq_range(rvu, pcifunc, link, &start, &end);
2168 schq = start + (pcifunc & RVU_PFVF_FUNC_MASK);
2169 if (end <= txsch->schq.max && schq < end &&
2170 !test_bit(schq, txsch->schq.bmap))
2171 free_cnt = 1;
2172 else
2173 free_cnt = 0;
2174 } else {
2175 free_cnt = rvu_rsrc_free_count(&txsch->schq);
2176 }
2177
2178 if (free_cnt < req_schq || req->schq[lvl] > MAX_TXSCHQ_PER_FUNC ||
2179 req->schq_contig[lvl] > MAX_TXSCHQ_PER_FUNC)
2180 return NIX_AF_ERR_TLX_ALLOC_FAIL;
2181
2182 /* If contiguous queues are needed, check for availability */
2183 if (!hw->cap.nix_fixed_txschq_mapping && req->schq_contig[lvl] &&
2184 !rvu_rsrc_check_contig(&txsch->schq, req->schq_contig[lvl]))
2185 return NIX_AF_ERR_TLX_ALLOC_FAIL;
2186
2187 return 0;
2188 }
2189
2190 static void nix_txsch_alloc(struct rvu *rvu, struct nix_txsch *txsch,
2191 struct nix_txsch_alloc_rsp *rsp,
2192 int lvl, int start, int end)
2193 {
2194 struct rvu_hwinfo *hw = rvu->hw;
2195 u16 pcifunc = rsp->hdr.pcifunc;
2196 int idx, schq;
2197
2198 /* For traffic aggregating levels, queue alloc is based
2199 * on transmit link to which PF_FUNC is mapped to.
2200 */
2201 if (lvl >= hw->cap.nix_tx_aggr_lvl) {
2202 /* A single TL queue is allocated */
2203 if (rsp->schq_contig[lvl]) {
2204 rsp->schq_contig[lvl] = 1;
2205 rsp->schq_contig_list[lvl][0] = start;
2206 }
2207
2208 /* Both contig and non-contig reqs doesn't make sense here */
2209 if (rsp->schq_contig[lvl])
2210 rsp->schq[lvl] = 0;
2211
2212 if (rsp->schq[lvl]) {
2213 rsp->schq[lvl] = 1;
2214 rsp->schq_list[lvl][0] = start;
2215 }
2216 return;
2217 }
2218
2219 /* Adjust the queue request count if HW supports
2220 * only one queue per level configuration.
2221 */
2222 if (hw->cap.nix_fixed_txschq_mapping) {
2223 idx = pcifunc & RVU_PFVF_FUNC_MASK;
2224 schq = start + idx;
2225 if (idx >= (end - start) || test_bit(schq, txsch->schq.bmap)) {
2226 rsp->schq_contig[lvl] = 0;
2227 rsp->schq[lvl] = 0;
2228 return;
2229 }
2230
2231 if (rsp->schq_contig[lvl]) {
2232 rsp->schq_contig[lvl] = 1;
2233 set_bit(schq, txsch->schq.bmap);
2234 rsp->schq_contig_list[lvl][0] = schq;
2235 rsp->schq[lvl] = 0;
2236 } else if (rsp->schq[lvl]) {
2237 rsp->schq[lvl] = 1;
2238 set_bit(schq, txsch->schq.bmap);
2239 rsp->schq_list[lvl][0] = schq;
2240 }
2241 return;
2242 }
2243
2244 /* Allocate contiguous queue indices requesty first */
2245 if (rsp->schq_contig[lvl]) {
2246 schq = bitmap_find_next_zero_area(txsch->schq.bmap,
2247 txsch->schq.max, start,
2248 rsp->schq_contig[lvl], 0);
2249 if (schq >= end)
2250 rsp->schq_contig[lvl] = 0;
2251 for (idx = 0; idx < rsp->schq_contig[lvl]; idx++) {
2252 set_bit(schq, txsch->schq.bmap);
2253 rsp->schq_contig_list[lvl][idx] = schq;
2254 schq++;
2255 }
2256 }
2257
2258 /* Allocate non-contiguous queue indices */
2259 if (rsp->schq[lvl]) {
2260 idx = 0;
2261 for (schq = start; schq < end; schq++) {
2262 if (!test_bit(schq, txsch->schq.bmap)) {
2263 set_bit(schq, txsch->schq.bmap);
2264 rsp->schq_list[lvl][idx++] = schq;
2265 }
2266 if (idx == rsp->schq[lvl])
2267 break;
2268 }
2269 /* Update how many were allocated */
2270 rsp->schq[lvl] = idx;
2271 }
2272 }
2273
2274 int rvu_mbox_handler_nix_txsch_alloc(struct rvu *rvu,
2275 struct nix_txsch_alloc_req *req,
2276 struct nix_txsch_alloc_rsp *rsp)
2277 {
2278 struct rvu_hwinfo *hw = rvu->hw;
2279 u16 pcifunc = req->hdr.pcifunc;
2280 int link, blkaddr, rc = 0;
2281 int lvl, idx, start, end;
2282 struct nix_txsch *txsch;
2283 struct nix_hw *nix_hw;
2284 u32 *pfvf_map;
2285 int nixlf;
2286 u16 schq;
2287
2288 rc = nix_get_nixlf(rvu, pcifunc, &nixlf, &blkaddr);
2289 if (rc)
2290 return rc;
2291
2292 nix_hw = get_nix_hw(rvu->hw, blkaddr);
2293 if (!nix_hw)
2294 return NIX_AF_ERR_INVALID_NIXBLK;
2295
2296 mutex_lock(&rvu->rsrc_lock);
2297
2298 /* Check if request is valid as per HW capabilities
2299 * and can be accomodated.
2300 */
2301 for (lvl = 0; lvl < NIX_TXSCH_LVL_CNT; lvl++) {
2302 rc = nix_check_txschq_alloc_req(rvu, lvl, pcifunc, nix_hw, req);
2303 if (rc)
2304 goto err;
2305 }
2306
2307 /* Allocate requested Tx scheduler queues */
2308 for (lvl = 0; lvl < NIX_TXSCH_LVL_CNT; lvl++) {
2309 txsch = &nix_hw->txsch[lvl];
2310 pfvf_map = txsch->pfvf_map;
2311
2312 if (!req->schq[lvl] && !req->schq_contig[lvl])
2313 continue;
2314
2315 rsp->schq[lvl] = req->schq[lvl];
2316 rsp->schq_contig[lvl] = req->schq_contig[lvl];
2317
2318 link = nix_get_tx_link(rvu, pcifunc);
2319
2320 if (lvl >= hw->cap.nix_tx_aggr_lvl) {
2321 start = link;
2322 end = link;
2323 } else if (hw->cap.nix_fixed_txschq_mapping) {
2324 nix_get_txschq_range(rvu, pcifunc, link, &start, &end);
2325 } else {
2326 start = 0;
2327 end = txsch->schq.max;
2328 }
2329
2330 nix_txsch_alloc(rvu, txsch, rsp, lvl, start, end);
2331
2332 /* Reset queue config */
2333 for (idx = 0; idx < req->schq_contig[lvl]; idx++) {
2334 schq = rsp->schq_contig_list[lvl][idx];
2335 if (!(TXSCH_MAP_FLAGS(pfvf_map[schq]) &
2336 NIX_TXSCHQ_CFG_DONE))
2337 pfvf_map[schq] = TXSCH_MAP(pcifunc, 0);
2338 nix_reset_tx_linkcfg(rvu, blkaddr, lvl, schq);
2339 nix_reset_tx_shaping(rvu, blkaddr, nixlf, lvl, schq);
2340 nix_reset_tx_schedule(rvu, blkaddr, lvl, schq);
2341 }
2342
2343 for (idx = 0; idx < req->schq[lvl]; idx++) {
2344 schq = rsp->schq_list[lvl][idx];
2345 if (!(TXSCH_MAP_FLAGS(pfvf_map[schq]) &
2346 NIX_TXSCHQ_CFG_DONE))
2347 pfvf_map[schq] = TXSCH_MAP(pcifunc, 0);
2348 nix_reset_tx_linkcfg(rvu, blkaddr, lvl, schq);
2349 nix_reset_tx_shaping(rvu, blkaddr, nixlf, lvl, schq);
2350 nix_reset_tx_schedule(rvu, blkaddr, lvl, schq);
2351 }
2352 }
2353
2354 rsp->aggr_level = hw->cap.nix_tx_aggr_lvl;
2355 rsp->aggr_lvl_rr_prio = TXSCH_TL1_DFLT_RR_PRIO;
2356 rsp->link_cfg_lvl = rvu_read64(rvu, blkaddr,
2357 NIX_AF_PSE_CHANNEL_LEVEL) & 0x01 ?
2358 NIX_TXSCH_LVL_TL3 : NIX_TXSCH_LVL_TL2;
2359 goto exit;
2360 err:
2361 rc = NIX_AF_ERR_TLX_ALLOC_FAIL;
2362 exit:
2363 mutex_unlock(&rvu->rsrc_lock);
2364 return rc;
2365 }
2366
2367 static void nix_smq_flush_fill_ctx(struct rvu *rvu, int blkaddr, int smq,
2368 struct nix_smq_flush_ctx *smq_flush_ctx)
2369 {
2370 struct nix_smq_tree_ctx *smq_tree_ctx;
2371 u64 parent_off, regval;
2372 u16 schq;
2373 int lvl;
2374
2375 smq_flush_ctx->smq = smq;
2376
2377 schq = smq;
2378 for (lvl = NIX_TXSCH_LVL_SMQ; lvl <= NIX_TXSCH_LVL_TL1; lvl++) {
2379 smq_tree_ctx = &smq_flush_ctx->smq_tree_ctx[lvl];
2380 smq_tree_ctx->schq = schq;
2381 if (lvl == NIX_TXSCH_LVL_TL1) {
2382 smq_tree_ctx->cir_off = NIX_AF_TL1X_CIR(schq);
2383 smq_tree_ctx->pir_off = 0;
2384 smq_tree_ctx->pir_val = 0;
2385 parent_off = 0;
2386 } else if (lvl == NIX_TXSCH_LVL_TL2) {
2387 smq_tree_ctx->cir_off = NIX_AF_TL2X_CIR(schq);
2388 smq_tree_ctx->pir_off = NIX_AF_TL2X_PIR(schq);
2389 parent_off = NIX_AF_TL2X_PARENT(schq);
2390 } else if (lvl == NIX_TXSCH_LVL_TL3) {
2391 smq_tree_ctx->cir_off = NIX_AF_TL3X_CIR(schq);
2392 smq_tree_ctx->pir_off = NIX_AF_TL3X_PIR(schq);
2393 parent_off = NIX_AF_TL3X_PARENT(schq);
2394 } else if (lvl == NIX_TXSCH_LVL_TL4) {
2395 smq_tree_ctx->cir_off = NIX_AF_TL4X_CIR(schq);
2396 smq_tree_ctx->pir_off = NIX_AF_TL4X_PIR(schq);
2397 parent_off = NIX_AF_TL4X_PARENT(schq);
2398 } else if (lvl == NIX_TXSCH_LVL_MDQ) {
2399 smq_tree_ctx->cir_off = NIX_AF_MDQX_CIR(schq);
2400 smq_tree_ctx->pir_off = NIX_AF_MDQX_PIR(schq);
2401 parent_off = NIX_AF_MDQX_PARENT(schq);
2402 }
2403 /* save cir/pir register values */
2404 smq_tree_ctx->cir_val = rvu_read64(rvu, blkaddr, smq_tree_ctx->cir_off);
2405 if (smq_tree_ctx->pir_off)
2406 smq_tree_ctx->pir_val = rvu_read64(rvu, blkaddr, smq_tree_ctx->pir_off);
2407
2408 /* get parent txsch node */
2409 if (parent_off) {
2410 regval = rvu_read64(rvu, blkaddr, parent_off);
2411 schq = (regval >> 16) & 0x1FF;
2412 }
2413 }
2414 }
2415
2416 static void nix_smq_flush_enadis_xoff(struct rvu *rvu, int blkaddr,
2417 struct nix_smq_flush_ctx *smq_flush_ctx, bool enable)
2418 {
2419 struct nix_txsch *txsch;
2420 struct nix_hw *nix_hw;
2421 int tl2, tl2_schq;
2422 u64 regoff;
2423
2424 nix_hw = get_nix_hw(rvu->hw, blkaddr);
2425 if (!nix_hw)
2426 return;
2427
2428 /* loop through all TL2s with matching PF_FUNC */
2429 txsch = &nix_hw->txsch[NIX_TXSCH_LVL_TL2];
2430 tl2_schq = smq_flush_ctx->smq_tree_ctx[NIX_TXSCH_LVL_TL2].schq;
2431 for (tl2 = 0; tl2 < txsch->schq.max; tl2++) {
2432 /* skip the smq(flush) TL2 */
2433 if (tl2 == tl2_schq)
2434 continue;
2435 /* skip unused TL2s */
2436 if (TXSCH_MAP_FLAGS(txsch->pfvf_map[tl2]) & NIX_TXSCHQ_FREE)
2437 continue;
2438 /* skip if PF_FUNC doesn't match */
2439 if ((TXSCH_MAP_FUNC(txsch->pfvf_map[tl2]) & ~RVU_PFVF_FUNC_MASK) !=
2440 (TXSCH_MAP_FUNC(txsch->pfvf_map[tl2_schq]) &
2441 ~RVU_PFVF_FUNC_MASK))
2442 continue;
2443 /* enable/disable XOFF */
2444 regoff = NIX_AF_TL2X_SW_XOFF(tl2);
2445 if (enable)
2446 rvu_write64(rvu, blkaddr, regoff, 0x1);
2447 else
2448 rvu_write64(rvu, blkaddr, regoff, 0x0);
2449 }
2450 }
2451
2452 static void nix_smq_flush_enadis_rate(struct rvu *rvu, int blkaddr,
2453 struct nix_smq_flush_ctx *smq_flush_ctx, bool enable)
2454 {
2455 u64 cir_off, pir_off, cir_val, pir_val;
2456 struct nix_smq_tree_ctx *smq_tree_ctx;
2457 int lvl;
2458
2459 for (lvl = NIX_TXSCH_LVL_SMQ; lvl <= NIX_TXSCH_LVL_TL1; lvl++) {
2460 smq_tree_ctx = &smq_flush_ctx->smq_tree_ctx[lvl];
2461 cir_off = smq_tree_ctx->cir_off;
2462 cir_val = smq_tree_ctx->cir_val;
2463 pir_off = smq_tree_ctx->pir_off;
2464 pir_val = smq_tree_ctx->pir_val;
2465
2466 if (enable) {
2467 rvu_write64(rvu, blkaddr, cir_off, cir_val);
2468 if (lvl != NIX_TXSCH_LVL_TL1)
2469 rvu_write64(rvu, blkaddr, pir_off, pir_val);
2470 } else {
2471 rvu_write64(rvu, blkaddr, cir_off, 0x0);
2472 if (lvl != NIX_TXSCH_LVL_TL1)
2473 rvu_write64(rvu, blkaddr, pir_off, 0x0);
2474 }
2475 }
2476 }
2477
2478 static int nix_smq_flush(struct rvu *rvu, int blkaddr,
2479 int smq, u16 pcifunc, int nixlf)
2480 {
2481 struct nix_smq_flush_ctx *smq_flush_ctx;
2482 int err, restore_tx_en = 0, i;
2483 int pf = rvu_get_pf(rvu->pdev, pcifunc);
2484 u8 cgx_id = 0, lmac_id = 0;
2485 u16 tl2_tl3_link_schq;
2486 u8 link, link_level;
2487 u64 cfg, bmap = 0;
2488
2489 if (!is_rvu_otx2(rvu)) {
2490 /* Skip SMQ flush if pkt count is zero */
2491 cfg = rvu_read64(rvu, blkaddr, NIX_AF_MDQX_IN_MD_COUNT(smq));
2492 if (!cfg)
2493 return 0;
2494 }
2495
2496 /* enable cgx tx if disabled */
2497 if (is_pf_cgxmapped(rvu, pf)) {
2498 rvu_get_cgx_lmac_id(rvu->pf2cgxlmac_map[pf], &cgx_id, &lmac_id);
2499 restore_tx_en = !rvu_cgx_config_tx(rvu_cgx_pdata(cgx_id, rvu),
2500 lmac_id, true);
2501 }
2502
2503 /* XOFF all TL2s whose parent TL1 matches SMQ tree TL1 */
2504 smq_flush_ctx = kzalloc_obj(*smq_flush_ctx);
2505 if (!smq_flush_ctx)
2506 return -ENOMEM;
2507 nix_smq_flush_fill_ctx(rvu, blkaddr, smq, smq_flush_ctx);
2508 nix_smq_flush_enadis_xoff(rvu, blkaddr, smq_flush_ctx, true);
2509 nix_smq_flush_enadis_rate(rvu, blkaddr, smq_flush_ctx, false);
2510
2511 /* Disable backpressure from physical link,
2512 * otherwise SMQ flush may stall.
2513 */
2514 rvu_cgx_enadis_rx_bp(rvu, pf, false);
2515
2516 link_level = rvu_read64(rvu, blkaddr, NIX_AF_PSE_CHANNEL_LEVEL) & 0x01 ?
2517 NIX_TXSCH_LVL_TL3 : NIX_TXSCH_LVL_TL2;
2518 tl2_tl3_link_schq = smq_flush_ctx->smq_tree_ctx[link_level].schq;
2519 link = smq_flush_ctx->smq_tree_ctx[NIX_TXSCH_LVL_TL1].schq;
2520
2521 /* SMQ set enqueue xoff */
2522 cfg = rvu_read64(rvu, blkaddr, NIX_AF_SMQX_CFG(smq));
2523 cfg |= BIT_ULL(50);
2524 rvu_write64(rvu, blkaddr, NIX_AF_SMQX_CFG(smq), cfg);
2525
2526 /* Clear all NIX_AF_TL3_TL2_LINK_CFG[ENA] for the TL3/TL2 queue */
2527 for (i = 0; i < (rvu->hw->cgx_links + rvu->hw->lbk_links); i++) {
2528 cfg = rvu_read64(rvu, blkaddr,
2529 NIX_AF_TL3_TL2X_LINKX_CFG(tl2_tl3_link_schq, link));
2530 if (!(cfg & BIT_ULL(12)))
2531 continue;
2532 bmap |= BIT_ULL(i);
2533 cfg &= ~BIT_ULL(12);
2534 rvu_write64(rvu, blkaddr,
2535 NIX_AF_TL3_TL2X_LINKX_CFG(tl2_tl3_link_schq, link), cfg);
2536 }
2537
2538 /* Do SMQ flush and set enqueue xoff */
2539 cfg = rvu_read64(rvu, blkaddr, NIX_AF_SMQX_CFG(smq));
2540 cfg |= BIT_ULL(50) | BIT_ULL(49);
2541 rvu_write64(rvu, blkaddr, NIX_AF_SMQX_CFG(smq), cfg);
2542
2543 /* Wait for flush to complete */
2544 err = rvu_poll_reg(rvu, blkaddr,
2545 NIX_AF_SMQX_CFG(smq), BIT_ULL(49), true);
2546 if (err)
2547 dev_info(rvu->dev,
2548 "NIXLF%d: SMQ%d flush failed, txlink might be busy\n",
2549 nixlf, smq);
2550
2551 /* Set NIX_AF_TL3_TL2_LINKX_CFG[ENA] for the TL3/TL2 queue */
2552 for (i = 0; i < (rvu->hw->cgx_links + rvu->hw->lbk_links); i++) {
2553 if (!(bmap & BIT_ULL(i)))
2554 continue;
2555 cfg = rvu_read64(rvu, blkaddr,
2556 NIX_AF_TL3_TL2X_LINKX_CFG(tl2_tl3_link_schq, link));
2557 cfg |= BIT_ULL(12);
2558 rvu_write64(rvu, blkaddr,
2559 NIX_AF_TL3_TL2X_LINKX_CFG(tl2_tl3_link_schq, link), cfg);
2560 }
2561
2562 /* clear XOFF on TL2s */
2563 nix_smq_flush_enadis_rate(rvu, blkaddr, smq_flush_ctx, true);
2564 nix_smq_flush_enadis_xoff(rvu, blkaddr, smq_flush_ctx, false);
2565 kfree(smq_flush_ctx);
2566
2567 rvu_cgx_enadis_rx_bp(rvu, pf, true);
2568 /* restore cgx tx state */
2569 if (restore_tx_en)
2570 rvu_cgx_config_tx(rvu_cgx_pdata(cgx_id, rvu), lmac_id, false);
2571 return err;
2572 }
2573
2574 static int nix_txschq_free(struct rvu *rvu, u16 pcifunc)
2575 {
2576 int blkaddr, nixlf, lvl, schq, err;
2577 struct rvu_hwinfo *hw = rvu->hw;
2578 struct nix_txsch *txsch;
2579 struct nix_hw *nix_hw;
2580 u16 map_func;
2581
2582 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NIX, pcifunc);
2583 if (blkaddr < 0)
2584 return NIX_AF_ERR_AF_LF_INVALID;
2585
2586 nix_hw = get_nix_hw(rvu->hw, blkaddr);
2587 if (!nix_hw)
2588 return NIX_AF_ERR_INVALID_NIXBLK;
2589
2590 nixlf = rvu_get_lf(rvu, &hw->block[blkaddr], pcifunc, 0);
2591 if (nixlf < 0)
2592 return NIX_AF_ERR_AF_LF_INVALID;
2593
2594 /* Disable TL2/3 queue links and all XOFF's before SMQ flush*/
2595 mutex_lock(&rvu->rsrc_lock);
2596 for (lvl = NIX_TXSCH_LVL_MDQ; lvl < NIX_TXSCH_LVL_CNT; lvl++) {
2597 txsch = &nix_hw->txsch[lvl];
2598
2599 if (lvl >= hw->cap.nix_tx_aggr_lvl)
2600 continue;
2601
2602 for (schq = 0; schq < txsch->schq.max; schq++) {
2603 if (TXSCH_MAP_FUNC(txsch->pfvf_map[schq]) != pcifunc)
2604 continue;
2605 nix_reset_tx_linkcfg(rvu, blkaddr, lvl, schq);
2606 nix_clear_tx_xoff(rvu, blkaddr, lvl, schq);
2607 nix_reset_tx_shaping(rvu, blkaddr, nixlf, lvl, schq);
2608 }
2609 }
2610 nix_clear_tx_xoff(rvu, blkaddr, NIX_TXSCH_LVL_TL1,
2611 nix_get_tx_link(rvu, pcifunc));
2612
2613 /* On PF cleanup, clear cfg done flag as
2614 * PF would have changed default config.
2615 */
2616 if (!(pcifunc & RVU_PFVF_FUNC_MASK)) {
2617 txsch = &nix_hw->txsch[NIX_TXSCH_LVL_TL1];
2618 schq = nix_get_tx_link(rvu, pcifunc);
2619 /* Do not clear pcifunc in txsch->pfvf_map[schq] because
2620 * VF might be using this TL1 queue
2621 */
2622 map_func = TXSCH_MAP_FUNC(txsch->pfvf_map[schq]);
2623 txsch->pfvf_map[schq] = TXSCH_SET_FLAG(map_func, 0x0);
2624 }
2625
2626 /* Flush SMQs */
2627 txsch = &nix_hw->txsch[NIX_TXSCH_LVL_SMQ];
2628 for (schq = 0; schq < txsch->schq.max; schq++) {
2629 if (TXSCH_MAP_FUNC(txsch->pfvf_map[schq]) != pcifunc)
2630 continue;
2631 nix_smq_flush(rvu, blkaddr, schq, pcifunc, nixlf);
2632 }
2633
2634 /* Now free scheduler queues to free pool */
2635 for (lvl = 0; lvl < NIX_TXSCH_LVL_CNT; lvl++) {
2636 /* TLs above aggregation level are shared across all PF
2637 * and it's VFs, hence skip freeing them.
2638 */
2639 if (lvl >= hw->cap.nix_tx_aggr_lvl)
2640 continue;
2641
2642 txsch = &nix_hw->txsch[lvl];
2643 for (schq = 0; schq < txsch->schq.max; schq++) {
2644 if (TXSCH_MAP_FUNC(txsch->pfvf_map[schq]) != pcifunc)
2645 continue;
2646 nix_reset_tx_schedule(rvu, blkaddr, lvl, schq);
2647 rvu_free_rsrc(&txsch->schq, schq);
2648 txsch->pfvf_map[schq] = TXSCH_MAP(0, NIX_TXSCHQ_FREE);
2649 }
2650 }
2651 mutex_unlock(&rvu->rsrc_lock);
2652
2653 err = rvu_ndc_sync(rvu, blkaddr, nixlf, NIX_AF_NDC_TX_SYNC);
2654 if (err)
2655 dev_err(rvu->dev, "NDC-TX sync failed for NIXLF %d\n", nixlf);
2656
2657 return 0;
2658 }
2659
2660 static int nix_txschq_free_one(struct rvu *rvu,
2661 struct nix_txsch_free_req *req)
2662 {
2663 struct rvu_hwinfo *hw = rvu->hw;
2664 u16 pcifunc = req->hdr.pcifunc;
2665 int lvl, schq, nixlf, blkaddr;
2666 struct nix_txsch *txsch;
2667 struct nix_hw *nix_hw;
2668 u32 *pfvf_map;
2669 int rc;
2670
2671 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NIX, pcifunc);
2672 if (blkaddr < 0)
2673 return NIX_AF_ERR_AF_LF_INVALID;
2674
2675 nix_hw = get_nix_hw(rvu->hw, blkaddr);
2676 if (!nix_hw)
2677 return NIX_AF_ERR_INVALID_NIXBLK;
2678
2679 nixlf = rvu_get_lf(rvu, &hw->block[blkaddr], pcifunc, 0);
2680 if (nixlf < 0)
2681 return NIX_AF_ERR_AF_LF_INVALID;
2682
2683 lvl = req->schq_lvl;
2684 schq = req->schq;
2685 txsch = &nix_hw->txsch[lvl];
2686
2687 if (lvl >= hw->cap.nix_tx_aggr_lvl || schq >= txsch->schq.max)
2688 return 0;
2689
2690 pfvf_map = txsch->pfvf_map;
2691 mutex_lock(&rvu->rsrc_lock);
2692
2693 if (TXSCH_MAP_FUNC(pfvf_map[schq]) != pcifunc) {
2694 rc = NIX_AF_ERR_TLX_INVALID;
2695 goto err;
2696 }
2697
2698 /* Clear SW_XOFF of this resource only.
2699 * For SMQ level, all path XOFF's
2700 * need to be made clear by user
2701 */
2702 nix_clear_tx_xoff(rvu, blkaddr, lvl, schq);
2703
2704 nix_reset_tx_linkcfg(rvu, blkaddr, lvl, schq);
2705 nix_reset_tx_shaping(rvu, blkaddr, nixlf, lvl, schq);
2706
2707 /* Flush if it is a SMQ. Onus of disabling
2708 * TL2/3 queue links before SMQ flush is on user
2709 */
2710 if (lvl == NIX_TXSCH_LVL_SMQ &&
2711 nix_smq_flush(rvu, blkaddr, schq, pcifunc, nixlf)) {
2712 rc = NIX_AF_SMQ_FLUSH_FAILED;
2713 goto err;
2714 }
2715
2716 nix_reset_tx_schedule(rvu, blkaddr, lvl, schq);
2717
2718 /* Free the resource */
2719 rvu_free_rsrc(&txsch->schq, schq);
2720 txsch->pfvf_map[schq] = TXSCH_MAP(0, NIX_TXSCHQ_FREE);
2721 mutex_unlock(&rvu->rsrc_lock);
2722 return 0;
2723 err:
2724 mutex_unlock(&rvu->rsrc_lock);
2725 return rc;
2726 }
2727
2728 int rvu_mbox_handler_nix_txsch_free(struct rvu *rvu,
2729 struct nix_txsch_free_req *req,
2730 struct msg_rsp *rsp)
2731 {
2732 if (req->flags & TXSCHQ_FREE_ALL)
2733 return nix_txschq_free(rvu, req->hdr.pcifunc);
2734 else
2735 return nix_txschq_free_one(rvu, req);
2736 }
2737
2738 static bool is_txschq_hierarchy_valid(struct rvu *rvu, u16 pcifunc, int blkaddr,
2739 int lvl, u64 reg, u64 regval)
2740 {
2741 u64 regbase = reg & 0xFFFF;
2742 u16 schq, parent;
2743
2744 if (!rvu_check_valid_reg(TXSCHQ_HWREGMAP, lvl, reg))
2745 return false;
2746
2747 schq = TXSCHQ_IDX(reg, TXSCHQ_IDX_SHIFT);
2748 /* Check if this schq belongs to this PF/VF or not */
2749 if (!is_valid_txschq(rvu, blkaddr, lvl, pcifunc, schq))
2750 return false;
2751
2752 parent = (regval >> 16) & 0x1FF;
2753 /* Validate MDQ's TL4 parent */
2754 if (regbase == NIX_AF_MDQX_PARENT(0) &&
2755 !is_valid_txschq(rvu, blkaddr, NIX_TXSCH_LVL_TL4, pcifunc, parent))
2756 return false;
2757
2758 /* Validate TL4's TL3 parent */
2759 if (regbase == NIX_AF_TL4X_PARENT(0) &&
2760 !is_valid_txschq(rvu, blkaddr, NIX_TXSCH_LVL_TL3, pcifunc, parent))
2761 return false;
2762
2763 /* Validate TL3's TL2 parent */
2764 if (regbase == NIX_AF_TL3X_PARENT(0) &&
2765 !is_valid_txschq(rvu, blkaddr, NIX_TXSCH_LVL_TL2, pcifunc, parent))
2766 return false;
2767
2768 /* Validate TL2's TL1 parent */
2769 if (regbase == NIX_AF_TL2X_PARENT(0) &&
2770 !is_valid_txschq(rvu, blkaddr, NIX_TXSCH_LVL_TL1, pcifunc, parent))
2771 return false;
2772
2773 return true;
2774 }
2775
2776 static bool is_txschq_shaping_valid(struct rvu_hwinfo *hw, int lvl, u64 reg)
2777 {
2778 u64 regbase;
2779
2780 if (hw->cap.nix_shaping)
2781 return true;
2782
2783 /* If shaping and coloring is not supported, then
2784 * *_CIR and *_PIR registers should not be configured.
2785 */
2786 regbase = reg & 0xFFFF;
2787
2788 switch (lvl) {
2789 case NIX_TXSCH_LVL_TL1:
2790 if (regbase == NIX_AF_TL1X_CIR(0))
2791 return false;
2792 break;
2793 case NIX_TXSCH_LVL_TL2:
2794 if (regbase == NIX_AF_TL2X_CIR(0) ||
2795 regbase == NIX_AF_TL2X_PIR(0))
2796 return false;
2797 break;
2798 case NIX_TXSCH_LVL_TL3:
2799 if (regbase == NIX_AF_TL3X_CIR(0) ||
2800 regbase == NIX_AF_TL3X_PIR(0))
2801 return false;
2802 break;
2803 case NIX_TXSCH_LVL_TL4:
2804 if (regbase == NIX_AF_TL4X_CIR(0) ||
2805 regbase == NIX_AF_TL4X_PIR(0))
2806 return false;
2807 break;
2808 case NIX_TXSCH_LVL_MDQ:
2809 if (regbase == NIX_AF_MDQX_CIR(0) ||
2810 regbase == NIX_AF_MDQX_PIR(0))
2811 return false;
2812 break;
2813 }
2814 return true;
2815 }
2816
2817 static void nix_tl1_default_cfg(struct rvu *rvu, struct nix_hw *nix_hw,
2818 u16 pcifunc, int blkaddr)
2819 {
2820 u32 *pfvf_map;
2821 int schq;
2822
2823 schq = nix_get_tx_link(rvu, pcifunc);
2824 pfvf_map = nix_hw->txsch[NIX_TXSCH_LVL_TL1].pfvf_map;
2825 /* Skip if PF has already done the config */
2826 if (TXSCH_MAP_FLAGS(pfvf_map[schq]) & NIX_TXSCHQ_CFG_DONE)
2827 return;
2828 rvu_write64(rvu, blkaddr, NIX_AF_TL1X_TOPOLOGY(schq),
2829 (TXSCH_TL1_DFLT_RR_PRIO << 1));
2830
2831 /* On OcteonTx2 the config was in bytes and newer silcons
2832 * it's changed to weight.
2833 */
2834 if (!rvu->hw->cap.nix_common_dwrr_mtu)
2835 rvu_write64(rvu, blkaddr, NIX_AF_TL1X_SCHEDULE(schq),
2836 TXSCH_TL1_DFLT_RR_QTM);
2837 else
2838 rvu_write64(rvu, blkaddr, NIX_AF_TL1X_SCHEDULE(schq),
2839 CN10K_MAX_DWRR_WEIGHT);
2840
2841 rvu_write64(rvu, blkaddr, NIX_AF_TL1X_CIR(schq), 0x00);
2842 pfvf_map[schq] = TXSCH_SET_FLAG(pfvf_map[schq], NIX_TXSCHQ_CFG_DONE);
2843 }
2844
2845 /* Register offset - [15:0]
2846 * Scheduler Queue number - [25:16]
2847 */
2848 #define NIX_TX_SCHQ_MASK GENMASK_ULL(25, 0)
2849
2850 static int nix_txschq_cfg_read(struct rvu *rvu, struct nix_hw *nix_hw,
2851 int blkaddr, struct nix_txschq_config *req,
2852 struct nix_txschq_config *rsp)
2853 {
2854 u16 pcifunc = req->hdr.pcifunc;
2855 int idx, schq;
2856 u64 reg;
2857
2858 for (idx = 0; idx < req->num_regs; idx++) {
2859 reg = req->reg[idx];
2860 reg &= NIX_TX_SCHQ_MASK;
2861 schq = TXSCHQ_IDX(reg, TXSCHQ_IDX_SHIFT);
2862 if (!rvu_check_valid_reg(TXSCHQ_HWREGMAP, req->lvl, reg) ||
2863 !is_valid_txschq(rvu, blkaddr, req->lvl, pcifunc, schq))
2864 return NIX_AF_INVAL_TXSCHQ_CFG;
2865 rsp->regval[idx] = rvu_read64(rvu, blkaddr, reg);
2866 }
2867 rsp->lvl = req->lvl;
2868 rsp->num_regs = req->num_regs;
2869 return 0;
2870 }
2871
2872 void rvu_nix_tx_tl2_cfg(struct rvu *rvu, int blkaddr, u16 pcifunc,
2873 struct nix_txsch *txsch, bool enable)
2874 {
2875 struct rvu_hwinfo *hw = rvu->hw;
2876 int lbk_link_start, lbk_links;
2877 u8 pf = rvu_get_pf(rvu->pdev, pcifunc);
2878 int schq;
2879 u64 cfg;
2880
2881 if (!is_pf_cgxmapped(rvu, pf) && !is_rep_dev(rvu, pcifunc))
2882 return;
2883
2884 cfg = enable ? (BIT_ULL(12) | RVU_SWITCH_LBK_CHAN) : 0;
2885 lbk_link_start = hw->cgx_links;
2886
2887 for (schq = 0; schq < txsch->schq.max; schq++) {
2888 if (TXSCH_MAP_FUNC(txsch->pfvf_map[schq]) != pcifunc)
2889 continue;
2890 /* Enable all LBK links with channel 63 by default so that
2891 * packets can be sent to LBK with a NPC TX MCAM rule
2892 */
2893 lbk_links = hw->lbk_links;
2894 while (lbk_links--)
2895 rvu_write64(rvu, blkaddr,
2896 NIX_AF_TL3_TL2X_LINKX_CFG(schq,
2897 lbk_link_start +
2898 lbk_links), cfg);
2899 }
2900 }
2901
2902 int rvu_mbox_handler_nix_txschq_cfg(struct rvu *rvu,
2903 struct nix_txschq_config *req,
2904 struct nix_txschq_config *rsp)
2905 {
2906 u64 reg, val, regval, schq_regbase, val_mask;
2907 struct rvu_hwinfo *hw = rvu->hw;
2908 u16 pcifunc = req->hdr.pcifunc;
2909 struct nix_txsch *txsch;
2910 struct nix_hw *nix_hw;
2911 int blkaddr, idx, err;
2912 int nixlf, schq;
2913 u32 *pfvf_map;
2914
2915 if (req->lvl >= NIX_TXSCH_LVL_CNT ||
2916 req->num_regs > MAX_REGS_PER_MBOX_MSG)
2917 return NIX_AF_INVAL_TXSCHQ_CFG;
2918
2919 err = nix_get_nixlf(rvu, pcifunc, &nixlf, &blkaddr);
2920 if (err)
2921 return err;
2922
2923 nix_hw = get_nix_hw(rvu->hw, blkaddr);
2924 if (!nix_hw)
2925 return NIX_AF_ERR_INVALID_NIXBLK;
2926
2927 if (req->read)
2928 return nix_txschq_cfg_read(rvu, nix_hw, blkaddr, req, rsp);
2929
2930 txsch = &nix_hw->txsch[req->lvl];
2931 pfvf_map = txsch->pfvf_map;
2932
2933 if (req->lvl >= hw->cap.nix_tx_aggr_lvl &&
2934 pcifunc & RVU_PFVF_FUNC_MASK) {
2935 mutex_lock(&rvu->rsrc_lock);
2936 if (req->lvl == NIX_TXSCH_LVL_TL1)
2937 nix_tl1_default_cfg(rvu, nix_hw, pcifunc, blkaddr);
2938 mutex_unlock(&rvu->rsrc_lock);
2939 return 0;
2940 }
2941
2942 for (idx = 0; idx < req->num_regs; idx++) {
2943 reg = req->reg[idx];
2944 reg &= NIX_TX_SCHQ_MASK;
2945 regval = req->regval[idx];
2946 schq_regbase = reg & 0xFFFF;
2947 val_mask = req->regval_mask[idx];
2948
2949 if (!is_txschq_hierarchy_valid(rvu, pcifunc, blkaddr,
2950 txsch->lvl, reg, regval))
2951 return NIX_AF_INVAL_TXSCHQ_CFG;
2952
2953 /* Check if shaping and coloring is supported */
2954 if (!is_txschq_shaping_valid(hw, req->lvl, reg))
2955 continue;
2956
2957 val = rvu_read64(rvu, blkaddr, reg);
2958 regval = (val & val_mask) | (regval & ~val_mask);
2959
2960 /* Handle shaping state toggle specially */
2961 if (hw->cap.nix_shaper_toggle_wait &&
2962 handle_txschq_shaper_update(rvu, blkaddr, nixlf,
2963 req->lvl, reg, regval))
2964 continue;
2965
2966 /* Replace PF/VF visible NIXLF slot with HW NIXLF id */
2967 if (schq_regbase == NIX_AF_SMQX_CFG(0)) {
2968 nixlf = rvu_get_lf(rvu, &hw->block[blkaddr],
2969 pcifunc, 0);
2970 regval &= ~(0x7FULL << 24);
2971 regval |= ((u64)nixlf << 24);
2972 }
2973
2974 /* Clear 'BP_ENA' config, if it's not allowed */
2975 if (!hw->cap.nix_tx_link_bp) {
2976 if (schq_regbase == NIX_AF_TL4X_SDP_LINK_CFG(0) ||
2977 (schq_regbase & 0xFF00) ==
2978 NIX_AF_TL3_TL2X_LINKX_CFG(0, 0))
2979 regval &= ~BIT_ULL(13);
2980 }
2981
2982 /* Mark config as done for TL1 by PF */
2983 if (schq_regbase >= NIX_AF_TL1X_SCHEDULE(0) &&
2984 schq_regbase <= NIX_AF_TL1X_GREEN_BYTES(0)) {
2985 schq = TXSCHQ_IDX(reg, TXSCHQ_IDX_SHIFT);
2986 mutex_lock(&rvu->rsrc_lock);
2987 pfvf_map[schq] = TXSCH_SET_FLAG(pfvf_map[schq],
2988 NIX_TXSCHQ_CFG_DONE);
2989 mutex_unlock(&rvu->rsrc_lock);
2990 }
2991
2992 /* SMQ flush is special hence split register writes such
2993 * that flush first and write rest of the bits later.
2994 */
2995 if (schq_regbase == NIX_AF_SMQX_CFG(0) &&
2996 (regval & BIT_ULL(49))) {
2997 schq = TXSCHQ_IDX(reg, TXSCHQ_IDX_SHIFT);
2998 nix_smq_flush(rvu, blkaddr, schq, pcifunc, nixlf);
2999 regval &= ~BIT_ULL(49);
3000 }
3001 rvu_write64(rvu, blkaddr, reg, regval);
3002 }
3003
3004 return 0;
3005 }
3006
3007 static int nix_rx_vtag_cfg(struct rvu *rvu, int nixlf, int blkaddr,
3008 struct nix_vtag_config *req)
3009 {
3010 u64 regval = req->vtag_size;
3011
3012 if (req->rx.vtag_type > NIX_AF_LFX_RX_VTAG_TYPE7 ||
3013 req->vtag_size > VTAGSIZE_T8)
3014 return -EINVAL;
3015
3016 /* RX VTAG Type 7 reserved for vf vlan */
3017 if (req->rx.vtag_type == NIX_AF_LFX_RX_VTAG_TYPE7)
3018 return NIX_AF_ERR_RX_VTAG_INUSE;
3019
3020 if (req->rx.capture_vtag)
3021 regval |= BIT_ULL(5);
3022 if (req->rx.strip_vtag)
3023 regval |= BIT_ULL(4);
3024
3025 rvu_write64(rvu, blkaddr,
3026 NIX_AF_LFX_RX_VTAG_TYPEX(nixlf, req->rx.vtag_type), regval);
3027 return 0;
3028 }
3029
3030 static int nix_tx_vtag_free(struct rvu *rvu, int blkaddr,
3031 u16 pcifunc, int index)
3032 {
3033 struct nix_hw *nix_hw = get_nix_hw(rvu->hw, blkaddr);
3034 struct nix_txvlan *vlan;
3035
3036 if (!nix_hw)
3037 return NIX_AF_ERR_INVALID_NIXBLK;
3038
3039 vlan = &nix_hw->txvlan;
3040 if (vlan->entry2pfvf_map[index] != pcifunc)
3041 return NIX_AF_ERR_PARAM;
3042
3043 rvu_write64(rvu, blkaddr,
3044 NIX_AF_TX_VTAG_DEFX_DATA(index), 0x0ull);
3045 rvu_write64(rvu, blkaddr,
3046 NIX_AF_TX_VTAG_DEFX_CTL(index), 0x0ull);
3047
3048 vlan->entry2pfvf_map[index] = 0;
3049 rvu_free_rsrc(&vlan->rsrc, index);
3050
3051 return 0;
3052 }
3053
3054 static void nix_free_tx_vtag_entries(struct rvu *rvu, u16 pcifunc)
3055 {
3056 struct nix_txvlan *vlan;
3057 struct nix_hw *nix_hw;
3058 int index, blkaddr;
3059
3060 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NIX, pcifunc);
3061 if (blkaddr < 0)
3062 return;
3063
3064 nix_hw = get_nix_hw(rvu->hw, blkaddr);
3065 if (!nix_hw)
3066 return;
3067
3068 vlan = &nix_hw->txvlan;
3069
3070 mutex_lock(&vlan->rsrc_lock);
3071 /* Scan all the entries and free the ones mapped to 'pcifunc' */
3072 for (index = 0; index < vlan->rsrc.max; index++) {
3073 if (vlan->entry2pfvf_map[index] == pcifunc)
3074 nix_tx_vtag_free(rvu, blkaddr, pcifunc, index);
3075 }
3076 mutex_unlock(&vlan->rsrc_lock);
3077 }
3078
3079 static int nix_tx_vtag_alloc(struct rvu *rvu, int blkaddr,
3080 u64 vtag, u8 size)
3081 {
3082 struct nix_hw *nix_hw = get_nix_hw(rvu->hw, blkaddr);
3083 struct nix_txvlan *vlan;
3084 u64 regval;
3085 int index;
3086
3087 if (!nix_hw)
3088 return NIX_AF_ERR_INVALID_NIXBLK;
3089
3090 vlan = &nix_hw->txvlan;
3091
3092 mutex_lock(&vlan->rsrc_lock);
3093
3094 index = rvu_alloc_rsrc(&vlan->rsrc);
3095 if (index < 0) {
3096 mutex_unlock(&vlan->rsrc_lock);
3097 return index;
3098 }
3099
3100 mutex_unlock(&vlan->rsrc_lock);
3101
3102 regval = size ? vtag : vtag << 32;
3103
3104 rvu_write64(rvu, blkaddr,
3105 NIX_AF_TX_VTAG_DEFX_DATA(index), regval);
3106 rvu_write64(rvu, blkaddr,
3107 NIX_AF_TX_VTAG_DEFX_CTL(index), size);
3108
3109 return index;
3110 }
3111
3112 static int nix_tx_vtag_decfg(struct rvu *rvu, int blkaddr,
3113 struct nix_vtag_config *req)
3114 {
3115 struct nix_hw *nix_hw = get_nix_hw(rvu->hw, blkaddr);
3116 u16 pcifunc = req->hdr.pcifunc;
3117 int idx0 = req->tx.vtag0_idx;
3118 int idx1 = req->tx.vtag1_idx;
3119 struct nix_txvlan *vlan;
3120 int err = 0;
3121
3122 if (!nix_hw)
3123 return NIX_AF_ERR_INVALID_NIXBLK;
3124
3125 vlan = &nix_hw->txvlan;
3126 if (req->tx.free_vtag0 && req->tx.free_vtag1)
3127 if (vlan->entry2pfvf_map[idx0] != pcifunc ||
3128 vlan->entry2pfvf_map[idx1] != pcifunc)
3129 return NIX_AF_ERR_PARAM;
3130
3131 mutex_lock(&vlan->rsrc_lock);
3132
3133 if (req->tx.free_vtag0) {
3134 err = nix_tx_vtag_free(rvu, blkaddr, pcifunc, idx0);
3135 if (err)
3136 goto exit;
3137 }
3138
3139 if (req->tx.free_vtag1)
3140 err = nix_tx_vtag_free(rvu, blkaddr, pcifunc, idx1);
3141
3142 exit:
3143 mutex_unlock(&vlan->rsrc_lock);
3144 return err;
3145 }
3146
3147 static int nix_tx_vtag_cfg(struct rvu *rvu, int blkaddr,
3148 struct nix_vtag_config *req,
3149 struct nix_vtag_config_rsp *rsp)
3150 {
3151 struct nix_hw *nix_hw = get_nix_hw(rvu->hw, blkaddr);
3152 struct nix_txvlan *vlan;
3153 u16 pcifunc = req->hdr.pcifunc;
3154
3155 if (!nix_hw)
3156 return NIX_AF_ERR_INVALID_NIXBLK;
3157
3158 vlan = &nix_hw->txvlan;
3159 if (req->tx.cfg_vtag0) {
3160 rsp->vtag0_idx =
3161 nix_tx_vtag_alloc(rvu, blkaddr,
3162 req->tx.vtag0, req->vtag_size);
3163
3164 if (rsp->vtag0_idx < 0)
3165 return NIX_AF_ERR_TX_VTAG_NOSPC;
3166
3167 vlan->entry2pfvf_map[rsp->vtag0_idx] = pcifunc;
3168 }
3169
3170 if (req->tx.cfg_vtag1) {
3171 rsp->vtag1_idx =
3172 nix_tx_vtag_alloc(rvu, blkaddr,
3173 req->tx.vtag1, req->vtag_size);
3174
3175 if (rsp->vtag1_idx < 0)
3176 goto err_free;
3177
3178 vlan->entry2pfvf_map[rsp->vtag1_idx] = pcifunc;
3179 }
3180
3181 return 0;
3182
3183 err_free:
3184 if (req->tx.cfg_vtag0)
3185 nix_tx_vtag_free(rvu, blkaddr, pcifunc, rsp->vtag0_idx);
3186
3187 return NIX_AF_ERR_TX_VTAG_NOSPC;
3188 }
3189
3190 int rvu_mbox_handler_nix_vtag_cfg(struct rvu *rvu,
3191 struct nix_vtag_config *req,
3192 struct nix_vtag_config_rsp *rsp)
3193 {
3194 u16 pcifunc = req->hdr.pcifunc;
3195 int blkaddr, nixlf, err;
3196
3197 err = nix_get_nixlf(rvu, pcifunc, &nixlf, &blkaddr);
3198 if (err)
3199 return err;
3200
3201 if (req->cfg_type) {
3202 /* rx vtag configuration */
3203 err = nix_rx_vtag_cfg(rvu, nixlf, blkaddr, req);
3204 if (err)
3205 return NIX_AF_ERR_PARAM;
3206 } else {
3207 /* tx vtag configuration */
3208 if ((req->tx.cfg_vtag0 || req->tx.cfg_vtag1) &&
3209 (req->tx.free_vtag0 || req->tx.free_vtag1))
3210 return NIX_AF_ERR_PARAM;
3211
3212 if (req->tx.cfg_vtag0 || req->tx.cfg_vtag1)
3213 return nix_tx_vtag_cfg(rvu, blkaddr, req, rsp);
3214
3215 if (req->tx.free_vtag0 || req->tx.free_vtag1)
3216 return nix_tx_vtag_decfg(rvu, blkaddr, req);
3217 }
3218
3219 return 0;
3220 }
3221
3222 static int nix_blk_setup_mce(struct rvu *rvu, struct nix_hw *nix_hw,
3223 int mce, u8 op, u16 pcifunc, int next,
3224 int index, u8 mce_op, bool eol)
3225 {
3226 struct nix_aq_enq_req aq_req;
3227 int err;
3228
3229 aq_req.hdr.pcifunc = 0;
3230 aq_req.ctype = NIX_AQ_CTYPE_MCE;
3231 aq_req.op = op;
3232 aq_req.qidx = mce;
3233
3234 /* Use RSS with RSS index 0 */
3235 aq_req.mce.op = mce_op;
3236 aq_req.mce.index = index;
3237 aq_req.mce.eol = eol;
3238 aq_req.mce.pf_func = pcifunc;
3239 aq_req.mce.next = next;
3240
3241 /* All fields valid */
3242 *(u64 *)(&aq_req.mce_mask) = ~0ULL;
3243
3244 err = rvu_nix_blk_aq_enq_inst(rvu, nix_hw, &aq_req, NULL);
3245 if (err) {
3246 dev_err(rvu->dev, "Failed to setup Bcast MCE for PF%d:VF%d\n",
3247 rvu_get_pf(rvu->pdev, pcifunc),
3248 pcifunc & RVU_PFVF_FUNC_MASK);
3249 return err;
3250 }
3251 return 0;
3252 }
3253
3254 static void nix_delete_mcast_mce_list(struct nix_mce_list *mce_list)
3255 {
3256 struct hlist_node *tmp;
3257 struct mce *mce;
3258
3259 /* Scan through the current list */
3260 hlist_for_each_entry_safe(mce, tmp, &mce_list->head, node) {
3261 hlist_del(&mce->node);
3262 kfree(mce);
3263 }
3264
3265 mce_list->count = 0;
3266 mce_list->max = 0;
3267 }
3268
3269 static int nix_get_last_mce_list_index(struct nix_mcast_grp_elem *elem)
3270 {
3271 return elem->mce_start_index + elem->mcast_mce_list.count - 1;
3272 }
3273
3274 static int nix_update_ingress_mce_list_hw(struct rvu *rvu,
3275 struct nix_hw *nix_hw,
3276 struct nix_mcast_grp_elem *elem)
3277 {
3278 int idx, last_idx, next_idx, err;
3279 struct nix_mce_list *mce_list;
3280 struct mce *mce, *prev_mce;
3281
3282 mce_list = &elem->mcast_mce_list;
3283 idx = elem->mce_start_index;
3284 last_idx = nix_get_last_mce_list_index(elem);
3285 hlist_for_each_entry(mce, &mce_list->head, node) {
3286 if (idx > last_idx)
3287 break;
3288
3289 if (!mce->is_active) {
3290 if (idx == elem->mce_start_index) {
3291 idx++;
3292 prev_mce = mce;
3293 elem->mce_start_index = idx;
3294 continue;
3295 } else if (idx == last_idx) {
3296 err = nix_blk_setup_mce(rvu, nix_hw, idx - 1, NIX_AQ_INSTOP_WRITE,
3297 prev_mce->pcifunc, next_idx,
3298 prev_mce->rq_rss_index,
3299 prev_mce->dest_type,
3300 false);
3301 if (err)
3302 return err;
3303
3304 break;
3305 }
3306 }
3307
3308 next_idx = idx + 1;
3309 /* EOL should be set in last MCE */
3310 err = nix_blk_setup_mce(rvu, nix_hw, idx, NIX_AQ_INSTOP_WRITE,
3311 mce->pcifunc, next_idx,
3312 mce->rq_rss_index, mce->dest_type,
3313 (next_idx > last_idx) ? true : false);
3314 if (err)
3315 return err;
3316
3317 idx++;
3318 prev_mce = mce;
3319 }
3320
3321 return 0;
3322 }
3323
3324 static void nix_update_egress_mce_list_hw(struct rvu *rvu,
3325 struct nix_hw *nix_hw,
3326 struct nix_mcast_grp_elem *elem)
3327 {
3328 struct nix_mce_list *mce_list;
3329 int idx, last_idx, next_idx;
3330 struct mce *mce, *prev_mce;
3331 u64 regval;
3332 u8 eol;
3333
3334 mce_list = &elem->mcast_mce_list;
3335 idx = elem->mce_start_index;
3336 last_idx = nix_get_last_mce_list_index(elem);
3337 hlist_for_each_entry(mce, &mce_list->head, node) {
3338 if (idx > last_idx)
3339 break;
3340
3341 if (!mce->is_active) {
3342 if (idx == elem->mce_start_index) {
3343 idx++;
3344 prev_mce = mce;
3345 elem->mce_start_index = idx;
3346 continue;
3347 } else if (idx == last_idx) {
3348 regval = (next_idx << 16) | (1 << 12) | prev_mce->channel;
3349 rvu_write64(rvu, nix_hw->blkaddr,
3350 NIX_AF_TX_MCASTX(idx - 1),
3351 regval);
3352 break;
3353 }
3354 }
3355
3356 eol = 0;
3357 next_idx = idx + 1;
3358 /* EOL should be set in last MCE */
3359 if (next_idx > last_idx)
3360 eol = 1;
3361
3362 regval = (next_idx << 16) | (eol << 12) | mce->channel;
3363 rvu_write64(rvu, nix_hw->blkaddr,
3364 NIX_AF_TX_MCASTX(idx),
3365 regval);
3366 idx++;
3367 prev_mce = mce;
3368 }
3369 }
3370
3371 static int nix_del_mce_list_entry(struct rvu *rvu,
3372 struct nix_hw *nix_hw,
3373 struct nix_mcast_grp_elem *elem,
3374 struct nix_mcast_grp_update_req *req)
3375 {
3376 u32 num_entry = req->num_mce_entry;
3377 struct nix_mce_list *mce_list;
3378 struct mce *mce;
3379 bool is_found;
3380 int i;
3381
3382 mce_list = &elem->mcast_mce_list;
3383 for (i = 0; i < num_entry; i++) {
3384 is_found = false;
3385 hlist_for_each_entry(mce, &mce_list->head, node) {
3386 /* If already exists, then delete */
3387 if (mce->pcifunc == req->pcifunc[i]) {
3388 hlist_del(&mce->node);
3389 kfree(mce);
3390 mce_list->count--;
3391 is_found = true;
3392 break;
3393 }
3394 }
3395
3396 if (!is_found)
3397 return NIX_AF_ERR_INVALID_MCAST_DEL_REQ;
3398 }
3399
3400 mce_list->max = mce_list->count;
3401 /* Dump the updated list to HW */
3402 if (elem->dir == NIX_MCAST_INGRESS)
3403 return nix_update_ingress_mce_list_hw(rvu, nix_hw, elem);
3404
3405 nix_update_egress_mce_list_hw(rvu, nix_hw, elem);
3406 return 0;
3407 }
3408
3409 static int nix_add_mce_list_entry(struct rvu *rvu,
3410 struct nix_hw *nix_hw,
3411 struct nix_mcast_grp_elem *elem,
3412 struct nix_mcast_grp_update_req *req)
3413 {
3414 u32 num_entry = req->num_mce_entry;
3415 struct nix_mce_list *mce_list;
3416 struct hlist_node *tmp;
3417 struct mce *mce;
3418 int i;
3419
3420 mce_list = &elem->mcast_mce_list;
3421 for (i = 0; i < num_entry; i++) {
3422 mce = kzalloc_obj(*mce);
3423 if (!mce)
3424 goto free_mce;
3425
3426 mce->pcifunc = req->pcifunc[i];
3427 mce->channel = req->channel[i];
3428 mce->rq_rss_index = req->rq_rss_index[i];
3429 mce->dest_type = req->dest_type[i];
3430 mce->is_active = 1;
3431 hlist_add_head(&mce->node, &mce_list->head);
3432 mce_list->count++;
3433 }
3434
3435 mce_list->max += num_entry;
3436
3437 /* Dump the updated list to HW */
3438 if (elem->dir == NIX_MCAST_INGRESS)
3439 return nix_update_ingress_mce_list_hw(rvu, nix_hw, elem);
3440
3441 nix_update_egress_mce_list_hw(rvu, nix_hw, elem);
3442 return 0;
3443
3444 free_mce:
3445 hlist_for_each_entry_safe(mce, tmp, &mce_list->head, node) {
3446 hlist_del(&mce->node);
3447 kfree(mce);
3448 mce_list->count--;
3449 }
3450
3451 return -ENOMEM;
3452 }
3453
3454 static int nix_update_mce_list_entry(struct nix_mce_list *mce_list,
3455 u16 pcifunc, bool add)
3456 {
3457 struct mce *mce, *tail = NULL;
3458 bool delete = false;
3459
3460 /* Scan through the current list */
3461 hlist_for_each_entry(mce, &mce_list->head, node) {
3462 /* If already exists, then delete */
3463 if (mce->pcifunc == pcifunc && !add) {
3464 delete = true;
3465 break;
3466 } else if (mce->pcifunc == pcifunc && add) {
3467 /* entry already exists */
3468 return 0;
3469 }
3470 tail = mce;
3471 }
3472
3473 if (delete) {
3474 hlist_del(&mce->node);
3475 kfree(mce);
3476 mce_list->count--;
3477 return 0;
3478 }
3479
3480 if (!add)
3481 return 0;
3482
3483 /* Add a new one to the list, at the tail */
3484 mce = kzalloc_obj(*mce);
3485 if (!mce)
3486 return -ENOMEM;
3487 mce->pcifunc = pcifunc;
3488 if (!tail)
3489 hlist_add_head(&mce->node, &mce_list->head);
3490 else
3491 hlist_add_behind(&mce->node, &tail->node);
3492 mce_list->count++;
3493 return 0;
3494 }
3495
3496 int nix_update_mce_list(struct rvu *rvu, u16 pcifunc,
3497 struct nix_mce_list *mce_list,
3498 int mce_idx, int mcam_index, bool add)
3499 {
3500 int err = 0, idx, next_idx, last_idx, blkaddr, npc_blkaddr;
3501 struct npc_mcam *mcam = &rvu->hw->mcam;
3502 struct nix_mcast *mcast;
3503 struct nix_hw *nix_hw;
3504 struct mce *mce;
3505
3506 if (!mce_list)
3507 return -EINVAL;
3508
3509 /* Get this PF/VF func's MCE index */
3510 idx = mce_idx + (pcifunc & RVU_PFVF_FUNC_MASK);
3511
3512 if (idx > (mce_idx + mce_list->max)) {
3513 dev_err(rvu->dev,
3514 "%s: Idx %d > max MCE idx %d, for PF%d bcast list\n",
3515 __func__, idx, mce_list->max,
3516 rvu_get_pf(rvu->pdev, pcifunc));
3517 return -EINVAL;
3518 }
3519
3520 err = nix_get_struct_ptrs(rvu, pcifunc, &nix_hw, &blkaddr);
3521 if (err)
3522 return err;
3523
3524 mcast = &nix_hw->mcast;
3525 mutex_lock(&mcast->mce_lock);
3526
3527 err = nix_update_mce_list_entry(mce_list, pcifunc, add);
3528 if (err)
3529 goto end;
3530
3531 /* Disable MCAM entry in NPC */
3532 if (!mce_list->count) {
3533 npc_blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
3534 npc_enable_mcam_entry(rvu, mcam, npc_blkaddr, mcam_index, false);
3535 goto end;
3536 }
3537
3538 /* Dump the updated list to HW */
3539 idx = mce_idx;
3540 last_idx = idx + mce_list->count - 1;
3541 hlist_for_each_entry(mce, &mce_list->head, node) {
3542 if (idx > last_idx)
3543 break;
3544
3545 next_idx = idx + 1;
3546 /* EOL should be set in last MCE */
3547 err = nix_blk_setup_mce(rvu, nix_hw, idx, NIX_AQ_INSTOP_WRITE,
3548 mce->pcifunc, next_idx,
3549 0, 1,
3550 (next_idx > last_idx) ? true : false);
3551 if (err)
3552 goto end;
3553 idx++;
3554 }
3555
3556 end:
3557 mutex_unlock(&mcast->mce_lock);
3558 return err;
3559 }
3560
3561 void nix_get_mce_list(struct rvu *rvu, u16 pcifunc, int type,
3562 struct nix_mce_list **mce_list, int *mce_idx)
3563 {
3564 struct rvu_hwinfo *hw = rvu->hw;
3565 struct rvu_pfvf *pfvf;
3566
3567 if (!hw->cap.nix_rx_multicast ||
3568 !is_pf_cgxmapped(rvu, rvu_get_pf(rvu->pdev,
3569 pcifunc & ~RVU_PFVF_FUNC_MASK))) {
3570 *mce_list = NULL;
3571 *mce_idx = 0;
3572 return;
3573 }
3574
3575 /* Get this PF/VF func's MCE index */
3576 pfvf = rvu_get_pfvf(rvu, pcifunc & ~RVU_PFVF_FUNC_MASK);
3577
3578 if (type == NIXLF_BCAST_ENTRY) {
3579 *mce_list = &pfvf->bcast_mce_list;
3580 *mce_idx = pfvf->bcast_mce_idx;
3581 } else if (type == NIXLF_ALLMULTI_ENTRY) {
3582 *mce_list = &pfvf->mcast_mce_list;
3583 *mce_idx = pfvf->mcast_mce_idx;
3584 } else if (type == NIXLF_PROMISC_ENTRY) {
3585 *mce_list = &pfvf->promisc_mce_list;
3586 *mce_idx = pfvf->promisc_mce_idx;
3587 } else {
3588 *mce_list = NULL;
3589 *mce_idx = 0;
3590 }
3591 }
3592
3593 static int nix_update_mce_rule(struct rvu *rvu, u16 pcifunc,
3594 int type, bool add)
3595 {
3596 int err = 0, nixlf, blkaddr, mcam_index, mce_idx;
3597 struct npc_mcam *mcam = &rvu->hw->mcam;
3598 struct rvu_hwinfo *hw = rvu->hw;
3599 struct nix_mce_list *mce_list;
3600 int pf;
3601
3602 /* skip multicast pkt replication for AF's VFs & SDP links */
3603 if (is_lbk_vf(rvu, pcifunc) || is_sdp_pfvf(rvu, pcifunc))
3604 return 0;
3605
3606 if (!hw->cap.nix_rx_multicast)
3607 return 0;
3608
3609 pf = rvu_get_pf(rvu->pdev, pcifunc);
3610 if (!is_pf_cgxmapped(rvu, pf))
3611 return 0;
3612
3613 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NIX, pcifunc);
3614 if (blkaddr < 0)
3615 return -EINVAL;
3616
3617 nixlf = rvu_get_lf(rvu, &hw->block[blkaddr], pcifunc, 0);
3618 if (nixlf < 0)
3619 return -EINVAL;
3620
3621 nix_get_mce_list(rvu, pcifunc, type, &mce_list, &mce_idx);
3622
3623 mcam_index = npc_get_nixlf_mcam_index(mcam,
3624 pcifunc & ~RVU_PFVF_FUNC_MASK,
3625 nixlf, type);
3626 if (mcam_index < 0)
3627 return -EINVAL;
3628
3629 err = nix_update_mce_list(rvu, pcifunc, mce_list,
3630 mce_idx, mcam_index, add);
3631 return err;
3632 }
3633
3634 static void nix_setup_mcast_grp(struct nix_hw *nix_hw)
3635 {
3636 struct nix_mcast_grp *mcast_grp = &nix_hw->mcast_grp;
3637
3638 INIT_LIST_HEAD(&mcast_grp->mcast_grp_head);
3639 mutex_init(&mcast_grp->mcast_grp_lock);
3640 mcast_grp->next_grp_index = 1;
3641 mcast_grp->count = 0;
3642 }
3643
3644 static int nix_setup_mce_tables(struct rvu *rvu, struct nix_hw *nix_hw)
3645 {
3646 struct nix_mcast *mcast = &nix_hw->mcast;
3647 int err, pf, numvfs, idx;
3648 struct rvu_pfvf *pfvf;
3649 u16 pcifunc;
3650 u64 cfg;
3651
3652 /* Skip PF0 (i.e AF) */
3653 for (pf = 1; pf < (rvu->cgx_mapped_pfs + 1); pf++) {
3654 cfg = rvu_read64(rvu, BLKADDR_RVUM, RVU_PRIV_PFX_CFG(pf));
3655 /* If PF is not enabled, nothing to do */
3656 if (!((cfg >> 20) & 0x01))
3657 continue;
3658 /* Get numVFs attached to this PF */
3659 numvfs = (cfg >> 12) & 0xFF;
3660
3661 pfvf = &rvu->pf[pf];
3662
3663 /* This NIX0/1 block mapped to PF ? */
3664 if (pfvf->nix_blkaddr != nix_hw->blkaddr)
3665 continue;
3666
3667 /* save start idx of broadcast mce list */
3668 pfvf->bcast_mce_idx = nix_alloc_mce_list(mcast, numvfs + 1, NIX_MCAST_INGRESS);
3669 nix_mce_list_init(&pfvf->bcast_mce_list, numvfs + 1);
3670
3671 /* save start idx of multicast mce list */
3672 pfvf->mcast_mce_idx = nix_alloc_mce_list(mcast, numvfs + 1, NIX_MCAST_INGRESS);
3673 nix_mce_list_init(&pfvf->mcast_mce_list, numvfs + 1);
3674
3675 /* save the start idx of promisc mce list */
3676 pfvf->promisc_mce_idx = nix_alloc_mce_list(mcast, numvfs + 1, NIX_MCAST_INGRESS);
3677 nix_mce_list_init(&pfvf->promisc_mce_list, numvfs + 1);
3678
3679 for (idx = 0; idx < (numvfs + 1); idx++) {
3680 /* idx-0 is for PF, followed by VFs */
3681 pcifunc = rvu_make_pcifunc(rvu->pdev, pf, 0);
3682 pcifunc |= idx;
3683 /* Add dummy entries now, so that we don't have to check
3684 * for whether AQ_OP should be INIT/WRITE later on.
3685 * Will be updated when a NIXLF is attached/detached to
3686 * these PF/VFs.
3687 */
3688 err = nix_blk_setup_mce(rvu, nix_hw,
3689 pfvf->bcast_mce_idx + idx,
3690 NIX_AQ_INSTOP_INIT,
3691 pcifunc, 0, 0, 1, true);
3692 if (err)
3693 return err;
3694
3695 /* add dummy entries to multicast mce list */
3696 err = nix_blk_setup_mce(rvu, nix_hw,
3697 pfvf->mcast_mce_idx + idx,
3698 NIX_AQ_INSTOP_INIT,
3699 pcifunc, 0, 0, 1, true);
3700 if (err)
3701 return err;
3702
3703 /* add dummy entries to promisc mce list */
3704 err = nix_blk_setup_mce(rvu, nix_hw,
3705 pfvf->promisc_mce_idx + idx,
3706 NIX_AQ_INSTOP_INIT,
3707 pcifunc, 0, 0, 1, true);
3708 if (err)
3709 return err;
3710 }
3711 }
3712 return 0;
3713 }
3714
3715 static int nix_setup_mcast(struct rvu *rvu, struct nix_hw *nix_hw, int blkaddr)
3716 {
3717 struct nix_mcast *mcast = &nix_hw->mcast;
3718 struct rvu_hwinfo *hw = rvu->hw;
3719 int err, size;
3720
3721 size = (rvu_read64(rvu, blkaddr, NIX_AF_CONST3) >> 16) & 0x0F;
3722 size = BIT_ULL(size);
3723
3724 /* Allocate bitmap for rx mce entries */
3725 mcast->mce_counter[NIX_MCAST_INGRESS].max = 256UL << MC_TBL_SIZE;
3726 err = rvu_alloc_bitmap(&mcast->mce_counter[NIX_MCAST_INGRESS]);
3727 if (err)
3728 return -ENOMEM;
3729
3730 /* Allocate bitmap for tx mce entries */
3731 mcast->mce_counter[NIX_MCAST_EGRESS].max = MC_TX_MAX;
3732 err = rvu_alloc_bitmap(&mcast->mce_counter[NIX_MCAST_EGRESS]);
3733 if (err) {
3734 rvu_free_bitmap(&mcast->mce_counter[NIX_MCAST_INGRESS]);
3735 return -ENOMEM;
3736 }
3737
3738 /* Alloc memory for multicast/mirror replication entries */
3739 err = qmem_alloc(rvu->dev, &mcast->mce_ctx,
3740 mcast->mce_counter[NIX_MCAST_INGRESS].max, size);
3741 if (err) {
3742 rvu_free_bitmap(&mcast->mce_counter[NIX_MCAST_INGRESS]);
3743 rvu_free_bitmap(&mcast->mce_counter[NIX_MCAST_EGRESS]);
3744 return -ENOMEM;
3745 }
3746
3747 rvu_write64(rvu, blkaddr, NIX_AF_RX_MCAST_BASE,
3748 (u64)mcast->mce_ctx->iova);
3749
3750 /* Set max list length equal to max no of VFs per PF + PF itself */
3751 rvu_write64(rvu, blkaddr, NIX_AF_RX_MCAST_CFG,
3752 BIT_ULL(36) | (hw->max_vfs_per_pf << 4) | MC_TBL_SIZE);
3753
3754 /* Alloc memory for multicast replication buffers */
3755 size = rvu_read64(rvu, blkaddr, NIX_AF_MC_MIRROR_CONST) & 0xFFFF;
3756 err = qmem_alloc(rvu->dev, &mcast->mcast_buf,
3757 (8UL << MC_BUF_CNT), size);
3758 if (err) {
3759 rvu_free_bitmap(&mcast->mce_counter[NIX_MCAST_INGRESS]);
3760 rvu_free_bitmap(&mcast->mce_counter[NIX_MCAST_EGRESS]);
3761 return -ENOMEM;
3762 }
3763
3764 rvu_write64(rvu, blkaddr, NIX_AF_RX_MCAST_BUF_BASE,
3765 (u64)mcast->mcast_buf->iova);
3766
3767 /* Alloc pkind for NIX internal RX multicast/mirror replay */
3768 mcast->replay_pkind = rvu_alloc_rsrc(&hw->pkind.rsrc);
3769
3770 rvu_write64(rvu, blkaddr, NIX_AF_RX_MCAST_BUF_CFG,
3771 BIT_ULL(63) | (mcast->replay_pkind << 24) |
3772 BIT_ULL(20) | MC_BUF_CNT);
3773
3774 mutex_init(&mcast->mce_lock);
3775
3776 nix_setup_mcast_grp(nix_hw);
3777
3778 return nix_setup_mce_tables(rvu, nix_hw);
3779 }
3780
3781 static int nix_setup_txvlan(struct rvu *rvu, struct nix_hw *nix_hw)
3782 {
3783 struct nix_txvlan *vlan = &nix_hw->txvlan;
3784 int err;
3785
3786 /* Allocate resource bimap for tx vtag def registers*/
3787 vlan->rsrc.max = NIX_TX_VTAG_DEF_MAX;
3788 err = rvu_alloc_bitmap(&vlan->rsrc);
3789 if (err)
3790 return -ENOMEM;
3791
3792 /* Alloc memory for saving entry to RVU PFFUNC allocation mapping */
3793 vlan->entry2pfvf_map = devm_kcalloc(rvu->dev, vlan->rsrc.max,
3794 sizeof(u16), GFP_KERNEL);
3795 if (!vlan->entry2pfvf_map)
3796 goto free_mem;
3797
3798 mutex_init(&vlan->rsrc_lock);
3799 return 0;
3800
3801 free_mem:
3802 kfree(vlan->rsrc.bmap);
3803 return -ENOMEM;
3804 }
3805
3806 static int nix_setup_txschq(struct rvu *rvu, struct nix_hw *nix_hw, int blkaddr)
3807 {
3808 struct nix_txsch *txsch;
3809 int err, lvl, schq;
3810 u64 cfg, reg;
3811
3812 /* Get scheduler queue count of each type and alloc
3813 * bitmap for each for alloc/free/attach operations.
3814 */
3815 for (lvl = 0; lvl < NIX_TXSCH_LVL_CNT; lvl++) {
3816 txsch = &nix_hw->txsch[lvl];
3817 txsch->lvl = lvl;
3818 switch (lvl) {
3819 case NIX_TXSCH_LVL_SMQ:
3820 reg = NIX_AF_MDQ_CONST;
3821 break;
3822 case NIX_TXSCH_LVL_TL4:
3823 reg = NIX_AF_TL4_CONST;
3824 break;
3825 case NIX_TXSCH_LVL_TL3:
3826 reg = NIX_AF_TL3_CONST;
3827 break;
3828 case NIX_TXSCH_LVL_TL2:
3829 reg = NIX_AF_TL2_CONST;
3830 break;
3831 case NIX_TXSCH_LVL_TL1:
3832 reg = NIX_AF_TL1_CONST;
3833 break;
3834 }
3835 cfg = rvu_read64(rvu, blkaddr, reg);
3836 txsch->schq.max = cfg & 0xFFFF;
3837 err = rvu_alloc_bitmap(&txsch->schq);
3838 if (err)
3839 return err;
3840
3841 /* Allocate memory for scheduler queues to
3842 * PF/VF pcifunc mapping info.
3843 */
3844 txsch->pfvf_map = devm_kcalloc(rvu->dev, txsch->schq.max,
3845 sizeof(u32), GFP_KERNEL);
3846 if (!txsch->pfvf_map)
3847 return -ENOMEM;
3848 for (schq = 0; schq < txsch->schq.max; schq++)
3849 txsch->pfvf_map[schq] = TXSCH_MAP(0, NIX_TXSCHQ_FREE);
3850 }
3851
3852 /* Setup a default value of 8192 as DWRR MTU */
3853 if (rvu->hw->cap.nix_common_dwrr_mtu ||
3854 rvu->hw->cap.nix_multiple_dwrr_mtu) {
3855 rvu_write64(rvu, blkaddr,
3856 nix_get_dwrr_mtu_reg(rvu->hw, SMQ_LINK_TYPE_RPM),
3857 convert_bytes_to_dwrr_mtu(8192));
3858 rvu_write64(rvu, blkaddr,
3859 nix_get_dwrr_mtu_reg(rvu->hw, SMQ_LINK_TYPE_LBK),
3860 convert_bytes_to_dwrr_mtu(8192));
3861 rvu_write64(rvu, blkaddr,
3862 nix_get_dwrr_mtu_reg(rvu->hw, SMQ_LINK_TYPE_SDP),
3863 convert_bytes_to_dwrr_mtu(8192));
3864 }
3865
3866 return 0;
3867 }
3868
3869 int rvu_nix_reserve_mark_format(struct rvu *rvu, struct nix_hw *nix_hw,
3870 int blkaddr, u32 cfg)
3871 {
3872 int fmt_idx;
3873
3874 for (fmt_idx = 0; fmt_idx < nix_hw->mark_format.in_use; fmt_idx++) {
3875 if (nix_hw->mark_format.cfg[fmt_idx] == cfg)
3876 return fmt_idx;
3877 }
3878 if (fmt_idx >= nix_hw->mark_format.total)
3879 return -ERANGE;
3880
3881 rvu_write64(rvu, blkaddr, NIX_AF_MARK_FORMATX_CTL(fmt_idx), cfg);
3882 nix_hw->mark_format.cfg[fmt_idx] = cfg;
3883 nix_hw->mark_format.in_use++;
3884 return fmt_idx;
3885 }
3886
3887 static int nix_af_mark_format_setup(struct rvu *rvu, struct nix_hw *nix_hw,
3888 int blkaddr)
3889 {
3890 u64 cfgs[] = {
3891 [NIX_MARK_CFG_IP_DSCP_RED] = 0x10003,
3892 [NIX_MARK_CFG_IP_DSCP_YELLOW] = 0x11200,
3893 [NIX_MARK_CFG_IP_DSCP_YELLOW_RED] = 0x11203,
3894 [NIX_MARK_CFG_IP_ECN_RED] = 0x6000c,
3895 [NIX_MARK_CFG_IP_ECN_YELLOW] = 0x60c00,
3896 [NIX_MARK_CFG_IP_ECN_YELLOW_RED] = 0x60c0c,
3897 [NIX_MARK_CFG_VLAN_DEI_RED] = 0x30008,
3898 [NIX_MARK_CFG_VLAN_DEI_YELLOW] = 0x30800,
3899 [NIX_MARK_CFG_VLAN_DEI_YELLOW_RED] = 0x30808,
3900 };
3901 int i, rc;
3902 u64 total;
3903
3904 total = (rvu_read64(rvu, blkaddr, NIX_AF_PSE_CONST) & 0xFF00) >> 8;
3905 nix_hw->mark_format.total = (u8)total;
3906 nix_hw->mark_format.cfg = devm_kcalloc(rvu->dev, total, sizeof(u32),
3907 GFP_KERNEL);
3908 if (!nix_hw->mark_format.cfg)
3909 return -ENOMEM;
3910 for (i = 0; i < NIX_MARK_CFG_MAX; i++) {
3911 rc = rvu_nix_reserve_mark_format(rvu, nix_hw, blkaddr, cfgs[i]);
3912 if (rc < 0)
3913 dev_err(rvu->dev, "Err %d in setup mark format %d\n",
3914 i, rc);
3915 }
3916
3917 return 0;
3918 }
3919
3920 static void rvu_get_lbk_link_max_frs(struct rvu *rvu, u16 *max_mtu)
3921 {
3922 /* CN10K supports LBK FIFO size 72 KB */
3923 if (rvu->hw->lbk_bufsize == 0x12000)
3924 *max_mtu = CN10K_LBK_LINK_MAX_FRS;
3925 else
3926 *max_mtu = NIC_HW_MAX_FRS;
3927 }
3928
3929 static void rvu_get_lmac_link_max_frs(struct rvu *rvu, u16 *max_mtu)
3930 {
3931 int fifo_size = rvu_cgx_get_fifolen(rvu);
3932
3933 /* RPM supports FIFO len 128 KB and RPM2 supports double the
3934 * FIFO len to accommodate 8 LMACS
3935 */
3936 if (fifo_size == 0x20000 || fifo_size == 0x40000)
3937 *max_mtu = CN10K_LMAC_LINK_MAX_FRS;
3938 else
3939 *max_mtu = NIC_HW_MAX_FRS;
3940 }
3941
3942 int rvu_mbox_handler_nix_get_hw_info(struct rvu *rvu, struct msg_req *req,
3943 struct nix_hw_info *rsp)
3944 {
3945 u16 pcifunc = req->hdr.pcifunc;
3946 u64 dwrr_mtu;
3947 int blkaddr;
3948
3949 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NIX, pcifunc);
3950 if (blkaddr < 0)
3951 return NIX_AF_ERR_AF_LF_INVALID;
3952
3953 if (is_lbk_vf(rvu, pcifunc))
3954 rvu_get_lbk_link_max_frs(rvu, &rsp->max_mtu);
3955 else
3956 rvu_get_lmac_link_max_frs(rvu, &rsp->max_mtu);
3957
3958 rsp->min_mtu = NIC_HW_MIN_FRS;
3959
3960 if (!rvu->hw->cap.nix_common_dwrr_mtu &&
3961 !rvu->hw->cap.nix_multiple_dwrr_mtu) {
3962 /* Return '1' on OTx2 */
3963 rsp->rpm_dwrr_mtu = 1;
3964 rsp->sdp_dwrr_mtu = 1;
3965 rsp->lbk_dwrr_mtu = 1;
3966 return 0;
3967 }
3968
3969 /* Return DWRR_MTU for TLx_SCHEDULE[RR_WEIGHT] config */
3970 dwrr_mtu = rvu_read64(rvu, blkaddr,
3971 nix_get_dwrr_mtu_reg(rvu->hw, SMQ_LINK_TYPE_RPM));
3972 rsp->rpm_dwrr_mtu = convert_dwrr_mtu_to_bytes(dwrr_mtu);
3973
3974 dwrr_mtu = rvu_read64(rvu, blkaddr,
3975 nix_get_dwrr_mtu_reg(rvu->hw, SMQ_LINK_TYPE_SDP));
3976 rsp->sdp_dwrr_mtu = convert_dwrr_mtu_to_bytes(dwrr_mtu);
3977
3978 dwrr_mtu = rvu_read64(rvu, blkaddr,
3979 nix_get_dwrr_mtu_reg(rvu->hw, SMQ_LINK_TYPE_LBK));
3980 rsp->lbk_dwrr_mtu = convert_dwrr_mtu_to_bytes(dwrr_mtu);
3981
3982 return 0;
3983 }
3984
3985 int rvu_mbox_handler_nix_stats_rst(struct rvu *rvu, struct msg_req *req,
3986 struct msg_rsp *rsp)
3987 {
3988 u16 pcifunc = req->hdr.pcifunc;
3989 int i, nixlf, blkaddr, err;
3990 u64 stats;
3991
3992 err = nix_get_nixlf(rvu, pcifunc, &nixlf, &blkaddr);
3993 if (err)
3994 return err;
3995
3996 /* Get stats count supported by HW */
3997 stats = rvu_read64(rvu, blkaddr, NIX_AF_CONST1);
3998
3999 /* Reset tx stats */
4000 for (i = 0; i < ((stats >> 24) & 0xFF); i++)
4001 rvu_write64(rvu, blkaddr, NIX_AF_LFX_TX_STATX(nixlf, i), 0);
4002
4003 /* Reset rx stats */
4004 for (i = 0; i < ((stats >> 32) & 0xFF); i++)
4005 rvu_write64(rvu, blkaddr, NIX_AF_LFX_RX_STATX(nixlf, i), 0);
4006
4007 return 0;
4008 }
4009
4010 /* Returns the ALG index to be set into NPC_RX_ACTION */
4011 static int get_flowkey_alg_idx(struct nix_hw *nix_hw, u32 flow_cfg)
4012 {
4013 int i;
4014
4015 /* Scan over exiting algo entries to find a match */
4016 for (i = 0; i < nix_hw->flowkey.in_use; i++)
4017 if (nix_hw->flowkey.flowkey[i] == flow_cfg)
4018 return i;
4019
4020 return -ERANGE;
4021 }
4022
4023 /* Mask to match ipv6(NPC_LT_LC_IP6) and ipv6 ext(NPC_LT_LC_IP6_EXT) */
4024 #define NPC_LT_LC_IP6_MATCH_MSK ((~(NPC_LT_LC_IP6 ^ NPC_LT_LC_IP6_EXT)) & 0xf)
4025 /* Mask to match both ipv4(NPC_LT_LC_IP) and ipv4 ext(NPC_LT_LC_IP_OPT) */
4026 #define NPC_LT_LC_IP_MATCH_MSK ((~(NPC_LT_LC_IP ^ NPC_LT_LC_IP_OPT)) & 0xf)
4027
4028 static int set_flowkey_fields(struct nix_rx_flowkey_alg *alg, u32 flow_cfg)
4029 {
4030 int idx, nr_field, key_off, field_marker, keyoff_marker;
4031 int max_key_off, max_bit_pos, group_member;
4032 struct nix_rx_flowkey_alg *field;
4033 struct nix_rx_flowkey_alg tmp;
4034 u32 key_type, valid_key;
4035 u32 l3_l4_src_dst;
4036 int l4_key_offset = 0;
4037
4038 if (!alg)
4039 return -EINVAL;
4040
4041 #define FIELDS_PER_ALG 5
4042 #define MAX_KEY_OFF 40
4043 /* Clear all fields */
4044 memset(alg, 0, sizeof(uint64_t) * FIELDS_PER_ALG);
4045
4046 /* Each of the 32 possible flow key algorithm definitions should
4047 * fall into above incremental config (except ALG0). Otherwise a
4048 * single NPC MCAM entry is not sufficient for supporting RSS.
4049 *
4050 * If a different definition or combination needed then NPC MCAM
4051 * has to be programmed to filter such pkts and it's action should
4052 * point to this definition to calculate flowtag or hash.
4053 *
4054 * The `for loop` goes over _all_ protocol field and the following
4055 * variables depicts the state machine forward progress logic.
4056 *
4057 * keyoff_marker - Enabled when hash byte length needs to be accounted
4058 * in field->key_offset update.
4059 * field_marker - Enabled when a new field needs to be selected.
4060 * group_member - Enabled when protocol is part of a group.
4061 */
4062
4063 /* Last 4 bits (31:28) are reserved to specify SRC, DST
4064 * selection for L3, L4 i.e IPV[4,6]_SRC, IPV[4,6]_DST,
4065 * [TCP,UDP,SCTP]_SRC, [TCP,UDP,SCTP]_DST
4066 * 31 => L3_SRC, 30 => L3_DST, 29 => L4_SRC, 28 => L4_DST
4067 */
4068 l3_l4_src_dst = flow_cfg;
4069 /* Reset these 4 bits, so that these won't be part of key */
4070 flow_cfg &= NIX_FLOW_KEY_TYPE_L3_L4_MASK;
4071
4072 keyoff_marker = 0; max_key_off = 0; group_member = 0;
4073 nr_field = 0; key_off = 0; field_marker = 1;
4074 field = &tmp; max_bit_pos = fls(flow_cfg);
4075 for (idx = 0;
4076 idx < max_bit_pos && nr_field < FIELDS_PER_ALG &&
4077 key_off < MAX_KEY_OFF; idx++) {
4078 key_type = BIT(idx);
4079 valid_key = flow_cfg & key_type;
4080 /* Found a field marker, reset the field values */
4081 if (field_marker)
4082 memset(&tmp, 0, sizeof(tmp));
4083
4084 field_marker = true;
4085 keyoff_marker = true;
4086 switch (key_type) {
4087 case NIX_FLOW_KEY_TYPE_PORT:
4088 field->sel_chan = true;
4089 /* This should be set to 1, when SEL_CHAN is set */
4090 field->bytesm1 = 1;
4091 break;
4092 case NIX_FLOW_KEY_TYPE_IPV4_PROTO:
4093 field->lid = NPC_LID_LC;
4094 field->hdr_offset = 9; /* offset */
4095 field->bytesm1 = 0; /* 1 byte */
4096 field->ltype_match = NPC_LT_LC_IP;
4097 field->ltype_mask = NPC_LT_LC_IP_MATCH_MSK;
4098 break;
4099 case NIX_FLOW_KEY_TYPE_IPV4:
4100 case NIX_FLOW_KEY_TYPE_INNR_IPV4:
4101 field->lid = NPC_LID_LC;
4102 field->ltype_match = NPC_LT_LC_IP;
4103 if (key_type == NIX_FLOW_KEY_TYPE_INNR_IPV4) {
4104 field->lid = NPC_LID_LG;
4105 field->ltype_match = NPC_LT_LG_TU_IP;
4106 }
4107 field->hdr_offset = 12; /* SIP offset */
4108 field->bytesm1 = 7; /* SIP + DIP, 8 bytes */
4109
4110 /* Only SIP */
4111 if (l3_l4_src_dst & NIX_FLOW_KEY_TYPE_L3_SRC_ONLY)
4112 field->bytesm1 = 3; /* SIP, 4 bytes */
4113
4114 if (l3_l4_src_dst & NIX_FLOW_KEY_TYPE_L3_DST_ONLY) {
4115 /* Both SIP + DIP */
4116 if (field->bytesm1 == 3) {
4117 field->bytesm1 = 7; /* SIP + DIP, 8B */
4118 } else {
4119 /* Only DIP */
4120 field->hdr_offset = 16; /* DIP off */
4121 field->bytesm1 = 3; /* DIP, 4 bytes */
4122 }
4123 }
4124 field->ltype_mask = NPC_LT_LC_IP_MATCH_MSK;
4125 keyoff_marker = false;
4126 break;
4127 case NIX_FLOW_KEY_TYPE_IPV6:
4128 case NIX_FLOW_KEY_TYPE_INNR_IPV6:
4129 field->lid = NPC_LID_LC;
4130 field->ltype_match = NPC_LT_LC_IP6;
4131 if (key_type == NIX_FLOW_KEY_TYPE_INNR_IPV6) {
4132 field->lid = NPC_LID_LG;
4133 field->ltype_match = NPC_LT_LG_TU_IP6;
4134 }
4135 field->hdr_offset = 8; /* SIP offset */
4136 field->bytesm1 = 31; /* SIP + DIP, 32 bytes */
4137
4138 /* Only SIP */
4139 if (l3_l4_src_dst & NIX_FLOW_KEY_TYPE_L3_SRC_ONLY)
4140 field->bytesm1 = 15; /* SIP, 16 bytes */
4141
4142 if (l3_l4_src_dst & NIX_FLOW_KEY_TYPE_L3_DST_ONLY) {
4143 /* Both SIP + DIP */
4144 if (field->bytesm1 == 15) {
4145 /* SIP + DIP, 32 bytes */
4146 field->bytesm1 = 31;
4147 } else {
4148 /* Only DIP */
4149 field->hdr_offset = 24; /* DIP off */
4150 field->bytesm1 = 15; /* DIP,16 bytes */
4151 }
4152 }
4153 field->ltype_mask = NPC_LT_LC_IP6_MATCH_MSK;
4154 break;
4155 case NIX_FLOW_KEY_TYPE_TCP:
4156 case NIX_FLOW_KEY_TYPE_UDP:
4157 case NIX_FLOW_KEY_TYPE_SCTP:
4158 case NIX_FLOW_KEY_TYPE_INNR_TCP:
4159 case NIX_FLOW_KEY_TYPE_INNR_UDP:
4160 case NIX_FLOW_KEY_TYPE_INNR_SCTP:
4161 field->lid = NPC_LID_LD;
4162 if (key_type == NIX_FLOW_KEY_TYPE_INNR_TCP ||
4163 key_type == NIX_FLOW_KEY_TYPE_INNR_UDP ||
4164 key_type == NIX_FLOW_KEY_TYPE_INNR_SCTP)
4165 field->lid = NPC_LID_LH;
4166 field->bytesm1 = 3; /* Sport + Dport, 4 bytes */
4167
4168 if (l3_l4_src_dst & NIX_FLOW_KEY_TYPE_L4_SRC_ONLY)
4169 field->bytesm1 = 1; /* SRC, 2 bytes */
4170
4171 if (l3_l4_src_dst & NIX_FLOW_KEY_TYPE_L4_DST_ONLY) {
4172 /* Both SRC + DST */
4173 if (field->bytesm1 == 1) {
4174 /* SRC + DST, 4 bytes */
4175 field->bytesm1 = 3;
4176 } else {
4177 /* Only DIP */
4178 field->hdr_offset = 2; /* DST off */
4179 field->bytesm1 = 1; /* DST, 2 bytes */
4180 }
4181 }
4182
4183 /* Enum values for NPC_LID_LD and NPC_LID_LG are same,
4184 * so no need to change the ltype_match, just change
4185 * the lid for inner protocols
4186 */
4187 BUILD_BUG_ON((int)NPC_LT_LD_TCP !=
4188 (int)NPC_LT_LH_TU_TCP);
4189 BUILD_BUG_ON((int)NPC_LT_LD_UDP !=
4190 (int)NPC_LT_LH_TU_UDP);
4191 BUILD_BUG_ON((int)NPC_LT_LD_SCTP !=
4192 (int)NPC_LT_LH_TU_SCTP);
4193
4194 if ((key_type == NIX_FLOW_KEY_TYPE_TCP ||
4195 key_type == NIX_FLOW_KEY_TYPE_INNR_TCP) &&
4196 valid_key) {
4197 field->ltype_match |= NPC_LT_LD_TCP;
4198 group_member = true;
4199 } else if ((key_type == NIX_FLOW_KEY_TYPE_UDP ||
4200 key_type == NIX_FLOW_KEY_TYPE_INNR_UDP) &&
4201 valid_key) {
4202 field->ltype_match |= NPC_LT_LD_UDP;
4203 group_member = true;
4204 } else if ((key_type == NIX_FLOW_KEY_TYPE_SCTP ||
4205 key_type == NIX_FLOW_KEY_TYPE_INNR_SCTP) &&
4206 valid_key) {
4207 field->ltype_match |= NPC_LT_LD_SCTP;
4208 group_member = true;
4209 }
4210 field->ltype_mask = ~field->ltype_match;
4211 if (key_type == NIX_FLOW_KEY_TYPE_SCTP ||
4212 key_type == NIX_FLOW_KEY_TYPE_INNR_SCTP) {
4213 /* Handle the case where any of the group item
4214 * is enabled in the group but not the final one
4215 */
4216 if (group_member) {
4217 valid_key = true;
4218 group_member = false;
4219 }
4220 } else {
4221 field_marker = false;
4222 keyoff_marker = false;
4223 }
4224
4225 /* TCP/UDP/SCTP and ESP/AH falls at same offset so
4226 * remember the TCP key offset of 40 byte hash key.
4227 */
4228 if (key_type == NIX_FLOW_KEY_TYPE_TCP)
4229 l4_key_offset = key_off;
4230 break;
4231 case NIX_FLOW_KEY_TYPE_NVGRE:
4232 field->lid = NPC_LID_LD;
4233 field->hdr_offset = 4; /* VSID offset */
4234 field->bytesm1 = 2;
4235 field->ltype_match = NPC_LT_LD_NVGRE;
4236 field->ltype_mask = 0xF;
4237 break;
4238 case NIX_FLOW_KEY_TYPE_VXLAN:
4239 case NIX_FLOW_KEY_TYPE_GENEVE:
4240 field->lid = NPC_LID_LE;
4241 field->bytesm1 = 2;
4242 field->hdr_offset = 4;
4243 field->ltype_mask = 0xF;
4244 field_marker = false;
4245 keyoff_marker = false;
4246
4247 if (key_type == NIX_FLOW_KEY_TYPE_VXLAN && valid_key) {
4248 field->ltype_match |= NPC_LT_LE_VXLAN;
4249 group_member = true;
4250 }
4251
4252 if (key_type == NIX_FLOW_KEY_TYPE_GENEVE && valid_key) {
4253 field->ltype_match |= NPC_LT_LE_GENEVE;
4254 group_member = true;
4255 }
4256
4257 if (key_type == NIX_FLOW_KEY_TYPE_GENEVE) {
4258 if (group_member) {
4259 field->ltype_mask = ~field->ltype_match;
4260 field_marker = true;
4261 keyoff_marker = true;
4262 valid_key = true;
4263 group_member = false;
4264 }
4265 }
4266 break;
4267 case NIX_FLOW_KEY_TYPE_ETH_DMAC:
4268 case NIX_FLOW_KEY_TYPE_INNR_ETH_DMAC:
4269 field->lid = NPC_LID_LA;
4270 field->ltype_match = NPC_LT_LA_ETHER;
4271 if (key_type == NIX_FLOW_KEY_TYPE_INNR_ETH_DMAC) {
4272 field->lid = NPC_LID_LF;
4273 field->ltype_match = NPC_LT_LF_TU_ETHER;
4274 }
4275 field->hdr_offset = 0;
4276 field->bytesm1 = 5; /* DMAC 6 Byte */
4277 field->ltype_mask = 0xF;
4278 break;
4279 case NIX_FLOW_KEY_TYPE_IPV6_EXT:
4280 field->lid = NPC_LID_LC;
4281 field->hdr_offset = 40; /* IPV6 hdr */
4282 field->bytesm1 = 0; /* 1 Byte ext hdr*/
4283 field->ltype_match = NPC_LT_LC_IP6_EXT;
4284 field->ltype_mask = 0xF;
4285 break;
4286 case NIX_FLOW_KEY_TYPE_GTPU:
4287 field->lid = NPC_LID_LE;
4288 field->hdr_offset = 4;
4289 field->bytesm1 = 3; /* 4 bytes TID*/
4290 field->ltype_match = NPC_LT_LE_GTPU;
4291 field->ltype_mask = 0xF;
4292 break;
4293 case NIX_FLOW_KEY_TYPE_CUSTOM0:
4294 field->lid = NPC_LID_LC;
4295 field->hdr_offset = 6;
4296 field->bytesm1 = 1; /* 2 Bytes*/
4297 field->ltype_match = NPC_LT_LC_CUSTOM0;
4298 field->ltype_mask = 0xF;
4299 break;
4300 case NIX_FLOW_KEY_TYPE_VLAN:
4301 field->lid = NPC_LID_LB;
4302 field->hdr_offset = 2; /* Skip TPID (2-bytes) */
4303 field->bytesm1 = 1; /* 2 Bytes (Actually 12 bits) */
4304 field->ltype_match = NPC_LT_LB_CTAG;
4305 field->ltype_mask = 0xF;
4306 field->fn_mask = 1; /* Mask out the first nibble */
4307 break;
4308 case NIX_FLOW_KEY_TYPE_AH:
4309 case NIX_FLOW_KEY_TYPE_ESP:
4310 field->hdr_offset = 0;
4311 field->bytesm1 = 7; /* SPI + sequence number */
4312 field->ltype_mask = 0xF;
4313 field->lid = NPC_LID_LE;
4314 field->ltype_match = NPC_LT_LE_ESP;
4315 if (key_type == NIX_FLOW_KEY_TYPE_AH) {
4316 field->lid = NPC_LID_LD;
4317 field->ltype_match = NPC_LT_LD_AH;
4318 field->hdr_offset = 4;
4319 keyoff_marker = false;
4320 }
4321 break;
4322 }
4323 field->ena = 1;
4324
4325 /* Found a valid flow key type */
4326 if (valid_key) {
4327 /* Use the key offset of TCP/UDP/SCTP fields
4328 * for ESP/AH fields.
4329 */
4330 if (key_type == NIX_FLOW_KEY_TYPE_ESP ||
4331 key_type == NIX_FLOW_KEY_TYPE_AH)
4332 key_off = l4_key_offset;
4333 field->key_offset = key_off;
4334 memcpy(&alg[nr_field], field, sizeof(*field));
4335 max_key_off = max(max_key_off, field->bytesm1 + 1);
4336
4337 /* Found a field marker, get the next field */
4338 if (field_marker)
4339 nr_field++;
4340 }
4341
4342 /* Found a keyoff marker, update the new key_off */
4343 if (keyoff_marker) {
4344 key_off += max_key_off;
4345 max_key_off = 0;
4346 }
4347 }
4348 /* Processed all the flow key types */
4349 if (idx == max_bit_pos && key_off <= MAX_KEY_OFF)
4350 return 0;
4351 else
4352 return NIX_AF_ERR_RSS_NOSPC_FIELD;
4353 }
4354
4355 static int reserve_flowkey_alg_idx(struct rvu *rvu, int blkaddr, u32 flow_cfg)
4356 {
4357 u64 field[FIELDS_PER_ALG];
4358 struct nix_hw *hw;
4359 int fid, rc;
4360
4361 hw = get_nix_hw(rvu->hw, blkaddr);
4362 if (!hw)
4363 return NIX_AF_ERR_INVALID_NIXBLK;
4364
4365 /* No room to add new flow hash algoritham */
4366 if (hw->flowkey.in_use >= NIX_FLOW_KEY_ALG_MAX)
4367 return NIX_AF_ERR_RSS_NOSPC_ALGO;
4368
4369 /* Generate algo fields for the given flow_cfg */
4370 rc = set_flowkey_fields((struct nix_rx_flowkey_alg *)field, flow_cfg);
4371 if (rc)
4372 return rc;
4373
4374 /* Update ALGX_FIELDX register with generated fields */
4375 for (fid = 0; fid < FIELDS_PER_ALG; fid++)
4376 rvu_write64(rvu, blkaddr,
4377 NIX_AF_RX_FLOW_KEY_ALGX_FIELDX(hw->flowkey.in_use,
4378 fid), field[fid]);
4379
4380 /* Store the flow_cfg for futher lookup */
4381 rc = hw->flowkey.in_use;
4382 hw->flowkey.flowkey[rc] = flow_cfg;
4383 hw->flowkey.in_use++;
4384
4385 return rc;
4386 }
4387
4388 int rvu_mbox_handler_nix_rss_flowkey_cfg(struct rvu *rvu,
4389 struct nix_rss_flowkey_cfg *req,
4390 struct nix_rss_flowkey_cfg_rsp *rsp)
4391 {
4392 u16 pcifunc = req->hdr.pcifunc;
4393 int alg_idx, nixlf, blkaddr;
4394 struct nix_hw *nix_hw;
4395 int err;
4396
4397 err = nix_get_nixlf(rvu, pcifunc, &nixlf, &blkaddr);
4398 if (err)
4399 return err;
4400
4401 nix_hw = get_nix_hw(rvu->hw, blkaddr);
4402 if (!nix_hw)
4403 return NIX_AF_ERR_INVALID_NIXBLK;
4404
4405 alg_idx = get_flowkey_alg_idx(nix_hw, req->flowkey_cfg);
4406 /* Failed to get algo index from the exiting list, reserve new */
4407 if (alg_idx < 0) {
4408 alg_idx = reserve_flowkey_alg_idx(rvu, blkaddr,
4409 req->flowkey_cfg);
4410 if (alg_idx < 0)
4411 return alg_idx;
4412 }
4413 rsp->alg_idx = alg_idx;
4414 rvu_npc_update_flowkey_alg_idx(rvu, pcifunc, nixlf, req->group,
4415 alg_idx, req->mcam_index);
4416 return 0;
4417 }
4418
4419 static int nix_rx_flowkey_alg_cfg(struct rvu *rvu, int blkaddr)
4420 {
4421 u32 flowkey_cfg, minkey_cfg;
4422 int alg, fid, rc;
4423
4424 /* Disable all flow key algx fieldx */
4425 for (alg = 0; alg < NIX_FLOW_KEY_ALG_MAX; alg++) {
4426 for (fid = 0; fid < FIELDS_PER_ALG; fid++)
4427 rvu_write64(rvu, blkaddr,
4428 NIX_AF_RX_FLOW_KEY_ALGX_FIELDX(alg, fid),
4429 0);
4430 }
4431
4432 /* IPv4/IPv6 SIP/DIPs */
4433 flowkey_cfg = NIX_FLOW_KEY_TYPE_IPV4 | NIX_FLOW_KEY_TYPE_IPV6;
4434 rc = reserve_flowkey_alg_idx(rvu, blkaddr, flowkey_cfg);
4435 if (rc < 0)
4436 return rc;
4437
4438 /* TCPv4/v6 4-tuple, SIP, DIP, Sport, Dport */
4439 minkey_cfg = flowkey_cfg;
4440 flowkey_cfg = minkey_cfg | NIX_FLOW_KEY_TYPE_TCP;
4441 rc = reserve_flowkey_alg_idx(rvu, blkaddr, flowkey_cfg);
4442 if (rc < 0)
4443 return rc;
4444
4445 /* UDPv4/v6 4-tuple, SIP, DIP, Sport, Dport */
4446 flowkey_cfg = minkey_cfg | NIX_FLOW_KEY_TYPE_UDP;
4447 rc = reserve_flowkey_alg_idx(rvu, blkaddr, flowkey_cfg);
4448 if (rc < 0)
4449 return rc;
4450
4451 /* SCTPv4/v6 4-tuple, SIP, DIP, Sport, Dport */
4452 flowkey_cfg = minkey_cfg | NIX_FLOW_KEY_TYPE_SCTP;
4453 rc = reserve_flowkey_alg_idx(rvu, blkaddr, flowkey_cfg);
4454 if (rc < 0)
4455 return rc;
4456
4457 /* TCP/UDP v4/v6 4-tuple, rest IP pkts 2-tuple */
4458 flowkey_cfg = minkey_cfg | NIX_FLOW_KEY_TYPE_TCP |
4459 NIX_FLOW_KEY_TYPE_UDP;
4460 rc = reserve_flowkey_alg_idx(rvu, blkaddr, flowkey_cfg);
4461 if (rc < 0)
4462 return rc;
4463
4464 /* TCP/SCTP v4/v6 4-tuple, rest IP pkts 2-tuple */
4465 flowkey_cfg = minkey_cfg | NIX_FLOW_KEY_TYPE_TCP |
4466 NIX_FLOW_KEY_TYPE_SCTP;
4467 rc = reserve_flowkey_alg_idx(rvu, blkaddr, flowkey_cfg);
4468 if (rc < 0)
4469 return rc;
4470
4471 /* UDP/SCTP v4/v6 4-tuple, rest IP pkts 2-tuple */
4472 flowkey_cfg = minkey_cfg | NIX_FLOW_KEY_TYPE_UDP |
4473 NIX_FLOW_KEY_TYPE_SCTP;
4474 rc = reserve_flowkey_alg_idx(rvu, blkaddr, flowkey_cfg);
4475 if (rc < 0)
4476 return rc;
4477
4478 /* TCP/UDP/SCTP v4/v6 4-tuple, rest IP pkts 2-tuple */
4479 flowkey_cfg = minkey_cfg | NIX_FLOW_KEY_TYPE_TCP |
4480 NIX_FLOW_KEY_TYPE_UDP | NIX_FLOW_KEY_TYPE_SCTP;
4481 rc = reserve_flowkey_alg_idx(rvu, blkaddr, flowkey_cfg);
4482 if (rc < 0)
4483 return rc;
4484
4485 return 0;
4486 }
4487
4488 int rvu_mbox_handler_nix_set_mac_addr(struct rvu *rvu,
4489 struct nix_set_mac_addr *req,
4490 struct msg_rsp *rsp)
4491 {
4492 bool from_vf = req->hdr.pcifunc & RVU_PFVF_FUNC_MASK;
4493 u16 pcifunc = req->hdr.pcifunc;
4494 int blkaddr, nixlf, err;
4495 struct rvu_pfvf *pfvf;
4496
4497 err = nix_get_nixlf(rvu, pcifunc, &nixlf, &blkaddr);
4498 if (err)
4499 return err;
4500
4501 pfvf = rvu_get_pfvf(rvu, pcifunc);
4502
4503 /* untrusted VF can't overwrite admin(PF) changes */
4504 if (!test_bit(PF_SET_VF_TRUSTED, &pfvf->flags) &&
4505 (from_vf && test_bit(PF_SET_VF_MAC, &pfvf->flags))) {
4506 dev_warn(rvu->dev,
4507 "MAC address set by admin(PF) cannot be overwritten by untrusted VF");
4508 return -EPERM;
4509 }
4510
4511 ether_addr_copy(pfvf->mac_addr, req->mac_addr);
4512
4513 rvu_npc_install_ucast_entry(rvu, pcifunc, nixlf,
4514 pfvf->rx_chan_base, req->mac_addr);
4515
4516 if (test_bit(PF_SET_VF_TRUSTED, &pfvf->flags) && from_vf)
4517 ether_addr_copy(pfvf->default_mac, req->mac_addr);
4518
4519 return 0;
4520 }
4521
4522 int rvu_mbox_handler_nix_get_mac_addr(struct rvu *rvu,
4523 struct msg_req *req,
4524 struct nix_get_mac_addr_rsp *rsp)
4525 {
4526 u16 pcifunc = req->hdr.pcifunc;
4527 struct rvu_pfvf *pfvf;
4528
4529 if (!is_nixlf_attached(rvu, pcifunc))
4530 return NIX_AF_ERR_AF_LF_INVALID;
4531
4532 pfvf = rvu_get_pfvf(rvu, pcifunc);
4533
4534 ether_addr_copy(rsp->mac_addr, pfvf->mac_addr);
4535
4536 return 0;
4537 }
4538
4539 int rvu_mbox_handler_nix_set_rx_mode(struct rvu *rvu, struct nix_rx_mode *req,
4540 struct msg_rsp *rsp)
4541 {
4542 bool allmulti, promisc, nix_rx_multicast;
4543 u16 pcifunc = req->hdr.pcifunc;
4544 struct rvu_pfvf *pfvf;
4545 int nixlf, err;
4546
4547 pfvf = rvu_get_pfvf(rvu, pcifunc);
4548 promisc = req->mode & NIX_RX_MODE_PROMISC ? true : false;
4549 allmulti = req->mode & NIX_RX_MODE_ALLMULTI ? true : false;
4550 pfvf->use_mce_list = req->mode & NIX_RX_MODE_USE_MCE ? true : false;
4551
4552 nix_rx_multicast = rvu->hw->cap.nix_rx_multicast & pfvf->use_mce_list;
4553
4554 if (is_vf(pcifunc) && !nix_rx_multicast &&
4555 (promisc || allmulti)) {
4556 dev_warn_ratelimited(rvu->dev,
4557 "VF promisc/multicast not supported\n");
4558 return 0;
4559 }
4560
4561 /* untrusted VF can't configure promisc/allmulti */
4562 if (is_vf(pcifunc) && !test_bit(PF_SET_VF_TRUSTED, &pfvf->flags) &&
4563 (promisc || allmulti))
4564 return 0;
4565
4566 err = nix_get_nixlf(rvu, pcifunc, &nixlf, NULL);
4567 if (err)
4568 return err;
4569
4570 if (nix_rx_multicast) {
4571 /* add/del this PF_FUNC to/from mcast pkt replication list */
4572 err = nix_update_mce_rule(rvu, pcifunc, NIXLF_ALLMULTI_ENTRY,
4573 allmulti);
4574 if (err) {
4575 dev_err(rvu->dev,
4576 "Failed to update pcifunc 0x%x to multicast list\n",
4577 pcifunc);
4578 return err;
4579 }
4580
4581 /* add/del this PF_FUNC to/from promisc pkt replication list */
4582 err = nix_update_mce_rule(rvu, pcifunc, NIXLF_PROMISC_ENTRY,
4583 promisc);
4584 if (err) {
4585 dev_err(rvu->dev,
4586 "Failed to update pcifunc 0x%x to promisc list\n",
4587 pcifunc);
4588 return err;
4589 }
4590 }
4591
4592 /* install/uninstall allmulti entry */
4593 if (allmulti) {
4594 rvu_npc_install_allmulti_entry(rvu, pcifunc, nixlf,
4595 pfvf->rx_chan_base);
4596 } else {
4597 if (!nix_rx_multicast && !is_vf(pcifunc))
4598 rvu_npc_enable_allmulti_entry(rvu, pcifunc, nixlf, false);
4599 }
4600
4601 /* install/uninstall promisc entry */
4602 if (promisc)
4603 rvu_npc_install_promisc_entry(rvu, pcifunc, nixlf,
4604 pfvf->rx_chan_base,
4605 pfvf->rx_chan_cnt);
4606 else
4607 if (!nix_rx_multicast && !is_vf(pcifunc))
4608 rvu_npc_enable_promisc_entry(rvu, pcifunc, nixlf, false);
4609
4610 return 0;
4611 }
4612
4613 static void nix_find_link_frs(struct rvu *rvu,
4614 struct nix_frs_cfg *req, u16 pcifunc)
4615 {
4616 int pf = rvu_get_pf(rvu->pdev, pcifunc);
4617 struct rvu_pfvf *pfvf;
4618 int maxlen, minlen;
4619 int numvfs, hwvf;
4620 int vf;
4621
4622 /* Update with requester's min/max lengths */
4623 pfvf = rvu_get_pfvf(rvu, pcifunc);
4624 pfvf->maxlen = req->maxlen;
4625 if (req->update_minlen)
4626 pfvf->minlen = req->minlen;
4627
4628 maxlen = req->maxlen;
4629 minlen = req->update_minlen ? req->minlen : 0;
4630
4631 /* Get this PF's numVFs and starting hwvf */
4632 rvu_get_pf_numvfs(rvu, pf, &numvfs, &hwvf);
4633
4634 /* For each VF, compare requested max/minlen */
4635 for (vf = 0; vf < numvfs; vf++) {
4636 pfvf = &rvu->hwvf[hwvf + vf];
4637 if (pfvf->maxlen > maxlen)
4638 maxlen = pfvf->maxlen;
4639 if (req->update_minlen &&
4640 pfvf->minlen && pfvf->minlen < minlen)
4641 minlen = pfvf->minlen;
4642 }
4643
4644 /* Compare requested max/minlen with PF's max/minlen */
4645 pfvf = &rvu->pf[pf];
4646 if (pfvf->maxlen > maxlen)
4647 maxlen = pfvf->maxlen;
4648 if (req->update_minlen &&
4649 pfvf->minlen && pfvf->minlen < minlen)
4650 minlen = pfvf->minlen;
4651
4652 /* Update the request with max/min PF's and it's VF's max/min */
4653 req->maxlen = maxlen;
4654 if (req->update_minlen)
4655 req->minlen = minlen;
4656 }
4657
4658 int rvu_mbox_handler_nix_set_hw_frs(struct rvu *rvu, struct nix_frs_cfg *req,
4659 struct msg_rsp *rsp)
4660 {
4661 struct rvu_hwinfo *hw = rvu->hw;
4662 u16 pcifunc = req->hdr.pcifunc;
4663 int pf = rvu_get_pf(rvu->pdev, pcifunc);
4664 int blkaddr, link = -1;
4665 struct nix_hw *nix_hw;
4666 struct rvu_pfvf *pfvf;
4667 u8 cgx = 0, lmac = 0;
4668 u16 max_mtu;
4669 u64 cfg;
4670
4671 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NIX, pcifunc);
4672 if (blkaddr < 0)
4673 return NIX_AF_ERR_AF_LF_INVALID;
4674
4675 nix_hw = get_nix_hw(rvu->hw, blkaddr);
4676 if (!nix_hw)
4677 return NIX_AF_ERR_INVALID_NIXBLK;
4678
4679 if (is_lbk_vf(rvu, pcifunc) || is_rep_dev(rvu, pcifunc))
4680 rvu_get_lbk_link_max_frs(rvu, &max_mtu);
4681 else
4682 rvu_get_lmac_link_max_frs(rvu, &max_mtu);
4683
4684 if (!req->sdp_link && req->maxlen > max_mtu)
4685 return NIX_AF_ERR_FRS_INVALID;
4686
4687 if (req->update_minlen && req->minlen < NIC_HW_MIN_FRS)
4688 return NIX_AF_ERR_FRS_INVALID;
4689
4690 /* Check if config is for SDP link */
4691 if (req->sdp_link) {
4692 if (!hw->sdp_links)
4693 return NIX_AF_ERR_RX_LINK_INVALID;
4694 link = hw->cgx_links + hw->lbk_links;
4695 goto linkcfg;
4696 }
4697
4698 /* Check if the request is from CGX mapped RVU PF */
4699 if (is_pf_cgxmapped(rvu, pf)) {
4700 /* Get CGX and LMAC to which this PF is mapped and find link */
4701 rvu_get_cgx_lmac_id(rvu->pf2cgxlmac_map[pf], &cgx, &lmac);
4702 link = (cgx * hw->lmac_per_cgx) + lmac;
4703 } else if (pf == 0) {
4704 /* For VFs of PF0 ingress is LBK port, so config LBK link */
4705 pfvf = rvu_get_pfvf(rvu, pcifunc);
4706 link = hw->cgx_links + pfvf->lbkid;
4707 } else if (is_rep_dev(rvu, pcifunc)) {
4708 link = hw->cgx_links + 0;
4709 }
4710
4711 if (link < 0)
4712 return NIX_AF_ERR_RX_LINK_INVALID;
4713
4714 linkcfg:
4715 nix_find_link_frs(rvu, req, pcifunc);
4716
4717 cfg = rvu_read64(rvu, blkaddr, NIX_AF_RX_LINKX_CFG(link));
4718 cfg = (cfg & ~(0xFFFFULL << 16)) | ((u64)req->maxlen << 16);
4719 if (req->update_minlen)
4720 cfg = (cfg & ~0xFFFFULL) | req->minlen;
4721 rvu_write64(rvu, blkaddr, NIX_AF_RX_LINKX_CFG(link), cfg);
4722
4723 return 0;
4724 }
4725
4726 int rvu_mbox_handler_nix_set_rx_cfg(struct rvu *rvu, struct nix_rx_cfg *req,
4727 struct msg_rsp *rsp)
4728 {
4729 int nixlf, blkaddr, err;
4730 u64 cfg;
4731
4732 err = nix_get_nixlf(rvu, req->hdr.pcifunc, &nixlf, &blkaddr);
4733 if (err)
4734 return err;
4735
4736 cfg = rvu_read64(rvu, blkaddr, NIX_AF_LFX_RX_CFG(nixlf));
4737 /* Set the interface configuration */
4738 if (req->len_verify & BIT(0))
4739 cfg |= BIT_ULL(41);
4740 else
4741 cfg &= ~BIT_ULL(41);
4742
4743 if (req->len_verify & BIT(1))
4744 cfg |= BIT_ULL(40);
4745 else
4746 cfg &= ~BIT_ULL(40);
4747
4748 if (req->len_verify & NIX_RX_DROP_RE)
4749 cfg |= BIT_ULL(32);
4750 else
4751 cfg &= ~BIT_ULL(32);
4752
4753 if (req->csum_verify & BIT(0))
4754 cfg |= BIT_ULL(37);
4755 else
4756 cfg &= ~BIT_ULL(37);
4757
4758 rvu_write64(rvu, blkaddr, NIX_AF_LFX_RX_CFG(nixlf), cfg);
4759
4760 return 0;
4761 }
4762
4763 static u64 rvu_get_lbk_link_credits(struct rvu *rvu, u16 lbk_max_frs)
4764 {
4765 return 1600; /* 16 * max LBK datarate = 16 * 100Gbps */
4766 }
4767
4768 static void nix_link_config(struct rvu *rvu, int blkaddr,
4769 struct nix_hw *nix_hw)
4770 {
4771 struct rvu_hwinfo *hw = rvu->hw;
4772 int cgx, lmac_cnt, slink, link;
4773 u16 lbk_max_frs, lmac_max_frs;
4774 unsigned long lmac_bmap;
4775 u64 tx_credits, cfg;
4776 u64 lmac_fifo_len;
4777 int iter;
4778
4779 rvu_get_lbk_link_max_frs(rvu, &lbk_max_frs);
4780 rvu_get_lmac_link_max_frs(rvu, &lmac_max_frs);
4781
4782 /* Set SDP link credit */
4783 rvu_write64(rvu, blkaddr, NIX_AF_SDP_LINK_CREDIT, SDP_LINK_CREDIT);
4784
4785 /* Set default min/max packet lengths allowed on NIX Rx links.
4786 *
4787 * With HW reset minlen value of 60byte, HW will treat ARP pkts
4788 * as undersize and report them to SW as error pkts, hence
4789 * setting it to 40 bytes.
4790 */
4791 for (link = 0; link < hw->cgx_links; link++) {
4792 rvu_write64(rvu, blkaddr, NIX_AF_RX_LINKX_CFG(link),
4793 ((u64)lmac_max_frs << 16) | NIC_HW_MIN_FRS);
4794 }
4795
4796 for (link = hw->cgx_links; link < hw->lbk_links; link++) {
4797 rvu_write64(rvu, blkaddr, NIX_AF_RX_LINKX_CFG(link),
4798 ((u64)lbk_max_frs << 16) | NIC_HW_MIN_FRS);
4799 }
4800 if (hw->sdp_links) {
4801 link = hw->cgx_links + hw->lbk_links;
4802 rvu_write64(rvu, blkaddr, NIX_AF_RX_LINKX_CFG(link),
4803 SDP_HW_MAX_FRS << 16 | SDP_HW_MIN_FRS);
4804 }
4805
4806 /* Get MCS external bypass status for CN10K-B */
4807 if (mcs_get_blkcnt() == 1) {
4808 /* Adjust for 2 credits when external bypass is disabled */
4809 nix_hw->cc_mcs_cnt = is_mcs_bypass(0) ? 0 : 2;
4810 }
4811
4812 /* Set credits for Tx links assuming max packet length allowed.
4813 * This will be reconfigured based on MTU set for PF/VF.
4814 */
4815 for (cgx = 0; cgx < hw->cgx; cgx++) {
4816 lmac_cnt = cgx_get_lmac_cnt(rvu_cgx_pdata(cgx, rvu));
4817 /* Skip when cgx is not available or lmac cnt is zero */
4818 if (lmac_cnt <= 0)
4819 continue;
4820 slink = cgx * hw->lmac_per_cgx;
4821
4822 /* Get LMAC id's from bitmap */
4823 lmac_bmap = cgx_get_lmac_bmap(rvu_cgx_pdata(cgx, rvu));
4824 for_each_set_bit(iter, &lmac_bmap, rvu->hw->lmac_per_cgx) {
4825 lmac_fifo_len = rvu_cgx_get_lmac_fifolen(rvu, cgx, iter);
4826 if (!lmac_fifo_len) {
4827 dev_err(rvu->dev,
4828 "%s: Failed to get CGX/RPM%d:LMAC%d FIFO size\n",
4829 __func__, cgx, iter);
4830 continue;
4831 }
4832 tx_credits = (lmac_fifo_len - lmac_max_frs) / 16;
4833 /* Enable credits and set credit pkt count to max allowed */
4834 cfg = (tx_credits << 12) | (0x1FF << 2) | BIT_ULL(1);
4835 cfg |= FIELD_PREP(NIX_AF_LINKX_MCS_CNT_MASK, nix_hw->cc_mcs_cnt);
4836
4837 link = iter + slink;
4838 nix_hw->tx_credits[link] = tx_credits;
4839 rvu_write64(rvu, blkaddr,
4840 NIX_AF_TX_LINKX_NORM_CREDIT(link), cfg);
4841 }
4842 }
4843
4844 /* Set Tx credits for LBK link */
4845 slink = hw->cgx_links;
4846 for (link = slink; link < (slink + hw->lbk_links); link++) {
4847 tx_credits = rvu_get_lbk_link_credits(rvu, lbk_max_frs);
4848 nix_hw->tx_credits[link] = tx_credits;
4849 /* Enable credits and set credit pkt count to max allowed */
4850 tx_credits = (tx_credits << 12) | (0x1FF << 2) | BIT_ULL(1);
4851 rvu_write64(rvu, blkaddr,
4852 NIX_AF_TX_LINKX_NORM_CREDIT(link), tx_credits);
4853 }
4854 }
4855
4856 static int nix_calibrate_x2p(struct rvu *rvu, int blkaddr)
4857 {
4858 int idx, err;
4859 u64 status;
4860
4861 /* Start X2P bus calibration */
4862 rvu_write64(rvu, blkaddr, NIX_AF_CFG,
4863 rvu_read64(rvu, blkaddr, NIX_AF_CFG) | BIT_ULL(9));
4864 /* Wait for calibration to complete */
4865 err = rvu_poll_reg(rvu, blkaddr,
4866 NIX_AF_STATUS, BIT_ULL(10), false);
4867 if (err) {
4868 dev_err(rvu->dev, "NIX X2P bus calibration failed\n");
4869 return err;
4870 }
4871
4872 status = rvu_read64(rvu, blkaddr, NIX_AF_STATUS);
4873 /* Check if CGX devices are ready */
4874 for (idx = 0; idx < rvu->cgx_cnt_max; idx++) {
4875 /* Skip when cgx port is not available */
4876 if (!rvu_cgx_pdata(idx, rvu) ||
4877 (status & (BIT_ULL(16 + idx))))
4878 continue;
4879 dev_err(rvu->dev,
4880 "CGX%d didn't respond to NIX X2P calibration\n", idx);
4881 err = -EBUSY;
4882 }
4883
4884 /* Check if LBK is ready */
4885 if (!(status & BIT_ULL(19))) {
4886 dev_err(rvu->dev,
4887 "LBK didn't respond to NIX X2P calibration\n");
4888 err = -EBUSY;
4889 }
4890
4891 /* Clear 'calibrate_x2p' bit */
4892 rvu_write64(rvu, blkaddr, NIX_AF_CFG,
4893 rvu_read64(rvu, blkaddr, NIX_AF_CFG) & ~BIT_ULL(9));
4894 if (err || (status & 0x3FFULL))
4895 dev_err(rvu->dev,
4896 "NIX X2P calibration failed, status 0x%llx\n", status);
4897 if (err)
4898 return err;
4899 return 0;
4900 }
4901
4902 static int nix_aq_init(struct rvu *rvu, struct rvu_block *block)
4903 {
4904 u64 cfg;
4905 int err;
4906
4907 /* Set admin queue endianness */
4908 cfg = rvu_read64(rvu, block->addr, NIX_AF_CFG);
4909 #ifdef __BIG_ENDIAN
4910 cfg |= BIT_ULL(8);
4911 rvu_write64(rvu, block->addr, NIX_AF_CFG, cfg);
4912 #else
4913 cfg &= ~BIT_ULL(8);
4914 rvu_write64(rvu, block->addr, NIX_AF_CFG, cfg);
4915 #endif
4916
4917 /* Do not bypass NDC cache */
4918 cfg = rvu_read64(rvu, block->addr, NIX_AF_NDC_CFG);
4919 cfg &= ~0x3FFEULL;
4920 #ifdef CONFIG_NDC_DIS_DYNAMIC_CACHING
4921 /* Disable caching of SQB aka SQEs */
4922 cfg |= 0x04ULL;
4923 #endif
4924 rvu_write64(rvu, block->addr, NIX_AF_NDC_CFG, cfg);
4925
4926 /* Result structure can be followed by RQ/SQ/CQ context at
4927 * RES + 128bytes and a write mask at RES + 256 bytes, depending on
4928 * operation type. Alloc sufficient result memory for all operations.
4929 */
4930 err = rvu_aq_alloc(rvu, &block->aq,
4931 Q_COUNT(AQ_SIZE), sizeof(struct nix_aq_inst_s),
4932 ALIGN(sizeof(struct nix_aq_res_s), 128) + 256);
4933 if (err)
4934 return err;
4935
4936 rvu_write64(rvu, block->addr, NIX_AF_AQ_CFG, AQ_SIZE);
4937 rvu_write64(rvu, block->addr,
4938 NIX_AF_AQ_BASE, (u64)block->aq->inst->iova);
4939 return 0;
4940 }
4941
4942 static void rvu_nix_setup_capabilities(struct rvu *rvu, int blkaddr)
4943 {
4944 struct rvu_hwinfo *hw = rvu->hw;
4945 u64 hw_const;
4946
4947 hw_const = rvu_read64(rvu, blkaddr, NIX_AF_CONST1);
4948
4949 /* On OcteonTx2 DWRR quantum is directly configured into each of
4950 * the transmit scheduler queues. And PF/VF drivers were free to
4951 * config any value upto 2^24.
4952 * On CN10K, HW is modified, the quantum configuration at scheduler
4953 * queues is in terms of weight. And SW needs to setup a base DWRR MTU
4954 * at NIX_AF_DWRR_RPM_MTU / NIX_AF_DWRR_SDP_MTU. HW will do
4955 * 'DWRR MTU * weight' to get the quantum.
4956 *
4957 * Check if HW uses a common MTU for all DWRR quantum configs.
4958 * On OcteonTx2 this register field is '0'.
4959 */
4960 if ((((hw_const >> 56) & 0x10) == 0x10) && !(hw_const & BIT_ULL(61)))
4961 hw->cap.nix_common_dwrr_mtu = true;
4962
4963 if (hw_const & BIT_ULL(61))
4964 hw->cap.nix_multiple_dwrr_mtu = true;
4965 }
4966
4967 static int rvu_nix_block_init(struct rvu *rvu, struct nix_hw *nix_hw)
4968 {
4969 const struct npc_lt_def_cfg *ltdefs;
4970 struct rvu_hwinfo *hw = rvu->hw;
4971 int blkaddr = nix_hw->blkaddr;
4972 struct rvu_block *block;
4973 int err;
4974 u64 cfg;
4975
4976 block = &hw->block[blkaddr];
4977
4978 if (is_rvu_96xx_B0(rvu)) {
4979 /* As per a HW errata in 96xx A0/B0 silicon, NIX may corrupt
4980 * internal state when conditional clocks are turned off.
4981 * Hence enable them.
4982 */
4983 rvu_write64(rvu, blkaddr, NIX_AF_CFG,
4984 rvu_read64(rvu, blkaddr, NIX_AF_CFG) | 0x40ULL);
4985 }
4986
4987 /* Set chan/link to backpressure TL3 instead of TL2 */
4988 rvu_write64(rvu, blkaddr, NIX_AF_PSE_CHANNEL_LEVEL, 0x01);
4989
4990 /* Disable SQ manager's sticky mode operation (set TM6 = 0, TM11 = 0)
4991 * This sticky mode is known to cause SQ stalls when multiple
4992 * SQs are mapped to same SMQ and transmitting pkts simultaneously.
4993 * NIX PSE may deadlock when there are any sticky to non-sticky
4994 * transmission. Hence disable it (TM5 = 0).
4995 */
4996 cfg = rvu_read64(rvu, blkaddr, NIX_AF_SQM_DBG_CTL_STATUS);
4997 cfg &= ~(BIT_ULL(15) | BIT_ULL(14) | BIT_ULL(23));
4998 /* NIX may drop credits when condition clocks are turned off.
4999 * Hence enable control flow clk (set TM9 = 1).
5000 */
5001 cfg |= BIT_ULL(21);
5002 rvu_write64(rvu, blkaddr, NIX_AF_SQM_DBG_CTL_STATUS, cfg);
5003
5004 ltdefs = rvu->kpu.lt_def;
5005 /* Calibrate X2P bus to check if CGX/LBK links are fine */
5006 err = nix_calibrate_x2p(rvu, blkaddr);
5007 if (err)
5008 return err;
5009
5010 /* Setup capabilities of the NIX block */
5011 rvu_nix_setup_capabilities(rvu, blkaddr);
5012
5013 /* Initialize admin queue */
5014 err = nix_aq_init(rvu, block);
5015 if (err)
5016 return err;
5017
5018 /* Restore CINT timer delay to HW reset values */
5019 rvu_write64(rvu, blkaddr, NIX_AF_CINT_DELAY, 0x0ULL);
5020
5021 cfg = rvu_read64(rvu, blkaddr, NIX_AF_SEB_CFG);
5022
5023 /* For better performance use NDC TX instead of NDC RX for SQ's SQEs" */
5024 cfg |= 1ULL;
5025 if (!is_rvu_otx2(rvu))
5026 cfg |= NIX_PTP_1STEP_EN;
5027
5028 rvu_write64(rvu, blkaddr, NIX_AF_SEB_CFG, cfg);
5029
5030 if (!is_rvu_otx2(rvu))
5031 rvu_nix_block_cn10k_init(rvu, nix_hw);
5032
5033 if (is_block_implemented(hw, blkaddr)) {
5034 err = nix_setup_txschq(rvu, nix_hw, blkaddr);
5035 if (err)
5036 return err;
5037
5038 err = nix_setup_ipolicers(rvu, nix_hw, blkaddr);
5039 if (err)
5040 return err;
5041
5042 err = nix_af_mark_format_setup(rvu, nix_hw, blkaddr);
5043 if (err)
5044 return err;
5045
5046 err = nix_setup_mcast(rvu, nix_hw, blkaddr);
5047 if (err)
5048 return err;
5049
5050 err = nix_setup_txvlan(rvu, nix_hw);
5051 if (err)
5052 return err;
5053
5054 err = nix_setup_bpids(rvu, nix_hw, blkaddr);
5055 if (err)
5056 return err;
5057
5058 /* Configure segmentation offload formats */
5059 nix_setup_lso(rvu, nix_hw, blkaddr);
5060
5061 /* Config Outer/Inner L2, IP, TCP, UDP and SCTP NPC layer info.
5062 * This helps HW protocol checker to identify headers
5063 * and validate length and checksums.
5064 */
5065 rvu_write64(rvu, blkaddr, NIX_AF_RX_DEF_OL2,
5066 (ltdefs->rx_ol2.lid << 8) | (ltdefs->rx_ol2.ltype_match << 4) |
5067 ltdefs->rx_ol2.ltype_mask);
5068 rvu_write64(rvu, blkaddr, NIX_AF_RX_DEF_OIP4,
5069 (ltdefs->rx_oip4.lid << 8) | (ltdefs->rx_oip4.ltype_match << 4) |
5070 ltdefs->rx_oip4.ltype_mask);
5071 rvu_write64(rvu, blkaddr, NIX_AF_RX_DEF_IIP4,
5072 (ltdefs->rx_iip4.lid << 8) | (ltdefs->rx_iip4.ltype_match << 4) |
5073 ltdefs->rx_iip4.ltype_mask);
5074 rvu_write64(rvu, blkaddr, NIX_AF_RX_DEF_OIP6,
5075 (ltdefs->rx_oip6.lid << 8) | (ltdefs->rx_oip6.ltype_match << 4) |
5076 ltdefs->rx_oip6.ltype_mask);
5077 rvu_write64(rvu, blkaddr, NIX_AF_RX_DEF_IIP6,
5078 (ltdefs->rx_iip6.lid << 8) | (ltdefs->rx_iip6.ltype_match << 4) |
5079 ltdefs->rx_iip6.ltype_mask);
5080 rvu_write64(rvu, blkaddr, NIX_AF_RX_DEF_OTCP,
5081 (ltdefs->rx_otcp.lid << 8) | (ltdefs->rx_otcp.ltype_match << 4) |
5082 ltdefs->rx_otcp.ltype_mask);
5083 rvu_write64(rvu, blkaddr, NIX_AF_RX_DEF_ITCP,
5084 (ltdefs->rx_itcp.lid << 8) | (ltdefs->rx_itcp.ltype_match << 4) |
5085 ltdefs->rx_itcp.ltype_mask);
5086 rvu_write64(rvu, blkaddr, NIX_AF_RX_DEF_OUDP,
5087 (ltdefs->rx_oudp.lid << 8) | (ltdefs->rx_oudp.ltype_match << 4) |
5088 ltdefs->rx_oudp.ltype_mask);
5089 rvu_write64(rvu, blkaddr, NIX_AF_RX_DEF_IUDP,
5090 (ltdefs->rx_iudp.lid << 8) | (ltdefs->rx_iudp.ltype_match << 4) |
5091 ltdefs->rx_iudp.ltype_mask);
5092 rvu_write64(rvu, blkaddr, NIX_AF_RX_DEF_OSCTP,
5093 (ltdefs->rx_osctp.lid << 8) | (ltdefs->rx_osctp.ltype_match << 4) |
5094 ltdefs->rx_osctp.ltype_mask);
5095 rvu_write64(rvu, blkaddr, NIX_AF_RX_DEF_ISCTP,
5096 (ltdefs->rx_isctp.lid << 8) | (ltdefs->rx_isctp.ltype_match << 4) |
5097 ltdefs->rx_isctp.ltype_mask);
5098
5099 if (!is_rvu_otx2(rvu)) {
5100 /* Enable APAD calculation for other protocols
5101 * matching APAD0 and APAD1 lt def registers.
5102 */
5103 rvu_write64(rvu, blkaddr, NIX_AF_RX_DEF_CST_APAD0,
5104 (ltdefs->rx_apad0.valid << 11) |
5105 (ltdefs->rx_apad0.lid << 8) |
5106 (ltdefs->rx_apad0.ltype_match << 4) |
5107 ltdefs->rx_apad0.ltype_mask);
5108 rvu_write64(rvu, blkaddr, NIX_AF_RX_DEF_CST_APAD1,
5109 (ltdefs->rx_apad1.valid << 11) |
5110 (ltdefs->rx_apad1.lid << 8) |
5111 (ltdefs->rx_apad1.ltype_match << 4) |
5112 ltdefs->rx_apad1.ltype_mask);
5113
5114 /* Receive ethertype definition register defines layer
5115 * information in NPC_RESULT_S to identify the Ethertype
5116 * location in L2 header. Used for Ethertype overwriting
5117 * in inline IPsec flow.
5118 */
5119 rvu_write64(rvu, blkaddr, NIX_AF_RX_DEF_ET(0),
5120 (ltdefs->rx_et[0].offset << 12) |
5121 (ltdefs->rx_et[0].valid << 11) |
5122 (ltdefs->rx_et[0].lid << 8) |
5123 (ltdefs->rx_et[0].ltype_match << 4) |
5124 ltdefs->rx_et[0].ltype_mask);
5125 rvu_write64(rvu, blkaddr, NIX_AF_RX_DEF_ET(1),
5126 (ltdefs->rx_et[1].offset << 12) |
5127 (ltdefs->rx_et[1].valid << 11) |
5128 (ltdefs->rx_et[1].lid << 8) |
5129 (ltdefs->rx_et[1].ltype_match << 4) |
5130 ltdefs->rx_et[1].ltype_mask);
5131 }
5132
5133 err = nix_rx_flowkey_alg_cfg(rvu, blkaddr);
5134 if (err)
5135 return err;
5136
5137 nix_hw->tx_credits = kcalloc(hw->cgx_links + hw->lbk_links,
5138 sizeof(u64), GFP_KERNEL);
5139 if (!nix_hw->tx_credits)
5140 return -ENOMEM;
5141
5142 /* Initialize CGX/LBK/SDP link credits, min/max pkt lengths */
5143 nix_link_config(rvu, blkaddr, nix_hw);
5144
5145 /* Enable Channel backpressure */
5146 rvu_write64(rvu, blkaddr, NIX_AF_RX_CFG, BIT_ULL(0));
5147 }
5148 return 0;
5149 }
5150
5151 int rvu_nix_init(struct rvu *rvu)
5152 {
5153 struct rvu_hwinfo *hw = rvu->hw;
5154 struct nix_hw *nix_hw;
5155 int blkaddr = 0, err;
5156 int i = 0;
5157
5158 hw->nix = devm_kcalloc(rvu->dev, MAX_NIX_BLKS, sizeof(struct nix_hw),
5159 GFP_KERNEL);
5160 if (!hw->nix)
5161 return -ENOMEM;
5162
5163 blkaddr = rvu_get_next_nix_blkaddr(rvu, blkaddr);
5164 while (blkaddr) {
5165 nix_hw = &hw->nix[i];
5166 nix_hw->rvu = rvu;
5167 nix_hw->blkaddr = blkaddr;
5168 err = rvu_nix_block_init(rvu, nix_hw);
5169 if (err)
5170 return err;
5171 blkaddr = rvu_get_next_nix_blkaddr(rvu, blkaddr);
5172 i++;
5173 }
5174
5175 return 0;
5176 }
5177
5178 static void rvu_nix_block_freemem(struct rvu *rvu, int blkaddr,
5179 struct rvu_block *block)
5180 {
5181 struct nix_txsch *txsch;
5182 struct nix_mcast *mcast;
5183 struct nix_txvlan *vlan;
5184 struct nix_hw *nix_hw;
5185 int lvl;
5186
5187 rvu_aq_free(rvu, block->aq);
5188
5189 if (is_block_implemented(rvu->hw, blkaddr)) {
5190 nix_hw = get_nix_hw(rvu->hw, blkaddr);
5191 if (!nix_hw)
5192 return;
5193
5194 for (lvl = 0; lvl < NIX_TXSCH_LVL_CNT; lvl++) {
5195 txsch = &nix_hw->txsch[lvl];
5196 kfree(txsch->schq.bmap);
5197 }
5198
5199 kfree(nix_hw->tx_credits);
5200
5201 nix_ipolicer_freemem(rvu, nix_hw);
5202
5203 vlan = &nix_hw->txvlan;
5204 kfree(vlan->rsrc.bmap);
5205 mutex_destroy(&vlan->rsrc_lock);
5206
5207 mcast = &nix_hw->mcast;
5208 qmem_free(rvu->dev, mcast->mce_ctx);
5209 qmem_free(rvu->dev, mcast->mcast_buf);
5210 mutex_destroy(&mcast->mce_lock);
5211 }
5212 }
5213
5214 void rvu_nix_freemem(struct rvu *rvu)
5215 {
5216 struct rvu_hwinfo *hw = rvu->hw;
5217 struct rvu_block *block;
5218 int blkaddr = 0;
5219
5220 blkaddr = rvu_get_next_nix_blkaddr(rvu, blkaddr);
5221 while (blkaddr) {
5222 block = &hw->block[blkaddr];
5223 rvu_nix_block_freemem(rvu, blkaddr, block);
5224 blkaddr = rvu_get_next_nix_blkaddr(rvu, blkaddr);
5225 }
5226 }
5227
5228 static void nix_mcast_update_action(struct rvu *rvu,
5229 struct nix_mcast_grp_elem *elem)
5230 {
5231 struct npc_mcam *mcam = &rvu->hw->mcam;
5232 struct nix_rx_action rx_action = { 0 };
5233 struct nix_tx_action tx_action = { 0 };
5234 int npc_blkaddr;
5235
5236 npc_blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
5237 if (elem->dir == NIX_MCAST_INGRESS) {
5238 *(u64 *)&rx_action = npc_get_mcam_action(rvu, mcam,
5239 npc_blkaddr,
5240 elem->mcam_index);
5241 rx_action.index = elem->mce_start_index;
5242 npc_set_mcam_action(rvu, mcam, npc_blkaddr, elem->mcam_index,
5243 *(u64 *)&rx_action);
5244 } else {
5245 *(u64 *)&tx_action = npc_get_mcam_action(rvu, mcam,
5246 npc_blkaddr,
5247 elem->mcam_index);
5248 tx_action.index = elem->mce_start_index;
5249 npc_set_mcam_action(rvu, mcam, npc_blkaddr, elem->mcam_index,
5250 *(u64 *)&tx_action);
5251 }
5252 }
5253
5254 static void nix_mcast_update_mce_entry(struct rvu *rvu, u16 pcifunc, u8 is_active)
5255 {
5256 struct nix_mcast_grp_elem *elem;
5257 struct nix_mcast_grp *mcast_grp;
5258 struct nix_hw *nix_hw;
5259 int blkaddr;
5260
5261 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NIX, pcifunc);
5262 nix_hw = get_nix_hw(rvu->hw, blkaddr);
5263 if (!nix_hw)
5264 return;
5265
5266 mcast_grp = &nix_hw->mcast_grp;
5267
5268 mutex_lock(&mcast_grp->mcast_grp_lock);
5269 list_for_each_entry(elem, &mcast_grp->mcast_grp_head, list) {
5270 struct nix_mce_list *mce_list;
5271 struct mce *mce;
5272
5273 /* Iterate the group elements and disable the element which
5274 * received the disable request.
5275 */
5276 mce_list = &elem->mcast_mce_list;
5277 hlist_for_each_entry(mce, &mce_list->head, node) {
5278 if (mce->pcifunc == pcifunc) {
5279 mce->is_active = is_active;
5280 break;
5281 }
5282 }
5283
5284 /* Dump the updated list to HW */
5285 if (elem->dir == NIX_MCAST_INGRESS)
5286 nix_update_ingress_mce_list_hw(rvu, nix_hw, elem);
5287 else
5288 nix_update_egress_mce_list_hw(rvu, nix_hw, elem);
5289
5290 /* Update the multicast index in NPC rule */
5291 nix_mcast_update_action(rvu, elem);
5292 }
5293 mutex_unlock(&mcast_grp->mcast_grp_lock);
5294 }
5295
5296 int rvu_mbox_handler_nix_lf_start_rx(struct rvu *rvu, struct msg_req *req,
5297 struct msg_rsp *rsp)
5298 {
5299 u16 pcifunc = req->hdr.pcifunc;
5300 struct rvu_pfvf *pfvf;
5301 int nixlf, err, pf;
5302
5303 err = nix_get_nixlf(rvu, pcifunc, &nixlf, NULL);
5304 if (err)
5305 return err;
5306
5307 /* Enable the interface if it is in any multicast list */
5308 nix_mcast_update_mce_entry(rvu, pcifunc, 1);
5309
5310 rvu_npc_enable_default_entries(rvu, pcifunc, nixlf);
5311
5312 npc_mcam_enable_flows(rvu, pcifunc);
5313
5314 pfvf = rvu_get_pfvf(rvu, pcifunc);
5315 set_bit(NIXLF_INITIALIZED, &pfvf->flags);
5316
5317 rvu_switch_update_rules(rvu, pcifunc, true);
5318
5319 pf = rvu_get_pf(rvu->pdev, pcifunc);
5320 if (is_pf_cgxmapped(rvu, pf) && rvu->rep_mode)
5321 rvu_rep_notify_pfvf_state(rvu, pcifunc, true);
5322
5323 return rvu_cgx_start_stop_io(rvu, pcifunc, true);
5324 }
5325
5326 int rvu_mbox_handler_nix_lf_stop_rx(struct rvu *rvu, struct msg_req *req,
5327 struct msg_rsp *rsp)
5328 {
5329 u16 pcifunc = req->hdr.pcifunc;
5330 struct rvu_pfvf *pfvf;
5331 int nixlf, err, pf;
5332
5333 err = nix_get_nixlf(rvu, pcifunc, &nixlf, NULL);
5334 if (err)
5335 return err;
5336
5337 rvu_npc_disable_mcam_entries(rvu, pcifunc, nixlf);
5338 /* Disable the interface if it is in any multicast list */
5339 nix_mcast_update_mce_entry(rvu, pcifunc, 0);
5340
5341 pfvf = rvu_get_pfvf(rvu, pcifunc);
5342 clear_bit(NIXLF_INITIALIZED, &pfvf->flags);
5343
5344 err = rvu_cgx_start_stop_io(rvu, pcifunc, false);
5345 if (err)
5346 return err;
5347
5348 rvu_switch_update_rules(rvu, pcifunc, false);
5349 rvu_cgx_tx_enable(rvu, pcifunc, true);
5350
5351 pf = rvu_get_pf(rvu->pdev, pcifunc);
5352 if (is_pf_cgxmapped(rvu, pf) && rvu->rep_mode)
5353 rvu_rep_notify_pfvf_state(rvu, pcifunc, false);
5354 return 0;
5355 }
5356
5357 #define RX_SA_BASE GENMASK_ULL(52, 7)
5358
5359 void rvu_nix_lf_teardown(struct rvu *rvu, u16 pcifunc, int blkaddr, int nixlf)
5360 {
5361 struct rvu_pfvf *pfvf = rvu_get_pfvf(rvu, pcifunc);
5362 struct hwctx_disable_req ctx_req;
5363 int pf = rvu_get_pf(rvu->pdev, pcifunc);
5364 struct mac_ops *mac_ops;
5365 u8 cgx_id, lmac_id;
5366 u64 sa_base;
5367 void *cgxd;
5368 int err;
5369
5370 ctx_req.hdr.pcifunc = pcifunc;
5371
5372 /* Cleanup NPC MCAM entries, free Tx scheduler queues being used */
5373 rvu_npc_disable_mcam_entries(rvu, pcifunc, nixlf);
5374 rvu_npc_free_mcam_entries(rvu, pcifunc, nixlf);
5375 nix_interface_deinit(rvu, pcifunc, nixlf);
5376 nix_rx_sync(rvu, blkaddr);
5377 nix_txschq_free(rvu, pcifunc);
5378
5379 clear_bit(NIXLF_INITIALIZED, &pfvf->flags);
5380
5381 if (is_pf_cgxmapped(rvu, pf) && rvu->rep_mode)
5382 rvu_rep_notify_pfvf_state(rvu, pcifunc, false);
5383
5384 rvu_cgx_start_stop_io(rvu, pcifunc, false);
5385
5386 if (pfvf->sq_ctx) {
5387 ctx_req.ctype = NIX_AQ_CTYPE_SQ;
5388 err = nix_lf_hwctx_disable(rvu, &ctx_req);
5389 if (err)
5390 dev_err(rvu->dev, "SQ ctx disable failed\n");
5391 }
5392
5393 if (pfvf->rq_ctx) {
5394 ctx_req.ctype = NIX_AQ_CTYPE_RQ;
5395 err = nix_lf_hwctx_disable(rvu, &ctx_req);
5396 if (err)
5397 dev_err(rvu->dev, "RQ ctx disable failed\n");
5398 }
5399
5400 if (pfvf->cq_ctx) {
5401 ctx_req.ctype = NIX_AQ_CTYPE_CQ;
5402 err = nix_lf_hwctx_disable(rvu, &ctx_req);
5403 if (err)
5404 dev_err(rvu->dev, "CQ ctx disable failed\n");
5405 }
5406
5407 /* reset HW config done for Switch headers */
5408 rvu_npc_set_parse_mode(rvu, pcifunc, OTX2_PRIV_FLAGS_DEFAULT,
5409 (PKIND_TX | PKIND_RX), 0, 0, 0, 0);
5410
5411 /* Disabling CGX and NPC config done for PTP */
5412 if (pfvf->hw_rx_tstamp_en) {
5413 rvu_get_cgx_lmac_id(rvu->pf2cgxlmac_map[pf], &cgx_id, &lmac_id);
5414 cgxd = rvu_cgx_pdata(cgx_id, rvu);
5415 mac_ops = get_mac_ops(cgxd);
5416 mac_ops->mac_enadis_ptp_config(cgxd, lmac_id, false);
5417 /* Undo NPC config done for PTP */
5418 if (npc_config_ts_kpuaction(rvu, pf, pcifunc, false))
5419 dev_err(rvu->dev, "NPC config for PTP failed\n");
5420 pfvf->hw_rx_tstamp_en = false;
5421 }
5422
5423 /* reset priority flow control config */
5424 rvu_cgx_prio_flow_ctrl_cfg(rvu, pcifunc, 0, 0, 0);
5425
5426 /* reset 802.3x flow control config */
5427 rvu_cgx_cfg_pause_frm(rvu, pcifunc, 0, 0);
5428
5429 nix_ctx_free(rvu, pfvf);
5430
5431 nix_free_all_bandprof(rvu, pcifunc);
5432
5433 sa_base = rvu_read64(rvu, blkaddr, NIX_AF_LFX_RX_IPSEC_SA_BASE(nixlf));
5434 if (FIELD_GET(RX_SA_BASE, sa_base)) {
5435 err = rvu_cpt_ctx_flush(rvu, pcifunc);
5436 if (err)
5437 dev_err(rvu->dev,
5438 "CPT ctx flush failed with error: %d\n", err);
5439 }
5440 }
5441
5442 #define NIX_AF_LFX_TX_CFG_PTP_EN BIT_ULL(32)
5443
5444 static int rvu_nix_lf_ptp_tx_cfg(struct rvu *rvu, u16 pcifunc, bool enable)
5445 {
5446 struct rvu_hwinfo *hw = rvu->hw;
5447 struct rvu_block *block;
5448 int blkaddr, pf;
5449 int nixlf;
5450 u64 cfg;
5451
5452 pf = rvu_get_pf(rvu->pdev, pcifunc);
5453 if (!is_mac_feature_supported(rvu, pf, RVU_LMAC_FEAT_PTP))
5454 return 0;
5455
5456 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NIX, pcifunc);
5457 if (blkaddr < 0)
5458 return NIX_AF_ERR_AF_LF_INVALID;
5459
5460 block = &hw->block[blkaddr];
5461 nixlf = rvu_get_lf(rvu, block, pcifunc, 0);
5462 if (nixlf < 0)
5463 return NIX_AF_ERR_AF_LF_INVALID;
5464
5465 cfg = rvu_read64(rvu, blkaddr, NIX_AF_LFX_TX_CFG(nixlf));
5466
5467 if (enable)
5468 cfg |= NIX_AF_LFX_TX_CFG_PTP_EN;
5469 else
5470 cfg &= ~NIX_AF_LFX_TX_CFG_PTP_EN;
5471
5472 rvu_write64(rvu, blkaddr, NIX_AF_LFX_TX_CFG(nixlf), cfg);
5473
5474 return 0;
5475 }
5476
5477 int rvu_mbox_handler_nix_lf_ptp_tx_enable(struct rvu *rvu, struct msg_req *req,
5478 struct msg_rsp *rsp)
5479 {
5480 return rvu_nix_lf_ptp_tx_cfg(rvu, req->hdr.pcifunc, true);
5481 }
5482
5483 int rvu_mbox_handler_nix_lf_ptp_tx_disable(struct rvu *rvu, struct msg_req *req,
5484 struct msg_rsp *rsp)
5485 {
5486 return rvu_nix_lf_ptp_tx_cfg(rvu, req->hdr.pcifunc, false);
5487 }
5488
5489 int rvu_mbox_handler_nix_lso_format_cfg(struct rvu *rvu,
5490 struct nix_lso_format_cfg *req,
5491 struct nix_lso_format_cfg_rsp *rsp)
5492 {
5493 u16 pcifunc = req->hdr.pcifunc;
5494 struct nix_hw *nix_hw;
5495 struct rvu_pfvf *pfvf;
5496 int blkaddr, idx, f;
5497 u64 reg;
5498
5499 pfvf = rvu_get_pfvf(rvu, pcifunc);
5500 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NIX, pcifunc);
5501 if (!pfvf->nixlf || blkaddr < 0)
5502 return NIX_AF_ERR_AF_LF_INVALID;
5503
5504 nix_hw = get_nix_hw(rvu->hw, blkaddr);
5505 if (!nix_hw)
5506 return NIX_AF_ERR_INVALID_NIXBLK;
5507
5508 /* Find existing matching LSO format, if any */
5509 for (idx = 0; idx < nix_hw->lso.in_use; idx++) {
5510 for (f = 0; f < NIX_LSO_FIELD_MAX; f++) {
5511 reg = rvu_read64(rvu, blkaddr,
5512 NIX_AF_LSO_FORMATX_FIELDX(idx, f));
5513 if (req->fields[f] != (reg & req->field_mask))
5514 break;
5515 }
5516
5517 if (f == NIX_LSO_FIELD_MAX)
5518 break;
5519 }
5520
5521 if (idx < nix_hw->lso.in_use) {
5522 /* Match found */
5523 rsp->lso_format_idx = idx;
5524 return 0;
5525 }
5526
5527 if (nix_hw->lso.in_use == nix_hw->lso.total)
5528 return NIX_AF_ERR_LSO_CFG_FAIL;
5529
5530 rsp->lso_format_idx = nix_hw->lso.in_use++;
5531
5532 for (f = 0; f < NIX_LSO_FIELD_MAX; f++)
5533 rvu_write64(rvu, blkaddr,
5534 NIX_AF_LSO_FORMATX_FIELDX(rsp->lso_format_idx, f),
5535 req->fields[f]);
5536
5537 return 0;
5538 }
5539
5540 #define IPSEC_GEN_CFG_EGRP GENMASK_ULL(50, 48)
5541 #define IPSEC_GEN_CFG_OPCODE GENMASK_ULL(47, 32)
5542 #define IPSEC_GEN_CFG_PARAM1 GENMASK_ULL(31, 16)
5543 #define IPSEC_GEN_CFG_PARAM2 GENMASK_ULL(15, 0)
5544
5545 #define CPT_INST_QSEL_BLOCK GENMASK_ULL(28, 24)
5546 #define CPT_INST_QSEL_PF_FUNC GENMASK_ULL(23, 8)
5547 #define CPT_INST_QSEL_SLOT GENMASK_ULL(7, 0)
5548
5549 #define CPT_INST_CREDIT_TH GENMASK_ULL(53, 32)
5550 #define CPT_INST_CREDIT_BPID GENMASK_ULL(30, 22)
5551 #define CPT_INST_CREDIT_CNT GENMASK_ULL(21, 0)
5552
5553 static void nix_inline_ipsec_cfg(struct rvu *rvu, struct nix_inline_ipsec_cfg *req,
5554 int blkaddr)
5555 {
5556 u8 cpt_idx, cpt_blkaddr;
5557 u64 val;
5558
5559 cpt_idx = (blkaddr == BLKADDR_NIX0) ? 0 : 1;
5560 if (req->enable) {
5561 val = 0;
5562 /* Enable context prefetching */
5563 if (!is_rvu_otx2(rvu))
5564 val |= BIT_ULL(51);
5565
5566 /* Set OPCODE and EGRP */
5567 val |= FIELD_PREP(IPSEC_GEN_CFG_EGRP, req->gen_cfg.egrp);
5568 val |= FIELD_PREP(IPSEC_GEN_CFG_OPCODE, req->gen_cfg.opcode);
5569 val |= FIELD_PREP(IPSEC_GEN_CFG_PARAM1, req->gen_cfg.param1);
5570 val |= FIELD_PREP(IPSEC_GEN_CFG_PARAM2, req->gen_cfg.param2);
5571
5572 rvu_write64(rvu, blkaddr, NIX_AF_RX_IPSEC_GEN_CFG, val);
5573
5574 /* Set CPT queue for inline IPSec */
5575 val = FIELD_PREP(CPT_INST_QSEL_SLOT, req->inst_qsel.cpt_slot);
5576 val |= FIELD_PREP(CPT_INST_QSEL_PF_FUNC,
5577 req->inst_qsel.cpt_pf_func);
5578
5579 if (!is_rvu_otx2(rvu)) {
5580 cpt_blkaddr = (cpt_idx == 0) ? BLKADDR_CPT0 :
5581 BLKADDR_CPT1;
5582 val |= FIELD_PREP(CPT_INST_QSEL_BLOCK, cpt_blkaddr);
5583 }
5584
5585 rvu_write64(rvu, blkaddr, NIX_AF_RX_CPTX_INST_QSEL(cpt_idx),
5586 val);
5587
5588 /* Set CPT credit */
5589 val = rvu_read64(rvu, blkaddr, NIX_AF_RX_CPTX_CREDIT(cpt_idx));
5590 if ((val & 0x3FFFFF) != 0x3FFFFF)
5591 rvu_write64(rvu, blkaddr, NIX_AF_RX_CPTX_CREDIT(cpt_idx),
5592 0x3FFFFF - val);
5593
5594 val = FIELD_PREP(CPT_INST_CREDIT_CNT, req->cpt_credit);
5595 val |= FIELD_PREP(CPT_INST_CREDIT_BPID, req->bpid);
5596 val |= FIELD_PREP(CPT_INST_CREDIT_TH, req->credit_th);
5597 rvu_write64(rvu, blkaddr, NIX_AF_RX_CPTX_CREDIT(cpt_idx), val);
5598 } else {
5599 rvu_write64(rvu, blkaddr, NIX_AF_RX_IPSEC_GEN_CFG, 0x0);
5600 rvu_write64(rvu, blkaddr, NIX_AF_RX_CPTX_INST_QSEL(cpt_idx),
5601 0x0);
5602 val = rvu_read64(rvu, blkaddr, NIX_AF_RX_CPTX_CREDIT(cpt_idx));
5603 if ((val & 0x3FFFFF) != 0x3FFFFF)
5604 rvu_write64(rvu, blkaddr, NIX_AF_RX_CPTX_CREDIT(cpt_idx),
5605 0x3FFFFF - val);
5606 }
5607 }
5608
5609 int rvu_mbox_handler_nix_inline_ipsec_cfg(struct rvu *rvu,
5610 struct nix_inline_ipsec_cfg *req,
5611 struct msg_rsp *rsp)
5612 {
5613 if (!is_block_implemented(rvu->hw, BLKADDR_CPT0))
5614 return 0;
5615
5616 nix_inline_ipsec_cfg(rvu, req, BLKADDR_NIX0);
5617 if (is_block_implemented(rvu->hw, BLKADDR_CPT1))
5618 nix_inline_ipsec_cfg(rvu, req, BLKADDR_NIX1);
5619
5620 return 0;
5621 }
5622
5623 int rvu_mbox_handler_nix_read_inline_ipsec_cfg(struct rvu *rvu,
5624 struct msg_req *req,
5625 struct nix_inline_ipsec_cfg *rsp)
5626
5627 {
5628 u64 val;
5629
5630 if (!is_block_implemented(rvu->hw, BLKADDR_CPT0))
5631 return 0;
5632
5633 val = rvu_read64(rvu, BLKADDR_NIX0, NIX_AF_RX_IPSEC_GEN_CFG);
5634 rsp->gen_cfg.egrp = FIELD_GET(IPSEC_GEN_CFG_EGRP, val);
5635 rsp->gen_cfg.opcode = FIELD_GET(IPSEC_GEN_CFG_OPCODE, val);
5636 rsp->gen_cfg.param1 = FIELD_GET(IPSEC_GEN_CFG_PARAM1, val);
5637 rsp->gen_cfg.param2 = FIELD_GET(IPSEC_GEN_CFG_PARAM2, val);
5638
5639 val = rvu_read64(rvu, BLKADDR_NIX0, NIX_AF_RX_CPTX_CREDIT(0));
5640 rsp->cpt_credit = FIELD_GET(CPT_INST_CREDIT_CNT, val);
5641 rsp->credit_th = FIELD_GET(CPT_INST_CREDIT_TH, val);
5642 rsp->bpid = FIELD_GET(CPT_INST_CREDIT_BPID, val);
5643
5644 return 0;
5645 }
5646
5647 int rvu_mbox_handler_nix_inline_ipsec_lf_cfg(struct rvu *rvu,
5648 struct nix_inline_ipsec_lf_cfg *req,
5649 struct msg_rsp *rsp)
5650 {
5651 int lf, blkaddr, err;
5652 u64 val;
5653
5654 if (!is_block_implemented(rvu->hw, BLKADDR_CPT0))
5655 return 0;
5656
5657 err = nix_get_nixlf(rvu, req->hdr.pcifunc, &lf, &blkaddr);
5658 if (err)
5659 return err;
5660
5661 if (req->enable) {
5662 /* Set TT, TAG_CONST, SA_POW2_SIZE and LENM1_MAX */
5663 val = (u64)req->ipsec_cfg0.tt << 44 |
5664 (u64)req->ipsec_cfg0.tag_const << 20 |
5665 (u64)req->ipsec_cfg0.sa_pow2_size << 16 |
5666 req->ipsec_cfg0.lenm1_max;
5667
5668 if (blkaddr == BLKADDR_NIX1)
5669 val |= BIT_ULL(46);
5670
5671 rvu_write64(rvu, blkaddr, NIX_AF_LFX_RX_IPSEC_CFG0(lf), val);
5672
5673 /* Set SA_IDX_W and SA_IDX_MAX */
5674 val = (u64)req->ipsec_cfg1.sa_idx_w << 32 |
5675 req->ipsec_cfg1.sa_idx_max;
5676 rvu_write64(rvu, blkaddr, NIX_AF_LFX_RX_IPSEC_CFG1(lf), val);
5677
5678 /* Set SA base address */
5679 rvu_write64(rvu, blkaddr, NIX_AF_LFX_RX_IPSEC_SA_BASE(lf),
5680 req->sa_base_addr);
5681 } else {
5682 rvu_write64(rvu, blkaddr, NIX_AF_LFX_RX_IPSEC_CFG0(lf), 0x0);
5683 rvu_write64(rvu, blkaddr, NIX_AF_LFX_RX_IPSEC_CFG1(lf), 0x0);
5684 rvu_write64(rvu, blkaddr, NIX_AF_LFX_RX_IPSEC_SA_BASE(lf),
5685 0x0);
5686 }
5687
5688 return 0;
5689 }
5690
5691 void rvu_nix_reset_mac(struct rvu_pfvf *pfvf, int pcifunc)
5692 {
5693 bool from_vf = !!(pcifunc & RVU_PFVF_FUNC_MASK);
5694
5695 /* overwrite vf mac address with default_mac */
5696 if (from_vf)
5697 ether_addr_copy(pfvf->mac_addr, pfvf->default_mac);
5698 }
5699
5700 /* NIX ingress policers or bandwidth profiles APIs */
5701 static void nix_config_rx_pkt_policer_precolor(struct rvu *rvu, int blkaddr)
5702 {
5703 struct npc_lt_def_cfg defs, *ltdefs;
5704
5705 ltdefs = &defs;
5706 memcpy(ltdefs, rvu->kpu.lt_def, sizeof(struct npc_lt_def_cfg));
5707
5708 /* Extract PCP and DEI fields from outer VLAN from byte offset
5709 * 2 from the start of LB_PTR (ie TAG).
5710 * VLAN0 is Outer VLAN and VLAN1 is Inner VLAN. Inner VLAN
5711 * fields are considered when 'Tunnel enable' is set in profile.
5712 */
5713 rvu_write64(rvu, blkaddr, NIX_AF_RX_DEF_VLAN0_PCP_DEI,
5714 (2UL << 12) | (ltdefs->ovlan.lid << 8) |
5715 (ltdefs->ovlan.ltype_match << 4) |
5716 ltdefs->ovlan.ltype_mask);
5717 rvu_write64(rvu, blkaddr, NIX_AF_RX_DEF_VLAN1_PCP_DEI,
5718 (2UL << 12) | (ltdefs->ivlan.lid << 8) |
5719 (ltdefs->ivlan.ltype_match << 4) |
5720 ltdefs->ivlan.ltype_mask);
5721
5722 /* DSCP field in outer and tunneled IPv4 packets */
5723 rvu_write64(rvu, blkaddr, NIX_AF_RX_DEF_OIP4_DSCP,
5724 (1UL << 12) | (ltdefs->rx_oip4.lid << 8) |
5725 (ltdefs->rx_oip4.ltype_match << 4) |
5726 ltdefs->rx_oip4.ltype_mask);
5727 rvu_write64(rvu, blkaddr, NIX_AF_RX_DEF_IIP4_DSCP,
5728 (1UL << 12) | (ltdefs->rx_iip4.lid << 8) |
5729 (ltdefs->rx_iip4.ltype_match << 4) |
5730 ltdefs->rx_iip4.ltype_mask);
5731
5732 /* DSCP field (traffic class) in outer and tunneled IPv6 packets */
5733 rvu_write64(rvu, blkaddr, NIX_AF_RX_DEF_OIP6_DSCP,
5734 (1UL << 11) | (ltdefs->rx_oip6.lid << 8) |
5735 (ltdefs->rx_oip6.ltype_match << 4) |
5736 ltdefs->rx_oip6.ltype_mask);
5737 rvu_write64(rvu, blkaddr, NIX_AF_RX_DEF_IIP6_DSCP,
5738 (1UL << 11) | (ltdefs->rx_iip6.lid << 8) |
5739 (ltdefs->rx_iip6.ltype_match << 4) |
5740 ltdefs->rx_iip6.ltype_mask);
5741 }
5742
5743 static int nix_init_policer_context(struct rvu *rvu, struct nix_hw *nix_hw,
5744 int layer, int prof_idx)
5745 {
5746 struct nix_cn10k_aq_enq_req aq_req;
5747 int rc;
5748
5749 memset(&aq_req, 0, sizeof(struct nix_cn10k_aq_enq_req));
5750
5751 aq_req.qidx = (prof_idx & 0x3FFF) | (layer << 14);
5752 aq_req.ctype = NIX_AQ_CTYPE_BANDPROF;
5753 aq_req.op = NIX_AQ_INSTOP_INIT;
5754
5755 /* Context is all zeros, submit to AQ */
5756 rc = rvu_nix_blk_aq_enq_inst(rvu, nix_hw,
5757 (struct nix_aq_enq_req *)&aq_req, NULL);
5758 if (rc)
5759 dev_err(rvu->dev, "Failed to INIT bandwidth profile layer %d profile %d\n",
5760 layer, prof_idx);
5761 return rc;
5762 }
5763
5764 static int nix_setup_ipolicers(struct rvu *rvu,
5765 struct nix_hw *nix_hw, int blkaddr)
5766 {
5767 struct rvu_hwinfo *hw = rvu->hw;
5768 struct nix_ipolicer *ipolicer;
5769 int err, layer, prof_idx;
5770 u64 cfg;
5771
5772 cfg = rvu_read64(rvu, blkaddr, NIX_AF_CONST);
5773 if (!(cfg & BIT_ULL(61))) {
5774 hw->cap.ipolicer = false;
5775 return 0;
5776 }
5777
5778 hw->cap.ipolicer = true;
5779 nix_hw->ipolicer = devm_kcalloc(rvu->dev, BAND_PROF_NUM_LAYERS,
5780 sizeof(*ipolicer), GFP_KERNEL);
5781 if (!nix_hw->ipolicer)
5782 return -ENOMEM;
5783
5784 cfg = rvu_read64(rvu, blkaddr, NIX_AF_PL_CONST);
5785
5786 for (layer = 0; layer < BAND_PROF_NUM_LAYERS; layer++) {
5787 ipolicer = &nix_hw->ipolicer[layer];
5788 switch (layer) {
5789 case BAND_PROF_LEAF_LAYER:
5790 ipolicer->band_prof.max = cfg & 0XFFFF;
5791 break;
5792 case BAND_PROF_MID_LAYER:
5793 ipolicer->band_prof.max = (cfg >> 16) & 0XFFFF;
5794 break;
5795 case BAND_PROF_TOP_LAYER:
5796 ipolicer->band_prof.max = (cfg >> 32) & 0XFFFF;
5797 break;
5798 }
5799
5800 if (!ipolicer->band_prof.max)
5801 continue;
5802
5803 err = rvu_alloc_bitmap(&ipolicer->band_prof);
5804 if (err)
5805 return err;
5806
5807 ipolicer->pfvf_map = devm_kcalloc(rvu->dev,
5808 ipolicer->band_prof.max,
5809 sizeof(u16), GFP_KERNEL);
5810 if (!ipolicer->pfvf_map)
5811 return -ENOMEM;
5812
5813 ipolicer->match_id = devm_kcalloc(rvu->dev,
5814 ipolicer->band_prof.max,
5815 sizeof(u16), GFP_KERNEL);
5816 if (!ipolicer->match_id)
5817 return -ENOMEM;
5818
5819 for (prof_idx = 0;
5820 prof_idx < ipolicer->band_prof.max; prof_idx++) {
5821 /* Set AF as current owner for INIT ops to succeed */
5822 ipolicer->pfvf_map[prof_idx] = 0x00;
5823
5824 /* There is no enable bit in the profile context,
5825 * so no context disable. So let's INIT them here
5826 * so that PF/VF later on have to just do WRITE to
5827 * setup policer rates and config.
5828 */
5829 err = nix_init_policer_context(rvu, nix_hw,
5830 layer, prof_idx);
5831 if (err)
5832 return err;
5833 }
5834
5835 /* Allocate memory for maintaining ref_counts for MID level
5836 * profiles, this will be needed for leaf layer profiles'
5837 * aggregation.
5838 */
5839 if (layer != BAND_PROF_MID_LAYER)
5840 continue;
5841
5842 ipolicer->ref_count = devm_kcalloc(rvu->dev,
5843 ipolicer->band_prof.max,
5844 sizeof(u16), GFP_KERNEL);
5845 if (!ipolicer->ref_count)
5846 return -ENOMEM;
5847 }
5848
5849 /* Set policer timeunit to 2us ie (19 + 1) * 100 nsec = 2us */
5850 rvu_write64(rvu, blkaddr, NIX_AF_PL_TS, 19);
5851
5852 nix_config_rx_pkt_policer_precolor(rvu, blkaddr);
5853
5854 return 0;
5855 }
5856
5857 static void nix_ipolicer_freemem(struct rvu *rvu, struct nix_hw *nix_hw)
5858 {
5859 struct nix_ipolicer *ipolicer;
5860 int layer;
5861
5862 if (!rvu->hw->cap.ipolicer)
5863 return;
5864
5865 for (layer = 0; layer < BAND_PROF_NUM_LAYERS; layer++) {
5866 ipolicer = &nix_hw->ipolicer[layer];
5867
5868 if (!ipolicer->band_prof.max)
5869 continue;
5870
5871 kfree(ipolicer->band_prof.bmap);
5872 }
5873 }
5874
5875 #define NIX_BW_PROF_HI_MASK GENMASK(10, 7)
5876
5877 static int nix_verify_bandprof(struct nix_cn10k_aq_enq_req *req,
5878 struct nix_hw *nix_hw, u16 pcifunc)
5879 {
5880 struct nix_ipolicer *ipolicer;
5881 int layer, hi_layer, prof_idx;
5882
5883 /* Bits [15:14] in profile index represent layer */
5884 layer = (req->qidx >> 14) & 0x03;
5885 prof_idx = req->qidx & 0x3FFF;
5886
5887 ipolicer = &nix_hw->ipolicer[layer];
5888 if (prof_idx >= ipolicer->band_prof.max)
5889 return -EINVAL;
5890
5891 /* Check if the profile is allocated to the requesting PCIFUNC or not
5892 * with the exception of AF. AF is allowed to read and update contexts.
5893 */
5894 if (pcifunc && ipolicer->pfvf_map[prof_idx] != pcifunc)
5895 return -EINVAL;
5896
5897 /* If this profile is linked to higher layer profile then check
5898 * if that profile is also allocated to the requesting PCIFUNC
5899 * or not.
5900 */
5901 if (!req->prof.hl_en)
5902 return 0;
5903
5904 /* Leaf layer profile can link only to mid layer and
5905 * mid layer to top layer.
5906 */
5907 if (layer == BAND_PROF_LEAF_LAYER)
5908 hi_layer = BAND_PROF_MID_LAYER;
5909 else if (layer == BAND_PROF_MID_LAYER)
5910 hi_layer = BAND_PROF_TOP_LAYER;
5911 else
5912 return -EINVAL;
5913
5914 ipolicer = &nix_hw->ipolicer[hi_layer];
5915 prof_idx = FIELD_PREP(NIX_BW_PROF_HI_MASK, req->prof.band_prof_id_h);
5916 prof_idx |= req->prof.band_prof_id;
5917 if (prof_idx >= ipolicer->band_prof.max ||
5918 ipolicer->pfvf_map[prof_idx] != pcifunc)
5919 return -EINVAL;
5920
5921 return 0;
5922 }
5923
5924 int rvu_mbox_handler_nix_bandprof_alloc(struct rvu *rvu,
5925 struct nix_bandprof_alloc_req *req,
5926 struct nix_bandprof_alloc_rsp *rsp)
5927 {
5928 int blkaddr, layer, prof, idx, err;
5929 u16 pcifunc = req->hdr.pcifunc;
5930 struct nix_ipolicer *ipolicer;
5931 struct nix_hw *nix_hw;
5932
5933 if (!rvu->hw->cap.ipolicer)
5934 return NIX_AF_ERR_IPOLICER_NOTSUPP;
5935
5936 err = nix_get_struct_ptrs(rvu, pcifunc, &nix_hw, &blkaddr);
5937 if (err)
5938 return err;
5939
5940 mutex_lock(&rvu->rsrc_lock);
5941 for (layer = 0; layer < BAND_PROF_NUM_LAYERS; layer++) {
5942 if (layer == BAND_PROF_INVAL_LAYER)
5943 continue;
5944 if (!req->prof_count[layer])
5945 continue;
5946
5947 ipolicer = &nix_hw->ipolicer[layer];
5948 for (idx = 0; idx < req->prof_count[layer]; idx++) {
5949 /* Allocate a max of 'MAX_BANDPROF_PER_PFFUNC' profiles */
5950 if (idx == MAX_BANDPROF_PER_PFFUNC)
5951 break;
5952
5953 prof = rvu_alloc_rsrc(&ipolicer->band_prof);
5954 if (prof < 0)
5955 break;
5956 rsp->prof_count[layer]++;
5957 rsp->prof_idx[layer][idx] = prof;
5958 ipolicer->pfvf_map[prof] = pcifunc;
5959 }
5960 }
5961 mutex_unlock(&rvu->rsrc_lock);
5962 return 0;
5963 }
5964
5965 static int nix_free_all_bandprof(struct rvu *rvu, u16 pcifunc)
5966 {
5967 int blkaddr, layer, prof_idx, err;
5968 struct nix_ipolicer *ipolicer;
5969 struct nix_hw *nix_hw;
5970
5971 if (!rvu->hw->cap.ipolicer)
5972 return NIX_AF_ERR_IPOLICER_NOTSUPP;
5973
5974 err = nix_get_struct_ptrs(rvu, pcifunc, &nix_hw, &blkaddr);
5975 if (err)
5976 return err;
5977
5978 mutex_lock(&rvu->rsrc_lock);
5979 /* Free all the profiles allocated to the PCIFUNC */
5980 for (layer = 0; layer < BAND_PROF_NUM_LAYERS; layer++) {
5981 if (layer == BAND_PROF_INVAL_LAYER)
5982 continue;
5983 ipolicer = &nix_hw->ipolicer[layer];
5984
5985 for (prof_idx = 0; prof_idx < ipolicer->band_prof.max; prof_idx++) {
5986 if (ipolicer->pfvf_map[prof_idx] != pcifunc)
5987 continue;
5988
5989 /* Clear ratelimit aggregation, if any */
5990 if (layer == BAND_PROF_LEAF_LAYER &&
5991 ipolicer->match_id[prof_idx])
5992 nix_clear_ratelimit_aggr(rvu, nix_hw, prof_idx);
5993
5994 ipolicer->pfvf_map[prof_idx] = 0x00;
5995 ipolicer->match_id[prof_idx] = 0;
5996 rvu_free_rsrc(&ipolicer->band_prof, prof_idx);
5997 }
5998 }
5999 mutex_unlock(&rvu->rsrc_lock);
6000 return 0;
6001 }
6002
6003 int rvu_mbox_handler_nix_bandprof_free(struct rvu *rvu,
6004 struct nix_bandprof_free_req *req,
6005 struct msg_rsp *rsp)
6006 {
6007 int blkaddr, layer, prof_idx, idx, err;
6008 u16 pcifunc = req->hdr.pcifunc;
6009 struct nix_ipolicer *ipolicer;
6010 struct nix_hw *nix_hw;
6011
6012 if (req->free_all)
6013 return nix_free_all_bandprof(rvu, pcifunc);
6014
6015 if (!rvu->hw->cap.ipolicer)
6016 return NIX_AF_ERR_IPOLICER_NOTSUPP;
6017
6018 err = nix_get_struct_ptrs(rvu, pcifunc, &nix_hw, &blkaddr);
6019 if (err)
6020 return err;
6021
6022 mutex_lock(&rvu->rsrc_lock);
6023 /* Free the requested profile indices */
6024 for (layer = 0; layer < BAND_PROF_NUM_LAYERS; layer++) {
6025 if (layer == BAND_PROF_INVAL_LAYER)
6026 continue;
6027 if (!req->prof_count[layer])
6028 continue;
6029
6030 ipolicer = &nix_hw->ipolicer[layer];
6031 for (idx = 0; idx < req->prof_count[layer]; idx++) {
6032 if (idx == MAX_BANDPROF_PER_PFFUNC)
6033 break;
6034 prof_idx = req->prof_idx[layer][idx];
6035 if (prof_idx >= ipolicer->band_prof.max ||
6036 ipolicer->pfvf_map[prof_idx] != pcifunc)
6037 continue;
6038
6039 /* Clear ratelimit aggregation, if any */
6040 if (layer == BAND_PROF_LEAF_LAYER &&
6041 ipolicer->match_id[prof_idx])
6042 nix_clear_ratelimit_aggr(rvu, nix_hw, prof_idx);
6043
6044 ipolicer->pfvf_map[prof_idx] = 0x00;
6045 ipolicer->match_id[prof_idx] = 0;
6046 rvu_free_rsrc(&ipolicer->band_prof, prof_idx);
6047 }
6048 }
6049 mutex_unlock(&rvu->rsrc_lock);
6050 return 0;
6051 }
6052
6053 int nix_aq_context_read(struct rvu *rvu, struct nix_hw *nix_hw,
6054 struct nix_cn10k_aq_enq_req *aq_req,
6055 struct nix_cn10k_aq_enq_rsp *aq_rsp,
6056 u16 pcifunc, u8 ctype, u32 qidx)
6057 {
6058 memset(aq_req, 0, sizeof(struct nix_cn10k_aq_enq_req));
6059 aq_req->hdr.pcifunc = pcifunc;
6060 aq_req->ctype = ctype;
6061 aq_req->op = NIX_AQ_INSTOP_READ;
6062 aq_req->qidx = qidx;
6063
6064 return rvu_nix_blk_aq_enq_inst(rvu, nix_hw,
6065 (struct nix_aq_enq_req *)aq_req,
6066 (struct nix_aq_enq_rsp *)aq_rsp);
6067 }
6068
6069 static int nix_ipolicer_map_leaf_midprofs(struct rvu *rvu,
6070 struct nix_hw *nix_hw,
6071 struct nix_cn10k_aq_enq_req *aq_req,
6072 struct nix_cn10k_aq_enq_rsp *aq_rsp,
6073 u32 leaf_prof, u16 mid_prof)
6074 {
6075 memset(aq_req, 0, sizeof(struct nix_cn10k_aq_enq_req));
6076 aq_req->hdr.pcifunc = 0x00;
6077 aq_req->ctype = NIX_AQ_CTYPE_BANDPROF;
6078 aq_req->op = NIX_AQ_INSTOP_WRITE;
6079 aq_req->qidx = leaf_prof;
6080
6081 aq_req->prof.band_prof_id = mid_prof & 0x7F;
6082 aq_req->prof_mask.band_prof_id = GENMASK(6, 0);
6083 aq_req->prof.band_prof_id_h = FIELD_GET(NIX_BW_PROF_HI_MASK, mid_prof);
6084 aq_req->prof_mask.band_prof_id_h = GENMASK(3, 0);
6085 aq_req->prof.hl_en = 1;
6086 aq_req->prof_mask.hl_en = 1;
6087
6088 return rvu_nix_blk_aq_enq_inst(rvu, nix_hw,
6089 (struct nix_aq_enq_req *)aq_req,
6090 (struct nix_aq_enq_rsp *)aq_rsp);
6091 }
6092
6093 #define NIX_RQ_PROF_HI_MASK GENMASK(13, 10)
6094
6095 int rvu_nix_setup_ratelimit_aggr(struct rvu *rvu, u16 pcifunc,
6096 u16 rq_idx, u16 match_id)
6097 {
6098 int leaf_prof, mid_prof, leaf_match;
6099 struct nix_cn10k_aq_enq_req aq_req;
6100 struct nix_cn10k_aq_enq_rsp aq_rsp;
6101 struct nix_ipolicer *ipolicer;
6102 struct nix_hw *nix_hw;
6103 int blkaddr, idx, rc;
6104
6105 if (!rvu->hw->cap.ipolicer)
6106 return 0;
6107
6108 rc = nix_get_struct_ptrs(rvu, pcifunc, &nix_hw, &blkaddr);
6109 if (rc)
6110 return rc;
6111
6112 /* Fetch the RQ's context to see if policing is enabled */
6113 rc = nix_aq_context_read(rvu, nix_hw, &aq_req, &aq_rsp, pcifunc,
6114 NIX_AQ_CTYPE_RQ, rq_idx);
6115 if (rc) {
6116 dev_err(rvu->dev,
6117 "%s: Failed to fetch RQ%d context of PFFUNC 0x%x\n",
6118 __func__, rq_idx, pcifunc);
6119 return rc;
6120 }
6121
6122 if (!aq_rsp.rq.policer_ena)
6123 return 0;
6124
6125 /* Get the bandwidth profile ID mapped to this RQ */
6126 leaf_prof = FIELD_PREP(NIX_RQ_PROF_HI_MASK, aq_rsp.rq.band_prof_id_h);
6127 leaf_prof |= aq_rsp.rq.band_prof_id;
6128
6129 ipolicer = &nix_hw->ipolicer[BAND_PROF_LEAF_LAYER];
6130 ipolicer->match_id[leaf_prof] = match_id;
6131
6132 /* Check if any other leaf profile is marked with same match_id */
6133 for (idx = 0; idx < ipolicer->band_prof.max; idx++) {
6134 if (idx == leaf_prof)
6135 continue;
6136 if (ipolicer->match_id[idx] != match_id)
6137 continue;
6138
6139 leaf_match = idx;
6140 break;
6141 }
6142
6143 if (idx == ipolicer->band_prof.max)
6144 return 0;
6145
6146 /* Fetch the matching profile's context to check if it's already
6147 * mapped to a mid level profile.
6148 */
6149 rc = nix_aq_context_read(rvu, nix_hw, &aq_req, &aq_rsp, 0x00,
6150 NIX_AQ_CTYPE_BANDPROF, leaf_match);
6151 if (rc) {
6152 dev_err(rvu->dev,
6153 "%s: Failed to fetch context of leaf profile %d\n",
6154 __func__, leaf_match);
6155 return rc;
6156 }
6157
6158 ipolicer = &nix_hw->ipolicer[BAND_PROF_MID_LAYER];
6159 if (aq_rsp.prof.hl_en) {
6160 /* Get Mid layer prof index and map leaf_prof index
6161 * also such that flows that are being steered
6162 * to different RQs and marked with same match_id
6163 * are rate limited in a aggregate fashion
6164 */
6165 mid_prof = FIELD_PREP(NIX_BW_PROF_HI_MASK,
6166 aq_rsp.prof.band_prof_id_h);
6167 mid_prof |= aq_rsp.prof.band_prof_id;
6168
6169 rc = nix_ipolicer_map_leaf_midprofs(rvu, nix_hw,
6170 &aq_req, &aq_rsp,
6171 leaf_prof, mid_prof);
6172 if (rc) {
6173 dev_err(rvu->dev,
6174 "%s: Failed to map leaf(%d) and mid(%d) profiles\n",
6175 __func__, leaf_prof, mid_prof);
6176 goto exit;
6177 }
6178
6179 mutex_lock(&rvu->rsrc_lock);
6180 ipolicer->ref_count[mid_prof]++;
6181 mutex_unlock(&rvu->rsrc_lock);
6182 goto exit;
6183 }
6184
6185 /* Allocate a mid layer profile and
6186 * map both 'leaf_prof' and 'leaf_match' profiles to it.
6187 */
6188 mutex_lock(&rvu->rsrc_lock);
6189 mid_prof = rvu_alloc_rsrc(&ipolicer->band_prof);
6190 if (mid_prof < 0) {
6191 dev_err(rvu->dev,
6192 "%s: Unable to allocate mid layer profile\n", __func__);
6193 mutex_unlock(&rvu->rsrc_lock);
6194 goto exit;
6195 }
6196 mutex_unlock(&rvu->rsrc_lock);
6197 ipolicer->pfvf_map[mid_prof] = 0x00;
6198 ipolicer->ref_count[mid_prof] = 0;
6199
6200 /* Initialize mid layer profile same as 'leaf_prof' */
6201 rc = nix_aq_context_read(rvu, nix_hw, &aq_req, &aq_rsp, 0x00,
6202 NIX_AQ_CTYPE_BANDPROF, leaf_prof);
6203 if (rc) {
6204 dev_err(rvu->dev,
6205 "%s: Failed to fetch context of leaf profile %d\n",
6206 __func__, leaf_prof);
6207 goto exit;
6208 }
6209
6210 memset(&aq_req, 0, sizeof(struct nix_cn10k_aq_enq_req));
6211 aq_req.hdr.pcifunc = 0x00;
6212 aq_req.qidx = (mid_prof & 0x3FFF) | (BAND_PROF_MID_LAYER << 14);
6213 aq_req.ctype = NIX_AQ_CTYPE_BANDPROF;
6214 aq_req.op = NIX_AQ_INSTOP_WRITE;
6215 memcpy(&aq_req.prof, &aq_rsp.prof, sizeof(struct nix_bandprof_s));
6216 memset((char *)&aq_req.prof_mask, 0xff, sizeof(struct nix_bandprof_s));
6217 /* Clear higher layer enable bit in the mid profile, just in case */
6218 aq_req.prof.hl_en = 0;
6219 aq_req.prof_mask.hl_en = 1;
6220
6221 rc = rvu_nix_blk_aq_enq_inst(rvu, nix_hw,
6222 (struct nix_aq_enq_req *)&aq_req, NULL);
6223 if (rc) {
6224 dev_err(rvu->dev,
6225 "%s: Failed to INIT context of mid layer profile %d\n",
6226 __func__, mid_prof);
6227 goto exit;
6228 }
6229
6230 /* Map both leaf profiles to this mid layer profile */
6231 rc = nix_ipolicer_map_leaf_midprofs(rvu, nix_hw,
6232 &aq_req, &aq_rsp,
6233 leaf_prof, mid_prof);
6234 if (rc) {
6235 dev_err(rvu->dev,
6236 "%s: Failed to map leaf(%d) and mid(%d) profiles\n",
6237 __func__, leaf_prof, mid_prof);
6238 goto exit;
6239 }
6240
6241 mutex_lock(&rvu->rsrc_lock);
6242 ipolicer->ref_count[mid_prof]++;
6243 mutex_unlock(&rvu->rsrc_lock);
6244
6245 rc = nix_ipolicer_map_leaf_midprofs(rvu, nix_hw,
6246 &aq_req, &aq_rsp,
6247 leaf_match, mid_prof);
6248 if (rc) {
6249 dev_err(rvu->dev,
6250 "%s: Failed to map leaf(%d) and mid(%d) profiles\n",
6251 __func__, leaf_match, mid_prof);
6252 ipolicer->ref_count[mid_prof]--;
6253 goto exit;
6254 }
6255
6256 mutex_lock(&rvu->rsrc_lock);
6257 ipolicer->ref_count[mid_prof]++;
6258 mutex_unlock(&rvu->rsrc_lock);
6259
6260 exit:
6261 return rc;
6262 }
6263
6264 /* Called with mutex rsrc_lock */
6265 static void nix_clear_ratelimit_aggr(struct rvu *rvu, struct nix_hw *nix_hw,
6266 u32 leaf_prof)
6267 {
6268 struct nix_cn10k_aq_enq_req aq_req;
6269 struct nix_cn10k_aq_enq_rsp aq_rsp;
6270 struct nix_ipolicer *ipolicer;
6271 u16 mid_prof;
6272 int rc;
6273
6274 mutex_unlock(&rvu->rsrc_lock);
6275
6276 rc = nix_aq_context_read(rvu, nix_hw, &aq_req, &aq_rsp, 0x00,
6277 NIX_AQ_CTYPE_BANDPROF, leaf_prof);
6278
6279 mutex_lock(&rvu->rsrc_lock);
6280 if (rc) {
6281 dev_err(rvu->dev,
6282 "%s: Failed to fetch context of leaf profile %d\n",
6283 __func__, leaf_prof);
6284 return;
6285 }
6286
6287 if (!aq_rsp.prof.hl_en)
6288 return;
6289
6290 mid_prof = FIELD_PREP(NIX_BW_PROF_HI_MASK, aq_rsp.prof.band_prof_id_h);
6291 mid_prof |= aq_rsp.prof.band_prof_id;
6292 ipolicer = &nix_hw->ipolicer[BAND_PROF_MID_LAYER];
6293 ipolicer->ref_count[mid_prof]--;
6294 /* If ref_count is zero, free mid layer profile */
6295 if (!ipolicer->ref_count[mid_prof]) {
6296 ipolicer->pfvf_map[mid_prof] = 0x00;
6297 rvu_free_rsrc(&ipolicer->band_prof, mid_prof);
6298 }
6299 }
6300
6301 int rvu_mbox_handler_nix_bandprof_get_hwinfo(struct rvu *rvu, struct msg_req *req,
6302 struct nix_bandprof_get_hwinfo_rsp *rsp)
6303 {
6304 struct nix_ipolicer *ipolicer;
6305 int blkaddr, layer, err;
6306 struct nix_hw *nix_hw;
6307 u64 tu;
6308
6309 if (!rvu->hw->cap.ipolicer)
6310 return NIX_AF_ERR_IPOLICER_NOTSUPP;
6311
6312 err = nix_get_struct_ptrs(rvu, req->hdr.pcifunc, &nix_hw, &blkaddr);
6313 if (err)
6314 return err;
6315
6316 /* Return number of bandwidth profiles free at each layer */
6317 mutex_lock(&rvu->rsrc_lock);
6318 for (layer = 0; layer < BAND_PROF_NUM_LAYERS; layer++) {
6319 if (layer == BAND_PROF_INVAL_LAYER)
6320 continue;
6321
6322 ipolicer = &nix_hw->ipolicer[layer];
6323 rsp->prof_count[layer] = rvu_rsrc_free_count(&ipolicer->band_prof);
6324 }
6325 mutex_unlock(&rvu->rsrc_lock);
6326
6327 /* Set the policer timeunit in nanosec */
6328 tu = rvu_read64(rvu, blkaddr, NIX_AF_PL_TS) & GENMASK_ULL(9, 0);
6329 rsp->policer_timeunit = (tu + 1) * 100;
6330
6331 return 0;
6332 }
6333
6334 static struct nix_mcast_grp_elem *rvu_nix_mcast_find_grp_elem(struct nix_mcast_grp *mcast_grp,
6335 u32 mcast_grp_idx)
6336 {
6337 struct nix_mcast_grp_elem *iter;
6338 bool is_found = false;
6339
6340 list_for_each_entry(iter, &mcast_grp->mcast_grp_head, list) {
6341 if (iter->mcast_grp_idx == mcast_grp_idx) {
6342 is_found = true;
6343 break;
6344 }
6345 }
6346
6347 if (is_found)
6348 return iter;
6349
6350 return NULL;
6351 }
6352
6353 int rvu_nix_mcast_get_mce_index(struct rvu *rvu, u16 pcifunc, u32 mcast_grp_idx)
6354 {
6355 struct nix_mcast_grp_elem *elem;
6356 struct nix_mcast_grp *mcast_grp;
6357 struct nix_hw *nix_hw;
6358 int blkaddr, ret;
6359
6360 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NIX, pcifunc);
6361 nix_hw = get_nix_hw(rvu->hw, blkaddr);
6362 if (!nix_hw)
6363 return NIX_AF_ERR_INVALID_NIXBLK;
6364
6365 mcast_grp = &nix_hw->mcast_grp;
6366 mutex_lock(&mcast_grp->mcast_grp_lock);
6367 elem = rvu_nix_mcast_find_grp_elem(mcast_grp, mcast_grp_idx);
6368 if (!elem)
6369 ret = NIX_AF_ERR_INVALID_MCAST_GRP;
6370 else
6371 ret = elem->mce_start_index;
6372
6373 mutex_unlock(&mcast_grp->mcast_grp_lock);
6374 return ret;
6375 }
6376
6377 void rvu_nix_mcast_flr_free_entries(struct rvu *rvu, u16 pcifunc)
6378 {
6379 struct nix_mcast_grp_destroy_req dreq = { 0 };
6380 struct nix_mcast_grp_update_req ureq = { 0 };
6381 struct nix_mcast_grp_update_rsp ursp = { 0 };
6382 struct nix_mcast_grp_elem *elem, *tmp;
6383 struct nix_mcast_grp *mcast_grp;
6384 struct nix_hw *nix_hw;
6385 int blkaddr;
6386
6387 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NIX, pcifunc);
6388 nix_hw = get_nix_hw(rvu->hw, blkaddr);
6389 if (!nix_hw)
6390 return;
6391
6392 mcast_grp = &nix_hw->mcast_grp;
6393
6394 mutex_lock(&mcast_grp->mcast_grp_lock);
6395 list_for_each_entry_safe(elem, tmp, &mcast_grp->mcast_grp_head, list) {
6396 struct nix_mce_list *mce_list;
6397 struct hlist_node *tmp;
6398 struct mce *mce;
6399
6400 /* If the pcifunc which created the multicast/mirror
6401 * group received an FLR, then delete the entire group.
6402 */
6403 if (elem->pcifunc == pcifunc) {
6404 /* Delete group */
6405 dreq.hdr.pcifunc = elem->pcifunc;
6406 dreq.mcast_grp_idx = elem->mcast_grp_idx;
6407 dreq.is_af = 1;
6408 rvu_mbox_handler_nix_mcast_grp_destroy(rvu, &dreq, NULL);
6409 continue;
6410 }
6411
6412 /* Iterate the group elements and delete the element which
6413 * received the FLR.
6414 */
6415 mce_list = &elem->mcast_mce_list;
6416 hlist_for_each_entry_safe(mce, tmp, &mce_list->head, node) {
6417 if (mce->pcifunc == pcifunc) {
6418 ureq.hdr.pcifunc = pcifunc;
6419 ureq.num_mce_entry = 1;
6420 ureq.mcast_grp_idx = elem->mcast_grp_idx;
6421 ureq.op = NIX_MCAST_OP_DEL_ENTRY;
6422 ureq.pcifunc[0] = pcifunc;
6423 ureq.is_af = 1;
6424 rvu_mbox_handler_nix_mcast_grp_update(rvu, &ureq, &ursp);
6425 break;
6426 }
6427 }
6428 }
6429 mutex_unlock(&mcast_grp->mcast_grp_lock);
6430 }
6431
6432 int rvu_nix_mcast_update_mcam_entry(struct rvu *rvu, u16 pcifunc,
6433 u32 mcast_grp_idx, u16 mcam_index)
6434 {
6435 struct nix_mcast_grp_elem *elem;
6436 struct nix_mcast_grp *mcast_grp;
6437 struct nix_hw *nix_hw;
6438 int blkaddr, ret = 0;
6439
6440 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NIX, pcifunc);
6441 nix_hw = get_nix_hw(rvu->hw, blkaddr);
6442 if (!nix_hw)
6443 return NIX_AF_ERR_INVALID_NIXBLK;
6444
6445 mcast_grp = &nix_hw->mcast_grp;
6446 mutex_lock(&mcast_grp->mcast_grp_lock);
6447 elem = rvu_nix_mcast_find_grp_elem(mcast_grp, mcast_grp_idx);
6448 if (!elem)
6449 ret = NIX_AF_ERR_INVALID_MCAST_GRP;
6450 else
6451 elem->mcam_index = mcam_index;
6452
6453 mutex_unlock(&mcast_grp->mcast_grp_lock);
6454 return ret;
6455 }
6456
6457 int rvu_mbox_handler_nix_mcast_grp_create(struct rvu *rvu,
6458 struct nix_mcast_grp_create_req *req,
6459 struct nix_mcast_grp_create_rsp *rsp)
6460 {
6461 struct nix_mcast_grp_elem *elem;
6462 struct nix_mcast_grp *mcast_grp;
6463 struct nix_hw *nix_hw;
6464 int blkaddr, err;
6465
6466 err = nix_get_struct_ptrs(rvu, req->hdr.pcifunc, &nix_hw, &blkaddr);
6467 if (err)
6468 return err;
6469
6470 mcast_grp = &nix_hw->mcast_grp;
6471 elem = kzalloc_obj(*elem);
6472 if (!elem)
6473 return -ENOMEM;
6474
6475 INIT_HLIST_HEAD(&elem->mcast_mce_list.head);
6476 elem->mcam_index = -1;
6477 elem->mce_start_index = -1;
6478 elem->pcifunc = req->hdr.pcifunc;
6479 elem->dir = req->dir;
6480 elem->mcast_grp_idx = mcast_grp->next_grp_index++;
6481
6482 mutex_lock(&mcast_grp->mcast_grp_lock);
6483 list_add_tail(&elem->list, &mcast_grp->mcast_grp_head);
6484 mcast_grp->count++;
6485 mutex_unlock(&mcast_grp->mcast_grp_lock);
6486
6487 rsp->mcast_grp_idx = elem->mcast_grp_idx;
6488 return 0;
6489 }
6490
6491 int rvu_mbox_handler_nix_mcast_grp_destroy(struct rvu *rvu,
6492 struct nix_mcast_grp_destroy_req *req,
6493 struct msg_rsp *rsp)
6494 {
6495 struct npc_delete_flow_req uninstall_req = { 0 };
6496 struct npc_delete_flow_rsp uninstall_rsp = { 0 };
6497 struct nix_mcast_grp_elem *elem;
6498 struct nix_mcast_grp *mcast_grp;
6499 int blkaddr, err, ret = 0;
6500 struct nix_mcast *mcast;
6501 struct nix_hw *nix_hw;
6502
6503 err = nix_get_struct_ptrs(rvu, req->hdr.pcifunc, &nix_hw, &blkaddr);
6504 if (err)
6505 return err;
6506
6507 mcast_grp = &nix_hw->mcast_grp;
6508
6509 /* If AF is requesting for the deletion,
6510 * then AF is already taking the lock
6511 */
6512 if (!req->is_af)
6513 mutex_lock(&mcast_grp->mcast_grp_lock);
6514
6515 elem = rvu_nix_mcast_find_grp_elem(mcast_grp, req->mcast_grp_idx);
6516 if (!elem) {
6517 ret = NIX_AF_ERR_INVALID_MCAST_GRP;
6518 goto unlock_grp;
6519 }
6520
6521 /* If no mce entries are associated with the group
6522 * then just remove it from the global list.
6523 */
6524 if (!elem->mcast_mce_list.count)
6525 goto delete_grp;
6526
6527 /* Delete the associated mcam entry and
6528 * remove all mce entries from the group
6529 */
6530 mcast = &nix_hw->mcast;
6531 mutex_lock(&mcast->mce_lock);
6532 if (elem->mcam_index != -1) {
6533 uninstall_req.hdr.pcifunc = req->hdr.pcifunc;
6534 uninstall_req.entry = elem->mcam_index;
6535 rvu_mbox_handler_npc_delete_flow(rvu, &uninstall_req, &uninstall_rsp);
6536 }
6537
6538 nix_free_mce_list(mcast, elem->mcast_mce_list.count,
6539 elem->mce_start_index, elem->dir);
6540 nix_delete_mcast_mce_list(&elem->mcast_mce_list);
6541 mutex_unlock(&mcast->mce_lock);
6542
6543 delete_grp:
6544 list_del(&elem->list);
6545 kfree(elem);
6546 mcast_grp->count--;
6547
6548 unlock_grp:
6549 if (!req->is_af)
6550 mutex_unlock(&mcast_grp->mcast_grp_lock);
6551
6552 return ret;
6553 }
6554
6555 int rvu_mbox_handler_nix_mcast_grp_update(struct rvu *rvu,
6556 struct nix_mcast_grp_update_req *req,
6557 struct nix_mcast_grp_update_rsp *rsp)
6558 {
6559 struct nix_mcast_grp_destroy_req dreq = { 0 };
6560 struct npc_mcam *mcam = &rvu->hw->mcam;
6561 struct nix_mcast_grp_elem *elem;
6562 struct nix_mcast_grp *mcast_grp;
6563 int blkaddr, err, npc_blkaddr;
6564 u16 prev_count, new_count;
6565 struct nix_mcast *mcast;
6566 struct nix_hw *nix_hw;
6567 int i, ret;
6568
6569 if (!req->num_mce_entry)
6570 return 0;
6571
6572 err = nix_get_struct_ptrs(rvu, req->hdr.pcifunc, &nix_hw, &blkaddr);
6573 if (err)
6574 return err;
6575
6576 mcast_grp = &nix_hw->mcast_grp;
6577
6578 /* If AF is requesting for the updation,
6579 * then AF is already taking the lock
6580 */
6581 if (!req->is_af)
6582 mutex_lock(&mcast_grp->mcast_grp_lock);
6583
6584 elem = rvu_nix_mcast_find_grp_elem(mcast_grp, req->mcast_grp_idx);
6585 if (!elem) {
6586 ret = NIX_AF_ERR_INVALID_MCAST_GRP;
6587 goto unlock_grp;
6588 }
6589
6590 /* If any pcifunc matches the group's pcifunc, then we can
6591 * delete the entire group.
6592 */
6593 if (req->op == NIX_MCAST_OP_DEL_ENTRY) {
6594 for (i = 0; i < req->num_mce_entry; i++) {
6595 if (elem->pcifunc == req->pcifunc[i]) {
6596 /* Delete group */
6597 dreq.hdr.pcifunc = elem->pcifunc;
6598 dreq.mcast_grp_idx = elem->mcast_grp_idx;
6599 dreq.is_af = 1;
6600 rvu_mbox_handler_nix_mcast_grp_destroy(rvu, &dreq, NULL);
6601 ret = 0;
6602 goto unlock_grp;
6603 }
6604 }
6605 }
6606
6607 mcast = &nix_hw->mcast;
6608 mutex_lock(&mcast->mce_lock);
6609 npc_blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
6610 if (elem->mcam_index != -1)
6611 npc_enable_mcam_entry(rvu, mcam, npc_blkaddr, elem->mcam_index, false);
6612
6613 prev_count = elem->mcast_mce_list.count;
6614 if (req->op == NIX_MCAST_OP_ADD_ENTRY) {
6615 new_count = prev_count + req->num_mce_entry;
6616 if (prev_count)
6617 nix_free_mce_list(mcast, prev_count, elem->mce_start_index, elem->dir);
6618
6619 elem->mce_start_index = nix_alloc_mce_list(mcast, new_count, elem->dir);
6620
6621 /* It is possible not to get contiguous memory */
6622 if (elem->mce_start_index < 0) {
6623 if (elem->mcam_index != -1) {
6624 npc_enable_mcam_entry(rvu, mcam, npc_blkaddr,
6625 elem->mcam_index, true);
6626 ret = NIX_AF_ERR_NON_CONTIG_MCE_LIST;
6627 goto unlock_mce;
6628 }
6629 }
6630
6631 ret = nix_add_mce_list_entry(rvu, nix_hw, elem, req);
6632 if (ret) {
6633 nix_free_mce_list(mcast, new_count, elem->mce_start_index, elem->dir);
6634 if (prev_count)
6635 elem->mce_start_index = nix_alloc_mce_list(mcast,
6636 prev_count,
6637 elem->dir);
6638
6639 if (elem->mcam_index != -1)
6640 npc_enable_mcam_entry(rvu, mcam, npc_blkaddr,
6641 elem->mcam_index, true);
6642
6643 goto unlock_mce;
6644 }
6645 } else {
6646 if (!prev_count || prev_count < req->num_mce_entry) {
6647 if (elem->mcam_index != -1)
6648 npc_enable_mcam_entry(rvu, mcam, npc_blkaddr,
6649 elem->mcam_index, true);
6650 ret = NIX_AF_ERR_INVALID_MCAST_DEL_REQ;
6651 goto unlock_mce;
6652 }
6653
6654 nix_free_mce_list(mcast, prev_count, elem->mce_start_index, elem->dir);
6655 new_count = prev_count - req->num_mce_entry;
6656 elem->mce_start_index = nix_alloc_mce_list(mcast, new_count, elem->dir);
6657 ret = nix_del_mce_list_entry(rvu, nix_hw, elem, req);
6658 if (ret) {
6659 nix_free_mce_list(mcast, new_count, elem->mce_start_index, elem->dir);
6660 elem->mce_start_index = nix_alloc_mce_list(mcast, prev_count, elem->dir);
6661 if (elem->mcam_index != -1)
6662 npc_enable_mcam_entry(rvu, mcam,
6663 npc_blkaddr,
6664 elem->mcam_index,
6665 true);
6666
6667 goto unlock_mce;
6668 }
6669 }
6670
6671 if (elem->mcam_index == -1) {
6672 rsp->mce_start_index = elem->mce_start_index;
6673 ret = 0;
6674 goto unlock_mce;
6675 }
6676
6677 nix_mcast_update_action(rvu, elem);
6678 npc_enable_mcam_entry(rvu, mcam, npc_blkaddr, elem->mcam_index, true);
6679 rsp->mce_start_index = elem->mce_start_index;
6680 ret = 0;
6681
6682 unlock_mce:
6683 mutex_unlock(&mcast->mce_lock);
6684
6685 unlock_grp:
6686 if (!req->is_af)
6687 mutex_unlock(&mcast_grp->mcast_grp_lock);
6688
6689 return ret;
6690 }
6691
6692 /* On CN10k and older series of silicons, hardware may incorrectly
6693 * assert XOFF on certain channels. Issue a write on NIX_AF_RX_CHANX_CFG
6694 * to broadcacst XON on the same.
6695 */
6696 void rvu_block_bcast_xon(struct rvu *rvu, int blkaddr)
6697 {
6698 struct rvu_block *block = &rvu->hw->block[blkaddr];
6699 u64 cfg;
6700
6701 if (!block->implemented || is_cn20k(rvu->pdev))
6702 return;
6703
6704 cfg = rvu_read64(rvu, blkaddr, NIX_AF_RX_CHANX_CFG(0));
6705 rvu_write64(rvu, blkaddr, NIX_AF_RX_CHANX_CFG(0), cfg);
6706 }