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>
13 #include <linux/workqueue.h>
15 #define MLX5E_PTP_CHANNEL_IX 0
16 #define MLX5E_PTP_MAX_LOG_SQ_SIZE (8U)
17 #define MLX5E_PTP_TS_CQE_UNDELIVERED_TIMEOUT (1 * NSEC_PER_SEC)
19 struct mlx5e_ptp_metadata_fifo {
26 struct mlx5e_ptp_metadata_map {
27 u16 undelivered_counter;
29 struct sk_buff **data;
33 struct mlx5e_txqsq txqsq;
34 struct mlx5e_cq ts_cq;
35 struct mlx5e_ptp_cq_stats *cq_stats;
38 struct work_struct report_unhealthy_work;
39 struct mlx5e_ptp_port_ts_cqe_list *ts_cqe_pending_list;
40 struct mlx5e_ptp_metadata_fifo metadata_freelist;
41 struct mlx5e_ptp_metadata_map metadata_map;
47 MLX5E_PTP_STATE_NUM_STATES,
52 struct mlx5e_ptpsq ptpsq[MLX5_MAX_NUM_TC];
54 struct napi_struct napi;
56 struct net_device *netdev;
61 /* data path - accessed per napi poll */
62 struct mlx5e_ch_stats *stats;
65 struct mlx5e_priv *priv;
66 struct mlx5_core_dev *mdev;
67 struct hwtstamp_config *tstamp;
68 DECLARE_BITMAP(state, MLX5E_PTP_STATE_NUM_STATES);
71 static inline bool mlx5e_use_ptpsq(struct sk_buff *skb)
75 if (!(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP))
78 if (!skb_flow_dissect_flow_keys(skb, &fk, 0))
81 if (fk.basic.n_proto == htons(ETH_P_1588))
84 if (fk.basic.n_proto != htons(ETH_P_IP) &&
85 fk.basic.n_proto != htons(ETH_P_IPV6))
88 return (fk.basic.ip_proto == IPPROTO_UDP &&
89 fk.ports.dst == htons(PTP_EV_PORT));
92 static inline void mlx5e_ptp_metadata_fifo_push(struct mlx5e_ptp_metadata_fifo *fifo, u8 metadata)
94 fifo->data[fifo->mask & fifo->pc++] = metadata;
98 mlx5e_ptp_metadata_fifo_peek(struct mlx5e_ptp_metadata_fifo *fifo)
100 return fifo->data[fifo->mask & fifo->cc];
104 mlx5e_ptp_metadata_fifo_pop(struct mlx5e_ptp_metadata_fifo *fifo)
110 mlx5e_ptp_metadata_map_put(struct mlx5e_ptp_metadata_map *map,
111 struct sk_buff *skb, u8 metadata)
113 WARN_ON_ONCE(map->data[metadata]);
114 map->data[metadata] = skb;
117 static inline bool mlx5e_ptpsq_metadata_freelist_empty(struct mlx5e_ptpsq *ptpsq)
119 struct mlx5e_ptp_metadata_fifo *freelist;
124 freelist = &ptpsq->metadata_freelist;
126 return freelist->pc == freelist->cc;
129 int mlx5e_ptp_open(struct mlx5e_priv *priv, struct mlx5e_params *params,
130 u8 lag_port, struct mlx5e_ptp **cp);
131 void mlx5e_ptp_close(struct mlx5e_ptp *c);
132 void mlx5e_ptp_activate_channel(struct mlx5e_ptp *c);
133 void mlx5e_ptp_deactivate_channel(struct mlx5e_ptp *c);
134 int mlx5e_ptp_get_rqn(struct mlx5e_ptp *c, u32 *rqn);
135 int mlx5e_ptp_alloc_rx_fs(struct mlx5e_flow_steering *fs,
136 const struct mlx5e_profile *profile);
137 void mlx5e_ptp_free_rx_fs(struct mlx5e_flow_steering *fs,
138 const struct mlx5e_profile *profile);
139 int mlx5e_ptp_rx_manage_fs(struct mlx5e_priv *priv, bool set);
141 void mlx5e_ptpsq_track_metadata(struct mlx5e_ptpsq *ptpsq, u8 metadata);
144 MLX5E_SKB_CB_CQE_HWTSTAMP = BIT(0),
145 MLX5E_SKB_CB_PORT_HWTSTAMP = BIT(1),
148 void mlx5e_skb_cb_hwtstamp_handler(struct sk_buff *skb, int hwtstamp_type,
150 struct mlx5e_ptp_cq_stats *cq_stats);
152 void mlx5e_skb_cb_hwtstamp_init(struct sk_buff *skb);
153 #endif /* __MLX5_EN_PTP_H__ */