1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2019 NXP
3 */
4
5 #include <sys/queue.h>
6 #include <stdio.h>
7 #include <errno.h>
8 #include <stdint.h>
9 #include <string.h>
10 #include <unistd.h>
11 #include <stdarg.h>
12
13 #include <rte_ethdev.h>
14 #include <rte_log.h>
15 #include <rte_eth_ctrl.h>
16 #include <rte_malloc.h>
17 #include <rte_time.h>
18
19 #include <rte_fslmc.h>
20 #include <fsl_dprtc.h>
21 #include <fsl_dpkg.h>
22
23 #include <dpaa2_ethdev.h>
24 #include <dpaa2_pmd_logs.h>
25
26 struct dpaa2_dprtc_dev {
27 struct fsl_mc_io dprtc; /** handle to DPRTC portal object */
28 uint16_t token;
29 uint32_t dprtc_id; /*HW ID for DPRTC object */
30 };
31 static struct dpaa2_dprtc_dev *dprtc_dev;
32
dpaa2_timesync_enable(struct rte_eth_dev * dev __rte_unused)33 int dpaa2_timesync_enable(struct rte_eth_dev *dev __rte_unused)
34 {
35 return 0;
36 }
37
dpaa2_timesync_disable(struct rte_eth_dev * dev __rte_unused)38 int dpaa2_timesync_disable(struct rte_eth_dev *dev __rte_unused)
39 {
40 return 0;
41 }
42
dpaa2_timesync_read_time(struct rte_eth_dev * dev,struct timespec * timestamp)43 int dpaa2_timesync_read_time(struct rte_eth_dev *dev,
44 struct timespec *timestamp)
45 {
46 uint64_t ns;
47 int ret = 0;
48
49 RTE_SET_USED(dev);
50
51 ret = dprtc_get_time(&dprtc_dev->dprtc, CMD_PRI_LOW,
52 dprtc_dev->token, &ns);
53 if (ret) {
54 DPAA2_PMD_ERR("dprtc_get_time failed ret: %d", ret);
55 return ret;
56 }
57
58 *timestamp = rte_ns_to_timespec(ns);
59
60 return 0;
61 }
62
dpaa2_timesync_write_time(struct rte_eth_dev * dev,const struct timespec * ts)63 int dpaa2_timesync_write_time(struct rte_eth_dev *dev,
64 const struct timespec *ts)
65 {
66 uint64_t ns;
67 int ret = 0;
68
69 RTE_SET_USED(dev);
70
71 ns = rte_timespec_to_ns(ts);
72
73 ret = dprtc_set_time(&dprtc_dev->dprtc, CMD_PRI_LOW,
74 dprtc_dev->token, ns);
75 if (ret) {
76 DPAA2_PMD_ERR("dprtc_set_time failed ret: %d", ret);
77 return ret;
78 }
79
80 return 0;
81 }
82
dpaa2_timesync_adjust_time(struct rte_eth_dev * dev,int64_t delta)83 int dpaa2_timesync_adjust_time(struct rte_eth_dev *dev, int64_t delta)
84 {
85 uint64_t ns;
86 int ret = 0;
87
88 RTE_SET_USED(dev);
89
90 ret = dprtc_get_time(&dprtc_dev->dprtc, CMD_PRI_LOW,
91 dprtc_dev->token, &ns);
92 if (ret) {
93 DPAA2_PMD_ERR("dprtc_get_time failed ret: %d", ret);
94 return ret;
95 }
96
97 ns += delta;
98
99 ret = dprtc_set_time(&dprtc_dev->dprtc, CMD_PRI_LOW,
100 dprtc_dev->token, ns);
101 if (ret) {
102 DPAA2_PMD_ERR("dprtc_set_time failed ret: %d", ret);
103 return ret;
104 }
105
106 return 0;
107 }
108
dpaa2_timesync_read_tx_timestamp(struct rte_eth_dev * dev,struct timespec * timestamp)109 int dpaa2_timesync_read_tx_timestamp(struct rte_eth_dev *dev,
110 struct timespec *timestamp)
111 {
112 struct dpaa2_dev_priv *priv = dev->data->dev_private;
113
114 if (priv->next_tx_conf_queue)
115 dpaa2_dev_tx_conf(priv->next_tx_conf_queue);
116 else
117 return -1;
118 *timestamp = rte_ns_to_timespec(priv->tx_timestamp);
119
120 return 0;
121 }
122
dpaa2_timesync_read_rx_timestamp(struct rte_eth_dev * dev,struct timespec * timestamp,uint32_t flags __rte_unused)123 int dpaa2_timesync_read_rx_timestamp(struct rte_eth_dev *dev,
124 struct timespec *timestamp,
125 uint32_t flags __rte_unused)
126 {
127 struct dpaa2_dev_priv *priv = dev->data->dev_private;
128 *timestamp = rte_ns_to_timespec(priv->rx_timestamp);
129 return 0;
130 }
131
132 #if defined(RTE_LIBRTE_IEEE1588)
133 static int
dpaa2_create_dprtc_device(int vdev_fd __rte_unused,struct vfio_device_info * obj_info __rte_unused,int dprtc_id)134 dpaa2_create_dprtc_device(int vdev_fd __rte_unused,
135 struct vfio_device_info *obj_info __rte_unused,
136 int dprtc_id)
137 {
138 struct dprtc_attr attr;
139 int ret;
140
141 PMD_INIT_FUNC_TRACE();
142
143 /* Allocate DPAA2 dprtc handle */
144 dprtc_dev = rte_malloc(NULL, sizeof(struct dpaa2_dprtc_dev), 0);
145 if (!dprtc_dev) {
146 DPAA2_PMD_ERR("Memory allocation failed for DPRTC Device");
147 return -1;
148 }
149
150 /* Open the dprtc object */
151 dprtc_dev->dprtc.regs = dpaa2_get_mcp_ptr(MC_PORTAL_INDEX);
152 ret = dprtc_open(&dprtc_dev->dprtc, CMD_PRI_LOW, dprtc_id,
153 &dprtc_dev->token);
154 if (ret) {
155 DPAA2_PMD_ERR("Unable to open dprtc object: err(%d)", ret);
156 goto init_err;
157 }
158
159 ret = dprtc_get_attributes(&dprtc_dev->dprtc, CMD_PRI_LOW,
160 dprtc_dev->token, &attr);
161 if (ret) {
162 DPAA2_PMD_ERR("Unable to get dprtc attr: err(%d)", ret);
163 goto init_err;
164 }
165
166 dprtc_dev->dprtc_id = dprtc_id;
167
168 return 0;
169
170 init_err:
171 if (dprtc_dev)
172 rte_free(dprtc_dev);
173
174 return -1;
175 }
176
177 static struct rte_dpaa2_object rte_dpaa2_dprtc_obj = {
178 .dev_type = DPAA2_DPRTC,
179 .create = dpaa2_create_dprtc_device,
180 };
181
182 RTE_PMD_REGISTER_DPAA2_OBJECT(dprtc, rte_dpaa2_dprtc_obj);
183 #endif
184