]> Pileus Git - ~andy/linux/blob - drivers/net/ethernet/mellanox/mlx4/en_netdev.c
ba212611157324c2147080afd95c2d5966b76e36
[~andy/linux] / 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
43 #include <linux/mlx4/driver.h>
44 #include <linux/mlx4/device.h>
45 #include <linux/mlx4/cmd.h>
46 #include <linux/mlx4/cq.h>
47
48 #include "mlx4_en.h"
49 #include "en_port.h"
50
51 int mlx4_en_setup_tc(struct net_device *dev, u8 up)
52 {
53         struct mlx4_en_priv *priv = netdev_priv(dev);
54         int i;
55         unsigned int offset = 0;
56
57         if (up && up != MLX4_EN_NUM_UP)
58                 return -EINVAL;
59
60         netdev_set_num_tc(dev, up);
61
62         /* Partition Tx queues evenly amongst UP's */
63         for (i = 0; i < up; i++) {
64                 netdev_set_tc_queue(dev, i, priv->num_tx_rings_p_up, offset);
65                 offset += priv->num_tx_rings_p_up;
66         }
67
68         return 0;
69 }
70
71 #ifdef CONFIG_NET_RX_BUSY_POLL
72 /* must be called with local_bh_disable()d */
73 static int mlx4_en_low_latency_recv(struct napi_struct *napi)
74 {
75         struct mlx4_en_cq *cq = container_of(napi, struct mlx4_en_cq, napi);
76         struct net_device *dev = cq->dev;
77         struct mlx4_en_priv *priv = netdev_priv(dev);
78         struct mlx4_en_rx_ring *rx_ring = priv->rx_ring[cq->ring];
79         int done;
80
81         if (!priv->port_up)
82                 return LL_FLUSH_FAILED;
83
84         if (!mlx4_en_cq_lock_poll(cq))
85                 return LL_FLUSH_BUSY;
86
87         done = mlx4_en_process_rx_cq(dev, cq, 4);
88         if (likely(done))
89                 rx_ring->cleaned += done;
90         else
91                 rx_ring->misses++;
92
93         mlx4_en_cq_unlock_poll(cq);
94
95         return done;
96 }
97 #endif  /* CONFIG_NET_RX_BUSY_POLL */
98
99 #ifdef CONFIG_RFS_ACCEL
100
101 struct mlx4_en_filter {
102         struct list_head next;
103         struct work_struct work;
104
105         u8     ip_proto;
106         __be32 src_ip;
107         __be32 dst_ip;
108         __be16 src_port;
109         __be16 dst_port;
110
111         int rxq_index;
112         struct mlx4_en_priv *priv;
113         u32 flow_id;                    /* RFS infrastructure id */
114         int id;                         /* mlx4_en driver id */
115         u64 reg_id;                     /* Flow steering API id */
116         u8 activated;                   /* Used to prevent expiry before filter
117                                          * is attached
118                                          */
119         struct hlist_node filter_chain;
120 };
121
122 static void mlx4_en_filter_rfs_expire(struct mlx4_en_priv *priv);
123
124 static enum mlx4_net_trans_rule_id mlx4_ip_proto_to_trans_rule_id(u8 ip_proto)
125 {
126         switch (ip_proto) {
127         case IPPROTO_UDP:
128                 return MLX4_NET_TRANS_RULE_ID_UDP;
129         case IPPROTO_TCP:
130                 return MLX4_NET_TRANS_RULE_ID_TCP;
131         default:
132                 return -EPROTONOSUPPORT;
133         }
134 };
135
136 static void mlx4_en_filter_work(struct work_struct *work)
137 {
138         struct mlx4_en_filter *filter = container_of(work,
139                                                      struct mlx4_en_filter,
140                                                      work);
141         struct mlx4_en_priv *priv = filter->priv;
142         struct mlx4_spec_list spec_tcp_udp = {
143                 .id = mlx4_ip_proto_to_trans_rule_id(filter->ip_proto),
144                 {
145                         .tcp_udp = {
146                                 .dst_port = filter->dst_port,
147                                 .dst_port_msk = (__force __be16)-1,
148                                 .src_port = filter->src_port,
149                                 .src_port_msk = (__force __be16)-1,
150                         },
151                 },
152         };
153         struct mlx4_spec_list spec_ip = {
154                 .id = MLX4_NET_TRANS_RULE_ID_IPV4,
155                 {
156                         .ipv4 = {
157                                 .dst_ip = filter->dst_ip,
158                                 .dst_ip_msk = (__force __be32)-1,
159                                 .src_ip = filter->src_ip,
160                                 .src_ip_msk = (__force __be32)-1,
161                         },
162                 },
163         };
164         struct mlx4_spec_list spec_eth = {
165                 .id = MLX4_NET_TRANS_RULE_ID_ETH,
166         };
167         struct mlx4_net_trans_rule rule = {
168                 .list = LIST_HEAD_INIT(rule.list),
169                 .queue_mode = MLX4_NET_TRANS_Q_LIFO,
170                 .exclusive = 1,
171                 .allow_loopback = 1,
172                 .promisc_mode = MLX4_FS_REGULAR,
173                 .port = priv->port,
174                 .priority = MLX4_DOMAIN_RFS,
175         };
176         int rc;
177         __be64 mac_mask = cpu_to_be64(MLX4_MAC_MASK << 16);
178
179         if (spec_tcp_udp.id < 0) {
180                 en_warn(priv, "RFS: ignoring unsupported ip protocol (%d)\n",
181                         filter->ip_proto);
182                 goto ignore;
183         }
184         list_add_tail(&spec_eth.list, &rule.list);
185         list_add_tail(&spec_ip.list, &rule.list);
186         list_add_tail(&spec_tcp_udp.list, &rule.list);
187
188         rule.qpn = priv->rss_map.qps[filter->rxq_index].qpn;
189         memcpy(spec_eth.eth.dst_mac, priv->dev->dev_addr, ETH_ALEN);
190         memcpy(spec_eth.eth.dst_mac_msk, &mac_mask, ETH_ALEN);
191
192         filter->activated = 0;
193
194         if (filter->reg_id) {
195                 rc = mlx4_flow_detach(priv->mdev->dev, filter->reg_id);
196                 if (rc && rc != -ENOENT)
197                         en_err(priv, "Error detaching flow. rc = %d\n", rc);
198         }
199
200         rc = mlx4_flow_attach(priv->mdev->dev, &rule, &filter->reg_id);
201         if (rc)
202                 en_err(priv, "Error attaching flow. err = %d\n", rc);
203
204 ignore:
205         mlx4_en_filter_rfs_expire(priv);
206
207         filter->activated = 1;
208 }
209
210 static inline struct hlist_head *
211 filter_hash_bucket(struct mlx4_en_priv *priv, __be32 src_ip, __be32 dst_ip,
212                    __be16 src_port, __be16 dst_port)
213 {
214         unsigned long l;
215         int bucket_idx;
216
217         l = (__force unsigned long)src_port |
218             ((__force unsigned long)dst_port << 2);
219         l ^= (__force unsigned long)(src_ip ^ dst_ip);
220
221         bucket_idx = hash_long(l, MLX4_EN_FILTER_HASH_SHIFT);
222
223         return &priv->filter_hash[bucket_idx];
224 }
225
226 static struct mlx4_en_filter *
227 mlx4_en_filter_alloc(struct mlx4_en_priv *priv, int rxq_index, __be32 src_ip,
228                      __be32 dst_ip, u8 ip_proto, __be16 src_port,
229                      __be16 dst_port, u32 flow_id)
230 {
231         struct mlx4_en_filter *filter = NULL;
232
233         filter = kzalloc(sizeof(struct mlx4_en_filter), GFP_ATOMIC);
234         if (!filter)
235                 return NULL;
236
237         filter->priv = priv;
238         filter->rxq_index = rxq_index;
239         INIT_WORK(&filter->work, mlx4_en_filter_work);
240
241         filter->src_ip = src_ip;
242         filter->dst_ip = dst_ip;
243         filter->ip_proto = ip_proto;
244         filter->src_port = src_port;
245         filter->dst_port = dst_port;
246
247         filter->flow_id = flow_id;
248
249         filter->id = priv->last_filter_id++ % RPS_NO_FILTER;
250
251         list_add_tail(&filter->next, &priv->filters);
252         hlist_add_head(&filter->filter_chain,
253                        filter_hash_bucket(priv, src_ip, dst_ip, src_port,
254                                           dst_port));
255
256         return filter;
257 }
258
259 static void mlx4_en_filter_free(struct mlx4_en_filter *filter)
260 {
261         struct mlx4_en_priv *priv = filter->priv;
262         int rc;
263
264         list_del(&filter->next);
265
266         rc = mlx4_flow_detach(priv->mdev->dev, filter->reg_id);
267         if (rc && rc != -ENOENT)
268                 en_err(priv, "Error detaching flow. rc = %d\n", rc);
269
270         kfree(filter);
271 }
272
273 static inline struct mlx4_en_filter *
274 mlx4_en_filter_find(struct mlx4_en_priv *priv, __be32 src_ip, __be32 dst_ip,
275                     u8 ip_proto, __be16 src_port, __be16 dst_port)
276 {
277         struct mlx4_en_filter *filter;
278         struct mlx4_en_filter *ret = NULL;
279
280         hlist_for_each_entry(filter,
281                              filter_hash_bucket(priv, src_ip, dst_ip,
282                                                 src_port, dst_port),
283                              filter_chain) {
284                 if (filter->src_ip == src_ip &&
285                     filter->dst_ip == dst_ip &&
286                     filter->ip_proto == ip_proto &&
287                     filter->src_port == src_port &&
288                     filter->dst_port == dst_port) {
289                         ret = filter;
290                         break;
291                 }
292         }
293
294         return ret;
295 }
296
297 static int
298 mlx4_en_filter_rfs(struct net_device *net_dev, const struct sk_buff *skb,
299                    u16 rxq_index, u32 flow_id)
300 {
301         struct mlx4_en_priv *priv = netdev_priv(net_dev);
302         struct mlx4_en_filter *filter;
303         const struct iphdr *ip;
304         const __be16 *ports;
305         u8 ip_proto;
306         __be32 src_ip;
307         __be32 dst_ip;
308         __be16 src_port;
309         __be16 dst_port;
310         int nhoff = skb_network_offset(skb);
311         int ret = 0;
312
313         if (skb->protocol != htons(ETH_P_IP))
314                 return -EPROTONOSUPPORT;
315
316         ip = (const struct iphdr *)(skb->data + nhoff);
317         if (ip_is_fragment(ip))
318                 return -EPROTONOSUPPORT;
319
320         if ((ip->protocol != IPPROTO_TCP) && (ip->protocol != IPPROTO_UDP))
321                 return -EPROTONOSUPPORT;
322         ports = (const __be16 *)(skb->data + nhoff + 4 * ip->ihl);
323
324         ip_proto = ip->protocol;
325         src_ip = ip->saddr;
326         dst_ip = ip->daddr;
327         src_port = ports[0];
328         dst_port = ports[1];
329
330         spin_lock_bh(&priv->filters_lock);
331         filter = mlx4_en_filter_find(priv, src_ip, dst_ip, ip_proto,
332                                      src_port, dst_port);
333         if (filter) {
334                 if (filter->rxq_index == rxq_index)
335                         goto out;
336
337                 filter->rxq_index = rxq_index;
338         } else {
339                 filter = mlx4_en_filter_alloc(priv, rxq_index,
340                                               src_ip, dst_ip, ip_proto,
341                                               src_port, dst_port, flow_id);
342                 if (!filter) {
343                         ret = -ENOMEM;
344                         goto err;
345                 }
346         }
347
348         queue_work(priv->mdev->workqueue, &filter->work);
349
350 out:
351         ret = filter->id;
352 err:
353         spin_unlock_bh(&priv->filters_lock);
354
355         return ret;
356 }
357
358 void mlx4_en_cleanup_filters(struct mlx4_en_priv *priv)
359 {
360         struct mlx4_en_filter *filter, *tmp;
361         LIST_HEAD(del_list);
362
363         spin_lock_bh(&priv->filters_lock);
364         list_for_each_entry_safe(filter, tmp, &priv->filters, next) {
365                 list_move(&filter->next, &del_list);
366                 hlist_del(&filter->filter_chain);
367         }
368         spin_unlock_bh(&priv->filters_lock);
369
370         list_for_each_entry_safe(filter, tmp, &del_list, next) {
371                 cancel_work_sync(&filter->work);
372                 mlx4_en_filter_free(filter);
373         }
374 }
375
376 static void mlx4_en_filter_rfs_expire(struct mlx4_en_priv *priv)
377 {
378         struct mlx4_en_filter *filter = NULL, *tmp, *last_filter = NULL;
379         LIST_HEAD(del_list);
380         int i = 0;
381
382         spin_lock_bh(&priv->filters_lock);
383         list_for_each_entry_safe(filter, tmp, &priv->filters, next) {
384                 if (i > MLX4_EN_FILTER_EXPIRY_QUOTA)
385                         break;
386
387                 if (filter->activated &&
388                     !work_pending(&filter->work) &&
389                     rps_may_expire_flow(priv->dev,
390                                         filter->rxq_index, filter->flow_id,
391                                         filter->id)) {
392                         list_move(&filter->next, &del_list);
393                         hlist_del(&filter->filter_chain);
394                 } else
395                         last_filter = filter;
396
397                 i++;
398         }
399
400         if (last_filter && (&last_filter->next != priv->filters.next))
401                 list_move(&priv->filters, &last_filter->next);
402
403         spin_unlock_bh(&priv->filters_lock);
404
405         list_for_each_entry_safe(filter, tmp, &del_list, next)
406                 mlx4_en_filter_free(filter);
407 }
408 #endif
409
410 static int mlx4_en_vlan_rx_add_vid(struct net_device *dev,
411                                    __be16 proto, u16 vid)
412 {
413         struct mlx4_en_priv *priv = netdev_priv(dev);
414         struct mlx4_en_dev *mdev = priv->mdev;
415         int err;
416         int idx;
417
418         en_dbg(HW, priv, "adding VLAN:%d\n", vid);
419
420         set_bit(vid, priv->active_vlans);
421
422         /* Add VID to port VLAN filter */
423         mutex_lock(&mdev->state_lock);
424         if (mdev->device_up && priv->port_up) {
425                 err = mlx4_SET_VLAN_FLTR(mdev->dev, priv);
426                 if (err)
427                         en_err(priv, "Failed configuring VLAN filter\n");
428         }
429         if (mlx4_register_vlan(mdev->dev, priv->port, vid, &idx))
430                 en_dbg(HW, priv, "failed adding vlan %d\n", vid);
431         mutex_unlock(&mdev->state_lock);
432
433         return 0;
434 }
435
436 static int mlx4_en_vlan_rx_kill_vid(struct net_device *dev,
437                                     __be16 proto, u16 vid)
438 {
439         struct mlx4_en_priv *priv = netdev_priv(dev);
440         struct mlx4_en_dev *mdev = priv->mdev;
441         int err;
442
443         en_dbg(HW, priv, "Killing VID:%d\n", vid);
444
445         clear_bit(vid, priv->active_vlans);
446
447         /* Remove VID from port VLAN filter */
448         mutex_lock(&mdev->state_lock);
449         mlx4_unregister_vlan(mdev->dev, priv->port, vid);
450
451         if (mdev->device_up && priv->port_up) {
452                 err = mlx4_SET_VLAN_FLTR(mdev->dev, priv);
453                 if (err)
454                         en_err(priv, "Failed configuring VLAN filter\n");
455         }
456         mutex_unlock(&mdev->state_lock);
457
458         return 0;
459 }
460
461 static void mlx4_en_u64_to_mac(unsigned char dst_mac[ETH_ALEN + 2], u64 src_mac)
462 {
463         int i;
464         for (i = ETH_ALEN - 1; i >= 0; --i) {
465                 dst_mac[i] = src_mac & 0xff;
466                 src_mac >>= 8;
467         }
468         memset(&dst_mac[ETH_ALEN], 0, 2);
469 }
470
471
472 static int mlx4_en_tunnel_steer_add(struct mlx4_en_priv *priv, unsigned char *addr,
473                                     int qpn, u64 *reg_id)
474 {
475         int err;
476         struct mlx4_spec_list spec_eth_outer = { {NULL} };
477         struct mlx4_spec_list spec_vxlan     = { {NULL} };
478         struct mlx4_spec_list spec_eth_inner = { {NULL} };
479
480         struct mlx4_net_trans_rule rule = {
481                 .queue_mode = MLX4_NET_TRANS_Q_FIFO,
482                 .exclusive = 0,
483                 .allow_loopback = 1,
484                 .promisc_mode = MLX4_FS_REGULAR,
485                 .priority = MLX4_DOMAIN_NIC,
486         };
487
488         __be64 mac_mask = cpu_to_be64(MLX4_MAC_MASK << 16);
489
490         if (priv->mdev->dev->caps.tunnel_offload_mode != MLX4_TUNNEL_OFFLOAD_MODE_VXLAN)
491                 return 0; /* do nothing */
492
493         rule.port = priv->port;
494         rule.qpn = qpn;
495         INIT_LIST_HEAD(&rule.list);
496
497         spec_eth_outer.id = MLX4_NET_TRANS_RULE_ID_ETH;
498         memcpy(spec_eth_outer.eth.dst_mac, addr, ETH_ALEN);
499         memcpy(spec_eth_outer.eth.dst_mac_msk, &mac_mask, ETH_ALEN);
500
501         spec_vxlan.id = MLX4_NET_TRANS_RULE_ID_VXLAN;    /* any vxlan header */
502         spec_eth_inner.id = MLX4_NET_TRANS_RULE_ID_ETH;  /* any inner eth header */
503
504         list_add_tail(&spec_eth_outer.list, &rule.list);
505         list_add_tail(&spec_vxlan.list,     &rule.list);
506         list_add_tail(&spec_eth_inner.list, &rule.list);
507
508         err = mlx4_flow_attach(priv->mdev->dev, &rule, reg_id);
509         if (err) {
510                 en_err(priv, "failed to add vxlan steering rule, err %d\n", err);
511                 return err;
512         }
513         en_dbg(DRV, priv, "added vxlan steering rule, mac %pM reg_id %llx\n", addr, *reg_id);
514         return 0;
515 }
516
517
518 static int mlx4_en_uc_steer_add(struct mlx4_en_priv *priv,
519                                 unsigned char *mac, int *qpn, u64 *reg_id)
520 {
521         struct mlx4_en_dev *mdev = priv->mdev;
522         struct mlx4_dev *dev = mdev->dev;
523         int err;
524
525         switch (dev->caps.steering_mode) {
526         case MLX4_STEERING_MODE_B0: {
527                 struct mlx4_qp qp;
528                 u8 gid[16] = {0};
529
530                 qp.qpn = *qpn;
531                 memcpy(&gid[10], mac, ETH_ALEN);
532                 gid[5] = priv->port;
533
534                 err = mlx4_unicast_attach(dev, &qp, gid, 0, MLX4_PROT_ETH);
535                 break;
536         }
537         case MLX4_STEERING_MODE_DEVICE_MANAGED: {
538                 struct mlx4_spec_list spec_eth = { {NULL} };
539                 __be64 mac_mask = cpu_to_be64(MLX4_MAC_MASK << 16);
540
541                 struct mlx4_net_trans_rule rule = {
542                         .queue_mode = MLX4_NET_TRANS_Q_FIFO,
543                         .exclusive = 0,
544                         .allow_loopback = 1,
545                         .promisc_mode = MLX4_FS_REGULAR,
546                         .priority = MLX4_DOMAIN_NIC,
547                 };
548
549                 rule.port = priv->port;
550                 rule.qpn = *qpn;
551                 INIT_LIST_HEAD(&rule.list);
552
553                 spec_eth.id = MLX4_NET_TRANS_RULE_ID_ETH;
554                 memcpy(spec_eth.eth.dst_mac, mac, ETH_ALEN);
555                 memcpy(spec_eth.eth.dst_mac_msk, &mac_mask, ETH_ALEN);
556                 list_add_tail(&spec_eth.list, &rule.list);
557
558                 err = mlx4_flow_attach(dev, &rule, reg_id);
559                 break;
560         }
561         default:
562                 return -EINVAL;
563         }
564         if (err)
565                 en_warn(priv, "Failed Attaching Unicast\n");
566
567         return err;
568 }
569
570 static void mlx4_en_uc_steer_release(struct mlx4_en_priv *priv,
571                                      unsigned char *mac, int qpn, u64 reg_id)
572 {
573         struct mlx4_en_dev *mdev = priv->mdev;
574         struct mlx4_dev *dev = mdev->dev;
575
576         switch (dev->caps.steering_mode) {
577         case MLX4_STEERING_MODE_B0: {
578                 struct mlx4_qp qp;
579                 u8 gid[16] = {0};
580
581                 qp.qpn = qpn;
582                 memcpy(&gid[10], mac, ETH_ALEN);
583                 gid[5] = priv->port;
584
585                 mlx4_unicast_detach(dev, &qp, gid, MLX4_PROT_ETH);
586                 break;
587         }
588         case MLX4_STEERING_MODE_DEVICE_MANAGED: {
589                 mlx4_flow_detach(dev, reg_id);
590                 break;
591         }
592         default:
593                 en_err(priv, "Invalid steering mode.\n");
594         }
595 }
596
597 static int mlx4_en_get_qp(struct mlx4_en_priv *priv)
598 {
599         struct mlx4_en_dev *mdev = priv->mdev;
600         struct mlx4_dev *dev = mdev->dev;
601         struct mlx4_mac_entry *entry;
602         int index = 0;
603         int err = 0;
604         u64 reg_id;
605         int *qpn = &priv->base_qpn;
606         u64 mac = mlx4_en_mac_to_u64(priv->dev->dev_addr);
607
608         en_dbg(DRV, priv, "Registering MAC: %pM for adding\n",
609                priv->dev->dev_addr);
610         index = mlx4_register_mac(dev, priv->port, mac);
611         if (index < 0) {
612                 err = index;
613                 en_err(priv, "Failed adding MAC: %pM\n",
614                        priv->dev->dev_addr);
615                 return err;
616         }
617
618         if (dev->caps.steering_mode == MLX4_STEERING_MODE_A0) {
619                 int base_qpn = mlx4_get_base_qpn(dev, priv->port);
620                 *qpn = base_qpn + index;
621                 return 0;
622         }
623
624         err = mlx4_qp_reserve_range(dev, 1, 1, qpn);
625         en_dbg(DRV, priv, "Reserved qp %d\n", *qpn);
626         if (err) {
627                 en_err(priv, "Failed to reserve qp for mac registration\n");
628                 goto qp_err;
629         }
630
631         err = mlx4_en_uc_steer_add(priv, priv->dev->dev_addr, qpn, &reg_id);
632         if (err)
633                 goto steer_err;
634
635         err = mlx4_en_tunnel_steer_add(priv, priv->dev->dev_addr, *qpn,
636                                        &priv->tunnel_reg_id);
637         if (err)
638                 goto tunnel_err;
639
640         entry = kmalloc(sizeof(*entry), GFP_KERNEL);
641         if (!entry) {
642                 err = -ENOMEM;
643                 goto alloc_err;
644         }
645         memcpy(entry->mac, priv->dev->dev_addr, sizeof(entry->mac));
646         entry->reg_id = reg_id;
647
648         hlist_add_head_rcu(&entry->hlist,
649                            &priv->mac_hash[entry->mac[MLX4_EN_MAC_HASH_IDX]]);
650
651         return 0;
652
653 alloc_err:
654         if (priv->tunnel_reg_id)
655                 mlx4_flow_detach(priv->mdev->dev, priv->tunnel_reg_id);
656 tunnel_err:
657         mlx4_en_uc_steer_release(priv, priv->dev->dev_addr, *qpn, reg_id);
658
659 steer_err:
660         mlx4_qp_release_range(dev, *qpn, 1);
661
662 qp_err:
663         mlx4_unregister_mac(dev, priv->port, mac);
664         return err;
665 }
666
667 static void mlx4_en_put_qp(struct mlx4_en_priv *priv)
668 {
669         struct mlx4_en_dev *mdev = priv->mdev;
670         struct mlx4_dev *dev = mdev->dev;
671         int qpn = priv->base_qpn;
672         u64 mac;
673
674         if (dev->caps.steering_mode == MLX4_STEERING_MODE_A0) {
675                 mac = mlx4_en_mac_to_u64(priv->dev->dev_addr);
676                 en_dbg(DRV, priv, "Registering MAC: %pM for deleting\n",
677                        priv->dev->dev_addr);
678                 mlx4_unregister_mac(dev, priv->port, mac);
679         } else {
680                 struct mlx4_mac_entry *entry;
681                 struct hlist_node *tmp;
682                 struct hlist_head *bucket;
683                 unsigned int i;
684
685                 for (i = 0; i < MLX4_EN_MAC_HASH_SIZE; ++i) {
686                         bucket = &priv->mac_hash[i];
687                         hlist_for_each_entry_safe(entry, tmp, bucket, hlist) {
688                                 mac = mlx4_en_mac_to_u64(entry->mac);
689                                 en_dbg(DRV, priv, "Registering MAC: %pM for deleting\n",
690                                        entry->mac);
691                                 mlx4_en_uc_steer_release(priv, entry->mac,
692                                                          qpn, entry->reg_id);
693
694                                 mlx4_unregister_mac(dev, priv->port, mac);
695                                 hlist_del_rcu(&entry->hlist);
696                                 kfree_rcu(entry, rcu);
697                         }
698                 }
699
700                 if (priv->tunnel_reg_id) {
701                         mlx4_flow_detach(priv->mdev->dev, priv->tunnel_reg_id);
702                         priv->tunnel_reg_id = 0;
703                 }
704
705                 en_dbg(DRV, priv, "Releasing qp: port %d, qpn %d\n",
706                        priv->port, qpn);
707                 mlx4_qp_release_range(dev, qpn, 1);
708                 priv->flags &= ~MLX4_EN_FLAG_FORCE_PROMISC;
709         }
710 }
711
712 static int mlx4_en_replace_mac(struct mlx4_en_priv *priv, int qpn,
713                                unsigned char *new_mac, unsigned char *prev_mac)
714 {
715         struct mlx4_en_dev *mdev = priv->mdev;
716         struct mlx4_dev *dev = mdev->dev;
717         int err = 0;
718         u64 new_mac_u64 = mlx4_en_mac_to_u64(new_mac);
719
720         if (dev->caps.steering_mode != MLX4_STEERING_MODE_A0) {
721                 struct hlist_head *bucket;
722                 unsigned int mac_hash;
723                 struct mlx4_mac_entry *entry;
724                 struct hlist_node *tmp;
725                 u64 prev_mac_u64 = mlx4_en_mac_to_u64(prev_mac);
726
727                 bucket = &priv->mac_hash[prev_mac[MLX4_EN_MAC_HASH_IDX]];
728                 hlist_for_each_entry_safe(entry, tmp, bucket, hlist) {
729                         if (ether_addr_equal_64bits(entry->mac, prev_mac)) {
730                                 mlx4_en_uc_steer_release(priv, entry->mac,
731                                                          qpn, entry->reg_id);
732                                 mlx4_unregister_mac(dev, priv->port,
733                                                     prev_mac_u64);
734                                 hlist_del_rcu(&entry->hlist);
735                                 synchronize_rcu();
736                                 memcpy(entry->mac, new_mac, ETH_ALEN);
737                                 entry->reg_id = 0;
738                                 mac_hash = new_mac[MLX4_EN_MAC_HASH_IDX];
739                                 hlist_add_head_rcu(&entry->hlist,
740                                                    &priv->mac_hash[mac_hash]);
741                                 mlx4_register_mac(dev, priv->port, new_mac_u64);
742                                 err = mlx4_en_uc_steer_add(priv, new_mac,
743                                                            &qpn,
744                                                            &entry->reg_id);
745                                 if (err)
746                                         return err;
747                                 if (priv->tunnel_reg_id) {
748                                         mlx4_flow_detach(priv->mdev->dev, priv->tunnel_reg_id);
749                                         priv->tunnel_reg_id = 0;
750                                 }
751                                 err = mlx4_en_tunnel_steer_add(priv, new_mac, qpn,
752                                                                &priv->tunnel_reg_id);
753                                 return err;
754                         }
755                 }
756                 return -EINVAL;
757         }
758
759         return __mlx4_replace_mac(dev, priv->port, qpn, new_mac_u64);
760 }
761
762 u64 mlx4_en_mac_to_u64(u8 *addr)
763 {
764         u64 mac = 0;
765         int i;
766
767         for (i = 0; i < ETH_ALEN; i++) {
768                 mac <<= 8;
769                 mac |= addr[i];
770         }
771         return mac;
772 }
773
774 static int mlx4_en_do_set_mac(struct mlx4_en_priv *priv)
775 {
776         int err = 0;
777
778         if (priv->port_up) {
779                 /* Remove old MAC and insert the new one */
780                 err = mlx4_en_replace_mac(priv, priv->base_qpn,
781                                           priv->dev->dev_addr, priv->prev_mac);
782                 if (err)
783                         en_err(priv, "Failed changing HW MAC address\n");
784                 memcpy(priv->prev_mac, priv->dev->dev_addr,
785                        sizeof(priv->prev_mac));
786         } else
787                 en_dbg(HW, priv, "Port is down while registering mac, exiting...\n");
788
789         return err;
790 }
791
792 static int mlx4_en_set_mac(struct net_device *dev, void *addr)
793 {
794         struct mlx4_en_priv *priv = netdev_priv(dev);
795         struct mlx4_en_dev *mdev = priv->mdev;
796         struct sockaddr *saddr = addr;
797         int err;
798
799         if (!is_valid_ether_addr(saddr->sa_data))
800                 return -EADDRNOTAVAIL;
801
802         memcpy(dev->dev_addr, saddr->sa_data, ETH_ALEN);
803
804         mutex_lock(&mdev->state_lock);
805         err = mlx4_en_do_set_mac(priv);
806         mutex_unlock(&mdev->state_lock);
807
808         return err;
809 }
810
811 static void mlx4_en_clear_list(struct net_device *dev)
812 {
813         struct mlx4_en_priv *priv = netdev_priv(dev);
814         struct mlx4_en_mc_list *tmp, *mc_to_del;
815
816         list_for_each_entry_safe(mc_to_del, tmp, &priv->mc_list, list) {
817                 list_del(&mc_to_del->list);
818                 kfree(mc_to_del);
819         }
820 }
821
822 static void mlx4_en_cache_mclist(struct net_device *dev)
823 {
824         struct mlx4_en_priv *priv = netdev_priv(dev);
825         struct netdev_hw_addr *ha;
826         struct mlx4_en_mc_list *tmp;
827
828         mlx4_en_clear_list(dev);
829         netdev_for_each_mc_addr(ha, dev) {
830                 tmp = kzalloc(sizeof(struct mlx4_en_mc_list), GFP_ATOMIC);
831                 if (!tmp) {
832                         mlx4_en_clear_list(dev);
833                         return;
834                 }
835                 memcpy(tmp->addr, ha->addr, ETH_ALEN);
836                 list_add_tail(&tmp->list, &priv->mc_list);
837         }
838 }
839
840 static void update_mclist_flags(struct mlx4_en_priv *priv,
841                                 struct list_head *dst,
842                                 struct list_head *src)
843 {
844         struct mlx4_en_mc_list *dst_tmp, *src_tmp, *new_mc;
845         bool found;
846
847         /* Find all the entries that should be removed from dst,
848          * These are the entries that are not found in src
849          */
850         list_for_each_entry(dst_tmp, dst, list) {
851                 found = false;
852                 list_for_each_entry(src_tmp, src, list) {
853                         if (ether_addr_equal(dst_tmp->addr, src_tmp->addr)) {
854                                 found = true;
855                                 break;
856                         }
857                 }
858                 if (!found)
859                         dst_tmp->action = MCLIST_REM;
860         }
861
862         /* Add entries that exist in src but not in dst
863          * mark them as need to add
864          */
865         list_for_each_entry(src_tmp, src, list) {
866                 found = false;
867                 list_for_each_entry(dst_tmp, dst, list) {
868                         if (ether_addr_equal(dst_tmp->addr, src_tmp->addr)) {
869                                 dst_tmp->action = MCLIST_NONE;
870                                 found = true;
871                                 break;
872                         }
873                 }
874                 if (!found) {
875                         new_mc = kmemdup(src_tmp,
876                                          sizeof(struct mlx4_en_mc_list),
877                                          GFP_KERNEL);
878                         if (!new_mc)
879                                 return;
880
881                         new_mc->action = MCLIST_ADD;
882                         list_add_tail(&new_mc->list, dst);
883                 }
884         }
885 }
886
887 static void mlx4_en_set_rx_mode(struct net_device *dev)
888 {
889         struct mlx4_en_priv *priv = netdev_priv(dev);
890
891         if (!priv->port_up)
892                 return;
893
894         queue_work(priv->mdev->workqueue, &priv->rx_mode_task);
895 }
896
897 static void mlx4_en_set_promisc_mode(struct mlx4_en_priv *priv,
898                                      struct mlx4_en_dev *mdev)
899 {
900         int err = 0;
901
902         if (!(priv->flags & MLX4_EN_FLAG_PROMISC)) {
903                 if (netif_msg_rx_status(priv))
904                         en_warn(priv, "Entering promiscuous mode\n");
905                 priv->flags |= MLX4_EN_FLAG_PROMISC;
906
907                 /* Enable promiscouos mode */
908                 switch (mdev->dev->caps.steering_mode) {
909                 case MLX4_STEERING_MODE_DEVICE_MANAGED:
910                         err = mlx4_flow_steer_promisc_add(mdev->dev,
911                                                           priv->port,
912                                                           priv->base_qpn,
913                                                           MLX4_FS_ALL_DEFAULT);
914                         if (err)
915                                 en_err(priv, "Failed enabling promiscuous mode\n");
916                         priv->flags |= MLX4_EN_FLAG_MC_PROMISC;
917                         break;
918
919                 case MLX4_STEERING_MODE_B0:
920                         err = mlx4_unicast_promisc_add(mdev->dev,
921                                                        priv->base_qpn,
922                                                        priv->port);
923                         if (err)
924                                 en_err(priv, "Failed enabling unicast promiscuous mode\n");
925
926                         /* Add the default qp number as multicast
927                          * promisc
928                          */
929                         if (!(priv->flags & MLX4_EN_FLAG_MC_PROMISC)) {
930                                 err = mlx4_multicast_promisc_add(mdev->dev,
931                                                                  priv->base_qpn,
932                                                                  priv->port);
933                                 if (err)
934                                         en_err(priv, "Failed enabling multicast promiscuous mode\n");
935                                 priv->flags |= MLX4_EN_FLAG_MC_PROMISC;
936                         }
937                         break;
938
939                 case MLX4_STEERING_MODE_A0:
940                         err = mlx4_SET_PORT_qpn_calc(mdev->dev,
941                                                      priv->port,
942                                                      priv->base_qpn,
943                                                      1);
944                         if (err)
945                                 en_err(priv, "Failed enabling promiscuous mode\n");
946                         break;
947                 }
948
949                 /* Disable port multicast filter (unconditionally) */
950                 err = mlx4_SET_MCAST_FLTR(mdev->dev, priv->port, 0,
951                                           0, MLX4_MCAST_DISABLE);
952                 if (err)
953                         en_err(priv, "Failed disabling multicast filter\n");
954
955                 /* Disable port VLAN filter */
956                 err = mlx4_SET_VLAN_FLTR(mdev->dev, priv);
957                 if (err)
958                         en_err(priv, "Failed disabling VLAN filter\n");
959         }
960 }
961
962 static void mlx4_en_clear_promisc_mode(struct mlx4_en_priv *priv,
963                                        struct mlx4_en_dev *mdev)
964 {
965         int err = 0;
966
967         if (netif_msg_rx_status(priv))
968                 en_warn(priv, "Leaving promiscuous mode\n");
969         priv->flags &= ~MLX4_EN_FLAG_PROMISC;
970
971         /* Disable promiscouos mode */
972         switch (mdev->dev->caps.steering_mode) {
973         case MLX4_STEERING_MODE_DEVICE_MANAGED:
974                 err = mlx4_flow_steer_promisc_remove(mdev->dev,
975                                                      priv->port,
976                                                      MLX4_FS_ALL_DEFAULT);
977                 if (err)
978                         en_err(priv, "Failed disabling promiscuous mode\n");
979                 priv->flags &= ~MLX4_EN_FLAG_MC_PROMISC;
980                 break;
981
982         case MLX4_STEERING_MODE_B0:
983                 err = mlx4_unicast_promisc_remove(mdev->dev,
984                                                   priv->base_qpn,
985                                                   priv->port);
986                 if (err)
987                         en_err(priv, "Failed disabling unicast promiscuous mode\n");
988                 /* Disable Multicast promisc */
989                 if (priv->flags & MLX4_EN_FLAG_MC_PROMISC) {
990                         err = mlx4_multicast_promisc_remove(mdev->dev,
991                                                             priv->base_qpn,
992                                                             priv->port);
993                         if (err)
994                                 en_err(priv, "Failed disabling multicast promiscuous mode\n");
995                         priv->flags &= ~MLX4_EN_FLAG_MC_PROMISC;
996                 }
997                 break;
998
999         case MLX4_STEERING_MODE_A0:
1000                 err = mlx4_SET_PORT_qpn_calc(mdev->dev,
1001                                              priv->port,
1002                                              priv->base_qpn, 0);
1003                 if (err)
1004                         en_err(priv, "Failed disabling promiscuous mode\n");
1005                 break;
1006         }
1007
1008         /* Enable port VLAN filter */
1009         err = mlx4_SET_VLAN_FLTR(mdev->dev, priv);
1010         if (err)
1011                 en_err(priv, "Failed enabling VLAN filter\n");
1012 }
1013
1014 static void mlx4_en_do_multicast(struct mlx4_en_priv *priv,
1015                                  struct net_device *dev,
1016                                  struct mlx4_en_dev *mdev)
1017 {
1018         struct mlx4_en_mc_list *mclist, *tmp;
1019         u64 mcast_addr = 0;
1020         u8 mc_list[16] = {0};
1021         int err = 0;
1022
1023         /* Enable/disable the multicast filter according to IFF_ALLMULTI */
1024         if (dev->flags & IFF_ALLMULTI) {
1025                 err = mlx4_SET_MCAST_FLTR(mdev->dev, priv->port, 0,
1026                                           0, MLX4_MCAST_DISABLE);
1027                 if (err)
1028                         en_err(priv, "Failed disabling multicast filter\n");
1029
1030                 /* Add the default qp number as multicast promisc */
1031                 if (!(priv->flags & MLX4_EN_FLAG_MC_PROMISC)) {
1032                         switch (mdev->dev->caps.steering_mode) {
1033                         case MLX4_STEERING_MODE_DEVICE_MANAGED:
1034                                 err = mlx4_flow_steer_promisc_add(mdev->dev,
1035                                                                   priv->port,
1036                                                                   priv->base_qpn,
1037                                                                   MLX4_FS_MC_DEFAULT);
1038                                 break;
1039
1040                         case MLX4_STEERING_MODE_B0:
1041                                 err = mlx4_multicast_promisc_add(mdev->dev,
1042                                                                  priv->base_qpn,
1043                                                                  priv->port);
1044                                 break;
1045
1046                         case MLX4_STEERING_MODE_A0:
1047                                 break;
1048                         }
1049                         if (err)
1050                                 en_err(priv, "Failed entering multicast promisc mode\n");
1051                         priv->flags |= MLX4_EN_FLAG_MC_PROMISC;
1052                 }
1053         } else {
1054                 /* Disable Multicast promisc */
1055                 if (priv->flags & MLX4_EN_FLAG_MC_PROMISC) {
1056                         switch (mdev->dev->caps.steering_mode) {
1057                         case MLX4_STEERING_MODE_DEVICE_MANAGED:
1058                                 err = mlx4_flow_steer_promisc_remove(mdev->dev,
1059                                                                      priv->port,
1060                                                                      MLX4_FS_MC_DEFAULT);
1061                                 break;
1062
1063                         case MLX4_STEERING_MODE_B0:
1064                                 err = mlx4_multicast_promisc_remove(mdev->dev,
1065                                                                     priv->base_qpn,
1066                                                                     priv->port);
1067                                 break;
1068
1069                         case MLX4_STEERING_MODE_A0:
1070                                 break;
1071                         }
1072                         if (err)
1073                                 en_err(priv, "Failed disabling multicast promiscuous mode\n");
1074                         priv->flags &= ~MLX4_EN_FLAG_MC_PROMISC;
1075                 }
1076
1077                 err = mlx4_SET_MCAST_FLTR(mdev->dev, priv->port, 0,
1078                                           0, MLX4_MCAST_DISABLE);
1079                 if (err)
1080                         en_err(priv, "Failed disabling multicast filter\n");
1081
1082                 /* Flush mcast filter and init it with broadcast address */
1083                 mlx4_SET_MCAST_FLTR(mdev->dev, priv->port, ETH_BCAST,
1084                                     1, MLX4_MCAST_CONFIG);
1085
1086                 /* Update multicast list - we cache all addresses so they won't
1087                  * change while HW is updated holding the command semaphor */
1088                 netif_addr_lock_bh(dev);
1089                 mlx4_en_cache_mclist(dev);
1090                 netif_addr_unlock_bh(dev);
1091                 list_for_each_entry(mclist, &priv->mc_list, list) {
1092                         mcast_addr = mlx4_en_mac_to_u64(mclist->addr);
1093                         mlx4_SET_MCAST_FLTR(mdev->dev, priv->port,
1094                                             mcast_addr, 0, MLX4_MCAST_CONFIG);
1095                 }
1096                 err = mlx4_SET_MCAST_FLTR(mdev->dev, priv->port, 0,
1097                                           0, MLX4_MCAST_ENABLE);
1098                 if (err)
1099                         en_err(priv, "Failed enabling multicast filter\n");
1100
1101                 update_mclist_flags(priv, &priv->curr_list, &priv->mc_list);
1102                 list_for_each_entry_safe(mclist, tmp, &priv->curr_list, list) {
1103                         if (mclist->action == MCLIST_REM) {
1104                                 /* detach this address and delete from list */
1105                                 memcpy(&mc_list[10], mclist->addr, ETH_ALEN);
1106                                 mc_list[5] = priv->port;
1107                                 err = mlx4_multicast_detach(mdev->dev,
1108                                                             &priv->rss_map.indir_qp,
1109                                                             mc_list,
1110                                                             MLX4_PROT_ETH,
1111                                                             mclist->reg_id);
1112                                 if (err)
1113                                         en_err(priv, "Fail to detach multicast address\n");
1114
1115                                 if (mclist->tunnel_reg_id) {
1116                                         err = mlx4_flow_detach(priv->mdev->dev, mclist->tunnel_reg_id);
1117                                         if (err)
1118                                                 en_err(priv, "Failed to detach multicast address\n");
1119                                 }
1120
1121                                 /* remove from list */
1122                                 list_del(&mclist->list);
1123                                 kfree(mclist);
1124                         } else if (mclist->action == MCLIST_ADD) {
1125                                 /* attach the address */
1126                                 memcpy(&mc_list[10], mclist->addr, ETH_ALEN);
1127                                 /* needed for B0 steering support */
1128                                 mc_list[5] = priv->port;
1129                                 err = mlx4_multicast_attach(mdev->dev,
1130                                                             &priv->rss_map.indir_qp,
1131                                                             mc_list,
1132                                                             priv->port, 0,
1133                                                             MLX4_PROT_ETH,
1134                                                             &mclist->reg_id);
1135                                 if (err)
1136                                         en_err(priv, "Fail to attach multicast address\n");
1137
1138                                 err = mlx4_en_tunnel_steer_add(priv, &mc_list[10], priv->base_qpn,
1139                                                                &mclist->tunnel_reg_id);
1140                                 if (err)
1141                                         en_err(priv, "Failed to attach multicast address\n");
1142                         }
1143                 }
1144         }
1145 }
1146
1147 static void mlx4_en_do_uc_filter(struct mlx4_en_priv *priv,
1148                                  struct net_device *dev,
1149                                  struct mlx4_en_dev *mdev)
1150 {
1151         struct netdev_hw_addr *ha;
1152         struct mlx4_mac_entry *entry;
1153         struct hlist_node *tmp;
1154         bool found;
1155         u64 mac;
1156         int err = 0;
1157         struct hlist_head *bucket;
1158         unsigned int i;
1159         int removed = 0;
1160         u32 prev_flags;
1161
1162         /* Note that we do not need to protect our mac_hash traversal with rcu,
1163          * since all modification code is protected by mdev->state_lock
1164          */
1165
1166         /* find what to remove */
1167         for (i = 0; i < MLX4_EN_MAC_HASH_SIZE; ++i) {
1168                 bucket = &priv->mac_hash[i];
1169                 hlist_for_each_entry_safe(entry, tmp, bucket, hlist) {
1170                         found = false;
1171                         netdev_for_each_uc_addr(ha, dev) {
1172                                 if (ether_addr_equal_64bits(entry->mac,
1173                                                             ha->addr)) {
1174                                         found = true;
1175                                         break;
1176                                 }
1177                         }
1178
1179                         /* MAC address of the port is not in uc list */
1180                         if (ether_addr_equal_64bits(entry->mac, dev->dev_addr))
1181                                 found = true;
1182
1183                         if (!found) {
1184                                 mac = mlx4_en_mac_to_u64(entry->mac);
1185                                 mlx4_en_uc_steer_release(priv, entry->mac,
1186                                                          priv->base_qpn,
1187                                                          entry->reg_id);
1188                                 mlx4_unregister_mac(mdev->dev, priv->port, mac);
1189
1190                                 hlist_del_rcu(&entry->hlist);
1191                                 kfree_rcu(entry, rcu);
1192                                 en_dbg(DRV, priv, "Removed MAC %pM on port:%d\n",
1193                                        entry->mac, priv->port);
1194                                 ++removed;
1195                         }
1196                 }
1197         }
1198
1199         /* if we didn't remove anything, there is no use in trying to add
1200          * again once we are in a forced promisc mode state
1201          */
1202         if ((priv->flags & MLX4_EN_FLAG_FORCE_PROMISC) && 0 == removed)
1203                 return;
1204
1205         prev_flags = priv->flags;
1206         priv->flags &= ~MLX4_EN_FLAG_FORCE_PROMISC;
1207
1208         /* find what to add */
1209         netdev_for_each_uc_addr(ha, dev) {
1210                 found = false;
1211                 bucket = &priv->mac_hash[ha->addr[MLX4_EN_MAC_HASH_IDX]];
1212                 hlist_for_each_entry(entry, bucket, hlist) {
1213                         if (ether_addr_equal_64bits(entry->mac, ha->addr)) {
1214                                 found = true;
1215                                 break;
1216                         }
1217                 }
1218
1219                 if (!found) {
1220                         entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1221                         if (!entry) {
1222                                 en_err(priv, "Failed adding MAC %pM on port:%d (out of memory)\n",
1223                                        ha->addr, priv->port);
1224                                 priv->flags |= MLX4_EN_FLAG_FORCE_PROMISC;
1225                                 break;
1226                         }
1227                         mac = mlx4_en_mac_to_u64(ha->addr);
1228                         memcpy(entry->mac, ha->addr, ETH_ALEN);
1229                         err = mlx4_register_mac(mdev->dev, priv->port, mac);
1230                         if (err < 0) {
1231                                 en_err(priv, "Failed registering MAC %pM on port %d: %d\n",
1232                                        ha->addr, priv->port, err);
1233                                 kfree(entry);
1234                                 priv->flags |= MLX4_EN_FLAG_FORCE_PROMISC;
1235                                 break;
1236                         }
1237                         err = mlx4_en_uc_steer_add(priv, ha->addr,
1238                                                    &priv->base_qpn,
1239                                                    &entry->reg_id);
1240                         if (err) {
1241                                 en_err(priv, "Failed adding MAC %pM on port %d: %d\n",
1242                                        ha->addr, priv->port, err);
1243                                 mlx4_unregister_mac(mdev->dev, priv->port, mac);
1244                                 kfree(entry);
1245                                 priv->flags |= MLX4_EN_FLAG_FORCE_PROMISC;
1246                                 break;
1247                         } else {
1248                                 unsigned int mac_hash;
1249                                 en_dbg(DRV, priv, "Added MAC %pM on port:%d\n",
1250                                        ha->addr, priv->port);
1251                                 mac_hash = ha->addr[MLX4_EN_MAC_HASH_IDX];
1252                                 bucket = &priv->mac_hash[mac_hash];
1253                                 hlist_add_head_rcu(&entry->hlist, bucket);
1254                         }
1255                 }
1256         }
1257
1258         if (priv->flags & MLX4_EN_FLAG_FORCE_PROMISC) {
1259                 en_warn(priv, "Forcing promiscuous mode on port:%d\n",
1260                         priv->port);
1261         } else if (prev_flags & MLX4_EN_FLAG_FORCE_PROMISC) {
1262                 en_warn(priv, "Stop forcing promiscuous mode on port:%d\n",
1263                         priv->port);
1264         }
1265 }
1266
1267 static void mlx4_en_do_set_rx_mode(struct work_struct *work)
1268 {
1269         struct mlx4_en_priv *priv = container_of(work, struct mlx4_en_priv,
1270                                                  rx_mode_task);
1271         struct mlx4_en_dev *mdev = priv->mdev;
1272         struct net_device *dev = priv->dev;
1273
1274         mutex_lock(&mdev->state_lock);
1275         if (!mdev->device_up) {
1276                 en_dbg(HW, priv, "Card is not up, ignoring rx mode change.\n");
1277                 goto out;
1278         }
1279         if (!priv->port_up) {
1280                 en_dbg(HW, priv, "Port is down, ignoring rx mode change.\n");
1281                 goto out;
1282         }
1283
1284         if (!netif_carrier_ok(dev)) {
1285                 if (!mlx4_en_QUERY_PORT(mdev, priv->port)) {
1286                         if (priv->port_state.link_state) {
1287                                 priv->last_link_state = MLX4_DEV_EVENT_PORT_UP;
1288                                 netif_carrier_on(dev);
1289                                 en_dbg(LINK, priv, "Link Up\n");
1290                         }
1291                 }
1292         }
1293
1294         if (dev->priv_flags & IFF_UNICAST_FLT)
1295                 mlx4_en_do_uc_filter(priv, dev, mdev);
1296
1297         /* Promsicuous mode: disable all filters */
1298         if ((dev->flags & IFF_PROMISC) ||
1299             (priv->flags & MLX4_EN_FLAG_FORCE_PROMISC)) {
1300                 mlx4_en_set_promisc_mode(priv, mdev);
1301                 goto out;
1302         }
1303
1304         /* Not in promiscuous mode */
1305         if (priv->flags & MLX4_EN_FLAG_PROMISC)
1306                 mlx4_en_clear_promisc_mode(priv, mdev);
1307
1308         mlx4_en_do_multicast(priv, dev, mdev);
1309 out:
1310         mutex_unlock(&mdev->state_lock);
1311 }
1312
1313 #ifdef CONFIG_NET_POLL_CONTROLLER
1314 static void mlx4_en_netpoll(struct net_device *dev)
1315 {
1316         struct mlx4_en_priv *priv = netdev_priv(dev);
1317         struct mlx4_en_cq *cq;
1318         unsigned long flags;
1319         int i;
1320
1321         for (i = 0; i < priv->rx_ring_num; i++) {
1322                 cq = priv->rx_cq[i];
1323                 spin_lock_irqsave(&cq->lock, flags);
1324                 napi_synchronize(&cq->napi);
1325                 mlx4_en_process_rx_cq(dev, cq, 0);
1326                 spin_unlock_irqrestore(&cq->lock, flags);
1327         }
1328 }
1329 #endif
1330
1331 static void mlx4_en_tx_timeout(struct net_device *dev)
1332 {
1333         struct mlx4_en_priv *priv = netdev_priv(dev);
1334         struct mlx4_en_dev *mdev = priv->mdev;
1335         int i;
1336
1337         if (netif_msg_timer(priv))
1338                 en_warn(priv, "Tx timeout called on port:%d\n", priv->port);
1339
1340         for (i = 0; i < priv->tx_ring_num; i++) {
1341                 if (!netif_tx_queue_stopped(netdev_get_tx_queue(dev, i)))
1342                         continue;
1343                 en_warn(priv, "TX timeout on queue: %d, QP: 0x%x, CQ: 0x%x, Cons: 0x%x, Prod: 0x%x\n",
1344                         i, priv->tx_ring[i]->qpn, priv->tx_ring[i]->cqn,
1345                         priv->tx_ring[i]->cons, priv->tx_ring[i]->prod);
1346         }
1347
1348         priv->port_stats.tx_timeout++;
1349         en_dbg(DRV, priv, "Scheduling watchdog\n");
1350         queue_work(mdev->workqueue, &priv->watchdog_task);
1351 }
1352
1353
1354 static struct net_device_stats *mlx4_en_get_stats(struct net_device *dev)
1355 {
1356         struct mlx4_en_priv *priv = netdev_priv(dev);
1357
1358         spin_lock_bh(&priv->stats_lock);
1359         memcpy(&priv->ret_stats, &priv->stats, sizeof(priv->stats));
1360         spin_unlock_bh(&priv->stats_lock);
1361
1362         return &priv->ret_stats;
1363 }
1364
1365 static void mlx4_en_set_default_moderation(struct mlx4_en_priv *priv)
1366 {
1367         struct mlx4_en_cq *cq;
1368         int i;
1369
1370         /* If we haven't received a specific coalescing setting
1371          * (module param), we set the moderation parameters as follows:
1372          * - moder_cnt is set to the number of mtu sized packets to
1373          *   satisfy our coalescing target.
1374          * - moder_time is set to a fixed value.
1375          */
1376         priv->rx_frames = MLX4_EN_RX_COAL_TARGET;
1377         priv->rx_usecs = MLX4_EN_RX_COAL_TIME;
1378         priv->tx_frames = MLX4_EN_TX_COAL_PKTS;
1379         priv->tx_usecs = MLX4_EN_TX_COAL_TIME;
1380         en_dbg(INTR, priv, "Default coalesing params for mtu:%d - rx_frames:%d rx_usecs:%d\n",
1381                priv->dev->mtu, priv->rx_frames, priv->rx_usecs);
1382
1383         /* Setup cq moderation params */
1384         for (i = 0; i < priv->rx_ring_num; i++) {
1385                 cq = priv->rx_cq[i];
1386                 cq->moder_cnt = priv->rx_frames;
1387                 cq->moder_time = priv->rx_usecs;
1388                 priv->last_moder_time[i] = MLX4_EN_AUTO_CONF;
1389                 priv->last_moder_packets[i] = 0;
1390                 priv->last_moder_bytes[i] = 0;
1391         }
1392
1393         for (i = 0; i < priv->tx_ring_num; i++) {
1394                 cq = priv->tx_cq[i];
1395                 cq->moder_cnt = priv->tx_frames;
1396                 cq->moder_time = priv->tx_usecs;
1397         }
1398
1399         /* Reset auto-moderation params */
1400         priv->pkt_rate_low = MLX4_EN_RX_RATE_LOW;
1401         priv->rx_usecs_low = MLX4_EN_RX_COAL_TIME_LOW;
1402         priv->pkt_rate_high = MLX4_EN_RX_RATE_HIGH;
1403         priv->rx_usecs_high = MLX4_EN_RX_COAL_TIME_HIGH;
1404         priv->sample_interval = MLX4_EN_SAMPLE_INTERVAL;
1405         priv->adaptive_rx_coal = 1;
1406         priv->last_moder_jiffies = 0;
1407         priv->last_moder_tx_packets = 0;
1408 }
1409
1410 static void mlx4_en_auto_moderation(struct mlx4_en_priv *priv)
1411 {
1412         unsigned long period = (unsigned long) (jiffies - priv->last_moder_jiffies);
1413         struct mlx4_en_cq *cq;
1414         unsigned long packets;
1415         unsigned long rate;
1416         unsigned long avg_pkt_size;
1417         unsigned long rx_packets;
1418         unsigned long rx_bytes;
1419         unsigned long rx_pkt_diff;
1420         int moder_time;
1421         int ring, err;
1422
1423         if (!priv->adaptive_rx_coal || period < priv->sample_interval * HZ)
1424                 return;
1425
1426         for (ring = 0; ring < priv->rx_ring_num; ring++) {
1427                 spin_lock_bh(&priv->stats_lock);
1428                 rx_packets = priv->rx_ring[ring]->packets;
1429                 rx_bytes = priv->rx_ring[ring]->bytes;
1430                 spin_unlock_bh(&priv->stats_lock);
1431
1432                 rx_pkt_diff = ((unsigned long) (rx_packets -
1433                                 priv->last_moder_packets[ring]));
1434                 packets = rx_pkt_diff;
1435                 rate = packets * HZ / period;
1436                 avg_pkt_size = packets ? ((unsigned long) (rx_bytes -
1437                                 priv->last_moder_bytes[ring])) / packets : 0;
1438
1439                 /* Apply auto-moderation only when packet rate
1440                  * exceeds a rate that it matters */
1441                 if (rate > (MLX4_EN_RX_RATE_THRESH / priv->rx_ring_num) &&
1442                     avg_pkt_size > MLX4_EN_AVG_PKT_SMALL) {
1443                         if (rate < priv->pkt_rate_low)
1444                                 moder_time = priv->rx_usecs_low;
1445                         else if (rate > priv->pkt_rate_high)
1446                                 moder_time = priv->rx_usecs_high;
1447                         else
1448                                 moder_time = (rate - priv->pkt_rate_low) *
1449                                         (priv->rx_usecs_high - priv->rx_usecs_low) /
1450                                         (priv->pkt_rate_high - priv->pkt_rate_low) +
1451                                         priv->rx_usecs_low;
1452                 } else {
1453                         moder_time = priv->rx_usecs_low;
1454                 }
1455
1456                 if (moder_time != priv->last_moder_time[ring]) {
1457                         priv->last_moder_time[ring] = moder_time;
1458                         cq = priv->rx_cq[ring];
1459                         cq->moder_time = moder_time;
1460                         cq->moder_cnt = priv->rx_frames;
1461                         err = mlx4_en_set_cq_moder(priv, cq);
1462                         if (err)
1463                                 en_err(priv, "Failed modifying moderation for cq:%d\n",
1464                                        ring);
1465                 }
1466                 priv->last_moder_packets[ring] = rx_packets;
1467                 priv->last_moder_bytes[ring] = rx_bytes;
1468         }
1469
1470         priv->last_moder_jiffies = jiffies;
1471 }
1472
1473 static void mlx4_en_do_get_stats(struct work_struct *work)
1474 {
1475         struct delayed_work *delay = to_delayed_work(work);
1476         struct mlx4_en_priv *priv = container_of(delay, struct mlx4_en_priv,
1477                                                  stats_task);
1478         struct mlx4_en_dev *mdev = priv->mdev;
1479         int err;
1480
1481         mutex_lock(&mdev->state_lock);
1482         if (mdev->device_up) {
1483                 if (priv->port_up) {
1484                         err = mlx4_en_DUMP_ETH_STATS(mdev, priv->port, 0);
1485                         if (err)
1486                                 en_dbg(HW, priv, "Could not update stats\n");
1487
1488                         mlx4_en_auto_moderation(priv);
1489                 }
1490
1491                 queue_delayed_work(mdev->workqueue, &priv->stats_task, STATS_DELAY);
1492         }
1493         if (mdev->mac_removed[MLX4_MAX_PORTS + 1 - priv->port]) {
1494                 mlx4_en_do_set_mac(priv);
1495                 mdev->mac_removed[MLX4_MAX_PORTS + 1 - priv->port] = 0;
1496         }
1497         mutex_unlock(&mdev->state_lock);
1498 }
1499
1500 /* mlx4_en_service_task - Run service task for tasks that needed to be done
1501  * periodically
1502  */
1503 static void mlx4_en_service_task(struct work_struct *work)
1504 {
1505         struct delayed_work *delay = to_delayed_work(work);
1506         struct mlx4_en_priv *priv = container_of(delay, struct mlx4_en_priv,
1507                                                  service_task);
1508         struct mlx4_en_dev *mdev = priv->mdev;
1509
1510         mutex_lock(&mdev->state_lock);
1511         if (mdev->device_up) {
1512                 if (mdev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_TS)
1513                         mlx4_en_ptp_overflow_check(mdev);
1514
1515                 queue_delayed_work(mdev->workqueue, &priv->service_task,
1516                                    SERVICE_TASK_DELAY);
1517         }
1518         mutex_unlock(&mdev->state_lock);
1519 }
1520
1521 static void mlx4_en_linkstate(struct work_struct *work)
1522 {
1523         struct mlx4_en_priv *priv = container_of(work, struct mlx4_en_priv,
1524                                                  linkstate_task);
1525         struct mlx4_en_dev *mdev = priv->mdev;
1526         int linkstate = priv->link_state;
1527
1528         mutex_lock(&mdev->state_lock);
1529         /* If observable port state changed set carrier state and
1530          * report to system log */
1531         if (priv->last_link_state != linkstate) {
1532                 if (linkstate == MLX4_DEV_EVENT_PORT_DOWN) {
1533                         en_info(priv, "Link Down\n");
1534                         netif_carrier_off(priv->dev);
1535                 } else {
1536                         en_info(priv, "Link Up\n");
1537                         netif_carrier_on(priv->dev);
1538                 }
1539         }
1540         priv->last_link_state = linkstate;
1541         mutex_unlock(&mdev->state_lock);
1542 }
1543
1544
1545 int mlx4_en_start_port(struct net_device *dev)
1546 {
1547         struct mlx4_en_priv *priv = netdev_priv(dev);
1548         struct mlx4_en_dev *mdev = priv->mdev;
1549         struct mlx4_en_cq *cq;
1550         struct mlx4_en_tx_ring *tx_ring;
1551         int rx_index = 0;
1552         int tx_index = 0;
1553         int err = 0;
1554         int i;
1555         int j;
1556         u8 mc_list[16] = {0};
1557
1558         if (priv->port_up) {
1559                 en_dbg(DRV, priv, "start port called while port already up\n");
1560                 return 0;
1561         }
1562
1563         INIT_LIST_HEAD(&priv->mc_list);
1564         INIT_LIST_HEAD(&priv->curr_list);
1565         INIT_LIST_HEAD(&priv->ethtool_list);
1566         memset(&priv->ethtool_rules[0], 0,
1567                sizeof(struct ethtool_flow_id) * MAX_NUM_OF_FS_RULES);
1568
1569         /* Calculate Rx buf size */
1570         dev->mtu = min(dev->mtu, priv->max_mtu);
1571         mlx4_en_calc_rx_buf(dev);
1572         en_dbg(DRV, priv, "Rx buf size:%d\n", priv->rx_skb_size);
1573
1574         /* Configure rx cq's and rings */
1575         err = mlx4_en_activate_rx_rings(priv);
1576         if (err) {
1577                 en_err(priv, "Failed to activate RX rings\n");
1578                 return err;
1579         }
1580         for (i = 0; i < priv->rx_ring_num; i++) {
1581                 cq = priv->rx_cq[i];
1582
1583                 mlx4_en_cq_init_lock(cq);
1584
1585                 err = mlx4_en_activate_cq(priv, cq, i);
1586                 if (err) {
1587                         en_err(priv, "Failed activating Rx CQ\n");
1588                         goto cq_err;
1589                 }
1590                 for (j = 0; j < cq->size; j++)
1591                         cq->buf[j].owner_sr_opcode = MLX4_CQE_OWNER_MASK;
1592                 err = mlx4_en_set_cq_moder(priv, cq);
1593                 if (err) {
1594                         en_err(priv, "Failed setting cq moderation parameters");
1595                         mlx4_en_deactivate_cq(priv, cq);
1596                         goto cq_err;
1597                 }
1598                 mlx4_en_arm_cq(priv, cq);
1599                 priv->rx_ring[i]->cqn = cq->mcq.cqn;
1600                 ++rx_index;
1601         }
1602
1603         /* Set qp number */
1604         en_dbg(DRV, priv, "Getting qp number for port %d\n", priv->port);
1605         err = mlx4_en_get_qp(priv);
1606         if (err) {
1607                 en_err(priv, "Failed getting eth qp\n");
1608                 goto cq_err;
1609         }
1610         mdev->mac_removed[priv->port] = 0;
1611
1612         err = mlx4_en_config_rss_steer(priv);
1613         if (err) {
1614                 en_err(priv, "Failed configuring rss steering\n");
1615                 goto mac_err;
1616         }
1617
1618         err = mlx4_en_create_drop_qp(priv);
1619         if (err)
1620                 goto rss_err;
1621
1622         /* Configure tx cq's and rings */
1623         for (i = 0; i < priv->tx_ring_num; i++) {
1624                 /* Configure cq */
1625                 cq = priv->tx_cq[i];
1626                 err = mlx4_en_activate_cq(priv, cq, i);
1627                 if (err) {
1628                         en_err(priv, "Failed allocating Tx CQ\n");
1629                         goto tx_err;
1630                 }
1631                 err = mlx4_en_set_cq_moder(priv, cq);
1632                 if (err) {
1633                         en_err(priv, "Failed setting cq moderation parameters");
1634                         mlx4_en_deactivate_cq(priv, cq);
1635                         goto tx_err;
1636                 }
1637                 en_dbg(DRV, priv, "Resetting index of collapsed CQ:%d to -1\n", i);
1638                 cq->buf->wqe_index = cpu_to_be16(0xffff);
1639
1640                 /* Configure ring */
1641                 tx_ring = priv->tx_ring[i];
1642                 err = mlx4_en_activate_tx_ring(priv, tx_ring, cq->mcq.cqn,
1643                         i / priv->num_tx_rings_p_up);
1644                 if (err) {
1645                         en_err(priv, "Failed allocating Tx ring\n");
1646                         mlx4_en_deactivate_cq(priv, cq);
1647                         goto tx_err;
1648                 }
1649                 tx_ring->tx_queue = netdev_get_tx_queue(dev, i);
1650
1651                 /* Arm CQ for TX completions */
1652                 mlx4_en_arm_cq(priv, cq);
1653
1654                 /* Set initial ownership of all Tx TXBBs to SW (1) */
1655                 for (j = 0; j < tx_ring->buf_size; j += STAMP_STRIDE)
1656                         *((u32 *) (tx_ring->buf + j)) = 0xffffffff;
1657                 ++tx_index;
1658         }
1659
1660         /* Configure port */
1661         err = mlx4_SET_PORT_general(mdev->dev, priv->port,
1662                                     priv->rx_skb_size + ETH_FCS_LEN,
1663                                     priv->prof->tx_pause,
1664                                     priv->prof->tx_ppp,
1665                                     priv->prof->rx_pause,
1666                                     priv->prof->rx_ppp);
1667         if (err) {
1668                 en_err(priv, "Failed setting port general configurations for port %d, with error %d\n",
1669                        priv->port, err);
1670                 goto tx_err;
1671         }
1672         /* Set default qp number */
1673         err = mlx4_SET_PORT_qpn_calc(mdev->dev, priv->port, priv->base_qpn, 0);
1674         if (err) {
1675                 en_err(priv, "Failed setting default qp numbers\n");
1676                 goto tx_err;
1677         }
1678
1679         if (mdev->dev->caps.tunnel_offload_mode == MLX4_TUNNEL_OFFLOAD_MODE_VXLAN) {
1680                 err = mlx4_SET_PORT_VXLAN(mdev->dev, priv->port, VXLAN_STEER_BY_OUTER_MAC);
1681                 if (err) {
1682                         en_err(priv, "Failed setting port L2 tunnel configuration, err %d\n",
1683                                err);
1684                         goto tx_err;
1685                 }
1686         }
1687
1688         /* Init port */
1689         en_dbg(HW, priv, "Initializing port\n");
1690         err = mlx4_INIT_PORT(mdev->dev, priv->port);
1691         if (err) {
1692                 en_err(priv, "Failed Initializing port\n");
1693                 goto tx_err;
1694         }
1695
1696         /* Attach rx QP to bradcast address */
1697         memset(&mc_list[10], 0xff, ETH_ALEN);
1698         mc_list[5] = priv->port; /* needed for B0 steering support */
1699         if (mlx4_multicast_attach(mdev->dev, &priv->rss_map.indir_qp, mc_list,
1700                                   priv->port, 0, MLX4_PROT_ETH,
1701                                   &priv->broadcast_id))
1702                 mlx4_warn(mdev, "Failed Attaching Broadcast\n");
1703
1704         /* Must redo promiscuous mode setup. */
1705         priv->flags &= ~(MLX4_EN_FLAG_PROMISC | MLX4_EN_FLAG_MC_PROMISC);
1706
1707         /* Schedule multicast task to populate multicast list */
1708         queue_work(mdev->workqueue, &priv->rx_mode_task);
1709
1710         mlx4_set_stats_bitmap(mdev->dev, &priv->stats_bitmap);
1711
1712         priv->port_up = true;
1713         netif_tx_start_all_queues(dev);
1714         netif_device_attach(dev);
1715
1716         return 0;
1717
1718 tx_err:
1719         while (tx_index--) {
1720                 mlx4_en_deactivate_tx_ring(priv, priv->tx_ring[tx_index]);
1721                 mlx4_en_deactivate_cq(priv, priv->tx_cq[tx_index]);
1722         }
1723         mlx4_en_destroy_drop_qp(priv);
1724 rss_err:
1725         mlx4_en_release_rss_steer(priv);
1726 mac_err:
1727         mlx4_en_put_qp(priv);
1728 cq_err:
1729         while (rx_index--)
1730                 mlx4_en_deactivate_cq(priv, priv->rx_cq[rx_index]);
1731         for (i = 0; i < priv->rx_ring_num; i++)
1732                 mlx4_en_deactivate_rx_ring(priv, priv->rx_ring[i]);
1733
1734         return err; /* need to close devices */
1735 }
1736
1737
1738 void mlx4_en_stop_port(struct net_device *dev, int detach)
1739 {
1740         struct mlx4_en_priv *priv = netdev_priv(dev);
1741         struct mlx4_en_dev *mdev = priv->mdev;
1742         struct mlx4_en_mc_list *mclist, *tmp;
1743         struct ethtool_flow_id *flow, *tmp_flow;
1744         int i;
1745         u8 mc_list[16] = {0};
1746
1747         if (!priv->port_up) {
1748                 en_dbg(DRV, priv, "stop port called while port already down\n");
1749                 return;
1750         }
1751
1752         /* close port*/
1753         mlx4_CLOSE_PORT(mdev->dev, priv->port);
1754
1755         /* Synchronize with tx routine */
1756         netif_tx_lock_bh(dev);
1757         if (detach)
1758                 netif_device_detach(dev);
1759         netif_tx_stop_all_queues(dev);
1760         netif_tx_unlock_bh(dev);
1761
1762         netif_tx_disable(dev);
1763
1764         /* Set port as not active */
1765         priv->port_up = false;
1766
1767         /* Promsicuous mode */
1768         if (mdev->dev->caps.steering_mode ==
1769             MLX4_STEERING_MODE_DEVICE_MANAGED) {
1770                 priv->flags &= ~(MLX4_EN_FLAG_PROMISC |
1771                                  MLX4_EN_FLAG_MC_PROMISC);
1772                 mlx4_flow_steer_promisc_remove(mdev->dev,
1773                                                priv->port,
1774                                                MLX4_FS_ALL_DEFAULT);
1775                 mlx4_flow_steer_promisc_remove(mdev->dev,
1776                                                priv->port,
1777                                                MLX4_FS_MC_DEFAULT);
1778         } else if (priv->flags & MLX4_EN_FLAG_PROMISC) {
1779                 priv->flags &= ~MLX4_EN_FLAG_PROMISC;
1780
1781                 /* Disable promiscouos mode */
1782                 mlx4_unicast_promisc_remove(mdev->dev, priv->base_qpn,
1783                                             priv->port);
1784
1785                 /* Disable Multicast promisc */
1786                 if (priv->flags & MLX4_EN_FLAG_MC_PROMISC) {
1787                         mlx4_multicast_promisc_remove(mdev->dev, priv->base_qpn,
1788                                                       priv->port);
1789                         priv->flags &= ~MLX4_EN_FLAG_MC_PROMISC;
1790                 }
1791         }
1792
1793         /* Detach All multicasts */
1794         memset(&mc_list[10], 0xff, ETH_ALEN);
1795         mc_list[5] = priv->port; /* needed for B0 steering support */
1796         mlx4_multicast_detach(mdev->dev, &priv->rss_map.indir_qp, mc_list,
1797                               MLX4_PROT_ETH, priv->broadcast_id);
1798         list_for_each_entry(mclist, &priv->curr_list, list) {
1799                 memcpy(&mc_list[10], mclist->addr, ETH_ALEN);
1800                 mc_list[5] = priv->port;
1801                 mlx4_multicast_detach(mdev->dev, &priv->rss_map.indir_qp,
1802                                       mc_list, MLX4_PROT_ETH, mclist->reg_id);
1803         }
1804         mlx4_en_clear_list(dev);
1805         list_for_each_entry_safe(mclist, tmp, &priv->curr_list, list) {
1806                 list_del(&mclist->list);
1807                 kfree(mclist);
1808         }
1809
1810         /* Flush multicast filter */
1811         mlx4_SET_MCAST_FLTR(mdev->dev, priv->port, 0, 1, MLX4_MCAST_CONFIG);
1812
1813         /* Remove flow steering rules for the port*/
1814         if (mdev->dev->caps.steering_mode ==
1815             MLX4_STEERING_MODE_DEVICE_MANAGED) {
1816                 ASSERT_RTNL();
1817                 list_for_each_entry_safe(flow, tmp_flow,
1818                                          &priv->ethtool_list, list) {
1819                         mlx4_flow_detach(mdev->dev, flow->id);
1820                         list_del(&flow->list);
1821                 }
1822         }
1823
1824         mlx4_en_destroy_drop_qp(priv);
1825
1826         /* Free TX Rings */
1827         for (i = 0; i < priv->tx_ring_num; i++) {
1828                 mlx4_en_deactivate_tx_ring(priv, priv->tx_ring[i]);
1829                 mlx4_en_deactivate_cq(priv, priv->tx_cq[i]);
1830         }
1831         msleep(10);
1832
1833         for (i = 0; i < priv->tx_ring_num; i++)
1834                 mlx4_en_free_tx_buf(dev, priv->tx_ring[i]);
1835
1836         /* Free RSS qps */
1837         mlx4_en_release_rss_steer(priv);
1838
1839         /* Unregister Mac address for the port */
1840         mlx4_en_put_qp(priv);
1841         if (!(mdev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_REASSIGN_MAC_EN))
1842                 mdev->mac_removed[priv->port] = 1;
1843
1844         /* Free RX Rings */
1845         for (i = 0; i < priv->rx_ring_num; i++) {
1846                 struct mlx4_en_cq *cq = priv->rx_cq[i];
1847
1848                 local_bh_disable();
1849                 while (!mlx4_en_cq_lock_napi(cq)) {
1850                         pr_info("CQ %d locked\n", i);
1851                         mdelay(1);
1852                 }
1853                 local_bh_enable();
1854
1855                 while (test_bit(NAPI_STATE_SCHED, &cq->napi.state))
1856                         msleep(1);
1857                 mlx4_en_deactivate_rx_ring(priv, priv->rx_ring[i]);
1858                 mlx4_en_deactivate_cq(priv, cq);
1859         }
1860 }
1861
1862 static void mlx4_en_restart(struct work_struct *work)
1863 {
1864         struct mlx4_en_priv *priv = container_of(work, struct mlx4_en_priv,
1865                                                  watchdog_task);
1866         struct mlx4_en_dev *mdev = priv->mdev;
1867         struct net_device *dev = priv->dev;
1868
1869         en_dbg(DRV, priv, "Watchdog task called for port %d\n", priv->port);
1870
1871         mutex_lock(&mdev->state_lock);
1872         if (priv->port_up) {
1873                 mlx4_en_stop_port(dev, 1);
1874                 if (mlx4_en_start_port(dev))
1875                         en_err(priv, "Failed restarting port %d\n", priv->port);
1876         }
1877         mutex_unlock(&mdev->state_lock);
1878 }
1879
1880 static void mlx4_en_clear_stats(struct net_device *dev)
1881 {
1882         struct mlx4_en_priv *priv = netdev_priv(dev);
1883         struct mlx4_en_dev *mdev = priv->mdev;
1884         int i;
1885
1886         if (mlx4_en_DUMP_ETH_STATS(mdev, priv->port, 1))
1887                 en_dbg(HW, priv, "Failed dumping statistics\n");
1888
1889         memset(&priv->stats, 0, sizeof(priv->stats));
1890         memset(&priv->pstats, 0, sizeof(priv->pstats));
1891         memset(&priv->pkstats, 0, sizeof(priv->pkstats));
1892         memset(&priv->port_stats, 0, sizeof(priv->port_stats));
1893
1894         for (i = 0; i < priv->tx_ring_num; i++) {
1895                 priv->tx_ring[i]->bytes = 0;
1896                 priv->tx_ring[i]->packets = 0;
1897                 priv->tx_ring[i]->tx_csum = 0;
1898         }
1899         for (i = 0; i < priv->rx_ring_num; i++) {
1900                 priv->rx_ring[i]->bytes = 0;
1901                 priv->rx_ring[i]->packets = 0;
1902                 priv->rx_ring[i]->csum_ok = 0;
1903                 priv->rx_ring[i]->csum_none = 0;
1904         }
1905 }
1906
1907 static int mlx4_en_open(struct net_device *dev)
1908 {
1909         struct mlx4_en_priv *priv = netdev_priv(dev);
1910         struct mlx4_en_dev *mdev = priv->mdev;
1911         int err = 0;
1912
1913         mutex_lock(&mdev->state_lock);
1914
1915         if (!mdev->device_up) {
1916                 en_err(priv, "Cannot open - device down/disabled\n");
1917                 err = -EBUSY;
1918                 goto out;
1919         }
1920
1921         /* Reset HW statistics and SW counters */
1922         mlx4_en_clear_stats(dev);
1923
1924         err = mlx4_en_start_port(dev);
1925         if (err)
1926                 en_err(priv, "Failed starting port:%d\n", priv->port);
1927
1928 out:
1929         mutex_unlock(&mdev->state_lock);
1930         return err;
1931 }
1932
1933
1934 static int mlx4_en_close(struct net_device *dev)
1935 {
1936         struct mlx4_en_priv *priv = netdev_priv(dev);
1937         struct mlx4_en_dev *mdev = priv->mdev;
1938
1939         en_dbg(IFDOWN, priv, "Close port called\n");
1940
1941         mutex_lock(&mdev->state_lock);
1942
1943         mlx4_en_stop_port(dev, 0);
1944         netif_carrier_off(dev);
1945
1946         mutex_unlock(&mdev->state_lock);
1947         return 0;
1948 }
1949
1950 void mlx4_en_free_resources(struct mlx4_en_priv *priv)
1951 {
1952         int i;
1953
1954 #ifdef CONFIG_RFS_ACCEL
1955         free_irq_cpu_rmap(priv->dev->rx_cpu_rmap);
1956         priv->dev->rx_cpu_rmap = NULL;
1957 #endif
1958
1959         for (i = 0; i < priv->tx_ring_num; i++) {
1960                 if (priv->tx_ring && priv->tx_ring[i])
1961                         mlx4_en_destroy_tx_ring(priv, &priv->tx_ring[i]);
1962                 if (priv->tx_cq && priv->tx_cq[i])
1963                         mlx4_en_destroy_cq(priv, &priv->tx_cq[i]);
1964         }
1965
1966         for (i = 0; i < priv->rx_ring_num; i++) {
1967                 if (priv->rx_ring[i])
1968                         mlx4_en_destroy_rx_ring(priv, &priv->rx_ring[i],
1969                                 priv->prof->rx_ring_size, priv->stride);
1970                 if (priv->rx_cq[i])
1971                         mlx4_en_destroy_cq(priv, &priv->rx_cq[i]);
1972         }
1973
1974         if (priv->base_tx_qpn) {
1975                 mlx4_qp_release_range(priv->mdev->dev, priv->base_tx_qpn, priv->tx_ring_num);
1976                 priv->base_tx_qpn = 0;
1977         }
1978 }
1979
1980 int mlx4_en_alloc_resources(struct mlx4_en_priv *priv)
1981 {
1982         struct mlx4_en_port_profile *prof = priv->prof;
1983         int i;
1984         int err;
1985         int node;
1986
1987         err = mlx4_qp_reserve_range(priv->mdev->dev, priv->tx_ring_num, 256, &priv->base_tx_qpn);
1988         if (err) {
1989                 en_err(priv, "failed reserving range for TX rings\n");
1990                 return err;
1991         }
1992
1993         /* Create tx Rings */
1994         for (i = 0; i < priv->tx_ring_num; i++) {
1995                 node = cpu_to_node(i % num_online_cpus());
1996                 if (mlx4_en_create_cq(priv, &priv->tx_cq[i],
1997                                       prof->tx_ring_size, i, TX, node))
1998                         goto err;
1999
2000                 if (mlx4_en_create_tx_ring(priv, &priv->tx_ring[i],
2001                                            priv->base_tx_qpn + i,
2002                                            prof->tx_ring_size, TXBB_SIZE,
2003                                            node, i))
2004                         goto err;
2005         }
2006
2007         /* Create rx Rings */
2008         for (i = 0; i < priv->rx_ring_num; i++) {
2009                 node = cpu_to_node(i % num_online_cpus());
2010                 if (mlx4_en_create_cq(priv, &priv->rx_cq[i],
2011                                       prof->rx_ring_size, i, RX, node))
2012                         goto err;
2013
2014                 if (mlx4_en_create_rx_ring(priv, &priv->rx_ring[i],
2015                                            prof->rx_ring_size, priv->stride,
2016                                            node))
2017                         goto err;
2018         }
2019
2020 #ifdef CONFIG_RFS_ACCEL
2021         if (priv->mdev->dev->caps.comp_pool) {
2022                 priv->dev->rx_cpu_rmap = alloc_irq_cpu_rmap(priv->mdev->dev->caps.comp_pool);
2023                 if (!priv->dev->rx_cpu_rmap)
2024                         goto err;
2025         }
2026 #endif
2027
2028         return 0;
2029
2030 err:
2031         en_err(priv, "Failed to allocate NIC resources\n");
2032         for (i = 0; i < priv->rx_ring_num; i++) {
2033                 if (priv->rx_ring[i])
2034                         mlx4_en_destroy_rx_ring(priv, &priv->rx_ring[i],
2035                                                 prof->rx_ring_size,
2036                                                 priv->stride);
2037                 if (priv->rx_cq[i])
2038                         mlx4_en_destroy_cq(priv, &priv->rx_cq[i]);
2039         }
2040         for (i = 0; i < priv->tx_ring_num; i++) {
2041                 if (priv->tx_ring[i])
2042                         mlx4_en_destroy_tx_ring(priv, &priv->tx_ring[i]);
2043                 if (priv->tx_cq[i])
2044                         mlx4_en_destroy_cq(priv, &priv->tx_cq[i]);
2045         }
2046         return -ENOMEM;
2047 }
2048
2049
2050 void mlx4_en_destroy_netdev(struct net_device *dev)
2051 {
2052         struct mlx4_en_priv *priv = netdev_priv(dev);
2053         struct mlx4_en_dev *mdev = priv->mdev;
2054
2055         en_dbg(DRV, priv, "Destroying netdev on port:%d\n", priv->port);
2056
2057         /* Unregister device - this will close the port if it was up */
2058         if (priv->registered)
2059                 unregister_netdev(dev);
2060
2061         if (priv->allocated)
2062                 mlx4_free_hwq_res(mdev->dev, &priv->res, MLX4_EN_PAGE_SIZE);
2063
2064         cancel_delayed_work(&priv->stats_task);
2065         cancel_delayed_work(&priv->service_task);
2066         /* flush any pending task for this netdev */
2067         flush_workqueue(mdev->workqueue);
2068
2069         /* Detach the netdev so tasks would not attempt to access it */
2070         mutex_lock(&mdev->state_lock);
2071         mdev->pndev[priv->port] = NULL;
2072         mutex_unlock(&mdev->state_lock);
2073
2074         mlx4_en_free_resources(priv);
2075
2076         kfree(priv->tx_ring);
2077         kfree(priv->tx_cq);
2078
2079         free_netdev(dev);
2080 }
2081
2082 static int mlx4_en_change_mtu(struct net_device *dev, int new_mtu)
2083 {
2084         struct mlx4_en_priv *priv = netdev_priv(dev);
2085         struct mlx4_en_dev *mdev = priv->mdev;
2086         int err = 0;
2087
2088         en_dbg(DRV, priv, "Change MTU called - current:%d new:%d\n",
2089                  dev->mtu, new_mtu);
2090
2091         if ((new_mtu < MLX4_EN_MIN_MTU) || (new_mtu > priv->max_mtu)) {
2092                 en_err(priv, "Bad MTU size:%d.\n", new_mtu);
2093                 return -EPERM;
2094         }
2095         dev->mtu = new_mtu;
2096
2097         if (netif_running(dev)) {
2098                 mutex_lock(&mdev->state_lock);
2099                 if (!mdev->device_up) {
2100                         /* NIC is probably restarting - let watchdog task reset
2101                          * the port */
2102                         en_dbg(DRV, priv, "Change MTU called with card down!?\n");
2103                 } else {
2104                         mlx4_en_stop_port(dev, 1);
2105                         err = mlx4_en_start_port(dev);
2106                         if (err) {
2107                                 en_err(priv, "Failed restarting port:%d\n",
2108                                          priv->port);
2109                                 queue_work(mdev->workqueue, &priv->watchdog_task);
2110                         }
2111                 }
2112                 mutex_unlock(&mdev->state_lock);
2113         }
2114         return 0;
2115 }
2116
2117 static int mlx4_en_hwtstamp_set(struct net_device *dev, struct ifreq *ifr)
2118 {
2119         struct mlx4_en_priv *priv = netdev_priv(dev);
2120         struct mlx4_en_dev *mdev = priv->mdev;
2121         struct hwtstamp_config config;
2122
2123         if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
2124                 return -EFAULT;
2125
2126         /* reserved for future extensions */
2127         if (config.flags)
2128                 return -EINVAL;
2129
2130         /* device doesn't support time stamping */
2131         if (!(mdev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_TS))
2132                 return -EINVAL;
2133
2134         /* TX HW timestamp */
2135         switch (config.tx_type) {
2136         case HWTSTAMP_TX_OFF:
2137         case HWTSTAMP_TX_ON:
2138                 break;
2139         default:
2140                 return -ERANGE;
2141         }
2142
2143         /* RX HW timestamp */
2144         switch (config.rx_filter) {
2145         case HWTSTAMP_FILTER_NONE:
2146                 break;
2147         case HWTSTAMP_FILTER_ALL:
2148         case HWTSTAMP_FILTER_SOME:
2149         case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
2150         case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
2151         case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
2152         case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
2153         case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
2154         case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
2155         case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
2156         case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
2157         case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
2158         case HWTSTAMP_FILTER_PTP_V2_EVENT:
2159         case HWTSTAMP_FILTER_PTP_V2_SYNC:
2160         case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
2161                 config.rx_filter = HWTSTAMP_FILTER_ALL;
2162                 break;
2163         default:
2164                 return -ERANGE;
2165         }
2166
2167         if (mlx4_en_timestamp_config(dev, config.tx_type, config.rx_filter)) {
2168                 config.tx_type = HWTSTAMP_TX_OFF;
2169                 config.rx_filter = HWTSTAMP_FILTER_NONE;
2170         }
2171
2172         return copy_to_user(ifr->ifr_data, &config,
2173                             sizeof(config)) ? -EFAULT : 0;
2174 }
2175
2176 static int mlx4_en_hwtstamp_get(struct net_device *dev, struct ifreq *ifr)
2177 {
2178         struct mlx4_en_priv *priv = netdev_priv(dev);
2179
2180         return copy_to_user(ifr->ifr_data, &priv->hwtstamp_config,
2181                             sizeof(priv->hwtstamp_config)) ? -EFAULT : 0;
2182 }
2183
2184 static int mlx4_en_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
2185 {
2186         switch (cmd) {
2187         case SIOCSHWTSTAMP:
2188                 return mlx4_en_hwtstamp_set(dev, ifr);
2189         case SIOCGHWTSTAMP:
2190                 return mlx4_en_hwtstamp_get(dev, ifr);
2191         default:
2192                 return -EOPNOTSUPP;
2193         }
2194 }
2195
2196 static int mlx4_en_set_features(struct net_device *netdev,
2197                 netdev_features_t features)
2198 {
2199         struct mlx4_en_priv *priv = netdev_priv(netdev);
2200
2201         if (features & NETIF_F_LOOPBACK)
2202                 priv->ctrl_flags |= cpu_to_be32(MLX4_WQE_CTRL_FORCE_LOOPBACK);
2203         else
2204                 priv->ctrl_flags &=
2205                         cpu_to_be32(~MLX4_WQE_CTRL_FORCE_LOOPBACK);
2206
2207         mlx4_en_update_loopback_state(netdev, features);
2208
2209         return 0;
2210
2211 }
2212
2213 static int mlx4_en_set_vf_mac(struct net_device *dev, int queue, u8 *mac)
2214 {
2215         struct mlx4_en_priv *en_priv = netdev_priv(dev);
2216         struct mlx4_en_dev *mdev = en_priv->mdev;
2217         u64 mac_u64 = mlx4_en_mac_to_u64(mac);
2218
2219         if (!is_valid_ether_addr(mac))
2220                 return -EINVAL;
2221
2222         return mlx4_set_vf_mac(mdev->dev, en_priv->port, queue, mac_u64);
2223 }
2224
2225 static int mlx4_en_set_vf_vlan(struct net_device *dev, int vf, u16 vlan, u8 qos)
2226 {
2227         struct mlx4_en_priv *en_priv = netdev_priv(dev);
2228         struct mlx4_en_dev *mdev = en_priv->mdev;
2229
2230         return mlx4_set_vf_vlan(mdev->dev, en_priv->port, vf, vlan, qos);
2231 }
2232
2233 static int mlx4_en_set_vf_spoofchk(struct net_device *dev, int vf, bool setting)
2234 {
2235         struct mlx4_en_priv *en_priv = netdev_priv(dev);
2236         struct mlx4_en_dev *mdev = en_priv->mdev;
2237
2238         return mlx4_set_vf_spoofchk(mdev->dev, en_priv->port, vf, setting);
2239 }
2240
2241 static int mlx4_en_get_vf_config(struct net_device *dev, int vf, struct ifla_vf_info *ivf)
2242 {
2243         struct mlx4_en_priv *en_priv = netdev_priv(dev);
2244         struct mlx4_en_dev *mdev = en_priv->mdev;
2245
2246         return mlx4_get_vf_config(mdev->dev, en_priv->port, vf, ivf);
2247 }
2248
2249 static int mlx4_en_set_vf_link_state(struct net_device *dev, int vf, int link_state)
2250 {
2251         struct mlx4_en_priv *en_priv = netdev_priv(dev);
2252         struct mlx4_en_dev *mdev = en_priv->mdev;
2253
2254         return mlx4_set_vf_link_state(mdev->dev, en_priv->port, vf, link_state);
2255 }
2256
2257 #define PORT_ID_BYTE_LEN 8
2258 static int mlx4_en_get_phys_port_id(struct net_device *dev,
2259                                     struct netdev_phys_port_id *ppid)
2260 {
2261         struct mlx4_en_priv *priv = netdev_priv(dev);
2262         struct mlx4_dev *mdev = priv->mdev->dev;
2263         int i;
2264         u64 phys_port_id = mdev->caps.phys_port_id[priv->port];
2265
2266         if (!phys_port_id)
2267                 return -EOPNOTSUPP;
2268
2269         ppid->id_len = sizeof(phys_port_id);
2270         for (i = PORT_ID_BYTE_LEN - 1; i >= 0; --i) {
2271                 ppid->id[i] =  phys_port_id & 0xff;
2272                 phys_port_id >>= 8;
2273         }
2274         return 0;
2275 }
2276
2277 static const struct net_device_ops mlx4_netdev_ops = {
2278         .ndo_open               = mlx4_en_open,
2279         .ndo_stop               = mlx4_en_close,
2280         .ndo_start_xmit         = mlx4_en_xmit,
2281         .ndo_select_queue       = mlx4_en_select_queue,
2282         .ndo_get_stats          = mlx4_en_get_stats,
2283         .ndo_set_rx_mode        = mlx4_en_set_rx_mode,
2284         .ndo_set_mac_address    = mlx4_en_set_mac,
2285         .ndo_validate_addr      = eth_validate_addr,
2286         .ndo_change_mtu         = mlx4_en_change_mtu,
2287         .ndo_do_ioctl           = mlx4_en_ioctl,
2288         .ndo_tx_timeout         = mlx4_en_tx_timeout,
2289         .ndo_vlan_rx_add_vid    = mlx4_en_vlan_rx_add_vid,
2290         .ndo_vlan_rx_kill_vid   = mlx4_en_vlan_rx_kill_vid,
2291 #ifdef CONFIG_NET_POLL_CONTROLLER
2292         .ndo_poll_controller    = mlx4_en_netpoll,
2293 #endif
2294         .ndo_set_features       = mlx4_en_set_features,
2295         .ndo_setup_tc           = mlx4_en_setup_tc,
2296 #ifdef CONFIG_RFS_ACCEL
2297         .ndo_rx_flow_steer      = mlx4_en_filter_rfs,
2298 #endif
2299 #ifdef CONFIG_NET_RX_BUSY_POLL
2300         .ndo_busy_poll          = mlx4_en_low_latency_recv,
2301 #endif
2302         .ndo_get_phys_port_id   = mlx4_en_get_phys_port_id,
2303 };
2304
2305 static const struct net_device_ops mlx4_netdev_ops_master = {
2306         .ndo_open               = mlx4_en_open,
2307         .ndo_stop               = mlx4_en_close,
2308         .ndo_start_xmit         = mlx4_en_xmit,
2309         .ndo_select_queue       = mlx4_en_select_queue,
2310         .ndo_get_stats          = mlx4_en_get_stats,
2311         .ndo_set_rx_mode        = mlx4_en_set_rx_mode,
2312         .ndo_set_mac_address    = mlx4_en_set_mac,
2313         .ndo_validate_addr      = eth_validate_addr,
2314         .ndo_change_mtu         = mlx4_en_change_mtu,
2315         .ndo_tx_timeout         = mlx4_en_tx_timeout,
2316         .ndo_vlan_rx_add_vid    = mlx4_en_vlan_rx_add_vid,
2317         .ndo_vlan_rx_kill_vid   = mlx4_en_vlan_rx_kill_vid,
2318         .ndo_set_vf_mac         = mlx4_en_set_vf_mac,
2319         .ndo_set_vf_vlan        = mlx4_en_set_vf_vlan,
2320         .ndo_set_vf_spoofchk    = mlx4_en_set_vf_spoofchk,
2321         .ndo_set_vf_link_state  = mlx4_en_set_vf_link_state,
2322         .ndo_get_vf_config      = mlx4_en_get_vf_config,
2323 #ifdef CONFIG_NET_POLL_CONTROLLER
2324         .ndo_poll_controller    = mlx4_en_netpoll,
2325 #endif
2326         .ndo_set_features       = mlx4_en_set_features,
2327         .ndo_setup_tc           = mlx4_en_setup_tc,
2328 #ifdef CONFIG_RFS_ACCEL
2329         .ndo_rx_flow_steer      = mlx4_en_filter_rfs,
2330 #endif
2331         .ndo_get_phys_port_id   = mlx4_en_get_phys_port_id,
2332 };
2333
2334 int mlx4_en_init_netdev(struct mlx4_en_dev *mdev, int port,
2335                         struct mlx4_en_port_profile *prof)
2336 {
2337         struct net_device *dev;
2338         struct mlx4_en_priv *priv;
2339         int i;
2340         int err;
2341         u64 mac_u64;
2342
2343         dev = alloc_etherdev_mqs(sizeof(struct mlx4_en_priv),
2344                                  MAX_TX_RINGS, MAX_RX_RINGS);
2345         if (dev == NULL)
2346                 return -ENOMEM;
2347
2348         netif_set_real_num_tx_queues(dev, prof->tx_ring_num);
2349         netif_set_real_num_rx_queues(dev, prof->rx_ring_num);
2350
2351         SET_NETDEV_DEV(dev, &mdev->dev->pdev->dev);
2352         dev->dev_id =  port - 1;
2353
2354         /*
2355          * Initialize driver private data
2356          */
2357
2358         priv = netdev_priv(dev);
2359         memset(priv, 0, sizeof(struct mlx4_en_priv));
2360         priv->dev = dev;
2361         priv->mdev = mdev;
2362         priv->ddev = &mdev->pdev->dev;
2363         priv->prof = prof;
2364         priv->port = port;
2365         priv->port_up = false;
2366         priv->flags = prof->flags;
2367         priv->ctrl_flags = cpu_to_be32(MLX4_WQE_CTRL_CQ_UPDATE |
2368                         MLX4_WQE_CTRL_SOLICITED);
2369         priv->num_tx_rings_p_up = mdev->profile.num_tx_rings_p_up;
2370         priv->tx_ring_num = prof->tx_ring_num;
2371
2372         priv->tx_ring = kzalloc(sizeof(struct mlx4_en_tx_ring *) * MAX_TX_RINGS,
2373                                 GFP_KERNEL);
2374         if (!priv->tx_ring) {
2375                 err = -ENOMEM;
2376                 goto out;
2377         }
2378         priv->tx_cq = kzalloc(sizeof(struct mlx4_en_cq *) * MAX_TX_RINGS,
2379                               GFP_KERNEL);
2380         if (!priv->tx_cq) {
2381                 err = -ENOMEM;
2382                 goto out;
2383         }
2384         priv->rx_ring_num = prof->rx_ring_num;
2385         priv->cqe_factor = (mdev->dev->caps.cqe_size == 64) ? 1 : 0;
2386         priv->mac_index = -1;
2387         priv->msg_enable = MLX4_EN_MSG_LEVEL;
2388         spin_lock_init(&priv->stats_lock);
2389         INIT_WORK(&priv->rx_mode_task, mlx4_en_do_set_rx_mode);
2390         INIT_WORK(&priv->watchdog_task, mlx4_en_restart);
2391         INIT_WORK(&priv->linkstate_task, mlx4_en_linkstate);
2392         INIT_DELAYED_WORK(&priv->stats_task, mlx4_en_do_get_stats);
2393         INIT_DELAYED_WORK(&priv->service_task, mlx4_en_service_task);
2394 #ifdef CONFIG_MLX4_EN_DCB
2395         if (!mlx4_is_slave(priv->mdev->dev)) {
2396                 if (mdev->dev->caps.flags & MLX4_DEV_CAP_FLAG_SET_ETH_SCHED) {
2397                         dev->dcbnl_ops = &mlx4_en_dcbnl_ops;
2398                 } else {
2399                         en_info(priv, "enabling only PFC DCB ops\n");
2400                         dev->dcbnl_ops = &mlx4_en_dcbnl_pfc_ops;
2401                 }
2402         }
2403 #endif
2404
2405         for (i = 0; i < MLX4_EN_MAC_HASH_SIZE; ++i)
2406                 INIT_HLIST_HEAD(&priv->mac_hash[i]);
2407
2408         /* Query for default mac and max mtu */
2409         priv->max_mtu = mdev->dev->caps.eth_mtu_cap[priv->port];
2410
2411         /* Set default MAC */
2412         dev->addr_len = ETH_ALEN;
2413         mlx4_en_u64_to_mac(dev->dev_addr, mdev->dev->caps.def_mac[priv->port]);
2414         if (!is_valid_ether_addr(dev->dev_addr)) {
2415                 if (mlx4_is_slave(priv->mdev->dev)) {
2416                         eth_hw_addr_random(dev);
2417                         en_warn(priv, "Assigned random MAC address %pM\n", dev->dev_addr);
2418                         mac_u64 = mlx4_en_mac_to_u64(dev->dev_addr);
2419                         mdev->dev->caps.def_mac[priv->port] = mac_u64;
2420                 } else {
2421                         en_err(priv, "Port: %d, invalid mac burned: %pM, quiting\n",
2422                                priv->port, dev->dev_addr);
2423                         err = -EINVAL;
2424                         goto out;
2425                 }
2426         }
2427
2428         memcpy(priv->prev_mac, dev->dev_addr, sizeof(priv->prev_mac));
2429
2430         priv->stride = roundup_pow_of_two(sizeof(struct mlx4_en_rx_desc) +
2431                                           DS_SIZE * MLX4_EN_MAX_RX_FRAGS);
2432         err = mlx4_en_alloc_resources(priv);
2433         if (err)
2434                 goto out;
2435
2436 #ifdef CONFIG_RFS_ACCEL
2437         INIT_LIST_HEAD(&priv->filters);
2438         spin_lock_init(&priv->filters_lock);
2439 #endif
2440
2441         /* Initialize time stamping config */
2442         priv->hwtstamp_config.flags = 0;
2443         priv->hwtstamp_config.tx_type = HWTSTAMP_TX_OFF;
2444         priv->hwtstamp_config.rx_filter = HWTSTAMP_FILTER_NONE;
2445
2446         /* Allocate page for receive rings */
2447         err = mlx4_alloc_hwq_res(mdev->dev, &priv->res,
2448                                 MLX4_EN_PAGE_SIZE, MLX4_EN_PAGE_SIZE);
2449         if (err) {
2450                 en_err(priv, "Failed to allocate page for rx qps\n");
2451                 goto out;
2452         }
2453         priv->allocated = 1;
2454
2455         /*
2456          * Initialize netdev entry points
2457          */
2458         if (mlx4_is_master(priv->mdev->dev))
2459                 dev->netdev_ops = &mlx4_netdev_ops_master;
2460         else
2461                 dev->netdev_ops = &mlx4_netdev_ops;
2462         dev->watchdog_timeo = MLX4_EN_WATCHDOG_TIMEOUT;
2463         netif_set_real_num_tx_queues(dev, priv->tx_ring_num);
2464         netif_set_real_num_rx_queues(dev, priv->rx_ring_num);
2465
2466         SET_ETHTOOL_OPS(dev, &mlx4_en_ethtool_ops);
2467
2468         /*
2469          * Set driver features
2470          */
2471         dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM;
2472         if (mdev->LSO_support)
2473                 dev->hw_features |= NETIF_F_TSO | NETIF_F_TSO6;
2474
2475         dev->vlan_features = dev->hw_features;
2476
2477         dev->hw_features |= NETIF_F_RXCSUM | NETIF_F_RXHASH;
2478         dev->features = dev->hw_features | NETIF_F_HIGHDMA |
2479                         NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX |
2480                         NETIF_F_HW_VLAN_CTAG_FILTER;
2481         dev->hw_features |= NETIF_F_LOOPBACK;
2482
2483         if (mdev->dev->caps.steering_mode ==
2484             MLX4_STEERING_MODE_DEVICE_MANAGED)
2485                 dev->hw_features |= NETIF_F_NTUPLE;
2486
2487         if (mdev->dev->caps.steering_mode != MLX4_STEERING_MODE_A0)
2488                 dev->priv_flags |= IFF_UNICAST_FLT;
2489
2490         if (mdev->dev->caps.tunnel_offload_mode == MLX4_TUNNEL_OFFLOAD_MODE_VXLAN) {
2491                 dev->hw_enc_features |= NETIF_F_IP_CSUM | NETIF_F_RXCSUM |
2492                                         NETIF_F_TSO | NETIF_F_GSO_UDP_TUNNEL;
2493                 dev->hw_features |= NETIF_F_GSO_UDP_TUNNEL;
2494                 dev->features    |= NETIF_F_GSO_UDP_TUNNEL;
2495         }
2496
2497         mdev->pndev[port] = dev;
2498
2499         netif_carrier_off(dev);
2500         mlx4_en_set_default_moderation(priv);
2501
2502         err = register_netdev(dev);
2503         if (err) {
2504                 en_err(priv, "Netdev registration failed for port %d\n", port);
2505                 goto out;
2506         }
2507         priv->registered = 1;
2508
2509         en_warn(priv, "Using %d TX rings\n", prof->tx_ring_num);
2510         en_warn(priv, "Using %d RX rings\n", prof->rx_ring_num);
2511
2512         mlx4_en_update_loopback_state(priv->dev, priv->dev->features);
2513
2514         /* Configure port */
2515         mlx4_en_calc_rx_buf(dev);
2516         err = mlx4_SET_PORT_general(mdev->dev, priv->port,
2517                                     priv->rx_skb_size + ETH_FCS_LEN,
2518                                     prof->tx_pause, prof->tx_ppp,
2519                                     prof->rx_pause, prof->rx_ppp);
2520         if (err) {
2521                 en_err(priv, "Failed setting port general configurations "
2522                        "for port %d, with error %d\n", priv->port, err);
2523                 goto out;
2524         }
2525
2526         if (mdev->dev->caps.tunnel_offload_mode == MLX4_TUNNEL_OFFLOAD_MODE_VXLAN) {
2527                 err = mlx4_SET_PORT_VXLAN(mdev->dev, priv->port, VXLAN_STEER_BY_OUTER_MAC);
2528                 if (err) {
2529                         en_err(priv, "Failed setting port L2 tunnel configuration, err %d\n",
2530                                err);
2531                         goto out;
2532                 }
2533         }
2534
2535         /* Init port */
2536         en_warn(priv, "Initializing port\n");
2537         err = mlx4_INIT_PORT(mdev->dev, priv->port);
2538         if (err) {
2539                 en_err(priv, "Failed Initializing port\n");
2540                 goto out;
2541         }
2542         queue_delayed_work(mdev->workqueue, &priv->stats_task, STATS_DELAY);
2543
2544         if (mdev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_TS)
2545                 queue_delayed_work(mdev->workqueue, &priv->service_task,
2546                                    SERVICE_TASK_DELAY);
2547
2548         return 0;
2549
2550 out:
2551         mlx4_en_destroy_netdev(dev);
2552         return err;
2553 }
2554