1 // SPDX-License-Identifier: GPL-2.0
2 /* Marvell RVU Ethernet driver
3 *
4 * Copyright (C) 2020 Marvell.
5 *
6 */
7
8 #include <linux/interrupt.h>
9 #include <linux/pci.h>
10 #include <net/page_pool/helpers.h>
11 #include <net/tso.h>
12 #include <linux/bitfield.h>
13 #include <linux/dcbnl.h>
14 #include <net/xfrm.h>
15
16 #include "otx2_reg.h"
17 #include "otx2_common.h"
18 #include "otx2_struct.h"
19 #include "cn10k.h"
20 #include "otx2_xsk.h"
21
otx2_is_pfc_enabled(struct otx2_nic * pfvf)22 static bool otx2_is_pfc_enabled(struct otx2_nic *pfvf)
23 {
24 return IS_ENABLED(CONFIG_DCB) && !!pfvf->pfc_en;
25 }
26
otx2_nix_rq_op_stats(struct queue_stats * stats,struct otx2_nic * pfvf,int qidx)27 static void otx2_nix_rq_op_stats(struct queue_stats *stats,
28 struct otx2_nic *pfvf, int qidx)
29 {
30 u64 incr = (u64)qidx << 32;
31 void __iomem *ptr;
32
33 ptr = otx2_get_regaddr(pfvf, NIX_LF_RQ_OP_OCTS);
34 stats->bytes = otx2_atomic64_add(incr, ptr);
35
36 ptr = otx2_get_regaddr(pfvf, NIX_LF_RQ_OP_PKTS);
37 stats->pkts = otx2_atomic64_add(incr, ptr);
38 }
39
otx2_nix_sq_op_stats(struct queue_stats * stats,struct otx2_nic * pfvf,int qidx)40 static void otx2_nix_sq_op_stats(struct queue_stats *stats,
41 struct otx2_nic *pfvf, int qidx)
42 {
43 u64 incr = (u64)qidx << 32;
44 void __iomem *ptr;
45
46 ptr = otx2_get_regaddr(pfvf, NIX_LF_SQ_OP_OCTS);
47 stats->bytes = otx2_atomic64_add(incr, ptr);
48
49 ptr = otx2_get_regaddr(pfvf, NIX_LF_SQ_OP_PKTS);
50 stats->pkts = otx2_atomic64_add(incr, ptr);
51 }
52
otx2_update_lmac_stats(struct otx2_nic * pfvf)53 void otx2_update_lmac_stats(struct otx2_nic *pfvf)
54 {
55 struct msg_req *req;
56
57 if (!netif_running(pfvf->netdev))
58 return;
59
60 mutex_lock(&pfvf->mbox.lock);
61 req = otx2_mbox_alloc_msg_cgx_stats(&pfvf->mbox);
62 if (!req) {
63 mutex_unlock(&pfvf->mbox.lock);
64 return;
65 }
66
67 otx2_sync_mbox_msg(&pfvf->mbox);
68 mutex_unlock(&pfvf->mbox.lock);
69 }
70
otx2_update_lmac_fec_stats(struct otx2_nic * pfvf)71 void otx2_update_lmac_fec_stats(struct otx2_nic *pfvf)
72 {
73 struct msg_req *req;
74
75 if (!netif_running(pfvf->netdev))
76 return;
77 mutex_lock(&pfvf->mbox.lock);
78 req = otx2_mbox_alloc_msg_cgx_fec_stats(&pfvf->mbox);
79 if (req)
80 otx2_sync_mbox_msg(&pfvf->mbox);
81 mutex_unlock(&pfvf->mbox.lock);
82 }
83
otx2_update_rq_stats(struct otx2_nic * pfvf,int qidx)84 int otx2_update_rq_stats(struct otx2_nic *pfvf, int qidx)
85 {
86 struct otx2_rcv_queue *rq = &pfvf->qset.rq[qidx];
87
88 if (!pfvf->qset.rq)
89 return 0;
90
91 otx2_nix_rq_op_stats(&rq->stats, pfvf, qidx);
92 return 1;
93 }
94 EXPORT_SYMBOL(otx2_update_rq_stats);
95
otx2_update_sq_stats(struct otx2_nic * pfvf,int qidx)96 int otx2_update_sq_stats(struct otx2_nic *pfvf, int qidx)
97 {
98 struct otx2_snd_queue *sq = &pfvf->qset.sq[qidx];
99
100 if (!pfvf->qset.sq)
101 return 0;
102
103 if (qidx >= pfvf->hw.non_qos_queues) {
104 if (!test_bit(qidx - pfvf->hw.non_qos_queues, pfvf->qos.qos_sq_bmap))
105 return 0;
106 }
107
108 otx2_nix_sq_op_stats(&sq->stats, pfvf, qidx);
109 return 1;
110 }
111 EXPORT_SYMBOL(otx2_update_sq_stats);
112
otx2_get_dev_stats(struct otx2_nic * pfvf)113 void otx2_get_dev_stats(struct otx2_nic *pfvf)
114 {
115 struct otx2_dev_stats *dev_stats = &pfvf->hw.dev_stats;
116
117 dev_stats->rx_bytes = OTX2_GET_RX_STATS(RX_OCTS);
118 dev_stats->rx_drops = OTX2_GET_RX_STATS(RX_DROP);
119 dev_stats->rx_bcast_frames = OTX2_GET_RX_STATS(RX_BCAST);
120 dev_stats->rx_mcast_frames = OTX2_GET_RX_STATS(RX_MCAST);
121 dev_stats->rx_ucast_frames = OTX2_GET_RX_STATS(RX_UCAST);
122 dev_stats->rx_frames = dev_stats->rx_bcast_frames +
123 dev_stats->rx_mcast_frames +
124 dev_stats->rx_ucast_frames;
125
126 dev_stats->tx_bytes = OTX2_GET_TX_STATS(TX_OCTS);
127 dev_stats->tx_drops = OTX2_GET_TX_STATS(TX_DROP) +
128 (unsigned long)atomic_long_read(&dev_stats->tx_discards);
129
130 dev_stats->tx_bcast_frames = OTX2_GET_TX_STATS(TX_BCAST);
131 dev_stats->tx_mcast_frames = OTX2_GET_TX_STATS(TX_MCAST);
132 dev_stats->tx_ucast_frames = OTX2_GET_TX_STATS(TX_UCAST);
133 dev_stats->tx_frames = dev_stats->tx_bcast_frames +
134 dev_stats->tx_mcast_frames +
135 dev_stats->tx_ucast_frames;
136 }
137
otx2_get_stats64(struct net_device * netdev,struct rtnl_link_stats64 * stats)138 void otx2_get_stats64(struct net_device *netdev,
139 struct rtnl_link_stats64 *stats)
140 {
141 struct otx2_nic *pfvf = netdev_priv(netdev);
142 struct otx2_dev_stats *dev_stats;
143
144 otx2_get_dev_stats(pfvf);
145
146 dev_stats = &pfvf->hw.dev_stats;
147 stats->rx_bytes = dev_stats->rx_bytes;
148 stats->rx_packets = dev_stats->rx_frames;
149 stats->rx_dropped = dev_stats->rx_drops;
150 stats->multicast = dev_stats->rx_mcast_frames;
151
152 stats->tx_bytes = dev_stats->tx_bytes;
153 stats->tx_packets = dev_stats->tx_frames;
154 stats->tx_dropped = dev_stats->tx_drops;
155 }
156 EXPORT_SYMBOL(otx2_get_stats64);
157
158 /* Sync MAC address with RVU AF */
otx2_hw_set_mac_addr(struct otx2_nic * pfvf,u8 * mac)159 static int otx2_hw_set_mac_addr(struct otx2_nic *pfvf, u8 *mac)
160 {
161 struct nix_set_mac_addr *req;
162 int err;
163
164 mutex_lock(&pfvf->mbox.lock);
165 req = otx2_mbox_alloc_msg_nix_set_mac_addr(&pfvf->mbox);
166 if (!req) {
167 mutex_unlock(&pfvf->mbox.lock);
168 return -ENOMEM;
169 }
170
171 ether_addr_copy(req->mac_addr, mac);
172
173 err = otx2_sync_mbox_msg(&pfvf->mbox);
174 mutex_unlock(&pfvf->mbox.lock);
175 return err;
176 }
177
otx2_hw_get_mac_addr(struct otx2_nic * pfvf,struct net_device * netdev)178 static int otx2_hw_get_mac_addr(struct otx2_nic *pfvf,
179 struct net_device *netdev)
180 {
181 struct nix_get_mac_addr_rsp *rsp;
182 struct mbox_msghdr *msghdr;
183 struct msg_req *req;
184 int err;
185
186 mutex_lock(&pfvf->mbox.lock);
187 req = otx2_mbox_alloc_msg_nix_get_mac_addr(&pfvf->mbox);
188 if (!req) {
189 mutex_unlock(&pfvf->mbox.lock);
190 return -ENOMEM;
191 }
192
193 err = otx2_sync_mbox_msg(&pfvf->mbox);
194 if (err) {
195 mutex_unlock(&pfvf->mbox.lock);
196 return err;
197 }
198
199 msghdr = otx2_mbox_get_rsp(&pfvf->mbox.mbox, 0, &req->hdr);
200 if (IS_ERR(msghdr)) {
201 mutex_unlock(&pfvf->mbox.lock);
202 return PTR_ERR(msghdr);
203 }
204 rsp = (struct nix_get_mac_addr_rsp *)msghdr;
205 eth_hw_addr_set(netdev, rsp->mac_addr);
206 mutex_unlock(&pfvf->mbox.lock);
207
208 return 0;
209 }
210
otx2_set_mac_address(struct net_device * netdev,void * p)211 int otx2_set_mac_address(struct net_device *netdev, void *p)
212 {
213 struct otx2_nic *pfvf = netdev_priv(netdev);
214 struct sockaddr *addr = p;
215
216 if (!is_valid_ether_addr(addr->sa_data))
217 return -EADDRNOTAVAIL;
218
219 if (!otx2_hw_set_mac_addr(pfvf, addr->sa_data)) {
220 eth_hw_addr_set(netdev, addr->sa_data);
221 /* update dmac field in vlan offload rule */
222 if (netif_running(netdev) &&
223 pfvf->flags & OTX2_FLAG_RX_VLAN_SUPPORT)
224 otx2_install_rxvlan_offload_flow(pfvf);
225 /* update dmac address in ntuple and DMAC filter list */
226 if (pfvf->flags & OTX2_FLAG_DMACFLTR_SUPPORT)
227 otx2_dmacflt_update_pfmac_flow(pfvf);
228 } else {
229 return -EPERM;
230 }
231
232 return 0;
233 }
234 EXPORT_SYMBOL(otx2_set_mac_address);
235
otx2_hw_set_mtu(struct otx2_nic * pfvf,int mtu)236 int otx2_hw_set_mtu(struct otx2_nic *pfvf, int mtu)
237 {
238 struct nix_frs_cfg *req;
239 u16 maxlen;
240 int err;
241
242 maxlen = pfvf->hw.max_mtu + OTX2_ETH_HLEN + OTX2_HW_TIMESTAMP_LEN;
243
244 mutex_lock(&pfvf->mbox.lock);
245 req = otx2_mbox_alloc_msg_nix_set_hw_frs(&pfvf->mbox);
246 if (!req) {
247 mutex_unlock(&pfvf->mbox.lock);
248 return -ENOMEM;
249 }
250
251 req->maxlen = mtu + OTX2_ETH_HLEN + OTX2_HW_TIMESTAMP_LEN;
252
253 /* Use max receive length supported by hardware for loopback devices */
254 if (is_otx2_lbkvf(pfvf->pdev))
255 req->maxlen = maxlen;
256
257 err = otx2_sync_mbox_msg(&pfvf->mbox);
258 mutex_unlock(&pfvf->mbox.lock);
259 return err;
260 }
261 EXPORT_SYMBOL(otx2_hw_set_mtu);
262
otx2_config_pause_frm(struct otx2_nic * pfvf)263 int otx2_config_pause_frm(struct otx2_nic *pfvf)
264 {
265 struct cgx_pause_frm_cfg *req;
266 int err;
267
268 if (is_otx2_lbkvf(pfvf->pdev) || is_otx2_sdp_rep(pfvf->pdev))
269 return 0;
270
271 mutex_lock(&pfvf->mbox.lock);
272 req = otx2_mbox_alloc_msg_cgx_cfg_pause_frm(&pfvf->mbox);
273 if (!req) {
274 err = -ENOMEM;
275 goto unlock;
276 }
277
278 req->rx_pause = !!(pfvf->flags & OTX2_FLAG_RX_PAUSE_ENABLED);
279 req->tx_pause = !!(pfvf->flags & OTX2_FLAG_TX_PAUSE_ENABLED);
280 req->set = 1;
281
282 err = otx2_sync_mbox_msg(&pfvf->mbox);
283 unlock:
284 mutex_unlock(&pfvf->mbox.lock);
285 return err;
286 }
287 EXPORT_SYMBOL(otx2_config_pause_frm);
288
otx2_set_flowkey_cfg(struct otx2_nic * pfvf)289 int otx2_set_flowkey_cfg(struct otx2_nic *pfvf)
290 {
291 struct otx2_rss_info *rss = &pfvf->hw.rss_info;
292 struct nix_rss_flowkey_cfg_rsp *rsp;
293 struct nix_rss_flowkey_cfg *req;
294 int err;
295
296 mutex_lock(&pfvf->mbox.lock);
297 req = otx2_mbox_alloc_msg_nix_rss_flowkey_cfg(&pfvf->mbox);
298 if (!req) {
299 mutex_unlock(&pfvf->mbox.lock);
300 return -ENOMEM;
301 }
302 req->mcam_index = -1; /* Default or reserved index */
303 req->flowkey_cfg = rss->flowkey_cfg;
304 req->group = DEFAULT_RSS_CONTEXT_GROUP;
305
306 err = otx2_sync_mbox_msg(&pfvf->mbox);
307 if (err)
308 goto fail;
309
310 rsp = (struct nix_rss_flowkey_cfg_rsp *)
311 otx2_mbox_get_rsp(&pfvf->mbox.mbox, 0, &req->hdr);
312 if (IS_ERR(rsp)) {
313 err = PTR_ERR(rsp);
314 goto fail;
315 }
316
317 pfvf->hw.flowkey_alg_idx = rsp->alg_idx;
318 fail:
319 mutex_unlock(&pfvf->mbox.lock);
320 return err;
321 }
322
otx2_set_rss_table(struct otx2_nic * pfvf,int ctx_id,const u32 * ind_tbl)323 int otx2_set_rss_table(struct otx2_nic *pfvf, int ctx_id, const u32 *ind_tbl)
324 {
325 struct otx2_rss_info *rss = &pfvf->hw.rss_info;
326 const int index = rss->rss_size * ctx_id;
327 struct mbox *mbox = &pfvf->mbox;
328 struct nix_aq_enq_req *aq;
329 int idx, err;
330
331 mutex_lock(&mbox->lock);
332 ind_tbl = ind_tbl ?: rss->ind_tbl;
333 /* Get memory to put this msg */
334 for (idx = 0; idx < rss->rss_size; idx++) {
335 /* Ignore the queue if AF_XDP zero copy is enabled */
336 if (pfvf->af_xdp_zc_qidx &&
337 test_bit(ind_tbl[idx], pfvf->af_xdp_zc_qidx))
338 continue;
339
340 aq = otx2_mbox_alloc_msg_nix_aq_enq(mbox);
341 if (!aq) {
342 /* The shared memory buffer can be full.
343 * Flush it and retry
344 */
345 err = otx2_sync_mbox_msg(mbox);
346 if (err) {
347 mutex_unlock(&mbox->lock);
348 return err;
349 }
350 aq = otx2_mbox_alloc_msg_nix_aq_enq(mbox);
351 if (!aq) {
352 mutex_unlock(&mbox->lock);
353 return -ENOMEM;
354 }
355 }
356
357 aq->rss.rq = ind_tbl[idx];
358
359 /* Fill AQ info */
360 aq->qidx = index + idx;
361 aq->ctype = NIX_AQ_CTYPE_RSS;
362 aq->op = NIX_AQ_INSTOP_INIT;
363 }
364 err = otx2_sync_mbox_msg(mbox);
365 mutex_unlock(&mbox->lock);
366 return err;
367 }
368
otx2_set_rss_key(struct otx2_nic * pfvf)369 void otx2_set_rss_key(struct otx2_nic *pfvf)
370 {
371 struct otx2_rss_info *rss = &pfvf->hw.rss_info;
372 u64 *key = (u64 *)&rss->key[4];
373 int idx;
374
375 /* 352bit or 44byte key needs to be configured as below
376 * NIX_LF_RX_SECRETX0 = key<351:288>
377 * NIX_LF_RX_SECRETX1 = key<287:224>
378 * NIX_LF_RX_SECRETX2 = key<223:160>
379 * NIX_LF_RX_SECRETX3 = key<159:96>
380 * NIX_LF_RX_SECRETX4 = key<95:32>
381 * NIX_LF_RX_SECRETX5<63:32> = key<31:0>
382 */
383 otx2_write64(pfvf, NIX_LF_RX_SECRETX(5),
384 (u64)(*((u32 *)&rss->key)) << 32);
385 idx = sizeof(rss->key) / sizeof(u64);
386 while (idx > 0) {
387 idx--;
388 otx2_write64(pfvf, NIX_LF_RX_SECRETX(idx), *key++);
389 }
390 }
391
otx2_rss_init(struct otx2_nic * pfvf)392 int otx2_rss_init(struct otx2_nic *pfvf)
393 {
394 struct otx2_rss_info *rss = &pfvf->hw.rss_info;
395 int idx, ret = 0;
396
397 rss->rss_size = ARRAY_SIZE(rss->ind_tbl);
398
399 /* Init RSS key if it is not setup already */
400 if (!rss->enable)
401 netdev_rss_key_fill(rss->key, sizeof(rss->key));
402 otx2_set_rss_key(pfvf);
403
404 if (!netif_is_rxfh_configured(pfvf->netdev))
405 for (idx = 0; idx < rss->rss_size; idx++)
406 rss->ind_tbl[idx] =
407 ethtool_rxfh_indir_default(idx,
408 pfvf->hw.rx_queues);
409
410 ret = otx2_set_rss_table(pfvf, DEFAULT_RSS_CONTEXT_GROUP, NULL);
411 if (ret)
412 return ret;
413
414 /* Flowkey or hash config to be used for generating flow tag */
415 rss->flowkey_cfg = rss->enable ? rss->flowkey_cfg :
416 NIX_FLOW_KEY_TYPE_IPV4 | NIX_FLOW_KEY_TYPE_IPV6 |
417 NIX_FLOW_KEY_TYPE_TCP | NIX_FLOW_KEY_TYPE_UDP |
418 NIX_FLOW_KEY_TYPE_SCTP | NIX_FLOW_KEY_TYPE_VLAN |
419 NIX_FLOW_KEY_TYPE_IPV4_PROTO;
420
421 ret = otx2_set_flowkey_cfg(pfvf);
422 if (ret)
423 return ret;
424
425 rss->enable = true;
426 return 0;
427 }
428
429 /* Setup UDP segmentation algorithm in HW */
otx2_setup_udp_segmentation(struct nix_lso_format_cfg * lso,bool v4)430 static void otx2_setup_udp_segmentation(struct nix_lso_format_cfg *lso, bool v4)
431 {
432 struct nix_lso_format *field;
433
434 field = (struct nix_lso_format *)&lso->fields[0];
435 lso->field_mask = GENMASK(18, 0);
436
437 /* IP's Length field */
438 field->layer = NIX_TXLAYER_OL3;
439 /* In ipv4, length field is at offset 2 bytes, for ipv6 it's 4 */
440 field->offset = v4 ? 2 : 4;
441 field->sizem1 = 1; /* i.e 2 bytes */
442 field->alg = NIX_LSOALG_ADD_PAYLEN;
443 field++;
444
445 /* No ID field in IPv6 header */
446 if (v4) {
447 /* Increment IPID */
448 field->layer = NIX_TXLAYER_OL3;
449 field->offset = 4;
450 field->sizem1 = 1; /* i.e 2 bytes */
451 field->alg = NIX_LSOALG_ADD_SEGNUM;
452 field++;
453 }
454
455 /* Update length in UDP header */
456 field->layer = NIX_TXLAYER_OL4;
457 field->offset = 4;
458 field->sizem1 = 1;
459 field->alg = NIX_LSOALG_ADD_PAYLEN;
460 }
461
462 /* Setup segmentation algorithms in HW and retrieve algorithm index */
otx2_setup_segmentation(struct otx2_nic * pfvf)463 void otx2_setup_segmentation(struct otx2_nic *pfvf)
464 {
465 struct nix_lso_format_cfg_rsp *rsp;
466 struct nix_lso_format_cfg *lso;
467 struct otx2_hw *hw = &pfvf->hw;
468 int err;
469
470 mutex_lock(&pfvf->mbox.lock);
471
472 /* UDPv4 segmentation */
473 lso = otx2_mbox_alloc_msg_nix_lso_format_cfg(&pfvf->mbox);
474 if (!lso)
475 goto fail;
476
477 /* Setup UDP/IP header fields that HW should update per segment */
478 otx2_setup_udp_segmentation(lso, true);
479
480 err = otx2_sync_mbox_msg(&pfvf->mbox);
481 if (err)
482 goto fail;
483
484 rsp = (struct nix_lso_format_cfg_rsp *)
485 otx2_mbox_get_rsp(&pfvf->mbox.mbox, 0, &lso->hdr);
486 if (IS_ERR(rsp))
487 goto fail;
488
489 hw->lso_udpv4_idx = rsp->lso_format_idx;
490
491 /* UDPv6 segmentation */
492 lso = otx2_mbox_alloc_msg_nix_lso_format_cfg(&pfvf->mbox);
493 if (!lso)
494 goto fail;
495
496 /* Setup UDP/IP header fields that HW should update per segment */
497 otx2_setup_udp_segmentation(lso, false);
498
499 err = otx2_sync_mbox_msg(&pfvf->mbox);
500 if (err)
501 goto fail;
502
503 rsp = (struct nix_lso_format_cfg_rsp *)
504 otx2_mbox_get_rsp(&pfvf->mbox.mbox, 0, &lso->hdr);
505 if (IS_ERR(rsp))
506 goto fail;
507
508 hw->lso_udpv6_idx = rsp->lso_format_idx;
509 mutex_unlock(&pfvf->mbox.lock);
510 return;
511 fail:
512 mutex_unlock(&pfvf->mbox.lock);
513 netdev_info(pfvf->netdev,
514 "Failed to get LSO index for UDP GSO offload, disabling\n");
515 pfvf->netdev->hw_features &= ~NETIF_F_GSO_UDP_L4;
516 }
517
otx2_config_irq_coalescing(struct otx2_nic * pfvf,int qidx)518 void otx2_config_irq_coalescing(struct otx2_nic *pfvf, int qidx)
519 {
520 /* Configure CQE interrupt coalescing parameters
521 *
522 * HW triggers an irq when ECOUNT > cq_ecount_wait, hence
523 * set 1 less than cq_ecount_wait. And cq_time_wait is in
524 * usecs, convert that to 100ns count.
525 */
526 otx2_write64(pfvf, NIX_LF_CINTX_WAIT(qidx),
527 ((u64)(pfvf->hw.cq_time_wait * 10) << 48) |
528 ((u64)pfvf->hw.cq_qcount_wait << 32) |
529 (pfvf->hw.cq_ecount_wait - 1));
530 }
531
otx2_alloc_pool_buf(struct otx2_nic * pfvf,struct otx2_pool * pool,dma_addr_t * dma)532 static int otx2_alloc_pool_buf(struct otx2_nic *pfvf, struct otx2_pool *pool,
533 dma_addr_t *dma)
534 {
535 unsigned int offset = 0;
536 struct page *page;
537 size_t sz;
538
539 sz = SKB_DATA_ALIGN(pool->rbsize);
540 sz = ALIGN(sz, OTX2_ALIGN);
541
542 page = page_pool_alloc_frag(pool->page_pool, &offset, sz, GFP_ATOMIC);
543 if (unlikely(!page))
544 return -ENOMEM;
545
546 *dma = page_pool_get_dma_addr(page) + offset;
547 return 0;
548 }
549
__otx2_alloc_rbuf(struct otx2_nic * pfvf,struct otx2_pool * pool,dma_addr_t * dma,int qidx,int idx)550 static int __otx2_alloc_rbuf(struct otx2_nic *pfvf, struct otx2_pool *pool,
551 dma_addr_t *dma, int qidx, int idx)
552 {
553 u8 *buf;
554
555 if (pool->xsk_pool)
556 return otx2_xsk_pool_alloc_buf(pfvf, pool, dma, idx);
557
558 if (pool->page_pool)
559 return otx2_alloc_pool_buf(pfvf, pool, dma);
560
561 buf = napi_alloc_frag_align(pool->rbsize, OTX2_ALIGN);
562 if (unlikely(!buf))
563 return -ENOMEM;
564
565 *dma = dma_map_single_attrs(pfvf->dev, buf, pool->rbsize,
566 DMA_FROM_DEVICE, DMA_ATTR_SKIP_CPU_SYNC);
567 if (unlikely(dma_mapping_error(pfvf->dev, *dma))) {
568 page_frag_free(buf);
569 return -ENOMEM;
570 }
571
572 return 0;
573 }
574
otx2_alloc_rbuf(struct otx2_nic * pfvf,struct otx2_pool * pool,dma_addr_t * dma,int qidx,int idx)575 int otx2_alloc_rbuf(struct otx2_nic *pfvf, struct otx2_pool *pool,
576 dma_addr_t *dma, int qidx, int idx)
577 {
578 int ret;
579
580 local_bh_disable();
581 ret = __otx2_alloc_rbuf(pfvf, pool, dma, qidx, idx);
582 local_bh_enable();
583 return ret;
584 }
585
otx2_alloc_buffer(struct otx2_nic * pfvf,struct otx2_cq_queue * cq,dma_addr_t * dma)586 int otx2_alloc_buffer(struct otx2_nic *pfvf, struct otx2_cq_queue *cq,
587 dma_addr_t *dma)
588 {
589 if (unlikely(__otx2_alloc_rbuf(pfvf, cq->rbpool, dma,
590 cq->cq_idx, cq->pool_ptrs - 1)))
591 return -ENOMEM;
592 return 0;
593 }
594
otx2_tx_timeout(struct net_device * netdev,unsigned int txq)595 void otx2_tx_timeout(struct net_device *netdev, unsigned int txq)
596 {
597 struct otx2_nic *pfvf = netdev_priv(netdev);
598
599 schedule_work(&pfvf->reset_task);
600 }
601 EXPORT_SYMBOL(otx2_tx_timeout);
602
otx2_get_mac_from_af(struct net_device * netdev)603 void otx2_get_mac_from_af(struct net_device *netdev)
604 {
605 struct otx2_nic *pfvf = netdev_priv(netdev);
606 int err;
607
608 err = otx2_hw_get_mac_addr(pfvf, netdev);
609 if (err)
610 dev_warn(pfvf->dev, "Failed to read mac from hardware\n");
611
612 /* If AF doesn't provide a valid MAC, generate a random one */
613 if (!is_valid_ether_addr(netdev->dev_addr))
614 eth_hw_addr_random(netdev);
615 }
616 EXPORT_SYMBOL(otx2_get_mac_from_af);
617
otx2_txschq_config(struct otx2_nic * pfvf,int lvl,int prio,bool txschq_for_pfc)618 int otx2_txschq_config(struct otx2_nic *pfvf, int lvl, int prio, bool txschq_for_pfc)
619 {
620 u16 (*schq_list)[MAX_TXSCHQ_PER_FUNC];
621 struct otx2_hw *hw = &pfvf->hw;
622 struct nix_txschq_config *req;
623 u64 schq, parent;
624 u64 dwrr_val;
625
626 dwrr_val = mtu_to_dwrr_weight(pfvf, pfvf->tx_max_pktlen);
627
628 req = otx2_mbox_alloc_msg_nix_txschq_cfg(&pfvf->mbox);
629 if (!req)
630 return -ENOMEM;
631
632 req->lvl = lvl;
633 req->num_regs = 1;
634
635 schq_list = hw->txschq_list;
636 #ifdef CONFIG_DCB
637 if (txschq_for_pfc)
638 schq_list = pfvf->pfc_schq_list;
639 #endif
640
641 schq = schq_list[lvl][prio];
642 /* Set topology e.t.c configuration */
643 if (lvl == NIX_TXSCH_LVL_SMQ) {
644 req->reg[0] = NIX_AF_SMQX_CFG(schq);
645 req->regval[0] = ((u64)pfvf->tx_max_pktlen << 8) | OTX2_MIN_MTU;
646 req->regval[0] |= (0x20ULL << 51) | (0x80ULL << 39) |
647 (0x2ULL << 36);
648 /* Set link type for DWRR MTU selection on CN10K silicons */
649 if (!is_dev_otx2(pfvf->pdev))
650 req->regval[0] |= FIELD_PREP(GENMASK_ULL(58, 57),
651 (u64)hw->smq_link_type);
652 req->num_regs++;
653 /* MDQ config */
654 parent = schq_list[NIX_TXSCH_LVL_TL4][prio];
655 req->reg[1] = NIX_AF_MDQX_PARENT(schq);
656 req->regval[1] = parent << 16;
657 req->num_regs++;
658 /* Set DWRR quantum */
659 req->reg[2] = NIX_AF_MDQX_SCHEDULE(schq);
660 req->regval[2] = dwrr_val;
661 } else if (lvl == NIX_TXSCH_LVL_TL4) {
662 int sdp_chan = hw->tx_chan_base + prio;
663
664 if (is_otx2_sdp_rep(pfvf->pdev))
665 prio = 0;
666 parent = schq_list[NIX_TXSCH_LVL_TL3][prio];
667 req->reg[0] = NIX_AF_TL4X_PARENT(schq);
668 req->regval[0] = (u64)parent << 16;
669 req->num_regs++;
670 req->reg[1] = NIX_AF_TL4X_SCHEDULE(schq);
671 req->regval[1] = dwrr_val;
672 if (is_otx2_sdp_rep(pfvf->pdev)) {
673 req->num_regs++;
674 req->reg[2] = NIX_AF_TL4X_SDP_LINK_CFG(schq);
675 req->regval[2] = BIT_ULL(12) | BIT_ULL(13) |
676 (sdp_chan & 0xff);
677 }
678 } else if (lvl == NIX_TXSCH_LVL_TL3) {
679 parent = schq_list[NIX_TXSCH_LVL_TL2][prio];
680 req->reg[0] = NIX_AF_TL3X_PARENT(schq);
681 req->regval[0] = (u64)parent << 16;
682 req->num_regs++;
683 req->reg[1] = NIX_AF_TL3X_SCHEDULE(schq);
684 req->regval[1] = dwrr_val;
685 if (lvl == hw->txschq_link_cfg_lvl &&
686 !is_otx2_sdp_rep(pfvf->pdev)) {
687 req->num_regs++;
688 req->reg[2] = NIX_AF_TL3_TL2X_LINKX_CFG(schq, hw->tx_link);
689 /* Enable this queue and backpressure
690 * and set relative channel
691 */
692 req->regval[2] = BIT_ULL(13) | BIT_ULL(12) | prio;
693 }
694 } else if (lvl == NIX_TXSCH_LVL_TL2) {
695 parent = schq_list[NIX_TXSCH_LVL_TL1][prio];
696 req->reg[0] = NIX_AF_TL2X_PARENT(schq);
697 req->regval[0] = (u64)parent << 16;
698
699 req->num_regs++;
700 req->reg[1] = NIX_AF_TL2X_SCHEDULE(schq);
701 req->regval[1] = (u64)hw->txschq_aggr_lvl_rr_prio << 24 | dwrr_val;
702
703 if (lvl == hw->txschq_link_cfg_lvl &&
704 !is_otx2_sdp_rep(pfvf->pdev)) {
705 req->num_regs++;
706 req->reg[2] = NIX_AF_TL3_TL2X_LINKX_CFG(schq, hw->tx_link);
707 /* Enable this queue and backpressure
708 * and set relative channel
709 */
710 req->regval[2] = BIT_ULL(13) | BIT_ULL(12) | prio;
711 }
712 } else if (lvl == NIX_TXSCH_LVL_TL1) {
713 /* Default config for TL1.
714 * For VF this is always ignored.
715 */
716
717 /* On CN10K, if RR_WEIGHT is greater than 16384, HW will
718 * clip it to 16384, so configuring a 24bit max value
719 * will work on both OTx2 and CN10K.
720 */
721 req->reg[0] = NIX_AF_TL1X_SCHEDULE(schq);
722 req->regval[0] = TXSCH_TL1_DFLT_RR_QTM;
723
724 req->num_regs++;
725 req->reg[1] = NIX_AF_TL1X_TOPOLOGY(schq);
726 req->regval[1] = hw->txschq_aggr_lvl_rr_prio << 1;
727
728 req->num_regs++;
729 req->reg[2] = NIX_AF_TL1X_CIR(schq);
730 req->regval[2] = 0;
731 }
732
733 return otx2_sync_mbox_msg(&pfvf->mbox);
734 }
735 EXPORT_SYMBOL(otx2_txschq_config);
736
otx2_smq_flush(struct otx2_nic * pfvf,int smq)737 int otx2_smq_flush(struct otx2_nic *pfvf, int smq)
738 {
739 struct nix_txschq_config *req;
740 int rc;
741
742 mutex_lock(&pfvf->mbox.lock);
743
744 req = otx2_mbox_alloc_msg_nix_txschq_cfg(&pfvf->mbox);
745 if (!req) {
746 mutex_unlock(&pfvf->mbox.lock);
747 return -ENOMEM;
748 }
749
750 req->lvl = NIX_TXSCH_LVL_SMQ;
751 req->reg[0] = NIX_AF_SMQX_CFG(smq);
752 req->regval[0] |= BIT_ULL(49);
753 req->num_regs++;
754
755 rc = otx2_sync_mbox_msg(&pfvf->mbox);
756 mutex_unlock(&pfvf->mbox.lock);
757 return rc;
758 }
759 EXPORT_SYMBOL(otx2_smq_flush);
760
otx2_txsch_alloc(struct otx2_nic * pfvf)761 int otx2_txsch_alloc(struct otx2_nic *pfvf)
762 {
763 int chan_cnt = pfvf->hw.tx_chan_cnt;
764 struct nix_txsch_alloc_req *req;
765 struct nix_txsch_alloc_rsp *rsp;
766 int lvl, schq, rc;
767
768 /* Get memory to put this msg */
769 req = otx2_mbox_alloc_msg_nix_txsch_alloc(&pfvf->mbox);
770 if (!req)
771 return -ENOMEM;
772
773 /* Request one schq per level */
774 for (lvl = 0; lvl < NIX_TXSCH_LVL_CNT; lvl++)
775 req->schq[lvl] = 1;
776
777 if (is_otx2_sdp_rep(pfvf->pdev) && chan_cnt > 1) {
778 req->schq[NIX_TXSCH_LVL_SMQ] = chan_cnt;
779 req->schq[NIX_TXSCH_LVL_TL4] = chan_cnt;
780 }
781
782 rc = otx2_sync_mbox_msg(&pfvf->mbox);
783 if (rc)
784 return rc;
785
786 rsp = (struct nix_txsch_alloc_rsp *)
787 otx2_mbox_get_rsp(&pfvf->mbox.mbox, 0, &req->hdr);
788 if (IS_ERR(rsp))
789 return PTR_ERR(rsp);
790
791 /* Setup transmit scheduler list */
792 for (lvl = 0; lvl < NIX_TXSCH_LVL_CNT; lvl++) {
793 pfvf->hw.txschq_cnt[lvl] = rsp->schq[lvl];
794 for (schq = 0; schq < rsp->schq[lvl]; schq++)
795 pfvf->hw.txschq_list[lvl][schq] =
796 rsp->schq_list[lvl][schq];
797 }
798
799 pfvf->hw.txschq_link_cfg_lvl = rsp->link_cfg_lvl;
800 pfvf->hw.txschq_aggr_lvl_rr_prio = rsp->aggr_lvl_rr_prio;
801
802 return 0;
803 }
804
otx2_txschq_free_one(struct otx2_nic * pfvf,u16 lvl,u16 schq)805 void otx2_txschq_free_one(struct otx2_nic *pfvf, u16 lvl, u16 schq)
806 {
807 struct nix_txsch_free_req *free_req;
808 int err;
809
810 mutex_lock(&pfvf->mbox.lock);
811
812 free_req = otx2_mbox_alloc_msg_nix_txsch_free(&pfvf->mbox);
813 if (!free_req) {
814 mutex_unlock(&pfvf->mbox.lock);
815 netdev_err(pfvf->netdev,
816 "Failed alloc txschq free req\n");
817 return;
818 }
819
820 free_req->schq_lvl = lvl;
821 free_req->schq = schq;
822
823 err = otx2_sync_mbox_msg(&pfvf->mbox);
824 if (err) {
825 netdev_err(pfvf->netdev,
826 "Failed stop txschq %d at level %d\n", schq, lvl);
827 }
828
829 mutex_unlock(&pfvf->mbox.lock);
830 }
831 EXPORT_SYMBOL(otx2_txschq_free_one);
832
otx2_txschq_stop(struct otx2_nic * pfvf)833 void otx2_txschq_stop(struct otx2_nic *pfvf)
834 {
835 int lvl, schq, idx;
836
837 /* free non QOS TLx nodes */
838 for (lvl = 0; lvl < NIX_TXSCH_LVL_CNT; lvl++) {
839 for (idx = 0; idx < pfvf->hw.txschq_cnt[lvl]; idx++) {
840 otx2_txschq_free_one(pfvf, lvl,
841 pfvf->hw.txschq_list[lvl][idx]);
842 }
843 }
844
845 /* Clear the txschq list */
846 for (lvl = 0; lvl < NIX_TXSCH_LVL_CNT; lvl++) {
847 for (schq = 0; schq < MAX_TXSCHQ_PER_FUNC; schq++)
848 pfvf->hw.txschq_list[lvl][schq] = 0;
849 }
850
851 }
852
otx2_sqb_flush(struct otx2_nic * pfvf)853 void otx2_sqb_flush(struct otx2_nic *pfvf)
854 {
855 int qidx, sqe_tail, sqe_head;
856 struct otx2_snd_queue *sq;
857 void __iomem *ptr;
858 u64 incr, val;
859
860 ptr = otx2_get_regaddr(pfvf, NIX_LF_SQ_OP_STATUS);
861 for (qidx = 0; qidx < otx2_get_total_tx_queues(pfvf); qidx++) {
862 sq = &pfvf->qset.sq[qidx];
863 if (!sq->sqb_ptrs)
864 continue;
865
866 incr = (u64)qidx << 32;
867 val = otx2_atomic64_add(incr, ptr);
868 sqe_head = (val >> 20) & 0x3F;
869 sqe_tail = (val >> 28) & 0x3F;
870 if (sqe_head != sqe_tail)
871 usleep_range(50, 60);
872 }
873 }
874
875 /* RED and drop levels of CQ on packet reception.
876 * For CQ level is measure of emptiness ( 0x0 = full, 255 = empty).
877 */
878 #define RQ_PASS_LVL_CQ(skid, qsize) ((((skid) + 16) * 256) / (qsize))
879 #define RQ_DROP_LVL_CQ(skid, qsize) (((skid) * 256) / (qsize))
880
881 /* RED and drop levels of AURA for packet reception.
882 * For AURA level is measure of fullness (0x0 = empty, 255 = full).
883 * Eg: For RQ length 1K, for pass/drop level 204/230.
884 * RED accepts pkts if free pointers > 102 & <= 205.
885 * Drops pkts if free pointers < 102.
886 */
887 #define RQ_BP_LVL_AURA (255 - ((85 * 256) / 100)) /* BP when 85% is full */
888 #define RQ_PASS_LVL_AURA (255 - ((95 * 256) / 100)) /* RED when 95% is full */
889 #define RQ_DROP_LVL_AURA (255 - ((99 * 256) / 100)) /* Drop when 99% is full */
890
otx2_rq_init(struct otx2_nic * pfvf,u16 qidx,u16 lpb_aura)891 int otx2_rq_init(struct otx2_nic *pfvf, u16 qidx, u16 lpb_aura)
892 {
893 struct otx2_qset *qset = &pfvf->qset;
894 struct nix_aq_enq_req *aq;
895
896 /* Get memory to put this msg */
897 aq = otx2_mbox_alloc_msg_nix_aq_enq(&pfvf->mbox);
898 if (!aq)
899 return -ENOMEM;
900
901 aq->rq.cq = qidx;
902 aq->rq.ena = 1;
903 aq->rq.pb_caching = 1;
904 aq->rq.lpb_aura = lpb_aura; /* Use large packet buffer aura */
905 aq->rq.lpb_sizem1 = (DMA_BUFFER_LEN(pfvf->rbsize) / 8) - 1;
906 aq->rq.xqe_imm_size = 0; /* Copying of packet to CQE not needed */
907 aq->rq.flow_tagw = 32; /* Copy full 32bit flow_tag to CQE header */
908 aq->rq.qint_idx = 0;
909 aq->rq.lpb_drop_ena = 1; /* Enable RED dropping for AURA */
910 aq->rq.xqe_drop_ena = 1; /* Enable RED dropping for CQ/SSO */
911 aq->rq.xqe_pass = RQ_PASS_LVL_CQ(pfvf->hw.rq_skid, qset->rqe_cnt);
912 aq->rq.xqe_drop = RQ_DROP_LVL_CQ(pfvf->hw.rq_skid, qset->rqe_cnt);
913 aq->rq.lpb_aura_pass = RQ_PASS_LVL_AURA;
914 aq->rq.lpb_aura_drop = RQ_DROP_LVL_AURA;
915
916 /* Fill AQ info */
917 aq->qidx = qidx;
918 aq->ctype = NIX_AQ_CTYPE_RQ;
919 aq->op = NIX_AQ_INSTOP_INIT;
920
921 return otx2_sync_mbox_msg(&pfvf->mbox);
922 }
923
otx2_sq_aq_init(void * dev,u16 qidx,u8 chan_offset,u16 sqb_aura)924 int otx2_sq_aq_init(void *dev, u16 qidx, u8 chan_offset, u16 sqb_aura)
925 {
926 struct otx2_nic *pfvf = dev;
927 struct otx2_snd_queue *sq;
928 struct nix_aq_enq_req *aq;
929
930 sq = &pfvf->qset.sq[qidx];
931 sq->lmt_addr = (__force u64 *)(pfvf->reg_base + LMT_LF_LMTLINEX(qidx));
932 /* Get memory to put this msg */
933 aq = otx2_mbox_alloc_msg_nix_aq_enq(&pfvf->mbox);
934 if (!aq)
935 return -ENOMEM;
936
937 aq->sq.cq = pfvf->hw.rx_queues + qidx;
938 aq->sq.max_sqe_size = NIX_MAXSQESZ_W16; /* 128 byte */
939 aq->sq.cq_ena = 1;
940 aq->sq.ena = 1;
941 aq->sq.smq = otx2_get_smq_idx(pfvf, qidx);
942 aq->sq.smq_rr_quantum = mtu_to_dwrr_weight(pfvf, pfvf->tx_max_pktlen);
943 aq->sq.default_chan = pfvf->hw.tx_chan_base + chan_offset;
944 aq->sq.sqe_stype = NIX_STYPE_STF; /* Cache SQB */
945 aq->sq.sqb_aura = sqb_aura;
946 aq->sq.sq_int_ena = NIX_SQINT_BITS;
947 aq->sq.qint_idx = 0;
948 /* Due pipelining impact minimum 2000 unused SQ CQE's
949 * need to maintain to avoid CQ overflow.
950 */
951 aq->sq.cq_limit = ((SEND_CQ_SKID * 256) / (pfvf->qset.sqe_cnt));
952
953 /* Fill AQ info */
954 aq->qidx = qidx;
955 aq->ctype = NIX_AQ_CTYPE_SQ;
956 aq->op = NIX_AQ_INSTOP_INIT;
957
958 return otx2_sync_mbox_msg(&pfvf->mbox);
959 }
960
otx2_sq_init(struct otx2_nic * pfvf,u16 qidx,u16 sqb_aura)961 int otx2_sq_init(struct otx2_nic *pfvf, u16 qidx, u16 sqb_aura)
962 {
963 struct otx2_qset *qset = &pfvf->qset;
964 struct otx2_snd_queue *sq;
965 struct otx2_pool *pool;
966 u8 chan_offset;
967 int err;
968
969 pool = &pfvf->qset.pool[sqb_aura];
970 sq = &qset->sq[qidx];
971 sq->sqe_size = NIX_SQESZ_W16 ? 64 : 128;
972 sq->sqe_cnt = qset->sqe_cnt;
973
974 err = qmem_alloc(pfvf->dev, &sq->sqe, 1, sq->sqe_size);
975 if (err)
976 return err;
977
978 /* Allocate memory for NIX SQE (which includes NIX SG) and CPT SG.
979 * SG of NIX and CPT are same in size. Allocate memory for CPT SG
980 * same as NIX SQE for base address alignment.
981 * Layout of a NIX SQE and CPT SG entry:
982 * -----------------------------
983 * | CPT Scatter Gather |
984 * | (SQE SIZE) |
985 * | |
986 * -----------------------------
987 * | NIX SQE |
988 * | (SQE SIZE) |
989 * | |
990 * -----------------------------
991 */
992 err = qmem_alloc(pfvf->dev, &sq->sqe_ring, qset->sqe_cnt,
993 sq->sqe_size * 2);
994 if (err)
995 return err;
996
997 err = qmem_alloc(pfvf->dev, &sq->cpt_resp, qset->sqe_cnt, 64);
998 if (err)
999 return err;
1000
1001 if (qidx < pfvf->hw.tx_queues) {
1002 err = qmem_alloc(pfvf->dev, &sq->tso_hdrs, qset->sqe_cnt,
1003 TSO_HEADER_SIZE);
1004 if (err)
1005 return err;
1006 }
1007
1008 sq->sqe_base = sq->sqe->base;
1009 sq->sg = kzalloc_objs(struct sg_list, qset->sqe_cnt);
1010 if (!sq->sg)
1011 return -ENOMEM;
1012
1013 if (pfvf->ptp && qidx < pfvf->hw.tx_queues) {
1014 err = qmem_alloc(pfvf->dev, &sq->timestamps, qset->sqe_cnt,
1015 sizeof(*sq->timestamps));
1016 if (err) {
1017 kfree(sq->sg);
1018 sq->sg = NULL;
1019 return err;
1020 }
1021 }
1022
1023 sq->head = 0;
1024 sq->cons_head = 0;
1025 sq->sqe_per_sqb = (pfvf->hw.sqb_size / sq->sqe_size) - 1;
1026 sq->num_sqbs = (qset->sqe_cnt + sq->sqe_per_sqb) / sq->sqe_per_sqb;
1027 /* Set SQE threshold to 10% of total SQEs */
1028 sq->sqe_thresh = ((sq->num_sqbs * sq->sqe_per_sqb) * 10) / 100;
1029 sq->aura_id = sqb_aura;
1030 sq->aura_fc_addr = pool->fc_addr->base;
1031 sq->io_addr = (__force u64)otx2_get_regaddr(pfvf, NIX_LF_OP_SENDX(0));
1032
1033 sq->stats.bytes = 0;
1034 sq->stats.pkts = 0;
1035 /* Attach XSK_BUFF_POOL to XDP queue */
1036 if (qidx > pfvf->hw.xdp_queues)
1037 otx2_attach_xsk_buff(pfvf, sq, (qidx - pfvf->hw.xdp_queues));
1038
1039 chan_offset = qidx % pfvf->hw.tx_chan_cnt;
1040 err = pfvf->hw_ops->sq_aq_init(pfvf, qidx, chan_offset, sqb_aura);
1041 if (err) {
1042 kfree(sq->sg);
1043 sq->sg = NULL;
1044 return err;
1045 }
1046
1047 return 0;
1048
1049 }
1050
otx2_cq_init(struct otx2_nic * pfvf,u16 qidx)1051 int otx2_cq_init(struct otx2_nic *pfvf, u16 qidx)
1052 {
1053 struct otx2_qset *qset = &pfvf->qset;
1054 int err, pool_id, non_xdp_queues;
1055 struct nix_aq_enq_req *aq;
1056 struct otx2_cq_queue *cq;
1057 struct otx2_pool *pool;
1058 u8 bpid_idx;
1059
1060 cq = &qset->cq[qidx];
1061 cq->cq_idx = qidx;
1062 non_xdp_queues = pfvf->hw.rx_queues + pfvf->hw.tx_queues;
1063 if (qidx < pfvf->hw.rx_queues) {
1064 cq->cq_type = CQ_RX;
1065 cq->cint_idx = qidx;
1066 cq->cqe_cnt = qset->rqe_cnt;
1067 if (pfvf->xdp_prog) {
1068 xdp_rxq_info_reg(&cq->xdp_rxq, pfvf->netdev, qidx, 0);
1069 pool = &qset->pool[qidx];
1070 if (pool->xsk_pool) {
1071 xdp_rxq_info_reg_mem_model(&cq->xdp_rxq,
1072 MEM_TYPE_XSK_BUFF_POOL,
1073 NULL);
1074 xsk_pool_set_rxq_info(pool->xsk_pool, &cq->xdp_rxq);
1075 } else if (pool->page_pool) {
1076 xdp_rxq_info_reg_mem_model(&cq->xdp_rxq,
1077 MEM_TYPE_PAGE_POOL,
1078 pool->page_pool);
1079 }
1080 }
1081 } else if (qidx < non_xdp_queues) {
1082 cq->cq_type = CQ_TX;
1083 cq->cint_idx = qidx - pfvf->hw.rx_queues;
1084 cq->cqe_cnt = qset->sqe_cnt;
1085 } else {
1086 if (pfvf->hw.xdp_queues &&
1087 qidx < non_xdp_queues + pfvf->hw.xdp_queues) {
1088 cq->cq_type = CQ_XDP;
1089 cq->cint_idx = qidx - non_xdp_queues;
1090 cq->cqe_cnt = qset->sqe_cnt;
1091 } else {
1092 cq->cq_type = CQ_QOS;
1093 cq->cint_idx = qidx - non_xdp_queues -
1094 pfvf->hw.xdp_queues;
1095 cq->cqe_cnt = qset->sqe_cnt;
1096 }
1097 }
1098 cq->cqe_size = pfvf->qset.xqe_size;
1099
1100 /* Allocate memory for CQEs */
1101 err = qmem_alloc(pfvf->dev, &cq->cqe, cq->cqe_cnt, cq->cqe_size);
1102 if (err)
1103 return err;
1104
1105 /* Save CQE CPU base for faster reference */
1106 cq->cqe_base = cq->cqe->base;
1107 /* In case where all RQs auras point to single pool,
1108 * all CQs receive buffer pool also point to same pool.
1109 */
1110 pool_id = ((cq->cq_type == CQ_RX) &&
1111 (pfvf->hw.rqpool_cnt != pfvf->hw.rx_queues)) ? 0 : qidx;
1112 cq->rbpool = &qset->pool[pool_id];
1113 cq->refill_task_sched = false;
1114
1115 /* Get memory to put this msg */
1116 aq = otx2_mbox_alloc_msg_nix_aq_enq(&pfvf->mbox);
1117 if (!aq)
1118 return -ENOMEM;
1119
1120 aq->cq.ena = 1;
1121 aq->cq.qsize = Q_SIZE(cq->cqe_cnt, 4);
1122 aq->cq.caching = 1;
1123 aq->cq.base = cq->cqe->iova;
1124 aq->cq.cint_idx = cq->cint_idx;
1125 aq->cq.cq_err_int_ena = NIX_CQERRINT_BITS;
1126 aq->cq.qint_idx = 0;
1127 aq->cq.avg_level = 255;
1128
1129 if (qidx < pfvf->hw.rx_queues) {
1130 aq->cq.drop = RQ_DROP_LVL_CQ(pfvf->hw.rq_skid, cq->cqe_cnt);
1131 aq->cq.drop_ena = 1;
1132
1133 if (!is_otx2_lbkvf(pfvf->pdev)) {
1134 /* Enable receive CQ backpressure */
1135 aq->cq.bp_ena = 1;
1136 bpid_idx = otx2_get_bpid_idx(pfvf, qidx);
1137 aq->cq.bpid = pfvf->bpid[bpid_idx];
1138
1139 /* Set backpressure level is same as cq pass level */
1140 aq->cq.bp = RQ_PASS_LVL_CQ(pfvf->hw.rq_skid, qset->rqe_cnt);
1141 }
1142 }
1143
1144 /* Fill AQ info */
1145 aq->qidx = qidx;
1146 aq->ctype = NIX_AQ_CTYPE_CQ;
1147 aq->op = NIX_AQ_INSTOP_INIT;
1148
1149 return otx2_sync_mbox_msg(&pfvf->mbox);
1150 }
1151
otx2_pool_refill_task(struct work_struct * work)1152 static void otx2_pool_refill_task(struct work_struct *work)
1153 {
1154 struct otx2_cq_queue *cq;
1155 struct refill_work *wrk;
1156 struct otx2_nic *pfvf;
1157 int qidx;
1158
1159 wrk = container_of(work, struct refill_work, pool_refill_work.work);
1160 pfvf = wrk->pf;
1161 qidx = wrk - pfvf->refill_wrk;
1162 cq = &pfvf->qset.cq[qidx];
1163
1164 cq->refill_task_sched = false;
1165
1166 local_bh_disable();
1167 napi_schedule(wrk->napi);
1168 local_bh_enable();
1169 }
1170
otx2_config_nix_queues(struct otx2_nic * pfvf)1171 int otx2_config_nix_queues(struct otx2_nic *pfvf)
1172 {
1173 int qidx, err;
1174
1175 /* Initialize RX queues */
1176 for (qidx = 0; qidx < pfvf->hw.rx_queues; qidx++) {
1177 u16 lpb_aura = otx2_get_pool_idx(pfvf, AURA_NIX_RQ, qidx);
1178
1179 err = otx2_rq_init(pfvf, qidx, lpb_aura);
1180 if (err)
1181 return err;
1182 }
1183
1184 /* Initialize TX queues */
1185 for (qidx = 0; qidx < pfvf->hw.non_qos_queues; qidx++) {
1186 u16 sqb_aura = otx2_get_pool_idx(pfvf, AURA_NIX_SQ, qidx);
1187
1188 err = otx2_sq_init(pfvf, qidx, sqb_aura);
1189 if (err)
1190 return err;
1191 }
1192
1193 /* Initialize completion queues */
1194 for (qidx = 0; qidx < pfvf->qset.cq_cnt; qidx++) {
1195 err = otx2_cq_init(pfvf, qidx);
1196 if (err)
1197 return err;
1198 }
1199
1200 pfvf->cq_op_addr = (__force u64 *)otx2_get_regaddr(pfvf,
1201 NIX_LF_CQ_OP_STATUS);
1202
1203 /* Initialize work queue for receive buffer refill */
1204 pfvf->refill_wrk = devm_kcalloc(pfvf->dev, pfvf->qset.cq_cnt,
1205 sizeof(struct refill_work), GFP_KERNEL);
1206 if (!pfvf->refill_wrk)
1207 return -ENOMEM;
1208
1209 for (qidx = 0; qidx < pfvf->qset.cq_cnt; qidx++) {
1210 pfvf->refill_wrk[qidx].pf = pfvf;
1211 INIT_DELAYED_WORK(&pfvf->refill_wrk[qidx].pool_refill_work,
1212 otx2_pool_refill_task);
1213 }
1214 return 0;
1215 }
1216
otx2_config_nix(struct otx2_nic * pfvf)1217 int otx2_config_nix(struct otx2_nic *pfvf)
1218 {
1219 struct nix_lf_alloc_req *nixlf;
1220 struct nix_lf_alloc_rsp *rsp;
1221 int err;
1222
1223 pfvf->qset.xqe_size = pfvf->hw.xqe_size;
1224
1225 /* Get memory to put this msg */
1226 nixlf = otx2_mbox_alloc_msg_nix_lf_alloc(&pfvf->mbox);
1227 if (!nixlf)
1228 return -ENOMEM;
1229
1230 /* Set RQ/SQ/CQ counts */
1231 nixlf->rq_cnt = pfvf->hw.rx_queues;
1232 nixlf->sq_cnt = otx2_get_total_tx_queues(pfvf);
1233 nixlf->cq_cnt = pfvf->qset.cq_cnt;
1234 nixlf->rss_sz = MAX_RSS_INDIR_TBL_SIZE;
1235 nixlf->rss_grps = MAX_RSS_GROUPS;
1236 nixlf->xqe_sz = pfvf->hw.xqe_size == 128 ? NIX_XQESZ_W16 : NIX_XQESZ_W64;
1237 /* We don't know absolute NPA LF idx attached.
1238 * AF will replace 'RVU_DEFAULT_PF_FUNC' with
1239 * NPA LF attached to this RVU PF/VF.
1240 */
1241 nixlf->npa_func = RVU_DEFAULT_PF_FUNC;
1242 /* Disable alignment pad, enable L2 length check,
1243 * enable L4 TCP/UDP checksum verification.
1244 */
1245 nixlf->rx_cfg = BIT_ULL(33) | BIT_ULL(35) | BIT_ULL(37);
1246
1247 err = otx2_sync_mbox_msg(&pfvf->mbox);
1248 if (err)
1249 return err;
1250
1251 rsp = (struct nix_lf_alloc_rsp *)otx2_mbox_get_rsp(&pfvf->mbox.mbox, 0,
1252 &nixlf->hdr);
1253 if (IS_ERR(rsp))
1254 return PTR_ERR(rsp);
1255
1256 if (rsp->qints < 1)
1257 return -ENXIO;
1258
1259 return rsp->hdr.rc;
1260 }
1261
otx2_sq_free_sqbs(struct otx2_nic * pfvf)1262 void otx2_sq_free_sqbs(struct otx2_nic *pfvf)
1263 {
1264 struct otx2_qset *qset = &pfvf->qset;
1265 struct otx2_hw *hw = &pfvf->hw;
1266 struct otx2_snd_queue *sq;
1267 int sqb, qidx;
1268 u64 iova, pa;
1269
1270 for (qidx = 0; qidx < otx2_get_total_tx_queues(pfvf); qidx++) {
1271 sq = &qset->sq[qidx];
1272 if (!sq->sqb_ptrs)
1273 continue;
1274 for (sqb = 0; sqb < sq->sqb_count; sqb++) {
1275 if (!sq->sqb_ptrs[sqb])
1276 continue;
1277 iova = sq->sqb_ptrs[sqb];
1278 pa = otx2_iova_to_phys(pfvf->iommu_domain, iova);
1279 dma_unmap_page_attrs(pfvf->dev, iova, hw->sqb_size,
1280 DMA_FROM_DEVICE,
1281 DMA_ATTR_SKIP_CPU_SYNC);
1282 put_page(virt_to_page(phys_to_virt(pa)));
1283 }
1284 sq->sqb_count = 0;
1285 }
1286 }
1287
otx2_free_bufs(struct otx2_nic * pfvf,struct otx2_pool * pool,u64 iova,int size)1288 void otx2_free_bufs(struct otx2_nic *pfvf, struct otx2_pool *pool,
1289 u64 iova, int size)
1290 {
1291 struct page *page;
1292 u64 pa;
1293
1294 pa = otx2_iova_to_phys(pfvf->iommu_domain, iova);
1295 page = virt_to_head_page(phys_to_virt(pa));
1296 if (pool->page_pool) {
1297 page_pool_put_full_page(pool->page_pool, page, true);
1298 } else if (pool->xsk_pool) {
1299 /* Note: No way of identifying xdp_buff */
1300 } else {
1301 dma_unmap_page_attrs(pfvf->dev, iova, size,
1302 DMA_FROM_DEVICE,
1303 DMA_ATTR_SKIP_CPU_SYNC);
1304
1305 put_page(page);
1306 }
1307 }
1308
otx2_free_aura_ptr(struct otx2_nic * pfvf,int type)1309 void otx2_free_aura_ptr(struct otx2_nic *pfvf, int type)
1310 {
1311 int pool_id, pool_start = 0, pool_end = 0, size = 0;
1312 struct otx2_pool *pool;
1313 u64 iova;
1314 int idx;
1315
1316 if (type == AURA_NIX_SQ) {
1317 pool_start = otx2_get_pool_idx(pfvf, type, 0);
1318 pool_end = pool_start + pfvf->hw.sqpool_cnt;
1319 size = pfvf->hw.sqb_size;
1320 }
1321 if (type == AURA_NIX_RQ) {
1322 pool_start = otx2_get_pool_idx(pfvf, type, 0);
1323 pool_end = pfvf->hw.rqpool_cnt;
1324 size = pfvf->rbsize;
1325 }
1326
1327 /* Free SQB and RQB pointers from the aura pool */
1328 for (pool_id = pool_start; pool_id < pool_end; pool_id++) {
1329 pool = &pfvf->qset.pool[pool_id];
1330 iova = otx2_aura_allocptr(pfvf, pool_id);
1331 while (iova) {
1332 if (type == AURA_NIX_RQ)
1333 iova -= OTX2_HEAD_ROOM;
1334 otx2_free_bufs(pfvf, pool, iova, size);
1335 iova = otx2_aura_allocptr(pfvf, pool_id);
1336 }
1337
1338 for (idx = 0 ; idx < pool->xdp_cnt; idx++) {
1339 if (!pool->xdp[idx])
1340 continue;
1341
1342 xsk_buff_free(pool->xdp[idx]);
1343 }
1344 }
1345 }
1346
otx2_aura_pool_free(struct otx2_nic * pfvf)1347 void otx2_aura_pool_free(struct otx2_nic *pfvf)
1348 {
1349 struct otx2_pool *pool;
1350 int pool_id;
1351
1352 if (!pfvf->qset.pool)
1353 return;
1354
1355 for (pool_id = 0; pool_id < pfvf->hw.pool_cnt; pool_id++) {
1356 pool = &pfvf->qset.pool[pool_id];
1357 qmem_free(pfvf->dev, pool->stack);
1358 qmem_free(pfvf->dev, pool->fc_addr);
1359 page_pool_destroy(pool->page_pool);
1360 devm_kfree(pfvf->dev, pool->xdp);
1361 pool->xsk_pool = NULL;
1362 }
1363 devm_kfree(pfvf->dev, pfvf->qset.pool);
1364 pfvf->qset.pool = NULL;
1365 }
1366
otx2_aura_init(struct otx2_nic * pfvf,int aura_id,int pool_id,int numptrs)1367 int otx2_aura_init(struct otx2_nic *pfvf, int aura_id,
1368 int pool_id, int numptrs)
1369 {
1370 return pfvf->hw_ops->aura_aq_init(pfvf, aura_id, pool_id,
1371 numptrs);
1372 }
1373
otx2_aura_aq_init(struct otx2_nic * pfvf,int aura_id,int pool_id,int numptrs)1374 int otx2_aura_aq_init(struct otx2_nic *pfvf, int aura_id,
1375 int pool_id, int numptrs)
1376 {
1377 struct npa_aq_enq_req *aq;
1378 struct otx2_pool *pool;
1379 u8 bpid_idx;
1380 int err;
1381
1382 pool = &pfvf->qset.pool[pool_id];
1383
1384 /* Allocate memory for HW to update Aura count.
1385 * Alloc one cache line, so that it fits all FC_STYPE modes.
1386 */
1387 if (!pool->fc_addr) {
1388 err = qmem_alloc(pfvf->dev, &pool->fc_addr, 1, OTX2_ALIGN);
1389 if (err)
1390 return err;
1391 }
1392
1393 /* Initialize this aura's context via AF */
1394 aq = otx2_mbox_alloc_msg_npa_aq_enq(&pfvf->mbox);
1395 if (!aq) {
1396 /* Shared mbox memory buffer is full, flush it and retry */
1397 err = otx2_sync_mbox_msg(&pfvf->mbox);
1398 if (err)
1399 return err;
1400 aq = otx2_mbox_alloc_msg_npa_aq_enq(&pfvf->mbox);
1401 if (!aq)
1402 return -ENOMEM;
1403 }
1404
1405 aq->aura_id = aura_id;
1406 /* Will be filled by AF with correct pool context address */
1407 aq->aura.pool_addr = pool_id;
1408 aq->aura.pool_caching = 1;
1409 aq->aura.shift = ilog2(numptrs) - 8;
1410 aq->aura.count = numptrs;
1411 aq->aura.limit = numptrs;
1412 aq->aura.avg_level = 255;
1413 aq->aura.ena = 1;
1414 aq->aura.fc_ena = 1;
1415 aq->aura.fc_addr = pool->fc_addr->iova;
1416 aq->aura.fc_hyst_bits = 0; /* Store count on all updates */
1417
1418 /* Enable backpressure for RQ aura */
1419 if (aura_id < pfvf->hw.rqpool_cnt && !is_otx2_lbkvf(pfvf->pdev)) {
1420 aq->aura.bp_ena = 0;
1421 /* If NIX1 LF is attached then specify NIX1_RX.
1422 *
1423 * Below NPA_AURA_S[BP_ENA] is set according to the
1424 * NPA_BPINTF_E enumeration given as:
1425 * 0x0 + a*0x1 where 'a' is 0 for NIX0_RX and 1 for NIX1_RX so
1426 * NIX0_RX is 0x0 + 0*0x1 = 0
1427 * NIX1_RX is 0x0 + 1*0x1 = 1
1428 * But in HRM it is given that
1429 * "NPA_AURA_S[BP_ENA](w1[33:32]) - Enable aura backpressure to
1430 * NIX-RX based on [BP] level. One bit per NIX-RX; index
1431 * enumerated by NPA_BPINTF_E."
1432 */
1433 if (pfvf->nix_blkaddr == BLKADDR_NIX1)
1434 aq->aura.bp_ena = 1;
1435 bpid_idx = otx2_get_bpid_idx(pfvf, aura_id);
1436 aq->aura.nix0_bpid = pfvf->bpid[bpid_idx];
1437
1438 /* Set backpressure level for RQ's Aura */
1439 aq->aura.bp = RQ_BP_LVL_AURA;
1440 }
1441
1442 /* Fill AQ info */
1443 aq->ctype = NPA_AQ_CTYPE_AURA;
1444 aq->op = NPA_AQ_INSTOP_INIT;
1445
1446 return 0;
1447 }
1448
otx2_pool_init(struct otx2_nic * pfvf,u16 pool_id,int stack_pages,int numptrs,int buf_size,int type)1449 int otx2_pool_init(struct otx2_nic *pfvf, u16 pool_id,
1450 int stack_pages, int numptrs, int buf_size, int type)
1451 {
1452 return pfvf->hw_ops->pool_aq_init(pfvf, pool_id, stack_pages, numptrs,
1453 buf_size, type);
1454 }
1455
otx2_pool_aq_init(struct otx2_nic * pfvf,u16 pool_id,int stack_pages,int numptrs,int buf_size,int type)1456 int otx2_pool_aq_init(struct otx2_nic *pfvf, u16 pool_id,
1457 int stack_pages, int numptrs, int buf_size, int type)
1458 {
1459 struct page_pool_params pp_params = { 0 };
1460 struct xsk_buff_pool *xsk_pool;
1461 struct npa_aq_enq_req *aq;
1462 struct otx2_pool *pool;
1463 int err;
1464
1465 pool = &pfvf->qset.pool[pool_id];
1466 /* Alloc memory for stack which is used to store buffer pointers */
1467 err = qmem_alloc(pfvf->dev, &pool->stack,
1468 stack_pages, pfvf->hw.stack_pg_bytes);
1469 if (err)
1470 return err;
1471
1472 pool->rbsize = buf_size;
1473
1474 /* Initialize this pool's context via AF */
1475 aq = otx2_mbox_alloc_msg_npa_aq_enq(&pfvf->mbox);
1476 if (!aq) {
1477 /* Shared mbox memory buffer is full, flush it and retry */
1478 err = otx2_sync_mbox_msg(&pfvf->mbox);
1479 if (err) {
1480 qmem_free(pfvf->dev, pool->stack);
1481 pool->stack = NULL;
1482 return err;
1483 }
1484 aq = otx2_mbox_alloc_msg_npa_aq_enq(&pfvf->mbox);
1485 if (!aq) {
1486 qmem_free(pfvf->dev, pool->stack);
1487 pool->stack = NULL;
1488 return -ENOMEM;
1489 }
1490 }
1491
1492 aq->aura_id = pool_id;
1493 aq->pool.stack_base = pool->stack->iova;
1494 aq->pool.stack_caching = 1;
1495 aq->pool.ena = 1;
1496 aq->pool.buf_size = buf_size / 128;
1497 aq->pool.stack_max_pages = stack_pages;
1498 aq->pool.shift = ilog2(numptrs) - 8;
1499 aq->pool.ptr_start = 0;
1500 aq->pool.ptr_end = ~0ULL;
1501
1502 /* Fill AQ info */
1503 aq->ctype = NPA_AQ_CTYPE_POOL;
1504 aq->op = NPA_AQ_INSTOP_INIT;
1505
1506 if (type != AURA_NIX_RQ)
1507 return 0;
1508
1509 if (!pfvf->af_xdp_zc_qidx ||
1510 !test_bit(pool_id, pfvf->af_xdp_zc_qidx)) {
1511 pp_params.order = get_order(buf_size);
1512 pp_params.flags = PP_FLAG_DMA_MAP;
1513 pp_params.pool_size = min(OTX2_PAGE_POOL_SZ, numptrs);
1514 pp_params.nid = NUMA_NO_NODE;
1515 pp_params.dev = pfvf->dev;
1516 pp_params.dma_dir = DMA_FROM_DEVICE;
1517 pp_params.netdev = pfvf->netdev;
1518 pool->page_pool = page_pool_create(&pp_params);
1519 if (IS_ERR(pool->page_pool)) {
1520 netdev_err(pfvf->netdev, "Creation of page pool failed\n");
1521 return PTR_ERR(pool->page_pool);
1522 }
1523 return 0;
1524 }
1525
1526 /* Set XSK pool to support AF_XDP zero-copy */
1527 xsk_pool = xsk_get_pool_from_qid(pfvf->netdev, pool_id);
1528 if (xsk_pool) {
1529 pool->xsk_pool = xsk_pool;
1530 pool->xdp_cnt = numptrs;
1531 pool->xdp = devm_kcalloc(pfvf->dev,
1532 numptrs, sizeof(struct xdp_buff *), GFP_KERNEL);
1533 if (!pool->xdp)
1534 return -ENOMEM;
1535 }
1536
1537 return 0;
1538 }
1539
otx2_sq_aura_pool_init(struct otx2_nic * pfvf)1540 int otx2_sq_aura_pool_init(struct otx2_nic *pfvf)
1541 {
1542 int qidx, pool_id, stack_pages, num_sqbs;
1543 struct otx2_qset *qset = &pfvf->qset;
1544 struct otx2_hw *hw = &pfvf->hw;
1545 struct otx2_snd_queue *sq;
1546 struct otx2_pool *pool;
1547 dma_addr_t bufptr;
1548 int err, ptr;
1549
1550 /* Calculate number of SQBs needed.
1551 *
1552 * For a 128byte SQE, and 4K size SQB, 31 SQEs will fit in one SQB.
1553 * Last SQE is used for pointing to next SQB.
1554 */
1555 num_sqbs = (hw->sqb_size / 128) - 1;
1556 num_sqbs = (qset->sqe_cnt + num_sqbs) / num_sqbs;
1557
1558 /* Get no of stack pages needed */
1559 stack_pages =
1560 (num_sqbs + hw->stack_pg_ptrs - 1) / hw->stack_pg_ptrs;
1561
1562 for (qidx = 0; qidx < hw->non_qos_queues; qidx++) {
1563 pool_id = otx2_get_pool_idx(pfvf, AURA_NIX_SQ, qidx);
1564 /* Initialize aura context */
1565 err = otx2_aura_init(pfvf, pool_id, pool_id, num_sqbs);
1566 if (err)
1567 goto fail;
1568
1569 /* Initialize pool context */
1570 err = otx2_pool_init(pfvf, pool_id, stack_pages,
1571 num_sqbs, hw->sqb_size, AURA_NIX_SQ);
1572 if (err)
1573 goto fail;
1574 }
1575
1576 /* Flush accumulated messages */
1577 err = otx2_sync_mbox_msg(&pfvf->mbox);
1578 if (err)
1579 goto fail;
1580
1581 /* Allocate pointers and free them to aura/pool */
1582 for (qidx = 0; qidx < hw->non_qos_queues; qidx++) {
1583 pool_id = otx2_get_pool_idx(pfvf, AURA_NIX_SQ, qidx);
1584 pool = &pfvf->qset.pool[pool_id];
1585
1586 sq = &qset->sq[qidx];
1587 sq->sqb_count = 0;
1588 sq->sqb_ptrs = kzalloc_objs(*sq->sqb_ptrs, num_sqbs);
1589 if (!sq->sqb_ptrs) {
1590 err = -ENOMEM;
1591 goto err_mem;
1592 }
1593
1594 for (ptr = 0; ptr < num_sqbs; ptr++) {
1595 err = otx2_alloc_rbuf(pfvf, pool, &bufptr, pool_id, ptr);
1596 if (err) {
1597 if (pool->xsk_pool) {
1598 ptr--;
1599 while (ptr >= 0) {
1600 xsk_buff_free(pool->xdp[ptr]);
1601 ptr--;
1602 }
1603 }
1604 goto err_mem;
1605 }
1606
1607 pfvf->hw_ops->aura_freeptr(pfvf, pool_id, bufptr);
1608 sq->sqb_ptrs[sq->sqb_count++] = (u64)bufptr;
1609 }
1610 }
1611
1612 err_mem:
1613 return err ? -ENOMEM : 0;
1614
1615 fail:
1616 otx2_mbox_reset(&pfvf->mbox.mbox, 0);
1617 otx2_aura_pool_free(pfvf);
1618 return err;
1619 }
1620
otx2_rq_aura_pool_init(struct otx2_nic * pfvf)1621 int otx2_rq_aura_pool_init(struct otx2_nic *pfvf)
1622 {
1623 struct otx2_hw *hw = &pfvf->hw;
1624 int stack_pages, pool_id, rq;
1625 struct otx2_pool *pool;
1626 int err, ptr, num_ptrs;
1627 dma_addr_t bufptr;
1628
1629 num_ptrs = pfvf->qset.rqe_cnt;
1630
1631 stack_pages =
1632 (num_ptrs + hw->stack_pg_ptrs - 1) / hw->stack_pg_ptrs;
1633
1634 for (rq = 0; rq < hw->rx_queues; rq++) {
1635 pool_id = otx2_get_pool_idx(pfvf, AURA_NIX_RQ, rq);
1636 /* Initialize aura context */
1637 err = otx2_aura_init(pfvf, pool_id, pool_id, num_ptrs);
1638 if (err)
1639 goto fail;
1640 }
1641 for (pool_id = 0; pool_id < hw->rqpool_cnt; pool_id++) {
1642 err = otx2_pool_init(pfvf, pool_id, stack_pages,
1643 num_ptrs, pfvf->rbsize, AURA_NIX_RQ);
1644 if (err)
1645 goto fail;
1646 }
1647
1648 /* Flush accumulated messages */
1649 err = otx2_sync_mbox_msg(&pfvf->mbox);
1650 if (err)
1651 goto fail;
1652
1653 /* Allocate pointers and free them to aura/pool */
1654 for (pool_id = 0; pool_id < hw->rqpool_cnt; pool_id++) {
1655 pool = &pfvf->qset.pool[pool_id];
1656
1657 for (ptr = 0; ptr < num_ptrs; ptr++) {
1658 err = otx2_alloc_rbuf(pfvf, pool, &bufptr, pool_id, ptr);
1659 if (err) {
1660 if (pool->xsk_pool) {
1661 while (ptr)
1662 xsk_buff_free(pool->xdp[--ptr]);
1663 }
1664 return -ENOMEM;
1665 }
1666
1667 pfvf->hw_ops->aura_freeptr(pfvf, pool_id,
1668 pool->xsk_pool ? bufptr :
1669 bufptr + OTX2_HEAD_ROOM);
1670 }
1671 }
1672 return 0;
1673 fail:
1674 otx2_mbox_reset(&pfvf->mbox.mbox, 0);
1675 otx2_aura_pool_free(pfvf);
1676 return err;
1677 }
1678
otx2_config_npa(struct otx2_nic * pfvf)1679 int otx2_config_npa(struct otx2_nic *pfvf)
1680 {
1681 struct otx2_qset *qset = &pfvf->qset;
1682 struct npa_lf_alloc_req *npalf;
1683 struct otx2_hw *hw = &pfvf->hw;
1684 int aura_cnt;
1685
1686 /* Pool - Stack of free buffer pointers
1687 * Aura - Alloc/frees pointers from/to pool for NIX DMA.
1688 */
1689
1690 if (!hw->pool_cnt)
1691 return -EINVAL;
1692
1693 qset->pool = devm_kcalloc(pfvf->dev, hw->pool_cnt,
1694 sizeof(struct otx2_pool), GFP_KERNEL);
1695 if (!qset->pool)
1696 return -ENOMEM;
1697
1698 /* Get memory to put this msg */
1699 npalf = otx2_mbox_alloc_msg_npa_lf_alloc(&pfvf->mbox);
1700 if (!npalf)
1701 return -ENOMEM;
1702
1703 /* Set aura and pool counts */
1704 npalf->nr_pools = hw->pool_cnt;
1705 aura_cnt = ilog2(roundup_pow_of_two(hw->pool_cnt));
1706 npalf->aura_sz = (aura_cnt >= ilog2(128)) ? (aura_cnt - 6) : 1;
1707
1708 return otx2_sync_mbox_msg(&pfvf->mbox);
1709 }
1710
otx2_detach_resources(struct mbox * mbox)1711 int otx2_detach_resources(struct mbox *mbox)
1712 {
1713 struct rsrc_detach *detach;
1714
1715 mutex_lock(&mbox->lock);
1716 detach = otx2_mbox_alloc_msg_detach_resources(mbox);
1717 if (!detach) {
1718 mutex_unlock(&mbox->lock);
1719 return -ENOMEM;
1720 }
1721
1722 /* detach all */
1723 detach->partial = false;
1724
1725 /* Send detach request to AF */
1726 otx2_sync_mbox_msg(mbox);
1727 mutex_unlock(&mbox->lock);
1728 return 0;
1729 }
1730 EXPORT_SYMBOL(otx2_detach_resources);
1731
otx2_attach_npa_nix(struct otx2_nic * pfvf)1732 int otx2_attach_npa_nix(struct otx2_nic *pfvf)
1733 {
1734 struct rsrc_attach *attach;
1735 struct msg_req *msix;
1736 int err;
1737
1738 mutex_lock(&pfvf->mbox.lock);
1739 /* Get memory to put this msg */
1740 attach = otx2_mbox_alloc_msg_attach_resources(&pfvf->mbox);
1741 if (!attach) {
1742 mutex_unlock(&pfvf->mbox.lock);
1743 return -ENOMEM;
1744 }
1745
1746 attach->npalf = true;
1747 attach->nixlf = true;
1748
1749 /* Send attach request to AF */
1750 err = otx2_sync_mbox_msg(&pfvf->mbox);
1751 if (err) {
1752 mutex_unlock(&pfvf->mbox.lock);
1753 return err;
1754 }
1755
1756 pfvf->nix_blkaddr = BLKADDR_NIX0;
1757
1758 /* If the platform has two NIX blocks then LF may be
1759 * allocated from NIX1.
1760 */
1761 if (otx2_read64(pfvf, RVU_PF_BLOCK_ADDRX_DISC(BLKADDR_NIX1)) & 0x1FFULL)
1762 pfvf->nix_blkaddr = BLKADDR_NIX1;
1763
1764 /* Get NPA and NIX MSIX vector offsets */
1765 msix = otx2_mbox_alloc_msg_msix_offset(&pfvf->mbox);
1766 if (!msix) {
1767 mutex_unlock(&pfvf->mbox.lock);
1768 return -ENOMEM;
1769 }
1770
1771 err = otx2_sync_mbox_msg(&pfvf->mbox);
1772 if (err) {
1773 mutex_unlock(&pfvf->mbox.lock);
1774 return err;
1775 }
1776 mutex_unlock(&pfvf->mbox.lock);
1777
1778 if (pfvf->hw.npa_msixoff == MSIX_VECTOR_INVALID ||
1779 pfvf->hw.nix_msixoff == MSIX_VECTOR_INVALID) {
1780 dev_err(pfvf->dev,
1781 "RVUPF: Invalid MSIX vector offset for NPA/NIX\n");
1782 return -EINVAL;
1783 }
1784
1785 return 0;
1786 }
1787 EXPORT_SYMBOL(otx2_attach_npa_nix);
1788
otx2_ctx_disable(struct mbox * mbox,int type,bool npa)1789 void otx2_ctx_disable(struct mbox *mbox, int type, bool npa)
1790 {
1791 struct hwctx_disable_req *req;
1792
1793 mutex_lock(&mbox->lock);
1794 /* Request AQ to disable this context */
1795 if (npa)
1796 req = otx2_mbox_alloc_msg_npa_hwctx_disable(mbox);
1797 else
1798 req = otx2_mbox_alloc_msg_nix_hwctx_disable(mbox);
1799
1800 if (!req) {
1801 mutex_unlock(&mbox->lock);
1802 return;
1803 }
1804
1805 req->ctype = type;
1806
1807 if (otx2_sync_mbox_msg(mbox))
1808 dev_err(mbox->pfvf->dev, "%s failed to disable context\n",
1809 __func__);
1810
1811 mutex_unlock(&mbox->lock);
1812 }
1813
otx2_nix_config_bp(struct otx2_nic * pfvf,bool enable)1814 int otx2_nix_config_bp(struct otx2_nic *pfvf, bool enable)
1815 {
1816 struct nix_bp_cfg_req *req;
1817
1818 if (enable)
1819 req = otx2_mbox_alloc_msg_nix_bp_enable(&pfvf->mbox);
1820 else
1821 req = otx2_mbox_alloc_msg_nix_bp_disable(&pfvf->mbox);
1822
1823 if (!req)
1824 return -ENOMEM;
1825
1826 req->chan_base = 0;
1827 if (otx2_is_pfc_enabled(pfvf)) {
1828 req->chan_cnt = IEEE_8021QAZ_MAX_TCS;
1829 req->bpid_per_chan = 1;
1830 } else {
1831 req->chan_cnt = pfvf->hw.rx_chan_cnt;
1832 req->bpid_per_chan = 0;
1833 }
1834
1835 return otx2_sync_mbox_msg(&pfvf->mbox);
1836 }
1837 EXPORT_SYMBOL(otx2_nix_config_bp);
1838
otx2_nix_cpt_config_bp(struct otx2_nic * pfvf,bool enable)1839 int otx2_nix_cpt_config_bp(struct otx2_nic *pfvf, bool enable)
1840 {
1841 struct nix_bp_cfg_req *req;
1842
1843 if (enable)
1844 req = otx2_mbox_alloc_msg_nix_cpt_bp_enable(&pfvf->mbox);
1845 else
1846 req = otx2_mbox_alloc_msg_nix_cpt_bp_disable(&pfvf->mbox);
1847
1848 if (!req)
1849 return -ENOMEM;
1850
1851 req->chan_base = 0;
1852 if (otx2_is_pfc_enabled(pfvf)) {
1853 req->chan_cnt = IEEE_8021QAZ_MAX_TCS;
1854 req->bpid_per_chan = 1;
1855 } else {
1856 req->chan_cnt = pfvf->hw.rx_chan_cnt;
1857 req->bpid_per_chan = 0;
1858 }
1859
1860 return otx2_sync_mbox_msg(&pfvf->mbox);
1861 }
1862 EXPORT_SYMBOL(otx2_nix_cpt_config_bp);
1863
1864 /* Mbox message handlers */
mbox_handler_cgx_stats(struct otx2_nic * pfvf,struct cgx_stats_rsp * rsp)1865 void mbox_handler_cgx_stats(struct otx2_nic *pfvf,
1866 struct cgx_stats_rsp *rsp)
1867 {
1868 int id;
1869
1870 for (id = 0; id < CGX_RX_STATS_COUNT; id++)
1871 pfvf->hw.cgx_rx_stats[id] = rsp->rx_stats[id];
1872 for (id = 0; id < CGX_TX_STATS_COUNT; id++)
1873 pfvf->hw.cgx_tx_stats[id] = rsp->tx_stats[id];
1874 }
1875
mbox_handler_cgx_fec_stats(struct otx2_nic * pfvf,struct cgx_fec_stats_rsp * rsp)1876 void mbox_handler_cgx_fec_stats(struct otx2_nic *pfvf,
1877 struct cgx_fec_stats_rsp *rsp)
1878 {
1879 pfvf->hw.cgx_fec_corr_blks += rsp->fec_corr_blks;
1880 pfvf->hw.cgx_fec_uncorr_blks += rsp->fec_uncorr_blks;
1881 }
1882
mbox_handler_npa_lf_alloc(struct otx2_nic * pfvf,struct npa_lf_alloc_rsp * rsp)1883 void mbox_handler_npa_lf_alloc(struct otx2_nic *pfvf,
1884 struct npa_lf_alloc_rsp *rsp)
1885 {
1886 pfvf->hw.stack_pg_ptrs = rsp->stack_pg_ptrs;
1887 pfvf->hw.stack_pg_bytes = rsp->stack_pg_bytes;
1888 }
1889 EXPORT_SYMBOL(mbox_handler_npa_lf_alloc);
1890
mbox_handler_nix_lf_alloc(struct otx2_nic * pfvf,struct nix_lf_alloc_rsp * rsp)1891 void mbox_handler_nix_lf_alloc(struct otx2_nic *pfvf,
1892 struct nix_lf_alloc_rsp *rsp)
1893 {
1894 pfvf->hw.sqb_size = rsp->sqb_size;
1895 pfvf->hw.rx_chan_base = rsp->rx_chan_base;
1896 pfvf->hw.tx_chan_base = rsp->tx_chan_base;
1897 pfvf->hw.rx_chan_cnt = rsp->rx_chan_cnt;
1898 pfvf->hw.tx_chan_cnt = rsp->tx_chan_cnt;
1899 pfvf->hw.lso_tsov4_idx = rsp->lso_tsov4_idx;
1900 pfvf->hw.lso_tsov6_idx = rsp->lso_tsov6_idx;
1901 pfvf->hw.cgx_links = rsp->cgx_links;
1902 pfvf->hw.lbk_links = rsp->lbk_links;
1903 pfvf->hw.tx_link = rsp->tx_link;
1904 }
1905 EXPORT_SYMBOL(mbox_handler_nix_lf_alloc);
1906
mbox_handler_msix_offset(struct otx2_nic * pfvf,struct msix_offset_rsp * rsp)1907 void mbox_handler_msix_offset(struct otx2_nic *pfvf,
1908 struct msix_offset_rsp *rsp)
1909 {
1910 pfvf->hw.npa_msixoff = rsp->npa_msixoff;
1911 pfvf->hw.nix_msixoff = rsp->nix_msixoff;
1912 }
1913 EXPORT_SYMBOL(mbox_handler_msix_offset);
1914
mbox_handler_nix_bp_enable(struct otx2_nic * pfvf,struct nix_bp_cfg_rsp * rsp)1915 void mbox_handler_nix_bp_enable(struct otx2_nic *pfvf,
1916 struct nix_bp_cfg_rsp *rsp)
1917 {
1918 int chan, chan_id;
1919
1920 for (chan = 0; chan < rsp->chan_cnt; chan++) {
1921 chan_id = ((rsp->chan_bpid[chan] >> 10) & 0x7F);
1922 pfvf->bpid[chan_id] = rsp->chan_bpid[chan] & 0x3FF;
1923 }
1924 }
1925 EXPORT_SYMBOL(mbox_handler_nix_bp_enable);
1926
otx2_free_cints(struct otx2_nic * pfvf,int n)1927 void otx2_free_cints(struct otx2_nic *pfvf, int n)
1928 {
1929 struct otx2_qset *qset = &pfvf->qset;
1930 struct otx2_hw *hw = &pfvf->hw;
1931 int irq, qidx;
1932
1933 for (qidx = 0, irq = hw->nix_msixoff + NIX_LF_CINT_VEC_START;
1934 qidx < n;
1935 qidx++, irq++) {
1936 int vector = pci_irq_vector(pfvf->pdev, irq);
1937
1938 irq_set_affinity_hint(vector, NULL);
1939 free_cpumask_var(hw->affinity_mask[irq]);
1940 free_irq(vector, &qset->napi[qidx]);
1941 }
1942 }
1943 EXPORT_SYMBOL(otx2_free_cints);
1944
otx2_set_cints_affinity(struct otx2_nic * pfvf)1945 void otx2_set_cints_affinity(struct otx2_nic *pfvf)
1946 {
1947 struct otx2_hw *hw = &pfvf->hw;
1948 int vec, cpu, irq, cint;
1949
1950 vec = hw->nix_msixoff + NIX_LF_CINT_VEC_START;
1951 cpu = cpumask_first(cpu_online_mask);
1952
1953 /* CQ interrupts */
1954 for (cint = 0; cint < pfvf->hw.cint_cnt; cint++, vec++) {
1955 if (!alloc_cpumask_var(&hw->affinity_mask[vec], GFP_KERNEL))
1956 return;
1957
1958 cpumask_set_cpu(cpu, hw->affinity_mask[vec]);
1959
1960 irq = pci_irq_vector(pfvf->pdev, vec);
1961 irq_set_affinity_hint(irq, hw->affinity_mask[vec]);
1962
1963 cpu = cpumask_next(cpu, cpu_online_mask);
1964 if (unlikely(cpu >= nr_cpu_ids))
1965 cpu = 0;
1966 }
1967 }
1968
get_dwrr_mtu(struct otx2_nic * pfvf,struct nix_hw_info * hw)1969 static u32 get_dwrr_mtu(struct otx2_nic *pfvf, struct nix_hw_info *hw)
1970 {
1971 if (is_otx2_lbkvf(pfvf->pdev)) {
1972 pfvf->hw.smq_link_type = SMQ_LINK_TYPE_LBK;
1973 return hw->lbk_dwrr_mtu;
1974 }
1975
1976 pfvf->hw.smq_link_type = SMQ_LINK_TYPE_RPM;
1977 return hw->rpm_dwrr_mtu;
1978 }
1979
otx2_get_max_mtu(struct otx2_nic * pfvf)1980 u16 otx2_get_max_mtu(struct otx2_nic *pfvf)
1981 {
1982 struct nix_hw_info *rsp;
1983 struct msg_req *req;
1984 u16 max_mtu;
1985 int rc;
1986
1987 mutex_lock(&pfvf->mbox.lock);
1988
1989 req = otx2_mbox_alloc_msg_nix_get_hw_info(&pfvf->mbox);
1990 if (!req) {
1991 rc = -ENOMEM;
1992 goto out;
1993 }
1994
1995 rc = otx2_sync_mbox_msg(&pfvf->mbox);
1996 if (!rc) {
1997 rsp = (struct nix_hw_info *)
1998 otx2_mbox_get_rsp(&pfvf->mbox.mbox, 0, &req->hdr);
1999 if (IS_ERR(rsp)) {
2000 rc = PTR_ERR(rsp);
2001 goto out;
2002 }
2003
2004 /* HW counts VLAN insertion bytes (8 for double tag)
2005 * irrespective of whether SQE is requesting to insert VLAN
2006 * in the packet or not. Hence these 8 bytes have to be
2007 * discounted from max packet size otherwise HW will throw
2008 * SMQ errors
2009 */
2010 max_mtu = rsp->max_mtu - 8 - OTX2_ETH_HLEN;
2011
2012 /* Also save DWRR MTU, needed for DWRR weight calculation */
2013 pfvf->hw.dwrr_mtu = get_dwrr_mtu(pfvf, rsp);
2014 if (!pfvf->hw.dwrr_mtu)
2015 pfvf->hw.dwrr_mtu = 1;
2016 }
2017
2018 out:
2019 mutex_unlock(&pfvf->mbox.lock);
2020 if (rc) {
2021 dev_warn(pfvf->dev,
2022 "Failed to get MTU from hardware setting default value(1500)\n");
2023 max_mtu = 1500;
2024 }
2025 return max_mtu;
2026 }
2027 EXPORT_SYMBOL(otx2_get_max_mtu);
2028
otx2_handle_ntuple_tc_features(struct net_device * netdev,netdev_features_t features)2029 int otx2_handle_ntuple_tc_features(struct net_device *netdev, netdev_features_t features)
2030 {
2031 netdev_features_t changed = features ^ netdev->features;
2032 struct otx2_nic *pfvf = netdev_priv(netdev);
2033 bool ntuple = !!(features & NETIF_F_NTUPLE);
2034 bool tc = !!(features & NETIF_F_HW_TC);
2035
2036 if ((changed & NETIF_F_NTUPLE) && !ntuple)
2037 otx2_destroy_ntuple_flows(pfvf);
2038
2039 if ((changed & NETIF_F_NTUPLE) && ntuple) {
2040 if (!pfvf->flow_cfg->max_flows) {
2041 netdev_err(netdev,
2042 "Can't enable NTUPLE, MCAM entries not allocated\n");
2043 return -EINVAL;
2044 }
2045 }
2046
2047 if ((changed & NETIF_F_HW_TC) && !tc &&
2048 otx2_tc_flower_rule_cnt(pfvf)) {
2049 netdev_err(netdev, "Can't disable TC hardware offload while flows are active\n");
2050 return -EBUSY;
2051 }
2052
2053 if ((changed & NETIF_F_NTUPLE) && ntuple &&
2054 otx2_tc_flower_rule_cnt(pfvf) && !(changed & NETIF_F_HW_TC)) {
2055 netdev_err(netdev,
2056 "Can't enable NTUPLE when TC flower offload is active, disable TC rules and retry\n");
2057 return -EINVAL;
2058 }
2059
2060 return 0;
2061 }
2062 EXPORT_SYMBOL(otx2_handle_ntuple_tc_features);
2063
otx2_set_hw_capabilities(struct otx2_nic * pfvf)2064 int otx2_set_hw_capabilities(struct otx2_nic *pfvf)
2065 {
2066 struct mbox *mbox = &pfvf->mbox;
2067 struct otx2_hw *hw = &pfvf->hw;
2068 struct get_hw_cap_rsp *rsp;
2069 struct msg_req *req;
2070 int ret = -ENOMEM;
2071
2072 mutex_lock(&mbox->lock);
2073
2074 req = otx2_mbox_alloc_msg_get_hw_cap(mbox);
2075 if (!req)
2076 goto fail;
2077
2078 ret = otx2_sync_mbox_msg(mbox);
2079 if (ret)
2080 goto fail;
2081
2082 rsp = (struct get_hw_cap_rsp *)otx2_mbox_get_rsp(&pfvf->mbox.mbox,
2083 0, &req->hdr);
2084 if (IS_ERR(rsp)) {
2085 ret = -EINVAL;
2086 goto fail;
2087 }
2088
2089 if (rsp->hw_caps & HW_CAP_MACSEC)
2090 __set_bit(CN10K_HW_MACSEC, &hw->cap_flag);
2091
2092 mutex_unlock(&mbox->lock);
2093
2094 return 0;
2095 fail:
2096 dev_err(pfvf->dev, "Cannot get MACSEC capability from AF\n");
2097 mutex_unlock(&mbox->lock);
2098 return ret;
2099 }
2100
2101 #define M(_name, _id, _fn_name, _req_type, _rsp_type) \
2102 int __weak \
2103 otx2_mbox_up_handler_ ## _fn_name(struct otx2_nic *pfvf, \
2104 struct _req_type *req, \
2105 struct _rsp_type *rsp) \
2106 { \
2107 /* Nothing to do here */ \
2108 return 0; \
2109 } \
2110 EXPORT_SYMBOL(otx2_mbox_up_handler_ ## _fn_name);
2111 MBOX_UP_CGX_MESSAGES
2112 MBOX_UP_MCS_MESSAGES
2113 #undef M
2114
otx2_dma_map_skb_frag(struct otx2_nic * pfvf,struct sk_buff * skb,int seg,int * len)2115 dma_addr_t otx2_dma_map_skb_frag(struct otx2_nic *pfvf,
2116 struct sk_buff *skb, int seg, int *len)
2117 {
2118 enum dma_data_direction dir = DMA_TO_DEVICE;
2119 const skb_frag_t *frag;
2120 struct page *page;
2121 int offset;
2122
2123 /* Crypto hardware need write permission for ipsec crypto offload */
2124 if (unlikely(xfrm_offload(skb))) {
2125 dir = DMA_BIDIRECTIONAL;
2126 skb = skb_unshare(skb, GFP_ATOMIC);
2127 }
2128
2129 /* First segment is always skb->data */
2130 if (!seg) {
2131 page = virt_to_page(skb->data);
2132 offset = offset_in_page(skb->data);
2133 *len = skb_headlen(skb);
2134 } else {
2135 frag = &skb_shinfo(skb)->frags[seg - 1];
2136 page = skb_frag_page(frag);
2137 offset = skb_frag_off(frag);
2138 *len = skb_frag_size(frag);
2139 }
2140 return otx2_dma_map_page(pfvf, page, offset, *len, dir);
2141 }
2142
otx2_dma_unmap_skb_frags(struct otx2_nic * pfvf,struct sg_list * sg)2143 void otx2_dma_unmap_skb_frags(struct otx2_nic *pfvf, struct sg_list *sg)
2144 {
2145 enum dma_data_direction dir = DMA_TO_DEVICE;
2146 struct sk_buff *skb = NULL;
2147 int seg;
2148
2149 skb = (struct sk_buff *)sg->skb;
2150 if (unlikely(xfrm_offload(skb)))
2151 dir = DMA_BIDIRECTIONAL;
2152
2153 for (seg = 0; seg < sg->num_segs; seg++) {
2154 otx2_dma_unmap_page(pfvf, sg->dma_addr[seg],
2155 sg->size[seg], dir);
2156 }
2157 sg->num_segs = 0;
2158 }
2159