-
Notifications
You must be signed in to change notification settings - Fork 176
/
ena_phc.c
305 lines (244 loc) · 7.64 KB
/
ena_phc.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
// SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
/* Copyright (c) Amazon.com, Inc. or its affiliates.
* All rights reserved.
*/
#include <linux/pci.h>
#include "ena_netdev.h"
#include "ena_phc.h"
#ifdef ENA_PHC_SUPPORT
#ifdef ENA_PHC_SUPPORT_ADJFREQ
static int ena_phc_adjfreq(struct ptp_clock_info *clock_info, s32 ppb)
{
return -EOPNOTSUPP;
}
#endif /* ENA_PHC_SUPPORT_ADJFREQ */
static int ena_phc_adjtime(struct ptp_clock_info *clock_info, s64 delta)
{
return -EOPNOTSUPP;
}
#ifdef ENA_PHC_SUPPORT_ADJFINE
static int ena_phc_adjfine(struct ptp_clock_info *clock_info, long scaled_ppm)
{
return -EOPNOTSUPP;
}
#endif /* ENA_PHC_SUPPORT_ADJFINE */
static int ena_phc_feature_enable(struct ptp_clock_info *clock_info, struct ptp_clock_request *rq,
int on)
{
return -EOPNOTSUPP;
}
#ifdef ENA_PHC_SUPPORT_GETTIME64
#ifdef ENA_PHC_SUPPORT_GETTIME64_EXTENDED
static int ena_phc_gettimex64(struct ptp_clock_info *clock_info, struct timespec64 *ts,
struct ptp_system_timestamp *sts)
{
struct ena_phc_info *phc_info = container_of(clock_info, struct ena_phc_info, clock_info);
unsigned long flags;
u64 timestamp_nsec;
int rc;
spin_lock_irqsave(&phc_info->lock, flags);
ptp_read_system_prets(sts);
rc = ena_com_phc_get_timestamp(phc_info->adapter->ena_dev, ×tamp_nsec);
ptp_read_system_postts(sts);
spin_unlock_irqrestore(&phc_info->lock, flags);
*ts = ns_to_timespec64(timestamp_nsec);
return rc;
}
#else /* ENA_PHC_SUPPORT_GETTIME64_EXTENDED */
static int ena_phc_gettime64(struct ptp_clock_info *clock_info, struct timespec64 *ts)
{
struct ena_phc_info *phc_info = container_of(clock_info, struct ena_phc_info, clock_info);
unsigned long flags;
u64 timestamp_nsec;
int rc;
spin_lock_irqsave(&phc_info->lock, flags);
rc = ena_com_phc_get_timestamp(phc_info->adapter->ena_dev, ×tamp_nsec);
spin_unlock_irqrestore(&phc_info->lock, flags);
*ts = ns_to_timespec64(timestamp_nsec);
return rc;
}
#endif /* ENA_PHC_SUPPORT_GETTIME64_EXTENDED */
static int ena_phc_settime64(struct ptp_clock_info *clock_info,
const struct timespec64 *ts)
{
return -EOPNOTSUPP;
}
#else /* ENA_PHC_SUPPORT_GETTIME64 */
static int ena_phc_gettime(struct ptp_clock_info *clock_info, struct timespec *ts)
{
struct ena_phc_info *phc_info = container_of(clock_info, struct ena_phc_info, clock_info);
unsigned long flags;
u64 timestamp_nsec;
u32 remainder;
int rc;
spin_lock_irqsave(&phc_info->lock, flags);
rc = ena_com_phc_get_timestamp(phc_info->adapter->ena_dev, ×tamp_nsec);
spin_unlock_irqrestore(&phc_info->lock, flags);
ts->tv_sec = div_u64_rem(timestamp_nsec, NSEC_PER_SEC, &remainder);
ts->tv_nsec = remainder;
return rc;
}
static int ena_phc_settime(struct ptp_clock_info *clock_info, const struct timespec *ts)
{
return -EOPNOTSUPP;
}
#endif /* ENA_PHC_SUPPORT_GETTIME64 */
static struct ptp_clock_info ena_ptp_clock_info = {
.owner = THIS_MODULE,
.n_alarm = 0,
.n_ext_ts = 0,
.n_per_out = 0,
.pps = 0,
#ifdef ENA_PHC_SUPPORT_ADJFREQ
.adjfreq = ena_phc_adjfreq,
#endif /* ENA_PHC_SUPPORT_ADJFREQ */
.adjtime = ena_phc_adjtime,
#ifdef ENA_PHC_SUPPORT_ADJFINE
.adjfine = ena_phc_adjfine,
#endif /* ENA_PHC_SUPPORT_ADJFINE */
#ifdef ENA_PHC_SUPPORT_GETTIME64
#ifdef ENA_PHC_SUPPORT_GETTIME64_EXTENDED
.gettimex64 = ena_phc_gettimex64,
#else /* ENA_PHC_SUPPORT_GETTIME64_EXTENDED */
.gettime64 = ena_phc_gettime64,
#endif /* ENA_PHC_SUPPORT_GETTIME64_EXTENDED */
.settime64 = ena_phc_settime64,
#else /* ENA_PHC_SUPPORT_GETTIME64 */
.gettime = ena_phc_gettime,
.settime = ena_phc_settime,
#endif /* ENA_PHC_SUPPORT_GETTIME64 */
.enable = ena_phc_feature_enable,
};
/* Enable/Disable PHC by the kernel, affects on the next init flow */
void ena_phc_enable(struct ena_adapter *adapter, bool enable)
{
struct ena_phc_info *phc_info = adapter->phc_info;
if (!phc_info) {
netdev_err(adapter->netdev, "phc_info is not allocated\n");
return;
}
phc_info->enabled = enable;
}
/* Check if PHC is enabled by the kernel */
bool ena_phc_is_enabled(struct ena_adapter *adapter)
{
struct ena_phc_info *phc_info = adapter->phc_info;
return (phc_info && phc_info->enabled);
}
/* PHC is activated if ptp clock is registered in the kernel */
bool ena_phc_is_active(struct ena_adapter *adapter)
{
struct ena_phc_info *phc_info = adapter->phc_info;
return (phc_info && phc_info->clock);
}
static int ena_phc_register(struct ena_adapter *adapter)
{
struct pci_dev *pdev = adapter->pdev;
struct ptp_clock_info *clock_info;
struct ena_phc_info *phc_info;
int rc = 0;
phc_info = adapter->phc_info;
clock_info = &phc_info->clock_info;
/* PHC may already be registered in case of a reset */
if (ena_phc_is_active(adapter))
return 0;
phc_info->adapter = adapter;
spin_lock_init(&phc_info->lock);
/* Fill the ptp_clock_info struct and register PTP clock */
*clock_info = ena_ptp_clock_info;
snprintf(clock_info->name,
sizeof(clock_info->name),
"ena-ptp-%02x",
PCI_SLOT(pdev->devfn));
phc_info->clock = ptp_clock_register(clock_info, &pdev->dev);
if (IS_ERR(phc_info->clock)) {
rc = PTR_ERR(phc_info->clock);
netdev_err(adapter->netdev, "Failed registering ptp clock, error: %d\n", rc);
phc_info->clock = NULL;
}
return rc;
}
static void ena_phc_unregister(struct ena_adapter *adapter)
{
struct ena_phc_info *phc_info = adapter->phc_info;
/* During reset flow, PHC must stay registered to keep kernel's PHC index */
if (ena_phc_is_active(adapter) && !test_bit(ENA_FLAG_TRIGGER_RESET, &adapter->flags)) {
ptp_clock_unregister(phc_info->clock);
phc_info->clock = NULL;
}
}
int ena_phc_alloc(struct ena_adapter *adapter)
{
/* Allocate driver specific PHC info */
adapter->phc_info = vzalloc(sizeof(*adapter->phc_info));
if (unlikely(!adapter->phc_info)) {
netdev_err(adapter->netdev, "Failed to alloc phc_info\n");
return -ENOMEM;
}
return 0;
}
void ena_phc_free(struct ena_adapter *adapter)
{
if (adapter->phc_info) {
vfree(adapter->phc_info);
adapter->phc_info = NULL;
}
}
int ena_phc_init(struct ena_adapter *adapter)
{
struct ena_com_dev *ena_dev = adapter->ena_dev;
struct net_device *netdev = adapter->netdev;
int rc = -EOPNOTSUPP;
/* Validate PHC feature is supported in the device */
if (!ena_com_phc_supported(ena_dev)) {
netdev_dbg(netdev, "PHC feature is not supported by the device\n");
goto err_ena_com_phc_init;
}
/* Validate PHC feature is enabled by the kernel */
if (!ena_phc_is_enabled(adapter)) {
netdev_dbg(netdev, "PHC feature is not enabled by the kernel\n");
goto err_ena_com_phc_init;
}
/* Initialize device specific PHC info */
rc = ena_com_phc_init(ena_dev);
if (unlikely(rc)) {
netdev_err(netdev, "Failed to init phc, error: %d\n", rc);
goto err_ena_com_phc_init;
}
/* Configure PHC feature in driver and device */
rc = ena_com_phc_config(ena_dev);
if (unlikely(rc)) {
netdev_err(netdev, "Failed to config phc, error: %d\n", rc);
goto err_ena_com_phc_config;
}
/* Register to PTP class driver */
rc = ena_phc_register(adapter);
if (unlikely(rc)) {
netdev_err(netdev, "Failed to register phc, error: %d\n", rc);
goto err_ena_com_phc_config;
}
return 0;
err_ena_com_phc_config:
ena_com_phc_destroy(ena_dev);
err_ena_com_phc_init:
ena_phc_enable(adapter, false);
return rc;
}
void ena_phc_destroy(struct ena_adapter *adapter)
{
ena_phc_unregister(adapter);
ena_com_phc_destroy(adapter->ena_dev);
}
int ena_phc_get_index(struct ena_adapter *adapter)
{
if (ena_phc_is_active(adapter))
return ptp_clock_index(adapter->phc_info->clock);
return -1;
}
int ena_phc_get_error_bound(struct ena_adapter *adapter, u32 *error_bound_nsec)
{
if (!ena_phc_is_active(adapter))
return -EOPNOTSUPP;
return ena_com_phc_get_error_bound(adapter->ena_dev, error_bound_nsec);
}
#endif /* ENA_PHC_SUPPORT */