net/mlx5e: Make tx_port_ts logic resilient to out-of-order CQEs
[platform/kernel/linux-rpi.git] / drivers / net / ethernet / mellanox / mlx5 / core / en / ptp.h
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 "en/txrx.h"
10 #include <linux/ktime.h>
11 #include <linux/ptp_classify.h>
12 #include <linux/time64.h>
13
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)
17
18 struct mlx5e_ptp_metadata_fifo {
19         u8  cc;
20         u8  pc;
21         u8  mask;
22         u8  *data;
23 };
24
25 struct mlx5e_ptp_metadata_map {
26         u16             undelivered_counter;
27         u16             capacity;
28         struct sk_buff  **data;
29 };
30
31 struct mlx5e_ptpsq {
32         struct mlx5e_txqsq       txqsq;
33         struct mlx5e_cq          ts_cq;
34         struct mlx5e_ptp_cq_stats *cq_stats;
35         u16                      ts_cqe_ctr_mask;
36
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;
40 };
41
42 enum {
43         MLX5E_PTP_STATE_TX,
44         MLX5E_PTP_STATE_RX,
45         MLX5E_PTP_STATE_NUM_STATES,
46 };
47
48 struct mlx5e_ptp {
49         /* data path */
50         struct mlx5e_ptpsq         ptpsq[MLX5E_MAX_NUM_TC];
51         struct mlx5e_rq            rq;
52         struct napi_struct         napi;
53         struct device             *pdev;
54         struct net_device         *netdev;
55         __be32                     mkey_be;
56         u8                         num_tc;
57         u8                         lag_port;
58
59         /* data path - accessed per napi poll */
60         struct mlx5e_ch_stats     *stats;
61
62         /* control */
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);
67 };
68
69 static inline bool mlx5e_use_ptpsq(struct sk_buff *skb)
70 {
71         struct flow_keys fk;
72
73         if (!(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP))
74                 return false;
75
76         if (!skb_flow_dissect_flow_keys(skb, &fk, 0))
77                 return false;
78
79         if (fk.basic.n_proto == htons(ETH_P_1588))
80                 return true;
81
82         if (fk.basic.n_proto != htons(ETH_P_IP) &&
83             fk.basic.n_proto != htons(ETH_P_IPV6))
84                 return false;
85
86         return (fk.basic.ip_proto == IPPROTO_UDP &&
87                 fk.ports.dst == htons(PTP_EV_PORT));
88 }
89
90 static inline void mlx5e_ptp_metadata_fifo_push(struct mlx5e_ptp_metadata_fifo *fifo, u8 metadata)
91 {
92         fifo->data[fifo->mask & fifo->pc++] = metadata;
93 }
94
95 static inline u8
96 mlx5e_ptp_metadata_fifo_pop(struct mlx5e_ptp_metadata_fifo *fifo)
97 {
98         return fifo->data[fifo->mask & fifo->cc++];
99 }
100
101 static inline void
102 mlx5e_ptp_metadata_map_put(struct mlx5e_ptp_metadata_map *map,
103                            struct sk_buff *skb, u8 metadata)
104 {
105         WARN_ON_ONCE(map->data[metadata]);
106         map->data[metadata] = skb;
107 }
108
109 static inline bool mlx5e_ptpsq_metadata_freelist_empty(struct mlx5e_ptpsq *ptpsq)
110 {
111         struct mlx5e_ptp_metadata_fifo *freelist;
112
113         if (likely(!ptpsq))
114                 return false;
115
116         freelist = &ptpsq->metadata_freelist;
117
118         return freelist->pc == freelist->cc;
119 }
120
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);
132
133 void mlx5e_ptpsq_track_metadata(struct mlx5e_ptpsq *ptpsq, u8 metadata);
134
135 enum {
136         MLX5E_SKB_CB_CQE_HWTSTAMP  = BIT(0),
137         MLX5E_SKB_CB_PORT_HWTSTAMP = BIT(1),
138 };
139
140 void mlx5e_skb_cb_hwtstamp_handler(struct sk_buff *skb, int hwtstamp_type,
141                                    ktime_t hwtstamp,
142                                    struct mlx5e_ptp_cq_stats *cq_stats);
143
144 void mlx5e_skb_cb_hwtstamp_init(struct sk_buff *skb);
145 #endif /* __MLX5_EN_PTP_H__ */