sja1105_flower.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright 2020 NXP
  3. */
  4. #include "sja1105.h"
  5. #include "sja1105_vl.h"
  6. struct sja1105_rule *sja1105_rule_find(struct sja1105_private *priv,
  7. unsigned long cookie)
  8. {
  9. struct sja1105_rule *rule;
  10. list_for_each_entry(rule, &priv->flow_block.rules, list)
  11. if (rule->cookie == cookie)
  12. return rule;
  13. return NULL;
  14. }
  15. static int sja1105_find_free_l2_policer(struct sja1105_private *priv)
  16. {
  17. int i;
  18. for (i = 0; i < SJA1105_NUM_L2_POLICERS; i++)
  19. if (!priv->flow_block.l2_policer_used[i])
  20. return i;
  21. return -1;
  22. }
  23. static int sja1105_setup_bcast_policer(struct sja1105_private *priv,
  24. struct netlink_ext_ack *extack,
  25. unsigned long cookie, int port,
  26. u64 rate_bytes_per_sec,
  27. u32 burst)
  28. {
  29. struct sja1105_rule *rule = sja1105_rule_find(priv, cookie);
  30. struct sja1105_l2_policing_entry *policing;
  31. struct dsa_switch *ds = priv->ds;
  32. bool new_rule = false;
  33. unsigned long p;
  34. int rc;
  35. if (!rule) {
  36. rule = kzalloc(sizeof(*rule), GFP_KERNEL);
  37. if (!rule)
  38. return -ENOMEM;
  39. rule->cookie = cookie;
  40. rule->type = SJA1105_RULE_BCAST_POLICER;
  41. rule->bcast_pol.sharindx = sja1105_find_free_l2_policer(priv);
  42. rule->key.type = SJA1105_KEY_BCAST;
  43. new_rule = true;
  44. }
  45. if (rule->bcast_pol.sharindx == -1) {
  46. NL_SET_ERR_MSG_MOD(extack, "No more L2 policers free");
  47. rc = -ENOSPC;
  48. goto out;
  49. }
  50. policing = priv->static_config.tables[BLK_IDX_L2_POLICING].entries;
  51. if (policing[(ds->num_ports * SJA1105_NUM_TC) + port].sharindx != port) {
  52. NL_SET_ERR_MSG_MOD(extack,
  53. "Port already has a broadcast policer");
  54. rc = -EEXIST;
  55. goto out;
  56. }
  57. rule->port_mask |= BIT(port);
  58. /* Make the broadcast policers of all ports attached to this block
  59. * point to the newly allocated policer
  60. */
  61. for_each_set_bit(p, &rule->port_mask, SJA1105_MAX_NUM_PORTS) {
  62. int bcast = (ds->num_ports * SJA1105_NUM_TC) + p;
  63. policing[bcast].sharindx = rule->bcast_pol.sharindx;
  64. }
  65. policing[rule->bcast_pol.sharindx].rate = div_u64(rate_bytes_per_sec *
  66. 512, 1000000);
  67. policing[rule->bcast_pol.sharindx].smax = burst;
  68. /* TODO: support per-flow MTU */
  69. policing[rule->bcast_pol.sharindx].maxlen = VLAN_ETH_FRAME_LEN +
  70. ETH_FCS_LEN;
  71. rc = sja1105_static_config_reload(priv, SJA1105_BEST_EFFORT_POLICING);
  72. out:
  73. if (rc == 0 && new_rule) {
  74. priv->flow_block.l2_policer_used[rule->bcast_pol.sharindx] = true;
  75. list_add(&rule->list, &priv->flow_block.rules);
  76. } else if (new_rule) {
  77. kfree(rule);
  78. }
  79. return rc;
  80. }
  81. static int sja1105_setup_tc_policer(struct sja1105_private *priv,
  82. struct netlink_ext_ack *extack,
  83. unsigned long cookie, int port, int tc,
  84. u64 rate_bytes_per_sec,
  85. u32 burst)
  86. {
  87. struct sja1105_rule *rule = sja1105_rule_find(priv, cookie);
  88. struct sja1105_l2_policing_entry *policing;
  89. bool new_rule = false;
  90. unsigned long p;
  91. int rc;
  92. if (!rule) {
  93. rule = kzalloc(sizeof(*rule), GFP_KERNEL);
  94. if (!rule)
  95. return -ENOMEM;
  96. rule->cookie = cookie;
  97. rule->type = SJA1105_RULE_TC_POLICER;
  98. rule->tc_pol.sharindx = sja1105_find_free_l2_policer(priv);
  99. rule->key.type = SJA1105_KEY_TC;
  100. rule->key.tc.pcp = tc;
  101. new_rule = true;
  102. }
  103. if (rule->tc_pol.sharindx == -1) {
  104. NL_SET_ERR_MSG_MOD(extack, "No more L2 policers free");
  105. rc = -ENOSPC;
  106. goto out;
  107. }
  108. policing = priv->static_config.tables[BLK_IDX_L2_POLICING].entries;
  109. if (policing[(port * SJA1105_NUM_TC) + tc].sharindx != port) {
  110. NL_SET_ERR_MSG_MOD(extack,
  111. "Port-TC pair already has an L2 policer");
  112. rc = -EEXIST;
  113. goto out;
  114. }
  115. rule->port_mask |= BIT(port);
  116. /* Make the policers for traffic class @tc of all ports attached to
  117. * this block point to the newly allocated policer
  118. */
  119. for_each_set_bit(p, &rule->port_mask, SJA1105_MAX_NUM_PORTS) {
  120. int index = (p * SJA1105_NUM_TC) + tc;
  121. policing[index].sharindx = rule->tc_pol.sharindx;
  122. }
  123. policing[rule->tc_pol.sharindx].rate = div_u64(rate_bytes_per_sec *
  124. 512, 1000000);
  125. policing[rule->tc_pol.sharindx].smax = burst;
  126. /* TODO: support per-flow MTU */
  127. policing[rule->tc_pol.sharindx].maxlen = VLAN_ETH_FRAME_LEN +
  128. ETH_FCS_LEN;
  129. rc = sja1105_static_config_reload(priv, SJA1105_BEST_EFFORT_POLICING);
  130. out:
  131. if (rc == 0 && new_rule) {
  132. priv->flow_block.l2_policer_used[rule->tc_pol.sharindx] = true;
  133. list_add(&rule->list, &priv->flow_block.rules);
  134. } else if (new_rule) {
  135. kfree(rule);
  136. }
  137. return rc;
  138. }
  139. static int sja1105_flower_policer(struct sja1105_private *priv, int port,
  140. struct netlink_ext_ack *extack,
  141. unsigned long cookie,
  142. struct sja1105_key *key,
  143. u64 rate_bytes_per_sec,
  144. u32 burst)
  145. {
  146. switch (key->type) {
  147. case SJA1105_KEY_BCAST:
  148. return sja1105_setup_bcast_policer(priv, extack, cookie, port,
  149. rate_bytes_per_sec, burst);
  150. case SJA1105_KEY_TC:
  151. return sja1105_setup_tc_policer(priv, extack, cookie, port,
  152. key->tc.pcp, rate_bytes_per_sec,
  153. burst);
  154. default:
  155. NL_SET_ERR_MSG_MOD(extack, "Unknown keys for policing");
  156. return -EOPNOTSUPP;
  157. }
  158. }
  159. static int sja1105_flower_parse_key(struct sja1105_private *priv,
  160. struct netlink_ext_ack *extack,
  161. struct flow_cls_offload *cls,
  162. struct sja1105_key *key)
  163. {
  164. struct flow_rule *rule = flow_cls_offload_flow_rule(cls);
  165. struct flow_dissector *dissector = rule->match.dissector;
  166. bool is_bcast_dmac = false;
  167. u64 dmac = U64_MAX;
  168. u16 vid = U16_MAX;
  169. u16 pcp = U16_MAX;
  170. if (dissector->used_keys &
  171. ~(BIT_ULL(FLOW_DISSECTOR_KEY_BASIC) |
  172. BIT_ULL(FLOW_DISSECTOR_KEY_CONTROL) |
  173. BIT_ULL(FLOW_DISSECTOR_KEY_VLAN) |
  174. BIT_ULL(FLOW_DISSECTOR_KEY_ETH_ADDRS))) {
  175. NL_SET_ERR_MSG_MOD(extack,
  176. "Unsupported keys used");
  177. return -EOPNOTSUPP;
  178. }
  179. if (flow_rule_match_has_control_flags(rule, extack))
  180. return -EOPNOTSUPP;
  181. if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_BASIC)) {
  182. struct flow_match_basic match;
  183. flow_rule_match_basic(rule, &match);
  184. if (match.key->n_proto) {
  185. NL_SET_ERR_MSG_MOD(extack,
  186. "Matching on protocol not supported");
  187. return -EOPNOTSUPP;
  188. }
  189. }
  190. if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
  191. u8 bcast[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
  192. u8 null[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
  193. struct flow_match_eth_addrs match;
  194. flow_rule_match_eth_addrs(rule, &match);
  195. if (!ether_addr_equal_masked(match.key->src, null,
  196. match.mask->src)) {
  197. NL_SET_ERR_MSG_MOD(extack,
  198. "Matching on source MAC not supported");
  199. return -EOPNOTSUPP;
  200. }
  201. if (!ether_addr_equal(match.mask->dst, bcast)) {
  202. NL_SET_ERR_MSG_MOD(extack,
  203. "Masked matching on MAC not supported");
  204. return -EOPNOTSUPP;
  205. }
  206. dmac = ether_addr_to_u64(match.key->dst);
  207. is_bcast_dmac = ether_addr_equal(match.key->dst, bcast);
  208. }
  209. if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_VLAN)) {
  210. struct flow_match_vlan match;
  211. flow_rule_match_vlan(rule, &match);
  212. if (match.mask->vlan_id &&
  213. match.mask->vlan_id != VLAN_VID_MASK) {
  214. NL_SET_ERR_MSG_MOD(extack,
  215. "Masked matching on VID is not supported");
  216. return -EOPNOTSUPP;
  217. }
  218. if (match.mask->vlan_priority &&
  219. match.mask->vlan_priority != 0x7) {
  220. NL_SET_ERR_MSG_MOD(extack,
  221. "Masked matching on PCP is not supported");
  222. return -EOPNOTSUPP;
  223. }
  224. if (match.mask->vlan_id)
  225. vid = match.key->vlan_id;
  226. if (match.mask->vlan_priority)
  227. pcp = match.key->vlan_priority;
  228. }
  229. if (is_bcast_dmac && vid == U16_MAX && pcp == U16_MAX) {
  230. key->type = SJA1105_KEY_BCAST;
  231. return 0;
  232. }
  233. if (dmac == U64_MAX && vid == U16_MAX && pcp != U16_MAX) {
  234. key->type = SJA1105_KEY_TC;
  235. key->tc.pcp = pcp;
  236. return 0;
  237. }
  238. if (dmac != U64_MAX && vid != U16_MAX && pcp != U16_MAX) {
  239. key->type = SJA1105_KEY_VLAN_AWARE_VL;
  240. key->vl.dmac = dmac;
  241. key->vl.vid = vid;
  242. key->vl.pcp = pcp;
  243. return 0;
  244. }
  245. if (dmac != U64_MAX) {
  246. key->type = SJA1105_KEY_VLAN_UNAWARE_VL;
  247. key->vl.dmac = dmac;
  248. return 0;
  249. }
  250. NL_SET_ERR_MSG_MOD(extack, "Not matching on any known key");
  251. return -EOPNOTSUPP;
  252. }
  253. static int sja1105_policer_validate(const struct flow_action *action,
  254. const struct flow_action_entry *act,
  255. struct netlink_ext_ack *extack)
  256. {
  257. if (act->police.exceed.act_id != FLOW_ACTION_DROP) {
  258. NL_SET_ERR_MSG_MOD(extack,
  259. "Offload not supported when exceed action is not drop");
  260. return -EOPNOTSUPP;
  261. }
  262. if (act->police.notexceed.act_id != FLOW_ACTION_PIPE &&
  263. act->police.notexceed.act_id != FLOW_ACTION_ACCEPT) {
  264. NL_SET_ERR_MSG_MOD(extack,
  265. "Offload not supported when conform action is not pipe or ok");
  266. return -EOPNOTSUPP;
  267. }
  268. if (act->police.notexceed.act_id == FLOW_ACTION_ACCEPT &&
  269. !flow_action_is_last_entry(action, act)) {
  270. NL_SET_ERR_MSG_MOD(extack,
  271. "Offload not supported when conform action is ok, but action is not last");
  272. return -EOPNOTSUPP;
  273. }
  274. if (act->police.peakrate_bytes_ps ||
  275. act->police.avrate || act->police.overhead) {
  276. NL_SET_ERR_MSG_MOD(extack,
  277. "Offload not supported when peakrate/avrate/overhead is configured");
  278. return -EOPNOTSUPP;
  279. }
  280. if (act->police.rate_pkt_ps) {
  281. NL_SET_ERR_MSG_MOD(extack,
  282. "QoS offload not support packets per second");
  283. return -EOPNOTSUPP;
  284. }
  285. return 0;
  286. }
  287. int sja1105_cls_flower_add(struct dsa_switch *ds, int port,
  288. struct flow_cls_offload *cls, bool ingress)
  289. {
  290. struct flow_rule *rule = flow_cls_offload_flow_rule(cls);
  291. struct netlink_ext_ack *extack = cls->common.extack;
  292. struct sja1105_private *priv = ds->priv;
  293. const struct flow_action_entry *act;
  294. unsigned long cookie = cls->cookie;
  295. bool routing_rule = false;
  296. struct sja1105_key key;
  297. bool gate_rule = false;
  298. bool vl_rule = false;
  299. int rc, i;
  300. rc = sja1105_flower_parse_key(priv, extack, cls, &key);
  301. if (rc)
  302. return rc;
  303. flow_action_for_each(i, act, &rule->action) {
  304. switch (act->id) {
  305. case FLOW_ACTION_POLICE:
  306. rc = sja1105_policer_validate(&rule->action, act, extack);
  307. if (rc)
  308. goto out;
  309. rc = sja1105_flower_policer(priv, port, extack, cookie,
  310. &key,
  311. act->police.rate_bytes_ps,
  312. act->police.burst);
  313. if (rc)
  314. goto out;
  315. break;
  316. case FLOW_ACTION_TRAP: {
  317. int cpu = dsa_upstream_port(ds, port);
  318. routing_rule = true;
  319. vl_rule = true;
  320. rc = sja1105_vl_redirect(priv, port, extack, cookie,
  321. &key, BIT(cpu), true);
  322. if (rc)
  323. goto out;
  324. break;
  325. }
  326. case FLOW_ACTION_REDIRECT: {
  327. struct dsa_port *to_dp;
  328. to_dp = dsa_port_from_netdev(act->dev);
  329. if (IS_ERR(to_dp)) {
  330. NL_SET_ERR_MSG_MOD(extack,
  331. "Destination not a switch port");
  332. return -EOPNOTSUPP;
  333. }
  334. routing_rule = true;
  335. vl_rule = true;
  336. rc = sja1105_vl_redirect(priv, port, extack, cookie,
  337. &key, BIT(to_dp->index), true);
  338. if (rc)
  339. goto out;
  340. break;
  341. }
  342. case FLOW_ACTION_DROP:
  343. vl_rule = true;
  344. rc = sja1105_vl_redirect(priv, port, extack, cookie,
  345. &key, 0, false);
  346. if (rc)
  347. goto out;
  348. break;
  349. case FLOW_ACTION_GATE:
  350. gate_rule = true;
  351. vl_rule = true;
  352. rc = sja1105_vl_gate(priv, port, extack, cookie,
  353. &key, act->hw_index,
  354. act->gate.prio,
  355. act->gate.basetime,
  356. act->gate.cycletime,
  357. act->gate.cycletimeext,
  358. act->gate.num_entries,
  359. act->gate.entries);
  360. if (rc)
  361. goto out;
  362. break;
  363. default:
  364. NL_SET_ERR_MSG_MOD(extack,
  365. "Action not supported");
  366. rc = -EOPNOTSUPP;
  367. goto out;
  368. }
  369. }
  370. if (vl_rule && !rc) {
  371. /* Delay scheduling configuration until DESTPORTS has been
  372. * populated by all other actions.
  373. */
  374. if (gate_rule) {
  375. if (!routing_rule) {
  376. NL_SET_ERR_MSG_MOD(extack,
  377. "Can only offload gate action together with redirect or trap");
  378. return -EOPNOTSUPP;
  379. }
  380. rc = sja1105_init_scheduling(priv);
  381. if (rc)
  382. goto out;
  383. }
  384. rc = sja1105_static_config_reload(priv, SJA1105_VIRTUAL_LINKS);
  385. }
  386. out:
  387. return rc;
  388. }
  389. int sja1105_cls_flower_del(struct dsa_switch *ds, int port,
  390. struct flow_cls_offload *cls, bool ingress)
  391. {
  392. struct sja1105_private *priv = ds->priv;
  393. struct sja1105_rule *rule = sja1105_rule_find(priv, cls->cookie);
  394. struct sja1105_l2_policing_entry *policing;
  395. int old_sharindx;
  396. if (!rule)
  397. return 0;
  398. if (rule->type == SJA1105_RULE_VL)
  399. return sja1105_vl_delete(priv, port, rule, cls->common.extack);
  400. policing = priv->static_config.tables[BLK_IDX_L2_POLICING].entries;
  401. if (rule->type == SJA1105_RULE_BCAST_POLICER) {
  402. int bcast = (ds->num_ports * SJA1105_NUM_TC) + port;
  403. old_sharindx = policing[bcast].sharindx;
  404. policing[bcast].sharindx = port;
  405. } else if (rule->type == SJA1105_RULE_TC_POLICER) {
  406. int index = (port * SJA1105_NUM_TC) + rule->key.tc.pcp;
  407. old_sharindx = policing[index].sharindx;
  408. policing[index].sharindx = port;
  409. } else {
  410. return -EINVAL;
  411. }
  412. rule->port_mask &= ~BIT(port);
  413. if (!rule->port_mask) {
  414. priv->flow_block.l2_policer_used[old_sharindx] = false;
  415. list_del(&rule->list);
  416. kfree(rule);
  417. }
  418. return sja1105_static_config_reload(priv, SJA1105_BEST_EFFORT_POLICING);
  419. }
  420. int sja1105_cls_flower_stats(struct dsa_switch *ds, int port,
  421. struct flow_cls_offload *cls, bool ingress)
  422. {
  423. struct sja1105_private *priv = ds->priv;
  424. struct sja1105_rule *rule = sja1105_rule_find(priv, cls->cookie);
  425. int rc;
  426. if (!rule)
  427. return 0;
  428. if (rule->type != SJA1105_RULE_VL)
  429. return 0;
  430. rc = sja1105_vl_stats(priv, port, rule, &cls->stats,
  431. cls->common.extack);
  432. if (rc)
  433. return rc;
  434. return 0;
  435. }
  436. void sja1105_flower_setup(struct dsa_switch *ds)
  437. {
  438. struct sja1105_private *priv = ds->priv;
  439. int port;
  440. INIT_LIST_HEAD(&priv->flow_block.rules);
  441. for (port = 0; port < ds->num_ports; port++)
  442. priv->flow_block.l2_policer_used[port] = true;
  443. }
  444. void sja1105_flower_teardown(struct dsa_switch *ds)
  445. {
  446. struct sja1105_private *priv = ds->priv;
  447. struct sja1105_rule *rule;
  448. struct list_head *pos, *n;
  449. list_for_each_safe(pos, n, &priv->flow_block.rules) {
  450. rule = list_entry(pos, struct sja1105_rule, list);
  451. list_del(&rule->list);
  452. kfree(rule);
  453. }
  454. }