1 /* SPDX-License-Identifier: BSD-3-Clause
3 * Copyright(c) 2019-2021 Xilinx, Inc.
4 * Copyright(c) 2007-2019 Solarflare Communications Inc.
11 * State diagram of the UDP tunnel table entries
12 * (efx_tunnel_udp_entry_state_t and busy flag):
15 * +--------| APPLIED |<-------+
18 * | efx_tunnel_reconfigure (end)
19 * efx_tunnel_config_udp_remove |
22 * +---------+ +------------+
25 * | efx_tunnel_reconfigure (begin)
26 * efx_tunnel_reconfigure (begin) |
29 * +--------------+ | ADDED |<---------+
30 * | BUSY REMOVED | +-----------+ |
31 * +--------------+ | |
32 * | efx_tunnel_config_udp_remove |
33 * efx_tunnel_reconfigure (end) | |
37 * +------->|| empty ||<-------+ |
39 * +---------+ efx_tunnel_config_udp_add
41 * +------------------------------+
43 * Note that there is no BUSY APPLIED state since removing an applied entry
44 * should not be blocked by ongoing reconfiguration in another thread -
45 * reconfiguration will remove only busy entries.
50 #if EFSYS_OPT_MEDFORD || EFSYS_OPT_MEDFORD2
51 static __checkReturn boolean_t
52 ef10_udp_encap_supported(
55 static __checkReturn efx_rc_t
56 ef10_tunnel_reconfigure(
62 #endif /* EFSYS_OPT_MEDFORD || EFSYS_OPT_MEDFORD2 */
64 #if EFSYS_OPT_SIENA || EFSYS_OPT_HUNTINGTON
65 static const efx_tunnel_ops_t __efx_tunnel_dummy_ops = {
66 NULL, /* eto_reconfigure */
69 #endif /* EFSYS_OPT_SIENA || EFSYS_OPT_HUNTINGTON */
71 #if EFSYS_OPT_MEDFORD || EFSYS_OPT_MEDFORD2
72 static const efx_tunnel_ops_t __efx_tunnel_ef10_ops = {
73 ef10_tunnel_reconfigure, /* eto_reconfigure */
74 ef10_tunnel_fini, /* eto_fini */
76 #endif /* EFSYS_OPT_MEDFORD || EFSYS_OPT_MEDFORD2 */
78 #if EFSYS_OPT_RIVERHEAD
79 static const efx_tunnel_ops_t __efx_tunnel_rhead_ops = {
80 rhead_tunnel_reconfigure, /* eto_reconfigure */
81 rhead_tunnel_fini, /* eto_fini */
83 #endif /* EFSYS_OPT_RIVERHEAD */
85 /* Indicates that an entry is to be set */
86 static __checkReturn boolean_t
88 __in efx_tunnel_udp_entry_t *entry)
90 switch (entry->etue_state) {
91 case EFX_TUNNEL_UDP_ENTRY_ADDED:
92 return (entry->etue_busy);
93 case EFX_TUNNEL_UDP_ENTRY_REMOVED:
94 return (!entry->etue_busy);
95 case EFX_TUNNEL_UDP_ENTRY_APPLIED:
103 static __checkReturn efx_rc_t
104 efx_mcdi_set_tunnel_encap_udp_ports(
106 __in efx_tunnel_cfg_t *etcp,
107 __in boolean_t unloading,
108 __out boolean_t *resetting)
111 EFX_MCDI_DECLARE_BUF(payload,
112 MC_CMD_SET_TUNNEL_ENCAP_UDP_PORTS_IN_LENMAX,
113 MC_CMD_SET_TUNNEL_ENCAP_UDP_PORTS_OUT_LEN);
117 unsigned int entries_num;
122 for (i = 0; i < etcp->etc_udp_entries_num; i++) {
123 if (ef10_entry_staged(&etcp->etc_udp_entries[i]) !=
130 req.emr_cmd = MC_CMD_SET_TUNNEL_ENCAP_UDP_PORTS;
131 req.emr_in_buf = payload;
133 MC_CMD_SET_TUNNEL_ENCAP_UDP_PORTS_IN_LEN(entries_num);
134 req.emr_out_buf = payload;
135 req.emr_out_length = MC_CMD_SET_TUNNEL_ENCAP_UDP_PORTS_OUT_LEN;
137 EFX_POPULATE_WORD_1(flags,
138 MC_CMD_SET_TUNNEL_ENCAP_UDP_PORTS_IN_UNLOADING,
139 (unloading == B_TRUE) ? 1 : 0);
140 MCDI_IN_SET_WORD(req, SET_TUNNEL_ENCAP_UDP_PORTS_IN_FLAGS,
141 EFX_WORD_FIELD(flags, EFX_WORD_0));
143 MCDI_IN_SET_WORD(req, SET_TUNNEL_ENCAP_UDP_PORTS_IN_NUM_ENTRIES,
146 for (i = 0, entry = 0; entry < entries_num; ++entry, ++i) {
147 uint16_t mcdi_udp_protocol;
149 while (ef10_entry_staged(&etcp->etc_udp_entries[i]) == B_FALSE)
152 switch (etcp->etc_udp_entries[i].etue_protocol) {
153 case EFX_TUNNEL_PROTOCOL_VXLAN:
154 mcdi_udp_protocol = TUNNEL_ENCAP_UDP_PORT_ENTRY_VXLAN;
156 case EFX_TUNNEL_PROTOCOL_GENEVE:
157 mcdi_udp_protocol = TUNNEL_ENCAP_UDP_PORT_ENTRY_GENEVE;
165 * UDP port is MCDI native little-endian in the request
166 * and EFX_POPULATE_DWORD cares about conversion from
167 * host/CPU byte order to little-endian.
169 EFX_STATIC_ASSERT(sizeof (efx_dword_t) ==
170 TUNNEL_ENCAP_UDP_PORT_ENTRY_LEN);
171 EFX_POPULATE_DWORD_2(
172 MCDI_IN2(req, efx_dword_t,
173 SET_TUNNEL_ENCAP_UDP_PORTS_IN_ENTRIES)[entry],
174 TUNNEL_ENCAP_UDP_PORT_ENTRY_UDP_PORT,
175 etcp->etc_udp_entries[i].etue_port,
176 TUNNEL_ENCAP_UDP_PORT_ENTRY_PROTOCOL,
180 efx_mcdi_execute(enp, &req);
182 if (req.emr_rc != 0) {
187 if (req.emr_out_length_used !=
188 MC_CMD_SET_TUNNEL_ENCAP_UDP_PORTS_OUT_LEN) {
193 *resetting = MCDI_OUT_WORD_FIELD(req,
194 SET_TUNNEL_ENCAP_UDP_PORTS_OUT_FLAGS,
195 SET_TUNNEL_ENCAP_UDP_PORTS_OUT_RESETTING);
206 EFSYS_PROBE1(fail1, efx_rc_t, rc);
211 __checkReturn efx_rc_t
215 efx_tunnel_cfg_t *etcp = &enp->en_tunnel_cfg;
216 const efx_tunnel_ops_t *etop;
219 EFSYS_ASSERT3U(enp->en_magic, ==, EFX_NIC_MAGIC);
220 EFSYS_ASSERT3U(enp->en_mod_flags, &, EFX_MOD_PROBE);
221 EFSYS_ASSERT(!(enp->en_mod_flags & EFX_MOD_TUNNEL));
223 EFX_STATIC_ASSERT(EFX_TUNNEL_MAXNENTRIES ==
224 MC_CMD_SET_TUNNEL_ENCAP_UDP_PORTS_IN_ENTRIES_MAXNUM);
226 switch (enp->en_family) {
228 case EFX_FAMILY_SIENA:
229 etop = &__efx_tunnel_dummy_ops;
231 #endif /* EFSYS_OPT_SIENA */
233 #if EFSYS_OPT_HUNTINGTON
234 case EFX_FAMILY_HUNTINGTON:
235 etop = &__efx_tunnel_dummy_ops;
237 #endif /* EFSYS_OPT_HUNTINGTON */
239 #if EFSYS_OPT_MEDFORD
240 case EFX_FAMILY_MEDFORD:
241 etop = &__efx_tunnel_ef10_ops;
243 #endif /* EFSYS_OPT_MEDFORD */
245 #if EFSYS_OPT_MEDFORD2
246 case EFX_FAMILY_MEDFORD2:
247 etop = &__efx_tunnel_ef10_ops;
249 #endif /* EFSYS_OPT_MEDFORD2 */
251 #if EFSYS_OPT_RIVERHEAD
252 case EFX_FAMILY_RIVERHEAD:
253 etop = &__efx_tunnel_rhead_ops;
255 #endif /* EFSYS_OPT_RIVERHEAD */
263 memset(etcp->etc_udp_entries, 0, sizeof (etcp->etc_udp_entries));
264 etcp->etc_udp_entries_num = 0;
267 enp->en_mod_flags |= EFX_MOD_TUNNEL;
272 EFSYS_PROBE1(fail1, efx_rc_t, rc);
275 enp->en_mod_flags &= ~EFX_MOD_TUNNEL;
284 EFSYS_ASSERT3U(enp->en_magic, ==, EFX_NIC_MAGIC);
285 EFSYS_ASSERT3U(enp->en_mod_flags, &, EFX_MOD_PROBE);
286 EFSYS_ASSERT3U(enp->en_mod_flags, &, EFX_MOD_TUNNEL);
288 if (enp->en_etop->eto_fini != NULL)
289 enp->en_etop->eto_fini(enp);
292 enp->en_mod_flags &= ~EFX_MOD_TUNNEL;
295 static __checkReturn efx_rc_t
296 efx_tunnel_config_find_udp_tunnel_entry(
297 __in efx_tunnel_cfg_t *etcp,
299 __out unsigned int *entryp)
303 for (i = 0; i < etcp->etc_udp_entries_num; ++i) {
304 efx_tunnel_udp_entry_t *p = &etcp->etc_udp_entries[i];
306 if (p->etue_port == port &&
307 p->etue_state != EFX_TUNNEL_UDP_ENTRY_REMOVED) {
316 __checkReturn efx_rc_t
317 efx_tunnel_config_udp_add(
319 __in uint16_t port /* host/cpu-endian */,
320 __in efx_tunnel_protocol_t protocol)
322 const efx_nic_cfg_t *encp = &enp->en_nic_cfg;
323 efx_tunnel_cfg_t *etcp = &enp->en_tunnel_cfg;
324 efsys_lock_state_t state;
328 EFSYS_ASSERT3U(enp->en_mod_flags, &, EFX_MOD_TUNNEL);
330 if (protocol >= EFX_TUNNEL_NPROTOS) {
335 if ((encp->enc_tunnel_encapsulations_supported &
336 (1u << protocol)) == 0) {
341 EFSYS_LOCK(enp->en_eslp, state);
343 rc = efx_tunnel_config_find_udp_tunnel_entry(etcp, port, &entry);
349 if (etcp->etc_udp_entries_num ==
350 encp->enc_tunnel_config_udp_entries_max) {
355 etcp->etc_udp_entries[etcp->etc_udp_entries_num].etue_port = port;
356 etcp->etc_udp_entries[etcp->etc_udp_entries_num].etue_protocol =
358 etcp->etc_udp_entries[etcp->etc_udp_entries_num].etue_state =
359 EFX_TUNNEL_UDP_ENTRY_ADDED;
361 etcp->etc_udp_entries_num++;
363 EFSYS_UNLOCK(enp->en_eslp, state);
372 EFSYS_UNLOCK(enp->en_eslp, state);
378 EFSYS_PROBE1(fail1, efx_rc_t, rc);
384 * Returns the index of the entry after the deleted one,
385 * or one past the last entry.
388 efx_tunnel_config_udp_do_remove(
389 __in efx_tunnel_cfg_t *etcp,
390 __in unsigned int entry)
392 EFSYS_ASSERT3U(etcp->etc_udp_entries_num, >, 0);
393 etcp->etc_udp_entries_num--;
395 if (entry < etcp->etc_udp_entries_num) {
396 memmove(&etcp->etc_udp_entries[entry],
397 &etcp->etc_udp_entries[entry + 1],
398 (etcp->etc_udp_entries_num - entry) *
399 sizeof (etcp->etc_udp_entries[0]));
402 memset(&etcp->etc_udp_entries[etcp->etc_udp_entries_num], 0,
403 sizeof (etcp->etc_udp_entries[0]));
409 * Returns the index of the entry after the specified one,
410 * or one past the last entry. The index is correct whether
411 * the specified entry was removed or not.
414 efx_tunnel_config_udp_remove_prepare(
415 __in efx_tunnel_cfg_t *etcp,
416 __in unsigned int entry)
418 unsigned int next = entry + 1;
420 switch (etcp->etc_udp_entries[entry].etue_state) {
421 case EFX_TUNNEL_UDP_ENTRY_ADDED:
422 next = efx_tunnel_config_udp_do_remove(etcp, entry);
424 case EFX_TUNNEL_UDP_ENTRY_REMOVED:
426 case EFX_TUNNEL_UDP_ENTRY_APPLIED:
427 etcp->etc_udp_entries[entry].etue_state =
428 EFX_TUNNEL_UDP_ENTRY_REMOVED;
438 __checkReturn efx_rc_t
439 efx_tunnel_config_udp_remove(
441 __in uint16_t port /* host/cpu-endian */,
442 __in efx_tunnel_protocol_t protocol)
444 efx_tunnel_cfg_t *etcp = &enp->en_tunnel_cfg;
445 efsys_lock_state_t state;
449 EFSYS_ASSERT3U(enp->en_mod_flags, &, EFX_MOD_TUNNEL);
451 EFSYS_LOCK(enp->en_eslp, state);
453 rc = efx_tunnel_config_find_udp_tunnel_entry(etcp, port, &entry);
457 if (etcp->etc_udp_entries[entry].etue_busy != B_FALSE) {
462 if (etcp->etc_udp_entries[entry].etue_protocol != protocol) {
467 (void) efx_tunnel_config_udp_remove_prepare(etcp, entry);
469 EFSYS_UNLOCK(enp->en_eslp, state);
480 EFSYS_PROBE1(fail1, efx_rc_t, rc);
481 EFSYS_UNLOCK(enp->en_eslp, state);
487 efx_tunnel_table_all_available(
488 __in efx_tunnel_cfg_t *etcp)
492 for (i = 0; i < etcp->etc_udp_entries_num; i++) {
493 if (etcp->etc_udp_entries[i].etue_busy != B_FALSE)
500 __checkReturn efx_rc_t
501 efx_tunnel_config_clear(
504 efx_tunnel_cfg_t *etcp = &enp->en_tunnel_cfg;
505 efsys_lock_state_t state;
509 EFSYS_ASSERT3U(enp->en_mod_flags, &, EFX_MOD_TUNNEL);
511 EFSYS_LOCK(enp->en_eslp, state);
513 if (efx_tunnel_table_all_available(etcp) == B_FALSE) {
519 while (i < etcp->etc_udp_entries_num)
520 i = efx_tunnel_config_udp_remove_prepare(etcp, i);
522 EFSYS_UNLOCK(enp->en_eslp, state);
527 EFSYS_PROBE1(fail1, efx_rc_t, rc);
528 EFSYS_UNLOCK(enp->en_eslp, state);
533 __checkReturn efx_rc_t
534 efx_tunnel_reconfigure(
537 const efx_tunnel_ops_t *etop = enp->en_etop;
538 efx_tunnel_cfg_t *etcp = &enp->en_tunnel_cfg;
539 efx_tunnel_udp_entry_t *entry;
540 boolean_t locked = B_FALSE;
541 efsys_lock_state_t state;
546 EFSYS_ASSERT3U(enp->en_mod_flags, &, EFX_MOD_TUNNEL);
548 if (etop->eto_reconfigure == NULL) {
553 EFSYS_LOCK(enp->en_eslp, state);
556 if (efx_tunnel_table_all_available(etcp) == B_FALSE) {
561 for (i = 0; i < etcp->etc_udp_entries_num; i++) {
562 entry = &etcp->etc_udp_entries[i];
563 if (entry->etue_state != EFX_TUNNEL_UDP_ENTRY_APPLIED)
564 entry->etue_busy = B_TRUE;
567 EFSYS_UNLOCK(enp->en_eslp, state);
570 rc = enp->en_etop->eto_reconfigure(enp);
571 if (rc != 0 && rc != EAGAIN)
574 resetting = (rc == EAGAIN) ? B_TRUE : B_FALSE;
576 EFSYS_LOCK(enp->en_eslp, state);
580 * Delete entries marked for removal since they are no longer
581 * needed after successful NIC-specific reconfiguration.
582 * Added entries become applied because they are installed in
587 while (i < etcp->etc_udp_entries_num) {
588 unsigned int next = i + 1;
590 entry = &etcp->etc_udp_entries[i];
591 if (entry->etue_busy != B_FALSE) {
592 entry->etue_busy = B_FALSE;
594 switch (entry->etue_state) {
595 case EFX_TUNNEL_UDP_ENTRY_APPLIED:
597 case EFX_TUNNEL_UDP_ENTRY_ADDED:
599 EFX_TUNNEL_UDP_ENTRY_APPLIED;
601 case EFX_TUNNEL_UDP_ENTRY_REMOVED:
602 next = efx_tunnel_config_udp_do_remove(etcp, i);
613 EFSYS_UNLOCK(enp->en_eslp, state);
616 return ((resetting == B_FALSE) ? 0 : EAGAIN);
621 EFSYS_ASSERT(locked == B_FALSE);
622 EFSYS_LOCK(enp->en_eslp, state);
624 for (i = 0; i < etcp->etc_udp_entries_num; i++)
625 etcp->etc_udp_entries[i].etue_busy = B_FALSE;
627 EFSYS_UNLOCK(enp->en_eslp, state);
633 EFSYS_PROBE1(fail1, efx_rc_t, rc);
635 EFSYS_UNLOCK(enp->en_eslp, state);
640 #if EFSYS_OPT_MEDFORD || EFSYS_OPT_MEDFORD2
641 static __checkReturn boolean_t
642 ef10_udp_encap_supported(
645 const efx_nic_cfg_t *encp = &enp->en_nic_cfg;
646 uint32_t udp_tunnels_mask = 0;
648 udp_tunnels_mask |= (1u << EFX_TUNNEL_PROTOCOL_VXLAN);
649 udp_tunnels_mask |= (1u << EFX_TUNNEL_PROTOCOL_GENEVE);
651 return ((encp->enc_tunnel_encapsulations_supported &
652 udp_tunnels_mask) == 0 ? B_FALSE : B_TRUE);
655 static __checkReturn efx_rc_t
656 ef10_tunnel_reconfigure(
659 efx_tunnel_cfg_t *etcp = &enp->en_tunnel_cfg;
661 boolean_t resetting = B_FALSE;
662 efsys_lock_state_t state;
663 efx_tunnel_cfg_t etc;
665 EFSYS_LOCK(enp->en_eslp, state);
666 memcpy(&etc, etcp, sizeof (etc));
667 EFSYS_UNLOCK(enp->en_eslp, state);
669 if (ef10_udp_encap_supported(enp) == B_FALSE) {
671 * It is OK to apply empty UDP tunnel ports when UDP
672 * tunnel encapsulations are not supported - just nothing
675 if (etc.etc_udp_entries_num == 0)
681 * All PCI functions can see a reset upon the
682 * MCDI request completion
684 rc = efx_mcdi_set_tunnel_encap_udp_ports(enp, &etc, B_FALSE,
688 * Do not fail if the access is denied when no
689 * tunnel encap UDP ports are configured.
691 if (rc != EACCES || etc.etc_udp_entries_num != 0)
696 * Although the caller should be able to handle MC reboot,
697 * it might come in handy to report the impending reboot
698 * by returning EAGAIN
700 return ((resetting) ? EAGAIN : 0);
706 EFSYS_PROBE1(fail1, efx_rc_t, rc);
717 if (ef10_udp_encap_supported(enp) != B_FALSE) {
719 * The UNLOADING flag allows the MC to suppress the datapath
720 * reset if it was set on the last call to
721 * MC_CMD_SET_TUNNEL_ENCAP_UDP_PORTS by all functions
723 (void) efx_mcdi_set_tunnel_encap_udp_ports(enp, NULL, B_TRUE,
727 #endif /* EFSYS_OPT_MEDFORD || EFSYS_OPT_MEDFORD2 */
729 #endif /* EFSYS_OPT_TUNNEL */