xref: /linux/drivers/net/ethernet/intel/idpf/idpf_ethtool.c (revision bf4afc53b77aeaa48b5409da5c8da6bb4eff7f43)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (C) 2023 Intel Corporation */
3 
4 #include "idpf.h"
5 #include "idpf_ptp.h"
6 #include "idpf_virtchnl.h"
7 
8 /**
9  * idpf_get_rx_ring_count - get RX ring count
10  * @netdev: network interface device structure
11  *
12  * Return: number of RX rings.
13  */
14 static u32 idpf_get_rx_ring_count(struct net_device *netdev)
15 {
16 	struct idpf_vport *vport;
17 	u32 num_rxq;
18 
19 	idpf_vport_ctrl_lock(netdev);
20 	vport = idpf_netdev_to_vport(netdev);
21 	num_rxq = vport->dflt_qv_rsrc.num_rxq;
22 	idpf_vport_ctrl_unlock(netdev);
23 
24 	return num_rxq;
25 }
26 
27 /**
28  * idpf_get_rxnfc - command to get RX flow classification rules
29  * @netdev: network interface device structure
30  * @cmd: ethtool rxnfc command
31  * @rule_locs: pointer to store rule locations
32  *
33  * Returns Success if the command is supported.
34  */
35 static int idpf_get_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd,
36 			  u32 *rule_locs)
37 {
38 	struct idpf_netdev_priv *np = netdev_priv(netdev);
39 	struct idpf_vport_user_config_data *user_config;
40 	struct idpf_vport_config *vport_config;
41 	struct idpf_fsteer_fltr *f;
42 	struct idpf_vport *vport;
43 	unsigned int cnt = 0;
44 	int err = 0;
45 
46 	idpf_vport_ctrl_lock(netdev);
47 	vport = idpf_netdev_to_vport(netdev);
48 	vport_config = np->adapter->vport_config[np->vport_idx];
49 	user_config = &vport_config->user_config;
50 
51 	switch (cmd->cmd) {
52 	case ETHTOOL_GRXCLSRLCNT:
53 		cmd->rule_cnt = user_config->num_fsteer_fltrs;
54 		cmd->data = idpf_fsteer_max_rules(vport);
55 		break;
56 	case ETHTOOL_GRXCLSRULE:
57 		err = -ENOENT;
58 		spin_lock_bh(&vport_config->flow_steer_list_lock);
59 		list_for_each_entry(f, &user_config->flow_steer_list, list)
60 			if (f->fs.location == cmd->fs.location) {
61 				/* Avoid infoleak from padding: zero first,
62 				 * then assign fields
63 				 */
64 				memset(&cmd->fs, 0, sizeof(cmd->fs));
65 				cmd->fs = f->fs;
66 				err = 0;
67 				break;
68 			}
69 		spin_unlock_bh(&vport_config->flow_steer_list_lock);
70 		break;
71 	case ETHTOOL_GRXCLSRLALL:
72 		cmd->data = idpf_fsteer_max_rules(vport);
73 		spin_lock_bh(&vport_config->flow_steer_list_lock);
74 		list_for_each_entry(f, &user_config->flow_steer_list, list) {
75 			if (cnt == cmd->rule_cnt) {
76 				err = -EMSGSIZE;
77 				break;
78 			}
79 			rule_locs[cnt] = f->fs.location;
80 			cnt++;
81 		}
82 		if (!err)
83 			cmd->rule_cnt = user_config->num_fsteer_fltrs;
84 		spin_unlock_bh(&vport_config->flow_steer_list_lock);
85 		break;
86 	default:
87 		break;
88 	}
89 
90 	idpf_vport_ctrl_unlock(netdev);
91 
92 	return err;
93 }
94 
95 static void idpf_fsteer_fill_ipv4(struct virtchnl2_proto_hdrs *hdrs,
96 				  struct ethtool_rx_flow_spec *fsp)
97 {
98 	struct iphdr *iph;
99 
100 	hdrs->proto_hdr[0].hdr_type = cpu_to_le32(VIRTCHNL2_PROTO_HDR_IPV4);
101 
102 	iph = (struct iphdr *)hdrs->proto_hdr[0].buffer_spec;
103 	iph->saddr = fsp->h_u.tcp_ip4_spec.ip4src;
104 	iph->daddr = fsp->h_u.tcp_ip4_spec.ip4dst;
105 
106 	iph = (struct iphdr *)hdrs->proto_hdr[0].buffer_mask;
107 	iph->saddr = fsp->m_u.tcp_ip4_spec.ip4src;
108 	iph->daddr = fsp->m_u.tcp_ip4_spec.ip4dst;
109 }
110 
111 static void idpf_fsteer_fill_udp(struct virtchnl2_proto_hdrs *hdrs,
112 				 struct ethtool_rx_flow_spec *fsp,
113 				 bool v4)
114 {
115 	struct udphdr *udph, *udpm;
116 
117 	hdrs->proto_hdr[1].hdr_type = cpu_to_le32(VIRTCHNL2_PROTO_HDR_UDP);
118 
119 	udph = (struct udphdr *)hdrs->proto_hdr[1].buffer_spec;
120 	udpm = (struct udphdr *)hdrs->proto_hdr[1].buffer_mask;
121 
122 	if (v4) {
123 		udph->source = fsp->h_u.udp_ip4_spec.psrc;
124 		udph->dest = fsp->h_u.udp_ip4_spec.pdst;
125 		udpm->source = fsp->m_u.udp_ip4_spec.psrc;
126 		udpm->dest = fsp->m_u.udp_ip4_spec.pdst;
127 	} else {
128 		udph->source = fsp->h_u.udp_ip6_spec.psrc;
129 		udph->dest = fsp->h_u.udp_ip6_spec.pdst;
130 		udpm->source = fsp->m_u.udp_ip6_spec.psrc;
131 		udpm->dest = fsp->m_u.udp_ip6_spec.pdst;
132 	}
133 }
134 
135 static void idpf_fsteer_fill_tcp(struct virtchnl2_proto_hdrs *hdrs,
136 				 struct ethtool_rx_flow_spec *fsp,
137 				 bool v4)
138 {
139 	struct tcphdr *tcph, *tcpm;
140 
141 	hdrs->proto_hdr[1].hdr_type = cpu_to_le32(VIRTCHNL2_PROTO_HDR_TCP);
142 
143 	tcph = (struct tcphdr *)hdrs->proto_hdr[1].buffer_spec;
144 	tcpm = (struct tcphdr *)hdrs->proto_hdr[1].buffer_mask;
145 
146 	if (v4) {
147 		tcph->source = fsp->h_u.tcp_ip4_spec.psrc;
148 		tcph->dest = fsp->h_u.tcp_ip4_spec.pdst;
149 		tcpm->source = fsp->m_u.tcp_ip4_spec.psrc;
150 		tcpm->dest = fsp->m_u.tcp_ip4_spec.pdst;
151 	} else {
152 		tcph->source = fsp->h_u.tcp_ip6_spec.psrc;
153 		tcph->dest = fsp->h_u.tcp_ip6_spec.pdst;
154 		tcpm->source = fsp->m_u.tcp_ip6_spec.psrc;
155 		tcpm->dest = fsp->m_u.tcp_ip6_spec.pdst;
156 	}
157 }
158 
159 /**
160  * idpf_add_flow_steer - add a Flow Steering filter
161  * @netdev: network interface device structure
162  * @cmd: command to add Flow Steering filter
163  *
164  * Return: 0 on success and negative values for failure
165  */
166 static int idpf_add_flow_steer(struct net_device *netdev,
167 			       struct ethtool_rxnfc *cmd)
168 {
169 	struct idpf_fsteer_fltr *fltr, *parent = NULL, *f;
170 	struct idpf_netdev_priv *np = netdev_priv(netdev);
171 	struct idpf_vport_user_config_data *user_config;
172 	struct ethtool_rx_flow_spec *fsp = &cmd->fs;
173 	struct virtchnl2_flow_rule_add_del *rule;
174 	struct idpf_vport_config *vport_config;
175 	struct virtchnl2_rule_action_set *acts;
176 	struct virtchnl2_flow_rule_info *info;
177 	struct virtchnl2_proto_hdrs *hdrs;
178 	struct idpf_vport *vport;
179 	u32 flow_type, q_index;
180 	u16 num_rxq;
181 	int err = 0;
182 
183 	vport = idpf_netdev_to_vport(netdev);
184 	vport_config = vport->adapter->vport_config[np->vport_idx];
185 	user_config = &vport_config->user_config;
186 	num_rxq = user_config->num_req_rx_qs;
187 
188 	flow_type = fsp->flow_type & ~(FLOW_EXT | FLOW_MAC_EXT | FLOW_RSS);
189 	if (flow_type != fsp->flow_type)
190 		return -EINVAL;
191 
192 	if (!idpf_sideband_action_ena(vport, fsp) ||
193 	    !idpf_sideband_flow_type_ena(vport, flow_type))
194 		return -EOPNOTSUPP;
195 
196 	if (user_config->num_fsteer_fltrs > idpf_fsteer_max_rules(vport))
197 		return -ENOSPC;
198 
199 	q_index = fsp->ring_cookie;
200 	if (q_index >= num_rxq)
201 		return -EINVAL;
202 
203 	rule = kzalloc_flex(*rule, rule_info, 1, GFP_KERNEL);
204 	if (!rule)
205 		return -ENOMEM;
206 
207 	fltr = kzalloc_obj(*fltr);
208 	if (!fltr) {
209 		err = -ENOMEM;
210 		goto out_free_rule;
211 	}
212 
213 	/* detect duplicate entry and reject before adding rules */
214 	spin_lock_bh(&vport_config->flow_steer_list_lock);
215 	list_for_each_entry(f, &user_config->flow_steer_list, list) {
216 		if (f->fs.location == fsp->location) {
217 			err = -EEXIST;
218 			break;
219 		}
220 
221 		if (f->fs.location > fsp->location)
222 			break;
223 		parent = f;
224 	}
225 	spin_unlock_bh(&vport_config->flow_steer_list_lock);
226 
227 	if (err)
228 		goto out;
229 
230 	rule->vport_id = cpu_to_le32(vport->vport_id);
231 	rule->count = cpu_to_le32(1);
232 	info = &rule->rule_info[0];
233 	info->rule_id = cpu_to_le32(fsp->location);
234 
235 	hdrs = &info->rule_cfg.proto_hdrs;
236 	hdrs->tunnel_level = 0;
237 	hdrs->count = cpu_to_le32(2);
238 
239 	acts = &info->rule_cfg.action_set;
240 	acts->count = cpu_to_le32(1);
241 	acts->actions[0].action_type = cpu_to_le32(VIRTCHNL2_ACTION_QUEUE);
242 	acts->actions[0].act_conf.q_id = cpu_to_le32(q_index);
243 
244 	switch (flow_type) {
245 	case UDP_V4_FLOW:
246 		idpf_fsteer_fill_ipv4(hdrs, fsp);
247 		idpf_fsteer_fill_udp(hdrs, fsp, true);
248 		break;
249 	case TCP_V4_FLOW:
250 		idpf_fsteer_fill_ipv4(hdrs, fsp);
251 		idpf_fsteer_fill_tcp(hdrs, fsp, true);
252 		break;
253 	default:
254 		err = -EINVAL;
255 		goto out;
256 	}
257 
258 	err = idpf_add_del_fsteer_filters(vport->adapter, rule,
259 					  VIRTCHNL2_OP_ADD_FLOW_RULE);
260 	if (err)
261 		goto out;
262 
263 	if (info->status != cpu_to_le32(VIRTCHNL2_FLOW_RULE_SUCCESS)) {
264 		err = -EIO;
265 		goto out;
266 	}
267 
268 	/* Save a copy of the user's flow spec so ethtool can later retrieve it */
269 	fltr->fs = *fsp;
270 
271 	spin_lock_bh(&vport_config->flow_steer_list_lock);
272 	parent ? list_add(&fltr->list, &parent->list) :
273 		 list_add(&fltr->list, &user_config->flow_steer_list);
274 
275 	user_config->num_fsteer_fltrs++;
276 	spin_unlock_bh(&vport_config->flow_steer_list_lock);
277 	goto out_free_rule;
278 
279 out:
280 	kfree(fltr);
281 out_free_rule:
282 	kfree(rule);
283 	return err;
284 }
285 
286 /**
287  * idpf_del_flow_steer - delete a Flow Steering filter
288  * @netdev: network interface device structure
289  * @cmd: command to add Flow Steering filter
290  *
291  * Return: 0 on success and negative values for failure
292  */
293 static int idpf_del_flow_steer(struct net_device *netdev,
294 			       struct ethtool_rxnfc *cmd)
295 {
296 	struct idpf_netdev_priv *np = netdev_priv(netdev);
297 	struct idpf_vport_user_config_data *user_config;
298 	struct ethtool_rx_flow_spec *fsp = &cmd->fs;
299 	struct virtchnl2_flow_rule_add_del *rule;
300 	struct idpf_vport_config *vport_config;
301 	struct virtchnl2_flow_rule_info *info;
302 	struct idpf_fsteer_fltr *f, *iter;
303 	struct idpf_vport *vport;
304 	int err;
305 
306 	vport = idpf_netdev_to_vport(netdev);
307 	vport_config = vport->adapter->vport_config[np->vport_idx];
308 	user_config = &vport_config->user_config;
309 
310 	if (!idpf_sideband_action_ena(vport, fsp))
311 		return -EOPNOTSUPP;
312 
313 	rule = kzalloc_flex(*rule, rule_info, 1, GFP_KERNEL);
314 	if (!rule)
315 		return -ENOMEM;
316 
317 	rule->vport_id = cpu_to_le32(vport->vport_id);
318 	rule->count = cpu_to_le32(1);
319 	info = &rule->rule_info[0];
320 	info->rule_id = cpu_to_le32(fsp->location);
321 
322 	err = idpf_add_del_fsteer_filters(vport->adapter, rule,
323 					  VIRTCHNL2_OP_DEL_FLOW_RULE);
324 	if (err)
325 		goto out;
326 
327 	if (info->status != cpu_to_le32(VIRTCHNL2_FLOW_RULE_SUCCESS)) {
328 		err = -EIO;
329 		goto out;
330 	}
331 
332 	spin_lock_bh(&vport_config->flow_steer_list_lock);
333 	list_for_each_entry_safe(f, iter,
334 				 &user_config->flow_steer_list, list) {
335 		if (f->fs.location == fsp->location) {
336 			list_del(&f->list);
337 			kfree(f);
338 			user_config->num_fsteer_fltrs--;
339 			goto out_unlock;
340 		}
341 	}
342 	err = -ENOENT;
343 
344 out_unlock:
345 	spin_unlock_bh(&vport_config->flow_steer_list_lock);
346 out:
347 	kfree(rule);
348 	return err;
349 }
350 
351 static int idpf_set_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd)
352 {
353 	int ret = -EOPNOTSUPP;
354 
355 	idpf_vport_ctrl_lock(netdev);
356 	switch (cmd->cmd) {
357 	case ETHTOOL_SRXCLSRLINS:
358 		ret = idpf_add_flow_steer(netdev, cmd);
359 		break;
360 	case ETHTOOL_SRXCLSRLDEL:
361 		ret = idpf_del_flow_steer(netdev, cmd);
362 		break;
363 	default:
364 		break;
365 	}
366 
367 	idpf_vport_ctrl_unlock(netdev);
368 	return ret;
369 }
370 
371 /**
372  * idpf_get_rxfh_key_size - get the RSS hash key size
373  * @netdev: network interface device structure
374  *
375  * Returns the key size on success, error value on failure.
376  */
377 static u32 idpf_get_rxfh_key_size(struct net_device *netdev)
378 {
379 	struct idpf_netdev_priv *np = netdev_priv(netdev);
380 	struct idpf_vport_user_config_data *user_config;
381 
382 	if (!idpf_is_cap_ena_all(np->adapter, IDPF_RSS_CAPS, IDPF_CAP_RSS))
383 		return 0;
384 
385 	user_config = &np->adapter->vport_config[np->vport_idx]->user_config;
386 
387 	return user_config->rss_data.rss_key_size;
388 }
389 
390 /**
391  * idpf_get_rxfh_indir_size - get the rx flow hash indirection table size
392  * @netdev: network interface device structure
393  *
394  * Returns the table size on success, error value on failure.
395  */
396 static u32 idpf_get_rxfh_indir_size(struct net_device *netdev)
397 {
398 	struct idpf_netdev_priv *np = netdev_priv(netdev);
399 	struct idpf_vport_user_config_data *user_config;
400 
401 	if (!idpf_is_cap_ena_all(np->adapter, IDPF_RSS_CAPS, IDPF_CAP_RSS))
402 		return 0;
403 
404 	user_config = &np->adapter->vport_config[np->vport_idx]->user_config;
405 
406 	return user_config->rss_data.rss_lut_size;
407 }
408 
409 /**
410  * idpf_get_rxfh - get the rx flow hash indirection table
411  * @netdev: network interface device structure
412  * @rxfh: pointer to param struct (indir, key, hfunc)
413  *
414  * RSS LUT and Key information are read from driver's cached
415  * copy. When rxhash is off, rss lut will be displayed as zeros.
416  *
417  * Return: 0 on success, -errno otherwise.
418  */
419 static int idpf_get_rxfh(struct net_device *netdev,
420 			 struct ethtool_rxfh_param *rxfh)
421 {
422 	struct idpf_netdev_priv *np = netdev_priv(netdev);
423 	struct idpf_rss_data *rss_data;
424 	struct idpf_adapter *adapter;
425 	struct idpf_vport *vport;
426 	bool rxhash_ena;
427 	int err = 0;
428 	u16 i;
429 
430 	idpf_vport_ctrl_lock(netdev);
431 	vport = idpf_netdev_to_vport(netdev);
432 
433 	adapter = np->adapter;
434 
435 	if (!idpf_is_cap_ena_all(adapter, IDPF_RSS_CAPS, IDPF_CAP_RSS)) {
436 		err = -EOPNOTSUPP;
437 		goto unlock_mutex;
438 	}
439 
440 	rss_data = &adapter->vport_config[np->vport_idx]->user_config.rss_data;
441 
442 	rxhash_ena = idpf_is_feature_ena(vport, NETIF_F_RXHASH);
443 	rxfh->hfunc = ETH_RSS_HASH_TOP;
444 
445 	if (rxfh->key)
446 		memcpy(rxfh->key, rss_data->rss_key, rss_data->rss_key_size);
447 
448 	if (rxfh->indir) {
449 		for (i = 0; i < rss_data->rss_lut_size; i++)
450 			rxfh->indir[i] = rxhash_ena ? rss_data->rss_lut[i] : 0;
451 	}
452 
453 unlock_mutex:
454 	idpf_vport_ctrl_unlock(netdev);
455 
456 	return err;
457 }
458 
459 /**
460  * idpf_set_rxfh - set the rx flow hash indirection table
461  * @netdev: network interface device structure
462  * @rxfh: pointer to param struct (indir, key, hfunc)
463  * @extack: extended ACK from the Netlink message
464  *
465  * Returns -EINVAL if the table specifies an invalid queue id, otherwise
466  * returns 0 after programming the table.
467  */
468 static int idpf_set_rxfh(struct net_device *netdev,
469 			 struct ethtool_rxfh_param *rxfh,
470 			 struct netlink_ext_ack *extack)
471 {
472 	struct idpf_netdev_priv *np = netdev_priv(netdev);
473 	struct idpf_rss_data *rss_data;
474 	struct idpf_adapter *adapter;
475 	struct idpf_vport *vport;
476 	int err = 0;
477 	u16 lut;
478 
479 	idpf_vport_ctrl_lock(netdev);
480 	vport = idpf_netdev_to_vport(netdev);
481 
482 	adapter = vport->adapter;
483 
484 	if (!idpf_is_cap_ena_all(adapter, IDPF_RSS_CAPS, IDPF_CAP_RSS)) {
485 		err = -EOPNOTSUPP;
486 		goto unlock_mutex;
487 	}
488 
489 	rss_data = &adapter->vport_config[vport->idx]->user_config.rss_data;
490 
491 	if (rxfh->hfunc != ETH_RSS_HASH_NO_CHANGE &&
492 	    rxfh->hfunc != ETH_RSS_HASH_TOP) {
493 		err = -EOPNOTSUPP;
494 		goto unlock_mutex;
495 	}
496 
497 	if (rxfh->key)
498 		memcpy(rss_data->rss_key, rxfh->key, rss_data->rss_key_size);
499 
500 	if (rxfh->indir) {
501 		for (lut = 0; lut < rss_data->rss_lut_size; lut++)
502 			rss_data->rss_lut[lut] = rxfh->indir[lut];
503 	}
504 
505 	if (test_bit(IDPF_VPORT_UP, np->state))
506 		err = idpf_config_rss(vport, rss_data);
507 
508 unlock_mutex:
509 	idpf_vport_ctrl_unlock(netdev);
510 
511 	return err;
512 }
513 
514 /**
515  * idpf_get_channels: get the number of channels supported by the device
516  * @netdev: network interface device structure
517  * @ch: channel information structure
518  *
519  * Report maximum of TX and RX. Report one extra channel to match our MailBox
520  * Queue.
521  */
522 static void idpf_get_channels(struct net_device *netdev,
523 			      struct ethtool_channels *ch)
524 {
525 	struct idpf_netdev_priv *np = netdev_priv(netdev);
526 	struct idpf_vport_config *vport_config;
527 	u16 num_txq, num_rxq;
528 	u16 combined;
529 
530 	vport_config = np->adapter->vport_config[np->vport_idx];
531 
532 	num_txq = vport_config->user_config.num_req_tx_qs;
533 	num_rxq = vport_config->user_config.num_req_rx_qs;
534 
535 	combined = min(num_txq, num_rxq);
536 
537 	/* Report maximum channels */
538 	ch->max_combined = min_t(u16, vport_config->max_q.max_txq,
539 				 vport_config->max_q.max_rxq);
540 	ch->max_rx = vport_config->max_q.max_rxq;
541 	ch->max_tx = vport_config->max_q.max_txq;
542 
543 	ch->max_other = IDPF_MAX_MBXQ;
544 	ch->other_count = IDPF_MAX_MBXQ;
545 
546 	ch->combined_count = combined;
547 	ch->rx_count = num_rxq - combined;
548 	ch->tx_count = num_txq - combined;
549 }
550 
551 /**
552  * idpf_set_channels: set the new channel count
553  * @netdev: network interface device structure
554  * @ch: channel information structure
555  *
556  * Negotiate a new number of channels with CP. Returns 0 on success, negative
557  * on failure.
558  */
559 static int idpf_set_channels(struct net_device *netdev,
560 			     struct ethtool_channels *ch)
561 {
562 	struct idpf_vport_config *vport_config;
563 	unsigned int num_req_tx_q;
564 	unsigned int num_req_rx_q;
565 	struct idpf_vport *vport;
566 	u16 num_txq, num_rxq;
567 	struct device *dev;
568 	int err = 0;
569 	u16 idx;
570 
571 	if (ch->rx_count && ch->tx_count) {
572 		netdev_err(netdev, "Dedicated RX or TX channels cannot be used simultaneously\n");
573 		return -EINVAL;
574 	}
575 
576 	idpf_vport_ctrl_lock(netdev);
577 	vport = idpf_netdev_to_vport(netdev);
578 
579 	idx = vport->idx;
580 	vport_config = vport->adapter->vport_config[idx];
581 
582 	num_txq = vport_config->user_config.num_req_tx_qs;
583 	num_rxq = vport_config->user_config.num_req_rx_qs;
584 
585 	num_req_tx_q = ch->combined_count + ch->tx_count;
586 	num_req_rx_q = ch->combined_count + ch->rx_count;
587 
588 	dev = &vport->adapter->pdev->dev;
589 	/* It's possible to specify number of queues that exceeds max.
590 	 * Stack checks max combined_count and max [tx|rx]_count but not the
591 	 * max combined_count + [tx|rx]_count. These checks should catch that.
592 	 */
593 	if (num_req_tx_q > vport_config->max_q.max_txq) {
594 		dev_info(dev, "Maximum TX queues is %d\n",
595 			 vport_config->max_q.max_txq);
596 		err = -EINVAL;
597 		goto unlock_mutex;
598 	}
599 	if (num_req_rx_q > vport_config->max_q.max_rxq) {
600 		dev_info(dev, "Maximum RX queues is %d\n",
601 			 vport_config->max_q.max_rxq);
602 		err = -EINVAL;
603 		goto unlock_mutex;
604 	}
605 
606 	if (num_req_tx_q == num_txq && num_req_rx_q == num_rxq)
607 		goto unlock_mutex;
608 
609 	vport_config->user_config.num_req_tx_qs = num_req_tx_q;
610 	vport_config->user_config.num_req_rx_qs = num_req_rx_q;
611 
612 	err = idpf_initiate_soft_reset(vport, IDPF_SR_Q_CHANGE);
613 	if (err) {
614 		/* roll back queue change */
615 		vport_config->user_config.num_req_tx_qs = num_txq;
616 		vport_config->user_config.num_req_rx_qs = num_rxq;
617 	}
618 
619 unlock_mutex:
620 	idpf_vport_ctrl_unlock(netdev);
621 
622 	return err;
623 }
624 
625 /**
626  * idpf_get_ringparam - Get ring parameters
627  * @netdev: network interface device structure
628  * @ring: ethtool ringparam structure
629  * @kring: unused
630  * @ext_ack: unused
631  *
632  * Returns current ring parameters. TX and RX rings are reported separately,
633  * but the number of rings is not reported.
634  */
635 static void idpf_get_ringparam(struct net_device *netdev,
636 			       struct ethtool_ringparam *ring,
637 			       struct kernel_ethtool_ringparam *kring,
638 			       struct netlink_ext_ack *ext_ack)
639 {
640 	struct idpf_vport *vport;
641 
642 	idpf_vport_ctrl_lock(netdev);
643 	vport = idpf_netdev_to_vport(netdev);
644 
645 	ring->rx_max_pending = IDPF_MAX_RXQ_DESC;
646 	ring->tx_max_pending = IDPF_MAX_TXQ_DESC;
647 	ring->rx_pending = vport->dflt_qv_rsrc.rxq_desc_count;
648 	ring->tx_pending = vport->dflt_qv_rsrc.txq_desc_count;
649 
650 	kring->tcp_data_split = idpf_vport_get_hsplit(vport);
651 
652 	idpf_vport_ctrl_unlock(netdev);
653 }
654 
655 /**
656  * idpf_set_ringparam - Set ring parameters
657  * @netdev: network interface device structure
658  * @ring: ethtool ringparam structure
659  * @kring: unused
660  * @ext_ack: unused
661  *
662  * Sets ring parameters. TX and RX rings are controlled separately, but the
663  * number of rings is not specified, so all rings get the same settings.
664  */
665 static int idpf_set_ringparam(struct net_device *netdev,
666 			      struct ethtool_ringparam *ring,
667 			      struct kernel_ethtool_ringparam *kring,
668 			      struct netlink_ext_ack *ext_ack)
669 {
670 	struct idpf_vport_user_config_data *config_data;
671 	u32 new_rx_count, new_tx_count;
672 	struct idpf_q_vec_rsrc *rsrc;
673 	struct idpf_vport *vport;
674 	int err = 0;
675 	u16 idx;
676 
677 	idpf_vport_ctrl_lock(netdev);
678 	vport = idpf_netdev_to_vport(netdev);
679 
680 	idx = vport->idx;
681 
682 	if (ring->tx_pending < IDPF_MIN_TXQ_DESC) {
683 		netdev_err(netdev, "Descriptors requested (Tx: %u) is less than min supported (%u)\n",
684 			   ring->tx_pending,
685 			   IDPF_MIN_TXQ_DESC);
686 		err = -EINVAL;
687 		goto unlock_mutex;
688 	}
689 
690 	if (ring->rx_pending < IDPF_MIN_RXQ_DESC) {
691 		netdev_err(netdev, "Descriptors requested (Rx: %u) is less than min supported (%u)\n",
692 			   ring->rx_pending,
693 			   IDPF_MIN_RXQ_DESC);
694 		err = -EINVAL;
695 		goto unlock_mutex;
696 	}
697 
698 	new_rx_count = ALIGN(ring->rx_pending, IDPF_REQ_RXQ_DESC_MULTIPLE);
699 	if (new_rx_count != ring->rx_pending)
700 		netdev_info(netdev, "Requested Rx descriptor count rounded up to %u\n",
701 			    new_rx_count);
702 
703 	new_tx_count = ALIGN(ring->tx_pending, IDPF_REQ_DESC_MULTIPLE);
704 	if (new_tx_count != ring->tx_pending)
705 		netdev_info(netdev, "Requested Tx descriptor count rounded up to %u\n",
706 			    new_tx_count);
707 
708 	rsrc = &vport->dflt_qv_rsrc;
709 	if (new_tx_count == rsrc->txq_desc_count &&
710 	    new_rx_count == rsrc->rxq_desc_count &&
711 	    kring->tcp_data_split == idpf_vport_get_hsplit(vport))
712 		goto unlock_mutex;
713 
714 	if (!idpf_vport_set_hsplit(vport, kring->tcp_data_split)) {
715 		NL_SET_ERR_MSG_MOD(ext_ack,
716 				   "setting TCP data split is not supported");
717 		err = -EOPNOTSUPP;
718 
719 		goto unlock_mutex;
720 	}
721 
722 	config_data = &vport->adapter->vport_config[idx]->user_config;
723 	config_data->num_req_txq_desc = new_tx_count;
724 	config_data->num_req_rxq_desc = new_rx_count;
725 
726 	/* Since we adjusted the RX completion queue count, the RX buffer queue
727 	 * descriptor count needs to be adjusted as well
728 	 */
729 	for (unsigned int i = 0; i < rsrc->num_bufqs_per_qgrp; i++)
730 		rsrc->bufq_desc_count[i] =
731 			IDPF_RX_BUFQ_DESC_COUNT(new_rx_count,
732 						rsrc->num_bufqs_per_qgrp);
733 
734 	err = idpf_initiate_soft_reset(vport, IDPF_SR_Q_DESC_CHANGE);
735 
736 unlock_mutex:
737 	idpf_vport_ctrl_unlock(netdev);
738 
739 	return err;
740 }
741 
742 /**
743  * struct idpf_stats - definition for an ethtool statistic
744  * @stat_string: statistic name to display in ethtool -S output
745  * @sizeof_stat: the sizeof() the stat, must be no greater than sizeof(u64)
746  * @stat_offset: offsetof() the stat from a base pointer
747  *
748  * This structure defines a statistic to be added to the ethtool stats buffer.
749  * It defines a statistic as offset from a common base pointer. Stats should
750  * be defined in constant arrays using the IDPF_STAT macro, with every element
751  * of the array using the same _type for calculating the sizeof_stat and
752  * stat_offset.
753  *
754  * The @sizeof_stat is expected to be sizeof(u8), sizeof(u16), sizeof(u32) or
755  * sizeof(u64). Other sizes are not expected and will produce a WARN_ONCE from
756  * the idpf_add_ethtool_stat() helper function.
757  *
758  * The @stat_string is interpreted as a format string, allowing formatted
759  * values to be inserted while looping over multiple structures for a given
760  * statistics array. Thus, every statistic string in an array should have the
761  * same type and number of format specifiers, to be formatted by variadic
762  * arguments to the idpf_add_stat_string() helper function.
763  */
764 struct idpf_stats {
765 	char stat_string[ETH_GSTRING_LEN];
766 	int sizeof_stat;
767 	int stat_offset;
768 };
769 
770 /* Helper macro to define an idpf_stat structure with proper size and type.
771  * Use this when defining constant statistics arrays. Note that @_type expects
772  * only a type name and is used multiple times.
773  */
774 #define IDPF_STAT(_type, _name, _stat) { \
775 	.stat_string = _name, \
776 	.sizeof_stat = sizeof_field(_type, _stat), \
777 	.stat_offset = offsetof(_type, _stat) \
778 }
779 
780 /* Helper macros for defining some statistics related to queues */
781 #define IDPF_RX_QUEUE_STAT(_name, _stat) \
782 	IDPF_STAT(struct idpf_rx_queue, _name, _stat)
783 #define IDPF_TX_QUEUE_STAT(_name, _stat) \
784 	IDPF_STAT(struct idpf_tx_queue, _name, _stat)
785 
786 /* Stats associated with a Tx queue */
787 static const struct idpf_stats idpf_gstrings_tx_queue_stats[] = {
788 	IDPF_TX_QUEUE_STAT("pkts", q_stats.packets),
789 	IDPF_TX_QUEUE_STAT("bytes", q_stats.bytes),
790 	IDPF_TX_QUEUE_STAT("lso_pkts", q_stats.lso_pkts),
791 };
792 
793 /* Stats associated with an Rx queue */
794 static const struct idpf_stats idpf_gstrings_rx_queue_stats[] = {
795 	IDPF_RX_QUEUE_STAT("pkts", q_stats.packets),
796 	IDPF_RX_QUEUE_STAT("bytes", q_stats.bytes),
797 	IDPF_RX_QUEUE_STAT("rx_gro_hw_pkts", q_stats.rsc_pkts),
798 };
799 
800 #define IDPF_TX_QUEUE_STATS_LEN		ARRAY_SIZE(idpf_gstrings_tx_queue_stats)
801 #define IDPF_RX_QUEUE_STATS_LEN		ARRAY_SIZE(idpf_gstrings_rx_queue_stats)
802 
803 #define IDPF_PORT_STAT(_name, _stat) \
804 	IDPF_STAT(struct idpf_vport,  _name, _stat)
805 
806 static const struct idpf_stats idpf_gstrings_port_stats[] = {
807 	IDPF_PORT_STAT("rx-csum_errors", port_stats.rx_hw_csum_err),
808 	IDPF_PORT_STAT("rx-hsplit", port_stats.rx_hsplit),
809 	IDPF_PORT_STAT("rx-hsplit_hbo", port_stats.rx_hsplit_hbo),
810 	IDPF_PORT_STAT("rx-bad_descs", port_stats.rx_bad_descs),
811 	IDPF_PORT_STAT("tx-skb_drops", port_stats.tx_drops),
812 	IDPF_PORT_STAT("tx-dma_map_errs", port_stats.tx_dma_map_errs),
813 	IDPF_PORT_STAT("tx-linearized_pkts", port_stats.tx_linearize),
814 	IDPF_PORT_STAT("tx-busy_events", port_stats.tx_busy),
815 	IDPF_PORT_STAT("rx-unicast_pkts", port_stats.vport_stats.rx_unicast),
816 	IDPF_PORT_STAT("rx-multicast_pkts", port_stats.vport_stats.rx_multicast),
817 	IDPF_PORT_STAT("rx-broadcast_pkts", port_stats.vport_stats.rx_broadcast),
818 	IDPF_PORT_STAT("rx-unknown_protocol", port_stats.vport_stats.rx_unknown_protocol),
819 	IDPF_PORT_STAT("tx-unicast_pkts", port_stats.vport_stats.tx_unicast),
820 	IDPF_PORT_STAT("tx-multicast_pkts", port_stats.vport_stats.tx_multicast),
821 	IDPF_PORT_STAT("tx-broadcast_pkts", port_stats.vport_stats.tx_broadcast),
822 };
823 
824 #define IDPF_PORT_STATS_LEN ARRAY_SIZE(idpf_gstrings_port_stats)
825 
826 /**
827  * __idpf_add_qstat_strings - copy stat strings into ethtool buffer
828  * @p: ethtool supplied buffer
829  * @stats: stat definitions array
830  * @size: size of the stats array
831  * @type: stat type
832  * @idx: stat index
833  *
834  * Format and copy the strings described by stats into the buffer pointed at
835  * by p.
836  */
837 static void __idpf_add_qstat_strings(u8 **p, const struct idpf_stats *stats,
838 				     const unsigned int size, const char *type,
839 				     unsigned int idx)
840 {
841 	unsigned int i;
842 
843 	for (i = 0; i < size; i++)
844 		ethtool_sprintf(p, "%s_q-%u_%s",
845 				type, idx, stats[i].stat_string);
846 }
847 
848 /**
849  * idpf_add_qstat_strings - Copy queue stat strings into ethtool buffer
850  * @p: ethtool supplied buffer
851  * @stats: stat definitions array
852  * @type: stat type
853  * @idx: stat idx
854  *
855  * Format and copy the strings described by the const static stats value into
856  * the buffer pointed at by p.
857  *
858  * The parameter @stats is evaluated twice, so parameters with side effects
859  * should be avoided. Additionally, stats must be an array such that
860  * ARRAY_SIZE can be called on it.
861  */
862 #define idpf_add_qstat_strings(p, stats, type, idx) \
863 	__idpf_add_qstat_strings(p, stats, ARRAY_SIZE(stats), type, idx)
864 
865 /**
866  * idpf_add_stat_strings - Copy port stat strings into ethtool buffer
867  * @p: ethtool buffer
868  * @stats: struct to copy from
869  * @size: size of stats array to copy from
870  */
871 static void idpf_add_stat_strings(u8 **p, const struct idpf_stats *stats,
872 				  const unsigned int size)
873 {
874 	unsigned int i;
875 
876 	for (i = 0; i < size; i++)
877 		ethtool_puts(p, stats[i].stat_string);
878 }
879 
880 /**
881  * idpf_get_stat_strings - Get stat strings
882  * @netdev: network interface device structure
883  * @data: buffer for string data
884  *
885  * Builds the statistics string table
886  */
887 static void idpf_get_stat_strings(struct net_device *netdev, u8 *data)
888 {
889 	struct idpf_netdev_priv *np = netdev_priv(netdev);
890 	struct idpf_vport_config *vport_config;
891 	unsigned int i;
892 
893 	idpf_add_stat_strings(&data, idpf_gstrings_port_stats,
894 			      IDPF_PORT_STATS_LEN);
895 
896 	vport_config = np->adapter->vport_config[np->vport_idx];
897 	/* It's critical that we always report a constant number of strings and
898 	 * that the strings are reported in the same order regardless of how
899 	 * many queues are actually in use.
900 	 */
901 	for (i = 0; i < vport_config->max_q.max_txq; i++)
902 		idpf_add_qstat_strings(&data, idpf_gstrings_tx_queue_stats,
903 				       "tx", i);
904 
905 	for (i = 0; i < vport_config->max_q.max_rxq; i++)
906 		idpf_add_qstat_strings(&data, idpf_gstrings_rx_queue_stats,
907 				       "rx", i);
908 }
909 
910 /**
911  * idpf_get_strings - Get string set
912  * @netdev: network interface device structure
913  * @sset: id of string set
914  * @data: buffer for string data
915  *
916  * Builds string tables for various string sets
917  */
918 static void idpf_get_strings(struct net_device *netdev, u32 sset, u8 *data)
919 {
920 	switch (sset) {
921 	case ETH_SS_STATS:
922 		idpf_get_stat_strings(netdev, data);
923 		break;
924 	default:
925 		break;
926 	}
927 }
928 
929 /**
930  * idpf_get_sset_count - Get length of string set
931  * @netdev: network interface device structure
932  * @sset: id of string set
933  *
934  * Reports size of various string tables.
935  */
936 static int idpf_get_sset_count(struct net_device *netdev, int sset)
937 {
938 	struct idpf_netdev_priv *np = netdev_priv(netdev);
939 	struct idpf_vport_config *vport_config;
940 	u16 max_txq, max_rxq;
941 
942 	if (sset != ETH_SS_STATS)
943 		return -EINVAL;
944 
945 	vport_config = np->adapter->vport_config[np->vport_idx];
946 	/* This size reported back here *must* be constant throughout the
947 	 * lifecycle of the netdevice, i.e. we must report the maximum length
948 	 * even for queues that don't technically exist.  This is due to the
949 	 * fact that this userspace API uses three separate ioctl calls to get
950 	 * stats data but has no way to communicate back to userspace when that
951 	 * size has changed, which can typically happen as a result of changing
952 	 * number of queues. If the number/order of stats change in the middle
953 	 * of this call chain it will lead to userspace crashing/accessing bad
954 	 * data through buffer under/overflow.
955 	 */
956 	max_txq = vport_config->max_q.max_txq;
957 	max_rxq = vport_config->max_q.max_rxq;
958 
959 	return IDPF_PORT_STATS_LEN + (IDPF_TX_QUEUE_STATS_LEN * max_txq) +
960 	       (IDPF_RX_QUEUE_STATS_LEN * max_rxq);
961 }
962 
963 /**
964  * idpf_add_one_ethtool_stat - copy the stat into the supplied buffer
965  * @data: location to store the stat value
966  * @pstat: old stat pointer to copy from
967  * @stat: the stat definition
968  *
969  * Copies the stat data defined by the pointer and stat structure pair into
970  * the memory supplied as data. If the pointer is null, data will be zero'd.
971  */
972 static void idpf_add_one_ethtool_stat(u64 *data, const void *pstat,
973 				      const struct idpf_stats *stat)
974 {
975 	char *p;
976 
977 	if (!pstat) {
978 		/* Ensure that the ethtool data buffer is zero'd for any stats
979 		 * which don't have a valid pointer.
980 		 */
981 		*data = 0;
982 		return;
983 	}
984 
985 	p = (char *)pstat + stat->stat_offset;
986 	switch (stat->sizeof_stat) {
987 	case sizeof(u64):
988 		*data = *((u64 *)p);
989 		break;
990 	case sizeof(u32):
991 		*data = *((u32 *)p);
992 		break;
993 	case sizeof(u16):
994 		*data = *((u16 *)p);
995 		break;
996 	case sizeof(u8):
997 		*data = *((u8 *)p);
998 		break;
999 	default:
1000 		WARN_ONCE(1, "unexpected stat size for %s",
1001 			  stat->stat_string);
1002 		*data = 0;
1003 	}
1004 }
1005 
1006 /**
1007  * idpf_add_queue_stats - copy queue statistics into supplied buffer
1008  * @data: ethtool stats buffer
1009  * @q: the queue to copy
1010  * @type: type of the queue
1011  *
1012  * Queue statistics must be copied while protected by u64_stats_fetch_begin,
1013  * so we can't directly use idpf_add_ethtool_stats. Assumes that queue stats
1014  * are defined in idpf_gstrings_queue_stats. If the queue pointer is null,
1015  * zero out the queue stat values and update the data pointer. Otherwise
1016  * safely copy the stats from the queue into the supplied buffer and update
1017  * the data pointer when finished.
1018  *
1019  * This function expects to be called while under rcu_read_lock().
1020  */
1021 static void idpf_add_queue_stats(u64 **data, const void *q,
1022 				 enum virtchnl2_queue_type type)
1023 {
1024 	const struct u64_stats_sync *stats_sync;
1025 	const struct idpf_stats *stats;
1026 	unsigned int start;
1027 	unsigned int size;
1028 	unsigned int i;
1029 
1030 	if (type == VIRTCHNL2_QUEUE_TYPE_RX) {
1031 		size = IDPF_RX_QUEUE_STATS_LEN;
1032 		stats = idpf_gstrings_rx_queue_stats;
1033 		stats_sync = &((const struct idpf_rx_queue *)q)->stats_sync;
1034 	} else {
1035 		size = IDPF_TX_QUEUE_STATS_LEN;
1036 		stats = idpf_gstrings_tx_queue_stats;
1037 		stats_sync = &((const struct idpf_tx_queue *)q)->stats_sync;
1038 	}
1039 
1040 	/* To avoid invalid statistics values, ensure that we keep retrying
1041 	 * the copy until we get a consistent value according to
1042 	 * u64_stats_fetch_retry.
1043 	 */
1044 	do {
1045 		start = u64_stats_fetch_begin(stats_sync);
1046 		for (i = 0; i < size; i++)
1047 			idpf_add_one_ethtool_stat(&(*data)[i], q, &stats[i]);
1048 	} while (u64_stats_fetch_retry(stats_sync, start));
1049 
1050 	/* Once we successfully copy the stats in, update the data pointer */
1051 	*data += size;
1052 }
1053 
1054 /**
1055  * idpf_add_empty_queue_stats - Add stats for a non-existent queue
1056  * @data: pointer to data buffer
1057  * @qtype: type of data queue
1058  *
1059  * We must report a constant length of stats back to userspace regardless of
1060  * how many queues are actually in use because stats collection happens over
1061  * three separate ioctls and there's no way to notify userspace the size
1062  * changed between those calls. This adds empty to data to the stats since we
1063  * don't have a real queue to refer to for this stats slot.
1064  */
1065 static void idpf_add_empty_queue_stats(u64 **data, u16 qtype)
1066 {
1067 	unsigned int i;
1068 	int stats_len;
1069 
1070 	if (qtype == VIRTCHNL2_QUEUE_TYPE_RX)
1071 		stats_len = IDPF_RX_QUEUE_STATS_LEN;
1072 	else
1073 		stats_len = IDPF_TX_QUEUE_STATS_LEN;
1074 
1075 	for (i = 0; i < stats_len; i++)
1076 		(*data)[i] = 0;
1077 	*data += stats_len;
1078 }
1079 
1080 /**
1081  * idpf_add_port_stats - Copy port stats into ethtool buffer
1082  * @vport: virtual port struct
1083  * @data: ethtool buffer to copy into
1084  */
1085 static void idpf_add_port_stats(struct idpf_vport *vport, u64 **data)
1086 {
1087 	unsigned int size = IDPF_PORT_STATS_LEN;
1088 	unsigned int start;
1089 	unsigned int i;
1090 
1091 	do {
1092 		start = u64_stats_fetch_begin(&vport->port_stats.stats_sync);
1093 		for (i = 0; i < size; i++)
1094 			idpf_add_one_ethtool_stat(&(*data)[i], vport,
1095 						  &idpf_gstrings_port_stats[i]);
1096 	} while (u64_stats_fetch_retry(&vport->port_stats.stats_sync, start));
1097 
1098 	*data += size;
1099 }
1100 
1101 /**
1102  * idpf_collect_queue_stats - accumulate various per queue stats
1103  * into port level stats
1104  * @vport: pointer to vport struct
1105  **/
1106 static void idpf_collect_queue_stats(struct idpf_vport *vport)
1107 {
1108 	struct idpf_port_stats *pstats = &vport->port_stats;
1109 	struct idpf_q_vec_rsrc *rsrc = &vport->dflt_qv_rsrc;
1110 
1111 	/* zero out port stats since they're actually tracked in per
1112 	 * queue stats; this is only for reporting
1113 	 */
1114 	u64_stats_update_begin(&pstats->stats_sync);
1115 	u64_stats_set(&pstats->rx_hw_csum_err, 0);
1116 	u64_stats_set(&pstats->rx_hsplit, 0);
1117 	u64_stats_set(&pstats->rx_hsplit_hbo, 0);
1118 	u64_stats_set(&pstats->rx_bad_descs, 0);
1119 	u64_stats_set(&pstats->tx_linearize, 0);
1120 	u64_stats_set(&pstats->tx_busy, 0);
1121 	u64_stats_set(&pstats->tx_drops, 0);
1122 	u64_stats_set(&pstats->tx_dma_map_errs, 0);
1123 	u64_stats_update_end(&pstats->stats_sync);
1124 
1125 	for (unsigned int i = 0; i < rsrc->num_rxq_grp; i++) {
1126 		struct idpf_rxq_group *rxq_grp = &rsrc->rxq_grps[i];
1127 		u16 num_rxq;
1128 
1129 		if (idpf_is_queue_model_split(rsrc->rxq_model))
1130 			num_rxq = rxq_grp->splitq.num_rxq_sets;
1131 		else
1132 			num_rxq = rxq_grp->singleq.num_rxq;
1133 
1134 		for (unsigned int j = 0; j < num_rxq; j++) {
1135 			u64 hw_csum_err, hsplit, hsplit_hbo, bad_descs;
1136 			struct idpf_rx_queue_stats *stats;
1137 			struct idpf_rx_queue *rxq;
1138 			unsigned int start;
1139 
1140 			if (idpf_is_queue_model_split(rsrc->rxq_model))
1141 				rxq = &rxq_grp->splitq.rxq_sets[j]->rxq;
1142 			else
1143 				rxq = rxq_grp->singleq.rxqs[j];
1144 
1145 			if (!rxq)
1146 				continue;
1147 
1148 			do {
1149 				start = u64_stats_fetch_begin(&rxq->stats_sync);
1150 
1151 				stats = &rxq->q_stats;
1152 				hw_csum_err = u64_stats_read(&stats->hw_csum_err);
1153 				hsplit = u64_stats_read(&stats->hsplit_pkts);
1154 				hsplit_hbo = u64_stats_read(&stats->hsplit_buf_ovf);
1155 				bad_descs = u64_stats_read(&stats->bad_descs);
1156 			} while (u64_stats_fetch_retry(&rxq->stats_sync, start));
1157 
1158 			u64_stats_update_begin(&pstats->stats_sync);
1159 			u64_stats_add(&pstats->rx_hw_csum_err, hw_csum_err);
1160 			u64_stats_add(&pstats->rx_hsplit, hsplit);
1161 			u64_stats_add(&pstats->rx_hsplit_hbo, hsplit_hbo);
1162 			u64_stats_add(&pstats->rx_bad_descs, bad_descs);
1163 			u64_stats_update_end(&pstats->stats_sync);
1164 		}
1165 	}
1166 
1167 	for (unsigned int i = 0; i < rsrc->num_txq_grp; i++) {
1168 		struct idpf_txq_group *txq_grp = &rsrc->txq_grps[i];
1169 
1170 		for (unsigned int j = 0; j < txq_grp->num_txq; j++) {
1171 			u64 linearize, qbusy, skb_drops, dma_map_errs;
1172 			struct idpf_tx_queue *txq = txq_grp->txqs[j];
1173 			struct idpf_tx_queue_stats *stats;
1174 			unsigned int start;
1175 
1176 			if (!txq)
1177 				continue;
1178 
1179 			do {
1180 				start = u64_stats_fetch_begin(&txq->stats_sync);
1181 
1182 				stats = &txq->q_stats;
1183 				linearize = u64_stats_read(&stats->linearize);
1184 				qbusy = u64_stats_read(&stats->q_busy);
1185 				skb_drops = u64_stats_read(&stats->skb_drops);
1186 				dma_map_errs = u64_stats_read(&stats->dma_map_errs);
1187 			} while (u64_stats_fetch_retry(&txq->stats_sync, start));
1188 
1189 			u64_stats_update_begin(&pstats->stats_sync);
1190 			u64_stats_add(&pstats->tx_linearize, linearize);
1191 			u64_stats_add(&pstats->tx_busy, qbusy);
1192 			u64_stats_add(&pstats->tx_drops, skb_drops);
1193 			u64_stats_add(&pstats->tx_dma_map_errs, dma_map_errs);
1194 			u64_stats_update_end(&pstats->stats_sync);
1195 		}
1196 	}
1197 }
1198 
1199 /**
1200  * idpf_get_ethtool_stats - report device statistics
1201  * @netdev: network interface device structure
1202  * @stats: ethtool statistics structure
1203  * @data: pointer to data buffer
1204  *
1205  * All statistics are added to the data buffer as an array of u64.
1206  */
1207 static void idpf_get_ethtool_stats(struct net_device *netdev,
1208 				   struct ethtool_stats __always_unused *stats,
1209 				   u64 *data)
1210 {
1211 	struct idpf_netdev_priv *np = netdev_priv(netdev);
1212 	struct idpf_vport_config *vport_config;
1213 	struct idpf_q_vec_rsrc *rsrc;
1214 	struct idpf_vport *vport;
1215 	unsigned int total = 0;
1216 	bool is_splitq;
1217 	u16 qtype;
1218 
1219 	idpf_vport_ctrl_lock(netdev);
1220 	vport = idpf_netdev_to_vport(netdev);
1221 
1222 	if (!test_bit(IDPF_VPORT_UP, np->state)) {
1223 		idpf_vport_ctrl_unlock(netdev);
1224 
1225 		return;
1226 	}
1227 
1228 	rcu_read_lock();
1229 
1230 	idpf_collect_queue_stats(vport);
1231 	idpf_add_port_stats(vport, &data);
1232 
1233 	rsrc = &vport->dflt_qv_rsrc;
1234 	for (unsigned int i = 0; i < rsrc->num_txq_grp; i++) {
1235 		struct idpf_txq_group *txq_grp = &rsrc->txq_grps[i];
1236 
1237 		qtype = VIRTCHNL2_QUEUE_TYPE_TX;
1238 
1239 		for (unsigned int j = 0; j < txq_grp->num_txq; j++, total++) {
1240 			struct idpf_tx_queue *txq = txq_grp->txqs[j];
1241 
1242 			if (!txq)
1243 				idpf_add_empty_queue_stats(&data, qtype);
1244 			else
1245 				idpf_add_queue_stats(&data, txq, qtype);
1246 		}
1247 	}
1248 
1249 	vport_config = vport->adapter->vport_config[vport->idx];
1250 	/* It is critical we provide a constant number of stats back to
1251 	 * userspace regardless of how many queues are actually in use because
1252 	 * there is no way to inform userspace the size has changed between
1253 	 * ioctl calls. This will fill in any missing stats with zero.
1254 	 */
1255 	for (; total < vport_config->max_q.max_txq; total++)
1256 		idpf_add_empty_queue_stats(&data, VIRTCHNL2_QUEUE_TYPE_TX);
1257 	total = 0;
1258 
1259 	is_splitq = idpf_is_queue_model_split(rsrc->rxq_model);
1260 
1261 	for (unsigned int i = 0; i < rsrc->num_rxq_grp; i++) {
1262 		struct idpf_rxq_group *rxq_grp = &rsrc->rxq_grps[i];
1263 		u16 num_rxq;
1264 
1265 		qtype = VIRTCHNL2_QUEUE_TYPE_RX;
1266 
1267 		if (is_splitq)
1268 			num_rxq = rxq_grp->splitq.num_rxq_sets;
1269 		else
1270 			num_rxq = rxq_grp->singleq.num_rxq;
1271 
1272 		for (unsigned int j = 0; j < num_rxq; j++, total++) {
1273 			struct idpf_rx_queue *rxq;
1274 
1275 			if (is_splitq)
1276 				rxq = &rxq_grp->splitq.rxq_sets[j]->rxq;
1277 			else
1278 				rxq = rxq_grp->singleq.rxqs[j];
1279 			if (!rxq)
1280 				idpf_add_empty_queue_stats(&data, qtype);
1281 			else
1282 				idpf_add_queue_stats(&data, rxq, qtype);
1283 		}
1284 	}
1285 
1286 	for (; total < vport_config->max_q.max_rxq; total++)
1287 		idpf_add_empty_queue_stats(&data, VIRTCHNL2_QUEUE_TYPE_RX);
1288 
1289 	rcu_read_unlock();
1290 
1291 	idpf_vport_ctrl_unlock(netdev);
1292 }
1293 
1294 /**
1295  * idpf_find_rxq_vec - find rxq vector from q index
1296  * @vport: virtual port associated to queue
1297  * @q_num: q index used to find queue
1298  *
1299  * returns pointer to rx vector
1300  */
1301 struct idpf_q_vector *idpf_find_rxq_vec(const struct idpf_vport *vport,
1302 					u32 q_num)
1303 {
1304 	const struct idpf_q_vec_rsrc *rsrc = &vport->dflt_qv_rsrc;
1305 	int q_grp, q_idx;
1306 
1307 	if (!idpf_is_queue_model_split(rsrc->rxq_model))
1308 		return rsrc->rxq_grps->singleq.rxqs[q_num]->q_vector;
1309 
1310 	q_grp = q_num / IDPF_DFLT_SPLITQ_RXQ_PER_GROUP;
1311 	q_idx = q_num % IDPF_DFLT_SPLITQ_RXQ_PER_GROUP;
1312 
1313 	return rsrc->rxq_grps[q_grp].splitq.rxq_sets[q_idx]->rxq.q_vector;
1314 }
1315 
1316 /**
1317  * idpf_find_txq_vec - find txq vector from q index
1318  * @vport: virtual port associated to queue
1319  * @q_num: q index used to find queue
1320  *
1321  * returns pointer to tx vector
1322  */
1323 struct idpf_q_vector *idpf_find_txq_vec(const struct idpf_vport *vport,
1324 					u32 q_num)
1325 {
1326 	const struct idpf_q_vec_rsrc *rsrc = &vport->dflt_qv_rsrc;
1327 	int q_grp;
1328 
1329 	if (!idpf_is_queue_model_split(rsrc->txq_model))
1330 		return vport->txqs[q_num]->q_vector;
1331 
1332 	q_grp = q_num / IDPF_DFLT_SPLITQ_TXQ_PER_GROUP;
1333 
1334 	return rsrc->txq_grps[q_grp].complq->q_vector;
1335 }
1336 
1337 /**
1338  * __idpf_get_q_coalesce - get ITR values for specific queue
1339  * @ec: ethtool structure to fill with driver's coalesce settings
1340  * @q_vector: queue vector corresponding to this queue
1341  * @type: queue type
1342  */
1343 static void __idpf_get_q_coalesce(struct ethtool_coalesce *ec,
1344 				  const struct idpf_q_vector *q_vector,
1345 				  enum virtchnl2_queue_type type)
1346 {
1347 	if (type == VIRTCHNL2_QUEUE_TYPE_RX) {
1348 		ec->use_adaptive_rx_coalesce =
1349 				IDPF_ITR_IS_DYNAMIC(q_vector->rx_intr_mode);
1350 		ec->rx_coalesce_usecs = q_vector->rx_itr_value;
1351 	} else {
1352 		ec->use_adaptive_tx_coalesce =
1353 				IDPF_ITR_IS_DYNAMIC(q_vector->tx_intr_mode);
1354 		ec->tx_coalesce_usecs = q_vector->tx_itr_value;
1355 	}
1356 }
1357 
1358 /**
1359  * idpf_get_q_coalesce - get ITR values for specific queue
1360  * @netdev: pointer to the netdev associated with this query
1361  * @ec: coalesce settings to program the device with
1362  * @q_num: update ITR/INTRL (coalesce) settings for this queue number/index
1363  *
1364  * Return 0 on success, and negative on failure
1365  */
1366 static int idpf_get_q_coalesce(struct net_device *netdev,
1367 			       struct ethtool_coalesce *ec,
1368 			       u32 q_num)
1369 {
1370 	const struct idpf_netdev_priv *np = netdev_priv(netdev);
1371 	struct idpf_q_vec_rsrc *rsrc;
1372 	struct idpf_vport *vport;
1373 	int err = 0;
1374 
1375 	idpf_vport_ctrl_lock(netdev);
1376 	vport = idpf_netdev_to_vport(netdev);
1377 
1378 	if (!test_bit(IDPF_VPORT_UP, np->state))
1379 		goto unlock_mutex;
1380 
1381 	rsrc = &vport->dflt_qv_rsrc;
1382 	if (q_num >= rsrc->num_rxq && q_num >= rsrc->num_txq) {
1383 		err = -EINVAL;
1384 		goto unlock_mutex;
1385 	}
1386 
1387 	if (q_num < rsrc->num_rxq)
1388 		__idpf_get_q_coalesce(ec, idpf_find_rxq_vec(vport, q_num),
1389 				      VIRTCHNL2_QUEUE_TYPE_RX);
1390 
1391 	if (q_num < rsrc->num_txq)
1392 		__idpf_get_q_coalesce(ec, idpf_find_txq_vec(vport, q_num),
1393 				      VIRTCHNL2_QUEUE_TYPE_TX);
1394 
1395 unlock_mutex:
1396 	idpf_vport_ctrl_unlock(netdev);
1397 
1398 	return err;
1399 }
1400 
1401 /**
1402  * idpf_get_coalesce - get ITR values as requested by user
1403  * @netdev: pointer to the netdev associated with this query
1404  * @ec: coalesce settings to be filled
1405  * @kec: unused
1406  * @extack: unused
1407  *
1408  * Return 0 on success, and negative on failure
1409  */
1410 static int idpf_get_coalesce(struct net_device *netdev,
1411 			     struct ethtool_coalesce *ec,
1412 			     struct kernel_ethtool_coalesce *kec,
1413 			     struct netlink_ext_ack *extack)
1414 {
1415 	/* Return coalesce based on queue number zero */
1416 	return idpf_get_q_coalesce(netdev, ec, 0);
1417 }
1418 
1419 /**
1420  * idpf_get_per_q_coalesce - get ITR values as requested by user
1421  * @netdev: pointer to the netdev associated with this query
1422  * @q_num: queue for which the itr values has to retrieved
1423  * @ec: coalesce settings to be filled
1424  *
1425  * Return 0 on success, and negative on failure
1426  */
1427 
1428 static int idpf_get_per_q_coalesce(struct net_device *netdev, u32 q_num,
1429 				   struct ethtool_coalesce *ec)
1430 {
1431 	return idpf_get_q_coalesce(netdev, ec, q_num);
1432 }
1433 
1434 /**
1435  * __idpf_set_q_coalesce - set ITR values for specific queue
1436  * @ec: ethtool structure from user to update ITR settings
1437  * @q_coal: per queue coalesce settings
1438  * @qv: queue vector for which itr values has to be set
1439  * @is_rxq: is queue type rx
1440  *
1441  * Returns 0 on success, negative otherwise.
1442  */
1443 static int __idpf_set_q_coalesce(const struct ethtool_coalesce *ec,
1444 				 struct idpf_q_coalesce *q_coal,
1445 				 struct idpf_q_vector *qv, bool is_rxq)
1446 {
1447 	u32 use_adaptive_coalesce, coalesce_usecs;
1448 	bool is_dim_ena = false;
1449 	u16 itr_val;
1450 
1451 	if (is_rxq) {
1452 		is_dim_ena = IDPF_ITR_IS_DYNAMIC(qv->rx_intr_mode);
1453 		use_adaptive_coalesce = ec->use_adaptive_rx_coalesce;
1454 		coalesce_usecs = ec->rx_coalesce_usecs;
1455 		itr_val = qv->rx_itr_value;
1456 	} else {
1457 		is_dim_ena = IDPF_ITR_IS_DYNAMIC(qv->tx_intr_mode);
1458 		use_adaptive_coalesce = ec->use_adaptive_tx_coalesce;
1459 		coalesce_usecs = ec->tx_coalesce_usecs;
1460 		itr_val = qv->tx_itr_value;
1461 	}
1462 	if (coalesce_usecs != itr_val && use_adaptive_coalesce) {
1463 		netdev_err(qv->vport->netdev, "Cannot set coalesce usecs if adaptive enabled\n");
1464 
1465 		return -EINVAL;
1466 	}
1467 
1468 	if (is_dim_ena && use_adaptive_coalesce)
1469 		return 0;
1470 
1471 	if (coalesce_usecs > IDPF_ITR_MAX) {
1472 		netdev_err(qv->vport->netdev,
1473 			   "Invalid value, %d-usecs range is 0-%d\n",
1474 			   coalesce_usecs, IDPF_ITR_MAX);
1475 
1476 		return -EINVAL;
1477 	}
1478 
1479 	if (coalesce_usecs % 2) {
1480 		coalesce_usecs--;
1481 		netdev_info(qv->vport->netdev,
1482 			    "HW only supports even ITR values, ITR rounded to %d\n",
1483 			    coalesce_usecs);
1484 	}
1485 
1486 	if (is_rxq) {
1487 		qv->rx_itr_value = coalesce_usecs;
1488 		q_coal->rx_coalesce_usecs = coalesce_usecs;
1489 		if (use_adaptive_coalesce) {
1490 			qv->rx_intr_mode = IDPF_ITR_DYNAMIC;
1491 			q_coal->rx_intr_mode = IDPF_ITR_DYNAMIC;
1492 		} else {
1493 			qv->rx_intr_mode = !IDPF_ITR_DYNAMIC;
1494 			q_coal->rx_intr_mode = !IDPF_ITR_DYNAMIC;
1495 			idpf_vport_intr_write_itr(qv, coalesce_usecs, false);
1496 		}
1497 	} else {
1498 		qv->tx_itr_value = coalesce_usecs;
1499 		q_coal->tx_coalesce_usecs = coalesce_usecs;
1500 		if (use_adaptive_coalesce) {
1501 			qv->tx_intr_mode = IDPF_ITR_DYNAMIC;
1502 			q_coal->tx_intr_mode = IDPF_ITR_DYNAMIC;
1503 		} else {
1504 			qv->tx_intr_mode = !IDPF_ITR_DYNAMIC;
1505 			q_coal->tx_intr_mode = !IDPF_ITR_DYNAMIC;
1506 			idpf_vport_intr_write_itr(qv, coalesce_usecs, true);
1507 		}
1508 	}
1509 
1510 	/* Update of static/dynamic itr will be taken care when interrupt is
1511 	 * fired
1512 	 */
1513 	return 0;
1514 }
1515 
1516 /**
1517  * idpf_set_q_coalesce - set ITR values for specific queue
1518  * @vport: vport associated to the queue that need updating
1519  * @q_coal: per queue coalesce settings
1520  * @ec: coalesce settings to program the device with
1521  * @q_num: update ITR/INTRL (coalesce) settings for this queue number/index
1522  * @is_rxq: is queue type rx
1523  *
1524  * Return 0 on success, and negative on failure
1525  */
1526 static int idpf_set_q_coalesce(const struct idpf_vport *vport,
1527 			       struct idpf_q_coalesce *q_coal,
1528 			       const struct ethtool_coalesce *ec,
1529 			       int q_num, bool is_rxq)
1530 {
1531 	struct idpf_q_vector *qv;
1532 
1533 	qv = is_rxq ? idpf_find_rxq_vec(vport, q_num) :
1534 		      idpf_find_txq_vec(vport, q_num);
1535 
1536 	if (qv && __idpf_set_q_coalesce(ec, q_coal, qv, is_rxq))
1537 		return -EINVAL;
1538 
1539 	return 0;
1540 }
1541 
1542 /**
1543  * idpf_set_coalesce - set ITR values as requested by user
1544  * @netdev: pointer to the netdev associated with this query
1545  * @ec: coalesce settings to program the device with
1546  * @kec: unused
1547  * @extack: unused
1548  *
1549  * Return 0 on success, and negative on failure
1550  */
1551 static int idpf_set_coalesce(struct net_device *netdev,
1552 			     struct ethtool_coalesce *ec,
1553 			     struct kernel_ethtool_coalesce *kec,
1554 			     struct netlink_ext_ack *extack)
1555 {
1556 	struct idpf_netdev_priv *np = netdev_priv(netdev);
1557 	struct idpf_vport_user_config_data *user_config;
1558 	struct idpf_q_coalesce *q_coal;
1559 	struct idpf_q_vec_rsrc *rsrc;
1560 	struct idpf_vport *vport;
1561 	int err = 0;
1562 
1563 	user_config = &np->adapter->vport_config[np->vport_idx]->user_config;
1564 
1565 	idpf_vport_ctrl_lock(netdev);
1566 	vport = idpf_netdev_to_vport(netdev);
1567 
1568 	if (!test_bit(IDPF_VPORT_UP, np->state))
1569 		goto unlock_mutex;
1570 
1571 	rsrc = &vport->dflt_qv_rsrc;
1572 	for (unsigned int i = 0; i < rsrc->num_txq; i++) {
1573 		q_coal = &user_config->q_coalesce[i];
1574 		err = idpf_set_q_coalesce(vport, q_coal, ec, i, false);
1575 		if (err)
1576 			goto unlock_mutex;
1577 	}
1578 
1579 	for (unsigned int i = 0; i < rsrc->num_rxq; i++) {
1580 		q_coal = &user_config->q_coalesce[i];
1581 		err = idpf_set_q_coalesce(vport, q_coal, ec, i, true);
1582 		if (err)
1583 			goto unlock_mutex;
1584 	}
1585 
1586 unlock_mutex:
1587 	idpf_vport_ctrl_unlock(netdev);
1588 
1589 	return err;
1590 }
1591 
1592 /**
1593  * idpf_set_per_q_coalesce - set ITR values as requested by user
1594  * @netdev: pointer to the netdev associated with this query
1595  * @q_num: queue for which the itr values has to be set
1596  * @ec: coalesce settings to program the device with
1597  *
1598  * Return 0 on success, and negative on failure
1599  */
1600 static int idpf_set_per_q_coalesce(struct net_device *netdev, u32 q_num,
1601 				   struct ethtool_coalesce *ec)
1602 {
1603 	struct idpf_netdev_priv *np = netdev_priv(netdev);
1604 	struct idpf_vport_user_config_data *user_config;
1605 	struct idpf_q_coalesce *q_coal;
1606 	struct idpf_vport *vport;
1607 	int err;
1608 
1609 	idpf_vport_ctrl_lock(netdev);
1610 	vport = idpf_netdev_to_vport(netdev);
1611 	user_config = &np->adapter->vport_config[np->vport_idx]->user_config;
1612 	q_coal = &user_config->q_coalesce[q_num];
1613 
1614 	err = idpf_set_q_coalesce(vport, q_coal, ec, q_num, false);
1615 	if (err) {
1616 		idpf_vport_ctrl_unlock(netdev);
1617 
1618 		return err;
1619 	}
1620 
1621 	err = idpf_set_q_coalesce(vport, q_coal, ec, q_num, true);
1622 
1623 	idpf_vport_ctrl_unlock(netdev);
1624 
1625 	return err;
1626 }
1627 
1628 /**
1629  * idpf_get_msglevel - Get debug message level
1630  * @netdev: network interface device structure
1631  *
1632  * Returns current debug message level.
1633  */
1634 static u32 idpf_get_msglevel(struct net_device *netdev)
1635 {
1636 	struct idpf_adapter *adapter = idpf_netdev_to_adapter(netdev);
1637 
1638 	return adapter->msg_enable;
1639 }
1640 
1641 /**
1642  * idpf_set_msglevel - Set debug message level
1643  * @netdev: network interface device structure
1644  * @data: message level
1645  *
1646  * Set current debug message level. Higher values cause the driver to
1647  * be noisier.
1648  */
1649 static void idpf_set_msglevel(struct net_device *netdev, u32 data)
1650 {
1651 	struct idpf_adapter *adapter = idpf_netdev_to_adapter(netdev);
1652 
1653 	adapter->msg_enable = data;
1654 }
1655 
1656 /**
1657  * idpf_get_link_ksettings - Get Link Speed and Duplex settings
1658  * @netdev: network interface device structure
1659  * @cmd: ethtool command
1660  *
1661  * Reports speed/duplex settings.
1662  **/
1663 static int idpf_get_link_ksettings(struct net_device *netdev,
1664 				   struct ethtool_link_ksettings *cmd)
1665 {
1666 	struct idpf_netdev_priv *np = netdev_priv(netdev);
1667 
1668 	ethtool_link_ksettings_zero_link_mode(cmd, supported);
1669 	cmd->base.autoneg = AUTONEG_DISABLE;
1670 	cmd->base.port = PORT_NONE;
1671 	if (netif_carrier_ok(netdev)) {
1672 		cmd->base.duplex = DUPLEX_FULL;
1673 		cmd->base.speed = np->link_speed_mbps;
1674 	} else {
1675 		cmd->base.duplex = DUPLEX_UNKNOWN;
1676 		cmd->base.speed = SPEED_UNKNOWN;
1677 	}
1678 
1679 	return 0;
1680 }
1681 
1682 /**
1683  * idpf_get_timestamp_filters - Get the supported timestamping mode
1684  * @vport: Virtual port structure
1685  * @info: ethtool timestamping info structure
1686  *
1687  * Get the Tx/Rx timestamp filters.
1688  */
1689 static void idpf_get_timestamp_filters(const struct idpf_vport *vport,
1690 				       struct kernel_ethtool_ts_info *info)
1691 {
1692 	info->so_timestamping = SOF_TIMESTAMPING_RX_HARDWARE |
1693 				SOF_TIMESTAMPING_RAW_HARDWARE;
1694 
1695 	info->tx_types = BIT(HWTSTAMP_TX_OFF);
1696 	info->rx_filters = BIT(HWTSTAMP_FILTER_NONE) | BIT(HWTSTAMP_FILTER_ALL);
1697 
1698 	if (!vport->tx_tstamp_caps ||
1699 	    vport->adapter->ptp->tx_tstamp_access == IDPF_PTP_NONE)
1700 		return;
1701 
1702 	info->so_timestamping |= SOF_TIMESTAMPING_TX_SOFTWARE |
1703 				 SOF_TIMESTAMPING_TX_HARDWARE;
1704 
1705 	info->tx_types |= BIT(HWTSTAMP_TX_ON);
1706 }
1707 
1708 /**
1709  * idpf_get_ts_info - Get device PHC association
1710  * @netdev: network interface device structure
1711  * @info: ethtool timestamping info structure
1712  *
1713  * Return: 0 on success, -errno otherwise.
1714  */
1715 static int idpf_get_ts_info(struct net_device *netdev,
1716 			    struct kernel_ethtool_ts_info *info)
1717 {
1718 	struct idpf_netdev_priv *np = netdev_priv(netdev);
1719 	struct idpf_vport *vport;
1720 	int err = 0;
1721 
1722 	if (!mutex_trylock(&np->adapter->vport_ctrl_lock))
1723 		return -EBUSY;
1724 
1725 	vport = idpf_netdev_to_vport(netdev);
1726 
1727 	if (!vport->adapter->ptp) {
1728 		err = -EOPNOTSUPP;
1729 		goto unlock;
1730 	}
1731 
1732 	if (idpf_is_cap_ena(vport->adapter, IDPF_OTHER_CAPS, VIRTCHNL2_CAP_PTP) &&
1733 	    vport->adapter->ptp->clock) {
1734 		info->phc_index = ptp_clock_index(vport->adapter->ptp->clock);
1735 		idpf_get_timestamp_filters(vport, info);
1736 	} else {
1737 		pci_dbg(vport->adapter->pdev, "PTP clock not detected\n");
1738 		err = ethtool_op_get_ts_info(netdev, info);
1739 	}
1740 
1741 unlock:
1742 	mutex_unlock(&np->adapter->vport_ctrl_lock);
1743 
1744 	return err;
1745 }
1746 
1747 /**
1748  * idpf_get_ts_stats - Collect HW tstamping statistics
1749  * @netdev: network interface device structure
1750  * @ts_stats: HW timestamping stats structure
1751  *
1752  * Collect HW timestamping statistics including successfully timestamped
1753  * packets, discarded due to illegal values, flushed during releasing PTP and
1754  * skipped due to lack of the free index.
1755  */
1756 static void idpf_get_ts_stats(struct net_device *netdev,
1757 			      struct ethtool_ts_stats *ts_stats)
1758 {
1759 	struct idpf_netdev_priv *np = netdev_priv(netdev);
1760 	struct idpf_q_vec_rsrc *rsrc;
1761 	struct idpf_vport *vport;
1762 	unsigned int start;
1763 
1764 	idpf_vport_ctrl_lock(netdev);
1765 	vport = idpf_netdev_to_vport(netdev);
1766 	do {
1767 		start = u64_stats_fetch_begin(&vport->tstamp_stats.stats_sync);
1768 		ts_stats->pkts = u64_stats_read(&vport->tstamp_stats.packets);
1769 		ts_stats->lost = u64_stats_read(&vport->tstamp_stats.flushed);
1770 		ts_stats->err = u64_stats_read(&vport->tstamp_stats.discarded);
1771 	} while (u64_stats_fetch_retry(&vport->tstamp_stats.stats_sync, start));
1772 
1773 	if (!test_bit(IDPF_VPORT_UP, np->state))
1774 		goto exit;
1775 
1776 	rsrc = &vport->dflt_qv_rsrc;
1777 	for (u16 i = 0; i < rsrc->num_txq_grp; i++) {
1778 		struct idpf_txq_group *txq_grp = &rsrc->txq_grps[i];
1779 
1780 		for (u16 j = 0; j < txq_grp->num_txq; j++) {
1781 			struct idpf_tx_queue *txq = txq_grp->txqs[j];
1782 			struct idpf_tx_queue_stats *stats;
1783 			u64 ts;
1784 
1785 			if (!txq)
1786 				continue;
1787 
1788 			stats = &txq->q_stats;
1789 			do {
1790 				start = u64_stats_fetch_begin(&txq->stats_sync);
1791 
1792 				ts = u64_stats_read(&stats->tstamp_skipped);
1793 			} while (u64_stats_fetch_retry(&txq->stats_sync,
1794 						       start));
1795 
1796 			ts_stats->lost += ts;
1797 		}
1798 	}
1799 
1800 exit:
1801 	idpf_vport_ctrl_unlock(netdev);
1802 }
1803 
1804 static const struct ethtool_ops idpf_ethtool_ops = {
1805 	.supported_coalesce_params = ETHTOOL_COALESCE_USECS |
1806 				     ETHTOOL_COALESCE_USE_ADAPTIVE,
1807 	.supported_ring_params	= ETHTOOL_RING_USE_TCP_DATA_SPLIT,
1808 	.get_msglevel		= idpf_get_msglevel,
1809 	.set_msglevel		= idpf_set_msglevel,
1810 	.get_link		= ethtool_op_get_link,
1811 	.get_coalesce		= idpf_get_coalesce,
1812 	.set_coalesce		= idpf_set_coalesce,
1813 	.get_per_queue_coalesce = idpf_get_per_q_coalesce,
1814 	.set_per_queue_coalesce = idpf_set_per_q_coalesce,
1815 	.get_ethtool_stats	= idpf_get_ethtool_stats,
1816 	.get_strings		= idpf_get_strings,
1817 	.get_sset_count		= idpf_get_sset_count,
1818 	.get_channels		= idpf_get_channels,
1819 	.get_rxnfc		= idpf_get_rxnfc,
1820 	.set_rxnfc		= idpf_set_rxnfc,
1821 	.get_rx_ring_count	= idpf_get_rx_ring_count,
1822 	.get_rxfh_key_size	= idpf_get_rxfh_key_size,
1823 	.get_rxfh_indir_size	= idpf_get_rxfh_indir_size,
1824 	.get_rxfh		= idpf_get_rxfh,
1825 	.set_rxfh		= idpf_set_rxfh,
1826 	.set_channels		= idpf_set_channels,
1827 	.get_ringparam		= idpf_get_ringparam,
1828 	.set_ringparam		= idpf_set_ringparam,
1829 	.get_link_ksettings	= idpf_get_link_ksettings,
1830 	.get_ts_info		= idpf_get_ts_info,
1831 	.get_ts_stats		= idpf_get_ts_stats,
1832 };
1833 
1834 /**
1835  * idpf_set_ethtool_ops - Initialize ethtool ops struct
1836  * @netdev: network interface device structure
1837  *
1838  * Sets ethtool ops struct in our netdev so that ethtool can call
1839  * our functions.
1840  */
1841 void idpf_set_ethtool_ops(struct net_device *netdev)
1842 {
1843 	netdev->ethtool_ops = &idpf_ethtool_ops;
1844 }
1845