1 /* SPDX-License-Identifier: GPL-2.0 */
2 /* Copyright(c) 2017 - 2019 Pensando Systems, Inc */
7 #include <linux/ptp_clock_kernel.h>
8 #include <linux/timecounter.h>
9 #include <uapi/linux/net_tstamp.h>
10 #include <linux/dim.h>
11 #include <linux/pci.h>
12 #include "ionic_rx_filter.h"
14 #define IONIC_ADMINQ_LENGTH 16 /* must be a power of two */
15 #define IONIC_NOTIFYQ_LENGTH 64 /* must be a power of two */
18 #define DEL_ADDR false
19 #define CAN_SLEEP true
20 #define CAN_NOT_SLEEP false
22 #define IONIC_RX_COPYBREAK_DEFAULT 256
23 #define IONIC_TX_BUDGET_DEFAULT 256
25 struct ionic_tx_stats {
43 struct ionic_rx_stats {
62 #define IONIC_QCQ_F_INITED BIT(0)
63 #define IONIC_QCQ_F_SG BIT(1)
64 #define IONIC_QCQ_F_INTR BIT(2)
65 #define IONIC_QCQ_F_TX_STATS BIT(3)
66 #define IONIC_QCQ_F_RX_STATS BIT(4)
67 #define IONIC_QCQ_F_NOTIFYQ BIT(5)
68 #define IONIC_QCQ_F_CMB_RINGS BIT(6)
76 dma_addr_t cq_base_pa;
78 dma_addr_t sg_base_pa;
81 void __iomem *cmb_q_base;
82 phys_addr_t cmb_q_base_pa;
89 struct napi_struct napi;
90 struct ionic_intr_info intr;
91 struct work_struct doorbell_napi_work;
92 struct dentry *dentry;
95 #define q_to_qcq(q) container_of(q, struct ionic_qcq, q)
96 #define q_to_tx_stats(q) (&(q)->lif->txqstats[(q)->index])
97 #define q_to_rx_stats(q) (&(q)->lif->rxqstats[(q)->index])
98 #define napi_to_qcq(napi) container_of(napi, struct ionic_qcq, napi)
99 #define napi_to_cq(napi) (&napi_to_qcq(napi)->cq)
101 enum ionic_deferred_work_type {
102 IONIC_DW_TYPE_RX_MODE,
103 IONIC_DW_TYPE_LINK_STATUS,
104 IONIC_DW_TYPE_LIF_RESET,
107 struct ionic_deferred_work {
108 struct list_head list;
109 enum ionic_deferred_work_type type;
116 struct ionic_deferred {
117 spinlock_t lock; /* lock for deferred work list */
118 struct list_head list;
119 struct work_struct work;
122 struct ionic_lif_sw_stats {
132 u64 rx_csum_complete;
134 u64 tx_hwstamp_valid;
135 u64 tx_hwstamp_invalid;
136 u64 rx_hwstamp_valid;
137 u64 rx_hwstamp_invalid;
140 u64 hw_rx_over_errors;
141 u64 hw_rx_missed_errors;
142 u64 hw_tx_aborted_errors;
151 enum ionic_lif_state_flags {
154 IONIC_LIF_F_LINK_CHECK_REQUESTED,
155 IONIC_LIF_F_FILTER_SYNC_NEEDED,
156 IONIC_LIF_F_FW_RESET,
157 IONIC_LIF_F_FW_STOPPING,
158 IONIC_LIF_F_SPLIT_INTR,
160 IONIC_LIF_F_TX_DIM_INTR,
161 IONIC_LIF_F_RX_DIM_INTR,
162 IONIC_LIF_F_CMB_TX_RINGS,
163 IONIC_LIF_F_CMB_RX_RINGS,
165 /* leave this as last */
166 IONIC_LIF_F_STATE_SIZE
169 struct ionic_qtype_info {
182 #define IONIC_LIF_NAME_MAX_SZ 32
184 struct net_device *netdev;
185 DECLARE_BITMAP(state, IONIC_LIF_F_STATE_SIZE);
188 unsigned int hw_index;
189 struct mutex queue_lock; /* lock for queue structures */
190 struct mutex config_lock; /* lock for config actions */
191 spinlock_t adminq_lock; /* lock for AdminQ operations */
192 struct ionic_qcq *adminqcq;
193 struct ionic_qcq *notifyqcq;
194 struct ionic_qcq **txqcqs;
195 struct ionic_qcq *hwstamp_txq;
196 struct ionic_tx_stats *txqstats;
197 struct ionic_qcq **rxqcqs;
198 struct ionic_qcq *hwstamp_rxq;
199 struct ionic_rx_stats *rxqstats;
200 struct ionic_deferred deferred;
201 struct work_struct tx_timeout_work;
203 unsigned int kern_pid;
204 u64 __iomem *kern_dbpage;
207 unsigned int ntxq_descs;
208 unsigned int nrxq_descs;
216 unsigned int link_down_count;
220 unsigned int max_vlans;
221 char name[IONIC_LIF_NAME_MAX_SZ];
223 union ionic_lif_identity *identity;
224 struct ionic_lif_info *info;
227 struct ionic_qtype_info qtype_info[IONIC_QTYPE_MAX];
229 u8 rss_hash_key[IONIC_RSS_HASH_KEY_SIZE];
231 dma_addr_t rss_ind_tbl_pa;
235 struct ionic_rx_filters rx_filters;
236 u32 rx_coalesce_usecs; /* what the user asked for */
237 u32 rx_coalesce_hw; /* what the hw is using */
238 u32 tx_coalesce_usecs; /* what the user asked for */
239 u32 tx_coalesce_hw; /* what the hw is using */
240 unsigned int dbid_count;
242 struct ionic_phc *phc;
244 struct dentry *dentry;
245 struct bpf_prog *xdp_prog;
249 spinlock_t lock; /* lock for cc and tc */
250 struct cyclecounter cc;
251 struct timecounter tc;
253 struct mutex config_lock; /* lock for ts_config */
254 struct hwtstamp_config ts_config;
255 u64 ts_config_rx_filt;
256 u32 ts_config_tx_mode;
261 struct ptp_clock_info ptp_info;
262 struct ptp_clock *ptp;
263 struct ionic_lif *lif;
266 struct ionic_queue_params {
268 unsigned int ntxq_descs;
269 unsigned int nrxq_descs;
271 struct bpf_prog *xdp_prog;
277 static inline void ionic_init_queue_params(struct ionic_lif *lif,
278 struct ionic_queue_params *qparam)
280 qparam->nxqs = lif->nxqs;
281 qparam->ntxq_descs = lif->ntxq_descs;
282 qparam->nrxq_descs = lif->nrxq_descs;
283 qparam->rxq_features = lif->rxq_features;
284 qparam->xdp_prog = lif->xdp_prog;
285 qparam->intr_split = test_bit(IONIC_LIF_F_SPLIT_INTR, lif->state);
286 qparam->cmb_tx = test_bit(IONIC_LIF_F_CMB_TX_RINGS, lif->state);
287 qparam->cmb_rx = test_bit(IONIC_LIF_F_CMB_RX_RINGS, lif->state);
290 static inline void ionic_set_queue_params(struct ionic_lif *lif,
291 struct ionic_queue_params *qparam)
293 lif->nxqs = qparam->nxqs;
294 lif->ntxq_descs = qparam->ntxq_descs;
295 lif->nrxq_descs = qparam->nrxq_descs;
296 lif->rxq_features = qparam->rxq_features;
298 if (qparam->intr_split)
299 set_bit(IONIC_LIF_F_SPLIT_INTR, lif->state);
301 clear_bit(IONIC_LIF_F_SPLIT_INTR, lif->state);
304 set_bit(IONIC_LIF_F_CMB_TX_RINGS, lif->state);
306 clear_bit(IONIC_LIF_F_CMB_TX_RINGS, lif->state);
309 set_bit(IONIC_LIF_F_CMB_RX_RINGS, lif->state);
311 clear_bit(IONIC_LIF_F_CMB_RX_RINGS, lif->state);
314 static inline u32 ionic_coal_usec_to_hw(struct ionic *ionic, u32 usecs)
316 u32 mult = le32_to_cpu(ionic->ident.dev.intr_coal_mult);
317 u32 div = le32_to_cpu(ionic->ident.dev.intr_coal_div);
319 /* Div-by-zero should never be an issue, but check anyway */
323 /* Round up in case usecs is close to the next hw unit */
324 usecs += (div / mult) >> 1;
326 /* Convert from usecs to device units */
327 return (usecs * mult) / div;
330 static inline bool ionic_txq_hwstamp_enabled(struct ionic_queue *q)
332 return q->features & IONIC_TXQ_F_HWSTAMP;
335 void ionic_link_status_check_request(struct ionic_lif *lif, bool can_sleep);
336 void ionic_get_stats64(struct net_device *netdev,
337 struct rtnl_link_stats64 *ns);
338 void ionic_lif_deferred_enqueue(struct ionic_lif *lif,
339 struct ionic_deferred_work *work);
340 int ionic_lif_alloc(struct ionic *ionic);
341 int ionic_lif_init(struct ionic_lif *lif);
342 void ionic_lif_free(struct ionic_lif *lif);
343 void ionic_lif_deinit(struct ionic_lif *lif);
345 int ionic_lif_addr_add(struct ionic_lif *lif, const u8 *addr);
346 int ionic_lif_addr_del(struct ionic_lif *lif, const u8 *addr);
348 void ionic_stop_queues_reconfig(struct ionic_lif *lif);
349 void ionic_txrx_free(struct ionic_lif *lif);
350 void ionic_qcqs_free(struct ionic_lif *lif);
351 int ionic_restart_lif(struct ionic_lif *lif);
353 int ionic_lif_register(struct ionic_lif *lif);
354 void ionic_lif_unregister(struct ionic_lif *lif);
355 int ionic_lif_identify(struct ionic *ionic, u8 lif_type,
356 union ionic_lif_identity *lif_ident);
357 int ionic_lif_size(struct ionic *ionic);
359 #if IS_ENABLED(CONFIG_PTP_1588_CLOCK)
360 void ionic_lif_hwstamp_replay(struct ionic_lif *lif);
361 void ionic_lif_hwstamp_recreate_queues(struct ionic_lif *lif);
362 int ionic_lif_hwstamp_set(struct ionic_lif *lif, struct ifreq *ifr);
363 int ionic_lif_hwstamp_get(struct ionic_lif *lif, struct ifreq *ifr);
364 ktime_t ionic_lif_phc_ktime(struct ionic_lif *lif, u64 counter);
365 void ionic_lif_register_phc(struct ionic_lif *lif);
366 void ionic_lif_unregister_phc(struct ionic_lif *lif);
367 void ionic_lif_alloc_phc(struct ionic_lif *lif);
368 void ionic_lif_free_phc(struct ionic_lif *lif);
370 static inline void ionic_lif_hwstamp_replay(struct ionic_lif *lif) {}
371 static inline void ionic_lif_hwstamp_recreate_queues(struct ionic_lif *lif) {}
373 static inline int ionic_lif_hwstamp_set(struct ionic_lif *lif, struct ifreq *ifr)
378 static inline int ionic_lif_hwstamp_get(struct ionic_lif *lif, struct ifreq *ifr)
383 static inline ktime_t ionic_lif_phc_ktime(struct ionic_lif *lif, u64 counter)
385 return ns_to_ktime(0);
388 static inline void ionic_lif_register_phc(struct ionic_lif *lif) {}
389 static inline void ionic_lif_unregister_phc(struct ionic_lif *lif) {}
390 static inline void ionic_lif_alloc_phc(struct ionic_lif *lif) {}
391 static inline void ionic_lif_free_phc(struct ionic_lif *lif) {}
394 int ionic_lif_create_hwstamp_txq(struct ionic_lif *lif);
395 int ionic_lif_create_hwstamp_rxq(struct ionic_lif *lif);
396 int ionic_lif_config_hwstamp_rxq_all(struct ionic_lif *lif, bool rx_all);
397 int ionic_lif_set_hwstamp_txmode(struct ionic_lif *lif, u16 txstamp_mode);
398 int ionic_lif_set_hwstamp_rxfilt(struct ionic_lif *lif, u64 pkt_class);
400 int ionic_lif_rss_config(struct ionic_lif *lif, u16 types,
401 const u8 *key, const u32 *indir);
402 void ionic_lif_rx_mode(struct ionic_lif *lif);
403 int ionic_reconfigure_queues(struct ionic_lif *lif,
404 struct ionic_queue_params *qparam);
405 #endif /* _IONIC_LIF_H_ */