1 /* SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB */
2 /* Copyright (c) 2020 Mellanox Technologies. */
3 
4 #ifndef __MLX5_EN_PTP_H__
5 #define __MLX5_EN_PTP_H__
6 
7 #include "en.h"
8 #include "en_stats.h"
9 #include <linux/ptp_classify.h>
10 
11 #define MLX5E_PTP_CHANNEL_IX 0
12 
13 struct mlx5e_ptpsq {
14 	struct mlx5e_txqsq       txqsq;
15 	struct mlx5e_cq          ts_cq;
16 	u16                      skb_fifo_cc;
17 	u16                      skb_fifo_pc;
18 	struct mlx5e_skb_fifo    skb_fifo;
19 	struct mlx5e_ptp_cq_stats *cq_stats;
20 	u16                      ts_cqe_ctr_mask;
21 };
22 
23 enum {
24 	MLX5E_PTP_STATE_TX,
25 	MLX5E_PTP_STATE_RX,
26 	MLX5E_PTP_STATE_NUM_STATES,
27 };
28 
29 struct mlx5e_ptp {
30 	/* data path */
31 	struct mlx5e_ptpsq         ptpsq[MLX5E_MAX_NUM_TC];
32 	struct mlx5e_rq            rq;
33 	struct napi_struct         napi;
34 	struct device             *pdev;
35 	struct net_device         *netdev;
36 	__be32                     mkey_be;
37 	u8                         num_tc;
38 	u8                         lag_port;
39 
40 	/* data path - accessed per napi poll */
41 	struct mlx5e_ch_stats     *stats;
42 
43 	/* control */
44 	struct mlx5e_priv         *priv;
45 	struct mlx5_core_dev      *mdev;
46 	struct hwtstamp_config    *tstamp;
47 	DECLARE_BITMAP(state, MLX5E_PTP_STATE_NUM_STATES);
48 };
49 
50 static inline bool mlx5e_use_ptpsq(struct sk_buff *skb)
51 {
52 	struct flow_keys fk;
53 
54 	if (!(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP))
55 		return false;
56 
57 	if (!skb_flow_dissect_flow_keys(skb, &fk, 0))
58 		return false;
59 
60 	if (fk.basic.n_proto == htons(ETH_P_1588))
61 		return true;
62 
63 	if (fk.basic.n_proto != htons(ETH_P_IP) &&
64 	    fk.basic.n_proto != htons(ETH_P_IPV6))
65 		return false;
66 
67 	return (fk.basic.ip_proto == IPPROTO_UDP &&
68 		fk.ports.dst == htons(PTP_EV_PORT));
69 }
70 
71 int mlx5e_ptp_open(struct mlx5e_priv *priv, struct mlx5e_params *params,
72 		   u8 lag_port, struct mlx5e_ptp **cp);
73 void mlx5e_ptp_close(struct mlx5e_ptp *c);
74 void mlx5e_ptp_activate_channel(struct mlx5e_ptp *c);
75 void mlx5e_ptp_deactivate_channel(struct mlx5e_ptp *c);
76 int mlx5e_ptp_get_rqn(struct mlx5e_ptp *c, u32 *rqn);
77 int mlx5e_ptp_alloc_rx_fs(struct mlx5e_priv *priv);
78 void mlx5e_ptp_free_rx_fs(struct mlx5e_priv *priv);
79 int mlx5e_ptp_rx_manage_fs(struct mlx5e_priv *priv, bool set);
80 
81 enum {
82 	MLX5E_SKB_CB_CQE_HWTSTAMP  = BIT(0),
83 	MLX5E_SKB_CB_PORT_HWTSTAMP = BIT(1),
84 };
85 
86 void mlx5e_skb_cb_hwtstamp_handler(struct sk_buff *skb, int hwtstamp_type,
87 				   ktime_t hwtstamp,
88 				   struct mlx5e_ptp_cq_stats *cq_stats);
89 
90 void mlx5e_skb_cb_hwtstamp_init(struct sk_buff *skb);
91 #endif /* __MLX5_EN_PTP_H__ */
92