2 * Copyright (c) 2017 Solarflare Communications Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
16 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
21 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
24 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 * The views and conclusions contained in the software and documentation are
27 * those of the authors and should not be interpreted as representing official
28 * policies, either expressed or implied, of the FreeBSD Project.
37 #if EFSYS_OPT_SIENA || EFSYS_OPT_HUNTINGTON
38 static const efx_tunnel_ops_t __efx_tunnel_dummy_ops = {
39 NULL, /* eto_udp_encap_supported */
40 NULL, /* eto_reconfigure */
42 #endif /* EFSYS_OPT_SIENA || EFSYS_OPT_HUNTINGTON */
45 static __checkReturn boolean_t
46 medford_udp_encap_supported(
49 static __checkReturn efx_rc_t
50 medford_tunnel_reconfigure(
53 static const efx_tunnel_ops_t __efx_tunnel_medford_ops = {
54 medford_udp_encap_supported, /* eto_udp_encap_supported */
55 medford_tunnel_reconfigure, /* eto_reconfigure */
57 #endif /* EFSYS_OPT_MEDFORD */
59 static __checkReturn efx_rc_t
60 efx_mcdi_set_tunnel_encap_udp_ports(
62 __in efx_tunnel_cfg_t *etcp,
63 __in boolean_t unloading,
64 __out boolean_t *resetting)
67 uint8_t payload[MAX(MC_CMD_SET_TUNNEL_ENCAP_UDP_PORTS_IN_LENMAX,
68 MC_CMD_SET_TUNNEL_ENCAP_UDP_PORTS_OUT_LEN)];
72 unsigned int entries_num;
77 entries_num = etcp->etc_udp_entries_num;
79 (void) memset(payload, 0, sizeof (payload));
80 req.emr_cmd = MC_CMD_SET_TUNNEL_ENCAP_UDP_PORTS;
81 req.emr_in_buf = payload;
83 MC_CMD_SET_TUNNEL_ENCAP_UDP_PORTS_IN_LEN(entries_num);
84 req.emr_out_buf = payload;
85 req.emr_out_length = MC_CMD_SET_TUNNEL_ENCAP_UDP_PORTS_OUT_LEN;
87 EFX_POPULATE_WORD_1(flags,
88 MC_CMD_SET_TUNNEL_ENCAP_UDP_PORTS_IN_UNLOADING,
89 (unloading == B_TRUE) ? 1 : 0);
90 MCDI_IN_SET_WORD(req, SET_TUNNEL_ENCAP_UDP_PORTS_IN_FLAGS,
91 EFX_WORD_FIELD(flags, EFX_WORD_0));
93 MCDI_IN_SET_WORD(req, SET_TUNNEL_ENCAP_UDP_PORTS_IN_NUM_ENTRIES,
96 for (i = 0; i < entries_num; ++i) {
97 uint16_t mcdi_udp_protocol;
99 switch (etcp->etc_udp_entries[i].etue_protocol) {
100 case EFX_TUNNEL_PROTOCOL_VXLAN:
101 mcdi_udp_protocol = TUNNEL_ENCAP_UDP_PORT_ENTRY_VXLAN;
103 case EFX_TUNNEL_PROTOCOL_GENEVE:
104 mcdi_udp_protocol = TUNNEL_ENCAP_UDP_PORT_ENTRY_GENEVE;
112 * UDP port is MCDI native little-endian in the request
113 * and EFX_POPULATE_DWORD cares about conversion from
114 * host/CPU byte order to little-endian.
116 EFX_STATIC_ASSERT(sizeof (efx_dword_t) ==
117 TUNNEL_ENCAP_UDP_PORT_ENTRY_LEN);
118 EFX_POPULATE_DWORD_2(
119 MCDI_IN2(req, efx_dword_t,
120 SET_TUNNEL_ENCAP_UDP_PORTS_IN_ENTRIES)[i],
121 TUNNEL_ENCAP_UDP_PORT_ENTRY_UDP_PORT,
122 etcp->etc_udp_entries[i].etue_port,
123 TUNNEL_ENCAP_UDP_PORT_ENTRY_PROTOCOL,
127 efx_mcdi_execute(enp, &req);
129 if (req.emr_rc != 0) {
134 if (req.emr_out_length_used !=
135 MC_CMD_SET_TUNNEL_ENCAP_UDP_PORTS_OUT_LEN) {
140 *resetting = MCDI_OUT_WORD_FIELD(req,
141 SET_TUNNEL_ENCAP_UDP_PORTS_OUT_FLAGS,
142 SET_TUNNEL_ENCAP_UDP_PORTS_OUT_RESETTING);
153 EFSYS_PROBE1(fail1, efx_rc_t, rc);
158 __checkReturn efx_rc_t
162 efx_tunnel_cfg_t *etcp = &enp->en_tunnel_cfg;
163 const efx_tunnel_ops_t *etop;
166 EFSYS_ASSERT3U(enp->en_magic, ==, EFX_NIC_MAGIC);
167 EFSYS_ASSERT3U(enp->en_mod_flags, &, EFX_MOD_PROBE);
168 EFSYS_ASSERT(!(enp->en_mod_flags & EFX_MOD_TUNNEL));
170 EFX_STATIC_ASSERT(EFX_TUNNEL_MAXNENTRIES ==
171 MC_CMD_SET_TUNNEL_ENCAP_UDP_PORTS_IN_ENTRIES_MAXNUM);
173 switch (enp->en_family) {
175 case EFX_FAMILY_SIENA:
176 etop = &__efx_tunnel_dummy_ops;
178 #endif /* EFSYS_OPT_SIENA */
180 #if EFSYS_OPT_HUNTINGTON
181 case EFX_FAMILY_HUNTINGTON:
182 etop = &__efx_tunnel_dummy_ops;
184 #endif /* EFSYS_OPT_HUNTINGTON */
186 #if EFSYS_OPT_MEDFORD
187 case EFX_FAMILY_MEDFORD:
188 etop = &__efx_tunnel_medford_ops;
190 #endif /* EFSYS_OPT_MEDFORD */
198 memset(etcp->etc_udp_entries, 0, sizeof (etcp->etc_udp_entries));
199 etcp->etc_udp_entries_num = 0;
202 enp->en_mod_flags |= EFX_MOD_TUNNEL;
207 EFSYS_PROBE1(fail1, efx_rc_t, rc);
210 enp->en_mod_flags &= ~EFX_MOD_TUNNEL;
221 EFSYS_ASSERT3U(enp->en_magic, ==, EFX_NIC_MAGIC);
222 EFSYS_ASSERT3U(enp->en_mod_flags, &, EFX_MOD_PROBE);
223 EFSYS_ASSERT3U(enp->en_mod_flags, &, EFX_MOD_TUNNEL);
225 if ((enp->en_etop->eto_udp_encap_supported != NULL) &&
226 enp->en_etop->eto_udp_encap_supported(enp)) {
228 * The UNLOADING flag allows the MC to suppress the datapath
229 * reset if it was set on the last call to
230 * MC_CMD_SET_TUNNEL_ENCAP_UDP_PORTS by all functions
232 (void) efx_mcdi_set_tunnel_encap_udp_ports(enp, NULL, B_TRUE,
237 enp->en_mod_flags &= ~EFX_MOD_TUNNEL;
240 static __checkReturn efx_rc_t
241 efx_tunnel_config_find_udp_tunnel_entry(
242 __in efx_tunnel_cfg_t *etcp,
244 __out unsigned int *entryp)
248 for (i = 0; i < etcp->etc_udp_entries_num; ++i) {
249 efx_tunnel_udp_entry_t *p = &etcp->etc_udp_entries[i];
251 if (p->etue_port == port) {
260 __checkReturn efx_rc_t
261 efx_tunnel_config_udp_add(
263 __in uint16_t port /* host/cpu-endian */,
264 __in efx_tunnel_protocol_t protocol)
266 const efx_nic_cfg_t *encp = &enp->en_nic_cfg;
267 efx_tunnel_cfg_t *etcp = &enp->en_tunnel_cfg;
268 efsys_lock_state_t state;
272 EFSYS_ASSERT3U(enp->en_mod_flags, &, EFX_MOD_TUNNEL);
274 if (protocol >= EFX_TUNNEL_NPROTOS) {
279 if ((encp->enc_tunnel_encapsulations_supported &
280 (1u << protocol)) == 0) {
285 EFSYS_LOCK(enp->en_eslp, state);
287 rc = efx_tunnel_config_find_udp_tunnel_entry(etcp, port, &entry);
293 if (etcp->etc_udp_entries_num ==
294 encp->enc_tunnel_config_udp_entries_max) {
299 etcp->etc_udp_entries[etcp->etc_udp_entries_num].etue_port = port;
300 etcp->etc_udp_entries[etcp->etc_udp_entries_num].etue_protocol =
303 etcp->etc_udp_entries_num++;
305 EFSYS_UNLOCK(enp->en_eslp, state);
314 EFSYS_UNLOCK(enp->en_eslp, state);
320 EFSYS_PROBE1(fail1, efx_rc_t, rc);
325 __checkReturn efx_rc_t
326 efx_tunnel_config_udp_remove(
328 __in uint16_t port /* host/cpu-endian */,
329 __in efx_tunnel_protocol_t protocol)
331 efx_tunnel_cfg_t *etcp = &enp->en_tunnel_cfg;
332 efsys_lock_state_t state;
336 EFSYS_ASSERT3U(enp->en_mod_flags, &, EFX_MOD_TUNNEL);
338 EFSYS_LOCK(enp->en_eslp, state);
340 rc = efx_tunnel_config_find_udp_tunnel_entry(etcp, port, &entry);
344 if (etcp->etc_udp_entries[entry].etue_protocol != protocol) {
349 EFSYS_ASSERT3U(etcp->etc_udp_entries_num, >, 0);
350 etcp->etc_udp_entries_num--;
352 if (entry < etcp->etc_udp_entries_num) {
353 memmove(&etcp->etc_udp_entries[entry],
354 &etcp->etc_udp_entries[entry + 1],
355 (etcp->etc_udp_entries_num - entry) *
356 sizeof (etcp->etc_udp_entries[0]));
359 memset(&etcp->etc_udp_entries[etcp->etc_udp_entries_num], 0,
360 sizeof (etcp->etc_udp_entries[0]));
362 EFSYS_UNLOCK(enp->en_eslp, state);
370 EFSYS_PROBE1(fail1, efx_rc_t, rc);
371 EFSYS_UNLOCK(enp->en_eslp, state);
377 efx_tunnel_config_clear(
380 efx_tunnel_cfg_t *etcp = &enp->en_tunnel_cfg;
381 efsys_lock_state_t state;
383 EFSYS_ASSERT3U(enp->en_mod_flags, &, EFX_MOD_TUNNEL);
385 EFSYS_LOCK(enp->en_eslp, state);
387 etcp->etc_udp_entries_num = 0;
388 memset(etcp->etc_udp_entries, 0, sizeof (etcp->etc_udp_entries));
390 EFSYS_UNLOCK(enp->en_eslp, state);
393 __checkReturn efx_rc_t
394 efx_tunnel_reconfigure(
397 const efx_tunnel_ops_t *etop = enp->en_etop;
400 EFSYS_ASSERT3U(enp->en_mod_flags, &, EFX_MOD_TUNNEL);
402 if (etop->eto_reconfigure == NULL) {
407 if ((rc = enp->en_etop->eto_reconfigure(enp)) != 0)
416 EFSYS_PROBE1(fail1, efx_rc_t, rc);
421 #if EFSYS_OPT_MEDFORD
422 static __checkReturn boolean_t
423 medford_udp_encap_supported(
426 const efx_nic_cfg_t *encp = &enp->en_nic_cfg;
427 uint32_t udp_tunnels_mask = 0;
429 udp_tunnels_mask |= (1u << EFX_TUNNEL_PROTOCOL_VXLAN);
430 udp_tunnels_mask |= (1u << EFX_TUNNEL_PROTOCOL_GENEVE);
432 return ((encp->enc_tunnel_encapsulations_supported &
433 udp_tunnels_mask) == 0 ? B_FALSE : B_TRUE);
436 static __checkReturn efx_rc_t
437 medford_tunnel_reconfigure(
440 efx_tunnel_cfg_t *etcp = &enp->en_tunnel_cfg;
443 efsys_lock_state_t state;
444 efx_tunnel_cfg_t etc;
446 EFSYS_LOCK(enp->en_eslp, state);
447 memcpy(&etc, etcp, sizeof (etc));
448 EFSYS_UNLOCK(enp->en_eslp, state);
450 if (medford_udp_encap_supported(enp) == B_FALSE) {
452 * It is OK to apply empty UDP tunnel ports when UDP
453 * tunnel encapsulations are not supported - just nothing
456 if (etc.etc_udp_entries_num == 0)
462 * All PCI functions can see a reset upon the
463 * MCDI request completion
465 rc = efx_mcdi_set_tunnel_encap_udp_ports(enp, &etc, B_FALSE,
471 * Although the caller should be able to handle MC reboot,
472 * it might come in handy to report the impending reboot
473 * by returning EAGAIN
475 return ((resetting) ? EAGAIN : 0);
481 EFSYS_PROBE1(fail1, efx_rc_t, rc);
485 #endif /* EFSYS_OPT_MEDFORD */
487 #endif /* EFSYS_OPT_TUNNEL */