fix same typo in multiple places
[dpdk.git] / drivers / mempool / dpaa2 / dpaa2_hw_mempool.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  *
3  *   Copyright (c) 2016 Freescale Semiconductor, Inc. All rights reserved.
4  *   Copyright 2016-2019 NXP
5  *
6  */
7
8 #include <unistd.h>
9 #include <stdio.h>
10 #include <sys/types.h>
11 #include <string.h>
12 #include <stdlib.h>
13 #include <fcntl.h>
14 #include <errno.h>
15
16 #include <rte_mbuf.h>
17 #include <rte_ethdev_driver.h>
18 #include <rte_malloc.h>
19 #include <rte_memcpy.h>
20 #include <rte_string_fns.h>
21 #include <rte_cycles.h>
22 #include <rte_kvargs.h>
23 #include <rte_dev.h>
24 #include "rte_dpaa2_mempool.h"
25
26 #include "fslmc_vfio.h"
27 #include <fslmc_logs.h>
28 #include <mc/fsl_dpbp.h>
29 #include <portal/dpaa2_hw_pvt.h>
30 #include <portal/dpaa2_hw_dpio.h>
31 #include "dpaa2_hw_mempool.h"
32 #include "dpaa2_hw_mempool_logs.h"
33
34 #include <dpaax_iova_table.h>
35
36 struct dpaa2_bp_info *rte_dpaa2_bpid_info;
37 static struct dpaa2_bp_list *h_bp_list;
38
39 /* Dynamic logging identified for mempool */
40 int dpaa2_logtype_mempool;
41
42 static int
43 rte_hw_mbuf_create_pool(struct rte_mempool *mp)
44 {
45         struct dpaa2_bp_list *bp_list;
46         struct dpaa2_dpbp_dev *avail_dpbp;
47         struct dpaa2_bp_info *bp_info;
48         struct dpbp_attr dpbp_attr;
49         uint32_t bpid;
50         int ret;
51
52         avail_dpbp = dpaa2_alloc_dpbp_dev();
53
54         if (rte_dpaa2_bpid_info == NULL) {
55                 rte_dpaa2_bpid_info = (struct dpaa2_bp_info *)rte_malloc(NULL,
56                                       sizeof(struct dpaa2_bp_info) * MAX_BPID,
57                                       RTE_CACHE_LINE_SIZE);
58                 if (rte_dpaa2_bpid_info == NULL)
59                         return -ENOMEM;
60                 memset(rte_dpaa2_bpid_info, 0,
61                        sizeof(struct dpaa2_bp_info) * MAX_BPID);
62         }
63
64         if (!avail_dpbp) {
65                 DPAA2_MEMPOOL_ERR("DPAA2 pool not available!");
66                 return -ENOENT;
67         }
68
69         if (unlikely(!DPAA2_PER_LCORE_DPIO)) {
70                 ret = dpaa2_affine_qbman_swp();
71                 if (ret) {
72                         DPAA2_MEMPOOL_ERR(
73                                 "Failed to allocate IO portal, tid: %d\n",
74                                 rte_gettid());
75                         goto err1;
76                 }
77         }
78
79         ret = dpbp_enable(&avail_dpbp->dpbp, CMD_PRI_LOW, avail_dpbp->token);
80         if (ret != 0) {
81                 DPAA2_MEMPOOL_ERR("Resource enable failure with err code: %d",
82                                   ret);
83                 goto err1;
84         }
85
86         ret = dpbp_get_attributes(&avail_dpbp->dpbp, CMD_PRI_LOW,
87                                   avail_dpbp->token, &dpbp_attr);
88         if (ret != 0) {
89                 DPAA2_MEMPOOL_ERR("Resource read failure with err code: %d",
90                                   ret);
91                 goto err2;
92         }
93
94         bp_info = rte_malloc(NULL,
95                              sizeof(struct dpaa2_bp_info),
96                              RTE_CACHE_LINE_SIZE);
97         if (!bp_info) {
98                 DPAA2_MEMPOOL_ERR("Unable to allocate buffer pool memory");
99                 ret = -ENOMEM;
100                 goto err2;
101         }
102
103         /* Allocate the bp_list which will be added into global_bp_list */
104         bp_list = rte_malloc(NULL, sizeof(struct dpaa2_bp_list),
105                              RTE_CACHE_LINE_SIZE);
106         if (!bp_list) {
107                 DPAA2_MEMPOOL_ERR("Unable to allocate buffer pool memory");
108                 ret = -ENOMEM;
109                 goto err3;
110         }
111
112         /* Set parameters of buffer pool list */
113         bp_list->buf_pool.num_bufs = mp->size;
114         bp_list->buf_pool.size = mp->elt_size
115                         - sizeof(struct rte_mbuf) - rte_pktmbuf_priv_size(mp);
116         bp_list->buf_pool.bpid = dpbp_attr.bpid;
117         bp_list->buf_pool.h_bpool_mem = NULL;
118         bp_list->buf_pool.dpbp_node = avail_dpbp;
119         /* Identification for our offloaded pool_data structure */
120         bp_list->dpaa2_ops_index = mp->ops_index;
121         bp_list->next = h_bp_list;
122         bp_list->mp = mp;
123
124         bpid = dpbp_attr.bpid;
125
126         rte_dpaa2_bpid_info[bpid].meta_data_size = sizeof(struct rte_mbuf)
127                                 + rte_pktmbuf_priv_size(mp);
128         rte_dpaa2_bpid_info[bpid].bp_list = bp_list;
129         rte_dpaa2_bpid_info[bpid].bpid = bpid;
130
131         rte_memcpy(bp_info, (void *)&rte_dpaa2_bpid_info[bpid],
132                    sizeof(struct dpaa2_bp_info));
133         mp->pool_data = (void *)bp_info;
134
135         DPAA2_MEMPOOL_DEBUG("BP List created for bpid =%d", dpbp_attr.bpid);
136
137         h_bp_list = bp_list;
138         return 0;
139 err3:
140         rte_free(bp_info);
141 err2:
142         dpbp_disable(&avail_dpbp->dpbp, CMD_PRI_LOW, avail_dpbp->token);
143 err1:
144         dpaa2_free_dpbp_dev(avail_dpbp);
145
146         return ret;
147 }
148
149 static void
150 rte_hw_mbuf_free_pool(struct rte_mempool *mp)
151 {
152         struct dpaa2_bp_info *bpinfo;
153         struct dpaa2_bp_list *bp;
154         struct dpaa2_dpbp_dev *dpbp_node;
155
156         if (!mp->pool_data) {
157                 DPAA2_MEMPOOL_ERR("Not a valid dpaa2 buffer pool");
158                 return;
159         }
160
161         bpinfo = (struct dpaa2_bp_info *)mp->pool_data;
162         bp = bpinfo->bp_list;
163         dpbp_node = bp->buf_pool.dpbp_node;
164
165         dpbp_disable(&(dpbp_node->dpbp), CMD_PRI_LOW, dpbp_node->token);
166
167         if (h_bp_list == bp) {
168                 h_bp_list = h_bp_list->next;
169         } else { /* if it is not the first node */
170                 struct dpaa2_bp_list *prev = h_bp_list, *temp;
171                 temp = h_bp_list->next;
172                 while (temp) {
173                         if (temp == bp) {
174                                 prev->next = temp->next;
175                                 rte_free(bp);
176                                 break;
177                         }
178                         prev = temp;
179                         temp = temp->next;
180                 }
181         }
182
183         rte_free(mp->pool_data);
184         dpaa2_free_dpbp_dev(dpbp_node);
185 }
186
187 static void
188 rte_dpaa2_mbuf_release(struct rte_mempool *pool __rte_unused,
189                         void * const *obj_table,
190                         uint32_t bpid,
191                         uint32_t meta_data_size,
192                         int count)
193 {
194         struct qbman_release_desc releasedesc;
195         struct qbman_swp *swp;
196         int ret;
197         int i, n, retry_count;
198         uint64_t bufs[DPAA2_MBUF_MAX_ACQ_REL];
199
200         if (unlikely(!DPAA2_PER_LCORE_DPIO)) {
201                 ret = dpaa2_affine_qbman_swp();
202                 if (ret != 0) {
203                         DPAA2_MEMPOOL_ERR(
204                                 "Failed to allocate IO portal, tid: %d\n",
205                                 rte_gettid());
206                         return;
207                 }
208         }
209         swp = DPAA2_PER_LCORE_PORTAL;
210
211         /* Create a release descriptor required for releasing
212          * buffers into QBMAN
213          */
214         qbman_release_desc_clear(&releasedesc);
215         qbman_release_desc_set_bpid(&releasedesc, bpid);
216
217         n = count % DPAA2_MBUF_MAX_ACQ_REL;
218         if (unlikely(!n))
219                 goto aligned;
220
221         /* convert mbuf to buffers for the remainder */
222         for (i = 0; i < n ; i++) {
223 #ifdef RTE_LIBRTE_DPAA2_USE_PHYS_IOVA
224                 bufs[i] = (uint64_t)rte_mempool_virt2iova(obj_table[i])
225                                 + meta_data_size;
226 #else
227                 bufs[i] = (uint64_t)obj_table[i] + meta_data_size;
228 #endif
229         }
230
231         /* feed them to bman */
232         retry_count = 0;
233         while ((ret = qbman_swp_release(swp, &releasedesc, bufs, n)) ==
234                         -EBUSY) {
235                 retry_count++;
236                 if (retry_count > DPAA2_MAX_TX_RETRY_COUNT) {
237                         DPAA2_MEMPOOL_ERR("bman release retry exceeded, low fbpr?");
238                         return;
239                 }
240         }
241
242 aligned:
243         /* if there are more buffers to free */
244         while (n < count) {
245                 /* convert mbuf to buffers */
246                 for (i = 0; i < DPAA2_MBUF_MAX_ACQ_REL; i++) {
247 #ifdef RTE_LIBRTE_DPAA2_USE_PHYS_IOVA
248                         bufs[i] = (uint64_t)
249                                   rte_mempool_virt2iova(obj_table[n + i])
250                                   + meta_data_size;
251 #else
252                         bufs[i] = (uint64_t)obj_table[n + i] + meta_data_size;
253 #endif
254                 }
255
256                 retry_count = 0;
257                 while ((ret = qbman_swp_release(swp, &releasedesc, bufs,
258                                         DPAA2_MBUF_MAX_ACQ_REL)) == -EBUSY) {
259                         retry_count++;
260                         if (retry_count > DPAA2_MAX_TX_RETRY_COUNT) {
261                                 DPAA2_MEMPOOL_ERR("bman release retry exceeded, low fbpr?");
262                                 return;
263                         }
264                 }
265                 n += DPAA2_MBUF_MAX_ACQ_REL;
266         }
267 }
268
269 uint16_t
270 rte_dpaa2_mbuf_pool_bpid(struct rte_mempool *mp)
271 {
272         struct dpaa2_bp_info *bp_info;
273
274         bp_info = mempool_to_bpinfo(mp);
275         if (!(bp_info->bp_list)) {
276                 RTE_LOG(ERR, PMD, "DPAA2 buffer pool not configured\n");
277                 return -ENOMEM;
278         }
279
280         return bp_info->bpid;
281 }
282
283 struct rte_mbuf *
284 rte_dpaa2_mbuf_from_buf_addr(struct rte_mempool *mp, void *buf_addr)
285 {
286         struct dpaa2_bp_info *bp_info;
287
288         bp_info = mempool_to_bpinfo(mp);
289         if (!(bp_info->bp_list)) {
290                 RTE_LOG(ERR, PMD, "DPAA2 buffer pool not configured\n");
291                 return NULL;
292         }
293
294         return (struct rte_mbuf *)((uint8_t *)buf_addr -
295                         bp_info->meta_data_size);
296 }
297
298 int
299 rte_dpaa2_mbuf_alloc_bulk(struct rte_mempool *pool,
300                           void **obj_table, unsigned int count)
301 {
302 #ifdef RTE_LIBRTE_DPAA2_DEBUG_DRIVER
303         static int alloc;
304 #endif
305         struct qbman_swp *swp;
306         uint16_t bpid;
307         size_t bufs[DPAA2_MBUF_MAX_ACQ_REL];
308         int i, ret;
309         unsigned int n = 0;
310         struct dpaa2_bp_info *bp_info;
311
312         bp_info = mempool_to_bpinfo(pool);
313
314         if (!(bp_info->bp_list)) {
315                 DPAA2_MEMPOOL_ERR("DPAA2 buffer pool not configured");
316                 return -ENOENT;
317         }
318
319         bpid = bp_info->bpid;
320
321         if (unlikely(!DPAA2_PER_LCORE_DPIO)) {
322                 ret = dpaa2_affine_qbman_swp();
323                 if (ret != 0) {
324                         DPAA2_MEMPOOL_ERR(
325                                 "Failed to allocate IO portal, tid: %d\n",
326                                 rte_gettid());
327                         return ret;
328                 }
329         }
330         swp = DPAA2_PER_LCORE_PORTAL;
331
332         while (n < count) {
333                 /* Acquire is all-or-nothing, so we drain in 7s,
334                  * then the remainder.
335                  */
336                 if ((count - n) > DPAA2_MBUF_MAX_ACQ_REL) {
337                         ret = qbman_swp_acquire(swp, bpid, (void *)bufs,
338                                                 DPAA2_MBUF_MAX_ACQ_REL);
339                 } else {
340                         ret = qbman_swp_acquire(swp, bpid, (void *)bufs,
341                                                 count - n);
342                 }
343                 /* In case of less than requested number of buffers available
344                  * in pool, qbman_swp_acquire returns 0
345                  */
346                 if (ret <= 0) {
347                         DPAA2_MEMPOOL_DP_DEBUG(
348                                 "Buffer acquire failed with err code: %d", ret);
349                         /* The API expect the exact number of requested bufs */
350                         /* Releasing all buffers allocated */
351                         rte_dpaa2_mbuf_release(pool, obj_table, bpid,
352                                            bp_info->meta_data_size, n);
353                         return -ENOBUFS;
354                 }
355                 /* assigning mbuf from the acquired objects */
356                 for (i = 0; (i < ret) && bufs[i]; i++) {
357                         DPAA2_MODIFY_IOVA_TO_VADDR(bufs[i], size_t);
358                         obj_table[n] = (struct rte_mbuf *)
359                                        (bufs[i] - bp_info->meta_data_size);
360                         DPAA2_MEMPOOL_DP_DEBUG(
361                                    "Acquired %p address %p from BMAN\n",
362                                    (void *)bufs[i], (void *)obj_table[n]);
363                         n++;
364                 }
365         }
366
367 #ifdef RTE_LIBRTE_DPAA2_DEBUG_DRIVER
368         alloc += n;
369         DPAA2_MEMPOOL_DP_DEBUG("Total = %d , req = %d done = %d\n",
370                                alloc, count, n);
371 #endif
372         return 0;
373 }
374
375 static int
376 rte_hw_mbuf_free_bulk(struct rte_mempool *pool,
377                   void * const *obj_table, unsigned int n)
378 {
379         struct dpaa2_bp_info *bp_info;
380
381         bp_info = mempool_to_bpinfo(pool);
382         if (!(bp_info->bp_list)) {
383                 DPAA2_MEMPOOL_ERR("DPAA2 buffer pool not configured");
384                 return -ENOENT;
385         }
386         rte_dpaa2_mbuf_release(pool, obj_table, bp_info->bpid,
387                            bp_info->meta_data_size, n);
388
389         return 0;
390 }
391
392 static unsigned int
393 rte_hw_mbuf_get_count(const struct rte_mempool *mp)
394 {
395         int ret;
396         unsigned int num_of_bufs = 0;
397         struct dpaa2_bp_info *bp_info;
398         struct dpaa2_dpbp_dev *dpbp_node;
399
400         if (!mp || !mp->pool_data) {
401                 DPAA2_MEMPOOL_ERR("Invalid mempool provided");
402                 return 0;
403         }
404
405         bp_info = (struct dpaa2_bp_info *)mp->pool_data;
406         dpbp_node = bp_info->bp_list->buf_pool.dpbp_node;
407
408         ret = dpbp_get_num_free_bufs(&dpbp_node->dpbp, CMD_PRI_LOW,
409                                      dpbp_node->token, &num_of_bufs);
410         if (ret) {
411                 DPAA2_MEMPOOL_ERR("Unable to obtain free buf count (err=%d)",
412                                   ret);
413                 return 0;
414         }
415
416         DPAA2_MEMPOOL_DP_DEBUG("Free bufs = %u\n", num_of_bufs);
417
418         return num_of_bufs;
419 }
420
421 static int
422 dpaa2_populate(struct rte_mempool *mp, unsigned int max_objs,
423               void *vaddr, rte_iova_t paddr, size_t len,
424               rte_mempool_populate_obj_cb_t *obj_cb, void *obj_cb_arg)
425 {
426         struct rte_memseg_list *msl;
427         /* The memsegment list exists incase the memory is not external.
428          * So, DMA-Map is required only when memory is provided by user,
429          * i.e. External.
430          */
431         msl = rte_mem_virt2memseg_list(vaddr);
432
433         if (!msl) {
434                 DPAA2_MEMPOOL_DEBUG("Memsegment is External.\n");
435                 rte_fslmc_vfio_mem_dmamap((size_t)vaddr,
436                                 (size_t)paddr, (size_t)len);
437         }
438         /* Insert entry into the PA->VA Table */
439         dpaax_iova_table_update(paddr, vaddr, len);
440
441         return rte_mempool_op_populate_helper(mp, 0, max_objs, vaddr, paddr,
442                                                len, obj_cb, obj_cb_arg);
443 }
444
445 static const struct rte_mempool_ops dpaa2_mpool_ops = {
446         .name = DPAA2_MEMPOOL_OPS_NAME,
447         .alloc = rte_hw_mbuf_create_pool,
448         .free = rte_hw_mbuf_free_pool,
449         .enqueue = rte_hw_mbuf_free_bulk,
450         .dequeue = rte_dpaa2_mbuf_alloc_bulk,
451         .get_count = rte_hw_mbuf_get_count,
452         .populate = dpaa2_populate,
453 };
454
455 MEMPOOL_REGISTER_OPS(dpaa2_mpool_ops);
456
457 RTE_INIT(dpaa2_mempool_init_log)
458 {
459         dpaa2_logtype_mempool = rte_log_register("mempool.dpaa2");
460         if (dpaa2_logtype_mempool >= 0)
461                 rte_log_set_level(dpaa2_logtype_mempool, RTE_LOG_NOTICE);
462 }