702-v5.19-08-net-ethernet-mtk_eth_soc-rework-hardware-flow-table-.patch 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. From: Felix Fietkau <[email protected]>
  2. Date: Mon, 21 Feb 2022 15:39:18 +0100
  3. Subject: [PATCH] net: ethernet: mtk_eth_soc: rework hardware flow table
  4. management
  5. The hardware was designed to handle flow detection and creation of flow entries
  6. by itself, relying on the software primarily for filling in egress routing
  7. information.
  8. When there is a hash collision between multiple flows, this allows the hardware
  9. to maintain the entry for the most active flow.
  10. Additionally, the hardware only keeps offloading active for entries with at
  11. least 30 packets per second.
  12. With this rework, the code no longer creates a hardware entries directly.
  13. Instead, the hardware entry is only created when the PPE reports a matching
  14. unbound flow with the minimum target rate.
  15. In order to reduce CPU overhead, looking for flows belonging to a hash entry
  16. is rate limited to once every 100ms.
  17. This rework is also used as preparation for emulating bridge offload by
  18. managing L4 offload entries on demand.
  19. Signed-off-by: Felix Fietkau <[email protected]>
  20. ---
  21. --- a/drivers/net/ethernet/mediatek/mtk_eth_soc.c
  22. +++ b/drivers/net/ethernet/mediatek/mtk_eth_soc.c
  23. @@ -21,6 +21,7 @@
  24. #include <linux/pinctrl/devinfo.h>
  25. #include <linux/phylink.h>
  26. #include <linux/jhash.h>
  27. +#include <linux/bitfield.h>
  28. #include <net/dsa.h>
  29. #include "mtk_eth_soc.h"
  30. @@ -1293,7 +1294,7 @@ static int mtk_poll_rx(struct napi_struc
  31. struct net_device *netdev;
  32. unsigned int pktlen;
  33. dma_addr_t dma_addr;
  34. - u32 hash;
  35. + u32 hash, reason;
  36. int mac;
  37. ring = mtk_get_rx_ring(eth);
  38. @@ -1372,6 +1373,11 @@ static int mtk_poll_rx(struct napi_struc
  39. skb_set_hash(skb, hash, PKT_HASH_TYPE_L4);
  40. }
  41. + reason = FIELD_GET(MTK_RXD4_PPE_CPU_REASON, trxd.rxd4);
  42. + if (reason == MTK_PPE_CPU_REASON_HIT_UNBIND_RATE_REACHED)
  43. + mtk_ppe_check_skb(eth->ppe, skb,
  44. + trxd.rxd4 & MTK_RXD4_FOE_ENTRY);
  45. +
  46. if (netdev->features & NETIF_F_HW_VLAN_CTAG_RX &&
  47. (trxd.rxd2 & RX_DMA_VTAG))
  48. __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
  49. @@ -3301,7 +3307,7 @@ static int mtk_probe(struct platform_dev
  50. }
  51. if (eth->soc->offload_version) {
  52. - eth->ppe = mtk_ppe_init(eth->dev, eth->base + MTK_ETH_PPE_BASE, 2);
  53. + eth->ppe = mtk_ppe_init(eth, eth->base + MTK_ETH_PPE_BASE, 2);
  54. if (!eth->ppe) {
  55. err = -ENOMEM;
  56. goto err_free_dev;
  57. --- a/drivers/net/ethernet/mediatek/mtk_ppe.c
  58. +++ b/drivers/net/ethernet/mediatek/mtk_ppe.c
  59. @@ -6,9 +6,12 @@
  60. #include <linux/iopoll.h>
  61. #include <linux/etherdevice.h>
  62. #include <linux/platform_device.h>
  63. +#include "mtk_eth_soc.h"
  64. #include "mtk_ppe.h"
  65. #include "mtk_ppe_regs.h"
  66. +static DEFINE_SPINLOCK(ppe_lock);
  67. +
  68. static void ppe_w32(struct mtk_ppe *ppe, u32 reg, u32 val)
  69. {
  70. writel(val, ppe->base + reg);
  71. @@ -41,6 +44,11 @@ static u32 ppe_clear(struct mtk_ppe *ppe
  72. return ppe_m32(ppe, reg, val, 0);
  73. }
  74. +static u32 mtk_eth_timestamp(struct mtk_eth *eth)
  75. +{
  76. + return mtk_r32(eth, 0x0010) & MTK_FOE_IB1_BIND_TIMESTAMP;
  77. +}
  78. +
  79. static int mtk_ppe_wait_busy(struct mtk_ppe *ppe)
  80. {
  81. int ret;
  82. @@ -353,26 +361,59 @@ static inline bool mtk_foe_entry_usable(
  83. FIELD_GET(MTK_FOE_IB1_STATE, entry->ib1) != MTK_FOE_STATE_BIND;
  84. }
  85. -int mtk_foe_entry_commit(struct mtk_ppe *ppe, struct mtk_foe_entry *entry,
  86. - u16 timestamp)
  87. +static bool
  88. +mtk_flow_entry_match(struct mtk_flow_entry *entry, struct mtk_foe_entry *data)
  89. +{
  90. + int type, len;
  91. +
  92. + if ((data->ib1 ^ entry->data.ib1) & MTK_FOE_IB1_UDP)
  93. + return false;
  94. +
  95. + type = FIELD_GET(MTK_FOE_IB1_PACKET_TYPE, entry->data.ib1);
  96. + if (type > MTK_PPE_PKT_TYPE_IPV4_DSLITE)
  97. + len = offsetof(struct mtk_foe_entry, ipv6._rsv);
  98. + else
  99. + len = offsetof(struct mtk_foe_entry, ipv4.ib2);
  100. +
  101. + return !memcmp(&entry->data.data, &data->data, len - 4);
  102. +}
  103. +
  104. +static void
  105. +mtk_flow_entry_update(struct mtk_ppe *ppe, struct mtk_flow_entry *entry)
  106. {
  107. struct mtk_foe_entry *hwe;
  108. - u32 hash;
  109. + struct mtk_foe_entry foe;
  110. + spin_lock_bh(&ppe_lock);
  111. + if (entry->hash == 0xffff)
  112. + goto out;
  113. +
  114. + hwe = &ppe->foe_table[entry->hash];
  115. + memcpy(&foe, hwe, sizeof(foe));
  116. + if (!mtk_flow_entry_match(entry, &foe)) {
  117. + entry->hash = 0xffff;
  118. + goto out;
  119. + }
  120. +
  121. + entry->data.ib1 = foe.ib1;
  122. +
  123. +out:
  124. + spin_unlock_bh(&ppe_lock);
  125. +}
  126. +
  127. +static void
  128. +__mtk_foe_entry_commit(struct mtk_ppe *ppe, struct mtk_foe_entry *entry,
  129. + u16 hash)
  130. +{
  131. + struct mtk_foe_entry *hwe;
  132. + u16 timestamp;
  133. +
  134. + timestamp = mtk_eth_timestamp(ppe->eth);
  135. timestamp &= MTK_FOE_IB1_BIND_TIMESTAMP;
  136. entry->ib1 &= ~MTK_FOE_IB1_BIND_TIMESTAMP;
  137. entry->ib1 |= FIELD_PREP(MTK_FOE_IB1_BIND_TIMESTAMP, timestamp);
  138. - hash = mtk_ppe_hash_entry(entry);
  139. hwe = &ppe->foe_table[hash];
  140. - if (!mtk_foe_entry_usable(hwe)) {
  141. - hwe++;
  142. - hash++;
  143. -
  144. - if (!mtk_foe_entry_usable(hwe))
  145. - return -ENOSPC;
  146. - }
  147. -
  148. memcpy(&hwe->data, &entry->data, sizeof(hwe->data));
  149. wmb();
  150. hwe->ib1 = entry->ib1;
  151. @@ -380,13 +421,77 @@ int mtk_foe_entry_commit(struct mtk_ppe
  152. dma_wmb();
  153. mtk_ppe_cache_clear(ppe);
  154. +}
  155. - return hash;
  156. +void mtk_foe_entry_clear(struct mtk_ppe *ppe, struct mtk_flow_entry *entry)
  157. +{
  158. + spin_lock_bh(&ppe_lock);
  159. + hlist_del_init(&entry->list);
  160. + if (entry->hash != 0xffff) {
  161. + ppe->foe_table[entry->hash].ib1 &= ~MTK_FOE_IB1_STATE;
  162. + ppe->foe_table[entry->hash].ib1 |= FIELD_PREP(MTK_FOE_IB1_STATE,
  163. + MTK_FOE_STATE_BIND);
  164. + dma_wmb();
  165. + }
  166. + entry->hash = 0xffff;
  167. + spin_unlock_bh(&ppe_lock);
  168. +}
  169. +
  170. +int mtk_foe_entry_commit(struct mtk_ppe *ppe, struct mtk_flow_entry *entry)
  171. +{
  172. + u32 hash = mtk_ppe_hash_entry(&entry->data);
  173. +
  174. + entry->hash = 0xffff;
  175. + spin_lock_bh(&ppe_lock);
  176. + hlist_add_head(&entry->list, &ppe->foe_flow[hash / 2]);
  177. + spin_unlock_bh(&ppe_lock);
  178. +
  179. + return 0;
  180. +}
  181. +
  182. +void __mtk_ppe_check_skb(struct mtk_ppe *ppe, struct sk_buff *skb, u16 hash)
  183. +{
  184. + struct hlist_head *head = &ppe->foe_flow[hash / 2];
  185. + struct mtk_flow_entry *entry;
  186. + struct mtk_foe_entry *hwe = &ppe->foe_table[hash];
  187. + bool found = false;
  188. +
  189. + if (hlist_empty(head))
  190. + return;
  191. +
  192. + spin_lock_bh(&ppe_lock);
  193. + hlist_for_each_entry(entry, head, list) {
  194. + if (found || !mtk_flow_entry_match(entry, hwe)) {
  195. + if (entry->hash != 0xffff)
  196. + entry->hash = 0xffff;
  197. + continue;
  198. + }
  199. +
  200. + entry->hash = hash;
  201. + __mtk_foe_entry_commit(ppe, &entry->data, hash);
  202. + found = true;
  203. + }
  204. + spin_unlock_bh(&ppe_lock);
  205. +}
  206. +
  207. +int mtk_foe_entry_idle_time(struct mtk_ppe *ppe, struct mtk_flow_entry *entry)
  208. +{
  209. + u16 now = mtk_eth_timestamp(ppe->eth) & MTK_FOE_IB1_BIND_TIMESTAMP;
  210. + u16 timestamp;
  211. +
  212. + mtk_flow_entry_update(ppe, entry);
  213. + timestamp = entry->data.ib1 & MTK_FOE_IB1_BIND_TIMESTAMP;
  214. +
  215. + if (timestamp > now)
  216. + return MTK_FOE_IB1_BIND_TIMESTAMP + 1 - timestamp + now;
  217. + else
  218. + return now - timestamp;
  219. }
  220. -struct mtk_ppe *mtk_ppe_init(struct device *dev, void __iomem *base,
  221. +struct mtk_ppe *mtk_ppe_init(struct mtk_eth *eth, void __iomem *base,
  222. int version)
  223. {
  224. + struct device *dev = eth->dev;
  225. struct mtk_foe_entry *foe;
  226. struct mtk_ppe *ppe;
  227. @@ -398,6 +503,7 @@ struct mtk_ppe *mtk_ppe_init(struct devi
  228. * not coherent.
  229. */
  230. ppe->base = base;
  231. + ppe->eth = eth;
  232. ppe->dev = dev;
  233. ppe->version = version;
  234. --- a/drivers/net/ethernet/mediatek/mtk_ppe.h
  235. +++ b/drivers/net/ethernet/mediatek/mtk_ppe.h
  236. @@ -235,7 +235,17 @@ enum {
  237. MTK_PPE_CPU_REASON_INVALID = 0x1f,
  238. };
  239. +struct mtk_flow_entry {
  240. + struct rhash_head node;
  241. + struct hlist_node list;
  242. + unsigned long cookie;
  243. + struct mtk_foe_entry data;
  244. + u16 hash;
  245. + s8 wed_index;
  246. +};
  247. +
  248. struct mtk_ppe {
  249. + struct mtk_eth *eth;
  250. struct device *dev;
  251. void __iomem *base;
  252. int version;
  253. @@ -243,18 +253,33 @@ struct mtk_ppe {
  254. struct mtk_foe_entry *foe_table;
  255. dma_addr_t foe_phys;
  256. + u16 foe_check_time[MTK_PPE_ENTRIES];
  257. + struct hlist_head foe_flow[MTK_PPE_ENTRIES / 2];
  258. +
  259. void *acct_table;
  260. };
  261. -struct mtk_ppe *mtk_ppe_init(struct device *dev, void __iomem *base, int version);
  262. +struct mtk_ppe *mtk_ppe_init(struct mtk_eth *eth, void __iomem *base, int version);
  263. int mtk_ppe_start(struct mtk_ppe *ppe);
  264. int mtk_ppe_stop(struct mtk_ppe *ppe);
  265. +void __mtk_ppe_check_skb(struct mtk_ppe *ppe, struct sk_buff *skb, u16 hash);
  266. +
  267. static inline void
  268. -mtk_foe_entry_clear(struct mtk_ppe *ppe, u16 hash)
  269. +mtk_ppe_check_skb(struct mtk_ppe *ppe, struct sk_buff *skb, u16 hash)
  270. {
  271. - ppe->foe_table[hash].ib1 = 0;
  272. - dma_wmb();
  273. + u16 now, diff;
  274. +
  275. + if (!ppe)
  276. + return;
  277. +
  278. + now = (u16)jiffies;
  279. + diff = now - ppe->foe_check_time[hash];
  280. + if (diff < HZ / 10)
  281. + return;
  282. +
  283. + ppe->foe_check_time[hash] = now;
  284. + __mtk_ppe_check_skb(ppe, skb, hash);
  285. }
  286. static inline int
  287. @@ -282,8 +307,9 @@ int mtk_foe_entry_set_vlan(struct mtk_fo
  288. int mtk_foe_entry_set_pppoe(struct mtk_foe_entry *entry, int sid);
  289. int mtk_foe_entry_set_wdma(struct mtk_foe_entry *entry, int wdma_idx, int txq,
  290. int bss, int wcid);
  291. -int mtk_foe_entry_commit(struct mtk_ppe *ppe, struct mtk_foe_entry *entry,
  292. - u16 timestamp);
  293. +int mtk_foe_entry_commit(struct mtk_ppe *ppe, struct mtk_flow_entry *entry);
  294. +void mtk_foe_entry_clear(struct mtk_ppe *ppe, struct mtk_flow_entry *entry);
  295. +int mtk_foe_entry_idle_time(struct mtk_ppe *ppe, struct mtk_flow_entry *entry);
  296. int mtk_ppe_debugfs_init(struct mtk_ppe *ppe);
  297. #endif
  298. --- a/drivers/net/ethernet/mediatek/mtk_ppe_offload.c
  299. +++ b/drivers/net/ethernet/mediatek/mtk_ppe_offload.c
  300. @@ -42,13 +42,6 @@ struct mtk_flow_data {
  301. } pppoe;
  302. };
  303. -struct mtk_flow_entry {
  304. - struct rhash_head node;
  305. - unsigned long cookie;
  306. - u16 hash;
  307. - s8 wed_index;
  308. -};
  309. -
  310. static const struct rhashtable_params mtk_flow_ht_params = {
  311. .head_offset = offsetof(struct mtk_flow_entry, node),
  312. .key_offset = offsetof(struct mtk_flow_entry, cookie),
  313. @@ -56,12 +49,6 @@ static const struct rhashtable_params mt
  314. .automatic_shrinking = true,
  315. };
  316. -static u32
  317. -mtk_eth_timestamp(struct mtk_eth *eth)
  318. -{
  319. - return mtk_r32(eth, 0x0010) & MTK_FOE_IB1_BIND_TIMESTAMP;
  320. -}
  321. -
  322. static int
  323. mtk_flow_set_ipv4_addr(struct mtk_foe_entry *foe, struct mtk_flow_data *data,
  324. bool egress)
  325. @@ -237,10 +224,8 @@ mtk_flow_offload_replace(struct mtk_eth
  326. int offload_type = 0;
  327. int wed_index = -1;
  328. u16 addr_type = 0;
  329. - u32 timestamp;
  330. u8 l4proto = 0;
  331. int err = 0;
  332. - int hash;
  333. int i;
  334. if (rhashtable_lookup(&eth->flow_table, &f->cookie, mtk_flow_ht_params))
  335. @@ -410,23 +395,21 @@ mtk_flow_offload_replace(struct mtk_eth
  336. return -ENOMEM;
  337. entry->cookie = f->cookie;
  338. - timestamp = mtk_eth_timestamp(eth);
  339. - hash = mtk_foe_entry_commit(eth->ppe, &foe, timestamp);
  340. - if (hash < 0) {
  341. - err = hash;
  342. + memcpy(&entry->data, &foe, sizeof(entry->data));
  343. + entry->wed_index = wed_index;
  344. +
  345. + if (mtk_foe_entry_commit(eth->ppe, entry) < 0)
  346. goto free;
  347. - }
  348. - entry->hash = hash;
  349. - entry->wed_index = wed_index;
  350. err = rhashtable_insert_fast(&eth->flow_table, &entry->node,
  351. mtk_flow_ht_params);
  352. if (err < 0)
  353. - goto clear_flow;
  354. + goto clear;
  355. return 0;
  356. -clear_flow:
  357. - mtk_foe_entry_clear(eth->ppe, hash);
  358. +
  359. +clear:
  360. + mtk_foe_entry_clear(eth->ppe, entry);
  361. free:
  362. kfree(entry);
  363. if (wed_index >= 0)
  364. @@ -444,7 +427,7 @@ mtk_flow_offload_destroy(struct mtk_eth
  365. if (!entry)
  366. return -ENOENT;
  367. - mtk_foe_entry_clear(eth->ppe, entry->hash);
  368. + mtk_foe_entry_clear(eth->ppe, entry);
  369. rhashtable_remove_fast(&eth->flow_table, &entry->node,
  370. mtk_flow_ht_params);
  371. if (entry->wed_index >= 0)
  372. @@ -458,7 +441,6 @@ static int
  373. mtk_flow_offload_stats(struct mtk_eth *eth, struct flow_cls_offload *f)
  374. {
  375. struct mtk_flow_entry *entry;
  376. - int timestamp;
  377. u32 idle;
  378. entry = rhashtable_lookup(&eth->flow_table, &f->cookie,
  379. @@ -466,11 +448,7 @@ mtk_flow_offload_stats(struct mtk_eth *e
  380. if (!entry)
  381. return -ENOENT;
  382. - timestamp = mtk_foe_entry_timestamp(eth->ppe, entry->hash);
  383. - if (timestamp < 0)
  384. - return -ETIMEDOUT;
  385. -
  386. - idle = mtk_eth_timestamp(eth) - timestamp;
  387. + idle = mtk_foe_entry_idle_time(eth->ppe, entry);
  388. f->stats.lastused = jiffies - idle * HZ;
  389. return 0;