xref: /linux/drivers/net/ethernet/meta/fbnic/fbnic_netdev.c (revision 37816488247ddddbc3de113c78c83572274b1e2e)
1bc610777SAlexander Duyck // SPDX-License-Identifier: GPL-2.0
2bc610777SAlexander Duyck /* Copyright (c) Meta Platforms, Inc. and affiliates. */
3bc610777SAlexander Duyck 
4bc610777SAlexander Duyck #include <linux/etherdevice.h>
5bc610777SAlexander Duyck #include <linux/ipv6.h>
6bc610777SAlexander Duyck #include <linux/types.h>
78be1bd91SStanislav Fomichev #include <net/netdev_queues.h>
8bc610777SAlexander Duyck 
9bc610777SAlexander Duyck #include "fbnic.h"
10bc610777SAlexander Duyck #include "fbnic_netdev.h"
11bc610777SAlexander Duyck #include "fbnic_txrx.h"
12bc610777SAlexander Duyck 
__fbnic_open(struct fbnic_net * fbn)13bc610777SAlexander Duyck int __fbnic_open(struct fbnic_net *fbn)
14bc610777SAlexander Duyck {
1520d2e88cSAlexander Duyck 	struct fbnic_dev *fbd = fbn->fbd;
16bc610777SAlexander Duyck 	int err;
17bc610777SAlexander Duyck 
18bc610777SAlexander Duyck 	err = fbnic_alloc_napi_vectors(fbn);
19bc610777SAlexander Duyck 	if (err)
20bc610777SAlexander Duyck 		return err;
21bc610777SAlexander Duyck 
2240bf06a1SAlexander Duyck 	err = fbnic_alloc_resources(fbn);
2340bf06a1SAlexander Duyck 	if (err)
2440bf06a1SAlexander Duyck 		goto free_napi_vectors;
2540bf06a1SAlexander Duyck 
26557d0223SAlexander Duyck 	err = fbnic_set_netif_queues(fbn);
27bc610777SAlexander Duyck 	if (err)
28bc610777SAlexander Duyck 		goto free_resources;
29bc610777SAlexander Duyck 
3020d2e88cSAlexander Duyck 	/* Send ownership message and flush to verify FW has seen it */
3120d2e88cSAlexander Duyck 	err = fbnic_fw_xmit_ownership_msg(fbd, true);
3220d2e88cSAlexander Duyck 	if (err) {
3320d2e88cSAlexander Duyck 		dev_warn(fbd->dev,
3420d2e88cSAlexander Duyck 			 "Error %d sending host ownership message to the firmware\n",
3520d2e88cSAlexander Duyck 			 err);
364b31bcb0SJakub Kicinski 		goto err_reset_queues;
3720d2e88cSAlexander Duyck 	}
3820d2e88cSAlexander Duyck 
39ad8e66a4SVadim Fedorenko 	err = fbnic_time_start(fbn);
4020d2e88cSAlexander Duyck 	if (err)
4120d2e88cSAlexander Duyck 		goto release_ownership;
4220d2e88cSAlexander Duyck 
43ad8e66a4SVadim Fedorenko 	err = fbnic_fw_init_heartbeat(fbd, false);
44ad8e66a4SVadim Fedorenko 	if (err)
45ad8e66a4SVadim Fedorenko 		goto time_stop;
46ad8e66a4SVadim Fedorenko 
47682a6128SAlexander Duyck 	err = fbnic_pcs_request_irq(fbd);
4869684376SAlexander Duyck 	if (err)
49ad8e66a4SVadim Fedorenko 		goto time_stop;
50682a6128SAlexander Duyck 
51eb690ef8SAlexander Duyck 	/* Pull the BMC config and initialize the RPC */
52eb690ef8SAlexander Duyck 	fbnic_bmc_rpc_init(fbd);
53355440a6SAlexander Duyck 	fbnic_rss_reinit(fbd, fbn);
5469684376SAlexander Duyck 
55bc610777SAlexander Duyck 	return 0;
56ad8e66a4SVadim Fedorenko time_stop:
57ad8e66a4SVadim Fedorenko 	fbnic_time_stop(fbn);
5820d2e88cSAlexander Duyck release_ownership:
5920d2e88cSAlexander Duyck 	fbnic_fw_xmit_ownership_msg(fbn->fbd, false);
604b31bcb0SJakub Kicinski err_reset_queues:
614b31bcb0SJakub Kicinski 	fbnic_reset_netif_queues(fbn);
62bc610777SAlexander Duyck free_resources:
6340bf06a1SAlexander Duyck 	fbnic_free_resources(fbn);
6440bf06a1SAlexander Duyck free_napi_vectors:
65bc610777SAlexander Duyck 	fbnic_free_napi_vectors(fbn);
66bc610777SAlexander Duyck 	return err;
67bc610777SAlexander Duyck }
68bc610777SAlexander Duyck 
fbnic_open(struct net_device * netdev)69bc610777SAlexander Duyck static int fbnic_open(struct net_device *netdev)
70bc610777SAlexander Duyck {
71bc610777SAlexander Duyck 	struct fbnic_net *fbn = netdev_priv(netdev);
72bc610777SAlexander Duyck 	int err;
73bc610777SAlexander Duyck 
743a856ab3SJakub Kicinski 	fbnic_napi_name_irqs(fbn->fbd);
753a856ab3SJakub Kicinski 
76bc610777SAlexander Duyck 	err = __fbnic_open(fbn);
77bc610777SAlexander Duyck 	if (!err)
78bc610777SAlexander Duyck 		fbnic_up(fbn);
79bc610777SAlexander Duyck 
80bc610777SAlexander Duyck 	return err;
81bc610777SAlexander Duyck }
82bc610777SAlexander Duyck 
fbnic_stop(struct net_device * netdev)83bc610777SAlexander Duyck static int fbnic_stop(struct net_device *netdev)
84bc610777SAlexander Duyck {
85bc610777SAlexander Duyck 	struct fbnic_net *fbn = netdev_priv(netdev);
86bc610777SAlexander Duyck 
87bc610777SAlexander Duyck 	fbnic_down(fbn);
88682a6128SAlexander Duyck 	fbnic_pcs_free_irq(fbn->fbd);
89bc610777SAlexander Duyck 
90ad8e66a4SVadim Fedorenko 	fbnic_time_stop(fbn);
9120d2e88cSAlexander Duyck 	fbnic_fw_xmit_ownership_msg(fbn->fbd, false);
9220d2e88cSAlexander Duyck 
93557d0223SAlexander Duyck 	fbnic_reset_netif_queues(fbn);
9440bf06a1SAlexander Duyck 	fbnic_free_resources(fbn);
95bc610777SAlexander Duyck 	fbnic_free_napi_vectors(fbn);
96bc610777SAlexander Duyck 
97bc610777SAlexander Duyck 	return 0;
98bc610777SAlexander Duyck }
99bc610777SAlexander Duyck 
fbnic_uc_sync(struct net_device * netdev,const unsigned char * addr)100eb690ef8SAlexander Duyck static int fbnic_uc_sync(struct net_device *netdev, const unsigned char *addr)
101eb690ef8SAlexander Duyck {
102eb690ef8SAlexander Duyck 	struct fbnic_net *fbn = netdev_priv(netdev);
103eb690ef8SAlexander Duyck 	struct fbnic_mac_addr *avail_addr;
104eb690ef8SAlexander Duyck 
105eb690ef8SAlexander Duyck 	if (WARN_ON(!is_valid_ether_addr(addr)))
106eb690ef8SAlexander Duyck 		return -EADDRNOTAVAIL;
107eb690ef8SAlexander Duyck 
108eb690ef8SAlexander Duyck 	avail_addr = __fbnic_uc_sync(fbn->fbd, addr);
109eb690ef8SAlexander Duyck 	if (!avail_addr)
110eb690ef8SAlexander Duyck 		return -ENOSPC;
111eb690ef8SAlexander Duyck 
112eb690ef8SAlexander Duyck 	/* Add type flag indicating this address is in use by the host */
113eb690ef8SAlexander Duyck 	set_bit(FBNIC_MAC_ADDR_T_UNICAST, avail_addr->act_tcam);
114eb690ef8SAlexander Duyck 
115eb690ef8SAlexander Duyck 	return 0;
116eb690ef8SAlexander Duyck }
117eb690ef8SAlexander Duyck 
fbnic_uc_unsync(struct net_device * netdev,const unsigned char * addr)118eb690ef8SAlexander Duyck static int fbnic_uc_unsync(struct net_device *netdev, const unsigned char *addr)
119eb690ef8SAlexander Duyck {
120eb690ef8SAlexander Duyck 	struct fbnic_net *fbn = netdev_priv(netdev);
121eb690ef8SAlexander Duyck 	struct fbnic_dev *fbd = fbn->fbd;
122eb690ef8SAlexander Duyck 	int i, ret;
123eb690ef8SAlexander Duyck 
124eb690ef8SAlexander Duyck 	/* Scan from middle of list to bottom, filling bottom up.
125eb690ef8SAlexander Duyck 	 * Skip the first entry which is reserved for dev_addr and
126eb690ef8SAlexander Duyck 	 * leave the last entry to use for promiscuous filtering.
127eb690ef8SAlexander Duyck 	 */
128eb690ef8SAlexander Duyck 	for (i = fbd->mac_addr_boundary, ret = -ENOENT;
129eb690ef8SAlexander Duyck 	     i < FBNIC_RPC_TCAM_MACDA_HOST_ADDR_IDX && ret; i++) {
130eb690ef8SAlexander Duyck 		struct fbnic_mac_addr *mac_addr = &fbd->mac_addr[i];
131eb690ef8SAlexander Duyck 
132eb690ef8SAlexander Duyck 		if (!ether_addr_equal(mac_addr->value.addr8, addr))
133eb690ef8SAlexander Duyck 			continue;
134eb690ef8SAlexander Duyck 
135eb690ef8SAlexander Duyck 		ret = __fbnic_uc_unsync(mac_addr);
136eb690ef8SAlexander Duyck 	}
137eb690ef8SAlexander Duyck 
138eb690ef8SAlexander Duyck 	return ret;
139eb690ef8SAlexander Duyck }
140eb690ef8SAlexander Duyck 
fbnic_mc_sync(struct net_device * netdev,const unsigned char * addr)141eb690ef8SAlexander Duyck static int fbnic_mc_sync(struct net_device *netdev, const unsigned char *addr)
142eb690ef8SAlexander Duyck {
143eb690ef8SAlexander Duyck 	struct fbnic_net *fbn = netdev_priv(netdev);
144eb690ef8SAlexander Duyck 	struct fbnic_mac_addr *avail_addr;
145eb690ef8SAlexander Duyck 
146eb690ef8SAlexander Duyck 	if (WARN_ON(!is_multicast_ether_addr(addr)))
147eb690ef8SAlexander Duyck 		return -EADDRNOTAVAIL;
148eb690ef8SAlexander Duyck 
149eb690ef8SAlexander Duyck 	avail_addr = __fbnic_mc_sync(fbn->fbd, addr);
150eb690ef8SAlexander Duyck 	if (!avail_addr)
151eb690ef8SAlexander Duyck 		return -ENOSPC;
152eb690ef8SAlexander Duyck 
153eb690ef8SAlexander Duyck 	/* Add type flag indicating this address is in use by the host */
154eb690ef8SAlexander Duyck 	set_bit(FBNIC_MAC_ADDR_T_MULTICAST, avail_addr->act_tcam);
155eb690ef8SAlexander Duyck 
156eb690ef8SAlexander Duyck 	return 0;
157eb690ef8SAlexander Duyck }
158eb690ef8SAlexander Duyck 
fbnic_mc_unsync(struct net_device * netdev,const unsigned char * addr)159eb690ef8SAlexander Duyck static int fbnic_mc_unsync(struct net_device *netdev, const unsigned char *addr)
160eb690ef8SAlexander Duyck {
161eb690ef8SAlexander Duyck 	struct fbnic_net *fbn = netdev_priv(netdev);
162eb690ef8SAlexander Duyck 	struct fbnic_dev *fbd = fbn->fbd;
163eb690ef8SAlexander Duyck 	int i, ret;
164eb690ef8SAlexander Duyck 
165eb690ef8SAlexander Duyck 	/* Scan from middle of list to top, filling top down.
166eb690ef8SAlexander Duyck 	 * Skip over the address reserved for the BMC MAC and
167eb690ef8SAlexander Duyck 	 * exclude index 0 as that belongs to the broadcast address
168eb690ef8SAlexander Duyck 	 */
169eb690ef8SAlexander Duyck 	for (i = fbd->mac_addr_boundary, ret = -ENOENT;
170eb690ef8SAlexander Duyck 	     --i > FBNIC_RPC_TCAM_MACDA_BROADCAST_IDX && ret;) {
171eb690ef8SAlexander Duyck 		struct fbnic_mac_addr *mac_addr = &fbd->mac_addr[i];
172eb690ef8SAlexander Duyck 
173eb690ef8SAlexander Duyck 		if (!ether_addr_equal(mac_addr->value.addr8, addr))
174eb690ef8SAlexander Duyck 			continue;
175eb690ef8SAlexander Duyck 
176eb690ef8SAlexander Duyck 		ret = __fbnic_mc_unsync(mac_addr);
177eb690ef8SAlexander Duyck 	}
178eb690ef8SAlexander Duyck 
179eb690ef8SAlexander Duyck 	return ret;
180eb690ef8SAlexander Duyck }
181eb690ef8SAlexander Duyck 
__fbnic_set_rx_mode(struct net_device * netdev)182eb690ef8SAlexander Duyck void __fbnic_set_rx_mode(struct net_device *netdev)
183eb690ef8SAlexander Duyck {
184eb690ef8SAlexander Duyck 	struct fbnic_net *fbn = netdev_priv(netdev);
185eb690ef8SAlexander Duyck 	bool uc_promisc = false, mc_promisc = false;
186eb690ef8SAlexander Duyck 	struct fbnic_dev *fbd = fbn->fbd;
187eb690ef8SAlexander Duyck 	struct fbnic_mac_addr *mac_addr;
188eb690ef8SAlexander Duyck 	int err;
189eb690ef8SAlexander Duyck 
190eb690ef8SAlexander Duyck 	/* Populate host address from dev_addr */
191eb690ef8SAlexander Duyck 	mac_addr = &fbd->mac_addr[FBNIC_RPC_TCAM_MACDA_HOST_ADDR_IDX];
192eb690ef8SAlexander Duyck 	if (!ether_addr_equal(mac_addr->value.addr8, netdev->dev_addr) ||
193eb690ef8SAlexander Duyck 	    mac_addr->state != FBNIC_TCAM_S_VALID) {
194eb690ef8SAlexander Duyck 		ether_addr_copy(mac_addr->value.addr8, netdev->dev_addr);
195eb690ef8SAlexander Duyck 		mac_addr->state = FBNIC_TCAM_S_UPDATE;
196eb690ef8SAlexander Duyck 		set_bit(FBNIC_MAC_ADDR_T_UNICAST, mac_addr->act_tcam);
197eb690ef8SAlexander Duyck 	}
198eb690ef8SAlexander Duyck 
199eb690ef8SAlexander Duyck 	/* Populate broadcast address if broadcast is enabled */
200eb690ef8SAlexander Duyck 	mac_addr = &fbd->mac_addr[FBNIC_RPC_TCAM_MACDA_BROADCAST_IDX];
201eb690ef8SAlexander Duyck 	if (netdev->flags & IFF_BROADCAST) {
202eb690ef8SAlexander Duyck 		if (!is_broadcast_ether_addr(mac_addr->value.addr8) ||
203eb690ef8SAlexander Duyck 		    mac_addr->state != FBNIC_TCAM_S_VALID) {
204eb690ef8SAlexander Duyck 			eth_broadcast_addr(mac_addr->value.addr8);
205eb690ef8SAlexander Duyck 			mac_addr->state = FBNIC_TCAM_S_ADD;
206eb690ef8SAlexander Duyck 		}
207eb690ef8SAlexander Duyck 		set_bit(FBNIC_MAC_ADDR_T_BROADCAST, mac_addr->act_tcam);
208eb690ef8SAlexander Duyck 	} else if (mac_addr->state == FBNIC_TCAM_S_VALID) {
209eb690ef8SAlexander Duyck 		__fbnic_xc_unsync(mac_addr, FBNIC_MAC_ADDR_T_BROADCAST);
210eb690ef8SAlexander Duyck 	}
211eb690ef8SAlexander Duyck 
212eb690ef8SAlexander Duyck 	/* Synchronize unicast and multicast address lists */
213eb690ef8SAlexander Duyck 	err = __dev_uc_sync(netdev, fbnic_uc_sync, fbnic_uc_unsync);
214eb690ef8SAlexander Duyck 	if (err == -ENOSPC)
215eb690ef8SAlexander Duyck 		uc_promisc = true;
216eb690ef8SAlexander Duyck 	err = __dev_mc_sync(netdev, fbnic_mc_sync, fbnic_mc_unsync);
217eb690ef8SAlexander Duyck 	if (err == -ENOSPC)
218eb690ef8SAlexander Duyck 		mc_promisc = true;
219eb690ef8SAlexander Duyck 
220eb690ef8SAlexander Duyck 	uc_promisc |= !!(netdev->flags & IFF_PROMISC);
221eb690ef8SAlexander Duyck 	mc_promisc |= !!(netdev->flags & IFF_ALLMULTI) || uc_promisc;
222eb690ef8SAlexander Duyck 
223eb690ef8SAlexander Duyck 	/* Populate last TCAM entry with promiscuous entry and 0/1 bit mask */
224eb690ef8SAlexander Duyck 	mac_addr = &fbd->mac_addr[FBNIC_RPC_TCAM_MACDA_PROMISC_IDX];
225eb690ef8SAlexander Duyck 	if (uc_promisc) {
226eb690ef8SAlexander Duyck 		if (!is_zero_ether_addr(mac_addr->value.addr8) ||
227eb690ef8SAlexander Duyck 		    mac_addr->state != FBNIC_TCAM_S_VALID) {
228eb690ef8SAlexander Duyck 			eth_zero_addr(mac_addr->value.addr8);
229eb690ef8SAlexander Duyck 			eth_broadcast_addr(mac_addr->mask.addr8);
230eb690ef8SAlexander Duyck 			clear_bit(FBNIC_MAC_ADDR_T_ALLMULTI,
231eb690ef8SAlexander Duyck 				  mac_addr->act_tcam);
232eb690ef8SAlexander Duyck 			set_bit(FBNIC_MAC_ADDR_T_PROMISC,
233eb690ef8SAlexander Duyck 				mac_addr->act_tcam);
234eb690ef8SAlexander Duyck 			mac_addr->state = FBNIC_TCAM_S_ADD;
235eb690ef8SAlexander Duyck 		}
236eb690ef8SAlexander Duyck 	} else if (mc_promisc &&
237eb690ef8SAlexander Duyck 		   (!fbnic_bmc_present(fbd) || !fbd->fw_cap.all_multi)) {
238eb690ef8SAlexander Duyck 		/* We have to add a special handler for multicast as the
239eb690ef8SAlexander Duyck 		 * BMC may have an all-multi rule already in place. As such
240eb690ef8SAlexander Duyck 		 * adding a rule ourselves won't do any good so we will have
241eb690ef8SAlexander Duyck 		 * to modify the rules for the ALL MULTI below if the BMC
242eb690ef8SAlexander Duyck 		 * already has the rule in place.
243eb690ef8SAlexander Duyck 		 */
244eb690ef8SAlexander Duyck 		if (!is_multicast_ether_addr(mac_addr->value.addr8) ||
245eb690ef8SAlexander Duyck 		    mac_addr->state != FBNIC_TCAM_S_VALID) {
246eb690ef8SAlexander Duyck 			eth_zero_addr(mac_addr->value.addr8);
247eb690ef8SAlexander Duyck 			eth_broadcast_addr(mac_addr->mask.addr8);
248eb690ef8SAlexander Duyck 			mac_addr->value.addr8[0] ^= 1;
249eb690ef8SAlexander Duyck 			mac_addr->mask.addr8[0] ^= 1;
250eb690ef8SAlexander Duyck 			set_bit(FBNIC_MAC_ADDR_T_ALLMULTI,
251eb690ef8SAlexander Duyck 				mac_addr->act_tcam);
252eb690ef8SAlexander Duyck 			clear_bit(FBNIC_MAC_ADDR_T_PROMISC,
253eb690ef8SAlexander Duyck 				  mac_addr->act_tcam);
254eb690ef8SAlexander Duyck 			mac_addr->state = FBNIC_TCAM_S_ADD;
255eb690ef8SAlexander Duyck 		}
256eb690ef8SAlexander Duyck 	} else if (mac_addr->state == FBNIC_TCAM_S_VALID) {
257eb690ef8SAlexander Duyck 		if (test_bit(FBNIC_MAC_ADDR_T_BMC, mac_addr->act_tcam)) {
258eb690ef8SAlexander Duyck 			clear_bit(FBNIC_MAC_ADDR_T_ALLMULTI,
259eb690ef8SAlexander Duyck 				  mac_addr->act_tcam);
260eb690ef8SAlexander Duyck 			clear_bit(FBNIC_MAC_ADDR_T_PROMISC,
261eb690ef8SAlexander Duyck 				  mac_addr->act_tcam);
262eb690ef8SAlexander Duyck 		} else {
263eb690ef8SAlexander Duyck 			mac_addr->state = FBNIC_TCAM_S_DELETE;
264eb690ef8SAlexander Duyck 		}
265eb690ef8SAlexander Duyck 	}
266eb690ef8SAlexander Duyck 
267eb690ef8SAlexander Duyck 	/* Add rules for BMC all multicast if it is enabled */
268eb690ef8SAlexander Duyck 	fbnic_bmc_rpc_all_multi_config(fbd, mc_promisc);
269eb690ef8SAlexander Duyck 
270eb690ef8SAlexander Duyck 	/* Sift out any unshared BMC rules and place them in BMC only section */
271eb690ef8SAlexander Duyck 	fbnic_sift_macda(fbd);
272eb690ef8SAlexander Duyck 
273eb690ef8SAlexander Duyck 	/* Write updates to hardware */
274355440a6SAlexander Duyck 	fbnic_write_rules(fbd);
275eb690ef8SAlexander Duyck 	fbnic_write_macda(fbd);
27690c940ffSMohsin Bashir 	fbnic_write_tce_tcam(fbd);
277eb690ef8SAlexander Duyck }
278eb690ef8SAlexander Duyck 
fbnic_set_rx_mode(struct net_device * netdev)279eb690ef8SAlexander Duyck static void fbnic_set_rx_mode(struct net_device *netdev)
280eb690ef8SAlexander Duyck {
281eb690ef8SAlexander Duyck 	/* No need to update the hardware if we are not running */
282eb690ef8SAlexander Duyck 	if (netif_running(netdev))
283eb690ef8SAlexander Duyck 		__fbnic_set_rx_mode(netdev);
284eb690ef8SAlexander Duyck }
285eb690ef8SAlexander Duyck 
fbnic_set_mac(struct net_device * netdev,void * p)286eb690ef8SAlexander Duyck static int fbnic_set_mac(struct net_device *netdev, void *p)
287eb690ef8SAlexander Duyck {
288eb690ef8SAlexander Duyck 	struct sockaddr *addr = p;
289eb690ef8SAlexander Duyck 
290eb690ef8SAlexander Duyck 	if (!is_valid_ether_addr(addr->sa_data))
291eb690ef8SAlexander Duyck 		return -EADDRNOTAVAIL;
292eb690ef8SAlexander Duyck 
293eb690ef8SAlexander Duyck 	eth_hw_addr_set(netdev, addr->sa_data);
294eb690ef8SAlexander Duyck 
295eb690ef8SAlexander Duyck 	fbnic_set_rx_mode(netdev);
296eb690ef8SAlexander Duyck 
297eb690ef8SAlexander Duyck 	return 0;
298eb690ef8SAlexander Duyck }
299eb690ef8SAlexander Duyck 
fbnic_clear_rx_mode(struct net_device * netdev)300eb690ef8SAlexander Duyck void fbnic_clear_rx_mode(struct net_device *netdev)
301eb690ef8SAlexander Duyck {
302eb690ef8SAlexander Duyck 	struct fbnic_net *fbn = netdev_priv(netdev);
303eb690ef8SAlexander Duyck 	struct fbnic_dev *fbd = fbn->fbd;
304eb690ef8SAlexander Duyck 	int idx;
305eb690ef8SAlexander Duyck 
306eb690ef8SAlexander Duyck 	for (idx = ARRAY_SIZE(fbd->mac_addr); idx--;) {
307eb690ef8SAlexander Duyck 		struct fbnic_mac_addr *mac_addr = &fbd->mac_addr[idx];
308eb690ef8SAlexander Duyck 
309eb690ef8SAlexander Duyck 		if (mac_addr->state != FBNIC_TCAM_S_VALID)
310eb690ef8SAlexander Duyck 			continue;
311eb690ef8SAlexander Duyck 
312eb690ef8SAlexander Duyck 		bitmap_clear(mac_addr->act_tcam,
313eb690ef8SAlexander Duyck 			     FBNIC_MAC_ADDR_T_HOST_START,
314eb690ef8SAlexander Duyck 			     FBNIC_MAC_ADDR_T_HOST_LEN);
315eb690ef8SAlexander Duyck 
316eb690ef8SAlexander Duyck 		if (bitmap_empty(mac_addr->act_tcam,
317eb690ef8SAlexander Duyck 				 FBNIC_RPC_TCAM_ACT_NUM_ENTRIES))
318eb690ef8SAlexander Duyck 			mac_addr->state = FBNIC_TCAM_S_DELETE;
319eb690ef8SAlexander Duyck 	}
320eb690ef8SAlexander Duyck 
321eb690ef8SAlexander Duyck 	/* Write updates to hardware */
322eb690ef8SAlexander Duyck 	fbnic_write_macda(fbd);
323eb690ef8SAlexander Duyck 
324eb690ef8SAlexander Duyck 	__dev_uc_unsync(netdev, NULL);
325eb690ef8SAlexander Duyck 	__dev_mc_unsync(netdev, NULL);
326eb690ef8SAlexander Duyck }
327eb690ef8SAlexander Duyck 
fbnic_hwtstamp_get(struct net_device * netdev,struct kernel_hwtstamp_config * config)3286a2b3edeSVadim Fedorenko static int fbnic_hwtstamp_get(struct net_device *netdev,
3296a2b3edeSVadim Fedorenko 			      struct kernel_hwtstamp_config *config)
3306a2b3edeSVadim Fedorenko {
3316a2b3edeSVadim Fedorenko 	struct fbnic_net *fbn = netdev_priv(netdev);
3326a2b3edeSVadim Fedorenko 
3336a2b3edeSVadim Fedorenko 	*config = fbn->hwtstamp_config;
3346a2b3edeSVadim Fedorenko 
3356a2b3edeSVadim Fedorenko 	return 0;
3366a2b3edeSVadim Fedorenko }
3376a2b3edeSVadim Fedorenko 
fbnic_hwtstamp_set(struct net_device * netdev,struct kernel_hwtstamp_config * config,struct netlink_ext_ack * extack)3386a2b3edeSVadim Fedorenko static int fbnic_hwtstamp_set(struct net_device *netdev,
3396a2b3edeSVadim Fedorenko 			      struct kernel_hwtstamp_config *config,
3406a2b3edeSVadim Fedorenko 			      struct netlink_ext_ack *extack)
3416a2b3edeSVadim Fedorenko {
3426a2b3edeSVadim Fedorenko 	struct fbnic_net *fbn = netdev_priv(netdev);
3436a2b3edeSVadim Fedorenko 	int old_rx_filter;
3446a2b3edeSVadim Fedorenko 
3456a2b3edeSVadim Fedorenko 	if (config->source != HWTSTAMP_SOURCE_NETDEV)
3466a2b3edeSVadim Fedorenko 		return -EOPNOTSUPP;
3476a2b3edeSVadim Fedorenko 
3486a2b3edeSVadim Fedorenko 	if (!kernel_hwtstamp_config_changed(config, &fbn->hwtstamp_config))
3496a2b3edeSVadim Fedorenko 		return 0;
3506a2b3edeSVadim Fedorenko 
3516a2b3edeSVadim Fedorenko 	/* Upscale the filters */
3526a2b3edeSVadim Fedorenko 	switch (config->rx_filter) {
3536a2b3edeSVadim Fedorenko 	case HWTSTAMP_FILTER_NONE:
3546a2b3edeSVadim Fedorenko 	case HWTSTAMP_FILTER_ALL:
3556a2b3edeSVadim Fedorenko 	case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
3566a2b3edeSVadim Fedorenko 	case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
3576a2b3edeSVadim Fedorenko 	case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
3586a2b3edeSVadim Fedorenko 	case HWTSTAMP_FILTER_PTP_V2_EVENT:
3596a2b3edeSVadim Fedorenko 		break;
3606a2b3edeSVadim Fedorenko 	case HWTSTAMP_FILTER_NTP_ALL:
3616a2b3edeSVadim Fedorenko 		config->rx_filter = HWTSTAMP_FILTER_ALL;
3626a2b3edeSVadim Fedorenko 		break;
3636a2b3edeSVadim Fedorenko 	case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
3646a2b3edeSVadim Fedorenko 	case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
3656a2b3edeSVadim Fedorenko 		config->rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_EVENT;
3666a2b3edeSVadim Fedorenko 		break;
3676a2b3edeSVadim Fedorenko 	case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
3686a2b3edeSVadim Fedorenko 	case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
3696a2b3edeSVadim Fedorenko 		config->rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_EVENT;
3706a2b3edeSVadim Fedorenko 		break;
3716a2b3edeSVadim Fedorenko 	case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
3726a2b3edeSVadim Fedorenko 	case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
3736a2b3edeSVadim Fedorenko 		config->rx_filter = HWTSTAMP_FILTER_PTP_V2_L2_EVENT;
3746a2b3edeSVadim Fedorenko 		break;
3756a2b3edeSVadim Fedorenko 	case HWTSTAMP_FILTER_PTP_V2_SYNC:
3766a2b3edeSVadim Fedorenko 	case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
3776a2b3edeSVadim Fedorenko 		config->rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
3786a2b3edeSVadim Fedorenko 		break;
3796a2b3edeSVadim Fedorenko 	default:
3806a2b3edeSVadim Fedorenko 		return -ERANGE;
3816a2b3edeSVadim Fedorenko 	}
3826a2b3edeSVadim Fedorenko 
3836a2b3edeSVadim Fedorenko 	/* Configure */
3846a2b3edeSVadim Fedorenko 	old_rx_filter = fbn->hwtstamp_config.rx_filter;
3856a2b3edeSVadim Fedorenko 	memcpy(&fbn->hwtstamp_config, config, sizeof(*config));
3866a2b3edeSVadim Fedorenko 
3876a2b3edeSVadim Fedorenko 	if (old_rx_filter != config->rx_filter && netif_running(fbn->netdev)) {
3886a2b3edeSVadim Fedorenko 		fbnic_rss_reinit(fbn->fbd, fbn);
3896a2b3edeSVadim Fedorenko 		fbnic_write_rules(fbn->fbd);
3906a2b3edeSVadim Fedorenko 	}
3916a2b3edeSVadim Fedorenko 
3926a2b3edeSVadim Fedorenko 	/* Save / report back filter configuration
3936a2b3edeSVadim Fedorenko 	 * Note that our filter configuration is inexact. Instead of
3946a2b3edeSVadim Fedorenko 	 * filtering for a specific UDP port or L2 Ethertype we are
3956a2b3edeSVadim Fedorenko 	 * filtering in all UDP or all non-IP packets for timestamping. So
3966a2b3edeSVadim Fedorenko 	 * if anything other than FILTER_ALL is requested we report
3976a2b3edeSVadim Fedorenko 	 * FILTER_SOME indicating that we will be timestamping a few
3986a2b3edeSVadim Fedorenko 	 * additional packets.
3996a2b3edeSVadim Fedorenko 	 */
4006a2b3edeSVadim Fedorenko 	if (config->rx_filter > HWTSTAMP_FILTER_ALL)
4016a2b3edeSVadim Fedorenko 		config->rx_filter = HWTSTAMP_FILTER_SOME;
4026a2b3edeSVadim Fedorenko 
4036a2b3edeSVadim Fedorenko 	return 0;
4046a2b3edeSVadim Fedorenko }
4056a2b3edeSVadim Fedorenko 
fbnic_get_stats64(struct net_device * dev,struct rtnl_link_stats64 * stats64)40645d84008SJakub Kicinski static void fbnic_get_stats64(struct net_device *dev,
40745d84008SJakub Kicinski 			      struct rtnl_link_stats64 *stats64)
40845d84008SJakub Kicinski {
4098f20a2bfSMohsin Bashir 	u64 rx_bytes, rx_packets, rx_dropped = 0, rx_errors = 0;
41045d84008SJakub Kicinski 	u64 tx_bytes, tx_packets, tx_dropped = 0;
41145d84008SJakub Kicinski 	struct fbnic_net *fbn = netdev_priv(dev);
4128f20a2bfSMohsin Bashir 	struct fbnic_dev *fbd = fbn->fbd;
41345d84008SJakub Kicinski 	struct fbnic_queue_stats *stats;
414986c63a0SMohsin Bashir 	u64 rx_over = 0, rx_missed = 0;
41545d84008SJakub Kicinski 	unsigned int start, i;
4168f20a2bfSMohsin Bashir 
4178f20a2bfSMohsin Bashir 	fbnic_get_hw_stats(fbd);
41845d84008SJakub Kicinski 
41945d84008SJakub Kicinski 	stats = &fbn->tx_stats;
42045d84008SJakub Kicinski 
42145d84008SJakub Kicinski 	tx_bytes = stats->bytes;
42245d84008SJakub Kicinski 	tx_packets = stats->packets;
42345d84008SJakub Kicinski 	tx_dropped = stats->dropped;
42445d84008SJakub Kicinski 
4255f8bd2ceSMohsin Bashir 	/* Record drops from Tx HW Datapath */
426*53abd9c8SMohsin Bashir 	spin_lock(&fbd->hw_stats_lock);
427f2957147SMohsin Bashir 	tx_dropped += fbd->hw_stats.tmi.drop.frames.value +
428fbaeb7b0SMohsin Bashir 		      fbd->hw_stats.tti.cm_drop.frames.value +
429f2957147SMohsin Bashir 		      fbd->hw_stats.tti.frame_drop.frames.value +
430fbaeb7b0SMohsin Bashir 		      fbd->hw_stats.tti.tbi_drop.frames.value;
431*53abd9c8SMohsin Bashir 	spin_unlock(&fbd->hw_stats_lock);
4325f8bd2ceSMohsin Bashir 
4332972395dSMohsin Bashir 	stats64->tx_bytes = tx_bytes;
4342972395dSMohsin Bashir 	stats64->tx_packets = tx_packets;
4352972395dSMohsin Bashir 	stats64->tx_dropped = tx_dropped;
4362972395dSMohsin Bashir 
43745d84008SJakub Kicinski 	for (i = 0; i < fbn->num_tx_queues; i++) {
43845d84008SJakub Kicinski 		struct fbnic_ring *txr = fbn->tx[i];
43945d84008SJakub Kicinski 
44045d84008SJakub Kicinski 		if (!txr)
44145d84008SJakub Kicinski 			continue;
44245d84008SJakub Kicinski 
44345d84008SJakub Kicinski 		stats = &txr->stats;
44445d84008SJakub Kicinski 		do {
44545d84008SJakub Kicinski 			start = u64_stats_fetch_begin(&stats->syncp);
44645d84008SJakub Kicinski 			tx_bytes = stats->bytes;
44745d84008SJakub Kicinski 			tx_packets = stats->packets;
44845d84008SJakub Kicinski 			tx_dropped = stats->dropped;
44945d84008SJakub Kicinski 		} while (u64_stats_fetch_retry(&stats->syncp, start));
45045d84008SJakub Kicinski 
45145d84008SJakub Kicinski 		stats64->tx_bytes += tx_bytes;
45245d84008SJakub Kicinski 		stats64->tx_packets += tx_packets;
45345d84008SJakub Kicinski 		stats64->tx_dropped += tx_dropped;
45445d84008SJakub Kicinski 	}
45545d84008SJakub Kicinski 
45645d84008SJakub Kicinski 	stats = &fbn->rx_stats;
45745d84008SJakub Kicinski 
45845d84008SJakub Kicinski 	rx_bytes = stats->bytes;
45945d84008SJakub Kicinski 	rx_packets = stats->packets;
46045d84008SJakub Kicinski 	rx_dropped = stats->dropped;
46145d84008SJakub Kicinski 
4628f20a2bfSMohsin Bashir 	spin_lock(&fbd->hw_stats_lock);
463986c63a0SMohsin Bashir 	/* Record drops for the host FIFOs.
464986c63a0SMohsin Bashir 	 * 4: network to Host,	6: BMC to Host
465986c63a0SMohsin Bashir 	 * Exclude the BMC and MC FIFOs as those stats may contain drops
466986c63a0SMohsin Bashir 	 * due to unrelated items such as TCAM misses. They are still
467986c63a0SMohsin Bashir 	 * accessible through the ethtool stats.
468986c63a0SMohsin Bashir 	 */
469986c63a0SMohsin Bashir 	i = FBNIC_RXB_FIFO_HOST;
470986c63a0SMohsin Bashir 	rx_missed += fbd->hw_stats.rxb.fifo[i].drop.frames.value;
471986c63a0SMohsin Bashir 	i = FBNIC_RXB_FIFO_BMC_TO_HOST;
472986c63a0SMohsin Bashir 	rx_missed += fbd->hw_stats.rxb.fifo[i].drop.frames.value;
473986c63a0SMohsin Bashir 
4748f20a2bfSMohsin Bashir 	for (i = 0; i < fbd->max_num_queues; i++) {
4758f20a2bfSMohsin Bashir 		/* Report packets dropped due to CQ/BDQ being full/empty */
4768f20a2bfSMohsin Bashir 		rx_over += fbd->hw_stats.hw_q[i].rde_pkt_cq_drop.value;
4778f20a2bfSMohsin Bashir 		rx_over += fbd->hw_stats.hw_q[i].rde_pkt_bdq_drop.value;
4788f20a2bfSMohsin Bashir 
4798f20a2bfSMohsin Bashir 		/* Report packets with errors */
4808f20a2bfSMohsin Bashir 		rx_errors += fbd->hw_stats.hw_q[i].rde_pkt_err.value;
4818f20a2bfSMohsin Bashir 	}
4828f20a2bfSMohsin Bashir 	spin_unlock(&fbd->hw_stats_lock);
4838f20a2bfSMohsin Bashir 
48445d84008SJakub Kicinski 	stats64->rx_bytes = rx_bytes;
48545d84008SJakub Kicinski 	stats64->rx_packets = rx_packets;
48645d84008SJakub Kicinski 	stats64->rx_dropped = rx_dropped;
4878f20a2bfSMohsin Bashir 	stats64->rx_over_errors = rx_over;
4888f20a2bfSMohsin Bashir 	stats64->rx_errors = rx_errors;
489986c63a0SMohsin Bashir 	stats64->rx_missed_errors = rx_missed;
49045d84008SJakub Kicinski 
49145d84008SJakub Kicinski 	for (i = 0; i < fbn->num_rx_queues; i++) {
49245d84008SJakub Kicinski 		struct fbnic_ring *rxr = fbn->rx[i];
49345d84008SJakub Kicinski 
49445d84008SJakub Kicinski 		if (!rxr)
49545d84008SJakub Kicinski 			continue;
49645d84008SJakub Kicinski 
49745d84008SJakub Kicinski 		stats = &rxr->stats;
49845d84008SJakub Kicinski 		do {
49945d84008SJakub Kicinski 			start = u64_stats_fetch_begin(&stats->syncp);
50045d84008SJakub Kicinski 			rx_bytes = stats->bytes;
50145d84008SJakub Kicinski 			rx_packets = stats->packets;
50245d84008SJakub Kicinski 			rx_dropped = stats->dropped;
50345d84008SJakub Kicinski 		} while (u64_stats_fetch_retry(&stats->syncp, start));
50445d84008SJakub Kicinski 
50545d84008SJakub Kicinski 		stats64->rx_bytes += rx_bytes;
50645d84008SJakub Kicinski 		stats64->rx_packets += rx_packets;
50745d84008SJakub Kicinski 		stats64->rx_dropped += rx_dropped;
50845d84008SJakub Kicinski 	}
50945d84008SJakub Kicinski }
51045d84008SJakub Kicinski 
511bc610777SAlexander Duyck static const struct net_device_ops fbnic_netdev_ops = {
512bc610777SAlexander Duyck 	.ndo_open		= fbnic_open,
513bc610777SAlexander Duyck 	.ndo_stop		= fbnic_stop,
514bc610777SAlexander Duyck 	.ndo_validate_addr	= eth_validate_addr,
515bc610777SAlexander Duyck 	.ndo_start_xmit		= fbnic_xmit_frame,
5169a57bacdSAlexander Duyck 	.ndo_features_check	= fbnic_features_check,
517eb690ef8SAlexander Duyck 	.ndo_set_mac_address	= fbnic_set_mac,
518eb690ef8SAlexander Duyck 	.ndo_set_rx_mode	= fbnic_set_rx_mode,
51945d84008SJakub Kicinski 	.ndo_get_stats64	= fbnic_get_stats64,
5206a2b3edeSVadim Fedorenko 	.ndo_hwtstamp_get	= fbnic_hwtstamp_get,
5216a2b3edeSVadim Fedorenko 	.ndo_hwtstamp_set	= fbnic_hwtstamp_set,
522bc610777SAlexander Duyck };
523bc610777SAlexander Duyck 
fbnic_get_queue_stats_rx(struct net_device * dev,int idx,struct netdev_queue_stats_rx * rx)5248be1bd91SStanislav Fomichev static void fbnic_get_queue_stats_rx(struct net_device *dev, int idx,
5258be1bd91SStanislav Fomichev 				     struct netdev_queue_stats_rx *rx)
5268be1bd91SStanislav Fomichev {
5278be1bd91SStanislav Fomichev 	struct fbnic_net *fbn = netdev_priv(dev);
5288be1bd91SStanislav Fomichev 	struct fbnic_ring *rxr = fbn->rx[idx];
5298f20a2bfSMohsin Bashir 	struct fbnic_dev *fbd = fbn->fbd;
5308be1bd91SStanislav Fomichev 	struct fbnic_queue_stats *stats;
53167dc4eb5SJakub Kicinski 	u64 bytes, packets, alloc_fail;
53267dc4eb5SJakub Kicinski 	u64 csum_complete, csum_none;
5338be1bd91SStanislav Fomichev 	unsigned int start;
5348be1bd91SStanislav Fomichev 
5358be1bd91SStanislav Fomichev 	if (!rxr)
5368be1bd91SStanislav Fomichev 		return;
5378be1bd91SStanislav Fomichev 
5388be1bd91SStanislav Fomichev 	stats = &rxr->stats;
5398be1bd91SStanislav Fomichev 	do {
5408be1bd91SStanislav Fomichev 		start = u64_stats_fetch_begin(&stats->syncp);
5418be1bd91SStanislav Fomichev 		bytes = stats->bytes;
5428be1bd91SStanislav Fomichev 		packets = stats->packets;
54367dc4eb5SJakub Kicinski 		alloc_fail = stats->rx.alloc_failed;
54467dc4eb5SJakub Kicinski 		csum_complete = stats->rx.csum_complete;
54567dc4eb5SJakub Kicinski 		csum_none = stats->rx.csum_none;
5468be1bd91SStanislav Fomichev 	} while (u64_stats_fetch_retry(&stats->syncp, start));
5478be1bd91SStanislav Fomichev 
5488be1bd91SStanislav Fomichev 	rx->bytes = bytes;
5498be1bd91SStanislav Fomichev 	rx->packets = packets;
55067dc4eb5SJakub Kicinski 	rx->alloc_fail = alloc_fail;
55167dc4eb5SJakub Kicinski 	rx->csum_complete = csum_complete;
55267dc4eb5SJakub Kicinski 	rx->csum_none = csum_none;
5538f20a2bfSMohsin Bashir 
5548f20a2bfSMohsin Bashir 	fbnic_get_hw_q_stats(fbd, fbd->hw_stats.hw_q);
5558f20a2bfSMohsin Bashir 
5568f20a2bfSMohsin Bashir 	spin_lock(&fbd->hw_stats_lock);
5578f20a2bfSMohsin Bashir 	rx->hw_drop_overruns = fbd->hw_stats.hw_q[idx].rde_pkt_cq_drop.value +
5588f20a2bfSMohsin Bashir 			       fbd->hw_stats.hw_q[idx].rde_pkt_bdq_drop.value;
5598f20a2bfSMohsin Bashir 	rx->hw_drops = fbd->hw_stats.hw_q[idx].rde_pkt_err.value +
5608f20a2bfSMohsin Bashir 		       rx->hw_drop_overruns;
5618f20a2bfSMohsin Bashir 	spin_unlock(&fbd->hw_stats_lock);
5628be1bd91SStanislav Fomichev }
5638be1bd91SStanislav Fomichev 
fbnic_get_queue_stats_tx(struct net_device * dev,int idx,struct netdev_queue_stats_tx * tx)5648be1bd91SStanislav Fomichev static void fbnic_get_queue_stats_tx(struct net_device *dev, int idx,
5658be1bd91SStanislav Fomichev 				     struct netdev_queue_stats_tx *tx)
5668be1bd91SStanislav Fomichev {
5678be1bd91SStanislav Fomichev 	struct fbnic_net *fbn = netdev_priv(dev);
5688be1bd91SStanislav Fomichev 	struct fbnic_ring *txr = fbn->tx[idx];
5698be1bd91SStanislav Fomichev 	struct fbnic_queue_stats *stats;
570b0b0f520SJakub Kicinski 	u64 stop, wake, csum, lso;
5718be1bd91SStanislav Fomichev 	unsigned int start;
5728be1bd91SStanislav Fomichev 	u64 bytes, packets;
5738be1bd91SStanislav Fomichev 
5748be1bd91SStanislav Fomichev 	if (!txr)
5758be1bd91SStanislav Fomichev 		return;
5768be1bd91SStanislav Fomichev 
5778be1bd91SStanislav Fomichev 	stats = &txr->stats;
5788be1bd91SStanislav Fomichev 	do {
5798be1bd91SStanislav Fomichev 		start = u64_stats_fetch_begin(&stats->syncp);
5808be1bd91SStanislav Fomichev 		bytes = stats->bytes;
5818be1bd91SStanislav Fomichev 		packets = stats->packets;
5821e07e361SJakub Kicinski 		csum = stats->twq.csum_partial;
583b0b0f520SJakub Kicinski 		lso = stats->twq.lso;
5841e07e361SJakub Kicinski 		stop = stats->twq.stop;
5851e07e361SJakub Kicinski 		wake = stats->twq.wake;
5868be1bd91SStanislav Fomichev 	} while (u64_stats_fetch_retry(&stats->syncp, start));
5878be1bd91SStanislav Fomichev 
5888be1bd91SStanislav Fomichev 	tx->bytes = bytes;
5898be1bd91SStanislav Fomichev 	tx->packets = packets;
590b0b0f520SJakub Kicinski 	tx->needs_csum = csum + lso;
591b0b0f520SJakub Kicinski 	tx->hw_gso_wire_packets = lso;
5921e07e361SJakub Kicinski 	tx->stop = stop;
5931e07e361SJakub Kicinski 	tx->wake = wake;
5948be1bd91SStanislav Fomichev }
5958be1bd91SStanislav Fomichev 
fbnic_get_base_stats(struct net_device * dev,struct netdev_queue_stats_rx * rx,struct netdev_queue_stats_tx * tx)5968be1bd91SStanislav Fomichev static void fbnic_get_base_stats(struct net_device *dev,
5978be1bd91SStanislav Fomichev 				 struct netdev_queue_stats_rx *rx,
5988be1bd91SStanislav Fomichev 				 struct netdev_queue_stats_tx *tx)
5998be1bd91SStanislav Fomichev {
6008be1bd91SStanislav Fomichev 	struct fbnic_net *fbn = netdev_priv(dev);
6018be1bd91SStanislav Fomichev 
6028be1bd91SStanislav Fomichev 	tx->bytes = fbn->tx_stats.bytes;
6038be1bd91SStanislav Fomichev 	tx->packets = fbn->tx_stats.packets;
604b0b0f520SJakub Kicinski 	tx->needs_csum = fbn->tx_stats.twq.csum_partial + fbn->tx_stats.twq.lso;
605b0b0f520SJakub Kicinski 	tx->hw_gso_wire_packets = fbn->tx_stats.twq.lso;
6061e07e361SJakub Kicinski 	tx->stop = fbn->tx_stats.twq.stop;
6071e07e361SJakub Kicinski 	tx->wake = fbn->tx_stats.twq.wake;
6088be1bd91SStanislav Fomichev 
6098be1bd91SStanislav Fomichev 	rx->bytes = fbn->rx_stats.bytes;
6108be1bd91SStanislav Fomichev 	rx->packets = fbn->rx_stats.packets;
61167dc4eb5SJakub Kicinski 	rx->alloc_fail = fbn->rx_stats.rx.alloc_failed;
61267dc4eb5SJakub Kicinski 	rx->csum_complete = fbn->rx_stats.rx.csum_complete;
61367dc4eb5SJakub Kicinski 	rx->csum_none = fbn->rx_stats.rx.csum_none;
6148be1bd91SStanislav Fomichev }
6158be1bd91SStanislav Fomichev 
6168be1bd91SStanislav Fomichev static const struct netdev_stat_ops fbnic_stat_ops = {
6178be1bd91SStanislav Fomichev 	.get_queue_stats_rx	= fbnic_get_queue_stats_rx,
6188be1bd91SStanislav Fomichev 	.get_queue_stats_tx	= fbnic_get_queue_stats_tx,
6198be1bd91SStanislav Fomichev 	.get_base_stats		= fbnic_get_base_stats,
6208be1bd91SStanislav Fomichev };
6218be1bd91SStanislav Fomichev 
fbnic_reset_queues(struct fbnic_net * fbn,unsigned int tx,unsigned int rx)622bc610777SAlexander Duyck void fbnic_reset_queues(struct fbnic_net *fbn,
623bc610777SAlexander Duyck 			unsigned int tx, unsigned int rx)
624bc610777SAlexander Duyck {
625bc610777SAlexander Duyck 	struct fbnic_dev *fbd = fbn->fbd;
626bc610777SAlexander Duyck 	unsigned int max_napis;
627bc610777SAlexander Duyck 
628bc610777SAlexander Duyck 	max_napis = fbd->num_irqs - FBNIC_NON_NAPI_VECTORS;
629bc610777SAlexander Duyck 
630bc610777SAlexander Duyck 	tx = min(tx, max_napis);
631bc610777SAlexander Duyck 	fbn->num_tx_queues = tx;
632bc610777SAlexander Duyck 
633bc610777SAlexander Duyck 	rx = min(rx, max_napis);
634bc610777SAlexander Duyck 	fbn->num_rx_queues = rx;
635bc610777SAlexander Duyck 
636bc610777SAlexander Duyck 	fbn->num_napi = max(tx, rx);
637bc610777SAlexander Duyck }
638bc610777SAlexander Duyck 
639bc610777SAlexander Duyck /**
640bc610777SAlexander Duyck  * fbnic_netdev_free - Free the netdev associate with fbnic
641bc610777SAlexander Duyck  * @fbd: Driver specific structure to free netdev from
642bc610777SAlexander Duyck  *
643bc610777SAlexander Duyck  * Allocate and initialize the netdev and netdev private structure. Bind
644bc610777SAlexander Duyck  * together the hardware, netdev, and pci data structures.
645bc610777SAlexander Duyck  **/
fbnic_netdev_free(struct fbnic_dev * fbd)646bc610777SAlexander Duyck void fbnic_netdev_free(struct fbnic_dev *fbd)
647bc610777SAlexander Duyck {
64869684376SAlexander Duyck 	struct fbnic_net *fbn = netdev_priv(fbd->netdev);
64969684376SAlexander Duyck 
65069684376SAlexander Duyck 	if (fbn->phylink)
65169684376SAlexander Duyck 		phylink_destroy(fbn->phylink);
65269684376SAlexander Duyck 
653bc610777SAlexander Duyck 	free_netdev(fbd->netdev);
654bc610777SAlexander Duyck 	fbd->netdev = NULL;
655bc610777SAlexander Duyck }
656bc610777SAlexander Duyck 
657bc610777SAlexander Duyck /**
658bc610777SAlexander Duyck  * fbnic_netdev_alloc - Allocate a netdev and associate with fbnic
659bc610777SAlexander Duyck  * @fbd: Driver specific structure to associate netdev with
660bc610777SAlexander Duyck  *
661bc610777SAlexander Duyck  * Allocate and initialize the netdev and netdev private structure. Bind
662bc610777SAlexander Duyck  * together the hardware, netdev, and pci data structures.
663bc610777SAlexander Duyck  *
66426aa7992SMohsin Bashir  *  Return: Pointer to net_device on success, NULL on failure
665bc610777SAlexander Duyck  **/
fbnic_netdev_alloc(struct fbnic_dev * fbd)666bc610777SAlexander Duyck struct net_device *fbnic_netdev_alloc(struct fbnic_dev *fbd)
667bc610777SAlexander Duyck {
668bc610777SAlexander Duyck 	struct net_device *netdev;
669bc610777SAlexander Duyck 	struct fbnic_net *fbn;
670bc610777SAlexander Duyck 	int default_queues;
671bc610777SAlexander Duyck 
672bc610777SAlexander Duyck 	netdev = alloc_etherdev_mq(sizeof(*fbn), FBNIC_MAX_RXQS);
673bc610777SAlexander Duyck 	if (!netdev)
674bc610777SAlexander Duyck 		return NULL;
675bc610777SAlexander Duyck 
676bc610777SAlexander Duyck 	SET_NETDEV_DEV(netdev, fbd->dev);
677bc610777SAlexander Duyck 	fbd->netdev = netdev;
678bc610777SAlexander Duyck 
679bc610777SAlexander Duyck 	netdev->netdev_ops = &fbnic_netdev_ops;
6808be1bd91SStanislav Fomichev 	netdev->stat_ops = &fbnic_stat_ops;
681bc610777SAlexander Duyck 
682bd2557a5SMohsin Bashir 	fbnic_set_ethtool_ops(netdev);
683bd2557a5SMohsin Bashir 
684bc610777SAlexander Duyck 	fbn = netdev_priv(netdev);
685bc610777SAlexander Duyck 
686bc610777SAlexander Duyck 	fbn->netdev = netdev;
687bc610777SAlexander Duyck 	fbn->fbd = fbd;
688bc610777SAlexander Duyck 
68940bf06a1SAlexander Duyck 	fbn->txq_size = FBNIC_TXQ_SIZE_DEFAULT;
6900cb4c0a1SAlexander Duyck 	fbn->hpq_size = FBNIC_HPQ_SIZE_DEFAULT;
6910cb4c0a1SAlexander Duyck 	fbn->ppq_size = FBNIC_PPQ_SIZE_DEFAULT;
6920cb4c0a1SAlexander Duyck 	fbn->rcq_size = FBNIC_RCQ_SIZE_DEFAULT;
69340bf06a1SAlexander Duyck 
6947b5b7a59SMohsin Bashir 	fbn->tx_usecs = FBNIC_TX_USECS_DEFAULT;
6957b5b7a59SMohsin Bashir 	fbn->rx_usecs = FBNIC_RX_USECS_DEFAULT;
6967b5b7a59SMohsin Bashir 	fbn->rx_max_frames = FBNIC_RX_FRAMES_DEFAULT;
6977b5b7a59SMohsin Bashir 
698bc610777SAlexander Duyck 	default_queues = netif_get_num_default_rss_queues();
699bc610777SAlexander Duyck 	if (default_queues > fbd->max_num_queues)
700bc610777SAlexander Duyck 		default_queues = fbd->max_num_queues;
701bc610777SAlexander Duyck 
702bc610777SAlexander Duyck 	fbnic_reset_queues(fbn, default_queues, default_queues);
703bc610777SAlexander Duyck 
704355440a6SAlexander Duyck 	fbnic_reset_indir_tbl(fbn);
705355440a6SAlexander Duyck 	fbnic_rss_key_fill(fbn->rss_key);
706355440a6SAlexander Duyck 	fbnic_rss_init_en_mask(fbn);
707355440a6SAlexander Duyck 
70809717c28SAlexander Duyck 	netdev->priv_flags |= IFF_UNICAST_FLT;
70909717c28SAlexander Duyck 
710b0b0f520SJakub Kicinski 	netdev->gso_partial_features =
711b0b0f520SJakub Kicinski 		NETIF_F_GSO_GRE |
712b0b0f520SJakub Kicinski 		NETIF_F_GSO_GRE_CSUM |
713b0b0f520SJakub Kicinski 		NETIF_F_GSO_IPXIP4 |
714b0b0f520SJakub Kicinski 		NETIF_F_GSO_UDP_TUNNEL |
715b0b0f520SJakub Kicinski 		NETIF_F_GSO_UDP_TUNNEL_CSUM;
716b0b0f520SJakub Kicinski 
7179a57bacdSAlexander Duyck 	netdev->features |=
718b0b0f520SJakub Kicinski 		netdev->gso_partial_features |
719b0b0f520SJakub Kicinski 		FBNIC_TUN_GSO_FEATURES |
720a29b8eb6SAlexander Duyck 		NETIF_F_RXHASH |
7219a57bacdSAlexander Duyck 		NETIF_F_SG |
722a29b8eb6SAlexander Duyck 		NETIF_F_HW_CSUM |
723b0b0f520SJakub Kicinski 		NETIF_F_RXCSUM |
724b0b0f520SJakub Kicinski 		NETIF_F_TSO |
725b0b0f520SJakub Kicinski 		NETIF_F_TSO_ECN |
726b0b0f520SJakub Kicinski 		NETIF_F_TSO6 |
727b0b0f520SJakub Kicinski 		NETIF_F_GSO_PARTIAL |
728b0b0f520SJakub Kicinski 		NETIF_F_GSO_UDP_L4;
7299a57bacdSAlexander Duyck 
7309a57bacdSAlexander Duyck 	netdev->hw_features |= netdev->features;
7319a57bacdSAlexander Duyck 	netdev->vlan_features |= netdev->features;
7329a57bacdSAlexander Duyck 	netdev->hw_enc_features |= netdev->features;
73322300354SAlexander Duyck 	netdev->features |= NETIF_F_NTUPLE;
7349a57bacdSAlexander Duyck 
735bc610777SAlexander Duyck 	netdev->min_mtu = IPV6_MIN_MTU;
736bc610777SAlexander Duyck 	netdev->max_mtu = FBNIC_MAX_JUMBO_FRAME_SIZE - ETH_HLEN;
737bc610777SAlexander Duyck 
73869684376SAlexander Duyck 	/* TBD: This is workaround for BMC as phylink doesn't have support
73969684376SAlexander Duyck 	 * for leavling the link enabled if a BMC is present.
74069684376SAlexander Duyck 	 */
74169684376SAlexander Duyck 	netdev->ethtool->wol_enabled = true;
74269684376SAlexander Duyck 
743bc610777SAlexander Duyck 	netif_carrier_off(netdev);
744bc610777SAlexander Duyck 
745bc610777SAlexander Duyck 	netif_tx_stop_all_queues(netdev);
746bc610777SAlexander Duyck 
74769684376SAlexander Duyck 	if (fbnic_phylink_init(netdev)) {
74869684376SAlexander Duyck 		fbnic_netdev_free(fbd);
74969684376SAlexander Duyck 		return NULL;
75069684376SAlexander Duyck 	}
75169684376SAlexander Duyck 
752bc610777SAlexander Duyck 	return netdev;
753bc610777SAlexander Duyck }
754bc610777SAlexander Duyck 
fbnic_dsn_to_mac_addr(u64 dsn,char * addr)755bc610777SAlexander Duyck static int fbnic_dsn_to_mac_addr(u64 dsn, char *addr)
756bc610777SAlexander Duyck {
757bc610777SAlexander Duyck 	addr[0] = (dsn >> 56) & 0xFF;
758bc610777SAlexander Duyck 	addr[1] = (dsn >> 48) & 0xFF;
759bc610777SAlexander Duyck 	addr[2] = (dsn >> 40) & 0xFF;
760bc610777SAlexander Duyck 	addr[3] = (dsn >> 16) & 0xFF;
761bc610777SAlexander Duyck 	addr[4] = (dsn >> 8) & 0xFF;
762bc610777SAlexander Duyck 	addr[5] = dsn & 0xFF;
763bc610777SAlexander Duyck 
764bc610777SAlexander Duyck 	return is_valid_ether_addr(addr) ? 0 : -EINVAL;
765bc610777SAlexander Duyck }
766bc610777SAlexander Duyck 
767bc610777SAlexander Duyck /**
768bc610777SAlexander Duyck  * fbnic_netdev_register - Initialize general software structures
769bc610777SAlexander Duyck  * @netdev: Netdev containing structure to initialize and register
770bc610777SAlexander Duyck  *
771bc610777SAlexander Duyck  * Initialize the MAC address for the netdev and register it.
772bc610777SAlexander Duyck  *
773bc610777SAlexander Duyck  *  Return: 0 on success, negative on failure
774bc610777SAlexander Duyck  **/
fbnic_netdev_register(struct net_device * netdev)775bc610777SAlexander Duyck int fbnic_netdev_register(struct net_device *netdev)
776bc610777SAlexander Duyck {
777bc610777SAlexander Duyck 	struct fbnic_net *fbn = netdev_priv(netdev);
778bc610777SAlexander Duyck 	struct fbnic_dev *fbd = fbn->fbd;
779bc610777SAlexander Duyck 	u64 dsn = fbd->dsn;
780bc610777SAlexander Duyck 	u8 addr[ETH_ALEN];
781bc610777SAlexander Duyck 	int err;
782bc610777SAlexander Duyck 
783bc610777SAlexander Duyck 	err = fbnic_dsn_to_mac_addr(dsn, addr);
784bc610777SAlexander Duyck 	if (!err) {
785bc610777SAlexander Duyck 		ether_addr_copy(netdev->perm_addr, addr);
786bc610777SAlexander Duyck 		eth_hw_addr_set(netdev, addr);
787bc610777SAlexander Duyck 	} else {
788bc610777SAlexander Duyck 		/* A randomly assigned MAC address will cause provisioning
789bc610777SAlexander Duyck 		 * issues so instead just fail to spawn the netdev and
790bc610777SAlexander Duyck 		 * avoid any confusion.
791bc610777SAlexander Duyck 		 */
792bc610777SAlexander Duyck 		dev_err(fbd->dev, "MAC addr %pM invalid\n", addr);
793bc610777SAlexander Duyck 		return err;
794bc610777SAlexander Duyck 	}
795bc610777SAlexander Duyck 
796bc610777SAlexander Duyck 	return register_netdev(netdev);
797bc610777SAlexander Duyck }
798bc610777SAlexander Duyck 
fbnic_netdev_unregister(struct net_device * netdev)799bc610777SAlexander Duyck void fbnic_netdev_unregister(struct net_device *netdev)
800bc610777SAlexander Duyck {
801bc610777SAlexander Duyck 	unregister_netdev(netdev);
802bc610777SAlexander Duyck }
803