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)
66 netdev_set_num_tc(dev, up);
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 static int __mlx4_en_setup_tc(struct net_device *dev, u32 handle, __be16 proto,
90 struct tc_to_netdev *tc)
92 if (tc->type != TC_SETUP_MQPRIO)
95 return mlx4_en_setup_tc(dev, tc->tc);
98 #ifdef CONFIG_RFS_ACCEL
100 struct mlx4_en_filter {
101 struct list_head next;
102 struct work_struct work;
111 struct mlx4_en_priv *priv;
112 u32 flow_id; /* RFS infrastructure id */
113 int id; /* mlx4_en driver id */
114 u64 reg_id; /* Flow steering API id */
115 u8 activated; /* Used to prevent expiry before filter
118 struct hlist_node filter_chain;
121 static void mlx4_en_filter_rfs_expire(struct mlx4_en_priv *priv);
123 static enum mlx4_net_trans_rule_id mlx4_ip_proto_to_trans_rule_id(u8 ip_proto)
127 return MLX4_NET_TRANS_RULE_ID_UDP;
129 return MLX4_NET_TRANS_RULE_ID_TCP;
131 return MLX4_NET_TRANS_RULE_NUM;
135 /* Must not acquire state_lock, as its corresponding work_sync
138 static void mlx4_en_filter_work(struct work_struct *work)
140 struct mlx4_en_filter *filter = container_of(work,
141 struct mlx4_en_filter,
143 struct mlx4_en_priv *priv = filter->priv;
144 struct mlx4_spec_list spec_tcp_udp = {
145 .id = mlx4_ip_proto_to_trans_rule_id(filter->ip_proto),
148 .dst_port = filter->dst_port,
149 .dst_port_msk = (__force __be16)-1,
150 .src_port = filter->src_port,
151 .src_port_msk = (__force __be16)-1,
155 struct mlx4_spec_list spec_ip = {
156 .id = MLX4_NET_TRANS_RULE_ID_IPV4,
159 .dst_ip = filter->dst_ip,
160 .dst_ip_msk = (__force __be32)-1,
161 .src_ip = filter->src_ip,
162 .src_ip_msk = (__force __be32)-1,
166 struct mlx4_spec_list spec_eth = {
167 .id = MLX4_NET_TRANS_RULE_ID_ETH,
169 struct mlx4_net_trans_rule rule = {
170 .list = LIST_HEAD_INIT(rule.list),
171 .queue_mode = MLX4_NET_TRANS_Q_LIFO,
174 .promisc_mode = MLX4_FS_REGULAR,
176 .priority = MLX4_DOMAIN_RFS,
179 __be64 mac_mask = cpu_to_be64(MLX4_MAC_MASK << 16);
181 if (spec_tcp_udp.id >= MLX4_NET_TRANS_RULE_NUM) {
182 en_warn(priv, "RFS: ignoring unsupported ip protocol (%d)\n",
186 list_add_tail(&spec_eth.list, &rule.list);
187 list_add_tail(&spec_ip.list, &rule.list);
188 list_add_tail(&spec_tcp_udp.list, &rule.list);
190 rule.qpn = priv->rss_map.qps[filter->rxq_index].qpn;
191 memcpy(spec_eth.eth.dst_mac, priv->dev->dev_addr, ETH_ALEN);
192 memcpy(spec_eth.eth.dst_mac_msk, &mac_mask, ETH_ALEN);
194 filter->activated = 0;
196 if (filter->reg_id) {
197 rc = mlx4_flow_detach(priv->mdev->dev, filter->reg_id);
198 if (rc && rc != -ENOENT)
199 en_err(priv, "Error detaching flow. rc = %d\n", rc);
202 rc = mlx4_flow_attach(priv->mdev->dev, &rule, &filter->reg_id);
204 en_err(priv, "Error attaching flow. err = %d\n", rc);
207 mlx4_en_filter_rfs_expire(priv);
209 filter->activated = 1;
212 static inline struct hlist_head *
213 filter_hash_bucket(struct mlx4_en_priv *priv, __be32 src_ip, __be32 dst_ip,
214 __be16 src_port, __be16 dst_port)
219 l = (__force unsigned long)src_port |
220 ((__force unsigned long)dst_port << 2);
221 l ^= (__force unsigned long)(src_ip ^ dst_ip);
223 bucket_idx = hash_long(l, MLX4_EN_FILTER_HASH_SHIFT);
225 return &priv->filter_hash[bucket_idx];
228 static struct mlx4_en_filter *
229 mlx4_en_filter_alloc(struct mlx4_en_priv *priv, int rxq_index, __be32 src_ip,
230 __be32 dst_ip, u8 ip_proto, __be16 src_port,
231 __be16 dst_port, u32 flow_id)
233 struct mlx4_en_filter *filter = NULL;
235 filter = kzalloc(sizeof(struct mlx4_en_filter), GFP_ATOMIC);
240 filter->rxq_index = rxq_index;
241 INIT_WORK(&filter->work, mlx4_en_filter_work);
243 filter->src_ip = src_ip;
244 filter->dst_ip = dst_ip;
245 filter->ip_proto = ip_proto;
246 filter->src_port = src_port;
247 filter->dst_port = dst_port;
249 filter->flow_id = flow_id;
251 filter->id = priv->last_filter_id++ % RPS_NO_FILTER;
253 list_add_tail(&filter->next, &priv->filters);
254 hlist_add_head(&filter->filter_chain,
255 filter_hash_bucket(priv, src_ip, dst_ip, src_port,
261 static void mlx4_en_filter_free(struct mlx4_en_filter *filter)
263 struct mlx4_en_priv *priv = filter->priv;
266 list_del(&filter->next);
268 rc = mlx4_flow_detach(priv->mdev->dev, filter->reg_id);
269 if (rc && rc != -ENOENT)
270 en_err(priv, "Error detaching flow. rc = %d\n", rc);
275 static inline struct mlx4_en_filter *
276 mlx4_en_filter_find(struct mlx4_en_priv *priv, __be32 src_ip, __be32 dst_ip,
277 u8 ip_proto, __be16 src_port, __be16 dst_port)
279 struct mlx4_en_filter *filter;
280 struct mlx4_en_filter *ret = NULL;
282 hlist_for_each_entry(filter,
283 filter_hash_bucket(priv, src_ip, dst_ip,
286 if (filter->src_ip == src_ip &&
287 filter->dst_ip == dst_ip &&
288 filter->ip_proto == ip_proto &&
289 filter->src_port == src_port &&
290 filter->dst_port == dst_port) {
300 mlx4_en_filter_rfs(struct net_device *net_dev, const struct sk_buff *skb,
301 u16 rxq_index, u32 flow_id)
303 struct mlx4_en_priv *priv = netdev_priv(net_dev);
304 struct mlx4_en_filter *filter;
305 const struct iphdr *ip;
312 int nhoff = skb_network_offset(skb);
315 if (skb->protocol != htons(ETH_P_IP))
316 return -EPROTONOSUPPORT;
318 ip = (const struct iphdr *)(skb->data + nhoff);
319 if (ip_is_fragment(ip))
320 return -EPROTONOSUPPORT;
322 if ((ip->protocol != IPPROTO_TCP) && (ip->protocol != IPPROTO_UDP))
323 return -EPROTONOSUPPORT;
324 ports = (const __be16 *)(skb->data + nhoff + 4 * ip->ihl);
326 ip_proto = ip->protocol;
332 spin_lock_bh(&priv->filters_lock);
333 filter = mlx4_en_filter_find(priv, src_ip, dst_ip, ip_proto,
336 if (filter->rxq_index == rxq_index)
339 filter->rxq_index = rxq_index;
341 filter = mlx4_en_filter_alloc(priv, rxq_index,
342 src_ip, dst_ip, ip_proto,
343 src_port, dst_port, flow_id);
350 queue_work(priv->mdev->workqueue, &filter->work);
355 spin_unlock_bh(&priv->filters_lock);
360 void mlx4_en_cleanup_filters(struct mlx4_en_priv *priv)
362 struct mlx4_en_filter *filter, *tmp;
365 spin_lock_bh(&priv->filters_lock);
366 list_for_each_entry_safe(filter, tmp, &priv->filters, next) {
367 list_move(&filter->next, &del_list);
368 hlist_del(&filter->filter_chain);
370 spin_unlock_bh(&priv->filters_lock);
372 list_for_each_entry_safe(filter, tmp, &del_list, next) {
373 cancel_work_sync(&filter->work);
374 mlx4_en_filter_free(filter);
378 static void mlx4_en_filter_rfs_expire(struct mlx4_en_priv *priv)
380 struct mlx4_en_filter *filter = NULL, *tmp, *last_filter = NULL;
384 spin_lock_bh(&priv->filters_lock);
385 list_for_each_entry_safe(filter, tmp, &priv->filters, next) {
386 if (i > MLX4_EN_FILTER_EXPIRY_QUOTA)
389 if (filter->activated &&
390 !work_pending(&filter->work) &&
391 rps_may_expire_flow(priv->dev,
392 filter->rxq_index, filter->flow_id,
394 list_move(&filter->next, &del_list);
395 hlist_del(&filter->filter_chain);
397 last_filter = filter;
402 if (last_filter && (&last_filter->next != priv->filters.next))
403 list_move(&priv->filters, &last_filter->next);
405 spin_unlock_bh(&priv->filters_lock);
407 list_for_each_entry_safe(filter, tmp, &del_list, next)
408 mlx4_en_filter_free(filter);
412 static int mlx4_en_vlan_rx_add_vid(struct net_device *dev,
413 __be16 proto, u16 vid)
415 struct mlx4_en_priv *priv = netdev_priv(dev);
416 struct mlx4_en_dev *mdev = priv->mdev;
420 en_dbg(HW, priv, "adding VLAN:%d\n", vid);
422 set_bit(vid, priv->active_vlans);
424 /* Add VID to port VLAN filter */
425 mutex_lock(&mdev->state_lock);
426 if (mdev->device_up && priv->port_up) {
427 err = mlx4_SET_VLAN_FLTR(mdev->dev, priv);
429 en_err(priv, "Failed configuring VLAN filter\n");
433 err = mlx4_register_vlan(mdev->dev, priv->port, vid, &idx);
435 en_dbg(HW, priv, "Failed adding vlan %d\n", vid);
438 mutex_unlock(&mdev->state_lock);
442 static int mlx4_en_vlan_rx_kill_vid(struct net_device *dev,
443 __be16 proto, u16 vid)
445 struct mlx4_en_priv *priv = netdev_priv(dev);
446 struct mlx4_en_dev *mdev = priv->mdev;
449 en_dbg(HW, priv, "Killing VID:%d\n", vid);
451 clear_bit(vid, priv->active_vlans);
453 /* Remove VID from port VLAN filter */
454 mutex_lock(&mdev->state_lock);
455 mlx4_unregister_vlan(mdev->dev, priv->port, vid);
457 if (mdev->device_up && priv->port_up) {
458 err = mlx4_SET_VLAN_FLTR(mdev->dev, priv);
460 en_err(priv, "Failed configuring VLAN filter\n");
462 mutex_unlock(&mdev->state_lock);
467 static void mlx4_en_u64_to_mac(unsigned char dst_mac[ETH_ALEN + 2], u64 src_mac)
470 for (i = ETH_ALEN - 1; i >= 0; --i) {
471 dst_mac[i] = src_mac & 0xff;
474 memset(&dst_mac[ETH_ALEN], 0, 2);
478 static int mlx4_en_tunnel_steer_add(struct mlx4_en_priv *priv, unsigned char *addr,
479 int qpn, u64 *reg_id)
483 if (priv->mdev->dev->caps.tunnel_offload_mode != MLX4_TUNNEL_OFFLOAD_MODE_VXLAN ||
484 priv->mdev->dev->caps.dmfs_high_steer_mode == MLX4_STEERING_DMFS_A0_STATIC)
485 return 0; /* do nothing */
487 err = mlx4_tunnel_steer_add(priv->mdev->dev, addr, priv->port, qpn,
488 MLX4_DOMAIN_NIC, reg_id);
490 en_err(priv, "failed to add vxlan steering rule, err %d\n", err);
493 en_dbg(DRV, priv, "added vxlan steering rule, mac %pM reg_id %llx\n", addr, *reg_id);
498 static int mlx4_en_uc_steer_add(struct mlx4_en_priv *priv,
499 unsigned char *mac, int *qpn, u64 *reg_id)
501 struct mlx4_en_dev *mdev = priv->mdev;
502 struct mlx4_dev *dev = mdev->dev;
505 switch (dev->caps.steering_mode) {
506 case MLX4_STEERING_MODE_B0: {
511 memcpy(&gid[10], mac, ETH_ALEN);
514 err = mlx4_unicast_attach(dev, &qp, gid, 0, MLX4_PROT_ETH);
517 case MLX4_STEERING_MODE_DEVICE_MANAGED: {
518 struct mlx4_spec_list spec_eth = { {NULL} };
519 __be64 mac_mask = cpu_to_be64(MLX4_MAC_MASK << 16);
521 struct mlx4_net_trans_rule rule = {
522 .queue_mode = MLX4_NET_TRANS_Q_FIFO,
525 .promisc_mode = MLX4_FS_REGULAR,
526 .priority = MLX4_DOMAIN_NIC,
529 rule.port = priv->port;
531 INIT_LIST_HEAD(&rule.list);
533 spec_eth.id = MLX4_NET_TRANS_RULE_ID_ETH;
534 memcpy(spec_eth.eth.dst_mac, mac, ETH_ALEN);
535 memcpy(spec_eth.eth.dst_mac_msk, &mac_mask, ETH_ALEN);
536 list_add_tail(&spec_eth.list, &rule.list);
538 err = mlx4_flow_attach(dev, &rule, reg_id);
545 en_warn(priv, "Failed Attaching Unicast\n");
550 static void mlx4_en_uc_steer_release(struct mlx4_en_priv *priv,
551 unsigned char *mac, int qpn, u64 reg_id)
553 struct mlx4_en_dev *mdev = priv->mdev;
554 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 mlx4_unicast_detach(dev, &qp, gid, MLX4_PROT_ETH);
568 case MLX4_STEERING_MODE_DEVICE_MANAGED: {
569 mlx4_flow_detach(dev, reg_id);
573 en_err(priv, "Invalid steering mode.\n");
577 static int mlx4_en_get_qp(struct mlx4_en_priv *priv)
579 struct mlx4_en_dev *mdev = priv->mdev;
580 struct mlx4_dev *dev = mdev->dev;
583 int *qpn = &priv->base_qpn;
584 u64 mac = mlx4_mac_to_u64(priv->dev->dev_addr);
586 en_dbg(DRV, priv, "Registering MAC: %pM for adding\n",
587 priv->dev->dev_addr);
588 index = mlx4_register_mac(dev, priv->port, mac);
591 en_err(priv, "Failed adding MAC: %pM\n",
592 priv->dev->dev_addr);
596 if (dev->caps.steering_mode == MLX4_STEERING_MODE_A0) {
597 int base_qpn = mlx4_get_base_qpn(dev, priv->port);
598 *qpn = base_qpn + index;
602 err = mlx4_qp_reserve_range(dev, 1, 1, qpn, MLX4_RESERVE_A0_QP);
603 en_dbg(DRV, priv, "Reserved qp %d\n", *qpn);
605 en_err(priv, "Failed to reserve qp for mac registration\n");
606 mlx4_unregister_mac(dev, priv->port, mac);
613 static void mlx4_en_put_qp(struct mlx4_en_priv *priv)
615 struct mlx4_en_dev *mdev = priv->mdev;
616 struct mlx4_dev *dev = mdev->dev;
617 int qpn = priv->base_qpn;
619 if (dev->caps.steering_mode == MLX4_STEERING_MODE_A0) {
620 u64 mac = mlx4_mac_to_u64(priv->dev->dev_addr);
621 en_dbg(DRV, priv, "Registering MAC: %pM for deleting\n",
622 priv->dev->dev_addr);
623 mlx4_unregister_mac(dev, priv->port, mac);
625 en_dbg(DRV, priv, "Releasing qp: port %d, qpn %d\n",
627 mlx4_qp_release_range(dev, qpn, 1);
628 priv->flags &= ~MLX4_EN_FLAG_FORCE_PROMISC;
632 static int mlx4_en_replace_mac(struct mlx4_en_priv *priv, int qpn,
633 unsigned char *new_mac, unsigned char *prev_mac)
635 struct mlx4_en_dev *mdev = priv->mdev;
636 struct mlx4_dev *dev = mdev->dev;
638 u64 new_mac_u64 = mlx4_mac_to_u64(new_mac);
640 if (dev->caps.steering_mode != MLX4_STEERING_MODE_A0) {
641 struct hlist_head *bucket;
642 unsigned int mac_hash;
643 struct mlx4_mac_entry *entry;
644 struct hlist_node *tmp;
645 u64 prev_mac_u64 = mlx4_mac_to_u64(prev_mac);
647 bucket = &priv->mac_hash[prev_mac[MLX4_EN_MAC_HASH_IDX]];
648 hlist_for_each_entry_safe(entry, tmp, bucket, hlist) {
649 if (ether_addr_equal_64bits(entry->mac, prev_mac)) {
650 mlx4_en_uc_steer_release(priv, entry->mac,
652 mlx4_unregister_mac(dev, priv->port,
654 hlist_del_rcu(&entry->hlist);
656 memcpy(entry->mac, new_mac, ETH_ALEN);
658 mac_hash = new_mac[MLX4_EN_MAC_HASH_IDX];
659 hlist_add_head_rcu(&entry->hlist,
660 &priv->mac_hash[mac_hash]);
661 mlx4_register_mac(dev, priv->port, new_mac_u64);
662 err = mlx4_en_uc_steer_add(priv, new_mac,
667 if (priv->tunnel_reg_id) {
668 mlx4_flow_detach(priv->mdev->dev, priv->tunnel_reg_id);
669 priv->tunnel_reg_id = 0;
671 err = mlx4_en_tunnel_steer_add(priv, new_mac, qpn,
672 &priv->tunnel_reg_id);
679 return __mlx4_replace_mac(dev, priv->port, qpn, new_mac_u64);
682 static int mlx4_en_do_set_mac(struct mlx4_en_priv *priv,
683 unsigned char new_mac[ETH_ALEN + 2])
688 /* Remove old MAC and insert the new one */
689 err = mlx4_en_replace_mac(priv, priv->base_qpn,
690 new_mac, priv->current_mac);
692 en_err(priv, "Failed changing HW MAC address\n");
694 en_dbg(HW, priv, "Port is down while registering mac, exiting...\n");
697 memcpy(priv->current_mac, new_mac, sizeof(priv->current_mac));
702 static int mlx4_en_set_mac(struct net_device *dev, void *addr)
704 struct mlx4_en_priv *priv = netdev_priv(dev);
705 struct mlx4_en_dev *mdev = priv->mdev;
706 struct sockaddr *saddr = addr;
707 unsigned char new_mac[ETH_ALEN + 2];
710 if (!is_valid_ether_addr(saddr->sa_data))
711 return -EADDRNOTAVAIL;
713 mutex_lock(&mdev->state_lock);
714 memcpy(new_mac, saddr->sa_data, ETH_ALEN);
715 err = mlx4_en_do_set_mac(priv, new_mac);
717 memcpy(dev->dev_addr, saddr->sa_data, ETH_ALEN);
718 mutex_unlock(&mdev->state_lock);
723 static void mlx4_en_clear_list(struct net_device *dev)
725 struct mlx4_en_priv *priv = netdev_priv(dev);
726 struct mlx4_en_mc_list *tmp, *mc_to_del;
728 list_for_each_entry_safe(mc_to_del, tmp, &priv->mc_list, list) {
729 list_del(&mc_to_del->list);
734 static void mlx4_en_cache_mclist(struct net_device *dev)
736 struct mlx4_en_priv *priv = netdev_priv(dev);
737 struct netdev_hw_addr *ha;
738 struct mlx4_en_mc_list *tmp;
740 mlx4_en_clear_list(dev);
741 netdev_for_each_mc_addr(ha, dev) {
742 tmp = kzalloc(sizeof(struct mlx4_en_mc_list), GFP_ATOMIC);
744 mlx4_en_clear_list(dev);
747 memcpy(tmp->addr, ha->addr, ETH_ALEN);
748 list_add_tail(&tmp->list, &priv->mc_list);
752 static void update_mclist_flags(struct mlx4_en_priv *priv,
753 struct list_head *dst,
754 struct list_head *src)
756 struct mlx4_en_mc_list *dst_tmp, *src_tmp, *new_mc;
759 /* Find all the entries that should be removed from dst,
760 * These are the entries that are not found in src
762 list_for_each_entry(dst_tmp, dst, list) {
764 list_for_each_entry(src_tmp, src, list) {
765 if (ether_addr_equal(dst_tmp->addr, src_tmp->addr)) {
771 dst_tmp->action = MCLIST_REM;
774 /* Add entries that exist in src but not in dst
775 * mark them as need to add
777 list_for_each_entry(src_tmp, src, list) {
779 list_for_each_entry(dst_tmp, dst, list) {
780 if (ether_addr_equal(dst_tmp->addr, src_tmp->addr)) {
781 dst_tmp->action = MCLIST_NONE;
787 new_mc = kmemdup(src_tmp,
788 sizeof(struct mlx4_en_mc_list),
793 new_mc->action = MCLIST_ADD;
794 list_add_tail(&new_mc->list, dst);
799 static void mlx4_en_set_rx_mode(struct net_device *dev)
801 struct mlx4_en_priv *priv = netdev_priv(dev);
806 queue_work(priv->mdev->workqueue, &priv->rx_mode_task);
809 static void mlx4_en_set_promisc_mode(struct mlx4_en_priv *priv,
810 struct mlx4_en_dev *mdev)
814 if (!(priv->flags & MLX4_EN_FLAG_PROMISC)) {
815 if (netif_msg_rx_status(priv))
816 en_warn(priv, "Entering promiscuous mode\n");
817 priv->flags |= MLX4_EN_FLAG_PROMISC;
819 /* Enable promiscouos mode */
820 switch (mdev->dev->caps.steering_mode) {
821 case MLX4_STEERING_MODE_DEVICE_MANAGED:
822 err = mlx4_flow_steer_promisc_add(mdev->dev,
825 MLX4_FS_ALL_DEFAULT);
827 en_err(priv, "Failed enabling promiscuous mode\n");
828 priv->flags |= MLX4_EN_FLAG_MC_PROMISC;
831 case MLX4_STEERING_MODE_B0:
832 err = mlx4_unicast_promisc_add(mdev->dev,
836 en_err(priv, "Failed enabling unicast promiscuous mode\n");
838 /* Add the default qp number as multicast
841 if (!(priv->flags & MLX4_EN_FLAG_MC_PROMISC)) {
842 err = mlx4_multicast_promisc_add(mdev->dev,
846 en_err(priv, "Failed enabling multicast promiscuous mode\n");
847 priv->flags |= MLX4_EN_FLAG_MC_PROMISC;
851 case MLX4_STEERING_MODE_A0:
852 err = mlx4_SET_PORT_qpn_calc(mdev->dev,
857 en_err(priv, "Failed enabling promiscuous mode\n");
861 /* Disable port multicast filter (unconditionally) */
862 err = mlx4_SET_MCAST_FLTR(mdev->dev, priv->port, 0,
863 0, MLX4_MCAST_DISABLE);
865 en_err(priv, "Failed disabling multicast filter\n");
869 static void mlx4_en_clear_promisc_mode(struct mlx4_en_priv *priv,
870 struct mlx4_en_dev *mdev)
874 if (netif_msg_rx_status(priv))
875 en_warn(priv, "Leaving promiscuous mode\n");
876 priv->flags &= ~MLX4_EN_FLAG_PROMISC;
878 /* Disable promiscouos mode */
879 switch (mdev->dev->caps.steering_mode) {
880 case MLX4_STEERING_MODE_DEVICE_MANAGED:
881 err = mlx4_flow_steer_promisc_remove(mdev->dev,
883 MLX4_FS_ALL_DEFAULT);
885 en_err(priv, "Failed disabling promiscuous mode\n");
886 priv->flags &= ~MLX4_EN_FLAG_MC_PROMISC;
889 case MLX4_STEERING_MODE_B0:
890 err = mlx4_unicast_promisc_remove(mdev->dev,
894 en_err(priv, "Failed disabling unicast promiscuous mode\n");
895 /* Disable Multicast promisc */
896 if (priv->flags & MLX4_EN_FLAG_MC_PROMISC) {
897 err = mlx4_multicast_promisc_remove(mdev->dev,
901 en_err(priv, "Failed disabling multicast promiscuous mode\n");
902 priv->flags &= ~MLX4_EN_FLAG_MC_PROMISC;
906 case MLX4_STEERING_MODE_A0:
907 err = mlx4_SET_PORT_qpn_calc(mdev->dev,
911 en_err(priv, "Failed disabling promiscuous mode\n");
916 static void mlx4_en_do_multicast(struct mlx4_en_priv *priv,
917 struct net_device *dev,
918 struct mlx4_en_dev *mdev)
920 struct mlx4_en_mc_list *mclist, *tmp;
922 u8 mc_list[16] = {0};
925 /* Enable/disable the multicast filter according to IFF_ALLMULTI */
926 if (dev->flags & IFF_ALLMULTI) {
927 err = mlx4_SET_MCAST_FLTR(mdev->dev, priv->port, 0,
928 0, MLX4_MCAST_DISABLE);
930 en_err(priv, "Failed disabling multicast filter\n");
932 /* Add the default qp number as multicast promisc */
933 if (!(priv->flags & MLX4_EN_FLAG_MC_PROMISC)) {
934 switch (mdev->dev->caps.steering_mode) {
935 case MLX4_STEERING_MODE_DEVICE_MANAGED:
936 err = mlx4_flow_steer_promisc_add(mdev->dev,
942 case MLX4_STEERING_MODE_B0:
943 err = mlx4_multicast_promisc_add(mdev->dev,
948 case MLX4_STEERING_MODE_A0:
952 en_err(priv, "Failed entering multicast promisc mode\n");
953 priv->flags |= MLX4_EN_FLAG_MC_PROMISC;
956 /* Disable Multicast promisc */
957 if (priv->flags & MLX4_EN_FLAG_MC_PROMISC) {
958 switch (mdev->dev->caps.steering_mode) {
959 case MLX4_STEERING_MODE_DEVICE_MANAGED:
960 err = mlx4_flow_steer_promisc_remove(mdev->dev,
965 case MLX4_STEERING_MODE_B0:
966 err = mlx4_multicast_promisc_remove(mdev->dev,
971 case MLX4_STEERING_MODE_A0:
975 en_err(priv, "Failed disabling multicast promiscuous mode\n");
976 priv->flags &= ~MLX4_EN_FLAG_MC_PROMISC;
979 err = mlx4_SET_MCAST_FLTR(mdev->dev, priv->port, 0,
980 0, MLX4_MCAST_DISABLE);
982 en_err(priv, "Failed disabling multicast filter\n");
984 /* Flush mcast filter and init it with broadcast address */
985 mlx4_SET_MCAST_FLTR(mdev->dev, priv->port, ETH_BCAST,
986 1, MLX4_MCAST_CONFIG);
988 /* Update multicast list - we cache all addresses so they won't
989 * change while HW is updated holding the command semaphor */
990 netif_addr_lock_bh(dev);
991 mlx4_en_cache_mclist(dev);
992 netif_addr_unlock_bh(dev);
993 list_for_each_entry(mclist, &priv->mc_list, list) {
994 mcast_addr = mlx4_mac_to_u64(mclist->addr);
995 mlx4_SET_MCAST_FLTR(mdev->dev, priv->port,
996 mcast_addr, 0, MLX4_MCAST_CONFIG);
998 err = mlx4_SET_MCAST_FLTR(mdev->dev, priv->port, 0,
999 0, MLX4_MCAST_ENABLE);
1001 en_err(priv, "Failed enabling multicast filter\n");
1003 update_mclist_flags(priv, &priv->curr_list, &priv->mc_list);
1004 list_for_each_entry_safe(mclist, tmp, &priv->curr_list, list) {
1005 if (mclist->action == MCLIST_REM) {
1006 /* detach this address and delete from list */
1007 memcpy(&mc_list[10], mclist->addr, ETH_ALEN);
1008 mc_list[5] = priv->port;
1009 err = mlx4_multicast_detach(mdev->dev,
1010 &priv->rss_map.indir_qp,
1015 en_err(priv, "Fail to detach multicast address\n");
1017 if (mclist->tunnel_reg_id) {
1018 err = mlx4_flow_detach(priv->mdev->dev, mclist->tunnel_reg_id);
1020 en_err(priv, "Failed to detach multicast address\n");
1023 /* remove from list */
1024 list_del(&mclist->list);
1026 } else if (mclist->action == MCLIST_ADD) {
1027 /* attach the address */
1028 memcpy(&mc_list[10], mclist->addr, ETH_ALEN);
1029 /* needed for B0 steering support */
1030 mc_list[5] = priv->port;
1031 err = mlx4_multicast_attach(mdev->dev,
1032 &priv->rss_map.indir_qp,
1038 en_err(priv, "Fail to attach multicast address\n");
1040 err = mlx4_en_tunnel_steer_add(priv, &mc_list[10], priv->base_qpn,
1041 &mclist->tunnel_reg_id);
1043 en_err(priv, "Failed to attach multicast address\n");
1049 static void mlx4_en_do_uc_filter(struct mlx4_en_priv *priv,
1050 struct net_device *dev,
1051 struct mlx4_en_dev *mdev)
1053 struct netdev_hw_addr *ha;
1054 struct mlx4_mac_entry *entry;
1055 struct hlist_node *tmp;
1059 struct hlist_head *bucket;
1064 /* Note that we do not need to protect our mac_hash traversal with rcu,
1065 * since all modification code is protected by mdev->state_lock
1068 /* find what to remove */
1069 for (i = 0; i < MLX4_EN_MAC_HASH_SIZE; ++i) {
1070 bucket = &priv->mac_hash[i];
1071 hlist_for_each_entry_safe(entry, tmp, bucket, hlist) {
1073 netdev_for_each_uc_addr(ha, dev) {
1074 if (ether_addr_equal_64bits(entry->mac,
1081 /* MAC address of the port is not in uc list */
1082 if (ether_addr_equal_64bits(entry->mac,
1087 mac = mlx4_mac_to_u64(entry->mac);
1088 mlx4_en_uc_steer_release(priv, entry->mac,
1091 mlx4_unregister_mac(mdev->dev, priv->port, mac);
1093 hlist_del_rcu(&entry->hlist);
1094 kfree_rcu(entry, rcu);
1095 en_dbg(DRV, priv, "Removed MAC %pM on port:%d\n",
1096 entry->mac, priv->port);
1102 /* if we didn't remove anything, there is no use in trying to add
1103 * again once we are in a forced promisc mode state
1105 if ((priv->flags & MLX4_EN_FLAG_FORCE_PROMISC) && 0 == removed)
1108 prev_flags = priv->flags;
1109 priv->flags &= ~MLX4_EN_FLAG_FORCE_PROMISC;
1111 /* find what to add */
1112 netdev_for_each_uc_addr(ha, dev) {
1114 bucket = &priv->mac_hash[ha->addr[MLX4_EN_MAC_HASH_IDX]];
1115 hlist_for_each_entry(entry, bucket, hlist) {
1116 if (ether_addr_equal_64bits(entry->mac, ha->addr)) {
1123 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1125 en_err(priv, "Failed adding MAC %pM on port:%d (out of memory)\n",
1126 ha->addr, priv->port);
1127 priv->flags |= MLX4_EN_FLAG_FORCE_PROMISC;
1130 mac = mlx4_mac_to_u64(ha->addr);
1131 memcpy(entry->mac, ha->addr, ETH_ALEN);
1132 err = mlx4_register_mac(mdev->dev, priv->port, mac);
1134 en_err(priv, "Failed registering MAC %pM on port %d: %d\n",
1135 ha->addr, priv->port, err);
1137 priv->flags |= MLX4_EN_FLAG_FORCE_PROMISC;
1140 err = mlx4_en_uc_steer_add(priv, ha->addr,
1144 en_err(priv, "Failed adding MAC %pM on port %d: %d\n",
1145 ha->addr, priv->port, err);
1146 mlx4_unregister_mac(mdev->dev, priv->port, mac);
1148 priv->flags |= MLX4_EN_FLAG_FORCE_PROMISC;
1151 unsigned int mac_hash;
1152 en_dbg(DRV, priv, "Added MAC %pM on port:%d\n",
1153 ha->addr, priv->port);
1154 mac_hash = ha->addr[MLX4_EN_MAC_HASH_IDX];
1155 bucket = &priv->mac_hash[mac_hash];
1156 hlist_add_head_rcu(&entry->hlist, bucket);
1161 if (priv->flags & MLX4_EN_FLAG_FORCE_PROMISC) {
1162 en_warn(priv, "Forcing promiscuous mode on port:%d\n",
1164 } else if (prev_flags & MLX4_EN_FLAG_FORCE_PROMISC) {
1165 en_warn(priv, "Stop forcing promiscuous mode on port:%d\n",
1170 static void mlx4_en_do_set_rx_mode(struct work_struct *work)
1172 struct mlx4_en_priv *priv = container_of(work, struct mlx4_en_priv,
1174 struct mlx4_en_dev *mdev = priv->mdev;
1175 struct net_device *dev = priv->dev;
1177 mutex_lock(&mdev->state_lock);
1178 if (!mdev->device_up) {
1179 en_dbg(HW, priv, "Card is not up, ignoring rx mode change.\n");
1182 if (!priv->port_up) {
1183 en_dbg(HW, priv, "Port is down, ignoring rx mode change.\n");
1187 if (!netif_carrier_ok(dev)) {
1188 if (!mlx4_en_QUERY_PORT(mdev, priv->port)) {
1189 if (priv->port_state.link_state) {
1190 priv->last_link_state = MLX4_DEV_EVENT_PORT_UP;
1191 netif_carrier_on(dev);
1192 en_dbg(LINK, priv, "Link Up\n");
1197 if (dev->priv_flags & IFF_UNICAST_FLT)
1198 mlx4_en_do_uc_filter(priv, dev, mdev);
1200 /* Promsicuous mode: disable all filters */
1201 if ((dev->flags & IFF_PROMISC) ||
1202 (priv->flags & MLX4_EN_FLAG_FORCE_PROMISC)) {
1203 mlx4_en_set_promisc_mode(priv, mdev);
1207 /* Not in promiscuous mode */
1208 if (priv->flags & MLX4_EN_FLAG_PROMISC)
1209 mlx4_en_clear_promisc_mode(priv, mdev);
1211 mlx4_en_do_multicast(priv, dev, mdev);
1213 mutex_unlock(&mdev->state_lock);
1216 #ifdef CONFIG_NET_POLL_CONTROLLER
1217 static void mlx4_en_netpoll(struct net_device *dev)
1219 struct mlx4_en_priv *priv = netdev_priv(dev);
1220 struct mlx4_en_cq *cq;
1223 for (i = 0; i < priv->tx_ring_num[TX]; i++) {
1224 cq = priv->tx_cq[TX][i];
1225 napi_schedule(&cq->napi);
1230 static int mlx4_en_set_rss_steer_rules(struct mlx4_en_priv *priv)
1234 int *qpn = &priv->base_qpn;
1235 struct mlx4_mac_entry *entry;
1237 err = mlx4_en_uc_steer_add(priv, priv->dev->dev_addr, qpn, ®_id);
1241 err = mlx4_en_tunnel_steer_add(priv, priv->dev->dev_addr, *qpn,
1242 &priv->tunnel_reg_id);
1246 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1252 memcpy(entry->mac, priv->dev->dev_addr, sizeof(entry->mac));
1253 memcpy(priv->current_mac, entry->mac, sizeof(priv->current_mac));
1254 entry->reg_id = reg_id;
1255 hlist_add_head_rcu(&entry->hlist,
1256 &priv->mac_hash[entry->mac[MLX4_EN_MAC_HASH_IDX]]);
1261 if (priv->tunnel_reg_id)
1262 mlx4_flow_detach(priv->mdev->dev, priv->tunnel_reg_id);
1265 mlx4_en_uc_steer_release(priv, priv->dev->dev_addr, *qpn, reg_id);
1269 static void mlx4_en_delete_rss_steer_rules(struct mlx4_en_priv *priv)
1273 int qpn = priv->base_qpn;
1274 struct hlist_head *bucket;
1275 struct hlist_node *tmp;
1276 struct mlx4_mac_entry *entry;
1278 for (i = 0; i < MLX4_EN_MAC_HASH_SIZE; ++i) {
1279 bucket = &priv->mac_hash[i];
1280 hlist_for_each_entry_safe(entry, tmp, bucket, hlist) {
1281 mac = mlx4_mac_to_u64(entry->mac);
1282 en_dbg(DRV, priv, "Registering MAC:%pM for deleting\n",
1284 mlx4_en_uc_steer_release(priv, entry->mac,
1285 qpn, entry->reg_id);
1287 mlx4_unregister_mac(priv->mdev->dev, priv->port, mac);
1288 hlist_del_rcu(&entry->hlist);
1289 kfree_rcu(entry, rcu);
1293 if (priv->tunnel_reg_id) {
1294 mlx4_flow_detach(priv->mdev->dev, priv->tunnel_reg_id);
1295 priv->tunnel_reg_id = 0;
1299 static void mlx4_en_tx_timeout(struct net_device *dev)
1301 struct mlx4_en_priv *priv = netdev_priv(dev);
1302 struct mlx4_en_dev *mdev = priv->mdev;
1305 if (netif_msg_timer(priv))
1306 en_warn(priv, "Tx timeout called on port:%d\n", priv->port);
1308 for (i = 0; i < priv->tx_ring_num[TX]; i++) {
1309 struct mlx4_en_tx_ring *tx_ring = priv->tx_ring[TX][i];
1311 if (!netif_tx_queue_stopped(netdev_get_tx_queue(dev, i)))
1313 en_warn(priv, "TX timeout on queue: %d, QP: 0x%x, CQ: 0x%x, Cons: 0x%x, Prod: 0x%x\n",
1314 i, tx_ring->qpn, tx_ring->sp_cqn,
1315 tx_ring->cons, tx_ring->prod);
1318 priv->port_stats.tx_timeout++;
1319 en_dbg(DRV, priv, "Scheduling watchdog\n");
1320 queue_work(mdev->workqueue, &priv->watchdog_task);
1324 static struct rtnl_link_stats64 *
1325 mlx4_en_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
1327 struct mlx4_en_priv *priv = netdev_priv(dev);
1329 spin_lock_bh(&priv->stats_lock);
1330 mlx4_en_fold_software_stats(dev);
1331 netdev_stats_to_stats64(stats, &dev->stats);
1332 spin_unlock_bh(&priv->stats_lock);
1337 static void mlx4_en_set_default_moderation(struct mlx4_en_priv *priv)
1339 struct mlx4_en_cq *cq;
1342 /* If we haven't received a specific coalescing setting
1343 * (module param), we set the moderation parameters as follows:
1344 * - moder_cnt is set to the number of mtu sized packets to
1345 * satisfy our coalescing target.
1346 * - moder_time is set to a fixed value.
1348 priv->rx_frames = MLX4_EN_RX_COAL_TARGET;
1349 priv->rx_usecs = MLX4_EN_RX_COAL_TIME;
1350 priv->tx_frames = MLX4_EN_TX_COAL_PKTS;
1351 priv->tx_usecs = MLX4_EN_TX_COAL_TIME;
1352 en_dbg(INTR, priv, "Default coalesing params for mtu:%d - rx_frames:%d rx_usecs:%d\n",
1353 priv->dev->mtu, priv->rx_frames, priv->rx_usecs);
1355 /* Setup cq moderation params */
1356 for (i = 0; i < priv->rx_ring_num; i++) {
1357 cq = priv->rx_cq[i];
1358 cq->moder_cnt = priv->rx_frames;
1359 cq->moder_time = priv->rx_usecs;
1360 priv->last_moder_time[i] = MLX4_EN_AUTO_CONF;
1361 priv->last_moder_packets[i] = 0;
1362 priv->last_moder_bytes[i] = 0;
1365 for (t = 0 ; t < MLX4_EN_NUM_TX_TYPES; t++) {
1366 for (i = 0; i < priv->tx_ring_num[t]; i++) {
1367 cq = priv->tx_cq[t][i];
1368 cq->moder_cnt = priv->tx_frames;
1369 cq->moder_time = priv->tx_usecs;
1373 /* Reset auto-moderation params */
1374 priv->pkt_rate_low = MLX4_EN_RX_RATE_LOW;
1375 priv->rx_usecs_low = MLX4_EN_RX_COAL_TIME_LOW;
1376 priv->pkt_rate_high = MLX4_EN_RX_RATE_HIGH;
1377 priv->rx_usecs_high = MLX4_EN_RX_COAL_TIME_HIGH;
1378 priv->sample_interval = MLX4_EN_SAMPLE_INTERVAL;
1379 priv->adaptive_rx_coal = 1;
1380 priv->last_moder_jiffies = 0;
1381 priv->last_moder_tx_packets = 0;
1384 static void mlx4_en_auto_moderation(struct mlx4_en_priv *priv)
1386 unsigned long period = (unsigned long) (jiffies - priv->last_moder_jiffies);
1387 struct mlx4_en_cq *cq;
1388 unsigned long packets;
1390 unsigned long avg_pkt_size;
1391 unsigned long rx_packets;
1392 unsigned long rx_bytes;
1393 unsigned long rx_pkt_diff;
1397 if (!priv->adaptive_rx_coal || period < priv->sample_interval * HZ)
1400 for (ring = 0; ring < priv->rx_ring_num; ring++) {
1401 rx_packets = READ_ONCE(priv->rx_ring[ring]->packets);
1402 rx_bytes = READ_ONCE(priv->rx_ring[ring]->bytes);
1404 rx_pkt_diff = ((unsigned long) (rx_packets -
1405 priv->last_moder_packets[ring]));
1406 packets = rx_pkt_diff;
1407 rate = packets * HZ / period;
1408 avg_pkt_size = packets ? ((unsigned long) (rx_bytes -
1409 priv->last_moder_bytes[ring])) / packets : 0;
1411 /* Apply auto-moderation only when packet rate
1412 * exceeds a rate that it matters */
1413 if (rate > (MLX4_EN_RX_RATE_THRESH / priv->rx_ring_num) &&
1414 avg_pkt_size > MLX4_EN_AVG_PKT_SMALL) {
1415 if (rate < priv->pkt_rate_low)
1416 moder_time = priv->rx_usecs_low;
1417 else if (rate > priv->pkt_rate_high)
1418 moder_time = priv->rx_usecs_high;
1420 moder_time = (rate - priv->pkt_rate_low) *
1421 (priv->rx_usecs_high - priv->rx_usecs_low) /
1422 (priv->pkt_rate_high - priv->pkt_rate_low) +
1425 moder_time = priv->rx_usecs_low;
1428 if (moder_time != priv->last_moder_time[ring]) {
1429 priv->last_moder_time[ring] = moder_time;
1430 cq = priv->rx_cq[ring];
1431 cq->moder_time = moder_time;
1432 cq->moder_cnt = priv->rx_frames;
1433 err = mlx4_en_set_cq_moder(priv, cq);
1435 en_err(priv, "Failed modifying moderation for cq:%d\n",
1438 priv->last_moder_packets[ring] = rx_packets;
1439 priv->last_moder_bytes[ring] = rx_bytes;
1442 priv->last_moder_jiffies = jiffies;
1445 static void mlx4_en_do_get_stats(struct work_struct *work)
1447 struct delayed_work *delay = to_delayed_work(work);
1448 struct mlx4_en_priv *priv = container_of(delay, struct mlx4_en_priv,
1450 struct mlx4_en_dev *mdev = priv->mdev;
1453 mutex_lock(&mdev->state_lock);
1454 if (mdev->device_up) {
1455 if (priv->port_up) {
1456 err = mlx4_en_DUMP_ETH_STATS(mdev, priv->port, 0);
1458 en_dbg(HW, priv, "Could not update stats\n");
1460 mlx4_en_auto_moderation(priv);
1463 queue_delayed_work(mdev->workqueue, &priv->stats_task, STATS_DELAY);
1465 if (mdev->mac_removed[MLX4_MAX_PORTS + 1 - priv->port]) {
1466 mlx4_en_do_set_mac(priv, priv->current_mac);
1467 mdev->mac_removed[MLX4_MAX_PORTS + 1 - priv->port] = 0;
1469 mutex_unlock(&mdev->state_lock);
1472 /* mlx4_en_service_task - Run service task for tasks that needed to be done
1475 static void mlx4_en_service_task(struct work_struct *work)
1477 struct delayed_work *delay = to_delayed_work(work);
1478 struct mlx4_en_priv *priv = container_of(delay, struct mlx4_en_priv,
1480 struct mlx4_en_dev *mdev = priv->mdev;
1482 mutex_lock(&mdev->state_lock);
1483 if (mdev->device_up) {
1484 if (mdev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_TS)
1485 mlx4_en_ptp_overflow_check(mdev);
1487 mlx4_en_recover_from_oom(priv);
1488 queue_delayed_work(mdev->workqueue, &priv->service_task,
1489 SERVICE_TASK_DELAY);
1491 mutex_unlock(&mdev->state_lock);
1494 static void mlx4_en_linkstate(struct work_struct *work)
1496 struct mlx4_en_priv *priv = container_of(work, struct mlx4_en_priv,
1498 struct mlx4_en_dev *mdev = priv->mdev;
1499 int linkstate = priv->link_state;
1501 mutex_lock(&mdev->state_lock);
1502 /* If observable port state changed set carrier state and
1503 * report to system log */
1504 if (priv->last_link_state != linkstate) {
1505 if (linkstate == MLX4_DEV_EVENT_PORT_DOWN) {
1506 en_info(priv, "Link Down\n");
1507 netif_carrier_off(priv->dev);
1509 en_info(priv, "Link Up\n");
1510 netif_carrier_on(priv->dev);
1513 priv->last_link_state = linkstate;
1514 mutex_unlock(&mdev->state_lock);
1517 static int mlx4_en_init_affinity_hint(struct mlx4_en_priv *priv, int ring_idx)
1519 struct mlx4_en_rx_ring *ring = priv->rx_ring[ring_idx];
1520 int numa_node = priv->mdev->dev->numa_node;
1522 if (!zalloc_cpumask_var(&ring->affinity_mask, GFP_KERNEL))
1525 cpumask_set_cpu(cpumask_local_spread(ring_idx, numa_node),
1526 ring->affinity_mask);
1530 static void mlx4_en_free_affinity_hint(struct mlx4_en_priv *priv, int ring_idx)
1532 free_cpumask_var(priv->rx_ring[ring_idx]->affinity_mask);
1535 static void mlx4_en_init_recycle_ring(struct mlx4_en_priv *priv,
1538 struct mlx4_en_tx_ring *tx_ring = priv->tx_ring[TX_XDP][tx_ring_idx];
1539 int rr_index = tx_ring_idx;
1541 tx_ring->free_tx_desc = mlx4_en_recycle_tx_desc;
1542 tx_ring->recycle_ring = priv->rx_ring[rr_index];
1543 en_dbg(DRV, priv, "Set tx_ring[%d][%d]->recycle_ring = rx_ring[%d]\n",
1544 TX_XDP, tx_ring_idx, rr_index);
1547 int mlx4_en_start_port(struct net_device *dev)
1549 struct mlx4_en_priv *priv = netdev_priv(dev);
1550 struct mlx4_en_dev *mdev = priv->mdev;
1551 struct mlx4_en_cq *cq;
1552 struct mlx4_en_tx_ring *tx_ring;
1557 u8 mc_list[16] = {0};
1559 if (priv->port_up) {
1560 en_dbg(DRV, priv, "start port called while port already up\n");
1564 INIT_LIST_HEAD(&priv->mc_list);
1565 INIT_LIST_HEAD(&priv->curr_list);
1566 INIT_LIST_HEAD(&priv->ethtool_list);
1567 memset(&priv->ethtool_rules[0], 0,
1568 sizeof(struct ethtool_flow_id) * MAX_NUM_OF_FS_RULES);
1570 /* Calculate Rx buf size */
1571 dev->mtu = min(dev->mtu, priv->max_mtu);
1572 mlx4_en_calc_rx_buf(dev);
1573 en_dbg(DRV, priv, "Rx buf size:%d\n", priv->rx_skb_size);
1575 /* Configure rx cq's and rings */
1576 err = mlx4_en_activate_rx_rings(priv);
1578 en_err(priv, "Failed to activate RX rings\n");
1581 for (i = 0; i < priv->rx_ring_num; i++) {
1582 cq = priv->rx_cq[i];
1584 err = mlx4_en_init_affinity_hint(priv, i);
1586 en_err(priv, "Failed preparing IRQ affinity hint\n");
1590 err = mlx4_en_activate_cq(priv, cq, i);
1592 en_err(priv, "Failed activating Rx CQ\n");
1593 mlx4_en_free_affinity_hint(priv, i);
1597 for (j = 0; j < cq->size; j++) {
1598 struct mlx4_cqe *cqe = NULL;
1600 cqe = mlx4_en_get_cqe(cq->buf, j, priv->cqe_size) +
1602 cqe->owner_sr_opcode = MLX4_CQE_OWNER_MASK;
1605 err = mlx4_en_set_cq_moder(priv, cq);
1607 en_err(priv, "Failed setting cq moderation parameters\n");
1608 mlx4_en_deactivate_cq(priv, cq);
1609 mlx4_en_free_affinity_hint(priv, i);
1612 mlx4_en_arm_cq(priv, cq);
1613 priv->rx_ring[i]->cqn = cq->mcq.cqn;
1618 en_dbg(DRV, priv, "Getting qp number for port %d\n", priv->port);
1619 err = mlx4_en_get_qp(priv);
1621 en_err(priv, "Failed getting eth qp\n");
1624 mdev->mac_removed[priv->port] = 0;
1626 priv->counter_index =
1627 mlx4_get_default_counter_index(mdev->dev, priv->port);
1629 err = mlx4_en_config_rss_steer(priv);
1631 en_err(priv, "Failed configuring rss steering\n");
1635 err = mlx4_en_create_drop_qp(priv);
1639 /* Configure tx cq's and rings */
1640 for (t = 0 ; t < MLX4_EN_NUM_TX_TYPES; t++) {
1641 u8 num_tx_rings_p_up = t == TX ?
1642 priv->num_tx_rings_p_up : priv->tx_ring_num[t];
1644 for (i = 0; i < priv->tx_ring_num[t]; i++) {
1646 cq = priv->tx_cq[t][i];
1647 err = mlx4_en_activate_cq(priv, cq, i);
1649 en_err(priv, "Failed allocating Tx CQ\n");
1652 err = mlx4_en_set_cq_moder(priv, cq);
1654 en_err(priv, "Failed setting cq moderation parameters\n");
1655 mlx4_en_deactivate_cq(priv, cq);
1659 "Resetting index of collapsed CQ:%d to -1\n", i);
1660 cq->buf->wqe_index = cpu_to_be16(0xffff);
1662 /* Configure ring */
1663 tx_ring = priv->tx_ring[t][i];
1664 err = mlx4_en_activate_tx_ring(priv, tx_ring,
1666 i / num_tx_rings_p_up);
1668 en_err(priv, "Failed allocating Tx ring\n");
1669 mlx4_en_deactivate_cq(priv, cq);
1673 tx_ring->tx_queue = netdev_get_tx_queue(dev, i);
1674 tx_ring->recycle_ring = NULL;
1676 mlx4_en_init_recycle_ring(priv, i);
1679 /* Arm CQ for TX completions */
1680 mlx4_en_arm_cq(priv, cq);
1682 /* Set initial ownership of all Tx TXBBs to SW (1) */
1683 for (j = 0; j < tx_ring->buf_size; j += STAMP_STRIDE)
1684 *((u32 *)(tx_ring->buf + j)) = 0xffffffff;
1688 /* Configure port */
1689 err = mlx4_SET_PORT_general(mdev->dev, priv->port,
1690 priv->rx_skb_size + ETH_FCS_LEN,
1691 priv->prof->tx_pause,
1693 priv->prof->rx_pause,
1694 priv->prof->rx_ppp);
1696 en_err(priv, "Failed setting port general configurations for port %d, with error %d\n",
1700 /* Set default qp number */
1701 err = mlx4_SET_PORT_qpn_calc(mdev->dev, priv->port, priv->base_qpn, 0);
1703 en_err(priv, "Failed setting default qp numbers\n");
1707 if (mdev->dev->caps.tunnel_offload_mode == MLX4_TUNNEL_OFFLOAD_MODE_VXLAN) {
1708 err = mlx4_SET_PORT_VXLAN(mdev->dev, priv->port, VXLAN_STEER_BY_OUTER_MAC, 1);
1710 en_err(priv, "Failed setting port L2 tunnel configuration, err %d\n",
1717 en_dbg(HW, priv, "Initializing port\n");
1718 err = mlx4_INIT_PORT(mdev->dev, priv->port);
1720 en_err(priv, "Failed Initializing port\n");
1724 /* Set Unicast and VXLAN steering rules */
1725 if (mdev->dev->caps.steering_mode != MLX4_STEERING_MODE_A0 &&
1726 mlx4_en_set_rss_steer_rules(priv))
1727 mlx4_warn(mdev, "Failed setting steering rules\n");
1729 /* Attach rx QP to bradcast address */
1730 eth_broadcast_addr(&mc_list[10]);
1731 mc_list[5] = priv->port; /* needed for B0 steering support */
1732 if (mlx4_multicast_attach(mdev->dev, &priv->rss_map.indir_qp, mc_list,
1733 priv->port, 0, MLX4_PROT_ETH,
1734 &priv->broadcast_id))
1735 mlx4_warn(mdev, "Failed Attaching Broadcast\n");
1737 /* Must redo promiscuous mode setup. */
1738 priv->flags &= ~(MLX4_EN_FLAG_PROMISC | MLX4_EN_FLAG_MC_PROMISC);
1740 /* Schedule multicast task to populate multicast list */
1741 queue_work(mdev->workqueue, &priv->rx_mode_task);
1743 if (priv->mdev->dev->caps.tunnel_offload_mode == MLX4_TUNNEL_OFFLOAD_MODE_VXLAN)
1744 udp_tunnel_get_rx_info(dev);
1746 priv->port_up = true;
1748 /* Process all completions if exist to prevent
1749 * the queues freezing if they are full
1751 for (i = 0; i < priv->rx_ring_num; i++) {
1753 napi_schedule(&priv->rx_cq[i]->napi);
1757 netif_tx_start_all_queues(dev);
1758 netif_device_attach(dev);
1763 if (t == MLX4_EN_NUM_TX_TYPES) {
1765 i = priv->tx_ring_num[t];
1769 mlx4_en_deactivate_tx_ring(priv, priv->tx_ring[t][i]);
1770 mlx4_en_deactivate_cq(priv, priv->tx_cq[t][i]);
1774 i = priv->tx_ring_num[t];
1776 mlx4_en_destroy_drop_qp(priv);
1778 mlx4_en_release_rss_steer(priv);
1780 mlx4_en_put_qp(priv);
1782 while (rx_index--) {
1783 mlx4_en_deactivate_cq(priv, priv->rx_cq[rx_index]);
1784 mlx4_en_free_affinity_hint(priv, rx_index);
1786 for (i = 0; i < priv->rx_ring_num; i++)
1787 mlx4_en_deactivate_rx_ring(priv, priv->rx_ring[i]);
1789 return err; /* need to close devices */
1793 void mlx4_en_stop_port(struct net_device *dev, int detach)
1795 struct mlx4_en_priv *priv = netdev_priv(dev);
1796 struct mlx4_en_dev *mdev = priv->mdev;
1797 struct mlx4_en_mc_list *mclist, *tmp;
1798 struct ethtool_flow_id *flow, *tmp_flow;
1800 u8 mc_list[16] = {0};
1802 if (!priv->port_up) {
1803 en_dbg(DRV, priv, "stop port called while port already down\n");
1808 mlx4_CLOSE_PORT(mdev->dev, priv->port);
1810 /* Synchronize with tx routine */
1811 netif_tx_lock_bh(dev);
1813 netif_device_detach(dev);
1814 netif_tx_stop_all_queues(dev);
1815 netif_tx_unlock_bh(dev);
1817 netif_tx_disable(dev);
1819 spin_lock_bh(&priv->stats_lock);
1820 mlx4_en_fold_software_stats(dev);
1821 /* Set port as not active */
1822 priv->port_up = false;
1823 spin_unlock_bh(&priv->stats_lock);
1825 priv->counter_index = MLX4_SINK_COUNTER_INDEX(mdev->dev);
1827 /* Promsicuous mode */
1828 if (mdev->dev->caps.steering_mode ==
1829 MLX4_STEERING_MODE_DEVICE_MANAGED) {
1830 priv->flags &= ~(MLX4_EN_FLAG_PROMISC |
1831 MLX4_EN_FLAG_MC_PROMISC);
1832 mlx4_flow_steer_promisc_remove(mdev->dev,
1834 MLX4_FS_ALL_DEFAULT);
1835 mlx4_flow_steer_promisc_remove(mdev->dev,
1837 MLX4_FS_MC_DEFAULT);
1838 } else if (priv->flags & MLX4_EN_FLAG_PROMISC) {
1839 priv->flags &= ~MLX4_EN_FLAG_PROMISC;
1841 /* Disable promiscouos mode */
1842 mlx4_unicast_promisc_remove(mdev->dev, priv->base_qpn,
1845 /* Disable Multicast promisc */
1846 if (priv->flags & MLX4_EN_FLAG_MC_PROMISC) {
1847 mlx4_multicast_promisc_remove(mdev->dev, priv->base_qpn,
1849 priv->flags &= ~MLX4_EN_FLAG_MC_PROMISC;
1853 /* Detach All multicasts */
1854 eth_broadcast_addr(&mc_list[10]);
1855 mc_list[5] = priv->port; /* needed for B0 steering support */
1856 mlx4_multicast_detach(mdev->dev, &priv->rss_map.indir_qp, mc_list,
1857 MLX4_PROT_ETH, priv->broadcast_id);
1858 list_for_each_entry(mclist, &priv->curr_list, list) {
1859 memcpy(&mc_list[10], mclist->addr, ETH_ALEN);
1860 mc_list[5] = priv->port;
1861 mlx4_multicast_detach(mdev->dev, &priv->rss_map.indir_qp,
1862 mc_list, MLX4_PROT_ETH, mclist->reg_id);
1863 if (mclist->tunnel_reg_id)
1864 mlx4_flow_detach(mdev->dev, mclist->tunnel_reg_id);
1866 mlx4_en_clear_list(dev);
1867 list_for_each_entry_safe(mclist, tmp, &priv->curr_list, list) {
1868 list_del(&mclist->list);
1872 /* Flush multicast filter */
1873 mlx4_SET_MCAST_FLTR(mdev->dev, priv->port, 0, 1, MLX4_MCAST_CONFIG);
1875 /* Remove flow steering rules for the port*/
1876 if (mdev->dev->caps.steering_mode ==
1877 MLX4_STEERING_MODE_DEVICE_MANAGED) {
1879 list_for_each_entry_safe(flow, tmp_flow,
1880 &priv->ethtool_list, list) {
1881 mlx4_flow_detach(mdev->dev, flow->id);
1882 list_del(&flow->list);
1886 mlx4_en_destroy_drop_qp(priv);
1889 for (t = 0; t < MLX4_EN_NUM_TX_TYPES; t++) {
1890 for (i = 0; i < priv->tx_ring_num[t]; i++) {
1891 mlx4_en_deactivate_tx_ring(priv, priv->tx_ring[t][i]);
1892 mlx4_en_deactivate_cq(priv, priv->tx_cq[t][i]);
1897 for (t = 0; t < MLX4_EN_NUM_TX_TYPES; t++)
1898 for (i = 0; i < priv->tx_ring_num[t]; i++)
1899 mlx4_en_free_tx_buf(dev, priv->tx_ring[t][i]);
1901 if (mdev->dev->caps.steering_mode != MLX4_STEERING_MODE_A0)
1902 mlx4_en_delete_rss_steer_rules(priv);
1905 mlx4_en_release_rss_steer(priv);
1907 /* Unregister Mac address for the port */
1908 mlx4_en_put_qp(priv);
1909 if (!(mdev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_REASSIGN_MAC_EN))
1910 mdev->mac_removed[priv->port] = 1;
1913 for (i = 0; i < priv->rx_ring_num; i++) {
1914 struct mlx4_en_cq *cq = priv->rx_cq[i];
1916 napi_synchronize(&cq->napi);
1917 mlx4_en_deactivate_rx_ring(priv, priv->rx_ring[i]);
1918 mlx4_en_deactivate_cq(priv, cq);
1920 mlx4_en_free_affinity_hint(priv, i);
1924 static void mlx4_en_restart(struct work_struct *work)
1926 struct mlx4_en_priv *priv = container_of(work, struct mlx4_en_priv,
1928 struct mlx4_en_dev *mdev = priv->mdev;
1929 struct net_device *dev = priv->dev;
1931 en_dbg(DRV, priv, "Watchdog task called for port %d\n", priv->port);
1934 mutex_lock(&mdev->state_lock);
1935 if (priv->port_up) {
1936 mlx4_en_stop_port(dev, 1);
1937 if (mlx4_en_start_port(dev))
1938 en_err(priv, "Failed restarting port %d\n", priv->port);
1940 mutex_unlock(&mdev->state_lock);
1944 static void mlx4_en_clear_stats(struct net_device *dev)
1946 struct mlx4_en_priv *priv = netdev_priv(dev);
1947 struct mlx4_en_dev *mdev = priv->mdev;
1948 struct mlx4_en_tx_ring **tx_ring;
1951 if (!mlx4_is_slave(mdev->dev))
1952 if (mlx4_en_DUMP_ETH_STATS(mdev, priv->port, 1))
1953 en_dbg(HW, priv, "Failed dumping statistics\n");
1955 memset(&priv->pstats, 0, sizeof(priv->pstats));
1956 memset(&priv->pkstats, 0, sizeof(priv->pkstats));
1957 memset(&priv->port_stats, 0, sizeof(priv->port_stats));
1958 memset(&priv->rx_flowstats, 0, sizeof(priv->rx_flowstats));
1959 memset(&priv->tx_flowstats, 0, sizeof(priv->tx_flowstats));
1960 memset(&priv->rx_priority_flowstats, 0,
1961 sizeof(priv->rx_priority_flowstats));
1962 memset(&priv->tx_priority_flowstats, 0,
1963 sizeof(priv->tx_priority_flowstats));
1964 memset(&priv->pf_stats, 0, sizeof(priv->pf_stats));
1966 tx_ring = priv->tx_ring[TX];
1967 for (i = 0; i < priv->tx_ring_num[TX]; i++) {
1968 tx_ring[i]->bytes = 0;
1969 tx_ring[i]->packets = 0;
1970 tx_ring[i]->tx_csum = 0;
1971 tx_ring[i]->tx_dropped = 0;
1972 tx_ring[i]->queue_stopped = 0;
1973 tx_ring[i]->wake_queue = 0;
1974 tx_ring[i]->tso_packets = 0;
1975 tx_ring[i]->xmit_more = 0;
1977 for (i = 0; i < priv->rx_ring_num; i++) {
1978 priv->rx_ring[i]->bytes = 0;
1979 priv->rx_ring[i]->packets = 0;
1980 priv->rx_ring[i]->csum_ok = 0;
1981 priv->rx_ring[i]->csum_none = 0;
1982 priv->rx_ring[i]->csum_complete = 0;
1986 static int mlx4_en_open(struct net_device *dev)
1988 struct mlx4_en_priv *priv = netdev_priv(dev);
1989 struct mlx4_en_dev *mdev = priv->mdev;
1992 mutex_lock(&mdev->state_lock);
1994 if (!mdev->device_up) {
1995 en_err(priv, "Cannot open - device down/disabled\n");
2000 /* Reset HW statistics and SW counters */
2001 mlx4_en_clear_stats(dev);
2003 err = mlx4_en_start_port(dev);
2005 en_err(priv, "Failed starting port:%d\n", priv->port);
2008 mutex_unlock(&mdev->state_lock);
2013 static int mlx4_en_close(struct net_device *dev)
2015 struct mlx4_en_priv *priv = netdev_priv(dev);
2016 struct mlx4_en_dev *mdev = priv->mdev;
2018 en_dbg(IFDOWN, priv, "Close port called\n");
2020 mutex_lock(&mdev->state_lock);
2022 mlx4_en_stop_port(dev, 0);
2023 netif_carrier_off(dev);
2025 mutex_unlock(&mdev->state_lock);
2029 static void mlx4_en_free_resources(struct mlx4_en_priv *priv)
2033 #ifdef CONFIG_RFS_ACCEL
2034 priv->dev->rx_cpu_rmap = NULL;
2037 for (t = 0; t < MLX4_EN_NUM_TX_TYPES; t++) {
2038 for (i = 0; i < priv->tx_ring_num[t]; i++) {
2039 if (priv->tx_ring[t] && priv->tx_ring[t][i])
2040 mlx4_en_destroy_tx_ring(priv,
2041 &priv->tx_ring[t][i]);
2042 if (priv->tx_cq[t] && priv->tx_cq[t][i])
2043 mlx4_en_destroy_cq(priv, &priv->tx_cq[t][i]);
2045 kfree(priv->tx_ring[t]);
2046 kfree(priv->tx_cq[t]);
2049 for (i = 0; i < priv->rx_ring_num; i++) {
2050 if (priv->rx_ring[i])
2051 mlx4_en_destroy_rx_ring(priv, &priv->rx_ring[i],
2052 priv->prof->rx_ring_size, priv->stride);
2054 mlx4_en_destroy_cq(priv, &priv->rx_cq[i]);
2059 static int mlx4_en_alloc_resources(struct mlx4_en_priv *priv)
2061 struct mlx4_en_port_profile *prof = priv->prof;
2065 /* Create tx Rings */
2066 for (t = 0; t < MLX4_EN_NUM_TX_TYPES; t++) {
2067 for (i = 0; i < priv->tx_ring_num[t]; i++) {
2068 node = cpu_to_node(i % num_online_cpus());
2069 if (mlx4_en_create_cq(priv, &priv->tx_cq[t][i],
2070 prof->tx_ring_size, i, t, node))
2073 if (mlx4_en_create_tx_ring(priv, &priv->tx_ring[t][i],
2075 TXBB_SIZE, node, i))
2080 /* Create rx Rings */
2081 for (i = 0; i < priv->rx_ring_num; i++) {
2082 node = cpu_to_node(i % num_online_cpus());
2083 if (mlx4_en_create_cq(priv, &priv->rx_cq[i],
2084 prof->rx_ring_size, i, RX, node))
2087 if (mlx4_en_create_rx_ring(priv, &priv->rx_ring[i],
2088 prof->rx_ring_size, priv->stride,
2093 #ifdef CONFIG_RFS_ACCEL
2094 priv->dev->rx_cpu_rmap = mlx4_get_cpu_rmap(priv->mdev->dev, priv->port);
2100 en_err(priv, "Failed to allocate NIC resources\n");
2101 for (i = 0; i < priv->rx_ring_num; i++) {
2102 if (priv->rx_ring[i])
2103 mlx4_en_destroy_rx_ring(priv, &priv->rx_ring[i],
2107 mlx4_en_destroy_cq(priv, &priv->rx_cq[i]);
2109 for (t = 0; t < MLX4_EN_NUM_TX_TYPES; t++) {
2110 for (i = 0; i < priv->tx_ring_num[t]; i++) {
2111 if (priv->tx_ring[t][i])
2112 mlx4_en_destroy_tx_ring(priv,
2113 &priv->tx_ring[t][i]);
2114 if (priv->tx_cq[t][i])
2115 mlx4_en_destroy_cq(priv, &priv->tx_cq[t][i]);
2122 static int mlx4_en_copy_priv(struct mlx4_en_priv *dst,
2123 struct mlx4_en_priv *src,
2124 struct mlx4_en_port_profile *prof)
2128 memcpy(&dst->hwtstamp_config, &prof->hwtstamp_config,
2129 sizeof(dst->hwtstamp_config));
2130 dst->num_tx_rings_p_up = src->mdev->profile.num_tx_rings_p_up;
2131 dst->rx_ring_num = prof->rx_ring_num;
2132 dst->flags = prof->flags;
2133 dst->mdev = src->mdev;
2134 dst->port = src->port;
2135 dst->dev = src->dev;
2137 dst->stride = roundup_pow_of_two(sizeof(struct mlx4_en_rx_desc) +
2138 DS_SIZE * MLX4_EN_MAX_RX_FRAGS);
2140 for (t = 0; t < MLX4_EN_NUM_TX_TYPES; t++) {
2141 dst->tx_ring_num[t] = prof->tx_ring_num[t];
2142 if (!dst->tx_ring_num[t])
2145 dst->tx_ring[t] = kzalloc(sizeof(struct mlx4_en_tx_ring *) *
2146 MAX_TX_RINGS, GFP_KERNEL);
2147 if (!dst->tx_ring[t])
2150 dst->tx_cq[t] = kzalloc(sizeof(struct mlx4_en_cq *) *
2151 MAX_TX_RINGS, GFP_KERNEL);
2152 if (!dst->tx_cq[t]) {
2153 kfree(dst->tx_ring[t]);
2162 kfree(dst->tx_ring[t]);
2163 kfree(dst->tx_cq[t]);
2168 static void mlx4_en_update_priv(struct mlx4_en_priv *dst,
2169 struct mlx4_en_priv *src)
2172 memcpy(dst->rx_ring, src->rx_ring,
2173 sizeof(struct mlx4_en_rx_ring *) * src->rx_ring_num);
2174 memcpy(dst->rx_cq, src->rx_cq,
2175 sizeof(struct mlx4_en_cq *) * src->rx_ring_num);
2176 memcpy(&dst->hwtstamp_config, &src->hwtstamp_config,
2177 sizeof(dst->hwtstamp_config));
2178 for (t = 0; t < MLX4_EN_NUM_TX_TYPES; t++) {
2179 dst->tx_ring_num[t] = src->tx_ring_num[t];
2180 dst->tx_ring[t] = src->tx_ring[t];
2181 dst->tx_cq[t] = src->tx_cq[t];
2183 dst->rx_ring_num = src->rx_ring_num;
2184 memcpy(dst->prof, src->prof, sizeof(struct mlx4_en_port_profile));
2187 int mlx4_en_try_alloc_resources(struct mlx4_en_priv *priv,
2188 struct mlx4_en_priv *tmp,
2189 struct mlx4_en_port_profile *prof,
2190 bool carry_xdp_prog)
2192 struct bpf_prog *xdp_prog;
2195 mlx4_en_copy_priv(tmp, priv, prof);
2197 if (mlx4_en_alloc_resources(tmp)) {
2199 "%s: Resource allocation failed, using previous configuration\n",
2201 for (t = 0; t < MLX4_EN_NUM_TX_TYPES; t++) {
2202 kfree(tmp->tx_ring[t]);
2203 kfree(tmp->tx_cq[t]);
2208 /* All rx_rings has the same xdp_prog. Pick the first one. */
2209 xdp_prog = rcu_dereference_protected(
2210 priv->rx_ring[0]->xdp_prog,
2211 lockdep_is_held(&priv->mdev->state_lock));
2213 if (xdp_prog && carry_xdp_prog) {
2214 xdp_prog = bpf_prog_add(xdp_prog, tmp->rx_ring_num);
2215 if (IS_ERR(xdp_prog)) {
2216 mlx4_en_free_resources(tmp);
2217 return PTR_ERR(xdp_prog);
2219 for (i = 0; i < tmp->rx_ring_num; i++)
2220 rcu_assign_pointer(tmp->rx_ring[i]->xdp_prog,
2227 void mlx4_en_safe_replace_resources(struct mlx4_en_priv *priv,
2228 struct mlx4_en_priv *tmp)
2230 mlx4_en_free_resources(priv);
2231 mlx4_en_update_priv(priv, tmp);
2234 void mlx4_en_destroy_netdev(struct net_device *dev)
2236 struct mlx4_en_priv *priv = netdev_priv(dev);
2237 struct mlx4_en_dev *mdev = priv->mdev;
2239 en_dbg(DRV, priv, "Destroying netdev on port:%d\n", priv->port);
2241 /* Unregister device - this will close the port if it was up */
2242 if (priv->registered) {
2243 devlink_port_type_clear(mlx4_get_devlink_port(mdev->dev,
2245 unregister_netdev(dev);
2248 if (priv->allocated)
2249 mlx4_free_hwq_res(mdev->dev, &priv->res, MLX4_EN_PAGE_SIZE);
2251 cancel_delayed_work(&priv->stats_task);
2252 cancel_delayed_work(&priv->service_task);
2253 /* flush any pending task for this netdev */
2254 flush_workqueue(mdev->workqueue);
2256 if (mdev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_TS)
2257 mlx4_en_remove_timestamp(mdev);
2259 /* Detach the netdev so tasks would not attempt to access it */
2260 mutex_lock(&mdev->state_lock);
2261 mdev->pndev[priv->port] = NULL;
2262 mdev->upper[priv->port] = NULL;
2264 #ifdef CONFIG_RFS_ACCEL
2265 mlx4_en_cleanup_filters(priv);
2268 mlx4_en_free_resources(priv);
2269 mutex_unlock(&mdev->state_lock);
2274 static bool mlx4_en_check_xdp_mtu(struct net_device *dev, int mtu)
2276 struct mlx4_en_priv *priv = netdev_priv(dev);
2278 if (mtu > MLX4_EN_MAX_XDP_MTU) {
2279 en_err(priv, "mtu:%d > max:%d when XDP prog is attached\n",
2280 mtu, MLX4_EN_MAX_XDP_MTU);
2287 static int mlx4_en_change_mtu(struct net_device *dev, int new_mtu)
2289 struct mlx4_en_priv *priv = netdev_priv(dev);
2290 struct mlx4_en_dev *mdev = priv->mdev;
2293 en_dbg(DRV, priv, "Change MTU called - current:%d new:%d\n",
2296 if (priv->tx_ring_num[TX_XDP] &&
2297 !mlx4_en_check_xdp_mtu(dev, new_mtu))
2302 if (netif_running(dev)) {
2303 mutex_lock(&mdev->state_lock);
2304 if (!mdev->device_up) {
2305 /* NIC is probably restarting - let watchdog task reset
2307 en_dbg(DRV, priv, "Change MTU called with card down!?\n");
2309 mlx4_en_stop_port(dev, 1);
2310 err = mlx4_en_start_port(dev);
2312 en_err(priv, "Failed restarting port:%d\n",
2314 queue_work(mdev->workqueue, &priv->watchdog_task);
2317 mutex_unlock(&mdev->state_lock);
2322 static int mlx4_en_hwtstamp_set(struct net_device *dev, struct ifreq *ifr)
2324 struct mlx4_en_priv *priv = netdev_priv(dev);
2325 struct mlx4_en_dev *mdev = priv->mdev;
2326 struct hwtstamp_config config;
2328 if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
2331 /* reserved for future extensions */
2335 /* device doesn't support time stamping */
2336 if (!(mdev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_TS))
2339 /* TX HW timestamp */
2340 switch (config.tx_type) {
2341 case HWTSTAMP_TX_OFF:
2342 case HWTSTAMP_TX_ON:
2348 /* RX HW timestamp */
2349 switch (config.rx_filter) {
2350 case HWTSTAMP_FILTER_NONE:
2352 case HWTSTAMP_FILTER_ALL:
2353 case HWTSTAMP_FILTER_SOME:
2354 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
2355 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
2356 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
2357 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
2358 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
2359 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
2360 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
2361 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
2362 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
2363 case HWTSTAMP_FILTER_PTP_V2_EVENT:
2364 case HWTSTAMP_FILTER_PTP_V2_SYNC:
2365 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
2366 config.rx_filter = HWTSTAMP_FILTER_ALL;
2372 if (mlx4_en_reset_config(dev, config, dev->features)) {
2373 config.tx_type = HWTSTAMP_TX_OFF;
2374 config.rx_filter = HWTSTAMP_FILTER_NONE;
2377 return copy_to_user(ifr->ifr_data, &config,
2378 sizeof(config)) ? -EFAULT : 0;
2381 static int mlx4_en_hwtstamp_get(struct net_device *dev, struct ifreq *ifr)
2383 struct mlx4_en_priv *priv = netdev_priv(dev);
2385 return copy_to_user(ifr->ifr_data, &priv->hwtstamp_config,
2386 sizeof(priv->hwtstamp_config)) ? -EFAULT : 0;
2389 static int mlx4_en_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
2393 return mlx4_en_hwtstamp_set(dev, ifr);
2395 return mlx4_en_hwtstamp_get(dev, ifr);
2401 static netdev_features_t mlx4_en_fix_features(struct net_device *netdev,
2402 netdev_features_t features)
2404 struct mlx4_en_priv *en_priv = netdev_priv(netdev);
2405 struct mlx4_en_dev *mdev = en_priv->mdev;
2407 /* Since there is no support for separate RX C-TAG/S-TAG vlan accel
2408 * enable/disable make sure S-TAG flag is always in same state as
2411 if (features & NETIF_F_HW_VLAN_CTAG_RX &&
2412 !(mdev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_SKIP_OUTER_VLAN))
2413 features |= NETIF_F_HW_VLAN_STAG_RX;
2415 features &= ~NETIF_F_HW_VLAN_STAG_RX;
2420 static int mlx4_en_set_features(struct net_device *netdev,
2421 netdev_features_t features)
2423 struct mlx4_en_priv *priv = netdev_priv(netdev);
2427 if (DEV_FEATURE_CHANGED(netdev, features, NETIF_F_RXFCS)) {
2428 en_info(priv, "Turn %s RX-FCS\n",
2429 (features & NETIF_F_RXFCS) ? "ON" : "OFF");
2433 if (DEV_FEATURE_CHANGED(netdev, features, NETIF_F_RXALL)) {
2434 u8 ignore_fcs_value = (features & NETIF_F_RXALL) ? 1 : 0;
2436 en_info(priv, "Turn %s RX-ALL\n",
2437 ignore_fcs_value ? "ON" : "OFF");
2438 ret = mlx4_SET_PORT_fcs_check(priv->mdev->dev,
2439 priv->port, ignore_fcs_value);
2444 if (DEV_FEATURE_CHANGED(netdev, features, NETIF_F_HW_VLAN_CTAG_RX)) {
2445 en_info(priv, "Turn %s RX vlan strip offload\n",
2446 (features & NETIF_F_HW_VLAN_CTAG_RX) ? "ON" : "OFF");
2450 if (DEV_FEATURE_CHANGED(netdev, features, NETIF_F_HW_VLAN_CTAG_TX))
2451 en_info(priv, "Turn %s TX vlan strip offload\n",
2452 (features & NETIF_F_HW_VLAN_CTAG_TX) ? "ON" : "OFF");
2454 if (DEV_FEATURE_CHANGED(netdev, features, NETIF_F_HW_VLAN_STAG_TX))
2455 en_info(priv, "Turn %s TX S-VLAN strip offload\n",
2456 (features & NETIF_F_HW_VLAN_STAG_TX) ? "ON" : "OFF");
2458 if (DEV_FEATURE_CHANGED(netdev, features, NETIF_F_LOOPBACK)) {
2459 en_info(priv, "Turn %s loopback\n",
2460 (features & NETIF_F_LOOPBACK) ? "ON" : "OFF");
2461 mlx4_en_update_loopback_state(netdev, features);
2465 ret = mlx4_en_reset_config(netdev, priv->hwtstamp_config,
2474 static int mlx4_en_set_vf_mac(struct net_device *dev, int queue, u8 *mac)
2476 struct mlx4_en_priv *en_priv = netdev_priv(dev);
2477 struct mlx4_en_dev *mdev = en_priv->mdev;
2478 u64 mac_u64 = mlx4_mac_to_u64(mac);
2480 if (is_multicast_ether_addr(mac))
2483 return mlx4_set_vf_mac(mdev->dev, en_priv->port, queue, mac_u64);
2486 static int mlx4_en_set_vf_vlan(struct net_device *dev, int vf, u16 vlan, u8 qos,
2489 struct mlx4_en_priv *en_priv = netdev_priv(dev);
2490 struct mlx4_en_dev *mdev = en_priv->mdev;
2492 return mlx4_set_vf_vlan(mdev->dev, en_priv->port, vf, vlan, qos,
2496 static int mlx4_en_set_vf_rate(struct net_device *dev, int vf, int min_tx_rate,
2499 struct mlx4_en_priv *en_priv = netdev_priv(dev);
2500 struct mlx4_en_dev *mdev = en_priv->mdev;
2502 return mlx4_set_vf_rate(mdev->dev, en_priv->port, vf, min_tx_rate,
2506 static int mlx4_en_set_vf_spoofchk(struct net_device *dev, int vf, bool setting)
2508 struct mlx4_en_priv *en_priv = netdev_priv(dev);
2509 struct mlx4_en_dev *mdev = en_priv->mdev;
2511 return mlx4_set_vf_spoofchk(mdev->dev, en_priv->port, vf, setting);
2514 static int mlx4_en_get_vf_config(struct net_device *dev, int vf, struct ifla_vf_info *ivf)
2516 struct mlx4_en_priv *en_priv = netdev_priv(dev);
2517 struct mlx4_en_dev *mdev = en_priv->mdev;
2519 return mlx4_get_vf_config(mdev->dev, en_priv->port, vf, ivf);
2522 static int mlx4_en_set_vf_link_state(struct net_device *dev, int vf, int link_state)
2524 struct mlx4_en_priv *en_priv = netdev_priv(dev);
2525 struct mlx4_en_dev *mdev = en_priv->mdev;
2527 return mlx4_set_vf_link_state(mdev->dev, en_priv->port, vf, link_state);
2530 static int mlx4_en_get_vf_stats(struct net_device *dev, int vf,
2531 struct ifla_vf_stats *vf_stats)
2533 struct mlx4_en_priv *en_priv = netdev_priv(dev);
2534 struct mlx4_en_dev *mdev = en_priv->mdev;
2536 return mlx4_get_vf_stats(mdev->dev, en_priv->port, vf, vf_stats);
2539 #define PORT_ID_BYTE_LEN 8
2540 static int mlx4_en_get_phys_port_id(struct net_device *dev,
2541 struct netdev_phys_item_id *ppid)
2543 struct mlx4_en_priv *priv = netdev_priv(dev);
2544 struct mlx4_dev *mdev = priv->mdev->dev;
2546 u64 phys_port_id = mdev->caps.phys_port_id[priv->port];
2551 ppid->id_len = sizeof(phys_port_id);
2552 for (i = PORT_ID_BYTE_LEN - 1; i >= 0; --i) {
2553 ppid->id[i] = phys_port_id & 0xff;
2559 static void mlx4_en_add_vxlan_offloads(struct work_struct *work)
2562 struct mlx4_en_priv *priv = container_of(work, struct mlx4_en_priv,
2565 ret = mlx4_config_vxlan_port(priv->mdev->dev, priv->vxlan_port);
2569 ret = mlx4_SET_PORT_VXLAN(priv->mdev->dev, priv->port,
2570 VXLAN_STEER_BY_OUTER_MAC, 1);
2573 en_err(priv, "failed setting L2 tunnel configuration ret %d\n", ret);
2578 priv->dev->hw_enc_features |= NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
2580 NETIF_F_TSO | NETIF_F_TSO6 |
2581 NETIF_F_GSO_UDP_TUNNEL |
2582 NETIF_F_GSO_UDP_TUNNEL_CSUM |
2583 NETIF_F_GSO_PARTIAL;
2586 static void mlx4_en_del_vxlan_offloads(struct work_struct *work)
2589 struct mlx4_en_priv *priv = container_of(work, struct mlx4_en_priv,
2591 /* unset offloads */
2592 priv->dev->hw_enc_features &= ~(NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
2594 NETIF_F_TSO | NETIF_F_TSO6 |
2595 NETIF_F_GSO_UDP_TUNNEL |
2596 NETIF_F_GSO_UDP_TUNNEL_CSUM |
2597 NETIF_F_GSO_PARTIAL);
2599 ret = mlx4_SET_PORT_VXLAN(priv->mdev->dev, priv->port,
2600 VXLAN_STEER_BY_OUTER_MAC, 0);
2602 en_err(priv, "failed setting L2 tunnel configuration ret %d\n", ret);
2604 priv->vxlan_port = 0;
2607 static void mlx4_en_add_vxlan_port(struct net_device *dev,
2608 struct udp_tunnel_info *ti)
2610 struct mlx4_en_priv *priv = netdev_priv(dev);
2611 __be16 port = ti->port;
2612 __be16 current_port;
2614 if (ti->type != UDP_TUNNEL_TYPE_VXLAN)
2617 if (ti->sa_family != AF_INET)
2620 if (priv->mdev->dev->caps.tunnel_offload_mode != MLX4_TUNNEL_OFFLOAD_MODE_VXLAN)
2623 current_port = priv->vxlan_port;
2624 if (current_port && current_port != port) {
2625 en_warn(priv, "vxlan port %d configured, can't add port %d\n",
2626 ntohs(current_port), ntohs(port));
2630 priv->vxlan_port = port;
2631 queue_work(priv->mdev->workqueue, &priv->vxlan_add_task);
2634 static void mlx4_en_del_vxlan_port(struct net_device *dev,
2635 struct udp_tunnel_info *ti)
2637 struct mlx4_en_priv *priv = netdev_priv(dev);
2638 __be16 port = ti->port;
2639 __be16 current_port;
2641 if (ti->type != UDP_TUNNEL_TYPE_VXLAN)
2644 if (ti->sa_family != AF_INET)
2647 if (priv->mdev->dev->caps.tunnel_offload_mode != MLX4_TUNNEL_OFFLOAD_MODE_VXLAN)
2650 current_port = priv->vxlan_port;
2651 if (current_port != port) {
2652 en_dbg(DRV, priv, "vxlan port %d isn't configured, ignoring\n", ntohs(port));
2656 queue_work(priv->mdev->workqueue, &priv->vxlan_del_task);
2659 static netdev_features_t mlx4_en_features_check(struct sk_buff *skb,
2660 struct net_device *dev,
2661 netdev_features_t features)
2663 features = vlan_features_check(skb, features);
2664 features = vxlan_features_check(skb, features);
2666 /* The ConnectX-3 doesn't support outer IPv6 checksums but it does
2667 * support inner IPv6 checksums and segmentation so we need to
2668 * strip that feature if this is an IPv6 encapsulated frame.
2670 if (skb->encapsulation &&
2671 (skb->ip_summed == CHECKSUM_PARTIAL)) {
2672 struct mlx4_en_priv *priv = netdev_priv(dev);
2674 if (!priv->vxlan_port ||
2675 (ip_hdr(skb)->version != 4) ||
2676 (udp_hdr(skb)->dest != priv->vxlan_port))
2677 features &= ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
2683 static int mlx4_en_set_tx_maxrate(struct net_device *dev, int queue_index, u32 maxrate)
2685 struct mlx4_en_priv *priv = netdev_priv(dev);
2686 struct mlx4_en_tx_ring *tx_ring = priv->tx_ring[TX][queue_index];
2687 struct mlx4_update_qp_params params;
2690 if (!(priv->mdev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_QP_RATE_LIMIT))
2693 /* rate provided to us in Mbs, check if it fits into 12 bits, if not use Gbs */
2694 if (maxrate >> 12) {
2695 params.rate_unit = MLX4_QP_RATE_LIMIT_GBS;
2696 params.rate_val = maxrate / 1000;
2697 } else if (maxrate) {
2698 params.rate_unit = MLX4_QP_RATE_LIMIT_MBS;
2699 params.rate_val = maxrate;
2700 } else { /* zero serves to revoke the QP rate-limitation */
2701 params.rate_unit = 0;
2702 params.rate_val = 0;
2705 err = mlx4_update_qp(priv->mdev->dev, tx_ring->qpn, MLX4_UPDATE_QP_RATE_LIMIT,
2710 static int mlx4_xdp_set(struct net_device *dev, struct bpf_prog *prog)
2712 struct mlx4_en_priv *priv = netdev_priv(dev);
2713 struct mlx4_en_dev *mdev = priv->mdev;
2714 struct mlx4_en_port_profile new_prof;
2715 struct bpf_prog *old_prog;
2716 struct mlx4_en_priv *tmp;
2723 xdp_ring_num = prog ? priv->rx_ring_num : 0;
2725 /* No need to reconfigure buffers when simply swapping the
2726 * program for a new one.
2728 if (priv->tx_ring_num[TX_XDP] == xdp_ring_num) {
2730 prog = bpf_prog_add(prog, priv->rx_ring_num - 1);
2732 return PTR_ERR(prog);
2734 mutex_lock(&mdev->state_lock);
2735 for (i = 0; i < priv->rx_ring_num; i++) {
2736 old_prog = rcu_dereference_protected(
2737 priv->rx_ring[i]->xdp_prog,
2738 lockdep_is_held(&mdev->state_lock));
2739 rcu_assign_pointer(priv->rx_ring[i]->xdp_prog, prog);
2741 bpf_prog_put(old_prog);
2743 mutex_unlock(&mdev->state_lock);
2747 if (!mlx4_en_check_xdp_mtu(dev, dev->mtu))
2750 tmp = kzalloc(sizeof(*tmp), GFP_KERNEL);
2755 prog = bpf_prog_add(prog, priv->rx_ring_num - 1);
2757 err = PTR_ERR(prog);
2762 mutex_lock(&mdev->state_lock);
2763 memcpy(&new_prof, priv->prof, sizeof(struct mlx4_en_port_profile));
2764 new_prof.tx_ring_num[TX_XDP] = xdp_ring_num;
2766 if (priv->tx_ring_num[TX] + xdp_ring_num > MAX_TX_RINGS) {
2768 new_prof.tx_ring_num[TX] =
2769 MAX_TX_RINGS - ALIGN(xdp_ring_num, MLX4_EN_NUM_UP);
2770 en_warn(priv, "Reducing the number of TX rings, to not exceed the max total rings number.\n");
2773 err = mlx4_en_try_alloc_resources(priv, tmp, &new_prof, false);
2776 bpf_prog_sub(prog, priv->rx_ring_num - 1);
2780 if (priv->port_up) {
2782 mlx4_en_stop_port(dev, 1);
2785 mlx4_en_safe_replace_resources(priv, tmp);
2787 netif_set_real_num_tx_queues(dev, priv->tx_ring_num[TX]);
2789 for (i = 0; i < priv->rx_ring_num; i++) {
2790 old_prog = rcu_dereference_protected(
2791 priv->rx_ring[i]->xdp_prog,
2792 lockdep_is_held(&mdev->state_lock));
2793 rcu_assign_pointer(priv->rx_ring[i]->xdp_prog, prog);
2795 bpf_prog_put(old_prog);
2799 err = mlx4_en_start_port(dev);
2801 en_err(priv, "Failed starting port %d for XDP change\n",
2803 queue_work(mdev->workqueue, &priv->watchdog_task);
2808 mutex_unlock(&mdev->state_lock);
2814 static bool mlx4_xdp_attached(struct net_device *dev)
2816 struct mlx4_en_priv *priv = netdev_priv(dev);
2818 return !!priv->tx_ring_num[TX_XDP];
2821 static int mlx4_xdp(struct net_device *dev, struct netdev_xdp *xdp)
2823 switch (xdp->command) {
2824 case XDP_SETUP_PROG:
2825 return mlx4_xdp_set(dev, xdp->prog);
2826 case XDP_QUERY_PROG:
2827 xdp->prog_attached = mlx4_xdp_attached(dev);
2834 static const struct net_device_ops mlx4_netdev_ops = {
2835 .ndo_open = mlx4_en_open,
2836 .ndo_stop = mlx4_en_close,
2837 .ndo_start_xmit = mlx4_en_xmit,
2838 .ndo_select_queue = mlx4_en_select_queue,
2839 .ndo_get_stats64 = mlx4_en_get_stats64,
2840 .ndo_set_rx_mode = mlx4_en_set_rx_mode,
2841 .ndo_set_mac_address = mlx4_en_set_mac,
2842 .ndo_validate_addr = eth_validate_addr,
2843 .ndo_change_mtu = mlx4_en_change_mtu,
2844 .ndo_do_ioctl = mlx4_en_ioctl,
2845 .ndo_tx_timeout = mlx4_en_tx_timeout,
2846 .ndo_vlan_rx_add_vid = mlx4_en_vlan_rx_add_vid,
2847 .ndo_vlan_rx_kill_vid = mlx4_en_vlan_rx_kill_vid,
2848 #ifdef CONFIG_NET_POLL_CONTROLLER
2849 .ndo_poll_controller = mlx4_en_netpoll,
2851 .ndo_set_features = mlx4_en_set_features,
2852 .ndo_fix_features = mlx4_en_fix_features,
2853 .ndo_setup_tc = __mlx4_en_setup_tc,
2854 #ifdef CONFIG_RFS_ACCEL
2855 .ndo_rx_flow_steer = mlx4_en_filter_rfs,
2857 .ndo_get_phys_port_id = mlx4_en_get_phys_port_id,
2858 .ndo_udp_tunnel_add = mlx4_en_add_vxlan_port,
2859 .ndo_udp_tunnel_del = mlx4_en_del_vxlan_port,
2860 .ndo_features_check = mlx4_en_features_check,
2861 .ndo_set_tx_maxrate = mlx4_en_set_tx_maxrate,
2862 .ndo_xdp = mlx4_xdp,
2865 static const struct net_device_ops mlx4_netdev_ops_master = {
2866 .ndo_open = mlx4_en_open,
2867 .ndo_stop = mlx4_en_close,
2868 .ndo_start_xmit = mlx4_en_xmit,
2869 .ndo_select_queue = mlx4_en_select_queue,
2870 .ndo_get_stats64 = mlx4_en_get_stats64,
2871 .ndo_set_rx_mode = mlx4_en_set_rx_mode,
2872 .ndo_set_mac_address = mlx4_en_set_mac,
2873 .ndo_validate_addr = eth_validate_addr,
2874 .ndo_change_mtu = mlx4_en_change_mtu,
2875 .ndo_tx_timeout = mlx4_en_tx_timeout,
2876 .ndo_vlan_rx_add_vid = mlx4_en_vlan_rx_add_vid,
2877 .ndo_vlan_rx_kill_vid = mlx4_en_vlan_rx_kill_vid,
2878 .ndo_set_vf_mac = mlx4_en_set_vf_mac,
2879 .ndo_set_vf_vlan = mlx4_en_set_vf_vlan,
2880 .ndo_set_vf_rate = mlx4_en_set_vf_rate,
2881 .ndo_set_vf_spoofchk = mlx4_en_set_vf_spoofchk,
2882 .ndo_set_vf_link_state = mlx4_en_set_vf_link_state,
2883 .ndo_get_vf_stats = mlx4_en_get_vf_stats,
2884 .ndo_get_vf_config = mlx4_en_get_vf_config,
2885 #ifdef CONFIG_NET_POLL_CONTROLLER
2886 .ndo_poll_controller = mlx4_en_netpoll,
2888 .ndo_set_features = mlx4_en_set_features,
2889 .ndo_fix_features = mlx4_en_fix_features,
2890 .ndo_setup_tc = __mlx4_en_setup_tc,
2891 #ifdef CONFIG_RFS_ACCEL
2892 .ndo_rx_flow_steer = mlx4_en_filter_rfs,
2894 .ndo_get_phys_port_id = mlx4_en_get_phys_port_id,
2895 .ndo_udp_tunnel_add = mlx4_en_add_vxlan_port,
2896 .ndo_udp_tunnel_del = mlx4_en_del_vxlan_port,
2897 .ndo_features_check = mlx4_en_features_check,
2898 .ndo_set_tx_maxrate = mlx4_en_set_tx_maxrate,
2899 .ndo_xdp = mlx4_xdp,
2902 struct mlx4_en_bond {
2903 struct work_struct work;
2904 struct mlx4_en_priv *priv;
2906 struct mlx4_port_map port_map;
2909 static void mlx4_en_bond_work(struct work_struct *work)
2911 struct mlx4_en_bond *bond = container_of(work,
2912 struct mlx4_en_bond,
2915 struct mlx4_dev *dev = bond->priv->mdev->dev;
2917 if (bond->is_bonded) {
2918 if (!mlx4_is_bonded(dev)) {
2919 err = mlx4_bond(dev);
2921 en_err(bond->priv, "Fail to bond device\n");
2924 err = mlx4_port_map_set(dev, &bond->port_map);
2926 en_err(bond->priv, "Fail to set port map [%d][%d]: %d\n",
2927 bond->port_map.port1,
2928 bond->port_map.port2,
2931 } else if (mlx4_is_bonded(dev)) {
2932 err = mlx4_unbond(dev);
2934 en_err(bond->priv, "Fail to unbond device\n");
2936 dev_put(bond->priv->dev);
2940 static int mlx4_en_queue_bond_work(struct mlx4_en_priv *priv, int is_bonded,
2941 u8 v2p_p1, u8 v2p_p2)
2943 struct mlx4_en_bond *bond = NULL;
2945 bond = kzalloc(sizeof(*bond), GFP_ATOMIC);
2949 INIT_WORK(&bond->work, mlx4_en_bond_work);
2951 bond->is_bonded = is_bonded;
2952 bond->port_map.port1 = v2p_p1;
2953 bond->port_map.port2 = v2p_p2;
2954 dev_hold(priv->dev);
2955 queue_work(priv->mdev->workqueue, &bond->work);
2959 int mlx4_en_netdev_event(struct notifier_block *this,
2960 unsigned long event, void *ptr)
2962 struct net_device *ndev = netdev_notifier_info_to_dev(ptr);
2964 struct mlx4_en_dev *mdev;
2965 struct mlx4_dev *dev;
2966 int i, num_eth_ports = 0;
2967 bool do_bond = true;
2968 struct mlx4_en_priv *priv;
2972 if (!net_eq(dev_net(ndev), &init_net))
2975 mdev = container_of(this, struct mlx4_en_dev, nb);
2978 /* Go into this mode only when two network devices set on two ports
2979 * of the same mlx4 device are slaves of the same bonding master
2981 mlx4_foreach_port(i, dev, MLX4_PORT_TYPE_ETH) {
2983 if (!port && (mdev->pndev[i] == ndev))
2985 mdev->upper[i] = mdev->pndev[i] ?
2986 netdev_master_upper_dev_get(mdev->pndev[i]) : NULL;
2987 /* condition not met: network device is a slave */
2988 if (!mdev->upper[i])
2990 if (num_eth_ports < 2)
2992 /* condition not met: same master */
2993 if (mdev->upper[i] != mdev->upper[i-1])
2996 /* condition not met: 2 salves */
2997 do_bond = (num_eth_ports == 2) ? do_bond : false;
2999 /* handle only events that come with enough info */
3000 if ((do_bond && (event != NETDEV_BONDING_INFO)) || !port)
3003 priv = netdev_priv(ndev);
3005 struct netdev_notifier_bonding_info *notifier_info = ptr;
3006 struct netdev_bonding_info *bonding_info =
3007 ¬ifier_info->bonding_info;
3009 /* required mode 1, 2 or 4 */
3010 if ((bonding_info->master.bond_mode != BOND_MODE_ACTIVEBACKUP) &&
3011 (bonding_info->master.bond_mode != BOND_MODE_XOR) &&
3012 (bonding_info->master.bond_mode != BOND_MODE_8023AD))
3015 /* require exactly 2 slaves */
3016 if (bonding_info->master.num_slaves != 2)
3021 if (bonding_info->master.bond_mode ==
3022 BOND_MODE_ACTIVEBACKUP) {
3023 /* in active-backup mode virtual ports are
3024 * mapped to the physical port of the active
3026 if (bonding_info->slave.state ==
3027 BOND_STATE_BACKUP) {
3035 } else { /* BOND_STATE_ACTIVE */
3044 } else { /* Active-Active */
3045 /* in active-active mode a virtual port is
3046 * mapped to the native physical port if and only
3047 * if the physical port is up */
3048 __s8 link = bonding_info->slave.link;
3054 if ((link == BOND_LINK_UP) ||
3055 (link == BOND_LINK_FAIL)) {
3060 } else { /* BOND_LINK_DOWN || BOND_LINK_BACK */
3070 mlx4_en_queue_bond_work(priv, do_bond,
3071 v2p_port1, v2p_port2);
3076 void mlx4_en_update_pfc_stats_bitmap(struct mlx4_dev *dev,
3077 struct mlx4_en_stats_bitmap *stats_bitmap,
3078 u8 rx_ppp, u8 rx_pause,
3079 u8 tx_ppp, u8 tx_pause)
3081 int last_i = NUM_MAIN_STATS + NUM_PORT_STATS + NUM_PF_STATS;
3083 if (!mlx4_is_slave(dev) &&
3084 (dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_FLOWSTATS_EN)) {
3085 mutex_lock(&stats_bitmap->mutex);
3086 bitmap_clear(stats_bitmap->bitmap, last_i, NUM_FLOW_STATS);
3089 bitmap_set(stats_bitmap->bitmap, last_i,
3090 NUM_FLOW_PRIORITY_STATS_RX);
3091 last_i += NUM_FLOW_PRIORITY_STATS_RX;
3093 if (rx_pause && !(rx_ppp))
3094 bitmap_set(stats_bitmap->bitmap, last_i,
3096 last_i += NUM_FLOW_STATS_RX;
3099 bitmap_set(stats_bitmap->bitmap, last_i,
3100 NUM_FLOW_PRIORITY_STATS_TX);
3101 last_i += NUM_FLOW_PRIORITY_STATS_TX;
3103 if (tx_pause && !(tx_ppp))
3104 bitmap_set(stats_bitmap->bitmap, last_i,
3106 last_i += NUM_FLOW_STATS_TX;
3108 mutex_unlock(&stats_bitmap->mutex);
3112 void mlx4_en_set_stats_bitmap(struct mlx4_dev *dev,
3113 struct mlx4_en_stats_bitmap *stats_bitmap,
3114 u8 rx_ppp, u8 rx_pause,
3115 u8 tx_ppp, u8 tx_pause)
3119 mutex_init(&stats_bitmap->mutex);
3120 bitmap_zero(stats_bitmap->bitmap, NUM_ALL_STATS);
3122 if (mlx4_is_slave(dev)) {
3123 bitmap_set(stats_bitmap->bitmap, last_i +
3124 MLX4_FIND_NETDEV_STAT(rx_packets), 1);
3125 bitmap_set(stats_bitmap->bitmap, last_i +
3126 MLX4_FIND_NETDEV_STAT(tx_packets), 1);
3127 bitmap_set(stats_bitmap->bitmap, last_i +
3128 MLX4_FIND_NETDEV_STAT(rx_bytes), 1);
3129 bitmap_set(stats_bitmap->bitmap, last_i +
3130 MLX4_FIND_NETDEV_STAT(tx_bytes), 1);
3131 bitmap_set(stats_bitmap->bitmap, last_i +
3132 MLX4_FIND_NETDEV_STAT(rx_dropped), 1);
3133 bitmap_set(stats_bitmap->bitmap, last_i +
3134 MLX4_FIND_NETDEV_STAT(tx_dropped), 1);
3136 bitmap_set(stats_bitmap->bitmap, last_i, NUM_MAIN_STATS);
3138 last_i += NUM_MAIN_STATS;
3140 bitmap_set(stats_bitmap->bitmap, last_i, NUM_PORT_STATS);
3141 last_i += NUM_PORT_STATS;
3143 if (mlx4_is_master(dev))
3144 bitmap_set(stats_bitmap->bitmap, last_i,
3146 last_i += NUM_PF_STATS;
3148 mlx4_en_update_pfc_stats_bitmap(dev, stats_bitmap,
3151 last_i += NUM_FLOW_STATS;
3153 if (!mlx4_is_slave(dev))
3154 bitmap_set(stats_bitmap->bitmap, last_i, NUM_PKT_STATS);
3155 last_i += NUM_PKT_STATS;
3157 bitmap_set(stats_bitmap->bitmap, last_i, NUM_XDP_STATS);
3158 last_i += NUM_XDP_STATS;
3161 int mlx4_en_init_netdev(struct mlx4_en_dev *mdev, int port,
3162 struct mlx4_en_port_profile *prof)
3164 struct net_device *dev;
3165 struct mlx4_en_priv *priv;
3169 dev = alloc_etherdev_mqs(sizeof(struct mlx4_en_priv),
3170 MAX_TX_RINGS, MAX_RX_RINGS);
3174 netif_set_real_num_tx_queues(dev, prof->tx_ring_num[TX]);
3175 netif_set_real_num_rx_queues(dev, prof->rx_ring_num);
3177 SET_NETDEV_DEV(dev, &mdev->dev->persist->pdev->dev);
3178 dev->dev_port = port - 1;
3181 * Initialize driver private data
3184 priv = netdev_priv(dev);
3185 memset(priv, 0, sizeof(struct mlx4_en_priv));
3186 priv->counter_index = MLX4_SINK_COUNTER_INDEX(mdev->dev);
3187 spin_lock_init(&priv->stats_lock);
3188 INIT_WORK(&priv->rx_mode_task, mlx4_en_do_set_rx_mode);
3189 INIT_WORK(&priv->watchdog_task, mlx4_en_restart);
3190 INIT_WORK(&priv->linkstate_task, mlx4_en_linkstate);
3191 INIT_DELAYED_WORK(&priv->stats_task, mlx4_en_do_get_stats);
3192 INIT_DELAYED_WORK(&priv->service_task, mlx4_en_service_task);
3193 INIT_WORK(&priv->vxlan_add_task, mlx4_en_add_vxlan_offloads);
3194 INIT_WORK(&priv->vxlan_del_task, mlx4_en_del_vxlan_offloads);
3195 #ifdef CONFIG_RFS_ACCEL
3196 INIT_LIST_HEAD(&priv->filters);
3197 spin_lock_init(&priv->filters_lock);
3202 priv->ddev = &mdev->pdev->dev;
3205 priv->port_up = false;
3206 priv->flags = prof->flags;
3207 priv->pflags = MLX4_EN_PRIV_FLAGS_BLUEFLAME;
3208 priv->ctrl_flags = cpu_to_be32(MLX4_WQE_CTRL_CQ_UPDATE |
3209 MLX4_WQE_CTRL_SOLICITED);
3210 priv->num_tx_rings_p_up = mdev->profile.num_tx_rings_p_up;
3211 priv->tx_work_limit = MLX4_EN_DEFAULT_TX_WORK;
3212 netdev_rss_key_fill(priv->rss_key, sizeof(priv->rss_key));
3214 for (t = 0; t < MLX4_EN_NUM_TX_TYPES; t++) {
3215 priv->tx_ring_num[t] = prof->tx_ring_num[t];
3216 if (!priv->tx_ring_num[t])
3219 priv->tx_ring[t] = kzalloc(sizeof(struct mlx4_en_tx_ring *) *
3220 MAX_TX_RINGS, GFP_KERNEL);
3221 if (!priv->tx_ring[t]) {
3225 priv->tx_cq[t] = kzalloc(sizeof(struct mlx4_en_cq *) *
3226 MAX_TX_RINGS, GFP_KERNEL);
3227 if (!priv->tx_cq[t]) {
3228 kfree(priv->tx_ring[t]);
3233 priv->rx_ring_num = prof->rx_ring_num;
3234 priv->cqe_factor = (mdev->dev->caps.cqe_size == 64) ? 1 : 0;
3235 priv->cqe_size = mdev->dev->caps.cqe_size;
3236 priv->mac_index = -1;
3237 priv->msg_enable = MLX4_EN_MSG_LEVEL;
3238 #ifdef CONFIG_MLX4_EN_DCB
3239 if (!mlx4_is_slave(priv->mdev->dev)) {
3240 priv->dcbx_cap = DCB_CAP_DCBX_VER_CEE | DCB_CAP_DCBX_HOST |
3241 DCB_CAP_DCBX_VER_IEEE;
3242 priv->flags |= MLX4_EN_DCB_ENABLED;
3243 priv->cee_config.pfc_state = false;
3245 for (i = 0; i < MLX4_EN_NUM_UP; i++)
3246 priv->cee_config.dcb_pfc[i] = pfc_disabled;
3248 if (mdev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_ETS_CFG) {
3249 dev->dcbnl_ops = &mlx4_en_dcbnl_ops;
3251 en_info(priv, "enabling only PFC DCB ops\n");
3252 dev->dcbnl_ops = &mlx4_en_dcbnl_pfc_ops;
3257 for (i = 0; i < MLX4_EN_MAC_HASH_SIZE; ++i)
3258 INIT_HLIST_HEAD(&priv->mac_hash[i]);
3260 /* Query for default mac and max mtu */
3261 priv->max_mtu = mdev->dev->caps.eth_mtu_cap[priv->port];
3263 if (mdev->dev->caps.rx_checksum_flags_port[priv->port] &
3264 MLX4_RX_CSUM_MODE_VAL_NON_TCP_UDP)
3265 priv->flags |= MLX4_EN_FLAG_RX_CSUM_NON_TCP_UDP;
3267 /* Set default MAC */
3268 dev->addr_len = ETH_ALEN;
3269 mlx4_en_u64_to_mac(dev->dev_addr, mdev->dev->caps.def_mac[priv->port]);
3270 if (!is_valid_ether_addr(dev->dev_addr)) {
3271 en_err(priv, "Port: %d, invalid mac burned: %pM, quiting\n",
3272 priv->port, dev->dev_addr);
3275 } else if (mlx4_is_slave(priv->mdev->dev) &&
3276 (priv->mdev->dev->port_random_macs & 1 << priv->port)) {
3277 /* Random MAC was assigned in mlx4_slave_cap
3278 * in mlx4_core module
3280 dev->addr_assign_type |= NET_ADDR_RANDOM;
3281 en_warn(priv, "Assigned random MAC address %pM\n", dev->dev_addr);
3284 memcpy(priv->current_mac, dev->dev_addr, sizeof(priv->current_mac));
3286 priv->stride = roundup_pow_of_two(sizeof(struct mlx4_en_rx_desc) +
3287 DS_SIZE * MLX4_EN_MAX_RX_FRAGS);
3288 err = mlx4_en_alloc_resources(priv);
3292 /* Initialize time stamping config */
3293 priv->hwtstamp_config.flags = 0;
3294 priv->hwtstamp_config.tx_type = HWTSTAMP_TX_OFF;
3295 priv->hwtstamp_config.rx_filter = HWTSTAMP_FILTER_NONE;
3297 /* Allocate page for receive rings */
3298 err = mlx4_alloc_hwq_res(mdev->dev, &priv->res,
3301 en_err(priv, "Failed to allocate page for rx qps\n");
3304 priv->allocated = 1;
3307 * Initialize netdev entry points
3309 if (mlx4_is_master(priv->mdev->dev))
3310 dev->netdev_ops = &mlx4_netdev_ops_master;
3312 dev->netdev_ops = &mlx4_netdev_ops;
3313 dev->watchdog_timeo = MLX4_EN_WATCHDOG_TIMEOUT;
3314 netif_set_real_num_tx_queues(dev, priv->tx_ring_num[TX]);
3315 netif_set_real_num_rx_queues(dev, priv->rx_ring_num);
3317 dev->ethtool_ops = &mlx4_en_ethtool_ops;
3320 * Set driver features
3322 dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM;
3323 if (mdev->LSO_support)
3324 dev->hw_features |= NETIF_F_TSO | NETIF_F_TSO6;
3326 dev->vlan_features = dev->hw_features;
3328 dev->hw_features |= NETIF_F_RXCSUM | NETIF_F_RXHASH;
3329 dev->features = dev->hw_features | NETIF_F_HIGHDMA |
3330 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX |
3331 NETIF_F_HW_VLAN_CTAG_FILTER;
3332 dev->hw_features |= NETIF_F_LOOPBACK |
3333 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX;
3335 if (!(mdev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_SKIP_OUTER_VLAN)) {
3336 dev->features |= NETIF_F_HW_VLAN_STAG_RX |
3337 NETIF_F_HW_VLAN_STAG_FILTER;
3338 dev->hw_features |= NETIF_F_HW_VLAN_STAG_RX;
3341 if (mlx4_is_slave(mdev->dev)) {
3342 bool vlan_offload_disabled;
3345 err = get_phv_bit(mdev->dev, port, &phv);
3347 dev->hw_features |= NETIF_F_HW_VLAN_STAG_TX;
3348 priv->pflags |= MLX4_EN_PRIV_FLAGS_PHV;
3350 err = mlx4_get_is_vlan_offload_disabled(mdev->dev, port,
3351 &vlan_offload_disabled);
3352 if (!err && vlan_offload_disabled) {
3353 dev->hw_features &= ~(NETIF_F_HW_VLAN_CTAG_TX |
3354 NETIF_F_HW_VLAN_CTAG_RX |
3355 NETIF_F_HW_VLAN_STAG_TX |
3356 NETIF_F_HW_VLAN_STAG_RX);
3357 dev->features &= ~(NETIF_F_HW_VLAN_CTAG_TX |
3358 NETIF_F_HW_VLAN_CTAG_RX |
3359 NETIF_F_HW_VLAN_STAG_TX |
3360 NETIF_F_HW_VLAN_STAG_RX);
3363 if (mdev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_PHV_EN &&
3364 !(mdev->dev->caps.flags2 &
3365 MLX4_DEV_CAP_FLAG2_SKIP_OUTER_VLAN))
3366 dev->hw_features |= NETIF_F_HW_VLAN_STAG_TX;
3369 if (mdev->dev->caps.flags & MLX4_DEV_CAP_FLAG_FCS_KEEP)
3370 dev->hw_features |= NETIF_F_RXFCS;
3372 if (mdev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_IGNORE_FCS)
3373 dev->hw_features |= NETIF_F_RXALL;
3375 if (mdev->dev->caps.steering_mode ==
3376 MLX4_STEERING_MODE_DEVICE_MANAGED &&
3377 mdev->dev->caps.dmfs_high_steer_mode != MLX4_STEERING_DMFS_A0_STATIC)
3378 dev->hw_features |= NETIF_F_NTUPLE;
3380 if (mdev->dev->caps.steering_mode != MLX4_STEERING_MODE_A0)
3381 dev->priv_flags |= IFF_UNICAST_FLT;
3383 /* Setting a default hash function value */
3384 if (mdev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_RSS_TOP) {
3385 priv->rss_hash_fn = ETH_RSS_HASH_TOP;
3386 } else if (mdev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_RSS_XOR) {
3387 priv->rss_hash_fn = ETH_RSS_HASH_XOR;
3390 "No RSS hash capabilities exposed, using Toeplitz\n");
3391 priv->rss_hash_fn = ETH_RSS_HASH_TOP;
3394 if (mdev->dev->caps.tunnel_offload_mode == MLX4_TUNNEL_OFFLOAD_MODE_VXLAN) {
3395 dev->hw_features |= NETIF_F_GSO_UDP_TUNNEL |
3396 NETIF_F_GSO_UDP_TUNNEL_CSUM |
3397 NETIF_F_GSO_PARTIAL;
3398 dev->features |= NETIF_F_GSO_UDP_TUNNEL |
3399 NETIF_F_GSO_UDP_TUNNEL_CSUM |
3400 NETIF_F_GSO_PARTIAL;
3401 dev->gso_partial_features = NETIF_F_GSO_UDP_TUNNEL_CSUM;
3404 /* MTU range: 46 - hw-specific max */
3405 dev->min_mtu = MLX4_EN_MIN_MTU;
3406 dev->max_mtu = priv->max_mtu;
3408 mdev->pndev[port] = dev;
3409 mdev->upper[port] = NULL;
3411 netif_carrier_off(dev);
3412 mlx4_en_set_default_moderation(priv);
3414 en_warn(priv, "Using %d TX rings\n", prof->tx_ring_num[TX]);
3415 en_warn(priv, "Using %d RX rings\n", prof->rx_ring_num);
3417 mlx4_en_update_loopback_state(priv->dev, priv->dev->features);
3419 /* Configure port */
3420 mlx4_en_calc_rx_buf(dev);
3421 err = mlx4_SET_PORT_general(mdev->dev, priv->port,
3422 priv->rx_skb_size + ETH_FCS_LEN,
3423 prof->tx_pause, prof->tx_ppp,
3424 prof->rx_pause, prof->rx_ppp);
3426 en_err(priv, "Failed setting port general configurations for port %d, with error %d\n",
3431 if (mdev->dev->caps.tunnel_offload_mode == MLX4_TUNNEL_OFFLOAD_MODE_VXLAN) {
3432 err = mlx4_SET_PORT_VXLAN(mdev->dev, priv->port, VXLAN_STEER_BY_OUTER_MAC, 1);
3434 en_err(priv, "Failed setting port L2 tunnel configuration, err %d\n",
3441 en_warn(priv, "Initializing port\n");
3442 err = mlx4_INIT_PORT(mdev->dev, priv->port);
3444 en_err(priv, "Failed Initializing port\n");
3447 queue_delayed_work(mdev->workqueue, &priv->stats_task, STATS_DELAY);
3449 /* Initialize time stamp mechanism */
3450 if (mdev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_TS)
3451 mlx4_en_init_timestamp(mdev);
3453 queue_delayed_work(mdev->workqueue, &priv->service_task,
3454 SERVICE_TASK_DELAY);
3456 mlx4_en_set_stats_bitmap(mdev->dev, &priv->stats_bitmap,
3457 mdev->profile.prof[priv->port].rx_ppp,
3458 mdev->profile.prof[priv->port].rx_pause,
3459 mdev->profile.prof[priv->port].tx_ppp,
3460 mdev->profile.prof[priv->port].tx_pause);
3462 err = register_netdev(dev);
3464 en_err(priv, "Netdev registration failed for port %d\n", port);
3468 priv->registered = 1;
3469 devlink_port_type_eth_set(mlx4_get_devlink_port(mdev->dev, priv->port),
3476 kfree(priv->tx_ring[t]);
3477 kfree(priv->tx_cq[t]);
3480 mlx4_en_destroy_netdev(dev);
3484 int mlx4_en_reset_config(struct net_device *dev,
3485 struct hwtstamp_config ts_config,
3486 netdev_features_t features)
3488 struct mlx4_en_priv *priv = netdev_priv(dev);
3489 struct mlx4_en_dev *mdev = priv->mdev;
3490 struct mlx4_en_port_profile new_prof;
3491 struct mlx4_en_priv *tmp;
3495 if (priv->hwtstamp_config.tx_type == ts_config.tx_type &&
3496 priv->hwtstamp_config.rx_filter == ts_config.rx_filter &&
3497 !DEV_FEATURE_CHANGED(dev, features, NETIF_F_HW_VLAN_CTAG_RX) &&
3498 !DEV_FEATURE_CHANGED(dev, features, NETIF_F_RXFCS))
3499 return 0; /* Nothing to change */
3501 if (DEV_FEATURE_CHANGED(dev, features, NETIF_F_HW_VLAN_CTAG_RX) &&
3502 (features & NETIF_F_HW_VLAN_CTAG_RX) &&
3503 (priv->hwtstamp_config.rx_filter != HWTSTAMP_FILTER_NONE)) {
3504 en_warn(priv, "Can't turn ON rx vlan offload while time-stamping rx filter is ON\n");
3508 tmp = kzalloc(sizeof(*tmp), GFP_KERNEL);
3512 mutex_lock(&mdev->state_lock);
3514 memcpy(&new_prof, priv->prof, sizeof(struct mlx4_en_port_profile));
3515 memcpy(&new_prof.hwtstamp_config, &ts_config, sizeof(ts_config));
3517 err = mlx4_en_try_alloc_resources(priv, tmp, &new_prof, true);
3521 if (priv->port_up) {
3523 mlx4_en_stop_port(dev, 1);
3526 en_warn(priv, "Changing device configuration rx filter(%x) rx vlan(%x)\n",
3527 ts_config.rx_filter,
3528 !!(features & NETIF_F_HW_VLAN_CTAG_RX));
3530 mlx4_en_safe_replace_resources(priv, tmp);
3532 if (DEV_FEATURE_CHANGED(dev, features, NETIF_F_HW_VLAN_CTAG_RX)) {
3533 if (features & NETIF_F_HW_VLAN_CTAG_RX)
3534 dev->features |= NETIF_F_HW_VLAN_CTAG_RX;
3536 dev->features &= ~NETIF_F_HW_VLAN_CTAG_RX;
3537 } else if (ts_config.rx_filter == HWTSTAMP_FILTER_NONE) {
3538 /* RX time-stamping is OFF, update the RX vlan offload
3539 * to the latest wanted state
3541 if (dev->wanted_features & NETIF_F_HW_VLAN_CTAG_RX)
3542 dev->features |= NETIF_F_HW_VLAN_CTAG_RX;
3544 dev->features &= ~NETIF_F_HW_VLAN_CTAG_RX;
3547 if (DEV_FEATURE_CHANGED(dev, features, NETIF_F_RXFCS)) {
3548 if (features & NETIF_F_RXFCS)
3549 dev->features |= NETIF_F_RXFCS;
3551 dev->features &= ~NETIF_F_RXFCS;
3554 /* RX vlan offload and RX time-stamping can't co-exist !
3555 * Regardless of the caller's choice,
3556 * Turn Off RX vlan offload in case of time-stamping is ON
3558 if (ts_config.rx_filter != HWTSTAMP_FILTER_NONE) {
3559 if (dev->features & NETIF_F_HW_VLAN_CTAG_RX)
3560 en_warn(priv, "Turning off RX vlan offload since RX time-stamping is ON\n");
3561 dev->features &= ~NETIF_F_HW_VLAN_CTAG_RX;
3565 err = mlx4_en_start_port(dev);
3567 en_err(priv, "Failed starting port\n");
3571 mutex_unlock(&mdev->state_lock);
3574 netdev_features_change(dev);