7de5417277e6830df8f11d75a72fc057b2326dca
[linux-2.6-microblaze.git] / drivers / net / ethernet / marvell / octeontx2 / af / rvu_nix.c
1 // SPDX-License-Identifier: GPL-2.0
2 /* Marvell OcteonTx2 RVU Admin Function driver
3  *
4  * Copyright (C) 2018 Marvell International Ltd.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  */
10
11 #include <linux/module.h>
12 #include <linux/pci.h>
13
14 #include "rvu_struct.h"
15 #include "rvu_reg.h"
16 #include "rvu.h"
17 #include "cgx.h"
18
19 static int nix_update_bcast_mce_list(struct rvu *rvu, u16 pcifunc, bool add);
20
21 enum mc_tbl_sz {
22         MC_TBL_SZ_256,
23         MC_TBL_SZ_512,
24         MC_TBL_SZ_1K,
25         MC_TBL_SZ_2K,
26         MC_TBL_SZ_4K,
27         MC_TBL_SZ_8K,
28         MC_TBL_SZ_16K,
29         MC_TBL_SZ_32K,
30         MC_TBL_SZ_64K,
31 };
32
33 enum mc_buf_cnt {
34         MC_BUF_CNT_8,
35         MC_BUF_CNT_16,
36         MC_BUF_CNT_32,
37         MC_BUF_CNT_64,
38         MC_BUF_CNT_128,
39         MC_BUF_CNT_256,
40         MC_BUF_CNT_512,
41         MC_BUF_CNT_1024,
42         MC_BUF_CNT_2048,
43 };
44
45 /* For now considering MC resources needed for broadcast
46  * pkt replication only. i.e 256 HWVFs + 12 PFs.
47  */
48 #define MC_TBL_SIZE     MC_TBL_SZ_512
49 #define MC_BUF_CNT      MC_BUF_CNT_128
50
51 struct mce {
52         struct hlist_node       node;
53         u16                     idx;
54         u16                     pcifunc;
55 };
56
57 static void nix_mce_list_init(struct nix_mce_list *list, int max)
58 {
59         INIT_HLIST_HEAD(&list->head);
60         list->count = 0;
61         list->max = max;
62 }
63
64 static u16 nix_alloc_mce_list(struct nix_mcast *mcast, int count)
65 {
66         int idx;
67
68         if (!mcast)
69                 return 0;
70
71         idx = mcast->next_free_mce;
72         mcast->next_free_mce += count;
73         return idx;
74 }
75
76 static inline struct nix_hw *get_nix_hw(struct rvu_hwinfo *hw, int blkaddr)
77 {
78         if (blkaddr == BLKADDR_NIX0 && hw->nix0)
79                 return hw->nix0;
80
81         return NULL;
82 }
83
84 static bool is_valid_txschq(struct rvu *rvu, int blkaddr,
85                             int lvl, u16 pcifunc, u16 schq)
86 {
87         struct nix_txsch *txsch;
88         struct nix_hw *nix_hw;
89
90         nix_hw = get_nix_hw(rvu->hw, blkaddr);
91         if (!nix_hw)
92                 return false;
93
94         txsch = &nix_hw->txsch[lvl];
95         /* Check out of bounds */
96         if (schq >= txsch->schq.max)
97                 return false;
98
99         spin_lock(&rvu->rsrc_lock);
100         if (txsch->pfvf_map[schq] != pcifunc) {
101                 spin_unlock(&rvu->rsrc_lock);
102                 return false;
103         }
104         spin_unlock(&rvu->rsrc_lock);
105         return true;
106 }
107
108 static int nix_interface_init(struct rvu *rvu, u16 pcifunc, int type, int nixlf)
109 {
110         struct rvu_pfvf *pfvf = rvu_get_pfvf(rvu, pcifunc);
111         u8 cgx_id, lmac_id;
112         int pkind, pf;
113         int err;
114
115         pf = rvu_get_pf(pcifunc);
116         if (!is_pf_cgxmapped(rvu, pf) && type != NIX_INTF_TYPE_LBK)
117                 return 0;
118
119         switch (type) {
120         case NIX_INTF_TYPE_CGX:
121                 pfvf->cgx_lmac = rvu->pf2cgxlmac_map[pf];
122                 rvu_get_cgx_lmac_id(pfvf->cgx_lmac, &cgx_id, &lmac_id);
123
124                 pkind = rvu_npc_get_pkind(rvu, pf);
125                 if (pkind < 0) {
126                         dev_err(rvu->dev,
127                                 "PF_Func 0x%x: Invalid pkind\n", pcifunc);
128                         return -EINVAL;
129                 }
130                 cgx_set_pkind(rvu_cgx_pdata(cgx_id, rvu), lmac_id, pkind);
131                 rvu_npc_set_pkind(rvu, pkind, pfvf);
132                 break;
133         case NIX_INTF_TYPE_LBK:
134                 break;
135         }
136
137         /* Add this PF_FUNC to bcast pkt replication list */
138         err = nix_update_bcast_mce_list(rvu, pcifunc, true);
139         if (err) {
140                 dev_err(rvu->dev,
141                         "Bcast list, failed to enable PF_FUNC 0x%x\n",
142                         pcifunc);
143         }
144         return 0;
145 }
146
147 static void nix_interface_deinit(struct rvu *rvu, u16 pcifunc, u8 nixlf)
148 {
149         int err;
150
151         /* Remove this PF_FUNC from bcast pkt replication list */
152         err = nix_update_bcast_mce_list(rvu, pcifunc, false);
153         if (err) {
154                 dev_err(rvu->dev,
155                         "Bcast list, failed to disable PF_FUNC 0x%x\n",
156                         pcifunc);
157         }
158 }
159
160 static void nix_setup_lso_tso_l3(struct rvu *rvu, int blkaddr,
161                                  u64 format, bool v4, u64 *fidx)
162 {
163         struct nix_lso_format field = {0};
164
165         /* IP's Length field */
166         field.layer = NIX_TXLAYER_OL3;
167         /* In ipv4, length field is at offset 2 bytes, for ipv6 it's 4 */
168         field.offset = v4 ? 2 : 4;
169         field.sizem1 = 1; /* i.e 2 bytes */
170         field.alg = NIX_LSOALG_ADD_PAYLEN;
171         rvu_write64(rvu, blkaddr,
172                     NIX_AF_LSO_FORMATX_FIELDX(format, (*fidx)++),
173                     *(u64 *)&field);
174
175         /* No ID field in IPv6 header */
176         if (!v4)
177                 return;
178
179         /* IP's ID field */
180         field.layer = NIX_TXLAYER_OL3;
181         field.offset = 4;
182         field.sizem1 = 1; /* i.e 2 bytes */
183         field.alg = NIX_LSOALG_ADD_SEGNUM;
184         rvu_write64(rvu, blkaddr,
185                     NIX_AF_LSO_FORMATX_FIELDX(format, (*fidx)++),
186                     *(u64 *)&field);
187 }
188
189 static void nix_setup_lso_tso_l4(struct rvu *rvu, int blkaddr,
190                                  u64 format, u64 *fidx)
191 {
192         struct nix_lso_format field = {0};
193
194         /* TCP's sequence number field */
195         field.layer = NIX_TXLAYER_OL4;
196         field.offset = 4;
197         field.sizem1 = 3; /* i.e 4 bytes */
198         field.alg = NIX_LSOALG_ADD_OFFSET;
199         rvu_write64(rvu, blkaddr,
200                     NIX_AF_LSO_FORMATX_FIELDX(format, (*fidx)++),
201                     *(u64 *)&field);
202
203         /* TCP's flags field */
204         field.layer = NIX_TXLAYER_OL4;
205         field.offset = 12;
206         field.sizem1 = 0; /* not needed */
207         field.alg = NIX_LSOALG_TCP_FLAGS;
208         rvu_write64(rvu, blkaddr,
209                     NIX_AF_LSO_FORMATX_FIELDX(format, (*fidx)++),
210                     *(u64 *)&field);
211 }
212
213 static void nix_setup_lso(struct rvu *rvu, int blkaddr)
214 {
215         u64 cfg, idx, fidx = 0;
216
217         /* Enable LSO */
218         cfg = rvu_read64(rvu, blkaddr, NIX_AF_LSO_CFG);
219         /* For TSO, set first and middle segment flags to
220          * mask out PSH, RST & FIN flags in TCP packet
221          */
222         cfg &= ~((0xFFFFULL << 32) | (0xFFFFULL << 16));
223         cfg |= (0xFFF2ULL << 32) | (0xFFF2ULL << 16);
224         rvu_write64(rvu, blkaddr, NIX_AF_LSO_CFG, cfg | BIT_ULL(63));
225
226         /* Configure format fields for TCPv4 segmentation offload */
227         idx = NIX_LSO_FORMAT_IDX_TSOV4;
228         nix_setup_lso_tso_l3(rvu, blkaddr, idx, true, &fidx);
229         nix_setup_lso_tso_l4(rvu, blkaddr, idx, &fidx);
230
231         /* Set rest of the fields to NOP */
232         for (; fidx < 8; fidx++) {
233                 rvu_write64(rvu, blkaddr,
234                             NIX_AF_LSO_FORMATX_FIELDX(idx, fidx), 0x0ULL);
235         }
236
237         /* Configure format fields for TCPv6 segmentation offload */
238         idx = NIX_LSO_FORMAT_IDX_TSOV6;
239         fidx = 0;
240         nix_setup_lso_tso_l3(rvu, blkaddr, idx, false, &fidx);
241         nix_setup_lso_tso_l4(rvu, blkaddr, idx, &fidx);
242
243         /* Set rest of the fields to NOP */
244         for (; fidx < 8; fidx++) {
245                 rvu_write64(rvu, blkaddr,
246                             NIX_AF_LSO_FORMATX_FIELDX(idx, fidx), 0x0ULL);
247         }
248 }
249
250 static void nix_ctx_free(struct rvu *rvu, struct rvu_pfvf *pfvf)
251 {
252         kfree(pfvf->rq_bmap);
253         kfree(pfvf->sq_bmap);
254         kfree(pfvf->cq_bmap);
255         if (pfvf->rq_ctx)
256                 qmem_free(rvu->dev, pfvf->rq_ctx);
257         if (pfvf->sq_ctx)
258                 qmem_free(rvu->dev, pfvf->sq_ctx);
259         if (pfvf->cq_ctx)
260                 qmem_free(rvu->dev, pfvf->cq_ctx);
261         if (pfvf->rss_ctx)
262                 qmem_free(rvu->dev, pfvf->rss_ctx);
263         if (pfvf->nix_qints_ctx)
264                 qmem_free(rvu->dev, pfvf->nix_qints_ctx);
265         if (pfvf->cq_ints_ctx)
266                 qmem_free(rvu->dev, pfvf->cq_ints_ctx);
267
268         pfvf->rq_bmap = NULL;
269         pfvf->cq_bmap = NULL;
270         pfvf->sq_bmap = NULL;
271         pfvf->rq_ctx = NULL;
272         pfvf->sq_ctx = NULL;
273         pfvf->cq_ctx = NULL;
274         pfvf->rss_ctx = NULL;
275         pfvf->nix_qints_ctx = NULL;
276         pfvf->cq_ints_ctx = NULL;
277 }
278
279 static int nixlf_rss_ctx_init(struct rvu *rvu, int blkaddr,
280                               struct rvu_pfvf *pfvf, int nixlf,
281                               int rss_sz, int rss_grps, int hwctx_size)
282 {
283         int err, grp, num_indices;
284
285         /* RSS is not requested for this NIXLF */
286         if (!rss_sz)
287                 return 0;
288         num_indices = rss_sz * rss_grps;
289
290         /* Alloc NIX RSS HW context memory and config the base */
291         err = qmem_alloc(rvu->dev, &pfvf->rss_ctx, num_indices, hwctx_size);
292         if (err)
293                 return err;
294
295         rvu_write64(rvu, blkaddr, NIX_AF_LFX_RSS_BASE(nixlf),
296                     (u64)pfvf->rss_ctx->iova);
297
298         /* Config full RSS table size, enable RSS and caching */
299         rvu_write64(rvu, blkaddr, NIX_AF_LFX_RSS_CFG(nixlf),
300                     BIT_ULL(36) | BIT_ULL(4) |
301                     ilog2(num_indices / MAX_RSS_INDIR_TBL_SIZE));
302         /* Config RSS group offset and sizes */
303         for (grp = 0; grp < rss_grps; grp++)
304                 rvu_write64(rvu, blkaddr, NIX_AF_LFX_RSS_GRPX(nixlf, grp),
305                             ((ilog2(rss_sz) - 1) << 16) | (rss_sz * grp));
306         return 0;
307 }
308
309 static int nix_aq_enqueue_wait(struct rvu *rvu, struct rvu_block *block,
310                                struct nix_aq_inst_s *inst)
311 {
312         struct admin_queue *aq = block->aq;
313         struct nix_aq_res_s *result;
314         int timeout = 1000;
315         u64 reg, head;
316
317         result = (struct nix_aq_res_s *)aq->res->base;
318
319         /* Get current head pointer where to append this instruction */
320         reg = rvu_read64(rvu, block->addr, NIX_AF_AQ_STATUS);
321         head = (reg >> 4) & AQ_PTR_MASK;
322
323         memcpy((void *)(aq->inst->base + (head * aq->inst->entry_sz)),
324                (void *)inst, aq->inst->entry_sz);
325         memset(result, 0, sizeof(*result));
326         /* sync into memory */
327         wmb();
328
329         /* Ring the doorbell and wait for result */
330         rvu_write64(rvu, block->addr, NIX_AF_AQ_DOOR, 1);
331         while (result->compcode == NIX_AQ_COMP_NOTDONE) {
332                 cpu_relax();
333                 udelay(1);
334                 timeout--;
335                 if (!timeout)
336                         return -EBUSY;
337         }
338
339         if (result->compcode != NIX_AQ_COMP_GOOD)
340                 /* TODO: Replace this with some error code */
341                 return -EBUSY;
342
343         return 0;
344 }
345
346 static int rvu_nix_aq_enq_inst(struct rvu *rvu, struct nix_aq_enq_req *req,
347                                struct nix_aq_enq_rsp *rsp)
348 {
349         struct rvu_hwinfo *hw = rvu->hw;
350         u16 pcifunc = req->hdr.pcifunc;
351         int nixlf, blkaddr, rc = 0;
352         struct nix_aq_inst_s inst;
353         struct rvu_block *block;
354         struct admin_queue *aq;
355         struct rvu_pfvf *pfvf;
356         void *ctx, *mask;
357         bool ena;
358         u64 cfg;
359
360         pfvf = rvu_get_pfvf(rvu, pcifunc);
361         blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NIX, pcifunc);
362         if (!pfvf->nixlf || blkaddr < 0)
363                 return NIX_AF_ERR_AF_LF_INVALID;
364
365         block = &hw->block[blkaddr];
366         aq = block->aq;
367         if (!aq) {
368                 dev_warn(rvu->dev, "%s: NIX AQ not initialized\n", __func__);
369                 return NIX_AF_ERR_AQ_ENQUEUE;
370         }
371
372         nixlf = rvu_get_lf(rvu, block, pcifunc, 0);
373         if (nixlf < 0)
374                 return NIX_AF_ERR_AF_LF_INVALID;
375
376         switch (req->ctype) {
377         case NIX_AQ_CTYPE_RQ:
378                 /* Check if index exceeds max no of queues */
379                 if (!pfvf->rq_ctx || req->qidx >= pfvf->rq_ctx->qsize)
380                         rc = NIX_AF_ERR_AQ_ENQUEUE;
381                 break;
382         case NIX_AQ_CTYPE_SQ:
383                 if (!pfvf->sq_ctx || req->qidx >= pfvf->sq_ctx->qsize)
384                         rc = NIX_AF_ERR_AQ_ENQUEUE;
385                 break;
386         case NIX_AQ_CTYPE_CQ:
387                 if (!pfvf->cq_ctx || req->qidx >= pfvf->cq_ctx->qsize)
388                         rc = NIX_AF_ERR_AQ_ENQUEUE;
389                 break;
390         case NIX_AQ_CTYPE_RSS:
391                 /* Check if RSS is enabled and qidx is within range */
392                 cfg = rvu_read64(rvu, blkaddr, NIX_AF_LFX_RSS_CFG(nixlf));
393                 if (!(cfg & BIT_ULL(4)) || !pfvf->rss_ctx ||
394                     (req->qidx >= (256UL << (cfg & 0xF))))
395                         rc = NIX_AF_ERR_AQ_ENQUEUE;
396                 break;
397         case NIX_AQ_CTYPE_MCE:
398                 cfg = rvu_read64(rvu, blkaddr, NIX_AF_RX_MCAST_CFG);
399                 /* Check if index exceeds MCE list length */
400                 if (!hw->nix0->mcast.mce_ctx ||
401                     (req->qidx >= (256UL << (cfg & 0xF))))
402                         rc = NIX_AF_ERR_AQ_ENQUEUE;
403
404                 /* Adding multicast lists for requests from PF/VFs is not
405                  * yet supported, so ignore this.
406                  */
407                 if (rsp)
408                         rc = NIX_AF_ERR_AQ_ENQUEUE;
409                 break;
410         default:
411                 rc = NIX_AF_ERR_AQ_ENQUEUE;
412         }
413
414         if (rc)
415                 return rc;
416
417         /* Check if SQ pointed SMQ belongs to this PF/VF or not */
418         if (req->ctype == NIX_AQ_CTYPE_SQ &&
419             req->op != NIX_AQ_INSTOP_WRITE) {
420                 if (!is_valid_txschq(rvu, blkaddr, NIX_TXSCH_LVL_SMQ,
421                                      pcifunc, req->sq.smq))
422                         return NIX_AF_ERR_AQ_ENQUEUE;
423         }
424
425         memset(&inst, 0, sizeof(struct nix_aq_inst_s));
426         inst.lf = nixlf;
427         inst.cindex = req->qidx;
428         inst.ctype = req->ctype;
429         inst.op = req->op;
430         /* Currently we are not supporting enqueuing multiple instructions,
431          * so always choose first entry in result memory.
432          */
433         inst.res_addr = (u64)aq->res->iova;
434
435         /* Clean result + context memory */
436         memset(aq->res->base, 0, aq->res->entry_sz);
437         /* Context needs to be written at RES_ADDR + 128 */
438         ctx = aq->res->base + 128;
439         /* Mask needs to be written at RES_ADDR + 256 */
440         mask = aq->res->base + 256;
441
442         switch (req->op) {
443         case NIX_AQ_INSTOP_WRITE:
444                 if (req->ctype == NIX_AQ_CTYPE_RQ)
445                         memcpy(mask, &req->rq_mask,
446                                sizeof(struct nix_rq_ctx_s));
447                 else if (req->ctype == NIX_AQ_CTYPE_SQ)
448                         memcpy(mask, &req->sq_mask,
449                                sizeof(struct nix_sq_ctx_s));
450                 else if (req->ctype == NIX_AQ_CTYPE_CQ)
451                         memcpy(mask, &req->cq_mask,
452                                sizeof(struct nix_cq_ctx_s));
453                 else if (req->ctype == NIX_AQ_CTYPE_RSS)
454                         memcpy(mask, &req->rss_mask,
455                                sizeof(struct nix_rsse_s));
456                 else if (req->ctype == NIX_AQ_CTYPE_MCE)
457                         memcpy(mask, &req->mce_mask,
458                                sizeof(struct nix_rx_mce_s));
459                 /* Fall through */
460         case NIX_AQ_INSTOP_INIT:
461                 if (req->ctype == NIX_AQ_CTYPE_RQ)
462                         memcpy(ctx, &req->rq, sizeof(struct nix_rq_ctx_s));
463                 else if (req->ctype == NIX_AQ_CTYPE_SQ)
464                         memcpy(ctx, &req->sq, sizeof(struct nix_sq_ctx_s));
465                 else if (req->ctype == NIX_AQ_CTYPE_CQ)
466                         memcpy(ctx, &req->cq, sizeof(struct nix_cq_ctx_s));
467                 else if (req->ctype == NIX_AQ_CTYPE_RSS)
468                         memcpy(ctx, &req->rss, sizeof(struct nix_rsse_s));
469                 else if (req->ctype == NIX_AQ_CTYPE_MCE)
470                         memcpy(ctx, &req->mce, sizeof(struct nix_rx_mce_s));
471                 break;
472         case NIX_AQ_INSTOP_NOP:
473         case NIX_AQ_INSTOP_READ:
474         case NIX_AQ_INSTOP_LOCK:
475         case NIX_AQ_INSTOP_UNLOCK:
476                 break;
477         default:
478                 rc = NIX_AF_ERR_AQ_ENQUEUE;
479                 return rc;
480         }
481
482         spin_lock(&aq->lock);
483
484         /* Submit the instruction to AQ */
485         rc = nix_aq_enqueue_wait(rvu, block, &inst);
486         if (rc) {
487                 spin_unlock(&aq->lock);
488                 return rc;
489         }
490
491         /* Set RQ/SQ/CQ bitmap if respective queue hw context is enabled */
492         if (req->op == NIX_AQ_INSTOP_INIT) {
493                 if (req->ctype == NIX_AQ_CTYPE_RQ && req->rq.ena)
494                         __set_bit(req->qidx, pfvf->rq_bmap);
495                 if (req->ctype == NIX_AQ_CTYPE_SQ && req->sq.ena)
496                         __set_bit(req->qidx, pfvf->sq_bmap);
497                 if (req->ctype == NIX_AQ_CTYPE_CQ && req->cq.ena)
498                         __set_bit(req->qidx, pfvf->cq_bmap);
499         }
500
501         if (req->op == NIX_AQ_INSTOP_WRITE) {
502                 if (req->ctype == NIX_AQ_CTYPE_RQ) {
503                         ena = (req->rq.ena & req->rq_mask.ena) |
504                                 (test_bit(req->qidx, pfvf->rq_bmap) &
505                                 ~req->rq_mask.ena);
506                         if (ena)
507                                 __set_bit(req->qidx, pfvf->rq_bmap);
508                         else
509                                 __clear_bit(req->qidx, pfvf->rq_bmap);
510                 }
511                 if (req->ctype == NIX_AQ_CTYPE_SQ) {
512                         ena = (req->rq.ena & req->sq_mask.ena) |
513                                 (test_bit(req->qidx, pfvf->sq_bmap) &
514                                 ~req->sq_mask.ena);
515                         if (ena)
516                                 __set_bit(req->qidx, pfvf->sq_bmap);
517                         else
518                                 __clear_bit(req->qidx, pfvf->sq_bmap);
519                 }
520                 if (req->ctype == NIX_AQ_CTYPE_CQ) {
521                         ena = (req->rq.ena & req->cq_mask.ena) |
522                                 (test_bit(req->qidx, pfvf->cq_bmap) &
523                                 ~req->cq_mask.ena);
524                         if (ena)
525                                 __set_bit(req->qidx, pfvf->cq_bmap);
526                         else
527                                 __clear_bit(req->qidx, pfvf->cq_bmap);
528                 }
529         }
530
531         if (rsp) {
532                 /* Copy read context into mailbox */
533                 if (req->op == NIX_AQ_INSTOP_READ) {
534                         if (req->ctype == NIX_AQ_CTYPE_RQ)
535                                 memcpy(&rsp->rq, ctx,
536                                        sizeof(struct nix_rq_ctx_s));
537                         else if (req->ctype == NIX_AQ_CTYPE_SQ)
538                                 memcpy(&rsp->sq, ctx,
539                                        sizeof(struct nix_sq_ctx_s));
540                         else if (req->ctype == NIX_AQ_CTYPE_CQ)
541                                 memcpy(&rsp->cq, ctx,
542                                        sizeof(struct nix_cq_ctx_s));
543                         else if (req->ctype == NIX_AQ_CTYPE_RSS)
544                                 memcpy(&rsp->rss, ctx,
545                                        sizeof(struct nix_cq_ctx_s));
546                         else if (req->ctype == NIX_AQ_CTYPE_MCE)
547                                 memcpy(&rsp->mce, ctx,
548                                        sizeof(struct nix_rx_mce_s));
549                 }
550         }
551
552         spin_unlock(&aq->lock);
553         return 0;
554 }
555
556 static int nix_lf_hwctx_disable(struct rvu *rvu, struct hwctx_disable_req *req)
557 {
558         struct rvu_pfvf *pfvf = rvu_get_pfvf(rvu, req->hdr.pcifunc);
559         struct nix_aq_enq_req aq_req;
560         unsigned long *bmap;
561         int qidx, q_cnt = 0;
562         int err = 0, rc;
563
564         if (!pfvf->cq_ctx || !pfvf->sq_ctx || !pfvf->rq_ctx)
565                 return NIX_AF_ERR_AQ_ENQUEUE;
566
567         memset(&aq_req, 0, sizeof(struct nix_aq_enq_req));
568         aq_req.hdr.pcifunc = req->hdr.pcifunc;
569
570         if (req->ctype == NIX_AQ_CTYPE_CQ) {
571                 aq_req.cq.ena = 0;
572                 aq_req.cq_mask.ena = 1;
573                 q_cnt = pfvf->cq_ctx->qsize;
574                 bmap = pfvf->cq_bmap;
575         }
576         if (req->ctype == NIX_AQ_CTYPE_SQ) {
577                 aq_req.sq.ena = 0;
578                 aq_req.sq_mask.ena = 1;
579                 q_cnt = pfvf->sq_ctx->qsize;
580                 bmap = pfvf->sq_bmap;
581         }
582         if (req->ctype == NIX_AQ_CTYPE_RQ) {
583                 aq_req.rq.ena = 0;
584                 aq_req.rq_mask.ena = 1;
585                 q_cnt = pfvf->rq_ctx->qsize;
586                 bmap = pfvf->rq_bmap;
587         }
588
589         aq_req.ctype = req->ctype;
590         aq_req.op = NIX_AQ_INSTOP_WRITE;
591
592         for (qidx = 0; qidx < q_cnt; qidx++) {
593                 if (!test_bit(qidx, bmap))
594                         continue;
595                 aq_req.qidx = qidx;
596                 rc = rvu_nix_aq_enq_inst(rvu, &aq_req, NULL);
597                 if (rc) {
598                         err = rc;
599                         dev_err(rvu->dev, "Failed to disable %s:%d context\n",
600                                 (req->ctype == NIX_AQ_CTYPE_CQ) ?
601                                 "CQ" : ((req->ctype == NIX_AQ_CTYPE_RQ) ?
602                                 "RQ" : "SQ"), qidx);
603                 }
604         }
605
606         return err;
607 }
608
609 int rvu_mbox_handler_NIX_AQ_ENQ(struct rvu *rvu,
610                                 struct nix_aq_enq_req *req,
611                                 struct nix_aq_enq_rsp *rsp)
612 {
613         return rvu_nix_aq_enq_inst(rvu, req, rsp);
614 }
615
616 int rvu_mbox_handler_NIX_HWCTX_DISABLE(struct rvu *rvu,
617                                        struct hwctx_disable_req *req,
618                                        struct msg_rsp *rsp)
619 {
620         return nix_lf_hwctx_disable(rvu, req);
621 }
622
623 int rvu_mbox_handler_NIX_LF_ALLOC(struct rvu *rvu,
624                                   struct nix_lf_alloc_req *req,
625                                   struct nix_lf_alloc_rsp *rsp)
626 {
627         int nixlf, qints, hwctx_size, err, rc = 0;
628         struct rvu_hwinfo *hw = rvu->hw;
629         u16 pcifunc = req->hdr.pcifunc;
630         struct rvu_block *block;
631         struct rvu_pfvf *pfvf;
632         u64 cfg, ctx_cfg;
633         int blkaddr;
634
635         if (!req->rq_cnt || !req->sq_cnt || !req->cq_cnt)
636                 return NIX_AF_ERR_PARAM;
637
638         pfvf = rvu_get_pfvf(rvu, pcifunc);
639         blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NIX, pcifunc);
640         if (!pfvf->nixlf || blkaddr < 0)
641                 return NIX_AF_ERR_AF_LF_INVALID;
642
643         block = &hw->block[blkaddr];
644         nixlf = rvu_get_lf(rvu, block, pcifunc, 0);
645         if (nixlf < 0)
646                 return NIX_AF_ERR_AF_LF_INVALID;
647
648         /* If RSS is being enabled, check if requested config is valid.
649          * RSS table size should be power of two, otherwise
650          * RSS_GRP::OFFSET + adder might go beyond that group or
651          * won't be able to use entire table.
652          */
653         if (req->rss_sz && (req->rss_sz > MAX_RSS_INDIR_TBL_SIZE ||
654                             !is_power_of_2(req->rss_sz)))
655                 return NIX_AF_ERR_RSS_SIZE_INVALID;
656
657         if (req->rss_sz &&
658             (!req->rss_grps || req->rss_grps > MAX_RSS_GROUPS))
659                 return NIX_AF_ERR_RSS_GRPS_INVALID;
660
661         /* Reset this NIX LF */
662         err = rvu_lf_reset(rvu, block, nixlf);
663         if (err) {
664                 dev_err(rvu->dev, "Failed to reset NIX%d LF%d\n",
665                         block->addr - BLKADDR_NIX0, nixlf);
666                 return NIX_AF_ERR_LF_RESET;
667         }
668
669         ctx_cfg = rvu_read64(rvu, blkaddr, NIX_AF_CONST3);
670
671         /* Alloc NIX RQ HW context memory and config the base */
672         hwctx_size = 1UL << ((ctx_cfg >> 4) & 0xF);
673         err = qmem_alloc(rvu->dev, &pfvf->rq_ctx, req->rq_cnt, hwctx_size);
674         if (err)
675                 goto free_mem;
676
677         pfvf->rq_bmap = kcalloc(req->rq_cnt, sizeof(long), GFP_KERNEL);
678         if (!pfvf->rq_bmap)
679                 goto free_mem;
680
681         rvu_write64(rvu, blkaddr, NIX_AF_LFX_RQS_BASE(nixlf),
682                     (u64)pfvf->rq_ctx->iova);
683
684         /* Set caching and queue count in HW */
685         cfg = BIT_ULL(36) | (req->rq_cnt - 1);
686         rvu_write64(rvu, blkaddr, NIX_AF_LFX_RQS_CFG(nixlf), cfg);
687
688         /* Alloc NIX SQ HW context memory and config the base */
689         hwctx_size = 1UL << (ctx_cfg & 0xF);
690         err = qmem_alloc(rvu->dev, &pfvf->sq_ctx, req->sq_cnt, hwctx_size);
691         if (err)
692                 goto free_mem;
693
694         pfvf->sq_bmap = kcalloc(req->sq_cnt, sizeof(long), GFP_KERNEL);
695         if (!pfvf->sq_bmap)
696                 goto free_mem;
697
698         rvu_write64(rvu, blkaddr, NIX_AF_LFX_SQS_BASE(nixlf),
699                     (u64)pfvf->sq_ctx->iova);
700         cfg = BIT_ULL(36) | (req->sq_cnt - 1);
701         rvu_write64(rvu, blkaddr, NIX_AF_LFX_SQS_CFG(nixlf), cfg);
702
703         /* Alloc NIX CQ HW context memory and config the base */
704         hwctx_size = 1UL << ((ctx_cfg >> 8) & 0xF);
705         err = qmem_alloc(rvu->dev, &pfvf->cq_ctx, req->cq_cnt, hwctx_size);
706         if (err)
707                 goto free_mem;
708
709         pfvf->cq_bmap = kcalloc(req->cq_cnt, sizeof(long), GFP_KERNEL);
710         if (!pfvf->cq_bmap)
711                 goto free_mem;
712
713         rvu_write64(rvu, blkaddr, NIX_AF_LFX_CQS_BASE(nixlf),
714                     (u64)pfvf->cq_ctx->iova);
715         cfg = BIT_ULL(36) | (req->cq_cnt - 1);
716         rvu_write64(rvu, blkaddr, NIX_AF_LFX_CQS_CFG(nixlf), cfg);
717
718         /* Initialize receive side scaling (RSS) */
719         hwctx_size = 1UL << ((ctx_cfg >> 12) & 0xF);
720         err = nixlf_rss_ctx_init(rvu, blkaddr, pfvf, nixlf,
721                                  req->rss_sz, req->rss_grps, hwctx_size);
722         if (err)
723                 goto free_mem;
724
725         /* Alloc memory for CQINT's HW contexts */
726         cfg = rvu_read64(rvu, blkaddr, NIX_AF_CONST2);
727         qints = (cfg >> 24) & 0xFFF;
728         hwctx_size = 1UL << ((ctx_cfg >> 24) & 0xF);
729         err = qmem_alloc(rvu->dev, &pfvf->cq_ints_ctx, qints, hwctx_size);
730         if (err)
731                 goto free_mem;
732
733         rvu_write64(rvu, blkaddr, NIX_AF_LFX_CINTS_BASE(nixlf),
734                     (u64)pfvf->cq_ints_ctx->iova);
735         rvu_write64(rvu, blkaddr, NIX_AF_LFX_CINTS_CFG(nixlf), BIT_ULL(36));
736
737         /* Alloc memory for QINT's HW contexts */
738         cfg = rvu_read64(rvu, blkaddr, NIX_AF_CONST2);
739         qints = (cfg >> 12) & 0xFFF;
740         hwctx_size = 1UL << ((ctx_cfg >> 20) & 0xF);
741         err = qmem_alloc(rvu->dev, &pfvf->nix_qints_ctx, qints, hwctx_size);
742         if (err)
743                 goto free_mem;
744
745         rvu_write64(rvu, blkaddr, NIX_AF_LFX_QINTS_BASE(nixlf),
746                     (u64)pfvf->nix_qints_ctx->iova);
747         rvu_write64(rvu, blkaddr, NIX_AF_LFX_QINTS_CFG(nixlf), BIT_ULL(36));
748
749         /* Enable LMTST for this NIX LF */
750         rvu_write64(rvu, blkaddr, NIX_AF_LFX_TX_CFG2(nixlf), BIT_ULL(0));
751
752         /* Set CQE/WQE size, NPA_PF_FUNC for SQBs and also SSO_PF_FUNC
753          * If requester has sent a 'RVU_DEFAULT_PF_FUNC' use this NIX LF's
754          * PCIFUNC itself.
755          */
756         if (req->npa_func == RVU_DEFAULT_PF_FUNC)
757                 cfg = pcifunc;
758         else
759                 cfg = req->npa_func;
760
761         if (req->sso_func == RVU_DEFAULT_PF_FUNC)
762                 cfg |= (u64)pcifunc << 16;
763         else
764                 cfg |= (u64)req->sso_func << 16;
765
766         cfg |= (u64)req->xqe_sz << 33;
767         rvu_write64(rvu, blkaddr, NIX_AF_LFX_CFG(nixlf), cfg);
768
769         /* Config Rx pkt length, csum checks and apad  enable / disable */
770         rvu_write64(rvu, blkaddr, NIX_AF_LFX_RX_CFG(nixlf), req->rx_cfg);
771
772         err = nix_interface_init(rvu, pcifunc, NIX_INTF_TYPE_CGX, nixlf);
773         if (err)
774                 goto free_mem;
775
776         goto exit;
777
778 free_mem:
779         nix_ctx_free(rvu, pfvf);
780         rc = -ENOMEM;
781
782 exit:
783         /* Set macaddr of this PF/VF */
784         ether_addr_copy(rsp->mac_addr, pfvf->mac_addr);
785
786         /* set SQB size info */
787         cfg = rvu_read64(rvu, blkaddr, NIX_AF_SQ_CONST);
788         rsp->sqb_size = (cfg >> 34) & 0xFFFF;
789         rsp->lso_tsov4_idx = NIX_LSO_FORMAT_IDX_TSOV4;
790         rsp->lso_tsov6_idx = NIX_LSO_FORMAT_IDX_TSOV6;
791         return rc;
792 }
793
794 int rvu_mbox_handler_NIX_LF_FREE(struct rvu *rvu, struct msg_req *req,
795                                  struct msg_rsp *rsp)
796 {
797         struct rvu_hwinfo *hw = rvu->hw;
798         u16 pcifunc = req->hdr.pcifunc;
799         struct rvu_block *block;
800         int blkaddr, nixlf, err;
801         struct rvu_pfvf *pfvf;
802
803         pfvf = rvu_get_pfvf(rvu, pcifunc);
804         blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NIX, pcifunc);
805         if (!pfvf->nixlf || blkaddr < 0)
806                 return NIX_AF_ERR_AF_LF_INVALID;
807
808         block = &hw->block[blkaddr];
809         nixlf = rvu_get_lf(rvu, block, pcifunc, 0);
810         if (nixlf < 0)
811                 return NIX_AF_ERR_AF_LF_INVALID;
812
813         nix_interface_deinit(rvu, pcifunc, nixlf);
814
815         /* Reset this NIX LF */
816         err = rvu_lf_reset(rvu, block, nixlf);
817         if (err) {
818                 dev_err(rvu->dev, "Failed to reset NIX%d LF%d\n",
819                         block->addr - BLKADDR_NIX0, nixlf);
820                 return NIX_AF_ERR_LF_RESET;
821         }
822
823         nix_ctx_free(rvu, pfvf);
824
825         return 0;
826 }
827
828 /* Disable shaping of pkts by a scheduler queue
829  * at a given scheduler level.
830  */
831 static void nix_reset_tx_shaping(struct rvu *rvu, int blkaddr,
832                                  int lvl, int schq)
833 {
834         u64  cir_reg = 0, pir_reg = 0;
835         u64  cfg;
836
837         switch (lvl) {
838         case NIX_TXSCH_LVL_TL1:
839                 cir_reg = NIX_AF_TL1X_CIR(schq);
840                 pir_reg = 0; /* PIR not available at TL1 */
841                 break;
842         case NIX_TXSCH_LVL_TL2:
843                 cir_reg = NIX_AF_TL2X_CIR(schq);
844                 pir_reg = NIX_AF_TL2X_PIR(schq);
845                 break;
846         case NIX_TXSCH_LVL_TL3:
847                 cir_reg = NIX_AF_TL3X_CIR(schq);
848                 pir_reg = NIX_AF_TL3X_PIR(schq);
849                 break;
850         case NIX_TXSCH_LVL_TL4:
851                 cir_reg = NIX_AF_TL4X_CIR(schq);
852                 pir_reg = NIX_AF_TL4X_PIR(schq);
853                 break;
854         }
855
856         if (!cir_reg)
857                 return;
858         cfg = rvu_read64(rvu, blkaddr, cir_reg);
859         rvu_write64(rvu, blkaddr, cir_reg, cfg & ~BIT_ULL(0));
860
861         if (!pir_reg)
862                 return;
863         cfg = rvu_read64(rvu, blkaddr, pir_reg);
864         rvu_write64(rvu, blkaddr, pir_reg, cfg & ~BIT_ULL(0));
865 }
866
867 static void nix_reset_tx_linkcfg(struct rvu *rvu, int blkaddr,
868                                  int lvl, int schq)
869 {
870         struct rvu_hwinfo *hw = rvu->hw;
871         int link;
872
873         /* Reset TL4's SDP link config */
874         if (lvl == NIX_TXSCH_LVL_TL4)
875                 rvu_write64(rvu, blkaddr, NIX_AF_TL4X_SDP_LINK_CFG(schq), 0x00);
876
877         if (lvl != NIX_TXSCH_LVL_TL2)
878                 return;
879
880         /* Reset TL2's CGX or LBK link config */
881         for (link = 0; link < (hw->cgx_links + hw->lbk_links); link++)
882                 rvu_write64(rvu, blkaddr,
883                             NIX_AF_TL3_TL2X_LINKX_CFG(schq, link), 0x00);
884 }
885
886 int rvu_mbox_handler_NIX_TXSCH_ALLOC(struct rvu *rvu,
887                                      struct nix_txsch_alloc_req *req,
888                                      struct nix_txsch_alloc_rsp *rsp)
889 {
890         u16 pcifunc = req->hdr.pcifunc;
891         struct nix_txsch *txsch;
892         int lvl, idx, req_schq;
893         struct rvu_pfvf *pfvf;
894         struct nix_hw *nix_hw;
895         int blkaddr, rc = 0;
896         u16 schq;
897
898         pfvf = rvu_get_pfvf(rvu, pcifunc);
899         blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NIX, pcifunc);
900         if (!pfvf->nixlf || blkaddr < 0)
901                 return NIX_AF_ERR_AF_LF_INVALID;
902
903         nix_hw = get_nix_hw(rvu->hw, blkaddr);
904         if (!nix_hw)
905                 return -EINVAL;
906
907         spin_lock(&rvu->rsrc_lock);
908         for (lvl = 0; lvl < NIX_TXSCH_LVL_CNT; lvl++) {
909                 txsch = &nix_hw->txsch[lvl];
910                 req_schq = req->schq_contig[lvl] + req->schq[lvl];
911
912                 /* There are only 28 TL1s */
913                 if (lvl == NIX_TXSCH_LVL_TL1 && req_schq > txsch->schq.max)
914                         goto err;
915
916                 /* Check if request is valid */
917                 if (!req_schq || req_schq > MAX_TXSCHQ_PER_FUNC)
918                         goto err;
919
920                 /* If contiguous queues are needed, check for availability */
921                 if (req->schq_contig[lvl] &&
922                     !rvu_rsrc_check_contig(&txsch->schq, req->schq_contig[lvl]))
923                         goto err;
924
925                 /* Check if full request can be accommodated */
926                 if (req_schq >= rvu_rsrc_free_count(&txsch->schq))
927                         goto err;
928         }
929
930         for (lvl = 0; lvl < NIX_TXSCH_LVL_CNT; lvl++) {
931                 txsch = &nix_hw->txsch[lvl];
932                 rsp->schq_contig[lvl] = req->schq_contig[lvl];
933                 rsp->schq[lvl] = req->schq[lvl];
934
935                 schq = 0;
936                 /* Alloc contiguous queues first */
937                 if (req->schq_contig[lvl]) {
938                         schq = rvu_alloc_rsrc_contig(&txsch->schq,
939                                                      req->schq_contig[lvl]);
940
941                         for (idx = 0; idx < req->schq_contig[lvl]; idx++) {
942                                 txsch->pfvf_map[schq] = pcifunc;
943                                 nix_reset_tx_linkcfg(rvu, blkaddr, lvl, schq);
944                                 nix_reset_tx_shaping(rvu, blkaddr, lvl, schq);
945                                 rsp->schq_contig_list[lvl][idx] = schq;
946                                 schq++;
947                         }
948                 }
949
950                 /* Alloc non-contiguous queues */
951                 for (idx = 0; idx < req->schq[lvl]; idx++) {
952                         schq = rvu_alloc_rsrc(&txsch->schq);
953                         txsch->pfvf_map[schq] = pcifunc;
954                         nix_reset_tx_linkcfg(rvu, blkaddr, lvl, schq);
955                         nix_reset_tx_shaping(rvu, blkaddr, lvl, schq);
956                         rsp->schq_list[lvl][idx] = schq;
957                 }
958         }
959         goto exit;
960 err:
961         rc = NIX_AF_ERR_TLX_ALLOC_FAIL;
962 exit:
963         spin_unlock(&rvu->rsrc_lock);
964         return rc;
965 }
966
967 static int nix_txschq_free(struct rvu *rvu, u16 pcifunc)
968 {
969         int blkaddr, nixlf, lvl, schq, err;
970         struct rvu_hwinfo *hw = rvu->hw;
971         struct nix_txsch *txsch;
972         struct nix_hw *nix_hw;
973         u64 cfg;
974
975         blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NIX, pcifunc);
976         if (blkaddr < 0)
977                 return NIX_AF_ERR_AF_LF_INVALID;
978
979         nix_hw = get_nix_hw(rvu->hw, blkaddr);
980         if (!nix_hw)
981                 return -EINVAL;
982
983         nixlf = rvu_get_lf(rvu, &hw->block[blkaddr], pcifunc, 0);
984         if (nixlf < 0)
985                 return NIX_AF_ERR_AF_LF_INVALID;
986
987         /* Disable TL2/3 queue links before SMQ flush*/
988         spin_lock(&rvu->rsrc_lock);
989         for (lvl = NIX_TXSCH_LVL_TL4; lvl < NIX_TXSCH_LVL_CNT; lvl++) {
990                 if (lvl != NIX_TXSCH_LVL_TL2 && lvl != NIX_TXSCH_LVL_TL4)
991                         continue;
992
993                 txsch = &nix_hw->txsch[lvl];
994                 for (schq = 0; schq < txsch->schq.max; schq++) {
995                         if (txsch->pfvf_map[schq] != pcifunc)
996                                 continue;
997                         nix_reset_tx_linkcfg(rvu, blkaddr, lvl, schq);
998                 }
999         }
1000
1001         /* Flush SMQs */
1002         txsch = &nix_hw->txsch[NIX_TXSCH_LVL_SMQ];
1003         for (schq = 0; schq < txsch->schq.max; schq++) {
1004                 if (txsch->pfvf_map[schq] != pcifunc)
1005                         continue;
1006                 cfg = rvu_read64(rvu, blkaddr, NIX_AF_SMQX_CFG(schq));
1007                 /* Do SMQ flush and set enqueue xoff */
1008                 cfg |= BIT_ULL(50) | BIT_ULL(49);
1009                 rvu_write64(rvu, blkaddr, NIX_AF_SMQX_CFG(schq), cfg);
1010
1011                 /* Wait for flush to complete */
1012                 err = rvu_poll_reg(rvu, blkaddr,
1013                                    NIX_AF_SMQX_CFG(schq), BIT_ULL(49), true);
1014                 if (err) {
1015                         dev_err(rvu->dev,
1016                                 "NIXLF%d: SMQ%d flush failed\n", nixlf, schq);
1017                 }
1018         }
1019
1020         /* Now free scheduler queues to free pool */
1021         for (lvl = 0; lvl < NIX_TXSCH_LVL_CNT; lvl++) {
1022                 txsch = &nix_hw->txsch[lvl];
1023                 for (schq = 0; schq < txsch->schq.max; schq++) {
1024                         if (txsch->pfvf_map[schq] != pcifunc)
1025                                 continue;
1026                         rvu_free_rsrc(&txsch->schq, schq);
1027                         txsch->pfvf_map[schq] = 0;
1028                 }
1029         }
1030         spin_unlock(&rvu->rsrc_lock);
1031
1032         /* Sync cached info for this LF in NDC-TX to LLC/DRAM */
1033         rvu_write64(rvu, blkaddr, NIX_AF_NDC_TX_SYNC, BIT_ULL(12) | nixlf);
1034         err = rvu_poll_reg(rvu, blkaddr, NIX_AF_NDC_TX_SYNC, BIT_ULL(12), true);
1035         if (err)
1036                 dev_err(rvu->dev, "NDC-TX sync failed for NIXLF %d\n", nixlf);
1037
1038         return 0;
1039 }
1040
1041 int rvu_mbox_handler_NIX_TXSCH_FREE(struct rvu *rvu,
1042                                     struct nix_txsch_free_req *req,
1043                                     struct msg_rsp *rsp)
1044 {
1045         return nix_txschq_free(rvu, req->hdr.pcifunc);
1046 }
1047
1048 static bool is_txschq_config_valid(struct rvu *rvu, u16 pcifunc, int blkaddr,
1049                                    int lvl, u64 reg, u64 regval)
1050 {
1051         u64 regbase = reg & 0xFFFF;
1052         u16 schq, parent;
1053
1054         if (!rvu_check_valid_reg(TXSCHQ_HWREGMAP, lvl, reg))
1055                 return false;
1056
1057         schq = TXSCHQ_IDX(reg, TXSCHQ_IDX_SHIFT);
1058         /* Check if this schq belongs to this PF/VF or not */
1059         if (!is_valid_txschq(rvu, blkaddr, lvl, pcifunc, schq))
1060                 return false;
1061
1062         parent = (regval >> 16) & 0x1FF;
1063         /* Validate MDQ's TL4 parent */
1064         if (regbase == NIX_AF_MDQX_PARENT(0) &&
1065             !is_valid_txschq(rvu, blkaddr, NIX_TXSCH_LVL_TL4, pcifunc, parent))
1066                 return false;
1067
1068         /* Validate TL4's TL3 parent */
1069         if (regbase == NIX_AF_TL4X_PARENT(0) &&
1070             !is_valid_txschq(rvu, blkaddr, NIX_TXSCH_LVL_TL3, pcifunc, parent))
1071                 return false;
1072
1073         /* Validate TL3's TL2 parent */
1074         if (regbase == NIX_AF_TL3X_PARENT(0) &&
1075             !is_valid_txschq(rvu, blkaddr, NIX_TXSCH_LVL_TL2, pcifunc, parent))
1076                 return false;
1077
1078         /* Validate TL2's TL1 parent */
1079         if (regbase == NIX_AF_TL2X_PARENT(0) &&
1080             !is_valid_txschq(rvu, blkaddr, NIX_TXSCH_LVL_TL1, pcifunc, parent))
1081                 return false;
1082
1083         return true;
1084 }
1085
1086 int rvu_mbox_handler_NIX_TXSCHQ_CFG(struct rvu *rvu,
1087                                     struct nix_txschq_config *req,
1088                                     struct msg_rsp *rsp)
1089 {
1090         struct rvu_hwinfo *hw = rvu->hw;
1091         u16 pcifunc = req->hdr.pcifunc;
1092         u64 reg, regval, schq_regbase;
1093         struct nix_txsch *txsch;
1094         struct nix_hw *nix_hw;
1095         int blkaddr, idx, err;
1096         int nixlf;
1097
1098         if (req->lvl >= NIX_TXSCH_LVL_CNT ||
1099             req->num_regs > MAX_REGS_PER_MBOX_MSG)
1100                 return NIX_AF_INVAL_TXSCHQ_CFG;
1101
1102         blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NIX, pcifunc);
1103         if (blkaddr < 0)
1104                 return NIX_AF_ERR_AF_LF_INVALID;
1105
1106         nix_hw = get_nix_hw(rvu->hw, blkaddr);
1107         if (!nix_hw)
1108                 return -EINVAL;
1109
1110         nixlf = rvu_get_lf(rvu, &hw->block[blkaddr], pcifunc, 0);
1111         if (nixlf < 0)
1112                 return NIX_AF_ERR_AF_LF_INVALID;
1113
1114         txsch = &nix_hw->txsch[req->lvl];
1115         for (idx = 0; idx < req->num_regs; idx++) {
1116                 reg = req->reg[idx];
1117                 regval = req->regval[idx];
1118                 schq_regbase = reg & 0xFFFF;
1119
1120                 if (!is_txschq_config_valid(rvu, pcifunc, blkaddr,
1121                                             txsch->lvl, reg, regval))
1122                         return NIX_AF_INVAL_TXSCHQ_CFG;
1123
1124                 /* Replace PF/VF visible NIXLF slot with HW NIXLF id */
1125                 if (schq_regbase == NIX_AF_SMQX_CFG(0)) {
1126                         nixlf = rvu_get_lf(rvu, &hw->block[blkaddr],
1127                                            pcifunc, 0);
1128                         regval &= ~(0x7FULL << 24);
1129                         regval |= ((u64)nixlf << 24);
1130                 }
1131
1132                 rvu_write64(rvu, blkaddr, reg, regval);
1133
1134                 /* Check for SMQ flush, if so, poll for its completion */
1135                 if (schq_regbase == NIX_AF_SMQX_CFG(0) &&
1136                     (regval & BIT_ULL(49))) {
1137                         err = rvu_poll_reg(rvu, blkaddr,
1138                                            reg, BIT_ULL(49), true);
1139                         if (err)
1140                                 return NIX_AF_SMQ_FLUSH_FAILED;
1141                 }
1142         }
1143         return 0;
1144 }
1145
1146 static int nix_rx_vtag_cfg(struct rvu *rvu, int nixlf, int blkaddr,
1147                            struct nix_vtag_config *req)
1148 {
1149         u64 regval = 0;
1150
1151 #define NIX_VTAGTYPE_MAX 0x8ull
1152 #define NIX_VTAGSIZE_MASK 0x7ull
1153 #define NIX_VTAGSTRIP_CAP_MASK 0x30ull
1154
1155         if (req->rx.vtag_type >= NIX_VTAGTYPE_MAX ||
1156             req->vtag_size > VTAGSIZE_T8)
1157                 return -EINVAL;
1158
1159         regval = rvu_read64(rvu, blkaddr,
1160                             NIX_AF_LFX_RX_VTAG_TYPEX(nixlf, req->rx.vtag_type));
1161
1162         if (req->rx.strip_vtag && req->rx.capture_vtag)
1163                 regval |= BIT_ULL(4) | BIT_ULL(5);
1164         else if (req->rx.strip_vtag)
1165                 regval |= BIT_ULL(4);
1166         else
1167                 regval &= ~(BIT_ULL(4) | BIT_ULL(5));
1168
1169         regval &= ~NIX_VTAGSIZE_MASK;
1170         regval |= req->vtag_size & NIX_VTAGSIZE_MASK;
1171
1172         rvu_write64(rvu, blkaddr,
1173                     NIX_AF_LFX_RX_VTAG_TYPEX(nixlf, req->rx.vtag_type), regval);
1174         return 0;
1175 }
1176
1177 int rvu_mbox_handler_NIX_VTAG_CFG(struct rvu *rvu,
1178                                   struct nix_vtag_config *req,
1179                                   struct msg_rsp *rsp)
1180 {
1181         struct rvu_hwinfo *hw = rvu->hw;
1182         u16 pcifunc = req->hdr.pcifunc;
1183         int blkaddr, nixlf, err;
1184
1185         blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NIX, pcifunc);
1186         if (blkaddr < 0)
1187                 return NIX_AF_ERR_AF_LF_INVALID;
1188
1189         nixlf = rvu_get_lf(rvu, &hw->block[blkaddr], pcifunc, 0);
1190         if (nixlf < 0)
1191                 return NIX_AF_ERR_AF_LF_INVALID;
1192
1193         if (req->cfg_type) {
1194                 err = nix_rx_vtag_cfg(rvu, nixlf, blkaddr, req);
1195                 if (err)
1196                         return NIX_AF_ERR_PARAM;
1197         } else {
1198                 /* TODO: handle tx vtag configuration */
1199                 return 0;
1200         }
1201
1202         return 0;
1203 }
1204
1205 static int nix_setup_mce(struct rvu *rvu, int mce, u8 op,
1206                          u16 pcifunc, int next, bool eol)
1207 {
1208         struct nix_aq_enq_req aq_req;
1209         int err;
1210
1211         aq_req.hdr.pcifunc = pcifunc;
1212         aq_req.ctype = NIX_AQ_CTYPE_MCE;
1213         aq_req.op = op;
1214         aq_req.qidx = mce;
1215
1216         /* Forward bcast pkts to RQ0, RSS not needed */
1217         aq_req.mce.op = 0;
1218         aq_req.mce.index = 0;
1219         aq_req.mce.eol = eol;
1220         aq_req.mce.pf_func = pcifunc;
1221         aq_req.mce.next = next;
1222
1223         /* All fields valid */
1224         *(u64 *)(&aq_req.mce_mask) = ~0ULL;
1225
1226         err = rvu_nix_aq_enq_inst(rvu, &aq_req, NULL);
1227         if (err) {
1228                 dev_err(rvu->dev, "Failed to setup Bcast MCE for PF%d:VF%d\n",
1229                         rvu_get_pf(pcifunc), pcifunc & RVU_PFVF_FUNC_MASK);
1230                 return err;
1231         }
1232         return 0;
1233 }
1234
1235 static int nix_update_mce_list(struct nix_mce_list *mce_list,
1236                                u16 pcifunc, int idx, bool add)
1237 {
1238         struct mce *mce, *tail = NULL;
1239         bool delete = false;
1240
1241         /* Scan through the current list */
1242         hlist_for_each_entry(mce, &mce_list->head, node) {
1243                 /* If already exists, then delete */
1244                 if (mce->pcifunc == pcifunc && !add) {
1245                         delete = true;
1246                         break;
1247                 }
1248                 tail = mce;
1249         }
1250
1251         if (delete) {
1252                 hlist_del(&mce->node);
1253                 kfree(mce);
1254                 mce_list->count--;
1255                 return 0;
1256         }
1257
1258         if (!add)
1259                 return 0;
1260
1261         /* Add a new one to the list, at the tail */
1262         mce = kzalloc(sizeof(*mce), GFP_KERNEL);
1263         if (!mce)
1264                 return -ENOMEM;
1265         mce->idx = idx;
1266         mce->pcifunc = pcifunc;
1267         if (!tail)
1268                 hlist_add_head(&mce->node, &mce_list->head);
1269         else
1270                 hlist_add_behind(&mce->node, &tail->node);
1271         mce_list->count++;
1272         return 0;
1273 }
1274
1275 static int nix_update_bcast_mce_list(struct rvu *rvu, u16 pcifunc, bool add)
1276 {
1277         int err = 0, idx, next_idx, count;
1278         struct nix_mce_list *mce_list;
1279         struct mce *mce, *next_mce;
1280         struct nix_mcast *mcast;
1281         struct nix_hw *nix_hw;
1282         struct rvu_pfvf *pfvf;
1283         int blkaddr;
1284
1285         blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NIX, pcifunc);
1286         if (blkaddr < 0)
1287                 return 0;
1288
1289         nix_hw = get_nix_hw(rvu->hw, blkaddr);
1290         if (!nix_hw)
1291                 return 0;
1292
1293         mcast = &nix_hw->mcast;
1294
1295         /* Get this PF/VF func's MCE index */
1296         pfvf = rvu_get_pfvf(rvu, pcifunc & ~RVU_PFVF_FUNC_MASK);
1297         idx = pfvf->bcast_mce_idx + (pcifunc & RVU_PFVF_FUNC_MASK);
1298
1299         mce_list = &pfvf->bcast_mce_list;
1300         if (idx > (pfvf->bcast_mce_idx + mce_list->max)) {
1301                 dev_err(rvu->dev,
1302                         "%s: Idx %d > max MCE idx %d, for PF%d bcast list\n",
1303                         __func__, idx, mce_list->max,
1304                         pcifunc >> RVU_PFVF_PF_SHIFT);
1305                 return -EINVAL;
1306         }
1307
1308         spin_lock(&mcast->mce_lock);
1309
1310         err = nix_update_mce_list(mce_list, pcifunc, idx, add);
1311         if (err)
1312                 goto end;
1313
1314         /* Disable MCAM entry in NPC */
1315
1316         if (!mce_list->count)
1317                 goto end;
1318         count = mce_list->count;
1319
1320         /* Dump the updated list to HW */
1321         hlist_for_each_entry(mce, &mce_list->head, node) {
1322                 next_idx = 0;
1323                 count--;
1324                 if (count) {
1325                         next_mce = hlist_entry(mce->node.next,
1326                                                struct mce, node);
1327                         next_idx = next_mce->idx;
1328                 }
1329                 /* EOL should be set in last MCE */
1330                 err = nix_setup_mce(rvu, mce->idx,
1331                                     NIX_AQ_INSTOP_WRITE, mce->pcifunc,
1332                                     next_idx, count ? false : true);
1333                 if (err)
1334                         goto end;
1335         }
1336
1337 end:
1338         spin_unlock(&mcast->mce_lock);
1339         return err;
1340 }
1341
1342 static int nix_setup_bcast_tables(struct rvu *rvu, struct nix_hw *nix_hw)
1343 {
1344         struct nix_mcast *mcast = &nix_hw->mcast;
1345         int err, pf, numvfs, idx;
1346         struct rvu_pfvf *pfvf;
1347         u16 pcifunc;
1348         u64 cfg;
1349
1350         /* Skip PF0 (i.e AF) */
1351         for (pf = 1; pf < (rvu->cgx_mapped_pfs + 1); pf++) {
1352                 cfg = rvu_read64(rvu, BLKADDR_RVUM, RVU_PRIV_PFX_CFG(pf));
1353                 /* If PF is not enabled, nothing to do */
1354                 if (!((cfg >> 20) & 0x01))
1355                         continue;
1356                 /* Get numVFs attached to this PF */
1357                 numvfs = (cfg >> 12) & 0xFF;
1358
1359                 pfvf = &rvu->pf[pf];
1360                 /* Save the start MCE */
1361                 pfvf->bcast_mce_idx = nix_alloc_mce_list(mcast, numvfs + 1);
1362
1363                 nix_mce_list_init(&pfvf->bcast_mce_list, numvfs + 1);
1364
1365                 for (idx = 0; idx < (numvfs + 1); idx++) {
1366                         /* idx-0 is for PF, followed by VFs */
1367                         pcifunc = (pf << RVU_PFVF_PF_SHIFT);
1368                         pcifunc |= idx;
1369                         /* Add dummy entries now, so that we don't have to check
1370                          * for whether AQ_OP should be INIT/WRITE later on.
1371                          * Will be updated when a NIXLF is attached/detached to
1372                          * these PF/VFs.
1373                          */
1374                         err = nix_setup_mce(rvu, pfvf->bcast_mce_idx + idx,
1375                                             NIX_AQ_INSTOP_INIT,
1376                                             pcifunc, 0, true);
1377                         if (err)
1378                                 return err;
1379                 }
1380         }
1381         return 0;
1382 }
1383
1384 static int nix_setup_mcast(struct rvu *rvu, struct nix_hw *nix_hw, int blkaddr)
1385 {
1386         struct nix_mcast *mcast = &nix_hw->mcast;
1387         struct rvu_hwinfo *hw = rvu->hw;
1388         int err, size;
1389
1390         size = (rvu_read64(rvu, blkaddr, NIX_AF_CONST3) >> 16) & 0x0F;
1391         size = (1ULL << size);
1392
1393         /* Alloc memory for multicast/mirror replication entries */
1394         err = qmem_alloc(rvu->dev, &mcast->mce_ctx,
1395                          (256UL << MC_TBL_SIZE), size);
1396         if (err)
1397                 return -ENOMEM;
1398
1399         rvu_write64(rvu, blkaddr, NIX_AF_RX_MCAST_BASE,
1400                     (u64)mcast->mce_ctx->iova);
1401
1402         /* Set max list length equal to max no of VFs per PF  + PF itself */
1403         rvu_write64(rvu, blkaddr, NIX_AF_RX_MCAST_CFG,
1404                     BIT_ULL(36) | (hw->max_vfs_per_pf << 4) | MC_TBL_SIZE);
1405
1406         /* Alloc memory for multicast replication buffers */
1407         size = rvu_read64(rvu, blkaddr, NIX_AF_MC_MIRROR_CONST) & 0xFFFF;
1408         err = qmem_alloc(rvu->dev, &mcast->mcast_buf,
1409                          (8UL << MC_BUF_CNT), size);
1410         if (err)
1411                 return -ENOMEM;
1412
1413         rvu_write64(rvu, blkaddr, NIX_AF_RX_MCAST_BUF_BASE,
1414                     (u64)mcast->mcast_buf->iova);
1415
1416         /* Alloc pkind for NIX internal RX multicast/mirror replay */
1417         mcast->replay_pkind = rvu_alloc_rsrc(&hw->pkind.rsrc);
1418
1419         rvu_write64(rvu, blkaddr, NIX_AF_RX_MCAST_BUF_CFG,
1420                     BIT_ULL(63) | (mcast->replay_pkind << 24) |
1421                     BIT_ULL(20) | MC_BUF_CNT);
1422
1423         spin_lock_init(&mcast->mce_lock);
1424
1425         return nix_setup_bcast_tables(rvu, nix_hw);
1426 }
1427
1428 static int nix_setup_txschq(struct rvu *rvu, struct nix_hw *nix_hw, int blkaddr)
1429 {
1430         struct nix_txsch *txsch;
1431         u64 cfg, reg;
1432         int err, lvl;
1433
1434         /* Get scheduler queue count of each type and alloc
1435          * bitmap for each for alloc/free/attach operations.
1436          */
1437         for (lvl = 0; lvl < NIX_TXSCH_LVL_CNT; lvl++) {
1438                 txsch = &nix_hw->txsch[lvl];
1439                 txsch->lvl = lvl;
1440                 switch (lvl) {
1441                 case NIX_TXSCH_LVL_SMQ:
1442                         reg = NIX_AF_MDQ_CONST;
1443                         break;
1444                 case NIX_TXSCH_LVL_TL4:
1445                         reg = NIX_AF_TL4_CONST;
1446                         break;
1447                 case NIX_TXSCH_LVL_TL3:
1448                         reg = NIX_AF_TL3_CONST;
1449                         break;
1450                 case NIX_TXSCH_LVL_TL2:
1451                         reg = NIX_AF_TL2_CONST;
1452                         break;
1453                 case NIX_TXSCH_LVL_TL1:
1454                         reg = NIX_AF_TL1_CONST;
1455                         break;
1456                 }
1457                 cfg = rvu_read64(rvu, blkaddr, reg);
1458                 txsch->schq.max = cfg & 0xFFFF;
1459                 err = rvu_alloc_bitmap(&txsch->schq);
1460                 if (err)
1461                         return err;
1462
1463                 /* Allocate memory for scheduler queues to
1464                  * PF/VF pcifunc mapping info.
1465                  */
1466                 txsch->pfvf_map = devm_kcalloc(rvu->dev, txsch->schq.max,
1467                                                sizeof(u16), GFP_KERNEL);
1468                 if (!txsch->pfvf_map)
1469                         return -ENOMEM;
1470         }
1471         return 0;
1472 }
1473
1474 int rvu_mbox_handler_NIX_STATS_RST(struct rvu *rvu, struct msg_req *req,
1475                                    struct msg_rsp *rsp)
1476 {
1477         struct rvu_hwinfo *hw = rvu->hw;
1478         u16 pcifunc = req->hdr.pcifunc;
1479         int i, nixlf, blkaddr;
1480         u64 stats;
1481
1482         blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NIX, pcifunc);
1483         if (blkaddr < 0)
1484                 return NIX_AF_ERR_AF_LF_INVALID;
1485
1486         nixlf = rvu_get_lf(rvu, &hw->block[blkaddr], pcifunc, 0);
1487         if (nixlf < 0)
1488                 return NIX_AF_ERR_AF_LF_INVALID;
1489
1490         /* Get stats count supported by HW */
1491         stats = rvu_read64(rvu, blkaddr, NIX_AF_CONST1);
1492
1493         /* Reset tx stats */
1494         for (i = 0; i < ((stats >> 24) & 0xFF); i++)
1495                 rvu_write64(rvu, blkaddr, NIX_AF_LFX_TX_STATX(nixlf, i), 0);
1496
1497         /* Reset rx stats */
1498         for (i = 0; i < ((stats >> 32) & 0xFF); i++)
1499                 rvu_write64(rvu, blkaddr, NIX_AF_LFX_RX_STATX(nixlf, i), 0);
1500
1501         return 0;
1502 }
1503
1504 static int nix_calibrate_x2p(struct rvu *rvu, int blkaddr)
1505 {
1506         int idx, err;
1507         u64 status;
1508
1509         /* Start X2P bus calibration */
1510         rvu_write64(rvu, blkaddr, NIX_AF_CFG,
1511                     rvu_read64(rvu, blkaddr, NIX_AF_CFG) | BIT_ULL(9));
1512         /* Wait for calibration to complete */
1513         err = rvu_poll_reg(rvu, blkaddr,
1514                            NIX_AF_STATUS, BIT_ULL(10), false);
1515         if (err) {
1516                 dev_err(rvu->dev, "NIX X2P bus calibration failed\n");
1517                 return err;
1518         }
1519
1520         status = rvu_read64(rvu, blkaddr, NIX_AF_STATUS);
1521         /* Check if CGX devices are ready */
1522         for (idx = 0; idx < cgx_get_cgx_cnt(); idx++) {
1523                 if (status & (BIT_ULL(16 + idx)))
1524                         continue;
1525                 dev_err(rvu->dev,
1526                         "CGX%d didn't respond to NIX X2P calibration\n", idx);
1527                 err = -EBUSY;
1528         }
1529
1530         /* Check if LBK is ready */
1531         if (!(status & BIT_ULL(19))) {
1532                 dev_err(rvu->dev,
1533                         "LBK didn't respond to NIX X2P calibration\n");
1534                 err = -EBUSY;
1535         }
1536
1537         /* Clear 'calibrate_x2p' bit */
1538         rvu_write64(rvu, blkaddr, NIX_AF_CFG,
1539                     rvu_read64(rvu, blkaddr, NIX_AF_CFG) & ~BIT_ULL(9));
1540         if (err || (status & 0x3FFULL))
1541                 dev_err(rvu->dev,
1542                         "NIX X2P calibration failed, status 0x%llx\n", status);
1543         if (err)
1544                 return err;
1545         return 0;
1546 }
1547
1548 static int nix_aq_init(struct rvu *rvu, struct rvu_block *block)
1549 {
1550         u64 cfg;
1551         int err;
1552
1553         /* Set admin queue endianness */
1554         cfg = rvu_read64(rvu, block->addr, NIX_AF_CFG);
1555 #ifdef __BIG_ENDIAN
1556         cfg |= BIT_ULL(1);
1557         rvu_write64(rvu, block->addr, NIX_AF_CFG, cfg);
1558 #else
1559         cfg &= ~BIT_ULL(1);
1560         rvu_write64(rvu, block->addr, NIX_AF_CFG, cfg);
1561 #endif
1562
1563         /* Do not bypass NDC cache */
1564         cfg = rvu_read64(rvu, block->addr, NIX_AF_NDC_CFG);
1565         cfg &= ~0x3FFEULL;
1566         rvu_write64(rvu, block->addr, NIX_AF_NDC_CFG, cfg);
1567
1568         /* Result structure can be followed by RQ/SQ/CQ context at
1569          * RES + 128bytes and a write mask at RES + 256 bytes, depending on
1570          * operation type. Alloc sufficient result memory for all operations.
1571          */
1572         err = rvu_aq_alloc(rvu, &block->aq,
1573                            Q_COUNT(AQ_SIZE), sizeof(struct nix_aq_inst_s),
1574                            ALIGN(sizeof(struct nix_aq_res_s), 128) + 256);
1575         if (err)
1576                 return err;
1577
1578         rvu_write64(rvu, block->addr, NIX_AF_AQ_CFG, AQ_SIZE);
1579         rvu_write64(rvu, block->addr,
1580                     NIX_AF_AQ_BASE, (u64)block->aq->inst->iova);
1581         return 0;
1582 }
1583
1584 int rvu_nix_init(struct rvu *rvu)
1585 {
1586         struct rvu_hwinfo *hw = rvu->hw;
1587         struct rvu_block *block;
1588         int blkaddr, err;
1589         u64 cfg;
1590
1591         blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NIX, 0);
1592         if (blkaddr < 0)
1593                 return 0;
1594         block = &hw->block[blkaddr];
1595
1596         /* Calibrate X2P bus to check if CGX/LBK links are fine */
1597         err = nix_calibrate_x2p(rvu, blkaddr);
1598         if (err)
1599                 return err;
1600
1601         /* Set num of links of each type */
1602         cfg = rvu_read64(rvu, blkaddr, NIX_AF_CONST);
1603         hw->cgx = (cfg >> 12) & 0xF;
1604         hw->lmac_per_cgx = (cfg >> 8) & 0xF;
1605         hw->cgx_links = hw->cgx * hw->lmac_per_cgx;
1606         hw->lbk_links = 1;
1607         hw->sdp_links = 1;
1608
1609         /* Initialize admin queue */
1610         err = nix_aq_init(rvu, block);
1611         if (err)
1612                 return err;
1613
1614         /* Restore CINT timer delay to HW reset values */
1615         rvu_write64(rvu, blkaddr, NIX_AF_CINT_DELAY, 0x0ULL);
1616
1617         /* Configure segmentation offload formats */
1618         nix_setup_lso(rvu, blkaddr);
1619
1620         if (blkaddr == BLKADDR_NIX0) {
1621                 hw->nix0 = devm_kzalloc(rvu->dev,
1622                                         sizeof(struct nix_hw), GFP_KERNEL);
1623                 if (!hw->nix0)
1624                         return -ENOMEM;
1625
1626                 err = nix_setup_txschq(rvu, hw->nix0, blkaddr);
1627                 if (err)
1628                         return err;
1629
1630                 err = nix_setup_mcast(rvu, hw->nix0, blkaddr);
1631                 if (err)
1632                         return err;
1633         }
1634         return 0;
1635 }
1636
1637 void rvu_nix_freemem(struct rvu *rvu)
1638 {
1639         struct rvu_hwinfo *hw = rvu->hw;
1640         struct rvu_block *block;
1641         struct nix_txsch *txsch;
1642         struct nix_mcast *mcast;
1643         struct nix_hw *nix_hw;
1644         int blkaddr, lvl;
1645
1646         blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NIX, 0);
1647         if (blkaddr < 0)
1648                 return;
1649
1650         block = &hw->block[blkaddr];
1651         rvu_aq_free(rvu, block->aq);
1652
1653         if (blkaddr == BLKADDR_NIX0) {
1654                 nix_hw = get_nix_hw(rvu->hw, blkaddr);
1655                 if (!nix_hw)
1656                         return;
1657
1658                 for (lvl = 0; lvl < NIX_TXSCH_LVL_CNT; lvl++) {
1659                         txsch = &nix_hw->txsch[lvl];
1660                         kfree(txsch->schq.bmap);
1661                 }
1662
1663                 mcast = &nix_hw->mcast;
1664                 qmem_free(rvu->dev, mcast->mce_ctx);
1665                 qmem_free(rvu->dev, mcast->mcast_buf);
1666         }
1667 }