xref: /linux/drivers/net/ethernet/meta/fbnic/fbnic_ethtool.c (revision f2c53ea949c5048f96b3dbb5a5ee7131ce4ff2de)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) Meta Platforms, Inc. and affiliates. */
3 
4 #include <linux/ethtool.h>
5 #include <linux/ethtool_netlink.h>
6 #include <linux/netdevice.h>
7 #include <linux/pci.h>
8 #include <net/ipv6.h>
9 
10 #include "fbnic.h"
11 #include "fbnic_netdev.h"
12 #include "fbnic_tlv.h"
13 
14 struct fbnic_stat {
15 	u8 string[ETH_GSTRING_LEN];
16 	unsigned int size;
17 	unsigned int offset;
18 };
19 
20 #define FBNIC_STAT_FIELDS(type, name, stat) { \
21 	.string = name, \
22 	.size = sizeof_field(struct type, stat), \
23 	.offset = offsetof(struct type, stat), \
24 }
25 
26 /* Hardware statistics not captured in rtnl_link_stats */
27 #define FBNIC_HW_STAT(name, stat) \
28 	FBNIC_STAT_FIELDS(fbnic_hw_stats, name, stat)
29 
30 static const struct fbnic_stat fbnic_gstrings_hw_stats[] = {
31 	/* TTI */
32 	FBNIC_HW_STAT("tti_cm_drop_frames", tti.cm_drop.frames),
33 	FBNIC_HW_STAT("tti_cm_drop_bytes", tti.cm_drop.bytes),
34 	FBNIC_HW_STAT("tti_frame_drop_frames", tti.frame_drop.frames),
35 	FBNIC_HW_STAT("tti_frame_drop_bytes", tti.frame_drop.bytes),
36 	FBNIC_HW_STAT("tti_tbi_drop_frames", tti.tbi_drop.frames),
37 	FBNIC_HW_STAT("tti_tbi_drop_bytes", tti.tbi_drop.bytes),
38 
39 	/* TMI */
40 	FBNIC_HW_STAT("ptp_illegal_req", tmi.ptp_illegal_req),
41 	FBNIC_HW_STAT("ptp_good_ts", tmi.ptp_good_ts),
42 	FBNIC_HW_STAT("ptp_bad_ts", tmi.ptp_bad_ts),
43 
44 	/* RPC */
45 	FBNIC_HW_STAT("rpc_unkn_etype", rpc.unkn_etype),
46 	FBNIC_HW_STAT("rpc_unkn_ext_hdr", rpc.unkn_ext_hdr),
47 	FBNIC_HW_STAT("rpc_ipv4_frag", rpc.ipv4_frag),
48 	FBNIC_HW_STAT("rpc_ipv6_frag", rpc.ipv6_frag),
49 	FBNIC_HW_STAT("rpc_ipv4_esp", rpc.ipv4_esp),
50 	FBNIC_HW_STAT("rpc_ipv6_esp", rpc.ipv6_esp),
51 	FBNIC_HW_STAT("rpc_tcp_opt_err", rpc.tcp_opt_err),
52 	FBNIC_HW_STAT("rpc_out_of_hdr_err", rpc.out_of_hdr_err),
53 };
54 
55 #define FBNIC_HW_FIXED_STATS_LEN ARRAY_SIZE(fbnic_gstrings_hw_stats)
56 
57 #define FBNIC_RXB_ENQUEUE_STAT(name, stat) \
58 	FBNIC_STAT_FIELDS(fbnic_rxb_enqueue_stats, name, stat)
59 
60 static const struct fbnic_stat fbnic_gstrings_rxb_enqueue_stats[] = {
61 	FBNIC_RXB_ENQUEUE_STAT("rxb_integrity_err%u", integrity_err),
62 	FBNIC_RXB_ENQUEUE_STAT("rxb_mac_err%u", mac_err),
63 	FBNIC_RXB_ENQUEUE_STAT("rxb_parser_err%u", parser_err),
64 	FBNIC_RXB_ENQUEUE_STAT("rxb_frm_err%u", frm_err),
65 
66 	FBNIC_RXB_ENQUEUE_STAT("rxb_drbo%u_frames", drbo.frames),
67 	FBNIC_RXB_ENQUEUE_STAT("rxb_drbo%u_bytes", drbo.bytes),
68 };
69 
70 #define FBNIC_HW_RXB_ENQUEUE_STATS_LEN \
71 	ARRAY_SIZE(fbnic_gstrings_rxb_enqueue_stats)
72 
73 #define FBNIC_RXB_FIFO_STAT(name, stat) \
74 	FBNIC_STAT_FIELDS(fbnic_rxb_fifo_stats, name, stat)
75 
76 static const struct fbnic_stat fbnic_gstrings_rxb_fifo_stats[] = {
77 	FBNIC_RXB_FIFO_STAT("rxb_fifo%u_drop", trans_drop),
78 	FBNIC_RXB_FIFO_STAT("rxb_fifo%u_dropped_frames", drop.frames),
79 	FBNIC_RXB_FIFO_STAT("rxb_fifo%u_ecn", trans_ecn),
80 	FBNIC_RXB_FIFO_STAT("rxb_fifo%u_level", level),
81 };
82 
83 #define FBNIC_HW_RXB_FIFO_STATS_LEN ARRAY_SIZE(fbnic_gstrings_rxb_fifo_stats)
84 
85 #define FBNIC_RXB_DEQUEUE_STAT(name, stat) \
86 	FBNIC_STAT_FIELDS(fbnic_rxb_dequeue_stats, name, stat)
87 
88 static const struct fbnic_stat fbnic_gstrings_rxb_dequeue_stats[] = {
89 	FBNIC_RXB_DEQUEUE_STAT("rxb_intf%u_frames", intf.frames),
90 	FBNIC_RXB_DEQUEUE_STAT("rxb_intf%u_bytes", intf.bytes),
91 	FBNIC_RXB_DEQUEUE_STAT("rxb_pbuf%u_frames", pbuf.frames),
92 	FBNIC_RXB_DEQUEUE_STAT("rxb_pbuf%u_bytes", pbuf.bytes),
93 };
94 
95 #define FBNIC_HW_RXB_DEQUEUE_STATS_LEN \
96 	ARRAY_SIZE(fbnic_gstrings_rxb_dequeue_stats)
97 
98 #define FBNIC_HW_Q_STAT(name, stat) \
99 	FBNIC_STAT_FIELDS(fbnic_hw_q_stats, name, stat.value)
100 
101 static const struct fbnic_stat fbnic_gstrings_hw_q_stats[] = {
102 	FBNIC_HW_Q_STAT("rde_%u_pkt_err", rde_pkt_err),
103 	FBNIC_HW_Q_STAT("rde_%u_pkt_cq_drop", rde_pkt_cq_drop),
104 	FBNIC_HW_Q_STAT("rde_%u_pkt_bdq_drop", rde_pkt_bdq_drop),
105 };
106 
107 #define FBNIC_HW_Q_STATS_LEN ARRAY_SIZE(fbnic_gstrings_hw_q_stats)
108 #define FBNIC_HW_STATS_LEN \
109 	(FBNIC_HW_FIXED_STATS_LEN + \
110 	 FBNIC_HW_RXB_ENQUEUE_STATS_LEN * FBNIC_RXB_ENQUEUE_INDICES + \
111 	 FBNIC_HW_RXB_FIFO_STATS_LEN * FBNIC_RXB_FIFO_INDICES + \
112 	 FBNIC_HW_RXB_DEQUEUE_STATS_LEN * FBNIC_RXB_DEQUEUE_INDICES + \
113 	 FBNIC_HW_Q_STATS_LEN * FBNIC_MAX_QUEUES)
114 
115 #define FBNIC_QUEUE_STAT(name, stat) \
116 	FBNIC_STAT_FIELDS(fbnic_ring, name, stat)
117 
118 static const struct fbnic_stat fbnic_gstrings_xdp_stats[] = {
119 	FBNIC_QUEUE_STAT("xdp_tx_queue_%u_packets", stats.packets),
120 	FBNIC_QUEUE_STAT("xdp_tx_queue_%u_bytes", stats.bytes),
121 	FBNIC_QUEUE_STAT("xdp_tx_queue_%u_dropped", stats.dropped),
122 };
123 
124 #define FBNIC_XDP_STATS_LEN ARRAY_SIZE(fbnic_gstrings_xdp_stats)
125 
126 #define FBNIC_STATS_LEN \
127 	(FBNIC_HW_STATS_LEN + FBNIC_XDP_STATS_LEN * FBNIC_MAX_XDPQS)
128 
129 enum fbnic_self_test_results {
130 	TEST_REG = 0,
131 	TEST_MSIX,
132 	TEST_MBX,
133 };
134 
135 static const char fbnic_gstrings_self_test[][ETH_GSTRING_LEN] = {
136 	[TEST_REG]	= "Register test (offline)",
137 	[TEST_MSIX]	= "MSI-X Interrupt test (offline)",
138 	[TEST_MBX]      = "FW mailbox test (on/offline)",
139 };
140 
141 #define FBNIC_TEST_LEN ARRAY_SIZE(fbnic_gstrings_self_test)
142 
143 static void
fbnic_get_drvinfo(struct net_device * netdev,struct ethtool_drvinfo * drvinfo)144 fbnic_get_drvinfo(struct net_device *netdev, struct ethtool_drvinfo *drvinfo)
145 {
146 	struct fbnic_net *fbn = netdev_priv(netdev);
147 	struct fbnic_dev *fbd = fbn->fbd;
148 
149 	fbnic_get_fw_ver_commit_str(fbd, drvinfo->fw_version,
150 				    sizeof(drvinfo->fw_version));
151 }
152 
fbnic_get_regs_len(struct net_device * netdev)153 static int fbnic_get_regs_len(struct net_device *netdev)
154 {
155 	struct fbnic_net *fbn = netdev_priv(netdev);
156 
157 	return fbnic_csr_regs_len(fbn->fbd) * sizeof(u32);
158 }
159 
fbnic_get_regs(struct net_device * netdev,struct ethtool_regs * regs,void * data)160 static void fbnic_get_regs(struct net_device *netdev,
161 			   struct ethtool_regs *regs, void *data)
162 {
163 	struct fbnic_net *fbn = netdev_priv(netdev);
164 
165 	fbnic_csr_get_regs(fbn->fbd, data, &regs->version);
166 }
167 
fbnic_clone_create(struct fbnic_net * orig)168 static struct fbnic_net *fbnic_clone_create(struct fbnic_net *orig)
169 {
170 	struct fbnic_net *clone;
171 
172 	clone = kmemdup(orig, sizeof(*orig), GFP_KERNEL);
173 	if (!clone)
174 		return NULL;
175 
176 	memset(clone->tx, 0, sizeof(clone->tx));
177 	memset(clone->rx, 0, sizeof(clone->rx));
178 	memset(clone->napi, 0, sizeof(clone->napi));
179 	return clone;
180 }
181 
fbnic_clone_swap_cfg(struct fbnic_net * orig,struct fbnic_net * clone)182 static void fbnic_clone_swap_cfg(struct fbnic_net *orig,
183 				 struct fbnic_net *clone)
184 {
185 	swap(clone->rcq_size, orig->rcq_size);
186 	swap(clone->hpq_size, orig->hpq_size);
187 	swap(clone->ppq_size, orig->ppq_size);
188 	swap(clone->txq_size, orig->txq_size);
189 	swap(clone->num_rx_queues, orig->num_rx_queues);
190 	swap(clone->num_tx_queues, orig->num_tx_queues);
191 	swap(clone->num_napi, orig->num_napi);
192 	swap(clone->hds_thresh, orig->hds_thresh);
193 }
194 
fbnic_aggregate_vector_counters(struct fbnic_net * fbn,struct fbnic_napi_vector * nv)195 static void fbnic_aggregate_vector_counters(struct fbnic_net *fbn,
196 					    struct fbnic_napi_vector *nv)
197 {
198 	int i, j;
199 
200 	for (i = 0; i < nv->txt_count; i++) {
201 		fbnic_aggregate_ring_tx_counters(fbn, &nv->qt[i].sub0);
202 		fbnic_aggregate_ring_xdp_counters(fbn, &nv->qt[i].sub1);
203 		fbnic_aggregate_ring_tx_counters(fbn, &nv->qt[i].cmpl);
204 	}
205 
206 	for (j = 0; j < nv->rxt_count; j++, i++) {
207 		fbnic_aggregate_ring_bdq_counters(fbn, &nv->qt[i].sub0);
208 		fbnic_aggregate_ring_bdq_counters(fbn, &nv->qt[i].sub1);
209 		fbnic_aggregate_ring_rx_counters(fbn, &nv->qt[i].cmpl);
210 	}
211 }
212 
fbnic_clone_swap(struct fbnic_net * orig,struct fbnic_net * clone)213 static void fbnic_clone_swap(struct fbnic_net *orig,
214 			     struct fbnic_net *clone)
215 {
216 	struct fbnic_dev *fbd = orig->fbd;
217 	unsigned int i;
218 
219 	for (i = 0; i < max(clone->num_napi, orig->num_napi); i++)
220 		fbnic_synchronize_irq(fbd, FBNIC_NON_NAPI_VECTORS + i);
221 	for (i = 0; i < orig->num_napi; i++)
222 		fbnic_aggregate_vector_counters(orig, orig->napi[i]);
223 
224 	fbnic_clone_swap_cfg(orig, clone);
225 
226 	for (i = 0; i < ARRAY_SIZE(orig->napi); i++)
227 		swap(clone->napi[i], orig->napi[i]);
228 	for (i = 0; i < ARRAY_SIZE(orig->tx); i++)
229 		swap(clone->tx[i], orig->tx[i]);
230 	for (i = 0; i < ARRAY_SIZE(orig->rx); i++)
231 		swap(clone->rx[i], orig->rx[i]);
232 }
233 
fbnic_clone_free(struct fbnic_net * clone)234 static void fbnic_clone_free(struct fbnic_net *clone)
235 {
236 	kfree(clone);
237 }
238 
fbnic_get_coalesce(struct net_device * netdev,struct ethtool_coalesce * ec,struct kernel_ethtool_coalesce * kernel_coal,struct netlink_ext_ack * extack)239 static int fbnic_get_coalesce(struct net_device *netdev,
240 			      struct ethtool_coalesce *ec,
241 			      struct kernel_ethtool_coalesce *kernel_coal,
242 			      struct netlink_ext_ack *extack)
243 {
244 	struct fbnic_net *fbn = netdev_priv(netdev);
245 
246 	ec->tx_coalesce_usecs = fbn->tx_usecs;
247 	ec->rx_coalesce_usecs = fbn->rx_usecs;
248 	ec->rx_max_coalesced_frames = fbn->rx_max_frames;
249 
250 	return 0;
251 }
252 
fbnic_set_coalesce(struct net_device * netdev,struct ethtool_coalesce * ec,struct kernel_ethtool_coalesce * kernel_coal,struct netlink_ext_ack * extack)253 static int fbnic_set_coalesce(struct net_device *netdev,
254 			      struct ethtool_coalesce *ec,
255 			      struct kernel_ethtool_coalesce *kernel_coal,
256 			      struct netlink_ext_ack *extack)
257 {
258 	struct fbnic_net *fbn = netdev_priv(netdev);
259 
260 	/* Verify against hardware limits */
261 	if (ec->rx_coalesce_usecs > FIELD_MAX(FBNIC_INTR_CQ_REARM_RCQ_TIMEOUT)) {
262 		NL_SET_ERR_MSG_MOD(extack, "rx_usecs is above device max");
263 		return -EINVAL;
264 	}
265 	if (ec->tx_coalesce_usecs > FIELD_MAX(FBNIC_INTR_CQ_REARM_TCQ_TIMEOUT)) {
266 		NL_SET_ERR_MSG_MOD(extack, "tx_usecs is above device max");
267 		return -EINVAL;
268 	}
269 	if (ec->rx_max_coalesced_frames >
270 	    FIELD_MAX(FBNIC_QUEUE_RIM_THRESHOLD_RCD_MASK) /
271 	    FBNIC_MIN_RXD_PER_FRAME) {
272 		NL_SET_ERR_MSG_MOD(extack, "rx_frames is above device max");
273 		return -EINVAL;
274 	}
275 
276 	fbn->tx_usecs = ec->tx_coalesce_usecs;
277 	fbn->rx_usecs = ec->rx_coalesce_usecs;
278 	fbn->rx_max_frames = ec->rx_max_coalesced_frames;
279 
280 	if (netif_running(netdev)) {
281 		int i;
282 
283 		for (i = 0; i < fbn->num_napi; i++) {
284 			struct fbnic_napi_vector *nv = fbn->napi[i];
285 
286 			fbnic_config_txrx_usecs(nv, 0);
287 			fbnic_config_rx_frames(nv);
288 		}
289 	}
290 
291 	return 0;
292 }
293 
294 static void
fbnic_get_ringparam(struct net_device * netdev,struct ethtool_ringparam * ring,struct kernel_ethtool_ringparam * kernel_ring,struct netlink_ext_ack * extack)295 fbnic_get_ringparam(struct net_device *netdev, struct ethtool_ringparam *ring,
296 		    struct kernel_ethtool_ringparam *kernel_ring,
297 		    struct netlink_ext_ack *extack)
298 {
299 	struct fbnic_net *fbn = netdev_priv(netdev);
300 
301 	ring->rx_max_pending = FBNIC_QUEUE_SIZE_MAX;
302 	ring->rx_mini_max_pending = FBNIC_QUEUE_SIZE_MAX;
303 	ring->rx_jumbo_max_pending = FBNIC_QUEUE_SIZE_MAX;
304 	ring->tx_max_pending = FBNIC_QUEUE_SIZE_MAX;
305 
306 	ring->rx_pending = fbn->rcq_size;
307 	ring->rx_mini_pending = fbn->hpq_size;
308 	ring->rx_jumbo_pending = fbn->ppq_size;
309 	ring->tx_pending = fbn->txq_size;
310 
311 	kernel_ring->tcp_data_split = ETHTOOL_TCP_DATA_SPLIT_ENABLED;
312 	kernel_ring->hds_thresh_max = FBNIC_HDS_THRESH_MAX;
313 	kernel_ring->hds_thresh = fbn->hds_thresh;
314 }
315 
fbnic_ring_size_pow2(u32 size)316 static u32 fbnic_ring_size_pow2(u32 size)
317 {
318 	return size ? roundup_pow_of_two(size) : 0;
319 }
320 
fbnic_set_rings(struct fbnic_net * fbn,struct ethtool_ringparam * ring,struct kernel_ethtool_ringparam * kernel_ring)321 static void fbnic_set_rings(struct fbnic_net *fbn,
322 			    struct ethtool_ringparam *ring,
323 			    struct kernel_ethtool_ringparam *kernel_ring)
324 {
325 	fbn->rcq_size = ring->rx_pending;
326 	fbn->hpq_size = ring->rx_mini_pending;
327 	fbn->ppq_size = ring->rx_jumbo_pending;
328 	fbn->txq_size = ring->tx_pending;
329 	fbn->hds_thresh = kernel_ring->hds_thresh;
330 }
331 
332 static int
fbnic_set_ringparam(struct net_device * netdev,struct ethtool_ringparam * ring,struct kernel_ethtool_ringparam * kernel_ring,struct netlink_ext_ack * extack)333 fbnic_set_ringparam(struct net_device *netdev, struct ethtool_ringparam *ring,
334 		    struct kernel_ethtool_ringparam *kernel_ring,
335 		    struct netlink_ext_ack *extack)
336 
337 {
338 	struct fbnic_net *fbn = netdev_priv(netdev);
339 	struct fbnic_net *clone;
340 	int err;
341 
342 	ring->rx_pending	= fbnic_ring_size_pow2(ring->rx_pending);
343 	ring->rx_mini_pending	= fbnic_ring_size_pow2(ring->rx_mini_pending);
344 	ring->rx_jumbo_pending	= fbnic_ring_size_pow2(ring->rx_jumbo_pending);
345 	ring->tx_pending	= fbnic_ring_size_pow2(ring->tx_pending);
346 
347 	/* These are absolute minimums allowing the device and driver to operate
348 	 * but not necessarily guarantee reasonable performance. Settings below
349 	 * Rx queue size of 128 and BDQs smaller than 64 are likely suboptimal
350 	 * at best.
351 	 */
352 	if (ring->rx_pending < max(FBNIC_QUEUE_SIZE_MIN, FBNIC_RX_DESC_MIN) ||
353 	    ring->rx_mini_pending < FBNIC_QUEUE_SIZE_MIN ||
354 	    ring->rx_jumbo_pending < FBNIC_QUEUE_SIZE_MIN ||
355 	    ring->tx_pending < max(FBNIC_QUEUE_SIZE_MIN, FBNIC_TX_DESC_MIN)) {
356 		NL_SET_ERR_MSG_MOD(extack, "requested ring size too small");
357 		return -EINVAL;
358 	}
359 
360 	if (kernel_ring->tcp_data_split == ETHTOOL_TCP_DATA_SPLIT_DISABLED) {
361 		NL_SET_ERR_MSG_MOD(extack, "Cannot disable TCP data split");
362 		return -EINVAL;
363 	}
364 
365 	/* If an XDP program is attached, we should check for potential frame
366 	 * splitting. If the new HDS threshold can cause splitting, we should
367 	 * only allow if the attached XDP program can handle frags.
368 	 */
369 	if (fbnic_check_split_frames(fbn->xdp_prog, netdev->mtu,
370 				     kernel_ring->hds_thresh)) {
371 		NL_SET_ERR_MSG_MOD(extack,
372 				   "Use higher HDS threshold or multi-buf capable program");
373 		return -EINVAL;
374 	}
375 
376 	if (!netif_running(netdev)) {
377 		fbnic_set_rings(fbn, ring, kernel_ring);
378 		return 0;
379 	}
380 
381 	clone = fbnic_clone_create(fbn);
382 	if (!clone)
383 		return -ENOMEM;
384 
385 	fbnic_set_rings(clone, ring, kernel_ring);
386 
387 	err = fbnic_alloc_napi_vectors(clone);
388 	if (err)
389 		goto err_free_clone;
390 
391 	err = fbnic_alloc_resources(clone);
392 	if (err)
393 		goto err_free_napis;
394 
395 	fbnic_down_noidle(fbn);
396 	err = fbnic_wait_all_queues_idle(fbn->fbd, true);
397 	if (err)
398 		goto err_start_stack;
399 
400 	err = fbnic_set_netif_queues(clone);
401 	if (err)
402 		goto err_start_stack;
403 
404 	/* Nothing can fail past this point */
405 	fbnic_flush(fbn);
406 
407 	fbnic_clone_swap(fbn, clone);
408 
409 	fbnic_up(fbn);
410 
411 	fbnic_free_resources(clone);
412 	fbnic_free_napi_vectors(clone);
413 	fbnic_clone_free(clone);
414 
415 	return 0;
416 
417 err_start_stack:
418 	fbnic_flush(fbn);
419 	fbnic_up(fbn);
420 	fbnic_free_resources(clone);
421 err_free_napis:
422 	fbnic_free_napi_vectors(clone);
423 err_free_clone:
424 	fbnic_clone_free(clone);
425 	return err;
426 }
427 
fbnic_get_rxb_enqueue_strings(u8 ** data,unsigned int idx)428 static void fbnic_get_rxb_enqueue_strings(u8 **data, unsigned int idx)
429 {
430 	const struct fbnic_stat *stat;
431 	int i;
432 
433 	stat = fbnic_gstrings_rxb_enqueue_stats;
434 	for (i = 0; i < FBNIC_HW_RXB_ENQUEUE_STATS_LEN; i++, stat++)
435 		ethtool_sprintf(data, stat->string, idx);
436 }
437 
fbnic_get_rxb_fifo_strings(u8 ** data,unsigned int idx)438 static void fbnic_get_rxb_fifo_strings(u8 **data, unsigned int idx)
439 {
440 	const struct fbnic_stat *stat;
441 	int i;
442 
443 	stat = fbnic_gstrings_rxb_fifo_stats;
444 	for (i = 0; i < FBNIC_HW_RXB_FIFO_STATS_LEN; i++, stat++)
445 		ethtool_sprintf(data, stat->string, idx);
446 }
447 
fbnic_get_rxb_dequeue_strings(u8 ** data,unsigned int idx)448 static void fbnic_get_rxb_dequeue_strings(u8 **data, unsigned int idx)
449 {
450 	const struct fbnic_stat *stat;
451 	int i;
452 
453 	stat = fbnic_gstrings_rxb_dequeue_stats;
454 	for (i = 0; i < FBNIC_HW_RXB_DEQUEUE_STATS_LEN; i++, stat++)
455 		ethtool_sprintf(data, stat->string, idx);
456 }
457 
fbnic_get_xdp_queue_strings(u8 ** data,unsigned int idx)458 static void fbnic_get_xdp_queue_strings(u8 **data, unsigned int idx)
459 {
460 	const struct fbnic_stat *stat;
461 	int i;
462 
463 	stat = fbnic_gstrings_xdp_stats;
464 	for (i = 0; i < FBNIC_XDP_STATS_LEN; i++, stat++)
465 		ethtool_sprintf(data, stat->string, idx);
466 }
467 
fbnic_get_strings(struct net_device * dev,u32 sset,u8 * data)468 static void fbnic_get_strings(struct net_device *dev, u32 sset, u8 *data)
469 {
470 	const struct fbnic_stat *stat;
471 	int i, idx;
472 
473 	switch (sset) {
474 	case ETH_SS_STATS:
475 		for (i = 0; i < FBNIC_HW_FIXED_STATS_LEN; i++)
476 			ethtool_puts(&data, fbnic_gstrings_hw_stats[i].string);
477 
478 		for (i = 0; i < FBNIC_RXB_ENQUEUE_INDICES; i++)
479 			fbnic_get_rxb_enqueue_strings(&data, i);
480 
481 		for (i = 0; i < FBNIC_RXB_FIFO_INDICES; i++)
482 			fbnic_get_rxb_fifo_strings(&data, i);
483 
484 		for (i = 0; i < FBNIC_RXB_DEQUEUE_INDICES; i++)
485 			fbnic_get_rxb_dequeue_strings(&data, i);
486 
487 		for (idx = 0; idx < FBNIC_MAX_QUEUES; idx++) {
488 			stat = fbnic_gstrings_hw_q_stats;
489 
490 			for (i = 0; i < FBNIC_HW_Q_STATS_LEN; i++, stat++)
491 				ethtool_sprintf(&data, stat->string, idx);
492 		}
493 
494 		for (i = 0; i < FBNIC_MAX_XDPQS; i++)
495 			fbnic_get_xdp_queue_strings(&data, i);
496 		break;
497 	case ETH_SS_TEST:
498 		memcpy(data, fbnic_gstrings_self_test,
499 		       sizeof(fbnic_gstrings_self_test));
500 		break;
501 	}
502 }
503 
fbnic_report_hw_stats(const struct fbnic_stat * stat,const void * base,int len,u64 ** data)504 static void fbnic_report_hw_stats(const struct fbnic_stat *stat,
505 				  const void *base, int len, u64 **data)
506 {
507 	while (len--) {
508 		u8 *curr = (u8 *)base + stat->offset;
509 
510 		**data = *(u64 *)curr;
511 
512 		stat++;
513 		(*data)++;
514 	}
515 }
516 
fbnic_get_xdp_queue_stats(struct fbnic_ring * ring,u64 ** data)517 static void fbnic_get_xdp_queue_stats(struct fbnic_ring *ring, u64 **data)
518 {
519 	const struct fbnic_stat *stat;
520 	int i;
521 
522 	if (!ring) {
523 		*data += FBNIC_XDP_STATS_LEN;
524 		return;
525 	}
526 
527 	stat = fbnic_gstrings_xdp_stats;
528 	for (i = 0; i < FBNIC_XDP_STATS_LEN; i++, stat++, (*data)++) {
529 		u8 *p = (u8 *)ring + stat->offset;
530 
531 		**data = *(u64 *)p;
532 	}
533 }
534 
fbnic_get_ethtool_stats(struct net_device * dev,struct ethtool_stats * stats,u64 * data)535 static void fbnic_get_ethtool_stats(struct net_device *dev,
536 				    struct ethtool_stats *stats, u64 *data)
537 {
538 	struct fbnic_net *fbn = netdev_priv(dev);
539 	struct fbnic_dev *fbd = fbn->fbd;
540 	int i;
541 
542 	fbnic_get_hw_stats(fbn->fbd);
543 
544 	spin_lock(&fbd->hw_stats.lock);
545 	fbnic_report_hw_stats(fbnic_gstrings_hw_stats, &fbd->hw_stats,
546 			      FBNIC_HW_FIXED_STATS_LEN, &data);
547 
548 	for (i = 0; i < FBNIC_RXB_ENQUEUE_INDICES; i++) {
549 		const struct fbnic_rxb_enqueue_stats *enq;
550 
551 		enq = &fbd->hw_stats.rxb.enq[i];
552 		fbnic_report_hw_stats(fbnic_gstrings_rxb_enqueue_stats,
553 				      enq, FBNIC_HW_RXB_ENQUEUE_STATS_LEN,
554 				      &data);
555 	}
556 
557 	for (i = 0; i < FBNIC_RXB_FIFO_INDICES; i++) {
558 		const struct fbnic_rxb_fifo_stats *fifo;
559 
560 		fifo = &fbd->hw_stats.rxb.fifo[i];
561 		fbnic_report_hw_stats(fbnic_gstrings_rxb_fifo_stats,
562 				      fifo, FBNIC_HW_RXB_FIFO_STATS_LEN,
563 				      &data);
564 	}
565 
566 	for (i = 0; i < FBNIC_RXB_DEQUEUE_INDICES; i++) {
567 		const struct fbnic_rxb_dequeue_stats *deq;
568 
569 		deq = &fbd->hw_stats.rxb.deq[i];
570 		fbnic_report_hw_stats(fbnic_gstrings_rxb_dequeue_stats,
571 				      deq, FBNIC_HW_RXB_DEQUEUE_STATS_LEN,
572 				      &data);
573 	}
574 
575 	for (i  = 0; i < FBNIC_MAX_QUEUES; i++) {
576 		const struct fbnic_hw_q_stats *hw_q = &fbd->hw_stats.hw_q[i];
577 
578 		fbnic_report_hw_stats(fbnic_gstrings_hw_q_stats, hw_q,
579 				      FBNIC_HW_Q_STATS_LEN, &data);
580 	}
581 	spin_unlock(&fbd->hw_stats.lock);
582 
583 	for (i = 0; i < FBNIC_MAX_XDPQS; i++)
584 		fbnic_get_xdp_queue_stats(fbn->tx[i + FBNIC_MAX_TXQS], &data);
585 }
586 
fbnic_get_sset_count(struct net_device * dev,int sset)587 static int fbnic_get_sset_count(struct net_device *dev, int sset)
588 {
589 	switch (sset) {
590 	case ETH_SS_STATS:
591 		return FBNIC_STATS_LEN;
592 	case ETH_SS_TEST:
593 		return FBNIC_TEST_LEN;
594 	default:
595 		return -EOPNOTSUPP;
596 	}
597 }
598 
fbnic_get_rss_hash_idx(u32 flow_type)599 static int fbnic_get_rss_hash_idx(u32 flow_type)
600 {
601 	switch (flow_type & ~(FLOW_EXT | FLOW_MAC_EXT | FLOW_RSS)) {
602 	case TCP_V4_FLOW:
603 		return FBNIC_TCP4_HASH_OPT;
604 	case TCP_V6_FLOW:
605 		return FBNIC_TCP6_HASH_OPT;
606 	case UDP_V4_FLOW:
607 		return FBNIC_UDP4_HASH_OPT;
608 	case UDP_V6_FLOW:
609 		return FBNIC_UDP6_HASH_OPT;
610 	case AH_V4_FLOW:
611 	case ESP_V4_FLOW:
612 	case AH_ESP_V4_FLOW:
613 	case SCTP_V4_FLOW:
614 	case IPV4_FLOW:
615 	case IPV4_USER_FLOW:
616 		return FBNIC_IPV4_HASH_OPT;
617 	case AH_V6_FLOW:
618 	case ESP_V6_FLOW:
619 	case AH_ESP_V6_FLOW:
620 	case SCTP_V6_FLOW:
621 	case IPV6_FLOW:
622 	case IPV6_USER_FLOW:
623 		return FBNIC_IPV6_HASH_OPT;
624 	case ETHER_FLOW:
625 		return FBNIC_ETHER_HASH_OPT;
626 	}
627 
628 	return -1;
629 }
630 
fbnic_get_cls_rule_all(struct fbnic_net * fbn,struct ethtool_rxnfc * cmd,u32 * rule_locs)631 static int fbnic_get_cls_rule_all(struct fbnic_net *fbn,
632 				  struct ethtool_rxnfc *cmd,
633 				  u32 *rule_locs)
634 {
635 	struct fbnic_dev *fbd = fbn->fbd;
636 	int i, cnt = 0;
637 
638 	/* Report maximum rule count */
639 	cmd->data = FBNIC_RPC_ACT_TBL_NFC_ENTRIES;
640 
641 	for (i = 0; i < FBNIC_RPC_ACT_TBL_NFC_ENTRIES; i++) {
642 		int idx = i + FBNIC_RPC_ACT_TBL_NFC_OFFSET;
643 		struct fbnic_act_tcam *act_tcam;
644 
645 		act_tcam = &fbd->act_tcam[idx];
646 		if (act_tcam->state != FBNIC_TCAM_S_VALID)
647 			continue;
648 
649 		if (rule_locs) {
650 			if (cnt == cmd->rule_cnt)
651 				return -EMSGSIZE;
652 
653 			rule_locs[cnt] = i;
654 		}
655 
656 		cnt++;
657 	}
658 
659 	return cnt;
660 }
661 
fbnic_get_cls_rule(struct fbnic_net * fbn,struct ethtool_rxnfc * cmd)662 static int fbnic_get_cls_rule(struct fbnic_net *fbn, struct ethtool_rxnfc *cmd)
663 {
664 	struct ethtool_rx_flow_spec *fsp;
665 	struct fbnic_dev *fbd = fbn->fbd;
666 	struct fbnic_act_tcam *act_tcam;
667 	int idx;
668 
669 	fsp = (struct ethtool_rx_flow_spec *)&cmd->fs;
670 
671 	if (fsp->location >= FBNIC_RPC_ACT_TBL_NFC_ENTRIES)
672 		return -EINVAL;
673 
674 	idx = fsp->location + FBNIC_RPC_ACT_TBL_NFC_OFFSET;
675 	act_tcam = &fbd->act_tcam[idx];
676 
677 	if (act_tcam->state != FBNIC_TCAM_S_VALID)
678 		return -EINVAL;
679 
680 	/* Report maximum rule count */
681 	cmd->data = FBNIC_RPC_ACT_TBL_NFC_ENTRIES;
682 
683 	/* Set flow type field */
684 	if (!(act_tcam->value.tcam[1] & FBNIC_RPC_TCAM_ACT1_IP_VALID)) {
685 		fsp->flow_type = ETHER_FLOW;
686 		if (!FIELD_GET(FBNIC_RPC_TCAM_ACT1_L2_MACDA_IDX,
687 			       act_tcam->mask.tcam[1])) {
688 			struct fbnic_mac_addr *mac_addr;
689 
690 			idx = FIELD_GET(FBNIC_RPC_TCAM_ACT1_L2_MACDA_IDX,
691 					act_tcam->value.tcam[1]);
692 			mac_addr = &fbd->mac_addr[idx];
693 
694 			ether_addr_copy(fsp->h_u.ether_spec.h_dest,
695 					mac_addr->value.addr8);
696 			eth_broadcast_addr(fsp->m_u.ether_spec.h_dest);
697 		}
698 	} else if (act_tcam->value.tcam[1] &
699 		   FBNIC_RPC_TCAM_ACT1_OUTER_IP_VALID) {
700 		fsp->flow_type = IPV6_USER_FLOW;
701 		fsp->h_u.usr_ip6_spec.l4_proto = IPPROTO_IPV6;
702 		fsp->m_u.usr_ip6_spec.l4_proto = 0xff;
703 
704 		if (!FIELD_GET(FBNIC_RPC_TCAM_ACT0_OUTER_IPSRC_IDX,
705 			       act_tcam->mask.tcam[0])) {
706 			struct fbnic_ip_addr *ip_addr;
707 			int i;
708 
709 			idx = FIELD_GET(FBNIC_RPC_TCAM_ACT0_OUTER_IPSRC_IDX,
710 					act_tcam->value.tcam[0]);
711 			ip_addr = &fbd->ipo_src[idx];
712 
713 			for (i = 0; i < 4; i++) {
714 				fsp->h_u.usr_ip6_spec.ip6src[i] =
715 					ip_addr->value.s6_addr32[i];
716 				fsp->m_u.usr_ip6_spec.ip6src[i] =
717 					~ip_addr->mask.s6_addr32[i];
718 			}
719 		}
720 
721 		if (!FIELD_GET(FBNIC_RPC_TCAM_ACT0_OUTER_IPDST_IDX,
722 			       act_tcam->mask.tcam[0])) {
723 			struct fbnic_ip_addr *ip_addr;
724 			int i;
725 
726 			idx = FIELD_GET(FBNIC_RPC_TCAM_ACT0_OUTER_IPDST_IDX,
727 					act_tcam->value.tcam[0]);
728 			ip_addr = &fbd->ipo_dst[idx];
729 
730 			for (i = 0; i < 4; i++) {
731 				fsp->h_u.usr_ip6_spec.ip6dst[i] =
732 					ip_addr->value.s6_addr32[i];
733 				fsp->m_u.usr_ip6_spec.ip6dst[i] =
734 					~ip_addr->mask.s6_addr32[i];
735 			}
736 		}
737 	} else if ((act_tcam->value.tcam[1] & FBNIC_RPC_TCAM_ACT1_IP_IS_V6)) {
738 		if (act_tcam->value.tcam[1] & FBNIC_RPC_TCAM_ACT1_L4_VALID) {
739 			if (act_tcam->value.tcam[1] &
740 			    FBNIC_RPC_TCAM_ACT1_L4_IS_UDP)
741 				fsp->flow_type = UDP_V6_FLOW;
742 			else
743 				fsp->flow_type = TCP_V6_FLOW;
744 			fsp->h_u.tcp_ip6_spec.psrc =
745 				cpu_to_be16(act_tcam->value.tcam[3]);
746 			fsp->m_u.tcp_ip6_spec.psrc =
747 				cpu_to_be16(~act_tcam->mask.tcam[3]);
748 			fsp->h_u.tcp_ip6_spec.pdst =
749 				cpu_to_be16(act_tcam->value.tcam[4]);
750 			fsp->m_u.tcp_ip6_spec.pdst =
751 				cpu_to_be16(~act_tcam->mask.tcam[4]);
752 		} else {
753 			fsp->flow_type = IPV6_USER_FLOW;
754 		}
755 
756 		if (!FIELD_GET(FBNIC_RPC_TCAM_ACT0_IPSRC_IDX,
757 			       act_tcam->mask.tcam[0])) {
758 			struct fbnic_ip_addr *ip_addr;
759 			int i;
760 
761 			idx = FIELD_GET(FBNIC_RPC_TCAM_ACT0_IPSRC_IDX,
762 					act_tcam->value.tcam[0]);
763 			ip_addr = &fbd->ip_src[idx];
764 
765 			for (i = 0; i < 4; i++) {
766 				fsp->h_u.usr_ip6_spec.ip6src[i] =
767 					ip_addr->value.s6_addr32[i];
768 				fsp->m_u.usr_ip6_spec.ip6src[i] =
769 					~ip_addr->mask.s6_addr32[i];
770 			}
771 		}
772 
773 		if (!FIELD_GET(FBNIC_RPC_TCAM_ACT0_IPDST_IDX,
774 			       act_tcam->mask.tcam[0])) {
775 			struct fbnic_ip_addr *ip_addr;
776 			int i;
777 
778 			idx = FIELD_GET(FBNIC_RPC_TCAM_ACT0_IPDST_IDX,
779 					act_tcam->value.tcam[0]);
780 			ip_addr = &fbd->ip_dst[idx];
781 
782 			for (i = 0; i < 4; i++) {
783 				fsp->h_u.usr_ip6_spec.ip6dst[i] =
784 					ip_addr->value.s6_addr32[i];
785 				fsp->m_u.usr_ip6_spec.ip6dst[i] =
786 					~ip_addr->mask.s6_addr32[i];
787 			}
788 		}
789 	} else {
790 		if (act_tcam->value.tcam[1] & FBNIC_RPC_TCAM_ACT1_L4_VALID) {
791 			if (act_tcam->value.tcam[1] &
792 			    FBNIC_RPC_TCAM_ACT1_L4_IS_UDP)
793 				fsp->flow_type = UDP_V4_FLOW;
794 			else
795 				fsp->flow_type = TCP_V4_FLOW;
796 			fsp->h_u.tcp_ip4_spec.psrc =
797 				cpu_to_be16(act_tcam->value.tcam[3]);
798 			fsp->m_u.tcp_ip4_spec.psrc =
799 				cpu_to_be16(~act_tcam->mask.tcam[3]);
800 			fsp->h_u.tcp_ip4_spec.pdst =
801 				cpu_to_be16(act_tcam->value.tcam[4]);
802 			fsp->m_u.tcp_ip4_spec.pdst =
803 				cpu_to_be16(~act_tcam->mask.tcam[4]);
804 		} else {
805 			fsp->flow_type = IPV4_USER_FLOW;
806 			fsp->h_u.usr_ip4_spec.ip_ver = ETH_RX_NFC_IP4;
807 		}
808 
809 		if (!FIELD_GET(FBNIC_RPC_TCAM_ACT0_IPSRC_IDX,
810 			       act_tcam->mask.tcam[0])) {
811 			struct fbnic_ip_addr *ip_addr;
812 
813 			idx = FIELD_GET(FBNIC_RPC_TCAM_ACT0_IPSRC_IDX,
814 					act_tcam->value.tcam[0]);
815 			ip_addr = &fbd->ip_src[idx];
816 
817 			fsp->h_u.usr_ip4_spec.ip4src =
818 				ip_addr->value.s6_addr32[3];
819 			fsp->m_u.usr_ip4_spec.ip4src =
820 				~ip_addr->mask.s6_addr32[3];
821 		}
822 
823 		if (!FIELD_GET(FBNIC_RPC_TCAM_ACT0_IPDST_IDX,
824 			       act_tcam->mask.tcam[0])) {
825 			struct fbnic_ip_addr *ip_addr;
826 
827 			idx = FIELD_GET(FBNIC_RPC_TCAM_ACT0_IPDST_IDX,
828 					act_tcam->value.tcam[0]);
829 			ip_addr = &fbd->ip_dst[idx];
830 
831 			fsp->h_u.usr_ip4_spec.ip4dst =
832 				ip_addr->value.s6_addr32[3];
833 			fsp->m_u.usr_ip4_spec.ip4dst =
834 				~ip_addr->mask.s6_addr32[3];
835 		}
836 	}
837 
838 	/* Record action */
839 	if (act_tcam->dest & FBNIC_RPC_ACT_TBL0_DROP)
840 		fsp->ring_cookie = RX_CLS_FLOW_DISC;
841 	else if (act_tcam->dest & FBNIC_RPC_ACT_TBL0_Q_SEL)
842 		fsp->ring_cookie = FIELD_GET(FBNIC_RPC_ACT_TBL0_Q_ID,
843 					     act_tcam->dest);
844 	else
845 		fsp->flow_type |= FLOW_RSS;
846 
847 	cmd->rss_context = FIELD_GET(FBNIC_RPC_ACT_TBL0_RSS_CTXT_ID,
848 				     act_tcam->dest);
849 
850 	return 0;
851 }
852 
fbnic_get_rx_ring_count(struct net_device * netdev)853 static u32 fbnic_get_rx_ring_count(struct net_device *netdev)
854 {
855 	struct fbnic_net *fbn = netdev_priv(netdev);
856 
857 	return fbn->num_rx_queues;
858 }
859 
fbnic_get_rxnfc(struct net_device * netdev,struct ethtool_rxnfc * cmd,u32 * rule_locs)860 static int fbnic_get_rxnfc(struct net_device *netdev,
861 			   struct ethtool_rxnfc *cmd, u32 *rule_locs)
862 {
863 	struct fbnic_net *fbn = netdev_priv(netdev);
864 	int ret = -EOPNOTSUPP;
865 	u32 special = 0;
866 
867 	switch (cmd->cmd) {
868 	case ETHTOOL_GRXCLSRULE:
869 		ret = fbnic_get_cls_rule(fbn, cmd);
870 		break;
871 	case ETHTOOL_GRXCLSRLCNT:
872 		rule_locs = NULL;
873 		special = RX_CLS_LOC_SPECIAL;
874 		fallthrough;
875 	case ETHTOOL_GRXCLSRLALL:
876 		ret = fbnic_get_cls_rule_all(fbn, cmd, rule_locs);
877 		if (ret < 0)
878 			break;
879 
880 		cmd->data |= special;
881 		cmd->rule_cnt = ret;
882 		ret = 0;
883 		break;
884 	}
885 
886 	return ret;
887 }
888 
fbnic_cls_rule_any_loc(struct fbnic_dev * fbd)889 static int fbnic_cls_rule_any_loc(struct fbnic_dev *fbd)
890 {
891 	int i;
892 
893 	for (i = FBNIC_RPC_ACT_TBL_NFC_ENTRIES; i--;) {
894 		int idx = i + FBNIC_RPC_ACT_TBL_NFC_OFFSET;
895 
896 		if (fbd->act_tcam[idx].state != FBNIC_TCAM_S_VALID)
897 			return i;
898 	}
899 
900 	return -ENOSPC;
901 }
902 
fbnic_set_cls_rule_ins(struct fbnic_net * fbn,const struct ethtool_rxnfc * cmd)903 static int fbnic_set_cls_rule_ins(struct fbnic_net *fbn,
904 				  const struct ethtool_rxnfc *cmd)
905 {
906 	u16 flow_value = 0, flow_mask = 0xffff, ip_value = 0, ip_mask = 0xffff;
907 	u16 sport = 0, sport_mask = ~0, dport = 0, dport_mask = ~0;
908 	u16 misc = 0, misc_mask = ~0;
909 	u32 dest = FIELD_PREP(FBNIC_RPC_ACT_TBL0_DEST_MASK,
910 			      FBNIC_RPC_ACT_TBL0_DEST_HOST);
911 	struct fbnic_ip_addr *ip_src = NULL, *ip_dst = NULL;
912 	struct fbnic_mac_addr *mac_addr = NULL;
913 	struct ethtool_rx_flow_spec *fsp;
914 	struct fbnic_dev *fbd = fbn->fbd;
915 	struct fbnic_act_tcam *act_tcam;
916 	struct in6_addr *addr6, *mask6;
917 	struct in_addr *addr4, *mask4;
918 	int hash_idx, location;
919 	u32 flow_type;
920 	int idx, j;
921 
922 	fsp = (struct ethtool_rx_flow_spec *)&cmd->fs;
923 
924 	if (fsp->location != RX_CLS_LOC_ANY)
925 		return -EINVAL;
926 	location = fbnic_cls_rule_any_loc(fbd);
927 	if (location < 0)
928 		return location;
929 
930 	if (fsp->ring_cookie == RX_CLS_FLOW_DISC) {
931 		dest = FBNIC_RPC_ACT_TBL0_DROP;
932 	} else if (fsp->flow_type & FLOW_RSS) {
933 		if (cmd->rss_context == 1)
934 			dest |= FBNIC_RPC_ACT_TBL0_RSS_CTXT_ID;
935 	} else {
936 		u32 ring_idx = ethtool_get_flow_spec_ring(fsp->ring_cookie);
937 
938 		if (ring_idx >= fbn->num_rx_queues)
939 			return -EINVAL;
940 
941 		dest |= FBNIC_RPC_ACT_TBL0_Q_SEL |
942 			FIELD_PREP(FBNIC_RPC_ACT_TBL0_Q_ID, ring_idx);
943 	}
944 
945 	idx = location + FBNIC_RPC_ACT_TBL_NFC_OFFSET;
946 	act_tcam = &fbd->act_tcam[idx];
947 
948 	/* Do not allow overwriting for now.
949 	 * To support overwriting rules we will need to add logic to free
950 	 * any IP or MACDA TCAMs that may be associated with the old rule.
951 	 */
952 	if (act_tcam->state != FBNIC_TCAM_S_DISABLED)
953 		return -EBUSY;
954 
955 	flow_type = fsp->flow_type & ~(FLOW_EXT | FLOW_RSS);
956 	hash_idx = fbnic_get_rss_hash_idx(flow_type);
957 
958 	switch (flow_type) {
959 	case UDP_V4_FLOW:
960 udp4_flow:
961 		flow_value |= FBNIC_RPC_TCAM_ACT1_L4_IS_UDP;
962 		fallthrough;
963 	case TCP_V4_FLOW:
964 tcp4_flow:
965 		flow_value |= FBNIC_RPC_TCAM_ACT1_L4_VALID;
966 		flow_mask &= ~(FBNIC_RPC_TCAM_ACT1_L4_IS_UDP |
967 			       FBNIC_RPC_TCAM_ACT1_L4_VALID);
968 
969 		sport = be16_to_cpu(fsp->h_u.tcp_ip4_spec.psrc);
970 		sport_mask = ~be16_to_cpu(fsp->m_u.tcp_ip4_spec.psrc);
971 		dport = be16_to_cpu(fsp->h_u.tcp_ip4_spec.pdst);
972 		dport_mask = ~be16_to_cpu(fsp->m_u.tcp_ip4_spec.pdst);
973 		goto ip4_flow;
974 	case IP_USER_FLOW:
975 		if (!fsp->m_u.usr_ip4_spec.proto)
976 			goto ip4_flow;
977 		if (fsp->m_u.usr_ip4_spec.proto != 0xff)
978 			return -EINVAL;
979 		if (fsp->h_u.usr_ip4_spec.proto == IPPROTO_UDP)
980 			goto udp4_flow;
981 		if (fsp->h_u.usr_ip4_spec.proto == IPPROTO_TCP)
982 			goto tcp4_flow;
983 		return -EINVAL;
984 ip4_flow:
985 		addr4 = (struct in_addr *)&fsp->h_u.usr_ip4_spec.ip4src;
986 		mask4 = (struct in_addr *)&fsp->m_u.usr_ip4_spec.ip4src;
987 		if (mask4->s_addr) {
988 			ip_src = __fbnic_ip4_sync(fbd, fbd->ip_src,
989 						  addr4, mask4);
990 			if (!ip_src)
991 				return -ENOSPC;
992 
993 			set_bit(idx, ip_src->act_tcam);
994 			ip_value |= FBNIC_RPC_TCAM_ACT0_IPSRC_VALID |
995 				    FIELD_PREP(FBNIC_RPC_TCAM_ACT0_IPSRC_IDX,
996 					       ip_src - fbd->ip_src);
997 			ip_mask &= ~(FBNIC_RPC_TCAM_ACT0_IPSRC_VALID |
998 				     FBNIC_RPC_TCAM_ACT0_IPSRC_IDX);
999 		}
1000 
1001 		addr4 = (struct in_addr *)&fsp->h_u.usr_ip4_spec.ip4dst;
1002 		mask4 = (struct in_addr *)&fsp->m_u.usr_ip4_spec.ip4dst;
1003 		if (mask4->s_addr) {
1004 			ip_dst = __fbnic_ip4_sync(fbd, fbd->ip_dst,
1005 						  addr4, mask4);
1006 			if (!ip_dst) {
1007 				if (ip_src && ip_src->state == FBNIC_TCAM_S_ADD)
1008 					memset(ip_src, 0, sizeof(*ip_src));
1009 				return -ENOSPC;
1010 			}
1011 
1012 			set_bit(idx, ip_dst->act_tcam);
1013 			ip_value |= FBNIC_RPC_TCAM_ACT0_IPDST_VALID |
1014 				    FIELD_PREP(FBNIC_RPC_TCAM_ACT0_IPDST_IDX,
1015 					       ip_dst - fbd->ip_dst);
1016 			ip_mask &= ~(FBNIC_RPC_TCAM_ACT0_IPDST_VALID |
1017 				     FBNIC_RPC_TCAM_ACT0_IPDST_IDX);
1018 		}
1019 		flow_value |= FBNIC_RPC_TCAM_ACT1_IP_VALID |
1020 			      FBNIC_RPC_TCAM_ACT1_L2_MACDA_VALID;
1021 		flow_mask &= ~(FBNIC_RPC_TCAM_ACT1_IP_IS_V6 |
1022 			       FBNIC_RPC_TCAM_ACT1_IP_VALID |
1023 			       FBNIC_RPC_TCAM_ACT1_L2_MACDA_VALID);
1024 		break;
1025 	case UDP_V6_FLOW:
1026 udp6_flow:
1027 		flow_value |= FBNIC_RPC_TCAM_ACT1_L4_IS_UDP;
1028 		fallthrough;
1029 	case TCP_V6_FLOW:
1030 tcp6_flow:
1031 		flow_value |= FBNIC_RPC_TCAM_ACT1_L4_VALID;
1032 		flow_mask &= ~(FBNIC_RPC_TCAM_ACT1_L4_IS_UDP |
1033 			  FBNIC_RPC_TCAM_ACT1_L4_VALID);
1034 
1035 		sport = be16_to_cpu(fsp->h_u.tcp_ip6_spec.psrc);
1036 		sport_mask = ~be16_to_cpu(fsp->m_u.tcp_ip6_spec.psrc);
1037 		dport = be16_to_cpu(fsp->h_u.tcp_ip6_spec.pdst);
1038 		dport_mask = ~be16_to_cpu(fsp->m_u.tcp_ip6_spec.pdst);
1039 		goto ipv6_flow;
1040 	case IPV6_USER_FLOW:
1041 		if (!fsp->m_u.usr_ip6_spec.l4_proto)
1042 			goto ipv6_flow;
1043 
1044 		if (fsp->m_u.usr_ip6_spec.l4_proto != 0xff)
1045 			return -EINVAL;
1046 		if (fsp->h_u.usr_ip6_spec.l4_proto == IPPROTO_UDP)
1047 			goto udp6_flow;
1048 		if (fsp->h_u.usr_ip6_spec.l4_proto == IPPROTO_TCP)
1049 			goto tcp6_flow;
1050 		if (fsp->h_u.usr_ip6_spec.l4_proto != IPPROTO_IPV6)
1051 			return -EINVAL;
1052 
1053 		addr6 = (struct in6_addr *)fsp->h_u.usr_ip6_spec.ip6src;
1054 		mask6 = (struct in6_addr *)fsp->m_u.usr_ip6_spec.ip6src;
1055 		if (!ipv6_addr_any(mask6)) {
1056 			ip_src = __fbnic_ip6_sync(fbd, fbd->ipo_src,
1057 						  addr6, mask6);
1058 			if (!ip_src)
1059 				return -ENOSPC;
1060 
1061 			set_bit(idx, ip_src->act_tcam);
1062 			ip_value |=
1063 				FBNIC_RPC_TCAM_ACT0_OUTER_IPSRC_VALID |
1064 				FIELD_PREP(FBNIC_RPC_TCAM_ACT0_OUTER_IPSRC_IDX,
1065 					   ip_src - fbd->ipo_src);
1066 			ip_mask &=
1067 				~(FBNIC_RPC_TCAM_ACT0_OUTER_IPSRC_VALID |
1068 				  FBNIC_RPC_TCAM_ACT0_OUTER_IPSRC_IDX);
1069 		}
1070 
1071 		addr6 = (struct in6_addr *)fsp->h_u.usr_ip6_spec.ip6dst;
1072 		mask6 = (struct in6_addr *)fsp->m_u.usr_ip6_spec.ip6dst;
1073 		if (!ipv6_addr_any(mask6)) {
1074 			ip_dst = __fbnic_ip6_sync(fbd, fbd->ipo_dst,
1075 						  addr6, mask6);
1076 			if (!ip_dst) {
1077 				if (ip_src && ip_src->state == FBNIC_TCAM_S_ADD)
1078 					memset(ip_src, 0, sizeof(*ip_src));
1079 				return -ENOSPC;
1080 			}
1081 
1082 			set_bit(idx, ip_dst->act_tcam);
1083 			ip_value |=
1084 				FBNIC_RPC_TCAM_ACT0_OUTER_IPDST_VALID |
1085 				FIELD_PREP(FBNIC_RPC_TCAM_ACT0_OUTER_IPDST_IDX,
1086 					   ip_dst - fbd->ipo_dst);
1087 			ip_mask &= ~(FBNIC_RPC_TCAM_ACT0_OUTER_IPDST_VALID |
1088 				     FBNIC_RPC_TCAM_ACT0_OUTER_IPDST_IDX);
1089 		}
1090 
1091 		flow_value |= FBNIC_RPC_TCAM_ACT1_OUTER_IP_VALID;
1092 		flow_mask &= FBNIC_RPC_TCAM_ACT1_OUTER_IP_VALID;
1093 ipv6_flow:
1094 		addr6 = (struct in6_addr *)fsp->h_u.usr_ip6_spec.ip6src;
1095 		mask6 = (struct in6_addr *)fsp->m_u.usr_ip6_spec.ip6src;
1096 		if (!ip_src && !ipv6_addr_any(mask6)) {
1097 			ip_src = __fbnic_ip6_sync(fbd, fbd->ip_src,
1098 						  addr6, mask6);
1099 			if (!ip_src)
1100 				return -ENOSPC;
1101 
1102 			set_bit(idx, ip_src->act_tcam);
1103 			ip_value |= FBNIC_RPC_TCAM_ACT0_IPSRC_VALID |
1104 				    FIELD_PREP(FBNIC_RPC_TCAM_ACT0_IPSRC_IDX,
1105 					       ip_src - fbd->ip_src);
1106 			ip_mask &= ~(FBNIC_RPC_TCAM_ACT0_IPSRC_VALID |
1107 				       FBNIC_RPC_TCAM_ACT0_IPSRC_IDX);
1108 		}
1109 
1110 		addr6 = (struct in6_addr *)fsp->h_u.usr_ip6_spec.ip6dst;
1111 		mask6 = (struct in6_addr *)fsp->m_u.usr_ip6_spec.ip6dst;
1112 		if (!ip_dst && !ipv6_addr_any(mask6)) {
1113 			ip_dst = __fbnic_ip6_sync(fbd, fbd->ip_dst,
1114 						  addr6, mask6);
1115 			if (!ip_dst) {
1116 				if (ip_src && ip_src->state == FBNIC_TCAM_S_ADD)
1117 					memset(ip_src, 0, sizeof(*ip_src));
1118 				return -ENOSPC;
1119 			}
1120 
1121 			set_bit(idx, ip_dst->act_tcam);
1122 			ip_value |= FBNIC_RPC_TCAM_ACT0_IPDST_VALID |
1123 				    FIELD_PREP(FBNIC_RPC_TCAM_ACT0_IPDST_IDX,
1124 					       ip_dst - fbd->ip_dst);
1125 			ip_mask &= ~(FBNIC_RPC_TCAM_ACT0_IPDST_VALID |
1126 				       FBNIC_RPC_TCAM_ACT0_IPDST_IDX);
1127 		}
1128 
1129 		flow_value |= FBNIC_RPC_TCAM_ACT1_IP_IS_V6 |
1130 			      FBNIC_RPC_TCAM_ACT1_IP_VALID |
1131 			      FBNIC_RPC_TCAM_ACT1_L2_MACDA_VALID;
1132 		flow_mask &= ~(FBNIC_RPC_TCAM_ACT1_IP_IS_V6 |
1133 			       FBNIC_RPC_TCAM_ACT1_IP_VALID |
1134 			       FBNIC_RPC_TCAM_ACT1_L2_MACDA_VALID);
1135 		break;
1136 	case ETHER_FLOW:
1137 		if (!is_zero_ether_addr(fsp->m_u.ether_spec.h_dest)) {
1138 			u8 *addr = fsp->h_u.ether_spec.h_dest;
1139 			u8 *mask = fsp->m_u.ether_spec.h_dest;
1140 
1141 			/* Do not allow MAC addr of 0 */
1142 			if (is_zero_ether_addr(addr))
1143 				return -EINVAL;
1144 
1145 			/* Only support full MAC address to avoid
1146 			 * conflicts with other MAC addresses.
1147 			 */
1148 			if (!is_broadcast_ether_addr(mask))
1149 				return -EINVAL;
1150 
1151 			if (is_multicast_ether_addr(addr))
1152 				mac_addr = __fbnic_mc_sync(fbd, addr);
1153 			else
1154 				mac_addr = __fbnic_uc_sync(fbd, addr);
1155 
1156 			if (!mac_addr)
1157 				return -ENOSPC;
1158 
1159 			set_bit(idx, mac_addr->act_tcam);
1160 			flow_value |=
1161 				FIELD_PREP(FBNIC_RPC_TCAM_ACT1_L2_MACDA_IDX,
1162 					   mac_addr - fbd->mac_addr);
1163 			flow_mask &= ~FBNIC_RPC_TCAM_ACT1_L2_MACDA_IDX;
1164 		}
1165 
1166 		flow_value |= FBNIC_RPC_TCAM_ACT1_L2_MACDA_VALID;
1167 		flow_mask &= ~FBNIC_RPC_TCAM_ACT1_L2_MACDA_VALID;
1168 		break;
1169 	default:
1170 		return -EINVAL;
1171 	}
1172 
1173 	dest |= FIELD_PREP(FBNIC_RPC_ACT_TBL0_DMA_HINT,
1174 			   FBNIC_RCD_HDR_AL_DMA_HINT_L4);
1175 
1176 	/* Write action table values */
1177 	act_tcam->dest = dest;
1178 	act_tcam->rss_en_mask = fbnic_flow_hash_2_rss_en_mask(fbn, hash_idx);
1179 
1180 	/* Write IP Match value/mask to action_tcam[0] */
1181 	act_tcam->value.tcam[0] = ip_value;
1182 	act_tcam->mask.tcam[0] = ip_mask;
1183 
1184 	/* Write flow type value/mask to action_tcam[1] */
1185 	act_tcam->value.tcam[1] = flow_value;
1186 	act_tcam->mask.tcam[1] = flow_mask;
1187 
1188 	/* Write error, DSCP, extra L4 matches to action_tcam[2] */
1189 	act_tcam->value.tcam[2] = misc;
1190 	act_tcam->mask.tcam[2] = misc_mask;
1191 
1192 	/* Write source/destination port values */
1193 	act_tcam->value.tcam[3] = sport;
1194 	act_tcam->mask.tcam[3] = sport_mask;
1195 	act_tcam->value.tcam[4] = dport;
1196 	act_tcam->mask.tcam[4] = dport_mask;
1197 
1198 	for (j = 5; j < FBNIC_RPC_TCAM_ACT_WORD_LEN; j++)
1199 		act_tcam->mask.tcam[j] = 0xffff;
1200 
1201 	act_tcam->state = FBNIC_TCAM_S_UPDATE;
1202 	fsp->location = location;
1203 
1204 	if (netif_running(fbn->netdev)) {
1205 		fbnic_write_rules(fbd);
1206 		if (ip_src || ip_dst)
1207 			fbnic_write_ip_addr(fbd);
1208 		if (mac_addr)
1209 			fbnic_write_macda(fbd);
1210 	}
1211 
1212 	return 0;
1213 }
1214 
fbnic_clear_nfc_macda(struct fbnic_net * fbn,unsigned int tcam_idx)1215 static void fbnic_clear_nfc_macda(struct fbnic_net *fbn,
1216 				  unsigned int tcam_idx)
1217 {
1218 	struct fbnic_dev *fbd = fbn->fbd;
1219 	int idx;
1220 
1221 	for (idx = ARRAY_SIZE(fbd->mac_addr); idx--;)
1222 		__fbnic_xc_unsync(&fbd->mac_addr[idx], tcam_idx);
1223 
1224 	/* Write updates to hardware */
1225 	if (netif_running(fbn->netdev))
1226 		fbnic_write_macda(fbd);
1227 }
1228 
fbnic_clear_nfc_ip_addr(struct fbnic_net * fbn,unsigned int tcam_idx)1229 static void fbnic_clear_nfc_ip_addr(struct fbnic_net *fbn,
1230 				    unsigned int tcam_idx)
1231 {
1232 	struct fbnic_dev *fbd = fbn->fbd;
1233 	int idx;
1234 
1235 	for (idx = ARRAY_SIZE(fbd->ip_src); idx--;)
1236 		__fbnic_ip_unsync(&fbd->ip_src[idx], tcam_idx);
1237 	for (idx = ARRAY_SIZE(fbd->ip_dst); idx--;)
1238 		__fbnic_ip_unsync(&fbd->ip_dst[idx], tcam_idx);
1239 	for (idx = ARRAY_SIZE(fbd->ipo_src); idx--;)
1240 		__fbnic_ip_unsync(&fbd->ipo_src[idx], tcam_idx);
1241 	for (idx = ARRAY_SIZE(fbd->ipo_dst); idx--;)
1242 		__fbnic_ip_unsync(&fbd->ipo_dst[idx], tcam_idx);
1243 
1244 	/* Write updates to hardware */
1245 	if (netif_running(fbn->netdev))
1246 		fbnic_write_ip_addr(fbd);
1247 }
1248 
fbnic_set_cls_rule_del(struct fbnic_net * fbn,const struct ethtool_rxnfc * cmd)1249 static int fbnic_set_cls_rule_del(struct fbnic_net *fbn,
1250 				  const struct ethtool_rxnfc *cmd)
1251 {
1252 	struct ethtool_rx_flow_spec *fsp;
1253 	struct fbnic_dev *fbd = fbn->fbd;
1254 	struct fbnic_act_tcam *act_tcam;
1255 	int idx;
1256 
1257 	fsp = (struct ethtool_rx_flow_spec *)&cmd->fs;
1258 
1259 	if (fsp->location >= FBNIC_RPC_ACT_TBL_NFC_ENTRIES)
1260 		return -EINVAL;
1261 
1262 	idx = fsp->location + FBNIC_RPC_ACT_TBL_NFC_OFFSET;
1263 	act_tcam = &fbd->act_tcam[idx];
1264 
1265 	if (act_tcam->state != FBNIC_TCAM_S_VALID)
1266 		return -EINVAL;
1267 
1268 	act_tcam->state = FBNIC_TCAM_S_DELETE;
1269 
1270 	if ((act_tcam->value.tcam[1] & FBNIC_RPC_TCAM_ACT1_L2_MACDA_VALID) &&
1271 	    (~act_tcam->mask.tcam[1] & FBNIC_RPC_TCAM_ACT1_L2_MACDA_IDX))
1272 		fbnic_clear_nfc_macda(fbn, idx);
1273 
1274 	if ((act_tcam->value.tcam[0] &
1275 	     (FBNIC_RPC_TCAM_ACT0_IPSRC_VALID |
1276 	      FBNIC_RPC_TCAM_ACT0_IPDST_VALID |
1277 	      FBNIC_RPC_TCAM_ACT0_OUTER_IPSRC_VALID |
1278 	      FBNIC_RPC_TCAM_ACT0_OUTER_IPDST_VALID)) &&
1279 	    (~act_tcam->mask.tcam[0] &
1280 	     (FBNIC_RPC_TCAM_ACT0_IPSRC_IDX |
1281 	      FBNIC_RPC_TCAM_ACT0_IPDST_IDX |
1282 	      FBNIC_RPC_TCAM_ACT0_OUTER_IPSRC_IDX |
1283 	      FBNIC_RPC_TCAM_ACT0_OUTER_IPDST_IDX)))
1284 		fbnic_clear_nfc_ip_addr(fbn, idx);
1285 
1286 	if (netif_running(fbn->netdev))
1287 		fbnic_write_rules(fbd);
1288 
1289 	return 0;
1290 }
1291 
fbnic_set_rxnfc(struct net_device * netdev,struct ethtool_rxnfc * cmd)1292 static int fbnic_set_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd)
1293 {
1294 	struct fbnic_net *fbn = netdev_priv(netdev);
1295 	int ret = -EOPNOTSUPP;
1296 
1297 	switch (cmd->cmd) {
1298 	case ETHTOOL_SRXCLSRLINS:
1299 		ret = fbnic_set_cls_rule_ins(fbn, cmd);
1300 		break;
1301 	case ETHTOOL_SRXCLSRLDEL:
1302 		ret = fbnic_set_cls_rule_del(fbn, cmd);
1303 		break;
1304 	}
1305 
1306 	return ret;
1307 }
1308 
fbnic_get_rxfh_key_size(struct net_device * netdev)1309 static u32 fbnic_get_rxfh_key_size(struct net_device *netdev)
1310 {
1311 	return FBNIC_RPC_RSS_KEY_BYTE_LEN;
1312 }
1313 
fbnic_get_rxfh_indir_size(struct net_device * netdev)1314 static u32 fbnic_get_rxfh_indir_size(struct net_device *netdev)
1315 {
1316 	return FBNIC_RPC_RSS_TBL_SIZE;
1317 }
1318 
1319 static int
fbnic_get_rxfh(struct net_device * netdev,struct ethtool_rxfh_param * rxfh)1320 fbnic_get_rxfh(struct net_device *netdev, struct ethtool_rxfh_param *rxfh)
1321 {
1322 	struct fbnic_net *fbn = netdev_priv(netdev);
1323 	unsigned int i;
1324 
1325 	rxfh->hfunc = ETH_RSS_HASH_TOP;
1326 
1327 	if (rxfh->key) {
1328 		for (i = 0; i < FBNIC_RPC_RSS_KEY_BYTE_LEN; i++) {
1329 			u32 rss_key = fbn->rss_key[i / 4] << ((i % 4) * 8);
1330 
1331 			rxfh->key[i] = rss_key >> 24;
1332 		}
1333 	}
1334 
1335 	if (rxfh->indir) {
1336 		for (i = 0; i < FBNIC_RPC_RSS_TBL_SIZE; i++)
1337 			rxfh->indir[i] = fbn->indir_tbl[0][i];
1338 	}
1339 
1340 	return 0;
1341 }
1342 
1343 static unsigned int
fbnic_set_indir(struct fbnic_net * fbn,unsigned int idx,const u32 * indir)1344 fbnic_set_indir(struct fbnic_net *fbn, unsigned int idx, const u32 *indir)
1345 {
1346 	unsigned int i, changes = 0;
1347 
1348 	for (i = 0; i < FBNIC_RPC_RSS_TBL_SIZE; i++) {
1349 		if (fbn->indir_tbl[idx][i] == indir[i])
1350 			continue;
1351 
1352 		fbn->indir_tbl[idx][i] = indir[i];
1353 		changes++;
1354 	}
1355 
1356 	return changes;
1357 }
1358 
1359 static int
fbnic_set_rxfh(struct net_device * netdev,struct ethtool_rxfh_param * rxfh,struct netlink_ext_ack * extack)1360 fbnic_set_rxfh(struct net_device *netdev, struct ethtool_rxfh_param *rxfh,
1361 	       struct netlink_ext_ack *extack)
1362 {
1363 	struct fbnic_net *fbn = netdev_priv(netdev);
1364 	unsigned int i, changes = 0;
1365 
1366 	if (rxfh->hfunc != ETH_RSS_HASH_NO_CHANGE &&
1367 	    rxfh->hfunc != ETH_RSS_HASH_TOP)
1368 		return -EINVAL;
1369 
1370 	if (rxfh->key) {
1371 		u32 rss_key = 0;
1372 
1373 		for (i = FBNIC_RPC_RSS_KEY_BYTE_LEN; i--;) {
1374 			rss_key >>= 8;
1375 			rss_key |= (u32)(rxfh->key[i]) << 24;
1376 
1377 			if (i % 4)
1378 				continue;
1379 
1380 			if (fbn->rss_key[i / 4] == rss_key)
1381 				continue;
1382 
1383 			fbn->rss_key[i / 4] = rss_key;
1384 			changes++;
1385 		}
1386 	}
1387 
1388 	if (rxfh->indir)
1389 		changes += fbnic_set_indir(fbn, 0, rxfh->indir);
1390 
1391 	if (changes && netif_running(netdev))
1392 		fbnic_rss_reinit_hw(fbn->fbd, fbn);
1393 
1394 	return 0;
1395 }
1396 
1397 static int
fbnic_get_rss_hash_opts(struct net_device * netdev,struct ethtool_rxfh_fields * cmd)1398 fbnic_get_rss_hash_opts(struct net_device *netdev,
1399 			struct ethtool_rxfh_fields *cmd)
1400 {
1401 	int hash_opt_idx = fbnic_get_rss_hash_idx(cmd->flow_type);
1402 	struct fbnic_net *fbn = netdev_priv(netdev);
1403 
1404 	if (hash_opt_idx < 0)
1405 		return -EINVAL;
1406 
1407 	/* Report options from rss_en table in fbn */
1408 	cmd->data = fbn->rss_flow_hash[hash_opt_idx];
1409 
1410 	return 0;
1411 }
1412 
1413 #define FBNIC_L2_HASH_OPTIONS \
1414 	(RXH_L2DA | RXH_DISCARD)
1415 #define FBNIC_L3_HASH_OPTIONS \
1416 	(FBNIC_L2_HASH_OPTIONS | RXH_IP_SRC | RXH_IP_DST | RXH_IP6_FL)
1417 #define FBNIC_L4_HASH_OPTIONS \
1418 	(FBNIC_L3_HASH_OPTIONS | RXH_L4_B_0_1 | RXH_L4_B_2_3)
1419 
1420 static int
fbnic_set_rss_hash_opts(struct net_device * netdev,const struct ethtool_rxfh_fields * cmd,struct netlink_ext_ack * extack)1421 fbnic_set_rss_hash_opts(struct net_device *netdev,
1422 			const struct ethtool_rxfh_fields *cmd,
1423 			struct netlink_ext_ack *extack)
1424 {
1425 	struct fbnic_net *fbn = netdev_priv(netdev);
1426 	int hash_opt_idx;
1427 
1428 	/* Verify the type requested is correct */
1429 	hash_opt_idx = fbnic_get_rss_hash_idx(cmd->flow_type);
1430 	if (hash_opt_idx < 0)
1431 		return -EINVAL;
1432 
1433 	/* Verify the fields asked for can actually be assigned based on type */
1434 	if (cmd->data & ~FBNIC_L4_HASH_OPTIONS ||
1435 	    (hash_opt_idx > FBNIC_L4_HASH_OPT &&
1436 	     cmd->data & ~FBNIC_L3_HASH_OPTIONS) ||
1437 	    (hash_opt_idx > FBNIC_IP_HASH_OPT &&
1438 	     cmd->data & ~FBNIC_L2_HASH_OPTIONS))
1439 		return -EINVAL;
1440 
1441 	fbn->rss_flow_hash[hash_opt_idx] = cmd->data;
1442 
1443 	if (netif_running(fbn->netdev)) {
1444 		fbnic_rss_reinit(fbn->fbd, fbn);
1445 		fbnic_write_rules(fbn->fbd);
1446 	}
1447 
1448 	return 0;
1449 }
1450 
1451 static int
fbnic_modify_rxfh_context(struct net_device * netdev,struct ethtool_rxfh_context * ctx,const struct ethtool_rxfh_param * rxfh,struct netlink_ext_ack * extack)1452 fbnic_modify_rxfh_context(struct net_device *netdev,
1453 			  struct ethtool_rxfh_context *ctx,
1454 			  const struct ethtool_rxfh_param *rxfh,
1455 			  struct netlink_ext_ack *extack)
1456 {
1457 	struct fbnic_net *fbn = netdev_priv(netdev);
1458 	const u32 *indir = rxfh->indir;
1459 	unsigned int changes;
1460 
1461 	if (!indir)
1462 		indir = ethtool_rxfh_context_indir(ctx);
1463 
1464 	changes = fbnic_set_indir(fbn, rxfh->rss_context, indir);
1465 	if (changes && netif_running(netdev))
1466 		fbnic_rss_reinit_hw(fbn->fbd, fbn);
1467 
1468 	return 0;
1469 }
1470 
1471 static int
fbnic_create_rxfh_context(struct net_device * netdev,struct ethtool_rxfh_context * ctx,const struct ethtool_rxfh_param * rxfh,struct netlink_ext_ack * extack)1472 fbnic_create_rxfh_context(struct net_device *netdev,
1473 			  struct ethtool_rxfh_context *ctx,
1474 			  const struct ethtool_rxfh_param *rxfh,
1475 			  struct netlink_ext_ack *extack)
1476 {
1477 	struct fbnic_net *fbn = netdev_priv(netdev);
1478 
1479 	if (rxfh->hfunc && rxfh->hfunc != ETH_RSS_HASH_TOP) {
1480 		NL_SET_ERR_MSG_MOD(extack, "RSS hash function not supported");
1481 		return -EOPNOTSUPP;
1482 	}
1483 	ctx->hfunc = ETH_RSS_HASH_TOP;
1484 
1485 	if (!rxfh->indir) {
1486 		u32 *indir = ethtool_rxfh_context_indir(ctx);
1487 		unsigned int num_rx = fbn->num_rx_queues;
1488 		unsigned int i;
1489 
1490 		for (i = 0; i < FBNIC_RPC_RSS_TBL_SIZE; i++)
1491 			indir[i] = ethtool_rxfh_indir_default(i, num_rx);
1492 	}
1493 
1494 	return fbnic_modify_rxfh_context(netdev, ctx, rxfh, extack);
1495 }
1496 
1497 static int
fbnic_remove_rxfh_context(struct net_device * netdev,struct ethtool_rxfh_context * ctx,u32 rss_context,struct netlink_ext_ack * extack)1498 fbnic_remove_rxfh_context(struct net_device *netdev,
1499 			  struct ethtool_rxfh_context *ctx, u32 rss_context,
1500 			  struct netlink_ext_ack *extack)
1501 {
1502 	/* Nothing to do, contexts are allocated statically */
1503 	return 0;
1504 }
1505 
fbnic_ethtool_regs_test(struct net_device * netdev,u64 * data)1506 static int fbnic_ethtool_regs_test(struct net_device *netdev, u64 *data)
1507 {
1508 	struct fbnic_net *fbn = netdev_priv(netdev);
1509 	struct fbnic_dev *fbd = fbn->fbd;
1510 
1511 	*data = fbnic_csr_regs_test(fbd);
1512 
1513 	return !!*data;
1514 }
1515 
1516 /**
1517  * fbnic_ethtool_msix_test - Verify behavior of NIC interrupts
1518  * @netdev: netdev device to test
1519  * @data: Pointer to results storage
1520  *
1521  * This function is meant to test the global interrupt registers and the
1522  * PCIe IP MSI-X functionality. It essentially goes through and tests
1523  * test various combinations of the set, clear, and mask bits in order to
1524  * verify the behavior is as we expect it to be from the driver.
1525  *
1526  * Return: non-zero on failure.
1527  **/
fbnic_ethtool_msix_test(struct net_device * netdev,u64 * data)1528 static int fbnic_ethtool_msix_test(struct net_device *netdev, u64 *data)
1529 {
1530 	struct fbnic_net *fbn = netdev_priv(netdev);
1531 	struct fbnic_dev *fbd = fbn->fbd;
1532 
1533 	*data = fbnic_msix_test(fbd);
1534 
1535 	return !!*data;
1536 }
1537 
fbnic_ethtool_mbx_self_test(struct net_device * netdev,u64 * data)1538 static int fbnic_ethtool_mbx_self_test(struct net_device *netdev, u64 *data)
1539 {
1540 	struct fbnic_net *fbn = netdev_priv(netdev);
1541 	struct fbnic_dev *fbd = fbn->fbd;
1542 
1543 	*data = fbnic_fw_mbx_self_test(fbd);
1544 
1545 	return !!*data;
1546 }
1547 
fbnic_self_test(struct net_device * netdev,struct ethtool_test * eth_test,u64 * data)1548 static void fbnic_self_test(struct net_device *netdev,
1549 			    struct ethtool_test *eth_test, u64 *data)
1550 {
1551 	bool if_running = netif_running(netdev);
1552 
1553 	if (fbnic_ethtool_mbx_self_test(netdev, &data[TEST_MBX]))
1554 		eth_test->flags |= ETH_TEST_FL_FAILED;
1555 
1556 	if (!(eth_test->flags & ETH_TEST_FL_OFFLINE)) {
1557 		data[TEST_REG] = 0;
1558 		data[TEST_MSIX] = 0;
1559 		return;
1560 	}
1561 
1562 	if (if_running)
1563 		netif_close(netdev);
1564 
1565 	if (fbnic_ethtool_regs_test(netdev, &data[TEST_REG]))
1566 		eth_test->flags |= ETH_TEST_FL_FAILED;
1567 
1568 	if (fbnic_ethtool_msix_test(netdev, &data[TEST_MSIX]))
1569 		eth_test->flags |= ETH_TEST_FL_FAILED;
1570 
1571 	if (if_running && netif_open(netdev, NULL)) {
1572 		netdev_err(netdev,
1573 			   "Failed to re-initialize hardware following test\n");
1574 		eth_test->flags |= ETH_TEST_FL_FAILED;
1575 	}
1576 }
1577 
fbnic_get_channels(struct net_device * netdev,struct ethtool_channels * ch)1578 static void fbnic_get_channels(struct net_device *netdev,
1579 			       struct ethtool_channels *ch)
1580 {
1581 	struct fbnic_net *fbn = netdev_priv(netdev);
1582 	struct fbnic_dev *fbd = fbn->fbd;
1583 
1584 	ch->max_rx = fbd->max_num_queues;
1585 	ch->max_tx = fbd->max_num_queues;
1586 	ch->max_combined = min(ch->max_rx, ch->max_tx);
1587 	ch->max_other =	FBNIC_NON_NAPI_VECTORS;
1588 
1589 	if (fbn->num_rx_queues > fbn->num_napi ||
1590 	    fbn->num_tx_queues > fbn->num_napi)
1591 		ch->combined_count = min(fbn->num_rx_queues,
1592 					 fbn->num_tx_queues);
1593 	else
1594 		ch->combined_count =
1595 			fbn->num_rx_queues + fbn->num_tx_queues - fbn->num_napi;
1596 	ch->rx_count = fbn->num_rx_queues - ch->combined_count;
1597 	ch->tx_count = fbn->num_tx_queues - ch->combined_count;
1598 	ch->other_count = FBNIC_NON_NAPI_VECTORS;
1599 }
1600 
fbnic_set_queues(struct fbnic_net * fbn,struct ethtool_channels * ch,unsigned int max_napis)1601 static void fbnic_set_queues(struct fbnic_net *fbn, struct ethtool_channels *ch,
1602 			     unsigned int max_napis)
1603 {
1604 	fbn->num_rx_queues = ch->rx_count + ch->combined_count;
1605 	fbn->num_tx_queues = ch->tx_count + ch->combined_count;
1606 	fbn->num_napi = min(ch->rx_count + ch->tx_count + ch->combined_count,
1607 			    max_napis);
1608 }
1609 
fbnic_set_channels(struct net_device * netdev,struct ethtool_channels * ch)1610 static int fbnic_set_channels(struct net_device *netdev,
1611 			      struct ethtool_channels *ch)
1612 {
1613 	struct fbnic_net *fbn = netdev_priv(netdev);
1614 	unsigned int max_napis, standalone;
1615 	struct fbnic_dev *fbd = fbn->fbd;
1616 	struct fbnic_net *clone;
1617 	int err;
1618 
1619 	max_napis = fbd->num_irqs - FBNIC_NON_NAPI_VECTORS;
1620 	standalone = ch->rx_count + ch->tx_count;
1621 
1622 	/* Limits for standalone queues:
1623 	 *  - each queue has its own NAPI (num_napi >= rx + tx + combined)
1624 	 *  - combining queues (combined not 0, rx or tx must be 0)
1625 	 */
1626 	if ((ch->rx_count && ch->tx_count && ch->combined_count) ||
1627 	    (standalone && standalone + ch->combined_count > max_napis) ||
1628 	    ch->rx_count + ch->combined_count > fbd->max_num_queues ||
1629 	    ch->tx_count + ch->combined_count > fbd->max_num_queues ||
1630 	    ch->other_count != FBNIC_NON_NAPI_VECTORS)
1631 		return -EINVAL;
1632 
1633 	if (!netif_running(netdev)) {
1634 		fbnic_set_queues(fbn, ch, max_napis);
1635 		fbnic_reset_indir_tbl(fbn);
1636 		return 0;
1637 	}
1638 
1639 	clone = fbnic_clone_create(fbn);
1640 	if (!clone)
1641 		return -ENOMEM;
1642 
1643 	fbnic_set_queues(clone, ch, max_napis);
1644 
1645 	err = fbnic_alloc_napi_vectors(clone);
1646 	if (err)
1647 		goto err_free_clone;
1648 
1649 	err = fbnic_alloc_resources(clone);
1650 	if (err)
1651 		goto err_free_napis;
1652 
1653 	fbnic_down_noidle(fbn);
1654 	err = fbnic_wait_all_queues_idle(fbn->fbd, true);
1655 	if (err)
1656 		goto err_start_stack;
1657 
1658 	err = fbnic_set_netif_queues(clone);
1659 	if (err)
1660 		goto err_start_stack;
1661 
1662 	/* Nothing can fail past this point */
1663 	fbnic_flush(fbn);
1664 
1665 	fbnic_clone_swap(fbn, clone);
1666 
1667 	/* Reset RSS indirection table */
1668 	fbnic_reset_indir_tbl(fbn);
1669 
1670 	fbnic_up(fbn);
1671 
1672 	fbnic_free_resources(clone);
1673 	fbnic_free_napi_vectors(clone);
1674 	fbnic_clone_free(clone);
1675 
1676 	return 0;
1677 
1678 err_start_stack:
1679 	fbnic_flush(fbn);
1680 	fbnic_up(fbn);
1681 	fbnic_free_resources(clone);
1682 err_free_napis:
1683 	fbnic_free_napi_vectors(clone);
1684 err_free_clone:
1685 	fbnic_clone_free(clone);
1686 	return err;
1687 }
1688 
1689 static int
fbnic_get_ts_info(struct net_device * netdev,struct kernel_ethtool_ts_info * tsinfo)1690 fbnic_get_ts_info(struct net_device *netdev,
1691 		  struct kernel_ethtool_ts_info *tsinfo)
1692 {
1693 	struct fbnic_net *fbn = netdev_priv(netdev);
1694 
1695 	tsinfo->phc_index = ptp_clock_index(fbn->fbd->ptp);
1696 
1697 	tsinfo->so_timestamping =
1698 		SOF_TIMESTAMPING_TX_SOFTWARE |
1699 		SOF_TIMESTAMPING_TX_HARDWARE |
1700 		SOF_TIMESTAMPING_RX_HARDWARE |
1701 		SOF_TIMESTAMPING_RAW_HARDWARE;
1702 
1703 	tsinfo->tx_types =
1704 		BIT(HWTSTAMP_TX_OFF) |
1705 		BIT(HWTSTAMP_TX_ON);
1706 
1707 	tsinfo->rx_filters =
1708 		BIT(HWTSTAMP_FILTER_NONE) |
1709 		BIT(HWTSTAMP_FILTER_PTP_V1_L4_EVENT) |
1710 		BIT(HWTSTAMP_FILTER_PTP_V2_L4_EVENT) |
1711 		BIT(HWTSTAMP_FILTER_PTP_V2_L2_EVENT) |
1712 		BIT(HWTSTAMP_FILTER_PTP_V2_EVENT) |
1713 		BIT(HWTSTAMP_FILTER_ALL);
1714 
1715 	return 0;
1716 }
1717 
fbnic_get_ts_stats(struct net_device * netdev,struct ethtool_ts_stats * ts_stats)1718 static void fbnic_get_ts_stats(struct net_device *netdev,
1719 			       struct ethtool_ts_stats *ts_stats)
1720 {
1721 	struct fbnic_net *fbn = netdev_priv(netdev);
1722 	u64 ts_packets, ts_lost;
1723 	struct fbnic_ring *ring;
1724 	unsigned int start;
1725 	int i;
1726 
1727 	ts_stats->pkts = fbn->tx_stats.twq.ts_packets;
1728 	ts_stats->lost = fbn->tx_stats.twq.ts_lost;
1729 	for (i = 0; i < fbn->num_tx_queues; i++) {
1730 		ring = fbn->tx[i];
1731 		do {
1732 			start = u64_stats_fetch_begin(&ring->stats.syncp);
1733 			ts_packets = ring->stats.twq.ts_packets;
1734 			ts_lost = ring->stats.twq.ts_lost;
1735 		} while (u64_stats_fetch_retry(&ring->stats.syncp, start));
1736 		ts_stats->pkts += ts_packets;
1737 		ts_stats->lost += ts_lost;
1738 	}
1739 }
1740 
fbnic_get_tunable(struct net_device * netdev,const struct ethtool_tunable * tun,void * data)1741 static int fbnic_get_tunable(struct net_device *netdev,
1742 			     const struct ethtool_tunable *tun,
1743 			     void *data)
1744 {
1745 	struct fbnic_net *fbn = netdev_priv(netdev);
1746 	int err = 0;
1747 
1748 	switch (tun->id) {
1749 	case ETHTOOL_PFC_PREVENTION_TOUT:
1750 		*(u16 *)data = fbn->fbd->ps_timeout;
1751 		break;
1752 	default:
1753 		err = -EOPNOTSUPP;
1754 		break;
1755 	}
1756 
1757 	return err;
1758 }
1759 
fbnic_set_tunable(struct net_device * netdev,const struct ethtool_tunable * tun,const void * data)1760 static int fbnic_set_tunable(struct net_device *netdev,
1761 			     const struct ethtool_tunable *tun,
1762 			     const void *data)
1763 {
1764 	struct fbnic_net *fbn = netdev_priv(netdev);
1765 	int err;
1766 
1767 	switch (tun->id) {
1768 	case ETHTOOL_PFC_PREVENTION_TOUT: {
1769 		u16 ps_timeout = *(u16 *)data;
1770 
1771 		err = fbnic_mac_ps_protect_to_config(fbn->fbd, ps_timeout);
1772 		break;
1773 	}
1774 	default:
1775 		err = -EOPNOTSUPP;
1776 		break;
1777 	}
1778 
1779 	return err;
1780 }
1781 
1782 static int
fbnic_get_module_eeprom_by_page(struct net_device * netdev,const struct ethtool_module_eeprom * page_data,struct netlink_ext_ack * extack)1783 fbnic_get_module_eeprom_by_page(struct net_device *netdev,
1784 				const struct ethtool_module_eeprom *page_data,
1785 				struct netlink_ext_ack *extack)
1786 {
1787 	struct fbnic_net *fbn = netdev_priv(netdev);
1788 	struct fbnic_fw_completion *fw_cmpl;
1789 	struct fbnic_dev *fbd = fbn->fbd;
1790 	int err;
1791 
1792 	if (page_data->i2c_address != 0x50) {
1793 		NL_SET_ERR_MSG_MOD(extack,
1794 				   "Invalid i2c address. Only 0x50 is supported");
1795 		return -EINVAL;
1796 	}
1797 
1798 	fw_cmpl = __fbnic_fw_alloc_cmpl(FBNIC_TLV_MSG_ID_QSFP_READ_RESP,
1799 					page_data->length);
1800 	if (!fw_cmpl)
1801 		return -ENOMEM;
1802 
1803 	/* Initialize completion and queue it for FW to process */
1804 	fw_cmpl->u.qsfp.length = page_data->length;
1805 	fw_cmpl->u.qsfp.offset = page_data->offset;
1806 	fw_cmpl->u.qsfp.page = page_data->page;
1807 	fw_cmpl->u.qsfp.bank = page_data->bank;
1808 
1809 	err = fbnic_fw_xmit_qsfp_read_msg(fbd, fw_cmpl, page_data->page,
1810 					  page_data->bank, page_data->offset,
1811 					  page_data->length);
1812 	if (err) {
1813 		NL_SET_ERR_MSG_MOD(extack,
1814 				   "Failed to transmit EEPROM read request");
1815 		goto exit_free;
1816 	}
1817 
1818 	if (!fbnic_mbx_wait_for_cmpl(fw_cmpl)) {
1819 		err = -ETIMEDOUT;
1820 		NL_SET_ERR_MSG_MOD(extack,
1821 				   "Timed out waiting for firmware response");
1822 		goto exit_cleanup;
1823 	}
1824 
1825 	if (fw_cmpl->result) {
1826 		err = fw_cmpl->result;
1827 		NL_SET_ERR_MSG_MOD(extack, "Failed to read EEPROM");
1828 		goto exit_cleanup;
1829 	}
1830 
1831 	memcpy(page_data->data, fw_cmpl->u.qsfp.data, page_data->length);
1832 
1833 exit_cleanup:
1834 	fbnic_mbx_clear_cmpl(fbd, fw_cmpl);
1835 exit_free:
1836 	fbnic_fw_put_cmpl(fw_cmpl);
1837 
1838 	return err ? : page_data->length;
1839 }
1840 
fbnic_set_counter(u64 * stat,struct fbnic_stat_counter * counter)1841 static void fbnic_set_counter(u64 *stat, struct fbnic_stat_counter *counter)
1842 {
1843 	if (counter->reported)
1844 		*stat = counter->value;
1845 }
1846 
1847 static void
fbnic_get_pause_stats(struct net_device * netdev,struct ethtool_pause_stats * pause_stats)1848 fbnic_get_pause_stats(struct net_device *netdev,
1849 		      struct ethtool_pause_stats *pause_stats)
1850 {
1851 	struct fbnic_net *fbn = netdev_priv(netdev);
1852 	struct fbnic_mac_stats *mac_stats;
1853 	struct fbnic_dev *fbd = fbn->fbd;
1854 	u64 tx_ps_events;
1855 
1856 	mac_stats = &fbd->hw_stats.mac;
1857 
1858 	fbd->mac->get_pause_stats(fbd, false, &mac_stats->pause);
1859 
1860 	pause_stats->tx_pause_frames = mac_stats->pause.tx_pause_frames.value;
1861 	pause_stats->rx_pause_frames = mac_stats->pause.rx_pause_frames.value;
1862 	tx_ps_events = mac_stats->pause.tx_pause_storm_events.value;
1863 	pause_stats->tx_pause_storm_events = tx_ps_events;
1864 }
1865 
1866 static void
fbnic_get_fec_stats(struct net_device * netdev,struct ethtool_fec_stats * fec_stats,struct ethtool_fec_hist * hist)1867 fbnic_get_fec_stats(struct net_device *netdev,
1868 		    struct ethtool_fec_stats *fec_stats,
1869 		    struct ethtool_fec_hist *hist)
1870 {
1871 	struct fbnic_net *fbn = netdev_priv(netdev);
1872 	struct fbnic_phy_stats *phy_stats;
1873 	struct fbnic_dev *fbd = fbn->fbd;
1874 
1875 	fbnic_get_hw_stats32(fbd);
1876 	phy_stats = &fbd->hw_stats.phy;
1877 
1878 	spin_lock(&fbd->hw_stats.lock);
1879 	fec_stats->corrected_blocks.total =
1880 		phy_stats->fec.corrected_blocks.value;
1881 	fec_stats->uncorrectable_blocks.total =
1882 		phy_stats->fec.uncorrectable_blocks.value;
1883 	spin_unlock(&fbd->hw_stats.lock);
1884 }
1885 
1886 static void
fbnic_get_eth_phy_stats(struct net_device * netdev,struct ethtool_eth_phy_stats * eth_phy_stats)1887 fbnic_get_eth_phy_stats(struct net_device *netdev,
1888 			struct ethtool_eth_phy_stats *eth_phy_stats)
1889 {
1890 	struct fbnic_net *fbn = netdev_priv(netdev);
1891 	struct fbnic_phy_stats *phy_stats;
1892 	struct fbnic_dev *fbd = fbn->fbd;
1893 	u64 total = 0;
1894 	int i;
1895 
1896 	fbnic_get_hw_stats32(fbd);
1897 	phy_stats = &fbd->hw_stats.phy;
1898 
1899 	spin_lock(&fbd->hw_stats.lock);
1900 	for (i = 0; i < FBNIC_PCS_MAX_LANES; i++)
1901 		total += phy_stats->pcs.SymbolErrorDuringCarrier.lanes[i].value;
1902 
1903 	eth_phy_stats->SymbolErrorDuringCarrier = total;
1904 	spin_unlock(&fbd->hw_stats.lock);
1905 }
1906 
1907 static void
fbnic_get_eth_mac_stats(struct net_device * netdev,struct ethtool_eth_mac_stats * eth_mac_stats)1908 fbnic_get_eth_mac_stats(struct net_device *netdev,
1909 			struct ethtool_eth_mac_stats *eth_mac_stats)
1910 {
1911 	struct fbnic_net *fbn = netdev_priv(netdev);
1912 	struct fbnic_mac_stats *mac_stats;
1913 	struct fbnic_dev *fbd = fbn->fbd;
1914 	const struct fbnic_mac *mac;
1915 
1916 	mac_stats = &fbd->hw_stats.mac;
1917 	mac = fbd->mac;
1918 
1919 	mac->get_eth_mac_stats(fbd, false, &mac_stats->eth_mac);
1920 
1921 	fbnic_set_counter(&eth_mac_stats->FramesTransmittedOK,
1922 			  &mac_stats->eth_mac.FramesTransmittedOK);
1923 	fbnic_set_counter(&eth_mac_stats->FramesReceivedOK,
1924 			  &mac_stats->eth_mac.FramesReceivedOK);
1925 	fbnic_set_counter(&eth_mac_stats->FrameCheckSequenceErrors,
1926 			  &mac_stats->eth_mac.FrameCheckSequenceErrors);
1927 	fbnic_set_counter(&eth_mac_stats->AlignmentErrors,
1928 			  &mac_stats->eth_mac.AlignmentErrors);
1929 	fbnic_set_counter(&eth_mac_stats->OctetsTransmittedOK,
1930 			  &mac_stats->eth_mac.OctetsTransmittedOK);
1931 	fbnic_set_counter(&eth_mac_stats->FramesLostDueToIntMACXmitError,
1932 			  &mac_stats->eth_mac.FramesLostDueToIntMACXmitError);
1933 	fbnic_set_counter(&eth_mac_stats->OctetsReceivedOK,
1934 			  &mac_stats->eth_mac.OctetsReceivedOK);
1935 	fbnic_set_counter(&eth_mac_stats->FramesLostDueToIntMACRcvError,
1936 			  &mac_stats->eth_mac.FramesLostDueToIntMACRcvError);
1937 	fbnic_set_counter(&eth_mac_stats->MulticastFramesXmittedOK,
1938 			  &mac_stats->eth_mac.MulticastFramesXmittedOK);
1939 	fbnic_set_counter(&eth_mac_stats->BroadcastFramesXmittedOK,
1940 			  &mac_stats->eth_mac.BroadcastFramesXmittedOK);
1941 	fbnic_set_counter(&eth_mac_stats->MulticastFramesReceivedOK,
1942 			  &mac_stats->eth_mac.MulticastFramesReceivedOK);
1943 	fbnic_set_counter(&eth_mac_stats->BroadcastFramesReceivedOK,
1944 			  &mac_stats->eth_mac.BroadcastFramesReceivedOK);
1945 	fbnic_set_counter(&eth_mac_stats->FrameTooLongErrors,
1946 			  &mac_stats->eth_mac.FrameTooLongErrors);
1947 }
1948 
1949 static void
fbnic_get_eth_ctrl_stats(struct net_device * netdev,struct ethtool_eth_ctrl_stats * eth_ctrl_stats)1950 fbnic_get_eth_ctrl_stats(struct net_device *netdev,
1951 			 struct ethtool_eth_ctrl_stats *eth_ctrl_stats)
1952 {
1953 	struct fbnic_net *fbn = netdev_priv(netdev);
1954 	struct fbnic_mac_stats *mac_stats;
1955 	struct fbnic_dev *fbd = fbn->fbd;
1956 
1957 	mac_stats = &fbd->hw_stats.mac;
1958 
1959 	fbd->mac->get_eth_ctrl_stats(fbd, false, &mac_stats->eth_ctrl);
1960 
1961 	eth_ctrl_stats->MACControlFramesReceived =
1962 		mac_stats->eth_ctrl.MACControlFramesReceived.value;
1963 	eth_ctrl_stats->MACControlFramesTransmitted =
1964 		mac_stats->eth_ctrl.MACControlFramesTransmitted.value;
1965 }
1966 
1967 static const struct ethtool_rmon_hist_range fbnic_rmon_ranges[] = {
1968 	{    0,   64 },
1969 	{   65,  127 },
1970 	{  128,  255 },
1971 	{  256,  511 },
1972 	{  512, 1023 },
1973 	{ 1024, 1518 },
1974 	{ 1519, 2047 },
1975 	{ 2048, 4095 },
1976 	{ 4096, 8191 },
1977 	{ 8192, 9216 },
1978 	{ 9217, FBNIC_MAX_JUMBO_FRAME_SIZE },
1979 	{}
1980 };
1981 
1982 static void
fbnic_get_rmon_stats(struct net_device * netdev,struct ethtool_rmon_stats * rmon_stats,const struct ethtool_rmon_hist_range ** ranges)1983 fbnic_get_rmon_stats(struct net_device *netdev,
1984 		     struct ethtool_rmon_stats *rmon_stats,
1985 		     const struct ethtool_rmon_hist_range **ranges)
1986 {
1987 	struct fbnic_net *fbn = netdev_priv(netdev);
1988 	struct fbnic_mac_stats *mac_stats;
1989 	struct fbnic_dev *fbd = fbn->fbd;
1990 	int i;
1991 
1992 	mac_stats = &fbd->hw_stats.mac;
1993 
1994 	fbd->mac->get_rmon_stats(fbd, false, &mac_stats->rmon);
1995 
1996 	rmon_stats->undersize_pkts =
1997 		mac_stats->rmon.undersize_pkts.value;
1998 	rmon_stats->oversize_pkts =
1999 		mac_stats->rmon.oversize_pkts.value;
2000 	rmon_stats->fragments =
2001 		mac_stats->rmon.fragments.value;
2002 	rmon_stats->jabbers =
2003 		mac_stats->rmon.jabbers.value;
2004 
2005 	for (i = 0; fbnic_rmon_ranges[i].high; i++) {
2006 		rmon_stats->hist[i] = mac_stats->rmon.hist[i].value;
2007 		rmon_stats->hist_tx[i] = mac_stats->rmon.hist_tx[i].value;
2008 	}
2009 
2010 	*ranges = fbnic_rmon_ranges;
2011 }
2012 
fbnic_get_link_ext_stats(struct net_device * netdev,struct ethtool_link_ext_stats * stats)2013 static void fbnic_get_link_ext_stats(struct net_device *netdev,
2014 				     struct ethtool_link_ext_stats *stats)
2015 {
2016 	struct fbnic_net *fbn = netdev_priv(netdev);
2017 
2018 	stats->link_down_events = fbn->link_down_events;
2019 }
2020 
2021 static const struct ethtool_ops fbnic_ethtool_ops = {
2022 	.cap_link_lanes_supported	= true,
2023 	.supported_coalesce_params	= ETHTOOL_COALESCE_USECS |
2024 					  ETHTOOL_COALESCE_RX_MAX_FRAMES,
2025 	.supported_ring_params		= ETHTOOL_RING_USE_TCP_DATA_SPLIT |
2026 					  ETHTOOL_RING_USE_HDS_THRS,
2027 	.rxfh_max_num_contexts		= FBNIC_RPC_RSS_TBL_COUNT,
2028 	.op_needs_rtnl			= ETHTOOL_OP_NEEDS_RTNL_LINKSETTINGS |
2029 					  ETHTOOL_OP_NEEDS_RTNL_GPAUSEPARAM |
2030 					  ETHTOOL_OP_NEEDS_RTNL_SPAUSEPARAM |
2031 					  ETHTOOL_OP_NEEDS_RTNL_SCHANNELS |
2032 					  ETHTOOL_OP_NEEDS_RTNL_SRINGPARAM |
2033 					  ETHTOOL_OP_NEEDS_RTNL_GLINK |
2034 					  ETHTOOL_OP_NEEDS_RTNL_TEST,
2035 	.get_drvinfo			= fbnic_get_drvinfo,
2036 	.get_regs_len			= fbnic_get_regs_len,
2037 	.get_regs			= fbnic_get_regs,
2038 	.get_link			= ethtool_op_get_link,
2039 	.get_link_ext_stats		= fbnic_get_link_ext_stats,
2040 	.get_coalesce			= fbnic_get_coalesce,
2041 	.set_coalesce			= fbnic_set_coalesce,
2042 	.get_ringparam			= fbnic_get_ringparam,
2043 	.set_ringparam			= fbnic_set_ringparam,
2044 	.get_pause_stats		= fbnic_get_pause_stats,
2045 	.get_pauseparam			= fbnic_phylink_get_pauseparam,
2046 	.set_pauseparam			= fbnic_phylink_set_pauseparam,
2047 	.self_test			= fbnic_self_test,
2048 	.get_strings			= fbnic_get_strings,
2049 	.get_ethtool_stats		= fbnic_get_ethtool_stats,
2050 	.get_sset_count			= fbnic_get_sset_count,
2051 	.get_rxnfc			= fbnic_get_rxnfc,
2052 	.set_rxnfc			= fbnic_set_rxnfc,
2053 	.get_rx_ring_count		= fbnic_get_rx_ring_count,
2054 	.get_rxfh_key_size		= fbnic_get_rxfh_key_size,
2055 	.get_rxfh_indir_size		= fbnic_get_rxfh_indir_size,
2056 	.get_rxfh			= fbnic_get_rxfh,
2057 	.set_rxfh			= fbnic_set_rxfh,
2058 	.get_rxfh_fields		= fbnic_get_rss_hash_opts,
2059 	.set_rxfh_fields		= fbnic_set_rss_hash_opts,
2060 	.create_rxfh_context		= fbnic_create_rxfh_context,
2061 	.modify_rxfh_context		= fbnic_modify_rxfh_context,
2062 	.remove_rxfh_context		= fbnic_remove_rxfh_context,
2063 	.get_channels			= fbnic_get_channels,
2064 	.set_channels			= fbnic_set_channels,
2065 	.get_ts_info			= fbnic_get_ts_info,
2066 	.get_ts_stats			= fbnic_get_ts_stats,
2067 	.get_tunable			= fbnic_get_tunable,
2068 	.set_tunable			= fbnic_set_tunable,
2069 	.get_link_ksettings		= fbnic_phylink_ethtool_ksettings_get,
2070 	.get_fec_stats			= fbnic_get_fec_stats,
2071 	.get_fecparam			= fbnic_phylink_get_fecparam,
2072 	.get_module_eeprom_by_page	= fbnic_get_module_eeprom_by_page,
2073 	.get_eth_phy_stats		= fbnic_get_eth_phy_stats,
2074 	.get_eth_mac_stats		= fbnic_get_eth_mac_stats,
2075 	.get_eth_ctrl_stats		= fbnic_get_eth_ctrl_stats,
2076 	.get_rmon_stats			= fbnic_get_rmon_stats,
2077 };
2078 
fbnic_set_ethtool_ops(struct net_device * dev)2079 void fbnic_set_ethtool_ops(struct net_device *dev)
2080 {
2081 	dev->ethtool_ops = &fbnic_ethtool_ops;
2082 }
2083