(*clock_init)(struct mlxsw_sp *mlxsw_sp, struct device *dev);
        void (*clock_fini)(struct mlxsw_sp_ptp_clock *clock);
 
+       struct mlxsw_sp_ptp_state *(*init)(struct mlxsw_sp *mlxsw_sp);
+       void (*fini)(struct mlxsw_sp_ptp_state *ptp_state);
+
        /* Notify a driver that a packet that might be PTP was received. Driver
         * is responsible for freeing the passed-in SKB.
         */
 static const struct mlxsw_sp_ptp_ops mlxsw_sp1_ptp_ops = {
        .clock_init     = mlxsw_sp1_ptp_clock_init,
        .clock_fini     = mlxsw_sp1_ptp_clock_fini,
+       .init           = mlxsw_sp1_ptp_init,
+       .fini           = mlxsw_sp1_ptp_fini,
        .receive        = mlxsw_sp1_ptp_receive,
        .transmitted    = mlxsw_sp1_ptp_transmitted,
 };
 static const struct mlxsw_sp_ptp_ops mlxsw_sp2_ptp_ops = {
        .clock_init     = mlxsw_sp2_ptp_clock_init,
        .clock_fini     = mlxsw_sp2_ptp_clock_fini,
+       .init           = mlxsw_sp2_ptp_init,
+       .fini           = mlxsw_sp2_ptp_fini,
        .receive        = mlxsw_sp2_ptp_receive,
        .transmitted    = mlxsw_sp2_ptp_transmitted,
 };
                }
        }
 
+       if (mlxsw_sp->clock) {
+               /* NULL is a valid return value from ptp_ops->init */
+               mlxsw_sp->ptp_state = mlxsw_sp->ptp_ops->init(mlxsw_sp);
+               if (IS_ERR(mlxsw_sp->ptp_state)) {
+                       err = PTR_ERR(mlxsw_sp->ptp_state);
+                       dev_err(mlxsw_sp->bus_info->dev, "Failed to initialize PTP\n");
+                       goto err_ptp_init;
+               }
+       }
+
        /* Initialize netdevice notifier after router and SPAN is initialized,
         * so that the event handler can use router structures and call SPAN
         * respin.
 err_dpipe_init:
        unregister_netdevice_notifier(&mlxsw_sp->netdevice_nb);
 err_netdev_notifier:
+       if (mlxsw_sp->clock)
+               mlxsw_sp->ptp_ops->fini(mlxsw_sp->ptp_state);
+err_ptp_init:
        if (mlxsw_sp->clock)
                mlxsw_sp->ptp_ops->clock_fini(mlxsw_sp->clock);
 err_ptp_clock_init:
        mlxsw_sp_ports_remove(mlxsw_sp);
        mlxsw_sp_dpipe_fini(mlxsw_sp);
        unregister_netdevice_notifier(&mlxsw_sp->netdevice_nb);
-       if (mlxsw_sp->clock)
+       if (mlxsw_sp->clock) {
+               mlxsw_sp->ptp_ops->fini(mlxsw_sp->ptp_state);
                mlxsw_sp->ptp_ops->clock_fini(mlxsw_sp->clock);
+       }
        mlxsw_sp_router_fini(mlxsw_sp);
        mlxsw_sp_acl_fini(mlxsw_sp);
        mlxsw_sp_nve_fini(mlxsw_sp);
 
 struct mlxsw_sp_nve_ops;
 struct mlxsw_sp_sb_vals;
 struct mlxsw_sp_port_type_speed_ops;
+struct mlxsw_sp_ptp_state;
 struct mlxsw_sp_ptp_ops;
 
 struct mlxsw_sp {
        struct mlxsw_sp_nve *nve;
        struct notifier_block netdevice_nb;
        struct mlxsw_sp_ptp_clock *clock;
+       struct mlxsw_sp_ptp_state *ptp_state;
 
        struct mlxsw_sp_counter_pool *counter_pool;
        struct {
 
 #include <linux/spinlock.h>
 #include <linux/device.h>
 
+#include "spectrum.h"
 #include "spectrum_ptp.h"
 #include "core.h"
 
 #define MLXSW_SP1_PTP_CLOCK_FREQ_KHZ           156257 /* 6.4nSec */
 #define MLXSW_SP1_PTP_CLOCK_MASK               64
 
+struct mlxsw_sp_ptp_state {
+       struct rhashtable unmatched_ht;
+       spinlock_t unmatched_lock; /* protects the HT */
+};
+
+struct mlxsw_sp1_ptp_key {
+       u8 local_port;
+       u8 message_type;
+       u16 sequence_id;
+       u8 domain_number;
+       bool ingress;
+};
+
+struct mlxsw_sp1_ptp_unmatched {
+       struct mlxsw_sp1_ptp_key key;
+       struct rhash_head ht_node;
+       struct rcu_head rcu;
+       struct sk_buff *skb;
+       u64 timestamp;
+};
+
+static const struct rhashtable_params mlxsw_sp1_ptp_unmatched_ht_params = {
+       .key_len = sizeof_field(struct mlxsw_sp1_ptp_unmatched, key),
+       .key_offset = offsetof(struct mlxsw_sp1_ptp_unmatched, key),
+       .head_offset = offsetof(struct mlxsw_sp1_ptp_unmatched, ht_node),
+};
+
 struct mlxsw_sp_ptp_clock {
        struct mlxsw_core *core;
        spinlock_t lock; /* protect this structure */
        kfree(clock);
 }
 
+static void mlxsw_sp1_ptp_unmatched_free_fn(void *ptr, void *arg)
+{
+       struct mlxsw_sp1_ptp_unmatched *unmatched = ptr;
+
+       /* This is invoked at a point where the ports are gone already. Nothing
+        * to do with whatever is left in the HT but to free it.
+        */
+       if (unmatched->skb)
+               dev_kfree_skb_any(unmatched->skb);
+       kfree_rcu(unmatched, rcu);
+}
+
 void mlxsw_sp1_ptp_receive(struct mlxsw_sp *mlxsw_sp, struct sk_buff *skb,
                           u8 local_port)
 {
 {
        dev_kfree_skb_any(skb);
 }
+
+struct mlxsw_sp_ptp_state *mlxsw_sp1_ptp_init(struct mlxsw_sp *mlxsw_sp)
+{
+       struct mlxsw_sp_ptp_state *ptp_state;
+       int err;
+
+       ptp_state = kzalloc(sizeof(*ptp_state), GFP_KERNEL);
+       if (!ptp_state)
+               return ERR_PTR(-ENOMEM);
+
+       spin_lock_init(&ptp_state->unmatched_lock);
+
+       err = rhashtable_init(&ptp_state->unmatched_ht,
+                             &mlxsw_sp1_ptp_unmatched_ht_params);
+       if (err)
+               goto err_hashtable_init;
+
+       return ptp_state;
+
+err_hashtable_init:
+       kfree(ptp_state);
+       return ERR_PTR(err);
+}
+
+void mlxsw_sp1_ptp_fini(struct mlxsw_sp_ptp_state *ptp_state)
+{
+       rhashtable_free_and_destroy(&ptp_state->unmatched_ht,
+                                   &mlxsw_sp1_ptp_unmatched_free_fn, NULL);
+       kfree(ptp_state);
+}
 
 #define _MLXSW_SPECTRUM_PTP_H
 
 #include <linux/device.h>
+#include <linux/rhashtable.h>
 
-#include "spectrum.h"
-
+struct mlxsw_sp;
+struct mlxsw_sp_port;
 struct mlxsw_sp_ptp_clock;
 
 #if IS_REACHABLE(CONFIG_PTP_1588_CLOCK)
 
 void mlxsw_sp1_ptp_clock_fini(struct mlxsw_sp_ptp_clock *clock);
 
+struct mlxsw_sp_ptp_state *mlxsw_sp1_ptp_init(struct mlxsw_sp *mlxsw_sp);
+
+void mlxsw_sp1_ptp_fini(struct mlxsw_sp_ptp_state *ptp_state);
+
 void mlxsw_sp1_ptp_receive(struct mlxsw_sp *mlxsw_sp, struct sk_buff *skb,
                           u8 local_port);
 
 {
 }
 
+static inline struct mlxsw_sp_ptp_state *
+mlxsw_sp1_ptp_init(struct mlxsw_sp *mlxsw_sp)
+{
+       return NULL;
+}
+
+static inline void mlxsw_sp1_ptp_fini(struct mlxsw_sp_ptp_state *ptp_state)
+{
+}
+
 static inline void mlxsw_sp1_ptp_receive(struct mlxsw_sp *mlxsw_sp,
                                         struct sk_buff *skb, u8 local_port)
 {
 {
 }
 
+static inline struct mlxsw_sp_ptp_state *
+mlxsw_sp2_ptp_init(struct mlxsw_sp *mlxsw_sp)
+{
+       return NULL;
+}
+
+static inline void mlxsw_sp2_ptp_fini(struct mlxsw_sp_ptp_state *ptp_state)
+{
+}
+
 static inline void mlxsw_sp2_ptp_receive(struct mlxsw_sp *mlxsw_sp,
                                         struct sk_buff *skb, u8 local_port)
 {