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