Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright(C) 2021 Marvell.
3 : : */
4 : :
5 : : #include <unistd.h>
6 : : #include <assert.h>
7 : : #include <rte_eal.h>
8 : : #include <rte_mempool.h>
9 : : #include <rte_mbuf.h>
10 : : #include <rte_io.h>
11 : : #include <rte_net.h>
12 : : #include <ethdev_pci.h>
13 : :
14 : : #include "otx_ep_common.h"
15 : : #include "otx_ep_vf.h"
16 : : #include "otx_ep_rxtx.h"
17 : :
18 : : static void
19 : 0 : otx_ep_dmazone_free(const struct rte_memzone *mz)
20 : : {
21 : : const struct rte_memzone *mz_tmp;
22 : : int ret = 0;
23 : :
24 [ # # ]: 0 : if (mz == NULL) {
25 : 0 : otx_ep_err("Memzone: NULL\n");
26 : 0 : return;
27 : : }
28 : :
29 : 0 : mz_tmp = rte_memzone_lookup(mz->name);
30 [ # # ]: 0 : if (mz_tmp == NULL) {
31 : 0 : otx_ep_err("Memzone %s Not Found\n", mz->name);
32 : 0 : return;
33 : : }
34 : :
35 : 0 : ret = rte_memzone_free(mz);
36 [ # # ]: 0 : if (ret)
37 : 0 : otx_ep_err("Memzone free failed : ret = %d\n", ret);
38 : : }
39 : :
40 : : /* Free IQ resources */
41 : : int
42 : 0 : otx_ep_delete_iqs(struct otx_ep_device *otx_ep, uint32_t iq_no)
43 : : {
44 : : struct otx_ep_instr_queue *iq;
45 : : uint32_t i;
46 : :
47 : 0 : iq = otx_ep->instr_queue[iq_no];
48 [ # # ]: 0 : if (iq == NULL) {
49 : 0 : otx_ep_err("Invalid IQ[%d]\n", iq_no);
50 : 0 : return -EINVAL;
51 : : }
52 : :
53 [ # # ]: 0 : if (iq->req_list) {
54 [ # # ]: 0 : for (i = 0; i < iq->nb_desc; i++)
55 : 0 : rte_free(iq->req_list[i].finfo.g.sg);
56 : 0 : rte_free(iq->req_list);
57 : : }
58 : :
59 : 0 : iq->req_list = NULL;
60 : :
61 [ # # ]: 0 : if (iq->iq_mz) {
62 : 0 : otx_ep_dmazone_free(iq->iq_mz);
63 : 0 : iq->iq_mz = NULL;
64 : : }
65 : :
66 : 0 : rte_free(otx_ep->instr_queue[iq_no]);
67 : 0 : otx_ep->instr_queue[iq_no] = NULL;
68 : :
69 : 0 : otx_ep->nb_tx_queues--;
70 : :
71 : 0 : otx_ep_info("IQ[%d] is deleted\n", iq_no);
72 : :
73 : 0 : return 0;
74 : : }
75 : :
76 : : /* IQ initialization */
77 : : static int
78 : 0 : otx_ep_init_instr_queue(struct otx_ep_device *otx_ep, int iq_no, int num_descs,
79 : : unsigned int socket_id)
80 : : {
81 : : const struct otx_ep_config *conf;
82 : : struct otx_ep_instr_queue *iq;
83 : : struct otx_ep_sg_entry *sg;
84 : : uint32_t i, q_size;
85 : : int ret;
86 : :
87 : 0 : conf = otx_ep->conf;
88 : 0 : iq = otx_ep->instr_queue[iq_no];
89 : 0 : q_size = conf->iq.instr_type * num_descs;
90 : :
91 : : /* IQ memory creation for Instruction submission to OCTEON 9 */
92 : 0 : iq->iq_mz = rte_eth_dma_zone_reserve(otx_ep->eth_dev,
93 : : "instr_queue", iq_no, q_size,
94 : : OTX_EP_PCI_RING_ALIGN,
95 : : socket_id);
96 [ # # ]: 0 : if (iq->iq_mz == NULL) {
97 : 0 : otx_ep_err("IQ[%d] memzone alloc failed\n", iq_no);
98 : 0 : goto iq_init_fail;
99 : : }
100 : :
101 : 0 : iq->base_addr_dma = iq->iq_mz->iova;
102 : 0 : iq->base_addr = (uint8_t *)iq->iq_mz->addr;
103 : :
104 [ # # ]: 0 : if (num_descs & (num_descs - 1)) {
105 : 0 : otx_ep_err("IQ[%d] descs not in power of 2\n", iq_no);
106 : 0 : goto iq_init_fail;
107 : : }
108 : :
109 : 0 : iq->nb_desc = num_descs;
110 : :
111 : : /* Create a IQ request list to hold requests that have been
112 : : * posted to OCTEON 9. This list will be used for freeing the IQ
113 : : * data buffer(s) later once the OCTEON 9 fetched the requests.
114 : : */
115 : 0 : iq->req_list = rte_zmalloc_socket("request_list",
116 : 0 : (iq->nb_desc * OTX_EP_IQREQ_LIST_SIZE),
117 : : RTE_CACHE_LINE_SIZE,
118 : 0 : rte_socket_id());
119 [ # # ]: 0 : if (iq->req_list == NULL) {
120 : 0 : otx_ep_err("IQ[%d] req_list alloc failed\n", iq_no);
121 : 0 : goto iq_init_fail;
122 : : }
123 : :
124 [ # # ]: 0 : for (i = 0; i < iq->nb_desc; i++) {
125 : 0 : sg = rte_zmalloc_socket("sg_entry", (OTX_EP_MAX_SG_LISTS * OTX_EP_SG_ENTRY_SIZE),
126 : 0 : OTX_EP_SG_ALIGN, rte_socket_id());
127 [ # # ]: 0 : if (sg == NULL) {
128 : 0 : otx_ep_err("IQ[%d] sg_entries alloc failed\n", iq_no);
129 : 0 : goto iq_init_fail;
130 : : }
131 : :
132 : 0 : iq->req_list[i].finfo.g.num_sg = OTX_EP_MAX_SG_LISTS;
133 : 0 : iq->req_list[i].finfo.g.sg = sg;
134 : : }
135 : :
136 : 0 : otx_ep_info("IQ[%d]: base: %p basedma: %lx count: %d\n",
137 : : iq_no, iq->base_addr, (unsigned long)iq->base_addr_dma,
138 : : iq->nb_desc);
139 : :
140 : 0 : iq->mbuf_list = rte_zmalloc_socket("mbuf_list", (iq->nb_desc * sizeof(struct rte_mbuf *)),
141 : 0 : RTE_CACHE_LINE_SIZE, rte_socket_id());
142 [ # # ]: 0 : if (!iq->mbuf_list) {
143 : 0 : otx_ep_err("IQ[%d] mbuf_list alloc failed\n", iq_no);
144 : 0 : goto iq_init_fail;
145 : : }
146 : :
147 : 0 : iq->otx_ep_dev = otx_ep;
148 : 0 : iq->q_no = iq_no;
149 : 0 : iq->fill_cnt = 0;
150 : 0 : iq->host_write_index = 0;
151 : 0 : iq->otx_read_index = 0;
152 : 0 : iq->flush_index = 0;
153 : 0 : iq->instr_pending = 0;
154 : :
155 : 0 : otx_ep->io_qmask.iq |= (1ull << iq_no);
156 : :
157 : : /* Set 32B/64B mode for each input queue */
158 [ # # ]: 0 : if (conf->iq.instr_type == 64)
159 : 0 : otx_ep->io_qmask.iq64B |= (1ull << iq_no);
160 : :
161 : 0 : iq->iqcmd_64B = (conf->iq.instr_type == 64);
162 : :
163 : : /* Set up IQ registers */
164 : 0 : ret = otx_ep->fn_list.setup_iq_regs(otx_ep, iq_no);
165 [ # # ]: 0 : if (ret)
166 : 0 : return ret;
167 : :
168 : : return 0;
169 : :
170 : : iq_init_fail:
171 : : return -ENOMEM;
172 : : }
173 : :
174 : : int
175 : 0 : otx_ep_setup_iqs(struct otx_ep_device *otx_ep, uint32_t iq_no, int num_descs,
176 : : unsigned int socket_id)
177 : : {
178 : : struct otx_ep_instr_queue *iq;
179 : :
180 : 0 : iq = (struct otx_ep_instr_queue *)rte_zmalloc("otx_ep_IQ", sizeof(*iq),
181 : : RTE_CACHE_LINE_SIZE);
182 [ # # ]: 0 : if (iq == NULL)
183 : : return -ENOMEM;
184 : :
185 : 0 : otx_ep->instr_queue[iq_no] = iq;
186 : :
187 [ # # ]: 0 : if (otx_ep_init_instr_queue(otx_ep, iq_no, num_descs, socket_id)) {
188 : 0 : otx_ep_err("IQ init is failed\n");
189 : 0 : goto delete_IQ;
190 : : }
191 : 0 : otx_ep->nb_tx_queues++;
192 : :
193 : 0 : otx_ep_info("IQ[%d] is created.\n", iq_no);
194 : :
195 : 0 : return 0;
196 : :
197 : : delete_IQ:
198 : 0 : otx_ep_delete_iqs(otx_ep, iq_no);
199 : 0 : return -ENOMEM;
200 : : }
201 : :
202 : : static void
203 : : otx_ep_droq_reset_indices(struct otx_ep_droq *droq)
204 : : {
205 : 0 : droq->read_idx = 0;
206 : 0 : droq->write_idx = 0;
207 : 0 : droq->refill_idx = 0;
208 : 0 : droq->refill_count = 0;
209 : 0 : droq->last_pkt_count = 0;
210 : 0 : droq->pkts_pending = 0;
211 : : }
212 : :
213 : : static void
214 : 0 : otx_ep_droq_destroy_ring_buffers(struct otx_ep_droq *droq)
215 : : {
216 : : uint32_t idx;
217 : :
218 [ # # ]: 0 : for (idx = 0; idx < droq->nb_desc; idx++) {
219 [ # # ]: 0 : if (droq->recv_buf_list[idx]) {
220 : 0 : rte_pktmbuf_free(droq->recv_buf_list[idx]);
221 : 0 : droq->recv_buf_list[idx] = NULL;
222 : : }
223 : : }
224 : :
225 : : otx_ep_droq_reset_indices(droq);
226 : 0 : }
227 : :
228 : : /* Free OQs resources */
229 : : int
230 : 0 : otx_ep_delete_oqs(struct otx_ep_device *otx_ep, uint32_t oq_no)
231 : : {
232 : : struct otx_ep_droq *droq;
233 : :
234 : 0 : droq = otx_ep->droq[oq_no];
235 [ # # ]: 0 : if (droq == NULL) {
236 : 0 : otx_ep_err("Invalid droq[%d]\n", oq_no);
237 : 0 : return -EINVAL;
238 : : }
239 : :
240 : 0 : otx_ep_droq_destroy_ring_buffers(droq);
241 : 0 : rte_free(droq->recv_buf_list);
242 : 0 : droq->recv_buf_list = NULL;
243 : :
244 [ # # ]: 0 : if (droq->desc_ring_mz) {
245 : 0 : otx_ep_dmazone_free(droq->desc_ring_mz);
246 : : droq->desc_ring_mz = NULL;
247 : : }
248 : :
249 : : memset(droq, 0, OTX_EP_DROQ_SIZE);
250 : :
251 : 0 : rte_free(otx_ep->droq[oq_no]);
252 : 0 : otx_ep->droq[oq_no] = NULL;
253 : :
254 : 0 : otx_ep->nb_rx_queues--;
255 : :
256 : 0 : otx_ep_info("OQ[%d] is deleted\n", oq_no);
257 : 0 : return 0;
258 : : }
259 : :
260 : : static int
261 : 0 : otx_ep_droq_setup_ring_buffers(struct otx_ep_droq *droq)
262 : : {
263 : 0 : struct otx_ep_droq_desc *desc_ring = droq->desc_ring;
264 : : struct otx_ep_droq_info *info;
265 : : struct rte_mbuf *buf;
266 : : uint32_t idx;
267 : :
268 [ # # ]: 0 : for (idx = 0; idx < droq->nb_desc; idx++) {
269 : 0 : buf = rte_pktmbuf_alloc(droq->mpool);
270 [ # # ]: 0 : if (buf == NULL) {
271 : 0 : otx_ep_err("OQ buffer alloc failed\n");
272 : 0 : droq->stats.rx_alloc_failure++;
273 : 0 : return -ENOMEM;
274 : : }
275 : :
276 : 0 : droq->recv_buf_list[idx] = buf;
277 : 0 : info = rte_pktmbuf_mtod(buf, struct otx_ep_droq_info *);
278 : : memset(info, 0, sizeof(*info));
279 : 0 : desc_ring[idx].buffer_ptr = rte_mbuf_data_iova_default(buf);
280 : : }
281 : :
282 : : otx_ep_droq_reset_indices(droq);
283 : :
284 : 0 : return 0;
285 : : }
286 : :
287 : : static inline uint64_t
288 : : otx_ep_set_rearm_data(struct otx_ep_device *otx_ep)
289 : : {
290 : 0 : uint16_t port_id = otx_ep->port_id;
291 : : struct rte_mbuf mb_def;
292 : : uint64_t *tmp;
293 : :
294 : : RTE_BUILD_BUG_ON(offsetof(struct rte_mbuf, data_off) % 8 != 0);
295 : : RTE_BUILD_BUG_ON(offsetof(struct rte_mbuf, refcnt) - offsetof(struct rte_mbuf, data_off) !=
296 : : 2);
297 : : RTE_BUILD_BUG_ON(offsetof(struct rte_mbuf, nb_segs) - offsetof(struct rte_mbuf, data_off) !=
298 : : 4);
299 : : RTE_BUILD_BUG_ON(offsetof(struct rte_mbuf, port) - offsetof(struct rte_mbuf, data_off) !=
300 : : 6);
301 : 0 : mb_def.nb_segs = 1;
302 : 0 : mb_def.data_off = RTE_PKTMBUF_HEADROOM + OTX_EP_INFO_SIZE;
303 : 0 : mb_def.port = port_id;
304 : : rte_mbuf_refcnt_set(&mb_def, 1);
305 : :
306 : : /* Prevent compiler reordering: rearm_data covers previous fields */
307 : 0 : rte_compiler_barrier();
308 : : tmp = (uint64_t *)&mb_def.rearm_data;
309 : :
310 : 0 : return *tmp;
311 : : }
312 : :
313 : : /* OQ initialization */
314 : : static int
315 : 0 : otx_ep_init_droq(struct otx_ep_device *otx_ep, uint32_t q_no,
316 : : uint32_t num_descs, uint32_t desc_size,
317 : : struct rte_mempool *mpool, unsigned int socket_id)
318 : : {
319 : 0 : const struct otx_ep_config *conf = otx_ep->conf;
320 : : uint32_t c_refill_threshold;
321 : : struct otx_ep_droq *droq;
322 : : uint32_t desc_ring_size;
323 : : int ret;
324 : :
325 : 0 : otx_ep_info("OQ[%d] Init start\n", q_no);
326 : :
327 : 0 : droq = otx_ep->droq[q_no];
328 : 0 : droq->otx_ep_dev = otx_ep;
329 : 0 : droq->q_no = q_no;
330 : 0 : droq->mpool = mpool;
331 : :
332 : 0 : droq->nb_desc = num_descs;
333 : 0 : droq->buffer_size = desc_size;
334 : 0 : c_refill_threshold = RTE_MAX(conf->oq.refill_threshold,
335 : : droq->nb_desc / 2);
336 : :
337 : : /* OQ desc_ring set up */
338 : 0 : desc_ring_size = droq->nb_desc * OTX_EP_DROQ_DESC_SIZE;
339 : 0 : droq->desc_ring_mz = rte_eth_dma_zone_reserve(otx_ep->eth_dev, "droq",
340 : : q_no, desc_ring_size,
341 : : OTX_EP_PCI_RING_ALIGN,
342 : : socket_id);
343 : :
344 [ # # ]: 0 : if (droq->desc_ring_mz == NULL) {
345 : 0 : otx_ep_err("OQ:%d desc_ring allocation failed\n", q_no);
346 : 0 : goto init_droq_fail;
347 : : }
348 : :
349 : 0 : droq->desc_ring_dma = droq->desc_ring_mz->iova;
350 : 0 : droq->desc_ring = (struct otx_ep_droq_desc *)droq->desc_ring_mz->addr;
351 : :
352 : 0 : otx_ep_dbg("OQ[%d]: desc_ring: virt: 0x%p, dma: %lx\n",
353 : : q_no, droq->desc_ring, (unsigned long)droq->desc_ring_dma);
354 : 0 : otx_ep_dbg("OQ[%d]: num_desc: %d\n", q_no, droq->nb_desc);
355 : :
356 : : /* OQ buf_list set up */
357 : 0 : droq->recv_buf_list = rte_zmalloc_socket("recv_buf_list",
358 : 0 : (droq->nb_desc * sizeof(struct rte_mbuf *)),
359 : : RTE_CACHE_LINE_SIZE, socket_id);
360 [ # # ]: 0 : if (droq->recv_buf_list == NULL) {
361 : 0 : otx_ep_err("OQ recv_buf_list alloc failed\n");
362 : 0 : goto init_droq_fail;
363 : : }
364 : :
365 [ # # ]: 0 : if (otx_ep_droq_setup_ring_buffers(droq))
366 : 0 : goto init_droq_fail;
367 : :
368 : 0 : droq->refill_threshold = c_refill_threshold;
369 : 0 : droq->rearm_data = otx_ep_set_rearm_data(otx_ep);
370 : :
371 : : /* Set up OQ registers */
372 : 0 : ret = otx_ep->fn_list.setup_oq_regs(otx_ep, q_no);
373 [ # # ]: 0 : if (ret)
374 : : return ret;
375 : :
376 : 0 : otx_ep->io_qmask.oq |= (1ull << q_no);
377 : :
378 : 0 : return 0;
379 : :
380 : : init_droq_fail:
381 : : return -ENOMEM;
382 : : }
383 : :
384 : : /* OQ configuration and setup */
385 : : int
386 : 0 : otx_ep_setup_oqs(struct otx_ep_device *otx_ep, int oq_no, int num_descs,
387 : : int desc_size, struct rte_mempool *mpool,
388 : : unsigned int socket_id)
389 : : {
390 : : struct otx_ep_droq *droq;
391 : :
392 : : /* Allocate new droq. */
393 : 0 : droq = (struct otx_ep_droq *)rte_zmalloc("otx_ep_OQ",
394 : : sizeof(*droq), RTE_CACHE_LINE_SIZE);
395 [ # # ]: 0 : if (droq == NULL) {
396 : 0 : otx_ep_err("Droq[%d] Creation Failed\n", oq_no);
397 : 0 : return -ENOMEM;
398 : : }
399 : 0 : otx_ep->droq[oq_no] = droq;
400 : :
401 [ # # ]: 0 : if (otx_ep_init_droq(otx_ep, oq_no, num_descs, desc_size, mpool,
402 : : socket_id)) {
403 : 0 : otx_ep_err("Droq[%d] Initialization failed\n", oq_no);
404 : 0 : goto delete_OQ;
405 : : }
406 : 0 : otx_ep_info("OQ[%d] is created.\n", oq_no);
407 : :
408 : 0 : otx_ep->nb_rx_queues++;
409 : :
410 : 0 : return 0;
411 : :
412 : : delete_OQ:
413 : 0 : otx_ep_delete_oqs(otx_ep, oq_no);
414 : 0 : return -ENOMEM;
415 : : }
416 : :
417 : : static inline void
418 : 0 : otx_ep_iqreq_delete(struct otx_ep_instr_queue *iq, uint32_t idx)
419 : : {
420 : : struct rte_mbuf *mbuf;
421 : : uint32_t reqtype;
422 : :
423 : 0 : mbuf = iq->req_list[idx].finfo.mbuf;
424 : 0 : reqtype = iq->req_list[idx].reqtype;
425 : :
426 [ # # ]: 0 : switch (reqtype) {
427 : 0 : case OTX_EP_REQTYPE_NORESP_NET:
428 : : case OTX_EP_REQTYPE_NORESP_GATHER:
429 : : /* This will take care of multiple segments also */
430 : 0 : rte_pktmbuf_free(mbuf);
431 : 0 : otx_ep_dbg("IQ buffer freed at idx[%d]\n", idx);
432 : 0 : break;
433 : :
434 : 0 : case OTX_EP_REQTYPE_NONE:
435 : : default:
436 : 0 : otx_ep_info("This iqreq mode is not supported:%d\n", reqtype);
437 : : }
438 : :
439 : : /* Reset the request list at this index */
440 : 0 : iq->req_list[idx].finfo.mbuf = NULL;
441 : 0 : iq->req_list[idx].reqtype = 0;
442 : 0 : }
443 : :
444 : : static inline void
445 : : otx_ep_iqreq_add(struct otx_ep_instr_queue *iq, struct rte_mbuf *mbuf,
446 : : uint32_t reqtype, int index)
447 : : {
448 : 0 : iq->req_list[index].finfo.mbuf = mbuf;
449 : 0 : iq->req_list[index].reqtype = reqtype;
450 : : }
451 : :
452 : : static uint32_t
453 : 0 : otx_vf_update_read_index(struct otx_ep_instr_queue *iq)
454 : : {
455 : : uint32_t val;
456 : :
457 : : /*
458 : : * Batch subtractions from the HW counter to reduce PCIe traffic
459 : : * This adds an extra local variable, but almost halves the
460 : : * number of PCIe writes.
461 : : */
462 : 0 : val = *iq->inst_cnt_ism;
463 : 0 : iq->inst_cnt += val - iq->inst_cnt_ism_prev;
464 : 0 : iq->inst_cnt_ism_prev = val;
465 : :
466 [ # # ]: 0 : if (val > (uint32_t)(1 << 31)) {
467 : : /*
468 : : * Only subtract the packet count in the HW counter
469 : : * when count above halfway to saturation.
470 : : */
471 : 0 : rte_write32(val, iq->inst_cnt_reg);
472 : : rte_mb();
473 : :
474 : 0 : rte_write64(OTX2_SDP_REQUEST_ISM, iq->inst_cnt_reg);
475 [ # # ]: 0 : while (__atomic_load_n(iq->inst_cnt_ism, __ATOMIC_RELAXED) >= val) {
476 : 0 : rte_write64(OTX2_SDP_REQUEST_ISM, iq->inst_cnt_reg);
477 : : rte_mb();
478 : : }
479 : :
480 : 0 : iq->inst_cnt_ism_prev = 0;
481 : : }
482 : 0 : rte_write64(OTX2_SDP_REQUEST_ISM, iq->inst_cnt_reg);
483 : :
484 : : /* Modulo of the new index with the IQ size will give us
485 : : * the new index.
486 : : */
487 : 0 : return iq->inst_cnt & (iq->nb_desc - 1);
488 : : }
489 : :
490 : : static void
491 : 0 : otx_ep_flush_iq(struct otx_ep_instr_queue *iq)
492 : : {
493 : : uint32_t instr_processed = 0;
494 : :
495 : 0 : iq->otx_read_index = otx_vf_update_read_index(iq);
496 [ # # ]: 0 : while (iq->flush_index != iq->otx_read_index) {
497 : : /* Free the IQ data buffer to the pool */
498 : 0 : otx_ep_iqreq_delete(iq, iq->flush_index);
499 : 0 : iq->flush_index =
500 : 0 : otx_ep_incr_index(iq->flush_index, 1, iq->nb_desc);
501 : :
502 : 0 : instr_processed++;
503 : : }
504 : :
505 : 0 : iq->stats.instr_processed = instr_processed;
506 : 0 : iq->instr_pending -= instr_processed;
507 : 0 : }
508 : :
509 : : static inline void
510 : : otx_ep_ring_doorbell(struct otx_ep_device *otx_ep __rte_unused,
511 : : struct otx_ep_instr_queue *iq)
512 : : {
513 : : rte_wmb();
514 : 0 : rte_write64(iq->fill_cnt, iq->doorbell_reg);
515 : 0 : iq->fill_cnt = 0;
516 : 0 : }
517 : :
518 : : static inline int
519 : 0 : post_iqcmd(struct otx_ep_instr_queue *iq, uint8_t *iqcmd)
520 : : {
521 : : uint8_t *iqptr, cmdsize;
522 : :
523 : : /* This ensures that the read index does not wrap around to
524 : : * the same position if queue gets full before OCTEON 9 could
525 : : * fetch any instr.
526 : : */
527 [ # # ]: 0 : if (iq->instr_pending > (iq->nb_desc - 1))
528 : : return OTX_EP_IQ_SEND_FAILED;
529 : :
530 : : /* Copy cmd into iq */
531 : : cmdsize = 64;
532 [ # # ]: 0 : iqptr = iq->base_addr + (iq->host_write_index << 6);
533 : :
534 : : rte_memcpy(iqptr, iqcmd, cmdsize);
535 : :
536 : : /* Increment the host write index */
537 : 0 : iq->host_write_index =
538 : 0 : otx_ep_incr_index(iq->host_write_index, 1, iq->nb_desc);
539 : :
540 : 0 : iq->fill_cnt++;
541 : :
542 : : /* Flush the command into memory. We need to be sure the data
543 : : * is in memory before indicating that the instruction is
544 : : * pending.
545 : : */
546 : 0 : iq->instr_pending++;
547 : : /* OTX_EP_IQ_SEND_SUCCESS */
548 : 0 : return 0;
549 : : }
550 : :
551 : :
552 : : static int
553 : 0 : otx_ep_send_data(struct otx_ep_device *otx_ep, struct otx_ep_instr_queue *iq,
554 : : void *cmd, int dbell)
555 : : {
556 : : uint32_t ret;
557 : :
558 : : /* Submit IQ command */
559 : 0 : ret = post_iqcmd(iq, cmd);
560 : :
561 [ # # ]: 0 : if (ret == OTX_EP_IQ_SEND_SUCCESS) {
562 [ # # ]: 0 : if (dbell)
563 : : otx_ep_ring_doorbell(otx_ep, iq);
564 : 0 : iq->stats.instr_posted++;
565 : :
566 : : } else {
567 : 0 : iq->stats.instr_dropped++;
568 [ # # ]: 0 : if (iq->fill_cnt)
569 : : otx_ep_ring_doorbell(otx_ep, iq);
570 : : }
571 : 0 : return ret;
572 : : }
573 : :
574 : : static inline void
575 : : set_sg_size(struct otx_ep_sg_entry *sg_entry, uint16_t size, uint32_t pos)
576 : : {
577 : : #if RTE_BYTE_ORDER == RTE_BIG_ENDIAN
578 : : sg_entry->u.size[pos] = size;
579 : : #elif RTE_BYTE_ORDER == RTE_LITTLE_ENDIAN
580 : 0 : sg_entry->u.size[(OTX_EP_NUM_SG_PTRS - 1) - pos] = size;
581 : : #endif
582 : : }
583 : :
584 : : static inline int
585 : 0 : prepare_xmit_gather_list(struct otx_ep_instr_queue *iq, struct rte_mbuf *m, uint64_t *dptr,
586 : : union otx_ep_instr_ih *ih)
587 : : {
588 : : uint16_t j = 0, frags, num_sg, mask = OTX_EP_NUM_SG_PTRS - 1;
589 : : struct otx_ep_buf_free_info *finfo;
590 : : uint32_t pkt_len;
591 : : int rc = -1;
592 : :
593 : 0 : pkt_len = rte_pktmbuf_pkt_len(m);
594 : 0 : frags = m->nb_segs;
595 : 0 : num_sg = (frags + mask) / OTX_EP_NUM_SG_PTRS;
596 : :
597 [ # # ]: 0 : if (unlikely(pkt_len > OTX_EP_MAX_PKT_SZ && num_sg > OTX_EP_MAX_SG_LISTS)) {
598 : 0 : otx_ep_err("Failed to xmit the pkt, pkt_len is higher or pkt has more segments\n");
599 : 0 : goto exit;
600 : : }
601 : :
602 : 0 : finfo = &iq->req_list[iq->host_write_index].finfo;
603 : 0 : *dptr = rte_mem_virt2iova(finfo->g.sg);
604 : 0 : ih->u64 |= ((1ULL << 62) | ((uint64_t)frags << 48) | (pkt_len + ih->s.fsz));
605 : :
606 [ # # ]: 0 : while (frags--) {
607 : 0 : finfo->g.sg[(j >> 2)].ptr[(j & mask)] = rte_mbuf_data_iova(m);
608 : 0 : set_sg_size(&finfo->g.sg[(j >> 2)], m->data_len, (j & mask));
609 : 0 : j++;
610 : 0 : m = m->next;
611 : : }
612 : :
613 : : return 0;
614 : :
615 : : exit:
616 : 0 : return rc;
617 : : }
618 : :
619 : : /* Enqueue requests/packets to OTX_EP IQ queue.
620 : : * returns number of requests enqueued successfully
621 : : */
622 : : uint16_t
623 : 0 : otx_ep_xmit_pkts(void *tx_queue, struct rte_mbuf **pkts, uint16_t nb_pkts)
624 : : {
625 : : struct otx_ep_instr_queue *iq = (struct otx_ep_instr_queue *)tx_queue;
626 : 0 : struct otx_ep_device *otx_ep = iq->otx_ep_dev;
627 : : struct otx_ep_instr_64B iqcmd;
628 : : int dbell, index, count = 0;
629 : : uint32_t iqreq_type;
630 : : uint32_t pkt_len, i;
631 : : struct rte_mbuf *m;
632 : :
633 : 0 : iqcmd.ih.u64 = 0;
634 : 0 : iqcmd.pki_ih3.u64 = 0;
635 : 0 : iqcmd.irh.u64 = 0;
636 : :
637 : : /* ih invars */
638 : 0 : iqcmd.ih.s.fsz = OTX_EP_FSZ;
639 : 0 : iqcmd.ih.s.pkind = otx_ep->pkind; /* The SDK decided PKIND value */
640 : :
641 : : /* pki ih3 invars */
642 : 0 : iqcmd.pki_ih3.s.w = 1;
643 : 0 : iqcmd.pki_ih3.s.utt = 1;
644 : : iqcmd.pki_ih3.s.tagtype = ORDERED_TAG;
645 : : /* sl will be sizeof(pki_ih3) */
646 : 0 : iqcmd.pki_ih3.s.sl = OTX_EP_FSZ + OTX_CUST_DATA_LEN;
647 : :
648 : : /* irh invars */
649 : 0 : iqcmd.irh.s.opcode = OTX_EP_NW_PKT_OP;
650 : :
651 [ # # ]: 0 : for (i = 0; i < nb_pkts; i++) {
652 : 0 : m = pkts[i];
653 [ # # ]: 0 : if (m->nb_segs == 1) {
654 : 0 : pkt_len = rte_pktmbuf_data_len(m);
655 : 0 : iqcmd.ih.s.tlen = pkt_len + iqcmd.ih.s.fsz;
656 : 0 : iqcmd.dptr = rte_mbuf_data_iova(m); /*dptr*/
657 : 0 : iqcmd.ih.s.gather = 0;
658 : 0 : iqcmd.ih.s.gsz = 0;
659 : : iqreq_type = OTX_EP_REQTYPE_NORESP_NET;
660 : : } else {
661 [ # # ]: 0 : if (!(otx_ep->tx_offloads & RTE_ETH_TX_OFFLOAD_MULTI_SEGS))
662 : 0 : goto xmit_fail;
663 : :
664 [ # # ]: 0 : if (unlikely(prepare_xmit_gather_list(iq, m, &iqcmd.dptr, &iqcmd.ih) < 0))
665 : 0 : goto xmit_fail;
666 : :
667 : 0 : pkt_len = rte_pktmbuf_pkt_len(m);
668 : : iqreq_type = OTX_EP_REQTYPE_NORESP_GATHER;
669 : : }
670 : :
671 [ # # ]: 0 : iqcmd.irh.u64 = rte_bswap64(iqcmd.irh.u64);
672 : :
673 : : #ifdef OTX_EP_IO_DEBUG
674 : : otx_ep_dbg("After swapping\n");
675 : : otx_ep_dbg("Word0 [dptr]: 0x%016lx\n",
676 : : (unsigned long)iqcmd.dptr);
677 : : otx_ep_dbg("Word1 [ihtx]: 0x%016lx\n", (unsigned long)iqcmd.ih);
678 : : otx_ep_dbg("Word2 [pki_ih3]: 0x%016lx\n",
679 : : (unsigned long)iqcmd.pki_ih3);
680 : : otx_ep_dbg("Word3 [rptr]: 0x%016lx\n",
681 : : (unsigned long)iqcmd.rptr);
682 : : otx_ep_dbg("Word4 [irh]: 0x%016lx\n", (unsigned long)iqcmd.irh);
683 : : otx_ep_dbg("Word5 [exhdr[0]]: 0x%016lx\n",
684 : : (unsigned long)iqcmd.exhdr[0]);
685 : : rte_pktmbuf_dump(stdout, m, rte_pktmbuf_pkt_len(m));
686 : : #endif
687 : 0 : dbell = (i == (unsigned int)(nb_pkts - 1)) ? 1 : 0;
688 : 0 : index = iq->host_write_index;
689 [ # # ]: 0 : if (otx_ep_send_data(otx_ep, iq, &iqcmd, dbell))
690 : 0 : goto xmit_fail;
691 : : otx_ep_iqreq_add(iq, m, iqreq_type, index);
692 : 0 : iq->stats.tx_pkts++;
693 : 0 : iq->stats.tx_bytes += pkt_len;
694 : 0 : count++;
695 : : }
696 : :
697 : 0 : xmit_fail:
698 [ # # ]: 0 : if (iq->instr_pending >= OTX_EP_MAX_INSTR)
699 : 0 : otx_ep_flush_iq(iq);
700 : :
701 : : /* Return no# of instructions posted successfully. */
702 : 0 : return count;
703 : : }
704 : :
705 : : static uint32_t
706 : 0 : otx_ep_droq_refill(struct otx_ep_droq *droq)
707 : : {
708 : 0 : struct otx_ep_droq_desc *desc_ring = droq->desc_ring;
709 : : struct otx_ep_droq_info *info;
710 : : struct rte_mbuf *buf = NULL;
711 : : uint32_t desc_refilled = 0;
712 : :
713 [ # # # # ]: 0 : while (droq->refill_count && (desc_refilled < droq->nb_desc)) {
714 : 0 : buf = rte_pktmbuf_alloc(droq->mpool);
715 : : /* If a buffer could not be allocated, no point in
716 : : * continuing
717 : : */
718 [ # # ]: 0 : if (unlikely(!buf)) {
719 : 0 : droq->stats.rx_alloc_failure++;
720 : 0 : break;
721 : : }
722 : 0 : info = rte_pktmbuf_mtod(buf, struct otx_ep_droq_info *);
723 : 0 : info->length = 0;
724 : :
725 : 0 : droq->recv_buf_list[droq->refill_idx] = buf;
726 : 0 : desc_ring[droq->refill_idx].buffer_ptr =
727 : : rte_mbuf_data_iova_default(buf);
728 : 0 : droq->refill_idx = otx_ep_incr_index(droq->refill_idx, 1,
729 : : droq->nb_desc);
730 : :
731 : 0 : desc_refilled++;
732 : 0 : droq->refill_count--;
733 : : }
734 : :
735 : 0 : return desc_refilled;
736 : : }
737 : :
738 : : static struct rte_mbuf *
739 : 0 : otx_ep_droq_read_packet(struct otx_ep_device *otx_ep, struct otx_ep_droq *droq, int next_fetch)
740 : : {
741 : : volatile struct otx_ep_droq_info *info;
742 : : struct rte_mbuf *mbuf_next = NULL;
743 : : struct rte_mbuf *mbuf = NULL;
744 : : uint64_t total_pkt_len;
745 : : uint32_t pkt_len = 0;
746 : : int next_idx;
747 : :
748 : 0 : mbuf = droq->recv_buf_list[droq->read_idx];
749 : 0 : info = rte_pktmbuf_mtod(mbuf, struct otx_ep_droq_info *);
750 : :
751 : : /* make sure info is available */
752 : : rte_rmb();
753 [ # # ]: 0 : if (unlikely(!info->length)) {
754 : : int retry = OTX_EP_MAX_DELAYED_PKT_RETRIES;
755 : : /* otx_ep_dbg("OCTEON DROQ[%d]: read_idx: %d; Data not ready "
756 : : * "yet, Retry; pending=%lu\n", droq->q_no, droq->read_idx,
757 : : * droq->pkts_pending);
758 : : */
759 : 0 : droq->stats.pkts_delayed_data++;
760 [ # # # # ]: 0 : while (retry && !info->length) {
761 : 0 : retry--;
762 : 0 : rte_delay_us_block(50);
763 : : }
764 [ # # # # ]: 0 : if (!retry && !info->length) {
765 : 0 : otx_ep_err("OCTEON DROQ[%d]: read_idx: %d; Retry failed !!\n",
766 : : droq->q_no, droq->read_idx);
767 : : /* May be zero length packet; drop it */
768 : 0 : assert(0);
769 : : }
770 : : }
771 : :
772 [ # # ]: 0 : if (next_fetch) {
773 : 0 : next_idx = otx_ep_incr_index(droq->read_idx, 1, droq->nb_desc);
774 : 0 : mbuf_next = droq->recv_buf_list[next_idx];
775 : 0 : rte_prefetch0(rte_pktmbuf_mtod(mbuf_next, void *));
776 : : }
777 : :
778 : 0 : info->length = rte_bswap16(info->length >> 48);
779 : : /* Deduce the actual data size */
780 : 0 : total_pkt_len = info->length + OTX_EP_INFO_SIZE;
781 [ # # ]: 0 : if (total_pkt_len <= droq->buffer_size) {
782 : 0 : mbuf->data_off += OTX_EP_INFO_SIZE;
783 : 0 : pkt_len = (uint32_t)info->length;
784 : 0 : mbuf->pkt_len = pkt_len;
785 : 0 : mbuf->data_len = pkt_len;
786 : 0 : mbuf->port = otx_ep->port_id;
787 : 0 : droq->recv_buf_list[droq->read_idx] = NULL;
788 : 0 : droq->read_idx = otx_ep_incr_index(droq->read_idx, 1, droq->nb_desc);
789 : 0 : droq->refill_count++;
790 : : } else {
791 : : struct rte_mbuf *first_buf = NULL;
792 : : struct rte_mbuf *last_buf = NULL;
793 : :
794 : : /* csr read helps to flush pending dma */
795 : 0 : droq->sent_reg_val = rte_read32(droq->pkts_sent_reg);
796 : : rte_rmb();
797 : :
798 [ # # ]: 0 : while (pkt_len < total_pkt_len) {
799 : : int cpy_len = 0;
800 : :
801 : 0 : cpy_len = ((pkt_len + droq->buffer_size) > total_pkt_len)
802 : 0 : ? ((uint32_t)total_pkt_len - pkt_len)
803 [ # # ]: 0 : : droq->buffer_size;
804 : :
805 : 0 : mbuf = droq->recv_buf_list[droq->read_idx];
806 : 0 : droq->recv_buf_list[droq->read_idx] = NULL;
807 : :
808 [ # # ]: 0 : if (likely(mbuf)) {
809 : : /* Note the first seg */
810 [ # # ]: 0 : if (!pkt_len)
811 : : first_buf = mbuf;
812 : :
813 : 0 : mbuf->port = otx_ep->port_id;
814 [ # # ]: 0 : if (!pkt_len) {
815 : 0 : mbuf->data_off += OTX_EP_INFO_SIZE;
816 : 0 : mbuf->pkt_len = cpy_len - OTX_EP_INFO_SIZE;
817 : 0 : mbuf->data_len = cpy_len - OTX_EP_INFO_SIZE;
818 : : } else {
819 : 0 : mbuf->pkt_len = cpy_len;
820 : 0 : mbuf->data_len = cpy_len;
821 : : }
822 : :
823 [ # # ]: 0 : if (pkt_len) {
824 : 0 : first_buf->nb_segs++;
825 : 0 : first_buf->pkt_len += mbuf->pkt_len;
826 : : }
827 : :
828 [ # # ]: 0 : if (last_buf)
829 : 0 : last_buf->next = mbuf;
830 : :
831 : : last_buf = mbuf;
832 : : } else {
833 : 0 : otx_ep_err("no buf\n");
834 : 0 : assert(0);
835 : : }
836 : :
837 : 0 : pkt_len += cpy_len;
838 : 0 : droq->read_idx = otx_ep_incr_index(droq->read_idx, 1, droq->nb_desc);
839 : 0 : droq->refill_count++;
840 : : }
841 : : mbuf = first_buf;
842 : : }
843 : :
844 : 0 : return mbuf;
845 : : }
846 : :
847 : : static inline uint32_t
848 : 0 : otx_ep_check_droq_pkts(struct otx_ep_droq *droq)
849 : : {
850 : : uint32_t new_pkts;
851 : : uint32_t val;
852 : :
853 : : /*
854 : : * Batch subtractions from the HW counter to reduce PCIe traffic
855 : : * This adds an extra local variable, but almost halves the
856 : : * number of PCIe writes.
857 : : */
858 : 0 : val = *droq->pkts_sent_ism;
859 : 0 : new_pkts = val - droq->pkts_sent_ism_prev;
860 : 0 : droq->pkts_sent_ism_prev = val;
861 : :
862 [ # # ]: 0 : if (val > (uint32_t)(1 << 31)) {
863 : : /*
864 : : * Only subtract the packet count in the HW counter
865 : : * when count above halfway to saturation.
866 : : */
867 : 0 : rte_write32(val, droq->pkts_sent_reg);
868 : : rte_mb();
869 : :
870 : 0 : rte_write64(OTX2_SDP_REQUEST_ISM, droq->pkts_sent_reg);
871 [ # # ]: 0 : while (__atomic_load_n(droq->pkts_sent_ism, __ATOMIC_RELAXED) >= val) {
872 : 0 : rte_write64(OTX2_SDP_REQUEST_ISM, droq->pkts_sent_reg);
873 : : rte_mb();
874 : : }
875 : :
876 : 0 : droq->pkts_sent_ism_prev = 0;
877 : : }
878 : 0 : rte_write64(OTX2_SDP_REQUEST_ISM, droq->pkts_sent_reg);
879 : 0 : droq->pkts_pending += new_pkts;
880 : :
881 : 0 : return new_pkts;
882 : : }
883 : :
884 : : static inline int32_t __rte_hot
885 : : otx_ep_rx_pkts_to_process(struct otx_ep_droq *droq, uint16_t nb_pkts)
886 : : {
887 : 0 : if (unlikely(droq->pkts_pending < nb_pkts))
888 : 0 : otx_ep_check_droq_pkts(droq);
889 : :
890 : 0 : return RTE_MIN(nb_pkts, droq->pkts_pending);
891 : : }
892 : :
893 : : /* Check for response arrival from OCTEON 9
894 : : * returns number of requests completed
895 : : */
896 : : uint16_t
897 : 0 : otx_ep_recv_pkts(void *rx_queue, struct rte_mbuf **rx_pkts, uint16_t nb_pkts)
898 : : {
899 : : struct otx_ep_droq *droq = rx_queue;
900 : : struct otx_ep_device *otx_ep;
901 : : struct rte_mbuf *oq_pkt;
902 : : uint16_t pkts, new_pkts;
903 : : uint32_t valid_pkts = 0;
904 : : int next_fetch;
905 : :
906 [ # # ]: 0 : otx_ep = droq->otx_ep_dev;
907 : 0 : new_pkts = otx_ep_rx_pkts_to_process(droq, nb_pkts);
908 : :
909 [ # # ]: 0 : for (pkts = 0; pkts < new_pkts; pkts++) {
910 : : /* Push the received pkt to application */
911 : 0 : next_fetch = (pkts == new_pkts - 1) ? 0 : 1;
912 : 0 : oq_pkt = otx_ep_droq_read_packet(otx_ep, droq, next_fetch);
913 [ # # ]: 0 : if (!oq_pkt) {
914 : 0 : RTE_LOG_DP(ERR, OTX_NET_EP,
915 : : "DROQ read pkt failed pending %" PRIu64
916 : : "last_pkt_count %" PRIu64 "new_pkts %d.\n",
917 : : droq->pkts_pending, droq->last_pkt_count,
918 : : new_pkts);
919 : 0 : droq->stats.rx_err++;
920 : 0 : continue;
921 : : } else {
922 : 0 : rx_pkts[valid_pkts] = oq_pkt;
923 : 0 : valid_pkts++;
924 : : /* Stats */
925 : 0 : droq->stats.pkts_received++;
926 : 0 : droq->stats.bytes_received += oq_pkt->pkt_len;
927 : : }
928 : : }
929 : 0 : droq->pkts_pending -= pkts;
930 : :
931 : : /* Refill DROQ buffers */
932 [ # # ]: 0 : if (droq->refill_count >= DROQ_REFILL_THRESHOLD) {
933 : 0 : int desc_refilled = otx_ep_droq_refill(droq);
934 : :
935 : : /* Flush the droq descriptor data to memory to be sure
936 : : * that when we update the credits the data in memory is
937 : : * accurate.
938 : : */
939 : 0 : rte_io_wmb();
940 : 0 : rte_write32(desc_refilled, droq->pkts_credit_reg);
941 : : } else {
942 : : /*
943 : : * SDP output goes into DROP state when output doorbell count
944 : : * goes below drop count. When door bell count is written with
945 : : * a value greater than drop count SDP output should come out
946 : : * of DROP state. Due to a race condition this is not happening.
947 : : * Writing doorbell register with 0 again may make SDP output
948 : : * come out of this state.
949 : : */
950 : :
951 : 0 : rte_write32(0, droq->pkts_credit_reg);
952 : : }
953 : 0 : return valid_pkts;
954 : : }
|