tc.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /****************************************************************************
  3. * Driver for Solarflare network controllers and boards
  4. * Copyright 2019 Solarflare Communications Inc.
  5. * Copyright 2020-2022 Xilinx Inc.
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License version 2 as published
  9. * by the Free Software Foundation, incorporated herein by reference.
  10. */
  11. #include <net/pkt_cls.h>
  12. #include "tc.h"
  13. #include "tc_bindings.h"
  14. #include "mae.h"
  15. #include "ef100_rep.h"
  16. #include "efx.h"
  17. #define EFX_EFV_PF NULL
  18. /* Look up the representor information (efv) for a device.
  19. * May return NULL for the PF (us), or an error pointer for a device that
  20. * isn't supported as a TC offload endpoint
  21. */
  22. static struct efx_rep *efx_tc_flower_lookup_efv(struct efx_nic *efx,
  23. struct net_device *dev)
  24. {
  25. struct efx_rep *efv;
  26. if (!dev)
  27. return ERR_PTR(-EOPNOTSUPP);
  28. /* Is it us (the PF)? */
  29. if (dev == efx->net_dev)
  30. return EFX_EFV_PF;
  31. /* Is it an efx vfrep at all? */
  32. if (dev->netdev_ops != &efx_ef100_rep_netdev_ops)
  33. return ERR_PTR(-EOPNOTSUPP);
  34. /* Is it ours? We don't support TC rules that include another
  35. * EF100's netdevices (not even on another port of the same NIC).
  36. */
  37. efv = netdev_priv(dev);
  38. if (efv->parent != efx)
  39. return ERR_PTR(-EOPNOTSUPP);
  40. return efv;
  41. }
  42. /* Convert a driver-internal vport ID into an external device (wire or VF) */
  43. static s64 efx_tc_flower_external_mport(struct efx_nic *efx, struct efx_rep *efv)
  44. {
  45. u32 mport;
  46. if (IS_ERR(efv))
  47. return PTR_ERR(efv);
  48. if (!efv) /* device is PF (us) */
  49. efx_mae_mport_wire(efx, &mport);
  50. else /* device is repr */
  51. efx_mae_mport_mport(efx, efv->mport, &mport);
  52. return mport;
  53. }
  54. static const struct rhashtable_params efx_tc_match_action_ht_params = {
  55. .key_len = sizeof(unsigned long),
  56. .key_offset = offsetof(struct efx_tc_flow_rule, cookie),
  57. .head_offset = offsetof(struct efx_tc_flow_rule, linkage),
  58. };
  59. static void efx_tc_free_action_set(struct efx_nic *efx,
  60. struct efx_tc_action_set *act, bool in_hw)
  61. {
  62. /* Failure paths calling this on the 'running action' set in_hw=false,
  63. * because if the alloc had succeeded we'd've put it in acts.list and
  64. * not still have it in act.
  65. */
  66. if (in_hw) {
  67. efx_mae_free_action_set(efx, act->fw_id);
  68. /* in_hw is true iff we are on an acts.list; make sure to
  69. * remove ourselves from that list before we are freed.
  70. */
  71. list_del(&act->list);
  72. }
  73. kfree(act);
  74. }
  75. static void efx_tc_free_action_set_list(struct efx_nic *efx,
  76. struct efx_tc_action_set_list *acts,
  77. bool in_hw)
  78. {
  79. struct efx_tc_action_set *act, *next;
  80. /* Failure paths set in_hw=false, because usually the acts didn't get
  81. * to efx_mae_alloc_action_set_list(); if they did, the failure tree
  82. * has a separate efx_mae_free_action_set_list() before calling us.
  83. */
  84. if (in_hw)
  85. efx_mae_free_action_set_list(efx, acts);
  86. /* Any act that's on the list will be in_hw even if the list isn't */
  87. list_for_each_entry_safe(act, next, &acts->list, list)
  88. efx_tc_free_action_set(efx, act, true);
  89. /* Don't kfree, as acts is embedded inside a struct efx_tc_flow_rule */
  90. }
  91. static void efx_tc_delete_rule(struct efx_nic *efx, struct efx_tc_flow_rule *rule)
  92. {
  93. efx_mae_delete_rule(efx, rule->fw_id);
  94. /* Release entries in subsidiary tables */
  95. efx_tc_free_action_set_list(efx, &rule->acts, true);
  96. rule->fw_id = MC_CMD_MAE_ACTION_RULE_INSERT_OUT_ACTION_RULE_ID_NULL;
  97. }
  98. static void efx_tc_flow_free(void *ptr, void *arg)
  99. {
  100. struct efx_tc_flow_rule *rule = ptr;
  101. struct efx_nic *efx = arg;
  102. netif_err(efx, drv, efx->net_dev,
  103. "tc rule %lx still present at teardown, removing\n",
  104. rule->cookie);
  105. efx_mae_delete_rule(efx, rule->fw_id);
  106. /* Release entries in subsidiary tables */
  107. efx_tc_free_action_set_list(efx, &rule->acts, true);
  108. kfree(rule);
  109. }
  110. static int efx_tc_flower_parse_match(struct efx_nic *efx,
  111. struct flow_rule *rule,
  112. struct efx_tc_match *match,
  113. struct netlink_ext_ack *extack)
  114. {
  115. struct flow_dissector *dissector = rule->match.dissector;
  116. if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_CONTROL)) {
  117. struct flow_match_control fm;
  118. flow_rule_match_control(rule, &fm);
  119. if (fm.mask->flags) {
  120. efx_tc_err(efx, "Unsupported match on control.flags %#x\n",
  121. fm.mask->flags);
  122. NL_SET_ERR_MSG_MOD(extack, "Unsupported match on control.flags");
  123. return -EOPNOTSUPP;
  124. }
  125. }
  126. if (dissector->used_keys &
  127. ~(BIT(FLOW_DISSECTOR_KEY_CONTROL) |
  128. BIT(FLOW_DISSECTOR_KEY_BASIC))) {
  129. efx_tc_err(efx, "Unsupported flower keys %#x\n", dissector->used_keys);
  130. NL_SET_ERR_MSG_MOD(extack, "Unsupported flower keys encountered");
  131. return -EOPNOTSUPP;
  132. }
  133. if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_BASIC)) {
  134. struct flow_match_basic fm;
  135. flow_rule_match_basic(rule, &fm);
  136. if (fm.mask->n_proto) {
  137. EFX_TC_ERR_MSG(efx, extack, "Unsupported eth_proto match\n");
  138. return -EOPNOTSUPP;
  139. }
  140. if (fm.mask->ip_proto) {
  141. EFX_TC_ERR_MSG(efx, extack, "Unsupported ip_proto match\n");
  142. return -EOPNOTSUPP;
  143. }
  144. }
  145. return 0;
  146. }
  147. static int efx_tc_flower_replace(struct efx_nic *efx,
  148. struct net_device *net_dev,
  149. struct flow_cls_offload *tc,
  150. struct efx_rep *efv)
  151. {
  152. struct flow_rule *fr = flow_cls_offload_flow_rule(tc);
  153. struct netlink_ext_ack *extack = tc->common.extack;
  154. struct efx_tc_flow_rule *rule = NULL, *old;
  155. struct efx_tc_action_set *act = NULL;
  156. const struct flow_action_entry *fa;
  157. struct efx_rep *from_efv, *to_efv;
  158. struct efx_tc_match match;
  159. s64 rc;
  160. int i;
  161. if (!tc_can_offload_extack(efx->net_dev, extack))
  162. return -EOPNOTSUPP;
  163. if (WARN_ON(!efx->tc))
  164. return -ENETDOWN;
  165. if (WARN_ON(!efx->tc->up))
  166. return -ENETDOWN;
  167. from_efv = efx_tc_flower_lookup_efv(efx, net_dev);
  168. if (IS_ERR(from_efv)) {
  169. /* Might be a tunnel decap rule from an indirect block.
  170. * Support for those not implemented yet.
  171. */
  172. return -EOPNOTSUPP;
  173. }
  174. if (efv != from_efv) {
  175. /* can't happen */
  176. efx_tc_err(efx, "for %s efv is %snull but from_efv is %snull\n",
  177. netdev_name(net_dev), efv ? "non-" : "",
  178. from_efv ? "non-" : "");
  179. if (efv)
  180. NL_SET_ERR_MSG_MOD(extack, "vfrep filter has PF net_dev (can't happen)");
  181. else
  182. NL_SET_ERR_MSG_MOD(extack, "PF filter has vfrep net_dev (can't happen)");
  183. return -EINVAL;
  184. }
  185. /* Parse match */
  186. memset(&match, 0, sizeof(match));
  187. rc = efx_tc_flower_external_mport(efx, from_efv);
  188. if (rc < 0) {
  189. EFX_TC_ERR_MSG(efx, extack, "Failed to identify ingress m-port");
  190. return rc;
  191. }
  192. match.value.ingress_port = rc;
  193. match.mask.ingress_port = ~0;
  194. rc = efx_tc_flower_parse_match(efx, fr, &match, extack);
  195. if (rc)
  196. return rc;
  197. if (tc->common.chain_index) {
  198. EFX_TC_ERR_MSG(efx, extack, "No support for nonzero chain_index");
  199. return -EOPNOTSUPP;
  200. }
  201. match.mask.recirc_id = 0xff;
  202. rc = efx_mae_match_check_caps(efx, &match.mask, extack);
  203. if (rc)
  204. return rc;
  205. rule = kzalloc(sizeof(*rule), GFP_USER);
  206. if (!rule)
  207. return -ENOMEM;
  208. INIT_LIST_HEAD(&rule->acts.list);
  209. rule->cookie = tc->cookie;
  210. old = rhashtable_lookup_get_insert_fast(&efx->tc->match_action_ht,
  211. &rule->linkage,
  212. efx_tc_match_action_ht_params);
  213. if (old) {
  214. netif_dbg(efx, drv, efx->net_dev,
  215. "Already offloaded rule (cookie %lx)\n", tc->cookie);
  216. rc = -EEXIST;
  217. NL_SET_ERR_MSG_MOD(extack, "Rule already offloaded");
  218. goto release;
  219. }
  220. /* Parse actions */
  221. act = kzalloc(sizeof(*act), GFP_USER);
  222. if (!act) {
  223. rc = -ENOMEM;
  224. goto release;
  225. }
  226. flow_action_for_each(i, fa, &fr->action) {
  227. struct efx_tc_action_set save;
  228. if (!act) {
  229. /* more actions after a non-pipe action */
  230. EFX_TC_ERR_MSG(efx, extack, "Action follows non-pipe action");
  231. rc = -EINVAL;
  232. goto release;
  233. }
  234. switch (fa->id) {
  235. case FLOW_ACTION_DROP:
  236. rc = efx_mae_alloc_action_set(efx, act);
  237. if (rc) {
  238. EFX_TC_ERR_MSG(efx, extack, "Failed to write action set to hw (drop)");
  239. goto release;
  240. }
  241. list_add_tail(&act->list, &rule->acts.list);
  242. act = NULL; /* end of the line */
  243. break;
  244. case FLOW_ACTION_REDIRECT:
  245. case FLOW_ACTION_MIRRED:
  246. save = *act;
  247. to_efv = efx_tc_flower_lookup_efv(efx, fa->dev);
  248. if (IS_ERR(to_efv)) {
  249. EFX_TC_ERR_MSG(efx, extack, "Mirred egress device not on switch");
  250. rc = PTR_ERR(to_efv);
  251. goto release;
  252. }
  253. rc = efx_tc_flower_external_mport(efx, to_efv);
  254. if (rc < 0) {
  255. EFX_TC_ERR_MSG(efx, extack, "Failed to identify egress m-port");
  256. goto release;
  257. }
  258. act->dest_mport = rc;
  259. act->deliver = 1;
  260. rc = efx_mae_alloc_action_set(efx, act);
  261. if (rc) {
  262. EFX_TC_ERR_MSG(efx, extack, "Failed to write action set to hw (mirred)");
  263. goto release;
  264. }
  265. list_add_tail(&act->list, &rule->acts.list);
  266. act = NULL;
  267. if (fa->id == FLOW_ACTION_REDIRECT)
  268. break; /* end of the line */
  269. /* Mirror, so continue on with saved act */
  270. act = kzalloc(sizeof(*act), GFP_USER);
  271. if (!act) {
  272. rc = -ENOMEM;
  273. goto release;
  274. }
  275. *act = save;
  276. break;
  277. default:
  278. efx_tc_err(efx, "Unhandled action %u\n", fa->id);
  279. rc = -EOPNOTSUPP;
  280. NL_SET_ERR_MSG_MOD(extack, "Unsupported action");
  281. goto release;
  282. }
  283. }
  284. if (act) {
  285. /* Not shot/redirected, so deliver to default dest */
  286. if (from_efv == EFX_EFV_PF)
  287. /* Rule applies to traffic from the wire,
  288. * and default dest is thus the PF
  289. */
  290. efx_mae_mport_uplink(efx, &act->dest_mport);
  291. else
  292. /* Representor, so rule applies to traffic from
  293. * representee, and default dest is thus the rep.
  294. * All reps use the same mport for delivery
  295. */
  296. efx_mae_mport_mport(efx, efx->tc->reps_mport_id,
  297. &act->dest_mport);
  298. act->deliver = 1;
  299. rc = efx_mae_alloc_action_set(efx, act);
  300. if (rc) {
  301. EFX_TC_ERR_MSG(efx, extack, "Failed to write action set to hw (deliver)");
  302. goto release;
  303. }
  304. list_add_tail(&act->list, &rule->acts.list);
  305. act = NULL; /* Prevent double-free in error path */
  306. }
  307. netif_dbg(efx, drv, efx->net_dev,
  308. "Successfully parsed filter (cookie %lx)\n",
  309. tc->cookie);
  310. rule->match = match;
  311. rc = efx_mae_alloc_action_set_list(efx, &rule->acts);
  312. if (rc) {
  313. EFX_TC_ERR_MSG(efx, extack, "Failed to write action set list to hw");
  314. goto release;
  315. }
  316. rc = efx_mae_insert_rule(efx, &rule->match, EFX_TC_PRIO_TC,
  317. rule->acts.fw_id, &rule->fw_id);
  318. if (rc) {
  319. EFX_TC_ERR_MSG(efx, extack, "Failed to insert rule in hw");
  320. goto release_acts;
  321. }
  322. return 0;
  323. release_acts:
  324. efx_mae_free_action_set_list(efx, &rule->acts);
  325. release:
  326. /* We failed to insert the rule, so free up any entries we created in
  327. * subsidiary tables.
  328. */
  329. if (act)
  330. efx_tc_free_action_set(efx, act, false);
  331. if (rule) {
  332. rhashtable_remove_fast(&efx->tc->match_action_ht,
  333. &rule->linkage,
  334. efx_tc_match_action_ht_params);
  335. efx_tc_free_action_set_list(efx, &rule->acts, false);
  336. }
  337. kfree(rule);
  338. return rc;
  339. }
  340. static int efx_tc_flower_destroy(struct efx_nic *efx,
  341. struct net_device *net_dev,
  342. struct flow_cls_offload *tc)
  343. {
  344. struct netlink_ext_ack *extack = tc->common.extack;
  345. struct efx_tc_flow_rule *rule;
  346. rule = rhashtable_lookup_fast(&efx->tc->match_action_ht, &tc->cookie,
  347. efx_tc_match_action_ht_params);
  348. if (!rule) {
  349. /* Only log a message if we're the ingress device. Otherwise
  350. * it's a foreign filter and we might just not have been
  351. * interested (e.g. we might not have been the egress device
  352. * either).
  353. */
  354. if (!IS_ERR(efx_tc_flower_lookup_efv(efx, net_dev)))
  355. netif_warn(efx, drv, efx->net_dev,
  356. "Filter %lx not found to remove\n", tc->cookie);
  357. NL_SET_ERR_MSG_MOD(extack, "Flow cookie not found in offloaded rules");
  358. return -ENOENT;
  359. }
  360. /* Remove it from HW */
  361. efx_tc_delete_rule(efx, rule);
  362. /* Delete it from SW */
  363. rhashtable_remove_fast(&efx->tc->match_action_ht, &rule->linkage,
  364. efx_tc_match_action_ht_params);
  365. netif_dbg(efx, drv, efx->net_dev, "Removed filter %lx\n", rule->cookie);
  366. kfree(rule);
  367. return 0;
  368. }
  369. int efx_tc_flower(struct efx_nic *efx, struct net_device *net_dev,
  370. struct flow_cls_offload *tc, struct efx_rep *efv)
  371. {
  372. int rc;
  373. if (!efx->tc)
  374. return -EOPNOTSUPP;
  375. mutex_lock(&efx->tc->mutex);
  376. switch (tc->command) {
  377. case FLOW_CLS_REPLACE:
  378. rc = efx_tc_flower_replace(efx, net_dev, tc, efv);
  379. break;
  380. case FLOW_CLS_DESTROY:
  381. rc = efx_tc_flower_destroy(efx, net_dev, tc);
  382. break;
  383. default:
  384. rc = -EOPNOTSUPP;
  385. break;
  386. }
  387. mutex_unlock(&efx->tc->mutex);
  388. return rc;
  389. }
  390. static int efx_tc_configure_default_rule(struct efx_nic *efx, u32 ing_port,
  391. u32 eg_port, struct efx_tc_flow_rule *rule)
  392. {
  393. struct efx_tc_action_set_list *acts = &rule->acts;
  394. struct efx_tc_match *match = &rule->match;
  395. struct efx_tc_action_set *act;
  396. int rc;
  397. match->value.ingress_port = ing_port;
  398. match->mask.ingress_port = ~0;
  399. act = kzalloc(sizeof(*act), GFP_KERNEL);
  400. if (!act)
  401. return -ENOMEM;
  402. act->deliver = 1;
  403. act->dest_mport = eg_port;
  404. rc = efx_mae_alloc_action_set(efx, act);
  405. if (rc)
  406. goto fail1;
  407. EFX_WARN_ON_PARANOID(!list_empty(&acts->list));
  408. list_add_tail(&act->list, &acts->list);
  409. rc = efx_mae_alloc_action_set_list(efx, acts);
  410. if (rc)
  411. goto fail2;
  412. rc = efx_mae_insert_rule(efx, match, EFX_TC_PRIO_DFLT,
  413. acts->fw_id, &rule->fw_id);
  414. if (rc)
  415. goto fail3;
  416. return 0;
  417. fail3:
  418. efx_mae_free_action_set_list(efx, acts);
  419. fail2:
  420. list_del(&act->list);
  421. efx_mae_free_action_set(efx, act->fw_id);
  422. fail1:
  423. kfree(act);
  424. return rc;
  425. }
  426. static int efx_tc_configure_default_rule_pf(struct efx_nic *efx)
  427. {
  428. struct efx_tc_flow_rule *rule = &efx->tc->dflt.pf;
  429. u32 ing_port, eg_port;
  430. efx_mae_mport_uplink(efx, &ing_port);
  431. efx_mae_mport_wire(efx, &eg_port);
  432. return efx_tc_configure_default_rule(efx, ing_port, eg_port, rule);
  433. }
  434. static int efx_tc_configure_default_rule_wire(struct efx_nic *efx)
  435. {
  436. struct efx_tc_flow_rule *rule = &efx->tc->dflt.wire;
  437. u32 ing_port, eg_port;
  438. efx_mae_mport_wire(efx, &ing_port);
  439. efx_mae_mport_uplink(efx, &eg_port);
  440. return efx_tc_configure_default_rule(efx, ing_port, eg_port, rule);
  441. }
  442. int efx_tc_configure_default_rule_rep(struct efx_rep *efv)
  443. {
  444. struct efx_tc_flow_rule *rule = &efv->dflt;
  445. struct efx_nic *efx = efv->parent;
  446. u32 ing_port, eg_port;
  447. efx_mae_mport_mport(efx, efv->mport, &ing_port);
  448. efx_mae_mport_mport(efx, efx->tc->reps_mport_id, &eg_port);
  449. return efx_tc_configure_default_rule(efx, ing_port, eg_port, rule);
  450. }
  451. void efx_tc_deconfigure_default_rule(struct efx_nic *efx,
  452. struct efx_tc_flow_rule *rule)
  453. {
  454. if (rule->fw_id != MC_CMD_MAE_ACTION_RULE_INSERT_OUT_ACTION_RULE_ID_NULL)
  455. efx_tc_delete_rule(efx, rule);
  456. rule->fw_id = MC_CMD_MAE_ACTION_RULE_INSERT_OUT_ACTION_RULE_ID_NULL;
  457. }
  458. static int efx_tc_configure_rep_mport(struct efx_nic *efx)
  459. {
  460. u32 rep_mport_label;
  461. int rc;
  462. rc = efx_mae_allocate_mport(efx, &efx->tc->reps_mport_id, &rep_mport_label);
  463. if (rc)
  464. return rc;
  465. pci_dbg(efx->pci_dev, "created rep mport 0x%08x (0x%04x)\n",
  466. efx->tc->reps_mport_id, rep_mport_label);
  467. /* Use mport *selector* as vport ID */
  468. efx_mae_mport_mport(efx, efx->tc->reps_mport_id,
  469. &efx->tc->reps_mport_vport_id);
  470. return 0;
  471. }
  472. static void efx_tc_deconfigure_rep_mport(struct efx_nic *efx)
  473. {
  474. efx_mae_free_mport(efx, efx->tc->reps_mport_id);
  475. efx->tc->reps_mport_id = MAE_MPORT_SELECTOR_NULL;
  476. }
  477. int efx_tc_insert_rep_filters(struct efx_nic *efx)
  478. {
  479. struct efx_filter_spec promisc, allmulti;
  480. int rc;
  481. if (efx->type->is_vf)
  482. return 0;
  483. if (!efx->tc)
  484. return 0;
  485. efx_filter_init_rx(&promisc, EFX_FILTER_PRI_REQUIRED, 0, 0);
  486. efx_filter_set_uc_def(&promisc);
  487. efx_filter_set_vport_id(&promisc, efx->tc->reps_mport_vport_id);
  488. rc = efx_filter_insert_filter(efx, &promisc, false);
  489. if (rc < 0)
  490. return rc;
  491. efx->tc->reps_filter_uc = rc;
  492. efx_filter_init_rx(&allmulti, EFX_FILTER_PRI_REQUIRED, 0, 0);
  493. efx_filter_set_mc_def(&allmulti);
  494. efx_filter_set_vport_id(&allmulti, efx->tc->reps_mport_vport_id);
  495. rc = efx_filter_insert_filter(efx, &allmulti, false);
  496. if (rc < 0)
  497. return rc;
  498. efx->tc->reps_filter_mc = rc;
  499. return 0;
  500. }
  501. void efx_tc_remove_rep_filters(struct efx_nic *efx)
  502. {
  503. if (efx->type->is_vf)
  504. return;
  505. if (!efx->tc)
  506. return;
  507. if (efx->tc->reps_filter_mc >= 0)
  508. efx_filter_remove_id_safe(efx, EFX_FILTER_PRI_REQUIRED, efx->tc->reps_filter_mc);
  509. efx->tc->reps_filter_mc = -1;
  510. if (efx->tc->reps_filter_uc >= 0)
  511. efx_filter_remove_id_safe(efx, EFX_FILTER_PRI_REQUIRED, efx->tc->reps_filter_uc);
  512. efx->tc->reps_filter_uc = -1;
  513. }
  514. int efx_init_tc(struct efx_nic *efx)
  515. {
  516. int rc;
  517. rc = efx_mae_get_caps(efx, efx->tc->caps);
  518. if (rc)
  519. return rc;
  520. if (efx->tc->caps->match_field_count > MAE_NUM_FIELDS)
  521. /* Firmware supports some match fields the driver doesn't know
  522. * about. Not fatal, unless any of those fields are required
  523. * (MAE_FIELD_SUPPORTED_MATCH_ALWAYS) but if so we don't know.
  524. */
  525. netif_warn(efx, probe, efx->net_dev,
  526. "FW reports additional match fields %u\n",
  527. efx->tc->caps->match_field_count);
  528. if (efx->tc->caps->action_prios < EFX_TC_PRIO__NUM) {
  529. netif_err(efx, probe, efx->net_dev,
  530. "Too few action prios supported (have %u, need %u)\n",
  531. efx->tc->caps->action_prios, EFX_TC_PRIO__NUM);
  532. return -EIO;
  533. }
  534. rc = efx_tc_configure_default_rule_pf(efx);
  535. if (rc)
  536. return rc;
  537. rc = efx_tc_configure_default_rule_wire(efx);
  538. if (rc)
  539. return rc;
  540. rc = efx_tc_configure_rep_mport(efx);
  541. if (rc)
  542. return rc;
  543. rc = flow_indr_dev_register(efx_tc_indr_setup_cb, efx);
  544. if (rc)
  545. return rc;
  546. efx->tc->up = true;
  547. return 0;
  548. }
  549. void efx_fini_tc(struct efx_nic *efx)
  550. {
  551. /* We can get called even if efx_init_struct_tc() failed */
  552. if (!efx->tc)
  553. return;
  554. if (efx->tc->up)
  555. flow_indr_dev_unregister(efx_tc_indr_setup_cb, efx, efx_tc_block_unbind);
  556. efx_tc_deconfigure_rep_mport(efx);
  557. efx_tc_deconfigure_default_rule(efx, &efx->tc->dflt.pf);
  558. efx_tc_deconfigure_default_rule(efx, &efx->tc->dflt.wire);
  559. efx->tc->up = false;
  560. }
  561. int efx_init_struct_tc(struct efx_nic *efx)
  562. {
  563. int rc;
  564. if (efx->type->is_vf)
  565. return 0;
  566. efx->tc = kzalloc(sizeof(*efx->tc), GFP_KERNEL);
  567. if (!efx->tc)
  568. return -ENOMEM;
  569. efx->tc->caps = kzalloc(sizeof(struct mae_caps), GFP_KERNEL);
  570. if (!efx->tc->caps) {
  571. rc = -ENOMEM;
  572. goto fail_alloc_caps;
  573. }
  574. INIT_LIST_HEAD(&efx->tc->block_list);
  575. mutex_init(&efx->tc->mutex);
  576. rc = rhashtable_init(&efx->tc->match_action_ht, &efx_tc_match_action_ht_params);
  577. if (rc < 0)
  578. goto fail_match_action_ht;
  579. efx->tc->reps_filter_uc = -1;
  580. efx->tc->reps_filter_mc = -1;
  581. INIT_LIST_HEAD(&efx->tc->dflt.pf.acts.list);
  582. efx->tc->dflt.pf.fw_id = MC_CMD_MAE_ACTION_RULE_INSERT_OUT_ACTION_RULE_ID_NULL;
  583. INIT_LIST_HEAD(&efx->tc->dflt.wire.acts.list);
  584. efx->tc->dflt.wire.fw_id = MC_CMD_MAE_ACTION_RULE_INSERT_OUT_ACTION_RULE_ID_NULL;
  585. return 0;
  586. fail_match_action_ht:
  587. mutex_destroy(&efx->tc->mutex);
  588. kfree(efx->tc->caps);
  589. fail_alloc_caps:
  590. kfree(efx->tc);
  591. efx->tc = NULL;
  592. return rc;
  593. }
  594. void efx_fini_struct_tc(struct efx_nic *efx)
  595. {
  596. if (!efx->tc)
  597. return;
  598. mutex_lock(&efx->tc->mutex);
  599. EFX_WARN_ON_PARANOID(efx->tc->dflt.pf.fw_id !=
  600. MC_CMD_MAE_ACTION_RULE_INSERT_OUT_ACTION_RULE_ID_NULL);
  601. EFX_WARN_ON_PARANOID(efx->tc->dflt.wire.fw_id !=
  602. MC_CMD_MAE_ACTION_RULE_INSERT_OUT_ACTION_RULE_ID_NULL);
  603. rhashtable_free_and_destroy(&efx->tc->match_action_ht, efx_tc_flow_free,
  604. efx);
  605. mutex_unlock(&efx->tc->mutex);
  606. mutex_destroy(&efx->tc->mutex);
  607. kfree(efx->tc->caps);
  608. kfree(efx->tc);
  609. efx->tc = NULL;
  610. }