Skip to content

Commit c459f60

Browse files
chintanv133Paolo Abeni
authored andcommitted
net: ethernet: ti: am65-cpts: Enable RX HW timestamp for PTP packets using CPTS FIFO
Add a new function "am65_cpts_rx_timestamp()" which checks for PTP packets from header and timestamps them. Add another function "am65_cpts_find_rx_ts()" which finds CPTS FIFO Event to get the timestamp of received PTP packet. Signed-off-by: Chintan Vankar <[email protected]> Signed-off-by: Paolo Abeni <[email protected]>
1 parent 9b9fd02 commit c459f60

File tree

2 files changed

+83
-7
lines changed

2 files changed

+83
-7
lines changed

drivers/net/ethernet/ti/am65-cpts.c

Lines changed: 77 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -275,15 +275,13 @@ static bool am65_cpts_fifo_pop_event(struct am65_cpts *cpts,
275275
return true;
276276
}
277277

278-
static int am65_cpts_fifo_read(struct am65_cpts *cpts)
278+
static int __am65_cpts_fifo_read(struct am65_cpts *cpts)
279279
{
280280
struct ptp_clock_event pevent;
281281
struct am65_cpts_event *event;
282282
bool schedule = false;
283283
int i, type, ret = 0;
284-
unsigned long flags;
285284

286-
spin_lock_irqsave(&cpts->lock, flags);
287285
for (i = 0; i < AM65_CPTS_FIFO_DEPTH; i++) {
288286
event = list_first_entry_or_null(&cpts->pool,
289287
struct am65_cpts_event, list);
@@ -312,8 +310,7 @@ static int am65_cpts_fifo_read(struct am65_cpts *cpts)
312310
event->tmo = jiffies +
313311
msecs_to_jiffies(AM65_CPTS_EVENT_RX_TX_TIMEOUT);
314312

315-
list_del_init(&event->list);
316-
list_add_tail(&event->list, &cpts->events);
313+
list_move_tail(&event->list, &cpts->events);
317314

318315
dev_dbg(cpts->dev,
319316
"AM65_CPTS_EV_TX e1:%08x e2:%08x t:%lld\n",
@@ -356,14 +353,24 @@ static int am65_cpts_fifo_read(struct am65_cpts *cpts)
356353
}
357354

358355
out:
359-
spin_unlock_irqrestore(&cpts->lock, flags);
360-
361356
if (schedule)
362357
ptp_schedule_worker(cpts->ptp_clock, 0);
363358

364359
return ret;
365360
}
366361

362+
static int am65_cpts_fifo_read(struct am65_cpts *cpts)
363+
{
364+
unsigned long flags;
365+
int ret = 0;
366+
367+
spin_lock_irqsave(&cpts->lock, flags);
368+
ret = __am65_cpts_fifo_read(cpts);
369+
spin_unlock_irqrestore(&cpts->lock, flags);
370+
371+
return ret;
372+
}
373+
367374
static u64 am65_cpts_gettime(struct am65_cpts *cpts,
368375
struct ptp_system_timestamp *sts)
369376
{
@@ -906,6 +913,69 @@ static int am65_skb_get_mtype_seqid(struct sk_buff *skb, u32 *mtype_seqid)
906913
return 1;
907914
}
908915

916+
static u64 am65_cpts_find_rx_ts(struct am65_cpts *cpts, u32 skb_mtype_seqid)
917+
{
918+
struct list_head *this, *next;
919+
struct am65_cpts_event *event;
920+
unsigned long flags;
921+
u32 mtype_seqid;
922+
u64 ns = 0;
923+
924+
spin_lock_irqsave(&cpts->lock, flags);
925+
__am65_cpts_fifo_read(cpts);
926+
list_for_each_safe(this, next, &cpts->events) {
927+
event = list_entry(this, struct am65_cpts_event, list);
928+
if (time_after(jiffies, event->tmo)) {
929+
list_move(&event->list, &cpts->pool);
930+
continue;
931+
}
932+
933+
mtype_seqid = event->event1 &
934+
(AM65_CPTS_EVENT_1_MESSAGE_TYPE_MASK |
935+
AM65_CPTS_EVENT_1_SEQUENCE_ID_MASK |
936+
AM65_CPTS_EVENT_1_EVENT_TYPE_MASK);
937+
938+
if (mtype_seqid == skb_mtype_seqid) {
939+
ns = event->timestamp;
940+
list_move(&event->list, &cpts->pool);
941+
break;
942+
}
943+
}
944+
spin_unlock_irqrestore(&cpts->lock, flags);
945+
946+
return ns;
947+
}
948+
949+
void am65_cpts_rx_timestamp(struct am65_cpts *cpts, struct sk_buff *skb)
950+
{
951+
struct am65_cpts_skb_cb_data *skb_cb = (struct am65_cpts_skb_cb_data *)skb->cb;
952+
struct skb_shared_hwtstamps *ssh;
953+
int ret;
954+
u64 ns;
955+
956+
/* am65_cpts_rx_timestamp() is called before eth_type_trans(), so
957+
* skb MAC Hdr properties are not configured yet. Hence need to
958+
* reset skb MAC header here
959+
*/
960+
skb_reset_mac_header(skb);
961+
ret = am65_skb_get_mtype_seqid(skb, &skb_cb->skb_mtype_seqid);
962+
if (!ret)
963+
return; /* if not PTP class packet */
964+
965+
skb_cb->skb_mtype_seqid |= (AM65_CPTS_EV_RX << AM65_CPTS_EVENT_1_EVENT_TYPE_SHIFT);
966+
967+
dev_dbg(cpts->dev, "%s mtype seqid %08x\n", __func__, skb_cb->skb_mtype_seqid);
968+
969+
ns = am65_cpts_find_rx_ts(cpts, skb_cb->skb_mtype_seqid);
970+
if (!ns)
971+
return;
972+
973+
ssh = skb_hwtstamps(skb);
974+
memset(ssh, 0, sizeof(*ssh));
975+
ssh->hwtstamp = ns_to_ktime(ns);
976+
}
977+
EXPORT_SYMBOL_GPL(am65_cpts_rx_timestamp);
978+
909979
/**
910980
* am65_cpts_tx_timestamp - save tx packet for timestamping
911981
* @cpts: cpts handle

drivers/net/ethernet/ti/am65-cpts.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ void am65_cpts_release(struct am65_cpts *cpts);
2222
struct am65_cpts *am65_cpts_create(struct device *dev, void __iomem *regs,
2323
struct device_node *node);
2424
int am65_cpts_phc_index(struct am65_cpts *cpts);
25+
void am65_cpts_rx_timestamp(struct am65_cpts *cpts, struct sk_buff *skb);
2526
void am65_cpts_tx_timestamp(struct am65_cpts *cpts, struct sk_buff *skb);
2627
void am65_cpts_prep_tx_timestamp(struct am65_cpts *cpts, struct sk_buff *skb);
2728
void am65_cpts_rx_enable(struct am65_cpts *cpts, bool en);
@@ -48,6 +49,11 @@ static inline int am65_cpts_phc_index(struct am65_cpts *cpts)
4849
return -1;
4950
}
5051

52+
static inline void am65_cpts_rx_timestamp(struct am65_cpts *cpts,
53+
struct sk_buff *skb)
54+
{
55+
}
56+
5157
static inline void am65_cpts_tx_timestamp(struct am65_cpts *cpts,
5258
struct sk_buff *skb)
5359
{

0 commit comments

Comments
 (0)