xref: /dpdk/drivers/net/nfp/nfp_service.c (revision e9fd1ebf981f361844aea9ec94e17f4bda5e1479)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright (c) 2023 Corigine, Inc.
3  * All rights reserved.
4  */
5 
6 #include "nfp_service.h"
7 
8 #include "nfpcore/nfp_cpp.h"
9 #include "nfp_logs.h"
10 
11 /* Disable service and try to get service status */
12 #define NFP_SERVICE_DISABLE_WAIT_COUNT 3000
13 
14 static int
15 nfp_map_service(struct nfp_service_info *info)
16 {
17 	int32_t ret;
18 	uint32_t slcore = 0;
19 	int32_t slcore_count;
20 	uint8_t service_count;
21 	const char *service_name;
22 	uint32_t slcore_array[RTE_MAX_LCORE];
23 	uint8_t min_service_count = UINT8_MAX;
24 
25 	slcore_count = rte_service_lcore_list(slcore_array, RTE_MAX_LCORE);
26 	if (slcore_count <= 0) {
27 		PMD_DRV_LOG(DEBUG, "No service cores found");
28 		return -ENOENT;
29 	}
30 
31 	/*
32 	 * Find a service core with the least number of services already
33 	 * registered to it
34 	 */
35 	while (slcore_count--) {
36 		service_count = rte_service_lcore_count_services(slcore_array[slcore_count]);
37 		if (service_count < min_service_count) {
38 			slcore = slcore_array[slcore_count];
39 			min_service_count = service_count;
40 		}
41 	}
42 
43 	service_name = rte_service_get_name(info->id);
44 	PMD_INIT_LOG(INFO, "Mapping service %s to core %u", service_name, slcore);
45 
46 	ret = rte_service_map_lcore_set(info->id, slcore, 1);
47 	if (ret != 0) {
48 		PMD_DRV_LOG(DEBUG, "Could not map flower service");
49 		return -ENOENT;
50 	}
51 
52 	rte_service_runstate_set(info->id, 1);
53 	rte_service_component_runstate_set(info->id, 1);
54 	rte_service_lcore_start(slcore);
55 	if (rte_service_may_be_active(slcore))
56 		PMD_DRV_LOG(INFO, "The service %s is running", service_name);
57 	else
58 		PMD_DRV_LOG(ERR, "The service %s is not running", service_name);
59 
60 	info->lcore = slcore;
61 
62 	return 0;
63 }
64 
65 int
66 nfp_service_enable(const struct rte_service_spec *service_spec,
67 		struct nfp_service_info *info)
68 {
69 	int ret;
70 
71 	/* Register the service */
72 	ret = rte_service_component_register(service_spec, &info->id);
73 	if (ret != 0) {
74 		PMD_DRV_LOG(DEBUG, "Could not register %s", service_spec->name);
75 		return -EINVAL;
76 	}
77 
78 	/* Map it to available service core */
79 	ret = nfp_map_service(info);
80 	if (ret != 0) {
81 		PMD_DRV_LOG(DEBUG, "Could not map %s", service_spec->name);
82 		return -EINVAL;
83 	}
84 
85 	PMD_DRV_LOG(DEBUG, "Enable service %s successfully", service_spec->name);
86 
87 	return 0;
88 }
89 
90 int
91 nfp_service_disable(struct nfp_service_info *info)
92 {
93 	uint32_t i;
94 	const char *service_name;
95 
96 	service_name = rte_service_get_name(info->id);
97 	if (service_name == NULL) {
98 		PMD_DRV_LOG(ERR, "Could not find service %u", info->id);
99 		return -EINVAL;
100 	}
101 
102 	rte_service_runstate_set(info->id, 0);
103 	rte_service_component_runstate_set(info->id, 0);
104 
105 	for (i = 0; i < NFP_SERVICE_DISABLE_WAIT_COUNT; i++) {
106 		if (rte_service_may_be_active(info->id) == 0)
107 			break;
108 		rte_delay_ms(1);
109 	}
110 	if (i == NFP_SERVICE_DISABLE_WAIT_COUNT)
111 		PMD_DRV_LOG(ERR, "Could not stop service %s", service_name);
112 
113 	rte_service_map_lcore_set(info->id, info->lcore, 0);
114 	rte_service_component_unregister(info->id);
115 
116 	return 0;
117 }
118