139249c07666a8a630cfb4d05ceed6139e5ecdb6
[dpdk.git] / drivers / bus / fslmc / portal / dpaa2_hw_dpbp.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  *
3  *   Copyright (c) 2016 Freescale Semiconductor, Inc. All rights reserved.
4  *   Copyright 2016 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_malloc.h>
17 #include <rte_memcpy.h>
18 #include <rte_string_fns.h>
19 #include <rte_cycles.h>
20 #include <rte_kvargs.h>
21 #include <rte_dev.h>
22 #include <rte_ethdev_driver.h>
23
24 #include <fslmc_logs.h>
25 #include <rte_fslmc.h>
26 #include <mc/fsl_dpbp.h>
27 #include "portal/dpaa2_hw_pvt.h"
28 #include "portal/dpaa2_hw_dpio.h"
29
30 TAILQ_HEAD(dpbp_dev_list, dpaa2_dpbp_dev);
31 static struct dpbp_dev_list dpbp_dev_list
32         = TAILQ_HEAD_INITIALIZER(dpbp_dev_list); /*!< DPBP device list */
33
34 static int
35 dpaa2_create_dpbp_device(int vdev_fd __rte_unused,
36                          struct vfio_device_info *obj_info __rte_unused,
37                          int dpbp_id)
38 {
39         struct dpaa2_dpbp_dev *dpbp_node;
40         int ret;
41
42         /* Allocate DPAA2 dpbp handle */
43         dpbp_node = rte_malloc(NULL, sizeof(struct dpaa2_dpbp_dev), 0);
44         if (!dpbp_node) {
45                 PMD_INIT_LOG(ERR, "Memory allocation failed for DPBP Device");
46                 return -1;
47         }
48
49         /* Open the dpbp object */
50         dpbp_node->dpbp.regs = rte_mcp_ptr_list[MC_PORTAL_INDEX];
51         ret = dpbp_open(&dpbp_node->dpbp,
52                         CMD_PRI_LOW, dpbp_id, &dpbp_node->token);
53         if (ret) {
54                 PMD_INIT_LOG(ERR, "Resource alloc failure with err code: %d",
55                              ret);
56                 rte_free(dpbp_node);
57                 return -1;
58         }
59
60         /* Clean the device first */
61         ret = dpbp_reset(&dpbp_node->dpbp, CMD_PRI_LOW, dpbp_node->token);
62         if (ret) {
63                 PMD_INIT_LOG(ERR, "Failure cleaning dpbp device with"
64                                         " error code %d\n", ret);
65                 dpbp_close(&dpbp_node->dpbp, CMD_PRI_LOW, dpbp_node->token);
66                 rte_free(dpbp_node);
67                 return -1;
68         }
69
70         dpbp_node->dpbp_id = dpbp_id;
71         rte_atomic16_init(&dpbp_node->in_use);
72
73         TAILQ_INSERT_TAIL(&dpbp_dev_list, dpbp_node, next);
74
75         RTE_LOG(DEBUG, PMD, "DPAA2: Added [dpbp.%d]\n", dpbp_id);
76
77         return 0;
78 }
79
80 struct dpaa2_dpbp_dev *dpaa2_alloc_dpbp_dev(void)
81 {
82         struct dpaa2_dpbp_dev *dpbp_dev = NULL;
83
84         /* Get DPBP dev handle from list using index */
85         TAILQ_FOREACH(dpbp_dev, &dpbp_dev_list, next) {
86                 if (dpbp_dev && rte_atomic16_test_and_set(&dpbp_dev->in_use))
87                         break;
88         }
89
90         return dpbp_dev;
91 }
92
93 void dpaa2_free_dpbp_dev(struct dpaa2_dpbp_dev *dpbp)
94 {
95         struct dpaa2_dpbp_dev *dpbp_dev = NULL;
96
97         /* Match DPBP handle and mark it free */
98         TAILQ_FOREACH(dpbp_dev, &dpbp_dev_list, next) {
99                 if (dpbp_dev == dpbp) {
100                         rte_atomic16_dec(&dpbp_dev->in_use);
101                         return;
102                 }
103         }
104 }
105
106 int dpaa2_dpbp_supported(void)
107 {
108         if (TAILQ_EMPTY(&dpbp_dev_list))
109                 return -1;
110         return 0;
111 }
112
113 static struct rte_dpaa2_object rte_dpaa2_dpbp_obj = {
114         .dev_type = DPAA2_BPOOL,
115         .create = dpaa2_create_dpbp_device,
116 };
117
118 RTE_PMD_REGISTER_DPAA2_OBJECT(dpbp, rte_dpaa2_dpbp_obj);