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/etherdevice.h>
35 #include <linux/tcp.h>
36 #include <linux/if_vlan.h>
37 #include <linux/delay.h>
38 #include <linux/slab.h>
39 #include <linux/hash.h>
41 #include <net/busy_poll.h>
42 #include <net/vxlan.h>
44 #include <linux/mlx4/driver.h>
45 #include <linux/mlx4/device.h>
46 #include <linux/mlx4/cmd.h>
47 #include <linux/mlx4/cq.h>
52 int mlx4_en_setup_tc(struct net_device *dev, u8 up)
54 struct mlx4_en_priv *priv = netdev_priv(dev);
56 unsigned int offset = 0;
58 if (up && up != MLX4_EN_NUM_UP)
61 netdev_set_num_tc(dev, up);
63 /* Partition Tx queues evenly amongst UP's */
64 for (i = 0; i < up; i++) {
65 netdev_set_tc_queue(dev, i, priv->num_tx_rings_p_up, offset);
66 offset += priv->num_tx_rings_p_up;
72 static int __mlx4_en_setup_tc(struct net_device *dev, u32 handle, __be16 proto,
73 struct tc_to_netdev *tc)
75 if (handle != TC_H_ROOT || tc->type != TC_SETUP_MQPRIO)
78 return mlx4_en_setup_tc(dev, tc->tc);
81 #ifdef CONFIG_RFS_ACCEL
83 struct mlx4_en_filter {
84 struct list_head next;
85 struct work_struct work;
94 struct mlx4_en_priv *priv;
95 u32 flow_id; /* RFS infrastructure id */
96 int id; /* mlx4_en driver id */
97 u64 reg_id; /* Flow steering API id */
98 u8 activated; /* Used to prevent expiry before filter
101 struct hlist_node filter_chain;
104 static void mlx4_en_filter_rfs_expire(struct mlx4_en_priv *priv);
106 static enum mlx4_net_trans_rule_id mlx4_ip_proto_to_trans_rule_id(u8 ip_proto)
110 return MLX4_NET_TRANS_RULE_ID_UDP;
112 return MLX4_NET_TRANS_RULE_ID_TCP;
114 return MLX4_NET_TRANS_RULE_NUM;
118 static void mlx4_en_filter_work(struct work_struct *work)
120 struct mlx4_en_filter *filter = container_of(work,
121 struct mlx4_en_filter,
123 struct mlx4_en_priv *priv = filter->priv;
124 struct mlx4_spec_list spec_tcp_udp = {
125 .id = mlx4_ip_proto_to_trans_rule_id(filter->ip_proto),
128 .dst_port = filter->dst_port,
129 .dst_port_msk = (__force __be16)-1,
130 .src_port = filter->src_port,
131 .src_port_msk = (__force __be16)-1,
135 struct mlx4_spec_list spec_ip = {
136 .id = MLX4_NET_TRANS_RULE_ID_IPV4,
139 .dst_ip = filter->dst_ip,
140 .dst_ip_msk = (__force __be32)-1,
141 .src_ip = filter->src_ip,
142 .src_ip_msk = (__force __be32)-1,
146 struct mlx4_spec_list spec_eth = {
147 .id = MLX4_NET_TRANS_RULE_ID_ETH,
149 struct mlx4_net_trans_rule rule = {
150 .list = LIST_HEAD_INIT(rule.list),
151 .queue_mode = MLX4_NET_TRANS_Q_LIFO,
154 .promisc_mode = MLX4_FS_REGULAR,
156 .priority = MLX4_DOMAIN_RFS,
159 __be64 mac_mask = cpu_to_be64(MLX4_MAC_MASK << 16);
161 if (spec_tcp_udp.id >= MLX4_NET_TRANS_RULE_NUM) {
162 en_warn(priv, "RFS: ignoring unsupported ip protocol (%d)\n",
166 list_add_tail(&spec_eth.list, &rule.list);
167 list_add_tail(&spec_ip.list, &rule.list);
168 list_add_tail(&spec_tcp_udp.list, &rule.list);
170 rule.qpn = priv->rss_map.qps[filter->rxq_index].qpn;
171 memcpy(spec_eth.eth.dst_mac, priv->dev->dev_addr, ETH_ALEN);
172 memcpy(spec_eth.eth.dst_mac_msk, &mac_mask, ETH_ALEN);
174 filter->activated = 0;
176 if (filter->reg_id) {
177 rc = mlx4_flow_detach(priv->mdev->dev, filter->reg_id);
178 if (rc && rc != -ENOENT)
179 en_err(priv, "Error detaching flow. rc = %d\n", rc);
182 rc = mlx4_flow_attach(priv->mdev->dev, &rule, &filter->reg_id);
184 en_err(priv, "Error attaching flow. err = %d\n", rc);
187 mlx4_en_filter_rfs_expire(priv);
189 filter->activated = 1;
192 static inline struct hlist_head *
193 filter_hash_bucket(struct mlx4_en_priv *priv, __be32 src_ip, __be32 dst_ip,
194 __be16 src_port, __be16 dst_port)
199 l = (__force unsigned long)src_port |
200 ((__force unsigned long)dst_port << 2);
201 l ^= (__force unsigned long)(src_ip ^ dst_ip);
203 bucket_idx = hash_long(l, MLX4_EN_FILTER_HASH_SHIFT);
205 return &priv->filter_hash[bucket_idx];
208 static struct mlx4_en_filter *
209 mlx4_en_filter_alloc(struct mlx4_en_priv *priv, int rxq_index, __be32 src_ip,
210 __be32 dst_ip, u8 ip_proto, __be16 src_port,
211 __be16 dst_port, u32 flow_id)
213 struct mlx4_en_filter *filter = NULL;
215 filter = kzalloc(sizeof(struct mlx4_en_filter), GFP_ATOMIC);
220 filter->rxq_index = rxq_index;
221 INIT_WORK(&filter->work, mlx4_en_filter_work);
223 filter->src_ip = src_ip;
224 filter->dst_ip = dst_ip;
225 filter->ip_proto = ip_proto;
226 filter->src_port = src_port;
227 filter->dst_port = dst_port;
229 filter->flow_id = flow_id;
231 filter->id = priv->last_filter_id++ % RPS_NO_FILTER;
233 list_add_tail(&filter->next, &priv->filters);
234 hlist_add_head(&filter->filter_chain,
235 filter_hash_bucket(priv, src_ip, dst_ip, src_port,
241 static void mlx4_en_filter_free(struct mlx4_en_filter *filter)
243 struct mlx4_en_priv *priv = filter->priv;
246 list_del(&filter->next);
248 rc = mlx4_flow_detach(priv->mdev->dev, filter->reg_id);
249 if (rc && rc != -ENOENT)
250 en_err(priv, "Error detaching flow. rc = %d\n", rc);
255 static inline struct mlx4_en_filter *
256 mlx4_en_filter_find(struct mlx4_en_priv *priv, __be32 src_ip, __be32 dst_ip,
257 u8 ip_proto, __be16 src_port, __be16 dst_port)
259 struct mlx4_en_filter *filter;
260 struct mlx4_en_filter *ret = NULL;
262 hlist_for_each_entry(filter,
263 filter_hash_bucket(priv, src_ip, dst_ip,
266 if (filter->src_ip == src_ip &&
267 filter->dst_ip == dst_ip &&
268 filter->ip_proto == ip_proto &&
269 filter->src_port == src_port &&
270 filter->dst_port == dst_port) {
280 mlx4_en_filter_rfs(struct net_device *net_dev, const struct sk_buff *skb,
281 u16 rxq_index, u32 flow_id)
283 struct mlx4_en_priv *priv = netdev_priv(net_dev);
284 struct mlx4_en_filter *filter;
285 const struct iphdr *ip;
292 int nhoff = skb_network_offset(skb);
295 if (skb->protocol != htons(ETH_P_IP))
296 return -EPROTONOSUPPORT;
298 ip = (const struct iphdr *)(skb->data + nhoff);
299 if (ip_is_fragment(ip))
300 return -EPROTONOSUPPORT;
302 if ((ip->protocol != IPPROTO_TCP) && (ip->protocol != IPPROTO_UDP))
303 return -EPROTONOSUPPORT;
304 ports = (const __be16 *)(skb->data + nhoff + 4 * ip->ihl);
306 ip_proto = ip->protocol;
312 spin_lock_bh(&priv->filters_lock);
313 filter = mlx4_en_filter_find(priv, src_ip, dst_ip, ip_proto,
316 if (filter->rxq_index == rxq_index)
319 filter->rxq_index = rxq_index;
321 filter = mlx4_en_filter_alloc(priv, rxq_index,
322 src_ip, dst_ip, ip_proto,
323 src_port, dst_port, flow_id);
330 queue_work(priv->mdev->workqueue, &filter->work);
335 spin_unlock_bh(&priv->filters_lock);
340 void mlx4_en_cleanup_filters(struct mlx4_en_priv *priv)
342 struct mlx4_en_filter *filter, *tmp;
345 spin_lock_bh(&priv->filters_lock);
346 list_for_each_entry_safe(filter, tmp, &priv->filters, next) {
347 list_move(&filter->next, &del_list);
348 hlist_del(&filter->filter_chain);
350 spin_unlock_bh(&priv->filters_lock);
352 list_for_each_entry_safe(filter, tmp, &del_list, next) {
353 cancel_work_sync(&filter->work);
354 mlx4_en_filter_free(filter);
358 static void mlx4_en_filter_rfs_expire(struct mlx4_en_priv *priv)
360 struct mlx4_en_filter *filter = NULL, *tmp, *last_filter = NULL;
364 spin_lock_bh(&priv->filters_lock);
365 list_for_each_entry_safe(filter, tmp, &priv->filters, next) {
366 if (i > MLX4_EN_FILTER_EXPIRY_QUOTA)
369 if (filter->activated &&
370 !work_pending(&filter->work) &&
371 rps_may_expire_flow(priv->dev,
372 filter->rxq_index, filter->flow_id,
374 list_move(&filter->next, &del_list);
375 hlist_del(&filter->filter_chain);
377 last_filter = filter;
382 if (last_filter && (&last_filter->next != priv->filters.next))
383 list_move(&priv->filters, &last_filter->next);
385 spin_unlock_bh(&priv->filters_lock);
387 list_for_each_entry_safe(filter, tmp, &del_list, next)
388 mlx4_en_filter_free(filter);
392 static int mlx4_en_vlan_rx_add_vid(struct net_device *dev,
393 __be16 proto, u16 vid)
395 struct mlx4_en_priv *priv = netdev_priv(dev);
396 struct mlx4_en_dev *mdev = priv->mdev;
400 en_dbg(HW, priv, "adding VLAN:%d\n", vid);
402 set_bit(vid, priv->active_vlans);
404 /* Add VID to port VLAN filter */
405 mutex_lock(&mdev->state_lock);
406 if (mdev->device_up && priv->port_up) {
407 err = mlx4_SET_VLAN_FLTR(mdev->dev, priv);
409 en_err(priv, "Failed configuring VLAN filter\n");
411 if (mlx4_register_vlan(mdev->dev, priv->port, vid, &idx))
412 en_dbg(HW, priv, "failed adding vlan %d\n", vid);
413 mutex_unlock(&mdev->state_lock);
418 static int mlx4_en_vlan_rx_kill_vid(struct net_device *dev,
419 __be16 proto, u16 vid)
421 struct mlx4_en_priv *priv = netdev_priv(dev);
422 struct mlx4_en_dev *mdev = priv->mdev;
425 en_dbg(HW, priv, "Killing VID:%d\n", vid);
427 clear_bit(vid, priv->active_vlans);
429 /* Remove VID from port VLAN filter */
430 mutex_lock(&mdev->state_lock);
431 mlx4_unregister_vlan(mdev->dev, priv->port, vid);
433 if (mdev->device_up && priv->port_up) {
434 err = mlx4_SET_VLAN_FLTR(mdev->dev, priv);
436 en_err(priv, "Failed configuring VLAN filter\n");
438 mutex_unlock(&mdev->state_lock);
443 static void mlx4_en_u64_to_mac(unsigned char dst_mac[ETH_ALEN + 2], u64 src_mac)
446 for (i = ETH_ALEN - 1; i >= 0; --i) {
447 dst_mac[i] = src_mac & 0xff;
450 memset(&dst_mac[ETH_ALEN], 0, 2);
454 static int mlx4_en_tunnel_steer_add(struct mlx4_en_priv *priv, unsigned char *addr,
455 int qpn, u64 *reg_id)
459 if (priv->mdev->dev->caps.tunnel_offload_mode != MLX4_TUNNEL_OFFLOAD_MODE_VXLAN ||
460 priv->mdev->dev->caps.dmfs_high_steer_mode == MLX4_STEERING_DMFS_A0_STATIC)
461 return 0; /* do nothing */
463 err = mlx4_tunnel_steer_add(priv->mdev->dev, addr, priv->port, qpn,
464 MLX4_DOMAIN_NIC, reg_id);
466 en_err(priv, "failed to add vxlan steering rule, err %d\n", err);
469 en_dbg(DRV, priv, "added vxlan steering rule, mac %pM reg_id %llx\n", addr, *reg_id);
474 static int mlx4_en_uc_steer_add(struct mlx4_en_priv *priv,
475 unsigned char *mac, int *qpn, u64 *reg_id)
477 struct mlx4_en_dev *mdev = priv->mdev;
478 struct mlx4_dev *dev = mdev->dev;
481 switch (dev->caps.steering_mode) {
482 case MLX4_STEERING_MODE_B0: {
487 memcpy(&gid[10], mac, ETH_ALEN);
490 err = mlx4_unicast_attach(dev, &qp, gid, 0, MLX4_PROT_ETH);
493 case MLX4_STEERING_MODE_DEVICE_MANAGED: {
494 struct mlx4_spec_list spec_eth = { {NULL} };
495 __be64 mac_mask = cpu_to_be64(MLX4_MAC_MASK << 16);
497 struct mlx4_net_trans_rule rule = {
498 .queue_mode = MLX4_NET_TRANS_Q_FIFO,
501 .promisc_mode = MLX4_FS_REGULAR,
502 .priority = MLX4_DOMAIN_NIC,
505 rule.port = priv->port;
507 INIT_LIST_HEAD(&rule.list);
509 spec_eth.id = MLX4_NET_TRANS_RULE_ID_ETH;
510 memcpy(spec_eth.eth.dst_mac, mac, ETH_ALEN);
511 memcpy(spec_eth.eth.dst_mac_msk, &mac_mask, ETH_ALEN);
512 list_add_tail(&spec_eth.list, &rule.list);
514 err = mlx4_flow_attach(dev, &rule, reg_id);
521 en_warn(priv, "Failed Attaching Unicast\n");
526 static void mlx4_en_uc_steer_release(struct mlx4_en_priv *priv,
527 unsigned char *mac, int qpn, u64 reg_id)
529 struct mlx4_en_dev *mdev = priv->mdev;
530 struct mlx4_dev *dev = mdev->dev;
532 switch (dev->caps.steering_mode) {
533 case MLX4_STEERING_MODE_B0: {
538 memcpy(&gid[10], mac, ETH_ALEN);
541 mlx4_unicast_detach(dev, &qp, gid, MLX4_PROT_ETH);
544 case MLX4_STEERING_MODE_DEVICE_MANAGED: {
545 mlx4_flow_detach(dev, reg_id);
549 en_err(priv, "Invalid steering mode.\n");
553 static int mlx4_en_get_qp(struct mlx4_en_priv *priv)
555 struct mlx4_en_dev *mdev = priv->mdev;
556 struct mlx4_dev *dev = mdev->dev;
559 int *qpn = &priv->base_qpn;
560 u64 mac = mlx4_mac_to_u64(priv->dev->dev_addr);
562 en_dbg(DRV, priv, "Registering MAC: %pM for adding\n",
563 priv->dev->dev_addr);
564 index = mlx4_register_mac(dev, priv->port, mac);
567 en_err(priv, "Failed adding MAC: %pM\n",
568 priv->dev->dev_addr);
572 if (dev->caps.steering_mode == MLX4_STEERING_MODE_A0) {
573 int base_qpn = mlx4_get_base_qpn(dev, priv->port);
574 *qpn = base_qpn + index;
578 err = mlx4_qp_reserve_range(dev, 1, 1, qpn, MLX4_RESERVE_A0_QP);
579 en_dbg(DRV, priv, "Reserved qp %d\n", *qpn);
581 en_err(priv, "Failed to reserve qp for mac registration\n");
582 mlx4_unregister_mac(dev, priv->port, mac);
589 static void mlx4_en_put_qp(struct mlx4_en_priv *priv)
591 struct mlx4_en_dev *mdev = priv->mdev;
592 struct mlx4_dev *dev = mdev->dev;
593 int qpn = priv->base_qpn;
595 if (dev->caps.steering_mode == MLX4_STEERING_MODE_A0) {
596 u64 mac = mlx4_mac_to_u64(priv->dev->dev_addr);
597 en_dbg(DRV, priv, "Registering MAC: %pM for deleting\n",
598 priv->dev->dev_addr);
599 mlx4_unregister_mac(dev, priv->port, mac);
601 en_dbg(DRV, priv, "Releasing qp: port %d, qpn %d\n",
603 mlx4_qp_release_range(dev, qpn, 1);
604 priv->flags &= ~MLX4_EN_FLAG_FORCE_PROMISC;
608 static int mlx4_en_replace_mac(struct mlx4_en_priv *priv, int qpn,
609 unsigned char *new_mac, unsigned char *prev_mac)
611 struct mlx4_en_dev *mdev = priv->mdev;
612 struct mlx4_dev *dev = mdev->dev;
614 u64 new_mac_u64 = mlx4_mac_to_u64(new_mac);
616 if (dev->caps.steering_mode != MLX4_STEERING_MODE_A0) {
617 struct hlist_head *bucket;
618 unsigned int mac_hash;
619 struct mlx4_mac_entry *entry;
620 struct hlist_node *tmp;
621 u64 prev_mac_u64 = mlx4_mac_to_u64(prev_mac);
623 bucket = &priv->mac_hash[prev_mac[MLX4_EN_MAC_HASH_IDX]];
624 hlist_for_each_entry_safe(entry, tmp, bucket, hlist) {
625 if (ether_addr_equal_64bits(entry->mac, prev_mac)) {
626 mlx4_en_uc_steer_release(priv, entry->mac,
628 mlx4_unregister_mac(dev, priv->port,
630 hlist_del_rcu(&entry->hlist);
632 memcpy(entry->mac, new_mac, ETH_ALEN);
634 mac_hash = new_mac[MLX4_EN_MAC_HASH_IDX];
635 hlist_add_head_rcu(&entry->hlist,
636 &priv->mac_hash[mac_hash]);
637 mlx4_register_mac(dev, priv->port, new_mac_u64);
638 err = mlx4_en_uc_steer_add(priv, new_mac,
643 if (priv->tunnel_reg_id) {
644 mlx4_flow_detach(priv->mdev->dev, priv->tunnel_reg_id);
645 priv->tunnel_reg_id = 0;
647 err = mlx4_en_tunnel_steer_add(priv, new_mac, qpn,
648 &priv->tunnel_reg_id);
655 return __mlx4_replace_mac(dev, priv->port, qpn, new_mac_u64);
658 static int mlx4_en_do_set_mac(struct mlx4_en_priv *priv,
659 unsigned char new_mac[ETH_ALEN + 2])
664 /* Remove old MAC and insert the new one */
665 err = mlx4_en_replace_mac(priv, priv->base_qpn,
666 new_mac, priv->current_mac);
668 en_err(priv, "Failed changing HW MAC address\n");
670 en_dbg(HW, priv, "Port is down while registering mac, exiting...\n");
673 memcpy(priv->current_mac, new_mac, sizeof(priv->current_mac));
678 static int mlx4_en_set_mac(struct net_device *dev, void *addr)
680 struct mlx4_en_priv *priv = netdev_priv(dev);
681 struct mlx4_en_dev *mdev = priv->mdev;
682 struct sockaddr *saddr = addr;
683 unsigned char new_mac[ETH_ALEN + 2];
686 if (!is_valid_ether_addr(saddr->sa_data))
687 return -EADDRNOTAVAIL;
689 mutex_lock(&mdev->state_lock);
690 memcpy(new_mac, saddr->sa_data, ETH_ALEN);
691 err = mlx4_en_do_set_mac(priv, new_mac);
693 memcpy(dev->dev_addr, saddr->sa_data, ETH_ALEN);
694 mutex_unlock(&mdev->state_lock);
699 static void mlx4_en_clear_list(struct net_device *dev)
701 struct mlx4_en_priv *priv = netdev_priv(dev);
702 struct mlx4_en_mc_list *tmp, *mc_to_del;
704 list_for_each_entry_safe(mc_to_del, tmp, &priv->mc_list, list) {
705 list_del(&mc_to_del->list);
710 static void mlx4_en_cache_mclist(struct net_device *dev)
712 struct mlx4_en_priv *priv = netdev_priv(dev);
713 struct netdev_hw_addr *ha;
714 struct mlx4_en_mc_list *tmp;
716 mlx4_en_clear_list(dev);
717 netdev_for_each_mc_addr(ha, dev) {
718 tmp = kzalloc(sizeof(struct mlx4_en_mc_list), GFP_ATOMIC);
720 mlx4_en_clear_list(dev);
723 memcpy(tmp->addr, ha->addr, ETH_ALEN);
724 list_add_tail(&tmp->list, &priv->mc_list);
728 static void update_mclist_flags(struct mlx4_en_priv *priv,
729 struct list_head *dst,
730 struct list_head *src)
732 struct mlx4_en_mc_list *dst_tmp, *src_tmp, *new_mc;
735 /* Find all the entries that should be removed from dst,
736 * These are the entries that are not found in src
738 list_for_each_entry(dst_tmp, dst, list) {
740 list_for_each_entry(src_tmp, src, list) {
741 if (ether_addr_equal(dst_tmp->addr, src_tmp->addr)) {
747 dst_tmp->action = MCLIST_REM;
750 /* Add entries that exist in src but not in dst
751 * mark them as need to add
753 list_for_each_entry(src_tmp, src, list) {
755 list_for_each_entry(dst_tmp, dst, list) {
756 if (ether_addr_equal(dst_tmp->addr, src_tmp->addr)) {
757 dst_tmp->action = MCLIST_NONE;
763 new_mc = kmemdup(src_tmp,
764 sizeof(struct mlx4_en_mc_list),
769 new_mc->action = MCLIST_ADD;
770 list_add_tail(&new_mc->list, dst);
775 static void mlx4_en_set_rx_mode(struct net_device *dev)
777 struct mlx4_en_priv *priv = netdev_priv(dev);
782 queue_work(priv->mdev->workqueue, &priv->rx_mode_task);
785 static void mlx4_en_set_promisc_mode(struct mlx4_en_priv *priv,
786 struct mlx4_en_dev *mdev)
790 if (!(priv->flags & MLX4_EN_FLAG_PROMISC)) {
791 if (netif_msg_rx_status(priv))
792 en_warn(priv, "Entering promiscuous mode\n");
793 priv->flags |= MLX4_EN_FLAG_PROMISC;
795 /* Enable promiscouos mode */
796 switch (mdev->dev->caps.steering_mode) {
797 case MLX4_STEERING_MODE_DEVICE_MANAGED:
798 err = mlx4_flow_steer_promisc_add(mdev->dev,
801 MLX4_FS_ALL_DEFAULT);
803 en_err(priv, "Failed enabling promiscuous mode\n");
804 priv->flags |= MLX4_EN_FLAG_MC_PROMISC;
807 case MLX4_STEERING_MODE_B0:
808 err = mlx4_unicast_promisc_add(mdev->dev,
812 en_err(priv, "Failed enabling unicast promiscuous mode\n");
814 /* Add the default qp number as multicast
817 if (!(priv->flags & MLX4_EN_FLAG_MC_PROMISC)) {
818 err = mlx4_multicast_promisc_add(mdev->dev,
822 en_err(priv, "Failed enabling multicast promiscuous mode\n");
823 priv->flags |= MLX4_EN_FLAG_MC_PROMISC;
827 case MLX4_STEERING_MODE_A0:
828 err = mlx4_SET_PORT_qpn_calc(mdev->dev,
833 en_err(priv, "Failed enabling promiscuous mode\n");
837 /* Disable port multicast filter (unconditionally) */
838 err = mlx4_SET_MCAST_FLTR(mdev->dev, priv->port, 0,
839 0, MLX4_MCAST_DISABLE);
841 en_err(priv, "Failed disabling multicast filter\n");
845 static void mlx4_en_clear_promisc_mode(struct mlx4_en_priv *priv,
846 struct mlx4_en_dev *mdev)
850 if (netif_msg_rx_status(priv))
851 en_warn(priv, "Leaving promiscuous mode\n");
852 priv->flags &= ~MLX4_EN_FLAG_PROMISC;
854 /* Disable promiscouos mode */
855 switch (mdev->dev->caps.steering_mode) {
856 case MLX4_STEERING_MODE_DEVICE_MANAGED:
857 err = mlx4_flow_steer_promisc_remove(mdev->dev,
859 MLX4_FS_ALL_DEFAULT);
861 en_err(priv, "Failed disabling promiscuous mode\n");
862 priv->flags &= ~MLX4_EN_FLAG_MC_PROMISC;
865 case MLX4_STEERING_MODE_B0:
866 err = mlx4_unicast_promisc_remove(mdev->dev,
870 en_err(priv, "Failed disabling unicast promiscuous mode\n");
871 /* Disable Multicast promisc */
872 if (priv->flags & MLX4_EN_FLAG_MC_PROMISC) {
873 err = mlx4_multicast_promisc_remove(mdev->dev,
877 en_err(priv, "Failed disabling multicast promiscuous mode\n");
878 priv->flags &= ~MLX4_EN_FLAG_MC_PROMISC;
882 case MLX4_STEERING_MODE_A0:
883 err = mlx4_SET_PORT_qpn_calc(mdev->dev,
887 en_err(priv, "Failed disabling promiscuous mode\n");
892 static void mlx4_en_do_multicast(struct mlx4_en_priv *priv,
893 struct net_device *dev,
894 struct mlx4_en_dev *mdev)
896 struct mlx4_en_mc_list *mclist, *tmp;
898 u8 mc_list[16] = {0};
901 /* Enable/disable the multicast filter according to IFF_ALLMULTI */
902 if (dev->flags & IFF_ALLMULTI) {
903 err = mlx4_SET_MCAST_FLTR(mdev->dev, priv->port, 0,
904 0, MLX4_MCAST_DISABLE);
906 en_err(priv, "Failed disabling multicast filter\n");
908 /* Add the default qp number as multicast promisc */
909 if (!(priv->flags & MLX4_EN_FLAG_MC_PROMISC)) {
910 switch (mdev->dev->caps.steering_mode) {
911 case MLX4_STEERING_MODE_DEVICE_MANAGED:
912 err = mlx4_flow_steer_promisc_add(mdev->dev,
918 case MLX4_STEERING_MODE_B0:
919 err = mlx4_multicast_promisc_add(mdev->dev,
924 case MLX4_STEERING_MODE_A0:
928 en_err(priv, "Failed entering multicast promisc mode\n");
929 priv->flags |= MLX4_EN_FLAG_MC_PROMISC;
932 /* Disable 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_remove(mdev->dev,
941 case MLX4_STEERING_MODE_B0:
942 err = mlx4_multicast_promisc_remove(mdev->dev,
947 case MLX4_STEERING_MODE_A0:
951 en_err(priv, "Failed disabling multicast promiscuous mode\n");
952 priv->flags &= ~MLX4_EN_FLAG_MC_PROMISC;
955 err = mlx4_SET_MCAST_FLTR(mdev->dev, priv->port, 0,
956 0, MLX4_MCAST_DISABLE);
958 en_err(priv, "Failed disabling multicast filter\n");
960 /* Flush mcast filter and init it with broadcast address */
961 mlx4_SET_MCAST_FLTR(mdev->dev, priv->port, ETH_BCAST,
962 1, MLX4_MCAST_CONFIG);
964 /* Update multicast list - we cache all addresses so they won't
965 * change while HW is updated holding the command semaphor */
966 netif_addr_lock_bh(dev);
967 mlx4_en_cache_mclist(dev);
968 netif_addr_unlock_bh(dev);
969 list_for_each_entry(mclist, &priv->mc_list, list) {
970 mcast_addr = mlx4_mac_to_u64(mclist->addr);
971 mlx4_SET_MCAST_FLTR(mdev->dev, priv->port,
972 mcast_addr, 0, MLX4_MCAST_CONFIG);
974 err = mlx4_SET_MCAST_FLTR(mdev->dev, priv->port, 0,
975 0, MLX4_MCAST_ENABLE);
977 en_err(priv, "Failed enabling multicast filter\n");
979 update_mclist_flags(priv, &priv->curr_list, &priv->mc_list);
980 list_for_each_entry_safe(mclist, tmp, &priv->curr_list, list) {
981 if (mclist->action == MCLIST_REM) {
982 /* detach this address and delete from list */
983 memcpy(&mc_list[10], mclist->addr, ETH_ALEN);
984 mc_list[5] = priv->port;
985 err = mlx4_multicast_detach(mdev->dev,
986 &priv->rss_map.indir_qp,
991 en_err(priv, "Fail to detach multicast address\n");
993 if (mclist->tunnel_reg_id) {
994 err = mlx4_flow_detach(priv->mdev->dev, mclist->tunnel_reg_id);
996 en_err(priv, "Failed to detach multicast address\n");
999 /* remove from list */
1000 list_del(&mclist->list);
1002 } else if (mclist->action == MCLIST_ADD) {
1003 /* attach the address */
1004 memcpy(&mc_list[10], mclist->addr, ETH_ALEN);
1005 /* needed for B0 steering support */
1006 mc_list[5] = priv->port;
1007 err = mlx4_multicast_attach(mdev->dev,
1008 &priv->rss_map.indir_qp,
1014 en_err(priv, "Fail to attach multicast address\n");
1016 err = mlx4_en_tunnel_steer_add(priv, &mc_list[10], priv->base_qpn,
1017 &mclist->tunnel_reg_id);
1019 en_err(priv, "Failed to attach multicast address\n");
1025 static void mlx4_en_do_uc_filter(struct mlx4_en_priv *priv,
1026 struct net_device *dev,
1027 struct mlx4_en_dev *mdev)
1029 struct netdev_hw_addr *ha;
1030 struct mlx4_mac_entry *entry;
1031 struct hlist_node *tmp;
1035 struct hlist_head *bucket;
1040 /* Note that we do not need to protect our mac_hash traversal with rcu,
1041 * since all modification code is protected by mdev->state_lock
1044 /* find what to remove */
1045 for (i = 0; i < MLX4_EN_MAC_HASH_SIZE; ++i) {
1046 bucket = &priv->mac_hash[i];
1047 hlist_for_each_entry_safe(entry, tmp, bucket, hlist) {
1049 netdev_for_each_uc_addr(ha, dev) {
1050 if (ether_addr_equal_64bits(entry->mac,
1057 /* MAC address of the port is not in uc list */
1058 if (ether_addr_equal_64bits(entry->mac,
1063 mac = mlx4_mac_to_u64(entry->mac);
1064 mlx4_en_uc_steer_release(priv, entry->mac,
1067 mlx4_unregister_mac(mdev->dev, priv->port, mac);
1069 hlist_del_rcu(&entry->hlist);
1070 kfree_rcu(entry, rcu);
1071 en_dbg(DRV, priv, "Removed MAC %pM on port:%d\n",
1072 entry->mac, priv->port);
1078 /* if we didn't remove anything, there is no use in trying to add
1079 * again once we are in a forced promisc mode state
1081 if ((priv->flags & MLX4_EN_FLAG_FORCE_PROMISC) && 0 == removed)
1084 prev_flags = priv->flags;
1085 priv->flags &= ~MLX4_EN_FLAG_FORCE_PROMISC;
1087 /* find what to add */
1088 netdev_for_each_uc_addr(ha, dev) {
1090 bucket = &priv->mac_hash[ha->addr[MLX4_EN_MAC_HASH_IDX]];
1091 hlist_for_each_entry(entry, bucket, hlist) {
1092 if (ether_addr_equal_64bits(entry->mac, ha->addr)) {
1099 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1101 en_err(priv, "Failed adding MAC %pM on port:%d (out of memory)\n",
1102 ha->addr, priv->port);
1103 priv->flags |= MLX4_EN_FLAG_FORCE_PROMISC;
1106 mac = mlx4_mac_to_u64(ha->addr);
1107 memcpy(entry->mac, ha->addr, ETH_ALEN);
1108 err = mlx4_register_mac(mdev->dev, priv->port, mac);
1110 en_err(priv, "Failed registering MAC %pM on port %d: %d\n",
1111 ha->addr, priv->port, err);
1113 priv->flags |= MLX4_EN_FLAG_FORCE_PROMISC;
1116 err = mlx4_en_uc_steer_add(priv, ha->addr,
1120 en_err(priv, "Failed adding MAC %pM on port %d: %d\n",
1121 ha->addr, priv->port, err);
1122 mlx4_unregister_mac(mdev->dev, priv->port, mac);
1124 priv->flags |= MLX4_EN_FLAG_FORCE_PROMISC;
1127 unsigned int mac_hash;
1128 en_dbg(DRV, priv, "Added MAC %pM on port:%d\n",
1129 ha->addr, priv->port);
1130 mac_hash = ha->addr[MLX4_EN_MAC_HASH_IDX];
1131 bucket = &priv->mac_hash[mac_hash];
1132 hlist_add_head_rcu(&entry->hlist, bucket);
1137 if (priv->flags & MLX4_EN_FLAG_FORCE_PROMISC) {
1138 en_warn(priv, "Forcing promiscuous mode on port:%d\n",
1140 } else if (prev_flags & MLX4_EN_FLAG_FORCE_PROMISC) {
1141 en_warn(priv, "Stop forcing promiscuous mode on port:%d\n",
1146 static void mlx4_en_do_set_rx_mode(struct work_struct *work)
1148 struct mlx4_en_priv *priv = container_of(work, struct mlx4_en_priv,
1150 struct mlx4_en_dev *mdev = priv->mdev;
1151 struct net_device *dev = priv->dev;
1153 mutex_lock(&mdev->state_lock);
1154 if (!mdev->device_up) {
1155 en_dbg(HW, priv, "Card is not up, ignoring rx mode change.\n");
1158 if (!priv->port_up) {
1159 en_dbg(HW, priv, "Port is down, ignoring rx mode change.\n");
1163 if (!netif_carrier_ok(dev)) {
1164 if (!mlx4_en_QUERY_PORT(mdev, priv->port)) {
1165 if (priv->port_state.link_state) {
1166 priv->last_link_state = MLX4_DEV_EVENT_PORT_UP;
1167 netif_carrier_on(dev);
1168 en_dbg(LINK, priv, "Link Up\n");
1173 if (dev->priv_flags & IFF_UNICAST_FLT)
1174 mlx4_en_do_uc_filter(priv, dev, mdev);
1176 /* Promsicuous mode: disable all filters */
1177 if ((dev->flags & IFF_PROMISC) ||
1178 (priv->flags & MLX4_EN_FLAG_FORCE_PROMISC)) {
1179 mlx4_en_set_promisc_mode(priv, mdev);
1183 /* Not in promiscuous mode */
1184 if (priv->flags & MLX4_EN_FLAG_PROMISC)
1185 mlx4_en_clear_promisc_mode(priv, mdev);
1187 mlx4_en_do_multicast(priv, dev, mdev);
1189 mutex_unlock(&mdev->state_lock);
1192 #ifdef CONFIG_NET_POLL_CONTROLLER
1193 static void mlx4_en_netpoll(struct net_device *dev)
1195 struct mlx4_en_priv *priv = netdev_priv(dev);
1196 struct mlx4_en_cq *cq;
1199 for (i = 0; i < priv->rx_ring_num; i++) {
1200 cq = priv->rx_cq[i];
1201 napi_schedule(&cq->napi);
1206 static int mlx4_en_set_rss_steer_rules(struct mlx4_en_priv *priv)
1210 int *qpn = &priv->base_qpn;
1211 struct mlx4_mac_entry *entry;
1213 err = mlx4_en_uc_steer_add(priv, priv->dev->dev_addr, qpn, ®_id);
1217 err = mlx4_en_tunnel_steer_add(priv, priv->dev->dev_addr, *qpn,
1218 &priv->tunnel_reg_id);
1222 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1228 memcpy(entry->mac, priv->dev->dev_addr, sizeof(entry->mac));
1229 memcpy(priv->current_mac, entry->mac, sizeof(priv->current_mac));
1230 entry->reg_id = reg_id;
1231 hlist_add_head_rcu(&entry->hlist,
1232 &priv->mac_hash[entry->mac[MLX4_EN_MAC_HASH_IDX]]);
1237 if (priv->tunnel_reg_id)
1238 mlx4_flow_detach(priv->mdev->dev, priv->tunnel_reg_id);
1241 mlx4_en_uc_steer_release(priv, priv->dev->dev_addr, *qpn, reg_id);
1245 static void mlx4_en_delete_rss_steer_rules(struct mlx4_en_priv *priv)
1249 int qpn = priv->base_qpn;
1250 struct hlist_head *bucket;
1251 struct hlist_node *tmp;
1252 struct mlx4_mac_entry *entry;
1254 for (i = 0; i < MLX4_EN_MAC_HASH_SIZE; ++i) {
1255 bucket = &priv->mac_hash[i];
1256 hlist_for_each_entry_safe(entry, tmp, bucket, hlist) {
1257 mac = mlx4_mac_to_u64(entry->mac);
1258 en_dbg(DRV, priv, "Registering MAC:%pM for deleting\n",
1260 mlx4_en_uc_steer_release(priv, entry->mac,
1261 qpn, entry->reg_id);
1263 mlx4_unregister_mac(priv->mdev->dev, priv->port, mac);
1264 hlist_del_rcu(&entry->hlist);
1265 kfree_rcu(entry, rcu);
1269 if (priv->tunnel_reg_id) {
1270 mlx4_flow_detach(priv->mdev->dev, priv->tunnel_reg_id);
1271 priv->tunnel_reg_id = 0;
1275 static void mlx4_en_tx_timeout(struct net_device *dev)
1277 struct mlx4_en_priv *priv = netdev_priv(dev);
1278 struct mlx4_en_dev *mdev = priv->mdev;
1281 if (netif_msg_timer(priv))
1282 en_warn(priv, "Tx timeout called on port:%d\n", priv->port);
1284 for (i = 0; i < priv->tx_ring_num; i++) {
1285 if (!netif_tx_queue_stopped(netdev_get_tx_queue(dev, i)))
1287 en_warn(priv, "TX timeout on queue: %d, QP: 0x%x, CQ: 0x%x, Cons: 0x%x, Prod: 0x%x\n",
1288 i, priv->tx_ring[i]->qpn, priv->tx_ring[i]->cqn,
1289 priv->tx_ring[i]->cons, priv->tx_ring[i]->prod);
1292 priv->port_stats.tx_timeout++;
1293 en_dbg(DRV, priv, "Scheduling watchdog\n");
1294 queue_work(mdev->workqueue, &priv->watchdog_task);
1298 static struct net_device_stats *mlx4_en_get_stats(struct net_device *dev)
1300 struct mlx4_en_priv *priv = netdev_priv(dev);
1302 spin_lock_bh(&priv->stats_lock);
1303 memcpy(&priv->ret_stats, &priv->stats, sizeof(priv->stats));
1304 spin_unlock_bh(&priv->stats_lock);
1306 return &priv->ret_stats;
1309 static void mlx4_en_set_default_moderation(struct mlx4_en_priv *priv)
1311 struct mlx4_en_cq *cq;
1314 /* If we haven't received a specific coalescing setting
1315 * (module param), we set the moderation parameters as follows:
1316 * - moder_cnt is set to the number of mtu sized packets to
1317 * satisfy our coalescing target.
1318 * - moder_time is set to a fixed value.
1320 priv->rx_frames = MLX4_EN_RX_COAL_TARGET;
1321 priv->rx_usecs = MLX4_EN_RX_COAL_TIME;
1322 priv->tx_frames = MLX4_EN_TX_COAL_PKTS;
1323 priv->tx_usecs = MLX4_EN_TX_COAL_TIME;
1324 en_dbg(INTR, priv, "Default coalesing params for mtu:%d - rx_frames:%d rx_usecs:%d\n",
1325 priv->dev->mtu, priv->rx_frames, priv->rx_usecs);
1327 /* Setup cq moderation params */
1328 for (i = 0; i < priv->rx_ring_num; i++) {
1329 cq = priv->rx_cq[i];
1330 cq->moder_cnt = priv->rx_frames;
1331 cq->moder_time = priv->rx_usecs;
1332 priv->last_moder_time[i] = MLX4_EN_AUTO_CONF;
1333 priv->last_moder_packets[i] = 0;
1334 priv->last_moder_bytes[i] = 0;
1337 for (i = 0; i < priv->tx_ring_num; i++) {
1338 cq = priv->tx_cq[i];
1339 cq->moder_cnt = priv->tx_frames;
1340 cq->moder_time = priv->tx_usecs;
1343 /* Reset auto-moderation params */
1344 priv->pkt_rate_low = MLX4_EN_RX_RATE_LOW;
1345 priv->rx_usecs_low = MLX4_EN_RX_COAL_TIME_LOW;
1346 priv->pkt_rate_high = MLX4_EN_RX_RATE_HIGH;
1347 priv->rx_usecs_high = MLX4_EN_RX_COAL_TIME_HIGH;
1348 priv->sample_interval = MLX4_EN_SAMPLE_INTERVAL;
1349 priv->adaptive_rx_coal = 1;
1350 priv->last_moder_jiffies = 0;
1351 priv->last_moder_tx_packets = 0;
1354 static void mlx4_en_auto_moderation(struct mlx4_en_priv *priv)
1356 unsigned long period = (unsigned long) (jiffies - priv->last_moder_jiffies);
1357 struct mlx4_en_cq *cq;
1358 unsigned long packets;
1360 unsigned long avg_pkt_size;
1361 unsigned long rx_packets;
1362 unsigned long rx_bytes;
1363 unsigned long rx_pkt_diff;
1367 if (!priv->adaptive_rx_coal || period < priv->sample_interval * HZ)
1370 for (ring = 0; ring < priv->rx_ring_num; ring++) {
1371 spin_lock_bh(&priv->stats_lock);
1372 rx_packets = priv->rx_ring[ring]->packets;
1373 rx_bytes = priv->rx_ring[ring]->bytes;
1374 spin_unlock_bh(&priv->stats_lock);
1376 rx_pkt_diff = ((unsigned long) (rx_packets -
1377 priv->last_moder_packets[ring]));
1378 packets = rx_pkt_diff;
1379 rate = packets * HZ / period;
1380 avg_pkt_size = packets ? ((unsigned long) (rx_bytes -
1381 priv->last_moder_bytes[ring])) / packets : 0;
1383 /* Apply auto-moderation only when packet rate
1384 * exceeds a rate that it matters */
1385 if (rate > (MLX4_EN_RX_RATE_THRESH / priv->rx_ring_num) &&
1386 avg_pkt_size > MLX4_EN_AVG_PKT_SMALL) {
1387 if (rate < priv->pkt_rate_low)
1388 moder_time = priv->rx_usecs_low;
1389 else if (rate > priv->pkt_rate_high)
1390 moder_time = priv->rx_usecs_high;
1392 moder_time = (rate - priv->pkt_rate_low) *
1393 (priv->rx_usecs_high - priv->rx_usecs_low) /
1394 (priv->pkt_rate_high - priv->pkt_rate_low) +
1397 moder_time = priv->rx_usecs_low;
1400 if (moder_time != priv->last_moder_time[ring]) {
1401 priv->last_moder_time[ring] = moder_time;
1402 cq = priv->rx_cq[ring];
1403 cq->moder_time = moder_time;
1404 cq->moder_cnt = priv->rx_frames;
1405 err = mlx4_en_set_cq_moder(priv, cq);
1407 en_err(priv, "Failed modifying moderation for cq:%d\n",
1410 priv->last_moder_packets[ring] = rx_packets;
1411 priv->last_moder_bytes[ring] = rx_bytes;
1414 priv->last_moder_jiffies = jiffies;
1417 static void mlx4_en_do_get_stats(struct work_struct *work)
1419 struct delayed_work *delay = to_delayed_work(work);
1420 struct mlx4_en_priv *priv = container_of(delay, struct mlx4_en_priv,
1422 struct mlx4_en_dev *mdev = priv->mdev;
1425 mutex_lock(&mdev->state_lock);
1426 if (mdev->device_up) {
1427 if (priv->port_up) {
1428 err = mlx4_en_DUMP_ETH_STATS(mdev, priv->port, 0);
1430 en_dbg(HW, priv, "Could not update stats\n");
1432 mlx4_en_auto_moderation(priv);
1435 queue_delayed_work(mdev->workqueue, &priv->stats_task, STATS_DELAY);
1437 if (mdev->mac_removed[MLX4_MAX_PORTS + 1 - priv->port]) {
1438 mlx4_en_do_set_mac(priv, priv->current_mac);
1439 mdev->mac_removed[MLX4_MAX_PORTS + 1 - priv->port] = 0;
1441 mutex_unlock(&mdev->state_lock);
1444 /* mlx4_en_service_task - Run service task for tasks that needed to be done
1447 static void mlx4_en_service_task(struct work_struct *work)
1449 struct delayed_work *delay = to_delayed_work(work);
1450 struct mlx4_en_priv *priv = container_of(delay, struct mlx4_en_priv,
1452 struct mlx4_en_dev *mdev = priv->mdev;
1454 mutex_lock(&mdev->state_lock);
1455 if (mdev->device_up) {
1456 if (mdev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_TS)
1457 mlx4_en_ptp_overflow_check(mdev);
1459 mlx4_en_recover_from_oom(priv);
1460 queue_delayed_work(mdev->workqueue, &priv->service_task,
1461 SERVICE_TASK_DELAY);
1463 mutex_unlock(&mdev->state_lock);
1466 static void mlx4_en_linkstate(struct work_struct *work)
1468 struct mlx4_en_priv *priv = container_of(work, struct mlx4_en_priv,
1470 struct mlx4_en_dev *mdev = priv->mdev;
1471 int linkstate = priv->link_state;
1473 mutex_lock(&mdev->state_lock);
1474 /* If observable port state changed set carrier state and
1475 * report to system log */
1476 if (priv->last_link_state != linkstate) {
1477 if (linkstate == MLX4_DEV_EVENT_PORT_DOWN) {
1478 en_info(priv, "Link Down\n");
1479 netif_carrier_off(priv->dev);
1481 en_info(priv, "Link Up\n");
1482 netif_carrier_on(priv->dev);
1485 priv->last_link_state = linkstate;
1486 mutex_unlock(&mdev->state_lock);
1489 static int mlx4_en_init_affinity_hint(struct mlx4_en_priv *priv, int ring_idx)
1491 struct mlx4_en_rx_ring *ring = priv->rx_ring[ring_idx];
1492 int numa_node = priv->mdev->dev->numa_node;
1494 if (!zalloc_cpumask_var(&ring->affinity_mask, GFP_KERNEL))
1497 cpumask_set_cpu(cpumask_local_spread(ring_idx, numa_node),
1498 ring->affinity_mask);
1502 static void mlx4_en_free_affinity_hint(struct mlx4_en_priv *priv, int ring_idx)
1504 free_cpumask_var(priv->rx_ring[ring_idx]->affinity_mask);
1507 int mlx4_en_start_port(struct net_device *dev)
1509 struct mlx4_en_priv *priv = netdev_priv(dev);
1510 struct mlx4_en_dev *mdev = priv->mdev;
1511 struct mlx4_en_cq *cq;
1512 struct mlx4_en_tx_ring *tx_ring;
1518 u8 mc_list[16] = {0};
1520 if (priv->port_up) {
1521 en_dbg(DRV, priv, "start port called while port already up\n");
1525 INIT_LIST_HEAD(&priv->mc_list);
1526 INIT_LIST_HEAD(&priv->curr_list);
1527 INIT_LIST_HEAD(&priv->ethtool_list);
1528 memset(&priv->ethtool_rules[0], 0,
1529 sizeof(struct ethtool_flow_id) * MAX_NUM_OF_FS_RULES);
1531 /* Calculate Rx buf size */
1532 dev->mtu = min(dev->mtu, priv->max_mtu);
1533 mlx4_en_calc_rx_buf(dev);
1534 en_dbg(DRV, priv, "Rx buf size:%d\n", priv->rx_skb_size);
1536 /* Configure rx cq's and rings */
1537 err = mlx4_en_activate_rx_rings(priv);
1539 en_err(priv, "Failed to activate RX rings\n");
1542 for (i = 0; i < priv->rx_ring_num; i++) {
1543 cq = priv->rx_cq[i];
1545 err = mlx4_en_init_affinity_hint(priv, i);
1547 en_err(priv, "Failed preparing IRQ affinity hint\n");
1551 err = mlx4_en_activate_cq(priv, cq, i);
1553 en_err(priv, "Failed activating Rx CQ\n");
1554 mlx4_en_free_affinity_hint(priv, i);
1558 for (j = 0; j < cq->size; j++) {
1559 struct mlx4_cqe *cqe = NULL;
1561 cqe = mlx4_en_get_cqe(cq->buf, j, priv->cqe_size) +
1563 cqe->owner_sr_opcode = MLX4_CQE_OWNER_MASK;
1566 err = mlx4_en_set_cq_moder(priv, cq);
1568 en_err(priv, "Failed setting cq moderation parameters\n");
1569 mlx4_en_deactivate_cq(priv, cq);
1570 mlx4_en_free_affinity_hint(priv, i);
1573 mlx4_en_arm_cq(priv, cq);
1574 priv->rx_ring[i]->cqn = cq->mcq.cqn;
1579 en_dbg(DRV, priv, "Getting qp number for port %d\n", priv->port);
1580 err = mlx4_en_get_qp(priv);
1582 en_err(priv, "Failed getting eth qp\n");
1585 mdev->mac_removed[priv->port] = 0;
1587 priv->counter_index =
1588 mlx4_get_default_counter_index(mdev->dev, priv->port);
1590 err = mlx4_en_config_rss_steer(priv);
1592 en_err(priv, "Failed configuring rss steering\n");
1596 err = mlx4_en_create_drop_qp(priv);
1600 /* Configure tx cq's and rings */
1601 for (i = 0; i < priv->tx_ring_num; i++) {
1603 cq = priv->tx_cq[i];
1604 err = mlx4_en_activate_cq(priv, cq, i);
1606 en_err(priv, "Failed allocating Tx CQ\n");
1609 err = mlx4_en_set_cq_moder(priv, cq);
1611 en_err(priv, "Failed setting cq moderation parameters\n");
1612 mlx4_en_deactivate_cq(priv, cq);
1615 en_dbg(DRV, priv, "Resetting index of collapsed CQ:%d to -1\n", i);
1616 cq->buf->wqe_index = cpu_to_be16(0xffff);
1618 /* Configure ring */
1619 tx_ring = priv->tx_ring[i];
1620 err = mlx4_en_activate_tx_ring(priv, tx_ring, cq->mcq.cqn,
1621 i / priv->num_tx_rings_p_up);
1623 en_err(priv, "Failed allocating Tx ring\n");
1624 mlx4_en_deactivate_cq(priv, cq);
1627 tx_ring->tx_queue = netdev_get_tx_queue(dev, i);
1629 /* Arm CQ for TX completions */
1630 mlx4_en_arm_cq(priv, cq);
1632 /* Set initial ownership of all Tx TXBBs to SW (1) */
1633 for (j = 0; j < tx_ring->buf_size; j += STAMP_STRIDE)
1634 *((u32 *) (tx_ring->buf + j)) = 0xffffffff;
1638 /* Configure port */
1639 err = mlx4_SET_PORT_general(mdev->dev, priv->port,
1640 priv->rx_skb_size + ETH_FCS_LEN,
1641 priv->prof->tx_pause,
1643 priv->prof->rx_pause,
1644 priv->prof->rx_ppp);
1646 en_err(priv, "Failed setting port general configurations for port %d, with error %d\n",
1650 /* Set default qp number */
1651 err = mlx4_SET_PORT_qpn_calc(mdev->dev, priv->port, priv->base_qpn, 0);
1653 en_err(priv, "Failed setting default qp numbers\n");
1657 if (mdev->dev->caps.tunnel_offload_mode == MLX4_TUNNEL_OFFLOAD_MODE_VXLAN) {
1658 err = mlx4_SET_PORT_VXLAN(mdev->dev, priv->port, VXLAN_STEER_BY_OUTER_MAC, 1);
1660 en_err(priv, "Failed setting port L2 tunnel configuration, err %d\n",
1667 en_dbg(HW, priv, "Initializing port\n");
1668 err = mlx4_INIT_PORT(mdev->dev, priv->port);
1670 en_err(priv, "Failed Initializing port\n");
1674 /* Set Unicast and VXLAN steering rules */
1675 if (mdev->dev->caps.steering_mode != MLX4_STEERING_MODE_A0 &&
1676 mlx4_en_set_rss_steer_rules(priv))
1677 mlx4_warn(mdev, "Failed setting steering rules\n");
1679 /* Attach rx QP to bradcast address */
1680 eth_broadcast_addr(&mc_list[10]);
1681 mc_list[5] = priv->port; /* needed for B0 steering support */
1682 if (mlx4_multicast_attach(mdev->dev, &priv->rss_map.indir_qp, mc_list,
1683 priv->port, 0, MLX4_PROT_ETH,
1684 &priv->broadcast_id))
1685 mlx4_warn(mdev, "Failed Attaching Broadcast\n");
1687 /* Must redo promiscuous mode setup. */
1688 priv->flags &= ~(MLX4_EN_FLAG_PROMISC | MLX4_EN_FLAG_MC_PROMISC);
1690 /* Schedule multicast task to populate multicast list */
1691 queue_work(mdev->workqueue, &priv->rx_mode_task);
1693 #ifdef CONFIG_MLX4_EN_VXLAN
1694 if (priv->mdev->dev->caps.tunnel_offload_mode == MLX4_TUNNEL_OFFLOAD_MODE_VXLAN)
1695 vxlan_get_rx_port(dev);
1697 priv->port_up = true;
1698 netif_tx_start_all_queues(dev);
1699 netif_device_attach(dev);
1704 while (tx_index--) {
1705 mlx4_en_deactivate_tx_ring(priv, priv->tx_ring[tx_index]);
1706 mlx4_en_deactivate_cq(priv, priv->tx_cq[tx_index]);
1708 mlx4_en_destroy_drop_qp(priv);
1710 mlx4_en_release_rss_steer(priv);
1712 mlx4_en_put_qp(priv);
1714 while (rx_index--) {
1715 mlx4_en_deactivate_cq(priv, priv->rx_cq[rx_index]);
1716 mlx4_en_free_affinity_hint(priv, rx_index);
1718 for (i = 0; i < priv->rx_ring_num; i++)
1719 mlx4_en_deactivate_rx_ring(priv, priv->rx_ring[i]);
1721 return err; /* need to close devices */
1725 void mlx4_en_stop_port(struct net_device *dev, int detach)
1727 struct mlx4_en_priv *priv = netdev_priv(dev);
1728 struct mlx4_en_dev *mdev = priv->mdev;
1729 struct mlx4_en_mc_list *mclist, *tmp;
1730 struct ethtool_flow_id *flow, *tmp_flow;
1732 u8 mc_list[16] = {0};
1734 if (!priv->port_up) {
1735 en_dbg(DRV, priv, "stop port called while port already down\n");
1740 mlx4_CLOSE_PORT(mdev->dev, priv->port);
1742 /* Synchronize with tx routine */
1743 netif_tx_lock_bh(dev);
1745 netif_device_detach(dev);
1746 netif_tx_stop_all_queues(dev);
1747 netif_tx_unlock_bh(dev);
1749 netif_tx_disable(dev);
1751 /* Set port as not active */
1752 priv->port_up = false;
1753 priv->counter_index = MLX4_SINK_COUNTER_INDEX(mdev->dev);
1755 /* Promsicuous mode */
1756 if (mdev->dev->caps.steering_mode ==
1757 MLX4_STEERING_MODE_DEVICE_MANAGED) {
1758 priv->flags &= ~(MLX4_EN_FLAG_PROMISC |
1759 MLX4_EN_FLAG_MC_PROMISC);
1760 mlx4_flow_steer_promisc_remove(mdev->dev,
1762 MLX4_FS_ALL_DEFAULT);
1763 mlx4_flow_steer_promisc_remove(mdev->dev,
1765 MLX4_FS_MC_DEFAULT);
1766 } else if (priv->flags & MLX4_EN_FLAG_PROMISC) {
1767 priv->flags &= ~MLX4_EN_FLAG_PROMISC;
1769 /* Disable promiscouos mode */
1770 mlx4_unicast_promisc_remove(mdev->dev, priv->base_qpn,
1773 /* Disable Multicast promisc */
1774 if (priv->flags & MLX4_EN_FLAG_MC_PROMISC) {
1775 mlx4_multicast_promisc_remove(mdev->dev, priv->base_qpn,
1777 priv->flags &= ~MLX4_EN_FLAG_MC_PROMISC;
1781 /* Detach All multicasts */
1782 eth_broadcast_addr(&mc_list[10]);
1783 mc_list[5] = priv->port; /* needed for B0 steering support */
1784 mlx4_multicast_detach(mdev->dev, &priv->rss_map.indir_qp, mc_list,
1785 MLX4_PROT_ETH, priv->broadcast_id);
1786 list_for_each_entry(mclist, &priv->curr_list, list) {
1787 memcpy(&mc_list[10], mclist->addr, ETH_ALEN);
1788 mc_list[5] = priv->port;
1789 mlx4_multicast_detach(mdev->dev, &priv->rss_map.indir_qp,
1790 mc_list, MLX4_PROT_ETH, mclist->reg_id);
1791 if (mclist->tunnel_reg_id)
1792 mlx4_flow_detach(mdev->dev, mclist->tunnel_reg_id);
1794 mlx4_en_clear_list(dev);
1795 list_for_each_entry_safe(mclist, tmp, &priv->curr_list, list) {
1796 list_del(&mclist->list);
1800 /* Flush multicast filter */
1801 mlx4_SET_MCAST_FLTR(mdev->dev, priv->port, 0, 1, MLX4_MCAST_CONFIG);
1803 /* Remove flow steering rules for the port*/
1804 if (mdev->dev->caps.steering_mode ==
1805 MLX4_STEERING_MODE_DEVICE_MANAGED) {
1807 list_for_each_entry_safe(flow, tmp_flow,
1808 &priv->ethtool_list, list) {
1809 mlx4_flow_detach(mdev->dev, flow->id);
1810 list_del(&flow->list);
1814 mlx4_en_destroy_drop_qp(priv);
1817 for (i = 0; i < priv->tx_ring_num; i++) {
1818 mlx4_en_deactivate_tx_ring(priv, priv->tx_ring[i]);
1819 mlx4_en_deactivate_cq(priv, priv->tx_cq[i]);
1823 for (i = 0; i < priv->tx_ring_num; i++)
1824 mlx4_en_free_tx_buf(dev, priv->tx_ring[i]);
1826 if (mdev->dev->caps.steering_mode != MLX4_STEERING_MODE_A0)
1827 mlx4_en_delete_rss_steer_rules(priv);
1830 mlx4_en_release_rss_steer(priv);
1832 /* Unregister Mac address for the port */
1833 mlx4_en_put_qp(priv);
1834 if (!(mdev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_REASSIGN_MAC_EN))
1835 mdev->mac_removed[priv->port] = 1;
1838 for (i = 0; i < priv->rx_ring_num; i++) {
1839 struct mlx4_en_cq *cq = priv->rx_cq[i];
1841 napi_synchronize(&cq->napi);
1842 mlx4_en_deactivate_rx_ring(priv, priv->rx_ring[i]);
1843 mlx4_en_deactivate_cq(priv, cq);
1845 mlx4_en_free_affinity_hint(priv, i);
1849 static void mlx4_en_restart(struct work_struct *work)
1851 struct mlx4_en_priv *priv = container_of(work, struct mlx4_en_priv,
1853 struct mlx4_en_dev *mdev = priv->mdev;
1854 struct net_device *dev = priv->dev;
1856 en_dbg(DRV, priv, "Watchdog task called for port %d\n", priv->port);
1858 mutex_lock(&mdev->state_lock);
1859 if (priv->port_up) {
1860 mlx4_en_stop_port(dev, 1);
1861 if (mlx4_en_start_port(dev))
1862 en_err(priv, "Failed restarting port %d\n", priv->port);
1864 mutex_unlock(&mdev->state_lock);
1867 static void mlx4_en_clear_stats(struct net_device *dev)
1869 struct mlx4_en_priv *priv = netdev_priv(dev);
1870 struct mlx4_en_dev *mdev = priv->mdev;
1873 if (mlx4_en_DUMP_ETH_STATS(mdev, priv->port, 1))
1874 en_dbg(HW, priv, "Failed dumping statistics\n");
1876 memset(&priv->stats, 0, sizeof(priv->stats));
1877 memset(&priv->pstats, 0, sizeof(priv->pstats));
1878 memset(&priv->pkstats, 0, sizeof(priv->pkstats));
1879 memset(&priv->port_stats, 0, sizeof(priv->port_stats));
1880 memset(&priv->rx_flowstats, 0, sizeof(priv->rx_flowstats));
1881 memset(&priv->tx_flowstats, 0, sizeof(priv->tx_flowstats));
1882 memset(&priv->rx_priority_flowstats, 0,
1883 sizeof(priv->rx_priority_flowstats));
1884 memset(&priv->tx_priority_flowstats, 0,
1885 sizeof(priv->tx_priority_flowstats));
1886 memset(&priv->pf_stats, 0, sizeof(priv->pf_stats));
1888 for (i = 0; i < priv->tx_ring_num; i++) {
1889 priv->tx_ring[i]->bytes = 0;
1890 priv->tx_ring[i]->packets = 0;
1891 priv->tx_ring[i]->tx_csum = 0;
1893 for (i = 0; i < priv->rx_ring_num; i++) {
1894 priv->rx_ring[i]->bytes = 0;
1895 priv->rx_ring[i]->packets = 0;
1896 priv->rx_ring[i]->csum_ok = 0;
1897 priv->rx_ring[i]->csum_none = 0;
1898 priv->rx_ring[i]->csum_complete = 0;
1902 static int mlx4_en_open(struct net_device *dev)
1904 struct mlx4_en_priv *priv = netdev_priv(dev);
1905 struct mlx4_en_dev *mdev = priv->mdev;
1908 mutex_lock(&mdev->state_lock);
1910 if (!mdev->device_up) {
1911 en_err(priv, "Cannot open - device down/disabled\n");
1916 /* Reset HW statistics and SW counters */
1917 mlx4_en_clear_stats(dev);
1919 err = mlx4_en_start_port(dev);
1921 en_err(priv, "Failed starting port:%d\n", priv->port);
1924 mutex_unlock(&mdev->state_lock);
1929 static int mlx4_en_close(struct net_device *dev)
1931 struct mlx4_en_priv *priv = netdev_priv(dev);
1932 struct mlx4_en_dev *mdev = priv->mdev;
1934 en_dbg(IFDOWN, priv, "Close port called\n");
1936 mutex_lock(&mdev->state_lock);
1938 mlx4_en_stop_port(dev, 0);
1939 netif_carrier_off(dev);
1941 mutex_unlock(&mdev->state_lock);
1945 void mlx4_en_free_resources(struct mlx4_en_priv *priv)
1949 #ifdef CONFIG_RFS_ACCEL
1950 priv->dev->rx_cpu_rmap = NULL;
1953 for (i = 0; i < priv->tx_ring_num; i++) {
1954 if (priv->tx_ring && priv->tx_ring[i])
1955 mlx4_en_destroy_tx_ring(priv, &priv->tx_ring[i]);
1956 if (priv->tx_cq && priv->tx_cq[i])
1957 mlx4_en_destroy_cq(priv, &priv->tx_cq[i]);
1960 for (i = 0; i < priv->rx_ring_num; i++) {
1961 if (priv->rx_ring[i])
1962 mlx4_en_destroy_rx_ring(priv, &priv->rx_ring[i],
1963 priv->prof->rx_ring_size, priv->stride);
1965 mlx4_en_destroy_cq(priv, &priv->rx_cq[i]);
1970 int mlx4_en_alloc_resources(struct mlx4_en_priv *priv)
1972 struct mlx4_en_port_profile *prof = priv->prof;
1976 /* Create tx Rings */
1977 for (i = 0; i < priv->tx_ring_num; i++) {
1978 node = cpu_to_node(i % num_online_cpus());
1979 if (mlx4_en_create_cq(priv, &priv->tx_cq[i],
1980 prof->tx_ring_size, i, TX, node))
1983 if (mlx4_en_create_tx_ring(priv, &priv->tx_ring[i],
1984 prof->tx_ring_size, TXBB_SIZE,
1989 /* Create rx Rings */
1990 for (i = 0; i < priv->rx_ring_num; i++) {
1991 node = cpu_to_node(i % num_online_cpus());
1992 if (mlx4_en_create_cq(priv, &priv->rx_cq[i],
1993 prof->rx_ring_size, i, RX, node))
1996 if (mlx4_en_create_rx_ring(priv, &priv->rx_ring[i],
1997 prof->rx_ring_size, priv->stride,
2002 #ifdef CONFIG_RFS_ACCEL
2003 priv->dev->rx_cpu_rmap = mlx4_get_cpu_rmap(priv->mdev->dev, priv->port);
2009 en_err(priv, "Failed to allocate NIC resources\n");
2010 for (i = 0; i < priv->rx_ring_num; i++) {
2011 if (priv->rx_ring[i])
2012 mlx4_en_destroy_rx_ring(priv, &priv->rx_ring[i],
2016 mlx4_en_destroy_cq(priv, &priv->rx_cq[i]);
2018 for (i = 0; i < priv->tx_ring_num; i++) {
2019 if (priv->tx_ring[i])
2020 mlx4_en_destroy_tx_ring(priv, &priv->tx_ring[i]);
2022 mlx4_en_destroy_cq(priv, &priv->tx_cq[i]);
2028 void mlx4_en_destroy_netdev(struct net_device *dev)
2030 struct mlx4_en_priv *priv = netdev_priv(dev);
2031 struct mlx4_en_dev *mdev = priv->mdev;
2033 en_dbg(DRV, priv, "Destroying netdev on port:%d\n", priv->port);
2035 /* Unregister device - this will close the port if it was up */
2036 if (priv->registered)
2037 unregister_netdev(dev);
2039 if (priv->allocated)
2040 mlx4_free_hwq_res(mdev->dev, &priv->res, MLX4_EN_PAGE_SIZE);
2042 cancel_delayed_work(&priv->stats_task);
2043 cancel_delayed_work(&priv->service_task);
2044 /* flush any pending task for this netdev */
2045 flush_workqueue(mdev->workqueue);
2047 if (mdev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_TS)
2048 mlx4_en_remove_timestamp(mdev);
2050 /* Detach the netdev so tasks would not attempt to access it */
2051 mutex_lock(&mdev->state_lock);
2052 mdev->pndev[priv->port] = NULL;
2053 mdev->upper[priv->port] = NULL;
2054 mutex_unlock(&mdev->state_lock);
2056 mlx4_en_free_resources(priv);
2058 kfree(priv->tx_ring);
2064 static int mlx4_en_change_mtu(struct net_device *dev, int new_mtu)
2066 struct mlx4_en_priv *priv = netdev_priv(dev);
2067 struct mlx4_en_dev *mdev = priv->mdev;
2070 en_dbg(DRV, priv, "Change MTU called - current:%d new:%d\n",
2073 if ((new_mtu < MLX4_EN_MIN_MTU) || (new_mtu > priv->max_mtu)) {
2074 en_err(priv, "Bad MTU size:%d.\n", new_mtu);
2079 if (netif_running(dev)) {
2080 mutex_lock(&mdev->state_lock);
2081 if (!mdev->device_up) {
2082 /* NIC is probably restarting - let watchdog task reset
2084 en_dbg(DRV, priv, "Change MTU called with card down!?\n");
2086 mlx4_en_stop_port(dev, 1);
2087 err = mlx4_en_start_port(dev);
2089 en_err(priv, "Failed restarting port:%d\n",
2091 queue_work(mdev->workqueue, &priv->watchdog_task);
2094 mutex_unlock(&mdev->state_lock);
2099 static int mlx4_en_hwtstamp_set(struct net_device *dev, struct ifreq *ifr)
2101 struct mlx4_en_priv *priv = netdev_priv(dev);
2102 struct mlx4_en_dev *mdev = priv->mdev;
2103 struct hwtstamp_config config;
2105 if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
2108 /* reserved for future extensions */
2112 /* device doesn't support time stamping */
2113 if (!(mdev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_TS))
2116 /* TX HW timestamp */
2117 switch (config.tx_type) {
2118 case HWTSTAMP_TX_OFF:
2119 case HWTSTAMP_TX_ON:
2125 /* RX HW timestamp */
2126 switch (config.rx_filter) {
2127 case HWTSTAMP_FILTER_NONE:
2129 case HWTSTAMP_FILTER_ALL:
2130 case HWTSTAMP_FILTER_SOME:
2131 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
2132 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
2133 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
2134 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
2135 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
2136 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
2137 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
2138 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
2139 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
2140 case HWTSTAMP_FILTER_PTP_V2_EVENT:
2141 case HWTSTAMP_FILTER_PTP_V2_SYNC:
2142 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
2143 config.rx_filter = HWTSTAMP_FILTER_ALL;
2149 if (mlx4_en_reset_config(dev, config, dev->features)) {
2150 config.tx_type = HWTSTAMP_TX_OFF;
2151 config.rx_filter = HWTSTAMP_FILTER_NONE;
2154 return copy_to_user(ifr->ifr_data, &config,
2155 sizeof(config)) ? -EFAULT : 0;
2158 static int mlx4_en_hwtstamp_get(struct net_device *dev, struct ifreq *ifr)
2160 struct mlx4_en_priv *priv = netdev_priv(dev);
2162 return copy_to_user(ifr->ifr_data, &priv->hwtstamp_config,
2163 sizeof(priv->hwtstamp_config)) ? -EFAULT : 0;
2166 static int mlx4_en_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
2170 return mlx4_en_hwtstamp_set(dev, ifr);
2172 return mlx4_en_hwtstamp_get(dev, ifr);
2178 static netdev_features_t mlx4_en_fix_features(struct net_device *netdev,
2179 netdev_features_t features)
2181 struct mlx4_en_priv *en_priv = netdev_priv(netdev);
2182 struct mlx4_en_dev *mdev = en_priv->mdev;
2184 /* Since there is no support for separate RX C-TAG/S-TAG vlan accel
2185 * enable/disable make sure S-TAG flag is always in same state as
2188 if (features & NETIF_F_HW_VLAN_CTAG_RX &&
2189 !(mdev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_SKIP_OUTER_VLAN))
2190 features |= NETIF_F_HW_VLAN_STAG_RX;
2192 features &= ~NETIF_F_HW_VLAN_STAG_RX;
2197 static int mlx4_en_set_features(struct net_device *netdev,
2198 netdev_features_t features)
2200 struct mlx4_en_priv *priv = netdev_priv(netdev);
2204 if (DEV_FEATURE_CHANGED(netdev, features, NETIF_F_RXFCS)) {
2205 en_info(priv, "Turn %s RX-FCS\n",
2206 (features & NETIF_F_RXFCS) ? "ON" : "OFF");
2210 if (DEV_FEATURE_CHANGED(netdev, features, NETIF_F_RXALL)) {
2211 u8 ignore_fcs_value = (features & NETIF_F_RXALL) ? 1 : 0;
2213 en_info(priv, "Turn %s RX-ALL\n",
2214 ignore_fcs_value ? "ON" : "OFF");
2215 ret = mlx4_SET_PORT_fcs_check(priv->mdev->dev,
2216 priv->port, ignore_fcs_value);
2221 if (DEV_FEATURE_CHANGED(netdev, features, NETIF_F_HW_VLAN_CTAG_RX)) {
2222 en_info(priv, "Turn %s RX vlan strip offload\n",
2223 (features & NETIF_F_HW_VLAN_CTAG_RX) ? "ON" : "OFF");
2227 if (DEV_FEATURE_CHANGED(netdev, features, NETIF_F_HW_VLAN_CTAG_TX))
2228 en_info(priv, "Turn %s TX vlan strip offload\n",
2229 (features & NETIF_F_HW_VLAN_CTAG_TX) ? "ON" : "OFF");
2231 if (DEV_FEATURE_CHANGED(netdev, features, NETIF_F_HW_VLAN_STAG_TX))
2232 en_info(priv, "Turn %s TX S-VLAN strip offload\n",
2233 (features & NETIF_F_HW_VLAN_STAG_TX) ? "ON" : "OFF");
2235 if (DEV_FEATURE_CHANGED(netdev, features, NETIF_F_LOOPBACK)) {
2236 en_info(priv, "Turn %s loopback\n",
2237 (features & NETIF_F_LOOPBACK) ? "ON" : "OFF");
2238 mlx4_en_update_loopback_state(netdev, features);
2242 ret = mlx4_en_reset_config(netdev, priv->hwtstamp_config,
2251 static int mlx4_en_set_vf_mac(struct net_device *dev, int queue, u8 *mac)
2253 struct mlx4_en_priv *en_priv = netdev_priv(dev);
2254 struct mlx4_en_dev *mdev = en_priv->mdev;
2255 u64 mac_u64 = mlx4_mac_to_u64(mac);
2257 if (!is_valid_ether_addr(mac))
2260 return mlx4_set_vf_mac(mdev->dev, en_priv->port, queue, mac_u64);
2263 static int mlx4_en_set_vf_vlan(struct net_device *dev, int vf, u16 vlan, u8 qos)
2265 struct mlx4_en_priv *en_priv = netdev_priv(dev);
2266 struct mlx4_en_dev *mdev = en_priv->mdev;
2268 return mlx4_set_vf_vlan(mdev->dev, en_priv->port, vf, vlan, qos);
2271 static int mlx4_en_set_vf_rate(struct net_device *dev, int vf, int min_tx_rate,
2274 struct mlx4_en_priv *en_priv = netdev_priv(dev);
2275 struct mlx4_en_dev *mdev = en_priv->mdev;
2277 return mlx4_set_vf_rate(mdev->dev, en_priv->port, vf, min_tx_rate,
2281 static int mlx4_en_set_vf_spoofchk(struct net_device *dev, int vf, bool setting)
2283 struct mlx4_en_priv *en_priv = netdev_priv(dev);
2284 struct mlx4_en_dev *mdev = en_priv->mdev;
2286 return mlx4_set_vf_spoofchk(mdev->dev, en_priv->port, vf, setting);
2289 static int mlx4_en_get_vf_config(struct net_device *dev, int vf, struct ifla_vf_info *ivf)
2291 struct mlx4_en_priv *en_priv = netdev_priv(dev);
2292 struct mlx4_en_dev *mdev = en_priv->mdev;
2294 return mlx4_get_vf_config(mdev->dev, en_priv->port, vf, ivf);
2297 static int mlx4_en_set_vf_link_state(struct net_device *dev, int vf, int link_state)
2299 struct mlx4_en_priv *en_priv = netdev_priv(dev);
2300 struct mlx4_en_dev *mdev = en_priv->mdev;
2302 return mlx4_set_vf_link_state(mdev->dev, en_priv->port, vf, link_state);
2305 static int mlx4_en_get_vf_stats(struct net_device *dev, int vf,
2306 struct ifla_vf_stats *vf_stats)
2308 struct mlx4_en_priv *en_priv = netdev_priv(dev);
2309 struct mlx4_en_dev *mdev = en_priv->mdev;
2311 return mlx4_get_vf_stats(mdev->dev, en_priv->port, vf, vf_stats);
2314 #define PORT_ID_BYTE_LEN 8
2315 static int mlx4_en_get_phys_port_id(struct net_device *dev,
2316 struct netdev_phys_item_id *ppid)
2318 struct mlx4_en_priv *priv = netdev_priv(dev);
2319 struct mlx4_dev *mdev = priv->mdev->dev;
2321 u64 phys_port_id = mdev->caps.phys_port_id[priv->port];
2326 ppid->id_len = sizeof(phys_port_id);
2327 for (i = PORT_ID_BYTE_LEN - 1; i >= 0; --i) {
2328 ppid->id[i] = phys_port_id & 0xff;
2334 #ifdef CONFIG_MLX4_EN_VXLAN
2335 static void mlx4_en_add_vxlan_offloads(struct work_struct *work)
2338 struct mlx4_en_priv *priv = container_of(work, struct mlx4_en_priv,
2341 ret = mlx4_config_vxlan_port(priv->mdev->dev, priv->vxlan_port);
2345 ret = mlx4_SET_PORT_VXLAN(priv->mdev->dev, priv->port,
2346 VXLAN_STEER_BY_OUTER_MAC, 1);
2349 en_err(priv, "failed setting L2 tunnel configuration ret %d\n", ret);
2354 priv->dev->hw_enc_features |= NETIF_F_IP_CSUM | NETIF_F_RXCSUM |
2355 NETIF_F_TSO | NETIF_F_GSO_UDP_TUNNEL;
2358 static void mlx4_en_del_vxlan_offloads(struct work_struct *work)
2361 struct mlx4_en_priv *priv = container_of(work, struct mlx4_en_priv,
2363 /* unset offloads */
2364 priv->dev->hw_enc_features &= ~(NETIF_F_IP_CSUM | NETIF_F_RXCSUM |
2365 NETIF_F_TSO | NETIF_F_GSO_UDP_TUNNEL);
2367 ret = mlx4_SET_PORT_VXLAN(priv->mdev->dev, priv->port,
2368 VXLAN_STEER_BY_OUTER_MAC, 0);
2370 en_err(priv, "failed setting L2 tunnel configuration ret %d\n", ret);
2372 priv->vxlan_port = 0;
2375 static void mlx4_en_add_vxlan_port(struct net_device *dev,
2376 sa_family_t sa_family, __be16 port)
2378 struct mlx4_en_priv *priv = netdev_priv(dev);
2379 __be16 current_port;
2381 if (priv->mdev->dev->caps.tunnel_offload_mode != MLX4_TUNNEL_OFFLOAD_MODE_VXLAN)
2384 if (sa_family == AF_INET6)
2387 current_port = priv->vxlan_port;
2388 if (current_port && current_port != port) {
2389 en_warn(priv, "vxlan port %d configured, can't add port %d\n",
2390 ntohs(current_port), ntohs(port));
2394 priv->vxlan_port = port;
2395 queue_work(priv->mdev->workqueue, &priv->vxlan_add_task);
2398 static void mlx4_en_del_vxlan_port(struct net_device *dev,
2399 sa_family_t sa_family, __be16 port)
2401 struct mlx4_en_priv *priv = netdev_priv(dev);
2402 __be16 current_port;
2404 if (priv->mdev->dev->caps.tunnel_offload_mode != MLX4_TUNNEL_OFFLOAD_MODE_VXLAN)
2407 if (sa_family == AF_INET6)
2410 current_port = priv->vxlan_port;
2411 if (current_port != port) {
2412 en_dbg(DRV, priv, "vxlan port %d isn't configured, ignoring\n", ntohs(port));
2416 queue_work(priv->mdev->workqueue, &priv->vxlan_del_task);
2419 static netdev_features_t mlx4_en_features_check(struct sk_buff *skb,
2420 struct net_device *dev,
2421 netdev_features_t features)
2423 features = vlan_features_check(skb, features);
2424 return vxlan_features_check(skb, features);
2428 static int mlx4_en_set_tx_maxrate(struct net_device *dev, int queue_index, u32 maxrate)
2430 struct mlx4_en_priv *priv = netdev_priv(dev);
2431 struct mlx4_en_tx_ring *tx_ring = priv->tx_ring[queue_index];
2432 struct mlx4_update_qp_params params;
2435 if (!(priv->mdev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_QP_RATE_LIMIT))
2438 /* rate provided to us in Mbs, check if it fits into 12 bits, if not use Gbs */
2439 if (maxrate >> 12) {
2440 params.rate_unit = MLX4_QP_RATE_LIMIT_GBS;
2441 params.rate_val = maxrate / 1000;
2442 } else if (maxrate) {
2443 params.rate_unit = MLX4_QP_RATE_LIMIT_MBS;
2444 params.rate_val = maxrate;
2445 } else { /* zero serves to revoke the QP rate-limitation */
2446 params.rate_unit = 0;
2447 params.rate_val = 0;
2450 err = mlx4_update_qp(priv->mdev->dev, tx_ring->qpn, MLX4_UPDATE_QP_RATE_LIMIT,
2455 static const struct net_device_ops mlx4_netdev_ops = {
2456 .ndo_open = mlx4_en_open,
2457 .ndo_stop = mlx4_en_close,
2458 .ndo_start_xmit = mlx4_en_xmit,
2459 .ndo_select_queue = mlx4_en_select_queue,
2460 .ndo_get_stats = mlx4_en_get_stats,
2461 .ndo_set_rx_mode = mlx4_en_set_rx_mode,
2462 .ndo_set_mac_address = mlx4_en_set_mac,
2463 .ndo_validate_addr = eth_validate_addr,
2464 .ndo_change_mtu = mlx4_en_change_mtu,
2465 .ndo_do_ioctl = mlx4_en_ioctl,
2466 .ndo_tx_timeout = mlx4_en_tx_timeout,
2467 .ndo_vlan_rx_add_vid = mlx4_en_vlan_rx_add_vid,
2468 .ndo_vlan_rx_kill_vid = mlx4_en_vlan_rx_kill_vid,
2469 #ifdef CONFIG_NET_POLL_CONTROLLER
2470 .ndo_poll_controller = mlx4_en_netpoll,
2472 .ndo_set_features = mlx4_en_set_features,
2473 .ndo_fix_features = mlx4_en_fix_features,
2474 .ndo_setup_tc = __mlx4_en_setup_tc,
2475 #ifdef CONFIG_RFS_ACCEL
2476 .ndo_rx_flow_steer = mlx4_en_filter_rfs,
2478 .ndo_get_phys_port_id = mlx4_en_get_phys_port_id,
2479 #ifdef CONFIG_MLX4_EN_VXLAN
2480 .ndo_add_vxlan_port = mlx4_en_add_vxlan_port,
2481 .ndo_del_vxlan_port = mlx4_en_del_vxlan_port,
2482 .ndo_features_check = mlx4_en_features_check,
2484 .ndo_set_tx_maxrate = mlx4_en_set_tx_maxrate,
2487 static const struct net_device_ops mlx4_netdev_ops_master = {
2488 .ndo_open = mlx4_en_open,
2489 .ndo_stop = mlx4_en_close,
2490 .ndo_start_xmit = mlx4_en_xmit,
2491 .ndo_select_queue = mlx4_en_select_queue,
2492 .ndo_get_stats = mlx4_en_get_stats,
2493 .ndo_set_rx_mode = mlx4_en_set_rx_mode,
2494 .ndo_set_mac_address = mlx4_en_set_mac,
2495 .ndo_validate_addr = eth_validate_addr,
2496 .ndo_change_mtu = mlx4_en_change_mtu,
2497 .ndo_tx_timeout = mlx4_en_tx_timeout,
2498 .ndo_vlan_rx_add_vid = mlx4_en_vlan_rx_add_vid,
2499 .ndo_vlan_rx_kill_vid = mlx4_en_vlan_rx_kill_vid,
2500 .ndo_set_vf_mac = mlx4_en_set_vf_mac,
2501 .ndo_set_vf_vlan = mlx4_en_set_vf_vlan,
2502 .ndo_set_vf_rate = mlx4_en_set_vf_rate,
2503 .ndo_set_vf_spoofchk = mlx4_en_set_vf_spoofchk,
2504 .ndo_set_vf_link_state = mlx4_en_set_vf_link_state,
2505 .ndo_get_vf_stats = mlx4_en_get_vf_stats,
2506 .ndo_get_vf_config = mlx4_en_get_vf_config,
2507 #ifdef CONFIG_NET_POLL_CONTROLLER
2508 .ndo_poll_controller = mlx4_en_netpoll,
2510 .ndo_set_features = mlx4_en_set_features,
2511 .ndo_fix_features = mlx4_en_fix_features,
2512 .ndo_setup_tc = __mlx4_en_setup_tc,
2513 #ifdef CONFIG_RFS_ACCEL
2514 .ndo_rx_flow_steer = mlx4_en_filter_rfs,
2516 .ndo_get_phys_port_id = mlx4_en_get_phys_port_id,
2517 #ifdef CONFIG_MLX4_EN_VXLAN
2518 .ndo_add_vxlan_port = mlx4_en_add_vxlan_port,
2519 .ndo_del_vxlan_port = mlx4_en_del_vxlan_port,
2520 .ndo_features_check = mlx4_en_features_check,
2522 .ndo_set_tx_maxrate = mlx4_en_set_tx_maxrate,
2525 struct mlx4_en_bond {
2526 struct work_struct work;
2527 struct mlx4_en_priv *priv;
2529 struct mlx4_port_map port_map;
2532 static void mlx4_en_bond_work(struct work_struct *work)
2534 struct mlx4_en_bond *bond = container_of(work,
2535 struct mlx4_en_bond,
2538 struct mlx4_dev *dev = bond->priv->mdev->dev;
2540 if (bond->is_bonded) {
2541 if (!mlx4_is_bonded(dev)) {
2542 err = mlx4_bond(dev);
2544 en_err(bond->priv, "Fail to bond device\n");
2547 err = mlx4_port_map_set(dev, &bond->port_map);
2549 en_err(bond->priv, "Fail to set port map [%d][%d]: %d\n",
2550 bond->port_map.port1,
2551 bond->port_map.port2,
2554 } else if (mlx4_is_bonded(dev)) {
2555 err = mlx4_unbond(dev);
2557 en_err(bond->priv, "Fail to unbond device\n");
2559 dev_put(bond->priv->dev);
2563 static int mlx4_en_queue_bond_work(struct mlx4_en_priv *priv, int is_bonded,
2564 u8 v2p_p1, u8 v2p_p2)
2566 struct mlx4_en_bond *bond = NULL;
2568 bond = kzalloc(sizeof(*bond), GFP_ATOMIC);
2572 INIT_WORK(&bond->work, mlx4_en_bond_work);
2574 bond->is_bonded = is_bonded;
2575 bond->port_map.port1 = v2p_p1;
2576 bond->port_map.port2 = v2p_p2;
2577 dev_hold(priv->dev);
2578 queue_work(priv->mdev->workqueue, &bond->work);
2582 int mlx4_en_netdev_event(struct notifier_block *this,
2583 unsigned long event, void *ptr)
2585 struct net_device *ndev = netdev_notifier_info_to_dev(ptr);
2587 struct mlx4_en_dev *mdev;
2588 struct mlx4_dev *dev;
2589 int i, num_eth_ports = 0;
2590 bool do_bond = true;
2591 struct mlx4_en_priv *priv;
2595 if (!net_eq(dev_net(ndev), &init_net))
2598 mdev = container_of(this, struct mlx4_en_dev, nb);
2601 /* Go into this mode only when two network devices set on two ports
2602 * of the same mlx4 device are slaves of the same bonding master
2604 mlx4_foreach_port(i, dev, MLX4_PORT_TYPE_ETH) {
2606 if (!port && (mdev->pndev[i] == ndev))
2608 mdev->upper[i] = mdev->pndev[i] ?
2609 netdev_master_upper_dev_get(mdev->pndev[i]) : NULL;
2610 /* condition not met: network device is a slave */
2611 if (!mdev->upper[i])
2613 if (num_eth_ports < 2)
2615 /* condition not met: same master */
2616 if (mdev->upper[i] != mdev->upper[i-1])
2619 /* condition not met: 2 salves */
2620 do_bond = (num_eth_ports == 2) ? do_bond : false;
2622 /* handle only events that come with enough info */
2623 if ((do_bond && (event != NETDEV_BONDING_INFO)) || !port)
2626 priv = netdev_priv(ndev);
2628 struct netdev_notifier_bonding_info *notifier_info = ptr;
2629 struct netdev_bonding_info *bonding_info =
2630 ¬ifier_info->bonding_info;
2632 /* required mode 1, 2 or 4 */
2633 if ((bonding_info->master.bond_mode != BOND_MODE_ACTIVEBACKUP) &&
2634 (bonding_info->master.bond_mode != BOND_MODE_XOR) &&
2635 (bonding_info->master.bond_mode != BOND_MODE_8023AD))
2638 /* require exactly 2 slaves */
2639 if (bonding_info->master.num_slaves != 2)
2644 if (bonding_info->master.bond_mode ==
2645 BOND_MODE_ACTIVEBACKUP) {
2646 /* in active-backup mode virtual ports are
2647 * mapped to the physical port of the active
2649 if (bonding_info->slave.state ==
2650 BOND_STATE_BACKUP) {
2658 } else { /* BOND_STATE_ACTIVE */
2667 } else { /* Active-Active */
2668 /* in active-active mode a virtual port is
2669 * mapped to the native physical port if and only
2670 * if the physical port is up */
2671 __s8 link = bonding_info->slave.link;
2677 if ((link == BOND_LINK_UP) ||
2678 (link == BOND_LINK_FAIL)) {
2683 } else { /* BOND_LINK_DOWN || BOND_LINK_BACK */
2693 mlx4_en_queue_bond_work(priv, do_bond,
2694 v2p_port1, v2p_port2);
2699 void mlx4_en_update_pfc_stats_bitmap(struct mlx4_dev *dev,
2700 struct mlx4_en_stats_bitmap *stats_bitmap,
2701 u8 rx_ppp, u8 rx_pause,
2702 u8 tx_ppp, u8 tx_pause)
2704 int last_i = NUM_MAIN_STATS + NUM_PORT_STATS + NUM_PF_STATS;
2706 if (!mlx4_is_slave(dev) &&
2707 (dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_FLOWSTATS_EN)) {
2708 mutex_lock(&stats_bitmap->mutex);
2709 bitmap_clear(stats_bitmap->bitmap, last_i, NUM_FLOW_STATS);
2712 bitmap_set(stats_bitmap->bitmap, last_i,
2713 NUM_FLOW_PRIORITY_STATS_RX);
2714 last_i += NUM_FLOW_PRIORITY_STATS_RX;
2716 if (rx_pause && !(rx_ppp))
2717 bitmap_set(stats_bitmap->bitmap, last_i,
2719 last_i += NUM_FLOW_STATS_RX;
2722 bitmap_set(stats_bitmap->bitmap, last_i,
2723 NUM_FLOW_PRIORITY_STATS_TX);
2724 last_i += NUM_FLOW_PRIORITY_STATS_TX;
2726 if (tx_pause && !(tx_ppp))
2727 bitmap_set(stats_bitmap->bitmap, last_i,
2729 last_i += NUM_FLOW_STATS_TX;
2731 mutex_unlock(&stats_bitmap->mutex);
2735 void mlx4_en_set_stats_bitmap(struct mlx4_dev *dev,
2736 struct mlx4_en_stats_bitmap *stats_bitmap,
2737 u8 rx_ppp, u8 rx_pause,
2738 u8 tx_ppp, u8 tx_pause)
2742 mutex_init(&stats_bitmap->mutex);
2743 bitmap_zero(stats_bitmap->bitmap, NUM_ALL_STATS);
2745 if (mlx4_is_slave(dev)) {
2746 bitmap_set(stats_bitmap->bitmap, last_i +
2747 MLX4_FIND_NETDEV_STAT(rx_packets), 1);
2748 bitmap_set(stats_bitmap->bitmap, last_i +
2749 MLX4_FIND_NETDEV_STAT(tx_packets), 1);
2750 bitmap_set(stats_bitmap->bitmap, last_i +
2751 MLX4_FIND_NETDEV_STAT(rx_bytes), 1);
2752 bitmap_set(stats_bitmap->bitmap, last_i +
2753 MLX4_FIND_NETDEV_STAT(tx_bytes), 1);
2754 bitmap_set(stats_bitmap->bitmap, last_i +
2755 MLX4_FIND_NETDEV_STAT(rx_dropped), 1);
2756 bitmap_set(stats_bitmap->bitmap, last_i +
2757 MLX4_FIND_NETDEV_STAT(tx_dropped), 1);
2759 bitmap_set(stats_bitmap->bitmap, last_i, NUM_MAIN_STATS);
2761 last_i += NUM_MAIN_STATS;
2763 bitmap_set(stats_bitmap->bitmap, last_i, NUM_PORT_STATS);
2764 last_i += NUM_PORT_STATS;
2766 if (mlx4_is_master(dev))
2767 bitmap_set(stats_bitmap->bitmap, last_i,
2769 last_i += NUM_PF_STATS;
2771 mlx4_en_update_pfc_stats_bitmap(dev, stats_bitmap,
2774 last_i += NUM_FLOW_STATS;
2776 if (!mlx4_is_slave(dev))
2777 bitmap_set(stats_bitmap->bitmap, last_i, NUM_PKT_STATS);
2780 int mlx4_en_init_netdev(struct mlx4_en_dev *mdev, int port,
2781 struct mlx4_en_port_profile *prof)
2783 struct net_device *dev;
2784 struct mlx4_en_priv *priv;
2788 dev = alloc_etherdev_mqs(sizeof(struct mlx4_en_priv),
2789 MAX_TX_RINGS, MAX_RX_RINGS);
2793 netif_set_real_num_tx_queues(dev, prof->tx_ring_num);
2794 netif_set_real_num_rx_queues(dev, prof->rx_ring_num);
2796 SET_NETDEV_DEV(dev, &mdev->dev->persist->pdev->dev);
2797 dev->dev_port = port - 1;
2800 * Initialize driver private data
2803 priv = netdev_priv(dev);
2804 memset(priv, 0, sizeof(struct mlx4_en_priv));
2805 priv->counter_index = MLX4_SINK_COUNTER_INDEX(mdev->dev);
2806 spin_lock_init(&priv->stats_lock);
2807 INIT_WORK(&priv->rx_mode_task, mlx4_en_do_set_rx_mode);
2808 INIT_WORK(&priv->watchdog_task, mlx4_en_restart);
2809 INIT_WORK(&priv->linkstate_task, mlx4_en_linkstate);
2810 INIT_DELAYED_WORK(&priv->stats_task, mlx4_en_do_get_stats);
2811 INIT_DELAYED_WORK(&priv->service_task, mlx4_en_service_task);
2812 #ifdef CONFIG_MLX4_EN_VXLAN
2813 INIT_WORK(&priv->vxlan_add_task, mlx4_en_add_vxlan_offloads);
2814 INIT_WORK(&priv->vxlan_del_task, mlx4_en_del_vxlan_offloads);
2816 #ifdef CONFIG_RFS_ACCEL
2817 INIT_LIST_HEAD(&priv->filters);
2818 spin_lock_init(&priv->filters_lock);
2823 priv->ddev = &mdev->pdev->dev;
2826 priv->port_up = false;
2827 priv->flags = prof->flags;
2828 priv->pflags = MLX4_EN_PRIV_FLAGS_BLUEFLAME;
2829 priv->ctrl_flags = cpu_to_be32(MLX4_WQE_CTRL_CQ_UPDATE |
2830 MLX4_WQE_CTRL_SOLICITED);
2831 priv->num_tx_rings_p_up = mdev->profile.num_tx_rings_p_up;
2832 priv->tx_ring_num = prof->tx_ring_num;
2833 priv->tx_work_limit = MLX4_EN_DEFAULT_TX_WORK;
2834 netdev_rss_key_fill(priv->rss_key, sizeof(priv->rss_key));
2836 priv->tx_ring = kzalloc(sizeof(struct mlx4_en_tx_ring *) * MAX_TX_RINGS,
2838 if (!priv->tx_ring) {
2842 priv->tx_cq = kzalloc(sizeof(struct mlx4_en_cq *) * MAX_TX_RINGS,
2848 priv->rx_ring_num = prof->rx_ring_num;
2849 priv->cqe_factor = (mdev->dev->caps.cqe_size == 64) ? 1 : 0;
2850 priv->cqe_size = mdev->dev->caps.cqe_size;
2851 priv->mac_index = -1;
2852 priv->msg_enable = MLX4_EN_MSG_LEVEL;
2853 #ifdef CONFIG_MLX4_EN_DCB
2854 if (!mlx4_is_slave(priv->mdev->dev)) {
2855 if (mdev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_ETS_CFG) {
2856 dev->dcbnl_ops = &mlx4_en_dcbnl_ops;
2858 en_info(priv, "enabling only PFC DCB ops\n");
2859 dev->dcbnl_ops = &mlx4_en_dcbnl_pfc_ops;
2864 for (i = 0; i < MLX4_EN_MAC_HASH_SIZE; ++i)
2865 INIT_HLIST_HEAD(&priv->mac_hash[i]);
2867 /* Query for default mac and max mtu */
2868 priv->max_mtu = mdev->dev->caps.eth_mtu_cap[priv->port];
2870 if (mdev->dev->caps.rx_checksum_flags_port[priv->port] &
2871 MLX4_RX_CSUM_MODE_VAL_NON_TCP_UDP)
2872 priv->flags |= MLX4_EN_FLAG_RX_CSUM_NON_TCP_UDP;
2874 /* Set default MAC */
2875 dev->addr_len = ETH_ALEN;
2876 mlx4_en_u64_to_mac(dev->dev_addr, mdev->dev->caps.def_mac[priv->port]);
2877 if (!is_valid_ether_addr(dev->dev_addr)) {
2878 en_err(priv, "Port: %d, invalid mac burned: %pM, quiting\n",
2879 priv->port, dev->dev_addr);
2882 } else if (mlx4_is_slave(priv->mdev->dev) &&
2883 (priv->mdev->dev->port_random_macs & 1 << priv->port)) {
2884 /* Random MAC was assigned in mlx4_slave_cap
2885 * in mlx4_core module
2887 dev->addr_assign_type |= NET_ADDR_RANDOM;
2888 en_warn(priv, "Assigned random MAC address %pM\n", dev->dev_addr);
2891 memcpy(priv->current_mac, dev->dev_addr, sizeof(priv->current_mac));
2893 priv->stride = roundup_pow_of_two(sizeof(struct mlx4_en_rx_desc) +
2894 DS_SIZE * MLX4_EN_MAX_RX_FRAGS);
2895 err = mlx4_en_alloc_resources(priv);
2899 /* Initialize time stamping config */
2900 priv->hwtstamp_config.flags = 0;
2901 priv->hwtstamp_config.tx_type = HWTSTAMP_TX_OFF;
2902 priv->hwtstamp_config.rx_filter = HWTSTAMP_FILTER_NONE;
2904 /* Allocate page for receive rings */
2905 err = mlx4_alloc_hwq_res(mdev->dev, &priv->res,
2906 MLX4_EN_PAGE_SIZE, MLX4_EN_PAGE_SIZE);
2908 en_err(priv, "Failed to allocate page for rx qps\n");
2911 priv->allocated = 1;
2914 * Initialize netdev entry points
2916 if (mlx4_is_master(priv->mdev->dev))
2917 dev->netdev_ops = &mlx4_netdev_ops_master;
2919 dev->netdev_ops = &mlx4_netdev_ops;
2920 dev->watchdog_timeo = MLX4_EN_WATCHDOG_TIMEOUT;
2921 netif_set_real_num_tx_queues(dev, priv->tx_ring_num);
2922 netif_set_real_num_rx_queues(dev, priv->rx_ring_num);
2924 dev->ethtool_ops = &mlx4_en_ethtool_ops;
2927 * Set driver features
2929 dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM;
2930 if (mdev->LSO_support)
2931 dev->hw_features |= NETIF_F_TSO | NETIF_F_TSO6;
2933 dev->vlan_features = dev->hw_features;
2935 dev->hw_features |= NETIF_F_RXCSUM | NETIF_F_RXHASH;
2936 dev->features = dev->hw_features | NETIF_F_HIGHDMA |
2937 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX |
2938 NETIF_F_HW_VLAN_CTAG_FILTER;
2939 dev->hw_features |= NETIF_F_LOOPBACK |
2940 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX;
2942 if (!(mdev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_SKIP_OUTER_VLAN)) {
2943 dev->features |= NETIF_F_HW_VLAN_STAG_RX |
2944 NETIF_F_HW_VLAN_STAG_FILTER;
2945 dev->hw_features |= NETIF_F_HW_VLAN_STAG_RX;
2948 if (mlx4_is_slave(mdev->dev)) {
2951 err = get_phv_bit(mdev->dev, port, &phv);
2953 dev->hw_features |= NETIF_F_HW_VLAN_STAG_TX;
2954 priv->pflags |= MLX4_EN_PRIV_FLAGS_PHV;
2957 if (mdev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_PHV_EN &&
2958 !(mdev->dev->caps.flags2 &
2959 MLX4_DEV_CAP_FLAG2_SKIP_OUTER_VLAN))
2960 dev->hw_features |= NETIF_F_HW_VLAN_STAG_TX;
2963 if (mdev->dev->caps.flags & MLX4_DEV_CAP_FLAG_FCS_KEEP)
2964 dev->hw_features |= NETIF_F_RXFCS;
2966 if (mdev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_IGNORE_FCS)
2967 dev->hw_features |= NETIF_F_RXALL;
2969 if (mdev->dev->caps.steering_mode ==
2970 MLX4_STEERING_MODE_DEVICE_MANAGED &&
2971 mdev->dev->caps.dmfs_high_steer_mode != MLX4_STEERING_DMFS_A0_STATIC)
2972 dev->hw_features |= NETIF_F_NTUPLE;
2974 if (mdev->dev->caps.steering_mode != MLX4_STEERING_MODE_A0)
2975 dev->priv_flags |= IFF_UNICAST_FLT;
2977 /* Setting a default hash function value */
2978 if (mdev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_RSS_TOP) {
2979 priv->rss_hash_fn = ETH_RSS_HASH_TOP;
2980 } else if (mdev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_RSS_XOR) {
2981 priv->rss_hash_fn = ETH_RSS_HASH_XOR;
2984 "No RSS hash capabilities exposed, using Toeplitz\n");
2985 priv->rss_hash_fn = ETH_RSS_HASH_TOP;
2988 if (mdev->dev->caps.tunnel_offload_mode == MLX4_TUNNEL_OFFLOAD_MODE_VXLAN) {
2989 dev->hw_features |= NETIF_F_GSO_UDP_TUNNEL;
2990 dev->features |= NETIF_F_GSO_UDP_TUNNEL;
2993 mdev->pndev[port] = dev;
2994 mdev->upper[port] = NULL;
2996 netif_carrier_off(dev);
2997 mlx4_en_set_default_moderation(priv);
2999 en_warn(priv, "Using %d TX rings\n", prof->tx_ring_num);
3000 en_warn(priv, "Using %d RX rings\n", prof->rx_ring_num);
3002 mlx4_en_update_loopback_state(priv->dev, priv->dev->features);
3004 /* Configure port */
3005 mlx4_en_calc_rx_buf(dev);
3006 err = mlx4_SET_PORT_general(mdev->dev, priv->port,
3007 priv->rx_skb_size + ETH_FCS_LEN,
3008 prof->tx_pause, prof->tx_ppp,
3009 prof->rx_pause, prof->rx_ppp);
3011 en_err(priv, "Failed setting port general configurations for port %d, with error %d\n",
3016 if (mdev->dev->caps.tunnel_offload_mode == MLX4_TUNNEL_OFFLOAD_MODE_VXLAN) {
3017 err = mlx4_SET_PORT_VXLAN(mdev->dev, priv->port, VXLAN_STEER_BY_OUTER_MAC, 1);
3019 en_err(priv, "Failed setting port L2 tunnel configuration, err %d\n",
3026 en_warn(priv, "Initializing port\n");
3027 err = mlx4_INIT_PORT(mdev->dev, priv->port);
3029 en_err(priv, "Failed Initializing port\n");
3032 queue_delayed_work(mdev->workqueue, &priv->stats_task, STATS_DELAY);
3034 /* Initialize time stamp mechanism */
3035 if (mdev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_TS)
3036 mlx4_en_init_timestamp(mdev);
3038 queue_delayed_work(mdev->workqueue, &priv->service_task,
3039 SERVICE_TASK_DELAY);
3041 mlx4_en_set_stats_bitmap(mdev->dev, &priv->stats_bitmap,
3042 mdev->profile.prof[priv->port].rx_ppp,
3043 mdev->profile.prof[priv->port].rx_pause,
3044 mdev->profile.prof[priv->port].tx_ppp,
3045 mdev->profile.prof[priv->port].tx_pause);
3047 err = register_netdev(dev);
3049 en_err(priv, "Netdev registration failed for port %d\n", port);
3053 priv->registered = 1;
3058 mlx4_en_destroy_netdev(dev);
3062 int mlx4_en_reset_config(struct net_device *dev,
3063 struct hwtstamp_config ts_config,
3064 netdev_features_t features)
3066 struct mlx4_en_priv *priv = netdev_priv(dev);
3067 struct mlx4_en_dev *mdev = priv->mdev;
3071 if (priv->hwtstamp_config.tx_type == ts_config.tx_type &&
3072 priv->hwtstamp_config.rx_filter == ts_config.rx_filter &&
3073 !DEV_FEATURE_CHANGED(dev, features, NETIF_F_HW_VLAN_CTAG_RX) &&
3074 !DEV_FEATURE_CHANGED(dev, features, NETIF_F_RXFCS))
3075 return 0; /* Nothing to change */
3077 if (DEV_FEATURE_CHANGED(dev, features, NETIF_F_HW_VLAN_CTAG_RX) &&
3078 (features & NETIF_F_HW_VLAN_CTAG_RX) &&
3079 (priv->hwtstamp_config.rx_filter != HWTSTAMP_FILTER_NONE)) {
3080 en_warn(priv, "Can't turn ON rx vlan offload while time-stamping rx filter is ON\n");
3084 mutex_lock(&mdev->state_lock);
3085 if (priv->port_up) {
3087 mlx4_en_stop_port(dev, 1);
3090 mlx4_en_free_resources(priv);
3092 en_warn(priv, "Changing device configuration rx filter(%x) rx vlan(%x)\n",
3093 ts_config.rx_filter, !!(features & NETIF_F_HW_VLAN_CTAG_RX));
3095 priv->hwtstamp_config.tx_type = ts_config.tx_type;
3096 priv->hwtstamp_config.rx_filter = ts_config.rx_filter;
3098 if (DEV_FEATURE_CHANGED(dev, features, NETIF_F_HW_VLAN_CTAG_RX)) {
3099 if (features & NETIF_F_HW_VLAN_CTAG_RX)
3100 dev->features |= NETIF_F_HW_VLAN_CTAG_RX;
3102 dev->features &= ~NETIF_F_HW_VLAN_CTAG_RX;
3103 } else if (ts_config.rx_filter == HWTSTAMP_FILTER_NONE) {
3104 /* RX time-stamping is OFF, update the RX vlan offload
3105 * to the latest wanted state
3107 if (dev->wanted_features & NETIF_F_HW_VLAN_CTAG_RX)
3108 dev->features |= NETIF_F_HW_VLAN_CTAG_RX;
3110 dev->features &= ~NETIF_F_HW_VLAN_CTAG_RX;
3113 if (DEV_FEATURE_CHANGED(dev, features, NETIF_F_RXFCS)) {
3114 if (features & NETIF_F_RXFCS)
3115 dev->features |= NETIF_F_RXFCS;
3117 dev->features &= ~NETIF_F_RXFCS;
3120 /* RX vlan offload and RX time-stamping can't co-exist !
3121 * Regardless of the caller's choice,
3122 * Turn Off RX vlan offload in case of time-stamping is ON
3124 if (ts_config.rx_filter != HWTSTAMP_FILTER_NONE) {
3125 if (dev->features & NETIF_F_HW_VLAN_CTAG_RX)
3126 en_warn(priv, "Turning off RX vlan offload since RX time-stamping is ON\n");
3127 dev->features &= ~NETIF_F_HW_VLAN_CTAG_RX;
3130 err = mlx4_en_alloc_resources(priv);
3132 en_err(priv, "Failed reallocating port resources\n");
3136 err = mlx4_en_start_port(dev);
3138 en_err(priv, "Failed starting port\n");
3142 mutex_unlock(&mdev->state_lock);
3143 netdev_features_change(dev);