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>
33 static int enic_set_port_profile(struct enic
*enic
)
35 struct net_device
*netdev
= enic
->netdev
;
36 struct vic_provinfo
*vp
;
37 const u8 oui
[3] = VIC_PROVINFO_CISCO_OUI
;
38 const u16 os_type
= htons(VIC_GENERIC_PROV_OS_TYPE_LINUX
);
40 char client_mac_str
[18];
44 if (!(enic
->pp
.set
& ENIC_SET_NAME
) || !strlen(enic
->pp
.name
))
47 vp
= vic_provinfo_alloc(GFP_KERNEL
, oui
,
48 VIC_PROVINFO_GENERIC_TYPE
);
52 VIC_PROVINFO_ADD_TLV(vp
,
53 VIC_GENERIC_PROV_TLV_PORT_PROFILE_NAME_STR
,
54 strlen(enic
->pp
.name
) + 1, enic
->pp
.name
);
56 if (!is_zero_ether_addr(enic
->pp
.mac_addr
))
57 client_mac
= enic
->pp
.mac_addr
;
59 client_mac
= netdev
->dev_addr
;
61 VIC_PROVINFO_ADD_TLV(vp
,
62 VIC_GENERIC_PROV_TLV_CLIENT_MAC_ADDR
,
63 ETH_ALEN
, client_mac
);
65 snprintf(client_mac_str
, sizeof(client_mac_str
), "%pM", client_mac
);
66 VIC_PROVINFO_ADD_TLV(vp
,
67 VIC_GENERIC_PROV_TLV_CLUSTER_PORT_UUID_STR
,
68 sizeof(client_mac_str
), client_mac_str
);
70 if (enic
->pp
.set
& ENIC_SET_INSTANCE
) {
71 sprintf(uuid_str
, "%pUB", enic
->pp
.instance_uuid
);
72 VIC_PROVINFO_ADD_TLV(vp
,
73 VIC_GENERIC_PROV_TLV_CLIENT_UUID_STR
,
74 sizeof(uuid_str
), uuid_str
);
77 if (enic
->pp
.set
& ENIC_SET_HOST
) {
78 sprintf(uuid_str
, "%pUB", enic
->pp
.host_uuid
);
79 VIC_PROVINFO_ADD_TLV(vp
,
80 VIC_GENERIC_PROV_TLV_HOST_UUID_STR
,
81 sizeof(uuid_str
), uuid_str
);
84 VIC_PROVINFO_ADD_TLV(vp
,
85 VIC_GENERIC_PROV_TLV_OS_TYPE
,
86 sizeof(os_type
), &os_type
);
88 err
= enic_dev_status_to_errno(enic_dev_init_prov2(enic
, vp
));
91 vic_provinfo_free(vp
);
96 static int enic_unset_port_profile(struct enic
*enic
)
100 err
= enic_vnic_dev_deinit(enic
);
102 return enic_dev_status_to_errno(err
);
104 enic_reset_addr_lists(enic
);
109 static int enic_are_pp_different(struct enic_port_profile
*pp1
,
110 struct enic_port_profile
*pp2
)
112 return strcmp(pp1
->name
, pp2
->name
) | !!memcmp(pp1
->instance_uuid
,
113 pp2
->instance_uuid
, PORT_UUID_MAX
) |
114 !!memcmp(pp1
->host_uuid
, pp2
->host_uuid
, PORT_UUID_MAX
) |
115 !!memcmp(pp1
->mac_addr
, pp2
->mac_addr
, ETH_ALEN
);
118 static int enic_pp_preassociate(struct enic
*enic
,
119 struct enic_port_profile
*prev_pp
, int *restore_pp
);
120 static int enic_pp_disassociate(struct enic
*enic
,
121 struct enic_port_profile
*prev_pp
, int *restore_pp
);
122 static int enic_pp_preassociate_rr(struct enic
*enic
,
123 struct enic_port_profile
*prev_pp
, int *restore_pp
);
124 static int enic_pp_associate(struct enic
*enic
,
125 struct enic_port_profile
*prev_pp
, int *restore_pp
);
127 static int (*enic_pp_handlers
[])(struct enic
*enic
,
128 struct enic_port_profile
*prev_state
, int *restore_pp
) = {
129 [PORT_REQUEST_PREASSOCIATE
] = enic_pp_preassociate
,
130 [PORT_REQUEST_PREASSOCIATE_RR
] = enic_pp_preassociate_rr
,
131 [PORT_REQUEST_ASSOCIATE
] = enic_pp_associate
,
132 [PORT_REQUEST_DISASSOCIATE
] = enic_pp_disassociate
,
135 static const int enic_pp_handlers_count
=
136 sizeof(enic_pp_handlers
)/sizeof(*enic_pp_handlers
);
138 static int enic_pp_preassociate(struct enic
*enic
,
139 struct enic_port_profile
*prev_pp
, int *restore_pp
)
144 static int enic_pp_disassociate(struct enic
*enic
,
145 struct enic_port_profile
*prev_pp
, int *restore_pp
)
147 return enic_unset_port_profile(enic
);
150 static int enic_pp_preassociate_rr(struct enic
*enic
,
151 struct enic_port_profile
*prev_pp
, int *restore_pp
)
156 if (enic
->pp
.request
!= PORT_REQUEST_ASSOCIATE
) {
157 /* If pre-associate is not part of an associate.
158 We always disassociate first */
159 err
= enic_pp_handlers
[PORT_REQUEST_DISASSOCIATE
](enic
,
160 prev_pp
, restore_pp
);
169 err
= enic_set_port_profile(enic
);
173 /* If pre-associate is not part of an associate. */
174 if (enic
->pp
.request
!= PORT_REQUEST_ASSOCIATE
)
175 err
= enic_dev_status_to_errno(enic_dev_enable2(enic
, active
));
180 static int enic_pp_associate(struct enic
*enic
,
181 struct enic_port_profile
*prev_pp
, int *restore_pp
)
186 /* Check if a pre-associate was called before */
187 if (prev_pp
->request
!= PORT_REQUEST_PREASSOCIATE_RR
||
188 (prev_pp
->request
== PORT_REQUEST_PREASSOCIATE_RR
&&
189 enic_are_pp_different(prev_pp
, &enic
->pp
))) {
190 err
= enic_pp_handlers
[PORT_REQUEST_DISASSOCIATE
](
191 enic
, prev_pp
, restore_pp
);
198 err
= enic_pp_handlers
[PORT_REQUEST_PREASSOCIATE_RR
](
199 enic
, prev_pp
, restore_pp
);
205 return enic_dev_status_to_errno(enic_dev_enable2(enic
, active
));
208 int enic_process_set_pp_request(struct enic
*enic
,
209 struct enic_port_profile
*prev_pp
, int *restore_pp
)
211 if (enic
->pp
.request
< enic_pp_handlers_count
212 && enic_pp_handlers
[enic
->pp
.request
])
213 return enic_pp_handlers
[enic
->pp
.request
](enic
,
214 prev_pp
, restore_pp
);
219 int enic_process_get_pp_request(struct enic
*enic
, int request
,
222 int err
, status
= ERR_SUCCESS
;
226 case PORT_REQUEST_PREASSOCIATE_RR
:
227 case PORT_REQUEST_ASSOCIATE
:
228 err
= enic_dev_enable2_done(enic
, &status
);
231 case PORT_REQUEST_DISASSOCIATE
:
232 err
= enic_dev_deinit_done(enic
, &status
);
244 *response
= PORT_PROFILE_RESPONSE_SUCCESS
;
247 *response
= PORT_PROFILE_RESPONSE_INVALID
;
250 *response
= PORT_PROFILE_RESPONSE_BADSTATE
;
253 *response
= PORT_PROFILE_RESPONSE_INSUFFICIENT_RESOURCES
;
255 case ERR_EINPROGRESS
:
256 *response
= PORT_PROFILE_RESPONSE_INPROGRESS
;
259 *response
= PORT_PROFILE_RESPONSE_ERROR
;