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  */
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
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
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  */
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  */
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  */
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 *
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
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
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
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
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
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
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  */
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
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  */
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 	int err;
297 
298 	repr = kzalloc(sizeof(*repr), GFP_KERNEL);
299 	if (!repr)
300 		return -ENOMEM;
301 
302 #ifdef CONFIG_ICE_SWITCHDEV
303 	repr->mac_rule = kzalloc(sizeof(*repr->mac_rule), GFP_KERNEL);
304 	if (!repr->mac_rule) {
305 		err = -ENOMEM;
306 		goto err_alloc_rule;
307 	}
308 #endif
309 
310 	repr->netdev = alloc_etherdev(sizeof(struct ice_netdev_priv));
311 	if (!repr->netdev) {
312 		err =  -ENOMEM;
313 		goto err_alloc;
314 	}
315 
316 	repr->src_vsi = ice_get_vf_vsi(vf);
317 	repr->vf = vf;
318 	vf->repr = repr;
319 	np = netdev_priv(repr->netdev);
320 	np->repr = repr;
321 
322 	q_vector = kzalloc(sizeof(*q_vector), GFP_KERNEL);
323 	if (!q_vector) {
324 		err = -ENOMEM;
325 		goto err_alloc_q_vector;
326 	}
327 	repr->q_vector = q_vector;
328 
329 	err = ice_devlink_create_vf_port(vf);
330 	if (err)
331 		goto err_devlink;
332 
333 	repr->netdev->min_mtu = ETH_MIN_MTU;
334 	repr->netdev->max_mtu = ICE_MAX_MTU;
335 
336 	err = ice_repr_reg_netdev(repr->netdev);
337 	if (err)
338 		goto err_netdev;
339 
340 	devlink_port_type_eth_set(&vf->devlink_port, repr->netdev);
341 
342 	ice_virtchnl_set_repr_ops(vf);
343 
344 	return 0;
345 
346 err_netdev:
347 	ice_devlink_destroy_vf_port(vf);
348 err_devlink:
349 	kfree(repr->q_vector);
350 	vf->repr->q_vector = NULL;
351 err_alloc_q_vector:
352 	free_netdev(repr->netdev);
353 	repr->netdev = NULL;
354 err_alloc:
355 #ifdef CONFIG_ICE_SWITCHDEV
356 	kfree(repr->mac_rule);
357 	repr->mac_rule = NULL;
358 err_alloc_rule:
359 #endif
360 	kfree(repr);
361 	vf->repr = NULL;
362 	return err;
363 }
364 
365 /**
366  * ice_repr_rem - remove representor from VF
367  * @vf: pointer to VF structure
368  */
369 static void ice_repr_rem(struct ice_vf *vf)
370 {
371 	if (!vf->repr)
372 		return;
373 
374 	ice_devlink_destroy_vf_port(vf);
375 	kfree(vf->repr->q_vector);
376 	vf->repr->q_vector = NULL;
377 	unregister_netdev(vf->repr->netdev);
378 	free_netdev(vf->repr->netdev);
379 	vf->repr->netdev = NULL;
380 #ifdef CONFIG_ICE_SWITCHDEV
381 	kfree(vf->repr->mac_rule);
382 	vf->repr->mac_rule = NULL;
383 #endif
384 	kfree(vf->repr);
385 	vf->repr = NULL;
386 
387 	ice_virtchnl_set_dflt_ops(vf);
388 }
389 
390 /**
391  * ice_repr_rem_from_all_vfs - remove port representor for all VFs
392  * @pf: pointer to PF structure
393  */
394 void ice_repr_rem_from_all_vfs(struct ice_pf *pf)
395 {
396 	struct ice_vf *vf;
397 	unsigned int bkt;
398 
399 	lockdep_assert_held(&pf->vfs.table_lock);
400 
401 	ice_for_each_vf(pf, bkt, vf)
402 		ice_repr_rem(vf);
403 }
404 
405 /**
406  * ice_repr_add_for_all_vfs - add port representor for all VFs
407  * @pf: pointer to PF structure
408  */
409 int ice_repr_add_for_all_vfs(struct ice_pf *pf)
410 {
411 	struct ice_vf *vf;
412 	unsigned int bkt;
413 	int err;
414 
415 	lockdep_assert_held(&pf->vfs.table_lock);
416 
417 	ice_for_each_vf(pf, bkt, vf) {
418 		err = ice_repr_add(vf);
419 		if (err)
420 			goto err;
421 	}
422 
423 	return 0;
424 
425 err:
426 	ice_repr_rem_from_all_vfs(pf);
427 
428 	return err;
429 }
430 
431 /**
432  * ice_repr_start_tx_queues - start Tx queues of port representor
433  * @repr: pointer to repr structure
434  */
435 void ice_repr_start_tx_queues(struct ice_repr *repr)
436 {
437 	netif_carrier_on(repr->netdev);
438 	netif_tx_start_all_queues(repr->netdev);
439 }
440 
441 /**
442  * ice_repr_stop_tx_queues - stop Tx queues of port representor
443  * @repr: pointer to repr structure
444  */
445 void ice_repr_stop_tx_queues(struct ice_repr *repr)
446 {
447 	netif_carrier_off(repr->netdev);
448 	netif_tx_stop_all_queues(repr->netdev);
449 }
450 
451 /**
452  * ice_repr_set_traffic_vsi - set traffic VSI for port representor
453  * @repr: repr on with VSI will be set
454  * @vsi: pointer to VSI that will be used by port representor to pass traffic
455  */
456 void ice_repr_set_traffic_vsi(struct ice_repr *repr, struct ice_vsi *vsi)
457 {
458 	struct ice_netdev_priv *np = netdev_priv(repr->netdev);
459 
460 	np->vsi = vsi;
461 }
462