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