net/liquidio: add APIs to allocate and free IQ
[dpdk.git] / drivers / net / liquidio / lio_rxtx.c
1 /*
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2017 Cavium, Inc.. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Cavium, Inc. nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER(S) OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #include <rte_ethdev.h>
35 #include <rte_cycles.h>
36 #include <rte_malloc.h>
37
38 #include "lio_logs.h"
39 #include "lio_struct.h"
40 #include "lio_ethdev.h"
41 #include "lio_rxtx.h"
42
43 static void
44 lio_dma_zone_free(struct lio_device *lio_dev, const struct rte_memzone *mz)
45 {
46         const struct rte_memzone *mz_tmp;
47         int ret = 0;
48
49         if (mz == NULL) {
50                 lio_dev_err(lio_dev, "Memzone NULL\n");
51                 return;
52         }
53
54         mz_tmp = rte_memzone_lookup(mz->name);
55         if (mz_tmp == NULL) {
56                 lio_dev_err(lio_dev, "Memzone %s Not Found\n", mz->name);
57                 return;
58         }
59
60         ret = rte_memzone_free(mz);
61         if (ret)
62                 lio_dev_err(lio_dev, "Memzone free Failed ret %d\n", ret);
63 }
64
65 /**
66  *  lio_init_instr_queue()
67  *  @param lio_dev      - pointer to the lio device structure.
68  *  @param txpciq       - queue to be initialized.
69  *
70  *  Called at driver init time for each input queue. iq_conf has the
71  *  configuration parameters for the queue.
72  *
73  *  @return  Success: 0 Failure: -1
74  */
75 static int
76 lio_init_instr_queue(struct lio_device *lio_dev,
77                      union octeon_txpciq txpciq,
78                      uint32_t num_descs, unsigned int socket_id)
79 {
80         uint32_t iq_no = (uint32_t)txpciq.s.q_no;
81         struct lio_instr_queue *iq;
82         uint32_t instr_type;
83         uint32_t q_size;
84
85         instr_type = LIO_IQ_INSTR_TYPE(lio_dev);
86
87         q_size = instr_type * num_descs;
88         iq = lio_dev->instr_queue[iq_no];
89         iq->iq_mz = rte_eth_dma_zone_reserve(lio_dev->eth_dev,
90                                              "instr_queue", iq_no, q_size,
91                                              RTE_CACHE_LINE_SIZE,
92                                              socket_id);
93         if (iq->iq_mz == NULL) {
94                 lio_dev_err(lio_dev, "Cannot allocate memory for instr queue %d\n",
95                             iq_no);
96                 return -1;
97         }
98
99         iq->base_addr_dma = iq->iq_mz->phys_addr;
100         iq->base_addr = (uint8_t *)iq->iq_mz->addr;
101
102         iq->max_count = num_descs;
103
104         /* Initialize a list to holds requests that have been posted to Octeon
105          * but has yet to be fetched by octeon
106          */
107         iq->request_list = rte_zmalloc_socket("request_list",
108                                               sizeof(*iq->request_list) *
109                                                         num_descs,
110                                               RTE_CACHE_LINE_SIZE,
111                                               socket_id);
112         if (iq->request_list == NULL) {
113                 lio_dev_err(lio_dev, "Alloc failed for IQ[%d] nr free list\n",
114                             iq_no);
115                 lio_dma_zone_free(lio_dev, iq->iq_mz);
116                 return -1;
117         }
118
119         lio_dev_dbg(lio_dev, "IQ[%d]: base: %p basedma: %lx count: %d\n",
120                     iq_no, iq->base_addr, (unsigned long)iq->base_addr_dma,
121                     iq->max_count);
122
123         iq->lio_dev = lio_dev;
124         iq->txpciq.txpciq64 = txpciq.txpciq64;
125         iq->fill_cnt = 0;
126         iq->host_write_index = 0;
127         iq->lio_read_index = 0;
128         iq->flush_index = 0;
129
130         rte_atomic64_set(&iq->instr_pending, 0);
131
132         /* Initialize the spinlock for this instruction queue */
133         rte_spinlock_init(&iq->lock);
134         rte_spinlock_init(&iq->post_lock);
135
136         rte_atomic64_clear(&iq->iq_flush_running);
137
138         lio_dev->io_qmask.iq |= (1ULL << iq_no);
139
140         /* Set the 32B/64B mode for each input queue */
141         lio_dev->io_qmask.iq64B |= ((instr_type == 64) << iq_no);
142         iq->iqcmd_64B = (instr_type == 64);
143
144         return 0;
145 }
146
147 int
148 lio_setup_instr_queue0(struct lio_device *lio_dev)
149 {
150         union octeon_txpciq txpciq;
151         uint32_t num_descs = 0;
152         uint32_t iq_no = 0;
153
154         num_descs = LIO_NUM_DEF_TX_DESCS_CFG(lio_dev);
155
156         lio_dev->num_iqs = 0;
157
158         lio_dev->instr_queue[0] = rte_zmalloc(NULL,
159                                         sizeof(struct lio_instr_queue), 0);
160         if (lio_dev->instr_queue[0] == NULL)
161                 return -ENOMEM;
162
163         lio_dev->instr_queue[0]->q_index = 0;
164         lio_dev->instr_queue[0]->app_ctx = (void *)(size_t)0;
165         txpciq.txpciq64 = 0;
166         txpciq.s.q_no = iq_no;
167         txpciq.s.pkind = lio_dev->pfvf_hsword.pkind;
168         txpciq.s.use_qpg = 0;
169         txpciq.s.qpg = 0;
170         if (lio_init_instr_queue(lio_dev, txpciq, num_descs, SOCKET_ID_ANY)) {
171                 rte_free(lio_dev->instr_queue[0]);
172                 lio_dev->instr_queue[0] = NULL;
173                 return -1;
174         }
175
176         lio_dev->num_iqs++;
177
178         return 0;
179 }
180
181 /**
182  *  lio_delete_instr_queue()
183  *  @param lio_dev      - pointer to the lio device structure.
184  *  @param iq_no        - queue to be deleted.
185  *
186  *  Called at driver unload time for each input queue. Deletes all
187  *  allocated resources for the input queue.
188  */
189 static void
190 lio_delete_instr_queue(struct lio_device *lio_dev, uint32_t iq_no)
191 {
192         struct lio_instr_queue *iq = lio_dev->instr_queue[iq_no];
193
194         rte_free(iq->request_list);
195         iq->request_list = NULL;
196         lio_dma_zone_free(lio_dev, iq->iq_mz);
197 }
198
199 void
200 lio_free_instr_queue0(struct lio_device *lio_dev)
201 {
202         lio_delete_instr_queue(lio_dev, 0);
203         rte_free(lio_dev->instr_queue[0]);
204         lio_dev->instr_queue[0] = NULL;
205         lio_dev->num_iqs--;
206 }