Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright(C) 2024 Marvell.
3 : : */
4 : :
5 : : #include <cnxk_eswitch.h>
6 : :
7 : : static __rte_always_inline struct rte_mbuf *
8 : : eswitch_nix_get_mbuf_from_cqe(void *cq, const uint64_t data_off)
9 : : {
10 : : rte_iova_t buff;
11 : :
12 : : /* Skip CQE, NIX_RX_PARSE_S and SG HDR(9 DWORDs) and peek buff addr */
13 : : buff = *((rte_iova_t *)((uint64_t *)cq + 9));
14 : : return (struct rte_mbuf *)(buff - data_off);
15 : : }
16 : :
17 : : static inline uint64_t
18 : : eswitch_nix_rx_nb_pkts(struct roc_nix_cq *cq, const uint64_t wdata, const uint32_t qmask)
19 : : {
20 : : uint64_t reg, head, tail;
21 : : uint32_t available;
22 : :
23 : : /* Update the available count if cached value is not enough */
24 : :
25 : : /* Use LDADDA version to avoid reorder */
26 : : reg = roc_atomic64_add_sync(wdata, cq->status);
27 : : /* CQ_OP_STATUS operation error */
28 : : if (reg & BIT_ULL(NIX_CQ_OP_STAT_OP_ERR) || reg & BIT_ULL(NIX_CQ_OP_STAT_CQ_ERR))
29 : : return 0;
30 : :
31 : : tail = reg & 0xFFFFF;
32 : : head = (reg >> 20) & 0xFFFFF;
33 : : if (tail < head)
34 : : available = tail - head + qmask + 1;
35 : : else
36 : : available = tail - head;
37 : :
38 : : return available;
39 : : }
40 : :
41 : : static inline void
42 : : nix_cn9k_xmit_one(uint64_t *cmd, void *lmt_addr, const plt_iova_t io_addr, uint16_t segdw)
43 : : {
44 : : uint64_t lmt_status;
45 : :
46 : : do {
47 : : roc_lmt_mov_seg(lmt_addr, (const void *)cmd, segdw);
48 : : lmt_status = roc_lmt_submit_ldeor(io_addr);
49 : : } while (lmt_status == 0);
50 : : }
51 : :
52 : : static __rte_always_inline uint16_t
53 : : cnxk_eswitch_prepare_mseg(struct rte_mbuf *m, union nix_send_sg_s *sg, uint64_t *cmd, uint8_t off)
54 : : {
55 : : union nix_send_sg_s l_sg;
56 : : struct rte_mbuf *m_next;
57 : : uint64_t nb_segs;
58 : : uint64_t *slist;
59 : : uint16_t segdw;
60 : : uint64_t dlen;
61 : :
62 : : l_sg.u = 0;
63 : : l_sg.ld_type = NIX_SENDLDTYPE_LDD;
64 : : l_sg.subdc = NIX_SUBDC_SG;
65 : :
66 : 0 : dlen = m->data_len;
67 : 0 : l_sg.u |= dlen;
68 : 0 : nb_segs = m->nb_segs - 1;
69 : 0 : m_next = m->next;
70 : 0 : m->next = NULL;
71 : 0 : slist = &cmd[off + 1];
72 [ # # ]: 0 : l_sg.segs = 1;
73 : 0 : *slist = rte_mbuf_data_iova(m);
74 : 0 : slist++;
75 : : m = m_next;
76 [ # # ]: 0 : if (!m)
77 : 0 : goto done;
78 : :
79 : : do {
80 : : uint64_t iova;
81 : :
82 [ # # ]: 0 : m_next = m->next;
83 : : iova = rte_mbuf_data_iova(m);
84 : 0 : dlen = m->data_len;
85 : :
86 : 0 : nb_segs--;
87 : 0 : m->next = NULL;
88 : :
89 : 0 : *slist = iova;
90 : : /* Set the segment length */
91 : 0 : l_sg.u |= ((uint64_t)dlen << (l_sg.segs << 4));
92 : 0 : l_sg.segs += 1;
93 : 0 : slist++;
94 [ # # # # ]: 0 : if (l_sg.segs > 2 && nb_segs) {
95 : 0 : sg->u = l_sg.u;
96 : : /* Next SG subdesc */
97 : : sg = (union nix_send_sg_s *)slist;
98 : 0 : l_sg.u = 0;
99 : : l_sg.ld_type = NIX_SENDLDTYPE_LDD;
100 : 0 : l_sg.subdc = NIX_SUBDC_SG;
101 : 0 : slist++;
102 : : }
103 : : m = m_next;
104 [ # # ]: 0 : } while (nb_segs);
105 : 0 : done:
106 : : /* Write the last subdc out */
107 : 0 : sg->u = l_sg.u;
108 : 0 : segdw = (uint64_t *)slist - (uint64_t *)&cmd[off];
109 : : /* Roundup extra dwords to multiple of 2 */
110 : 0 : segdw = (segdw >> 1) + (segdw & 0x1);
111 : : /* Default dwords */
112 : 0 : segdw += (off >> 1);
113 : :
114 : : return segdw;
115 : : }
116 : :
117 : : uint16_t
118 : 0 : cnxk_eswitch_dev_tx_burst(struct cnxk_eswitch_dev *eswitch_dev, uint16_t qid,
119 : : struct rte_mbuf **pkts, uint16_t nb_xmit, const uint16_t flags)
120 : : {
121 : 0 : struct roc_nix_sq *sq = &eswitch_dev->txq[qid].sqs;
122 : : struct roc_nix_rq *rq = &eswitch_dev->rxq[qid].rqs;
123 : : uint64_t cmd[6 + CNXK_NIX_TX_MSEG_SG_DWORDS - 2];
124 : : uint16_t lmt_id, pkt = 0, nb_tx = 0;
125 : : struct nix_send_ext_s *send_hdr_ext;
126 : : struct nix_send_hdr_s *send_hdr;
127 : : uint64_t aura_handle, data = 0;
128 : : uint16_t vlan_tci = qid;
129 : : union nix_send_sg_s *sg;
130 : : uintptr_t lmt_base, pa;
131 : : int64_t fc_pkts, dw_m1;
132 : : struct rte_mbuf *m;
133 : : rte_iova_t io_addr;
134 : : uint16_t segdw;
135 : : uint64_t len;
136 : : uint8_t off;
137 : :
138 [ # # ]: 0 : if (unlikely(eswitch_dev->txq[qid].state != CNXK_ESWITCH_QUEUE_STATE_STARTED))
139 : : return 0;
140 : :
141 : 0 : lmt_base = sq->roc_nix->lmt_base;
142 : : io_addr = sq->io_addr;
143 : : aura_handle = rq->aura_handle;
144 : : /* Get LMT base address and LMT ID as per thread ID */
145 : 0 : lmt_id = roc_plt_control_lmt_id_get();
146 [ # # ]: 0 : lmt_base += ((uint64_t)lmt_id << ROC_LMT_LINE_SIZE_LOG2);
147 : : /* Double word minus 1: LMTST size-1 in units of 128 bits */
148 : : /* 2(HDR) + 2(EXT_HDR) + 1(SG) + 1(IOVA) = 6/2 - 1 = 2 */
149 : : dw_m1 = cn10k_nix_tx_ext_subs(flags) + 1;
150 : :
151 : : memset(cmd, 0, sizeof(cmd));
152 : :
153 : : send_hdr = (struct nix_send_hdr_s *)&cmd[0];
154 : 0 : send_hdr->w0.sq = sq->qid;
155 : :
156 [ # # ]: 0 : if (dw_m1 >= 2) {
157 : : send_hdr_ext = (struct nix_send_ext_s *)&cmd[2];
158 : 0 : send_hdr_ext->w0.subdc = NIX_SUBDC_EXT;
159 [ # # ]: 0 : if (flags & NIX_TX_OFFLOAD_VLAN_QINQ_F) {
160 : 0 : send_hdr_ext->w1.vlan0_ins_ena = true;
161 : : /* 2B before end of l2 header */
162 : 0 : send_hdr_ext->w1.vlan0_ins_ptr = 12;
163 : 0 : send_hdr_ext->w1.vlan0_ins_tci = vlan_tci;
164 : : }
165 : : off = 4;
166 : : } else {
167 : : off = 2;
168 : : }
169 : :
170 : 0 : sg = (union nix_send_sg_s *)&cmd[off];
171 : 0 : sg->subdc = NIX_SUBDC_SG;
172 : 0 : sg->ld_type = NIX_SENDLDTYPE_LDD;
173 : :
174 : : /* Tx */
175 : 0 : fc_pkts = ((int64_t)sq->nb_sqb_bufs_adj - *((uint64_t *)sq->fc)) << sq->sqes_per_sqb_log2;
176 : :
177 [ # # ]: 0 : if (fc_pkts < 0)
178 : : nb_tx = 0;
179 : : else
180 : 0 : nb_tx = PLT_MIN(nb_xmit, (uint64_t)fc_pkts);
181 : :
182 [ # # ]: 0 : for (pkt = 0; pkt < nb_tx; pkt++) {
183 : 0 : m = pkts[pkt];
184 : 0 : len = m->pkt_len;
185 : 0 : send_hdr->w0.total = len;
186 [ # # ]: 0 : if (m->pool) {
187 : 0 : aura_handle = m->pool->pool_id;
188 : 0 : send_hdr->w0.aura = roc_npa_aura_handle_to_aura(aura_handle);
189 : : } else {
190 : 0 : send_hdr->w0.df = 1;
191 : : }
192 : :
193 : : segdw = cnxk_eswitch_prepare_mseg(m, sg, cmd, off);
194 : 0 : send_hdr->w0.sizem1 = segdw - 1;
195 : :
196 : 0 : plt_esw_dbg("Transmitting pkt %d (%p) vlan tci %x on sq %d esw qid %d", pkt,
197 : : pkts[pkt], vlan_tci, sq->qid, qid);
198 [ # # ]: 0 : if (roc_model_is_cn9k()) {
199 : : nix_cn9k_xmit_one(cmd, sq->lmt_addr, sq->io_addr, segdw);
200 : : } else {
201 : : cn10k_nix_xmit_mv_lmt_base(lmt_base, cmd, flags);
202 : : /* PA<6:4> = LMTST size-1 in units of 128 bits. Size of the first LMTST in
203 : : * burst.
204 : : */
205 : : pa = io_addr | ((segdw - 1) << 4);
206 : : data &= ~0x7ULL;
207 : : /*<15:12> = CNTM1: Count minus one of LMTSTs in the burst */
208 : : data = (0ULL << 12);
209 : : /* *<10:0> = LMT_ID: Identifies which LMT line is used for the first LMTST
210 : : */
211 : : data |= (uint64_t)lmt_id;
212 : :
213 : : /* STEOR0 */
214 : : roc_lmt_submit_steorl(data, pa);
215 : 0 : rte_io_wmb();
216 : : }
217 : : }
218 : :
219 : : return nb_tx;
220 : : }
221 : :
222 : : static __rte_always_inline void
223 : : cnxk_eswitch_xtract_mseg(struct rte_mbuf *mbuf, const union nix_rx_parse_u *rx)
224 : : {
225 : : const rte_iova_t *iova_list;
226 : : uint16_t later_skip = 0;
227 : : const rte_iova_t *eol;
228 : : struct rte_mbuf *head;
229 : : uint8_t nb_segs;
230 : : uint16_t sg_len;
231 : : uint64_t sg;
232 : :
233 : : sg = *(const uint64_t *)(rx + 1);
234 : : nb_segs = (sg >> 48) & 0x3;
235 : : if (nb_segs == 1)
236 : : return;
237 : :
238 : : mbuf->nb_segs = nb_segs;
239 : : mbuf->data_len = sg & 0xFFFF;
240 : : head = mbuf;
241 : : eol = ((const rte_iova_t *)(rx + 1) + ((rx->desc_sizem1 + 1) << 1));
242 : : sg = sg >> 16;
243 : : /* Skip SG_S and first IOVA*/
244 : : iova_list = ((const rte_iova_t *)(rx + 1)) + 2;
245 : : nb_segs--;
246 : : later_skip = (uintptr_t)mbuf->buf_addr - (uintptr_t)mbuf;
247 : :
248 : : while (nb_segs) {
249 : : mbuf->next = (struct rte_mbuf *)(*iova_list - later_skip);
250 : : mbuf = mbuf->next;
251 : :
252 : : RTE_MEMPOOL_CHECK_COOKIES(mbuf->pool, (void **)&mbuf, 1, 1);
253 : :
254 : : sg_len = sg & 0XFFFF;
255 : :
256 : : mbuf->data_len = sg_len;
257 : : sg = sg >> 16;
258 : : nb_segs--;
259 : : iova_list++;
260 : :
261 : : if (!nb_segs && (iova_list + 1 < eol)) {
262 : : sg = *(const uint64_t *)(iova_list);
263 : : nb_segs = (sg >> 48) & 0x3;
264 : : head->nb_segs += nb_segs;
265 : : iova_list = (const rte_iova_t *)(iova_list + 1);
266 : : }
267 : : }
268 : : }
269 : :
270 : : uint16_t
271 : 0 : cnxk_eswitch_dev_rx_burst(struct cnxk_eswitch_dev *eswitch_dev, uint16_t qid,
272 : : struct rte_mbuf **pkts, uint16_t nb_pkts)
273 : : {
274 : : struct roc_nix_rq *rq = &eswitch_dev->rxq[qid].rqs;
275 : : struct roc_nix_cq *cq = &eswitch_dev->cxq[qid].cqs;
276 : : const union nix_rx_parse_u *rx;
277 : : struct nix_cqe_hdr_s *cqe;
278 : : uint64_t pkt = 0, nb_rx;
279 : : struct rte_mbuf *mbuf;
280 : : uint64_t wdata;
281 : : uint32_t qmask;
282 : : uintptr_t desc;
283 : : uint32_t head;
284 : :
285 : : if (unlikely(eswitch_dev->rxq[qid].state != CNXK_ESWITCH_QUEUE_STATE_STARTED))
286 : : return 0;
287 : :
288 : : wdata = cq->wdata;
289 : : qmask = cq->qmask;
290 : : desc = (uintptr_t)cq->desc_base;
291 : : nb_rx = eswitch_nix_rx_nb_pkts(cq, wdata, qmask);
292 : : nb_rx = RTE_MIN(nb_rx, nb_pkts);
293 : : head = cq->head;
294 : :
295 : : /* Nothing to receive */
296 : : if (!nb_rx)
297 : : return 0;
298 : :
299 : : /* Rx */
300 : : for (pkt = 0; pkt < nb_rx; pkt++) {
301 : : /* Prefetch N desc ahead */
302 : : rte_prefetch_non_temporal((void *)(desc + (CQE_SZ((head + 2) & qmask))));
303 : : cqe = (struct nix_cqe_hdr_s *)(desc + CQE_SZ(head));
304 : : rx = (const union nix_rx_parse_u *)((const uint64_t *)cqe + 1);
305 : :
306 : : /* Skip QE, NIX_RX_PARSE_S and SG HDR(9 DWORDs) and peek buff addr */
307 : : mbuf = eswitch_nix_get_mbuf_from_cqe(cqe, rq->first_skip);
308 : : mbuf->pkt_len = rx->pkt_lenm1 + 1;
309 : : mbuf->data_len = rx->pkt_lenm1 + 1;
310 : : mbuf->data_off = 128;
311 : : /* Rx parse to capture vlan info */
312 : : if (rx->vtag0_valid)
313 : : mbuf->vlan_tci = rx->vtag0_tci;
314 : : /* Populate RSS hash */
315 : : mbuf->hash.rss = cqe->tag;
316 : : mbuf->ol_flags = RTE_MBUF_F_RX_RSS_HASH;
317 : :
318 : : cnxk_eswitch_xtract_mseg(mbuf, rx);
319 : : pkts[pkt] = mbuf;
320 : : roc_prefetch_store_keep(mbuf);
321 : : plt_esw_dbg("Packet %d rec on queue %d esw qid %d hash %x mbuf %p vlan tci %d",
322 : : (uint32_t)pkt, rq->qid, qid, mbuf->hash.rss, mbuf, mbuf->vlan_tci);
323 : : head++;
324 : : head &= qmask;
325 : : }
326 : :
327 : : /* Free all the CQs that we've processed */
328 : : rte_write64_relaxed((wdata | nb_rx), (void *)cq->door);
329 : : cq->head = head;
330 : :
331 : : return nb_rx;
332 : : }
|