1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) Meta Platforms, Inc. and affiliates. */
3 
4 #include <linux/etherdevice.h>
5 #include <linux/ipv6.h>
6 #include <linux/types.h>
7 #include <net/netdev_queues.h>
8 
9 #include "fbnic.h"
10 #include "fbnic_netdev.h"
11 #include "fbnic_txrx.h"
12 
__fbnic_open(struct fbnic_net * fbn)13 int __fbnic_open(struct fbnic_net *fbn)
14 {
15 	struct fbnic_dev *fbd = fbn->fbd;
16 	int err;
17 
18 	err = fbnic_alloc_napi_vectors(fbn);
19 	if (err)
20 		return err;
21 
22 	err = fbnic_alloc_resources(fbn);
23 	if (err)
24 		goto free_napi_vectors;
25 
26 	err = fbnic_set_netif_queues(fbn);
27 	if (err)
28 		goto free_resources;
29 
30 	/* Send ownership message and flush to verify FW has seen it */
31 	err = fbnic_fw_xmit_ownership_msg(fbd, true);
32 	if (err) {
33 		dev_warn(fbd->dev,
34 			 "Error %d sending host ownership message to the firmware\n",
35 			 err);
36 		goto free_resources;
37 	}
38 
39 	err = fbnic_time_start(fbn);
40 	if (err)
41 		goto release_ownership;
42 
43 	err = fbnic_fw_init_heartbeat(fbd, false);
44 	if (err)
45 		goto time_stop;
46 
47 	err = fbnic_pcs_request_irq(fbd);
48 	if (err)
49 		goto time_stop;
50 
51 	/* Pull the BMC config and initialize the RPC */
52 	fbnic_bmc_rpc_init(fbd);
53 	fbnic_rss_reinit(fbd, fbn);
54 
55 	return 0;
56 time_stop:
57 	fbnic_time_stop(fbn);
58 release_ownership:
59 	fbnic_fw_xmit_ownership_msg(fbn->fbd, false);
60 free_resources:
61 	fbnic_free_resources(fbn);
62 free_napi_vectors:
63 	fbnic_free_napi_vectors(fbn);
64 	return err;
65 }
66 
fbnic_open(struct net_device * netdev)67 static int fbnic_open(struct net_device *netdev)
68 {
69 	struct fbnic_net *fbn = netdev_priv(netdev);
70 	int err;
71 
72 	fbnic_napi_name_irqs(fbn->fbd);
73 
74 	err = __fbnic_open(fbn);
75 	if (!err)
76 		fbnic_up(fbn);
77 
78 	return err;
79 }
80 
fbnic_stop(struct net_device * netdev)81 static int fbnic_stop(struct net_device *netdev)
82 {
83 	struct fbnic_net *fbn = netdev_priv(netdev);
84 
85 	fbnic_down(fbn);
86 	fbnic_pcs_free_irq(fbn->fbd);
87 
88 	fbnic_time_stop(fbn);
89 	fbnic_fw_xmit_ownership_msg(fbn->fbd, false);
90 
91 	fbnic_reset_netif_queues(fbn);
92 	fbnic_free_resources(fbn);
93 	fbnic_free_napi_vectors(fbn);
94 
95 	return 0;
96 }
97 
fbnic_uc_sync(struct net_device * netdev,const unsigned char * addr)98 static int fbnic_uc_sync(struct net_device *netdev, const unsigned char *addr)
99 {
100 	struct fbnic_net *fbn = netdev_priv(netdev);
101 	struct fbnic_mac_addr *avail_addr;
102 
103 	if (WARN_ON(!is_valid_ether_addr(addr)))
104 		return -EADDRNOTAVAIL;
105 
106 	avail_addr = __fbnic_uc_sync(fbn->fbd, addr);
107 	if (!avail_addr)
108 		return -ENOSPC;
109 
110 	/* Add type flag indicating this address is in use by the host */
111 	set_bit(FBNIC_MAC_ADDR_T_UNICAST, avail_addr->act_tcam);
112 
113 	return 0;
114 }
115 
fbnic_uc_unsync(struct net_device * netdev,const unsigned char * addr)116 static int fbnic_uc_unsync(struct net_device *netdev, const unsigned char *addr)
117 {
118 	struct fbnic_net *fbn = netdev_priv(netdev);
119 	struct fbnic_dev *fbd = fbn->fbd;
120 	int i, ret;
121 
122 	/* Scan from middle of list to bottom, filling bottom up.
123 	 * Skip the first entry which is reserved for dev_addr and
124 	 * leave the last entry to use for promiscuous filtering.
125 	 */
126 	for (i = fbd->mac_addr_boundary, ret = -ENOENT;
127 	     i < FBNIC_RPC_TCAM_MACDA_HOST_ADDR_IDX && ret; i++) {
128 		struct fbnic_mac_addr *mac_addr = &fbd->mac_addr[i];
129 
130 		if (!ether_addr_equal(mac_addr->value.addr8, addr))
131 			continue;
132 
133 		ret = __fbnic_uc_unsync(mac_addr);
134 	}
135 
136 	return ret;
137 }
138 
fbnic_mc_sync(struct net_device * netdev,const unsigned char * addr)139 static int fbnic_mc_sync(struct net_device *netdev, const unsigned char *addr)
140 {
141 	struct fbnic_net *fbn = netdev_priv(netdev);
142 	struct fbnic_mac_addr *avail_addr;
143 
144 	if (WARN_ON(!is_multicast_ether_addr(addr)))
145 		return -EADDRNOTAVAIL;
146 
147 	avail_addr = __fbnic_mc_sync(fbn->fbd, addr);
148 	if (!avail_addr)
149 		return -ENOSPC;
150 
151 	/* Add type flag indicating this address is in use by the host */
152 	set_bit(FBNIC_MAC_ADDR_T_MULTICAST, avail_addr->act_tcam);
153 
154 	return 0;
155 }
156 
fbnic_mc_unsync(struct net_device * netdev,const unsigned char * addr)157 static int fbnic_mc_unsync(struct net_device *netdev, const unsigned char *addr)
158 {
159 	struct fbnic_net *fbn = netdev_priv(netdev);
160 	struct fbnic_dev *fbd = fbn->fbd;
161 	int i, ret;
162 
163 	/* Scan from middle of list to top, filling top down.
164 	 * Skip over the address reserved for the BMC MAC and
165 	 * exclude index 0 as that belongs to the broadcast address
166 	 */
167 	for (i = fbd->mac_addr_boundary, ret = -ENOENT;
168 	     --i > FBNIC_RPC_TCAM_MACDA_BROADCAST_IDX && ret;) {
169 		struct fbnic_mac_addr *mac_addr = &fbd->mac_addr[i];
170 
171 		if (!ether_addr_equal(mac_addr->value.addr8, addr))
172 			continue;
173 
174 		ret = __fbnic_mc_unsync(mac_addr);
175 	}
176 
177 	return ret;
178 }
179 
__fbnic_set_rx_mode(struct net_device * netdev)180 void __fbnic_set_rx_mode(struct net_device *netdev)
181 {
182 	struct fbnic_net *fbn = netdev_priv(netdev);
183 	bool uc_promisc = false, mc_promisc = false;
184 	struct fbnic_dev *fbd = fbn->fbd;
185 	struct fbnic_mac_addr *mac_addr;
186 	int err;
187 
188 	/* Populate host address from dev_addr */
189 	mac_addr = &fbd->mac_addr[FBNIC_RPC_TCAM_MACDA_HOST_ADDR_IDX];
190 	if (!ether_addr_equal(mac_addr->value.addr8, netdev->dev_addr) ||
191 	    mac_addr->state != FBNIC_TCAM_S_VALID) {
192 		ether_addr_copy(mac_addr->value.addr8, netdev->dev_addr);
193 		mac_addr->state = FBNIC_TCAM_S_UPDATE;
194 		set_bit(FBNIC_MAC_ADDR_T_UNICAST, mac_addr->act_tcam);
195 	}
196 
197 	/* Populate broadcast address if broadcast is enabled */
198 	mac_addr = &fbd->mac_addr[FBNIC_RPC_TCAM_MACDA_BROADCAST_IDX];
199 	if (netdev->flags & IFF_BROADCAST) {
200 		if (!is_broadcast_ether_addr(mac_addr->value.addr8) ||
201 		    mac_addr->state != FBNIC_TCAM_S_VALID) {
202 			eth_broadcast_addr(mac_addr->value.addr8);
203 			mac_addr->state = FBNIC_TCAM_S_ADD;
204 		}
205 		set_bit(FBNIC_MAC_ADDR_T_BROADCAST, mac_addr->act_tcam);
206 	} else if (mac_addr->state == FBNIC_TCAM_S_VALID) {
207 		__fbnic_xc_unsync(mac_addr, FBNIC_MAC_ADDR_T_BROADCAST);
208 	}
209 
210 	/* Synchronize unicast and multicast address lists */
211 	err = __dev_uc_sync(netdev, fbnic_uc_sync, fbnic_uc_unsync);
212 	if (err == -ENOSPC)
213 		uc_promisc = true;
214 	err = __dev_mc_sync(netdev, fbnic_mc_sync, fbnic_mc_unsync);
215 	if (err == -ENOSPC)
216 		mc_promisc = true;
217 
218 	uc_promisc |= !!(netdev->flags & IFF_PROMISC);
219 	mc_promisc |= !!(netdev->flags & IFF_ALLMULTI) || uc_promisc;
220 
221 	/* Populate last TCAM entry with promiscuous entry and 0/1 bit mask */
222 	mac_addr = &fbd->mac_addr[FBNIC_RPC_TCAM_MACDA_PROMISC_IDX];
223 	if (uc_promisc) {
224 		if (!is_zero_ether_addr(mac_addr->value.addr8) ||
225 		    mac_addr->state != FBNIC_TCAM_S_VALID) {
226 			eth_zero_addr(mac_addr->value.addr8);
227 			eth_broadcast_addr(mac_addr->mask.addr8);
228 			clear_bit(FBNIC_MAC_ADDR_T_ALLMULTI,
229 				  mac_addr->act_tcam);
230 			set_bit(FBNIC_MAC_ADDR_T_PROMISC,
231 				mac_addr->act_tcam);
232 			mac_addr->state = FBNIC_TCAM_S_ADD;
233 		}
234 	} else if (mc_promisc &&
235 		   (!fbnic_bmc_present(fbd) || !fbd->fw_cap.all_multi)) {
236 		/* We have to add a special handler for multicast as the
237 		 * BMC may have an all-multi rule already in place. As such
238 		 * adding a rule ourselves won't do any good so we will have
239 		 * to modify the rules for the ALL MULTI below if the BMC
240 		 * already has the rule in place.
241 		 */
242 		if (!is_multicast_ether_addr(mac_addr->value.addr8) ||
243 		    mac_addr->state != FBNIC_TCAM_S_VALID) {
244 			eth_zero_addr(mac_addr->value.addr8);
245 			eth_broadcast_addr(mac_addr->mask.addr8);
246 			mac_addr->value.addr8[0] ^= 1;
247 			mac_addr->mask.addr8[0] ^= 1;
248 			set_bit(FBNIC_MAC_ADDR_T_ALLMULTI,
249 				mac_addr->act_tcam);
250 			clear_bit(FBNIC_MAC_ADDR_T_PROMISC,
251 				  mac_addr->act_tcam);
252 			mac_addr->state = FBNIC_TCAM_S_ADD;
253 		}
254 	} else if (mac_addr->state == FBNIC_TCAM_S_VALID) {
255 		if (test_bit(FBNIC_MAC_ADDR_T_BMC, mac_addr->act_tcam)) {
256 			clear_bit(FBNIC_MAC_ADDR_T_ALLMULTI,
257 				  mac_addr->act_tcam);
258 			clear_bit(FBNIC_MAC_ADDR_T_PROMISC,
259 				  mac_addr->act_tcam);
260 		} else {
261 			mac_addr->state = FBNIC_TCAM_S_DELETE;
262 		}
263 	}
264 
265 	/* Add rules for BMC all multicast if it is enabled */
266 	fbnic_bmc_rpc_all_multi_config(fbd, mc_promisc);
267 
268 	/* Sift out any unshared BMC rules and place them in BMC only section */
269 	fbnic_sift_macda(fbd);
270 
271 	/* Write updates to hardware */
272 	fbnic_write_rules(fbd);
273 	fbnic_write_macda(fbd);
274 	fbnic_write_tce_tcam(fbd);
275 }
276 
fbnic_set_rx_mode(struct net_device * netdev)277 static void fbnic_set_rx_mode(struct net_device *netdev)
278 {
279 	/* No need to update the hardware if we are not running */
280 	if (netif_running(netdev))
281 		__fbnic_set_rx_mode(netdev);
282 }
283 
fbnic_set_mac(struct net_device * netdev,void * p)284 static int fbnic_set_mac(struct net_device *netdev, void *p)
285 {
286 	struct sockaddr *addr = p;
287 
288 	if (!is_valid_ether_addr(addr->sa_data))
289 		return -EADDRNOTAVAIL;
290 
291 	eth_hw_addr_set(netdev, addr->sa_data);
292 
293 	fbnic_set_rx_mode(netdev);
294 
295 	return 0;
296 }
297 
fbnic_clear_rx_mode(struct net_device * netdev)298 void fbnic_clear_rx_mode(struct net_device *netdev)
299 {
300 	struct fbnic_net *fbn = netdev_priv(netdev);
301 	struct fbnic_dev *fbd = fbn->fbd;
302 	int idx;
303 
304 	for (idx = ARRAY_SIZE(fbd->mac_addr); idx--;) {
305 		struct fbnic_mac_addr *mac_addr = &fbd->mac_addr[idx];
306 
307 		if (mac_addr->state != FBNIC_TCAM_S_VALID)
308 			continue;
309 
310 		bitmap_clear(mac_addr->act_tcam,
311 			     FBNIC_MAC_ADDR_T_HOST_START,
312 			     FBNIC_MAC_ADDR_T_HOST_LEN);
313 
314 		if (bitmap_empty(mac_addr->act_tcam,
315 				 FBNIC_RPC_TCAM_ACT_NUM_ENTRIES))
316 			mac_addr->state = FBNIC_TCAM_S_DELETE;
317 	}
318 
319 	/* Write updates to hardware */
320 	fbnic_write_macda(fbd);
321 
322 	__dev_uc_unsync(netdev, NULL);
323 	__dev_mc_unsync(netdev, NULL);
324 }
325 
fbnic_hwtstamp_get(struct net_device * netdev,struct kernel_hwtstamp_config * config)326 static int fbnic_hwtstamp_get(struct net_device *netdev,
327 			      struct kernel_hwtstamp_config *config)
328 {
329 	struct fbnic_net *fbn = netdev_priv(netdev);
330 
331 	*config = fbn->hwtstamp_config;
332 
333 	return 0;
334 }
335 
fbnic_hwtstamp_set(struct net_device * netdev,struct kernel_hwtstamp_config * config,struct netlink_ext_ack * extack)336 static int fbnic_hwtstamp_set(struct net_device *netdev,
337 			      struct kernel_hwtstamp_config *config,
338 			      struct netlink_ext_ack *extack)
339 {
340 	struct fbnic_net *fbn = netdev_priv(netdev);
341 	int old_rx_filter;
342 
343 	if (config->source != HWTSTAMP_SOURCE_NETDEV)
344 		return -EOPNOTSUPP;
345 
346 	if (!kernel_hwtstamp_config_changed(config, &fbn->hwtstamp_config))
347 		return 0;
348 
349 	/* Upscale the filters */
350 	switch (config->rx_filter) {
351 	case HWTSTAMP_FILTER_NONE:
352 	case HWTSTAMP_FILTER_ALL:
353 	case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
354 	case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
355 	case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
356 	case HWTSTAMP_FILTER_PTP_V2_EVENT:
357 		break;
358 	case HWTSTAMP_FILTER_NTP_ALL:
359 		config->rx_filter = HWTSTAMP_FILTER_ALL;
360 		break;
361 	case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
362 	case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
363 		config->rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_EVENT;
364 		break;
365 	case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
366 	case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
367 		config->rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_EVENT;
368 		break;
369 	case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
370 	case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
371 		config->rx_filter = HWTSTAMP_FILTER_PTP_V2_L2_EVENT;
372 		break;
373 	case HWTSTAMP_FILTER_PTP_V2_SYNC:
374 	case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
375 		config->rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
376 		break;
377 	default:
378 		return -ERANGE;
379 	}
380 
381 	/* Configure */
382 	old_rx_filter = fbn->hwtstamp_config.rx_filter;
383 	memcpy(&fbn->hwtstamp_config, config, sizeof(*config));
384 
385 	if (old_rx_filter != config->rx_filter && netif_running(fbn->netdev)) {
386 		fbnic_rss_reinit(fbn->fbd, fbn);
387 		fbnic_write_rules(fbn->fbd);
388 	}
389 
390 	/* Save / report back filter configuration
391 	 * Note that our filter configuration is inexact. Instead of
392 	 * filtering for a specific UDP port or L2 Ethertype we are
393 	 * filtering in all UDP or all non-IP packets for timestamping. So
394 	 * if anything other than FILTER_ALL is requested we report
395 	 * FILTER_SOME indicating that we will be timestamping a few
396 	 * additional packets.
397 	 */
398 	if (config->rx_filter > HWTSTAMP_FILTER_ALL)
399 		config->rx_filter = HWTSTAMP_FILTER_SOME;
400 
401 	return 0;
402 }
403 
fbnic_get_stats64(struct net_device * dev,struct rtnl_link_stats64 * stats64)404 static void fbnic_get_stats64(struct net_device *dev,
405 			      struct rtnl_link_stats64 *stats64)
406 {
407 	u64 tx_bytes, tx_packets, tx_dropped = 0;
408 	u64 rx_bytes, rx_packets, rx_dropped = 0;
409 	struct fbnic_net *fbn = netdev_priv(dev);
410 	struct fbnic_queue_stats *stats;
411 	unsigned int start, i;
412 
413 	stats = &fbn->tx_stats;
414 
415 	tx_bytes = stats->bytes;
416 	tx_packets = stats->packets;
417 	tx_dropped = stats->dropped;
418 
419 	stats64->tx_bytes = tx_bytes;
420 	stats64->tx_packets = tx_packets;
421 	stats64->tx_dropped = tx_dropped;
422 
423 	for (i = 0; i < fbn->num_tx_queues; i++) {
424 		struct fbnic_ring *txr = fbn->tx[i];
425 
426 		if (!txr)
427 			continue;
428 
429 		stats = &txr->stats;
430 		do {
431 			start = u64_stats_fetch_begin(&stats->syncp);
432 			tx_bytes = stats->bytes;
433 			tx_packets = stats->packets;
434 			tx_dropped = stats->dropped;
435 		} while (u64_stats_fetch_retry(&stats->syncp, start));
436 
437 		stats64->tx_bytes += tx_bytes;
438 		stats64->tx_packets += tx_packets;
439 		stats64->tx_dropped += tx_dropped;
440 	}
441 
442 	stats = &fbn->rx_stats;
443 
444 	rx_bytes = stats->bytes;
445 	rx_packets = stats->packets;
446 	rx_dropped = stats->dropped;
447 
448 	stats64->rx_bytes = rx_bytes;
449 	stats64->rx_packets = rx_packets;
450 	stats64->rx_dropped = rx_dropped;
451 
452 	for (i = 0; i < fbn->num_rx_queues; i++) {
453 		struct fbnic_ring *rxr = fbn->rx[i];
454 
455 		if (!rxr)
456 			continue;
457 
458 		stats = &rxr->stats;
459 		do {
460 			start = u64_stats_fetch_begin(&stats->syncp);
461 			rx_bytes = stats->bytes;
462 			rx_packets = stats->packets;
463 			rx_dropped = stats->dropped;
464 		} while (u64_stats_fetch_retry(&stats->syncp, start));
465 
466 		stats64->rx_bytes += rx_bytes;
467 		stats64->rx_packets += rx_packets;
468 		stats64->rx_dropped += rx_dropped;
469 	}
470 }
471 
472 static const struct net_device_ops fbnic_netdev_ops = {
473 	.ndo_open		= fbnic_open,
474 	.ndo_stop		= fbnic_stop,
475 	.ndo_validate_addr	= eth_validate_addr,
476 	.ndo_start_xmit		= fbnic_xmit_frame,
477 	.ndo_features_check	= fbnic_features_check,
478 	.ndo_set_mac_address	= fbnic_set_mac,
479 	.ndo_set_rx_mode	= fbnic_set_rx_mode,
480 	.ndo_get_stats64	= fbnic_get_stats64,
481 	.ndo_hwtstamp_get	= fbnic_hwtstamp_get,
482 	.ndo_hwtstamp_set	= fbnic_hwtstamp_set,
483 };
484 
fbnic_get_queue_stats_rx(struct net_device * dev,int idx,struct netdev_queue_stats_rx * rx)485 static void fbnic_get_queue_stats_rx(struct net_device *dev, int idx,
486 				     struct netdev_queue_stats_rx *rx)
487 {
488 	struct fbnic_net *fbn = netdev_priv(dev);
489 	struct fbnic_ring *rxr = fbn->rx[idx];
490 	struct fbnic_queue_stats *stats;
491 	u64 bytes, packets, alloc_fail;
492 	u64 csum_complete, csum_none;
493 	unsigned int start;
494 
495 	if (!rxr)
496 		return;
497 
498 	stats = &rxr->stats;
499 	do {
500 		start = u64_stats_fetch_begin(&stats->syncp);
501 		bytes = stats->bytes;
502 		packets = stats->packets;
503 		alloc_fail = stats->rx.alloc_failed;
504 		csum_complete = stats->rx.csum_complete;
505 		csum_none = stats->rx.csum_none;
506 	} while (u64_stats_fetch_retry(&stats->syncp, start));
507 
508 	rx->bytes = bytes;
509 	rx->packets = packets;
510 	rx->alloc_fail = alloc_fail;
511 	rx->csum_complete = csum_complete;
512 	rx->csum_none = csum_none;
513 }
514 
fbnic_get_queue_stats_tx(struct net_device * dev,int idx,struct netdev_queue_stats_tx * tx)515 static void fbnic_get_queue_stats_tx(struct net_device *dev, int idx,
516 				     struct netdev_queue_stats_tx *tx)
517 {
518 	struct fbnic_net *fbn = netdev_priv(dev);
519 	struct fbnic_ring *txr = fbn->tx[idx];
520 	struct fbnic_queue_stats *stats;
521 	u64 stop, wake, csum, lso;
522 	unsigned int start;
523 	u64 bytes, packets;
524 
525 	if (!txr)
526 		return;
527 
528 	stats = &txr->stats;
529 	do {
530 		start = u64_stats_fetch_begin(&stats->syncp);
531 		bytes = stats->bytes;
532 		packets = stats->packets;
533 		csum = stats->twq.csum_partial;
534 		lso = stats->twq.lso;
535 		stop = stats->twq.stop;
536 		wake = stats->twq.wake;
537 	} while (u64_stats_fetch_retry(&stats->syncp, start));
538 
539 	tx->bytes = bytes;
540 	tx->packets = packets;
541 	tx->needs_csum = csum + lso;
542 	tx->hw_gso_wire_packets = lso;
543 	tx->stop = stop;
544 	tx->wake = wake;
545 }
546 
fbnic_get_base_stats(struct net_device * dev,struct netdev_queue_stats_rx * rx,struct netdev_queue_stats_tx * tx)547 static void fbnic_get_base_stats(struct net_device *dev,
548 				 struct netdev_queue_stats_rx *rx,
549 				 struct netdev_queue_stats_tx *tx)
550 {
551 	struct fbnic_net *fbn = netdev_priv(dev);
552 
553 	tx->bytes = fbn->tx_stats.bytes;
554 	tx->packets = fbn->tx_stats.packets;
555 	tx->needs_csum = fbn->tx_stats.twq.csum_partial + fbn->tx_stats.twq.lso;
556 	tx->hw_gso_wire_packets = fbn->tx_stats.twq.lso;
557 	tx->stop = fbn->tx_stats.twq.stop;
558 	tx->wake = fbn->tx_stats.twq.wake;
559 
560 	rx->bytes = fbn->rx_stats.bytes;
561 	rx->packets = fbn->rx_stats.packets;
562 	rx->alloc_fail = fbn->rx_stats.rx.alloc_failed;
563 	rx->csum_complete = fbn->rx_stats.rx.csum_complete;
564 	rx->csum_none = fbn->rx_stats.rx.csum_none;
565 }
566 
567 static const struct netdev_stat_ops fbnic_stat_ops = {
568 	.get_queue_stats_rx	= fbnic_get_queue_stats_rx,
569 	.get_queue_stats_tx	= fbnic_get_queue_stats_tx,
570 	.get_base_stats		= fbnic_get_base_stats,
571 };
572 
fbnic_reset_queues(struct fbnic_net * fbn,unsigned int tx,unsigned int rx)573 void fbnic_reset_queues(struct fbnic_net *fbn,
574 			unsigned int tx, unsigned int rx)
575 {
576 	struct fbnic_dev *fbd = fbn->fbd;
577 	unsigned int max_napis;
578 
579 	max_napis = fbd->num_irqs - FBNIC_NON_NAPI_VECTORS;
580 
581 	tx = min(tx, max_napis);
582 	fbn->num_tx_queues = tx;
583 
584 	rx = min(rx, max_napis);
585 	fbn->num_rx_queues = rx;
586 
587 	fbn->num_napi = max(tx, rx);
588 }
589 
590 /**
591  * fbnic_netdev_free - Free the netdev associate with fbnic
592  * @fbd: Driver specific structure to free netdev from
593  *
594  * Allocate and initialize the netdev and netdev private structure. Bind
595  * together the hardware, netdev, and pci data structures.
596  **/
fbnic_netdev_free(struct fbnic_dev * fbd)597 void fbnic_netdev_free(struct fbnic_dev *fbd)
598 {
599 	struct fbnic_net *fbn = netdev_priv(fbd->netdev);
600 
601 	if (fbn->phylink)
602 		phylink_destroy(fbn->phylink);
603 
604 	free_netdev(fbd->netdev);
605 	fbd->netdev = NULL;
606 }
607 
608 /**
609  * fbnic_netdev_alloc - Allocate a netdev and associate with fbnic
610  * @fbd: Driver specific structure to associate netdev with
611  *
612  * Allocate and initialize the netdev and netdev private structure. Bind
613  * together the hardware, netdev, and pci data structures.
614  *
615  *  Return: Pointer to net_device on success, NULL on failure
616  **/
fbnic_netdev_alloc(struct fbnic_dev * fbd)617 struct net_device *fbnic_netdev_alloc(struct fbnic_dev *fbd)
618 {
619 	struct net_device *netdev;
620 	struct fbnic_net *fbn;
621 	int default_queues;
622 
623 	netdev = alloc_etherdev_mq(sizeof(*fbn), FBNIC_MAX_RXQS);
624 	if (!netdev)
625 		return NULL;
626 
627 	SET_NETDEV_DEV(netdev, fbd->dev);
628 	fbd->netdev = netdev;
629 
630 	netdev->netdev_ops = &fbnic_netdev_ops;
631 	netdev->stat_ops = &fbnic_stat_ops;
632 
633 	fbnic_set_ethtool_ops(netdev);
634 
635 	fbn = netdev_priv(netdev);
636 
637 	fbn->netdev = netdev;
638 	fbn->fbd = fbd;
639 
640 	fbn->txq_size = FBNIC_TXQ_SIZE_DEFAULT;
641 	fbn->hpq_size = FBNIC_HPQ_SIZE_DEFAULT;
642 	fbn->ppq_size = FBNIC_PPQ_SIZE_DEFAULT;
643 	fbn->rcq_size = FBNIC_RCQ_SIZE_DEFAULT;
644 
645 	fbn->tx_usecs = FBNIC_TX_USECS_DEFAULT;
646 	fbn->rx_usecs = FBNIC_RX_USECS_DEFAULT;
647 	fbn->rx_max_frames = FBNIC_RX_FRAMES_DEFAULT;
648 
649 	default_queues = netif_get_num_default_rss_queues();
650 	if (default_queues > fbd->max_num_queues)
651 		default_queues = fbd->max_num_queues;
652 
653 	fbnic_reset_queues(fbn, default_queues, default_queues);
654 
655 	fbnic_reset_indir_tbl(fbn);
656 	fbnic_rss_key_fill(fbn->rss_key);
657 	fbnic_rss_init_en_mask(fbn);
658 
659 	netdev->priv_flags |= IFF_UNICAST_FLT;
660 
661 	netdev->gso_partial_features =
662 		NETIF_F_GSO_GRE |
663 		NETIF_F_GSO_GRE_CSUM |
664 		NETIF_F_GSO_IPXIP4 |
665 		NETIF_F_GSO_UDP_TUNNEL |
666 		NETIF_F_GSO_UDP_TUNNEL_CSUM;
667 
668 	netdev->features |=
669 		netdev->gso_partial_features |
670 		FBNIC_TUN_GSO_FEATURES |
671 		NETIF_F_RXHASH |
672 		NETIF_F_SG |
673 		NETIF_F_HW_CSUM |
674 		NETIF_F_RXCSUM |
675 		NETIF_F_TSO |
676 		NETIF_F_TSO_ECN |
677 		NETIF_F_TSO6 |
678 		NETIF_F_GSO_PARTIAL |
679 		NETIF_F_GSO_UDP_L4;
680 
681 	netdev->hw_features |= netdev->features;
682 	netdev->vlan_features |= netdev->features;
683 	netdev->hw_enc_features |= netdev->features;
684 	netdev->features |= NETIF_F_NTUPLE;
685 
686 	netdev->min_mtu = IPV6_MIN_MTU;
687 	netdev->max_mtu = FBNIC_MAX_JUMBO_FRAME_SIZE - ETH_HLEN;
688 
689 	/* TBD: This is workaround for BMC as phylink doesn't have support
690 	 * for leavling the link enabled if a BMC is present.
691 	 */
692 	netdev->ethtool->wol_enabled = true;
693 
694 	fbn->fec = FBNIC_FEC_AUTO | FBNIC_FEC_RS;
695 	fbn->link_mode = FBNIC_LINK_AUTO | FBNIC_LINK_50R2;
696 	netif_carrier_off(netdev);
697 
698 	netif_tx_stop_all_queues(netdev);
699 
700 	if (fbnic_phylink_init(netdev)) {
701 		fbnic_netdev_free(fbd);
702 		return NULL;
703 	}
704 
705 	return netdev;
706 }
707 
fbnic_dsn_to_mac_addr(u64 dsn,char * addr)708 static int fbnic_dsn_to_mac_addr(u64 dsn, char *addr)
709 {
710 	addr[0] = (dsn >> 56) & 0xFF;
711 	addr[1] = (dsn >> 48) & 0xFF;
712 	addr[2] = (dsn >> 40) & 0xFF;
713 	addr[3] = (dsn >> 16) & 0xFF;
714 	addr[4] = (dsn >> 8) & 0xFF;
715 	addr[5] = dsn & 0xFF;
716 
717 	return is_valid_ether_addr(addr) ? 0 : -EINVAL;
718 }
719 
720 /**
721  * fbnic_netdev_register - Initialize general software structures
722  * @netdev: Netdev containing structure to initialize and register
723  *
724  * Initialize the MAC address for the netdev and register it.
725  *
726  *  Return: 0 on success, negative on failure
727  **/
fbnic_netdev_register(struct net_device * netdev)728 int fbnic_netdev_register(struct net_device *netdev)
729 {
730 	struct fbnic_net *fbn = netdev_priv(netdev);
731 	struct fbnic_dev *fbd = fbn->fbd;
732 	u64 dsn = fbd->dsn;
733 	u8 addr[ETH_ALEN];
734 	int err;
735 
736 	err = fbnic_dsn_to_mac_addr(dsn, addr);
737 	if (!err) {
738 		ether_addr_copy(netdev->perm_addr, addr);
739 		eth_hw_addr_set(netdev, addr);
740 	} else {
741 		/* A randomly assigned MAC address will cause provisioning
742 		 * issues so instead just fail to spawn the netdev and
743 		 * avoid any confusion.
744 		 */
745 		dev_err(fbd->dev, "MAC addr %pM invalid\n", addr);
746 		return err;
747 	}
748 
749 	return register_netdev(netdev);
750 }
751 
fbnic_netdev_unregister(struct net_device * netdev)752 void fbnic_netdev_unregister(struct net_device *netdev)
753 {
754 	unregister_netdev(netdev);
755 }
756