ipa_nat_statemach.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /*
  2. * Copyright (c) 2019-2020 The Linux Foundation. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are
  6. * met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above
  10. * copyright notice, this list of conditions and the following
  11. * disclaimer in the documentation and/or other materials provided
  12. * with the distribution.
  13. * * Neither the name of The Linux Foundation nor the names of its
  14. * contributors may be used to endorse or promote products derived
  15. * from this software without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
  18. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  19. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
  21. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  22. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  23. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  24. * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  25. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
  26. * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
  27. * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. #if !defined(_IPA_NAT_STATEMACH_H_)
  30. # define _IPA_NAT_STATEMACH_H_
  31. typedef uintptr_t arb_t;
  32. /******************************************************************************/
  33. /**
  34. * The following enum represents the states that a nati object can be
  35. * in.
  36. */
  37. typedef enum {
  38. NATI_STATE_NULL = 0,
  39. NATI_STATE_DDR_ONLY = 1, /* NAT in DDR only (traditional) */
  40. NATI_STATE_SRAM_ONLY = 2, /* NAT in SRAM only (new) */
  41. NATI_STATE_HYBRID = 3, /* NAT simultaneously in both SRAM/DDR */
  42. NATI_STATE_HYBRID_DDR = 4, /* NAT transitioned from SRAM to DDR */
  43. NATI_STATE_LAST
  44. } ipa_nati_state;
  45. # undef strcasesame
  46. # define strcasesame(a, b) (!strcasecmp(a, b))
  47. static inline ipa_nati_state mem_type_str_to_ipa_nati_state(
  48. const char* str )
  49. {
  50. if ( str ) {
  51. if (strcasesame(str, "HYBRID" ))
  52. return NATI_STATE_HYBRID;
  53. if (strcasesame(str, "SRAM" ))
  54. return NATI_STATE_SRAM_ONLY;
  55. }
  56. return NATI_STATE_DDR_ONLY;
  57. }
  58. /******************************************************************************/
  59. /**
  60. * The following enum represents the API triggers that may or may not
  61. * cause a nati object to transition through its various allowable
  62. * states defined in ipa_nati_state above.
  63. */
  64. typedef enum {
  65. NATI_TRIG_NULL = 0,
  66. NATI_TRIG_ADD_TABLE = 1,
  67. NATI_TRIG_DEL_TABLE = 2,
  68. NATI_TRIG_CLR_TABLE = 3,
  69. NATI_TRIG_WLK_TABLE = 4,
  70. NATI_TRIG_TBL_STATS = 5,
  71. NATI_TRIG_ADD_RULE = 6,
  72. NATI_TRIG_DEL_RULE = 7,
  73. NATI_TRIG_TBL_SWITCH = 8,
  74. NATI_TRIG_GOTO_DDR = 9,
  75. NATI_TRIG_GOTO_SRAM = 10,
  76. NATI_TRIG_GET_TSTAMP = 11,
  77. NATI_TRIG_LAST
  78. } ipa_nati_trigger;
  79. /******************************************************************************/
  80. /**
  81. * The following structure used to keep switch stats.
  82. */
  83. typedef struct
  84. {
  85. uint32_t pass;
  86. uint32_t fail;
  87. } nati_switch_stats;
  88. /******************************************************************************/
  89. /**
  90. * The following structure used to direct map usage.
  91. *
  92. * Maps are needed to map rule handles..orig to new and new to orig.
  93. * See comments in ipa_nat_statemach.c on this topic...
  94. */
  95. typedef struct
  96. {
  97. uint32_t orig2new_map;
  98. uint32_t new2orig_map;
  99. } nati_map_pair;
  100. /******************************************************************************/
  101. /**
  102. * The following is a nati object that will maintain state relative to
  103. * various API calls.
  104. */
  105. typedef struct
  106. {
  107. ipa_nati_state prev_state;
  108. ipa_nati_state curr_state;
  109. uint32_t ddr_tbl_hdl;
  110. uint32_t sram_tbl_hdl;
  111. uint32_t tot_slots_in_sram;
  112. uint32_t back_to_sram_thresh;
  113. /*
  114. * tot_rules_in_table[0] for ddr, and
  115. * tot_rules_in_table[1] for sram
  116. */
  117. uint32_t tot_rules_in_table[2];
  118. /*
  119. * map_pairs[0] for ddr, and
  120. * map_pairs[1] for sram
  121. */
  122. nati_map_pair map_pairs[2];
  123. /*
  124. * sw_stats[0] for ddr, and
  125. * sw_stats[1] for sram
  126. */
  127. nati_switch_stats sw_stats[2];
  128. } ipa_nati_obj;
  129. /*
  130. * For use with the arrays above..in ipa_nati_obj...
  131. */
  132. #undef DDR_SUB
  133. #undef SRAM_SUB
  134. #define DDR_SUB 0
  135. #define SRAM_SUB 1
  136. #undef IN_HYBRID_STATE
  137. #define IN_HYBRID_STATE() \
  138. ( nati_obj.curr_state == NATI_STATE_HYBRID || \
  139. nati_obj.curr_state == NATI_STATE_HYBRID_DDR )
  140. #undef SRAM_CURRENTLY_ACTIVE
  141. #define SRAM_CURRENTLY_ACTIVE() \
  142. ( nati_obj.curr_state == NATI_STATE_SRAM_ONLY || \
  143. nati_obj.curr_state == NATI_STATE_HYBRID )
  144. #define SRAM_TO_BE_ACCESSED(t) \
  145. ( SRAM_CURRENTLY_ACTIVE() || \
  146. (t) == NATI_TRIG_GOTO_SRAM || \
  147. (t) == NATI_TRIG_TBL_SWITCH )
  148. /*
  149. * NOTE: The exclusion of timestamp retrieval and table creation
  150. * below.
  151. *
  152. * Why?
  153. *
  154. * In re timestamp:
  155. *
  156. * Because timestamp retrieval institutes too many repetitive
  157. * accesses, hence would lead to too many successive votes. Instead,
  158. * it will be handled differently and in the app layer above.
  159. *
  160. * In re table creation:
  161. *
  162. * Because it can't be known, apriori, whether or not sram is
  163. * really available for use. Instead, we'll move table creation
  164. * voting to a place where we know sram is available.
  165. */
  166. #undef VOTE_REQUIRED
  167. #define VOTE_REQUIRED(t) \
  168. ( SRAM_TO_BE_ACCESSED(t) && \
  169. (t) != NATI_TRIG_GET_TSTAMP && \
  170. (t) != NATI_TRIG_ADD_TABLE )
  171. /******************************************************************************/
  172. /**
  173. * A helper macro for changing a nati object's state...
  174. */
  175. # undef SET_NATIOBJ_STATE
  176. # define SET_NATIOBJ_STATE(x, s) { \
  177. (x)->prev_state = (x)->curr_state; \
  178. (x)->curr_state = s; \
  179. }
  180. /******************************************************************************/
  181. /**
  182. * A function signature for a state/trigger callback function...
  183. */
  184. typedef int (*nati_statemach_cb)(
  185. ipa_nati_obj* nati_obj_ptr,
  186. ipa_nati_trigger trigger,
  187. arb_t* arb_data_ptr );
  188. /******************************************************************************/
  189. /**
  190. * A structure for relating state to trigger callbacks.
  191. */
  192. typedef struct
  193. {
  194. ipa_nati_state state;
  195. ipa_nati_trigger trigger;
  196. nati_statemach_cb sm_cb;
  197. const char* state_as_str;
  198. const char* trigger_as_str;
  199. const char* sm_cb_as_str;
  200. } nati_statemach_tuple;
  201. #undef SM_ROW
  202. #define SM_ROW(s, t, f) \
  203. { s, t, f, #s, #t, #f }
  204. /******************************************************************************/
  205. /**
  206. * FUNCTION: ipa_nati_statemach
  207. *
  208. * PARAMS:
  209. *
  210. * @nati_obj_ptr (IN) A pointer to an initialized nati object
  211. *
  212. * @trigger (IN) The trigger to run through the state machine
  213. *
  214. * @arb_data_ptr (IN) Anything you like. Will be passed, untouched,
  215. * to the state/trigger callback function.
  216. *
  217. * DESCRIPTION:
  218. *
  219. * This function allows a nati object and a trigger to be run
  220. * through the state machine.
  221. *
  222. * RETURNS:
  223. *
  224. * zero on success, otherwise non-zero
  225. */
  226. int ipa_nati_statemach(
  227. ipa_nati_obj* nati_obj_ptr,
  228. ipa_nati_trigger trigger,
  229. arb_t* arb_data_ptr );
  230. #endif /* #if !defined(_IPA_NAT_STATEMACH_H_) */