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