net/cxgbe: support to match on ingress physical port
[dpdk.git] / drivers / net / cxgbe / cxgbe_flow.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2018 Chelsio Communications.
3  * All rights reserved.
4  */
5 #include "common.h"
6 #include "cxgbe_flow.h"
7
8 #define __CXGBE_FILL_FS(__v, __m, fs, elem, e) \
9 do { \
10         if (!((fs)->val.elem || (fs)->mask.elem)) { \
11                 (fs)->val.elem = (__v); \
12                 (fs)->mask.elem = (__m); \
13         } else { \
14                 return rte_flow_error_set(e, EINVAL, RTE_FLOW_ERROR_TYPE_ITEM, \
15                                           NULL, "a filter can be specified" \
16                                           " only once"); \
17         } \
18 } while (0)
19
20 #define __CXGBE_FILL_FS_MEMCPY(__v, __m, fs, elem) \
21 do { \
22         memcpy(&(fs)->val.elem, &(__v), sizeof(__v)); \
23         memcpy(&(fs)->mask.elem, &(__m), sizeof(__m)); \
24 } while (0)
25
26 #define CXGBE_FILL_FS(v, m, elem) \
27         __CXGBE_FILL_FS(v, m, fs, elem, e)
28
29 #define CXGBE_FILL_FS_MEMCPY(v, m, elem) \
30         __CXGBE_FILL_FS_MEMCPY(v, m, fs, elem)
31
32 static int
33 cxgbe_validate_item(const struct rte_flow_item *i, struct rte_flow_error *e)
34 {
35         /* rte_flow specification does not allow it. */
36         if (!i->spec && (i->mask ||  i->last))
37                 return rte_flow_error_set(e, EINVAL, RTE_FLOW_ERROR_TYPE_ITEM,
38                                    i, "last or mask given without spec");
39         /*
40          * We don't support it.
41          * Although, we can support values in last as 0's or last == spec.
42          * But this will not provide user with any additional functionality
43          * and will only increase the complexity for us.
44          */
45         if (i->last)
46                 return rte_flow_error_set(e, ENOTSUP, RTE_FLOW_ERROR_TYPE_ITEM,
47                                    i, "last is not supported by chelsio pmd");
48         return 0;
49 }
50
51 static void
52 cxgbe_fill_filter_region(struct adapter *adap,
53                          struct ch_filter_specification *fs)
54 {
55         struct tp_params *tp = &adap->params.tp;
56         u64 hash_filter_mask = tp->hash_filter_mask;
57         u64 ntuple_mask = 0;
58
59         fs->cap = 0;
60
61         if (!is_hashfilter(adap))
62                 return;
63
64         if (fs->type) {
65                 uint8_t biton[16] = {0xff, 0xff, 0xff, 0xff,
66                                      0xff, 0xff, 0xff, 0xff,
67                                      0xff, 0xff, 0xff, 0xff,
68                                      0xff, 0xff, 0xff, 0xff};
69                 uint8_t bitoff[16] = {0};
70
71                 if (!memcmp(fs->val.lip, bitoff, sizeof(bitoff)) ||
72                     !memcmp(fs->val.fip, bitoff, sizeof(bitoff)) ||
73                     memcmp(fs->mask.lip, biton, sizeof(biton)) ||
74                     memcmp(fs->mask.fip, biton, sizeof(biton)))
75                         return;
76         } else {
77                 uint32_t biton  = 0xffffffff;
78                 uint32_t bitoff = 0x0U;
79
80                 if (!memcmp(fs->val.lip, &bitoff, sizeof(bitoff)) ||
81                     !memcmp(fs->val.fip, &bitoff, sizeof(bitoff)) ||
82                     memcmp(fs->mask.lip, &biton, sizeof(biton)) ||
83                     memcmp(fs->mask.fip, &biton, sizeof(biton)))
84                         return;
85         }
86
87         if (!fs->val.lport || fs->mask.lport != 0xffff)
88                 return;
89         if (!fs->val.fport || fs->mask.fport != 0xffff)
90                 return;
91
92         if (tp->protocol_shift >= 0)
93                 ntuple_mask |= (u64)fs->mask.proto << tp->protocol_shift;
94         if (tp->ethertype_shift >= 0)
95                 ntuple_mask |= (u64)fs->mask.ethtype << tp->ethertype_shift;
96         if (tp->port_shift >= 0)
97                 ntuple_mask |= (u64)fs->mask.iport << tp->port_shift;
98
99         if (ntuple_mask != hash_filter_mask)
100                 return;
101
102         fs->cap = 1;    /* use hash region */
103 }
104
105 static int
106 ch_rte_parsetype_port(const void *dmask, const struct rte_flow_item *item,
107                       struct ch_filter_specification *fs,
108                       struct rte_flow_error *e)
109 {
110         const struct rte_flow_item_phy_port *val = item->spec;
111         const struct rte_flow_item_phy_port *umask = item->mask;
112         const struct rte_flow_item_phy_port *mask;
113
114         mask = umask ? umask : (const struct rte_flow_item_phy_port *)dmask;
115
116         if (val->index > 0x7)
117                 return rte_flow_error_set(e, EINVAL, RTE_FLOW_ERROR_TYPE_ITEM,
118                                           item,
119                                           "port index upto 0x7 is supported");
120
121         CXGBE_FILL_FS(val->index, mask->index, iport);
122
123         return 0;
124 }
125
126 static int
127 ch_rte_parsetype_udp(const void *dmask, const struct rte_flow_item *item,
128                      struct ch_filter_specification *fs,
129                      struct rte_flow_error *e)
130 {
131         const struct rte_flow_item_udp *val = item->spec;
132         const struct rte_flow_item_udp *umask = item->mask;
133         const struct rte_flow_item_udp *mask;
134
135         mask = umask ? umask : (const struct rte_flow_item_udp *)dmask;
136
137         if (mask->hdr.dgram_len || mask->hdr.dgram_cksum)
138                 return rte_flow_error_set(e, ENOTSUP, RTE_FLOW_ERROR_TYPE_ITEM,
139                                           item,
140                                           "udp: only src/dst port supported");
141
142         CXGBE_FILL_FS(IPPROTO_UDP, 0xff, proto);
143         if (!val)
144                 return 0;
145         CXGBE_FILL_FS(be16_to_cpu(val->hdr.src_port),
146                       be16_to_cpu(mask->hdr.src_port), fport);
147         CXGBE_FILL_FS(be16_to_cpu(val->hdr.dst_port),
148                       be16_to_cpu(mask->hdr.dst_port), lport);
149         return 0;
150 }
151
152 static int
153 ch_rte_parsetype_tcp(const void *dmask, const struct rte_flow_item *item,
154                      struct ch_filter_specification *fs,
155                      struct rte_flow_error *e)
156 {
157         const struct rte_flow_item_tcp *val = item->spec;
158         const struct rte_flow_item_tcp *umask = item->mask;
159         const struct rte_flow_item_tcp *mask;
160
161         mask = umask ? umask : (const struct rte_flow_item_tcp *)dmask;
162
163         if (mask->hdr.sent_seq || mask->hdr.recv_ack || mask->hdr.data_off ||
164             mask->hdr.tcp_flags || mask->hdr.rx_win || mask->hdr.cksum ||
165             mask->hdr.tcp_urp)
166                 return rte_flow_error_set(e, ENOTSUP, RTE_FLOW_ERROR_TYPE_ITEM,
167                                           item,
168                                           "tcp: only src/dst port supported");
169
170         CXGBE_FILL_FS(IPPROTO_TCP, 0xff, proto);
171         if (!val)
172                 return 0;
173         CXGBE_FILL_FS(be16_to_cpu(val->hdr.src_port),
174                       be16_to_cpu(mask->hdr.src_port), fport);
175         CXGBE_FILL_FS(be16_to_cpu(val->hdr.dst_port),
176                       be16_to_cpu(mask->hdr.dst_port), lport);
177         return 0;
178 }
179
180 static int
181 ch_rte_parsetype_ipv4(const void *dmask, const struct rte_flow_item *item,
182                       struct ch_filter_specification *fs,
183                       struct rte_flow_error *e)
184 {
185         const struct rte_flow_item_ipv4 *val = item->spec;
186         const struct rte_flow_item_ipv4 *umask = item->mask;
187         const struct rte_flow_item_ipv4 *mask;
188
189         mask = umask ? umask : (const struct rte_flow_item_ipv4 *)dmask;
190
191         if (mask->hdr.time_to_live || mask->hdr.type_of_service)
192                 return rte_flow_error_set(e, ENOTSUP, RTE_FLOW_ERROR_TYPE_ITEM,
193                                           item, "ttl/tos are not supported");
194
195         fs->type = FILTER_TYPE_IPV4;
196         CXGBE_FILL_FS(ETHER_TYPE_IPv4, 0xffff, ethtype);
197         if (!val)
198                 return 0; /* ipv4 wild card */
199
200         CXGBE_FILL_FS(val->hdr.next_proto_id, mask->hdr.next_proto_id, proto);
201         CXGBE_FILL_FS_MEMCPY(val->hdr.dst_addr, mask->hdr.dst_addr, lip);
202         CXGBE_FILL_FS_MEMCPY(val->hdr.src_addr, mask->hdr.src_addr, fip);
203
204         return 0;
205 }
206
207 static int
208 ch_rte_parsetype_ipv6(const void *dmask, const struct rte_flow_item *item,
209                       struct ch_filter_specification *fs,
210                       struct rte_flow_error *e)
211 {
212         const struct rte_flow_item_ipv6 *val = item->spec;
213         const struct rte_flow_item_ipv6 *umask = item->mask;
214         const struct rte_flow_item_ipv6 *mask;
215
216         mask = umask ? umask : (const struct rte_flow_item_ipv6 *)dmask;
217
218         if (mask->hdr.vtc_flow ||
219             mask->hdr.payload_len || mask->hdr.hop_limits)
220                 return rte_flow_error_set(e, ENOTSUP, RTE_FLOW_ERROR_TYPE_ITEM,
221                                           item,
222                                           "tc/flow/hop are not supported");
223
224         fs->type = FILTER_TYPE_IPV6;
225         CXGBE_FILL_FS(ETHER_TYPE_IPv6, 0xffff, ethtype);
226         if (!val)
227                 return 0; /* ipv6 wild card */
228
229         CXGBE_FILL_FS(val->hdr.proto, mask->hdr.proto, proto);
230         CXGBE_FILL_FS_MEMCPY(val->hdr.dst_addr, mask->hdr.dst_addr, lip);
231         CXGBE_FILL_FS_MEMCPY(val->hdr.src_addr, mask->hdr.src_addr, fip);
232
233         return 0;
234 }
235
236 static int
237 cxgbe_rtef_parse_attr(struct rte_flow *flow, const struct rte_flow_attr *attr,
238                       struct rte_flow_error *e)
239 {
240         if (attr->egress)
241                 return rte_flow_error_set(e, ENOTSUP, RTE_FLOW_ERROR_TYPE_ATTR,
242                                           attr, "attribute:<egress> is"
243                                           " not supported !");
244         if (attr->group > 0)
245                 return rte_flow_error_set(e, ENOTSUP, RTE_FLOW_ERROR_TYPE_ATTR,
246                                           attr, "group parameter is"
247                                           " not supported.");
248
249         flow->fidx = attr->priority ? attr->priority - 1 : FILTER_ID_MAX;
250
251         return 0;
252 }
253
254 static inline int check_rxq(struct rte_eth_dev *dev, uint16_t rxq)
255 {
256         struct port_info *pi = ethdev2pinfo(dev);
257
258         if (rxq > pi->n_rx_qsets)
259                 return -EINVAL;
260         return 0;
261 }
262
263 static int cxgbe_validate_fidxondel(struct filter_entry *f, unsigned int fidx)
264 {
265         struct adapter *adap = ethdev2adap(f->dev);
266         struct ch_filter_specification fs = f->fs;
267
268         if (fidx >= adap->tids.nftids) {
269                 dev_err(adap, "invalid flow index %d.\n", fidx);
270                 return -EINVAL;
271         }
272         if (!is_filter_set(&adap->tids, fidx, fs.type)) {
273                 dev_err(adap, "Already free fidx:%d f:%p\n", fidx, f);
274                 return -EINVAL;
275         }
276
277         return 0;
278 }
279
280 static int
281 cxgbe_validate_fidxonadd(struct ch_filter_specification *fs,
282                          struct adapter *adap, unsigned int fidx)
283 {
284         if (is_filter_set(&adap->tids, fidx, fs->type)) {
285                 dev_err(adap, "filter index: %d is busy.\n", fidx);
286                 return -EBUSY;
287         }
288         if (fidx >= adap->tids.nftids) {
289                 dev_err(adap, "filter index (%u) >= max(%u)\n",
290                         fidx, adap->tids.nftids);
291                 return -ERANGE;
292         }
293
294         return 0;
295 }
296
297 static int
298 cxgbe_verify_fidx(struct rte_flow *flow, unsigned int fidx, uint8_t del)
299 {
300         if (flow->fs.cap)
301                 return 0; /* Hash filters */
302         return del ? cxgbe_validate_fidxondel(flow->f, fidx) :
303                 cxgbe_validate_fidxonadd(&flow->fs,
304                                          ethdev2adap(flow->dev), fidx);
305 }
306
307 static int cxgbe_get_fidx(struct rte_flow *flow, unsigned int *fidx)
308 {
309         struct ch_filter_specification *fs = &flow->fs;
310         struct adapter *adap = ethdev2adap(flow->dev);
311
312         /* For tcam get the next available slot, if default value specified */
313         if (flow->fidx == FILTER_ID_MAX) {
314                 int idx;
315
316                 idx = cxgbe_alloc_ftid(adap, fs->type);
317                 if (idx < 0) {
318                         dev_err(adap, "unable to get a filter index in tcam\n");
319                         return -ENOMEM;
320                 }
321                 *fidx = (unsigned int)idx;
322         } else {
323                 *fidx = flow->fidx;
324         }
325
326         return 0;
327 }
328
329 static int
330 cxgbe_rtef_parse_actions(struct rte_flow *flow,
331                          const struct rte_flow_action action[],
332                          struct rte_flow_error *e)
333 {
334         struct ch_filter_specification *fs = &flow->fs;
335         const struct rte_flow_action_queue *q;
336         const struct rte_flow_action *a;
337         char abit = 0;
338
339         for (a = action; a->type != RTE_FLOW_ACTION_TYPE_END; a++) {
340                 switch (a->type) {
341                 case RTE_FLOW_ACTION_TYPE_VOID:
342                         continue;
343                 case RTE_FLOW_ACTION_TYPE_DROP:
344                         if (abit++)
345                                 return rte_flow_error_set(e, EINVAL,
346                                                 RTE_FLOW_ERROR_TYPE_ACTION, a,
347                                                 "specify only 1 pass/drop");
348                         fs->action = FILTER_DROP;
349                         break;
350                 case RTE_FLOW_ACTION_TYPE_QUEUE:
351                         q = (const struct rte_flow_action_queue *)a->conf;
352                         if (!q)
353                                 return rte_flow_error_set(e, EINVAL,
354                                                 RTE_FLOW_ERROR_TYPE_ACTION, q,
355                                                 "specify rx queue index");
356                         if (check_rxq(flow->dev, q->index))
357                                 return rte_flow_error_set(e, EINVAL,
358                                                 RTE_FLOW_ERROR_TYPE_ACTION, q,
359                                                 "Invalid rx queue");
360                         if (abit++)
361                                 return rte_flow_error_set(e, EINVAL,
362                                                 RTE_FLOW_ERROR_TYPE_ACTION, a,
363                                                 "specify only 1 pass/drop");
364                         fs->action = FILTER_PASS;
365                         fs->dirsteer = 1;
366                         fs->iq = q->index;
367                         break;
368                 case RTE_FLOW_ACTION_TYPE_COUNT:
369                         fs->hitcnts = 1;
370                         break;
371                 default:
372                         /* Not supported action : return error */
373                         return rte_flow_error_set(e, ENOTSUP,
374                                                   RTE_FLOW_ERROR_TYPE_ACTION,
375                                                   a, "Action not supported");
376                 }
377         }
378
379         return 0;
380 }
381
382 struct chrte_fparse parseitem[] = {
383                 [RTE_FLOW_ITEM_TYPE_PHY_PORT] = {
384                 .fptr = ch_rte_parsetype_port,
385                 .dmask = &(const struct rte_flow_item_phy_port){
386                         .index = 0x7,
387                 }
388         },
389
390         [RTE_FLOW_ITEM_TYPE_IPV4] = {
391                 .fptr  = ch_rte_parsetype_ipv4,
392                 .dmask = &rte_flow_item_ipv4_mask,
393         },
394
395         [RTE_FLOW_ITEM_TYPE_IPV6] = {
396                 .fptr  = ch_rte_parsetype_ipv6,
397                 .dmask = &rte_flow_item_ipv6_mask,
398         },
399
400         [RTE_FLOW_ITEM_TYPE_UDP] = {
401                 .fptr  = ch_rte_parsetype_udp,
402                 .dmask = &rte_flow_item_udp_mask,
403         },
404
405         [RTE_FLOW_ITEM_TYPE_TCP] = {
406                 .fptr  = ch_rte_parsetype_tcp,
407                 .dmask = &rte_flow_item_tcp_mask,
408         },
409 };
410
411 static int
412 cxgbe_rtef_parse_items(struct rte_flow *flow,
413                        const struct rte_flow_item items[],
414                        struct rte_flow_error *e)
415 {
416         struct adapter *adap = ethdev2adap(flow->dev);
417         const struct rte_flow_item *i;
418         char repeat[ARRAY_SIZE(parseitem)] = {0};
419
420         for (i = items; i->type != RTE_FLOW_ITEM_TYPE_END; i++) {
421                 struct chrte_fparse *idx = &flow->item_parser[i->type];
422                 int ret;
423
424                 if (i->type > ARRAY_SIZE(parseitem))
425                         return rte_flow_error_set(e, ENOTSUP,
426                                                   RTE_FLOW_ERROR_TYPE_ITEM,
427                                                   i, "Item not supported");
428
429                 switch (i->type) {
430                 case RTE_FLOW_ITEM_TYPE_VOID:
431                         continue;
432                 default:
433                         /* check if item is repeated */
434                         if (repeat[i->type])
435                                 return rte_flow_error_set(e, ENOTSUP,
436                                                 RTE_FLOW_ERROR_TYPE_ITEM, i,
437                                                 "parse items cannot be repeated (except void)");
438                         repeat[i->type] = 1;
439
440                         /* validate the item */
441                         ret = cxgbe_validate_item(i, e);
442                         if (ret)
443                                 return ret;
444
445                         if (!idx || !idx->fptr) {
446                                 return rte_flow_error_set(e, ENOTSUP,
447                                                 RTE_FLOW_ERROR_TYPE_ITEM, i,
448                                                 "Item not supported");
449                         } else {
450                                 ret = idx->fptr(idx->dmask, i, &flow->fs, e);
451                                 if (ret)
452                                         return ret;
453                         }
454                 }
455         }
456
457         cxgbe_fill_filter_region(adap, &flow->fs);
458
459         return 0;
460 }
461
462 static int
463 cxgbe_flow_parse(struct rte_flow *flow,
464                  const struct rte_flow_attr *attr,
465                  const struct rte_flow_item item[],
466                  const struct rte_flow_action action[],
467                  struct rte_flow_error *e)
468 {
469         int ret;
470
471         /* parse user request into ch_filter_specification */
472         ret = cxgbe_rtef_parse_attr(flow, attr, e);
473         if (ret)
474                 return ret;
475         ret = cxgbe_rtef_parse_items(flow, item, e);
476         if (ret)
477                 return ret;
478         return cxgbe_rtef_parse_actions(flow, action, e);
479 }
480
481 static int __cxgbe_flow_create(struct rte_eth_dev *dev, struct rte_flow *flow)
482 {
483         struct ch_filter_specification *fs = &flow->fs;
484         struct adapter *adap = ethdev2adap(dev);
485         struct tid_info *t = &adap->tids;
486         struct filter_ctx ctx;
487         unsigned int fidx;
488         int err;
489
490         if (cxgbe_get_fidx(flow, &fidx))
491                 return -ENOMEM;
492         if (cxgbe_verify_fidx(flow, fidx, 0))
493                 return -1;
494
495         t4_init_completion(&ctx.completion);
496         /* go create the filter */
497         err = cxgbe_set_filter(dev, fidx, fs, &ctx);
498         if (err) {
499                 dev_err(adap, "Error %d while creating filter.\n", err);
500                 return err;
501         }
502
503         /* Poll the FW for reply */
504         err = cxgbe_poll_for_completion(&adap->sge.fw_evtq,
505                                         CXGBE_FLOW_POLL_US,
506                                         CXGBE_FLOW_POLL_CNT,
507                                         &ctx.completion);
508         if (err) {
509                 dev_err(adap, "Filter set operation timed out (%d)\n", err);
510                 return err;
511         }
512         if (ctx.result) {
513                 dev_err(adap, "Hardware error %d while creating the filter.\n",
514                         ctx.result);
515                 return ctx.result;
516         }
517
518         if (fs->cap) { /* to destroy the filter */
519                 flow->fidx = ctx.tid;
520                 flow->f = lookup_tid(t, ctx.tid);
521         } else {
522                 flow->fidx = fidx;
523                 flow->f = &adap->tids.ftid_tab[fidx];
524         }
525
526         return 0;
527 }
528
529 static struct rte_flow *
530 cxgbe_flow_create(struct rte_eth_dev *dev,
531                   const struct rte_flow_attr *attr,
532                   const struct rte_flow_item item[],
533                   const struct rte_flow_action action[],
534                   struct rte_flow_error *e)
535 {
536         struct rte_flow *flow;
537         int ret;
538
539         flow = t4_os_alloc(sizeof(struct rte_flow));
540         if (!flow) {
541                 rte_flow_error_set(e, ENOMEM, RTE_FLOW_ERROR_TYPE_HANDLE,
542                                    NULL, "Unable to allocate memory for"
543                                    " filter_entry");
544                 return NULL;
545         }
546
547         flow->item_parser = parseitem;
548         flow->dev = dev;
549
550         if (cxgbe_flow_parse(flow, attr, item, action, e)) {
551                 t4_os_free(flow);
552                 return NULL;
553         }
554
555         /* go, interact with cxgbe_filter */
556         ret = __cxgbe_flow_create(dev, flow);
557         if (ret) {
558                 rte_flow_error_set(e, ret, RTE_FLOW_ERROR_TYPE_HANDLE,
559                                    NULL, "Unable to create flow rule");
560                 t4_os_free(flow);
561                 return NULL;
562         }
563
564         flow->f->private = flow; /* Will be used during flush */
565
566         return flow;
567 }
568
569 static int __cxgbe_flow_destroy(struct rte_eth_dev *dev, struct rte_flow *flow)
570 {
571         struct adapter *adap = ethdev2adap(dev);
572         struct filter_entry *f = flow->f;
573         struct ch_filter_specification *fs;
574         struct filter_ctx ctx;
575         int err;
576
577         fs = &f->fs;
578         if (cxgbe_verify_fidx(flow, flow->fidx, 1))
579                 return -1;
580
581         t4_init_completion(&ctx.completion);
582         err = cxgbe_del_filter(dev, flow->fidx, fs, &ctx);
583         if (err) {
584                 dev_err(adap, "Error %d while deleting filter.\n", err);
585                 return err;
586         }
587
588         /* Poll the FW for reply */
589         err = cxgbe_poll_for_completion(&adap->sge.fw_evtq,
590                                         CXGBE_FLOW_POLL_US,
591                                         CXGBE_FLOW_POLL_CNT,
592                                         &ctx.completion);
593         if (err) {
594                 dev_err(adap, "Filter delete operation timed out (%d)\n", err);
595                 return err;
596         }
597         if (ctx.result) {
598                 dev_err(adap, "Hardware error %d while deleting the filter.\n",
599                         ctx.result);
600                 return ctx.result;
601         }
602
603         return 0;
604 }
605
606 static int
607 cxgbe_flow_destroy(struct rte_eth_dev *dev, struct rte_flow *flow,
608                    struct rte_flow_error *e)
609 {
610         int ret;
611
612         ret = __cxgbe_flow_destroy(dev, flow);
613         if (ret)
614                 return rte_flow_error_set(e, ret, RTE_FLOW_ERROR_TYPE_HANDLE,
615                                           flow, "error destroying filter.");
616         t4_os_free(flow);
617         return 0;
618 }
619
620 static int __cxgbe_flow_query(struct rte_flow *flow, u64 *count,
621                               u64 *byte_count)
622 {
623         struct adapter *adap = ethdev2adap(flow->dev);
624         struct ch_filter_specification fs = flow->f->fs;
625         unsigned int fidx = flow->fidx;
626         int ret = 0;
627
628         ret = cxgbe_get_filter_count(adap, fidx, count, fs.cap, 0);
629         if (ret)
630                 return ret;
631         return cxgbe_get_filter_count(adap, fidx, byte_count, fs.cap, 1);
632 }
633
634 static int
635 cxgbe_flow_query(struct rte_eth_dev *dev, struct rte_flow *flow,
636                  const struct rte_flow_action *action, void *data,
637                  struct rte_flow_error *e)
638 {
639         struct ch_filter_specification fs;
640         struct rte_flow_query_count *c;
641         struct filter_entry *f;
642         int ret;
643
644         RTE_SET_USED(dev);
645
646         f = flow->f;
647         fs = f->fs;
648
649         if (action->type != RTE_FLOW_ACTION_TYPE_COUNT)
650                 return rte_flow_error_set(e, ENOTSUP,
651                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
652                                           "only count supported for query");
653
654         /*
655          * This is a valid operation, Since we are allowed to do chelsio
656          * specific operations in rte side of our code but not vise-versa
657          *
658          * So, fs can be queried/modified here BUT rte_flow_query_count
659          * cannot be worked on by the lower layer since we want to maintain
660          * it as rte_flow agnostic.
661          */
662         if (!fs.hitcnts)
663                 return rte_flow_error_set(e, EINVAL, RTE_FLOW_ERROR_TYPE_ACTION,
664                                           &fs, "filter hit counters were not"
665                                           " enabled during filter creation");
666
667         c = (struct rte_flow_query_count *)data;
668         ret = __cxgbe_flow_query(flow, &c->hits, &c->bytes);
669         if (ret)
670                 return rte_flow_error_set(e, -ret, RTE_FLOW_ERROR_TYPE_ACTION,
671                                           f, "cxgbe pmd failed to"
672                                           " perform query");
673
674         /* Query was successful */
675         c->bytes_set = 1;
676         c->hits_set = 1;
677
678         return 0; /* success / partial_success */
679 }
680
681 static int
682 cxgbe_flow_validate(struct rte_eth_dev *dev,
683                     const struct rte_flow_attr *attr,
684                     const struct rte_flow_item item[],
685                     const struct rte_flow_action action[],
686                     struct rte_flow_error *e)
687 {
688         struct adapter *adap = ethdev2adap(dev);
689         struct rte_flow *flow;
690         unsigned int fidx;
691         int ret;
692
693         flow = t4_os_alloc(sizeof(struct rte_flow));
694         if (!flow)
695                 return rte_flow_error_set(e, ENOMEM, RTE_FLOW_ERROR_TYPE_HANDLE,
696                                 NULL,
697                                 "Unable to allocate memory for filter_entry");
698
699         flow->item_parser = parseitem;
700         flow->dev = dev;
701
702         ret = cxgbe_flow_parse(flow, attr, item, action, e);
703         if (ret) {
704                 t4_os_free(flow);
705                 return ret;
706         }
707
708         if (validate_filter(adap, &flow->fs)) {
709                 t4_os_free(flow);
710                 return rte_flow_error_set(e, EINVAL, RTE_FLOW_ERROR_TYPE_HANDLE,
711                                 NULL,
712                                 "validation failed. Check f/w config file.");
713         }
714
715         if (cxgbe_get_fidx(flow, &fidx)) {
716                 t4_os_free(flow);
717                 return rte_flow_error_set(e, ENOMEM, RTE_FLOW_ERROR_TYPE_HANDLE,
718                                           NULL, "no memory in tcam.");
719         }
720
721         if (cxgbe_verify_fidx(flow, fidx, 0)) {
722                 t4_os_free(flow);
723                 return rte_flow_error_set(e, EINVAL, RTE_FLOW_ERROR_TYPE_HANDLE,
724                                           NULL, "validation failed");
725         }
726
727         t4_os_free(flow);
728         return 0;
729 }
730
731 /*
732  * @ret : > 0 filter destroyed succsesfully
733  *        < 0 error destroying filter
734  *        == 1 filter not active / not found
735  */
736 static int
737 cxgbe_check_n_destroy(struct filter_entry *f, struct rte_eth_dev *dev,
738                       struct rte_flow_error *e)
739 {
740         if (f && (f->valid || f->pending) &&
741             f->dev == dev && /* Only if user has asked for this port */
742              f->private) /* We (rte_flow) created this filter */
743                 return cxgbe_flow_destroy(dev, (struct rte_flow *)f->private,
744                                           e);
745         return 1;
746 }
747
748 static int cxgbe_flow_flush(struct rte_eth_dev *dev, struct rte_flow_error *e)
749 {
750         struct adapter *adap = ethdev2adap(dev);
751         unsigned int i;
752         int ret = 0;
753
754         if (adap->tids.ftid_tab) {
755                 struct filter_entry *f = &adap->tids.ftid_tab[0];
756
757                 for (i = 0; i < adap->tids.nftids; i++, f++) {
758                         ret = cxgbe_check_n_destroy(f, dev, e);
759                         if (ret < 0)
760                                 goto out;
761                 }
762         }
763
764         if (is_hashfilter(adap) && adap->tids.tid_tab) {
765                 struct filter_entry *f;
766
767                 for (i = adap->tids.hash_base; i <= adap->tids.ntids; i++) {
768                         f = (struct filter_entry *)adap->tids.tid_tab[i];
769
770                         ret = cxgbe_check_n_destroy(f, dev, e);
771                         if (ret < 0)
772                                 goto out;
773                 }
774         }
775
776 out:
777         return ret >= 0 ? 0 : ret;
778 }
779
780 static const struct rte_flow_ops cxgbe_flow_ops = {
781         .validate       = cxgbe_flow_validate,
782         .create         = cxgbe_flow_create,
783         .destroy        = cxgbe_flow_destroy,
784         .flush          = cxgbe_flow_flush,
785         .query          = cxgbe_flow_query,
786         .isolate        = NULL,
787 };
788
789 int
790 cxgbe_dev_filter_ctrl(struct rte_eth_dev *dev,
791                       enum rte_filter_type filter_type,
792                       enum rte_filter_op filter_op,
793                       void *arg)
794 {
795         int ret = 0;
796
797         RTE_SET_USED(dev);
798         switch (filter_type) {
799         case RTE_ETH_FILTER_GENERIC:
800                 if (filter_op != RTE_ETH_FILTER_GET)
801                         return -EINVAL;
802                 *(const void **)arg = &cxgbe_flow_ops;
803                 break;
804         default:
805                 ret = -ENOTSUP;
806                 break;
807         }
808         return ret;
809 }