stmmac_tc.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. // SPDX-License-Identifier: (GPL-2.0 OR MIT)
  2. /*
  3. * Copyright (c) 2018 Synopsys, Inc. and/or its affiliates.
  4. * stmmac TC Handling (HW only)
  5. */
  6. #include <net/pkt_cls.h>
  7. #include <net/tc_act/tc_gact.h>
  8. #include "common.h"
  9. #include "dwmac4.h"
  10. #include "dwmac5.h"
  11. #include "stmmac.h"
  12. static void tc_fill_all_pass_entry(struct stmmac_tc_entry *entry)
  13. {
  14. memset(entry, 0, sizeof(*entry));
  15. entry->in_use = true;
  16. entry->is_last = true;
  17. entry->is_frag = false;
  18. entry->prio = ~0x0;
  19. entry->handle = 0;
  20. entry->val.match_data = 0x0;
  21. entry->val.match_en = 0x0;
  22. entry->val.af = 1;
  23. entry->val.dma_ch_no = 0x0;
  24. }
  25. static struct stmmac_tc_entry *tc_find_entry(struct stmmac_priv *priv,
  26. struct tc_cls_u32_offload *cls,
  27. bool free)
  28. {
  29. struct stmmac_tc_entry *entry, *first = NULL, *dup = NULL;
  30. u32 loc = cls->knode.handle;
  31. int i;
  32. for (i = 0; i < priv->tc_entries_max; i++) {
  33. entry = &priv->tc_entries[i];
  34. if (!entry->in_use && !first && free)
  35. first = entry;
  36. if (entry->handle == loc && !free)
  37. dup = entry;
  38. }
  39. if (dup)
  40. return dup;
  41. if (first) {
  42. first->handle = loc;
  43. first->in_use = true;
  44. /* Reset HW values */
  45. memset(&first->val, 0, sizeof(first->val));
  46. }
  47. return first;
  48. }
  49. static int tc_fill_actions(struct stmmac_tc_entry *entry,
  50. struct stmmac_tc_entry *frag,
  51. struct tc_cls_u32_offload *cls)
  52. {
  53. struct stmmac_tc_entry *action_entry = entry;
  54. const struct tc_action *act;
  55. struct tcf_exts *exts;
  56. LIST_HEAD(actions);
  57. exts = cls->knode.exts;
  58. if (!tcf_exts_has_actions(exts))
  59. return -EINVAL;
  60. if (frag)
  61. action_entry = frag;
  62. tcf_exts_to_list(exts, &actions);
  63. list_for_each_entry(act, &actions, list) {
  64. /* Accept */
  65. if (is_tcf_gact_ok(act)) {
  66. action_entry->val.af = 1;
  67. break;
  68. }
  69. /* Drop */
  70. if (is_tcf_gact_shot(act)) {
  71. action_entry->val.rf = 1;
  72. break;
  73. }
  74. /* Unsupported */
  75. return -EINVAL;
  76. }
  77. return 0;
  78. }
  79. static int tc_fill_entry(struct stmmac_priv *priv,
  80. struct tc_cls_u32_offload *cls)
  81. {
  82. struct stmmac_tc_entry *entry, *frag = NULL;
  83. struct tc_u32_sel *sel = cls->knode.sel;
  84. u32 off, data, mask, real_off, rem;
  85. u32 prio = cls->common.prio;
  86. int ret;
  87. /* Only 1 match per entry */
  88. if (sel->nkeys <= 0 || sel->nkeys > 1)
  89. return -EINVAL;
  90. off = sel->keys[0].off << sel->offshift;
  91. data = sel->keys[0].val;
  92. mask = sel->keys[0].mask;
  93. switch (ntohs(cls->common.protocol)) {
  94. case ETH_P_ALL:
  95. break;
  96. case ETH_P_IP:
  97. off += ETH_HLEN;
  98. break;
  99. default:
  100. return -EINVAL;
  101. }
  102. if (off > priv->tc_off_max)
  103. return -EINVAL;
  104. real_off = off / 4;
  105. rem = off % 4;
  106. entry = tc_find_entry(priv, cls, true);
  107. if (!entry)
  108. return -EINVAL;
  109. if (rem) {
  110. frag = tc_find_entry(priv, cls, true);
  111. if (!frag) {
  112. ret = -EINVAL;
  113. goto err_unuse;
  114. }
  115. entry->frag_ptr = frag;
  116. entry->val.match_en = (mask << (rem * 8)) &
  117. GENMASK(31, rem * 8);
  118. entry->val.match_data = (data << (rem * 8)) &
  119. GENMASK(31, rem * 8);
  120. entry->val.frame_offset = real_off;
  121. entry->prio = prio;
  122. frag->val.match_en = (mask >> (rem * 8)) &
  123. GENMASK(rem * 8 - 1, 0);
  124. frag->val.match_data = (data >> (rem * 8)) &
  125. GENMASK(rem * 8 - 1, 0);
  126. frag->val.frame_offset = real_off + 1;
  127. frag->prio = prio;
  128. frag->is_frag = true;
  129. } else {
  130. entry->frag_ptr = NULL;
  131. entry->val.match_en = mask;
  132. entry->val.match_data = data;
  133. entry->val.frame_offset = real_off;
  134. entry->prio = prio;
  135. }
  136. ret = tc_fill_actions(entry, frag, cls);
  137. if (ret)
  138. goto err_unuse;
  139. return 0;
  140. err_unuse:
  141. if (frag)
  142. frag->in_use = false;
  143. entry->in_use = false;
  144. return ret;
  145. }
  146. static void tc_unfill_entry(struct stmmac_priv *priv,
  147. struct tc_cls_u32_offload *cls)
  148. {
  149. struct stmmac_tc_entry *entry;
  150. entry = tc_find_entry(priv, cls, false);
  151. if (!entry)
  152. return;
  153. entry->in_use = false;
  154. if (entry->frag_ptr) {
  155. entry = entry->frag_ptr;
  156. entry->is_frag = false;
  157. entry->in_use = false;
  158. }
  159. }
  160. static int tc_config_knode(struct stmmac_priv *priv,
  161. struct tc_cls_u32_offload *cls)
  162. {
  163. int ret;
  164. ret = tc_fill_entry(priv, cls);
  165. if (ret)
  166. return ret;
  167. ret = stmmac_rxp_config(priv, priv->hw->pcsr, priv->tc_entries,
  168. priv->tc_entries_max);
  169. if (ret)
  170. goto err_unfill;
  171. return 0;
  172. err_unfill:
  173. tc_unfill_entry(priv, cls);
  174. return ret;
  175. }
  176. static int tc_delete_knode(struct stmmac_priv *priv,
  177. struct tc_cls_u32_offload *cls)
  178. {
  179. int ret;
  180. /* Set entry and fragments as not used */
  181. tc_unfill_entry(priv, cls);
  182. ret = stmmac_rxp_config(priv, priv->hw->pcsr, priv->tc_entries,
  183. priv->tc_entries_max);
  184. if (ret)
  185. return ret;
  186. return 0;
  187. }
  188. static int tc_setup_cls_u32(struct stmmac_priv *priv,
  189. struct tc_cls_u32_offload *cls)
  190. {
  191. switch (cls->command) {
  192. case TC_CLSU32_REPLACE_KNODE:
  193. tc_unfill_entry(priv, cls);
  194. /* Fall through */
  195. case TC_CLSU32_NEW_KNODE:
  196. return tc_config_knode(priv, cls);
  197. case TC_CLSU32_DELETE_KNODE:
  198. return tc_delete_knode(priv, cls);
  199. default:
  200. return -EOPNOTSUPP;
  201. }
  202. }
  203. static int tc_init(struct stmmac_priv *priv)
  204. {
  205. struct dma_features *dma_cap = &priv->dma_cap;
  206. unsigned int count;
  207. if (!dma_cap->frpsel)
  208. return -EINVAL;
  209. switch (dma_cap->frpbs) {
  210. case 0x0:
  211. priv->tc_off_max = 64;
  212. break;
  213. case 0x1:
  214. priv->tc_off_max = 128;
  215. break;
  216. case 0x2:
  217. priv->tc_off_max = 256;
  218. break;
  219. default:
  220. return -EINVAL;
  221. }
  222. switch (dma_cap->frpes) {
  223. case 0x0:
  224. count = 64;
  225. break;
  226. case 0x1:
  227. count = 128;
  228. break;
  229. case 0x2:
  230. count = 256;
  231. break;
  232. default:
  233. return -EINVAL;
  234. }
  235. /* Reserve one last filter which lets all pass */
  236. priv->tc_entries_max = count;
  237. priv->tc_entries = devm_kcalloc(priv->device,
  238. count, sizeof(*priv->tc_entries), GFP_KERNEL);
  239. if (!priv->tc_entries)
  240. return -ENOMEM;
  241. tc_fill_all_pass_entry(&priv->tc_entries[count - 1]);
  242. dev_info(priv->device, "Enabling HW TC (entries=%d, max_off=%d)\n",
  243. priv->tc_entries_max, priv->tc_off_max);
  244. return 0;
  245. }
  246. const struct stmmac_tc_ops dwmac510_tc_ops = {
  247. .init = tc_init,
  248. .setup_cls_u32 = tc_setup_cls_u32,
  249. };