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