2 * Copyright (c) 2007 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
34 #include <linux/bpf.h>
35 #include <linux/etherdevice.h>
36 #include <linux/tcp.h>
37 #include <linux/if_vlan.h>
38 #include <linux/delay.h>
39 #include <linux/slab.h>
40 #include <linux/hash.h>
42 #include <net/busy_poll.h>
43 #include <net/vxlan.h>
44 #include <net/devlink.h>
46 #include <linux/mlx4/driver.h>
47 #include <linux/mlx4/device.h>
48 #include <linux/mlx4/cmd.h>
49 #include <linux/mlx4/cq.h>
54 #define MLX4_EN_MAX_XDP_MTU ((int)(PAGE_SIZE - ETH_HLEN - (2 * VLAN_HLEN) - \
57 int mlx4_en_setup_tc(struct net_device *dev, u8 up)
59 struct mlx4_en_priv *priv = netdev_priv(dev);
61 unsigned int offset = 0;
63 if (up && up != MLX4_EN_NUM_UP_HIGH)
66 netdev_set_num_tc(dev, up);
67 netif_set_real_num_tx_queues(dev, priv->tx_ring_num[TX]);
68 /* Partition Tx queues evenly amongst UP's */
69 for (i = 0; i < up; i++) {
70 netdev_set_tc_queue(dev, i, priv->num_tx_rings_p_up, offset);
71 offset += priv->num_tx_rings_p_up;
74 #ifdef CONFIG_MLX4_EN_DCB
75 if (!mlx4_is_slave(priv->mdev->dev)) {
78 priv->flags |= MLX4_EN_FLAG_DCB_ENABLED;
80 priv->flags &= ~MLX4_EN_FLAG_DCB_ENABLED;
81 priv->cee_config.pfc_state = false;
84 #endif /* CONFIG_MLX4_EN_DCB */
89 int mlx4_en_alloc_tx_queue_per_tc(struct net_device *dev, u8 tc)
91 struct mlx4_en_priv *priv = netdev_priv(dev);
92 struct mlx4_en_dev *mdev = priv->mdev;
93 struct mlx4_en_port_profile new_prof;
94 struct mlx4_en_priv *tmp;
98 tmp = kzalloc(sizeof(*tmp), GFP_KERNEL);
102 mutex_lock(&mdev->state_lock);
103 memcpy(&new_prof, priv->prof, sizeof(struct mlx4_en_port_profile));
104 new_prof.num_up = (tc == 0) ? MLX4_EN_NUM_UP_LOW :
106 new_prof.tx_ring_num[TX] = new_prof.num_tx_rings_p_up *
108 err = mlx4_en_try_alloc_resources(priv, tmp, &new_prof, true);
114 mlx4_en_stop_port(dev, 1);
117 mlx4_en_safe_replace_resources(priv, tmp);
119 err = mlx4_en_start_port(dev);
121 en_err(priv, "Failed starting port for setup TC\n");
126 err = mlx4_en_setup_tc(dev, tc);
128 mutex_unlock(&mdev->state_lock);
133 static int __mlx4_en_setup_tc(struct net_device *dev, enum tc_setup_type type,
136 struct tc_mqprio_qopt *mqprio = type_data;
138 if (type != TC_SETUP_QDISC_MQPRIO)
141 if (mqprio->num_tc && mqprio->num_tc != MLX4_EN_NUM_UP_HIGH)
144 mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS;
146 return mlx4_en_alloc_tx_queue_per_tc(dev, mqprio->num_tc);
149 #ifdef CONFIG_RFS_ACCEL
151 struct mlx4_en_filter {
152 struct list_head next;
153 struct work_struct work;
162 struct mlx4_en_priv *priv;
163 u32 flow_id; /* RFS infrastructure id */
164 int id; /* mlx4_en driver id */
165 u64 reg_id; /* Flow steering API id */
166 u8 activated; /* Used to prevent expiry before filter
169 struct hlist_node filter_chain;
172 static void mlx4_en_filter_rfs_expire(struct mlx4_en_priv *priv);
174 static enum mlx4_net_trans_rule_id mlx4_ip_proto_to_trans_rule_id(u8 ip_proto)
178 return MLX4_NET_TRANS_RULE_ID_UDP;
180 return MLX4_NET_TRANS_RULE_ID_TCP;
182 return MLX4_NET_TRANS_RULE_NUM;
186 /* Must not acquire state_lock, as its corresponding work_sync
189 static void mlx4_en_filter_work(struct work_struct *work)
191 struct mlx4_en_filter *filter = container_of(work,
192 struct mlx4_en_filter,
194 struct mlx4_en_priv *priv = filter->priv;
195 struct mlx4_spec_list spec_tcp_udp = {
196 .id = mlx4_ip_proto_to_trans_rule_id(filter->ip_proto),
199 .dst_port = filter->dst_port,
200 .dst_port_msk = (__force __be16)-1,
201 .src_port = filter->src_port,
202 .src_port_msk = (__force __be16)-1,
206 struct mlx4_spec_list spec_ip = {
207 .id = MLX4_NET_TRANS_RULE_ID_IPV4,
210 .dst_ip = filter->dst_ip,
211 .dst_ip_msk = (__force __be32)-1,
212 .src_ip = filter->src_ip,
213 .src_ip_msk = (__force __be32)-1,
217 struct mlx4_spec_list spec_eth = {
218 .id = MLX4_NET_TRANS_RULE_ID_ETH,
220 struct mlx4_net_trans_rule rule = {
221 .list = LIST_HEAD_INIT(rule.list),
222 .queue_mode = MLX4_NET_TRANS_Q_LIFO,
225 .promisc_mode = MLX4_FS_REGULAR,
227 .priority = MLX4_DOMAIN_RFS,
230 __be64 mac_mask = cpu_to_be64(MLX4_MAC_MASK << 16);
232 if (spec_tcp_udp.id >= MLX4_NET_TRANS_RULE_NUM) {
233 en_warn(priv, "RFS: ignoring unsupported ip protocol (%d)\n",
237 list_add_tail(&spec_eth.list, &rule.list);
238 list_add_tail(&spec_ip.list, &rule.list);
239 list_add_tail(&spec_tcp_udp.list, &rule.list);
241 rule.qpn = priv->rss_map.qps[filter->rxq_index].qpn;
242 memcpy(spec_eth.eth.dst_mac, priv->dev->dev_addr, ETH_ALEN);
243 memcpy(spec_eth.eth.dst_mac_msk, &mac_mask, ETH_ALEN);
245 filter->activated = 0;
247 if (filter->reg_id) {
248 rc = mlx4_flow_detach(priv->mdev->dev, filter->reg_id);
249 if (rc && rc != -ENOENT)
250 en_err(priv, "Error detaching flow. rc = %d\n", rc);
253 rc = mlx4_flow_attach(priv->mdev->dev, &rule, &filter->reg_id);
255 en_err(priv, "Error attaching flow. err = %d\n", rc);
258 mlx4_en_filter_rfs_expire(priv);
260 filter->activated = 1;
263 static inline struct hlist_head *
264 filter_hash_bucket(struct mlx4_en_priv *priv, __be32 src_ip, __be32 dst_ip,
265 __be16 src_port, __be16 dst_port)
270 l = (__force unsigned long)src_port |
271 ((__force unsigned long)dst_port << 2);
272 l ^= (__force unsigned long)(src_ip ^ dst_ip);
274 bucket_idx = hash_long(l, MLX4_EN_FILTER_HASH_SHIFT);
276 return &priv->filter_hash[bucket_idx];
279 static struct mlx4_en_filter *
280 mlx4_en_filter_alloc(struct mlx4_en_priv *priv, int rxq_index, __be32 src_ip,
281 __be32 dst_ip, u8 ip_proto, __be16 src_port,
282 __be16 dst_port, u32 flow_id)
284 struct mlx4_en_filter *filter = NULL;
286 filter = kzalloc(sizeof(struct mlx4_en_filter), GFP_ATOMIC);
291 filter->rxq_index = rxq_index;
292 INIT_WORK(&filter->work, mlx4_en_filter_work);
294 filter->src_ip = src_ip;
295 filter->dst_ip = dst_ip;
296 filter->ip_proto = ip_proto;
297 filter->src_port = src_port;
298 filter->dst_port = dst_port;
300 filter->flow_id = flow_id;
302 filter->id = priv->last_filter_id++ % RPS_NO_FILTER;
304 list_add_tail(&filter->next, &priv->filters);
305 hlist_add_head(&filter->filter_chain,
306 filter_hash_bucket(priv, src_ip, dst_ip, src_port,
312 static void mlx4_en_filter_free(struct mlx4_en_filter *filter)
314 struct mlx4_en_priv *priv = filter->priv;
317 list_del(&filter->next);
319 rc = mlx4_flow_detach(priv->mdev->dev, filter->reg_id);
320 if (rc && rc != -ENOENT)
321 en_err(priv, "Error detaching flow. rc = %d\n", rc);
326 static inline struct mlx4_en_filter *
327 mlx4_en_filter_find(struct mlx4_en_priv *priv, __be32 src_ip, __be32 dst_ip,
328 u8 ip_proto, __be16 src_port, __be16 dst_port)
330 struct mlx4_en_filter *filter;
331 struct mlx4_en_filter *ret = NULL;
333 hlist_for_each_entry(filter,
334 filter_hash_bucket(priv, src_ip, dst_ip,
337 if (filter->src_ip == src_ip &&
338 filter->dst_ip == dst_ip &&
339 filter->ip_proto == ip_proto &&
340 filter->src_port == src_port &&
341 filter->dst_port == dst_port) {
351 mlx4_en_filter_rfs(struct net_device *net_dev, const struct sk_buff *skb,
352 u16 rxq_index, u32 flow_id)
354 struct mlx4_en_priv *priv = netdev_priv(net_dev);
355 struct mlx4_en_filter *filter;
356 const struct iphdr *ip;
363 int nhoff = skb_network_offset(skb);
366 if (skb->protocol != htons(ETH_P_IP))
367 return -EPROTONOSUPPORT;
369 ip = (const struct iphdr *)(skb->data + nhoff);
370 if (ip_is_fragment(ip))
371 return -EPROTONOSUPPORT;
373 if ((ip->protocol != IPPROTO_TCP) && (ip->protocol != IPPROTO_UDP))
374 return -EPROTONOSUPPORT;
375 ports = (const __be16 *)(skb->data + nhoff + 4 * ip->ihl);
377 ip_proto = ip->protocol;
383 spin_lock_bh(&priv->filters_lock);
384 filter = mlx4_en_filter_find(priv, src_ip, dst_ip, ip_proto,
387 if (filter->rxq_index == rxq_index)
390 filter->rxq_index = rxq_index;
392 filter = mlx4_en_filter_alloc(priv, rxq_index,
393 src_ip, dst_ip, ip_proto,
394 src_port, dst_port, flow_id);
401 queue_work(priv->mdev->workqueue, &filter->work);
406 spin_unlock_bh(&priv->filters_lock);
411 void mlx4_en_cleanup_filters(struct mlx4_en_priv *priv)
413 struct mlx4_en_filter *filter, *tmp;
416 spin_lock_bh(&priv->filters_lock);
417 list_for_each_entry_safe(filter, tmp, &priv->filters, next) {
418 list_move(&filter->next, &del_list);
419 hlist_del(&filter->filter_chain);
421 spin_unlock_bh(&priv->filters_lock);
423 list_for_each_entry_safe(filter, tmp, &del_list, next) {
424 cancel_work_sync(&filter->work);
425 mlx4_en_filter_free(filter);
429 static void mlx4_en_filter_rfs_expire(struct mlx4_en_priv *priv)
431 struct mlx4_en_filter *filter = NULL, *tmp, *last_filter = NULL;
435 spin_lock_bh(&priv->filters_lock);
436 list_for_each_entry_safe(filter, tmp, &priv->filters, next) {
437 if (i > MLX4_EN_FILTER_EXPIRY_QUOTA)
440 if (filter->activated &&
441 !work_pending(&filter->work) &&
442 rps_may_expire_flow(priv->dev,
443 filter->rxq_index, filter->flow_id,
445 list_move(&filter->next, &del_list);
446 hlist_del(&filter->filter_chain);
448 last_filter = filter;
453 if (last_filter && (&last_filter->next != priv->filters.next))
454 list_move(&priv->filters, &last_filter->next);
456 spin_unlock_bh(&priv->filters_lock);
458 list_for_each_entry_safe(filter, tmp, &del_list, next)
459 mlx4_en_filter_free(filter);
463 static int mlx4_en_vlan_rx_add_vid(struct net_device *dev,
464 __be16 proto, u16 vid)
466 struct mlx4_en_priv *priv = netdev_priv(dev);
467 struct mlx4_en_dev *mdev = priv->mdev;
471 en_dbg(HW, priv, "adding VLAN:%d\n", vid);
473 set_bit(vid, priv->active_vlans);
475 /* Add VID to port VLAN filter */
476 mutex_lock(&mdev->state_lock);
477 if (mdev->device_up && priv->port_up) {
478 err = mlx4_SET_VLAN_FLTR(mdev->dev, priv);
480 en_err(priv, "Failed configuring VLAN filter\n");
484 err = mlx4_register_vlan(mdev->dev, priv->port, vid, &idx);
486 en_dbg(HW, priv, "Failed adding vlan %d\n", vid);
489 mutex_unlock(&mdev->state_lock);
493 static int mlx4_en_vlan_rx_kill_vid(struct net_device *dev,
494 __be16 proto, u16 vid)
496 struct mlx4_en_priv *priv = netdev_priv(dev);
497 struct mlx4_en_dev *mdev = priv->mdev;
500 en_dbg(HW, priv, "Killing VID:%d\n", vid);
502 clear_bit(vid, priv->active_vlans);
504 /* Remove VID from port VLAN filter */
505 mutex_lock(&mdev->state_lock);
506 mlx4_unregister_vlan(mdev->dev, priv->port, vid);
508 if (mdev->device_up && priv->port_up) {
509 err = mlx4_SET_VLAN_FLTR(mdev->dev, priv);
511 en_err(priv, "Failed configuring VLAN filter\n");
513 mutex_unlock(&mdev->state_lock);
518 static void mlx4_en_u64_to_mac(unsigned char dst_mac[ETH_ALEN + 2], u64 src_mac)
521 for (i = ETH_ALEN - 1; i >= 0; --i) {
522 dst_mac[i] = src_mac & 0xff;
525 memset(&dst_mac[ETH_ALEN], 0, 2);
529 static int mlx4_en_tunnel_steer_add(struct mlx4_en_priv *priv, unsigned char *addr,
530 int qpn, u64 *reg_id)
534 if (priv->mdev->dev->caps.tunnel_offload_mode != MLX4_TUNNEL_OFFLOAD_MODE_VXLAN ||
535 priv->mdev->dev->caps.dmfs_high_steer_mode == MLX4_STEERING_DMFS_A0_STATIC)
536 return 0; /* do nothing */
538 err = mlx4_tunnel_steer_add(priv->mdev->dev, addr, priv->port, qpn,
539 MLX4_DOMAIN_NIC, reg_id);
541 en_err(priv, "failed to add vxlan steering rule, err %d\n", err);
544 en_dbg(DRV, priv, "added vxlan steering rule, mac %pM reg_id %llx\n", addr, *reg_id);
549 static int mlx4_en_uc_steer_add(struct mlx4_en_priv *priv,
550 unsigned char *mac, int *qpn, u64 *reg_id)
552 struct mlx4_en_dev *mdev = priv->mdev;
553 struct mlx4_dev *dev = mdev->dev;
556 switch (dev->caps.steering_mode) {
557 case MLX4_STEERING_MODE_B0: {
562 memcpy(&gid[10], mac, ETH_ALEN);
565 err = mlx4_unicast_attach(dev, &qp, gid, 0, MLX4_PROT_ETH);
568 case MLX4_STEERING_MODE_DEVICE_MANAGED: {
569 struct mlx4_spec_list spec_eth = { {NULL} };
570 __be64 mac_mask = cpu_to_be64(MLX4_MAC_MASK << 16);
572 struct mlx4_net_trans_rule rule = {
573 .queue_mode = MLX4_NET_TRANS_Q_FIFO,
576 .promisc_mode = MLX4_FS_REGULAR,
577 .priority = MLX4_DOMAIN_NIC,
580 rule.port = priv->port;
582 INIT_LIST_HEAD(&rule.list);
584 spec_eth.id = MLX4_NET_TRANS_RULE_ID_ETH;
585 memcpy(spec_eth.eth.dst_mac, mac, ETH_ALEN);
586 memcpy(spec_eth.eth.dst_mac_msk, &mac_mask, ETH_ALEN);
587 list_add_tail(&spec_eth.list, &rule.list);
589 err = mlx4_flow_attach(dev, &rule, reg_id);
596 en_warn(priv, "Failed Attaching Unicast\n");
601 static void mlx4_en_uc_steer_release(struct mlx4_en_priv *priv,
602 unsigned char *mac, int qpn, u64 reg_id)
604 struct mlx4_en_dev *mdev = priv->mdev;
605 struct mlx4_dev *dev = mdev->dev;
607 switch (dev->caps.steering_mode) {
608 case MLX4_STEERING_MODE_B0: {
613 memcpy(&gid[10], mac, ETH_ALEN);
616 mlx4_unicast_detach(dev, &qp, gid, MLX4_PROT_ETH);
619 case MLX4_STEERING_MODE_DEVICE_MANAGED: {
620 mlx4_flow_detach(dev, reg_id);
624 en_err(priv, "Invalid steering mode.\n");
628 static int mlx4_en_get_qp(struct mlx4_en_priv *priv)
630 struct mlx4_en_dev *mdev = priv->mdev;
631 struct mlx4_dev *dev = mdev->dev;
634 int *qpn = &priv->base_qpn;
635 u64 mac = mlx4_mac_to_u64(priv->dev->dev_addr);
637 en_dbg(DRV, priv, "Registering MAC: %pM for adding\n",
638 priv->dev->dev_addr);
639 index = mlx4_register_mac(dev, priv->port, mac);
642 en_err(priv, "Failed adding MAC: %pM\n",
643 priv->dev->dev_addr);
647 en_info(priv, "Steering Mode %d\n", dev->caps.steering_mode);
649 if (dev->caps.steering_mode == MLX4_STEERING_MODE_A0) {
650 int base_qpn = mlx4_get_base_qpn(dev, priv->port);
651 *qpn = base_qpn + index;
655 err = mlx4_qp_reserve_range(dev, 1, 1, qpn, MLX4_RESERVE_A0_QP,
656 MLX4_RES_USAGE_DRIVER);
657 en_dbg(DRV, priv, "Reserved qp %d\n", *qpn);
659 en_err(priv, "Failed to reserve qp for mac registration\n");
660 mlx4_unregister_mac(dev, priv->port, mac);
667 static void mlx4_en_put_qp(struct mlx4_en_priv *priv)
669 struct mlx4_en_dev *mdev = priv->mdev;
670 struct mlx4_dev *dev = mdev->dev;
671 int qpn = priv->base_qpn;
673 if (dev->caps.steering_mode == MLX4_STEERING_MODE_A0) {
674 u64 mac = mlx4_mac_to_u64(priv->dev->dev_addr);
675 en_dbg(DRV, priv, "Registering MAC: %pM for deleting\n",
676 priv->dev->dev_addr);
677 mlx4_unregister_mac(dev, priv->port, mac);
679 en_dbg(DRV, priv, "Releasing qp: port %d, qpn %d\n",
681 mlx4_qp_release_range(dev, qpn, 1);
682 priv->flags &= ~MLX4_EN_FLAG_FORCE_PROMISC;
686 static int mlx4_en_replace_mac(struct mlx4_en_priv *priv, int qpn,
687 unsigned char *new_mac, unsigned char *prev_mac)
689 struct mlx4_en_dev *mdev = priv->mdev;
690 struct mlx4_dev *dev = mdev->dev;
692 u64 new_mac_u64 = mlx4_mac_to_u64(new_mac);
694 if (dev->caps.steering_mode != MLX4_STEERING_MODE_A0) {
695 struct hlist_head *bucket;
696 unsigned int mac_hash;
697 struct mlx4_mac_entry *entry;
698 struct hlist_node *tmp;
699 u64 prev_mac_u64 = mlx4_mac_to_u64(prev_mac);
701 bucket = &priv->mac_hash[prev_mac[MLX4_EN_MAC_HASH_IDX]];
702 hlist_for_each_entry_safe(entry, tmp, bucket, hlist) {
703 if (ether_addr_equal_64bits(entry->mac, prev_mac)) {
704 mlx4_en_uc_steer_release(priv, entry->mac,
706 mlx4_unregister_mac(dev, priv->port,
708 hlist_del_rcu(&entry->hlist);
710 memcpy(entry->mac, new_mac, ETH_ALEN);
712 mac_hash = new_mac[MLX4_EN_MAC_HASH_IDX];
713 hlist_add_head_rcu(&entry->hlist,
714 &priv->mac_hash[mac_hash]);
715 mlx4_register_mac(dev, priv->port, new_mac_u64);
716 err = mlx4_en_uc_steer_add(priv, new_mac,
721 if (priv->tunnel_reg_id) {
722 mlx4_flow_detach(priv->mdev->dev, priv->tunnel_reg_id);
723 priv->tunnel_reg_id = 0;
725 err = mlx4_en_tunnel_steer_add(priv, new_mac, qpn,
726 &priv->tunnel_reg_id);
733 return __mlx4_replace_mac(dev, priv->port, qpn, new_mac_u64);
736 static void mlx4_en_update_user_mac(struct mlx4_en_priv *priv,
737 unsigned char new_mac[ETH_ALEN + 2])
739 struct mlx4_en_dev *mdev = priv->mdev;
742 if (!(mdev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_USER_MAC_EN))
745 err = mlx4_SET_PORT_user_mac(mdev->dev, priv->port, new_mac);
747 en_err(priv, "Failed to pass user MAC(%pM) to Firmware for port %d, with error %d\n",
748 new_mac, priv->port, err);
751 static int mlx4_en_do_set_mac(struct mlx4_en_priv *priv,
752 unsigned char new_mac[ETH_ALEN + 2])
757 /* Remove old MAC and insert the new one */
758 err = mlx4_en_replace_mac(priv, priv->base_qpn,
759 new_mac, priv->current_mac);
761 en_err(priv, "Failed changing HW MAC address\n");
763 en_dbg(HW, priv, "Port is down while registering mac, exiting...\n");
766 memcpy(priv->current_mac, new_mac, sizeof(priv->current_mac));
771 static int mlx4_en_set_mac(struct net_device *dev, void *addr)
773 struct mlx4_en_priv *priv = netdev_priv(dev);
774 struct mlx4_en_dev *mdev = priv->mdev;
775 struct sockaddr *saddr = addr;
776 unsigned char new_mac[ETH_ALEN + 2];
779 if (!is_valid_ether_addr(saddr->sa_data))
780 return -EADDRNOTAVAIL;
782 mutex_lock(&mdev->state_lock);
783 memcpy(new_mac, saddr->sa_data, ETH_ALEN);
784 err = mlx4_en_do_set_mac(priv, new_mac);
788 memcpy(dev->dev_addr, saddr->sa_data, ETH_ALEN);
789 mlx4_en_update_user_mac(priv, new_mac);
791 mutex_unlock(&mdev->state_lock);
796 static void mlx4_en_clear_list(struct net_device *dev)
798 struct mlx4_en_priv *priv = netdev_priv(dev);
799 struct mlx4_en_mc_list *tmp, *mc_to_del;
801 list_for_each_entry_safe(mc_to_del, tmp, &priv->mc_list, list) {
802 list_del(&mc_to_del->list);
807 static void mlx4_en_cache_mclist(struct net_device *dev)
809 struct mlx4_en_priv *priv = netdev_priv(dev);
810 struct netdev_hw_addr *ha;
811 struct mlx4_en_mc_list *tmp;
813 mlx4_en_clear_list(dev);
814 netdev_for_each_mc_addr(ha, dev) {
815 tmp = kzalloc(sizeof(struct mlx4_en_mc_list), GFP_ATOMIC);
817 mlx4_en_clear_list(dev);
820 memcpy(tmp->addr, ha->addr, ETH_ALEN);
821 list_add_tail(&tmp->list, &priv->mc_list);
825 static void update_mclist_flags(struct mlx4_en_priv *priv,
826 struct list_head *dst,
827 struct list_head *src)
829 struct mlx4_en_mc_list *dst_tmp, *src_tmp, *new_mc;
832 /* Find all the entries that should be removed from dst,
833 * These are the entries that are not found in src
835 list_for_each_entry(dst_tmp, dst, list) {
837 list_for_each_entry(src_tmp, src, list) {
838 if (ether_addr_equal(dst_tmp->addr, src_tmp->addr)) {
844 dst_tmp->action = MCLIST_REM;
847 /* Add entries that exist in src but not in dst
848 * mark them as need to add
850 list_for_each_entry(src_tmp, src, list) {
852 list_for_each_entry(dst_tmp, dst, list) {
853 if (ether_addr_equal(dst_tmp->addr, src_tmp->addr)) {
854 dst_tmp->action = MCLIST_NONE;
860 new_mc = kmemdup(src_tmp,
861 sizeof(struct mlx4_en_mc_list),
866 new_mc->action = MCLIST_ADD;
867 list_add_tail(&new_mc->list, dst);
872 static void mlx4_en_set_rx_mode(struct net_device *dev)
874 struct mlx4_en_priv *priv = netdev_priv(dev);
879 queue_work(priv->mdev->workqueue, &priv->rx_mode_task);
882 static void mlx4_en_set_promisc_mode(struct mlx4_en_priv *priv,
883 struct mlx4_en_dev *mdev)
887 if (!(priv->flags & MLX4_EN_FLAG_PROMISC)) {
888 if (netif_msg_rx_status(priv))
889 en_warn(priv, "Entering promiscuous mode\n");
890 priv->flags |= MLX4_EN_FLAG_PROMISC;
892 /* Enable promiscouos mode */
893 switch (mdev->dev->caps.steering_mode) {
894 case MLX4_STEERING_MODE_DEVICE_MANAGED:
895 err = mlx4_flow_steer_promisc_add(mdev->dev,
898 MLX4_FS_ALL_DEFAULT);
900 en_err(priv, "Failed enabling promiscuous mode\n");
901 priv->flags |= MLX4_EN_FLAG_MC_PROMISC;
904 case MLX4_STEERING_MODE_B0:
905 err = mlx4_unicast_promisc_add(mdev->dev,
909 en_err(priv, "Failed enabling unicast promiscuous mode\n");
911 /* Add the default qp number as multicast
914 if (!(priv->flags & MLX4_EN_FLAG_MC_PROMISC)) {
915 err = mlx4_multicast_promisc_add(mdev->dev,
919 en_err(priv, "Failed enabling multicast promiscuous mode\n");
920 priv->flags |= MLX4_EN_FLAG_MC_PROMISC;
924 case MLX4_STEERING_MODE_A0:
925 err = mlx4_SET_PORT_qpn_calc(mdev->dev,
930 en_err(priv, "Failed enabling promiscuous mode\n");
934 /* Disable port multicast filter (unconditionally) */
935 err = mlx4_SET_MCAST_FLTR(mdev->dev, priv->port, 0,
936 0, MLX4_MCAST_DISABLE);
938 en_err(priv, "Failed disabling multicast filter\n");
942 static void mlx4_en_clear_promisc_mode(struct mlx4_en_priv *priv,
943 struct mlx4_en_dev *mdev)
947 if (netif_msg_rx_status(priv))
948 en_warn(priv, "Leaving promiscuous mode\n");
949 priv->flags &= ~MLX4_EN_FLAG_PROMISC;
951 /* Disable promiscouos mode */
952 switch (mdev->dev->caps.steering_mode) {
953 case MLX4_STEERING_MODE_DEVICE_MANAGED:
954 err = mlx4_flow_steer_promisc_remove(mdev->dev,
956 MLX4_FS_ALL_DEFAULT);
958 en_err(priv, "Failed disabling promiscuous mode\n");
959 priv->flags &= ~MLX4_EN_FLAG_MC_PROMISC;
962 case MLX4_STEERING_MODE_B0:
963 err = mlx4_unicast_promisc_remove(mdev->dev,
967 en_err(priv, "Failed disabling unicast promiscuous mode\n");
968 /* Disable Multicast promisc */
969 if (priv->flags & MLX4_EN_FLAG_MC_PROMISC) {
970 err = mlx4_multicast_promisc_remove(mdev->dev,
974 en_err(priv, "Failed disabling multicast promiscuous mode\n");
975 priv->flags &= ~MLX4_EN_FLAG_MC_PROMISC;
979 case MLX4_STEERING_MODE_A0:
980 err = mlx4_SET_PORT_qpn_calc(mdev->dev,
984 en_err(priv, "Failed disabling promiscuous mode\n");
989 static void mlx4_en_do_multicast(struct mlx4_en_priv *priv,
990 struct net_device *dev,
991 struct mlx4_en_dev *mdev)
993 struct mlx4_en_mc_list *mclist, *tmp;
995 u8 mc_list[16] = {0};
998 /* Enable/disable the multicast filter according to IFF_ALLMULTI */
999 if (dev->flags & IFF_ALLMULTI) {
1000 err = mlx4_SET_MCAST_FLTR(mdev->dev, priv->port, 0,
1001 0, MLX4_MCAST_DISABLE);
1003 en_err(priv, "Failed disabling multicast filter\n");
1005 /* Add the default qp number as multicast promisc */
1006 if (!(priv->flags & MLX4_EN_FLAG_MC_PROMISC)) {
1007 switch (mdev->dev->caps.steering_mode) {
1008 case MLX4_STEERING_MODE_DEVICE_MANAGED:
1009 err = mlx4_flow_steer_promisc_add(mdev->dev,
1012 MLX4_FS_MC_DEFAULT);
1015 case MLX4_STEERING_MODE_B0:
1016 err = mlx4_multicast_promisc_add(mdev->dev,
1021 case MLX4_STEERING_MODE_A0:
1025 en_err(priv, "Failed entering multicast promisc mode\n");
1026 priv->flags |= MLX4_EN_FLAG_MC_PROMISC;
1029 /* Disable Multicast promisc */
1030 if (priv->flags & MLX4_EN_FLAG_MC_PROMISC) {
1031 switch (mdev->dev->caps.steering_mode) {
1032 case MLX4_STEERING_MODE_DEVICE_MANAGED:
1033 err = mlx4_flow_steer_promisc_remove(mdev->dev,
1035 MLX4_FS_MC_DEFAULT);
1038 case MLX4_STEERING_MODE_B0:
1039 err = mlx4_multicast_promisc_remove(mdev->dev,
1044 case MLX4_STEERING_MODE_A0:
1048 en_err(priv, "Failed disabling multicast promiscuous mode\n");
1049 priv->flags &= ~MLX4_EN_FLAG_MC_PROMISC;
1052 err = mlx4_SET_MCAST_FLTR(mdev->dev, priv->port, 0,
1053 0, MLX4_MCAST_DISABLE);
1055 en_err(priv, "Failed disabling multicast filter\n");
1057 /* Flush mcast filter and init it with broadcast address */
1058 mlx4_SET_MCAST_FLTR(mdev->dev, priv->port, ETH_BCAST,
1059 1, MLX4_MCAST_CONFIG);
1061 /* Update multicast list - we cache all addresses so they won't
1062 * change while HW is updated holding the command semaphor */
1063 netif_addr_lock_bh(dev);
1064 mlx4_en_cache_mclist(dev);
1065 netif_addr_unlock_bh(dev);
1066 list_for_each_entry(mclist, &priv->mc_list, list) {
1067 mcast_addr = mlx4_mac_to_u64(mclist->addr);
1068 mlx4_SET_MCAST_FLTR(mdev->dev, priv->port,
1069 mcast_addr, 0, MLX4_MCAST_CONFIG);
1071 err = mlx4_SET_MCAST_FLTR(mdev->dev, priv->port, 0,
1072 0, MLX4_MCAST_ENABLE);
1074 en_err(priv, "Failed enabling multicast filter\n");
1076 update_mclist_flags(priv, &priv->curr_list, &priv->mc_list);
1077 list_for_each_entry_safe(mclist, tmp, &priv->curr_list, list) {
1078 if (mclist->action == MCLIST_REM) {
1079 /* detach this address and delete from list */
1080 memcpy(&mc_list[10], mclist->addr, ETH_ALEN);
1081 mc_list[5] = priv->port;
1082 err = mlx4_multicast_detach(mdev->dev,
1083 priv->rss_map.indir_qp,
1088 en_err(priv, "Fail to detach multicast address\n");
1090 if (mclist->tunnel_reg_id) {
1091 err = mlx4_flow_detach(priv->mdev->dev, mclist->tunnel_reg_id);
1093 en_err(priv, "Failed to detach multicast address\n");
1096 /* remove from list */
1097 list_del(&mclist->list);
1099 } else if (mclist->action == MCLIST_ADD) {
1100 /* attach the address */
1101 memcpy(&mc_list[10], mclist->addr, ETH_ALEN);
1102 /* needed for B0 steering support */
1103 mc_list[5] = priv->port;
1104 err = mlx4_multicast_attach(mdev->dev,
1105 priv->rss_map.indir_qp,
1111 en_err(priv, "Fail to attach multicast address\n");
1113 err = mlx4_en_tunnel_steer_add(priv, &mc_list[10], priv->base_qpn,
1114 &mclist->tunnel_reg_id);
1116 en_err(priv, "Failed to attach multicast address\n");
1122 static void mlx4_en_do_uc_filter(struct mlx4_en_priv *priv,
1123 struct net_device *dev,
1124 struct mlx4_en_dev *mdev)
1126 struct netdev_hw_addr *ha;
1127 struct mlx4_mac_entry *entry;
1128 struct hlist_node *tmp;
1132 struct hlist_head *bucket;
1137 /* Note that we do not need to protect our mac_hash traversal with rcu,
1138 * since all modification code is protected by mdev->state_lock
1141 /* find what to remove */
1142 for (i = 0; i < MLX4_EN_MAC_HASH_SIZE; ++i) {
1143 bucket = &priv->mac_hash[i];
1144 hlist_for_each_entry_safe(entry, tmp, bucket, hlist) {
1146 netdev_for_each_uc_addr(ha, dev) {
1147 if (ether_addr_equal_64bits(entry->mac,
1154 /* MAC address of the port is not in uc list */
1155 if (ether_addr_equal_64bits(entry->mac,
1160 mac = mlx4_mac_to_u64(entry->mac);
1161 mlx4_en_uc_steer_release(priv, entry->mac,
1164 mlx4_unregister_mac(mdev->dev, priv->port, mac);
1166 hlist_del_rcu(&entry->hlist);
1167 kfree_rcu(entry, rcu);
1168 en_dbg(DRV, priv, "Removed MAC %pM on port:%d\n",
1169 entry->mac, priv->port);
1175 /* if we didn't remove anything, there is no use in trying to add
1176 * again once we are in a forced promisc mode state
1178 if ((priv->flags & MLX4_EN_FLAG_FORCE_PROMISC) && 0 == removed)
1181 prev_flags = priv->flags;
1182 priv->flags &= ~MLX4_EN_FLAG_FORCE_PROMISC;
1184 /* find what to add */
1185 netdev_for_each_uc_addr(ha, dev) {
1187 bucket = &priv->mac_hash[ha->addr[MLX4_EN_MAC_HASH_IDX]];
1188 hlist_for_each_entry(entry, bucket, hlist) {
1189 if (ether_addr_equal_64bits(entry->mac, ha->addr)) {
1196 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1198 en_err(priv, "Failed adding MAC %pM on port:%d (out of memory)\n",
1199 ha->addr, priv->port);
1200 priv->flags |= MLX4_EN_FLAG_FORCE_PROMISC;
1203 mac = mlx4_mac_to_u64(ha->addr);
1204 memcpy(entry->mac, ha->addr, ETH_ALEN);
1205 err = mlx4_register_mac(mdev->dev, priv->port, mac);
1207 en_err(priv, "Failed registering MAC %pM on port %d: %d\n",
1208 ha->addr, priv->port, err);
1210 priv->flags |= MLX4_EN_FLAG_FORCE_PROMISC;
1213 err = mlx4_en_uc_steer_add(priv, ha->addr,
1217 en_err(priv, "Failed adding MAC %pM on port %d: %d\n",
1218 ha->addr, priv->port, err);
1219 mlx4_unregister_mac(mdev->dev, priv->port, mac);
1221 priv->flags |= MLX4_EN_FLAG_FORCE_PROMISC;
1224 unsigned int mac_hash;
1225 en_dbg(DRV, priv, "Added MAC %pM on port:%d\n",
1226 ha->addr, priv->port);
1227 mac_hash = ha->addr[MLX4_EN_MAC_HASH_IDX];
1228 bucket = &priv->mac_hash[mac_hash];
1229 hlist_add_head_rcu(&entry->hlist, bucket);
1234 if (priv->flags & MLX4_EN_FLAG_FORCE_PROMISC) {
1235 en_warn(priv, "Forcing promiscuous mode on port:%d\n",
1237 } else if (prev_flags & MLX4_EN_FLAG_FORCE_PROMISC) {
1238 en_warn(priv, "Stop forcing promiscuous mode on port:%d\n",
1243 static void mlx4_en_do_set_rx_mode(struct work_struct *work)
1245 struct mlx4_en_priv *priv = container_of(work, struct mlx4_en_priv,
1247 struct mlx4_en_dev *mdev = priv->mdev;
1248 struct net_device *dev = priv->dev;
1250 mutex_lock(&mdev->state_lock);
1251 if (!mdev->device_up) {
1252 en_dbg(HW, priv, "Card is not up, ignoring rx mode change.\n");
1255 if (!priv->port_up) {
1256 en_dbg(HW, priv, "Port is down, ignoring rx mode change.\n");
1260 if (!netif_carrier_ok(dev)) {
1261 if (!mlx4_en_QUERY_PORT(mdev, priv->port)) {
1262 if (priv->port_state.link_state) {
1263 priv->last_link_state = MLX4_DEV_EVENT_PORT_UP;
1264 netif_carrier_on(dev);
1265 en_dbg(LINK, priv, "Link Up\n");
1270 if (dev->priv_flags & IFF_UNICAST_FLT)
1271 mlx4_en_do_uc_filter(priv, dev, mdev);
1273 /* Promsicuous mode: disable all filters */
1274 if ((dev->flags & IFF_PROMISC) ||
1275 (priv->flags & MLX4_EN_FLAG_FORCE_PROMISC)) {
1276 mlx4_en_set_promisc_mode(priv, mdev);
1280 /* Not in promiscuous mode */
1281 if (priv->flags & MLX4_EN_FLAG_PROMISC)
1282 mlx4_en_clear_promisc_mode(priv, mdev);
1284 mlx4_en_do_multicast(priv, dev, mdev);
1286 mutex_unlock(&mdev->state_lock);
1289 #ifdef CONFIG_NET_POLL_CONTROLLER
1290 static void mlx4_en_netpoll(struct net_device *dev)
1292 struct mlx4_en_priv *priv = netdev_priv(dev);
1293 struct mlx4_en_cq *cq;
1296 for (i = 0; i < priv->tx_ring_num[TX]; i++) {
1297 cq = priv->tx_cq[TX][i];
1298 napi_schedule(&cq->napi);
1303 static int mlx4_en_set_rss_steer_rules(struct mlx4_en_priv *priv)
1307 int *qpn = &priv->base_qpn;
1308 struct mlx4_mac_entry *entry;
1310 err = mlx4_en_uc_steer_add(priv, priv->dev->dev_addr, qpn, ®_id);
1314 err = mlx4_en_tunnel_steer_add(priv, priv->dev->dev_addr, *qpn,
1315 &priv->tunnel_reg_id);
1319 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1325 memcpy(entry->mac, priv->dev->dev_addr, sizeof(entry->mac));
1326 memcpy(priv->current_mac, entry->mac, sizeof(priv->current_mac));
1327 entry->reg_id = reg_id;
1328 hlist_add_head_rcu(&entry->hlist,
1329 &priv->mac_hash[entry->mac[MLX4_EN_MAC_HASH_IDX]]);
1334 if (priv->tunnel_reg_id)
1335 mlx4_flow_detach(priv->mdev->dev, priv->tunnel_reg_id);
1338 mlx4_en_uc_steer_release(priv, priv->dev->dev_addr, *qpn, reg_id);
1342 static void mlx4_en_delete_rss_steer_rules(struct mlx4_en_priv *priv)
1346 int qpn = priv->base_qpn;
1347 struct hlist_head *bucket;
1348 struct hlist_node *tmp;
1349 struct mlx4_mac_entry *entry;
1351 for (i = 0; i < MLX4_EN_MAC_HASH_SIZE; ++i) {
1352 bucket = &priv->mac_hash[i];
1353 hlist_for_each_entry_safe(entry, tmp, bucket, hlist) {
1354 mac = mlx4_mac_to_u64(entry->mac);
1355 en_dbg(DRV, priv, "Registering MAC:%pM for deleting\n",
1357 mlx4_en_uc_steer_release(priv, entry->mac,
1358 qpn, entry->reg_id);
1360 mlx4_unregister_mac(priv->mdev->dev, priv->port, mac);
1361 hlist_del_rcu(&entry->hlist);
1362 kfree_rcu(entry, rcu);
1366 if (priv->tunnel_reg_id) {
1367 mlx4_flow_detach(priv->mdev->dev, priv->tunnel_reg_id);
1368 priv->tunnel_reg_id = 0;
1372 static void mlx4_en_tx_timeout(struct net_device *dev)
1374 struct mlx4_en_priv *priv = netdev_priv(dev);
1375 struct mlx4_en_dev *mdev = priv->mdev;
1378 if (netif_msg_timer(priv))
1379 en_warn(priv, "Tx timeout called on port:%d\n", priv->port);
1381 for (i = 0; i < priv->tx_ring_num[TX]; i++) {
1382 struct mlx4_en_tx_ring *tx_ring = priv->tx_ring[TX][i];
1384 if (!netif_tx_queue_stopped(netdev_get_tx_queue(dev, i)))
1386 en_warn(priv, "TX timeout on queue: %d, QP: 0x%x, CQ: 0x%x, Cons: 0x%x, Prod: 0x%x\n",
1387 i, tx_ring->qpn, tx_ring->sp_cqn,
1388 tx_ring->cons, tx_ring->prod);
1391 priv->port_stats.tx_timeout++;
1392 en_dbg(DRV, priv, "Scheduling watchdog\n");
1393 queue_work(mdev->workqueue, &priv->watchdog_task);
1398 mlx4_en_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
1400 struct mlx4_en_priv *priv = netdev_priv(dev);
1402 spin_lock_bh(&priv->stats_lock);
1403 mlx4_en_fold_software_stats(dev);
1404 netdev_stats_to_stats64(stats, &dev->stats);
1405 spin_unlock_bh(&priv->stats_lock);
1408 static void mlx4_en_set_default_moderation(struct mlx4_en_priv *priv)
1410 struct mlx4_en_cq *cq;
1413 /* If we haven't received a specific coalescing setting
1414 * (module param), we set the moderation parameters as follows:
1415 * - moder_cnt is set to the number of mtu sized packets to
1416 * satisfy our coalescing target.
1417 * - moder_time is set to a fixed value.
1419 priv->rx_frames = MLX4_EN_RX_COAL_TARGET;
1420 priv->rx_usecs = MLX4_EN_RX_COAL_TIME;
1421 priv->tx_frames = MLX4_EN_TX_COAL_PKTS;
1422 priv->tx_usecs = MLX4_EN_TX_COAL_TIME;
1423 en_dbg(INTR, priv, "Default coalescing params for mtu:%d - rx_frames:%d rx_usecs:%d\n",
1424 priv->dev->mtu, priv->rx_frames, priv->rx_usecs);
1426 /* Setup cq moderation params */
1427 for (i = 0; i < priv->rx_ring_num; i++) {
1428 cq = priv->rx_cq[i];
1429 cq->moder_cnt = priv->rx_frames;
1430 cq->moder_time = priv->rx_usecs;
1431 priv->last_moder_time[i] = MLX4_EN_AUTO_CONF;
1432 priv->last_moder_packets[i] = 0;
1433 priv->last_moder_bytes[i] = 0;
1436 for (t = 0 ; t < MLX4_EN_NUM_TX_TYPES; t++) {
1437 for (i = 0; i < priv->tx_ring_num[t]; i++) {
1438 cq = priv->tx_cq[t][i];
1439 cq->moder_cnt = priv->tx_frames;
1440 cq->moder_time = priv->tx_usecs;
1444 /* Reset auto-moderation params */
1445 priv->pkt_rate_low = MLX4_EN_RX_RATE_LOW;
1446 priv->rx_usecs_low = MLX4_EN_RX_COAL_TIME_LOW;
1447 priv->pkt_rate_high = MLX4_EN_RX_RATE_HIGH;
1448 priv->rx_usecs_high = MLX4_EN_RX_COAL_TIME_HIGH;
1449 priv->sample_interval = MLX4_EN_SAMPLE_INTERVAL;
1450 priv->adaptive_rx_coal = 1;
1451 priv->last_moder_jiffies = 0;
1452 priv->last_moder_tx_packets = 0;
1455 static void mlx4_en_auto_moderation(struct mlx4_en_priv *priv)
1457 unsigned long period = (unsigned long) (jiffies - priv->last_moder_jiffies);
1458 u32 pkt_rate_high, pkt_rate_low;
1459 struct mlx4_en_cq *cq;
1460 unsigned long packets;
1462 unsigned long avg_pkt_size;
1463 unsigned long rx_packets;
1464 unsigned long rx_bytes;
1465 unsigned long rx_pkt_diff;
1469 if (!priv->adaptive_rx_coal || period < priv->sample_interval * HZ)
1472 pkt_rate_low = READ_ONCE(priv->pkt_rate_low);
1473 pkt_rate_high = READ_ONCE(priv->pkt_rate_high);
1475 for (ring = 0; ring < priv->rx_ring_num; ring++) {
1476 rx_packets = READ_ONCE(priv->rx_ring[ring]->packets);
1477 rx_bytes = READ_ONCE(priv->rx_ring[ring]->bytes);
1479 rx_pkt_diff = rx_packets - priv->last_moder_packets[ring];
1480 packets = rx_pkt_diff;
1481 rate = packets * HZ / period;
1482 avg_pkt_size = packets ? (rx_bytes -
1483 priv->last_moder_bytes[ring]) / packets : 0;
1485 /* Apply auto-moderation only when packet rate
1486 * exceeds a rate that it matters */
1487 if (rate > (MLX4_EN_RX_RATE_THRESH / priv->rx_ring_num) &&
1488 avg_pkt_size > MLX4_EN_AVG_PKT_SMALL) {
1489 if (rate <= pkt_rate_low)
1490 moder_time = priv->rx_usecs_low;
1491 else if (rate >= pkt_rate_high)
1492 moder_time = priv->rx_usecs_high;
1494 moder_time = (rate - pkt_rate_low) *
1495 (priv->rx_usecs_high - priv->rx_usecs_low) /
1496 (pkt_rate_high - pkt_rate_low) +
1499 moder_time = priv->rx_usecs_low;
1502 cq = priv->rx_cq[ring];
1503 if (moder_time != priv->last_moder_time[ring] ||
1504 cq->moder_cnt != priv->rx_frames) {
1505 priv->last_moder_time[ring] = moder_time;
1506 cq->moder_time = moder_time;
1507 cq->moder_cnt = priv->rx_frames;
1508 err = mlx4_en_set_cq_moder(priv, cq);
1510 en_err(priv, "Failed modifying moderation for cq:%d\n",
1513 priv->last_moder_packets[ring] = rx_packets;
1514 priv->last_moder_bytes[ring] = rx_bytes;
1517 priv->last_moder_jiffies = jiffies;
1520 static void mlx4_en_do_get_stats(struct work_struct *work)
1522 struct delayed_work *delay = to_delayed_work(work);
1523 struct mlx4_en_priv *priv = container_of(delay, struct mlx4_en_priv,
1525 struct mlx4_en_dev *mdev = priv->mdev;
1528 mutex_lock(&mdev->state_lock);
1529 if (mdev->device_up) {
1530 if (priv->port_up) {
1531 err = mlx4_en_DUMP_ETH_STATS(mdev, priv->port, 0);
1533 en_dbg(HW, priv, "Could not update stats\n");
1535 mlx4_en_auto_moderation(priv);
1538 queue_delayed_work(mdev->workqueue, &priv->stats_task, STATS_DELAY);
1540 if (mdev->mac_removed[MLX4_MAX_PORTS + 1 - priv->port]) {
1541 mlx4_en_do_set_mac(priv, priv->current_mac);
1542 mdev->mac_removed[MLX4_MAX_PORTS + 1 - priv->port] = 0;
1544 mutex_unlock(&mdev->state_lock);
1547 /* mlx4_en_service_task - Run service task for tasks that needed to be done
1550 static void mlx4_en_service_task(struct work_struct *work)
1552 struct delayed_work *delay = to_delayed_work(work);
1553 struct mlx4_en_priv *priv = container_of(delay, struct mlx4_en_priv,
1555 struct mlx4_en_dev *mdev = priv->mdev;
1557 mutex_lock(&mdev->state_lock);
1558 if (mdev->device_up) {
1559 if (mdev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_TS)
1560 mlx4_en_ptp_overflow_check(mdev);
1562 mlx4_en_recover_from_oom(priv);
1563 queue_delayed_work(mdev->workqueue, &priv->service_task,
1564 SERVICE_TASK_DELAY);
1566 mutex_unlock(&mdev->state_lock);
1569 static void mlx4_en_linkstate(struct work_struct *work)
1571 struct mlx4_en_priv *priv = container_of(work, struct mlx4_en_priv,
1573 struct mlx4_en_dev *mdev = priv->mdev;
1574 int linkstate = priv->link_state;
1576 mutex_lock(&mdev->state_lock);
1577 /* If observable port state changed set carrier state and
1578 * report to system log */
1579 if (priv->last_link_state != linkstate) {
1580 if (linkstate == MLX4_DEV_EVENT_PORT_DOWN) {
1581 en_info(priv, "Link Down\n");
1582 netif_carrier_off(priv->dev);
1584 en_info(priv, "Link Up\n");
1585 netif_carrier_on(priv->dev);
1588 priv->last_link_state = linkstate;
1589 mutex_unlock(&mdev->state_lock);
1592 static int mlx4_en_init_affinity_hint(struct mlx4_en_priv *priv, int ring_idx)
1594 struct mlx4_en_rx_ring *ring = priv->rx_ring[ring_idx];
1595 int numa_node = priv->mdev->dev->numa_node;
1597 if (!zalloc_cpumask_var(&ring->affinity_mask, GFP_KERNEL))
1600 cpumask_set_cpu(cpumask_local_spread(ring_idx, numa_node),
1601 ring->affinity_mask);
1605 static void mlx4_en_free_affinity_hint(struct mlx4_en_priv *priv, int ring_idx)
1607 free_cpumask_var(priv->rx_ring[ring_idx]->affinity_mask);
1610 static void mlx4_en_init_recycle_ring(struct mlx4_en_priv *priv,
1613 struct mlx4_en_tx_ring *tx_ring = priv->tx_ring[TX_XDP][tx_ring_idx];
1614 int rr_index = tx_ring_idx;
1616 tx_ring->free_tx_desc = mlx4_en_recycle_tx_desc;
1617 tx_ring->recycle_ring = priv->rx_ring[rr_index];
1618 en_dbg(DRV, priv, "Set tx_ring[%d][%d]->recycle_ring = rx_ring[%d]\n",
1619 TX_XDP, tx_ring_idx, rr_index);
1622 int mlx4_en_start_port(struct net_device *dev)
1624 struct mlx4_en_priv *priv = netdev_priv(dev);
1625 struct mlx4_en_dev *mdev = priv->mdev;
1626 struct mlx4_en_cq *cq;
1627 struct mlx4_en_tx_ring *tx_ring;
1632 u8 mc_list[16] = {0};
1634 if (priv->port_up) {
1635 en_dbg(DRV, priv, "start port called while port already up\n");
1639 INIT_LIST_HEAD(&priv->mc_list);
1640 INIT_LIST_HEAD(&priv->curr_list);
1641 INIT_LIST_HEAD(&priv->ethtool_list);
1642 memset(&priv->ethtool_rules[0], 0,
1643 sizeof(struct ethtool_flow_id) * MAX_NUM_OF_FS_RULES);
1645 /* Calculate Rx buf size */
1646 dev->mtu = min(dev->mtu, priv->max_mtu);
1647 mlx4_en_calc_rx_buf(dev);
1648 en_dbg(DRV, priv, "Rx buf size:%d\n", priv->rx_skb_size);
1650 /* Configure rx cq's and rings */
1651 err = mlx4_en_activate_rx_rings(priv);
1653 en_err(priv, "Failed to activate RX rings\n");
1656 for (i = 0; i < priv->rx_ring_num; i++) {
1657 cq = priv->rx_cq[i];
1659 err = mlx4_en_init_affinity_hint(priv, i);
1661 en_err(priv, "Failed preparing IRQ affinity hint\n");
1665 err = mlx4_en_activate_cq(priv, cq, i);
1667 en_err(priv, "Failed activating Rx CQ\n");
1668 mlx4_en_free_affinity_hint(priv, i);
1672 for (j = 0; j < cq->size; j++) {
1673 struct mlx4_cqe *cqe = NULL;
1675 cqe = mlx4_en_get_cqe(cq->buf, j, priv->cqe_size) +
1677 cqe->owner_sr_opcode = MLX4_CQE_OWNER_MASK;
1680 err = mlx4_en_set_cq_moder(priv, cq);
1682 en_err(priv, "Failed setting cq moderation parameters\n");
1683 mlx4_en_deactivate_cq(priv, cq);
1684 mlx4_en_free_affinity_hint(priv, i);
1687 mlx4_en_arm_cq(priv, cq);
1688 priv->rx_ring[i]->cqn = cq->mcq.cqn;
1693 en_dbg(DRV, priv, "Getting qp number for port %d\n", priv->port);
1694 err = mlx4_en_get_qp(priv);
1696 en_err(priv, "Failed getting eth qp\n");
1699 mdev->mac_removed[priv->port] = 0;
1701 priv->counter_index =
1702 mlx4_get_default_counter_index(mdev->dev, priv->port);
1704 err = mlx4_en_config_rss_steer(priv);
1706 en_err(priv, "Failed configuring rss steering\n");
1710 err = mlx4_en_create_drop_qp(priv);
1714 /* Configure tx cq's and rings */
1715 for (t = 0 ; t < MLX4_EN_NUM_TX_TYPES; t++) {
1716 u8 num_tx_rings_p_up = t == TX ?
1717 priv->num_tx_rings_p_up : priv->tx_ring_num[t];
1719 for (i = 0; i < priv->tx_ring_num[t]; i++) {
1721 cq = priv->tx_cq[t][i];
1722 err = mlx4_en_activate_cq(priv, cq, i);
1724 en_err(priv, "Failed allocating Tx CQ\n");
1727 err = mlx4_en_set_cq_moder(priv, cq);
1729 en_err(priv, "Failed setting cq moderation parameters\n");
1730 mlx4_en_deactivate_cq(priv, cq);
1734 "Resetting index of collapsed CQ:%d to -1\n", i);
1735 cq->buf->wqe_index = cpu_to_be16(0xffff);
1737 /* Configure ring */
1738 tx_ring = priv->tx_ring[t][i];
1739 err = mlx4_en_activate_tx_ring(priv, tx_ring,
1741 i / num_tx_rings_p_up);
1743 en_err(priv, "Failed allocating Tx ring\n");
1744 mlx4_en_deactivate_cq(priv, cq);
1748 tx_ring->tx_queue = netdev_get_tx_queue(dev, i);
1749 tx_ring->recycle_ring = NULL;
1751 /* Arm CQ for TX completions */
1752 mlx4_en_arm_cq(priv, cq);
1755 mlx4_en_init_tx_xdp_ring_descs(priv, tx_ring);
1756 mlx4_en_init_recycle_ring(priv, i);
1757 /* XDP TX CQ should never be armed */
1760 /* Set initial ownership of all Tx TXBBs to SW (1) */
1761 for (j = 0; j < tx_ring->buf_size; j += STAMP_STRIDE)
1762 *((u32 *)(tx_ring->buf + j)) = 0xffffffff;
1766 /* Configure port */
1767 err = mlx4_SET_PORT_general(mdev->dev, priv->port,
1768 priv->rx_skb_size + ETH_FCS_LEN,
1769 priv->prof->tx_pause,
1771 priv->prof->rx_pause,
1772 priv->prof->rx_ppp);
1774 en_err(priv, "Failed setting port general configurations for port %d, with error %d\n",
1779 err = mlx4_SET_PORT_user_mtu(mdev->dev, priv->port, dev->mtu);
1781 en_err(priv, "Failed to pass user MTU(%d) to Firmware for port %d, with error %d\n",
1782 dev->mtu, priv->port, err);
1786 /* Set default qp number */
1787 err = mlx4_SET_PORT_qpn_calc(mdev->dev, priv->port, priv->base_qpn, 0);
1789 en_err(priv, "Failed setting default qp numbers\n");
1793 if (mdev->dev->caps.tunnel_offload_mode == MLX4_TUNNEL_OFFLOAD_MODE_VXLAN) {
1794 err = mlx4_SET_PORT_VXLAN(mdev->dev, priv->port, VXLAN_STEER_BY_OUTER_MAC, 1);
1796 en_err(priv, "Failed setting port L2 tunnel configuration, err %d\n",
1803 en_dbg(HW, priv, "Initializing port\n");
1804 err = mlx4_INIT_PORT(mdev->dev, priv->port);
1806 en_err(priv, "Failed Initializing port\n");
1810 /* Set Unicast and VXLAN steering rules */
1811 if (mdev->dev->caps.steering_mode != MLX4_STEERING_MODE_A0 &&
1812 mlx4_en_set_rss_steer_rules(priv))
1813 mlx4_warn(mdev, "Failed setting steering rules\n");
1815 /* Attach rx QP to bradcast address */
1816 eth_broadcast_addr(&mc_list[10]);
1817 mc_list[5] = priv->port; /* needed for B0 steering support */
1818 if (mlx4_multicast_attach(mdev->dev, priv->rss_map.indir_qp, mc_list,
1819 priv->port, 0, MLX4_PROT_ETH,
1820 &priv->broadcast_id))
1821 mlx4_warn(mdev, "Failed Attaching Broadcast\n");
1823 /* Must redo promiscuous mode setup. */
1824 priv->flags &= ~(MLX4_EN_FLAG_PROMISC | MLX4_EN_FLAG_MC_PROMISC);
1826 /* Schedule multicast task to populate multicast list */
1827 queue_work(mdev->workqueue, &priv->rx_mode_task);
1829 if (priv->mdev->dev->caps.tunnel_offload_mode == MLX4_TUNNEL_OFFLOAD_MODE_VXLAN)
1830 udp_tunnel_get_rx_info(dev);
1832 priv->port_up = true;
1834 /* Process all completions if exist to prevent
1835 * the queues freezing if they are full
1837 for (i = 0; i < priv->rx_ring_num; i++) {
1839 napi_schedule(&priv->rx_cq[i]->napi);
1843 netif_tx_start_all_queues(dev);
1844 netif_device_attach(dev);
1849 if (t == MLX4_EN_NUM_TX_TYPES) {
1851 i = priv->tx_ring_num[t];
1855 mlx4_en_deactivate_tx_ring(priv, priv->tx_ring[t][i]);
1856 mlx4_en_deactivate_cq(priv, priv->tx_cq[t][i]);
1860 i = priv->tx_ring_num[t];
1862 mlx4_en_destroy_drop_qp(priv);
1864 mlx4_en_release_rss_steer(priv);
1866 mlx4_en_put_qp(priv);
1868 while (rx_index--) {
1869 mlx4_en_deactivate_cq(priv, priv->rx_cq[rx_index]);
1870 mlx4_en_free_affinity_hint(priv, rx_index);
1872 for (i = 0; i < priv->rx_ring_num; i++)
1873 mlx4_en_deactivate_rx_ring(priv, priv->rx_ring[i]);
1875 return err; /* need to close devices */
1879 void mlx4_en_stop_port(struct net_device *dev, int detach)
1881 struct mlx4_en_priv *priv = netdev_priv(dev);
1882 struct mlx4_en_dev *mdev = priv->mdev;
1883 struct mlx4_en_mc_list *mclist, *tmp;
1884 struct ethtool_flow_id *flow, *tmp_flow;
1886 u8 mc_list[16] = {0};
1888 if (!priv->port_up) {
1889 en_dbg(DRV, priv, "stop port called while port already down\n");
1894 mlx4_CLOSE_PORT(mdev->dev, priv->port);
1896 /* Synchronize with tx routine */
1897 netif_tx_lock_bh(dev);
1899 netif_device_detach(dev);
1900 netif_tx_stop_all_queues(dev);
1901 netif_tx_unlock_bh(dev);
1903 netif_tx_disable(dev);
1905 spin_lock_bh(&priv->stats_lock);
1906 mlx4_en_fold_software_stats(dev);
1907 /* Set port as not active */
1908 priv->port_up = false;
1909 spin_unlock_bh(&priv->stats_lock);
1911 priv->counter_index = MLX4_SINK_COUNTER_INDEX(mdev->dev);
1913 /* Promsicuous mode */
1914 if (mdev->dev->caps.steering_mode ==
1915 MLX4_STEERING_MODE_DEVICE_MANAGED) {
1916 priv->flags &= ~(MLX4_EN_FLAG_PROMISC |
1917 MLX4_EN_FLAG_MC_PROMISC);
1918 mlx4_flow_steer_promisc_remove(mdev->dev,
1920 MLX4_FS_ALL_DEFAULT);
1921 mlx4_flow_steer_promisc_remove(mdev->dev,
1923 MLX4_FS_MC_DEFAULT);
1924 } else if (priv->flags & MLX4_EN_FLAG_PROMISC) {
1925 priv->flags &= ~MLX4_EN_FLAG_PROMISC;
1927 /* Disable promiscouos mode */
1928 mlx4_unicast_promisc_remove(mdev->dev, priv->base_qpn,
1931 /* Disable Multicast promisc */
1932 if (priv->flags & MLX4_EN_FLAG_MC_PROMISC) {
1933 mlx4_multicast_promisc_remove(mdev->dev, priv->base_qpn,
1935 priv->flags &= ~MLX4_EN_FLAG_MC_PROMISC;
1939 /* Detach All multicasts */
1940 eth_broadcast_addr(&mc_list[10]);
1941 mc_list[5] = priv->port; /* needed for B0 steering support */
1942 mlx4_multicast_detach(mdev->dev, priv->rss_map.indir_qp, mc_list,
1943 MLX4_PROT_ETH, priv->broadcast_id);
1944 list_for_each_entry(mclist, &priv->curr_list, list) {
1945 memcpy(&mc_list[10], mclist->addr, ETH_ALEN);
1946 mc_list[5] = priv->port;
1947 mlx4_multicast_detach(mdev->dev, priv->rss_map.indir_qp,
1948 mc_list, MLX4_PROT_ETH, mclist->reg_id);
1949 if (mclist->tunnel_reg_id)
1950 mlx4_flow_detach(mdev->dev, mclist->tunnel_reg_id);
1952 mlx4_en_clear_list(dev);
1953 list_for_each_entry_safe(mclist, tmp, &priv->curr_list, list) {
1954 list_del(&mclist->list);
1958 /* Flush multicast filter */
1959 mlx4_SET_MCAST_FLTR(mdev->dev, priv->port, 0, 1, MLX4_MCAST_CONFIG);
1961 /* Remove flow steering rules for the port*/
1962 if (mdev->dev->caps.steering_mode ==
1963 MLX4_STEERING_MODE_DEVICE_MANAGED) {
1965 list_for_each_entry_safe(flow, tmp_flow,
1966 &priv->ethtool_list, list) {
1967 mlx4_flow_detach(mdev->dev, flow->id);
1968 list_del(&flow->list);
1972 mlx4_en_destroy_drop_qp(priv);
1975 for (t = 0; t < MLX4_EN_NUM_TX_TYPES; t++) {
1976 for (i = 0; i < priv->tx_ring_num[t]; i++) {
1977 mlx4_en_deactivate_tx_ring(priv, priv->tx_ring[t][i]);
1978 mlx4_en_deactivate_cq(priv, priv->tx_cq[t][i]);
1983 for (t = 0; t < MLX4_EN_NUM_TX_TYPES; t++)
1984 for (i = 0; i < priv->tx_ring_num[t]; i++)
1985 mlx4_en_free_tx_buf(dev, priv->tx_ring[t][i]);
1987 if (mdev->dev->caps.steering_mode != MLX4_STEERING_MODE_A0)
1988 mlx4_en_delete_rss_steer_rules(priv);
1991 mlx4_en_release_rss_steer(priv);
1993 /* Unregister Mac address for the port */
1994 mlx4_en_put_qp(priv);
1995 if (!(mdev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_REASSIGN_MAC_EN))
1996 mdev->mac_removed[priv->port] = 1;
1999 for (i = 0; i < priv->rx_ring_num; i++) {
2000 struct mlx4_en_cq *cq = priv->rx_cq[i];
2002 napi_synchronize(&cq->napi);
2003 mlx4_en_deactivate_rx_ring(priv, priv->rx_ring[i]);
2004 mlx4_en_deactivate_cq(priv, cq);
2006 mlx4_en_free_affinity_hint(priv, i);
2010 static void mlx4_en_restart(struct work_struct *work)
2012 struct mlx4_en_priv *priv = container_of(work, struct mlx4_en_priv,
2014 struct mlx4_en_dev *mdev = priv->mdev;
2015 struct net_device *dev = priv->dev;
2017 en_dbg(DRV, priv, "Watchdog task called for port %d\n", priv->port);
2020 mutex_lock(&mdev->state_lock);
2021 if (priv->port_up) {
2022 mlx4_en_stop_port(dev, 1);
2023 if (mlx4_en_start_port(dev))
2024 en_err(priv, "Failed restarting port %d\n", priv->port);
2026 mutex_unlock(&mdev->state_lock);
2030 static void mlx4_en_clear_stats(struct net_device *dev)
2032 struct mlx4_en_priv *priv = netdev_priv(dev);
2033 struct mlx4_en_dev *mdev = priv->mdev;
2034 struct mlx4_en_tx_ring **tx_ring;
2037 if (!mlx4_is_slave(mdev->dev))
2038 if (mlx4_en_DUMP_ETH_STATS(mdev, priv->port, 1))
2039 en_dbg(HW, priv, "Failed dumping statistics\n");
2041 memset(&priv->pstats, 0, sizeof(priv->pstats));
2042 memset(&priv->pkstats, 0, sizeof(priv->pkstats));
2043 memset(&priv->port_stats, 0, sizeof(priv->port_stats));
2044 memset(&priv->rx_flowstats, 0, sizeof(priv->rx_flowstats));
2045 memset(&priv->tx_flowstats, 0, sizeof(priv->tx_flowstats));
2046 memset(&priv->rx_priority_flowstats, 0,
2047 sizeof(priv->rx_priority_flowstats));
2048 memset(&priv->tx_priority_flowstats, 0,
2049 sizeof(priv->tx_priority_flowstats));
2050 memset(&priv->pf_stats, 0, sizeof(priv->pf_stats));
2052 tx_ring = priv->tx_ring[TX];
2053 for (i = 0; i < priv->tx_ring_num[TX]; i++) {
2054 tx_ring[i]->bytes = 0;
2055 tx_ring[i]->packets = 0;
2056 tx_ring[i]->tx_csum = 0;
2057 tx_ring[i]->tx_dropped = 0;
2058 tx_ring[i]->queue_stopped = 0;
2059 tx_ring[i]->wake_queue = 0;
2060 tx_ring[i]->tso_packets = 0;
2061 tx_ring[i]->xmit_more = 0;
2063 for (i = 0; i < priv->rx_ring_num; i++) {
2064 priv->rx_ring[i]->bytes = 0;
2065 priv->rx_ring[i]->packets = 0;
2066 priv->rx_ring[i]->csum_ok = 0;
2067 priv->rx_ring[i]->csum_none = 0;
2068 priv->rx_ring[i]->csum_complete = 0;
2072 static int mlx4_en_open(struct net_device *dev)
2074 struct mlx4_en_priv *priv = netdev_priv(dev);
2075 struct mlx4_en_dev *mdev = priv->mdev;
2078 mutex_lock(&mdev->state_lock);
2080 if (!mdev->device_up) {
2081 en_err(priv, "Cannot open - device down/disabled\n");
2086 /* Reset HW statistics and SW counters */
2087 mlx4_en_clear_stats(dev);
2089 err = mlx4_en_start_port(dev);
2091 en_err(priv, "Failed starting port:%d\n", priv->port);
2094 mutex_unlock(&mdev->state_lock);
2099 static int mlx4_en_close(struct net_device *dev)
2101 struct mlx4_en_priv *priv = netdev_priv(dev);
2102 struct mlx4_en_dev *mdev = priv->mdev;
2104 en_dbg(IFDOWN, priv, "Close port called\n");
2106 mutex_lock(&mdev->state_lock);
2108 mlx4_en_stop_port(dev, 0);
2109 netif_carrier_off(dev);
2111 mutex_unlock(&mdev->state_lock);
2115 static void mlx4_en_free_resources(struct mlx4_en_priv *priv)
2119 #ifdef CONFIG_RFS_ACCEL
2120 priv->dev->rx_cpu_rmap = NULL;
2123 for (t = 0; t < MLX4_EN_NUM_TX_TYPES; t++) {
2124 for (i = 0; i < priv->tx_ring_num[t]; i++) {
2125 if (priv->tx_ring[t] && priv->tx_ring[t][i])
2126 mlx4_en_destroy_tx_ring(priv,
2127 &priv->tx_ring[t][i]);
2128 if (priv->tx_cq[t] && priv->tx_cq[t][i])
2129 mlx4_en_destroy_cq(priv, &priv->tx_cq[t][i]);
2131 kfree(priv->tx_ring[t]);
2132 kfree(priv->tx_cq[t]);
2135 for (i = 0; i < priv->rx_ring_num; i++) {
2136 if (priv->rx_ring[i])
2137 mlx4_en_destroy_rx_ring(priv, &priv->rx_ring[i],
2138 priv->prof->rx_ring_size, priv->stride);
2140 mlx4_en_destroy_cq(priv, &priv->rx_cq[i]);
2145 static int mlx4_en_alloc_resources(struct mlx4_en_priv *priv)
2147 struct mlx4_en_port_profile *prof = priv->prof;
2151 /* Create tx Rings */
2152 for (t = 0; t < MLX4_EN_NUM_TX_TYPES; t++) {
2153 for (i = 0; i < priv->tx_ring_num[t]; i++) {
2154 node = cpu_to_node(i % num_online_cpus());
2155 if (mlx4_en_create_cq(priv, &priv->tx_cq[t][i],
2156 prof->tx_ring_size, i, t, node))
2159 if (mlx4_en_create_tx_ring(priv, &priv->tx_ring[t][i],
2161 TXBB_SIZE, node, i))
2166 /* Create rx Rings */
2167 for (i = 0; i < priv->rx_ring_num; i++) {
2168 node = cpu_to_node(i % num_online_cpus());
2169 if (mlx4_en_create_cq(priv, &priv->rx_cq[i],
2170 prof->rx_ring_size, i, RX, node))
2173 if (mlx4_en_create_rx_ring(priv, &priv->rx_ring[i],
2174 prof->rx_ring_size, priv->stride,
2180 #ifdef CONFIG_RFS_ACCEL
2181 priv->dev->rx_cpu_rmap = mlx4_get_cpu_rmap(priv->mdev->dev, priv->port);
2187 en_err(priv, "Failed to allocate NIC resources\n");
2188 for (i = 0; i < priv->rx_ring_num; i++) {
2189 if (priv->rx_ring[i])
2190 mlx4_en_destroy_rx_ring(priv, &priv->rx_ring[i],
2194 mlx4_en_destroy_cq(priv, &priv->rx_cq[i]);
2196 for (t = 0; t < MLX4_EN_NUM_TX_TYPES; t++) {
2197 for (i = 0; i < priv->tx_ring_num[t]; i++) {
2198 if (priv->tx_ring[t][i])
2199 mlx4_en_destroy_tx_ring(priv,
2200 &priv->tx_ring[t][i]);
2201 if (priv->tx_cq[t][i])
2202 mlx4_en_destroy_cq(priv, &priv->tx_cq[t][i]);
2209 static int mlx4_en_copy_priv(struct mlx4_en_priv *dst,
2210 struct mlx4_en_priv *src,
2211 struct mlx4_en_port_profile *prof)
2215 memcpy(&dst->hwtstamp_config, &prof->hwtstamp_config,
2216 sizeof(dst->hwtstamp_config));
2217 dst->num_tx_rings_p_up = prof->num_tx_rings_p_up;
2218 dst->rx_ring_num = prof->rx_ring_num;
2219 dst->flags = prof->flags;
2220 dst->mdev = src->mdev;
2221 dst->port = src->port;
2222 dst->dev = src->dev;
2224 dst->stride = roundup_pow_of_two(sizeof(struct mlx4_en_rx_desc) +
2225 DS_SIZE * MLX4_EN_MAX_RX_FRAGS);
2227 for (t = 0; t < MLX4_EN_NUM_TX_TYPES; t++) {
2228 dst->tx_ring_num[t] = prof->tx_ring_num[t];
2229 if (!dst->tx_ring_num[t])
2232 dst->tx_ring[t] = kzalloc(sizeof(struct mlx4_en_tx_ring *) *
2233 MAX_TX_RINGS, GFP_KERNEL);
2234 if (!dst->tx_ring[t])
2237 dst->tx_cq[t] = kzalloc(sizeof(struct mlx4_en_cq *) *
2238 MAX_TX_RINGS, GFP_KERNEL);
2239 if (!dst->tx_cq[t]) {
2240 kfree(dst->tx_ring[t]);
2249 kfree(dst->tx_ring[t]);
2250 kfree(dst->tx_cq[t]);
2255 static void mlx4_en_update_priv(struct mlx4_en_priv *dst,
2256 struct mlx4_en_priv *src)
2259 memcpy(dst->rx_ring, src->rx_ring,
2260 sizeof(struct mlx4_en_rx_ring *) * src->rx_ring_num);
2261 memcpy(dst->rx_cq, src->rx_cq,
2262 sizeof(struct mlx4_en_cq *) * src->rx_ring_num);
2263 memcpy(&dst->hwtstamp_config, &src->hwtstamp_config,
2264 sizeof(dst->hwtstamp_config));
2265 for (t = 0; t < MLX4_EN_NUM_TX_TYPES; t++) {
2266 dst->tx_ring_num[t] = src->tx_ring_num[t];
2267 dst->tx_ring[t] = src->tx_ring[t];
2268 dst->tx_cq[t] = src->tx_cq[t];
2270 dst->num_tx_rings_p_up = src->num_tx_rings_p_up;
2271 dst->rx_ring_num = src->rx_ring_num;
2272 memcpy(dst->prof, src->prof, sizeof(struct mlx4_en_port_profile));
2275 int mlx4_en_try_alloc_resources(struct mlx4_en_priv *priv,
2276 struct mlx4_en_priv *tmp,
2277 struct mlx4_en_port_profile *prof,
2278 bool carry_xdp_prog)
2280 struct bpf_prog *xdp_prog;
2283 mlx4_en_copy_priv(tmp, priv, prof);
2285 if (mlx4_en_alloc_resources(tmp)) {
2287 "%s: Resource allocation failed, using previous configuration\n",
2289 for (t = 0; t < MLX4_EN_NUM_TX_TYPES; t++) {
2290 kfree(tmp->tx_ring[t]);
2291 kfree(tmp->tx_cq[t]);
2296 /* All rx_rings has the same xdp_prog. Pick the first one. */
2297 xdp_prog = rcu_dereference_protected(
2298 priv->rx_ring[0]->xdp_prog,
2299 lockdep_is_held(&priv->mdev->state_lock));
2301 if (xdp_prog && carry_xdp_prog) {
2302 xdp_prog = bpf_prog_add(xdp_prog, tmp->rx_ring_num);
2303 if (IS_ERR(xdp_prog)) {
2304 mlx4_en_free_resources(tmp);
2305 return PTR_ERR(xdp_prog);
2307 for (i = 0; i < tmp->rx_ring_num; i++)
2308 rcu_assign_pointer(tmp->rx_ring[i]->xdp_prog,
2315 void mlx4_en_safe_replace_resources(struct mlx4_en_priv *priv,
2316 struct mlx4_en_priv *tmp)
2318 mlx4_en_free_resources(priv);
2319 mlx4_en_update_priv(priv, tmp);
2322 void mlx4_en_destroy_netdev(struct net_device *dev)
2324 struct mlx4_en_priv *priv = netdev_priv(dev);
2325 struct mlx4_en_dev *mdev = priv->mdev;
2327 en_dbg(DRV, priv, "Destroying netdev on port:%d\n", priv->port);
2329 /* Unregister device - this will close the port if it was up */
2330 if (priv->registered) {
2331 devlink_port_type_clear(mlx4_get_devlink_port(mdev->dev,
2333 unregister_netdev(dev);
2336 if (priv->allocated)
2337 mlx4_free_hwq_res(mdev->dev, &priv->res, MLX4_EN_PAGE_SIZE);
2339 cancel_delayed_work(&priv->stats_task);
2340 cancel_delayed_work(&priv->service_task);
2341 /* flush any pending task for this netdev */
2342 flush_workqueue(mdev->workqueue);
2344 if (mdev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_TS)
2345 mlx4_en_remove_timestamp(mdev);
2347 /* Detach the netdev so tasks would not attempt to access it */
2348 mutex_lock(&mdev->state_lock);
2349 mdev->pndev[priv->port] = NULL;
2350 mdev->upper[priv->port] = NULL;
2352 #ifdef CONFIG_RFS_ACCEL
2353 mlx4_en_cleanup_filters(priv);
2356 mlx4_en_free_resources(priv);
2357 mutex_unlock(&mdev->state_lock);
2362 static bool mlx4_en_check_xdp_mtu(struct net_device *dev, int mtu)
2364 struct mlx4_en_priv *priv = netdev_priv(dev);
2366 if (mtu > MLX4_EN_MAX_XDP_MTU) {
2367 en_err(priv, "mtu:%d > max:%d when XDP prog is attached\n",
2368 mtu, MLX4_EN_MAX_XDP_MTU);
2375 static int mlx4_en_change_mtu(struct net_device *dev, int new_mtu)
2377 struct mlx4_en_priv *priv = netdev_priv(dev);
2378 struct mlx4_en_dev *mdev = priv->mdev;
2381 en_dbg(DRV, priv, "Change MTU called - current:%d new:%d\n",
2384 if (priv->tx_ring_num[TX_XDP] &&
2385 !mlx4_en_check_xdp_mtu(dev, new_mtu))
2390 if (netif_running(dev)) {
2391 mutex_lock(&mdev->state_lock);
2392 if (!mdev->device_up) {
2393 /* NIC is probably restarting - let watchdog task reset
2395 en_dbg(DRV, priv, "Change MTU called with card down!?\n");
2397 mlx4_en_stop_port(dev, 1);
2398 err = mlx4_en_start_port(dev);
2400 en_err(priv, "Failed restarting port:%d\n",
2402 queue_work(mdev->workqueue, &priv->watchdog_task);
2405 mutex_unlock(&mdev->state_lock);
2410 static int mlx4_en_hwtstamp_set(struct net_device *dev, struct ifreq *ifr)
2412 struct mlx4_en_priv *priv = netdev_priv(dev);
2413 struct mlx4_en_dev *mdev = priv->mdev;
2414 struct hwtstamp_config config;
2416 if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
2419 /* reserved for future extensions */
2423 /* device doesn't support time stamping */
2424 if (!(mdev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_TS))
2427 /* TX HW timestamp */
2428 switch (config.tx_type) {
2429 case HWTSTAMP_TX_OFF:
2430 case HWTSTAMP_TX_ON:
2436 /* RX HW timestamp */
2437 switch (config.rx_filter) {
2438 case HWTSTAMP_FILTER_NONE:
2440 case HWTSTAMP_FILTER_ALL:
2441 case HWTSTAMP_FILTER_SOME:
2442 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
2443 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
2444 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
2445 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
2446 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
2447 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
2448 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
2449 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
2450 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
2451 case HWTSTAMP_FILTER_PTP_V2_EVENT:
2452 case HWTSTAMP_FILTER_PTP_V2_SYNC:
2453 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
2454 case HWTSTAMP_FILTER_NTP_ALL:
2455 config.rx_filter = HWTSTAMP_FILTER_ALL;
2461 if (mlx4_en_reset_config(dev, config, dev->features)) {
2462 config.tx_type = HWTSTAMP_TX_OFF;
2463 config.rx_filter = HWTSTAMP_FILTER_NONE;
2466 return copy_to_user(ifr->ifr_data, &config,
2467 sizeof(config)) ? -EFAULT : 0;
2470 static int mlx4_en_hwtstamp_get(struct net_device *dev, struct ifreq *ifr)
2472 struct mlx4_en_priv *priv = netdev_priv(dev);
2474 return copy_to_user(ifr->ifr_data, &priv->hwtstamp_config,
2475 sizeof(priv->hwtstamp_config)) ? -EFAULT : 0;
2478 static int mlx4_en_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
2482 return mlx4_en_hwtstamp_set(dev, ifr);
2484 return mlx4_en_hwtstamp_get(dev, ifr);
2490 static netdev_features_t mlx4_en_fix_features(struct net_device *netdev,
2491 netdev_features_t features)
2493 struct mlx4_en_priv *en_priv = netdev_priv(netdev);
2494 struct mlx4_en_dev *mdev = en_priv->mdev;
2496 /* Since there is no support for separate RX C-TAG/S-TAG vlan accel
2497 * enable/disable make sure S-TAG flag is always in same state as
2500 if (features & NETIF_F_HW_VLAN_CTAG_RX &&
2501 !(mdev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_SKIP_OUTER_VLAN))
2502 features |= NETIF_F_HW_VLAN_STAG_RX;
2504 features &= ~NETIF_F_HW_VLAN_STAG_RX;
2509 static int mlx4_en_set_features(struct net_device *netdev,
2510 netdev_features_t features)
2512 struct mlx4_en_priv *priv = netdev_priv(netdev);
2516 if (DEV_FEATURE_CHANGED(netdev, features, NETIF_F_RXFCS)) {
2517 en_info(priv, "Turn %s RX-FCS\n",
2518 (features & NETIF_F_RXFCS) ? "ON" : "OFF");
2522 if (DEV_FEATURE_CHANGED(netdev, features, NETIF_F_RXALL)) {
2523 u8 ignore_fcs_value = (features & NETIF_F_RXALL) ? 1 : 0;
2525 en_info(priv, "Turn %s RX-ALL\n",
2526 ignore_fcs_value ? "ON" : "OFF");
2527 ret = mlx4_SET_PORT_fcs_check(priv->mdev->dev,
2528 priv->port, ignore_fcs_value);
2533 if (DEV_FEATURE_CHANGED(netdev, features, NETIF_F_HW_VLAN_CTAG_RX)) {
2534 en_info(priv, "Turn %s RX vlan strip offload\n",
2535 (features & NETIF_F_HW_VLAN_CTAG_RX) ? "ON" : "OFF");
2539 if (DEV_FEATURE_CHANGED(netdev, features, NETIF_F_HW_VLAN_CTAG_TX))
2540 en_info(priv, "Turn %s TX vlan strip offload\n",
2541 (features & NETIF_F_HW_VLAN_CTAG_TX) ? "ON" : "OFF");
2543 if (DEV_FEATURE_CHANGED(netdev, features, NETIF_F_HW_VLAN_STAG_TX))
2544 en_info(priv, "Turn %s TX S-VLAN strip offload\n",
2545 (features & NETIF_F_HW_VLAN_STAG_TX) ? "ON" : "OFF");
2547 if (DEV_FEATURE_CHANGED(netdev, features, NETIF_F_LOOPBACK)) {
2548 en_info(priv, "Turn %s loopback\n",
2549 (features & NETIF_F_LOOPBACK) ? "ON" : "OFF");
2550 mlx4_en_update_loopback_state(netdev, features);
2554 ret = mlx4_en_reset_config(netdev, priv->hwtstamp_config,
2563 static int mlx4_en_set_vf_mac(struct net_device *dev, int queue, u8 *mac)
2565 struct mlx4_en_priv *en_priv = netdev_priv(dev);
2566 struct mlx4_en_dev *mdev = en_priv->mdev;
2568 return mlx4_set_vf_mac(mdev->dev, en_priv->port, queue, mac);
2571 static int mlx4_en_set_vf_vlan(struct net_device *dev, int vf, u16 vlan, u8 qos,
2574 struct mlx4_en_priv *en_priv = netdev_priv(dev);
2575 struct mlx4_en_dev *mdev = en_priv->mdev;
2577 return mlx4_set_vf_vlan(mdev->dev, en_priv->port, vf, vlan, qos,
2581 static int mlx4_en_set_vf_rate(struct net_device *dev, int vf, int min_tx_rate,
2584 struct mlx4_en_priv *en_priv = netdev_priv(dev);
2585 struct mlx4_en_dev *mdev = en_priv->mdev;
2587 return mlx4_set_vf_rate(mdev->dev, en_priv->port, vf, min_tx_rate,
2591 static int mlx4_en_set_vf_spoofchk(struct net_device *dev, int vf, bool setting)
2593 struct mlx4_en_priv *en_priv = netdev_priv(dev);
2594 struct mlx4_en_dev *mdev = en_priv->mdev;
2596 return mlx4_set_vf_spoofchk(mdev->dev, en_priv->port, vf, setting);
2599 static int mlx4_en_get_vf_config(struct net_device *dev, int vf, struct ifla_vf_info *ivf)
2601 struct mlx4_en_priv *en_priv = netdev_priv(dev);
2602 struct mlx4_en_dev *mdev = en_priv->mdev;
2604 return mlx4_get_vf_config(mdev->dev, en_priv->port, vf, ivf);
2607 static int mlx4_en_set_vf_link_state(struct net_device *dev, int vf, int link_state)
2609 struct mlx4_en_priv *en_priv = netdev_priv(dev);
2610 struct mlx4_en_dev *mdev = en_priv->mdev;
2612 return mlx4_set_vf_link_state(mdev->dev, en_priv->port, vf, link_state);
2615 static int mlx4_en_get_vf_stats(struct net_device *dev, int vf,
2616 struct ifla_vf_stats *vf_stats)
2618 struct mlx4_en_priv *en_priv = netdev_priv(dev);
2619 struct mlx4_en_dev *mdev = en_priv->mdev;
2621 return mlx4_get_vf_stats(mdev->dev, en_priv->port, vf, vf_stats);
2624 #define PORT_ID_BYTE_LEN 8
2625 static int mlx4_en_get_phys_port_id(struct net_device *dev,
2626 struct netdev_phys_item_id *ppid)
2628 struct mlx4_en_priv *priv = netdev_priv(dev);
2629 struct mlx4_dev *mdev = priv->mdev->dev;
2631 u64 phys_port_id = mdev->caps.phys_port_id[priv->port];
2636 ppid->id_len = sizeof(phys_port_id);
2637 for (i = PORT_ID_BYTE_LEN - 1; i >= 0; --i) {
2638 ppid->id[i] = phys_port_id & 0xff;
2644 static void mlx4_en_add_vxlan_offloads(struct work_struct *work)
2647 struct mlx4_en_priv *priv = container_of(work, struct mlx4_en_priv,
2650 ret = mlx4_config_vxlan_port(priv->mdev->dev, priv->vxlan_port);
2654 ret = mlx4_SET_PORT_VXLAN(priv->mdev->dev, priv->port,
2655 VXLAN_STEER_BY_OUTER_MAC, 1);
2658 en_err(priv, "failed setting L2 tunnel configuration ret %d\n", ret);
2663 priv->dev->hw_enc_features |= NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
2665 NETIF_F_TSO | NETIF_F_TSO6 |
2666 NETIF_F_GSO_UDP_TUNNEL |
2667 NETIF_F_GSO_UDP_TUNNEL_CSUM |
2668 NETIF_F_GSO_PARTIAL;
2671 static void mlx4_en_del_vxlan_offloads(struct work_struct *work)
2674 struct mlx4_en_priv *priv = container_of(work, struct mlx4_en_priv,
2676 /* unset offloads */
2677 priv->dev->hw_enc_features &= ~(NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
2679 NETIF_F_TSO | NETIF_F_TSO6 |
2680 NETIF_F_GSO_UDP_TUNNEL |
2681 NETIF_F_GSO_UDP_TUNNEL_CSUM |
2682 NETIF_F_GSO_PARTIAL);
2684 ret = mlx4_SET_PORT_VXLAN(priv->mdev->dev, priv->port,
2685 VXLAN_STEER_BY_OUTER_MAC, 0);
2687 en_err(priv, "failed setting L2 tunnel configuration ret %d\n", ret);
2689 priv->vxlan_port = 0;
2692 static void mlx4_en_add_vxlan_port(struct net_device *dev,
2693 struct udp_tunnel_info *ti)
2695 struct mlx4_en_priv *priv = netdev_priv(dev);
2696 __be16 port = ti->port;
2697 __be16 current_port;
2699 if (ti->type != UDP_TUNNEL_TYPE_VXLAN)
2702 if (ti->sa_family != AF_INET)
2705 if (priv->mdev->dev->caps.tunnel_offload_mode != MLX4_TUNNEL_OFFLOAD_MODE_VXLAN)
2708 current_port = priv->vxlan_port;
2709 if (current_port && current_port != port) {
2710 en_warn(priv, "vxlan port %d configured, can't add port %d\n",
2711 ntohs(current_port), ntohs(port));
2715 priv->vxlan_port = port;
2716 queue_work(priv->mdev->workqueue, &priv->vxlan_add_task);
2719 static void mlx4_en_del_vxlan_port(struct net_device *dev,
2720 struct udp_tunnel_info *ti)
2722 struct mlx4_en_priv *priv = netdev_priv(dev);
2723 __be16 port = ti->port;
2724 __be16 current_port;
2726 if (ti->type != UDP_TUNNEL_TYPE_VXLAN)
2729 if (ti->sa_family != AF_INET)
2732 if (priv->mdev->dev->caps.tunnel_offload_mode != MLX4_TUNNEL_OFFLOAD_MODE_VXLAN)
2735 current_port = priv->vxlan_port;
2736 if (current_port != port) {
2737 en_dbg(DRV, priv, "vxlan port %d isn't configured, ignoring\n", ntohs(port));
2741 queue_work(priv->mdev->workqueue, &priv->vxlan_del_task);
2744 static netdev_features_t mlx4_en_features_check(struct sk_buff *skb,
2745 struct net_device *dev,
2746 netdev_features_t features)
2748 features = vlan_features_check(skb, features);
2749 features = vxlan_features_check(skb, features);
2751 /* The ConnectX-3 doesn't support outer IPv6 checksums but it does
2752 * support inner IPv6 checksums and segmentation so we need to
2753 * strip that feature if this is an IPv6 encapsulated frame.
2755 if (skb->encapsulation &&
2756 (skb->ip_summed == CHECKSUM_PARTIAL)) {
2757 struct mlx4_en_priv *priv = netdev_priv(dev);
2759 if (!priv->vxlan_port ||
2760 (ip_hdr(skb)->version != 4) ||
2761 (udp_hdr(skb)->dest != priv->vxlan_port))
2762 features &= ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
2768 static int mlx4_en_set_tx_maxrate(struct net_device *dev, int queue_index, u32 maxrate)
2770 struct mlx4_en_priv *priv = netdev_priv(dev);
2771 struct mlx4_en_tx_ring *tx_ring = priv->tx_ring[TX][queue_index];
2772 struct mlx4_update_qp_params params;
2775 if (!(priv->mdev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_QP_RATE_LIMIT))
2778 /* rate provided to us in Mbs, check if it fits into 12 bits, if not use Gbs */
2779 if (maxrate >> 12) {
2780 params.rate_unit = MLX4_QP_RATE_LIMIT_GBS;
2781 params.rate_val = maxrate / 1000;
2782 } else if (maxrate) {
2783 params.rate_unit = MLX4_QP_RATE_LIMIT_MBS;
2784 params.rate_val = maxrate;
2785 } else { /* zero serves to revoke the QP rate-limitation */
2786 params.rate_unit = 0;
2787 params.rate_val = 0;
2790 err = mlx4_update_qp(priv->mdev->dev, tx_ring->qpn, MLX4_UPDATE_QP_RATE_LIMIT,
2795 static int mlx4_xdp_set(struct net_device *dev, struct bpf_prog *prog)
2797 struct mlx4_en_priv *priv = netdev_priv(dev);
2798 struct mlx4_en_dev *mdev = priv->mdev;
2799 struct mlx4_en_port_profile new_prof;
2800 struct bpf_prog *old_prog;
2801 struct mlx4_en_priv *tmp;
2808 xdp_ring_num = prog ? priv->rx_ring_num : 0;
2810 /* No need to reconfigure buffers when simply swapping the
2811 * program for a new one.
2813 if (priv->tx_ring_num[TX_XDP] == xdp_ring_num) {
2815 prog = bpf_prog_add(prog, priv->rx_ring_num - 1);
2817 return PTR_ERR(prog);
2819 mutex_lock(&mdev->state_lock);
2820 for (i = 0; i < priv->rx_ring_num; i++) {
2821 old_prog = rcu_dereference_protected(
2822 priv->rx_ring[i]->xdp_prog,
2823 lockdep_is_held(&mdev->state_lock));
2824 rcu_assign_pointer(priv->rx_ring[i]->xdp_prog, prog);
2826 bpf_prog_put(old_prog);
2828 mutex_unlock(&mdev->state_lock);
2832 if (!mlx4_en_check_xdp_mtu(dev, dev->mtu))
2835 tmp = kzalloc(sizeof(*tmp), GFP_KERNEL);
2840 prog = bpf_prog_add(prog, priv->rx_ring_num - 1);
2842 err = PTR_ERR(prog);
2847 mutex_lock(&mdev->state_lock);
2848 memcpy(&new_prof, priv->prof, sizeof(struct mlx4_en_port_profile));
2849 new_prof.tx_ring_num[TX_XDP] = xdp_ring_num;
2851 if (priv->tx_ring_num[TX] + xdp_ring_num > MAX_TX_RINGS) {
2853 new_prof.tx_ring_num[TX] =
2854 MAX_TX_RINGS - ALIGN(xdp_ring_num, priv->prof->num_up);
2855 en_warn(priv, "Reducing the number of TX rings, to not exceed the max total rings number.\n");
2858 err = mlx4_en_try_alloc_resources(priv, tmp, &new_prof, false);
2861 bpf_prog_sub(prog, priv->rx_ring_num - 1);
2865 if (priv->port_up) {
2867 mlx4_en_stop_port(dev, 1);
2870 mlx4_en_safe_replace_resources(priv, tmp);
2872 netif_set_real_num_tx_queues(dev, priv->tx_ring_num[TX]);
2874 for (i = 0; i < priv->rx_ring_num; i++) {
2875 old_prog = rcu_dereference_protected(
2876 priv->rx_ring[i]->xdp_prog,
2877 lockdep_is_held(&mdev->state_lock));
2878 rcu_assign_pointer(priv->rx_ring[i]->xdp_prog, prog);
2880 bpf_prog_put(old_prog);
2884 err = mlx4_en_start_port(dev);
2886 en_err(priv, "Failed starting port %d for XDP change\n",
2888 queue_work(mdev->workqueue, &priv->watchdog_task);
2893 mutex_unlock(&mdev->state_lock);
2899 static u32 mlx4_xdp_query(struct net_device *dev)
2901 struct mlx4_en_priv *priv = netdev_priv(dev);
2902 struct mlx4_en_dev *mdev = priv->mdev;
2903 const struct bpf_prog *xdp_prog;
2906 if (!priv->tx_ring_num[TX_XDP])
2909 mutex_lock(&mdev->state_lock);
2910 xdp_prog = rcu_dereference_protected(
2911 priv->rx_ring[0]->xdp_prog,
2912 lockdep_is_held(&mdev->state_lock));
2914 prog_id = xdp_prog->aux->id;
2915 mutex_unlock(&mdev->state_lock);
2920 static int mlx4_xdp(struct net_device *dev, struct netdev_bpf *xdp)
2922 switch (xdp->command) {
2923 case XDP_SETUP_PROG:
2924 return mlx4_xdp_set(dev, xdp->prog);
2925 case XDP_QUERY_PROG:
2926 xdp->prog_id = mlx4_xdp_query(dev);
2927 xdp->prog_attached = !!xdp->prog_id;
2934 static const struct net_device_ops mlx4_netdev_ops = {
2935 .ndo_open = mlx4_en_open,
2936 .ndo_stop = mlx4_en_close,
2937 .ndo_start_xmit = mlx4_en_xmit,
2938 .ndo_select_queue = mlx4_en_select_queue,
2939 .ndo_get_stats64 = mlx4_en_get_stats64,
2940 .ndo_set_rx_mode = mlx4_en_set_rx_mode,
2941 .ndo_set_mac_address = mlx4_en_set_mac,
2942 .ndo_validate_addr = eth_validate_addr,
2943 .ndo_change_mtu = mlx4_en_change_mtu,
2944 .ndo_do_ioctl = mlx4_en_ioctl,
2945 .ndo_tx_timeout = mlx4_en_tx_timeout,
2946 .ndo_vlan_rx_add_vid = mlx4_en_vlan_rx_add_vid,
2947 .ndo_vlan_rx_kill_vid = mlx4_en_vlan_rx_kill_vid,
2948 #ifdef CONFIG_NET_POLL_CONTROLLER
2949 .ndo_poll_controller = mlx4_en_netpoll,
2951 .ndo_set_features = mlx4_en_set_features,
2952 .ndo_fix_features = mlx4_en_fix_features,
2953 .ndo_setup_tc = __mlx4_en_setup_tc,
2954 #ifdef CONFIG_RFS_ACCEL
2955 .ndo_rx_flow_steer = mlx4_en_filter_rfs,
2957 .ndo_get_phys_port_id = mlx4_en_get_phys_port_id,
2958 .ndo_udp_tunnel_add = mlx4_en_add_vxlan_port,
2959 .ndo_udp_tunnel_del = mlx4_en_del_vxlan_port,
2960 .ndo_features_check = mlx4_en_features_check,
2961 .ndo_set_tx_maxrate = mlx4_en_set_tx_maxrate,
2962 .ndo_bpf = mlx4_xdp,
2965 static const struct net_device_ops mlx4_netdev_ops_master = {
2966 .ndo_open = mlx4_en_open,
2967 .ndo_stop = mlx4_en_close,
2968 .ndo_start_xmit = mlx4_en_xmit,
2969 .ndo_select_queue = mlx4_en_select_queue,
2970 .ndo_get_stats64 = mlx4_en_get_stats64,
2971 .ndo_set_rx_mode = mlx4_en_set_rx_mode,
2972 .ndo_set_mac_address = mlx4_en_set_mac,
2973 .ndo_validate_addr = eth_validate_addr,
2974 .ndo_change_mtu = mlx4_en_change_mtu,
2975 .ndo_tx_timeout = mlx4_en_tx_timeout,
2976 .ndo_vlan_rx_add_vid = mlx4_en_vlan_rx_add_vid,
2977 .ndo_vlan_rx_kill_vid = mlx4_en_vlan_rx_kill_vid,
2978 .ndo_set_vf_mac = mlx4_en_set_vf_mac,
2979 .ndo_set_vf_vlan = mlx4_en_set_vf_vlan,
2980 .ndo_set_vf_rate = mlx4_en_set_vf_rate,
2981 .ndo_set_vf_spoofchk = mlx4_en_set_vf_spoofchk,
2982 .ndo_set_vf_link_state = mlx4_en_set_vf_link_state,
2983 .ndo_get_vf_stats = mlx4_en_get_vf_stats,
2984 .ndo_get_vf_config = mlx4_en_get_vf_config,
2985 #ifdef CONFIG_NET_POLL_CONTROLLER
2986 .ndo_poll_controller = mlx4_en_netpoll,
2988 .ndo_set_features = mlx4_en_set_features,
2989 .ndo_fix_features = mlx4_en_fix_features,
2990 .ndo_setup_tc = __mlx4_en_setup_tc,
2991 #ifdef CONFIG_RFS_ACCEL
2992 .ndo_rx_flow_steer = mlx4_en_filter_rfs,
2994 .ndo_get_phys_port_id = mlx4_en_get_phys_port_id,
2995 .ndo_udp_tunnel_add = mlx4_en_add_vxlan_port,
2996 .ndo_udp_tunnel_del = mlx4_en_del_vxlan_port,
2997 .ndo_features_check = mlx4_en_features_check,
2998 .ndo_set_tx_maxrate = mlx4_en_set_tx_maxrate,
2999 .ndo_bpf = mlx4_xdp,
3002 struct mlx4_en_bond {
3003 struct work_struct work;
3004 struct mlx4_en_priv *priv;
3006 struct mlx4_port_map port_map;
3009 static void mlx4_en_bond_work(struct work_struct *work)
3011 struct mlx4_en_bond *bond = container_of(work,
3012 struct mlx4_en_bond,
3015 struct mlx4_dev *dev = bond->priv->mdev->dev;
3017 if (bond->is_bonded) {
3018 if (!mlx4_is_bonded(dev)) {
3019 err = mlx4_bond(dev);
3021 en_err(bond->priv, "Fail to bond device\n");
3024 err = mlx4_port_map_set(dev, &bond->port_map);
3026 en_err(bond->priv, "Fail to set port map [%d][%d]: %d\n",
3027 bond->port_map.port1,
3028 bond->port_map.port2,
3031 } else if (mlx4_is_bonded(dev)) {
3032 err = mlx4_unbond(dev);
3034 en_err(bond->priv, "Fail to unbond device\n");
3036 dev_put(bond->priv->dev);
3040 static int mlx4_en_queue_bond_work(struct mlx4_en_priv *priv, int is_bonded,
3041 u8 v2p_p1, u8 v2p_p2)
3043 struct mlx4_en_bond *bond = NULL;
3045 bond = kzalloc(sizeof(*bond), GFP_ATOMIC);
3049 INIT_WORK(&bond->work, mlx4_en_bond_work);
3051 bond->is_bonded = is_bonded;
3052 bond->port_map.port1 = v2p_p1;
3053 bond->port_map.port2 = v2p_p2;
3054 dev_hold(priv->dev);
3055 queue_work(priv->mdev->workqueue, &bond->work);
3059 int mlx4_en_netdev_event(struct notifier_block *this,
3060 unsigned long event, void *ptr)
3062 struct net_device *ndev = netdev_notifier_info_to_dev(ptr);
3064 struct mlx4_en_dev *mdev;
3065 struct mlx4_dev *dev;
3066 int i, num_eth_ports = 0;
3067 bool do_bond = true;
3068 struct mlx4_en_priv *priv;
3072 if (!net_eq(dev_net(ndev), &init_net))
3075 mdev = container_of(this, struct mlx4_en_dev, nb);
3078 /* Go into this mode only when two network devices set on two ports
3079 * of the same mlx4 device are slaves of the same bonding master
3081 mlx4_foreach_port(i, dev, MLX4_PORT_TYPE_ETH) {
3083 if (!port && (mdev->pndev[i] == ndev))
3085 mdev->upper[i] = mdev->pndev[i] ?
3086 netdev_master_upper_dev_get(mdev->pndev[i]) : NULL;
3087 /* condition not met: network device is a slave */
3088 if (!mdev->upper[i])
3090 if (num_eth_ports < 2)
3092 /* condition not met: same master */
3093 if (mdev->upper[i] != mdev->upper[i-1])
3096 /* condition not met: 2 salves */
3097 do_bond = (num_eth_ports == 2) ? do_bond : false;
3099 /* handle only events that come with enough info */
3100 if ((do_bond && (event != NETDEV_BONDING_INFO)) || !port)
3103 priv = netdev_priv(ndev);
3105 struct netdev_notifier_bonding_info *notifier_info = ptr;
3106 struct netdev_bonding_info *bonding_info =
3107 ¬ifier_info->bonding_info;
3109 /* required mode 1, 2 or 4 */
3110 if ((bonding_info->master.bond_mode != BOND_MODE_ACTIVEBACKUP) &&
3111 (bonding_info->master.bond_mode != BOND_MODE_XOR) &&
3112 (bonding_info->master.bond_mode != BOND_MODE_8023AD))
3115 /* require exactly 2 slaves */
3116 if (bonding_info->master.num_slaves != 2)
3121 if (bonding_info->master.bond_mode ==
3122 BOND_MODE_ACTIVEBACKUP) {
3123 /* in active-backup mode virtual ports are
3124 * mapped to the physical port of the active
3126 if (bonding_info->slave.state ==
3127 BOND_STATE_BACKUP) {
3135 } else { /* BOND_STATE_ACTIVE */
3144 } else { /* Active-Active */
3145 /* in active-active mode a virtual port is
3146 * mapped to the native physical port if and only
3147 * if the physical port is up */
3148 __s8 link = bonding_info->slave.link;
3154 if ((link == BOND_LINK_UP) ||
3155 (link == BOND_LINK_FAIL)) {
3160 } else { /* BOND_LINK_DOWN || BOND_LINK_BACK */
3170 mlx4_en_queue_bond_work(priv, do_bond,
3171 v2p_port1, v2p_port2);
3176 void mlx4_en_update_pfc_stats_bitmap(struct mlx4_dev *dev,
3177 struct mlx4_en_stats_bitmap *stats_bitmap,
3178 u8 rx_ppp, u8 rx_pause,
3179 u8 tx_ppp, u8 tx_pause)
3181 int last_i = NUM_MAIN_STATS + NUM_PORT_STATS + NUM_PF_STATS;
3183 if (!mlx4_is_slave(dev) &&
3184 (dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_FLOWSTATS_EN)) {
3185 mutex_lock(&stats_bitmap->mutex);
3186 bitmap_clear(stats_bitmap->bitmap, last_i, NUM_FLOW_STATS);
3189 bitmap_set(stats_bitmap->bitmap, last_i,
3190 NUM_FLOW_PRIORITY_STATS_RX);
3191 last_i += NUM_FLOW_PRIORITY_STATS_RX;
3193 if (rx_pause && !(rx_ppp))
3194 bitmap_set(stats_bitmap->bitmap, last_i,
3196 last_i += NUM_FLOW_STATS_RX;
3199 bitmap_set(stats_bitmap->bitmap, last_i,
3200 NUM_FLOW_PRIORITY_STATS_TX);
3201 last_i += NUM_FLOW_PRIORITY_STATS_TX;
3203 if (tx_pause && !(tx_ppp))
3204 bitmap_set(stats_bitmap->bitmap, last_i,
3206 last_i += NUM_FLOW_STATS_TX;
3208 mutex_unlock(&stats_bitmap->mutex);
3212 void mlx4_en_set_stats_bitmap(struct mlx4_dev *dev,
3213 struct mlx4_en_stats_bitmap *stats_bitmap,
3214 u8 rx_ppp, u8 rx_pause,
3215 u8 tx_ppp, u8 tx_pause)
3219 mutex_init(&stats_bitmap->mutex);
3220 bitmap_zero(stats_bitmap->bitmap, NUM_ALL_STATS);
3222 if (mlx4_is_slave(dev)) {
3223 bitmap_set(stats_bitmap->bitmap, last_i +
3224 MLX4_FIND_NETDEV_STAT(rx_packets), 1);
3225 bitmap_set(stats_bitmap->bitmap, last_i +
3226 MLX4_FIND_NETDEV_STAT(tx_packets), 1);
3227 bitmap_set(stats_bitmap->bitmap, last_i +
3228 MLX4_FIND_NETDEV_STAT(rx_bytes), 1);
3229 bitmap_set(stats_bitmap->bitmap, last_i +
3230 MLX4_FIND_NETDEV_STAT(tx_bytes), 1);
3231 bitmap_set(stats_bitmap->bitmap, last_i +
3232 MLX4_FIND_NETDEV_STAT(rx_dropped), 1);
3233 bitmap_set(stats_bitmap->bitmap, last_i +
3234 MLX4_FIND_NETDEV_STAT(tx_dropped), 1);
3236 bitmap_set(stats_bitmap->bitmap, last_i, NUM_MAIN_STATS);
3238 last_i += NUM_MAIN_STATS;
3240 bitmap_set(stats_bitmap->bitmap, last_i, NUM_PORT_STATS);
3241 last_i += NUM_PORT_STATS;
3243 if (mlx4_is_master(dev))
3244 bitmap_set(stats_bitmap->bitmap, last_i,
3246 last_i += NUM_PF_STATS;
3248 mlx4_en_update_pfc_stats_bitmap(dev, stats_bitmap,
3251 last_i += NUM_FLOW_STATS;
3253 if (!mlx4_is_slave(dev))
3254 bitmap_set(stats_bitmap->bitmap, last_i, NUM_PKT_STATS);
3255 last_i += NUM_PKT_STATS;
3257 bitmap_set(stats_bitmap->bitmap, last_i, NUM_XDP_STATS);
3258 last_i += NUM_XDP_STATS;
3260 if (!mlx4_is_slave(dev))
3261 bitmap_set(stats_bitmap->bitmap, last_i, NUM_PHY_STATS);
3262 last_i += NUM_PHY_STATS;
3265 int mlx4_en_init_netdev(struct mlx4_en_dev *mdev, int port,
3266 struct mlx4_en_port_profile *prof)
3268 struct net_device *dev;
3269 struct mlx4_en_priv *priv;
3273 dev = alloc_etherdev_mqs(sizeof(struct mlx4_en_priv),
3274 MAX_TX_RINGS, MAX_RX_RINGS);
3278 netif_set_real_num_tx_queues(dev, prof->tx_ring_num[TX]);
3279 netif_set_real_num_rx_queues(dev, prof->rx_ring_num);
3281 SET_NETDEV_DEV(dev, &mdev->dev->persist->pdev->dev);
3282 dev->dev_port = port - 1;
3285 * Initialize driver private data
3288 priv = netdev_priv(dev);
3289 memset(priv, 0, sizeof(struct mlx4_en_priv));
3290 priv->counter_index = MLX4_SINK_COUNTER_INDEX(mdev->dev);
3291 spin_lock_init(&priv->stats_lock);
3292 INIT_WORK(&priv->rx_mode_task, mlx4_en_do_set_rx_mode);
3293 INIT_WORK(&priv->watchdog_task, mlx4_en_restart);
3294 INIT_WORK(&priv->linkstate_task, mlx4_en_linkstate);
3295 INIT_DELAYED_WORK(&priv->stats_task, mlx4_en_do_get_stats);
3296 INIT_DELAYED_WORK(&priv->service_task, mlx4_en_service_task);
3297 INIT_WORK(&priv->vxlan_add_task, mlx4_en_add_vxlan_offloads);
3298 INIT_WORK(&priv->vxlan_del_task, mlx4_en_del_vxlan_offloads);
3299 #ifdef CONFIG_RFS_ACCEL
3300 INIT_LIST_HEAD(&priv->filters);
3301 spin_lock_init(&priv->filters_lock);
3306 priv->ddev = &mdev->pdev->dev;
3309 priv->port_up = false;
3310 priv->flags = prof->flags;
3311 priv->pflags = MLX4_EN_PRIV_FLAGS_BLUEFLAME;
3312 priv->ctrl_flags = cpu_to_be32(MLX4_WQE_CTRL_CQ_UPDATE |
3313 MLX4_WQE_CTRL_SOLICITED);
3314 priv->num_tx_rings_p_up = mdev->profile.max_num_tx_rings_p_up;
3315 priv->tx_work_limit = MLX4_EN_DEFAULT_TX_WORK;
3316 netdev_rss_key_fill(priv->rss_key, sizeof(priv->rss_key));
3318 for (t = 0; t < MLX4_EN_NUM_TX_TYPES; t++) {
3319 priv->tx_ring_num[t] = prof->tx_ring_num[t];
3320 if (!priv->tx_ring_num[t])
3323 priv->tx_ring[t] = kzalloc(sizeof(struct mlx4_en_tx_ring *) *
3324 MAX_TX_RINGS, GFP_KERNEL);
3325 if (!priv->tx_ring[t]) {
3329 priv->tx_cq[t] = kzalloc(sizeof(struct mlx4_en_cq *) *
3330 MAX_TX_RINGS, GFP_KERNEL);
3331 if (!priv->tx_cq[t]) {
3336 priv->rx_ring_num = prof->rx_ring_num;
3337 priv->cqe_factor = (mdev->dev->caps.cqe_size == 64) ? 1 : 0;
3338 priv->cqe_size = mdev->dev->caps.cqe_size;
3339 priv->mac_index = -1;
3340 priv->msg_enable = MLX4_EN_MSG_LEVEL;
3341 #ifdef CONFIG_MLX4_EN_DCB
3342 if (!mlx4_is_slave(priv->mdev->dev)) {
3345 for (prio = 0; prio < IEEE_8021QAZ_MAX_TCS; ++prio) {
3346 priv->ets.prio_tc[prio] = prio;
3347 priv->ets.tc_tsa[prio] = IEEE_8021QAZ_TSA_VENDOR;
3350 priv->dcbx_cap = DCB_CAP_DCBX_VER_CEE | DCB_CAP_DCBX_HOST |
3351 DCB_CAP_DCBX_VER_IEEE;
3352 priv->flags |= MLX4_EN_DCB_ENABLED;
3353 priv->cee_config.pfc_state = false;
3355 for (i = 0; i < MLX4_EN_NUM_UP_HIGH; i++)
3356 priv->cee_config.dcb_pfc[i] = pfc_disabled;
3358 if (mdev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_ETS_CFG) {
3359 dev->dcbnl_ops = &mlx4_en_dcbnl_ops;
3361 en_info(priv, "enabling only PFC DCB ops\n");
3362 dev->dcbnl_ops = &mlx4_en_dcbnl_pfc_ops;
3367 for (i = 0; i < MLX4_EN_MAC_HASH_SIZE; ++i)
3368 INIT_HLIST_HEAD(&priv->mac_hash[i]);
3370 /* Query for default mac and max mtu */
3371 priv->max_mtu = mdev->dev->caps.eth_mtu_cap[priv->port];
3373 if (mdev->dev->caps.rx_checksum_flags_port[priv->port] &
3374 MLX4_RX_CSUM_MODE_VAL_NON_TCP_UDP)
3375 priv->flags |= MLX4_EN_FLAG_RX_CSUM_NON_TCP_UDP;
3377 /* Set default MAC */
3378 dev->addr_len = ETH_ALEN;
3379 mlx4_en_u64_to_mac(dev->dev_addr, mdev->dev->caps.def_mac[priv->port]);
3380 if (!is_valid_ether_addr(dev->dev_addr)) {
3381 en_err(priv, "Port: %d, invalid mac burned: %pM, quiting\n",
3382 priv->port, dev->dev_addr);
3385 } else if (mlx4_is_slave(priv->mdev->dev) &&
3386 (priv->mdev->dev->port_random_macs & 1 << priv->port)) {
3387 /* Random MAC was assigned in mlx4_slave_cap
3388 * in mlx4_core module
3390 dev->addr_assign_type |= NET_ADDR_RANDOM;
3391 en_warn(priv, "Assigned random MAC address %pM\n", dev->dev_addr);
3394 memcpy(priv->current_mac, dev->dev_addr, sizeof(priv->current_mac));
3396 priv->stride = roundup_pow_of_two(sizeof(struct mlx4_en_rx_desc) +
3397 DS_SIZE * MLX4_EN_MAX_RX_FRAGS);
3398 err = mlx4_en_alloc_resources(priv);
3402 /* Initialize time stamping config */
3403 priv->hwtstamp_config.flags = 0;
3404 priv->hwtstamp_config.tx_type = HWTSTAMP_TX_OFF;
3405 priv->hwtstamp_config.rx_filter = HWTSTAMP_FILTER_NONE;
3407 /* Allocate page for receive rings */
3408 err = mlx4_alloc_hwq_res(mdev->dev, &priv->res,
3411 en_err(priv, "Failed to allocate page for rx qps\n");
3414 priv->allocated = 1;
3417 * Initialize netdev entry points
3419 if (mlx4_is_master(priv->mdev->dev))
3420 dev->netdev_ops = &mlx4_netdev_ops_master;
3422 dev->netdev_ops = &mlx4_netdev_ops;
3423 dev->watchdog_timeo = MLX4_EN_WATCHDOG_TIMEOUT;
3424 netif_set_real_num_tx_queues(dev, priv->tx_ring_num[TX]);
3425 netif_set_real_num_rx_queues(dev, priv->rx_ring_num);
3427 dev->ethtool_ops = &mlx4_en_ethtool_ops;
3430 * Set driver features
3432 dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM;
3433 if (mdev->LSO_support)
3434 dev->hw_features |= NETIF_F_TSO | NETIF_F_TSO6;
3436 dev->vlan_features = dev->hw_features;
3438 dev->hw_features |= NETIF_F_RXCSUM | NETIF_F_RXHASH;
3439 dev->features = dev->hw_features | NETIF_F_HIGHDMA |
3440 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX |
3441 NETIF_F_HW_VLAN_CTAG_FILTER;
3442 dev->hw_features |= NETIF_F_LOOPBACK |
3443 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX;
3445 if (!(mdev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_SKIP_OUTER_VLAN)) {
3446 dev->features |= NETIF_F_HW_VLAN_STAG_RX |
3447 NETIF_F_HW_VLAN_STAG_FILTER;
3448 dev->hw_features |= NETIF_F_HW_VLAN_STAG_RX;
3451 if (mlx4_is_slave(mdev->dev)) {
3452 bool vlan_offload_disabled;
3455 err = get_phv_bit(mdev->dev, port, &phv);
3457 dev->hw_features |= NETIF_F_HW_VLAN_STAG_TX;
3458 priv->pflags |= MLX4_EN_PRIV_FLAGS_PHV;
3460 err = mlx4_get_is_vlan_offload_disabled(mdev->dev, port,
3461 &vlan_offload_disabled);
3462 if (!err && vlan_offload_disabled) {
3463 dev->hw_features &= ~(NETIF_F_HW_VLAN_CTAG_TX |
3464 NETIF_F_HW_VLAN_CTAG_RX |
3465 NETIF_F_HW_VLAN_STAG_TX |
3466 NETIF_F_HW_VLAN_STAG_RX);
3467 dev->features &= ~(NETIF_F_HW_VLAN_CTAG_TX |
3468 NETIF_F_HW_VLAN_CTAG_RX |
3469 NETIF_F_HW_VLAN_STAG_TX |
3470 NETIF_F_HW_VLAN_STAG_RX);
3473 if (mdev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_PHV_EN &&
3474 !(mdev->dev->caps.flags2 &
3475 MLX4_DEV_CAP_FLAG2_SKIP_OUTER_VLAN))
3476 dev->hw_features |= NETIF_F_HW_VLAN_STAG_TX;
3479 if (mdev->dev->caps.flags & MLX4_DEV_CAP_FLAG_FCS_KEEP)
3480 dev->hw_features |= NETIF_F_RXFCS;
3482 if (mdev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_IGNORE_FCS)
3483 dev->hw_features |= NETIF_F_RXALL;
3485 if (mdev->dev->caps.steering_mode ==
3486 MLX4_STEERING_MODE_DEVICE_MANAGED &&
3487 mdev->dev->caps.dmfs_high_steer_mode != MLX4_STEERING_DMFS_A0_STATIC)
3488 dev->hw_features |= NETIF_F_NTUPLE;
3490 if (mdev->dev->caps.steering_mode != MLX4_STEERING_MODE_A0)
3491 dev->priv_flags |= IFF_UNICAST_FLT;
3493 /* Setting a default hash function value */
3494 if (mdev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_RSS_TOP) {
3495 priv->rss_hash_fn = ETH_RSS_HASH_TOP;
3496 } else if (mdev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_RSS_XOR) {
3497 priv->rss_hash_fn = ETH_RSS_HASH_XOR;
3500 "No RSS hash capabilities exposed, using Toeplitz\n");
3501 priv->rss_hash_fn = ETH_RSS_HASH_TOP;
3504 if (mdev->dev->caps.tunnel_offload_mode == MLX4_TUNNEL_OFFLOAD_MODE_VXLAN) {
3505 dev->hw_features |= NETIF_F_GSO_UDP_TUNNEL |
3506 NETIF_F_GSO_UDP_TUNNEL_CSUM |
3507 NETIF_F_GSO_PARTIAL;
3508 dev->features |= NETIF_F_GSO_UDP_TUNNEL |
3509 NETIF_F_GSO_UDP_TUNNEL_CSUM |
3510 NETIF_F_GSO_PARTIAL;
3511 dev->gso_partial_features = NETIF_F_GSO_UDP_TUNNEL_CSUM;
3514 /* MTU range: 46 - hw-specific max */
3515 dev->min_mtu = MLX4_EN_MIN_MTU;
3516 dev->max_mtu = priv->max_mtu;
3518 mdev->pndev[port] = dev;
3519 mdev->upper[port] = NULL;
3521 netif_carrier_off(dev);
3522 mlx4_en_set_default_moderation(priv);
3524 en_warn(priv, "Using %d TX rings\n", prof->tx_ring_num[TX]);
3525 en_warn(priv, "Using %d RX rings\n", prof->rx_ring_num);
3527 mlx4_en_update_loopback_state(priv->dev, priv->dev->features);
3529 /* Configure port */
3530 mlx4_en_calc_rx_buf(dev);
3531 err = mlx4_SET_PORT_general(mdev->dev, priv->port,
3532 priv->rx_skb_size + ETH_FCS_LEN,
3533 prof->tx_pause, prof->tx_ppp,
3534 prof->rx_pause, prof->rx_ppp);
3536 en_err(priv, "Failed setting port general configurations for port %d, with error %d\n",
3541 if (mdev->dev->caps.tunnel_offload_mode == MLX4_TUNNEL_OFFLOAD_MODE_VXLAN) {
3542 err = mlx4_SET_PORT_VXLAN(mdev->dev, priv->port, VXLAN_STEER_BY_OUTER_MAC, 1);
3544 en_err(priv, "Failed setting port L2 tunnel configuration, err %d\n",
3551 en_warn(priv, "Initializing port\n");
3552 err = mlx4_INIT_PORT(mdev->dev, priv->port);
3554 en_err(priv, "Failed Initializing port\n");
3557 queue_delayed_work(mdev->workqueue, &priv->stats_task, STATS_DELAY);
3559 /* Initialize time stamp mechanism */
3560 if (mdev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_TS)
3561 mlx4_en_init_timestamp(mdev);
3563 queue_delayed_work(mdev->workqueue, &priv->service_task,
3564 SERVICE_TASK_DELAY);
3566 mlx4_en_set_stats_bitmap(mdev->dev, &priv->stats_bitmap,
3567 mdev->profile.prof[priv->port].rx_ppp,
3568 mdev->profile.prof[priv->port].rx_pause,
3569 mdev->profile.prof[priv->port].tx_ppp,
3570 mdev->profile.prof[priv->port].tx_pause);
3572 err = register_netdev(dev);
3574 en_err(priv, "Netdev registration failed for port %d\n", port);
3578 priv->registered = 1;
3579 devlink_port_type_eth_set(mlx4_get_devlink_port(mdev->dev, priv->port),
3585 mlx4_en_destroy_netdev(dev);
3589 int mlx4_en_reset_config(struct net_device *dev,
3590 struct hwtstamp_config ts_config,
3591 netdev_features_t features)
3593 struct mlx4_en_priv *priv = netdev_priv(dev);
3594 struct mlx4_en_dev *mdev = priv->mdev;
3595 struct mlx4_en_port_profile new_prof;
3596 struct mlx4_en_priv *tmp;
3600 if (priv->hwtstamp_config.tx_type == ts_config.tx_type &&
3601 priv->hwtstamp_config.rx_filter == ts_config.rx_filter &&
3602 !DEV_FEATURE_CHANGED(dev, features, NETIF_F_HW_VLAN_CTAG_RX) &&
3603 !DEV_FEATURE_CHANGED(dev, features, NETIF_F_RXFCS))
3604 return 0; /* Nothing to change */
3606 if (DEV_FEATURE_CHANGED(dev, features, NETIF_F_HW_VLAN_CTAG_RX) &&
3607 (features & NETIF_F_HW_VLAN_CTAG_RX) &&
3608 (priv->hwtstamp_config.rx_filter != HWTSTAMP_FILTER_NONE)) {
3609 en_warn(priv, "Can't turn ON rx vlan offload while time-stamping rx filter is ON\n");
3613 tmp = kzalloc(sizeof(*tmp), GFP_KERNEL);
3617 mutex_lock(&mdev->state_lock);
3619 memcpy(&new_prof, priv->prof, sizeof(struct mlx4_en_port_profile));
3620 memcpy(&new_prof.hwtstamp_config, &ts_config, sizeof(ts_config));
3622 err = mlx4_en_try_alloc_resources(priv, tmp, &new_prof, true);
3626 if (priv->port_up) {
3628 mlx4_en_stop_port(dev, 1);
3631 mlx4_en_safe_replace_resources(priv, tmp);
3633 if (DEV_FEATURE_CHANGED(dev, features, NETIF_F_HW_VLAN_CTAG_RX)) {
3634 if (features & NETIF_F_HW_VLAN_CTAG_RX)
3635 dev->features |= NETIF_F_HW_VLAN_CTAG_RX;
3637 dev->features &= ~NETIF_F_HW_VLAN_CTAG_RX;
3638 } else if (ts_config.rx_filter == HWTSTAMP_FILTER_NONE) {
3639 /* RX time-stamping is OFF, update the RX vlan offload
3640 * to the latest wanted state
3642 if (dev->wanted_features & NETIF_F_HW_VLAN_CTAG_RX)
3643 dev->features |= NETIF_F_HW_VLAN_CTAG_RX;
3645 dev->features &= ~NETIF_F_HW_VLAN_CTAG_RX;
3648 if (DEV_FEATURE_CHANGED(dev, features, NETIF_F_RXFCS)) {
3649 if (features & NETIF_F_RXFCS)
3650 dev->features |= NETIF_F_RXFCS;
3652 dev->features &= ~NETIF_F_RXFCS;
3655 /* RX vlan offload and RX time-stamping can't co-exist !
3656 * Regardless of the caller's choice,
3657 * Turn Off RX vlan offload in case of time-stamping is ON
3659 if (ts_config.rx_filter != HWTSTAMP_FILTER_NONE) {
3660 if (dev->features & NETIF_F_HW_VLAN_CTAG_RX)
3661 en_warn(priv, "Turning off RX vlan offload since RX time-stamping is ON\n");
3662 dev->features &= ~NETIF_F_HW_VLAN_CTAG_RX;
3666 err = mlx4_en_start_port(dev);
3668 en_err(priv, "Failed starting port\n");
3672 mutex_unlock(&mdev->state_lock);
3675 netdev_features_change(dev);