GNU Linux-libre 4.9.337-gnu1
[releases.git] / drivers / net / ethernet / mellanox / mlx4 / en_netdev.c
1 /*
2  * Copyright (c) 2007 Mellanox Technologies. All rights reserved.
3  *
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:
9  *
10  *     Redistribution and use in source and binary forms, with or
11  *     without modification, are permitted provided that the following
12  *     conditions are met:
13  *
14  *      - Redistributions of source code must retain the above
15  *        copyright notice, this list of conditions and the following
16  *        disclaimer.
17  *
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.
22  *
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.
31  *
32  */
33
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>
45
46 #include <linux/mlx4/driver.h>
47 #include <linux/mlx4/device.h>
48 #include <linux/mlx4/cmd.h>
49 #include <linux/mlx4/cq.h>
50
51 #include "mlx4_en.h"
52 #include "en_port.h"
53
54 int mlx4_en_setup_tc(struct net_device *dev, u8 up)
55 {
56         struct mlx4_en_priv *priv = netdev_priv(dev);
57         int i;
58         unsigned int offset = 0;
59
60         if (up && up != MLX4_EN_NUM_UP)
61                 return -EINVAL;
62
63         netdev_set_num_tc(dev, up);
64
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;
69         }
70
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;
79                 }
80         }
81 #endif /* CONFIG_MLX4_EN_DCB */
82
83         return 0;
84 }
85
86 static int __mlx4_en_setup_tc(struct net_device *dev, u32 handle, __be16 proto,
87                               struct tc_to_netdev *tc)
88 {
89         if (tc->type != TC_SETUP_MQPRIO)
90                 return -EINVAL;
91
92         return mlx4_en_setup_tc(dev, tc->tc);
93 }
94
95 #ifdef CONFIG_RFS_ACCEL
96
97 struct mlx4_en_filter {
98         struct list_head next;
99         struct work_struct work;
100
101         u8     ip_proto;
102         __be32 src_ip;
103         __be32 dst_ip;
104         __be16 src_port;
105         __be16 dst_port;
106
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
114                                          */
115         struct hlist_node filter_chain;
116 };
117
118 static void mlx4_en_filter_rfs_expire(struct mlx4_en_priv *priv);
119
120 static enum mlx4_net_trans_rule_id mlx4_ip_proto_to_trans_rule_id(u8 ip_proto)
121 {
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;
129         }
130 };
131
132 /* Must not acquire state_lock, as its corresponding work_sync
133  * is done under it.
134  */
135 static void mlx4_en_filter_work(struct work_struct *work)
136 {
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),
143                 {
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,
149                         },
150                 },
151         };
152         struct mlx4_spec_list spec_ip = {
153                 .id = MLX4_NET_TRANS_RULE_ID_IPV4,
154                 {
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,
160                         },
161                 },
162         };
163         struct mlx4_spec_list spec_eth = {
164                 .id = MLX4_NET_TRANS_RULE_ID_ETH,
165         };
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,
174         };
175         int rc;
176         __be64 mac_mask = cpu_to_be64(MLX4_MAC_MASK << 16);
177
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;
182         }
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);
186
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);
190
191         filter->activated = 0;
192
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);
197         }
198
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);
202
203 ignore:
204         mlx4_en_filter_rfs_expire(priv);
205
206         filter->activated = 1;
207 }
208
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)
212 {
213         unsigned long l;
214         int bucket_idx;
215
216         l = (__force unsigned long)src_port |
217             ((__force unsigned long)dst_port << 2);
218         l ^= (__force unsigned long)(src_ip ^ dst_ip);
219
220         bucket_idx = hash_long(l, MLX4_EN_FILTER_HASH_SHIFT);
221
222         return &priv->filter_hash[bucket_idx];
223 }
224
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)
229 {
230         struct mlx4_en_filter *filter = NULL;
231
232         filter = kzalloc(sizeof(struct mlx4_en_filter), GFP_ATOMIC);
233         if (!filter)
234                 return NULL;
235
236         filter->priv = priv;
237         filter->rxq_index = rxq_index;
238         INIT_WORK(&filter->work, mlx4_en_filter_work);
239
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;
245
246         filter->flow_id = flow_id;
247
248         filter->id = priv->last_filter_id++ % RPS_NO_FILTER;
249
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));
254
255         return filter;
256 }
257
258 static void mlx4_en_filter_free(struct mlx4_en_filter *filter)
259 {
260         struct mlx4_en_priv *priv = filter->priv;
261         int rc;
262
263         list_del(&filter->next);
264
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);
268
269         kfree(filter);
270 }
271
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)
275 {
276         struct mlx4_en_filter *filter;
277         struct mlx4_en_filter *ret = NULL;
278
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;
290                 }
291         }
292
293         return ret;
294 }
295
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)
299 {
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;
311
312         if (skb->encapsulation)
313                 return -EPROTONOSUPPORT;
314
315         if (skb->protocol != htons(ETH_P_IP))
316                 return -EPROTONOSUPPORT;
317
318         ip = (const struct iphdr *)(skb->data + nhoff);
319         if (ip_is_fragment(ip))
320                 return -EPROTONOSUPPORT;
321
322         if ((ip->protocol != IPPROTO_TCP) && (ip->protocol != IPPROTO_UDP))
323                 return -EPROTONOSUPPORT;
324         ports = (const __be16 *)(skb->data + nhoff + 4 * ip->ihl);
325
326         ip_proto = ip->protocol;
327         src_ip = ip->saddr;
328         dst_ip = ip->daddr;
329         src_port = ports[0];
330         dst_port = ports[1];
331
332         spin_lock_bh(&priv->filters_lock);
333         filter = mlx4_en_filter_find(priv, src_ip, dst_ip, ip_proto,
334                                      src_port, dst_port);
335         if (filter) {
336                 if (filter->rxq_index == rxq_index)
337                         goto out;
338
339                 filter->rxq_index = rxq_index;
340         } else {
341                 filter = mlx4_en_filter_alloc(priv, rxq_index,
342                                               src_ip, dst_ip, ip_proto,
343                                               src_port, dst_port, flow_id);
344                 if (!filter) {
345                         ret = -ENOMEM;
346                         goto err;
347                 }
348         }
349
350         queue_work(priv->mdev->workqueue, &filter->work);
351
352 out:
353         ret = filter->id;
354 err:
355         spin_unlock_bh(&priv->filters_lock);
356
357         return ret;
358 }
359
360 void mlx4_en_cleanup_filters(struct mlx4_en_priv *priv)
361 {
362         struct mlx4_en_filter *filter, *tmp;
363         LIST_HEAD(del_list);
364
365         spin_lock_bh(&priv->filters_lock);
366         list_for_each_entry_safe(filter, tmp, &priv->filters, next) {
367                 list_move(&filter->next, &del_list);
368                 hlist_del(&filter->filter_chain);
369         }
370         spin_unlock_bh(&priv->filters_lock);
371
372         list_for_each_entry_safe(filter, tmp, &del_list, next) {
373                 cancel_work_sync(&filter->work);
374                 mlx4_en_filter_free(filter);
375         }
376 }
377
378 static void mlx4_en_filter_rfs_expire(struct mlx4_en_priv *priv)
379 {
380         struct mlx4_en_filter *filter = NULL, *tmp, *last_filter = NULL;
381         LIST_HEAD(del_list);
382         int i = 0;
383
384         spin_lock_bh(&priv->filters_lock);
385         list_for_each_entry_safe(filter, tmp, &priv->filters, next) {
386                 if (i > MLX4_EN_FILTER_EXPIRY_QUOTA)
387                         break;
388
389                 if (filter->activated &&
390                     !work_pending(&filter->work) &&
391                     rps_may_expire_flow(priv->dev,
392                                         filter->rxq_index, filter->flow_id,
393                                         filter->id)) {
394                         list_move(&filter->next, &del_list);
395                         hlist_del(&filter->filter_chain);
396                 } else
397                         last_filter = filter;
398
399                 i++;
400         }
401
402         if (last_filter && (&last_filter->next != priv->filters.next))
403                 list_move(&priv->filters, &last_filter->next);
404
405         spin_unlock_bh(&priv->filters_lock);
406
407         list_for_each_entry_safe(filter, tmp, &del_list, next)
408                 mlx4_en_filter_free(filter);
409 }
410 #endif
411
412 static int mlx4_en_vlan_rx_add_vid(struct net_device *dev,
413                                    __be16 proto, u16 vid)
414 {
415         struct mlx4_en_priv *priv = netdev_priv(dev);
416         struct mlx4_en_dev *mdev = priv->mdev;
417         int err;
418         int idx;
419
420         en_dbg(HW, priv, "adding VLAN:%d\n", vid);
421
422         set_bit(vid, priv->active_vlans);
423
424         /* Add VID to port VLAN filter */
425         mutex_lock(&mdev->state_lock);
426         if (mdev->device_up && priv->port_up) {
427                 err = mlx4_SET_VLAN_FLTR(mdev->dev, priv);
428                 if (err) {
429                         en_err(priv, "Failed configuring VLAN filter\n");
430                         goto out;
431                 }
432         }
433         err = mlx4_register_vlan(mdev->dev, priv->port, vid, &idx);
434         if (err)
435                 en_dbg(HW, priv, "Failed adding vlan %d\n", vid);
436
437 out:
438         mutex_unlock(&mdev->state_lock);
439         return err;
440 }
441
442 static int mlx4_en_vlan_rx_kill_vid(struct net_device *dev,
443                                     __be16 proto, u16 vid)
444 {
445         struct mlx4_en_priv *priv = netdev_priv(dev);
446         struct mlx4_en_dev *mdev = priv->mdev;
447         int err = 0;
448
449         en_dbg(HW, priv, "Killing VID:%d\n", vid);
450
451         clear_bit(vid, priv->active_vlans);
452
453         /* Remove VID from port VLAN filter */
454         mutex_lock(&mdev->state_lock);
455         mlx4_unregister_vlan(mdev->dev, priv->port, vid);
456
457         if (mdev->device_up && priv->port_up) {
458                 err = mlx4_SET_VLAN_FLTR(mdev->dev, priv);
459                 if (err)
460                         en_err(priv, "Failed configuring VLAN filter\n");
461         }
462         mutex_unlock(&mdev->state_lock);
463
464         return err;
465 }
466
467 static void mlx4_en_u64_to_mac(unsigned char dst_mac[ETH_ALEN + 2], u64 src_mac)
468 {
469         int i;
470         for (i = ETH_ALEN - 1; i >= 0; --i) {
471                 dst_mac[i] = src_mac & 0xff;
472                 src_mac >>= 8;
473         }
474         memset(&dst_mac[ETH_ALEN], 0, 2);
475 }
476
477
478 static int mlx4_en_tunnel_steer_add(struct mlx4_en_priv *priv, unsigned char *addr,
479                                     int qpn, u64 *reg_id)
480 {
481         int err;
482
483         if (priv->mdev->dev->caps.tunnel_offload_mode != MLX4_TUNNEL_OFFLOAD_MODE_VXLAN ||
484             priv->mdev->dev->caps.dmfs_high_steer_mode == MLX4_STEERING_DMFS_A0_STATIC)
485                 return 0; /* do nothing */
486
487         err = mlx4_tunnel_steer_add(priv->mdev->dev, addr, priv->port, qpn,
488                                     MLX4_DOMAIN_NIC, reg_id);
489         if (err) {
490                 en_err(priv, "failed to add vxlan steering rule, err %d\n", err);
491                 return err;
492         }
493         en_dbg(DRV, priv, "added vxlan steering rule, mac %pM reg_id %llx\n", addr, *reg_id);
494         return 0;
495 }
496
497
498 static int mlx4_en_uc_steer_add(struct mlx4_en_priv *priv,
499                                 unsigned char *mac, int *qpn, u64 *reg_id)
500 {
501         struct mlx4_en_dev *mdev = priv->mdev;
502         struct mlx4_dev *dev = mdev->dev;
503         int err;
504
505         switch (dev->caps.steering_mode) {
506         case MLX4_STEERING_MODE_B0: {
507                 struct mlx4_qp qp;
508                 u8 gid[16] = {0};
509
510                 qp.qpn = *qpn;
511                 memcpy(&gid[10], mac, ETH_ALEN);
512                 gid[5] = priv->port;
513
514                 err = mlx4_unicast_attach(dev, &qp, gid, 0, MLX4_PROT_ETH);
515                 break;
516         }
517         case MLX4_STEERING_MODE_DEVICE_MANAGED: {
518                 struct mlx4_spec_list spec_eth = { {NULL} };
519                 __be64 mac_mask = cpu_to_be64(MLX4_MAC_MASK << 16);
520
521                 struct mlx4_net_trans_rule rule = {
522                         .queue_mode = MLX4_NET_TRANS_Q_FIFO,
523                         .exclusive = 0,
524                         .allow_loopback = 1,
525                         .promisc_mode = MLX4_FS_REGULAR,
526                         .priority = MLX4_DOMAIN_NIC,
527                 };
528
529                 rule.port = priv->port;
530                 rule.qpn = *qpn;
531                 INIT_LIST_HEAD(&rule.list);
532
533                 spec_eth.id = MLX4_NET_TRANS_RULE_ID_ETH;
534                 memcpy(spec_eth.eth.dst_mac, mac, ETH_ALEN);
535                 memcpy(spec_eth.eth.dst_mac_msk, &mac_mask, ETH_ALEN);
536                 list_add_tail(&spec_eth.list, &rule.list);
537
538                 err = mlx4_flow_attach(dev, &rule, reg_id);
539                 break;
540         }
541         default:
542                 return -EINVAL;
543         }
544         if (err)
545                 en_warn(priv, "Failed Attaching Unicast\n");
546
547         return err;
548 }
549
550 static void mlx4_en_uc_steer_release(struct mlx4_en_priv *priv,
551                                      unsigned char *mac, int qpn, u64 reg_id)
552 {
553         struct mlx4_en_dev *mdev = priv->mdev;
554         struct mlx4_dev *dev = mdev->dev;
555
556         switch (dev->caps.steering_mode) {
557         case MLX4_STEERING_MODE_B0: {
558                 struct mlx4_qp qp;
559                 u8 gid[16] = {0};
560
561                 qp.qpn = qpn;
562                 memcpy(&gid[10], mac, ETH_ALEN);
563                 gid[5] = priv->port;
564
565                 mlx4_unicast_detach(dev, &qp, gid, MLX4_PROT_ETH);
566                 break;
567         }
568         case MLX4_STEERING_MODE_DEVICE_MANAGED: {
569                 mlx4_flow_detach(dev, reg_id);
570                 break;
571         }
572         default:
573                 en_err(priv, "Invalid steering mode.\n");
574         }
575 }
576
577 static int mlx4_en_get_qp(struct mlx4_en_priv *priv)
578 {
579         struct mlx4_en_dev *mdev = priv->mdev;
580         struct mlx4_dev *dev = mdev->dev;
581         int index = 0;
582         int err = 0;
583         int *qpn = &priv->base_qpn;
584         u64 mac = mlx4_mac_to_u64(priv->dev->dev_addr);
585
586         en_dbg(DRV, priv, "Registering MAC: %pM for adding\n",
587                priv->dev->dev_addr);
588         index = mlx4_register_mac(dev, priv->port, mac);
589         if (index < 0) {
590                 err = index;
591                 en_err(priv, "Failed adding MAC: %pM\n",
592                        priv->dev->dev_addr);
593                 return err;
594         }
595
596         if (dev->caps.steering_mode == MLX4_STEERING_MODE_A0) {
597                 int base_qpn = mlx4_get_base_qpn(dev, priv->port);
598                 *qpn = base_qpn + index;
599                 return 0;
600         }
601
602         err = mlx4_qp_reserve_range(dev, 1, 1, qpn, MLX4_RESERVE_A0_QP);
603         en_dbg(DRV, priv, "Reserved qp %d\n", *qpn);
604         if (err) {
605                 en_err(priv, "Failed to reserve qp for mac registration\n");
606                 mlx4_unregister_mac(dev, priv->port, mac);
607                 return err;
608         }
609
610         return 0;
611 }
612
613 static void mlx4_en_put_qp(struct mlx4_en_priv *priv)
614 {
615         struct mlx4_en_dev *mdev = priv->mdev;
616         struct mlx4_dev *dev = mdev->dev;
617         int qpn = priv->base_qpn;
618
619         if (dev->caps.steering_mode == MLX4_STEERING_MODE_A0) {
620                 u64 mac = mlx4_mac_to_u64(priv->dev->dev_addr);
621                 en_dbg(DRV, priv, "Registering MAC: %pM for deleting\n",
622                        priv->dev->dev_addr);
623                 mlx4_unregister_mac(dev, priv->port, mac);
624         } else {
625                 en_dbg(DRV, priv, "Releasing qp: port %d, qpn %d\n",
626                        priv->port, qpn);
627                 mlx4_qp_release_range(dev, qpn, 1);
628                 priv->flags &= ~MLX4_EN_FLAG_FORCE_PROMISC;
629         }
630 }
631
632 static int mlx4_en_replace_mac(struct mlx4_en_priv *priv, int qpn,
633                                unsigned char *new_mac, unsigned char *prev_mac)
634 {
635         struct mlx4_en_dev *mdev = priv->mdev;
636         struct mlx4_dev *dev = mdev->dev;
637         int err = 0;
638         u64 new_mac_u64 = mlx4_mac_to_u64(new_mac);
639
640         if (dev->caps.steering_mode != MLX4_STEERING_MODE_A0) {
641                 struct hlist_head *bucket;
642                 unsigned int mac_hash;
643                 struct mlx4_mac_entry *entry;
644                 struct hlist_node *tmp;
645                 u64 prev_mac_u64 = mlx4_mac_to_u64(prev_mac);
646
647                 bucket = &priv->mac_hash[prev_mac[MLX4_EN_MAC_HASH_IDX]];
648                 hlist_for_each_entry_safe(entry, tmp, bucket, hlist) {
649                         if (ether_addr_equal_64bits(entry->mac, prev_mac)) {
650                                 mlx4_en_uc_steer_release(priv, entry->mac,
651                                                          qpn, entry->reg_id);
652                                 mlx4_unregister_mac(dev, priv->port,
653                                                     prev_mac_u64);
654                                 hlist_del_rcu(&entry->hlist);
655                                 synchronize_rcu();
656                                 memcpy(entry->mac, new_mac, ETH_ALEN);
657                                 entry->reg_id = 0;
658                                 mac_hash = new_mac[MLX4_EN_MAC_HASH_IDX];
659                                 hlist_add_head_rcu(&entry->hlist,
660                                                    &priv->mac_hash[mac_hash]);
661                                 mlx4_register_mac(dev, priv->port, new_mac_u64);
662                                 err = mlx4_en_uc_steer_add(priv, new_mac,
663                                                            &qpn,
664                                                            &entry->reg_id);
665                                 if (err)
666                                         return err;
667                                 if (priv->tunnel_reg_id) {
668                                         mlx4_flow_detach(priv->mdev->dev, priv->tunnel_reg_id);
669                                         priv->tunnel_reg_id = 0;
670                                 }
671                                 err = mlx4_en_tunnel_steer_add(priv, new_mac, qpn,
672                                                                &priv->tunnel_reg_id);
673                                 return err;
674                         }
675                 }
676                 return -EINVAL;
677         }
678
679         return __mlx4_replace_mac(dev, priv->port, qpn, new_mac_u64);
680 }
681
682 static int mlx4_en_do_set_mac(struct mlx4_en_priv *priv,
683                               unsigned char new_mac[ETH_ALEN + 2])
684 {
685         int err = 0;
686
687         if (priv->port_up) {
688                 /* Remove old MAC and insert the new one */
689                 err = mlx4_en_replace_mac(priv, priv->base_qpn,
690                                           new_mac, priv->current_mac);
691                 if (err)
692                         en_err(priv, "Failed changing HW MAC address\n");
693         } else
694                 en_dbg(HW, priv, "Port is down while registering mac, exiting...\n");
695
696         if (!err)
697                 memcpy(priv->current_mac, new_mac, sizeof(priv->current_mac));
698
699         return err;
700 }
701
702 static int mlx4_en_set_mac(struct net_device *dev, void *addr)
703 {
704         struct mlx4_en_priv *priv = netdev_priv(dev);
705         struct mlx4_en_dev *mdev = priv->mdev;
706         struct sockaddr *saddr = addr;
707         unsigned char new_mac[ETH_ALEN + 2];
708         int err;
709
710         if (!is_valid_ether_addr(saddr->sa_data))
711                 return -EADDRNOTAVAIL;
712
713         mutex_lock(&mdev->state_lock);
714         memcpy(new_mac, saddr->sa_data, ETH_ALEN);
715         err = mlx4_en_do_set_mac(priv, new_mac);
716         if (!err)
717                 memcpy(dev->dev_addr, saddr->sa_data, ETH_ALEN);
718         mutex_unlock(&mdev->state_lock);
719
720         return err;
721 }
722
723 static void mlx4_en_clear_list(struct net_device *dev)
724 {
725         struct mlx4_en_priv *priv = netdev_priv(dev);
726         struct mlx4_en_mc_list *tmp, *mc_to_del;
727
728         list_for_each_entry_safe(mc_to_del, tmp, &priv->mc_list, list) {
729                 list_del(&mc_to_del->list);
730                 kfree(mc_to_del);
731         }
732 }
733
734 static void mlx4_en_cache_mclist(struct net_device *dev)
735 {
736         struct mlx4_en_priv *priv = netdev_priv(dev);
737         struct netdev_hw_addr *ha;
738         struct mlx4_en_mc_list *tmp;
739
740         mlx4_en_clear_list(dev);
741         netdev_for_each_mc_addr(ha, dev) {
742                 tmp = kzalloc(sizeof(struct mlx4_en_mc_list), GFP_ATOMIC);
743                 if (!tmp) {
744                         mlx4_en_clear_list(dev);
745                         return;
746                 }
747                 memcpy(tmp->addr, ha->addr, ETH_ALEN);
748                 list_add_tail(&tmp->list, &priv->mc_list);
749         }
750 }
751
752 static void update_mclist_flags(struct mlx4_en_priv *priv,
753                                 struct list_head *dst,
754                                 struct list_head *src)
755 {
756         struct mlx4_en_mc_list *dst_tmp, *src_tmp, *new_mc;
757         bool found;
758
759         /* Find all the entries that should be removed from dst,
760          * These are the entries that are not found in src
761          */
762         list_for_each_entry(dst_tmp, dst, list) {
763                 found = false;
764                 list_for_each_entry(src_tmp, src, list) {
765                         if (ether_addr_equal(dst_tmp->addr, src_tmp->addr)) {
766                                 found = true;
767                                 break;
768                         }
769                 }
770                 if (!found)
771                         dst_tmp->action = MCLIST_REM;
772         }
773
774         /* Add entries that exist in src but not in dst
775          * mark them as need to add
776          */
777         list_for_each_entry(src_tmp, src, list) {
778                 found = false;
779                 list_for_each_entry(dst_tmp, dst, list) {
780                         if (ether_addr_equal(dst_tmp->addr, src_tmp->addr)) {
781                                 dst_tmp->action = MCLIST_NONE;
782                                 found = true;
783                                 break;
784                         }
785                 }
786                 if (!found) {
787                         new_mc = kmemdup(src_tmp,
788                                          sizeof(struct mlx4_en_mc_list),
789                                          GFP_KERNEL);
790                         if (!new_mc)
791                                 return;
792
793                         new_mc->action = MCLIST_ADD;
794                         list_add_tail(&new_mc->list, dst);
795                 }
796         }
797 }
798
799 static void mlx4_en_set_rx_mode(struct net_device *dev)
800 {
801         struct mlx4_en_priv *priv = netdev_priv(dev);
802
803         if (!priv->port_up)
804                 return;
805
806         queue_work(priv->mdev->workqueue, &priv->rx_mode_task);
807 }
808
809 static void mlx4_en_set_promisc_mode(struct mlx4_en_priv *priv,
810                                      struct mlx4_en_dev *mdev)
811 {
812         int err = 0;
813
814         if (!(priv->flags & MLX4_EN_FLAG_PROMISC)) {
815                 if (netif_msg_rx_status(priv))
816                         en_warn(priv, "Entering promiscuous mode\n");
817                 priv->flags |= MLX4_EN_FLAG_PROMISC;
818
819                 /* Enable promiscouos mode */
820                 switch (mdev->dev->caps.steering_mode) {
821                 case MLX4_STEERING_MODE_DEVICE_MANAGED:
822                         err = mlx4_flow_steer_promisc_add(mdev->dev,
823                                                           priv->port,
824                                                           priv->base_qpn,
825                                                           MLX4_FS_ALL_DEFAULT);
826                         if (err)
827                                 en_err(priv, "Failed enabling promiscuous mode\n");
828                         priv->flags |= MLX4_EN_FLAG_MC_PROMISC;
829                         break;
830
831                 case MLX4_STEERING_MODE_B0:
832                         err = mlx4_unicast_promisc_add(mdev->dev,
833                                                        priv->base_qpn,
834                                                        priv->port);
835                         if (err)
836                                 en_err(priv, "Failed enabling unicast promiscuous mode\n");
837
838                         /* Add the default qp number as multicast
839                          * promisc
840                          */
841                         if (!(priv->flags & MLX4_EN_FLAG_MC_PROMISC)) {
842                                 err = mlx4_multicast_promisc_add(mdev->dev,
843                                                                  priv->base_qpn,
844                                                                  priv->port);
845                                 if (err)
846                                         en_err(priv, "Failed enabling multicast promiscuous mode\n");
847                                 priv->flags |= MLX4_EN_FLAG_MC_PROMISC;
848                         }
849                         break;
850
851                 case MLX4_STEERING_MODE_A0:
852                         err = mlx4_SET_PORT_qpn_calc(mdev->dev,
853                                                      priv->port,
854                                                      priv->base_qpn,
855                                                      1);
856                         if (err)
857                                 en_err(priv, "Failed enabling promiscuous mode\n");
858                         break;
859                 }
860
861                 /* Disable port multicast filter (unconditionally) */
862                 err = mlx4_SET_MCAST_FLTR(mdev->dev, priv->port, 0,
863                                           0, MLX4_MCAST_DISABLE);
864                 if (err)
865                         en_err(priv, "Failed disabling multicast filter\n");
866         }
867 }
868
869 static void mlx4_en_clear_promisc_mode(struct mlx4_en_priv *priv,
870                                        struct mlx4_en_dev *mdev)
871 {
872         int err = 0;
873
874         if (netif_msg_rx_status(priv))
875                 en_warn(priv, "Leaving promiscuous mode\n");
876         priv->flags &= ~MLX4_EN_FLAG_PROMISC;
877
878         /* Disable promiscouos mode */
879         switch (mdev->dev->caps.steering_mode) {
880         case MLX4_STEERING_MODE_DEVICE_MANAGED:
881                 err = mlx4_flow_steer_promisc_remove(mdev->dev,
882                                                      priv->port,
883                                                      MLX4_FS_ALL_DEFAULT);
884                 if (err)
885                         en_err(priv, "Failed disabling promiscuous mode\n");
886                 priv->flags &= ~MLX4_EN_FLAG_MC_PROMISC;
887                 break;
888
889         case MLX4_STEERING_MODE_B0:
890                 err = mlx4_unicast_promisc_remove(mdev->dev,
891                                                   priv->base_qpn,
892                                                   priv->port);
893                 if (err)
894                         en_err(priv, "Failed disabling unicast promiscuous mode\n");
895                 /* Disable Multicast promisc */
896                 if (priv->flags & MLX4_EN_FLAG_MC_PROMISC) {
897                         err = mlx4_multicast_promisc_remove(mdev->dev,
898                                                             priv->base_qpn,
899                                                             priv->port);
900                         if (err)
901                                 en_err(priv, "Failed disabling multicast promiscuous mode\n");
902                         priv->flags &= ~MLX4_EN_FLAG_MC_PROMISC;
903                 }
904                 break;
905
906         case MLX4_STEERING_MODE_A0:
907                 err = mlx4_SET_PORT_qpn_calc(mdev->dev,
908                                              priv->port,
909                                              priv->base_qpn, 0);
910                 if (err)
911                         en_err(priv, "Failed disabling promiscuous mode\n");
912                 break;
913         }
914 }
915
916 static void mlx4_en_do_multicast(struct mlx4_en_priv *priv,
917                                  struct net_device *dev,
918                                  struct mlx4_en_dev *mdev)
919 {
920         struct mlx4_en_mc_list *mclist, *tmp;
921         u64 mcast_addr = 0;
922         u8 mc_list[16] = {0};
923         int err = 0;
924
925         /* Enable/disable the multicast filter according to IFF_ALLMULTI */
926         if (dev->flags & IFF_ALLMULTI) {
927                 err = mlx4_SET_MCAST_FLTR(mdev->dev, priv->port, 0,
928                                           0, MLX4_MCAST_DISABLE);
929                 if (err)
930                         en_err(priv, "Failed disabling multicast filter\n");
931
932                 /* Add the default qp number as multicast promisc */
933                 if (!(priv->flags & MLX4_EN_FLAG_MC_PROMISC)) {
934                         switch (mdev->dev->caps.steering_mode) {
935                         case MLX4_STEERING_MODE_DEVICE_MANAGED:
936                                 err = mlx4_flow_steer_promisc_add(mdev->dev,
937                                                                   priv->port,
938                                                                   priv->base_qpn,
939                                                                   MLX4_FS_MC_DEFAULT);
940                                 break;
941
942                         case MLX4_STEERING_MODE_B0:
943                                 err = mlx4_multicast_promisc_add(mdev->dev,
944                                                                  priv->base_qpn,
945                                                                  priv->port);
946                                 break;
947
948                         case MLX4_STEERING_MODE_A0:
949                                 break;
950                         }
951                         if (err)
952                                 en_err(priv, "Failed entering multicast promisc mode\n");
953                         priv->flags |= MLX4_EN_FLAG_MC_PROMISC;
954                 }
955         } else {
956                 /* Disable Multicast promisc */
957                 if (priv->flags & MLX4_EN_FLAG_MC_PROMISC) {
958                         switch (mdev->dev->caps.steering_mode) {
959                         case MLX4_STEERING_MODE_DEVICE_MANAGED:
960                                 err = mlx4_flow_steer_promisc_remove(mdev->dev,
961                                                                      priv->port,
962                                                                      MLX4_FS_MC_DEFAULT);
963                                 break;
964
965                         case MLX4_STEERING_MODE_B0:
966                                 err = mlx4_multicast_promisc_remove(mdev->dev,
967                                                                     priv->base_qpn,
968                                                                     priv->port);
969                                 break;
970
971                         case MLX4_STEERING_MODE_A0:
972                                 break;
973                         }
974                         if (err)
975                                 en_err(priv, "Failed disabling multicast promiscuous mode\n");
976                         priv->flags &= ~MLX4_EN_FLAG_MC_PROMISC;
977                 }
978
979                 err = mlx4_SET_MCAST_FLTR(mdev->dev, priv->port, 0,
980                                           0, MLX4_MCAST_DISABLE);
981                 if (err)
982                         en_err(priv, "Failed disabling multicast filter\n");
983
984                 /* Flush mcast filter and init it with broadcast address */
985                 mlx4_SET_MCAST_FLTR(mdev->dev, priv->port, ETH_BCAST,
986                                     1, MLX4_MCAST_CONFIG);
987
988                 /* Update multicast list - we cache all addresses so they won't
989                  * change while HW is updated holding the command semaphor */
990                 netif_addr_lock_bh(dev);
991                 mlx4_en_cache_mclist(dev);
992                 netif_addr_unlock_bh(dev);
993                 list_for_each_entry(mclist, &priv->mc_list, list) {
994                         mcast_addr = mlx4_mac_to_u64(mclist->addr);
995                         mlx4_SET_MCAST_FLTR(mdev->dev, priv->port,
996                                             mcast_addr, 0, MLX4_MCAST_CONFIG);
997                 }
998                 err = mlx4_SET_MCAST_FLTR(mdev->dev, priv->port, 0,
999                                           0, MLX4_MCAST_ENABLE);
1000                 if (err)
1001                         en_err(priv, "Failed enabling multicast filter\n");
1002
1003                 update_mclist_flags(priv, &priv->curr_list, &priv->mc_list);
1004                 list_for_each_entry_safe(mclist, tmp, &priv->curr_list, list) {
1005                         if (mclist->action == MCLIST_REM) {
1006                                 /* detach this address and delete from list */
1007                                 memcpy(&mc_list[10], mclist->addr, ETH_ALEN);
1008                                 mc_list[5] = priv->port;
1009                                 err = mlx4_multicast_detach(mdev->dev,
1010                                                             &priv->rss_map.indir_qp,
1011                                                             mc_list,
1012                                                             MLX4_PROT_ETH,
1013                                                             mclist->reg_id);
1014                                 if (err)
1015                                         en_err(priv, "Fail to detach multicast address\n");
1016
1017                                 if (mclist->tunnel_reg_id) {
1018                                         err = mlx4_flow_detach(priv->mdev->dev, mclist->tunnel_reg_id);
1019                                         if (err)
1020                                                 en_err(priv, "Failed to detach multicast address\n");
1021                                 }
1022
1023                                 /* remove from list */
1024                                 list_del(&mclist->list);
1025                                 kfree(mclist);
1026                         } else if (mclist->action == MCLIST_ADD) {
1027                                 /* attach the address */
1028                                 memcpy(&mc_list[10], mclist->addr, ETH_ALEN);
1029                                 /* needed for B0 steering support */
1030                                 mc_list[5] = priv->port;
1031                                 err = mlx4_multicast_attach(mdev->dev,
1032                                                             &priv->rss_map.indir_qp,
1033                                                             mc_list,
1034                                                             priv->port, 0,
1035                                                             MLX4_PROT_ETH,
1036                                                             &mclist->reg_id);
1037                                 if (err)
1038                                         en_err(priv, "Fail to attach multicast address\n");
1039
1040                                 err = mlx4_en_tunnel_steer_add(priv, &mc_list[10], priv->base_qpn,
1041                                                                &mclist->tunnel_reg_id);
1042                                 if (err)
1043                                         en_err(priv, "Failed to attach multicast address\n");
1044                         }
1045                 }
1046         }
1047 }
1048
1049 static void mlx4_en_do_uc_filter(struct mlx4_en_priv *priv,
1050                                  struct net_device *dev,
1051                                  struct mlx4_en_dev *mdev)
1052 {
1053         struct netdev_hw_addr *ha;
1054         struct mlx4_mac_entry *entry;
1055         struct hlist_node *tmp;
1056         bool found;
1057         u64 mac;
1058         int err = 0;
1059         struct hlist_head *bucket;
1060         unsigned int i;
1061         int removed = 0;
1062         u32 prev_flags;
1063
1064         /* Note that we do not need to protect our mac_hash traversal with rcu,
1065          * since all modification code is protected by mdev->state_lock
1066          */
1067
1068         /* find what to remove */
1069         for (i = 0; i < MLX4_EN_MAC_HASH_SIZE; ++i) {
1070                 bucket = &priv->mac_hash[i];
1071                 hlist_for_each_entry_safe(entry, tmp, bucket, hlist) {
1072                         found = false;
1073                         netdev_for_each_uc_addr(ha, dev) {
1074                                 if (ether_addr_equal_64bits(entry->mac,
1075                                                             ha->addr)) {
1076                                         found = true;
1077                                         break;
1078                                 }
1079                         }
1080
1081                         /* MAC address of the port is not in uc list */
1082                         if (ether_addr_equal_64bits(entry->mac,
1083                                                     priv->current_mac))
1084                                 found = true;
1085
1086                         if (!found) {
1087                                 mac = mlx4_mac_to_u64(entry->mac);
1088                                 mlx4_en_uc_steer_release(priv, entry->mac,
1089                                                          priv->base_qpn,
1090                                                          entry->reg_id);
1091                                 mlx4_unregister_mac(mdev->dev, priv->port, mac);
1092
1093                                 hlist_del_rcu(&entry->hlist);
1094                                 kfree_rcu(entry, rcu);
1095                                 en_dbg(DRV, priv, "Removed MAC %pM on port:%d\n",
1096                                        entry->mac, priv->port);
1097                                 ++removed;
1098                         }
1099                 }
1100         }
1101
1102         /* if we didn't remove anything, there is no use in trying to add
1103          * again once we are in a forced promisc mode state
1104          */
1105         if ((priv->flags & MLX4_EN_FLAG_FORCE_PROMISC) && 0 == removed)
1106                 return;
1107
1108         prev_flags = priv->flags;
1109         priv->flags &= ~MLX4_EN_FLAG_FORCE_PROMISC;
1110
1111         /* find what to add */
1112         netdev_for_each_uc_addr(ha, dev) {
1113                 found = false;
1114                 bucket = &priv->mac_hash[ha->addr[MLX4_EN_MAC_HASH_IDX]];
1115                 hlist_for_each_entry(entry, bucket, hlist) {
1116                         if (ether_addr_equal_64bits(entry->mac, ha->addr)) {
1117                                 found = true;
1118                                 break;
1119                         }
1120                 }
1121
1122                 if (!found) {
1123                         entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1124                         if (!entry) {
1125                                 en_err(priv, "Failed adding MAC %pM on port:%d (out of memory)\n",
1126                                        ha->addr, priv->port);
1127                                 priv->flags |= MLX4_EN_FLAG_FORCE_PROMISC;
1128                                 break;
1129                         }
1130                         mac = mlx4_mac_to_u64(ha->addr);
1131                         memcpy(entry->mac, ha->addr, ETH_ALEN);
1132                         err = mlx4_register_mac(mdev->dev, priv->port, mac);
1133                         if (err < 0) {
1134                                 en_err(priv, "Failed registering MAC %pM on port %d: %d\n",
1135                                        ha->addr, priv->port, err);
1136                                 kfree(entry);
1137                                 priv->flags |= MLX4_EN_FLAG_FORCE_PROMISC;
1138                                 break;
1139                         }
1140                         err = mlx4_en_uc_steer_add(priv, ha->addr,
1141                                                    &priv->base_qpn,
1142                                                    &entry->reg_id);
1143                         if (err) {
1144                                 en_err(priv, "Failed adding MAC %pM on port %d: %d\n",
1145                                        ha->addr, priv->port, err);
1146                                 mlx4_unregister_mac(mdev->dev, priv->port, mac);
1147                                 kfree(entry);
1148                                 priv->flags |= MLX4_EN_FLAG_FORCE_PROMISC;
1149                                 break;
1150                         } else {
1151                                 unsigned int mac_hash;
1152                                 en_dbg(DRV, priv, "Added MAC %pM on port:%d\n",
1153                                        ha->addr, priv->port);
1154                                 mac_hash = ha->addr[MLX4_EN_MAC_HASH_IDX];
1155                                 bucket = &priv->mac_hash[mac_hash];
1156                                 hlist_add_head_rcu(&entry->hlist, bucket);
1157                         }
1158                 }
1159         }
1160
1161         if (priv->flags & MLX4_EN_FLAG_FORCE_PROMISC) {
1162                 en_warn(priv, "Forcing promiscuous mode on port:%d\n",
1163                         priv->port);
1164         } else if (prev_flags & MLX4_EN_FLAG_FORCE_PROMISC) {
1165                 en_warn(priv, "Stop forcing promiscuous mode on port:%d\n",
1166                         priv->port);
1167         }
1168 }
1169
1170 static void mlx4_en_do_set_rx_mode(struct work_struct *work)
1171 {
1172         struct mlx4_en_priv *priv = container_of(work, struct mlx4_en_priv,
1173                                                  rx_mode_task);
1174         struct mlx4_en_dev *mdev = priv->mdev;
1175         struct net_device *dev = priv->dev;
1176
1177         mutex_lock(&mdev->state_lock);
1178         if (!mdev->device_up) {
1179                 en_dbg(HW, priv, "Card is not up, ignoring rx mode change.\n");
1180                 goto out;
1181         }
1182         if (!priv->port_up) {
1183                 en_dbg(HW, priv, "Port is down, ignoring rx mode change.\n");
1184                 goto out;
1185         }
1186
1187         if (!netif_carrier_ok(dev)) {
1188                 if (!mlx4_en_QUERY_PORT(mdev, priv->port)) {
1189                         if (priv->port_state.link_state) {
1190                                 priv->last_link_state = MLX4_DEV_EVENT_PORT_UP;
1191                                 netif_carrier_on(dev);
1192                                 en_dbg(LINK, priv, "Link Up\n");
1193                         }
1194                 }
1195         }
1196
1197         if (dev->priv_flags & IFF_UNICAST_FLT)
1198                 mlx4_en_do_uc_filter(priv, dev, mdev);
1199
1200         /* Promsicuous mode: disable all filters */
1201         if ((dev->flags & IFF_PROMISC) ||
1202             (priv->flags & MLX4_EN_FLAG_FORCE_PROMISC)) {
1203                 mlx4_en_set_promisc_mode(priv, mdev);
1204                 goto out;
1205         }
1206
1207         /* Not in promiscuous mode */
1208         if (priv->flags & MLX4_EN_FLAG_PROMISC)
1209                 mlx4_en_clear_promisc_mode(priv, mdev);
1210
1211         mlx4_en_do_multicast(priv, dev, mdev);
1212 out:
1213         mutex_unlock(&mdev->state_lock);
1214 }
1215
1216 #ifdef CONFIG_NET_POLL_CONTROLLER
1217 static void mlx4_en_netpoll(struct net_device *dev)
1218 {
1219         struct mlx4_en_priv *priv = netdev_priv(dev);
1220         struct mlx4_en_cq *cq;
1221         int i;
1222
1223         for (i = 0; i < priv->tx_ring_num; i++) {
1224                 cq = priv->tx_cq[i];
1225                 napi_schedule(&cq->napi);
1226         }
1227 }
1228 #endif
1229
1230 static int mlx4_en_set_rss_steer_rules(struct mlx4_en_priv *priv)
1231 {
1232         u64 reg_id;
1233         int err = 0;
1234         int *qpn = &priv->base_qpn;
1235         struct mlx4_mac_entry *entry;
1236
1237         err = mlx4_en_uc_steer_add(priv, priv->dev->dev_addr, qpn, &reg_id);
1238         if (err)
1239                 return err;
1240
1241         err = mlx4_en_tunnel_steer_add(priv, priv->dev->dev_addr, *qpn,
1242                                        &priv->tunnel_reg_id);
1243         if (err)
1244                 goto tunnel_err;
1245
1246         entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1247         if (!entry) {
1248                 err = -ENOMEM;
1249                 goto alloc_err;
1250         }
1251
1252         memcpy(entry->mac, priv->dev->dev_addr, sizeof(entry->mac));
1253         memcpy(priv->current_mac, entry->mac, sizeof(priv->current_mac));
1254         entry->reg_id = reg_id;
1255         hlist_add_head_rcu(&entry->hlist,
1256                            &priv->mac_hash[entry->mac[MLX4_EN_MAC_HASH_IDX]]);
1257
1258         return 0;
1259
1260 alloc_err:
1261         if (priv->tunnel_reg_id)
1262                 mlx4_flow_detach(priv->mdev->dev, priv->tunnel_reg_id);
1263
1264 tunnel_err:
1265         mlx4_en_uc_steer_release(priv, priv->dev->dev_addr, *qpn, reg_id);
1266         return err;
1267 }
1268
1269 static void mlx4_en_delete_rss_steer_rules(struct mlx4_en_priv *priv)
1270 {
1271         u64 mac;
1272         unsigned int i;
1273         int qpn = priv->base_qpn;
1274         struct hlist_head *bucket;
1275         struct hlist_node *tmp;
1276         struct mlx4_mac_entry *entry;
1277
1278         for (i = 0; i < MLX4_EN_MAC_HASH_SIZE; ++i) {
1279                 bucket = &priv->mac_hash[i];
1280                 hlist_for_each_entry_safe(entry, tmp, bucket, hlist) {
1281                         mac = mlx4_mac_to_u64(entry->mac);
1282                         en_dbg(DRV, priv, "Registering MAC:%pM for deleting\n",
1283                                entry->mac);
1284                         mlx4_en_uc_steer_release(priv, entry->mac,
1285                                                  qpn, entry->reg_id);
1286
1287                         mlx4_unregister_mac(priv->mdev->dev, priv->port, mac);
1288                         hlist_del_rcu(&entry->hlist);
1289                         kfree_rcu(entry, rcu);
1290                 }
1291         }
1292
1293         if (priv->tunnel_reg_id) {
1294                 mlx4_flow_detach(priv->mdev->dev, priv->tunnel_reg_id);
1295                 priv->tunnel_reg_id = 0;
1296         }
1297 }
1298
1299 static void mlx4_en_tx_timeout(struct net_device *dev)
1300 {
1301         struct mlx4_en_priv *priv = netdev_priv(dev);
1302         struct mlx4_en_dev *mdev = priv->mdev;
1303         int i;
1304
1305         if (netif_msg_timer(priv))
1306                 en_warn(priv, "Tx timeout called on port:%d\n", priv->port);
1307
1308         for (i = 0; i < priv->tx_ring_num; i++) {
1309                 if (!netif_tx_queue_stopped(netdev_get_tx_queue(dev, i)))
1310                         continue;
1311                 en_warn(priv, "TX timeout on queue: %d, QP: 0x%x, CQ: 0x%x, Cons: 0x%x, Prod: 0x%x\n",
1312                         i, priv->tx_ring[i]->qpn, priv->tx_ring[i]->cqn,
1313                         priv->tx_ring[i]->cons, priv->tx_ring[i]->prod);
1314         }
1315
1316         priv->port_stats.tx_timeout++;
1317         if (!test_and_set_bit(MLX4_EN_STATE_FLAG_RESTARTING, &priv->state)) {
1318                 en_dbg(DRV, priv, "Scheduling port restart\n");
1319                 queue_work(mdev->workqueue, &priv->restart_task);
1320         }
1321 }
1322
1323
1324 static struct rtnl_link_stats64 *
1325 mlx4_en_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
1326 {
1327         struct mlx4_en_priv *priv = netdev_priv(dev);
1328
1329         spin_lock_bh(&priv->stats_lock);
1330         netdev_stats_to_stats64(stats, &dev->stats);
1331         spin_unlock_bh(&priv->stats_lock);
1332
1333         return stats;
1334 }
1335
1336 static void mlx4_en_set_default_moderation(struct mlx4_en_priv *priv)
1337 {
1338         struct mlx4_en_cq *cq;
1339         int i;
1340
1341         /* If we haven't received a specific coalescing setting
1342          * (module param), we set the moderation parameters as follows:
1343          * - moder_cnt is set to the number of mtu sized packets to
1344          *   satisfy our coalescing target.
1345          * - moder_time is set to a fixed value.
1346          */
1347         priv->rx_frames = MLX4_EN_RX_COAL_TARGET;
1348         priv->rx_usecs = MLX4_EN_RX_COAL_TIME;
1349         priv->tx_frames = MLX4_EN_TX_COAL_PKTS;
1350         priv->tx_usecs = MLX4_EN_TX_COAL_TIME;
1351         en_dbg(INTR, priv, "Default coalesing params for mtu:%d - rx_frames:%d rx_usecs:%d\n",
1352                priv->dev->mtu, priv->rx_frames, priv->rx_usecs);
1353
1354         /* Setup cq moderation params */
1355         for (i = 0; i < priv->rx_ring_num; i++) {
1356                 cq = priv->rx_cq[i];
1357                 cq->moder_cnt = priv->rx_frames;
1358                 cq->moder_time = priv->rx_usecs;
1359                 priv->last_moder_time[i] = MLX4_EN_AUTO_CONF;
1360                 priv->last_moder_packets[i] = 0;
1361                 priv->last_moder_bytes[i] = 0;
1362         }
1363
1364         for (i = 0; i < priv->tx_ring_num; i++) {
1365                 cq = priv->tx_cq[i];
1366                 cq->moder_cnt = priv->tx_frames;
1367                 cq->moder_time = priv->tx_usecs;
1368         }
1369
1370         /* Reset auto-moderation params */
1371         priv->pkt_rate_low = MLX4_EN_RX_RATE_LOW;
1372         priv->rx_usecs_low = MLX4_EN_RX_COAL_TIME_LOW;
1373         priv->pkt_rate_high = MLX4_EN_RX_RATE_HIGH;
1374         priv->rx_usecs_high = MLX4_EN_RX_COAL_TIME_HIGH;
1375         priv->sample_interval = MLX4_EN_SAMPLE_INTERVAL;
1376         priv->adaptive_rx_coal = 1;
1377         priv->last_moder_jiffies = 0;
1378         priv->last_moder_tx_packets = 0;
1379 }
1380
1381 static void mlx4_en_auto_moderation(struct mlx4_en_priv *priv)
1382 {
1383         unsigned long period = (unsigned long) (jiffies - priv->last_moder_jiffies);
1384         struct mlx4_en_cq *cq;
1385         unsigned long packets;
1386         unsigned long rate;
1387         unsigned long avg_pkt_size;
1388         unsigned long rx_packets;
1389         unsigned long rx_bytes;
1390         unsigned long rx_pkt_diff;
1391         int moder_time;
1392         int ring, err;
1393
1394         if (!priv->adaptive_rx_coal || period < priv->sample_interval * HZ)
1395                 return;
1396
1397         for (ring = 0; ring < priv->rx_ring_num; ring++) {
1398                 spin_lock_bh(&priv->stats_lock);
1399                 rx_packets = priv->rx_ring[ring]->packets;
1400                 rx_bytes = priv->rx_ring[ring]->bytes;
1401                 spin_unlock_bh(&priv->stats_lock);
1402
1403                 rx_pkt_diff = ((unsigned long) (rx_packets -
1404                                 priv->last_moder_packets[ring]));
1405                 packets = rx_pkt_diff;
1406                 rate = packets * HZ / period;
1407                 avg_pkt_size = packets ? ((unsigned long) (rx_bytes -
1408                                 priv->last_moder_bytes[ring])) / packets : 0;
1409
1410                 /* Apply auto-moderation only when packet rate
1411                  * exceeds a rate that it matters */
1412                 if (rate > (MLX4_EN_RX_RATE_THRESH / priv->rx_ring_num) &&
1413                     avg_pkt_size > MLX4_EN_AVG_PKT_SMALL) {
1414                         if (rate < priv->pkt_rate_low)
1415                                 moder_time = priv->rx_usecs_low;
1416                         else if (rate > priv->pkt_rate_high)
1417                                 moder_time = priv->rx_usecs_high;
1418                         else
1419                                 moder_time = (rate - priv->pkt_rate_low) *
1420                                         (priv->rx_usecs_high - priv->rx_usecs_low) /
1421                                         (priv->pkt_rate_high - priv->pkt_rate_low) +
1422                                         priv->rx_usecs_low;
1423                 } else {
1424                         moder_time = priv->rx_usecs_low;
1425                 }
1426
1427                 if (moder_time != priv->last_moder_time[ring]) {
1428                         priv->last_moder_time[ring] = moder_time;
1429                         cq = priv->rx_cq[ring];
1430                         cq->moder_time = moder_time;
1431                         cq->moder_cnt = priv->rx_frames;
1432                         err = mlx4_en_set_cq_moder(priv, cq);
1433                         if (err)
1434                                 en_err(priv, "Failed modifying moderation for cq:%d\n",
1435                                        ring);
1436                 }
1437                 priv->last_moder_packets[ring] = rx_packets;
1438                 priv->last_moder_bytes[ring] = rx_bytes;
1439         }
1440
1441         priv->last_moder_jiffies = jiffies;
1442 }
1443
1444 static void mlx4_en_do_get_stats(struct work_struct *work)
1445 {
1446         struct delayed_work *delay = to_delayed_work(work);
1447         struct mlx4_en_priv *priv = container_of(delay, struct mlx4_en_priv,
1448                                                  stats_task);
1449         struct mlx4_en_dev *mdev = priv->mdev;
1450         int err;
1451
1452         mutex_lock(&mdev->state_lock);
1453         if (mdev->device_up) {
1454                 if (priv->port_up) {
1455                         err = mlx4_en_DUMP_ETH_STATS(mdev, priv->port, 0);
1456                         if (err)
1457                                 en_dbg(HW, priv, "Could not update stats\n");
1458
1459                         mlx4_en_auto_moderation(priv);
1460                 }
1461
1462                 queue_delayed_work(mdev->workqueue, &priv->stats_task, STATS_DELAY);
1463         }
1464         if (mdev->mac_removed[MLX4_MAX_PORTS + 1 - priv->port]) {
1465                 mlx4_en_do_set_mac(priv, priv->current_mac);
1466                 mdev->mac_removed[MLX4_MAX_PORTS + 1 - priv->port] = 0;
1467         }
1468         mutex_unlock(&mdev->state_lock);
1469 }
1470
1471 /* mlx4_en_service_task - Run service task for tasks that needed to be done
1472  * periodically
1473  */
1474 static void mlx4_en_service_task(struct work_struct *work)
1475 {
1476         struct delayed_work *delay = to_delayed_work(work);
1477         struct mlx4_en_priv *priv = container_of(delay, struct mlx4_en_priv,
1478                                                  service_task);
1479         struct mlx4_en_dev *mdev = priv->mdev;
1480
1481         mutex_lock(&mdev->state_lock);
1482         if (mdev->device_up) {
1483                 if (mdev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_TS)
1484                         mlx4_en_ptp_overflow_check(mdev);
1485
1486                 mlx4_en_recover_from_oom(priv);
1487                 queue_delayed_work(mdev->workqueue, &priv->service_task,
1488                                    SERVICE_TASK_DELAY);
1489         }
1490         mutex_unlock(&mdev->state_lock);
1491 }
1492
1493 static void mlx4_en_linkstate(struct work_struct *work)
1494 {
1495         struct mlx4_en_priv *priv = container_of(work, struct mlx4_en_priv,
1496                                                  linkstate_task);
1497         struct mlx4_en_dev *mdev = priv->mdev;
1498         int linkstate = priv->link_state;
1499
1500         mutex_lock(&mdev->state_lock);
1501         /* If observable port state changed set carrier state and
1502          * report to system log */
1503         if (priv->last_link_state != linkstate) {
1504                 if (linkstate == MLX4_DEV_EVENT_PORT_DOWN) {
1505                         en_info(priv, "Link Down\n");
1506                         netif_carrier_off(priv->dev);
1507                 } else {
1508                         en_info(priv, "Link Up\n");
1509                         netif_carrier_on(priv->dev);
1510                 }
1511         }
1512         priv->last_link_state = linkstate;
1513         mutex_unlock(&mdev->state_lock);
1514 }
1515
1516 static int mlx4_en_init_affinity_hint(struct mlx4_en_priv *priv, int ring_idx)
1517 {
1518         struct mlx4_en_rx_ring *ring = priv->rx_ring[ring_idx];
1519         int numa_node = priv->mdev->dev->numa_node;
1520
1521         if (!zalloc_cpumask_var(&ring->affinity_mask, GFP_KERNEL))
1522                 return -ENOMEM;
1523
1524         cpumask_set_cpu(cpumask_local_spread(ring_idx, numa_node),
1525                         ring->affinity_mask);
1526         return 0;
1527 }
1528
1529 static void mlx4_en_free_affinity_hint(struct mlx4_en_priv *priv, int ring_idx)
1530 {
1531         free_cpumask_var(priv->rx_ring[ring_idx]->affinity_mask);
1532 }
1533
1534 static void mlx4_en_init_recycle_ring(struct mlx4_en_priv *priv,
1535                                       int tx_ring_idx)
1536 {
1537         struct mlx4_en_tx_ring *tx_ring = priv->tx_ring[tx_ring_idx];
1538         int rr_index;
1539
1540         rr_index = (priv->xdp_ring_num - priv->tx_ring_num) + tx_ring_idx;
1541         if (rr_index >= 0) {
1542                 tx_ring->free_tx_desc = mlx4_en_recycle_tx_desc;
1543                 tx_ring->recycle_ring = priv->rx_ring[rr_index];
1544                 en_dbg(DRV, priv,
1545                        "Set tx_ring[%d]->recycle_ring = rx_ring[%d]\n",
1546                        tx_ring_idx, rr_index);
1547         } else {
1548                 tx_ring->recycle_ring = NULL;
1549         }
1550 }
1551
1552 int mlx4_en_start_port(struct net_device *dev)
1553 {
1554         struct mlx4_en_priv *priv = netdev_priv(dev);
1555         struct mlx4_en_dev *mdev = priv->mdev;
1556         struct mlx4_en_cq *cq;
1557         struct mlx4_en_tx_ring *tx_ring;
1558         int rx_index = 0;
1559         int tx_index = 0;
1560         int err = 0;
1561         int i;
1562         int j;
1563         u8 mc_list[16] = {0};
1564
1565         if (priv->port_up) {
1566                 en_dbg(DRV, priv, "start port called while port already up\n");
1567                 return 0;
1568         }
1569
1570         INIT_LIST_HEAD(&priv->mc_list);
1571         INIT_LIST_HEAD(&priv->curr_list);
1572         INIT_LIST_HEAD(&priv->ethtool_list);
1573         memset(&priv->ethtool_rules[0], 0,
1574                sizeof(struct ethtool_flow_id) * MAX_NUM_OF_FS_RULES);
1575
1576         /* Calculate Rx buf size */
1577         dev->mtu = min(dev->mtu, priv->max_mtu);
1578         mlx4_en_calc_rx_buf(dev);
1579         en_dbg(DRV, priv, "Rx buf size:%d\n", priv->rx_skb_size);
1580
1581         /* Configure rx cq's and rings */
1582         err = mlx4_en_activate_rx_rings(priv);
1583         if (err) {
1584                 en_err(priv, "Failed to activate RX rings\n");
1585                 return err;
1586         }
1587         for (i = 0; i < priv->rx_ring_num; i++) {
1588                 cq = priv->rx_cq[i];
1589
1590                 err = mlx4_en_init_affinity_hint(priv, i);
1591                 if (err) {
1592                         en_err(priv, "Failed preparing IRQ affinity hint\n");
1593                         goto cq_err;
1594                 }
1595
1596                 err = mlx4_en_activate_cq(priv, cq, i);
1597                 if (err) {
1598                         en_err(priv, "Failed activating Rx CQ\n");
1599                         mlx4_en_free_affinity_hint(priv, i);
1600                         goto cq_err;
1601                 }
1602
1603                 for (j = 0; j < cq->size; j++) {
1604                         struct mlx4_cqe *cqe = NULL;
1605
1606                         cqe = mlx4_en_get_cqe(cq->buf, j, priv->cqe_size) +
1607                               priv->cqe_factor;
1608                         cqe->owner_sr_opcode = MLX4_CQE_OWNER_MASK;
1609                 }
1610
1611                 err = mlx4_en_set_cq_moder(priv, cq);
1612                 if (err) {
1613                         en_err(priv, "Failed setting cq moderation parameters\n");
1614                         mlx4_en_deactivate_cq(priv, cq);
1615                         mlx4_en_free_affinity_hint(priv, i);
1616                         goto cq_err;
1617                 }
1618                 mlx4_en_arm_cq(priv, cq);
1619                 priv->rx_ring[i]->cqn = cq->mcq.cqn;
1620                 ++rx_index;
1621         }
1622
1623         /* Set qp number */
1624         en_dbg(DRV, priv, "Getting qp number for port %d\n", priv->port);
1625         err = mlx4_en_get_qp(priv);
1626         if (err) {
1627                 en_err(priv, "Failed getting eth qp\n");
1628                 goto cq_err;
1629         }
1630         mdev->mac_removed[priv->port] = 0;
1631
1632         priv->counter_index =
1633                         mlx4_get_default_counter_index(mdev->dev, priv->port);
1634
1635         err = mlx4_en_config_rss_steer(priv);
1636         if (err) {
1637                 en_err(priv, "Failed configuring rss steering\n");
1638                 goto mac_err;
1639         }
1640
1641         err = mlx4_en_create_drop_qp(priv);
1642         if (err)
1643                 goto rss_err;
1644
1645         /* Configure tx cq's and rings */
1646         for (i = 0; i < priv->tx_ring_num; i++) {
1647                 /* Configure cq */
1648                 cq = priv->tx_cq[i];
1649                 err = mlx4_en_activate_cq(priv, cq, i);
1650                 if (err) {
1651                         en_err(priv, "Failed allocating Tx CQ\n");
1652                         goto tx_err;
1653                 }
1654                 err = mlx4_en_set_cq_moder(priv, cq);
1655                 if (err) {
1656                         en_err(priv, "Failed setting cq moderation parameters\n");
1657                         mlx4_en_deactivate_cq(priv, cq);
1658                         goto tx_err;
1659                 }
1660                 en_dbg(DRV, priv, "Resetting index of collapsed CQ:%d to -1\n", i);
1661                 cq->buf->wqe_index = cpu_to_be16(0xffff);
1662
1663                 /* Configure ring */
1664                 tx_ring = priv->tx_ring[i];
1665                 err = mlx4_en_activate_tx_ring(priv, tx_ring, cq->mcq.cqn,
1666                         i / priv->num_tx_rings_p_up);
1667                 if (err) {
1668                         en_err(priv, "Failed allocating Tx ring\n");
1669                         mlx4_en_deactivate_cq(priv, cq);
1670                         goto tx_err;
1671                 }
1672                 tx_ring->tx_queue = netdev_get_tx_queue(dev, i);
1673
1674                 mlx4_en_init_recycle_ring(priv, i);
1675
1676                 /* Arm CQ for TX completions */
1677                 mlx4_en_arm_cq(priv, cq);
1678
1679                 /* Set initial ownership of all Tx TXBBs to SW (1) */
1680                 for (j = 0; j < tx_ring->buf_size; j += STAMP_STRIDE)
1681                         *((u32 *) (tx_ring->buf + j)) = 0xffffffff;
1682                 ++tx_index;
1683         }
1684
1685         /* Configure port */
1686         err = mlx4_SET_PORT_general(mdev->dev, priv->port,
1687                                     priv->rx_skb_size + ETH_FCS_LEN,
1688                                     priv->prof->tx_pause,
1689                                     priv->prof->tx_ppp,
1690                                     priv->prof->rx_pause,
1691                                     priv->prof->rx_ppp);
1692         if (err) {
1693                 en_err(priv, "Failed setting port general configurations for port %d, with error %d\n",
1694                        priv->port, err);
1695                 goto tx_err;
1696         }
1697         /* Set default qp number */
1698         err = mlx4_SET_PORT_qpn_calc(mdev->dev, priv->port, priv->base_qpn, 0);
1699         if (err) {
1700                 en_err(priv, "Failed setting default qp numbers\n");
1701                 goto tx_err;
1702         }
1703
1704         if (mdev->dev->caps.tunnel_offload_mode == MLX4_TUNNEL_OFFLOAD_MODE_VXLAN) {
1705                 err = mlx4_SET_PORT_VXLAN(mdev->dev, priv->port, VXLAN_STEER_BY_OUTER_MAC, 1);
1706                 if (err) {
1707                         en_err(priv, "Failed setting port L2 tunnel configuration, err %d\n",
1708                                err);
1709                         goto tx_err;
1710                 }
1711         }
1712
1713         /* Init port */
1714         en_dbg(HW, priv, "Initializing port\n");
1715         err = mlx4_INIT_PORT(mdev->dev, priv->port);
1716         if (err) {
1717                 en_err(priv, "Failed Initializing port\n");
1718                 goto tx_err;
1719         }
1720
1721         /* Set Unicast and VXLAN steering rules */
1722         if (mdev->dev->caps.steering_mode != MLX4_STEERING_MODE_A0 &&
1723             mlx4_en_set_rss_steer_rules(priv))
1724                 mlx4_warn(mdev, "Failed setting steering rules\n");
1725
1726         /* Attach rx QP to bradcast address */
1727         eth_broadcast_addr(&mc_list[10]);
1728         mc_list[5] = priv->port; /* needed for B0 steering support */
1729         if (mlx4_multicast_attach(mdev->dev, &priv->rss_map.indir_qp, mc_list,
1730                                   priv->port, 0, MLX4_PROT_ETH,
1731                                   &priv->broadcast_id))
1732                 mlx4_warn(mdev, "Failed Attaching Broadcast\n");
1733
1734         /* Must redo promiscuous mode setup. */
1735         priv->flags &= ~(MLX4_EN_FLAG_PROMISC | MLX4_EN_FLAG_MC_PROMISC);
1736
1737         /* Schedule multicast task to populate multicast list */
1738         queue_work(mdev->workqueue, &priv->rx_mode_task);
1739
1740         if (priv->mdev->dev->caps.tunnel_offload_mode == MLX4_TUNNEL_OFFLOAD_MODE_VXLAN)
1741                 udp_tunnel_get_rx_info(dev);
1742
1743         priv->port_up = true;
1744
1745         /* Process all completions if exist to prevent
1746          * the queues freezing if they are full
1747          */
1748         for (i = 0; i < priv->rx_ring_num; i++) {
1749                 local_bh_disable();
1750                 napi_schedule(&priv->rx_cq[i]->napi);
1751                 local_bh_enable();
1752         }
1753
1754         clear_bit(MLX4_EN_STATE_FLAG_RESTARTING, &priv->state);
1755         netif_tx_start_all_queues(dev);
1756         netif_device_attach(dev);
1757
1758         return 0;
1759
1760 tx_err:
1761         while (tx_index--) {
1762                 mlx4_en_deactivate_tx_ring(priv, priv->tx_ring[tx_index]);
1763                 mlx4_en_deactivate_cq(priv, priv->tx_cq[tx_index]);
1764         }
1765         mlx4_en_destroy_drop_qp(priv);
1766 rss_err:
1767         mlx4_en_release_rss_steer(priv);
1768 mac_err:
1769         mlx4_en_put_qp(priv);
1770 cq_err:
1771         while (rx_index--) {
1772                 mlx4_en_deactivate_cq(priv, priv->rx_cq[rx_index]);
1773                 mlx4_en_free_affinity_hint(priv, rx_index);
1774         }
1775         for (i = 0; i < priv->rx_ring_num; i++)
1776                 mlx4_en_deactivate_rx_ring(priv, priv->rx_ring[i]);
1777
1778         return err; /* need to close devices */
1779 }
1780
1781
1782 void mlx4_en_stop_port(struct net_device *dev, int detach)
1783 {
1784         struct mlx4_en_priv *priv = netdev_priv(dev);
1785         struct mlx4_en_dev *mdev = priv->mdev;
1786         struct mlx4_en_mc_list *mclist, *tmp;
1787         struct ethtool_flow_id *flow, *tmp_flow;
1788         int i;
1789         u8 mc_list[16] = {0};
1790
1791         if (!priv->port_up) {
1792                 en_dbg(DRV, priv, "stop port called while port already down\n");
1793                 return;
1794         }
1795
1796         /* close port*/
1797         mlx4_CLOSE_PORT(mdev->dev, priv->port);
1798
1799         /* Synchronize with tx routine */
1800         netif_tx_lock_bh(dev);
1801         if (detach)
1802                 netif_device_detach(dev);
1803         netif_tx_stop_all_queues(dev);
1804         netif_tx_unlock_bh(dev);
1805
1806         netif_tx_disable(dev);
1807
1808         /* Set port as not active */
1809         priv->port_up = false;
1810         priv->counter_index = MLX4_SINK_COUNTER_INDEX(mdev->dev);
1811
1812         /* Promsicuous mode */
1813         if (mdev->dev->caps.steering_mode ==
1814             MLX4_STEERING_MODE_DEVICE_MANAGED) {
1815                 priv->flags &= ~(MLX4_EN_FLAG_PROMISC |
1816                                  MLX4_EN_FLAG_MC_PROMISC);
1817                 mlx4_flow_steer_promisc_remove(mdev->dev,
1818                                                priv->port,
1819                                                MLX4_FS_ALL_DEFAULT);
1820                 mlx4_flow_steer_promisc_remove(mdev->dev,
1821                                                priv->port,
1822                                                MLX4_FS_MC_DEFAULT);
1823         } else if (priv->flags & MLX4_EN_FLAG_PROMISC) {
1824                 priv->flags &= ~MLX4_EN_FLAG_PROMISC;
1825
1826                 /* Disable promiscouos mode */
1827                 mlx4_unicast_promisc_remove(mdev->dev, priv->base_qpn,
1828                                             priv->port);
1829
1830                 /* Disable Multicast promisc */
1831                 if (priv->flags & MLX4_EN_FLAG_MC_PROMISC) {
1832                         mlx4_multicast_promisc_remove(mdev->dev, priv->base_qpn,
1833                                                       priv->port);
1834                         priv->flags &= ~MLX4_EN_FLAG_MC_PROMISC;
1835                 }
1836         }
1837
1838         /* Detach All multicasts */
1839         eth_broadcast_addr(&mc_list[10]);
1840         mc_list[5] = priv->port; /* needed for B0 steering support */
1841         mlx4_multicast_detach(mdev->dev, &priv->rss_map.indir_qp, mc_list,
1842                               MLX4_PROT_ETH, priv->broadcast_id);
1843         list_for_each_entry(mclist, &priv->curr_list, list) {
1844                 memcpy(&mc_list[10], mclist->addr, ETH_ALEN);
1845                 mc_list[5] = priv->port;
1846                 mlx4_multicast_detach(mdev->dev, &priv->rss_map.indir_qp,
1847                                       mc_list, MLX4_PROT_ETH, mclist->reg_id);
1848                 if (mclist->tunnel_reg_id)
1849                         mlx4_flow_detach(mdev->dev, mclist->tunnel_reg_id);
1850         }
1851         mlx4_en_clear_list(dev);
1852         list_for_each_entry_safe(mclist, tmp, &priv->curr_list, list) {
1853                 list_del(&mclist->list);
1854                 kfree(mclist);
1855         }
1856
1857         /* Flush multicast filter */
1858         mlx4_SET_MCAST_FLTR(mdev->dev, priv->port, 0, 1, MLX4_MCAST_CONFIG);
1859
1860         /* Remove flow steering rules for the port*/
1861         if (mdev->dev->caps.steering_mode ==
1862             MLX4_STEERING_MODE_DEVICE_MANAGED) {
1863                 ASSERT_RTNL();
1864                 list_for_each_entry_safe(flow, tmp_flow,
1865                                          &priv->ethtool_list, list) {
1866                         mlx4_flow_detach(mdev->dev, flow->id);
1867                         list_del(&flow->list);
1868                 }
1869         }
1870
1871         mlx4_en_destroy_drop_qp(priv);
1872
1873         /* Free TX Rings */
1874         for (i = 0; i < priv->tx_ring_num; i++) {
1875                 mlx4_en_deactivate_tx_ring(priv, priv->tx_ring[i]);
1876                 mlx4_en_deactivate_cq(priv, priv->tx_cq[i]);
1877         }
1878         msleep(10);
1879
1880         for (i = 0; i < priv->tx_ring_num; i++)
1881                 mlx4_en_free_tx_buf(dev, priv->tx_ring[i]);
1882
1883         if (mdev->dev->caps.steering_mode != MLX4_STEERING_MODE_A0)
1884                 mlx4_en_delete_rss_steer_rules(priv);
1885
1886         /* Free RSS qps */
1887         mlx4_en_release_rss_steer(priv);
1888
1889         /* Unregister Mac address for the port */
1890         mlx4_en_put_qp(priv);
1891         if (!(mdev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_REASSIGN_MAC_EN))
1892                 mdev->mac_removed[priv->port] = 1;
1893
1894         /* Free RX Rings */
1895         for (i = 0; i < priv->rx_ring_num; i++) {
1896                 struct mlx4_en_cq *cq = priv->rx_cq[i];
1897
1898                 napi_synchronize(&cq->napi);
1899                 mlx4_en_deactivate_rx_ring(priv, priv->rx_ring[i]);
1900                 mlx4_en_deactivate_cq(priv, cq);
1901
1902                 mlx4_en_free_affinity_hint(priv, i);
1903         }
1904 }
1905
1906 static void mlx4_en_restart(struct work_struct *work)
1907 {
1908         struct mlx4_en_priv *priv = container_of(work, struct mlx4_en_priv,
1909                                                  restart_task);
1910         struct mlx4_en_dev *mdev = priv->mdev;
1911         struct net_device *dev = priv->dev;
1912
1913         en_dbg(DRV, priv, "Watchdog task called for port %d\n", priv->port);
1914
1915         rtnl_lock();
1916         mutex_lock(&mdev->state_lock);
1917         if (priv->port_up) {
1918                 mlx4_en_stop_port(dev, 1);
1919                 if (mlx4_en_start_port(dev))
1920                         en_err(priv, "Failed restarting port %d\n", priv->port);
1921         }
1922         mutex_unlock(&mdev->state_lock);
1923         rtnl_unlock();
1924 }
1925
1926 static void mlx4_en_clear_stats(struct net_device *dev)
1927 {
1928         struct mlx4_en_priv *priv = netdev_priv(dev);
1929         struct mlx4_en_dev *mdev = priv->mdev;
1930         int i;
1931
1932         if (!mlx4_is_slave(mdev->dev))
1933                 if (mlx4_en_DUMP_ETH_STATS(mdev, priv->port, 1))
1934                         en_dbg(HW, priv, "Failed dumping statistics\n");
1935
1936         memset(&priv->pstats, 0, sizeof(priv->pstats));
1937         memset(&priv->pkstats, 0, sizeof(priv->pkstats));
1938         memset(&priv->port_stats, 0, sizeof(priv->port_stats));
1939         memset(&priv->rx_flowstats, 0, sizeof(priv->rx_flowstats));
1940         memset(&priv->tx_flowstats, 0, sizeof(priv->tx_flowstats));
1941         memset(&priv->rx_priority_flowstats, 0,
1942                sizeof(priv->rx_priority_flowstats));
1943         memset(&priv->tx_priority_flowstats, 0,
1944                sizeof(priv->tx_priority_flowstats));
1945         memset(&priv->pf_stats, 0, sizeof(priv->pf_stats));
1946
1947         for (i = 0; i < priv->tx_ring_num; i++) {
1948                 priv->tx_ring[i]->bytes = 0;
1949                 priv->tx_ring[i]->packets = 0;
1950                 priv->tx_ring[i]->tx_csum = 0;
1951                 priv->tx_ring[i]->tx_dropped = 0;
1952                 priv->tx_ring[i]->queue_stopped = 0;
1953                 priv->tx_ring[i]->wake_queue = 0;
1954                 priv->tx_ring[i]->tso_packets = 0;
1955                 priv->tx_ring[i]->xmit_more = 0;
1956         }
1957         for (i = 0; i < priv->rx_ring_num; i++) {
1958                 priv->rx_ring[i]->bytes = 0;
1959                 priv->rx_ring[i]->packets = 0;
1960                 priv->rx_ring[i]->csum_ok = 0;
1961                 priv->rx_ring[i]->csum_none = 0;
1962                 priv->rx_ring[i]->csum_complete = 0;
1963         }
1964 }
1965
1966 static int mlx4_en_open(struct net_device *dev)
1967 {
1968         struct mlx4_en_priv *priv = netdev_priv(dev);
1969         struct mlx4_en_dev *mdev = priv->mdev;
1970         int err = 0;
1971
1972         mutex_lock(&mdev->state_lock);
1973
1974         if (!mdev->device_up) {
1975                 en_err(priv, "Cannot open - device down/disabled\n");
1976                 err = -EBUSY;
1977                 goto out;
1978         }
1979
1980         /* Reset HW statistics and SW counters */
1981         mlx4_en_clear_stats(dev);
1982
1983         err = mlx4_en_start_port(dev);
1984         if (err)
1985                 en_err(priv, "Failed starting port:%d\n", priv->port);
1986
1987 out:
1988         mutex_unlock(&mdev->state_lock);
1989         return err;
1990 }
1991
1992
1993 static int mlx4_en_close(struct net_device *dev)
1994 {
1995         struct mlx4_en_priv *priv = netdev_priv(dev);
1996         struct mlx4_en_dev *mdev = priv->mdev;
1997
1998         en_dbg(IFDOWN, priv, "Close port called\n");
1999
2000         mutex_lock(&mdev->state_lock);
2001
2002         mlx4_en_stop_port(dev, 0);
2003         netif_carrier_off(dev);
2004
2005         mutex_unlock(&mdev->state_lock);
2006         return 0;
2007 }
2008
2009 static void mlx4_en_free_resources(struct mlx4_en_priv *priv)
2010 {
2011         int i;
2012
2013 #ifdef CONFIG_RFS_ACCEL
2014         priv->dev->rx_cpu_rmap = NULL;
2015 #endif
2016
2017         for (i = 0; i < priv->tx_ring_num; i++) {
2018                 if (priv->tx_ring && priv->tx_ring[i])
2019                         mlx4_en_destroy_tx_ring(priv, &priv->tx_ring[i]);
2020                 if (priv->tx_cq && priv->tx_cq[i])
2021                         mlx4_en_destroy_cq(priv, &priv->tx_cq[i]);
2022         }
2023
2024         for (i = 0; i < priv->rx_ring_num; i++) {
2025                 if (priv->rx_ring[i])
2026                         mlx4_en_destroy_rx_ring(priv, &priv->rx_ring[i],
2027                                 priv->prof->rx_ring_size, priv->stride);
2028                 if (priv->rx_cq[i])
2029                         mlx4_en_destroy_cq(priv, &priv->rx_cq[i]);
2030         }
2031
2032 }
2033
2034 static int mlx4_en_alloc_resources(struct mlx4_en_priv *priv)
2035 {
2036         struct mlx4_en_port_profile *prof = priv->prof;
2037         int i;
2038         int node;
2039
2040         /* Create tx Rings */
2041         for (i = 0; i < priv->tx_ring_num; i++) {
2042                 node = cpu_to_node(i % num_online_cpus());
2043                 if (mlx4_en_create_cq(priv, &priv->tx_cq[i],
2044                                       prof->tx_ring_size, i, TX, node))
2045                         goto err;
2046
2047                 if (mlx4_en_create_tx_ring(priv, &priv->tx_ring[i],
2048                                            prof->tx_ring_size, TXBB_SIZE,
2049                                            node, i))
2050                         goto err;
2051         }
2052
2053         /* Create rx Rings */
2054         for (i = 0; i < priv->rx_ring_num; i++) {
2055                 node = cpu_to_node(i % num_online_cpus());
2056                 if (mlx4_en_create_cq(priv, &priv->rx_cq[i],
2057                                       prof->rx_ring_size, i, RX, node))
2058                         goto err;
2059
2060                 if (mlx4_en_create_rx_ring(priv, &priv->rx_ring[i],
2061                                            prof->rx_ring_size, priv->stride,
2062                                            node))
2063                         goto err;
2064         }
2065
2066 #ifdef CONFIG_RFS_ACCEL
2067         priv->dev->rx_cpu_rmap = mlx4_get_cpu_rmap(priv->mdev->dev, priv->port);
2068 #endif
2069
2070         return 0;
2071
2072 err:
2073         en_err(priv, "Failed to allocate NIC resources\n");
2074         for (i = 0; i < priv->rx_ring_num; i++) {
2075                 if (priv->rx_ring[i])
2076                         mlx4_en_destroy_rx_ring(priv, &priv->rx_ring[i],
2077                                                 prof->rx_ring_size,
2078                                                 priv->stride);
2079                 if (priv->rx_cq[i])
2080                         mlx4_en_destroy_cq(priv, &priv->rx_cq[i]);
2081         }
2082         for (i = 0; i < priv->tx_ring_num; i++) {
2083                 if (priv->tx_ring[i])
2084                         mlx4_en_destroy_tx_ring(priv, &priv->tx_ring[i]);
2085                 if (priv->tx_cq[i])
2086                         mlx4_en_destroy_cq(priv, &priv->tx_cq[i]);
2087         }
2088         return -ENOMEM;
2089 }
2090
2091
2092 static int mlx4_en_copy_priv(struct mlx4_en_priv *dst,
2093                              struct mlx4_en_priv *src,
2094                              struct mlx4_en_port_profile *prof)
2095 {
2096         memcpy(&dst->hwtstamp_config, &prof->hwtstamp_config,
2097                sizeof(dst->hwtstamp_config));
2098         dst->num_tx_rings_p_up = src->mdev->profile.num_tx_rings_p_up;
2099         dst->tx_ring_num = prof->tx_ring_num;
2100         dst->rx_ring_num = prof->rx_ring_num;
2101         dst->flags = prof->flags;
2102         dst->mdev = src->mdev;
2103         dst->port = src->port;
2104         dst->dev = src->dev;
2105         dst->prof = prof;
2106         dst->stride = roundup_pow_of_two(sizeof(struct mlx4_en_rx_desc) +
2107                                          DS_SIZE * MLX4_EN_MAX_RX_FRAGS);
2108
2109         dst->tx_ring = kzalloc(sizeof(struct mlx4_en_tx_ring *) * MAX_TX_RINGS,
2110                                 GFP_KERNEL);
2111         if (!dst->tx_ring)
2112                 return -ENOMEM;
2113
2114         dst->tx_cq = kzalloc(sizeof(struct mlx4_en_cq *) * MAX_TX_RINGS,
2115                               GFP_KERNEL);
2116         if (!dst->tx_cq) {
2117                 kfree(dst->tx_ring);
2118                 return -ENOMEM;
2119         }
2120         return 0;
2121 }
2122
2123 static void mlx4_en_update_priv(struct mlx4_en_priv *dst,
2124                                 struct mlx4_en_priv *src)
2125 {
2126         memcpy(dst->rx_ring, src->rx_ring,
2127                sizeof(struct mlx4_en_rx_ring *) * src->rx_ring_num);
2128         memcpy(dst->rx_cq, src->rx_cq,
2129                sizeof(struct mlx4_en_cq *) * src->rx_ring_num);
2130         memcpy(&dst->hwtstamp_config, &src->hwtstamp_config,
2131                sizeof(dst->hwtstamp_config));
2132         dst->tx_ring_num = src->tx_ring_num;
2133         dst->rx_ring_num = src->rx_ring_num;
2134         dst->tx_ring = src->tx_ring;
2135         dst->tx_cq = src->tx_cq;
2136         memcpy(dst->prof, src->prof, sizeof(struct mlx4_en_port_profile));
2137 }
2138
2139 int mlx4_en_try_alloc_resources(struct mlx4_en_priv *priv,
2140                                 struct mlx4_en_priv *tmp,
2141                                 struct mlx4_en_port_profile *prof)
2142 {
2143         mlx4_en_copy_priv(tmp, priv, prof);
2144
2145         if (mlx4_en_alloc_resources(tmp)) {
2146                 en_warn(priv,
2147                         "%s: Resource allocation failed, using previous configuration\n",
2148                         __func__);
2149                 kfree(tmp->tx_ring);
2150                 kfree(tmp->tx_cq);
2151                 return -ENOMEM;
2152         }
2153         return 0;
2154 }
2155
2156 void mlx4_en_safe_replace_resources(struct mlx4_en_priv *priv,
2157                                     struct mlx4_en_priv *tmp)
2158 {
2159         mlx4_en_free_resources(priv);
2160         mlx4_en_update_priv(priv, tmp);
2161 }
2162
2163 void mlx4_en_destroy_netdev(struct net_device *dev)
2164 {
2165         struct mlx4_en_priv *priv = netdev_priv(dev);
2166         struct mlx4_en_dev *mdev = priv->mdev;
2167
2168         en_dbg(DRV, priv, "Destroying netdev on port:%d\n", priv->port);
2169
2170         /* Unregister device - this will close the port if it was up */
2171         if (priv->registered) {
2172                 devlink_port_type_clear(mlx4_get_devlink_port(mdev->dev,
2173                                                               priv->port));
2174                 unregister_netdev(dev);
2175         }
2176
2177         if (priv->allocated)
2178                 mlx4_free_hwq_res(mdev->dev, &priv->res, MLX4_EN_PAGE_SIZE);
2179
2180         cancel_delayed_work(&priv->stats_task);
2181         cancel_delayed_work(&priv->service_task);
2182         /* flush any pending task for this netdev */
2183         flush_workqueue(mdev->workqueue);
2184
2185         if (mdev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_TS)
2186                 mlx4_en_remove_timestamp(mdev);
2187
2188         /* Detach the netdev so tasks would not attempt to access it */
2189         mutex_lock(&mdev->state_lock);
2190         mdev->pndev[priv->port] = NULL;
2191         mdev->upper[priv->port] = NULL;
2192
2193 #ifdef CONFIG_RFS_ACCEL
2194         mlx4_en_cleanup_filters(priv);
2195 #endif
2196
2197         mlx4_en_free_resources(priv);
2198         mutex_unlock(&mdev->state_lock);
2199
2200         kfree(priv->tx_ring);
2201         kfree(priv->tx_cq);
2202
2203         free_netdev(dev);
2204 }
2205
2206 static int mlx4_en_change_mtu(struct net_device *dev, int new_mtu)
2207 {
2208         struct mlx4_en_priv *priv = netdev_priv(dev);
2209         struct mlx4_en_dev *mdev = priv->mdev;
2210         int err = 0;
2211
2212         en_dbg(DRV, priv, "Change MTU called - current:%d new:%d\n",
2213                  dev->mtu, new_mtu);
2214
2215         if ((new_mtu < MLX4_EN_MIN_MTU) || (new_mtu > priv->max_mtu)) {
2216                 en_err(priv, "Bad MTU size:%d.\n", new_mtu);
2217                 return -EPERM;
2218         }
2219         if (priv->xdp_ring_num && MLX4_EN_EFF_MTU(new_mtu) > FRAG_SZ0) {
2220                 en_err(priv, "MTU size:%d requires frags but XDP running\n",
2221                        new_mtu);
2222                 return -EOPNOTSUPP;
2223         }
2224         dev->mtu = new_mtu;
2225
2226         if (netif_running(dev)) {
2227                 mutex_lock(&mdev->state_lock);
2228                 if (!mdev->device_up) {
2229                         /* NIC is probably restarting - let restart task reset
2230                          * the port */
2231                         en_dbg(DRV, priv, "Change MTU called with card down!?\n");
2232                 } else {
2233                         mlx4_en_stop_port(dev, 1);
2234                         err = mlx4_en_start_port(dev);
2235                         if (err) {
2236                                 en_err(priv, "Failed restarting port:%d\n",
2237                                          priv->port);
2238                                 if (!test_and_set_bit(MLX4_EN_STATE_FLAG_RESTARTING,
2239                                                       &priv->state))
2240                                         queue_work(mdev->workqueue, &priv->restart_task);
2241                         }
2242                 }
2243                 mutex_unlock(&mdev->state_lock);
2244         }
2245         return 0;
2246 }
2247
2248 static int mlx4_en_hwtstamp_set(struct net_device *dev, struct ifreq *ifr)
2249 {
2250         struct mlx4_en_priv *priv = netdev_priv(dev);
2251         struct mlx4_en_dev *mdev = priv->mdev;
2252         struct hwtstamp_config config;
2253
2254         if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
2255                 return -EFAULT;
2256
2257         /* reserved for future extensions */
2258         if (config.flags)
2259                 return -EINVAL;
2260
2261         /* device doesn't support time stamping */
2262         if (!(mdev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_TS))
2263                 return -EINVAL;
2264
2265         /* TX HW timestamp */
2266         switch (config.tx_type) {
2267         case HWTSTAMP_TX_OFF:
2268         case HWTSTAMP_TX_ON:
2269                 break;
2270         default:
2271                 return -ERANGE;
2272         }
2273
2274         /* RX HW timestamp */
2275         switch (config.rx_filter) {
2276         case HWTSTAMP_FILTER_NONE:
2277                 break;
2278         case HWTSTAMP_FILTER_ALL:
2279         case HWTSTAMP_FILTER_SOME:
2280         case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
2281         case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
2282         case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
2283         case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
2284         case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
2285         case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
2286         case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
2287         case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
2288         case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
2289         case HWTSTAMP_FILTER_PTP_V2_EVENT:
2290         case HWTSTAMP_FILTER_PTP_V2_SYNC:
2291         case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
2292                 config.rx_filter = HWTSTAMP_FILTER_ALL;
2293                 break;
2294         default:
2295                 return -ERANGE;
2296         }
2297
2298         if (mlx4_en_reset_config(dev, config, dev->features)) {
2299                 config.tx_type = HWTSTAMP_TX_OFF;
2300                 config.rx_filter = HWTSTAMP_FILTER_NONE;
2301         }
2302
2303         return copy_to_user(ifr->ifr_data, &config,
2304                             sizeof(config)) ? -EFAULT : 0;
2305 }
2306
2307 static int mlx4_en_hwtstamp_get(struct net_device *dev, struct ifreq *ifr)
2308 {
2309         struct mlx4_en_priv *priv = netdev_priv(dev);
2310
2311         return copy_to_user(ifr->ifr_data, &priv->hwtstamp_config,
2312                             sizeof(priv->hwtstamp_config)) ? -EFAULT : 0;
2313 }
2314
2315 static int mlx4_en_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
2316 {
2317         switch (cmd) {
2318         case SIOCSHWTSTAMP:
2319                 return mlx4_en_hwtstamp_set(dev, ifr);
2320         case SIOCGHWTSTAMP:
2321                 return mlx4_en_hwtstamp_get(dev, ifr);
2322         default:
2323                 return -EOPNOTSUPP;
2324         }
2325 }
2326
2327 static netdev_features_t mlx4_en_fix_features(struct net_device *netdev,
2328                                               netdev_features_t features)
2329 {
2330         struct mlx4_en_priv *en_priv = netdev_priv(netdev);
2331         struct mlx4_en_dev *mdev = en_priv->mdev;
2332
2333         /* Since there is no support for separate RX C-TAG/S-TAG vlan accel
2334          * enable/disable make sure S-TAG flag is always in same state as
2335          * C-TAG.
2336          */
2337         if (features & NETIF_F_HW_VLAN_CTAG_RX &&
2338             !(mdev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_SKIP_OUTER_VLAN))
2339                 features |= NETIF_F_HW_VLAN_STAG_RX;
2340         else
2341                 features &= ~NETIF_F_HW_VLAN_STAG_RX;
2342
2343         return features;
2344 }
2345
2346 static int mlx4_en_set_features(struct net_device *netdev,
2347                 netdev_features_t features)
2348 {
2349         struct mlx4_en_priv *priv = netdev_priv(netdev);
2350         bool reset = false;
2351         int ret = 0;
2352
2353         if (DEV_FEATURE_CHANGED(netdev, features, NETIF_F_RXFCS)) {
2354                 en_info(priv, "Turn %s RX-FCS\n",
2355                         (features & NETIF_F_RXFCS) ? "ON" : "OFF");
2356                 reset = true;
2357         }
2358
2359         if (DEV_FEATURE_CHANGED(netdev, features, NETIF_F_RXALL)) {
2360                 u8 ignore_fcs_value = (features & NETIF_F_RXALL) ? 1 : 0;
2361
2362                 en_info(priv, "Turn %s RX-ALL\n",
2363                         ignore_fcs_value ? "ON" : "OFF");
2364                 ret = mlx4_SET_PORT_fcs_check(priv->mdev->dev,
2365                                               priv->port, ignore_fcs_value);
2366                 if (ret)
2367                         return ret;
2368         }
2369
2370         if (DEV_FEATURE_CHANGED(netdev, features, NETIF_F_HW_VLAN_CTAG_RX)) {
2371                 en_info(priv, "Turn %s RX vlan strip offload\n",
2372                         (features & NETIF_F_HW_VLAN_CTAG_RX) ? "ON" : "OFF");
2373                 reset = true;
2374         }
2375
2376         if (DEV_FEATURE_CHANGED(netdev, features, NETIF_F_HW_VLAN_CTAG_TX))
2377                 en_info(priv, "Turn %s TX vlan strip offload\n",
2378                         (features & NETIF_F_HW_VLAN_CTAG_TX) ? "ON" : "OFF");
2379
2380         if (DEV_FEATURE_CHANGED(netdev, features, NETIF_F_HW_VLAN_STAG_TX))
2381                 en_info(priv, "Turn %s TX S-VLAN strip offload\n",
2382                         (features & NETIF_F_HW_VLAN_STAG_TX) ? "ON" : "OFF");
2383
2384         if (DEV_FEATURE_CHANGED(netdev, features, NETIF_F_LOOPBACK)) {
2385                 en_info(priv, "Turn %s loopback\n",
2386                         (features & NETIF_F_LOOPBACK) ? "ON" : "OFF");
2387                 mlx4_en_update_loopback_state(netdev, features);
2388         }
2389
2390         if (reset) {
2391                 ret = mlx4_en_reset_config(netdev, priv->hwtstamp_config,
2392                                            features);
2393                 if (ret)
2394                         return ret;
2395         }
2396
2397         return 0;
2398 }
2399
2400 static int mlx4_en_set_vf_mac(struct net_device *dev, int queue, u8 *mac)
2401 {
2402         struct mlx4_en_priv *en_priv = netdev_priv(dev);
2403         struct mlx4_en_dev *mdev = en_priv->mdev;
2404         u64 mac_u64 = mlx4_mac_to_u64(mac);
2405
2406         if (is_multicast_ether_addr(mac))
2407                 return -EINVAL;
2408
2409         return mlx4_set_vf_mac(mdev->dev, en_priv->port, queue, mac_u64);
2410 }
2411
2412 static int mlx4_en_set_vf_vlan(struct net_device *dev, int vf, u16 vlan, u8 qos,
2413                                __be16 vlan_proto)
2414 {
2415         struct mlx4_en_priv *en_priv = netdev_priv(dev);
2416         struct mlx4_en_dev *mdev = en_priv->mdev;
2417
2418         return mlx4_set_vf_vlan(mdev->dev, en_priv->port, vf, vlan, qos,
2419                                 vlan_proto);
2420 }
2421
2422 static int mlx4_en_set_vf_rate(struct net_device *dev, int vf, int min_tx_rate,
2423                                int max_tx_rate)
2424 {
2425         struct mlx4_en_priv *en_priv = netdev_priv(dev);
2426         struct mlx4_en_dev *mdev = en_priv->mdev;
2427
2428         return mlx4_set_vf_rate(mdev->dev, en_priv->port, vf, min_tx_rate,
2429                                 max_tx_rate);
2430 }
2431
2432 static int mlx4_en_set_vf_spoofchk(struct net_device *dev, int vf, bool setting)
2433 {
2434         struct mlx4_en_priv *en_priv = netdev_priv(dev);
2435         struct mlx4_en_dev *mdev = en_priv->mdev;
2436
2437         return mlx4_set_vf_spoofchk(mdev->dev, en_priv->port, vf, setting);
2438 }
2439
2440 static int mlx4_en_get_vf_config(struct net_device *dev, int vf, struct ifla_vf_info *ivf)
2441 {
2442         struct mlx4_en_priv *en_priv = netdev_priv(dev);
2443         struct mlx4_en_dev *mdev = en_priv->mdev;
2444
2445         return mlx4_get_vf_config(mdev->dev, en_priv->port, vf, ivf);
2446 }
2447
2448 static int mlx4_en_set_vf_link_state(struct net_device *dev, int vf, int link_state)
2449 {
2450         struct mlx4_en_priv *en_priv = netdev_priv(dev);
2451         struct mlx4_en_dev *mdev = en_priv->mdev;
2452
2453         return mlx4_set_vf_link_state(mdev->dev, en_priv->port, vf, link_state);
2454 }
2455
2456 static int mlx4_en_get_vf_stats(struct net_device *dev, int vf,
2457                                 struct ifla_vf_stats *vf_stats)
2458 {
2459         struct mlx4_en_priv *en_priv = netdev_priv(dev);
2460         struct mlx4_en_dev *mdev = en_priv->mdev;
2461
2462         return mlx4_get_vf_stats(mdev->dev, en_priv->port, vf, vf_stats);
2463 }
2464
2465 #define PORT_ID_BYTE_LEN 8
2466 static int mlx4_en_get_phys_port_id(struct net_device *dev,
2467                                     struct netdev_phys_item_id *ppid)
2468 {
2469         struct mlx4_en_priv *priv = netdev_priv(dev);
2470         struct mlx4_dev *mdev = priv->mdev->dev;
2471         int i;
2472         u64 phys_port_id = mdev->caps.phys_port_id[priv->port];
2473
2474         if (!phys_port_id)
2475                 return -EOPNOTSUPP;
2476
2477         ppid->id_len = sizeof(phys_port_id);
2478         for (i = PORT_ID_BYTE_LEN - 1; i >= 0; --i) {
2479                 ppid->id[i] =  phys_port_id & 0xff;
2480                 phys_port_id >>= 8;
2481         }
2482         return 0;
2483 }
2484
2485 static void mlx4_en_add_vxlan_offloads(struct work_struct *work)
2486 {
2487         int ret;
2488         struct mlx4_en_priv *priv = container_of(work, struct mlx4_en_priv,
2489                                                  vxlan_add_task);
2490
2491         ret = mlx4_config_vxlan_port(priv->mdev->dev, priv->vxlan_port);
2492         if (ret)
2493                 goto out;
2494
2495         ret = mlx4_SET_PORT_VXLAN(priv->mdev->dev, priv->port,
2496                                   VXLAN_STEER_BY_OUTER_MAC, 1);
2497 out:
2498         if (ret) {
2499                 en_err(priv, "failed setting L2 tunnel configuration ret %d\n", ret);
2500                 return;
2501         }
2502
2503         /* set offloads */
2504         priv->dev->hw_enc_features |= NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
2505                                       NETIF_F_RXCSUM |
2506                                       NETIF_F_TSO | NETIF_F_TSO6 |
2507                                       NETIF_F_GSO_UDP_TUNNEL |
2508                                       NETIF_F_GSO_UDP_TUNNEL_CSUM |
2509                                       NETIF_F_GSO_PARTIAL;
2510 }
2511
2512 static void mlx4_en_del_vxlan_offloads(struct work_struct *work)
2513 {
2514         int ret;
2515         struct mlx4_en_priv *priv = container_of(work, struct mlx4_en_priv,
2516                                                  vxlan_del_task);
2517         /* unset offloads */
2518         priv->dev->hw_enc_features &= ~(NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
2519                                         NETIF_F_RXCSUM |
2520                                         NETIF_F_TSO | NETIF_F_TSO6 |
2521                                         NETIF_F_GSO_UDP_TUNNEL |
2522                                         NETIF_F_GSO_UDP_TUNNEL_CSUM |
2523                                         NETIF_F_GSO_PARTIAL);
2524
2525         ret = mlx4_SET_PORT_VXLAN(priv->mdev->dev, priv->port,
2526                                   VXLAN_STEER_BY_OUTER_MAC, 0);
2527         if (ret)
2528                 en_err(priv, "failed setting L2 tunnel configuration ret %d\n", ret);
2529
2530         priv->vxlan_port = 0;
2531 }
2532
2533 static void mlx4_en_add_vxlan_port(struct  net_device *dev,
2534                                    struct udp_tunnel_info *ti)
2535 {
2536         struct mlx4_en_priv *priv = netdev_priv(dev);
2537         __be16 port = ti->port;
2538         __be16 current_port;
2539
2540         if (ti->type != UDP_TUNNEL_TYPE_VXLAN)
2541                 return;
2542
2543         if (ti->sa_family != AF_INET)
2544                 return;
2545
2546         if (priv->mdev->dev->caps.tunnel_offload_mode != MLX4_TUNNEL_OFFLOAD_MODE_VXLAN)
2547                 return;
2548
2549         current_port = priv->vxlan_port;
2550         if (current_port && current_port != port) {
2551                 en_warn(priv, "vxlan port %d configured, can't add port %d\n",
2552                         ntohs(current_port), ntohs(port));
2553                 return;
2554         }
2555
2556         priv->vxlan_port = port;
2557         queue_work(priv->mdev->workqueue, &priv->vxlan_add_task);
2558 }
2559
2560 static void mlx4_en_del_vxlan_port(struct  net_device *dev,
2561                                    struct udp_tunnel_info *ti)
2562 {
2563         struct mlx4_en_priv *priv = netdev_priv(dev);
2564         __be16 port = ti->port;
2565         __be16 current_port;
2566
2567         if (ti->type != UDP_TUNNEL_TYPE_VXLAN)
2568                 return;
2569
2570         if (ti->sa_family != AF_INET)
2571                 return;
2572
2573         if (priv->mdev->dev->caps.tunnel_offload_mode != MLX4_TUNNEL_OFFLOAD_MODE_VXLAN)
2574                 return;
2575
2576         current_port = priv->vxlan_port;
2577         if (current_port != port) {
2578                 en_dbg(DRV, priv, "vxlan port %d isn't configured, ignoring\n", ntohs(port));
2579                 return;
2580         }
2581
2582         queue_work(priv->mdev->workqueue, &priv->vxlan_del_task);
2583 }
2584
2585 static netdev_features_t mlx4_en_features_check(struct sk_buff *skb,
2586                                                 struct net_device *dev,
2587                                                 netdev_features_t features)
2588 {
2589         features = vlan_features_check(skb, features);
2590         features = vxlan_features_check(skb, features);
2591
2592         /* The ConnectX-3 doesn't support outer IPv6 checksums but it does
2593          * support inner IPv6 checksums and segmentation so  we need to
2594          * strip that feature if this is an IPv6 encapsulated frame.
2595          */
2596         if (skb->encapsulation &&
2597             (skb->ip_summed == CHECKSUM_PARTIAL)) {
2598                 struct mlx4_en_priv *priv = netdev_priv(dev);
2599
2600                 if (!priv->vxlan_port ||
2601                     (ip_hdr(skb)->version != 4) ||
2602                     (udp_hdr(skb)->dest != priv->vxlan_port))
2603                         features &= ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
2604         }
2605
2606         return features;
2607 }
2608
2609 static int mlx4_en_set_tx_maxrate(struct net_device *dev, int queue_index, u32 maxrate)
2610 {
2611         struct mlx4_en_priv *priv = netdev_priv(dev);
2612         struct mlx4_en_tx_ring *tx_ring = priv->tx_ring[queue_index];
2613         struct mlx4_update_qp_params params;
2614         int err;
2615
2616         if (!(priv->mdev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_QP_RATE_LIMIT))
2617                 return -EOPNOTSUPP;
2618
2619         /* rate provided to us in Mbs, check if it fits into 12 bits, if not use Gbs */
2620         if (maxrate >> 12) {
2621                 params.rate_unit = MLX4_QP_RATE_LIMIT_GBS;
2622                 params.rate_val  = maxrate / 1000;
2623         } else if (maxrate) {
2624                 params.rate_unit = MLX4_QP_RATE_LIMIT_MBS;
2625                 params.rate_val  = maxrate;
2626         } else { /* zero serves to revoke the QP rate-limitation */
2627                 params.rate_unit = 0;
2628                 params.rate_val  = 0;
2629         }
2630
2631         err = mlx4_update_qp(priv->mdev->dev, tx_ring->qpn, MLX4_UPDATE_QP_RATE_LIMIT,
2632                              &params);
2633         return err;
2634 }
2635
2636 static int mlx4_xdp_set(struct net_device *dev, struct bpf_prog *prog)
2637 {
2638         struct mlx4_en_priv *priv = netdev_priv(dev);
2639         struct mlx4_en_dev *mdev = priv->mdev;
2640         struct bpf_prog *old_prog;
2641         int xdp_ring_num;
2642         int port_up = 0;
2643         int err;
2644         int i;
2645
2646         xdp_ring_num = prog ? ALIGN(priv->rx_ring_num, MLX4_EN_NUM_UP) : 0;
2647
2648         /* No need to reconfigure buffers when simply swapping the
2649          * program for a new one.
2650          */
2651         if (priv->xdp_ring_num == xdp_ring_num) {
2652                 if (prog) {
2653                         prog = bpf_prog_add(prog, priv->rx_ring_num - 1);
2654                         if (IS_ERR(prog))
2655                                 return PTR_ERR(prog);
2656                 }
2657                 mutex_lock(&mdev->state_lock);
2658                 for (i = 0; i < priv->rx_ring_num; i++) {
2659                         old_prog = rcu_dereference_protected(
2660                                         priv->rx_ring[i]->xdp_prog,
2661                                         lockdep_is_held(&mdev->state_lock));
2662                         rcu_assign_pointer(priv->rx_ring[i]->xdp_prog, prog);
2663                         if (old_prog)
2664                                 bpf_prog_put(old_prog);
2665                 }
2666                 mutex_unlock(&mdev->state_lock);
2667                 return 0;
2668         }
2669
2670         if (priv->num_frags > 1) {
2671                 en_err(priv, "Cannot set XDP if MTU requires multiple frags\n");
2672                 return -EOPNOTSUPP;
2673         }
2674
2675         if (priv->tx_ring_num < xdp_ring_num + MLX4_EN_NUM_UP) {
2676                 en_err(priv,
2677                        "Minimum %d tx channels required to run XDP\n",
2678                        (xdp_ring_num + MLX4_EN_NUM_UP) / MLX4_EN_NUM_UP);
2679                 return -EINVAL;
2680         }
2681
2682         if (prog) {
2683                 prog = bpf_prog_add(prog, priv->rx_ring_num - 1);
2684                 if (IS_ERR(prog))
2685                         return PTR_ERR(prog);
2686         }
2687
2688         mutex_lock(&mdev->state_lock);
2689         if (priv->port_up) {
2690                 port_up = 1;
2691                 mlx4_en_stop_port(dev, 1);
2692         }
2693
2694         priv->xdp_ring_num = xdp_ring_num;
2695         netif_set_real_num_tx_queues(dev, priv->tx_ring_num -
2696                                                         priv->xdp_ring_num);
2697
2698         for (i = 0; i < priv->rx_ring_num; i++) {
2699                 old_prog = rcu_dereference_protected(
2700                                         priv->rx_ring[i]->xdp_prog,
2701                                         lockdep_is_held(&mdev->state_lock));
2702                 rcu_assign_pointer(priv->rx_ring[i]->xdp_prog, prog);
2703                 if (old_prog)
2704                         bpf_prog_put(old_prog);
2705         }
2706
2707         if (port_up) {
2708                 err = mlx4_en_start_port(dev);
2709                 if (err) {
2710                         en_err(priv, "Failed starting port %d for XDP change\n",
2711                                priv->port);
2712                         if (!test_and_set_bit(MLX4_EN_STATE_FLAG_RESTARTING, &priv->state))
2713                                 queue_work(mdev->workqueue, &priv->restart_task);
2714                 }
2715         }
2716
2717         mutex_unlock(&mdev->state_lock);
2718         return 0;
2719 }
2720
2721 static bool mlx4_xdp_attached(struct net_device *dev)
2722 {
2723         struct mlx4_en_priv *priv = netdev_priv(dev);
2724
2725         return !!priv->xdp_ring_num;
2726 }
2727
2728 static int mlx4_xdp(struct net_device *dev, struct netdev_xdp *xdp)
2729 {
2730         switch (xdp->command) {
2731         case XDP_SETUP_PROG:
2732                 return mlx4_xdp_set(dev, xdp->prog);
2733         case XDP_QUERY_PROG:
2734                 xdp->prog_attached = mlx4_xdp_attached(dev);
2735                 return 0;
2736         default:
2737                 return -EINVAL;
2738         }
2739 }
2740
2741 static const struct net_device_ops mlx4_netdev_ops = {
2742         .ndo_open               = mlx4_en_open,
2743         .ndo_stop               = mlx4_en_close,
2744         .ndo_start_xmit         = mlx4_en_xmit,
2745         .ndo_select_queue       = mlx4_en_select_queue,
2746         .ndo_get_stats64        = mlx4_en_get_stats64,
2747         .ndo_set_rx_mode        = mlx4_en_set_rx_mode,
2748         .ndo_set_mac_address    = mlx4_en_set_mac,
2749         .ndo_validate_addr      = eth_validate_addr,
2750         .ndo_change_mtu         = mlx4_en_change_mtu,
2751         .ndo_do_ioctl           = mlx4_en_ioctl,
2752         .ndo_tx_timeout         = mlx4_en_tx_timeout,
2753         .ndo_vlan_rx_add_vid    = mlx4_en_vlan_rx_add_vid,
2754         .ndo_vlan_rx_kill_vid   = mlx4_en_vlan_rx_kill_vid,
2755 #ifdef CONFIG_NET_POLL_CONTROLLER
2756         .ndo_poll_controller    = mlx4_en_netpoll,
2757 #endif
2758         .ndo_set_features       = mlx4_en_set_features,
2759         .ndo_fix_features       = mlx4_en_fix_features,
2760         .ndo_setup_tc           = __mlx4_en_setup_tc,
2761 #ifdef CONFIG_RFS_ACCEL
2762         .ndo_rx_flow_steer      = mlx4_en_filter_rfs,
2763 #endif
2764         .ndo_get_phys_port_id   = mlx4_en_get_phys_port_id,
2765         .ndo_udp_tunnel_add     = mlx4_en_add_vxlan_port,
2766         .ndo_udp_tunnel_del     = mlx4_en_del_vxlan_port,
2767         .ndo_features_check     = mlx4_en_features_check,
2768         .ndo_set_tx_maxrate     = mlx4_en_set_tx_maxrate,
2769         .ndo_xdp                = mlx4_xdp,
2770 };
2771
2772 static const struct net_device_ops mlx4_netdev_ops_master = {
2773         .ndo_open               = mlx4_en_open,
2774         .ndo_stop               = mlx4_en_close,
2775         .ndo_start_xmit         = mlx4_en_xmit,
2776         .ndo_select_queue       = mlx4_en_select_queue,
2777         .ndo_get_stats64        = mlx4_en_get_stats64,
2778         .ndo_set_rx_mode        = mlx4_en_set_rx_mode,
2779         .ndo_set_mac_address    = mlx4_en_set_mac,
2780         .ndo_validate_addr      = eth_validate_addr,
2781         .ndo_change_mtu         = mlx4_en_change_mtu,
2782         .ndo_tx_timeout         = mlx4_en_tx_timeout,
2783         .ndo_vlan_rx_add_vid    = mlx4_en_vlan_rx_add_vid,
2784         .ndo_vlan_rx_kill_vid   = mlx4_en_vlan_rx_kill_vid,
2785         .ndo_set_vf_mac         = mlx4_en_set_vf_mac,
2786         .ndo_set_vf_vlan        = mlx4_en_set_vf_vlan,
2787         .ndo_set_vf_rate        = mlx4_en_set_vf_rate,
2788         .ndo_set_vf_spoofchk    = mlx4_en_set_vf_spoofchk,
2789         .ndo_set_vf_link_state  = mlx4_en_set_vf_link_state,
2790         .ndo_get_vf_stats       = mlx4_en_get_vf_stats,
2791         .ndo_get_vf_config      = mlx4_en_get_vf_config,
2792 #ifdef CONFIG_NET_POLL_CONTROLLER
2793         .ndo_poll_controller    = mlx4_en_netpoll,
2794 #endif
2795         .ndo_set_features       = mlx4_en_set_features,
2796         .ndo_fix_features       = mlx4_en_fix_features,
2797         .ndo_setup_tc           = __mlx4_en_setup_tc,
2798 #ifdef CONFIG_RFS_ACCEL
2799         .ndo_rx_flow_steer      = mlx4_en_filter_rfs,
2800 #endif
2801         .ndo_get_phys_port_id   = mlx4_en_get_phys_port_id,
2802         .ndo_udp_tunnel_add     = mlx4_en_add_vxlan_port,
2803         .ndo_udp_tunnel_del     = mlx4_en_del_vxlan_port,
2804         .ndo_features_check     = mlx4_en_features_check,
2805         .ndo_set_tx_maxrate     = mlx4_en_set_tx_maxrate,
2806         .ndo_xdp                = mlx4_xdp,
2807 };
2808
2809 struct mlx4_en_bond {
2810         struct work_struct work;
2811         struct mlx4_en_priv *priv;
2812         int is_bonded;
2813         struct mlx4_port_map port_map;
2814 };
2815
2816 static void mlx4_en_bond_work(struct work_struct *work)
2817 {
2818         struct mlx4_en_bond *bond = container_of(work,
2819                                                      struct mlx4_en_bond,
2820                                                      work);
2821         int err = 0;
2822         struct mlx4_dev *dev = bond->priv->mdev->dev;
2823
2824         if (bond->is_bonded) {
2825                 if (!mlx4_is_bonded(dev)) {
2826                         err = mlx4_bond(dev);
2827                         if (err)
2828                                 en_err(bond->priv, "Fail to bond device\n");
2829                 }
2830                 if (!err) {
2831                         err = mlx4_port_map_set(dev, &bond->port_map);
2832                         if (err)
2833                                 en_err(bond->priv, "Fail to set port map [%d][%d]: %d\n",
2834                                        bond->port_map.port1,
2835                                        bond->port_map.port2,
2836                                        err);
2837                 }
2838         } else if (mlx4_is_bonded(dev)) {
2839                 err = mlx4_unbond(dev);
2840                 if (err)
2841                         en_err(bond->priv, "Fail to unbond device\n");
2842         }
2843         dev_put(bond->priv->dev);
2844         kfree(bond);
2845 }
2846
2847 static int mlx4_en_queue_bond_work(struct mlx4_en_priv *priv, int is_bonded,
2848                                    u8 v2p_p1, u8 v2p_p2)
2849 {
2850         struct mlx4_en_bond *bond = NULL;
2851
2852         bond = kzalloc(sizeof(*bond), GFP_ATOMIC);
2853         if (!bond)
2854                 return -ENOMEM;
2855
2856         INIT_WORK(&bond->work, mlx4_en_bond_work);
2857         bond->priv = priv;
2858         bond->is_bonded = is_bonded;
2859         bond->port_map.port1 = v2p_p1;
2860         bond->port_map.port2 = v2p_p2;
2861         dev_hold(priv->dev);
2862         queue_work(priv->mdev->workqueue, &bond->work);
2863         return 0;
2864 }
2865
2866 int mlx4_en_netdev_event(struct notifier_block *this,
2867                          unsigned long event, void *ptr)
2868 {
2869         struct net_device *ndev = netdev_notifier_info_to_dev(ptr);
2870         u8 port = 0;
2871         struct mlx4_en_dev *mdev;
2872         struct mlx4_dev *dev;
2873         int i, num_eth_ports = 0;
2874         bool do_bond = true;
2875         struct mlx4_en_priv *priv;
2876         u8 v2p_port1 = 0;
2877         u8 v2p_port2 = 0;
2878
2879         if (!net_eq(dev_net(ndev), &init_net))
2880                 return NOTIFY_DONE;
2881
2882         mdev = container_of(this, struct mlx4_en_dev, nb);
2883         dev = mdev->dev;
2884
2885         /* Go into this mode only when two network devices set on two ports
2886          * of the same mlx4 device are slaves of the same bonding master
2887          */
2888         mlx4_foreach_port(i, dev, MLX4_PORT_TYPE_ETH) {
2889                 ++num_eth_ports;
2890                 if (!port && (mdev->pndev[i] == ndev))
2891                         port = i;
2892                 mdev->upper[i] = mdev->pndev[i] ?
2893                         netdev_master_upper_dev_get(mdev->pndev[i]) : NULL;
2894                 /* condition not met: network device is a slave */
2895                 if (!mdev->upper[i])
2896                         do_bond = false;
2897                 if (num_eth_ports < 2)
2898                         continue;
2899                 /* condition not met: same master */
2900                 if (mdev->upper[i] != mdev->upper[i-1])
2901                         do_bond = false;
2902         }
2903         /* condition not met: 2 salves */
2904         do_bond = (num_eth_ports ==  2) ? do_bond : false;
2905
2906         /* handle only events that come with enough info */
2907         if ((do_bond && (event != NETDEV_BONDING_INFO)) || !port)
2908                 return NOTIFY_DONE;
2909
2910         priv = netdev_priv(ndev);
2911         if (do_bond) {
2912                 struct netdev_notifier_bonding_info *notifier_info = ptr;
2913                 struct netdev_bonding_info *bonding_info =
2914                         &notifier_info->bonding_info;
2915
2916                 /* required mode 1, 2 or 4 */
2917                 if ((bonding_info->master.bond_mode != BOND_MODE_ACTIVEBACKUP) &&
2918                     (bonding_info->master.bond_mode != BOND_MODE_XOR) &&
2919                     (bonding_info->master.bond_mode != BOND_MODE_8023AD))
2920                         do_bond = false;
2921
2922                 /* require exactly 2 slaves */
2923                 if (bonding_info->master.num_slaves != 2)
2924                         do_bond = false;
2925
2926                 /* calc v2p */
2927                 if (do_bond) {
2928                         if (bonding_info->master.bond_mode ==
2929                             BOND_MODE_ACTIVEBACKUP) {
2930                                 /* in active-backup mode virtual ports are
2931                                  * mapped to the physical port of the active
2932                                  * slave */
2933                                 if (bonding_info->slave.state ==
2934                                     BOND_STATE_BACKUP) {
2935                                         if (port == 1) {
2936                                                 v2p_port1 = 2;
2937                                                 v2p_port2 = 2;
2938                                         } else {
2939                                                 v2p_port1 = 1;
2940                                                 v2p_port2 = 1;
2941                                         }
2942                                 } else { /* BOND_STATE_ACTIVE */
2943                                         if (port == 1) {
2944                                                 v2p_port1 = 1;
2945                                                 v2p_port2 = 1;
2946                                         } else {
2947                                                 v2p_port1 = 2;
2948                                                 v2p_port2 = 2;
2949                                         }
2950                                 }
2951                         } else { /* Active-Active */
2952                                 /* in active-active mode a virtual port is
2953                                  * mapped to the native physical port if and only
2954                                  * if the physical port is up */
2955                                 __s8 link = bonding_info->slave.link;
2956
2957                                 if (port == 1)
2958                                         v2p_port2 = 2;
2959                                 else
2960                                         v2p_port1 = 1;
2961                                 if ((link == BOND_LINK_UP) ||
2962                                     (link == BOND_LINK_FAIL)) {
2963                                         if (port == 1)
2964                                                 v2p_port1 = 1;
2965                                         else
2966                                                 v2p_port2 = 2;
2967                                 } else { /* BOND_LINK_DOWN || BOND_LINK_BACK */
2968                                         if (port == 1)
2969                                                 v2p_port1 = 2;
2970                                         else
2971                                                 v2p_port2 = 1;
2972                                 }
2973                         }
2974                 }
2975         }
2976
2977         mlx4_en_queue_bond_work(priv, do_bond,
2978                                 v2p_port1, v2p_port2);
2979
2980         return NOTIFY_DONE;
2981 }
2982
2983 void mlx4_en_update_pfc_stats_bitmap(struct mlx4_dev *dev,
2984                                      struct mlx4_en_stats_bitmap *stats_bitmap,
2985                                      u8 rx_ppp, u8 rx_pause,
2986                                      u8 tx_ppp, u8 tx_pause)
2987 {
2988         int last_i = NUM_MAIN_STATS + NUM_PORT_STATS + NUM_PF_STATS;
2989
2990         if (!mlx4_is_slave(dev) &&
2991             (dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_FLOWSTATS_EN)) {
2992                 mutex_lock(&stats_bitmap->mutex);
2993                 bitmap_clear(stats_bitmap->bitmap, last_i, NUM_FLOW_STATS);
2994
2995                 if (rx_ppp)
2996                         bitmap_set(stats_bitmap->bitmap, last_i,
2997                                    NUM_FLOW_PRIORITY_STATS_RX);
2998                 last_i += NUM_FLOW_PRIORITY_STATS_RX;
2999
3000                 if (rx_pause && !(rx_ppp))
3001                         bitmap_set(stats_bitmap->bitmap, last_i,
3002                                    NUM_FLOW_STATS_RX);
3003                 last_i += NUM_FLOW_STATS_RX;
3004
3005                 if (tx_ppp)
3006                         bitmap_set(stats_bitmap->bitmap, last_i,
3007                                    NUM_FLOW_PRIORITY_STATS_TX);
3008                 last_i += NUM_FLOW_PRIORITY_STATS_TX;
3009
3010                 if (tx_pause && !(tx_ppp))
3011                         bitmap_set(stats_bitmap->bitmap, last_i,
3012                                    NUM_FLOW_STATS_TX);
3013                 last_i += NUM_FLOW_STATS_TX;
3014
3015                 mutex_unlock(&stats_bitmap->mutex);
3016         }
3017 }
3018
3019 void mlx4_en_set_stats_bitmap(struct mlx4_dev *dev,
3020                               struct mlx4_en_stats_bitmap *stats_bitmap,
3021                               u8 rx_ppp, u8 rx_pause,
3022                               u8 tx_ppp, u8 tx_pause)
3023 {
3024         int last_i = 0;
3025
3026         mutex_init(&stats_bitmap->mutex);
3027         bitmap_zero(stats_bitmap->bitmap, NUM_ALL_STATS);
3028
3029         if (mlx4_is_slave(dev)) {
3030                 bitmap_set(stats_bitmap->bitmap, last_i +
3031                                          MLX4_FIND_NETDEV_STAT(rx_packets), 1);
3032                 bitmap_set(stats_bitmap->bitmap, last_i +
3033                                          MLX4_FIND_NETDEV_STAT(tx_packets), 1);
3034                 bitmap_set(stats_bitmap->bitmap, last_i +
3035                                          MLX4_FIND_NETDEV_STAT(rx_bytes), 1);
3036                 bitmap_set(stats_bitmap->bitmap, last_i +
3037                                          MLX4_FIND_NETDEV_STAT(tx_bytes), 1);
3038                 bitmap_set(stats_bitmap->bitmap, last_i +
3039                                          MLX4_FIND_NETDEV_STAT(rx_dropped), 1);
3040                 bitmap_set(stats_bitmap->bitmap, last_i +
3041                                          MLX4_FIND_NETDEV_STAT(tx_dropped), 1);
3042         } else {
3043                 bitmap_set(stats_bitmap->bitmap, last_i, NUM_MAIN_STATS);
3044         }
3045         last_i += NUM_MAIN_STATS;
3046
3047         bitmap_set(stats_bitmap->bitmap, last_i, NUM_PORT_STATS);
3048         last_i += NUM_PORT_STATS;
3049
3050         if (mlx4_is_master(dev))
3051                 bitmap_set(stats_bitmap->bitmap, last_i,
3052                            NUM_PF_STATS);
3053         last_i += NUM_PF_STATS;
3054
3055         mlx4_en_update_pfc_stats_bitmap(dev, stats_bitmap,
3056                                         rx_ppp, rx_pause,
3057                                         tx_ppp, tx_pause);
3058         last_i += NUM_FLOW_STATS;
3059
3060         if (!mlx4_is_slave(dev))
3061                 bitmap_set(stats_bitmap->bitmap, last_i, NUM_PKT_STATS);
3062 }
3063
3064 int mlx4_en_init_netdev(struct mlx4_en_dev *mdev, int port,
3065                         struct mlx4_en_port_profile *prof)
3066 {
3067         struct net_device *dev;
3068         struct mlx4_en_priv *priv;
3069         int i;
3070         int err;
3071
3072         dev = alloc_etherdev_mqs(sizeof(struct mlx4_en_priv),
3073                                  MAX_TX_RINGS, MAX_RX_RINGS);
3074         if (dev == NULL)
3075                 return -ENOMEM;
3076
3077         netif_set_real_num_tx_queues(dev, prof->tx_ring_num);
3078         netif_set_real_num_rx_queues(dev, prof->rx_ring_num);
3079
3080         SET_NETDEV_DEV(dev, &mdev->dev->persist->pdev->dev);
3081         dev->dev_port = port - 1;
3082
3083         /*
3084          * Initialize driver private data
3085          */
3086
3087         priv = netdev_priv(dev);
3088         memset(priv, 0, sizeof(struct mlx4_en_priv));
3089         priv->counter_index = MLX4_SINK_COUNTER_INDEX(mdev->dev);
3090         spin_lock_init(&priv->stats_lock);
3091         INIT_WORK(&priv->rx_mode_task, mlx4_en_do_set_rx_mode);
3092         INIT_WORK(&priv->restart_task, mlx4_en_restart);
3093         INIT_WORK(&priv->linkstate_task, mlx4_en_linkstate);
3094         INIT_DELAYED_WORK(&priv->stats_task, mlx4_en_do_get_stats);
3095         INIT_DELAYED_WORK(&priv->service_task, mlx4_en_service_task);
3096         INIT_WORK(&priv->vxlan_add_task, mlx4_en_add_vxlan_offloads);
3097         INIT_WORK(&priv->vxlan_del_task, mlx4_en_del_vxlan_offloads);
3098 #ifdef CONFIG_RFS_ACCEL
3099         INIT_LIST_HEAD(&priv->filters);
3100         spin_lock_init(&priv->filters_lock);
3101 #endif
3102
3103         priv->dev = dev;
3104         priv->mdev = mdev;
3105         priv->ddev = &mdev->pdev->dev;
3106         priv->prof = prof;
3107         priv->port = port;
3108         priv->port_up = false;
3109         priv->flags = prof->flags;
3110         priv->pflags = MLX4_EN_PRIV_FLAGS_BLUEFLAME;
3111         priv->ctrl_flags = cpu_to_be32(MLX4_WQE_CTRL_CQ_UPDATE |
3112                         MLX4_WQE_CTRL_SOLICITED);
3113         priv->num_tx_rings_p_up = mdev->profile.num_tx_rings_p_up;
3114         priv->tx_ring_num = prof->tx_ring_num;
3115         priv->tx_work_limit = MLX4_EN_DEFAULT_TX_WORK;
3116         netdev_rss_key_fill(priv->rss_key, sizeof(priv->rss_key));
3117
3118         priv->tx_ring = kzalloc(sizeof(struct mlx4_en_tx_ring *) * MAX_TX_RINGS,
3119                                 GFP_KERNEL);
3120         if (!priv->tx_ring) {
3121                 err = -ENOMEM;
3122                 goto out;
3123         }
3124         priv->tx_cq = kzalloc(sizeof(struct mlx4_en_cq *) * MAX_TX_RINGS,
3125                               GFP_KERNEL);
3126         if (!priv->tx_cq) {
3127                 err = -ENOMEM;
3128                 goto out;
3129         }
3130         priv->rx_ring_num = prof->rx_ring_num;
3131         priv->cqe_factor = (mdev->dev->caps.cqe_size == 64) ? 1 : 0;
3132         priv->cqe_size = mdev->dev->caps.cqe_size;
3133         priv->mac_index = -1;
3134         priv->msg_enable = MLX4_EN_MSG_LEVEL;
3135 #ifdef CONFIG_MLX4_EN_DCB
3136         if (!mlx4_is_slave(priv->mdev->dev)) {
3137                 u8 prio;
3138
3139                 for (prio = 0; prio < IEEE_8021QAZ_MAX_TCS; ++prio) {
3140                         priv->ets.prio_tc[prio] = prio;
3141                         priv->ets.tc_tsa[prio]  = IEEE_8021QAZ_TSA_VENDOR;
3142                 }
3143
3144                 priv->dcbx_cap = DCB_CAP_DCBX_VER_CEE | DCB_CAP_DCBX_HOST |
3145                         DCB_CAP_DCBX_VER_IEEE;
3146                 priv->flags |= MLX4_EN_DCB_ENABLED;
3147                 priv->cee_config.pfc_state = false;
3148
3149                 for (i = 0; i < MLX4_EN_NUM_UP; i++)
3150                         priv->cee_config.dcb_pfc[i] = pfc_disabled;
3151
3152                 if (mdev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_ETS_CFG) {
3153                         dev->dcbnl_ops = &mlx4_en_dcbnl_ops;
3154                 } else {
3155                         en_info(priv, "enabling only PFC DCB ops\n");
3156                         dev->dcbnl_ops = &mlx4_en_dcbnl_pfc_ops;
3157                 }
3158         }
3159 #endif
3160
3161         for (i = 0; i < MLX4_EN_MAC_HASH_SIZE; ++i)
3162                 INIT_HLIST_HEAD(&priv->mac_hash[i]);
3163
3164         /* Query for default mac and max mtu */
3165         priv->max_mtu = mdev->dev->caps.eth_mtu_cap[priv->port];
3166
3167         if (mdev->dev->caps.rx_checksum_flags_port[priv->port] &
3168             MLX4_RX_CSUM_MODE_VAL_NON_TCP_UDP)
3169                 priv->flags |= MLX4_EN_FLAG_RX_CSUM_NON_TCP_UDP;
3170
3171         /* Set default MAC */
3172         dev->addr_len = ETH_ALEN;
3173         mlx4_en_u64_to_mac(dev->dev_addr, mdev->dev->caps.def_mac[priv->port]);
3174         if (!is_valid_ether_addr(dev->dev_addr)) {
3175                 en_err(priv, "Port: %d, invalid mac burned: %pM, quiting\n",
3176                        priv->port, dev->dev_addr);
3177                 err = -EINVAL;
3178                 goto out;
3179         } else if (mlx4_is_slave(priv->mdev->dev) &&
3180                    (priv->mdev->dev->port_random_macs & 1 << priv->port)) {
3181                 /* Random MAC was assigned in mlx4_slave_cap
3182                  * in mlx4_core module
3183                  */
3184                 dev->addr_assign_type |= NET_ADDR_RANDOM;
3185                 en_warn(priv, "Assigned random MAC address %pM\n", dev->dev_addr);
3186         }
3187
3188         memcpy(priv->current_mac, dev->dev_addr, sizeof(priv->current_mac));
3189
3190         priv->stride = roundup_pow_of_two(sizeof(struct mlx4_en_rx_desc) +
3191                                           DS_SIZE * MLX4_EN_MAX_RX_FRAGS);
3192         err = mlx4_en_alloc_resources(priv);
3193         if (err)
3194                 goto out;
3195
3196         /* Initialize time stamping config */
3197         priv->hwtstamp_config.flags = 0;
3198         priv->hwtstamp_config.tx_type = HWTSTAMP_TX_OFF;
3199         priv->hwtstamp_config.rx_filter = HWTSTAMP_FILTER_NONE;
3200
3201         /* Allocate page for receive rings */
3202         err = mlx4_alloc_hwq_res(mdev->dev, &priv->res,
3203                                 MLX4_EN_PAGE_SIZE);
3204         if (err) {
3205                 en_err(priv, "Failed to allocate page for rx qps\n");
3206                 goto out;
3207         }
3208         priv->allocated = 1;
3209
3210         /*
3211          * Initialize netdev entry points
3212          */
3213         if (mlx4_is_master(priv->mdev->dev))
3214                 dev->netdev_ops = &mlx4_netdev_ops_master;
3215         else
3216                 dev->netdev_ops = &mlx4_netdev_ops;
3217         dev->watchdog_timeo = MLX4_EN_WATCHDOG_TIMEOUT;
3218         netif_set_real_num_tx_queues(dev, priv->tx_ring_num);
3219         netif_set_real_num_rx_queues(dev, priv->rx_ring_num);
3220
3221         dev->ethtool_ops = &mlx4_en_ethtool_ops;
3222
3223         /*
3224          * Set driver features
3225          */
3226         dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM;
3227         if (mdev->LSO_support)
3228                 dev->hw_features |= NETIF_F_TSO | NETIF_F_TSO6;
3229
3230         dev->vlan_features = dev->hw_features;
3231
3232         dev->hw_features |= NETIF_F_RXCSUM | NETIF_F_RXHASH;
3233         dev->features = dev->hw_features | NETIF_F_HIGHDMA |
3234                         NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX |
3235                         NETIF_F_HW_VLAN_CTAG_FILTER;
3236         dev->hw_features |= NETIF_F_LOOPBACK |
3237                         NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX;
3238
3239         if (!(mdev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_SKIP_OUTER_VLAN)) {
3240                 dev->features |= NETIF_F_HW_VLAN_STAG_RX |
3241                         NETIF_F_HW_VLAN_STAG_FILTER;
3242                 dev->hw_features |= NETIF_F_HW_VLAN_STAG_RX;
3243         }
3244
3245         if (mlx4_is_slave(mdev->dev)) {
3246                 bool vlan_offload_disabled;
3247                 int phv;
3248
3249                 err = get_phv_bit(mdev->dev, port, &phv);
3250                 if (!err && phv) {
3251                         dev->hw_features |= NETIF_F_HW_VLAN_STAG_TX;
3252                         priv->pflags |= MLX4_EN_PRIV_FLAGS_PHV;
3253                 }
3254                 err = mlx4_get_is_vlan_offload_disabled(mdev->dev, port,
3255                                                         &vlan_offload_disabled);
3256                 if (!err && vlan_offload_disabled) {
3257                         dev->hw_features &= ~(NETIF_F_HW_VLAN_CTAG_TX |
3258                                               NETIF_F_HW_VLAN_CTAG_RX |
3259                                               NETIF_F_HW_VLAN_STAG_TX |
3260                                               NETIF_F_HW_VLAN_STAG_RX);
3261                         dev->features &= ~(NETIF_F_HW_VLAN_CTAG_TX |
3262                                            NETIF_F_HW_VLAN_CTAG_RX |
3263                                            NETIF_F_HW_VLAN_STAG_TX |
3264                                            NETIF_F_HW_VLAN_STAG_RX);
3265                 }
3266         } else {
3267                 if (mdev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_PHV_EN &&
3268                     !(mdev->dev->caps.flags2 &
3269                       MLX4_DEV_CAP_FLAG2_SKIP_OUTER_VLAN))
3270                         dev->hw_features |= NETIF_F_HW_VLAN_STAG_TX;
3271         }
3272
3273         if (mdev->dev->caps.flags & MLX4_DEV_CAP_FLAG_FCS_KEEP)
3274                 dev->hw_features |= NETIF_F_RXFCS;
3275
3276         if (mdev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_IGNORE_FCS)
3277                 dev->hw_features |= NETIF_F_RXALL;
3278
3279         if (mdev->dev->caps.steering_mode ==
3280             MLX4_STEERING_MODE_DEVICE_MANAGED &&
3281             mdev->dev->caps.dmfs_high_steer_mode != MLX4_STEERING_DMFS_A0_STATIC)
3282                 dev->hw_features |= NETIF_F_NTUPLE;
3283
3284         if (mdev->dev->caps.steering_mode != MLX4_STEERING_MODE_A0)
3285                 dev->priv_flags |= IFF_UNICAST_FLT;
3286
3287         /* Setting a default hash function value */
3288         if (mdev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_RSS_TOP) {
3289                 priv->rss_hash_fn = ETH_RSS_HASH_TOP;
3290         } else if (mdev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_RSS_XOR) {
3291                 priv->rss_hash_fn = ETH_RSS_HASH_XOR;
3292         } else {
3293                 en_warn(priv,
3294                         "No RSS hash capabilities exposed, using Toeplitz\n");
3295                 priv->rss_hash_fn = ETH_RSS_HASH_TOP;
3296         }
3297
3298         if (mdev->dev->caps.tunnel_offload_mode == MLX4_TUNNEL_OFFLOAD_MODE_VXLAN) {
3299                 dev->hw_features |= NETIF_F_GSO_UDP_TUNNEL |
3300                                     NETIF_F_GSO_UDP_TUNNEL_CSUM |
3301                                     NETIF_F_GSO_PARTIAL;
3302                 dev->features    |= NETIF_F_GSO_UDP_TUNNEL |
3303                                     NETIF_F_GSO_UDP_TUNNEL_CSUM |
3304                                     NETIF_F_GSO_PARTIAL;
3305                 dev->gso_partial_features = NETIF_F_GSO_UDP_TUNNEL_CSUM;
3306         }
3307
3308         mdev->pndev[port] = dev;
3309         mdev->upper[port] = NULL;
3310
3311         netif_carrier_off(dev);
3312         mlx4_en_set_default_moderation(priv);
3313
3314         en_warn(priv, "Using %d TX rings\n", prof->tx_ring_num);
3315         en_warn(priv, "Using %d RX rings\n", prof->rx_ring_num);
3316
3317         mlx4_en_update_loopback_state(priv->dev, priv->dev->features);
3318
3319         /* Configure port */
3320         mlx4_en_calc_rx_buf(dev);
3321         err = mlx4_SET_PORT_general(mdev->dev, priv->port,
3322                                     priv->rx_skb_size + ETH_FCS_LEN,
3323                                     prof->tx_pause, prof->tx_ppp,
3324                                     prof->rx_pause, prof->rx_ppp);
3325         if (err) {
3326                 en_err(priv, "Failed setting port general configurations for port %d, with error %d\n",
3327                        priv->port, err);
3328                 goto out;
3329         }
3330
3331         if (mdev->dev->caps.tunnel_offload_mode == MLX4_TUNNEL_OFFLOAD_MODE_VXLAN) {
3332                 err = mlx4_SET_PORT_VXLAN(mdev->dev, priv->port, VXLAN_STEER_BY_OUTER_MAC, 1);
3333                 if (err) {
3334                         en_err(priv, "Failed setting port L2 tunnel configuration, err %d\n",
3335                                err);
3336                         goto out;
3337                 }
3338         }
3339
3340         /* Init port */
3341         en_warn(priv, "Initializing port\n");
3342         err = mlx4_INIT_PORT(mdev->dev, priv->port);
3343         if (err) {
3344                 en_err(priv, "Failed Initializing port\n");
3345                 goto out;
3346         }
3347         queue_delayed_work(mdev->workqueue, &priv->stats_task, STATS_DELAY);
3348
3349         /* Initialize time stamp mechanism */
3350         if (mdev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_TS)
3351                 mlx4_en_init_timestamp(mdev);
3352
3353         queue_delayed_work(mdev->workqueue, &priv->service_task,
3354                            SERVICE_TASK_DELAY);
3355
3356         mlx4_en_set_stats_bitmap(mdev->dev, &priv->stats_bitmap,
3357                                  mdev->profile.prof[priv->port].rx_ppp,
3358                                  mdev->profile.prof[priv->port].rx_pause,
3359                                  mdev->profile.prof[priv->port].tx_ppp,
3360                                  mdev->profile.prof[priv->port].tx_pause);
3361
3362         err = register_netdev(dev);
3363         if (err) {
3364                 en_err(priv, "Netdev registration failed for port %d\n", port);
3365                 goto out;
3366         }
3367
3368         priv->registered = 1;
3369         devlink_port_type_eth_set(mlx4_get_devlink_port(mdev->dev, priv->port),
3370                                   dev);
3371
3372         return 0;
3373
3374 out:
3375         mlx4_en_destroy_netdev(dev);
3376         return err;
3377 }
3378
3379 int mlx4_en_reset_config(struct net_device *dev,
3380                          struct hwtstamp_config ts_config,
3381                          netdev_features_t features)
3382 {
3383         struct mlx4_en_priv *priv = netdev_priv(dev);
3384         struct mlx4_en_dev *mdev = priv->mdev;
3385         struct mlx4_en_port_profile new_prof;
3386         struct mlx4_en_priv *tmp;
3387         int port_up = 0;
3388         int err = 0;
3389
3390         if (priv->hwtstamp_config.tx_type == ts_config.tx_type &&
3391             priv->hwtstamp_config.rx_filter == ts_config.rx_filter &&
3392             !DEV_FEATURE_CHANGED(dev, features, NETIF_F_HW_VLAN_CTAG_RX) &&
3393             !DEV_FEATURE_CHANGED(dev, features, NETIF_F_RXFCS))
3394                 return 0; /* Nothing to change */
3395
3396         if (DEV_FEATURE_CHANGED(dev, features, NETIF_F_HW_VLAN_CTAG_RX) &&
3397             (features & NETIF_F_HW_VLAN_CTAG_RX) &&
3398             (priv->hwtstamp_config.rx_filter != HWTSTAMP_FILTER_NONE)) {
3399                 en_warn(priv, "Can't turn ON rx vlan offload while time-stamping rx filter is ON\n");
3400                 return -EINVAL;
3401         }
3402
3403         tmp = kzalloc(sizeof(*tmp), GFP_KERNEL);
3404         if (!tmp)
3405                 return -ENOMEM;
3406
3407         mutex_lock(&mdev->state_lock);
3408
3409         memcpy(&new_prof, priv->prof, sizeof(struct mlx4_en_port_profile));
3410         memcpy(&new_prof.hwtstamp_config, &ts_config, sizeof(ts_config));
3411
3412         err = mlx4_en_try_alloc_resources(priv, tmp, &new_prof);
3413         if (err)
3414                 goto out;
3415
3416         if (priv->port_up) {
3417                 port_up = 1;
3418                 mlx4_en_stop_port(dev, 1);
3419         }
3420
3421         en_warn(priv, "Changing device configuration rx filter(%x) rx vlan(%x)\n",
3422                 ts_config.rx_filter,
3423                 !!(features & NETIF_F_HW_VLAN_CTAG_RX));
3424
3425         mlx4_en_safe_replace_resources(priv, tmp);
3426
3427         if (DEV_FEATURE_CHANGED(dev, features, NETIF_F_HW_VLAN_CTAG_RX)) {
3428                 if (features & NETIF_F_HW_VLAN_CTAG_RX)
3429                         dev->features |= NETIF_F_HW_VLAN_CTAG_RX;
3430                 else
3431                         dev->features &= ~NETIF_F_HW_VLAN_CTAG_RX;
3432         } else if (ts_config.rx_filter == HWTSTAMP_FILTER_NONE) {
3433                 /* RX time-stamping is OFF, update the RX vlan offload
3434                  * to the latest wanted state
3435                  */
3436                 if (dev->wanted_features & NETIF_F_HW_VLAN_CTAG_RX)
3437                         dev->features |= NETIF_F_HW_VLAN_CTAG_RX;
3438                 else
3439                         dev->features &= ~NETIF_F_HW_VLAN_CTAG_RX;
3440         }
3441
3442         if (DEV_FEATURE_CHANGED(dev, features, NETIF_F_RXFCS)) {
3443                 if (features & NETIF_F_RXFCS)
3444                         dev->features |= NETIF_F_RXFCS;
3445                 else
3446                         dev->features &= ~NETIF_F_RXFCS;
3447         }
3448
3449         /* RX vlan offload and RX time-stamping can't co-exist !
3450          * Regardless of the caller's choice,
3451          * Turn Off RX vlan offload in case of time-stamping is ON
3452          */
3453         if (ts_config.rx_filter != HWTSTAMP_FILTER_NONE) {
3454                 if (dev->features & NETIF_F_HW_VLAN_CTAG_RX)
3455                         en_warn(priv, "Turning off RX vlan offload since RX time-stamping is ON\n");
3456                 dev->features &= ~NETIF_F_HW_VLAN_CTAG_RX;
3457         }
3458
3459         if (port_up) {
3460                 err = mlx4_en_start_port(dev);
3461                 if (err)
3462                         en_err(priv, "Failed starting port\n");
3463         }
3464
3465         if (!err)
3466                 err = mlx4_en_moderation_update(priv);
3467 out:
3468         mutex_unlock(&mdev->state_lock);
3469         kfree(tmp);
3470         if (!err)
3471                 netdev_features_change(dev);
3472         return err;
3473 }