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