mtk_ppe.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* Copyright (C) 2020 Felix Fietkau <nbd@nbd.name> */
  3. #include <linux/kernel.h>
  4. #include <linux/io.h>
  5. #include <linux/iopoll.h>
  6. #include <linux/etherdevice.h>
  7. #include <linux/platform_device.h>
  8. #include <linux/if_ether.h>
  9. #include <linux/if_vlan.h>
  10. #include <net/dst_metadata.h>
  11. #include <net/dsa.h>
  12. #include <net/ipv6.h>
  13. #include "mtk_eth_soc.h"
  14. #include "mtk_ppe.h"
  15. #include "mtk_ppe_regs.h"
  16. static DEFINE_SPINLOCK(ppe_lock);
  17. static const struct rhashtable_params mtk_flow_l2_ht_params = {
  18. .head_offset = offsetof(struct mtk_flow_entry, l2_node),
  19. .key_offset = offsetof(struct mtk_flow_entry, data.bridge),
  20. .key_len = offsetof(struct mtk_foe_bridge, key_end),
  21. .automatic_shrinking = true,
  22. };
  23. static void ppe_w32(struct mtk_ppe *ppe, u32 reg, u32 val)
  24. {
  25. writel(val, ppe->base + reg);
  26. }
  27. static u32 ppe_r32(struct mtk_ppe *ppe, u32 reg)
  28. {
  29. return readl(ppe->base + reg);
  30. }
  31. static u32 ppe_m32(struct mtk_ppe *ppe, u32 reg, u32 mask, u32 set)
  32. {
  33. u32 val;
  34. val = ppe_r32(ppe, reg);
  35. val &= ~mask;
  36. val |= set;
  37. ppe_w32(ppe, reg, val);
  38. return val;
  39. }
  40. static u32 ppe_set(struct mtk_ppe *ppe, u32 reg, u32 val)
  41. {
  42. return ppe_m32(ppe, reg, 0, val);
  43. }
  44. static u32 ppe_clear(struct mtk_ppe *ppe, u32 reg, u32 val)
  45. {
  46. return ppe_m32(ppe, reg, val, 0);
  47. }
  48. static u32 mtk_eth_timestamp(struct mtk_eth *eth)
  49. {
  50. return mtk_r32(eth, 0x0010) & mtk_get_ib1_ts_mask(eth);
  51. }
  52. static int mtk_ppe_wait_busy(struct mtk_ppe *ppe)
  53. {
  54. int ret;
  55. u32 val;
  56. ret = readl_poll_timeout(ppe->base + MTK_PPE_GLO_CFG, val,
  57. !(val & MTK_PPE_GLO_CFG_BUSY),
  58. 20, MTK_PPE_WAIT_TIMEOUT_US);
  59. if (ret)
  60. dev_err(ppe->dev, "PPE table busy");
  61. return ret;
  62. }
  63. static int mtk_ppe_mib_wait_busy(struct mtk_ppe *ppe)
  64. {
  65. int ret;
  66. u32 val;
  67. ret = readl_poll_timeout(ppe->base + MTK_PPE_MIB_SER_CR, val,
  68. !(val & MTK_PPE_MIB_SER_CR_ST),
  69. 20, MTK_PPE_WAIT_TIMEOUT_US);
  70. if (ret)
  71. dev_err(ppe->dev, "MIB table busy");
  72. return ret;
  73. }
  74. static int mtk_mib_entry_read(struct mtk_ppe *ppe, u16 index, u64 *bytes, u64 *packets)
  75. {
  76. u32 val, cnt_r0, cnt_r1, cnt_r2;
  77. int ret;
  78. val = FIELD_PREP(MTK_PPE_MIB_SER_CR_ADDR, index) | MTK_PPE_MIB_SER_CR_ST;
  79. ppe_w32(ppe, MTK_PPE_MIB_SER_CR, val);
  80. ret = mtk_ppe_mib_wait_busy(ppe);
  81. if (ret)
  82. return ret;
  83. cnt_r0 = readl(ppe->base + MTK_PPE_MIB_SER_R0);
  84. cnt_r1 = readl(ppe->base + MTK_PPE_MIB_SER_R1);
  85. cnt_r2 = readl(ppe->base + MTK_PPE_MIB_SER_R2);
  86. if (mtk_is_netsys_v3_or_greater(ppe->eth)) {
  87. /* 64 bit for each counter */
  88. u32 cnt_r3 = readl(ppe->base + MTK_PPE_MIB_SER_R3);
  89. *bytes = ((u64)cnt_r1 << 32) | cnt_r0;
  90. *packets = ((u64)cnt_r3 << 32) | cnt_r2;
  91. } else {
  92. /* 48 bit byte counter, 40 bit packet counter */
  93. u32 byte_cnt_low = FIELD_GET(MTK_PPE_MIB_SER_R0_BYTE_CNT_LOW, cnt_r0);
  94. u32 byte_cnt_high = FIELD_GET(MTK_PPE_MIB_SER_R1_BYTE_CNT_HIGH, cnt_r1);
  95. u32 pkt_cnt_low = FIELD_GET(MTK_PPE_MIB_SER_R1_PKT_CNT_LOW, cnt_r1);
  96. u32 pkt_cnt_high = FIELD_GET(MTK_PPE_MIB_SER_R2_PKT_CNT_HIGH, cnt_r2);
  97. *bytes = ((u64)byte_cnt_high << 32) | byte_cnt_low;
  98. *packets = ((u64)pkt_cnt_high << 16) | pkt_cnt_low;
  99. }
  100. return 0;
  101. }
  102. static void mtk_ppe_cache_clear(struct mtk_ppe *ppe)
  103. {
  104. ppe_set(ppe, MTK_PPE_CACHE_CTL, MTK_PPE_CACHE_CTL_CLEAR);
  105. ppe_clear(ppe, MTK_PPE_CACHE_CTL, MTK_PPE_CACHE_CTL_CLEAR);
  106. }
  107. static void mtk_ppe_cache_enable(struct mtk_ppe *ppe, bool enable)
  108. {
  109. mtk_ppe_cache_clear(ppe);
  110. ppe_m32(ppe, MTK_PPE_CACHE_CTL, MTK_PPE_CACHE_CTL_EN,
  111. enable * MTK_PPE_CACHE_CTL_EN);
  112. }
  113. static u32 mtk_ppe_hash_entry(struct mtk_eth *eth, struct mtk_foe_entry *e)
  114. {
  115. u32 hv1, hv2, hv3;
  116. u32 hash;
  117. switch (mtk_get_ib1_pkt_type(eth, e->ib1)) {
  118. case MTK_PPE_PKT_TYPE_IPV4_ROUTE:
  119. case MTK_PPE_PKT_TYPE_IPV4_HNAPT:
  120. hv1 = e->ipv4.orig.ports;
  121. hv2 = e->ipv4.orig.dest_ip;
  122. hv3 = e->ipv4.orig.src_ip;
  123. break;
  124. case MTK_PPE_PKT_TYPE_IPV6_ROUTE_3T:
  125. case MTK_PPE_PKT_TYPE_IPV6_ROUTE_5T:
  126. hv1 = e->ipv6.src_ip[3] ^ e->ipv6.dest_ip[3];
  127. hv1 ^= e->ipv6.ports;
  128. hv2 = e->ipv6.src_ip[2] ^ e->ipv6.dest_ip[2];
  129. hv2 ^= e->ipv6.dest_ip[0];
  130. hv3 = e->ipv6.src_ip[1] ^ e->ipv6.dest_ip[1];
  131. hv3 ^= e->ipv6.src_ip[0];
  132. break;
  133. case MTK_PPE_PKT_TYPE_IPV4_DSLITE:
  134. case MTK_PPE_PKT_TYPE_IPV6_6RD:
  135. default:
  136. WARN_ON_ONCE(1);
  137. return MTK_PPE_HASH_MASK;
  138. }
  139. hash = (hv1 & hv2) | ((~hv1) & hv3);
  140. hash = (hash >> 24) | ((hash & 0xffffff) << 8);
  141. hash ^= hv1 ^ hv2 ^ hv3;
  142. hash ^= hash >> 16;
  143. hash <<= (ffs(eth->soc->hash_offset) - 1);
  144. hash &= MTK_PPE_ENTRIES - 1;
  145. return hash;
  146. }
  147. static inline struct mtk_foe_mac_info *
  148. mtk_foe_entry_l2(struct mtk_eth *eth, struct mtk_foe_entry *entry)
  149. {
  150. int type = mtk_get_ib1_pkt_type(eth, entry->ib1);
  151. if (type == MTK_PPE_PKT_TYPE_BRIDGE)
  152. return &entry->bridge.l2;
  153. if (type >= MTK_PPE_PKT_TYPE_IPV4_DSLITE)
  154. return &entry->ipv6.l2;
  155. return &entry->ipv4.l2;
  156. }
  157. static inline u32 *
  158. mtk_foe_entry_ib2(struct mtk_eth *eth, struct mtk_foe_entry *entry)
  159. {
  160. int type = mtk_get_ib1_pkt_type(eth, entry->ib1);
  161. if (type == MTK_PPE_PKT_TYPE_BRIDGE)
  162. return &entry->bridge.ib2;
  163. if (type >= MTK_PPE_PKT_TYPE_IPV4_DSLITE)
  164. return &entry->ipv6.ib2;
  165. return &entry->ipv4.ib2;
  166. }
  167. int mtk_foe_entry_prepare(struct mtk_eth *eth, struct mtk_foe_entry *entry,
  168. int type, int l4proto, u8 pse_port, u8 *src_mac,
  169. u8 *dest_mac)
  170. {
  171. struct mtk_foe_mac_info *l2;
  172. u32 ports_pad, val;
  173. memset(entry, 0, sizeof(*entry));
  174. if (mtk_is_netsys_v2_or_greater(eth)) {
  175. val = FIELD_PREP(MTK_FOE_IB1_STATE, MTK_FOE_STATE_BIND) |
  176. FIELD_PREP(MTK_FOE_IB1_PACKET_TYPE_V2, type) |
  177. FIELD_PREP(MTK_FOE_IB1_UDP, l4proto == IPPROTO_UDP) |
  178. MTK_FOE_IB1_BIND_CACHE_V2 | MTK_FOE_IB1_BIND_TTL_V2;
  179. entry->ib1 = val;
  180. val = FIELD_PREP(MTK_FOE_IB2_DEST_PORT_V2, pse_port) |
  181. FIELD_PREP(MTK_FOE_IB2_PORT_AG_V2, 0xf);
  182. } else {
  183. int port_mg = eth->soc->offload_version > 1 ? 0 : 0x3f;
  184. val = FIELD_PREP(MTK_FOE_IB1_STATE, MTK_FOE_STATE_BIND) |
  185. FIELD_PREP(MTK_FOE_IB1_PACKET_TYPE, type) |
  186. FIELD_PREP(MTK_FOE_IB1_UDP, l4proto == IPPROTO_UDP) |
  187. MTK_FOE_IB1_BIND_CACHE | MTK_FOE_IB1_BIND_TTL;
  188. entry->ib1 = val;
  189. val = FIELD_PREP(MTK_FOE_IB2_DEST_PORT, pse_port) |
  190. FIELD_PREP(MTK_FOE_IB2_PORT_MG, port_mg) |
  191. FIELD_PREP(MTK_FOE_IB2_PORT_AG, 0x1f);
  192. }
  193. if (is_multicast_ether_addr(dest_mac))
  194. val |= mtk_get_ib2_multicast_mask(eth);
  195. ports_pad = 0xa5a5a500 | (l4proto & 0xff);
  196. if (type == MTK_PPE_PKT_TYPE_IPV4_ROUTE)
  197. entry->ipv4.orig.ports = ports_pad;
  198. if (type == MTK_PPE_PKT_TYPE_IPV6_ROUTE_3T)
  199. entry->ipv6.ports = ports_pad;
  200. if (type == MTK_PPE_PKT_TYPE_BRIDGE) {
  201. ether_addr_copy(entry->bridge.src_mac, src_mac);
  202. ether_addr_copy(entry->bridge.dest_mac, dest_mac);
  203. entry->bridge.ib2 = val;
  204. l2 = &entry->bridge.l2;
  205. } else if (type >= MTK_PPE_PKT_TYPE_IPV4_DSLITE) {
  206. entry->ipv6.ib2 = val;
  207. l2 = &entry->ipv6.l2;
  208. } else {
  209. entry->ipv4.ib2 = val;
  210. l2 = &entry->ipv4.l2;
  211. }
  212. l2->dest_mac_hi = get_unaligned_be32(dest_mac);
  213. l2->dest_mac_lo = get_unaligned_be16(dest_mac + 4);
  214. l2->src_mac_hi = get_unaligned_be32(src_mac);
  215. l2->src_mac_lo = get_unaligned_be16(src_mac + 4);
  216. if (type >= MTK_PPE_PKT_TYPE_IPV6_ROUTE_3T)
  217. l2->etype = ETH_P_IPV6;
  218. else
  219. l2->etype = ETH_P_IP;
  220. return 0;
  221. }
  222. int mtk_foe_entry_set_pse_port(struct mtk_eth *eth,
  223. struct mtk_foe_entry *entry, u8 port)
  224. {
  225. u32 *ib2 = mtk_foe_entry_ib2(eth, entry);
  226. u32 val = *ib2;
  227. if (mtk_is_netsys_v2_or_greater(eth)) {
  228. val &= ~MTK_FOE_IB2_DEST_PORT_V2;
  229. val |= FIELD_PREP(MTK_FOE_IB2_DEST_PORT_V2, port);
  230. } else {
  231. val &= ~MTK_FOE_IB2_DEST_PORT;
  232. val |= FIELD_PREP(MTK_FOE_IB2_DEST_PORT, port);
  233. }
  234. *ib2 = val;
  235. return 0;
  236. }
  237. int mtk_foe_entry_set_ipv4_tuple(struct mtk_eth *eth,
  238. struct mtk_foe_entry *entry, bool egress,
  239. __be32 src_addr, __be16 src_port,
  240. __be32 dest_addr, __be16 dest_port)
  241. {
  242. int type = mtk_get_ib1_pkt_type(eth, entry->ib1);
  243. struct mtk_ipv4_tuple *t;
  244. switch (type) {
  245. case MTK_PPE_PKT_TYPE_IPV4_HNAPT:
  246. if (egress) {
  247. t = &entry->ipv4.new;
  248. break;
  249. }
  250. fallthrough;
  251. case MTK_PPE_PKT_TYPE_IPV4_DSLITE:
  252. case MTK_PPE_PKT_TYPE_IPV4_ROUTE:
  253. t = &entry->ipv4.orig;
  254. break;
  255. case MTK_PPE_PKT_TYPE_IPV6_6RD:
  256. entry->ipv6_6rd.tunnel_src_ip = be32_to_cpu(src_addr);
  257. entry->ipv6_6rd.tunnel_dest_ip = be32_to_cpu(dest_addr);
  258. return 0;
  259. default:
  260. WARN_ON_ONCE(1);
  261. return -EINVAL;
  262. }
  263. t->src_ip = be32_to_cpu(src_addr);
  264. t->dest_ip = be32_to_cpu(dest_addr);
  265. if (type == MTK_PPE_PKT_TYPE_IPV4_ROUTE)
  266. return 0;
  267. t->src_port = be16_to_cpu(src_port);
  268. t->dest_port = be16_to_cpu(dest_port);
  269. return 0;
  270. }
  271. int mtk_foe_entry_set_ipv6_tuple(struct mtk_eth *eth,
  272. struct mtk_foe_entry *entry,
  273. __be32 *src_addr, __be16 src_port,
  274. __be32 *dest_addr, __be16 dest_port)
  275. {
  276. int type = mtk_get_ib1_pkt_type(eth, entry->ib1);
  277. u32 *src, *dest;
  278. switch (type) {
  279. case MTK_PPE_PKT_TYPE_IPV4_DSLITE:
  280. src = entry->dslite.tunnel_src_ip;
  281. dest = entry->dslite.tunnel_dest_ip;
  282. break;
  283. case MTK_PPE_PKT_TYPE_IPV6_ROUTE_5T:
  284. case MTK_PPE_PKT_TYPE_IPV6_6RD:
  285. entry->ipv6.src_port = be16_to_cpu(src_port);
  286. entry->ipv6.dest_port = be16_to_cpu(dest_port);
  287. fallthrough;
  288. case MTK_PPE_PKT_TYPE_IPV6_ROUTE_3T:
  289. src = entry->ipv6.src_ip;
  290. dest = entry->ipv6.dest_ip;
  291. break;
  292. default:
  293. WARN_ON_ONCE(1);
  294. return -EINVAL;
  295. }
  296. ipv6_addr_be32_to_cpu(src, src_addr);
  297. ipv6_addr_be32_to_cpu(dest, dest_addr);
  298. return 0;
  299. }
  300. int mtk_foe_entry_set_dsa(struct mtk_eth *eth, struct mtk_foe_entry *entry,
  301. int port)
  302. {
  303. struct mtk_foe_mac_info *l2 = mtk_foe_entry_l2(eth, entry);
  304. l2->etype = BIT(port);
  305. if (!(entry->ib1 & mtk_get_ib1_vlan_layer_mask(eth)))
  306. entry->ib1 |= mtk_prep_ib1_vlan_layer(eth, 1);
  307. else
  308. l2->etype |= BIT(8);
  309. entry->ib1 &= ~mtk_get_ib1_vlan_tag_mask(eth);
  310. return 0;
  311. }
  312. int mtk_foe_entry_set_vlan(struct mtk_eth *eth, struct mtk_foe_entry *entry,
  313. int vid)
  314. {
  315. struct mtk_foe_mac_info *l2 = mtk_foe_entry_l2(eth, entry);
  316. switch (mtk_get_ib1_vlan_layer(eth, entry->ib1)) {
  317. case 0:
  318. entry->ib1 |= mtk_get_ib1_vlan_tag_mask(eth) |
  319. mtk_prep_ib1_vlan_layer(eth, 1);
  320. l2->vlan1 = vid;
  321. return 0;
  322. case 1:
  323. if (!(entry->ib1 & mtk_get_ib1_vlan_tag_mask(eth))) {
  324. l2->vlan1 = vid;
  325. l2->etype |= BIT(8);
  326. } else {
  327. l2->vlan2 = vid;
  328. entry->ib1 += mtk_prep_ib1_vlan_layer(eth, 1);
  329. }
  330. return 0;
  331. default:
  332. return -ENOSPC;
  333. }
  334. }
  335. int mtk_foe_entry_set_pppoe(struct mtk_eth *eth, struct mtk_foe_entry *entry,
  336. int sid)
  337. {
  338. struct mtk_foe_mac_info *l2 = mtk_foe_entry_l2(eth, entry);
  339. if (!(entry->ib1 & mtk_get_ib1_vlan_layer_mask(eth)) ||
  340. (entry->ib1 & mtk_get_ib1_vlan_tag_mask(eth)))
  341. l2->etype = ETH_P_PPP_SES;
  342. entry->ib1 |= mtk_get_ib1_ppoe_mask(eth);
  343. l2->pppoe_id = sid;
  344. return 0;
  345. }
  346. int mtk_foe_entry_set_wdma(struct mtk_eth *eth, struct mtk_foe_entry *entry,
  347. int wdma_idx, int txq, int bss, int wcid,
  348. bool amsdu_en)
  349. {
  350. struct mtk_foe_mac_info *l2 = mtk_foe_entry_l2(eth, entry);
  351. u32 *ib2 = mtk_foe_entry_ib2(eth, entry);
  352. switch (eth->soc->version) {
  353. case 3:
  354. *ib2 &= ~MTK_FOE_IB2_PORT_MG_V2;
  355. *ib2 |= FIELD_PREP(MTK_FOE_IB2_RX_IDX, txq) |
  356. MTK_FOE_IB2_WDMA_WINFO_V2;
  357. l2->w3info = FIELD_PREP(MTK_FOE_WINFO_WCID_V3, wcid) |
  358. FIELD_PREP(MTK_FOE_WINFO_BSS_V3, bss);
  359. l2->amsdu = FIELD_PREP(MTK_FOE_WINFO_AMSDU_EN, amsdu_en);
  360. break;
  361. case 2:
  362. *ib2 &= ~MTK_FOE_IB2_PORT_MG_V2;
  363. *ib2 |= FIELD_PREP(MTK_FOE_IB2_RX_IDX, txq) |
  364. MTK_FOE_IB2_WDMA_WINFO_V2;
  365. l2->winfo = FIELD_PREP(MTK_FOE_WINFO_WCID, wcid) |
  366. FIELD_PREP(MTK_FOE_WINFO_BSS, bss);
  367. break;
  368. default:
  369. *ib2 &= ~MTK_FOE_IB2_PORT_MG;
  370. *ib2 |= MTK_FOE_IB2_WDMA_WINFO;
  371. if (wdma_idx)
  372. *ib2 |= MTK_FOE_IB2_WDMA_DEVIDX;
  373. l2->vlan2 = FIELD_PREP(MTK_FOE_VLAN2_WINFO_BSS, bss) |
  374. FIELD_PREP(MTK_FOE_VLAN2_WINFO_WCID, wcid) |
  375. FIELD_PREP(MTK_FOE_VLAN2_WINFO_RING, txq);
  376. break;
  377. }
  378. return 0;
  379. }
  380. int mtk_foe_entry_set_queue(struct mtk_eth *eth, struct mtk_foe_entry *entry,
  381. unsigned int queue)
  382. {
  383. u32 *ib2 = mtk_foe_entry_ib2(eth, entry);
  384. if (mtk_is_netsys_v2_or_greater(eth)) {
  385. *ib2 &= ~MTK_FOE_IB2_QID_V2;
  386. *ib2 |= FIELD_PREP(MTK_FOE_IB2_QID_V2, queue);
  387. *ib2 |= MTK_FOE_IB2_PSE_QOS_V2;
  388. } else {
  389. *ib2 &= ~MTK_FOE_IB2_QID;
  390. *ib2 |= FIELD_PREP(MTK_FOE_IB2_QID, queue);
  391. *ib2 |= MTK_FOE_IB2_PSE_QOS;
  392. }
  393. return 0;
  394. }
  395. static bool
  396. mtk_flow_entry_match(struct mtk_eth *eth, struct mtk_flow_entry *entry,
  397. struct mtk_foe_entry *data)
  398. {
  399. int type, len;
  400. if ((data->ib1 ^ entry->data.ib1) & MTK_FOE_IB1_UDP)
  401. return false;
  402. type = mtk_get_ib1_pkt_type(eth, entry->data.ib1);
  403. if (type > MTK_PPE_PKT_TYPE_IPV4_DSLITE)
  404. len = offsetof(struct mtk_foe_entry, ipv6._rsv);
  405. else
  406. len = offsetof(struct mtk_foe_entry, ipv4.ib2);
  407. return !memcmp(&entry->data.data, &data->data, len - 4);
  408. }
  409. static void
  410. __mtk_foe_entry_clear(struct mtk_ppe *ppe, struct mtk_flow_entry *entry)
  411. {
  412. struct hlist_head *head;
  413. struct hlist_node *tmp;
  414. if (entry->type == MTK_FLOW_TYPE_L2) {
  415. rhashtable_remove_fast(&ppe->l2_flows, &entry->l2_node,
  416. mtk_flow_l2_ht_params);
  417. head = &entry->l2_flows;
  418. hlist_for_each_entry_safe(entry, tmp, head, l2_data.list)
  419. __mtk_foe_entry_clear(ppe, entry);
  420. return;
  421. }
  422. hlist_del_init(&entry->list);
  423. if (entry->hash != 0xffff) {
  424. struct mtk_foe_entry *hwe = mtk_foe_get_entry(ppe, entry->hash);
  425. hwe->ib1 &= ~MTK_FOE_IB1_STATE;
  426. hwe->ib1 |= FIELD_PREP(MTK_FOE_IB1_STATE, MTK_FOE_STATE_INVALID);
  427. dma_wmb();
  428. mtk_ppe_cache_clear(ppe);
  429. if (ppe->accounting) {
  430. struct mtk_foe_accounting *acct;
  431. acct = ppe->acct_table + entry->hash * sizeof(*acct);
  432. acct->packets = 0;
  433. acct->bytes = 0;
  434. }
  435. }
  436. entry->hash = 0xffff;
  437. if (entry->type != MTK_FLOW_TYPE_L2_SUBFLOW)
  438. return;
  439. hlist_del_init(&entry->l2_data.list);
  440. kfree(entry);
  441. }
  442. static int __mtk_foe_entry_idle_time(struct mtk_ppe *ppe, u32 ib1)
  443. {
  444. u32 ib1_ts_mask = mtk_get_ib1_ts_mask(ppe->eth);
  445. u16 now = mtk_eth_timestamp(ppe->eth);
  446. u16 timestamp = ib1 & ib1_ts_mask;
  447. if (timestamp > now)
  448. return ib1_ts_mask + 1 - timestamp + now;
  449. else
  450. return now - timestamp;
  451. }
  452. static void
  453. mtk_flow_entry_update_l2(struct mtk_ppe *ppe, struct mtk_flow_entry *entry)
  454. {
  455. u32 ib1_ts_mask = mtk_get_ib1_ts_mask(ppe->eth);
  456. struct mtk_flow_entry *cur;
  457. struct mtk_foe_entry *hwe;
  458. struct hlist_node *tmp;
  459. int idle;
  460. idle = __mtk_foe_entry_idle_time(ppe, entry->data.ib1);
  461. hlist_for_each_entry_safe(cur, tmp, &entry->l2_flows, l2_data.list) {
  462. int cur_idle;
  463. u32 ib1;
  464. hwe = mtk_foe_get_entry(ppe, cur->hash);
  465. ib1 = READ_ONCE(hwe->ib1);
  466. if (FIELD_GET(MTK_FOE_IB1_STATE, ib1) != MTK_FOE_STATE_BIND) {
  467. cur->hash = 0xffff;
  468. __mtk_foe_entry_clear(ppe, cur);
  469. continue;
  470. }
  471. cur_idle = __mtk_foe_entry_idle_time(ppe, ib1);
  472. if (cur_idle >= idle)
  473. continue;
  474. idle = cur_idle;
  475. entry->data.ib1 &= ~ib1_ts_mask;
  476. entry->data.ib1 |= ib1 & ib1_ts_mask;
  477. }
  478. }
  479. static void
  480. mtk_flow_entry_update(struct mtk_ppe *ppe, struct mtk_flow_entry *entry)
  481. {
  482. struct mtk_foe_entry foe = {};
  483. struct mtk_foe_entry *hwe;
  484. spin_lock_bh(&ppe_lock);
  485. if (entry->type == MTK_FLOW_TYPE_L2) {
  486. mtk_flow_entry_update_l2(ppe, entry);
  487. goto out;
  488. }
  489. if (entry->hash == 0xffff)
  490. goto out;
  491. hwe = mtk_foe_get_entry(ppe, entry->hash);
  492. memcpy(&foe, hwe, ppe->eth->soc->foe_entry_size);
  493. if (!mtk_flow_entry_match(ppe->eth, entry, &foe)) {
  494. entry->hash = 0xffff;
  495. goto out;
  496. }
  497. entry->data.ib1 = foe.ib1;
  498. out:
  499. spin_unlock_bh(&ppe_lock);
  500. }
  501. static void
  502. __mtk_foe_entry_commit(struct mtk_ppe *ppe, struct mtk_foe_entry *entry,
  503. u16 hash)
  504. {
  505. struct mtk_eth *eth = ppe->eth;
  506. u16 timestamp = mtk_eth_timestamp(eth);
  507. struct mtk_foe_entry *hwe;
  508. u32 val;
  509. if (mtk_is_netsys_v2_or_greater(eth)) {
  510. entry->ib1 &= ~MTK_FOE_IB1_BIND_TIMESTAMP_V2;
  511. entry->ib1 |= FIELD_PREP(MTK_FOE_IB1_BIND_TIMESTAMP_V2,
  512. timestamp);
  513. } else {
  514. entry->ib1 &= ~MTK_FOE_IB1_BIND_TIMESTAMP;
  515. entry->ib1 |= FIELD_PREP(MTK_FOE_IB1_BIND_TIMESTAMP,
  516. timestamp);
  517. }
  518. hwe = mtk_foe_get_entry(ppe, hash);
  519. memcpy(&hwe->data, &entry->data, eth->soc->foe_entry_size - sizeof(hwe->ib1));
  520. wmb();
  521. hwe->ib1 = entry->ib1;
  522. if (ppe->accounting) {
  523. if (mtk_is_netsys_v2_or_greater(eth))
  524. val = MTK_FOE_IB2_MIB_CNT_V2;
  525. else
  526. val = MTK_FOE_IB2_MIB_CNT;
  527. *mtk_foe_entry_ib2(eth, hwe) |= val;
  528. }
  529. dma_wmb();
  530. mtk_ppe_cache_clear(ppe);
  531. }
  532. void mtk_foe_entry_clear(struct mtk_ppe *ppe, struct mtk_flow_entry *entry)
  533. {
  534. spin_lock_bh(&ppe_lock);
  535. __mtk_foe_entry_clear(ppe, entry);
  536. spin_unlock_bh(&ppe_lock);
  537. }
  538. static int
  539. mtk_foe_entry_commit_l2(struct mtk_ppe *ppe, struct mtk_flow_entry *entry)
  540. {
  541. struct mtk_flow_entry *prev;
  542. entry->type = MTK_FLOW_TYPE_L2;
  543. prev = rhashtable_lookup_get_insert_fast(&ppe->l2_flows, &entry->l2_node,
  544. mtk_flow_l2_ht_params);
  545. if (likely(!prev))
  546. return 0;
  547. if (IS_ERR(prev))
  548. return PTR_ERR(prev);
  549. return rhashtable_replace_fast(&ppe->l2_flows, &prev->l2_node,
  550. &entry->l2_node, mtk_flow_l2_ht_params);
  551. }
  552. int mtk_foe_entry_commit(struct mtk_ppe *ppe, struct mtk_flow_entry *entry)
  553. {
  554. const struct mtk_soc_data *soc = ppe->eth->soc;
  555. int type = mtk_get_ib1_pkt_type(ppe->eth, entry->data.ib1);
  556. u32 hash;
  557. if (type == MTK_PPE_PKT_TYPE_BRIDGE)
  558. return mtk_foe_entry_commit_l2(ppe, entry);
  559. hash = mtk_ppe_hash_entry(ppe->eth, &entry->data);
  560. entry->hash = 0xffff;
  561. spin_lock_bh(&ppe_lock);
  562. hlist_add_head(&entry->list, &ppe->foe_flow[hash / soc->hash_offset]);
  563. spin_unlock_bh(&ppe_lock);
  564. return 0;
  565. }
  566. static void
  567. mtk_foe_entry_commit_subflow(struct mtk_ppe *ppe, struct mtk_flow_entry *entry,
  568. u16 hash)
  569. {
  570. const struct mtk_soc_data *soc = ppe->eth->soc;
  571. struct mtk_flow_entry *flow_info;
  572. struct mtk_foe_entry foe = {}, *hwe;
  573. struct mtk_foe_mac_info *l2;
  574. u32 ib1_mask = mtk_get_ib1_pkt_type_mask(ppe->eth) | MTK_FOE_IB1_UDP;
  575. int type;
  576. flow_info = kzalloc_obj(*flow_info, GFP_ATOMIC);
  577. if (!flow_info)
  578. return;
  579. flow_info->l2_data.base_flow = entry;
  580. flow_info->type = MTK_FLOW_TYPE_L2_SUBFLOW;
  581. flow_info->hash = hash;
  582. hlist_add_head(&flow_info->list,
  583. &ppe->foe_flow[hash / soc->hash_offset]);
  584. hlist_add_head(&flow_info->l2_data.list, &entry->l2_flows);
  585. hwe = mtk_foe_get_entry(ppe, hash);
  586. memcpy(&foe, hwe, soc->foe_entry_size);
  587. foe.ib1 &= ib1_mask;
  588. foe.ib1 |= entry->data.ib1 & ~ib1_mask;
  589. l2 = mtk_foe_entry_l2(ppe->eth, &foe);
  590. memcpy(l2, &entry->data.bridge.l2, sizeof(*l2));
  591. type = mtk_get_ib1_pkt_type(ppe->eth, foe.ib1);
  592. if (type == MTK_PPE_PKT_TYPE_IPV4_HNAPT)
  593. memcpy(&foe.ipv4.new, &foe.ipv4.orig, sizeof(foe.ipv4.new));
  594. else if (type >= MTK_PPE_PKT_TYPE_IPV6_ROUTE_3T && l2->etype == ETH_P_IP)
  595. l2->etype = ETH_P_IPV6;
  596. *mtk_foe_entry_ib2(ppe->eth, &foe) = entry->data.bridge.ib2;
  597. __mtk_foe_entry_commit(ppe, &foe, hash);
  598. }
  599. void __mtk_ppe_check_skb(struct mtk_ppe *ppe, struct sk_buff *skb, u16 hash)
  600. {
  601. const struct mtk_soc_data *soc = ppe->eth->soc;
  602. struct hlist_head *head = &ppe->foe_flow[hash / soc->hash_offset];
  603. struct mtk_foe_entry *hwe = mtk_foe_get_entry(ppe, hash);
  604. struct mtk_flow_entry *entry;
  605. struct mtk_foe_bridge key = {};
  606. struct hlist_node *n;
  607. struct ethhdr *eh;
  608. bool found = false;
  609. u8 *tag;
  610. spin_lock_bh(&ppe_lock);
  611. if (FIELD_GET(MTK_FOE_IB1_STATE, hwe->ib1) == MTK_FOE_STATE_BIND)
  612. goto out;
  613. hlist_for_each_entry_safe(entry, n, head, list) {
  614. if (entry->type == MTK_FLOW_TYPE_L2_SUBFLOW) {
  615. if (unlikely(FIELD_GET(MTK_FOE_IB1_STATE, hwe->ib1) ==
  616. MTK_FOE_STATE_BIND))
  617. continue;
  618. entry->hash = 0xffff;
  619. __mtk_foe_entry_clear(ppe, entry);
  620. continue;
  621. }
  622. if (found || !mtk_flow_entry_match(ppe->eth, entry, hwe)) {
  623. if (entry->hash != 0xffff)
  624. entry->hash = 0xffff;
  625. continue;
  626. }
  627. entry->hash = hash;
  628. __mtk_foe_entry_commit(ppe, &entry->data, hash);
  629. found = true;
  630. }
  631. if (found)
  632. goto out;
  633. eh = eth_hdr(skb);
  634. ether_addr_copy(key.dest_mac, eh->h_dest);
  635. ether_addr_copy(key.src_mac, eh->h_source);
  636. tag = skb->data - 2;
  637. key.vlan = 0;
  638. switch (skb->protocol) {
  639. #if IS_ENABLED(CONFIG_NET_DSA)
  640. case htons(ETH_P_XDSA):
  641. if (!netdev_uses_dsa(skb->dev) ||
  642. skb->dev->dsa_ptr->tag_ops->proto != DSA_TAG_PROTO_MTK)
  643. goto out;
  644. if (!skb_metadata_dst(skb))
  645. tag += 4;
  646. if (get_unaligned_be16(tag) != ETH_P_8021Q)
  647. break;
  648. fallthrough;
  649. #endif
  650. case htons(ETH_P_8021Q):
  651. key.vlan = get_unaligned_be16(tag + 2) & VLAN_VID_MASK;
  652. break;
  653. default:
  654. break;
  655. }
  656. entry = rhashtable_lookup_fast(&ppe->l2_flows, &key, mtk_flow_l2_ht_params);
  657. if (!entry)
  658. goto out;
  659. mtk_foe_entry_commit_subflow(ppe, entry, hash);
  660. out:
  661. spin_unlock_bh(&ppe_lock);
  662. }
  663. int mtk_foe_entry_idle_time(struct mtk_ppe *ppe, struct mtk_flow_entry *entry)
  664. {
  665. mtk_flow_entry_update(ppe, entry);
  666. return __mtk_foe_entry_idle_time(ppe, entry->data.ib1);
  667. }
  668. int mtk_ppe_prepare_reset(struct mtk_ppe *ppe)
  669. {
  670. if (!ppe)
  671. return -EINVAL;
  672. /* disable KA */
  673. ppe_clear(ppe, MTK_PPE_TB_CFG, MTK_PPE_TB_CFG_KEEPALIVE);
  674. ppe_clear(ppe, MTK_PPE_BIND_LMT1, MTK_PPE_NTU_KEEPALIVE);
  675. ppe_w32(ppe, MTK_PPE_KEEPALIVE, 0);
  676. usleep_range(10000, 11000);
  677. /* set KA timer to maximum */
  678. ppe_set(ppe, MTK_PPE_BIND_LMT1, MTK_PPE_NTU_KEEPALIVE);
  679. ppe_w32(ppe, MTK_PPE_KEEPALIVE, 0xffffffff);
  680. /* set KA tick select */
  681. ppe_set(ppe, MTK_PPE_TB_CFG, MTK_PPE_TB_TICK_SEL);
  682. ppe_set(ppe, MTK_PPE_TB_CFG, MTK_PPE_TB_CFG_KEEPALIVE);
  683. usleep_range(10000, 11000);
  684. /* disable scan mode */
  685. ppe_clear(ppe, MTK_PPE_TB_CFG, MTK_PPE_TB_CFG_SCAN_MODE);
  686. usleep_range(10000, 11000);
  687. return mtk_ppe_wait_busy(ppe);
  688. }
  689. struct mtk_foe_accounting *mtk_foe_entry_get_mib(struct mtk_ppe *ppe, u32 index,
  690. struct mtk_foe_accounting *diff)
  691. {
  692. struct mtk_foe_accounting *acct;
  693. int size = sizeof(struct mtk_foe_accounting);
  694. u64 bytes, packets;
  695. if (!ppe->accounting)
  696. return NULL;
  697. if (mtk_mib_entry_read(ppe, index, &bytes, &packets))
  698. return NULL;
  699. acct = ppe->acct_table + index * size;
  700. acct->bytes += bytes;
  701. acct->packets += packets;
  702. if (diff) {
  703. diff->bytes = bytes;
  704. diff->packets = packets;
  705. }
  706. return acct;
  707. }
  708. struct mtk_ppe *mtk_ppe_init(struct mtk_eth *eth, void __iomem *base, int index)
  709. {
  710. bool accounting = eth->soc->has_accounting;
  711. const struct mtk_soc_data *soc = eth->soc;
  712. struct mtk_foe_accounting *acct;
  713. struct device *dev = eth->dev;
  714. struct mtk_mib_entry *mib;
  715. struct mtk_ppe *ppe;
  716. u32 foe_flow_size;
  717. void *foe;
  718. ppe = devm_kzalloc(dev, sizeof(*ppe), GFP_KERNEL);
  719. if (!ppe)
  720. return NULL;
  721. rhashtable_init(&ppe->l2_flows, &mtk_flow_l2_ht_params);
  722. /* need to allocate a separate device, since it PPE DMA access is
  723. * not coherent.
  724. */
  725. ppe->base = base;
  726. ppe->eth = eth;
  727. ppe->dev = dev;
  728. ppe->version = eth->soc->offload_version;
  729. ppe->accounting = accounting;
  730. foe = dmam_alloc_coherent(ppe->dev,
  731. MTK_PPE_ENTRIES * soc->foe_entry_size,
  732. &ppe->foe_phys, GFP_KERNEL);
  733. if (!foe)
  734. goto err_free_l2_flows;
  735. ppe->foe_table = foe;
  736. foe_flow_size = (MTK_PPE_ENTRIES / soc->hash_offset) *
  737. sizeof(*ppe->foe_flow);
  738. ppe->foe_flow = devm_kzalloc(dev, foe_flow_size, GFP_KERNEL);
  739. if (!ppe->foe_flow)
  740. goto err_free_l2_flows;
  741. if (accounting) {
  742. mib = dmam_alloc_coherent(ppe->dev, MTK_PPE_ENTRIES * sizeof(*mib),
  743. &ppe->mib_phys, GFP_KERNEL);
  744. if (!mib)
  745. return NULL;
  746. ppe->mib_table = mib;
  747. acct = devm_kzalloc(dev, MTK_PPE_ENTRIES * sizeof(*acct),
  748. GFP_KERNEL);
  749. if (!acct)
  750. return NULL;
  751. ppe->acct_table = acct;
  752. }
  753. mtk_ppe_debugfs_init(ppe, index);
  754. return ppe;
  755. err_free_l2_flows:
  756. rhashtable_destroy(&ppe->l2_flows);
  757. return NULL;
  758. }
  759. void mtk_ppe_deinit(struct mtk_eth *eth)
  760. {
  761. int i;
  762. for (i = 0; i < ARRAY_SIZE(eth->ppe); i++) {
  763. if (!eth->ppe[i])
  764. return;
  765. rhashtable_destroy(&eth->ppe[i]->l2_flows);
  766. }
  767. }
  768. static void mtk_ppe_init_foe_table(struct mtk_ppe *ppe)
  769. {
  770. static const u8 skip[] = { 12, 25, 38, 51, 76, 89, 102 };
  771. int i, k;
  772. memset(ppe->foe_table, 0,
  773. MTK_PPE_ENTRIES * ppe->eth->soc->foe_entry_size);
  774. if (!IS_ENABLED(CONFIG_SOC_MT7621))
  775. return;
  776. /* skip all entries that cross the 1024 byte boundary */
  777. for (i = 0; i < MTK_PPE_ENTRIES; i += 128) {
  778. for (k = 0; k < ARRAY_SIZE(skip); k++) {
  779. struct mtk_foe_entry *hwe;
  780. hwe = mtk_foe_get_entry(ppe, i + skip[k]);
  781. hwe->ib1 |= MTK_FOE_IB1_STATIC;
  782. }
  783. }
  784. }
  785. void mtk_ppe_start(struct mtk_ppe *ppe)
  786. {
  787. u32 val;
  788. if (!ppe)
  789. return;
  790. mtk_ppe_init_foe_table(ppe);
  791. ppe_w32(ppe, MTK_PPE_TB_BASE, ppe->foe_phys);
  792. val = MTK_PPE_TB_CFG_AGE_NON_L4 |
  793. MTK_PPE_TB_CFG_AGE_UNBIND |
  794. MTK_PPE_TB_CFG_AGE_TCP |
  795. MTK_PPE_TB_CFG_AGE_UDP |
  796. MTK_PPE_TB_CFG_AGE_TCP_FIN |
  797. FIELD_PREP(MTK_PPE_TB_CFG_SEARCH_MISS,
  798. MTK_PPE_SEARCH_MISS_ACTION_FORWARD_BUILD) |
  799. FIELD_PREP(MTK_PPE_TB_CFG_KEEPALIVE,
  800. MTK_PPE_KEEPALIVE_DISABLE) |
  801. FIELD_PREP(MTK_PPE_TB_CFG_HASH_MODE, 1) |
  802. FIELD_PREP(MTK_PPE_TB_CFG_SCAN_MODE,
  803. MTK_PPE_SCAN_MODE_CHECK_AGE) |
  804. FIELD_PREP(MTK_PPE_TB_CFG_ENTRY_NUM,
  805. MTK_PPE_ENTRIES_SHIFT);
  806. if (mtk_is_netsys_v2_or_greater(ppe->eth))
  807. val |= MTK_PPE_TB_CFG_INFO_SEL;
  808. if (!mtk_is_netsys_v3_or_greater(ppe->eth))
  809. val |= MTK_PPE_TB_CFG_ENTRY_80B;
  810. ppe_w32(ppe, MTK_PPE_TB_CFG, val);
  811. ppe_w32(ppe, MTK_PPE_IP_PROTO_CHK,
  812. MTK_PPE_IP_PROTO_CHK_IPV4 | MTK_PPE_IP_PROTO_CHK_IPV6);
  813. mtk_ppe_cache_enable(ppe, true);
  814. val = MTK_PPE_FLOW_CFG_IP6_3T_ROUTE |
  815. MTK_PPE_FLOW_CFG_IP6_5T_ROUTE |
  816. MTK_PPE_FLOW_CFG_IP6_6RD |
  817. MTK_PPE_FLOW_CFG_IP4_NAT |
  818. MTK_PPE_FLOW_CFG_IP4_NAPT |
  819. MTK_PPE_FLOW_CFG_IP4_DSLITE |
  820. MTK_PPE_FLOW_CFG_IP4_NAT_FRAG;
  821. if (mtk_is_netsys_v2_or_greater(ppe->eth))
  822. val |= MTK_PPE_MD_TOAP_BYP_CRSN0 |
  823. MTK_PPE_MD_TOAP_BYP_CRSN1 |
  824. MTK_PPE_MD_TOAP_BYP_CRSN2 |
  825. MTK_PPE_FLOW_CFG_IP4_HASH_GRE_KEY;
  826. else
  827. val |= MTK_PPE_FLOW_CFG_IP4_TCP_FRAG |
  828. MTK_PPE_FLOW_CFG_IP4_UDP_FRAG;
  829. ppe_w32(ppe, MTK_PPE_FLOW_CFG, val);
  830. val = FIELD_PREP(MTK_PPE_UNBIND_AGE_MIN_PACKETS, 1000) |
  831. FIELD_PREP(MTK_PPE_UNBIND_AGE_DELTA, 3);
  832. ppe_w32(ppe, MTK_PPE_UNBIND_AGE, val);
  833. val = FIELD_PREP(MTK_PPE_BIND_AGE0_DELTA_UDP, 12) |
  834. FIELD_PREP(MTK_PPE_BIND_AGE0_DELTA_NON_L4, 1);
  835. ppe_w32(ppe, MTK_PPE_BIND_AGE0, val);
  836. val = FIELD_PREP(MTK_PPE_BIND_AGE1_DELTA_TCP_FIN, 1) |
  837. FIELD_PREP(MTK_PPE_BIND_AGE1_DELTA_TCP, 7);
  838. ppe_w32(ppe, MTK_PPE_BIND_AGE1, val);
  839. val = MTK_PPE_BIND_LIMIT0_QUARTER | MTK_PPE_BIND_LIMIT0_HALF;
  840. ppe_w32(ppe, MTK_PPE_BIND_LIMIT0, val);
  841. val = MTK_PPE_BIND_LIMIT1_FULL |
  842. FIELD_PREP(MTK_PPE_BIND_LIMIT1_NON_L4, 1);
  843. ppe_w32(ppe, MTK_PPE_BIND_LIMIT1, val);
  844. val = FIELD_PREP(MTK_PPE_BIND_RATE_BIND, 30) |
  845. FIELD_PREP(MTK_PPE_BIND_RATE_PREBIND, 1);
  846. ppe_w32(ppe, MTK_PPE_BIND_RATE, val);
  847. /* enable PPE */
  848. val = MTK_PPE_GLO_CFG_EN |
  849. MTK_PPE_GLO_CFG_IP4_L4_CS_DROP |
  850. MTK_PPE_GLO_CFG_IP4_CS_DROP |
  851. MTK_PPE_GLO_CFG_FLOW_DROP_UPDATE;
  852. ppe_w32(ppe, MTK_PPE_GLO_CFG, val);
  853. ppe_w32(ppe, MTK_PPE_DEFAULT_CPU_PORT, 0);
  854. if (mtk_is_netsys_v2_or_greater(ppe->eth)) {
  855. ppe_w32(ppe, MTK_PPE_DEFAULT_CPU_PORT1, 0xcb777);
  856. ppe_w32(ppe, MTK_PPE_SBW_CTRL, 0x7f);
  857. }
  858. if (ppe->accounting && ppe->mib_phys) {
  859. ppe_w32(ppe, MTK_PPE_MIB_TB_BASE, ppe->mib_phys);
  860. ppe_m32(ppe, MTK_PPE_MIB_CFG, MTK_PPE_MIB_CFG_EN,
  861. MTK_PPE_MIB_CFG_EN);
  862. ppe_m32(ppe, MTK_PPE_MIB_CFG, MTK_PPE_MIB_CFG_RD_CLR,
  863. MTK_PPE_MIB_CFG_RD_CLR);
  864. ppe_m32(ppe, MTK_PPE_MIB_CACHE_CTL, MTK_PPE_MIB_CACHE_CTL_EN,
  865. MTK_PPE_MIB_CFG_RD_CLR);
  866. }
  867. }
  868. int mtk_ppe_stop(struct mtk_ppe *ppe)
  869. {
  870. u32 val;
  871. int i;
  872. if (!ppe)
  873. return 0;
  874. for (i = 0; i < MTK_PPE_ENTRIES; i++) {
  875. struct mtk_foe_entry *hwe = mtk_foe_get_entry(ppe, i);
  876. hwe->ib1 = FIELD_PREP(MTK_FOE_IB1_STATE,
  877. MTK_FOE_STATE_INVALID);
  878. }
  879. mtk_ppe_cache_enable(ppe, false);
  880. /* disable aging */
  881. val = MTK_PPE_TB_CFG_AGE_NON_L4 |
  882. MTK_PPE_TB_CFG_AGE_UNBIND |
  883. MTK_PPE_TB_CFG_AGE_TCP |
  884. MTK_PPE_TB_CFG_AGE_UDP |
  885. MTK_PPE_TB_CFG_AGE_TCP_FIN |
  886. MTK_PPE_TB_CFG_SCAN_MODE;
  887. ppe_clear(ppe, MTK_PPE_TB_CFG, val);
  888. if (mtk_ppe_wait_busy(ppe))
  889. return -ETIMEDOUT;
  890. /* disable offload engine */
  891. ppe_clear(ppe, MTK_PPE_GLO_CFG, MTK_PPE_GLO_CFG_EN);
  892. ppe_w32(ppe, MTK_PPE_FLOW_CFG, 0);
  893. return 0;
  894. }