summaryrefslogtreecommitdiffstats
path: root/src/spdk/dpdk/drivers/net/octeontx2/otx2_mcast.c
blob: f84aa1bf570c33f68d13bfd7e8bb81850ff6e061 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/* SPDX-License-Identifier: BSD-3-Clause
 * Copyright(C) 2019 Marvell International Ltd.
 */

#include "otx2_ethdev.h"

static int
nix_mc_addr_list_free(struct otx2_eth_dev *dev, uint32_t entry_count)
{
	struct npc_mcam_free_entry_req *req;
	struct otx2_mbox *mbox = dev->mbox;
	struct mcast_entry *entry;
	int rc = 0;

	if (entry_count == 0)
		goto exit;

	TAILQ_FOREACH(entry, &dev->mc_fltr_tbl, next) {
		req = otx2_mbox_alloc_msg_npc_mcam_free_entry(mbox);
		req->entry = entry->mcam_index;

		rc = otx2_mbox_process_msg(mbox, NULL);
		if (rc < 0)
			goto exit;

		TAILQ_REMOVE(&dev->mc_fltr_tbl, entry, next);
		rte_free(entry);
		entry_count--;

		if (entry_count == 0)
			break;
	}

	if (entry == NULL)
		dev->mc_tbl_set = false;

exit:
	return rc;
}

static int
nix_hw_update_mc_addr_list(struct rte_eth_dev *eth_dev)
{
	struct otx2_eth_dev *dev = otx2_eth_pmd_priv(eth_dev);
	struct otx2_npc_flow_info *npc = &dev->npc_flow;
	volatile uint8_t *key_data, *key_mask;
	struct npc_mcam_write_entry_req *req;
	struct otx2_mbox *mbox = dev->mbox;
	struct npc_xtract_info *x_info;
	uint64_t mcam_data, mcam_mask;
	struct mcast_entry *entry;
	otx2_dxcfg_t *ld_cfg;
	uint8_t *mac_addr;
	uint64_t action;
	int idx, rc = 0;

	ld_cfg = &npc->prx_dxcfg;
	/* Get ETH layer profile info for populating mcam entries */
	x_info = &(*ld_cfg)[NPC_MCAM_RX][NPC_LID_LA][NPC_LT_LA_ETHER].xtract[0];

	TAILQ_FOREACH(entry, &dev->mc_fltr_tbl, next) {
		req = otx2_mbox_alloc_msg_npc_mcam_write_entry(mbox);
		if (req == NULL) {
			/* The mbox memory buffer can be full.
			 * Flush it and retry
			 */
			otx2_mbox_msg_send(mbox, 0);
			rc = otx2_mbox_wait_for_rsp(mbox, 0);
			if (rc < 0)
				goto exit;

			req = otx2_mbox_alloc_msg_npc_mcam_write_entry(mbox);
			if (req == NULL) {
				rc = -ENOMEM;
				goto exit;
			}
		}
		req->entry = entry->mcam_index;
		req->intf = NPC_MCAM_RX;
		req->enable_entry = 1;

		/* Channel base extracted to KW0[11:0] */
		req->entry_data.kw[0] = dev->rx_chan_base;
		req->entry_data.kw_mask[0] = RTE_LEN2MASK(12, uint64_t);

		/* Update mcam address */
		key_data = (volatile uint8_t *)req->entry_data.kw;
		key_mask = (volatile uint8_t *)req->entry_data.kw_mask;

		mcam_data = 0ull;
		mcam_mask = RTE_LEN2MASK(48, uint64_t);
		mac_addr = &entry->mcast_mac.addr_bytes[0];
		for (idx = RTE_ETHER_ADDR_LEN - 1; idx >= 0; idx--)
			mcam_data |= ((uint64_t)*mac_addr++) << (8 * idx);

		otx2_mbox_memcpy(key_data + x_info->key_off,
				 &mcam_data, x_info->len);
		otx2_mbox_memcpy(key_mask + x_info->key_off,
				 &mcam_mask, x_info->len);

		action = NIX_RX_ACTIONOP_UCAST;

		if (eth_dev->data->dev_conf.rxmode.mq_mode == ETH_MQ_RX_RSS) {
			action = NIX_RX_ACTIONOP_RSS;
			action |= (uint64_t)(dev->rss_info.alg_idx) << 56;
		}

		action |= ((uint64_t)otx2_pfvf_func(dev->pf, dev->vf)) << 4;
		req->entry_data.action = action;
	}

	otx2_mbox_msg_send(mbox, 0);
	rc = otx2_mbox_wait_for_rsp(mbox, 0);

exit:
	return rc;
}

int
otx2_nix_mc_addr_list_install(struct rte_eth_dev *eth_dev)
{
	struct otx2_eth_dev *dev = otx2_eth_pmd_priv(eth_dev);
	struct npc_mcam_alloc_entry_req *req;
	struct npc_mcam_alloc_entry_rsp *rsp;
	struct otx2_mbox *mbox = dev->mbox;
	uint32_t entry_count = 0, idx  = 0;
	struct mcast_entry *entry;
	int rc = 0;

	if (!dev->mc_tbl_set)
		return 0;

	TAILQ_FOREACH(entry, &dev->mc_fltr_tbl, next)
		entry_count++;

	req = otx2_mbox_alloc_msg_npc_mcam_alloc_entry(mbox);
	req->priority = NPC_MCAM_ANY_PRIO;
	req->count = entry_count;

	rc = otx2_mbox_process_msg(mbox, (void *)&rsp);
	if (rc || rsp->count  < entry_count) {
		otx2_err("Failed to allocate required mcam entries");
		goto exit;
	}

	TAILQ_FOREACH(entry, &dev->mc_fltr_tbl, next)
		entry->mcam_index = rsp->entry_list[idx];

	rc = nix_hw_update_mc_addr_list(eth_dev);

exit:
	return rc;
}

int
otx2_nix_mc_addr_list_uninstall(struct rte_eth_dev *eth_dev)
{
	struct otx2_eth_dev *dev = otx2_eth_pmd_priv(eth_dev);
	struct npc_mcam_free_entry_req *req;
	struct otx2_mbox *mbox = dev->mbox;
	struct mcast_entry *entry;
	int rc = 0;

	if (!dev->mc_tbl_set)
		return 0;

	TAILQ_FOREACH(entry, &dev->mc_fltr_tbl, next) {
		req = otx2_mbox_alloc_msg_npc_mcam_free_entry(mbox);
		if (req == NULL) {
			otx2_mbox_msg_send(mbox, 0);
			rc = otx2_mbox_wait_for_rsp(mbox, 0);
			if (rc < 0)
				goto exit;

			req = otx2_mbox_alloc_msg_npc_mcam_free_entry(mbox);
			if (req == NULL) {
				rc = -ENOMEM;
				goto exit;
			}
		}
		req->entry = entry->mcam_index;
	}

	otx2_mbox_msg_send(mbox, 0);
	rc = otx2_mbox_wait_for_rsp(mbox, 0);

exit:
	return rc;
}

static int
nix_setup_mc_addr_list(struct otx2_eth_dev *dev,
		       struct rte_ether_addr *mc_addr_set)
{
	struct npc_mcam_ena_dis_entry_req *req;
	struct otx2_mbox *mbox = dev->mbox;
	struct mcast_entry *entry;
	uint32_t idx = 0;
	int rc = 0;

	/* Populate PMD's mcast list with given mcast mac addresses and
	 * disable all mcam entries pertaining to the mcast list.
	 */
	TAILQ_FOREACH(entry, &dev->mc_fltr_tbl, next) {
		rte_memcpy(&entry->mcast_mac, &mc_addr_set[idx++],
			   RTE_ETHER_ADDR_LEN);

		req = otx2_mbox_alloc_msg_npc_mcam_dis_entry(mbox);
		if (req == NULL) {
			otx2_mbox_msg_send(mbox, 0);
			rc = otx2_mbox_wait_for_rsp(mbox, 0);
			if (rc < 0)
				goto exit;

			req = otx2_mbox_alloc_msg_npc_mcam_dis_entry(mbox);
			if (req == NULL) {
				rc = -ENOMEM;
				goto exit;
			}
		}
		req->entry = entry->mcam_index;
	}

	otx2_mbox_msg_send(mbox, 0);
	rc = otx2_mbox_wait_for_rsp(mbox, 0);

exit:
	return rc;
}

int
otx2_nix_set_mc_addr_list(struct rte_eth_dev *eth_dev,
			  struct rte_ether_addr *mc_addr_set,
			  uint32_t nb_mc_addr)
{
	struct otx2_eth_dev *dev = otx2_eth_pmd_priv(eth_dev);
	struct npc_mcam_alloc_entry_req *req;
	struct npc_mcam_alloc_entry_rsp *rsp;
	struct otx2_mbox *mbox = dev->mbox;
	uint32_t idx, priv_count = 0;
	struct mcast_entry *entry;
	int rc = 0;

	if (otx2_dev_is_vf(dev))
		return -ENOTSUP;

	TAILQ_FOREACH(entry, &dev->mc_fltr_tbl, next)
		priv_count++;

	if (nb_mc_addr == 0 || mc_addr_set == NULL) {
		/* Free existing list if new list is null */
		nb_mc_addr = priv_count;
		goto exit;
	}

	for (idx = 0; idx < nb_mc_addr; idx++) {
		if (!rte_is_multicast_ether_addr(&mc_addr_set[idx]))
			return -EINVAL;
	}

	/* New list is bigger than the existing list,
	 * allocate mcam entries for the extra entries.
	 */
	if (nb_mc_addr > priv_count) {
		req = otx2_mbox_alloc_msg_npc_mcam_alloc_entry(mbox);
		req->priority = NPC_MCAM_ANY_PRIO;
		req->count = nb_mc_addr - priv_count;

		rc = otx2_mbox_process_msg(mbox, (void *)&rsp);
		if (rc || (rsp->count + priv_count < nb_mc_addr)) {
			otx2_err("Failed to allocate required entries");
			nb_mc_addr = priv_count;
			goto exit;
		}

		/* Append new mcam entries to the existing mc list */
		for (idx = 0; idx < rsp->count; idx++) {
			entry = rte_zmalloc("otx2_nix_mc_entry",
					    sizeof(struct mcast_entry), 0);
			if (!entry) {
				otx2_err("Failed to allocate memory");
				nb_mc_addr = priv_count;
				rc = -ENOMEM;
				goto exit;
			}
			entry->mcam_index = rsp->entry_list[idx];
			TAILQ_INSERT_HEAD(&dev->mc_fltr_tbl, entry, next);
		}
	} else {
		/* Free the extra mcam entries if the new list is smaller
		 * than exiting list.
		 */
		nix_mc_addr_list_free(dev, priv_count - nb_mc_addr);
	}


	/* Now mc_fltr_tbl has the required number of mcam entries,
	 * Traverse through it and add new multicast filter table entries.
	 */
	rc = nix_setup_mc_addr_list(dev, mc_addr_set);
	if (rc < 0)
		goto exit;

	rc = nix_hw_update_mc_addr_list(eth_dev);
	if (rc < 0)
		goto exit;

	dev->mc_tbl_set = true;

	return 0;

exit:
	nix_mc_addr_list_free(dev, nb_mc_addr);
	return rc;
}

void
otx2_nix_mc_filter_init(struct otx2_eth_dev *dev)
{
	if (otx2_dev_is_vf(dev))
		return;

	TAILQ_INIT(&dev->mc_fltr_tbl);
}

void
otx2_nix_mc_filter_fini(struct otx2_eth_dev *dev)
{
	struct mcast_entry *entry;
	uint32_t count = 0;

	if (otx2_dev_is_vf(dev))
		return;

	TAILQ_FOREACH(entry, &dev->mc_fltr_tbl, next)
		count++;

	nix_mc_addr_list_free(dev, count);
}