xref: /openbmc/linux/drivers/net/ethernet/mellanox/mlx4/en_netdev.c (revision c51d39010a1bccc9c1294e2d7c00005aefeb2b5c)
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/bpf.h>
35 #include <linux/etherdevice.h>
36 #include <linux/tcp.h>
37 #include <linux/if_vlan.h>
38 #include <linux/delay.h>
39 #include <linux/slab.h>
40 #include <linux/hash.h>
41 #include <net/ip.h>
42 #include <net/busy_poll.h>
43 #include <net/vxlan.h>
44 #include <net/devlink.h>
45 
46 #include <linux/mlx4/driver.h>
47 #include <linux/mlx4/device.h>
48 #include <linux/mlx4/cmd.h>
49 #include <linux/mlx4/cq.h>
50 
51 #include "mlx4_en.h"
52 #include "en_port.h"
53 
54 int mlx4_en_setup_tc(struct net_device *dev, u8 up)
55 {
56 	struct mlx4_en_priv *priv = netdev_priv(dev);
57 	int i;
58 	unsigned int offset = 0;
59 
60 	if (up && up != MLX4_EN_NUM_UP)
61 		return -EINVAL;
62 
63 	netdev_set_num_tc(dev, up);
64 
65 	/* Partition Tx queues evenly amongst UP's */
66 	for (i = 0; i < up; i++) {
67 		netdev_set_tc_queue(dev, i, priv->num_tx_rings_p_up, offset);
68 		offset += priv->num_tx_rings_p_up;
69 	}
70 
71 #ifdef CONFIG_MLX4_EN_DCB
72 	if (!mlx4_is_slave(priv->mdev->dev)) {
73 		if (up) {
74 			if (priv->dcbx_cap)
75 				priv->flags |= MLX4_EN_FLAG_DCB_ENABLED;
76 		} else {
77 			priv->flags &= ~MLX4_EN_FLAG_DCB_ENABLED;
78 			priv->cee_config.pfc_state = false;
79 		}
80 	}
81 #endif /* CONFIG_MLX4_EN_DCB */
82 
83 	return 0;
84 }
85 
86 static int __mlx4_en_setup_tc(struct net_device *dev, u32 handle, __be16 proto,
87 			      struct tc_to_netdev *tc)
88 {
89 	if (tc->type != TC_SETUP_MQPRIO)
90 		return -EINVAL;
91 
92 	return mlx4_en_setup_tc(dev, tc->tc);
93 }
94 
95 #ifdef CONFIG_RFS_ACCEL
96 
97 struct mlx4_en_filter {
98 	struct list_head next;
99 	struct work_struct work;
100 
101 	u8     ip_proto;
102 	__be32 src_ip;
103 	__be32 dst_ip;
104 	__be16 src_port;
105 	__be16 dst_port;
106 
107 	int rxq_index;
108 	struct mlx4_en_priv *priv;
109 	u32 flow_id;			/* RFS infrastructure id */
110 	int id;				/* mlx4_en driver id */
111 	u64 reg_id;			/* Flow steering API id */
112 	u8 activated;			/* Used to prevent expiry before filter
113 					 * is attached
114 					 */
115 	struct hlist_node filter_chain;
116 };
117 
118 static void mlx4_en_filter_rfs_expire(struct mlx4_en_priv *priv);
119 
120 static enum mlx4_net_trans_rule_id mlx4_ip_proto_to_trans_rule_id(u8 ip_proto)
121 {
122 	switch (ip_proto) {
123 	case IPPROTO_UDP:
124 		return MLX4_NET_TRANS_RULE_ID_UDP;
125 	case IPPROTO_TCP:
126 		return MLX4_NET_TRANS_RULE_ID_TCP;
127 	default:
128 		return MLX4_NET_TRANS_RULE_NUM;
129 	}
130 };
131 
132 /* Must not acquire state_lock, as its corresponding work_sync
133  * is done under it.
134  */
135 static void mlx4_en_filter_work(struct work_struct *work)
136 {
137 	struct mlx4_en_filter *filter = container_of(work,
138 						     struct mlx4_en_filter,
139 						     work);
140 	struct mlx4_en_priv *priv = filter->priv;
141 	struct mlx4_spec_list spec_tcp_udp = {
142 		.id = mlx4_ip_proto_to_trans_rule_id(filter->ip_proto),
143 		{
144 			.tcp_udp = {
145 				.dst_port = filter->dst_port,
146 				.dst_port_msk = (__force __be16)-1,
147 				.src_port = filter->src_port,
148 				.src_port_msk = (__force __be16)-1,
149 			},
150 		},
151 	};
152 	struct mlx4_spec_list spec_ip = {
153 		.id = MLX4_NET_TRANS_RULE_ID_IPV4,
154 		{
155 			.ipv4 = {
156 				.dst_ip = filter->dst_ip,
157 				.dst_ip_msk = (__force __be32)-1,
158 				.src_ip = filter->src_ip,
159 				.src_ip_msk = (__force __be32)-1,
160 			},
161 		},
162 	};
163 	struct mlx4_spec_list spec_eth = {
164 		.id = MLX4_NET_TRANS_RULE_ID_ETH,
165 	};
166 	struct mlx4_net_trans_rule rule = {
167 		.list = LIST_HEAD_INIT(rule.list),
168 		.queue_mode = MLX4_NET_TRANS_Q_LIFO,
169 		.exclusive = 1,
170 		.allow_loopback = 1,
171 		.promisc_mode = MLX4_FS_REGULAR,
172 		.port = priv->port,
173 		.priority = MLX4_DOMAIN_RFS,
174 	};
175 	int rc;
176 	__be64 mac_mask = cpu_to_be64(MLX4_MAC_MASK << 16);
177 
178 	if (spec_tcp_udp.id >= MLX4_NET_TRANS_RULE_NUM) {
179 		en_warn(priv, "RFS: ignoring unsupported ip protocol (%d)\n",
180 			filter->ip_proto);
181 		goto ignore;
182 	}
183 	list_add_tail(&spec_eth.list, &rule.list);
184 	list_add_tail(&spec_ip.list, &rule.list);
185 	list_add_tail(&spec_tcp_udp.list, &rule.list);
186 
187 	rule.qpn = priv->rss_map.qps[filter->rxq_index].qpn;
188 	memcpy(spec_eth.eth.dst_mac, priv->dev->dev_addr, ETH_ALEN);
189 	memcpy(spec_eth.eth.dst_mac_msk, &mac_mask, ETH_ALEN);
190 
191 	filter->activated = 0;
192 
193 	if (filter->reg_id) {
194 		rc = mlx4_flow_detach(priv->mdev->dev, filter->reg_id);
195 		if (rc && rc != -ENOENT)
196 			en_err(priv, "Error detaching flow. rc = %d\n", rc);
197 	}
198 
199 	rc = mlx4_flow_attach(priv->mdev->dev, &rule, &filter->reg_id);
200 	if (rc)
201 		en_err(priv, "Error attaching flow. err = %d\n", rc);
202 
203 ignore:
204 	mlx4_en_filter_rfs_expire(priv);
205 
206 	filter->activated = 1;
207 }
208 
209 static inline struct hlist_head *
210 filter_hash_bucket(struct mlx4_en_priv *priv, __be32 src_ip, __be32 dst_ip,
211 		   __be16 src_port, __be16 dst_port)
212 {
213 	unsigned long l;
214 	int bucket_idx;
215 
216 	l = (__force unsigned long)src_port |
217 	    ((__force unsigned long)dst_port << 2);
218 	l ^= (__force unsigned long)(src_ip ^ dst_ip);
219 
220 	bucket_idx = hash_long(l, MLX4_EN_FILTER_HASH_SHIFT);
221 
222 	return &priv->filter_hash[bucket_idx];
223 }
224 
225 static struct mlx4_en_filter *
226 mlx4_en_filter_alloc(struct mlx4_en_priv *priv, int rxq_index, __be32 src_ip,
227 		     __be32 dst_ip, u8 ip_proto, __be16 src_port,
228 		     __be16 dst_port, u32 flow_id)
229 {
230 	struct mlx4_en_filter *filter = NULL;
231 
232 	filter = kzalloc(sizeof(struct mlx4_en_filter), GFP_ATOMIC);
233 	if (!filter)
234 		return NULL;
235 
236 	filter->priv = priv;
237 	filter->rxq_index = rxq_index;
238 	INIT_WORK(&filter->work, mlx4_en_filter_work);
239 
240 	filter->src_ip = src_ip;
241 	filter->dst_ip = dst_ip;
242 	filter->ip_proto = ip_proto;
243 	filter->src_port = src_port;
244 	filter->dst_port = dst_port;
245 
246 	filter->flow_id = flow_id;
247 
248 	filter->id = priv->last_filter_id++ % RPS_NO_FILTER;
249 
250 	list_add_tail(&filter->next, &priv->filters);
251 	hlist_add_head(&filter->filter_chain,
252 		       filter_hash_bucket(priv, src_ip, dst_ip, src_port,
253 					  dst_port));
254 
255 	return filter;
256 }
257 
258 static void mlx4_en_filter_free(struct mlx4_en_filter *filter)
259 {
260 	struct mlx4_en_priv *priv = filter->priv;
261 	int rc;
262 
263 	list_del(&filter->next);
264 
265 	rc = mlx4_flow_detach(priv->mdev->dev, filter->reg_id);
266 	if (rc && rc != -ENOENT)
267 		en_err(priv, "Error detaching flow. rc = %d\n", rc);
268 
269 	kfree(filter);
270 }
271 
272 static inline struct mlx4_en_filter *
273 mlx4_en_filter_find(struct mlx4_en_priv *priv, __be32 src_ip, __be32 dst_ip,
274 		    u8 ip_proto, __be16 src_port, __be16 dst_port)
275 {
276 	struct mlx4_en_filter *filter;
277 	struct mlx4_en_filter *ret = NULL;
278 
279 	hlist_for_each_entry(filter,
280 			     filter_hash_bucket(priv, src_ip, dst_ip,
281 						src_port, dst_port),
282 			     filter_chain) {
283 		if (filter->src_ip == src_ip &&
284 		    filter->dst_ip == dst_ip &&
285 		    filter->ip_proto == ip_proto &&
286 		    filter->src_port == src_port &&
287 		    filter->dst_port == dst_port) {
288 			ret = filter;
289 			break;
290 		}
291 	}
292 
293 	return ret;
294 }
295 
296 static int
297 mlx4_en_filter_rfs(struct net_device *net_dev, const struct sk_buff *skb,
298 		   u16 rxq_index, u32 flow_id)
299 {
300 	struct mlx4_en_priv *priv = netdev_priv(net_dev);
301 	struct mlx4_en_filter *filter;
302 	const struct iphdr *ip;
303 	const __be16 *ports;
304 	u8 ip_proto;
305 	__be32 src_ip;
306 	__be32 dst_ip;
307 	__be16 src_port;
308 	__be16 dst_port;
309 	int nhoff = skb_network_offset(skb);
310 	int ret = 0;
311 
312 	if (skb->protocol != htons(ETH_P_IP))
313 		return -EPROTONOSUPPORT;
314 
315 	ip = (const struct iphdr *)(skb->data + nhoff);
316 	if (ip_is_fragment(ip))
317 		return -EPROTONOSUPPORT;
318 
319 	if ((ip->protocol != IPPROTO_TCP) && (ip->protocol != IPPROTO_UDP))
320 		return -EPROTONOSUPPORT;
321 	ports = (const __be16 *)(skb->data + nhoff + 4 * ip->ihl);
322 
323 	ip_proto = ip->protocol;
324 	src_ip = ip->saddr;
325 	dst_ip = ip->daddr;
326 	src_port = ports[0];
327 	dst_port = ports[1];
328 
329 	spin_lock_bh(&priv->filters_lock);
330 	filter = mlx4_en_filter_find(priv, src_ip, dst_ip, ip_proto,
331 				     src_port, dst_port);
332 	if (filter) {
333 		if (filter->rxq_index == rxq_index)
334 			goto out;
335 
336 		filter->rxq_index = rxq_index;
337 	} else {
338 		filter = mlx4_en_filter_alloc(priv, rxq_index,
339 					      src_ip, dst_ip, ip_proto,
340 					      src_port, dst_port, flow_id);
341 		if (!filter) {
342 			ret = -ENOMEM;
343 			goto err;
344 		}
345 	}
346 
347 	queue_work(priv->mdev->workqueue, &filter->work);
348 
349 out:
350 	ret = filter->id;
351 err:
352 	spin_unlock_bh(&priv->filters_lock);
353 
354 	return ret;
355 }
356 
357 void mlx4_en_cleanup_filters(struct mlx4_en_priv *priv)
358 {
359 	struct mlx4_en_filter *filter, *tmp;
360 	LIST_HEAD(del_list);
361 
362 	spin_lock_bh(&priv->filters_lock);
363 	list_for_each_entry_safe(filter, tmp, &priv->filters, next) {
364 		list_move(&filter->next, &del_list);
365 		hlist_del(&filter->filter_chain);
366 	}
367 	spin_unlock_bh(&priv->filters_lock);
368 
369 	list_for_each_entry_safe(filter, tmp, &del_list, next) {
370 		cancel_work_sync(&filter->work);
371 		mlx4_en_filter_free(filter);
372 	}
373 }
374 
375 static void mlx4_en_filter_rfs_expire(struct mlx4_en_priv *priv)
376 {
377 	struct mlx4_en_filter *filter = NULL, *tmp, *last_filter = NULL;
378 	LIST_HEAD(del_list);
379 	int i = 0;
380 
381 	spin_lock_bh(&priv->filters_lock);
382 	list_for_each_entry_safe(filter, tmp, &priv->filters, next) {
383 		if (i > MLX4_EN_FILTER_EXPIRY_QUOTA)
384 			break;
385 
386 		if (filter->activated &&
387 		    !work_pending(&filter->work) &&
388 		    rps_may_expire_flow(priv->dev,
389 					filter->rxq_index, filter->flow_id,
390 					filter->id)) {
391 			list_move(&filter->next, &del_list);
392 			hlist_del(&filter->filter_chain);
393 		} else
394 			last_filter = filter;
395 
396 		i++;
397 	}
398 
399 	if (last_filter && (&last_filter->next != priv->filters.next))
400 		list_move(&priv->filters, &last_filter->next);
401 
402 	spin_unlock_bh(&priv->filters_lock);
403 
404 	list_for_each_entry_safe(filter, tmp, &del_list, next)
405 		mlx4_en_filter_free(filter);
406 }
407 #endif
408 
409 static int mlx4_en_vlan_rx_add_vid(struct net_device *dev,
410 				   __be16 proto, u16 vid)
411 {
412 	struct mlx4_en_priv *priv = netdev_priv(dev);
413 	struct mlx4_en_dev *mdev = priv->mdev;
414 	int err;
415 	int idx;
416 
417 	en_dbg(HW, priv, "adding VLAN:%d\n", vid);
418 
419 	set_bit(vid, priv->active_vlans);
420 
421 	/* Add VID to port VLAN filter */
422 	mutex_lock(&mdev->state_lock);
423 	if (mdev->device_up && priv->port_up) {
424 		err = mlx4_SET_VLAN_FLTR(mdev->dev, priv);
425 		if (err) {
426 			en_err(priv, "Failed configuring VLAN filter\n");
427 			goto out;
428 		}
429 	}
430 	err = mlx4_register_vlan(mdev->dev, priv->port, vid, &idx);
431 	if (err)
432 		en_dbg(HW, priv, "Failed adding vlan %d\n", vid);
433 
434 out:
435 	mutex_unlock(&mdev->state_lock);
436 	return err;
437 }
438 
439 static int mlx4_en_vlan_rx_kill_vid(struct net_device *dev,
440 				    __be16 proto, u16 vid)
441 {
442 	struct mlx4_en_priv *priv = netdev_priv(dev);
443 	struct mlx4_en_dev *mdev = priv->mdev;
444 	int err = 0;
445 
446 	en_dbg(HW, priv, "Killing VID:%d\n", vid);
447 
448 	clear_bit(vid, priv->active_vlans);
449 
450 	/* Remove VID from port VLAN filter */
451 	mutex_lock(&mdev->state_lock);
452 	mlx4_unregister_vlan(mdev->dev, priv->port, vid);
453 
454 	if (mdev->device_up && priv->port_up) {
455 		err = mlx4_SET_VLAN_FLTR(mdev->dev, priv);
456 		if (err)
457 			en_err(priv, "Failed configuring VLAN filter\n");
458 	}
459 	mutex_unlock(&mdev->state_lock);
460 
461 	return err;
462 }
463 
464 static void mlx4_en_u64_to_mac(unsigned char dst_mac[ETH_ALEN + 2], u64 src_mac)
465 {
466 	int i;
467 	for (i = ETH_ALEN - 1; i >= 0; --i) {
468 		dst_mac[i] = src_mac & 0xff;
469 		src_mac >>= 8;
470 	}
471 	memset(&dst_mac[ETH_ALEN], 0, 2);
472 }
473 
474 
475 static int mlx4_en_tunnel_steer_add(struct mlx4_en_priv *priv, unsigned char *addr,
476 				    int qpn, u64 *reg_id)
477 {
478 	int err;
479 
480 	if (priv->mdev->dev->caps.tunnel_offload_mode != MLX4_TUNNEL_OFFLOAD_MODE_VXLAN ||
481 	    priv->mdev->dev->caps.dmfs_high_steer_mode == MLX4_STEERING_DMFS_A0_STATIC)
482 		return 0; /* do nothing */
483 
484 	err = mlx4_tunnel_steer_add(priv->mdev->dev, addr, priv->port, qpn,
485 				    MLX4_DOMAIN_NIC, reg_id);
486 	if (err) {
487 		en_err(priv, "failed to add vxlan steering rule, err %d\n", err);
488 		return err;
489 	}
490 	en_dbg(DRV, priv, "added vxlan steering rule, mac %pM reg_id %llx\n", addr, *reg_id);
491 	return 0;
492 }
493 
494 
495 static int mlx4_en_uc_steer_add(struct mlx4_en_priv *priv,
496 				unsigned char *mac, int *qpn, u64 *reg_id)
497 {
498 	struct mlx4_en_dev *mdev = priv->mdev;
499 	struct mlx4_dev *dev = mdev->dev;
500 	int err;
501 
502 	switch (dev->caps.steering_mode) {
503 	case MLX4_STEERING_MODE_B0: {
504 		struct mlx4_qp qp;
505 		u8 gid[16] = {0};
506 
507 		qp.qpn = *qpn;
508 		memcpy(&gid[10], mac, ETH_ALEN);
509 		gid[5] = priv->port;
510 
511 		err = mlx4_unicast_attach(dev, &qp, gid, 0, MLX4_PROT_ETH);
512 		break;
513 	}
514 	case MLX4_STEERING_MODE_DEVICE_MANAGED: {
515 		struct mlx4_spec_list spec_eth = { {NULL} };
516 		__be64 mac_mask = cpu_to_be64(MLX4_MAC_MASK << 16);
517 
518 		struct mlx4_net_trans_rule rule = {
519 			.queue_mode = MLX4_NET_TRANS_Q_FIFO,
520 			.exclusive = 0,
521 			.allow_loopback = 1,
522 			.promisc_mode = MLX4_FS_REGULAR,
523 			.priority = MLX4_DOMAIN_NIC,
524 		};
525 
526 		rule.port = priv->port;
527 		rule.qpn = *qpn;
528 		INIT_LIST_HEAD(&rule.list);
529 
530 		spec_eth.id = MLX4_NET_TRANS_RULE_ID_ETH;
531 		memcpy(spec_eth.eth.dst_mac, mac, ETH_ALEN);
532 		memcpy(spec_eth.eth.dst_mac_msk, &mac_mask, ETH_ALEN);
533 		list_add_tail(&spec_eth.list, &rule.list);
534 
535 		err = mlx4_flow_attach(dev, &rule, reg_id);
536 		break;
537 	}
538 	default:
539 		return -EINVAL;
540 	}
541 	if (err)
542 		en_warn(priv, "Failed Attaching Unicast\n");
543 
544 	return err;
545 }
546 
547 static void mlx4_en_uc_steer_release(struct mlx4_en_priv *priv,
548 				     unsigned char *mac, int qpn, u64 reg_id)
549 {
550 	struct mlx4_en_dev *mdev = priv->mdev;
551 	struct mlx4_dev *dev = mdev->dev;
552 
553 	switch (dev->caps.steering_mode) {
554 	case MLX4_STEERING_MODE_B0: {
555 		struct mlx4_qp qp;
556 		u8 gid[16] = {0};
557 
558 		qp.qpn = qpn;
559 		memcpy(&gid[10], mac, ETH_ALEN);
560 		gid[5] = priv->port;
561 
562 		mlx4_unicast_detach(dev, &qp, gid, MLX4_PROT_ETH);
563 		break;
564 	}
565 	case MLX4_STEERING_MODE_DEVICE_MANAGED: {
566 		mlx4_flow_detach(dev, reg_id);
567 		break;
568 	}
569 	default:
570 		en_err(priv, "Invalid steering mode.\n");
571 	}
572 }
573 
574 static int mlx4_en_get_qp(struct mlx4_en_priv *priv)
575 {
576 	struct mlx4_en_dev *mdev = priv->mdev;
577 	struct mlx4_dev *dev = mdev->dev;
578 	int index = 0;
579 	int err = 0;
580 	int *qpn = &priv->base_qpn;
581 	u64 mac = mlx4_mac_to_u64(priv->dev->dev_addr);
582 
583 	en_dbg(DRV, priv, "Registering MAC: %pM for adding\n",
584 	       priv->dev->dev_addr);
585 	index = mlx4_register_mac(dev, priv->port, mac);
586 	if (index < 0) {
587 		err = index;
588 		en_err(priv, "Failed adding MAC: %pM\n",
589 		       priv->dev->dev_addr);
590 		return err;
591 	}
592 
593 	if (dev->caps.steering_mode == MLX4_STEERING_MODE_A0) {
594 		int base_qpn = mlx4_get_base_qpn(dev, priv->port);
595 		*qpn = base_qpn + index;
596 		return 0;
597 	}
598 
599 	err = mlx4_qp_reserve_range(dev, 1, 1, qpn, MLX4_RESERVE_A0_QP);
600 	en_dbg(DRV, priv, "Reserved qp %d\n", *qpn);
601 	if (err) {
602 		en_err(priv, "Failed to reserve qp for mac registration\n");
603 		mlx4_unregister_mac(dev, priv->port, mac);
604 		return err;
605 	}
606 
607 	return 0;
608 }
609 
610 static void mlx4_en_put_qp(struct mlx4_en_priv *priv)
611 {
612 	struct mlx4_en_dev *mdev = priv->mdev;
613 	struct mlx4_dev *dev = mdev->dev;
614 	int qpn = priv->base_qpn;
615 
616 	if (dev->caps.steering_mode == MLX4_STEERING_MODE_A0) {
617 		u64 mac = mlx4_mac_to_u64(priv->dev->dev_addr);
618 		en_dbg(DRV, priv, "Registering MAC: %pM for deleting\n",
619 		       priv->dev->dev_addr);
620 		mlx4_unregister_mac(dev, priv->port, mac);
621 	} else {
622 		en_dbg(DRV, priv, "Releasing qp: port %d, qpn %d\n",
623 		       priv->port, qpn);
624 		mlx4_qp_release_range(dev, qpn, 1);
625 		priv->flags &= ~MLX4_EN_FLAG_FORCE_PROMISC;
626 	}
627 }
628 
629 static int mlx4_en_replace_mac(struct mlx4_en_priv *priv, int qpn,
630 			       unsigned char *new_mac, unsigned char *prev_mac)
631 {
632 	struct mlx4_en_dev *mdev = priv->mdev;
633 	struct mlx4_dev *dev = mdev->dev;
634 	int err = 0;
635 	u64 new_mac_u64 = mlx4_mac_to_u64(new_mac);
636 
637 	if (dev->caps.steering_mode != MLX4_STEERING_MODE_A0) {
638 		struct hlist_head *bucket;
639 		unsigned int mac_hash;
640 		struct mlx4_mac_entry *entry;
641 		struct hlist_node *tmp;
642 		u64 prev_mac_u64 = mlx4_mac_to_u64(prev_mac);
643 
644 		bucket = &priv->mac_hash[prev_mac[MLX4_EN_MAC_HASH_IDX]];
645 		hlist_for_each_entry_safe(entry, tmp, bucket, hlist) {
646 			if (ether_addr_equal_64bits(entry->mac, prev_mac)) {
647 				mlx4_en_uc_steer_release(priv, entry->mac,
648 							 qpn, entry->reg_id);
649 				mlx4_unregister_mac(dev, priv->port,
650 						    prev_mac_u64);
651 				hlist_del_rcu(&entry->hlist);
652 				synchronize_rcu();
653 				memcpy(entry->mac, new_mac, ETH_ALEN);
654 				entry->reg_id = 0;
655 				mac_hash = new_mac[MLX4_EN_MAC_HASH_IDX];
656 				hlist_add_head_rcu(&entry->hlist,
657 						   &priv->mac_hash[mac_hash]);
658 				mlx4_register_mac(dev, priv->port, new_mac_u64);
659 				err = mlx4_en_uc_steer_add(priv, new_mac,
660 							   &qpn,
661 							   &entry->reg_id);
662 				if (err)
663 					return err;
664 				if (priv->tunnel_reg_id) {
665 					mlx4_flow_detach(priv->mdev->dev, priv->tunnel_reg_id);
666 					priv->tunnel_reg_id = 0;
667 				}
668 				err = mlx4_en_tunnel_steer_add(priv, new_mac, qpn,
669 							       &priv->tunnel_reg_id);
670 				return err;
671 			}
672 		}
673 		return -EINVAL;
674 	}
675 
676 	return __mlx4_replace_mac(dev, priv->port, qpn, new_mac_u64);
677 }
678 
679 static int mlx4_en_do_set_mac(struct mlx4_en_priv *priv,
680 			      unsigned char new_mac[ETH_ALEN + 2])
681 {
682 	int err = 0;
683 
684 	if (priv->port_up) {
685 		/* Remove old MAC and insert the new one */
686 		err = mlx4_en_replace_mac(priv, priv->base_qpn,
687 					  new_mac, priv->current_mac);
688 		if (err)
689 			en_err(priv, "Failed changing HW MAC address\n");
690 	} else
691 		en_dbg(HW, priv, "Port is down while registering mac, exiting...\n");
692 
693 	if (!err)
694 		memcpy(priv->current_mac, new_mac, sizeof(priv->current_mac));
695 
696 	return err;
697 }
698 
699 static int mlx4_en_set_mac(struct net_device *dev, void *addr)
700 {
701 	struct mlx4_en_priv *priv = netdev_priv(dev);
702 	struct mlx4_en_dev *mdev = priv->mdev;
703 	struct sockaddr *saddr = addr;
704 	unsigned char new_mac[ETH_ALEN + 2];
705 	int err;
706 
707 	if (!is_valid_ether_addr(saddr->sa_data))
708 		return -EADDRNOTAVAIL;
709 
710 	mutex_lock(&mdev->state_lock);
711 	memcpy(new_mac, saddr->sa_data, ETH_ALEN);
712 	err = mlx4_en_do_set_mac(priv, new_mac);
713 	if (!err)
714 		memcpy(dev->dev_addr, saddr->sa_data, ETH_ALEN);
715 	mutex_unlock(&mdev->state_lock);
716 
717 	return err;
718 }
719 
720 static void mlx4_en_clear_list(struct net_device *dev)
721 {
722 	struct mlx4_en_priv *priv = netdev_priv(dev);
723 	struct mlx4_en_mc_list *tmp, *mc_to_del;
724 
725 	list_for_each_entry_safe(mc_to_del, tmp, &priv->mc_list, list) {
726 		list_del(&mc_to_del->list);
727 		kfree(mc_to_del);
728 	}
729 }
730 
731 static void mlx4_en_cache_mclist(struct net_device *dev)
732 {
733 	struct mlx4_en_priv *priv = netdev_priv(dev);
734 	struct netdev_hw_addr *ha;
735 	struct mlx4_en_mc_list *tmp;
736 
737 	mlx4_en_clear_list(dev);
738 	netdev_for_each_mc_addr(ha, dev) {
739 		tmp = kzalloc(sizeof(struct mlx4_en_mc_list), GFP_ATOMIC);
740 		if (!tmp) {
741 			mlx4_en_clear_list(dev);
742 			return;
743 		}
744 		memcpy(tmp->addr, ha->addr, ETH_ALEN);
745 		list_add_tail(&tmp->list, &priv->mc_list);
746 	}
747 }
748 
749 static void update_mclist_flags(struct mlx4_en_priv *priv,
750 				struct list_head *dst,
751 				struct list_head *src)
752 {
753 	struct mlx4_en_mc_list *dst_tmp, *src_tmp, *new_mc;
754 	bool found;
755 
756 	/* Find all the entries that should be removed from dst,
757 	 * These are the entries that are not found in src
758 	 */
759 	list_for_each_entry(dst_tmp, dst, list) {
760 		found = false;
761 		list_for_each_entry(src_tmp, src, list) {
762 			if (ether_addr_equal(dst_tmp->addr, src_tmp->addr)) {
763 				found = true;
764 				break;
765 			}
766 		}
767 		if (!found)
768 			dst_tmp->action = MCLIST_REM;
769 	}
770 
771 	/* Add entries that exist in src but not in dst
772 	 * mark them as need to add
773 	 */
774 	list_for_each_entry(src_tmp, src, list) {
775 		found = false;
776 		list_for_each_entry(dst_tmp, dst, list) {
777 			if (ether_addr_equal(dst_tmp->addr, src_tmp->addr)) {
778 				dst_tmp->action = MCLIST_NONE;
779 				found = true;
780 				break;
781 			}
782 		}
783 		if (!found) {
784 			new_mc = kmemdup(src_tmp,
785 					 sizeof(struct mlx4_en_mc_list),
786 					 GFP_KERNEL);
787 			if (!new_mc)
788 				return;
789 
790 			new_mc->action = MCLIST_ADD;
791 			list_add_tail(&new_mc->list, dst);
792 		}
793 	}
794 }
795 
796 static void mlx4_en_set_rx_mode(struct net_device *dev)
797 {
798 	struct mlx4_en_priv *priv = netdev_priv(dev);
799 
800 	if (!priv->port_up)
801 		return;
802 
803 	queue_work(priv->mdev->workqueue, &priv->rx_mode_task);
804 }
805 
806 static void mlx4_en_set_promisc_mode(struct mlx4_en_priv *priv,
807 				     struct mlx4_en_dev *mdev)
808 {
809 	int err = 0;
810 
811 	if (!(priv->flags & MLX4_EN_FLAG_PROMISC)) {
812 		if (netif_msg_rx_status(priv))
813 			en_warn(priv, "Entering promiscuous mode\n");
814 		priv->flags |= MLX4_EN_FLAG_PROMISC;
815 
816 		/* Enable promiscouos mode */
817 		switch (mdev->dev->caps.steering_mode) {
818 		case MLX4_STEERING_MODE_DEVICE_MANAGED:
819 			err = mlx4_flow_steer_promisc_add(mdev->dev,
820 							  priv->port,
821 							  priv->base_qpn,
822 							  MLX4_FS_ALL_DEFAULT);
823 			if (err)
824 				en_err(priv, "Failed enabling promiscuous mode\n");
825 			priv->flags |= MLX4_EN_FLAG_MC_PROMISC;
826 			break;
827 
828 		case MLX4_STEERING_MODE_B0:
829 			err = mlx4_unicast_promisc_add(mdev->dev,
830 						       priv->base_qpn,
831 						       priv->port);
832 			if (err)
833 				en_err(priv, "Failed enabling unicast promiscuous mode\n");
834 
835 			/* Add the default qp number as multicast
836 			 * promisc
837 			 */
838 			if (!(priv->flags & MLX4_EN_FLAG_MC_PROMISC)) {
839 				err = mlx4_multicast_promisc_add(mdev->dev,
840 								 priv->base_qpn,
841 								 priv->port);
842 				if (err)
843 					en_err(priv, "Failed enabling multicast promiscuous mode\n");
844 				priv->flags |= MLX4_EN_FLAG_MC_PROMISC;
845 			}
846 			break;
847 
848 		case MLX4_STEERING_MODE_A0:
849 			err = mlx4_SET_PORT_qpn_calc(mdev->dev,
850 						     priv->port,
851 						     priv->base_qpn,
852 						     1);
853 			if (err)
854 				en_err(priv, "Failed enabling promiscuous mode\n");
855 			break;
856 		}
857 
858 		/* Disable port multicast filter (unconditionally) */
859 		err = mlx4_SET_MCAST_FLTR(mdev->dev, priv->port, 0,
860 					  0, MLX4_MCAST_DISABLE);
861 		if (err)
862 			en_err(priv, "Failed disabling multicast filter\n");
863 	}
864 }
865 
866 static void mlx4_en_clear_promisc_mode(struct mlx4_en_priv *priv,
867 				       struct mlx4_en_dev *mdev)
868 {
869 	int err = 0;
870 
871 	if (netif_msg_rx_status(priv))
872 		en_warn(priv, "Leaving promiscuous mode\n");
873 	priv->flags &= ~MLX4_EN_FLAG_PROMISC;
874 
875 	/* Disable promiscouos mode */
876 	switch (mdev->dev->caps.steering_mode) {
877 	case MLX4_STEERING_MODE_DEVICE_MANAGED:
878 		err = mlx4_flow_steer_promisc_remove(mdev->dev,
879 						     priv->port,
880 						     MLX4_FS_ALL_DEFAULT);
881 		if (err)
882 			en_err(priv, "Failed disabling 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_remove(mdev->dev,
888 						  priv->base_qpn,
889 						  priv->port);
890 		if (err)
891 			en_err(priv, "Failed disabling unicast promiscuous mode\n");
892 		/* Disable Multicast promisc */
893 		if (priv->flags & MLX4_EN_FLAG_MC_PROMISC) {
894 			err = mlx4_multicast_promisc_remove(mdev->dev,
895 							    priv->base_qpn,
896 							    priv->port);
897 			if (err)
898 				en_err(priv, "Failed disabling multicast promiscuous mode\n");
899 			priv->flags &= ~MLX4_EN_FLAG_MC_PROMISC;
900 		}
901 		break;
902 
903 	case MLX4_STEERING_MODE_A0:
904 		err = mlx4_SET_PORT_qpn_calc(mdev->dev,
905 					     priv->port,
906 					     priv->base_qpn, 0);
907 		if (err)
908 			en_err(priv, "Failed disabling promiscuous mode\n");
909 		break;
910 	}
911 }
912 
913 static void mlx4_en_do_multicast(struct mlx4_en_priv *priv,
914 				 struct net_device *dev,
915 				 struct mlx4_en_dev *mdev)
916 {
917 	struct mlx4_en_mc_list *mclist, *tmp;
918 	u64 mcast_addr = 0;
919 	u8 mc_list[16] = {0};
920 	int err = 0;
921 
922 	/* Enable/disable the multicast filter according to IFF_ALLMULTI */
923 	if (dev->flags & IFF_ALLMULTI) {
924 		err = mlx4_SET_MCAST_FLTR(mdev->dev, priv->port, 0,
925 					  0, MLX4_MCAST_DISABLE);
926 		if (err)
927 			en_err(priv, "Failed disabling multicast filter\n");
928 
929 		/* Add the default qp number as multicast promisc */
930 		if (!(priv->flags & MLX4_EN_FLAG_MC_PROMISC)) {
931 			switch (mdev->dev->caps.steering_mode) {
932 			case MLX4_STEERING_MODE_DEVICE_MANAGED:
933 				err = mlx4_flow_steer_promisc_add(mdev->dev,
934 								  priv->port,
935 								  priv->base_qpn,
936 								  MLX4_FS_MC_DEFAULT);
937 				break;
938 
939 			case MLX4_STEERING_MODE_B0:
940 				err = mlx4_multicast_promisc_add(mdev->dev,
941 								 priv->base_qpn,
942 								 priv->port);
943 				break;
944 
945 			case MLX4_STEERING_MODE_A0:
946 				break;
947 			}
948 			if (err)
949 				en_err(priv, "Failed entering multicast promisc mode\n");
950 			priv->flags |= MLX4_EN_FLAG_MC_PROMISC;
951 		}
952 	} else {
953 		/* Disable Multicast promisc */
954 		if (priv->flags & MLX4_EN_FLAG_MC_PROMISC) {
955 			switch (mdev->dev->caps.steering_mode) {
956 			case MLX4_STEERING_MODE_DEVICE_MANAGED:
957 				err = mlx4_flow_steer_promisc_remove(mdev->dev,
958 								     priv->port,
959 								     MLX4_FS_MC_DEFAULT);
960 				break;
961 
962 			case MLX4_STEERING_MODE_B0:
963 				err = mlx4_multicast_promisc_remove(mdev->dev,
964 								    priv->base_qpn,
965 								    priv->port);
966 				break;
967 
968 			case MLX4_STEERING_MODE_A0:
969 				break;
970 			}
971 			if (err)
972 				en_err(priv, "Failed disabling multicast promiscuous mode\n");
973 			priv->flags &= ~MLX4_EN_FLAG_MC_PROMISC;
974 		}
975 
976 		err = mlx4_SET_MCAST_FLTR(mdev->dev, priv->port, 0,
977 					  0, MLX4_MCAST_DISABLE);
978 		if (err)
979 			en_err(priv, "Failed disabling multicast filter\n");
980 
981 		/* Flush mcast filter and init it with broadcast address */
982 		mlx4_SET_MCAST_FLTR(mdev->dev, priv->port, ETH_BCAST,
983 				    1, MLX4_MCAST_CONFIG);
984 
985 		/* Update multicast list - we cache all addresses so they won't
986 		 * change while HW is updated holding the command semaphor */
987 		netif_addr_lock_bh(dev);
988 		mlx4_en_cache_mclist(dev);
989 		netif_addr_unlock_bh(dev);
990 		list_for_each_entry(mclist, &priv->mc_list, list) {
991 			mcast_addr = mlx4_mac_to_u64(mclist->addr);
992 			mlx4_SET_MCAST_FLTR(mdev->dev, priv->port,
993 					    mcast_addr, 0, MLX4_MCAST_CONFIG);
994 		}
995 		err = mlx4_SET_MCAST_FLTR(mdev->dev, priv->port, 0,
996 					  0, MLX4_MCAST_ENABLE);
997 		if (err)
998 			en_err(priv, "Failed enabling multicast filter\n");
999 
1000 		update_mclist_flags(priv, &priv->curr_list, &priv->mc_list);
1001 		list_for_each_entry_safe(mclist, tmp, &priv->curr_list, list) {
1002 			if (mclist->action == MCLIST_REM) {
1003 				/* detach this address and delete from list */
1004 				memcpy(&mc_list[10], mclist->addr, ETH_ALEN);
1005 				mc_list[5] = priv->port;
1006 				err = mlx4_multicast_detach(mdev->dev,
1007 							    &priv->rss_map.indir_qp,
1008 							    mc_list,
1009 							    MLX4_PROT_ETH,
1010 							    mclist->reg_id);
1011 				if (err)
1012 					en_err(priv, "Fail to detach multicast address\n");
1013 
1014 				if (mclist->tunnel_reg_id) {
1015 					err = mlx4_flow_detach(priv->mdev->dev, mclist->tunnel_reg_id);
1016 					if (err)
1017 						en_err(priv, "Failed to detach multicast address\n");
1018 				}
1019 
1020 				/* remove from list */
1021 				list_del(&mclist->list);
1022 				kfree(mclist);
1023 			} else if (mclist->action == MCLIST_ADD) {
1024 				/* attach the address */
1025 				memcpy(&mc_list[10], mclist->addr, ETH_ALEN);
1026 				/* needed for B0 steering support */
1027 				mc_list[5] = priv->port;
1028 				err = mlx4_multicast_attach(mdev->dev,
1029 							    &priv->rss_map.indir_qp,
1030 							    mc_list,
1031 							    priv->port, 0,
1032 							    MLX4_PROT_ETH,
1033 							    &mclist->reg_id);
1034 				if (err)
1035 					en_err(priv, "Fail to attach multicast address\n");
1036 
1037 				err = mlx4_en_tunnel_steer_add(priv, &mc_list[10], priv->base_qpn,
1038 							       &mclist->tunnel_reg_id);
1039 				if (err)
1040 					en_err(priv, "Failed to attach multicast address\n");
1041 			}
1042 		}
1043 	}
1044 }
1045 
1046 static void mlx4_en_do_uc_filter(struct mlx4_en_priv *priv,
1047 				 struct net_device *dev,
1048 				 struct mlx4_en_dev *mdev)
1049 {
1050 	struct netdev_hw_addr *ha;
1051 	struct mlx4_mac_entry *entry;
1052 	struct hlist_node *tmp;
1053 	bool found;
1054 	u64 mac;
1055 	int err = 0;
1056 	struct hlist_head *bucket;
1057 	unsigned int i;
1058 	int removed = 0;
1059 	u32 prev_flags;
1060 
1061 	/* Note that we do not need to protect our mac_hash traversal with rcu,
1062 	 * since all modification code is protected by mdev->state_lock
1063 	 */
1064 
1065 	/* find what to remove */
1066 	for (i = 0; i < MLX4_EN_MAC_HASH_SIZE; ++i) {
1067 		bucket = &priv->mac_hash[i];
1068 		hlist_for_each_entry_safe(entry, tmp, bucket, hlist) {
1069 			found = false;
1070 			netdev_for_each_uc_addr(ha, dev) {
1071 				if (ether_addr_equal_64bits(entry->mac,
1072 							    ha->addr)) {
1073 					found = true;
1074 					break;
1075 				}
1076 			}
1077 
1078 			/* MAC address of the port is not in uc list */
1079 			if (ether_addr_equal_64bits(entry->mac,
1080 						    priv->current_mac))
1081 				found = true;
1082 
1083 			if (!found) {
1084 				mac = mlx4_mac_to_u64(entry->mac);
1085 				mlx4_en_uc_steer_release(priv, entry->mac,
1086 							 priv->base_qpn,
1087 							 entry->reg_id);
1088 				mlx4_unregister_mac(mdev->dev, priv->port, mac);
1089 
1090 				hlist_del_rcu(&entry->hlist);
1091 				kfree_rcu(entry, rcu);
1092 				en_dbg(DRV, priv, "Removed MAC %pM on port:%d\n",
1093 				       entry->mac, priv->port);
1094 				++removed;
1095 			}
1096 		}
1097 	}
1098 
1099 	/* if we didn't remove anything, there is no use in trying to add
1100 	 * again once we are in a forced promisc mode state
1101 	 */
1102 	if ((priv->flags & MLX4_EN_FLAG_FORCE_PROMISC) && 0 == removed)
1103 		return;
1104 
1105 	prev_flags = priv->flags;
1106 	priv->flags &= ~MLX4_EN_FLAG_FORCE_PROMISC;
1107 
1108 	/* find what to add */
1109 	netdev_for_each_uc_addr(ha, dev) {
1110 		found = false;
1111 		bucket = &priv->mac_hash[ha->addr[MLX4_EN_MAC_HASH_IDX]];
1112 		hlist_for_each_entry(entry, bucket, hlist) {
1113 			if (ether_addr_equal_64bits(entry->mac, ha->addr)) {
1114 				found = true;
1115 				break;
1116 			}
1117 		}
1118 
1119 		if (!found) {
1120 			entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1121 			if (!entry) {
1122 				en_err(priv, "Failed adding MAC %pM on port:%d (out of memory)\n",
1123 				       ha->addr, priv->port);
1124 				priv->flags |= MLX4_EN_FLAG_FORCE_PROMISC;
1125 				break;
1126 			}
1127 			mac = mlx4_mac_to_u64(ha->addr);
1128 			memcpy(entry->mac, ha->addr, ETH_ALEN);
1129 			err = mlx4_register_mac(mdev->dev, priv->port, mac);
1130 			if (err < 0) {
1131 				en_err(priv, "Failed registering MAC %pM on port %d: %d\n",
1132 				       ha->addr, priv->port, err);
1133 				kfree(entry);
1134 				priv->flags |= MLX4_EN_FLAG_FORCE_PROMISC;
1135 				break;
1136 			}
1137 			err = mlx4_en_uc_steer_add(priv, ha->addr,
1138 						   &priv->base_qpn,
1139 						   &entry->reg_id);
1140 			if (err) {
1141 				en_err(priv, "Failed adding MAC %pM on port %d: %d\n",
1142 				       ha->addr, priv->port, err);
1143 				mlx4_unregister_mac(mdev->dev, priv->port, mac);
1144 				kfree(entry);
1145 				priv->flags |= MLX4_EN_FLAG_FORCE_PROMISC;
1146 				break;
1147 			} else {
1148 				unsigned int mac_hash;
1149 				en_dbg(DRV, priv, "Added MAC %pM on port:%d\n",
1150 				       ha->addr, priv->port);
1151 				mac_hash = ha->addr[MLX4_EN_MAC_HASH_IDX];
1152 				bucket = &priv->mac_hash[mac_hash];
1153 				hlist_add_head_rcu(&entry->hlist, bucket);
1154 			}
1155 		}
1156 	}
1157 
1158 	if (priv->flags & MLX4_EN_FLAG_FORCE_PROMISC) {
1159 		en_warn(priv, "Forcing promiscuous mode on port:%d\n",
1160 			priv->port);
1161 	} else if (prev_flags & MLX4_EN_FLAG_FORCE_PROMISC) {
1162 		en_warn(priv, "Stop forcing promiscuous mode on port:%d\n",
1163 			priv->port);
1164 	}
1165 }
1166 
1167 static void mlx4_en_do_set_rx_mode(struct work_struct *work)
1168 {
1169 	struct mlx4_en_priv *priv = container_of(work, struct mlx4_en_priv,
1170 						 rx_mode_task);
1171 	struct mlx4_en_dev *mdev = priv->mdev;
1172 	struct net_device *dev = priv->dev;
1173 
1174 	mutex_lock(&mdev->state_lock);
1175 	if (!mdev->device_up) {
1176 		en_dbg(HW, priv, "Card is not up, ignoring rx mode change.\n");
1177 		goto out;
1178 	}
1179 	if (!priv->port_up) {
1180 		en_dbg(HW, priv, "Port is down, ignoring rx mode change.\n");
1181 		goto out;
1182 	}
1183 
1184 	if (!netif_carrier_ok(dev)) {
1185 		if (!mlx4_en_QUERY_PORT(mdev, priv->port)) {
1186 			if (priv->port_state.link_state) {
1187 				priv->last_link_state = MLX4_DEV_EVENT_PORT_UP;
1188 				netif_carrier_on(dev);
1189 				en_dbg(LINK, priv, "Link Up\n");
1190 			}
1191 		}
1192 	}
1193 
1194 	if (dev->priv_flags & IFF_UNICAST_FLT)
1195 		mlx4_en_do_uc_filter(priv, dev, mdev);
1196 
1197 	/* Promsicuous mode: disable all filters */
1198 	if ((dev->flags & IFF_PROMISC) ||
1199 	    (priv->flags & MLX4_EN_FLAG_FORCE_PROMISC)) {
1200 		mlx4_en_set_promisc_mode(priv, mdev);
1201 		goto out;
1202 	}
1203 
1204 	/* Not in promiscuous mode */
1205 	if (priv->flags & MLX4_EN_FLAG_PROMISC)
1206 		mlx4_en_clear_promisc_mode(priv, mdev);
1207 
1208 	mlx4_en_do_multicast(priv, dev, mdev);
1209 out:
1210 	mutex_unlock(&mdev->state_lock);
1211 }
1212 
1213 #ifdef CONFIG_NET_POLL_CONTROLLER
1214 static void mlx4_en_netpoll(struct net_device *dev)
1215 {
1216 	struct mlx4_en_priv *priv = netdev_priv(dev);
1217 	struct mlx4_en_cq *cq;
1218 	int i;
1219 
1220 	for (i = 0; i < priv->tx_ring_num[TX]; i++) {
1221 		cq = priv->tx_cq[TX][i];
1222 		napi_schedule(&cq->napi);
1223 	}
1224 }
1225 #endif
1226 
1227 static int mlx4_en_set_rss_steer_rules(struct mlx4_en_priv *priv)
1228 {
1229 	u64 reg_id;
1230 	int err = 0;
1231 	int *qpn = &priv->base_qpn;
1232 	struct mlx4_mac_entry *entry;
1233 
1234 	err = mlx4_en_uc_steer_add(priv, priv->dev->dev_addr, qpn, &reg_id);
1235 	if (err)
1236 		return err;
1237 
1238 	err = mlx4_en_tunnel_steer_add(priv, priv->dev->dev_addr, *qpn,
1239 				       &priv->tunnel_reg_id);
1240 	if (err)
1241 		goto tunnel_err;
1242 
1243 	entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1244 	if (!entry) {
1245 		err = -ENOMEM;
1246 		goto alloc_err;
1247 	}
1248 
1249 	memcpy(entry->mac, priv->dev->dev_addr, sizeof(entry->mac));
1250 	memcpy(priv->current_mac, entry->mac, sizeof(priv->current_mac));
1251 	entry->reg_id = reg_id;
1252 	hlist_add_head_rcu(&entry->hlist,
1253 			   &priv->mac_hash[entry->mac[MLX4_EN_MAC_HASH_IDX]]);
1254 
1255 	return 0;
1256 
1257 alloc_err:
1258 	if (priv->tunnel_reg_id)
1259 		mlx4_flow_detach(priv->mdev->dev, priv->tunnel_reg_id);
1260 
1261 tunnel_err:
1262 	mlx4_en_uc_steer_release(priv, priv->dev->dev_addr, *qpn, reg_id);
1263 	return err;
1264 }
1265 
1266 static void mlx4_en_delete_rss_steer_rules(struct mlx4_en_priv *priv)
1267 {
1268 	u64 mac;
1269 	unsigned int i;
1270 	int qpn = priv->base_qpn;
1271 	struct hlist_head *bucket;
1272 	struct hlist_node *tmp;
1273 	struct mlx4_mac_entry *entry;
1274 
1275 	for (i = 0; i < MLX4_EN_MAC_HASH_SIZE; ++i) {
1276 		bucket = &priv->mac_hash[i];
1277 		hlist_for_each_entry_safe(entry, tmp, bucket, hlist) {
1278 			mac = mlx4_mac_to_u64(entry->mac);
1279 			en_dbg(DRV, priv, "Registering MAC:%pM for deleting\n",
1280 			       entry->mac);
1281 			mlx4_en_uc_steer_release(priv, entry->mac,
1282 						 qpn, entry->reg_id);
1283 
1284 			mlx4_unregister_mac(priv->mdev->dev, priv->port, mac);
1285 			hlist_del_rcu(&entry->hlist);
1286 			kfree_rcu(entry, rcu);
1287 		}
1288 	}
1289 
1290 	if (priv->tunnel_reg_id) {
1291 		mlx4_flow_detach(priv->mdev->dev, priv->tunnel_reg_id);
1292 		priv->tunnel_reg_id = 0;
1293 	}
1294 }
1295 
1296 static void mlx4_en_tx_timeout(struct net_device *dev)
1297 {
1298 	struct mlx4_en_priv *priv = netdev_priv(dev);
1299 	struct mlx4_en_dev *mdev = priv->mdev;
1300 	int i;
1301 
1302 	if (netif_msg_timer(priv))
1303 		en_warn(priv, "Tx timeout called on port:%d\n", priv->port);
1304 
1305 	for (i = 0; i < priv->tx_ring_num[TX]; i++) {
1306 		struct mlx4_en_tx_ring *tx_ring = priv->tx_ring[TX][i];
1307 
1308 		if (!netif_tx_queue_stopped(netdev_get_tx_queue(dev, i)))
1309 			continue;
1310 		en_warn(priv, "TX timeout on queue: %d, QP: 0x%x, CQ: 0x%x, Cons: 0x%x, Prod: 0x%x\n",
1311 			i, tx_ring->qpn, tx_ring->sp_cqn,
1312 			tx_ring->cons, tx_ring->prod);
1313 	}
1314 
1315 	priv->port_stats.tx_timeout++;
1316 	en_dbg(DRV, priv, "Scheduling watchdog\n");
1317 	queue_work(mdev->workqueue, &priv->watchdog_task);
1318 }
1319 
1320 
1321 static struct rtnl_link_stats64 *
1322 mlx4_en_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
1323 {
1324 	struct mlx4_en_priv *priv = netdev_priv(dev);
1325 
1326 	spin_lock_bh(&priv->stats_lock);
1327 	mlx4_en_fold_software_stats(dev);
1328 	netdev_stats_to_stats64(stats, &dev->stats);
1329 	spin_unlock_bh(&priv->stats_lock);
1330 
1331 	return stats;
1332 }
1333 
1334 static void mlx4_en_set_default_moderation(struct mlx4_en_priv *priv)
1335 {
1336 	struct mlx4_en_cq *cq;
1337 	int i, t;
1338 
1339 	/* If we haven't received a specific coalescing setting
1340 	 * (module param), we set the moderation parameters as follows:
1341 	 * - moder_cnt is set to the number of mtu sized packets to
1342 	 *   satisfy our coalescing target.
1343 	 * - moder_time is set to a fixed value.
1344 	 */
1345 	priv->rx_frames = MLX4_EN_RX_COAL_TARGET;
1346 	priv->rx_usecs = MLX4_EN_RX_COAL_TIME;
1347 	priv->tx_frames = MLX4_EN_TX_COAL_PKTS;
1348 	priv->tx_usecs = MLX4_EN_TX_COAL_TIME;
1349 	en_dbg(INTR, priv, "Default coalesing params for mtu:%d - rx_frames:%d rx_usecs:%d\n",
1350 	       priv->dev->mtu, priv->rx_frames, priv->rx_usecs);
1351 
1352 	/* Setup cq moderation params */
1353 	for (i = 0; i < priv->rx_ring_num; i++) {
1354 		cq = priv->rx_cq[i];
1355 		cq->moder_cnt = priv->rx_frames;
1356 		cq->moder_time = priv->rx_usecs;
1357 		priv->last_moder_time[i] = MLX4_EN_AUTO_CONF;
1358 		priv->last_moder_packets[i] = 0;
1359 		priv->last_moder_bytes[i] = 0;
1360 	}
1361 
1362 	for (t = 0 ; t < MLX4_EN_NUM_TX_TYPES; t++) {
1363 		for (i = 0; i < priv->tx_ring_num[t]; i++) {
1364 			cq = priv->tx_cq[t][i];
1365 			cq->moder_cnt = priv->tx_frames;
1366 			cq->moder_time = priv->tx_usecs;
1367 		}
1368 	}
1369 
1370 	/* Reset auto-moderation params */
1371 	priv->pkt_rate_low = MLX4_EN_RX_RATE_LOW;
1372 	priv->rx_usecs_low = MLX4_EN_RX_COAL_TIME_LOW;
1373 	priv->pkt_rate_high = MLX4_EN_RX_RATE_HIGH;
1374 	priv->rx_usecs_high = MLX4_EN_RX_COAL_TIME_HIGH;
1375 	priv->sample_interval = MLX4_EN_SAMPLE_INTERVAL;
1376 	priv->adaptive_rx_coal = 1;
1377 	priv->last_moder_jiffies = 0;
1378 	priv->last_moder_tx_packets = 0;
1379 }
1380 
1381 static void mlx4_en_auto_moderation(struct mlx4_en_priv *priv)
1382 {
1383 	unsigned long period = (unsigned long) (jiffies - priv->last_moder_jiffies);
1384 	struct mlx4_en_cq *cq;
1385 	unsigned long packets;
1386 	unsigned long rate;
1387 	unsigned long avg_pkt_size;
1388 	unsigned long rx_packets;
1389 	unsigned long rx_bytes;
1390 	unsigned long rx_pkt_diff;
1391 	int moder_time;
1392 	int ring, err;
1393 
1394 	if (!priv->adaptive_rx_coal || period < priv->sample_interval * HZ)
1395 		return;
1396 
1397 	for (ring = 0; ring < priv->rx_ring_num; ring++) {
1398 		rx_packets = READ_ONCE(priv->rx_ring[ring]->packets);
1399 		rx_bytes = READ_ONCE(priv->rx_ring[ring]->bytes);
1400 
1401 		rx_pkt_diff = ((unsigned long) (rx_packets -
1402 				priv->last_moder_packets[ring]));
1403 		packets = rx_pkt_diff;
1404 		rate = packets * HZ / period;
1405 		avg_pkt_size = packets ? ((unsigned long) (rx_bytes -
1406 				priv->last_moder_bytes[ring])) / packets : 0;
1407 
1408 		/* Apply auto-moderation only when packet rate
1409 		 * exceeds a rate that it matters */
1410 		if (rate > (MLX4_EN_RX_RATE_THRESH / priv->rx_ring_num) &&
1411 		    avg_pkt_size > MLX4_EN_AVG_PKT_SMALL) {
1412 			if (rate < priv->pkt_rate_low)
1413 				moder_time = priv->rx_usecs_low;
1414 			else if (rate > priv->pkt_rate_high)
1415 				moder_time = priv->rx_usecs_high;
1416 			else
1417 				moder_time = (rate - priv->pkt_rate_low) *
1418 					(priv->rx_usecs_high - priv->rx_usecs_low) /
1419 					(priv->pkt_rate_high - priv->pkt_rate_low) +
1420 					priv->rx_usecs_low;
1421 		} else {
1422 			moder_time = priv->rx_usecs_low;
1423 		}
1424 
1425 		if (moder_time != priv->last_moder_time[ring]) {
1426 			priv->last_moder_time[ring] = moder_time;
1427 			cq = priv->rx_cq[ring];
1428 			cq->moder_time = moder_time;
1429 			cq->moder_cnt = priv->rx_frames;
1430 			err = mlx4_en_set_cq_moder(priv, cq);
1431 			if (err)
1432 				en_err(priv, "Failed modifying moderation for cq:%d\n",
1433 				       ring);
1434 		}
1435 		priv->last_moder_packets[ring] = rx_packets;
1436 		priv->last_moder_bytes[ring] = rx_bytes;
1437 	}
1438 
1439 	priv->last_moder_jiffies = jiffies;
1440 }
1441 
1442 static void mlx4_en_do_get_stats(struct work_struct *work)
1443 {
1444 	struct delayed_work *delay = to_delayed_work(work);
1445 	struct mlx4_en_priv *priv = container_of(delay, struct mlx4_en_priv,
1446 						 stats_task);
1447 	struct mlx4_en_dev *mdev = priv->mdev;
1448 	int err;
1449 
1450 	mutex_lock(&mdev->state_lock);
1451 	if (mdev->device_up) {
1452 		if (priv->port_up) {
1453 			err = mlx4_en_DUMP_ETH_STATS(mdev, priv->port, 0);
1454 			if (err)
1455 				en_dbg(HW, priv, "Could not update stats\n");
1456 
1457 			mlx4_en_auto_moderation(priv);
1458 		}
1459 
1460 		queue_delayed_work(mdev->workqueue, &priv->stats_task, STATS_DELAY);
1461 	}
1462 	if (mdev->mac_removed[MLX4_MAX_PORTS + 1 - priv->port]) {
1463 		mlx4_en_do_set_mac(priv, priv->current_mac);
1464 		mdev->mac_removed[MLX4_MAX_PORTS + 1 - priv->port] = 0;
1465 	}
1466 	mutex_unlock(&mdev->state_lock);
1467 }
1468 
1469 /* mlx4_en_service_task - Run service task for tasks that needed to be done
1470  * periodically
1471  */
1472 static void mlx4_en_service_task(struct work_struct *work)
1473 {
1474 	struct delayed_work *delay = to_delayed_work(work);
1475 	struct mlx4_en_priv *priv = container_of(delay, struct mlx4_en_priv,
1476 						 service_task);
1477 	struct mlx4_en_dev *mdev = priv->mdev;
1478 
1479 	mutex_lock(&mdev->state_lock);
1480 	if (mdev->device_up) {
1481 		if (mdev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_TS)
1482 			mlx4_en_ptp_overflow_check(mdev);
1483 
1484 		mlx4_en_recover_from_oom(priv);
1485 		queue_delayed_work(mdev->workqueue, &priv->service_task,
1486 				   SERVICE_TASK_DELAY);
1487 	}
1488 	mutex_unlock(&mdev->state_lock);
1489 }
1490 
1491 static void mlx4_en_linkstate(struct work_struct *work)
1492 {
1493 	struct mlx4_en_priv *priv = container_of(work, struct mlx4_en_priv,
1494 						 linkstate_task);
1495 	struct mlx4_en_dev *mdev = priv->mdev;
1496 	int linkstate = priv->link_state;
1497 
1498 	mutex_lock(&mdev->state_lock);
1499 	/* If observable port state changed set carrier state and
1500 	 * report to system log */
1501 	if (priv->last_link_state != linkstate) {
1502 		if (linkstate == MLX4_DEV_EVENT_PORT_DOWN) {
1503 			en_info(priv, "Link Down\n");
1504 			netif_carrier_off(priv->dev);
1505 		} else {
1506 			en_info(priv, "Link Up\n");
1507 			netif_carrier_on(priv->dev);
1508 		}
1509 	}
1510 	priv->last_link_state = linkstate;
1511 	mutex_unlock(&mdev->state_lock);
1512 }
1513 
1514 static int mlx4_en_init_affinity_hint(struct mlx4_en_priv *priv, int ring_idx)
1515 {
1516 	struct mlx4_en_rx_ring *ring = priv->rx_ring[ring_idx];
1517 	int numa_node = priv->mdev->dev->numa_node;
1518 
1519 	if (!zalloc_cpumask_var(&ring->affinity_mask, GFP_KERNEL))
1520 		return -ENOMEM;
1521 
1522 	cpumask_set_cpu(cpumask_local_spread(ring_idx, numa_node),
1523 			ring->affinity_mask);
1524 	return 0;
1525 }
1526 
1527 static void mlx4_en_free_affinity_hint(struct mlx4_en_priv *priv, int ring_idx)
1528 {
1529 	free_cpumask_var(priv->rx_ring[ring_idx]->affinity_mask);
1530 }
1531 
1532 static void mlx4_en_init_recycle_ring(struct mlx4_en_priv *priv,
1533 				      int tx_ring_idx)
1534 {
1535 	struct mlx4_en_tx_ring *tx_ring = priv->tx_ring[TX_XDP][tx_ring_idx];
1536 	int rr_index = tx_ring_idx;
1537 
1538 	tx_ring->free_tx_desc = mlx4_en_recycle_tx_desc;
1539 	tx_ring->recycle_ring = priv->rx_ring[rr_index];
1540 	en_dbg(DRV, priv, "Set tx_ring[%d][%d]->recycle_ring = rx_ring[%d]\n",
1541 	       TX_XDP, tx_ring_idx, rr_index);
1542 }
1543 
1544 int mlx4_en_start_port(struct net_device *dev)
1545 {
1546 	struct mlx4_en_priv *priv = netdev_priv(dev);
1547 	struct mlx4_en_dev *mdev = priv->mdev;
1548 	struct mlx4_en_cq *cq;
1549 	struct mlx4_en_tx_ring *tx_ring;
1550 	int rx_index = 0;
1551 	int err = 0;
1552 	int i, t;
1553 	int j;
1554 	u8 mc_list[16] = {0};
1555 
1556 	if (priv->port_up) {
1557 		en_dbg(DRV, priv, "start port called while port already up\n");
1558 		return 0;
1559 	}
1560 
1561 	INIT_LIST_HEAD(&priv->mc_list);
1562 	INIT_LIST_HEAD(&priv->curr_list);
1563 	INIT_LIST_HEAD(&priv->ethtool_list);
1564 	memset(&priv->ethtool_rules[0], 0,
1565 	       sizeof(struct ethtool_flow_id) * MAX_NUM_OF_FS_RULES);
1566 
1567 	/* Calculate Rx buf size */
1568 	dev->mtu = min(dev->mtu, priv->max_mtu);
1569 	mlx4_en_calc_rx_buf(dev);
1570 	en_dbg(DRV, priv, "Rx buf size:%d\n", priv->rx_skb_size);
1571 
1572 	/* Configure rx cq's and rings */
1573 	err = mlx4_en_activate_rx_rings(priv);
1574 	if (err) {
1575 		en_err(priv, "Failed to activate RX rings\n");
1576 		return err;
1577 	}
1578 	for (i = 0; i < priv->rx_ring_num; i++) {
1579 		cq = priv->rx_cq[i];
1580 
1581 		err = mlx4_en_init_affinity_hint(priv, i);
1582 		if (err) {
1583 			en_err(priv, "Failed preparing IRQ affinity hint\n");
1584 			goto cq_err;
1585 		}
1586 
1587 		err = mlx4_en_activate_cq(priv, cq, i);
1588 		if (err) {
1589 			en_err(priv, "Failed activating Rx CQ\n");
1590 			mlx4_en_free_affinity_hint(priv, i);
1591 			goto cq_err;
1592 		}
1593 
1594 		for (j = 0; j < cq->size; j++) {
1595 			struct mlx4_cqe *cqe = NULL;
1596 
1597 			cqe = mlx4_en_get_cqe(cq->buf, j, priv->cqe_size) +
1598 			      priv->cqe_factor;
1599 			cqe->owner_sr_opcode = MLX4_CQE_OWNER_MASK;
1600 		}
1601 
1602 		err = mlx4_en_set_cq_moder(priv, cq);
1603 		if (err) {
1604 			en_err(priv, "Failed setting cq moderation parameters\n");
1605 			mlx4_en_deactivate_cq(priv, cq);
1606 			mlx4_en_free_affinity_hint(priv, i);
1607 			goto cq_err;
1608 		}
1609 		mlx4_en_arm_cq(priv, cq);
1610 		priv->rx_ring[i]->cqn = cq->mcq.cqn;
1611 		++rx_index;
1612 	}
1613 
1614 	/* Set qp number */
1615 	en_dbg(DRV, priv, "Getting qp number for port %d\n", priv->port);
1616 	err = mlx4_en_get_qp(priv);
1617 	if (err) {
1618 		en_err(priv, "Failed getting eth qp\n");
1619 		goto cq_err;
1620 	}
1621 	mdev->mac_removed[priv->port] = 0;
1622 
1623 	priv->counter_index =
1624 			mlx4_get_default_counter_index(mdev->dev, priv->port);
1625 
1626 	err = mlx4_en_config_rss_steer(priv);
1627 	if (err) {
1628 		en_err(priv, "Failed configuring rss steering\n");
1629 		goto mac_err;
1630 	}
1631 
1632 	err = mlx4_en_create_drop_qp(priv);
1633 	if (err)
1634 		goto rss_err;
1635 
1636 	/* Configure tx cq's and rings */
1637 	for (t = 0 ; t < MLX4_EN_NUM_TX_TYPES; t++) {
1638 		u8 num_tx_rings_p_up = t == TX ? priv->num_tx_rings_p_up : 1;
1639 
1640 		for (i = 0; i < priv->tx_ring_num[t]; i++) {
1641 			/* Configure cq */
1642 			cq = priv->tx_cq[t][i];
1643 			err = mlx4_en_activate_cq(priv, cq, i);
1644 			if (err) {
1645 				en_err(priv, "Failed allocating Tx CQ\n");
1646 				goto tx_err;
1647 			}
1648 			err = mlx4_en_set_cq_moder(priv, cq);
1649 			if (err) {
1650 				en_err(priv, "Failed setting cq moderation parameters\n");
1651 				mlx4_en_deactivate_cq(priv, cq);
1652 				goto tx_err;
1653 			}
1654 			en_dbg(DRV, priv,
1655 			       "Resetting index of collapsed CQ:%d to -1\n", i);
1656 			cq->buf->wqe_index = cpu_to_be16(0xffff);
1657 
1658 			/* Configure ring */
1659 			tx_ring = priv->tx_ring[t][i];
1660 			err = mlx4_en_activate_tx_ring(priv, tx_ring,
1661 						       cq->mcq.cqn,
1662 						       i / num_tx_rings_p_up);
1663 			if (err) {
1664 				en_err(priv, "Failed allocating Tx ring\n");
1665 				mlx4_en_deactivate_cq(priv, cq);
1666 				goto tx_err;
1667 			}
1668 			if (t != TX_XDP) {
1669 				tx_ring->tx_queue = netdev_get_tx_queue(dev, i);
1670 				tx_ring->recycle_ring = NULL;
1671 			} else {
1672 				mlx4_en_init_recycle_ring(priv, i);
1673 			}
1674 
1675 			/* Arm CQ for TX completions */
1676 			mlx4_en_arm_cq(priv, cq);
1677 
1678 			/* Set initial ownership of all Tx TXBBs to SW (1) */
1679 			for (j = 0; j < tx_ring->buf_size; j += STAMP_STRIDE)
1680 				*((u32 *)(tx_ring->buf + j)) = 0xffffffff;
1681 		}
1682 	}
1683 
1684 	/* Configure port */
1685 	err = mlx4_SET_PORT_general(mdev->dev, priv->port,
1686 				    priv->rx_skb_size + ETH_FCS_LEN,
1687 				    priv->prof->tx_pause,
1688 				    priv->prof->tx_ppp,
1689 				    priv->prof->rx_pause,
1690 				    priv->prof->rx_ppp);
1691 	if (err) {
1692 		en_err(priv, "Failed setting port general configurations for port %d, with error %d\n",
1693 		       priv->port, err);
1694 		goto tx_err;
1695 	}
1696 	/* Set default qp number */
1697 	err = mlx4_SET_PORT_qpn_calc(mdev->dev, priv->port, priv->base_qpn, 0);
1698 	if (err) {
1699 		en_err(priv, "Failed setting default qp numbers\n");
1700 		goto tx_err;
1701 	}
1702 
1703 	if (mdev->dev->caps.tunnel_offload_mode == MLX4_TUNNEL_OFFLOAD_MODE_VXLAN) {
1704 		err = mlx4_SET_PORT_VXLAN(mdev->dev, priv->port, VXLAN_STEER_BY_OUTER_MAC, 1);
1705 		if (err) {
1706 			en_err(priv, "Failed setting port L2 tunnel configuration, err %d\n",
1707 			       err);
1708 			goto tx_err;
1709 		}
1710 	}
1711 
1712 	/* Init port */
1713 	en_dbg(HW, priv, "Initializing port\n");
1714 	err = mlx4_INIT_PORT(mdev->dev, priv->port);
1715 	if (err) {
1716 		en_err(priv, "Failed Initializing port\n");
1717 		goto tx_err;
1718 	}
1719 
1720 	/* Set Unicast and VXLAN steering rules */
1721 	if (mdev->dev->caps.steering_mode != MLX4_STEERING_MODE_A0 &&
1722 	    mlx4_en_set_rss_steer_rules(priv))
1723 		mlx4_warn(mdev, "Failed setting steering rules\n");
1724 
1725 	/* Attach rx QP to bradcast address */
1726 	eth_broadcast_addr(&mc_list[10]);
1727 	mc_list[5] = priv->port; /* needed for B0 steering support */
1728 	if (mlx4_multicast_attach(mdev->dev, &priv->rss_map.indir_qp, mc_list,
1729 				  priv->port, 0, MLX4_PROT_ETH,
1730 				  &priv->broadcast_id))
1731 		mlx4_warn(mdev, "Failed Attaching Broadcast\n");
1732 
1733 	/* Must redo promiscuous mode setup. */
1734 	priv->flags &= ~(MLX4_EN_FLAG_PROMISC | MLX4_EN_FLAG_MC_PROMISC);
1735 
1736 	/* Schedule multicast task to populate multicast list */
1737 	queue_work(mdev->workqueue, &priv->rx_mode_task);
1738 
1739 	if (priv->mdev->dev->caps.tunnel_offload_mode == MLX4_TUNNEL_OFFLOAD_MODE_VXLAN)
1740 		udp_tunnel_get_rx_info(dev);
1741 
1742 	priv->port_up = true;
1743 
1744 	/* Process all completions if exist to prevent
1745 	 * the queues freezing if they are full
1746 	 */
1747 	for (i = 0; i < priv->rx_ring_num; i++)
1748 		napi_schedule(&priv->rx_cq[i]->napi);
1749 
1750 	netif_tx_start_all_queues(dev);
1751 	netif_device_attach(dev);
1752 
1753 	return 0;
1754 
1755 tx_err:
1756 	if (t == MLX4_EN_NUM_TX_TYPES) {
1757 		t--;
1758 		i = priv->tx_ring_num[t];
1759 	}
1760 	while (t >= 0) {
1761 		while (i--) {
1762 			mlx4_en_deactivate_tx_ring(priv, priv->tx_ring[t][i]);
1763 			mlx4_en_deactivate_cq(priv, priv->tx_cq[t][i]);
1764 		}
1765 		if (!t--)
1766 			break;
1767 		i = priv->tx_ring_num[t];
1768 	}
1769 	mlx4_en_destroy_drop_qp(priv);
1770 rss_err:
1771 	mlx4_en_release_rss_steer(priv);
1772 mac_err:
1773 	mlx4_en_put_qp(priv);
1774 cq_err:
1775 	while (rx_index--) {
1776 		mlx4_en_deactivate_cq(priv, priv->rx_cq[rx_index]);
1777 		mlx4_en_free_affinity_hint(priv, rx_index);
1778 	}
1779 	for (i = 0; i < priv->rx_ring_num; i++)
1780 		mlx4_en_deactivate_rx_ring(priv, priv->rx_ring[i]);
1781 
1782 	return err; /* need to close devices */
1783 }
1784 
1785 
1786 void mlx4_en_stop_port(struct net_device *dev, int detach)
1787 {
1788 	struct mlx4_en_priv *priv = netdev_priv(dev);
1789 	struct mlx4_en_dev *mdev = priv->mdev;
1790 	struct mlx4_en_mc_list *mclist, *tmp;
1791 	struct ethtool_flow_id *flow, *tmp_flow;
1792 	int i, t;
1793 	u8 mc_list[16] = {0};
1794 
1795 	if (!priv->port_up) {
1796 		en_dbg(DRV, priv, "stop port called while port already down\n");
1797 		return;
1798 	}
1799 
1800 	/* close port*/
1801 	mlx4_CLOSE_PORT(mdev->dev, priv->port);
1802 
1803 	/* Synchronize with tx routine */
1804 	netif_tx_lock_bh(dev);
1805 	if (detach)
1806 		netif_device_detach(dev);
1807 	netif_tx_stop_all_queues(dev);
1808 	netif_tx_unlock_bh(dev);
1809 
1810 	netif_tx_disable(dev);
1811 
1812 	spin_lock_bh(&priv->stats_lock);
1813 	mlx4_en_fold_software_stats(dev);
1814 	/* Set port as not active */
1815 	priv->port_up = false;
1816 	spin_unlock_bh(&priv->stats_lock);
1817 
1818 	priv->counter_index = MLX4_SINK_COUNTER_INDEX(mdev->dev);
1819 
1820 	/* Promsicuous mode */
1821 	if (mdev->dev->caps.steering_mode ==
1822 	    MLX4_STEERING_MODE_DEVICE_MANAGED) {
1823 		priv->flags &= ~(MLX4_EN_FLAG_PROMISC |
1824 				 MLX4_EN_FLAG_MC_PROMISC);
1825 		mlx4_flow_steer_promisc_remove(mdev->dev,
1826 					       priv->port,
1827 					       MLX4_FS_ALL_DEFAULT);
1828 		mlx4_flow_steer_promisc_remove(mdev->dev,
1829 					       priv->port,
1830 					       MLX4_FS_MC_DEFAULT);
1831 	} else if (priv->flags & MLX4_EN_FLAG_PROMISC) {
1832 		priv->flags &= ~MLX4_EN_FLAG_PROMISC;
1833 
1834 		/* Disable promiscouos mode */
1835 		mlx4_unicast_promisc_remove(mdev->dev, priv->base_qpn,
1836 					    priv->port);
1837 
1838 		/* Disable Multicast promisc */
1839 		if (priv->flags & MLX4_EN_FLAG_MC_PROMISC) {
1840 			mlx4_multicast_promisc_remove(mdev->dev, priv->base_qpn,
1841 						      priv->port);
1842 			priv->flags &= ~MLX4_EN_FLAG_MC_PROMISC;
1843 		}
1844 	}
1845 
1846 	/* Detach All multicasts */
1847 	eth_broadcast_addr(&mc_list[10]);
1848 	mc_list[5] = priv->port; /* needed for B0 steering support */
1849 	mlx4_multicast_detach(mdev->dev, &priv->rss_map.indir_qp, mc_list,
1850 			      MLX4_PROT_ETH, priv->broadcast_id);
1851 	list_for_each_entry(mclist, &priv->curr_list, list) {
1852 		memcpy(&mc_list[10], mclist->addr, ETH_ALEN);
1853 		mc_list[5] = priv->port;
1854 		mlx4_multicast_detach(mdev->dev, &priv->rss_map.indir_qp,
1855 				      mc_list, MLX4_PROT_ETH, mclist->reg_id);
1856 		if (mclist->tunnel_reg_id)
1857 			mlx4_flow_detach(mdev->dev, mclist->tunnel_reg_id);
1858 	}
1859 	mlx4_en_clear_list(dev);
1860 	list_for_each_entry_safe(mclist, tmp, &priv->curr_list, list) {
1861 		list_del(&mclist->list);
1862 		kfree(mclist);
1863 	}
1864 
1865 	/* Flush multicast filter */
1866 	mlx4_SET_MCAST_FLTR(mdev->dev, priv->port, 0, 1, MLX4_MCAST_CONFIG);
1867 
1868 	/* Remove flow steering rules for the port*/
1869 	if (mdev->dev->caps.steering_mode ==
1870 	    MLX4_STEERING_MODE_DEVICE_MANAGED) {
1871 		ASSERT_RTNL();
1872 		list_for_each_entry_safe(flow, tmp_flow,
1873 					 &priv->ethtool_list, list) {
1874 			mlx4_flow_detach(mdev->dev, flow->id);
1875 			list_del(&flow->list);
1876 		}
1877 	}
1878 
1879 	mlx4_en_destroy_drop_qp(priv);
1880 
1881 	/* Free TX Rings */
1882 	for (t = 0; t < MLX4_EN_NUM_TX_TYPES; t++) {
1883 		for (i = 0; i < priv->tx_ring_num[t]; i++) {
1884 			mlx4_en_deactivate_tx_ring(priv, priv->tx_ring[t][i]);
1885 			mlx4_en_deactivate_cq(priv, priv->tx_cq[t][i]);
1886 		}
1887 	}
1888 	msleep(10);
1889 
1890 	for (t = 0; t < MLX4_EN_NUM_TX_TYPES; t++)
1891 		for (i = 0; i < priv->tx_ring_num[t]; i++)
1892 			mlx4_en_free_tx_buf(dev, priv->tx_ring[t][i]);
1893 
1894 	if (mdev->dev->caps.steering_mode != MLX4_STEERING_MODE_A0)
1895 		mlx4_en_delete_rss_steer_rules(priv);
1896 
1897 	/* Free RSS qps */
1898 	mlx4_en_release_rss_steer(priv);
1899 
1900 	/* Unregister Mac address for the port */
1901 	mlx4_en_put_qp(priv);
1902 	if (!(mdev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_REASSIGN_MAC_EN))
1903 		mdev->mac_removed[priv->port] = 1;
1904 
1905 	/* Free RX Rings */
1906 	for (i = 0; i < priv->rx_ring_num; i++) {
1907 		struct mlx4_en_cq *cq = priv->rx_cq[i];
1908 
1909 		napi_synchronize(&cq->napi);
1910 		mlx4_en_deactivate_rx_ring(priv, priv->rx_ring[i]);
1911 		mlx4_en_deactivate_cq(priv, cq);
1912 
1913 		mlx4_en_free_affinity_hint(priv, i);
1914 	}
1915 }
1916 
1917 static void mlx4_en_restart(struct work_struct *work)
1918 {
1919 	struct mlx4_en_priv *priv = container_of(work, struct mlx4_en_priv,
1920 						 watchdog_task);
1921 	struct mlx4_en_dev *mdev = priv->mdev;
1922 	struct net_device *dev = priv->dev;
1923 
1924 	en_dbg(DRV, priv, "Watchdog task called for port %d\n", priv->port);
1925 
1926 	rtnl_lock();
1927 	mutex_lock(&mdev->state_lock);
1928 	if (priv->port_up) {
1929 		mlx4_en_stop_port(dev, 1);
1930 		if (mlx4_en_start_port(dev))
1931 			en_err(priv, "Failed restarting port %d\n", priv->port);
1932 	}
1933 	mutex_unlock(&mdev->state_lock);
1934 	rtnl_unlock();
1935 }
1936 
1937 static void mlx4_en_clear_stats(struct net_device *dev)
1938 {
1939 	struct mlx4_en_priv *priv = netdev_priv(dev);
1940 	struct mlx4_en_dev *mdev = priv->mdev;
1941 	struct mlx4_en_tx_ring **tx_ring;
1942 	int i;
1943 
1944 	if (!mlx4_is_slave(mdev->dev))
1945 		if (mlx4_en_DUMP_ETH_STATS(mdev, priv->port, 1))
1946 			en_dbg(HW, priv, "Failed dumping statistics\n");
1947 
1948 	memset(&priv->pstats, 0, sizeof(priv->pstats));
1949 	memset(&priv->pkstats, 0, sizeof(priv->pkstats));
1950 	memset(&priv->port_stats, 0, sizeof(priv->port_stats));
1951 	memset(&priv->rx_flowstats, 0, sizeof(priv->rx_flowstats));
1952 	memset(&priv->tx_flowstats, 0, sizeof(priv->tx_flowstats));
1953 	memset(&priv->rx_priority_flowstats, 0,
1954 	       sizeof(priv->rx_priority_flowstats));
1955 	memset(&priv->tx_priority_flowstats, 0,
1956 	       sizeof(priv->tx_priority_flowstats));
1957 	memset(&priv->pf_stats, 0, sizeof(priv->pf_stats));
1958 
1959 	tx_ring = priv->tx_ring[TX];
1960 	for (i = 0; i < priv->tx_ring_num[TX]; i++) {
1961 		tx_ring[i]->bytes = 0;
1962 		tx_ring[i]->packets = 0;
1963 		tx_ring[i]->tx_csum = 0;
1964 		tx_ring[i]->tx_dropped = 0;
1965 		tx_ring[i]->queue_stopped = 0;
1966 		tx_ring[i]->wake_queue = 0;
1967 		tx_ring[i]->tso_packets = 0;
1968 		tx_ring[i]->xmit_more = 0;
1969 	}
1970 	for (i = 0; i < priv->rx_ring_num; i++) {
1971 		priv->rx_ring[i]->bytes = 0;
1972 		priv->rx_ring[i]->packets = 0;
1973 		priv->rx_ring[i]->csum_ok = 0;
1974 		priv->rx_ring[i]->csum_none = 0;
1975 		priv->rx_ring[i]->csum_complete = 0;
1976 	}
1977 }
1978 
1979 static int mlx4_en_open(struct net_device *dev)
1980 {
1981 	struct mlx4_en_priv *priv = netdev_priv(dev);
1982 	struct mlx4_en_dev *mdev = priv->mdev;
1983 	int err = 0;
1984 
1985 	mutex_lock(&mdev->state_lock);
1986 
1987 	if (!mdev->device_up) {
1988 		en_err(priv, "Cannot open - device down/disabled\n");
1989 		err = -EBUSY;
1990 		goto out;
1991 	}
1992 
1993 	/* Reset HW statistics and SW counters */
1994 	mlx4_en_clear_stats(dev);
1995 
1996 	err = mlx4_en_start_port(dev);
1997 	if (err)
1998 		en_err(priv, "Failed starting port:%d\n", priv->port);
1999 
2000 out:
2001 	mutex_unlock(&mdev->state_lock);
2002 	return err;
2003 }
2004 
2005 
2006 static int mlx4_en_close(struct net_device *dev)
2007 {
2008 	struct mlx4_en_priv *priv = netdev_priv(dev);
2009 	struct mlx4_en_dev *mdev = priv->mdev;
2010 
2011 	en_dbg(IFDOWN, priv, "Close port called\n");
2012 
2013 	mutex_lock(&mdev->state_lock);
2014 
2015 	mlx4_en_stop_port(dev, 0);
2016 	netif_carrier_off(dev);
2017 
2018 	mutex_unlock(&mdev->state_lock);
2019 	return 0;
2020 }
2021 
2022 static void mlx4_en_free_resources(struct mlx4_en_priv *priv)
2023 {
2024 	int i, t;
2025 
2026 #ifdef CONFIG_RFS_ACCEL
2027 	priv->dev->rx_cpu_rmap = NULL;
2028 #endif
2029 
2030 	for (t = 0; t < MLX4_EN_NUM_TX_TYPES; t++) {
2031 		for (i = 0; i < priv->tx_ring_num[t]; i++) {
2032 			if (priv->tx_ring[t] && priv->tx_ring[t][i])
2033 				mlx4_en_destroy_tx_ring(priv,
2034 							&priv->tx_ring[t][i]);
2035 			if (priv->tx_cq[t] && priv->tx_cq[t][i])
2036 				mlx4_en_destroy_cq(priv, &priv->tx_cq[t][i]);
2037 		}
2038 	}
2039 
2040 	for (i = 0; i < priv->rx_ring_num; i++) {
2041 		if (priv->rx_ring[i])
2042 			mlx4_en_destroy_rx_ring(priv, &priv->rx_ring[i],
2043 				priv->prof->rx_ring_size, priv->stride);
2044 		if (priv->rx_cq[i])
2045 			mlx4_en_destroy_cq(priv, &priv->rx_cq[i]);
2046 	}
2047 
2048 }
2049 
2050 static int mlx4_en_alloc_resources(struct mlx4_en_priv *priv)
2051 {
2052 	struct mlx4_en_port_profile *prof = priv->prof;
2053 	int i, t;
2054 	int node;
2055 
2056 	/* Create tx Rings */
2057 	for (t = 0; t < MLX4_EN_NUM_TX_TYPES; t++) {
2058 		for (i = 0; i < priv->tx_ring_num[t]; i++) {
2059 			node = cpu_to_node(i % num_online_cpus());
2060 			if (mlx4_en_create_cq(priv, &priv->tx_cq[t][i],
2061 					      prof->tx_ring_size, i, t, node))
2062 				goto err;
2063 
2064 			if (mlx4_en_create_tx_ring(priv, &priv->tx_ring[t][i],
2065 						   prof->tx_ring_size,
2066 						   TXBB_SIZE, node, i))
2067 				goto err;
2068 		}
2069 	}
2070 
2071 	/* Create rx Rings */
2072 	for (i = 0; i < priv->rx_ring_num; i++) {
2073 		node = cpu_to_node(i % num_online_cpus());
2074 		if (mlx4_en_create_cq(priv, &priv->rx_cq[i],
2075 				      prof->rx_ring_size, i, RX, node))
2076 			goto err;
2077 
2078 		if (mlx4_en_create_rx_ring(priv, &priv->rx_ring[i],
2079 					   prof->rx_ring_size, priv->stride,
2080 					   node))
2081 			goto err;
2082 	}
2083 
2084 #ifdef CONFIG_RFS_ACCEL
2085 	priv->dev->rx_cpu_rmap = mlx4_get_cpu_rmap(priv->mdev->dev, priv->port);
2086 #endif
2087 
2088 	return 0;
2089 
2090 err:
2091 	en_err(priv, "Failed to allocate NIC resources\n");
2092 	for (i = 0; i < priv->rx_ring_num; i++) {
2093 		if (priv->rx_ring[i])
2094 			mlx4_en_destroy_rx_ring(priv, &priv->rx_ring[i],
2095 						prof->rx_ring_size,
2096 						priv->stride);
2097 		if (priv->rx_cq[i])
2098 			mlx4_en_destroy_cq(priv, &priv->rx_cq[i]);
2099 	}
2100 	for (t = 0; t < MLX4_EN_NUM_TX_TYPES; t++) {
2101 		for (i = 0; i < priv->tx_ring_num[t]; i++) {
2102 			if (priv->tx_ring[t][i])
2103 				mlx4_en_destroy_tx_ring(priv,
2104 							&priv->tx_ring[t][i]);
2105 			if (priv->tx_cq[t][i])
2106 				mlx4_en_destroy_cq(priv, &priv->tx_cq[t][i]);
2107 		}
2108 	}
2109 	return -ENOMEM;
2110 }
2111 
2112 
2113 static int mlx4_en_copy_priv(struct mlx4_en_priv *dst,
2114 			     struct mlx4_en_priv *src,
2115 			     struct mlx4_en_port_profile *prof)
2116 {
2117 	int t;
2118 
2119 	memcpy(&dst->hwtstamp_config, &prof->hwtstamp_config,
2120 	       sizeof(dst->hwtstamp_config));
2121 	dst->num_tx_rings_p_up = src->mdev->profile.num_tx_rings_p_up;
2122 	dst->rx_ring_num = prof->rx_ring_num;
2123 	dst->flags = prof->flags;
2124 	dst->mdev = src->mdev;
2125 	dst->port = src->port;
2126 	dst->dev = src->dev;
2127 	dst->prof = prof;
2128 	dst->stride = roundup_pow_of_two(sizeof(struct mlx4_en_rx_desc) +
2129 					 DS_SIZE * MLX4_EN_MAX_RX_FRAGS);
2130 
2131 	for (t = 0; t < MLX4_EN_NUM_TX_TYPES; t++) {
2132 		dst->tx_ring_num[t] = prof->tx_ring_num[t];
2133 		if (!dst->tx_ring_num[t])
2134 			continue;
2135 
2136 		dst->tx_ring[t] = kzalloc(sizeof(struct mlx4_en_tx_ring *) *
2137 					  MAX_TX_RINGS, GFP_KERNEL);
2138 		if (!dst->tx_ring[t])
2139 			goto err_free_tx;
2140 
2141 		dst->tx_cq[t] = kzalloc(sizeof(struct mlx4_en_cq *) *
2142 					MAX_TX_RINGS, GFP_KERNEL);
2143 		if (!dst->tx_cq[t]) {
2144 			kfree(dst->tx_ring[t]);
2145 			goto err_free_tx;
2146 		}
2147 	}
2148 
2149 	return 0;
2150 
2151 err_free_tx:
2152 	while (t--) {
2153 		kfree(dst->tx_ring[t]);
2154 		kfree(dst->tx_cq[t]);
2155 	}
2156 	return -ENOMEM;
2157 }
2158 
2159 static void mlx4_en_update_priv(struct mlx4_en_priv *dst,
2160 				struct mlx4_en_priv *src)
2161 {
2162 	int t;
2163 	memcpy(dst->rx_ring, src->rx_ring,
2164 	       sizeof(struct mlx4_en_rx_ring *) * src->rx_ring_num);
2165 	memcpy(dst->rx_cq, src->rx_cq,
2166 	       sizeof(struct mlx4_en_cq *) * src->rx_ring_num);
2167 	memcpy(&dst->hwtstamp_config, &src->hwtstamp_config,
2168 	       sizeof(dst->hwtstamp_config));
2169 	for (t = 0; t < MLX4_EN_NUM_TX_TYPES; t++) {
2170 		dst->tx_ring_num[t] = src->tx_ring_num[t];
2171 		dst->tx_ring[t] = src->tx_ring[t];
2172 		dst->tx_cq[t] = src->tx_cq[t];
2173 	}
2174 	dst->rx_ring_num = src->rx_ring_num;
2175 	memcpy(dst->prof, src->prof, sizeof(struct mlx4_en_port_profile));
2176 }
2177 
2178 int mlx4_en_try_alloc_resources(struct mlx4_en_priv *priv,
2179 				struct mlx4_en_priv *tmp,
2180 				struct mlx4_en_port_profile *prof)
2181 {
2182 	int t;
2183 
2184 	mlx4_en_copy_priv(tmp, priv, prof);
2185 
2186 	if (mlx4_en_alloc_resources(tmp)) {
2187 		en_warn(priv,
2188 			"%s: Resource allocation failed, using previous configuration\n",
2189 			__func__);
2190 		for (t = 0; t < MLX4_EN_NUM_TX_TYPES; t++) {
2191 			kfree(tmp->tx_ring[t]);
2192 			kfree(tmp->tx_cq[t]);
2193 		}
2194 		return -ENOMEM;
2195 	}
2196 	return 0;
2197 }
2198 
2199 void mlx4_en_safe_replace_resources(struct mlx4_en_priv *priv,
2200 				    struct mlx4_en_priv *tmp)
2201 {
2202 	mlx4_en_free_resources(priv);
2203 	mlx4_en_update_priv(priv, tmp);
2204 }
2205 
2206 void mlx4_en_destroy_netdev(struct net_device *dev)
2207 {
2208 	struct mlx4_en_priv *priv = netdev_priv(dev);
2209 	struct mlx4_en_dev *mdev = priv->mdev;
2210 	int t;
2211 
2212 	en_dbg(DRV, priv, "Destroying netdev on port:%d\n", priv->port);
2213 
2214 	/* Unregister device - this will close the port if it was up */
2215 	if (priv->registered) {
2216 		devlink_port_type_clear(mlx4_get_devlink_port(mdev->dev,
2217 							      priv->port));
2218 		unregister_netdev(dev);
2219 	}
2220 
2221 	if (priv->allocated)
2222 		mlx4_free_hwq_res(mdev->dev, &priv->res, MLX4_EN_PAGE_SIZE);
2223 
2224 	cancel_delayed_work(&priv->stats_task);
2225 	cancel_delayed_work(&priv->service_task);
2226 	/* flush any pending task for this netdev */
2227 	flush_workqueue(mdev->workqueue);
2228 
2229 	if (mdev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_TS)
2230 		mlx4_en_remove_timestamp(mdev);
2231 
2232 	/* Detach the netdev so tasks would not attempt to access it */
2233 	mutex_lock(&mdev->state_lock);
2234 	mdev->pndev[priv->port] = NULL;
2235 	mdev->upper[priv->port] = NULL;
2236 
2237 #ifdef CONFIG_RFS_ACCEL
2238 	mlx4_en_cleanup_filters(priv);
2239 #endif
2240 
2241 	mlx4_en_free_resources(priv);
2242 	mutex_unlock(&mdev->state_lock);
2243 
2244 	for (t = 0; t < MLX4_EN_NUM_TX_TYPES; t++) {
2245 		kfree(priv->tx_ring[t]);
2246 		kfree(priv->tx_cq[t]);
2247 	}
2248 
2249 	free_netdev(dev);
2250 }
2251 
2252 static int mlx4_en_change_mtu(struct net_device *dev, int new_mtu)
2253 {
2254 	struct mlx4_en_priv *priv = netdev_priv(dev);
2255 	struct mlx4_en_dev *mdev = priv->mdev;
2256 	int err = 0;
2257 
2258 	en_dbg(DRV, priv, "Change MTU called - current:%d new:%d\n",
2259 		 dev->mtu, new_mtu);
2260 
2261 	if (priv->tx_ring_num[TX_XDP] && MLX4_EN_EFF_MTU(new_mtu) > FRAG_SZ0) {
2262 		en_err(priv, "MTU size:%d requires frags but XDP running\n",
2263 		       new_mtu);
2264 		return -EOPNOTSUPP;
2265 	}
2266 	dev->mtu = new_mtu;
2267 
2268 	if (netif_running(dev)) {
2269 		mutex_lock(&mdev->state_lock);
2270 		if (!mdev->device_up) {
2271 			/* NIC is probably restarting - let watchdog task reset
2272 			 * the port */
2273 			en_dbg(DRV, priv, "Change MTU called with card down!?\n");
2274 		} else {
2275 			mlx4_en_stop_port(dev, 1);
2276 			err = mlx4_en_start_port(dev);
2277 			if (err) {
2278 				en_err(priv, "Failed restarting port:%d\n",
2279 					 priv->port);
2280 				queue_work(mdev->workqueue, &priv->watchdog_task);
2281 			}
2282 		}
2283 		mutex_unlock(&mdev->state_lock);
2284 	}
2285 	return 0;
2286 }
2287 
2288 static int mlx4_en_hwtstamp_set(struct net_device *dev, struct ifreq *ifr)
2289 {
2290 	struct mlx4_en_priv *priv = netdev_priv(dev);
2291 	struct mlx4_en_dev *mdev = priv->mdev;
2292 	struct hwtstamp_config config;
2293 
2294 	if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
2295 		return -EFAULT;
2296 
2297 	/* reserved for future extensions */
2298 	if (config.flags)
2299 		return -EINVAL;
2300 
2301 	/* device doesn't support time stamping */
2302 	if (!(mdev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_TS))
2303 		return -EINVAL;
2304 
2305 	/* TX HW timestamp */
2306 	switch (config.tx_type) {
2307 	case HWTSTAMP_TX_OFF:
2308 	case HWTSTAMP_TX_ON:
2309 		break;
2310 	default:
2311 		return -ERANGE;
2312 	}
2313 
2314 	/* RX HW timestamp */
2315 	switch (config.rx_filter) {
2316 	case HWTSTAMP_FILTER_NONE:
2317 		break;
2318 	case HWTSTAMP_FILTER_ALL:
2319 	case HWTSTAMP_FILTER_SOME:
2320 	case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
2321 	case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
2322 	case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
2323 	case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
2324 	case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
2325 	case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
2326 	case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
2327 	case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
2328 	case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
2329 	case HWTSTAMP_FILTER_PTP_V2_EVENT:
2330 	case HWTSTAMP_FILTER_PTP_V2_SYNC:
2331 	case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
2332 		config.rx_filter = HWTSTAMP_FILTER_ALL;
2333 		break;
2334 	default:
2335 		return -ERANGE;
2336 	}
2337 
2338 	if (mlx4_en_reset_config(dev, config, dev->features)) {
2339 		config.tx_type = HWTSTAMP_TX_OFF;
2340 		config.rx_filter = HWTSTAMP_FILTER_NONE;
2341 	}
2342 
2343 	return copy_to_user(ifr->ifr_data, &config,
2344 			    sizeof(config)) ? -EFAULT : 0;
2345 }
2346 
2347 static int mlx4_en_hwtstamp_get(struct net_device *dev, struct ifreq *ifr)
2348 {
2349 	struct mlx4_en_priv *priv = netdev_priv(dev);
2350 
2351 	return copy_to_user(ifr->ifr_data, &priv->hwtstamp_config,
2352 			    sizeof(priv->hwtstamp_config)) ? -EFAULT : 0;
2353 }
2354 
2355 static int mlx4_en_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
2356 {
2357 	switch (cmd) {
2358 	case SIOCSHWTSTAMP:
2359 		return mlx4_en_hwtstamp_set(dev, ifr);
2360 	case SIOCGHWTSTAMP:
2361 		return mlx4_en_hwtstamp_get(dev, ifr);
2362 	default:
2363 		return -EOPNOTSUPP;
2364 	}
2365 }
2366 
2367 static netdev_features_t mlx4_en_fix_features(struct net_device *netdev,
2368 					      netdev_features_t features)
2369 {
2370 	struct mlx4_en_priv *en_priv = netdev_priv(netdev);
2371 	struct mlx4_en_dev *mdev = en_priv->mdev;
2372 
2373 	/* Since there is no support for separate RX C-TAG/S-TAG vlan accel
2374 	 * enable/disable make sure S-TAG flag is always in same state as
2375 	 * C-TAG.
2376 	 */
2377 	if (features & NETIF_F_HW_VLAN_CTAG_RX &&
2378 	    !(mdev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_SKIP_OUTER_VLAN))
2379 		features |= NETIF_F_HW_VLAN_STAG_RX;
2380 	else
2381 		features &= ~NETIF_F_HW_VLAN_STAG_RX;
2382 
2383 	return features;
2384 }
2385 
2386 static int mlx4_en_set_features(struct net_device *netdev,
2387 		netdev_features_t features)
2388 {
2389 	struct mlx4_en_priv *priv = netdev_priv(netdev);
2390 	bool reset = false;
2391 	int ret = 0;
2392 
2393 	if (DEV_FEATURE_CHANGED(netdev, features, NETIF_F_RXFCS)) {
2394 		en_info(priv, "Turn %s RX-FCS\n",
2395 			(features & NETIF_F_RXFCS) ? "ON" : "OFF");
2396 		reset = true;
2397 	}
2398 
2399 	if (DEV_FEATURE_CHANGED(netdev, features, NETIF_F_RXALL)) {
2400 		u8 ignore_fcs_value = (features & NETIF_F_RXALL) ? 1 : 0;
2401 
2402 		en_info(priv, "Turn %s RX-ALL\n",
2403 			ignore_fcs_value ? "ON" : "OFF");
2404 		ret = mlx4_SET_PORT_fcs_check(priv->mdev->dev,
2405 					      priv->port, ignore_fcs_value);
2406 		if (ret)
2407 			return ret;
2408 	}
2409 
2410 	if (DEV_FEATURE_CHANGED(netdev, features, NETIF_F_HW_VLAN_CTAG_RX)) {
2411 		en_info(priv, "Turn %s RX vlan strip offload\n",
2412 			(features & NETIF_F_HW_VLAN_CTAG_RX) ? "ON" : "OFF");
2413 		reset = true;
2414 	}
2415 
2416 	if (DEV_FEATURE_CHANGED(netdev, features, NETIF_F_HW_VLAN_CTAG_TX))
2417 		en_info(priv, "Turn %s TX vlan strip offload\n",
2418 			(features & NETIF_F_HW_VLAN_CTAG_TX) ? "ON" : "OFF");
2419 
2420 	if (DEV_FEATURE_CHANGED(netdev, features, NETIF_F_HW_VLAN_STAG_TX))
2421 		en_info(priv, "Turn %s TX S-VLAN strip offload\n",
2422 			(features & NETIF_F_HW_VLAN_STAG_TX) ? "ON" : "OFF");
2423 
2424 	if (DEV_FEATURE_CHANGED(netdev, features, NETIF_F_LOOPBACK)) {
2425 		en_info(priv, "Turn %s loopback\n",
2426 			(features & NETIF_F_LOOPBACK) ? "ON" : "OFF");
2427 		mlx4_en_update_loopback_state(netdev, features);
2428 	}
2429 
2430 	if (reset) {
2431 		ret = mlx4_en_reset_config(netdev, priv->hwtstamp_config,
2432 					   features);
2433 		if (ret)
2434 			return ret;
2435 	}
2436 
2437 	return 0;
2438 }
2439 
2440 static int mlx4_en_set_vf_mac(struct net_device *dev, int queue, u8 *mac)
2441 {
2442 	struct mlx4_en_priv *en_priv = netdev_priv(dev);
2443 	struct mlx4_en_dev *mdev = en_priv->mdev;
2444 	u64 mac_u64 = mlx4_mac_to_u64(mac);
2445 
2446 	if (is_multicast_ether_addr(mac))
2447 		return -EINVAL;
2448 
2449 	return mlx4_set_vf_mac(mdev->dev, en_priv->port, queue, mac_u64);
2450 }
2451 
2452 static int mlx4_en_set_vf_vlan(struct net_device *dev, int vf, u16 vlan, u8 qos,
2453 			       __be16 vlan_proto)
2454 {
2455 	struct mlx4_en_priv *en_priv = netdev_priv(dev);
2456 	struct mlx4_en_dev *mdev = en_priv->mdev;
2457 
2458 	return mlx4_set_vf_vlan(mdev->dev, en_priv->port, vf, vlan, qos,
2459 				vlan_proto);
2460 }
2461 
2462 static int mlx4_en_set_vf_rate(struct net_device *dev, int vf, int min_tx_rate,
2463 			       int max_tx_rate)
2464 {
2465 	struct mlx4_en_priv *en_priv = netdev_priv(dev);
2466 	struct mlx4_en_dev *mdev = en_priv->mdev;
2467 
2468 	return mlx4_set_vf_rate(mdev->dev, en_priv->port, vf, min_tx_rate,
2469 				max_tx_rate);
2470 }
2471 
2472 static int mlx4_en_set_vf_spoofchk(struct net_device *dev, int vf, bool setting)
2473 {
2474 	struct mlx4_en_priv *en_priv = netdev_priv(dev);
2475 	struct mlx4_en_dev *mdev = en_priv->mdev;
2476 
2477 	return mlx4_set_vf_spoofchk(mdev->dev, en_priv->port, vf, setting);
2478 }
2479 
2480 static int mlx4_en_get_vf_config(struct net_device *dev, int vf, struct ifla_vf_info *ivf)
2481 {
2482 	struct mlx4_en_priv *en_priv = netdev_priv(dev);
2483 	struct mlx4_en_dev *mdev = en_priv->mdev;
2484 
2485 	return mlx4_get_vf_config(mdev->dev, en_priv->port, vf, ivf);
2486 }
2487 
2488 static int mlx4_en_set_vf_link_state(struct net_device *dev, int vf, int link_state)
2489 {
2490 	struct mlx4_en_priv *en_priv = netdev_priv(dev);
2491 	struct mlx4_en_dev *mdev = en_priv->mdev;
2492 
2493 	return mlx4_set_vf_link_state(mdev->dev, en_priv->port, vf, link_state);
2494 }
2495 
2496 static int mlx4_en_get_vf_stats(struct net_device *dev, int vf,
2497 				struct ifla_vf_stats *vf_stats)
2498 {
2499 	struct mlx4_en_priv *en_priv = netdev_priv(dev);
2500 	struct mlx4_en_dev *mdev = en_priv->mdev;
2501 
2502 	return mlx4_get_vf_stats(mdev->dev, en_priv->port, vf, vf_stats);
2503 }
2504 
2505 #define PORT_ID_BYTE_LEN 8
2506 static int mlx4_en_get_phys_port_id(struct net_device *dev,
2507 				    struct netdev_phys_item_id *ppid)
2508 {
2509 	struct mlx4_en_priv *priv = netdev_priv(dev);
2510 	struct mlx4_dev *mdev = priv->mdev->dev;
2511 	int i;
2512 	u64 phys_port_id = mdev->caps.phys_port_id[priv->port];
2513 
2514 	if (!phys_port_id)
2515 		return -EOPNOTSUPP;
2516 
2517 	ppid->id_len = sizeof(phys_port_id);
2518 	for (i = PORT_ID_BYTE_LEN - 1; i >= 0; --i) {
2519 		ppid->id[i] =  phys_port_id & 0xff;
2520 		phys_port_id >>= 8;
2521 	}
2522 	return 0;
2523 }
2524 
2525 static void mlx4_en_add_vxlan_offloads(struct work_struct *work)
2526 {
2527 	int ret;
2528 	struct mlx4_en_priv *priv = container_of(work, struct mlx4_en_priv,
2529 						 vxlan_add_task);
2530 
2531 	ret = mlx4_config_vxlan_port(priv->mdev->dev, priv->vxlan_port);
2532 	if (ret)
2533 		goto out;
2534 
2535 	ret = mlx4_SET_PORT_VXLAN(priv->mdev->dev, priv->port,
2536 				  VXLAN_STEER_BY_OUTER_MAC, 1);
2537 out:
2538 	if (ret) {
2539 		en_err(priv, "failed setting L2 tunnel configuration ret %d\n", ret);
2540 		return;
2541 	}
2542 
2543 	/* set offloads */
2544 	priv->dev->hw_enc_features |= NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
2545 				      NETIF_F_RXCSUM |
2546 				      NETIF_F_TSO | NETIF_F_TSO6 |
2547 				      NETIF_F_GSO_UDP_TUNNEL |
2548 				      NETIF_F_GSO_UDP_TUNNEL_CSUM |
2549 				      NETIF_F_GSO_PARTIAL;
2550 }
2551 
2552 static void mlx4_en_del_vxlan_offloads(struct work_struct *work)
2553 {
2554 	int ret;
2555 	struct mlx4_en_priv *priv = container_of(work, struct mlx4_en_priv,
2556 						 vxlan_del_task);
2557 	/* unset offloads */
2558 	priv->dev->hw_enc_features &= ~(NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
2559 					NETIF_F_RXCSUM |
2560 					NETIF_F_TSO | NETIF_F_TSO6 |
2561 					NETIF_F_GSO_UDP_TUNNEL |
2562 					NETIF_F_GSO_UDP_TUNNEL_CSUM |
2563 					NETIF_F_GSO_PARTIAL);
2564 
2565 	ret = mlx4_SET_PORT_VXLAN(priv->mdev->dev, priv->port,
2566 				  VXLAN_STEER_BY_OUTER_MAC, 0);
2567 	if (ret)
2568 		en_err(priv, "failed setting L2 tunnel configuration ret %d\n", ret);
2569 
2570 	priv->vxlan_port = 0;
2571 }
2572 
2573 static void mlx4_en_add_vxlan_port(struct  net_device *dev,
2574 				   struct udp_tunnel_info *ti)
2575 {
2576 	struct mlx4_en_priv *priv = netdev_priv(dev);
2577 	__be16 port = ti->port;
2578 	__be16 current_port;
2579 
2580 	if (ti->type != UDP_TUNNEL_TYPE_VXLAN)
2581 		return;
2582 
2583 	if (ti->sa_family != AF_INET)
2584 		return;
2585 
2586 	if (priv->mdev->dev->caps.tunnel_offload_mode != MLX4_TUNNEL_OFFLOAD_MODE_VXLAN)
2587 		return;
2588 
2589 	current_port = priv->vxlan_port;
2590 	if (current_port && current_port != port) {
2591 		en_warn(priv, "vxlan port %d configured, can't add port %d\n",
2592 			ntohs(current_port), ntohs(port));
2593 		return;
2594 	}
2595 
2596 	priv->vxlan_port = port;
2597 	queue_work(priv->mdev->workqueue, &priv->vxlan_add_task);
2598 }
2599 
2600 static void mlx4_en_del_vxlan_port(struct  net_device *dev,
2601 				   struct udp_tunnel_info *ti)
2602 {
2603 	struct mlx4_en_priv *priv = netdev_priv(dev);
2604 	__be16 port = ti->port;
2605 	__be16 current_port;
2606 
2607 	if (ti->type != UDP_TUNNEL_TYPE_VXLAN)
2608 		return;
2609 
2610 	if (ti->sa_family != AF_INET)
2611 		return;
2612 
2613 	if (priv->mdev->dev->caps.tunnel_offload_mode != MLX4_TUNNEL_OFFLOAD_MODE_VXLAN)
2614 		return;
2615 
2616 	current_port = priv->vxlan_port;
2617 	if (current_port != port) {
2618 		en_dbg(DRV, priv, "vxlan port %d isn't configured, ignoring\n", ntohs(port));
2619 		return;
2620 	}
2621 
2622 	queue_work(priv->mdev->workqueue, &priv->vxlan_del_task);
2623 }
2624 
2625 static netdev_features_t mlx4_en_features_check(struct sk_buff *skb,
2626 						struct net_device *dev,
2627 						netdev_features_t features)
2628 {
2629 	features = vlan_features_check(skb, features);
2630 	features = vxlan_features_check(skb, features);
2631 
2632 	/* The ConnectX-3 doesn't support outer IPv6 checksums but it does
2633 	 * support inner IPv6 checksums and segmentation so  we need to
2634 	 * strip that feature if this is an IPv6 encapsulated frame.
2635 	 */
2636 	if (skb->encapsulation &&
2637 	    (skb->ip_summed == CHECKSUM_PARTIAL)) {
2638 		struct mlx4_en_priv *priv = netdev_priv(dev);
2639 
2640 		if (!priv->vxlan_port ||
2641 		    (ip_hdr(skb)->version != 4) ||
2642 		    (udp_hdr(skb)->dest != priv->vxlan_port))
2643 			features &= ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
2644 	}
2645 
2646 	return features;
2647 }
2648 
2649 static int mlx4_en_set_tx_maxrate(struct net_device *dev, int queue_index, u32 maxrate)
2650 {
2651 	struct mlx4_en_priv *priv = netdev_priv(dev);
2652 	struct mlx4_en_tx_ring *tx_ring = priv->tx_ring[TX][queue_index];
2653 	struct mlx4_update_qp_params params;
2654 	int err;
2655 
2656 	if (!(priv->mdev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_QP_RATE_LIMIT))
2657 		return -EOPNOTSUPP;
2658 
2659 	/* rate provided to us in Mbs, check if it fits into 12 bits, if not use Gbs */
2660 	if (maxrate >> 12) {
2661 		params.rate_unit = MLX4_QP_RATE_LIMIT_GBS;
2662 		params.rate_val  = maxrate / 1000;
2663 	} else if (maxrate) {
2664 		params.rate_unit = MLX4_QP_RATE_LIMIT_MBS;
2665 		params.rate_val  = maxrate;
2666 	} else { /* zero serves to revoke the QP rate-limitation */
2667 		params.rate_unit = 0;
2668 		params.rate_val  = 0;
2669 	}
2670 
2671 	err = mlx4_update_qp(priv->mdev->dev, tx_ring->qpn, MLX4_UPDATE_QP_RATE_LIMIT,
2672 			     &params);
2673 	return err;
2674 }
2675 
2676 static int mlx4_xdp_set(struct net_device *dev, struct bpf_prog *prog)
2677 {
2678 	struct mlx4_en_priv *priv = netdev_priv(dev);
2679 	struct mlx4_en_dev *mdev = priv->mdev;
2680 	struct mlx4_en_port_profile new_prof;
2681 	struct bpf_prog *old_prog;
2682 	struct mlx4_en_priv *tmp;
2683 	int tx_changed = 0;
2684 	int xdp_ring_num;
2685 	int port_up = 0;
2686 	int err;
2687 	int i;
2688 
2689 	xdp_ring_num = prog ? priv->rx_ring_num : 0;
2690 
2691 	/* No need to reconfigure buffers when simply swapping the
2692 	 * program for a new one.
2693 	 */
2694 	if (priv->tx_ring_num[TX_XDP] == xdp_ring_num) {
2695 		if (prog) {
2696 			prog = bpf_prog_add(prog, priv->rx_ring_num - 1);
2697 			if (IS_ERR(prog))
2698 				return PTR_ERR(prog);
2699 		}
2700 		mutex_lock(&mdev->state_lock);
2701 		for (i = 0; i < priv->rx_ring_num; i++) {
2702 			old_prog = rcu_dereference_protected(
2703 					priv->rx_ring[i]->xdp_prog,
2704 					lockdep_is_held(&mdev->state_lock));
2705 			rcu_assign_pointer(priv->rx_ring[i]->xdp_prog, prog);
2706 			if (old_prog)
2707 				bpf_prog_put(old_prog);
2708 		}
2709 		mutex_unlock(&mdev->state_lock);
2710 		return 0;
2711 	}
2712 
2713 	if (priv->num_frags > 1) {
2714 		en_err(priv, "Cannot set XDP if MTU requires multiple frags\n");
2715 		return -EOPNOTSUPP;
2716 	}
2717 
2718 	tmp = kzalloc(sizeof(*tmp), GFP_KERNEL);
2719 	if (!tmp)
2720 		return -ENOMEM;
2721 
2722 	if (prog) {
2723 		prog = bpf_prog_add(prog, priv->rx_ring_num - 1);
2724 		if (IS_ERR(prog)) {
2725 			err = PTR_ERR(prog);
2726 			goto out;
2727 		}
2728 	}
2729 
2730 	mutex_lock(&mdev->state_lock);
2731 	memcpy(&new_prof, priv->prof, sizeof(struct mlx4_en_port_profile));
2732 	new_prof.tx_ring_num[TX_XDP] = xdp_ring_num;
2733 
2734 	if (priv->tx_ring_num[TX] + xdp_ring_num > MAX_TX_RINGS) {
2735 		tx_changed = 1;
2736 		new_prof.tx_ring_num[TX] =
2737 			MAX_TX_RINGS - ALIGN(xdp_ring_num, MLX4_EN_NUM_UP);
2738 		en_warn(priv, "Reducing the number of TX rings, to not exceed the max total rings number.\n");
2739 	}
2740 
2741 	err = mlx4_en_try_alloc_resources(priv, tmp, &new_prof);
2742 	if (err) {
2743 		if (prog)
2744 			bpf_prog_sub(prog, priv->rx_ring_num - 1);
2745 		goto unlock_out;
2746 	}
2747 
2748 	if (priv->port_up) {
2749 		port_up = 1;
2750 		mlx4_en_stop_port(dev, 1);
2751 	}
2752 
2753 	mlx4_en_safe_replace_resources(priv, tmp);
2754 	if (tx_changed)
2755 		netif_set_real_num_tx_queues(dev, priv->tx_ring_num[TX]);
2756 
2757 	for (i = 0; i < priv->rx_ring_num; i++) {
2758 		old_prog = rcu_dereference_protected(
2759 					priv->rx_ring[i]->xdp_prog,
2760 					lockdep_is_held(&mdev->state_lock));
2761 		rcu_assign_pointer(priv->rx_ring[i]->xdp_prog, prog);
2762 		if (old_prog)
2763 			bpf_prog_put(old_prog);
2764 	}
2765 
2766 	if (port_up) {
2767 		err = mlx4_en_start_port(dev);
2768 		if (err) {
2769 			en_err(priv, "Failed starting port %d for XDP change\n",
2770 			       priv->port);
2771 			queue_work(mdev->workqueue, &priv->watchdog_task);
2772 		}
2773 	}
2774 
2775 unlock_out:
2776 	mutex_unlock(&mdev->state_lock);
2777 out:
2778 	kfree(tmp);
2779 	return err;
2780 }
2781 
2782 static bool mlx4_xdp_attached(struct net_device *dev)
2783 {
2784 	struct mlx4_en_priv *priv = netdev_priv(dev);
2785 
2786 	return !!priv->tx_ring_num[TX_XDP];
2787 }
2788 
2789 static int mlx4_xdp(struct net_device *dev, struct netdev_xdp *xdp)
2790 {
2791 	switch (xdp->command) {
2792 	case XDP_SETUP_PROG:
2793 		return mlx4_xdp_set(dev, xdp->prog);
2794 	case XDP_QUERY_PROG:
2795 		xdp->prog_attached = mlx4_xdp_attached(dev);
2796 		return 0;
2797 	default:
2798 		return -EINVAL;
2799 	}
2800 }
2801 
2802 static const struct net_device_ops mlx4_netdev_ops = {
2803 	.ndo_open		= mlx4_en_open,
2804 	.ndo_stop		= mlx4_en_close,
2805 	.ndo_start_xmit		= mlx4_en_xmit,
2806 	.ndo_select_queue	= mlx4_en_select_queue,
2807 	.ndo_get_stats64	= mlx4_en_get_stats64,
2808 	.ndo_set_rx_mode	= mlx4_en_set_rx_mode,
2809 	.ndo_set_mac_address	= mlx4_en_set_mac,
2810 	.ndo_validate_addr	= eth_validate_addr,
2811 	.ndo_change_mtu		= mlx4_en_change_mtu,
2812 	.ndo_do_ioctl		= mlx4_en_ioctl,
2813 	.ndo_tx_timeout		= mlx4_en_tx_timeout,
2814 	.ndo_vlan_rx_add_vid	= mlx4_en_vlan_rx_add_vid,
2815 	.ndo_vlan_rx_kill_vid	= mlx4_en_vlan_rx_kill_vid,
2816 #ifdef CONFIG_NET_POLL_CONTROLLER
2817 	.ndo_poll_controller	= mlx4_en_netpoll,
2818 #endif
2819 	.ndo_set_features	= mlx4_en_set_features,
2820 	.ndo_fix_features	= mlx4_en_fix_features,
2821 	.ndo_setup_tc		= __mlx4_en_setup_tc,
2822 #ifdef CONFIG_RFS_ACCEL
2823 	.ndo_rx_flow_steer	= mlx4_en_filter_rfs,
2824 #endif
2825 	.ndo_get_phys_port_id	= mlx4_en_get_phys_port_id,
2826 	.ndo_udp_tunnel_add	= mlx4_en_add_vxlan_port,
2827 	.ndo_udp_tunnel_del	= mlx4_en_del_vxlan_port,
2828 	.ndo_features_check	= mlx4_en_features_check,
2829 	.ndo_set_tx_maxrate	= mlx4_en_set_tx_maxrate,
2830 	.ndo_xdp		= mlx4_xdp,
2831 };
2832 
2833 static const struct net_device_ops mlx4_netdev_ops_master = {
2834 	.ndo_open		= mlx4_en_open,
2835 	.ndo_stop		= mlx4_en_close,
2836 	.ndo_start_xmit		= mlx4_en_xmit,
2837 	.ndo_select_queue	= mlx4_en_select_queue,
2838 	.ndo_get_stats64	= mlx4_en_get_stats64,
2839 	.ndo_set_rx_mode	= mlx4_en_set_rx_mode,
2840 	.ndo_set_mac_address	= mlx4_en_set_mac,
2841 	.ndo_validate_addr	= eth_validate_addr,
2842 	.ndo_change_mtu		= mlx4_en_change_mtu,
2843 	.ndo_tx_timeout		= mlx4_en_tx_timeout,
2844 	.ndo_vlan_rx_add_vid	= mlx4_en_vlan_rx_add_vid,
2845 	.ndo_vlan_rx_kill_vid	= mlx4_en_vlan_rx_kill_vid,
2846 	.ndo_set_vf_mac		= mlx4_en_set_vf_mac,
2847 	.ndo_set_vf_vlan	= mlx4_en_set_vf_vlan,
2848 	.ndo_set_vf_rate	= mlx4_en_set_vf_rate,
2849 	.ndo_set_vf_spoofchk	= mlx4_en_set_vf_spoofchk,
2850 	.ndo_set_vf_link_state	= mlx4_en_set_vf_link_state,
2851 	.ndo_get_vf_stats       = mlx4_en_get_vf_stats,
2852 	.ndo_get_vf_config	= mlx4_en_get_vf_config,
2853 #ifdef CONFIG_NET_POLL_CONTROLLER
2854 	.ndo_poll_controller	= mlx4_en_netpoll,
2855 #endif
2856 	.ndo_set_features	= mlx4_en_set_features,
2857 	.ndo_fix_features	= mlx4_en_fix_features,
2858 	.ndo_setup_tc		= __mlx4_en_setup_tc,
2859 #ifdef CONFIG_RFS_ACCEL
2860 	.ndo_rx_flow_steer	= mlx4_en_filter_rfs,
2861 #endif
2862 	.ndo_get_phys_port_id	= mlx4_en_get_phys_port_id,
2863 	.ndo_udp_tunnel_add	= mlx4_en_add_vxlan_port,
2864 	.ndo_udp_tunnel_del	= mlx4_en_del_vxlan_port,
2865 	.ndo_features_check	= mlx4_en_features_check,
2866 	.ndo_set_tx_maxrate	= mlx4_en_set_tx_maxrate,
2867 	.ndo_xdp		= mlx4_xdp,
2868 };
2869 
2870 struct mlx4_en_bond {
2871 	struct work_struct work;
2872 	struct mlx4_en_priv *priv;
2873 	int is_bonded;
2874 	struct mlx4_port_map port_map;
2875 };
2876 
2877 static void mlx4_en_bond_work(struct work_struct *work)
2878 {
2879 	struct mlx4_en_bond *bond = container_of(work,
2880 						     struct mlx4_en_bond,
2881 						     work);
2882 	int err = 0;
2883 	struct mlx4_dev *dev = bond->priv->mdev->dev;
2884 
2885 	if (bond->is_bonded) {
2886 		if (!mlx4_is_bonded(dev)) {
2887 			err = mlx4_bond(dev);
2888 			if (err)
2889 				en_err(bond->priv, "Fail to bond device\n");
2890 		}
2891 		if (!err) {
2892 			err = mlx4_port_map_set(dev, &bond->port_map);
2893 			if (err)
2894 				en_err(bond->priv, "Fail to set port map [%d][%d]: %d\n",
2895 				       bond->port_map.port1,
2896 				       bond->port_map.port2,
2897 				       err);
2898 		}
2899 	} else if (mlx4_is_bonded(dev)) {
2900 		err = mlx4_unbond(dev);
2901 		if (err)
2902 			en_err(bond->priv, "Fail to unbond device\n");
2903 	}
2904 	dev_put(bond->priv->dev);
2905 	kfree(bond);
2906 }
2907 
2908 static int mlx4_en_queue_bond_work(struct mlx4_en_priv *priv, int is_bonded,
2909 				   u8 v2p_p1, u8 v2p_p2)
2910 {
2911 	struct mlx4_en_bond *bond = NULL;
2912 
2913 	bond = kzalloc(sizeof(*bond), GFP_ATOMIC);
2914 	if (!bond)
2915 		return -ENOMEM;
2916 
2917 	INIT_WORK(&bond->work, mlx4_en_bond_work);
2918 	bond->priv = priv;
2919 	bond->is_bonded = is_bonded;
2920 	bond->port_map.port1 = v2p_p1;
2921 	bond->port_map.port2 = v2p_p2;
2922 	dev_hold(priv->dev);
2923 	queue_work(priv->mdev->workqueue, &bond->work);
2924 	return 0;
2925 }
2926 
2927 int mlx4_en_netdev_event(struct notifier_block *this,
2928 			 unsigned long event, void *ptr)
2929 {
2930 	struct net_device *ndev = netdev_notifier_info_to_dev(ptr);
2931 	u8 port = 0;
2932 	struct mlx4_en_dev *mdev;
2933 	struct mlx4_dev *dev;
2934 	int i, num_eth_ports = 0;
2935 	bool do_bond = true;
2936 	struct mlx4_en_priv *priv;
2937 	u8 v2p_port1 = 0;
2938 	u8 v2p_port2 = 0;
2939 
2940 	if (!net_eq(dev_net(ndev), &init_net))
2941 		return NOTIFY_DONE;
2942 
2943 	mdev = container_of(this, struct mlx4_en_dev, nb);
2944 	dev = mdev->dev;
2945 
2946 	/* Go into this mode only when two network devices set on two ports
2947 	 * of the same mlx4 device are slaves of the same bonding master
2948 	 */
2949 	mlx4_foreach_port(i, dev, MLX4_PORT_TYPE_ETH) {
2950 		++num_eth_ports;
2951 		if (!port && (mdev->pndev[i] == ndev))
2952 			port = i;
2953 		mdev->upper[i] = mdev->pndev[i] ?
2954 			netdev_master_upper_dev_get(mdev->pndev[i]) : NULL;
2955 		/* condition not met: network device is a slave */
2956 		if (!mdev->upper[i])
2957 			do_bond = false;
2958 		if (num_eth_ports < 2)
2959 			continue;
2960 		/* condition not met: same master */
2961 		if (mdev->upper[i] != mdev->upper[i-1])
2962 			do_bond = false;
2963 	}
2964 	/* condition not met: 2 salves */
2965 	do_bond = (num_eth_ports ==  2) ? do_bond : false;
2966 
2967 	/* handle only events that come with enough info */
2968 	if ((do_bond && (event != NETDEV_BONDING_INFO)) || !port)
2969 		return NOTIFY_DONE;
2970 
2971 	priv = netdev_priv(ndev);
2972 	if (do_bond) {
2973 		struct netdev_notifier_bonding_info *notifier_info = ptr;
2974 		struct netdev_bonding_info *bonding_info =
2975 			&notifier_info->bonding_info;
2976 
2977 		/* required mode 1, 2 or 4 */
2978 		if ((bonding_info->master.bond_mode != BOND_MODE_ACTIVEBACKUP) &&
2979 		    (bonding_info->master.bond_mode != BOND_MODE_XOR) &&
2980 		    (bonding_info->master.bond_mode != BOND_MODE_8023AD))
2981 			do_bond = false;
2982 
2983 		/* require exactly 2 slaves */
2984 		if (bonding_info->master.num_slaves != 2)
2985 			do_bond = false;
2986 
2987 		/* calc v2p */
2988 		if (do_bond) {
2989 			if (bonding_info->master.bond_mode ==
2990 			    BOND_MODE_ACTIVEBACKUP) {
2991 				/* in active-backup mode virtual ports are
2992 				 * mapped to the physical port of the active
2993 				 * slave */
2994 				if (bonding_info->slave.state ==
2995 				    BOND_STATE_BACKUP) {
2996 					if (port == 1) {
2997 						v2p_port1 = 2;
2998 						v2p_port2 = 2;
2999 					} else {
3000 						v2p_port1 = 1;
3001 						v2p_port2 = 1;
3002 					}
3003 				} else { /* BOND_STATE_ACTIVE */
3004 					if (port == 1) {
3005 						v2p_port1 = 1;
3006 						v2p_port2 = 1;
3007 					} else {
3008 						v2p_port1 = 2;
3009 						v2p_port2 = 2;
3010 					}
3011 				}
3012 			} else { /* Active-Active */
3013 				/* in active-active mode a virtual port is
3014 				 * mapped to the native physical port if and only
3015 				 * if the physical port is up */
3016 				__s8 link = bonding_info->slave.link;
3017 
3018 				if (port == 1)
3019 					v2p_port2 = 2;
3020 				else
3021 					v2p_port1 = 1;
3022 				if ((link == BOND_LINK_UP) ||
3023 				    (link == BOND_LINK_FAIL)) {
3024 					if (port == 1)
3025 						v2p_port1 = 1;
3026 					else
3027 						v2p_port2 = 2;
3028 				} else { /* BOND_LINK_DOWN || BOND_LINK_BACK */
3029 					if (port == 1)
3030 						v2p_port1 = 2;
3031 					else
3032 						v2p_port2 = 1;
3033 				}
3034 			}
3035 		}
3036 	}
3037 
3038 	mlx4_en_queue_bond_work(priv, do_bond,
3039 				v2p_port1, v2p_port2);
3040 
3041 	return NOTIFY_DONE;
3042 }
3043 
3044 void mlx4_en_update_pfc_stats_bitmap(struct mlx4_dev *dev,
3045 				     struct mlx4_en_stats_bitmap *stats_bitmap,
3046 				     u8 rx_ppp, u8 rx_pause,
3047 				     u8 tx_ppp, u8 tx_pause)
3048 {
3049 	int last_i = NUM_MAIN_STATS + NUM_PORT_STATS + NUM_PF_STATS;
3050 
3051 	if (!mlx4_is_slave(dev) &&
3052 	    (dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_FLOWSTATS_EN)) {
3053 		mutex_lock(&stats_bitmap->mutex);
3054 		bitmap_clear(stats_bitmap->bitmap, last_i, NUM_FLOW_STATS);
3055 
3056 		if (rx_ppp)
3057 			bitmap_set(stats_bitmap->bitmap, last_i,
3058 				   NUM_FLOW_PRIORITY_STATS_RX);
3059 		last_i += NUM_FLOW_PRIORITY_STATS_RX;
3060 
3061 		if (rx_pause && !(rx_ppp))
3062 			bitmap_set(stats_bitmap->bitmap, last_i,
3063 				   NUM_FLOW_STATS_RX);
3064 		last_i += NUM_FLOW_STATS_RX;
3065 
3066 		if (tx_ppp)
3067 			bitmap_set(stats_bitmap->bitmap, last_i,
3068 				   NUM_FLOW_PRIORITY_STATS_TX);
3069 		last_i += NUM_FLOW_PRIORITY_STATS_TX;
3070 
3071 		if (tx_pause && !(tx_ppp))
3072 			bitmap_set(stats_bitmap->bitmap, last_i,
3073 				   NUM_FLOW_STATS_TX);
3074 		last_i += NUM_FLOW_STATS_TX;
3075 
3076 		mutex_unlock(&stats_bitmap->mutex);
3077 	}
3078 }
3079 
3080 void mlx4_en_set_stats_bitmap(struct mlx4_dev *dev,
3081 			      struct mlx4_en_stats_bitmap *stats_bitmap,
3082 			      u8 rx_ppp, u8 rx_pause,
3083 			      u8 tx_ppp, u8 tx_pause)
3084 {
3085 	int last_i = 0;
3086 
3087 	mutex_init(&stats_bitmap->mutex);
3088 	bitmap_zero(stats_bitmap->bitmap, NUM_ALL_STATS);
3089 
3090 	if (mlx4_is_slave(dev)) {
3091 		bitmap_set(stats_bitmap->bitmap, last_i +
3092 					 MLX4_FIND_NETDEV_STAT(rx_packets), 1);
3093 		bitmap_set(stats_bitmap->bitmap, last_i +
3094 					 MLX4_FIND_NETDEV_STAT(tx_packets), 1);
3095 		bitmap_set(stats_bitmap->bitmap, last_i +
3096 					 MLX4_FIND_NETDEV_STAT(rx_bytes), 1);
3097 		bitmap_set(stats_bitmap->bitmap, last_i +
3098 					 MLX4_FIND_NETDEV_STAT(tx_bytes), 1);
3099 		bitmap_set(stats_bitmap->bitmap, last_i +
3100 					 MLX4_FIND_NETDEV_STAT(rx_dropped), 1);
3101 		bitmap_set(stats_bitmap->bitmap, last_i +
3102 					 MLX4_FIND_NETDEV_STAT(tx_dropped), 1);
3103 	} else {
3104 		bitmap_set(stats_bitmap->bitmap, last_i, NUM_MAIN_STATS);
3105 	}
3106 	last_i += NUM_MAIN_STATS;
3107 
3108 	bitmap_set(stats_bitmap->bitmap, last_i, NUM_PORT_STATS);
3109 	last_i += NUM_PORT_STATS;
3110 
3111 	if (mlx4_is_master(dev))
3112 		bitmap_set(stats_bitmap->bitmap, last_i,
3113 			   NUM_PF_STATS);
3114 	last_i += NUM_PF_STATS;
3115 
3116 	mlx4_en_update_pfc_stats_bitmap(dev, stats_bitmap,
3117 					rx_ppp, rx_pause,
3118 					tx_ppp, tx_pause);
3119 	last_i += NUM_FLOW_STATS;
3120 
3121 	if (!mlx4_is_slave(dev))
3122 		bitmap_set(stats_bitmap->bitmap, last_i, NUM_PKT_STATS);
3123 	last_i += NUM_PKT_STATS;
3124 
3125 	bitmap_set(stats_bitmap->bitmap, last_i, NUM_XDP_STATS);
3126 	last_i += NUM_XDP_STATS;
3127 }
3128 
3129 int mlx4_en_init_netdev(struct mlx4_en_dev *mdev, int port,
3130 			struct mlx4_en_port_profile *prof)
3131 {
3132 	struct net_device *dev;
3133 	struct mlx4_en_priv *priv;
3134 	int i, t;
3135 	int err;
3136 
3137 	dev = alloc_etherdev_mqs(sizeof(struct mlx4_en_priv),
3138 				 MAX_TX_RINGS, MAX_RX_RINGS);
3139 	if (dev == NULL)
3140 		return -ENOMEM;
3141 
3142 	netif_set_real_num_tx_queues(dev, prof->tx_ring_num[TX]);
3143 	netif_set_real_num_rx_queues(dev, prof->rx_ring_num);
3144 
3145 	SET_NETDEV_DEV(dev, &mdev->dev->persist->pdev->dev);
3146 	dev->dev_port = port - 1;
3147 
3148 	/*
3149 	 * Initialize driver private data
3150 	 */
3151 
3152 	priv = netdev_priv(dev);
3153 	memset(priv, 0, sizeof(struct mlx4_en_priv));
3154 	priv->counter_index = MLX4_SINK_COUNTER_INDEX(mdev->dev);
3155 	spin_lock_init(&priv->stats_lock);
3156 	INIT_WORK(&priv->rx_mode_task, mlx4_en_do_set_rx_mode);
3157 	INIT_WORK(&priv->watchdog_task, mlx4_en_restart);
3158 	INIT_WORK(&priv->linkstate_task, mlx4_en_linkstate);
3159 	INIT_DELAYED_WORK(&priv->stats_task, mlx4_en_do_get_stats);
3160 	INIT_DELAYED_WORK(&priv->service_task, mlx4_en_service_task);
3161 	INIT_WORK(&priv->vxlan_add_task, mlx4_en_add_vxlan_offloads);
3162 	INIT_WORK(&priv->vxlan_del_task, mlx4_en_del_vxlan_offloads);
3163 #ifdef CONFIG_RFS_ACCEL
3164 	INIT_LIST_HEAD(&priv->filters);
3165 	spin_lock_init(&priv->filters_lock);
3166 #endif
3167 
3168 	priv->dev = dev;
3169 	priv->mdev = mdev;
3170 	priv->ddev = &mdev->pdev->dev;
3171 	priv->prof = prof;
3172 	priv->port = port;
3173 	priv->port_up = false;
3174 	priv->flags = prof->flags;
3175 	priv->pflags = MLX4_EN_PRIV_FLAGS_BLUEFLAME;
3176 	priv->ctrl_flags = cpu_to_be32(MLX4_WQE_CTRL_CQ_UPDATE |
3177 			MLX4_WQE_CTRL_SOLICITED);
3178 	priv->num_tx_rings_p_up = mdev->profile.num_tx_rings_p_up;
3179 	priv->tx_work_limit = MLX4_EN_DEFAULT_TX_WORK;
3180 	netdev_rss_key_fill(priv->rss_key, sizeof(priv->rss_key));
3181 
3182 	for (t = 0; t < MLX4_EN_NUM_TX_TYPES; t++) {
3183 		priv->tx_ring_num[t] = prof->tx_ring_num[t];
3184 		if (!priv->tx_ring_num[t])
3185 			continue;
3186 
3187 		priv->tx_ring[t] = kzalloc(sizeof(struct mlx4_en_tx_ring *) *
3188 					   MAX_TX_RINGS, GFP_KERNEL);
3189 		if (!priv->tx_ring[t]) {
3190 			err = -ENOMEM;
3191 			goto err_free_tx;
3192 		}
3193 		priv->tx_cq[t] = kzalloc(sizeof(struct mlx4_en_cq *) *
3194 					 MAX_TX_RINGS, GFP_KERNEL);
3195 		if (!priv->tx_cq[t]) {
3196 			kfree(priv->tx_ring[t]);
3197 			err = -ENOMEM;
3198 			goto out;
3199 		}
3200 	}
3201 	priv->rx_ring_num = prof->rx_ring_num;
3202 	priv->cqe_factor = (mdev->dev->caps.cqe_size == 64) ? 1 : 0;
3203 	priv->cqe_size = mdev->dev->caps.cqe_size;
3204 	priv->mac_index = -1;
3205 	priv->msg_enable = MLX4_EN_MSG_LEVEL;
3206 #ifdef CONFIG_MLX4_EN_DCB
3207 	if (!mlx4_is_slave(priv->mdev->dev)) {
3208 		priv->dcbx_cap = DCB_CAP_DCBX_VER_CEE | DCB_CAP_DCBX_HOST |
3209 			DCB_CAP_DCBX_VER_IEEE;
3210 		priv->flags |= MLX4_EN_DCB_ENABLED;
3211 		priv->cee_config.pfc_state = false;
3212 
3213 		for (i = 0; i < MLX4_EN_NUM_UP; i++)
3214 			priv->cee_config.dcb_pfc[i] = pfc_disabled;
3215 
3216 		if (mdev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_ETS_CFG) {
3217 			dev->dcbnl_ops = &mlx4_en_dcbnl_ops;
3218 		} else {
3219 			en_info(priv, "enabling only PFC DCB ops\n");
3220 			dev->dcbnl_ops = &mlx4_en_dcbnl_pfc_ops;
3221 		}
3222 	}
3223 #endif
3224 
3225 	for (i = 0; i < MLX4_EN_MAC_HASH_SIZE; ++i)
3226 		INIT_HLIST_HEAD(&priv->mac_hash[i]);
3227 
3228 	/* Query for default mac and max mtu */
3229 	priv->max_mtu = mdev->dev->caps.eth_mtu_cap[priv->port];
3230 
3231 	if (mdev->dev->caps.rx_checksum_flags_port[priv->port] &
3232 	    MLX4_RX_CSUM_MODE_VAL_NON_TCP_UDP)
3233 		priv->flags |= MLX4_EN_FLAG_RX_CSUM_NON_TCP_UDP;
3234 
3235 	/* Set default MAC */
3236 	dev->addr_len = ETH_ALEN;
3237 	mlx4_en_u64_to_mac(dev->dev_addr, mdev->dev->caps.def_mac[priv->port]);
3238 	if (!is_valid_ether_addr(dev->dev_addr)) {
3239 		en_err(priv, "Port: %d, invalid mac burned: %pM, quiting\n",
3240 		       priv->port, dev->dev_addr);
3241 		err = -EINVAL;
3242 		goto out;
3243 	} else if (mlx4_is_slave(priv->mdev->dev) &&
3244 		   (priv->mdev->dev->port_random_macs & 1 << priv->port)) {
3245 		/* Random MAC was assigned in mlx4_slave_cap
3246 		 * in mlx4_core module
3247 		 */
3248 		dev->addr_assign_type |= NET_ADDR_RANDOM;
3249 		en_warn(priv, "Assigned random MAC address %pM\n", dev->dev_addr);
3250 	}
3251 
3252 	memcpy(priv->current_mac, dev->dev_addr, sizeof(priv->current_mac));
3253 
3254 	priv->stride = roundup_pow_of_two(sizeof(struct mlx4_en_rx_desc) +
3255 					  DS_SIZE * MLX4_EN_MAX_RX_FRAGS);
3256 	err = mlx4_en_alloc_resources(priv);
3257 	if (err)
3258 		goto out;
3259 
3260 	/* Initialize time stamping config */
3261 	priv->hwtstamp_config.flags = 0;
3262 	priv->hwtstamp_config.tx_type = HWTSTAMP_TX_OFF;
3263 	priv->hwtstamp_config.rx_filter = HWTSTAMP_FILTER_NONE;
3264 
3265 	/* Allocate page for receive rings */
3266 	err = mlx4_alloc_hwq_res(mdev->dev, &priv->res,
3267 				MLX4_EN_PAGE_SIZE);
3268 	if (err) {
3269 		en_err(priv, "Failed to allocate page for rx qps\n");
3270 		goto out;
3271 	}
3272 	priv->allocated = 1;
3273 
3274 	/*
3275 	 * Initialize netdev entry points
3276 	 */
3277 	if (mlx4_is_master(priv->mdev->dev))
3278 		dev->netdev_ops = &mlx4_netdev_ops_master;
3279 	else
3280 		dev->netdev_ops = &mlx4_netdev_ops;
3281 	dev->watchdog_timeo = MLX4_EN_WATCHDOG_TIMEOUT;
3282 	netif_set_real_num_tx_queues(dev, priv->tx_ring_num[TX]);
3283 	netif_set_real_num_rx_queues(dev, priv->rx_ring_num);
3284 
3285 	dev->ethtool_ops = &mlx4_en_ethtool_ops;
3286 
3287 	/*
3288 	 * Set driver features
3289 	 */
3290 	dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM;
3291 	if (mdev->LSO_support)
3292 		dev->hw_features |= NETIF_F_TSO | NETIF_F_TSO6;
3293 
3294 	dev->vlan_features = dev->hw_features;
3295 
3296 	dev->hw_features |= NETIF_F_RXCSUM | NETIF_F_RXHASH;
3297 	dev->features = dev->hw_features | NETIF_F_HIGHDMA |
3298 			NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX |
3299 			NETIF_F_HW_VLAN_CTAG_FILTER;
3300 	dev->hw_features |= NETIF_F_LOOPBACK |
3301 			NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX;
3302 
3303 	if (!(mdev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_SKIP_OUTER_VLAN)) {
3304 		dev->features |= NETIF_F_HW_VLAN_STAG_RX |
3305 			NETIF_F_HW_VLAN_STAG_FILTER;
3306 		dev->hw_features |= NETIF_F_HW_VLAN_STAG_RX;
3307 	}
3308 
3309 	if (mlx4_is_slave(mdev->dev)) {
3310 		bool vlan_offload_disabled;
3311 		int phv;
3312 
3313 		err = get_phv_bit(mdev->dev, port, &phv);
3314 		if (!err && phv) {
3315 			dev->hw_features |= NETIF_F_HW_VLAN_STAG_TX;
3316 			priv->pflags |= MLX4_EN_PRIV_FLAGS_PHV;
3317 		}
3318 		err = mlx4_get_is_vlan_offload_disabled(mdev->dev, port,
3319 							&vlan_offload_disabled);
3320 		if (!err && vlan_offload_disabled) {
3321 			dev->hw_features &= ~(NETIF_F_HW_VLAN_CTAG_TX |
3322 					      NETIF_F_HW_VLAN_CTAG_RX |
3323 					      NETIF_F_HW_VLAN_STAG_TX |
3324 					      NETIF_F_HW_VLAN_STAG_RX);
3325 			dev->features &= ~(NETIF_F_HW_VLAN_CTAG_TX |
3326 					   NETIF_F_HW_VLAN_CTAG_RX |
3327 					   NETIF_F_HW_VLAN_STAG_TX |
3328 					   NETIF_F_HW_VLAN_STAG_RX);
3329 		}
3330 	} else {
3331 		if (mdev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_PHV_EN &&
3332 		    !(mdev->dev->caps.flags2 &
3333 		      MLX4_DEV_CAP_FLAG2_SKIP_OUTER_VLAN))
3334 			dev->hw_features |= NETIF_F_HW_VLAN_STAG_TX;
3335 	}
3336 
3337 	if (mdev->dev->caps.flags & MLX4_DEV_CAP_FLAG_FCS_KEEP)
3338 		dev->hw_features |= NETIF_F_RXFCS;
3339 
3340 	if (mdev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_IGNORE_FCS)
3341 		dev->hw_features |= NETIF_F_RXALL;
3342 
3343 	if (mdev->dev->caps.steering_mode ==
3344 	    MLX4_STEERING_MODE_DEVICE_MANAGED &&
3345 	    mdev->dev->caps.dmfs_high_steer_mode != MLX4_STEERING_DMFS_A0_STATIC)
3346 		dev->hw_features |= NETIF_F_NTUPLE;
3347 
3348 	if (mdev->dev->caps.steering_mode != MLX4_STEERING_MODE_A0)
3349 		dev->priv_flags |= IFF_UNICAST_FLT;
3350 
3351 	/* Setting a default hash function value */
3352 	if (mdev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_RSS_TOP) {
3353 		priv->rss_hash_fn = ETH_RSS_HASH_TOP;
3354 	} else if (mdev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_RSS_XOR) {
3355 		priv->rss_hash_fn = ETH_RSS_HASH_XOR;
3356 	} else {
3357 		en_warn(priv,
3358 			"No RSS hash capabilities exposed, using Toeplitz\n");
3359 		priv->rss_hash_fn = ETH_RSS_HASH_TOP;
3360 	}
3361 
3362 	if (mdev->dev->caps.tunnel_offload_mode == MLX4_TUNNEL_OFFLOAD_MODE_VXLAN) {
3363 		dev->hw_features |= NETIF_F_GSO_UDP_TUNNEL |
3364 				    NETIF_F_GSO_UDP_TUNNEL_CSUM |
3365 				    NETIF_F_GSO_PARTIAL;
3366 		dev->features    |= NETIF_F_GSO_UDP_TUNNEL |
3367 				    NETIF_F_GSO_UDP_TUNNEL_CSUM |
3368 				    NETIF_F_GSO_PARTIAL;
3369 		dev->gso_partial_features = NETIF_F_GSO_UDP_TUNNEL_CSUM;
3370 	}
3371 
3372 	/* MTU range: 46 - hw-specific max */
3373 	dev->min_mtu = MLX4_EN_MIN_MTU;
3374 	dev->max_mtu = priv->max_mtu;
3375 
3376 	mdev->pndev[port] = dev;
3377 	mdev->upper[port] = NULL;
3378 
3379 	netif_carrier_off(dev);
3380 	mlx4_en_set_default_moderation(priv);
3381 
3382 	en_warn(priv, "Using %d TX rings\n", prof->tx_ring_num[TX]);
3383 	en_warn(priv, "Using %d RX rings\n", prof->rx_ring_num);
3384 
3385 	mlx4_en_update_loopback_state(priv->dev, priv->dev->features);
3386 
3387 	/* Configure port */
3388 	mlx4_en_calc_rx_buf(dev);
3389 	err = mlx4_SET_PORT_general(mdev->dev, priv->port,
3390 				    priv->rx_skb_size + ETH_FCS_LEN,
3391 				    prof->tx_pause, prof->tx_ppp,
3392 				    prof->rx_pause, prof->rx_ppp);
3393 	if (err) {
3394 		en_err(priv, "Failed setting port general configurations for port %d, with error %d\n",
3395 		       priv->port, err);
3396 		goto out;
3397 	}
3398 
3399 	if (mdev->dev->caps.tunnel_offload_mode == MLX4_TUNNEL_OFFLOAD_MODE_VXLAN) {
3400 		err = mlx4_SET_PORT_VXLAN(mdev->dev, priv->port, VXLAN_STEER_BY_OUTER_MAC, 1);
3401 		if (err) {
3402 			en_err(priv, "Failed setting port L2 tunnel configuration, err %d\n",
3403 			       err);
3404 			goto out;
3405 		}
3406 	}
3407 
3408 	/* Init port */
3409 	en_warn(priv, "Initializing port\n");
3410 	err = mlx4_INIT_PORT(mdev->dev, priv->port);
3411 	if (err) {
3412 		en_err(priv, "Failed Initializing port\n");
3413 		goto out;
3414 	}
3415 	queue_delayed_work(mdev->workqueue, &priv->stats_task, STATS_DELAY);
3416 
3417 	/* Initialize time stamp mechanism */
3418 	if (mdev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_TS)
3419 		mlx4_en_init_timestamp(mdev);
3420 
3421 	queue_delayed_work(mdev->workqueue, &priv->service_task,
3422 			   SERVICE_TASK_DELAY);
3423 
3424 	mlx4_en_set_stats_bitmap(mdev->dev, &priv->stats_bitmap,
3425 				 mdev->profile.prof[priv->port].rx_ppp,
3426 				 mdev->profile.prof[priv->port].rx_pause,
3427 				 mdev->profile.prof[priv->port].tx_ppp,
3428 				 mdev->profile.prof[priv->port].tx_pause);
3429 
3430 	err = register_netdev(dev);
3431 	if (err) {
3432 		en_err(priv, "Netdev registration failed for port %d\n", port);
3433 		goto out;
3434 	}
3435 
3436 	priv->registered = 1;
3437 	devlink_port_type_eth_set(mlx4_get_devlink_port(mdev->dev, priv->port),
3438 				  dev);
3439 
3440 	return 0;
3441 
3442 err_free_tx:
3443 	while (t--) {
3444 		kfree(priv->tx_ring[t]);
3445 		kfree(priv->tx_cq[t]);
3446 	}
3447 out:
3448 	mlx4_en_destroy_netdev(dev);
3449 	return err;
3450 }
3451 
3452 int mlx4_en_reset_config(struct net_device *dev,
3453 			 struct hwtstamp_config ts_config,
3454 			 netdev_features_t features)
3455 {
3456 	struct mlx4_en_priv *priv = netdev_priv(dev);
3457 	struct mlx4_en_dev *mdev = priv->mdev;
3458 	struct mlx4_en_port_profile new_prof;
3459 	struct mlx4_en_priv *tmp;
3460 	int port_up = 0;
3461 	int err = 0;
3462 
3463 	if (priv->hwtstamp_config.tx_type == ts_config.tx_type &&
3464 	    priv->hwtstamp_config.rx_filter == ts_config.rx_filter &&
3465 	    !DEV_FEATURE_CHANGED(dev, features, NETIF_F_HW_VLAN_CTAG_RX) &&
3466 	    !DEV_FEATURE_CHANGED(dev, features, NETIF_F_RXFCS))
3467 		return 0; /* Nothing to change */
3468 
3469 	if (DEV_FEATURE_CHANGED(dev, features, NETIF_F_HW_VLAN_CTAG_RX) &&
3470 	    (features & NETIF_F_HW_VLAN_CTAG_RX) &&
3471 	    (priv->hwtstamp_config.rx_filter != HWTSTAMP_FILTER_NONE)) {
3472 		en_warn(priv, "Can't turn ON rx vlan offload while time-stamping rx filter is ON\n");
3473 		return -EINVAL;
3474 	}
3475 
3476 	tmp = kzalloc(sizeof(*tmp), GFP_KERNEL);
3477 	if (!tmp)
3478 		return -ENOMEM;
3479 
3480 	mutex_lock(&mdev->state_lock);
3481 
3482 	memcpy(&new_prof, priv->prof, sizeof(struct mlx4_en_port_profile));
3483 	memcpy(&new_prof.hwtstamp_config, &ts_config, sizeof(ts_config));
3484 
3485 	err = mlx4_en_try_alloc_resources(priv, tmp, &new_prof);
3486 	if (err)
3487 		goto out;
3488 
3489 	if (priv->port_up) {
3490 		port_up = 1;
3491 		mlx4_en_stop_port(dev, 1);
3492 	}
3493 
3494 	en_warn(priv, "Changing device configuration rx filter(%x) rx vlan(%x)\n",
3495 		ts_config.rx_filter,
3496 		!!(features & NETIF_F_HW_VLAN_CTAG_RX));
3497 
3498 	mlx4_en_safe_replace_resources(priv, tmp);
3499 
3500 	if (DEV_FEATURE_CHANGED(dev, features, NETIF_F_HW_VLAN_CTAG_RX)) {
3501 		if (features & NETIF_F_HW_VLAN_CTAG_RX)
3502 			dev->features |= NETIF_F_HW_VLAN_CTAG_RX;
3503 		else
3504 			dev->features &= ~NETIF_F_HW_VLAN_CTAG_RX;
3505 	} else if (ts_config.rx_filter == HWTSTAMP_FILTER_NONE) {
3506 		/* RX time-stamping is OFF, update the RX vlan offload
3507 		 * to the latest wanted state
3508 		 */
3509 		if (dev->wanted_features & NETIF_F_HW_VLAN_CTAG_RX)
3510 			dev->features |= NETIF_F_HW_VLAN_CTAG_RX;
3511 		else
3512 			dev->features &= ~NETIF_F_HW_VLAN_CTAG_RX;
3513 	}
3514 
3515 	if (DEV_FEATURE_CHANGED(dev, features, NETIF_F_RXFCS)) {
3516 		if (features & NETIF_F_RXFCS)
3517 			dev->features |= NETIF_F_RXFCS;
3518 		else
3519 			dev->features &= ~NETIF_F_RXFCS;
3520 	}
3521 
3522 	/* RX vlan offload and RX time-stamping can't co-exist !
3523 	 * Regardless of the caller's choice,
3524 	 * Turn Off RX vlan offload in case of time-stamping is ON
3525 	 */
3526 	if (ts_config.rx_filter != HWTSTAMP_FILTER_NONE) {
3527 		if (dev->features & NETIF_F_HW_VLAN_CTAG_RX)
3528 			en_warn(priv, "Turning off RX vlan offload since RX time-stamping is ON\n");
3529 		dev->features &= ~NETIF_F_HW_VLAN_CTAG_RX;
3530 	}
3531 
3532 	if (port_up) {
3533 		err = mlx4_en_start_port(dev);
3534 		if (err)
3535 			en_err(priv, "Failed starting port\n");
3536 	}
3537 
3538 out:
3539 	mutex_unlock(&mdev->state_lock);
3540 	kfree(tmp);
3541 	if (!err)
3542 		netdev_features_change(dev);
3543 	return err;
3544 }
3545