1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2019-2020 Broadcom
3  * All rights reserved.
4  */
5 
6 #ifndef _TF_SHADOW_TBL_H_
7 #define _TF_SHADOW_TBL_H_
8 
9 #include "tf_core.h"
10 
11 /**
12  * The Shadow Table module provides shadow DB handling for table based
13  * TF types. A shadow DB provides the capability that allows for reuse
14  * of TF resources.
15  *
16  * A Shadow table DB is intended to be used by the Table Type module
17  * only.
18  */
19 
20 /**
21  * Shadow DB configuration information for a single table type.
22  *
23  * During Device initialization the HCAPI device specifics are learned
24  * and as well as the RM DB creation. From that those initial steps
25  * this structure can be populated.
26  *
27  * NOTE:
28  * If used in an array of table types then such array must be ordered
29  * by the TF type is represents.
30  */
31 struct tf_shadow_tbl_cfg_parms {
32 	/**
33 	 * [in] The number of elements in the alloc_cnt and base_addr
34 	 * For now, it should always be equal to TF_TBL_TYPE_MAX
35 	 */
36 	int num_entries;
37 
38 	/**
39 	 * [in] Resource allocation count array
40 	 * This array content originates from the tf_session_resources
41 	 * that is passed in on session open
42 	 * Array size is TF_TBL_TYPE_MAX
43 	 */
44 	uint16_t *alloc_cnt;
45 	/**
46 	 * [in] The base index for each table
47 	 */
48 	uint16_t base_addr[TF_TBL_TYPE_MAX];
49 };
50 
51 /**
52  * Shadow table DB creation parameters
53  */
54 struct tf_shadow_tbl_create_db_parms {
55 	/**
56 	 * [in] Receive or transmit direction
57 	 */
58 	enum tf_dir dir;
59 	/**
60 	 * [in] Configuration information for the shadow db
61 	 */
62 	struct tf_shadow_tbl_cfg_parms *cfg;
63 	/**
64 	 * [out] Shadow table DB handle
65 	 */
66 	void **shadow_db;
67 };
68 
69 /**
70  * Shadow table DB free parameters
71  */
72 struct tf_shadow_tbl_free_db_parms {
73 	/**
74 	 * [in] Shadow table DB handle
75 	 */
76 	void *shadow_db;
77 };
78 
79 /**
80  * Shadow table search parameters
81  */
82 struct tf_shadow_tbl_search_parms {
83 	/**
84 	 * [in] Shadow table DB handle
85 	 */
86 	void *shadow_db;
87 	/**
88 	 * [in,out] The search parms from tf core
89 	 */
90 	struct tf_tbl_alloc_search_parms *sparms;
91 	/**
92 	 * [out] Reference count incremented if hit
93 	 */
94 	uint32_t hb_handle;
95 };
96 
97 /**
98  * Shadow Table bind index parameters
99  */
100 struct tf_shadow_tbl_bind_index_parms {
101 	/**
102 	 * [in] Shadow tcam DB handle
103 	 */
104 	void *shadow_db;
105 	/**
106 	 * [in] receive or transmit direction
107 	 */
108 	enum tf_dir dir;
109 	/**
110 	 * [in] TCAM table type
111 	 */
112 	enum tf_tbl_type type;
113 	/**
114 	 * [in] index of the entry to program
115 	 */
116 	uint16_t idx;
117 	/**
118 	 * [in] struct containing key
119 	 */
120 	uint8_t *data;
121 	/**
122 	 * [in] data size in bytes
123 	 */
124 	uint16_t data_sz_in_bytes;
125 	/**
126 	 * [in] The hash bucket handled returned from the search
127 	 */
128 	uint32_t hb_handle;
129 };
130 
131 /**
132  * Shadow table insert parameters
133  */
134 struct tf_shadow_tbl_insert_parms {
135 	/**
136 	 * [in] Shadow table DB handle
137 	 */
138 	void *shadow_db;
139 	/**
140 	 * [in] The insert parms from tf core
141 	 */
142 	struct tf_tbl_set_parms *sparms;
143 };
144 
145 /**
146  * Shadow table remove parameters
147  */
148 struct tf_shadow_tbl_remove_parms {
149 	/**
150 	 * [in] Shadow table DB handle
151 	 */
152 	void *shadow_db;
153 	/**
154 	 * [in] The free parms from tf core
155 	 */
156 	struct tf_tbl_free_parms *fparms;
157 };
158 
159 /**
160  * @page shadow_tbl Shadow table DB
161  *
162  * @ref tf_shadow_tbl_create_db
163  *
164  * @ref tf_shadow_tbl_free_db
165  *
166  * @reg tf_shadow_tbl_search
167  *
168  * @reg tf_shadow_tbl_insert
169  *
170  * @reg tf_shadow_tbl_remove
171  */
172 
173 /**
174  * Creates and fills a Shadow table DB. The DB is indexed per the
175  * parms structure.
176  *
177  * [in] parms
178  *   Pointer to create db parameters
179  *
180  * Returns
181  *   - (0) if successful.
182  *   - (-EINVAL) on failure.
183  */
184 int tf_shadow_tbl_create_db(struct tf_shadow_tbl_create_db_parms *parms);
185 
186 /**
187  * Closes the Shadow table DB and frees all allocated
188  * resources per the associated database.
189  *
190  * [in] parms
191  *   Pointer to the free DB parameters
192  *
193  * Returns
194  *   - (0) if successful.
195  *   - (-EINVAL) on failure.
196  */
197 int tf_shadow_tbl_free_db(struct tf_shadow_tbl_free_db_parms *parms);
198 
199 /**
200  * Search Shadow table db for matching result
201  *
202  * [in] parms
203  *   Pointer to the search parameters
204  *
205  * Returns
206  *   - (0) if successful, element was found.
207  *   - (-EINVAL) on failure.
208  *
209  * If there is a miss, but there is room for insertion, the hb_handle returned
210  * is used for insertion during the bind index API
211  */
212 int tf_shadow_tbl_search(struct tf_shadow_tbl_search_parms *parms);
213 
214 /**
215  * Bind Shadow table db hash and result tables with result from search/alloc
216  *
217  * [in] parms
218  *   Pointer to the search parameters
219  *
220  * Returns
221  *   - (0) if successful
222  *   - (-EINVAL) on failure.
223  *
224  * This is only called after a MISS in the search returns a hb_handle
225  */
226 int tf_shadow_tbl_bind_index(struct tf_shadow_tbl_bind_index_parms *parms);
227 
228 /**
229  * Inserts an element into the Shadow table DB. Will fail if the
230  * elements ref_count is different from 0. Ref_count after insert will
231  * be incremented.
232  *
233  * [in] parms
234  *   Pointer to insert parameters
235  *
236  * Returns
237  *   - (0) if successful.
238  *   - (-EINVAL) on failure.
239  */
240 int tf_shadow_tbl_insert(struct tf_shadow_tbl_insert_parms *parms);
241 
242 /**
243  * Removes an element from the Shadow table DB. Will fail if the
244  * elements ref_count is 0. Ref_count after removal will be
245  * decremented.
246  *
247  * [in] parms
248  *   Pointer to remove parameter
249  *
250  * Returns
251  *   - (0) if successful.
252  *   - (-EINVAL) on failure.
253  */
254 int tf_shadow_tbl_remove(struct tf_shadow_tbl_remove_parms *parms);
255 
256 #endif /* _TF_SHADOW_TBL_H_ */
257