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