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>
43 #include <linux/mlx4/driver.h>
44 #include <linux/mlx4/device.h>
45 #include <linux/mlx4/cmd.h>
46 #include <linux/mlx4/cq.h>
51 int mlx4_en_setup_tc(struct net_device *dev, u8 up)
53 struct mlx4_en_priv *priv = netdev_priv(dev);
55 unsigned int offset = 0;
57 if (up && up != MLX4_EN_NUM_UP)
60 netdev_set_num_tc(dev, up);
62 /* Partition Tx queues evenly amongst UP's */
63 for (i = 0; i < up; i++) {
64 netdev_set_tc_queue(dev, i, priv->num_tx_rings_p_up, offset);
65 offset += priv->num_tx_rings_p_up;
71 #ifdef CONFIG_NET_RX_BUSY_POLL
72 /* must be called with local_bh_disable()d */
73 static int mlx4_en_low_latency_recv(struct napi_struct *napi)
75 struct mlx4_en_cq *cq = container_of(napi, struct mlx4_en_cq, napi);
76 struct net_device *dev = cq->dev;
77 struct mlx4_en_priv *priv = netdev_priv(dev);
78 struct mlx4_en_rx_ring *rx_ring = priv->rx_ring[cq->ring];
82 return LL_FLUSH_FAILED;
84 if (!mlx4_en_cq_lock_poll(cq))
87 done = mlx4_en_process_rx_cq(dev, cq, 4);
89 rx_ring->cleaned += done;
93 mlx4_en_cq_unlock_poll(cq);
97 #endif /* CONFIG_NET_RX_BUSY_POLL */
99 #ifdef CONFIG_RFS_ACCEL
101 struct mlx4_en_filter {
102 struct list_head next;
103 struct work_struct work;
112 struct mlx4_en_priv *priv;
113 u32 flow_id; /* RFS infrastructure id */
114 int id; /* mlx4_en driver id */
115 u64 reg_id; /* Flow steering API id */
116 u8 activated; /* Used to prevent expiry before filter
119 struct hlist_node filter_chain;
122 static void mlx4_en_filter_rfs_expire(struct mlx4_en_priv *priv);
124 static enum mlx4_net_trans_rule_id mlx4_ip_proto_to_trans_rule_id(u8 ip_proto)
128 return MLX4_NET_TRANS_RULE_ID_UDP;
130 return MLX4_NET_TRANS_RULE_ID_TCP;
132 return -EPROTONOSUPPORT;
136 static void mlx4_en_filter_work(struct work_struct *work)
138 struct mlx4_en_filter *filter = container_of(work,
139 struct mlx4_en_filter,
141 struct mlx4_en_priv *priv = filter->priv;
142 struct mlx4_spec_list spec_tcp_udp = {
143 .id = mlx4_ip_proto_to_trans_rule_id(filter->ip_proto),
146 .dst_port = filter->dst_port,
147 .dst_port_msk = (__force __be16)-1,
148 .src_port = filter->src_port,
149 .src_port_msk = (__force __be16)-1,
153 struct mlx4_spec_list spec_ip = {
154 .id = MLX4_NET_TRANS_RULE_ID_IPV4,
157 .dst_ip = filter->dst_ip,
158 .dst_ip_msk = (__force __be32)-1,
159 .src_ip = filter->src_ip,
160 .src_ip_msk = (__force __be32)-1,
164 struct mlx4_spec_list spec_eth = {
165 .id = MLX4_NET_TRANS_RULE_ID_ETH,
167 struct mlx4_net_trans_rule rule = {
168 .list = LIST_HEAD_INIT(rule.list),
169 .queue_mode = MLX4_NET_TRANS_Q_LIFO,
172 .promisc_mode = MLX4_FS_REGULAR,
174 .priority = MLX4_DOMAIN_RFS,
177 __be64 mac_mask = cpu_to_be64(MLX4_MAC_MASK << 16);
179 if (spec_tcp_udp.id < 0) {
180 en_warn(priv, "RFS: ignoring unsupported ip protocol (%d)\n",
184 list_add_tail(&spec_eth.list, &rule.list);
185 list_add_tail(&spec_ip.list, &rule.list);
186 list_add_tail(&spec_tcp_udp.list, &rule.list);
188 rule.qpn = priv->rss_map.qps[filter->rxq_index].qpn;
189 memcpy(spec_eth.eth.dst_mac, priv->dev->dev_addr, ETH_ALEN);
190 memcpy(spec_eth.eth.dst_mac_msk, &mac_mask, ETH_ALEN);
192 filter->activated = 0;
194 if (filter->reg_id) {
195 rc = mlx4_flow_detach(priv->mdev->dev, filter->reg_id);
196 if (rc && rc != -ENOENT)
197 en_err(priv, "Error detaching flow. rc = %d\n", rc);
200 rc = mlx4_flow_attach(priv->mdev->dev, &rule, &filter->reg_id);
202 en_err(priv, "Error attaching flow. err = %d\n", rc);
205 mlx4_en_filter_rfs_expire(priv);
207 filter->activated = 1;
210 static inline struct hlist_head *
211 filter_hash_bucket(struct mlx4_en_priv *priv, __be32 src_ip, __be32 dst_ip,
212 __be16 src_port, __be16 dst_port)
217 l = (__force unsigned long)src_port |
218 ((__force unsigned long)dst_port << 2);
219 l ^= (__force unsigned long)(src_ip ^ dst_ip);
221 bucket_idx = hash_long(l, MLX4_EN_FILTER_HASH_SHIFT);
223 return &priv->filter_hash[bucket_idx];
226 static struct mlx4_en_filter *
227 mlx4_en_filter_alloc(struct mlx4_en_priv *priv, int rxq_index, __be32 src_ip,
228 __be32 dst_ip, u8 ip_proto, __be16 src_port,
229 __be16 dst_port, u32 flow_id)
231 struct mlx4_en_filter *filter = NULL;
233 filter = kzalloc(sizeof(struct mlx4_en_filter), GFP_ATOMIC);
238 filter->rxq_index = rxq_index;
239 INIT_WORK(&filter->work, mlx4_en_filter_work);
241 filter->src_ip = src_ip;
242 filter->dst_ip = dst_ip;
243 filter->ip_proto = ip_proto;
244 filter->src_port = src_port;
245 filter->dst_port = dst_port;
247 filter->flow_id = flow_id;
249 filter->id = priv->last_filter_id++ % RPS_NO_FILTER;
251 list_add_tail(&filter->next, &priv->filters);
252 hlist_add_head(&filter->filter_chain,
253 filter_hash_bucket(priv, src_ip, dst_ip, src_port,
259 static void mlx4_en_filter_free(struct mlx4_en_filter *filter)
261 struct mlx4_en_priv *priv = filter->priv;
264 list_del(&filter->next);
266 rc = mlx4_flow_detach(priv->mdev->dev, filter->reg_id);
267 if (rc && rc != -ENOENT)
268 en_err(priv, "Error detaching flow. rc = %d\n", rc);
273 static inline struct mlx4_en_filter *
274 mlx4_en_filter_find(struct mlx4_en_priv *priv, __be32 src_ip, __be32 dst_ip,
275 u8 ip_proto, __be16 src_port, __be16 dst_port)
277 struct mlx4_en_filter *filter;
278 struct mlx4_en_filter *ret = NULL;
280 hlist_for_each_entry(filter,
281 filter_hash_bucket(priv, src_ip, dst_ip,
284 if (filter->src_ip == src_ip &&
285 filter->dst_ip == dst_ip &&
286 filter->ip_proto == ip_proto &&
287 filter->src_port == src_port &&
288 filter->dst_port == dst_port) {
298 mlx4_en_filter_rfs(struct net_device *net_dev, const struct sk_buff *skb,
299 u16 rxq_index, u32 flow_id)
301 struct mlx4_en_priv *priv = netdev_priv(net_dev);
302 struct mlx4_en_filter *filter;
303 const struct iphdr *ip;
310 int nhoff = skb_network_offset(skb);
313 if (skb->protocol != htons(ETH_P_IP))
314 return -EPROTONOSUPPORT;
316 ip = (const struct iphdr *)(skb->data + nhoff);
317 if (ip_is_fragment(ip))
318 return -EPROTONOSUPPORT;
320 if ((ip->protocol != IPPROTO_TCP) && (ip->protocol != IPPROTO_UDP))
321 return -EPROTONOSUPPORT;
322 ports = (const __be16 *)(skb->data + nhoff + 4 * ip->ihl);
324 ip_proto = ip->protocol;
330 spin_lock_bh(&priv->filters_lock);
331 filter = mlx4_en_filter_find(priv, src_ip, dst_ip, ip_proto,
334 if (filter->rxq_index == rxq_index)
337 filter->rxq_index = rxq_index;
339 filter = mlx4_en_filter_alloc(priv, rxq_index,
340 src_ip, dst_ip, ip_proto,
341 src_port, dst_port, flow_id);
348 queue_work(priv->mdev->workqueue, &filter->work);
353 spin_unlock_bh(&priv->filters_lock);
358 void mlx4_en_cleanup_filters(struct mlx4_en_priv *priv)
360 struct mlx4_en_filter *filter, *tmp;
363 spin_lock_bh(&priv->filters_lock);
364 list_for_each_entry_safe(filter, tmp, &priv->filters, next) {
365 list_move(&filter->next, &del_list);
366 hlist_del(&filter->filter_chain);
368 spin_unlock_bh(&priv->filters_lock);
370 list_for_each_entry_safe(filter, tmp, &del_list, next) {
371 cancel_work_sync(&filter->work);
372 mlx4_en_filter_free(filter);
376 static void mlx4_en_filter_rfs_expire(struct mlx4_en_priv *priv)
378 struct mlx4_en_filter *filter = NULL, *tmp, *last_filter = NULL;
382 spin_lock_bh(&priv->filters_lock);
383 list_for_each_entry_safe(filter, tmp, &priv->filters, next) {
384 if (i > MLX4_EN_FILTER_EXPIRY_QUOTA)
387 if (filter->activated &&
388 !work_pending(&filter->work) &&
389 rps_may_expire_flow(priv->dev,
390 filter->rxq_index, filter->flow_id,
392 list_move(&filter->next, &del_list);
393 hlist_del(&filter->filter_chain);
395 last_filter = filter;
400 if (last_filter && (&last_filter->next != priv->filters.next))
401 list_move(&priv->filters, &last_filter->next);
403 spin_unlock_bh(&priv->filters_lock);
405 list_for_each_entry_safe(filter, tmp, &del_list, next)
406 mlx4_en_filter_free(filter);
410 static int mlx4_en_vlan_rx_add_vid(struct net_device *dev,
411 __be16 proto, u16 vid)
413 struct mlx4_en_priv *priv = netdev_priv(dev);
414 struct mlx4_en_dev *mdev = priv->mdev;
418 en_dbg(HW, priv, "adding VLAN:%d\n", vid);
420 set_bit(vid, priv->active_vlans);
422 /* Add VID to port VLAN filter */
423 mutex_lock(&mdev->state_lock);
424 if (mdev->device_up && priv->port_up) {
425 err = mlx4_SET_VLAN_FLTR(mdev->dev, priv);
427 en_err(priv, "Failed configuring VLAN filter\n");
429 if (mlx4_register_vlan(mdev->dev, priv->port, vid, &idx))
430 en_dbg(HW, priv, "failed adding vlan %d\n", vid);
431 mutex_unlock(&mdev->state_lock);
436 static int mlx4_en_vlan_rx_kill_vid(struct net_device *dev,
437 __be16 proto, u16 vid)
439 struct mlx4_en_priv *priv = netdev_priv(dev);
440 struct mlx4_en_dev *mdev = priv->mdev;
443 en_dbg(HW, priv, "Killing VID:%d\n", vid);
445 clear_bit(vid, priv->active_vlans);
447 /* Remove VID from port VLAN filter */
448 mutex_lock(&mdev->state_lock);
449 mlx4_unregister_vlan(mdev->dev, priv->port, vid);
451 if (mdev->device_up && priv->port_up) {
452 err = mlx4_SET_VLAN_FLTR(mdev->dev, priv);
454 en_err(priv, "Failed configuring VLAN filter\n");
456 mutex_unlock(&mdev->state_lock);
461 static void mlx4_en_u64_to_mac(unsigned char dst_mac[ETH_ALEN + 2], u64 src_mac)
464 for (i = ETH_ALEN - 1; i >= 0; --i) {
465 dst_mac[i] = src_mac & 0xff;
468 memset(&dst_mac[ETH_ALEN], 0, 2);
472 static int mlx4_en_tunnel_steer_add(struct mlx4_en_priv *priv, unsigned char *addr,
473 int qpn, u64 *reg_id)
476 struct mlx4_spec_list spec_eth_outer = { {NULL} };
477 struct mlx4_spec_list spec_vxlan = { {NULL} };
478 struct mlx4_spec_list spec_eth_inner = { {NULL} };
480 struct mlx4_net_trans_rule rule = {
481 .queue_mode = MLX4_NET_TRANS_Q_FIFO,
484 .promisc_mode = MLX4_FS_REGULAR,
485 .priority = MLX4_DOMAIN_NIC,
488 __be64 mac_mask = cpu_to_be64(MLX4_MAC_MASK << 16);
490 if (priv->mdev->dev->caps.tunnel_offload_mode != MLX4_TUNNEL_OFFLOAD_MODE_VXLAN)
491 return 0; /* do nothing */
493 rule.port = priv->port;
495 INIT_LIST_HEAD(&rule.list);
497 spec_eth_outer.id = MLX4_NET_TRANS_RULE_ID_ETH;
498 memcpy(spec_eth_outer.eth.dst_mac, addr, ETH_ALEN);
499 memcpy(spec_eth_outer.eth.dst_mac_msk, &mac_mask, ETH_ALEN);
501 spec_vxlan.id = MLX4_NET_TRANS_RULE_ID_VXLAN; /* any vxlan header */
502 spec_eth_inner.id = MLX4_NET_TRANS_RULE_ID_ETH; /* any inner eth header */
504 list_add_tail(&spec_eth_outer.list, &rule.list);
505 list_add_tail(&spec_vxlan.list, &rule.list);
506 list_add_tail(&spec_eth_inner.list, &rule.list);
508 err = mlx4_flow_attach(priv->mdev->dev, &rule, reg_id);
510 en_err(priv, "failed to add vxlan steering rule, err %d\n", err);
513 en_dbg(DRV, priv, "added vxlan steering rule, mac %pM reg_id %llx\n", addr, *reg_id);
518 static int mlx4_en_uc_steer_add(struct mlx4_en_priv *priv,
519 unsigned char *mac, int *qpn, u64 *reg_id)
521 struct mlx4_en_dev *mdev = priv->mdev;
522 struct mlx4_dev *dev = mdev->dev;
525 switch (dev->caps.steering_mode) {
526 case MLX4_STEERING_MODE_B0: {
531 memcpy(&gid[10], mac, ETH_ALEN);
534 err = mlx4_unicast_attach(dev, &qp, gid, 0, MLX4_PROT_ETH);
537 case MLX4_STEERING_MODE_DEVICE_MANAGED: {
538 struct mlx4_spec_list spec_eth = { {NULL} };
539 __be64 mac_mask = cpu_to_be64(MLX4_MAC_MASK << 16);
541 struct mlx4_net_trans_rule rule = {
542 .queue_mode = MLX4_NET_TRANS_Q_FIFO,
545 .promisc_mode = MLX4_FS_REGULAR,
546 .priority = MLX4_DOMAIN_NIC,
549 rule.port = priv->port;
551 INIT_LIST_HEAD(&rule.list);
553 spec_eth.id = MLX4_NET_TRANS_RULE_ID_ETH;
554 memcpy(spec_eth.eth.dst_mac, mac, ETH_ALEN);
555 memcpy(spec_eth.eth.dst_mac_msk, &mac_mask, ETH_ALEN);
556 list_add_tail(&spec_eth.list, &rule.list);
558 err = mlx4_flow_attach(dev, &rule, reg_id);
565 en_warn(priv, "Failed Attaching Unicast\n");
570 static void mlx4_en_uc_steer_release(struct mlx4_en_priv *priv,
571 unsigned char *mac, int qpn, u64 reg_id)
573 struct mlx4_en_dev *mdev = priv->mdev;
574 struct mlx4_dev *dev = mdev->dev;
576 switch (dev->caps.steering_mode) {
577 case MLX4_STEERING_MODE_B0: {
582 memcpy(&gid[10], mac, ETH_ALEN);
585 mlx4_unicast_detach(dev, &qp, gid, MLX4_PROT_ETH);
588 case MLX4_STEERING_MODE_DEVICE_MANAGED: {
589 mlx4_flow_detach(dev, reg_id);
593 en_err(priv, "Invalid steering mode.\n");
597 static int mlx4_en_get_qp(struct mlx4_en_priv *priv)
599 struct mlx4_en_dev *mdev = priv->mdev;
600 struct mlx4_dev *dev = mdev->dev;
601 struct mlx4_mac_entry *entry;
605 int *qpn = &priv->base_qpn;
606 u64 mac = mlx4_en_mac_to_u64(priv->dev->dev_addr);
608 en_dbg(DRV, priv, "Registering MAC: %pM for adding\n",
609 priv->dev->dev_addr);
610 index = mlx4_register_mac(dev, priv->port, mac);
613 en_err(priv, "Failed adding MAC: %pM\n",
614 priv->dev->dev_addr);
618 if (dev->caps.steering_mode == MLX4_STEERING_MODE_A0) {
619 int base_qpn = mlx4_get_base_qpn(dev, priv->port);
620 *qpn = base_qpn + index;
624 err = mlx4_qp_reserve_range(dev, 1, 1, qpn);
625 en_dbg(DRV, priv, "Reserved qp %d\n", *qpn);
627 en_err(priv, "Failed to reserve qp for mac registration\n");
631 err = mlx4_en_uc_steer_add(priv, priv->dev->dev_addr, qpn, ®_id);
635 err = mlx4_en_tunnel_steer_add(priv, priv->dev->dev_addr, *qpn,
636 &priv->tunnel_reg_id);
640 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
645 memcpy(entry->mac, priv->dev->dev_addr, sizeof(entry->mac));
646 entry->reg_id = reg_id;
648 hlist_add_head_rcu(&entry->hlist,
649 &priv->mac_hash[entry->mac[MLX4_EN_MAC_HASH_IDX]]);
654 if (priv->tunnel_reg_id)
655 mlx4_flow_detach(priv->mdev->dev, priv->tunnel_reg_id);
657 mlx4_en_uc_steer_release(priv, priv->dev->dev_addr, *qpn, reg_id);
660 mlx4_qp_release_range(dev, *qpn, 1);
663 mlx4_unregister_mac(dev, priv->port, mac);
667 static void mlx4_en_put_qp(struct mlx4_en_priv *priv)
669 struct mlx4_en_dev *mdev = priv->mdev;
670 struct mlx4_dev *dev = mdev->dev;
671 int qpn = priv->base_qpn;
674 if (dev->caps.steering_mode == MLX4_STEERING_MODE_A0) {
675 mac = mlx4_en_mac_to_u64(priv->dev->dev_addr);
676 en_dbg(DRV, priv, "Registering MAC: %pM for deleting\n",
677 priv->dev->dev_addr);
678 mlx4_unregister_mac(dev, priv->port, mac);
680 struct mlx4_mac_entry *entry;
681 struct hlist_node *tmp;
682 struct hlist_head *bucket;
685 for (i = 0; i < MLX4_EN_MAC_HASH_SIZE; ++i) {
686 bucket = &priv->mac_hash[i];
687 hlist_for_each_entry_safe(entry, tmp, bucket, hlist) {
688 mac = mlx4_en_mac_to_u64(entry->mac);
689 en_dbg(DRV, priv, "Registering MAC: %pM for deleting\n",
691 mlx4_en_uc_steer_release(priv, entry->mac,
694 mlx4_unregister_mac(dev, priv->port, mac);
695 hlist_del_rcu(&entry->hlist);
696 kfree_rcu(entry, rcu);
700 if (priv->tunnel_reg_id) {
701 mlx4_flow_detach(priv->mdev->dev, priv->tunnel_reg_id);
702 priv->tunnel_reg_id = 0;
705 en_dbg(DRV, priv, "Releasing qp: port %d, qpn %d\n",
707 mlx4_qp_release_range(dev, qpn, 1);
708 priv->flags &= ~MLX4_EN_FLAG_FORCE_PROMISC;
712 static int mlx4_en_replace_mac(struct mlx4_en_priv *priv, int qpn,
713 unsigned char *new_mac, unsigned char *prev_mac)
715 struct mlx4_en_dev *mdev = priv->mdev;
716 struct mlx4_dev *dev = mdev->dev;
718 u64 new_mac_u64 = mlx4_en_mac_to_u64(new_mac);
720 if (dev->caps.steering_mode != MLX4_STEERING_MODE_A0) {
721 struct hlist_head *bucket;
722 unsigned int mac_hash;
723 struct mlx4_mac_entry *entry;
724 struct hlist_node *tmp;
725 u64 prev_mac_u64 = mlx4_en_mac_to_u64(prev_mac);
727 bucket = &priv->mac_hash[prev_mac[MLX4_EN_MAC_HASH_IDX]];
728 hlist_for_each_entry_safe(entry, tmp, bucket, hlist) {
729 if (ether_addr_equal_64bits(entry->mac, prev_mac)) {
730 mlx4_en_uc_steer_release(priv, entry->mac,
732 mlx4_unregister_mac(dev, priv->port,
734 hlist_del_rcu(&entry->hlist);
736 memcpy(entry->mac, new_mac, ETH_ALEN);
738 mac_hash = new_mac[MLX4_EN_MAC_HASH_IDX];
739 hlist_add_head_rcu(&entry->hlist,
740 &priv->mac_hash[mac_hash]);
741 mlx4_register_mac(dev, priv->port, new_mac_u64);
742 err = mlx4_en_uc_steer_add(priv, new_mac,
747 if (priv->tunnel_reg_id) {
748 mlx4_flow_detach(priv->mdev->dev, priv->tunnel_reg_id);
749 priv->tunnel_reg_id = 0;
751 err = mlx4_en_tunnel_steer_add(priv, new_mac, qpn,
752 &priv->tunnel_reg_id);
759 return __mlx4_replace_mac(dev, priv->port, qpn, new_mac_u64);
762 u64 mlx4_en_mac_to_u64(u8 *addr)
767 for (i = 0; i < ETH_ALEN; i++) {
774 static int mlx4_en_do_set_mac(struct mlx4_en_priv *priv)
779 /* Remove old MAC and insert the new one */
780 err = mlx4_en_replace_mac(priv, priv->base_qpn,
781 priv->dev->dev_addr, priv->prev_mac);
783 en_err(priv, "Failed changing HW MAC address\n");
784 memcpy(priv->prev_mac, priv->dev->dev_addr,
785 sizeof(priv->prev_mac));
787 en_dbg(HW, priv, "Port is down while registering mac, exiting...\n");
792 static int mlx4_en_set_mac(struct net_device *dev, void *addr)
794 struct mlx4_en_priv *priv = netdev_priv(dev);
795 struct mlx4_en_dev *mdev = priv->mdev;
796 struct sockaddr *saddr = addr;
799 if (!is_valid_ether_addr(saddr->sa_data))
800 return -EADDRNOTAVAIL;
802 memcpy(dev->dev_addr, saddr->sa_data, ETH_ALEN);
804 mutex_lock(&mdev->state_lock);
805 err = mlx4_en_do_set_mac(priv);
806 mutex_unlock(&mdev->state_lock);
811 static void mlx4_en_clear_list(struct net_device *dev)
813 struct mlx4_en_priv *priv = netdev_priv(dev);
814 struct mlx4_en_mc_list *tmp, *mc_to_del;
816 list_for_each_entry_safe(mc_to_del, tmp, &priv->mc_list, list) {
817 list_del(&mc_to_del->list);
822 static void mlx4_en_cache_mclist(struct net_device *dev)
824 struct mlx4_en_priv *priv = netdev_priv(dev);
825 struct netdev_hw_addr *ha;
826 struct mlx4_en_mc_list *tmp;
828 mlx4_en_clear_list(dev);
829 netdev_for_each_mc_addr(ha, dev) {
830 tmp = kzalloc(sizeof(struct mlx4_en_mc_list), GFP_ATOMIC);
832 mlx4_en_clear_list(dev);
835 memcpy(tmp->addr, ha->addr, ETH_ALEN);
836 list_add_tail(&tmp->list, &priv->mc_list);
840 static void update_mclist_flags(struct mlx4_en_priv *priv,
841 struct list_head *dst,
842 struct list_head *src)
844 struct mlx4_en_mc_list *dst_tmp, *src_tmp, *new_mc;
847 /* Find all the entries that should be removed from dst,
848 * These are the entries that are not found in src
850 list_for_each_entry(dst_tmp, dst, list) {
852 list_for_each_entry(src_tmp, src, list) {
853 if (ether_addr_equal(dst_tmp->addr, src_tmp->addr)) {
859 dst_tmp->action = MCLIST_REM;
862 /* Add entries that exist in src but not in dst
863 * mark them as need to add
865 list_for_each_entry(src_tmp, src, list) {
867 list_for_each_entry(dst_tmp, dst, list) {
868 if (ether_addr_equal(dst_tmp->addr, src_tmp->addr)) {
869 dst_tmp->action = MCLIST_NONE;
875 new_mc = kmemdup(src_tmp,
876 sizeof(struct mlx4_en_mc_list),
881 new_mc->action = MCLIST_ADD;
882 list_add_tail(&new_mc->list, dst);
887 static void mlx4_en_set_rx_mode(struct net_device *dev)
889 struct mlx4_en_priv *priv = netdev_priv(dev);
894 queue_work(priv->mdev->workqueue, &priv->rx_mode_task);
897 static void mlx4_en_set_promisc_mode(struct mlx4_en_priv *priv,
898 struct mlx4_en_dev *mdev)
902 if (!(priv->flags & MLX4_EN_FLAG_PROMISC)) {
903 if (netif_msg_rx_status(priv))
904 en_warn(priv, "Entering promiscuous mode\n");
905 priv->flags |= MLX4_EN_FLAG_PROMISC;
907 /* Enable promiscouos mode */
908 switch (mdev->dev->caps.steering_mode) {
909 case MLX4_STEERING_MODE_DEVICE_MANAGED:
910 err = mlx4_flow_steer_promisc_add(mdev->dev,
913 MLX4_FS_ALL_DEFAULT);
915 en_err(priv, "Failed enabling promiscuous mode\n");
916 priv->flags |= MLX4_EN_FLAG_MC_PROMISC;
919 case MLX4_STEERING_MODE_B0:
920 err = mlx4_unicast_promisc_add(mdev->dev,
924 en_err(priv, "Failed enabling unicast promiscuous mode\n");
926 /* Add the default qp number as multicast
929 if (!(priv->flags & MLX4_EN_FLAG_MC_PROMISC)) {
930 err = mlx4_multicast_promisc_add(mdev->dev,
934 en_err(priv, "Failed enabling multicast promiscuous mode\n");
935 priv->flags |= MLX4_EN_FLAG_MC_PROMISC;
939 case MLX4_STEERING_MODE_A0:
940 err = mlx4_SET_PORT_qpn_calc(mdev->dev,
945 en_err(priv, "Failed enabling promiscuous mode\n");
949 /* Disable port multicast filter (unconditionally) */
950 err = mlx4_SET_MCAST_FLTR(mdev->dev, priv->port, 0,
951 0, MLX4_MCAST_DISABLE);
953 en_err(priv, "Failed disabling multicast filter\n");
955 /* Disable port VLAN filter */
956 err = mlx4_SET_VLAN_FLTR(mdev->dev, priv);
958 en_err(priv, "Failed disabling VLAN filter\n");
962 static void mlx4_en_clear_promisc_mode(struct mlx4_en_priv *priv,
963 struct mlx4_en_dev *mdev)
967 if (netif_msg_rx_status(priv))
968 en_warn(priv, "Leaving promiscuous mode\n");
969 priv->flags &= ~MLX4_EN_FLAG_PROMISC;
971 /* Disable promiscouos mode */
972 switch (mdev->dev->caps.steering_mode) {
973 case MLX4_STEERING_MODE_DEVICE_MANAGED:
974 err = mlx4_flow_steer_promisc_remove(mdev->dev,
976 MLX4_FS_ALL_DEFAULT);
978 en_err(priv, "Failed disabling promiscuous mode\n");
979 priv->flags &= ~MLX4_EN_FLAG_MC_PROMISC;
982 case MLX4_STEERING_MODE_B0:
983 err = mlx4_unicast_promisc_remove(mdev->dev,
987 en_err(priv, "Failed disabling unicast promiscuous mode\n");
988 /* Disable Multicast promisc */
989 if (priv->flags & MLX4_EN_FLAG_MC_PROMISC) {
990 err = mlx4_multicast_promisc_remove(mdev->dev,
994 en_err(priv, "Failed disabling multicast promiscuous mode\n");
995 priv->flags &= ~MLX4_EN_FLAG_MC_PROMISC;
999 case MLX4_STEERING_MODE_A0:
1000 err = mlx4_SET_PORT_qpn_calc(mdev->dev,
1004 en_err(priv, "Failed disabling promiscuous mode\n");
1008 /* Enable port VLAN filter */
1009 err = mlx4_SET_VLAN_FLTR(mdev->dev, priv);
1011 en_err(priv, "Failed enabling VLAN filter\n");
1014 static void mlx4_en_do_multicast(struct mlx4_en_priv *priv,
1015 struct net_device *dev,
1016 struct mlx4_en_dev *mdev)
1018 struct mlx4_en_mc_list *mclist, *tmp;
1020 u8 mc_list[16] = {0};
1023 /* Enable/disable the multicast filter according to IFF_ALLMULTI */
1024 if (dev->flags & IFF_ALLMULTI) {
1025 err = mlx4_SET_MCAST_FLTR(mdev->dev, priv->port, 0,
1026 0, MLX4_MCAST_DISABLE);
1028 en_err(priv, "Failed disabling multicast filter\n");
1030 /* Add the default qp number as multicast promisc */
1031 if (!(priv->flags & MLX4_EN_FLAG_MC_PROMISC)) {
1032 switch (mdev->dev->caps.steering_mode) {
1033 case MLX4_STEERING_MODE_DEVICE_MANAGED:
1034 err = mlx4_flow_steer_promisc_add(mdev->dev,
1037 MLX4_FS_MC_DEFAULT);
1040 case MLX4_STEERING_MODE_B0:
1041 err = mlx4_multicast_promisc_add(mdev->dev,
1046 case MLX4_STEERING_MODE_A0:
1050 en_err(priv, "Failed entering multicast promisc mode\n");
1051 priv->flags |= MLX4_EN_FLAG_MC_PROMISC;
1054 /* Disable Multicast promisc */
1055 if (priv->flags & MLX4_EN_FLAG_MC_PROMISC) {
1056 switch (mdev->dev->caps.steering_mode) {
1057 case MLX4_STEERING_MODE_DEVICE_MANAGED:
1058 err = mlx4_flow_steer_promisc_remove(mdev->dev,
1060 MLX4_FS_MC_DEFAULT);
1063 case MLX4_STEERING_MODE_B0:
1064 err = mlx4_multicast_promisc_remove(mdev->dev,
1069 case MLX4_STEERING_MODE_A0:
1073 en_err(priv, "Failed disabling multicast promiscuous mode\n");
1074 priv->flags &= ~MLX4_EN_FLAG_MC_PROMISC;
1077 err = mlx4_SET_MCAST_FLTR(mdev->dev, priv->port, 0,
1078 0, MLX4_MCAST_DISABLE);
1080 en_err(priv, "Failed disabling multicast filter\n");
1082 /* Flush mcast filter and init it with broadcast address */
1083 mlx4_SET_MCAST_FLTR(mdev->dev, priv->port, ETH_BCAST,
1084 1, MLX4_MCAST_CONFIG);
1086 /* Update multicast list - we cache all addresses so they won't
1087 * change while HW is updated holding the command semaphor */
1088 netif_addr_lock_bh(dev);
1089 mlx4_en_cache_mclist(dev);
1090 netif_addr_unlock_bh(dev);
1091 list_for_each_entry(mclist, &priv->mc_list, list) {
1092 mcast_addr = mlx4_en_mac_to_u64(mclist->addr);
1093 mlx4_SET_MCAST_FLTR(mdev->dev, priv->port,
1094 mcast_addr, 0, MLX4_MCAST_CONFIG);
1096 err = mlx4_SET_MCAST_FLTR(mdev->dev, priv->port, 0,
1097 0, MLX4_MCAST_ENABLE);
1099 en_err(priv, "Failed enabling multicast filter\n");
1101 update_mclist_flags(priv, &priv->curr_list, &priv->mc_list);
1102 list_for_each_entry_safe(mclist, tmp, &priv->curr_list, list) {
1103 if (mclist->action == MCLIST_REM) {
1104 /* detach this address and delete from list */
1105 memcpy(&mc_list[10], mclist->addr, ETH_ALEN);
1106 mc_list[5] = priv->port;
1107 err = mlx4_multicast_detach(mdev->dev,
1108 &priv->rss_map.indir_qp,
1113 en_err(priv, "Fail to detach multicast address\n");
1115 if (mclist->tunnel_reg_id) {
1116 err = mlx4_flow_detach(priv->mdev->dev, mclist->tunnel_reg_id);
1118 en_err(priv, "Failed to detach multicast address\n");
1121 /* remove from list */
1122 list_del(&mclist->list);
1124 } else if (mclist->action == MCLIST_ADD) {
1125 /* attach the address */
1126 memcpy(&mc_list[10], mclist->addr, ETH_ALEN);
1127 /* needed for B0 steering support */
1128 mc_list[5] = priv->port;
1129 err = mlx4_multicast_attach(mdev->dev,
1130 &priv->rss_map.indir_qp,
1136 en_err(priv, "Fail to attach multicast address\n");
1138 err = mlx4_en_tunnel_steer_add(priv, &mc_list[10], priv->base_qpn,
1139 &mclist->tunnel_reg_id);
1141 en_err(priv, "Failed to attach multicast address\n");
1147 static void mlx4_en_do_uc_filter(struct mlx4_en_priv *priv,
1148 struct net_device *dev,
1149 struct mlx4_en_dev *mdev)
1151 struct netdev_hw_addr *ha;
1152 struct mlx4_mac_entry *entry;
1153 struct hlist_node *tmp;
1157 struct hlist_head *bucket;
1162 /* Note that we do not need to protect our mac_hash traversal with rcu,
1163 * since all modification code is protected by mdev->state_lock
1166 /* find what to remove */
1167 for (i = 0; i < MLX4_EN_MAC_HASH_SIZE; ++i) {
1168 bucket = &priv->mac_hash[i];
1169 hlist_for_each_entry_safe(entry, tmp, bucket, hlist) {
1171 netdev_for_each_uc_addr(ha, dev) {
1172 if (ether_addr_equal_64bits(entry->mac,
1179 /* MAC address of the port is not in uc list */
1180 if (ether_addr_equal_64bits(entry->mac, dev->dev_addr))
1184 mac = mlx4_en_mac_to_u64(entry->mac);
1185 mlx4_en_uc_steer_release(priv, entry->mac,
1188 mlx4_unregister_mac(mdev->dev, priv->port, mac);
1190 hlist_del_rcu(&entry->hlist);
1191 kfree_rcu(entry, rcu);
1192 en_dbg(DRV, priv, "Removed MAC %pM on port:%d\n",
1193 entry->mac, priv->port);
1199 /* if we didn't remove anything, there is no use in trying to add
1200 * again once we are in a forced promisc mode state
1202 if ((priv->flags & MLX4_EN_FLAG_FORCE_PROMISC) && 0 == removed)
1205 prev_flags = priv->flags;
1206 priv->flags &= ~MLX4_EN_FLAG_FORCE_PROMISC;
1208 /* find what to add */
1209 netdev_for_each_uc_addr(ha, dev) {
1211 bucket = &priv->mac_hash[ha->addr[MLX4_EN_MAC_HASH_IDX]];
1212 hlist_for_each_entry(entry, bucket, hlist) {
1213 if (ether_addr_equal_64bits(entry->mac, ha->addr)) {
1220 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1222 en_err(priv, "Failed adding MAC %pM on port:%d (out of memory)\n",
1223 ha->addr, priv->port);
1224 priv->flags |= MLX4_EN_FLAG_FORCE_PROMISC;
1227 mac = mlx4_en_mac_to_u64(ha->addr);
1228 memcpy(entry->mac, ha->addr, ETH_ALEN);
1229 err = mlx4_register_mac(mdev->dev, priv->port, mac);
1231 en_err(priv, "Failed registering MAC %pM on port %d: %d\n",
1232 ha->addr, priv->port, err);
1234 priv->flags |= MLX4_EN_FLAG_FORCE_PROMISC;
1237 err = mlx4_en_uc_steer_add(priv, ha->addr,
1241 en_err(priv, "Failed adding MAC %pM on port %d: %d\n",
1242 ha->addr, priv->port, err);
1243 mlx4_unregister_mac(mdev->dev, priv->port, mac);
1245 priv->flags |= MLX4_EN_FLAG_FORCE_PROMISC;
1248 unsigned int mac_hash;
1249 en_dbg(DRV, priv, "Added MAC %pM on port:%d\n",
1250 ha->addr, priv->port);
1251 mac_hash = ha->addr[MLX4_EN_MAC_HASH_IDX];
1252 bucket = &priv->mac_hash[mac_hash];
1253 hlist_add_head_rcu(&entry->hlist, bucket);
1258 if (priv->flags & MLX4_EN_FLAG_FORCE_PROMISC) {
1259 en_warn(priv, "Forcing promiscuous mode on port:%d\n",
1261 } else if (prev_flags & MLX4_EN_FLAG_FORCE_PROMISC) {
1262 en_warn(priv, "Stop forcing promiscuous mode on port:%d\n",
1267 static void mlx4_en_do_set_rx_mode(struct work_struct *work)
1269 struct mlx4_en_priv *priv = container_of(work, struct mlx4_en_priv,
1271 struct mlx4_en_dev *mdev = priv->mdev;
1272 struct net_device *dev = priv->dev;
1274 mutex_lock(&mdev->state_lock);
1275 if (!mdev->device_up) {
1276 en_dbg(HW, priv, "Card is not up, ignoring rx mode change.\n");
1279 if (!priv->port_up) {
1280 en_dbg(HW, priv, "Port is down, ignoring rx mode change.\n");
1284 if (!netif_carrier_ok(dev)) {
1285 if (!mlx4_en_QUERY_PORT(mdev, priv->port)) {
1286 if (priv->port_state.link_state) {
1287 priv->last_link_state = MLX4_DEV_EVENT_PORT_UP;
1288 netif_carrier_on(dev);
1289 en_dbg(LINK, priv, "Link Up\n");
1294 if (dev->priv_flags & IFF_UNICAST_FLT)
1295 mlx4_en_do_uc_filter(priv, dev, mdev);
1297 /* Promsicuous mode: disable all filters */
1298 if ((dev->flags & IFF_PROMISC) ||
1299 (priv->flags & MLX4_EN_FLAG_FORCE_PROMISC)) {
1300 mlx4_en_set_promisc_mode(priv, mdev);
1304 /* Not in promiscuous mode */
1305 if (priv->flags & MLX4_EN_FLAG_PROMISC)
1306 mlx4_en_clear_promisc_mode(priv, mdev);
1308 mlx4_en_do_multicast(priv, dev, mdev);
1310 mutex_unlock(&mdev->state_lock);
1313 #ifdef CONFIG_NET_POLL_CONTROLLER
1314 static void mlx4_en_netpoll(struct net_device *dev)
1316 struct mlx4_en_priv *priv = netdev_priv(dev);
1317 struct mlx4_en_cq *cq;
1318 unsigned long flags;
1321 for (i = 0; i < priv->rx_ring_num; i++) {
1322 cq = priv->rx_cq[i];
1323 spin_lock_irqsave(&cq->lock, flags);
1324 napi_synchronize(&cq->napi);
1325 mlx4_en_process_rx_cq(dev, cq, 0);
1326 spin_unlock_irqrestore(&cq->lock, flags);
1331 static void mlx4_en_tx_timeout(struct net_device *dev)
1333 struct mlx4_en_priv *priv = netdev_priv(dev);
1334 struct mlx4_en_dev *mdev = priv->mdev;
1337 if (netif_msg_timer(priv))
1338 en_warn(priv, "Tx timeout called on port:%d\n", priv->port);
1340 for (i = 0; i < priv->tx_ring_num; i++) {
1341 if (!netif_tx_queue_stopped(netdev_get_tx_queue(dev, i)))
1343 en_warn(priv, "TX timeout on queue: %d, QP: 0x%x, CQ: 0x%x, Cons: 0x%x, Prod: 0x%x\n",
1344 i, priv->tx_ring[i]->qpn, priv->tx_ring[i]->cqn,
1345 priv->tx_ring[i]->cons, priv->tx_ring[i]->prod);
1348 priv->port_stats.tx_timeout++;
1349 en_dbg(DRV, priv, "Scheduling watchdog\n");
1350 queue_work(mdev->workqueue, &priv->watchdog_task);
1354 static struct net_device_stats *mlx4_en_get_stats(struct net_device *dev)
1356 struct mlx4_en_priv *priv = netdev_priv(dev);
1358 spin_lock_bh(&priv->stats_lock);
1359 memcpy(&priv->ret_stats, &priv->stats, sizeof(priv->stats));
1360 spin_unlock_bh(&priv->stats_lock);
1362 return &priv->ret_stats;
1365 static void mlx4_en_set_default_moderation(struct mlx4_en_priv *priv)
1367 struct mlx4_en_cq *cq;
1370 /* If we haven't received a specific coalescing setting
1371 * (module param), we set the moderation parameters as follows:
1372 * - moder_cnt is set to the number of mtu sized packets to
1373 * satisfy our coalescing target.
1374 * - moder_time is set to a fixed value.
1376 priv->rx_frames = MLX4_EN_RX_COAL_TARGET;
1377 priv->rx_usecs = MLX4_EN_RX_COAL_TIME;
1378 priv->tx_frames = MLX4_EN_TX_COAL_PKTS;
1379 priv->tx_usecs = MLX4_EN_TX_COAL_TIME;
1380 en_dbg(INTR, priv, "Default coalesing params for mtu:%d - rx_frames:%d rx_usecs:%d\n",
1381 priv->dev->mtu, priv->rx_frames, priv->rx_usecs);
1383 /* Setup cq moderation params */
1384 for (i = 0; i < priv->rx_ring_num; i++) {
1385 cq = priv->rx_cq[i];
1386 cq->moder_cnt = priv->rx_frames;
1387 cq->moder_time = priv->rx_usecs;
1388 priv->last_moder_time[i] = MLX4_EN_AUTO_CONF;
1389 priv->last_moder_packets[i] = 0;
1390 priv->last_moder_bytes[i] = 0;
1393 for (i = 0; i < priv->tx_ring_num; i++) {
1394 cq = priv->tx_cq[i];
1395 cq->moder_cnt = priv->tx_frames;
1396 cq->moder_time = priv->tx_usecs;
1399 /* Reset auto-moderation params */
1400 priv->pkt_rate_low = MLX4_EN_RX_RATE_LOW;
1401 priv->rx_usecs_low = MLX4_EN_RX_COAL_TIME_LOW;
1402 priv->pkt_rate_high = MLX4_EN_RX_RATE_HIGH;
1403 priv->rx_usecs_high = MLX4_EN_RX_COAL_TIME_HIGH;
1404 priv->sample_interval = MLX4_EN_SAMPLE_INTERVAL;
1405 priv->adaptive_rx_coal = 1;
1406 priv->last_moder_jiffies = 0;
1407 priv->last_moder_tx_packets = 0;
1410 static void mlx4_en_auto_moderation(struct mlx4_en_priv *priv)
1412 unsigned long period = (unsigned long) (jiffies - priv->last_moder_jiffies);
1413 struct mlx4_en_cq *cq;
1414 unsigned long packets;
1416 unsigned long avg_pkt_size;
1417 unsigned long rx_packets;
1418 unsigned long rx_bytes;
1419 unsigned long rx_pkt_diff;
1423 if (!priv->adaptive_rx_coal || period < priv->sample_interval * HZ)
1426 for (ring = 0; ring < priv->rx_ring_num; ring++) {
1427 spin_lock_bh(&priv->stats_lock);
1428 rx_packets = priv->rx_ring[ring]->packets;
1429 rx_bytes = priv->rx_ring[ring]->bytes;
1430 spin_unlock_bh(&priv->stats_lock);
1432 rx_pkt_diff = ((unsigned long) (rx_packets -
1433 priv->last_moder_packets[ring]));
1434 packets = rx_pkt_diff;
1435 rate = packets * HZ / period;
1436 avg_pkt_size = packets ? ((unsigned long) (rx_bytes -
1437 priv->last_moder_bytes[ring])) / packets : 0;
1439 /* Apply auto-moderation only when packet rate
1440 * exceeds a rate that it matters */
1441 if (rate > (MLX4_EN_RX_RATE_THRESH / priv->rx_ring_num) &&
1442 avg_pkt_size > MLX4_EN_AVG_PKT_SMALL) {
1443 if (rate < priv->pkt_rate_low)
1444 moder_time = priv->rx_usecs_low;
1445 else if (rate > priv->pkt_rate_high)
1446 moder_time = priv->rx_usecs_high;
1448 moder_time = (rate - priv->pkt_rate_low) *
1449 (priv->rx_usecs_high - priv->rx_usecs_low) /
1450 (priv->pkt_rate_high - priv->pkt_rate_low) +
1453 moder_time = priv->rx_usecs_low;
1456 if (moder_time != priv->last_moder_time[ring]) {
1457 priv->last_moder_time[ring] = moder_time;
1458 cq = priv->rx_cq[ring];
1459 cq->moder_time = moder_time;
1460 cq->moder_cnt = priv->rx_frames;
1461 err = mlx4_en_set_cq_moder(priv, cq);
1463 en_err(priv, "Failed modifying moderation for cq:%d\n",
1466 priv->last_moder_packets[ring] = rx_packets;
1467 priv->last_moder_bytes[ring] = rx_bytes;
1470 priv->last_moder_jiffies = jiffies;
1473 static void mlx4_en_do_get_stats(struct work_struct *work)
1475 struct delayed_work *delay = to_delayed_work(work);
1476 struct mlx4_en_priv *priv = container_of(delay, struct mlx4_en_priv,
1478 struct mlx4_en_dev *mdev = priv->mdev;
1481 mutex_lock(&mdev->state_lock);
1482 if (mdev->device_up) {
1483 if (priv->port_up) {
1484 err = mlx4_en_DUMP_ETH_STATS(mdev, priv->port, 0);
1486 en_dbg(HW, priv, "Could not update stats\n");
1488 mlx4_en_auto_moderation(priv);
1491 queue_delayed_work(mdev->workqueue, &priv->stats_task, STATS_DELAY);
1493 if (mdev->mac_removed[MLX4_MAX_PORTS + 1 - priv->port]) {
1494 mlx4_en_do_set_mac(priv);
1495 mdev->mac_removed[MLX4_MAX_PORTS + 1 - priv->port] = 0;
1497 mutex_unlock(&mdev->state_lock);
1500 /* mlx4_en_service_task - Run service task for tasks that needed to be done
1503 static void mlx4_en_service_task(struct work_struct *work)
1505 struct delayed_work *delay = to_delayed_work(work);
1506 struct mlx4_en_priv *priv = container_of(delay, struct mlx4_en_priv,
1508 struct mlx4_en_dev *mdev = priv->mdev;
1510 mutex_lock(&mdev->state_lock);
1511 if (mdev->device_up) {
1512 if (mdev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_TS)
1513 mlx4_en_ptp_overflow_check(mdev);
1515 queue_delayed_work(mdev->workqueue, &priv->service_task,
1516 SERVICE_TASK_DELAY);
1518 mutex_unlock(&mdev->state_lock);
1521 static void mlx4_en_linkstate(struct work_struct *work)
1523 struct mlx4_en_priv *priv = container_of(work, struct mlx4_en_priv,
1525 struct mlx4_en_dev *mdev = priv->mdev;
1526 int linkstate = priv->link_state;
1528 mutex_lock(&mdev->state_lock);
1529 /* If observable port state changed set carrier state and
1530 * report to system log */
1531 if (priv->last_link_state != linkstate) {
1532 if (linkstate == MLX4_DEV_EVENT_PORT_DOWN) {
1533 en_info(priv, "Link Down\n");
1534 netif_carrier_off(priv->dev);
1536 en_info(priv, "Link Up\n");
1537 netif_carrier_on(priv->dev);
1540 priv->last_link_state = linkstate;
1541 mutex_unlock(&mdev->state_lock);
1545 int mlx4_en_start_port(struct net_device *dev)
1547 struct mlx4_en_priv *priv = netdev_priv(dev);
1548 struct mlx4_en_dev *mdev = priv->mdev;
1549 struct mlx4_en_cq *cq;
1550 struct mlx4_en_tx_ring *tx_ring;
1556 u8 mc_list[16] = {0};
1558 if (priv->port_up) {
1559 en_dbg(DRV, priv, "start port called while port already up\n");
1563 INIT_LIST_HEAD(&priv->mc_list);
1564 INIT_LIST_HEAD(&priv->curr_list);
1565 INIT_LIST_HEAD(&priv->ethtool_list);
1566 memset(&priv->ethtool_rules[0], 0,
1567 sizeof(struct ethtool_flow_id) * MAX_NUM_OF_FS_RULES);
1569 /* Calculate Rx buf size */
1570 dev->mtu = min(dev->mtu, priv->max_mtu);
1571 mlx4_en_calc_rx_buf(dev);
1572 en_dbg(DRV, priv, "Rx buf size:%d\n", priv->rx_skb_size);
1574 /* Configure rx cq's and rings */
1575 err = mlx4_en_activate_rx_rings(priv);
1577 en_err(priv, "Failed to activate RX rings\n");
1580 for (i = 0; i < priv->rx_ring_num; i++) {
1581 cq = priv->rx_cq[i];
1583 mlx4_en_cq_init_lock(cq);
1585 err = mlx4_en_activate_cq(priv, cq, i);
1587 en_err(priv, "Failed activating Rx CQ\n");
1590 for (j = 0; j < cq->size; j++)
1591 cq->buf[j].owner_sr_opcode = MLX4_CQE_OWNER_MASK;
1592 err = mlx4_en_set_cq_moder(priv, cq);
1594 en_err(priv, "Failed setting cq moderation parameters");
1595 mlx4_en_deactivate_cq(priv, cq);
1598 mlx4_en_arm_cq(priv, cq);
1599 priv->rx_ring[i]->cqn = cq->mcq.cqn;
1604 en_dbg(DRV, priv, "Getting qp number for port %d\n", priv->port);
1605 err = mlx4_en_get_qp(priv);
1607 en_err(priv, "Failed getting eth qp\n");
1610 mdev->mac_removed[priv->port] = 0;
1612 err = mlx4_en_config_rss_steer(priv);
1614 en_err(priv, "Failed configuring rss steering\n");
1618 err = mlx4_en_create_drop_qp(priv);
1622 /* Configure tx cq's and rings */
1623 for (i = 0; i < priv->tx_ring_num; i++) {
1625 cq = priv->tx_cq[i];
1626 err = mlx4_en_activate_cq(priv, cq, i);
1628 en_err(priv, "Failed allocating Tx CQ\n");
1631 err = mlx4_en_set_cq_moder(priv, cq);
1633 en_err(priv, "Failed setting cq moderation parameters");
1634 mlx4_en_deactivate_cq(priv, cq);
1637 en_dbg(DRV, priv, "Resetting index of collapsed CQ:%d to -1\n", i);
1638 cq->buf->wqe_index = cpu_to_be16(0xffff);
1640 /* Configure ring */
1641 tx_ring = priv->tx_ring[i];
1642 err = mlx4_en_activate_tx_ring(priv, tx_ring, cq->mcq.cqn,
1643 i / priv->num_tx_rings_p_up);
1645 en_err(priv, "Failed allocating Tx ring\n");
1646 mlx4_en_deactivate_cq(priv, cq);
1649 tx_ring->tx_queue = netdev_get_tx_queue(dev, i);
1651 /* Arm CQ for TX completions */
1652 mlx4_en_arm_cq(priv, cq);
1654 /* Set initial ownership of all Tx TXBBs to SW (1) */
1655 for (j = 0; j < tx_ring->buf_size; j += STAMP_STRIDE)
1656 *((u32 *) (tx_ring->buf + j)) = 0xffffffff;
1660 /* Configure port */
1661 err = mlx4_SET_PORT_general(mdev->dev, priv->port,
1662 priv->rx_skb_size + ETH_FCS_LEN,
1663 priv->prof->tx_pause,
1665 priv->prof->rx_pause,
1666 priv->prof->rx_ppp);
1668 en_err(priv, "Failed setting port general configurations for port %d, with error %d\n",
1672 /* Set default qp number */
1673 err = mlx4_SET_PORT_qpn_calc(mdev->dev, priv->port, priv->base_qpn, 0);
1675 en_err(priv, "Failed setting default qp numbers\n");
1679 if (mdev->dev->caps.tunnel_offload_mode == MLX4_TUNNEL_OFFLOAD_MODE_VXLAN) {
1680 err = mlx4_SET_PORT_VXLAN(mdev->dev, priv->port, VXLAN_STEER_BY_OUTER_MAC);
1682 en_err(priv, "Failed setting port L2 tunnel configuration, err %d\n",
1689 en_dbg(HW, priv, "Initializing port\n");
1690 err = mlx4_INIT_PORT(mdev->dev, priv->port);
1692 en_err(priv, "Failed Initializing port\n");
1696 /* Attach rx QP to bradcast address */
1697 memset(&mc_list[10], 0xff, ETH_ALEN);
1698 mc_list[5] = priv->port; /* needed for B0 steering support */
1699 if (mlx4_multicast_attach(mdev->dev, &priv->rss_map.indir_qp, mc_list,
1700 priv->port, 0, MLX4_PROT_ETH,
1701 &priv->broadcast_id))
1702 mlx4_warn(mdev, "Failed Attaching Broadcast\n");
1704 /* Must redo promiscuous mode setup. */
1705 priv->flags &= ~(MLX4_EN_FLAG_PROMISC | MLX4_EN_FLAG_MC_PROMISC);
1707 /* Schedule multicast task to populate multicast list */
1708 queue_work(mdev->workqueue, &priv->rx_mode_task);
1710 mlx4_set_stats_bitmap(mdev->dev, &priv->stats_bitmap);
1712 priv->port_up = true;
1713 netif_tx_start_all_queues(dev);
1714 netif_device_attach(dev);
1719 while (tx_index--) {
1720 mlx4_en_deactivate_tx_ring(priv, priv->tx_ring[tx_index]);
1721 mlx4_en_deactivate_cq(priv, priv->tx_cq[tx_index]);
1723 mlx4_en_destroy_drop_qp(priv);
1725 mlx4_en_release_rss_steer(priv);
1727 mlx4_en_put_qp(priv);
1730 mlx4_en_deactivate_cq(priv, priv->rx_cq[rx_index]);
1731 for (i = 0; i < priv->rx_ring_num; i++)
1732 mlx4_en_deactivate_rx_ring(priv, priv->rx_ring[i]);
1734 return err; /* need to close devices */
1738 void mlx4_en_stop_port(struct net_device *dev, int detach)
1740 struct mlx4_en_priv *priv = netdev_priv(dev);
1741 struct mlx4_en_dev *mdev = priv->mdev;
1742 struct mlx4_en_mc_list *mclist, *tmp;
1743 struct ethtool_flow_id *flow, *tmp_flow;
1745 u8 mc_list[16] = {0};
1747 if (!priv->port_up) {
1748 en_dbg(DRV, priv, "stop port called while port already down\n");
1753 mlx4_CLOSE_PORT(mdev->dev, priv->port);
1755 /* Synchronize with tx routine */
1756 netif_tx_lock_bh(dev);
1758 netif_device_detach(dev);
1759 netif_tx_stop_all_queues(dev);
1760 netif_tx_unlock_bh(dev);
1762 netif_tx_disable(dev);
1764 /* Set port as not active */
1765 priv->port_up = false;
1767 /* Promsicuous mode */
1768 if (mdev->dev->caps.steering_mode ==
1769 MLX4_STEERING_MODE_DEVICE_MANAGED) {
1770 priv->flags &= ~(MLX4_EN_FLAG_PROMISC |
1771 MLX4_EN_FLAG_MC_PROMISC);
1772 mlx4_flow_steer_promisc_remove(mdev->dev,
1774 MLX4_FS_ALL_DEFAULT);
1775 mlx4_flow_steer_promisc_remove(mdev->dev,
1777 MLX4_FS_MC_DEFAULT);
1778 } else if (priv->flags & MLX4_EN_FLAG_PROMISC) {
1779 priv->flags &= ~MLX4_EN_FLAG_PROMISC;
1781 /* Disable promiscouos mode */
1782 mlx4_unicast_promisc_remove(mdev->dev, priv->base_qpn,
1785 /* Disable Multicast promisc */
1786 if (priv->flags & MLX4_EN_FLAG_MC_PROMISC) {
1787 mlx4_multicast_promisc_remove(mdev->dev, priv->base_qpn,
1789 priv->flags &= ~MLX4_EN_FLAG_MC_PROMISC;
1793 /* Detach All multicasts */
1794 memset(&mc_list[10], 0xff, ETH_ALEN);
1795 mc_list[5] = priv->port; /* needed for B0 steering support */
1796 mlx4_multicast_detach(mdev->dev, &priv->rss_map.indir_qp, mc_list,
1797 MLX4_PROT_ETH, priv->broadcast_id);
1798 list_for_each_entry(mclist, &priv->curr_list, list) {
1799 memcpy(&mc_list[10], mclist->addr, ETH_ALEN);
1800 mc_list[5] = priv->port;
1801 mlx4_multicast_detach(mdev->dev, &priv->rss_map.indir_qp,
1802 mc_list, MLX4_PROT_ETH, mclist->reg_id);
1803 if (mclist->tunnel_reg_id)
1804 mlx4_flow_detach(mdev->dev, mclist->tunnel_reg_id);
1806 mlx4_en_clear_list(dev);
1807 list_for_each_entry_safe(mclist, tmp, &priv->curr_list, list) {
1808 list_del(&mclist->list);
1812 /* Flush multicast filter */
1813 mlx4_SET_MCAST_FLTR(mdev->dev, priv->port, 0, 1, MLX4_MCAST_CONFIG);
1815 /* Remove flow steering rules for the port*/
1816 if (mdev->dev->caps.steering_mode ==
1817 MLX4_STEERING_MODE_DEVICE_MANAGED) {
1819 list_for_each_entry_safe(flow, tmp_flow,
1820 &priv->ethtool_list, list) {
1821 mlx4_flow_detach(mdev->dev, flow->id);
1822 list_del(&flow->list);
1826 mlx4_en_destroy_drop_qp(priv);
1829 for (i = 0; i < priv->tx_ring_num; i++) {
1830 mlx4_en_deactivate_tx_ring(priv, priv->tx_ring[i]);
1831 mlx4_en_deactivate_cq(priv, priv->tx_cq[i]);
1835 for (i = 0; i < priv->tx_ring_num; i++)
1836 mlx4_en_free_tx_buf(dev, priv->tx_ring[i]);
1839 mlx4_en_release_rss_steer(priv);
1841 /* Unregister Mac address for the port */
1842 mlx4_en_put_qp(priv);
1843 if (!(mdev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_REASSIGN_MAC_EN))
1844 mdev->mac_removed[priv->port] = 1;
1847 for (i = 0; i < priv->rx_ring_num; i++) {
1848 struct mlx4_en_cq *cq = priv->rx_cq[i];
1851 while (!mlx4_en_cq_lock_napi(cq)) {
1852 pr_info("CQ %d locked\n", i);
1857 while (test_bit(NAPI_STATE_SCHED, &cq->napi.state))
1859 mlx4_en_deactivate_rx_ring(priv, priv->rx_ring[i]);
1860 mlx4_en_deactivate_cq(priv, cq);
1864 static void mlx4_en_restart(struct work_struct *work)
1866 struct mlx4_en_priv *priv = container_of(work, struct mlx4_en_priv,
1868 struct mlx4_en_dev *mdev = priv->mdev;
1869 struct net_device *dev = priv->dev;
1871 en_dbg(DRV, priv, "Watchdog task called for port %d\n", priv->port);
1873 mutex_lock(&mdev->state_lock);
1874 if (priv->port_up) {
1875 mlx4_en_stop_port(dev, 1);
1876 if (mlx4_en_start_port(dev))
1877 en_err(priv, "Failed restarting port %d\n", priv->port);
1879 mutex_unlock(&mdev->state_lock);
1882 static void mlx4_en_clear_stats(struct net_device *dev)
1884 struct mlx4_en_priv *priv = netdev_priv(dev);
1885 struct mlx4_en_dev *mdev = priv->mdev;
1888 if (mlx4_en_DUMP_ETH_STATS(mdev, priv->port, 1))
1889 en_dbg(HW, priv, "Failed dumping statistics\n");
1891 memset(&priv->stats, 0, sizeof(priv->stats));
1892 memset(&priv->pstats, 0, sizeof(priv->pstats));
1893 memset(&priv->pkstats, 0, sizeof(priv->pkstats));
1894 memset(&priv->port_stats, 0, sizeof(priv->port_stats));
1896 for (i = 0; i < priv->tx_ring_num; i++) {
1897 priv->tx_ring[i]->bytes = 0;
1898 priv->tx_ring[i]->packets = 0;
1899 priv->tx_ring[i]->tx_csum = 0;
1901 for (i = 0; i < priv->rx_ring_num; i++) {
1902 priv->rx_ring[i]->bytes = 0;
1903 priv->rx_ring[i]->packets = 0;
1904 priv->rx_ring[i]->csum_ok = 0;
1905 priv->rx_ring[i]->csum_none = 0;
1909 static int mlx4_en_open(struct net_device *dev)
1911 struct mlx4_en_priv *priv = netdev_priv(dev);
1912 struct mlx4_en_dev *mdev = priv->mdev;
1915 mutex_lock(&mdev->state_lock);
1917 if (!mdev->device_up) {
1918 en_err(priv, "Cannot open - device down/disabled\n");
1923 /* Reset HW statistics and SW counters */
1924 mlx4_en_clear_stats(dev);
1926 err = mlx4_en_start_port(dev);
1928 en_err(priv, "Failed starting port:%d\n", priv->port);
1931 mutex_unlock(&mdev->state_lock);
1936 static int mlx4_en_close(struct net_device *dev)
1938 struct mlx4_en_priv *priv = netdev_priv(dev);
1939 struct mlx4_en_dev *mdev = priv->mdev;
1941 en_dbg(IFDOWN, priv, "Close port called\n");
1943 mutex_lock(&mdev->state_lock);
1945 mlx4_en_stop_port(dev, 0);
1946 netif_carrier_off(dev);
1948 mutex_unlock(&mdev->state_lock);
1952 void mlx4_en_free_resources(struct mlx4_en_priv *priv)
1956 #ifdef CONFIG_RFS_ACCEL
1957 free_irq_cpu_rmap(priv->dev->rx_cpu_rmap);
1958 priv->dev->rx_cpu_rmap = NULL;
1961 for (i = 0; i < priv->tx_ring_num; i++) {
1962 if (priv->tx_ring && priv->tx_ring[i])
1963 mlx4_en_destroy_tx_ring(priv, &priv->tx_ring[i]);
1964 if (priv->tx_cq && priv->tx_cq[i])
1965 mlx4_en_destroy_cq(priv, &priv->tx_cq[i]);
1968 for (i = 0; i < priv->rx_ring_num; i++) {
1969 if (priv->rx_ring[i])
1970 mlx4_en_destroy_rx_ring(priv, &priv->rx_ring[i],
1971 priv->prof->rx_ring_size, priv->stride);
1973 mlx4_en_destroy_cq(priv, &priv->rx_cq[i]);
1976 if (priv->base_tx_qpn) {
1977 mlx4_qp_release_range(priv->mdev->dev, priv->base_tx_qpn, priv->tx_ring_num);
1978 priv->base_tx_qpn = 0;
1982 int mlx4_en_alloc_resources(struct mlx4_en_priv *priv)
1984 struct mlx4_en_port_profile *prof = priv->prof;
1989 err = mlx4_qp_reserve_range(priv->mdev->dev, priv->tx_ring_num, 256, &priv->base_tx_qpn);
1991 en_err(priv, "failed reserving range for TX rings\n");
1995 /* Create tx Rings */
1996 for (i = 0; i < priv->tx_ring_num; i++) {
1997 node = cpu_to_node(i % num_online_cpus());
1998 if (mlx4_en_create_cq(priv, &priv->tx_cq[i],
1999 prof->tx_ring_size, i, TX, node))
2002 if (mlx4_en_create_tx_ring(priv, &priv->tx_ring[i],
2003 priv->base_tx_qpn + i,
2004 prof->tx_ring_size, TXBB_SIZE,
2009 /* Create rx Rings */
2010 for (i = 0; i < priv->rx_ring_num; i++) {
2011 node = cpu_to_node(i % num_online_cpus());
2012 if (mlx4_en_create_cq(priv, &priv->rx_cq[i],
2013 prof->rx_ring_size, i, RX, node))
2016 if (mlx4_en_create_rx_ring(priv, &priv->rx_ring[i],
2017 prof->rx_ring_size, priv->stride,
2022 #ifdef CONFIG_RFS_ACCEL
2023 if (priv->mdev->dev->caps.comp_pool) {
2024 priv->dev->rx_cpu_rmap = alloc_irq_cpu_rmap(priv->mdev->dev->caps.comp_pool);
2025 if (!priv->dev->rx_cpu_rmap)
2033 en_err(priv, "Failed to allocate NIC resources\n");
2034 for (i = 0; i < priv->rx_ring_num; i++) {
2035 if (priv->rx_ring[i])
2036 mlx4_en_destroy_rx_ring(priv, &priv->rx_ring[i],
2040 mlx4_en_destroy_cq(priv, &priv->rx_cq[i]);
2042 for (i = 0; i < priv->tx_ring_num; i++) {
2043 if (priv->tx_ring[i])
2044 mlx4_en_destroy_tx_ring(priv, &priv->tx_ring[i]);
2046 mlx4_en_destroy_cq(priv, &priv->tx_cq[i]);
2052 void mlx4_en_destroy_netdev(struct net_device *dev)
2054 struct mlx4_en_priv *priv = netdev_priv(dev);
2055 struct mlx4_en_dev *mdev = priv->mdev;
2057 en_dbg(DRV, priv, "Destroying netdev on port:%d\n", priv->port);
2059 /* Unregister device - this will close the port if it was up */
2060 if (priv->registered)
2061 unregister_netdev(dev);
2063 if (priv->allocated)
2064 mlx4_free_hwq_res(mdev->dev, &priv->res, MLX4_EN_PAGE_SIZE);
2066 cancel_delayed_work(&priv->stats_task);
2067 cancel_delayed_work(&priv->service_task);
2068 /* flush any pending task for this netdev */
2069 flush_workqueue(mdev->workqueue);
2071 /* Detach the netdev so tasks would not attempt to access it */
2072 mutex_lock(&mdev->state_lock);
2073 mdev->pndev[priv->port] = NULL;
2074 mutex_unlock(&mdev->state_lock);
2076 mlx4_en_free_resources(priv);
2078 kfree(priv->tx_ring);
2084 static int mlx4_en_change_mtu(struct net_device *dev, int new_mtu)
2086 struct mlx4_en_priv *priv = netdev_priv(dev);
2087 struct mlx4_en_dev *mdev = priv->mdev;
2090 en_dbg(DRV, priv, "Change MTU called - current:%d new:%d\n",
2093 if ((new_mtu < MLX4_EN_MIN_MTU) || (new_mtu > priv->max_mtu)) {
2094 en_err(priv, "Bad MTU size:%d.\n", new_mtu);
2099 if (netif_running(dev)) {
2100 mutex_lock(&mdev->state_lock);
2101 if (!mdev->device_up) {
2102 /* NIC is probably restarting - let watchdog task reset
2104 en_dbg(DRV, priv, "Change MTU called with card down!?\n");
2106 mlx4_en_stop_port(dev, 1);
2107 err = mlx4_en_start_port(dev);
2109 en_err(priv, "Failed restarting port:%d\n",
2111 queue_work(mdev->workqueue, &priv->watchdog_task);
2114 mutex_unlock(&mdev->state_lock);
2119 static int mlx4_en_hwtstamp_set(struct net_device *dev, struct ifreq *ifr)
2121 struct mlx4_en_priv *priv = netdev_priv(dev);
2122 struct mlx4_en_dev *mdev = priv->mdev;
2123 struct hwtstamp_config config;
2125 if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
2128 /* reserved for future extensions */
2132 /* device doesn't support time stamping */
2133 if (!(mdev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_TS))
2136 /* TX HW timestamp */
2137 switch (config.tx_type) {
2138 case HWTSTAMP_TX_OFF:
2139 case HWTSTAMP_TX_ON:
2145 /* RX HW timestamp */
2146 switch (config.rx_filter) {
2147 case HWTSTAMP_FILTER_NONE:
2149 case HWTSTAMP_FILTER_ALL:
2150 case HWTSTAMP_FILTER_SOME:
2151 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
2152 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
2153 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
2154 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
2155 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
2156 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
2157 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
2158 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
2159 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
2160 case HWTSTAMP_FILTER_PTP_V2_EVENT:
2161 case HWTSTAMP_FILTER_PTP_V2_SYNC:
2162 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
2163 config.rx_filter = HWTSTAMP_FILTER_ALL;
2169 if (mlx4_en_timestamp_config(dev, config.tx_type, config.rx_filter)) {
2170 config.tx_type = HWTSTAMP_TX_OFF;
2171 config.rx_filter = HWTSTAMP_FILTER_NONE;
2174 return copy_to_user(ifr->ifr_data, &config,
2175 sizeof(config)) ? -EFAULT : 0;
2178 static int mlx4_en_hwtstamp_get(struct net_device *dev, struct ifreq *ifr)
2180 struct mlx4_en_priv *priv = netdev_priv(dev);
2182 return copy_to_user(ifr->ifr_data, &priv->hwtstamp_config,
2183 sizeof(priv->hwtstamp_config)) ? -EFAULT : 0;
2186 static int mlx4_en_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
2190 return mlx4_en_hwtstamp_set(dev, ifr);
2192 return mlx4_en_hwtstamp_get(dev, ifr);
2198 static int mlx4_en_set_features(struct net_device *netdev,
2199 netdev_features_t features)
2201 struct mlx4_en_priv *priv = netdev_priv(netdev);
2203 if (features & NETIF_F_LOOPBACK)
2204 priv->ctrl_flags |= cpu_to_be32(MLX4_WQE_CTRL_FORCE_LOOPBACK);
2207 cpu_to_be32(~MLX4_WQE_CTRL_FORCE_LOOPBACK);
2209 mlx4_en_update_loopback_state(netdev, features);
2215 static int mlx4_en_set_vf_mac(struct net_device *dev, int queue, u8 *mac)
2217 struct mlx4_en_priv *en_priv = netdev_priv(dev);
2218 struct mlx4_en_dev *mdev = en_priv->mdev;
2219 u64 mac_u64 = mlx4_en_mac_to_u64(mac);
2221 if (!is_valid_ether_addr(mac))
2224 return mlx4_set_vf_mac(mdev->dev, en_priv->port, queue, mac_u64);
2227 static int mlx4_en_set_vf_vlan(struct net_device *dev, int vf, u16 vlan, u8 qos)
2229 struct mlx4_en_priv *en_priv = netdev_priv(dev);
2230 struct mlx4_en_dev *mdev = en_priv->mdev;
2232 return mlx4_set_vf_vlan(mdev->dev, en_priv->port, vf, vlan, qos);
2235 static int mlx4_en_set_vf_spoofchk(struct net_device *dev, int vf, bool setting)
2237 struct mlx4_en_priv *en_priv = netdev_priv(dev);
2238 struct mlx4_en_dev *mdev = en_priv->mdev;
2240 return mlx4_set_vf_spoofchk(mdev->dev, en_priv->port, vf, setting);
2243 static int mlx4_en_get_vf_config(struct net_device *dev, int vf, struct ifla_vf_info *ivf)
2245 struct mlx4_en_priv *en_priv = netdev_priv(dev);
2246 struct mlx4_en_dev *mdev = en_priv->mdev;
2248 return mlx4_get_vf_config(mdev->dev, en_priv->port, vf, ivf);
2251 static int mlx4_en_set_vf_link_state(struct net_device *dev, int vf, int link_state)
2253 struct mlx4_en_priv *en_priv = netdev_priv(dev);
2254 struct mlx4_en_dev *mdev = en_priv->mdev;
2256 return mlx4_set_vf_link_state(mdev->dev, en_priv->port, vf, link_state);
2259 #define PORT_ID_BYTE_LEN 8
2260 static int mlx4_en_get_phys_port_id(struct net_device *dev,
2261 struct netdev_phys_port_id *ppid)
2263 struct mlx4_en_priv *priv = netdev_priv(dev);
2264 struct mlx4_dev *mdev = priv->mdev->dev;
2266 u64 phys_port_id = mdev->caps.phys_port_id[priv->port];
2271 ppid->id_len = sizeof(phys_port_id);
2272 for (i = PORT_ID_BYTE_LEN - 1; i >= 0; --i) {
2273 ppid->id[i] = phys_port_id & 0xff;
2279 static const struct net_device_ops mlx4_netdev_ops = {
2280 .ndo_open = mlx4_en_open,
2281 .ndo_stop = mlx4_en_close,
2282 .ndo_start_xmit = mlx4_en_xmit,
2283 .ndo_select_queue = mlx4_en_select_queue,
2284 .ndo_get_stats = mlx4_en_get_stats,
2285 .ndo_set_rx_mode = mlx4_en_set_rx_mode,
2286 .ndo_set_mac_address = mlx4_en_set_mac,
2287 .ndo_validate_addr = eth_validate_addr,
2288 .ndo_change_mtu = mlx4_en_change_mtu,
2289 .ndo_do_ioctl = mlx4_en_ioctl,
2290 .ndo_tx_timeout = mlx4_en_tx_timeout,
2291 .ndo_vlan_rx_add_vid = mlx4_en_vlan_rx_add_vid,
2292 .ndo_vlan_rx_kill_vid = mlx4_en_vlan_rx_kill_vid,
2293 #ifdef CONFIG_NET_POLL_CONTROLLER
2294 .ndo_poll_controller = mlx4_en_netpoll,
2296 .ndo_set_features = mlx4_en_set_features,
2297 .ndo_setup_tc = mlx4_en_setup_tc,
2298 #ifdef CONFIG_RFS_ACCEL
2299 .ndo_rx_flow_steer = mlx4_en_filter_rfs,
2301 #ifdef CONFIG_NET_RX_BUSY_POLL
2302 .ndo_busy_poll = mlx4_en_low_latency_recv,
2304 .ndo_get_phys_port_id = mlx4_en_get_phys_port_id,
2307 static const struct net_device_ops mlx4_netdev_ops_master = {
2308 .ndo_open = mlx4_en_open,
2309 .ndo_stop = mlx4_en_close,
2310 .ndo_start_xmit = mlx4_en_xmit,
2311 .ndo_select_queue = mlx4_en_select_queue,
2312 .ndo_get_stats = mlx4_en_get_stats,
2313 .ndo_set_rx_mode = mlx4_en_set_rx_mode,
2314 .ndo_set_mac_address = mlx4_en_set_mac,
2315 .ndo_validate_addr = eth_validate_addr,
2316 .ndo_change_mtu = mlx4_en_change_mtu,
2317 .ndo_tx_timeout = mlx4_en_tx_timeout,
2318 .ndo_vlan_rx_add_vid = mlx4_en_vlan_rx_add_vid,
2319 .ndo_vlan_rx_kill_vid = mlx4_en_vlan_rx_kill_vid,
2320 .ndo_set_vf_mac = mlx4_en_set_vf_mac,
2321 .ndo_set_vf_vlan = mlx4_en_set_vf_vlan,
2322 .ndo_set_vf_spoofchk = mlx4_en_set_vf_spoofchk,
2323 .ndo_set_vf_link_state = mlx4_en_set_vf_link_state,
2324 .ndo_get_vf_config = mlx4_en_get_vf_config,
2325 #ifdef CONFIG_NET_POLL_CONTROLLER
2326 .ndo_poll_controller = mlx4_en_netpoll,
2328 .ndo_set_features = mlx4_en_set_features,
2329 .ndo_setup_tc = mlx4_en_setup_tc,
2330 #ifdef CONFIG_RFS_ACCEL
2331 .ndo_rx_flow_steer = mlx4_en_filter_rfs,
2333 .ndo_get_phys_port_id = mlx4_en_get_phys_port_id,
2336 int mlx4_en_init_netdev(struct mlx4_en_dev *mdev, int port,
2337 struct mlx4_en_port_profile *prof)
2339 struct net_device *dev;
2340 struct mlx4_en_priv *priv;
2345 dev = alloc_etherdev_mqs(sizeof(struct mlx4_en_priv),
2346 MAX_TX_RINGS, MAX_RX_RINGS);
2350 netif_set_real_num_tx_queues(dev, prof->tx_ring_num);
2351 netif_set_real_num_rx_queues(dev, prof->rx_ring_num);
2353 SET_NETDEV_DEV(dev, &mdev->dev->pdev->dev);
2354 dev->dev_id = port - 1;
2357 * Initialize driver private data
2360 priv = netdev_priv(dev);
2361 memset(priv, 0, sizeof(struct mlx4_en_priv));
2364 priv->ddev = &mdev->pdev->dev;
2367 priv->port_up = false;
2368 priv->flags = prof->flags;
2369 priv->ctrl_flags = cpu_to_be32(MLX4_WQE_CTRL_CQ_UPDATE |
2370 MLX4_WQE_CTRL_SOLICITED);
2371 priv->num_tx_rings_p_up = mdev->profile.num_tx_rings_p_up;
2372 priv->tx_ring_num = prof->tx_ring_num;
2374 priv->tx_ring = kzalloc(sizeof(struct mlx4_en_tx_ring *) * MAX_TX_RINGS,
2376 if (!priv->tx_ring) {
2380 priv->tx_cq = kzalloc(sizeof(struct mlx4_en_cq *) * MAX_TX_RINGS,
2386 priv->rx_ring_num = prof->rx_ring_num;
2387 priv->cqe_factor = (mdev->dev->caps.cqe_size == 64) ? 1 : 0;
2388 priv->mac_index = -1;
2389 priv->msg_enable = MLX4_EN_MSG_LEVEL;
2390 spin_lock_init(&priv->stats_lock);
2391 INIT_WORK(&priv->rx_mode_task, mlx4_en_do_set_rx_mode);
2392 INIT_WORK(&priv->watchdog_task, mlx4_en_restart);
2393 INIT_WORK(&priv->linkstate_task, mlx4_en_linkstate);
2394 INIT_DELAYED_WORK(&priv->stats_task, mlx4_en_do_get_stats);
2395 INIT_DELAYED_WORK(&priv->service_task, mlx4_en_service_task);
2396 #ifdef CONFIG_MLX4_EN_DCB
2397 if (!mlx4_is_slave(priv->mdev->dev)) {
2398 if (mdev->dev->caps.flags & MLX4_DEV_CAP_FLAG_SET_ETH_SCHED) {
2399 dev->dcbnl_ops = &mlx4_en_dcbnl_ops;
2401 en_info(priv, "enabling only PFC DCB ops\n");
2402 dev->dcbnl_ops = &mlx4_en_dcbnl_pfc_ops;
2407 for (i = 0; i < MLX4_EN_MAC_HASH_SIZE; ++i)
2408 INIT_HLIST_HEAD(&priv->mac_hash[i]);
2410 /* Query for default mac and max mtu */
2411 priv->max_mtu = mdev->dev->caps.eth_mtu_cap[priv->port];
2413 /* Set default MAC */
2414 dev->addr_len = ETH_ALEN;
2415 mlx4_en_u64_to_mac(dev->dev_addr, mdev->dev->caps.def_mac[priv->port]);
2416 if (!is_valid_ether_addr(dev->dev_addr)) {
2417 if (mlx4_is_slave(priv->mdev->dev)) {
2418 eth_hw_addr_random(dev);
2419 en_warn(priv, "Assigned random MAC address %pM\n", dev->dev_addr);
2420 mac_u64 = mlx4_en_mac_to_u64(dev->dev_addr);
2421 mdev->dev->caps.def_mac[priv->port] = mac_u64;
2423 en_err(priv, "Port: %d, invalid mac burned: %pM, quiting\n",
2424 priv->port, dev->dev_addr);
2430 memcpy(priv->prev_mac, dev->dev_addr, sizeof(priv->prev_mac));
2432 priv->stride = roundup_pow_of_two(sizeof(struct mlx4_en_rx_desc) +
2433 DS_SIZE * MLX4_EN_MAX_RX_FRAGS);
2434 err = mlx4_en_alloc_resources(priv);
2438 #ifdef CONFIG_RFS_ACCEL
2439 INIT_LIST_HEAD(&priv->filters);
2440 spin_lock_init(&priv->filters_lock);
2443 /* Initialize time stamping config */
2444 priv->hwtstamp_config.flags = 0;
2445 priv->hwtstamp_config.tx_type = HWTSTAMP_TX_OFF;
2446 priv->hwtstamp_config.rx_filter = HWTSTAMP_FILTER_NONE;
2448 /* Allocate page for receive rings */
2449 err = mlx4_alloc_hwq_res(mdev->dev, &priv->res,
2450 MLX4_EN_PAGE_SIZE, MLX4_EN_PAGE_SIZE);
2452 en_err(priv, "Failed to allocate page for rx qps\n");
2455 priv->allocated = 1;
2458 * Initialize netdev entry points
2460 if (mlx4_is_master(priv->mdev->dev))
2461 dev->netdev_ops = &mlx4_netdev_ops_master;
2463 dev->netdev_ops = &mlx4_netdev_ops;
2464 dev->watchdog_timeo = MLX4_EN_WATCHDOG_TIMEOUT;
2465 netif_set_real_num_tx_queues(dev, priv->tx_ring_num);
2466 netif_set_real_num_rx_queues(dev, priv->rx_ring_num);
2468 SET_ETHTOOL_OPS(dev, &mlx4_en_ethtool_ops);
2471 * Set driver features
2473 dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM;
2474 if (mdev->LSO_support)
2475 dev->hw_features |= NETIF_F_TSO | NETIF_F_TSO6;
2477 dev->vlan_features = dev->hw_features;
2479 dev->hw_features |= NETIF_F_RXCSUM | NETIF_F_RXHASH;
2480 dev->features = dev->hw_features | NETIF_F_HIGHDMA |
2481 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX |
2482 NETIF_F_HW_VLAN_CTAG_FILTER;
2483 dev->hw_features |= NETIF_F_LOOPBACK;
2485 if (mdev->dev->caps.steering_mode ==
2486 MLX4_STEERING_MODE_DEVICE_MANAGED)
2487 dev->hw_features |= NETIF_F_NTUPLE;
2489 if (mdev->dev->caps.steering_mode != MLX4_STEERING_MODE_A0)
2490 dev->priv_flags |= IFF_UNICAST_FLT;
2492 if (mdev->dev->caps.tunnel_offload_mode == MLX4_TUNNEL_OFFLOAD_MODE_VXLAN) {
2493 dev->hw_enc_features |= NETIF_F_IP_CSUM | NETIF_F_RXCSUM |
2494 NETIF_F_TSO | NETIF_F_GSO_UDP_TUNNEL;
2495 dev->hw_features |= NETIF_F_GSO_UDP_TUNNEL;
2496 dev->features |= NETIF_F_GSO_UDP_TUNNEL;
2499 mdev->pndev[port] = dev;
2501 netif_carrier_off(dev);
2502 mlx4_en_set_default_moderation(priv);
2504 err = register_netdev(dev);
2506 en_err(priv, "Netdev registration failed for port %d\n", port);
2509 priv->registered = 1;
2511 en_warn(priv, "Using %d TX rings\n", prof->tx_ring_num);
2512 en_warn(priv, "Using %d RX rings\n", prof->rx_ring_num);
2514 mlx4_en_update_loopback_state(priv->dev, priv->dev->features);
2516 /* Configure port */
2517 mlx4_en_calc_rx_buf(dev);
2518 err = mlx4_SET_PORT_general(mdev->dev, priv->port,
2519 priv->rx_skb_size + ETH_FCS_LEN,
2520 prof->tx_pause, prof->tx_ppp,
2521 prof->rx_pause, prof->rx_ppp);
2523 en_err(priv, "Failed setting port general configurations "
2524 "for port %d, with error %d\n", priv->port, err);
2528 if (mdev->dev->caps.tunnel_offload_mode == MLX4_TUNNEL_OFFLOAD_MODE_VXLAN) {
2529 err = mlx4_SET_PORT_VXLAN(mdev->dev, priv->port, VXLAN_STEER_BY_OUTER_MAC);
2531 en_err(priv, "Failed setting port L2 tunnel configuration, err %d\n",
2538 en_warn(priv, "Initializing port\n");
2539 err = mlx4_INIT_PORT(mdev->dev, priv->port);
2541 en_err(priv, "Failed Initializing port\n");
2544 queue_delayed_work(mdev->workqueue, &priv->stats_task, STATS_DELAY);
2546 if (mdev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_TS)
2547 queue_delayed_work(mdev->workqueue, &priv->service_task,
2548 SERVICE_TASK_DELAY);
2553 mlx4_en_destroy_netdev(dev);