2 * Copyright (c) 2015-2016, Mellanox Technologies. All rights reserved.
4 * This software is available to you under a choice of one of two
5 * licenses. You may choose to be licensed under the terms of the GNU
6 * General Public License (GPL) Version 2, available from the file
7 * COPYING in the main directory of this source tree, or the
8 * OpenIB.org BSD license below:
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
18 * - Redistributions in binary form must reproduce the above
19 * copyright notice, this list of conditions and the following
20 * disclaimer in the documentation and/or other materials
21 * provided with the distribution.
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
35 #include <linux/if_vlan.h>
36 #include <linux/etherdevice.h>
37 #include <linux/timecounter.h>
38 #include <linux/net_tstamp.h>
39 #include <linux/ptp_clock_kernel.h>
40 #include <linux/mlx5/driver.h>
41 #include <linux/mlx5/qp.h>
42 #include <linux/mlx5/cq.h>
43 #include <linux/mlx5/port.h>
44 #include <linux/mlx5/vport.h>
45 #include <linux/mlx5/transobj.h>
46 #include <linux/rhashtable.h>
48 #include "mlx5_core.h"
50 #define MLX5E_MAX_NUM_TC 8
52 #define MLX5E_PARAMS_MINIMUM_LOG_SQ_SIZE 0x6
53 #define MLX5E_PARAMS_DEFAULT_LOG_SQ_SIZE 0xa
54 #define MLX5E_PARAMS_MAXIMUM_LOG_SQ_SIZE 0xd
56 #define MLX5E_PARAMS_MINIMUM_LOG_RQ_SIZE 0x1
57 #define MLX5E_PARAMS_DEFAULT_LOG_RQ_SIZE 0xa
58 #define MLX5E_PARAMS_MAXIMUM_LOG_RQ_SIZE 0xd
60 #define MLX5E_PARAMS_DEFAULT_LRO_WQE_SZ (64 * 1024)
61 #define MLX5E_PARAMS_DEFAULT_RX_CQ_MODERATION_USEC 0x10
62 #define MLX5E_PARAMS_DEFAULT_RX_CQ_MODERATION_PKTS 0x20
63 #define MLX5E_PARAMS_DEFAULT_TX_CQ_MODERATION_USEC 0x10
64 #define MLX5E_PARAMS_DEFAULT_TX_CQ_MODERATION_PKTS 0x20
65 #define MLX5E_PARAMS_DEFAULT_MIN_RX_WQES 0x80
67 #define MLX5E_LOG_INDIR_RQT_SIZE 0x7
68 #define MLX5E_INDIR_RQT_SIZE BIT(MLX5E_LOG_INDIR_RQT_SIZE)
69 #define MLX5E_MAX_NUM_CHANNELS (MLX5E_INDIR_RQT_SIZE >> 1)
70 #define MLX5E_TX_CQ_POLL_BUDGET 128
71 #define MLX5E_UPDATE_STATS_INTERVAL 200 /* msecs */
72 #define MLX5E_SQ_BF_BUDGET 16
74 #define MLX5E_NUM_MAIN_GROUPS 9
76 #ifdef CONFIG_MLX5_CORE_EN_DCB
77 #define MLX5E_MAX_BW_ALLOC 100 /* Max percentage of BW allocation */
78 #define MLX5E_MIN_BW_ALLOC 1 /* Min percentage of BW allocation */
81 static const char vport_strings[][ETH_GSTRING_LEN] = {
82 /* vport statistics */
95 "rx_multicast_packets",
97 "tx_multicast_packets",
99 "rx_broadcast_packets",
100 "rx_broadcast_bytes",
101 "tx_broadcast_packets",
102 "tx_broadcast_bytes",
122 struct mlx5e_vport_stats {
128 u64 rx_error_packets;
130 u64 tx_error_packets;
132 u64 rx_unicast_packets;
133 u64 rx_unicast_bytes;
134 u64 tx_unicast_packets;
135 u64 tx_unicast_bytes;
136 u64 rx_multicast_packets;
137 u64 rx_multicast_bytes;
138 u64 tx_multicast_packets;
139 u64 tx_multicast_bytes;
140 u64 rx_broadcast_packets;
141 u64 rx_broadcast_bytes;
142 u64 tx_broadcast_packets;
143 u64 tx_broadcast_bytes;
148 u64 tso_inner_packets;
157 u64 tx_queue_stopped;
159 u64 tx_queue_dropped;
162 #define NUM_VPORT_COUNTERS 35
165 static const char pport_strings[][ETH_GSTRING_LEN] = {
166 /* IEEE802.3 counters */
177 "in_range_len_errors",
187 /* RFC2863 counters */
199 "out_multicast_pkts",
200 "out_broadcast_pkts",
202 /* RFC2819 counters */
223 "p8192to10239octets",
226 #define NUM_IEEE_802_3_COUNTERS 19
227 #define NUM_RFC_2863_COUNTERS 13
228 #define NUM_RFC_2819_COUNTERS 21
229 #define NUM_PPORT_COUNTERS (NUM_IEEE_802_3_COUNTERS + \
230 NUM_RFC_2863_COUNTERS + \
231 NUM_RFC_2819_COUNTERS)
233 struct mlx5e_pport_stats {
234 __be64 IEEE_802_3_counters[NUM_IEEE_802_3_COUNTERS];
235 __be64 RFC_2863_counters[NUM_RFC_2863_COUNTERS];
236 __be64 RFC_2819_counters[NUM_RFC_2819_COUNTERS];
239 static const char qcounter_stats_strings[][ETH_GSTRING_LEN] = {
243 struct mlx5e_qcounter_stats {
244 u32 rx_out_of_buffer;
245 #define NUM_Q_COUNTERS 1
248 static const char rq_stats_strings[][ETH_GSTRING_LEN] = {
258 struct mlx5e_rq_stats {
266 #define NUM_RQ_STATS 7
269 static const char sq_stats_strings[][ETH_GSTRING_LEN] = {
276 "csum_offload_inner",
284 struct mlx5e_sq_stats {
285 /* commonly accessed in data path */
290 u64 tso_inner_packets;
292 u64 csum_offload_inner;
294 /* less likely accessed in data path */
295 u64 csum_offload_none;
299 #define NUM_SQ_STATS 12
303 struct mlx5e_vport_stats vport;
304 struct mlx5e_pport_stats pport;
305 struct mlx5e_qcounter_stats qcnt;
308 struct mlx5e_params {
313 u16 rx_cq_moderation_usec;
314 u16 rx_cq_moderation_pkts;
315 u16 tx_cq_moderation_usec;
316 u16 tx_cq_moderation_pkts;
322 u8 toeplitz_hash_key[40];
323 u32 indirection_rqt[MLX5E_INDIR_RQT_SIZE];
324 #ifdef CONFIG_MLX5_CORE_EN_DCB
329 struct mlx5e_tstamp {
331 struct cyclecounter cycles;
332 struct timecounter clock;
333 struct hwtstamp_config hwtstamp_config;
335 unsigned long overflow_period;
336 struct delayed_work overflow_work;
337 struct mlx5_core_dev *mdev;
338 struct ptp_clock *ptp;
339 struct ptp_clock_info ptp_info;
343 MLX5E_RQ_STATE_POST_WQES_ENABLE,
347 /* data path - accessed per cqe */
350 /* data path - accessed per napi poll */
351 struct napi_struct *napi;
352 struct mlx5_core_cq mcq;
353 struct mlx5e_channel *channel;
354 struct mlx5e_priv *priv;
357 struct mlx5_wq_ctrl wq_ctrl;
358 } ____cacheline_aligned_in_smp;
362 struct mlx5_wq_ll wq;
364 struct sk_buff **skb;
367 struct net_device *netdev;
368 struct mlx5e_tstamp *tstamp;
369 struct mlx5e_rq_stats stats;
376 struct mlx5_wq_ctrl wq_ctrl;
378 struct mlx5e_channel *channel;
379 struct mlx5e_priv *priv;
380 } ____cacheline_aligned_in_smp;
382 struct mlx5e_tx_wqe_info {
388 enum mlx5e_dma_map_type {
389 MLX5E_DMA_MAP_SINGLE,
393 struct mlx5e_sq_dma {
396 enum mlx5e_dma_map_type type;
400 MLX5E_SQ_STATE_WAKE_TXQ_ENABLE,
401 MLX5E_SQ_STATE_BF_ENABLE,
407 /* dirtied @completion */
412 u16 pc ____cacheline_aligned_in_smp;
417 struct mlx5e_sq_stats stats;
421 /* pointers to per packet info: write@xmit, read@completion */
422 struct sk_buff **skb;
423 struct mlx5e_sq_dma *dma_fifo;
424 struct mlx5e_tx_wqe_info *wqe_info;
427 struct mlx5_wq_cyc wq;
429 void __iomem *uar_map;
430 struct netdev_queue *txq;
436 struct mlx5e_tstamp *tstamp;
441 struct mlx5_wq_ctrl wq_ctrl;
443 struct mlx5e_channel *channel;
445 } ____cacheline_aligned_in_smp;
447 static inline bool mlx5e_sq_has_room_for(struct mlx5e_sq *sq, u16 n)
449 return (((sq->wq.sz_m1 & (sq->cc - sq->pc)) >= n) ||
454 MLX5E_CHANNEL_NAPI_SCHED = 1,
457 struct mlx5e_channel {
460 struct mlx5e_sq sq[MLX5E_MAX_NUM_TC];
461 struct napi_struct napi;
463 struct net_device *netdev;
469 struct mlx5e_priv *priv;
474 enum mlx5e_traffic_types {
479 MLX5E_TT_IPV4_IPSEC_AH,
480 MLX5E_TT_IPV6_IPSEC_AH,
481 MLX5E_TT_IPV4_IPSEC_ESP,
482 MLX5E_TT_IPV6_IPSEC_ESP,
489 #define IS_HASHING_TT(tt) (tt != MLX5E_TT_ANY)
492 MLX5E_INDIRECTION_RQT,
497 struct mlx5e_eth_addr_info {
498 u8 addr[ETH_ALEN + 2];
500 struct mlx5_flow_rule *ft_rule[MLX5E_NUM_TT];
503 #define MLX5E_ETH_ADDR_HASH_SIZE (1 << BITS_PER_BYTE)
505 struct mlx5e_eth_addr_db {
506 struct hlist_head netdev_uc[MLX5E_ETH_ADDR_HASH_SIZE];
507 struct hlist_head netdev_mc[MLX5E_ETH_ADDR_HASH_SIZE];
508 struct mlx5e_eth_addr_info broadcast;
509 struct mlx5e_eth_addr_info allmulti;
510 struct mlx5e_eth_addr_info promisc;
511 bool broadcast_enabled;
512 bool allmulti_enabled;
513 bool promisc_enabled;
517 MLX5E_STATE_ASYNC_EVENTS_ENABLE,
519 MLX5E_STATE_DESTROYING,
522 struct mlx5e_vlan_db {
523 unsigned long active_vlans[BITS_TO_LONGS(VLAN_N_VID)];
524 struct mlx5_flow_rule *active_vlans_rule[VLAN_N_VID];
525 struct mlx5_flow_rule *untagged_rule;
526 struct mlx5_flow_rule *any_vlan_rule;
527 bool filter_disabled;
530 struct mlx5e_vxlan_db {
531 spinlock_t lock; /* protect vxlan table */
532 struct radix_tree_root tree;
535 struct mlx5e_flow_table {
537 struct mlx5_flow_table *t;
538 struct mlx5_flow_group **g;
541 struct mlx5e_tc_flow_table {
542 struct mlx5_flow_table *t;
544 struct rhashtable_params ht_params;
545 struct rhashtable ht;
548 struct mlx5e_flow_tables {
549 struct mlx5_flow_namespace *ns;
550 struct mlx5e_tc_flow_table tc;
551 struct mlx5e_flow_table vlan;
552 struct mlx5e_flow_table main;
556 /* priv data path fields - start */
557 struct mlx5e_sq **txq_to_sq_map;
558 int channeltc_to_txq_map[MLX5E_MAX_NUM_CHANNELS][MLX5E_MAX_NUM_TC];
559 /* priv data path fields - end */
562 struct mutex state_lock; /* Protects Interface state */
563 struct mlx5_uar cq_uar;
566 struct mlx5_core_mkey mkey;
567 struct mlx5e_rq drop_rq;
569 struct mlx5e_channel **channel;
570 u32 tisn[MLX5E_MAX_NUM_TC];
571 u32 rqtn[MLX5E_NUM_RQT];
572 u32 tirn[MLX5E_NUM_TT];
574 struct mlx5e_flow_tables fts;
575 struct mlx5e_eth_addr_db eth_addr;
576 struct mlx5e_vlan_db vlan;
577 struct mlx5e_vxlan_db vxlan;
579 struct mlx5e_params params;
580 struct work_struct update_carrier_work;
581 struct work_struct set_rx_mode_work;
582 struct delayed_work update_stats_work;
584 struct mlx5_core_dev *mdev;
585 struct net_device *netdev;
586 struct mlx5e_stats stats;
587 struct mlx5e_tstamp tstamp;
591 #define MLX5E_NET_IP_ALIGN 2
593 struct mlx5e_tx_wqe {
594 struct mlx5_wqe_ctrl_seg ctrl;
595 struct mlx5_wqe_eth_seg eth;
598 struct mlx5e_rx_wqe {
599 struct mlx5_wqe_srq_next_seg next;
600 struct mlx5_wqe_data_seg data;
603 enum mlx5e_link_mode {
604 MLX5E_1000BASE_CX_SGMII = 0,
605 MLX5E_1000BASE_KX = 1,
606 MLX5E_10GBASE_CX4 = 2,
607 MLX5E_10GBASE_KX4 = 3,
608 MLX5E_10GBASE_KR = 4,
609 MLX5E_20GBASE_KR2 = 5,
610 MLX5E_40GBASE_CR4 = 6,
611 MLX5E_40GBASE_KR4 = 7,
612 MLX5E_56GBASE_R4 = 8,
613 MLX5E_10GBASE_CR = 12,
614 MLX5E_10GBASE_SR = 13,
615 MLX5E_10GBASE_ER = 14,
616 MLX5E_40GBASE_SR4 = 15,
617 MLX5E_40GBASE_LR4 = 16,
618 MLX5E_100GBASE_CR4 = 20,
619 MLX5E_100GBASE_SR4 = 21,
620 MLX5E_100GBASE_KR4 = 22,
621 MLX5E_100GBASE_LR4 = 23,
622 MLX5E_100BASE_TX = 24,
623 MLX5E_100BASE_T = 25,
624 MLX5E_10GBASE_T = 26,
625 MLX5E_25GBASE_CR = 27,
626 MLX5E_25GBASE_KR = 28,
627 MLX5E_25GBASE_SR = 29,
628 MLX5E_50GBASE_CR2 = 30,
629 MLX5E_50GBASE_KR2 = 31,
630 MLX5E_LINK_MODES_NUMBER,
633 #define MLX5E_PROT_MASK(link_mode) (1 << link_mode)
635 void mlx5e_send_nop(struct mlx5e_sq *sq, bool notify_hw);
636 u16 mlx5e_select_queue(struct net_device *dev, struct sk_buff *skb,
637 void *accel_priv, select_queue_fallback_t fallback);
638 netdev_tx_t mlx5e_xmit(struct sk_buff *skb, struct net_device *dev);
640 void mlx5e_completion_event(struct mlx5_core_cq *mcq);
641 void mlx5e_cq_error_event(struct mlx5_core_cq *mcq, enum mlx5_event event);
642 int mlx5e_napi_poll(struct napi_struct *napi, int budget);
643 bool mlx5e_poll_tx_cq(struct mlx5e_cq *cq, int napi_budget);
644 int mlx5e_poll_rx_cq(struct mlx5e_cq *cq, int budget);
645 bool mlx5e_post_rx_wqes(struct mlx5e_rq *rq);
646 struct mlx5_cqe64 *mlx5e_get_cqe(struct mlx5e_cq *cq);
648 void mlx5e_update_stats(struct mlx5e_priv *priv);
650 int mlx5e_create_flow_tables(struct mlx5e_priv *priv);
651 void mlx5e_destroy_flow_tables(struct mlx5e_priv *priv);
652 void mlx5e_init_eth_addr(struct mlx5e_priv *priv);
653 void mlx5e_set_rx_mode_work(struct work_struct *work);
655 void mlx5e_fill_hwstamp(struct mlx5e_tstamp *clock, u64 timestamp,
656 struct skb_shared_hwtstamps *hwts);
657 void mlx5e_timestamp_init(struct mlx5e_priv *priv);
658 void mlx5e_timestamp_cleanup(struct mlx5e_priv *priv);
659 int mlx5e_hwstamp_set(struct net_device *dev, struct ifreq *ifr);
660 int mlx5e_hwstamp_get(struct net_device *dev, struct ifreq *ifr);
662 int mlx5e_vlan_rx_add_vid(struct net_device *dev, __always_unused __be16 proto,
664 int mlx5e_vlan_rx_kill_vid(struct net_device *dev, __always_unused __be16 proto,
666 void mlx5e_enable_vlan_filter(struct mlx5e_priv *priv);
667 void mlx5e_disable_vlan_filter(struct mlx5e_priv *priv);
669 int mlx5e_redirect_rqt(struct mlx5e_priv *priv, enum mlx5e_rqt_ix rqt_ix);
670 void mlx5e_build_tir_ctx_hash(void *tirc, struct mlx5e_priv *priv);
672 int mlx5e_open_locked(struct net_device *netdev);
673 int mlx5e_close_locked(struct net_device *netdev);
674 void mlx5e_build_default_indir_rqt(struct mlx5_core_dev *mdev,
675 u32 *indirection_rqt, int len,
678 static inline void mlx5e_tx_notify_hw(struct mlx5e_sq *sq,
679 struct mlx5e_tx_wqe *wqe, int bf_sz)
681 u16 ofst = MLX5_BF_OFFSET + sq->bf_offset;
683 /* ensure wqe is visible to device before updating doorbell record */
686 *sq->wq.db = cpu_to_be32(sq->pc);
688 /* ensure doorbell record is visible to device before ringing the
693 __iowrite64_copy(sq->uar_map + ofst, &wqe->ctrl, bf_sz);
695 mlx5_write64((__be32 *)&wqe->ctrl, sq->uar_map + ofst, NULL);
696 /* flush the write-combining mapped buffer */
699 sq->bf_offset ^= sq->bf_buf_size;
702 static inline void mlx5e_cq_arm(struct mlx5e_cq *cq)
704 struct mlx5_core_cq *mcq;
707 mlx5_cq_arm(mcq, MLX5_CQ_DB_REQ_NOT, mcq->uar->map, NULL, cq->wq.cc);
710 static inline int mlx5e_get_max_num_channels(struct mlx5_core_dev *mdev)
712 return min_t(int, mdev->priv.eq_table.num_comp_vectors,
713 MLX5E_MAX_NUM_CHANNELS);
716 extern const struct ethtool_ops mlx5e_ethtool_ops;
717 #ifdef CONFIG_MLX5_CORE_EN_DCB
718 extern const struct dcbnl_rtnl_ops mlx5e_dcbnl_ops;
719 int mlx5e_dcbnl_ieee_setets_core(struct mlx5e_priv *priv, struct ieee_ets *ets);
722 u16 mlx5e_get_max_inline_cap(struct mlx5_core_dev *mdev);
724 #endif /* __MLX5_EN_H__ */