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