1 // SPDX-License-Identifier: GPL-2.0-only
2 // Copyright 2011 Cisco Systems, Inc. All rights reserved.
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>
20 * Checks validity of vf index that came in
21 * port profile request
23 int enic_is_valid_pp_vf(struct enic *enic, int vf, int *err)
25 if (vf != PORT_SELF_VF) {
27 if (enic_sriov_enabled(enic)) {
28 if (vf < 0 || vf >= enic->num_vfs) {
42 if (vf == PORT_SELF_VF && !enic_is_dynamic(enic)) {
54 static int enic_set_port_profile(struct enic *enic, int vf)
56 struct net_device *netdev = enic->netdev;
57 struct enic_port_profile *pp;
58 struct vic_provinfo *vp;
59 const u8 oui[3] = VIC_PROVINFO_CISCO_OUI;
60 const __be16 os_type = htons(VIC_GENERIC_PROV_OS_TYPE_LINUX);
63 char client_mac_str[18];
66 ENIC_PP_BY_INDEX(enic, vf, pp, &err);
70 if (!(pp->set & ENIC_SET_NAME) || !strlen(pp->name))
73 vp = vic_provinfo_alloc(GFP_KERNEL, oui,
74 VIC_PROVINFO_GENERIC_TYPE);
78 VIC_PROVINFO_ADD_TLV(vp,
79 VIC_GENERIC_PROV_TLV_PORT_PROFILE_NAME_STR,
80 strlen(pp->name) + 1, pp->name);
82 if (!is_zero_ether_addr(pp->mac_addr)) {
83 client_mac = pp->mac_addr;
84 } else if (vf == PORT_SELF_VF) {
85 client_mac = netdev->dev_addr;
87 netdev_err(netdev, "Cannot find pp mac address "
93 VIC_PROVINFO_ADD_TLV(vp,
94 VIC_GENERIC_PROV_TLV_CLIENT_MAC_ADDR,
95 ETH_ALEN, client_mac);
97 snprintf(client_mac_str, sizeof(client_mac_str), "%pM", client_mac);
98 VIC_PROVINFO_ADD_TLV(vp,
99 VIC_GENERIC_PROV_TLV_CLUSTER_PORT_UUID_STR,
100 sizeof(client_mac_str), client_mac_str);
102 if (pp->set & ENIC_SET_INSTANCE) {
103 sprintf(uuid_str, "%pUB", pp->instance_uuid);
104 VIC_PROVINFO_ADD_TLV(vp,
105 VIC_GENERIC_PROV_TLV_CLIENT_UUID_STR,
106 sizeof(uuid_str), uuid_str);
109 if (pp->set & ENIC_SET_HOST) {
110 sprintf(uuid_str, "%pUB", pp->host_uuid);
111 VIC_PROVINFO_ADD_TLV(vp,
112 VIC_GENERIC_PROV_TLV_HOST_UUID_STR,
113 sizeof(uuid_str), uuid_str);
116 VIC_PROVINFO_ADD_TLV(vp,
117 VIC_GENERIC_PROV_TLV_OS_TYPE,
118 sizeof(os_type), &os_type);
120 ENIC_DEVCMD_PROXY_BY_INDEX(vf, err, enic, vnic_dev_init_prov2, (u8 *)vp,
121 vic_provinfo_size(vp));
122 err = enic_dev_status_to_errno(err);
125 vic_provinfo_free(vp);
130 static int enic_unset_port_profile(struct enic *enic, int vf)
134 ENIC_DEVCMD_PROXY_BY_INDEX(vf, err, enic, vnic_dev_deinit);
136 return enic_dev_status_to_errno(err);
138 if (vf == PORT_SELF_VF)
139 enic_reset_addr_lists(enic);
144 static int enic_are_pp_different(struct enic_port_profile *pp1,
145 struct enic_port_profile *pp2)
147 return strcmp(pp1->name, pp2->name) | !!memcmp(pp1->instance_uuid,
148 pp2->instance_uuid, PORT_UUID_MAX) |
149 !!memcmp(pp1->host_uuid, pp2->host_uuid, PORT_UUID_MAX) |
150 !ether_addr_equal(pp1->mac_addr, pp2->mac_addr);
153 static int enic_pp_preassociate(struct enic *enic, int vf,
154 struct enic_port_profile *prev_pp, int *restore_pp);
155 static int enic_pp_disassociate(struct enic *enic, int vf,
156 struct enic_port_profile *prev_pp, int *restore_pp);
157 static int enic_pp_preassociate_rr(struct enic *enic, int vf,
158 struct enic_port_profile *prev_pp, int *restore_pp);
159 static int enic_pp_associate(struct enic *enic, int vf,
160 struct enic_port_profile *prev_pp, int *restore_pp);
162 static int (*enic_pp_handlers[])(struct enic *enic, int vf,
163 struct enic_port_profile *prev_state,
165 [PORT_REQUEST_PREASSOCIATE] = enic_pp_preassociate,
166 [PORT_REQUEST_PREASSOCIATE_RR] = enic_pp_preassociate_rr,
167 [PORT_REQUEST_ASSOCIATE] = enic_pp_associate,
168 [PORT_REQUEST_DISASSOCIATE] = enic_pp_disassociate,
171 static const int enic_pp_handlers_count =
172 ARRAY_SIZE(enic_pp_handlers);
174 static int enic_pp_preassociate(struct enic *enic, int vf,
175 struct enic_port_profile *prev_pp, int *restore_pp)
180 static int enic_pp_disassociate(struct enic *enic, int vf,
181 struct enic_port_profile *prev_pp, int *restore_pp)
183 struct net_device *netdev = enic->netdev;
184 struct enic_port_profile *pp;
187 ENIC_PP_BY_INDEX(enic, vf, pp, &err);
191 /* Deregister mac addresses */
192 if (!is_zero_ether_addr(pp->mac_addr))
193 ENIC_DEVCMD_PROXY_BY_INDEX(vf, err, enic, vnic_dev_del_addr,
195 else if (vf == PORT_SELF_VF && !is_zero_ether_addr(netdev->dev_addr))
196 ENIC_DEVCMD_PROXY_BY_INDEX(vf, err, enic, vnic_dev_del_addr,
199 return enic_unset_port_profile(enic, vf);
202 static int enic_pp_preassociate_rr(struct enic *enic, int vf,
203 struct enic_port_profile *prev_pp, int *restore_pp)
205 struct enic_port_profile *pp;
209 ENIC_PP_BY_INDEX(enic, vf, pp, &err);
213 if (pp->request != PORT_REQUEST_ASSOCIATE) {
214 /* If pre-associate is not part of an associate.
215 We always disassociate first */
216 err = enic_pp_handlers[PORT_REQUEST_DISASSOCIATE](enic, vf,
217 prev_pp, restore_pp);
226 err = enic_set_port_profile(enic, vf);
230 /* If pre-associate is not part of an associate. */
231 if (pp->request != PORT_REQUEST_ASSOCIATE) {
232 /* Enable device as standby */
233 ENIC_DEVCMD_PROXY_BY_INDEX(vf, err, enic, vnic_dev_enable2,
235 err = enic_dev_status_to_errno(err);
241 static int enic_pp_associate(struct enic *enic, int vf,
242 struct enic_port_profile *prev_pp, int *restore_pp)
244 struct net_device *netdev = enic->netdev;
245 struct enic_port_profile *pp;
249 ENIC_PP_BY_INDEX(enic, vf, pp, &err);
253 /* Check if a pre-associate was called before */
254 if (prev_pp->request != PORT_REQUEST_PREASSOCIATE_RR ||
255 (prev_pp->request == PORT_REQUEST_PREASSOCIATE_RR &&
256 enic_are_pp_different(prev_pp, pp))) {
257 err = enic_pp_handlers[PORT_REQUEST_DISASSOCIATE](
258 enic, vf, prev_pp, restore_pp);
265 err = enic_pp_handlers[PORT_REQUEST_PREASSOCIATE_RR](
266 enic, vf, prev_pp, restore_pp);
272 /* Enable device as active */
273 ENIC_DEVCMD_PROXY_BY_INDEX(vf, err, enic, vnic_dev_enable2, active);
274 err = enic_dev_status_to_errno(err);
278 /* Register mac address */
279 if (!is_zero_ether_addr(pp->mac_addr))
280 ENIC_DEVCMD_PROXY_BY_INDEX(vf, err, enic, vnic_dev_add_addr,
282 else if (vf == PORT_SELF_VF && !is_zero_ether_addr(netdev->dev_addr))
283 ENIC_DEVCMD_PROXY_BY_INDEX(vf, err, enic, vnic_dev_add_addr,
289 int enic_process_set_pp_request(struct enic *enic, int vf,
290 struct enic_port_profile *prev_pp, int *restore_pp)
292 struct enic_port_profile *pp;
295 ENIC_PP_BY_INDEX(enic, vf, pp, &err);
299 if (pp->request >= enic_pp_handlers_count
300 || !enic_pp_handlers[pp->request])
303 return enic_pp_handlers[pp->request](enic, vf, prev_pp, restore_pp);
306 int enic_process_get_pp_request(struct enic *enic, int vf,
307 int request, u16 *response)
309 int err, status = ERR_SUCCESS;
313 case PORT_REQUEST_PREASSOCIATE_RR:
314 case PORT_REQUEST_ASSOCIATE:
315 ENIC_DEVCMD_PROXY_BY_INDEX(vf, err, enic,
316 vnic_dev_enable2_done, &status);
319 case PORT_REQUEST_DISASSOCIATE:
320 ENIC_DEVCMD_PROXY_BY_INDEX(vf, err, enic,
321 vnic_dev_deinit_done, &status);
333 *response = PORT_PROFILE_RESPONSE_SUCCESS;
336 *response = PORT_PROFILE_RESPONSE_INVALID;
339 *response = PORT_PROFILE_RESPONSE_BADSTATE;
342 *response = PORT_PROFILE_RESPONSE_INSUFFICIENT_RESOURCES;
344 case ERR_EINPROGRESS:
345 *response = PORT_PROFILE_RESPONSE_INPROGRESS;
348 *response = PORT_PROFILE_RESPONSE_ERROR;