pvrusb2: reduce stack usage pvr2_eeprom_analyze()
[linux/fpc-iii.git] / drivers / net / ethernet / mellanox / mlx4 / en_netdev.c
blobd223e7cb68ba316630f53e0279c0a52953ca0701
1 /*
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
12 * conditions are met:
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer.
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
30 * SOFTWARE.
34 #include <linux/bpf.h>
35 #include <linux/etherdevice.h>
36 #include <linux/tcp.h>
37 #include <linux/if_vlan.h>
38 #include <linux/delay.h>
39 #include <linux/slab.h>
40 #include <linux/hash.h>
41 #include <net/ip.h>
42 #include <net/busy_poll.h>
43 #include <net/vxlan.h>
44 #include <net/devlink.h>
46 #include <linux/mlx4/driver.h>
47 #include <linux/mlx4/device.h>
48 #include <linux/mlx4/cmd.h>
49 #include <linux/mlx4/cq.h>
51 #include "mlx4_en.h"
52 #include "en_port.h"
54 int mlx4_en_setup_tc(struct net_device *dev, u8 up)
56 struct mlx4_en_priv *priv = netdev_priv(dev);
57 int i;
58 unsigned int offset = 0;
60 if (up && up != MLX4_EN_NUM_UP)
61 return -EINVAL;
63 netdev_set_num_tc(dev, up);
65 /* Partition Tx queues evenly amongst UP's */
66 for (i = 0; i < up; i++) {
67 netdev_set_tc_queue(dev, i, priv->num_tx_rings_p_up, offset);
68 offset += priv->num_tx_rings_p_up;
71 #ifdef CONFIG_MLX4_EN_DCB
72 if (!mlx4_is_slave(priv->mdev->dev)) {
73 if (up) {
74 if (priv->dcbx_cap)
75 priv->flags |= MLX4_EN_FLAG_DCB_ENABLED;
76 } else {
77 priv->flags &= ~MLX4_EN_FLAG_DCB_ENABLED;
78 priv->cee_config.pfc_state = false;
81 #endif /* CONFIG_MLX4_EN_DCB */
83 return 0;
86 static int __mlx4_en_setup_tc(struct net_device *dev, u32 handle, __be16 proto,
87 struct tc_to_netdev *tc)
89 if (tc->type != TC_SETUP_MQPRIO)
90 return -EINVAL;
92 return mlx4_en_setup_tc(dev, tc->tc);
95 #ifdef CONFIG_RFS_ACCEL
97 struct mlx4_en_filter {
98 struct list_head next;
99 struct work_struct work;
101 u8 ip_proto;
102 __be32 src_ip;
103 __be32 dst_ip;
104 __be16 src_port;
105 __be16 dst_port;
107 int rxq_index;
108 struct mlx4_en_priv *priv;
109 u32 flow_id; /* RFS infrastructure id */
110 int id; /* mlx4_en driver id */
111 u64 reg_id; /* Flow steering API id */
112 u8 activated; /* Used to prevent expiry before filter
113 * is attached
115 struct hlist_node filter_chain;
118 static void mlx4_en_filter_rfs_expire(struct mlx4_en_priv *priv);
120 static enum mlx4_net_trans_rule_id mlx4_ip_proto_to_trans_rule_id(u8 ip_proto)
122 switch (ip_proto) {
123 case IPPROTO_UDP:
124 return MLX4_NET_TRANS_RULE_ID_UDP;
125 case IPPROTO_TCP:
126 return MLX4_NET_TRANS_RULE_ID_TCP;
127 default:
128 return MLX4_NET_TRANS_RULE_NUM;
132 /* Must not acquire state_lock, as its corresponding work_sync
133 * is done under it.
135 static void mlx4_en_filter_work(struct work_struct *work)
137 struct mlx4_en_filter *filter = container_of(work,
138 struct mlx4_en_filter,
139 work);
140 struct mlx4_en_priv *priv = filter->priv;
141 struct mlx4_spec_list spec_tcp_udp = {
142 .id = mlx4_ip_proto_to_trans_rule_id(filter->ip_proto),
144 .tcp_udp = {
145 .dst_port = filter->dst_port,
146 .dst_port_msk = (__force __be16)-1,
147 .src_port = filter->src_port,
148 .src_port_msk = (__force __be16)-1,
152 struct mlx4_spec_list spec_ip = {
153 .id = MLX4_NET_TRANS_RULE_ID_IPV4,
155 .ipv4 = {
156 .dst_ip = filter->dst_ip,
157 .dst_ip_msk = (__force __be32)-1,
158 .src_ip = filter->src_ip,
159 .src_ip_msk = (__force __be32)-1,
163 struct mlx4_spec_list spec_eth = {
164 .id = MLX4_NET_TRANS_RULE_ID_ETH,
166 struct mlx4_net_trans_rule rule = {
167 .list = LIST_HEAD_INIT(rule.list),
168 .queue_mode = MLX4_NET_TRANS_Q_LIFO,
169 .exclusive = 1,
170 .allow_loopback = 1,
171 .promisc_mode = MLX4_FS_REGULAR,
172 .port = priv->port,
173 .priority = MLX4_DOMAIN_RFS,
175 int rc;
176 __be64 mac_mask = cpu_to_be64(MLX4_MAC_MASK << 16);
178 if (spec_tcp_udp.id >= MLX4_NET_TRANS_RULE_NUM) {
179 en_warn(priv, "RFS: ignoring unsupported ip protocol (%d)\n",
180 filter->ip_proto);
181 goto ignore;
183 list_add_tail(&spec_eth.list, &rule.list);
184 list_add_tail(&spec_ip.list, &rule.list);
185 list_add_tail(&spec_tcp_udp.list, &rule.list);
187 rule.qpn = priv->rss_map.qps[filter->rxq_index].qpn;
188 memcpy(spec_eth.eth.dst_mac, priv->dev->dev_addr, ETH_ALEN);
189 memcpy(spec_eth.eth.dst_mac_msk, &mac_mask, ETH_ALEN);
191 filter->activated = 0;
193 if (filter->reg_id) {
194 rc = mlx4_flow_detach(priv->mdev->dev, filter->reg_id);
195 if (rc && rc != -ENOENT)
196 en_err(priv, "Error detaching flow. rc = %d\n", rc);
199 rc = mlx4_flow_attach(priv->mdev->dev, &rule, &filter->reg_id);
200 if (rc)
201 en_err(priv, "Error attaching flow. err = %d\n", rc);
203 ignore:
204 mlx4_en_filter_rfs_expire(priv);
206 filter->activated = 1;
209 static inline struct hlist_head *
210 filter_hash_bucket(struct mlx4_en_priv *priv, __be32 src_ip, __be32 dst_ip,
211 __be16 src_port, __be16 dst_port)
213 unsigned long l;
214 int bucket_idx;
216 l = (__force unsigned long)src_port |
217 ((__force unsigned long)dst_port << 2);
218 l ^= (__force unsigned long)(src_ip ^ dst_ip);
220 bucket_idx = hash_long(l, MLX4_EN_FILTER_HASH_SHIFT);
222 return &priv->filter_hash[bucket_idx];
225 static struct mlx4_en_filter *
226 mlx4_en_filter_alloc(struct mlx4_en_priv *priv, int rxq_index, __be32 src_ip,
227 __be32 dst_ip, u8 ip_proto, __be16 src_port,
228 __be16 dst_port, u32 flow_id)
230 struct mlx4_en_filter *filter = NULL;
232 filter = kzalloc(sizeof(struct mlx4_en_filter), GFP_ATOMIC);
233 if (!filter)
234 return NULL;
236 filter->priv = priv;
237 filter->rxq_index = rxq_index;
238 INIT_WORK(&filter->work, mlx4_en_filter_work);
240 filter->src_ip = src_ip;
241 filter->dst_ip = dst_ip;
242 filter->ip_proto = ip_proto;
243 filter->src_port = src_port;
244 filter->dst_port = dst_port;
246 filter->flow_id = flow_id;
248 filter->id = priv->last_filter_id++ % RPS_NO_FILTER;
250 list_add_tail(&filter->next, &priv->filters);
251 hlist_add_head(&filter->filter_chain,
252 filter_hash_bucket(priv, src_ip, dst_ip, src_port,
253 dst_port));
255 return filter;
258 static void mlx4_en_filter_free(struct mlx4_en_filter *filter)
260 struct mlx4_en_priv *priv = filter->priv;
261 int rc;
263 list_del(&filter->next);
265 rc = mlx4_flow_detach(priv->mdev->dev, filter->reg_id);
266 if (rc && rc != -ENOENT)
267 en_err(priv, "Error detaching flow. rc = %d\n", rc);
269 kfree(filter);
272 static inline struct mlx4_en_filter *
273 mlx4_en_filter_find(struct mlx4_en_priv *priv, __be32 src_ip, __be32 dst_ip,
274 u8 ip_proto, __be16 src_port, __be16 dst_port)
276 struct mlx4_en_filter *filter;
277 struct mlx4_en_filter *ret = NULL;
279 hlist_for_each_entry(filter,
280 filter_hash_bucket(priv, src_ip, dst_ip,
281 src_port, dst_port),
282 filter_chain) {
283 if (filter->src_ip == src_ip &&
284 filter->dst_ip == dst_ip &&
285 filter->ip_proto == ip_proto &&
286 filter->src_port == src_port &&
287 filter->dst_port == dst_port) {
288 ret = filter;
289 break;
293 return ret;
296 static int
297 mlx4_en_filter_rfs(struct net_device *net_dev, const struct sk_buff *skb,
298 u16 rxq_index, u32 flow_id)
300 struct mlx4_en_priv *priv = netdev_priv(net_dev);
301 struct mlx4_en_filter *filter;
302 const struct iphdr *ip;
303 const __be16 *ports;
304 u8 ip_proto;
305 __be32 src_ip;
306 __be32 dst_ip;
307 __be16 src_port;
308 __be16 dst_port;
309 int nhoff = skb_network_offset(skb);
310 int ret = 0;
312 if (skb->protocol != htons(ETH_P_IP))
313 return -EPROTONOSUPPORT;
315 ip = (const struct iphdr *)(skb->data + nhoff);
316 if (ip_is_fragment(ip))
317 return -EPROTONOSUPPORT;
319 if ((ip->protocol != IPPROTO_TCP) && (ip->protocol != IPPROTO_UDP))
320 return -EPROTONOSUPPORT;
321 ports = (const __be16 *)(skb->data + nhoff + 4 * ip->ihl);
323 ip_proto = ip->protocol;
324 src_ip = ip->saddr;
325 dst_ip = ip->daddr;
326 src_port = ports[0];
327 dst_port = ports[1];
329 spin_lock_bh(&priv->filters_lock);
330 filter = mlx4_en_filter_find(priv, src_ip, dst_ip, ip_proto,
331 src_port, dst_port);
332 if (filter) {
333 if (filter->rxq_index == rxq_index)
334 goto out;
336 filter->rxq_index = rxq_index;
337 } else {
338 filter = mlx4_en_filter_alloc(priv, rxq_index,
339 src_ip, dst_ip, ip_proto,
340 src_port, dst_port, flow_id);
341 if (!filter) {
342 ret = -ENOMEM;
343 goto err;
347 queue_work(priv->mdev->workqueue, &filter->work);
349 out:
350 ret = filter->id;
351 err:
352 spin_unlock_bh(&priv->filters_lock);
354 return ret;
357 void mlx4_en_cleanup_filters(struct mlx4_en_priv *priv)
359 struct mlx4_en_filter *filter, *tmp;
360 LIST_HEAD(del_list);
362 spin_lock_bh(&priv->filters_lock);
363 list_for_each_entry_safe(filter, tmp, &priv->filters, next) {
364 list_move(&filter->next, &del_list);
365 hlist_del(&filter->filter_chain);
367 spin_unlock_bh(&priv->filters_lock);
369 list_for_each_entry_safe(filter, tmp, &del_list, next) {
370 cancel_work_sync(&filter->work);
371 mlx4_en_filter_free(filter);
375 static void mlx4_en_filter_rfs_expire(struct mlx4_en_priv *priv)
377 struct mlx4_en_filter *filter = NULL, *tmp, *last_filter = NULL;
378 LIST_HEAD(del_list);
379 int i = 0;
381 spin_lock_bh(&priv->filters_lock);
382 list_for_each_entry_safe(filter, tmp, &priv->filters, next) {
383 if (i > MLX4_EN_FILTER_EXPIRY_QUOTA)
384 break;
386 if (filter->activated &&
387 !work_pending(&filter->work) &&
388 rps_may_expire_flow(priv->dev,
389 filter->rxq_index, filter->flow_id,
390 filter->id)) {
391 list_move(&filter->next, &del_list);
392 hlist_del(&filter->filter_chain);
393 } else
394 last_filter = filter;
396 i++;
399 if (last_filter && (&last_filter->next != priv->filters.next))
400 list_move(&priv->filters, &last_filter->next);
402 spin_unlock_bh(&priv->filters_lock);
404 list_for_each_entry_safe(filter, tmp, &del_list, next)
405 mlx4_en_filter_free(filter);
407 #endif
409 static int mlx4_en_vlan_rx_add_vid(struct net_device *dev,
410 __be16 proto, u16 vid)
412 struct mlx4_en_priv *priv = netdev_priv(dev);
413 struct mlx4_en_dev *mdev = priv->mdev;
414 int err;
415 int idx;
417 en_dbg(HW, priv, "adding VLAN:%d\n", vid);
419 set_bit(vid, priv->active_vlans);
421 /* Add VID to port VLAN filter */
422 mutex_lock(&mdev->state_lock);
423 if (mdev->device_up && priv->port_up) {
424 err = mlx4_SET_VLAN_FLTR(mdev->dev, priv);
425 if (err) {
426 en_err(priv, "Failed configuring VLAN filter\n");
427 goto out;
430 err = mlx4_register_vlan(mdev->dev, priv->port, vid, &idx);
431 if (err)
432 en_dbg(HW, priv, "Failed adding vlan %d\n", vid);
434 out:
435 mutex_unlock(&mdev->state_lock);
436 return err;
439 static int mlx4_en_vlan_rx_kill_vid(struct net_device *dev,
440 __be16 proto, u16 vid)
442 struct mlx4_en_priv *priv = netdev_priv(dev);
443 struct mlx4_en_dev *mdev = priv->mdev;
444 int err = 0;
446 en_dbg(HW, priv, "Killing VID:%d\n", vid);
448 clear_bit(vid, priv->active_vlans);
450 /* Remove VID from port VLAN filter */
451 mutex_lock(&mdev->state_lock);
452 mlx4_unregister_vlan(mdev->dev, priv->port, vid);
454 if (mdev->device_up && priv->port_up) {
455 err = mlx4_SET_VLAN_FLTR(mdev->dev, priv);
456 if (err)
457 en_err(priv, "Failed configuring VLAN filter\n");
459 mutex_unlock(&mdev->state_lock);
461 return err;
464 static void mlx4_en_u64_to_mac(unsigned char dst_mac[ETH_ALEN + 2], u64 src_mac)
466 int i;
467 for (i = ETH_ALEN - 1; i >= 0; --i) {
468 dst_mac[i] = src_mac & 0xff;
469 src_mac >>= 8;
471 memset(&dst_mac[ETH_ALEN], 0, 2);
475 static int mlx4_en_tunnel_steer_add(struct mlx4_en_priv *priv, unsigned char *addr,
476 int qpn, u64 *reg_id)
478 int err;
480 if (priv->mdev->dev->caps.tunnel_offload_mode != MLX4_TUNNEL_OFFLOAD_MODE_VXLAN ||
481 priv->mdev->dev->caps.dmfs_high_steer_mode == MLX4_STEERING_DMFS_A0_STATIC)
482 return 0; /* do nothing */
484 err = mlx4_tunnel_steer_add(priv->mdev->dev, addr, priv->port, qpn,
485 MLX4_DOMAIN_NIC, reg_id);
486 if (err) {
487 en_err(priv, "failed to add vxlan steering rule, err %d\n", err);
488 return err;
490 en_dbg(DRV, priv, "added vxlan steering rule, mac %pM reg_id %llx\n", addr, *reg_id);
491 return 0;
495 static int mlx4_en_uc_steer_add(struct mlx4_en_priv *priv,
496 unsigned char *mac, int *qpn, u64 *reg_id)
498 struct mlx4_en_dev *mdev = priv->mdev;
499 struct mlx4_dev *dev = mdev->dev;
500 int err;
502 switch (dev->caps.steering_mode) {
503 case MLX4_STEERING_MODE_B0: {
504 struct mlx4_qp qp;
505 u8 gid[16] = {0};
507 qp.qpn = *qpn;
508 memcpy(&gid[10], mac, ETH_ALEN);
509 gid[5] = priv->port;
511 err = mlx4_unicast_attach(dev, &qp, gid, 0, MLX4_PROT_ETH);
512 break;
514 case MLX4_STEERING_MODE_DEVICE_MANAGED: {
515 struct mlx4_spec_list spec_eth = { {NULL} };
516 __be64 mac_mask = cpu_to_be64(MLX4_MAC_MASK << 16);
518 struct mlx4_net_trans_rule rule = {
519 .queue_mode = MLX4_NET_TRANS_Q_FIFO,
520 .exclusive = 0,
521 .allow_loopback = 1,
522 .promisc_mode = MLX4_FS_REGULAR,
523 .priority = MLX4_DOMAIN_NIC,
526 rule.port = priv->port;
527 rule.qpn = *qpn;
528 INIT_LIST_HEAD(&rule.list);
530 spec_eth.id = MLX4_NET_TRANS_RULE_ID_ETH;
531 memcpy(spec_eth.eth.dst_mac, mac, ETH_ALEN);
532 memcpy(spec_eth.eth.dst_mac_msk, &mac_mask, ETH_ALEN);
533 list_add_tail(&spec_eth.list, &rule.list);
535 err = mlx4_flow_attach(dev, &rule, reg_id);
536 break;
538 default:
539 return -EINVAL;
541 if (err)
542 en_warn(priv, "Failed Attaching Unicast\n");
544 return err;
547 static void mlx4_en_uc_steer_release(struct mlx4_en_priv *priv,
548 unsigned char *mac, int qpn, u64 reg_id)
550 struct mlx4_en_dev *mdev = priv->mdev;
551 struct mlx4_dev *dev = mdev->dev;
553 switch (dev->caps.steering_mode) {
554 case MLX4_STEERING_MODE_B0: {
555 struct mlx4_qp qp;
556 u8 gid[16] = {0};
558 qp.qpn = qpn;
559 memcpy(&gid[10], mac, ETH_ALEN);
560 gid[5] = priv->port;
562 mlx4_unicast_detach(dev, &qp, gid, MLX4_PROT_ETH);
563 break;
565 case MLX4_STEERING_MODE_DEVICE_MANAGED: {
566 mlx4_flow_detach(dev, reg_id);
567 break;
569 default:
570 en_err(priv, "Invalid steering mode.\n");
574 static int mlx4_en_get_qp(struct mlx4_en_priv *priv)
576 struct mlx4_en_dev *mdev = priv->mdev;
577 struct mlx4_dev *dev = mdev->dev;
578 int index = 0;
579 int err = 0;
580 int *qpn = &priv->base_qpn;
581 u64 mac = mlx4_mac_to_u64(priv->dev->dev_addr);
583 en_dbg(DRV, priv, "Registering MAC: %pM for adding\n",
584 priv->dev->dev_addr);
585 index = mlx4_register_mac(dev, priv->port, mac);
586 if (index < 0) {
587 err = index;
588 en_err(priv, "Failed adding MAC: %pM\n",
589 priv->dev->dev_addr);
590 return err;
593 if (dev->caps.steering_mode == MLX4_STEERING_MODE_A0) {
594 int base_qpn = mlx4_get_base_qpn(dev, priv->port);
595 *qpn = base_qpn + index;
596 return 0;
599 err = mlx4_qp_reserve_range(dev, 1, 1, qpn, MLX4_RESERVE_A0_QP);
600 en_dbg(DRV, priv, "Reserved qp %d\n", *qpn);
601 if (err) {
602 en_err(priv, "Failed to reserve qp for mac registration\n");
603 mlx4_unregister_mac(dev, priv->port, mac);
604 return err;
607 return 0;
610 static void mlx4_en_put_qp(struct mlx4_en_priv *priv)
612 struct mlx4_en_dev *mdev = priv->mdev;
613 struct mlx4_dev *dev = mdev->dev;
614 int qpn = priv->base_qpn;
616 if (dev->caps.steering_mode == MLX4_STEERING_MODE_A0) {
617 u64 mac = mlx4_mac_to_u64(priv->dev->dev_addr);
618 en_dbg(DRV, priv, "Registering MAC: %pM for deleting\n",
619 priv->dev->dev_addr);
620 mlx4_unregister_mac(dev, priv->port, mac);
621 } else {
622 en_dbg(DRV, priv, "Releasing qp: port %d, qpn %d\n",
623 priv->port, qpn);
624 mlx4_qp_release_range(dev, qpn, 1);
625 priv->flags &= ~MLX4_EN_FLAG_FORCE_PROMISC;
629 static int mlx4_en_replace_mac(struct mlx4_en_priv *priv, int qpn,
630 unsigned char *new_mac, unsigned char *prev_mac)
632 struct mlx4_en_dev *mdev = priv->mdev;
633 struct mlx4_dev *dev = mdev->dev;
634 int err = 0;
635 u64 new_mac_u64 = mlx4_mac_to_u64(new_mac);
637 if (dev->caps.steering_mode != MLX4_STEERING_MODE_A0) {
638 struct hlist_head *bucket;
639 unsigned int mac_hash;
640 struct mlx4_mac_entry *entry;
641 struct hlist_node *tmp;
642 u64 prev_mac_u64 = mlx4_mac_to_u64(prev_mac);
644 bucket = &priv->mac_hash[prev_mac[MLX4_EN_MAC_HASH_IDX]];
645 hlist_for_each_entry_safe(entry, tmp, bucket, hlist) {
646 if (ether_addr_equal_64bits(entry->mac, prev_mac)) {
647 mlx4_en_uc_steer_release(priv, entry->mac,
648 qpn, entry->reg_id);
649 mlx4_unregister_mac(dev, priv->port,
650 prev_mac_u64);
651 hlist_del_rcu(&entry->hlist);
652 synchronize_rcu();
653 memcpy(entry->mac, new_mac, ETH_ALEN);
654 entry->reg_id = 0;
655 mac_hash = new_mac[MLX4_EN_MAC_HASH_IDX];
656 hlist_add_head_rcu(&entry->hlist,
657 &priv->mac_hash[mac_hash]);
658 mlx4_register_mac(dev, priv->port, new_mac_u64);
659 err = mlx4_en_uc_steer_add(priv, new_mac,
660 &qpn,
661 &entry->reg_id);
662 if (err)
663 return err;
664 if (priv->tunnel_reg_id) {
665 mlx4_flow_detach(priv->mdev->dev, priv->tunnel_reg_id);
666 priv->tunnel_reg_id = 0;
668 err = mlx4_en_tunnel_steer_add(priv, new_mac, qpn,
669 &priv->tunnel_reg_id);
670 return err;
673 return -EINVAL;
676 return __mlx4_replace_mac(dev, priv->port, qpn, new_mac_u64);
679 static int mlx4_en_do_set_mac(struct mlx4_en_priv *priv,
680 unsigned char new_mac[ETH_ALEN + 2])
682 int err = 0;
684 if (priv->port_up) {
685 /* Remove old MAC and insert the new one */
686 err = mlx4_en_replace_mac(priv, priv->base_qpn,
687 new_mac, priv->current_mac);
688 if (err)
689 en_err(priv, "Failed changing HW MAC address\n");
690 } else
691 en_dbg(HW, priv, "Port is down while registering mac, exiting...\n");
693 if (!err)
694 memcpy(priv->current_mac, new_mac, sizeof(priv->current_mac));
696 return err;
699 static int mlx4_en_set_mac(struct net_device *dev, void *addr)
701 struct mlx4_en_priv *priv = netdev_priv(dev);
702 struct mlx4_en_dev *mdev = priv->mdev;
703 struct sockaddr *saddr = addr;
704 unsigned char new_mac[ETH_ALEN + 2];
705 int err;
707 if (!is_valid_ether_addr(saddr->sa_data))
708 return -EADDRNOTAVAIL;
710 mutex_lock(&mdev->state_lock);
711 memcpy(new_mac, saddr->sa_data, ETH_ALEN);
712 err = mlx4_en_do_set_mac(priv, new_mac);
713 if (!err)
714 memcpy(dev->dev_addr, saddr->sa_data, ETH_ALEN);
715 mutex_unlock(&mdev->state_lock);
717 return err;
720 static void mlx4_en_clear_list(struct net_device *dev)
722 struct mlx4_en_priv *priv = netdev_priv(dev);
723 struct mlx4_en_mc_list *tmp, *mc_to_del;
725 list_for_each_entry_safe(mc_to_del, tmp, &priv->mc_list, list) {
726 list_del(&mc_to_del->list);
727 kfree(mc_to_del);
731 static void mlx4_en_cache_mclist(struct net_device *dev)
733 struct mlx4_en_priv *priv = netdev_priv(dev);
734 struct netdev_hw_addr *ha;
735 struct mlx4_en_mc_list *tmp;
737 mlx4_en_clear_list(dev);
738 netdev_for_each_mc_addr(ha, dev) {
739 tmp = kzalloc(sizeof(struct mlx4_en_mc_list), GFP_ATOMIC);
740 if (!tmp) {
741 mlx4_en_clear_list(dev);
742 return;
744 memcpy(tmp->addr, ha->addr, ETH_ALEN);
745 list_add_tail(&tmp->list, &priv->mc_list);
749 static void update_mclist_flags(struct mlx4_en_priv *priv,
750 struct list_head *dst,
751 struct list_head *src)
753 struct mlx4_en_mc_list *dst_tmp, *src_tmp, *new_mc;
754 bool found;
756 /* Find all the entries that should be removed from dst,
757 * These are the entries that are not found in src
759 list_for_each_entry(dst_tmp, dst, list) {
760 found = false;
761 list_for_each_entry(src_tmp, src, list) {
762 if (ether_addr_equal(dst_tmp->addr, src_tmp->addr)) {
763 found = true;
764 break;
767 if (!found)
768 dst_tmp->action = MCLIST_REM;
771 /* Add entries that exist in src but not in dst
772 * mark them as need to add
774 list_for_each_entry(src_tmp, src, list) {
775 found = false;
776 list_for_each_entry(dst_tmp, dst, list) {
777 if (ether_addr_equal(dst_tmp->addr, src_tmp->addr)) {
778 dst_tmp->action = MCLIST_NONE;
779 found = true;
780 break;
783 if (!found) {
784 new_mc = kmemdup(src_tmp,
785 sizeof(struct mlx4_en_mc_list),
786 GFP_KERNEL);
787 if (!new_mc)
788 return;
790 new_mc->action = MCLIST_ADD;
791 list_add_tail(&new_mc->list, dst);
796 static void mlx4_en_set_rx_mode(struct net_device *dev)
798 struct mlx4_en_priv *priv = netdev_priv(dev);
800 if (!priv->port_up)
801 return;
803 queue_work(priv->mdev->workqueue, &priv->rx_mode_task);
806 static void mlx4_en_set_promisc_mode(struct mlx4_en_priv *priv,
807 struct mlx4_en_dev *mdev)
809 int err = 0;
811 if (!(priv->flags & MLX4_EN_FLAG_PROMISC)) {
812 if (netif_msg_rx_status(priv))
813 en_warn(priv, "Entering promiscuous mode\n");
814 priv->flags |= MLX4_EN_FLAG_PROMISC;
816 /* Enable promiscouos mode */
817 switch (mdev->dev->caps.steering_mode) {
818 case MLX4_STEERING_MODE_DEVICE_MANAGED:
819 err = mlx4_flow_steer_promisc_add(mdev->dev,
820 priv->port,
821 priv->base_qpn,
822 MLX4_FS_ALL_DEFAULT);
823 if (err)
824 en_err(priv, "Failed enabling promiscuous mode\n");
825 priv->flags |= MLX4_EN_FLAG_MC_PROMISC;
826 break;
828 case MLX4_STEERING_MODE_B0:
829 err = mlx4_unicast_promisc_add(mdev->dev,
830 priv->base_qpn,
831 priv->port);
832 if (err)
833 en_err(priv, "Failed enabling unicast promiscuous mode\n");
835 /* Add the default qp number as multicast
836 * promisc
838 if (!(priv->flags & MLX4_EN_FLAG_MC_PROMISC)) {
839 err = mlx4_multicast_promisc_add(mdev->dev,
840 priv->base_qpn,
841 priv->port);
842 if (err)
843 en_err(priv, "Failed enabling multicast promiscuous mode\n");
844 priv->flags |= MLX4_EN_FLAG_MC_PROMISC;
846 break;
848 case MLX4_STEERING_MODE_A0:
849 err = mlx4_SET_PORT_qpn_calc(mdev->dev,
850 priv->port,
851 priv->base_qpn,
853 if (err)
854 en_err(priv, "Failed enabling promiscuous mode\n");
855 break;
858 /* Disable port multicast filter (unconditionally) */
859 err = mlx4_SET_MCAST_FLTR(mdev->dev, priv->port, 0,
860 0, MLX4_MCAST_DISABLE);
861 if (err)
862 en_err(priv, "Failed disabling multicast filter\n");
866 static void mlx4_en_clear_promisc_mode(struct mlx4_en_priv *priv,
867 struct mlx4_en_dev *mdev)
869 int err = 0;
871 if (netif_msg_rx_status(priv))
872 en_warn(priv, "Leaving promiscuous mode\n");
873 priv->flags &= ~MLX4_EN_FLAG_PROMISC;
875 /* Disable promiscouos mode */
876 switch (mdev->dev->caps.steering_mode) {
877 case MLX4_STEERING_MODE_DEVICE_MANAGED:
878 err = mlx4_flow_steer_promisc_remove(mdev->dev,
879 priv->port,
880 MLX4_FS_ALL_DEFAULT);
881 if (err)
882 en_err(priv, "Failed disabling promiscuous mode\n");
883 priv->flags &= ~MLX4_EN_FLAG_MC_PROMISC;
884 break;
886 case MLX4_STEERING_MODE_B0:
887 err = mlx4_unicast_promisc_remove(mdev->dev,
888 priv->base_qpn,
889 priv->port);
890 if (err)
891 en_err(priv, "Failed disabling unicast promiscuous mode\n");
892 /* Disable Multicast promisc */
893 if (priv->flags & MLX4_EN_FLAG_MC_PROMISC) {
894 err = mlx4_multicast_promisc_remove(mdev->dev,
895 priv->base_qpn,
896 priv->port);
897 if (err)
898 en_err(priv, "Failed disabling multicast promiscuous mode\n");
899 priv->flags &= ~MLX4_EN_FLAG_MC_PROMISC;
901 break;
903 case MLX4_STEERING_MODE_A0:
904 err = mlx4_SET_PORT_qpn_calc(mdev->dev,
905 priv->port,
906 priv->base_qpn, 0);
907 if (err)
908 en_err(priv, "Failed disabling promiscuous mode\n");
909 break;
913 static void mlx4_en_do_multicast(struct mlx4_en_priv *priv,
914 struct net_device *dev,
915 struct mlx4_en_dev *mdev)
917 struct mlx4_en_mc_list *mclist, *tmp;
918 u64 mcast_addr = 0;
919 u8 mc_list[16] = {0};
920 int err = 0;
922 /* Enable/disable the multicast filter according to IFF_ALLMULTI */
923 if (dev->flags & IFF_ALLMULTI) {
924 err = mlx4_SET_MCAST_FLTR(mdev->dev, priv->port, 0,
925 0, MLX4_MCAST_DISABLE);
926 if (err)
927 en_err(priv, "Failed disabling multicast filter\n");
929 /* Add the default qp number as multicast promisc */
930 if (!(priv->flags & MLX4_EN_FLAG_MC_PROMISC)) {
931 switch (mdev->dev->caps.steering_mode) {
932 case MLX4_STEERING_MODE_DEVICE_MANAGED:
933 err = mlx4_flow_steer_promisc_add(mdev->dev,
934 priv->port,
935 priv->base_qpn,
936 MLX4_FS_MC_DEFAULT);
937 break;
939 case MLX4_STEERING_MODE_B0:
940 err = mlx4_multicast_promisc_add(mdev->dev,
941 priv->base_qpn,
942 priv->port);
943 break;
945 case MLX4_STEERING_MODE_A0:
946 break;
948 if (err)
949 en_err(priv, "Failed entering multicast promisc mode\n");
950 priv->flags |= MLX4_EN_FLAG_MC_PROMISC;
952 } else {
953 /* Disable Multicast promisc */
954 if (priv->flags & MLX4_EN_FLAG_MC_PROMISC) {
955 switch (mdev->dev->caps.steering_mode) {
956 case MLX4_STEERING_MODE_DEVICE_MANAGED:
957 err = mlx4_flow_steer_promisc_remove(mdev->dev,
958 priv->port,
959 MLX4_FS_MC_DEFAULT);
960 break;
962 case MLX4_STEERING_MODE_B0:
963 err = mlx4_multicast_promisc_remove(mdev->dev,
964 priv->base_qpn,
965 priv->port);
966 break;
968 case MLX4_STEERING_MODE_A0:
969 break;
971 if (err)
972 en_err(priv, "Failed disabling multicast promiscuous mode\n");
973 priv->flags &= ~MLX4_EN_FLAG_MC_PROMISC;
976 err = mlx4_SET_MCAST_FLTR(mdev->dev, priv->port, 0,
977 0, MLX4_MCAST_DISABLE);
978 if (err)
979 en_err(priv, "Failed disabling multicast filter\n");
981 /* Flush mcast filter and init it with broadcast address */
982 mlx4_SET_MCAST_FLTR(mdev->dev, priv->port, ETH_BCAST,
983 1, MLX4_MCAST_CONFIG);
985 /* Update multicast list - we cache all addresses so they won't
986 * change while HW is updated holding the command semaphor */
987 netif_addr_lock_bh(dev);
988 mlx4_en_cache_mclist(dev);
989 netif_addr_unlock_bh(dev);
990 list_for_each_entry(mclist, &priv->mc_list, list) {
991 mcast_addr = mlx4_mac_to_u64(mclist->addr);
992 mlx4_SET_MCAST_FLTR(mdev->dev, priv->port,
993 mcast_addr, 0, MLX4_MCAST_CONFIG);
995 err = mlx4_SET_MCAST_FLTR(mdev->dev, priv->port, 0,
996 0, MLX4_MCAST_ENABLE);
997 if (err)
998 en_err(priv, "Failed enabling multicast filter\n");
1000 update_mclist_flags(priv, &priv->curr_list, &priv->mc_list);
1001 list_for_each_entry_safe(mclist, tmp, &priv->curr_list, list) {
1002 if (mclist->action == MCLIST_REM) {
1003 /* detach this address and delete from list */
1004 memcpy(&mc_list[10], mclist->addr, ETH_ALEN);
1005 mc_list[5] = priv->port;
1006 err = mlx4_multicast_detach(mdev->dev,
1007 &priv->rss_map.indir_qp,
1008 mc_list,
1009 MLX4_PROT_ETH,
1010 mclist->reg_id);
1011 if (err)
1012 en_err(priv, "Fail to detach multicast address\n");
1014 if (mclist->tunnel_reg_id) {
1015 err = mlx4_flow_detach(priv->mdev->dev, mclist->tunnel_reg_id);
1016 if (err)
1017 en_err(priv, "Failed to detach multicast address\n");
1020 /* remove from list */
1021 list_del(&mclist->list);
1022 kfree(mclist);
1023 } else if (mclist->action == MCLIST_ADD) {
1024 /* attach the address */
1025 memcpy(&mc_list[10], mclist->addr, ETH_ALEN);
1026 /* needed for B0 steering support */
1027 mc_list[5] = priv->port;
1028 err = mlx4_multicast_attach(mdev->dev,
1029 &priv->rss_map.indir_qp,
1030 mc_list,
1031 priv->port, 0,
1032 MLX4_PROT_ETH,
1033 &mclist->reg_id);
1034 if (err)
1035 en_err(priv, "Fail to attach multicast address\n");
1037 err = mlx4_en_tunnel_steer_add(priv, &mc_list[10], priv->base_qpn,
1038 &mclist->tunnel_reg_id);
1039 if (err)
1040 en_err(priv, "Failed to attach multicast address\n");
1046 static void mlx4_en_do_uc_filter(struct mlx4_en_priv *priv,
1047 struct net_device *dev,
1048 struct mlx4_en_dev *mdev)
1050 struct netdev_hw_addr *ha;
1051 struct mlx4_mac_entry *entry;
1052 struct hlist_node *tmp;
1053 bool found;
1054 u64 mac;
1055 int err = 0;
1056 struct hlist_head *bucket;
1057 unsigned int i;
1058 int removed = 0;
1059 u32 prev_flags;
1061 /* Note that we do not need to protect our mac_hash traversal with rcu,
1062 * since all modification code is protected by mdev->state_lock
1065 /* find what to remove */
1066 for (i = 0; i < MLX4_EN_MAC_HASH_SIZE; ++i) {
1067 bucket = &priv->mac_hash[i];
1068 hlist_for_each_entry_safe(entry, tmp, bucket, hlist) {
1069 found = false;
1070 netdev_for_each_uc_addr(ha, dev) {
1071 if (ether_addr_equal_64bits(entry->mac,
1072 ha->addr)) {
1073 found = true;
1074 break;
1078 /* MAC address of the port is not in uc list */
1079 if (ether_addr_equal_64bits(entry->mac,
1080 priv->current_mac))
1081 found = true;
1083 if (!found) {
1084 mac = mlx4_mac_to_u64(entry->mac);
1085 mlx4_en_uc_steer_release(priv, entry->mac,
1086 priv->base_qpn,
1087 entry->reg_id);
1088 mlx4_unregister_mac(mdev->dev, priv->port, mac);
1090 hlist_del_rcu(&entry->hlist);
1091 kfree_rcu(entry, rcu);
1092 en_dbg(DRV, priv, "Removed MAC %pM on port:%d\n",
1093 entry->mac, priv->port);
1094 ++removed;
1099 /* if we didn't remove anything, there is no use in trying to add
1100 * again once we are in a forced promisc mode state
1102 if ((priv->flags & MLX4_EN_FLAG_FORCE_PROMISC) && 0 == removed)
1103 return;
1105 prev_flags = priv->flags;
1106 priv->flags &= ~MLX4_EN_FLAG_FORCE_PROMISC;
1108 /* find what to add */
1109 netdev_for_each_uc_addr(ha, dev) {
1110 found = false;
1111 bucket = &priv->mac_hash[ha->addr[MLX4_EN_MAC_HASH_IDX]];
1112 hlist_for_each_entry(entry, bucket, hlist) {
1113 if (ether_addr_equal_64bits(entry->mac, ha->addr)) {
1114 found = true;
1115 break;
1119 if (!found) {
1120 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1121 if (!entry) {
1122 en_err(priv, "Failed adding MAC %pM on port:%d (out of memory)\n",
1123 ha->addr, priv->port);
1124 priv->flags |= MLX4_EN_FLAG_FORCE_PROMISC;
1125 break;
1127 mac = mlx4_mac_to_u64(ha->addr);
1128 memcpy(entry->mac, ha->addr, ETH_ALEN);
1129 err = mlx4_register_mac(mdev->dev, priv->port, mac);
1130 if (err < 0) {
1131 en_err(priv, "Failed registering MAC %pM on port %d: %d\n",
1132 ha->addr, priv->port, err);
1133 kfree(entry);
1134 priv->flags |= MLX4_EN_FLAG_FORCE_PROMISC;
1135 break;
1137 err = mlx4_en_uc_steer_add(priv, ha->addr,
1138 &priv->base_qpn,
1139 &entry->reg_id);
1140 if (err) {
1141 en_err(priv, "Failed adding MAC %pM on port %d: %d\n",
1142 ha->addr, priv->port, err);
1143 mlx4_unregister_mac(mdev->dev, priv->port, mac);
1144 kfree(entry);
1145 priv->flags |= MLX4_EN_FLAG_FORCE_PROMISC;
1146 break;
1147 } else {
1148 unsigned int mac_hash;
1149 en_dbg(DRV, priv, "Added MAC %pM on port:%d\n",
1150 ha->addr, priv->port);
1151 mac_hash = ha->addr[MLX4_EN_MAC_HASH_IDX];
1152 bucket = &priv->mac_hash[mac_hash];
1153 hlist_add_head_rcu(&entry->hlist, bucket);
1158 if (priv->flags & MLX4_EN_FLAG_FORCE_PROMISC) {
1159 en_warn(priv, "Forcing promiscuous mode on port:%d\n",
1160 priv->port);
1161 } else if (prev_flags & MLX4_EN_FLAG_FORCE_PROMISC) {
1162 en_warn(priv, "Stop forcing promiscuous mode on port:%d\n",
1163 priv->port);
1167 static void mlx4_en_do_set_rx_mode(struct work_struct *work)
1169 struct mlx4_en_priv *priv = container_of(work, struct mlx4_en_priv,
1170 rx_mode_task);
1171 struct mlx4_en_dev *mdev = priv->mdev;
1172 struct net_device *dev = priv->dev;
1174 mutex_lock(&mdev->state_lock);
1175 if (!mdev->device_up) {
1176 en_dbg(HW, priv, "Card is not up, ignoring rx mode change.\n");
1177 goto out;
1179 if (!priv->port_up) {
1180 en_dbg(HW, priv, "Port is down, ignoring rx mode change.\n");
1181 goto out;
1184 if (!netif_carrier_ok(dev)) {
1185 if (!mlx4_en_QUERY_PORT(mdev, priv->port)) {
1186 if (priv->port_state.link_state) {
1187 priv->last_link_state = MLX4_DEV_EVENT_PORT_UP;
1188 netif_carrier_on(dev);
1189 en_dbg(LINK, priv, "Link Up\n");
1194 if (dev->priv_flags & IFF_UNICAST_FLT)
1195 mlx4_en_do_uc_filter(priv, dev, mdev);
1197 /* Promsicuous mode: disable all filters */
1198 if ((dev->flags & IFF_PROMISC) ||
1199 (priv->flags & MLX4_EN_FLAG_FORCE_PROMISC)) {
1200 mlx4_en_set_promisc_mode(priv, mdev);
1201 goto out;
1204 /* Not in promiscuous mode */
1205 if (priv->flags & MLX4_EN_FLAG_PROMISC)
1206 mlx4_en_clear_promisc_mode(priv, mdev);
1208 mlx4_en_do_multicast(priv, dev, mdev);
1209 out:
1210 mutex_unlock(&mdev->state_lock);
1213 #ifdef CONFIG_NET_POLL_CONTROLLER
1214 static void mlx4_en_netpoll(struct net_device *dev)
1216 struct mlx4_en_priv *priv = netdev_priv(dev);
1217 struct mlx4_en_cq *cq;
1218 int i;
1220 for (i = 0; i < priv->tx_ring_num; i++) {
1221 cq = priv->tx_cq[i];
1222 napi_schedule(&cq->napi);
1225 #endif
1227 static int mlx4_en_set_rss_steer_rules(struct mlx4_en_priv *priv)
1229 u64 reg_id;
1230 int err = 0;
1231 int *qpn = &priv->base_qpn;
1232 struct mlx4_mac_entry *entry;
1234 err = mlx4_en_uc_steer_add(priv, priv->dev->dev_addr, qpn, &reg_id);
1235 if (err)
1236 return err;
1238 err = mlx4_en_tunnel_steer_add(priv, priv->dev->dev_addr, *qpn,
1239 &priv->tunnel_reg_id);
1240 if (err)
1241 goto tunnel_err;
1243 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1244 if (!entry) {
1245 err = -ENOMEM;
1246 goto alloc_err;
1249 memcpy(entry->mac, priv->dev->dev_addr, sizeof(entry->mac));
1250 memcpy(priv->current_mac, entry->mac, sizeof(priv->current_mac));
1251 entry->reg_id = reg_id;
1252 hlist_add_head_rcu(&entry->hlist,
1253 &priv->mac_hash[entry->mac[MLX4_EN_MAC_HASH_IDX]]);
1255 return 0;
1257 alloc_err:
1258 if (priv->tunnel_reg_id)
1259 mlx4_flow_detach(priv->mdev->dev, priv->tunnel_reg_id);
1261 tunnel_err:
1262 mlx4_en_uc_steer_release(priv, priv->dev->dev_addr, *qpn, reg_id);
1263 return err;
1266 static void mlx4_en_delete_rss_steer_rules(struct mlx4_en_priv *priv)
1268 u64 mac;
1269 unsigned int i;
1270 int qpn = priv->base_qpn;
1271 struct hlist_head *bucket;
1272 struct hlist_node *tmp;
1273 struct mlx4_mac_entry *entry;
1275 for (i = 0; i < MLX4_EN_MAC_HASH_SIZE; ++i) {
1276 bucket = &priv->mac_hash[i];
1277 hlist_for_each_entry_safe(entry, tmp, bucket, hlist) {
1278 mac = mlx4_mac_to_u64(entry->mac);
1279 en_dbg(DRV, priv, "Registering MAC:%pM for deleting\n",
1280 entry->mac);
1281 mlx4_en_uc_steer_release(priv, entry->mac,
1282 qpn, entry->reg_id);
1284 mlx4_unregister_mac(priv->mdev->dev, priv->port, mac);
1285 hlist_del_rcu(&entry->hlist);
1286 kfree_rcu(entry, rcu);
1290 if (priv->tunnel_reg_id) {
1291 mlx4_flow_detach(priv->mdev->dev, priv->tunnel_reg_id);
1292 priv->tunnel_reg_id = 0;
1296 static void mlx4_en_tx_timeout(struct net_device *dev)
1298 struct mlx4_en_priv *priv = netdev_priv(dev);
1299 struct mlx4_en_dev *mdev = priv->mdev;
1300 int i;
1302 if (netif_msg_timer(priv))
1303 en_warn(priv, "Tx timeout called on port:%d\n", priv->port);
1305 for (i = 0; i < priv->tx_ring_num; i++) {
1306 if (!netif_tx_queue_stopped(netdev_get_tx_queue(dev, i)))
1307 continue;
1308 en_warn(priv, "TX timeout on queue: %d, QP: 0x%x, CQ: 0x%x, Cons: 0x%x, Prod: 0x%x\n",
1309 i, priv->tx_ring[i]->qpn, priv->tx_ring[i]->cqn,
1310 priv->tx_ring[i]->cons, priv->tx_ring[i]->prod);
1313 priv->port_stats.tx_timeout++;
1314 en_dbg(DRV, priv, "Scheduling watchdog\n");
1315 queue_work(mdev->workqueue, &priv->watchdog_task);
1319 static struct rtnl_link_stats64 *
1320 mlx4_en_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
1322 struct mlx4_en_priv *priv = netdev_priv(dev);
1324 spin_lock_bh(&priv->stats_lock);
1325 netdev_stats_to_stats64(stats, &dev->stats);
1326 spin_unlock_bh(&priv->stats_lock);
1328 return stats;
1331 static void mlx4_en_set_default_moderation(struct mlx4_en_priv *priv)
1333 struct mlx4_en_cq *cq;
1334 int i;
1336 /* If we haven't received a specific coalescing setting
1337 * (module param), we set the moderation parameters as follows:
1338 * - moder_cnt is set to the number of mtu sized packets to
1339 * satisfy our coalescing target.
1340 * - moder_time is set to a fixed value.
1342 priv->rx_frames = MLX4_EN_RX_COAL_TARGET;
1343 priv->rx_usecs = MLX4_EN_RX_COAL_TIME;
1344 priv->tx_frames = MLX4_EN_TX_COAL_PKTS;
1345 priv->tx_usecs = MLX4_EN_TX_COAL_TIME;
1346 en_dbg(INTR, priv, "Default coalesing params for mtu:%d - rx_frames:%d rx_usecs:%d\n",
1347 priv->dev->mtu, priv->rx_frames, priv->rx_usecs);
1349 /* Setup cq moderation params */
1350 for (i = 0; i < priv->rx_ring_num; i++) {
1351 cq = priv->rx_cq[i];
1352 cq->moder_cnt = priv->rx_frames;
1353 cq->moder_time = priv->rx_usecs;
1354 priv->last_moder_time[i] = MLX4_EN_AUTO_CONF;
1355 priv->last_moder_packets[i] = 0;
1356 priv->last_moder_bytes[i] = 0;
1359 for (i = 0; i < priv->tx_ring_num; i++) {
1360 cq = priv->tx_cq[i];
1361 cq->moder_cnt = priv->tx_frames;
1362 cq->moder_time = priv->tx_usecs;
1365 /* Reset auto-moderation params */
1366 priv->pkt_rate_low = MLX4_EN_RX_RATE_LOW;
1367 priv->rx_usecs_low = MLX4_EN_RX_COAL_TIME_LOW;
1368 priv->pkt_rate_high = MLX4_EN_RX_RATE_HIGH;
1369 priv->rx_usecs_high = MLX4_EN_RX_COAL_TIME_HIGH;
1370 priv->sample_interval = MLX4_EN_SAMPLE_INTERVAL;
1371 priv->adaptive_rx_coal = 1;
1372 priv->last_moder_jiffies = 0;
1373 priv->last_moder_tx_packets = 0;
1376 static void mlx4_en_auto_moderation(struct mlx4_en_priv *priv)
1378 unsigned long period = (unsigned long) (jiffies - priv->last_moder_jiffies);
1379 struct mlx4_en_cq *cq;
1380 unsigned long packets;
1381 unsigned long rate;
1382 unsigned long avg_pkt_size;
1383 unsigned long rx_packets;
1384 unsigned long rx_bytes;
1385 unsigned long rx_pkt_diff;
1386 int moder_time;
1387 int ring, err;
1389 if (!priv->adaptive_rx_coal || period < priv->sample_interval * HZ)
1390 return;
1392 for (ring = 0; ring < priv->rx_ring_num; ring++) {
1393 spin_lock_bh(&priv->stats_lock);
1394 rx_packets = priv->rx_ring[ring]->packets;
1395 rx_bytes = priv->rx_ring[ring]->bytes;
1396 spin_unlock_bh(&priv->stats_lock);
1398 rx_pkt_diff = ((unsigned long) (rx_packets -
1399 priv->last_moder_packets[ring]));
1400 packets = rx_pkt_diff;
1401 rate = packets * HZ / period;
1402 avg_pkt_size = packets ? ((unsigned long) (rx_bytes -
1403 priv->last_moder_bytes[ring])) / packets : 0;
1405 /* Apply auto-moderation only when packet rate
1406 * exceeds a rate that it matters */
1407 if (rate > (MLX4_EN_RX_RATE_THRESH / priv->rx_ring_num) &&
1408 avg_pkt_size > MLX4_EN_AVG_PKT_SMALL) {
1409 if (rate < priv->pkt_rate_low)
1410 moder_time = priv->rx_usecs_low;
1411 else if (rate > priv->pkt_rate_high)
1412 moder_time = priv->rx_usecs_high;
1413 else
1414 moder_time = (rate - priv->pkt_rate_low) *
1415 (priv->rx_usecs_high - priv->rx_usecs_low) /
1416 (priv->pkt_rate_high - priv->pkt_rate_low) +
1417 priv->rx_usecs_low;
1418 } else {
1419 moder_time = priv->rx_usecs_low;
1422 if (moder_time != priv->last_moder_time[ring]) {
1423 priv->last_moder_time[ring] = moder_time;
1424 cq = priv->rx_cq[ring];
1425 cq->moder_time = moder_time;
1426 cq->moder_cnt = priv->rx_frames;
1427 err = mlx4_en_set_cq_moder(priv, cq);
1428 if (err)
1429 en_err(priv, "Failed modifying moderation for cq:%d\n",
1430 ring);
1432 priv->last_moder_packets[ring] = rx_packets;
1433 priv->last_moder_bytes[ring] = rx_bytes;
1436 priv->last_moder_jiffies = jiffies;
1439 static void mlx4_en_do_get_stats(struct work_struct *work)
1441 struct delayed_work *delay = to_delayed_work(work);
1442 struct mlx4_en_priv *priv = container_of(delay, struct mlx4_en_priv,
1443 stats_task);
1444 struct mlx4_en_dev *mdev = priv->mdev;
1445 int err;
1447 mutex_lock(&mdev->state_lock);
1448 if (mdev->device_up) {
1449 if (priv->port_up) {
1450 err = mlx4_en_DUMP_ETH_STATS(mdev, priv->port, 0);
1451 if (err)
1452 en_dbg(HW, priv, "Could not update stats\n");
1454 mlx4_en_auto_moderation(priv);
1457 queue_delayed_work(mdev->workqueue, &priv->stats_task, STATS_DELAY);
1459 if (mdev->mac_removed[MLX4_MAX_PORTS + 1 - priv->port]) {
1460 mlx4_en_do_set_mac(priv, priv->current_mac);
1461 mdev->mac_removed[MLX4_MAX_PORTS + 1 - priv->port] = 0;
1463 mutex_unlock(&mdev->state_lock);
1466 /* mlx4_en_service_task - Run service task for tasks that needed to be done
1467 * periodically
1469 static void mlx4_en_service_task(struct work_struct *work)
1471 struct delayed_work *delay = to_delayed_work(work);
1472 struct mlx4_en_priv *priv = container_of(delay, struct mlx4_en_priv,
1473 service_task);
1474 struct mlx4_en_dev *mdev = priv->mdev;
1476 mutex_lock(&mdev->state_lock);
1477 if (mdev->device_up) {
1478 if (mdev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_TS)
1479 mlx4_en_ptp_overflow_check(mdev);
1481 mlx4_en_recover_from_oom(priv);
1482 queue_delayed_work(mdev->workqueue, &priv->service_task,
1483 SERVICE_TASK_DELAY);
1485 mutex_unlock(&mdev->state_lock);
1488 static void mlx4_en_linkstate(struct work_struct *work)
1490 struct mlx4_en_priv *priv = container_of(work, struct mlx4_en_priv,
1491 linkstate_task);
1492 struct mlx4_en_dev *mdev = priv->mdev;
1493 int linkstate = priv->link_state;
1495 mutex_lock(&mdev->state_lock);
1496 /* If observable port state changed set carrier state and
1497 * report to system log */
1498 if (priv->last_link_state != linkstate) {
1499 if (linkstate == MLX4_DEV_EVENT_PORT_DOWN) {
1500 en_info(priv, "Link Down\n");
1501 netif_carrier_off(priv->dev);
1502 } else {
1503 en_info(priv, "Link Up\n");
1504 netif_carrier_on(priv->dev);
1507 priv->last_link_state = linkstate;
1508 mutex_unlock(&mdev->state_lock);
1511 static int mlx4_en_init_affinity_hint(struct mlx4_en_priv *priv, int ring_idx)
1513 struct mlx4_en_rx_ring *ring = priv->rx_ring[ring_idx];
1514 int numa_node = priv->mdev->dev->numa_node;
1516 if (!zalloc_cpumask_var(&ring->affinity_mask, GFP_KERNEL))
1517 return -ENOMEM;
1519 cpumask_set_cpu(cpumask_local_spread(ring_idx, numa_node),
1520 ring->affinity_mask);
1521 return 0;
1524 static void mlx4_en_free_affinity_hint(struct mlx4_en_priv *priv, int ring_idx)
1526 free_cpumask_var(priv->rx_ring[ring_idx]->affinity_mask);
1529 static void mlx4_en_init_recycle_ring(struct mlx4_en_priv *priv,
1530 int tx_ring_idx)
1532 struct mlx4_en_tx_ring *tx_ring = priv->tx_ring[tx_ring_idx];
1533 int rr_index;
1535 rr_index = (priv->xdp_ring_num - priv->tx_ring_num) + tx_ring_idx;
1536 if (rr_index >= 0) {
1537 tx_ring->free_tx_desc = mlx4_en_recycle_tx_desc;
1538 tx_ring->recycle_ring = priv->rx_ring[rr_index];
1539 en_dbg(DRV, priv,
1540 "Set tx_ring[%d]->recycle_ring = rx_ring[%d]\n",
1541 tx_ring_idx, rr_index);
1542 } else {
1543 tx_ring->recycle_ring = NULL;
1547 int mlx4_en_start_port(struct net_device *dev)
1549 struct mlx4_en_priv *priv = netdev_priv(dev);
1550 struct mlx4_en_dev *mdev = priv->mdev;
1551 struct mlx4_en_cq *cq;
1552 struct mlx4_en_tx_ring *tx_ring;
1553 int rx_index = 0;
1554 int tx_index = 0;
1555 int err = 0;
1556 int i;
1557 int j;
1558 u8 mc_list[16] = {0};
1560 if (priv->port_up) {
1561 en_dbg(DRV, priv, "start port called while port already up\n");
1562 return 0;
1565 INIT_LIST_HEAD(&priv->mc_list);
1566 INIT_LIST_HEAD(&priv->curr_list);
1567 INIT_LIST_HEAD(&priv->ethtool_list);
1568 memset(&priv->ethtool_rules[0], 0,
1569 sizeof(struct ethtool_flow_id) * MAX_NUM_OF_FS_RULES);
1571 /* Calculate Rx buf size */
1572 dev->mtu = min(dev->mtu, priv->max_mtu);
1573 mlx4_en_calc_rx_buf(dev);
1574 en_dbg(DRV, priv, "Rx buf size:%d\n", priv->rx_skb_size);
1576 /* Configure rx cq's and rings */
1577 err = mlx4_en_activate_rx_rings(priv);
1578 if (err) {
1579 en_err(priv, "Failed to activate RX rings\n");
1580 return err;
1582 for (i = 0; i < priv->rx_ring_num; i++) {
1583 cq = priv->rx_cq[i];
1585 err = mlx4_en_init_affinity_hint(priv, i);
1586 if (err) {
1587 en_err(priv, "Failed preparing IRQ affinity hint\n");
1588 goto cq_err;
1591 err = mlx4_en_activate_cq(priv, cq, i);
1592 if (err) {
1593 en_err(priv, "Failed activating Rx CQ\n");
1594 mlx4_en_free_affinity_hint(priv, i);
1595 goto cq_err;
1598 for (j = 0; j < cq->size; j++) {
1599 struct mlx4_cqe *cqe = NULL;
1601 cqe = mlx4_en_get_cqe(cq->buf, j, priv->cqe_size) +
1602 priv->cqe_factor;
1603 cqe->owner_sr_opcode = MLX4_CQE_OWNER_MASK;
1606 err = mlx4_en_set_cq_moder(priv, cq);
1607 if (err) {
1608 en_err(priv, "Failed setting cq moderation parameters\n");
1609 mlx4_en_deactivate_cq(priv, cq);
1610 mlx4_en_free_affinity_hint(priv, i);
1611 goto cq_err;
1613 mlx4_en_arm_cq(priv, cq);
1614 priv->rx_ring[i]->cqn = cq->mcq.cqn;
1615 ++rx_index;
1618 /* Set qp number */
1619 en_dbg(DRV, priv, "Getting qp number for port %d\n", priv->port);
1620 err = mlx4_en_get_qp(priv);
1621 if (err) {
1622 en_err(priv, "Failed getting eth qp\n");
1623 goto cq_err;
1625 mdev->mac_removed[priv->port] = 0;
1627 priv->counter_index =
1628 mlx4_get_default_counter_index(mdev->dev, priv->port);
1630 err = mlx4_en_config_rss_steer(priv);
1631 if (err) {
1632 en_err(priv, "Failed configuring rss steering\n");
1633 goto mac_err;
1636 err = mlx4_en_create_drop_qp(priv);
1637 if (err)
1638 goto rss_err;
1640 /* Configure tx cq's and rings */
1641 for (i = 0; i < priv->tx_ring_num; i++) {
1642 /* Configure cq */
1643 cq = priv->tx_cq[i];
1644 err = mlx4_en_activate_cq(priv, cq, i);
1645 if (err) {
1646 en_err(priv, "Failed allocating Tx CQ\n");
1647 goto tx_err;
1649 err = mlx4_en_set_cq_moder(priv, cq);
1650 if (err) {
1651 en_err(priv, "Failed setting cq moderation parameters\n");
1652 mlx4_en_deactivate_cq(priv, cq);
1653 goto tx_err;
1655 en_dbg(DRV, priv, "Resetting index of collapsed CQ:%d to -1\n", i);
1656 cq->buf->wqe_index = cpu_to_be16(0xffff);
1658 /* Configure ring */
1659 tx_ring = priv->tx_ring[i];
1660 err = mlx4_en_activate_tx_ring(priv, tx_ring, cq->mcq.cqn,
1661 i / priv->num_tx_rings_p_up);
1662 if (err) {
1663 en_err(priv, "Failed allocating Tx ring\n");
1664 mlx4_en_deactivate_cq(priv, cq);
1665 goto tx_err;
1667 tx_ring->tx_queue = netdev_get_tx_queue(dev, i);
1669 mlx4_en_init_recycle_ring(priv, i);
1671 /* Arm CQ for TX completions */
1672 mlx4_en_arm_cq(priv, cq);
1674 /* Set initial ownership of all Tx TXBBs to SW (1) */
1675 for (j = 0; j < tx_ring->buf_size; j += STAMP_STRIDE)
1676 *((u32 *) (tx_ring->buf + j)) = 0xffffffff;
1677 ++tx_index;
1680 /* Configure port */
1681 err = mlx4_SET_PORT_general(mdev->dev, priv->port,
1682 priv->rx_skb_size + ETH_FCS_LEN,
1683 priv->prof->tx_pause,
1684 priv->prof->tx_ppp,
1685 priv->prof->rx_pause,
1686 priv->prof->rx_ppp);
1687 if (err) {
1688 en_err(priv, "Failed setting port general configurations for port %d, with error %d\n",
1689 priv->port, err);
1690 goto tx_err;
1692 /* Set default qp number */
1693 err = mlx4_SET_PORT_qpn_calc(mdev->dev, priv->port, priv->base_qpn, 0);
1694 if (err) {
1695 en_err(priv, "Failed setting default qp numbers\n");
1696 goto tx_err;
1699 if (mdev->dev->caps.tunnel_offload_mode == MLX4_TUNNEL_OFFLOAD_MODE_VXLAN) {
1700 err = mlx4_SET_PORT_VXLAN(mdev->dev, priv->port, VXLAN_STEER_BY_OUTER_MAC, 1);
1701 if (err) {
1702 en_err(priv, "Failed setting port L2 tunnel configuration, err %d\n",
1703 err);
1704 goto tx_err;
1708 /* Init port */
1709 en_dbg(HW, priv, "Initializing port\n");
1710 err = mlx4_INIT_PORT(mdev->dev, priv->port);
1711 if (err) {
1712 en_err(priv, "Failed Initializing port\n");
1713 goto tx_err;
1716 /* Set Unicast and VXLAN steering rules */
1717 if (mdev->dev->caps.steering_mode != MLX4_STEERING_MODE_A0 &&
1718 mlx4_en_set_rss_steer_rules(priv))
1719 mlx4_warn(mdev, "Failed setting steering rules\n");
1721 /* Attach rx QP to bradcast address */
1722 eth_broadcast_addr(&mc_list[10]);
1723 mc_list[5] = priv->port; /* needed for B0 steering support */
1724 if (mlx4_multicast_attach(mdev->dev, &priv->rss_map.indir_qp, mc_list,
1725 priv->port, 0, MLX4_PROT_ETH,
1726 &priv->broadcast_id))
1727 mlx4_warn(mdev, "Failed Attaching Broadcast\n");
1729 /* Must redo promiscuous mode setup. */
1730 priv->flags &= ~(MLX4_EN_FLAG_PROMISC | MLX4_EN_FLAG_MC_PROMISC);
1732 /* Schedule multicast task to populate multicast list */
1733 queue_work(mdev->workqueue, &priv->rx_mode_task);
1735 if (priv->mdev->dev->caps.tunnel_offload_mode == MLX4_TUNNEL_OFFLOAD_MODE_VXLAN)
1736 udp_tunnel_get_rx_info(dev);
1738 priv->port_up = true;
1740 /* Process all completions if exist to prevent
1741 * the queues freezing if they are full
1743 for (i = 0; i < priv->rx_ring_num; i++) {
1744 local_bh_disable();
1745 napi_schedule(&priv->rx_cq[i]->napi);
1746 local_bh_enable();
1749 netif_tx_start_all_queues(dev);
1750 netif_device_attach(dev);
1752 return 0;
1754 tx_err:
1755 while (tx_index--) {
1756 mlx4_en_deactivate_tx_ring(priv, priv->tx_ring[tx_index]);
1757 mlx4_en_deactivate_cq(priv, priv->tx_cq[tx_index]);
1759 mlx4_en_destroy_drop_qp(priv);
1760 rss_err:
1761 mlx4_en_release_rss_steer(priv);
1762 mac_err:
1763 mlx4_en_put_qp(priv);
1764 cq_err:
1765 while (rx_index--) {
1766 mlx4_en_deactivate_cq(priv, priv->rx_cq[rx_index]);
1767 mlx4_en_free_affinity_hint(priv, rx_index);
1769 for (i = 0; i < priv->rx_ring_num; i++)
1770 mlx4_en_deactivate_rx_ring(priv, priv->rx_ring[i]);
1772 return err; /* need to close devices */
1776 void mlx4_en_stop_port(struct net_device *dev, int detach)
1778 struct mlx4_en_priv *priv = netdev_priv(dev);
1779 struct mlx4_en_dev *mdev = priv->mdev;
1780 struct mlx4_en_mc_list *mclist, *tmp;
1781 struct ethtool_flow_id *flow, *tmp_flow;
1782 int i;
1783 u8 mc_list[16] = {0};
1785 if (!priv->port_up) {
1786 en_dbg(DRV, priv, "stop port called while port already down\n");
1787 return;
1790 /* close port*/
1791 mlx4_CLOSE_PORT(mdev->dev, priv->port);
1793 /* Synchronize with tx routine */
1794 netif_tx_lock_bh(dev);
1795 if (detach)
1796 netif_device_detach(dev);
1797 netif_tx_stop_all_queues(dev);
1798 netif_tx_unlock_bh(dev);
1800 netif_tx_disable(dev);
1802 /* Set port as not active */
1803 priv->port_up = false;
1804 priv->counter_index = MLX4_SINK_COUNTER_INDEX(mdev->dev);
1806 /* Promsicuous mode */
1807 if (mdev->dev->caps.steering_mode ==
1808 MLX4_STEERING_MODE_DEVICE_MANAGED) {
1809 priv->flags &= ~(MLX4_EN_FLAG_PROMISC |
1810 MLX4_EN_FLAG_MC_PROMISC);
1811 mlx4_flow_steer_promisc_remove(mdev->dev,
1812 priv->port,
1813 MLX4_FS_ALL_DEFAULT);
1814 mlx4_flow_steer_promisc_remove(mdev->dev,
1815 priv->port,
1816 MLX4_FS_MC_DEFAULT);
1817 } else if (priv->flags & MLX4_EN_FLAG_PROMISC) {
1818 priv->flags &= ~MLX4_EN_FLAG_PROMISC;
1820 /* Disable promiscouos mode */
1821 mlx4_unicast_promisc_remove(mdev->dev, priv->base_qpn,
1822 priv->port);
1824 /* Disable Multicast promisc */
1825 if (priv->flags & MLX4_EN_FLAG_MC_PROMISC) {
1826 mlx4_multicast_promisc_remove(mdev->dev, priv->base_qpn,
1827 priv->port);
1828 priv->flags &= ~MLX4_EN_FLAG_MC_PROMISC;
1832 /* Detach All multicasts */
1833 eth_broadcast_addr(&mc_list[10]);
1834 mc_list[5] = priv->port; /* needed for B0 steering support */
1835 mlx4_multicast_detach(mdev->dev, &priv->rss_map.indir_qp, mc_list,
1836 MLX4_PROT_ETH, priv->broadcast_id);
1837 list_for_each_entry(mclist, &priv->curr_list, list) {
1838 memcpy(&mc_list[10], mclist->addr, ETH_ALEN);
1839 mc_list[5] = priv->port;
1840 mlx4_multicast_detach(mdev->dev, &priv->rss_map.indir_qp,
1841 mc_list, MLX4_PROT_ETH, mclist->reg_id);
1842 if (mclist->tunnel_reg_id)
1843 mlx4_flow_detach(mdev->dev, mclist->tunnel_reg_id);
1845 mlx4_en_clear_list(dev);
1846 list_for_each_entry_safe(mclist, tmp, &priv->curr_list, list) {
1847 list_del(&mclist->list);
1848 kfree(mclist);
1851 /* Flush multicast filter */
1852 mlx4_SET_MCAST_FLTR(mdev->dev, priv->port, 0, 1, MLX4_MCAST_CONFIG);
1854 /* Remove flow steering rules for the port*/
1855 if (mdev->dev->caps.steering_mode ==
1856 MLX4_STEERING_MODE_DEVICE_MANAGED) {
1857 ASSERT_RTNL();
1858 list_for_each_entry_safe(flow, tmp_flow,
1859 &priv->ethtool_list, list) {
1860 mlx4_flow_detach(mdev->dev, flow->id);
1861 list_del(&flow->list);
1865 mlx4_en_destroy_drop_qp(priv);
1867 /* Free TX Rings */
1868 for (i = 0; i < priv->tx_ring_num; i++) {
1869 mlx4_en_deactivate_tx_ring(priv, priv->tx_ring[i]);
1870 mlx4_en_deactivate_cq(priv, priv->tx_cq[i]);
1872 msleep(10);
1874 for (i = 0; i < priv->tx_ring_num; i++)
1875 mlx4_en_free_tx_buf(dev, priv->tx_ring[i]);
1877 if (mdev->dev->caps.steering_mode != MLX4_STEERING_MODE_A0)
1878 mlx4_en_delete_rss_steer_rules(priv);
1880 /* Free RSS qps */
1881 mlx4_en_release_rss_steer(priv);
1883 /* Unregister Mac address for the port */
1884 mlx4_en_put_qp(priv);
1885 if (!(mdev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_REASSIGN_MAC_EN))
1886 mdev->mac_removed[priv->port] = 1;
1888 /* Free RX Rings */
1889 for (i = 0; i < priv->rx_ring_num; i++) {
1890 struct mlx4_en_cq *cq = priv->rx_cq[i];
1892 napi_synchronize(&cq->napi);
1893 mlx4_en_deactivate_rx_ring(priv, priv->rx_ring[i]);
1894 mlx4_en_deactivate_cq(priv, cq);
1896 mlx4_en_free_affinity_hint(priv, i);
1900 static void mlx4_en_restart(struct work_struct *work)
1902 struct mlx4_en_priv *priv = container_of(work, struct mlx4_en_priv,
1903 watchdog_task);
1904 struct mlx4_en_dev *mdev = priv->mdev;
1905 struct net_device *dev = priv->dev;
1907 en_dbg(DRV, priv, "Watchdog task called for port %d\n", priv->port);
1909 rtnl_lock();
1910 mutex_lock(&mdev->state_lock);
1911 if (priv->port_up) {
1912 mlx4_en_stop_port(dev, 1);
1913 if (mlx4_en_start_port(dev))
1914 en_err(priv, "Failed restarting port %d\n", priv->port);
1916 mutex_unlock(&mdev->state_lock);
1917 rtnl_unlock();
1920 static void mlx4_en_clear_stats(struct net_device *dev)
1922 struct mlx4_en_priv *priv = netdev_priv(dev);
1923 struct mlx4_en_dev *mdev = priv->mdev;
1924 int i;
1926 if (!mlx4_is_slave(mdev->dev))
1927 if (mlx4_en_DUMP_ETH_STATS(mdev, priv->port, 1))
1928 en_dbg(HW, priv, "Failed dumping statistics\n");
1930 memset(&priv->pstats, 0, sizeof(priv->pstats));
1931 memset(&priv->pkstats, 0, sizeof(priv->pkstats));
1932 memset(&priv->port_stats, 0, sizeof(priv->port_stats));
1933 memset(&priv->rx_flowstats, 0, sizeof(priv->rx_flowstats));
1934 memset(&priv->tx_flowstats, 0, sizeof(priv->tx_flowstats));
1935 memset(&priv->rx_priority_flowstats, 0,
1936 sizeof(priv->rx_priority_flowstats));
1937 memset(&priv->tx_priority_flowstats, 0,
1938 sizeof(priv->tx_priority_flowstats));
1939 memset(&priv->pf_stats, 0, sizeof(priv->pf_stats));
1941 for (i = 0; i < priv->tx_ring_num; i++) {
1942 priv->tx_ring[i]->bytes = 0;
1943 priv->tx_ring[i]->packets = 0;
1944 priv->tx_ring[i]->tx_csum = 0;
1945 priv->tx_ring[i]->tx_dropped = 0;
1946 priv->tx_ring[i]->queue_stopped = 0;
1947 priv->tx_ring[i]->wake_queue = 0;
1948 priv->tx_ring[i]->tso_packets = 0;
1949 priv->tx_ring[i]->xmit_more = 0;
1951 for (i = 0; i < priv->rx_ring_num; i++) {
1952 priv->rx_ring[i]->bytes = 0;
1953 priv->rx_ring[i]->packets = 0;
1954 priv->rx_ring[i]->csum_ok = 0;
1955 priv->rx_ring[i]->csum_none = 0;
1956 priv->rx_ring[i]->csum_complete = 0;
1960 static int mlx4_en_open(struct net_device *dev)
1962 struct mlx4_en_priv *priv = netdev_priv(dev);
1963 struct mlx4_en_dev *mdev = priv->mdev;
1964 int err = 0;
1966 mutex_lock(&mdev->state_lock);
1968 if (!mdev->device_up) {
1969 en_err(priv, "Cannot open - device down/disabled\n");
1970 err = -EBUSY;
1971 goto out;
1974 /* Reset HW statistics and SW counters */
1975 mlx4_en_clear_stats(dev);
1977 err = mlx4_en_start_port(dev);
1978 if (err)
1979 en_err(priv, "Failed starting port:%d\n", priv->port);
1981 out:
1982 mutex_unlock(&mdev->state_lock);
1983 return err;
1987 static int mlx4_en_close(struct net_device *dev)
1989 struct mlx4_en_priv *priv = netdev_priv(dev);
1990 struct mlx4_en_dev *mdev = priv->mdev;
1992 en_dbg(IFDOWN, priv, "Close port called\n");
1994 mutex_lock(&mdev->state_lock);
1996 mlx4_en_stop_port(dev, 0);
1997 netif_carrier_off(dev);
1999 mutex_unlock(&mdev->state_lock);
2000 return 0;
2003 static void mlx4_en_free_resources(struct mlx4_en_priv *priv)
2005 int i;
2007 #ifdef CONFIG_RFS_ACCEL
2008 priv->dev->rx_cpu_rmap = NULL;
2009 #endif
2011 for (i = 0; i < priv->tx_ring_num; i++) {
2012 if (priv->tx_ring && priv->tx_ring[i])
2013 mlx4_en_destroy_tx_ring(priv, &priv->tx_ring[i]);
2014 if (priv->tx_cq && priv->tx_cq[i])
2015 mlx4_en_destroy_cq(priv, &priv->tx_cq[i]);
2018 for (i = 0; i < priv->rx_ring_num; i++) {
2019 if (priv->rx_ring[i])
2020 mlx4_en_destroy_rx_ring(priv, &priv->rx_ring[i],
2021 priv->prof->rx_ring_size, priv->stride);
2022 if (priv->rx_cq[i])
2023 mlx4_en_destroy_cq(priv, &priv->rx_cq[i]);
2028 static int mlx4_en_alloc_resources(struct mlx4_en_priv *priv)
2030 struct mlx4_en_port_profile *prof = priv->prof;
2031 int i;
2032 int node;
2034 /* Create tx Rings */
2035 for (i = 0; i < priv->tx_ring_num; i++) {
2036 node = cpu_to_node(i % num_online_cpus());
2037 if (mlx4_en_create_cq(priv, &priv->tx_cq[i],
2038 prof->tx_ring_size, i, TX, node))
2039 goto err;
2041 if (mlx4_en_create_tx_ring(priv, &priv->tx_ring[i],
2042 prof->tx_ring_size, TXBB_SIZE,
2043 node, i))
2044 goto err;
2047 /* Create rx Rings */
2048 for (i = 0; i < priv->rx_ring_num; i++) {
2049 node = cpu_to_node(i % num_online_cpus());
2050 if (mlx4_en_create_cq(priv, &priv->rx_cq[i],
2051 prof->rx_ring_size, i, RX, node))
2052 goto err;
2054 if (mlx4_en_create_rx_ring(priv, &priv->rx_ring[i],
2055 prof->rx_ring_size, priv->stride,
2056 node))
2057 goto err;
2060 #ifdef CONFIG_RFS_ACCEL
2061 priv->dev->rx_cpu_rmap = mlx4_get_cpu_rmap(priv->mdev->dev, priv->port);
2062 #endif
2064 return 0;
2066 err:
2067 en_err(priv, "Failed to allocate NIC resources\n");
2068 for (i = 0; i < priv->rx_ring_num; i++) {
2069 if (priv->rx_ring[i])
2070 mlx4_en_destroy_rx_ring(priv, &priv->rx_ring[i],
2071 prof->rx_ring_size,
2072 priv->stride);
2073 if (priv->rx_cq[i])
2074 mlx4_en_destroy_cq(priv, &priv->rx_cq[i]);
2076 for (i = 0; i < priv->tx_ring_num; i++) {
2077 if (priv->tx_ring[i])
2078 mlx4_en_destroy_tx_ring(priv, &priv->tx_ring[i]);
2079 if (priv->tx_cq[i])
2080 mlx4_en_destroy_cq(priv, &priv->tx_cq[i]);
2082 return -ENOMEM;
2086 static int mlx4_en_copy_priv(struct mlx4_en_priv *dst,
2087 struct mlx4_en_priv *src,
2088 struct mlx4_en_port_profile *prof)
2090 memcpy(&dst->hwtstamp_config, &prof->hwtstamp_config,
2091 sizeof(dst->hwtstamp_config));
2092 dst->num_tx_rings_p_up = src->mdev->profile.num_tx_rings_p_up;
2093 dst->tx_ring_num = prof->tx_ring_num;
2094 dst->rx_ring_num = prof->rx_ring_num;
2095 dst->flags = prof->flags;
2096 dst->mdev = src->mdev;
2097 dst->port = src->port;
2098 dst->dev = src->dev;
2099 dst->prof = prof;
2100 dst->stride = roundup_pow_of_two(sizeof(struct mlx4_en_rx_desc) +
2101 DS_SIZE * MLX4_EN_MAX_RX_FRAGS);
2103 dst->tx_ring = kzalloc(sizeof(struct mlx4_en_tx_ring *) * MAX_TX_RINGS,
2104 GFP_KERNEL);
2105 if (!dst->tx_ring)
2106 return -ENOMEM;
2108 dst->tx_cq = kzalloc(sizeof(struct mlx4_en_cq *) * MAX_TX_RINGS,
2109 GFP_KERNEL);
2110 if (!dst->tx_cq) {
2111 kfree(dst->tx_ring);
2112 return -ENOMEM;
2114 return 0;
2117 static void mlx4_en_update_priv(struct mlx4_en_priv *dst,
2118 struct mlx4_en_priv *src)
2120 memcpy(dst->rx_ring, src->rx_ring,
2121 sizeof(struct mlx4_en_rx_ring *) * src->rx_ring_num);
2122 memcpy(dst->rx_cq, src->rx_cq,
2123 sizeof(struct mlx4_en_cq *) * src->rx_ring_num);
2124 memcpy(&dst->hwtstamp_config, &src->hwtstamp_config,
2125 sizeof(dst->hwtstamp_config));
2126 dst->tx_ring_num = src->tx_ring_num;
2127 dst->rx_ring_num = src->rx_ring_num;
2128 dst->tx_ring = src->tx_ring;
2129 dst->tx_cq = src->tx_cq;
2130 memcpy(dst->prof, src->prof, sizeof(struct mlx4_en_port_profile));
2133 int mlx4_en_try_alloc_resources(struct mlx4_en_priv *priv,
2134 struct mlx4_en_priv *tmp,
2135 struct mlx4_en_port_profile *prof)
2137 mlx4_en_copy_priv(tmp, priv, prof);
2139 if (mlx4_en_alloc_resources(tmp)) {
2140 en_warn(priv,
2141 "%s: Resource allocation failed, using previous configuration\n",
2142 __func__);
2143 kfree(tmp->tx_ring);
2144 kfree(tmp->tx_cq);
2145 return -ENOMEM;
2147 return 0;
2150 void mlx4_en_safe_replace_resources(struct mlx4_en_priv *priv,
2151 struct mlx4_en_priv *tmp)
2153 mlx4_en_free_resources(priv);
2154 mlx4_en_update_priv(priv, tmp);
2157 void mlx4_en_destroy_netdev(struct net_device *dev)
2159 struct mlx4_en_priv *priv = netdev_priv(dev);
2160 struct mlx4_en_dev *mdev = priv->mdev;
2162 en_dbg(DRV, priv, "Destroying netdev on port:%d\n", priv->port);
2164 /* Unregister device - this will close the port if it was up */
2165 if (priv->registered) {
2166 devlink_port_type_clear(mlx4_get_devlink_port(mdev->dev,
2167 priv->port));
2168 unregister_netdev(dev);
2171 if (priv->allocated)
2172 mlx4_free_hwq_res(mdev->dev, &priv->res, MLX4_EN_PAGE_SIZE);
2174 cancel_delayed_work(&priv->stats_task);
2175 cancel_delayed_work(&priv->service_task);
2176 /* flush any pending task for this netdev */
2177 flush_workqueue(mdev->workqueue);
2179 if (mdev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_TS)
2180 mlx4_en_remove_timestamp(mdev);
2182 /* Detach the netdev so tasks would not attempt to access it */
2183 mutex_lock(&mdev->state_lock);
2184 mdev->pndev[priv->port] = NULL;
2185 mdev->upper[priv->port] = NULL;
2187 #ifdef CONFIG_RFS_ACCEL
2188 mlx4_en_cleanup_filters(priv);
2189 #endif
2191 mlx4_en_free_resources(priv);
2192 mutex_unlock(&mdev->state_lock);
2194 kfree(priv->tx_ring);
2195 kfree(priv->tx_cq);
2197 free_netdev(dev);
2200 static int mlx4_en_change_mtu(struct net_device *dev, int new_mtu)
2202 struct mlx4_en_priv *priv = netdev_priv(dev);
2203 struct mlx4_en_dev *mdev = priv->mdev;
2204 int err = 0;
2206 en_dbg(DRV, priv, "Change MTU called - current:%d new:%d\n",
2207 dev->mtu, new_mtu);
2209 if ((new_mtu < MLX4_EN_MIN_MTU) || (new_mtu > priv->max_mtu)) {
2210 en_err(priv, "Bad MTU size:%d.\n", new_mtu);
2211 return -EPERM;
2213 if (priv->xdp_ring_num && MLX4_EN_EFF_MTU(new_mtu) > FRAG_SZ0) {
2214 en_err(priv, "MTU size:%d requires frags but XDP running\n",
2215 new_mtu);
2216 return -EOPNOTSUPP;
2218 dev->mtu = new_mtu;
2220 if (netif_running(dev)) {
2221 mutex_lock(&mdev->state_lock);
2222 if (!mdev->device_up) {
2223 /* NIC is probably restarting - let watchdog task reset
2224 * the port */
2225 en_dbg(DRV, priv, "Change MTU called with card down!?\n");
2226 } else {
2227 mlx4_en_stop_port(dev, 1);
2228 err = mlx4_en_start_port(dev);
2229 if (err) {
2230 en_err(priv, "Failed restarting port:%d\n",
2231 priv->port);
2232 queue_work(mdev->workqueue, &priv->watchdog_task);
2235 mutex_unlock(&mdev->state_lock);
2237 return 0;
2240 static int mlx4_en_hwtstamp_set(struct net_device *dev, struct ifreq *ifr)
2242 struct mlx4_en_priv *priv = netdev_priv(dev);
2243 struct mlx4_en_dev *mdev = priv->mdev;
2244 struct hwtstamp_config config;
2246 if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
2247 return -EFAULT;
2249 /* reserved for future extensions */
2250 if (config.flags)
2251 return -EINVAL;
2253 /* device doesn't support time stamping */
2254 if (!(mdev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_TS))
2255 return -EINVAL;
2257 /* TX HW timestamp */
2258 switch (config.tx_type) {
2259 case HWTSTAMP_TX_OFF:
2260 case HWTSTAMP_TX_ON:
2261 break;
2262 default:
2263 return -ERANGE;
2266 /* RX HW timestamp */
2267 switch (config.rx_filter) {
2268 case HWTSTAMP_FILTER_NONE:
2269 break;
2270 case HWTSTAMP_FILTER_ALL:
2271 case HWTSTAMP_FILTER_SOME:
2272 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
2273 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
2274 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
2275 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
2276 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
2277 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
2278 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
2279 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
2280 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
2281 case HWTSTAMP_FILTER_PTP_V2_EVENT:
2282 case HWTSTAMP_FILTER_PTP_V2_SYNC:
2283 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
2284 config.rx_filter = HWTSTAMP_FILTER_ALL;
2285 break;
2286 default:
2287 return -ERANGE;
2290 if (mlx4_en_reset_config(dev, config, dev->features)) {
2291 config.tx_type = HWTSTAMP_TX_OFF;
2292 config.rx_filter = HWTSTAMP_FILTER_NONE;
2295 return copy_to_user(ifr->ifr_data, &config,
2296 sizeof(config)) ? -EFAULT : 0;
2299 static int mlx4_en_hwtstamp_get(struct net_device *dev, struct ifreq *ifr)
2301 struct mlx4_en_priv *priv = netdev_priv(dev);
2303 return copy_to_user(ifr->ifr_data, &priv->hwtstamp_config,
2304 sizeof(priv->hwtstamp_config)) ? -EFAULT : 0;
2307 static int mlx4_en_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
2309 switch (cmd) {
2310 case SIOCSHWTSTAMP:
2311 return mlx4_en_hwtstamp_set(dev, ifr);
2312 case SIOCGHWTSTAMP:
2313 return mlx4_en_hwtstamp_get(dev, ifr);
2314 default:
2315 return -EOPNOTSUPP;
2319 static netdev_features_t mlx4_en_fix_features(struct net_device *netdev,
2320 netdev_features_t features)
2322 struct mlx4_en_priv *en_priv = netdev_priv(netdev);
2323 struct mlx4_en_dev *mdev = en_priv->mdev;
2325 /* Since there is no support for separate RX C-TAG/S-TAG vlan accel
2326 * enable/disable make sure S-TAG flag is always in same state as
2327 * C-TAG.
2329 if (features & NETIF_F_HW_VLAN_CTAG_RX &&
2330 !(mdev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_SKIP_OUTER_VLAN))
2331 features |= NETIF_F_HW_VLAN_STAG_RX;
2332 else
2333 features &= ~NETIF_F_HW_VLAN_STAG_RX;
2335 return features;
2338 static int mlx4_en_set_features(struct net_device *netdev,
2339 netdev_features_t features)
2341 struct mlx4_en_priv *priv = netdev_priv(netdev);
2342 bool reset = false;
2343 int ret = 0;
2345 if (DEV_FEATURE_CHANGED(netdev, features, NETIF_F_RXFCS)) {
2346 en_info(priv, "Turn %s RX-FCS\n",
2347 (features & NETIF_F_RXFCS) ? "ON" : "OFF");
2348 reset = true;
2351 if (DEV_FEATURE_CHANGED(netdev, features, NETIF_F_RXALL)) {
2352 u8 ignore_fcs_value = (features & NETIF_F_RXALL) ? 1 : 0;
2354 en_info(priv, "Turn %s RX-ALL\n",
2355 ignore_fcs_value ? "ON" : "OFF");
2356 ret = mlx4_SET_PORT_fcs_check(priv->mdev->dev,
2357 priv->port, ignore_fcs_value);
2358 if (ret)
2359 return ret;
2362 if (DEV_FEATURE_CHANGED(netdev, features, NETIF_F_HW_VLAN_CTAG_RX)) {
2363 en_info(priv, "Turn %s RX vlan strip offload\n",
2364 (features & NETIF_F_HW_VLAN_CTAG_RX) ? "ON" : "OFF");
2365 reset = true;
2368 if (DEV_FEATURE_CHANGED(netdev, features, NETIF_F_HW_VLAN_CTAG_TX))
2369 en_info(priv, "Turn %s TX vlan strip offload\n",
2370 (features & NETIF_F_HW_VLAN_CTAG_TX) ? "ON" : "OFF");
2372 if (DEV_FEATURE_CHANGED(netdev, features, NETIF_F_HW_VLAN_STAG_TX))
2373 en_info(priv, "Turn %s TX S-VLAN strip offload\n",
2374 (features & NETIF_F_HW_VLAN_STAG_TX) ? "ON" : "OFF");
2376 if (DEV_FEATURE_CHANGED(netdev, features, NETIF_F_LOOPBACK)) {
2377 en_info(priv, "Turn %s loopback\n",
2378 (features & NETIF_F_LOOPBACK) ? "ON" : "OFF");
2379 mlx4_en_update_loopback_state(netdev, features);
2382 if (reset) {
2383 ret = mlx4_en_reset_config(netdev, priv->hwtstamp_config,
2384 features);
2385 if (ret)
2386 return ret;
2389 return 0;
2392 static int mlx4_en_set_vf_mac(struct net_device *dev, int queue, u8 *mac)
2394 struct mlx4_en_priv *en_priv = netdev_priv(dev);
2395 struct mlx4_en_dev *mdev = en_priv->mdev;
2396 u64 mac_u64 = mlx4_mac_to_u64(mac);
2398 if (is_multicast_ether_addr(mac))
2399 return -EINVAL;
2401 return mlx4_set_vf_mac(mdev->dev, en_priv->port, queue, mac_u64);
2404 static int mlx4_en_set_vf_vlan(struct net_device *dev, int vf, u16 vlan, u8 qos,
2405 __be16 vlan_proto)
2407 struct mlx4_en_priv *en_priv = netdev_priv(dev);
2408 struct mlx4_en_dev *mdev = en_priv->mdev;
2410 return mlx4_set_vf_vlan(mdev->dev, en_priv->port, vf, vlan, qos,
2411 vlan_proto);
2414 static int mlx4_en_set_vf_rate(struct net_device *dev, int vf, int min_tx_rate,
2415 int max_tx_rate)
2417 struct mlx4_en_priv *en_priv = netdev_priv(dev);
2418 struct mlx4_en_dev *mdev = en_priv->mdev;
2420 return mlx4_set_vf_rate(mdev->dev, en_priv->port, vf, min_tx_rate,
2421 max_tx_rate);
2424 static int mlx4_en_set_vf_spoofchk(struct net_device *dev, int vf, bool setting)
2426 struct mlx4_en_priv *en_priv = netdev_priv(dev);
2427 struct mlx4_en_dev *mdev = en_priv->mdev;
2429 return mlx4_set_vf_spoofchk(mdev->dev, en_priv->port, vf, setting);
2432 static int mlx4_en_get_vf_config(struct net_device *dev, int vf, struct ifla_vf_info *ivf)
2434 struct mlx4_en_priv *en_priv = netdev_priv(dev);
2435 struct mlx4_en_dev *mdev = en_priv->mdev;
2437 return mlx4_get_vf_config(mdev->dev, en_priv->port, vf, ivf);
2440 static int mlx4_en_set_vf_link_state(struct net_device *dev, int vf, int link_state)
2442 struct mlx4_en_priv *en_priv = netdev_priv(dev);
2443 struct mlx4_en_dev *mdev = en_priv->mdev;
2445 return mlx4_set_vf_link_state(mdev->dev, en_priv->port, vf, link_state);
2448 static int mlx4_en_get_vf_stats(struct net_device *dev, int vf,
2449 struct ifla_vf_stats *vf_stats)
2451 struct mlx4_en_priv *en_priv = netdev_priv(dev);
2452 struct mlx4_en_dev *mdev = en_priv->mdev;
2454 return mlx4_get_vf_stats(mdev->dev, en_priv->port, vf, vf_stats);
2457 #define PORT_ID_BYTE_LEN 8
2458 static int mlx4_en_get_phys_port_id(struct net_device *dev,
2459 struct netdev_phys_item_id *ppid)
2461 struct mlx4_en_priv *priv = netdev_priv(dev);
2462 struct mlx4_dev *mdev = priv->mdev->dev;
2463 int i;
2464 u64 phys_port_id = mdev->caps.phys_port_id[priv->port];
2466 if (!phys_port_id)
2467 return -EOPNOTSUPP;
2469 ppid->id_len = sizeof(phys_port_id);
2470 for (i = PORT_ID_BYTE_LEN - 1; i >= 0; --i) {
2471 ppid->id[i] = phys_port_id & 0xff;
2472 phys_port_id >>= 8;
2474 return 0;
2477 static void mlx4_en_add_vxlan_offloads(struct work_struct *work)
2479 int ret;
2480 struct mlx4_en_priv *priv = container_of(work, struct mlx4_en_priv,
2481 vxlan_add_task);
2483 ret = mlx4_config_vxlan_port(priv->mdev->dev, priv->vxlan_port);
2484 if (ret)
2485 goto out;
2487 ret = mlx4_SET_PORT_VXLAN(priv->mdev->dev, priv->port,
2488 VXLAN_STEER_BY_OUTER_MAC, 1);
2489 out:
2490 if (ret) {
2491 en_err(priv, "failed setting L2 tunnel configuration ret %d\n", ret);
2492 return;
2495 /* set offloads */
2496 priv->dev->hw_enc_features |= NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
2497 NETIF_F_RXCSUM |
2498 NETIF_F_TSO | NETIF_F_TSO6 |
2499 NETIF_F_GSO_UDP_TUNNEL |
2500 NETIF_F_GSO_UDP_TUNNEL_CSUM |
2501 NETIF_F_GSO_PARTIAL;
2504 static void mlx4_en_del_vxlan_offloads(struct work_struct *work)
2506 int ret;
2507 struct mlx4_en_priv *priv = container_of(work, struct mlx4_en_priv,
2508 vxlan_del_task);
2509 /* unset offloads */
2510 priv->dev->hw_enc_features &= ~(NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
2511 NETIF_F_RXCSUM |
2512 NETIF_F_TSO | NETIF_F_TSO6 |
2513 NETIF_F_GSO_UDP_TUNNEL |
2514 NETIF_F_GSO_UDP_TUNNEL_CSUM |
2515 NETIF_F_GSO_PARTIAL);
2517 ret = mlx4_SET_PORT_VXLAN(priv->mdev->dev, priv->port,
2518 VXLAN_STEER_BY_OUTER_MAC, 0);
2519 if (ret)
2520 en_err(priv, "failed setting L2 tunnel configuration ret %d\n", ret);
2522 priv->vxlan_port = 0;
2525 static void mlx4_en_add_vxlan_port(struct net_device *dev,
2526 struct udp_tunnel_info *ti)
2528 struct mlx4_en_priv *priv = netdev_priv(dev);
2529 __be16 port = ti->port;
2530 __be16 current_port;
2532 if (ti->type != UDP_TUNNEL_TYPE_VXLAN)
2533 return;
2535 if (ti->sa_family != AF_INET)
2536 return;
2538 if (priv->mdev->dev->caps.tunnel_offload_mode != MLX4_TUNNEL_OFFLOAD_MODE_VXLAN)
2539 return;
2541 current_port = priv->vxlan_port;
2542 if (current_port && current_port != port) {
2543 en_warn(priv, "vxlan port %d configured, can't add port %d\n",
2544 ntohs(current_port), ntohs(port));
2545 return;
2548 priv->vxlan_port = port;
2549 queue_work(priv->mdev->workqueue, &priv->vxlan_add_task);
2552 static void mlx4_en_del_vxlan_port(struct net_device *dev,
2553 struct udp_tunnel_info *ti)
2555 struct mlx4_en_priv *priv = netdev_priv(dev);
2556 __be16 port = ti->port;
2557 __be16 current_port;
2559 if (ti->type != UDP_TUNNEL_TYPE_VXLAN)
2560 return;
2562 if (ti->sa_family != AF_INET)
2563 return;
2565 if (priv->mdev->dev->caps.tunnel_offload_mode != MLX4_TUNNEL_OFFLOAD_MODE_VXLAN)
2566 return;
2568 current_port = priv->vxlan_port;
2569 if (current_port != port) {
2570 en_dbg(DRV, priv, "vxlan port %d isn't configured, ignoring\n", ntohs(port));
2571 return;
2574 queue_work(priv->mdev->workqueue, &priv->vxlan_del_task);
2577 static netdev_features_t mlx4_en_features_check(struct sk_buff *skb,
2578 struct net_device *dev,
2579 netdev_features_t features)
2581 features = vlan_features_check(skb, features);
2582 features = vxlan_features_check(skb, features);
2584 /* The ConnectX-3 doesn't support outer IPv6 checksums but it does
2585 * support inner IPv6 checksums and segmentation so we need to
2586 * strip that feature if this is an IPv6 encapsulated frame.
2588 if (skb->encapsulation &&
2589 (skb->ip_summed == CHECKSUM_PARTIAL)) {
2590 struct mlx4_en_priv *priv = netdev_priv(dev);
2592 if (!priv->vxlan_port ||
2593 (ip_hdr(skb)->version != 4) ||
2594 (udp_hdr(skb)->dest != priv->vxlan_port))
2595 features &= ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
2598 return features;
2601 static int mlx4_en_set_tx_maxrate(struct net_device *dev, int queue_index, u32 maxrate)
2603 struct mlx4_en_priv *priv = netdev_priv(dev);
2604 struct mlx4_en_tx_ring *tx_ring = priv->tx_ring[queue_index];
2605 struct mlx4_update_qp_params params;
2606 int err;
2608 if (!(priv->mdev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_QP_RATE_LIMIT))
2609 return -EOPNOTSUPP;
2611 /* rate provided to us in Mbs, check if it fits into 12 bits, if not use Gbs */
2612 if (maxrate >> 12) {
2613 params.rate_unit = MLX4_QP_RATE_LIMIT_GBS;
2614 params.rate_val = maxrate / 1000;
2615 } else if (maxrate) {
2616 params.rate_unit = MLX4_QP_RATE_LIMIT_MBS;
2617 params.rate_val = maxrate;
2618 } else { /* zero serves to revoke the QP rate-limitation */
2619 params.rate_unit = 0;
2620 params.rate_val = 0;
2623 err = mlx4_update_qp(priv->mdev->dev, tx_ring->qpn, MLX4_UPDATE_QP_RATE_LIMIT,
2624 &params);
2625 return err;
2628 static int mlx4_xdp_set(struct net_device *dev, struct bpf_prog *prog)
2630 struct mlx4_en_priv *priv = netdev_priv(dev);
2631 struct mlx4_en_dev *mdev = priv->mdev;
2632 struct bpf_prog *old_prog;
2633 int xdp_ring_num;
2634 int port_up = 0;
2635 int err;
2636 int i;
2638 xdp_ring_num = prog ? ALIGN(priv->rx_ring_num, MLX4_EN_NUM_UP) : 0;
2640 /* No need to reconfigure buffers when simply swapping the
2641 * program for a new one.
2643 if (priv->xdp_ring_num == xdp_ring_num) {
2644 if (prog) {
2645 prog = bpf_prog_add(prog, priv->rx_ring_num - 1);
2646 if (IS_ERR(prog))
2647 return PTR_ERR(prog);
2649 mutex_lock(&mdev->state_lock);
2650 for (i = 0; i < priv->rx_ring_num; i++) {
2651 old_prog = rcu_dereference_protected(
2652 priv->rx_ring[i]->xdp_prog,
2653 lockdep_is_held(&mdev->state_lock));
2654 rcu_assign_pointer(priv->rx_ring[i]->xdp_prog, prog);
2655 if (old_prog)
2656 bpf_prog_put(old_prog);
2658 mutex_unlock(&mdev->state_lock);
2659 return 0;
2662 if (priv->num_frags > 1) {
2663 en_err(priv, "Cannot set XDP if MTU requires multiple frags\n");
2664 return -EOPNOTSUPP;
2667 if (priv->tx_ring_num < xdp_ring_num + MLX4_EN_NUM_UP) {
2668 en_err(priv,
2669 "Minimum %d tx channels required to run XDP\n",
2670 (xdp_ring_num + MLX4_EN_NUM_UP) / MLX4_EN_NUM_UP);
2671 return -EINVAL;
2674 if (prog) {
2675 prog = bpf_prog_add(prog, priv->rx_ring_num - 1);
2676 if (IS_ERR(prog))
2677 return PTR_ERR(prog);
2680 mutex_lock(&mdev->state_lock);
2681 if (priv->port_up) {
2682 port_up = 1;
2683 mlx4_en_stop_port(dev, 1);
2686 priv->xdp_ring_num = xdp_ring_num;
2687 netif_set_real_num_tx_queues(dev, priv->tx_ring_num -
2688 priv->xdp_ring_num);
2690 for (i = 0; i < priv->rx_ring_num; i++) {
2691 old_prog = rcu_dereference_protected(
2692 priv->rx_ring[i]->xdp_prog,
2693 lockdep_is_held(&mdev->state_lock));
2694 rcu_assign_pointer(priv->rx_ring[i]->xdp_prog, prog);
2695 if (old_prog)
2696 bpf_prog_put(old_prog);
2699 if (port_up) {
2700 err = mlx4_en_start_port(dev);
2701 if (err) {
2702 en_err(priv, "Failed starting port %d for XDP change\n",
2703 priv->port);
2704 queue_work(mdev->workqueue, &priv->watchdog_task);
2708 mutex_unlock(&mdev->state_lock);
2709 return 0;
2712 static bool mlx4_xdp_attached(struct net_device *dev)
2714 struct mlx4_en_priv *priv = netdev_priv(dev);
2716 return !!priv->xdp_ring_num;
2719 static int mlx4_xdp(struct net_device *dev, struct netdev_xdp *xdp)
2721 switch (xdp->command) {
2722 case XDP_SETUP_PROG:
2723 return mlx4_xdp_set(dev, xdp->prog);
2724 case XDP_QUERY_PROG:
2725 xdp->prog_attached = mlx4_xdp_attached(dev);
2726 return 0;
2727 default:
2728 return -EINVAL;
2732 static const struct net_device_ops mlx4_netdev_ops = {
2733 .ndo_open = mlx4_en_open,
2734 .ndo_stop = mlx4_en_close,
2735 .ndo_start_xmit = mlx4_en_xmit,
2736 .ndo_select_queue = mlx4_en_select_queue,
2737 .ndo_get_stats64 = mlx4_en_get_stats64,
2738 .ndo_set_rx_mode = mlx4_en_set_rx_mode,
2739 .ndo_set_mac_address = mlx4_en_set_mac,
2740 .ndo_validate_addr = eth_validate_addr,
2741 .ndo_change_mtu = mlx4_en_change_mtu,
2742 .ndo_do_ioctl = mlx4_en_ioctl,
2743 .ndo_tx_timeout = mlx4_en_tx_timeout,
2744 .ndo_vlan_rx_add_vid = mlx4_en_vlan_rx_add_vid,
2745 .ndo_vlan_rx_kill_vid = mlx4_en_vlan_rx_kill_vid,
2746 #ifdef CONFIG_NET_POLL_CONTROLLER
2747 .ndo_poll_controller = mlx4_en_netpoll,
2748 #endif
2749 .ndo_set_features = mlx4_en_set_features,
2750 .ndo_fix_features = mlx4_en_fix_features,
2751 .ndo_setup_tc = __mlx4_en_setup_tc,
2752 #ifdef CONFIG_RFS_ACCEL
2753 .ndo_rx_flow_steer = mlx4_en_filter_rfs,
2754 #endif
2755 .ndo_get_phys_port_id = mlx4_en_get_phys_port_id,
2756 .ndo_udp_tunnel_add = mlx4_en_add_vxlan_port,
2757 .ndo_udp_tunnel_del = mlx4_en_del_vxlan_port,
2758 .ndo_features_check = mlx4_en_features_check,
2759 .ndo_set_tx_maxrate = mlx4_en_set_tx_maxrate,
2760 .ndo_xdp = mlx4_xdp,
2763 static const struct net_device_ops mlx4_netdev_ops_master = {
2764 .ndo_open = mlx4_en_open,
2765 .ndo_stop = mlx4_en_close,
2766 .ndo_start_xmit = mlx4_en_xmit,
2767 .ndo_select_queue = mlx4_en_select_queue,
2768 .ndo_get_stats64 = mlx4_en_get_stats64,
2769 .ndo_set_rx_mode = mlx4_en_set_rx_mode,
2770 .ndo_set_mac_address = mlx4_en_set_mac,
2771 .ndo_validate_addr = eth_validate_addr,
2772 .ndo_change_mtu = mlx4_en_change_mtu,
2773 .ndo_tx_timeout = mlx4_en_tx_timeout,
2774 .ndo_vlan_rx_add_vid = mlx4_en_vlan_rx_add_vid,
2775 .ndo_vlan_rx_kill_vid = mlx4_en_vlan_rx_kill_vid,
2776 .ndo_set_vf_mac = mlx4_en_set_vf_mac,
2777 .ndo_set_vf_vlan = mlx4_en_set_vf_vlan,
2778 .ndo_set_vf_rate = mlx4_en_set_vf_rate,
2779 .ndo_set_vf_spoofchk = mlx4_en_set_vf_spoofchk,
2780 .ndo_set_vf_link_state = mlx4_en_set_vf_link_state,
2781 .ndo_get_vf_stats = mlx4_en_get_vf_stats,
2782 .ndo_get_vf_config = mlx4_en_get_vf_config,
2783 #ifdef CONFIG_NET_POLL_CONTROLLER
2784 .ndo_poll_controller = mlx4_en_netpoll,
2785 #endif
2786 .ndo_set_features = mlx4_en_set_features,
2787 .ndo_fix_features = mlx4_en_fix_features,
2788 .ndo_setup_tc = __mlx4_en_setup_tc,
2789 #ifdef CONFIG_RFS_ACCEL
2790 .ndo_rx_flow_steer = mlx4_en_filter_rfs,
2791 #endif
2792 .ndo_get_phys_port_id = mlx4_en_get_phys_port_id,
2793 .ndo_udp_tunnel_add = mlx4_en_add_vxlan_port,
2794 .ndo_udp_tunnel_del = mlx4_en_del_vxlan_port,
2795 .ndo_features_check = mlx4_en_features_check,
2796 .ndo_set_tx_maxrate = mlx4_en_set_tx_maxrate,
2797 .ndo_xdp = mlx4_xdp,
2800 struct mlx4_en_bond {
2801 struct work_struct work;
2802 struct mlx4_en_priv *priv;
2803 int is_bonded;
2804 struct mlx4_port_map port_map;
2807 static void mlx4_en_bond_work(struct work_struct *work)
2809 struct mlx4_en_bond *bond = container_of(work,
2810 struct mlx4_en_bond,
2811 work);
2812 int err = 0;
2813 struct mlx4_dev *dev = bond->priv->mdev->dev;
2815 if (bond->is_bonded) {
2816 if (!mlx4_is_bonded(dev)) {
2817 err = mlx4_bond(dev);
2818 if (err)
2819 en_err(bond->priv, "Fail to bond device\n");
2821 if (!err) {
2822 err = mlx4_port_map_set(dev, &bond->port_map);
2823 if (err)
2824 en_err(bond->priv, "Fail to set port map [%d][%d]: %d\n",
2825 bond->port_map.port1,
2826 bond->port_map.port2,
2827 err);
2829 } else if (mlx4_is_bonded(dev)) {
2830 err = mlx4_unbond(dev);
2831 if (err)
2832 en_err(bond->priv, "Fail to unbond device\n");
2834 dev_put(bond->priv->dev);
2835 kfree(bond);
2838 static int mlx4_en_queue_bond_work(struct mlx4_en_priv *priv, int is_bonded,
2839 u8 v2p_p1, u8 v2p_p2)
2841 struct mlx4_en_bond *bond = NULL;
2843 bond = kzalloc(sizeof(*bond), GFP_ATOMIC);
2844 if (!bond)
2845 return -ENOMEM;
2847 INIT_WORK(&bond->work, mlx4_en_bond_work);
2848 bond->priv = priv;
2849 bond->is_bonded = is_bonded;
2850 bond->port_map.port1 = v2p_p1;
2851 bond->port_map.port2 = v2p_p2;
2852 dev_hold(priv->dev);
2853 queue_work(priv->mdev->workqueue, &bond->work);
2854 return 0;
2857 int mlx4_en_netdev_event(struct notifier_block *this,
2858 unsigned long event, void *ptr)
2860 struct net_device *ndev = netdev_notifier_info_to_dev(ptr);
2861 u8 port = 0;
2862 struct mlx4_en_dev *mdev;
2863 struct mlx4_dev *dev;
2864 int i, num_eth_ports = 0;
2865 bool do_bond = true;
2866 struct mlx4_en_priv *priv;
2867 u8 v2p_port1 = 0;
2868 u8 v2p_port2 = 0;
2870 if (!net_eq(dev_net(ndev), &init_net))
2871 return NOTIFY_DONE;
2873 mdev = container_of(this, struct mlx4_en_dev, nb);
2874 dev = mdev->dev;
2876 /* Go into this mode only when two network devices set on two ports
2877 * of the same mlx4 device are slaves of the same bonding master
2879 mlx4_foreach_port(i, dev, MLX4_PORT_TYPE_ETH) {
2880 ++num_eth_ports;
2881 if (!port && (mdev->pndev[i] == ndev))
2882 port = i;
2883 mdev->upper[i] = mdev->pndev[i] ?
2884 netdev_master_upper_dev_get(mdev->pndev[i]) : NULL;
2885 /* condition not met: network device is a slave */
2886 if (!mdev->upper[i])
2887 do_bond = false;
2888 if (num_eth_ports < 2)
2889 continue;
2890 /* condition not met: same master */
2891 if (mdev->upper[i] != mdev->upper[i-1])
2892 do_bond = false;
2894 /* condition not met: 2 salves */
2895 do_bond = (num_eth_ports == 2) ? do_bond : false;
2897 /* handle only events that come with enough info */
2898 if ((do_bond && (event != NETDEV_BONDING_INFO)) || !port)
2899 return NOTIFY_DONE;
2901 priv = netdev_priv(ndev);
2902 if (do_bond) {
2903 struct netdev_notifier_bonding_info *notifier_info = ptr;
2904 struct netdev_bonding_info *bonding_info =
2905 &notifier_info->bonding_info;
2907 /* required mode 1, 2 or 4 */
2908 if ((bonding_info->master.bond_mode != BOND_MODE_ACTIVEBACKUP) &&
2909 (bonding_info->master.bond_mode != BOND_MODE_XOR) &&
2910 (bonding_info->master.bond_mode != BOND_MODE_8023AD))
2911 do_bond = false;
2913 /* require exactly 2 slaves */
2914 if (bonding_info->master.num_slaves != 2)
2915 do_bond = false;
2917 /* calc v2p */
2918 if (do_bond) {
2919 if (bonding_info->master.bond_mode ==
2920 BOND_MODE_ACTIVEBACKUP) {
2921 /* in active-backup mode virtual ports are
2922 * mapped to the physical port of the active
2923 * slave */
2924 if (bonding_info->slave.state ==
2925 BOND_STATE_BACKUP) {
2926 if (port == 1) {
2927 v2p_port1 = 2;
2928 v2p_port2 = 2;
2929 } else {
2930 v2p_port1 = 1;
2931 v2p_port2 = 1;
2933 } else { /* BOND_STATE_ACTIVE */
2934 if (port == 1) {
2935 v2p_port1 = 1;
2936 v2p_port2 = 1;
2937 } else {
2938 v2p_port1 = 2;
2939 v2p_port2 = 2;
2942 } else { /* Active-Active */
2943 /* in active-active mode a virtual port is
2944 * mapped to the native physical port if and only
2945 * if the physical port is up */
2946 __s8 link = bonding_info->slave.link;
2948 if (port == 1)
2949 v2p_port2 = 2;
2950 else
2951 v2p_port1 = 1;
2952 if ((link == BOND_LINK_UP) ||
2953 (link == BOND_LINK_FAIL)) {
2954 if (port == 1)
2955 v2p_port1 = 1;
2956 else
2957 v2p_port2 = 2;
2958 } else { /* BOND_LINK_DOWN || BOND_LINK_BACK */
2959 if (port == 1)
2960 v2p_port1 = 2;
2961 else
2962 v2p_port2 = 1;
2968 mlx4_en_queue_bond_work(priv, do_bond,
2969 v2p_port1, v2p_port2);
2971 return NOTIFY_DONE;
2974 void mlx4_en_update_pfc_stats_bitmap(struct mlx4_dev *dev,
2975 struct mlx4_en_stats_bitmap *stats_bitmap,
2976 u8 rx_ppp, u8 rx_pause,
2977 u8 tx_ppp, u8 tx_pause)
2979 int last_i = NUM_MAIN_STATS + NUM_PORT_STATS + NUM_PF_STATS;
2981 if (!mlx4_is_slave(dev) &&
2982 (dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_FLOWSTATS_EN)) {
2983 mutex_lock(&stats_bitmap->mutex);
2984 bitmap_clear(stats_bitmap->bitmap, last_i, NUM_FLOW_STATS);
2986 if (rx_ppp)
2987 bitmap_set(stats_bitmap->bitmap, last_i,
2988 NUM_FLOW_PRIORITY_STATS_RX);
2989 last_i += NUM_FLOW_PRIORITY_STATS_RX;
2991 if (rx_pause && !(rx_ppp))
2992 bitmap_set(stats_bitmap->bitmap, last_i,
2993 NUM_FLOW_STATS_RX);
2994 last_i += NUM_FLOW_STATS_RX;
2996 if (tx_ppp)
2997 bitmap_set(stats_bitmap->bitmap, last_i,
2998 NUM_FLOW_PRIORITY_STATS_TX);
2999 last_i += NUM_FLOW_PRIORITY_STATS_TX;
3001 if (tx_pause && !(tx_ppp))
3002 bitmap_set(stats_bitmap->bitmap, last_i,
3003 NUM_FLOW_STATS_TX);
3004 last_i += NUM_FLOW_STATS_TX;
3006 mutex_unlock(&stats_bitmap->mutex);
3010 void mlx4_en_set_stats_bitmap(struct mlx4_dev *dev,
3011 struct mlx4_en_stats_bitmap *stats_bitmap,
3012 u8 rx_ppp, u8 rx_pause,
3013 u8 tx_ppp, u8 tx_pause)
3015 int last_i = 0;
3017 mutex_init(&stats_bitmap->mutex);
3018 bitmap_zero(stats_bitmap->bitmap, NUM_ALL_STATS);
3020 if (mlx4_is_slave(dev)) {
3021 bitmap_set(stats_bitmap->bitmap, last_i +
3022 MLX4_FIND_NETDEV_STAT(rx_packets), 1);
3023 bitmap_set(stats_bitmap->bitmap, last_i +
3024 MLX4_FIND_NETDEV_STAT(tx_packets), 1);
3025 bitmap_set(stats_bitmap->bitmap, last_i +
3026 MLX4_FIND_NETDEV_STAT(rx_bytes), 1);
3027 bitmap_set(stats_bitmap->bitmap, last_i +
3028 MLX4_FIND_NETDEV_STAT(tx_bytes), 1);
3029 bitmap_set(stats_bitmap->bitmap, last_i +
3030 MLX4_FIND_NETDEV_STAT(rx_dropped), 1);
3031 bitmap_set(stats_bitmap->bitmap, last_i +
3032 MLX4_FIND_NETDEV_STAT(tx_dropped), 1);
3033 } else {
3034 bitmap_set(stats_bitmap->bitmap, last_i, NUM_MAIN_STATS);
3036 last_i += NUM_MAIN_STATS;
3038 bitmap_set(stats_bitmap->bitmap, last_i, NUM_PORT_STATS);
3039 last_i += NUM_PORT_STATS;
3041 if (mlx4_is_master(dev))
3042 bitmap_set(stats_bitmap->bitmap, last_i,
3043 NUM_PF_STATS);
3044 last_i += NUM_PF_STATS;
3046 mlx4_en_update_pfc_stats_bitmap(dev, stats_bitmap,
3047 rx_ppp, rx_pause,
3048 tx_ppp, tx_pause);
3049 last_i += NUM_FLOW_STATS;
3051 if (!mlx4_is_slave(dev))
3052 bitmap_set(stats_bitmap->bitmap, last_i, NUM_PKT_STATS);
3055 int mlx4_en_init_netdev(struct mlx4_en_dev *mdev, int port,
3056 struct mlx4_en_port_profile *prof)
3058 struct net_device *dev;
3059 struct mlx4_en_priv *priv;
3060 int i;
3061 int err;
3063 dev = alloc_etherdev_mqs(sizeof(struct mlx4_en_priv),
3064 MAX_TX_RINGS, MAX_RX_RINGS);
3065 if (dev == NULL)
3066 return -ENOMEM;
3068 netif_set_real_num_tx_queues(dev, prof->tx_ring_num);
3069 netif_set_real_num_rx_queues(dev, prof->rx_ring_num);
3071 SET_NETDEV_DEV(dev, &mdev->dev->persist->pdev->dev);
3072 dev->dev_port = port - 1;
3075 * Initialize driver private data
3078 priv = netdev_priv(dev);
3079 memset(priv, 0, sizeof(struct mlx4_en_priv));
3080 priv->counter_index = MLX4_SINK_COUNTER_INDEX(mdev->dev);
3081 spin_lock_init(&priv->stats_lock);
3082 INIT_WORK(&priv->rx_mode_task, mlx4_en_do_set_rx_mode);
3083 INIT_WORK(&priv->watchdog_task, mlx4_en_restart);
3084 INIT_WORK(&priv->linkstate_task, mlx4_en_linkstate);
3085 INIT_DELAYED_WORK(&priv->stats_task, mlx4_en_do_get_stats);
3086 INIT_DELAYED_WORK(&priv->service_task, mlx4_en_service_task);
3087 INIT_WORK(&priv->vxlan_add_task, mlx4_en_add_vxlan_offloads);
3088 INIT_WORK(&priv->vxlan_del_task, mlx4_en_del_vxlan_offloads);
3089 #ifdef CONFIG_RFS_ACCEL
3090 INIT_LIST_HEAD(&priv->filters);
3091 spin_lock_init(&priv->filters_lock);
3092 #endif
3094 priv->dev = dev;
3095 priv->mdev = mdev;
3096 priv->ddev = &mdev->pdev->dev;
3097 priv->prof = prof;
3098 priv->port = port;
3099 priv->port_up = false;
3100 priv->flags = prof->flags;
3101 priv->pflags = MLX4_EN_PRIV_FLAGS_BLUEFLAME;
3102 priv->ctrl_flags = cpu_to_be32(MLX4_WQE_CTRL_CQ_UPDATE |
3103 MLX4_WQE_CTRL_SOLICITED);
3104 priv->num_tx_rings_p_up = mdev->profile.num_tx_rings_p_up;
3105 priv->tx_ring_num = prof->tx_ring_num;
3106 priv->tx_work_limit = MLX4_EN_DEFAULT_TX_WORK;
3107 netdev_rss_key_fill(priv->rss_key, sizeof(priv->rss_key));
3109 priv->tx_ring = kzalloc(sizeof(struct mlx4_en_tx_ring *) * MAX_TX_RINGS,
3110 GFP_KERNEL);
3111 if (!priv->tx_ring) {
3112 err = -ENOMEM;
3113 goto out;
3115 priv->tx_cq = kzalloc(sizeof(struct mlx4_en_cq *) * MAX_TX_RINGS,
3116 GFP_KERNEL);
3117 if (!priv->tx_cq) {
3118 err = -ENOMEM;
3119 goto out;
3121 priv->rx_ring_num = prof->rx_ring_num;
3122 priv->cqe_factor = (mdev->dev->caps.cqe_size == 64) ? 1 : 0;
3123 priv->cqe_size = mdev->dev->caps.cqe_size;
3124 priv->mac_index = -1;
3125 priv->msg_enable = MLX4_EN_MSG_LEVEL;
3126 #ifdef CONFIG_MLX4_EN_DCB
3127 if (!mlx4_is_slave(priv->mdev->dev)) {
3128 priv->dcbx_cap = DCB_CAP_DCBX_VER_CEE | DCB_CAP_DCBX_HOST |
3129 DCB_CAP_DCBX_VER_IEEE;
3130 priv->flags |= MLX4_EN_DCB_ENABLED;
3131 priv->cee_config.pfc_state = false;
3133 for (i = 0; i < MLX4_EN_NUM_UP; i++)
3134 priv->cee_config.dcb_pfc[i] = pfc_disabled;
3136 if (mdev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_ETS_CFG) {
3137 dev->dcbnl_ops = &mlx4_en_dcbnl_ops;
3138 } else {
3139 en_info(priv, "enabling only PFC DCB ops\n");
3140 dev->dcbnl_ops = &mlx4_en_dcbnl_pfc_ops;
3143 #endif
3145 for (i = 0; i < MLX4_EN_MAC_HASH_SIZE; ++i)
3146 INIT_HLIST_HEAD(&priv->mac_hash[i]);
3148 /* Query for default mac and max mtu */
3149 priv->max_mtu = mdev->dev->caps.eth_mtu_cap[priv->port];
3151 if (mdev->dev->caps.rx_checksum_flags_port[priv->port] &
3152 MLX4_RX_CSUM_MODE_VAL_NON_TCP_UDP)
3153 priv->flags |= MLX4_EN_FLAG_RX_CSUM_NON_TCP_UDP;
3155 /* Set default MAC */
3156 dev->addr_len = ETH_ALEN;
3157 mlx4_en_u64_to_mac(dev->dev_addr, mdev->dev->caps.def_mac[priv->port]);
3158 if (!is_valid_ether_addr(dev->dev_addr)) {
3159 en_err(priv, "Port: %d, invalid mac burned: %pM, quiting\n",
3160 priv->port, dev->dev_addr);
3161 err = -EINVAL;
3162 goto out;
3163 } else if (mlx4_is_slave(priv->mdev->dev) &&
3164 (priv->mdev->dev->port_random_macs & 1 << priv->port)) {
3165 /* Random MAC was assigned in mlx4_slave_cap
3166 * in mlx4_core module
3168 dev->addr_assign_type |= NET_ADDR_RANDOM;
3169 en_warn(priv, "Assigned random MAC address %pM\n", dev->dev_addr);
3172 memcpy(priv->current_mac, dev->dev_addr, sizeof(priv->current_mac));
3174 priv->stride = roundup_pow_of_two(sizeof(struct mlx4_en_rx_desc) +
3175 DS_SIZE * MLX4_EN_MAX_RX_FRAGS);
3176 err = mlx4_en_alloc_resources(priv);
3177 if (err)
3178 goto out;
3180 /* Initialize time stamping config */
3181 priv->hwtstamp_config.flags = 0;
3182 priv->hwtstamp_config.tx_type = HWTSTAMP_TX_OFF;
3183 priv->hwtstamp_config.rx_filter = HWTSTAMP_FILTER_NONE;
3185 /* Allocate page for receive rings */
3186 err = mlx4_alloc_hwq_res(mdev->dev, &priv->res,
3187 MLX4_EN_PAGE_SIZE);
3188 if (err) {
3189 en_err(priv, "Failed to allocate page for rx qps\n");
3190 goto out;
3192 priv->allocated = 1;
3195 * Initialize netdev entry points
3197 if (mlx4_is_master(priv->mdev->dev))
3198 dev->netdev_ops = &mlx4_netdev_ops_master;
3199 else
3200 dev->netdev_ops = &mlx4_netdev_ops;
3201 dev->watchdog_timeo = MLX4_EN_WATCHDOG_TIMEOUT;
3202 netif_set_real_num_tx_queues(dev, priv->tx_ring_num);
3203 netif_set_real_num_rx_queues(dev, priv->rx_ring_num);
3205 dev->ethtool_ops = &mlx4_en_ethtool_ops;
3208 * Set driver features
3210 dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM;
3211 if (mdev->LSO_support)
3212 dev->hw_features |= NETIF_F_TSO | NETIF_F_TSO6;
3214 dev->vlan_features = dev->hw_features;
3216 dev->hw_features |= NETIF_F_RXCSUM | NETIF_F_RXHASH;
3217 dev->features = dev->hw_features | NETIF_F_HIGHDMA |
3218 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX |
3219 NETIF_F_HW_VLAN_CTAG_FILTER;
3220 dev->hw_features |= NETIF_F_LOOPBACK |
3221 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX;
3223 if (!(mdev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_SKIP_OUTER_VLAN)) {
3224 dev->features |= NETIF_F_HW_VLAN_STAG_RX |
3225 NETIF_F_HW_VLAN_STAG_FILTER;
3226 dev->hw_features |= NETIF_F_HW_VLAN_STAG_RX;
3229 if (mlx4_is_slave(mdev->dev)) {
3230 bool vlan_offload_disabled;
3231 int phv;
3233 err = get_phv_bit(mdev->dev, port, &phv);
3234 if (!err && phv) {
3235 dev->hw_features |= NETIF_F_HW_VLAN_STAG_TX;
3236 priv->pflags |= MLX4_EN_PRIV_FLAGS_PHV;
3238 err = mlx4_get_is_vlan_offload_disabled(mdev->dev, port,
3239 &vlan_offload_disabled);
3240 if (!err && vlan_offload_disabled) {
3241 dev->hw_features &= ~(NETIF_F_HW_VLAN_CTAG_TX |
3242 NETIF_F_HW_VLAN_CTAG_RX |
3243 NETIF_F_HW_VLAN_STAG_TX |
3244 NETIF_F_HW_VLAN_STAG_RX);
3245 dev->features &= ~(NETIF_F_HW_VLAN_CTAG_TX |
3246 NETIF_F_HW_VLAN_CTAG_RX |
3247 NETIF_F_HW_VLAN_STAG_TX |
3248 NETIF_F_HW_VLAN_STAG_RX);
3250 } else {
3251 if (mdev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_PHV_EN &&
3252 !(mdev->dev->caps.flags2 &
3253 MLX4_DEV_CAP_FLAG2_SKIP_OUTER_VLAN))
3254 dev->hw_features |= NETIF_F_HW_VLAN_STAG_TX;
3257 if (mdev->dev->caps.flags & MLX4_DEV_CAP_FLAG_FCS_KEEP)
3258 dev->hw_features |= NETIF_F_RXFCS;
3260 if (mdev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_IGNORE_FCS)
3261 dev->hw_features |= NETIF_F_RXALL;
3263 if (mdev->dev->caps.steering_mode ==
3264 MLX4_STEERING_MODE_DEVICE_MANAGED &&
3265 mdev->dev->caps.dmfs_high_steer_mode != MLX4_STEERING_DMFS_A0_STATIC)
3266 dev->hw_features |= NETIF_F_NTUPLE;
3268 if (mdev->dev->caps.steering_mode != MLX4_STEERING_MODE_A0)
3269 dev->priv_flags |= IFF_UNICAST_FLT;
3271 /* Setting a default hash function value */
3272 if (mdev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_RSS_TOP) {
3273 priv->rss_hash_fn = ETH_RSS_HASH_TOP;
3274 } else if (mdev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_RSS_XOR) {
3275 priv->rss_hash_fn = ETH_RSS_HASH_XOR;
3276 } else {
3277 en_warn(priv,
3278 "No RSS hash capabilities exposed, using Toeplitz\n");
3279 priv->rss_hash_fn = ETH_RSS_HASH_TOP;
3282 if (mdev->dev->caps.tunnel_offload_mode == MLX4_TUNNEL_OFFLOAD_MODE_VXLAN) {
3283 dev->hw_features |= NETIF_F_GSO_UDP_TUNNEL |
3284 NETIF_F_GSO_UDP_TUNNEL_CSUM |
3285 NETIF_F_GSO_PARTIAL;
3286 dev->features |= NETIF_F_GSO_UDP_TUNNEL |
3287 NETIF_F_GSO_UDP_TUNNEL_CSUM |
3288 NETIF_F_GSO_PARTIAL;
3289 dev->gso_partial_features = NETIF_F_GSO_UDP_TUNNEL_CSUM;
3292 mdev->pndev[port] = dev;
3293 mdev->upper[port] = NULL;
3295 netif_carrier_off(dev);
3296 mlx4_en_set_default_moderation(priv);
3298 en_warn(priv, "Using %d TX rings\n", prof->tx_ring_num);
3299 en_warn(priv, "Using %d RX rings\n", prof->rx_ring_num);
3301 mlx4_en_update_loopback_state(priv->dev, priv->dev->features);
3303 /* Configure port */
3304 mlx4_en_calc_rx_buf(dev);
3305 err = mlx4_SET_PORT_general(mdev->dev, priv->port,
3306 priv->rx_skb_size + ETH_FCS_LEN,
3307 prof->tx_pause, prof->tx_ppp,
3308 prof->rx_pause, prof->rx_ppp);
3309 if (err) {
3310 en_err(priv, "Failed setting port general configurations for port %d, with error %d\n",
3311 priv->port, err);
3312 goto out;
3315 if (mdev->dev->caps.tunnel_offload_mode == MLX4_TUNNEL_OFFLOAD_MODE_VXLAN) {
3316 err = mlx4_SET_PORT_VXLAN(mdev->dev, priv->port, VXLAN_STEER_BY_OUTER_MAC, 1);
3317 if (err) {
3318 en_err(priv, "Failed setting port L2 tunnel configuration, err %d\n",
3319 err);
3320 goto out;
3324 /* Init port */
3325 en_warn(priv, "Initializing port\n");
3326 err = mlx4_INIT_PORT(mdev->dev, priv->port);
3327 if (err) {
3328 en_err(priv, "Failed Initializing port\n");
3329 goto out;
3331 queue_delayed_work(mdev->workqueue, &priv->stats_task, STATS_DELAY);
3333 /* Initialize time stamp mechanism */
3334 if (mdev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_TS)
3335 mlx4_en_init_timestamp(mdev);
3337 queue_delayed_work(mdev->workqueue, &priv->service_task,
3338 SERVICE_TASK_DELAY);
3340 mlx4_en_set_stats_bitmap(mdev->dev, &priv->stats_bitmap,
3341 mdev->profile.prof[priv->port].rx_ppp,
3342 mdev->profile.prof[priv->port].rx_pause,
3343 mdev->profile.prof[priv->port].tx_ppp,
3344 mdev->profile.prof[priv->port].tx_pause);
3346 err = register_netdev(dev);
3347 if (err) {
3348 en_err(priv, "Netdev registration failed for port %d\n", port);
3349 goto out;
3352 priv->registered = 1;
3353 devlink_port_type_eth_set(mlx4_get_devlink_port(mdev->dev, priv->port),
3354 dev);
3356 return 0;
3358 out:
3359 mlx4_en_destroy_netdev(dev);
3360 return err;
3363 int mlx4_en_reset_config(struct net_device *dev,
3364 struct hwtstamp_config ts_config,
3365 netdev_features_t features)
3367 struct mlx4_en_priv *priv = netdev_priv(dev);
3368 struct mlx4_en_dev *mdev = priv->mdev;
3369 struct mlx4_en_port_profile new_prof;
3370 struct mlx4_en_priv *tmp;
3371 int port_up = 0;
3372 int err = 0;
3374 if (priv->hwtstamp_config.tx_type == ts_config.tx_type &&
3375 priv->hwtstamp_config.rx_filter == ts_config.rx_filter &&
3376 !DEV_FEATURE_CHANGED(dev, features, NETIF_F_HW_VLAN_CTAG_RX) &&
3377 !DEV_FEATURE_CHANGED(dev, features, NETIF_F_RXFCS))
3378 return 0; /* Nothing to change */
3380 if (DEV_FEATURE_CHANGED(dev, features, NETIF_F_HW_VLAN_CTAG_RX) &&
3381 (features & NETIF_F_HW_VLAN_CTAG_RX) &&
3382 (priv->hwtstamp_config.rx_filter != HWTSTAMP_FILTER_NONE)) {
3383 en_warn(priv, "Can't turn ON rx vlan offload while time-stamping rx filter is ON\n");
3384 return -EINVAL;
3387 tmp = kzalloc(sizeof(*tmp), GFP_KERNEL);
3388 if (!tmp)
3389 return -ENOMEM;
3391 mutex_lock(&mdev->state_lock);
3393 memcpy(&new_prof, priv->prof, sizeof(struct mlx4_en_port_profile));
3394 memcpy(&new_prof.hwtstamp_config, &ts_config, sizeof(ts_config));
3396 err = mlx4_en_try_alloc_resources(priv, tmp, &new_prof);
3397 if (err)
3398 goto out;
3400 if (priv->port_up) {
3401 port_up = 1;
3402 mlx4_en_stop_port(dev, 1);
3405 en_warn(priv, "Changing device configuration rx filter(%x) rx vlan(%x)\n",
3406 ts_config.rx_filter,
3407 !!(features & NETIF_F_HW_VLAN_CTAG_RX));
3409 mlx4_en_safe_replace_resources(priv, tmp);
3411 if (DEV_FEATURE_CHANGED(dev, features, NETIF_F_HW_VLAN_CTAG_RX)) {
3412 if (features & NETIF_F_HW_VLAN_CTAG_RX)
3413 dev->features |= NETIF_F_HW_VLAN_CTAG_RX;
3414 else
3415 dev->features &= ~NETIF_F_HW_VLAN_CTAG_RX;
3416 } else if (ts_config.rx_filter == HWTSTAMP_FILTER_NONE) {
3417 /* RX time-stamping is OFF, update the RX vlan offload
3418 * to the latest wanted state
3420 if (dev->wanted_features & NETIF_F_HW_VLAN_CTAG_RX)
3421 dev->features |= NETIF_F_HW_VLAN_CTAG_RX;
3422 else
3423 dev->features &= ~NETIF_F_HW_VLAN_CTAG_RX;
3426 if (DEV_FEATURE_CHANGED(dev, features, NETIF_F_RXFCS)) {
3427 if (features & NETIF_F_RXFCS)
3428 dev->features |= NETIF_F_RXFCS;
3429 else
3430 dev->features &= ~NETIF_F_RXFCS;
3433 /* RX vlan offload and RX time-stamping can't co-exist !
3434 * Regardless of the caller's choice,
3435 * Turn Off RX vlan offload in case of time-stamping is ON
3437 if (ts_config.rx_filter != HWTSTAMP_FILTER_NONE) {
3438 if (dev->features & NETIF_F_HW_VLAN_CTAG_RX)
3439 en_warn(priv, "Turning off RX vlan offload since RX time-stamping is ON\n");
3440 dev->features &= ~NETIF_F_HW_VLAN_CTAG_RX;
3443 if (port_up) {
3444 err = mlx4_en_start_port(dev);
3445 if (err)
3446 en_err(priv, "Failed starting port\n");
3449 out:
3450 mutex_unlock(&mdev->state_lock);
3451 kfree(tmp);
3452 if (!err)
3453 netdev_features_change(dev);
3454 return err;