Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright(c) 2019-2023 Broadcom
3 : : * All rights reserved.
4 : : */
5 : :
6 : : #include <rte_common.h>
7 : : #include <rte_cycles.h>
8 : : #include <rte_malloc.h>
9 : : #include <rte_log.h>
10 : : #include <rte_alarm.h>
11 : : #include "bnxt.h"
12 : : #include "bnxt_ulp.h"
13 : : #include "bnxt_tf_common.h"
14 : : #include "ulp_ha_mgr.h"
15 : : #include "ulp_flow_db.h"
16 : :
17 : : /* Local only MACROs and defines that aren't exported */
18 : : #define ULP_HA_TIMER_THREAD (1 << 0)
19 : : #define ULP_HA_TIMER_IS_RUNNING(info) (!!((info)->flags & ULP_HA_TIMER_THREAD))
20 : : #define ULP_HA_TIMER_SEC 1
21 : : #define ULP_HA_WAIT_TIME (MS_PER_S / 10)
22 : : #define ULP_HA_WAIT_TIMEOUT (MS_PER_S * 2)
23 : :
24 : : #define ULP_HA_IF_TBL_DIR TF_DIR_RX
25 : : #define ULP_HA_IF_TBL_TYPE TF_IF_TBL_TYPE_PROF_PARIF_ERR_ACT_REC_PTR
26 : :
27 : : static void ulp_ha_mgr_timer_cancel(struct bnxt_ulp_context *ulp_ctx);
28 : : static int32_t ulp_ha_mgr_timer_start(void *arg);
29 : : static void ulp_ha_mgr_timer_cb(void *arg);
30 : : static int32_t ulp_ha_mgr_app_type_set(struct bnxt_ulp_context *ulp_ctx,
31 : : enum ulp_ha_mgr_app_type app_type);
32 : : static int32_t
33 : : ulp_ha_mgr_region_set(struct bnxt_ulp_context *ulp_ctx,
34 : : enum ulp_ha_mgr_region region);
35 : : static int32_t
36 : : ulp_ha_mgr_state_set(struct bnxt_ulp_context *ulp_ctx,
37 : : enum ulp_ha_mgr_state state);
38 : :
39 : : static int32_t
40 : : ulp_ha_mgr_tf_client_num_get(struct bnxt_ulp_context *ulp_ctx, uint32_t *cnt);
41 : :
42 : : static int32_t
43 : 0 : ulp_ha_mgr_state_set_v1(struct bnxt_ulp_context *ulp_ctx,
44 : : enum ulp_ha_mgr_state state)
45 : : {
46 : 0 : struct tf_set_if_tbl_entry_parms set_parms = { 0 };
47 : : struct tf *tfp;
48 : 0 : uint32_t val = 0;
49 : : int32_t rc = 0;
50 : :
51 [ # # ]: 0 : if (ulp_ctx == NULL) {
52 : 0 : BNXT_TF_DBG(ERR, "Invalid parms in state get.\n");
53 : 0 : return -EINVAL;
54 : : }
55 : 0 : tfp = bnxt_ulp_cntxt_tfp_get(ulp_ctx, BNXT_ULP_SESSION_TYPE_DEFAULT);
56 [ # # ]: 0 : if (tfp == NULL) {
57 : 0 : BNXT_TF_DBG(ERR, "Unable to get the TFP.\n");
58 : 0 : return -EINVAL;
59 : : }
60 : :
61 : 0 : val = (uint32_t)state;
62 : :
63 : 0 : set_parms.dir = ULP_HA_IF_TBL_DIR;
64 : 0 : set_parms.type = ULP_HA_IF_TBL_TYPE;
65 : 0 : set_parms.data = (uint8_t *)&val;
66 : 0 : set_parms.data_sz_in_bytes = sizeof(val);
67 : 0 : set_parms.idx = bnxt_ulp_ha_reg_state_get(ulp_ctx);
68 : :
69 : 0 : rc = tf_set_if_tbl_entry(tfp, &set_parms);
70 [ # # ]: 0 : if (rc)
71 : 0 : BNXT_TF_DBG(ERR, "Failed to write the HA state\n");
72 : :
73 : : return rc;
74 : : }
75 : :
76 : : static int32_t
77 : 0 : ulp_ha_mgr_state_set_v2(struct bnxt_ulp_context *ulp_ctx,
78 : : enum ulp_ha_mgr_state state)
79 : : {
80 : 0 : struct tf_set_session_hotup_state_parms parms = { 0 };
81 : : struct tf *tfp;
82 : : int32_t rc = 0;
83 : :
84 [ # # ]: 0 : if (ulp_ctx == NULL) {
85 : 0 : BNXT_TF_DBG(ERR, "Invalid parms in state get.\n");
86 : 0 : return -EINVAL;
87 : : }
88 : :
89 : 0 : tfp = bnxt_ulp_cntxt_tfp_get(ulp_ctx, BNXT_ULP_SESSION_TYPE_SHARED_WC);
90 [ # # ]: 0 : if (tfp == NULL) {
91 : 0 : BNXT_TF_DBG(ERR, "Unable to get the TFP.\n");
92 : 0 : return -EINVAL;
93 : : }
94 : :
95 : 0 : parms.state = (uint16_t)state;
96 : 0 : rc = tf_set_session_hotup_state(tfp, &parms);
97 [ # # ]: 0 : if (rc) {
98 : 0 : BNXT_TF_DBG(ERR, "Failed to write the HA state\n");
99 : 0 : return rc;
100 : : }
101 : :
102 : : return rc;
103 : : }
104 : :
105 : : static int32_t
106 : 0 : ulp_ha_mgr_state_set(struct bnxt_ulp_context *ulp_ctx,
107 : : enum ulp_ha_mgr_state state)
108 : : {
109 [ # # ]: 0 : if (bnxt_ulp_cntxt_multi_shared_session_enabled(ulp_ctx))
110 : 0 : return ulp_ha_mgr_state_set_v2(ulp_ctx, state);
111 : : else
112 : 0 : return ulp_ha_mgr_state_set_v1(ulp_ctx, state);
113 : : }
114 : :
115 : : static int32_t
116 : 0 : ulp_ha_mgr_tf_state_get(struct bnxt_ulp_context *ulp_ctx,
117 : : uint32_t *state,
118 : : uint32_t *cnt)
119 : : {
120 : 0 : struct tf_get_session_hotup_state_parms parms = { 0 };
121 : : struct tf *tfp;
122 : : int32_t rc = 0;
123 : :
124 [ # # ]: 0 : if (ulp_ctx == NULL) {
125 : 0 : BNXT_TF_DBG(ERR, "Invalid parms in client num get.\n");
126 : 0 : return -EINVAL;
127 : : }
128 : :
129 : 0 : tfp = bnxt_ulp_cntxt_tfp_get(ulp_ctx, BNXT_ULP_SESSION_TYPE_SHARED_WC);
130 [ # # ]: 0 : if (tfp == NULL) {
131 : 0 : BNXT_TF_DBG(ERR, "Unable to get the TFP.\n");
132 : 0 : return -EINVAL;
133 : : }
134 : :
135 : 0 : rc = tf_get_session_hotup_state(tfp, &parms);
136 [ # # ]: 0 : if (rc) {
137 : 0 : BNXT_TF_DBG(ERR, "Failed to read the HA state\n");
138 : 0 : return rc;
139 : : }
140 : :
141 [ # # ]: 0 : if (state)
142 : 0 : *state = parms.state;
143 : :
144 [ # # ]: 0 : if (cnt)
145 : 0 : *cnt = parms.ref_cnt;
146 : :
147 : : return rc;
148 : : }
149 : :
150 : : static int32_t
151 : 0 : ulp_ha_mgr_tf_client_num_get_v1(struct bnxt_ulp_context *ulp_ctx,
152 : : uint32_t *cnt)
153 : : {
154 : 0 : struct tf_get_if_tbl_entry_parms get_parms = { 0 };
155 : : struct tf *tfp;
156 : 0 : uint32_t val = 0;
157 : : int32_t rc = 0;
158 : :
159 [ # # ]: 0 : if (ulp_ctx == NULL || cnt == NULL) {
160 : 0 : BNXT_TF_DBG(ERR, "Invalid parms in client num get.\n");
161 : 0 : return -EINVAL;
162 : : }
163 : 0 : tfp = bnxt_ulp_cntxt_tfp_get(ulp_ctx, BNXT_ULP_SESSION_TYPE_DEFAULT);
164 [ # # ]: 0 : if (tfp == NULL) {
165 : 0 : BNXT_TF_DBG(ERR, "Unable to get the TFP.\n");
166 : 0 : return -EINVAL;
167 : : }
168 : :
169 : 0 : get_parms.dir = ULP_HA_IF_TBL_DIR;
170 : 0 : get_parms.type = ULP_HA_IF_TBL_TYPE;
171 : 0 : get_parms.idx = bnxt_ulp_ha_reg_cnt_get(ulp_ctx);
172 : 0 : get_parms.data = (uint8_t *)&val;
173 : 0 : get_parms.data_sz_in_bytes = sizeof(val);
174 : :
175 : 0 : rc = tf_get_if_tbl_entry(tfp, &get_parms);
176 [ # # ]: 0 : if (rc)
177 : 0 : BNXT_TF_DBG(ERR, "Failed to read the number of HA clients\n");
178 : :
179 : 0 : *cnt = val;
180 : 0 : return rc;
181 : : }
182 : :
183 : : static int32_t
184 : 0 : ulp_ha_mgr_tf_client_num_get(struct bnxt_ulp_context *ulp_ctx,
185 : : uint32_t *cnt)
186 : : {
187 [ # # ]: 0 : if (bnxt_ulp_cntxt_multi_shared_session_enabled(ulp_ctx))
188 : 0 : return ulp_ha_mgr_tf_state_get(ulp_ctx, NULL, cnt);
189 : : else
190 : 0 : return ulp_ha_mgr_tf_client_num_get_v1(ulp_ctx, cnt);
191 : : }
192 : :
193 : : static int32_t
194 : 0 : ulp_ha_mgr_region_set(struct bnxt_ulp_context *ulp_ctx,
195 : : enum ulp_ha_mgr_region region)
196 : : {
197 : : struct bnxt_ulp_ha_mgr_info *ha_info;
198 : :
199 [ # # ]: 0 : if (ulp_ctx == NULL) {
200 : 0 : BNXT_TF_DBG(ERR, "Invalid params in ha region get.\n");
201 : 0 : return -EINVAL;
202 : : }
203 : :
204 : 0 : ha_info = bnxt_ulp_cntxt_ptr2_ha_info_get(ulp_ctx);
205 [ # # ]: 0 : if (ha_info == NULL) {
206 : 0 : BNXT_TF_DBG(ERR, "Unable to get ha info\n");
207 : 0 : return -EINVAL;
208 : : }
209 : 0 : ha_info->region = region;
210 : :
211 : 0 : return 0;
212 : : }
213 : :
214 : : static int32_t
215 : 0 : ulp_ha_mgr_app_type_set(struct bnxt_ulp_context *ulp_ctx,
216 : : enum ulp_ha_mgr_app_type app_type)
217 : : {
218 : : struct bnxt_ulp_ha_mgr_info *ha_info;
219 : :
220 [ # # ]: 0 : if (ulp_ctx == NULL) {
221 : 0 : BNXT_TF_DBG(ERR, "Invalid Parms.\n");
222 : 0 : return -EINVAL;
223 : : }
224 : :
225 : 0 : ha_info = bnxt_ulp_cntxt_ptr2_ha_info_get(ulp_ctx);
226 [ # # ]: 0 : if (ha_info == NULL) {
227 : 0 : BNXT_TF_DBG(ERR, "Unable to get the ha info.\n");
228 : 0 : return -EINVAL;
229 : : }
230 : 0 : ha_info->app_type = app_type;
231 : :
232 : 0 : return 0;
233 : : }
234 : :
235 : : static void
236 : 0 : ulp_ha_mgr_timer_cb(void *arg)
237 : : {
238 : 0 : struct tf_move_tcam_shared_entries_parms mparms = { 0 };
239 : 0 : struct tf_clear_tcam_shared_entries_parms cparms = { 0 };
240 : : struct bnxt_ulp_context *ulp_ctx;
241 : : enum ulp_ha_mgr_state curr_state;
242 : : enum ulp_ha_mgr_app_type app_type;
243 : : uint8_t myclient_cnt = 0;
244 : 0 : uint32_t client_cnt = 0;
245 : : struct tf *tfp;
246 : : int32_t rc;
247 : :
248 : 0 : ulp_ctx = bnxt_ulp_cntxt_entry_acquire(arg);
249 [ # # ]: 0 : if (ulp_ctx == NULL) {
250 : : ulp_ha_mgr_timer_start(arg);
251 : 0 : return;
252 : : }
253 : :
254 : 0 : myclient_cnt = bnxt_ulp_cntxt_num_shared_clients_get(ulp_ctx);
255 [ # # ]: 0 : if (myclient_cnt == 0) {
256 : 0 : BNXT_TF_DBG(ERR,
257 : : "PANIC Client Count is zero kill timer\n.");
258 : 0 : return;
259 : : }
260 : :
261 : 0 : tfp = bnxt_ulp_cntxt_tfp_get(ulp_ctx, BNXT_ULP_SESSION_TYPE_SHARED_WC);
262 [ # # ]: 0 : if (tfp == NULL) {
263 : 0 : BNXT_TF_DBG(ERR, "Unable to get the TFP.\n");
264 : 0 : goto cb_restart;
265 : : }
266 : :
267 : 0 : rc = ulp_ha_mgr_state_get(ulp_ctx, &curr_state);
268 [ # # ]: 0 : if (rc) {
269 : : /*
270 : : * This shouldn't happen, if it does, reset the timer
271 : : * and try again next time.
272 : : */
273 : 0 : BNXT_TF_DBG(ERR, "Failed(%d) to get state.\n",
274 : : rc);
275 : 0 : goto cb_restart;
276 : : }
277 : :
278 : 0 : rc = ulp_ha_mgr_tf_client_num_get(ulp_ctx, &client_cnt);
279 [ # # ]: 0 : if (rc) {
280 : 0 : BNXT_TF_DBG(ERR, "Failed(%d) to get cnt.\n",
281 : : rc);
282 : 0 : goto cb_restart;
283 : : }
284 : :
285 : 0 : rc = ulp_ha_mgr_app_type_get(ulp_ctx, &app_type);
286 [ # # ]: 0 : if (rc) {
287 : 0 : BNXT_TF_DBG(ERR, "Failed(%d) to get type.\n",
288 : : rc);
289 : 0 : goto cb_restart;
290 : : }
291 : :
292 : : /* Handle the Cleanup if an app went away */
293 [ # # ]: 0 : if (client_cnt == myclient_cnt) {
294 [ # # ]: 0 : if (curr_state == ULP_HA_STATE_PRIM_SEC_RUN &&
295 [ # # ]: 0 : app_type == ULP_HA_APP_TYPE_PRIM) {
296 : : /*
297 : : * The SECONDARY went away:
298 : : * 1. Set the state to PRIM_RUN
299 : : * 2. Clear the High region so our TCAM will hit.
300 : : */
301 : 0 : rc = ulp_ha_mgr_state_set(ulp_ctx,
302 : : ULP_HA_STATE_PRIM_RUN);
303 [ # # ]: 0 : if (rc) {
304 : 0 : BNXT_TF_DBG(ERR,
305 : : "On HA CB:Failed(%d) to set state\n",
306 : : rc);
307 : 0 : goto cb_restart;
308 : : }
309 : :
310 : 0 : cparms.dir = TF_DIR_RX;
311 : 0 : cparms.tcam_tbl_type =
312 : : TF_TCAM_TBL_TYPE_WC_TCAM_HIGH;
313 : 0 : rc = tf_clear_tcam_shared_entries(tfp, &cparms);
314 [ # # ]: 0 : if (rc) {
315 : 0 : BNXT_TF_DBG(ERR,
316 : : "On HA CB:Failed(%d) clear tcam\n",
317 : : rc);
318 : 0 : goto cb_restart;
319 : : }
320 [ # # ]: 0 : } else if (curr_state == ULP_HA_STATE_PRIM_SEC_RUN &&
321 [ # # ]: 0 : app_type == ULP_HA_APP_TYPE_SEC) {
322 : : /*
323 : : * The PRIMARY went away:
324 : : * 1. Set the state to SEC_COPY
325 : : * 2. Clear the Low Region for the next copy
326 : : */
327 : 0 : rc = ulp_ha_mgr_state_set(ulp_ctx,
328 : : ULP_HA_STATE_SEC_TIMER_COPY);
329 [ # # ]: 0 : if (rc) {
330 : 0 : BNXT_TF_DBG(ERR,
331 : : "On HA CB:Failed(%d) to set state\n",
332 : : rc);
333 : 0 : goto cb_restart;
334 : : }
335 : 0 : curr_state = ULP_HA_STATE_SEC_TIMER_COPY;
336 : : }
337 : : }
338 : :
339 : : /* Only the Secondary has work to on SEC_TIMER_COPY */
340 [ # # ]: 0 : if (curr_state != ULP_HA_STATE_SEC_TIMER_COPY ||
341 [ # # ]: 0 : app_type != ULP_HA_APP_TYPE_SEC)
342 : 0 : goto cb_restart;
343 : :
344 : : /* Protect the flow database during the copy */
345 [ # # ]: 0 : if (bnxt_ulp_cntxt_acquire_fdb_lock(ulp_ctx)) {
346 : : /* Should not fail, if we do, restart timer and try again */
347 : 0 : BNXT_TF_DBG(ERR, "Flow db lock acquire failed\n");
348 : 0 : goto cb_restart;
349 : : }
350 : : /* All paths after this point must release the fdb lock */
351 : :
352 : : /* The Primary has issued a close and we are in the timer copy
353 : : * phase. Become the new Primary, Set state to Primary Run and
354 : : * move WC entries to Low Region.
355 : : */
356 : 0 : BNXT_TF_DBG(INFO, "On HA CB: Moving entries HI to LOW\n");
357 : :
358 : 0 : cparms.dir = TF_DIR_RX;
359 : 0 : cparms.tcam_tbl_type = TF_TCAM_TBL_TYPE_WC_TCAM_LOW;
360 : 0 : rc = tf_clear_tcam_shared_entries(tfp, &cparms);
361 [ # # ]: 0 : if (rc) {
362 : 0 : BNXT_TF_DBG(ERR,
363 : : "On HA CB:Failed(%d) clear tcam low\n",
364 : : rc);
365 : 0 : goto unlock;
366 : : }
367 : :
368 : 0 : mparms.dir = TF_DIR_RX;
369 : 0 : mparms.tcam_tbl_type = TF_TCAM_TBL_TYPE_WC_TCAM_HIGH;
370 : 0 : rc = tf_move_tcam_shared_entries(tfp, &mparms);
371 [ # # ]: 0 : if (rc) {
372 : 0 : BNXT_TF_DBG(ERR, "On HA_CB: Failed to move entries\n");
373 : 0 : goto unlock;
374 : : }
375 : :
376 : 0 : ulp_ha_mgr_region_set(ulp_ctx, ULP_HA_REGION_LOW);
377 : 0 : ulp_ha_mgr_app_type_set(ulp_ctx, ULP_HA_APP_TYPE_PRIM);
378 : 0 : ulp_ha_mgr_state_set(ulp_ctx, ULP_HA_STATE_PRIM_RUN);
379 : 0 : BNXT_TF_DBG(INFO, "On HA CB: SEC[SEC_TIMER_COPY] => PRIM[PRIM_RUN]\n");
380 : 0 : unlock:
381 : 0 : bnxt_ulp_cntxt_release_fdb_lock(ulp_ctx);
382 : 0 : cb_restart:
383 : 0 : bnxt_ulp_cntxt_entry_release();
384 : : ulp_ha_mgr_timer_start(arg);
385 : : }
386 : :
387 : : static int32_t
388 : : ulp_ha_mgr_timer_start(void *arg)
389 : : {
390 : 0 : rte_eal_alarm_set(US_PER_S * ULP_HA_TIMER_SEC,
391 : : ulp_ha_mgr_timer_cb, arg);
392 : : return 0;
393 : : }
394 : :
395 : : static void
396 : : ulp_ha_mgr_timer_cancel(struct bnxt_ulp_context *ulp_ctx)
397 : : {
398 : 0 : rte_eal_alarm_cancel(ulp_ha_mgr_timer_cb, ulp_ctx->cfg_data);
399 : : }
400 : :
401 : : int32_t
402 : 0 : ulp_ha_mgr_init(struct bnxt_ulp_context *ulp_ctx)
403 : : {
404 : : struct bnxt_ulp_ha_mgr_info *ha_info;
405 : : int32_t rc;
406 : 0 : ha_info = rte_zmalloc("ulp_ha_mgr_info", sizeof(*ha_info), 0);
407 [ # # ]: 0 : if (!ha_info)
408 : : return -ENOMEM;
409 : :
410 : : /* Add the HA info tbl to the ulp context. */
411 : 0 : bnxt_ulp_cntxt_ptr2_ha_info_set(ulp_ctx, ha_info);
412 : :
413 : 0 : rc = pthread_mutex_init(&ha_info->ha_lock, NULL);
414 [ # # ]: 0 : if (rc) {
415 : 0 : PMD_DRV_LOG(ERR, "Failed to initialize ha mutex\n");
416 : 0 : goto cleanup;
417 : : }
418 : 0 : rc = ulp_ha_mgr_timer_start(ulp_ctx->cfg_data);
419 : : if (rc) {
420 : : BNXT_TF_DBG(ERR, "Unable to start timer CB.\n");
421 : : goto cleanup;
422 : : }
423 : :
424 : 0 : return 0;
425 : : cleanup:
426 : : if (ha_info != NULL)
427 : 0 : ulp_ha_mgr_deinit(ulp_ctx);
428 : 0 : return -ENOMEM;
429 : : }
430 : :
431 : : void
432 : 0 : ulp_ha_mgr_deinit(struct bnxt_ulp_context *ulp_ctx)
433 : : {
434 : : struct bnxt_ulp_ha_mgr_info *ha_info;
435 : :
436 : : ulp_ha_mgr_timer_cancel(ulp_ctx);
437 : :
438 : 0 : ha_info = bnxt_ulp_cntxt_ptr2_ha_info_get(ulp_ctx);
439 [ # # ]: 0 : if (ha_info == NULL) {
440 : 0 : BNXT_TF_DBG(ERR, "Unable to get HA Info for deinit.\n");
441 : 0 : return;
442 : : }
443 : :
444 : 0 : pthread_mutex_destroy(&ha_info->ha_lock);
445 : 0 : rte_free(ha_info);
446 : :
447 : 0 : bnxt_ulp_cntxt_ptr2_ha_info_set(ulp_ctx, NULL);
448 : : }
449 : :
450 : : int32_t
451 : 0 : ulp_ha_mgr_app_type_get(struct bnxt_ulp_context *ulp_ctx,
452 : : enum ulp_ha_mgr_app_type *app_type)
453 : : {
454 : : struct bnxt_ulp_ha_mgr_info *ha_info;
455 : :
456 [ # # ]: 0 : if (ulp_ctx == NULL || app_type == NULL) {
457 : 0 : BNXT_TF_DBG(ERR, "Invalid Parms.\n");
458 : 0 : return -EINVAL;
459 : : }
460 : :
461 : 0 : ha_info = bnxt_ulp_cntxt_ptr2_ha_info_get(ulp_ctx);
462 [ # # ]: 0 : if (ha_info == NULL) {
463 : 0 : BNXT_TF_DBG(ERR, "Unable to get the HA info.\n");
464 : 0 : return -EINVAL;
465 : : }
466 : 0 : *app_type = ha_info->app_type;
467 : :
468 : 0 : return 0;
469 : : }
470 : :
471 : : static int32_t
472 : 0 : ulp_ha_mgr_state_get_v1(struct bnxt_ulp_context *ulp_ctx,
473 : : enum ulp_ha_mgr_state *state)
474 : : {
475 : 0 : struct tf_get_if_tbl_entry_parms get_parms = { 0 };
476 : : struct tf *tfp;
477 : 0 : uint32_t val = 0;
478 : : int32_t rc = 0;
479 : :
480 [ # # ]: 0 : if (ulp_ctx == NULL || state == NULL) {
481 : 0 : BNXT_TF_DBG(ERR, "Invalid parms in state get.\n");
482 : 0 : return -EINVAL;
483 : : }
484 : 0 : tfp = bnxt_ulp_cntxt_tfp_get(ulp_ctx, BNXT_ULP_SESSION_TYPE_DEFAULT);
485 [ # # ]: 0 : if (tfp == NULL) {
486 : 0 : BNXT_TF_DBG(ERR, "Unable to get the TFP.\n");
487 : 0 : return -EINVAL;
488 : : }
489 : :
490 : 0 : get_parms.dir = ULP_HA_IF_TBL_DIR;
491 : 0 : get_parms.type = ULP_HA_IF_TBL_TYPE;
492 : 0 : get_parms.idx = bnxt_ulp_ha_reg_state_get(ulp_ctx);
493 : 0 : get_parms.data = (uint8_t *)&val;
494 : 0 : get_parms.data_sz_in_bytes = sizeof(val);
495 : :
496 : 0 : rc = tf_get_if_tbl_entry(tfp, &get_parms);
497 [ # # ]: 0 : if (rc)
498 : 0 : BNXT_TF_DBG(ERR, "Failed to read the HA state\n");
499 : :
500 : 0 : *state = val;
501 : 0 : return rc;
502 : : }
503 : :
504 : : int32_t
505 : 0 : ulp_ha_mgr_state_get(struct bnxt_ulp_context *ulp_ctx,
506 : : enum ulp_ha_mgr_state *state)
507 : : {
508 [ # # ]: 0 : if (bnxt_ulp_cntxt_multi_shared_session_enabled(ulp_ctx))
509 : 0 : return ulp_ha_mgr_tf_state_get(ulp_ctx, state, NULL);
510 : : else
511 : 0 : return ulp_ha_mgr_state_get_v1(ulp_ctx, state);
512 : : }
513 : :
514 : : int32_t
515 : 0 : ulp_ha_mgr_open(struct bnxt_ulp_context *ulp_ctx)
516 : : {
517 : : enum ulp_ha_mgr_state curr_state;
518 : : int32_t rc;
519 : :
520 : 0 : rc = ulp_ha_mgr_state_get(ulp_ctx, &curr_state);
521 [ # # ]: 0 : if (rc) {
522 : 0 : BNXT_TF_DBG(ERR, "Failed to get HA state on Open (%d)\n", rc);
523 : 0 : return -EINVAL;
524 : : }
525 : :
526 : : /*
527 : : * An Open can only occur during the Init and Primary Run states. During
528 : : * Init, the system attempting to Open will become the only system
529 : : * running. During Primary Run, the system attempting to Open will
530 : : * become the secondary system temporarily, and should eventually be
531 : : * transitioned to the primary system.
532 : : */
533 [ # # # ]: 0 : switch (curr_state) {
534 : 0 : case ULP_HA_STATE_INIT:
535 : : /*
536 : : * No system is running, as we are the primary. Since no other
537 : : * system is running, we start writing into the low region. By
538 : : * writing into the low region, we save room for the secondary
539 : : * system to override our entries by using the high region.
540 : : */
541 : 0 : ulp_ha_mgr_app_type_set(ulp_ctx, ULP_HA_APP_TYPE_PRIM);
542 : 0 : ulp_ha_mgr_region_set(ulp_ctx, ULP_HA_REGION_LOW);
543 : 0 : rc = ulp_ha_mgr_state_set(ulp_ctx, ULP_HA_STATE_PRIM_RUN);
544 [ # # ]: 0 : if (rc) {
545 : 0 : BNXT_TF_DBG(ERR, "On Open: Failed to set PRIM_RUN.\n");
546 : 0 : return -EINVAL;
547 : : }
548 : :
549 : 0 : BNXT_TF_DBG(INFO, "On Open: [INIT] => PRIM[PRIM_RUN]\n");
550 : 0 : break;
551 : 0 : case ULP_HA_STATE_PRIM_RUN:
552 : : /*
553 : : * The secondary system is starting in order to take over.
554 : : * The current primary is expected to eventually close and pass
555 : : * full control to this system;however, until the primary closes
556 : : * both are operational.
557 : : */
558 : 0 : ulp_ha_mgr_app_type_set(ulp_ctx, ULP_HA_APP_TYPE_SEC);
559 : 0 : ulp_ha_mgr_region_set(ulp_ctx, ULP_HA_REGION_HI);
560 : :
561 : 0 : rc = ulp_ha_mgr_state_set(ulp_ctx, ULP_HA_STATE_PRIM_SEC_RUN);
562 [ # # ]: 0 : if (rc) {
563 : 0 : BNXT_TF_DBG(ERR, "On Open: Failed to set PRIM_SEC_RUN\n");
564 : 0 : return -EINVAL;
565 : : }
566 : 0 : BNXT_TF_DBG(INFO, "On Open: [PRIM_RUN] => [PRIM_SEC_RUN]\n");
567 : 0 : break;
568 : 0 : default:
569 : 0 : BNXT_TF_DBG(ERR, "On Open: Unknown state 0x%x\n", curr_state);
570 : 0 : return -EINVAL;
571 : : }
572 : :
573 : : return 0;
574 : : }
575 : :
576 : : int32_t
577 : 0 : ulp_ha_mgr_close(struct bnxt_ulp_context *ulp_ctx)
578 : : {
579 : : enum ulp_ha_mgr_state curr_state, next_state, poll_state;
580 : : enum ulp_ha_mgr_app_type app_type;
581 : : int32_t timeout;
582 : : int32_t rc;
583 : :
584 : 0 : curr_state = ULP_HA_STATE_INIT;
585 : 0 : app_type = ULP_HA_APP_TYPE_NONE;
586 : 0 : rc = ulp_ha_mgr_state_get(ulp_ctx, &curr_state);
587 [ # # ]: 0 : if (rc) {
588 : 0 : BNXT_TF_DBG(ERR, "On Close: Failed(%d) to get HA state\n", rc);
589 : 0 : return -EINVAL;
590 : : }
591 : :
592 : 0 : rc = ulp_ha_mgr_app_type_get(ulp_ctx, &app_type);
593 [ # # ]: 0 : if (rc) {
594 : 0 : BNXT_TF_DBG(ERR, "On Close: Failed to get the app type.\n");
595 : 0 : return -EINVAL;
596 : : }
597 : :
598 [ # # ]: 0 : if (curr_state == ULP_HA_STATE_PRIM_RUN &&
599 [ # # ]: 0 : app_type == ULP_HA_APP_TYPE_PRIM) {
600 : : /*
601 : : * Only the primary is running, so a close effectively moves the
602 : : * system back to INIT.
603 : : */
604 : : next_state = ULP_HA_STATE_INIT;
605 : 0 : ulp_ha_mgr_state_set(ulp_ctx, next_state);
606 : 0 : BNXT_TF_DBG(INFO, "On Close: PRIM[PRIM_RUN] => [INIT]\n");
607 [ # # ]: 0 : } else if (curr_state == ULP_HA_STATE_PRIM_SEC_RUN &&
608 [ # # ]: 0 : app_type == ULP_HA_APP_TYPE_PRIM) {
609 : : /*
610 : : * While both are running, the primary received a close.
611 : : * Cleanup the flows, set the COPY state, and wait for the
612 : : * secondary to become the Primary.
613 : : */
614 : 0 : BNXT_TF_DBG(INFO,
615 : : "On Close: PRIM[PRIM_SEC_RUN] flushing flows.\n");
616 : :
617 : 0 : ulp_flow_db_flush_flows(ulp_ctx, BNXT_ULP_FDB_TYPE_REGULAR);
618 : 0 : ulp_ha_mgr_state_set(ulp_ctx, ULP_HA_STATE_SEC_TIMER_COPY);
619 : :
620 : : /*
621 : : * TODO: This needs to be bounded in case the other system does
622 : : * not move to PRIM_RUN.
623 : : */
624 : 0 : BNXT_TF_DBG(INFO,
625 : : "On Close: PRIM[PRIM_SEC_RUN] => [Copy], enter wait.\n");
626 : : timeout = ULP_HA_WAIT_TIMEOUT;
627 : : do {
628 : : rte_delay_ms(ULP_HA_WAIT_TIME);
629 : 0 : rc = ulp_ha_mgr_state_get(ulp_ctx, &poll_state);
630 [ # # ]: 0 : if (rc) {
631 : 0 : BNXT_TF_DBG(ERR,
632 : : "Failed to get HA state on Close (%d)\n",
633 : : rc);
634 : 0 : goto cleanup;
635 : : }
636 : 0 : timeout -= ULP_HA_WAIT_TIME;
637 : 0 : BNXT_TF_DBG(INFO,
638 : : "On Close: Waiting %d ms for PRIM_RUN\n",
639 : : timeout);
640 [ # # # # ]: 0 : } while (poll_state != ULP_HA_STATE_PRIM_RUN && timeout > 0);
641 : :
642 [ # # ]: 0 : if (timeout <= 0) {
643 : 0 : BNXT_TF_DBG(ERR, "On Close: SEC[COPY] Timed out\n");
644 : 0 : goto cleanup;
645 : : }
646 : :
647 : 0 : BNXT_TF_DBG(INFO, "On Close: PRIM[PRIM_SEC_RUN] => [COPY]\n");
648 [ # # ]: 0 : } else if (curr_state == ULP_HA_STATE_PRIM_SEC_RUN &&
649 [ # # ]: 0 : app_type == ULP_HA_APP_TYPE_SEC) {
650 : : /*
651 : : * While both are running, the secondary unexpectedly received a
652 : : * close.
653 : : */
654 : 0 : ulp_ha_mgr_state_set(ulp_ctx, ULP_HA_STATE_PRIM_RUN);
655 : :
656 : 0 : BNXT_TF_DBG(INFO, "On Close: SEC[PRIM_SEC_RUN] => [PRIM_RUN]\n");
657 [ # # ]: 0 : } else if (curr_state == ULP_HA_STATE_SEC_TIMER_COPY &&
658 [ # # ]: 0 : app_type == ULP_HA_APP_TYPE_SEC) {
659 : : /*
660 : : * While both were running and the Secondary went into copy,
661 : : * secondary received a close. Wait until the former Primary
662 : : * clears the copy stage, close, and set to INIT.
663 : : */
664 : 0 : BNXT_TF_DBG(INFO, "On Close: SEC[COPY] wait for PRIM_RUN\n");
665 : :
666 : : timeout = ULP_HA_WAIT_TIMEOUT;
667 : : do {
668 : : rte_delay_ms(ULP_HA_WAIT_TIME);
669 : 0 : rc = ulp_ha_mgr_state_get(ulp_ctx, &poll_state);
670 [ # # ]: 0 : if (rc) {
671 : 0 : BNXT_TF_DBG(ERR,
672 : : "Failed to get HA state on Close (%d)\n",
673 : : rc);
674 : 0 : goto cleanup;
675 : : }
676 : :
677 : 0 : timeout -= ULP_HA_WAIT_TIME;
678 : 0 : BNXT_TF_DBG(INFO,
679 : : "On Close: Waiting %d ms for PRIM_RUN\n",
680 : : timeout);
681 [ # # # # ]: 0 : } while (poll_state != ULP_HA_STATE_PRIM_RUN &&
682 : : timeout >= 0);
683 : :
684 [ # # ]: 0 : if (timeout <= 0) {
685 : 0 : BNXT_TF_DBG(ERR,
686 : : "On Close: SEC[COPY] Timed out\n");
687 : 0 : goto cleanup;
688 : : }
689 : :
690 : : next_state = ULP_HA_STATE_INIT;
691 : 0 : rc = ulp_ha_mgr_state_set(ulp_ctx, next_state);
692 [ # # ]: 0 : if (rc) {
693 : 0 : BNXT_TF_DBG(ERR,
694 : : "On Close: Failed to set state to INIT(%x)\n",
695 : : rc);
696 : 0 : goto cleanup;
697 : : }
698 : :
699 : 0 : BNXT_TF_DBG(INFO,
700 : : "On Close: SEC[COPY] => [INIT] after %d ms\n",
701 : : ULP_HA_WAIT_TIMEOUT - timeout);
702 : : }
703 : : /* else do nothing just return*/
704 : :
705 : 0 : cleanup:
706 : : return rc;
707 : : }
708 : :
709 : : int32_t
710 : 0 : ulp_ha_mgr_region_get(struct bnxt_ulp_context *ulp_ctx,
711 : : enum ulp_ha_mgr_region *region)
712 : : {
713 : : struct bnxt_ulp_ha_mgr_info *ha_info;
714 : :
715 [ # # ]: 0 : if (ulp_ctx == NULL || region == NULL) {
716 : 0 : BNXT_TF_DBG(ERR, "Invalid params in ha region get.\n");
717 : 0 : return -EINVAL;
718 : : }
719 : :
720 : 0 : ha_info = bnxt_ulp_cntxt_ptr2_ha_info_get(ulp_ctx);
721 [ # # ]: 0 : if (ha_info == NULL) {
722 : 0 : BNXT_TF_DBG(ERR, "Unable to get ha info\n");
723 : 0 : return -EINVAL;
724 : : }
725 : 0 : *region = ha_info->region;
726 : :
727 : 0 : return 0;
728 : : }
|