New upstream version 18.02
[deb_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                 PMD_INIT_LOG(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                 PMD_INIT_LOG(ERR, "Resource alloc failure with err code: %d",
57                              ret);
58                 rte_free(dpci_node);
59                 return -1;
60         }
61
62         /* Get the device attributes */
63         ret = dpci_get_attributes(&dpci_node->dpci,
64                                   CMD_PRI_LOW, dpci_node->token, &attr);
65         if (ret != 0) {
66                 PMD_INIT_LOG(ERR, "Reading device failed with err code: %d",
67                              ret);
68                 rte_free(dpci_node);
69                 return -1;
70         }
71
72         /* Set up the Rx Queue */
73         memset(&rx_queue_cfg, 0, sizeof(struct dpci_rx_queue_cfg));
74         ret = dpci_set_rx_queue(&dpci_node->dpci,
75                                 CMD_PRI_LOW,
76                                 dpci_node->token,
77                                 0, &rx_queue_cfg);
78         if (ret) {
79                 PMD_INIT_LOG(ERR, "Setting Rx queue failed with err code: %d",
80                              ret);
81                 rte_free(dpci_node);
82                 return -1;
83         }
84
85         /* Enable the device */
86         ret = dpci_enable(&dpci_node->dpci,
87                           CMD_PRI_LOW, dpci_node->token);
88         if (ret != 0) {
89                 PMD_INIT_LOG(ERR, "Enabling device failed with err code: %d",
90                              ret);
91                 rte_free(dpci_node);
92                 return -1;
93         }
94
95         for (i = 0; i < DPAA2_DPCI_MAX_QUEUES; i++) {
96                 /* Get the Rx FQID's */
97                 ret = dpci_get_rx_queue(&dpci_node->dpci,
98                                         CMD_PRI_LOW,
99                                         dpci_node->token, i,
100                                         &rx_attr);
101                 if (ret != 0) {
102                         PMD_INIT_LOG(ERR,
103                                      "Reading device failed with err code: %d",
104                                 ret);
105                         rte_free(dpci_node);
106                         return -1;
107                 }
108
109                 dpci_node->queue[i].fqid = rx_attr.fqid;
110         }
111
112         dpci_node->dpci_id = dpci_id;
113         rte_atomic16_init(&dpci_node->in_use);
114
115         TAILQ_INSERT_TAIL(&dpci_dev_list, dpci_node, next);
116
117         RTE_LOG(DEBUG, PMD, "DPAA2: Added [dpci.%d]\n", dpci_id);
118
119         return 0;
120 }
121
122 struct dpaa2_dpci_dev *rte_dpaa2_alloc_dpci_dev(void)
123 {
124         struct dpaa2_dpci_dev *dpci_dev = NULL;
125
126         /* Get DPCI dev handle from list using index */
127         TAILQ_FOREACH(dpci_dev, &dpci_dev_list, next) {
128                 if (dpci_dev && rte_atomic16_test_and_set(&dpci_dev->in_use))
129                         break;
130         }
131
132         return dpci_dev;
133 }
134
135 void rte_dpaa2_free_dpci_dev(struct dpaa2_dpci_dev *dpci)
136 {
137         struct dpaa2_dpci_dev *dpci_dev = NULL;
138
139         /* Match DPCI handle and mark it free */
140         TAILQ_FOREACH(dpci_dev, &dpci_dev_list, next) {
141                 if (dpci_dev == dpci) {
142                         rte_atomic16_dec(&dpci_dev->in_use);
143                         return;
144                 }
145         }
146 }
147
148 static struct rte_dpaa2_object rte_dpaa2_dpci_obj = {
149         .dev_type = DPAA2_CI,
150         .create = rte_dpaa2_create_dpci_device,
151 };
152
153 RTE_PMD_REGISTER_DPAA2_OBJECT(dpci, rte_dpaa2_dpci_obj);