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 */
50 static __checkReturn boolean_t
51 medford_udp_encap_supported(
54 static __checkReturn efx_rc_t
55 medford_tunnel_reconfigure(
58 static const efx_tunnel_ops_t __efx_tunnel_medford_ops = {
59 medford_udp_encap_supported, /* eto_udp_encap_supported */
60 medford_tunnel_reconfigure, /* eto_reconfigure */
62 #endif /* EFSYS_OPT_MEDFORD */
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 uint8_t payload[MAX(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 (void) memset(payload, 0, sizeof (payload));
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_medford_ops;
195 #endif /* EFSYS_OPT_MEDFORD */
203 memset(etcp->etc_udp_entries, 0, sizeof (etcp->etc_udp_entries));
204 etcp->etc_udp_entries_num = 0;
207 enp->en_mod_flags |= EFX_MOD_TUNNEL;
212 EFSYS_PROBE1(fail1, efx_rc_t, rc);
215 enp->en_mod_flags &= ~EFX_MOD_TUNNEL;
226 EFSYS_ASSERT3U(enp->en_magic, ==, EFX_NIC_MAGIC);
227 EFSYS_ASSERT3U(enp->en_mod_flags, &, EFX_MOD_PROBE);
228 EFSYS_ASSERT3U(enp->en_mod_flags, &, EFX_MOD_TUNNEL);
230 if ((enp->en_etop->eto_udp_encap_supported != NULL) &&
231 enp->en_etop->eto_udp_encap_supported(enp)) {
233 * The UNLOADING flag allows the MC to suppress the datapath
234 * reset if it was set on the last call to
235 * MC_CMD_SET_TUNNEL_ENCAP_UDP_PORTS by all functions
237 (void) efx_mcdi_set_tunnel_encap_udp_ports(enp, NULL, B_TRUE,
242 enp->en_mod_flags &= ~EFX_MOD_TUNNEL;
245 static __checkReturn efx_rc_t
246 efx_tunnel_config_find_udp_tunnel_entry(
247 __in efx_tunnel_cfg_t *etcp,
249 __out unsigned int *entryp)
253 for (i = 0; i < etcp->etc_udp_entries_num; ++i) {
254 efx_tunnel_udp_entry_t *p = &etcp->etc_udp_entries[i];
256 if (p->etue_port == port) {
265 __checkReturn efx_rc_t
266 efx_tunnel_config_udp_add(
268 __in uint16_t port /* host/cpu-endian */,
269 __in efx_tunnel_protocol_t protocol)
271 const efx_nic_cfg_t *encp = &enp->en_nic_cfg;
272 efx_tunnel_cfg_t *etcp = &enp->en_tunnel_cfg;
273 efsys_lock_state_t state;
277 EFSYS_ASSERT3U(enp->en_mod_flags, &, EFX_MOD_TUNNEL);
279 if (protocol >= EFX_TUNNEL_NPROTOS) {
284 if ((encp->enc_tunnel_encapsulations_supported &
285 (1u << protocol)) == 0) {
290 EFSYS_LOCK(enp->en_eslp, state);
292 rc = efx_tunnel_config_find_udp_tunnel_entry(etcp, port, &entry);
298 if (etcp->etc_udp_entries_num ==
299 encp->enc_tunnel_config_udp_entries_max) {
304 etcp->etc_udp_entries[etcp->etc_udp_entries_num].etue_port = port;
305 etcp->etc_udp_entries[etcp->etc_udp_entries_num].etue_protocol =
308 etcp->etc_udp_entries_num++;
310 EFSYS_UNLOCK(enp->en_eslp, state);
319 EFSYS_UNLOCK(enp->en_eslp, state);
325 EFSYS_PROBE1(fail1, efx_rc_t, rc);
330 __checkReturn efx_rc_t
331 efx_tunnel_config_udp_remove(
333 __in uint16_t port /* host/cpu-endian */,
334 __in efx_tunnel_protocol_t protocol)
336 efx_tunnel_cfg_t *etcp = &enp->en_tunnel_cfg;
337 efsys_lock_state_t state;
341 EFSYS_ASSERT3U(enp->en_mod_flags, &, EFX_MOD_TUNNEL);
343 EFSYS_LOCK(enp->en_eslp, state);
345 rc = efx_tunnel_config_find_udp_tunnel_entry(etcp, port, &entry);
349 if (etcp->etc_udp_entries[entry].etue_protocol != protocol) {
354 EFSYS_ASSERT3U(etcp->etc_udp_entries_num, >, 0);
355 etcp->etc_udp_entries_num--;
357 if (entry < etcp->etc_udp_entries_num) {
358 memmove(&etcp->etc_udp_entries[entry],
359 &etcp->etc_udp_entries[entry + 1],
360 (etcp->etc_udp_entries_num - entry) *
361 sizeof (etcp->etc_udp_entries[0]));
364 memset(&etcp->etc_udp_entries[etcp->etc_udp_entries_num], 0,
365 sizeof (etcp->etc_udp_entries[0]));
367 EFSYS_UNLOCK(enp->en_eslp, state);
375 EFSYS_PROBE1(fail1, efx_rc_t, rc);
376 EFSYS_UNLOCK(enp->en_eslp, state);
382 efx_tunnel_config_clear(
385 efx_tunnel_cfg_t *etcp = &enp->en_tunnel_cfg;
386 efsys_lock_state_t state;
388 EFSYS_ASSERT3U(enp->en_mod_flags, &, EFX_MOD_TUNNEL);
390 EFSYS_LOCK(enp->en_eslp, state);
392 etcp->etc_udp_entries_num = 0;
393 memset(etcp->etc_udp_entries, 0, sizeof (etcp->etc_udp_entries));
395 EFSYS_UNLOCK(enp->en_eslp, state);
398 __checkReturn efx_rc_t
399 efx_tunnel_reconfigure(
402 const efx_tunnel_ops_t *etop = enp->en_etop;
405 EFSYS_ASSERT3U(enp->en_mod_flags, &, EFX_MOD_TUNNEL);
407 if (etop->eto_reconfigure == NULL) {
412 if ((rc = enp->en_etop->eto_reconfigure(enp)) != 0)
421 EFSYS_PROBE1(fail1, efx_rc_t, rc);
426 #if EFSYS_OPT_MEDFORD
427 static __checkReturn boolean_t
428 medford_udp_encap_supported(
431 const efx_nic_cfg_t *encp = &enp->en_nic_cfg;
432 uint32_t udp_tunnels_mask = 0;
434 udp_tunnels_mask |= (1u << EFX_TUNNEL_PROTOCOL_VXLAN);
435 udp_tunnels_mask |= (1u << EFX_TUNNEL_PROTOCOL_GENEVE);
437 return ((encp->enc_tunnel_encapsulations_supported &
438 udp_tunnels_mask) == 0 ? B_FALSE : B_TRUE);
441 static __checkReturn efx_rc_t
442 medford_tunnel_reconfigure(
445 efx_tunnel_cfg_t *etcp = &enp->en_tunnel_cfg;
448 efsys_lock_state_t state;
449 efx_tunnel_cfg_t etc;
451 EFSYS_LOCK(enp->en_eslp, state);
452 memcpy(&etc, etcp, sizeof (etc));
453 EFSYS_UNLOCK(enp->en_eslp, state);
455 if (medford_udp_encap_supported(enp) == B_FALSE) {
457 * It is OK to apply empty UDP tunnel ports when UDP
458 * tunnel encapsulations are not supported - just nothing
461 if (etc.etc_udp_entries_num == 0)
467 * All PCI functions can see a reset upon the
468 * MCDI request completion
470 rc = efx_mcdi_set_tunnel_encap_udp_ports(enp, &etc, B_FALSE,
476 * Although the caller should be able to handle MC reboot,
477 * it might come in handy to report the impending reboot
478 * by returning EAGAIN
480 return ((resetting) ? EAGAIN : 0);
486 EFSYS_PROBE1(fail1, efx_rc_t, rc);
490 #endif /* EFSYS_OPT_MEDFORD */
492 #endif /* EFSYS_OPT_TUNNEL */