xref: /linux/drivers/net/ethernet/marvell/octeontx2/nic/otx2_common.c (revision 258c3ae4a4af0c7948a3d99e7262701aaa350d31)
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 
22 static bool otx2_is_pfc_enabled(struct otx2_nic *pfvf)
23 {
24 	return IS_ENABLED(CONFIG_DCB) && !!pfvf->pfc_en;
25 }
26 
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 
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 
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 
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 
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 
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 
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 
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 */
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 
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 
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 
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 
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 
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 
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 
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 
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 */
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 */
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 
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 
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 
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 
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 
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 
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 
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 
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 
736 int otx2_smq_flush(struct otx2_nic *pfvf, int smq)
737 {
738 	struct nix_txschq_config *req;
739 	int rc;
740 
741 	mutex_lock(&pfvf->mbox.lock);
742 
743 	req = otx2_mbox_alloc_msg_nix_txschq_cfg(&pfvf->mbox);
744 	if (!req) {
745 		mutex_unlock(&pfvf->mbox.lock);
746 		return -ENOMEM;
747 	}
748 
749 	req->lvl = NIX_TXSCH_LVL_SMQ;
750 	req->reg[0] = NIX_AF_SMQX_CFG(smq);
751 	req->regval[0] |= BIT_ULL(49);
752 	req->num_regs++;
753 
754 	rc = otx2_sync_mbox_msg(&pfvf->mbox);
755 	mutex_unlock(&pfvf->mbox.lock);
756 	return rc;
757 }
758 
759 int otx2_txsch_alloc(struct otx2_nic *pfvf)
760 {
761 	int chan_cnt = pfvf->hw.tx_chan_cnt;
762 	struct nix_txsch_alloc_req *req;
763 	struct nix_txsch_alloc_rsp *rsp;
764 	int lvl, schq, rc;
765 
766 	/* Get memory to put this msg */
767 	req = otx2_mbox_alloc_msg_nix_txsch_alloc(&pfvf->mbox);
768 	if (!req)
769 		return -ENOMEM;
770 
771 	/* Request one schq per level */
772 	for (lvl = 0; lvl < NIX_TXSCH_LVL_CNT; lvl++)
773 		req->schq[lvl] = 1;
774 
775 	if (is_otx2_sdp_rep(pfvf->pdev) && chan_cnt > 1) {
776 		req->schq[NIX_TXSCH_LVL_SMQ] = chan_cnt;
777 		req->schq[NIX_TXSCH_LVL_TL4] = chan_cnt;
778 	}
779 
780 	rc = otx2_sync_mbox_msg(&pfvf->mbox);
781 	if (rc)
782 		return rc;
783 
784 	rsp = (struct nix_txsch_alloc_rsp *)
785 	      otx2_mbox_get_rsp(&pfvf->mbox.mbox, 0, &req->hdr);
786 	if (IS_ERR(rsp))
787 		return PTR_ERR(rsp);
788 
789 	/* Setup transmit scheduler list */
790 	for (lvl = 0; lvl < NIX_TXSCH_LVL_CNT; lvl++) {
791 		pfvf->hw.txschq_cnt[lvl] = rsp->schq[lvl];
792 		for (schq = 0; schq < rsp->schq[lvl]; schq++)
793 			pfvf->hw.txschq_list[lvl][schq] =
794 				rsp->schq_list[lvl][schq];
795 	}
796 
797 	pfvf->hw.txschq_link_cfg_lvl = rsp->link_cfg_lvl;
798 	pfvf->hw.txschq_aggr_lvl_rr_prio = rsp->aggr_lvl_rr_prio;
799 
800 	return 0;
801 }
802 
803 void otx2_txschq_free_one(struct otx2_nic *pfvf, u16 lvl, u16 schq)
804 {
805 	struct nix_txsch_free_req *free_req;
806 	int err;
807 
808 	mutex_lock(&pfvf->mbox.lock);
809 
810 	free_req = otx2_mbox_alloc_msg_nix_txsch_free(&pfvf->mbox);
811 	if (!free_req) {
812 		mutex_unlock(&pfvf->mbox.lock);
813 		netdev_err(pfvf->netdev,
814 			   "Failed alloc txschq free req\n");
815 		return;
816 	}
817 
818 	free_req->schq_lvl = lvl;
819 	free_req->schq = schq;
820 
821 	err = otx2_sync_mbox_msg(&pfvf->mbox);
822 	if (err) {
823 		netdev_err(pfvf->netdev,
824 			   "Failed stop txschq %d at level %d\n", schq, lvl);
825 	}
826 
827 	mutex_unlock(&pfvf->mbox.lock);
828 }
829 
830 void otx2_txschq_stop(struct otx2_nic *pfvf)
831 {
832 	int lvl, schq, idx;
833 
834 	/* free non QOS TLx nodes */
835 	for (lvl = 0; lvl < NIX_TXSCH_LVL_CNT; lvl++) {
836 		for (idx = 0; idx < pfvf->hw.txschq_cnt[lvl]; idx++) {
837 			otx2_txschq_free_one(pfvf, lvl,
838 					     pfvf->hw.txschq_list[lvl][idx]);
839 		}
840 	}
841 
842 	/* Clear the txschq list */
843 	for (lvl = 0; lvl < NIX_TXSCH_LVL_CNT; lvl++) {
844 		for (schq = 0; schq < MAX_TXSCHQ_PER_FUNC; schq++)
845 			pfvf->hw.txschq_list[lvl][schq] = 0;
846 	}
847 
848 }
849 
850 void otx2_sqb_flush(struct otx2_nic *pfvf)
851 {
852 	int qidx, sqe_tail, sqe_head;
853 	struct otx2_snd_queue *sq;
854 	void __iomem *ptr;
855 	u64 incr, val;
856 
857 	ptr = otx2_get_regaddr(pfvf, NIX_LF_SQ_OP_STATUS);
858 	for (qidx = 0; qidx < otx2_get_total_tx_queues(pfvf); qidx++) {
859 		sq = &pfvf->qset.sq[qidx];
860 		if (!sq->sqb_ptrs)
861 			continue;
862 
863 		incr = (u64)qidx << 32;
864 		val = otx2_atomic64_add(incr, ptr);
865 		sqe_head = (val >> 20) & 0x3F;
866 		sqe_tail = (val >> 28) & 0x3F;
867 		if (sqe_head != sqe_tail)
868 			usleep_range(50, 60);
869 	}
870 }
871 
872 /* RED and drop levels of CQ on packet reception.
873  * For CQ level is measure of emptiness ( 0x0 = full, 255 = empty).
874  */
875 #define RQ_PASS_LVL_CQ(skid, qsize)	((((skid) + 16) * 256) / (qsize))
876 #define RQ_DROP_LVL_CQ(skid, qsize)	(((skid) * 256) / (qsize))
877 
878 /* RED and drop levels of AURA for packet reception.
879  * For AURA level is measure of fullness (0x0 = empty, 255 = full).
880  * Eg: For RQ length 1K, for pass/drop level 204/230.
881  * RED accepts pkts if free pointers > 102 & <= 205.
882  * Drops pkts if free pointers < 102.
883  */
884 #define RQ_BP_LVL_AURA   (255 - ((85 * 256) / 100)) /* BP when 85% is full */
885 #define RQ_PASS_LVL_AURA (255 - ((95 * 256) / 100)) /* RED when 95% is full */
886 #define RQ_DROP_LVL_AURA (255 - ((99 * 256) / 100)) /* Drop when 99% is full */
887 
888 int otx2_rq_init(struct otx2_nic *pfvf, u16 qidx, u16 lpb_aura)
889 {
890 	struct otx2_qset *qset = &pfvf->qset;
891 	struct nix_aq_enq_req *aq;
892 
893 	/* Get memory to put this msg */
894 	aq = otx2_mbox_alloc_msg_nix_aq_enq(&pfvf->mbox);
895 	if (!aq)
896 		return -ENOMEM;
897 
898 	aq->rq.cq = qidx;
899 	aq->rq.ena = 1;
900 	aq->rq.pb_caching = 1;
901 	aq->rq.lpb_aura = lpb_aura; /* Use large packet buffer aura */
902 	aq->rq.lpb_sizem1 = (DMA_BUFFER_LEN(pfvf->rbsize) / 8) - 1;
903 	aq->rq.xqe_imm_size = 0; /* Copying of packet to CQE not needed */
904 	aq->rq.flow_tagw = 32; /* Copy full 32bit flow_tag to CQE header */
905 	aq->rq.qint_idx = 0;
906 	aq->rq.lpb_drop_ena = 1; /* Enable RED dropping for AURA */
907 	aq->rq.xqe_drop_ena = 1; /* Enable RED dropping for CQ/SSO */
908 	aq->rq.xqe_pass = RQ_PASS_LVL_CQ(pfvf->hw.rq_skid, qset->rqe_cnt);
909 	aq->rq.xqe_drop = RQ_DROP_LVL_CQ(pfvf->hw.rq_skid, qset->rqe_cnt);
910 	aq->rq.lpb_aura_pass = RQ_PASS_LVL_AURA;
911 	aq->rq.lpb_aura_drop = RQ_DROP_LVL_AURA;
912 
913 	/* Fill AQ info */
914 	aq->qidx = qidx;
915 	aq->ctype = NIX_AQ_CTYPE_RQ;
916 	aq->op = NIX_AQ_INSTOP_INIT;
917 
918 	return otx2_sync_mbox_msg(&pfvf->mbox);
919 }
920 
921 int otx2_sq_aq_init(void *dev, u16 qidx, u8 chan_offset, u16 sqb_aura)
922 {
923 	struct otx2_nic *pfvf = dev;
924 	struct otx2_snd_queue *sq;
925 	struct nix_aq_enq_req *aq;
926 
927 	sq = &pfvf->qset.sq[qidx];
928 	sq->lmt_addr = (__force u64 *)(pfvf->reg_base + LMT_LF_LMTLINEX(qidx));
929 	/* Get memory to put this msg */
930 	aq = otx2_mbox_alloc_msg_nix_aq_enq(&pfvf->mbox);
931 	if (!aq)
932 		return -ENOMEM;
933 
934 	aq->sq.cq = pfvf->hw.rx_queues + qidx;
935 	aq->sq.max_sqe_size = NIX_MAXSQESZ_W16; /* 128 byte */
936 	aq->sq.cq_ena = 1;
937 	aq->sq.ena = 1;
938 	aq->sq.smq = otx2_get_smq_idx(pfvf, qidx);
939 	aq->sq.smq_rr_quantum = mtu_to_dwrr_weight(pfvf, pfvf->tx_max_pktlen);
940 	aq->sq.default_chan = pfvf->hw.tx_chan_base + chan_offset;
941 	aq->sq.sqe_stype = NIX_STYPE_STF; /* Cache SQB */
942 	aq->sq.sqb_aura = sqb_aura;
943 	aq->sq.sq_int_ena = NIX_SQINT_BITS;
944 	aq->sq.qint_idx = 0;
945 	/* Due pipelining impact minimum 2000 unused SQ CQE's
946 	 * need to maintain to avoid CQ overflow.
947 	 */
948 	aq->sq.cq_limit = ((SEND_CQ_SKID * 256) / (pfvf->qset.sqe_cnt));
949 
950 	/* Fill AQ info */
951 	aq->qidx = qidx;
952 	aq->ctype = NIX_AQ_CTYPE_SQ;
953 	aq->op = NIX_AQ_INSTOP_INIT;
954 
955 	return otx2_sync_mbox_msg(&pfvf->mbox);
956 }
957 
958 int otx2_sq_init(struct otx2_nic *pfvf, u16 qidx, u16 sqb_aura)
959 {
960 	struct otx2_qset *qset = &pfvf->qset;
961 	struct otx2_snd_queue *sq;
962 	struct otx2_pool *pool;
963 	u8 chan_offset;
964 	int err;
965 
966 	pool = &pfvf->qset.pool[sqb_aura];
967 	sq = &qset->sq[qidx];
968 	sq->sqe_size = NIX_SQESZ_W16 ? 64 : 128;
969 	sq->sqe_cnt = qset->sqe_cnt;
970 
971 	err = qmem_alloc(pfvf->dev, &sq->sqe, 1, sq->sqe_size);
972 	if (err)
973 		return err;
974 
975 	/* Allocate memory for NIX SQE (which includes NIX SG) and CPT SG.
976 	 * SG of NIX and CPT are same in size. Allocate memory for CPT SG
977 	 * same as NIX SQE for base address alignment.
978 	 * Layout of a NIX SQE and CPT SG entry:
979 	 *      -----------------------------
980 	 *     |     CPT Scatter Gather      |
981 	 *     |       (SQE SIZE)            |
982 	 *     |                             |
983 	 *      -----------------------------
984 	 *     |       NIX SQE               |
985 	 *     |       (SQE SIZE)            |
986 	 *     |                             |
987 	 *      -----------------------------
988 	 */
989 	err = qmem_alloc(pfvf->dev, &sq->sqe_ring, qset->sqe_cnt,
990 			 sq->sqe_size * 2);
991 	if (err)
992 		return err;
993 
994 	err = qmem_alloc(pfvf->dev, &sq->cpt_resp, qset->sqe_cnt, 64);
995 	if (err)
996 		return err;
997 
998 	if (qidx < pfvf->hw.tx_queues) {
999 		err = qmem_alloc(pfvf->dev, &sq->tso_hdrs, qset->sqe_cnt,
1000 				 TSO_HEADER_SIZE);
1001 		if (err)
1002 			return err;
1003 	}
1004 
1005 	sq->sqe_base = sq->sqe->base;
1006 	sq->sg = kzalloc_objs(struct sg_list, qset->sqe_cnt);
1007 	if (!sq->sg)
1008 		return -ENOMEM;
1009 
1010 	if (pfvf->ptp && qidx < pfvf->hw.tx_queues) {
1011 		err = qmem_alloc(pfvf->dev, &sq->timestamps, qset->sqe_cnt,
1012 				 sizeof(*sq->timestamps));
1013 		if (err) {
1014 			kfree(sq->sg);
1015 			sq->sg = NULL;
1016 			return err;
1017 		}
1018 	}
1019 
1020 	sq->head = 0;
1021 	sq->cons_head = 0;
1022 	sq->sqe_per_sqb = (pfvf->hw.sqb_size / sq->sqe_size) - 1;
1023 	sq->num_sqbs = (qset->sqe_cnt + sq->sqe_per_sqb) / sq->sqe_per_sqb;
1024 	/* Set SQE threshold to 10% of total SQEs */
1025 	sq->sqe_thresh = ((sq->num_sqbs * sq->sqe_per_sqb) * 10) / 100;
1026 	sq->aura_id = sqb_aura;
1027 	sq->aura_fc_addr = pool->fc_addr->base;
1028 	sq->io_addr = (__force u64)otx2_get_regaddr(pfvf, NIX_LF_OP_SENDX(0));
1029 
1030 	sq->stats.bytes = 0;
1031 	sq->stats.pkts = 0;
1032 	/* Attach XSK_BUFF_POOL to XDP queue */
1033 	if (qidx > pfvf->hw.xdp_queues)
1034 		otx2_attach_xsk_buff(pfvf, sq, (qidx - pfvf->hw.xdp_queues));
1035 
1036 	chan_offset = qidx % pfvf->hw.tx_chan_cnt;
1037 	err = pfvf->hw_ops->sq_aq_init(pfvf, qidx, chan_offset, sqb_aura);
1038 	if (err) {
1039 		kfree(sq->sg);
1040 		sq->sg = NULL;
1041 		return err;
1042 	}
1043 
1044 	return 0;
1045 
1046 }
1047 
1048 int otx2_cq_init(struct otx2_nic *pfvf, u16 qidx)
1049 {
1050 	struct otx2_qset *qset = &pfvf->qset;
1051 	int err, pool_id, non_xdp_queues;
1052 	struct nix_aq_enq_req *aq;
1053 	struct otx2_cq_queue *cq;
1054 	struct otx2_pool *pool;
1055 	u8 bpid_idx;
1056 
1057 	cq = &qset->cq[qidx];
1058 	cq->cq_idx = qidx;
1059 	non_xdp_queues = pfvf->hw.rx_queues + pfvf->hw.tx_queues;
1060 	if (qidx < pfvf->hw.rx_queues) {
1061 		cq->cq_type = CQ_RX;
1062 		cq->cint_idx = qidx;
1063 		cq->cqe_cnt = qset->rqe_cnt;
1064 		if (pfvf->xdp_prog) {
1065 			xdp_rxq_info_reg(&cq->xdp_rxq, pfvf->netdev, qidx, 0);
1066 			pool = &qset->pool[qidx];
1067 			if (pool->xsk_pool) {
1068 				xdp_rxq_info_reg_mem_model(&cq->xdp_rxq,
1069 							   MEM_TYPE_XSK_BUFF_POOL,
1070 							   NULL);
1071 				xsk_pool_set_rxq_info(pool->xsk_pool, &cq->xdp_rxq);
1072 			} else if (pool->page_pool) {
1073 				xdp_rxq_info_reg_mem_model(&cq->xdp_rxq,
1074 							   MEM_TYPE_PAGE_POOL,
1075 							   pool->page_pool);
1076 			}
1077 		}
1078 	} else if (qidx < non_xdp_queues) {
1079 		cq->cq_type = CQ_TX;
1080 		cq->cint_idx = qidx - pfvf->hw.rx_queues;
1081 		cq->cqe_cnt = qset->sqe_cnt;
1082 	} else {
1083 		if (pfvf->hw.xdp_queues &&
1084 		    qidx < non_xdp_queues + pfvf->hw.xdp_queues) {
1085 			cq->cq_type = CQ_XDP;
1086 			cq->cint_idx = qidx - non_xdp_queues;
1087 			cq->cqe_cnt = qset->sqe_cnt;
1088 		} else {
1089 			cq->cq_type = CQ_QOS;
1090 			cq->cint_idx = qidx - non_xdp_queues -
1091 				       pfvf->hw.xdp_queues;
1092 			cq->cqe_cnt = qset->sqe_cnt;
1093 		}
1094 	}
1095 	cq->cqe_size = pfvf->qset.xqe_size;
1096 
1097 	/* Allocate memory for CQEs */
1098 	err = qmem_alloc(pfvf->dev, &cq->cqe, cq->cqe_cnt, cq->cqe_size);
1099 	if (err)
1100 		return err;
1101 
1102 	/* Save CQE CPU base for faster reference */
1103 	cq->cqe_base = cq->cqe->base;
1104 	/* In case where all RQs auras point to single pool,
1105 	 * all CQs receive buffer pool also point to same pool.
1106 	 */
1107 	pool_id = ((cq->cq_type == CQ_RX) &&
1108 		   (pfvf->hw.rqpool_cnt != pfvf->hw.rx_queues)) ? 0 : qidx;
1109 	cq->rbpool = &qset->pool[pool_id];
1110 	cq->refill_task_sched = false;
1111 
1112 	/* Get memory to put this msg */
1113 	aq = otx2_mbox_alloc_msg_nix_aq_enq(&pfvf->mbox);
1114 	if (!aq)
1115 		return -ENOMEM;
1116 
1117 	aq->cq.ena = 1;
1118 	aq->cq.qsize = Q_SIZE(cq->cqe_cnt, 4);
1119 	aq->cq.caching = 1;
1120 	aq->cq.base = cq->cqe->iova;
1121 	aq->cq.cint_idx = cq->cint_idx;
1122 	aq->cq.cq_err_int_ena = NIX_CQERRINT_BITS;
1123 	aq->cq.qint_idx = 0;
1124 	aq->cq.avg_level = 255;
1125 
1126 	if (qidx < pfvf->hw.rx_queues) {
1127 		aq->cq.drop = RQ_DROP_LVL_CQ(pfvf->hw.rq_skid, cq->cqe_cnt);
1128 		aq->cq.drop_ena = 1;
1129 
1130 		if (!is_otx2_lbkvf(pfvf->pdev)) {
1131 			/* Enable receive CQ backpressure */
1132 			aq->cq.bp_ena = 1;
1133 			bpid_idx = otx2_get_bpid_idx(pfvf, qidx);
1134 			aq->cq.bpid = pfvf->bpid[bpid_idx];
1135 
1136 			/* Set backpressure level is same as cq pass level */
1137 			aq->cq.bp = RQ_PASS_LVL_CQ(pfvf->hw.rq_skid, qset->rqe_cnt);
1138 		}
1139 	}
1140 
1141 	/* Fill AQ info */
1142 	aq->qidx = qidx;
1143 	aq->ctype = NIX_AQ_CTYPE_CQ;
1144 	aq->op = NIX_AQ_INSTOP_INIT;
1145 
1146 	return otx2_sync_mbox_msg(&pfvf->mbox);
1147 }
1148 
1149 static void otx2_pool_refill_task(struct work_struct *work)
1150 {
1151 	struct otx2_cq_queue *cq;
1152 	struct refill_work *wrk;
1153 	struct otx2_nic *pfvf;
1154 	int qidx;
1155 
1156 	wrk = container_of(work, struct refill_work, pool_refill_work.work);
1157 	pfvf = wrk->pf;
1158 	qidx = wrk - pfvf->refill_wrk;
1159 	cq = &pfvf->qset.cq[qidx];
1160 
1161 	cq->refill_task_sched = false;
1162 
1163 	local_bh_disable();
1164 	napi_schedule(wrk->napi);
1165 	local_bh_enable();
1166 }
1167 
1168 int otx2_config_nix_queues(struct otx2_nic *pfvf)
1169 {
1170 	int qidx, err;
1171 
1172 	/* Initialize RX queues */
1173 	for (qidx = 0; qidx < pfvf->hw.rx_queues; qidx++) {
1174 		u16 lpb_aura = otx2_get_pool_idx(pfvf, AURA_NIX_RQ, qidx);
1175 
1176 		err = otx2_rq_init(pfvf, qidx, lpb_aura);
1177 		if (err)
1178 			return err;
1179 	}
1180 
1181 	/* Initialize TX queues */
1182 	for (qidx = 0; qidx < pfvf->hw.non_qos_queues; qidx++) {
1183 		u16 sqb_aura = otx2_get_pool_idx(pfvf, AURA_NIX_SQ, qidx);
1184 
1185 		err = otx2_sq_init(pfvf, qidx, sqb_aura);
1186 		if (err)
1187 			return err;
1188 	}
1189 
1190 	/* Initialize completion queues */
1191 	for (qidx = 0; qidx < pfvf->qset.cq_cnt; qidx++) {
1192 		err = otx2_cq_init(pfvf, qidx);
1193 		if (err)
1194 			return err;
1195 	}
1196 
1197 	pfvf->cq_op_addr = (__force u64 *)otx2_get_regaddr(pfvf,
1198 							   NIX_LF_CQ_OP_STATUS);
1199 
1200 	/* Initialize work queue for receive buffer refill */
1201 	pfvf->refill_wrk = devm_kcalloc(pfvf->dev, pfvf->qset.cq_cnt,
1202 					sizeof(struct refill_work), GFP_KERNEL);
1203 	if (!pfvf->refill_wrk)
1204 		return -ENOMEM;
1205 
1206 	for (qidx = 0; qidx < pfvf->qset.cq_cnt; qidx++) {
1207 		pfvf->refill_wrk[qidx].pf = pfvf;
1208 		INIT_DELAYED_WORK(&pfvf->refill_wrk[qidx].pool_refill_work,
1209 				  otx2_pool_refill_task);
1210 	}
1211 	return 0;
1212 }
1213 
1214 int otx2_config_nix(struct otx2_nic *pfvf)
1215 {
1216 	struct nix_lf_alloc_req  *nixlf;
1217 	struct nix_lf_alloc_rsp *rsp;
1218 	int err;
1219 
1220 	pfvf->qset.xqe_size = pfvf->hw.xqe_size;
1221 
1222 	/* Get memory to put this msg */
1223 	nixlf = otx2_mbox_alloc_msg_nix_lf_alloc(&pfvf->mbox);
1224 	if (!nixlf)
1225 		return -ENOMEM;
1226 
1227 	/* Set RQ/SQ/CQ counts */
1228 	nixlf->rq_cnt = pfvf->hw.rx_queues;
1229 	nixlf->sq_cnt = otx2_get_total_tx_queues(pfvf);
1230 	nixlf->cq_cnt = pfvf->qset.cq_cnt;
1231 	nixlf->rss_sz = MAX_RSS_INDIR_TBL_SIZE;
1232 	nixlf->rss_grps = MAX_RSS_GROUPS;
1233 	nixlf->xqe_sz = pfvf->hw.xqe_size == 128 ? NIX_XQESZ_W16 : NIX_XQESZ_W64;
1234 	/* We don't know absolute NPA LF idx attached.
1235 	 * AF will replace 'RVU_DEFAULT_PF_FUNC' with
1236 	 * NPA LF attached to this RVU PF/VF.
1237 	 */
1238 	nixlf->npa_func = RVU_DEFAULT_PF_FUNC;
1239 	/* Disable alignment pad, enable L2 length check,
1240 	 * enable L4 TCP/UDP checksum verification.
1241 	 */
1242 	nixlf->rx_cfg = BIT_ULL(33) | BIT_ULL(35) | BIT_ULL(37);
1243 
1244 	err = otx2_sync_mbox_msg(&pfvf->mbox);
1245 	if (err)
1246 		return err;
1247 
1248 	rsp = (struct nix_lf_alloc_rsp *)otx2_mbox_get_rsp(&pfvf->mbox.mbox, 0,
1249 							   &nixlf->hdr);
1250 	if (IS_ERR(rsp))
1251 		return PTR_ERR(rsp);
1252 
1253 	if (rsp->qints < 1)
1254 		return -ENXIO;
1255 
1256 	return rsp->hdr.rc;
1257 }
1258 
1259 void otx2_sq_free_sqbs(struct otx2_nic *pfvf)
1260 {
1261 	struct otx2_qset *qset = &pfvf->qset;
1262 	struct otx2_hw *hw = &pfvf->hw;
1263 	struct otx2_snd_queue *sq;
1264 	int sqb, qidx;
1265 	u64 iova, pa;
1266 
1267 	for (qidx = 0; qidx < otx2_get_total_tx_queues(pfvf); qidx++) {
1268 		sq = &qset->sq[qidx];
1269 		if (!sq->sqb_ptrs)
1270 			continue;
1271 		for (sqb = 0; sqb < sq->sqb_count; sqb++) {
1272 			if (!sq->sqb_ptrs[sqb])
1273 				continue;
1274 			iova = sq->sqb_ptrs[sqb];
1275 			pa = otx2_iova_to_phys(pfvf->iommu_domain, iova);
1276 			dma_unmap_page_attrs(pfvf->dev, iova, hw->sqb_size,
1277 					     DMA_FROM_DEVICE,
1278 					     DMA_ATTR_SKIP_CPU_SYNC);
1279 			put_page(virt_to_page(phys_to_virt(pa)));
1280 		}
1281 		sq->sqb_count = 0;
1282 	}
1283 }
1284 
1285 void otx2_free_bufs(struct otx2_nic *pfvf, struct otx2_pool *pool,
1286 		    u64 iova, int size)
1287 {
1288 	struct page *page;
1289 	u64 pa;
1290 
1291 	pa = otx2_iova_to_phys(pfvf->iommu_domain, iova);
1292 	page = virt_to_head_page(phys_to_virt(pa));
1293 	if (pool->page_pool) {
1294 		page_pool_put_full_page(pool->page_pool, page, true);
1295 	} else if (pool->xsk_pool) {
1296 		/* Note: No way of identifying xdp_buff */
1297 	} else {
1298 		dma_unmap_page_attrs(pfvf->dev, iova, size,
1299 				     DMA_FROM_DEVICE,
1300 				     DMA_ATTR_SKIP_CPU_SYNC);
1301 
1302 		put_page(page);
1303 	}
1304 }
1305 
1306 void otx2_free_aura_ptr(struct otx2_nic *pfvf, int type)
1307 {
1308 	int pool_id, pool_start = 0, pool_end = 0, size = 0;
1309 	struct otx2_pool *pool;
1310 	u64 iova;
1311 	int idx;
1312 
1313 	if (type == AURA_NIX_SQ) {
1314 		pool_start = otx2_get_pool_idx(pfvf, type, 0);
1315 		pool_end =  pool_start + pfvf->hw.sqpool_cnt;
1316 		size = pfvf->hw.sqb_size;
1317 	}
1318 	if (type == AURA_NIX_RQ) {
1319 		pool_start = otx2_get_pool_idx(pfvf, type, 0);
1320 		pool_end = pfvf->hw.rqpool_cnt;
1321 		size = pfvf->rbsize;
1322 	}
1323 
1324 	/* Free SQB and RQB pointers from the aura pool */
1325 	for (pool_id = pool_start; pool_id < pool_end; pool_id++) {
1326 		pool = &pfvf->qset.pool[pool_id];
1327 		iova = otx2_aura_allocptr(pfvf, pool_id);
1328 		while (iova) {
1329 			if (type == AURA_NIX_RQ)
1330 				iova -= OTX2_HEAD_ROOM;
1331 			otx2_free_bufs(pfvf, pool, iova, size);
1332 			iova = otx2_aura_allocptr(pfvf, pool_id);
1333 		}
1334 
1335 		for (idx = 0 ; idx < pool->xdp_cnt; idx++) {
1336 			if (!pool->xdp[idx])
1337 				continue;
1338 
1339 			xsk_buff_free(pool->xdp[idx]);
1340 		}
1341 	}
1342 }
1343 
1344 void otx2_aura_pool_free(struct otx2_nic *pfvf)
1345 {
1346 	struct otx2_pool *pool;
1347 	int pool_id;
1348 
1349 	if (!pfvf->qset.pool)
1350 		return;
1351 
1352 	for (pool_id = 0; pool_id < pfvf->hw.pool_cnt; pool_id++) {
1353 		pool = &pfvf->qset.pool[pool_id];
1354 		qmem_free(pfvf->dev, pool->stack);
1355 		qmem_free(pfvf->dev, pool->fc_addr);
1356 		page_pool_destroy(pool->page_pool);
1357 		devm_kfree(pfvf->dev, pool->xdp);
1358 		pool->xsk_pool = NULL;
1359 	}
1360 	devm_kfree(pfvf->dev, pfvf->qset.pool);
1361 	pfvf->qset.pool = NULL;
1362 }
1363 
1364 int otx2_aura_init(struct otx2_nic *pfvf, int aura_id,
1365 		   int pool_id, int numptrs)
1366 {
1367 	return pfvf->hw_ops->aura_aq_init(pfvf, aura_id, pool_id,
1368 					  numptrs);
1369 }
1370 
1371 int otx2_aura_aq_init(struct otx2_nic *pfvf, int aura_id,
1372 		      int pool_id, int numptrs)
1373 {
1374 	struct npa_aq_enq_req *aq;
1375 	struct otx2_pool *pool;
1376 	u8 bpid_idx;
1377 	int err;
1378 
1379 	pool = &pfvf->qset.pool[pool_id];
1380 
1381 	/* Allocate memory for HW to update Aura count.
1382 	 * Alloc one cache line, so that it fits all FC_STYPE modes.
1383 	 */
1384 	if (!pool->fc_addr) {
1385 		err = qmem_alloc(pfvf->dev, &pool->fc_addr, 1, OTX2_ALIGN);
1386 		if (err)
1387 			return err;
1388 	}
1389 
1390 	/* Initialize this aura's context via AF */
1391 	aq = otx2_mbox_alloc_msg_npa_aq_enq(&pfvf->mbox);
1392 	if (!aq) {
1393 		/* Shared mbox memory buffer is full, flush it and retry */
1394 		err = otx2_sync_mbox_msg(&pfvf->mbox);
1395 		if (err)
1396 			return err;
1397 		aq = otx2_mbox_alloc_msg_npa_aq_enq(&pfvf->mbox);
1398 		if (!aq)
1399 			return -ENOMEM;
1400 	}
1401 
1402 	aq->aura_id = aura_id;
1403 	/* Will be filled by AF with correct pool context address */
1404 	aq->aura.pool_addr = pool_id;
1405 	aq->aura.pool_caching = 1;
1406 	aq->aura.shift = ilog2(numptrs) - 8;
1407 	aq->aura.count = numptrs;
1408 	aq->aura.limit = numptrs;
1409 	aq->aura.avg_level = 255;
1410 	aq->aura.ena = 1;
1411 	aq->aura.fc_ena = 1;
1412 	aq->aura.fc_addr = pool->fc_addr->iova;
1413 	aq->aura.fc_hyst_bits = 0; /* Store count on all updates */
1414 
1415 	/* Enable backpressure for RQ aura */
1416 	if (aura_id < pfvf->hw.rqpool_cnt && !is_otx2_lbkvf(pfvf->pdev)) {
1417 		aq->aura.bp_ena = 0;
1418 		/* If NIX1 LF is attached then specify NIX1_RX.
1419 		 *
1420 		 * Below NPA_AURA_S[BP_ENA] is set according to the
1421 		 * NPA_BPINTF_E enumeration given as:
1422 		 * 0x0 + a*0x1 where 'a' is 0 for NIX0_RX and 1 for NIX1_RX so
1423 		 * NIX0_RX is 0x0 + 0*0x1 = 0
1424 		 * NIX1_RX is 0x0 + 1*0x1 = 1
1425 		 * But in HRM it is given that
1426 		 * "NPA_AURA_S[BP_ENA](w1[33:32]) - Enable aura backpressure to
1427 		 * NIX-RX based on [BP] level. One bit per NIX-RX; index
1428 		 * enumerated by NPA_BPINTF_E."
1429 		 */
1430 		if (pfvf->nix_blkaddr == BLKADDR_NIX1)
1431 			aq->aura.bp_ena = 1;
1432 		bpid_idx = otx2_get_bpid_idx(pfvf, aura_id);
1433 		aq->aura.nix0_bpid = pfvf->bpid[bpid_idx];
1434 
1435 		/* Set backpressure level for RQ's Aura */
1436 		aq->aura.bp = RQ_BP_LVL_AURA;
1437 	}
1438 
1439 	/* Fill AQ info */
1440 	aq->ctype = NPA_AQ_CTYPE_AURA;
1441 	aq->op = NPA_AQ_INSTOP_INIT;
1442 
1443 	return 0;
1444 }
1445 
1446 int otx2_pool_init(struct otx2_nic *pfvf, u16 pool_id,
1447 		   int stack_pages, int numptrs, int buf_size, int type)
1448 {
1449 	return pfvf->hw_ops->pool_aq_init(pfvf, pool_id, stack_pages, numptrs,
1450 					  buf_size, type);
1451 }
1452 
1453 int otx2_pool_aq_init(struct otx2_nic *pfvf, u16 pool_id,
1454 		      int stack_pages, int numptrs, int buf_size, int type)
1455 {
1456 	struct page_pool_params pp_params = { 0 };
1457 	struct xsk_buff_pool *xsk_pool;
1458 	struct npa_aq_enq_req *aq;
1459 	struct otx2_pool *pool;
1460 	int err;
1461 
1462 	pool = &pfvf->qset.pool[pool_id];
1463 	/* Alloc memory for stack which is used to store buffer pointers */
1464 	err = qmem_alloc(pfvf->dev, &pool->stack,
1465 			 stack_pages, pfvf->hw.stack_pg_bytes);
1466 	if (err)
1467 		return err;
1468 
1469 	pool->rbsize = buf_size;
1470 
1471 	/* Initialize this pool's context via AF */
1472 	aq = otx2_mbox_alloc_msg_npa_aq_enq(&pfvf->mbox);
1473 	if (!aq) {
1474 		/* Shared mbox memory buffer is full, flush it and retry */
1475 		err = otx2_sync_mbox_msg(&pfvf->mbox);
1476 		if (err) {
1477 			qmem_free(pfvf->dev, pool->stack);
1478 			pool->stack = NULL;
1479 			return err;
1480 		}
1481 		aq = otx2_mbox_alloc_msg_npa_aq_enq(&pfvf->mbox);
1482 		if (!aq) {
1483 			qmem_free(pfvf->dev, pool->stack);
1484 			pool->stack = NULL;
1485 			return -ENOMEM;
1486 		}
1487 	}
1488 
1489 	aq->aura_id = pool_id;
1490 	aq->pool.stack_base = pool->stack->iova;
1491 	aq->pool.stack_caching = 1;
1492 	aq->pool.ena = 1;
1493 	aq->pool.buf_size = buf_size / 128;
1494 	aq->pool.stack_max_pages = stack_pages;
1495 	aq->pool.shift = ilog2(numptrs) - 8;
1496 	aq->pool.ptr_start = 0;
1497 	aq->pool.ptr_end = ~0ULL;
1498 
1499 	/* Fill AQ info */
1500 	aq->ctype = NPA_AQ_CTYPE_POOL;
1501 	aq->op = NPA_AQ_INSTOP_INIT;
1502 
1503 	if (type != AURA_NIX_RQ)
1504 		return 0;
1505 
1506 	if (!pfvf->af_xdp_zc_qidx ||
1507 	    !test_bit(pool_id, pfvf->af_xdp_zc_qidx)) {
1508 		pp_params.order = get_order(buf_size);
1509 		pp_params.flags = PP_FLAG_DMA_MAP;
1510 		pp_params.pool_size = min(OTX2_PAGE_POOL_SZ, numptrs);
1511 		pp_params.nid = NUMA_NO_NODE;
1512 		pp_params.dev = pfvf->dev;
1513 		pp_params.dma_dir = DMA_FROM_DEVICE;
1514 		pp_params.netdev = pfvf->netdev;
1515 		pool->page_pool = page_pool_create(&pp_params);
1516 		if (IS_ERR(pool->page_pool)) {
1517 			netdev_err(pfvf->netdev, "Creation of page pool failed\n");
1518 			return PTR_ERR(pool->page_pool);
1519 		}
1520 		return 0;
1521 	}
1522 
1523 	/* Set XSK pool to support AF_XDP zero-copy */
1524 	xsk_pool = xsk_get_pool_from_qid(pfvf->netdev, pool_id);
1525 	if (xsk_pool) {
1526 		pool->xsk_pool = xsk_pool;
1527 		pool->xdp_cnt = numptrs;
1528 		pool->xdp = devm_kcalloc(pfvf->dev,
1529 					 numptrs, sizeof(struct xdp_buff *), GFP_KERNEL);
1530 		if (!pool->xdp)
1531 			return -ENOMEM;
1532 	}
1533 
1534 	return 0;
1535 }
1536 
1537 int otx2_sq_aura_pool_init(struct otx2_nic *pfvf)
1538 {
1539 	int qidx, pool_id, stack_pages, num_sqbs;
1540 	struct otx2_qset *qset = &pfvf->qset;
1541 	struct otx2_hw *hw = &pfvf->hw;
1542 	struct otx2_snd_queue *sq;
1543 	struct otx2_pool *pool;
1544 	dma_addr_t bufptr;
1545 	int err, ptr;
1546 
1547 	/* Calculate number of SQBs needed.
1548 	 *
1549 	 * For a 128byte SQE, and 4K size SQB, 31 SQEs will fit in one SQB.
1550 	 * Last SQE is used for pointing to next SQB.
1551 	 */
1552 	num_sqbs = (hw->sqb_size / 128) - 1;
1553 	num_sqbs = (qset->sqe_cnt + num_sqbs) / num_sqbs;
1554 
1555 	/* Get no of stack pages needed */
1556 	stack_pages =
1557 		(num_sqbs + hw->stack_pg_ptrs - 1) / hw->stack_pg_ptrs;
1558 
1559 	for (qidx = 0; qidx < hw->non_qos_queues; qidx++) {
1560 		pool_id = otx2_get_pool_idx(pfvf, AURA_NIX_SQ, qidx);
1561 		/* Initialize aura context */
1562 		err = otx2_aura_init(pfvf, pool_id, pool_id, num_sqbs);
1563 		if (err)
1564 			goto fail;
1565 
1566 		/* Initialize pool context */
1567 		err = otx2_pool_init(pfvf, pool_id, stack_pages,
1568 				     num_sqbs, hw->sqb_size, AURA_NIX_SQ);
1569 		if (err)
1570 			goto fail;
1571 	}
1572 
1573 	/* Flush accumulated messages */
1574 	err = otx2_sync_mbox_msg(&pfvf->mbox);
1575 	if (err)
1576 		goto fail;
1577 
1578 	/* Allocate pointers and free them to aura/pool */
1579 	for (qidx = 0; qidx < hw->non_qos_queues; qidx++) {
1580 		pool_id = otx2_get_pool_idx(pfvf, AURA_NIX_SQ, qidx);
1581 		pool = &pfvf->qset.pool[pool_id];
1582 
1583 		sq = &qset->sq[qidx];
1584 		sq->sqb_count = 0;
1585 		sq->sqb_ptrs = kzalloc_objs(*sq->sqb_ptrs, num_sqbs);
1586 		if (!sq->sqb_ptrs) {
1587 			err = -ENOMEM;
1588 			goto err_mem;
1589 		}
1590 
1591 		for (ptr = 0; ptr < num_sqbs; ptr++) {
1592 			err = otx2_alloc_rbuf(pfvf, pool, &bufptr, pool_id, ptr);
1593 			if (err) {
1594 				if (pool->xsk_pool) {
1595 					ptr--;
1596 					while (ptr >= 0) {
1597 						xsk_buff_free(pool->xdp[ptr]);
1598 						ptr--;
1599 					}
1600 				}
1601 				goto err_mem;
1602 			}
1603 
1604 			pfvf->hw_ops->aura_freeptr(pfvf, pool_id, bufptr);
1605 			sq->sqb_ptrs[sq->sqb_count++] = (u64)bufptr;
1606 		}
1607 	}
1608 
1609 err_mem:
1610 	return err ? -ENOMEM : 0;
1611 
1612 fail:
1613 	otx2_mbox_reset(&pfvf->mbox.mbox, 0);
1614 	otx2_aura_pool_free(pfvf);
1615 	return err;
1616 }
1617 
1618 int otx2_rq_aura_pool_init(struct otx2_nic *pfvf)
1619 {
1620 	struct otx2_hw *hw = &pfvf->hw;
1621 	int stack_pages, pool_id, rq;
1622 	struct otx2_pool *pool;
1623 	int err, ptr, num_ptrs;
1624 	dma_addr_t bufptr;
1625 
1626 	num_ptrs = pfvf->qset.rqe_cnt;
1627 
1628 	stack_pages =
1629 		(num_ptrs + hw->stack_pg_ptrs - 1) / hw->stack_pg_ptrs;
1630 
1631 	for (rq = 0; rq < hw->rx_queues; rq++) {
1632 		pool_id = otx2_get_pool_idx(pfvf, AURA_NIX_RQ, rq);
1633 		/* Initialize aura context */
1634 		err = otx2_aura_init(pfvf, pool_id, pool_id, num_ptrs);
1635 		if (err)
1636 			goto fail;
1637 	}
1638 	for (pool_id = 0; pool_id < hw->rqpool_cnt; pool_id++) {
1639 		err = otx2_pool_init(pfvf, pool_id, stack_pages,
1640 				     num_ptrs, pfvf->rbsize, AURA_NIX_RQ);
1641 		if (err)
1642 			goto fail;
1643 	}
1644 
1645 	/* Flush accumulated messages */
1646 	err = otx2_sync_mbox_msg(&pfvf->mbox);
1647 	if (err)
1648 		goto fail;
1649 
1650 	/* Allocate pointers and free them to aura/pool */
1651 	for (pool_id = 0; pool_id < hw->rqpool_cnt; pool_id++) {
1652 		pool = &pfvf->qset.pool[pool_id];
1653 
1654 		for (ptr = 0; ptr < num_ptrs; ptr++) {
1655 			err = otx2_alloc_rbuf(pfvf, pool, &bufptr, pool_id, ptr);
1656 			if (err) {
1657 				if (pool->xsk_pool) {
1658 					while (ptr)
1659 						xsk_buff_free(pool->xdp[--ptr]);
1660 				}
1661 				return -ENOMEM;
1662 			}
1663 
1664 			pfvf->hw_ops->aura_freeptr(pfvf, pool_id,
1665 						   pool->xsk_pool ? bufptr :
1666 						   bufptr + OTX2_HEAD_ROOM);
1667 		}
1668 	}
1669 	return 0;
1670 fail:
1671 	otx2_mbox_reset(&pfvf->mbox.mbox, 0);
1672 	otx2_aura_pool_free(pfvf);
1673 	return err;
1674 }
1675 
1676 int otx2_config_npa(struct otx2_nic *pfvf)
1677 {
1678 	struct otx2_qset *qset = &pfvf->qset;
1679 	struct npa_lf_alloc_req  *npalf;
1680 	struct otx2_hw *hw = &pfvf->hw;
1681 	int aura_cnt;
1682 
1683 	/* Pool - Stack of free buffer pointers
1684 	 * Aura - Alloc/frees pointers from/to pool for NIX DMA.
1685 	 */
1686 
1687 	if (!hw->pool_cnt)
1688 		return -EINVAL;
1689 
1690 	qset->pool = devm_kcalloc(pfvf->dev, hw->pool_cnt,
1691 				  sizeof(struct otx2_pool), GFP_KERNEL);
1692 	if (!qset->pool)
1693 		return -ENOMEM;
1694 
1695 	/* Get memory to put this msg */
1696 	npalf = otx2_mbox_alloc_msg_npa_lf_alloc(&pfvf->mbox);
1697 	if (!npalf)
1698 		return -ENOMEM;
1699 
1700 	/* Set aura and pool counts */
1701 	npalf->nr_pools = hw->pool_cnt;
1702 	aura_cnt = ilog2(roundup_pow_of_two(hw->pool_cnt));
1703 	npalf->aura_sz = (aura_cnt >= ilog2(128)) ? (aura_cnt - 6) : 1;
1704 
1705 	return otx2_sync_mbox_msg(&pfvf->mbox);
1706 }
1707 
1708 int otx2_detach_resources(struct mbox *mbox)
1709 {
1710 	struct rsrc_detach *detach;
1711 
1712 	mutex_lock(&mbox->lock);
1713 	detach = otx2_mbox_alloc_msg_detach_resources(mbox);
1714 	if (!detach) {
1715 		mutex_unlock(&mbox->lock);
1716 		return -ENOMEM;
1717 	}
1718 
1719 	/* detach all */
1720 	detach->partial = false;
1721 
1722 	/* Send detach request to AF */
1723 	otx2_sync_mbox_msg(mbox);
1724 	mutex_unlock(&mbox->lock);
1725 	return 0;
1726 }
1727 EXPORT_SYMBOL(otx2_detach_resources);
1728 
1729 int otx2_attach_npa_nix(struct otx2_nic *pfvf)
1730 {
1731 	struct rsrc_attach *attach;
1732 	struct msg_req *msix;
1733 	int err;
1734 
1735 	mutex_lock(&pfvf->mbox.lock);
1736 	/* Get memory to put this msg */
1737 	attach = otx2_mbox_alloc_msg_attach_resources(&pfvf->mbox);
1738 	if (!attach) {
1739 		mutex_unlock(&pfvf->mbox.lock);
1740 		return -ENOMEM;
1741 	}
1742 
1743 	attach->npalf = true;
1744 	attach->nixlf = true;
1745 
1746 	/* Send attach request to AF */
1747 	err = otx2_sync_mbox_msg(&pfvf->mbox);
1748 	if (err) {
1749 		mutex_unlock(&pfvf->mbox.lock);
1750 		return err;
1751 	}
1752 
1753 	pfvf->nix_blkaddr = BLKADDR_NIX0;
1754 
1755 	/* If the platform has two NIX blocks then LF may be
1756 	 * allocated from NIX1.
1757 	 */
1758 	if (otx2_read64(pfvf, RVU_PF_BLOCK_ADDRX_DISC(BLKADDR_NIX1)) & 0x1FFULL)
1759 		pfvf->nix_blkaddr = BLKADDR_NIX1;
1760 
1761 	/* Get NPA and NIX MSIX vector offsets */
1762 	msix = otx2_mbox_alloc_msg_msix_offset(&pfvf->mbox);
1763 	if (!msix) {
1764 		mutex_unlock(&pfvf->mbox.lock);
1765 		return -ENOMEM;
1766 	}
1767 
1768 	err = otx2_sync_mbox_msg(&pfvf->mbox);
1769 	if (err) {
1770 		mutex_unlock(&pfvf->mbox.lock);
1771 		return err;
1772 	}
1773 	mutex_unlock(&pfvf->mbox.lock);
1774 
1775 	if (pfvf->hw.npa_msixoff == MSIX_VECTOR_INVALID ||
1776 	    pfvf->hw.nix_msixoff == MSIX_VECTOR_INVALID) {
1777 		dev_err(pfvf->dev,
1778 			"RVUPF: Invalid MSIX vector offset for NPA/NIX\n");
1779 		return -EINVAL;
1780 	}
1781 
1782 	return 0;
1783 }
1784 EXPORT_SYMBOL(otx2_attach_npa_nix);
1785 
1786 void otx2_ctx_disable(struct mbox *mbox, int type, bool npa)
1787 {
1788 	struct hwctx_disable_req *req;
1789 
1790 	mutex_lock(&mbox->lock);
1791 	/* Request AQ to disable this context */
1792 	if (npa)
1793 		req = otx2_mbox_alloc_msg_npa_hwctx_disable(mbox);
1794 	else
1795 		req = otx2_mbox_alloc_msg_nix_hwctx_disable(mbox);
1796 
1797 	if (!req) {
1798 		mutex_unlock(&mbox->lock);
1799 		return;
1800 	}
1801 
1802 	req->ctype = type;
1803 
1804 	if (otx2_sync_mbox_msg(mbox))
1805 		dev_err(mbox->pfvf->dev, "%s failed to disable context\n",
1806 			__func__);
1807 
1808 	mutex_unlock(&mbox->lock);
1809 }
1810 
1811 int otx2_nix_config_bp(struct otx2_nic *pfvf, bool enable)
1812 {
1813 	struct nix_bp_cfg_req *req;
1814 
1815 	if (enable)
1816 		req = otx2_mbox_alloc_msg_nix_bp_enable(&pfvf->mbox);
1817 	else
1818 		req = otx2_mbox_alloc_msg_nix_bp_disable(&pfvf->mbox);
1819 
1820 	if (!req)
1821 		return -ENOMEM;
1822 
1823 	req->chan_base = 0;
1824 	if (otx2_is_pfc_enabled(pfvf)) {
1825 		req->chan_cnt = IEEE_8021QAZ_MAX_TCS;
1826 		req->bpid_per_chan = 1;
1827 	} else {
1828 		req->chan_cnt = pfvf->hw.rx_chan_cnt;
1829 		req->bpid_per_chan = 0;
1830 	}
1831 
1832 	return otx2_sync_mbox_msg(&pfvf->mbox);
1833 }
1834 
1835 int otx2_nix_cpt_config_bp(struct otx2_nic *pfvf, bool enable)
1836 {
1837 	struct nix_bp_cfg_req *req;
1838 
1839 	if (enable)
1840 		req = otx2_mbox_alloc_msg_nix_cpt_bp_enable(&pfvf->mbox);
1841 	else
1842 		req = otx2_mbox_alloc_msg_nix_cpt_bp_disable(&pfvf->mbox);
1843 
1844 	if (!req)
1845 		return -ENOMEM;
1846 
1847 	req->chan_base = 0;
1848 	if (otx2_is_pfc_enabled(pfvf)) {
1849 		req->chan_cnt = IEEE_8021QAZ_MAX_TCS;
1850 		req->bpid_per_chan = 1;
1851 	} else {
1852 		req->chan_cnt = pfvf->hw.rx_chan_cnt;
1853 		req->bpid_per_chan = 0;
1854 	}
1855 
1856 	return otx2_sync_mbox_msg(&pfvf->mbox);
1857 }
1858 
1859 /* Mbox message handlers */
1860 void mbox_handler_cgx_stats(struct otx2_nic *pfvf,
1861 			    struct cgx_stats_rsp *rsp)
1862 {
1863 	int id;
1864 
1865 	for (id = 0; id < CGX_RX_STATS_COUNT; id++)
1866 		pfvf->hw.cgx_rx_stats[id] = rsp->rx_stats[id];
1867 	for (id = 0; id < CGX_TX_STATS_COUNT; id++)
1868 		pfvf->hw.cgx_tx_stats[id] = rsp->tx_stats[id];
1869 }
1870 
1871 void mbox_handler_cgx_fec_stats(struct otx2_nic *pfvf,
1872 				struct cgx_fec_stats_rsp *rsp)
1873 {
1874 	pfvf->hw.cgx_fec_corr_blks += rsp->fec_corr_blks;
1875 	pfvf->hw.cgx_fec_uncorr_blks += rsp->fec_uncorr_blks;
1876 }
1877 
1878 void mbox_handler_npa_lf_alloc(struct otx2_nic *pfvf,
1879 			       struct npa_lf_alloc_rsp *rsp)
1880 {
1881 	pfvf->hw.stack_pg_ptrs = rsp->stack_pg_ptrs;
1882 	pfvf->hw.stack_pg_bytes = rsp->stack_pg_bytes;
1883 }
1884 EXPORT_SYMBOL(mbox_handler_npa_lf_alloc);
1885 
1886 void mbox_handler_nix_lf_alloc(struct otx2_nic *pfvf,
1887 			       struct nix_lf_alloc_rsp *rsp)
1888 {
1889 	pfvf->hw.sqb_size = rsp->sqb_size;
1890 	pfvf->hw.rx_chan_base = rsp->rx_chan_base;
1891 	pfvf->hw.tx_chan_base = rsp->tx_chan_base;
1892 	pfvf->hw.rx_chan_cnt = rsp->rx_chan_cnt;
1893 	pfvf->hw.tx_chan_cnt = rsp->tx_chan_cnt;
1894 	pfvf->hw.lso_tsov4_idx = rsp->lso_tsov4_idx;
1895 	pfvf->hw.lso_tsov6_idx = rsp->lso_tsov6_idx;
1896 	pfvf->hw.cgx_links = rsp->cgx_links;
1897 	pfvf->hw.lbk_links = rsp->lbk_links;
1898 	pfvf->hw.tx_link = rsp->tx_link;
1899 }
1900 EXPORT_SYMBOL(mbox_handler_nix_lf_alloc);
1901 
1902 void mbox_handler_msix_offset(struct otx2_nic *pfvf,
1903 			      struct msix_offset_rsp *rsp)
1904 {
1905 	pfvf->hw.npa_msixoff = rsp->npa_msixoff;
1906 	pfvf->hw.nix_msixoff = rsp->nix_msixoff;
1907 }
1908 EXPORT_SYMBOL(mbox_handler_msix_offset);
1909 
1910 void mbox_handler_nix_bp_enable(struct otx2_nic *pfvf,
1911 				struct nix_bp_cfg_rsp *rsp)
1912 {
1913 	int chan, chan_id;
1914 
1915 	for (chan = 0; chan < rsp->chan_cnt; chan++) {
1916 		chan_id = ((rsp->chan_bpid[chan] >> 10) & 0x7F);
1917 		pfvf->bpid[chan_id] = rsp->chan_bpid[chan] & 0x3FF;
1918 	}
1919 }
1920 EXPORT_SYMBOL(mbox_handler_nix_bp_enable);
1921 
1922 void otx2_free_cints(struct otx2_nic *pfvf, int n)
1923 {
1924 	struct otx2_qset *qset = &pfvf->qset;
1925 	struct otx2_hw *hw = &pfvf->hw;
1926 	int irq, qidx;
1927 
1928 	for (qidx = 0, irq = hw->nix_msixoff + NIX_LF_CINT_VEC_START;
1929 	     qidx < n;
1930 	     qidx++, irq++) {
1931 		int vector = pci_irq_vector(pfvf->pdev, irq);
1932 
1933 		irq_set_affinity_hint(vector, NULL);
1934 		free_cpumask_var(hw->affinity_mask[irq]);
1935 		free_irq(vector, &qset->napi[qidx]);
1936 	}
1937 }
1938 EXPORT_SYMBOL(otx2_free_cints);
1939 
1940 void otx2_set_cints_affinity(struct otx2_nic *pfvf)
1941 {
1942 	struct otx2_hw *hw = &pfvf->hw;
1943 	int vec, cpu, irq, cint;
1944 
1945 	vec = hw->nix_msixoff + NIX_LF_CINT_VEC_START;
1946 	cpu = cpumask_first(cpu_online_mask);
1947 
1948 	/* CQ interrupts */
1949 	for (cint = 0; cint < pfvf->hw.cint_cnt; cint++, vec++) {
1950 		if (!alloc_cpumask_var(&hw->affinity_mask[vec], GFP_KERNEL))
1951 			return;
1952 
1953 		cpumask_set_cpu(cpu, hw->affinity_mask[vec]);
1954 
1955 		irq = pci_irq_vector(pfvf->pdev, vec);
1956 		irq_set_affinity_hint(irq, hw->affinity_mask[vec]);
1957 
1958 		cpu = cpumask_next(cpu, cpu_online_mask);
1959 		if (unlikely(cpu >= nr_cpu_ids))
1960 			cpu = 0;
1961 	}
1962 }
1963 
1964 static u32 get_dwrr_mtu(struct otx2_nic *pfvf, struct nix_hw_info *hw)
1965 {
1966 	if (is_otx2_lbkvf(pfvf->pdev)) {
1967 		pfvf->hw.smq_link_type = SMQ_LINK_TYPE_LBK;
1968 		return hw->lbk_dwrr_mtu;
1969 	}
1970 
1971 	pfvf->hw.smq_link_type = SMQ_LINK_TYPE_RPM;
1972 	return hw->rpm_dwrr_mtu;
1973 }
1974 
1975 u16 otx2_get_max_mtu(struct otx2_nic *pfvf)
1976 {
1977 	struct nix_hw_info *rsp;
1978 	struct msg_req *req;
1979 	u16 max_mtu;
1980 	int rc;
1981 
1982 	mutex_lock(&pfvf->mbox.lock);
1983 
1984 	req = otx2_mbox_alloc_msg_nix_get_hw_info(&pfvf->mbox);
1985 	if (!req) {
1986 		rc =  -ENOMEM;
1987 		goto out;
1988 	}
1989 
1990 	rc = otx2_sync_mbox_msg(&pfvf->mbox);
1991 	if (!rc) {
1992 		rsp = (struct nix_hw_info *)
1993 		       otx2_mbox_get_rsp(&pfvf->mbox.mbox, 0, &req->hdr);
1994 		if (IS_ERR(rsp)) {
1995 			rc = PTR_ERR(rsp);
1996 			goto out;
1997 		}
1998 
1999 		/* HW counts VLAN insertion bytes (8 for double tag)
2000 		 * irrespective of whether SQE is requesting to insert VLAN
2001 		 * in the packet or not. Hence these 8 bytes have to be
2002 		 * discounted from max packet size otherwise HW will throw
2003 		 * SMQ errors
2004 		 */
2005 		max_mtu = rsp->max_mtu - 8 - OTX2_ETH_HLEN;
2006 
2007 		/* Also save DWRR MTU, needed for DWRR weight calculation */
2008 		pfvf->hw.dwrr_mtu = get_dwrr_mtu(pfvf, rsp);
2009 		if (!pfvf->hw.dwrr_mtu)
2010 			pfvf->hw.dwrr_mtu = 1;
2011 	}
2012 
2013 out:
2014 	mutex_unlock(&pfvf->mbox.lock);
2015 	if (rc) {
2016 		dev_warn(pfvf->dev,
2017 			 "Failed to get MTU from hardware setting default value(1500)\n");
2018 		max_mtu = 1500;
2019 	}
2020 	return max_mtu;
2021 }
2022 EXPORT_SYMBOL(otx2_get_max_mtu);
2023 
2024 int otx2_handle_ntuple_tc_features(struct net_device *netdev, netdev_features_t features)
2025 {
2026 	netdev_features_t changed = features ^ netdev->features;
2027 	struct otx2_nic *pfvf = netdev_priv(netdev);
2028 	bool ntuple = !!(features & NETIF_F_NTUPLE);
2029 	bool tc = !!(features & NETIF_F_HW_TC);
2030 
2031 	if ((changed & NETIF_F_NTUPLE) && !ntuple)
2032 		otx2_destroy_ntuple_flows(pfvf);
2033 
2034 	if ((changed & NETIF_F_NTUPLE) && ntuple) {
2035 		if (!pfvf->flow_cfg->max_flows) {
2036 			netdev_err(netdev,
2037 				   "Can't enable NTUPLE, MCAM entries not allocated\n");
2038 			return -EINVAL;
2039 		}
2040 	}
2041 
2042 	if ((changed & NETIF_F_HW_TC) && !tc &&
2043 	    otx2_tc_flower_rule_cnt(pfvf)) {
2044 		netdev_err(netdev, "Can't disable TC hardware offload while flows are active\n");
2045 		return -EBUSY;
2046 	}
2047 
2048 	if ((changed & NETIF_F_NTUPLE) && ntuple &&
2049 	    otx2_tc_flower_rule_cnt(pfvf) && !(changed & NETIF_F_HW_TC)) {
2050 		netdev_err(netdev,
2051 			   "Can't enable NTUPLE when TC flower offload is active, disable TC rules and retry\n");
2052 		return -EINVAL;
2053 	}
2054 
2055 	return 0;
2056 }
2057 EXPORT_SYMBOL(otx2_handle_ntuple_tc_features);
2058 
2059 int otx2_set_hw_capabilities(struct otx2_nic *pfvf)
2060 {
2061 	struct mbox *mbox = &pfvf->mbox;
2062 	struct otx2_hw *hw = &pfvf->hw;
2063 	struct get_hw_cap_rsp *rsp;
2064 	struct msg_req *req;
2065 	int ret = -ENOMEM;
2066 
2067 	mutex_lock(&mbox->lock);
2068 
2069 	req = otx2_mbox_alloc_msg_get_hw_cap(mbox);
2070 	if (!req)
2071 		goto fail;
2072 
2073 	ret = otx2_sync_mbox_msg(mbox);
2074 	if (ret)
2075 		goto fail;
2076 
2077 	rsp = (struct get_hw_cap_rsp *)otx2_mbox_get_rsp(&pfvf->mbox.mbox,
2078 							 0, &req->hdr);
2079 	if (IS_ERR(rsp)) {
2080 		ret = -EINVAL;
2081 		goto fail;
2082 	}
2083 
2084 	if (rsp->hw_caps & HW_CAP_MACSEC)
2085 		__set_bit(CN10K_HW_MACSEC, &hw->cap_flag);
2086 
2087 	mutex_unlock(&mbox->lock);
2088 
2089 	return 0;
2090 fail:
2091 	dev_err(pfvf->dev, "Cannot get MACSEC capability from AF\n");
2092 	mutex_unlock(&mbox->lock);
2093 	return ret;
2094 }
2095 
2096 #define M(_name, _id, _fn_name, _req_type, _rsp_type)			\
2097 int __weak								\
2098 otx2_mbox_up_handler_ ## _fn_name(struct otx2_nic *pfvf,		\
2099 				struct _req_type *req,			\
2100 				struct _rsp_type *rsp)			\
2101 {									\
2102 	/* Nothing to do here */					\
2103 	return 0;							\
2104 }
2105 MBOX_UP_CGX_MESSAGES
2106 MBOX_UP_MCS_MESSAGES
2107 #undef M
2108 
2109 dma_addr_t otx2_dma_map_skb_frag(struct otx2_nic *pfvf,
2110 				 struct sk_buff *skb, int seg, int *len)
2111 {
2112 	enum dma_data_direction dir = DMA_TO_DEVICE;
2113 	const skb_frag_t *frag;
2114 	struct page *page;
2115 	int offset;
2116 
2117 	/* Crypto hardware need write permission for ipsec crypto offload */
2118 	if (unlikely(xfrm_offload(skb))) {
2119 		dir = DMA_BIDIRECTIONAL;
2120 		skb = skb_unclone_full(skb, GFP_ATOMIC);
2121 	}
2122 
2123 	/* First segment is always skb->data */
2124 	if (!seg) {
2125 		page = virt_to_page(skb->data);
2126 		offset = offset_in_page(skb->data);
2127 		*len = skb_headlen(skb);
2128 	} else {
2129 		frag = &skb_shinfo(skb)->frags[seg - 1];
2130 		page = skb_frag_page(frag);
2131 		offset = skb_frag_off(frag);
2132 		*len = skb_frag_size(frag);
2133 	}
2134 	return otx2_dma_map_page(pfvf, page, offset, *len, dir);
2135 }
2136 
2137 void otx2_dma_unmap_skb_frags(struct otx2_nic *pfvf, struct sg_list *sg)
2138 {
2139 	enum dma_data_direction dir = DMA_TO_DEVICE;
2140 	struct sk_buff *skb = NULL;
2141 	int seg;
2142 
2143 	skb = (struct sk_buff *)sg->skb;
2144 	if (unlikely(xfrm_offload(skb)))
2145 		dir = DMA_BIDIRECTIONAL;
2146 
2147 	for (seg = 0; seg < sg->num_segs; seg++) {
2148 		otx2_dma_unmap_page(pfvf, sg->dma_addr[seg],
2149 				    sg->size[seg], dir);
2150 	}
2151 	sg->num_segs = 0;
2152 }
2153