1 // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
2 /* Copyright (c) 2019 Mellanox Technologies. All rights reserved */
4 #include <linux/ptp_clock_kernel.h>
5 #include <linux/clocksource.h>
6 #include <linux/timecounter.h>
7 #include <linux/spinlock.h>
8 #include <linux/device.h>
9 #include <linux/rhashtable.h>
10 #include <linux/ptp_classify.h>
11 #include <linux/if_ether.h>
12 #include <linux/if_vlan.h>
13 #include <linux/net_tstamp.h>
14 #include <linux/refcount.h>
17 #include "spectrum_ptp.h"
20 #define MLXSW_SP1_PTP_CLOCK_CYCLES_SHIFT 29
21 #define MLXSW_SP1_PTP_CLOCK_FREQ_KHZ 156257 /* 6.4nSec */
22 #define MLXSW_SP1_PTP_CLOCK_MASK 64
24 #define MLXSW_SP1_PTP_HT_GC_INTERVAL 500 /* ms */
26 /* How long, approximately, should the unmatched entries stay in the hash table
27 * before they are collected. Should be evenly divisible by the GC interval.
29 #define MLXSW_SP1_PTP_HT_GC_TIMEOUT 1000 /* ms */
31 struct mlxsw_sp_ptp_state {
32 struct mlxsw_sp *mlxsw_sp;
35 struct mlxsw_sp1_ptp_state {
36 struct mlxsw_sp_ptp_state common;
37 struct rhltable unmatched_ht;
38 spinlock_t unmatched_lock; /* protects the HT */
39 struct delayed_work ht_gc_dw;
43 struct mlxsw_sp2_ptp_state {
44 struct mlxsw_sp_ptp_state common;
45 refcount_t ptp_port_enabled_ref; /* Number of ports with time stamping
48 struct hwtstamp_config config;
51 struct mlxsw_sp1_ptp_key {
59 struct mlxsw_sp1_ptp_unmatched {
60 struct mlxsw_sp1_ptp_key key;
61 struct rhlist_head ht_node;
68 static const struct rhashtable_params mlxsw_sp1_ptp_unmatched_ht_params = {
69 .key_len = sizeof_field(struct mlxsw_sp1_ptp_unmatched, key),
70 .key_offset = offsetof(struct mlxsw_sp1_ptp_unmatched, key),
71 .head_offset = offsetof(struct mlxsw_sp1_ptp_unmatched, ht_node),
74 struct mlxsw_sp_ptp_clock {
75 struct mlxsw_core *core;
76 struct ptp_clock *ptp;
77 struct ptp_clock_info ptp_info;
80 struct mlxsw_sp1_ptp_clock {
81 struct mlxsw_sp_ptp_clock common;
82 spinlock_t lock; /* protect this structure */
83 struct cyclecounter cycles;
84 struct timecounter tc;
86 unsigned long overflow_period;
87 struct delayed_work overflow_work;
90 static struct mlxsw_sp1_ptp_state *
91 mlxsw_sp1_ptp_state(struct mlxsw_sp *mlxsw_sp)
93 return container_of(mlxsw_sp->ptp_state, struct mlxsw_sp1_ptp_state,
97 static struct mlxsw_sp2_ptp_state *
98 mlxsw_sp2_ptp_state(struct mlxsw_sp *mlxsw_sp)
100 return container_of(mlxsw_sp->ptp_state, struct mlxsw_sp2_ptp_state,
104 static struct mlxsw_sp1_ptp_clock *
105 mlxsw_sp1_ptp_clock(struct ptp_clock_info *ptp)
107 return container_of(ptp, struct mlxsw_sp1_ptp_clock, common.ptp_info);
110 static u64 __mlxsw_sp1_ptp_read_frc(struct mlxsw_sp1_ptp_clock *clock,
111 struct ptp_system_timestamp *sts)
113 struct mlxsw_core *mlxsw_core = clock->common.core;
114 u32 frc_h1, frc_h2, frc_l;
116 frc_h1 = mlxsw_core_read_frc_h(mlxsw_core);
117 ptp_read_system_prets(sts);
118 frc_l = mlxsw_core_read_frc_l(mlxsw_core);
119 ptp_read_system_postts(sts);
120 frc_h2 = mlxsw_core_read_frc_h(mlxsw_core);
122 if (frc_h1 != frc_h2) {
124 ptp_read_system_prets(sts);
125 frc_l = mlxsw_core_read_frc_l(mlxsw_core);
126 ptp_read_system_postts(sts);
129 return (u64) frc_l | (u64) frc_h2 << 32;
132 static u64 mlxsw_sp1_ptp_read_frc(const struct cyclecounter *cc)
134 struct mlxsw_sp1_ptp_clock *clock =
135 container_of(cc, struct mlxsw_sp1_ptp_clock, cycles);
137 return __mlxsw_sp1_ptp_read_frc(clock, NULL) & cc->mask;
141 mlxsw_sp_ptp_phc_adjfreq(struct mlxsw_sp_ptp_clock *clock, int freq_adj)
143 struct mlxsw_core *mlxsw_core = clock->core;
144 char mtutc_pl[MLXSW_REG_MTUTC_LEN];
146 mlxsw_reg_mtutc_pack(mtutc_pl, MLXSW_REG_MTUTC_OPERATION_ADJUST_FREQ,
148 return mlxsw_reg_write(mlxsw_core, MLXSW_REG(mtutc), mtutc_pl);
151 static u64 mlxsw_sp1_ptp_ns2cycles(const struct timecounter *tc, u64 nsec)
153 u64 cycles = (u64) nsec;
155 cycles <<= tc->cc->shift;
156 cycles = div_u64(cycles, tc->cc->mult);
162 mlxsw_sp1_ptp_phc_settime(struct mlxsw_sp1_ptp_clock *clock, u64 nsec)
164 struct mlxsw_core *mlxsw_core = clock->common.core;
165 u64 next_sec, next_sec_in_nsec, cycles;
166 char mtutc_pl[MLXSW_REG_MTUTC_LEN];
167 char mtpps_pl[MLXSW_REG_MTPPS_LEN];
170 next_sec = div_u64(nsec, NSEC_PER_SEC) + 1;
171 next_sec_in_nsec = next_sec * NSEC_PER_SEC;
173 spin_lock_bh(&clock->lock);
174 cycles = mlxsw_sp1_ptp_ns2cycles(&clock->tc, next_sec_in_nsec);
175 spin_unlock_bh(&clock->lock);
177 mlxsw_reg_mtpps_vpin_pack(mtpps_pl, cycles);
178 err = mlxsw_reg_write(mlxsw_core, MLXSW_REG(mtpps), mtpps_pl);
182 mlxsw_reg_mtutc_pack(mtutc_pl,
183 MLXSW_REG_MTUTC_OPERATION_SET_TIME_AT_NEXT_SEC,
185 return mlxsw_reg_write(mlxsw_core, MLXSW_REG(mtutc), mtutc_pl);
188 static int mlxsw_sp1_ptp_adjfine(struct ptp_clock_info *ptp, long scaled_ppm)
190 struct mlxsw_sp1_ptp_clock *clock = mlxsw_sp1_ptp_clock(ptp);
196 ppb = scaled_ppm_to_ppb(scaled_ppm);
203 adj = clock->nominal_c_mult;
205 diff = div_u64(adj, NSEC_PER_SEC);
207 spin_lock_bh(&clock->lock);
208 timecounter_read(&clock->tc);
209 clock->cycles.mult = neg_adj ? clock->nominal_c_mult - diff :
210 clock->nominal_c_mult + diff;
211 spin_unlock_bh(&clock->lock);
213 return mlxsw_sp_ptp_phc_adjfreq(&clock->common, neg_adj ? -ppb : ppb);
216 static int mlxsw_sp1_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta)
218 struct mlxsw_sp1_ptp_clock *clock = mlxsw_sp1_ptp_clock(ptp);
221 spin_lock_bh(&clock->lock);
222 timecounter_adjtime(&clock->tc, delta);
223 nsec = timecounter_read(&clock->tc);
224 spin_unlock_bh(&clock->lock);
226 return mlxsw_sp1_ptp_phc_settime(clock, nsec);
229 static int mlxsw_sp1_ptp_gettimex(struct ptp_clock_info *ptp,
230 struct timespec64 *ts,
231 struct ptp_system_timestamp *sts)
233 struct mlxsw_sp1_ptp_clock *clock = mlxsw_sp1_ptp_clock(ptp);
236 spin_lock_bh(&clock->lock);
237 cycles = __mlxsw_sp1_ptp_read_frc(clock, sts);
238 nsec = timecounter_cyc2time(&clock->tc, cycles);
239 spin_unlock_bh(&clock->lock);
241 *ts = ns_to_timespec64(nsec);
246 static int mlxsw_sp1_ptp_settime(struct ptp_clock_info *ptp,
247 const struct timespec64 *ts)
249 struct mlxsw_sp1_ptp_clock *clock = mlxsw_sp1_ptp_clock(ptp);
250 u64 nsec = timespec64_to_ns(ts);
252 spin_lock_bh(&clock->lock);
253 timecounter_init(&clock->tc, &clock->cycles, nsec);
254 nsec = timecounter_read(&clock->tc);
255 spin_unlock_bh(&clock->lock);
257 return mlxsw_sp1_ptp_phc_settime(clock, nsec);
260 static const struct ptp_clock_info mlxsw_sp1_ptp_clock_info = {
261 .owner = THIS_MODULE,
262 .name = "mlxsw_sp_clock",
263 .max_adj = 100000000,
264 .adjfine = mlxsw_sp1_ptp_adjfine,
265 .adjtime = mlxsw_sp1_ptp_adjtime,
266 .gettimex64 = mlxsw_sp1_ptp_gettimex,
267 .settime64 = mlxsw_sp1_ptp_settime,
270 static void mlxsw_sp1_ptp_clock_overflow(struct work_struct *work)
272 struct delayed_work *dwork = to_delayed_work(work);
273 struct mlxsw_sp1_ptp_clock *clock;
275 clock = container_of(dwork, struct mlxsw_sp1_ptp_clock, overflow_work);
277 spin_lock_bh(&clock->lock);
278 timecounter_read(&clock->tc);
279 spin_unlock_bh(&clock->lock);
280 mlxsw_core_schedule_dw(&clock->overflow_work, clock->overflow_period);
283 struct mlxsw_sp_ptp_clock *
284 mlxsw_sp1_ptp_clock_init(struct mlxsw_sp *mlxsw_sp, struct device *dev)
286 u64 overflow_cycles, nsec, frac = 0;
287 struct mlxsw_sp1_ptp_clock *clock;
290 clock = kzalloc(sizeof(*clock), GFP_KERNEL);
292 return ERR_PTR(-ENOMEM);
294 spin_lock_init(&clock->lock);
295 clock->cycles.read = mlxsw_sp1_ptp_read_frc;
296 clock->cycles.shift = MLXSW_SP1_PTP_CLOCK_CYCLES_SHIFT;
297 clock->cycles.mult = clocksource_khz2mult(MLXSW_SP1_PTP_CLOCK_FREQ_KHZ,
298 clock->cycles.shift);
299 clock->nominal_c_mult = clock->cycles.mult;
300 clock->cycles.mask = CLOCKSOURCE_MASK(MLXSW_SP1_PTP_CLOCK_MASK);
301 clock->common.core = mlxsw_sp->core;
303 timecounter_init(&clock->tc, &clock->cycles, 0);
305 /* Calculate period in seconds to call the overflow watchdog - to make
306 * sure counter is checked at least twice every wrap around.
307 * The period is calculated as the minimum between max HW cycles count
308 * (The clock source mask) and max amount of cycles that can be
309 * multiplied by clock multiplier where the result doesn't exceed
312 overflow_cycles = div64_u64(~0ULL >> 1, clock->cycles.mult);
313 overflow_cycles = min(overflow_cycles, div_u64(clock->cycles.mask, 3));
315 nsec = cyclecounter_cyc2ns(&clock->cycles, overflow_cycles, 0, &frac);
316 clock->overflow_period = nsecs_to_jiffies(nsec);
318 INIT_DELAYED_WORK(&clock->overflow_work, mlxsw_sp1_ptp_clock_overflow);
319 mlxsw_core_schedule_dw(&clock->overflow_work, 0);
321 clock->common.ptp_info = mlxsw_sp1_ptp_clock_info;
322 clock->common.ptp = ptp_clock_register(&clock->common.ptp_info, dev);
323 if (IS_ERR(clock->common.ptp)) {
324 err = PTR_ERR(clock->common.ptp);
325 dev_err(dev, "ptp_clock_register failed %d\n", err);
326 goto err_ptp_clock_register;
329 return &clock->common;
331 err_ptp_clock_register:
332 cancel_delayed_work_sync(&clock->overflow_work);
337 void mlxsw_sp1_ptp_clock_fini(struct mlxsw_sp_ptp_clock *clock_common)
339 struct mlxsw_sp1_ptp_clock *clock =
340 container_of(clock_common, struct mlxsw_sp1_ptp_clock, common);
342 ptp_clock_unregister(clock_common->ptp);
343 cancel_delayed_work_sync(&clock->overflow_work);
347 static u64 mlxsw_sp2_ptp_read_utc(struct mlxsw_sp_ptp_clock *clock,
348 struct ptp_system_timestamp *sts)
350 struct mlxsw_core *mlxsw_core = clock->core;
351 u32 utc_sec1, utc_sec2, utc_nsec;
353 utc_sec1 = mlxsw_core_read_utc_sec(mlxsw_core);
354 ptp_read_system_prets(sts);
355 utc_nsec = mlxsw_core_read_utc_nsec(mlxsw_core);
356 ptp_read_system_postts(sts);
357 utc_sec2 = mlxsw_core_read_utc_sec(mlxsw_core);
359 if (utc_sec1 != utc_sec2) {
361 ptp_read_system_prets(sts);
362 utc_nsec = mlxsw_core_read_utc_nsec(mlxsw_core);
363 ptp_read_system_postts(sts);
366 return (u64)utc_sec2 * NSEC_PER_SEC + utc_nsec;
370 mlxsw_sp2_ptp_phc_settime(struct mlxsw_sp_ptp_clock *clock, u64 nsec)
372 struct mlxsw_core *mlxsw_core = clock->core;
373 char mtutc_pl[MLXSW_REG_MTUTC_LEN];
376 sec = div_u64_rem(nsec, NSEC_PER_SEC, &nsec_rem);
377 mlxsw_reg_mtutc_pack(mtutc_pl,
378 MLXSW_REG_MTUTC_OPERATION_SET_TIME_IMMEDIATE,
379 0, sec, nsec_rem, 0);
380 return mlxsw_reg_write(mlxsw_core, MLXSW_REG(mtutc), mtutc_pl);
383 static int mlxsw_sp2_ptp_adjfine(struct ptp_clock_info *ptp, long scaled_ppm)
385 struct mlxsw_sp_ptp_clock *clock =
386 container_of(ptp, struct mlxsw_sp_ptp_clock, ptp_info);
387 s32 ppb = scaled_ppm_to_ppb(scaled_ppm);
389 /* In Spectrum-2 and newer ASICs, the frequency adjustment in MTUTC is
390 * reversed, positive values mean to decrease the frequency. Adjust the
391 * sign of PPB to this behavior.
393 return mlxsw_sp_ptp_phc_adjfreq(clock, -ppb);
396 static int mlxsw_sp2_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta)
398 struct mlxsw_sp_ptp_clock *clock =
399 container_of(ptp, struct mlxsw_sp_ptp_clock, ptp_info);
400 struct mlxsw_core *mlxsw_core = clock->core;
401 char mtutc_pl[MLXSW_REG_MTUTC_LEN];
403 /* HW time adjustment range is s16. If out of range, set time instead. */
404 if (delta < S16_MIN || delta > S16_MAX) {
407 nsec = mlxsw_sp2_ptp_read_utc(clock, NULL);
410 return mlxsw_sp2_ptp_phc_settime(clock, nsec);
413 mlxsw_reg_mtutc_pack(mtutc_pl,
414 MLXSW_REG_MTUTC_OPERATION_ADJUST_TIME,
416 return mlxsw_reg_write(mlxsw_core, MLXSW_REG(mtutc), mtutc_pl);
419 static int mlxsw_sp2_ptp_gettimex(struct ptp_clock_info *ptp,
420 struct timespec64 *ts,
421 struct ptp_system_timestamp *sts)
423 struct mlxsw_sp_ptp_clock *clock =
424 container_of(ptp, struct mlxsw_sp_ptp_clock, ptp_info);
427 nsec = mlxsw_sp2_ptp_read_utc(clock, sts);
428 *ts = ns_to_timespec64(nsec);
433 static int mlxsw_sp2_ptp_settime(struct ptp_clock_info *ptp,
434 const struct timespec64 *ts)
436 struct mlxsw_sp_ptp_clock *clock =
437 container_of(ptp, struct mlxsw_sp_ptp_clock, ptp_info);
438 u64 nsec = timespec64_to_ns(ts);
440 return mlxsw_sp2_ptp_phc_settime(clock, nsec);
443 static const struct ptp_clock_info mlxsw_sp2_ptp_clock_info = {
444 .owner = THIS_MODULE,
445 .name = "mlxsw_sp_clock",
446 .max_adj = MLXSW_REG_MTUTC_MAX_FREQ_ADJ,
447 .adjfine = mlxsw_sp2_ptp_adjfine,
448 .adjtime = mlxsw_sp2_ptp_adjtime,
449 .gettimex64 = mlxsw_sp2_ptp_gettimex,
450 .settime64 = mlxsw_sp2_ptp_settime,
453 struct mlxsw_sp_ptp_clock *
454 mlxsw_sp2_ptp_clock_init(struct mlxsw_sp *mlxsw_sp, struct device *dev)
456 struct mlxsw_sp_ptp_clock *clock;
459 clock = kzalloc(sizeof(*clock), GFP_KERNEL);
461 return ERR_PTR(-ENOMEM);
463 clock->core = mlxsw_sp->core;
465 clock->ptp_info = mlxsw_sp2_ptp_clock_info;
467 err = mlxsw_sp2_ptp_phc_settime(clock, 0);
469 dev_err(dev, "setting UTC time failed %d\n", err);
470 goto err_ptp_phc_settime;
473 clock->ptp = ptp_clock_register(&clock->ptp_info, dev);
474 if (IS_ERR(clock->ptp)) {
475 err = PTR_ERR(clock->ptp);
476 dev_err(dev, "ptp_clock_register failed %d\n", err);
477 goto err_ptp_clock_register;
482 err_ptp_clock_register:
488 void mlxsw_sp2_ptp_clock_fini(struct mlxsw_sp_ptp_clock *clock)
490 ptp_clock_unregister(clock->ptp);
494 static int mlxsw_sp_ptp_parse(struct sk_buff *skb,
499 unsigned int ptp_class;
500 struct ptp_header *hdr;
502 ptp_class = ptp_classify_raw(skb);
504 switch (ptp_class & PTP_CLASS_VMASK) {
512 hdr = ptp_parse_header(skb, ptp_class);
516 *p_message_type = ptp_get_msgtype(hdr, ptp_class);
517 *p_domain_number = hdr->domain_number;
518 *p_sequence_id = be16_to_cpu(hdr->sequence_id);
523 /* Returns NULL on successful insertion, a pointer on conflict, or an ERR_PTR on
527 mlxsw_sp1_ptp_unmatched_save(struct mlxsw_sp *mlxsw_sp,
528 struct mlxsw_sp1_ptp_key key,
532 int cycles = MLXSW_SP1_PTP_HT_GC_TIMEOUT / MLXSW_SP1_PTP_HT_GC_INTERVAL;
533 struct mlxsw_sp1_ptp_state *ptp_state = mlxsw_sp1_ptp_state(mlxsw_sp);
534 struct mlxsw_sp1_ptp_unmatched *unmatched;
537 unmatched = kzalloc(sizeof(*unmatched), GFP_ATOMIC);
541 unmatched->key = key;
542 unmatched->skb = skb;
543 unmatched->timestamp = timestamp;
544 unmatched->gc_cycle = ptp_state->gc_cycle + cycles;
546 err = rhltable_insert(&ptp_state->unmatched_ht, &unmatched->ht_node,
547 mlxsw_sp1_ptp_unmatched_ht_params);
554 static struct mlxsw_sp1_ptp_unmatched *
555 mlxsw_sp1_ptp_unmatched_lookup(struct mlxsw_sp *mlxsw_sp,
556 struct mlxsw_sp1_ptp_key key, int *p_length)
558 struct mlxsw_sp1_ptp_state *ptp_state = mlxsw_sp1_ptp_state(mlxsw_sp);
559 struct mlxsw_sp1_ptp_unmatched *unmatched, *last = NULL;
560 struct rhlist_head *tmp, *list;
563 list = rhltable_lookup(&ptp_state->unmatched_ht, &key,
564 mlxsw_sp1_ptp_unmatched_ht_params);
565 rhl_for_each_entry_rcu(unmatched, tmp, list, ht_node) {
575 mlxsw_sp1_ptp_unmatched_remove(struct mlxsw_sp *mlxsw_sp,
576 struct mlxsw_sp1_ptp_unmatched *unmatched)
578 struct mlxsw_sp1_ptp_state *ptp_state = mlxsw_sp1_ptp_state(mlxsw_sp);
580 return rhltable_remove(&ptp_state->unmatched_ht,
582 mlxsw_sp1_ptp_unmatched_ht_params);
585 /* This function is called in the following scenarios:
587 * 1) When a packet is matched with its timestamp.
588 * 2) In several situation when it is necessary to immediately pass on
589 * an SKB without a timestamp.
590 * 3) From GC indirectly through mlxsw_sp1_ptp_unmatched_finish().
591 * This case is similar to 2) above.
593 static void mlxsw_sp1_ptp_packet_finish(struct mlxsw_sp *mlxsw_sp,
594 struct sk_buff *skb, u16 local_port,
596 struct skb_shared_hwtstamps *hwtstamps)
598 struct mlxsw_sp_port *mlxsw_sp_port;
600 /* Between capturing the packet and finishing it, there is a window of
601 * opportunity for the originating port to go away (e.g. due to a
602 * split). Also make sure the SKB device reference is still valid.
604 mlxsw_sp_port = mlxsw_sp->ports[local_port];
605 if (!(mlxsw_sp_port && (!skb->dev || skb->dev == mlxsw_sp_port->dev))) {
606 dev_kfree_skb_any(skb);
612 *skb_hwtstamps(skb) = *hwtstamps;
613 mlxsw_sp_rx_listener_no_mark_func(skb, local_port, mlxsw_sp);
615 /* skb_tstamp_tx() allows hwtstamps to be NULL. */
616 skb_tstamp_tx(skb, hwtstamps);
617 dev_kfree_skb_any(skb);
621 static void mlxsw_sp1_packet_timestamp(struct mlxsw_sp *mlxsw_sp,
622 struct mlxsw_sp1_ptp_key key,
626 struct mlxsw_sp_ptp_clock *clock_common = mlxsw_sp->clock;
627 struct mlxsw_sp1_ptp_clock *clock =
628 container_of(clock_common, struct mlxsw_sp1_ptp_clock, common);
630 struct skb_shared_hwtstamps hwtstamps;
633 spin_lock_bh(&clock->lock);
634 nsec = timecounter_cyc2time(&clock->tc, timestamp);
635 spin_unlock_bh(&clock->lock);
637 hwtstamps.hwtstamp = ns_to_ktime(nsec);
638 mlxsw_sp1_ptp_packet_finish(mlxsw_sp, skb,
639 key.local_port, key.ingress, &hwtstamps);
643 mlxsw_sp1_ptp_unmatched_finish(struct mlxsw_sp *mlxsw_sp,
644 struct mlxsw_sp1_ptp_unmatched *unmatched)
646 if (unmatched->skb && unmatched->timestamp)
647 mlxsw_sp1_packet_timestamp(mlxsw_sp, unmatched->key,
649 unmatched->timestamp);
650 else if (unmatched->skb)
651 mlxsw_sp1_ptp_packet_finish(mlxsw_sp, unmatched->skb,
652 unmatched->key.local_port,
653 unmatched->key.ingress, NULL);
654 kfree_rcu(unmatched, rcu);
657 static void mlxsw_sp1_ptp_unmatched_free_fn(void *ptr, void *arg)
659 struct mlxsw_sp1_ptp_unmatched *unmatched = ptr;
661 /* This is invoked at a point where the ports are gone already. Nothing
662 * to do with whatever is left in the HT but to free it.
665 dev_kfree_skb_any(unmatched->skb);
666 kfree_rcu(unmatched, rcu);
669 static void mlxsw_sp1_ptp_got_piece(struct mlxsw_sp *mlxsw_sp,
670 struct mlxsw_sp1_ptp_key key,
671 struct sk_buff *skb, u64 timestamp)
673 struct mlxsw_sp1_ptp_state *ptp_state = mlxsw_sp1_ptp_state(mlxsw_sp);
674 struct mlxsw_sp1_ptp_unmatched *unmatched;
680 spin_lock(&ptp_state->unmatched_lock);
682 unmatched = mlxsw_sp1_ptp_unmatched_lookup(mlxsw_sp, key, &length);
683 if (skb && unmatched && unmatched->timestamp) {
684 unmatched->skb = skb;
685 } else if (timestamp && unmatched && unmatched->skb) {
686 unmatched->timestamp = timestamp;
688 /* Either there is no entry to match, or one that is there is
692 err = mlxsw_sp1_ptp_unmatched_save(mlxsw_sp, key,
697 mlxsw_sp1_ptp_packet_finish(mlxsw_sp, skb,
704 err = mlxsw_sp1_ptp_unmatched_remove(mlxsw_sp, unmatched);
708 spin_unlock(&ptp_state->unmatched_lock);
711 mlxsw_sp1_ptp_unmatched_finish(mlxsw_sp, unmatched);
716 static void mlxsw_sp1_ptp_got_packet(struct mlxsw_sp *mlxsw_sp,
717 struct sk_buff *skb, u16 local_port,
720 struct mlxsw_sp_port *mlxsw_sp_port;
721 struct mlxsw_sp1_ptp_key key;
725 mlxsw_sp_port = mlxsw_sp->ports[local_port];
729 types = ingress ? mlxsw_sp_port->ptp.ing_types :
730 mlxsw_sp_port->ptp.egr_types;
734 memset(&key, 0, sizeof(key));
735 key.local_port = local_port;
736 key.ingress = ingress;
738 err = mlxsw_sp_ptp_parse(skb, &key.domain_number, &key.message_type,
743 /* For packets whose timestamping was not enabled on this port, don't
744 * bother trying to match the timestamp.
746 if (!((1 << key.message_type) & types))
749 mlxsw_sp1_ptp_got_piece(mlxsw_sp, key, skb, 0);
753 mlxsw_sp1_ptp_packet_finish(mlxsw_sp, skb, local_port, ingress, NULL);
756 void mlxsw_sp1_ptp_got_timestamp(struct mlxsw_sp *mlxsw_sp, bool ingress,
757 u16 local_port, u8 message_type,
758 u8 domain_number, u16 sequence_id,
761 struct mlxsw_sp_port *mlxsw_sp_port;
762 struct mlxsw_sp1_ptp_key key;
765 if (WARN_ON_ONCE(!mlxsw_sp_local_port_is_valid(mlxsw_sp, local_port)))
767 mlxsw_sp_port = mlxsw_sp->ports[local_port];
771 types = ingress ? mlxsw_sp_port->ptp.ing_types :
772 mlxsw_sp_port->ptp.egr_types;
774 /* For message types whose timestamping was not enabled on this port,
775 * don't bother with the timestamp.
777 if (!((1 << message_type) & types))
780 memset(&key, 0, sizeof(key));
781 key.local_port = local_port;
782 key.domain_number = domain_number;
783 key.message_type = message_type;
784 key.sequence_id = sequence_id;
785 key.ingress = ingress;
787 mlxsw_sp1_ptp_got_piece(mlxsw_sp, key, NULL, timestamp);
790 void mlxsw_sp1_ptp_receive(struct mlxsw_sp *mlxsw_sp, struct sk_buff *skb,
793 skb_reset_mac_header(skb);
794 mlxsw_sp1_ptp_got_packet(mlxsw_sp, skb, local_port, true);
797 void mlxsw_sp1_ptp_transmitted(struct mlxsw_sp *mlxsw_sp,
798 struct sk_buff *skb, u16 local_port)
800 mlxsw_sp1_ptp_got_packet(mlxsw_sp, skb, local_port, false);
804 mlxsw_sp1_ptp_ht_gc_collect(struct mlxsw_sp1_ptp_state *ptp_state,
805 struct mlxsw_sp1_ptp_unmatched *unmatched)
807 struct mlxsw_sp *mlxsw_sp = ptp_state->common.mlxsw_sp;
808 struct mlxsw_sp_ptp_port_dir_stats *stats;
809 struct mlxsw_sp_port *mlxsw_sp_port;
812 /* If an unmatched entry has an SKB, it has to be handed over to the
813 * networking stack. This is usually done from a trap handler, which is
814 * invoked in a softirq context. Here we are going to do it in process
815 * context. If that were to be interrupted by a softirq, it could cause
816 * a deadlock when an attempt is made to take an already-taken lock
817 * somewhere along the sending path. Disable softirqs to prevent this.
821 spin_lock(&ptp_state->unmatched_lock);
822 err = rhltable_remove(&ptp_state->unmatched_ht, &unmatched->ht_node,
823 mlxsw_sp1_ptp_unmatched_ht_params);
824 spin_unlock(&ptp_state->unmatched_lock);
827 /* The packet was matched with timestamp during the walk. */
830 mlxsw_sp_port = mlxsw_sp->ports[unmatched->key.local_port];
832 stats = unmatched->key.ingress ?
833 &mlxsw_sp_port->ptp.stats.rx_gcd :
834 &mlxsw_sp_port->ptp.stats.tx_gcd;
841 /* mlxsw_sp1_ptp_unmatched_finish() invokes netif_receive_skb(). While
842 * the comment at that function states that it can only be called in
843 * soft IRQ context, this pattern of local_bh_disable() +
844 * netif_receive_skb(), in process context, is seen elsewhere in the
845 * kernel, notably in pktgen.
847 mlxsw_sp1_ptp_unmatched_finish(mlxsw_sp, unmatched);
853 static void mlxsw_sp1_ptp_ht_gc(struct work_struct *work)
855 struct delayed_work *dwork = to_delayed_work(work);
856 struct mlxsw_sp1_ptp_unmatched *unmatched;
857 struct mlxsw_sp1_ptp_state *ptp_state;
858 struct rhashtable_iter iter;
862 ptp_state = container_of(dwork, struct mlxsw_sp1_ptp_state, ht_gc_dw);
863 gc_cycle = ptp_state->gc_cycle++;
865 rhltable_walk_enter(&ptp_state->unmatched_ht, &iter);
866 rhashtable_walk_start(&iter);
867 while ((obj = rhashtable_walk_next(&iter))) {
872 if (unmatched->gc_cycle <= gc_cycle)
873 mlxsw_sp1_ptp_ht_gc_collect(ptp_state, unmatched);
875 rhashtable_walk_stop(&iter);
876 rhashtable_walk_exit(&iter);
878 mlxsw_core_schedule_dw(&ptp_state->ht_gc_dw,
879 MLXSW_SP1_PTP_HT_GC_INTERVAL);
882 static int mlxsw_sp_ptp_mtptpt_set(struct mlxsw_sp *mlxsw_sp,
883 enum mlxsw_reg_mtptpt_trap_id trap_id,
886 char mtptpt_pl[MLXSW_REG_MTPTPT_LEN];
888 mlxsw_reg_mtptpt_pack(mtptpt_pl, trap_id, message_type);
889 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(mtptpt), mtptpt_pl);
892 static int mlxsw_sp1_ptp_set_fifo_clr_on_trap(struct mlxsw_sp *mlxsw_sp,
895 char mogcr_pl[MLXSW_REG_MOGCR_LEN] = {0};
898 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(mogcr), mogcr_pl);
902 mlxsw_reg_mogcr_ptp_iftc_set(mogcr_pl, clr);
903 mlxsw_reg_mogcr_ptp_eftc_set(mogcr_pl, clr);
904 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(mogcr), mogcr_pl);
907 static int mlxsw_sp1_ptp_mtpppc_set(struct mlxsw_sp *mlxsw_sp,
908 u16 ing_types, u16 egr_types)
910 char mtpppc_pl[MLXSW_REG_MTPPPC_LEN];
912 mlxsw_reg_mtpppc_pack(mtpppc_pl, ing_types, egr_types);
913 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(mtpppc), mtpppc_pl);
916 struct mlxsw_sp1_ptp_shaper_params {
918 enum mlxsw_reg_qpsc_port_speed port_speed;
920 u8 shaper_time_mantissa;
923 u8 port_to_shaper_credits;
924 int ing_timestamp_inc;
925 int egr_timestamp_inc;
928 static const struct mlxsw_sp1_ptp_shaper_params
929 mlxsw_sp1_ptp_shaper_params[] = {
931 .ethtool_speed = SPEED_100,
932 .port_speed = MLXSW_REG_QPSC_PORT_SPEED_100M,
933 .shaper_time_exp = 4,
934 .shaper_time_mantissa = 12,
937 .port_to_shaper_credits = 1,
938 .ing_timestamp_inc = -313,
939 .egr_timestamp_inc = 313,
942 .ethtool_speed = SPEED_1000,
943 .port_speed = MLXSW_REG_QPSC_PORT_SPEED_1G,
944 .shaper_time_exp = 0,
945 .shaper_time_mantissa = 12,
948 .port_to_shaper_credits = 1,
949 .ing_timestamp_inc = -35,
950 .egr_timestamp_inc = 35,
953 .ethtool_speed = SPEED_10000,
954 .port_speed = MLXSW_REG_QPSC_PORT_SPEED_10G,
955 .shaper_time_exp = 0,
956 .shaper_time_mantissa = 2,
959 .port_to_shaper_credits = 1,
960 .ing_timestamp_inc = -11,
961 .egr_timestamp_inc = 11,
964 .ethtool_speed = SPEED_25000,
965 .port_speed = MLXSW_REG_QPSC_PORT_SPEED_25G,
966 .shaper_time_exp = 0,
967 .shaper_time_mantissa = 0,
970 .port_to_shaper_credits = 1,
971 .ing_timestamp_inc = -14,
972 .egr_timestamp_inc = 14,
976 #define MLXSW_SP1_PTP_SHAPER_PARAMS_LEN ARRAY_SIZE(mlxsw_sp1_ptp_shaper_params)
978 static int mlxsw_sp1_ptp_shaper_params_set(struct mlxsw_sp *mlxsw_sp)
980 const struct mlxsw_sp1_ptp_shaper_params *params;
981 char qpsc_pl[MLXSW_REG_QPSC_LEN];
984 for (i = 0; i < MLXSW_SP1_PTP_SHAPER_PARAMS_LEN; i++) {
985 params = &mlxsw_sp1_ptp_shaper_params[i];
986 mlxsw_reg_qpsc_pack(qpsc_pl, params->port_speed,
987 params->shaper_time_exp,
988 params->shaper_time_mantissa,
989 params->shaper_inc, params->shaper_bs,
990 params->port_to_shaper_credits,
991 params->ing_timestamp_inc,
992 params->egr_timestamp_inc);
993 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(qpsc), qpsc_pl);
1001 static int mlxsw_sp_ptp_traps_set(struct mlxsw_sp *mlxsw_sp)
1003 u16 event_message_type;
1006 /* Deliver these message types as PTP0. */
1007 event_message_type = BIT(PTP_MSGTYPE_SYNC) |
1008 BIT(PTP_MSGTYPE_DELAY_REQ) |
1009 BIT(PTP_MSGTYPE_PDELAY_REQ) |
1010 BIT(PTP_MSGTYPE_PDELAY_RESP);
1012 err = mlxsw_sp_ptp_mtptpt_set(mlxsw_sp, MLXSW_REG_MTPTPT_TRAP_ID_PTP0,
1013 event_message_type);
1017 /* Everything else is PTP1. */
1018 err = mlxsw_sp_ptp_mtptpt_set(mlxsw_sp, MLXSW_REG_MTPTPT_TRAP_ID_PTP1,
1019 ~event_message_type);
1021 goto err_mtptpt1_set;
1026 mlxsw_sp_ptp_mtptpt_set(mlxsw_sp, MLXSW_REG_MTPTPT_TRAP_ID_PTP0, 0);
1030 static void mlxsw_sp_ptp_traps_unset(struct mlxsw_sp *mlxsw_sp)
1032 mlxsw_sp_ptp_mtptpt_set(mlxsw_sp, MLXSW_REG_MTPTPT_TRAP_ID_PTP1, 0);
1033 mlxsw_sp_ptp_mtptpt_set(mlxsw_sp, MLXSW_REG_MTPTPT_TRAP_ID_PTP0, 0);
1036 struct mlxsw_sp_ptp_state *mlxsw_sp1_ptp_init(struct mlxsw_sp *mlxsw_sp)
1038 struct mlxsw_sp1_ptp_state *ptp_state;
1041 err = mlxsw_sp1_ptp_shaper_params_set(mlxsw_sp);
1043 return ERR_PTR(err);
1045 ptp_state = kzalloc(sizeof(*ptp_state), GFP_KERNEL);
1047 return ERR_PTR(-ENOMEM);
1048 ptp_state->common.mlxsw_sp = mlxsw_sp;
1050 spin_lock_init(&ptp_state->unmatched_lock);
1052 err = rhltable_init(&ptp_state->unmatched_ht,
1053 &mlxsw_sp1_ptp_unmatched_ht_params);
1055 goto err_hashtable_init;
1057 err = mlxsw_sp_ptp_traps_set(mlxsw_sp);
1059 goto err_ptp_traps_set;
1061 err = mlxsw_sp1_ptp_set_fifo_clr_on_trap(mlxsw_sp, true);
1065 INIT_DELAYED_WORK(&ptp_state->ht_gc_dw, mlxsw_sp1_ptp_ht_gc);
1066 mlxsw_core_schedule_dw(&ptp_state->ht_gc_dw,
1067 MLXSW_SP1_PTP_HT_GC_INTERVAL);
1068 return &ptp_state->common;
1071 mlxsw_sp_ptp_traps_unset(mlxsw_sp);
1073 rhltable_destroy(&ptp_state->unmatched_ht);
1076 return ERR_PTR(err);
1079 void mlxsw_sp1_ptp_fini(struct mlxsw_sp_ptp_state *ptp_state_common)
1081 struct mlxsw_sp *mlxsw_sp = ptp_state_common->mlxsw_sp;
1082 struct mlxsw_sp1_ptp_state *ptp_state;
1084 ptp_state = mlxsw_sp1_ptp_state(mlxsw_sp);
1086 cancel_delayed_work_sync(&ptp_state->ht_gc_dw);
1087 mlxsw_sp1_ptp_mtpppc_set(mlxsw_sp, 0, 0);
1088 mlxsw_sp1_ptp_set_fifo_clr_on_trap(mlxsw_sp, false);
1089 mlxsw_sp_ptp_traps_unset(mlxsw_sp);
1090 rhltable_free_and_destroy(&ptp_state->unmatched_ht,
1091 &mlxsw_sp1_ptp_unmatched_free_fn, NULL);
1095 int mlxsw_sp1_ptp_hwtstamp_get(struct mlxsw_sp_port *mlxsw_sp_port,
1096 struct hwtstamp_config *config)
1098 *config = mlxsw_sp_port->ptp.hwtstamp_config;
1103 mlxsw_sp1_ptp_get_message_types(const struct hwtstamp_config *config,
1104 u16 *p_ing_types, u16 *p_egr_types,
1105 enum hwtstamp_rx_filters *p_rx_filter)
1107 enum hwtstamp_rx_filters rx_filter = config->rx_filter;
1108 enum hwtstamp_tx_types tx_type = config->tx_type;
1109 u16 ing_types = 0x00;
1110 u16 egr_types = 0x00;
1113 case HWTSTAMP_TX_OFF:
1116 case HWTSTAMP_TX_ON:
1119 case HWTSTAMP_TX_ONESTEP_SYNC:
1120 case HWTSTAMP_TX_ONESTEP_P2P:
1126 switch (rx_filter) {
1127 case HWTSTAMP_FILTER_NONE:
1130 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
1131 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
1132 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
1133 case HWTSTAMP_FILTER_PTP_V2_SYNC:
1136 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
1137 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
1138 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
1139 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
1142 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
1143 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
1144 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
1145 case HWTSTAMP_FILTER_PTP_V2_EVENT:
1148 case HWTSTAMP_FILTER_ALL:
1151 case HWTSTAMP_FILTER_SOME:
1152 case HWTSTAMP_FILTER_NTP_ALL:
1158 *p_ing_types = ing_types;
1159 *p_egr_types = egr_types;
1160 *p_rx_filter = rx_filter;
1164 static int mlxsw_sp1_ptp_mtpppc_update(struct mlxsw_sp_port *mlxsw_sp_port,
1165 u16 ing_types, u16 egr_types)
1167 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1168 struct mlxsw_sp_port *tmp;
1169 u16 orig_ing_types = 0;
1170 u16 orig_egr_types = 0;
1174 /* MTPPPC configures timestamping globally, not per port. Find the
1175 * configuration that contains all configured timestamping requests.
1177 for (i = 1; i < mlxsw_core_max_ports(mlxsw_sp->core); i++) {
1178 tmp = mlxsw_sp->ports[i];
1180 orig_ing_types |= tmp->ptp.ing_types;
1181 orig_egr_types |= tmp->ptp.egr_types;
1183 if (tmp && tmp != mlxsw_sp_port) {
1184 ing_types |= tmp->ptp.ing_types;
1185 egr_types |= tmp->ptp.egr_types;
1189 if ((ing_types || egr_types) && !(orig_ing_types || orig_egr_types)) {
1190 err = mlxsw_sp_parsing_depth_inc(mlxsw_sp);
1192 netdev_err(mlxsw_sp_port->dev, "Failed to increase parsing depth");
1196 if (!(ing_types || egr_types) && (orig_ing_types || orig_egr_types))
1197 mlxsw_sp_parsing_depth_dec(mlxsw_sp);
1199 return mlxsw_sp1_ptp_mtpppc_set(mlxsw_sp_port->mlxsw_sp,
1200 ing_types, egr_types);
1203 static bool mlxsw_sp1_ptp_hwtstamp_enabled(struct mlxsw_sp_port *mlxsw_sp_port)
1205 return mlxsw_sp_port->ptp.ing_types || mlxsw_sp_port->ptp.egr_types;
1209 mlxsw_sp1_ptp_port_shaper_set(struct mlxsw_sp_port *mlxsw_sp_port, bool enable)
1211 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1212 char qeec_pl[MLXSW_REG_QEEC_LEN];
1214 mlxsw_reg_qeec_ptps_pack(qeec_pl, mlxsw_sp_port->local_port, enable);
1215 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(qeec), qeec_pl);
1218 static int mlxsw_sp1_ptp_port_shaper_check(struct mlxsw_sp_port *mlxsw_sp_port)
1224 if (!mlxsw_sp1_ptp_hwtstamp_enabled(mlxsw_sp_port))
1225 return mlxsw_sp1_ptp_port_shaper_set(mlxsw_sp_port, false);
1227 err = mlxsw_sp_port_speed_get(mlxsw_sp_port, &speed);
1231 for (i = 0; i < MLXSW_SP1_PTP_SHAPER_PARAMS_LEN; i++) {
1232 if (mlxsw_sp1_ptp_shaper_params[i].ethtool_speed == speed) {
1238 return mlxsw_sp1_ptp_port_shaper_set(mlxsw_sp_port, ptps);
1241 void mlxsw_sp1_ptp_shaper_work(struct work_struct *work)
1243 struct delayed_work *dwork = to_delayed_work(work);
1244 struct mlxsw_sp_port *mlxsw_sp_port;
1247 mlxsw_sp_port = container_of(dwork, struct mlxsw_sp_port,
1250 if (!mlxsw_sp1_ptp_hwtstamp_enabled(mlxsw_sp_port))
1253 err = mlxsw_sp1_ptp_port_shaper_check(mlxsw_sp_port);
1255 netdev_err(mlxsw_sp_port->dev, "Failed to set up PTP shaper\n");
1258 int mlxsw_sp1_ptp_hwtstamp_set(struct mlxsw_sp_port *mlxsw_sp_port,
1259 struct hwtstamp_config *config)
1261 enum hwtstamp_rx_filters rx_filter;
1266 err = mlxsw_sp1_ptp_get_message_types(config, &ing_types, &egr_types,
1271 err = mlxsw_sp1_ptp_mtpppc_update(mlxsw_sp_port, ing_types, egr_types);
1275 mlxsw_sp_port->ptp.hwtstamp_config = *config;
1276 mlxsw_sp_port->ptp.ing_types = ing_types;
1277 mlxsw_sp_port->ptp.egr_types = egr_types;
1279 err = mlxsw_sp1_ptp_port_shaper_check(mlxsw_sp_port);
1283 /* Notify the ioctl caller what we are actually timestamping. */
1284 config->rx_filter = rx_filter;
1289 int mlxsw_sp1_ptp_get_ts_info(struct mlxsw_sp *mlxsw_sp,
1290 struct ethtool_ts_info *info)
1292 info->phc_index = ptp_clock_index(mlxsw_sp->clock->ptp);
1294 info->so_timestamping = SOF_TIMESTAMPING_TX_HARDWARE |
1295 SOF_TIMESTAMPING_RX_HARDWARE |
1296 SOF_TIMESTAMPING_RAW_HARDWARE;
1298 info->tx_types = BIT(HWTSTAMP_TX_OFF) |
1299 BIT(HWTSTAMP_TX_ON);
1301 info->rx_filters = BIT(HWTSTAMP_FILTER_NONE) |
1302 BIT(HWTSTAMP_FILTER_ALL);
1307 struct mlxsw_sp_ptp_port_stat {
1308 char str[ETH_GSTRING_LEN];
1312 #define MLXSW_SP_PTP_PORT_STAT(NAME, FIELD) \
1315 .offset = offsetof(struct mlxsw_sp_ptp_port_stats, \
1319 static const struct mlxsw_sp_ptp_port_stat mlxsw_sp_ptp_port_stats[] = {
1320 MLXSW_SP_PTP_PORT_STAT("ptp_rx_gcd_packets", rx_gcd.packets),
1321 MLXSW_SP_PTP_PORT_STAT("ptp_rx_gcd_timestamps", rx_gcd.timestamps),
1322 MLXSW_SP_PTP_PORT_STAT("ptp_tx_gcd_packets", tx_gcd.packets),
1323 MLXSW_SP_PTP_PORT_STAT("ptp_tx_gcd_timestamps", tx_gcd.timestamps),
1326 #undef MLXSW_SP_PTP_PORT_STAT
1328 #define MLXSW_SP_PTP_PORT_STATS_LEN \
1329 ARRAY_SIZE(mlxsw_sp_ptp_port_stats)
1331 int mlxsw_sp1_get_stats_count(void)
1333 return MLXSW_SP_PTP_PORT_STATS_LEN;
1336 void mlxsw_sp1_get_stats_strings(u8 **p)
1340 for (i = 0; i < MLXSW_SP_PTP_PORT_STATS_LEN; i++) {
1341 memcpy(*p, mlxsw_sp_ptp_port_stats[i].str,
1343 *p += ETH_GSTRING_LEN;
1347 void mlxsw_sp1_get_stats(struct mlxsw_sp_port *mlxsw_sp_port,
1348 u64 *data, int data_index)
1350 void *stats = &mlxsw_sp_port->ptp.stats;
1355 for (i = 0; i < MLXSW_SP_PTP_PORT_STATS_LEN; i++) {
1356 offset = mlxsw_sp_ptp_port_stats[i].offset;
1357 *data++ = *(u64 *)(stats + offset);
1361 struct mlxsw_sp_ptp_state *mlxsw_sp2_ptp_init(struct mlxsw_sp *mlxsw_sp)
1363 struct mlxsw_sp2_ptp_state *ptp_state;
1366 ptp_state = kzalloc(sizeof(*ptp_state), GFP_KERNEL);
1368 return ERR_PTR(-ENOMEM);
1370 ptp_state->common.mlxsw_sp = mlxsw_sp;
1372 err = mlxsw_sp_ptp_traps_set(mlxsw_sp);
1374 goto err_ptp_traps_set;
1376 refcount_set(&ptp_state->ptp_port_enabled_ref, 0);
1377 return &ptp_state->common;
1381 return ERR_PTR(err);
1384 void mlxsw_sp2_ptp_fini(struct mlxsw_sp_ptp_state *ptp_state_common)
1386 struct mlxsw_sp *mlxsw_sp = ptp_state_common->mlxsw_sp;
1387 struct mlxsw_sp2_ptp_state *ptp_state;
1389 ptp_state = mlxsw_sp2_ptp_state(mlxsw_sp);
1391 mlxsw_sp_ptp_traps_unset(mlxsw_sp);
1395 static u32 mlxsw_ptp_utc_time_stamp_sec_get(struct mlxsw_core *mlxsw_core,
1398 u32 utc_sec = mlxsw_core_read_utc_sec(mlxsw_core);
1400 if (cqe_ts_sec > (utc_sec & 0xff))
1401 /* Time stamp above the last bits of UTC (UTC & 0xff) means the
1402 * latter has wrapped after the time stamp was collected.
1407 utc_sec |= cqe_ts_sec;
1412 static void mlxsw_sp2_ptp_hwtstamp_fill(struct mlxsw_core *mlxsw_core,
1413 const struct mlxsw_skb_cb *cb,
1414 struct skb_shared_hwtstamps *hwtstamps)
1416 u64 ts_sec, ts_nsec, nsec;
1418 WARN_ON_ONCE(!cb->cqe_ts.sec && !cb->cqe_ts.nsec);
1420 /* The time stamp in the CQE is represented by 38 bits, which is a short
1421 * representation of UTC time. Software should create the full time
1422 * stamp using the global UTC clock. The seconds have only 8 bits in the
1423 * CQE, to create the full time stamp, use the current UTC time and fix
1424 * the seconds according to the relation between UTC seconds and CQE
1427 ts_sec = mlxsw_ptp_utc_time_stamp_sec_get(mlxsw_core, cb->cqe_ts.sec);
1428 ts_nsec = cb->cqe_ts.nsec;
1430 nsec = ts_sec * NSEC_PER_SEC + ts_nsec;
1432 hwtstamps->hwtstamp = ns_to_ktime(nsec);
1435 void mlxsw_sp2_ptp_receive(struct mlxsw_sp *mlxsw_sp, struct sk_buff *skb,
1438 struct skb_shared_hwtstamps hwtstamps;
1440 mlxsw_sp2_ptp_hwtstamp_fill(mlxsw_sp->core, mlxsw_skb_cb(skb),
1442 *skb_hwtstamps(skb) = hwtstamps;
1443 mlxsw_sp_rx_listener_no_mark_func(skb, local_port, mlxsw_sp);
1446 void mlxsw_sp2_ptp_transmitted(struct mlxsw_sp *mlxsw_sp,
1447 struct sk_buff *skb, u16 local_port)
1449 struct skb_shared_hwtstamps hwtstamps;
1451 mlxsw_sp2_ptp_hwtstamp_fill(mlxsw_sp->core, mlxsw_skb_cb(skb),
1453 skb_tstamp_tx(skb, &hwtstamps);
1454 dev_kfree_skb_any(skb);
1457 int mlxsw_sp2_ptp_hwtstamp_get(struct mlxsw_sp_port *mlxsw_sp_port,
1458 struct hwtstamp_config *config)
1460 struct mlxsw_sp2_ptp_state *ptp_state;
1462 ptp_state = mlxsw_sp2_ptp_state(mlxsw_sp_port->mlxsw_sp);
1464 *config = ptp_state->config;
1469 mlxsw_sp2_ptp_get_message_types(const struct hwtstamp_config *config,
1470 u16 *p_ing_types, u16 *p_egr_types,
1471 enum hwtstamp_rx_filters *p_rx_filter)
1473 enum hwtstamp_rx_filters rx_filter = config->rx_filter;
1474 enum hwtstamp_tx_types tx_type = config->tx_type;
1475 u16 ing_types = 0x00;
1476 u16 egr_types = 0x00;
1478 *p_rx_filter = rx_filter;
1480 switch (rx_filter) {
1481 case HWTSTAMP_FILTER_NONE:
1484 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
1485 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
1486 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
1487 case HWTSTAMP_FILTER_PTP_V2_SYNC:
1488 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
1489 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
1490 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
1491 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
1492 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
1493 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
1494 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
1495 case HWTSTAMP_FILTER_PTP_V2_EVENT:
1496 /* In Spectrum-2 and above, all packets get time stamp by
1497 * default and the driver fill the time stamp only for event
1498 * packets. Return all event types even if only specific types
1502 *p_rx_filter = HWTSTAMP_FILTER_SOME;
1504 case HWTSTAMP_FILTER_ALL:
1505 case HWTSTAMP_FILTER_SOME:
1506 case HWTSTAMP_FILTER_NTP_ALL:
1513 case HWTSTAMP_TX_OFF:
1516 case HWTSTAMP_TX_ON:
1519 case HWTSTAMP_TX_ONESTEP_SYNC:
1520 case HWTSTAMP_TX_ONESTEP_P2P:
1526 *p_ing_types = ing_types;
1527 *p_egr_types = egr_types;
1531 static int mlxsw_sp2_ptp_mtpcpc_set(struct mlxsw_sp *mlxsw_sp, bool ptp_trap_en,
1532 u16 ing_types, u16 egr_types)
1534 char mtpcpc_pl[MLXSW_REG_MTPCPC_LEN];
1536 mlxsw_reg_mtpcpc_pack(mtpcpc_pl, false, 0, ptp_trap_en, ing_types,
1538 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(mtpcpc), mtpcpc_pl);
1541 static int mlxsw_sp2_ptp_enable(struct mlxsw_sp *mlxsw_sp, u16 ing_types,
1543 struct hwtstamp_config new_config)
1545 struct mlxsw_sp2_ptp_state *ptp_state = mlxsw_sp2_ptp_state(mlxsw_sp);
1548 err = mlxsw_sp2_ptp_mtpcpc_set(mlxsw_sp, true, ing_types, egr_types);
1552 ptp_state->config = new_config;
1556 static int mlxsw_sp2_ptp_disable(struct mlxsw_sp *mlxsw_sp,
1557 struct hwtstamp_config new_config)
1559 struct mlxsw_sp2_ptp_state *ptp_state = mlxsw_sp2_ptp_state(mlxsw_sp);
1562 err = mlxsw_sp2_ptp_mtpcpc_set(mlxsw_sp, false, 0, 0);
1566 ptp_state->config = new_config;
1570 static int mlxsw_sp2_ptp_configure_port(struct mlxsw_sp_port *mlxsw_sp_port,
1571 u16 ing_types, u16 egr_types,
1572 struct hwtstamp_config new_config)
1574 struct mlxsw_sp2_ptp_state *ptp_state;
1579 ptp_state = mlxsw_sp2_ptp_state(mlxsw_sp_port->mlxsw_sp);
1581 if (refcount_inc_not_zero(&ptp_state->ptp_port_enabled_ref))
1584 err = mlxsw_sp2_ptp_enable(mlxsw_sp_port->mlxsw_sp, ing_types,
1585 egr_types, new_config);
1589 refcount_set(&ptp_state->ptp_port_enabled_ref, 1);
1594 static int mlxsw_sp2_ptp_deconfigure_port(struct mlxsw_sp_port *mlxsw_sp_port,
1595 struct hwtstamp_config new_config)
1597 struct mlxsw_sp2_ptp_state *ptp_state;
1602 ptp_state = mlxsw_sp2_ptp_state(mlxsw_sp_port->mlxsw_sp);
1604 if (!refcount_dec_and_test(&ptp_state->ptp_port_enabled_ref))
1607 err = mlxsw_sp2_ptp_disable(mlxsw_sp_port->mlxsw_sp, new_config);
1609 goto err_ptp_disable;
1614 refcount_set(&ptp_state->ptp_port_enabled_ref, 1);
1618 int mlxsw_sp2_ptp_hwtstamp_set(struct mlxsw_sp_port *mlxsw_sp_port,
1619 struct hwtstamp_config *config)
1621 enum hwtstamp_rx_filters rx_filter;
1622 struct hwtstamp_config new_config;
1623 u16 new_ing_types, new_egr_types;
1627 err = mlxsw_sp2_ptp_get_message_types(config, &new_ing_types,
1628 &new_egr_types, &rx_filter);
1632 new_config.flags = config->flags;
1633 new_config.tx_type = config->tx_type;
1634 new_config.rx_filter = rx_filter;
1636 ptp_enabled = mlxsw_sp_port->ptp.ing_types ||
1637 mlxsw_sp_port->ptp.egr_types;
1639 if ((new_ing_types || new_egr_types) && !ptp_enabled) {
1640 err = mlxsw_sp2_ptp_configure_port(mlxsw_sp_port, new_ing_types,
1641 new_egr_types, new_config);
1644 } else if (!new_ing_types && !new_egr_types && ptp_enabled) {
1645 err = mlxsw_sp2_ptp_deconfigure_port(mlxsw_sp_port, new_config);
1650 mlxsw_sp_port->ptp.ing_types = new_ing_types;
1651 mlxsw_sp_port->ptp.egr_types = new_egr_types;
1653 /* Notify the ioctl caller what we are actually timestamping. */
1654 config->rx_filter = rx_filter;
1659 int mlxsw_sp2_ptp_get_ts_info(struct mlxsw_sp *mlxsw_sp,
1660 struct ethtool_ts_info *info)
1662 info->phc_index = ptp_clock_index(mlxsw_sp->clock->ptp);
1664 info->so_timestamping = SOF_TIMESTAMPING_TX_HARDWARE |
1665 SOF_TIMESTAMPING_RX_HARDWARE |
1666 SOF_TIMESTAMPING_RAW_HARDWARE;
1668 info->tx_types = BIT(HWTSTAMP_TX_OFF) |
1669 BIT(HWTSTAMP_TX_ON);
1671 info->rx_filters = BIT(HWTSTAMP_FILTER_NONE) |
1672 BIT(HWTSTAMP_FILTER_PTP_V1_L4_EVENT) |
1673 BIT(HWTSTAMP_FILTER_PTP_V2_EVENT);
1678 int mlxsw_sp_ptp_txhdr_construct(struct mlxsw_core *mlxsw_core,
1679 struct mlxsw_sp_port *mlxsw_sp_port,
1680 struct sk_buff *skb,
1681 const struct mlxsw_tx_info *tx_info)
1683 mlxsw_sp_txhdr_construct(skb, tx_info);
1687 int mlxsw_sp2_ptp_txhdr_construct(struct mlxsw_core *mlxsw_core,
1688 struct mlxsw_sp_port *mlxsw_sp_port,
1689 struct sk_buff *skb,
1690 const struct mlxsw_tx_info *tx_info)
1692 /* In Spectrum-2 and Spectrum-3, in order for PTP event packets to have
1693 * their correction field correctly set on the egress port they must be
1694 * transmitted as data packets. Such packets ingress the ASIC via the
1695 * CPU port and must have a VLAN tag, as the CPU port is not configured
1696 * with a PVID. Push the default VLAN (4095), which is configured as
1697 * egress untagged on all the ports.
1699 if (!skb_vlan_tagged(skb)) {
1700 skb = vlan_insert_tag_set_proto(skb, htons(ETH_P_8021Q),
1701 MLXSW_SP_DEFAULT_VID);
1703 this_cpu_inc(mlxsw_sp_port->pcpu_stats->tx_dropped);
1708 return mlxsw_sp_txhdr_ptp_data_construct(mlxsw_core, mlxsw_sp_port, skb,