]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/sfxge/common/efx_tunnel.c
sfxge(4): add API to control UDP tunnel ports
[FreeBSD/FreeBSD.git] / sys / dev / sfxge / common / efx_tunnel.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2017-2018 Solarflare Communications Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are met:
9  *
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.
15  *
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.
27  *
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.
31  */
32
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
35
36 #include "efx.h"
37 #include "efx_impl.h"
38
39
40 #if EFSYS_OPT_TUNNEL
41
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 */
46 };
47 #endif /* EFSYS_OPT_SIENA || EFSYS_OPT_HUNTINGTON */
48
49 #if EFSYS_OPT_MEDFORD
50 static  __checkReturn   boolean_t
51 medford_udp_encap_supported(
52         __in            efx_nic_t *enp);
53
54 static  __checkReturn   efx_rc_t
55 medford_tunnel_reconfigure(
56         __in            efx_nic_t *enp);
57
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 */
61 };
62 #endif /* EFSYS_OPT_MEDFORD */
63
64 static  __checkReturn           efx_rc_t
65 efx_mcdi_set_tunnel_encap_udp_ports(
66         __in                    efx_nic_t *enp,
67         __in                    efx_tunnel_cfg_t *etcp,
68         __in                    boolean_t unloading,
69         __out                   boolean_t *resetting)
70 {
71         efx_mcdi_req_t req;
72         uint8_t payload[MAX(MC_CMD_SET_TUNNEL_ENCAP_UDP_PORTS_IN_LENMAX,
73                             MC_CMD_SET_TUNNEL_ENCAP_UDP_PORTS_OUT_LEN)];
74         efx_word_t flags;
75         efx_rc_t rc;
76         unsigned int i;
77         unsigned int entries_num;
78
79         if (etcp == NULL)
80                 entries_num = 0;
81         else
82                 entries_num = etcp->etc_udp_entries_num;
83
84         (void) memset(payload, 0, sizeof (payload));
85         req.emr_cmd = MC_CMD_SET_TUNNEL_ENCAP_UDP_PORTS;
86         req.emr_in_buf = payload;
87         req.emr_in_length =
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;
91
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));
97
98         MCDI_IN_SET_WORD(req, SET_TUNNEL_ENCAP_UDP_PORTS_IN_NUM_ENTRIES,
99             entries_num);
100
101         for (i = 0; i < entries_num; ++i) {
102                 uint16_t mcdi_udp_protocol;
103
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;
107                         break;
108                 case EFX_TUNNEL_PROTOCOL_GENEVE:
109                         mcdi_udp_protocol = TUNNEL_ENCAP_UDP_PORT_ENTRY_GENEVE;
110                         break;
111                 default:
112                         rc = EINVAL;
113                         goto fail1;
114                 }
115
116                 /*
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.
120                  */
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,
129                     mcdi_udp_protocol);
130         }
131
132         efx_mcdi_execute(enp, &req);
133
134         if (req.emr_rc != 0) {
135                 rc = req.emr_rc;
136                 goto fail2;
137         }
138
139         if (req.emr_out_length_used !=
140             MC_CMD_SET_TUNNEL_ENCAP_UDP_PORTS_OUT_LEN) {
141                 rc = EMSGSIZE;
142                 goto fail3;
143         }
144
145         *resetting = MCDI_OUT_WORD_FIELD(req,
146             SET_TUNNEL_ENCAP_UDP_PORTS_OUT_FLAGS,
147             SET_TUNNEL_ENCAP_UDP_PORTS_OUT_RESETTING);
148
149         return (0);
150
151 fail3:
152         EFSYS_PROBE(fail3);
153
154 fail2:
155         EFSYS_PROBE(fail2);
156
157 fail1:
158         EFSYS_PROBE1(fail1, efx_rc_t, rc);
159
160         return (rc);
161 }
162
163         __checkReturn   efx_rc_t
164 efx_tunnel_init(
165         __in            efx_nic_t *enp)
166 {
167         efx_tunnel_cfg_t *etcp = &enp->en_tunnel_cfg;
168         const efx_tunnel_ops_t *etop;
169         efx_rc_t rc;
170
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));
174
175         EFX_STATIC_ASSERT(EFX_TUNNEL_MAXNENTRIES ==
176             MC_CMD_SET_TUNNEL_ENCAP_UDP_PORTS_IN_ENTRIES_MAXNUM);
177
178         switch (enp->en_family) {
179 #if EFSYS_OPT_SIENA
180         case EFX_FAMILY_SIENA:
181                 etop = &__efx_tunnel_dummy_ops;
182                 break;
183 #endif /* EFSYS_OPT_SIENA */
184
185 #if EFSYS_OPT_HUNTINGTON
186         case EFX_FAMILY_HUNTINGTON:
187                 etop = &__efx_tunnel_dummy_ops;
188                 break;
189 #endif /* EFSYS_OPT_HUNTINGTON */
190
191 #if EFSYS_OPT_MEDFORD
192         case EFX_FAMILY_MEDFORD:
193                 etop = &__efx_tunnel_medford_ops;
194                 break;
195 #endif /* EFSYS_OPT_MEDFORD */
196
197         default:
198                 EFSYS_ASSERT(0);
199                 rc = ENOTSUP;
200                 goto fail1;
201         }
202
203         memset(etcp->etc_udp_entries, 0, sizeof (etcp->etc_udp_entries));
204         etcp->etc_udp_entries_num = 0;
205
206         enp->en_etop = etop;
207         enp->en_mod_flags |= EFX_MOD_TUNNEL;
208
209         return (0);
210
211 fail1:
212         EFSYS_PROBE1(fail1, efx_rc_t, rc);
213
214         enp->en_etop = NULL;
215         enp->en_mod_flags &= ~EFX_MOD_TUNNEL;
216
217         return (rc);
218 }
219
220                         void
221 efx_tunnel_fini(
222         __in            efx_nic_t *enp)
223 {
224         boolean_t resetting;
225
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);
229
230         if ((enp->en_etop->eto_udp_encap_supported != NULL) &&
231             enp->en_etop->eto_udp_encap_supported(enp)) {
232                 /*
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
236                  */
237                 (void) efx_mcdi_set_tunnel_encap_udp_ports(enp, NULL, B_TRUE,
238                     &resetting);
239         }
240
241         enp->en_etop = NULL;
242         enp->en_mod_flags &= ~EFX_MOD_TUNNEL;
243 }
244
245 static  __checkReturn   efx_rc_t
246 efx_tunnel_config_find_udp_tunnel_entry(
247         __in            efx_tunnel_cfg_t *etcp,
248         __in            uint16_t port,
249         __out           unsigned int *entryp)
250 {
251         unsigned int i;
252
253         for (i = 0; i < etcp->etc_udp_entries_num; ++i) {
254                 efx_tunnel_udp_entry_t *p = &etcp->etc_udp_entries[i];
255
256                 if (p->etue_port == port) {
257                         *entryp = i;
258                         return (0);
259                 }
260         }
261
262         return (ENOENT);
263 }
264
265         __checkReturn   efx_rc_t
266 efx_tunnel_config_udp_add(
267         __in            efx_nic_t *enp,
268         __in            uint16_t port /* host/cpu-endian */,
269         __in            efx_tunnel_protocol_t protocol)
270 {
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;
274         efx_rc_t rc;
275         unsigned int entry;
276
277         EFSYS_ASSERT3U(enp->en_mod_flags, &, EFX_MOD_TUNNEL);
278
279         if (protocol >= EFX_TUNNEL_NPROTOS) {
280                 rc = EINVAL;
281                 goto fail1;
282         }
283
284         if ((encp->enc_tunnel_encapsulations_supported &
285             (1u << protocol)) == 0) {
286                 rc = ENOTSUP;
287                 goto fail2;
288         }
289
290         EFSYS_LOCK(enp->en_eslp, state);
291
292         rc = efx_tunnel_config_find_udp_tunnel_entry(etcp, port, &entry);
293         if (rc == 0) {
294                 rc = EEXIST;
295                 goto fail3;
296         }
297
298         if (etcp->etc_udp_entries_num ==
299             encp->enc_tunnel_config_udp_entries_max) {
300                 rc = ENOSPC;
301                 goto fail4;
302         }
303
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 =
306             protocol;
307
308         etcp->etc_udp_entries_num++;
309
310         EFSYS_UNLOCK(enp->en_eslp, state);
311
312         return (0);
313
314 fail4:
315         EFSYS_PROBE(fail4);
316
317 fail3:
318         EFSYS_PROBE(fail3);
319         EFSYS_UNLOCK(enp->en_eslp, state);
320
321 fail2:
322         EFSYS_PROBE(fail2);
323
324 fail1:
325         EFSYS_PROBE1(fail1, efx_rc_t, rc);
326
327         return (rc);
328 }
329
330         __checkReturn   efx_rc_t
331 efx_tunnel_config_udp_remove(
332         __in            efx_nic_t *enp,
333         __in            uint16_t port /* host/cpu-endian */,
334         __in            efx_tunnel_protocol_t protocol)
335 {
336         efx_tunnel_cfg_t *etcp = &enp->en_tunnel_cfg;
337         efsys_lock_state_t state;
338         unsigned int entry;
339         efx_rc_t rc;
340
341         EFSYS_ASSERT3U(enp->en_mod_flags, &, EFX_MOD_TUNNEL);
342
343         EFSYS_LOCK(enp->en_eslp, state);
344
345         rc = efx_tunnel_config_find_udp_tunnel_entry(etcp, port, &entry);
346         if (rc != 0)
347                 goto fail1;
348
349         if (etcp->etc_udp_entries[entry].etue_protocol != protocol) {
350                 rc = EINVAL;
351                 goto fail2;
352         }
353
354         EFSYS_ASSERT3U(etcp->etc_udp_entries_num, >, 0);
355         etcp->etc_udp_entries_num--;
356
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]));
362         }
363
364         memset(&etcp->etc_udp_entries[etcp->etc_udp_entries_num], 0,
365             sizeof (etcp->etc_udp_entries[0]));
366
367         EFSYS_UNLOCK(enp->en_eslp, state);
368
369         return (0);
370
371 fail2:
372         EFSYS_PROBE(fail2);
373
374 fail1:
375         EFSYS_PROBE1(fail1, efx_rc_t, rc);
376         EFSYS_UNLOCK(enp->en_eslp, state);
377
378         return (rc);
379 }
380
381                         void
382 efx_tunnel_config_clear(
383         __in                    efx_nic_t *enp)
384 {
385         efx_tunnel_cfg_t *etcp = &enp->en_tunnel_cfg;
386         efsys_lock_state_t state;
387
388         EFSYS_ASSERT3U(enp->en_mod_flags, &, EFX_MOD_TUNNEL);
389
390         EFSYS_LOCK(enp->en_eslp, state);
391
392         etcp->etc_udp_entries_num = 0;
393         memset(etcp->etc_udp_entries, 0, sizeof (etcp->etc_udp_entries));
394
395         EFSYS_UNLOCK(enp->en_eslp, state);
396 }
397
398         __checkReturn   efx_rc_t
399 efx_tunnel_reconfigure(
400         __in            efx_nic_t *enp)
401 {
402         const efx_tunnel_ops_t *etop = enp->en_etop;
403         efx_rc_t rc;
404
405         EFSYS_ASSERT3U(enp->en_mod_flags, &, EFX_MOD_TUNNEL);
406
407         if (etop->eto_reconfigure == NULL) {
408                 rc = ENOTSUP;
409                 goto fail1;
410         }
411
412         if ((rc = enp->en_etop->eto_reconfigure(enp)) != 0)
413                 goto fail2;
414
415         return (0);
416
417 fail2:
418         EFSYS_PROBE(fail2);
419
420 fail1:
421         EFSYS_PROBE1(fail1, efx_rc_t, rc);
422
423         return (rc);
424 }
425
426 #if EFSYS_OPT_MEDFORD
427 static  __checkReturn           boolean_t
428 medford_udp_encap_supported(
429         __in            efx_nic_t *enp)
430 {
431         const efx_nic_cfg_t *encp = &enp->en_nic_cfg;
432         uint32_t udp_tunnels_mask = 0;
433
434         udp_tunnels_mask |= (1u << EFX_TUNNEL_PROTOCOL_VXLAN);
435         udp_tunnels_mask |= (1u << EFX_TUNNEL_PROTOCOL_GENEVE);
436
437         return ((encp->enc_tunnel_encapsulations_supported &
438             udp_tunnels_mask) == 0 ? B_FALSE : B_TRUE);
439 }
440
441 static  __checkReturn   efx_rc_t
442 medford_tunnel_reconfigure(
443         __in            efx_nic_t *enp)
444 {
445         efx_tunnel_cfg_t *etcp = &enp->en_tunnel_cfg;
446         efx_rc_t rc;
447         boolean_t resetting;
448         efsys_lock_state_t state;
449         efx_tunnel_cfg_t etc;
450
451         EFSYS_LOCK(enp->en_eslp, state);
452         memcpy(&etc, etcp, sizeof (etc));
453         EFSYS_UNLOCK(enp->en_eslp, state);
454
455         if (medford_udp_encap_supported(enp) == B_FALSE) {
456                 /*
457                  * It is OK to apply empty UDP tunnel ports when UDP
458                  * tunnel encapsulations are not supported - just nothing
459                  * should be done.
460                  */
461                 if (etc.etc_udp_entries_num == 0)
462                         return (0);
463                 rc = ENOTSUP;
464                 goto fail1;
465         } else {
466                 /*
467                  * All PCI functions can see a reset upon the
468                  * MCDI request completion
469                  */
470                 rc = efx_mcdi_set_tunnel_encap_udp_ports(enp, &etc, B_FALSE,
471                     &resetting);
472                 if (rc != 0)
473                         goto fail2;
474
475                 /*
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
479                  */
480                 return ((resetting) ? EAGAIN : 0);
481         }
482 fail2:
483         EFSYS_PROBE(fail2);
484
485 fail1:
486         EFSYS_PROBE1(fail1, efx_rc_t, rc);
487
488         return (rc);
489 }
490 #endif /* EFSYS_OPT_MEDFORD */
491
492 #endif /* EFSYS_OPT_TUNNEL */