1 // SPDX-License-Identifier: GPL-2.0-only
2 // Copyright 2011 Cisco Systems, Inc. All rights reserved.
3
4 #include <linux/kernel.h>
5 #include <linux/string.h>
6 #include <linux/errno.h>
7 #include <linux/types.h>
8 #include <linux/netdevice.h>
9 #include <linux/etherdevice.h>
10 #include <linux/rtnetlink.h>
11 #include <net/ip.h>
12
13 #include "vnic_vic.h"
14 #include "enic_res.h"
15 #include "enic.h"
16 #include "enic_dev.h"
17 #include "enic_pp.h"
18
19 /*
20 * Checks validity of vf index that came in
21 * port profile request
22 */
enic_is_valid_pp_vf(struct enic * enic,int vf,int * err)23 int enic_is_valid_pp_vf(struct enic *enic, int vf, int *err)
24 {
25 if (vf != PORT_SELF_VF) {
26 #ifdef CONFIG_PCI_IOV
27 if (enic_sriov_enabled(enic)) {
28 /* V2 SR-IOV uses MBOX, not port profiles */
29 if (enic->vf_type == ENIC_VF_TYPE_V2) {
30 *err = -EOPNOTSUPP;
31 goto err_out;
32 }
33 if (vf < 0 || vf >= enic->num_vfs) {
34 *err = -EINVAL;
35 goto err_out;
36 }
37 } else {
38 *err = -EOPNOTSUPP;
39 goto err_out;
40 }
41 #else
42 *err = -EOPNOTSUPP;
43 goto err_out;
44 #endif
45 }
46
47 if (vf == PORT_SELF_VF && !enic_is_dynamic(enic)) {
48 *err = -EOPNOTSUPP;
49 goto err_out;
50 }
51
52 *err = 0;
53 return 1;
54
55 err_out:
56 return 0;
57 }
58
enic_set_port_profile(struct enic * enic,int vf)59 static int enic_set_port_profile(struct enic *enic, int vf)
60 {
61 struct net_device *netdev = enic->netdev;
62 struct enic_port_profile *pp;
63 struct vic_provinfo *vp;
64 const u8 oui[3] = VIC_PROVINFO_CISCO_OUI;
65 const __be16 os_type = htons(VIC_GENERIC_PROV_OS_TYPE_LINUX);
66 const u8 *client_mac;
67 char uuid_str[38];
68 char client_mac_str[18];
69 int err;
70
71 ENIC_PP_BY_INDEX(enic, vf, pp, &err);
72 if (err)
73 return err;
74
75 if (!(pp->set & ENIC_SET_NAME) || !strlen(pp->name))
76 return -EINVAL;
77
78 vp = vic_provinfo_alloc(GFP_KERNEL, oui,
79 VIC_PROVINFO_GENERIC_TYPE);
80 if (!vp)
81 return -ENOMEM;
82
83 VIC_PROVINFO_ADD_TLV(vp,
84 VIC_GENERIC_PROV_TLV_PORT_PROFILE_NAME_STR,
85 strlen(pp->name) + 1, pp->name);
86
87 if (!is_zero_ether_addr(pp->mac_addr)) {
88 client_mac = pp->mac_addr;
89 } else if (vf == PORT_SELF_VF) {
90 client_mac = netdev->dev_addr;
91 } else {
92 netdev_err(netdev, "Cannot find pp mac address "
93 "for VF %d\n", vf);
94 err = -EINVAL;
95 goto add_tlv_failure;
96 }
97
98 VIC_PROVINFO_ADD_TLV(vp,
99 VIC_GENERIC_PROV_TLV_CLIENT_MAC_ADDR,
100 ETH_ALEN, client_mac);
101
102 snprintf(client_mac_str, sizeof(client_mac_str), "%pM", client_mac);
103 VIC_PROVINFO_ADD_TLV(vp,
104 VIC_GENERIC_PROV_TLV_CLUSTER_PORT_UUID_STR,
105 sizeof(client_mac_str), client_mac_str);
106
107 if (pp->set & ENIC_SET_INSTANCE) {
108 sprintf(uuid_str, "%pUB", pp->instance_uuid);
109 VIC_PROVINFO_ADD_TLV(vp,
110 VIC_GENERIC_PROV_TLV_CLIENT_UUID_STR,
111 sizeof(uuid_str), uuid_str);
112 }
113
114 if (pp->set & ENIC_SET_HOST) {
115 sprintf(uuid_str, "%pUB", pp->host_uuid);
116 VIC_PROVINFO_ADD_TLV(vp,
117 VIC_GENERIC_PROV_TLV_HOST_UUID_STR,
118 sizeof(uuid_str), uuid_str);
119 }
120
121 VIC_PROVINFO_ADD_TLV(vp,
122 VIC_GENERIC_PROV_TLV_OS_TYPE,
123 sizeof(os_type), &os_type);
124
125 ENIC_DEVCMD_PROXY_BY_INDEX(vf, err, enic, vnic_dev_init_prov2, (u8 *)vp,
126 vic_provinfo_size(vp));
127 err = enic_dev_status_to_errno(err);
128
129 add_tlv_failure:
130 vic_provinfo_free(vp);
131
132 return err;
133 }
134
enic_unset_port_profile(struct enic * enic,int vf)135 static int enic_unset_port_profile(struct enic *enic, int vf)
136 {
137 int err;
138
139 ENIC_DEVCMD_PROXY_BY_INDEX(vf, err, enic, vnic_dev_deinit);
140 if (err)
141 return enic_dev_status_to_errno(err);
142
143 if (vf == PORT_SELF_VF)
144 enic_reset_addr_lists(enic);
145
146 return 0;
147 }
148
enic_are_pp_different(struct enic_port_profile * pp1,struct enic_port_profile * pp2)149 static int enic_are_pp_different(struct enic_port_profile *pp1,
150 struct enic_port_profile *pp2)
151 {
152 return strcmp(pp1->name, pp2->name) | !!memcmp(pp1->instance_uuid,
153 pp2->instance_uuid, PORT_UUID_MAX) |
154 !!memcmp(pp1->host_uuid, pp2->host_uuid, PORT_UUID_MAX) |
155 !ether_addr_equal(pp1->mac_addr, pp2->mac_addr);
156 }
157
158 static int enic_pp_preassociate(struct enic *enic, int vf,
159 struct enic_port_profile *prev_pp, int *restore_pp);
160 static int enic_pp_disassociate(struct enic *enic, int vf,
161 struct enic_port_profile *prev_pp, int *restore_pp);
162 static int enic_pp_preassociate_rr(struct enic *enic, int vf,
163 struct enic_port_profile *prev_pp, int *restore_pp);
164 static int enic_pp_associate(struct enic *enic, int vf,
165 struct enic_port_profile *prev_pp, int *restore_pp);
166
167 static int (*enic_pp_handlers[])(struct enic *enic, int vf,
168 struct enic_port_profile *prev_state,
169 int *restore_pp) = {
170 [PORT_REQUEST_PREASSOCIATE] = enic_pp_preassociate,
171 [PORT_REQUEST_PREASSOCIATE_RR] = enic_pp_preassociate_rr,
172 [PORT_REQUEST_ASSOCIATE] = enic_pp_associate,
173 [PORT_REQUEST_DISASSOCIATE] = enic_pp_disassociate,
174 };
175
176 static const int enic_pp_handlers_count =
177 ARRAY_SIZE(enic_pp_handlers);
178
enic_pp_preassociate(struct enic * enic,int vf,struct enic_port_profile * prev_pp,int * restore_pp)179 static int enic_pp_preassociate(struct enic *enic, int vf,
180 struct enic_port_profile *prev_pp, int *restore_pp)
181 {
182 return -EOPNOTSUPP;
183 }
184
enic_pp_disassociate(struct enic * enic,int vf,struct enic_port_profile * prev_pp,int * restore_pp)185 static int enic_pp_disassociate(struct enic *enic, int vf,
186 struct enic_port_profile *prev_pp, int *restore_pp)
187 {
188 struct net_device *netdev = enic->netdev;
189 struct enic_port_profile *pp;
190 int err;
191
192 ENIC_PP_BY_INDEX(enic, vf, pp, &err);
193 if (err)
194 return err;
195
196 /* Deregister mac addresses */
197 if (!is_zero_ether_addr(pp->mac_addr))
198 ENIC_DEVCMD_PROXY_BY_INDEX(vf, err, enic, vnic_dev_del_addr,
199 pp->mac_addr);
200 else if (vf == PORT_SELF_VF && !is_zero_ether_addr(netdev->dev_addr))
201 ENIC_DEVCMD_PROXY_BY_INDEX(vf, err, enic, vnic_dev_del_addr,
202 netdev->dev_addr);
203
204 return enic_unset_port_profile(enic, vf);
205 }
206
enic_pp_preassociate_rr(struct enic * enic,int vf,struct enic_port_profile * prev_pp,int * restore_pp)207 static int enic_pp_preassociate_rr(struct enic *enic, int vf,
208 struct enic_port_profile *prev_pp, int *restore_pp)
209 {
210 struct enic_port_profile *pp;
211 int err;
212 int active = 0;
213
214 ENIC_PP_BY_INDEX(enic, vf, pp, &err);
215 if (err)
216 return err;
217
218 if (pp->request != PORT_REQUEST_ASSOCIATE) {
219 /* If pre-associate is not part of an associate.
220 We always disassociate first */
221 err = enic_pp_handlers[PORT_REQUEST_DISASSOCIATE](enic, vf,
222 prev_pp, restore_pp);
223 if (err)
224 return err;
225
226 *restore_pp = 0;
227 }
228
229 *restore_pp = 0;
230
231 err = enic_set_port_profile(enic, vf);
232 if (err)
233 return err;
234
235 /* If pre-associate is not part of an associate. */
236 if (pp->request != PORT_REQUEST_ASSOCIATE) {
237 /* Enable device as standby */
238 ENIC_DEVCMD_PROXY_BY_INDEX(vf, err, enic, vnic_dev_enable2,
239 active);
240 err = enic_dev_status_to_errno(err);
241 }
242
243 return err;
244 }
245
enic_pp_associate(struct enic * enic,int vf,struct enic_port_profile * prev_pp,int * restore_pp)246 static int enic_pp_associate(struct enic *enic, int vf,
247 struct enic_port_profile *prev_pp, int *restore_pp)
248 {
249 struct net_device *netdev = enic->netdev;
250 struct enic_port_profile *pp;
251 int err;
252 int active = 1;
253
254 ENIC_PP_BY_INDEX(enic, vf, pp, &err);
255 if (err)
256 return err;
257
258 /* Check if a pre-associate was called before */
259 if (prev_pp->request != PORT_REQUEST_PREASSOCIATE_RR ||
260 (prev_pp->request == PORT_REQUEST_PREASSOCIATE_RR &&
261 enic_are_pp_different(prev_pp, pp))) {
262 err = enic_pp_handlers[PORT_REQUEST_DISASSOCIATE](
263 enic, vf, prev_pp, restore_pp);
264 if (err)
265 return err;
266
267 *restore_pp = 0;
268 }
269
270 err = enic_pp_handlers[PORT_REQUEST_PREASSOCIATE_RR](
271 enic, vf, prev_pp, restore_pp);
272 if (err)
273 return err;
274
275 *restore_pp = 0;
276
277 /* Enable device as active */
278 ENIC_DEVCMD_PROXY_BY_INDEX(vf, err, enic, vnic_dev_enable2, active);
279 err = enic_dev_status_to_errno(err);
280 if (err)
281 return err;
282
283 /* Register mac address */
284 if (!is_zero_ether_addr(pp->mac_addr))
285 ENIC_DEVCMD_PROXY_BY_INDEX(vf, err, enic, vnic_dev_add_addr,
286 pp->mac_addr);
287 else if (vf == PORT_SELF_VF && !is_zero_ether_addr(netdev->dev_addr))
288 ENIC_DEVCMD_PROXY_BY_INDEX(vf, err, enic, vnic_dev_add_addr,
289 netdev->dev_addr);
290
291 return 0;
292 }
293
enic_process_set_pp_request(struct enic * enic,int vf,struct enic_port_profile * prev_pp,int * restore_pp)294 int enic_process_set_pp_request(struct enic *enic, int vf,
295 struct enic_port_profile *prev_pp, int *restore_pp)
296 {
297 struct enic_port_profile *pp;
298 int err;
299
300 ENIC_PP_BY_INDEX(enic, vf, pp, &err);
301 if (err)
302 return err;
303
304 if (pp->request >= enic_pp_handlers_count
305 || !enic_pp_handlers[pp->request])
306 return -EOPNOTSUPP;
307
308 return enic_pp_handlers[pp->request](enic, vf, prev_pp, restore_pp);
309 }
310
enic_process_get_pp_request(struct enic * enic,int vf,int request,u16 * response)311 int enic_process_get_pp_request(struct enic *enic, int vf,
312 int request, u16 *response)
313 {
314 int err, status = ERR_SUCCESS;
315
316 switch (request) {
317
318 case PORT_REQUEST_PREASSOCIATE_RR:
319 case PORT_REQUEST_ASSOCIATE:
320 ENIC_DEVCMD_PROXY_BY_INDEX(vf, err, enic,
321 vnic_dev_enable2_done, &status);
322 break;
323
324 case PORT_REQUEST_DISASSOCIATE:
325 ENIC_DEVCMD_PROXY_BY_INDEX(vf, err, enic,
326 vnic_dev_deinit_done, &status);
327 break;
328
329 default:
330 return -EINVAL;
331 }
332
333 if (err)
334 status = err;
335
336 switch (status) {
337 case ERR_SUCCESS:
338 *response = PORT_PROFILE_RESPONSE_SUCCESS;
339 break;
340 case ERR_EINVAL:
341 *response = PORT_PROFILE_RESPONSE_INVALID;
342 break;
343 case ERR_EBADSTATE:
344 *response = PORT_PROFILE_RESPONSE_BADSTATE;
345 break;
346 case ERR_ENOMEM:
347 *response = PORT_PROFILE_RESPONSE_INSUFFICIENT_RESOURCES;
348 break;
349 case ERR_EINPROGRESS:
350 *response = PORT_PROFILE_RESPONSE_INPROGRESS;
351 break;
352 default:
353 *response = PORT_PROFILE_RESPONSE_ERROR;
354 break;
355 }
356
357 return 0;
358 }
359