fbnic_rpc.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright (c) Meta Platforms, Inc. and affiliates. */
  3. #include <linux/etherdevice.h>
  4. #include <linux/ethtool.h>
  5. #include <net/ipv6.h>
  6. #include "fbnic.h"
  7. #include "fbnic_fw.h"
  8. #include "fbnic_netdev.h"
  9. #include "fbnic_rpc.h"
  10. void fbnic_reset_indir_tbl(struct fbnic_net *fbn)
  11. {
  12. unsigned int num_rx = fbn->num_rx_queues;
  13. unsigned int i;
  14. if (netif_is_rxfh_configured(fbn->netdev))
  15. return;
  16. for (i = 0; i < FBNIC_RPC_RSS_TBL_SIZE; i++)
  17. fbn->indir_tbl[0][i] = ethtool_rxfh_indir_default(i, num_rx);
  18. }
  19. void fbnic_rss_key_fill(u32 *buffer)
  20. {
  21. static u32 rss_key[FBNIC_RPC_RSS_KEY_DWORD_LEN];
  22. net_get_random_once(rss_key, sizeof(rss_key));
  23. rss_key[FBNIC_RPC_RSS_KEY_LAST_IDX] &= FBNIC_RPC_RSS_KEY_LAST_MASK;
  24. memcpy(buffer, rss_key, sizeof(rss_key));
  25. }
  26. #define RX_HASH_OPT_L4 \
  27. (RXH_IP_SRC | RXH_IP_DST | RXH_L4_B_0_1 | RXH_L4_B_2_3)
  28. #define RX_HASH_OPT_L3 \
  29. (RXH_IP_SRC | RXH_IP_DST)
  30. #define RX_HASH_OPT_L2 RXH_L2DA
  31. void fbnic_rss_init_en_mask(struct fbnic_net *fbn)
  32. {
  33. fbn->rss_flow_hash[FBNIC_TCP4_HASH_OPT] = RX_HASH_OPT_L4;
  34. fbn->rss_flow_hash[FBNIC_TCP6_HASH_OPT] = RX_HASH_OPT_L4;
  35. fbn->rss_flow_hash[FBNIC_UDP4_HASH_OPT] = RX_HASH_OPT_L3;
  36. fbn->rss_flow_hash[FBNIC_UDP6_HASH_OPT] = RX_HASH_OPT_L3;
  37. fbn->rss_flow_hash[FBNIC_IPV4_HASH_OPT] = RX_HASH_OPT_L3;
  38. fbn->rss_flow_hash[FBNIC_IPV6_HASH_OPT] = RX_HASH_OPT_L3;
  39. fbn->rss_flow_hash[FBNIC_ETHER_HASH_OPT] = RX_HASH_OPT_L2;
  40. }
  41. void fbnic_rss_disable_hw(struct fbnic_dev *fbd)
  42. {
  43. /* Disable RPC by clearing enable bit and configuration */
  44. if (!fbnic_bmc_present(fbd))
  45. wr32(fbd, FBNIC_RPC_RMI_CONFIG,
  46. FIELD_PREP(FBNIC_RPC_RMI_CONFIG_OH_BYTES, 20));
  47. }
  48. #define FBNIC_FH_2_RSSEM_BIT(_fh, _rssem, _val) \
  49. FIELD_PREP(FBNIC_RPC_ACT_TBL1_RSS_ENA_##_rssem, \
  50. FIELD_GET(RXH_##_fh, _val))
  51. u16 fbnic_flow_hash_2_rss_en_mask(struct fbnic_net *fbn, int flow_type)
  52. {
  53. u32 flow_hash = fbn->rss_flow_hash[flow_type];
  54. u32 rss_en_mask = 0;
  55. rss_en_mask |= FBNIC_FH_2_RSSEM_BIT(L2DA, L2_DA, flow_hash);
  56. rss_en_mask |= FBNIC_FH_2_RSSEM_BIT(IP_SRC, IP_SRC, flow_hash);
  57. rss_en_mask |= FBNIC_FH_2_RSSEM_BIT(IP_DST, IP_DST, flow_hash);
  58. rss_en_mask |= FBNIC_FH_2_RSSEM_BIT(L4_B_0_1, L4_SRC, flow_hash);
  59. rss_en_mask |= FBNIC_FH_2_RSSEM_BIT(L4_B_2_3, L4_DST, flow_hash);
  60. rss_en_mask |= FBNIC_FH_2_RSSEM_BIT(IP6_FL, OV6_FL_LBL, flow_hash);
  61. rss_en_mask |= FBNIC_FH_2_RSSEM_BIT(IP6_FL, IV6_FL_LBL, flow_hash);
  62. return rss_en_mask;
  63. }
  64. void fbnic_rss_reinit_hw(struct fbnic_dev *fbd, struct fbnic_net *fbn)
  65. {
  66. unsigned int i;
  67. for (i = 0; i < FBNIC_RPC_RSS_TBL_SIZE; i++) {
  68. wr32(fbd, FBNIC_RPC_RSS_TBL(0, i), fbn->indir_tbl[0][i]);
  69. wr32(fbd, FBNIC_RPC_RSS_TBL(1, i), fbn->indir_tbl[1][i]);
  70. }
  71. for (i = 0; i < FBNIC_RPC_RSS_KEY_DWORD_LEN; i++)
  72. wr32(fbd, FBNIC_RPC_RSS_KEY(i), fbn->rss_key[i]);
  73. /* Default action for this to drop w/ no destination */
  74. wr32(fbd, FBNIC_RPC_ACT_TBL0_DEFAULT, FBNIC_RPC_ACT_TBL0_DROP);
  75. wrfl(fbd);
  76. wr32(fbd, FBNIC_RPC_ACT_TBL1_DEFAULT, 0);
  77. /* If it isn't already enabled set the RMI Config value to enable RPC */
  78. wr32(fbd, FBNIC_RPC_RMI_CONFIG,
  79. FIELD_PREP(FBNIC_RPC_RMI_CONFIG_MTU, FBNIC_MAX_JUMBO_FRAME_SIZE) |
  80. FIELD_PREP(FBNIC_RPC_RMI_CONFIG_OH_BYTES, 20) |
  81. FBNIC_RPC_RMI_CONFIG_ENABLE);
  82. }
  83. void fbnic_bmc_rpc_all_multi_config(struct fbnic_dev *fbd,
  84. bool enable_host)
  85. {
  86. struct fbnic_act_tcam *act_tcam;
  87. struct fbnic_mac_addr *mac_addr;
  88. int j;
  89. /* We need to add the all multicast filter at the end of the
  90. * multicast address list. This way if there are any that are
  91. * shared between the host and the BMC they can be directed to
  92. * both. Otherwise the remainder just get sent directly to the
  93. * BMC.
  94. */
  95. mac_addr = &fbd->mac_addr[fbd->mac_addr_boundary - 1];
  96. if (fbnic_bmc_present(fbd) && fbd->fw_cap.all_multi) {
  97. if (mac_addr->state != FBNIC_TCAM_S_VALID) {
  98. eth_zero_addr(mac_addr->value.addr8);
  99. eth_broadcast_addr(mac_addr->mask.addr8);
  100. mac_addr->value.addr8[0] ^= 1;
  101. mac_addr->mask.addr8[0] ^= 1;
  102. set_bit(FBNIC_MAC_ADDR_T_BMC, mac_addr->act_tcam);
  103. mac_addr->state = FBNIC_TCAM_S_ADD;
  104. }
  105. if (enable_host)
  106. set_bit(FBNIC_MAC_ADDR_T_ALLMULTI,
  107. mac_addr->act_tcam);
  108. else
  109. clear_bit(FBNIC_MAC_ADDR_T_ALLMULTI,
  110. mac_addr->act_tcam);
  111. } else {
  112. __fbnic_xc_unsync(mac_addr, FBNIC_MAC_ADDR_T_BMC);
  113. __fbnic_xc_unsync(mac_addr, FBNIC_MAC_ADDR_T_ALLMULTI);
  114. }
  115. /* We have to add a special handler for multicast as the
  116. * BMC may have an all-multi rule already in place. As such
  117. * adding a rule ourselves won't do any good so we will have
  118. * to modify the rules for the ALL MULTI below if the BMC
  119. * already has the rule in place.
  120. */
  121. act_tcam = &fbd->act_tcam[FBNIC_RPC_ACT_TBL_BMC_ALL_MULTI_OFFSET];
  122. /* If we are not enabling the rule just delete it. We will fall
  123. * back to the RSS rules that support the multicast addresses.
  124. */
  125. if (!fbnic_bmc_present(fbd) || !fbd->fw_cap.all_multi || enable_host) {
  126. if (act_tcam->state == FBNIC_TCAM_S_VALID)
  127. act_tcam->state = FBNIC_TCAM_S_DELETE;
  128. return;
  129. }
  130. /* Rewrite TCAM rule 23 to handle BMC all-multi traffic */
  131. act_tcam->dest = FIELD_PREP(FBNIC_RPC_ACT_TBL0_DEST_MASK,
  132. FBNIC_RPC_ACT_TBL0_DEST_BMC);
  133. act_tcam->mask.tcam[0] = 0xffff;
  134. /* MACDA 0 - 3 is reserved for the BMC MAC address */
  135. act_tcam->value.tcam[1] =
  136. FIELD_PREP(FBNIC_RPC_TCAM_ACT1_L2_MACDA_IDX,
  137. fbd->mac_addr_boundary - 1) |
  138. FBNIC_RPC_TCAM_ACT1_L2_MACDA_VALID;
  139. act_tcam->mask.tcam[1] = 0xffff &
  140. ~FBNIC_RPC_TCAM_ACT1_L2_MACDA_IDX &
  141. ~FBNIC_RPC_TCAM_ACT1_L2_MACDA_VALID;
  142. for (j = 2; j < FBNIC_RPC_TCAM_ACT_WORD_LEN; j++)
  143. act_tcam->mask.tcam[j] = 0xffff;
  144. act_tcam->state = FBNIC_TCAM_S_UPDATE;
  145. }
  146. void fbnic_bmc_rpc_init(struct fbnic_dev *fbd)
  147. {
  148. int i = FBNIC_RPC_TCAM_MACDA_BMC_ADDR_IDX;
  149. struct fbnic_act_tcam *act_tcam;
  150. struct fbnic_mac_addr *mac_addr;
  151. int j;
  152. /* Check if BMC is present */
  153. if (!fbnic_bmc_present(fbd))
  154. return;
  155. /* Fetch BMC MAC addresses from firmware capabilities */
  156. for (j = 0; j < 4; j++) {
  157. u8 *bmc_mac = fbd->fw_cap.bmc_mac_addr[j];
  158. /* Validate BMC MAC addresses */
  159. if (is_zero_ether_addr(bmc_mac))
  160. continue;
  161. if (is_multicast_ether_addr(bmc_mac))
  162. mac_addr = __fbnic_mc_sync(fbd, bmc_mac);
  163. else
  164. mac_addr = &fbd->mac_addr[i++];
  165. if (!mac_addr) {
  166. netdev_err(fbd->netdev,
  167. "No slot for BMC MAC address[%d]\n", j);
  168. continue;
  169. }
  170. ether_addr_copy(mac_addr->value.addr8, bmc_mac);
  171. eth_zero_addr(mac_addr->mask.addr8);
  172. set_bit(FBNIC_MAC_ADDR_T_BMC, mac_addr->act_tcam);
  173. mac_addr->state = FBNIC_TCAM_S_ADD;
  174. }
  175. /* Validate Broadcast is also present, record it and tag it */
  176. mac_addr = &fbd->mac_addr[FBNIC_RPC_TCAM_MACDA_BROADCAST_IDX];
  177. eth_broadcast_addr(mac_addr->value.addr8);
  178. set_bit(FBNIC_MAC_ADDR_T_BMC, mac_addr->act_tcam);
  179. mac_addr->state = FBNIC_TCAM_S_ADD;
  180. /* Rewrite TCAM rule 0 if it isn't present to relocate BMC rules */
  181. act_tcam = &fbd->act_tcam[FBNIC_RPC_ACT_TBL_BMC_OFFSET];
  182. act_tcam->dest = FIELD_PREP(FBNIC_RPC_ACT_TBL0_DEST_MASK,
  183. FBNIC_RPC_ACT_TBL0_DEST_BMC);
  184. act_tcam->mask.tcam[0] = 0xffff;
  185. /* MACDA 0 - 3 is reserved for the BMC MAC address
  186. * to account for that we have to mask out the lower 2 bits
  187. * of the macda by performing an &= with 0x1c.
  188. */
  189. act_tcam->value.tcam[1] = FBNIC_RPC_TCAM_ACT1_L2_MACDA_VALID;
  190. act_tcam->mask.tcam[1] = 0xffff &
  191. ~FIELD_PREP(FBNIC_RPC_TCAM_ACT1_L2_MACDA_IDX, 0x1c) &
  192. ~FBNIC_RPC_TCAM_ACT1_L2_MACDA_VALID;
  193. for (j = 2; j < FBNIC_RPC_TCAM_ACT_WORD_LEN; j++)
  194. act_tcam->mask.tcam[j] = 0xffff;
  195. act_tcam->state = FBNIC_TCAM_S_UPDATE;
  196. }
  197. void fbnic_bmc_rpc_check(struct fbnic_dev *fbd)
  198. {
  199. int err;
  200. if (fbd->fw_cap.need_bmc_tcam_reinit) {
  201. fbnic_bmc_rpc_init(fbd);
  202. __fbnic_set_rx_mode(fbd);
  203. fbd->fw_cap.need_bmc_tcam_reinit = false;
  204. }
  205. if (fbd->fw_cap.need_bmc_macda_sync) {
  206. err = fbnic_fw_xmit_rpc_macda_sync(fbd);
  207. if (err)
  208. dev_warn(fbd->dev,
  209. "Writing MACDA table to FW failed, err: %d\n", err);
  210. fbd->fw_cap.need_bmc_macda_sync = false;
  211. }
  212. }
  213. #define FBNIC_ACT1_INIT(_l4, _udp, _ip, _v6) \
  214. (((_l4) ? FBNIC_RPC_TCAM_ACT1_L4_VALID : 0) | \
  215. ((_udp) ? FBNIC_RPC_TCAM_ACT1_L4_IS_UDP : 0) | \
  216. ((_ip) ? FBNIC_RPC_TCAM_ACT1_IP_VALID : 0) | \
  217. ((_v6) ? FBNIC_RPC_TCAM_ACT1_IP_IS_V6 : 0))
  218. #define FBNIC_TSTAMP_MASK(_all, _udp, _ether) \
  219. (((_all) ? ((1u << FBNIC_NUM_HASH_OPT) - 1) : 0) | \
  220. ((_udp) ? (1u << FBNIC_UDP6_HASH_OPT) | \
  221. (1u << FBNIC_UDP4_HASH_OPT) : 0) | \
  222. ((_ether) ? (1u << FBNIC_ETHER_HASH_OPT) : 0))
  223. void fbnic_rss_reinit(struct fbnic_dev *fbd, struct fbnic_net *fbn)
  224. {
  225. static const u32 act1_value[FBNIC_NUM_HASH_OPT] = {
  226. FBNIC_ACT1_INIT(1, 1, 1, 1), /* UDP6 */
  227. FBNIC_ACT1_INIT(1, 1, 1, 0), /* UDP4 */
  228. FBNIC_ACT1_INIT(1, 0, 1, 1), /* TCP6 */
  229. FBNIC_ACT1_INIT(1, 0, 1, 0), /* TCP4 */
  230. FBNIC_ACT1_INIT(0, 0, 1, 1), /* IP6 */
  231. FBNIC_ACT1_INIT(0, 0, 1, 0), /* IP4 */
  232. 0 /* Ether */
  233. };
  234. u32 tstamp_mask = 0;
  235. unsigned int i;
  236. /* To support scenarios where a BMC is present we must write the
  237. * rules twice, once for the unicast cases, and once again for
  238. * the broadcast/multicast cases as we have to support 2 destinations.
  239. */
  240. BUILD_BUG_ON(FBNIC_RSS_EN_NUM_UNICAST * 2 != FBNIC_RSS_EN_NUM_ENTRIES);
  241. BUILD_BUG_ON(ARRAY_SIZE(act1_value) != FBNIC_NUM_HASH_OPT);
  242. /* Set timestamp mask with 1b per flow type */
  243. if (fbn->hwtstamp_config.rx_filter != HWTSTAMP_FILTER_NONE) {
  244. switch (fbn->hwtstamp_config.rx_filter) {
  245. case HWTSTAMP_FILTER_ALL:
  246. tstamp_mask = FBNIC_TSTAMP_MASK(1, 1, 1);
  247. break;
  248. case HWTSTAMP_FILTER_PTP_V2_EVENT:
  249. tstamp_mask = FBNIC_TSTAMP_MASK(0, 1, 1);
  250. break;
  251. case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
  252. case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
  253. tstamp_mask = FBNIC_TSTAMP_MASK(0, 1, 0);
  254. break;
  255. case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
  256. tstamp_mask = FBNIC_TSTAMP_MASK(0, 0, 1);
  257. break;
  258. default:
  259. netdev_warn(fbn->netdev, "Unsupported hwtstamp_rx_filter\n");
  260. break;
  261. }
  262. }
  263. /* Program RSS hash enable mask for host in action TCAM/table. */
  264. for (i = fbnic_bmc_present(fbd) ? 0 : FBNIC_RSS_EN_NUM_UNICAST;
  265. i < FBNIC_RSS_EN_NUM_ENTRIES; i++) {
  266. unsigned int idx = i + FBNIC_RPC_ACT_TBL_RSS_OFFSET;
  267. struct fbnic_act_tcam *act_tcam = &fbd->act_tcam[idx];
  268. u32 flow_hash, dest, rss_en_mask;
  269. int flow_type, j;
  270. u16 value = 0;
  271. flow_type = i % FBNIC_RSS_EN_NUM_UNICAST;
  272. flow_hash = fbn->rss_flow_hash[flow_type];
  273. /* Set DEST_HOST based on absence of RXH_DISCARD */
  274. dest = FIELD_PREP(FBNIC_RPC_ACT_TBL0_DEST_MASK,
  275. !(RXH_DISCARD & flow_hash) ?
  276. FBNIC_RPC_ACT_TBL0_DEST_HOST : 0);
  277. if (i >= FBNIC_RSS_EN_NUM_UNICAST && fbnic_bmc_present(fbd))
  278. dest |= FIELD_PREP(FBNIC_RPC_ACT_TBL0_DEST_MASK,
  279. FBNIC_RPC_ACT_TBL0_DEST_BMC);
  280. if (!dest)
  281. dest = FBNIC_RPC_ACT_TBL0_DROP;
  282. else if (tstamp_mask & (1u << flow_type))
  283. dest |= FBNIC_RPC_ACT_TBL0_TS_ENA;
  284. dest |= FIELD_PREP(FBNIC_RPC_ACT_TBL0_DMA_HINT,
  285. FBNIC_RCD_HDR_AL_DMA_HINT_L4);
  286. rss_en_mask = fbnic_flow_hash_2_rss_en_mask(fbn, flow_type);
  287. act_tcam->dest = dest;
  288. act_tcam->rss_en_mask = rss_en_mask;
  289. act_tcam->state = FBNIC_TCAM_S_UPDATE;
  290. act_tcam->mask.tcam[0] = 0xffff;
  291. /* We reserve the upper 8 MACDA TCAM entries for host
  292. * unicast. So we set the value to 24, and the mask the
  293. * lower bits so that the lower entries can be used as
  294. * multicast or BMC addresses.
  295. */
  296. if (i < FBNIC_RSS_EN_NUM_UNICAST)
  297. value = FIELD_PREP(FBNIC_RPC_TCAM_ACT1_L2_MACDA_IDX,
  298. fbd->mac_addr_boundary);
  299. value |= FBNIC_RPC_TCAM_ACT1_L2_MACDA_VALID;
  300. flow_type = i % FBNIC_RSS_EN_NUM_UNICAST;
  301. value |= act1_value[flow_type];
  302. act_tcam->value.tcam[1] = value;
  303. act_tcam->mask.tcam[1] = ~value;
  304. for (j = 2; j < FBNIC_RPC_TCAM_ACT_WORD_LEN; j++)
  305. act_tcam->mask.tcam[j] = 0xffff;
  306. act_tcam->state = FBNIC_TCAM_S_UPDATE;
  307. }
  308. }
  309. struct fbnic_mac_addr *__fbnic_uc_sync(struct fbnic_dev *fbd,
  310. const unsigned char *addr)
  311. {
  312. struct fbnic_mac_addr *avail_addr = NULL;
  313. unsigned int i;
  314. /* Scan from middle of list to bottom, filling bottom up.
  315. * Skip the first entry which is reserved for dev_addr and
  316. * leave the last entry to use for promiscuous filtering.
  317. */
  318. for (i = fbd->mac_addr_boundary - 1;
  319. i < FBNIC_RPC_TCAM_MACDA_HOST_ADDR_IDX; i++) {
  320. struct fbnic_mac_addr *mac_addr = &fbd->mac_addr[i];
  321. if (mac_addr->state == FBNIC_TCAM_S_DISABLED) {
  322. avail_addr = mac_addr;
  323. } else if (ether_addr_equal(mac_addr->value.addr8, addr)) {
  324. avail_addr = mac_addr;
  325. break;
  326. }
  327. }
  328. if (avail_addr && avail_addr->state == FBNIC_TCAM_S_DISABLED) {
  329. ether_addr_copy(avail_addr->value.addr8, addr);
  330. eth_zero_addr(avail_addr->mask.addr8);
  331. avail_addr->state = FBNIC_TCAM_S_ADD;
  332. }
  333. return avail_addr;
  334. }
  335. struct fbnic_mac_addr *__fbnic_mc_sync(struct fbnic_dev *fbd,
  336. const unsigned char *addr)
  337. {
  338. struct fbnic_mac_addr *avail_addr = NULL;
  339. unsigned int i;
  340. /* Scan from middle of list to top, filling top down.
  341. * Skip over the address reserved for the BMC MAC and
  342. * exclude index 0 as that belongs to the broadcast address
  343. */
  344. for (i = fbd->mac_addr_boundary;
  345. --i > FBNIC_RPC_TCAM_MACDA_BROADCAST_IDX;) {
  346. struct fbnic_mac_addr *mac_addr = &fbd->mac_addr[i];
  347. if (mac_addr->state == FBNIC_TCAM_S_DISABLED) {
  348. avail_addr = mac_addr;
  349. } else if (ether_addr_equal(mac_addr->value.addr8, addr)) {
  350. avail_addr = mac_addr;
  351. break;
  352. }
  353. }
  354. /* Scan the BMC addresses to see if it may have already
  355. * reserved the address.
  356. */
  357. while (--i) {
  358. struct fbnic_mac_addr *mac_addr = &fbd->mac_addr[i];
  359. if (!is_zero_ether_addr(mac_addr->mask.addr8))
  360. continue;
  361. /* Only move on if we find a match */
  362. if (!ether_addr_equal(mac_addr->value.addr8, addr))
  363. continue;
  364. /* We need to pull this address to the shared area */
  365. if (avail_addr) {
  366. memcpy(avail_addr, mac_addr, sizeof(*mac_addr));
  367. mac_addr->state = FBNIC_TCAM_S_DELETE;
  368. avail_addr->state = FBNIC_TCAM_S_ADD;
  369. }
  370. break;
  371. }
  372. if (avail_addr && avail_addr->state == FBNIC_TCAM_S_DISABLED) {
  373. ether_addr_copy(avail_addr->value.addr8, addr);
  374. eth_zero_addr(avail_addr->mask.addr8);
  375. avail_addr->state = FBNIC_TCAM_S_ADD;
  376. }
  377. return avail_addr;
  378. }
  379. int __fbnic_xc_unsync(struct fbnic_mac_addr *mac_addr, unsigned int tcam_idx)
  380. {
  381. if (!test_and_clear_bit(tcam_idx, mac_addr->act_tcam))
  382. return -ENOENT;
  383. if (bitmap_empty(mac_addr->act_tcam, FBNIC_RPC_TCAM_ACT_NUM_ENTRIES))
  384. mac_addr->state = FBNIC_TCAM_S_DELETE;
  385. return 0;
  386. }
  387. void fbnic_promisc_sync(struct fbnic_dev *fbd,
  388. bool uc_promisc, bool mc_promisc)
  389. {
  390. struct fbnic_mac_addr *mac_addr;
  391. /* Populate last TCAM entry with promiscuous entry and 0/1 bit mask */
  392. mac_addr = &fbd->mac_addr[FBNIC_RPC_TCAM_MACDA_PROMISC_IDX];
  393. if (uc_promisc) {
  394. if (!is_zero_ether_addr(mac_addr->value.addr8) ||
  395. mac_addr->state != FBNIC_TCAM_S_VALID) {
  396. eth_zero_addr(mac_addr->value.addr8);
  397. eth_broadcast_addr(mac_addr->mask.addr8);
  398. clear_bit(FBNIC_MAC_ADDR_T_ALLMULTI,
  399. mac_addr->act_tcam);
  400. set_bit(FBNIC_MAC_ADDR_T_PROMISC,
  401. mac_addr->act_tcam);
  402. mac_addr->state = FBNIC_TCAM_S_ADD;
  403. }
  404. } else if (mc_promisc &&
  405. (!fbnic_bmc_present(fbd) || !fbd->fw_cap.all_multi)) {
  406. /* We have to add a special handler for multicast as the
  407. * BMC may have an all-multi rule already in place. As such
  408. * adding a rule ourselves won't do any good so we will have
  409. * to modify the rules for the ALL MULTI below if the BMC
  410. * already has the rule in place.
  411. */
  412. if (!is_multicast_ether_addr(mac_addr->value.addr8) ||
  413. mac_addr->state != FBNIC_TCAM_S_VALID) {
  414. eth_zero_addr(mac_addr->value.addr8);
  415. eth_broadcast_addr(mac_addr->mask.addr8);
  416. mac_addr->value.addr8[0] ^= 1;
  417. mac_addr->mask.addr8[0] ^= 1;
  418. set_bit(FBNIC_MAC_ADDR_T_ALLMULTI,
  419. mac_addr->act_tcam);
  420. clear_bit(FBNIC_MAC_ADDR_T_PROMISC,
  421. mac_addr->act_tcam);
  422. mac_addr->state = FBNIC_TCAM_S_ADD;
  423. }
  424. } else if (mac_addr->state == FBNIC_TCAM_S_VALID) {
  425. __fbnic_xc_unsync(mac_addr, FBNIC_MAC_ADDR_T_ALLMULTI);
  426. __fbnic_xc_unsync(mac_addr, FBNIC_MAC_ADDR_T_PROMISC);
  427. }
  428. }
  429. void fbnic_sift_macda(struct fbnic_dev *fbd)
  430. {
  431. int dest, src;
  432. /* Move BMC only addresses back into BMC region */
  433. for (dest = FBNIC_RPC_TCAM_MACDA_BMC_ADDR_IDX,
  434. src = FBNIC_RPC_TCAM_MACDA_MULTICAST_IDX;
  435. ++dest < FBNIC_RPC_TCAM_MACDA_BROADCAST_IDX &&
  436. src < fbd->mac_addr_boundary;) {
  437. struct fbnic_mac_addr *dest_addr = &fbd->mac_addr[dest];
  438. if (dest_addr->state != FBNIC_TCAM_S_DISABLED)
  439. continue;
  440. while (src < fbd->mac_addr_boundary) {
  441. struct fbnic_mac_addr *src_addr = &fbd->mac_addr[src++];
  442. /* Verify BMC bit is set */
  443. if (!test_bit(FBNIC_MAC_ADDR_T_BMC, src_addr->act_tcam))
  444. continue;
  445. /* Verify filter isn't already disabled */
  446. if (src_addr->state == FBNIC_TCAM_S_DISABLED ||
  447. src_addr->state == FBNIC_TCAM_S_DELETE)
  448. continue;
  449. /* Verify only BMC bit is set */
  450. if (bitmap_weight(src_addr->act_tcam,
  451. FBNIC_RPC_TCAM_ACT_NUM_ENTRIES) != 1)
  452. continue;
  453. /* Verify we are not moving wildcard address */
  454. if (!is_zero_ether_addr(src_addr->mask.addr8))
  455. continue;
  456. memcpy(dest_addr, src_addr, sizeof(*src_addr));
  457. src_addr->state = FBNIC_TCAM_S_DELETE;
  458. dest_addr->state = FBNIC_TCAM_S_ADD;
  459. }
  460. }
  461. }
  462. static void fbnic_clear_macda_entry(struct fbnic_dev *fbd, unsigned int idx)
  463. {
  464. int i;
  465. /* Invalidate entry and clear addr state info */
  466. for (i = 0; i <= FBNIC_RPC_TCAM_MACDA_WORD_LEN; i++)
  467. wr32(fbd, FBNIC_RPC_TCAM_MACDA(idx, i), 0);
  468. }
  469. static void fbnic_clear_macda(struct fbnic_dev *fbd)
  470. {
  471. int idx;
  472. for (idx = ARRAY_SIZE(fbd->mac_addr); idx--;) {
  473. struct fbnic_mac_addr *mac_addr = &fbd->mac_addr[idx];
  474. if (mac_addr->state == FBNIC_TCAM_S_DISABLED)
  475. continue;
  476. if (test_bit(FBNIC_MAC_ADDR_T_BMC, mac_addr->act_tcam)) {
  477. if (fbnic_bmc_present(fbd))
  478. continue;
  479. dev_warn_once(fbd->dev,
  480. "Found BMC MAC address w/ BMC not present\n");
  481. }
  482. fbnic_clear_macda_entry(fbd, idx);
  483. /* If rule was already destined for deletion just wipe it now */
  484. if (mac_addr->state == FBNIC_TCAM_S_DELETE) {
  485. memset(mac_addr, 0, sizeof(*mac_addr));
  486. continue;
  487. }
  488. /* Change state to update so that we will rewrite
  489. * this tcam the next time fbnic_write_macda is called.
  490. */
  491. mac_addr->state = FBNIC_TCAM_S_UPDATE;
  492. }
  493. }
  494. static void fbnic_clear_valid_macda(struct fbnic_dev *fbd)
  495. {
  496. int idx;
  497. for (idx = ARRAY_SIZE(fbd->mac_addr); idx--;) {
  498. struct fbnic_mac_addr *mac_addr = &fbd->mac_addr[idx];
  499. if (mac_addr->state == FBNIC_TCAM_S_VALID) {
  500. fbnic_clear_macda_entry(fbd, idx);
  501. mac_addr->state = FBNIC_TCAM_S_UPDATE;
  502. }
  503. }
  504. }
  505. static void fbnic_write_macda_entry(struct fbnic_dev *fbd, unsigned int idx,
  506. struct fbnic_mac_addr *mac_addr)
  507. {
  508. __be16 *mask, *value;
  509. int i;
  510. mask = &mac_addr->mask.addr16[FBNIC_RPC_TCAM_MACDA_WORD_LEN - 1];
  511. value = &mac_addr->value.addr16[FBNIC_RPC_TCAM_MACDA_WORD_LEN - 1];
  512. for (i = 0; i < FBNIC_RPC_TCAM_MACDA_WORD_LEN; i++)
  513. wr32(fbd, FBNIC_RPC_TCAM_MACDA(idx, i),
  514. FIELD_PREP(FBNIC_RPC_TCAM_MACDA_MASK, ntohs(*mask--)) |
  515. FIELD_PREP(FBNIC_RPC_TCAM_MACDA_VALUE, ntohs(*value--)));
  516. wrfl(fbd);
  517. wr32(fbd, FBNIC_RPC_TCAM_MACDA(idx, i), FBNIC_RPC_TCAM_VALIDATE);
  518. }
  519. void fbnic_write_macda(struct fbnic_dev *fbd)
  520. {
  521. int idx, updates = 0;
  522. for (idx = ARRAY_SIZE(fbd->mac_addr); idx--;) {
  523. struct fbnic_mac_addr *mac_addr = &fbd->mac_addr[idx];
  524. /* Check if update flag is set else exit. */
  525. if (!(mac_addr->state & FBNIC_TCAM_S_UPDATE))
  526. continue;
  527. /* Record update count */
  528. updates++;
  529. /* Clear by writing 0s. */
  530. if (mac_addr->state == FBNIC_TCAM_S_DELETE) {
  531. /* Invalidate entry and clear addr state info */
  532. fbnic_clear_macda_entry(fbd, idx);
  533. memset(mac_addr, 0, sizeof(*mac_addr));
  534. continue;
  535. }
  536. fbnic_write_macda_entry(fbd, idx, mac_addr);
  537. mac_addr->state = FBNIC_TCAM_S_VALID;
  538. }
  539. /* If reinitializing the BMC TCAM we are doing an initial update */
  540. if (fbd->fw_cap.need_bmc_tcam_reinit)
  541. updates++;
  542. /* If needed notify firmware of changes to MACDA TCAM */
  543. if (updates != 0 && fbnic_bmc_present(fbd))
  544. fbd->fw_cap.need_bmc_macda_sync = true;
  545. }
  546. static void fbnic_clear_act_tcam(struct fbnic_dev *fbd, unsigned int idx)
  547. {
  548. int i;
  549. /* Invalidate entry and clear addr state info */
  550. for (i = 0; i <= FBNIC_RPC_TCAM_ACT_WORD_LEN; i++)
  551. wr32(fbd, FBNIC_RPC_TCAM_ACT(idx, i), 0);
  552. }
  553. static void fbnic_clear_tce_tcam_entry(struct fbnic_dev *fbd, unsigned int idx)
  554. {
  555. int i;
  556. /* Invalidate entry and clear addr state info */
  557. for (i = 0; i <= FBNIC_TCE_TCAM_WORD_LEN; i++)
  558. wr32(fbd, FBNIC_TCE_RAM_TCAM(idx, i), 0);
  559. }
  560. static void fbnic_write_tce_tcam_dest(struct fbnic_dev *fbd, unsigned int idx,
  561. struct fbnic_mac_addr *mac_addr)
  562. {
  563. u32 dest = FBNIC_TCE_TCAM_DEST_BMC;
  564. u32 idx2dest_map;
  565. if (is_multicast_ether_addr(mac_addr->value.addr8))
  566. dest |= FBNIC_TCE_TCAM_DEST_MAC;
  567. idx2dest_map = rd32(fbd, FBNIC_TCE_TCAM_IDX2DEST_MAP);
  568. idx2dest_map &= ~(FBNIC_TCE_TCAM_IDX2DEST_MAP_DEST_ID_0 << (4 * idx));
  569. idx2dest_map |= dest << (4 * idx);
  570. wr32(fbd, FBNIC_TCE_TCAM_IDX2DEST_MAP, idx2dest_map);
  571. }
  572. static void fbnic_write_tce_tcam_entry(struct fbnic_dev *fbd, unsigned int idx,
  573. struct fbnic_mac_addr *mac_addr)
  574. {
  575. __be16 *mask, *value;
  576. int i;
  577. mask = &mac_addr->mask.addr16[FBNIC_TCE_TCAM_WORD_LEN - 1];
  578. value = &mac_addr->value.addr16[FBNIC_TCE_TCAM_WORD_LEN - 1];
  579. for (i = 0; i < FBNIC_TCE_TCAM_WORD_LEN; i++)
  580. wr32(fbd, FBNIC_TCE_RAM_TCAM(idx, i),
  581. FIELD_PREP(FBNIC_TCE_RAM_TCAM_MASK, ntohs(*mask--)) |
  582. FIELD_PREP(FBNIC_TCE_RAM_TCAM_VALUE, ntohs(*value--)));
  583. wrfl(fbd);
  584. wr32(fbd, FBNIC_TCE_RAM_TCAM3(idx), FBNIC_TCE_RAM_TCAM3_MCQ_MASK |
  585. FBNIC_TCE_RAM_TCAM3_DEST_MASK |
  586. FBNIC_TCE_RAM_TCAM3_VALIDATE);
  587. }
  588. static void __fbnic_write_tce_tcam_rev(struct fbnic_dev *fbd)
  589. {
  590. int tcam_idx = FBNIC_TCE_TCAM_NUM_ENTRIES;
  591. int mac_idx;
  592. for (mac_idx = ARRAY_SIZE(fbd->mac_addr); mac_idx--;) {
  593. struct fbnic_mac_addr *mac_addr = &fbd->mac_addr[mac_idx];
  594. /* Verify BMC bit is set */
  595. if (!test_bit(FBNIC_MAC_ADDR_T_BMC, mac_addr->act_tcam))
  596. continue;
  597. if (!tcam_idx) {
  598. dev_err(fbd->dev, "TCE TCAM overflow\n");
  599. return;
  600. }
  601. tcam_idx--;
  602. fbnic_write_tce_tcam_dest(fbd, tcam_idx, mac_addr);
  603. fbnic_write_tce_tcam_entry(fbd, tcam_idx, mac_addr);
  604. }
  605. while (tcam_idx)
  606. fbnic_clear_tce_tcam_entry(fbd, --tcam_idx);
  607. fbd->tce_tcam_last = tcam_idx;
  608. }
  609. static void __fbnic_write_tce_tcam(struct fbnic_dev *fbd)
  610. {
  611. int tcam_idx = 0;
  612. int mac_idx;
  613. for (mac_idx = 0; mac_idx < ARRAY_SIZE(fbd->mac_addr); mac_idx++) {
  614. struct fbnic_mac_addr *mac_addr = &fbd->mac_addr[mac_idx];
  615. /* Verify BMC bit is set */
  616. if (!test_bit(FBNIC_MAC_ADDR_T_BMC, mac_addr->act_tcam))
  617. continue;
  618. if (tcam_idx == FBNIC_TCE_TCAM_NUM_ENTRIES) {
  619. dev_err(fbd->dev, "TCE TCAM overflow\n");
  620. return;
  621. }
  622. fbnic_write_tce_tcam_dest(fbd, tcam_idx, mac_addr);
  623. fbnic_write_tce_tcam_entry(fbd, tcam_idx, mac_addr);
  624. tcam_idx++;
  625. }
  626. while (tcam_idx < FBNIC_TCE_TCAM_NUM_ENTRIES)
  627. fbnic_clear_tce_tcam_entry(fbd, tcam_idx++);
  628. fbd->tce_tcam_last = tcam_idx;
  629. }
  630. void fbnic_write_tce_tcam(struct fbnic_dev *fbd)
  631. {
  632. if (fbd->tce_tcam_last)
  633. __fbnic_write_tce_tcam_rev(fbd);
  634. else
  635. __fbnic_write_tce_tcam(fbd);
  636. }
  637. struct fbnic_ip_addr *__fbnic_ip4_sync(struct fbnic_dev *fbd,
  638. struct fbnic_ip_addr *ip_addr,
  639. const struct in_addr *addr,
  640. const struct in_addr *mask)
  641. {
  642. struct fbnic_ip_addr *avail_addr = NULL;
  643. unsigned int i;
  644. /* Scan from top of list to bottom, filling bottom up. */
  645. for (i = 0; i < FBNIC_RPC_TCAM_IP_ADDR_NUM_ENTRIES; i++, ip_addr++) {
  646. struct in6_addr *m = &ip_addr->mask;
  647. if (ip_addr->state == FBNIC_TCAM_S_DISABLED) {
  648. avail_addr = ip_addr;
  649. continue;
  650. }
  651. if (ip_addr->version != 4)
  652. continue;
  653. /* Drop avail_addr if mask is a subset of our current mask,
  654. * This prevents us from inserting a longer prefix behind a
  655. * shorter one.
  656. *
  657. * The mask is stored inverted value so as an example:
  658. * m ffff ffff ffff ffff ffff ffff ffff 0000 0000
  659. * mask 0000 0000 0000 0000 0000 0000 0000 ffff ffff
  660. *
  661. * "m" and "mask" represent typical IPv4 mask stored in
  662. * the TCAM and those provided by the stack. The code below
  663. * should return a non-zero result if there is a 0 stored
  664. * anywhere in "m" where "mask" has a 0.
  665. */
  666. if (~m->s6_addr32[3] & ~mask->s_addr) {
  667. avail_addr = NULL;
  668. continue;
  669. }
  670. /* Check to see if the mask actually contains fewer bits than
  671. * our new mask "m". The XOR below should only result in 0 if
  672. * "m" is masking a bit that we are looking for in our new
  673. * "mask", we eliminated the 0^0 case with the check above.
  674. *
  675. * If it contains fewer bits we need to stop here, otherwise
  676. * we might be adding an unreachable rule.
  677. */
  678. if (~(m->s6_addr32[3] ^ mask->s_addr))
  679. break;
  680. if (ip_addr->value.s6_addr32[3] == addr->s_addr) {
  681. avail_addr = ip_addr;
  682. break;
  683. }
  684. }
  685. if (avail_addr && avail_addr->state == FBNIC_TCAM_S_DISABLED) {
  686. ipv6_addr_set(&avail_addr->value, 0, 0, 0, addr->s_addr);
  687. ipv6_addr_set(&avail_addr->mask, htonl(~0), htonl(~0),
  688. htonl(~0), ~mask->s_addr);
  689. avail_addr->version = 4;
  690. avail_addr->state = FBNIC_TCAM_S_ADD;
  691. }
  692. return avail_addr;
  693. }
  694. struct fbnic_ip_addr *__fbnic_ip6_sync(struct fbnic_dev *fbd,
  695. struct fbnic_ip_addr *ip_addr,
  696. const struct in6_addr *addr,
  697. const struct in6_addr *mask)
  698. {
  699. struct fbnic_ip_addr *avail_addr = NULL;
  700. unsigned int i;
  701. ip_addr = &ip_addr[FBNIC_RPC_TCAM_IP_ADDR_NUM_ENTRIES - 1];
  702. /* Scan from bottom of list to top, filling top down. */
  703. for (i = FBNIC_RPC_TCAM_IP_ADDR_NUM_ENTRIES; i--; ip_addr--) {
  704. struct in6_addr *m = &ip_addr->mask;
  705. if (ip_addr->state == FBNIC_TCAM_S_DISABLED) {
  706. avail_addr = ip_addr;
  707. continue;
  708. }
  709. if (ip_addr->version != 6)
  710. continue;
  711. /* Drop avail_addr if mask is a superset of our current mask.
  712. * This prevents us from inserting a longer prefix behind a
  713. * shorter one.
  714. *
  715. * The mask is stored inverted value so as an example:
  716. * m 0000 0000 0000 0000 0000 0000 0000 0000 0000
  717. * mask ffff ffff ffff ffff ffff ffff ffff ffff ffff
  718. *
  719. * "m" and "mask" represent typical IPv6 mask stored in
  720. * the TCAM and those provided by the stack. The code below
  721. * should return a non-zero result which will cause us
  722. * to drop the avail_addr value that might be cached
  723. * to prevent us from dropping a v6 address behind it.
  724. */
  725. if ((m->s6_addr32[0] & mask->s6_addr32[0]) |
  726. (m->s6_addr32[1] & mask->s6_addr32[1]) |
  727. (m->s6_addr32[2] & mask->s6_addr32[2]) |
  728. (m->s6_addr32[3] & mask->s6_addr32[3])) {
  729. avail_addr = NULL;
  730. continue;
  731. }
  732. /* The previous test eliminated any overlap between the
  733. * two values so now we need to check for gaps.
  734. *
  735. * If the mask is equal to our current mask then it should
  736. * result with m ^ mask = ffff ffff, if however the value
  737. * stored in m is bigger then we should see a 0 appear
  738. * somewhere in the mask.
  739. */
  740. if (~(m->s6_addr32[0] ^ mask->s6_addr32[0]) |
  741. ~(m->s6_addr32[1] ^ mask->s6_addr32[1]) |
  742. ~(m->s6_addr32[2] ^ mask->s6_addr32[2]) |
  743. ~(m->s6_addr32[3] ^ mask->s6_addr32[3]))
  744. break;
  745. if (ipv6_addr_cmp(&ip_addr->value, addr))
  746. continue;
  747. avail_addr = ip_addr;
  748. break;
  749. }
  750. if (avail_addr && avail_addr->state == FBNIC_TCAM_S_DISABLED) {
  751. memcpy(&avail_addr->value, addr, sizeof(*addr));
  752. ipv6_addr_set(&avail_addr->mask,
  753. ~mask->s6_addr32[0], ~mask->s6_addr32[1],
  754. ~mask->s6_addr32[2], ~mask->s6_addr32[3]);
  755. avail_addr->version = 6;
  756. avail_addr->state = FBNIC_TCAM_S_ADD;
  757. }
  758. return avail_addr;
  759. }
  760. int __fbnic_ip_unsync(struct fbnic_ip_addr *ip_addr, unsigned int tcam_idx)
  761. {
  762. if (!test_and_clear_bit(tcam_idx, ip_addr->act_tcam))
  763. return -ENOENT;
  764. if (bitmap_empty(ip_addr->act_tcam, FBNIC_RPC_TCAM_ACT_NUM_ENTRIES))
  765. ip_addr->state = FBNIC_TCAM_S_DELETE;
  766. return 0;
  767. }
  768. static void fbnic_clear_ip_src_entry(struct fbnic_dev *fbd, unsigned int idx)
  769. {
  770. int i;
  771. /* Invalidate entry and clear addr state info */
  772. for (i = 0; i <= FBNIC_RPC_TCAM_IP_ADDR_WORD_LEN; i++)
  773. wr32(fbd, FBNIC_RPC_TCAM_IPSRC(idx, i), 0);
  774. }
  775. static void fbnic_clear_ip_dst_entry(struct fbnic_dev *fbd, unsigned int idx)
  776. {
  777. int i;
  778. /* Invalidate entry and clear addr state info */
  779. for (i = 0; i <= FBNIC_RPC_TCAM_IP_ADDR_WORD_LEN; i++)
  780. wr32(fbd, FBNIC_RPC_TCAM_IPDST(idx, i), 0);
  781. }
  782. static void fbnic_clear_ip_outer_src_entry(struct fbnic_dev *fbd,
  783. unsigned int idx)
  784. {
  785. int i;
  786. /* Invalidate entry and clear addr state info */
  787. for (i = 0; i <= FBNIC_RPC_TCAM_IP_ADDR_WORD_LEN; i++)
  788. wr32(fbd, FBNIC_RPC_TCAM_OUTER_IPSRC(idx, i), 0);
  789. }
  790. static void fbnic_clear_ip_outer_dst_entry(struct fbnic_dev *fbd,
  791. unsigned int idx)
  792. {
  793. int i;
  794. /* Invalidate entry and clear addr state info */
  795. for (i = 0; i <= FBNIC_RPC_TCAM_IP_ADDR_WORD_LEN; i++)
  796. wr32(fbd, FBNIC_RPC_TCAM_OUTER_IPDST(idx, i), 0);
  797. }
  798. static void fbnic_write_ip_src_entry(struct fbnic_dev *fbd, unsigned int idx,
  799. struct fbnic_ip_addr *ip_addr)
  800. {
  801. __be16 *mask, *value;
  802. int i;
  803. mask = &ip_addr->mask.s6_addr16[FBNIC_RPC_TCAM_IP_ADDR_WORD_LEN - 1];
  804. value = &ip_addr->value.s6_addr16[FBNIC_RPC_TCAM_IP_ADDR_WORD_LEN - 1];
  805. for (i = 0; i < FBNIC_RPC_TCAM_IP_ADDR_WORD_LEN; i++)
  806. wr32(fbd, FBNIC_RPC_TCAM_IPSRC(idx, i),
  807. FIELD_PREP(FBNIC_RPC_TCAM_IP_ADDR_MASK, ntohs(*mask--)) |
  808. FIELD_PREP(FBNIC_RPC_TCAM_IP_ADDR_VALUE, ntohs(*value--)));
  809. wrfl(fbd);
  810. /* Bit 129 is used to flag for v4/v6 */
  811. wr32(fbd, FBNIC_RPC_TCAM_IPSRC(idx, i),
  812. (ip_addr->version == 6) | FBNIC_RPC_TCAM_VALIDATE);
  813. }
  814. static void fbnic_write_ip_dst_entry(struct fbnic_dev *fbd, unsigned int idx,
  815. struct fbnic_ip_addr *ip_addr)
  816. {
  817. __be16 *mask, *value;
  818. int i;
  819. mask = &ip_addr->mask.s6_addr16[FBNIC_RPC_TCAM_IP_ADDR_WORD_LEN - 1];
  820. value = &ip_addr->value.s6_addr16[FBNIC_RPC_TCAM_IP_ADDR_WORD_LEN - 1];
  821. for (i = 0; i < FBNIC_RPC_TCAM_IP_ADDR_WORD_LEN; i++)
  822. wr32(fbd, FBNIC_RPC_TCAM_IPDST(idx, i),
  823. FIELD_PREP(FBNIC_RPC_TCAM_IP_ADDR_MASK, ntohs(*mask--)) |
  824. FIELD_PREP(FBNIC_RPC_TCAM_IP_ADDR_VALUE, ntohs(*value--)));
  825. wrfl(fbd);
  826. /* Bit 129 is used to flag for v4/v6 */
  827. wr32(fbd, FBNIC_RPC_TCAM_IPDST(idx, i),
  828. (ip_addr->version == 6) | FBNIC_RPC_TCAM_VALIDATE);
  829. }
  830. static void fbnic_write_ip_outer_src_entry(struct fbnic_dev *fbd,
  831. unsigned int idx,
  832. struct fbnic_ip_addr *ip_addr)
  833. {
  834. __be16 *mask, *value;
  835. int i;
  836. mask = &ip_addr->mask.s6_addr16[FBNIC_RPC_TCAM_IP_ADDR_WORD_LEN - 1];
  837. value = &ip_addr->value.s6_addr16[FBNIC_RPC_TCAM_IP_ADDR_WORD_LEN - 1];
  838. for (i = 0; i < FBNIC_RPC_TCAM_IP_ADDR_WORD_LEN; i++)
  839. wr32(fbd, FBNIC_RPC_TCAM_OUTER_IPSRC(idx, i),
  840. FIELD_PREP(FBNIC_RPC_TCAM_IP_ADDR_MASK, ntohs(*mask--)) |
  841. FIELD_PREP(FBNIC_RPC_TCAM_IP_ADDR_VALUE, ntohs(*value--)));
  842. wrfl(fbd);
  843. wr32(fbd, FBNIC_RPC_TCAM_OUTER_IPSRC(idx, i), FBNIC_RPC_TCAM_VALIDATE);
  844. }
  845. static void fbnic_write_ip_outer_dst_entry(struct fbnic_dev *fbd,
  846. unsigned int idx,
  847. struct fbnic_ip_addr *ip_addr)
  848. {
  849. __be16 *mask, *value;
  850. int i;
  851. mask = &ip_addr->mask.s6_addr16[FBNIC_RPC_TCAM_IP_ADDR_WORD_LEN - 1];
  852. value = &ip_addr->value.s6_addr16[FBNIC_RPC_TCAM_IP_ADDR_WORD_LEN - 1];
  853. for (i = 0; i < FBNIC_RPC_TCAM_IP_ADDR_WORD_LEN; i++)
  854. wr32(fbd, FBNIC_RPC_TCAM_OUTER_IPDST(idx, i),
  855. FIELD_PREP(FBNIC_RPC_TCAM_IP_ADDR_MASK, ntohs(*mask--)) |
  856. FIELD_PREP(FBNIC_RPC_TCAM_IP_ADDR_VALUE, ntohs(*value--)));
  857. wrfl(fbd);
  858. wr32(fbd, FBNIC_RPC_TCAM_OUTER_IPDST(idx, i), FBNIC_RPC_TCAM_VALIDATE);
  859. }
  860. void fbnic_write_ip_addr(struct fbnic_dev *fbd)
  861. {
  862. int idx;
  863. for (idx = ARRAY_SIZE(fbd->ip_src); idx--;) {
  864. struct fbnic_ip_addr *ip_addr = &fbd->ip_src[idx];
  865. /* Check if update flag is set else skip. */
  866. if (!(ip_addr->state & FBNIC_TCAM_S_UPDATE))
  867. continue;
  868. /* Clear by writing 0s. */
  869. if (ip_addr->state == FBNIC_TCAM_S_DELETE) {
  870. /* Invalidate entry and clear addr state info */
  871. fbnic_clear_ip_src_entry(fbd, idx);
  872. memset(ip_addr, 0, sizeof(*ip_addr));
  873. continue;
  874. }
  875. fbnic_write_ip_src_entry(fbd, idx, ip_addr);
  876. ip_addr->state = FBNIC_TCAM_S_VALID;
  877. }
  878. /* Repeat process for other IP TCAMs */
  879. for (idx = ARRAY_SIZE(fbd->ip_dst); idx--;) {
  880. struct fbnic_ip_addr *ip_addr = &fbd->ip_dst[idx];
  881. if (!(ip_addr->state & FBNIC_TCAM_S_UPDATE))
  882. continue;
  883. if (ip_addr->state == FBNIC_TCAM_S_DELETE) {
  884. fbnic_clear_ip_dst_entry(fbd, idx);
  885. memset(ip_addr, 0, sizeof(*ip_addr));
  886. continue;
  887. }
  888. fbnic_write_ip_dst_entry(fbd, idx, ip_addr);
  889. ip_addr->state = FBNIC_TCAM_S_VALID;
  890. }
  891. for (idx = ARRAY_SIZE(fbd->ipo_src); idx--;) {
  892. struct fbnic_ip_addr *ip_addr = &fbd->ipo_src[idx];
  893. if (!(ip_addr->state & FBNIC_TCAM_S_UPDATE))
  894. continue;
  895. if (ip_addr->state == FBNIC_TCAM_S_DELETE) {
  896. fbnic_clear_ip_outer_src_entry(fbd, idx);
  897. memset(ip_addr, 0, sizeof(*ip_addr));
  898. continue;
  899. }
  900. fbnic_write_ip_outer_src_entry(fbd, idx, ip_addr);
  901. ip_addr->state = FBNIC_TCAM_S_VALID;
  902. }
  903. for (idx = ARRAY_SIZE(fbd->ipo_dst); idx--;) {
  904. struct fbnic_ip_addr *ip_addr = &fbd->ipo_dst[idx];
  905. if (!(ip_addr->state & FBNIC_TCAM_S_UPDATE))
  906. continue;
  907. if (ip_addr->state == FBNIC_TCAM_S_DELETE) {
  908. fbnic_clear_ip_outer_dst_entry(fbd, idx);
  909. memset(ip_addr, 0, sizeof(*ip_addr));
  910. continue;
  911. }
  912. fbnic_write_ip_outer_dst_entry(fbd, idx, ip_addr);
  913. ip_addr->state = FBNIC_TCAM_S_VALID;
  914. }
  915. }
  916. static void fbnic_clear_valid_act_tcam(struct fbnic_dev *fbd)
  917. {
  918. int i = FBNIC_RPC_TCAM_ACT_NUM_ENTRIES - 1;
  919. struct fbnic_act_tcam *act_tcam;
  920. /* Work from the bottom up deleting all other rules from hardware */
  921. do {
  922. act_tcam = &fbd->act_tcam[i];
  923. if (act_tcam->state != FBNIC_TCAM_S_VALID)
  924. continue;
  925. fbnic_clear_act_tcam(fbd, i);
  926. act_tcam->state = FBNIC_TCAM_S_UPDATE;
  927. } while (i--);
  928. }
  929. void fbnic_clear_rules(struct fbnic_dev *fbd)
  930. {
  931. /* Clear MAC rules */
  932. fbnic_clear_macda(fbd);
  933. /* If BMC is present we need to preserve the last rule which
  934. * will be used to route traffic to the BMC if it is received.
  935. *
  936. * At this point it should be the only MAC address in the MACDA
  937. * so any unicast or multicast traffic received should be routed
  938. * to it. So leave the last rule in place.
  939. *
  940. * It will be rewritten to add the host again when we bring
  941. * the interface back up.
  942. */
  943. if (fbnic_bmc_present(fbd)) {
  944. u32 dest = FIELD_PREP(FBNIC_RPC_ACT_TBL0_DEST_MASK,
  945. FBNIC_RPC_ACT_TBL0_DEST_BMC);
  946. int i = FBNIC_RPC_TCAM_ACT_NUM_ENTRIES - 1;
  947. struct fbnic_act_tcam *act_tcam;
  948. act_tcam = &fbd->act_tcam[i];
  949. if (act_tcam->state == FBNIC_TCAM_S_VALID &&
  950. (act_tcam->dest & dest)) {
  951. wr32(fbd, FBNIC_RPC_ACT_TBL0(i), dest);
  952. wr32(fbd, FBNIC_RPC_ACT_TBL1(i), 0);
  953. act_tcam->state = FBNIC_TCAM_S_UPDATE;
  954. }
  955. }
  956. fbnic_clear_valid_act_tcam(fbd);
  957. }
  958. static void fbnic_delete_act_tcam(struct fbnic_dev *fbd, unsigned int idx)
  959. {
  960. fbnic_clear_act_tcam(fbd, idx);
  961. memset(&fbd->act_tcam[idx], 0, sizeof(struct fbnic_act_tcam));
  962. }
  963. static void fbnic_update_act_tcam(struct fbnic_dev *fbd, unsigned int idx)
  964. {
  965. struct fbnic_act_tcam *act_tcam = &fbd->act_tcam[idx];
  966. int i;
  967. /* Update entry by writing the destination and RSS mask */
  968. wr32(fbd, FBNIC_RPC_ACT_TBL0(idx), act_tcam->dest);
  969. wr32(fbd, FBNIC_RPC_ACT_TBL1(idx), act_tcam->rss_en_mask);
  970. /* Write new TCAM rule to hardware */
  971. for (i = 0; i < FBNIC_RPC_TCAM_ACT_WORD_LEN; i++)
  972. wr32(fbd, FBNIC_RPC_TCAM_ACT(idx, i),
  973. FIELD_PREP(FBNIC_RPC_TCAM_ACT_MASK,
  974. act_tcam->mask.tcam[i]) |
  975. FIELD_PREP(FBNIC_RPC_TCAM_ACT_VALUE,
  976. act_tcam->value.tcam[i]));
  977. wrfl(fbd);
  978. wr32(fbd, FBNIC_RPC_TCAM_ACT(idx, i), FBNIC_RPC_TCAM_VALIDATE);
  979. act_tcam->state = FBNIC_TCAM_S_VALID;
  980. }
  981. void fbnic_write_rules(struct fbnic_dev *fbd)
  982. {
  983. int i;
  984. /* Flush any pending action table rules */
  985. for (i = 0; i < FBNIC_RPC_ACT_TBL_NUM_ENTRIES; i++) {
  986. struct fbnic_act_tcam *act_tcam = &fbd->act_tcam[i];
  987. /* Check if update flag is set else exit. */
  988. if (!(act_tcam->state & FBNIC_TCAM_S_UPDATE))
  989. continue;
  990. if (act_tcam->state == FBNIC_TCAM_S_DELETE)
  991. fbnic_delete_act_tcam(fbd, i);
  992. else
  993. fbnic_update_act_tcam(fbd, i);
  994. }
  995. }
  996. void fbnic_rpc_reset_valid_entries(struct fbnic_dev *fbd)
  997. {
  998. fbnic_clear_valid_act_tcam(fbd);
  999. fbnic_clear_valid_macda(fbd);
  1000. }