1 /* SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB */
2 /* Copyright (c) 2020 Mellanox Technologies. */
4 #ifndef __MLX5_EN_PTP_H__
5 #define __MLX5_EN_PTP_H__
10 #include <linux/ktime.h>
11 #include <linux/ptp_classify.h>
12 #include <linux/time64.h>
14 #define MLX5E_PTP_CHANNEL_IX 0
15 #define MLX5E_PTP_MAX_LOG_SQ_SIZE (8U)
16 #define MLX5E_PTP_TS_CQE_UNDELIVERED_TIMEOUT (1 * NSEC_PER_SEC)
18 struct mlx5e_ptp_metadata_fifo {
25 struct mlx5e_ptp_metadata_map {
26 u16 undelivered_counter;
28 struct sk_buff **data;
32 struct mlx5e_txqsq txqsq;
33 struct mlx5e_cq ts_cq;
34 struct mlx5e_ptp_cq_stats *cq_stats;
37 struct mlx5e_ptp_port_ts_cqe_list *ts_cqe_pending_list;
38 struct mlx5e_ptp_metadata_fifo metadata_freelist;
39 struct mlx5e_ptp_metadata_map metadata_map;
45 MLX5E_PTP_STATE_NUM_STATES,
50 struct mlx5e_ptpsq ptpsq[MLX5E_MAX_NUM_TC];
52 struct napi_struct napi;
54 struct net_device *netdev;
59 /* data path - accessed per napi poll */
60 struct mlx5e_ch_stats *stats;
63 struct mlx5e_priv *priv;
64 struct mlx5_core_dev *mdev;
65 struct hwtstamp_config *tstamp;
66 DECLARE_BITMAP(state, MLX5E_PTP_STATE_NUM_STATES);
69 static inline bool mlx5e_use_ptpsq(struct sk_buff *skb)
73 if (!(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP))
76 if (!skb_flow_dissect_flow_keys(skb, &fk, 0))
79 if (fk.basic.n_proto == htons(ETH_P_1588))
82 if (fk.basic.n_proto != htons(ETH_P_IP) &&
83 fk.basic.n_proto != htons(ETH_P_IPV6))
86 return (fk.basic.ip_proto == IPPROTO_UDP &&
87 fk.ports.dst == htons(PTP_EV_PORT));
90 static inline void mlx5e_ptp_metadata_fifo_push(struct mlx5e_ptp_metadata_fifo *fifo, u8 metadata)
92 fifo->data[fifo->mask & fifo->pc++] = metadata;
96 mlx5e_ptp_metadata_fifo_pop(struct mlx5e_ptp_metadata_fifo *fifo)
98 return fifo->data[fifo->mask & fifo->cc++];
102 mlx5e_ptp_metadata_map_put(struct mlx5e_ptp_metadata_map *map,
103 struct sk_buff *skb, u8 metadata)
105 WARN_ON_ONCE(map->data[metadata]);
106 map->data[metadata] = skb;
109 static inline bool mlx5e_ptpsq_metadata_freelist_empty(struct mlx5e_ptpsq *ptpsq)
111 struct mlx5e_ptp_metadata_fifo *freelist;
116 freelist = &ptpsq->metadata_freelist;
118 return freelist->pc == freelist->cc;
121 int mlx5e_ptp_open(struct mlx5e_priv *priv, struct mlx5e_params *params,
122 u8 lag_port, struct mlx5e_ptp **cp);
123 void mlx5e_ptp_close(struct mlx5e_ptp *c);
124 void mlx5e_ptp_activate_channel(struct mlx5e_ptp *c);
125 void mlx5e_ptp_deactivate_channel(struct mlx5e_ptp *c);
126 int mlx5e_ptp_get_rqn(struct mlx5e_ptp *c, u32 *rqn);
127 int mlx5e_ptp_alloc_rx_fs(struct mlx5e_flow_steering *fs,
128 const struct mlx5e_profile *profile);
129 void mlx5e_ptp_free_rx_fs(struct mlx5e_flow_steering *fs,
130 const struct mlx5e_profile *profile);
131 int mlx5e_ptp_rx_manage_fs(struct mlx5e_priv *priv, bool set);
133 void mlx5e_ptpsq_track_metadata(struct mlx5e_ptpsq *ptpsq, u8 metadata);
136 MLX5E_SKB_CB_CQE_HWTSTAMP = BIT(0),
137 MLX5E_SKB_CB_PORT_HWTSTAMP = BIT(1),
140 void mlx5e_skb_cb_hwtstamp_handler(struct sk_buff *skb, int hwtstamp_type,
142 struct mlx5e_ptp_cq_stats *cq_stats);
144 void mlx5e_skb_cb_hwtstamp_init(struct sk_buff *skb);
145 #endif /* __MLX5_EN_PTP_H__ */