33f9eedfcdfdad315df80c598e469c7de18c38f4
[deb_dpdk.git] / drivers / bus / fslmc / portal / dpaa2_hw_dpbp.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright (c) 2016 Freescale Semiconductor, Inc. All rights reserved.
5  *   Copyright 2016 NXP.
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 Freescale Semiconductor, 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 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 <unistd.h>
35 #include <stdio.h>
36 #include <sys/types.h>
37 #include <string.h>
38 #include <stdlib.h>
39 #include <fcntl.h>
40 #include <errno.h>
41
42 #include <rte_malloc.h>
43 #include <rte_memcpy.h>
44 #include <rte_string_fns.h>
45 #include <rte_cycles.h>
46 #include <rte_kvargs.h>
47 #include <rte_dev.h>
48 #include <rte_ethdev.h>
49
50 #include <fslmc_logs.h>
51 #include <fslmc_vfio.h>
52 #include <mc/fsl_dpbp.h>
53 #include "portal/dpaa2_hw_pvt.h"
54 #include "portal/dpaa2_hw_dpio.h"
55
56 TAILQ_HEAD(dpbp_dev_list, dpaa2_dpbp_dev);
57 static struct dpbp_dev_list dpbp_dev_list
58         = TAILQ_HEAD_INITIALIZER(dpbp_dev_list); /*!< DPBP device list */
59
60 static int
61 dpaa2_create_dpbp_device(struct fslmc_vfio_device *vdev __rte_unused,
62                          struct vfio_device_info *obj_info __rte_unused,
63                          int dpbp_id)
64 {
65         struct dpaa2_dpbp_dev *dpbp_node;
66         int ret;
67
68         /* Allocate DPAA2 dpbp handle */
69         dpbp_node = rte_malloc(NULL, sizeof(struct dpaa2_dpbp_dev), 0);
70         if (!dpbp_node) {
71                 PMD_INIT_LOG(ERR, "Memory allocation failed for DPBP Device");
72                 return -1;
73         }
74
75         /* Open the dpbp object */
76         dpbp_node->dpbp.regs = rte_mcp_ptr_list[MC_PORTAL_INDEX];
77         ret = dpbp_open(&dpbp_node->dpbp,
78                         CMD_PRI_LOW, dpbp_id, &dpbp_node->token);
79         if (ret) {
80                 PMD_INIT_LOG(ERR, "Resource alloc failure with err code: %d",
81                              ret);
82                 rte_free(dpbp_node);
83                 return -1;
84         }
85
86         /* Clean the device first */
87         ret = dpbp_reset(&dpbp_node->dpbp, CMD_PRI_LOW, dpbp_node->token);
88         if (ret) {
89                 PMD_INIT_LOG(ERR, "Failure cleaning dpbp device with"
90                                         " error code %d\n", ret);
91                 dpbp_close(&dpbp_node->dpbp, CMD_PRI_LOW, dpbp_node->token);
92                 rte_free(dpbp_node);
93                 return -1;
94         }
95
96         dpbp_node->dpbp_id = dpbp_id;
97         rte_atomic16_init(&dpbp_node->in_use);
98
99         TAILQ_INSERT_TAIL(&dpbp_dev_list, dpbp_node, next);
100
101         PMD_INIT_LOG(DEBUG, "DPAA2: Added [dpbp.%d]", dpbp_id);
102
103         return 0;
104 }
105
106 struct dpaa2_dpbp_dev *dpaa2_alloc_dpbp_dev(void)
107 {
108         struct dpaa2_dpbp_dev *dpbp_dev = NULL;
109
110         /* Get DPBP dev handle from list using index */
111         TAILQ_FOREACH(dpbp_dev, &dpbp_dev_list, next) {
112                 if (dpbp_dev && rte_atomic16_test_and_set(&dpbp_dev->in_use))
113                         break;
114         }
115
116         return dpbp_dev;
117 }
118
119 void dpaa2_free_dpbp_dev(struct dpaa2_dpbp_dev *dpbp)
120 {
121         struct dpaa2_dpbp_dev *dpbp_dev = NULL;
122
123         /* Match DPBP handle and mark it free */
124         TAILQ_FOREACH(dpbp_dev, &dpbp_dev_list, next) {
125                 if (dpbp_dev == dpbp) {
126                         rte_atomic16_dec(&dpbp_dev->in_use);
127                         return;
128                 }
129         }
130 }
131
132 static struct rte_dpaa2_object rte_dpaa2_dpbp_obj = {
133         .object_id = DPAA2_MC_DPBP_DEVID,
134         .create = dpaa2_create_dpbp_device,
135 };
136
137 RTE_PMD_REGISTER_DPAA2_OBJECT(dpbp, rte_dpaa2_dpbp_obj);