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 
9 /**
10  * ice_repr_get_sw_port_id - get port ID associated with representor
11  * @repr: pointer to port representor
12  */
13 static int ice_repr_get_sw_port_id(struct ice_repr *repr)
14 {
15 	return repr->vf->pf->hw.port_info->lport;
16 }
17 
18 /**
19  * ice_repr_get_phys_port_name - get phys port name
20  * @netdev: pointer to port representor netdev
21  * @buf: write here port name
22  * @len: max length of buf
23  */
24 static int
25 ice_repr_get_phys_port_name(struct net_device *netdev, char *buf, size_t len)
26 {
27 	struct ice_netdev_priv *np = netdev_priv(netdev);
28 	struct ice_repr *repr = np->repr;
29 	int res;
30 
31 	/* Devlink port is registered and devlink core is taking care of name formatting. */
32 	if (repr->vf->devlink_port.devlink)
33 		return -EOPNOTSUPP;
34 
35 	res = snprintf(buf, len, "pf%dvfr%d", ice_repr_get_sw_port_id(repr),
36 		       repr->vf->vf_id);
37 	if (res <= 0)
38 		return -EOPNOTSUPP;
39 	return 0;
40 }
41 
42 /**
43  * ice_netdev_to_repr - Get port representor for given netdevice
44  * @netdev: pointer to port representor netdev
45  */
46 struct ice_repr *ice_netdev_to_repr(struct net_device *netdev)
47 {
48 	struct ice_netdev_priv *np = netdev_priv(netdev);
49 
50 	return np->repr;
51 }
52 
53 /**
54  * ice_repr_open - Enable port representor's network interface
55  * @netdev: network interface device structure
56  *
57  * The open entry point is called when a port representor's network
58  * interface is made active by the system (IFF_UP). Corresponding
59  * VF is notified about link status change.
60  *
61  * Returns 0 on success
62  */
63 static int ice_repr_open(struct net_device *netdev)
64 {
65 	struct ice_repr *repr = ice_netdev_to_repr(netdev);
66 	struct ice_vf *vf;
67 
68 	vf = repr->vf;
69 	vf->link_forced = true;
70 	vf->link_up = true;
71 	ice_vc_notify_vf_link_state(vf);
72 
73 	netif_carrier_on(netdev);
74 	netif_tx_start_all_queues(netdev);
75 
76 	return 0;
77 }
78 
79 /**
80  * ice_repr_stop - Disable port representor's network interface
81  * @netdev: network interface device structure
82  *
83  * The stop entry point is called when a port representor's network
84  * interface is de-activated by the system. Corresponding
85  * VF is notified about link status change.
86  *
87  * Returns 0 on success
88  */
89 static int ice_repr_stop(struct net_device *netdev)
90 {
91 	struct ice_repr *repr = ice_netdev_to_repr(netdev);
92 	struct ice_vf *vf;
93 
94 	vf = repr->vf;
95 	vf->link_forced = true;
96 	vf->link_up = false;
97 	ice_vc_notify_vf_link_state(vf);
98 
99 	netif_carrier_off(netdev);
100 	netif_tx_stop_all_queues(netdev);
101 
102 	return 0;
103 }
104 
105 static struct devlink_port *
106 ice_repr_get_devlink_port(struct net_device *netdev)
107 {
108 	struct ice_repr *repr = ice_netdev_to_repr(netdev);
109 
110 	return &repr->vf->devlink_port;
111 }
112 
113 static const struct net_device_ops ice_repr_netdev_ops = {
114 	.ndo_get_phys_port_name = ice_repr_get_phys_port_name,
115 	.ndo_open = ice_repr_open,
116 	.ndo_stop = ice_repr_stop,
117 	.ndo_start_xmit = ice_eswitch_port_start_xmit,
118 	.ndo_get_devlink_port = ice_repr_get_devlink_port,
119 };
120 
121 /**
122  * ice_is_port_repr_netdev - Check if a given netdevice is a port representor netdev
123  * @netdev: pointer to netdev
124  */
125 bool ice_is_port_repr_netdev(struct net_device *netdev)
126 {
127 	return netdev && (netdev->netdev_ops == &ice_repr_netdev_ops);
128 }
129 
130 /**
131  * ice_repr_reg_netdev - register port representor netdev
132  * @netdev: pointer to port representor netdev
133  */
134 static int
135 ice_repr_reg_netdev(struct net_device *netdev)
136 {
137 	eth_hw_addr_random(netdev);
138 	netdev->netdev_ops = &ice_repr_netdev_ops;
139 
140 	netif_carrier_off(netdev);
141 	netif_tx_stop_all_queues(netdev);
142 
143 	return register_netdev(netdev);
144 }
145 
146 /**
147  * ice_repr_add - add representor for VF
148  * @vf: pointer to VF structure
149  */
150 static int ice_repr_add(struct ice_vf *vf)
151 {
152 	struct ice_q_vector *q_vector;
153 	struct ice_netdev_priv *np;
154 	struct ice_repr *repr;
155 	int err;
156 
157 	repr = kzalloc(sizeof(*repr), GFP_KERNEL);
158 	if (!repr)
159 		return -ENOMEM;
160 
161 	repr->netdev = alloc_etherdev(sizeof(struct ice_netdev_priv));
162 	if (!repr->netdev) {
163 		err =  -ENOMEM;
164 		goto err_alloc;
165 	}
166 
167 	repr->src_vsi = ice_get_vf_vsi(vf);
168 	repr->vf = vf;
169 	vf->repr = repr;
170 	np = netdev_priv(repr->netdev);
171 	np->repr = repr;
172 
173 	q_vector = kzalloc(sizeof(*q_vector), GFP_KERNEL);
174 	if (!q_vector) {
175 		err = -ENOMEM;
176 		goto err_alloc_q_vector;
177 	}
178 	repr->q_vector = q_vector;
179 
180 	err = ice_devlink_create_vf_port(vf);
181 	if (err)
182 		goto err_devlink;
183 
184 	err = ice_repr_reg_netdev(repr->netdev);
185 	if (err)
186 		goto err_netdev;
187 
188 	devlink_port_type_eth_set(&vf->devlink_port, repr->netdev);
189 
190 	return 0;
191 
192 err_netdev:
193 	ice_devlink_destroy_vf_port(vf);
194 err_devlink:
195 	kfree(repr->q_vector);
196 	vf->repr->q_vector = NULL;
197 err_alloc_q_vector:
198 	free_netdev(repr->netdev);
199 	repr->netdev = NULL;
200 err_alloc:
201 	kfree(repr);
202 	vf->repr = NULL;
203 	return err;
204 }
205 
206 /**
207  * ice_repr_rem - remove representor from VF
208  * @vf: pointer to VF structure
209  */
210 static void ice_repr_rem(struct ice_vf *vf)
211 {
212 	ice_devlink_destroy_vf_port(vf);
213 	kfree(vf->repr->q_vector);
214 	vf->repr->q_vector = NULL;
215 	unregister_netdev(vf->repr->netdev);
216 	free_netdev(vf->repr->netdev);
217 	vf->repr->netdev = NULL;
218 	kfree(vf->repr);
219 	vf->repr = NULL;
220 }
221 
222 /**
223  * ice_repr_add_for_all_vfs - add port representor for all VFs
224  * @pf: pointer to PF structure
225  */
226 int ice_repr_add_for_all_vfs(struct ice_pf *pf)
227 {
228 	int err;
229 	int i;
230 
231 	ice_for_each_vf(pf, i) {
232 		struct ice_vf *vf = &pf->vf[i];
233 
234 		err = ice_repr_add(vf);
235 		if (err)
236 			goto err;
237 
238 		ice_vc_change_ops_to_repr(&vf->vc_ops);
239 	}
240 
241 	return 0;
242 
243 err:
244 	for (i = i - 1; i >= 0; i--) {
245 		struct ice_vf *vf = &pf->vf[i];
246 
247 		ice_repr_rem(vf);
248 		ice_vc_set_dflt_vf_ops(&vf->vc_ops);
249 	}
250 
251 	return err;
252 }
253 
254 /**
255  * ice_repr_rem_from_all_vfs - remove port representor for all VFs
256  * @pf: pointer to PF structure
257  */
258 void ice_repr_rem_from_all_vfs(struct ice_pf *pf)
259 {
260 	int i;
261 
262 	ice_for_each_vf(pf, i) {
263 		struct ice_vf *vf = &pf->vf[i];
264 
265 		ice_repr_rem(vf);
266 		ice_vc_set_dflt_vf_ops(&vf->vc_ops);
267 	}
268 }
269 
270 /**
271  * ice_repr_start_tx_queues - start Tx queues of port representor
272  * @repr: pointer to repr structure
273  */
274 void ice_repr_start_tx_queues(struct ice_repr *repr)
275 {
276 	netif_carrier_on(repr->netdev);
277 	netif_tx_start_all_queues(repr->netdev);
278 }
279 
280 /**
281  * ice_repr_stop_tx_queues - stop Tx queues of port representor
282  * @repr: pointer to repr structure
283  */
284 void ice_repr_stop_tx_queues(struct ice_repr *repr)
285 {
286 	netif_carrier_off(repr->netdev);
287 	netif_tx_stop_all_queues(repr->netdev);
288 }
289 
290 /**
291  * ice_repr_set_traffic_vsi - set traffic VSI for port representor
292  * @repr: repr on with VSI will be set
293  * @vsi: pointer to VSI that will be used by port representor to pass traffic
294  */
295 void ice_repr_set_traffic_vsi(struct ice_repr *repr, struct ice_vsi *vsi)
296 {
297 	struct ice_netdev_priv *np = netdev_priv(repr->netdev);
298 
299 	np->vsi = vsi;
300 }
301