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