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 int mlx4_en_setup_tc(struct net_device *dev, u8 up)
56 struct mlx4_en_priv *priv = netdev_priv(dev);
58 unsigned int offset = 0;
60 if (up && up != MLX4_EN_NUM_UP)
63 netdev_set_num_tc(dev, up);
65 /* Partition Tx queues evenly amongst UP's */
66 for (i = 0; i < up; i++) {
67 netdev_set_tc_queue(dev, i, priv->num_tx_rings_p_up, offset);
68 offset += priv->num_tx_rings_p_up;
71 #ifdef CONFIG_MLX4_EN_DCB
72 if (!mlx4_is_slave(priv->mdev->dev)) {
74 priv->flags |= MLX4_EN_FLAG_DCB_ENABLED;
76 priv->flags &= ~MLX4_EN_FLAG_DCB_ENABLED;
77 priv->cee_params.dcb_cfg.pfc_state = false;
80 #endif /* CONFIG_MLX4_EN_DCB */
85 static int __mlx4_en_setup_tc(struct net_device *dev, u32 handle, __be16 proto,
86 struct tc_to_netdev *tc)
88 if (tc->type != TC_SETUP_MQPRIO)
91 return mlx4_en_setup_tc(dev, tc->tc);
94 #ifdef CONFIG_RFS_ACCEL
96 struct mlx4_en_filter {
97 struct list_head next;
98 struct work_struct work;
107 struct mlx4_en_priv *priv;
108 u32 flow_id; /* RFS infrastructure id */
109 int id; /* mlx4_en driver id */
110 u64 reg_id; /* Flow steering API id */
111 u8 activated; /* Used to prevent expiry before filter
114 struct hlist_node filter_chain;
117 static void mlx4_en_filter_rfs_expire(struct mlx4_en_priv *priv);
119 static enum mlx4_net_trans_rule_id mlx4_ip_proto_to_trans_rule_id(u8 ip_proto)
123 return MLX4_NET_TRANS_RULE_ID_UDP;
125 return MLX4_NET_TRANS_RULE_ID_TCP;
127 return MLX4_NET_TRANS_RULE_NUM;
131 static void mlx4_en_filter_work(struct work_struct *work)
133 struct mlx4_en_filter *filter = container_of(work,
134 struct mlx4_en_filter,
136 struct mlx4_en_priv *priv = filter->priv;
137 struct mlx4_spec_list spec_tcp_udp = {
138 .id = mlx4_ip_proto_to_trans_rule_id(filter->ip_proto),
141 .dst_port = filter->dst_port,
142 .dst_port_msk = (__force __be16)-1,
143 .src_port = filter->src_port,
144 .src_port_msk = (__force __be16)-1,
148 struct mlx4_spec_list spec_ip = {
149 .id = MLX4_NET_TRANS_RULE_ID_IPV4,
152 .dst_ip = filter->dst_ip,
153 .dst_ip_msk = (__force __be32)-1,
154 .src_ip = filter->src_ip,
155 .src_ip_msk = (__force __be32)-1,
159 struct mlx4_spec_list spec_eth = {
160 .id = MLX4_NET_TRANS_RULE_ID_ETH,
162 struct mlx4_net_trans_rule rule = {
163 .list = LIST_HEAD_INIT(rule.list),
164 .queue_mode = MLX4_NET_TRANS_Q_LIFO,
167 .promisc_mode = MLX4_FS_REGULAR,
169 .priority = MLX4_DOMAIN_RFS,
172 __be64 mac_mask = cpu_to_be64(MLX4_MAC_MASK << 16);
174 if (spec_tcp_udp.id >= MLX4_NET_TRANS_RULE_NUM) {
175 en_warn(priv, "RFS: ignoring unsupported ip protocol (%d)\n",
179 list_add_tail(&spec_eth.list, &rule.list);
180 list_add_tail(&spec_ip.list, &rule.list);
181 list_add_tail(&spec_tcp_udp.list, &rule.list);
183 rule.qpn = priv->rss_map.qps[filter->rxq_index].qpn;
184 memcpy(spec_eth.eth.dst_mac, priv->dev->dev_addr, ETH_ALEN);
185 memcpy(spec_eth.eth.dst_mac_msk, &mac_mask, ETH_ALEN);
187 filter->activated = 0;
189 if (filter->reg_id) {
190 rc = mlx4_flow_detach(priv->mdev->dev, filter->reg_id);
191 if (rc && rc != -ENOENT)
192 en_err(priv, "Error detaching flow. rc = %d\n", rc);
195 rc = mlx4_flow_attach(priv->mdev->dev, &rule, &filter->reg_id);
197 en_err(priv, "Error attaching flow. err = %d\n", rc);
200 mlx4_en_filter_rfs_expire(priv);
202 filter->activated = 1;
205 static inline struct hlist_head *
206 filter_hash_bucket(struct mlx4_en_priv *priv, __be32 src_ip, __be32 dst_ip,
207 __be16 src_port, __be16 dst_port)
212 l = (__force unsigned long)src_port |
213 ((__force unsigned long)dst_port << 2);
214 l ^= (__force unsigned long)(src_ip ^ dst_ip);
216 bucket_idx = hash_long(l, MLX4_EN_FILTER_HASH_SHIFT);
218 return &priv->filter_hash[bucket_idx];
221 static struct mlx4_en_filter *
222 mlx4_en_filter_alloc(struct mlx4_en_priv *priv, int rxq_index, __be32 src_ip,
223 __be32 dst_ip, u8 ip_proto, __be16 src_port,
224 __be16 dst_port, u32 flow_id)
226 struct mlx4_en_filter *filter = NULL;
228 filter = kzalloc(sizeof(struct mlx4_en_filter), GFP_ATOMIC);
233 filter->rxq_index = rxq_index;
234 INIT_WORK(&filter->work, mlx4_en_filter_work);
236 filter->src_ip = src_ip;
237 filter->dst_ip = dst_ip;
238 filter->ip_proto = ip_proto;
239 filter->src_port = src_port;
240 filter->dst_port = dst_port;
242 filter->flow_id = flow_id;
244 filter->id = priv->last_filter_id++ % RPS_NO_FILTER;
246 list_add_tail(&filter->next, &priv->filters);
247 hlist_add_head(&filter->filter_chain,
248 filter_hash_bucket(priv, src_ip, dst_ip, src_port,
254 static void mlx4_en_filter_free(struct mlx4_en_filter *filter)
256 struct mlx4_en_priv *priv = filter->priv;
259 list_del(&filter->next);
261 rc = mlx4_flow_detach(priv->mdev->dev, filter->reg_id);
262 if (rc && rc != -ENOENT)
263 en_err(priv, "Error detaching flow. rc = %d\n", rc);
268 static inline struct mlx4_en_filter *
269 mlx4_en_filter_find(struct mlx4_en_priv *priv, __be32 src_ip, __be32 dst_ip,
270 u8 ip_proto, __be16 src_port, __be16 dst_port)
272 struct mlx4_en_filter *filter;
273 struct mlx4_en_filter *ret = NULL;
275 hlist_for_each_entry(filter,
276 filter_hash_bucket(priv, src_ip, dst_ip,
279 if (filter->src_ip == src_ip &&
280 filter->dst_ip == dst_ip &&
281 filter->ip_proto == ip_proto &&
282 filter->src_port == src_port &&
283 filter->dst_port == dst_port) {
293 mlx4_en_filter_rfs(struct net_device *net_dev, const struct sk_buff *skb,
294 u16 rxq_index, u32 flow_id)
296 struct mlx4_en_priv *priv = netdev_priv(net_dev);
297 struct mlx4_en_filter *filter;
298 const struct iphdr *ip;
305 int nhoff = skb_network_offset(skb);
308 if (skb->protocol != htons(ETH_P_IP))
309 return -EPROTONOSUPPORT;
311 ip = (const struct iphdr *)(skb->data + nhoff);
312 if (ip_is_fragment(ip))
313 return -EPROTONOSUPPORT;
315 if ((ip->protocol != IPPROTO_TCP) && (ip->protocol != IPPROTO_UDP))
316 return -EPROTONOSUPPORT;
317 ports = (const __be16 *)(skb->data + nhoff + 4 * ip->ihl);
319 ip_proto = ip->protocol;
325 spin_lock_bh(&priv->filters_lock);
326 filter = mlx4_en_filter_find(priv, src_ip, dst_ip, ip_proto,
329 if (filter->rxq_index == rxq_index)
332 filter->rxq_index = rxq_index;
334 filter = mlx4_en_filter_alloc(priv, rxq_index,
335 src_ip, dst_ip, ip_proto,
336 src_port, dst_port, flow_id);
343 queue_work(priv->mdev->workqueue, &filter->work);
348 spin_unlock_bh(&priv->filters_lock);
353 void mlx4_en_cleanup_filters(struct mlx4_en_priv *priv)
355 struct mlx4_en_filter *filter, *tmp;
358 spin_lock_bh(&priv->filters_lock);
359 list_for_each_entry_safe(filter, tmp, &priv->filters, next) {
360 list_move(&filter->next, &del_list);
361 hlist_del(&filter->filter_chain);
363 spin_unlock_bh(&priv->filters_lock);
365 list_for_each_entry_safe(filter, tmp, &del_list, next) {
366 cancel_work_sync(&filter->work);
367 mlx4_en_filter_free(filter);
371 static void mlx4_en_filter_rfs_expire(struct mlx4_en_priv *priv)
373 struct mlx4_en_filter *filter = NULL, *tmp, *last_filter = NULL;
377 spin_lock_bh(&priv->filters_lock);
378 list_for_each_entry_safe(filter, tmp, &priv->filters, next) {
379 if (i > MLX4_EN_FILTER_EXPIRY_QUOTA)
382 if (filter->activated &&
383 !work_pending(&filter->work) &&
384 rps_may_expire_flow(priv->dev,
385 filter->rxq_index, filter->flow_id,
387 list_move(&filter->next, &del_list);
388 hlist_del(&filter->filter_chain);
390 last_filter = filter;
395 if (last_filter && (&last_filter->next != priv->filters.next))
396 list_move(&priv->filters, &last_filter->next);
398 spin_unlock_bh(&priv->filters_lock);
400 list_for_each_entry_safe(filter, tmp, &del_list, next)
401 mlx4_en_filter_free(filter);
405 static int mlx4_en_vlan_rx_add_vid(struct net_device *dev,
406 __be16 proto, u16 vid)
408 struct mlx4_en_priv *priv = netdev_priv(dev);
409 struct mlx4_en_dev *mdev = priv->mdev;
413 en_dbg(HW, priv, "adding VLAN:%d\n", vid);
415 set_bit(vid, priv->active_vlans);
417 /* Add VID to port VLAN filter */
418 mutex_lock(&mdev->state_lock);
419 if (mdev->device_up && priv->port_up) {
420 err = mlx4_SET_VLAN_FLTR(mdev->dev, priv);
422 en_err(priv, "Failed configuring VLAN filter\n");
426 err = mlx4_register_vlan(mdev->dev, priv->port, vid, &idx);
428 en_dbg(HW, priv, "Failed adding vlan %d\n", vid);
431 mutex_unlock(&mdev->state_lock);
435 static int mlx4_en_vlan_rx_kill_vid(struct net_device *dev,
436 __be16 proto, u16 vid)
438 struct mlx4_en_priv *priv = netdev_priv(dev);
439 struct mlx4_en_dev *mdev = priv->mdev;
442 en_dbg(HW, priv, "Killing VID:%d\n", vid);
444 clear_bit(vid, priv->active_vlans);
446 /* Remove VID from port VLAN filter */
447 mutex_lock(&mdev->state_lock);
448 mlx4_unregister_vlan(mdev->dev, priv->port, vid);
450 if (mdev->device_up && priv->port_up) {
451 err = mlx4_SET_VLAN_FLTR(mdev->dev, priv);
453 en_err(priv, "Failed configuring VLAN filter\n");
455 mutex_unlock(&mdev->state_lock);
460 static void mlx4_en_u64_to_mac(unsigned char dst_mac[ETH_ALEN + 2], u64 src_mac)
463 for (i = ETH_ALEN - 1; i >= 0; --i) {
464 dst_mac[i] = src_mac & 0xff;
467 memset(&dst_mac[ETH_ALEN], 0, 2);
471 static int mlx4_en_tunnel_steer_add(struct mlx4_en_priv *priv, unsigned char *addr,
472 int qpn, u64 *reg_id)
476 if (priv->mdev->dev->caps.tunnel_offload_mode != MLX4_TUNNEL_OFFLOAD_MODE_VXLAN ||
477 priv->mdev->dev->caps.dmfs_high_steer_mode == MLX4_STEERING_DMFS_A0_STATIC)
478 return 0; /* do nothing */
480 err = mlx4_tunnel_steer_add(priv->mdev->dev, addr, priv->port, qpn,
481 MLX4_DOMAIN_NIC, reg_id);
483 en_err(priv, "failed to add vxlan steering rule, err %d\n", err);
486 en_dbg(DRV, priv, "added vxlan steering rule, mac %pM reg_id %llx\n", addr, *reg_id);
491 static int mlx4_en_uc_steer_add(struct mlx4_en_priv *priv,
492 unsigned char *mac, int *qpn, u64 *reg_id)
494 struct mlx4_en_dev *mdev = priv->mdev;
495 struct mlx4_dev *dev = mdev->dev;
498 switch (dev->caps.steering_mode) {
499 case MLX4_STEERING_MODE_B0: {
504 memcpy(&gid[10], mac, ETH_ALEN);
507 err = mlx4_unicast_attach(dev, &qp, gid, 0, MLX4_PROT_ETH);
510 case MLX4_STEERING_MODE_DEVICE_MANAGED: {
511 struct mlx4_spec_list spec_eth = { {NULL} };
512 __be64 mac_mask = cpu_to_be64(MLX4_MAC_MASK << 16);
514 struct mlx4_net_trans_rule rule = {
515 .queue_mode = MLX4_NET_TRANS_Q_FIFO,
518 .promisc_mode = MLX4_FS_REGULAR,
519 .priority = MLX4_DOMAIN_NIC,
522 rule.port = priv->port;
524 INIT_LIST_HEAD(&rule.list);
526 spec_eth.id = MLX4_NET_TRANS_RULE_ID_ETH;
527 memcpy(spec_eth.eth.dst_mac, mac, ETH_ALEN);
528 memcpy(spec_eth.eth.dst_mac_msk, &mac_mask, ETH_ALEN);
529 list_add_tail(&spec_eth.list, &rule.list);
531 err = mlx4_flow_attach(dev, &rule, reg_id);
538 en_warn(priv, "Failed Attaching Unicast\n");
543 static void mlx4_en_uc_steer_release(struct mlx4_en_priv *priv,
544 unsigned char *mac, int qpn, u64 reg_id)
546 struct mlx4_en_dev *mdev = priv->mdev;
547 struct mlx4_dev *dev = mdev->dev;
549 switch (dev->caps.steering_mode) {
550 case MLX4_STEERING_MODE_B0: {
555 memcpy(&gid[10], mac, ETH_ALEN);
558 mlx4_unicast_detach(dev, &qp, gid, MLX4_PROT_ETH);
561 case MLX4_STEERING_MODE_DEVICE_MANAGED: {
562 mlx4_flow_detach(dev, reg_id);
566 en_err(priv, "Invalid steering mode.\n");
570 static int mlx4_en_get_qp(struct mlx4_en_priv *priv)
572 struct mlx4_en_dev *mdev = priv->mdev;
573 struct mlx4_dev *dev = mdev->dev;
576 int *qpn = &priv->base_qpn;
577 u64 mac = mlx4_mac_to_u64(priv->dev->dev_addr);
579 en_dbg(DRV, priv, "Registering MAC: %pM for adding\n",
580 priv->dev->dev_addr);
581 index = mlx4_register_mac(dev, priv->port, mac);
584 en_err(priv, "Failed adding MAC: %pM\n",
585 priv->dev->dev_addr);
589 if (dev->caps.steering_mode == MLX4_STEERING_MODE_A0) {
590 int base_qpn = mlx4_get_base_qpn(dev, priv->port);
591 *qpn = base_qpn + index;
595 err = mlx4_qp_reserve_range(dev, 1, 1, qpn, MLX4_RESERVE_A0_QP);
596 en_dbg(DRV, priv, "Reserved qp %d\n", *qpn);
598 en_err(priv, "Failed to reserve qp for mac registration\n");
599 mlx4_unregister_mac(dev, priv->port, mac);
606 static void mlx4_en_put_qp(struct mlx4_en_priv *priv)
608 struct mlx4_en_dev *mdev = priv->mdev;
609 struct mlx4_dev *dev = mdev->dev;
610 int qpn = priv->base_qpn;
612 if (dev->caps.steering_mode == MLX4_STEERING_MODE_A0) {
613 u64 mac = mlx4_mac_to_u64(priv->dev->dev_addr);
614 en_dbg(DRV, priv, "Registering MAC: %pM for deleting\n",
615 priv->dev->dev_addr);
616 mlx4_unregister_mac(dev, priv->port, mac);
618 en_dbg(DRV, priv, "Releasing qp: port %d, qpn %d\n",
620 mlx4_qp_release_range(dev, qpn, 1);
621 priv->flags &= ~MLX4_EN_FLAG_FORCE_PROMISC;
625 static int mlx4_en_replace_mac(struct mlx4_en_priv *priv, int qpn,
626 unsigned char *new_mac, unsigned char *prev_mac)
628 struct mlx4_en_dev *mdev = priv->mdev;
629 struct mlx4_dev *dev = mdev->dev;
631 u64 new_mac_u64 = mlx4_mac_to_u64(new_mac);
633 if (dev->caps.steering_mode != MLX4_STEERING_MODE_A0) {
634 struct hlist_head *bucket;
635 unsigned int mac_hash;
636 struct mlx4_mac_entry *entry;
637 struct hlist_node *tmp;
638 u64 prev_mac_u64 = mlx4_mac_to_u64(prev_mac);
640 bucket = &priv->mac_hash[prev_mac[MLX4_EN_MAC_HASH_IDX]];
641 hlist_for_each_entry_safe(entry, tmp, bucket, hlist) {
642 if (ether_addr_equal_64bits(entry->mac, prev_mac)) {
643 mlx4_en_uc_steer_release(priv, entry->mac,
645 mlx4_unregister_mac(dev, priv->port,
647 hlist_del_rcu(&entry->hlist);
649 memcpy(entry->mac, new_mac, ETH_ALEN);
651 mac_hash = new_mac[MLX4_EN_MAC_HASH_IDX];
652 hlist_add_head_rcu(&entry->hlist,
653 &priv->mac_hash[mac_hash]);
654 mlx4_register_mac(dev, priv->port, new_mac_u64);
655 err = mlx4_en_uc_steer_add(priv, new_mac,
660 if (priv->tunnel_reg_id) {
661 mlx4_flow_detach(priv->mdev->dev, priv->tunnel_reg_id);
662 priv->tunnel_reg_id = 0;
664 err = mlx4_en_tunnel_steer_add(priv, new_mac, qpn,
665 &priv->tunnel_reg_id);
672 return __mlx4_replace_mac(dev, priv->port, qpn, new_mac_u64);
675 static int mlx4_en_do_set_mac(struct mlx4_en_priv *priv,
676 unsigned char new_mac[ETH_ALEN + 2])
681 /* Remove old MAC and insert the new one */
682 err = mlx4_en_replace_mac(priv, priv->base_qpn,
683 new_mac, priv->current_mac);
685 en_err(priv, "Failed changing HW MAC address\n");
687 en_dbg(HW, priv, "Port is down while registering mac, exiting...\n");
690 memcpy(priv->current_mac, new_mac, sizeof(priv->current_mac));
695 static int mlx4_en_set_mac(struct net_device *dev, void *addr)
697 struct mlx4_en_priv *priv = netdev_priv(dev);
698 struct mlx4_en_dev *mdev = priv->mdev;
699 struct sockaddr *saddr = addr;
700 unsigned char new_mac[ETH_ALEN + 2];
703 if (!is_valid_ether_addr(saddr->sa_data))
704 return -EADDRNOTAVAIL;
706 mutex_lock(&mdev->state_lock);
707 memcpy(new_mac, saddr->sa_data, ETH_ALEN);
708 err = mlx4_en_do_set_mac(priv, new_mac);
710 memcpy(dev->dev_addr, saddr->sa_data, ETH_ALEN);
711 mutex_unlock(&mdev->state_lock);
716 static void mlx4_en_clear_list(struct net_device *dev)
718 struct mlx4_en_priv *priv = netdev_priv(dev);
719 struct mlx4_en_mc_list *tmp, *mc_to_del;
721 list_for_each_entry_safe(mc_to_del, tmp, &priv->mc_list, list) {
722 list_del(&mc_to_del->list);
727 static void mlx4_en_cache_mclist(struct net_device *dev)
729 struct mlx4_en_priv *priv = netdev_priv(dev);
730 struct netdev_hw_addr *ha;
731 struct mlx4_en_mc_list *tmp;
733 mlx4_en_clear_list(dev);
734 netdev_for_each_mc_addr(ha, dev) {
735 tmp = kzalloc(sizeof(struct mlx4_en_mc_list), GFP_ATOMIC);
737 mlx4_en_clear_list(dev);
740 memcpy(tmp->addr, ha->addr, ETH_ALEN);
741 list_add_tail(&tmp->list, &priv->mc_list);
745 static void update_mclist_flags(struct mlx4_en_priv *priv,
746 struct list_head *dst,
747 struct list_head *src)
749 struct mlx4_en_mc_list *dst_tmp, *src_tmp, *new_mc;
752 /* Find all the entries that should be removed from dst,
753 * These are the entries that are not found in src
755 list_for_each_entry(dst_tmp, dst, list) {
757 list_for_each_entry(src_tmp, src, list) {
758 if (ether_addr_equal(dst_tmp->addr, src_tmp->addr)) {
764 dst_tmp->action = MCLIST_REM;
767 /* Add entries that exist in src but not in dst
768 * mark them as need to add
770 list_for_each_entry(src_tmp, src, list) {
772 list_for_each_entry(dst_tmp, dst, list) {
773 if (ether_addr_equal(dst_tmp->addr, src_tmp->addr)) {
774 dst_tmp->action = MCLIST_NONE;
780 new_mc = kmemdup(src_tmp,
781 sizeof(struct mlx4_en_mc_list),
786 new_mc->action = MCLIST_ADD;
787 list_add_tail(&new_mc->list, dst);
792 static void mlx4_en_set_rx_mode(struct net_device *dev)
794 struct mlx4_en_priv *priv = netdev_priv(dev);
799 queue_work(priv->mdev->workqueue, &priv->rx_mode_task);
802 static void mlx4_en_set_promisc_mode(struct mlx4_en_priv *priv,
803 struct mlx4_en_dev *mdev)
807 if (!(priv->flags & MLX4_EN_FLAG_PROMISC)) {
808 if (netif_msg_rx_status(priv))
809 en_warn(priv, "Entering promiscuous mode\n");
810 priv->flags |= MLX4_EN_FLAG_PROMISC;
812 /* Enable promiscouos mode */
813 switch (mdev->dev->caps.steering_mode) {
814 case MLX4_STEERING_MODE_DEVICE_MANAGED:
815 err = mlx4_flow_steer_promisc_add(mdev->dev,
818 MLX4_FS_ALL_DEFAULT);
820 en_err(priv, "Failed enabling promiscuous mode\n");
821 priv->flags |= MLX4_EN_FLAG_MC_PROMISC;
824 case MLX4_STEERING_MODE_B0:
825 err = mlx4_unicast_promisc_add(mdev->dev,
829 en_err(priv, "Failed enabling unicast promiscuous mode\n");
831 /* Add the default qp number as multicast
834 if (!(priv->flags & MLX4_EN_FLAG_MC_PROMISC)) {
835 err = mlx4_multicast_promisc_add(mdev->dev,
839 en_err(priv, "Failed enabling multicast promiscuous mode\n");
840 priv->flags |= MLX4_EN_FLAG_MC_PROMISC;
844 case MLX4_STEERING_MODE_A0:
845 err = mlx4_SET_PORT_qpn_calc(mdev->dev,
850 en_err(priv, "Failed enabling promiscuous mode\n");
854 /* Disable port multicast filter (unconditionally) */
855 err = mlx4_SET_MCAST_FLTR(mdev->dev, priv->port, 0,
856 0, MLX4_MCAST_DISABLE);
858 en_err(priv, "Failed disabling multicast filter\n");
862 static void mlx4_en_clear_promisc_mode(struct mlx4_en_priv *priv,
863 struct mlx4_en_dev *mdev)
867 if (netif_msg_rx_status(priv))
868 en_warn(priv, "Leaving promiscuous mode\n");
869 priv->flags &= ~MLX4_EN_FLAG_PROMISC;
871 /* Disable promiscouos mode */
872 switch (mdev->dev->caps.steering_mode) {
873 case MLX4_STEERING_MODE_DEVICE_MANAGED:
874 err = mlx4_flow_steer_promisc_remove(mdev->dev,
876 MLX4_FS_ALL_DEFAULT);
878 en_err(priv, "Failed disabling promiscuous mode\n");
879 priv->flags &= ~MLX4_EN_FLAG_MC_PROMISC;
882 case MLX4_STEERING_MODE_B0:
883 err = mlx4_unicast_promisc_remove(mdev->dev,
887 en_err(priv, "Failed disabling unicast promiscuous mode\n");
888 /* Disable Multicast promisc */
889 if (priv->flags & MLX4_EN_FLAG_MC_PROMISC) {
890 err = mlx4_multicast_promisc_remove(mdev->dev,
894 en_err(priv, "Failed disabling multicast promiscuous mode\n");
895 priv->flags &= ~MLX4_EN_FLAG_MC_PROMISC;
899 case MLX4_STEERING_MODE_A0:
900 err = mlx4_SET_PORT_qpn_calc(mdev->dev,
904 en_err(priv, "Failed disabling promiscuous mode\n");
909 static void mlx4_en_do_multicast(struct mlx4_en_priv *priv,
910 struct net_device *dev,
911 struct mlx4_en_dev *mdev)
913 struct mlx4_en_mc_list *mclist, *tmp;
915 u8 mc_list[16] = {0};
918 /* Enable/disable the multicast filter according to IFF_ALLMULTI */
919 if (dev->flags & IFF_ALLMULTI) {
920 err = mlx4_SET_MCAST_FLTR(mdev->dev, priv->port, 0,
921 0, MLX4_MCAST_DISABLE);
923 en_err(priv, "Failed disabling multicast filter\n");
925 /* Add the default qp number as multicast promisc */
926 if (!(priv->flags & MLX4_EN_FLAG_MC_PROMISC)) {
927 switch (mdev->dev->caps.steering_mode) {
928 case MLX4_STEERING_MODE_DEVICE_MANAGED:
929 err = mlx4_flow_steer_promisc_add(mdev->dev,
935 case MLX4_STEERING_MODE_B0:
936 err = mlx4_multicast_promisc_add(mdev->dev,
941 case MLX4_STEERING_MODE_A0:
945 en_err(priv, "Failed entering multicast promisc mode\n");
946 priv->flags |= MLX4_EN_FLAG_MC_PROMISC;
949 /* Disable Multicast promisc */
950 if (priv->flags & MLX4_EN_FLAG_MC_PROMISC) {
951 switch (mdev->dev->caps.steering_mode) {
952 case MLX4_STEERING_MODE_DEVICE_MANAGED:
953 err = mlx4_flow_steer_promisc_remove(mdev->dev,
958 case MLX4_STEERING_MODE_B0:
959 err = mlx4_multicast_promisc_remove(mdev->dev,
964 case MLX4_STEERING_MODE_A0:
968 en_err(priv, "Failed disabling multicast promiscuous mode\n");
969 priv->flags &= ~MLX4_EN_FLAG_MC_PROMISC;
972 err = mlx4_SET_MCAST_FLTR(mdev->dev, priv->port, 0,
973 0, MLX4_MCAST_DISABLE);
975 en_err(priv, "Failed disabling multicast filter\n");
977 /* Flush mcast filter and init it with broadcast address */
978 mlx4_SET_MCAST_FLTR(mdev->dev, priv->port, ETH_BCAST,
979 1, MLX4_MCAST_CONFIG);
981 /* Update multicast list - we cache all addresses so they won't
982 * change while HW is updated holding the command semaphor */
983 netif_addr_lock_bh(dev);
984 mlx4_en_cache_mclist(dev);
985 netif_addr_unlock_bh(dev);
986 list_for_each_entry(mclist, &priv->mc_list, list) {
987 mcast_addr = mlx4_mac_to_u64(mclist->addr);
988 mlx4_SET_MCAST_FLTR(mdev->dev, priv->port,
989 mcast_addr, 0, MLX4_MCAST_CONFIG);
991 err = mlx4_SET_MCAST_FLTR(mdev->dev, priv->port, 0,
992 0, MLX4_MCAST_ENABLE);
994 en_err(priv, "Failed enabling multicast filter\n");
996 update_mclist_flags(priv, &priv->curr_list, &priv->mc_list);
997 list_for_each_entry_safe(mclist, tmp, &priv->curr_list, list) {
998 if (mclist->action == MCLIST_REM) {
999 /* detach this address and delete from list */
1000 memcpy(&mc_list[10], mclist->addr, ETH_ALEN);
1001 mc_list[5] = priv->port;
1002 err = mlx4_multicast_detach(mdev->dev,
1003 &priv->rss_map.indir_qp,
1008 en_err(priv, "Fail to detach multicast address\n");
1010 if (mclist->tunnel_reg_id) {
1011 err = mlx4_flow_detach(priv->mdev->dev, mclist->tunnel_reg_id);
1013 en_err(priv, "Failed to detach multicast address\n");
1016 /* remove from list */
1017 list_del(&mclist->list);
1019 } else if (mclist->action == MCLIST_ADD) {
1020 /* attach the address */
1021 memcpy(&mc_list[10], mclist->addr, ETH_ALEN);
1022 /* needed for B0 steering support */
1023 mc_list[5] = priv->port;
1024 err = mlx4_multicast_attach(mdev->dev,
1025 &priv->rss_map.indir_qp,
1031 en_err(priv, "Fail to attach multicast address\n");
1033 err = mlx4_en_tunnel_steer_add(priv, &mc_list[10], priv->base_qpn,
1034 &mclist->tunnel_reg_id);
1036 en_err(priv, "Failed to attach multicast address\n");
1042 static void mlx4_en_do_uc_filter(struct mlx4_en_priv *priv,
1043 struct net_device *dev,
1044 struct mlx4_en_dev *mdev)
1046 struct netdev_hw_addr *ha;
1047 struct mlx4_mac_entry *entry;
1048 struct hlist_node *tmp;
1052 struct hlist_head *bucket;
1057 /* Note that we do not need to protect our mac_hash traversal with rcu,
1058 * since all modification code is protected by mdev->state_lock
1061 /* find what to remove */
1062 for (i = 0; i < MLX4_EN_MAC_HASH_SIZE; ++i) {
1063 bucket = &priv->mac_hash[i];
1064 hlist_for_each_entry_safe(entry, tmp, bucket, hlist) {
1066 netdev_for_each_uc_addr(ha, dev) {
1067 if (ether_addr_equal_64bits(entry->mac,
1074 /* MAC address of the port is not in uc list */
1075 if (ether_addr_equal_64bits(entry->mac,
1080 mac = mlx4_mac_to_u64(entry->mac);
1081 mlx4_en_uc_steer_release(priv, entry->mac,
1084 mlx4_unregister_mac(mdev->dev, priv->port, mac);
1086 hlist_del_rcu(&entry->hlist);
1087 kfree_rcu(entry, rcu);
1088 en_dbg(DRV, priv, "Removed MAC %pM on port:%d\n",
1089 entry->mac, priv->port);
1095 /* if we didn't remove anything, there is no use in trying to add
1096 * again once we are in a forced promisc mode state
1098 if ((priv->flags & MLX4_EN_FLAG_FORCE_PROMISC) && 0 == removed)
1101 prev_flags = priv->flags;
1102 priv->flags &= ~MLX4_EN_FLAG_FORCE_PROMISC;
1104 /* find what to add */
1105 netdev_for_each_uc_addr(ha, dev) {
1107 bucket = &priv->mac_hash[ha->addr[MLX4_EN_MAC_HASH_IDX]];
1108 hlist_for_each_entry(entry, bucket, hlist) {
1109 if (ether_addr_equal_64bits(entry->mac, ha->addr)) {
1116 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1118 en_err(priv, "Failed adding MAC %pM on port:%d (out of memory)\n",
1119 ha->addr, priv->port);
1120 priv->flags |= MLX4_EN_FLAG_FORCE_PROMISC;
1123 mac = mlx4_mac_to_u64(ha->addr);
1124 memcpy(entry->mac, ha->addr, ETH_ALEN);
1125 err = mlx4_register_mac(mdev->dev, priv->port, mac);
1127 en_err(priv, "Failed registering MAC %pM on port %d: %d\n",
1128 ha->addr, priv->port, err);
1130 priv->flags |= MLX4_EN_FLAG_FORCE_PROMISC;
1133 err = mlx4_en_uc_steer_add(priv, ha->addr,
1137 en_err(priv, "Failed adding MAC %pM on port %d: %d\n",
1138 ha->addr, priv->port, err);
1139 mlx4_unregister_mac(mdev->dev, priv->port, mac);
1141 priv->flags |= MLX4_EN_FLAG_FORCE_PROMISC;
1144 unsigned int mac_hash;
1145 en_dbg(DRV, priv, "Added MAC %pM on port:%d\n",
1146 ha->addr, priv->port);
1147 mac_hash = ha->addr[MLX4_EN_MAC_HASH_IDX];
1148 bucket = &priv->mac_hash[mac_hash];
1149 hlist_add_head_rcu(&entry->hlist, bucket);
1154 if (priv->flags & MLX4_EN_FLAG_FORCE_PROMISC) {
1155 en_warn(priv, "Forcing promiscuous mode on port:%d\n",
1157 } else if (prev_flags & MLX4_EN_FLAG_FORCE_PROMISC) {
1158 en_warn(priv, "Stop forcing promiscuous mode on port:%d\n",
1163 static void mlx4_en_do_set_rx_mode(struct work_struct *work)
1165 struct mlx4_en_priv *priv = container_of(work, struct mlx4_en_priv,
1167 struct mlx4_en_dev *mdev = priv->mdev;
1168 struct net_device *dev = priv->dev;
1170 mutex_lock(&mdev->state_lock);
1171 if (!mdev->device_up) {
1172 en_dbg(HW, priv, "Card is not up, ignoring rx mode change.\n");
1175 if (!priv->port_up) {
1176 en_dbg(HW, priv, "Port is down, ignoring rx mode change.\n");
1180 if (!netif_carrier_ok(dev)) {
1181 if (!mlx4_en_QUERY_PORT(mdev, priv->port)) {
1182 if (priv->port_state.link_state) {
1183 priv->last_link_state = MLX4_DEV_EVENT_PORT_UP;
1184 netif_carrier_on(dev);
1185 en_dbg(LINK, priv, "Link Up\n");
1190 if (dev->priv_flags & IFF_UNICAST_FLT)
1191 mlx4_en_do_uc_filter(priv, dev, mdev);
1193 /* Promsicuous mode: disable all filters */
1194 if ((dev->flags & IFF_PROMISC) ||
1195 (priv->flags & MLX4_EN_FLAG_FORCE_PROMISC)) {
1196 mlx4_en_set_promisc_mode(priv, mdev);
1200 /* Not in promiscuous mode */
1201 if (priv->flags & MLX4_EN_FLAG_PROMISC)
1202 mlx4_en_clear_promisc_mode(priv, mdev);
1204 mlx4_en_do_multicast(priv, dev, mdev);
1206 mutex_unlock(&mdev->state_lock);
1209 #ifdef CONFIG_NET_POLL_CONTROLLER
1210 static void mlx4_en_netpoll(struct net_device *dev)
1212 struct mlx4_en_priv *priv = netdev_priv(dev);
1213 struct mlx4_en_cq *cq;
1216 for (i = 0; i < priv->tx_ring_num; i++) {
1217 cq = priv->tx_cq[i];
1218 napi_schedule(&cq->napi);
1223 static int mlx4_en_set_rss_steer_rules(struct mlx4_en_priv *priv)
1227 int *qpn = &priv->base_qpn;
1228 struct mlx4_mac_entry *entry;
1230 err = mlx4_en_uc_steer_add(priv, priv->dev->dev_addr, qpn, ®_id);
1234 err = mlx4_en_tunnel_steer_add(priv, priv->dev->dev_addr, *qpn,
1235 &priv->tunnel_reg_id);
1239 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1245 memcpy(entry->mac, priv->dev->dev_addr, sizeof(entry->mac));
1246 memcpy(priv->current_mac, entry->mac, sizeof(priv->current_mac));
1247 entry->reg_id = reg_id;
1248 hlist_add_head_rcu(&entry->hlist,
1249 &priv->mac_hash[entry->mac[MLX4_EN_MAC_HASH_IDX]]);
1254 if (priv->tunnel_reg_id)
1255 mlx4_flow_detach(priv->mdev->dev, priv->tunnel_reg_id);
1258 mlx4_en_uc_steer_release(priv, priv->dev->dev_addr, *qpn, reg_id);
1262 static void mlx4_en_delete_rss_steer_rules(struct mlx4_en_priv *priv)
1266 int qpn = priv->base_qpn;
1267 struct hlist_head *bucket;
1268 struct hlist_node *tmp;
1269 struct mlx4_mac_entry *entry;
1271 for (i = 0; i < MLX4_EN_MAC_HASH_SIZE; ++i) {
1272 bucket = &priv->mac_hash[i];
1273 hlist_for_each_entry_safe(entry, tmp, bucket, hlist) {
1274 mac = mlx4_mac_to_u64(entry->mac);
1275 en_dbg(DRV, priv, "Registering MAC:%pM for deleting\n",
1277 mlx4_en_uc_steer_release(priv, entry->mac,
1278 qpn, entry->reg_id);
1280 mlx4_unregister_mac(priv->mdev->dev, priv->port, mac);
1281 hlist_del_rcu(&entry->hlist);
1282 kfree_rcu(entry, rcu);
1286 if (priv->tunnel_reg_id) {
1287 mlx4_flow_detach(priv->mdev->dev, priv->tunnel_reg_id);
1288 priv->tunnel_reg_id = 0;
1292 static void mlx4_en_tx_timeout(struct net_device *dev)
1294 struct mlx4_en_priv *priv = netdev_priv(dev);
1295 struct mlx4_en_dev *mdev = priv->mdev;
1298 if (netif_msg_timer(priv))
1299 en_warn(priv, "Tx timeout called on port:%d\n", priv->port);
1301 for (i = 0; i < priv->tx_ring_num; i++) {
1302 if (!netif_tx_queue_stopped(netdev_get_tx_queue(dev, i)))
1304 en_warn(priv, "TX timeout on queue: %d, QP: 0x%x, CQ: 0x%x, Cons: 0x%x, Prod: 0x%x\n",
1305 i, priv->tx_ring[i]->qpn, priv->tx_ring[i]->cqn,
1306 priv->tx_ring[i]->cons, priv->tx_ring[i]->prod);
1309 priv->port_stats.tx_timeout++;
1310 en_dbg(DRV, priv, "Scheduling watchdog\n");
1311 queue_work(mdev->workqueue, &priv->watchdog_task);
1315 static struct rtnl_link_stats64 *
1316 mlx4_en_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
1318 struct mlx4_en_priv *priv = netdev_priv(dev);
1320 spin_lock_bh(&priv->stats_lock);
1321 netdev_stats_to_stats64(stats, &dev->stats);
1322 spin_unlock_bh(&priv->stats_lock);
1327 static void mlx4_en_set_default_moderation(struct mlx4_en_priv *priv)
1329 struct mlx4_en_cq *cq;
1332 /* If we haven't received a specific coalescing setting
1333 * (module param), we set the moderation parameters as follows:
1334 * - moder_cnt is set to the number of mtu sized packets to
1335 * satisfy our coalescing target.
1336 * - moder_time is set to a fixed value.
1338 priv->rx_frames = MLX4_EN_RX_COAL_TARGET;
1339 priv->rx_usecs = MLX4_EN_RX_COAL_TIME;
1340 priv->tx_frames = MLX4_EN_TX_COAL_PKTS;
1341 priv->tx_usecs = MLX4_EN_TX_COAL_TIME;
1342 en_dbg(INTR, priv, "Default coalesing params for mtu:%d - rx_frames:%d rx_usecs:%d\n",
1343 priv->dev->mtu, priv->rx_frames, priv->rx_usecs);
1345 /* Setup cq moderation params */
1346 for (i = 0; i < priv->rx_ring_num; i++) {
1347 cq = priv->rx_cq[i];
1348 cq->moder_cnt = priv->rx_frames;
1349 cq->moder_time = priv->rx_usecs;
1350 priv->last_moder_time[i] = MLX4_EN_AUTO_CONF;
1351 priv->last_moder_packets[i] = 0;
1352 priv->last_moder_bytes[i] = 0;
1355 for (i = 0; i < priv->tx_ring_num; i++) {
1356 cq = priv->tx_cq[i];
1357 cq->moder_cnt = priv->tx_frames;
1358 cq->moder_time = priv->tx_usecs;
1361 /* Reset auto-moderation params */
1362 priv->pkt_rate_low = MLX4_EN_RX_RATE_LOW;
1363 priv->rx_usecs_low = MLX4_EN_RX_COAL_TIME_LOW;
1364 priv->pkt_rate_high = MLX4_EN_RX_RATE_HIGH;
1365 priv->rx_usecs_high = MLX4_EN_RX_COAL_TIME_HIGH;
1366 priv->sample_interval = MLX4_EN_SAMPLE_INTERVAL;
1367 priv->adaptive_rx_coal = 1;
1368 priv->last_moder_jiffies = 0;
1369 priv->last_moder_tx_packets = 0;
1372 static void mlx4_en_auto_moderation(struct mlx4_en_priv *priv)
1374 unsigned long period = (unsigned long) (jiffies - priv->last_moder_jiffies);
1375 struct mlx4_en_cq *cq;
1376 unsigned long packets;
1378 unsigned long avg_pkt_size;
1379 unsigned long rx_packets;
1380 unsigned long rx_bytes;
1381 unsigned long rx_pkt_diff;
1385 if (!priv->adaptive_rx_coal || period < priv->sample_interval * HZ)
1388 for (ring = 0; ring < priv->rx_ring_num; ring++) {
1389 spin_lock_bh(&priv->stats_lock);
1390 rx_packets = priv->rx_ring[ring]->packets;
1391 rx_bytes = priv->rx_ring[ring]->bytes;
1392 spin_unlock_bh(&priv->stats_lock);
1394 rx_pkt_diff = ((unsigned long) (rx_packets -
1395 priv->last_moder_packets[ring]));
1396 packets = rx_pkt_diff;
1397 rate = packets * HZ / period;
1398 avg_pkt_size = packets ? ((unsigned long) (rx_bytes -
1399 priv->last_moder_bytes[ring])) / packets : 0;
1401 /* Apply auto-moderation only when packet rate
1402 * exceeds a rate that it matters */
1403 if (rate > (MLX4_EN_RX_RATE_THRESH / priv->rx_ring_num) &&
1404 avg_pkt_size > MLX4_EN_AVG_PKT_SMALL) {
1405 if (rate < priv->pkt_rate_low)
1406 moder_time = priv->rx_usecs_low;
1407 else if (rate > priv->pkt_rate_high)
1408 moder_time = priv->rx_usecs_high;
1410 moder_time = (rate - priv->pkt_rate_low) *
1411 (priv->rx_usecs_high - priv->rx_usecs_low) /
1412 (priv->pkt_rate_high - priv->pkt_rate_low) +
1415 moder_time = priv->rx_usecs_low;
1418 if (moder_time != priv->last_moder_time[ring]) {
1419 priv->last_moder_time[ring] = moder_time;
1420 cq = priv->rx_cq[ring];
1421 cq->moder_time = moder_time;
1422 cq->moder_cnt = priv->rx_frames;
1423 err = mlx4_en_set_cq_moder(priv, cq);
1425 en_err(priv, "Failed modifying moderation for cq:%d\n",
1428 priv->last_moder_packets[ring] = rx_packets;
1429 priv->last_moder_bytes[ring] = rx_bytes;
1432 priv->last_moder_jiffies = jiffies;
1435 static void mlx4_en_do_get_stats(struct work_struct *work)
1437 struct delayed_work *delay = to_delayed_work(work);
1438 struct mlx4_en_priv *priv = container_of(delay, struct mlx4_en_priv,
1440 struct mlx4_en_dev *mdev = priv->mdev;
1443 mutex_lock(&mdev->state_lock);
1444 if (mdev->device_up) {
1445 if (priv->port_up) {
1446 err = mlx4_en_DUMP_ETH_STATS(mdev, priv->port, 0);
1448 en_dbg(HW, priv, "Could not update stats\n");
1450 mlx4_en_auto_moderation(priv);
1453 queue_delayed_work(mdev->workqueue, &priv->stats_task, STATS_DELAY);
1455 if (mdev->mac_removed[MLX4_MAX_PORTS + 1 - priv->port]) {
1456 mlx4_en_do_set_mac(priv, priv->current_mac);
1457 mdev->mac_removed[MLX4_MAX_PORTS + 1 - priv->port] = 0;
1459 mutex_unlock(&mdev->state_lock);
1462 /* mlx4_en_service_task - Run service task for tasks that needed to be done
1465 static void mlx4_en_service_task(struct work_struct *work)
1467 struct delayed_work *delay = to_delayed_work(work);
1468 struct mlx4_en_priv *priv = container_of(delay, struct mlx4_en_priv,
1470 struct mlx4_en_dev *mdev = priv->mdev;
1472 mutex_lock(&mdev->state_lock);
1473 if (mdev->device_up) {
1474 if (mdev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_TS)
1475 mlx4_en_ptp_overflow_check(mdev);
1477 mlx4_en_recover_from_oom(priv);
1478 queue_delayed_work(mdev->workqueue, &priv->service_task,
1479 SERVICE_TASK_DELAY);
1481 mutex_unlock(&mdev->state_lock);
1484 static void mlx4_en_linkstate(struct work_struct *work)
1486 struct mlx4_en_priv *priv = container_of(work, struct mlx4_en_priv,
1488 struct mlx4_en_dev *mdev = priv->mdev;
1489 int linkstate = priv->link_state;
1491 mutex_lock(&mdev->state_lock);
1492 /* If observable port state changed set carrier state and
1493 * report to system log */
1494 if (priv->last_link_state != linkstate) {
1495 if (linkstate == MLX4_DEV_EVENT_PORT_DOWN) {
1496 en_info(priv, "Link Down\n");
1497 netif_carrier_off(priv->dev);
1499 en_info(priv, "Link Up\n");
1500 netif_carrier_on(priv->dev);
1503 priv->last_link_state = linkstate;
1504 mutex_unlock(&mdev->state_lock);
1507 static int mlx4_en_init_affinity_hint(struct mlx4_en_priv *priv, int ring_idx)
1509 struct mlx4_en_rx_ring *ring = priv->rx_ring[ring_idx];
1510 int numa_node = priv->mdev->dev->numa_node;
1512 if (!zalloc_cpumask_var(&ring->affinity_mask, GFP_KERNEL))
1515 cpumask_set_cpu(cpumask_local_spread(ring_idx, numa_node),
1516 ring->affinity_mask);
1520 static void mlx4_en_free_affinity_hint(struct mlx4_en_priv *priv, int ring_idx)
1522 free_cpumask_var(priv->rx_ring[ring_idx]->affinity_mask);
1525 static void mlx4_en_init_recycle_ring(struct mlx4_en_priv *priv,
1528 struct mlx4_en_tx_ring *tx_ring = priv->tx_ring[tx_ring_idx];
1531 rr_index = (priv->xdp_ring_num - priv->tx_ring_num) + tx_ring_idx;
1532 if (rr_index >= 0) {
1533 tx_ring->free_tx_desc = mlx4_en_recycle_tx_desc;
1534 tx_ring->recycle_ring = priv->rx_ring[rr_index];
1536 "Set tx_ring[%d]->recycle_ring = rx_ring[%d]\n",
1537 tx_ring_idx, rr_index);
1539 tx_ring->recycle_ring = NULL;
1543 int mlx4_en_start_port(struct net_device *dev)
1545 struct mlx4_en_priv *priv = netdev_priv(dev);
1546 struct mlx4_en_dev *mdev = priv->mdev;
1547 struct mlx4_en_cq *cq;
1548 struct mlx4_en_tx_ring *tx_ring;
1554 u8 mc_list[16] = {0};
1556 if (priv->port_up) {
1557 en_dbg(DRV, priv, "start port called while port already up\n");
1561 INIT_LIST_HEAD(&priv->mc_list);
1562 INIT_LIST_HEAD(&priv->curr_list);
1563 INIT_LIST_HEAD(&priv->ethtool_list);
1564 memset(&priv->ethtool_rules[0], 0,
1565 sizeof(struct ethtool_flow_id) * MAX_NUM_OF_FS_RULES);
1567 /* Calculate Rx buf size */
1568 dev->mtu = min(dev->mtu, priv->max_mtu);
1569 mlx4_en_calc_rx_buf(dev);
1570 en_dbg(DRV, priv, "Rx buf size:%d\n", priv->rx_skb_size);
1572 /* Configure rx cq's and rings */
1573 err = mlx4_en_activate_rx_rings(priv);
1575 en_err(priv, "Failed to activate RX rings\n");
1578 for (i = 0; i < priv->rx_ring_num; i++) {
1579 cq = priv->rx_cq[i];
1581 err = mlx4_en_init_affinity_hint(priv, i);
1583 en_err(priv, "Failed preparing IRQ affinity hint\n");
1587 err = mlx4_en_activate_cq(priv, cq, i);
1589 en_err(priv, "Failed activating Rx CQ\n");
1590 mlx4_en_free_affinity_hint(priv, i);
1594 for (j = 0; j < cq->size; j++) {
1595 struct mlx4_cqe *cqe = NULL;
1597 cqe = mlx4_en_get_cqe(cq->buf, j, priv->cqe_size) +
1599 cqe->owner_sr_opcode = MLX4_CQE_OWNER_MASK;
1602 err = mlx4_en_set_cq_moder(priv, cq);
1604 en_err(priv, "Failed setting cq moderation parameters\n");
1605 mlx4_en_deactivate_cq(priv, cq);
1606 mlx4_en_free_affinity_hint(priv, i);
1609 mlx4_en_arm_cq(priv, cq);
1610 priv->rx_ring[i]->cqn = cq->mcq.cqn;
1615 en_dbg(DRV, priv, "Getting qp number for port %d\n", priv->port);
1616 err = mlx4_en_get_qp(priv);
1618 en_err(priv, "Failed getting eth qp\n");
1621 mdev->mac_removed[priv->port] = 0;
1623 priv->counter_index =
1624 mlx4_get_default_counter_index(mdev->dev, priv->port);
1626 err = mlx4_en_config_rss_steer(priv);
1628 en_err(priv, "Failed configuring rss steering\n");
1632 err = mlx4_en_create_drop_qp(priv);
1636 /* Configure tx cq's and rings */
1637 for (i = 0; i < priv->tx_ring_num; i++) {
1639 cq = priv->tx_cq[i];
1640 err = mlx4_en_activate_cq(priv, cq, i);
1642 en_err(priv, "Failed allocating Tx CQ\n");
1645 err = mlx4_en_set_cq_moder(priv, cq);
1647 en_err(priv, "Failed setting cq moderation parameters\n");
1648 mlx4_en_deactivate_cq(priv, cq);
1651 en_dbg(DRV, priv, "Resetting index of collapsed CQ:%d to -1\n", i);
1652 cq->buf->wqe_index = cpu_to_be16(0xffff);
1654 /* Configure ring */
1655 tx_ring = priv->tx_ring[i];
1656 err = mlx4_en_activate_tx_ring(priv, tx_ring, cq->mcq.cqn,
1657 i / priv->num_tx_rings_p_up);
1659 en_err(priv, "Failed allocating Tx ring\n");
1660 mlx4_en_deactivate_cq(priv, cq);
1663 tx_ring->tx_queue = netdev_get_tx_queue(dev, i);
1665 mlx4_en_init_recycle_ring(priv, i);
1667 /* Arm CQ for TX completions */
1668 mlx4_en_arm_cq(priv, cq);
1670 /* Set initial ownership of all Tx TXBBs to SW (1) */
1671 for (j = 0; j < tx_ring->buf_size; j += STAMP_STRIDE)
1672 *((u32 *) (tx_ring->buf + j)) = 0xffffffff;
1676 /* Configure port */
1677 err = mlx4_SET_PORT_general(mdev->dev, priv->port,
1678 priv->rx_skb_size + ETH_FCS_LEN,
1679 priv->prof->tx_pause,
1681 priv->prof->rx_pause,
1682 priv->prof->rx_ppp);
1684 en_err(priv, "Failed setting port general configurations for port %d, with error %d\n",
1688 /* Set default qp number */
1689 err = mlx4_SET_PORT_qpn_calc(mdev->dev, priv->port, priv->base_qpn, 0);
1691 en_err(priv, "Failed setting default qp numbers\n");
1695 if (mdev->dev->caps.tunnel_offload_mode == MLX4_TUNNEL_OFFLOAD_MODE_VXLAN) {
1696 err = mlx4_SET_PORT_VXLAN(mdev->dev, priv->port, VXLAN_STEER_BY_OUTER_MAC, 1);
1698 en_err(priv, "Failed setting port L2 tunnel configuration, err %d\n",
1705 en_dbg(HW, priv, "Initializing port\n");
1706 err = mlx4_INIT_PORT(mdev->dev, priv->port);
1708 en_err(priv, "Failed Initializing port\n");
1712 /* Set Unicast and VXLAN steering rules */
1713 if (mdev->dev->caps.steering_mode != MLX4_STEERING_MODE_A0 &&
1714 mlx4_en_set_rss_steer_rules(priv))
1715 mlx4_warn(mdev, "Failed setting steering rules\n");
1717 /* Attach rx QP to bradcast address */
1718 eth_broadcast_addr(&mc_list[10]);
1719 mc_list[5] = priv->port; /* needed for B0 steering support */
1720 if (mlx4_multicast_attach(mdev->dev, &priv->rss_map.indir_qp, mc_list,
1721 priv->port, 0, MLX4_PROT_ETH,
1722 &priv->broadcast_id))
1723 mlx4_warn(mdev, "Failed Attaching Broadcast\n");
1725 /* Must redo promiscuous mode setup. */
1726 priv->flags &= ~(MLX4_EN_FLAG_PROMISC | MLX4_EN_FLAG_MC_PROMISC);
1728 /* Schedule multicast task to populate multicast list */
1729 queue_work(mdev->workqueue, &priv->rx_mode_task);
1731 if (priv->mdev->dev->caps.tunnel_offload_mode == MLX4_TUNNEL_OFFLOAD_MODE_VXLAN)
1732 udp_tunnel_get_rx_info(dev);
1734 priv->port_up = true;
1735 netif_tx_start_all_queues(dev);
1736 netif_device_attach(dev);
1741 while (tx_index--) {
1742 mlx4_en_deactivate_tx_ring(priv, priv->tx_ring[tx_index]);
1743 mlx4_en_deactivate_cq(priv, priv->tx_cq[tx_index]);
1745 mlx4_en_destroy_drop_qp(priv);
1747 mlx4_en_release_rss_steer(priv);
1749 mlx4_en_put_qp(priv);
1751 while (rx_index--) {
1752 mlx4_en_deactivate_cq(priv, priv->rx_cq[rx_index]);
1753 mlx4_en_free_affinity_hint(priv, rx_index);
1755 for (i = 0; i < priv->rx_ring_num; i++)
1756 mlx4_en_deactivate_rx_ring(priv, priv->rx_ring[i]);
1758 return err; /* need to close devices */
1762 void mlx4_en_stop_port(struct net_device *dev, int detach)
1764 struct mlx4_en_priv *priv = netdev_priv(dev);
1765 struct mlx4_en_dev *mdev = priv->mdev;
1766 struct mlx4_en_mc_list *mclist, *tmp;
1767 struct ethtool_flow_id *flow, *tmp_flow;
1769 u8 mc_list[16] = {0};
1771 if (!priv->port_up) {
1772 en_dbg(DRV, priv, "stop port called while port already down\n");
1777 mlx4_CLOSE_PORT(mdev->dev, priv->port);
1779 /* Synchronize with tx routine */
1780 netif_tx_lock_bh(dev);
1782 netif_device_detach(dev);
1783 netif_tx_stop_all_queues(dev);
1784 netif_tx_unlock_bh(dev);
1786 netif_tx_disable(dev);
1788 /* Set port as not active */
1789 priv->port_up = false;
1790 priv->counter_index = MLX4_SINK_COUNTER_INDEX(mdev->dev);
1792 /* Promsicuous mode */
1793 if (mdev->dev->caps.steering_mode ==
1794 MLX4_STEERING_MODE_DEVICE_MANAGED) {
1795 priv->flags &= ~(MLX4_EN_FLAG_PROMISC |
1796 MLX4_EN_FLAG_MC_PROMISC);
1797 mlx4_flow_steer_promisc_remove(mdev->dev,
1799 MLX4_FS_ALL_DEFAULT);
1800 mlx4_flow_steer_promisc_remove(mdev->dev,
1802 MLX4_FS_MC_DEFAULT);
1803 } else if (priv->flags & MLX4_EN_FLAG_PROMISC) {
1804 priv->flags &= ~MLX4_EN_FLAG_PROMISC;
1806 /* Disable promiscouos mode */
1807 mlx4_unicast_promisc_remove(mdev->dev, priv->base_qpn,
1810 /* Disable Multicast promisc */
1811 if (priv->flags & MLX4_EN_FLAG_MC_PROMISC) {
1812 mlx4_multicast_promisc_remove(mdev->dev, priv->base_qpn,
1814 priv->flags &= ~MLX4_EN_FLAG_MC_PROMISC;
1818 /* Detach All multicasts */
1819 eth_broadcast_addr(&mc_list[10]);
1820 mc_list[5] = priv->port; /* needed for B0 steering support */
1821 mlx4_multicast_detach(mdev->dev, &priv->rss_map.indir_qp, mc_list,
1822 MLX4_PROT_ETH, priv->broadcast_id);
1823 list_for_each_entry(mclist, &priv->curr_list, list) {
1824 memcpy(&mc_list[10], mclist->addr, ETH_ALEN);
1825 mc_list[5] = priv->port;
1826 mlx4_multicast_detach(mdev->dev, &priv->rss_map.indir_qp,
1827 mc_list, MLX4_PROT_ETH, mclist->reg_id);
1828 if (mclist->tunnel_reg_id)
1829 mlx4_flow_detach(mdev->dev, mclist->tunnel_reg_id);
1831 mlx4_en_clear_list(dev);
1832 list_for_each_entry_safe(mclist, tmp, &priv->curr_list, list) {
1833 list_del(&mclist->list);
1837 /* Flush multicast filter */
1838 mlx4_SET_MCAST_FLTR(mdev->dev, priv->port, 0, 1, MLX4_MCAST_CONFIG);
1840 /* Remove flow steering rules for the port*/
1841 if (mdev->dev->caps.steering_mode ==
1842 MLX4_STEERING_MODE_DEVICE_MANAGED) {
1844 list_for_each_entry_safe(flow, tmp_flow,
1845 &priv->ethtool_list, list) {
1846 mlx4_flow_detach(mdev->dev, flow->id);
1847 list_del(&flow->list);
1851 mlx4_en_destroy_drop_qp(priv);
1854 for (i = 0; i < priv->tx_ring_num; i++) {
1855 mlx4_en_deactivate_tx_ring(priv, priv->tx_ring[i]);
1856 mlx4_en_deactivate_cq(priv, priv->tx_cq[i]);
1860 for (i = 0; i < priv->tx_ring_num; i++)
1861 mlx4_en_free_tx_buf(dev, priv->tx_ring[i]);
1863 if (mdev->dev->caps.steering_mode != MLX4_STEERING_MODE_A0)
1864 mlx4_en_delete_rss_steer_rules(priv);
1867 mlx4_en_release_rss_steer(priv);
1869 /* Unregister Mac address for the port */
1870 mlx4_en_put_qp(priv);
1871 if (!(mdev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_REASSIGN_MAC_EN))
1872 mdev->mac_removed[priv->port] = 1;
1875 for (i = 0; i < priv->rx_ring_num; i++) {
1876 struct mlx4_en_cq *cq = priv->rx_cq[i];
1878 napi_synchronize(&cq->napi);
1879 mlx4_en_deactivate_rx_ring(priv, priv->rx_ring[i]);
1880 mlx4_en_deactivate_cq(priv, cq);
1882 mlx4_en_free_affinity_hint(priv, i);
1886 static void mlx4_en_restart(struct work_struct *work)
1888 struct mlx4_en_priv *priv = container_of(work, struct mlx4_en_priv,
1890 struct mlx4_en_dev *mdev = priv->mdev;
1891 struct net_device *dev = priv->dev;
1893 en_dbg(DRV, priv, "Watchdog task called for port %d\n", priv->port);
1896 mutex_lock(&mdev->state_lock);
1897 if (priv->port_up) {
1898 mlx4_en_stop_port(dev, 1);
1899 if (mlx4_en_start_port(dev))
1900 en_err(priv, "Failed restarting port %d\n", priv->port);
1902 mutex_unlock(&mdev->state_lock);
1906 static void mlx4_en_clear_stats(struct net_device *dev)
1908 struct mlx4_en_priv *priv = netdev_priv(dev);
1909 struct mlx4_en_dev *mdev = priv->mdev;
1912 if (mlx4_en_DUMP_ETH_STATS(mdev, priv->port, 1))
1913 en_dbg(HW, priv, "Failed dumping statistics\n");
1915 memset(&priv->pstats, 0, sizeof(priv->pstats));
1916 memset(&priv->pkstats, 0, sizeof(priv->pkstats));
1917 memset(&priv->port_stats, 0, sizeof(priv->port_stats));
1918 memset(&priv->rx_flowstats, 0, sizeof(priv->rx_flowstats));
1919 memset(&priv->tx_flowstats, 0, sizeof(priv->tx_flowstats));
1920 memset(&priv->rx_priority_flowstats, 0,
1921 sizeof(priv->rx_priority_flowstats));
1922 memset(&priv->tx_priority_flowstats, 0,
1923 sizeof(priv->tx_priority_flowstats));
1924 memset(&priv->pf_stats, 0, sizeof(priv->pf_stats));
1926 for (i = 0; i < priv->tx_ring_num; i++) {
1927 priv->tx_ring[i]->bytes = 0;
1928 priv->tx_ring[i]->packets = 0;
1929 priv->tx_ring[i]->tx_csum = 0;
1930 priv->tx_ring[i]->tx_dropped = 0;
1931 priv->tx_ring[i]->queue_stopped = 0;
1932 priv->tx_ring[i]->wake_queue = 0;
1933 priv->tx_ring[i]->tso_packets = 0;
1934 priv->tx_ring[i]->xmit_more = 0;
1936 for (i = 0; i < priv->rx_ring_num; i++) {
1937 priv->rx_ring[i]->bytes = 0;
1938 priv->rx_ring[i]->packets = 0;
1939 priv->rx_ring[i]->csum_ok = 0;
1940 priv->rx_ring[i]->csum_none = 0;
1941 priv->rx_ring[i]->csum_complete = 0;
1945 static int mlx4_en_open(struct net_device *dev)
1947 struct mlx4_en_priv *priv = netdev_priv(dev);
1948 struct mlx4_en_dev *mdev = priv->mdev;
1951 mutex_lock(&mdev->state_lock);
1953 if (!mdev->device_up) {
1954 en_err(priv, "Cannot open - device down/disabled\n");
1959 /* Reset HW statistics and SW counters */
1960 mlx4_en_clear_stats(dev);
1962 err = mlx4_en_start_port(dev);
1964 en_err(priv, "Failed starting port:%d\n", priv->port);
1967 mutex_unlock(&mdev->state_lock);
1972 static int mlx4_en_close(struct net_device *dev)
1974 struct mlx4_en_priv *priv = netdev_priv(dev);
1975 struct mlx4_en_dev *mdev = priv->mdev;
1977 en_dbg(IFDOWN, priv, "Close port called\n");
1979 mutex_lock(&mdev->state_lock);
1981 mlx4_en_stop_port(dev, 0);
1982 netif_carrier_off(dev);
1984 mutex_unlock(&mdev->state_lock);
1988 static void mlx4_en_free_resources(struct mlx4_en_priv *priv)
1992 #ifdef CONFIG_RFS_ACCEL
1993 priv->dev->rx_cpu_rmap = NULL;
1996 for (i = 0; i < priv->tx_ring_num; i++) {
1997 if (priv->tx_ring && priv->tx_ring[i])
1998 mlx4_en_destroy_tx_ring(priv, &priv->tx_ring[i]);
1999 if (priv->tx_cq && priv->tx_cq[i])
2000 mlx4_en_destroy_cq(priv, &priv->tx_cq[i]);
2003 for (i = 0; i < priv->rx_ring_num; i++) {
2004 if (priv->rx_ring[i])
2005 mlx4_en_destroy_rx_ring(priv, &priv->rx_ring[i],
2006 priv->prof->rx_ring_size, priv->stride);
2008 mlx4_en_destroy_cq(priv, &priv->rx_cq[i]);
2013 static int mlx4_en_alloc_resources(struct mlx4_en_priv *priv)
2015 struct mlx4_en_port_profile *prof = priv->prof;
2019 /* Create tx Rings */
2020 for (i = 0; i < priv->tx_ring_num; i++) {
2021 node = cpu_to_node(i % num_online_cpus());
2022 if (mlx4_en_create_cq(priv, &priv->tx_cq[i],
2023 prof->tx_ring_size, i, TX, node))
2026 if (mlx4_en_create_tx_ring(priv, &priv->tx_ring[i],
2027 prof->tx_ring_size, TXBB_SIZE,
2032 /* Create rx Rings */
2033 for (i = 0; i < priv->rx_ring_num; i++) {
2034 node = cpu_to_node(i % num_online_cpus());
2035 if (mlx4_en_create_cq(priv, &priv->rx_cq[i],
2036 prof->rx_ring_size, i, RX, node))
2039 if (mlx4_en_create_rx_ring(priv, &priv->rx_ring[i],
2040 prof->rx_ring_size, priv->stride,
2045 #ifdef CONFIG_RFS_ACCEL
2046 priv->dev->rx_cpu_rmap = mlx4_get_cpu_rmap(priv->mdev->dev, priv->port);
2052 en_err(priv, "Failed to allocate NIC resources\n");
2053 for (i = 0; i < priv->rx_ring_num; i++) {
2054 if (priv->rx_ring[i])
2055 mlx4_en_destroy_rx_ring(priv, &priv->rx_ring[i],
2059 mlx4_en_destroy_cq(priv, &priv->rx_cq[i]);
2061 for (i = 0; i < priv->tx_ring_num; i++) {
2062 if (priv->tx_ring[i])
2063 mlx4_en_destroy_tx_ring(priv, &priv->tx_ring[i]);
2065 mlx4_en_destroy_cq(priv, &priv->tx_cq[i]);
2070 static void mlx4_en_shutdown(struct net_device *dev)
2073 netif_device_detach(dev);
2078 static int mlx4_en_copy_priv(struct mlx4_en_priv *dst,
2079 struct mlx4_en_priv *src,
2080 struct mlx4_en_port_profile *prof)
2082 memcpy(&dst->hwtstamp_config, &prof->hwtstamp_config,
2083 sizeof(dst->hwtstamp_config));
2084 dst->num_tx_rings_p_up = src->mdev->profile.num_tx_rings_p_up;
2085 dst->tx_ring_num = prof->tx_ring_num;
2086 dst->rx_ring_num = prof->rx_ring_num;
2087 dst->flags = prof->flags;
2088 dst->mdev = src->mdev;
2089 dst->port = src->port;
2090 dst->dev = src->dev;
2092 dst->stride = roundup_pow_of_two(sizeof(struct mlx4_en_rx_desc) +
2093 DS_SIZE * MLX4_EN_MAX_RX_FRAGS);
2095 dst->tx_ring = kzalloc(sizeof(struct mlx4_en_tx_ring *) * MAX_TX_RINGS,
2100 dst->tx_cq = kzalloc(sizeof(struct mlx4_en_cq *) * MAX_TX_RINGS,
2103 kfree(dst->tx_ring);
2109 static void mlx4_en_update_priv(struct mlx4_en_priv *dst,
2110 struct mlx4_en_priv *src)
2112 memcpy(dst->rx_ring, src->rx_ring,
2113 sizeof(struct mlx4_en_rx_ring *) * src->rx_ring_num);
2114 memcpy(dst->rx_cq, src->rx_cq,
2115 sizeof(struct mlx4_en_cq *) * src->rx_ring_num);
2116 memcpy(&dst->hwtstamp_config, &src->hwtstamp_config,
2117 sizeof(dst->hwtstamp_config));
2118 dst->tx_ring_num = src->tx_ring_num;
2119 dst->rx_ring_num = src->rx_ring_num;
2120 dst->tx_ring = src->tx_ring;
2121 dst->tx_cq = src->tx_cq;
2122 memcpy(dst->prof, src->prof, sizeof(struct mlx4_en_port_profile));
2125 int mlx4_en_try_alloc_resources(struct mlx4_en_priv *priv,
2126 struct mlx4_en_priv *tmp,
2127 struct mlx4_en_port_profile *prof)
2129 mlx4_en_copy_priv(tmp, priv, prof);
2131 if (mlx4_en_alloc_resources(tmp)) {
2133 "%s: Resource allocation failed, using previous configuration\n",
2135 kfree(tmp->tx_ring);
2142 void mlx4_en_safe_replace_resources(struct mlx4_en_priv *priv,
2143 struct mlx4_en_priv *tmp)
2145 mlx4_en_free_resources(priv);
2146 mlx4_en_update_priv(priv, tmp);
2149 void mlx4_en_destroy_netdev(struct net_device *dev)
2151 struct mlx4_en_priv *priv = netdev_priv(dev);
2152 struct mlx4_en_dev *mdev = priv->mdev;
2153 bool shutdown = mdev->dev->persist->interface_state &
2154 MLX4_INTERFACE_STATE_SHUTDOWN;
2156 en_dbg(DRV, priv, "Destroying netdev on port:%d\n", priv->port);
2158 /* Unregister device - this will close the port if it was up */
2159 if (priv->registered) {
2160 devlink_port_type_clear(mlx4_get_devlink_port(mdev->dev,
2163 mlx4_en_shutdown(dev);
2165 unregister_netdev(dev);
2168 if (priv->allocated)
2169 mlx4_free_hwq_res(mdev->dev, &priv->res, MLX4_EN_PAGE_SIZE);
2171 cancel_delayed_work(&priv->stats_task);
2172 cancel_delayed_work(&priv->service_task);
2173 /* flush any pending task for this netdev */
2174 flush_workqueue(mdev->workqueue);
2176 if (mdev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_TS)
2177 mlx4_en_remove_timestamp(mdev);
2179 /* Detach the netdev so tasks would not attempt to access it */
2180 mutex_lock(&mdev->state_lock);
2181 mdev->pndev[priv->port] = NULL;
2182 mdev->upper[priv->port] = NULL;
2183 mutex_unlock(&mdev->state_lock);
2185 #ifdef CONFIG_RFS_ACCEL
2186 mlx4_en_cleanup_filters(priv);
2189 mlx4_en_free_resources(priv);
2191 kfree(priv->tx_ring);
2198 static int mlx4_en_change_mtu(struct net_device *dev, int new_mtu)
2200 struct mlx4_en_priv *priv = netdev_priv(dev);
2201 struct mlx4_en_dev *mdev = priv->mdev;
2204 en_dbg(DRV, priv, "Change MTU called - current:%d new:%d\n",
2207 if ((new_mtu < MLX4_EN_MIN_MTU) || (new_mtu > priv->max_mtu)) {
2208 en_err(priv, "Bad MTU size:%d.\n", new_mtu);
2211 if (priv->xdp_ring_num && MLX4_EN_EFF_MTU(new_mtu) > FRAG_SZ0) {
2212 en_err(priv, "MTU size:%d requires frags but XDP running\n",
2218 if (netif_running(dev)) {
2219 mutex_lock(&mdev->state_lock);
2220 if (!mdev->device_up) {
2221 /* NIC is probably restarting - let watchdog task reset
2223 en_dbg(DRV, priv, "Change MTU called with card down!?\n");
2225 mlx4_en_stop_port(dev, 1);
2226 err = mlx4_en_start_port(dev);
2228 en_err(priv, "Failed restarting port:%d\n",
2230 queue_work(mdev->workqueue, &priv->watchdog_task);
2233 mutex_unlock(&mdev->state_lock);
2238 static int mlx4_en_hwtstamp_set(struct net_device *dev, struct ifreq *ifr)
2240 struct mlx4_en_priv *priv = netdev_priv(dev);
2241 struct mlx4_en_dev *mdev = priv->mdev;
2242 struct hwtstamp_config config;
2244 if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
2247 /* reserved for future extensions */
2251 /* device doesn't support time stamping */
2252 if (!(mdev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_TS))
2255 /* TX HW timestamp */
2256 switch (config.tx_type) {
2257 case HWTSTAMP_TX_OFF:
2258 case HWTSTAMP_TX_ON:
2264 /* RX HW timestamp */
2265 switch (config.rx_filter) {
2266 case HWTSTAMP_FILTER_NONE:
2268 case HWTSTAMP_FILTER_ALL:
2269 case HWTSTAMP_FILTER_SOME:
2270 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
2271 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
2272 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
2273 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
2274 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
2275 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
2276 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
2277 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
2278 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
2279 case HWTSTAMP_FILTER_PTP_V2_EVENT:
2280 case HWTSTAMP_FILTER_PTP_V2_SYNC:
2281 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
2282 config.rx_filter = HWTSTAMP_FILTER_ALL;
2288 if (mlx4_en_reset_config(dev, config, dev->features)) {
2289 config.tx_type = HWTSTAMP_TX_OFF;
2290 config.rx_filter = HWTSTAMP_FILTER_NONE;
2293 return copy_to_user(ifr->ifr_data, &config,
2294 sizeof(config)) ? -EFAULT : 0;
2297 static int mlx4_en_hwtstamp_get(struct net_device *dev, struct ifreq *ifr)
2299 struct mlx4_en_priv *priv = netdev_priv(dev);
2301 return copy_to_user(ifr->ifr_data, &priv->hwtstamp_config,
2302 sizeof(priv->hwtstamp_config)) ? -EFAULT : 0;
2305 static int mlx4_en_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
2309 return mlx4_en_hwtstamp_set(dev, ifr);
2311 return mlx4_en_hwtstamp_get(dev, ifr);
2317 static netdev_features_t mlx4_en_fix_features(struct net_device *netdev,
2318 netdev_features_t features)
2320 struct mlx4_en_priv *en_priv = netdev_priv(netdev);
2321 struct mlx4_en_dev *mdev = en_priv->mdev;
2323 /* Since there is no support for separate RX C-TAG/S-TAG vlan accel
2324 * enable/disable make sure S-TAG flag is always in same state as
2327 if (features & NETIF_F_HW_VLAN_CTAG_RX &&
2328 !(mdev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_SKIP_OUTER_VLAN))
2329 features |= NETIF_F_HW_VLAN_STAG_RX;
2331 features &= ~NETIF_F_HW_VLAN_STAG_RX;
2336 static int mlx4_en_set_features(struct net_device *netdev,
2337 netdev_features_t features)
2339 struct mlx4_en_priv *priv = netdev_priv(netdev);
2343 if (DEV_FEATURE_CHANGED(netdev, features, NETIF_F_RXFCS)) {
2344 en_info(priv, "Turn %s RX-FCS\n",
2345 (features & NETIF_F_RXFCS) ? "ON" : "OFF");
2349 if (DEV_FEATURE_CHANGED(netdev, features, NETIF_F_RXALL)) {
2350 u8 ignore_fcs_value = (features & NETIF_F_RXALL) ? 1 : 0;
2352 en_info(priv, "Turn %s RX-ALL\n",
2353 ignore_fcs_value ? "ON" : "OFF");
2354 ret = mlx4_SET_PORT_fcs_check(priv->mdev->dev,
2355 priv->port, ignore_fcs_value);
2360 if (DEV_FEATURE_CHANGED(netdev, features, NETIF_F_HW_VLAN_CTAG_RX)) {
2361 en_info(priv, "Turn %s RX vlan strip offload\n",
2362 (features & NETIF_F_HW_VLAN_CTAG_RX) ? "ON" : "OFF");
2366 if (DEV_FEATURE_CHANGED(netdev, features, NETIF_F_HW_VLAN_CTAG_TX))
2367 en_info(priv, "Turn %s TX vlan strip offload\n",
2368 (features & NETIF_F_HW_VLAN_CTAG_TX) ? "ON" : "OFF");
2370 if (DEV_FEATURE_CHANGED(netdev, features, NETIF_F_HW_VLAN_STAG_TX))
2371 en_info(priv, "Turn %s TX S-VLAN strip offload\n",
2372 (features & NETIF_F_HW_VLAN_STAG_TX) ? "ON" : "OFF");
2374 if (DEV_FEATURE_CHANGED(netdev, features, NETIF_F_LOOPBACK)) {
2375 en_info(priv, "Turn %s loopback\n",
2376 (features & NETIF_F_LOOPBACK) ? "ON" : "OFF");
2377 mlx4_en_update_loopback_state(netdev, features);
2381 ret = mlx4_en_reset_config(netdev, priv->hwtstamp_config,
2390 static int mlx4_en_set_vf_mac(struct net_device *dev, int queue, u8 *mac)
2392 struct mlx4_en_priv *en_priv = netdev_priv(dev);
2393 struct mlx4_en_dev *mdev = en_priv->mdev;
2394 u64 mac_u64 = mlx4_mac_to_u64(mac);
2396 if (is_multicast_ether_addr(mac))
2399 return mlx4_set_vf_mac(mdev->dev, en_priv->port, queue, mac_u64);
2402 static int mlx4_en_set_vf_vlan(struct net_device *dev, int vf, u16 vlan, u8 qos)
2404 struct mlx4_en_priv *en_priv = netdev_priv(dev);
2405 struct mlx4_en_dev *mdev = en_priv->mdev;
2407 return mlx4_set_vf_vlan(mdev->dev, en_priv->port, vf, vlan, qos);
2410 static int mlx4_en_set_vf_rate(struct net_device *dev, int vf, int min_tx_rate,
2413 struct mlx4_en_priv *en_priv = netdev_priv(dev);
2414 struct mlx4_en_dev *mdev = en_priv->mdev;
2416 return mlx4_set_vf_rate(mdev->dev, en_priv->port, vf, min_tx_rate,
2420 static int mlx4_en_set_vf_spoofchk(struct net_device *dev, int vf, bool setting)
2422 struct mlx4_en_priv *en_priv = netdev_priv(dev);
2423 struct mlx4_en_dev *mdev = en_priv->mdev;
2425 return mlx4_set_vf_spoofchk(mdev->dev, en_priv->port, vf, setting);
2428 static int mlx4_en_get_vf_config(struct net_device *dev, int vf, struct ifla_vf_info *ivf)
2430 struct mlx4_en_priv *en_priv = netdev_priv(dev);
2431 struct mlx4_en_dev *mdev = en_priv->mdev;
2433 return mlx4_get_vf_config(mdev->dev, en_priv->port, vf, ivf);
2436 static int mlx4_en_set_vf_link_state(struct net_device *dev, int vf, int link_state)
2438 struct mlx4_en_priv *en_priv = netdev_priv(dev);
2439 struct mlx4_en_dev *mdev = en_priv->mdev;
2441 return mlx4_set_vf_link_state(mdev->dev, en_priv->port, vf, link_state);
2444 static int mlx4_en_get_vf_stats(struct net_device *dev, int vf,
2445 struct ifla_vf_stats *vf_stats)
2447 struct mlx4_en_priv *en_priv = netdev_priv(dev);
2448 struct mlx4_en_dev *mdev = en_priv->mdev;
2450 return mlx4_get_vf_stats(mdev->dev, en_priv->port, vf, vf_stats);
2453 #define PORT_ID_BYTE_LEN 8
2454 static int mlx4_en_get_phys_port_id(struct net_device *dev,
2455 struct netdev_phys_item_id *ppid)
2457 struct mlx4_en_priv *priv = netdev_priv(dev);
2458 struct mlx4_dev *mdev = priv->mdev->dev;
2460 u64 phys_port_id = mdev->caps.phys_port_id[priv->port];
2465 ppid->id_len = sizeof(phys_port_id);
2466 for (i = PORT_ID_BYTE_LEN - 1; i >= 0; --i) {
2467 ppid->id[i] = phys_port_id & 0xff;
2473 static void mlx4_en_add_vxlan_offloads(struct work_struct *work)
2476 struct mlx4_en_priv *priv = container_of(work, struct mlx4_en_priv,
2479 ret = mlx4_config_vxlan_port(priv->mdev->dev, priv->vxlan_port);
2483 ret = mlx4_SET_PORT_VXLAN(priv->mdev->dev, priv->port,
2484 VXLAN_STEER_BY_OUTER_MAC, 1);
2487 en_err(priv, "failed setting L2 tunnel configuration ret %d\n", ret);
2492 priv->dev->hw_enc_features |= NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
2494 NETIF_F_TSO | NETIF_F_TSO6 |
2495 NETIF_F_GSO_UDP_TUNNEL |
2496 NETIF_F_GSO_UDP_TUNNEL_CSUM |
2497 NETIF_F_GSO_PARTIAL;
2500 static void mlx4_en_del_vxlan_offloads(struct work_struct *work)
2503 struct mlx4_en_priv *priv = container_of(work, struct mlx4_en_priv,
2505 /* unset offloads */
2506 priv->dev->hw_enc_features &= ~(NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
2508 NETIF_F_TSO | NETIF_F_TSO6 |
2509 NETIF_F_GSO_UDP_TUNNEL |
2510 NETIF_F_GSO_UDP_TUNNEL_CSUM |
2511 NETIF_F_GSO_PARTIAL);
2513 ret = mlx4_SET_PORT_VXLAN(priv->mdev->dev, priv->port,
2514 VXLAN_STEER_BY_OUTER_MAC, 0);
2516 en_err(priv, "failed setting L2 tunnel configuration ret %d\n", ret);
2518 priv->vxlan_port = 0;
2521 static void mlx4_en_add_vxlan_port(struct net_device *dev,
2522 struct udp_tunnel_info *ti)
2524 struct mlx4_en_priv *priv = netdev_priv(dev);
2525 __be16 port = ti->port;
2526 __be16 current_port;
2528 if (ti->type != UDP_TUNNEL_TYPE_VXLAN)
2531 if (ti->sa_family != AF_INET)
2534 if (priv->mdev->dev->caps.tunnel_offload_mode != MLX4_TUNNEL_OFFLOAD_MODE_VXLAN)
2537 current_port = priv->vxlan_port;
2538 if (current_port && current_port != port) {
2539 en_warn(priv, "vxlan port %d configured, can't add port %d\n",
2540 ntohs(current_port), ntohs(port));
2544 priv->vxlan_port = port;
2545 queue_work(priv->mdev->workqueue, &priv->vxlan_add_task);
2548 static void mlx4_en_del_vxlan_port(struct net_device *dev,
2549 struct udp_tunnel_info *ti)
2551 struct mlx4_en_priv *priv = netdev_priv(dev);
2552 __be16 port = ti->port;
2553 __be16 current_port;
2555 if (ti->type != UDP_TUNNEL_TYPE_VXLAN)
2558 if (ti->sa_family != AF_INET)
2561 if (priv->mdev->dev->caps.tunnel_offload_mode != MLX4_TUNNEL_OFFLOAD_MODE_VXLAN)
2564 current_port = priv->vxlan_port;
2565 if (current_port != port) {
2566 en_dbg(DRV, priv, "vxlan port %d isn't configured, ignoring\n", ntohs(port));
2570 queue_work(priv->mdev->workqueue, &priv->vxlan_del_task);
2573 static netdev_features_t mlx4_en_features_check(struct sk_buff *skb,
2574 struct net_device *dev,
2575 netdev_features_t features)
2577 features = vlan_features_check(skb, features);
2578 features = vxlan_features_check(skb, features);
2580 /* The ConnectX-3 doesn't support outer IPv6 checksums but it does
2581 * support inner IPv6 checksums and segmentation so we need to
2582 * strip that feature if this is an IPv6 encapsulated frame.
2584 if (skb->encapsulation &&
2585 (skb->ip_summed == CHECKSUM_PARTIAL)) {
2586 struct mlx4_en_priv *priv = netdev_priv(dev);
2588 if (!priv->vxlan_port ||
2589 (ip_hdr(skb)->version != 4) ||
2590 (udp_hdr(skb)->dest != priv->vxlan_port))
2591 features &= ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
2597 static int mlx4_en_set_tx_maxrate(struct net_device *dev, int queue_index, u32 maxrate)
2599 struct mlx4_en_priv *priv = netdev_priv(dev);
2600 struct mlx4_en_tx_ring *tx_ring = priv->tx_ring[queue_index];
2601 struct mlx4_update_qp_params params;
2604 if (!(priv->mdev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_QP_RATE_LIMIT))
2607 /* rate provided to us in Mbs, check if it fits into 12 bits, if not use Gbs */
2608 if (maxrate >> 12) {
2609 params.rate_unit = MLX4_QP_RATE_LIMIT_GBS;
2610 params.rate_val = maxrate / 1000;
2611 } else if (maxrate) {
2612 params.rate_unit = MLX4_QP_RATE_LIMIT_MBS;
2613 params.rate_val = maxrate;
2614 } else { /* zero serves to revoke the QP rate-limitation */
2615 params.rate_unit = 0;
2616 params.rate_val = 0;
2619 err = mlx4_update_qp(priv->mdev->dev, tx_ring->qpn, MLX4_UPDATE_QP_RATE_LIMIT,
2624 static int mlx4_xdp_set(struct net_device *dev, struct bpf_prog *prog)
2626 struct mlx4_en_priv *priv = netdev_priv(dev);
2627 struct mlx4_en_dev *mdev = priv->mdev;
2628 struct bpf_prog *old_prog;
2634 xdp_ring_num = prog ? ALIGN(priv->rx_ring_num, MLX4_EN_NUM_UP) : 0;
2636 /* No need to reconfigure buffers when simply swapping the
2637 * program for a new one.
2639 if (priv->xdp_ring_num == xdp_ring_num) {
2641 prog = bpf_prog_add(prog, priv->rx_ring_num - 1);
2643 return PTR_ERR(prog);
2645 for (i = 0; i < priv->rx_ring_num; i++) {
2646 /* This xchg is paired with READ_ONCE in the fastpath */
2647 old_prog = xchg(&priv->rx_ring[i]->xdp_prog, prog);
2649 bpf_prog_put(old_prog);
2654 if (priv->num_frags > 1) {
2655 en_err(priv, "Cannot set XDP if MTU requires multiple frags\n");
2659 if (priv->tx_ring_num < xdp_ring_num + MLX4_EN_NUM_UP) {
2661 "Minimum %d tx channels required to run XDP\n",
2662 (xdp_ring_num + MLX4_EN_NUM_UP) / MLX4_EN_NUM_UP);
2667 prog = bpf_prog_add(prog, priv->rx_ring_num - 1);
2669 return PTR_ERR(prog);
2672 mutex_lock(&mdev->state_lock);
2673 if (priv->port_up) {
2675 mlx4_en_stop_port(dev, 1);
2678 priv->xdp_ring_num = xdp_ring_num;
2679 netif_set_real_num_tx_queues(dev, priv->tx_ring_num -
2680 priv->xdp_ring_num);
2682 for (i = 0; i < priv->rx_ring_num; i++) {
2683 old_prog = xchg(&priv->rx_ring[i]->xdp_prog, prog);
2685 bpf_prog_put(old_prog);
2689 err = mlx4_en_start_port(dev);
2691 en_err(priv, "Failed starting port %d for XDP change\n",
2693 queue_work(mdev->workqueue, &priv->watchdog_task);
2697 mutex_unlock(&mdev->state_lock);
2701 static bool mlx4_xdp_attached(struct net_device *dev)
2703 struct mlx4_en_priv *priv = netdev_priv(dev);
2705 return !!priv->xdp_ring_num;
2708 static int mlx4_xdp(struct net_device *dev, struct netdev_xdp *xdp)
2710 switch (xdp->command) {
2711 case XDP_SETUP_PROG:
2712 return mlx4_xdp_set(dev, xdp->prog);
2713 case XDP_QUERY_PROG:
2714 xdp->prog_attached = mlx4_xdp_attached(dev);
2721 static const struct net_device_ops mlx4_netdev_ops = {
2722 .ndo_open = mlx4_en_open,
2723 .ndo_stop = mlx4_en_close,
2724 .ndo_start_xmit = mlx4_en_xmit,
2725 .ndo_select_queue = mlx4_en_select_queue,
2726 .ndo_get_stats64 = mlx4_en_get_stats64,
2727 .ndo_set_rx_mode = mlx4_en_set_rx_mode,
2728 .ndo_set_mac_address = mlx4_en_set_mac,
2729 .ndo_validate_addr = eth_validate_addr,
2730 .ndo_change_mtu = mlx4_en_change_mtu,
2731 .ndo_do_ioctl = mlx4_en_ioctl,
2732 .ndo_tx_timeout = mlx4_en_tx_timeout,
2733 .ndo_vlan_rx_add_vid = mlx4_en_vlan_rx_add_vid,
2734 .ndo_vlan_rx_kill_vid = mlx4_en_vlan_rx_kill_vid,
2735 #ifdef CONFIG_NET_POLL_CONTROLLER
2736 .ndo_poll_controller = mlx4_en_netpoll,
2738 .ndo_set_features = mlx4_en_set_features,
2739 .ndo_fix_features = mlx4_en_fix_features,
2740 .ndo_setup_tc = __mlx4_en_setup_tc,
2741 #ifdef CONFIG_RFS_ACCEL
2742 .ndo_rx_flow_steer = mlx4_en_filter_rfs,
2744 .ndo_get_phys_port_id = mlx4_en_get_phys_port_id,
2745 .ndo_udp_tunnel_add = mlx4_en_add_vxlan_port,
2746 .ndo_udp_tunnel_del = mlx4_en_del_vxlan_port,
2747 .ndo_features_check = mlx4_en_features_check,
2748 .ndo_set_tx_maxrate = mlx4_en_set_tx_maxrate,
2749 .ndo_xdp = mlx4_xdp,
2752 static const struct net_device_ops mlx4_netdev_ops_master = {
2753 .ndo_open = mlx4_en_open,
2754 .ndo_stop = mlx4_en_close,
2755 .ndo_start_xmit = mlx4_en_xmit,
2756 .ndo_select_queue = mlx4_en_select_queue,
2757 .ndo_get_stats64 = mlx4_en_get_stats64,
2758 .ndo_set_rx_mode = mlx4_en_set_rx_mode,
2759 .ndo_set_mac_address = mlx4_en_set_mac,
2760 .ndo_validate_addr = eth_validate_addr,
2761 .ndo_change_mtu = mlx4_en_change_mtu,
2762 .ndo_tx_timeout = mlx4_en_tx_timeout,
2763 .ndo_vlan_rx_add_vid = mlx4_en_vlan_rx_add_vid,
2764 .ndo_vlan_rx_kill_vid = mlx4_en_vlan_rx_kill_vid,
2765 .ndo_set_vf_mac = mlx4_en_set_vf_mac,
2766 .ndo_set_vf_vlan = mlx4_en_set_vf_vlan,
2767 .ndo_set_vf_rate = mlx4_en_set_vf_rate,
2768 .ndo_set_vf_spoofchk = mlx4_en_set_vf_spoofchk,
2769 .ndo_set_vf_link_state = mlx4_en_set_vf_link_state,
2770 .ndo_get_vf_stats = mlx4_en_get_vf_stats,
2771 .ndo_get_vf_config = mlx4_en_get_vf_config,
2772 #ifdef CONFIG_NET_POLL_CONTROLLER
2773 .ndo_poll_controller = mlx4_en_netpoll,
2775 .ndo_set_features = mlx4_en_set_features,
2776 .ndo_fix_features = mlx4_en_fix_features,
2777 .ndo_setup_tc = __mlx4_en_setup_tc,
2778 #ifdef CONFIG_RFS_ACCEL
2779 .ndo_rx_flow_steer = mlx4_en_filter_rfs,
2781 .ndo_get_phys_port_id = mlx4_en_get_phys_port_id,
2782 .ndo_udp_tunnel_add = mlx4_en_add_vxlan_port,
2783 .ndo_udp_tunnel_del = mlx4_en_del_vxlan_port,
2784 .ndo_features_check = mlx4_en_features_check,
2785 .ndo_set_tx_maxrate = mlx4_en_set_tx_maxrate,
2786 .ndo_xdp = mlx4_xdp,
2789 struct mlx4_en_bond {
2790 struct work_struct work;
2791 struct mlx4_en_priv *priv;
2793 struct mlx4_port_map port_map;
2796 static void mlx4_en_bond_work(struct work_struct *work)
2798 struct mlx4_en_bond *bond = container_of(work,
2799 struct mlx4_en_bond,
2802 struct mlx4_dev *dev = bond->priv->mdev->dev;
2804 if (bond->is_bonded) {
2805 if (!mlx4_is_bonded(dev)) {
2806 err = mlx4_bond(dev);
2808 en_err(bond->priv, "Fail to bond device\n");
2811 err = mlx4_port_map_set(dev, &bond->port_map);
2813 en_err(bond->priv, "Fail to set port map [%d][%d]: %d\n",
2814 bond->port_map.port1,
2815 bond->port_map.port2,
2818 } else if (mlx4_is_bonded(dev)) {
2819 err = mlx4_unbond(dev);
2821 en_err(bond->priv, "Fail to unbond device\n");
2823 dev_put(bond->priv->dev);
2827 static int mlx4_en_queue_bond_work(struct mlx4_en_priv *priv, int is_bonded,
2828 u8 v2p_p1, u8 v2p_p2)
2830 struct mlx4_en_bond *bond = NULL;
2832 bond = kzalloc(sizeof(*bond), GFP_ATOMIC);
2836 INIT_WORK(&bond->work, mlx4_en_bond_work);
2838 bond->is_bonded = is_bonded;
2839 bond->port_map.port1 = v2p_p1;
2840 bond->port_map.port2 = v2p_p2;
2841 dev_hold(priv->dev);
2842 queue_work(priv->mdev->workqueue, &bond->work);
2846 int mlx4_en_netdev_event(struct notifier_block *this,
2847 unsigned long event, void *ptr)
2849 struct net_device *ndev = netdev_notifier_info_to_dev(ptr);
2851 struct mlx4_en_dev *mdev;
2852 struct mlx4_dev *dev;
2853 int i, num_eth_ports = 0;
2854 bool do_bond = true;
2855 struct mlx4_en_priv *priv;
2859 if (!net_eq(dev_net(ndev), &init_net))
2862 mdev = container_of(this, struct mlx4_en_dev, nb);
2865 /* Go into this mode only when two network devices set on two ports
2866 * of the same mlx4 device are slaves of the same bonding master
2868 mlx4_foreach_port(i, dev, MLX4_PORT_TYPE_ETH) {
2870 if (!port && (mdev->pndev[i] == ndev))
2872 mdev->upper[i] = mdev->pndev[i] ?
2873 netdev_master_upper_dev_get(mdev->pndev[i]) : NULL;
2874 /* condition not met: network device is a slave */
2875 if (!mdev->upper[i])
2877 if (num_eth_ports < 2)
2879 /* condition not met: same master */
2880 if (mdev->upper[i] != mdev->upper[i-1])
2883 /* condition not met: 2 salves */
2884 do_bond = (num_eth_ports == 2) ? do_bond : false;
2886 /* handle only events that come with enough info */
2887 if ((do_bond && (event != NETDEV_BONDING_INFO)) || !port)
2890 priv = netdev_priv(ndev);
2892 struct netdev_notifier_bonding_info *notifier_info = ptr;
2893 struct netdev_bonding_info *bonding_info =
2894 ¬ifier_info->bonding_info;
2896 /* required mode 1, 2 or 4 */
2897 if ((bonding_info->master.bond_mode != BOND_MODE_ACTIVEBACKUP) &&
2898 (bonding_info->master.bond_mode != BOND_MODE_XOR) &&
2899 (bonding_info->master.bond_mode != BOND_MODE_8023AD))
2902 /* require exactly 2 slaves */
2903 if (bonding_info->master.num_slaves != 2)
2908 if (bonding_info->master.bond_mode ==
2909 BOND_MODE_ACTIVEBACKUP) {
2910 /* in active-backup mode virtual ports are
2911 * mapped to the physical port of the active
2913 if (bonding_info->slave.state ==
2914 BOND_STATE_BACKUP) {
2922 } else { /* BOND_STATE_ACTIVE */
2931 } else { /* Active-Active */
2932 /* in active-active mode a virtual port is
2933 * mapped to the native physical port if and only
2934 * if the physical port is up */
2935 __s8 link = bonding_info->slave.link;
2941 if ((link == BOND_LINK_UP) ||
2942 (link == BOND_LINK_FAIL)) {
2947 } else { /* BOND_LINK_DOWN || BOND_LINK_BACK */
2957 mlx4_en_queue_bond_work(priv, do_bond,
2958 v2p_port1, v2p_port2);
2963 void mlx4_en_update_pfc_stats_bitmap(struct mlx4_dev *dev,
2964 struct mlx4_en_stats_bitmap *stats_bitmap,
2965 u8 rx_ppp, u8 rx_pause,
2966 u8 tx_ppp, u8 tx_pause)
2968 int last_i = NUM_MAIN_STATS + NUM_PORT_STATS + NUM_PF_STATS;
2970 if (!mlx4_is_slave(dev) &&
2971 (dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_FLOWSTATS_EN)) {
2972 mutex_lock(&stats_bitmap->mutex);
2973 bitmap_clear(stats_bitmap->bitmap, last_i, NUM_FLOW_STATS);
2976 bitmap_set(stats_bitmap->bitmap, last_i,
2977 NUM_FLOW_PRIORITY_STATS_RX);
2978 last_i += NUM_FLOW_PRIORITY_STATS_RX;
2980 if (rx_pause && !(rx_ppp))
2981 bitmap_set(stats_bitmap->bitmap, last_i,
2983 last_i += NUM_FLOW_STATS_RX;
2986 bitmap_set(stats_bitmap->bitmap, last_i,
2987 NUM_FLOW_PRIORITY_STATS_TX);
2988 last_i += NUM_FLOW_PRIORITY_STATS_TX;
2990 if (tx_pause && !(tx_ppp))
2991 bitmap_set(stats_bitmap->bitmap, last_i,
2993 last_i += NUM_FLOW_STATS_TX;
2995 mutex_unlock(&stats_bitmap->mutex);
2999 void mlx4_en_set_stats_bitmap(struct mlx4_dev *dev,
3000 struct mlx4_en_stats_bitmap *stats_bitmap,
3001 u8 rx_ppp, u8 rx_pause,
3002 u8 tx_ppp, u8 tx_pause)
3006 mutex_init(&stats_bitmap->mutex);
3007 bitmap_zero(stats_bitmap->bitmap, NUM_ALL_STATS);
3009 if (mlx4_is_slave(dev)) {
3010 bitmap_set(stats_bitmap->bitmap, last_i +
3011 MLX4_FIND_NETDEV_STAT(rx_packets), 1);
3012 bitmap_set(stats_bitmap->bitmap, last_i +
3013 MLX4_FIND_NETDEV_STAT(tx_packets), 1);
3014 bitmap_set(stats_bitmap->bitmap, last_i +
3015 MLX4_FIND_NETDEV_STAT(rx_bytes), 1);
3016 bitmap_set(stats_bitmap->bitmap, last_i +
3017 MLX4_FIND_NETDEV_STAT(tx_bytes), 1);
3018 bitmap_set(stats_bitmap->bitmap, last_i +
3019 MLX4_FIND_NETDEV_STAT(rx_dropped), 1);
3020 bitmap_set(stats_bitmap->bitmap, last_i +
3021 MLX4_FIND_NETDEV_STAT(tx_dropped), 1);
3023 bitmap_set(stats_bitmap->bitmap, last_i, NUM_MAIN_STATS);
3025 last_i += NUM_MAIN_STATS;
3027 bitmap_set(stats_bitmap->bitmap, last_i, NUM_PORT_STATS);
3028 last_i += NUM_PORT_STATS;
3030 if (mlx4_is_master(dev))
3031 bitmap_set(stats_bitmap->bitmap, last_i,
3033 last_i += NUM_PF_STATS;
3035 mlx4_en_update_pfc_stats_bitmap(dev, stats_bitmap,
3038 last_i += NUM_FLOW_STATS;
3040 if (!mlx4_is_slave(dev))
3041 bitmap_set(stats_bitmap->bitmap, last_i, NUM_PKT_STATS);
3044 int mlx4_en_init_netdev(struct mlx4_en_dev *mdev, int port,
3045 struct mlx4_en_port_profile *prof)
3047 struct net_device *dev;
3048 struct mlx4_en_priv *priv;
3051 #ifdef CONFIG_MLX4_EN_DCB
3052 struct tc_configuration *tc;
3055 dev = alloc_etherdev_mqs(sizeof(struct mlx4_en_priv),
3056 MAX_TX_RINGS, MAX_RX_RINGS);
3060 netif_set_real_num_tx_queues(dev, prof->tx_ring_num);
3061 netif_set_real_num_rx_queues(dev, prof->rx_ring_num);
3063 SET_NETDEV_DEV(dev, &mdev->dev->persist->pdev->dev);
3064 dev->dev_port = port - 1;
3067 * Initialize driver private data
3070 priv = netdev_priv(dev);
3071 memset(priv, 0, sizeof(struct mlx4_en_priv));
3072 priv->counter_index = MLX4_SINK_COUNTER_INDEX(mdev->dev);
3073 spin_lock_init(&priv->stats_lock);
3074 INIT_WORK(&priv->rx_mode_task, mlx4_en_do_set_rx_mode);
3075 INIT_WORK(&priv->watchdog_task, mlx4_en_restart);
3076 INIT_WORK(&priv->linkstate_task, mlx4_en_linkstate);
3077 INIT_DELAYED_WORK(&priv->stats_task, mlx4_en_do_get_stats);
3078 INIT_DELAYED_WORK(&priv->service_task, mlx4_en_service_task);
3079 INIT_WORK(&priv->vxlan_add_task, mlx4_en_add_vxlan_offloads);
3080 INIT_WORK(&priv->vxlan_del_task, mlx4_en_del_vxlan_offloads);
3081 #ifdef CONFIG_RFS_ACCEL
3082 INIT_LIST_HEAD(&priv->filters);
3083 spin_lock_init(&priv->filters_lock);
3088 priv->ddev = &mdev->pdev->dev;
3091 priv->port_up = false;
3092 priv->flags = prof->flags;
3093 priv->pflags = MLX4_EN_PRIV_FLAGS_BLUEFLAME;
3094 priv->ctrl_flags = cpu_to_be32(MLX4_WQE_CTRL_CQ_UPDATE |
3095 MLX4_WQE_CTRL_SOLICITED);
3096 priv->num_tx_rings_p_up = mdev->profile.num_tx_rings_p_up;
3097 priv->tx_ring_num = prof->tx_ring_num;
3098 priv->tx_work_limit = MLX4_EN_DEFAULT_TX_WORK;
3099 netdev_rss_key_fill(priv->rss_key, sizeof(priv->rss_key));
3101 priv->tx_ring = kzalloc(sizeof(struct mlx4_en_tx_ring *) * MAX_TX_RINGS,
3103 if (!priv->tx_ring) {
3107 priv->tx_cq = kzalloc(sizeof(struct mlx4_en_cq *) * MAX_TX_RINGS,
3113 priv->rx_ring_num = prof->rx_ring_num;
3114 priv->cqe_factor = (mdev->dev->caps.cqe_size == 64) ? 1 : 0;
3115 priv->cqe_size = mdev->dev->caps.cqe_size;
3116 priv->mac_index = -1;
3117 priv->msg_enable = MLX4_EN_MSG_LEVEL;
3118 #ifdef CONFIG_MLX4_EN_DCB
3119 if (!mlx4_is_slave(priv->mdev->dev)) {
3120 priv->cee_params.dcbx_cap = DCB_CAP_DCBX_VER_CEE |
3122 DCB_CAP_DCBX_VER_IEEE;
3123 priv->flags |= MLX4_EN_DCB_ENABLED;
3124 priv->cee_params.dcb_cfg.pfc_state = false;
3126 for (i = 0; i < MLX4_EN_NUM_UP; i++) {
3127 tc = &priv->cee_params.dcb_cfg.tc_config[i];
3128 tc->dcb_pfc = pfc_disabled;
3131 if (mdev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_ETS_CFG) {
3132 dev->dcbnl_ops = &mlx4_en_dcbnl_ops;
3134 en_info(priv, "enabling only PFC DCB ops\n");
3135 dev->dcbnl_ops = &mlx4_en_dcbnl_pfc_ops;
3140 for (i = 0; i < MLX4_EN_MAC_HASH_SIZE; ++i)
3141 INIT_HLIST_HEAD(&priv->mac_hash[i]);
3143 /* Query for default mac and max mtu */
3144 priv->max_mtu = mdev->dev->caps.eth_mtu_cap[priv->port];
3146 if (mdev->dev->caps.rx_checksum_flags_port[priv->port] &
3147 MLX4_RX_CSUM_MODE_VAL_NON_TCP_UDP)
3148 priv->flags |= MLX4_EN_FLAG_RX_CSUM_NON_TCP_UDP;
3150 /* Set default MAC */
3151 dev->addr_len = ETH_ALEN;
3152 mlx4_en_u64_to_mac(dev->dev_addr, mdev->dev->caps.def_mac[priv->port]);
3153 if (!is_valid_ether_addr(dev->dev_addr)) {
3154 en_err(priv, "Port: %d, invalid mac burned: %pM, quiting\n",
3155 priv->port, dev->dev_addr);
3158 } else if (mlx4_is_slave(priv->mdev->dev) &&
3159 (priv->mdev->dev->port_random_macs & 1 << priv->port)) {
3160 /* Random MAC was assigned in mlx4_slave_cap
3161 * in mlx4_core module
3163 dev->addr_assign_type |= NET_ADDR_RANDOM;
3164 en_warn(priv, "Assigned random MAC address %pM\n", dev->dev_addr);
3167 memcpy(priv->current_mac, dev->dev_addr, sizeof(priv->current_mac));
3169 priv->stride = roundup_pow_of_two(sizeof(struct mlx4_en_rx_desc) +
3170 DS_SIZE * MLX4_EN_MAX_RX_FRAGS);
3171 err = mlx4_en_alloc_resources(priv);
3175 /* Initialize time stamping config */
3176 priv->hwtstamp_config.flags = 0;
3177 priv->hwtstamp_config.tx_type = HWTSTAMP_TX_OFF;
3178 priv->hwtstamp_config.rx_filter = HWTSTAMP_FILTER_NONE;
3180 /* Allocate page for receive rings */
3181 err = mlx4_alloc_hwq_res(mdev->dev, &priv->res,
3184 en_err(priv, "Failed to allocate page for rx qps\n");
3187 priv->allocated = 1;
3190 * Initialize netdev entry points
3192 if (mlx4_is_master(priv->mdev->dev))
3193 dev->netdev_ops = &mlx4_netdev_ops_master;
3195 dev->netdev_ops = &mlx4_netdev_ops;
3196 dev->watchdog_timeo = MLX4_EN_WATCHDOG_TIMEOUT;
3197 netif_set_real_num_tx_queues(dev, priv->tx_ring_num);
3198 netif_set_real_num_rx_queues(dev, priv->rx_ring_num);
3200 dev->ethtool_ops = &mlx4_en_ethtool_ops;
3203 * Set driver features
3205 dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM;
3206 if (mdev->LSO_support)
3207 dev->hw_features |= NETIF_F_TSO | NETIF_F_TSO6;
3209 dev->vlan_features = dev->hw_features;
3211 dev->hw_features |= NETIF_F_RXCSUM | NETIF_F_RXHASH;
3212 dev->features = dev->hw_features | NETIF_F_HIGHDMA |
3213 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX |
3214 NETIF_F_HW_VLAN_CTAG_FILTER;
3215 dev->hw_features |= NETIF_F_LOOPBACK |
3216 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX;
3218 if (!(mdev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_SKIP_OUTER_VLAN)) {
3219 dev->features |= NETIF_F_HW_VLAN_STAG_RX |
3220 NETIF_F_HW_VLAN_STAG_FILTER;
3221 dev->hw_features |= NETIF_F_HW_VLAN_STAG_RX;
3224 if (mlx4_is_slave(mdev->dev)) {
3227 err = get_phv_bit(mdev->dev, port, &phv);
3229 dev->hw_features |= NETIF_F_HW_VLAN_STAG_TX;
3230 priv->pflags |= MLX4_EN_PRIV_FLAGS_PHV;
3233 if (mdev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_PHV_EN &&
3234 !(mdev->dev->caps.flags2 &
3235 MLX4_DEV_CAP_FLAG2_SKIP_OUTER_VLAN))
3236 dev->hw_features |= NETIF_F_HW_VLAN_STAG_TX;
3239 if (mdev->dev->caps.flags & MLX4_DEV_CAP_FLAG_FCS_KEEP)
3240 dev->hw_features |= NETIF_F_RXFCS;
3242 if (mdev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_IGNORE_FCS)
3243 dev->hw_features |= NETIF_F_RXALL;
3245 if (mdev->dev->caps.steering_mode ==
3246 MLX4_STEERING_MODE_DEVICE_MANAGED &&
3247 mdev->dev->caps.dmfs_high_steer_mode != MLX4_STEERING_DMFS_A0_STATIC)
3248 dev->hw_features |= NETIF_F_NTUPLE;
3250 if (mdev->dev->caps.steering_mode != MLX4_STEERING_MODE_A0)
3251 dev->priv_flags |= IFF_UNICAST_FLT;
3253 /* Setting a default hash function value */
3254 if (mdev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_RSS_TOP) {
3255 priv->rss_hash_fn = ETH_RSS_HASH_TOP;
3256 } else if (mdev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_RSS_XOR) {
3257 priv->rss_hash_fn = ETH_RSS_HASH_XOR;
3260 "No RSS hash capabilities exposed, using Toeplitz\n");
3261 priv->rss_hash_fn = ETH_RSS_HASH_TOP;
3264 if (mdev->dev->caps.tunnel_offload_mode == MLX4_TUNNEL_OFFLOAD_MODE_VXLAN) {
3265 dev->hw_features |= NETIF_F_GSO_UDP_TUNNEL |
3266 NETIF_F_GSO_UDP_TUNNEL_CSUM |
3267 NETIF_F_GSO_PARTIAL;
3268 dev->features |= NETIF_F_GSO_UDP_TUNNEL |
3269 NETIF_F_GSO_UDP_TUNNEL_CSUM |
3270 NETIF_F_GSO_PARTIAL;
3271 dev->gso_partial_features = NETIF_F_GSO_UDP_TUNNEL_CSUM;
3274 mdev->pndev[port] = dev;
3275 mdev->upper[port] = NULL;
3277 netif_carrier_off(dev);
3278 mlx4_en_set_default_moderation(priv);
3280 en_warn(priv, "Using %d TX rings\n", prof->tx_ring_num);
3281 en_warn(priv, "Using %d RX rings\n", prof->rx_ring_num);
3283 mlx4_en_update_loopback_state(priv->dev, priv->dev->features);
3285 /* Configure port */
3286 mlx4_en_calc_rx_buf(dev);
3287 err = mlx4_SET_PORT_general(mdev->dev, priv->port,
3288 priv->rx_skb_size + ETH_FCS_LEN,
3289 prof->tx_pause, prof->tx_ppp,
3290 prof->rx_pause, prof->rx_ppp);
3292 en_err(priv, "Failed setting port general configurations for port %d, with error %d\n",
3297 if (mdev->dev->caps.tunnel_offload_mode == MLX4_TUNNEL_OFFLOAD_MODE_VXLAN) {
3298 err = mlx4_SET_PORT_VXLAN(mdev->dev, priv->port, VXLAN_STEER_BY_OUTER_MAC, 1);
3300 en_err(priv, "Failed setting port L2 tunnel configuration, err %d\n",
3307 en_warn(priv, "Initializing port\n");
3308 err = mlx4_INIT_PORT(mdev->dev, priv->port);
3310 en_err(priv, "Failed Initializing port\n");
3313 queue_delayed_work(mdev->workqueue, &priv->stats_task, STATS_DELAY);
3315 /* Initialize time stamp mechanism */
3316 if (mdev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_TS)
3317 mlx4_en_init_timestamp(mdev);
3319 queue_delayed_work(mdev->workqueue, &priv->service_task,
3320 SERVICE_TASK_DELAY);
3322 mlx4_en_set_stats_bitmap(mdev->dev, &priv->stats_bitmap,
3323 mdev->profile.prof[priv->port].rx_ppp,
3324 mdev->profile.prof[priv->port].rx_pause,
3325 mdev->profile.prof[priv->port].tx_ppp,
3326 mdev->profile.prof[priv->port].tx_pause);
3328 err = register_netdev(dev);
3330 en_err(priv, "Netdev registration failed for port %d\n", port);
3334 priv->registered = 1;
3335 devlink_port_type_eth_set(mlx4_get_devlink_port(mdev->dev, priv->port),
3341 mlx4_en_destroy_netdev(dev);
3345 int mlx4_en_reset_config(struct net_device *dev,
3346 struct hwtstamp_config ts_config,
3347 netdev_features_t features)
3349 struct mlx4_en_priv *priv = netdev_priv(dev);
3350 struct mlx4_en_dev *mdev = priv->mdev;
3351 struct mlx4_en_port_profile new_prof;
3352 struct mlx4_en_priv *tmp;
3356 if (priv->hwtstamp_config.tx_type == ts_config.tx_type &&
3357 priv->hwtstamp_config.rx_filter == ts_config.rx_filter &&
3358 !DEV_FEATURE_CHANGED(dev, features, NETIF_F_HW_VLAN_CTAG_RX) &&
3359 !DEV_FEATURE_CHANGED(dev, features, NETIF_F_RXFCS))
3360 return 0; /* Nothing to change */
3362 if (DEV_FEATURE_CHANGED(dev, features, NETIF_F_HW_VLAN_CTAG_RX) &&
3363 (features & NETIF_F_HW_VLAN_CTAG_RX) &&
3364 (priv->hwtstamp_config.rx_filter != HWTSTAMP_FILTER_NONE)) {
3365 en_warn(priv, "Can't turn ON rx vlan offload while time-stamping rx filter is ON\n");
3369 tmp = kzalloc(sizeof(*tmp), GFP_KERNEL);
3373 mutex_lock(&mdev->state_lock);
3375 memcpy(&new_prof, priv->prof, sizeof(struct mlx4_en_port_profile));
3376 memcpy(&new_prof.hwtstamp_config, &ts_config, sizeof(ts_config));
3378 err = mlx4_en_try_alloc_resources(priv, tmp, &new_prof);
3382 if (priv->port_up) {
3384 mlx4_en_stop_port(dev, 1);
3387 en_warn(priv, "Changing device configuration rx filter(%x) rx vlan(%x)\n",
3388 ts_config.rx_filter,
3389 !!(features & NETIF_F_HW_VLAN_CTAG_RX));
3391 mlx4_en_safe_replace_resources(priv, tmp);
3393 if (DEV_FEATURE_CHANGED(dev, features, NETIF_F_HW_VLAN_CTAG_RX)) {
3394 if (features & NETIF_F_HW_VLAN_CTAG_RX)
3395 dev->features |= NETIF_F_HW_VLAN_CTAG_RX;
3397 dev->features &= ~NETIF_F_HW_VLAN_CTAG_RX;
3398 } else if (ts_config.rx_filter == HWTSTAMP_FILTER_NONE) {
3399 /* RX time-stamping is OFF, update the RX vlan offload
3400 * to the latest wanted state
3402 if (dev->wanted_features & NETIF_F_HW_VLAN_CTAG_RX)
3403 dev->features |= NETIF_F_HW_VLAN_CTAG_RX;
3405 dev->features &= ~NETIF_F_HW_VLAN_CTAG_RX;
3408 if (DEV_FEATURE_CHANGED(dev, features, NETIF_F_RXFCS)) {
3409 if (features & NETIF_F_RXFCS)
3410 dev->features |= NETIF_F_RXFCS;
3412 dev->features &= ~NETIF_F_RXFCS;
3415 /* RX vlan offload and RX time-stamping can't co-exist !
3416 * Regardless of the caller's choice,
3417 * Turn Off RX vlan offload in case of time-stamping is ON
3419 if (ts_config.rx_filter != HWTSTAMP_FILTER_NONE) {
3420 if (dev->features & NETIF_F_HW_VLAN_CTAG_RX)
3421 en_warn(priv, "Turning off RX vlan offload since RX time-stamping is ON\n");
3422 dev->features &= ~NETIF_F_HW_VLAN_CTAG_RX;
3426 err = mlx4_en_start_port(dev);
3428 en_err(priv, "Failed starting port\n");
3432 mutex_unlock(&mdev->state_lock);
3435 netdev_features_change(dev);