1fdbe38cfSAndrew Rybchenko /*-
2*4d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
3fdbe38cfSAndrew Rybchenko *
4fdbe38cfSAndrew Rybchenko * Copyright (c) 2017-2018 Solarflare Communications Inc.
5fdbe38cfSAndrew Rybchenko * All rights reserved.
6fdbe38cfSAndrew Rybchenko *
7fdbe38cfSAndrew Rybchenko * Redistribution and use in source and binary forms, with or without
8fdbe38cfSAndrew Rybchenko * modification, are permitted provided that the following conditions are met:
9fdbe38cfSAndrew Rybchenko *
10fdbe38cfSAndrew Rybchenko * 1. Redistributions of source code must retain the above copyright notice,
11fdbe38cfSAndrew Rybchenko * this list of conditions and the following disclaimer.
12fdbe38cfSAndrew Rybchenko * 2. Redistributions in binary form must reproduce the above copyright notice,
13fdbe38cfSAndrew Rybchenko * this list of conditions and the following disclaimer in the documentation
14fdbe38cfSAndrew Rybchenko * and/or other materials provided with the distribution.
15fdbe38cfSAndrew Rybchenko *
16fdbe38cfSAndrew Rybchenko * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17fdbe38cfSAndrew Rybchenko * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
18fdbe38cfSAndrew Rybchenko * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19fdbe38cfSAndrew Rybchenko * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
20fdbe38cfSAndrew Rybchenko * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21fdbe38cfSAndrew Rybchenko * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22fdbe38cfSAndrew Rybchenko * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
23fdbe38cfSAndrew Rybchenko * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24fdbe38cfSAndrew Rybchenko * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
25fdbe38cfSAndrew Rybchenko * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
26fdbe38cfSAndrew Rybchenko * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27fdbe38cfSAndrew Rybchenko *
28fdbe38cfSAndrew Rybchenko * The views and conclusions contained in the software and documentation are
29fdbe38cfSAndrew Rybchenko * those of the authors and should not be interpreted as representing official
30fdbe38cfSAndrew Rybchenko * policies, either expressed or implied, of the FreeBSD Project.
31fdbe38cfSAndrew Rybchenko */
32fdbe38cfSAndrew Rybchenko
33fdbe38cfSAndrew Rybchenko #include <sys/cdefs.h>
34fdbe38cfSAndrew Rybchenko #include "efx.h"
35fdbe38cfSAndrew Rybchenko #include "efx_impl.h"
36fdbe38cfSAndrew Rybchenko
37fdbe38cfSAndrew Rybchenko #if EFSYS_OPT_TUNNEL
38fdbe38cfSAndrew Rybchenko
39fdbe38cfSAndrew Rybchenko #if EFSYS_OPT_SIENA || EFSYS_OPT_HUNTINGTON
40fdbe38cfSAndrew Rybchenko static const efx_tunnel_ops_t __efx_tunnel_dummy_ops = {
41fdbe38cfSAndrew Rybchenko NULL, /* eto_udp_encap_supported */
42fdbe38cfSAndrew Rybchenko NULL, /* eto_reconfigure */
43fdbe38cfSAndrew Rybchenko };
44fdbe38cfSAndrew Rybchenko #endif /* EFSYS_OPT_SIENA || EFSYS_OPT_HUNTINGTON */
45fdbe38cfSAndrew Rybchenko
46edaff290SAndrew Rybchenko #if EFSYS_OPT_MEDFORD || EFSYS_OPT_MEDFORD2
47fdbe38cfSAndrew Rybchenko static __checkReturn boolean_t
48edaff290SAndrew Rybchenko ef10_udp_encap_supported(
49fdbe38cfSAndrew Rybchenko __in efx_nic_t *enp);
50fdbe38cfSAndrew Rybchenko
51fdbe38cfSAndrew Rybchenko static __checkReturn efx_rc_t
52edaff290SAndrew Rybchenko ef10_tunnel_reconfigure(
53fdbe38cfSAndrew Rybchenko __in efx_nic_t *enp);
54fdbe38cfSAndrew Rybchenko
55edaff290SAndrew Rybchenko static const efx_tunnel_ops_t __efx_tunnel_ef10_ops = {
56edaff290SAndrew Rybchenko ef10_udp_encap_supported, /* eto_udp_encap_supported */
57edaff290SAndrew Rybchenko ef10_tunnel_reconfigure, /* eto_reconfigure */
58fdbe38cfSAndrew Rybchenko };
59edaff290SAndrew Rybchenko #endif /* EFSYS_OPT_MEDFORD || EFSYS_OPT_MEDFORD2 */
60fdbe38cfSAndrew Rybchenko
61fdbe38cfSAndrew Rybchenko static __checkReturn efx_rc_t
efx_mcdi_set_tunnel_encap_udp_ports(__in efx_nic_t * enp,__in efx_tunnel_cfg_t * etcp,__in boolean_t unloading,__out boolean_t * resetting)62fdbe38cfSAndrew Rybchenko efx_mcdi_set_tunnel_encap_udp_ports(
63fdbe38cfSAndrew Rybchenko __in efx_nic_t *enp,
64fdbe38cfSAndrew Rybchenko __in efx_tunnel_cfg_t *etcp,
65fdbe38cfSAndrew Rybchenko __in boolean_t unloading,
66fdbe38cfSAndrew Rybchenko __out boolean_t *resetting)
67fdbe38cfSAndrew Rybchenko {
68fdbe38cfSAndrew Rybchenko efx_mcdi_req_t req;
69315bbbaaSAndrew Rybchenko EFX_MCDI_DECLARE_BUF(payload,
70315bbbaaSAndrew Rybchenko MC_CMD_SET_TUNNEL_ENCAP_UDP_PORTS_IN_LENMAX,
71315bbbaaSAndrew Rybchenko MC_CMD_SET_TUNNEL_ENCAP_UDP_PORTS_OUT_LEN);
72fdbe38cfSAndrew Rybchenko efx_word_t flags;
73fdbe38cfSAndrew Rybchenko efx_rc_t rc;
74fdbe38cfSAndrew Rybchenko unsigned int i;
75fdbe38cfSAndrew Rybchenko unsigned int entries_num;
76fdbe38cfSAndrew Rybchenko
77fdbe38cfSAndrew Rybchenko if (etcp == NULL)
78fdbe38cfSAndrew Rybchenko entries_num = 0;
79fdbe38cfSAndrew Rybchenko else
80fdbe38cfSAndrew Rybchenko entries_num = etcp->etc_udp_entries_num;
81fdbe38cfSAndrew Rybchenko
82fdbe38cfSAndrew Rybchenko req.emr_cmd = MC_CMD_SET_TUNNEL_ENCAP_UDP_PORTS;
83fdbe38cfSAndrew Rybchenko req.emr_in_buf = payload;
84fdbe38cfSAndrew Rybchenko req.emr_in_length =
85fdbe38cfSAndrew Rybchenko MC_CMD_SET_TUNNEL_ENCAP_UDP_PORTS_IN_LEN(entries_num);
86fdbe38cfSAndrew Rybchenko req.emr_out_buf = payload;
87fdbe38cfSAndrew Rybchenko req.emr_out_length = MC_CMD_SET_TUNNEL_ENCAP_UDP_PORTS_OUT_LEN;
88fdbe38cfSAndrew Rybchenko
89fdbe38cfSAndrew Rybchenko EFX_POPULATE_WORD_1(flags,
90fdbe38cfSAndrew Rybchenko MC_CMD_SET_TUNNEL_ENCAP_UDP_PORTS_IN_UNLOADING,
91fdbe38cfSAndrew Rybchenko (unloading == B_TRUE) ? 1 : 0);
92fdbe38cfSAndrew Rybchenko MCDI_IN_SET_WORD(req, SET_TUNNEL_ENCAP_UDP_PORTS_IN_FLAGS,
93fdbe38cfSAndrew Rybchenko EFX_WORD_FIELD(flags, EFX_WORD_0));
94fdbe38cfSAndrew Rybchenko
95fdbe38cfSAndrew Rybchenko MCDI_IN_SET_WORD(req, SET_TUNNEL_ENCAP_UDP_PORTS_IN_NUM_ENTRIES,
96fdbe38cfSAndrew Rybchenko entries_num);
97fdbe38cfSAndrew Rybchenko
98fdbe38cfSAndrew Rybchenko for (i = 0; i < entries_num; ++i) {
99fdbe38cfSAndrew Rybchenko uint16_t mcdi_udp_protocol;
100fdbe38cfSAndrew Rybchenko
101fdbe38cfSAndrew Rybchenko switch (etcp->etc_udp_entries[i].etue_protocol) {
102fdbe38cfSAndrew Rybchenko case EFX_TUNNEL_PROTOCOL_VXLAN:
103fdbe38cfSAndrew Rybchenko mcdi_udp_protocol = TUNNEL_ENCAP_UDP_PORT_ENTRY_VXLAN;
104fdbe38cfSAndrew Rybchenko break;
105fdbe38cfSAndrew Rybchenko case EFX_TUNNEL_PROTOCOL_GENEVE:
106fdbe38cfSAndrew Rybchenko mcdi_udp_protocol = TUNNEL_ENCAP_UDP_PORT_ENTRY_GENEVE;
107fdbe38cfSAndrew Rybchenko break;
108fdbe38cfSAndrew Rybchenko default:
109fdbe38cfSAndrew Rybchenko rc = EINVAL;
110fdbe38cfSAndrew Rybchenko goto fail1;
111fdbe38cfSAndrew Rybchenko }
112fdbe38cfSAndrew Rybchenko
113fdbe38cfSAndrew Rybchenko /*
114fdbe38cfSAndrew Rybchenko * UDP port is MCDI native little-endian in the request
115fdbe38cfSAndrew Rybchenko * and EFX_POPULATE_DWORD cares about conversion from
116fdbe38cfSAndrew Rybchenko * host/CPU byte order to little-endian.
117fdbe38cfSAndrew Rybchenko */
118fdbe38cfSAndrew Rybchenko EFX_STATIC_ASSERT(sizeof (efx_dword_t) ==
119fdbe38cfSAndrew Rybchenko TUNNEL_ENCAP_UDP_PORT_ENTRY_LEN);
120fdbe38cfSAndrew Rybchenko EFX_POPULATE_DWORD_2(
121fdbe38cfSAndrew Rybchenko MCDI_IN2(req, efx_dword_t,
122fdbe38cfSAndrew Rybchenko SET_TUNNEL_ENCAP_UDP_PORTS_IN_ENTRIES)[i],
123fdbe38cfSAndrew Rybchenko TUNNEL_ENCAP_UDP_PORT_ENTRY_UDP_PORT,
124fdbe38cfSAndrew Rybchenko etcp->etc_udp_entries[i].etue_port,
125fdbe38cfSAndrew Rybchenko TUNNEL_ENCAP_UDP_PORT_ENTRY_PROTOCOL,
126fdbe38cfSAndrew Rybchenko mcdi_udp_protocol);
127fdbe38cfSAndrew Rybchenko }
128fdbe38cfSAndrew Rybchenko
129fdbe38cfSAndrew Rybchenko efx_mcdi_execute(enp, &req);
130fdbe38cfSAndrew Rybchenko
131fdbe38cfSAndrew Rybchenko if (req.emr_rc != 0) {
132fdbe38cfSAndrew Rybchenko rc = req.emr_rc;
133fdbe38cfSAndrew Rybchenko goto fail2;
134fdbe38cfSAndrew Rybchenko }
135fdbe38cfSAndrew Rybchenko
136fdbe38cfSAndrew Rybchenko if (req.emr_out_length_used !=
137fdbe38cfSAndrew Rybchenko MC_CMD_SET_TUNNEL_ENCAP_UDP_PORTS_OUT_LEN) {
138fdbe38cfSAndrew Rybchenko rc = EMSGSIZE;
139fdbe38cfSAndrew Rybchenko goto fail3;
140fdbe38cfSAndrew Rybchenko }
141fdbe38cfSAndrew Rybchenko
142fdbe38cfSAndrew Rybchenko *resetting = MCDI_OUT_WORD_FIELD(req,
143fdbe38cfSAndrew Rybchenko SET_TUNNEL_ENCAP_UDP_PORTS_OUT_FLAGS,
144fdbe38cfSAndrew Rybchenko SET_TUNNEL_ENCAP_UDP_PORTS_OUT_RESETTING);
145fdbe38cfSAndrew Rybchenko
146fdbe38cfSAndrew Rybchenko return (0);
147fdbe38cfSAndrew Rybchenko
148fdbe38cfSAndrew Rybchenko fail3:
149fdbe38cfSAndrew Rybchenko EFSYS_PROBE(fail3);
150fdbe38cfSAndrew Rybchenko
151fdbe38cfSAndrew Rybchenko fail2:
152fdbe38cfSAndrew Rybchenko EFSYS_PROBE(fail2);
153fdbe38cfSAndrew Rybchenko
154fdbe38cfSAndrew Rybchenko fail1:
155fdbe38cfSAndrew Rybchenko EFSYS_PROBE1(fail1, efx_rc_t, rc);
156fdbe38cfSAndrew Rybchenko
157fdbe38cfSAndrew Rybchenko return (rc);
158fdbe38cfSAndrew Rybchenko }
159fdbe38cfSAndrew Rybchenko
160fdbe38cfSAndrew Rybchenko __checkReturn efx_rc_t
efx_tunnel_init(__in efx_nic_t * enp)161fdbe38cfSAndrew Rybchenko efx_tunnel_init(
162fdbe38cfSAndrew Rybchenko __in efx_nic_t *enp)
163fdbe38cfSAndrew Rybchenko {
164fdbe38cfSAndrew Rybchenko efx_tunnel_cfg_t *etcp = &enp->en_tunnel_cfg;
165fdbe38cfSAndrew Rybchenko const efx_tunnel_ops_t *etop;
166fdbe38cfSAndrew Rybchenko efx_rc_t rc;
167fdbe38cfSAndrew Rybchenko
168fdbe38cfSAndrew Rybchenko EFSYS_ASSERT3U(enp->en_magic, ==, EFX_NIC_MAGIC);
169fdbe38cfSAndrew Rybchenko EFSYS_ASSERT3U(enp->en_mod_flags, &, EFX_MOD_PROBE);
170fdbe38cfSAndrew Rybchenko EFSYS_ASSERT(!(enp->en_mod_flags & EFX_MOD_TUNNEL));
171fdbe38cfSAndrew Rybchenko
172fdbe38cfSAndrew Rybchenko EFX_STATIC_ASSERT(EFX_TUNNEL_MAXNENTRIES ==
173fdbe38cfSAndrew Rybchenko MC_CMD_SET_TUNNEL_ENCAP_UDP_PORTS_IN_ENTRIES_MAXNUM);
174fdbe38cfSAndrew Rybchenko
175fdbe38cfSAndrew Rybchenko switch (enp->en_family) {
176fdbe38cfSAndrew Rybchenko #if EFSYS_OPT_SIENA
177fdbe38cfSAndrew Rybchenko case EFX_FAMILY_SIENA:
178fdbe38cfSAndrew Rybchenko etop = &__efx_tunnel_dummy_ops;
179fdbe38cfSAndrew Rybchenko break;
180fdbe38cfSAndrew Rybchenko #endif /* EFSYS_OPT_SIENA */
181fdbe38cfSAndrew Rybchenko
182fdbe38cfSAndrew Rybchenko #if EFSYS_OPT_HUNTINGTON
183fdbe38cfSAndrew Rybchenko case EFX_FAMILY_HUNTINGTON:
184fdbe38cfSAndrew Rybchenko etop = &__efx_tunnel_dummy_ops;
185fdbe38cfSAndrew Rybchenko break;
186fdbe38cfSAndrew Rybchenko #endif /* EFSYS_OPT_HUNTINGTON */
187fdbe38cfSAndrew Rybchenko
188fdbe38cfSAndrew Rybchenko #if EFSYS_OPT_MEDFORD
189fdbe38cfSAndrew Rybchenko case EFX_FAMILY_MEDFORD:
190edaff290SAndrew Rybchenko etop = &__efx_tunnel_ef10_ops;
191fdbe38cfSAndrew Rybchenko break;
192fdbe38cfSAndrew Rybchenko #endif /* EFSYS_OPT_MEDFORD */
193fdbe38cfSAndrew Rybchenko
194edaff290SAndrew Rybchenko #if EFSYS_OPT_MEDFORD2
195edaff290SAndrew Rybchenko case EFX_FAMILY_MEDFORD2:
196edaff290SAndrew Rybchenko etop = &__efx_tunnel_ef10_ops;
197edaff290SAndrew Rybchenko break;
198edaff290SAndrew Rybchenko #endif /* EFSYS_OPT_MEDFORD2 */
199edaff290SAndrew Rybchenko
200fdbe38cfSAndrew Rybchenko default:
201fdbe38cfSAndrew Rybchenko EFSYS_ASSERT(0);
202fdbe38cfSAndrew Rybchenko rc = ENOTSUP;
203fdbe38cfSAndrew Rybchenko goto fail1;
204fdbe38cfSAndrew Rybchenko }
205fdbe38cfSAndrew Rybchenko
206fdbe38cfSAndrew Rybchenko memset(etcp->etc_udp_entries, 0, sizeof (etcp->etc_udp_entries));
207fdbe38cfSAndrew Rybchenko etcp->etc_udp_entries_num = 0;
208fdbe38cfSAndrew Rybchenko
209fdbe38cfSAndrew Rybchenko enp->en_etop = etop;
210fdbe38cfSAndrew Rybchenko enp->en_mod_flags |= EFX_MOD_TUNNEL;
211fdbe38cfSAndrew Rybchenko
212fdbe38cfSAndrew Rybchenko return (0);
213fdbe38cfSAndrew Rybchenko
214fdbe38cfSAndrew Rybchenko fail1:
215fdbe38cfSAndrew Rybchenko EFSYS_PROBE1(fail1, efx_rc_t, rc);
216fdbe38cfSAndrew Rybchenko
217fdbe38cfSAndrew Rybchenko enp->en_etop = NULL;
218fdbe38cfSAndrew Rybchenko enp->en_mod_flags &= ~EFX_MOD_TUNNEL;
219fdbe38cfSAndrew Rybchenko
220fdbe38cfSAndrew Rybchenko return (rc);
221fdbe38cfSAndrew Rybchenko }
222fdbe38cfSAndrew Rybchenko
223fdbe38cfSAndrew Rybchenko void
efx_tunnel_fini(__in efx_nic_t * enp)224fdbe38cfSAndrew Rybchenko efx_tunnel_fini(
225fdbe38cfSAndrew Rybchenko __in efx_nic_t *enp)
226fdbe38cfSAndrew Rybchenko {
227fdbe38cfSAndrew Rybchenko boolean_t resetting;
228fdbe38cfSAndrew Rybchenko
229fdbe38cfSAndrew Rybchenko EFSYS_ASSERT3U(enp->en_magic, ==, EFX_NIC_MAGIC);
230fdbe38cfSAndrew Rybchenko EFSYS_ASSERT3U(enp->en_mod_flags, &, EFX_MOD_PROBE);
231fdbe38cfSAndrew Rybchenko EFSYS_ASSERT3U(enp->en_mod_flags, &, EFX_MOD_TUNNEL);
232fdbe38cfSAndrew Rybchenko
233fdbe38cfSAndrew Rybchenko if ((enp->en_etop->eto_udp_encap_supported != NULL) &&
234fdbe38cfSAndrew Rybchenko enp->en_etop->eto_udp_encap_supported(enp)) {
235fdbe38cfSAndrew Rybchenko /*
236fdbe38cfSAndrew Rybchenko * The UNLOADING flag allows the MC to suppress the datapath
237fdbe38cfSAndrew Rybchenko * reset if it was set on the last call to
238fdbe38cfSAndrew Rybchenko * MC_CMD_SET_TUNNEL_ENCAP_UDP_PORTS by all functions
239fdbe38cfSAndrew Rybchenko */
240fdbe38cfSAndrew Rybchenko (void) efx_mcdi_set_tunnel_encap_udp_ports(enp, NULL, B_TRUE,
241fdbe38cfSAndrew Rybchenko &resetting);
242fdbe38cfSAndrew Rybchenko }
243fdbe38cfSAndrew Rybchenko
244fdbe38cfSAndrew Rybchenko enp->en_etop = NULL;
245fdbe38cfSAndrew Rybchenko enp->en_mod_flags &= ~EFX_MOD_TUNNEL;
246fdbe38cfSAndrew Rybchenko }
247fdbe38cfSAndrew Rybchenko
248fdbe38cfSAndrew Rybchenko static __checkReturn efx_rc_t
efx_tunnel_config_find_udp_tunnel_entry(__in efx_tunnel_cfg_t * etcp,__in uint16_t port,__out unsigned int * entryp)249fdbe38cfSAndrew Rybchenko efx_tunnel_config_find_udp_tunnel_entry(
250fdbe38cfSAndrew Rybchenko __in efx_tunnel_cfg_t *etcp,
251fdbe38cfSAndrew Rybchenko __in uint16_t port,
252fdbe38cfSAndrew Rybchenko __out unsigned int *entryp)
253fdbe38cfSAndrew Rybchenko {
254fdbe38cfSAndrew Rybchenko unsigned int i;
255fdbe38cfSAndrew Rybchenko
256fdbe38cfSAndrew Rybchenko for (i = 0; i < etcp->etc_udp_entries_num; ++i) {
257fdbe38cfSAndrew Rybchenko efx_tunnel_udp_entry_t *p = &etcp->etc_udp_entries[i];
258fdbe38cfSAndrew Rybchenko
259fdbe38cfSAndrew Rybchenko if (p->etue_port == port) {
260fdbe38cfSAndrew Rybchenko *entryp = i;
261fdbe38cfSAndrew Rybchenko return (0);
262fdbe38cfSAndrew Rybchenko }
263fdbe38cfSAndrew Rybchenko }
264fdbe38cfSAndrew Rybchenko
265fdbe38cfSAndrew Rybchenko return (ENOENT);
266fdbe38cfSAndrew Rybchenko }
267fdbe38cfSAndrew Rybchenko
268fdbe38cfSAndrew Rybchenko __checkReturn efx_rc_t
efx_tunnel_config_udp_add(__in efx_nic_t * enp,__in uint16_t port,__in efx_tunnel_protocol_t protocol)269fdbe38cfSAndrew Rybchenko efx_tunnel_config_udp_add(
270fdbe38cfSAndrew Rybchenko __in efx_nic_t *enp,
271fdbe38cfSAndrew Rybchenko __in uint16_t port /* host/cpu-endian */,
272fdbe38cfSAndrew Rybchenko __in efx_tunnel_protocol_t protocol)
273fdbe38cfSAndrew Rybchenko {
274fdbe38cfSAndrew Rybchenko const efx_nic_cfg_t *encp = &enp->en_nic_cfg;
275fdbe38cfSAndrew Rybchenko efx_tunnel_cfg_t *etcp = &enp->en_tunnel_cfg;
276fdbe38cfSAndrew Rybchenko efsys_lock_state_t state;
277fdbe38cfSAndrew Rybchenko efx_rc_t rc;
278fdbe38cfSAndrew Rybchenko unsigned int entry;
279fdbe38cfSAndrew Rybchenko
280fdbe38cfSAndrew Rybchenko EFSYS_ASSERT3U(enp->en_mod_flags, &, EFX_MOD_TUNNEL);
281fdbe38cfSAndrew Rybchenko
282fdbe38cfSAndrew Rybchenko if (protocol >= EFX_TUNNEL_NPROTOS) {
283fdbe38cfSAndrew Rybchenko rc = EINVAL;
284fdbe38cfSAndrew Rybchenko goto fail1;
285fdbe38cfSAndrew Rybchenko }
286fdbe38cfSAndrew Rybchenko
287fdbe38cfSAndrew Rybchenko if ((encp->enc_tunnel_encapsulations_supported &
288fdbe38cfSAndrew Rybchenko (1u << protocol)) == 0) {
289fdbe38cfSAndrew Rybchenko rc = ENOTSUP;
290fdbe38cfSAndrew Rybchenko goto fail2;
291fdbe38cfSAndrew Rybchenko }
292fdbe38cfSAndrew Rybchenko
293fdbe38cfSAndrew Rybchenko EFSYS_LOCK(enp->en_eslp, state);
294fdbe38cfSAndrew Rybchenko
295fdbe38cfSAndrew Rybchenko rc = efx_tunnel_config_find_udp_tunnel_entry(etcp, port, &entry);
296fdbe38cfSAndrew Rybchenko if (rc == 0) {
297fdbe38cfSAndrew Rybchenko rc = EEXIST;
298fdbe38cfSAndrew Rybchenko goto fail3;
299fdbe38cfSAndrew Rybchenko }
300fdbe38cfSAndrew Rybchenko
301fdbe38cfSAndrew Rybchenko if (etcp->etc_udp_entries_num ==
302fdbe38cfSAndrew Rybchenko encp->enc_tunnel_config_udp_entries_max) {
303fdbe38cfSAndrew Rybchenko rc = ENOSPC;
304fdbe38cfSAndrew Rybchenko goto fail4;
305fdbe38cfSAndrew Rybchenko }
306fdbe38cfSAndrew Rybchenko
307fdbe38cfSAndrew Rybchenko etcp->etc_udp_entries[etcp->etc_udp_entries_num].etue_port = port;
308fdbe38cfSAndrew Rybchenko etcp->etc_udp_entries[etcp->etc_udp_entries_num].etue_protocol =
309fdbe38cfSAndrew Rybchenko protocol;
310fdbe38cfSAndrew Rybchenko
311fdbe38cfSAndrew Rybchenko etcp->etc_udp_entries_num++;
312fdbe38cfSAndrew Rybchenko
313fdbe38cfSAndrew Rybchenko EFSYS_UNLOCK(enp->en_eslp, state);
314fdbe38cfSAndrew Rybchenko
315fdbe38cfSAndrew Rybchenko return (0);
316fdbe38cfSAndrew Rybchenko
317fdbe38cfSAndrew Rybchenko fail4:
318fdbe38cfSAndrew Rybchenko EFSYS_PROBE(fail4);
319fdbe38cfSAndrew Rybchenko
320fdbe38cfSAndrew Rybchenko fail3:
321fdbe38cfSAndrew Rybchenko EFSYS_PROBE(fail3);
322fdbe38cfSAndrew Rybchenko EFSYS_UNLOCK(enp->en_eslp, state);
323fdbe38cfSAndrew Rybchenko
324fdbe38cfSAndrew Rybchenko fail2:
325fdbe38cfSAndrew Rybchenko EFSYS_PROBE(fail2);
326fdbe38cfSAndrew Rybchenko
327fdbe38cfSAndrew Rybchenko fail1:
328fdbe38cfSAndrew Rybchenko EFSYS_PROBE1(fail1, efx_rc_t, rc);
329fdbe38cfSAndrew Rybchenko
330fdbe38cfSAndrew Rybchenko return (rc);
331fdbe38cfSAndrew Rybchenko }
332fdbe38cfSAndrew Rybchenko
333fdbe38cfSAndrew Rybchenko __checkReturn efx_rc_t
efx_tunnel_config_udp_remove(__in efx_nic_t * enp,__in uint16_t port,__in efx_tunnel_protocol_t protocol)334fdbe38cfSAndrew Rybchenko efx_tunnel_config_udp_remove(
335fdbe38cfSAndrew Rybchenko __in efx_nic_t *enp,
336fdbe38cfSAndrew Rybchenko __in uint16_t port /* host/cpu-endian */,
337fdbe38cfSAndrew Rybchenko __in efx_tunnel_protocol_t protocol)
338fdbe38cfSAndrew Rybchenko {
339fdbe38cfSAndrew Rybchenko efx_tunnel_cfg_t *etcp = &enp->en_tunnel_cfg;
340fdbe38cfSAndrew Rybchenko efsys_lock_state_t state;
341fdbe38cfSAndrew Rybchenko unsigned int entry;
342fdbe38cfSAndrew Rybchenko efx_rc_t rc;
343fdbe38cfSAndrew Rybchenko
344fdbe38cfSAndrew Rybchenko EFSYS_ASSERT3U(enp->en_mod_flags, &, EFX_MOD_TUNNEL);
345fdbe38cfSAndrew Rybchenko
346fdbe38cfSAndrew Rybchenko EFSYS_LOCK(enp->en_eslp, state);
347fdbe38cfSAndrew Rybchenko
348fdbe38cfSAndrew Rybchenko rc = efx_tunnel_config_find_udp_tunnel_entry(etcp, port, &entry);
349fdbe38cfSAndrew Rybchenko if (rc != 0)
350fdbe38cfSAndrew Rybchenko goto fail1;
351fdbe38cfSAndrew Rybchenko
352fdbe38cfSAndrew Rybchenko if (etcp->etc_udp_entries[entry].etue_protocol != protocol) {
353fdbe38cfSAndrew Rybchenko rc = EINVAL;
354fdbe38cfSAndrew Rybchenko goto fail2;
355fdbe38cfSAndrew Rybchenko }
356fdbe38cfSAndrew Rybchenko
357fdbe38cfSAndrew Rybchenko EFSYS_ASSERT3U(etcp->etc_udp_entries_num, >, 0);
358fdbe38cfSAndrew Rybchenko etcp->etc_udp_entries_num--;
359fdbe38cfSAndrew Rybchenko
360fdbe38cfSAndrew Rybchenko if (entry < etcp->etc_udp_entries_num) {
361fdbe38cfSAndrew Rybchenko memmove(&etcp->etc_udp_entries[entry],
362fdbe38cfSAndrew Rybchenko &etcp->etc_udp_entries[entry + 1],
363fdbe38cfSAndrew Rybchenko (etcp->etc_udp_entries_num - entry) *
364fdbe38cfSAndrew Rybchenko sizeof (etcp->etc_udp_entries[0]));
365fdbe38cfSAndrew Rybchenko }
366fdbe38cfSAndrew Rybchenko
367fdbe38cfSAndrew Rybchenko memset(&etcp->etc_udp_entries[etcp->etc_udp_entries_num], 0,
368fdbe38cfSAndrew Rybchenko sizeof (etcp->etc_udp_entries[0]));
369fdbe38cfSAndrew Rybchenko
370fdbe38cfSAndrew Rybchenko EFSYS_UNLOCK(enp->en_eslp, state);
371fdbe38cfSAndrew Rybchenko
372fdbe38cfSAndrew Rybchenko return (0);
373fdbe38cfSAndrew Rybchenko
374fdbe38cfSAndrew Rybchenko fail2:
375fdbe38cfSAndrew Rybchenko EFSYS_PROBE(fail2);
376fdbe38cfSAndrew Rybchenko
377fdbe38cfSAndrew Rybchenko fail1:
378fdbe38cfSAndrew Rybchenko EFSYS_PROBE1(fail1, efx_rc_t, rc);
379fdbe38cfSAndrew Rybchenko EFSYS_UNLOCK(enp->en_eslp, state);
380fdbe38cfSAndrew Rybchenko
381fdbe38cfSAndrew Rybchenko return (rc);
382fdbe38cfSAndrew Rybchenko }
383fdbe38cfSAndrew Rybchenko
384fdbe38cfSAndrew Rybchenko void
efx_tunnel_config_clear(__in efx_nic_t * enp)385fdbe38cfSAndrew Rybchenko efx_tunnel_config_clear(
386fdbe38cfSAndrew Rybchenko __in efx_nic_t *enp)
387fdbe38cfSAndrew Rybchenko {
388fdbe38cfSAndrew Rybchenko efx_tunnel_cfg_t *etcp = &enp->en_tunnel_cfg;
389fdbe38cfSAndrew Rybchenko efsys_lock_state_t state;
390fdbe38cfSAndrew Rybchenko
391fdbe38cfSAndrew Rybchenko EFSYS_ASSERT3U(enp->en_mod_flags, &, EFX_MOD_TUNNEL);
392fdbe38cfSAndrew Rybchenko
393fdbe38cfSAndrew Rybchenko EFSYS_LOCK(enp->en_eslp, state);
394fdbe38cfSAndrew Rybchenko
395fdbe38cfSAndrew Rybchenko etcp->etc_udp_entries_num = 0;
396fdbe38cfSAndrew Rybchenko memset(etcp->etc_udp_entries, 0, sizeof (etcp->etc_udp_entries));
397fdbe38cfSAndrew Rybchenko
398fdbe38cfSAndrew Rybchenko EFSYS_UNLOCK(enp->en_eslp, state);
399fdbe38cfSAndrew Rybchenko }
400fdbe38cfSAndrew Rybchenko
401fdbe38cfSAndrew Rybchenko __checkReturn efx_rc_t
efx_tunnel_reconfigure(__in efx_nic_t * enp)402fdbe38cfSAndrew Rybchenko efx_tunnel_reconfigure(
403fdbe38cfSAndrew Rybchenko __in efx_nic_t *enp)
404fdbe38cfSAndrew Rybchenko {
405fdbe38cfSAndrew Rybchenko const efx_tunnel_ops_t *etop = enp->en_etop;
406fdbe38cfSAndrew Rybchenko efx_rc_t rc;
407fdbe38cfSAndrew Rybchenko
408fdbe38cfSAndrew Rybchenko EFSYS_ASSERT3U(enp->en_mod_flags, &, EFX_MOD_TUNNEL);
409fdbe38cfSAndrew Rybchenko
410fdbe38cfSAndrew Rybchenko if (etop->eto_reconfigure == NULL) {
411fdbe38cfSAndrew Rybchenko rc = ENOTSUP;
412fdbe38cfSAndrew Rybchenko goto fail1;
413fdbe38cfSAndrew Rybchenko }
414fdbe38cfSAndrew Rybchenko
415fdbe38cfSAndrew Rybchenko if ((rc = enp->en_etop->eto_reconfigure(enp)) != 0)
416fdbe38cfSAndrew Rybchenko goto fail2;
417fdbe38cfSAndrew Rybchenko
418fdbe38cfSAndrew Rybchenko return (0);
419fdbe38cfSAndrew Rybchenko
420fdbe38cfSAndrew Rybchenko fail2:
421fdbe38cfSAndrew Rybchenko EFSYS_PROBE(fail2);
422fdbe38cfSAndrew Rybchenko
423fdbe38cfSAndrew Rybchenko fail1:
424fdbe38cfSAndrew Rybchenko EFSYS_PROBE1(fail1, efx_rc_t, rc);
425fdbe38cfSAndrew Rybchenko
426fdbe38cfSAndrew Rybchenko return (rc);
427fdbe38cfSAndrew Rybchenko }
428fdbe38cfSAndrew Rybchenko
429edaff290SAndrew Rybchenko #if EFSYS_OPT_MEDFORD || EFSYS_OPT_MEDFORD2
430fdbe38cfSAndrew Rybchenko static __checkReturn boolean_t
ef10_udp_encap_supported(__in efx_nic_t * enp)431edaff290SAndrew Rybchenko ef10_udp_encap_supported(
432fdbe38cfSAndrew Rybchenko __in efx_nic_t *enp)
433fdbe38cfSAndrew Rybchenko {
434fdbe38cfSAndrew Rybchenko const efx_nic_cfg_t *encp = &enp->en_nic_cfg;
435fdbe38cfSAndrew Rybchenko uint32_t udp_tunnels_mask = 0;
436fdbe38cfSAndrew Rybchenko
437fdbe38cfSAndrew Rybchenko udp_tunnels_mask |= (1u << EFX_TUNNEL_PROTOCOL_VXLAN);
438fdbe38cfSAndrew Rybchenko udp_tunnels_mask |= (1u << EFX_TUNNEL_PROTOCOL_GENEVE);
439fdbe38cfSAndrew Rybchenko
440fdbe38cfSAndrew Rybchenko return ((encp->enc_tunnel_encapsulations_supported &
441fdbe38cfSAndrew Rybchenko udp_tunnels_mask) == 0 ? B_FALSE : B_TRUE);
442fdbe38cfSAndrew Rybchenko }
443fdbe38cfSAndrew Rybchenko
444fdbe38cfSAndrew Rybchenko static __checkReturn efx_rc_t
ef10_tunnel_reconfigure(__in efx_nic_t * enp)445edaff290SAndrew Rybchenko ef10_tunnel_reconfigure(
446fdbe38cfSAndrew Rybchenko __in efx_nic_t *enp)
447fdbe38cfSAndrew Rybchenko {
448fdbe38cfSAndrew Rybchenko efx_tunnel_cfg_t *etcp = &enp->en_tunnel_cfg;
449fdbe38cfSAndrew Rybchenko efx_rc_t rc;
450fdbe38cfSAndrew Rybchenko boolean_t resetting;
451fdbe38cfSAndrew Rybchenko efsys_lock_state_t state;
452fdbe38cfSAndrew Rybchenko efx_tunnel_cfg_t etc;
453fdbe38cfSAndrew Rybchenko
454fdbe38cfSAndrew Rybchenko EFSYS_LOCK(enp->en_eslp, state);
455fdbe38cfSAndrew Rybchenko memcpy(&etc, etcp, sizeof (etc));
456fdbe38cfSAndrew Rybchenko EFSYS_UNLOCK(enp->en_eslp, state);
457fdbe38cfSAndrew Rybchenko
458edaff290SAndrew Rybchenko if (ef10_udp_encap_supported(enp) == B_FALSE) {
459fdbe38cfSAndrew Rybchenko /*
460fdbe38cfSAndrew Rybchenko * It is OK to apply empty UDP tunnel ports when UDP
461fdbe38cfSAndrew Rybchenko * tunnel encapsulations are not supported - just nothing
462fdbe38cfSAndrew Rybchenko * should be done.
463fdbe38cfSAndrew Rybchenko */
464fdbe38cfSAndrew Rybchenko if (etc.etc_udp_entries_num == 0)
465fdbe38cfSAndrew Rybchenko return (0);
466fdbe38cfSAndrew Rybchenko rc = ENOTSUP;
467fdbe38cfSAndrew Rybchenko goto fail1;
468fdbe38cfSAndrew Rybchenko } else {
469fdbe38cfSAndrew Rybchenko /*
470fdbe38cfSAndrew Rybchenko * All PCI functions can see a reset upon the
471fdbe38cfSAndrew Rybchenko * MCDI request completion
472fdbe38cfSAndrew Rybchenko */
473fdbe38cfSAndrew Rybchenko rc = efx_mcdi_set_tunnel_encap_udp_ports(enp, &etc, B_FALSE,
474fdbe38cfSAndrew Rybchenko &resetting);
475fdbe38cfSAndrew Rybchenko if (rc != 0)
476fdbe38cfSAndrew Rybchenko goto fail2;
477fdbe38cfSAndrew Rybchenko
478fdbe38cfSAndrew Rybchenko /*
479fdbe38cfSAndrew Rybchenko * Although the caller should be able to handle MC reboot,
480fdbe38cfSAndrew Rybchenko * it might come in handy to report the impending reboot
481fdbe38cfSAndrew Rybchenko * by returning EAGAIN
482fdbe38cfSAndrew Rybchenko */
483fdbe38cfSAndrew Rybchenko return ((resetting) ? EAGAIN : 0);
484fdbe38cfSAndrew Rybchenko }
485fdbe38cfSAndrew Rybchenko fail2:
486fdbe38cfSAndrew Rybchenko EFSYS_PROBE(fail2);
487fdbe38cfSAndrew Rybchenko
488fdbe38cfSAndrew Rybchenko fail1:
489fdbe38cfSAndrew Rybchenko EFSYS_PROBE1(fail1, efx_rc_t, rc);
490fdbe38cfSAndrew Rybchenko
491fdbe38cfSAndrew Rybchenko return (rc);
492fdbe38cfSAndrew Rybchenko }
493edaff290SAndrew Rybchenko #endif /* EFSYS_OPT_MEDFORD || EFSYS_OPT_MEDFORD2 */
494fdbe38cfSAndrew Rybchenko
495fdbe38cfSAndrew Rybchenko #endif /* EFSYS_OPT_TUNNEL */
496