Skip to content

Commit 9b8be49

Browse files
theadibdanieldegrasse
authored andcommitted
drivers: ethernet: nxp: timestamping to all potential packets.
add timestamping on Tx to packets marked for timestamping add timestamping on Rx to all packets for later use fix race condidition on adding timestamp when sending delay_req Signed-off-by: Adib Taraben <[email protected]>
1 parent feb9d48 commit 9b8be49

File tree

2 files changed

+26
-25
lines changed

2 files changed

+26
-25
lines changed

drivers/ethernet/eth_nxp_enet.c

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -163,12 +163,15 @@ static inline void ts_register_tx_event(const struct device *dev,
163163
struct net_pkt *pkt = frameinfo->context;
164164

165165
if (pkt && atomic_get(&pkt->atomic_ref) > 0) {
166-
if (eth_get_ptp_data(net_pkt_iface(pkt), pkt) && frameinfo->isTsAvail) {
166+
if ((eth_get_ptp_data(net_pkt_iface(pkt), pkt) ||
167+
net_pkt_is_tx_timestamping(pkt)) &&
168+
frameinfo->isTsAvail) {
167169
/* Timestamp is written to packet in ISR.
168170
* Semaphore ensures sequential execution of writing
169171
* the timestamp here and subsequently reading the timestamp
170172
* after waiting for the semaphore in eth_wait_for_ptp_ts().
171173
*/
174+
172175
pkt->timestamp.nanosecond = frameinfo->timeStamp.nanosecond;
173176
pkt->timestamp.second = frameinfo->timeStamp.second;
174177

@@ -184,7 +187,9 @@ static inline void eth_wait_for_ptp_ts(const struct device *dev, struct net_pkt
184187
struct nxp_enet_mac_data *data = dev->data;
185188

186189
net_pkt_ref(pkt);
187-
k_sem_take(&data->ptp.ptp_ts_sem, K_FOREVER);
190+
while (k_sem_take(&data->ptp.ptp_ts_sem, K_MSEC(200)) != 0) {
191+
LOG_ERR("error on take PTP semaphore");
192+
}
188193
}
189194
#else
190195
#define eth_get_ptp_data(...) false
@@ -220,10 +225,11 @@ static int eth_nxp_enet_tx(const struct device *dev, struct net_pkt *pkt)
220225
goto exit;
221226
}
222227

223-
frame_is_timestamped = eth_get_ptp_data(net_pkt_iface(pkt), pkt);
228+
frame_is_timestamped =
229+
eth_get_ptp_data(net_pkt_iface(pkt), pkt) || net_pkt_is_tx_timestamping(pkt);
224230

225-
ret = ENET_SendFrame(data->base, &data->enet_handle, data->tx_frame_buf,
226-
total_len, RING_ID, frame_is_timestamped, pkt);
231+
ret = ENET_SendFrame(data->base, &data->enet_handle, data->tx_frame_buf, total_len, RING_ID,
232+
frame_is_timestamped, pkt);
227233

228234
if (ret != kStatus_Success) {
229235
LOG_ERR("ENET_SendFrame error: %d", ret);
@@ -339,6 +345,7 @@ static int eth_nxp_enet_rx(const struct device *dev)
339345
{
340346
#if defined(CONFIG_PTP_CLOCK_NXP_ENET)
341347
const struct nxp_enet_mac_config *config = dev->config;
348+
struct net_ptp_time ptp_time;
342349
#endif
343350
struct nxp_enet_mac_data *data = dev->data;
344351
uint32_t frame_length = 0U;
@@ -391,26 +398,20 @@ static int eth_nxp_enet_rx(const struct device *dev)
391398
#if defined(CONFIG_PTP_CLOCK_NXP_ENET)
392399
k_mutex_lock(data->ptp.ptp_mutex, K_FOREVER);
393400

394-
/* Invalid value by default. */
395-
pkt->timestamp.nanosecond = UINT32_MAX;
396-
pkt->timestamp.second = UINT64_MAX;
397-
398-
/* Timestamp the packet using PTP clock */
399-
if (eth_get_ptp_data(get_iface(data), pkt)) {
400-
struct net_ptp_time ptp_time;
401-
402-
ptp_clock_get(config->ptp_clock, &ptp_time);
403-
404-
/* If latest timestamp reloads after getting from Rx BD,
405-
* then second - 1 to make sure the actual Rx timestamp is accurate
406-
*/
407-
if (ptp_time.nanosecond < ts) {
408-
ptp_time.second--;
409-
}
401+
/* Timestamp the packet using PTP clock. Add full second part
402+
* the hardware timestamp contains the fractional part of the second only
403+
*/
404+
ptp_clock_get(config->ptp_clock, &ptp_time);
410405

411-
pkt->timestamp.nanosecond = ts;
412-
pkt->timestamp.second = ptp_time.second;
406+
/* If latest timestamp reloads after getting from Rx BD,
407+
* then second - 1 to make sure the actual Rx timestamp is accurate
408+
*/
409+
if (ptp_time.nanosecond < ts) {
410+
ptp_time.second--;
413411
}
412+
413+
pkt->timestamp.nanosecond = ts;
414+
pkt->timestamp.second = ptp_time.second;
414415
k_mutex_unlock(data->ptp.ptp_mutex);
415416
#endif /* CONFIG_PTP_CLOCK_NXP_ENET */
416417

subsys/net/lib/ptp/port.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -309,14 +309,14 @@ static int port_delay_req_msg_transmit(struct ptp_port *port)
309309
port->iface,
310310
port_delay_req_timestamp_cb);
311311

312+
sys_slist_append(&port->delay_req_list, &msg->node);
312313
ret = port_msg_send(port, msg, PTP_SOCKET_EVENT);
313314
if (ret < 0) {
315+
sys_slist_find_and_remove(&port->delay_req_list, &msg->node);
314316
ptp_msg_unref(msg);
315317
return -EFAULT;
316318
}
317319

318-
sys_slist_append(&port->delay_req_list, &msg->node);
319-
320320
LOG_DBG("Port %d sends Delay_Req message", port->port_ds.id.port_number);
321321
return 0;
322322
}

0 commit comments

Comments
 (0)