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