2 * Copyright 2011 Cisco Systems, Inc. All rights reserved.
4 * This program is free software; you may redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 2 of the License.
8 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
9 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
10 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
11 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
12 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
13 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
14 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19 #include <linux/kernel.h>
20 #include <linux/string.h>
21 #include <linux/errno.h>
22 #include <linux/types.h>
23 #include <linux/netdevice.h>
24 #include <linux/etherdevice.h>
25 #include <linux/rtnetlink.h>
35 * Checks validity of vf index that came in
36 * port profile request
38 int enic_is_valid_pp_vf(struct enic
*enic
, int vf
, int *err
)
40 if (vf
!= PORT_SELF_VF
) {
42 if (enic_sriov_enabled(enic
)) {
43 if (vf
< 0 || vf
>= enic
->num_vfs
) {
57 if (vf
== PORT_SELF_VF
&& !enic_is_dynamic(enic
)) {
69 static int enic_set_port_profile(struct enic
*enic
, int vf
)
71 struct net_device
*netdev
= enic
->netdev
;
72 struct enic_port_profile
*pp
;
73 struct vic_provinfo
*vp
;
74 const u8 oui
[3] = VIC_PROVINFO_CISCO_OUI
;
75 const __be16 os_type
= htons(VIC_GENERIC_PROV_OS_TYPE_LINUX
);
77 char client_mac_str
[18];
81 ENIC_PP_BY_INDEX(enic
, vf
, pp
, &err
);
85 if (!(pp
->set
& ENIC_SET_NAME
) || !strlen(pp
->name
))
88 vp
= vic_provinfo_alloc(GFP_KERNEL
, oui
,
89 VIC_PROVINFO_GENERIC_TYPE
);
93 VIC_PROVINFO_ADD_TLV(vp
,
94 VIC_GENERIC_PROV_TLV_PORT_PROFILE_NAME_STR
,
95 strlen(pp
->name
) + 1, pp
->name
);
97 if (!is_zero_ether_addr(pp
->mac_addr
)) {
98 client_mac
= pp
->mac_addr
;
99 } else if (vf
== PORT_SELF_VF
) {
100 client_mac
= netdev
->dev_addr
;
102 netdev_err(netdev
, "Cannot find pp mac address "
105 goto add_tlv_failure
;
108 VIC_PROVINFO_ADD_TLV(vp
,
109 VIC_GENERIC_PROV_TLV_CLIENT_MAC_ADDR
,
110 ETH_ALEN
, client_mac
);
112 snprintf(client_mac_str
, sizeof(client_mac_str
), "%pM", client_mac
);
113 VIC_PROVINFO_ADD_TLV(vp
,
114 VIC_GENERIC_PROV_TLV_CLUSTER_PORT_UUID_STR
,
115 sizeof(client_mac_str
), client_mac_str
);
117 if (pp
->set
& ENIC_SET_INSTANCE
) {
118 sprintf(uuid_str
, "%pUB", pp
->instance_uuid
);
119 VIC_PROVINFO_ADD_TLV(vp
,
120 VIC_GENERIC_PROV_TLV_CLIENT_UUID_STR
,
121 sizeof(uuid_str
), uuid_str
);
124 if (pp
->set
& ENIC_SET_HOST
) {
125 sprintf(uuid_str
, "%pUB", pp
->host_uuid
);
126 VIC_PROVINFO_ADD_TLV(vp
,
127 VIC_GENERIC_PROV_TLV_HOST_UUID_STR
,
128 sizeof(uuid_str
), uuid_str
);
131 VIC_PROVINFO_ADD_TLV(vp
,
132 VIC_GENERIC_PROV_TLV_OS_TYPE
,
133 sizeof(os_type
), &os_type
);
135 ENIC_DEVCMD_PROXY_BY_INDEX(vf
, err
, enic
, vnic_dev_init_prov2
, (u8
*)vp
,
136 vic_provinfo_size(vp
));
137 err
= enic_dev_status_to_errno(err
);
140 vic_provinfo_free(vp
);
145 static int enic_unset_port_profile(struct enic
*enic
, int vf
)
149 ENIC_DEVCMD_PROXY_BY_INDEX(vf
, err
, enic
, vnic_dev_deinit
);
151 return enic_dev_status_to_errno(err
);
153 if (vf
== PORT_SELF_VF
)
154 enic_reset_addr_lists(enic
);
159 static int enic_are_pp_different(struct enic_port_profile
*pp1
,
160 struct enic_port_profile
*pp2
)
162 return strcmp(pp1
->name
, pp2
->name
) | !!memcmp(pp1
->instance_uuid
,
163 pp2
->instance_uuid
, PORT_UUID_MAX
) |
164 !!memcmp(pp1
->host_uuid
, pp2
->host_uuid
, PORT_UUID_MAX
) |
165 !!memcmp(pp1
->mac_addr
, pp2
->mac_addr
, ETH_ALEN
);
168 static int enic_pp_preassociate(struct enic
*enic
, int vf
,
169 struct enic_port_profile
*prev_pp
, int *restore_pp
);
170 static int enic_pp_disassociate(struct enic
*enic
, int vf
,
171 struct enic_port_profile
*prev_pp
, int *restore_pp
);
172 static int enic_pp_preassociate_rr(struct enic
*enic
, int vf
,
173 struct enic_port_profile
*prev_pp
, int *restore_pp
);
174 static int enic_pp_associate(struct enic
*enic
, int vf
,
175 struct enic_port_profile
*prev_pp
, int *restore_pp
);
177 static int (*enic_pp_handlers
[])(struct enic
*enic
, int vf
,
178 struct enic_port_profile
*prev_state
,
180 [PORT_REQUEST_PREASSOCIATE
] = enic_pp_preassociate
,
181 [PORT_REQUEST_PREASSOCIATE_RR
] = enic_pp_preassociate_rr
,
182 [PORT_REQUEST_ASSOCIATE
] = enic_pp_associate
,
183 [PORT_REQUEST_DISASSOCIATE
] = enic_pp_disassociate
,
186 static const int enic_pp_handlers_count
=
187 sizeof(enic_pp_handlers
)/sizeof(*enic_pp_handlers
);
189 static int enic_pp_preassociate(struct enic
*enic
, int vf
,
190 struct enic_port_profile
*prev_pp
, int *restore_pp
)
195 static int enic_pp_disassociate(struct enic
*enic
, int vf
,
196 struct enic_port_profile
*prev_pp
, int *restore_pp
)
198 struct net_device
*netdev
= enic
->netdev
;
199 struct enic_port_profile
*pp
;
202 ENIC_PP_BY_INDEX(enic
, vf
, pp
, &err
);
206 /* Deregister mac addresses */
207 if (!is_zero_ether_addr(pp
->mac_addr
))
208 ENIC_DEVCMD_PROXY_BY_INDEX(vf
, err
, enic
, vnic_dev_del_addr
,
210 else if (!is_zero_ether_addr(netdev
->dev_addr
))
211 ENIC_DEVCMD_PROXY_BY_INDEX(vf
, err
, enic
, vnic_dev_del_addr
,
214 return enic_unset_port_profile(enic
, vf
);
217 static int enic_pp_preassociate_rr(struct enic
*enic
, int vf
,
218 struct enic_port_profile
*prev_pp
, int *restore_pp
)
220 struct enic_port_profile
*pp
;
224 ENIC_PP_BY_INDEX(enic
, vf
, pp
, &err
);
228 if (pp
->request
!= PORT_REQUEST_ASSOCIATE
) {
229 /* If pre-associate is not part of an associate.
230 We always disassociate first */
231 err
= enic_pp_handlers
[PORT_REQUEST_DISASSOCIATE
](enic
, vf
,
232 prev_pp
, restore_pp
);
241 err
= enic_set_port_profile(enic
, vf
);
245 /* If pre-associate is not part of an associate. */
246 if (pp
->request
!= PORT_REQUEST_ASSOCIATE
) {
247 /* Enable device as standby */
248 ENIC_DEVCMD_PROXY_BY_INDEX(vf
, err
, enic
, vnic_dev_enable2
,
250 err
= enic_dev_status_to_errno(err
);
256 static int enic_pp_associate(struct enic
*enic
, int vf
,
257 struct enic_port_profile
*prev_pp
, int *restore_pp
)
259 struct net_device
*netdev
= enic
->netdev
;
260 struct enic_port_profile
*pp
;
264 ENIC_PP_BY_INDEX(enic
, vf
, pp
, &err
);
268 /* Check if a pre-associate was called before */
269 if (prev_pp
->request
!= PORT_REQUEST_PREASSOCIATE_RR
||
270 (prev_pp
->request
== PORT_REQUEST_PREASSOCIATE_RR
&&
271 enic_are_pp_different(prev_pp
, pp
))) {
272 err
= enic_pp_handlers
[PORT_REQUEST_DISASSOCIATE
](
273 enic
, vf
, prev_pp
, restore_pp
);
280 err
= enic_pp_handlers
[PORT_REQUEST_PREASSOCIATE_RR
](
281 enic
, vf
, prev_pp
, restore_pp
);
287 /* Enable device as active */
288 ENIC_DEVCMD_PROXY_BY_INDEX(vf
, err
, enic
, vnic_dev_enable2
, active
);
289 err
= enic_dev_status_to_errno(err
);
293 /* Register mac address */
294 if (!is_zero_ether_addr(pp
->mac_addr
))
295 ENIC_DEVCMD_PROXY_BY_INDEX(vf
, err
, enic
, vnic_dev_add_addr
,
297 else if (!is_zero_ether_addr(netdev
->dev_addr
))
298 ENIC_DEVCMD_PROXY_BY_INDEX(vf
, err
, enic
, vnic_dev_add_addr
,
304 int enic_process_set_pp_request(struct enic
*enic
, int vf
,
305 struct enic_port_profile
*prev_pp
, int *restore_pp
)
307 struct enic_port_profile
*pp
;
310 ENIC_PP_BY_INDEX(enic
, vf
, pp
, &err
);
314 if (pp
->request
>= enic_pp_handlers_count
315 || !enic_pp_handlers
[pp
->request
])
318 return enic_pp_handlers
[pp
->request
](enic
, vf
, prev_pp
, restore_pp
);
321 int enic_process_get_pp_request(struct enic
*enic
, int vf
,
322 int request
, u16
*response
)
324 int err
, status
= ERR_SUCCESS
;
328 case PORT_REQUEST_PREASSOCIATE_RR
:
329 case PORT_REQUEST_ASSOCIATE
:
330 ENIC_DEVCMD_PROXY_BY_INDEX(vf
, err
, enic
,
331 vnic_dev_enable2_done
, &status
);
334 case PORT_REQUEST_DISASSOCIATE
:
335 ENIC_DEVCMD_PROXY_BY_INDEX(vf
, err
, enic
,
336 vnic_dev_deinit_done
, &status
);
348 *response
= PORT_PROFILE_RESPONSE_SUCCESS
;
351 *response
= PORT_PROFILE_RESPONSE_INVALID
;
354 *response
= PORT_PROFILE_RESPONSE_BADSTATE
;
357 *response
= PORT_PROFILE_RESPONSE_INSUFFICIENT_RESOURCES
;
359 case ERR_EINPROGRESS
:
360 *response
= PORT_PROFILE_RESPONSE_INPROGRESS
;
363 *response
= PORT_PROFILE_RESPONSE_ERROR
;