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