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_virtchnl_pf.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 static int
146 ice_repr_setup_tc_cls_flower(struct ice_repr *repr,
147 			     struct flow_cls_offload *flower)
148 {
149 	switch (flower->command) {
150 	case FLOW_CLS_REPLACE:
151 		return ice_add_cls_flower(repr->netdev, repr->src_vsi, flower);
152 	case FLOW_CLS_DESTROY:
153 		return ice_del_cls_flower(repr->src_vsi, flower);
154 	default:
155 		return -EINVAL;
156 	}
157 }
158 
159 static int
160 ice_repr_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
161 			   void *cb_priv)
162 {
163 	struct flow_cls_offload *flower = (struct flow_cls_offload *)type_data;
164 	struct ice_netdev_priv *np = (struct ice_netdev_priv *)cb_priv;
165 
166 	switch (type) {
167 	case TC_SETUP_CLSFLOWER:
168 		return ice_repr_setup_tc_cls_flower(np->repr, flower);
169 	default:
170 		return -EOPNOTSUPP;
171 	}
172 }
173 
174 static LIST_HEAD(ice_repr_block_cb_list);
175 
176 static int
177 ice_repr_setup_tc(struct net_device *netdev, enum tc_setup_type type,
178 		  void *type_data)
179 {
180 	struct ice_netdev_priv *np = netdev_priv(netdev);
181 
182 	switch (type) {
183 	case TC_SETUP_BLOCK:
184 		return flow_block_cb_setup_simple((struct flow_block_offload *)
185 						  type_data,
186 						  &ice_repr_block_cb_list,
187 						  ice_repr_setup_tc_block_cb,
188 						  np, np, true);
189 	default:
190 		return -EOPNOTSUPP;
191 	}
192 }
193 
194 static const struct net_device_ops ice_repr_netdev_ops = {
195 	.ndo_get_phys_port_name = ice_repr_get_phys_port_name,
196 	.ndo_get_stats64 = ice_repr_get_stats64,
197 	.ndo_open = ice_repr_open,
198 	.ndo_stop = ice_repr_stop,
199 	.ndo_start_xmit = ice_eswitch_port_start_xmit,
200 	.ndo_get_devlink_port = ice_repr_get_devlink_port,
201 	.ndo_setup_tc = ice_repr_setup_tc,
202 };
203 
204 /**
205  * ice_is_port_repr_netdev - Check if a given netdevice is a port representor netdev
206  * @netdev: pointer to netdev
207  */
208 bool ice_is_port_repr_netdev(struct net_device *netdev)
209 {
210 	return netdev && (netdev->netdev_ops == &ice_repr_netdev_ops);
211 }
212 
213 /**
214  * ice_repr_reg_netdev - register port representor netdev
215  * @netdev: pointer to port representor netdev
216  */
217 static int
218 ice_repr_reg_netdev(struct net_device *netdev)
219 {
220 	eth_hw_addr_random(netdev);
221 	netdev->netdev_ops = &ice_repr_netdev_ops;
222 	ice_set_ethtool_repr_ops(netdev);
223 
224 	netdev->hw_features |= NETIF_F_HW_TC;
225 
226 	netif_carrier_off(netdev);
227 	netif_tx_stop_all_queues(netdev);
228 
229 	return register_netdev(netdev);
230 }
231 
232 /**
233  * ice_repr_add - add representor for VF
234  * @vf: pointer to VF structure
235  */
236 static int ice_repr_add(struct ice_vf *vf)
237 {
238 	struct ice_q_vector *q_vector;
239 	struct ice_netdev_priv *np;
240 	struct ice_repr *repr;
241 	int err;
242 
243 	repr = kzalloc(sizeof(*repr), GFP_KERNEL);
244 	if (!repr)
245 		return -ENOMEM;
246 
247 	repr->netdev = alloc_etherdev(sizeof(struct ice_netdev_priv));
248 	if (!repr->netdev) {
249 		err =  -ENOMEM;
250 		goto err_alloc;
251 	}
252 
253 	repr->src_vsi = ice_get_vf_vsi(vf);
254 	repr->vf = vf;
255 	vf->repr = repr;
256 	np = netdev_priv(repr->netdev);
257 	np->repr = repr;
258 
259 	q_vector = kzalloc(sizeof(*q_vector), GFP_KERNEL);
260 	if (!q_vector) {
261 		err = -ENOMEM;
262 		goto err_alloc_q_vector;
263 	}
264 	repr->q_vector = q_vector;
265 
266 	err = ice_devlink_create_vf_port(vf);
267 	if (err)
268 		goto err_devlink;
269 
270 	repr->netdev->min_mtu = ETH_MIN_MTU;
271 	repr->netdev->max_mtu = ICE_MAX_MTU;
272 
273 	err = ice_repr_reg_netdev(repr->netdev);
274 	if (err)
275 		goto err_netdev;
276 
277 	devlink_port_type_eth_set(&vf->devlink_port, repr->netdev);
278 
279 	return 0;
280 
281 err_netdev:
282 	ice_devlink_destroy_vf_port(vf);
283 err_devlink:
284 	kfree(repr->q_vector);
285 	vf->repr->q_vector = NULL;
286 err_alloc_q_vector:
287 	free_netdev(repr->netdev);
288 	repr->netdev = NULL;
289 err_alloc:
290 	kfree(repr);
291 	vf->repr = NULL;
292 	return err;
293 }
294 
295 /**
296  * ice_repr_rem - remove representor from VF
297  * @vf: pointer to VF structure
298  */
299 static void ice_repr_rem(struct ice_vf *vf)
300 {
301 	ice_devlink_destroy_vf_port(vf);
302 	kfree(vf->repr->q_vector);
303 	vf->repr->q_vector = NULL;
304 	unregister_netdev(vf->repr->netdev);
305 	free_netdev(vf->repr->netdev);
306 	vf->repr->netdev = NULL;
307 	kfree(vf->repr);
308 	vf->repr = NULL;
309 }
310 
311 /**
312  * ice_repr_add_for_all_vfs - add port representor for all VFs
313  * @pf: pointer to PF structure
314  */
315 int ice_repr_add_for_all_vfs(struct ice_pf *pf)
316 {
317 	int err;
318 	int i;
319 
320 	ice_for_each_vf(pf, i) {
321 		struct ice_vf *vf = &pf->vf[i];
322 
323 		err = ice_repr_add(vf);
324 		if (err)
325 			goto err;
326 
327 		ice_vc_change_ops_to_repr(&vf->vc_ops);
328 	}
329 
330 	return 0;
331 
332 err:
333 	for (i = i - 1; i >= 0; i--) {
334 		struct ice_vf *vf = &pf->vf[i];
335 
336 		ice_repr_rem(vf);
337 		ice_vc_set_dflt_vf_ops(&vf->vc_ops);
338 	}
339 
340 	return err;
341 }
342 
343 /**
344  * ice_repr_rem_from_all_vfs - remove port representor for all VFs
345  * @pf: pointer to PF structure
346  */
347 void ice_repr_rem_from_all_vfs(struct ice_pf *pf)
348 {
349 	int i;
350 
351 	ice_for_each_vf(pf, i) {
352 		struct ice_vf *vf = &pf->vf[i];
353 
354 		ice_repr_rem(vf);
355 		ice_vc_set_dflt_vf_ops(&vf->vc_ops);
356 	}
357 }
358 
359 /**
360  * ice_repr_start_tx_queues - start Tx queues of port representor
361  * @repr: pointer to repr structure
362  */
363 void ice_repr_start_tx_queues(struct ice_repr *repr)
364 {
365 	netif_carrier_on(repr->netdev);
366 	netif_tx_start_all_queues(repr->netdev);
367 }
368 
369 /**
370  * ice_repr_stop_tx_queues - stop Tx queues of port representor
371  * @repr: pointer to repr structure
372  */
373 void ice_repr_stop_tx_queues(struct ice_repr *repr)
374 {
375 	netif_carrier_off(repr->netdev);
376 	netif_tx_stop_all_queues(repr->netdev);
377 }
378 
379 /**
380  * ice_repr_set_traffic_vsi - set traffic VSI for port representor
381  * @repr: repr on with VSI will be set
382  * @vsi: pointer to VSI that will be used by port representor to pass traffic
383  */
384 void ice_repr_set_traffic_vsi(struct ice_repr *repr, struct ice_vsi *vsi)
385 {
386 	struct ice_netdev_priv *np = netdev_priv(repr->netdev);
387 
388 	np->vsi = vsi;
389 }
390