• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (C) 2019-2021, Intel Corporation. */
3 
4 #include "ice.h"
5 #include "ice_eswitch.h"
6 #include "ice_devlink.h"
7 #include "ice_sriov.h"
8 #include "ice_tc_lib.h"
9 
10 /**
11  * ice_repr_get_sw_port_id - get port ID associated with representor
12  * @repr: pointer to port representor
13  */
ice_repr_get_sw_port_id(struct ice_repr * repr)14 static int ice_repr_get_sw_port_id(struct ice_repr *repr)
15 {
16 	return repr->vf->pf->hw.port_info->lport;
17 }
18 
19 /**
20  * ice_repr_get_phys_port_name - get phys port name
21  * @netdev: pointer to port representor netdev
22  * @buf: write here port name
23  * @len: max length of buf
24  */
25 static int
ice_repr_get_phys_port_name(struct net_device * netdev,char * buf,size_t len)26 ice_repr_get_phys_port_name(struct net_device *netdev, char *buf, size_t len)
27 {
28 	struct ice_netdev_priv *np = netdev_priv(netdev);
29 	struct ice_repr *repr = np->repr;
30 	int res;
31 
32 	/* Devlink port is registered and devlink core is taking care of name formatting. */
33 	if (repr->vf->devlink_port.devlink)
34 		return -EOPNOTSUPP;
35 
36 	res = snprintf(buf, len, "pf%dvfr%d", ice_repr_get_sw_port_id(repr),
37 		       repr->vf->vf_id);
38 	if (res <= 0)
39 		return -EOPNOTSUPP;
40 	return 0;
41 }
42 
43 /**
44  * ice_repr_get_stats64 - get VF stats for VFPR use
45  * @netdev: pointer to port representor netdev
46  * @stats: pointer to struct where stats can be stored
47  */
48 static void
ice_repr_get_stats64(struct net_device * netdev,struct rtnl_link_stats64 * stats)49 ice_repr_get_stats64(struct net_device *netdev, struct rtnl_link_stats64 *stats)
50 {
51 	struct ice_netdev_priv *np = netdev_priv(netdev);
52 	struct ice_eth_stats *eth_stats;
53 	struct ice_vsi *vsi;
54 
55 	if (ice_is_vf_disabled(np->repr->vf))
56 		return;
57 	vsi = np->repr->src_vsi;
58 
59 	ice_update_vsi_stats(vsi);
60 	eth_stats = &vsi->eth_stats;
61 
62 	stats->tx_packets = eth_stats->tx_unicast + eth_stats->tx_broadcast +
63 			    eth_stats->tx_multicast;
64 	stats->rx_packets = eth_stats->rx_unicast + eth_stats->rx_broadcast +
65 			    eth_stats->rx_multicast;
66 	stats->tx_bytes = eth_stats->tx_bytes;
67 	stats->rx_bytes = eth_stats->rx_bytes;
68 	stats->multicast = eth_stats->rx_multicast;
69 	stats->tx_errors = eth_stats->tx_errors;
70 	stats->tx_dropped = eth_stats->tx_discards;
71 	stats->rx_dropped = eth_stats->rx_discards;
72 }
73 
74 /**
75  * ice_netdev_to_repr - Get port representor for given netdevice
76  * @netdev: pointer to port representor netdev
77  */
ice_netdev_to_repr(struct net_device * netdev)78 struct ice_repr *ice_netdev_to_repr(struct net_device *netdev)
79 {
80 	struct ice_netdev_priv *np = netdev_priv(netdev);
81 
82 	return np->repr;
83 }
84 
85 /**
86  * ice_repr_open - Enable port representor's network interface
87  * @netdev: network interface device structure
88  *
89  * The open entry point is called when a port representor's network
90  * interface is made active by the system (IFF_UP). Corresponding
91  * VF is notified about link status change.
92  *
93  * Returns 0 on success
94  */
ice_repr_open(struct net_device * netdev)95 static int ice_repr_open(struct net_device *netdev)
96 {
97 	struct ice_repr *repr = ice_netdev_to_repr(netdev);
98 	struct ice_vf *vf;
99 
100 	vf = repr->vf;
101 	vf->link_forced = true;
102 	vf->link_up = true;
103 	ice_vc_notify_vf_link_state(vf);
104 
105 	netif_carrier_on(netdev);
106 	netif_tx_start_all_queues(netdev);
107 
108 	return 0;
109 }
110 
111 /**
112  * ice_repr_stop - Disable port representor's network interface
113  * @netdev: network interface device structure
114  *
115  * The stop entry point is called when a port representor's network
116  * interface is de-activated by the system. Corresponding
117  * VF is notified about link status change.
118  *
119  * Returns 0 on success
120  */
ice_repr_stop(struct net_device * netdev)121 static int ice_repr_stop(struct net_device *netdev)
122 {
123 	struct ice_repr *repr = ice_netdev_to_repr(netdev);
124 	struct ice_vf *vf;
125 
126 	vf = repr->vf;
127 	vf->link_forced = true;
128 	vf->link_up = false;
129 	ice_vc_notify_vf_link_state(vf);
130 
131 	netif_carrier_off(netdev);
132 	netif_tx_stop_all_queues(netdev);
133 
134 	return 0;
135 }
136 
137 static struct devlink_port *
ice_repr_get_devlink_port(struct net_device * netdev)138 ice_repr_get_devlink_port(struct net_device *netdev)
139 {
140 	struct ice_repr *repr = ice_netdev_to_repr(netdev);
141 
142 	return &repr->vf->devlink_port;
143 }
144 
145 /**
146  * ice_repr_sp_stats64 - get slow path stats for port representor
147  * @dev: network interface device structure
148  * @stats: netlink stats structure
149  *
150  * RX/TX stats are being swapped here to be consistent with VF stats. In slow
151  * path, port representor receives data when the corresponding VF is sending it
152  * (and vice versa), TX and RX bytes/packets are effectively swapped on port
153  * representor.
154  */
155 static int
ice_repr_sp_stats64(const struct net_device * dev,struct rtnl_link_stats64 * stats)156 ice_repr_sp_stats64(const struct net_device *dev,
157 		    struct rtnl_link_stats64 *stats)
158 {
159 	struct ice_netdev_priv *np = netdev_priv(dev);
160 	int vf_id = np->repr->vf->vf_id;
161 	struct ice_tx_ring *tx_ring;
162 	struct ice_rx_ring *rx_ring;
163 	u64 pkts, bytes;
164 
165 	tx_ring = np->vsi->tx_rings[vf_id];
166 	ice_fetch_u64_stats_per_ring(&tx_ring->syncp, tx_ring->stats,
167 				     &pkts, &bytes);
168 	stats->rx_packets = pkts;
169 	stats->rx_bytes = bytes;
170 
171 	rx_ring = np->vsi->rx_rings[vf_id];
172 	ice_fetch_u64_stats_per_ring(&rx_ring->syncp, rx_ring->stats,
173 				     &pkts, &bytes);
174 	stats->tx_packets = pkts;
175 	stats->tx_bytes = bytes;
176 	stats->tx_dropped = rx_ring->rx_stats.alloc_page_failed +
177 			    rx_ring->rx_stats.alloc_buf_failed;
178 
179 	return 0;
180 }
181 
182 static bool
ice_repr_ndo_has_offload_stats(const struct net_device * dev,int attr_id)183 ice_repr_ndo_has_offload_stats(const struct net_device *dev, int attr_id)
184 {
185 	return attr_id == IFLA_OFFLOAD_XSTATS_CPU_HIT;
186 }
187 
188 static int
ice_repr_ndo_get_offload_stats(int attr_id,const struct net_device * dev,void * sp)189 ice_repr_ndo_get_offload_stats(int attr_id, const struct net_device *dev,
190 			       void *sp)
191 {
192 	if (attr_id == IFLA_OFFLOAD_XSTATS_CPU_HIT)
193 		return ice_repr_sp_stats64(dev, (struct rtnl_link_stats64 *)sp);
194 
195 	return -EINVAL;
196 }
197 
198 static int
ice_repr_setup_tc_cls_flower(struct ice_repr * repr,struct flow_cls_offload * flower)199 ice_repr_setup_tc_cls_flower(struct ice_repr *repr,
200 			     struct flow_cls_offload *flower)
201 {
202 	switch (flower->command) {
203 	case FLOW_CLS_REPLACE:
204 		return ice_add_cls_flower(repr->netdev, repr->src_vsi, flower);
205 	case FLOW_CLS_DESTROY:
206 		return ice_del_cls_flower(repr->src_vsi, flower);
207 	default:
208 		return -EINVAL;
209 	}
210 }
211 
212 static int
ice_repr_setup_tc_block_cb(enum tc_setup_type type,void * type_data,void * cb_priv)213 ice_repr_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
214 			   void *cb_priv)
215 {
216 	struct flow_cls_offload *flower = (struct flow_cls_offload *)type_data;
217 	struct ice_netdev_priv *np = (struct ice_netdev_priv *)cb_priv;
218 
219 	switch (type) {
220 	case TC_SETUP_CLSFLOWER:
221 		return ice_repr_setup_tc_cls_flower(np->repr, flower);
222 	default:
223 		return -EOPNOTSUPP;
224 	}
225 }
226 
227 static LIST_HEAD(ice_repr_block_cb_list);
228 
229 static int
ice_repr_setup_tc(struct net_device * netdev,enum tc_setup_type type,void * type_data)230 ice_repr_setup_tc(struct net_device *netdev, enum tc_setup_type type,
231 		  void *type_data)
232 {
233 	struct ice_netdev_priv *np = netdev_priv(netdev);
234 
235 	switch (type) {
236 	case TC_SETUP_BLOCK:
237 		return flow_block_cb_setup_simple((struct flow_block_offload *)
238 						  type_data,
239 						  &ice_repr_block_cb_list,
240 						  ice_repr_setup_tc_block_cb,
241 						  np, np, true);
242 	default:
243 		return -EOPNOTSUPP;
244 	}
245 }
246 
247 static const struct net_device_ops ice_repr_netdev_ops = {
248 	.ndo_get_phys_port_name = ice_repr_get_phys_port_name,
249 	.ndo_get_stats64 = ice_repr_get_stats64,
250 	.ndo_open = ice_repr_open,
251 	.ndo_stop = ice_repr_stop,
252 	.ndo_start_xmit = ice_eswitch_port_start_xmit,
253 	.ndo_get_devlink_port = ice_repr_get_devlink_port,
254 	.ndo_setup_tc = ice_repr_setup_tc,
255 	.ndo_has_offload_stats = ice_repr_ndo_has_offload_stats,
256 	.ndo_get_offload_stats = ice_repr_ndo_get_offload_stats,
257 };
258 
259 /**
260  * ice_is_port_repr_netdev - Check if a given netdevice is a port representor netdev
261  * @netdev: pointer to netdev
262  */
ice_is_port_repr_netdev(struct net_device * netdev)263 bool ice_is_port_repr_netdev(struct net_device *netdev)
264 {
265 	return netdev && (netdev->netdev_ops == &ice_repr_netdev_ops);
266 }
267 
268 /**
269  * ice_repr_reg_netdev - register port representor netdev
270  * @netdev: pointer to port representor netdev
271  */
272 static int
ice_repr_reg_netdev(struct net_device * netdev)273 ice_repr_reg_netdev(struct net_device *netdev)
274 {
275 	eth_hw_addr_random(netdev);
276 	netdev->netdev_ops = &ice_repr_netdev_ops;
277 	ice_set_ethtool_repr_ops(netdev);
278 
279 	netdev->hw_features |= NETIF_F_HW_TC;
280 
281 	netif_carrier_off(netdev);
282 	netif_tx_stop_all_queues(netdev);
283 
284 	return register_netdev(netdev);
285 }
286 
287 /**
288  * ice_repr_add - add representor for VF
289  * @vf: pointer to VF structure
290  */
ice_repr_add(struct ice_vf * vf)291 static int ice_repr_add(struct ice_vf *vf)
292 {
293 	struct ice_q_vector *q_vector;
294 	struct ice_netdev_priv *np;
295 	struct ice_repr *repr;
296 	struct ice_vsi *vsi;
297 	int err;
298 
299 	vsi = ice_get_vf_vsi(vf);
300 	if (!vsi)
301 		return -EINVAL;
302 
303 	repr = kzalloc(sizeof(*repr), GFP_KERNEL);
304 	if (!repr)
305 		return -ENOMEM;
306 
307 #ifdef CONFIG_ICE_SWITCHDEV
308 	repr->mac_rule = kzalloc(sizeof(*repr->mac_rule), GFP_KERNEL);
309 	if (!repr->mac_rule) {
310 		err = -ENOMEM;
311 		goto err_alloc_rule;
312 	}
313 #endif
314 
315 	repr->netdev = alloc_etherdev(sizeof(struct ice_netdev_priv));
316 	if (!repr->netdev) {
317 		err =  -ENOMEM;
318 		goto err_alloc;
319 	}
320 
321 	repr->src_vsi = vsi;
322 	repr->vf = vf;
323 	vf->repr = repr;
324 	np = netdev_priv(repr->netdev);
325 	np->repr = repr;
326 
327 	q_vector = kzalloc(sizeof(*q_vector), GFP_KERNEL);
328 	if (!q_vector) {
329 		err = -ENOMEM;
330 		goto err_alloc_q_vector;
331 	}
332 	repr->q_vector = q_vector;
333 
334 	err = ice_devlink_create_vf_port(vf);
335 	if (err)
336 		goto err_devlink;
337 
338 	repr->netdev->min_mtu = ETH_MIN_MTU;
339 	repr->netdev->max_mtu = ICE_MAX_MTU;
340 
341 	SET_NETDEV_DEV(repr->netdev, ice_pf_to_dev(vf->pf));
342 	err = ice_repr_reg_netdev(repr->netdev);
343 	if (err)
344 		goto err_netdev;
345 
346 	devlink_port_type_eth_set(&vf->devlink_port, repr->netdev);
347 
348 	ice_virtchnl_set_repr_ops(vf);
349 
350 	return 0;
351 
352 err_netdev:
353 	ice_devlink_destroy_vf_port(vf);
354 err_devlink:
355 	kfree(repr->q_vector);
356 	vf->repr->q_vector = NULL;
357 err_alloc_q_vector:
358 	free_netdev(repr->netdev);
359 	repr->netdev = NULL;
360 err_alloc:
361 #ifdef CONFIG_ICE_SWITCHDEV
362 	kfree(repr->mac_rule);
363 	repr->mac_rule = NULL;
364 err_alloc_rule:
365 #endif
366 	kfree(repr);
367 	vf->repr = NULL;
368 	return err;
369 }
370 
371 /**
372  * ice_repr_rem - remove representor from VF
373  * @vf: pointer to VF structure
374  */
ice_repr_rem(struct ice_vf * vf)375 static void ice_repr_rem(struct ice_vf *vf)
376 {
377 	if (!vf->repr)
378 		return;
379 
380 	kfree(vf->repr->q_vector);
381 	vf->repr->q_vector = NULL;
382 	unregister_netdev(vf->repr->netdev);
383 	ice_devlink_destroy_vf_port(vf);
384 	free_netdev(vf->repr->netdev);
385 	vf->repr->netdev = NULL;
386 #ifdef CONFIG_ICE_SWITCHDEV
387 	kfree(vf->repr->mac_rule);
388 	vf->repr->mac_rule = NULL;
389 #endif
390 	kfree(vf->repr);
391 	vf->repr = NULL;
392 
393 	ice_virtchnl_set_dflt_ops(vf);
394 }
395 
396 /**
397  * ice_repr_rem_from_all_vfs - remove port representor for all VFs
398  * @pf: pointer to PF structure
399  */
ice_repr_rem_from_all_vfs(struct ice_pf * pf)400 void ice_repr_rem_from_all_vfs(struct ice_pf *pf)
401 {
402 	struct ice_vf *vf;
403 	unsigned int bkt;
404 
405 	lockdep_assert_held(&pf->vfs.table_lock);
406 
407 	ice_for_each_vf(pf, bkt, vf)
408 		ice_repr_rem(vf);
409 }
410 
411 /**
412  * ice_repr_add_for_all_vfs - add port representor for all VFs
413  * @pf: pointer to PF structure
414  */
ice_repr_add_for_all_vfs(struct ice_pf * pf)415 int ice_repr_add_for_all_vfs(struct ice_pf *pf)
416 {
417 	struct ice_vf *vf;
418 	unsigned int bkt;
419 	int err;
420 
421 	lockdep_assert_held(&pf->vfs.table_lock);
422 
423 	ice_for_each_vf(pf, bkt, vf) {
424 		err = ice_repr_add(vf);
425 		if (err)
426 			goto err;
427 	}
428 
429 	return 0;
430 
431 err:
432 	ice_repr_rem_from_all_vfs(pf);
433 
434 	return err;
435 }
436 
437 /**
438  * ice_repr_start_tx_queues - start Tx queues of port representor
439  * @repr: pointer to repr structure
440  */
ice_repr_start_tx_queues(struct ice_repr * repr)441 void ice_repr_start_tx_queues(struct ice_repr *repr)
442 {
443 	netif_carrier_on(repr->netdev);
444 	netif_tx_start_all_queues(repr->netdev);
445 }
446 
447 /**
448  * ice_repr_stop_tx_queues - stop Tx queues of port representor
449  * @repr: pointer to repr structure
450  */
ice_repr_stop_tx_queues(struct ice_repr * repr)451 void ice_repr_stop_tx_queues(struct ice_repr *repr)
452 {
453 	netif_carrier_off(repr->netdev);
454 	netif_tx_stop_all_queues(repr->netdev);
455 }
456 
457 /**
458  * ice_repr_set_traffic_vsi - set traffic VSI for port representor
459  * @repr: repr on with VSI will be set
460  * @vsi: pointer to VSI that will be used by port representor to pass traffic
461  */
ice_repr_set_traffic_vsi(struct ice_repr * repr,struct ice_vsi * vsi)462 void ice_repr_set_traffic_vsi(struct ice_repr *repr, struct ice_vsi *vsi)
463 {
464 	struct ice_netdev_priv *np = netdev_priv(repr->netdev);
465 
466 	np->vsi = vsi;
467 }
468