xref: /linux/drivers/net/ethernet/meta/fbnic/fbnic_netdev.c (revision 18a7e218cfcdca6666e1f7356533e4c988780b57)
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 err_reset_queues;
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 	phylink_resume(fbn->phylink);
56 
57 	return 0;
58 time_stop:
59 	fbnic_time_stop(fbn);
60 release_ownership:
61 	fbnic_fw_xmit_ownership_msg(fbn->fbd, false);
62 err_reset_queues:
63 	fbnic_reset_netif_queues(fbn);
64 free_resources:
65 	fbnic_free_resources(fbn);
66 free_napi_vectors:
67 	fbnic_free_napi_vectors(fbn);
68 	return err;
69 }
70 
fbnic_open(struct net_device * netdev)71 static int fbnic_open(struct net_device *netdev)
72 {
73 	struct fbnic_net *fbn = netdev_priv(netdev);
74 	int err;
75 
76 	fbnic_napi_name_irqs(fbn->fbd);
77 
78 	err = __fbnic_open(fbn);
79 	if (!err)
80 		fbnic_up(fbn);
81 
82 	return err;
83 }
84 
fbnic_stop(struct net_device * netdev)85 static int fbnic_stop(struct net_device *netdev)
86 {
87 	struct fbnic_net *fbn = netdev_priv(netdev);
88 
89 	phylink_suspend(fbn->phylink, fbnic_bmc_present(fbn->fbd));
90 
91 	fbnic_down(fbn);
92 	fbnic_pcs_free_irq(fbn->fbd);
93 
94 	fbnic_time_stop(fbn);
95 	fbnic_fw_xmit_ownership_msg(fbn->fbd, false);
96 
97 	fbnic_reset_netif_queues(fbn);
98 	fbnic_free_resources(fbn);
99 	fbnic_free_napi_vectors(fbn);
100 
101 	return 0;
102 }
103 
fbnic_uc_sync(struct net_device * netdev,const unsigned char * addr)104 static int fbnic_uc_sync(struct net_device *netdev, const unsigned char *addr)
105 {
106 	struct fbnic_net *fbn = netdev_priv(netdev);
107 	struct fbnic_mac_addr *avail_addr;
108 
109 	if (WARN_ON(!is_valid_ether_addr(addr)))
110 		return -EADDRNOTAVAIL;
111 
112 	avail_addr = __fbnic_uc_sync(fbn->fbd, addr);
113 	if (!avail_addr)
114 		return -ENOSPC;
115 
116 	/* Add type flag indicating this address is in use by the host */
117 	set_bit(FBNIC_MAC_ADDR_T_UNICAST, avail_addr->act_tcam);
118 
119 	return 0;
120 }
121 
fbnic_uc_unsync(struct net_device * netdev,const unsigned char * addr)122 static int fbnic_uc_unsync(struct net_device *netdev, const unsigned char *addr)
123 {
124 	struct fbnic_net *fbn = netdev_priv(netdev);
125 	struct fbnic_dev *fbd = fbn->fbd;
126 	int i, ret;
127 
128 	/* Scan from middle of list to bottom, filling bottom up.
129 	 * Skip the first entry which is reserved for dev_addr and
130 	 * leave the last entry to use for promiscuous filtering.
131 	 */
132 	for (i = fbd->mac_addr_boundary, ret = -ENOENT;
133 	     i < FBNIC_RPC_TCAM_MACDA_HOST_ADDR_IDX && ret; i++) {
134 		struct fbnic_mac_addr *mac_addr = &fbd->mac_addr[i];
135 
136 		if (!ether_addr_equal(mac_addr->value.addr8, addr))
137 			continue;
138 
139 		ret = __fbnic_uc_unsync(mac_addr);
140 	}
141 
142 	return ret;
143 }
144 
fbnic_mc_sync(struct net_device * netdev,const unsigned char * addr)145 static int fbnic_mc_sync(struct net_device *netdev, const unsigned char *addr)
146 {
147 	struct fbnic_net *fbn = netdev_priv(netdev);
148 	struct fbnic_mac_addr *avail_addr;
149 
150 	if (WARN_ON(!is_multicast_ether_addr(addr)))
151 		return -EADDRNOTAVAIL;
152 
153 	avail_addr = __fbnic_mc_sync(fbn->fbd, addr);
154 	if (!avail_addr)
155 		return -ENOSPC;
156 
157 	/* Add type flag indicating this address is in use by the host */
158 	set_bit(FBNIC_MAC_ADDR_T_MULTICAST, avail_addr->act_tcam);
159 
160 	return 0;
161 }
162 
fbnic_mc_unsync(struct net_device * netdev,const unsigned char * addr)163 static int fbnic_mc_unsync(struct net_device *netdev, const unsigned char *addr)
164 {
165 	struct fbnic_net *fbn = netdev_priv(netdev);
166 	struct fbnic_dev *fbd = fbn->fbd;
167 	int i, ret;
168 
169 	/* Scan from middle of list to top, filling top down.
170 	 * Skip over the address reserved for the BMC MAC and
171 	 * exclude index 0 as that belongs to the broadcast address
172 	 */
173 	for (i = fbd->mac_addr_boundary, ret = -ENOENT;
174 	     --i > FBNIC_RPC_TCAM_MACDA_BROADCAST_IDX && ret;) {
175 		struct fbnic_mac_addr *mac_addr = &fbd->mac_addr[i];
176 
177 		if (!ether_addr_equal(mac_addr->value.addr8, addr))
178 			continue;
179 
180 		ret = __fbnic_mc_unsync(mac_addr);
181 	}
182 
183 	return ret;
184 }
185 
__fbnic_set_rx_mode(struct fbnic_dev * fbd)186 void __fbnic_set_rx_mode(struct fbnic_dev *fbd)
187 {
188 	bool uc_promisc = false, mc_promisc = false;
189 	struct net_device *netdev = fbd->netdev;
190 	struct fbnic_mac_addr *mac_addr;
191 	int err;
192 
193 	/* Populate host address from dev_addr */
194 	mac_addr = &fbd->mac_addr[FBNIC_RPC_TCAM_MACDA_HOST_ADDR_IDX];
195 	if (!ether_addr_equal(mac_addr->value.addr8, netdev->dev_addr) ||
196 	    mac_addr->state != FBNIC_TCAM_S_VALID) {
197 		ether_addr_copy(mac_addr->value.addr8, netdev->dev_addr);
198 		mac_addr->state = FBNIC_TCAM_S_UPDATE;
199 		set_bit(FBNIC_MAC_ADDR_T_UNICAST, mac_addr->act_tcam);
200 	}
201 
202 	/* Populate broadcast address if broadcast is enabled */
203 	mac_addr = &fbd->mac_addr[FBNIC_RPC_TCAM_MACDA_BROADCAST_IDX];
204 	if (netdev->flags & IFF_BROADCAST) {
205 		if (!is_broadcast_ether_addr(mac_addr->value.addr8) ||
206 		    mac_addr->state != FBNIC_TCAM_S_VALID) {
207 			eth_broadcast_addr(mac_addr->value.addr8);
208 			mac_addr->state = FBNIC_TCAM_S_ADD;
209 		}
210 		set_bit(FBNIC_MAC_ADDR_T_BROADCAST, mac_addr->act_tcam);
211 	} else if (mac_addr->state == FBNIC_TCAM_S_VALID) {
212 		__fbnic_xc_unsync(mac_addr, FBNIC_MAC_ADDR_T_BROADCAST);
213 	}
214 
215 	/* Synchronize unicast and multicast address lists */
216 	err = __dev_uc_sync(netdev, fbnic_uc_sync, fbnic_uc_unsync);
217 	if (err == -ENOSPC)
218 		uc_promisc = true;
219 	err = __dev_mc_sync(netdev, fbnic_mc_sync, fbnic_mc_unsync);
220 	if (err == -ENOSPC)
221 		mc_promisc = true;
222 
223 	uc_promisc |= !!(netdev->flags & IFF_PROMISC);
224 	mc_promisc |= !!(netdev->flags & IFF_ALLMULTI) || uc_promisc;
225 
226 	/* Update the promiscuous rules */
227 	fbnic_promisc_sync(fbd, uc_promisc, mc_promisc);
228 
229 	/* Add rules for BMC all multicast if it is enabled */
230 	fbnic_bmc_rpc_all_multi_config(fbd, mc_promisc);
231 
232 	/* Sift out any unshared BMC rules and place them in BMC only section */
233 	fbnic_sift_macda(fbd);
234 
235 	/* Write updates to hardware */
236 	fbnic_write_rules(fbd);
237 	fbnic_write_macda(fbd);
238 	fbnic_write_tce_tcam(fbd);
239 }
240 
fbnic_set_rx_mode(struct net_device * netdev)241 static void fbnic_set_rx_mode(struct net_device *netdev)
242 {
243 	struct fbnic_net *fbn = netdev_priv(netdev);
244 	struct fbnic_dev *fbd = fbn->fbd;
245 
246 	/* No need to update the hardware if we are not running */
247 	if (netif_running(netdev))
248 		__fbnic_set_rx_mode(fbd);
249 }
250 
fbnic_set_mac(struct net_device * netdev,void * p)251 static int fbnic_set_mac(struct net_device *netdev, void *p)
252 {
253 	struct sockaddr *addr = p;
254 
255 	if (!is_valid_ether_addr(addr->sa_data))
256 		return -EADDRNOTAVAIL;
257 
258 	eth_hw_addr_set(netdev, addr->sa_data);
259 
260 	fbnic_set_rx_mode(netdev);
261 
262 	return 0;
263 }
264 
fbnic_clear_rx_mode(struct fbnic_dev * fbd)265 void fbnic_clear_rx_mode(struct fbnic_dev *fbd)
266 {
267 	struct net_device *netdev = fbd->netdev;
268 	int idx;
269 
270 	for (idx = ARRAY_SIZE(fbd->mac_addr); idx--;) {
271 		struct fbnic_mac_addr *mac_addr = &fbd->mac_addr[idx];
272 
273 		if (mac_addr->state != FBNIC_TCAM_S_VALID)
274 			continue;
275 
276 		bitmap_clear(mac_addr->act_tcam,
277 			     FBNIC_MAC_ADDR_T_HOST_START,
278 			     FBNIC_MAC_ADDR_T_HOST_LEN);
279 
280 		if (bitmap_empty(mac_addr->act_tcam,
281 				 FBNIC_RPC_TCAM_ACT_NUM_ENTRIES))
282 			mac_addr->state = FBNIC_TCAM_S_DELETE;
283 	}
284 
285 	/* Write updates to hardware */
286 	fbnic_write_macda(fbd);
287 
288 	__dev_uc_unsync(netdev, NULL);
289 	__dev_mc_unsync(netdev, NULL);
290 }
291 
fbnic_hwtstamp_get(struct net_device * netdev,struct kernel_hwtstamp_config * config)292 static int fbnic_hwtstamp_get(struct net_device *netdev,
293 			      struct kernel_hwtstamp_config *config)
294 {
295 	struct fbnic_net *fbn = netdev_priv(netdev);
296 
297 	*config = fbn->hwtstamp_config;
298 
299 	return 0;
300 }
301 
fbnic_hwtstamp_set(struct net_device * netdev,struct kernel_hwtstamp_config * config,struct netlink_ext_ack * extack)302 static int fbnic_hwtstamp_set(struct net_device *netdev,
303 			      struct kernel_hwtstamp_config *config,
304 			      struct netlink_ext_ack *extack)
305 {
306 	struct fbnic_net *fbn = netdev_priv(netdev);
307 	int old_rx_filter;
308 
309 	if (config->source != HWTSTAMP_SOURCE_NETDEV)
310 		return -EOPNOTSUPP;
311 
312 	if (!kernel_hwtstamp_config_changed(config, &fbn->hwtstamp_config))
313 		return 0;
314 
315 	/* Upscale the filters */
316 	switch (config->rx_filter) {
317 	case HWTSTAMP_FILTER_NONE:
318 	case HWTSTAMP_FILTER_ALL:
319 	case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
320 	case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
321 	case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
322 	case HWTSTAMP_FILTER_PTP_V2_EVENT:
323 		break;
324 	case HWTSTAMP_FILTER_NTP_ALL:
325 		config->rx_filter = HWTSTAMP_FILTER_ALL;
326 		break;
327 	case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
328 	case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
329 		config->rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_EVENT;
330 		break;
331 	case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
332 	case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
333 		config->rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_EVENT;
334 		break;
335 	case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
336 	case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
337 		config->rx_filter = HWTSTAMP_FILTER_PTP_V2_L2_EVENT;
338 		break;
339 	case HWTSTAMP_FILTER_PTP_V2_SYNC:
340 	case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
341 		config->rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
342 		break;
343 	default:
344 		return -ERANGE;
345 	}
346 
347 	/* Configure */
348 	old_rx_filter = fbn->hwtstamp_config.rx_filter;
349 	memcpy(&fbn->hwtstamp_config, config, sizeof(*config));
350 
351 	if (old_rx_filter != config->rx_filter && netif_running(fbn->netdev)) {
352 		fbnic_rss_reinit(fbn->fbd, fbn);
353 		fbnic_write_rules(fbn->fbd);
354 	}
355 
356 	/* Save / report back filter configuration
357 	 * Note that our filter configuration is inexact. Instead of
358 	 * filtering for a specific UDP port or L2 Ethertype we are
359 	 * filtering in all UDP or all non-IP packets for timestamping. So
360 	 * if anything other than FILTER_ALL is requested we report
361 	 * FILTER_SOME indicating that we will be timestamping a few
362 	 * additional packets.
363 	 */
364 	if (config->rx_filter > HWTSTAMP_FILTER_ALL)
365 		config->rx_filter = HWTSTAMP_FILTER_SOME;
366 
367 	return 0;
368 }
369 
fbnic_get_stats64(struct net_device * dev,struct rtnl_link_stats64 * stats64)370 static void fbnic_get_stats64(struct net_device *dev,
371 			      struct rtnl_link_stats64 *stats64)
372 {
373 	u64 rx_bytes, rx_packets, rx_dropped = 0, rx_errors = 0;
374 	u64 rx_over = 0, rx_missed = 0, rx_length = 0;
375 	u64 tx_bytes, tx_packets, tx_dropped = 0;
376 	struct fbnic_net *fbn = netdev_priv(dev);
377 	struct fbnic_dev *fbd = fbn->fbd;
378 	struct fbnic_queue_stats *stats;
379 
380 	unsigned int start, i;
381 
382 	fbnic_get_hw_stats(fbd);
383 
384 	stats = &fbn->tx_stats;
385 
386 	tx_bytes = stats->bytes;
387 	tx_packets = stats->packets;
388 	tx_dropped = stats->dropped;
389 
390 	/* Record drops from Tx HW Datapath */
391 	spin_lock(&fbd->hw_stats.lock);
392 	tx_dropped += fbd->hw_stats.tmi.drop.frames.value +
393 		      fbd->hw_stats.tti.cm_drop.frames.value +
394 		      fbd->hw_stats.tti.frame_drop.frames.value +
395 		      fbd->hw_stats.tti.tbi_drop.frames.value;
396 	spin_unlock(&fbd->hw_stats.lock);
397 
398 	stats64->tx_bytes = tx_bytes;
399 	stats64->tx_packets = tx_packets;
400 	stats64->tx_dropped = tx_dropped;
401 
402 	for (i = 0; i < fbn->num_tx_queues; i++) {
403 		struct fbnic_ring *txr = fbn->tx[i];
404 
405 		if (!txr)
406 			continue;
407 
408 		stats = &txr->stats;
409 		do {
410 			start = u64_stats_fetch_begin(&stats->syncp);
411 			tx_bytes = stats->bytes;
412 			tx_packets = stats->packets;
413 			tx_dropped = stats->dropped;
414 		} while (u64_stats_fetch_retry(&stats->syncp, start));
415 
416 		stats64->tx_bytes += tx_bytes;
417 		stats64->tx_packets += tx_packets;
418 		stats64->tx_dropped += tx_dropped;
419 	}
420 
421 	stats = &fbn->rx_stats;
422 
423 	rx_bytes = stats->bytes;
424 	rx_packets = stats->packets;
425 	rx_dropped = stats->dropped;
426 
427 	spin_lock(&fbd->hw_stats.lock);
428 	/* Record drops for the host FIFOs.
429 	 * 4: network to Host,	6: BMC to Host
430 	 * Exclude the BMC and MC FIFOs as those stats may contain drops
431 	 * due to unrelated items such as TCAM misses. They are still
432 	 * accessible through the ethtool stats.
433 	 */
434 	i = FBNIC_RXB_FIFO_HOST;
435 	rx_missed += fbd->hw_stats.rxb.fifo[i].drop.frames.value;
436 	i = FBNIC_RXB_FIFO_BMC_TO_HOST;
437 	rx_missed += fbd->hw_stats.rxb.fifo[i].drop.frames.value;
438 
439 	for (i = 0; i < fbd->max_num_queues; i++) {
440 		/* Report packets dropped due to CQ/BDQ being full/empty */
441 		rx_over += fbd->hw_stats.hw_q[i].rde_pkt_cq_drop.value;
442 		rx_over += fbd->hw_stats.hw_q[i].rde_pkt_bdq_drop.value;
443 
444 		/* Report packets with errors */
445 		rx_errors += fbd->hw_stats.hw_q[i].rde_pkt_err.value;
446 	}
447 	spin_unlock(&fbd->hw_stats.lock);
448 
449 	stats64->rx_bytes = rx_bytes;
450 	stats64->rx_packets = rx_packets;
451 	stats64->rx_dropped = rx_dropped;
452 	stats64->rx_over_errors = rx_over;
453 	stats64->rx_errors = rx_errors;
454 	stats64->rx_missed_errors = rx_missed;
455 
456 	for (i = 0; i < fbn->num_rx_queues; i++) {
457 		struct fbnic_ring *xdpr = fbn->tx[FBNIC_MAX_TXQS + i];
458 		struct fbnic_ring *rxr = fbn->rx[i];
459 
460 		if (!rxr)
461 			continue;
462 
463 		stats = &rxr->stats;
464 		do {
465 			start = u64_stats_fetch_begin(&stats->syncp);
466 			rx_bytes = stats->bytes;
467 			rx_packets = stats->packets;
468 			rx_dropped = stats->dropped;
469 			rx_length = stats->rx.length_errors;
470 		} while (u64_stats_fetch_retry(&stats->syncp, start));
471 
472 		stats64->rx_bytes += rx_bytes;
473 		stats64->rx_packets += rx_packets;
474 		stats64->rx_dropped += rx_dropped;
475 		stats64->rx_errors += rx_length;
476 		stats64->rx_length_errors += rx_length;
477 
478 		if (!xdpr)
479 			continue;
480 
481 		stats = &xdpr->stats;
482 		do {
483 			start = u64_stats_fetch_begin(&stats->syncp);
484 			tx_bytes = stats->bytes;
485 			tx_packets = stats->packets;
486 			tx_dropped = stats->dropped;
487 		} while (u64_stats_fetch_retry(&stats->syncp, start));
488 
489 		stats64->tx_bytes += tx_bytes;
490 		stats64->tx_packets += tx_packets;
491 		stats64->tx_dropped += tx_dropped;
492 	}
493 }
494 
fbnic_check_split_frames(struct bpf_prog * prog,unsigned int mtu,u32 hds_thresh)495 bool fbnic_check_split_frames(struct bpf_prog *prog, unsigned int mtu,
496 			      u32 hds_thresh)
497 {
498 	if (!prog)
499 		return false;
500 
501 	if (prog->aux->xdp_has_frags)
502 		return false;
503 
504 	return mtu + ETH_HLEN > hds_thresh;
505 }
506 
fbnic_bpf(struct net_device * netdev,struct netdev_bpf * bpf)507 static int fbnic_bpf(struct net_device *netdev, struct netdev_bpf *bpf)
508 {
509 	struct bpf_prog *prog = bpf->prog, *prev_prog;
510 	struct fbnic_net *fbn = netdev_priv(netdev);
511 
512 	if (bpf->command != XDP_SETUP_PROG)
513 		return -EINVAL;
514 
515 	if (fbnic_check_split_frames(prog, netdev->mtu,
516 				     fbn->hds_thresh)) {
517 		NL_SET_ERR_MSG_MOD(bpf->extack,
518 				   "MTU too high, or HDS threshold is too low for single buffer XDP");
519 		return -EOPNOTSUPP;
520 	}
521 
522 	prev_prog = xchg(&fbn->xdp_prog, prog);
523 	if (prev_prog)
524 		bpf_prog_put(prev_prog);
525 
526 	return 0;
527 }
528 
529 static const struct net_device_ops fbnic_netdev_ops = {
530 	.ndo_open		= fbnic_open,
531 	.ndo_stop		= fbnic_stop,
532 	.ndo_validate_addr	= eth_validate_addr,
533 	.ndo_start_xmit		= fbnic_xmit_frame,
534 	.ndo_features_check	= fbnic_features_check,
535 	.ndo_set_mac_address	= fbnic_set_mac,
536 	.ndo_set_rx_mode	= fbnic_set_rx_mode,
537 	.ndo_get_stats64	= fbnic_get_stats64,
538 	.ndo_bpf		= fbnic_bpf,
539 	.ndo_hwtstamp_get	= fbnic_hwtstamp_get,
540 	.ndo_hwtstamp_set	= fbnic_hwtstamp_set,
541 };
542 
fbnic_get_queue_stats_rx(struct net_device * dev,int idx,struct netdev_queue_stats_rx * rx)543 static void fbnic_get_queue_stats_rx(struct net_device *dev, int idx,
544 				     struct netdev_queue_stats_rx *rx)
545 {
546 	u64 bytes, packets, alloc_fail, alloc_fail_bdq;
547 	struct fbnic_net *fbn = netdev_priv(dev);
548 	struct fbnic_ring *rxr = fbn->rx[idx];
549 	struct fbnic_dev *fbd = fbn->fbd;
550 	struct fbnic_queue_stats *stats;
551 	u64 csum_complete, csum_none;
552 	struct fbnic_q_triad *qt;
553 	unsigned int start;
554 
555 	if (!rxr)
556 		return;
557 
558 	/* fbn->rx points to completion queues */
559 	qt = container_of(rxr, struct fbnic_q_triad, cmpl);
560 
561 	stats = &rxr->stats;
562 	do {
563 		start = u64_stats_fetch_begin(&stats->syncp);
564 		bytes = stats->bytes;
565 		packets = stats->packets;
566 		alloc_fail = stats->rx.alloc_failed;
567 		csum_complete = stats->rx.csum_complete;
568 		csum_none = stats->rx.csum_none;
569 	} while (u64_stats_fetch_retry(&stats->syncp, start));
570 
571 	stats = &qt->sub0.stats;
572 	do {
573 		start = u64_stats_fetch_begin(&stats->syncp);
574 		alloc_fail_bdq = stats->bdq.alloc_failed;
575 	} while (u64_stats_fetch_retry(&stats->syncp, start));
576 	alloc_fail += alloc_fail_bdq;
577 
578 	stats = &qt->sub1.stats;
579 	do {
580 		start = u64_stats_fetch_begin(&stats->syncp);
581 		alloc_fail_bdq = stats->bdq.alloc_failed;
582 	} while (u64_stats_fetch_retry(&stats->syncp, start));
583 	alloc_fail += alloc_fail_bdq;
584 
585 	rx->bytes = bytes;
586 	rx->packets = packets;
587 	rx->alloc_fail = alloc_fail;
588 	rx->csum_complete = csum_complete;
589 	rx->csum_none = csum_none;
590 
591 	fbnic_get_hw_q_stats(fbd, fbd->hw_stats.hw_q);
592 
593 	spin_lock(&fbd->hw_stats.lock);
594 	rx->hw_drop_overruns = fbd->hw_stats.hw_q[idx].rde_pkt_cq_drop.value +
595 			       fbd->hw_stats.hw_q[idx].rde_pkt_bdq_drop.value;
596 	rx->hw_drops = fbd->hw_stats.hw_q[idx].rde_pkt_err.value +
597 		       rx->hw_drop_overruns;
598 	spin_unlock(&fbd->hw_stats.lock);
599 }
600 
fbnic_get_queue_stats_tx(struct net_device * dev,int idx,struct netdev_queue_stats_tx * tx)601 static void fbnic_get_queue_stats_tx(struct net_device *dev, int idx,
602 				     struct netdev_queue_stats_tx *tx)
603 {
604 	struct fbnic_net *fbn = netdev_priv(dev);
605 	struct fbnic_ring *txr = fbn->tx[idx];
606 	struct fbnic_queue_stats *stats;
607 	u64 stop, wake, csum, lso;
608 	struct fbnic_ring *xdpr;
609 	unsigned int start;
610 	u64 bytes, packets;
611 
612 	if (!txr)
613 		return;
614 
615 	stats = &txr->stats;
616 	do {
617 		start = u64_stats_fetch_begin(&stats->syncp);
618 		bytes = stats->bytes;
619 		packets = stats->packets;
620 		csum = stats->twq.csum_partial;
621 		lso = stats->twq.lso;
622 		stop = stats->twq.stop;
623 		wake = stats->twq.wake;
624 	} while (u64_stats_fetch_retry(&stats->syncp, start));
625 
626 	tx->bytes = bytes;
627 	tx->packets = packets;
628 	tx->needs_csum = csum + lso;
629 	tx->hw_gso_wire_packets = lso;
630 	tx->stop = stop;
631 	tx->wake = wake;
632 
633 	xdpr = fbn->tx[FBNIC_MAX_TXQS + idx];
634 	if (xdpr) {
635 		stats = &xdpr->stats;
636 		do {
637 			start = u64_stats_fetch_begin(&stats->syncp);
638 			bytes = stats->bytes;
639 			packets = stats->packets;
640 		} while (u64_stats_fetch_retry(&stats->syncp, start));
641 
642 		tx->bytes += bytes;
643 		tx->packets += packets;
644 	}
645 }
646 
fbnic_get_base_stats(struct net_device * dev,struct netdev_queue_stats_rx * rx,struct netdev_queue_stats_tx * tx)647 static void fbnic_get_base_stats(struct net_device *dev,
648 				 struct netdev_queue_stats_rx *rx,
649 				 struct netdev_queue_stats_tx *tx)
650 {
651 	struct fbnic_net *fbn = netdev_priv(dev);
652 
653 	tx->bytes = fbn->tx_stats.bytes;
654 	tx->packets = fbn->tx_stats.packets;
655 	tx->needs_csum = fbn->tx_stats.twq.csum_partial + fbn->tx_stats.twq.lso;
656 	tx->hw_gso_wire_packets = fbn->tx_stats.twq.lso;
657 	tx->stop = fbn->tx_stats.twq.stop;
658 	tx->wake = fbn->tx_stats.twq.wake;
659 
660 	rx->bytes = fbn->rx_stats.bytes;
661 	rx->packets = fbn->rx_stats.packets;
662 	rx->alloc_fail = fbn->rx_stats.rx.alloc_failed +
663 		fbn->bdq_stats.bdq.alloc_failed;
664 	rx->csum_complete = fbn->rx_stats.rx.csum_complete;
665 	rx->csum_none = fbn->rx_stats.rx.csum_none;
666 }
667 
668 static const struct netdev_stat_ops fbnic_stat_ops = {
669 	.get_queue_stats_rx	= fbnic_get_queue_stats_rx,
670 	.get_queue_stats_tx	= fbnic_get_queue_stats_tx,
671 	.get_base_stats		= fbnic_get_base_stats,
672 };
673 
fbnic_reset_queues(struct fbnic_net * fbn,unsigned int tx,unsigned int rx)674 void fbnic_reset_queues(struct fbnic_net *fbn,
675 			unsigned int tx, unsigned int rx)
676 {
677 	struct fbnic_dev *fbd = fbn->fbd;
678 	unsigned int max_napis;
679 
680 	max_napis = fbd->num_irqs - FBNIC_NON_NAPI_VECTORS;
681 
682 	tx = min(tx, max_napis);
683 	fbn->num_tx_queues = tx;
684 
685 	rx = min(rx, max_napis);
686 	fbn->num_rx_queues = rx;
687 
688 	fbn->num_napi = max(tx, rx);
689 }
690 
691 /**
692  * fbnic_netdev_free - Free the netdev associate with fbnic
693  * @fbd: Driver specific structure to free netdev from
694  *
695  * Allocate and initialize the netdev and netdev private structure. Bind
696  * together the hardware, netdev, and pci data structures.
697  **/
fbnic_netdev_free(struct fbnic_dev * fbd)698 void fbnic_netdev_free(struct fbnic_dev *fbd)
699 {
700 	struct fbnic_net *fbn = netdev_priv(fbd->netdev);
701 
702 	if (fbn->phylink)
703 		phylink_destroy(fbn->phylink);
704 
705 	free_netdev(fbd->netdev);
706 	fbd->netdev = NULL;
707 }
708 
709 /**
710  * fbnic_netdev_alloc - Allocate a netdev and associate with fbnic
711  * @fbd: Driver specific structure to associate netdev with
712  *
713  * Allocate and initialize the netdev and netdev private structure. Bind
714  * together the hardware, netdev, and pci data structures.
715  *
716  *  Return: Pointer to net_device on success, NULL on failure
717  **/
fbnic_netdev_alloc(struct fbnic_dev * fbd)718 struct net_device *fbnic_netdev_alloc(struct fbnic_dev *fbd)
719 {
720 	struct net_device *netdev;
721 	struct fbnic_net *fbn;
722 	int default_queues;
723 
724 	netdev = alloc_etherdev_mq(sizeof(*fbn), FBNIC_MAX_RXQS);
725 	if (!netdev)
726 		return NULL;
727 
728 	SET_NETDEV_DEV(netdev, fbd->dev);
729 	fbd->netdev = netdev;
730 
731 	netdev->netdev_ops = &fbnic_netdev_ops;
732 	netdev->stat_ops = &fbnic_stat_ops;
733 	netdev->queue_mgmt_ops = &fbnic_queue_mgmt_ops;
734 	netdev->netmem_tx = true;
735 
736 	fbnic_set_ethtool_ops(netdev);
737 
738 	fbn = netdev_priv(netdev);
739 
740 	fbn->netdev = netdev;
741 	fbn->fbd = fbd;
742 
743 	fbn->txq_size = FBNIC_TXQ_SIZE_DEFAULT;
744 	fbn->hpq_size = FBNIC_HPQ_SIZE_DEFAULT;
745 	fbn->ppq_size = FBNIC_PPQ_SIZE_DEFAULT;
746 	fbn->rcq_size = FBNIC_RCQ_SIZE_DEFAULT;
747 
748 	fbn->tx_usecs = FBNIC_TX_USECS_DEFAULT;
749 	fbn->rx_usecs = FBNIC_RX_USECS_DEFAULT;
750 	fbn->rx_max_frames = FBNIC_RX_FRAMES_DEFAULT;
751 
752 	/* Initialize the hds_thresh */
753 	netdev->cfg->hds_thresh = FBNIC_HDS_THRESH_DEFAULT;
754 	fbn->hds_thresh = FBNIC_HDS_THRESH_DEFAULT;
755 
756 	default_queues = netif_get_num_default_rss_queues();
757 	if (default_queues > fbd->max_num_queues)
758 		default_queues = fbd->max_num_queues;
759 
760 	fbnic_reset_queues(fbn, default_queues, default_queues);
761 
762 	fbnic_reset_indir_tbl(fbn);
763 	fbnic_rss_key_fill(fbn->rss_key);
764 	fbnic_rss_init_en_mask(fbn);
765 
766 	netdev->priv_flags |= IFF_UNICAST_FLT;
767 
768 	netdev->gso_partial_features =
769 		NETIF_F_GSO_GRE |
770 		NETIF_F_GSO_GRE_CSUM |
771 		NETIF_F_GSO_IPXIP4 |
772 		NETIF_F_GSO_UDP_TUNNEL |
773 		NETIF_F_GSO_UDP_TUNNEL_CSUM;
774 
775 	netdev->features |=
776 		netdev->gso_partial_features |
777 		FBNIC_TUN_GSO_FEATURES |
778 		NETIF_F_RXHASH |
779 		NETIF_F_SG |
780 		NETIF_F_HW_CSUM |
781 		NETIF_F_RXCSUM |
782 		NETIF_F_TSO |
783 		NETIF_F_TSO_ECN |
784 		NETIF_F_TSO6 |
785 		NETIF_F_GSO_PARTIAL |
786 		NETIF_F_GSO_UDP_L4;
787 
788 	netdev->hw_features |= netdev->features;
789 	netdev->vlan_features |= netdev->features;
790 	netdev->hw_enc_features |= netdev->features;
791 	netdev->features |= NETIF_F_NTUPLE;
792 
793 	netdev->min_mtu = IPV6_MIN_MTU;
794 	netdev->max_mtu = FBNIC_MAX_JUMBO_FRAME_SIZE - ETH_HLEN;
795 
796 	/* TBD: This is workaround for BMC as phylink doesn't have support
797 	 * for leavling the link enabled if a BMC is present.
798 	 */
799 	netdev->ethtool->wol_enabled = true;
800 
801 	netif_carrier_off(netdev);
802 
803 	netif_tx_stop_all_queues(netdev);
804 
805 	if (fbnic_phylink_init(netdev)) {
806 		fbnic_netdev_free(fbd);
807 		return NULL;
808 	}
809 
810 	return netdev;
811 }
812 
fbnic_dsn_to_mac_addr(u64 dsn,char * addr)813 static int fbnic_dsn_to_mac_addr(u64 dsn, char *addr)
814 {
815 	addr[0] = (dsn >> 56) & 0xFF;
816 	addr[1] = (dsn >> 48) & 0xFF;
817 	addr[2] = (dsn >> 40) & 0xFF;
818 	addr[3] = (dsn >> 16) & 0xFF;
819 	addr[4] = (dsn >> 8) & 0xFF;
820 	addr[5] = dsn & 0xFF;
821 
822 	return is_valid_ether_addr(addr) ? 0 : -EINVAL;
823 }
824 
825 /**
826  * fbnic_netdev_register - Initialize general software structures
827  * @netdev: Netdev containing structure to initialize and register
828  *
829  * Initialize the MAC address for the netdev and register it.
830  *
831  *  Return: 0 on success, negative on failure
832  **/
fbnic_netdev_register(struct net_device * netdev)833 int fbnic_netdev_register(struct net_device *netdev)
834 {
835 	struct fbnic_net *fbn = netdev_priv(netdev);
836 	struct fbnic_dev *fbd = fbn->fbd;
837 	u64 dsn = fbd->dsn;
838 	u8 addr[ETH_ALEN];
839 	int err;
840 
841 	err = fbnic_dsn_to_mac_addr(dsn, addr);
842 	if (!err) {
843 		ether_addr_copy(netdev->perm_addr, addr);
844 		eth_hw_addr_set(netdev, addr);
845 	} else {
846 		/* A randomly assigned MAC address will cause provisioning
847 		 * issues so instead just fail to spawn the netdev and
848 		 * avoid any confusion.
849 		 */
850 		dev_err(fbd->dev, "MAC addr %pM invalid\n", addr);
851 		return err;
852 	}
853 
854 	return register_netdev(netdev);
855 }
856 
fbnic_netdev_unregister(struct net_device * netdev)857 void fbnic_netdev_unregister(struct net_device *netdev)
858 {
859 	unregister_netdev(netdev);
860 }
861