xref: /freebsd/sys/dev/mlx4/mlx4_en/mlx4_en_netdev.c (revision e9b1dc32c9bd2ebae5f9e140bfa0e0321bc366b5)
1 /*
2  * Copyright (c) 2007, 2014 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/delay.h>
36 #include <linux/slab.h>
37 #include <linux/compat.h>
38 #ifdef CONFIG_NET_RX_BUSY_POLL
39 #include <net/busy_poll.h>
40 #endif
41 
42 #include <linux/list.h>
43 #include <linux/if_ether.h>
44 
45 #include <dev/mlx4/driver.h>
46 #include <dev/mlx4/device.h>
47 #include <dev/mlx4/cmd.h>
48 #include <dev/mlx4/cq.h>
49 
50 #include <sys/sockio.h>
51 #include <sys/sysctl.h>
52 
53 #include "en.h"
54 #include "en_port.h"
55 
56 static void mlx4_en_sysctl_stat(struct mlx4_en_priv *priv);
57 static void mlx4_en_sysctl_conf(struct mlx4_en_priv *priv);
58 
59 #ifdef CONFIG_NET_RX_BUSY_POLL
60 /* must be called with local_bh_disable()d */
61 static int mlx4_en_low_latency_recv(struct napi_struct *napi)
62 {
63 	struct mlx4_en_cq *cq = container_of(napi, struct mlx4_en_cq, napi);
64 	struct net_device *dev = cq->dev;
65 	struct mlx4_en_priv *priv = netdev_priv(dev);
66 	struct mlx4_en_rx_ring *rx_ring = priv->rx_ring[cq->ring];
67 	int done;
68 
69 	if (!priv->port_up)
70 		return LL_FLUSH_FAILED;
71 
72 	if (!mlx4_en_cq_lock_poll(cq))
73 		return LL_FLUSH_BUSY;
74 
75 	done = mlx4_en_process_rx_cq(dev, cq, 4);
76 #ifdef LL_EXTENDED_STATS
77 	if (likely(done))
78 		rx_ring->cleaned += done;
79 	else
80 		rx_ring->misses++;
81 #endif
82 
83 	mlx4_en_cq_unlock_poll(cq);
84 
85 	return done;
86 }
87 #endif	/* CONFIG_NET_RX_BUSY_POLL */
88 
89 #ifdef CONFIG_RFS_ACCEL
90 
91 struct mlx4_en_filter {
92 	struct list_head next;
93 	struct work_struct work;
94 
95 	u8     ip_proto;
96 	__be32 src_ip;
97 	__be32 dst_ip;
98 	__be16 src_port;
99 	__be16 dst_port;
100 
101 	int rxq_index;
102 	struct mlx4_en_priv *priv;
103 	u32 flow_id;			/* RFS infrastructure id */
104 	int id;				/* mlx4_en driver id */
105 	u64 reg_id;			/* Flow steering API id */
106 	u8 activated;			/* Used to prevent expiry before filter
107 					 * is attached
108 					 */
109 	struct hlist_node filter_chain;
110 };
111 
112 static void mlx4_en_filter_rfs_expire(struct mlx4_en_priv *priv);
113 
114 static enum mlx4_net_trans_rule_id mlx4_ip_proto_to_trans_rule_id(u8 ip_proto)
115 {
116 	switch (ip_proto) {
117 	case IPPROTO_UDP:
118 		return MLX4_NET_TRANS_RULE_ID_UDP;
119 	case IPPROTO_TCP:
120 		return MLX4_NET_TRANS_RULE_ID_TCP;
121 	default:
122 		return MLX4_NET_TRANS_RULE_NUM;
123 	}
124 };
125 
126 static void mlx4_en_filter_work(struct work_struct *work)
127 {
128 	struct mlx4_en_filter *filter = container_of(work,
129 						     struct mlx4_en_filter,
130 						     work);
131 	struct mlx4_en_priv *priv = filter->priv;
132 	struct mlx4_spec_list spec_tcp_udp = {
133 		.id = mlx4_ip_proto_to_trans_rule_id(filter->ip_proto),
134 		{
135 			.tcp_udp = {
136 				.dst_port = filter->dst_port,
137 				.dst_port_msk = (__force __be16)-1,
138 				.src_port = filter->src_port,
139 				.src_port_msk = (__force __be16)-1,
140 			},
141 		},
142 	};
143 	struct mlx4_spec_list spec_ip = {
144 		.id = MLX4_NET_TRANS_RULE_ID_IPV4,
145 		{
146 			.ipv4 = {
147 				.dst_ip = filter->dst_ip,
148 				.dst_ip_msk = (__force __be32)-1,
149 				.src_ip = filter->src_ip,
150 				.src_ip_msk = (__force __be32)-1,
151 			},
152 		},
153 	};
154 	struct mlx4_spec_list spec_eth = {
155 		.id = MLX4_NET_TRANS_RULE_ID_ETH,
156 	};
157 	struct mlx4_net_trans_rule rule = {
158 		.list = LIST_HEAD_INIT(rule.list),
159 		.queue_mode = MLX4_NET_TRANS_Q_LIFO,
160 		.exclusive = 1,
161 		.allow_loopback = 1,
162 		.promisc_mode = MLX4_FS_REGULAR,
163 		.port = priv->port,
164 		.priority = MLX4_DOMAIN_RFS,
165 	};
166 	int rc;
167 	__be64 mac_mask = cpu_to_be64(MLX4_MAC_MASK << 16);
168 
169 	if (spec_tcp_udp.id >= MLX4_NET_TRANS_RULE_NUM) {
170 		en_warn(priv, "RFS: ignoring unsupported ip protocol (%d)\n",
171 			filter->ip_proto);
172 		goto ignore;
173 	}
174 	list_add_tail(&spec_eth.list, &rule.list);
175 	list_add_tail(&spec_ip.list, &rule.list);
176 	list_add_tail(&spec_tcp_udp.list, &rule.list);
177 
178 	rule.qpn = priv->rss_map.qps[filter->rxq_index].qpn;
179 	memcpy(spec_eth.eth.dst_mac, priv->dev->dev_addr, ETH_ALEN);
180 	memcpy(spec_eth.eth.dst_mac_msk, &mac_mask, ETH_ALEN);
181 
182 	filter->activated = 0;
183 
184 	if (filter->reg_id) {
185 		rc = mlx4_flow_detach(priv->mdev->dev, filter->reg_id);
186 		if (rc && rc != -ENOENT)
187 			en_err(priv, "Error detaching flow. rc = %d\n", rc);
188 	}
189 
190 	rc = mlx4_flow_attach(priv->mdev->dev, &rule, &filter->reg_id);
191 	if (rc)
192 		en_err(priv, "Error attaching flow. err = %d\n", rc);
193 
194 ignore:
195 	mlx4_en_filter_rfs_expire(priv);
196 
197 	filter->activated = 1;
198 }
199 
200 static inline struct hlist_head *
201 filter_hash_bucket(struct mlx4_en_priv *priv, __be32 src_ip, __be32 dst_ip,
202 		   __be16 src_port, __be16 dst_port)
203 {
204 	unsigned long l;
205 	int bucket_idx;
206 
207 	l = (__force unsigned long)src_port |
208 	    ((__force unsigned long)dst_port << 2);
209 	l ^= (__force unsigned long)(src_ip ^ dst_ip);
210 
211 	bucket_idx = hash_long(l, MLX4_EN_FILTER_HASH_SHIFT);
212 
213 	return &priv->filter_hash[bucket_idx];
214 }
215 
216 static struct mlx4_en_filter *
217 mlx4_en_filter_alloc(struct mlx4_en_priv *priv, int rxq_index, __be32 src_ip,
218 		     __be32 dst_ip, u8 ip_proto, __be16 src_port,
219 		     __be16 dst_port, u32 flow_id)
220 {
221 	struct mlx4_en_filter *filter = NULL;
222 
223 	filter = kzalloc(sizeof(struct mlx4_en_filter), GFP_ATOMIC);
224 	if (!filter)
225 		return NULL;
226 
227 	filter->priv = priv;
228 	filter->rxq_index = rxq_index;
229 	INIT_WORK(&filter->work, mlx4_en_filter_work);
230 
231 	filter->src_ip = src_ip;
232 	filter->dst_ip = dst_ip;
233 	filter->ip_proto = ip_proto;
234 	filter->src_port = src_port;
235 	filter->dst_port = dst_port;
236 
237 	filter->flow_id = flow_id;
238 
239 	filter->id = priv->last_filter_id++ % RPS_NO_FILTER;
240 
241 	list_add_tail(&filter->next, &priv->filters);
242 	hlist_add_head(&filter->filter_chain,
243 		       filter_hash_bucket(priv, src_ip, dst_ip, src_port,
244 					  dst_port));
245 
246 	return filter;
247 }
248 
249 static void mlx4_en_filter_free(struct mlx4_en_filter *filter)
250 {
251 	struct mlx4_en_priv *priv = filter->priv;
252 	int rc;
253 
254 	list_del(&filter->next);
255 
256 	rc = mlx4_flow_detach(priv->mdev->dev, filter->reg_id);
257 	if (rc && rc != -ENOENT)
258 		en_err(priv, "Error detaching flow. rc = %d\n", rc);
259 
260 	kfree(filter);
261 }
262 
263 static inline struct mlx4_en_filter *
264 mlx4_en_filter_find(struct mlx4_en_priv *priv, __be32 src_ip, __be32 dst_ip,
265 		    u8 ip_proto, __be16 src_port, __be16 dst_port)
266 {
267 	struct mlx4_en_filter *filter;
268 	struct mlx4_en_filter *ret = NULL;
269 
270 	hlist_for_each_entry(filter,
271 			     filter_hash_bucket(priv, src_ip, dst_ip,
272 						src_port, dst_port),
273 			     filter_chain) {
274 		if (filter->src_ip == src_ip &&
275 		    filter->dst_ip == dst_ip &&
276 		    filter->ip_proto == ip_proto &&
277 		    filter->src_port == src_port &&
278 		    filter->dst_port == dst_port) {
279 			ret = filter;
280 			break;
281 		}
282 	}
283 
284 	return ret;
285 }
286 
287 static int
288 mlx4_en_filter_rfs(struct net_device *net_dev, const struct sk_buff *skb,
289 		   u16 rxq_index, u32 flow_id)
290 {
291 	struct mlx4_en_priv *priv = netdev_priv(net_dev);
292 	struct mlx4_en_filter *filter;
293 	const struct iphdr *ip;
294 	const __be16 *ports;
295 	u8 ip_proto;
296 	__be32 src_ip;
297 	__be32 dst_ip;
298 	__be16 src_port;
299 	__be16 dst_port;
300 	int nhoff = skb_network_offset(skb);
301 	int ret = 0;
302 
303 	if (skb->protocol != htons(ETH_P_IP))
304 		return -EPROTONOSUPPORT;
305 
306 	ip = (const struct iphdr *)(skb->data + nhoff);
307 	if (ip_is_fragment(ip))
308 		return -EPROTONOSUPPORT;
309 
310 	if ((ip->protocol != IPPROTO_TCP) && (ip->protocol != IPPROTO_UDP))
311 		return -EPROTONOSUPPORT;
312 	ports = (const __be16 *)(skb->data + nhoff + 4 * ip->ihl);
313 
314 	ip_proto = ip->protocol;
315 	src_ip = ip->saddr;
316 	dst_ip = ip->daddr;
317 	src_port = ports[0];
318 	dst_port = ports[1];
319 
320 	spin_lock_bh(&priv->filters_lock);
321 	filter = mlx4_en_filter_find(priv, src_ip, dst_ip, ip_proto,
322 				     src_port, dst_port);
323 	if (filter) {
324 		if (filter->rxq_index == rxq_index)
325 			goto out;
326 
327 		filter->rxq_index = rxq_index;
328 	} else {
329 		filter = mlx4_en_filter_alloc(priv, rxq_index,
330 					      src_ip, dst_ip, ip_proto,
331 					      src_port, dst_port, flow_id);
332 		if (!filter) {
333 			ret = -ENOMEM;
334 			goto err;
335 		}
336 	}
337 
338 	queue_work(priv->mdev->workqueue, &filter->work);
339 
340 out:
341 	ret = filter->id;
342 err:
343 	spin_unlock_bh(&priv->filters_lock);
344 
345 	return ret;
346 }
347 
348 void mlx4_en_cleanup_filters(struct mlx4_en_priv *priv)
349 {
350 	struct mlx4_en_filter *filter, *tmp;
351 	LIST_HEAD(del_list);
352 
353 	spin_lock_bh(&priv->filters_lock);
354 	list_for_each_entry_safe(filter, tmp, &priv->filters, next) {
355 		list_move(&filter->next, &del_list);
356 		hlist_del(&filter->filter_chain);
357 	}
358 	spin_unlock_bh(&priv->filters_lock);
359 
360 	list_for_each_entry_safe(filter, tmp, &del_list, next) {
361 		cancel_work_sync(&filter->work);
362 		mlx4_en_filter_free(filter);
363 	}
364 }
365 
366 static void mlx4_en_filter_rfs_expire(struct mlx4_en_priv *priv)
367 {
368 	struct mlx4_en_filter *filter = NULL, *tmp, *last_filter = NULL;
369 	LIST_HEAD(del_list);
370 	int i = 0;
371 
372 	spin_lock_bh(&priv->filters_lock);
373 	list_for_each_entry_safe(filter, tmp, &priv->filters, next) {
374 		if (i > MLX4_EN_FILTER_EXPIRY_QUOTA)
375 			break;
376 
377 		if (filter->activated &&
378 		    !work_pending(&filter->work) &&
379 		    rps_may_expire_flow(priv->dev,
380 					filter->rxq_index, filter->flow_id,
381 					filter->id)) {
382 			list_move(&filter->next, &del_list);
383 			hlist_del(&filter->filter_chain);
384 		} else
385 			last_filter = filter;
386 
387 		i++;
388 	}
389 
390 	if (last_filter && (&last_filter->next != priv->filters.next))
391 		list_move(&priv->filters, &last_filter->next);
392 
393 	spin_unlock_bh(&priv->filters_lock);
394 
395 	list_for_each_entry_safe(filter, tmp, &del_list, next)
396 		mlx4_en_filter_free(filter);
397 }
398 #endif
399 
400 static void mlx4_en_vlan_rx_add_vid(void *arg, struct net_device *dev, u16 vid)
401 {
402 	struct mlx4_en_priv *priv = netdev_priv(dev);
403 	struct mlx4_en_dev *mdev = priv->mdev;
404 	int err;
405 	int idx;
406 
407 	if (arg != priv)
408 		return;
409 
410 	en_dbg(HW, priv, "adding VLAN:%d\n", vid);
411 
412 	set_bit(vid, priv->active_vlans);
413 
414 	/* Add VID to port VLAN filter */
415 	mutex_lock(&mdev->state_lock);
416 	if (mdev->device_up && priv->port_up) {
417 		err = mlx4_SET_VLAN_FLTR(mdev->dev, priv);
418 		if (err)
419 			en_err(priv, "Failed configuring VLAN filter\n");
420 	}
421 	if (mlx4_register_vlan(mdev->dev, priv->port, vid, &idx))
422 		en_dbg(HW, priv, "failed adding vlan %d\n", vid);
423 	mutex_unlock(&mdev->state_lock);
424 
425 }
426 
427 static void mlx4_en_vlan_rx_kill_vid(void *arg, struct net_device *dev, u16 vid)
428 {
429 	struct mlx4_en_priv *priv = netdev_priv(dev);
430 	struct mlx4_en_dev *mdev = priv->mdev;
431 	int err;
432 
433 	if (arg != priv)
434 		return;
435 
436 	en_dbg(HW, priv, "Killing VID:%d\n", vid);
437 
438 	clear_bit(vid, priv->active_vlans);
439 
440 	/* Remove VID from port VLAN filter */
441 	mutex_lock(&mdev->state_lock);
442 	mlx4_unregister_vlan(mdev->dev, priv->port, vid);
443 
444 	if (mdev->device_up && priv->port_up) {
445 		err = mlx4_SET_VLAN_FLTR(mdev->dev, priv);
446 		if (err)
447 			en_err(priv, "Failed configuring VLAN filter\n");
448 	}
449 	mutex_unlock(&mdev->state_lock);
450 
451 }
452 
453 static int mlx4_en_tunnel_steer_add(struct mlx4_en_priv *priv, unsigned char *addr,
454 				    int qpn, u64 *reg_id)
455 {
456 	int err;
457 
458 	if (priv->mdev->dev->caps.tunnel_offload_mode != MLX4_TUNNEL_OFFLOAD_MODE_VXLAN ||
459 	    priv->mdev->dev->caps.dmfs_high_steer_mode == MLX4_STEERING_DMFS_A0_STATIC)
460 		return 0; /* do nothing */
461 
462 	err = mlx4_tunnel_steer_add(priv->mdev->dev, addr, priv->port, qpn,
463 				    MLX4_DOMAIN_NIC, reg_id);
464 	if (err) {
465 		en_err(priv, "failed to add vxlan steering rule, err %d\n", err);
466 		return err;
467 	}
468 	en_dbg(DRV, priv, "added vxlan steering rule, mac %pM reg_id %llx\n", addr, (long long)*reg_id);
469 	return 0;
470 }
471 
472 static int mlx4_en_uc_steer_add(struct mlx4_en_priv *priv,
473 				unsigned char *mac, int *qpn, u64 *reg_id)
474 {
475 	struct mlx4_en_dev *mdev = priv->mdev;
476 	struct mlx4_dev *dev = mdev->dev;
477 	int err;
478 
479 	switch (dev->caps.steering_mode) {
480 	case MLX4_STEERING_MODE_B0: {
481 		struct mlx4_qp qp;
482 		u8 gid[16] = {0};
483 
484 		qp.qpn = *qpn;
485 		memcpy(&gid[10], mac, ETH_ALEN);
486 		gid[5] = priv->port;
487 
488 		err = mlx4_unicast_attach(dev, &qp, gid, 0, MLX4_PROT_ETH);
489 		break;
490 	}
491 	case MLX4_STEERING_MODE_DEVICE_MANAGED: {
492 		struct mlx4_spec_list spec_eth = { {NULL} };
493 		__be64 mac_mask = cpu_to_be64(MLX4_MAC_MASK << 16);
494 
495 		struct mlx4_net_trans_rule rule = {
496 			.queue_mode = MLX4_NET_TRANS_Q_FIFO,
497 			.exclusive = 0,
498 			.allow_loopback = 1,
499 			.promisc_mode = MLX4_FS_REGULAR,
500 			.priority = MLX4_DOMAIN_NIC,
501 		};
502 
503 		rule.port = priv->port;
504 		rule.qpn = *qpn;
505 		INIT_LIST_HEAD(&rule.list);
506 
507 		spec_eth.id = MLX4_NET_TRANS_RULE_ID_ETH;
508 		memcpy(spec_eth.eth.dst_mac, mac, ETH_ALEN);
509 		memcpy(spec_eth.eth.dst_mac_msk, &mac_mask, ETH_ALEN);
510 		list_add_tail(&spec_eth.list, &rule.list);
511 
512 		err = mlx4_flow_attach(dev, &rule, reg_id);
513 		break;
514 	}
515 	default:
516 		return -EINVAL;
517 	}
518 	if (err)
519 		en_warn(priv, "Failed Attaching Unicast\n");
520 
521 	return err;
522 }
523 
524 static void mlx4_en_uc_steer_release(struct mlx4_en_priv *priv,
525 				     unsigned char *mac, int qpn, u64 reg_id)
526 {
527 	struct mlx4_en_dev *mdev = priv->mdev;
528 	struct mlx4_dev *dev = mdev->dev;
529 
530 	switch (dev->caps.steering_mode) {
531 	case MLX4_STEERING_MODE_B0: {
532 		struct mlx4_qp qp;
533 		u8 gid[16] = {0};
534 
535 		qp.qpn = qpn;
536 		memcpy(&gid[10], mac, ETH_ALEN);
537 		gid[5] = priv->port;
538 
539 		mlx4_unicast_detach(dev, &qp, gid, MLX4_PROT_ETH);
540 		break;
541 	}
542 	case MLX4_STEERING_MODE_DEVICE_MANAGED: {
543 		mlx4_flow_detach(dev, reg_id);
544 		break;
545 	}
546 	default:
547 		en_err(priv, "Invalid steering mode.\n");
548 	}
549 }
550 
551 static int mlx4_en_get_qp(struct mlx4_en_priv *priv)
552 {
553 	struct mlx4_en_dev *mdev = priv->mdev;
554 	struct mlx4_dev *dev = mdev->dev;
555 	int index = 0;
556 	int err = 0;
557 	int *qpn = &priv->base_qpn;
558 	u64 mac = mlx4_mac_to_u64(IF_LLADDR(priv->dev));
559 
560 	en_dbg(DRV, priv, "Registering MAC: %pM for adding\n",
561 	       IF_LLADDR(priv->dev));
562 	index = mlx4_register_mac(dev, priv->port, mac);
563 	if (index < 0) {
564 		err = index;
565 		en_err(priv, "Failed adding MAC: %pM\n",
566 		       IF_LLADDR(priv->dev));
567 		return err;
568 	}
569 
570 	if (dev->caps.steering_mode == MLX4_STEERING_MODE_A0) {
571 		int base_qpn = mlx4_get_base_qpn(dev, priv->port);
572 		*qpn = base_qpn + index;
573 		return 0;
574 	}
575 
576 	err = mlx4_qp_reserve_range(dev, 1, 1, qpn, MLX4_RESERVE_A0_QP);
577 	en_dbg(DRV, priv, "Reserved qp %d\n", *qpn);
578 	if (err) {
579 		en_err(priv, "Failed to reserve qp for mac registration\n");
580 		mlx4_unregister_mac(dev, priv->port, mac);
581 		return err;
582 	}
583 
584 	return 0;
585 }
586 
587 static void mlx4_en_put_qp(struct mlx4_en_priv *priv)
588 {
589 	struct mlx4_en_dev *mdev = priv->mdev;
590 	struct mlx4_dev *dev = mdev->dev;
591 	int qpn = priv->base_qpn;
592 
593 	if (dev->caps.steering_mode == MLX4_STEERING_MODE_A0) {
594 		u64 mac = mlx4_mac_to_u64(IF_LLADDR(priv->dev));
595 		en_dbg(DRV, priv, "Registering MAC: %pM for deleting\n",
596 		       IF_LLADDR(priv->dev));
597 		mlx4_unregister_mac(dev, priv->port, mac);
598 	} else {
599 		en_dbg(DRV, priv, "Releasing qp: port %d, qpn %d\n",
600 		       priv->port, qpn);
601 		mlx4_qp_release_range(dev, qpn, 1);
602 		priv->flags &= ~MLX4_EN_FLAG_FORCE_PROMISC;
603 	}
604 }
605 
606 static void mlx4_en_clear_uclist(struct net_device *dev)
607 {
608 	struct mlx4_en_priv *priv = netdev_priv(dev);
609 	struct mlx4_en_addr_list *tmp, *uc_to_del;
610 
611 	list_for_each_entry_safe(uc_to_del, tmp, &priv->uc_list, list) {
612 		list_del(&uc_to_del->list);
613 		kfree(uc_to_del);
614 	}
615 }
616 
617 static void mlx4_en_cache_uclist(struct net_device *dev)
618 {
619 	struct mlx4_en_priv *priv = netdev_priv(dev);
620 	struct mlx4_en_addr_list *tmp;
621 	struct ifaddr *ifa;
622 
623 	mlx4_en_clear_uclist(dev);
624 
625 	if_addr_rlock(dev);
626 	CK_STAILQ_FOREACH(ifa, &dev->if_addrhead, ifa_link) {
627 		if (ifa->ifa_addr->sa_family != AF_LINK)
628 			continue;
629 		if (((struct sockaddr_dl *)ifa->ifa_addr)->sdl_alen !=
630 				ETHER_ADDR_LEN)
631 			continue;
632 		tmp = kzalloc(sizeof(struct mlx4_en_addr_list), GFP_ATOMIC);
633 		if (tmp == NULL) {
634 			en_err(priv, "Failed to allocate address list\n");
635 			break;
636 		}
637 		memcpy(tmp->addr,
638 			LLADDR((struct sockaddr_dl *)ifa->ifa_addr), ETH_ALEN);
639 		list_add_tail(&tmp->list, &priv->uc_list);
640 	}
641 	if_addr_runlock(dev);
642 }
643 
644 static void mlx4_en_clear_mclist(struct net_device *dev)
645 {
646 	struct mlx4_en_priv *priv = netdev_priv(dev);
647 	struct mlx4_en_addr_list *tmp, *mc_to_del;
648 
649 	list_for_each_entry_safe(mc_to_del, tmp, &priv->mc_list, list) {
650 		list_del(&mc_to_del->list);
651 		kfree(mc_to_del);
652 	}
653 }
654 
655 static void mlx4_en_cache_mclist(struct net_device *dev)
656 {
657 	struct mlx4_en_priv *priv = netdev_priv(dev);
658 	struct mlx4_en_addr_list *tmp;
659 	struct ifmultiaddr *ifma;
660 
661 	mlx4_en_clear_mclist(dev);
662 
663 	if_maddr_rlock(dev);
664 	CK_STAILQ_FOREACH(ifma, &dev->if_multiaddrs, ifma_link) {
665 		if (ifma->ifma_addr->sa_family != AF_LINK)
666 			continue;
667 		if (((struct sockaddr_dl *)ifma->ifma_addr)->sdl_alen !=
668 				ETHER_ADDR_LEN)
669 			continue;
670 		tmp = kzalloc(sizeof(struct mlx4_en_addr_list), GFP_ATOMIC);
671 		if (tmp == NULL) {
672 			en_err(priv, "Failed to allocate address list\n");
673 			break;
674 		}
675 		memcpy(tmp->addr,
676 			LLADDR((struct sockaddr_dl *)ifma->ifma_addr), ETH_ALEN);
677 		list_add_tail(&tmp->list, &priv->mc_list);
678 	}
679 	if_maddr_runlock(dev);
680 }
681 
682 static void update_addr_list_flags(struct mlx4_en_priv *priv,
683 				struct list_head *dst,
684 				struct list_head *src)
685 {
686 	struct mlx4_en_addr_list *dst_tmp, *src_tmp, *new_mc;
687 	bool found;
688 
689 	/* Find all the entries that should be removed from dst,
690 	 * These are the entries that are not found in src
691 	 */
692 	list_for_each_entry(dst_tmp, dst, list) {
693 		found = false;
694 		list_for_each_entry(src_tmp, src, list) {
695 			if (!memcmp(dst_tmp->addr, src_tmp->addr, ETH_ALEN)) {
696 				found = true;
697 				break;
698 			}
699 		}
700 		if (!found)
701 			dst_tmp->action = MLX4_ADDR_LIST_REM;
702 	}
703 
704 	/* Add entries that exist in src but not in dst
705 	 * mark them as need to add
706 	 */
707 	list_for_each_entry(src_tmp, src, list) {
708 		found = false;
709 		list_for_each_entry(dst_tmp, dst, list) {
710 			if (!memcmp(dst_tmp->addr, src_tmp->addr, ETH_ALEN)) {
711 				dst_tmp->action = MLX4_ADDR_LIST_NONE;
712 				found = true;
713 				break;
714 			}
715 		}
716 		if (!found) {
717 			new_mc = kmalloc(sizeof(struct mlx4_en_addr_list),
718 					 GFP_KERNEL);
719 			if (!new_mc) {
720 				en_err(priv, "Failed to allocate current multicast list\n");
721 				return;
722 			}
723 			memcpy(new_mc, src_tmp,
724 			       sizeof(struct mlx4_en_addr_list));
725 			new_mc->action = MLX4_ADDR_LIST_ADD;
726 			list_add_tail(&new_mc->list, dst);
727 		}
728 	}
729 }
730 
731 static void mlx4_en_set_rx_mode(struct net_device *dev)
732 {
733 	struct mlx4_en_priv *priv = netdev_priv(dev);
734 
735 	if (!priv->port_up)
736 		return;
737 
738 	queue_work(priv->mdev->workqueue, &priv->rx_mode_task);
739 }
740 
741 static void mlx4_en_set_promisc_mode(struct mlx4_en_priv *priv,
742 				     struct mlx4_en_dev *mdev)
743 {
744 	int err = 0;
745 
746 	if (!(priv->flags & MLX4_EN_FLAG_PROMISC)) {
747 		priv->flags |= MLX4_EN_FLAG_PROMISC;
748 
749 		/* Enable promiscouos mode */
750 		switch (mdev->dev->caps.steering_mode) {
751 		case MLX4_STEERING_MODE_DEVICE_MANAGED:
752 			err = mlx4_flow_steer_promisc_add(mdev->dev,
753 							  priv->port,
754 							  priv->base_qpn,
755 							  MLX4_FS_ALL_DEFAULT);
756 			if (err)
757 				en_err(priv, "Failed enabling promiscuous mode\n");
758 			priv->flags |= MLX4_EN_FLAG_MC_PROMISC;
759 			break;
760 
761 		case MLX4_STEERING_MODE_B0:
762 			err = mlx4_unicast_promisc_add(mdev->dev,
763 						       priv->base_qpn,
764 						       priv->port);
765 			if (err)
766 				en_err(priv, "Failed enabling unicast promiscuous mode\n");
767 
768 			/* Add the default qp number as multicast
769 			 * promisc
770 			 */
771 			if (!(priv->flags & MLX4_EN_FLAG_MC_PROMISC)) {
772 				err = mlx4_multicast_promisc_add(mdev->dev,
773 								 priv->base_qpn,
774 								 priv->port);
775 				if (err)
776 					en_err(priv, "Failed enabling multicast promiscuous mode\n");
777 				priv->flags |= MLX4_EN_FLAG_MC_PROMISC;
778 			}
779 			break;
780 
781 		case MLX4_STEERING_MODE_A0:
782 			err = mlx4_SET_PORT_qpn_calc(mdev->dev,
783 						     priv->port,
784 						     priv->base_qpn,
785 						     1);
786 			if (err)
787 				en_err(priv, "Failed enabling promiscuous mode\n");
788 			break;
789 		}
790 
791 		/* Disable port multicast filter (unconditionally) */
792 		err = mlx4_SET_MCAST_FLTR(mdev->dev, priv->port, 0,
793 					  0, MLX4_MCAST_DISABLE);
794 		if (err)
795 			en_err(priv, "Failed disabling multicast filter\n");
796 	}
797 }
798 
799 static void mlx4_en_clear_promisc_mode(struct mlx4_en_priv *priv,
800 				       struct mlx4_en_dev *mdev)
801 {
802 	int err = 0;
803 
804 	priv->flags &= ~MLX4_EN_FLAG_PROMISC;
805 
806 	/* Disable promiscouos mode */
807 	switch (mdev->dev->caps.steering_mode) {
808 	case MLX4_STEERING_MODE_DEVICE_MANAGED:
809 		err = mlx4_flow_steer_promisc_remove(mdev->dev,
810 						     priv->port,
811 						     MLX4_FS_ALL_DEFAULT);
812 		if (err)
813 			en_err(priv, "Failed disabling promiscuous mode\n");
814 		priv->flags &= ~MLX4_EN_FLAG_MC_PROMISC;
815 		break;
816 
817 	case MLX4_STEERING_MODE_B0:
818 		err = mlx4_unicast_promisc_remove(mdev->dev,
819 						  priv->base_qpn,
820 						  priv->port);
821 		if (err)
822 			en_err(priv, "Failed disabling unicast promiscuous mode\n");
823 		/* Disable Multicast promisc */
824 		if (priv->flags & MLX4_EN_FLAG_MC_PROMISC) {
825 			err = mlx4_multicast_promisc_remove(mdev->dev,
826 							    priv->base_qpn,
827 							    priv->port);
828 			if (err)
829 				en_err(priv, "Failed disabling multicast promiscuous mode\n");
830 			priv->flags &= ~MLX4_EN_FLAG_MC_PROMISC;
831 		}
832 		break;
833 
834 	case MLX4_STEERING_MODE_A0:
835 		err = mlx4_SET_PORT_qpn_calc(mdev->dev,
836 					     priv->port,
837 					     priv->base_qpn, 0);
838 		if (err)
839 			en_err(priv, "Failed disabling promiscuous mode\n");
840 		break;
841 	}
842 }
843 
844 static void mlx4_en_do_multicast(struct mlx4_en_priv *priv,
845 				 struct net_device *dev,
846 				 struct mlx4_en_dev *mdev)
847 {
848 	struct mlx4_en_addr_list *addr_list, *tmp;
849 	u8 mc_list[16] = {0};
850 	int err = 0;
851 	u64 mcast_addr = 0;
852 
853 
854 	/* Enable/disable the multicast filter according to IFF_ALLMULTI */
855 	if (dev->if_flags & IFF_ALLMULTI) {
856 		err = mlx4_SET_MCAST_FLTR(mdev->dev, priv->port, 0,
857 					  0, MLX4_MCAST_DISABLE);
858 		if (err)
859 			en_err(priv, "Failed disabling multicast filter\n");
860 
861 		/* Add the default qp number as multicast promisc */
862 		if (!(priv->flags & MLX4_EN_FLAG_MC_PROMISC)) {
863 			switch (mdev->dev->caps.steering_mode) {
864 			case MLX4_STEERING_MODE_DEVICE_MANAGED:
865 				err = mlx4_flow_steer_promisc_add(mdev->dev,
866 								  priv->port,
867 								  priv->base_qpn,
868 								  MLX4_FS_MC_DEFAULT);
869 				break;
870 
871 			case MLX4_STEERING_MODE_B0:
872 				err = mlx4_multicast_promisc_add(mdev->dev,
873 								 priv->base_qpn,
874 								 priv->port);
875 				break;
876 
877 			case MLX4_STEERING_MODE_A0:
878 				break;
879 			}
880 			if (err)
881 				en_err(priv, "Failed entering multicast promisc mode\n");
882 			priv->flags |= MLX4_EN_FLAG_MC_PROMISC;
883 		}
884 	} else {
885 		/* Disable Multicast promisc */
886 		if (priv->flags & MLX4_EN_FLAG_MC_PROMISC) {
887 			switch (mdev->dev->caps.steering_mode) {
888 			case MLX4_STEERING_MODE_DEVICE_MANAGED:
889 				err = mlx4_flow_steer_promisc_remove(mdev->dev,
890 								     priv->port,
891 								     MLX4_FS_MC_DEFAULT);
892 				break;
893 
894 			case MLX4_STEERING_MODE_B0:
895 				err = mlx4_multicast_promisc_remove(mdev->dev,
896 								    priv->base_qpn,
897 								    priv->port);
898 				break;
899 
900 			case MLX4_STEERING_MODE_A0:
901 				break;
902 			}
903 			if (err)
904 				en_err(priv, "Failed disabling multicast promiscuous mode\n");
905 			priv->flags &= ~MLX4_EN_FLAG_MC_PROMISC;
906 		}
907 
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 		/* Flush mcast filter and init it with broadcast address */
914 		mlx4_SET_MCAST_FLTR(mdev->dev, priv->port, ETH_BCAST,
915 				    1, MLX4_MCAST_CONFIG);
916 
917 		/* Update multicast list - we cache all addresses so they won't
918 		 * change while HW is updated holding the command semaphor */
919 		mlx4_en_cache_mclist(dev);
920 		list_for_each_entry(addr_list, &priv->mc_list, list) {
921 			mcast_addr = mlx4_mac_to_u64(addr_list->addr);
922 			mlx4_SET_MCAST_FLTR(mdev->dev, priv->port,
923 					mcast_addr, 0, MLX4_MCAST_CONFIG);
924 		}
925 		err = mlx4_SET_MCAST_FLTR(mdev->dev, priv->port, 0,
926 					  0, MLX4_MCAST_ENABLE);
927 		if (err)
928 			en_err(priv, "Failed enabling multicast filter\n");
929 
930 		update_addr_list_flags(priv, &priv->curr_mc_list, &priv->mc_list);
931 
932 		list_for_each_entry_safe(addr_list, tmp, &priv->curr_mc_list, list) {
933 			if (addr_list->action == MLX4_ADDR_LIST_REM) {
934 				/* detach this address and delete from list */
935 				memcpy(&mc_list[10], addr_list->addr, ETH_ALEN);
936 				mc_list[5] = priv->port;
937 				err = mlx4_multicast_detach(mdev->dev,
938 							    &priv->rss_map.indir_qp,
939 							    mc_list,
940 							    MLX4_PROT_ETH,
941 							    addr_list->reg_id);
942 				if (err)
943 					en_err(priv, "Fail to detach multicast address\n");
944 
945 				if (addr_list->tunnel_reg_id) {
946 					err = mlx4_flow_detach(priv->mdev->dev, addr_list->tunnel_reg_id);
947 					if (err)
948 						en_err(priv, "Failed to detach multicast address\n");
949 				}
950 
951 				/* remove from list */
952 				list_del(&addr_list->list);
953 				kfree(addr_list);
954 			} else if (addr_list->action == MLX4_ADDR_LIST_ADD) {
955 				/* attach the address */
956 				memcpy(&mc_list[10], addr_list->addr, ETH_ALEN);
957 				/* needed for B0 steering support */
958 				mc_list[5] = priv->port;
959 				err = mlx4_multicast_attach(mdev->dev,
960 							    &priv->rss_map.indir_qp,
961 							    mc_list,
962 							    priv->port, 0,
963 							    MLX4_PROT_ETH,
964 							    &addr_list->reg_id);
965 				if (err)
966 					en_err(priv, "Fail to attach multicast address\n");
967 
968 				err = mlx4_en_tunnel_steer_add(priv, &mc_list[10], priv->base_qpn,
969 							       &addr_list->tunnel_reg_id);
970 				if (err)
971 					en_err(priv, "Failed to attach multicast address\n");
972 			}
973 		}
974 	}
975 }
976 
977 static void mlx4_en_do_unicast(struct mlx4_en_priv *priv,
978 			       struct net_device *dev,
979 			       struct mlx4_en_dev *mdev)
980 {
981 	struct mlx4_en_addr_list *addr_list, *tmp;
982 	int err;
983 
984 	/* Update unicast list */
985 	mlx4_en_cache_uclist(dev);
986 
987 	update_addr_list_flags(priv, &priv->curr_uc_list, &priv->uc_list);
988 
989 	list_for_each_entry_safe(addr_list, tmp, &priv->curr_uc_list, list) {
990 		if (addr_list->action == MLX4_ADDR_LIST_REM) {
991 			mlx4_en_uc_steer_release(priv, addr_list->addr,
992 						 priv->rss_map.indir_qp.qpn,
993 						 addr_list->reg_id);
994 			/* remove from list */
995 			list_del(&addr_list->list);
996 			kfree(addr_list);
997 		} else if (addr_list->action == MLX4_ADDR_LIST_ADD) {
998 			err = mlx4_en_uc_steer_add(priv, addr_list->addr,
999 						   &priv->rss_map.indir_qp.qpn,
1000 						   &addr_list->reg_id);
1001 			if (err)
1002 				en_err(priv, "Fail to add unicast address\n");
1003 		}
1004 	}
1005 }
1006 
1007 static void mlx4_en_do_set_rx_mode(struct work_struct *work)
1008 {
1009 	struct mlx4_en_priv *priv = container_of(work, struct mlx4_en_priv,
1010 						 rx_mode_task);
1011 	struct mlx4_en_dev *mdev = priv->mdev;
1012 	struct net_device *dev = priv->dev;
1013 
1014 	mutex_lock(&mdev->state_lock);
1015 	if (!mdev->device_up) {
1016 		en_dbg(HW, priv, "Card is not up, ignoring rx mode change.\n");
1017 		goto out;
1018 	}
1019 	if (!priv->port_up) {
1020 		en_dbg(HW, priv, "Port is down, ignoring rx mode change.\n");
1021 		goto out;
1022 	}
1023 	if (!mlx4_en_QUERY_PORT(mdev, priv->port)) {
1024 		if (priv->port_state.link_state) {
1025 			priv->last_link_state = MLX4_DEV_EVENT_PORT_UP;
1026 			/* update netif baudrate */
1027 			priv->dev->if_baudrate =
1028 			    IF_Mbps(priv->port_state.link_speed);
1029 			/* Important note: the following call for if_link_state_change
1030 			 * is needed for interface up scenario (start port, link state
1031 			 * change) */
1032 			if_link_state_change(priv->dev, LINK_STATE_UP);
1033 			en_dbg(HW, priv, "Link Up\n");
1034 		}
1035 	}
1036 
1037 	/* Set unicast rules */
1038 	mlx4_en_do_unicast(priv, dev, mdev);
1039 
1040 	/* Promsicuous mode: disable all filters */
1041 	if ((dev->if_flags & IFF_PROMISC) ||
1042 	    (priv->flags & MLX4_EN_FLAG_FORCE_PROMISC)) {
1043 		mlx4_en_set_promisc_mode(priv, mdev);
1044 	} else if (priv->flags & MLX4_EN_FLAG_PROMISC) {
1045 		/* Not in promiscuous mode */
1046 		mlx4_en_clear_promisc_mode(priv, mdev);
1047 	}
1048 
1049 	/* Set multicast rules */
1050 	mlx4_en_do_multicast(priv, dev, mdev);
1051 out:
1052 	mutex_unlock(&mdev->state_lock);
1053 }
1054 
1055 static void mlx4_en_watchdog_timeout(void *arg)
1056 {
1057         struct mlx4_en_priv *priv = arg;
1058         struct mlx4_en_dev *mdev = priv->mdev;
1059 
1060         en_dbg(DRV, priv, "Scheduling watchdog\n");
1061         queue_work(mdev->workqueue, &priv->watchdog_task);
1062         if (priv->port_up)
1063                 callout_reset(&priv->watchdog_timer, MLX4_EN_WATCHDOG_TIMEOUT,
1064                                 mlx4_en_watchdog_timeout, priv);
1065 }
1066 
1067 
1068 
1069 static void mlx4_en_set_default_moderation(struct mlx4_en_priv *priv)
1070 {
1071 	struct mlx4_en_cq *cq;
1072 	int i;
1073 
1074 	/* If we haven't received a specific coalescing setting
1075 	 * (module param), we set the moderation parameters as follows:
1076 	 * - moder_cnt is set to the number of mtu sized packets to
1077 	 *   satisfy our coalescing target.
1078 	 * - moder_time is set to a fixed value.
1079 	 */
1080 	priv->rx_frames = MLX4_EN_RX_COAL_TARGET;
1081 	priv->rx_usecs = MLX4_EN_RX_COAL_TIME;
1082 	priv->tx_frames = MLX4_EN_TX_COAL_PKTS;
1083 	priv->tx_usecs = MLX4_EN_TX_COAL_TIME;
1084 	en_dbg(INTR, priv, "Default coalesing params for mtu: %u - "
1085 	       "rx_frames:%d rx_usecs:%d\n",
1086 	       (unsigned)priv->dev->if_mtu, priv->rx_frames, priv->rx_usecs);
1087 
1088 	/* Setup cq moderation params */
1089 	for (i = 0; i < priv->rx_ring_num; i++) {
1090 		cq = priv->rx_cq[i];
1091 		cq->moder_cnt = priv->rx_frames;
1092 		cq->moder_time = priv->rx_usecs;
1093 		priv->last_moder_time[i] = MLX4_EN_AUTO_CONF;
1094 		priv->last_moder_packets[i] = 0;
1095 		priv->last_moder_bytes[i] = 0;
1096 	}
1097 
1098 	for (i = 0; i < priv->tx_ring_num; i++) {
1099 		cq = priv->tx_cq[i];
1100 		cq->moder_cnt = priv->tx_frames;
1101 		cq->moder_time = priv->tx_usecs;
1102 	}
1103 
1104 	/* Reset auto-moderation params */
1105 	priv->pkt_rate_low = MLX4_EN_RX_RATE_LOW;
1106 	priv->rx_usecs_low = MLX4_EN_RX_COAL_TIME_LOW;
1107 	priv->pkt_rate_high = MLX4_EN_RX_RATE_HIGH;
1108 	priv->rx_usecs_high = MLX4_EN_RX_COAL_TIME_HIGH;
1109 	priv->sample_interval = MLX4_EN_SAMPLE_INTERVAL;
1110 	priv->adaptive_rx_coal = 1;
1111 	priv->last_moder_jiffies = 0;
1112 	priv->last_moder_tx_packets = 0;
1113 }
1114 
1115 static void mlx4_en_auto_moderation(struct mlx4_en_priv *priv)
1116 {
1117 	unsigned long period = (unsigned long) (jiffies - priv->last_moder_jiffies);
1118 	struct mlx4_en_cq *cq;
1119 	unsigned long packets;
1120 	unsigned long rate;
1121 	unsigned long avg_pkt_size;
1122 	unsigned long rx_packets;
1123 	unsigned long rx_bytes;
1124 	unsigned long rx_pkt_diff;
1125 	int moder_time;
1126 	int ring, err;
1127 
1128 	if (!priv->adaptive_rx_coal || period < priv->sample_interval * HZ)
1129 		return;
1130 
1131 	for (ring = 0; ring < priv->rx_ring_num; ring++) {
1132                 spin_lock(&priv->stats_lock);
1133 		rx_packets = priv->rx_ring[ring]->packets;
1134 		rx_bytes = priv->rx_ring[ring]->bytes;
1135 		spin_unlock(&priv->stats_lock);
1136 
1137 		rx_pkt_diff = ((unsigned long) (rx_packets -
1138 				priv->last_moder_packets[ring]));
1139 		packets = rx_pkt_diff;
1140 		rate = packets * HZ / period;
1141 		avg_pkt_size = packets ? ((unsigned long) (rx_bytes -
1142 				priv->last_moder_bytes[ring])) / packets : 0;
1143 
1144 		/* Apply auto-moderation only when packet rate
1145 		 * exceeds a rate that it matters */
1146 		if (rate > (MLX4_EN_RX_RATE_THRESH / priv->rx_ring_num) &&
1147 		    avg_pkt_size > MLX4_EN_AVG_PKT_SMALL) {
1148 			if (rate < priv->pkt_rate_low)
1149 				moder_time = priv->rx_usecs_low;
1150 			else if (rate > priv->pkt_rate_high)
1151 				moder_time = priv->rx_usecs_high;
1152 			else
1153 				moder_time = (rate - priv->pkt_rate_low) *
1154 					(priv->rx_usecs_high - priv->rx_usecs_low) /
1155 					(priv->pkt_rate_high - priv->pkt_rate_low) +
1156 					priv->rx_usecs_low;
1157 		} else {
1158 			moder_time = priv->rx_usecs_low;
1159 		}
1160 
1161 		if (moder_time != priv->last_moder_time[ring]) {
1162 			priv->last_moder_time[ring] = moder_time;
1163 			cq = priv->rx_cq[ring];
1164 			cq->moder_time = moder_time;
1165 			cq->moder_cnt = priv->rx_frames;
1166 			err = mlx4_en_set_cq_moder(priv, cq);
1167 			if (err)
1168 				en_err(priv, "Failed modifying moderation for cq:%d\n",
1169 				       ring);
1170 		}
1171 		priv->last_moder_packets[ring] = rx_packets;
1172 		priv->last_moder_bytes[ring] = rx_bytes;
1173 	}
1174 
1175 	priv->last_moder_jiffies = jiffies;
1176 }
1177 
1178 static void mlx4_en_do_get_stats(struct work_struct *work)
1179 {
1180 	struct delayed_work *delay = to_delayed_work(work);
1181 	struct mlx4_en_priv *priv = container_of(delay, struct mlx4_en_priv,
1182 						 stats_task);
1183 	struct mlx4_en_dev *mdev = priv->mdev;
1184 	int err;
1185 
1186 	mutex_lock(&mdev->state_lock);
1187 	if (mdev->device_up) {
1188 		if (priv->port_up) {
1189 			if (mlx4_is_slave(mdev->dev))
1190 				err = mlx4_en_get_vport_stats(mdev, priv->port);
1191 			else
1192 				err = mlx4_en_DUMP_ETH_STATS(mdev, priv->port, 0);
1193 			if (err)
1194 				en_dbg(HW, priv, "Could not update stats\n");
1195 
1196 			mlx4_en_auto_moderation(priv);
1197 		}
1198 
1199 		queue_delayed_work(mdev->workqueue, &priv->stats_task, STATS_DELAY);
1200 	}
1201 	mutex_unlock(&mdev->state_lock);
1202 }
1203 
1204 /* mlx4_en_service_task - Run service task for tasks that needed to be done
1205  * periodically
1206  */
1207 static void mlx4_en_service_task(struct work_struct *work)
1208 {
1209 	struct delayed_work *delay = to_delayed_work(work);
1210 	struct mlx4_en_priv *priv = container_of(delay, struct mlx4_en_priv,
1211 						 service_task);
1212 	struct mlx4_en_dev *mdev = priv->mdev;
1213 
1214 	mutex_lock(&mdev->state_lock);
1215 	if (mdev->device_up) {
1216 		queue_delayed_work(mdev->workqueue, &priv->service_task,
1217 				   SERVICE_TASK_DELAY);
1218 	}
1219 	mutex_unlock(&mdev->state_lock);
1220 }
1221 
1222 static void mlx4_en_linkstate(struct work_struct *work)
1223 {
1224 	struct mlx4_en_priv *priv = container_of(work, struct mlx4_en_priv,
1225 						 linkstate_task);
1226 	struct mlx4_en_dev *mdev = priv->mdev;
1227 	int linkstate = priv->link_state;
1228 
1229 	mutex_lock(&mdev->state_lock);
1230 	/* If observable port state changed set carrier state and
1231 	 * report to system log */
1232 	if (priv->last_link_state != linkstate) {
1233 		if (linkstate == MLX4_DEV_EVENT_PORT_DOWN) {
1234 			en_info(priv, "Link Down\n");
1235 			if_link_state_change(priv->dev, LINK_STATE_DOWN);
1236 			/* update netif baudrate */
1237 			priv->dev->if_baudrate = 0;
1238 
1239 		/* make sure the port is up before notifying the OS.
1240 		 * This is tricky since we get here on INIT_PORT and
1241 		 * in such case we can't tell the OS the port is up.
1242 		 * To solve this there is a call to if_link_state_change
1243 		 * in set_rx_mode.
1244 		 * */
1245 		} else if (priv->port_up && (linkstate == MLX4_DEV_EVENT_PORT_UP)){
1246 			if (mlx4_en_QUERY_PORT(priv->mdev, priv->port))
1247 				en_info(priv, "Query port failed\n");
1248 			priv->dev->if_baudrate =
1249 			    IF_Mbps(priv->port_state.link_speed);
1250 			en_info(priv, "Link Up\n");
1251 			if_link_state_change(priv->dev, LINK_STATE_UP);
1252 		}
1253 	}
1254 	priv->last_link_state = linkstate;
1255 	mutex_unlock(&mdev->state_lock);
1256 }
1257 
1258 
1259 int mlx4_en_start_port(struct net_device *dev)
1260 {
1261 	struct mlx4_en_priv *priv = netdev_priv(dev);
1262 	struct mlx4_en_dev *mdev = priv->mdev;
1263 	struct mlx4_en_cq *cq;
1264 	struct mlx4_en_tx_ring *tx_ring;
1265 	int rx_index = 0;
1266 	int tx_index = 0;
1267 	int err = 0;
1268 	int i;
1269 	int j;
1270 	u8 mc_list[16] = {0};
1271 
1272 
1273 	if (priv->port_up) {
1274 		en_dbg(DRV, priv, "start port called while port already up\n");
1275 		return 0;
1276 	}
1277 
1278 	INIT_LIST_HEAD(&priv->mc_list);
1279 	INIT_LIST_HEAD(&priv->uc_list);
1280 	INIT_LIST_HEAD(&priv->curr_mc_list);
1281 	INIT_LIST_HEAD(&priv->curr_uc_list);
1282 	INIT_LIST_HEAD(&priv->ethtool_list);
1283 
1284 	/* Calculate Rx buf size */
1285 	dev->if_mtu = min(dev->if_mtu, priv->max_mtu);
1286         mlx4_en_calc_rx_buf(dev);
1287 	en_dbg(DRV, priv, "Rx buf size:%d\n", priv->rx_mb_size);
1288 
1289 	/* Configure rx cq's and rings */
1290 	err = mlx4_en_activate_rx_rings(priv);
1291 	if (err) {
1292 		en_err(priv, "Failed to activate RX rings\n");
1293 		return err;
1294 	}
1295 	for (i = 0; i < priv->rx_ring_num; i++) {
1296 		cq = priv->rx_cq[i];
1297 
1298 		mlx4_en_cq_init_lock(cq);
1299 		err = mlx4_en_activate_cq(priv, cq, i);
1300 		if (err) {
1301 			en_err(priv, "Failed activating Rx CQ\n");
1302 			goto cq_err;
1303 		}
1304 		for (j = 0; j < cq->size; j++)
1305 			cq->buf[j].owner_sr_opcode = MLX4_CQE_OWNER_MASK;
1306 		err = mlx4_en_set_cq_moder(priv, cq);
1307 		if (err) {
1308 			en_err(priv, "Failed setting cq moderation parameters");
1309 			mlx4_en_deactivate_cq(priv, cq);
1310 			goto cq_err;
1311 		}
1312 		mlx4_en_arm_cq(priv, cq);
1313 		priv->rx_ring[i]->cqn = cq->mcq.cqn;
1314 		++rx_index;
1315 	}
1316 
1317 	/* Set qp number */
1318 	en_dbg(DRV, priv, "Getting qp number for port %d\n", priv->port);
1319 	err = mlx4_en_get_qp(priv);
1320 	if (err) {
1321 		en_err(priv, "Failed getting eth qp\n");
1322 		goto cq_err;
1323 	}
1324 	mdev->mac_removed[priv->port] = 0;
1325 
1326 	priv->counter_index =
1327 			mlx4_get_default_counter_index(mdev->dev, priv->port);
1328 
1329 	err = mlx4_en_config_rss_steer(priv);
1330 	if (err) {
1331 		en_err(priv, "Failed configuring rss steering\n");
1332 		goto mac_err;
1333 	}
1334 
1335 	err = mlx4_en_create_drop_qp(priv);
1336 	if (err)
1337 		goto rss_err;
1338 
1339 	/* Configure tx cq's and rings */
1340 	for (i = 0; i < priv->tx_ring_num; i++) {
1341 		/* Configure cq */
1342 		cq = priv->tx_cq[i];
1343 		err = mlx4_en_activate_cq(priv, cq, i);
1344 		if (err) {
1345 			en_err(priv, "Failed activating Tx CQ\n");
1346 			goto tx_err;
1347 		}
1348 		err = mlx4_en_set_cq_moder(priv, cq);
1349 		if (err) {
1350 			en_err(priv, "Failed setting cq moderation parameters");
1351 			mlx4_en_deactivate_cq(priv, cq);
1352 			goto tx_err;
1353 		}
1354 		en_dbg(DRV, priv, "Resetting index of collapsed CQ:%d to -1\n", i);
1355 		cq->buf->wqe_index = cpu_to_be16(0xffff);
1356 
1357 		/* Configure ring */
1358 		tx_ring = priv->tx_ring[i];
1359 
1360 		err = mlx4_en_activate_tx_ring(priv, tx_ring, cq->mcq.cqn,
1361 					       i / priv->num_tx_rings_p_up);
1362 		if (err) {
1363 			en_err(priv, "Failed activating Tx ring %d\n", i);
1364 			mlx4_en_deactivate_cq(priv, cq);
1365 			goto tx_err;
1366 		}
1367 
1368 		/* Arm CQ for TX completions */
1369 		mlx4_en_arm_cq(priv, cq);
1370 
1371 		/* Set initial ownership of all Tx TXBBs to SW (1) */
1372 		for (j = 0; j < tx_ring->buf_size; j += STAMP_STRIDE)
1373 			*((u32 *) (tx_ring->buf + j)) = INIT_OWNER_BIT;
1374 		++tx_index;
1375 	}
1376 
1377 	/* Configure port */
1378 	err = mlx4_SET_PORT_general(mdev->dev, priv->port,
1379 				    priv->rx_mb_size,
1380 				    priv->prof->tx_pause,
1381 				    priv->prof->tx_ppp,
1382 				    priv->prof->rx_pause,
1383 				    priv->prof->rx_ppp);
1384 	if (err) {
1385 		en_err(priv, "Failed setting port general configurations for port %d, with error %d\n",
1386 		       priv->port, err);
1387 		goto tx_err;
1388 	}
1389 	/* Set default qp number */
1390 	err = mlx4_SET_PORT_qpn_calc(mdev->dev, priv->port, priv->base_qpn, 0);
1391 	if (err) {
1392 		en_err(priv, "Failed setting default qp numbers\n");
1393 		goto tx_err;
1394 	}
1395 
1396 	/* Init port */
1397 	en_dbg(HW, priv, "Initializing port\n");
1398 	err = mlx4_INIT_PORT(mdev->dev, priv->port);
1399 	if (err) {
1400 		en_err(priv, "Failed Initializing port\n");
1401 		goto tx_err;
1402 	}
1403 
1404 	/* Attach rx QP to bradcast address */
1405 	memset(&mc_list[10], 0xff, ETH_ALEN);
1406 	mc_list[5] = priv->port; /* needed for B0 steering support */
1407 	if (mlx4_multicast_attach(mdev->dev, &priv->rss_map.indir_qp, mc_list,
1408 				  priv->port, 0, MLX4_PROT_ETH,
1409 				  &priv->broadcast_id))
1410 		mlx4_warn(mdev, "Failed Attaching Broadcast\n");
1411 
1412 	/* Must redo promiscuous mode setup. */
1413 	priv->flags &= ~(MLX4_EN_FLAG_PROMISC | MLX4_EN_FLAG_MC_PROMISC);
1414 
1415 	/* Schedule multicast task to populate multicast list */
1416 	queue_work(mdev->workqueue, &priv->rx_mode_task);
1417 
1418 	priv->port_up = true;
1419 
1420         /* Enable the queues. */
1421         dev->if_drv_flags &= ~IFF_DRV_OACTIVE;
1422         dev->if_drv_flags |= IFF_DRV_RUNNING;
1423 #ifdef CONFIG_DEBUG_FS
1424 	mlx4_en_create_debug_files(priv);
1425 #endif
1426         callout_reset(&priv->watchdog_timer, MLX4_EN_WATCHDOG_TIMEOUT,
1427                     mlx4_en_watchdog_timeout, priv);
1428 
1429 
1430 	return 0;
1431 
1432 tx_err:
1433 	while (tx_index--) {
1434 		mlx4_en_deactivate_tx_ring(priv, priv->tx_ring[tx_index]);
1435 		mlx4_en_deactivate_cq(priv, priv->tx_cq[tx_index]);
1436 	}
1437 	mlx4_en_destroy_drop_qp(priv);
1438 rss_err:
1439 	mlx4_en_release_rss_steer(priv);
1440 mac_err:
1441 	mlx4_en_put_qp(priv);
1442 cq_err:
1443 	while (rx_index--)
1444 		mlx4_en_deactivate_cq(priv, priv->rx_cq[rx_index]);
1445 	for (i = 0; i < priv->rx_ring_num; i++)
1446 		mlx4_en_deactivate_rx_ring(priv, priv->rx_ring[i]);
1447 
1448 	return err; /* need to close devices */
1449 }
1450 
1451 
1452 void mlx4_en_stop_port(struct net_device *dev)
1453 {
1454 	struct mlx4_en_priv *priv = netdev_priv(dev);
1455 	struct mlx4_en_dev *mdev = priv->mdev;
1456 	struct mlx4_en_addr_list *addr_list, *tmp;
1457 	int i;
1458 	u8 mc_list[16] = {0};
1459 
1460 	if (!priv->port_up) {
1461 		en_dbg(DRV, priv, "stop port called while port already down\n");
1462 		return;
1463 	}
1464 
1465 #ifdef CONFIG_DEBUG_FS
1466 	mlx4_en_delete_debug_files(priv);
1467 #endif
1468 
1469 	/* close port*/
1470 	mlx4_CLOSE_PORT(mdev->dev, priv->port);
1471 
1472 	/* Set port as not active */
1473 	priv->port_up = false;
1474 	priv->counter_index = MLX4_SINK_COUNTER_INDEX(mdev->dev);
1475 
1476 	/* Promsicuous mode */
1477 	if (mdev->dev->caps.steering_mode ==
1478 	    MLX4_STEERING_MODE_DEVICE_MANAGED) {
1479 		priv->flags &= ~(MLX4_EN_FLAG_PROMISC |
1480 				 MLX4_EN_FLAG_MC_PROMISC);
1481 		mlx4_flow_steer_promisc_remove(mdev->dev,
1482 					       priv->port,
1483 					       MLX4_FS_ALL_DEFAULT);
1484 		mlx4_flow_steer_promisc_remove(mdev->dev,
1485 					       priv->port,
1486 					       MLX4_FS_MC_DEFAULT);
1487 	} else if (priv->flags & MLX4_EN_FLAG_PROMISC) {
1488 		priv->flags &= ~MLX4_EN_FLAG_PROMISC;
1489 
1490 		/* Disable promiscouos mode */
1491 		mlx4_unicast_promisc_remove(mdev->dev, priv->base_qpn,
1492 					    priv->port);
1493 
1494 		/* Disable Multicast promisc */
1495 		if (priv->flags & MLX4_EN_FLAG_MC_PROMISC) {
1496 			mlx4_multicast_promisc_remove(mdev->dev, priv->base_qpn,
1497 						      priv->port);
1498 			priv->flags &= ~MLX4_EN_FLAG_MC_PROMISC;
1499 		}
1500 	}
1501 
1502 	/* Detach All unicasts */
1503 	list_for_each_entry(addr_list, &priv->curr_uc_list, list) {
1504 		mlx4_en_uc_steer_release(priv, addr_list->addr,
1505 					 priv->rss_map.indir_qp.qpn,
1506 					 addr_list->reg_id);
1507 	}
1508 	mlx4_en_clear_uclist(dev);
1509 	list_for_each_entry_safe(addr_list, tmp, &priv->curr_uc_list, list) {
1510 		list_del(&addr_list->list);
1511 		kfree(addr_list);
1512 	}
1513 
1514 	/* Detach All multicasts */
1515 	memset(&mc_list[10], 0xff, ETH_ALEN);
1516 	mc_list[5] = priv->port; /* needed for B0 steering support */
1517 	mlx4_multicast_detach(mdev->dev, &priv->rss_map.indir_qp, mc_list,
1518 			      MLX4_PROT_ETH, priv->broadcast_id);
1519 	list_for_each_entry(addr_list, &priv->curr_mc_list, list) {
1520 		memcpy(&mc_list[10], addr_list->addr, ETH_ALEN);
1521 		mc_list[5] = priv->port;
1522 		mlx4_multicast_detach(mdev->dev, &priv->rss_map.indir_qp,
1523 				      mc_list, MLX4_PROT_ETH, addr_list->reg_id);
1524 	}
1525 	mlx4_en_clear_mclist(dev);
1526 	list_for_each_entry_safe(addr_list, tmp, &priv->curr_mc_list, list) {
1527 		list_del(&addr_list->list);
1528 		kfree(addr_list);
1529 	}
1530 
1531 	/* Flush multicast filter */
1532 	mlx4_SET_MCAST_FLTR(mdev->dev, priv->port, 0, 1, MLX4_MCAST_CONFIG);
1533 	mlx4_en_destroy_drop_qp(priv);
1534 
1535 	/* Free TX Rings */
1536 	for (i = 0; i < priv->tx_ring_num; i++) {
1537 		mlx4_en_deactivate_tx_ring(priv, priv->tx_ring[i]);
1538 		mlx4_en_deactivate_cq(priv, priv->tx_cq[i]);
1539 	}
1540 	msleep(10);
1541 
1542 	for (i = 0; i < priv->tx_ring_num; i++)
1543 		mlx4_en_free_tx_buf(dev, priv->tx_ring[i]);
1544 
1545 	/* Free RSS qps */
1546 	mlx4_en_release_rss_steer(priv);
1547 
1548 	/* Unregister Mac address for the port */
1549 	mlx4_en_put_qp(priv);
1550 	mdev->mac_removed[priv->port] = 1;
1551 
1552 	/* Free RX Rings */
1553 	for (i = 0; i < priv->rx_ring_num; i++) {
1554 		struct mlx4_en_cq *cq = priv->rx_cq[i];
1555 		mlx4_en_deactivate_rx_ring(priv, priv->rx_ring[i]);
1556 		mlx4_en_deactivate_cq(priv, cq);
1557 	}
1558 
1559         callout_stop(&priv->watchdog_timer);
1560 
1561         dev->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
1562 }
1563 
1564 static void mlx4_en_restart(struct work_struct *work)
1565 {
1566 	struct mlx4_en_priv *priv = container_of(work, struct mlx4_en_priv,
1567 						 watchdog_task);
1568 	struct mlx4_en_dev *mdev = priv->mdev;
1569 	struct net_device *dev = priv->dev;
1570 	struct mlx4_en_tx_ring *ring;
1571 	int i;
1572 
1573 
1574 	if (priv->blocked == 0 || priv->port_up == 0)
1575 		return;
1576 	for (i = 0; i < priv->tx_ring_num; i++) {
1577 		ring = priv->tx_ring[i];
1578 		if (ring->blocked &&
1579 				ring->watchdog_time + MLX4_EN_WATCHDOG_TIMEOUT < ticks)
1580 			goto reset;
1581 	}
1582 	return;
1583 
1584 reset:
1585 	priv->port_stats.tx_timeout++;
1586 	en_dbg(DRV, priv, "Watchdog task called for port %d\n", priv->port);
1587 
1588 	mutex_lock(&mdev->state_lock);
1589 	if (priv->port_up) {
1590 		mlx4_en_stop_port(dev);
1591                 //for (i = 0; i < priv->tx_ring_num; i++)
1592                 //        netdev_tx_reset_queue(priv->tx_ring[i]->tx_queue);
1593 		if (mlx4_en_start_port(dev))
1594 			en_err(priv, "Failed restarting port %d\n", priv->port);
1595 	}
1596 	mutex_unlock(&mdev->state_lock);
1597 }
1598 
1599 static void mlx4_en_clear_stats(struct net_device *dev)
1600 {
1601 	struct mlx4_en_priv *priv = netdev_priv(dev);
1602 	struct mlx4_en_dev *mdev = priv->mdev;
1603 	int i;
1604 
1605 	if (!mlx4_is_slave(mdev->dev))
1606 		if (mlx4_en_DUMP_ETH_STATS(mdev, priv->port, 1))
1607 			en_dbg(HW, priv, "Failed dumping statistics\n");
1608 
1609 	memset(&priv->pstats, 0, sizeof(priv->pstats));
1610 	memset(&priv->pkstats, 0, sizeof(priv->pkstats));
1611 	memset(&priv->port_stats, 0, sizeof(priv->port_stats));
1612 	memset(&priv->vport_stats, 0, sizeof(priv->vport_stats));
1613 
1614 	for (i = 0; i < priv->tx_ring_num; i++) {
1615 		priv->tx_ring[i]->bytes = 0;
1616 		priv->tx_ring[i]->packets = 0;
1617 		priv->tx_ring[i]->tx_csum = 0;
1618 		priv->tx_ring[i]->oversized_packets = 0;
1619 	}
1620 	for (i = 0; i < priv->rx_ring_num; i++) {
1621 		priv->rx_ring[i]->bytes = 0;
1622 		priv->rx_ring[i]->packets = 0;
1623 		priv->rx_ring[i]->csum_ok = 0;
1624 		priv->rx_ring[i]->csum_none = 0;
1625 	}
1626 }
1627 
1628 static void mlx4_en_open(void* arg)
1629 {
1630 
1631         struct mlx4_en_priv *priv;
1632         struct mlx4_en_dev *mdev;
1633         struct net_device *dev;
1634         int err = 0;
1635 
1636         priv = arg;
1637         mdev = priv->mdev;
1638         dev = priv->dev;
1639 
1640 
1641 	mutex_lock(&mdev->state_lock);
1642 
1643 	if (!mdev->device_up) {
1644 		en_err(priv, "Cannot open - device down/disabled\n");
1645 		goto out;
1646 	}
1647 
1648 	/* Reset HW statistics and SW counters */
1649 	mlx4_en_clear_stats(dev);
1650 
1651 	err = mlx4_en_start_port(dev);
1652 	if (err)
1653 		en_err(priv, "Failed starting port:%d\n", priv->port);
1654 
1655 out:
1656 	mutex_unlock(&mdev->state_lock);
1657 	return;
1658 }
1659 
1660 void mlx4_en_free_resources(struct mlx4_en_priv *priv)
1661 {
1662 	int i;
1663 
1664 #ifdef CONFIG_RFS_ACCEL
1665 	if (priv->dev->rx_cpu_rmap) {
1666 		free_irq_cpu_rmap(priv->dev->rx_cpu_rmap);
1667 		priv->dev->rx_cpu_rmap = NULL;
1668 	}
1669 #endif
1670 
1671 	for (i = 0; i < priv->tx_ring_num; i++) {
1672 		if (priv->tx_ring && priv->tx_ring[i])
1673 			mlx4_en_destroy_tx_ring(priv, &priv->tx_ring[i]);
1674 		if (priv->tx_cq && priv->tx_cq[i])
1675 			mlx4_en_destroy_cq(priv, &priv->tx_cq[i]);
1676 	}
1677 
1678 	for (i = 0; i < priv->rx_ring_num; i++) {
1679 		if (priv->rx_ring[i])
1680 			mlx4_en_destroy_rx_ring(priv, &priv->rx_ring[i],
1681 				priv->prof->rx_ring_size, priv->stride);
1682 		if (priv->rx_cq[i])
1683 			mlx4_en_destroy_cq(priv, &priv->rx_cq[i]);
1684 	}
1685 
1686 	if (priv->stat_sysctl != NULL)
1687 		sysctl_ctx_free(&priv->stat_ctx);
1688 }
1689 
1690 int mlx4_en_alloc_resources(struct mlx4_en_priv *priv)
1691 {
1692 	struct mlx4_en_port_profile *prof = priv->prof;
1693 	int i;
1694 	int node = 0;
1695 
1696 	/* Create rx Rings */
1697 	for (i = 0; i < priv->rx_ring_num; i++) {
1698 		if (mlx4_en_create_cq(priv, &priv->rx_cq[i],
1699 				      prof->rx_ring_size, i, RX, node))
1700 			goto err;
1701 
1702 		if (mlx4_en_create_rx_ring(priv, &priv->rx_ring[i],
1703 					   prof->rx_ring_size, node))
1704 			goto err;
1705 	}
1706 
1707 	/* Create tx Rings */
1708 	for (i = 0; i < priv->tx_ring_num; i++) {
1709 		if (mlx4_en_create_cq(priv, &priv->tx_cq[i],
1710 				      prof->tx_ring_size, i, TX, node))
1711 			goto err;
1712 
1713 		if (mlx4_en_create_tx_ring(priv, &priv->tx_ring[i],
1714 					   prof->tx_ring_size, TXBB_SIZE, node, i))
1715 			goto err;
1716 	}
1717 
1718 #ifdef CONFIG_RFS_ACCEL
1719 	priv->dev->rx_cpu_rmap = alloc_irq_cpu_rmap(priv->rx_ring_num);
1720 	if (!priv->dev->rx_cpu_rmap)
1721 		goto err;
1722 #endif
1723         /* Re-create stat sysctls in case the number of rings changed. */
1724 	mlx4_en_sysctl_stat(priv);
1725 	return 0;
1726 
1727 err:
1728 	en_err(priv, "Failed to allocate NIC resources\n");
1729 	for (i = 0; i < priv->rx_ring_num; i++) {
1730 		if (priv->rx_ring[i])
1731 			mlx4_en_destroy_rx_ring(priv, &priv->rx_ring[i],
1732 						prof->rx_ring_size,
1733 						priv->stride);
1734 		if (priv->rx_cq[i])
1735 			mlx4_en_destroy_cq(priv, &priv->rx_cq[i]);
1736 	}
1737 	for (i = 0; i < priv->tx_ring_num; i++) {
1738 		if (priv->tx_ring[i])
1739 			mlx4_en_destroy_tx_ring(priv, &priv->tx_ring[i]);
1740 		if (priv->tx_cq[i])
1741 			mlx4_en_destroy_cq(priv, &priv->tx_cq[i]);
1742 	}
1743 	priv->port_up = false;
1744 	return -ENOMEM;
1745 }
1746 
1747 struct en_port_attribute {
1748 	struct attribute attr;
1749 	ssize_t (*show)(struct en_port *, struct en_port_attribute *, char *buf);
1750 	ssize_t (*store)(struct en_port *, struct en_port_attribute *, char *buf, size_t count);
1751 };
1752 
1753 #define PORT_ATTR_RO(_name) \
1754 struct en_port_attribute en_port_attr_##_name = __ATTR_RO(_name)
1755 
1756 #define EN_PORT_ATTR(_name, _mode, _show, _store) \
1757 struct en_port_attribute en_port_attr_##_name = __ATTR(_name, _mode, _show, _store)
1758 
1759 void mlx4_en_destroy_netdev(struct net_device *dev)
1760 {
1761 	struct mlx4_en_priv *priv = netdev_priv(dev);
1762 	struct mlx4_en_dev *mdev = priv->mdev;
1763 
1764 	en_dbg(DRV, priv, "Destroying netdev on port:%d\n", priv->port);
1765 
1766 	/* don't allow more IOCTLs */
1767 	priv->gone = 1;
1768 
1769 	/* XXX wait a bit to allow IOCTL handlers to complete */
1770 	pause("W", hz);
1771 
1772 	if (priv->vlan_attach != NULL)
1773 		EVENTHANDLER_DEREGISTER(vlan_config, priv->vlan_attach);
1774 	if (priv->vlan_detach != NULL)
1775 		EVENTHANDLER_DEREGISTER(vlan_unconfig, priv->vlan_detach);
1776 
1777 	/* Unregister device - this will close the port if it was up */
1778 	if (priv->registered) {
1779 		mutex_lock(&mdev->state_lock);
1780 		ether_ifdetach(dev);
1781 		mutex_unlock(&mdev->state_lock);
1782 	}
1783 
1784 	mutex_lock(&mdev->state_lock);
1785 	mlx4_en_stop_port(dev);
1786 	mutex_unlock(&mdev->state_lock);
1787 
1788 	if (priv->allocated)
1789 		mlx4_free_hwq_res(mdev->dev, &priv->res, MLX4_EN_PAGE_SIZE);
1790 
1791 	cancel_delayed_work(&priv->stats_task);
1792 	cancel_delayed_work(&priv->service_task);
1793 	/* flush any pending task for this netdev */
1794 	flush_workqueue(mdev->workqueue);
1795         callout_drain(&priv->watchdog_timer);
1796 
1797 	/* Detach the netdev so tasks would not attempt to access it */
1798 	mutex_lock(&mdev->state_lock);
1799 	mdev->pndev[priv->port] = NULL;
1800 	mutex_unlock(&mdev->state_lock);
1801 
1802 
1803 	mlx4_en_free_resources(priv);
1804 
1805 	/* freeing the sysctl conf cannot be called from within mlx4_en_free_resources */
1806 	if (priv->conf_sysctl != NULL)
1807 		sysctl_ctx_free(&priv->conf_ctx);
1808 
1809 	kfree(priv->tx_ring);
1810 	kfree(priv->tx_cq);
1811 
1812         kfree(priv);
1813         if_free(dev);
1814 
1815 }
1816 
1817 static int mlx4_en_change_mtu(struct net_device *dev, int new_mtu)
1818 {
1819 	struct mlx4_en_priv *priv = netdev_priv(dev);
1820 	struct mlx4_en_dev *mdev = priv->mdev;
1821 	int err = 0;
1822 
1823 	en_dbg(DRV, priv, "Change MTU called - current:%u new:%u\n",
1824 	       (unsigned)dev->if_mtu, (unsigned)new_mtu);
1825 
1826 	if ((new_mtu < MLX4_EN_MIN_MTU) || (new_mtu > priv->max_mtu)) {
1827 		en_err(priv, "Bad MTU size:%d, max %u.\n", new_mtu,
1828 		    priv->max_mtu);
1829 		return -EPERM;
1830 	}
1831 	mutex_lock(&mdev->state_lock);
1832 	dev->if_mtu = new_mtu;
1833 	if (dev->if_drv_flags & IFF_DRV_RUNNING) {
1834 		if (!mdev->device_up) {
1835 			/* NIC is probably restarting - let watchdog task reset
1836 			 *                          * the port */
1837 			en_dbg(DRV, priv, "Change MTU called with card down!?\n");
1838 		} else {
1839 			mlx4_en_stop_port(dev);
1840 			err = mlx4_en_start_port(dev);
1841 			if (err) {
1842 				en_err(priv, "Failed restarting port:%d\n",
1843 						priv->port);
1844 				queue_work(mdev->workqueue, &priv->watchdog_task);
1845 			}
1846 		}
1847 	}
1848 	mutex_unlock(&mdev->state_lock);
1849 	return 0;
1850 }
1851 
1852 static int mlx4_en_calc_media(struct mlx4_en_priv *priv)
1853 {
1854 	int trans_type;
1855 	int active;
1856 
1857 	active = IFM_ETHER;
1858 	if (priv->last_link_state == MLX4_DEV_EVENT_PORT_DOWN)
1859 		return (active);
1860 	active |= IFM_FDX;
1861 	trans_type = priv->port_state.transceiver;
1862 	/* XXX I don't know all of the transceiver values. */
1863 	switch (priv->port_state.link_speed) {
1864 	case 100:
1865 		active |= IFM_100_T;
1866 		break;
1867 	case 1000:
1868 		active |= IFM_1000_T;
1869 		break;
1870 	case 10000:
1871 		if (trans_type > 0 && trans_type <= 0xC)
1872 			active |= IFM_10G_SR;
1873 		else if (trans_type == 0x80 || trans_type == 0)
1874 			active |= IFM_10G_CX4;
1875 		break;
1876 	case 40000:
1877 		active |= IFM_40G_CR4;
1878 		break;
1879 	}
1880 	if (priv->prof->tx_pause)
1881 		active |= IFM_ETH_TXPAUSE;
1882 	if (priv->prof->rx_pause)
1883 		active |= IFM_ETH_RXPAUSE;
1884 
1885 	return (active);
1886 }
1887 
1888 static void mlx4_en_media_status(struct ifnet *dev, struct ifmediareq *ifmr)
1889 {
1890 	struct mlx4_en_priv *priv;
1891 
1892 	priv = dev->if_softc;
1893 	ifmr->ifm_status = IFM_AVALID;
1894 	if (priv->last_link_state != MLX4_DEV_EVENT_PORT_DOWN)
1895 		ifmr->ifm_status |= IFM_ACTIVE;
1896 	ifmr->ifm_active = mlx4_en_calc_media(priv);
1897 
1898 	return;
1899 }
1900 
1901 static int mlx4_en_media_change(struct ifnet *dev)
1902 {
1903 	struct mlx4_en_priv *priv;
1904         struct ifmedia *ifm;
1905 	int rxpause;
1906 	int txpause;
1907 	int error;
1908 
1909 	priv = dev->if_softc;
1910 	ifm = &priv->media;
1911 	rxpause = txpause = 0;
1912 	error = 0;
1913 
1914 	if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
1915 		return (EINVAL);
1916         switch (IFM_SUBTYPE(ifm->ifm_media)) {
1917         case IFM_AUTO:
1918 		break;
1919 	case IFM_10G_SR:
1920 	case IFM_10G_CX4:
1921 	case IFM_1000_T:
1922 	case IFM_40G_CR4:
1923 		if ((IFM_SUBTYPE(ifm->ifm_media)
1924 			== IFM_SUBTYPE(mlx4_en_calc_media(priv)))
1925 			&& (ifm->ifm_media & IFM_FDX))
1926 			break;
1927 		/* Fallthrough */
1928 	default:
1929                 printf("%s: Only auto media type\n", if_name(dev));
1930                 return (EINVAL);
1931 	}
1932 	/* Allow user to set/clear pause */
1933 	if (IFM_OPTIONS(ifm->ifm_media) & IFM_ETH_RXPAUSE)
1934 		rxpause = 1;
1935 	if (IFM_OPTIONS(ifm->ifm_media) & IFM_ETH_TXPAUSE)
1936 		txpause = 1;
1937 	if (priv->prof->tx_pause != txpause || priv->prof->rx_pause != rxpause) {
1938 		priv->prof->tx_pause = txpause;
1939 		priv->prof->rx_pause = rxpause;
1940 		error = -mlx4_SET_PORT_general(priv->mdev->dev, priv->port,
1941 		     priv->rx_mb_size + ETHER_CRC_LEN, priv->prof->tx_pause,
1942 		     priv->prof->tx_ppp, priv->prof->rx_pause,
1943 		     priv->prof->rx_ppp);
1944 	}
1945 	return (error);
1946 }
1947 
1948 static int mlx4_en_ioctl(struct ifnet *dev, u_long command, caddr_t data)
1949 {
1950 	struct mlx4_en_priv *priv;
1951 	struct mlx4_en_dev *mdev;
1952 	struct ifreq *ifr;
1953 	int error;
1954 	int mask;
1955 	struct ifrsskey *ifrk;
1956 	const u32 *key;
1957 	struct ifrsshash *ifrh;
1958 	u8 rss_mask;
1959 
1960 	error = 0;
1961 	mask = 0;
1962 	priv = dev->if_softc;
1963 
1964 	/* check if detaching */
1965 	if (priv == NULL || priv->gone != 0)
1966 		return (ENXIO);
1967 
1968 	mdev = priv->mdev;
1969 	ifr = (struct ifreq *) data;
1970 
1971 	switch (command) {
1972 	case SIOCSIFMTU:
1973 		error = -mlx4_en_change_mtu(dev, ifr->ifr_mtu);
1974 		break;
1975 	case SIOCSIFFLAGS:
1976 		if (dev->if_flags & IFF_UP) {
1977 			if ((dev->if_drv_flags & IFF_DRV_RUNNING) == 0) {
1978 				mutex_lock(&mdev->state_lock);
1979 				mlx4_en_start_port(dev);
1980 				mutex_unlock(&mdev->state_lock);
1981 			} else {
1982 				mlx4_en_set_rx_mode(dev);
1983 			}
1984 		} else {
1985 			mutex_lock(&mdev->state_lock);
1986 			if (dev->if_drv_flags & IFF_DRV_RUNNING) {
1987 				mlx4_en_stop_port(dev);
1988 				if_link_state_change(dev, LINK_STATE_DOWN);
1989 			}
1990 			mutex_unlock(&mdev->state_lock);
1991 		}
1992 		break;
1993 	case SIOCADDMULTI:
1994 	case SIOCDELMULTI:
1995 		mlx4_en_set_rx_mode(dev);
1996 		break;
1997 	case SIOCSIFMEDIA:
1998 	case SIOCGIFMEDIA:
1999 		error = ifmedia_ioctl(dev, ifr, &priv->media, command);
2000 		break;
2001 	case SIOCSIFCAP:
2002 		mutex_lock(&mdev->state_lock);
2003 		mask = ifr->ifr_reqcap ^ dev->if_capenable;
2004 		if (mask & IFCAP_TXCSUM) {
2005 			dev->if_capenable ^= IFCAP_TXCSUM;
2006 			dev->if_hwassist ^= (CSUM_TCP | CSUM_UDP | CSUM_IP);
2007 
2008 			if (IFCAP_TSO4 & dev->if_capenable &&
2009 			    !(IFCAP_TXCSUM & dev->if_capenable)) {
2010 				dev->if_capenable &= ~IFCAP_TSO4;
2011 				dev->if_hwassist &= ~CSUM_IP_TSO;
2012 				if_printf(dev,
2013 				    "tso4 disabled due to -txcsum.\n");
2014 			}
2015 		}
2016 		if (mask & IFCAP_TXCSUM_IPV6) {
2017 			dev->if_capenable ^= IFCAP_TXCSUM_IPV6;
2018 			dev->if_hwassist ^= (CSUM_UDP_IPV6 | CSUM_TCP_IPV6);
2019 
2020 			if (IFCAP_TSO6 & dev->if_capenable &&
2021 			    !(IFCAP_TXCSUM_IPV6 & dev->if_capenable)) {
2022 				dev->if_capenable &= ~IFCAP_TSO6;
2023 				dev->if_hwassist &= ~CSUM_IP6_TSO;
2024 				if_printf(dev,
2025 				    "tso6 disabled due to -txcsum6.\n");
2026 			}
2027 		}
2028 		if (mask & IFCAP_RXCSUM)
2029 			dev->if_capenable ^= IFCAP_RXCSUM;
2030 		if (mask & IFCAP_RXCSUM_IPV6)
2031 			dev->if_capenable ^= IFCAP_RXCSUM_IPV6;
2032 
2033 		if (mask & IFCAP_TSO4) {
2034 			if (!(IFCAP_TSO4 & dev->if_capenable) &&
2035 			    !(IFCAP_TXCSUM & dev->if_capenable)) {
2036 				if_printf(dev, "enable txcsum first.\n");
2037 				error = EAGAIN;
2038 				goto out;
2039 			}
2040 			dev->if_capenable ^= IFCAP_TSO4;
2041 			dev->if_hwassist ^= CSUM_IP_TSO;
2042 		}
2043 		if (mask & IFCAP_TSO6) {
2044 			if (!(IFCAP_TSO6 & dev->if_capenable) &&
2045 			    !(IFCAP_TXCSUM_IPV6 & dev->if_capenable)) {
2046 				if_printf(dev, "enable txcsum6 first.\n");
2047 				error = EAGAIN;
2048 				goto out;
2049 			}
2050 			dev->if_capenable ^= IFCAP_TSO6;
2051 			dev->if_hwassist ^= CSUM_IP6_TSO;
2052 		}
2053 		if (mask & IFCAP_LRO)
2054 			dev->if_capenable ^= IFCAP_LRO;
2055 		if (mask & IFCAP_VLAN_HWTAGGING)
2056 			dev->if_capenable ^= IFCAP_VLAN_HWTAGGING;
2057 		if (mask & IFCAP_VLAN_HWFILTER)
2058 			dev->if_capenable ^= IFCAP_VLAN_HWFILTER;
2059 		if (mask & IFCAP_WOL_MAGIC)
2060 			dev->if_capenable ^= IFCAP_WOL_MAGIC;
2061 		if (dev->if_drv_flags & IFF_DRV_RUNNING)
2062 			mlx4_en_start_port(dev);
2063 out:
2064 		mutex_unlock(&mdev->state_lock);
2065 		VLAN_CAPABILITIES(dev);
2066 		break;
2067 #if __FreeBSD_version >= 1100036
2068 	case SIOCGI2C: {
2069 		struct ifi2creq i2c;
2070 
2071 		error = copyin(ifr_data_get_ptr(ifr), &i2c, sizeof(i2c));
2072 		if (error)
2073 			break;
2074 		if (i2c.len > sizeof(i2c.data)) {
2075 			error = EINVAL;
2076 			break;
2077 		}
2078 		/*
2079 		 * Note that we ignore i2c.addr here. The driver hardcodes
2080 		 * the address to 0x50, while standard expects it to be 0xA0.
2081 		 */
2082 		error = mlx4_get_module_info(mdev->dev, priv->port,
2083 		    i2c.offset, i2c.len, i2c.data);
2084 		if (error < 0) {
2085 			error = -error;
2086 			break;
2087 		}
2088 		error = copyout(&i2c, ifr_data_get_ptr(ifr), sizeof(i2c));
2089 		break;
2090 	}
2091 #endif
2092 	case SIOCGIFRSSKEY:
2093 		ifrk = (struct ifrsskey *)data;
2094 		ifrk->ifrk_func = RSS_FUNC_TOEPLITZ;
2095 		mutex_lock(&mdev->state_lock);
2096 		key = mlx4_en_get_rss_key(priv, &ifrk->ifrk_keylen);
2097 		if (ifrk->ifrk_keylen > RSS_KEYLEN)
2098 			error = EINVAL;
2099 		else
2100 			memcpy(ifrk->ifrk_key, key, ifrk->ifrk_keylen);
2101 		mutex_unlock(&mdev->state_lock);
2102 		break;
2103 
2104 	case SIOCGIFRSSHASH:
2105 		mutex_lock(&mdev->state_lock);
2106 		rss_mask = mlx4_en_get_rss_mask(priv);
2107 		mutex_unlock(&mdev->state_lock);
2108 		ifrh = (struct ifrsshash *)data;
2109 		ifrh->ifrh_func = RSS_FUNC_TOEPLITZ;
2110 		ifrh->ifrh_types = 0;
2111 		if (rss_mask & MLX4_RSS_IPV4)
2112 			ifrh->ifrh_types |= RSS_TYPE_IPV4;
2113 		if (rss_mask & MLX4_RSS_TCP_IPV4)
2114 			ifrh->ifrh_types |= RSS_TYPE_TCP_IPV4;
2115 		if (rss_mask & MLX4_RSS_IPV6)
2116 			ifrh->ifrh_types |= RSS_TYPE_IPV6;
2117 		if (rss_mask & MLX4_RSS_TCP_IPV6)
2118 			ifrh->ifrh_types |= RSS_TYPE_TCP_IPV6;
2119 		if (rss_mask & MLX4_RSS_UDP_IPV4)
2120 			ifrh->ifrh_types |= RSS_TYPE_UDP_IPV4;
2121 		if (rss_mask & MLX4_RSS_UDP_IPV6)
2122 			ifrh->ifrh_types |= RSS_TYPE_UDP_IPV6;
2123 		break;
2124 
2125 	default:
2126 		error = ether_ioctl(dev, command, data);
2127 		break;
2128 	}
2129 
2130 	return (error);
2131 }
2132 
2133 
2134 int mlx4_en_init_netdev(struct mlx4_en_dev *mdev, int port,
2135 			struct mlx4_en_port_profile *prof)
2136 {
2137 	struct net_device *dev;
2138 	struct mlx4_en_priv *priv;
2139 	uint8_t dev_addr[ETHER_ADDR_LEN];
2140 	int err;
2141 	int i;
2142 
2143 	priv = kzalloc(sizeof(*priv), GFP_KERNEL);
2144 	dev = priv->dev = if_alloc(IFT_ETHER);
2145 	if (dev == NULL) {
2146 		en_err(priv, "Net device allocation failed\n");
2147 		kfree(priv);
2148 		return -ENOMEM;
2149 	}
2150 	dev->if_softc = priv;
2151 	if_initname(dev, "mlxen", (device_get_unit(
2152 	    mdev->pdev->dev.bsddev) * MLX4_MAX_PORTS) + port - 1);
2153 	dev->if_mtu = ETHERMTU;
2154 	dev->if_init = mlx4_en_open;
2155 	dev->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
2156 	dev->if_ioctl = mlx4_en_ioctl;
2157 	dev->if_transmit = mlx4_en_transmit;
2158 	dev->if_qflush = mlx4_en_qflush;
2159 	dev->if_snd.ifq_maxlen = prof->tx_ring_size;
2160 
2161 	/*
2162 	 * Initialize driver private data
2163 	 */
2164 	priv->counter_index = 0xff;
2165 	spin_lock_init(&priv->stats_lock);
2166 	INIT_WORK(&priv->rx_mode_task, mlx4_en_do_set_rx_mode);
2167 	INIT_WORK(&priv->watchdog_task, mlx4_en_restart);
2168 	INIT_WORK(&priv->linkstate_task, mlx4_en_linkstate);
2169 	INIT_DELAYED_WORK(&priv->stats_task, mlx4_en_do_get_stats);
2170 	INIT_DELAYED_WORK(&priv->service_task, mlx4_en_service_task);
2171 	callout_init(&priv->watchdog_timer, 1);
2172 #ifdef CONFIG_RFS_ACCEL
2173 	INIT_LIST_HEAD(&priv->filters);
2174 	spin_lock_init(&priv->filters_lock);
2175 #endif
2176 
2177 	priv->msg_enable = MLX4_EN_MSG_LEVEL;
2178 	priv->dev = dev;
2179 	priv->mdev = mdev;
2180 	priv->ddev = &mdev->pdev->dev;
2181 	priv->prof = prof;
2182 	priv->port = port;
2183 	priv->port_up = false;
2184 	priv->flags = prof->flags;
2185 
2186 	priv->num_tx_rings_p_up = mdev->profile.num_tx_rings_p_up;
2187 	priv->tx_ring_num = prof->tx_ring_num;
2188 	priv->tx_ring = kcalloc(MAX_TX_RINGS,
2189 				sizeof(struct mlx4_en_tx_ring *), GFP_KERNEL);
2190 	if (!priv->tx_ring) {
2191 		err = -ENOMEM;
2192 		goto out;
2193 	}
2194 	priv->tx_cq = kcalloc(sizeof(struct mlx4_en_cq *), MAX_TX_RINGS,
2195 			GFP_KERNEL);
2196 	if (!priv->tx_cq) {
2197 		err = -ENOMEM;
2198 		goto out;
2199 	}
2200 
2201 	priv->rx_ring_num = prof->rx_ring_num;
2202 	priv->cqe_factor = (mdev->dev->caps.cqe_size == 64) ? 1 : 0;
2203 	priv->mac_index = -1;
2204 	priv->last_ifq_jiffies = 0;
2205 	priv->if_counters_rx_errors = 0;
2206 	priv->if_counters_rx_no_buffer = 0;
2207 #ifdef CONFIG_MLX4_EN_DCB
2208 	if (!mlx4_is_slave(priv->mdev->dev)) {
2209 		priv->dcbx_cap = DCB_CAP_DCBX_HOST;
2210 		priv->flags |= MLX4_EN_FLAG_DCB_ENABLED;
2211 		if (mdev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_ETS_CFG) {
2212 			dev->dcbnl_ops = &mlx4_en_dcbnl_ops;
2213 		} else {
2214 			en_info(priv, "QoS disabled - no HW support\n");
2215 			dev->dcbnl_ops = &mlx4_en_dcbnl_pfc_ops;
2216 		}
2217 	}
2218 #endif
2219 
2220 	/* Query for default mac and max mtu */
2221 	priv->max_mtu = mdev->dev->caps.eth_mtu_cap[priv->port];
2222         priv->mac = mdev->dev->caps.def_mac[priv->port];
2223         if (ILLEGAL_MAC(priv->mac)) {
2224 #if BITS_PER_LONG == 64
2225                 en_err(priv, "Port: %d, invalid mac burned: 0x%lx, quiting\n",
2226                                 priv->port, priv->mac);
2227 #elif BITS_PER_LONG == 32
2228                 en_err(priv, "Port: %d, invalid mac burned: 0x%llx, quiting\n",
2229                                 priv->port, priv->mac);
2230 #endif
2231                 err = -EINVAL;
2232                 goto out;
2233         }
2234 
2235 	priv->stride = roundup_pow_of_two(sizeof(struct mlx4_en_rx_desc) +
2236 					  DS_SIZE);
2237 
2238 	mlx4_en_sysctl_conf(priv);
2239 
2240 	err = mlx4_en_alloc_resources(priv);
2241 	if (err)
2242 		goto out;
2243 
2244 	/* Allocate page for receive rings */
2245 	err = mlx4_alloc_hwq_res(mdev->dev, &priv->res,
2246 				MLX4_EN_PAGE_SIZE, MLX4_EN_PAGE_SIZE);
2247 	if (err) {
2248 		en_err(priv, "Failed to allocate page for rx qps\n");
2249 		goto out;
2250 	}
2251 	priv->allocated = 1;
2252 
2253 	/*
2254 	 * Set driver features
2255 	 */
2256 	dev->if_capabilities |= IFCAP_HWCSUM | IFCAP_HWCSUM_IPV6;
2257 	dev->if_capabilities |= IFCAP_VLAN_MTU | IFCAP_VLAN_HWTAGGING;
2258 	dev->if_capabilities |= IFCAP_VLAN_HWCSUM | IFCAP_VLAN_HWFILTER;
2259 	dev->if_capabilities |= IFCAP_LINKSTATE | IFCAP_JUMBO_MTU;
2260 	dev->if_capabilities |= IFCAP_LRO;
2261 	dev->if_capabilities |= IFCAP_HWSTATS;
2262 
2263 	if (mdev->LSO_support)
2264 		dev->if_capabilities |= IFCAP_TSO4 | IFCAP_TSO6 | IFCAP_VLAN_HWTSO;
2265 
2266 #if __FreeBSD_version >= 1100000
2267 	/* set TSO limits so that we don't have to drop TX packets */
2268 	dev->if_hw_tsomax = MLX4_EN_TX_MAX_PAYLOAD_SIZE - (ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN) /* hdr */;
2269 	dev->if_hw_tsomaxsegcount = MLX4_EN_TX_MAX_MBUF_FRAGS - 1 /* hdr */;
2270 	dev->if_hw_tsomaxsegsize = MLX4_EN_TX_MAX_MBUF_SIZE;
2271 #endif
2272 
2273 	dev->if_capenable = dev->if_capabilities;
2274 
2275 	dev->if_hwassist = 0;
2276 	if (dev->if_capenable & (IFCAP_TSO4 | IFCAP_TSO6))
2277 		dev->if_hwassist |= CSUM_TSO;
2278 	if (dev->if_capenable & IFCAP_TXCSUM)
2279 		dev->if_hwassist |= (CSUM_TCP | CSUM_UDP | CSUM_IP);
2280 	if (dev->if_capenable & IFCAP_TXCSUM_IPV6)
2281 		dev->if_hwassist |= (CSUM_UDP_IPV6 | CSUM_TCP_IPV6);
2282 
2283 
2284         /* Register for VLAN events */
2285 	priv->vlan_attach = EVENTHANDLER_REGISTER(vlan_config,
2286             mlx4_en_vlan_rx_add_vid, priv, EVENTHANDLER_PRI_FIRST);
2287 	priv->vlan_detach = EVENTHANDLER_REGISTER(vlan_unconfig,
2288             mlx4_en_vlan_rx_kill_vid, priv, EVENTHANDLER_PRI_FIRST);
2289 
2290 	mdev->pndev[priv->port] = dev;
2291 
2292 	priv->last_link_state = MLX4_DEV_EVENT_PORT_DOWN;
2293         mlx4_en_set_default_moderation(priv);
2294 
2295 	/* Set default MAC */
2296 	for (i = 0; i < ETHER_ADDR_LEN; i++)
2297 		dev_addr[ETHER_ADDR_LEN - 1 - i] = (u8) (priv->mac >> (8 * i));
2298 
2299 
2300 	ether_ifattach(dev, dev_addr);
2301 	if_link_state_change(dev, LINK_STATE_DOWN);
2302 	ifmedia_init(&priv->media, IFM_IMASK | IFM_ETH_FMASK,
2303 	    mlx4_en_media_change, mlx4_en_media_status);
2304 	ifmedia_add(&priv->media, IFM_ETHER | IFM_FDX | IFM_1000_T, 0, NULL);
2305 	ifmedia_add(&priv->media, IFM_ETHER | IFM_FDX | IFM_10G_SR, 0, NULL);
2306 	ifmedia_add(&priv->media, IFM_ETHER | IFM_FDX | IFM_10G_CX4, 0, NULL);
2307 	ifmedia_add(&priv->media, IFM_ETHER | IFM_FDX | IFM_40G_CR4, 0, NULL);
2308 	ifmedia_add(&priv->media, IFM_ETHER | IFM_AUTO, 0, NULL);
2309 	ifmedia_set(&priv->media, IFM_ETHER | IFM_AUTO);
2310 
2311 	en_warn(priv, "Using %d TX rings\n", prof->tx_ring_num);
2312 	en_warn(priv, "Using %d RX rings\n", prof->rx_ring_num);
2313 
2314 	priv->registered = 1;
2315 
2316         en_warn(priv, "Using %d TX rings\n", prof->tx_ring_num);
2317         en_warn(priv, "Using %d RX rings\n", prof->rx_ring_num);
2318 
2319 
2320 	priv->rx_mb_size = dev->if_mtu + ETH_HLEN + VLAN_HLEN + ETH_FCS_LEN;
2321 	err = mlx4_SET_PORT_general(mdev->dev, priv->port,
2322 				    priv->rx_mb_size,
2323 				    prof->tx_pause, prof->tx_ppp,
2324 				    prof->rx_pause, prof->rx_ppp);
2325 	if (err) {
2326 		en_err(priv, "Failed setting port general configurations "
2327 		       "for port %d, with error %d\n", priv->port, err);
2328 		goto out;
2329 	}
2330 
2331 	/* Init port */
2332 	en_warn(priv, "Initializing port\n");
2333 	err = mlx4_INIT_PORT(mdev->dev, priv->port);
2334 	if (err) {
2335 		en_err(priv, "Failed Initializing port\n");
2336 		goto out;
2337 	}
2338 
2339 	queue_delayed_work(mdev->workqueue, &priv->stats_task, STATS_DELAY);
2340 
2341         if (mdev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_TS)
2342                 queue_delayed_work(mdev->workqueue, &priv->service_task, SERVICE_TASK_DELAY);
2343 
2344 	return 0;
2345 
2346 out:
2347 	mlx4_en_destroy_netdev(dev);
2348 	return err;
2349 }
2350 
2351 static int mlx4_en_set_ring_size(struct net_device *dev,
2352     int rx_size, int tx_size)
2353 {
2354         struct mlx4_en_priv *priv = netdev_priv(dev);
2355         struct mlx4_en_dev *mdev = priv->mdev;
2356         int port_up = 0;
2357         int err = 0;
2358 
2359         rx_size = roundup_pow_of_two(rx_size);
2360         rx_size = max_t(u32, rx_size, MLX4_EN_MIN_RX_SIZE);
2361         rx_size = min_t(u32, rx_size, MLX4_EN_MAX_RX_SIZE);
2362         tx_size = roundup_pow_of_two(tx_size);
2363         tx_size = max_t(u32, tx_size, MLX4_EN_MIN_TX_SIZE);
2364         tx_size = min_t(u32, tx_size, MLX4_EN_MAX_TX_SIZE);
2365 
2366         if (rx_size == (priv->port_up ?
2367             priv->rx_ring[0]->actual_size : priv->rx_ring[0]->size) &&
2368             tx_size == priv->tx_ring[0]->size)
2369                 return 0;
2370         mutex_lock(&mdev->state_lock);
2371         if (priv->port_up) {
2372                 port_up = 1;
2373                 mlx4_en_stop_port(dev);
2374         }
2375         mlx4_en_free_resources(priv);
2376         priv->prof->tx_ring_size = tx_size;
2377         priv->prof->rx_ring_size = rx_size;
2378         err = mlx4_en_alloc_resources(priv);
2379         if (err) {
2380                 en_err(priv, "Failed reallocating port resources\n");
2381                 goto out;
2382         }
2383         if (port_up) {
2384                 err = mlx4_en_start_port(dev);
2385                 if (err)
2386                         en_err(priv, "Failed starting port\n");
2387         }
2388 out:
2389         mutex_unlock(&mdev->state_lock);
2390         return err;
2391 }
2392 static int mlx4_en_set_rx_ring_size(SYSCTL_HANDLER_ARGS)
2393 {
2394         struct mlx4_en_priv *priv;
2395         int size;
2396         int error;
2397 
2398         priv = arg1;
2399         size = priv->prof->rx_ring_size;
2400         error = sysctl_handle_int(oidp, &size, 0, req);
2401         if (error || !req->newptr)
2402                 return (error);
2403         error = -mlx4_en_set_ring_size(priv->dev, size,
2404             priv->prof->tx_ring_size);
2405         return (error);
2406 }
2407 
2408 static int mlx4_en_set_tx_ring_size(SYSCTL_HANDLER_ARGS)
2409 {
2410         struct mlx4_en_priv *priv;
2411         int size;
2412         int error;
2413 
2414         priv = arg1;
2415         size = priv->prof->tx_ring_size;
2416         error = sysctl_handle_int(oidp, &size, 0, req);
2417         if (error || !req->newptr)
2418                 return (error);
2419         error = -mlx4_en_set_ring_size(priv->dev, priv->prof->rx_ring_size,
2420             size);
2421 
2422         return (error);
2423 }
2424 
2425 static int mlx4_en_get_module_info(struct net_device *dev,
2426 				   struct ethtool_modinfo *modinfo)
2427 {
2428 	struct mlx4_en_priv *priv = netdev_priv(dev);
2429 	struct mlx4_en_dev *mdev = priv->mdev;
2430 	int ret;
2431 	u8 data[4];
2432 
2433 	/* Read first 2 bytes to get Module & REV ID */
2434 	ret = mlx4_get_module_info(mdev->dev, priv->port,
2435 				   0/*offset*/, 2/*size*/, data);
2436 
2437 	if (ret < 2) {
2438 		en_err(priv, "Failed to read eeprom module first two bytes, error: 0x%x\n", -ret);
2439 		return -EIO;
2440 	}
2441 
2442 	switch (data[0] /* identifier */) {
2443 	case MLX4_MODULE_ID_QSFP:
2444 		modinfo->type = ETH_MODULE_SFF_8436;
2445 		modinfo->eeprom_len = ETH_MODULE_SFF_8436_LEN;
2446 		break;
2447 	case MLX4_MODULE_ID_QSFP_PLUS:
2448 		if (data[1] >= 0x3) { /* revision id */
2449 			modinfo->type = ETH_MODULE_SFF_8636;
2450 			modinfo->eeprom_len = ETH_MODULE_SFF_8636_LEN;
2451 		} else {
2452 			modinfo->type = ETH_MODULE_SFF_8436;
2453 			modinfo->eeprom_len = ETH_MODULE_SFF_8436_LEN;
2454 		}
2455 		break;
2456 	case MLX4_MODULE_ID_QSFP28:
2457 		modinfo->type = ETH_MODULE_SFF_8636;
2458 		modinfo->eeprom_len = ETH_MODULE_SFF_8636_LEN;
2459 		break;
2460 	case MLX4_MODULE_ID_SFP:
2461 		modinfo->type = ETH_MODULE_SFF_8472;
2462 		modinfo->eeprom_len = ETH_MODULE_SFF_8472_LEN;
2463 		break;
2464 	default:
2465 		en_err(priv, "mlx4_en_get_module_info :  Not recognized cable type\n");
2466 		return -EINVAL;
2467 	}
2468 
2469 	return 0;
2470 }
2471 
2472 static int mlx4_en_get_module_eeprom(struct net_device *dev,
2473 				     struct ethtool_eeprom *ee,
2474 				     u8 *data)
2475 {
2476 	struct mlx4_en_priv *priv = netdev_priv(dev);
2477 	struct mlx4_en_dev *mdev = priv->mdev;
2478 	int offset = ee->offset;
2479 	int i = 0, ret;
2480 
2481 	if (ee->len == 0)
2482 		return -EINVAL;
2483 
2484 	memset(data, 0, ee->len);
2485 
2486 	while (i < ee->len) {
2487 		en_dbg(DRV, priv,
2488 		       "mlx4_get_module_info i(%d) offset(%d) len(%d)\n",
2489 		       i, offset, ee->len - i);
2490 
2491 		ret = mlx4_get_module_info(mdev->dev, priv->port,
2492 					   offset, ee->len - i, data + i);
2493 
2494 		if (!ret) /* Done reading */
2495 			return 0;
2496 
2497 		if (ret < 0) {
2498 			en_err(priv,
2499 			       "mlx4_get_module_info i(%d) offset(%d) bytes_to_read(%d) - FAILED (0x%x)\n",
2500 			       i, offset, ee->len - i, ret);
2501 			return -1;
2502 		}
2503 
2504 		i += ret;
2505 		offset += ret;
2506 	}
2507 	return 0;
2508 }
2509 
2510 static void mlx4_en_print_eeprom(u8 *data, __u32 len)
2511 {
2512 	int		i;
2513 	int		j = 0;
2514 	int		row = 0;
2515 	const int	NUM_OF_BYTES = 16;
2516 
2517 	printf("\nOffset\t\tValues\n");
2518 	printf("------\t\t------\n");
2519 	while(row < len){
2520 		printf("0x%04x\t\t",row);
2521 		for(i=0; i < NUM_OF_BYTES; i++){
2522 			printf("%02x ", data[j]);
2523 			row++;
2524 			j++;
2525 		}
2526 		printf("\n");
2527 	}
2528 }
2529 
2530 /* Read cable EEPROM module information by first inspecting the first
2531  * two bytes to get the length and then read the rest of the information.
2532  * The information is printed to dmesg. */
2533 static int mlx4_en_read_eeprom(SYSCTL_HANDLER_ARGS)
2534 {
2535 
2536 	u8*		data;
2537 	int		error;
2538 	int		result = 0;
2539 	struct		mlx4_en_priv *priv;
2540 	struct		net_device *dev;
2541 	struct		ethtool_modinfo modinfo;
2542 	struct		ethtool_eeprom ee;
2543 
2544 	error = sysctl_handle_int(oidp, &result, 0, req);
2545 	if (error || !req->newptr)
2546 		return (error);
2547 
2548 	if (result == 1) {
2549 		priv = arg1;
2550 		dev = priv->dev;
2551 		data = kmalloc(PAGE_SIZE, GFP_KERNEL);
2552 
2553 		error = mlx4_en_get_module_info(dev, &modinfo);
2554 		if (error) {
2555 			en_err(priv,
2556 			       "mlx4_en_get_module_info returned with error - FAILED (0x%x)\n",
2557 			       -error);
2558 			goto out;
2559 		}
2560 
2561 		ee.len = modinfo.eeprom_len;
2562 		ee.offset = 0;
2563 
2564 		error = mlx4_en_get_module_eeprom(dev, &ee, data);
2565 		if (error) {
2566 			en_err(priv,
2567 			       "mlx4_en_get_module_eeprom returned with error - FAILED (0x%x)\n",
2568 			       -error);
2569 			/* Continue printing partial information in case of an error */
2570 		}
2571 
2572 		/* EEPROM information will be printed in dmesg */
2573 		mlx4_en_print_eeprom(data, ee.len);
2574 out:
2575 		kfree(data);
2576 	}
2577 	/* Return zero to prevent sysctl failure. */
2578 	return (0);
2579 }
2580 
2581 static int mlx4_en_set_tx_ppp(SYSCTL_HANDLER_ARGS)
2582 {
2583         struct mlx4_en_priv *priv;
2584         int ppp;
2585         int error;
2586 
2587         priv = arg1;
2588         ppp = priv->prof->tx_ppp;
2589         error = sysctl_handle_int(oidp, &ppp, 0, req);
2590         if (error || !req->newptr)
2591                 return (error);
2592         if (ppp > 0xff || ppp < 0)
2593                 return (-EINVAL);
2594         priv->prof->tx_ppp = ppp;
2595         error = -mlx4_SET_PORT_general(priv->mdev->dev, priv->port,
2596                                        priv->rx_mb_size + ETHER_CRC_LEN,
2597                                        priv->prof->tx_pause,
2598                                        priv->prof->tx_ppp,
2599                                        priv->prof->rx_pause,
2600                                        priv->prof->rx_ppp);
2601 
2602         return (error);
2603 }
2604 
2605 static int mlx4_en_set_rx_ppp(SYSCTL_HANDLER_ARGS)
2606 {
2607         struct mlx4_en_priv *priv;
2608         struct mlx4_en_dev *mdev;
2609         int ppp;
2610         int error;
2611         int port_up;
2612 
2613         port_up = 0;
2614         priv = arg1;
2615         mdev = priv->mdev;
2616         ppp = priv->prof->rx_ppp;
2617         error = sysctl_handle_int(oidp, &ppp, 0, req);
2618         if (error || !req->newptr)
2619                 return (error);
2620         if (ppp > 0xff || ppp < 0)
2621                 return (-EINVAL);
2622         /* See if we have to change the number of tx queues. */
2623         if (!ppp != !priv->prof->rx_ppp) {
2624                 mutex_lock(&mdev->state_lock);
2625                 if (priv->port_up) {
2626                         port_up = 1;
2627                         mlx4_en_stop_port(priv->dev);
2628                 }
2629                 mlx4_en_free_resources(priv);
2630                 priv->prof->rx_ppp = ppp;
2631                 error = -mlx4_en_alloc_resources(priv);
2632                 if (error)
2633                         en_err(priv, "Failed reallocating port resources\n");
2634                 if (error == 0 && port_up) {
2635                         error = -mlx4_en_start_port(priv->dev);
2636                         if (error)
2637                                 en_err(priv, "Failed starting port\n");
2638                 }
2639                 mutex_unlock(&mdev->state_lock);
2640                 return (error);
2641 
2642         }
2643         priv->prof->rx_ppp = ppp;
2644         error = -mlx4_SET_PORT_general(priv->mdev->dev, priv->port,
2645                                        priv->rx_mb_size + ETHER_CRC_LEN,
2646                                        priv->prof->tx_pause,
2647                                        priv->prof->tx_ppp,
2648                                        priv->prof->rx_pause,
2649                                        priv->prof->rx_ppp);
2650 
2651         return (error);
2652 }
2653 
2654 static void mlx4_en_sysctl_conf(struct mlx4_en_priv *priv)
2655 {
2656         struct net_device *dev;
2657         struct sysctl_ctx_list *ctx;
2658         struct sysctl_oid *node;
2659         struct sysctl_oid_list *node_list;
2660         struct sysctl_oid *coal;
2661         struct sysctl_oid_list *coal_list;
2662 	const char *pnameunit;
2663         dev = priv->dev;
2664         ctx = &priv->conf_ctx;
2665 	pnameunit = device_get_nameunit(priv->mdev->pdev->dev.bsddev);
2666 
2667         sysctl_ctx_init(ctx);
2668         priv->conf_sysctl = SYSCTL_ADD_NODE(ctx, SYSCTL_STATIC_CHILDREN(_hw),
2669             OID_AUTO, dev->if_xname, CTLFLAG_RD, 0, "mlx4 10gig ethernet");
2670         node = SYSCTL_ADD_NODE(ctx, SYSCTL_CHILDREN(priv->conf_sysctl), OID_AUTO,
2671             "conf", CTLFLAG_RD, NULL, "Configuration");
2672         node_list = SYSCTL_CHILDREN(node);
2673 
2674         SYSCTL_ADD_UINT(ctx, node_list, OID_AUTO, "msg_enable",
2675             CTLFLAG_RW, &priv->msg_enable, 0,
2676             "Driver message enable bitfield");
2677         SYSCTL_ADD_UINT(ctx, node_list, OID_AUTO, "rx_rings",
2678             CTLFLAG_RD, &priv->rx_ring_num, 0,
2679             "Number of receive rings");
2680         SYSCTL_ADD_UINT(ctx, node_list, OID_AUTO, "tx_rings",
2681             CTLFLAG_RD, &priv->tx_ring_num, 0,
2682             "Number of transmit rings");
2683         SYSCTL_ADD_PROC(ctx, node_list, OID_AUTO, "rx_size",
2684             CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, priv, 0,
2685             mlx4_en_set_rx_ring_size, "I", "Receive ring size");
2686         SYSCTL_ADD_PROC(ctx, node_list, OID_AUTO, "tx_size",
2687             CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, priv, 0,
2688             mlx4_en_set_tx_ring_size, "I", "Transmit ring size");
2689         SYSCTL_ADD_PROC(ctx, node_list, OID_AUTO, "tx_ppp",
2690             CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, priv, 0,
2691             mlx4_en_set_tx_ppp, "I", "TX Per-priority pause");
2692         SYSCTL_ADD_PROC(ctx, node_list, OID_AUTO, "rx_ppp",
2693             CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, priv, 0,
2694             mlx4_en_set_rx_ppp, "I", "RX Per-priority pause");
2695         SYSCTL_ADD_UINT(ctx, node_list, OID_AUTO, "port_num",
2696             CTLFLAG_RD, &priv->port, 0,
2697             "Port Number");
2698         SYSCTL_ADD_STRING(ctx, node_list, OID_AUTO, "device_name",
2699 	    CTLFLAG_RD, __DECONST(void *, pnameunit), 0,
2700 	    "PCI device name");
2701         /* Add coalescer configuration. */
2702         coal = SYSCTL_ADD_NODE(ctx, node_list, OID_AUTO,
2703             "coalesce", CTLFLAG_RD, NULL, "Interrupt coalesce configuration");
2704         coal_list = SYSCTL_CHILDREN(coal);
2705         SYSCTL_ADD_UINT(ctx, coal_list, OID_AUTO, "pkt_rate_low",
2706             CTLFLAG_RW, &priv->pkt_rate_low, 0,
2707             "Packets per-second for minimum delay");
2708         SYSCTL_ADD_UINT(ctx, coal_list, OID_AUTO, "rx_usecs_low",
2709             CTLFLAG_RW, &priv->rx_usecs_low, 0,
2710             "Minimum RX delay in micro-seconds");
2711         SYSCTL_ADD_UINT(ctx, coal_list, OID_AUTO, "pkt_rate_high",
2712             CTLFLAG_RW, &priv->pkt_rate_high, 0,
2713             "Packets per-second for maximum delay");
2714         SYSCTL_ADD_UINT(ctx, coal_list, OID_AUTO, "rx_usecs_high",
2715             CTLFLAG_RW, &priv->rx_usecs_high, 0,
2716             "Maximum RX delay in micro-seconds");
2717         SYSCTL_ADD_UINT(ctx, coal_list, OID_AUTO, "sample_interval",
2718             CTLFLAG_RW, &priv->sample_interval, 0,
2719             "adaptive frequency in units of HZ ticks");
2720         SYSCTL_ADD_UINT(ctx, coal_list, OID_AUTO, "adaptive_rx_coal",
2721             CTLFLAG_RW, &priv->adaptive_rx_coal, 0,
2722             "Enable adaptive rx coalescing");
2723 	/* EEPROM support */
2724 	SYSCTL_ADD_PROC(ctx, node_list, OID_AUTO, "eeprom_info",
2725 	    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, priv, 0,
2726 	    mlx4_en_read_eeprom, "I", "EEPROM information");
2727 }
2728 
2729 static void mlx4_en_sysctl_stat(struct mlx4_en_priv *priv)
2730 {
2731 	struct sysctl_ctx_list *ctx;
2732 	struct sysctl_oid_list *node_list;
2733 	struct sysctl_oid *ring_node;
2734 	struct sysctl_oid_list *ring_list;
2735 	struct mlx4_en_tx_ring *tx_ring;
2736 	struct mlx4_en_rx_ring *rx_ring;
2737 	char namebuf[128];
2738 	int i;
2739 
2740 	ctx = &priv->stat_ctx;
2741 	sysctl_ctx_init(ctx);
2742 	priv->stat_sysctl = SYSCTL_ADD_NODE(ctx, SYSCTL_CHILDREN(priv->conf_sysctl), OID_AUTO,
2743 	    "stat", CTLFLAG_RD, NULL, "Statistics");
2744 	node_list = SYSCTL_CHILDREN(priv->stat_sysctl);
2745 
2746 #ifdef MLX4_EN_PERF_STAT
2747 	SYSCTL_ADD_UINT(ctx, node_list, OID_AUTO, "tx_poll", CTLFLAG_RD,
2748 	    &priv->pstats.tx_poll, "TX Poll calls");
2749 	SYSCTL_ADD_QUAD(ctx, node_list, OID_AUTO, "tx_pktsz_avg", CTLFLAG_RD,
2750 	    &priv->pstats.tx_pktsz_avg, "TX average packet size");
2751 	SYSCTL_ADD_UINT(ctx, node_list, OID_AUTO, "inflight_avg", CTLFLAG_RD,
2752 	    &priv->pstats.inflight_avg, "TX average packets in-flight");
2753 	SYSCTL_ADD_UINT(ctx, node_list, OID_AUTO, "tx_coal_avg", CTLFLAG_RD,
2754 	    &priv->pstats.tx_coal_avg, "TX average coalesced completions");
2755 	SYSCTL_ADD_UINT(ctx, node_list, OID_AUTO, "rx_coal_avg", CTLFLAG_RD,
2756 	    &priv->pstats.rx_coal_avg, "RX average coalesced completions");
2757 #endif
2758 
2759 	SYSCTL_ADD_U64(ctx, node_list, OID_AUTO, "tso_packets", CTLFLAG_RD,
2760 	    &priv->port_stats.tso_packets, 0, "TSO packets sent");
2761 	SYSCTL_ADD_U64(ctx, node_list, OID_AUTO, "queue_stopped", CTLFLAG_RD,
2762 	    &priv->port_stats.queue_stopped, 0, "Queue full");
2763 	SYSCTL_ADD_U64(ctx, node_list, OID_AUTO, "wake_queue", CTLFLAG_RD,
2764 	    &priv->port_stats.wake_queue, 0, "Queue resumed after full");
2765 	SYSCTL_ADD_U64(ctx, node_list, OID_AUTO, "tx_timeout", CTLFLAG_RD,
2766 	    &priv->port_stats.tx_timeout, 0, "Transmit timeouts");
2767 	SYSCTL_ADD_U64(ctx, node_list, OID_AUTO, "tx_oversized_packets", CTLFLAG_RD,
2768 	    &priv->port_stats.oversized_packets, 0, "TX oversized packets, m_defrag failed");
2769 	SYSCTL_ADD_U64(ctx, node_list, OID_AUTO, "rx_alloc_failed", CTLFLAG_RD,
2770 	    &priv->port_stats.rx_alloc_failed, 0, "RX failed to allocate mbuf");
2771 	SYSCTL_ADD_U64(ctx, node_list, OID_AUTO, "rx_chksum_good", CTLFLAG_RD,
2772             &priv->port_stats.rx_chksum_good, 0, "RX checksum offload success");
2773 	SYSCTL_ADD_U64(ctx, node_list, OID_AUTO, "rx_chksum_none", CTLFLAG_RD,
2774 	    &priv->port_stats.rx_chksum_none, 0, "RX without checksum offload");
2775 	SYSCTL_ADD_U64(ctx, node_list, OID_AUTO, "tx_chksum_offload",
2776 	    CTLFLAG_RD, &priv->port_stats.tx_chksum_offload, 0,
2777 	    "TX checksum offloads");
2778 	SYSCTL_ADD_U64(ctx, node_list, OID_AUTO, "defrag_attempts",
2779 	    CTLFLAG_RD, &priv->port_stats.defrag_attempts, 0,
2780 	    "Oversized chains defragged");
2781 
2782 	/* Could strdup the names and add in a loop.  This is simpler. */
2783 	SYSCTL_ADD_U64(ctx, node_list, OID_AUTO, "rx_bytes", CTLFLAG_RD,
2784 	    &priv->pkstats.rx_bytes, 0, "RX Bytes");
2785 	SYSCTL_ADD_U64(ctx, node_list, OID_AUTO, "rx_packets", CTLFLAG_RD,
2786 	    &priv->pkstats.rx_packets, 0, "RX packets");
2787 	SYSCTL_ADD_U64(ctx, node_list, OID_AUTO, "rx_multicast_packets", CTLFLAG_RD,
2788 	    &priv->pkstats.rx_multicast_packets, 0, "RX Multicast Packets");
2789 	SYSCTL_ADD_U64(ctx, node_list, OID_AUTO, "rx_broadcast_packets", CTLFLAG_RD,
2790 	    &priv->pkstats.rx_broadcast_packets, 0, "RX Broadcast Packets");
2791 	SYSCTL_ADD_U64(ctx, node_list, OID_AUTO, "rx_errors", CTLFLAG_RD,
2792 	    &priv->pkstats.rx_errors, 0, "RX Errors");
2793 	SYSCTL_ADD_U64(ctx, node_list, OID_AUTO, "rx_dropped", CTLFLAG_RD,
2794 	    &priv->pkstats.rx_dropped, 0, "RX Dropped");
2795 	SYSCTL_ADD_U64(ctx, node_list, OID_AUTO, "rx_length_errors", CTLFLAG_RD,
2796 	    &priv->pkstats.rx_length_errors, 0, "RX Length Errors");
2797 	SYSCTL_ADD_U64(ctx, node_list, OID_AUTO, "rx_over_errors", CTLFLAG_RD,
2798 	    &priv->pkstats.rx_over_errors, 0, "RX Over Errors");
2799 	SYSCTL_ADD_U64(ctx, node_list, OID_AUTO, "rx_crc_errors", CTLFLAG_RD,
2800 	    &priv->pkstats.rx_crc_errors, 0, "RX CRC Errors");
2801 	SYSCTL_ADD_U64(ctx, node_list, OID_AUTO, "rx_jabbers", CTLFLAG_RD,
2802 	    &priv->pkstats.rx_jabbers, 0, "RX Jabbers");
2803 
2804 	SYSCTL_ADD_U64(ctx, node_list, OID_AUTO, "rx_in_range_length_error", CTLFLAG_RD,
2805 	    &priv->pkstats.rx_in_range_length_error, 0, "RX IN_Range Length Error");
2806 	SYSCTL_ADD_U64(ctx, node_list, OID_AUTO, "rx_out_range_length_error",
2807 	    CTLFLAG_RD, &priv->pkstats.rx_out_range_length_error, 0,
2808 	    "RX Out Range Length Error");
2809 	SYSCTL_ADD_U64(ctx, node_list, OID_AUTO, "rx_lt_64_bytes_packets", CTLFLAG_RD,
2810 	    &priv->pkstats.rx_lt_64_bytes_packets, 0, "RX Lt 64 Bytes Packets");
2811 	SYSCTL_ADD_U64(ctx, node_list, OID_AUTO, "rx_127_bytes_packets", CTLFLAG_RD,
2812 	    &priv->pkstats.rx_127_bytes_packets, 0, "RX 127 bytes Packets");
2813 	SYSCTL_ADD_U64(ctx, node_list, OID_AUTO, "rx_255_bytes_packets", CTLFLAG_RD,
2814 	    &priv->pkstats.rx_255_bytes_packets, 0, "RX 255 bytes Packets");
2815 	SYSCTL_ADD_U64(ctx, node_list, OID_AUTO, "rx_511_bytes_packets", CTLFLAG_RD,
2816 	    &priv->pkstats.rx_511_bytes_packets, 0, "RX 511 bytes Packets");
2817 	SYSCTL_ADD_U64(ctx, node_list, OID_AUTO, "rx_1023_bytes_packets", CTLFLAG_RD,
2818 	    &priv->pkstats.rx_1023_bytes_packets, 0, "RX 1023 bytes Packets");
2819 	SYSCTL_ADD_U64(ctx, node_list, OID_AUTO, "rx_1518_bytes_packets", CTLFLAG_RD,
2820 	    &priv->pkstats.rx_1518_bytes_packets, 0, "RX 1518 bytes Packets");
2821 	SYSCTL_ADD_U64(ctx, node_list, OID_AUTO, "rx_1522_bytes_packets", CTLFLAG_RD,
2822 	    &priv->pkstats.rx_1522_bytes_packets, 0, "RX 1522 bytes Packets");
2823 	SYSCTL_ADD_U64(ctx, node_list, OID_AUTO, "rx_1548_bytes_packets", CTLFLAG_RD,
2824 	    &priv->pkstats.rx_1548_bytes_packets, 0, "RX 1548 bytes Packets");
2825 	SYSCTL_ADD_U64(ctx, node_list, OID_AUTO, "rx_gt_1548_bytes_packets", CTLFLAG_RD,
2826 	    &priv->pkstats.rx_gt_1548_bytes_packets, 0,
2827 	    "RX Greater Then 1548 bytes Packets");
2828 
2829 	SYSCTL_ADD_U64(ctx, node_list, OID_AUTO, "tx_packets", CTLFLAG_RD,
2830 	    &priv->pkstats.tx_packets, 0, "TX packets");
2831 	SYSCTL_ADD_U64(ctx, node_list, OID_AUTO, "tx_bytes", CTLFLAG_RD,
2832 	    &priv->pkstats.tx_bytes, 0, "TX Bytes");
2833 	SYSCTL_ADD_U64(ctx, node_list, OID_AUTO, "tx_multicast_packets", CTLFLAG_RD,
2834 	    &priv->pkstats.tx_multicast_packets, 0, "TX Multicast Packets");
2835 	SYSCTL_ADD_U64(ctx, node_list, OID_AUTO, "tx_broadcast_packets", CTLFLAG_RD,
2836 	    &priv->pkstats.tx_broadcast_packets, 0, "TX Broadcast Packets");
2837 	SYSCTL_ADD_U64(ctx, node_list, OID_AUTO, "tx_errors", CTLFLAG_RD,
2838 	    &priv->pkstats.tx_errors, 0, "TX Errors");
2839 	SYSCTL_ADD_U64(ctx, node_list, OID_AUTO, "tx_dropped", CTLFLAG_RD,
2840 	    &priv->pkstats.tx_dropped, 0, "TX Dropped");
2841 	SYSCTL_ADD_U64(ctx, node_list, OID_AUTO, "tx_lt_64_bytes_packets", CTLFLAG_RD,
2842 	    &priv->pkstats.tx_lt_64_bytes_packets, 0, "TX Less Then 64 Bytes Packets");
2843 	SYSCTL_ADD_U64(ctx, node_list, OID_AUTO, "tx_127_bytes_packets", CTLFLAG_RD,
2844 	    &priv->pkstats.tx_127_bytes_packets, 0, "TX 127 Bytes Packets");
2845 	SYSCTL_ADD_U64(ctx, node_list, OID_AUTO, "tx_255_bytes_packets", CTLFLAG_RD,
2846 	    &priv->pkstats.tx_255_bytes_packets, 0, "TX 255 Bytes Packets");
2847 	SYSCTL_ADD_U64(ctx, node_list, OID_AUTO, "tx_511_bytes_packets", CTLFLAG_RD,
2848 	    &priv->pkstats.tx_511_bytes_packets, 0, "TX 511 Bytes Packets");
2849 	SYSCTL_ADD_U64(ctx, node_list, OID_AUTO, "tx_1023_bytes_packets", CTLFLAG_RD,
2850 	    &priv->pkstats.tx_1023_bytes_packets, 0, "TX 1023 Bytes Packets");
2851 	SYSCTL_ADD_U64(ctx, node_list, OID_AUTO, "tx_1518_bytes_packets", CTLFLAG_RD,
2852 	    &priv->pkstats.tx_1518_bytes_packets, 0, "TX 1518 Bytes Packets");
2853 	SYSCTL_ADD_U64(ctx, node_list, OID_AUTO, "tx_1522_bytes_packets", CTLFLAG_RD,
2854 	    &priv->pkstats.tx_1522_bytes_packets, 0, "TX 1522 Bytes Packets");
2855 	SYSCTL_ADD_U64(ctx, node_list, OID_AUTO, "tx_1548_bytes_packets", CTLFLAG_RD,
2856 	    &priv->pkstats.tx_1548_bytes_packets, 0, "TX 1548 Bytes Packets");
2857 	SYSCTL_ADD_U64(ctx, node_list, OID_AUTO, "tx_gt_1548_bytes_packets", CTLFLAG_RD,
2858 	    &priv->pkstats.tx_gt_1548_bytes_packets, 0,
2859 	    "TX Greater Then 1548 Bytes Packets");
2860 
2861 	for (i = 0; i < priv->tx_ring_num; i++) {
2862 		tx_ring = priv->tx_ring[i];
2863 		snprintf(namebuf, sizeof(namebuf), "tx_ring%d", i);
2864 		ring_node = SYSCTL_ADD_NODE(ctx, node_list, OID_AUTO, namebuf,
2865 		    CTLFLAG_RD, NULL, "TX Ring");
2866 		ring_list = SYSCTL_CHILDREN(ring_node);
2867 		SYSCTL_ADD_U64(ctx, ring_list, OID_AUTO, "packets",
2868 		    CTLFLAG_RD, &tx_ring->packets, 0, "TX packets");
2869 		SYSCTL_ADD_U64(ctx, ring_list, OID_AUTO, "bytes",
2870 		    CTLFLAG_RD, &tx_ring->bytes, 0, "TX bytes");
2871 		SYSCTL_ADD_U64(ctx, ring_list, OID_AUTO, "tso_packets",
2872 		    CTLFLAG_RD, &tx_ring->tso_packets, 0, "TSO packets");
2873 		SYSCTL_ADD_U64(ctx, ring_list, OID_AUTO, "defrag_attempts",
2874 		    CTLFLAG_RD, &tx_ring->defrag_attempts, 0,
2875 		    "Oversized chains defragged");
2876 	}
2877 
2878 	for (i = 0; i < priv->rx_ring_num; i++) {
2879 		rx_ring = priv->rx_ring[i];
2880 		snprintf(namebuf, sizeof(namebuf), "rx_ring%d", i);
2881 		ring_node = SYSCTL_ADD_NODE(ctx, node_list, OID_AUTO, namebuf,
2882 		    CTLFLAG_RD, NULL, "RX Ring");
2883 		ring_list = SYSCTL_CHILDREN(ring_node);
2884 		SYSCTL_ADD_U64(ctx, ring_list, OID_AUTO, "packets",
2885 		    CTLFLAG_RD, &rx_ring->packets, 0, "RX packets");
2886 		SYSCTL_ADD_U64(ctx, ring_list, OID_AUTO, "bytes",
2887 		    CTLFLAG_RD, &rx_ring->bytes, 0, "RX bytes");
2888 		SYSCTL_ADD_U64(ctx, ring_list, OID_AUTO, "error",
2889 		    CTLFLAG_RD, &rx_ring->errors, 0, "RX soft errors");
2890 	}
2891 }
2892