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