1 /* SPDX-License-Identifier: BSD-3-Clause
3 * Copyright(c) 2021 Xilinx, Inc.
9 #if EFSYS_OPT_RIVERHEAD && EFSYS_OPT_TUNNEL
11 /* Match by Ether-type */
12 #define EFX_VNIC_ENCAP_RULE_MATCH_ETHER_TYPE \
13 (1u << MC_CMD_VNIC_ENCAP_RULE_ADD_IN_MATCH_ETHER_TYPE_LBN)
14 /* Match by outer VLAN ID */
15 #define EFX_VNIC_ENCAP_RULE_MATCH_OUTER_VID \
16 (1u << MC_CMD_VNIC_ENCAP_RULE_ADD_IN_MATCH_OUTER_VLAN_LBN)
17 /* Match by local IP host address */
18 #define EFX_VNIC_ENCAP_RULE_MATCH_LOC_HOST \
19 (1u << MC_CMD_VNIC_ENCAP_RULE_ADD_IN_MATCH_DST_IP_LBN)
20 /* Match by IP transport protocol */
21 #define EFX_VNIC_ENCAP_RULE_MATCH_IP_PROTO \
22 (1u << MC_CMD_VNIC_ENCAP_RULE_ADD_IN_MATCH_IP_PROTO_LBN)
23 /* Match by local TCP/UDP port */
24 #define EFX_VNIC_ENCAP_RULE_MATCH_LOC_PORT \
25 (1u << MC_CMD_VNIC_ENCAP_RULE_ADD_IN_MATCH_DST_PORT_LBN)
28 * Helper structure to pass parameters to MCDI function to add a VNIC
31 typedef struct efx_vnic_encap_rule_spec_s {
32 uint32_t evers_mport_selector; /* Host-endian */
33 uint32_t evers_match_flags; /* Host-endian */
34 uint16_t evers_ether_type; /* Host-endian */
35 uint16_t evers_outer_vid; /* Host-endian */
36 efx_oword_t evers_loc_host; /* Big-endian */
37 uint8_t evers_ip_proto;
38 uint16_t evers_loc_port; /* Host-endian */
39 efx_tunnel_protocol_t evers_encap_type;
40 } efx_vnic_encap_rule_spec_t;
43 efx_tunnel_protocol2mae_encap_type(
44 __in efx_tunnel_protocol_t proto,
45 __out uint32_t *typep)
50 case EFX_TUNNEL_PROTOCOL_NONE:
51 *typep = MAE_MCDI_ENCAP_TYPE_NONE;
53 case EFX_TUNNEL_PROTOCOL_VXLAN:
54 *typep = MAE_MCDI_ENCAP_TYPE_VXLAN;
56 case EFX_TUNNEL_PROTOCOL_GENEVE:
57 *typep = MAE_MCDI_ENCAP_TYPE_GENEVE;
59 case EFX_TUNNEL_PROTOCOL_NVGRE:
60 *typep = MAE_MCDI_ENCAP_TYPE_NVGRE;
70 EFSYS_PROBE1(fail1, efx_rc_t, rc);
75 static __checkReturn efx_rc_t
76 efx_mcdi_vnic_encap_rule_add(
78 __in const efx_vnic_encap_rule_spec_t *spec,
79 __out efx_vnic_encap_rule_handle_t *handle)
83 EFX_MCDI_DECLARE_BUF(payload,
84 MC_CMD_VNIC_ENCAP_RULE_ADD_IN_LEN,
85 MC_CMD_VNIC_ENCAP_RULE_ADD_OUT_LEN);
89 req.emr_cmd = MC_CMD_VNIC_ENCAP_RULE_ADD;
90 req.emr_in_buf = payload;
91 req.emr_in_length = MC_CMD_VNIC_ENCAP_RULE_ADD_IN_LEN;
92 req.emr_out_buf = payload;
93 req.emr_out_length = MC_CMD_VNIC_ENCAP_RULE_ADD_OUT_LEN;
95 MCDI_IN_SET_DWORD(req, VNIC_ENCAP_RULE_ADD_IN_MPORT_SELECTOR,
96 spec->evers_mport_selector);
97 MCDI_IN_SET_DWORD(req, VNIC_ENCAP_RULE_ADD_IN_MATCH_FLAGS,
98 spec->evers_match_flags);
100 MCDI_IN_SET_WORD_NATIVE(req, VNIC_ENCAP_RULE_ADD_IN_ETHER_TYPE,
101 __CPU_TO_BE_16(spec->evers_ether_type));
102 MCDI_IN_SET_WORD_NATIVE(req, VNIC_ENCAP_RULE_ADD_IN_OUTER_VLAN_WORD,
103 __CPU_TO_BE_16(spec->evers_outer_vid));
106 * Address is already in network order as well as the MCDI field,
107 * so plain copy is used.
109 EFX_STATIC_ASSERT(sizeof (spec->evers_loc_host) ==
110 MC_CMD_VNIC_ENCAP_RULE_ADD_IN_DST_IP_LEN);
111 memcpy(MCDI_IN2(req, uint8_t, VNIC_ENCAP_RULE_ADD_IN_DST_IP),
112 &spec->evers_loc_host.eo_byte[0],
113 MC_CMD_VNIC_ENCAP_RULE_ADD_IN_DST_IP_LEN);
115 MCDI_IN_SET_BYTE(req, VNIC_ENCAP_RULE_ADD_IN_IP_PROTO,
116 spec->evers_ip_proto);
117 MCDI_IN_SET_WORD_NATIVE(req, VNIC_ENCAP_RULE_ADD_IN_DST_PORT,
118 __CPU_TO_BE_16(spec->evers_loc_port));
120 rc = efx_tunnel_protocol2mae_encap_type(spec->evers_encap_type,
125 MCDI_IN_SET_DWORD(req, VNIC_ENCAP_RULE_ADD_IN_ENCAP_TYPE, encap_type);
127 efx_mcdi_execute(enp, &req);
129 if (req.emr_rc != 0) {
134 if (req.emr_out_length_used != MC_CMD_VNIC_ENCAP_RULE_ADD_OUT_LEN) {
140 *handle = MCDI_OUT_DWORD(req, VNIC_ENCAP_RULE_ADD_OUT_HANDLE);
151 EFSYS_PROBE1(fail1, efx_rc_t, rc);
156 static __checkReturn efx_rc_t
157 efx_mcdi_vnic_encap_rule_remove(
159 __in efx_vnic_encap_rule_handle_t handle)
163 EFX_MCDI_DECLARE_BUF(payload,
164 MC_CMD_VNIC_ENCAP_RULE_REMOVE_IN_LEN,
165 MC_CMD_VNIC_ENCAP_RULE_REMOVE_OUT_LEN);
168 req.emr_cmd = MC_CMD_VNIC_ENCAP_RULE_REMOVE;
169 req.emr_in_buf = payload;
170 req.emr_in_length = MC_CMD_VNIC_ENCAP_RULE_REMOVE_IN_LEN;
171 req.emr_out_buf = payload;
172 req.emr_out_length = MC_CMD_VNIC_ENCAP_RULE_REMOVE_OUT_LEN;
174 MCDI_IN_SET_DWORD(req, VNIC_ENCAP_RULE_REMOVE_IN_HANDLE, handle);
176 efx_mcdi_execute(enp, &req);
178 if (req.emr_rc != 0) {
183 if (req.emr_out_length_used != MC_CMD_VNIC_ENCAP_RULE_REMOVE_OUT_LEN) {
194 EFSYS_PROBE1(fail1, efx_rc_t, rc);
200 rhead_vnic_encap_rule_spec_init(
201 __in const efx_tunnel_udp_entry_t *etuep,
202 __out efx_vnic_encap_rule_spec_t *spec)
204 memset(spec, 0, sizeof (*spec));
206 spec->evers_mport_selector = MAE_MPORT_SELECTOR_ASSIGNED;
207 spec->evers_match_flags = EFX_VNIC_ENCAP_RULE_MATCH_IP_PROTO |
208 EFX_VNIC_ENCAP_RULE_MATCH_LOC_PORT;
209 spec->evers_ip_proto = EFX_IPPROTO_UDP;
210 spec->evers_loc_port = etuep->etue_port;
211 spec->evers_encap_type = etuep->etue_protocol;
214 static __checkReturn efx_rc_t
215 rhead_udp_port_tunnel_add(
217 __inout efx_tunnel_udp_entry_t *etuep)
219 efx_vnic_encap_rule_spec_t spec;
221 rhead_vnic_encap_rule_spec_init(etuep, &spec);
222 return (efx_mcdi_vnic_encap_rule_add(enp, &spec, &etuep->etue_handle));
225 static __checkReturn efx_rc_t
226 rhead_udp_port_tunnel_remove(
228 __in efx_tunnel_udp_entry_t *etuep)
230 return (efx_mcdi_vnic_encap_rule_remove(enp, etuep->etue_handle));
233 __checkReturn efx_rc_t
234 rhead_tunnel_reconfigure(
237 efx_tunnel_cfg_t *etcp = &enp->en_tunnel_cfg;
239 efsys_lock_state_t state;
240 efx_tunnel_cfg_t etc;
241 efx_tunnel_cfg_t added;
245 memset(&added, 0, sizeof(added));
248 * Make a local copy of UDP tunnel table to release the lock
249 * when executing MCDIs.
251 EFSYS_LOCK(enp->en_eslp, state);
252 memcpy(&etc, etcp, sizeof (etc));
253 EFSYS_UNLOCK(enp->en_eslp, state);
255 for (i = 0; i < etc.etc_udp_entries_num; i++) {
256 efx_tunnel_udp_entry_t *etc_entry = &etc.etc_udp_entries[i];
258 if (etc_entry->etue_busy == B_FALSE)
261 switch (etc_entry->etue_state) {
262 case EFX_TUNNEL_UDP_ENTRY_APPLIED:
264 case EFX_TUNNEL_UDP_ENTRY_ADDED:
265 rc = rhead_udp_port_tunnel_add(enp, etc_entry);
268 added.etc_udp_entries[added.etc_udp_entries_num] =
270 added.etc_udp_entries_num++;
272 case EFX_TUNNEL_UDP_ENTRY_REMOVED:
273 rc = rhead_udp_port_tunnel_remove(enp, etc_entry);
283 EFSYS_LOCK(enp->en_eslp, state);
286 * Adding or removing non-busy entries does not change the
287 * order of busy entries. Therefore one linear search iteration
290 for (i = 0, j = 0; i < etcp->etc_udp_entries_num; i++) {
291 efx_tunnel_udp_entry_t *cur_entry = &etcp->etc_udp_entries[i];
292 efx_tunnel_udp_entry_t *added_entry = &added.etc_udp_entries[j];
294 if (cur_entry->etue_state == EFX_TUNNEL_UDP_ENTRY_ADDED &&
295 cur_entry->etue_port == added_entry->etue_port) {
296 cur_entry->etue_handle = added_entry->etue_handle;
301 EFSYS_UNLOCK(enp->en_eslp, state);
309 EFSYS_PROBE1(fail1, efx_rc_t, rc);
312 if (etc.etc_udp_entries[i].etue_busy == B_FALSE)
315 switch (etc.etc_udp_entries[i].etue_state) {
316 case EFX_TUNNEL_UDP_ENTRY_APPLIED:
318 case EFX_TUNNEL_UDP_ENTRY_ADDED:
319 (void) rhead_udp_port_tunnel_remove(enp,
320 &etc.etc_udp_entries[i]);
322 case EFX_TUNNEL_UDP_ENTRY_REMOVED:
323 (void) rhead_udp_port_tunnel_add(enp,
324 &etc.etc_udp_entries[i]);
339 (void) efx_tunnel_config_clear(enp);
340 (void) efx_tunnel_reconfigure(enp);
343 #endif /* EFSYS_OPT_RIVERHEAD && EFSYS_OPT_TUNNEL */