ipa_table.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. /*
  2. * Copyright (c) 2018-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. #ifndef IPA_TABLE_H
  30. #define IPA_TABLE_H
  31. #include <stdint.h>
  32. #include <stdbool.h>
  33. #include <linux/msm_ipa.h>
  34. #define IPA_TABLE_MAX_ENTRIES 5120
  35. #define IPA_TABLE_INVALID_ENTRY 0x0
  36. #undef VALID_INDEX
  37. #define VALID_INDEX(idx) \
  38. ( (idx) != IPA_TABLE_INVALID_ENTRY )
  39. #undef VALID_RULE_HDL
  40. #define VALID_RULE_HDL(hdl) \
  41. ( (hdl) != IPA_TABLE_INVALID_ENTRY )
  42. #undef GOTO_REC
  43. #define GOTO_REC(tbl, rec_idx) \
  44. ( (tbl)->table_addr + ((rec_idx) * (tbl)->entry_size) )
  45. typedef enum
  46. {
  47. IPA_NAT_BASE_TBL = 0,
  48. IPA_NAT_EXPN_TBL = 1,
  49. IPA_NAT_INDX_TBL = 2,
  50. IPA_NAT_INDEX_EXPN_TBL = 3,
  51. IPA_IPV6CT_BASE_TBL = 4,
  52. IPA_IPV6CT_EXPN_TBL = 5,
  53. } ipa_table_dma_type;
  54. #define VALID_IPA_TABLE_DMA_TYPE(t) \
  55. ( (t) >= IPA_NAT_BASE_TBL && (t) <= IPA_IPV6CT_EXPN_TBL )
  56. /*
  57. * --------- NAT Rule Handle Entry ID structure ---------
  58. *
  59. * +-----------+-----------+------------------+----------------+
  60. * | 1 bit | 2 bits | 12 bits | 1 bit |
  61. * +-----------+-----------+------------------+----------------+
  62. * | 0 - DDR | reserved | index into table | 0 - base |
  63. * | 1 - SRAM | | | 1 - expansion |
  64. * +-----------+-----------+------------------+----------------+
  65. */
  66. #define IPA_TABLE_TYPE_BITS 0x00000001
  67. #define IPA_TABLE_TYPE_MASK 0x00000001
  68. #define IPA_TABLE_INDX_MASK 0x00000FFF
  69. #define IPA_TABLE_TYPE_MEM_SHIFT 15
  70. #undef BREAK_RULE_HDL
  71. #define BREAK_RULE_HDL(tbl, hdl, mt, iet, indx) \
  72. do { \
  73. mt = ((hdl) >> IPA_TABLE_TYPE_MEM_SHIFT) & IPA_TABLE_TYPE_MASK; \
  74. iet = (hdl) & IPA_TABLE_TYPE_MASK; \
  75. indx = ((hdl) >> IPA_TABLE_TYPE_BITS) & IPA_TABLE_INDX_MASK; \
  76. indx += (iet) ? tbl->table_entries : 0; \
  77. /*IPADBG("hdl(%u) -> mt(%u) iet(%u) indx(%u)\n", hdl, mt, iet, indx);*/ \
  78. } while ( 0 )
  79. typedef int (*entry_validity_checker)(
  80. void* entry);
  81. typedef uint16_t (*entry_next_index_getter)(
  82. void* entry);
  83. typedef uint16_t (*entry_prev_index_getter)(
  84. void* entry,
  85. uint16_t entry_index,
  86. void* meta,
  87. uint16_t base_table_size);
  88. typedef void (*entry_prev_index_setter)(
  89. void* entry,
  90. uint16_t entry_index,
  91. uint16_t prev_index,
  92. void* meta,
  93. uint16_t base_table_size);
  94. typedef int (*entry_head_inserter)(
  95. void* entry,
  96. void* user_data,
  97. uint16_t* dma_command_data);
  98. typedef int (*entry_tail_inserter)(
  99. void* entry,
  100. void* user_data);
  101. typedef uint16_t (*entry_delete_head_dma_command_data_getter)(
  102. void* head,
  103. void* next_entry);
  104. typedef struct
  105. {
  106. entry_validity_checker entry_is_valid;
  107. entry_next_index_getter entry_get_next_index;
  108. entry_prev_index_getter entry_get_prev_index;
  109. entry_prev_index_setter entry_set_prev_index;
  110. entry_head_inserter entry_head_insert;
  111. entry_tail_inserter entry_tail_insert;
  112. entry_delete_head_dma_command_data_getter
  113. entry_get_delete_head_dma_command_data;
  114. } ipa_table_entry_interface;
  115. typedef enum
  116. {
  117. HELP_UPDATE_HEAD = 0,
  118. HELP_UPDATE_ENTRY = 1,
  119. HELP_DELETE_HEAD = 2,
  120. HELP_UPDATE_MAX,
  121. } dma_help_type;
  122. #undef VALID_DMA_HELP_TYPE
  123. #define VALID_DMA_HELP_TYPE(t) \
  124. ( (t) >= HELP_UPDATE_HEAD && (t) < HELP_UPDATE_MAX )
  125. typedef struct
  126. {
  127. uint32_t offset;
  128. ipa_table_dma_type table_type;
  129. ipa_table_dma_type expn_table_type;
  130. uint8_t table_indx;
  131. } ipa_table_dma_cmd_helper;
  132. typedef struct
  133. {
  134. char name[IPA_RESOURCE_NAME_MAX];
  135. enum ipa3_nat_mem_in nmi;
  136. int entry_size;
  137. uint16_t table_entries;
  138. uint16_t expn_table_entries;
  139. uint32_t tot_tbl_ents;
  140. uint8_t* table_addr;
  141. uint8_t* expn_table_addr;
  142. uint16_t cur_tbl_cnt;
  143. uint16_t cur_expn_tbl_cnt;
  144. ipa_table_entry_interface* entry_interface;
  145. ipa_table_dma_cmd_helper* dma_help[HELP_UPDATE_MAX];
  146. void* meta;
  147. int meta_entry_size;
  148. } ipa_table;
  149. typedef struct
  150. {
  151. uint16_t prev_index;
  152. void* prev_entry;
  153. uint16_t curr_index;
  154. void* curr_entry;
  155. uint16_t next_index;
  156. void* next_entry;
  157. } ipa_table_iterator;
  158. void ipa_table_init(
  159. ipa_table* table,
  160. const char* table_name,
  161. enum ipa3_nat_mem_in nmi,
  162. int entry_size,
  163. void* meta,
  164. int meta_entry_size,
  165. ipa_table_entry_interface* entry_interface);
  166. int ipa_table_calculate_entries_num(
  167. ipa_table* table,
  168. uint16_t number_of_entries,
  169. enum ipa3_nat_mem_in nmi);
  170. int ipa_table_calculate_size(
  171. ipa_table* table);
  172. uint8_t* ipa_table_calculate_addresses(
  173. ipa_table* table,
  174. uint8_t* base_addr);
  175. void ipa_table_reset(
  176. ipa_table* table);
  177. int ipa_table_add_entry(
  178. ipa_table* table,
  179. void* user_data,
  180. uint16_t* index,
  181. uint32_t* rule_hdl,
  182. struct ipa_ioc_nat_dma_cmd* cmd);
  183. void ipa_table_create_delete_command(
  184. ipa_table* table,
  185. struct ipa_ioc_nat_dma_cmd* cmd,
  186. ipa_table_iterator* iterator);
  187. void ipa_table_delete_entry(
  188. ipa_table* table,
  189. ipa_table_iterator* iterator,
  190. uint8_t is_prev_empty);
  191. void ipa_table_erase_entry(
  192. ipa_table* table,
  193. uint16_t index);
  194. int ipa_table_get_entry(
  195. ipa_table* table,
  196. uint32_t entry_handle,
  197. void** entry,
  198. uint16_t* entry_index);
  199. void* ipa_table_get_entry_by_index(
  200. ipa_table* table,
  201. uint16_t index);
  202. void ipa_table_dma_cmd_helper_init(
  203. ipa_table_dma_cmd_helper* dma_cmd_helper,
  204. uint8_t table_indx,
  205. ipa_table_dma_type table_type,
  206. ipa_table_dma_type expn_table_type,
  207. uint32_t offset);
  208. void ipa_table_dma_cmd_generate(
  209. ipa_table_dma_cmd_helper* dma_cmd_helper,
  210. uint8_t is_expn,
  211. uint32_t entry_offset,
  212. uint16_t data,
  213. struct ipa_ioc_nat_dma_cmd* cmd);
  214. int ipa_table_iterator_init(
  215. ipa_table_iterator* iterator,
  216. ipa_table* table,
  217. void* curr_entry,
  218. uint16_t curr_index);
  219. int ipa_table_iterator_next(
  220. ipa_table_iterator* iterator,
  221. ipa_table* table);
  222. int ipa_table_iterator_end(
  223. ipa_table_iterator* iterator,
  224. ipa_table* table,
  225. uint16_t head_index,
  226. void* head);
  227. int ipa_table_iterator_is_head_with_tail(
  228. ipa_table_iterator* iterator);
  229. int ipa_calc_num_sram_table_entries(
  230. uint32_t sram_size,
  231. uint32_t table1_ent_size,
  232. uint32_t table2_ent_size,
  233. uint16_t* num_entries_ptr);
  234. typedef int (*ipa_table_walk_cb)(
  235. ipa_table* table_ptr,
  236. uint32_t rule_hdl,
  237. void* record_ptr,
  238. uint16_t record_index,
  239. void* meta_record_ptr,
  240. uint16_t meta_record_index,
  241. void* arb_data_ptr );
  242. typedef enum
  243. {
  244. WHEN_SLOT_EMPTY = 0,
  245. WHEN_SLOT_FILLED = 1,
  246. WHEN_SLOT_MAX
  247. } When2Callback;
  248. #define VALID_WHEN2CALLBACK(w) \
  249. ( (w) >= WHEN_SLOT_EMPTY && (w) < WHEN_SLOT_MAX )
  250. int ipa_table_walk(
  251. ipa_table* table,
  252. uint16_t start_index,
  253. When2Callback when,
  254. ipa_table_walk_cb walk_cb,
  255. void* arb_data_ptr );
  256. int ipa_table_add_dma_cmd(
  257. ipa_table* tbl_ptr,
  258. dma_help_type help_type,
  259. void* rec_ptr,
  260. uint16_t rec_index,
  261. uint16_t data_for_entry,
  262. struct ipa_ioc_nat_dma_cmd* cmd_ptr );
  263. #endif