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