1161b5515SJasvinder Singh /* SPDX-License-Identifier: BSD-3-Clause
2161b5515SJasvinder Singh * Copyright(c) 2010-2018 Intel Corporation
3161b5515SJasvinder Singh */
4161b5515SJasvinder Singh
5161b5515SJasvinder Singh #include <stdlib.h>
6161b5515SJasvinder Singh #include <string.h>
7161b5515SJasvinder Singh
8161b5515SJasvinder Singh #include <rte_string_fns.h>
93edc242dSJasvinder Singh #include <rte_tailq.h>
10161b5515SJasvinder Singh
11161b5515SJasvinder Singh #include "rte_eth_softnic_internals.h"
12161b5515SJasvinder Singh
13161b5515SJasvinder Singh int
softnic_swq_init(struct pmd_internals * p)14161b5515SJasvinder Singh softnic_swq_init(struct pmd_internals *p)
15161b5515SJasvinder Singh {
16161b5515SJasvinder Singh TAILQ_INIT(&p->swq_list);
17161b5515SJasvinder Singh
18161b5515SJasvinder Singh return 0;
19161b5515SJasvinder Singh }
20161b5515SJasvinder Singh
21161b5515SJasvinder Singh void
softnic_swq_free(struct pmd_internals * p)22161b5515SJasvinder Singh softnic_swq_free(struct pmd_internals *p)
23161b5515SJasvinder Singh {
24161b5515SJasvinder Singh for ( ; ; ) {
25161b5515SJasvinder Singh struct softnic_swq *swq;
26161b5515SJasvinder Singh
27161b5515SJasvinder Singh swq = TAILQ_FIRST(&p->swq_list);
28161b5515SJasvinder Singh if (swq == NULL)
29161b5515SJasvinder Singh break;
30161b5515SJasvinder Singh
31161b5515SJasvinder Singh TAILQ_REMOVE(&p->swq_list, swq, node);
32161b5515SJasvinder Singh rte_ring_free(swq->r);
33161b5515SJasvinder Singh free(swq);
34161b5515SJasvinder Singh }
35161b5515SJasvinder Singh }
36161b5515SJasvinder Singh
37bef50bcbSJasvinder Singh void
softnic_softnic_swq_free_keep_rxq_txq(struct pmd_internals * p)38bef50bcbSJasvinder Singh softnic_softnic_swq_free_keep_rxq_txq(struct pmd_internals *p)
39bef50bcbSJasvinder Singh {
403edc242dSJasvinder Singh struct softnic_swq *swq, *tswq;
41bef50bcbSJasvinder Singh
42*f1f6ebc0SWilliam Tu RTE_TAILQ_FOREACH_SAFE(swq, &p->swq_list, node, tswq) {
43bef50bcbSJasvinder Singh if ((strncmp(swq->name, "RXQ", strlen("RXQ")) == 0) ||
44bef50bcbSJasvinder Singh (strncmp(swq->name, "TXQ", strlen("TXQ")) == 0))
45bef50bcbSJasvinder Singh continue;
46bef50bcbSJasvinder Singh
47bef50bcbSJasvinder Singh TAILQ_REMOVE(&p->swq_list, swq, node);
48bef50bcbSJasvinder Singh rte_ring_free(swq->r);
49bef50bcbSJasvinder Singh free(swq);
50bef50bcbSJasvinder Singh }
51bef50bcbSJasvinder Singh }
52bef50bcbSJasvinder Singh
53161b5515SJasvinder Singh struct softnic_swq *
softnic_swq_find(struct pmd_internals * p,const char * name)54161b5515SJasvinder Singh softnic_swq_find(struct pmd_internals *p,
55161b5515SJasvinder Singh const char *name)
56161b5515SJasvinder Singh {
57161b5515SJasvinder Singh struct softnic_swq *swq;
58161b5515SJasvinder Singh
59161b5515SJasvinder Singh if (name == NULL)
60161b5515SJasvinder Singh return NULL;
61161b5515SJasvinder Singh
62161b5515SJasvinder Singh TAILQ_FOREACH(swq, &p->swq_list, node)
63161b5515SJasvinder Singh if (strcmp(swq->name, name) == 0)
64161b5515SJasvinder Singh return swq;
65161b5515SJasvinder Singh
66161b5515SJasvinder Singh return NULL;
67161b5515SJasvinder Singh }
68161b5515SJasvinder Singh
69161b5515SJasvinder Singh struct softnic_swq *
softnic_swq_create(struct pmd_internals * p,const char * name,struct softnic_swq_params * params)70161b5515SJasvinder Singh softnic_swq_create(struct pmd_internals *p,
71161b5515SJasvinder Singh const char *name,
72161b5515SJasvinder Singh struct softnic_swq_params *params)
73161b5515SJasvinder Singh {
74161b5515SJasvinder Singh char ring_name[NAME_SIZE];
75161b5515SJasvinder Singh struct softnic_swq *swq;
76161b5515SJasvinder Singh struct rte_ring *r;
77161b5515SJasvinder Singh unsigned int flags = RING_F_SP_ENQ | RING_F_SC_DEQ;
78161b5515SJasvinder Singh
79161b5515SJasvinder Singh /* Check input params */
80161b5515SJasvinder Singh if (name == NULL ||
81161b5515SJasvinder Singh softnic_swq_find(p, name) ||
82161b5515SJasvinder Singh params == NULL ||
83161b5515SJasvinder Singh params->size == 0)
84161b5515SJasvinder Singh return NULL;
85161b5515SJasvinder Singh
86161b5515SJasvinder Singh /* Resource create */
87161b5515SJasvinder Singh snprintf(ring_name, sizeof(ring_name), "%s_%s",
88161b5515SJasvinder Singh p->params.name,
89161b5515SJasvinder Singh name);
90161b5515SJasvinder Singh
91161b5515SJasvinder Singh r = rte_ring_create(ring_name,
92161b5515SJasvinder Singh params->size,
93161b5515SJasvinder Singh p->params.cpu_id,
94161b5515SJasvinder Singh flags);
95161b5515SJasvinder Singh
96161b5515SJasvinder Singh if (r == NULL)
97161b5515SJasvinder Singh return NULL;
98161b5515SJasvinder Singh
99161b5515SJasvinder Singh /* Node allocation */
100161b5515SJasvinder Singh swq = calloc(1, sizeof(struct softnic_swq));
101161b5515SJasvinder Singh if (swq == NULL) {
102161b5515SJasvinder Singh rte_ring_free(r);
103161b5515SJasvinder Singh return NULL;
104161b5515SJasvinder Singh }
105161b5515SJasvinder Singh
106161b5515SJasvinder Singh /* Node fill in */
107161b5515SJasvinder Singh strlcpy(swq->name, name, sizeof(swq->name));
108161b5515SJasvinder Singh swq->r = r;
109161b5515SJasvinder Singh
110161b5515SJasvinder Singh /* Node add to list */
111161b5515SJasvinder Singh TAILQ_INSERT_TAIL(&p->swq_list, swq, node);
112161b5515SJasvinder Singh
113161b5515SJasvinder Singh return swq;
114161b5515SJasvinder Singh }
115