Debian Codegraph @

drivers/net/ethernet/marvell/octeontx2/nic/otx2_flows.c

raw symbols SCIP
1 // SPDX-License-Identifier: GPL-2.0
2 /* Marvell RVU Ethernet driver
3 *
4 * Copyright (C) 2020 Marvell.
5 *
6 */
7
8 #include <net/ipv6.h>
9 #include <linux/sort.h>
10
11 #include "otx2_common.h"
12
13 #define OTX2_DEFAULT_ACTION 0x1
14
15 struct otx2_flow {
16 struct ethtool_rx_flow_spec flow_spec;
17 struct list_head list;
18 u32 location;
19 u32 entry;
20 bool is_vf;
21 u8 rss_ctx_id;
22 #define DMAC_FILTER_RULE BIT(0)
23 #define PFC_FLOWCTRL_RULE BIT(1)
24 u16 rule_type;
25 int vf;
26 };
27
28 enum dmac_req {
29 DMAC_ADDR_UPDATE,
30 DMAC_ADDR_DEL
31 };
32
33 static void otx2_clear_ntuple_flow_info(struct otx2_nic *pfvf, struct otx2_flow_config *flow_cfg)
34 {
35 devm_kfree(pfvf->dev, flow_cfg->flow_ent);
36 flow_cfg->flow_ent = NULL;
37 flow_cfg->max_flows = 0;
38 }
39
40 static int otx2_mcam_pfl_info_get(struct otx2_nic *pfvf, bool *is_x2,
41 u16 *x4_slots)
42 {
43 struct npc_get_pfl_info_rsp *rsp;
44 struct msg_req *req;
45 static struct {
46 bool is_set;
47 bool is_x2;
48 u16 x4_slots;
49 } pfl_info;
50
51 /* Avoid sending mboxes for constant information
52 * like x4_slots
53 */
54 mutex_lock(&pfvf->mbox.lock);
55 if (pfl_info.is_set) {
56 *is_x2 = pfl_info.is_x2;
57 *x4_slots = pfl_info.x4_slots;
58 mutex_unlock(&pfvf->mbox.lock);
59 return 0;
60 }
61
62 req = otx2_mbox_alloc_msg_npc_get_pfl_info(&pfvf->mbox);
63 if (!req) {
64 mutex_unlock(&pfvf->mbox.lock);
65 return -ENOMEM;
66 }
67
68 /* Send message to AF */
69 if (otx2_sync_mbox_msg(&pfvf->mbox)) {
70 mutex_unlock(&pfvf->mbox.lock);
71 return -EFAULT;
72 }
73
74 rsp = (struct npc_get_pfl_info_rsp *)otx2_mbox_get_rsp
75 (&pfvf->mbox.mbox, 0, &req->hdr);
76
77 if (IS_ERR(rsp)) {
78 mutex_unlock(&pfvf->mbox.lock);
79 return -EFAULT;
80 }
81
82 *is_x2 = (rsp->kw_type == NPC_MCAM_KEY_X2);
83 if (*is_x2)
84 *x4_slots = 0;
85 else
86 *x4_slots = rsp->x4_slots;
87
88 pfl_info.is_x2 = *is_x2;
89 pfl_info.x4_slots = *x4_slots;
90 pfl_info.is_set = true;
91
92 mutex_unlock(&pfvf->mbox.lock);
93 return 0;
94 }
95
96 static int otx2_get_dft_rl_idx(struct otx2_nic *pfvf, u16 *mcam_idx)
97 {
98 struct npc_get_dft_rl_idxs_rsp *rsp;
99 struct msg_req *req;
100
101 mutex_lock(&pfvf->mbox.lock);
102
103 req = otx2_mbox_alloc_msg_npc_get_dft_rl_idxs(&pfvf->mbox);
104 if (!req) {
105 mutex_unlock(&pfvf->mbox.lock);
106 return -ENOMEM;
107 }
108
109 /* Send message to AF */
110 if (otx2_sync_mbox_msg(&pfvf->mbox)) {
111 mutex_unlock(&pfvf->mbox.lock);
112 return -EINVAL;
113 }
114
115 rsp = (struct npc_get_dft_rl_idxs_rsp *)otx2_mbox_get_rsp
116 (&pfvf->mbox.mbox, 0, &req->hdr);
117
118 if (IS_ERR(rsp)) {
119 mutex_unlock(&pfvf->mbox.lock);
120 return -EFAULT;
121 }
122
123 if (is_otx2_lbkvf(pfvf->pdev))
124 *mcam_idx = rsp->promisc;
125 else
126 *mcam_idx = rsp->ucast;
127
128 mutex_unlock(&pfvf->mbox.lock);
129 return 0;
130 }
131
132 static int otx2_free_ntuple_mcam_entries(struct otx2_nic *pfvf)
133 {
134 struct otx2_flow_config *flow_cfg = pfvf->flow_cfg;
135 struct npc_mcam_free_entry_req *req;
136 int ent, err;
137
138 if (!flow_cfg->max_flows)
139 return 0;
140
141 mutex_lock(&pfvf->mbox.lock);
142 for (ent = 0; ent < flow_cfg->max_flows; ent++) {
143 req = otx2_mbox_alloc_msg_npc_mcam_free_entry(&pfvf->mbox);
144 if (!req)
145 break;
146
147 req->entry = flow_cfg->flow_ent[ent];
148
149 /* Send message to AF to free MCAM entries */
150 err = otx2_sync_mbox_msg(&pfvf->mbox);
151 if (err)
152 break;
153 }
154 mutex_unlock(&pfvf->mbox.lock);
155 otx2_clear_ntuple_flow_info(pfvf, flow_cfg);
156 return 0;
157 }
158
159 int otx2_alloc_mcam_entries(struct otx2_nic *pfvf, u16 count)
160 {
161 struct otx2_flow_config *flow_cfg = pfvf->flow_cfg;
162 struct npc_mcam_alloc_entry_req *req;
163 struct npc_mcam_alloc_entry_rsp *rsp;
164 u16 dft_idx = 0, x4_slots = 0;
165 int ent, allocated = 0, ref;
166 bool is_x2 = false;
167 int rc;
168
169 /* Free current ones and allocate new ones with requested count */
170 otx2_free_ntuple_mcam_entries(pfvf);
171
172 if (!count)
173 return 0;
174
175 flow_cfg->flow_ent = devm_kmalloc_array(pfvf->dev, count,
176 sizeof(u16), GFP_KERNEL);
177 if (!flow_cfg->flow_ent) {
178 netdev_err(pfvf->netdev,
179 "%s: Unable to allocate memory for flow entries\n",
180 __func__);
181 return -ENOMEM;
182 }
183
184 if (is_cn20k(pfvf->pdev)) {
185 rc = otx2_mcam_pfl_info_get(pfvf, &is_x2, &x4_slots);
186 if (rc) {
187 netdev_err(pfvf->netdev, "Error to retrieve profile info\n");
188 return rc;
189 }
190
191 rc = otx2_get_dft_rl_idx(pfvf, &dft_idx);
192 if (rc) {
193 netdev_err(pfvf->netdev,
194 "Error to retrieve ucast mcam idx for pcifunc %#x\n",
195 pfvf->pcifunc);
196 return rc;
197 }
198 }
199
200 mutex_lock(&pfvf->mbox.lock);
201
202 /* In a single request a max of NPC_MAX_NONCONTIG_ENTRIES MCAM entries
203 * can only be allocated.
204 */
205 while (allocated < count) {
206 req = otx2_mbox_alloc_msg_npc_mcam_alloc_entry(&pfvf->mbox);
207 if (!req)
208 goto exit;
209
210 req->kw_type = is_x2 ? NPC_MCAM_KEY_X2 : NPC_MCAM_KEY_X4;
211 req->contig = false;
212 req->count = (count - allocated) > NPC_MAX_NONCONTIG_ENTRIES ?
213 NPC_MAX_NONCONTIG_ENTRIES : count - allocated;
214
215 ref = 0;
216
217 if (is_cn20k(pfvf->pdev)) {
218 req->ref_prio = NPC_MCAM_HIGHER_PRIO;
219 ref = dft_idx;
220 }
221
222 /* Allocate higher priority entries for PFs, so that VF's entries
223 * will be on top of PF.
224 */
225 if (!is_otx2_vf(pfvf->pcifunc)) {
226 req->ref_prio = NPC_MCAM_HIGHER_PRIO;
227 ref = flow_cfg->def_ent[0];
228 }
229
230 if (is_cn20k(pfvf->pdev))
231 ref = is_x2 ? ref : ref & (x4_slots - 1);
232
233 req->ref_entry = ref;
234
235 /* Send message to AF */
236 if (otx2_sync_mbox_msg(&pfvf->mbox))
237 goto exit;
238
239 rsp = (struct npc_mcam_alloc_entry_rsp *)otx2_mbox_get_rsp
240 (&pfvf->mbox.mbox, 0, &req->hdr);
241 if (IS_ERR(rsp))
242 goto exit;
243
244 for (ent = 0; ent < rsp->count; ent++)
245 flow_cfg->flow_ent[ent + allocated] = rsp->entry_list[ent];
246
247 allocated += rsp->count;
248
249 /* If this request is not fulfilled, no need to send
250 * further requests.
251 */
252 if (rsp->count != req->count)
253 break;
254 }
255
256 /* Multiple MCAM entry alloc requests could result in non-sequential
257 * MCAM entries in the flow_ent[] array. Sort them in an ascending order,
258 * otherwise user installed ntuple filter index and MCAM entry index will
259 * not be in sync.
260 */
261 if (allocated)
262 sort(&flow_cfg->flow_ent[0], allocated,
263 sizeof(flow_cfg->flow_ent[0]), mcam_entry_cmp, NULL);
264
265 exit:
266 mutex_unlock(&pfvf->mbox.lock);
267
268 flow_cfg->max_flows = allocated;
269
270 if (allocated) {
271 pfvf->flags |= OTX2_FLAG_MCAM_ENTRIES_ALLOC;
272 pfvf->flags |= OTX2_FLAG_NTUPLE_SUPPORT;
273 pfvf->flags |= OTX2_FLAG_TC_FLOWER_SUPPORT;
274 }
275
276 if (allocated != count)
277 netdev_info(pfvf->netdev,
278 "Unable to allocate %d MCAM entries, got only %d\n",
279 count, allocated);
280 return allocated;
281 }
282 EXPORT_SYMBOL(otx2_alloc_mcam_entries);
283
284 int otx2_mcam_entry_init(struct otx2_nic *pfvf)
285 {
286 struct otx2_flow_config *flow_cfg = pfvf->flow_cfg;
287 struct npc_get_field_status_req *freq;
288 struct npc_get_field_status_rsp *frsp;
289 struct npc_mcam_alloc_entry_req *req;
290 struct npc_mcam_alloc_entry_rsp *rsp;
291 int vf_vlan_max_flows, count;
292 int rc, ref, prio, ent;
293 u16 dft_idx;
294
295 ref = 0;
296 prio = 0;
297 if (is_cn20k(pfvf->pdev)) {
298 rc = otx2_get_dft_rl_idx(pfvf, &dft_idx);
299 if (rc) {
300 netdev_err(pfvf->netdev,
301 "Error to retrieve ucast mcam idx for pcifunc %#x\n",
302 pfvf->pcifunc);
303 return rc;
304 }
305
306 ref = dft_idx;
307 prio = NPC_MCAM_HIGHER_PRIO;
308 }
309
310 vf_vlan_max_flows = pfvf->total_vfs * OTX2_PER_VF_VLAN_FLOWS;
311 count = flow_cfg->ucast_flt_cnt +
312 OTX2_MAX_VLAN_FLOWS + vf_vlan_max_flows;
313
314 flow_cfg->def_ent = devm_kmalloc_array(pfvf->dev, count,
315 sizeof(u16), GFP_KERNEL);
316 if (!flow_cfg->def_ent)
317 return -ENOMEM;
318
319 mutex_lock(&pfvf->mbox.lock);
320
321 req = otx2_mbox_alloc_msg_npc_mcam_alloc_entry(&pfvf->mbox);
322 if (!req) {
323 mutex_unlock(&pfvf->mbox.lock);
324 return -ENOMEM;
325 }
326
327 req->kw_type = NPC_MCAM_KEY_X2;
328 req->contig = false;
329 req->count = count;
330 req->ref_prio = prio;
331 req->ref_entry = ref;
332
333 /* Send message to AF */
334 if (otx2_sync_mbox_msg(&pfvf->mbox)) {
335 mutex_unlock(&pfvf->mbox.lock);
336 return -EINVAL;
337 }
338
339 rsp = (struct npc_mcam_alloc_entry_rsp *)otx2_mbox_get_rsp
340 (&pfvf->mbox.mbox, 0, &req->hdr);
341 if (IS_ERR(rsp)) {
342 mutex_unlock(&pfvf->mbox.lock);
343 return PTR_ERR(rsp);
344 }
345
346 if (rsp->count != req->count) {
347 netdev_info(pfvf->netdev,
348 "Unable to allocate MCAM entries for ucast, vlan and vf_vlan\n");
349 mutex_unlock(&pfvf->mbox.lock);
350 devm_kfree(pfvf->dev, flow_cfg->def_ent);
351 return 0;
352 }
353
354 for (ent = 0; ent < rsp->count; ent++)
355 flow_cfg->def_ent[ent] = rsp->entry_list[ent];
356
357 flow_cfg->vf_vlan_offset = 0;
358 flow_cfg->unicast_offset = vf_vlan_max_flows;
359 flow_cfg->rx_vlan_offset = flow_cfg->unicast_offset +
360 flow_cfg->ucast_flt_cnt;
361 pfvf->flags |= OTX2_FLAG_UCAST_FLTR_SUPPORT;
362
363 /* Check if NPC_DMAC field is supported
364 * by the mkex profile before setting VLAN support flag.
365 */
366 freq = otx2_mbox_alloc_msg_npc_get_field_status(&pfvf->mbox);
367 if (!freq) {
368 mutex_unlock(&pfvf->mbox.lock);
369 return -ENOMEM;
370 }
371
372 freq->field = NPC_DMAC;
373 if (otx2_sync_mbox_msg(&pfvf->mbox)) {
374 mutex_unlock(&pfvf->mbox.lock);
375 return -EINVAL;
376 }
377
378 frsp = (struct npc_get_field_status_rsp *)otx2_mbox_get_rsp
379 (&pfvf->mbox.mbox, 0, &freq->hdr);
380 if (IS_ERR(frsp)) {
381 mutex_unlock(&pfvf->mbox.lock);
382 return PTR_ERR(frsp);
383 }
384
385 if (frsp->enable) {
386 pfvf->flags |= OTX2_FLAG_RX_VLAN_SUPPORT;
387 pfvf->flags |= OTX2_FLAG_VF_VLAN_SUPPORT;
388 }
389
390 pfvf->flags |= OTX2_FLAG_MCAM_ENTRIES_ALLOC;
391 mutex_unlock(&pfvf->mbox.lock);
392
393 /* Allocate entries for Ntuple filters */
394 count = otx2_alloc_mcam_entries(pfvf, flow_cfg->ntuple_cnt);
395 if (count <= 0) {
396 otx2_clear_ntuple_flow_info(pfvf, flow_cfg);
397 return 0;
398 }
399
400 pfvf->flags |= OTX2_FLAG_TC_FLOWER_SUPPORT;
401
402 refcount_set(&flow_cfg->mark_flows, 1);
403 return 0;
404 }
405 EXPORT_SYMBOL(otx2_mcam_entry_init);
406
407 /* TODO : revisit on size */
408 #define OTX2_DMAC_FLTR_BITMAP_SZ (4 * 2048 + 32)
409
410 int otx2vf_mcam_flow_init(struct otx2_nic *pfvf)
411 {
412 struct otx2_flow_config *flow_cfg;
413
414 pfvf->flow_cfg = devm_kzalloc(pfvf->dev,
415 sizeof(struct otx2_flow_config),
416 GFP_KERNEL);
417 if (!pfvf->flow_cfg)
418 return -ENOMEM;
419
420 pfvf->flow_cfg->dmacflt_bmap = devm_kcalloc(pfvf->dev,
421 BITS_TO_LONGS(OTX2_DMAC_FLTR_BITMAP_SZ),
422 sizeof(long), GFP_KERNEL);
423 if (!pfvf->flow_cfg->dmacflt_bmap)
424 return -ENOMEM;
425
426 flow_cfg = pfvf->flow_cfg;
427 INIT_LIST_HEAD(&flow_cfg->flow_list);
428 INIT_LIST_HEAD(&flow_cfg->flow_list_tc);
429 flow_cfg->max_flows = 0;
430
431 return 0;
432 }
433 EXPORT_SYMBOL(otx2vf_mcam_flow_init);
434
435 int otx2_mcam_flow_init(struct otx2_nic *pf)
436 {
437 int err;
438
439 pf->flow_cfg = devm_kzalloc(pf->dev, sizeof(struct otx2_flow_config),
440 GFP_KERNEL);
441 if (!pf->flow_cfg)
442 return -ENOMEM;
443
444 pf->flow_cfg->dmacflt_bmap = devm_kcalloc(pf->dev,
445 BITS_TO_LONGS(OTX2_DMAC_FLTR_BITMAP_SZ),
446 sizeof(long), GFP_KERNEL);
447 if (!pf->flow_cfg->dmacflt_bmap)
448 return -ENOMEM;
449
450 INIT_LIST_HEAD(&pf->flow_cfg->flow_list);
451 INIT_LIST_HEAD(&pf->flow_cfg->flow_list_tc);
452
453 pf->flow_cfg->ucast_flt_cnt = OTX2_DEFAULT_UNICAST_FLOWS;
454 pf->flow_cfg->ntuple_cnt = OTX2_DEFAULT_FLOWCOUNT;
455
456 /* Allocate bare minimum number of MCAM entries needed for
457 * unicast and ntuple filters.
458 */
459 err = otx2_mcam_entry_init(pf);
460 if (err)
461 return err;
462
463 /* Check if MCAM entries are allocate or not */
464 if (!(pf->flags & OTX2_FLAG_UCAST_FLTR_SUPPORT))
465 return 0;
466
467 pf->mac_table = devm_kzalloc(pf->dev, sizeof(struct otx2_mac_table)
468 * pf->flow_cfg->ucast_flt_cnt, GFP_KERNEL);
469 if (!pf->mac_table)
470 return -ENOMEM;
471
472 otx2_dmacflt_get_max_cnt(pf);
473
474 /* DMAC filters are not allocated */
475 if (!pf->flow_cfg->dmacflt_max_flows)
476 return 0;
477
478 pf->flow_cfg->bmap_to_dmacindex =
479 devm_kzalloc(pf->dev, sizeof(u32) *
480 pf->flow_cfg->dmacflt_max_flows,
481 GFP_KERNEL);
482
483 if (!pf->flow_cfg->bmap_to_dmacindex)
484 return -ENOMEM;
485
486 pf->flags |= OTX2_FLAG_DMACFLTR_SUPPORT;
487
488 return 0;
489 }
490
491 void otx2_mcam_flow_del(struct otx2_nic *pf)
492 {
493 otx2_destroy_mcam_flows(pf);
494 }
495 EXPORT_SYMBOL(otx2_mcam_flow_del);
496
497 /* On success adds mcam entry
498 * On failure enable promisous mode
499 */
500 static int otx2_do_add_macfilter(struct otx2_nic *pf, const u8 *mac)
501 {
502 struct otx2_flow_config *flow_cfg = pf->flow_cfg;
503 struct npc_install_flow_req *req;
504 int err, i;
505
506 if (!(pf->flags & OTX2_FLAG_UCAST_FLTR_SUPPORT))
507 return -ENOMEM;
508
509 /* dont have free mcam entries or uc list is greater than alloted */
510 if (netdev_uc_count(pf->netdev) > pf->flow_cfg->ucast_flt_cnt)
511 return -ENOMEM;
512
513 mutex_lock(&pf->mbox.lock);
514 req = otx2_mbox_alloc_msg_npc_install_flow(&pf->mbox);
515 if (!req) {
516 mutex_unlock(&pf->mbox.lock);
517 return -ENOMEM;
518 }
519
520 /* unicast offset starts with 32 0..31 for ntuple */
521 for (i = 0; i < pf->flow_cfg->ucast_flt_cnt; i++) {
522 if (pf->mac_table[i].inuse)
523 continue;
524 ether_addr_copy(pf->mac_table[i].addr, mac);
525 pf->mac_table[i].inuse = true;
526 pf->mac_table[i].mcam_entry =
527 flow_cfg->def_ent[i + flow_cfg->unicast_offset];
528 req->entry = pf->mac_table[i].mcam_entry;
529 break;
530 }
531
532 ether_addr_copy(req->packet.dmac, mac);
533 eth_broadcast_addr((u8 *)&req->mask.dmac);
534 req->features = BIT_ULL(NPC_DMAC);
535 req->channel = pf->hw.rx_chan_base;
536 req->intf = NIX_INTF_RX;
537 req->op = NIX_RX_ACTION_DEFAULT;
538 req->set_cntr = 1;
539
540 err = otx2_sync_mbox_msg(&pf->mbox);
541 mutex_unlock(&pf->mbox.lock);
542
543 return err;
544 }
545
546 int otx2_add_macfilter(struct net_device *netdev, const u8 *mac)
547 {
548 struct otx2_nic *pf = netdev_priv(netdev);
549
550 if (!bitmap_empty(pf->flow_cfg->dmacflt_bmap,
551 pf->flow_cfg->dmacflt_max_flows))
552 netdev_warn(netdev,
553 "Add %pM to CGX/RPM DMAC filters list as well\n",
554 mac);
555
556 return otx2_do_add_macfilter(pf, mac);
557 }
558
559 static bool otx2_get_mcamentry_for_mac(struct otx2_nic *pf, const u8 *mac,
560 int *mcam_entry)
561 {
562 int i;
563
564 for (i = 0; i < pf->flow_cfg->ucast_flt_cnt; i++) {
565 if (!pf->mac_table[i].inuse)
566 continue;
567
568 if (ether_addr_equal(pf->mac_table[i].addr, mac)) {
569 *mcam_entry = pf->mac_table[i].mcam_entry;
570 pf->mac_table[i].inuse = false;
571 return true;
572 }
573 }
574 return false;
575 }
576
577 int otx2_del_macfilter(struct net_device *netdev, const u8 *mac)
578 {
579 struct otx2_nic *pf = netdev_priv(netdev);
580 struct npc_delete_flow_req *req;
581 int err, mcam_entry;
582
583 /* check does mcam entry exists for given mac */
584 if (!otx2_get_mcamentry_for_mac(pf, mac, &mcam_entry))
585 return 0;
586
587 mutex_lock(&pf->mbox.lock);
588 req = otx2_mbox_alloc_msg_npc_delete_flow(&pf->mbox);
589 if (!req) {
590 mutex_unlock(&pf->mbox.lock);
591 return -ENOMEM;
592 }
593 req->entry = mcam_entry;
594 /* Send message to AF */
595 err = otx2_sync_mbox_msg(&pf->mbox);
596 mutex_unlock(&pf->mbox.lock);
597
598 return err;
599 }
600
601 static struct otx2_flow *otx2_find_flow(struct otx2_nic *pfvf, u32 location)
602 {
603 struct otx2_flow *iter;
604
605 list_for_each_entry(iter, &pfvf->flow_cfg->flow_list, list) {
606 if (iter->location == location)
607 return iter;
608 }
609
610 return NULL;
611 }
612
613 static void otx2_add_flow_to_list(struct otx2_nic *pfvf, struct otx2_flow *flow)
614 {
615 struct list_head *head = &pfvf->flow_cfg->flow_list;
616 struct otx2_flow *iter;
617
618 list_for_each_entry(iter, &pfvf->flow_cfg->flow_list, list) {
619 if (iter->location > flow->location)
620 break;
621 head = &iter->list;
622 }
623
624 list_add(&flow->list, head);
625 }
626
627 int otx2_get_maxflows(struct otx2_flow_config *flow_cfg)
628 {
629 if (!flow_cfg)
630 return 0;
631
632 if (flow_cfg->nr_flows == flow_cfg->max_flows ||
633 !bitmap_empty(flow_cfg->dmacflt_bmap,
634 flow_cfg->dmacflt_max_flows))
635 return flow_cfg->max_flows + flow_cfg->dmacflt_max_flows;
636 else
637 return flow_cfg->max_flows;
638 }
639 EXPORT_SYMBOL(otx2_get_maxflows);
640
641 int otx2_get_flow(struct otx2_nic *pfvf, struct ethtool_rxnfc *nfc,
642 u32 location)
643 {
644 struct otx2_flow *iter;
645
646 if (location >= otx2_get_maxflows(pfvf->flow_cfg))
647 return -EINVAL;
648
649 list_for_each_entry(iter, &pfvf->flow_cfg->flow_list, list) {
650 if (iter->location == location) {
651 nfc->fs = iter->flow_spec;
652 nfc->rss_context = iter->rss_ctx_id;
653 return 0;
654 }
655 }
656
657 return -ENOENT;
658 }
659
660 int otx2_get_all_flows(struct otx2_nic *pfvf, struct ethtool_rxnfc *nfc,
661 u32 *rule_locs)
662 {
663 u32 rule_cnt = nfc->rule_cnt;
664 u32 location = 0;
665 int idx = 0;
666 int err = 0;
667
668 nfc->data = otx2_get_maxflows(pfvf->flow_cfg);
669 while ((!err || err == -ENOENT) && idx < rule_cnt) {
670 err = otx2_get_flow(pfvf, nfc, location);
671 if (!err)
672 rule_locs[idx++] = location;
673 location++;
674 }
675 nfc->rule_cnt = rule_cnt;
676
677 return err;
678 }
679
680 static int otx2_prepare_ipv4_flow(struct ethtool_rx_flow_spec *fsp,
681 struct npc_install_flow_req *req,
682 u32 flow_type)
683 {
684 struct ethtool_usrip4_spec *ipv4_usr_mask = &fsp->m_u.usr_ip4_spec;
685 struct ethtool_usrip4_spec *ipv4_usr_hdr = &fsp->h_u.usr_ip4_spec;
686 struct ethtool_tcpip4_spec *ipv4_l4_mask = &fsp->m_u.tcp_ip4_spec;
687 struct ethtool_tcpip4_spec *ipv4_l4_hdr = &fsp->h_u.tcp_ip4_spec;
688 struct ethtool_ah_espip4_spec *ah_esp_hdr = &fsp->h_u.ah_ip4_spec;
689 struct ethtool_ah_espip4_spec *ah_esp_mask = &fsp->m_u.ah_ip4_spec;
690 struct flow_msg *pmask = &req->mask;
691 struct flow_msg *pkt = &req->packet;
692
693 switch (flow_type) {
694 case IP_USER_FLOW:
695 if (ipv4_usr_mask->ip4src) {
696 memcpy(&pkt->ip4src, &ipv4_usr_hdr->ip4src,
697 sizeof(pkt->ip4src));
698 memcpy(&pmask->ip4src, &ipv4_usr_mask->ip4src,
699 sizeof(pmask->ip4src));
700 req->features |= BIT_ULL(NPC_SIP_IPV4);
701 }
702 if (ipv4_usr_mask->ip4dst) {
703 memcpy(&pkt->ip4dst, &ipv4_usr_hdr->ip4dst,
704 sizeof(pkt->ip4dst));
705 memcpy(&pmask->ip4dst, &ipv4_usr_mask->ip4dst,
706 sizeof(pmask->ip4dst));
707 req->features |= BIT_ULL(NPC_DIP_IPV4);
708 }
709 if (ipv4_usr_mask->tos) {
710 pkt->tos = ipv4_usr_hdr->tos;
711 pmask->tos = ipv4_usr_mask->tos;
712 req->features |= BIT_ULL(NPC_TOS);
713 }
714 if (ipv4_usr_mask->proto) {
715 switch (ipv4_usr_hdr->proto) {
716 case IPPROTO_ICMP:
717 req->features |= BIT_ULL(NPC_IPPROTO_ICMP);
718 break;
719 case IPPROTO_TCP:
720 req->features |= BIT_ULL(NPC_IPPROTO_TCP);
721 break;
722 case IPPROTO_UDP:
723 req->features |= BIT_ULL(NPC_IPPROTO_UDP);
724 break;
725 case IPPROTO_SCTP:
726 req->features |= BIT_ULL(NPC_IPPROTO_SCTP);
727 break;
728 case IPPROTO_AH:
729 req->features |= BIT_ULL(NPC_IPPROTO_AH);
730 break;
731 case IPPROTO_ESP:
732 req->features |= BIT_ULL(NPC_IPPROTO_ESP);
733 break;
734 default:
735 return -EOPNOTSUPP;
736 }
737 }
738 pkt->etype = cpu_to_be16(ETH_P_IP);
739 pmask->etype = cpu_to_be16(0xFFFF);
740 req->features |= BIT_ULL(NPC_ETYPE);
741 break;
742 case TCP_V4_FLOW:
743 case UDP_V4_FLOW:
744 case SCTP_V4_FLOW:
745 pkt->etype = cpu_to_be16(ETH_P_IP);
746 pmask->etype = cpu_to_be16(0xFFFF);
747 req->features |= BIT_ULL(NPC_ETYPE);
748 if (ipv4_l4_mask->ip4src) {
749 memcpy(&pkt->ip4src, &ipv4_l4_hdr->ip4src,
750 sizeof(pkt->ip4src));
751 memcpy(&pmask->ip4src, &ipv4_l4_mask->ip4src,
752 sizeof(pmask->ip4src));
753 req->features |= BIT_ULL(NPC_SIP_IPV4);
754 }
755 if (ipv4_l4_mask->ip4dst) {
756 memcpy(&pkt->ip4dst, &ipv4_l4_hdr->ip4dst,
757 sizeof(pkt->ip4dst));
758 memcpy(&pmask->ip4dst, &ipv4_l4_mask->ip4dst,
759 sizeof(pmask->ip4dst));
760 req->features |= BIT_ULL(NPC_DIP_IPV4);
761 }
762 if (ipv4_l4_mask->tos) {
763 pkt->tos = ipv4_l4_hdr->tos;
764 pmask->tos = ipv4_l4_mask->tos;
765 req->features |= BIT_ULL(NPC_TOS);
766 }
767 if (ipv4_l4_mask->psrc) {
768 memcpy(&pkt->sport, &ipv4_l4_hdr->psrc,
769 sizeof(pkt->sport));
770 memcpy(&pmask->sport, &ipv4_l4_mask->psrc,
771 sizeof(pmask->sport));
772 if (flow_type == UDP_V4_FLOW)
773 req->features |= BIT_ULL(NPC_SPORT_UDP);
774 else if (flow_type == TCP_V4_FLOW)
775 req->features |= BIT_ULL(NPC_SPORT_TCP);
776 else
777 req->features |= BIT_ULL(NPC_SPORT_SCTP);
778 }
779 if (ipv4_l4_mask->pdst) {
780 memcpy(&pkt->dport, &ipv4_l4_hdr->pdst,
781 sizeof(pkt->dport));
782 memcpy(&pmask->dport, &ipv4_l4_mask->pdst,
783 sizeof(pmask->dport));
784 if (flow_type == UDP_V4_FLOW)
785 req->features |= BIT_ULL(NPC_DPORT_UDP);
786 else if (flow_type == TCP_V4_FLOW)
787 req->features |= BIT_ULL(NPC_DPORT_TCP);
788 else
789 req->features |= BIT_ULL(NPC_DPORT_SCTP);
790 }
791 if (flow_type == UDP_V4_FLOW)
792 req->features |= BIT_ULL(NPC_IPPROTO_UDP);
793 else if (flow_type == TCP_V4_FLOW)
794 req->features |= BIT_ULL(NPC_IPPROTO_TCP);
795 else
796 req->features |= BIT_ULL(NPC_IPPROTO_SCTP);
797 break;
798 case AH_V4_FLOW:
799 case ESP_V4_FLOW:
800 pkt->etype = cpu_to_be16(ETH_P_IP);
801 pmask->etype = cpu_to_be16(0xFFFF);
802 req->features |= BIT_ULL(NPC_ETYPE);
803 if (ah_esp_mask->ip4src) {
804 memcpy(&pkt->ip4src, &ah_esp_hdr->ip4src,
805 sizeof(pkt->ip4src));
806 memcpy(&pmask->ip4src, &ah_esp_mask->ip4src,
807 sizeof(pmask->ip4src));
808 req->features |= BIT_ULL(NPC_SIP_IPV4);
809 }
810 if (ah_esp_mask->ip4dst) {
811 memcpy(&pkt->ip4dst, &ah_esp_hdr->ip4dst,
812 sizeof(pkt->ip4dst));
813 memcpy(&pmask->ip4dst, &ah_esp_mask->ip4dst,
814 sizeof(pmask->ip4dst));
815 req->features |= BIT_ULL(NPC_DIP_IPV4);
816 }
817 if (ah_esp_mask->tos) {
818 pkt->tos = ah_esp_hdr->tos;
819 pmask->tos = ah_esp_mask->tos;
820 req->features |= BIT_ULL(NPC_TOS);
821 }
822
823 /* NPC profile doesn't extract AH/ESP header fields */
824 if (ah_esp_mask->spi & ah_esp_hdr->spi)
825 return -EOPNOTSUPP;
826
827 if (flow_type == AH_V4_FLOW)
828 req->features |= BIT_ULL(NPC_IPPROTO_AH);
829 else
830 req->features |= BIT_ULL(NPC_IPPROTO_ESP);
831 break;
832 default:
833 break;
834 }
835
836 return 0;
837 }
838
839 static int otx2_prepare_ipv6_flow(struct ethtool_rx_flow_spec *fsp,
840 struct npc_install_flow_req *req,
841 u32 flow_type)
842 {
843 struct ethtool_usrip6_spec *ipv6_usr_mask = &fsp->m_u.usr_ip6_spec;
844 struct ethtool_usrip6_spec *ipv6_usr_hdr = &fsp->h_u.usr_ip6_spec;
845 struct ethtool_tcpip6_spec *ipv6_l4_mask = &fsp->m_u.tcp_ip6_spec;
846 struct ethtool_tcpip6_spec *ipv6_l4_hdr = &fsp->h_u.tcp_ip6_spec;
847 struct ethtool_ah_espip6_spec *ah_esp_hdr = &fsp->h_u.ah_ip6_spec;
848 struct ethtool_ah_espip6_spec *ah_esp_mask = &fsp->m_u.ah_ip6_spec;
849 struct flow_msg *pmask = &req->mask;
850 struct flow_msg *pkt = &req->packet;
851
852 switch (flow_type) {
853 case IPV6_USER_FLOW:
854 if (!ipv6_addr_any((struct in6_addr *)ipv6_usr_mask->ip6src)) {
855 memcpy(&pkt->ip6src, &ipv6_usr_hdr->ip6src,
856 sizeof(pkt->ip6src));
857 memcpy(&pmask->ip6src, &ipv6_usr_mask->ip6src,
858 sizeof(pmask->ip6src));
859 req->features |= BIT_ULL(NPC_SIP_IPV6);
860 }
861 if (!ipv6_addr_any((struct in6_addr *)ipv6_usr_mask->ip6dst)) {
862 memcpy(&pkt->ip6dst, &ipv6_usr_hdr->ip6dst,
863 sizeof(pkt->ip6dst));
864 memcpy(&pmask->ip6dst, &ipv6_usr_mask->ip6dst,
865 sizeof(pmask->ip6dst));
866 req->features |= BIT_ULL(NPC_DIP_IPV6);
867 }
868 if (ipv6_usr_hdr->l4_proto == IPPROTO_FRAGMENT) {
869 pkt->next_header = ipv6_usr_hdr->l4_proto;
870 pmask->next_header = ipv6_usr_mask->l4_proto;
871 req->features |= BIT_ULL(NPC_IPFRAG_IPV6);
872 }
873 pkt->etype = cpu_to_be16(ETH_P_IPV6);
874 pmask->etype = cpu_to_be16(0xFFFF);
875 req->features |= BIT_ULL(NPC_ETYPE);
876 break;
877 case TCP_V6_FLOW:
878 case UDP_V6_FLOW:
879 case SCTP_V6_FLOW:
880 pkt->etype = cpu_to_be16(ETH_P_IPV6);
881 pmask->etype = cpu_to_be16(0xFFFF);
882 req->features |= BIT_ULL(NPC_ETYPE);
883 if (!ipv6_addr_any((struct in6_addr *)ipv6_l4_mask->ip6src)) {
884 memcpy(&pkt->ip6src, &ipv6_l4_hdr->ip6src,
885 sizeof(pkt->ip6src));
886 memcpy(&pmask->ip6src, &ipv6_l4_mask->ip6src,
887 sizeof(pmask->ip6src));
888 req->features |= BIT_ULL(NPC_SIP_IPV6);
889 }
890 if (!ipv6_addr_any((struct in6_addr *)ipv6_l4_mask->ip6dst)) {
891 memcpy(&pkt->ip6dst, &ipv6_l4_hdr->ip6dst,
892 sizeof(pkt->ip6dst));
893 memcpy(&pmask->ip6dst, &ipv6_l4_mask->ip6dst,
894 sizeof(pmask->ip6dst));
895 req->features |= BIT_ULL(NPC_DIP_IPV6);
896 }
897 if (ipv6_l4_mask->psrc) {
898 memcpy(&pkt->sport, &ipv6_l4_hdr->psrc,
899 sizeof(pkt->sport));
900 memcpy(&pmask->sport, &ipv6_l4_mask->psrc,
901 sizeof(pmask->sport));
902 if (flow_type == UDP_V6_FLOW)
903 req->features |= BIT_ULL(NPC_SPORT_UDP);
904 else if (flow_type == TCP_V6_FLOW)
905 req->features |= BIT_ULL(NPC_SPORT_TCP);
906 else
907 req->features |= BIT_ULL(NPC_SPORT_SCTP);
908 }
909 if (ipv6_l4_mask->pdst) {
910 memcpy(&pkt->dport, &ipv6_l4_hdr->pdst,
911 sizeof(pkt->dport));
912 memcpy(&pmask->dport, &ipv6_l4_mask->pdst,
913 sizeof(pmask->dport));
914 if (flow_type == UDP_V6_FLOW)
915 req->features |= BIT_ULL(NPC_DPORT_UDP);
916 else if (flow_type == TCP_V6_FLOW)
917 req->features |= BIT_ULL(NPC_DPORT_TCP);
918 else
919 req->features |= BIT_ULL(NPC_DPORT_SCTP);
920 }
921 if (flow_type == UDP_V6_FLOW)
922 req->features |= BIT_ULL(NPC_IPPROTO_UDP);
923 else if (flow_type == TCP_V6_FLOW)
924 req->features |= BIT_ULL(NPC_IPPROTO_TCP);
925 else
926 req->features |= BIT_ULL(NPC_IPPROTO_SCTP);
927 break;
928 case AH_V6_FLOW:
929 case ESP_V6_FLOW:
930 pkt->etype = cpu_to_be16(ETH_P_IPV6);
931 pmask->etype = cpu_to_be16(0xFFFF);
932 req->features |= BIT_ULL(NPC_ETYPE);
933 if (!ipv6_addr_any((struct in6_addr *)ah_esp_hdr->ip6src)) {
934 memcpy(&pkt->ip6src, &ah_esp_hdr->ip6src,
935 sizeof(pkt->ip6src));
936 memcpy(&pmask->ip6src, &ah_esp_mask->ip6src,
937 sizeof(pmask->ip6src));
938 req->features |= BIT_ULL(NPC_SIP_IPV6);
939 }
940 if (!ipv6_addr_any((struct in6_addr *)ah_esp_hdr->ip6dst)) {
941 memcpy(&pkt->ip6dst, &ah_esp_hdr->ip6dst,
942 sizeof(pkt->ip6dst));
943 memcpy(&pmask->ip6dst, &ah_esp_mask->ip6dst,
944 sizeof(pmask->ip6dst));
945 req->features |= BIT_ULL(NPC_DIP_IPV6);
946 }
947
948 /* NPC profile doesn't extract AH/ESP header fields */
949 if ((ah_esp_mask->spi & ah_esp_hdr->spi) ||
950 (ah_esp_mask->tclass & ah_esp_hdr->tclass))
951 return -EOPNOTSUPP;
952
953 if (flow_type == AH_V6_FLOW)
954 req->features |= BIT_ULL(NPC_IPPROTO_AH);
955 else
956 req->features |= BIT_ULL(NPC_IPPROTO_ESP);
957 break;
958 default:
959 break;
960 }
961
962 return 0;
963 }
964
965 static int otx2_prepare_flow_request(struct ethtool_rx_flow_spec *fsp,
966 struct npc_install_flow_req *req)
967 {
968 struct ethhdr *eth_mask = &fsp->m_u.ether_spec;
969 struct ethhdr *eth_hdr = &fsp->h_u.ether_spec;
970 struct flow_msg *pmask = &req->mask;
971 struct flow_msg *pkt = &req->packet;
972 u32 flow_type;
973 int ret;
974
975 flow_type = fsp->flow_type & ~(FLOW_EXT | FLOW_MAC_EXT | FLOW_RSS);
976 switch (flow_type) {
977 /* bits not set in mask are don't care */
978 case ETHER_FLOW:
979 if (!is_zero_ether_addr(eth_mask->h_source)) {
980 ether_addr_copy(pkt->smac, eth_hdr->h_source);
981 ether_addr_copy(pmask->smac, eth_mask->h_source);
982 req->features |= BIT_ULL(NPC_SMAC);
983 }
984 if (!is_zero_ether_addr(eth_mask->h_dest)) {
985 ether_addr_copy(pkt->dmac, eth_hdr->h_dest);
986 ether_addr_copy(pmask->dmac, eth_mask->h_dest);
987 req->features |= BIT_ULL(NPC_DMAC);
988 }
989 if (eth_hdr->h_proto) {
990 memcpy(&pkt->etype, &eth_hdr->h_proto,
991 sizeof(pkt->etype));
992 memcpy(&pmask->etype, &eth_mask->h_proto,
993 sizeof(pmask->etype));
994 req->features |= BIT_ULL(NPC_ETYPE);
995 }
996 break;
997 case IP_USER_FLOW:
998 case TCP_V4_FLOW:
999 case UDP_V4_FLOW:
1000 case SCTP_V4_FLOW:
1001 case AH_V4_FLOW:
1002 case ESP_V4_FLOW:
1003 ret = otx2_prepare_ipv4_flow(fsp, req, flow_type);
1004 if (ret)
1005 return ret;
1006 break;
1007 case IPV6_USER_FLOW:
1008 case TCP_V6_FLOW:
1009 case UDP_V6_FLOW:
1010 case SCTP_V6_FLOW:
1011 case AH_V6_FLOW:
1012 case ESP_V6_FLOW:
1013 ret = otx2_prepare_ipv6_flow(fsp, req, flow_type);
1014 if (ret)
1015 return ret;
1016 break;
1017 default:
1018 return -EOPNOTSUPP;
1019 }
1020 if (fsp->flow_type & FLOW_EXT) {
1021 u16 vlan_etype;
1022
1023 if (fsp->m_ext.vlan_etype) {
1024 /* Partial masks not supported */
1025 if (be16_to_cpu(fsp->m_ext.vlan_etype) != 0xFFFF)
1026 return -EINVAL;
1027
1028 vlan_etype = be16_to_cpu(fsp->h_ext.vlan_etype);
1029
1030 /* Drop rule with vlan_etype == 802.1Q
1031 * and vlan_id == 0 is not supported
1032 */
1033 if (vlan_etype == ETH_P_8021Q && !fsp->m_ext.vlan_tci &&
1034 fsp->ring_cookie == RX_CLS_FLOW_DISC)
1035 return -EINVAL;
1036
1037 /* Only ETH_P_8021Q and ETH_P_802AD types supported */
1038 if (vlan_etype != ETH_P_8021Q &&
1039 vlan_etype != ETH_P_8021AD)
1040 return -EINVAL;
1041
1042 memcpy(&pkt->vlan_etype, &fsp->h_ext.vlan_etype,
1043 sizeof(pkt->vlan_etype));
1044 memcpy(&pmask->vlan_etype, &fsp->m_ext.vlan_etype,
1045 sizeof(pmask->vlan_etype));
1046
1047 if (vlan_etype == ETH_P_8021Q)
1048 req->features |= BIT_ULL(NPC_VLAN_ETYPE_CTAG);
1049 else
1050 req->features |= BIT_ULL(NPC_VLAN_ETYPE_STAG);
1051 }
1052
1053 if (fsp->m_ext.vlan_tci) {
1054 memcpy(&pkt->vlan_tci, &fsp->h_ext.vlan_tci,
1055 sizeof(pkt->vlan_tci));
1056 memcpy(&pmask->vlan_tci, &fsp->m_ext.vlan_tci,
1057 sizeof(pmask->vlan_tci));
1058 req->features |= BIT_ULL(NPC_OUTER_VID);
1059 }
1060
1061 if (fsp->m_ext.data[1]) {
1062 if (flow_type == IP_USER_FLOW) {
1063 if (be32_to_cpu(fsp->h_ext.data[1]) != IPV4_FLAG_MORE)
1064 return -EINVAL;
1065
1066 pkt->ip_flag = be32_to_cpu(fsp->h_ext.data[1]);
1067 pmask->ip_flag = be32_to_cpu(fsp->m_ext.data[1]);
1068 req->features |= BIT_ULL(NPC_IPFRAG_IPV4);
1069 } else if (fsp->h_ext.data[1] ==
1070 cpu_to_be32(OTX2_DEFAULT_ACTION)) {
1071 /* Not Drop/Direct to queue but use action
1072 * in default entry
1073 */
1074 req->op = NIX_RX_ACTION_DEFAULT;
1075 }
1076 }
1077 }
1078
1079 if (fsp->flow_type & FLOW_MAC_EXT &&
1080 !is_zero_ether_addr(fsp->m_ext.h_dest)) {
1081 ether_addr_copy(pkt->dmac, fsp->h_ext.h_dest);
1082 ether_addr_copy(pmask->dmac, fsp->m_ext.h_dest);
1083 req->features |= BIT_ULL(NPC_DMAC);
1084 }
1085
1086 if (!req->features)
1087 return -EOPNOTSUPP;
1088
1089 return 0;
1090 }
1091
1092 static int otx2_get_kw_type(struct otx2_nic *pfvf,
1093 struct npc_install_flow_req *fl_req,
1094 u8 *kw_type)
1095 {
1096 struct npc_get_num_kws_req *req;
1097 struct npc_get_num_kws_rsp *rsp;
1098 u8 *src, *dst;
1099 int off, err;
1100 int kw_bits;
1101
1102 off = offsetof(struct npc_install_flow_req, packet);
1103
1104 mutex_lock(&pfvf->mbox.lock);
1105
1106 req = otx2_mbox_alloc_msg_npc_get_num_kws(&pfvf->mbox);
1107 if (!req) {
1108 mutex_unlock(&pfvf->mbox.lock);
1109 return -ENOMEM;
1110 }
1111
1112 dst = (u8 *)&req->fl + off;
1113 src = (u8 *)fl_req + off;
1114
1115 memcpy(dst, src, sizeof(struct npc_install_flow_req) - off);
1116
1117 err = otx2_sync_mbox_msg(&pfvf->mbox);
1118 if (err) {
1119 mutex_unlock(&pfvf->mbox.lock);
1120 netdev_err(pfvf->netdev,
1121 "Error to get default number of keywords\n");
1122 return err;
1123 }
1124
1125 rsp = (struct npc_get_num_kws_rsp *)otx2_mbox_get_rsp
1126 (&pfvf->mbox.mbox, 0, &req->hdr);
1127 if (IS_ERR(rsp)) {
1128 mutex_unlock(&pfvf->mbox.lock);
1129 return -EFAULT;
1130 }
1131
1132 kw_bits = rsp->kws * 64;
1133
1134 if (kw_bits <= 256)
1135 *kw_type = NPC_MCAM_KEY_X2;
1136 else
1137 *kw_type = NPC_MCAM_KEY_X4;
1138
1139 mutex_unlock(&pfvf->mbox.lock);
1140
1141 return 0;
1142 }
1143
1144 static int otx2_is_flow_rule_dmacfilter(struct otx2_nic *pfvf,
1145 struct ethtool_rx_flow_spec *fsp)
1146 {
1147 struct ethhdr *eth_mask = &fsp->m_u.ether_spec;
1148 struct ethhdr *eth_hdr = &fsp->h_u.ether_spec;
1149 u64 ring_cookie = fsp->ring_cookie;
1150 u32 flow_type;
1151
1152 if (!(pfvf->flags & OTX2_FLAG_DMACFLTR_SUPPORT))
1153 return false;
1154
1155 flow_type = fsp->flow_type & ~(FLOW_EXT | FLOW_MAC_EXT | FLOW_RSS);
1156
1157 /* CGX/RPM block dmac filtering configured for white listing
1158 * check for action other than DROP
1159 */
1160 if (flow_type == ETHER_FLOW && ring_cookie != RX_CLS_FLOW_DISC &&
1161 !ethtool_get_flow_spec_ring_vf(ring_cookie)) {
1162 if (is_zero_ether_addr(eth_mask->h_dest) &&
1163 is_valid_ether_addr(eth_hdr->h_dest))
1164 return true;
1165 }
1166
1167 return false;
1168 }
1169
1170 static int otx2_add_flow_msg(struct otx2_nic *pfvf, struct otx2_flow *flow)
1171 {
1172 struct otx2_flow_config *flow_cfg = pfvf->flow_cfg;
1173 struct npc_install_flow_req *req, treq = { 0 };
1174 u64 ring_cookie = flow->flow_spec.ring_cookie;
1175 #ifdef CONFIG_DCB
1176 int vlan_prio, qidx, pfc_rule = 0;
1177 #endif
1178 int err, vf = 0, off, sz;
1179 bool modify = false;
1180 u8 kw_type = 0;
1181 u8 *src, *dst;
1182 u16 x4_slots;
1183 bool is_x2;
1184
1185 if (is_cn20k(pfvf->pdev)) {
1186 err = otx2_mcam_pfl_info_get(pfvf, &is_x2, &x4_slots);
1187 if (err) {
1188 netdev_err(pfvf->netdev,
1189 "Error to retrieve NPC profile info, pcifunc=%#x\n",
1190 pfvf->pcifunc);
1191 return -EFAULT;
1192 }
1193
1194 if (!is_x2) {
1195 err = otx2_prepare_flow_request(&flow->flow_spec,
1196 &treq);
1197 if (err)
1198 return err;
1199
1200 err = otx2_get_kw_type(pfvf, &treq, &kw_type);
1201 if (err)
1202 return err;
1203
1204 modify = true;
1205 }
1206 }
1207
1208 mutex_lock(&pfvf->mbox.lock);
1209 req = otx2_mbox_alloc_msg_npc_install_flow(&pfvf->mbox);
1210 if (!req) {
1211 mutex_unlock(&pfvf->mbox.lock);
1212 return -ENOMEM;
1213 }
1214
1215 if (modify) {
1216 off = offsetof(struct npc_install_flow_req, packet);
1217 sz = sizeof(struct npc_install_flow_req) - off;
1218 dst = (u8 *)req + off;
1219 src = (u8 *)&treq + off;
1220
1221 memcpy(dst, src, sz);
1222 req->req_kw_type = kw_type;
1223 } else {
1224 err = otx2_prepare_flow_request(&flow->flow_spec, req);
1225 if (err) {
1226 /* free the allocated msg above */
1227 otx2_mbox_reset(&pfvf->mbox.mbox, 0);
1228 mutex_unlock(&pfvf->mbox.lock);
1229 return err;
1230 }
1231 }
1232
1233 netdev_dbg(pfvf->netdev,
1234 "flow entry (%u) installed at loc:%u kw_type=%u\n",
1235 flow_cfg->flow_ent[flow->location],
1236 flow->location, kw_type);
1237
1238 req->entry = flow->entry;
1239 req->intf = NIX_INTF_RX;
1240 req->set_cntr = 1;
1241 req->channel = pfvf->hw.rx_chan_base;
1242 if (ring_cookie == RX_CLS_FLOW_DISC) {
1243 req->op = NIX_RX_ACTIONOP_DROP;
1244 } else {
1245 /* change to unicast only if action of default entry is not
1246 * requested by user
1247 */
1248 if (flow->flow_spec.flow_type & FLOW_RSS) {
1249 req->op = NIX_RX_ACTIONOP_RSS;
1250 req->index = flow->rss_ctx_id;
1251 req->flow_key_alg = pfvf->hw.flowkey_alg_idx;
1252 } else {
1253 req->op = NIX_RX_ACTIONOP_UCAST;
1254 req->index = ethtool_get_flow_spec_ring(ring_cookie);
1255 }
1256 vf = ethtool_get_flow_spec_ring_vf(ring_cookie);
1257 if (vf > pci_num_vf(pfvf->pdev)) {
1258 mutex_unlock(&pfvf->mbox.lock);
1259 return -EINVAL;
1260 }
1261
1262 #ifdef CONFIG_DCB
1263 /* Identify PFC rule if PFC enabled and ntuple rule is vlan */
1264 if (!vf && (req->features & BIT_ULL(NPC_OUTER_VID)) &&
1265 pfvf->pfc_en && req->op != NIX_RX_ACTIONOP_RSS) {
1266 vlan_prio = ntohs(req->packet.vlan_tci) &
1267 ntohs(req->mask.vlan_tci);
1268
1269 /* Get the priority */
1270 vlan_prio >>= 13;
1271 flow->rule_type |= PFC_FLOWCTRL_RULE;
1272 /* Check if PFC enabled for this priority */
1273 if (pfvf->pfc_en & BIT(vlan_prio)) {
1274 pfc_rule = true;
1275 qidx = req->index;
1276 }
1277 }
1278 #endif
1279 }
1280
1281 /* ethtool ring_cookie has (VF + 1) for VF */
1282 if (vf) {
1283 req->vf = vf;
1284 flow->is_vf = true;
1285 flow->vf = vf;
1286 }
1287
1288 /* Send message to AF */
1289 err = otx2_sync_mbox_msg(&pfvf->mbox);
1290
1291 #ifdef CONFIG_DCB
1292 if (!err && pfc_rule)
1293 otx2_update_bpid_in_rqctx(pfvf, vlan_prio, qidx, true);
1294 #endif
1295
1296 mutex_unlock(&pfvf->mbox.lock);
1297 return err;
1298 }
1299
1300 static int otx2_add_flow_with_pfmac(struct otx2_nic *pfvf,
1301 struct otx2_flow *flow)
1302 {
1303 struct otx2_flow *pf_mac;
1304 struct ethhdr *eth_hdr;
1305
1306 pf_mac = kzalloc_obj(*pf_mac);
1307 if (!pf_mac)
1308 return -ENOMEM;
1309
1310 pf_mac->entry = 0;
1311 pf_mac->rule_type |= DMAC_FILTER_RULE;
1312 pf_mac->location = pfvf->flow_cfg->max_flows;
1313 memcpy(&pf_mac->flow_spec, &flow->flow_spec,
1314 sizeof(struct ethtool_rx_flow_spec));
1315 pf_mac->flow_spec.location = pf_mac->location;
1316
1317 /* Copy PF mac address */
1318 eth_hdr = &pf_mac->flow_spec.h_u.ether_spec;
1319 ether_addr_copy(eth_hdr->h_dest, pfvf->netdev->dev_addr);
1320
1321 /* Install DMAC filter with PF mac address */
1322 otx2_dmacflt_add(pfvf, eth_hdr->h_dest, 0);
1323
1324 otx2_add_flow_to_list(pfvf, pf_mac);
1325 pfvf->flow_cfg->nr_flows++;
1326 set_bit(0, pfvf->flow_cfg->dmacflt_bmap);
1327
1328 return 0;
1329 }
1330
1331 int otx2_add_flow(struct otx2_nic *pfvf, struct ethtool_rxnfc *nfc)
1332 {
1333 struct otx2_flow_config *flow_cfg = pfvf->flow_cfg;
1334 struct ethtool_rx_flow_spec *fsp = &nfc->fs;
1335 struct otx2_flow *flow;
1336 struct ethhdr *eth_hdr;
1337 bool new = false;
1338 int err = 0;
1339 u64 vf_num;
1340 u32 ring;
1341
1342 if (!flow_cfg->max_flows) {
1343 netdev_err(pfvf->netdev,
1344 "Ntuple rule count is 0, allocate and retry\n");
1345 return -EINVAL;
1346 }
1347
1348 ring = ethtool_get_flow_spec_ring(fsp->ring_cookie);
1349 if (!(pfvf->flags & OTX2_FLAG_NTUPLE_SUPPORT))
1350 return -ENOMEM;
1351
1352 /* Number of queues on a VF can be greater or less than
1353 * the PF's queue. Hence no need to check for the
1354 * queue count. Hence no need to check queue count if PF
1355 * is installing for its VF. Below is the expected vf_num value
1356 * based on the ethtool commands.
1357 *
1358 * e.g.
1359 * 1. ethtool -U <netdev> ... action -1 ==> vf_num:255
1360 * 2. ethtool -U <netdev> ... action <queue_num> ==> vf_num:0
1361 * 3. ethtool -U <netdev> ... vf <vf_idx> queue <queue_num> ==>
1362 * vf_num:vf_idx+1
1363 */
1364 vf_num = ethtool_get_flow_spec_ring_vf(fsp->ring_cookie);
1365 if (!is_otx2_vf(pfvf->pcifunc) && !vf_num &&
1366 ring >= pfvf->hw.rx_queues && fsp->ring_cookie != RX_CLS_FLOW_DISC)
1367 return -EINVAL;
1368
1369 if (fsp->location >= otx2_get_maxflows(flow_cfg))
1370 return -EINVAL;
1371
1372 flow = otx2_find_flow(pfvf, fsp->location);
1373 if (!flow) {
1374 flow = kzalloc_obj(*flow);
1375 if (!flow)
1376 return -ENOMEM;
1377 flow->location = fsp->location;
1378 flow->entry = flow_cfg->flow_ent[flow->location];
1379 new = true;
1380 }
1381 /* struct copy */
1382 flow->flow_spec = *fsp;
1383
1384 if (fsp->flow_type & FLOW_RSS)
1385 flow->rss_ctx_id = nfc->rss_context;
1386
1387 if (otx2_is_flow_rule_dmacfilter(pfvf, &flow->flow_spec)) {
1388 eth_hdr = &flow->flow_spec.h_u.ether_spec;
1389
1390 /* Sync dmac filter table with updated fields */
1391 if (flow->rule_type & DMAC_FILTER_RULE)
1392 return otx2_dmacflt_update(pfvf, eth_hdr->h_dest,
1393 flow->entry);
1394
1395 if (bitmap_full(flow_cfg->dmacflt_bmap,
1396 flow_cfg->dmacflt_max_flows)) {
1397 netdev_warn(pfvf->netdev,
1398 "Can't insert the rule %d as max allowed dmac filters are %d\n",
1399 flow->location +
1400 flow_cfg->dmacflt_max_flows,
1401 flow_cfg->dmacflt_max_flows);
1402 err = -EINVAL;
1403 if (new)
1404 kfree(flow);
1405 return err;
1406 }
1407
1408 /* Install PF mac address to DMAC filter list */
1409 if (!test_bit(0, flow_cfg->dmacflt_bmap))
1410 otx2_add_flow_with_pfmac(pfvf, flow);
1411
1412 flow->rule_type |= DMAC_FILTER_RULE;
1413 flow->entry = find_first_zero_bit(flow_cfg->dmacflt_bmap,
1414 flow_cfg->dmacflt_max_flows);
1415 fsp->location = flow_cfg->max_flows + flow->entry;
1416 flow->flow_spec.location = fsp->location;
1417 flow->location = fsp->location;
1418
1419 set_bit(flow->entry, flow_cfg->dmacflt_bmap);
1420 otx2_dmacflt_add(pfvf, eth_hdr->h_dest, flow->entry);
1421
1422 } else {
1423 if (flow->location >= pfvf->flow_cfg->max_flows) {
1424 netdev_warn(pfvf->netdev,
1425 "Can't insert non dmac ntuple rule at %d, allowed range %d-0\n",
1426 flow->location,
1427 flow_cfg->max_flows - 1);
1428 err = -EINVAL;
1429 } else {
1430 err = otx2_add_flow_msg(pfvf, flow);
1431 }
1432 }
1433
1434 if (err) {
1435 if (err == MBOX_MSG_INVALID)
1436 err = -EINVAL;
1437 if (new)
1438 kfree(flow);
1439 return err;
1440 }
1441
1442 /* add the new flow installed to list */
1443 if (new) {
1444 otx2_add_flow_to_list(pfvf, flow);
1445 flow_cfg->nr_flows++;
1446 }
1447
1448 if (flow->is_vf)
1449 netdev_info(pfvf->netdev,
1450 "Make sure that VF's queue number is within its queue limit\n");
1451 return 0;
1452 }
1453
1454 static int otx2_remove_flow_msg(struct otx2_nic *pfvf, u16 entry, bool all)
1455 {
1456 struct npc_delete_flow_req *req;
1457 int err;
1458
1459 mutex_lock(&pfvf->mbox.lock);
1460 req = otx2_mbox_alloc_msg_npc_delete_flow(&pfvf->mbox);
1461 if (!req) {
1462 mutex_unlock(&pfvf->mbox.lock);
1463 return -ENOMEM;
1464 }
1465
1466 req->entry = entry;
1467 if (all)
1468 req->all = 1;
1469
1470 /* Send message to AF */
1471 err = otx2_sync_mbox_msg(&pfvf->mbox);
1472 mutex_unlock(&pfvf->mbox.lock);
1473 return err;
1474 }
1475
1476 static void otx2_update_rem_pfmac(struct otx2_nic *pfvf, int req)
1477 {
1478 struct otx2_flow *iter;
1479 struct ethhdr *eth_hdr;
1480 bool found = false;
1481
1482 list_for_each_entry(iter, &pfvf->flow_cfg->flow_list, list) {
1483 if ((iter->rule_type & DMAC_FILTER_RULE) && iter->entry == 0) {
1484 eth_hdr = &iter->flow_spec.h_u.ether_spec;
1485 if (req == DMAC_ADDR_DEL) {
1486 otx2_dmacflt_remove(pfvf, eth_hdr->h_dest,
1487 0);
1488 clear_bit(0, pfvf->flow_cfg->dmacflt_bmap);
1489 found = true;
1490 } else {
1491 ether_addr_copy(eth_hdr->h_dest,
1492 pfvf->netdev->dev_addr);
1493
1494 otx2_dmacflt_update(pfvf, eth_hdr->h_dest, 0);
1495 }
1496 break;
1497 }
1498 }
1499
1500 if (found) {
1501 list_del(&iter->list);
1502 kfree(iter);
1503 pfvf->flow_cfg->nr_flows--;
1504 }
1505 }
1506
1507 int otx2_remove_flow(struct otx2_nic *pfvf, u32 location)
1508 {
1509 struct otx2_flow_config *flow_cfg = pfvf->flow_cfg;
1510 struct otx2_flow *flow;
1511 int err;
1512
1513 if (location >= otx2_get_maxflows(flow_cfg))
1514 return -EINVAL;
1515
1516 flow = otx2_find_flow(pfvf, location);
1517 if (!flow)
1518 return -ENOENT;
1519
1520 if (flow->rule_type & DMAC_FILTER_RULE) {
1521 struct ethhdr *eth_hdr = &flow->flow_spec.h_u.ether_spec;
1522
1523 /* user not allowed to remove dmac filter with interface mac */
1524 if (ether_addr_equal(pfvf->netdev->dev_addr, eth_hdr->h_dest))
1525 return -EPERM;
1526
1527 err = otx2_dmacflt_remove(pfvf, eth_hdr->h_dest,
1528 flow->entry);
1529 clear_bit(flow->entry, flow_cfg->dmacflt_bmap);
1530 /* If all dmac filters are removed delete macfilter with
1531 * interface mac address and configure CGX/RPM block in
1532 * promiscuous mode
1533 */
1534 if (bitmap_weight(flow_cfg->dmacflt_bmap,
1535 flow_cfg->dmacflt_max_flows) == 1)
1536 otx2_update_rem_pfmac(pfvf, DMAC_ADDR_DEL);
1537 } else {
1538 #ifdef CONFIG_DCB
1539 if (flow->rule_type & PFC_FLOWCTRL_RULE)
1540 otx2_update_bpid_in_rqctx(pfvf, 0,
1541 flow->flow_spec.ring_cookie,
1542 false);
1543 #endif
1544
1545 err = otx2_remove_flow_msg(pfvf, flow->entry, false);
1546 }
1547
1548 if (err)
1549 return err;
1550
1551 list_del(&flow->list);
1552 kfree(flow);
1553 flow_cfg->nr_flows--;
1554
1555 return 0;
1556 }
1557
1558 void otx2_rss_ctx_flow_del(struct otx2_nic *pfvf, int ctx_id)
1559 {
1560 struct otx2_flow *flow, *tmp;
1561 int err;
1562
1563 list_for_each_entry_safe(flow, tmp, &pfvf->flow_cfg->flow_list, list) {
1564 if (flow->rss_ctx_id != ctx_id)
1565 continue;
1566 err = otx2_remove_flow(pfvf, flow->location);
1567 if (err)
1568 netdev_warn(pfvf->netdev,
1569 "Can't delete the rule %d associated with this rss group err:%d",
1570 flow->location, err);
1571 }
1572 }
1573
1574 int otx2_destroy_ntuple_flows(struct otx2_nic *pfvf)
1575 {
1576 struct otx2_flow_config *flow_cfg = pfvf->flow_cfg;
1577 struct npc_delete_flow_req *req;
1578 struct otx2_flow *iter, *tmp;
1579 int err;
1580
1581 if (!(pfvf->flags & OTX2_FLAG_NTUPLE_SUPPORT))
1582 return 0;
1583
1584 if (!flow_cfg->max_flows)
1585 return 0;
1586
1587 mutex_lock(&pfvf->mbox.lock);
1588 req = otx2_mbox_alloc_msg_npc_delete_flow(&pfvf->mbox);
1589 if (!req) {
1590 mutex_unlock(&pfvf->mbox.lock);
1591 return -ENOMEM;
1592 }
1593
1594 req->start = flow_cfg->flow_ent[0];
1595 req->end = flow_cfg->flow_ent[flow_cfg->max_flows - 1];
1596 err = otx2_sync_mbox_msg(&pfvf->mbox);
1597 mutex_unlock(&pfvf->mbox.lock);
1598
1599 list_for_each_entry_safe(iter, tmp, &flow_cfg->flow_list, list) {
1600 list_del(&iter->list);
1601 kfree(iter);
1602 flow_cfg->nr_flows--;
1603 }
1604 return err;
1605 }
1606
1607 int otx2_destroy_mcam_flows(struct otx2_nic *pfvf)
1608 {
1609 struct otx2_flow_config *flow_cfg = pfvf->flow_cfg;
1610 struct npc_mcam_free_entry_req *req;
1611 struct otx2_flow *iter, *tmp;
1612 int err;
1613
1614 if (!(pfvf->flags & OTX2_FLAG_MCAM_ENTRIES_ALLOC))
1615 return 0;
1616
1617 /* remove all flows */
1618 err = otx2_remove_flow_msg(pfvf, 0, true);
1619 if (err)
1620 return err;
1621
1622 list_for_each_entry_safe(iter, tmp, &flow_cfg->flow_list, list) {
1623 list_del(&iter->list);
1624 kfree(iter);
1625 flow_cfg->nr_flows--;
1626 }
1627
1628 mutex_lock(&pfvf->mbox.lock);
1629 req = otx2_mbox_alloc_msg_npc_mcam_free_entry(&pfvf->mbox);
1630 if (!req) {
1631 mutex_unlock(&pfvf->mbox.lock);
1632 return -ENOMEM;
1633 }
1634
1635 req->all = 1;
1636 /* Send message to AF to free MCAM entries */
1637 err = otx2_sync_mbox_msg(&pfvf->mbox);
1638 if (err) {
1639 mutex_unlock(&pfvf->mbox.lock);
1640 return err;
1641 }
1642
1643 pfvf->flags &= ~OTX2_FLAG_MCAM_ENTRIES_ALLOC;
1644 flow_cfg->max_flows = 0;
1645 mutex_unlock(&pfvf->mbox.lock);
1646
1647 return 0;
1648 }
1649
1650 int otx2_install_rxvlan_offload_flow(struct otx2_nic *pfvf)
1651 {
1652 struct otx2_flow_config *flow_cfg = pfvf->flow_cfg;
1653 struct npc_install_flow_req *req;
1654 int err;
1655
1656 mutex_lock(&pfvf->mbox.lock);
1657 req = otx2_mbox_alloc_msg_npc_install_flow(&pfvf->mbox);
1658 if (!req) {
1659 mutex_unlock(&pfvf->mbox.lock);
1660 return -ENOMEM;
1661 }
1662
1663 req->entry = flow_cfg->def_ent[flow_cfg->rx_vlan_offset];
1664 req->intf = NIX_INTF_RX;
1665 ether_addr_copy(req->packet.dmac, pfvf->netdev->dev_addr);
1666 eth_broadcast_addr((u8 *)&req->mask.dmac);
1667 req->channel = pfvf->hw.rx_chan_base;
1668 req->op = NIX_RX_ACTION_DEFAULT;
1669 req->features = BIT_ULL(NPC_OUTER_VID) | BIT_ULL(NPC_DMAC);
1670 req->vtag0_valid = true;
1671 req->vtag0_type = NIX_AF_LFX_RX_VTAG_TYPE0;
1672
1673 /* Send message to AF */
1674 err = otx2_sync_mbox_msg(&pfvf->mbox);
1675 mutex_unlock(&pfvf->mbox.lock);
1676 return err;
1677 }
1678
1679 static int otx2_delete_rxvlan_offload_flow(struct otx2_nic *pfvf)
1680 {
1681 struct otx2_flow_config *flow_cfg = pfvf->flow_cfg;
1682 struct npc_delete_flow_req *req;
1683 int err;
1684
1685 mutex_lock(&pfvf->mbox.lock);
1686 req = otx2_mbox_alloc_msg_npc_delete_flow(&pfvf->mbox);
1687 if (!req) {
1688 mutex_unlock(&pfvf->mbox.lock);
1689 return -ENOMEM;
1690 }
1691
1692 req->entry = flow_cfg->def_ent[flow_cfg->rx_vlan_offset];
1693 /* Send message to AF */
1694 err = otx2_sync_mbox_msg(&pfvf->mbox);
1695 mutex_unlock(&pfvf->mbox.lock);
1696 return err;
1697 }
1698
1699 int otx2_enable_rxvlan(struct otx2_nic *pf, bool enable)
1700 {
1701 struct nix_vtag_config *req;
1702 struct mbox_msghdr *rsp_hdr;
1703 int err;
1704
1705 /* Dont have enough mcam entries */
1706 if (!(pf->flags & OTX2_FLAG_RX_VLAN_SUPPORT))
1707 return -ENOMEM;
1708
1709 if (enable) {
1710 err = otx2_install_rxvlan_offload_flow(pf);
1711 if (err)
1712 return err;
1713 } else {
1714 err = otx2_delete_rxvlan_offload_flow(pf);
1715 if (err)
1716 return err;
1717 }
1718
1719 mutex_lock(&pf->mbox.lock);
1720 req = otx2_mbox_alloc_msg_nix_vtag_cfg(&pf->mbox);
1721 if (!req) {
1722 mutex_unlock(&pf->mbox.lock);
1723 return -ENOMEM;
1724 }
1725
1726 /* config strip, capture and size */
1727 req->vtag_size = VTAGSIZE_T4;
1728 req->cfg_type = 1; /* rx vlan cfg */
1729 req->rx.vtag_type = NIX_AF_LFX_RX_VTAG_TYPE0;
1730 req->rx.strip_vtag = enable;
1731 req->rx.capture_vtag = enable;
1732
1733 err = otx2_sync_mbox_msg(&pf->mbox);
1734 if (err) {
1735 mutex_unlock(&pf->mbox.lock);
1736 return err;
1737 }
1738
1739 rsp_hdr = otx2_mbox_get_rsp(&pf->mbox.mbox, 0, &req->hdr);
1740 if (IS_ERR(rsp_hdr)) {
1741 mutex_unlock(&pf->mbox.lock);
1742 return PTR_ERR(rsp_hdr);
1743 }
1744
1745 mutex_unlock(&pf->mbox.lock);
1746 return rsp_hdr->rc;
1747 }
1748
1749 void otx2_dmacflt_reinstall_flows(struct otx2_nic *pf)
1750 {
1751 struct otx2_flow *iter;
1752 struct ethhdr *eth_hdr;
1753
1754 list_for_each_entry(iter, &pf->flow_cfg->flow_list, list) {
1755 if (iter->rule_type & DMAC_FILTER_RULE) {
1756 eth_hdr = &iter->flow_spec.h_u.ether_spec;
1757 otx2_dmacflt_add(pf, eth_hdr->h_dest,
1758 iter->entry);
1759 }
1760 }
1761 }
1762
1763 void otx2_dmacflt_update_pfmac_flow(struct otx2_nic *pfvf)
1764 {
1765 otx2_update_rem_pfmac(pfvf, DMAC_ADDR_UPDATE);
1766 }