xref: /linux/drivers/net/ethernet/marvell/octeontx2/nic/otx2_common.c (revision 8be4d31cb8aaeea27bde4b7ddb26e28a89062ebf)
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 	dev_stats->tx_bcast_frames = OTX2_GET_TX_STATS(TX_BCAST);
129 	dev_stats->tx_mcast_frames = OTX2_GET_TX_STATS(TX_MCAST);
130 	dev_stats->tx_ucast_frames = OTX2_GET_TX_STATS(TX_UCAST);
131 	dev_stats->tx_frames = dev_stats->tx_bcast_frames +
132 			       dev_stats->tx_mcast_frames +
133 			       dev_stats->tx_ucast_frames;
134 }
135 
otx2_get_stats64(struct net_device * netdev,struct rtnl_link_stats64 * stats)136 void otx2_get_stats64(struct net_device *netdev,
137 		      struct rtnl_link_stats64 *stats)
138 {
139 	struct otx2_nic *pfvf = netdev_priv(netdev);
140 	struct otx2_dev_stats *dev_stats;
141 
142 	otx2_get_dev_stats(pfvf);
143 
144 	dev_stats = &pfvf->hw.dev_stats;
145 	stats->rx_bytes = dev_stats->rx_bytes;
146 	stats->rx_packets = dev_stats->rx_frames;
147 	stats->rx_dropped = dev_stats->rx_drops;
148 	stats->multicast = dev_stats->rx_mcast_frames;
149 
150 	stats->tx_bytes = dev_stats->tx_bytes;
151 	stats->tx_packets = dev_stats->tx_frames;
152 	stats->tx_dropped = dev_stats->tx_drops;
153 }
154 EXPORT_SYMBOL(otx2_get_stats64);
155 
156 /* Sync MAC address with RVU AF */
otx2_hw_set_mac_addr(struct otx2_nic * pfvf,u8 * mac)157 static int otx2_hw_set_mac_addr(struct otx2_nic *pfvf, u8 *mac)
158 {
159 	struct nix_set_mac_addr *req;
160 	int err;
161 
162 	mutex_lock(&pfvf->mbox.lock);
163 	req = otx2_mbox_alloc_msg_nix_set_mac_addr(&pfvf->mbox);
164 	if (!req) {
165 		mutex_unlock(&pfvf->mbox.lock);
166 		return -ENOMEM;
167 	}
168 
169 	ether_addr_copy(req->mac_addr, mac);
170 
171 	err = otx2_sync_mbox_msg(&pfvf->mbox);
172 	mutex_unlock(&pfvf->mbox.lock);
173 	return err;
174 }
175 
otx2_hw_get_mac_addr(struct otx2_nic * pfvf,struct net_device * netdev)176 static int otx2_hw_get_mac_addr(struct otx2_nic *pfvf,
177 				struct net_device *netdev)
178 {
179 	struct nix_get_mac_addr_rsp *rsp;
180 	struct mbox_msghdr *msghdr;
181 	struct msg_req *req;
182 	int err;
183 
184 	mutex_lock(&pfvf->mbox.lock);
185 	req = otx2_mbox_alloc_msg_nix_get_mac_addr(&pfvf->mbox);
186 	if (!req) {
187 		mutex_unlock(&pfvf->mbox.lock);
188 		return -ENOMEM;
189 	}
190 
191 	err = otx2_sync_mbox_msg(&pfvf->mbox);
192 	if (err) {
193 		mutex_unlock(&pfvf->mbox.lock);
194 		return err;
195 	}
196 
197 	msghdr = otx2_mbox_get_rsp(&pfvf->mbox.mbox, 0, &req->hdr);
198 	if (IS_ERR(msghdr)) {
199 		mutex_unlock(&pfvf->mbox.lock);
200 		return PTR_ERR(msghdr);
201 	}
202 	rsp = (struct nix_get_mac_addr_rsp *)msghdr;
203 	eth_hw_addr_set(netdev, rsp->mac_addr);
204 	mutex_unlock(&pfvf->mbox.lock);
205 
206 	return 0;
207 }
208 
otx2_set_mac_address(struct net_device * netdev,void * p)209 int otx2_set_mac_address(struct net_device *netdev, void *p)
210 {
211 	struct otx2_nic *pfvf = netdev_priv(netdev);
212 	struct sockaddr *addr = p;
213 
214 	if (!is_valid_ether_addr(addr->sa_data))
215 		return -EADDRNOTAVAIL;
216 
217 	if (!otx2_hw_set_mac_addr(pfvf, addr->sa_data)) {
218 		eth_hw_addr_set(netdev, addr->sa_data);
219 		/* update dmac field in vlan offload rule */
220 		if (netif_running(netdev) &&
221 		    pfvf->flags & OTX2_FLAG_RX_VLAN_SUPPORT)
222 			otx2_install_rxvlan_offload_flow(pfvf);
223 		/* update dmac address in ntuple and DMAC filter list */
224 		if (pfvf->flags & OTX2_FLAG_DMACFLTR_SUPPORT)
225 			otx2_dmacflt_update_pfmac_flow(pfvf);
226 	} else {
227 		return -EPERM;
228 	}
229 
230 	return 0;
231 }
232 EXPORT_SYMBOL(otx2_set_mac_address);
233 
otx2_hw_set_mtu(struct otx2_nic * pfvf,int mtu)234 int otx2_hw_set_mtu(struct otx2_nic *pfvf, int mtu)
235 {
236 	struct nix_frs_cfg *req;
237 	u16 maxlen;
238 	int err;
239 
240 	maxlen = pfvf->hw.max_mtu + OTX2_ETH_HLEN + OTX2_HW_TIMESTAMP_LEN;
241 
242 	mutex_lock(&pfvf->mbox.lock);
243 	req = otx2_mbox_alloc_msg_nix_set_hw_frs(&pfvf->mbox);
244 	if (!req) {
245 		mutex_unlock(&pfvf->mbox.lock);
246 		return -ENOMEM;
247 	}
248 
249 	req->maxlen = mtu + OTX2_ETH_HLEN + OTX2_HW_TIMESTAMP_LEN;
250 
251 	/* Use max receive length supported by hardware for loopback devices */
252 	if (is_otx2_lbkvf(pfvf->pdev))
253 		req->maxlen = maxlen;
254 
255 	err = otx2_sync_mbox_msg(&pfvf->mbox);
256 	mutex_unlock(&pfvf->mbox.lock);
257 	return err;
258 }
259 EXPORT_SYMBOL(otx2_hw_set_mtu);
260 
otx2_config_pause_frm(struct otx2_nic * pfvf)261 int otx2_config_pause_frm(struct otx2_nic *pfvf)
262 {
263 	struct cgx_pause_frm_cfg *req;
264 	int err;
265 
266 	if (is_otx2_lbkvf(pfvf->pdev) || is_otx2_sdp_rep(pfvf->pdev))
267 		return 0;
268 
269 	mutex_lock(&pfvf->mbox.lock);
270 	req = otx2_mbox_alloc_msg_cgx_cfg_pause_frm(&pfvf->mbox);
271 	if (!req) {
272 		err = -ENOMEM;
273 		goto unlock;
274 	}
275 
276 	req->rx_pause = !!(pfvf->flags & OTX2_FLAG_RX_PAUSE_ENABLED);
277 	req->tx_pause = !!(pfvf->flags & OTX2_FLAG_TX_PAUSE_ENABLED);
278 	req->set = 1;
279 
280 	err = otx2_sync_mbox_msg(&pfvf->mbox);
281 unlock:
282 	mutex_unlock(&pfvf->mbox.lock);
283 	return err;
284 }
285 EXPORT_SYMBOL(otx2_config_pause_frm);
286 
otx2_set_flowkey_cfg(struct otx2_nic * pfvf)287 int otx2_set_flowkey_cfg(struct otx2_nic *pfvf)
288 {
289 	struct otx2_rss_info *rss = &pfvf->hw.rss_info;
290 	struct nix_rss_flowkey_cfg_rsp *rsp;
291 	struct nix_rss_flowkey_cfg *req;
292 	int err;
293 
294 	mutex_lock(&pfvf->mbox.lock);
295 	req = otx2_mbox_alloc_msg_nix_rss_flowkey_cfg(&pfvf->mbox);
296 	if (!req) {
297 		mutex_unlock(&pfvf->mbox.lock);
298 		return -ENOMEM;
299 	}
300 	req->mcam_index = -1; /* Default or reserved index */
301 	req->flowkey_cfg = rss->flowkey_cfg;
302 	req->group = DEFAULT_RSS_CONTEXT_GROUP;
303 
304 	err = otx2_sync_mbox_msg(&pfvf->mbox);
305 	if (err)
306 		goto fail;
307 
308 	rsp = (struct nix_rss_flowkey_cfg_rsp *)
309 			otx2_mbox_get_rsp(&pfvf->mbox.mbox, 0, &req->hdr);
310 	if (IS_ERR(rsp)) {
311 		err = PTR_ERR(rsp);
312 		goto fail;
313 	}
314 
315 	pfvf->hw.flowkey_alg_idx = rsp->alg_idx;
316 fail:
317 	mutex_unlock(&pfvf->mbox.lock);
318 	return err;
319 }
320 
otx2_set_rss_table(struct otx2_nic * pfvf,int ctx_id,const u32 * ind_tbl)321 int otx2_set_rss_table(struct otx2_nic *pfvf, int ctx_id, const u32 *ind_tbl)
322 {
323 	struct otx2_rss_info *rss = &pfvf->hw.rss_info;
324 	const int index = rss->rss_size * ctx_id;
325 	struct mbox *mbox = &pfvf->mbox;
326 	struct nix_aq_enq_req *aq;
327 	int idx, err;
328 
329 	mutex_lock(&mbox->lock);
330 	ind_tbl = ind_tbl ?: rss->ind_tbl;
331 	/* Get memory to put this msg */
332 	for (idx = 0; idx < rss->rss_size; idx++) {
333 		/* Ignore the queue if AF_XDP zero copy is enabled */
334 		if (test_bit(ind_tbl[idx], pfvf->af_xdp_zc_qidx))
335 			continue;
336 
337 		aq = otx2_mbox_alloc_msg_nix_aq_enq(mbox);
338 		if (!aq) {
339 			/* The shared memory buffer can be full.
340 			 * Flush it and retry
341 			 */
342 			err = otx2_sync_mbox_msg(mbox);
343 			if (err) {
344 				mutex_unlock(&mbox->lock);
345 				return err;
346 			}
347 			aq = otx2_mbox_alloc_msg_nix_aq_enq(mbox);
348 			if (!aq) {
349 				mutex_unlock(&mbox->lock);
350 				return -ENOMEM;
351 			}
352 		}
353 
354 		aq->rss.rq = ind_tbl[idx];
355 
356 		/* Fill AQ info */
357 		aq->qidx = index + idx;
358 		aq->ctype = NIX_AQ_CTYPE_RSS;
359 		aq->op = NIX_AQ_INSTOP_INIT;
360 	}
361 	err = otx2_sync_mbox_msg(mbox);
362 	mutex_unlock(&mbox->lock);
363 	return err;
364 }
365 
otx2_set_rss_key(struct otx2_nic * pfvf)366 void otx2_set_rss_key(struct otx2_nic *pfvf)
367 {
368 	struct otx2_rss_info *rss = &pfvf->hw.rss_info;
369 	u64 *key = (u64 *)&rss->key[4];
370 	int idx;
371 
372 	/* 352bit or 44byte key needs to be configured as below
373 	 * NIX_LF_RX_SECRETX0 = key<351:288>
374 	 * NIX_LF_RX_SECRETX1 = key<287:224>
375 	 * NIX_LF_RX_SECRETX2 = key<223:160>
376 	 * NIX_LF_RX_SECRETX3 = key<159:96>
377 	 * NIX_LF_RX_SECRETX4 = key<95:32>
378 	 * NIX_LF_RX_SECRETX5<63:32> = key<31:0>
379 	 */
380 	otx2_write64(pfvf, NIX_LF_RX_SECRETX(5),
381 		     (u64)(*((u32 *)&rss->key)) << 32);
382 	idx = sizeof(rss->key) / sizeof(u64);
383 	while (idx > 0) {
384 		idx--;
385 		otx2_write64(pfvf, NIX_LF_RX_SECRETX(idx), *key++);
386 	}
387 }
388 
otx2_rss_init(struct otx2_nic * pfvf)389 int otx2_rss_init(struct otx2_nic *pfvf)
390 {
391 	struct otx2_rss_info *rss = &pfvf->hw.rss_info;
392 	int idx, ret = 0;
393 
394 	rss->rss_size = sizeof(*rss->ind_tbl);
395 
396 	/* Init RSS key if it is not setup already */
397 	if (!rss->enable)
398 		netdev_rss_key_fill(rss->key, sizeof(rss->key));
399 	otx2_set_rss_key(pfvf);
400 
401 	if (!netif_is_rxfh_configured(pfvf->netdev))
402 		for (idx = 0; idx < rss->rss_size; idx++)
403 			rss->ind_tbl[idx] =
404 				ethtool_rxfh_indir_default(idx,
405 							   pfvf->hw.rx_queues);
406 
407 	ret = otx2_set_rss_table(pfvf, DEFAULT_RSS_CONTEXT_GROUP, NULL);
408 	if (ret)
409 		return ret;
410 
411 	/* Flowkey or hash config to be used for generating flow tag */
412 	rss->flowkey_cfg = rss->enable ? rss->flowkey_cfg :
413 			   NIX_FLOW_KEY_TYPE_IPV4 | NIX_FLOW_KEY_TYPE_IPV6 |
414 			   NIX_FLOW_KEY_TYPE_TCP | NIX_FLOW_KEY_TYPE_UDP |
415 			   NIX_FLOW_KEY_TYPE_SCTP | NIX_FLOW_KEY_TYPE_VLAN |
416 			   NIX_FLOW_KEY_TYPE_IPV4_PROTO;
417 
418 	ret = otx2_set_flowkey_cfg(pfvf);
419 	if (ret)
420 		return ret;
421 
422 	rss->enable = true;
423 	return 0;
424 }
425 
426 /* Setup UDP segmentation algorithm in HW */
otx2_setup_udp_segmentation(struct nix_lso_format_cfg * lso,bool v4)427 static void otx2_setup_udp_segmentation(struct nix_lso_format_cfg *lso, bool v4)
428 {
429 	struct nix_lso_format *field;
430 
431 	field = (struct nix_lso_format *)&lso->fields[0];
432 	lso->field_mask = GENMASK(18, 0);
433 
434 	/* IP's Length field */
435 	field->layer = NIX_TXLAYER_OL3;
436 	/* In ipv4, length field is at offset 2 bytes, for ipv6 it's 4 */
437 	field->offset = v4 ? 2 : 4;
438 	field->sizem1 = 1; /* i.e 2 bytes */
439 	field->alg = NIX_LSOALG_ADD_PAYLEN;
440 	field++;
441 
442 	/* No ID field in IPv6 header */
443 	if (v4) {
444 		/* Increment IPID */
445 		field->layer = NIX_TXLAYER_OL3;
446 		field->offset = 4;
447 		field->sizem1 = 1; /* i.e 2 bytes */
448 		field->alg = NIX_LSOALG_ADD_SEGNUM;
449 		field++;
450 	}
451 
452 	/* Update length in UDP header */
453 	field->layer = NIX_TXLAYER_OL4;
454 	field->offset = 4;
455 	field->sizem1 = 1;
456 	field->alg = NIX_LSOALG_ADD_PAYLEN;
457 }
458 
459 /* Setup segmentation algorithms in HW and retrieve algorithm index */
otx2_setup_segmentation(struct otx2_nic * pfvf)460 void otx2_setup_segmentation(struct otx2_nic *pfvf)
461 {
462 	struct nix_lso_format_cfg_rsp *rsp;
463 	struct nix_lso_format_cfg *lso;
464 	struct otx2_hw *hw = &pfvf->hw;
465 	int err;
466 
467 	mutex_lock(&pfvf->mbox.lock);
468 
469 	/* UDPv4 segmentation */
470 	lso = otx2_mbox_alloc_msg_nix_lso_format_cfg(&pfvf->mbox);
471 	if (!lso)
472 		goto fail;
473 
474 	/* Setup UDP/IP header fields that HW should update per segment */
475 	otx2_setup_udp_segmentation(lso, true);
476 
477 	err = otx2_sync_mbox_msg(&pfvf->mbox);
478 	if (err)
479 		goto fail;
480 
481 	rsp = (struct nix_lso_format_cfg_rsp *)
482 			otx2_mbox_get_rsp(&pfvf->mbox.mbox, 0, &lso->hdr);
483 	if (IS_ERR(rsp))
484 		goto fail;
485 
486 	hw->lso_udpv4_idx = rsp->lso_format_idx;
487 
488 	/* UDPv6 segmentation */
489 	lso = otx2_mbox_alloc_msg_nix_lso_format_cfg(&pfvf->mbox);
490 	if (!lso)
491 		goto fail;
492 
493 	/* Setup UDP/IP header fields that HW should update per segment */
494 	otx2_setup_udp_segmentation(lso, false);
495 
496 	err = otx2_sync_mbox_msg(&pfvf->mbox);
497 	if (err)
498 		goto fail;
499 
500 	rsp = (struct nix_lso_format_cfg_rsp *)
501 			otx2_mbox_get_rsp(&pfvf->mbox.mbox, 0, &lso->hdr);
502 	if (IS_ERR(rsp))
503 		goto fail;
504 
505 	hw->lso_udpv6_idx = rsp->lso_format_idx;
506 	mutex_unlock(&pfvf->mbox.lock);
507 	return;
508 fail:
509 	mutex_unlock(&pfvf->mbox.lock);
510 	netdev_info(pfvf->netdev,
511 		    "Failed to get LSO index for UDP GSO offload, disabling\n");
512 	pfvf->netdev->hw_features &= ~NETIF_F_GSO_UDP_L4;
513 }
514 
otx2_config_irq_coalescing(struct otx2_nic * pfvf,int qidx)515 void otx2_config_irq_coalescing(struct otx2_nic *pfvf, int qidx)
516 {
517 	/* Configure CQE interrupt coalescing parameters
518 	 *
519 	 * HW triggers an irq when ECOUNT > cq_ecount_wait, hence
520 	 * set 1 less than cq_ecount_wait. And cq_time_wait is in
521 	 * usecs, convert that to 100ns count.
522 	 */
523 	otx2_write64(pfvf, NIX_LF_CINTX_WAIT(qidx),
524 		     ((u64)(pfvf->hw.cq_time_wait * 10) << 48) |
525 		     ((u64)pfvf->hw.cq_qcount_wait << 32) |
526 		     (pfvf->hw.cq_ecount_wait - 1));
527 }
528 
otx2_alloc_pool_buf(struct otx2_nic * pfvf,struct otx2_pool * pool,dma_addr_t * dma)529 static int otx2_alloc_pool_buf(struct otx2_nic *pfvf, struct otx2_pool *pool,
530 			       dma_addr_t *dma)
531 {
532 	unsigned int offset = 0;
533 	struct page *page;
534 	size_t sz;
535 
536 	sz = SKB_DATA_ALIGN(pool->rbsize);
537 	sz = ALIGN(sz, OTX2_ALIGN);
538 
539 	page = page_pool_alloc_frag(pool->page_pool, &offset, sz, GFP_ATOMIC);
540 	if (unlikely(!page))
541 		return -ENOMEM;
542 
543 	*dma = page_pool_get_dma_addr(page) + offset;
544 	return 0;
545 }
546 
__otx2_alloc_rbuf(struct otx2_nic * pfvf,struct otx2_pool * pool,dma_addr_t * dma,int qidx,int idx)547 static int __otx2_alloc_rbuf(struct otx2_nic *pfvf, struct otx2_pool *pool,
548 			     dma_addr_t *dma, int qidx, int idx)
549 {
550 	u8 *buf;
551 
552 	if (pool->xsk_pool)
553 		return otx2_xsk_pool_alloc_buf(pfvf, pool, dma, idx);
554 
555 	if (pool->page_pool)
556 		return otx2_alloc_pool_buf(pfvf, pool, dma);
557 
558 	buf = napi_alloc_frag_align(pool->rbsize, OTX2_ALIGN);
559 	if (unlikely(!buf))
560 		return -ENOMEM;
561 
562 	*dma = dma_map_single_attrs(pfvf->dev, buf, pool->rbsize,
563 				    DMA_FROM_DEVICE, DMA_ATTR_SKIP_CPU_SYNC);
564 	if (unlikely(dma_mapping_error(pfvf->dev, *dma))) {
565 		page_frag_free(buf);
566 		return -ENOMEM;
567 	}
568 
569 	return 0;
570 }
571 
otx2_alloc_rbuf(struct otx2_nic * pfvf,struct otx2_pool * pool,dma_addr_t * dma,int qidx,int idx)572 int otx2_alloc_rbuf(struct otx2_nic *pfvf, struct otx2_pool *pool,
573 		    dma_addr_t *dma, int qidx, int idx)
574 {
575 	int ret;
576 
577 	local_bh_disable();
578 	ret = __otx2_alloc_rbuf(pfvf, pool, dma, qidx, idx);
579 	local_bh_enable();
580 	return ret;
581 }
582 
otx2_alloc_buffer(struct otx2_nic * pfvf,struct otx2_cq_queue * cq,dma_addr_t * dma)583 int otx2_alloc_buffer(struct otx2_nic *pfvf, struct otx2_cq_queue *cq,
584 		      dma_addr_t *dma)
585 {
586 	if (unlikely(__otx2_alloc_rbuf(pfvf, cq->rbpool, dma,
587 				       cq->cq_idx, cq->pool_ptrs - 1)))
588 		return -ENOMEM;
589 	return 0;
590 }
591 
otx2_tx_timeout(struct net_device * netdev,unsigned int txq)592 void otx2_tx_timeout(struct net_device *netdev, unsigned int txq)
593 {
594 	struct otx2_nic *pfvf = netdev_priv(netdev);
595 
596 	schedule_work(&pfvf->reset_task);
597 }
598 EXPORT_SYMBOL(otx2_tx_timeout);
599 
otx2_get_mac_from_af(struct net_device * netdev)600 void otx2_get_mac_from_af(struct net_device *netdev)
601 {
602 	struct otx2_nic *pfvf = netdev_priv(netdev);
603 	int err;
604 
605 	err = otx2_hw_get_mac_addr(pfvf, netdev);
606 	if (err)
607 		dev_warn(pfvf->dev, "Failed to read mac from hardware\n");
608 
609 	/* If AF doesn't provide a valid MAC, generate a random one */
610 	if (!is_valid_ether_addr(netdev->dev_addr))
611 		eth_hw_addr_random(netdev);
612 }
613 EXPORT_SYMBOL(otx2_get_mac_from_af);
614 
otx2_txschq_config(struct otx2_nic * pfvf,int lvl,int prio,bool txschq_for_pfc)615 int otx2_txschq_config(struct otx2_nic *pfvf, int lvl, int prio, bool txschq_for_pfc)
616 {
617 	u16 (*schq_list)[MAX_TXSCHQ_PER_FUNC];
618 	struct otx2_hw *hw = &pfvf->hw;
619 	struct nix_txschq_config *req;
620 	u64 schq, parent;
621 	u64 dwrr_val;
622 
623 	dwrr_val = mtu_to_dwrr_weight(pfvf, pfvf->tx_max_pktlen);
624 
625 	req = otx2_mbox_alloc_msg_nix_txschq_cfg(&pfvf->mbox);
626 	if (!req)
627 		return -ENOMEM;
628 
629 	req->lvl = lvl;
630 	req->num_regs = 1;
631 
632 	schq_list = hw->txschq_list;
633 #ifdef CONFIG_DCB
634 	if (txschq_for_pfc)
635 		schq_list = pfvf->pfc_schq_list;
636 #endif
637 
638 	schq = schq_list[lvl][prio];
639 	/* Set topology e.t.c configuration */
640 	if (lvl == NIX_TXSCH_LVL_SMQ) {
641 		req->reg[0] = NIX_AF_SMQX_CFG(schq);
642 		req->regval[0] = ((u64)pfvf->tx_max_pktlen << 8) | OTX2_MIN_MTU;
643 		req->regval[0] |= (0x20ULL << 51) | (0x80ULL << 39) |
644 				  (0x2ULL << 36);
645 		/* Set link type for DWRR MTU selection on CN10K silicons */
646 		if (!is_dev_otx2(pfvf->pdev))
647 			req->regval[0] |= FIELD_PREP(GENMASK_ULL(58, 57),
648 						(u64)hw->smq_link_type);
649 		req->num_regs++;
650 		/* MDQ config */
651 		parent = schq_list[NIX_TXSCH_LVL_TL4][prio];
652 		req->reg[1] = NIX_AF_MDQX_PARENT(schq);
653 		req->regval[1] = parent << 16;
654 		req->num_regs++;
655 		/* Set DWRR quantum */
656 		req->reg[2] = NIX_AF_MDQX_SCHEDULE(schq);
657 		req->regval[2] =  dwrr_val;
658 	} else if (lvl == NIX_TXSCH_LVL_TL4) {
659 		int sdp_chan =  hw->tx_chan_base + prio;
660 
661 		if (is_otx2_sdp_rep(pfvf->pdev))
662 			prio = 0;
663 		parent = schq_list[NIX_TXSCH_LVL_TL3][prio];
664 		req->reg[0] = NIX_AF_TL4X_PARENT(schq);
665 		req->regval[0] = (u64)parent << 16;
666 		req->num_regs++;
667 		req->reg[1] = NIX_AF_TL4X_SCHEDULE(schq);
668 		req->regval[1] = dwrr_val;
669 		if (is_otx2_sdp_rep(pfvf->pdev)) {
670 			req->num_regs++;
671 			req->reg[2] = NIX_AF_TL4X_SDP_LINK_CFG(schq);
672 			req->regval[2] = BIT_ULL(12) | BIT_ULL(13) |
673 					 (sdp_chan & 0xff);
674 		}
675 	} else if (lvl == NIX_TXSCH_LVL_TL3) {
676 		parent = schq_list[NIX_TXSCH_LVL_TL2][prio];
677 		req->reg[0] = NIX_AF_TL3X_PARENT(schq);
678 		req->regval[0] = (u64)parent << 16;
679 		req->num_regs++;
680 		req->reg[1] = NIX_AF_TL3X_SCHEDULE(schq);
681 		req->regval[1] = dwrr_val;
682 		if (lvl == hw->txschq_link_cfg_lvl &&
683 		    !is_otx2_sdp_rep(pfvf->pdev)) {
684 			req->num_regs++;
685 			req->reg[2] = NIX_AF_TL3_TL2X_LINKX_CFG(schq, hw->tx_link);
686 			/* Enable this queue and backpressure
687 			 * and set relative channel
688 			 */
689 			req->regval[2] = BIT_ULL(13) | BIT_ULL(12) | prio;
690 		}
691 	} else if (lvl == NIX_TXSCH_LVL_TL2) {
692 		parent = schq_list[NIX_TXSCH_LVL_TL1][prio];
693 		req->reg[0] = NIX_AF_TL2X_PARENT(schq);
694 		req->regval[0] = (u64)parent << 16;
695 
696 		req->num_regs++;
697 		req->reg[1] = NIX_AF_TL2X_SCHEDULE(schq);
698 		req->regval[1] = (u64)hw->txschq_aggr_lvl_rr_prio << 24 | dwrr_val;
699 
700 		if (lvl == hw->txschq_link_cfg_lvl &&
701 		    !is_otx2_sdp_rep(pfvf->pdev)) {
702 			req->num_regs++;
703 			req->reg[2] = NIX_AF_TL3_TL2X_LINKX_CFG(schq, hw->tx_link);
704 			/* Enable this queue and backpressure
705 			 * and set relative channel
706 			 */
707 			req->regval[2] = BIT_ULL(13) | BIT_ULL(12) | prio;
708 		}
709 	} else if (lvl == NIX_TXSCH_LVL_TL1) {
710 		/* Default config for TL1.
711 		 * For VF this is always ignored.
712 		 */
713 
714 		/* On CN10K, if RR_WEIGHT is greater than 16384, HW will
715 		 * clip it to 16384, so configuring a 24bit max value
716 		 * will work on both OTx2 and CN10K.
717 		 */
718 		req->reg[0] = NIX_AF_TL1X_SCHEDULE(schq);
719 		req->regval[0] = TXSCH_TL1_DFLT_RR_QTM;
720 
721 		req->num_regs++;
722 		req->reg[1] = NIX_AF_TL1X_TOPOLOGY(schq);
723 		req->regval[1] = hw->txschq_aggr_lvl_rr_prio << 1;
724 
725 		req->num_regs++;
726 		req->reg[2] = NIX_AF_TL1X_CIR(schq);
727 		req->regval[2] = 0;
728 	}
729 
730 	return otx2_sync_mbox_msg(&pfvf->mbox);
731 }
732 EXPORT_SYMBOL(otx2_txschq_config);
733 
otx2_smq_flush(struct otx2_nic * pfvf,int smq)734 int otx2_smq_flush(struct otx2_nic *pfvf, int smq)
735 {
736 	struct nix_txschq_config *req;
737 	int rc;
738 
739 	mutex_lock(&pfvf->mbox.lock);
740 
741 	req = otx2_mbox_alloc_msg_nix_txschq_cfg(&pfvf->mbox);
742 	if (!req) {
743 		mutex_unlock(&pfvf->mbox.lock);
744 		return -ENOMEM;
745 	}
746 
747 	req->lvl = NIX_TXSCH_LVL_SMQ;
748 	req->reg[0] = NIX_AF_SMQX_CFG(smq);
749 	req->regval[0] |= BIT_ULL(49);
750 	req->num_regs++;
751 
752 	rc = otx2_sync_mbox_msg(&pfvf->mbox);
753 	mutex_unlock(&pfvf->mbox.lock);
754 	return rc;
755 }
756 EXPORT_SYMBOL(otx2_smq_flush);
757 
otx2_txsch_alloc(struct otx2_nic * pfvf)758 int otx2_txsch_alloc(struct otx2_nic *pfvf)
759 {
760 	int chan_cnt = pfvf->hw.tx_chan_cnt;
761 	struct nix_txsch_alloc_req *req;
762 	struct nix_txsch_alloc_rsp *rsp;
763 	int lvl, schq, rc;
764 
765 	/* Get memory to put this msg */
766 	req = otx2_mbox_alloc_msg_nix_txsch_alloc(&pfvf->mbox);
767 	if (!req)
768 		return -ENOMEM;
769 
770 	/* Request one schq per level */
771 	for (lvl = 0; lvl < NIX_TXSCH_LVL_CNT; lvl++)
772 		req->schq[lvl] = 1;
773 
774 	if (is_otx2_sdp_rep(pfvf->pdev) && chan_cnt > 1) {
775 		req->schq[NIX_TXSCH_LVL_SMQ] = chan_cnt;
776 		req->schq[NIX_TXSCH_LVL_TL4] = chan_cnt;
777 	}
778 
779 	rc = otx2_sync_mbox_msg(&pfvf->mbox);
780 	if (rc)
781 		return rc;
782 
783 	rsp = (struct nix_txsch_alloc_rsp *)
784 	      otx2_mbox_get_rsp(&pfvf->mbox.mbox, 0, &req->hdr);
785 	if (IS_ERR(rsp))
786 		return PTR_ERR(rsp);
787 
788 	/* Setup transmit scheduler list */
789 	for (lvl = 0; lvl < NIX_TXSCH_LVL_CNT; lvl++) {
790 		pfvf->hw.txschq_cnt[lvl] = rsp->schq[lvl];
791 		for (schq = 0; schq < rsp->schq[lvl]; schq++)
792 			pfvf->hw.txschq_list[lvl][schq] =
793 				rsp->schq_list[lvl][schq];
794 	}
795 
796 	pfvf->hw.txschq_link_cfg_lvl = rsp->link_cfg_lvl;
797 	pfvf->hw.txschq_aggr_lvl_rr_prio = rsp->aggr_lvl_rr_prio;
798 
799 	return 0;
800 }
801 
otx2_txschq_free_one(struct otx2_nic * pfvf,u16 lvl,u16 schq)802 void otx2_txschq_free_one(struct otx2_nic *pfvf, u16 lvl, u16 schq)
803 {
804 	struct nix_txsch_free_req *free_req;
805 	int err;
806 
807 	mutex_lock(&pfvf->mbox.lock);
808 
809 	free_req = otx2_mbox_alloc_msg_nix_txsch_free(&pfvf->mbox);
810 	if (!free_req) {
811 		mutex_unlock(&pfvf->mbox.lock);
812 		netdev_err(pfvf->netdev,
813 			   "Failed alloc txschq free req\n");
814 		return;
815 	}
816 
817 	free_req->schq_lvl = lvl;
818 	free_req->schq = schq;
819 
820 	err = otx2_sync_mbox_msg(&pfvf->mbox);
821 	if (err) {
822 		netdev_err(pfvf->netdev,
823 			   "Failed stop txschq %d at level %d\n", schq, lvl);
824 	}
825 
826 	mutex_unlock(&pfvf->mbox.lock);
827 }
828 EXPORT_SYMBOL(otx2_txschq_free_one);
829 
otx2_txschq_stop(struct otx2_nic * pfvf)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 
otx2_sqb_flush(struct otx2_nic * pfvf)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 
otx2_rq_init(struct otx2_nic * pfvf,u16 qidx,u16 lpb_aura)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 
otx2_sq_aq_init(void * dev,u16 qidx,u8 chan_offset,u16 sqb_aura)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 
otx2_sq_init(struct otx2_nic * pfvf,u16 qidx,u16 sqb_aura)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 = kcalloc(qset->sqe_cnt, sizeof(struct sg_list), GFP_KERNEL);
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 
1037 	chan_offset = qidx % pfvf->hw.tx_chan_cnt;
1038 	err = pfvf->hw_ops->sq_aq_init(pfvf, qidx, chan_offset, sqb_aura);
1039 	if (err) {
1040 		kfree(sq->sg);
1041 		sq->sg = NULL;
1042 		return err;
1043 	}
1044 
1045 	return 0;
1046 
1047 }
1048 
otx2_cq_init(struct otx2_nic * pfvf,u16 qidx)1049 int otx2_cq_init(struct otx2_nic *pfvf, u16 qidx)
1050 {
1051 	struct otx2_qset *qset = &pfvf->qset;
1052 	int err, pool_id, non_xdp_queues;
1053 	struct nix_aq_enq_req *aq;
1054 	struct otx2_cq_queue *cq;
1055 	struct otx2_pool *pool;
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 #ifdef CONFIG_DCB
1134 			aq->cq.bpid = pfvf->bpid[pfvf->queue_to_pfc_map[qidx]];
1135 #else
1136 			aq->cq.bpid = pfvf->bpid[0];
1137 #endif
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 	struct npa_aq_enq_req *aq;
1371 	struct otx2_pool *pool;
1372 	int err;
1373 
1374 	pool = &pfvf->qset.pool[pool_id];
1375 
1376 	/* Allocate memory for HW to update Aura count.
1377 	 * Alloc one cache line, so that it fits all FC_STYPE modes.
1378 	 */
1379 	if (!pool->fc_addr) {
1380 		err = qmem_alloc(pfvf->dev, &pool->fc_addr, 1, OTX2_ALIGN);
1381 		if (err)
1382 			return err;
1383 	}
1384 
1385 	/* Initialize this aura's context via AF */
1386 	aq = otx2_mbox_alloc_msg_npa_aq_enq(&pfvf->mbox);
1387 	if (!aq) {
1388 		/* Shared mbox memory buffer is full, flush it and retry */
1389 		err = otx2_sync_mbox_msg(&pfvf->mbox);
1390 		if (err)
1391 			return err;
1392 		aq = otx2_mbox_alloc_msg_npa_aq_enq(&pfvf->mbox);
1393 		if (!aq)
1394 			return -ENOMEM;
1395 	}
1396 
1397 	aq->aura_id = aura_id;
1398 	/* Will be filled by AF with correct pool context address */
1399 	aq->aura.pool_addr = pool_id;
1400 	aq->aura.pool_caching = 1;
1401 	aq->aura.shift = ilog2(numptrs) - 8;
1402 	aq->aura.count = numptrs;
1403 	aq->aura.limit = numptrs;
1404 	aq->aura.avg_level = 255;
1405 	aq->aura.ena = 1;
1406 	aq->aura.fc_ena = 1;
1407 	aq->aura.fc_addr = pool->fc_addr->iova;
1408 	aq->aura.fc_hyst_bits = 0; /* Store count on all updates */
1409 
1410 	/* Enable backpressure for RQ aura */
1411 	if (aura_id < pfvf->hw.rqpool_cnt && !is_otx2_lbkvf(pfvf->pdev)) {
1412 		aq->aura.bp_ena = 0;
1413 		/* If NIX1 LF is attached then specify NIX1_RX.
1414 		 *
1415 		 * Below NPA_AURA_S[BP_ENA] is set according to the
1416 		 * NPA_BPINTF_E enumeration given as:
1417 		 * 0x0 + a*0x1 where 'a' is 0 for NIX0_RX and 1 for NIX1_RX so
1418 		 * NIX0_RX is 0x0 + 0*0x1 = 0
1419 		 * NIX1_RX is 0x0 + 1*0x1 = 1
1420 		 * But in HRM it is given that
1421 		 * "NPA_AURA_S[BP_ENA](w1[33:32]) - Enable aura backpressure to
1422 		 * NIX-RX based on [BP] level. One bit per NIX-RX; index
1423 		 * enumerated by NPA_BPINTF_E."
1424 		 */
1425 		if (pfvf->nix_blkaddr == BLKADDR_NIX1)
1426 			aq->aura.bp_ena = 1;
1427 #ifdef CONFIG_DCB
1428 		aq->aura.nix0_bpid = pfvf->bpid[pfvf->queue_to_pfc_map[aura_id]];
1429 #else
1430 		aq->aura.nix0_bpid = pfvf->bpid[0];
1431 #endif
1432 
1433 		/* Set backpressure level for RQ's Aura */
1434 		aq->aura.bp = RQ_BP_LVL_AURA;
1435 	}
1436 
1437 	/* Fill AQ info */
1438 	aq->ctype = NPA_AQ_CTYPE_AURA;
1439 	aq->op = NPA_AQ_INSTOP_INIT;
1440 
1441 	return 0;
1442 }
1443 
otx2_pool_init(struct otx2_nic * pfvf,u16 pool_id,int stack_pages,int numptrs,int buf_size,int type)1444 int otx2_pool_init(struct otx2_nic *pfvf, u16 pool_id,
1445 		   int stack_pages, int numptrs, int buf_size, int type)
1446 {
1447 	struct page_pool_params pp_params = { 0 };
1448 	struct xsk_buff_pool *xsk_pool;
1449 	struct npa_aq_enq_req *aq;
1450 	struct otx2_pool *pool;
1451 	int err;
1452 
1453 	pool = &pfvf->qset.pool[pool_id];
1454 	/* Alloc memory for stack which is used to store buffer pointers */
1455 	err = qmem_alloc(pfvf->dev, &pool->stack,
1456 			 stack_pages, pfvf->hw.stack_pg_bytes);
1457 	if (err)
1458 		return err;
1459 
1460 	pool->rbsize = buf_size;
1461 
1462 	/* Initialize this pool's context via AF */
1463 	aq = otx2_mbox_alloc_msg_npa_aq_enq(&pfvf->mbox);
1464 	if (!aq) {
1465 		/* Shared mbox memory buffer is full, flush it and retry */
1466 		err = otx2_sync_mbox_msg(&pfvf->mbox);
1467 		if (err) {
1468 			qmem_free(pfvf->dev, pool->stack);
1469 			return err;
1470 		}
1471 		aq = otx2_mbox_alloc_msg_npa_aq_enq(&pfvf->mbox);
1472 		if (!aq) {
1473 			qmem_free(pfvf->dev, pool->stack);
1474 			return -ENOMEM;
1475 		}
1476 	}
1477 
1478 	aq->aura_id = pool_id;
1479 	aq->pool.stack_base = pool->stack->iova;
1480 	aq->pool.stack_caching = 1;
1481 	aq->pool.ena = 1;
1482 	aq->pool.buf_size = buf_size / 128;
1483 	aq->pool.stack_max_pages = stack_pages;
1484 	aq->pool.shift = ilog2(numptrs) - 8;
1485 	aq->pool.ptr_start = 0;
1486 	aq->pool.ptr_end = ~0ULL;
1487 
1488 	/* Fill AQ info */
1489 	aq->ctype = NPA_AQ_CTYPE_POOL;
1490 	aq->op = NPA_AQ_INSTOP_INIT;
1491 
1492 	if (type != AURA_NIX_RQ)
1493 		return 0;
1494 
1495 	if (!test_bit(pool_id, pfvf->af_xdp_zc_qidx)) {
1496 		pp_params.order = get_order(buf_size);
1497 		pp_params.flags = PP_FLAG_DMA_MAP;
1498 		pp_params.pool_size = min(OTX2_PAGE_POOL_SZ, numptrs);
1499 		pp_params.nid = NUMA_NO_NODE;
1500 		pp_params.dev = pfvf->dev;
1501 		pp_params.dma_dir = DMA_FROM_DEVICE;
1502 		pool->page_pool = page_pool_create(&pp_params);
1503 		if (IS_ERR(pool->page_pool)) {
1504 			netdev_err(pfvf->netdev, "Creation of page pool failed\n");
1505 			return PTR_ERR(pool->page_pool);
1506 		}
1507 		return 0;
1508 	}
1509 
1510 	/* Set XSK pool to support AF_XDP zero-copy */
1511 	xsk_pool = xsk_get_pool_from_qid(pfvf->netdev, pool_id);
1512 	if (xsk_pool) {
1513 		pool->xsk_pool = xsk_pool;
1514 		pool->xdp_cnt = numptrs;
1515 		pool->xdp = devm_kcalloc(pfvf->dev,
1516 					 numptrs, sizeof(struct xdp_buff *), GFP_KERNEL);
1517 		if (IS_ERR(pool->xdp)) {
1518 			netdev_err(pfvf->netdev, "Creation of xsk pool failed\n");
1519 			return PTR_ERR(pool->xdp);
1520 		}
1521 	}
1522 
1523 	return 0;
1524 }
1525 
otx2_sq_aura_pool_init(struct otx2_nic * pfvf)1526 int otx2_sq_aura_pool_init(struct otx2_nic *pfvf)
1527 {
1528 	int qidx, pool_id, stack_pages, num_sqbs;
1529 	struct otx2_qset *qset = &pfvf->qset;
1530 	struct otx2_hw *hw = &pfvf->hw;
1531 	struct otx2_snd_queue *sq;
1532 	struct otx2_pool *pool;
1533 	dma_addr_t bufptr;
1534 	int err, ptr;
1535 
1536 	/* Calculate number of SQBs needed.
1537 	 *
1538 	 * For a 128byte SQE, and 4K size SQB, 31 SQEs will fit in one SQB.
1539 	 * Last SQE is used for pointing to next SQB.
1540 	 */
1541 	num_sqbs = (hw->sqb_size / 128) - 1;
1542 	num_sqbs = (qset->sqe_cnt + num_sqbs) / num_sqbs;
1543 
1544 	/* Get no of stack pages needed */
1545 	stack_pages =
1546 		(num_sqbs + hw->stack_pg_ptrs - 1) / hw->stack_pg_ptrs;
1547 
1548 	for (qidx = 0; qidx < hw->non_qos_queues; qidx++) {
1549 		pool_id = otx2_get_pool_idx(pfvf, AURA_NIX_SQ, qidx);
1550 		/* Initialize aura context */
1551 		err = otx2_aura_init(pfvf, pool_id, pool_id, num_sqbs);
1552 		if (err)
1553 			goto fail;
1554 
1555 		/* Initialize pool context */
1556 		err = otx2_pool_init(pfvf, pool_id, stack_pages,
1557 				     num_sqbs, hw->sqb_size, AURA_NIX_SQ);
1558 		if (err)
1559 			goto fail;
1560 	}
1561 
1562 	/* Flush accumulated messages */
1563 	err = otx2_sync_mbox_msg(&pfvf->mbox);
1564 	if (err)
1565 		goto fail;
1566 
1567 	/* Allocate pointers and free them to aura/pool */
1568 	for (qidx = 0; qidx < hw->non_qos_queues; qidx++) {
1569 		pool_id = otx2_get_pool_idx(pfvf, AURA_NIX_SQ, qidx);
1570 		pool = &pfvf->qset.pool[pool_id];
1571 
1572 		sq = &qset->sq[qidx];
1573 		sq->sqb_count = 0;
1574 		sq->sqb_ptrs = kcalloc(num_sqbs, sizeof(*sq->sqb_ptrs), GFP_KERNEL);
1575 		if (!sq->sqb_ptrs) {
1576 			err = -ENOMEM;
1577 			goto err_mem;
1578 		}
1579 
1580 		for (ptr = 0; ptr < num_sqbs; ptr++) {
1581 			err = otx2_alloc_rbuf(pfvf, pool, &bufptr, pool_id, ptr);
1582 			if (err) {
1583 				if (pool->xsk_pool) {
1584 					ptr--;
1585 					while (ptr >= 0) {
1586 						xsk_buff_free(pool->xdp[ptr]);
1587 						ptr--;
1588 					}
1589 				}
1590 				goto err_mem;
1591 			}
1592 
1593 			pfvf->hw_ops->aura_freeptr(pfvf, pool_id, bufptr);
1594 			sq->sqb_ptrs[sq->sqb_count++] = (u64)bufptr;
1595 		}
1596 	}
1597 
1598 err_mem:
1599 	return err ? -ENOMEM : 0;
1600 
1601 fail:
1602 	otx2_mbox_reset(&pfvf->mbox.mbox, 0);
1603 	otx2_aura_pool_free(pfvf);
1604 	return err;
1605 }
1606 
otx2_rq_aura_pool_init(struct otx2_nic * pfvf)1607 int otx2_rq_aura_pool_init(struct otx2_nic *pfvf)
1608 {
1609 	struct otx2_hw *hw = &pfvf->hw;
1610 	int stack_pages, pool_id, rq;
1611 	struct otx2_pool *pool;
1612 	int err, ptr, num_ptrs;
1613 	dma_addr_t bufptr;
1614 
1615 	num_ptrs = pfvf->qset.rqe_cnt;
1616 
1617 	stack_pages =
1618 		(num_ptrs + hw->stack_pg_ptrs - 1) / hw->stack_pg_ptrs;
1619 
1620 	for (rq = 0; rq < hw->rx_queues; rq++) {
1621 		pool_id = otx2_get_pool_idx(pfvf, AURA_NIX_RQ, rq);
1622 		/* Initialize aura context */
1623 		err = otx2_aura_init(pfvf, pool_id, pool_id, num_ptrs);
1624 		if (err)
1625 			goto fail;
1626 	}
1627 	for (pool_id = 0; pool_id < hw->rqpool_cnt; pool_id++) {
1628 		err = otx2_pool_init(pfvf, pool_id, stack_pages,
1629 				     num_ptrs, pfvf->rbsize, AURA_NIX_RQ);
1630 		if (err)
1631 			goto fail;
1632 	}
1633 
1634 	/* Flush accumulated messages */
1635 	err = otx2_sync_mbox_msg(&pfvf->mbox);
1636 	if (err)
1637 		goto fail;
1638 
1639 	/* Allocate pointers and free them to aura/pool */
1640 	for (pool_id = 0; pool_id < hw->rqpool_cnt; pool_id++) {
1641 		pool = &pfvf->qset.pool[pool_id];
1642 
1643 		for (ptr = 0; ptr < num_ptrs; ptr++) {
1644 			err = otx2_alloc_rbuf(pfvf, pool, &bufptr, pool_id, ptr);
1645 			if (err) {
1646 				if (pool->xsk_pool) {
1647 					while (ptr)
1648 						xsk_buff_free(pool->xdp[--ptr]);
1649 				}
1650 				return -ENOMEM;
1651 			}
1652 
1653 			pfvf->hw_ops->aura_freeptr(pfvf, pool_id,
1654 						   pool->xsk_pool ? bufptr :
1655 						   bufptr + OTX2_HEAD_ROOM);
1656 		}
1657 	}
1658 	return 0;
1659 fail:
1660 	otx2_mbox_reset(&pfvf->mbox.mbox, 0);
1661 	otx2_aura_pool_free(pfvf);
1662 	return err;
1663 }
1664 
otx2_config_npa(struct otx2_nic * pfvf)1665 int otx2_config_npa(struct otx2_nic *pfvf)
1666 {
1667 	struct otx2_qset *qset = &pfvf->qset;
1668 	struct npa_lf_alloc_req  *npalf;
1669 	struct otx2_hw *hw = &pfvf->hw;
1670 	int aura_cnt;
1671 
1672 	/* Pool - Stack of free buffer pointers
1673 	 * Aura - Alloc/frees pointers from/to pool for NIX DMA.
1674 	 */
1675 
1676 	if (!hw->pool_cnt)
1677 		return -EINVAL;
1678 
1679 	qset->pool = devm_kcalloc(pfvf->dev, hw->pool_cnt,
1680 				  sizeof(struct otx2_pool), GFP_KERNEL);
1681 	if (!qset->pool)
1682 		return -ENOMEM;
1683 
1684 	/* Get memory to put this msg */
1685 	npalf = otx2_mbox_alloc_msg_npa_lf_alloc(&pfvf->mbox);
1686 	if (!npalf)
1687 		return -ENOMEM;
1688 
1689 	/* Set aura and pool counts */
1690 	npalf->nr_pools = hw->pool_cnt;
1691 	aura_cnt = ilog2(roundup_pow_of_two(hw->pool_cnt));
1692 	npalf->aura_sz = (aura_cnt >= ilog2(128)) ? (aura_cnt - 6) : 1;
1693 
1694 	return otx2_sync_mbox_msg(&pfvf->mbox);
1695 }
1696 
otx2_detach_resources(struct mbox * mbox)1697 int otx2_detach_resources(struct mbox *mbox)
1698 {
1699 	struct rsrc_detach *detach;
1700 
1701 	mutex_lock(&mbox->lock);
1702 	detach = otx2_mbox_alloc_msg_detach_resources(mbox);
1703 	if (!detach) {
1704 		mutex_unlock(&mbox->lock);
1705 		return -ENOMEM;
1706 	}
1707 
1708 	/* detach all */
1709 	detach->partial = false;
1710 
1711 	/* Send detach request to AF */
1712 	otx2_sync_mbox_msg(mbox);
1713 	mutex_unlock(&mbox->lock);
1714 	return 0;
1715 }
1716 EXPORT_SYMBOL(otx2_detach_resources);
1717 
otx2_attach_npa_nix(struct otx2_nic * pfvf)1718 int otx2_attach_npa_nix(struct otx2_nic *pfvf)
1719 {
1720 	struct rsrc_attach *attach;
1721 	struct msg_req *msix;
1722 	int err;
1723 
1724 	mutex_lock(&pfvf->mbox.lock);
1725 	/* Get memory to put this msg */
1726 	attach = otx2_mbox_alloc_msg_attach_resources(&pfvf->mbox);
1727 	if (!attach) {
1728 		mutex_unlock(&pfvf->mbox.lock);
1729 		return -ENOMEM;
1730 	}
1731 
1732 	attach->npalf = true;
1733 	attach->nixlf = true;
1734 
1735 	/* Send attach request to AF */
1736 	err = otx2_sync_mbox_msg(&pfvf->mbox);
1737 	if (err) {
1738 		mutex_unlock(&pfvf->mbox.lock);
1739 		return err;
1740 	}
1741 
1742 	pfvf->nix_blkaddr = BLKADDR_NIX0;
1743 
1744 	/* If the platform has two NIX blocks then LF may be
1745 	 * allocated from NIX1.
1746 	 */
1747 	if (otx2_read64(pfvf, RVU_PF_BLOCK_ADDRX_DISC(BLKADDR_NIX1)) & 0x1FFULL)
1748 		pfvf->nix_blkaddr = BLKADDR_NIX1;
1749 
1750 	/* Get NPA and NIX MSIX vector offsets */
1751 	msix = otx2_mbox_alloc_msg_msix_offset(&pfvf->mbox);
1752 	if (!msix) {
1753 		mutex_unlock(&pfvf->mbox.lock);
1754 		return -ENOMEM;
1755 	}
1756 
1757 	err = otx2_sync_mbox_msg(&pfvf->mbox);
1758 	if (err) {
1759 		mutex_unlock(&pfvf->mbox.lock);
1760 		return err;
1761 	}
1762 	mutex_unlock(&pfvf->mbox.lock);
1763 
1764 	if (pfvf->hw.npa_msixoff == MSIX_VECTOR_INVALID ||
1765 	    pfvf->hw.nix_msixoff == MSIX_VECTOR_INVALID) {
1766 		dev_err(pfvf->dev,
1767 			"RVUPF: Invalid MSIX vector offset for NPA/NIX\n");
1768 		return -EINVAL;
1769 	}
1770 
1771 	return 0;
1772 }
1773 EXPORT_SYMBOL(otx2_attach_npa_nix);
1774 
otx2_ctx_disable(struct mbox * mbox,int type,bool npa)1775 void otx2_ctx_disable(struct mbox *mbox, int type, bool npa)
1776 {
1777 	struct hwctx_disable_req *req;
1778 
1779 	mutex_lock(&mbox->lock);
1780 	/* Request AQ to disable this context */
1781 	if (npa)
1782 		req = otx2_mbox_alloc_msg_npa_hwctx_disable(mbox);
1783 	else
1784 		req = otx2_mbox_alloc_msg_nix_hwctx_disable(mbox);
1785 
1786 	if (!req) {
1787 		mutex_unlock(&mbox->lock);
1788 		return;
1789 	}
1790 
1791 	req->ctype = type;
1792 
1793 	if (otx2_sync_mbox_msg(mbox))
1794 		dev_err(mbox->pfvf->dev, "%s failed to disable context\n",
1795 			__func__);
1796 
1797 	mutex_unlock(&mbox->lock);
1798 }
1799 
otx2_nix_config_bp(struct otx2_nic * pfvf,bool enable)1800 int otx2_nix_config_bp(struct otx2_nic *pfvf, bool enable)
1801 {
1802 	struct nix_bp_cfg_req *req;
1803 
1804 	if (enable)
1805 		req = otx2_mbox_alloc_msg_nix_bp_enable(&pfvf->mbox);
1806 	else
1807 		req = otx2_mbox_alloc_msg_nix_bp_disable(&pfvf->mbox);
1808 
1809 	if (!req)
1810 		return -ENOMEM;
1811 
1812 	req->chan_base = 0;
1813 	if (otx2_is_pfc_enabled(pfvf)) {
1814 		req->chan_cnt = IEEE_8021QAZ_MAX_TCS;
1815 		req->bpid_per_chan = 1;
1816 	} else {
1817 		req->chan_cnt = pfvf->hw.rx_chan_cnt;
1818 		req->bpid_per_chan = 0;
1819 	}
1820 
1821 	return otx2_sync_mbox_msg(&pfvf->mbox);
1822 }
1823 EXPORT_SYMBOL(otx2_nix_config_bp);
1824 
otx2_nix_cpt_config_bp(struct otx2_nic * pfvf,bool enable)1825 int otx2_nix_cpt_config_bp(struct otx2_nic *pfvf, bool enable)
1826 {
1827 	struct nix_bp_cfg_req *req;
1828 
1829 	if (enable)
1830 		req = otx2_mbox_alloc_msg_nix_cpt_bp_enable(&pfvf->mbox);
1831 	else
1832 		req = otx2_mbox_alloc_msg_nix_cpt_bp_disable(&pfvf->mbox);
1833 
1834 	if (!req)
1835 		return -ENOMEM;
1836 
1837 	req->chan_base = 0;
1838 	if (otx2_is_pfc_enabled(pfvf)) {
1839 		req->chan_cnt = IEEE_8021QAZ_MAX_TCS;
1840 		req->bpid_per_chan = 1;
1841 	} else {
1842 		req->chan_cnt = pfvf->hw.rx_chan_cnt;
1843 		req->bpid_per_chan = 0;
1844 	}
1845 
1846 	return otx2_sync_mbox_msg(&pfvf->mbox);
1847 }
1848 EXPORT_SYMBOL(otx2_nix_cpt_config_bp);
1849 
1850 /* Mbox message handlers */
mbox_handler_cgx_stats(struct otx2_nic * pfvf,struct cgx_stats_rsp * rsp)1851 void mbox_handler_cgx_stats(struct otx2_nic *pfvf,
1852 			    struct cgx_stats_rsp *rsp)
1853 {
1854 	int id;
1855 
1856 	for (id = 0; id < CGX_RX_STATS_COUNT; id++)
1857 		pfvf->hw.cgx_rx_stats[id] = rsp->rx_stats[id];
1858 	for (id = 0; id < CGX_TX_STATS_COUNT; id++)
1859 		pfvf->hw.cgx_tx_stats[id] = rsp->tx_stats[id];
1860 }
1861 
mbox_handler_cgx_fec_stats(struct otx2_nic * pfvf,struct cgx_fec_stats_rsp * rsp)1862 void mbox_handler_cgx_fec_stats(struct otx2_nic *pfvf,
1863 				struct cgx_fec_stats_rsp *rsp)
1864 {
1865 	pfvf->hw.cgx_fec_corr_blks += rsp->fec_corr_blks;
1866 	pfvf->hw.cgx_fec_uncorr_blks += rsp->fec_uncorr_blks;
1867 }
1868 
mbox_handler_npa_lf_alloc(struct otx2_nic * pfvf,struct npa_lf_alloc_rsp * rsp)1869 void mbox_handler_npa_lf_alloc(struct otx2_nic *pfvf,
1870 			       struct npa_lf_alloc_rsp *rsp)
1871 {
1872 	pfvf->hw.stack_pg_ptrs = rsp->stack_pg_ptrs;
1873 	pfvf->hw.stack_pg_bytes = rsp->stack_pg_bytes;
1874 }
1875 EXPORT_SYMBOL(mbox_handler_npa_lf_alloc);
1876 
mbox_handler_nix_lf_alloc(struct otx2_nic * pfvf,struct nix_lf_alloc_rsp * rsp)1877 void mbox_handler_nix_lf_alloc(struct otx2_nic *pfvf,
1878 			       struct nix_lf_alloc_rsp *rsp)
1879 {
1880 	pfvf->hw.sqb_size = rsp->sqb_size;
1881 	pfvf->hw.rx_chan_base = rsp->rx_chan_base;
1882 	pfvf->hw.tx_chan_base = rsp->tx_chan_base;
1883 	pfvf->hw.rx_chan_cnt = rsp->rx_chan_cnt;
1884 	pfvf->hw.tx_chan_cnt = rsp->tx_chan_cnt;
1885 	pfvf->hw.lso_tsov4_idx = rsp->lso_tsov4_idx;
1886 	pfvf->hw.lso_tsov6_idx = rsp->lso_tsov6_idx;
1887 	pfvf->hw.cgx_links = rsp->cgx_links;
1888 	pfvf->hw.lbk_links = rsp->lbk_links;
1889 	pfvf->hw.tx_link = rsp->tx_link;
1890 }
1891 EXPORT_SYMBOL(mbox_handler_nix_lf_alloc);
1892 
mbox_handler_msix_offset(struct otx2_nic * pfvf,struct msix_offset_rsp * rsp)1893 void mbox_handler_msix_offset(struct otx2_nic *pfvf,
1894 			      struct msix_offset_rsp *rsp)
1895 {
1896 	pfvf->hw.npa_msixoff = rsp->npa_msixoff;
1897 	pfvf->hw.nix_msixoff = rsp->nix_msixoff;
1898 }
1899 EXPORT_SYMBOL(mbox_handler_msix_offset);
1900 
mbox_handler_nix_bp_enable(struct otx2_nic * pfvf,struct nix_bp_cfg_rsp * rsp)1901 void mbox_handler_nix_bp_enable(struct otx2_nic *pfvf,
1902 				struct nix_bp_cfg_rsp *rsp)
1903 {
1904 	int chan, chan_id;
1905 
1906 	for (chan = 0; chan < rsp->chan_cnt; chan++) {
1907 		chan_id = ((rsp->chan_bpid[chan] >> 10) & 0x7F);
1908 		pfvf->bpid[chan_id] = rsp->chan_bpid[chan] & 0x3FF;
1909 	}
1910 }
1911 EXPORT_SYMBOL(mbox_handler_nix_bp_enable);
1912 
otx2_free_cints(struct otx2_nic * pfvf,int n)1913 void otx2_free_cints(struct otx2_nic *pfvf, int n)
1914 {
1915 	struct otx2_qset *qset = &pfvf->qset;
1916 	struct otx2_hw *hw = &pfvf->hw;
1917 	int irq, qidx;
1918 
1919 	for (qidx = 0, irq = hw->nix_msixoff + NIX_LF_CINT_VEC_START;
1920 	     qidx < n;
1921 	     qidx++, irq++) {
1922 		int vector = pci_irq_vector(pfvf->pdev, irq);
1923 
1924 		irq_set_affinity_hint(vector, NULL);
1925 		free_cpumask_var(hw->affinity_mask[irq]);
1926 		free_irq(vector, &qset->napi[qidx]);
1927 	}
1928 }
1929 EXPORT_SYMBOL(otx2_free_cints);
1930 
otx2_set_cints_affinity(struct otx2_nic * pfvf)1931 void otx2_set_cints_affinity(struct otx2_nic *pfvf)
1932 {
1933 	struct otx2_hw *hw = &pfvf->hw;
1934 	int vec, cpu, irq, cint;
1935 
1936 	vec = hw->nix_msixoff + NIX_LF_CINT_VEC_START;
1937 	cpu = cpumask_first(cpu_online_mask);
1938 
1939 	/* CQ interrupts */
1940 	for (cint = 0; cint < pfvf->hw.cint_cnt; cint++, vec++) {
1941 		if (!alloc_cpumask_var(&hw->affinity_mask[vec], GFP_KERNEL))
1942 			return;
1943 
1944 		cpumask_set_cpu(cpu, hw->affinity_mask[vec]);
1945 
1946 		irq = pci_irq_vector(pfvf->pdev, vec);
1947 		irq_set_affinity_hint(irq, hw->affinity_mask[vec]);
1948 
1949 		cpu = cpumask_next(cpu, cpu_online_mask);
1950 		if (unlikely(cpu >= nr_cpu_ids))
1951 			cpu = 0;
1952 	}
1953 }
1954 
get_dwrr_mtu(struct otx2_nic * pfvf,struct nix_hw_info * hw)1955 static u32 get_dwrr_mtu(struct otx2_nic *pfvf, struct nix_hw_info *hw)
1956 {
1957 	if (is_otx2_lbkvf(pfvf->pdev)) {
1958 		pfvf->hw.smq_link_type = SMQ_LINK_TYPE_LBK;
1959 		return hw->lbk_dwrr_mtu;
1960 	}
1961 
1962 	pfvf->hw.smq_link_type = SMQ_LINK_TYPE_RPM;
1963 	return hw->rpm_dwrr_mtu;
1964 }
1965 
otx2_get_max_mtu(struct otx2_nic * pfvf)1966 u16 otx2_get_max_mtu(struct otx2_nic *pfvf)
1967 {
1968 	struct nix_hw_info *rsp;
1969 	struct msg_req *req;
1970 	u16 max_mtu;
1971 	int rc;
1972 
1973 	mutex_lock(&pfvf->mbox.lock);
1974 
1975 	req = otx2_mbox_alloc_msg_nix_get_hw_info(&pfvf->mbox);
1976 	if (!req) {
1977 		rc =  -ENOMEM;
1978 		goto out;
1979 	}
1980 
1981 	rc = otx2_sync_mbox_msg(&pfvf->mbox);
1982 	if (!rc) {
1983 		rsp = (struct nix_hw_info *)
1984 		       otx2_mbox_get_rsp(&pfvf->mbox.mbox, 0, &req->hdr);
1985 		if (IS_ERR(rsp)) {
1986 			rc = PTR_ERR(rsp);
1987 			goto out;
1988 		}
1989 
1990 		/* HW counts VLAN insertion bytes (8 for double tag)
1991 		 * irrespective of whether SQE is requesting to insert VLAN
1992 		 * in the packet or not. Hence these 8 bytes have to be
1993 		 * discounted from max packet size otherwise HW will throw
1994 		 * SMQ errors
1995 		 */
1996 		max_mtu = rsp->max_mtu - 8 - OTX2_ETH_HLEN;
1997 
1998 		/* Also save DWRR MTU, needed for DWRR weight calculation */
1999 		pfvf->hw.dwrr_mtu = get_dwrr_mtu(pfvf, rsp);
2000 		if (!pfvf->hw.dwrr_mtu)
2001 			pfvf->hw.dwrr_mtu = 1;
2002 	}
2003 
2004 out:
2005 	mutex_unlock(&pfvf->mbox.lock);
2006 	if (rc) {
2007 		dev_warn(pfvf->dev,
2008 			 "Failed to get MTU from hardware setting default value(1500)\n");
2009 		max_mtu = 1500;
2010 	}
2011 	return max_mtu;
2012 }
2013 EXPORT_SYMBOL(otx2_get_max_mtu);
2014 
otx2_handle_ntuple_tc_features(struct net_device * netdev,netdev_features_t features)2015 int otx2_handle_ntuple_tc_features(struct net_device *netdev, netdev_features_t features)
2016 {
2017 	netdev_features_t changed = features ^ netdev->features;
2018 	struct otx2_nic *pfvf = netdev_priv(netdev);
2019 	bool ntuple = !!(features & NETIF_F_NTUPLE);
2020 	bool tc = !!(features & NETIF_F_HW_TC);
2021 
2022 	if ((changed & NETIF_F_NTUPLE) && !ntuple)
2023 		otx2_destroy_ntuple_flows(pfvf);
2024 
2025 	if ((changed & NETIF_F_NTUPLE) && ntuple) {
2026 		if (!pfvf->flow_cfg->max_flows) {
2027 			netdev_err(netdev,
2028 				   "Can't enable NTUPLE, MCAM entries not allocated\n");
2029 			return -EINVAL;
2030 		}
2031 	}
2032 
2033 	if ((changed & NETIF_F_HW_TC) && !tc &&
2034 	    otx2_tc_flower_rule_cnt(pfvf)) {
2035 		netdev_err(netdev, "Can't disable TC hardware offload while flows are active\n");
2036 		return -EBUSY;
2037 	}
2038 
2039 	if ((changed & NETIF_F_NTUPLE) && ntuple &&
2040 	    otx2_tc_flower_rule_cnt(pfvf) && !(changed & NETIF_F_HW_TC)) {
2041 		netdev_err(netdev,
2042 			   "Can't enable NTUPLE when TC flower offload is active, disable TC rules and retry\n");
2043 		return -EINVAL;
2044 	}
2045 
2046 	return 0;
2047 }
2048 EXPORT_SYMBOL(otx2_handle_ntuple_tc_features);
2049 
otx2_set_hw_capabilities(struct otx2_nic * pfvf)2050 int otx2_set_hw_capabilities(struct otx2_nic *pfvf)
2051 {
2052 	struct mbox *mbox = &pfvf->mbox;
2053 	struct otx2_hw *hw = &pfvf->hw;
2054 	struct get_hw_cap_rsp *rsp;
2055 	struct msg_req *req;
2056 	int ret = -ENOMEM;
2057 
2058 	mutex_lock(&mbox->lock);
2059 
2060 	req = otx2_mbox_alloc_msg_get_hw_cap(mbox);
2061 	if (!req)
2062 		goto fail;
2063 
2064 	ret = otx2_sync_mbox_msg(mbox);
2065 	if (ret)
2066 		goto fail;
2067 
2068 	rsp = (struct get_hw_cap_rsp *)otx2_mbox_get_rsp(&pfvf->mbox.mbox,
2069 							 0, &req->hdr);
2070 	if (IS_ERR(rsp)) {
2071 		ret = -EINVAL;
2072 		goto fail;
2073 	}
2074 
2075 	if (rsp->hw_caps & HW_CAP_MACSEC)
2076 		__set_bit(CN10K_HW_MACSEC, &hw->cap_flag);
2077 
2078 	mutex_unlock(&mbox->lock);
2079 
2080 	return 0;
2081 fail:
2082 	dev_err(pfvf->dev, "Cannot get MACSEC capability from AF\n");
2083 	mutex_unlock(&mbox->lock);
2084 	return ret;
2085 }
2086 
2087 #define M(_name, _id, _fn_name, _req_type, _rsp_type)			\
2088 int __weak								\
2089 otx2_mbox_up_handler_ ## _fn_name(struct otx2_nic *pfvf,		\
2090 				struct _req_type *req,			\
2091 				struct _rsp_type *rsp)			\
2092 {									\
2093 	/* Nothing to do here */					\
2094 	return 0;							\
2095 }									\
2096 EXPORT_SYMBOL(otx2_mbox_up_handler_ ## _fn_name);
2097 MBOX_UP_CGX_MESSAGES
2098 MBOX_UP_MCS_MESSAGES
2099 #undef M
2100 
otx2_dma_map_skb_frag(struct otx2_nic * pfvf,struct sk_buff * skb,int seg,int * len)2101 dma_addr_t otx2_dma_map_skb_frag(struct otx2_nic *pfvf,
2102 				 struct sk_buff *skb, int seg, int *len)
2103 {
2104 	enum dma_data_direction dir = DMA_TO_DEVICE;
2105 	const skb_frag_t *frag;
2106 	struct page *page;
2107 	int offset;
2108 
2109 	/* Crypto hardware need write permission for ipsec crypto offload */
2110 	if (unlikely(xfrm_offload(skb))) {
2111 		dir = DMA_BIDIRECTIONAL;
2112 		skb = skb_unshare(skb, GFP_ATOMIC);
2113 	}
2114 
2115 	/* First segment is always skb->data */
2116 	if (!seg) {
2117 		page = virt_to_page(skb->data);
2118 		offset = offset_in_page(skb->data);
2119 		*len = skb_headlen(skb);
2120 	} else {
2121 		frag = &skb_shinfo(skb)->frags[seg - 1];
2122 		page = skb_frag_page(frag);
2123 		offset = skb_frag_off(frag);
2124 		*len = skb_frag_size(frag);
2125 	}
2126 	return otx2_dma_map_page(pfvf, page, offset, *len, dir);
2127 }
2128 
otx2_dma_unmap_skb_frags(struct otx2_nic * pfvf,struct sg_list * sg)2129 void otx2_dma_unmap_skb_frags(struct otx2_nic *pfvf, struct sg_list *sg)
2130 {
2131 	enum dma_data_direction dir = DMA_TO_DEVICE;
2132 	struct sk_buff *skb = NULL;
2133 	int seg;
2134 
2135 	skb = (struct sk_buff *)sg->skb;
2136 	if (unlikely(xfrm_offload(skb)))
2137 		dir = DMA_BIDIRECTIONAL;
2138 
2139 	for (seg = 0; seg < sg->num_segs; seg++) {
2140 		otx2_dma_unmap_page(pfvf, sg->dma_addr[seg],
2141 				    sg->size[seg], dir);
2142 	}
2143 	sg->num_segs = 0;
2144 }
2145