2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2017-2018 Solarflare Communications Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
10 * 1. Redistributions of source code must retain the above copyright notice,
11 * this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright notice,
13 * this list of conditions and the following disclaimer in the documentation
14 * and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
18 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
20 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
23 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
25 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
26 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 * The views and conclusions contained in the software and documentation are
29 * those of the authors and should not be interpreted as representing official
30 * policies, either expressed or implied, of the FreeBSD Project.
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
42 #if EFSYS_OPT_SIENA || EFSYS_OPT_HUNTINGTON
43 static const efx_tunnel_ops_t __efx_tunnel_dummy_ops = {
44 NULL, /* eto_udp_encap_supported */
45 NULL, /* eto_reconfigure */
47 #endif /* EFSYS_OPT_SIENA || EFSYS_OPT_HUNTINGTON */
49 #if EFSYS_OPT_MEDFORD || EFSYS_OPT_MEDFORD2
50 static __checkReturn boolean_t
51 ef10_udp_encap_supported(
54 static __checkReturn efx_rc_t
55 ef10_tunnel_reconfigure(
58 static const efx_tunnel_ops_t __efx_tunnel_ef10_ops = {
59 ef10_udp_encap_supported, /* eto_udp_encap_supported */
60 ef10_tunnel_reconfigure, /* eto_reconfigure */
62 #endif /* EFSYS_OPT_MEDFORD || EFSYS_OPT_MEDFORD2 */
64 static __checkReturn efx_rc_t
65 efx_mcdi_set_tunnel_encap_udp_ports(
67 __in efx_tunnel_cfg_t *etcp,
68 __in boolean_t unloading,
69 __out boolean_t *resetting)
72 EFX_MCDI_DECLARE_BUF(payload,
73 MC_CMD_SET_TUNNEL_ENCAP_UDP_PORTS_IN_LENMAX,
74 MC_CMD_SET_TUNNEL_ENCAP_UDP_PORTS_OUT_LEN);
78 unsigned int entries_num;
83 entries_num = etcp->etc_udp_entries_num;
85 req.emr_cmd = MC_CMD_SET_TUNNEL_ENCAP_UDP_PORTS;
86 req.emr_in_buf = payload;
88 MC_CMD_SET_TUNNEL_ENCAP_UDP_PORTS_IN_LEN(entries_num);
89 req.emr_out_buf = payload;
90 req.emr_out_length = MC_CMD_SET_TUNNEL_ENCAP_UDP_PORTS_OUT_LEN;
92 EFX_POPULATE_WORD_1(flags,
93 MC_CMD_SET_TUNNEL_ENCAP_UDP_PORTS_IN_UNLOADING,
94 (unloading == B_TRUE) ? 1 : 0);
95 MCDI_IN_SET_WORD(req, SET_TUNNEL_ENCAP_UDP_PORTS_IN_FLAGS,
96 EFX_WORD_FIELD(flags, EFX_WORD_0));
98 MCDI_IN_SET_WORD(req, SET_TUNNEL_ENCAP_UDP_PORTS_IN_NUM_ENTRIES,
101 for (i = 0; i < entries_num; ++i) {
102 uint16_t mcdi_udp_protocol;
104 switch (etcp->etc_udp_entries[i].etue_protocol) {
105 case EFX_TUNNEL_PROTOCOL_VXLAN:
106 mcdi_udp_protocol = TUNNEL_ENCAP_UDP_PORT_ENTRY_VXLAN;
108 case EFX_TUNNEL_PROTOCOL_GENEVE:
109 mcdi_udp_protocol = TUNNEL_ENCAP_UDP_PORT_ENTRY_GENEVE;
117 * UDP port is MCDI native little-endian in the request
118 * and EFX_POPULATE_DWORD cares about conversion from
119 * host/CPU byte order to little-endian.
121 EFX_STATIC_ASSERT(sizeof (efx_dword_t) ==
122 TUNNEL_ENCAP_UDP_PORT_ENTRY_LEN);
123 EFX_POPULATE_DWORD_2(
124 MCDI_IN2(req, efx_dword_t,
125 SET_TUNNEL_ENCAP_UDP_PORTS_IN_ENTRIES)[i],
126 TUNNEL_ENCAP_UDP_PORT_ENTRY_UDP_PORT,
127 etcp->etc_udp_entries[i].etue_port,
128 TUNNEL_ENCAP_UDP_PORT_ENTRY_PROTOCOL,
132 efx_mcdi_execute(enp, &req);
134 if (req.emr_rc != 0) {
139 if (req.emr_out_length_used !=
140 MC_CMD_SET_TUNNEL_ENCAP_UDP_PORTS_OUT_LEN) {
145 *resetting = MCDI_OUT_WORD_FIELD(req,
146 SET_TUNNEL_ENCAP_UDP_PORTS_OUT_FLAGS,
147 SET_TUNNEL_ENCAP_UDP_PORTS_OUT_RESETTING);
158 EFSYS_PROBE1(fail1, efx_rc_t, rc);
163 __checkReturn efx_rc_t
167 efx_tunnel_cfg_t *etcp = &enp->en_tunnel_cfg;
168 const efx_tunnel_ops_t *etop;
171 EFSYS_ASSERT3U(enp->en_magic, ==, EFX_NIC_MAGIC);
172 EFSYS_ASSERT3U(enp->en_mod_flags, &, EFX_MOD_PROBE);
173 EFSYS_ASSERT(!(enp->en_mod_flags & EFX_MOD_TUNNEL));
175 EFX_STATIC_ASSERT(EFX_TUNNEL_MAXNENTRIES ==
176 MC_CMD_SET_TUNNEL_ENCAP_UDP_PORTS_IN_ENTRIES_MAXNUM);
178 switch (enp->en_family) {
180 case EFX_FAMILY_SIENA:
181 etop = &__efx_tunnel_dummy_ops;
183 #endif /* EFSYS_OPT_SIENA */
185 #if EFSYS_OPT_HUNTINGTON
186 case EFX_FAMILY_HUNTINGTON:
187 etop = &__efx_tunnel_dummy_ops;
189 #endif /* EFSYS_OPT_HUNTINGTON */
191 #if EFSYS_OPT_MEDFORD
192 case EFX_FAMILY_MEDFORD:
193 etop = &__efx_tunnel_ef10_ops;
195 #endif /* EFSYS_OPT_MEDFORD */
197 #if EFSYS_OPT_MEDFORD2
198 case EFX_FAMILY_MEDFORD2:
199 etop = &__efx_tunnel_ef10_ops;
201 #endif /* EFSYS_OPT_MEDFORD2 */
209 memset(etcp->etc_udp_entries, 0, sizeof (etcp->etc_udp_entries));
210 etcp->etc_udp_entries_num = 0;
213 enp->en_mod_flags |= EFX_MOD_TUNNEL;
218 EFSYS_PROBE1(fail1, efx_rc_t, rc);
221 enp->en_mod_flags &= ~EFX_MOD_TUNNEL;
232 EFSYS_ASSERT3U(enp->en_magic, ==, EFX_NIC_MAGIC);
233 EFSYS_ASSERT3U(enp->en_mod_flags, &, EFX_MOD_PROBE);
234 EFSYS_ASSERT3U(enp->en_mod_flags, &, EFX_MOD_TUNNEL);
236 if ((enp->en_etop->eto_udp_encap_supported != NULL) &&
237 enp->en_etop->eto_udp_encap_supported(enp)) {
239 * The UNLOADING flag allows the MC to suppress the datapath
240 * reset if it was set on the last call to
241 * MC_CMD_SET_TUNNEL_ENCAP_UDP_PORTS by all functions
243 (void) efx_mcdi_set_tunnel_encap_udp_ports(enp, NULL, B_TRUE,
248 enp->en_mod_flags &= ~EFX_MOD_TUNNEL;
251 static __checkReturn efx_rc_t
252 efx_tunnel_config_find_udp_tunnel_entry(
253 __in efx_tunnel_cfg_t *etcp,
255 __out unsigned int *entryp)
259 for (i = 0; i < etcp->etc_udp_entries_num; ++i) {
260 efx_tunnel_udp_entry_t *p = &etcp->etc_udp_entries[i];
262 if (p->etue_port == port) {
271 __checkReturn efx_rc_t
272 efx_tunnel_config_udp_add(
274 __in uint16_t port /* host/cpu-endian */,
275 __in efx_tunnel_protocol_t protocol)
277 const efx_nic_cfg_t *encp = &enp->en_nic_cfg;
278 efx_tunnel_cfg_t *etcp = &enp->en_tunnel_cfg;
279 efsys_lock_state_t state;
283 EFSYS_ASSERT3U(enp->en_mod_flags, &, EFX_MOD_TUNNEL);
285 if (protocol >= EFX_TUNNEL_NPROTOS) {
290 if ((encp->enc_tunnel_encapsulations_supported &
291 (1u << protocol)) == 0) {
296 EFSYS_LOCK(enp->en_eslp, state);
298 rc = efx_tunnel_config_find_udp_tunnel_entry(etcp, port, &entry);
304 if (etcp->etc_udp_entries_num ==
305 encp->enc_tunnel_config_udp_entries_max) {
310 etcp->etc_udp_entries[etcp->etc_udp_entries_num].etue_port = port;
311 etcp->etc_udp_entries[etcp->etc_udp_entries_num].etue_protocol =
314 etcp->etc_udp_entries_num++;
316 EFSYS_UNLOCK(enp->en_eslp, state);
325 EFSYS_UNLOCK(enp->en_eslp, state);
331 EFSYS_PROBE1(fail1, efx_rc_t, rc);
336 __checkReturn efx_rc_t
337 efx_tunnel_config_udp_remove(
339 __in uint16_t port /* host/cpu-endian */,
340 __in efx_tunnel_protocol_t protocol)
342 efx_tunnel_cfg_t *etcp = &enp->en_tunnel_cfg;
343 efsys_lock_state_t state;
347 EFSYS_ASSERT3U(enp->en_mod_flags, &, EFX_MOD_TUNNEL);
349 EFSYS_LOCK(enp->en_eslp, state);
351 rc = efx_tunnel_config_find_udp_tunnel_entry(etcp, port, &entry);
355 if (etcp->etc_udp_entries[entry].etue_protocol != protocol) {
360 EFSYS_ASSERT3U(etcp->etc_udp_entries_num, >, 0);
361 etcp->etc_udp_entries_num--;
363 if (entry < etcp->etc_udp_entries_num) {
364 memmove(&etcp->etc_udp_entries[entry],
365 &etcp->etc_udp_entries[entry + 1],
366 (etcp->etc_udp_entries_num - entry) *
367 sizeof (etcp->etc_udp_entries[0]));
370 memset(&etcp->etc_udp_entries[etcp->etc_udp_entries_num], 0,
371 sizeof (etcp->etc_udp_entries[0]));
373 EFSYS_UNLOCK(enp->en_eslp, state);
381 EFSYS_PROBE1(fail1, efx_rc_t, rc);
382 EFSYS_UNLOCK(enp->en_eslp, state);
388 efx_tunnel_config_clear(
391 efx_tunnel_cfg_t *etcp = &enp->en_tunnel_cfg;
392 efsys_lock_state_t state;
394 EFSYS_ASSERT3U(enp->en_mod_flags, &, EFX_MOD_TUNNEL);
396 EFSYS_LOCK(enp->en_eslp, state);
398 etcp->etc_udp_entries_num = 0;
399 memset(etcp->etc_udp_entries, 0, sizeof (etcp->etc_udp_entries));
401 EFSYS_UNLOCK(enp->en_eslp, state);
404 __checkReturn efx_rc_t
405 efx_tunnel_reconfigure(
408 const efx_tunnel_ops_t *etop = enp->en_etop;
411 EFSYS_ASSERT3U(enp->en_mod_flags, &, EFX_MOD_TUNNEL);
413 if (etop->eto_reconfigure == NULL) {
418 if ((rc = enp->en_etop->eto_reconfigure(enp)) != 0)
427 EFSYS_PROBE1(fail1, efx_rc_t, rc);
432 #if EFSYS_OPT_MEDFORD || EFSYS_OPT_MEDFORD2
433 static __checkReturn boolean_t
434 ef10_udp_encap_supported(
437 const efx_nic_cfg_t *encp = &enp->en_nic_cfg;
438 uint32_t udp_tunnels_mask = 0;
440 udp_tunnels_mask |= (1u << EFX_TUNNEL_PROTOCOL_VXLAN);
441 udp_tunnels_mask |= (1u << EFX_TUNNEL_PROTOCOL_GENEVE);
443 return ((encp->enc_tunnel_encapsulations_supported &
444 udp_tunnels_mask) == 0 ? B_FALSE : B_TRUE);
447 static __checkReturn efx_rc_t
448 ef10_tunnel_reconfigure(
451 efx_tunnel_cfg_t *etcp = &enp->en_tunnel_cfg;
454 efsys_lock_state_t state;
455 efx_tunnel_cfg_t etc;
457 EFSYS_LOCK(enp->en_eslp, state);
458 memcpy(&etc, etcp, sizeof (etc));
459 EFSYS_UNLOCK(enp->en_eslp, state);
461 if (ef10_udp_encap_supported(enp) == B_FALSE) {
463 * It is OK to apply empty UDP tunnel ports when UDP
464 * tunnel encapsulations are not supported - just nothing
467 if (etc.etc_udp_entries_num == 0)
473 * All PCI functions can see a reset upon the
474 * MCDI request completion
476 rc = efx_mcdi_set_tunnel_encap_udp_ports(enp, &etc, B_FALSE,
482 * Although the caller should be able to handle MC reboot,
483 * it might come in handy to report the impending reboot
484 * by returning EAGAIN
486 return ((resetting) ? EAGAIN : 0);
492 EFSYS_PROBE1(fail1, efx_rc_t, rc);
496 #endif /* EFSYS_OPT_MEDFORD || EFSYS_OPT_MEDFORD2 */
498 #endif /* EFSYS_OPT_TUNNEL */