xref: /openbmc/linux/drivers/net/ethernet/marvell/octeontx2/nic/otx2_common.c (revision ecc23d0a422a3118fcf6e4f0a46e17a6c2047b02)
15a6d7c9dSSunil Goutham // SPDX-License-Identifier: GPL-2.0
2cb0e3ec4SSunil Goutham /* Marvell RVU Ethernet driver
35a6d7c9dSSunil Goutham  *
4cb0e3ec4SSunil Goutham  * Copyright (C) 2020 Marvell.
55a6d7c9dSSunil Goutham  *
65a6d7c9dSSunil Goutham  */
75a6d7c9dSSunil Goutham 
85a6d7c9dSSunil Goutham #include <linux/interrupt.h>
95a6d7c9dSSunil Goutham #include <linux/pci.h>
10a9ca9f9cSYunsheng Lin #include <net/page_pool/helpers.h>
1186d74760SSunil Goutham #include <net/tso.h>
12bbba125eSSunil Goutham #include <linux/bitfield.h>
135a6d7c9dSSunil Goutham 
145a6d7c9dSSunil Goutham #include "otx2_reg.h"
155a6d7c9dSSunil Goutham #include "otx2_common.h"
1605fcc9e0SSunil Goutham #include "otx2_struct.h"
174c236d5dSGeetha sowjanya #include "cn10k.h"
1805fcc9e0SSunil Goutham 
otx2_nix_rq_op_stats(struct queue_stats * stats,struct otx2_nic * pfvf,int qidx)19d45d8979SChristina Jacob static void otx2_nix_rq_op_stats(struct queue_stats *stats,
20d45d8979SChristina Jacob 				 struct otx2_nic *pfvf, int qidx)
21d45d8979SChristina Jacob {
22d45d8979SChristina Jacob 	u64 incr = (u64)qidx << 32;
23d45d8979SChristina Jacob 	u64 *ptr;
24d45d8979SChristina Jacob 
25d45d8979SChristina Jacob 	ptr = (u64 *)otx2_get_regaddr(pfvf, NIX_LF_RQ_OP_OCTS);
26d45d8979SChristina Jacob 	stats->bytes = otx2_atomic64_add(incr, ptr);
27d45d8979SChristina Jacob 
28d45d8979SChristina Jacob 	ptr = (u64 *)otx2_get_regaddr(pfvf, NIX_LF_RQ_OP_PKTS);
29d45d8979SChristina Jacob 	stats->pkts = otx2_atomic64_add(incr, ptr);
30d45d8979SChristina Jacob }
31d45d8979SChristina Jacob 
otx2_nix_sq_op_stats(struct queue_stats * stats,struct otx2_nic * pfvf,int qidx)32d45d8979SChristina Jacob static void otx2_nix_sq_op_stats(struct queue_stats *stats,
33d45d8979SChristina Jacob 				 struct otx2_nic *pfvf, int qidx)
34d45d8979SChristina Jacob {
35d45d8979SChristina Jacob 	u64 incr = (u64)qidx << 32;
36d45d8979SChristina Jacob 	u64 *ptr;
37d45d8979SChristina Jacob 
38d45d8979SChristina Jacob 	ptr = (u64 *)otx2_get_regaddr(pfvf, NIX_LF_SQ_OP_OCTS);
39d45d8979SChristina Jacob 	stats->bytes = otx2_atomic64_add(incr, ptr);
40d45d8979SChristina Jacob 
41d45d8979SChristina Jacob 	ptr = (u64 *)otx2_get_regaddr(pfvf, NIX_LF_SQ_OP_PKTS);
42d45d8979SChristina Jacob 	stats->pkts = otx2_atomic64_add(incr, ptr);
43d45d8979SChristina Jacob }
44d45d8979SChristina Jacob 
otx2_update_lmac_stats(struct otx2_nic * pfvf)45d45d8979SChristina Jacob void otx2_update_lmac_stats(struct otx2_nic *pfvf)
46d45d8979SChristina Jacob {
47d45d8979SChristina Jacob 	struct msg_req *req;
48d45d8979SChristina Jacob 
49d45d8979SChristina Jacob 	if (!netif_running(pfvf->netdev))
50d45d8979SChristina Jacob 		return;
51d45d8979SChristina Jacob 
524c3212f5SSunil Goutham 	mutex_lock(&pfvf->mbox.lock);
53d45d8979SChristina Jacob 	req = otx2_mbox_alloc_msg_cgx_stats(&pfvf->mbox);
54d45d8979SChristina Jacob 	if (!req) {
554c3212f5SSunil Goutham 		mutex_unlock(&pfvf->mbox.lock);
56d45d8979SChristina Jacob 		return;
57d45d8979SChristina Jacob 	}
58d45d8979SChristina Jacob 
59d45d8979SChristina Jacob 	otx2_sync_mbox_msg(&pfvf->mbox);
604c3212f5SSunil Goutham 	mutex_unlock(&pfvf->mbox.lock);
61d45d8979SChristina Jacob }
62d45d8979SChristina Jacob 
otx2_update_lmac_fec_stats(struct otx2_nic * pfvf)63d0cf9503SChristina Jacob void otx2_update_lmac_fec_stats(struct otx2_nic *pfvf)
64d0cf9503SChristina Jacob {
65d0cf9503SChristina Jacob 	struct msg_req *req;
66d0cf9503SChristina Jacob 
67d0cf9503SChristina Jacob 	if (!netif_running(pfvf->netdev))
68d0cf9503SChristina Jacob 		return;
69d0cf9503SChristina Jacob 	mutex_lock(&pfvf->mbox.lock);
70d0cf9503SChristina Jacob 	req = otx2_mbox_alloc_msg_cgx_fec_stats(&pfvf->mbox);
71d0cf9503SChristina Jacob 	if (req)
72d0cf9503SChristina Jacob 		otx2_sync_mbox_msg(&pfvf->mbox);
73d0cf9503SChristina Jacob 	mutex_unlock(&pfvf->mbox.lock);
74d0cf9503SChristina Jacob }
75d0cf9503SChristina Jacob 
otx2_update_rq_stats(struct otx2_nic * pfvf,int qidx)76d45d8979SChristina Jacob int otx2_update_rq_stats(struct otx2_nic *pfvf, int qidx)
77d45d8979SChristina Jacob {
78d45d8979SChristina Jacob 	struct otx2_rcv_queue *rq = &pfvf->qset.rq[qidx];
79d45d8979SChristina Jacob 
80d45d8979SChristina Jacob 	if (!pfvf->qset.rq)
81d45d8979SChristina Jacob 		return 0;
82d45d8979SChristina Jacob 
83d45d8979SChristina Jacob 	otx2_nix_rq_op_stats(&rq->stats, pfvf, qidx);
84d45d8979SChristina Jacob 	return 1;
85d45d8979SChristina Jacob }
86d45d8979SChristina Jacob 
otx2_update_sq_stats(struct otx2_nic * pfvf,int qidx)87d45d8979SChristina Jacob int otx2_update_sq_stats(struct otx2_nic *pfvf, int qidx)
88d45d8979SChristina Jacob {
89d45d8979SChristina Jacob 	struct otx2_snd_queue *sq = &pfvf->qset.sq[qidx];
90d45d8979SChristina Jacob 
91d45d8979SChristina Jacob 	if (!pfvf->qset.sq)
92d45d8979SChristina Jacob 		return 0;
93d45d8979SChristina Jacob 
945e6808b4SNaveen Mamindlapalli 	if (qidx >= pfvf->hw.non_qos_queues) {
955e6808b4SNaveen Mamindlapalli 		if (!test_bit(qidx - pfvf->hw.non_qos_queues, pfvf->qos.qos_sq_bmap))
965e6808b4SNaveen Mamindlapalli 			return 0;
975e6808b4SNaveen Mamindlapalli 	}
985e6808b4SNaveen Mamindlapalli 
99d45d8979SChristina Jacob 	otx2_nix_sq_op_stats(&sq->stats, pfvf, qidx);
100d45d8979SChristina Jacob 	return 1;
101d45d8979SChristina Jacob }
102d45d8979SChristina Jacob 
otx2_get_dev_stats(struct otx2_nic * pfvf)103e239d0c7SGeetha sowjanya void otx2_get_dev_stats(struct otx2_nic *pfvf)
104e239d0c7SGeetha sowjanya {
105e239d0c7SGeetha sowjanya 	struct otx2_dev_stats *dev_stats = &pfvf->hw.dev_stats;
106e239d0c7SGeetha sowjanya 
107e239d0c7SGeetha sowjanya 	dev_stats->rx_bytes = OTX2_GET_RX_STATS(RX_OCTS);
108e239d0c7SGeetha sowjanya 	dev_stats->rx_drops = OTX2_GET_RX_STATS(RX_DROP);
109e239d0c7SGeetha sowjanya 	dev_stats->rx_bcast_frames = OTX2_GET_RX_STATS(RX_BCAST);
110e239d0c7SGeetha sowjanya 	dev_stats->rx_mcast_frames = OTX2_GET_RX_STATS(RX_MCAST);
111e239d0c7SGeetha sowjanya 	dev_stats->rx_ucast_frames = OTX2_GET_RX_STATS(RX_UCAST);
112e239d0c7SGeetha sowjanya 	dev_stats->rx_frames = dev_stats->rx_bcast_frames +
113e239d0c7SGeetha sowjanya 			       dev_stats->rx_mcast_frames +
114e239d0c7SGeetha sowjanya 			       dev_stats->rx_ucast_frames;
115e239d0c7SGeetha sowjanya 
116e239d0c7SGeetha sowjanya 	dev_stats->tx_bytes = OTX2_GET_TX_STATS(TX_OCTS);
117e239d0c7SGeetha sowjanya 	dev_stats->tx_drops = OTX2_GET_TX_STATS(TX_DROP);
118e239d0c7SGeetha sowjanya 	dev_stats->tx_bcast_frames = OTX2_GET_TX_STATS(TX_BCAST);
119e239d0c7SGeetha sowjanya 	dev_stats->tx_mcast_frames = OTX2_GET_TX_STATS(TX_MCAST);
120e239d0c7SGeetha sowjanya 	dev_stats->tx_ucast_frames = OTX2_GET_TX_STATS(TX_UCAST);
121e239d0c7SGeetha sowjanya 	dev_stats->tx_frames = dev_stats->tx_bcast_frames +
122e239d0c7SGeetha sowjanya 			       dev_stats->tx_mcast_frames +
123e239d0c7SGeetha sowjanya 			       dev_stats->tx_ucast_frames;
124e239d0c7SGeetha sowjanya }
125e239d0c7SGeetha sowjanya 
otx2_get_stats64(struct net_device * netdev,struct rtnl_link_stats64 * stats)126e239d0c7SGeetha sowjanya void otx2_get_stats64(struct net_device *netdev,
127e239d0c7SGeetha sowjanya 		      struct rtnl_link_stats64 *stats)
128e239d0c7SGeetha sowjanya {
129e239d0c7SGeetha sowjanya 	struct otx2_nic *pfvf = netdev_priv(netdev);
130e239d0c7SGeetha sowjanya 	struct otx2_dev_stats *dev_stats;
131e239d0c7SGeetha sowjanya 
132e239d0c7SGeetha sowjanya 	otx2_get_dev_stats(pfvf);
133e239d0c7SGeetha sowjanya 
134e239d0c7SGeetha sowjanya 	dev_stats = &pfvf->hw.dev_stats;
135e239d0c7SGeetha sowjanya 	stats->rx_bytes = dev_stats->rx_bytes;
136e239d0c7SGeetha sowjanya 	stats->rx_packets = dev_stats->rx_frames;
137e239d0c7SGeetha sowjanya 	stats->rx_dropped = dev_stats->rx_drops;
138e239d0c7SGeetha sowjanya 	stats->multicast = dev_stats->rx_mcast_frames;
139e239d0c7SGeetha sowjanya 
140e239d0c7SGeetha sowjanya 	stats->tx_bytes = dev_stats->tx_bytes;
141e239d0c7SGeetha sowjanya 	stats->tx_packets = dev_stats->tx_frames;
142e239d0c7SGeetha sowjanya 	stats->tx_dropped = dev_stats->tx_drops;
143e239d0c7SGeetha sowjanya }
1443184fb5bSTomasz Duszynski EXPORT_SYMBOL(otx2_get_stats64);
145e239d0c7SGeetha sowjanya 
14634bfe0ebSSunil Goutham /* Sync MAC address with RVU AF */
otx2_hw_set_mac_addr(struct otx2_nic * pfvf,u8 * mac)14734bfe0ebSSunil Goutham static int otx2_hw_set_mac_addr(struct otx2_nic *pfvf, u8 *mac)
14834bfe0ebSSunil Goutham {
14934bfe0ebSSunil Goutham 	struct nix_set_mac_addr *req;
15034bfe0ebSSunil Goutham 	int err;
15134bfe0ebSSunil Goutham 
1524c3212f5SSunil Goutham 	mutex_lock(&pfvf->mbox.lock);
15334bfe0ebSSunil Goutham 	req = otx2_mbox_alloc_msg_nix_set_mac_addr(&pfvf->mbox);
15434bfe0ebSSunil Goutham 	if (!req) {
1554c3212f5SSunil Goutham 		mutex_unlock(&pfvf->mbox.lock);
15634bfe0ebSSunil Goutham 		return -ENOMEM;
15734bfe0ebSSunil Goutham 	}
15834bfe0ebSSunil Goutham 
15934bfe0ebSSunil Goutham 	ether_addr_copy(req->mac_addr, mac);
16034bfe0ebSSunil Goutham 
16134bfe0ebSSunil Goutham 	err = otx2_sync_mbox_msg(&pfvf->mbox);
1624c3212f5SSunil Goutham 	mutex_unlock(&pfvf->mbox.lock);
16334bfe0ebSSunil Goutham 	return err;
16434bfe0ebSSunil Goutham }
16534bfe0ebSSunil Goutham 
otx2_hw_get_mac_addr(struct otx2_nic * pfvf,struct net_device * netdev)16634bfe0ebSSunil Goutham static int otx2_hw_get_mac_addr(struct otx2_nic *pfvf,
16734bfe0ebSSunil Goutham 				struct net_device *netdev)
16834bfe0ebSSunil Goutham {
16934bfe0ebSSunil Goutham 	struct nix_get_mac_addr_rsp *rsp;
17034bfe0ebSSunil Goutham 	struct mbox_msghdr *msghdr;
17134bfe0ebSSunil Goutham 	struct msg_req *req;
17234bfe0ebSSunil Goutham 	int err;
17334bfe0ebSSunil Goutham 
1744c3212f5SSunil Goutham 	mutex_lock(&pfvf->mbox.lock);
17534bfe0ebSSunil Goutham 	req = otx2_mbox_alloc_msg_nix_get_mac_addr(&pfvf->mbox);
17634bfe0ebSSunil Goutham 	if (!req) {
1774c3212f5SSunil Goutham 		mutex_unlock(&pfvf->mbox.lock);
17834bfe0ebSSunil Goutham 		return -ENOMEM;
17934bfe0ebSSunil Goutham 	}
18034bfe0ebSSunil Goutham 
18134bfe0ebSSunil Goutham 	err = otx2_sync_mbox_msg(&pfvf->mbox);
18234bfe0ebSSunil Goutham 	if (err) {
1834c3212f5SSunil Goutham 		mutex_unlock(&pfvf->mbox.lock);
18434bfe0ebSSunil Goutham 		return err;
18534bfe0ebSSunil Goutham 	}
18634bfe0ebSSunil Goutham 
18734bfe0ebSSunil Goutham 	msghdr = otx2_mbox_get_rsp(&pfvf->mbox.mbox, 0, &req->hdr);
18808ff7818SDan Carpenter 	if (IS_ERR(msghdr)) {
1894c3212f5SSunil Goutham 		mutex_unlock(&pfvf->mbox.lock);
19008ff7818SDan Carpenter 		return PTR_ERR(msghdr);
19134bfe0ebSSunil Goutham 	}
19234bfe0ebSSunil Goutham 	rsp = (struct nix_get_mac_addr_rsp *)msghdr;
193f3956ebbSJakub Kicinski 	eth_hw_addr_set(netdev, rsp->mac_addr);
1944c3212f5SSunil Goutham 	mutex_unlock(&pfvf->mbox.lock);
19534bfe0ebSSunil Goutham 
19634bfe0ebSSunil Goutham 	return 0;
19734bfe0ebSSunil Goutham }
19834bfe0ebSSunil Goutham 
otx2_set_mac_address(struct net_device * netdev,void * p)19934bfe0ebSSunil Goutham int otx2_set_mac_address(struct net_device *netdev, void *p)
20034bfe0ebSSunil Goutham {
20134bfe0ebSSunil Goutham 	struct otx2_nic *pfvf = netdev_priv(netdev);
20234bfe0ebSSunil Goutham 	struct sockaddr *addr = p;
20334bfe0ebSSunil Goutham 
20434bfe0ebSSunil Goutham 	if (!is_valid_ether_addr(addr->sa_data))
20534bfe0ebSSunil Goutham 		return -EADDRNOTAVAIL;
20634bfe0ebSSunil Goutham 
207fd9d7859SHariprasad Kelam 	if (!otx2_hw_set_mac_addr(pfvf, addr->sa_data)) {
208a05e4c0aSJakub Kicinski 		eth_hw_addr_set(netdev, addr->sa_data);
209fd9d7859SHariprasad Kelam 		/* update dmac field in vlan offload rule */
21005209e35SSunil Goutham 		if (netif_running(netdev) &&
21105209e35SSunil Goutham 		    pfvf->flags & OTX2_FLAG_RX_VLAN_SUPPORT)
212fd9d7859SHariprasad Kelam 			otx2_install_rxvlan_offload_flow(pfvf);
21379d2be38SHariprasad Kelam 		/* update dmac address in ntuple and DMAC filter list */
21479d2be38SHariprasad Kelam 		if (pfvf->flags & OTX2_FLAG_DMACFLTR_SUPPORT)
21579d2be38SHariprasad Kelam 			otx2_dmacflt_update_pfmac_flow(pfvf);
216fd9d7859SHariprasad Kelam 	} else {
21734bfe0ebSSunil Goutham 		return -EPERM;
218fd9d7859SHariprasad Kelam 	}
21934bfe0ebSSunil Goutham 
22034bfe0ebSSunil Goutham 	return 0;
22134bfe0ebSSunil Goutham }
2223184fb5bSTomasz Duszynski EXPORT_SYMBOL(otx2_set_mac_address);
22334bfe0ebSSunil Goutham 
otx2_hw_set_mtu(struct otx2_nic * pfvf,int mtu)22434bfe0ebSSunil Goutham int otx2_hw_set_mtu(struct otx2_nic *pfvf, int mtu)
22534bfe0ebSSunil Goutham {
22634bfe0ebSSunil Goutham 	struct nix_frs_cfg *req;
227a989eb66SSubbaraya Sundeep 	u16 maxlen;
22834bfe0ebSSunil Goutham 	int err;
22934bfe0ebSSunil Goutham 
230a989eb66SSubbaraya Sundeep 	maxlen = otx2_get_max_mtu(pfvf) + OTX2_ETH_HLEN + OTX2_HW_TIMESTAMP_LEN;
231a989eb66SSubbaraya Sundeep 
2324c3212f5SSunil Goutham 	mutex_lock(&pfvf->mbox.lock);
23334bfe0ebSSunil Goutham 	req = otx2_mbox_alloc_msg_nix_set_hw_frs(&pfvf->mbox);
23434bfe0ebSSunil Goutham 	if (!req) {
2354c3212f5SSunil Goutham 		mutex_unlock(&pfvf->mbox.lock);
23634bfe0ebSSunil Goutham 		return -ENOMEM;
23734bfe0ebSSunil Goutham 	}
23834bfe0ebSSunil Goutham 
2390182d078SSubbaraya Sundeep 	req->maxlen = pfvf->netdev->mtu + OTX2_ETH_HLEN + OTX2_HW_TIMESTAMP_LEN;
24034bfe0ebSSunil Goutham 
241a989eb66SSubbaraya Sundeep 	/* Use max receive length supported by hardware for loopback devices */
242a989eb66SSubbaraya Sundeep 	if (is_otx2_lbkvf(pfvf->pdev))
243a989eb66SSubbaraya Sundeep 		req->maxlen = maxlen;
244a989eb66SSubbaraya Sundeep 
24534bfe0ebSSunil Goutham 	err = otx2_sync_mbox_msg(&pfvf->mbox);
2464c3212f5SSunil Goutham 	mutex_unlock(&pfvf->mbox.lock);
24734bfe0ebSSunil Goutham 	return err;
24834bfe0ebSSunil Goutham }
24934bfe0ebSSunil Goutham 
otx2_config_pause_frm(struct otx2_nic * pfvf)25075f36270SGeetha sowjanya int otx2_config_pause_frm(struct otx2_nic *pfvf)
25175f36270SGeetha sowjanya {
25275f36270SGeetha sowjanya 	struct cgx_pause_frm_cfg *req;
25375f36270SGeetha sowjanya 	int err;
25475f36270SGeetha sowjanya 
2553184fb5bSTomasz Duszynski 	if (is_otx2_lbkvf(pfvf->pdev))
2563184fb5bSTomasz Duszynski 		return 0;
2573184fb5bSTomasz Duszynski 
2584c3212f5SSunil Goutham 	mutex_lock(&pfvf->mbox.lock);
25975f36270SGeetha sowjanya 	req = otx2_mbox_alloc_msg_cgx_cfg_pause_frm(&pfvf->mbox);
2608a765471SDan Carpenter 	if (!req) {
2618a765471SDan Carpenter 		err = -ENOMEM;
2628a765471SDan Carpenter 		goto unlock;
2638a765471SDan Carpenter 	}
26475f36270SGeetha sowjanya 
26575f36270SGeetha sowjanya 	req->rx_pause = !!(pfvf->flags & OTX2_FLAG_RX_PAUSE_ENABLED);
26675f36270SGeetha sowjanya 	req->tx_pause = !!(pfvf->flags & OTX2_FLAG_TX_PAUSE_ENABLED);
26775f36270SGeetha sowjanya 	req->set = 1;
26875f36270SGeetha sowjanya 
26975f36270SGeetha sowjanya 	err = otx2_sync_mbox_msg(&pfvf->mbox);
2708a765471SDan Carpenter unlock:
2714c3212f5SSunil Goutham 	mutex_unlock(&pfvf->mbox.lock);
27275f36270SGeetha sowjanya 	return err;
27375f36270SGeetha sowjanya }
274d957b51fSHariprasad Kelam EXPORT_SYMBOL(otx2_config_pause_frm);
27575f36270SGeetha sowjanya 
otx2_set_flowkey_cfg(struct otx2_nic * pfvf)2766e92d71bSSunil Goutham int otx2_set_flowkey_cfg(struct otx2_nic *pfvf)
27785069e95SSunil Goutham {
27885069e95SSunil Goutham 	struct otx2_rss_info *rss = &pfvf->hw.rss_info;
279e7938365SSunil Goutham 	struct nix_rss_flowkey_cfg_rsp *rsp;
28085069e95SSunil Goutham 	struct nix_rss_flowkey_cfg *req;
28185069e95SSunil Goutham 	int err;
28285069e95SSunil Goutham 
2834c3212f5SSunil Goutham 	mutex_lock(&pfvf->mbox.lock);
28485069e95SSunil Goutham 	req = otx2_mbox_alloc_msg_nix_rss_flowkey_cfg(&pfvf->mbox);
28585069e95SSunil Goutham 	if (!req) {
2864c3212f5SSunil Goutham 		mutex_unlock(&pfvf->mbox.lock);
28785069e95SSunil Goutham 		return -ENOMEM;
28885069e95SSunil Goutham 	}
28985069e95SSunil Goutham 	req->mcam_index = -1; /* Default or reserved index */
29085069e95SSunil Goutham 	req->flowkey_cfg = rss->flowkey_cfg;
29185069e95SSunil Goutham 	req->group = DEFAULT_RSS_CONTEXT_GROUP;
29285069e95SSunil Goutham 
29385069e95SSunil Goutham 	err = otx2_sync_mbox_msg(&pfvf->mbox);
294e7938365SSunil Goutham 	if (err)
295e7938365SSunil Goutham 		goto fail;
296e7938365SSunil Goutham 
297e7938365SSunil Goutham 	rsp = (struct nix_rss_flowkey_cfg_rsp *)
298e7938365SSunil Goutham 			otx2_mbox_get_rsp(&pfvf->mbox.mbox, 0, &req->hdr);
2995e8243e6SYang Yingliang 	if (IS_ERR(rsp)) {
3005e8243e6SYang Yingliang 		err = PTR_ERR(rsp);
301e7938365SSunil Goutham 		goto fail;
3025e8243e6SYang Yingliang 	}
303e7938365SSunil Goutham 
304e7938365SSunil Goutham 	pfvf->hw.flowkey_alg_idx = rsp->alg_idx;
305e7938365SSunil Goutham fail:
3064c3212f5SSunil Goutham 	mutex_unlock(&pfvf->mbox.lock);
30785069e95SSunil Goutham 	return err;
30885069e95SSunil Goutham }
30985069e95SSunil Goutham 
otx2_set_rss_table(struct otx2_nic * pfvf,int ctx_id)31081a43620SGeetha sowjanya int otx2_set_rss_table(struct otx2_nic *pfvf, int ctx_id)
31185069e95SSunil Goutham {
31285069e95SSunil Goutham 	struct otx2_rss_info *rss = &pfvf->hw.rss_info;
31381a43620SGeetha sowjanya 	const int index = rss->rss_size * ctx_id;
31485069e95SSunil Goutham 	struct mbox *mbox = &pfvf->mbox;
31581a43620SGeetha sowjanya 	struct otx2_rss_ctx *rss_ctx;
31685069e95SSunil Goutham 	struct nix_aq_enq_req *aq;
31785069e95SSunil Goutham 	int idx, err;
31885069e95SSunil Goutham 
3194c3212f5SSunil Goutham 	mutex_lock(&mbox->lock);
32081a43620SGeetha sowjanya 	rss_ctx = rss->rss_ctx[ctx_id];
32185069e95SSunil Goutham 	/* Get memory to put this msg */
32285069e95SSunil Goutham 	for (idx = 0; idx < rss->rss_size; idx++) {
32385069e95SSunil Goutham 		aq = otx2_mbox_alloc_msg_nix_aq_enq(mbox);
32485069e95SSunil Goutham 		if (!aq) {
32585069e95SSunil Goutham 			/* The shared memory buffer can be full.
32685069e95SSunil Goutham 			 * Flush it and retry
32785069e95SSunil Goutham 			 */
32885069e95SSunil Goutham 			err = otx2_sync_mbox_msg(mbox);
32985069e95SSunil Goutham 			if (err) {
3304c3212f5SSunil Goutham 				mutex_unlock(&mbox->lock);
33185069e95SSunil Goutham 				return err;
33285069e95SSunil Goutham 			}
33385069e95SSunil Goutham 			aq = otx2_mbox_alloc_msg_nix_aq_enq(mbox);
33485069e95SSunil Goutham 			if (!aq) {
3354c3212f5SSunil Goutham 				mutex_unlock(&mbox->lock);
33685069e95SSunil Goutham 				return -ENOMEM;
33785069e95SSunil Goutham 			}
33885069e95SSunil Goutham 		}
33985069e95SSunil Goutham 
34081a43620SGeetha sowjanya 		aq->rss.rq = rss_ctx->ind_tbl[idx];
34185069e95SSunil Goutham 
34285069e95SSunil Goutham 		/* Fill AQ info */
34381a43620SGeetha sowjanya 		aq->qidx = index + idx;
34485069e95SSunil Goutham 		aq->ctype = NIX_AQ_CTYPE_RSS;
34585069e95SSunil Goutham 		aq->op = NIX_AQ_INSTOP_INIT;
34685069e95SSunil Goutham 	}
34785069e95SSunil Goutham 	err = otx2_sync_mbox_msg(mbox);
3484c3212f5SSunil Goutham 	mutex_unlock(&mbox->lock);
34985069e95SSunil Goutham 	return err;
35085069e95SSunil Goutham }
35185069e95SSunil Goutham 
otx2_set_rss_key(struct otx2_nic * pfvf)3526e92d71bSSunil Goutham void otx2_set_rss_key(struct otx2_nic *pfvf)
35385069e95SSunil Goutham {
35485069e95SSunil Goutham 	struct otx2_rss_info *rss = &pfvf->hw.rss_info;
35585069e95SSunil Goutham 	u64 *key = (u64 *)&rss->key[4];
35685069e95SSunil Goutham 	int idx;
35785069e95SSunil Goutham 
35885069e95SSunil Goutham 	/* 352bit or 44byte key needs to be configured as below
35985069e95SSunil Goutham 	 * NIX_LF_RX_SECRETX0 = key<351:288>
36085069e95SSunil Goutham 	 * NIX_LF_RX_SECRETX1 = key<287:224>
36185069e95SSunil Goutham 	 * NIX_LF_RX_SECRETX2 = key<223:160>
36285069e95SSunil Goutham 	 * NIX_LF_RX_SECRETX3 = key<159:96>
36385069e95SSunil Goutham 	 * NIX_LF_RX_SECRETX4 = key<95:32>
36485069e95SSunil Goutham 	 * NIX_LF_RX_SECRETX5<63:32> = key<31:0>
36585069e95SSunil Goutham 	 */
36685069e95SSunil Goutham 	otx2_write64(pfvf, NIX_LF_RX_SECRETX(5),
36785069e95SSunil Goutham 		     (u64)(*((u32 *)&rss->key)) << 32);
36885069e95SSunil Goutham 	idx = sizeof(rss->key) / sizeof(u64);
36985069e95SSunil Goutham 	while (idx > 0) {
37085069e95SSunil Goutham 		idx--;
37185069e95SSunil Goutham 		otx2_write64(pfvf, NIX_LF_RX_SECRETX(idx), *key++);
37285069e95SSunil Goutham 	}
37385069e95SSunil Goutham }
37485069e95SSunil Goutham 
otx2_rss_init(struct otx2_nic * pfvf)37585069e95SSunil Goutham int otx2_rss_init(struct otx2_nic *pfvf)
37685069e95SSunil Goutham {
37785069e95SSunil Goutham 	struct otx2_rss_info *rss = &pfvf->hw.rss_info;
37881a43620SGeetha sowjanya 	struct otx2_rss_ctx *rss_ctx;
37985069e95SSunil Goutham 	int idx, ret = 0;
38085069e95SSunil Goutham 
38181a43620SGeetha sowjanya 	rss->rss_size = sizeof(*rss->rss_ctx[DEFAULT_RSS_CONTEXT_GROUP]);
38285069e95SSunil Goutham 
38385069e95SSunil Goutham 	/* Init RSS key if it is not setup already */
38485069e95SSunil Goutham 	if (!rss->enable)
38585069e95SSunil Goutham 		netdev_rss_key_fill(rss->key, sizeof(rss->key));
38685069e95SSunil Goutham 	otx2_set_rss_key(pfvf);
38785069e95SSunil Goutham 
38885069e95SSunil Goutham 	if (!netif_is_rxfh_configured(pfvf->netdev)) {
38981a43620SGeetha sowjanya 		/* Set RSS group 0 as default indirection table */
39081a43620SGeetha sowjanya 		rss->rss_ctx[DEFAULT_RSS_CONTEXT_GROUP] = kzalloc(rss->rss_size,
39181a43620SGeetha sowjanya 								  GFP_KERNEL);
39281a43620SGeetha sowjanya 		if (!rss->rss_ctx[DEFAULT_RSS_CONTEXT_GROUP])
39381a43620SGeetha sowjanya 			return -ENOMEM;
39481a43620SGeetha sowjanya 
39581a43620SGeetha sowjanya 		rss_ctx = rss->rss_ctx[DEFAULT_RSS_CONTEXT_GROUP];
39685069e95SSunil Goutham 		for (idx = 0; idx < rss->rss_size; idx++)
39781a43620SGeetha sowjanya 			rss_ctx->ind_tbl[idx] =
39885069e95SSunil Goutham 				ethtool_rxfh_indir_default(idx,
39985069e95SSunil Goutham 							   pfvf->hw.rx_queues);
40085069e95SSunil Goutham 	}
40181a43620SGeetha sowjanya 	ret = otx2_set_rss_table(pfvf, DEFAULT_RSS_CONTEXT_GROUP);
40285069e95SSunil Goutham 	if (ret)
40385069e95SSunil Goutham 		return ret;
40485069e95SSunil Goutham 
40585069e95SSunil Goutham 	/* Flowkey or hash config to be used for generating flow tag */
40685069e95SSunil Goutham 	rss->flowkey_cfg = rss->enable ? rss->flowkey_cfg :
40785069e95SSunil Goutham 			   NIX_FLOW_KEY_TYPE_IPV4 | NIX_FLOW_KEY_TYPE_IPV6 |
40885069e95SSunil Goutham 			   NIX_FLOW_KEY_TYPE_TCP | NIX_FLOW_KEY_TYPE_UDP |
409f9e425e9SGeorge Cherian 			   NIX_FLOW_KEY_TYPE_SCTP | NIX_FLOW_KEY_TYPE_VLAN |
410f9e425e9SGeorge Cherian 			   NIX_FLOW_KEY_TYPE_IPV4_PROTO;
41185069e95SSunil Goutham 
41285069e95SSunil Goutham 	ret = otx2_set_flowkey_cfg(pfvf);
41385069e95SSunil Goutham 	if (ret)
41485069e95SSunil Goutham 		return ret;
41585069e95SSunil Goutham 
41685069e95SSunil Goutham 	rss->enable = true;
41785069e95SSunil Goutham 	return 0;
41885069e95SSunil Goutham }
41985069e95SSunil Goutham 
420dc1a9bf2SSunil Goutham /* Setup UDP segmentation algorithm in HW */
otx2_setup_udp_segmentation(struct nix_lso_format_cfg * lso,bool v4)421dc1a9bf2SSunil Goutham static void otx2_setup_udp_segmentation(struct nix_lso_format_cfg *lso, bool v4)
422dc1a9bf2SSunil Goutham {
423dc1a9bf2SSunil Goutham 	struct nix_lso_format *field;
424dc1a9bf2SSunil Goutham 
425dc1a9bf2SSunil Goutham 	field = (struct nix_lso_format *)&lso->fields[0];
426dc1a9bf2SSunil Goutham 	lso->field_mask = GENMASK(18, 0);
427dc1a9bf2SSunil Goutham 
428dc1a9bf2SSunil Goutham 	/* IP's Length field */
429dc1a9bf2SSunil Goutham 	field->layer = NIX_TXLAYER_OL3;
430dc1a9bf2SSunil Goutham 	/* In ipv4, length field is at offset 2 bytes, for ipv6 it's 4 */
431dc1a9bf2SSunil Goutham 	field->offset = v4 ? 2 : 4;
432dc1a9bf2SSunil Goutham 	field->sizem1 = 1; /* i.e 2 bytes */
433dc1a9bf2SSunil Goutham 	field->alg = NIX_LSOALG_ADD_PAYLEN;
434dc1a9bf2SSunil Goutham 	field++;
435dc1a9bf2SSunil Goutham 
436dc1a9bf2SSunil Goutham 	/* No ID field in IPv6 header */
437dc1a9bf2SSunil Goutham 	if (v4) {
438dc1a9bf2SSunil Goutham 		/* Increment IPID */
439dc1a9bf2SSunil Goutham 		field->layer = NIX_TXLAYER_OL3;
440dc1a9bf2SSunil Goutham 		field->offset = 4;
441dc1a9bf2SSunil Goutham 		field->sizem1 = 1; /* i.e 2 bytes */
442dc1a9bf2SSunil Goutham 		field->alg = NIX_LSOALG_ADD_SEGNUM;
443dc1a9bf2SSunil Goutham 		field++;
444dc1a9bf2SSunil Goutham 	}
445dc1a9bf2SSunil Goutham 
446dc1a9bf2SSunil Goutham 	/* Update length in UDP header */
447dc1a9bf2SSunil Goutham 	field->layer = NIX_TXLAYER_OL4;
448dc1a9bf2SSunil Goutham 	field->offset = 4;
449dc1a9bf2SSunil Goutham 	field->sizem1 = 1;
450dc1a9bf2SSunil Goutham 	field->alg = NIX_LSOALG_ADD_PAYLEN;
451dc1a9bf2SSunil Goutham }
452dc1a9bf2SSunil Goutham 
453dc1a9bf2SSunil Goutham /* Setup segmentation algorithms in HW and retrieve algorithm index */
otx2_setup_segmentation(struct otx2_nic * pfvf)454dc1a9bf2SSunil Goutham void otx2_setup_segmentation(struct otx2_nic *pfvf)
455dc1a9bf2SSunil Goutham {
456dc1a9bf2SSunil Goutham 	struct nix_lso_format_cfg_rsp *rsp;
457dc1a9bf2SSunil Goutham 	struct nix_lso_format_cfg *lso;
458dc1a9bf2SSunil Goutham 	struct otx2_hw *hw = &pfvf->hw;
459dc1a9bf2SSunil Goutham 	int err;
460dc1a9bf2SSunil Goutham 
461dc1a9bf2SSunil Goutham 	mutex_lock(&pfvf->mbox.lock);
462dc1a9bf2SSunil Goutham 
463dc1a9bf2SSunil Goutham 	/* UDPv4 segmentation */
464dc1a9bf2SSunil Goutham 	lso = otx2_mbox_alloc_msg_nix_lso_format_cfg(&pfvf->mbox);
465dc1a9bf2SSunil Goutham 	if (!lso)
466dc1a9bf2SSunil Goutham 		goto fail;
467dc1a9bf2SSunil Goutham 
468dc1a9bf2SSunil Goutham 	/* Setup UDP/IP header fields that HW should update per segment */
469dc1a9bf2SSunil Goutham 	otx2_setup_udp_segmentation(lso, true);
470dc1a9bf2SSunil Goutham 
471dc1a9bf2SSunil Goutham 	err = otx2_sync_mbox_msg(&pfvf->mbox);
472dc1a9bf2SSunil Goutham 	if (err)
473dc1a9bf2SSunil Goutham 		goto fail;
474dc1a9bf2SSunil Goutham 
475dc1a9bf2SSunil Goutham 	rsp = (struct nix_lso_format_cfg_rsp *)
476dc1a9bf2SSunil Goutham 			otx2_mbox_get_rsp(&pfvf->mbox.mbox, 0, &lso->hdr);
477dc1a9bf2SSunil Goutham 	if (IS_ERR(rsp))
478dc1a9bf2SSunil Goutham 		goto fail;
479dc1a9bf2SSunil Goutham 
480dc1a9bf2SSunil Goutham 	hw->lso_udpv4_idx = rsp->lso_format_idx;
481dc1a9bf2SSunil Goutham 
482dc1a9bf2SSunil Goutham 	/* UDPv6 segmentation */
483dc1a9bf2SSunil Goutham 	lso = otx2_mbox_alloc_msg_nix_lso_format_cfg(&pfvf->mbox);
484dc1a9bf2SSunil Goutham 	if (!lso)
485dc1a9bf2SSunil Goutham 		goto fail;
486dc1a9bf2SSunil Goutham 
487dc1a9bf2SSunil Goutham 	/* Setup UDP/IP header fields that HW should update per segment */
488dc1a9bf2SSunil Goutham 	otx2_setup_udp_segmentation(lso, false);
489dc1a9bf2SSunil Goutham 
490dc1a9bf2SSunil Goutham 	err = otx2_sync_mbox_msg(&pfvf->mbox);
491dc1a9bf2SSunil Goutham 	if (err)
492dc1a9bf2SSunil Goutham 		goto fail;
493dc1a9bf2SSunil Goutham 
494dc1a9bf2SSunil Goutham 	rsp = (struct nix_lso_format_cfg_rsp *)
495dc1a9bf2SSunil Goutham 			otx2_mbox_get_rsp(&pfvf->mbox.mbox, 0, &lso->hdr);
496dc1a9bf2SSunil Goutham 	if (IS_ERR(rsp))
497dc1a9bf2SSunil Goutham 		goto fail;
498dc1a9bf2SSunil Goutham 
499dc1a9bf2SSunil Goutham 	hw->lso_udpv6_idx = rsp->lso_format_idx;
500dc1a9bf2SSunil Goutham 	mutex_unlock(&pfvf->mbox.lock);
501dc1a9bf2SSunil Goutham 	return;
502dc1a9bf2SSunil Goutham fail:
503dc1a9bf2SSunil Goutham 	mutex_unlock(&pfvf->mbox.lock);
504dc1a9bf2SSunil Goutham 	netdev_info(pfvf->netdev,
505dc1a9bf2SSunil Goutham 		    "Failed to get LSO index for UDP GSO offload, disabling\n");
506dc1a9bf2SSunil Goutham 	pfvf->netdev->hw_features &= ~NETIF_F_GSO_UDP_L4;
507dc1a9bf2SSunil Goutham }
508dc1a9bf2SSunil Goutham 
otx2_config_irq_coalescing(struct otx2_nic * pfvf,int qidx)50904a21ef3SSunil Goutham void otx2_config_irq_coalescing(struct otx2_nic *pfvf, int qidx)
51004a21ef3SSunil Goutham {
51104a21ef3SSunil Goutham 	/* Configure CQE interrupt coalescing parameters
51204a21ef3SSunil Goutham 	 *
51304a21ef3SSunil Goutham 	 * HW triggers an irq when ECOUNT > cq_ecount_wait, hence
51404a21ef3SSunil Goutham 	 * set 1 less than cq_ecount_wait. And cq_time_wait is in
51504a21ef3SSunil Goutham 	 * usecs, convert that to 100ns count.
51604a21ef3SSunil Goutham 	 */
51704a21ef3SSunil Goutham 	otx2_write64(pfvf, NIX_LF_CINTX_WAIT(qidx),
51804a21ef3SSunil Goutham 		     ((u64)(pfvf->hw.cq_time_wait * 10) << 48) |
51904a21ef3SSunil Goutham 		     ((u64)pfvf->hw.cq_qcount_wait << 32) |
52004a21ef3SSunil Goutham 		     (pfvf->hw.cq_ecount_wait - 1));
52104a21ef3SSunil Goutham }
52204a21ef3SSunil Goutham 
otx2_alloc_pool_buf(struct otx2_nic * pfvf,struct otx2_pool * pool,dma_addr_t * dma)523b2e3406aSRatheesh Kannoth static int otx2_alloc_pool_buf(struct otx2_nic *pfvf, struct otx2_pool *pool,
524b2e3406aSRatheesh Kannoth 			       dma_addr_t *dma)
525b2e3406aSRatheesh Kannoth {
526b2e3406aSRatheesh Kannoth 	unsigned int offset = 0;
527b2e3406aSRatheesh Kannoth 	struct page *page;
528b2e3406aSRatheesh Kannoth 	size_t sz;
529b2e3406aSRatheesh Kannoth 
530b2e3406aSRatheesh Kannoth 	sz = SKB_DATA_ALIGN(pool->rbsize);
531b2e3406aSRatheesh Kannoth 	sz = ALIGN(sz, OTX2_ALIGN);
532b2e3406aSRatheesh Kannoth 
533b2e3406aSRatheesh Kannoth 	page = page_pool_alloc_frag(pool->page_pool, &offset, sz, GFP_ATOMIC);
534b2e3406aSRatheesh Kannoth 	if (unlikely(!page))
535b2e3406aSRatheesh Kannoth 		return -ENOMEM;
536b2e3406aSRatheesh Kannoth 
537b2e3406aSRatheesh Kannoth 	*dma = page_pool_get_dma_addr(page) + offset;
538b2e3406aSRatheesh Kannoth 	return 0;
539b2e3406aSRatheesh Kannoth }
540b2e3406aSRatheesh Kannoth 
__otx2_alloc_rbuf(struct otx2_nic * pfvf,struct otx2_pool * pool,dma_addr_t * dma)541ab6dddd2SSubbaraya Sundeep static int __otx2_alloc_rbuf(struct otx2_nic *pfvf, struct otx2_pool *pool,
5421fb3ca76SKevin Hao 			     dma_addr_t *dma)
543caa2da34SSunil Goutham {
5447a36e491SKevin Hao 	u8 *buf;
545caa2da34SSunil Goutham 
546b2e3406aSRatheesh Kannoth 	if (pool->page_pool)
547b2e3406aSRatheesh Kannoth 		return otx2_alloc_pool_buf(pfvf, pool, dma);
548b2e3406aSRatheesh Kannoth 
5491b041601SKevin Hao 	buf = napi_alloc_frag_align(pool->rbsize, OTX2_ALIGN);
5507a36e491SKevin Hao 	if (unlikely(!buf))
551caa2da34SSunil Goutham 		return -ENOMEM;
552caa2da34SSunil Goutham 
5531fb3ca76SKevin Hao 	*dma = dma_map_single_attrs(pfvf->dev, buf, pool->rbsize,
5547a36e491SKevin Hao 				    DMA_FROM_DEVICE, DMA_ATTR_SKIP_CPU_SYNC);
5551fb3ca76SKevin Hao 	if (unlikely(dma_mapping_error(pfvf->dev, *dma))) {
5567a36e491SKevin Hao 		page_frag_free(buf);
557caa2da34SSunil Goutham 		return -ENOMEM;
558caa2da34SSunil Goutham 	}
5597a36e491SKevin Hao 
5601fb3ca76SKevin Hao 	return 0;
561caa2da34SSunil Goutham }
562caa2da34SSunil Goutham 
otx2_alloc_rbuf(struct otx2_nic * pfvf,struct otx2_pool * pool,dma_addr_t * dma)563ab6dddd2SSubbaraya Sundeep int otx2_alloc_rbuf(struct otx2_nic *pfvf, struct otx2_pool *pool,
5641fb3ca76SKevin Hao 		    dma_addr_t *dma)
5657a36e491SKevin Hao {
5661fb3ca76SKevin Hao 	int ret;
5677a36e491SKevin Hao 
5687a36e491SKevin Hao 	local_bh_disable();
5691fb3ca76SKevin Hao 	ret = __otx2_alloc_rbuf(pfvf, pool, dma);
5707a36e491SKevin Hao 	local_bh_enable();
5711fb3ca76SKevin Hao 	return ret;
5727a36e491SKevin Hao }
5737a36e491SKevin Hao 
otx2_alloc_buffer(struct otx2_nic * pfvf,struct otx2_cq_queue * cq,dma_addr_t * dma)5744c236d5dSGeetha sowjanya int otx2_alloc_buffer(struct otx2_nic *pfvf, struct otx2_cq_queue *cq,
5754c236d5dSGeetha sowjanya 		      dma_addr_t *dma)
5764c236d5dSGeetha sowjanya {
57788e69af0SRatheesh Kannoth 	if (unlikely(__otx2_alloc_rbuf(pfvf, cq->rbpool, dma)))
5784c236d5dSGeetha sowjanya 		return -ENOMEM;
5794c236d5dSGeetha sowjanya 	return 0;
5804c236d5dSGeetha sowjanya }
5814c236d5dSGeetha sowjanya 
otx2_tx_timeout(struct net_device * netdev,unsigned int txq)5824ff7d148SGeetha sowjanya void otx2_tx_timeout(struct net_device *netdev, unsigned int txq)
5834ff7d148SGeetha sowjanya {
5844ff7d148SGeetha sowjanya 	struct otx2_nic *pfvf = netdev_priv(netdev);
5854ff7d148SGeetha sowjanya 
5864ff7d148SGeetha sowjanya 	schedule_work(&pfvf->reset_task);
5874ff7d148SGeetha sowjanya }
5883184fb5bSTomasz Duszynski EXPORT_SYMBOL(otx2_tx_timeout);
5894ff7d148SGeetha sowjanya 
otx2_get_mac_from_af(struct net_device * netdev)59034bfe0ebSSunil Goutham void otx2_get_mac_from_af(struct net_device *netdev)
59134bfe0ebSSunil Goutham {
59234bfe0ebSSunil Goutham 	struct otx2_nic *pfvf = netdev_priv(netdev);
59334bfe0ebSSunil Goutham 	int err;
59434bfe0ebSSunil Goutham 
59534bfe0ebSSunil Goutham 	err = otx2_hw_get_mac_addr(pfvf, netdev);
59634bfe0ebSSunil Goutham 	if (err)
59734bfe0ebSSunil Goutham 		dev_warn(pfvf->dev, "Failed to read mac from hardware\n");
59834bfe0ebSSunil Goutham 
59934bfe0ebSSunil Goutham 	/* If AF doesn't provide a valid MAC, generate a random one */
60034bfe0ebSSunil Goutham 	if (!is_valid_ether_addr(netdev->dev_addr))
60134bfe0ebSSunil Goutham 		eth_hw_addr_random(netdev);
60234bfe0ebSSunil Goutham }
6033184fb5bSTomasz Duszynski EXPORT_SYMBOL(otx2_get_mac_from_af);
60434bfe0ebSSunil Goutham 
otx2_txschq_config(struct otx2_nic * pfvf,int lvl,int prio,bool txschq_for_pfc)60599c969a8SSuman Ghosh int otx2_txschq_config(struct otx2_nic *pfvf, int lvl, int prio, bool txschq_for_pfc)
606caa2da34SSunil Goutham {
60799c969a8SSuman Ghosh 	u16 (*schq_list)[MAX_TXSCHQ_PER_FUNC];
608caa2da34SSunil Goutham 	struct otx2_hw *hw = &pfvf->hw;
609caa2da34SSunil Goutham 	struct nix_txschq_config *req;
610caa2da34SSunil Goutham 	u64 schq, parent;
611c39830a4SSunil Goutham 	u64 dwrr_val;
612c39830a4SSunil Goutham 
6130182d078SSubbaraya Sundeep 	dwrr_val = mtu_to_dwrr_weight(pfvf, pfvf->tx_max_pktlen);
614caa2da34SSunil Goutham 
615caa2da34SSunil Goutham 	req = otx2_mbox_alloc_msg_nix_txschq_cfg(&pfvf->mbox);
616caa2da34SSunil Goutham 	if (!req)
617caa2da34SSunil Goutham 		return -ENOMEM;
618caa2da34SSunil Goutham 
619caa2da34SSunil Goutham 	req->lvl = lvl;
620caa2da34SSunil Goutham 	req->num_regs = 1;
621caa2da34SSunil Goutham 
62299c969a8SSuman Ghosh 	schq_list = hw->txschq_list;
62399c969a8SSuman Ghosh #ifdef CONFIG_DCB
62499c969a8SSuman Ghosh 	if (txschq_for_pfc)
62599c969a8SSuman Ghosh 		schq_list = pfvf->pfc_schq_list;
62699c969a8SSuman Ghosh #endif
62799c969a8SSuman Ghosh 
62899c969a8SSuman Ghosh 	schq = schq_list[lvl][prio];
629caa2da34SSunil Goutham 	/* Set topology e.t.c configuration */
630caa2da34SSunil Goutham 	if (lvl == NIX_TXSCH_LVL_SMQ) {
631caa2da34SSunil Goutham 		req->reg[0] = NIX_AF_SMQX_CFG(schq);
6320182d078SSubbaraya Sundeep 		req->regval[0] = ((u64)pfvf->tx_max_pktlen << 8) | OTX2_MIN_MTU;
633caa2da34SSunil Goutham 		req->regval[0] |= (0x20ULL << 51) | (0x80ULL << 39) |
634caa2da34SSunil Goutham 				  (0x2ULL << 36);
635bbba125eSSunil Goutham 		/* Set link type for DWRR MTU selection on CN10K silicons */
636bbba125eSSunil Goutham 		if (!is_dev_otx2(pfvf->pdev))
637bbba125eSSunil Goutham 			req->regval[0] |= FIELD_PREP(GENMASK_ULL(58, 57),
638bbba125eSSunil Goutham 						(u64)hw->smq_link_type);
639caa2da34SSunil Goutham 		req->num_regs++;
640caa2da34SSunil Goutham 		/* MDQ config */
64199c969a8SSuman Ghosh 		parent = schq_list[NIX_TXSCH_LVL_TL4][prio];
642caa2da34SSunil Goutham 		req->reg[1] = NIX_AF_MDQX_PARENT(schq);
643caa2da34SSunil Goutham 		req->regval[1] = parent << 16;
644caa2da34SSunil Goutham 		req->num_regs++;
645caa2da34SSunil Goutham 		/* Set DWRR quantum */
646caa2da34SSunil Goutham 		req->reg[2] = NIX_AF_MDQX_SCHEDULE(schq);
647c39830a4SSunil Goutham 		req->regval[2] =  dwrr_val;
648caa2da34SSunil Goutham 	} else if (lvl == NIX_TXSCH_LVL_TL4) {
64999c969a8SSuman Ghosh 		parent = schq_list[NIX_TXSCH_LVL_TL3][prio];
650caa2da34SSunil Goutham 		req->reg[0] = NIX_AF_TL4X_PARENT(schq);
65138608d07SRatheesh Kannoth 		req->regval[0] = (u64)parent << 16;
652caa2da34SSunil Goutham 		req->num_regs++;
653caa2da34SSunil Goutham 		req->reg[1] = NIX_AF_TL4X_SCHEDULE(schq);
654c39830a4SSunil Goutham 		req->regval[1] = dwrr_val;
655caa2da34SSunil Goutham 	} else if (lvl == NIX_TXSCH_LVL_TL3) {
65699c969a8SSuman Ghosh 		parent = schq_list[NIX_TXSCH_LVL_TL2][prio];
657caa2da34SSunil Goutham 		req->reg[0] = NIX_AF_TL3X_PARENT(schq);
65838608d07SRatheesh Kannoth 		req->regval[0] = (u64)parent << 16;
659caa2da34SSunil Goutham 		req->num_regs++;
660caa2da34SSunil Goutham 		req->reg[1] = NIX_AF_TL3X_SCHEDULE(schq);
661c39830a4SSunil Goutham 		req->regval[1] = dwrr_val;
66213c9f4dcSNaveen Mamindlapalli 		if (lvl == hw->txschq_link_cfg_lvl) {
66313c9f4dcSNaveen Mamindlapalli 			req->num_regs++;
66413c9f4dcSNaveen Mamindlapalli 			req->reg[2] = NIX_AF_TL3_TL2X_LINKX_CFG(schq, hw->tx_link);
66599c969a8SSuman Ghosh 			/* Enable this queue and backpressure
66699c969a8SSuman Ghosh 			 * and set relative channel
66799c969a8SSuman Ghosh 			 */
66899c969a8SSuman Ghosh 			req->regval[2] = BIT_ULL(13) | BIT_ULL(12) | prio;
66913c9f4dcSNaveen Mamindlapalli 		}
670caa2da34SSunil Goutham 	} else if (lvl == NIX_TXSCH_LVL_TL2) {
67199c969a8SSuman Ghosh 		parent = schq_list[NIX_TXSCH_LVL_TL1][prio];
672caa2da34SSunil Goutham 		req->reg[0] = NIX_AF_TL2X_PARENT(schq);
67338608d07SRatheesh Kannoth 		req->regval[0] = (u64)parent << 16;
674caa2da34SSunil Goutham 
675caa2da34SSunil Goutham 		req->num_regs++;
676caa2da34SSunil Goutham 		req->reg[1] = NIX_AF_TL2X_SCHEDULE(schq);
67738608d07SRatheesh Kannoth 		req->regval[1] = (u64)hw->txschq_aggr_lvl_rr_prio << 24 | dwrr_val;
678caa2da34SSunil Goutham 
67913c9f4dcSNaveen Mamindlapalli 		if (lvl == hw->txschq_link_cfg_lvl) {
680caa2da34SSunil Goutham 			req->num_regs++;
681039190bbSSubbaraya Sundeep 			req->reg[2] = NIX_AF_TL3_TL2X_LINKX_CFG(schq, hw->tx_link);
68299c969a8SSuman Ghosh 			/* Enable this queue and backpressure
68399c969a8SSuman Ghosh 			 * and set relative channel
68499c969a8SSuman Ghosh 			 */
68599c969a8SSuman Ghosh 			req->regval[2] = BIT_ULL(13) | BIT_ULL(12) | prio;
68613c9f4dcSNaveen Mamindlapalli 		}
687caa2da34SSunil Goutham 	} else if (lvl == NIX_TXSCH_LVL_TL1) {
688caa2da34SSunil Goutham 		/* Default config for TL1.
689caa2da34SSunil Goutham 		 * For VF this is always ignored.
690caa2da34SSunil Goutham 		 */
691caa2da34SSunil Goutham 
692c39830a4SSunil Goutham 		/* On CN10K, if RR_WEIGHT is greater than 16384, HW will
693c39830a4SSunil Goutham 		 * clip it to 16384, so configuring a 24bit max value
694c39830a4SSunil Goutham 		 * will work on both OTx2 and CN10K.
695c39830a4SSunil Goutham 		 */
696caa2da34SSunil Goutham 		req->reg[0] = NIX_AF_TL1X_SCHEDULE(schq);
697caa2da34SSunil Goutham 		req->regval[0] = TXSCH_TL1_DFLT_RR_QTM;
698caa2da34SSunil Goutham 
699caa2da34SSunil Goutham 		req->num_regs++;
700caa2da34SSunil Goutham 		req->reg[1] = NIX_AF_TL1X_TOPOLOGY(schq);
70138608d07SRatheesh Kannoth 		req->regval[1] = hw->txschq_aggr_lvl_rr_prio << 1;
702caa2da34SSunil Goutham 
703caa2da34SSunil Goutham 		req->num_regs++;
704caa2da34SSunil Goutham 		req->reg[2] = NIX_AF_TL1X_CIR(schq);
705caa2da34SSunil Goutham 		req->regval[2] = 0;
706caa2da34SSunil Goutham 	}
707caa2da34SSunil Goutham 
708caa2da34SSunil Goutham 	return otx2_sync_mbox_msg(&pfvf->mbox);
709caa2da34SSunil Goutham }
71099c969a8SSuman Ghosh EXPORT_SYMBOL(otx2_txschq_config);
71199c969a8SSuman Ghosh 
otx2_smq_flush(struct otx2_nic * pfvf,int smq)71299c969a8SSuman Ghosh int otx2_smq_flush(struct otx2_nic *pfvf, int smq)
71399c969a8SSuman Ghosh {
71499c969a8SSuman Ghosh 	struct nix_txschq_config *req;
71599c969a8SSuman Ghosh 	int rc;
71699c969a8SSuman Ghosh 
71799c969a8SSuman Ghosh 	mutex_lock(&pfvf->mbox.lock);
71899c969a8SSuman Ghosh 
71999c969a8SSuman Ghosh 	req = otx2_mbox_alloc_msg_nix_txschq_cfg(&pfvf->mbox);
72099c969a8SSuman Ghosh 	if (!req) {
72199c969a8SSuman Ghosh 		mutex_unlock(&pfvf->mbox.lock);
72299c969a8SSuman Ghosh 		return -ENOMEM;
72399c969a8SSuman Ghosh 	}
72499c969a8SSuman Ghosh 
72599c969a8SSuman Ghosh 	req->lvl = NIX_TXSCH_LVL_SMQ;
72699c969a8SSuman Ghosh 	req->reg[0] = NIX_AF_SMQX_CFG(smq);
72799c969a8SSuman Ghosh 	req->regval[0] |= BIT_ULL(49);
72899c969a8SSuman Ghosh 	req->num_regs++;
72999c969a8SSuman Ghosh 
73099c969a8SSuman Ghosh 	rc = otx2_sync_mbox_msg(&pfvf->mbox);
73199c969a8SSuman Ghosh 	mutex_unlock(&pfvf->mbox.lock);
73299c969a8SSuman Ghosh 	return rc;
73399c969a8SSuman Ghosh }
73499c969a8SSuman Ghosh EXPORT_SYMBOL(otx2_smq_flush);
735caa2da34SSunil Goutham 
otx2_txsch_alloc(struct otx2_nic * pfvf)736caa2da34SSunil Goutham int otx2_txsch_alloc(struct otx2_nic *pfvf)
737caa2da34SSunil Goutham {
738caa2da34SSunil Goutham 	struct nix_txsch_alloc_req *req;
7396b4b2dedSHariprasad Kelam 	struct nix_txsch_alloc_rsp *rsp;
7406b4b2dedSHariprasad Kelam 	int lvl, schq, rc;
741caa2da34SSunil Goutham 
742caa2da34SSunil Goutham 	/* Get memory to put this msg */
743caa2da34SSunil Goutham 	req = otx2_mbox_alloc_msg_nix_txsch_alloc(&pfvf->mbox);
744caa2da34SSunil Goutham 	if (!req)
745caa2da34SSunil Goutham 		return -ENOMEM;
746caa2da34SSunil Goutham 
747caa2da34SSunil Goutham 	/* Request one schq per level */
748caa2da34SSunil Goutham 	for (lvl = 0; lvl < NIX_TXSCH_LVL_CNT; lvl++)
749caa2da34SSunil Goutham 		req->schq[lvl] = 1;
7506b4b2dedSHariprasad Kelam 	rc = otx2_sync_mbox_msg(&pfvf->mbox);
7516b4b2dedSHariprasad Kelam 	if (rc)
7526b4b2dedSHariprasad Kelam 		return rc;
753caa2da34SSunil Goutham 
7546b4b2dedSHariprasad Kelam 	rsp = (struct nix_txsch_alloc_rsp *)
7556b4b2dedSHariprasad Kelam 	      otx2_mbox_get_rsp(&pfvf->mbox.mbox, 0, &req->hdr);
7566b4b2dedSHariprasad Kelam 	if (IS_ERR(rsp))
7576b4b2dedSHariprasad Kelam 		return PTR_ERR(rsp);
7586b4b2dedSHariprasad Kelam 
7596b4b2dedSHariprasad Kelam 	/* Setup transmit scheduler list */
7606b4b2dedSHariprasad Kelam 	for (lvl = 0; lvl < NIX_TXSCH_LVL_CNT; lvl++)
7616b4b2dedSHariprasad Kelam 		for (schq = 0; schq < rsp->schq[lvl]; schq++)
7626b4b2dedSHariprasad Kelam 			pfvf->hw.txschq_list[lvl][schq] =
7636b4b2dedSHariprasad Kelam 				rsp->schq_list[lvl][schq];
7646b4b2dedSHariprasad Kelam 
7656b4b2dedSHariprasad Kelam 	pfvf->hw.txschq_link_cfg_lvl = rsp->link_cfg_lvl;
76647a9656fSNaveen Mamindlapalli 	pfvf->hw.txschq_aggr_lvl_rr_prio = rsp->aggr_lvl_rr_prio;
7676b4b2dedSHariprasad Kelam 
7686b4b2dedSHariprasad Kelam 	return 0;
769caa2da34SSunil Goutham }
770caa2da34SSunil Goutham 
otx2_txschq_free_one(struct otx2_nic * pfvf,u16 lvl,u16 schq)7716b4b2dedSHariprasad Kelam void otx2_txschq_free_one(struct otx2_nic *pfvf, u16 lvl, u16 schq)
772caa2da34SSunil Goutham {
773caa2da34SSunil Goutham 	struct nix_txsch_free_req *free_req;
7746b4b2dedSHariprasad Kelam 	int err;
775caa2da34SSunil Goutham 
7764c3212f5SSunil Goutham 	mutex_lock(&pfvf->mbox.lock);
7776b4b2dedSHariprasad Kelam 
778caa2da34SSunil Goutham 	free_req = otx2_mbox_alloc_msg_nix_txsch_free(&pfvf->mbox);
779caa2da34SSunil Goutham 	if (!free_req) {
7804c3212f5SSunil Goutham 		mutex_unlock(&pfvf->mbox.lock);
7816b4b2dedSHariprasad Kelam 		netdev_err(pfvf->netdev,
7826b4b2dedSHariprasad Kelam 			   "Failed alloc txschq free req\n");
7836b4b2dedSHariprasad Kelam 		return;
784caa2da34SSunil Goutham 	}
785caa2da34SSunil Goutham 
7866b4b2dedSHariprasad Kelam 	free_req->schq_lvl = lvl;
7876b4b2dedSHariprasad Kelam 	free_req->schq = schq;
7886b4b2dedSHariprasad Kelam 
789caa2da34SSunil Goutham 	err = otx2_sync_mbox_msg(&pfvf->mbox);
7906b4b2dedSHariprasad Kelam 	if (err) {
7916b4b2dedSHariprasad Kelam 		netdev_err(pfvf->netdev,
7926b4b2dedSHariprasad Kelam 			   "Failed stop txschq %d at level %d\n", schq, lvl);
7936b4b2dedSHariprasad Kelam 	}
7946b4b2dedSHariprasad Kelam 
7954c3212f5SSunil Goutham 	mutex_unlock(&pfvf->mbox.lock);
7966b4b2dedSHariprasad Kelam }
797a9ac2e18SSuman Ghosh EXPORT_SYMBOL(otx2_txschq_free_one);
7986b4b2dedSHariprasad Kelam 
otx2_txschq_stop(struct otx2_nic * pfvf)7996b4b2dedSHariprasad Kelam void otx2_txschq_stop(struct otx2_nic *pfvf)
8006b4b2dedSHariprasad Kelam {
8016b4b2dedSHariprasad Kelam 	int lvl, schq;
8026b4b2dedSHariprasad Kelam 
8036b4b2dedSHariprasad Kelam 	/* free non QOS TLx nodes */
8046b4b2dedSHariprasad Kelam 	for (lvl = 0; lvl < NIX_TXSCH_LVL_CNT; lvl++)
8056b4b2dedSHariprasad Kelam 		otx2_txschq_free_one(pfvf, lvl,
8066b4b2dedSHariprasad Kelam 				     pfvf->hw.txschq_list[lvl][0]);
807caa2da34SSunil Goutham 
808caa2da34SSunil Goutham 	/* Clear the txschq list */
809caa2da34SSunil Goutham 	for (lvl = 0; lvl < NIX_TXSCH_LVL_CNT; lvl++) {
810caa2da34SSunil Goutham 		for (schq = 0; schq < MAX_TXSCHQ_PER_FUNC; schq++)
811caa2da34SSunil Goutham 			pfvf->hw.txschq_list[lvl][schq] = 0;
812caa2da34SSunil Goutham 	}
8136b4b2dedSHariprasad Kelam 
814caa2da34SSunil Goutham }
815caa2da34SSunil Goutham 
otx2_sqb_flush(struct otx2_nic * pfvf)816caa2da34SSunil Goutham void otx2_sqb_flush(struct otx2_nic *pfvf)
817caa2da34SSunil Goutham {
818caa2da34SSunil Goutham 	int qidx, sqe_tail, sqe_head;
819ab6dddd2SSubbaraya Sundeep 	struct otx2_snd_queue *sq;
820caa2da34SSunil Goutham 	u64 incr, *ptr, val;
821caa2da34SSunil Goutham 
822caa2da34SSunil Goutham 	ptr = (u64 *)otx2_get_regaddr(pfvf, NIX_LF_SQ_OP_STATUS);
823ab6dddd2SSubbaraya Sundeep 	for (qidx = 0; qidx < otx2_get_total_tx_queues(pfvf); qidx++) {
824ab6dddd2SSubbaraya Sundeep 		sq = &pfvf->qset.sq[qidx];
825ab6dddd2SSubbaraya Sundeep 		if (!sq->sqb_ptrs)
826ab6dddd2SSubbaraya Sundeep 			continue;
827ab6dddd2SSubbaraya Sundeep 
828caa2da34SSunil Goutham 		incr = (u64)qidx << 32;
829caa2da34SSunil Goutham 		val = otx2_atomic64_add(incr, ptr);
830caa2da34SSunil Goutham 		sqe_head = (val >> 20) & 0x3F;
831caa2da34SSunil Goutham 		sqe_tail = (val >> 28) & 0x3F;
83292662d9fSGeetha sowjanya 		if (sqe_head != sqe_tail)
83392662d9fSGeetha sowjanya 			usleep_range(50, 60);
834caa2da34SSunil Goutham 	}
835caa2da34SSunil Goutham }
836caa2da34SSunil Goutham 
837caa2da34SSunil Goutham /* RED and drop levels of CQ on packet reception.
838caa2da34SSunil Goutham  * For CQ level is measure of emptiness ( 0x0 = full, 255 = empty).
839caa2da34SSunil Goutham  */
840caa2da34SSunil Goutham #define RQ_PASS_LVL_CQ(skid, qsize)	((((skid) + 16) * 256) / (qsize))
841caa2da34SSunil Goutham #define RQ_DROP_LVL_CQ(skid, qsize)	(((skid) * 256) / (qsize))
842caa2da34SSunil Goutham 
843caa2da34SSunil Goutham /* RED and drop levels of AURA for packet reception.
844caa2da34SSunil Goutham  * For AURA level is measure of fullness (0x0 = empty, 255 = full).
845caa2da34SSunil Goutham  * Eg: For RQ length 1K, for pass/drop level 204/230.
846caa2da34SSunil Goutham  * RED accepts pkts if free pointers > 102 & <= 205.
847caa2da34SSunil Goutham  * Drops pkts if free pointers < 102.
848caa2da34SSunil Goutham  */
84975f36270SGeetha sowjanya #define RQ_BP_LVL_AURA   (255 - ((85 * 256) / 100)) /* BP when 85% is full */
850caa2da34SSunil Goutham #define RQ_PASS_LVL_AURA (255 - ((95 * 256) / 100)) /* RED when 95% is full */
851caa2da34SSunil Goutham #define RQ_DROP_LVL_AURA (255 - ((99 * 256) / 100)) /* Drop when 99% is full */
852caa2da34SSunil Goutham 
otx2_rq_init(struct otx2_nic * pfvf,u16 qidx,u16 lpb_aura)853caa2da34SSunil Goutham static int otx2_rq_init(struct otx2_nic *pfvf, u16 qidx, u16 lpb_aura)
854caa2da34SSunil Goutham {
855caa2da34SSunil Goutham 	struct otx2_qset *qset = &pfvf->qset;
856caa2da34SSunil Goutham 	struct nix_aq_enq_req *aq;
857caa2da34SSunil Goutham 
858caa2da34SSunil Goutham 	/* Get memory to put this msg */
859caa2da34SSunil Goutham 	aq = otx2_mbox_alloc_msg_nix_aq_enq(&pfvf->mbox);
860caa2da34SSunil Goutham 	if (!aq)
861caa2da34SSunil Goutham 		return -ENOMEM;
862caa2da34SSunil Goutham 
863caa2da34SSunil Goutham 	aq->rq.cq = qidx;
864caa2da34SSunil Goutham 	aq->rq.ena = 1;
865caa2da34SSunil Goutham 	aq->rq.pb_caching = 1;
866caa2da34SSunil Goutham 	aq->rq.lpb_aura = lpb_aura; /* Use large packet buffer aura */
867caa2da34SSunil Goutham 	aq->rq.lpb_sizem1 = (DMA_BUFFER_LEN(pfvf->rbsize) / 8) - 1;
868caa2da34SSunil Goutham 	aq->rq.xqe_imm_size = 0; /* Copying of packet to CQE not needed */
869caa2da34SSunil Goutham 	aq->rq.flow_tagw = 32; /* Copy full 32bit flow_tag to CQE header */
8704ff7d148SGeetha sowjanya 	aq->rq.qint_idx = 0;
871caa2da34SSunil Goutham 	aq->rq.lpb_drop_ena = 1; /* Enable RED dropping for AURA */
872caa2da34SSunil Goutham 	aq->rq.xqe_drop_ena = 1; /* Enable RED dropping for CQ/SSO */
873caa2da34SSunil Goutham 	aq->rq.xqe_pass = RQ_PASS_LVL_CQ(pfvf->hw.rq_skid, qset->rqe_cnt);
874caa2da34SSunil Goutham 	aq->rq.xqe_drop = RQ_DROP_LVL_CQ(pfvf->hw.rq_skid, qset->rqe_cnt);
875caa2da34SSunil Goutham 	aq->rq.lpb_aura_pass = RQ_PASS_LVL_AURA;
876caa2da34SSunil Goutham 	aq->rq.lpb_aura_drop = RQ_DROP_LVL_AURA;
877caa2da34SSunil Goutham 
878caa2da34SSunil Goutham 	/* Fill AQ info */
879caa2da34SSunil Goutham 	aq->qidx = qidx;
880caa2da34SSunil Goutham 	aq->ctype = NIX_AQ_CTYPE_RQ;
881caa2da34SSunil Goutham 	aq->op = NIX_AQ_INSTOP_INIT;
882caa2da34SSunil Goutham 
883caa2da34SSunil Goutham 	return otx2_sync_mbox_msg(&pfvf->mbox);
884caa2da34SSunil Goutham }
885caa2da34SSunil Goutham 
otx2_sq_aq_init(void * dev,u16 qidx,u16 sqb_aura)8864c236d5dSGeetha sowjanya int otx2_sq_aq_init(void *dev, u16 qidx, u16 sqb_aura)
887d21a8575SGeetha sowjanya {
8884c236d5dSGeetha sowjanya 	struct otx2_nic *pfvf = dev;
8894c236d5dSGeetha sowjanya 	struct otx2_snd_queue *sq;
890d21a8575SGeetha sowjanya 	struct nix_aq_enq_req *aq;
891d21a8575SGeetha sowjanya 
8924c236d5dSGeetha sowjanya 	sq = &pfvf->qset.sq[qidx];
8934c236d5dSGeetha sowjanya 	sq->lmt_addr = (__force u64 *)(pfvf->reg_base + LMT_LF_LMTLINEX(qidx));
894d21a8575SGeetha sowjanya 	/* Get memory to put this msg */
895d21a8575SGeetha sowjanya 	aq = otx2_mbox_alloc_msg_nix_aq_enq(&pfvf->mbox);
896d21a8575SGeetha sowjanya 	if (!aq)
897d21a8575SGeetha sowjanya 		return -ENOMEM;
898d21a8575SGeetha sowjanya 
899d21a8575SGeetha sowjanya 	aq->sq.cq = pfvf->hw.rx_queues + qidx;
900d21a8575SGeetha sowjanya 	aq->sq.max_sqe_size = NIX_MAXSQESZ_W16; /* 128 byte */
901d21a8575SGeetha sowjanya 	aq->sq.cq_ena = 1;
902d21a8575SGeetha sowjanya 	aq->sq.ena = 1;
90399c969a8SSuman Ghosh 	aq->sq.smq = otx2_get_smq_idx(pfvf, qidx);
9040182d078SSubbaraya Sundeep 	aq->sq.smq_rr_quantum = mtu_to_dwrr_weight(pfvf, pfvf->tx_max_pktlen);
905d21a8575SGeetha sowjanya 	aq->sq.default_chan = pfvf->hw.tx_chan_base;
906d21a8575SGeetha sowjanya 	aq->sq.sqe_stype = NIX_STYPE_STF; /* Cache SQB */
907d21a8575SGeetha sowjanya 	aq->sq.sqb_aura = sqb_aura;
908d21a8575SGeetha sowjanya 	aq->sq.sq_int_ena = NIX_SQINT_BITS;
909d21a8575SGeetha sowjanya 	aq->sq.qint_idx = 0;
910d21a8575SGeetha sowjanya 	/* Due pipelining impact minimum 2000 unused SQ CQE's
911d21a8575SGeetha sowjanya 	 * need to maintain to avoid CQ overflow.
912d21a8575SGeetha sowjanya 	 */
913d21a8575SGeetha sowjanya 	aq->sq.cq_limit = ((SEND_CQ_SKID * 256) / (pfvf->qset.sqe_cnt));
914d21a8575SGeetha sowjanya 
915d21a8575SGeetha sowjanya 	/* Fill AQ info */
916d21a8575SGeetha sowjanya 	aq->qidx = qidx;
917d21a8575SGeetha sowjanya 	aq->ctype = NIX_AQ_CTYPE_SQ;
918d21a8575SGeetha sowjanya 	aq->op = NIX_AQ_INSTOP_INIT;
919d21a8575SGeetha sowjanya 
920d21a8575SGeetha sowjanya 	return otx2_sync_mbox_msg(&pfvf->mbox);
921d21a8575SGeetha sowjanya }
922d21a8575SGeetha sowjanya 
otx2_sq_init(struct otx2_nic * pfvf,u16 qidx,u16 sqb_aura)923ab6dddd2SSubbaraya Sundeep int otx2_sq_init(struct otx2_nic *pfvf, u16 qidx, u16 sqb_aura)
924caa2da34SSunil Goutham {
925caa2da34SSunil Goutham 	struct otx2_qset *qset = &pfvf->qset;
926caa2da34SSunil Goutham 	struct otx2_snd_queue *sq;
927caa2da34SSunil Goutham 	struct otx2_pool *pool;
928caa2da34SSunil Goutham 	int err;
929caa2da34SSunil Goutham 
930caa2da34SSunil Goutham 	pool = &pfvf->qset.pool[sqb_aura];
931caa2da34SSunil Goutham 	sq = &qset->sq[qidx];
932caa2da34SSunil Goutham 	sq->sqe_size = NIX_SQESZ_W16 ? 64 : 128;
933caa2da34SSunil Goutham 	sq->sqe_cnt = qset->sqe_cnt;
934caa2da34SSunil Goutham 
935caa2da34SSunil Goutham 	err = qmem_alloc(pfvf->dev, &sq->sqe, 1, sq->sqe_size);
936caa2da34SSunil Goutham 	if (err)
937caa2da34SSunil Goutham 		return err;
938caa2da34SSunil Goutham 
93906059a1aSGeetha sowjanya 	if (qidx < pfvf->hw.tx_queues) {
94086d74760SSunil Goutham 		err = qmem_alloc(pfvf->dev, &sq->tso_hdrs, qset->sqe_cnt,
94186d74760SSunil Goutham 				 TSO_HEADER_SIZE);
94286d74760SSunil Goutham 		if (err)
94386d74760SSunil Goutham 			return err;
94406059a1aSGeetha sowjanya 	}
94586d74760SSunil Goutham 
946caa2da34SSunil Goutham 	sq->sqe_base = sq->sqe->base;
9473ca6c4c8SSunil Goutham 	sq->sg = kcalloc(qset->sqe_cnt, sizeof(struct sg_list), GFP_KERNEL);
9483ca6c4c8SSunil Goutham 	if (!sq->sg)
9493ca6c4c8SSunil Goutham 		return -ENOMEM;
950caa2da34SSunil Goutham 
95106059a1aSGeetha sowjanya 	if (pfvf->ptp && qidx < pfvf->hw.tx_queues) {
952c9c12d33SAleksey Makarov 		err = qmem_alloc(pfvf->dev, &sq->timestamps, qset->sqe_cnt,
953c9c12d33SAleksey Makarov 				 sizeof(*sq->timestamps));
95476ce07ebSZhipeng Lu 		if (err) {
95576ce07ebSZhipeng Lu 			kfree(sq->sg);
95676ce07ebSZhipeng Lu 			sq->sg = NULL;
957c9c12d33SAleksey Makarov 			return err;
958c9c12d33SAleksey Makarov 		}
95976ce07ebSZhipeng Lu 	}
960c9c12d33SAleksey Makarov 
9613ca6c4c8SSunil Goutham 	sq->head = 0;
962f0dfc4c8SRatheesh Kannoth 	sq->cons_head = 0;
963caa2da34SSunil Goutham 	sq->sqe_per_sqb = (pfvf->hw.sqb_size / sq->sqe_size) - 1;
964caa2da34SSunil Goutham 	sq->num_sqbs = (qset->sqe_cnt + sq->sqe_per_sqb) / sq->sqe_per_sqb;
9653ca6c4c8SSunil Goutham 	/* Set SQE threshold to 10% of total SQEs */
9663ca6c4c8SSunil Goutham 	sq->sqe_thresh = ((sq->num_sqbs * sq->sqe_per_sqb) * 10) / 100;
967caa2da34SSunil Goutham 	sq->aura_id = sqb_aura;
968caa2da34SSunil Goutham 	sq->aura_fc_addr = pool->fc_addr->base;
969caa2da34SSunil Goutham 	sq->io_addr = (__force u64)otx2_get_regaddr(pfvf, NIX_LF_OP_SENDX(0));
970caa2da34SSunil Goutham 
971d45d8979SChristina Jacob 	sq->stats.bytes = 0;
972d45d8979SChristina Jacob 	sq->stats.pkts = 0;
973d45d8979SChristina Jacob 
97476ce07ebSZhipeng Lu 	err = pfvf->hw_ops->sq_aq_init(pfvf, qidx, sqb_aura);
97576ce07ebSZhipeng Lu 	if (err) {
97676ce07ebSZhipeng Lu 		kfree(sq->sg);
97776ce07ebSZhipeng Lu 		sq->sg = NULL;
97876ce07ebSZhipeng Lu 		return err;
97976ce07ebSZhipeng Lu 	}
98076ce07ebSZhipeng Lu 
98176ce07ebSZhipeng Lu 	return 0;
982caa2da34SSunil Goutham 
983caa2da34SSunil Goutham }
984caa2da34SSunil Goutham 
otx2_cq_init(struct otx2_nic * pfvf,u16 qidx)985caa2da34SSunil Goutham static int otx2_cq_init(struct otx2_nic *pfvf, u16 qidx)
986caa2da34SSunil Goutham {
987caa2da34SSunil Goutham 	struct otx2_qset *qset = &pfvf->qset;
98806059a1aSGeetha sowjanya 	int err, pool_id, non_xdp_queues;
989caa2da34SSunil Goutham 	struct nix_aq_enq_req *aq;
990caa2da34SSunil Goutham 	struct otx2_cq_queue *cq;
991caa2da34SSunil Goutham 
992caa2da34SSunil Goutham 	cq = &qset->cq[qidx];
993caa2da34SSunil Goutham 	cq->cq_idx = qidx;
99406059a1aSGeetha sowjanya 	non_xdp_queues = pfvf->hw.rx_queues + pfvf->hw.tx_queues;
995caa2da34SSunil Goutham 	if (qidx < pfvf->hw.rx_queues) {
996caa2da34SSunil Goutham 		cq->cq_type = CQ_RX;
997abe02543SSunil Goutham 		cq->cint_idx = qidx;
998caa2da34SSunil Goutham 		cq->cqe_cnt = qset->rqe_cnt;
99906059a1aSGeetha sowjanya 		if (pfvf->xdp_prog)
100006059a1aSGeetha sowjanya 			xdp_rxq_info_reg(&cq->xdp_rxq, pfvf->netdev, qidx, 0);
100106059a1aSGeetha sowjanya 	} else if (qidx < non_xdp_queues) {
1002caa2da34SSunil Goutham 		cq->cq_type = CQ_TX;
10033ca6c4c8SSunil Goutham 		cq->cint_idx = qidx - pfvf->hw.rx_queues;
1004caa2da34SSunil Goutham 		cq->cqe_cnt = qset->sqe_cnt;
100506059a1aSGeetha sowjanya 	} else {
1006ab6dddd2SSubbaraya Sundeep 		if (pfvf->hw.xdp_queues &&
1007ab6dddd2SSubbaraya Sundeep 		    qidx < non_xdp_queues + pfvf->hw.xdp_queues) {
100806059a1aSGeetha sowjanya 			cq->cq_type = CQ_XDP;
100906059a1aSGeetha sowjanya 			cq->cint_idx = qidx - non_xdp_queues;
101006059a1aSGeetha sowjanya 			cq->cqe_cnt = qset->sqe_cnt;
1011ab6dddd2SSubbaraya Sundeep 		} else {
1012ab6dddd2SSubbaraya Sundeep 			cq->cq_type = CQ_QOS;
1013ab6dddd2SSubbaraya Sundeep 			cq->cint_idx = qidx - non_xdp_queues -
1014ab6dddd2SSubbaraya Sundeep 				       pfvf->hw.xdp_queues;
1015ab6dddd2SSubbaraya Sundeep 			cq->cqe_cnt = qset->sqe_cnt;
1016ab6dddd2SSubbaraya Sundeep 		}
1017caa2da34SSunil Goutham 	}
1018caa2da34SSunil Goutham 	cq->cqe_size = pfvf->qset.xqe_size;
1019caa2da34SSunil Goutham 
1020caa2da34SSunil Goutham 	/* Allocate memory for CQEs */
1021caa2da34SSunil Goutham 	err = qmem_alloc(pfvf->dev, &cq->cqe, cq->cqe_cnt, cq->cqe_size);
1022caa2da34SSunil Goutham 	if (err)
1023caa2da34SSunil Goutham 		return err;
1024caa2da34SSunil Goutham 
1025caa2da34SSunil Goutham 	/* Save CQE CPU base for faster reference */
1026caa2da34SSunil Goutham 	cq->cqe_base = cq->cqe->base;
1027caa2da34SSunil Goutham 	/* In case where all RQs auras point to single pool,
1028caa2da34SSunil Goutham 	 * all CQs receive buffer pool also point to same pool.
1029caa2da34SSunil Goutham 	 */
1030caa2da34SSunil Goutham 	pool_id = ((cq->cq_type == CQ_RX) &&
1031caa2da34SSunil Goutham 		   (pfvf->hw.rqpool_cnt != pfvf->hw.rx_queues)) ? 0 : qidx;
1032caa2da34SSunil Goutham 	cq->rbpool = &qset->pool[pool_id];
10334ff7d148SGeetha sowjanya 	cq->refill_task_sched = false;
1034caa2da34SSunil Goutham 
1035caa2da34SSunil Goutham 	/* Get memory to put this msg */
1036caa2da34SSunil Goutham 	aq = otx2_mbox_alloc_msg_nix_aq_enq(&pfvf->mbox);
1037caa2da34SSunil Goutham 	if (!aq)
1038caa2da34SSunil Goutham 		return -ENOMEM;
1039caa2da34SSunil Goutham 
1040caa2da34SSunil Goutham 	aq->cq.ena = 1;
1041caa2da34SSunil Goutham 	aq->cq.qsize = Q_SIZE(cq->cqe_cnt, 4);
1042caa2da34SSunil Goutham 	aq->cq.caching = 1;
1043caa2da34SSunil Goutham 	aq->cq.base = cq->cqe->iova;
1044abe02543SSunil Goutham 	aq->cq.cint_idx = cq->cint_idx;
10454ff7d148SGeetha sowjanya 	aq->cq.cq_err_int_ena = NIX_CQERRINT_BITS;
10464ff7d148SGeetha sowjanya 	aq->cq.qint_idx = 0;
1047caa2da34SSunil Goutham 	aq->cq.avg_level = 255;
1048caa2da34SSunil Goutham 
1049caa2da34SSunil Goutham 	if (qidx < pfvf->hw.rx_queues) {
1050caa2da34SSunil Goutham 		aq->cq.drop = RQ_DROP_LVL_CQ(pfvf->hw.rq_skid, cq->cqe_cnt);
1051caa2da34SSunil Goutham 		aq->cq.drop_ena = 1;
105275f36270SGeetha sowjanya 
10534c85e575SHariprasad Kelam 		if (!is_otx2_lbkvf(pfvf->pdev)) {
105475f36270SGeetha sowjanya 			/* Enable receive CQ backpressure */
105575f36270SGeetha sowjanya 			aq->cq.bp_ena = 1;
10568e675581SHariprasad Kelam #ifdef CONFIG_DCB
10578e675581SHariprasad Kelam 			aq->cq.bpid = pfvf->bpid[pfvf->queue_to_pfc_map[qidx]];
10588e675581SHariprasad Kelam #else
105975f36270SGeetha sowjanya 			aq->cq.bpid = pfvf->bpid[0];
10608e675581SHariprasad Kelam #endif
106175f36270SGeetha sowjanya 
106275f36270SGeetha sowjanya 			/* Set backpressure level is same as cq pass level */
106375f36270SGeetha sowjanya 			aq->cq.bp = RQ_PASS_LVL_CQ(pfvf->hw.rq_skid, qset->rqe_cnt);
1064caa2da34SSunil Goutham 		}
10654c85e575SHariprasad Kelam 	}
1066caa2da34SSunil Goutham 
1067caa2da34SSunil Goutham 	/* Fill AQ info */
1068caa2da34SSunil Goutham 	aq->qidx = qidx;
1069caa2da34SSunil Goutham 	aq->ctype = NIX_AQ_CTYPE_CQ;
1070caa2da34SSunil Goutham 	aq->op = NIX_AQ_INSTOP_INIT;
1071caa2da34SSunil Goutham 
1072caa2da34SSunil Goutham 	return otx2_sync_mbox_msg(&pfvf->mbox);
1073caa2da34SSunil Goutham }
1074caa2da34SSunil Goutham 
otx2_pool_refill_task(struct work_struct * work)10754ff7d148SGeetha sowjanya static void otx2_pool_refill_task(struct work_struct *work)
10764ff7d148SGeetha sowjanya {
10774ff7d148SGeetha sowjanya 	struct otx2_cq_queue *cq;
10784ff7d148SGeetha sowjanya 	struct refill_work *wrk;
10794ff7d148SGeetha sowjanya 	struct otx2_nic *pfvf;
108088e69af0SRatheesh Kannoth 	int qidx;
10814ff7d148SGeetha sowjanya 
10824ff7d148SGeetha sowjanya 	wrk = container_of(work, struct refill_work, pool_refill_work.work);
10834ff7d148SGeetha sowjanya 	pfvf = wrk->pf;
10844ff7d148SGeetha sowjanya 	qidx = wrk - pfvf->refill_wrk;
10854ff7d148SGeetha sowjanya 	cq = &pfvf->qset.cq[qidx];
10864ff7d148SGeetha sowjanya 
108788e69af0SRatheesh Kannoth 	cq->refill_task_sched = false;
10884ff7d148SGeetha sowjanya 
108988e69af0SRatheesh Kannoth 	local_bh_disable();
109088e69af0SRatheesh Kannoth 	napi_schedule(wrk->napi);
109188e69af0SRatheesh Kannoth 	local_bh_enable();
10924ff7d148SGeetha sowjanya }
10934ff7d148SGeetha sowjanya 
otx2_config_nix_queues(struct otx2_nic * pfvf)1094caa2da34SSunil Goutham int otx2_config_nix_queues(struct otx2_nic *pfvf)
1095caa2da34SSunil Goutham {
1096caa2da34SSunil Goutham 	int qidx, err;
1097caa2da34SSunil Goutham 
1098caa2da34SSunil Goutham 	/* Initialize RX queues */
1099caa2da34SSunil Goutham 	for (qidx = 0; qidx < pfvf->hw.rx_queues; qidx++) {
1100caa2da34SSunil Goutham 		u16 lpb_aura = otx2_get_pool_idx(pfvf, AURA_NIX_RQ, qidx);
1101caa2da34SSunil Goutham 
1102caa2da34SSunil Goutham 		err = otx2_rq_init(pfvf, qidx, lpb_aura);
1103caa2da34SSunil Goutham 		if (err)
1104caa2da34SSunil Goutham 			return err;
1105caa2da34SSunil Goutham 	}
1106caa2da34SSunil Goutham 
1107caa2da34SSunil Goutham 	/* Initialize TX queues */
1108508c58f7SHariprasad Kelam 	for (qidx = 0; qidx < pfvf->hw.non_qos_queues; qidx++) {
1109caa2da34SSunil Goutham 		u16 sqb_aura = otx2_get_pool_idx(pfvf, AURA_NIX_SQ, qidx);
1110caa2da34SSunil Goutham 
1111caa2da34SSunil Goutham 		err = otx2_sq_init(pfvf, qidx, sqb_aura);
1112caa2da34SSunil Goutham 		if (err)
1113caa2da34SSunil Goutham 			return err;
1114caa2da34SSunil Goutham 	}
1115caa2da34SSunil Goutham 
1116caa2da34SSunil Goutham 	/* Initialize completion queues */
1117caa2da34SSunil Goutham 	for (qidx = 0; qidx < pfvf->qset.cq_cnt; qidx++) {
1118caa2da34SSunil Goutham 		err = otx2_cq_init(pfvf, qidx);
1119caa2da34SSunil Goutham 		if (err)
1120caa2da34SSunil Goutham 			return err;
1121caa2da34SSunil Goutham 	}
1122caa2da34SSunil Goutham 
1123af3826dbSGeetha sowjanya 	pfvf->cq_op_addr = (__force u64 *)otx2_get_regaddr(pfvf,
1124af3826dbSGeetha sowjanya 							   NIX_LF_CQ_OP_STATUS);
1125af3826dbSGeetha sowjanya 
11264ff7d148SGeetha sowjanya 	/* Initialize work queue for receive buffer refill */
11274ff7d148SGeetha sowjanya 	pfvf->refill_wrk = devm_kcalloc(pfvf->dev, pfvf->qset.cq_cnt,
11284ff7d148SGeetha sowjanya 					sizeof(struct refill_work), GFP_KERNEL);
11294ff7d148SGeetha sowjanya 	if (!pfvf->refill_wrk)
11304ff7d148SGeetha sowjanya 		return -ENOMEM;
11314ff7d148SGeetha sowjanya 
11324ff7d148SGeetha sowjanya 	for (qidx = 0; qidx < pfvf->qset.cq_cnt; qidx++) {
11334ff7d148SGeetha sowjanya 		pfvf->refill_wrk[qidx].pf = pfvf;
11344ff7d148SGeetha sowjanya 		INIT_DELAYED_WORK(&pfvf->refill_wrk[qidx].pool_refill_work,
11354ff7d148SGeetha sowjanya 				  otx2_pool_refill_task);
11364ff7d148SGeetha sowjanya 	}
1137caa2da34SSunil Goutham 	return 0;
1138caa2da34SSunil Goutham }
1139caa2da34SSunil Goutham 
otx2_config_nix(struct otx2_nic * pfvf)114005fcc9e0SSunil Goutham int otx2_config_nix(struct otx2_nic *pfvf)
114105fcc9e0SSunil Goutham {
114205fcc9e0SSunil Goutham 	struct nix_lf_alloc_req  *nixlf;
114305fcc9e0SSunil Goutham 	struct nix_lf_alloc_rsp *rsp;
114405fcc9e0SSunil Goutham 	int err;
114505fcc9e0SSunil Goutham 
114668258596SSubbaraya Sundeep 	pfvf->qset.xqe_size = pfvf->hw.xqe_size;
114705fcc9e0SSunil Goutham 
114805fcc9e0SSunil Goutham 	/* Get memory to put this msg */
114905fcc9e0SSunil Goutham 	nixlf = otx2_mbox_alloc_msg_nix_lf_alloc(&pfvf->mbox);
115005fcc9e0SSunil Goutham 	if (!nixlf)
115105fcc9e0SSunil Goutham 		return -ENOMEM;
115205fcc9e0SSunil Goutham 
115305fcc9e0SSunil Goutham 	/* Set RQ/SQ/CQ counts */
115405fcc9e0SSunil Goutham 	nixlf->rq_cnt = pfvf->hw.rx_queues;
1155ab6dddd2SSubbaraya Sundeep 	nixlf->sq_cnt = otx2_get_total_tx_queues(pfvf);
115605fcc9e0SSunil Goutham 	nixlf->cq_cnt = pfvf->qset.cq_cnt;
115785069e95SSunil Goutham 	nixlf->rss_sz = MAX_RSS_INDIR_TBL_SIZE;
115881a43620SGeetha sowjanya 	nixlf->rss_grps = MAX_RSS_GROUPS;
115968258596SSubbaraya Sundeep 	nixlf->xqe_sz = pfvf->hw.xqe_size == 128 ? NIX_XQESZ_W16 : NIX_XQESZ_W64;
116005fcc9e0SSunil Goutham 	/* We don't know absolute NPA LF idx attached.
116105fcc9e0SSunil Goutham 	 * AF will replace 'RVU_DEFAULT_PF_FUNC' with
116205fcc9e0SSunil Goutham 	 * NPA LF attached to this RVU PF/VF.
116305fcc9e0SSunil Goutham 	 */
116405fcc9e0SSunil Goutham 	nixlf->npa_func = RVU_DEFAULT_PF_FUNC;
116505fcc9e0SSunil Goutham 	/* Disable alignment pad, enable L2 length check,
116605fcc9e0SSunil Goutham 	 * enable L4 TCP/UDP checksum verification.
116705fcc9e0SSunil Goutham 	 */
116805fcc9e0SSunil Goutham 	nixlf->rx_cfg = BIT_ULL(33) | BIT_ULL(35) | BIT_ULL(37);
116905fcc9e0SSunil Goutham 
117005fcc9e0SSunil Goutham 	err = otx2_sync_mbox_msg(&pfvf->mbox);
117105fcc9e0SSunil Goutham 	if (err)
117205fcc9e0SSunil Goutham 		return err;
117305fcc9e0SSunil Goutham 
117405fcc9e0SSunil Goutham 	rsp = (struct nix_lf_alloc_rsp *)otx2_mbox_get_rsp(&pfvf->mbox.mbox, 0,
117505fcc9e0SSunil Goutham 							   &nixlf->hdr);
117605fcc9e0SSunil Goutham 	if (IS_ERR(rsp))
117705fcc9e0SSunil Goutham 		return PTR_ERR(rsp);
117805fcc9e0SSunil Goutham 
117905fcc9e0SSunil Goutham 	if (rsp->qints < 1)
118005fcc9e0SSunil Goutham 		return -ENXIO;
118105fcc9e0SSunil Goutham 
118205fcc9e0SSunil Goutham 	return rsp->hdr.rc;
118305fcc9e0SSunil Goutham }
118405fcc9e0SSunil Goutham 
otx2_sq_free_sqbs(struct otx2_nic * pfvf)1185caa2da34SSunil Goutham void otx2_sq_free_sqbs(struct otx2_nic *pfvf)
1186caa2da34SSunil Goutham {
1187caa2da34SSunil Goutham 	struct otx2_qset *qset = &pfvf->qset;
1188caa2da34SSunil Goutham 	struct otx2_hw *hw = &pfvf->hw;
1189caa2da34SSunil Goutham 	struct otx2_snd_queue *sq;
1190caa2da34SSunil Goutham 	int sqb, qidx;
1191caa2da34SSunil Goutham 	u64 iova, pa;
1192caa2da34SSunil Goutham 
1193ab6dddd2SSubbaraya Sundeep 	for (qidx = 0; qidx < otx2_get_total_tx_queues(pfvf); qidx++) {
1194caa2da34SSunil Goutham 		sq = &qset->sq[qidx];
1195caa2da34SSunil Goutham 		if (!sq->sqb_ptrs)
1196caa2da34SSunil Goutham 			continue;
1197caa2da34SSunil Goutham 		for (sqb = 0; sqb < sq->sqb_count; sqb++) {
1198caa2da34SSunil Goutham 			if (!sq->sqb_ptrs[sqb])
1199caa2da34SSunil Goutham 				continue;
1200caa2da34SSunil Goutham 			iova = sq->sqb_ptrs[sqb];
1201caa2da34SSunil Goutham 			pa = otx2_iova_to_phys(pfvf->iommu_domain, iova);
1202caa2da34SSunil Goutham 			dma_unmap_page_attrs(pfvf->dev, iova, hw->sqb_size,
1203caa2da34SSunil Goutham 					     DMA_FROM_DEVICE,
1204caa2da34SSunil Goutham 					     DMA_ATTR_SKIP_CPU_SYNC);
1205caa2da34SSunil Goutham 			put_page(virt_to_page(phys_to_virt(pa)));
1206caa2da34SSunil Goutham 		}
1207caa2da34SSunil Goutham 		sq->sqb_count = 0;
1208caa2da34SSunil Goutham 	}
1209caa2da34SSunil Goutham }
1210caa2da34SSunil Goutham 
otx2_free_bufs(struct otx2_nic * pfvf,struct otx2_pool * pool,u64 iova,int size)1211b2e3406aSRatheesh Kannoth void otx2_free_bufs(struct otx2_nic *pfvf, struct otx2_pool *pool,
1212b2e3406aSRatheesh Kannoth 		    u64 iova, int size)
1213b2e3406aSRatheesh Kannoth {
1214b2e3406aSRatheesh Kannoth 	struct page *page;
1215b2e3406aSRatheesh Kannoth 	u64 pa;
1216b2e3406aSRatheesh Kannoth 
1217b2e3406aSRatheesh Kannoth 	pa = otx2_iova_to_phys(pfvf->iommu_domain, iova);
1218b2e3406aSRatheesh Kannoth 	page = virt_to_head_page(phys_to_virt(pa));
1219b2e3406aSRatheesh Kannoth 
1220b2e3406aSRatheesh Kannoth 	if (pool->page_pool) {
1221b2e3406aSRatheesh Kannoth 		page_pool_put_full_page(pool->page_pool, page, true);
1222b2e3406aSRatheesh Kannoth 	} else {
1223b2e3406aSRatheesh Kannoth 		dma_unmap_page_attrs(pfvf->dev, iova, size,
1224b2e3406aSRatheesh Kannoth 				     DMA_FROM_DEVICE,
1225b2e3406aSRatheesh Kannoth 				     DMA_ATTR_SKIP_CPU_SYNC);
1226b2e3406aSRatheesh Kannoth 
1227b2e3406aSRatheesh Kannoth 		put_page(page);
1228b2e3406aSRatheesh Kannoth 	}
1229b2e3406aSRatheesh Kannoth }
1230b2e3406aSRatheesh Kannoth 
otx2_free_aura_ptr(struct otx2_nic * pfvf,int type)1231caa2da34SSunil Goutham void otx2_free_aura_ptr(struct otx2_nic *pfvf, int type)
1232caa2da34SSunil Goutham {
1233caa2da34SSunil Goutham 	int pool_id, pool_start = 0, pool_end = 0, size = 0;
1234b2e3406aSRatheesh Kannoth 	struct otx2_pool *pool;
1235b2e3406aSRatheesh Kannoth 	u64 iova;
1236caa2da34SSunil Goutham 
1237caa2da34SSunil Goutham 	if (type == AURA_NIX_SQ) {
1238caa2da34SSunil Goutham 		pool_start = otx2_get_pool_idx(pfvf, type, 0);
1239caa2da34SSunil Goutham 		pool_end =  pool_start + pfvf->hw.sqpool_cnt;
1240caa2da34SSunil Goutham 		size = pfvf->hw.sqb_size;
1241caa2da34SSunil Goutham 	}
1242caa2da34SSunil Goutham 	if (type == AURA_NIX_RQ) {
1243caa2da34SSunil Goutham 		pool_start = otx2_get_pool_idx(pfvf, type, 0);
1244caa2da34SSunil Goutham 		pool_end = pfvf->hw.rqpool_cnt;
1245caa2da34SSunil Goutham 		size = pfvf->rbsize;
1246caa2da34SSunil Goutham 	}
1247caa2da34SSunil Goutham 
1248caa2da34SSunil Goutham 	/* Free SQB and RQB pointers from the aura pool */
1249caa2da34SSunil Goutham 	for (pool_id = pool_start; pool_id < pool_end; pool_id++) {
1250caa2da34SSunil Goutham 		iova = otx2_aura_allocptr(pfvf, pool_id);
1251b2e3406aSRatheesh Kannoth 		pool = &pfvf->qset.pool[pool_id];
1252caa2da34SSunil Goutham 		while (iova) {
1253caa2da34SSunil Goutham 			if (type == AURA_NIX_RQ)
1254caa2da34SSunil Goutham 				iova -= OTX2_HEAD_ROOM;
1255caa2da34SSunil Goutham 
1256b2e3406aSRatheesh Kannoth 			otx2_free_bufs(pfvf, pool, iova, size);
1257b2e3406aSRatheesh Kannoth 
1258caa2da34SSunil Goutham 			iova = otx2_aura_allocptr(pfvf, pool_id);
1259caa2da34SSunil Goutham 		}
1260caa2da34SSunil Goutham 	}
1261caa2da34SSunil Goutham }
1262caa2da34SSunil Goutham 
otx2_aura_pool_free(struct otx2_nic * pfvf)1263caa2da34SSunil Goutham void otx2_aura_pool_free(struct otx2_nic *pfvf)
1264caa2da34SSunil Goutham {
1265caa2da34SSunil Goutham 	struct otx2_pool *pool;
1266caa2da34SSunil Goutham 	int pool_id;
1267caa2da34SSunil Goutham 
1268caa2da34SSunil Goutham 	if (!pfvf->qset.pool)
1269caa2da34SSunil Goutham 		return;
1270caa2da34SSunil Goutham 
1271caa2da34SSunil Goutham 	for (pool_id = 0; pool_id < pfvf->hw.pool_cnt; pool_id++) {
1272caa2da34SSunil Goutham 		pool = &pfvf->qset.pool[pool_id];
1273caa2da34SSunil Goutham 		qmem_free(pfvf->dev, pool->stack);
1274caa2da34SSunil Goutham 		qmem_free(pfvf->dev, pool->fc_addr);
1275b2e3406aSRatheesh Kannoth 		page_pool_destroy(pool->page_pool);
1276b2e3406aSRatheesh Kannoth 		pool->page_pool = NULL;
1277caa2da34SSunil Goutham 	}
1278caa2da34SSunil Goutham 	devm_kfree(pfvf->dev, pfvf->qset.pool);
1279b1bc8457SGeetha sowjanya 	pfvf->qset.pool = NULL;
1280caa2da34SSunil Goutham }
1281caa2da34SSunil Goutham 
otx2_aura_init(struct otx2_nic * pfvf,int aura_id,int pool_id,int numptrs)1282ab6dddd2SSubbaraya Sundeep int otx2_aura_init(struct otx2_nic *pfvf, int aura_id,
1283caa2da34SSunil Goutham 		   int pool_id, int numptrs)
1284caa2da34SSunil Goutham {
1285caa2da34SSunil Goutham 	struct npa_aq_enq_req *aq;
1286caa2da34SSunil Goutham 	struct otx2_pool *pool;
1287caa2da34SSunil Goutham 	int err;
1288caa2da34SSunil Goutham 
1289caa2da34SSunil Goutham 	pool = &pfvf->qset.pool[pool_id];
1290caa2da34SSunil Goutham 
1291caa2da34SSunil Goutham 	/* Allocate memory for HW to update Aura count.
1292caa2da34SSunil Goutham 	 * Alloc one cache line, so that it fits all FC_STYPE modes.
1293caa2da34SSunil Goutham 	 */
1294caa2da34SSunil Goutham 	if (!pool->fc_addr) {
1295caa2da34SSunil Goutham 		err = qmem_alloc(pfvf->dev, &pool->fc_addr, 1, OTX2_ALIGN);
1296caa2da34SSunil Goutham 		if (err)
1297caa2da34SSunil Goutham 			return err;
1298caa2da34SSunil Goutham 	}
1299caa2da34SSunil Goutham 
1300caa2da34SSunil Goutham 	/* Initialize this aura's context via AF */
1301caa2da34SSunil Goutham 	aq = otx2_mbox_alloc_msg_npa_aq_enq(&pfvf->mbox);
1302caa2da34SSunil Goutham 	if (!aq) {
1303caa2da34SSunil Goutham 		/* Shared mbox memory buffer is full, flush it and retry */
1304caa2da34SSunil Goutham 		err = otx2_sync_mbox_msg(&pfvf->mbox);
1305caa2da34SSunil Goutham 		if (err)
1306caa2da34SSunil Goutham 			return err;
1307caa2da34SSunil Goutham 		aq = otx2_mbox_alloc_msg_npa_aq_enq(&pfvf->mbox);
1308caa2da34SSunil Goutham 		if (!aq)
1309caa2da34SSunil Goutham 			return -ENOMEM;
1310caa2da34SSunil Goutham 	}
1311caa2da34SSunil Goutham 
1312caa2da34SSunil Goutham 	aq->aura_id = aura_id;
1313caa2da34SSunil Goutham 	/* Will be filled by AF with correct pool context address */
1314caa2da34SSunil Goutham 	aq->aura.pool_addr = pool_id;
1315caa2da34SSunil Goutham 	aq->aura.pool_caching = 1;
1316caa2da34SSunil Goutham 	aq->aura.shift = ilog2(numptrs) - 8;
1317caa2da34SSunil Goutham 	aq->aura.count = numptrs;
1318caa2da34SSunil Goutham 	aq->aura.limit = numptrs;
1319caa2da34SSunil Goutham 	aq->aura.avg_level = 255;
1320caa2da34SSunil Goutham 	aq->aura.ena = 1;
1321caa2da34SSunil Goutham 	aq->aura.fc_ena = 1;
1322caa2da34SSunil Goutham 	aq->aura.fc_addr = pool->fc_addr->iova;
1323caa2da34SSunil Goutham 	aq->aura.fc_hyst_bits = 0; /* Store count on all updates */
1324caa2da34SSunil Goutham 
132575f36270SGeetha sowjanya 	/* Enable backpressure for RQ aura */
13264c85e575SHariprasad Kelam 	if (aura_id < pfvf->hw.rqpool_cnt && !is_otx2_lbkvf(pfvf->pdev)) {
132775f36270SGeetha sowjanya 		aq->aura.bp_ena = 0;
1328e8fb4df1SSubbaraya Sundeep 		/* If NIX1 LF is attached then specify NIX1_RX.
1329e8fb4df1SSubbaraya Sundeep 		 *
1330e8fb4df1SSubbaraya Sundeep 		 * Below NPA_AURA_S[BP_ENA] is set according to the
1331e8fb4df1SSubbaraya Sundeep 		 * NPA_BPINTF_E enumeration given as:
1332e8fb4df1SSubbaraya Sundeep 		 * 0x0 + a*0x1 where 'a' is 0 for NIX0_RX and 1 for NIX1_RX so
1333e8fb4df1SSubbaraya Sundeep 		 * NIX0_RX is 0x0 + 0*0x1 = 0
1334e8fb4df1SSubbaraya Sundeep 		 * NIX1_RX is 0x0 + 1*0x1 = 1
1335e8fb4df1SSubbaraya Sundeep 		 * But in HRM it is given that
1336e8fb4df1SSubbaraya Sundeep 		 * "NPA_AURA_S[BP_ENA](w1[33:32]) - Enable aura backpressure to
1337e8fb4df1SSubbaraya Sundeep 		 * NIX-RX based on [BP] level. One bit per NIX-RX; index
1338e8fb4df1SSubbaraya Sundeep 		 * enumerated by NPA_BPINTF_E."
1339e8fb4df1SSubbaraya Sundeep 		 */
1340e8fb4df1SSubbaraya Sundeep 		if (pfvf->nix_blkaddr == BLKADDR_NIX1)
1341e8fb4df1SSubbaraya Sundeep 			aq->aura.bp_ena = 1;
13428e675581SHariprasad Kelam #ifdef CONFIG_DCB
13438e675581SHariprasad Kelam 		aq->aura.nix0_bpid = pfvf->bpid[pfvf->queue_to_pfc_map[aura_id]];
13448e675581SHariprasad Kelam #else
134575f36270SGeetha sowjanya 		aq->aura.nix0_bpid = pfvf->bpid[0];
13468e675581SHariprasad Kelam #endif
1347e8fb4df1SSubbaraya Sundeep 
134875f36270SGeetha sowjanya 		/* Set backpressure level for RQ's Aura */
134975f36270SGeetha sowjanya 		aq->aura.bp = RQ_BP_LVL_AURA;
135075f36270SGeetha sowjanya 	}
135175f36270SGeetha sowjanya 
1352caa2da34SSunil Goutham 	/* Fill AQ info */
1353caa2da34SSunil Goutham 	aq->ctype = NPA_AQ_CTYPE_AURA;
1354caa2da34SSunil Goutham 	aq->op = NPA_AQ_INSTOP_INIT;
1355caa2da34SSunil Goutham 
1356caa2da34SSunil Goutham 	return 0;
1357caa2da34SSunil Goutham }
1358caa2da34SSunil Goutham 
otx2_pool_init(struct otx2_nic * pfvf,u16 pool_id,int stack_pages,int numptrs,int buf_size,int type)1359ab6dddd2SSubbaraya Sundeep int otx2_pool_init(struct otx2_nic *pfvf, u16 pool_id,
1360b2e3406aSRatheesh Kannoth 		   int stack_pages, int numptrs, int buf_size, int type)
1361caa2da34SSunil Goutham {
1362b2e3406aSRatheesh Kannoth 	struct page_pool_params pp_params = { 0 };
1363caa2da34SSunil Goutham 	struct npa_aq_enq_req *aq;
1364caa2da34SSunil Goutham 	struct otx2_pool *pool;
1365caa2da34SSunil Goutham 	int err;
1366caa2da34SSunil Goutham 
1367caa2da34SSunil Goutham 	pool = &pfvf->qset.pool[pool_id];
1368caa2da34SSunil Goutham 	/* Alloc memory for stack which is used to store buffer pointers */
1369caa2da34SSunil Goutham 	err = qmem_alloc(pfvf->dev, &pool->stack,
1370caa2da34SSunil Goutham 			 stack_pages, pfvf->hw.stack_pg_bytes);
1371caa2da34SSunil Goutham 	if (err)
1372caa2da34SSunil Goutham 		return err;
1373caa2da34SSunil Goutham 
1374caa2da34SSunil Goutham 	pool->rbsize = buf_size;
1375caa2da34SSunil Goutham 
1376caa2da34SSunil Goutham 	/* Initialize this pool's context via AF */
1377caa2da34SSunil Goutham 	aq = otx2_mbox_alloc_msg_npa_aq_enq(&pfvf->mbox);
1378caa2da34SSunil Goutham 	if (!aq) {
1379caa2da34SSunil Goutham 		/* Shared mbox memory buffer is full, flush it and retry */
1380caa2da34SSunil Goutham 		err = otx2_sync_mbox_msg(&pfvf->mbox);
1381caa2da34SSunil Goutham 		if (err) {
1382caa2da34SSunil Goutham 			qmem_free(pfvf->dev, pool->stack);
1383caa2da34SSunil Goutham 			return err;
1384caa2da34SSunil Goutham 		}
1385caa2da34SSunil Goutham 		aq = otx2_mbox_alloc_msg_npa_aq_enq(&pfvf->mbox);
1386caa2da34SSunil Goutham 		if (!aq) {
1387caa2da34SSunil Goutham 			qmem_free(pfvf->dev, pool->stack);
1388caa2da34SSunil Goutham 			return -ENOMEM;
1389caa2da34SSunil Goutham 		}
1390caa2da34SSunil Goutham 	}
1391caa2da34SSunil Goutham 
1392caa2da34SSunil Goutham 	aq->aura_id = pool_id;
1393caa2da34SSunil Goutham 	aq->pool.stack_base = pool->stack->iova;
1394caa2da34SSunil Goutham 	aq->pool.stack_caching = 1;
1395caa2da34SSunil Goutham 	aq->pool.ena = 1;
1396caa2da34SSunil Goutham 	aq->pool.buf_size = buf_size / 128;
1397caa2da34SSunil Goutham 	aq->pool.stack_max_pages = stack_pages;
1398caa2da34SSunil Goutham 	aq->pool.shift = ilog2(numptrs) - 8;
1399caa2da34SSunil Goutham 	aq->pool.ptr_start = 0;
1400caa2da34SSunil Goutham 	aq->pool.ptr_end = ~0ULL;
1401caa2da34SSunil Goutham 
1402caa2da34SSunil Goutham 	/* Fill AQ info */
1403caa2da34SSunil Goutham 	aq->ctype = NPA_AQ_CTYPE_POOL;
1404caa2da34SSunil Goutham 	aq->op = NPA_AQ_INSTOP_INIT;
1405caa2da34SSunil Goutham 
1406b2e3406aSRatheesh Kannoth 	if (type != AURA_NIX_RQ) {
1407b2e3406aSRatheesh Kannoth 		pool->page_pool = NULL;
1408b2e3406aSRatheesh Kannoth 		return 0;
1409b2e3406aSRatheesh Kannoth 	}
1410b2e3406aSRatheesh Kannoth 
141150e49214SRatheesh Kannoth 	pp_params.order = get_order(buf_size);
1412b2e3406aSRatheesh Kannoth 	pp_params.flags = PP_FLAG_PAGE_FRAG | PP_FLAG_DMA_MAP;
141349fa4b0dSRatheesh Kannoth 	pp_params.pool_size = min(OTX2_PAGE_POOL_SZ, numptrs);
1414b2e3406aSRatheesh Kannoth 	pp_params.nid = NUMA_NO_NODE;
1415b2e3406aSRatheesh Kannoth 	pp_params.dev = pfvf->dev;
1416b2e3406aSRatheesh Kannoth 	pp_params.dma_dir = DMA_FROM_DEVICE;
1417b2e3406aSRatheesh Kannoth 	pool->page_pool = page_pool_create(&pp_params);
1418b2e3406aSRatheesh Kannoth 	if (IS_ERR(pool->page_pool)) {
1419b2e3406aSRatheesh Kannoth 		netdev_err(pfvf->netdev, "Creation of page pool failed\n");
1420b2e3406aSRatheesh Kannoth 		return PTR_ERR(pool->page_pool);
1421b2e3406aSRatheesh Kannoth 	}
1422b2e3406aSRatheesh Kannoth 
1423caa2da34SSunil Goutham 	return 0;
1424caa2da34SSunil Goutham }
1425caa2da34SSunil Goutham 
otx2_sq_aura_pool_init(struct otx2_nic * pfvf)1426caa2da34SSunil Goutham int otx2_sq_aura_pool_init(struct otx2_nic *pfvf)
1427caa2da34SSunil Goutham {
1428caa2da34SSunil Goutham 	int qidx, pool_id, stack_pages, num_sqbs;
1429caa2da34SSunil Goutham 	struct otx2_qset *qset = &pfvf->qset;
1430caa2da34SSunil Goutham 	struct otx2_hw *hw = &pfvf->hw;
1431caa2da34SSunil Goutham 	struct otx2_snd_queue *sq;
1432caa2da34SSunil Goutham 	struct otx2_pool *pool;
14331fb3ca76SKevin Hao 	dma_addr_t bufptr;
1434caa2da34SSunil Goutham 	int err, ptr;
1435caa2da34SSunil Goutham 
1436caa2da34SSunil Goutham 	/* Calculate number of SQBs needed.
1437caa2da34SSunil Goutham 	 *
1438caa2da34SSunil Goutham 	 * For a 128byte SQE, and 4K size SQB, 31 SQEs will fit in one SQB.
1439caa2da34SSunil Goutham 	 * Last SQE is used for pointing to next SQB.
1440caa2da34SSunil Goutham 	 */
1441caa2da34SSunil Goutham 	num_sqbs = (hw->sqb_size / 128) - 1;
1442caa2da34SSunil Goutham 	num_sqbs = (qset->sqe_cnt + num_sqbs) / num_sqbs;
1443caa2da34SSunil Goutham 
1444caa2da34SSunil Goutham 	/* Get no of stack pages needed */
1445caa2da34SSunil Goutham 	stack_pages =
1446caa2da34SSunil Goutham 		(num_sqbs + hw->stack_pg_ptrs - 1) / hw->stack_pg_ptrs;
1447caa2da34SSunil Goutham 
1448508c58f7SHariprasad Kelam 	for (qidx = 0; qidx < hw->non_qos_queues; qidx++) {
1449caa2da34SSunil Goutham 		pool_id = otx2_get_pool_idx(pfvf, AURA_NIX_SQ, qidx);
1450caa2da34SSunil Goutham 		/* Initialize aura context */
1451caa2da34SSunil Goutham 		err = otx2_aura_init(pfvf, pool_id, pool_id, num_sqbs);
1452caa2da34SSunil Goutham 		if (err)
1453caa2da34SSunil Goutham 			goto fail;
1454caa2da34SSunil Goutham 
1455caa2da34SSunil Goutham 		/* Initialize pool context */
1456caa2da34SSunil Goutham 		err = otx2_pool_init(pfvf, pool_id, stack_pages,
1457b2e3406aSRatheesh Kannoth 				     num_sqbs, hw->sqb_size, AURA_NIX_SQ);
1458caa2da34SSunil Goutham 		if (err)
1459caa2da34SSunil Goutham 			goto fail;
1460caa2da34SSunil Goutham 	}
1461caa2da34SSunil Goutham 
1462caa2da34SSunil Goutham 	/* Flush accumulated messages */
1463caa2da34SSunil Goutham 	err = otx2_sync_mbox_msg(&pfvf->mbox);
1464caa2da34SSunil Goutham 	if (err)
1465caa2da34SSunil Goutham 		goto fail;
1466caa2da34SSunil Goutham 
1467caa2da34SSunil Goutham 	/* Allocate pointers and free them to aura/pool */
1468508c58f7SHariprasad Kelam 	for (qidx = 0; qidx < hw->non_qos_queues; qidx++) {
1469caa2da34SSunil Goutham 		pool_id = otx2_get_pool_idx(pfvf, AURA_NIX_SQ, qidx);
1470caa2da34SSunil Goutham 		pool = &pfvf->qset.pool[pool_id];
1471caa2da34SSunil Goutham 
1472caa2da34SSunil Goutham 		sq = &qset->sq[qidx];
1473caa2da34SSunil Goutham 		sq->sqb_count = 0;
1474873b807cSColin Ian King 		sq->sqb_ptrs = kcalloc(num_sqbs, sizeof(*sq->sqb_ptrs), GFP_KERNEL);
14754af1b64fSGeetha sowjanya 		if (!sq->sqb_ptrs) {
14764af1b64fSGeetha sowjanya 			err = -ENOMEM;
14774af1b64fSGeetha sowjanya 			goto err_mem;
14784af1b64fSGeetha sowjanya 		}
1479caa2da34SSunil Goutham 
1480caa2da34SSunil Goutham 		for (ptr = 0; ptr < num_sqbs; ptr++) {
14814af1b64fSGeetha sowjanya 			err = otx2_alloc_rbuf(pfvf, pool, &bufptr);
14824af1b64fSGeetha sowjanya 			if (err)
14834af1b64fSGeetha sowjanya 				goto err_mem;
14844c236d5dSGeetha sowjanya 			pfvf->hw_ops->aura_freeptr(pfvf, pool_id, bufptr);
1485caa2da34SSunil Goutham 			sq->sqb_ptrs[sq->sqb_count++] = (u64)bufptr;
1486caa2da34SSunil Goutham 		}
1487caa2da34SSunil Goutham 	}
1488caa2da34SSunil Goutham 
14894af1b64fSGeetha sowjanya err_mem:
14904af1b64fSGeetha sowjanya 	return err ? -ENOMEM : 0;
14914af1b64fSGeetha sowjanya 
1492caa2da34SSunil Goutham fail:
1493caa2da34SSunil Goutham 	otx2_mbox_reset(&pfvf->mbox.mbox, 0);
1494caa2da34SSunil Goutham 	otx2_aura_pool_free(pfvf);
1495caa2da34SSunil Goutham 	return err;
1496caa2da34SSunil Goutham }
1497caa2da34SSunil Goutham 
otx2_rq_aura_pool_init(struct otx2_nic * pfvf)1498caa2da34SSunil Goutham int otx2_rq_aura_pool_init(struct otx2_nic *pfvf)
1499caa2da34SSunil Goutham {
1500caa2da34SSunil Goutham 	struct otx2_hw *hw = &pfvf->hw;
1501caa2da34SSunil Goutham 	int stack_pages, pool_id, rq;
1502caa2da34SSunil Goutham 	struct otx2_pool *pool;
1503caa2da34SSunil Goutham 	int err, ptr, num_ptrs;
15041fb3ca76SKevin Hao 	dma_addr_t bufptr;
1505caa2da34SSunil Goutham 
1506caa2da34SSunil Goutham 	num_ptrs = pfvf->qset.rqe_cnt;
1507caa2da34SSunil Goutham 
1508caa2da34SSunil Goutham 	stack_pages =
1509caa2da34SSunil Goutham 		(num_ptrs + hw->stack_pg_ptrs - 1) / hw->stack_pg_ptrs;
1510caa2da34SSunil Goutham 
1511caa2da34SSunil Goutham 	for (rq = 0; rq < hw->rx_queues; rq++) {
1512caa2da34SSunil Goutham 		pool_id = otx2_get_pool_idx(pfvf, AURA_NIX_RQ, rq);
1513caa2da34SSunil Goutham 		/* Initialize aura context */
1514caa2da34SSunil Goutham 		err = otx2_aura_init(pfvf, pool_id, pool_id, num_ptrs);
1515caa2da34SSunil Goutham 		if (err)
1516caa2da34SSunil Goutham 			goto fail;
1517caa2da34SSunil Goutham 	}
1518caa2da34SSunil Goutham 	for (pool_id = 0; pool_id < hw->rqpool_cnt; pool_id++) {
1519caa2da34SSunil Goutham 		err = otx2_pool_init(pfvf, pool_id, stack_pages,
1520b2e3406aSRatheesh Kannoth 				     num_ptrs, pfvf->rbsize, AURA_NIX_RQ);
1521caa2da34SSunil Goutham 		if (err)
1522caa2da34SSunil Goutham 			goto fail;
1523caa2da34SSunil Goutham 	}
1524caa2da34SSunil Goutham 
1525caa2da34SSunil Goutham 	/* Flush accumulated messages */
1526caa2da34SSunil Goutham 	err = otx2_sync_mbox_msg(&pfvf->mbox);
1527caa2da34SSunil Goutham 	if (err)
1528caa2da34SSunil Goutham 		goto fail;
1529caa2da34SSunil Goutham 
1530caa2da34SSunil Goutham 	/* Allocate pointers and free them to aura/pool */
1531caa2da34SSunil Goutham 	for (pool_id = 0; pool_id < hw->rqpool_cnt; pool_id++) {
1532caa2da34SSunil Goutham 		pool = &pfvf->qset.pool[pool_id];
1533caa2da34SSunil Goutham 		for (ptr = 0; ptr < num_ptrs; ptr++) {
15344af1b64fSGeetha sowjanya 			err = otx2_alloc_rbuf(pfvf, pool, &bufptr);
15354af1b64fSGeetha sowjanya 			if (err)
153655ba18dcSKevin Hao 				return -ENOMEM;
15374c236d5dSGeetha sowjanya 			pfvf->hw_ops->aura_freeptr(pfvf, pool_id,
1538caa2da34SSunil Goutham 						   bufptr + OTX2_HEAD_ROOM);
1539caa2da34SSunil Goutham 		}
1540caa2da34SSunil Goutham 	}
154155ba18dcSKevin Hao 	return 0;
1542caa2da34SSunil Goutham fail:
1543caa2da34SSunil Goutham 	otx2_mbox_reset(&pfvf->mbox.mbox, 0);
1544caa2da34SSunil Goutham 	otx2_aura_pool_free(pfvf);
1545caa2da34SSunil Goutham 	return err;
1546caa2da34SSunil Goutham }
1547caa2da34SSunil Goutham 
otx2_config_npa(struct otx2_nic * pfvf)154805fcc9e0SSunil Goutham int otx2_config_npa(struct otx2_nic *pfvf)
154905fcc9e0SSunil Goutham {
155005fcc9e0SSunil Goutham 	struct otx2_qset *qset = &pfvf->qset;
155105fcc9e0SSunil Goutham 	struct npa_lf_alloc_req  *npalf;
155205fcc9e0SSunil Goutham 	struct otx2_hw *hw = &pfvf->hw;
155305fcc9e0SSunil Goutham 	int aura_cnt;
155405fcc9e0SSunil Goutham 
155505fcc9e0SSunil Goutham 	/* Pool - Stack of free buffer pointers
155605fcc9e0SSunil Goutham 	 * Aura - Alloc/frees pointers from/to pool for NIX DMA.
155705fcc9e0SSunil Goutham 	 */
155805fcc9e0SSunil Goutham 
155905fcc9e0SSunil Goutham 	if (!hw->pool_cnt)
156005fcc9e0SSunil Goutham 		return -EINVAL;
156105fcc9e0SSunil Goutham 
1562bf2bcd6fSXu Wang 	qset->pool = devm_kcalloc(pfvf->dev, hw->pool_cnt,
1563bf2bcd6fSXu Wang 				  sizeof(struct otx2_pool), GFP_KERNEL);
156405fcc9e0SSunil Goutham 	if (!qset->pool)
156505fcc9e0SSunil Goutham 		return -ENOMEM;
156605fcc9e0SSunil Goutham 
156705fcc9e0SSunil Goutham 	/* Get memory to put this msg */
156805fcc9e0SSunil Goutham 	npalf = otx2_mbox_alloc_msg_npa_lf_alloc(&pfvf->mbox);
156905fcc9e0SSunil Goutham 	if (!npalf)
157005fcc9e0SSunil Goutham 		return -ENOMEM;
157105fcc9e0SSunil Goutham 
157205fcc9e0SSunil Goutham 	/* Set aura and pool counts */
157305fcc9e0SSunil Goutham 	npalf->nr_pools = hw->pool_cnt;
157405fcc9e0SSunil Goutham 	aura_cnt = ilog2(roundup_pow_of_two(hw->pool_cnt));
157505fcc9e0SSunil Goutham 	npalf->aura_sz = (aura_cnt >= ilog2(128)) ? (aura_cnt - 6) : 1;
157605fcc9e0SSunil Goutham 
157705fcc9e0SSunil Goutham 	return otx2_sync_mbox_msg(&pfvf->mbox);
157805fcc9e0SSunil Goutham }
157905fcc9e0SSunil Goutham 
otx2_detach_resources(struct mbox * mbox)158005fcc9e0SSunil Goutham int otx2_detach_resources(struct mbox *mbox)
158105fcc9e0SSunil Goutham {
158205fcc9e0SSunil Goutham 	struct rsrc_detach *detach;
158305fcc9e0SSunil Goutham 
15844c3212f5SSunil Goutham 	mutex_lock(&mbox->lock);
158505fcc9e0SSunil Goutham 	detach = otx2_mbox_alloc_msg_detach_resources(mbox);
158605fcc9e0SSunil Goutham 	if (!detach) {
15874c3212f5SSunil Goutham 		mutex_unlock(&mbox->lock);
158805fcc9e0SSunil Goutham 		return -ENOMEM;
158905fcc9e0SSunil Goutham 	}
159005fcc9e0SSunil Goutham 
159105fcc9e0SSunil Goutham 	/* detach all */
159205fcc9e0SSunil Goutham 	detach->partial = false;
159305fcc9e0SSunil Goutham 
159405fcc9e0SSunil Goutham 	/* Send detach request to AF */
15951a2ce88aSSubbaraya Sundeep 	otx2_sync_mbox_msg(mbox);
15964c3212f5SSunil Goutham 	mutex_unlock(&mbox->lock);
159705fcc9e0SSunil Goutham 	return 0;
159805fcc9e0SSunil Goutham }
15993184fb5bSTomasz Duszynski EXPORT_SYMBOL(otx2_detach_resources);
160005fcc9e0SSunil Goutham 
otx2_attach_npa_nix(struct otx2_nic * pfvf)160105fcc9e0SSunil Goutham int otx2_attach_npa_nix(struct otx2_nic *pfvf)
160205fcc9e0SSunil Goutham {
160305fcc9e0SSunil Goutham 	struct rsrc_attach *attach;
160405fcc9e0SSunil Goutham 	struct msg_req *msix;
160505fcc9e0SSunil Goutham 	int err;
160605fcc9e0SSunil Goutham 
16074c3212f5SSunil Goutham 	mutex_lock(&pfvf->mbox.lock);
160805fcc9e0SSunil Goutham 	/* Get memory to put this msg */
160905fcc9e0SSunil Goutham 	attach = otx2_mbox_alloc_msg_attach_resources(&pfvf->mbox);
161005fcc9e0SSunil Goutham 	if (!attach) {
16114c3212f5SSunil Goutham 		mutex_unlock(&pfvf->mbox.lock);
161205fcc9e0SSunil Goutham 		return -ENOMEM;
161305fcc9e0SSunil Goutham 	}
161405fcc9e0SSunil Goutham 
161505fcc9e0SSunil Goutham 	attach->npalf = true;
161605fcc9e0SSunil Goutham 	attach->nixlf = true;
161705fcc9e0SSunil Goutham 
161805fcc9e0SSunil Goutham 	/* Send attach request to AF */
161905fcc9e0SSunil Goutham 	err = otx2_sync_mbox_msg(&pfvf->mbox);
162005fcc9e0SSunil Goutham 	if (err) {
16214c3212f5SSunil Goutham 		mutex_unlock(&pfvf->mbox.lock);
162205fcc9e0SSunil Goutham 		return err;
162305fcc9e0SSunil Goutham 	}
162405fcc9e0SSunil Goutham 
1625caa2da34SSunil Goutham 	pfvf->nix_blkaddr = BLKADDR_NIX0;
1626caa2da34SSunil Goutham 
1627caa2da34SSunil Goutham 	/* If the platform has two NIX blocks then LF may be
1628caa2da34SSunil Goutham 	 * allocated from NIX1.
1629caa2da34SSunil Goutham 	 */
1630caa2da34SSunil Goutham 	if (otx2_read64(pfvf, RVU_PF_BLOCK_ADDRX_DISC(BLKADDR_NIX1)) & 0x1FFULL)
1631caa2da34SSunil Goutham 		pfvf->nix_blkaddr = BLKADDR_NIX1;
1632caa2da34SSunil Goutham 
163305fcc9e0SSunil Goutham 	/* Get NPA and NIX MSIX vector offsets */
163405fcc9e0SSunil Goutham 	msix = otx2_mbox_alloc_msg_msix_offset(&pfvf->mbox);
163505fcc9e0SSunil Goutham 	if (!msix) {
16364c3212f5SSunil Goutham 		mutex_unlock(&pfvf->mbox.lock);
163705fcc9e0SSunil Goutham 		return -ENOMEM;
163805fcc9e0SSunil Goutham 	}
163905fcc9e0SSunil Goutham 
164005fcc9e0SSunil Goutham 	err = otx2_sync_mbox_msg(&pfvf->mbox);
164105fcc9e0SSunil Goutham 	if (err) {
16424c3212f5SSunil Goutham 		mutex_unlock(&pfvf->mbox.lock);
164305fcc9e0SSunil Goutham 		return err;
164405fcc9e0SSunil Goutham 	}
16454c3212f5SSunil Goutham 	mutex_unlock(&pfvf->mbox.lock);
164605fcc9e0SSunil Goutham 
164705fcc9e0SSunil Goutham 	if (pfvf->hw.npa_msixoff == MSIX_VECTOR_INVALID ||
164805fcc9e0SSunil Goutham 	    pfvf->hw.nix_msixoff == MSIX_VECTOR_INVALID) {
164905fcc9e0SSunil Goutham 		dev_err(pfvf->dev,
165005fcc9e0SSunil Goutham 			"RVUPF: Invalid MSIX vector offset for NPA/NIX\n");
165105fcc9e0SSunil Goutham 		return -EINVAL;
165205fcc9e0SSunil Goutham 	}
165305fcc9e0SSunil Goutham 
165405fcc9e0SSunil Goutham 	return 0;
165505fcc9e0SSunil Goutham }
16563184fb5bSTomasz Duszynski EXPORT_SYMBOL(otx2_attach_npa_nix);
165705fcc9e0SSunil Goutham 
otx2_ctx_disable(struct mbox * mbox,int type,bool npa)1658caa2da34SSunil Goutham void otx2_ctx_disable(struct mbox *mbox, int type, bool npa)
1659caa2da34SSunil Goutham {
1660caa2da34SSunil Goutham 	struct hwctx_disable_req *req;
1661caa2da34SSunil Goutham 
16624c3212f5SSunil Goutham 	mutex_lock(&mbox->lock);
1663caa2da34SSunil Goutham 	/* Request AQ to disable this context */
1664caa2da34SSunil Goutham 	if (npa)
1665caa2da34SSunil Goutham 		req = otx2_mbox_alloc_msg_npa_hwctx_disable(mbox);
1666caa2da34SSunil Goutham 	else
1667caa2da34SSunil Goutham 		req = otx2_mbox_alloc_msg_nix_hwctx_disable(mbox);
1668caa2da34SSunil Goutham 
1669caa2da34SSunil Goutham 	if (!req) {
16704c3212f5SSunil Goutham 		mutex_unlock(&mbox->lock);
1671caa2da34SSunil Goutham 		return;
1672caa2da34SSunil Goutham 	}
1673caa2da34SSunil Goutham 
1674caa2da34SSunil Goutham 	req->ctype = type;
1675caa2da34SSunil Goutham 
1676caa2da34SSunil Goutham 	if (otx2_sync_mbox_msg(mbox))
1677caa2da34SSunil Goutham 		dev_err(mbox->pfvf->dev, "%s failed to disable context\n",
1678caa2da34SSunil Goutham 			__func__);
1679caa2da34SSunil Goutham 
16804c3212f5SSunil Goutham 	mutex_unlock(&mbox->lock);
1681caa2da34SSunil Goutham }
1682caa2da34SSunil Goutham 
otx2_nix_config_bp(struct otx2_nic * pfvf,bool enable)168375f36270SGeetha sowjanya int otx2_nix_config_bp(struct otx2_nic *pfvf, bool enable)
168475f36270SGeetha sowjanya {
168575f36270SGeetha sowjanya 	struct nix_bp_cfg_req *req;
168675f36270SGeetha sowjanya 
168775f36270SGeetha sowjanya 	if (enable)
168875f36270SGeetha sowjanya 		req = otx2_mbox_alloc_msg_nix_bp_enable(&pfvf->mbox);
168975f36270SGeetha sowjanya 	else
169075f36270SGeetha sowjanya 		req = otx2_mbox_alloc_msg_nix_bp_disable(&pfvf->mbox);
169175f36270SGeetha sowjanya 
169275f36270SGeetha sowjanya 	if (!req)
169375f36270SGeetha sowjanya 		return -ENOMEM;
169475f36270SGeetha sowjanya 
169575f36270SGeetha sowjanya 	req->chan_base = 0;
16968e675581SHariprasad Kelam #ifdef CONFIG_DCB
16978e675581SHariprasad Kelam 	req->chan_cnt = pfvf->pfc_en ? IEEE_8021QAZ_MAX_TCS : 1;
16988e675581SHariprasad Kelam 	req->bpid_per_chan = pfvf->pfc_en ? 1 : 0;
16998e675581SHariprasad Kelam #else
170075f36270SGeetha sowjanya 	req->chan_cnt =  1;
170175f36270SGeetha sowjanya 	req->bpid_per_chan = 0;
17028e675581SHariprasad Kelam #endif
17038e675581SHariprasad Kelam 
170475f36270SGeetha sowjanya 	return otx2_sync_mbox_msg(&pfvf->mbox);
170575f36270SGeetha sowjanya }
17068e675581SHariprasad Kelam EXPORT_SYMBOL(otx2_nix_config_bp);
170775f36270SGeetha sowjanya 
1708d45d8979SChristina Jacob /* Mbox message handlers */
mbox_handler_cgx_stats(struct otx2_nic * pfvf,struct cgx_stats_rsp * rsp)1709d45d8979SChristina Jacob void mbox_handler_cgx_stats(struct otx2_nic *pfvf,
1710d45d8979SChristina Jacob 			    struct cgx_stats_rsp *rsp)
1711d45d8979SChristina Jacob {
1712d45d8979SChristina Jacob 	int id;
1713d45d8979SChristina Jacob 
1714d45d8979SChristina Jacob 	for (id = 0; id < CGX_RX_STATS_COUNT; id++)
1715d45d8979SChristina Jacob 		pfvf->hw.cgx_rx_stats[id] = rsp->rx_stats[id];
1716d45d8979SChristina Jacob 	for (id = 0; id < CGX_TX_STATS_COUNT; id++)
1717d45d8979SChristina Jacob 		pfvf->hw.cgx_tx_stats[id] = rsp->tx_stats[id];
1718d45d8979SChristina Jacob }
1719d45d8979SChristina Jacob 
mbox_handler_cgx_fec_stats(struct otx2_nic * pfvf,struct cgx_fec_stats_rsp * rsp)1720d0cf9503SChristina Jacob void mbox_handler_cgx_fec_stats(struct otx2_nic *pfvf,
1721d0cf9503SChristina Jacob 				struct cgx_fec_stats_rsp *rsp)
1722d0cf9503SChristina Jacob {
1723d0cf9503SChristina Jacob 	pfvf->hw.cgx_fec_corr_blks += rsp->fec_corr_blks;
1724d0cf9503SChristina Jacob 	pfvf->hw.cgx_fec_uncorr_blks += rsp->fec_uncorr_blks;
1725d0cf9503SChristina Jacob }
1726d0cf9503SChristina Jacob 
mbox_handler_npa_lf_alloc(struct otx2_nic * pfvf,struct npa_lf_alloc_rsp * rsp)172705fcc9e0SSunil Goutham void mbox_handler_npa_lf_alloc(struct otx2_nic *pfvf,
172805fcc9e0SSunil Goutham 			       struct npa_lf_alloc_rsp *rsp)
172905fcc9e0SSunil Goutham {
173005fcc9e0SSunil Goutham 	pfvf->hw.stack_pg_ptrs = rsp->stack_pg_ptrs;
173105fcc9e0SSunil Goutham 	pfvf->hw.stack_pg_bytes = rsp->stack_pg_bytes;
173205fcc9e0SSunil Goutham }
17333184fb5bSTomasz Duszynski EXPORT_SYMBOL(mbox_handler_npa_lf_alloc);
173405fcc9e0SSunil Goutham 
mbox_handler_nix_lf_alloc(struct otx2_nic * pfvf,struct nix_lf_alloc_rsp * rsp)173505fcc9e0SSunil Goutham void mbox_handler_nix_lf_alloc(struct otx2_nic *pfvf,
173605fcc9e0SSunil Goutham 			       struct nix_lf_alloc_rsp *rsp)
173705fcc9e0SSunil Goutham {
173805fcc9e0SSunil Goutham 	pfvf->hw.sqb_size = rsp->sqb_size;
173905fcc9e0SSunil Goutham 	pfvf->hw.rx_chan_base = rsp->rx_chan_base;
174005fcc9e0SSunil Goutham 	pfvf->hw.tx_chan_base = rsp->tx_chan_base;
174186d74760SSunil Goutham 	pfvf->hw.lso_tsov4_idx = rsp->lso_tsov4_idx;
174286d74760SSunil Goutham 	pfvf->hw.lso_tsov6_idx = rsp->lso_tsov6_idx;
17438bcf5cedSSubbaraya Sundeep 	pfvf->hw.cgx_links = rsp->cgx_links;
17448bcf5cedSSubbaraya Sundeep 	pfvf->hw.lbk_links = rsp->lbk_links;
1745039190bbSSubbaraya Sundeep 	pfvf->hw.tx_link = rsp->tx_link;
174605fcc9e0SSunil Goutham }
17473184fb5bSTomasz Duszynski EXPORT_SYMBOL(mbox_handler_nix_lf_alloc);
174805fcc9e0SSunil Goutham 
mbox_handler_msix_offset(struct otx2_nic * pfvf,struct msix_offset_rsp * rsp)174905fcc9e0SSunil Goutham void mbox_handler_msix_offset(struct otx2_nic *pfvf,
175005fcc9e0SSunil Goutham 			      struct msix_offset_rsp *rsp)
175105fcc9e0SSunil Goutham {
175205fcc9e0SSunil Goutham 	pfvf->hw.npa_msixoff = rsp->npa_msixoff;
175305fcc9e0SSunil Goutham 	pfvf->hw.nix_msixoff = rsp->nix_msixoff;
175405fcc9e0SSunil Goutham }
17553184fb5bSTomasz Duszynski EXPORT_SYMBOL(mbox_handler_msix_offset);
17565a6d7c9dSSunil Goutham 
mbox_handler_nix_bp_enable(struct otx2_nic * pfvf,struct nix_bp_cfg_rsp * rsp)175775f36270SGeetha sowjanya void mbox_handler_nix_bp_enable(struct otx2_nic *pfvf,
175875f36270SGeetha sowjanya 				struct nix_bp_cfg_rsp *rsp)
175975f36270SGeetha sowjanya {
176075f36270SGeetha sowjanya 	int chan, chan_id;
176175f36270SGeetha sowjanya 
176275f36270SGeetha sowjanya 	for (chan = 0; chan < rsp->chan_cnt; chan++) {
176375f36270SGeetha sowjanya 		chan_id = ((rsp->chan_bpid[chan] >> 10) & 0x7F);
176475f36270SGeetha sowjanya 		pfvf->bpid[chan_id] = rsp->chan_bpid[chan] & 0x3FF;
176575f36270SGeetha sowjanya 	}
176675f36270SGeetha sowjanya }
17673184fb5bSTomasz Duszynski EXPORT_SYMBOL(mbox_handler_nix_bp_enable);
176875f36270SGeetha sowjanya 
otx2_free_cints(struct otx2_nic * pfvf,int n)176904a21ef3SSunil Goutham void otx2_free_cints(struct otx2_nic *pfvf, int n)
177004a21ef3SSunil Goutham {
177104a21ef3SSunil Goutham 	struct otx2_qset *qset = &pfvf->qset;
177204a21ef3SSunil Goutham 	struct otx2_hw *hw = &pfvf->hw;
177304a21ef3SSunil Goutham 	int irq, qidx;
177404a21ef3SSunil Goutham 
177504a21ef3SSunil Goutham 	for (qidx = 0, irq = hw->nix_msixoff + NIX_LF_CINT_VEC_START;
177604a21ef3SSunil Goutham 	     qidx < n;
177704a21ef3SSunil Goutham 	     qidx++, irq++) {
177804a21ef3SSunil Goutham 		int vector = pci_irq_vector(pfvf->pdev, irq);
177904a21ef3SSunil Goutham 
178004a21ef3SSunil Goutham 		irq_set_affinity_hint(vector, NULL);
178104a21ef3SSunil Goutham 		free_cpumask_var(hw->affinity_mask[irq]);
178204a21ef3SSunil Goutham 		free_irq(vector, &qset->napi[qidx]);
178304a21ef3SSunil Goutham 	}
178404a21ef3SSunil Goutham }
178504a21ef3SSunil Goutham 
otx2_set_cints_affinity(struct otx2_nic * pfvf)178604a21ef3SSunil Goutham void otx2_set_cints_affinity(struct otx2_nic *pfvf)
178704a21ef3SSunil Goutham {
178804a21ef3SSunil Goutham 	struct otx2_hw *hw = &pfvf->hw;
178904a21ef3SSunil Goutham 	int vec, cpu, irq, cint;
179004a21ef3SSunil Goutham 
179104a21ef3SSunil Goutham 	vec = hw->nix_msixoff + NIX_LF_CINT_VEC_START;
179204a21ef3SSunil Goutham 	cpu = cpumask_first(cpu_online_mask);
179304a21ef3SSunil Goutham 
179404a21ef3SSunil Goutham 	/* CQ interrupts */
179504a21ef3SSunil Goutham 	for (cint = 0; cint < pfvf->hw.cint_cnt; cint++, vec++) {
179604a21ef3SSunil Goutham 		if (!alloc_cpumask_var(&hw->affinity_mask[vec], GFP_KERNEL))
179704a21ef3SSunil Goutham 			return;
179804a21ef3SSunil Goutham 
179904a21ef3SSunil Goutham 		cpumask_set_cpu(cpu, hw->affinity_mask[vec]);
180004a21ef3SSunil Goutham 
180104a21ef3SSunil Goutham 		irq = pci_irq_vector(pfvf->pdev, vec);
180204a21ef3SSunil Goutham 		irq_set_affinity_hint(irq, hw->affinity_mask[vec]);
180304a21ef3SSunil Goutham 
180404a21ef3SSunil Goutham 		cpu = cpumask_next(cpu, cpu_online_mask);
180504a21ef3SSunil Goutham 		if (unlikely(cpu >= nr_cpu_ids))
180604a21ef3SSunil Goutham 			cpu = 0;
180704a21ef3SSunil Goutham 	}
180804a21ef3SSunil Goutham }
180904a21ef3SSunil Goutham 
get_dwrr_mtu(struct otx2_nic * pfvf,struct nix_hw_info * hw)1810bbba125eSSunil Goutham static u32 get_dwrr_mtu(struct otx2_nic *pfvf, struct nix_hw_info *hw)
1811bbba125eSSunil Goutham {
1812bbba125eSSunil Goutham 	if (is_otx2_lbkvf(pfvf->pdev)) {
1813bbba125eSSunil Goutham 		pfvf->hw.smq_link_type = SMQ_LINK_TYPE_LBK;
1814bbba125eSSunil Goutham 		return hw->lbk_dwrr_mtu;
1815bbba125eSSunil Goutham 	}
1816bbba125eSSunil Goutham 
1817bbba125eSSunil Goutham 	pfvf->hw.smq_link_type = SMQ_LINK_TYPE_RPM;
1818bbba125eSSunil Goutham 	return hw->rpm_dwrr_mtu;
1819bbba125eSSunil Goutham }
1820bbba125eSSunil Goutham 
otx2_get_max_mtu(struct otx2_nic * pfvf)1821ab58a416SHariprasad Kelam u16 otx2_get_max_mtu(struct otx2_nic *pfvf)
1822ab58a416SHariprasad Kelam {
1823ab58a416SHariprasad Kelam 	struct nix_hw_info *rsp;
1824ab58a416SHariprasad Kelam 	struct msg_req *req;
1825ab58a416SHariprasad Kelam 	u16 max_mtu;
1826ab58a416SHariprasad Kelam 	int rc;
1827ab58a416SHariprasad Kelam 
1828ab58a416SHariprasad Kelam 	mutex_lock(&pfvf->mbox.lock);
1829ab58a416SHariprasad Kelam 
1830ab58a416SHariprasad Kelam 	req = otx2_mbox_alloc_msg_nix_get_hw_info(&pfvf->mbox);
1831ab58a416SHariprasad Kelam 	if (!req) {
1832ab58a416SHariprasad Kelam 		rc =  -ENOMEM;
1833ab58a416SHariprasad Kelam 		goto out;
1834ab58a416SHariprasad Kelam 	}
1835ab58a416SHariprasad Kelam 
1836ab58a416SHariprasad Kelam 	rc = otx2_sync_mbox_msg(&pfvf->mbox);
1837ab58a416SHariprasad Kelam 	if (!rc) {
1838ab58a416SHariprasad Kelam 		rsp = (struct nix_hw_info *)
1839ab58a416SHariprasad Kelam 		       otx2_mbox_get_rsp(&pfvf->mbox.mbox, 0, &req->hdr);
1840*52c63a6aSDipendra Khadka 		if (IS_ERR(rsp)) {
1841*52c63a6aSDipendra Khadka 			rc = PTR_ERR(rsp);
1842*52c63a6aSDipendra Khadka 			goto out;
1843*52c63a6aSDipendra Khadka 		}
1844ab58a416SHariprasad Kelam 
1845ab58a416SHariprasad Kelam 		/* HW counts VLAN insertion bytes (8 for double tag)
1846ab58a416SHariprasad Kelam 		 * irrespective of whether SQE is requesting to insert VLAN
1847ab58a416SHariprasad Kelam 		 * in the packet or not. Hence these 8 bytes have to be
1848ab58a416SHariprasad Kelam 		 * discounted from max packet size otherwise HW will throw
1849ab58a416SHariprasad Kelam 		 * SMQ errors
1850ab58a416SHariprasad Kelam 		 */
1851ab58a416SHariprasad Kelam 		max_mtu = rsp->max_mtu - 8 - OTX2_ETH_HLEN;
1852c39830a4SSunil Goutham 
1853c39830a4SSunil Goutham 		/* Also save DWRR MTU, needed for DWRR weight calculation */
1854bbba125eSSunil Goutham 		pfvf->hw.dwrr_mtu = get_dwrr_mtu(pfvf, rsp);
1855c39830a4SSunil Goutham 		if (!pfvf->hw.dwrr_mtu)
1856c39830a4SSunil Goutham 			pfvf->hw.dwrr_mtu = 1;
1857ab58a416SHariprasad Kelam 	}
1858ab58a416SHariprasad Kelam 
1859ab58a416SHariprasad Kelam out:
1860ab58a416SHariprasad Kelam 	mutex_unlock(&pfvf->mbox.lock);
1861ab58a416SHariprasad Kelam 	if (rc) {
1862ab58a416SHariprasad Kelam 		dev_warn(pfvf->dev,
1863ab58a416SHariprasad Kelam 			 "Failed to get MTU from hardware setting default value(1500)\n");
1864ab58a416SHariprasad Kelam 		max_mtu = 1500;
1865ab58a416SHariprasad Kelam 	}
1866ab58a416SHariprasad Kelam 	return max_mtu;
1867ab58a416SHariprasad Kelam }
1868ab58a416SHariprasad Kelam EXPORT_SYMBOL(otx2_get_max_mtu);
1869ab58a416SHariprasad Kelam 
otx2_handle_ntuple_tc_features(struct net_device * netdev,netdev_features_t features)18704b0385bcSSubbaraya Sundeep int otx2_handle_ntuple_tc_features(struct net_device *netdev, netdev_features_t features)
18714b0385bcSSubbaraya Sundeep {
18724b0385bcSSubbaraya Sundeep 	netdev_features_t changed = features ^ netdev->features;
18734b0385bcSSubbaraya Sundeep 	struct otx2_nic *pfvf = netdev_priv(netdev);
18744b0385bcSSubbaraya Sundeep 	bool ntuple = !!(features & NETIF_F_NTUPLE);
18754b0385bcSSubbaraya Sundeep 	bool tc = !!(features & NETIF_F_HW_TC);
18764b0385bcSSubbaraya Sundeep 
18774b0385bcSSubbaraya Sundeep 	if ((changed & NETIF_F_NTUPLE) && !ntuple)
18784b0385bcSSubbaraya Sundeep 		otx2_destroy_ntuple_flows(pfvf);
18794b0385bcSSubbaraya Sundeep 
18804b0385bcSSubbaraya Sundeep 	if ((changed & NETIF_F_NTUPLE) && ntuple) {
18814b0385bcSSubbaraya Sundeep 		if (!pfvf->flow_cfg->max_flows) {
18824b0385bcSSubbaraya Sundeep 			netdev_err(netdev,
18834b0385bcSSubbaraya Sundeep 				   "Can't enable NTUPLE, MCAM entries not allocated\n");
18844b0385bcSSubbaraya Sundeep 			return -EINVAL;
18854b0385bcSSubbaraya Sundeep 		}
18864b0385bcSSubbaraya Sundeep 	}
18874b0385bcSSubbaraya Sundeep 
18884b0385bcSSubbaraya Sundeep 	if ((changed & NETIF_F_HW_TC) && !tc &&
188961f98da4SHariprasad Kelam 	    otx2_tc_flower_rule_cnt(pfvf)) {
18904b0385bcSSubbaraya Sundeep 		netdev_err(netdev, "Can't disable TC hardware offload while flows are active\n");
18914b0385bcSSubbaraya Sundeep 		return -EBUSY;
18924b0385bcSSubbaraya Sundeep 	}
18934b0385bcSSubbaraya Sundeep 
18944b0385bcSSubbaraya Sundeep 	if ((changed & NETIF_F_NTUPLE) && ntuple &&
189561f98da4SHariprasad Kelam 	    otx2_tc_flower_rule_cnt(pfvf) && !(changed & NETIF_F_HW_TC)) {
18964b0385bcSSubbaraya Sundeep 		netdev_err(netdev,
189761f98da4SHariprasad Kelam 			   "Can't enable NTUPLE when TC flower offload is active, disable TC rules and retry\n");
18984b0385bcSSubbaraya Sundeep 		return -EINVAL;
18994b0385bcSSubbaraya Sundeep 	}
19004b0385bcSSubbaraya Sundeep 
19014b0385bcSSubbaraya Sundeep 	return 0;
19024b0385bcSSubbaraya Sundeep }
19034b0385bcSSubbaraya Sundeep EXPORT_SYMBOL(otx2_handle_ntuple_tc_features);
19044b0385bcSSubbaraya Sundeep 
19055a6d7c9dSSunil Goutham #define M(_name, _id, _fn_name, _req_type, _rsp_type)			\
19065a6d7c9dSSunil Goutham int __weak								\
19075a6d7c9dSSunil Goutham otx2_mbox_up_handler_ ## _fn_name(struct otx2_nic *pfvf,		\
19085a6d7c9dSSunil Goutham 				struct _req_type *req,			\
19095a6d7c9dSSunil Goutham 				struct _rsp_type *rsp)			\
19105a6d7c9dSSunil Goutham {									\
19115a6d7c9dSSunil Goutham 	/* Nothing to do here */					\
19125a6d7c9dSSunil Goutham 	return 0;							\
19135a6d7c9dSSunil Goutham }									\
19145a6d7c9dSSunil Goutham EXPORT_SYMBOL(otx2_mbox_up_handler_ ## _fn_name);
19155a6d7c9dSSunil Goutham MBOX_UP_CGX_MESSAGES
1916c54ffc73SSubbaraya Sundeep MBOX_UP_MCS_MESSAGES
19175a6d7c9dSSunil Goutham #undef M
1918