2 * Copyright (c) 2015, 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
33 #include <linux/if_vlan.h>
34 #include <linux/etherdevice.h>
35 #include <linux/timecounter.h>
36 #include <linux/net_tstamp.h>
37 #include <linux/ptp_clock_kernel.h>
38 #include <linux/mlx5/driver.h>
39 #include <linux/mlx5/qp.h>
40 #include <linux/mlx5/cq.h>
41 #include <linux/mlx5/port.h>
42 #include <linux/mlx5/vport.h>
43 #include <linux/mlx5/transobj.h>
45 #include "mlx5_core.h"
47 #define MLX5E_MAX_NUM_TC 8
49 #define MLX5E_PARAMS_MINIMUM_LOG_SQ_SIZE 0x6
50 #define MLX5E_PARAMS_DEFAULT_LOG_SQ_SIZE 0xa
51 #define MLX5E_PARAMS_MAXIMUM_LOG_SQ_SIZE 0xd
53 #define MLX5E_PARAMS_MINIMUM_LOG_RQ_SIZE 0x1
54 #define MLX5E_PARAMS_DEFAULT_LOG_RQ_SIZE 0xa
55 #define MLX5E_PARAMS_MAXIMUM_LOG_RQ_SIZE 0xd
57 #define MLX5E_PARAMS_DEFAULT_LRO_WQE_SZ (64 * 1024)
58 #define MLX5E_PARAMS_DEFAULT_RX_CQ_MODERATION_USEC 0x10
59 #define MLX5E_PARAMS_DEFAULT_RX_CQ_MODERATION_PKTS 0x20
60 #define MLX5E_PARAMS_DEFAULT_TX_CQ_MODERATION_USEC 0x10
61 #define MLX5E_PARAMS_DEFAULT_TX_CQ_MODERATION_PKTS 0x20
62 #define MLX5E_PARAMS_DEFAULT_MIN_RX_WQES 0x80
64 #define MLX5E_LOG_INDIR_RQT_SIZE 0x7
65 #define MLX5E_INDIR_RQT_SIZE BIT(MLX5E_LOG_INDIR_RQT_SIZE)
66 #define MLX5E_MAX_NUM_CHANNELS (MLX5E_INDIR_RQT_SIZE >> 1)
67 #define MLX5E_TX_CQ_POLL_BUDGET 128
68 #define MLX5E_UPDATE_STATS_INTERVAL 200 /* msecs */
69 #define MLX5E_SQ_BF_BUDGET 16
71 #define MLX5E_NUM_MAIN_GROUPS 9
73 #ifdef CONFIG_MLX5_CORE_EN_DCB
74 #define MLX5E_MAX_BW_ALLOC 100 /* Max percentage of BW allocation */
75 #define MLX5E_MIN_BW_ALLOC 1 /* Min percentage of BW allocation */
78 static const char vport_strings[][ETH_GSTRING_LEN] = {
79 /* vport statistics */
92 "rx_multicast_packets",
94 "tx_multicast_packets",
96 "rx_broadcast_packets",
98 "tx_broadcast_packets",
116 struct mlx5e_vport_stats {
122 u64 rx_error_packets;
124 u64 tx_error_packets;
126 u64 rx_unicast_packets;
127 u64 rx_unicast_bytes;
128 u64 tx_unicast_packets;
129 u64 tx_unicast_bytes;
130 u64 rx_multicast_packets;
131 u64 rx_multicast_bytes;
132 u64 tx_multicast_packets;
133 u64 tx_multicast_bytes;
134 u64 rx_broadcast_packets;
135 u64 rx_broadcast_bytes;
136 u64 tx_broadcast_packets;
137 u64 tx_broadcast_bytes;
148 u64 tx_queue_stopped;
150 u64 tx_queue_dropped;
153 #define NUM_VPORT_COUNTERS 32
156 static const char pport_strings[][ETH_GSTRING_LEN] = {
157 /* IEEE802.3 counters */
168 "in_range_len_errors",
178 /* RFC2863 counters */
190 "out_multicast_pkts",
191 "out_broadcast_pkts",
193 /* RFC2819 counters */
214 "p8192to10239octets",
217 #define NUM_IEEE_802_3_COUNTERS 19
218 #define NUM_RFC_2863_COUNTERS 13
219 #define NUM_RFC_2819_COUNTERS 21
220 #define NUM_PPORT_COUNTERS (NUM_IEEE_802_3_COUNTERS + \
221 NUM_RFC_2863_COUNTERS + \
222 NUM_RFC_2819_COUNTERS)
224 struct mlx5e_pport_stats {
225 __be64 IEEE_802_3_counters[NUM_IEEE_802_3_COUNTERS];
226 __be64 RFC_2863_counters[NUM_RFC_2863_COUNTERS];
227 __be64 RFC_2819_counters[NUM_RFC_2819_COUNTERS];
230 static const char rq_stats_strings[][ETH_GSTRING_LEN] = {
239 struct mlx5e_rq_stats {
246 #define NUM_RQ_STATS 6
249 static const char sq_stats_strings[][ETH_GSTRING_LEN] = {
260 struct mlx5e_sq_stats {
264 u64 csum_offload_none;
269 #define NUM_SQ_STATS 8
273 struct mlx5e_vport_stats vport;
274 struct mlx5e_pport_stats pport;
277 struct mlx5e_params {
282 u16 rx_cq_moderation_usec;
283 u16 rx_cq_moderation_pkts;
284 u16 tx_cq_moderation_usec;
285 u16 tx_cq_moderation_pkts;
291 u8 toeplitz_hash_key[40];
292 u32 indirection_rqt[MLX5E_INDIR_RQT_SIZE];
293 #ifdef CONFIG_MLX5_CORE_EN_DCB
298 struct mlx5e_tstamp {
300 struct cyclecounter cycles;
301 struct timecounter clock;
302 struct hwtstamp_config hwtstamp_config;
304 unsigned long overflow_period;
305 struct delayed_work overflow_work;
306 struct mlx5_core_dev *mdev;
307 struct ptp_clock *ptp;
308 struct ptp_clock_info ptp_info;
312 MLX5E_RQ_STATE_POST_WQES_ENABLE,
316 MLX5E_CQ_HAS_CQES = 1,
320 /* data path - accessed per cqe */
324 /* data path - accessed per napi poll */
325 struct napi_struct *napi;
326 struct mlx5_core_cq mcq;
327 struct mlx5e_channel *channel;
328 struct mlx5e_priv *priv;
331 struct mlx5_wq_ctrl wq_ctrl;
332 } ____cacheline_aligned_in_smp;
336 struct mlx5_wq_ll wq;
338 struct sk_buff **skb;
341 struct net_device *netdev;
342 struct mlx5e_tstamp *tstamp;
343 struct mlx5e_rq_stats stats;
350 struct mlx5_wq_ctrl wq_ctrl;
352 struct mlx5e_channel *channel;
353 struct mlx5e_priv *priv;
354 } ____cacheline_aligned_in_smp;
356 struct mlx5e_tx_wqe_info {
362 enum mlx5e_dma_map_type {
363 MLX5E_DMA_MAP_SINGLE,
367 struct mlx5e_sq_dma {
370 enum mlx5e_dma_map_type type;
374 MLX5E_SQ_STATE_WAKE_TXQ_ENABLE,
380 /* dirtied @completion */
385 u16 pc ____cacheline_aligned_in_smp;
390 struct mlx5e_sq_stats stats;
394 /* pointers to per packet info: write@xmit, read@completion */
395 struct sk_buff **skb;
396 struct mlx5e_sq_dma *dma_fifo;
397 struct mlx5e_tx_wqe_info *wqe_info;
400 struct mlx5_wq_cyc wq;
402 void __iomem *uar_map;
403 void __iomem *uar_bf_map;
404 struct netdev_queue *txq;
410 struct mlx5e_tstamp *tstamp;
415 struct mlx5_wq_ctrl wq_ctrl;
417 struct mlx5e_channel *channel;
419 } ____cacheline_aligned_in_smp;
421 static inline bool mlx5e_sq_has_room_for(struct mlx5e_sq *sq, u16 n)
423 return (((sq->wq.sz_m1 & (sq->cc - sq->pc)) >= n) ||
428 MLX5E_CHANNEL_NAPI_SCHED = 1,
431 struct mlx5e_channel {
434 struct mlx5e_sq sq[MLX5E_MAX_NUM_TC];
435 struct napi_struct napi;
437 struct net_device *netdev;
443 struct mlx5e_priv *priv;
448 enum mlx5e_traffic_types {
453 MLX5E_TT_IPV4_IPSEC_AH,
454 MLX5E_TT_IPV6_IPSEC_AH,
455 MLX5E_TT_IPV4_IPSEC_ESP,
456 MLX5E_TT_IPV6_IPSEC_ESP,
464 MLX5E_INDIRECTION_RQT,
469 struct mlx5e_eth_addr_info {
470 u8 addr[ETH_ALEN + 2];
472 struct mlx5_flow_rule *ft_rule[MLX5E_NUM_TT];
475 #define MLX5E_ETH_ADDR_HASH_SIZE (1 << BITS_PER_BYTE)
477 struct mlx5e_eth_addr_db {
478 struct hlist_head netdev_uc[MLX5E_ETH_ADDR_HASH_SIZE];
479 struct hlist_head netdev_mc[MLX5E_ETH_ADDR_HASH_SIZE];
480 struct mlx5e_eth_addr_info broadcast;
481 struct mlx5e_eth_addr_info allmulti;
482 struct mlx5e_eth_addr_info promisc;
483 bool broadcast_enabled;
484 bool allmulti_enabled;
485 bool promisc_enabled;
489 MLX5E_STATE_ASYNC_EVENTS_ENABLE,
491 MLX5E_STATE_DESTROYING,
494 struct mlx5e_vlan_db {
495 unsigned long active_vlans[BITS_TO_LONGS(VLAN_N_VID)];
496 struct mlx5_flow_rule *active_vlans_rule[VLAN_N_VID];
497 struct mlx5_flow_rule *untagged_rule;
498 struct mlx5_flow_rule *any_vlan_rule;
499 bool filter_disabled;
502 struct mlx5e_flow_table {
504 struct mlx5_flow_table *t;
505 struct mlx5_flow_group **g;
508 struct mlx5e_flow_tables {
509 struct mlx5_flow_namespace *ns;
510 struct mlx5e_flow_table vlan;
511 struct mlx5e_flow_table main;
515 /* priv data path fields - start */
516 struct mlx5e_sq **txq_to_sq_map;
517 int channeltc_to_txq_map[MLX5E_MAX_NUM_CHANNELS][MLX5E_MAX_NUM_TC];
518 /* priv data path fields - end */
521 struct mutex state_lock; /* Protects Interface state */
522 struct mlx5_uar cq_uar;
525 struct mlx5_core_mr mr;
526 struct mlx5e_rq drop_rq;
528 struct mlx5e_channel **channel;
529 u32 tisn[MLX5E_MAX_NUM_TC];
530 u32 rqtn[MLX5E_NUM_RQT];
531 u32 tirn[MLX5E_NUM_TT];
533 struct mlx5e_flow_tables fts;
534 struct mlx5e_eth_addr_db eth_addr;
535 struct mlx5e_vlan_db vlan;
537 struct mlx5e_params params;
538 spinlock_t async_events_spinlock; /* sync hw events */
539 struct work_struct update_carrier_work;
540 struct work_struct set_rx_mode_work;
541 struct delayed_work update_stats_work;
543 struct mlx5_core_dev *mdev;
544 struct net_device *netdev;
545 struct mlx5e_stats stats;
546 struct mlx5e_tstamp tstamp;
549 #define MLX5E_NET_IP_ALIGN 2
551 struct mlx5e_tx_wqe {
552 struct mlx5_wqe_ctrl_seg ctrl;
553 struct mlx5_wqe_eth_seg eth;
556 struct mlx5e_rx_wqe {
557 struct mlx5_wqe_srq_next_seg next;
558 struct mlx5_wqe_data_seg data;
561 enum mlx5e_link_mode {
562 MLX5E_1000BASE_CX_SGMII = 0,
563 MLX5E_1000BASE_KX = 1,
564 MLX5E_10GBASE_CX4 = 2,
565 MLX5E_10GBASE_KX4 = 3,
566 MLX5E_10GBASE_KR = 4,
567 MLX5E_20GBASE_KR2 = 5,
568 MLX5E_40GBASE_CR4 = 6,
569 MLX5E_40GBASE_KR4 = 7,
570 MLX5E_56GBASE_R4 = 8,
571 MLX5E_10GBASE_CR = 12,
572 MLX5E_10GBASE_SR = 13,
573 MLX5E_10GBASE_ER = 14,
574 MLX5E_40GBASE_SR4 = 15,
575 MLX5E_40GBASE_LR4 = 16,
576 MLX5E_100GBASE_CR4 = 20,
577 MLX5E_100GBASE_SR4 = 21,
578 MLX5E_100GBASE_KR4 = 22,
579 MLX5E_100GBASE_LR4 = 23,
580 MLX5E_100BASE_TX = 24,
581 MLX5E_100BASE_T = 25,
582 MLX5E_10GBASE_T = 26,
583 MLX5E_25GBASE_CR = 27,
584 MLX5E_25GBASE_KR = 28,
585 MLX5E_25GBASE_SR = 29,
586 MLX5E_50GBASE_CR2 = 30,
587 MLX5E_50GBASE_KR2 = 31,
588 MLX5E_LINK_MODES_NUMBER,
591 #define MLX5E_PROT_MASK(link_mode) (1 << link_mode)
593 void mlx5e_send_nop(struct mlx5e_sq *sq, bool notify_hw);
594 u16 mlx5e_select_queue(struct net_device *dev, struct sk_buff *skb,
595 void *accel_priv, select_queue_fallback_t fallback);
596 netdev_tx_t mlx5e_xmit(struct sk_buff *skb, struct net_device *dev);
598 void mlx5e_completion_event(struct mlx5_core_cq *mcq);
599 void mlx5e_cq_error_event(struct mlx5_core_cq *mcq, enum mlx5_event event);
600 int mlx5e_napi_poll(struct napi_struct *napi, int budget);
601 bool mlx5e_poll_tx_cq(struct mlx5e_cq *cq);
602 int mlx5e_poll_rx_cq(struct mlx5e_cq *cq, int budget);
603 bool mlx5e_post_rx_wqes(struct mlx5e_rq *rq);
604 struct mlx5_cqe64 *mlx5e_get_cqe(struct mlx5e_cq *cq);
606 void mlx5e_update_stats(struct mlx5e_priv *priv);
608 int mlx5e_create_flow_tables(struct mlx5e_priv *priv);
609 void mlx5e_destroy_flow_tables(struct mlx5e_priv *priv);
610 void mlx5e_init_eth_addr(struct mlx5e_priv *priv);
611 void mlx5e_set_rx_mode_work(struct work_struct *work);
613 void mlx5e_fill_hwstamp(struct mlx5e_tstamp *clock, u64 timestamp,
614 struct skb_shared_hwtstamps *hwts);
615 void mlx5e_timestamp_init(struct mlx5e_priv *priv);
616 void mlx5e_timestamp_cleanup(struct mlx5e_priv *priv);
617 int mlx5e_hwstamp_set(struct net_device *dev, struct ifreq *ifr);
618 int mlx5e_hwstamp_get(struct net_device *dev, struct ifreq *ifr);
620 int mlx5e_vlan_rx_add_vid(struct net_device *dev, __always_unused __be16 proto,
622 int mlx5e_vlan_rx_kill_vid(struct net_device *dev, __always_unused __be16 proto,
624 void mlx5e_enable_vlan_filter(struct mlx5e_priv *priv);
625 void mlx5e_disable_vlan_filter(struct mlx5e_priv *priv);
627 int mlx5e_redirect_rqt(struct mlx5e_priv *priv, enum mlx5e_rqt_ix rqt_ix);
629 int mlx5e_open_locked(struct net_device *netdev);
630 int mlx5e_close_locked(struct net_device *netdev);
632 static inline void mlx5e_tx_notify_hw(struct mlx5e_sq *sq,
633 struct mlx5e_tx_wqe *wqe, int bf_sz)
635 u16 ofst = MLX5_BF_OFFSET + sq->bf_offset;
637 /* ensure wqe is visible to device before updating doorbell record */
640 *sq->wq.db = cpu_to_be32(sq->pc);
642 /* ensure doorbell record is visible to device before ringing the
648 __iowrite64_copy(sq->uar_bf_map + ofst, &wqe->ctrl, bf_sz);
650 /* flush the write-combining mapped buffer */
654 mlx5_write64((__be32 *)&wqe->ctrl, sq->uar_map + ofst, NULL);
657 sq->bf_offset ^= sq->bf_buf_size;
660 static inline void mlx5e_cq_arm(struct mlx5e_cq *cq)
662 struct mlx5_core_cq *mcq;
665 mlx5_cq_arm(mcq, MLX5_CQ_DB_REQ_NOT, mcq->uar->map, NULL, cq->wq.cc);
668 static inline int mlx5e_get_max_num_channels(struct mlx5_core_dev *mdev)
670 return min_t(int, mdev->priv.eq_table.num_comp_vectors,
671 MLX5E_MAX_NUM_CHANNELS);
674 extern const struct ethtool_ops mlx5e_ethtool_ops;
675 #ifdef CONFIG_MLX5_CORE_EN_DCB
676 extern const struct dcbnl_rtnl_ops mlx5e_dcbnl_ops;
677 int mlx5e_dcbnl_ieee_setets_core(struct mlx5e_priv *priv, struct ieee_ets *ets);
680 u16 mlx5e_get_max_inline_cap(struct mlx5_core_dev *mdev);