hal_rx_flow.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  1. /*
  2. * Copyright (c) 2019 The Linux Foundation. All rights reserved.
  3. *
  4. * Permission to use, copy, modify, and/or distribute this software for
  5. * any purpose with or without fee is hereby granted, provided that the
  6. * above copyright notice and this permission notice appear in all
  7. * copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
  10. * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
  11. * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
  12. * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  13. * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  14. * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  15. * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  16. * PERFORMANCE OF THIS SOFTWARE.
  17. */
  18. #ifndef __HAL_RX_FLOW_H
  19. #define __HAL_RX_FLOW_H
  20. #include "hal_flow.h"
  21. #include "wlan_cfg.h"
  22. #include "hal_api.h"
  23. #include "qdf_mem.h"
  24. #include "rx_flow_search_entry.h"
  25. #define HAL_FST_HASH_KEY_SIZE_BITS 315
  26. #define HAL_FST_HASH_KEY_SIZE_BYTES 40
  27. #define HAL_FST_HASH_KEY_SIZE_WORDS 10
  28. #define HAL_FST_HASH_DATA_SIZE 37
  29. #define HAL_FST_HASH_MASK 0x7ffff
  30. #define HAL_RX_FST_ENTRY_SIZE (NUM_OF_DWORDS_RX_FLOW_SEARCH_ENTRY * 4)
  31. /**
  32. * Four possible options for IP SA/DA prefix, currently use 0x0 which
  33. * maps to type 2 in HW spec
  34. */
  35. #define HAL_FST_IP_DA_SA_PFX_TYPE_IPV4_COMPATIBLE_IPV6 2
  36. #define HAL_IP_DA_SA_PREFIX_IPV4_COMPATIBLE_IPV6 0x0
  37. /**
  38. * REO destination indication is a lower 4-bits of hash value
  39. * This should match the REO destination used in Rx hash based routing.
  40. */
  41. #define HAL_REO_DEST_IND_HASH_MASK 0xF
  42. /**
  43. * REO destinations are valid from 16-31 for Hawkeye
  44. * and 0-15 are not setup for SW
  45. */
  46. #define HAL_REO_DEST_IND_START_OFFSET 0x10
  47. /**
  48. * struct hal_rx_flow - Rx Flow parameters to be sent to HW
  49. * @tuple_info: Rx Flow 5-tuple (src & dest IP, src & dest ports, L4 protocol)
  50. * @reo_destination_handler: REO destination for this flow
  51. * @reo_destination_indication: REO indication for this flow
  52. * @fse_metadata: Flow metadata or tag passed to HW for marking packets
  53. */
  54. struct hal_rx_flow {
  55. struct hal_flow_tuple_info tuple_info;
  56. uint8_t reo_destination_handler;
  57. uint8_t reo_destination_indication;
  58. uint32_t fse_metadata;
  59. };
  60. /**
  61. * enum hal_rx_fse_reo_destination_handler
  62. * @HAL_RX_FSE_REO_DEST_FT: Use this entry's destination indication
  63. * @HAL_RX_FSE_REO_DEST_ASPT: Use Address Search + Peer Table's entry
  64. * @HAL_RX_FSE_REO_DEST_FT2: Use FT2's destination indication
  65. * @HAL_RX_FSE_REO_DEST_CCE: Use CCE's destination indication for this entry
  66. */
  67. enum hal_rx_fse_reo_destination_handler {
  68. HAL_RX_FSE_REO_DEST_FT = 0,
  69. HAL_RX_FSE_REO_DEST_ASPT = 1,
  70. HAL_RX_FSE_REO_DEST_FT2 = 2,
  71. HAL_RX_FSE_REO_DEST_CCE = 3,
  72. };
  73. /**
  74. * struct hal_rx_fst - HAL RX Flow search table context
  75. * @base_vaddr: Virtual Base address of HW FST
  76. * @base_paddr: Physical Base address of HW FST
  77. * @key: Pointer to 320-bit Key read from cfg
  78. * @shifted_key: Pointer to left-shifted 320-bit Key used for Toeplitz Hash
  79. * @max_entries : Max number of entries in flow searchh table
  80. * @max_skid_length : Max search length if there is hash collision
  81. * @hash_mask: Hash mask to apply to index into FST
  82. * @key_cache: Toepliz Key Cache configured key
  83. */
  84. struct hal_rx_fst {
  85. uint8_t *base_vaddr;
  86. qdf_dma_addr_t base_paddr;
  87. uint8_t *key;
  88. uint8_t shifted_key[HAL_FST_HASH_KEY_SIZE_BYTES];
  89. uint16_t max_entries;
  90. uint16_t max_skid_length;
  91. uint16_t hash_mask;
  92. uint32_t key_cache[HAL_FST_HASH_KEY_SIZE_BYTES][1 << 8];
  93. };
  94. /**
  95. * hal_rx_flow_setup_fse() - Setup a flow search entry in HW FST
  96. * @fst: Pointer to the Rx Flow Search Table
  97. * @table_offset: offset into the table where the flow is to be setup
  98. * @flow: Flow Parameters
  99. *
  100. * Return: Success/Failure
  101. */
  102. static void *
  103. hal_rx_flow_setup_fse(struct hal_rx_fst *fst, uint32_t table_offset,
  104. struct hal_rx_flow *flow)
  105. {
  106. uint8_t *fse;
  107. bool fse_valid;
  108. if (table_offset >= fst->max_entries) {
  109. QDF_TRACE(QDF_MODULE_ID_TXRX, QDF_TRACE_LEVEL_ERROR,
  110. "HAL FSE table offset %u exceeds max entries %u",
  111. table_offset, fst->max_entries);
  112. return NULL;
  113. }
  114. fse = (uint8_t *)fst->base_vaddr +
  115. (table_offset * HAL_RX_FST_ENTRY_SIZE);
  116. fse_valid = HAL_GET_FLD(fse, RX_FLOW_SEARCH_ENTRY_9, VALID);
  117. if (fse_valid) {
  118. QDF_TRACE(QDF_MODULE_ID_TXRX, QDF_TRACE_LEVEL_DEBUG,
  119. "HAL FSE %pK already valid", fse);
  120. return NULL;
  121. }
  122. HAL_SET_FLD(fse, RX_FLOW_SEARCH_ENTRY_0, SRC_IP_127_96) =
  123. HAL_SET_FLD_SM(RX_FLOW_SEARCH_ENTRY_0, SRC_IP_127_96,
  124. qdf_htonl(flow->tuple_info.src_ip_127_96));
  125. HAL_SET_FLD(fse, RX_FLOW_SEARCH_ENTRY_1, SRC_IP_95_64) =
  126. HAL_SET_FLD_SM(RX_FLOW_SEARCH_ENTRY_1, SRC_IP_95_64,
  127. qdf_htonl(flow->tuple_info.src_ip_95_64));
  128. HAL_SET_FLD(fse, RX_FLOW_SEARCH_ENTRY_2, SRC_IP_63_32) =
  129. HAL_SET_FLD_SM(RX_FLOW_SEARCH_ENTRY_2, SRC_IP_63_32,
  130. qdf_htonl(flow->tuple_info.src_ip_63_32));
  131. HAL_SET_FLD(fse, RX_FLOW_SEARCH_ENTRY_3, SRC_IP_31_0) =
  132. HAL_SET_FLD_SM(RX_FLOW_SEARCH_ENTRY_3, SRC_IP_31_0,
  133. qdf_htonl(flow->tuple_info.src_ip_31_0));
  134. HAL_SET_FLD(fse, RX_FLOW_SEARCH_ENTRY_4, DEST_IP_127_96) =
  135. HAL_SET_FLD_SM(RX_FLOW_SEARCH_ENTRY_4, DEST_IP_127_96,
  136. qdf_htonl(flow->tuple_info.dest_ip_127_96));
  137. HAL_SET_FLD(fse, RX_FLOW_SEARCH_ENTRY_5, DEST_IP_95_64) =
  138. HAL_SET_FLD_SM(RX_FLOW_SEARCH_ENTRY_5, DEST_IP_95_64,
  139. qdf_htonl(flow->tuple_info.dest_ip_95_64));
  140. HAL_SET_FLD(fse, RX_FLOW_SEARCH_ENTRY_6, DEST_IP_63_32) =
  141. HAL_SET_FLD_SM(RX_FLOW_SEARCH_ENTRY_6, DEST_IP_63_32,
  142. qdf_htonl(flow->tuple_info.dest_ip_63_32));
  143. HAL_SET_FLD(fse, RX_FLOW_SEARCH_ENTRY_7, DEST_IP_31_0) =
  144. HAL_SET_FLD_SM(RX_FLOW_SEARCH_ENTRY_7, DEST_IP_31_0,
  145. qdf_htonl(flow->tuple_info.dest_ip_31_0));
  146. HAL_CLR_FLD(fse, RX_FLOW_SEARCH_ENTRY_8, DEST_PORT);
  147. HAL_SET_FLD(fse, RX_FLOW_SEARCH_ENTRY_8, DEST_PORT) |=
  148. HAL_SET_FLD_SM(RX_FLOW_SEARCH_ENTRY_8, DEST_PORT,
  149. (flow->tuple_info.dest_port));
  150. HAL_CLR_FLD(fse, RX_FLOW_SEARCH_ENTRY_8, SRC_PORT);
  151. HAL_SET_FLD(fse, RX_FLOW_SEARCH_ENTRY_8, SRC_PORT) |=
  152. HAL_SET_FLD_SM(RX_FLOW_SEARCH_ENTRY_8, SRC_PORT,
  153. (flow->tuple_info.src_port));
  154. HAL_CLR_FLD(fse, RX_FLOW_SEARCH_ENTRY_9, L4_PROTOCOL);
  155. HAL_SET_FLD(fse, RX_FLOW_SEARCH_ENTRY_9, L4_PROTOCOL) |=
  156. HAL_SET_FLD_SM(RX_FLOW_SEARCH_ENTRY_9, L4_PROTOCOL,
  157. flow->tuple_info.l4_protocol);
  158. HAL_CLR_FLD(fse, RX_FLOW_SEARCH_ENTRY_9, REO_DESTINATION_HANDLER);
  159. HAL_SET_FLD(fse, RX_FLOW_SEARCH_ENTRY_9, REO_DESTINATION_HANDLER) |=
  160. HAL_SET_FLD_SM(RX_FLOW_SEARCH_ENTRY_9, REO_DESTINATION_HANDLER,
  161. flow->reo_destination_handler);
  162. HAL_CLR_FLD(fse, RX_FLOW_SEARCH_ENTRY_9, VALID);
  163. HAL_SET_FLD(fse, RX_FLOW_SEARCH_ENTRY_9, VALID) |=
  164. HAL_SET_FLD_SM(RX_FLOW_SEARCH_ENTRY_9, VALID, 1);
  165. HAL_CLR_FLD(fse, RX_FLOW_SEARCH_ENTRY_10, METADATA);
  166. HAL_SET_FLD(fse, RX_FLOW_SEARCH_ENTRY_10, METADATA) =
  167. HAL_SET_FLD_SM(RX_FLOW_SEARCH_ENTRY_10, METADATA,
  168. flow->fse_metadata);
  169. HAL_CLR_FLD(fse, RX_FLOW_SEARCH_ENTRY_11, REO_DESTINATION_INDICATION);
  170. HAL_SET_FLD(fse, RX_FLOW_SEARCH_ENTRY_11, REO_DESTINATION_INDICATION) |=
  171. HAL_SET_FLD_SM(RX_FLOW_SEARCH_ENTRY_11,
  172. REO_DESTINATION_INDICATION,
  173. flow->reo_destination_indication);
  174. /* Reset all the other fields in FSE */
  175. HAL_CLR_FLD(fse, RX_FLOW_SEARCH_ENTRY_9, RESERVED_9);
  176. HAL_CLR_FLD(fse, RX_FLOW_SEARCH_ENTRY_11, MSDU_DROP);
  177. HAL_CLR_FLD(fse, RX_FLOW_SEARCH_ENTRY_11, RESERVED_11);
  178. HAL_CLR_FLD(fse, RX_FLOW_SEARCH_ENTRY_11, MSDU_COUNT);
  179. HAL_CLR_FLD(fse, RX_FLOW_SEARCH_ENTRY_12, MSDU_BYTE_COUNT);
  180. HAL_CLR_FLD(fse, RX_FLOW_SEARCH_ENTRY_13, TIMESTAMP);
  181. return fse;
  182. }
  183. /**
  184. * hal_rx_flow_delete_entry() - Delete a flow from the Rx Flow Search Table
  185. * @fst: Pointer to the Rx Flow Search Table
  186. * @hal_rx_fse: Pointer to the Rx Flow that is to be deleted from the FST
  187. *
  188. * Return: Success/Failure
  189. */
  190. static inline QDF_STATUS
  191. hal_rx_flow_delete_entry(struct hal_rx_fst *fst, void *hal_rx_fse)
  192. {
  193. uint8_t *fse = (uint8_t *)hal_rx_fse;
  194. if (!HAL_GET_FLD(fse, RX_FLOW_SEARCH_ENTRY_9, VALID))
  195. return QDF_STATUS_E_NOENT;
  196. HAL_CLR_FLD(fse, RX_FLOW_SEARCH_ENTRY_9, VALID);
  197. return QDF_STATUS_SUCCESS;
  198. }
  199. /**
  200. * hal_rx_fst_key_configure() - Configure the Toeplitz key in the FST
  201. * @fst: Pointer to the Rx Flow Search Table
  202. *
  203. * Return: Success/Failure
  204. */
  205. static void hal_rx_fst_key_configure(struct hal_rx_fst *fst)
  206. {
  207. uint8_t key_bytes[HAL_FST_HASH_KEY_SIZE_BYTES];
  208. qdf_mem_copy(key_bytes, fst->key, HAL_FST_HASH_KEY_SIZE_BYTES);
  209. /**
  210. * The Toeplitz algorithm as per the Microsoft spec works in a
  211. * “big-endian” manner, using the MSBs of the key to hash the
  212. * initial bytes of the input going on to use up the lower order bits
  213. * of the key to hash further bytes of the input until the LSBs of the
  214. * key are used finally.
  215. *
  216. * So first, rightshift 320-bit input key 5 times to get 315 MS bits
  217. */
  218. key_bitwise_shift_left(key_bytes, HAL_FST_HASH_KEY_SIZE_BYTES, 5);
  219. key_reverse(fst->shifted_key, key_bytes, HAL_FST_HASH_KEY_SIZE_BYTES);
  220. }
  221. /**
  222. * hal_rx_fst_get_base() - Retrieve the virtual base address of the Rx FST
  223. * @fst: Pointer to the Rx Flow Search Table
  224. *
  225. * Return: Success/Failure
  226. */
  227. static inline void *hal_rx_fst_get_base(struct hal_rx_fst *fst)
  228. {
  229. return fst->base_vaddr;
  230. }
  231. /**
  232. * hal_rx_fst_get_fse_size() - Retrieve the size of each entry(flow) in Rx FST
  233. *
  234. * Return: size of each entry/flow in Rx FST
  235. */
  236. static inline uint32_t hal_rx_fst_get_fse_size(void)
  237. {
  238. return HAL_RX_FST_ENTRY_SIZE;
  239. }
  240. /**
  241. * hal_rx_flow_get_tuple_info() - Retrieve the 5-tuple flow info for an entry
  242. * @hal_fse: Pointer to the Flow in Rx FST
  243. * @tuple_info: 5-tuple info of the flow returned to the caller
  244. *
  245. * Return: Success/Failure
  246. */
  247. QDF_STATUS hal_rx_flow_get_tuple_info(void *hal_fse,
  248. struct hal_flow_tuple_info *tuple_info)
  249. {
  250. if (!hal_fse || !tuple_info)
  251. return QDF_STATUS_E_INVAL;
  252. if (!HAL_GET_FLD(hal_fse, RX_FLOW_SEARCH_ENTRY_9, VALID))
  253. return QDF_STATUS_E_NOENT;
  254. tuple_info->src_ip_127_96 = qdf_ntohl(HAL_GET_FLD(hal_fse,
  255. RX_FLOW_SEARCH_ENTRY_0, SRC_IP_127_96));
  256. tuple_info->src_ip_95_64 = qdf_ntohl(HAL_GET_FLD(hal_fse,
  257. RX_FLOW_SEARCH_ENTRY_1, SRC_IP_95_64));
  258. tuple_info->src_ip_63_32 = qdf_ntohl(HAL_GET_FLD(hal_fse,
  259. RX_FLOW_SEARCH_ENTRY_2, SRC_IP_63_32));
  260. tuple_info->src_ip_31_0 = qdf_ntohl(HAL_GET_FLD(hal_fse,
  261. RX_FLOW_SEARCH_ENTRY_3, SRC_IP_31_0));
  262. tuple_info->dest_ip_127_96 =
  263. qdf_ntohl(HAL_GET_FLD(hal_fse,
  264. RX_FLOW_SEARCH_ENTRY_4, DEST_IP_127_96));
  265. tuple_info->dest_ip_95_64 = qdf_ntohl(HAL_GET_FLD(hal_fse,
  266. RX_FLOW_SEARCH_ENTRY_5, DEST_IP_95_64));
  267. tuple_info->dest_ip_63_32 = qdf_ntohl(HAL_GET_FLD(hal_fse,
  268. RX_FLOW_SEARCH_ENTRY_6, DEST_IP_63_32));
  269. tuple_info->dest_ip_31_0 = qdf_ntohl(HAL_GET_FLD(hal_fse,
  270. RX_FLOW_SEARCH_ENTRY_7, DEST_IP_31_0));
  271. tuple_info->dest_port = (HAL_GET_FLD(hal_fse,
  272. RX_FLOW_SEARCH_ENTRY_8, DEST_PORT));
  273. tuple_info->src_port = (HAL_GET_FLD(hal_fse,
  274. RX_FLOW_SEARCH_ENTRY_8, SRC_PORT));
  275. tuple_info->l4_protocol = HAL_GET_FLD(hal_fse,
  276. RX_FLOW_SEARCH_ENTRY_9, L4_PROTOCOL);
  277. return QDF_STATUS_SUCCESS;
  278. }
  279. /**
  280. * hal_flow_toeplitz_create_cache() - Calculate hashes for each possible
  281. * byte value with the key taken as is
  282. *
  283. * @fst: FST Handle
  284. * @key: Hash Key
  285. *
  286. * Return: Success/Failure
  287. */
  288. void hal_flow_toeplitz_create_cache(struct hal_rx_fst *fst)
  289. {
  290. int bit;
  291. int val;
  292. int i;
  293. uint8_t *key = fst->shifted_key;
  294. /*
  295. * Initialise to first 32 bits of the key; shift in further key material
  296. * through the loop
  297. */
  298. uint32_t cur_key = (key[0] << 24) | (key[1] << 16) | (key[2] << 8) |
  299. key[3];
  300. for (i = 0; i < HAL_FST_HASH_KEY_SIZE_BYTES; i++) {
  301. uint8_t new_key_byte;
  302. uint32_t shifted_key[8];
  303. if (i + 4 < HAL_FST_HASH_KEY_SIZE_BYTES)
  304. new_key_byte = key[i + 4];
  305. else
  306. new_key_byte = 0;
  307. shifted_key[0] = cur_key;
  308. for (bit = 1; bit < 8; bit++) {
  309. /*
  310. * For each iteration, shift out one more bit of the
  311. * current key and shift in one more bit of the new key
  312. * material
  313. */
  314. shifted_key[bit] = cur_key << bit |
  315. new_key_byte >> (8 - bit);
  316. }
  317. for (val = 0; val < (1 << 8); val++) {
  318. uint32_t hash = 0;
  319. int mask;
  320. /*
  321. * For each bit set in the input, XOR in
  322. * the appropriately shifted key
  323. */
  324. for (bit = 0, mask = 1 << 7; bit < 8; bit++, mask >>= 1)
  325. if ((val & mask))
  326. hash ^= shifted_key[bit];
  327. fst->key_cache[i][val] = hash;
  328. }
  329. cur_key = cur_key << 8 | new_key_byte;
  330. }
  331. }
  332. /**
  333. * hal_rx_fst_attach() - Initialize Rx flow search table in HW FST
  334. *
  335. * @qdf_dev: QDF device handle
  336. * @hal_fst_base_paddr: Pointer to the physical base address of the Rx FST
  337. * @max_entries: Max number of flows allowed in the FST
  338. * @max_search: Number of collisions allowed in the hash-based FST
  339. * @hash_key: Toeplitz key used for the hash FST
  340. *
  341. * Return:
  342. */
  343. static struct hal_rx_fst *
  344. hal_rx_fst_attach(qdf_device_t qdf_dev,
  345. uint64_t *hal_fst_base_paddr, uint16_t max_entries,
  346. uint16_t max_search, uint8_t *hash_key)
  347. {
  348. struct hal_rx_fst *fst = qdf_mem_malloc(sizeof(struct hal_rx_fst));
  349. if (!fst) {
  350. QDF_TRACE(QDF_MODULE_ID_TXRX, QDF_TRACE_LEVEL_ERROR,
  351. FL("hal fst allocation failed,"));
  352. return NULL;
  353. }
  354. qdf_mem_set(fst, 0, sizeof(struct hal_rx_fst));
  355. fst->key = hash_key;
  356. fst->max_skid_length = max_search;
  357. fst->max_entries = max_entries;
  358. fst->hash_mask = max_entries - 1;
  359. QDF_TRACE(QDF_MODULE_ID_TXRX, QDF_TRACE_LEVEL_DEBUG,
  360. "HAL FST allocation %x %d * %d\n", fst,
  361. fst->max_entries, HAL_RX_FST_ENTRY_SIZE);
  362. fst->base_vaddr = (uint8_t *)qdf_mem_alloc_consistent(qdf_dev,
  363. qdf_dev->dev,
  364. (fst->max_entries * HAL_RX_FST_ENTRY_SIZE),
  365. &fst->base_paddr);
  366. if (!fst->base_vaddr) {
  367. QDF_TRACE(QDF_MODULE_ID_TXRX, QDF_TRACE_LEVEL_ERROR,
  368. FL("hal fst->base_vaddr allocation failed"));
  369. qdf_mem_free(fst);
  370. return NULL;
  371. }
  372. QDF_TRACE_HEX_DUMP(QDF_MODULE_ID_ANY, QDF_TRACE_LEVEL_DEBUG,
  373. (void *)fst->key, HAL_FST_HASH_KEY_SIZE_BYTES);
  374. qdf_mem_set((uint8_t *)fst->base_vaddr, 0,
  375. (fst->max_entries * HAL_RX_FST_ENTRY_SIZE));
  376. hal_rx_fst_key_configure(fst);
  377. hal_flow_toeplitz_create_cache(fst);
  378. *hal_fst_base_paddr = (uint64_t)fst->base_paddr;
  379. return fst;
  380. }
  381. /**
  382. * hal_rx_fst_detach() - De-init the Rx flow search table from HW
  383. *
  384. * @rx_fst: Pointer to the Rx FST
  385. * @qdf_dev: QDF device handle
  386. *
  387. * Return:
  388. */
  389. void hal_rx_fst_detach(struct hal_rx_fst *rx_fst,
  390. qdf_device_t qdf_dev)
  391. {
  392. if (!rx_fst || !qdf_dev)
  393. return;
  394. qdf_mem_free_consistent(qdf_dev, qdf_dev->dev,
  395. rx_fst->max_entries * HAL_RX_FST_ENTRY_SIZE,
  396. rx_fst->base_vaddr, rx_fst->base_paddr, 0);
  397. qdf_mem_free(rx_fst);
  398. }
  399. /**
  400. * hal_flow_toeplitz_hash() - Calculate Toeplitz hash by using the cached key
  401. *
  402. * @hal_fst: FST Handle
  403. * @flow: Flow Parameters
  404. *
  405. * Return: Success/Failure
  406. */
  407. static inline uint32_t
  408. hal_flow_toeplitz_hash(void *hal_fst, struct hal_rx_flow *flow)
  409. {
  410. int i, j;
  411. uint32_t hash = 0;
  412. struct hal_rx_fst *fst = (struct hal_rx_fst *)hal_fst;
  413. uint32_t input[HAL_FST_HASH_KEY_SIZE_WORDS];
  414. uint8_t *tuple;
  415. qdf_mem_zero(input, HAL_FST_HASH_KEY_SIZE_BYTES);
  416. *(uint32_t *)&input[0] = qdf_htonl(flow->tuple_info.src_ip_127_96);
  417. *(uint32_t *)&input[1] = qdf_htonl(flow->tuple_info.src_ip_95_64);
  418. *(uint32_t *)&input[2] = qdf_htonl(flow->tuple_info.src_ip_63_32);
  419. *(uint32_t *)&input[3] = qdf_htonl(flow->tuple_info.src_ip_31_0);
  420. *(uint32_t *)&input[4] = qdf_htonl(flow->tuple_info.dest_ip_127_96);
  421. *(uint32_t *)&input[5] = qdf_htonl(flow->tuple_info.dest_ip_95_64);
  422. *(uint32_t *)&input[6] = qdf_htonl(flow->tuple_info.dest_ip_63_32);
  423. *(uint32_t *)&input[7] = qdf_htonl(flow->tuple_info.dest_ip_31_0);
  424. *(uint32_t *)&input[8] = (flow->tuple_info.dest_port << 16) |
  425. (flow->tuple_info.src_port);
  426. *(uint32_t *)&input[9] = flow->tuple_info.l4_protocol;
  427. tuple = (uint8_t *)input;
  428. QDF_TRACE_HEX_DUMP(QDF_MODULE_ID_DP, QDF_TRACE_LEVEL_DEBUG,
  429. tuple, sizeof(input));
  430. for (i = 0, j = HAL_FST_HASH_DATA_SIZE - 1;
  431. i < HAL_FST_HASH_KEY_SIZE_BYTES && j >= 0; i++, j--) {
  432. hash ^= fst->key_cache[i][tuple[j]];
  433. }
  434. QDF_TRACE(QDF_MODULE_ID_DP, QDF_TRACE_LEVEL_INFO_LOW,
  435. "Hash value %u %u truncated hash %u\n", hash,
  436. (hash >> 12), (hash >> 12) % (fst->max_entries));
  437. hash >>= 12;
  438. hash &= (fst->max_entries - 1);
  439. return hash;
  440. }
  441. /**
  442. * hal_rx_get_hal_hash() - Retrieve hash index of a flow in the FST table
  443. *
  444. * @hal_fst: HAL Rx FST Handle
  445. * @flow_hash: Flow hash computed from flow tuple
  446. *
  447. * Return: hash index truncated to the size of the hash table
  448. */
  449. inline
  450. uint32_t hal_rx_get_hal_hash(struct hal_rx_fst *hal_fst, uint32_t flow_hash)
  451. {
  452. uint32_t trunc_hash = flow_hash;
  453. /* Take care of hash wrap around scenario */
  454. if (flow_hash >= hal_fst->max_entries)
  455. trunc_hash &= hal_fst->hash_mask;
  456. return trunc_hash;
  457. }
  458. /**
  459. * hal_rx_insert_flow_entry() - Add a flow into the FST table
  460. *
  461. * @hal_fst: HAL Rx FST Handle
  462. * @flow_hash: Flow hash computed from flow tuple
  463. * @flow_tuple_info: Flow tuple used to compute the hash
  464. * @flow_index: Hash index of the flow in the table when inserted successfully
  465. *
  466. * Return: Success if flow is inserted into the table, error otherwise
  467. */
  468. QDF_STATUS
  469. hal_rx_insert_flow_entry(struct hal_rx_fst *fst, uint32_t flow_hash,
  470. void *flow_tuple_info, uint32_t *flow_idx) {
  471. int i;
  472. void *hal_fse;
  473. uint32_t hal_hash;
  474. struct hal_flow_tuple_info hal_tuple_info = { 0 };
  475. QDF_STATUS status;
  476. for (i = 0; i < fst->max_skid_length; i++) {
  477. hal_hash = hal_rx_get_hal_hash(fst, (flow_hash + i));
  478. hal_fse = (uint8_t *)fst->base_vaddr +
  479. (hal_hash * HAL_RX_FST_ENTRY_SIZE);
  480. status = hal_rx_flow_get_tuple_info(hal_fse, &hal_tuple_info);
  481. if (QDF_STATUS_E_NOENT == status)
  482. break;
  483. /* Find the matching flow entry in HW FST */
  484. if (!qdf_mem_cmp(&hal_tuple_info,
  485. flow_tuple_info,
  486. sizeof(struct hal_flow_tuple_info))) {
  487. dp_err("Duplicate flow entry in FST %u at skid %u ",
  488. hal_hash, i);
  489. return QDF_STATUS_E_EXISTS;
  490. }
  491. }
  492. if (i == fst->max_skid_length) {
  493. dp_err("Max skid length reached for hash %u", flow_hash);
  494. return QDF_STATUS_E_RANGE;
  495. }
  496. *flow_idx = hal_hash;
  497. dp_info("flow_hash = %u, skid_entry = %d, flow_addr = %pK flow_idx = %d",
  498. flow_hash, i, hal_fse, *flow_idx);
  499. return QDF_STATUS_SUCCESS;
  500. }
  501. /**
  502. * hal_rx_find_flow_from_tuple() - Find a flow in the FST table
  503. *
  504. * @fst: HAL Rx FST Handle
  505. * @flow_hash: Flow hash computed from flow tuple
  506. * @flow_tuple_info: Flow tuple used to compute the hash
  507. * @flow_index: Hash index of the flow in the table when found
  508. *
  509. * Return: Success if matching flow is found in the table, error otherwise
  510. */
  511. QDF_STATUS
  512. hal_rx_find_flow_from_tuple(struct hal_rx_fst *fst, uint32_t flow_hash,
  513. void *flow_tuple_info, uint32_t *flow_idx)
  514. {
  515. int i;
  516. void *hal_fse;
  517. uint32_t hal_hash;
  518. struct hal_flow_tuple_info hal_tuple_info = { 0 };
  519. QDF_STATUS status;
  520. for (i = 0; i < fst->max_skid_length; i++) {
  521. hal_hash = hal_rx_get_hal_hash(fst, (flow_hash + i));
  522. hal_fse = (uint8_t *)fst->base_vaddr +
  523. (hal_hash * HAL_RX_FST_ENTRY_SIZE);
  524. status = hal_rx_flow_get_tuple_info(hal_fse, &hal_tuple_info);
  525. if (QDF_STATUS_SUCCESS != status)
  526. continue;
  527. /* Find the matching flow entry in HW FST */
  528. if (!qdf_mem_cmp(&hal_tuple_info,
  529. flow_tuple_info,
  530. sizeof(struct hal_flow_tuple_info))) {
  531. break;
  532. }
  533. }
  534. if (i == fst->max_skid_length) {
  535. dp_err("Max skid length reached for hash %u", flow_hash);
  536. return QDF_STATUS_E_RANGE;
  537. }
  538. *flow_idx = hal_hash;
  539. dp_info("flow_hash = %u, skid_entry = %d, flow_addr = %pK flow_idx = %d",
  540. flow_hash, i, hal_fse, *flow_idx);
  541. return QDF_STATUS_SUCCESS;
  542. }
  543. #endif /* HAL_RX_FLOW_H */