4 * Copyright (C) Cavium Inc. 2017. All Right reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
16 * * Neither the name of Cavium networks nor the names of its
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42 #include <rte_atomic.h>
45 #include <rte_errno.h>
46 #include <rte_memory.h>
47 #include <rte_malloc.h>
48 #include <rte_spinlock.h>
51 #include "octeontx_mbox.h"
52 #include "octeontx_fpavf.h"
54 /* FPA Mbox Message */
57 #define FPA_CONFIGSET 0x1
58 #define FPA_CONFIGGET 0x2
59 #define FPA_START_COUNT 0x3
60 #define FPA_STOP_COUNT 0x4
61 #define FPA_ATTACHAURA 0x5
62 #define FPA_DETACHAURA 0x6
63 #define FPA_SETAURALVL 0x7
64 #define FPA_GETAURALVL 0x8
66 #define FPA_COPROC 0x1
69 struct octeontx_mbox_fpa_cfg {
72 uint64_t pool_stack_base;
73 uint64_t pool_stack_end;
77 struct __attribute__((__packed__)) gen_req {
81 struct __attribute__((__packed__)) idn_req {
85 struct __attribute__((__packed__)) gen_resp {
90 struct __attribute__((__packed__)) dcfg_resp {
96 uint8_t net_port_count;
97 uint8_t virt_port_count;
100 #define FPA_MAX_POOL 32
101 #define FPA_PF_PAGE_SZ 4096
103 #define FPA_LN_SIZE 128
104 #define FPA_ROUND_UP(x, size) \
105 ((((unsigned long)(x)) + size-1) & (~(size-1)))
106 #define FPA_OBJSZ_2_CACHE_LINE(sz) (((sz) + RTE_CACHE_LINE_MASK) >> 7)
107 #define FPA_CACHE_LINE_2_OBJSZ(sz) ((sz) << 7)
109 #define POOL_ENA (0x1 << 0)
110 #define POOL_DIS (0x0 << 0)
111 #define POOL_SET_NAT_ALIGN (0x1 << 1)
112 #define POOL_DIS_NAT_ALIGN (0x0 << 1)
113 #define POOL_STYPE(x) (((x) & 0x1) << 2)
114 #define POOL_LTYPE(x) (((x) & 0x3) << 3)
115 #define POOL_BUF_OFFSET(x) (((x) & 0x7fffULL) << 16)
116 #define POOL_BUF_SIZE(x) (((x) & 0x7ffULL) << 32)
119 void *pool_stack_base;
121 uint64_t stack_ln_ptr;
123 uint16_t vf_id; /* gpool_id */
124 uint16_t sz128; /* Block size in cache lines */
128 struct octeontx_fpadev {
130 uint8_t total_gpool_cnt;
131 struct fpavf_res pool[FPA_VF_MAX];
134 static struct octeontx_fpadev fpadev;
136 /* lock is taken by caller */
138 octeontx_fpa_gpool_alloc(unsigned int object_size)
140 struct fpavf_res *res = NULL;
144 sz128 = FPA_OBJSZ_2_CACHE_LINE(object_size);
146 for (gpool = 0; gpool < FPA_VF_MAX; gpool++) {
148 /* Skip VF that is not mapped Or _inuse */
149 if ((fpadev.pool[gpool].bar0 == NULL) ||
150 (fpadev.pool[gpool].is_inuse == true))
153 res = &fpadev.pool[gpool];
155 RTE_ASSERT(res->domain_id != (uint16_t)~0);
156 RTE_ASSERT(res->vf_id != (uint16_t)~0);
157 RTE_ASSERT(res->stack_ln_ptr != 0);
159 if (res->sz128 == 0) {
162 fpavf_log_dbg("gpool %d blk_sz %d\n", gpool, sz128);
170 /* lock is taken by caller */
171 static __rte_always_inline uintptr_t
172 octeontx_fpa_gpool2handle(uint16_t gpool)
174 struct fpavf_res *res = NULL;
176 RTE_ASSERT(gpool < FPA_VF_MAX);
178 res = &fpadev.pool[gpool];
179 return (uintptr_t)res->bar0 | gpool;
182 static __rte_always_inline bool
183 octeontx_fpa_handle_valid(uintptr_t handle)
185 struct fpavf_res *res = NULL;
190 if (unlikely(!handle))
194 gpool = octeontx_fpa_bufpool_gpool(handle);
196 /* get the bar address */
197 handle &= ~(uint64_t)FPA_GPOOL_MASK;
198 for (i = 0; i < FPA_VF_MAX; i++) {
199 if ((uintptr_t)fpadev.pool[i].bar0 != handle)
206 res = &fpadev.pool[i];
208 if (res->sz128 == 0 || res->domain_id == (uint16_t)~0 ||
209 res->stack_ln_ptr == 0)
220 octeontx_fpapf_pool_setup(unsigned int gpool, unsigned int buf_size,
221 signed short buf_offset, unsigned int max_buf_count)
224 phys_addr_t phys_addr;
226 struct fpavf_res *fpa = NULL;
228 struct octeontx_mbox_hdr hdr;
229 struct dcfg_resp resp;
230 struct octeontx_mbox_fpa_cfg cfg;
233 fpa = &fpadev.pool[gpool];
234 memsz = FPA_ROUND_UP(max_buf_count / fpa->stack_ln_ptr, FPA_LN_SIZE) *
237 /* Round-up to page size */
238 memsz = (memsz + FPA_PF_PAGE_SZ - 1) & ~(uintptr_t)(FPA_PF_PAGE_SZ-1);
239 memptr = rte_malloc(NULL, memsz, RTE_CACHE_LINE_SIZE);
240 if (memptr == NULL) {
245 /* Configure stack */
246 fpa->pool_stack_base = memptr;
247 phys_addr = rte_malloc_virt2phy(memptr);
249 buf_size /= FPA_LN_SIZE;
252 hdr.coproc = FPA_COPROC;
253 hdr.msg = FPA_CONFIGSET;
254 hdr.vfid = fpa->vf_id;
257 buf_offset /= FPA_LN_SIZE;
258 reg = POOL_BUF_SIZE(buf_size) | POOL_BUF_OFFSET(buf_offset) |
259 POOL_LTYPE(0x2) | POOL_STYPE(0) | POOL_SET_NAT_ALIGN |
264 cfg.pool_stack_base = phys_addr;
265 cfg.pool_stack_end = phys_addr + memsz;
266 cfg.aura_cfg = (1 << 9);
268 ret = octeontx_ssovf_mbox_send(&hdr, &cfg,
269 sizeof(struct octeontx_mbox_fpa_cfg),
270 &resp, sizeof(resp));
276 fpavf_log_dbg(" vfid %d gpool %d aid %d pool_cfg 0x%x pool_stack_base %" PRIx64 " pool_stack_end %" PRIx64" aura_cfg %" PRIx64 "\n",
277 fpa->vf_id, gpool, cfg.aid, (unsigned int)cfg.pool_cfg,
278 cfg.pool_stack_base, cfg.pool_stack_end, cfg.aura_cfg);
280 /* Now pool is in_use */
281 fpa->is_inuse = true;
291 octeontx_fpapf_pool_destroy(unsigned int gpool_index)
293 struct octeontx_mbox_hdr hdr;
294 struct dcfg_resp resp;
295 struct octeontx_mbox_fpa_cfg cfg;
296 struct fpavf_res *fpa = NULL;
299 fpa = &fpadev.pool[gpool_index];
301 hdr.coproc = FPA_COPROC;
302 hdr.msg = FPA_CONFIGSET;
303 hdr.vfid = fpa->vf_id;
306 /* reset and free the pool */
309 cfg.pool_stack_base = 0;
310 cfg.pool_stack_end = 0;
313 ret = octeontx_ssovf_mbox_send(&hdr, &cfg,
314 sizeof(struct octeontx_mbox_fpa_cfg),
315 &resp, sizeof(resp));
323 /* anycase free pool stack memory */
324 rte_free(fpa->pool_stack_base);
325 fpa->pool_stack_base = NULL;
330 octeontx_fpapf_aura_attach(unsigned int gpool_index)
332 struct octeontx_mbox_hdr hdr;
333 struct dcfg_resp resp;
334 struct octeontx_mbox_fpa_cfg cfg;
337 if (gpool_index >= FPA_MAX_POOL) {
341 hdr.coproc = FPA_COPROC;
342 hdr.msg = FPA_ATTACHAURA;
343 hdr.vfid = gpool_index;
345 memset(&cfg, 0x0, sizeof(struct octeontx_mbox_fpa_cfg));
346 cfg.aid = gpool_index; /* gpool is guara */
348 ret = octeontx_ssovf_mbox_send(&hdr, &cfg,
349 sizeof(struct octeontx_mbox_fpa_cfg),
350 &resp, sizeof(resp));
352 fpavf_log_err("Could not attach fpa ");
353 fpavf_log_err("aura %d to pool %d. Err=%d. FuncErr=%d\n",
354 gpool_index, gpool_index, ret, hdr.res_code);
363 octeontx_fpapf_aura_detach(unsigned int gpool_index)
365 struct octeontx_mbox_fpa_cfg cfg = {0};
366 struct octeontx_mbox_hdr hdr = {0};
369 if (gpool_index >= FPA_MAX_POOL) {
374 cfg.aid = gpool_index; /* gpool is gaura */
375 hdr.coproc = FPA_COPROC;
376 hdr.msg = FPA_DETACHAURA;
377 hdr.vfid = gpool_index;
378 ret = octeontx_ssovf_mbox_send(&hdr, &cfg, sizeof(cfg), NULL, 0);
380 fpavf_log_err("Couldn't detach FPA aura %d Err=%d FuncErr=%d\n",
381 gpool_index, ret, hdr.res_code);
390 octeontx_fpavf_pool_setup(uintptr_t handle, unsigned long memsz,
391 void *memva, uint16_t gpool)
395 if (unlikely(!handle))
398 va_end = (uintptr_t)memva + memsz;
399 va_end &= ~RTE_CACHE_LINE_MASK;
402 fpavf_write64((uintptr_t)memva,
403 (void *)((uintptr_t)handle +
404 FPA_VF_VHPOOL_START_ADDR(gpool)));
405 fpavf_write64(va_end,
406 (void *)((uintptr_t)handle +
407 FPA_VF_VHPOOL_END_ADDR(gpool)));
412 octeontx_fpapf_start_count(uint16_t gpool_index)
415 struct octeontx_mbox_hdr hdr = {0};
417 if (gpool_index >= FPA_MAX_POOL) {
422 hdr.coproc = FPA_COPROC;
423 hdr.msg = FPA_START_COUNT;
424 hdr.vfid = gpool_index;
425 ret = octeontx_ssovf_mbox_send(&hdr, NULL, 0, NULL, 0);
427 fpavf_log_err("Could not start buffer counting for ");
428 fpavf_log_err("FPA pool %d. Err=%d. FuncErr=%d\n",
429 gpool_index, ret, hdr.res_code);
438 static __rte_always_inline int
439 octeontx_fpavf_free(unsigned int gpool)
443 if (gpool >= FPA_MAX_POOL) {
449 fpadev.pool[gpool].is_inuse = false;
455 static __rte_always_inline int
456 octeontx_gpool_free(uint16_t gpool)
458 if (fpadev.pool[gpool].sz128 != 0) {
459 fpadev.pool[gpool].sz128 = 0;
466 * Return buffer size for a given pool
469 octeontx_fpa_bufpool_block_size(uintptr_t handle)
471 struct fpavf_res *res = NULL;
474 if (unlikely(!octeontx_fpa_handle_valid(handle)))
478 gpool = octeontx_fpa_bufpool_gpool(handle);
479 res = &fpadev.pool[gpool];
480 return FPA_CACHE_LINE_2_OBJSZ(res->sz128);
484 octeontx_fpa_bufpool_free_count(uintptr_t handle)
486 uint64_t cnt, limit, avail;
490 if (unlikely(!octeontx_fpa_handle_valid(handle)))
494 gpool = octeontx_fpa_bufpool_gpool(handle);
496 /* Get pool bar address from handle */
497 pool_bar = handle & ~(uint64_t)FPA_GPOOL_MASK;
499 cnt = fpavf_read64((void *)((uintptr_t)pool_bar +
500 FPA_VF_VHAURA_CNT(gpool)));
501 limit = fpavf_read64((void *)((uintptr_t)pool_bar +
502 FPA_VF_VHAURA_CNT_LIMIT(gpool)));
504 avail = fpavf_read64((void *)((uintptr_t)pool_bar +
505 FPA_VF_VHPOOL_AVAILABLE(gpool)));
507 return RTE_MIN(avail, (limit - cnt));
511 octeontx_fpa_bufpool_create(unsigned int object_size, unsigned int object_count,
512 unsigned int buf_offset, char **va_start,
518 uintptr_t gpool_handle;
522 RTE_SET_USED(node_id);
523 RTE_BUILD_BUG_ON(sizeof(struct rte_mbuf) > OCTEONTX_FPAVF_BUF_OFFSET);
525 if (unlikely(*va_start == NULL))
528 object_size = RTE_CACHE_LINE_ROUNDUP(object_size);
529 if (object_size > FPA_MAX_OBJ_SIZE) {
534 rte_spinlock_lock(&fpadev.lock);
535 res = octeontx_fpa_gpool_alloc(object_size);
538 if (unlikely(res < 0)) {
546 /* get pool handle */
547 gpool_handle = octeontx_fpa_gpool2handle(gpool);
548 if (!octeontx_fpa_handle_valid(gpool_handle)) {
550 goto error_gpool_free;
553 /* Get pool bar address from handle */
554 pool_bar = gpool_handle & ~(uint64_t)FPA_GPOOL_MASK;
556 res = octeontx_fpapf_pool_setup(gpool, object_size, buf_offset,
560 goto error_gpool_free;
563 /* populate AURA fields */
564 res = octeontx_fpapf_aura_attach(gpool);
567 goto error_pool_destroy;
571 memsz = object_size * object_count;
573 res = octeontx_fpavf_pool_setup(pool_bar, memsz, memva, gpool);
576 goto error_gaura_detach;
580 rte_spinlock_unlock(&fpadev.lock);
582 /* populate AURA registers */
583 fpavf_write64(object_count, (void *)((uintptr_t)pool_bar +
584 FPA_VF_VHAURA_CNT(gpool)));
585 fpavf_write64(object_count, (void *)((uintptr_t)pool_bar +
586 FPA_VF_VHAURA_CNT_LIMIT(gpool)));
587 fpavf_write64(object_count + 1, (void *)((uintptr_t)pool_bar +
588 FPA_VF_VHAURA_CNT_THRESHOLD(gpool)));
590 octeontx_fpapf_start_count(gpool);
595 (void) octeontx_fpapf_aura_detach(gpool);
597 octeontx_fpavf_free(gpool);
598 octeontx_fpapf_pool_destroy(gpool);
600 octeontx_gpool_free(gpool);
602 rte_spinlock_unlock(&fpadev.lock);
604 return (uintptr_t)NULL;
608 * Destroy a buffer pool.
611 octeontx_fpa_bufpool_destroy(uintptr_t handle, int node_id)
613 void **node, **curr, *head = NULL;
620 RTE_SET_USED(node_id);
622 /* Wait for all outstanding writes to be committed */
625 if (unlikely(!octeontx_fpa_handle_valid(handle)))
629 gpool = octeontx_fpa_bufpool_gpool(handle);
631 /* Get pool bar address from handle */
632 pool_bar = handle & ~(uint64_t)FPA_GPOOL_MASK;
634 /* Check for no outstanding buffers */
635 cnt = fpavf_read64((void *)((uintptr_t)pool_bar +
636 FPA_VF_VHAURA_CNT(gpool)));
638 fpavf_log_dbg("buffer exist in pool cnt %" PRId64 "\n", cnt);
642 rte_spinlock_lock(&fpadev.lock);
644 avail = fpavf_read64((void *)((uintptr_t)pool_bar +
645 FPA_VF_VHPOOL_AVAILABLE(gpool)));
647 /* Prepare to empty the entire POOL */
648 fpavf_write64(avail, (void *)((uintptr_t)pool_bar +
649 FPA_VF_VHAURA_CNT_LIMIT(gpool)));
650 fpavf_write64(avail + 1, (void *)((uintptr_t)pool_bar +
651 FPA_VF_VHAURA_CNT_THRESHOLD(gpool)));
654 /* Invalidate the POOL */
655 octeontx_gpool_free(gpool);
657 /* Process all buffers in the pool */
660 /* Yank a buffer from the pool */
661 node = (void *)(uintptr_t)
662 fpavf_read64((void *)
663 (pool_bar + FPA_VF_VHAURA_OP_ALLOC(gpool)));
666 fpavf_log_err("GAURA[%u] missing %" PRIx64 " buf\n",
671 /* Imsert it into an ordered linked list */
672 for (curr = &head; curr[0] != NULL; curr = curr[0]) {
673 if ((uintptr_t)node <= (uintptr_t)curr[0])
680 /* Verify the linked list to be a perfect series */
681 sz = octeontx_fpa_bufpool_block_size(handle) << 7;
682 for (curr = head; curr != NULL && curr[0] != NULL;
684 if (curr == curr[0] ||
685 ((uintptr_t)curr != ((uintptr_t)curr[0] - sz))) {
686 fpavf_log_err("POOL# %u buf sequence err (%p vs. %p)\n",
687 gpool, curr, curr[0]);
691 /* Disable pool operation */
692 fpavf_write64(~0ul, (void *)((uintptr_t)pool_bar +
693 FPA_VF_VHPOOL_START_ADDR(gpool)));
694 fpavf_write64(~0ul, (void *)((uintptr_t)pool_bar +
695 FPA_VF_VHPOOL_END_ADDR(gpool)));
697 (void)octeontx_fpapf_pool_destroy(gpool);
699 /* Deactivate the AURA */
700 fpavf_write64(0, (void *)((uintptr_t)pool_bar +
701 FPA_VF_VHAURA_CNT_LIMIT(gpool)));
702 fpavf_write64(0, (void *)((uintptr_t)pool_bar +
703 FPA_VF_VHAURA_CNT_THRESHOLD(gpool)));
705 ret = octeontx_fpapf_aura_detach(gpool);
707 fpavf_log_err("Failed to dettach gaura %u. error code=%d\n",
712 (void)octeontx_fpavf_free(gpool);
714 rte_spinlock_unlock(&fpadev.lock);
719 octeontx_fpavf_setup(void)
722 static bool init_once;
725 rte_spinlock_init(&fpadev.lock);
726 fpadev.total_gpool_cnt = 0;
728 for (i = 0; i < FPA_VF_MAX; i++) {
730 fpadev.pool[i].domain_id = ~0;
731 fpadev.pool[i].stack_ln_ptr = 0;
732 fpadev.pool[i].sz128 = 0;
733 fpadev.pool[i].bar0 = NULL;
734 fpadev.pool[i].pool_stack_base = NULL;
735 fpadev.pool[i].is_inuse = false;
742 octeontx_fpavf_identify(void *bar0)
747 uint64_t stack_ln_ptr;
749 val = fpavf_read64((void *)((uintptr_t)bar0 +
750 FPA_VF_VHAURA_CNT_THRESHOLD(0)));
752 domain_id = (val >> 8) & 0xffff;
753 vf_id = (val >> 24) & 0xffff;
755 stack_ln_ptr = fpavf_read64((void *)((uintptr_t)bar0 +
756 FPA_VF_VHPOOL_THRESHOLD(0)));
757 if (vf_id >= FPA_VF_MAX) {
758 fpavf_log_err("vf_id(%d) greater than max vf (32)\n", vf_id);
762 if (fpadev.pool[vf_id].is_inuse) {
763 fpavf_log_err("vf_id %d is_inuse\n", vf_id);
767 fpadev.pool[vf_id].domain_id = domain_id;
768 fpadev.pool[vf_id].vf_id = vf_id;
769 fpadev.pool[vf_id].bar0 = bar0;
770 fpadev.pool[vf_id].stack_ln_ptr = stack_ln_ptr;
776 /* FPAVF pcie device aka mempool probe */
778 fpavf_probe(struct rte_pci_driver *pci_drv, struct rte_pci_device *pci_dev)
782 struct fpavf_res *fpa = NULL;
784 RTE_SET_USED(pci_drv);
787 /* For secondary processes, the primary has done all the work */
788 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
791 if (pci_dev->mem_resource[0].addr == NULL) {
792 fpavf_log_err("Empty bars %p ", pci_dev->mem_resource[0].addr);
795 idreg = pci_dev->mem_resource[0].addr;
797 octeontx_fpavf_setup();
799 res = octeontx_fpavf_identify(idreg);
803 fpa = &fpadev.pool[res];
804 fpadev.total_gpool_cnt++;
807 fpavf_log_dbg("total_fpavfs %d bar0 %p domain %d vf %d stk_ln_ptr 0x%x",
808 fpadev.total_gpool_cnt, fpa->bar0, fpa->domain_id,
809 fpa->vf_id, (unsigned int)fpa->stack_ln_ptr);
814 static const struct rte_pci_id pci_fpavf_map[] = {
816 RTE_PCI_DEVICE(PCI_VENDOR_ID_CAVIUM,
817 PCI_DEVICE_ID_OCTEONTX_FPA_VF)
824 static struct rte_pci_driver pci_fpavf = {
825 .id_table = pci_fpavf_map,
826 .drv_flags = RTE_PCI_DRV_NEED_MAPPING | RTE_PCI_DRV_IOVA_AS_VA,
827 .probe = fpavf_probe,
830 RTE_PMD_REGISTER_PCI(octeontx_fpavf, pci_fpavf);