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