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$");
41 #if EFSYS_OPT_SIENA || EFSYS_OPT_HUNTINGTON
42 static const efx_tunnel_ops_t __efx_tunnel_dummy_ops = {
43 NULL, /* eto_udp_encap_supported */
44 NULL, /* eto_reconfigure */
46 #endif /* EFSYS_OPT_SIENA || EFSYS_OPT_HUNTINGTON */
48 #if EFSYS_OPT_MEDFORD || EFSYS_OPT_MEDFORD2
49 static __checkReturn boolean_t
50 ef10_udp_encap_supported(
53 static __checkReturn efx_rc_t
54 ef10_tunnel_reconfigure(
57 static const efx_tunnel_ops_t __efx_tunnel_ef10_ops = {
58 ef10_udp_encap_supported, /* eto_udp_encap_supported */
59 ef10_tunnel_reconfigure, /* eto_reconfigure */
61 #endif /* EFSYS_OPT_MEDFORD || EFSYS_OPT_MEDFORD2 */
63 static __checkReturn efx_rc_t
64 efx_mcdi_set_tunnel_encap_udp_ports(
66 __in efx_tunnel_cfg_t *etcp,
67 __in boolean_t unloading,
68 __out boolean_t *resetting)
71 EFX_MCDI_DECLARE_BUF(payload,
72 MC_CMD_SET_TUNNEL_ENCAP_UDP_PORTS_IN_LENMAX,
73 MC_CMD_SET_TUNNEL_ENCAP_UDP_PORTS_OUT_LEN);
77 unsigned int entries_num;
82 entries_num = etcp->etc_udp_entries_num;
84 req.emr_cmd = MC_CMD_SET_TUNNEL_ENCAP_UDP_PORTS;
85 req.emr_in_buf = payload;
87 MC_CMD_SET_TUNNEL_ENCAP_UDP_PORTS_IN_LEN(entries_num);
88 req.emr_out_buf = payload;
89 req.emr_out_length = MC_CMD_SET_TUNNEL_ENCAP_UDP_PORTS_OUT_LEN;
91 EFX_POPULATE_WORD_1(flags,
92 MC_CMD_SET_TUNNEL_ENCAP_UDP_PORTS_IN_UNLOADING,
93 (unloading == B_TRUE) ? 1 : 0);
94 MCDI_IN_SET_WORD(req, SET_TUNNEL_ENCAP_UDP_PORTS_IN_FLAGS,
95 EFX_WORD_FIELD(flags, EFX_WORD_0));
97 MCDI_IN_SET_WORD(req, SET_TUNNEL_ENCAP_UDP_PORTS_IN_NUM_ENTRIES,
100 for (i = 0; i < entries_num; ++i) {
101 uint16_t mcdi_udp_protocol;
103 switch (etcp->etc_udp_entries[i].etue_protocol) {
104 case EFX_TUNNEL_PROTOCOL_VXLAN:
105 mcdi_udp_protocol = TUNNEL_ENCAP_UDP_PORT_ENTRY_VXLAN;
107 case EFX_TUNNEL_PROTOCOL_GENEVE:
108 mcdi_udp_protocol = TUNNEL_ENCAP_UDP_PORT_ENTRY_GENEVE;
116 * UDP port is MCDI native little-endian in the request
117 * and EFX_POPULATE_DWORD cares about conversion from
118 * host/CPU byte order to little-endian.
120 EFX_STATIC_ASSERT(sizeof (efx_dword_t) ==
121 TUNNEL_ENCAP_UDP_PORT_ENTRY_LEN);
122 EFX_POPULATE_DWORD_2(
123 MCDI_IN2(req, efx_dword_t,
124 SET_TUNNEL_ENCAP_UDP_PORTS_IN_ENTRIES)[i],
125 TUNNEL_ENCAP_UDP_PORT_ENTRY_UDP_PORT,
126 etcp->etc_udp_entries[i].etue_port,
127 TUNNEL_ENCAP_UDP_PORT_ENTRY_PROTOCOL,
131 efx_mcdi_execute(enp, &req);
133 if (req.emr_rc != 0) {
138 if (req.emr_out_length_used !=
139 MC_CMD_SET_TUNNEL_ENCAP_UDP_PORTS_OUT_LEN) {
144 *resetting = MCDI_OUT_WORD_FIELD(req,
145 SET_TUNNEL_ENCAP_UDP_PORTS_OUT_FLAGS,
146 SET_TUNNEL_ENCAP_UDP_PORTS_OUT_RESETTING);
157 EFSYS_PROBE1(fail1, efx_rc_t, rc);
162 __checkReturn efx_rc_t
166 efx_tunnel_cfg_t *etcp = &enp->en_tunnel_cfg;
167 const efx_tunnel_ops_t *etop;
170 EFSYS_ASSERT3U(enp->en_magic, ==, EFX_NIC_MAGIC);
171 EFSYS_ASSERT3U(enp->en_mod_flags, &, EFX_MOD_PROBE);
172 EFSYS_ASSERT(!(enp->en_mod_flags & EFX_MOD_TUNNEL));
174 EFX_STATIC_ASSERT(EFX_TUNNEL_MAXNENTRIES ==
175 MC_CMD_SET_TUNNEL_ENCAP_UDP_PORTS_IN_ENTRIES_MAXNUM);
177 switch (enp->en_family) {
179 case EFX_FAMILY_SIENA:
180 etop = &__efx_tunnel_dummy_ops;
182 #endif /* EFSYS_OPT_SIENA */
184 #if EFSYS_OPT_HUNTINGTON
185 case EFX_FAMILY_HUNTINGTON:
186 etop = &__efx_tunnel_dummy_ops;
188 #endif /* EFSYS_OPT_HUNTINGTON */
190 #if EFSYS_OPT_MEDFORD
191 case EFX_FAMILY_MEDFORD:
192 etop = &__efx_tunnel_ef10_ops;
194 #endif /* EFSYS_OPT_MEDFORD */
196 #if EFSYS_OPT_MEDFORD2
197 case EFX_FAMILY_MEDFORD2:
198 etop = &__efx_tunnel_ef10_ops;
200 #endif /* EFSYS_OPT_MEDFORD2 */
208 memset(etcp->etc_udp_entries, 0, sizeof (etcp->etc_udp_entries));
209 etcp->etc_udp_entries_num = 0;
212 enp->en_mod_flags |= EFX_MOD_TUNNEL;
217 EFSYS_PROBE1(fail1, efx_rc_t, rc);
220 enp->en_mod_flags &= ~EFX_MOD_TUNNEL;
231 EFSYS_ASSERT3U(enp->en_magic, ==, EFX_NIC_MAGIC);
232 EFSYS_ASSERT3U(enp->en_mod_flags, &, EFX_MOD_PROBE);
233 EFSYS_ASSERT3U(enp->en_mod_flags, &, EFX_MOD_TUNNEL);
235 if ((enp->en_etop->eto_udp_encap_supported != NULL) &&
236 enp->en_etop->eto_udp_encap_supported(enp)) {
238 * The UNLOADING flag allows the MC to suppress the datapath
239 * reset if it was set on the last call to
240 * MC_CMD_SET_TUNNEL_ENCAP_UDP_PORTS by all functions
242 (void) efx_mcdi_set_tunnel_encap_udp_ports(enp, NULL, B_TRUE,
247 enp->en_mod_flags &= ~EFX_MOD_TUNNEL;
250 static __checkReturn efx_rc_t
251 efx_tunnel_config_find_udp_tunnel_entry(
252 __in efx_tunnel_cfg_t *etcp,
254 __out unsigned int *entryp)
258 for (i = 0; i < etcp->etc_udp_entries_num; ++i) {
259 efx_tunnel_udp_entry_t *p = &etcp->etc_udp_entries[i];
261 if (p->etue_port == port) {
270 __checkReturn efx_rc_t
271 efx_tunnel_config_udp_add(
273 __in uint16_t port /* host/cpu-endian */,
274 __in efx_tunnel_protocol_t protocol)
276 const efx_nic_cfg_t *encp = &enp->en_nic_cfg;
277 efx_tunnel_cfg_t *etcp = &enp->en_tunnel_cfg;
278 efsys_lock_state_t state;
282 EFSYS_ASSERT3U(enp->en_mod_flags, &, EFX_MOD_TUNNEL);
284 if (protocol >= EFX_TUNNEL_NPROTOS) {
289 if ((encp->enc_tunnel_encapsulations_supported &
290 (1u << protocol)) == 0) {
295 EFSYS_LOCK(enp->en_eslp, state);
297 rc = efx_tunnel_config_find_udp_tunnel_entry(etcp, port, &entry);
303 if (etcp->etc_udp_entries_num ==
304 encp->enc_tunnel_config_udp_entries_max) {
309 etcp->etc_udp_entries[etcp->etc_udp_entries_num].etue_port = port;
310 etcp->etc_udp_entries[etcp->etc_udp_entries_num].etue_protocol =
313 etcp->etc_udp_entries_num++;
315 EFSYS_UNLOCK(enp->en_eslp, state);
324 EFSYS_UNLOCK(enp->en_eslp, state);
330 EFSYS_PROBE1(fail1, efx_rc_t, rc);
335 __checkReturn efx_rc_t
336 efx_tunnel_config_udp_remove(
338 __in uint16_t port /* host/cpu-endian */,
339 __in efx_tunnel_protocol_t protocol)
341 efx_tunnel_cfg_t *etcp = &enp->en_tunnel_cfg;
342 efsys_lock_state_t state;
346 EFSYS_ASSERT3U(enp->en_mod_flags, &, EFX_MOD_TUNNEL);
348 EFSYS_LOCK(enp->en_eslp, state);
350 rc = efx_tunnel_config_find_udp_tunnel_entry(etcp, port, &entry);
354 if (etcp->etc_udp_entries[entry].etue_protocol != protocol) {
359 EFSYS_ASSERT3U(etcp->etc_udp_entries_num, >, 0);
360 etcp->etc_udp_entries_num--;
362 if (entry < etcp->etc_udp_entries_num) {
363 memmove(&etcp->etc_udp_entries[entry],
364 &etcp->etc_udp_entries[entry + 1],
365 (etcp->etc_udp_entries_num - entry) *
366 sizeof (etcp->etc_udp_entries[0]));
369 memset(&etcp->etc_udp_entries[etcp->etc_udp_entries_num], 0,
370 sizeof (etcp->etc_udp_entries[0]));
372 EFSYS_UNLOCK(enp->en_eslp, state);
380 EFSYS_PROBE1(fail1, efx_rc_t, rc);
381 EFSYS_UNLOCK(enp->en_eslp, state);
387 efx_tunnel_config_clear(
390 efx_tunnel_cfg_t *etcp = &enp->en_tunnel_cfg;
391 efsys_lock_state_t state;
393 EFSYS_ASSERT3U(enp->en_mod_flags, &, EFX_MOD_TUNNEL);
395 EFSYS_LOCK(enp->en_eslp, state);
397 etcp->etc_udp_entries_num = 0;
398 memset(etcp->etc_udp_entries, 0, sizeof (etcp->etc_udp_entries));
400 EFSYS_UNLOCK(enp->en_eslp, state);
403 __checkReturn efx_rc_t
404 efx_tunnel_reconfigure(
407 const efx_tunnel_ops_t *etop = enp->en_etop;
410 EFSYS_ASSERT3U(enp->en_mod_flags, &, EFX_MOD_TUNNEL);
412 if (etop->eto_reconfigure == NULL) {
417 if ((rc = enp->en_etop->eto_reconfigure(enp)) != 0)
426 EFSYS_PROBE1(fail1, efx_rc_t, rc);
431 #if EFSYS_OPT_MEDFORD || EFSYS_OPT_MEDFORD2
432 static __checkReturn boolean_t
433 ef10_udp_encap_supported(
436 const efx_nic_cfg_t *encp = &enp->en_nic_cfg;
437 uint32_t udp_tunnels_mask = 0;
439 udp_tunnels_mask |= (1u << EFX_TUNNEL_PROTOCOL_VXLAN);
440 udp_tunnels_mask |= (1u << EFX_TUNNEL_PROTOCOL_GENEVE);
442 return ((encp->enc_tunnel_encapsulations_supported &
443 udp_tunnels_mask) == 0 ? B_FALSE : B_TRUE);
446 static __checkReturn efx_rc_t
447 ef10_tunnel_reconfigure(
450 efx_tunnel_cfg_t *etcp = &enp->en_tunnel_cfg;
453 efsys_lock_state_t state;
454 efx_tunnel_cfg_t etc;
456 EFSYS_LOCK(enp->en_eslp, state);
457 memcpy(&etc, etcp, sizeof (etc));
458 EFSYS_UNLOCK(enp->en_eslp, state);
460 if (ef10_udp_encap_supported(enp) == B_FALSE) {
462 * It is OK to apply empty UDP tunnel ports when UDP
463 * tunnel encapsulations are not supported - just nothing
466 if (etc.etc_udp_entries_num == 0)
472 * All PCI functions can see a reset upon the
473 * MCDI request completion
475 rc = efx_mcdi_set_tunnel_encap_udp_ports(enp, &etc, B_FALSE,
481 * Although the caller should be able to handle MC reboot,
482 * it might come in handy to report the impending reboot
483 * by returning EAGAIN
485 return ((resetting) ? EAGAIN : 0);
491 EFSYS_PROBE1(fail1, efx_rc_t, rc);
495 #endif /* EFSYS_OPT_MEDFORD || EFSYS_OPT_MEDFORD2 */
497 #endif /* EFSYS_OPT_TUNNEL */