mem: add function to walk all memsegs
[dpdk.git] / drivers / bus / fslmc / portal / dpaa2_hw_dpci.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  *
3  *   Copyright 2017 NXP
4  *
5  */
6
7 #include <unistd.h>
8 #include <stdio.h>
9 #include <sys/types.h>
10 #include <string.h>
11 #include <stdlib.h>
12 #include <fcntl.h>
13 #include <errno.h>
14
15 #include <rte_malloc.h>
16 #include <rte_memcpy.h>
17 #include <rte_string_fns.h>
18 #include <rte_cycles.h>
19 #include <rte_kvargs.h>
20 #include <rte_dev.h>
21 #include <rte_ethdev_driver.h>
22
23 #include <fslmc_logs.h>
24 #include <rte_fslmc.h>
25 #include <mc/fsl_dpci.h>
26 #include "portal/dpaa2_hw_pvt.h"
27 #include "portal/dpaa2_hw_dpio.h"
28
29 TAILQ_HEAD(dpci_dev_list, dpaa2_dpci_dev);
30 static struct dpci_dev_list dpci_dev_list
31         = TAILQ_HEAD_INITIALIZER(dpci_dev_list); /*!< DPCI device list */
32
33 static int
34 rte_dpaa2_create_dpci_device(int vdev_fd __rte_unused,
35                              struct vfio_device_info *obj_info __rte_unused,
36                              int dpci_id)
37 {
38         struct dpaa2_dpci_dev *dpci_node;
39         struct dpci_attr attr;
40         struct dpci_rx_queue_cfg rx_queue_cfg;
41         struct dpci_rx_queue_attr rx_attr;
42         int ret, i;
43
44         /* Allocate DPAA2 dpci handle */
45         dpci_node = rte_malloc(NULL, sizeof(struct dpaa2_dpci_dev), 0);
46         if (!dpci_node) {
47                 DPAA2_BUS_ERR("Memory allocation failed for DPCI Device");
48                 return -1;
49         }
50
51         /* Open the dpci object */
52         dpci_node->dpci.regs = rte_mcp_ptr_list[MC_PORTAL_INDEX];
53         ret = dpci_open(&dpci_node->dpci,
54                         CMD_PRI_LOW, dpci_id, &dpci_node->token);
55         if (ret) {
56                 DPAA2_BUS_ERR("Resource alloc failure with err code: %d", ret);
57                 rte_free(dpci_node);
58                 return -1;
59         }
60
61         /* Get the device attributes */
62         ret = dpci_get_attributes(&dpci_node->dpci,
63                                   CMD_PRI_LOW, dpci_node->token, &attr);
64         if (ret != 0) {
65                 DPAA2_BUS_ERR("Reading device failed with err code: %d", ret);
66                 rte_free(dpci_node);
67                 return -1;
68         }
69
70         /* Set up the Rx Queue */
71         memset(&rx_queue_cfg, 0, sizeof(struct dpci_rx_queue_cfg));
72         ret = dpci_set_rx_queue(&dpci_node->dpci,
73                                 CMD_PRI_LOW,
74                                 dpci_node->token,
75                                 0, &rx_queue_cfg);
76         if (ret) {
77                 DPAA2_BUS_ERR("Setting Rx queue failed with err code: %d",
78                               ret);
79                 rte_free(dpci_node);
80                 return -1;
81         }
82
83         /* Enable the device */
84         ret = dpci_enable(&dpci_node->dpci,
85                           CMD_PRI_LOW, dpci_node->token);
86         if (ret != 0) {
87                 DPAA2_BUS_ERR("Enabling device failed with err code: %d", ret);
88                 rte_free(dpci_node);
89                 return -1;
90         }
91
92         for (i = 0; i < DPAA2_DPCI_MAX_QUEUES; i++) {
93                 /* Get the Rx FQID's */
94                 ret = dpci_get_rx_queue(&dpci_node->dpci,
95                                         CMD_PRI_LOW,
96                                         dpci_node->token, i,
97                                         &rx_attr);
98                 if (ret != 0) {
99                         DPAA2_BUS_ERR("Rx queue fetch failed with err code:"
100                                       " %d", ret);
101                         rte_free(dpci_node);
102                         return -1;
103                 }
104
105                 dpci_node->queue[i].fqid = rx_attr.fqid;
106         }
107
108         dpci_node->dpci_id = dpci_id;
109         rte_atomic16_init(&dpci_node->in_use);
110
111         TAILQ_INSERT_TAIL(&dpci_dev_list, dpci_node, next);
112
113         return 0;
114 }
115
116 struct dpaa2_dpci_dev *rte_dpaa2_alloc_dpci_dev(void)
117 {
118         struct dpaa2_dpci_dev *dpci_dev = NULL;
119
120         /* Get DPCI dev handle from list using index */
121         TAILQ_FOREACH(dpci_dev, &dpci_dev_list, next) {
122                 if (dpci_dev && rte_atomic16_test_and_set(&dpci_dev->in_use))
123                         break;
124         }
125
126         return dpci_dev;
127 }
128
129 void rte_dpaa2_free_dpci_dev(struct dpaa2_dpci_dev *dpci)
130 {
131         struct dpaa2_dpci_dev *dpci_dev = NULL;
132
133         /* Match DPCI handle and mark it free */
134         TAILQ_FOREACH(dpci_dev, &dpci_dev_list, next) {
135                 if (dpci_dev == dpci) {
136                         rte_atomic16_dec(&dpci_dev->in_use);
137                         return;
138                 }
139         }
140 }
141
142 static struct rte_dpaa2_object rte_dpaa2_dpci_obj = {
143         .dev_type = DPAA2_CI,
144         .create = rte_dpaa2_create_dpci_device,
145 };
146
147 RTE_PMD_REGISTER_DPAA2_OBJECT(dpci, rte_dpaa2_dpci_obj);