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