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