bee3bd64033bc54b34ee67bf2981ec495802a379
[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         if (tp->macmatch_shift >= 0)
99                 ntuple_mask |= (u64)fs->mask.macidx << tp->macmatch_shift;
100
101         if (ntuple_mask != hash_filter_mask)
102                 return;
103
104         fs->cap = 1;    /* use hash region */
105 }
106
107 static int
108 ch_rte_parsetype_eth(const void *dmask, const struct rte_flow_item *item,
109                      struct ch_filter_specification *fs,
110                      struct rte_flow_error *e)
111 {
112         const struct rte_flow_item_eth *spec = item->spec;
113         const struct rte_flow_item_eth *umask = item->mask;
114         const struct rte_flow_item_eth *mask;
115
116         /* If user has not given any mask, then use chelsio supported mask. */
117         mask = umask ? umask : (const struct rte_flow_item_eth *)dmask;
118
119         /* we don't support SRC_MAC filtering*/
120         if (!is_zero_ether_addr(&mask->src))
121                 return rte_flow_error_set(e, ENOTSUP, RTE_FLOW_ERROR_TYPE_ITEM,
122                                           item,
123                                           "src mac filtering not supported");
124
125         if (!is_zero_ether_addr(&mask->dst)) {
126                 const u8 *addr = (const u8 *)&spec->dst.addr_bytes[0];
127                 const u8 *m = (const u8 *)&mask->dst.addr_bytes[0];
128                 struct rte_flow *flow = (struct rte_flow *)fs->private;
129                 struct port_info *pi = (struct port_info *)
130                                         (flow->dev->data->dev_private);
131                 int idx;
132
133                 idx = cxgbe_mpstcam_alloc(pi, addr, m);
134                 if (idx <= 0)
135                         return rte_flow_error_set(e, idx,
136                                                   RTE_FLOW_ERROR_TYPE_ITEM,
137                                                   NULL, "unable to allocate mac"
138                                                   " entry in h/w");
139                 CXGBE_FILL_FS(idx, 0x1ff, macidx);
140         }
141
142         CXGBE_FILL_FS(be16_to_cpu(spec->type),
143                       be16_to_cpu(mask->type), ethtype);
144         return 0;
145 }
146
147 static int
148 ch_rte_parsetype_port(const void *dmask, const struct rte_flow_item *item,
149                       struct ch_filter_specification *fs,
150                       struct rte_flow_error *e)
151 {
152         const struct rte_flow_item_phy_port *val = item->spec;
153         const struct rte_flow_item_phy_port *umask = item->mask;
154         const struct rte_flow_item_phy_port *mask;
155
156         mask = umask ? umask : (const struct rte_flow_item_phy_port *)dmask;
157
158         if (val->index > 0x7)
159                 return rte_flow_error_set(e, EINVAL, RTE_FLOW_ERROR_TYPE_ITEM,
160                                           item,
161                                           "port index upto 0x7 is supported");
162
163         CXGBE_FILL_FS(val->index, mask->index, iport);
164
165         return 0;
166 }
167
168 static int
169 ch_rte_parsetype_udp(const void *dmask, const struct rte_flow_item *item,
170                      struct ch_filter_specification *fs,
171                      struct rte_flow_error *e)
172 {
173         const struct rte_flow_item_udp *val = item->spec;
174         const struct rte_flow_item_udp *umask = item->mask;
175         const struct rte_flow_item_udp *mask;
176
177         mask = umask ? umask : (const struct rte_flow_item_udp *)dmask;
178
179         if (mask->hdr.dgram_len || mask->hdr.dgram_cksum)
180                 return rte_flow_error_set(e, ENOTSUP, RTE_FLOW_ERROR_TYPE_ITEM,
181                                           item,
182                                           "udp: only src/dst port supported");
183
184         CXGBE_FILL_FS(IPPROTO_UDP, 0xff, proto);
185         if (!val)
186                 return 0;
187         CXGBE_FILL_FS(be16_to_cpu(val->hdr.src_port),
188                       be16_to_cpu(mask->hdr.src_port), fport);
189         CXGBE_FILL_FS(be16_to_cpu(val->hdr.dst_port),
190                       be16_to_cpu(mask->hdr.dst_port), lport);
191         return 0;
192 }
193
194 static int
195 ch_rte_parsetype_tcp(const void *dmask, const struct rte_flow_item *item,
196                      struct ch_filter_specification *fs,
197                      struct rte_flow_error *e)
198 {
199         const struct rte_flow_item_tcp *val = item->spec;
200         const struct rte_flow_item_tcp *umask = item->mask;
201         const struct rte_flow_item_tcp *mask;
202
203         mask = umask ? umask : (const struct rte_flow_item_tcp *)dmask;
204
205         if (mask->hdr.sent_seq || mask->hdr.recv_ack || mask->hdr.data_off ||
206             mask->hdr.tcp_flags || mask->hdr.rx_win || mask->hdr.cksum ||
207             mask->hdr.tcp_urp)
208                 return rte_flow_error_set(e, ENOTSUP, RTE_FLOW_ERROR_TYPE_ITEM,
209                                           item,
210                                           "tcp: only src/dst port supported");
211
212         CXGBE_FILL_FS(IPPROTO_TCP, 0xff, proto);
213         if (!val)
214                 return 0;
215         CXGBE_FILL_FS(be16_to_cpu(val->hdr.src_port),
216                       be16_to_cpu(mask->hdr.src_port), fport);
217         CXGBE_FILL_FS(be16_to_cpu(val->hdr.dst_port),
218                       be16_to_cpu(mask->hdr.dst_port), lport);
219         return 0;
220 }
221
222 static int
223 ch_rte_parsetype_ipv4(const void *dmask, const struct rte_flow_item *item,
224                       struct ch_filter_specification *fs,
225                       struct rte_flow_error *e)
226 {
227         const struct rte_flow_item_ipv4 *val = item->spec;
228         const struct rte_flow_item_ipv4 *umask = item->mask;
229         const struct rte_flow_item_ipv4 *mask;
230
231         mask = umask ? umask : (const struct rte_flow_item_ipv4 *)dmask;
232
233         if (mask->hdr.time_to_live || mask->hdr.type_of_service)
234                 return rte_flow_error_set(e, ENOTSUP, RTE_FLOW_ERROR_TYPE_ITEM,
235                                           item, "ttl/tos are not supported");
236
237         fs->type = FILTER_TYPE_IPV4;
238         CXGBE_FILL_FS(ETHER_TYPE_IPv4, 0xffff, ethtype);
239         if (!val)
240                 return 0; /* ipv4 wild card */
241
242         CXGBE_FILL_FS(val->hdr.next_proto_id, mask->hdr.next_proto_id, proto);
243         CXGBE_FILL_FS_MEMCPY(val->hdr.dst_addr, mask->hdr.dst_addr, lip);
244         CXGBE_FILL_FS_MEMCPY(val->hdr.src_addr, mask->hdr.src_addr, fip);
245
246         return 0;
247 }
248
249 static int
250 ch_rte_parsetype_ipv6(const void *dmask, const struct rte_flow_item *item,
251                       struct ch_filter_specification *fs,
252                       struct rte_flow_error *e)
253 {
254         const struct rte_flow_item_ipv6 *val = item->spec;
255         const struct rte_flow_item_ipv6 *umask = item->mask;
256         const struct rte_flow_item_ipv6 *mask;
257
258         mask = umask ? umask : (const struct rte_flow_item_ipv6 *)dmask;
259
260         if (mask->hdr.vtc_flow ||
261             mask->hdr.payload_len || mask->hdr.hop_limits)
262                 return rte_flow_error_set(e, ENOTSUP, RTE_FLOW_ERROR_TYPE_ITEM,
263                                           item,
264                                           "tc/flow/hop are not supported");
265
266         fs->type = FILTER_TYPE_IPV6;
267         CXGBE_FILL_FS(ETHER_TYPE_IPv6, 0xffff, ethtype);
268         if (!val)
269                 return 0; /* ipv6 wild card */
270
271         CXGBE_FILL_FS(val->hdr.proto, mask->hdr.proto, proto);
272         CXGBE_FILL_FS_MEMCPY(val->hdr.dst_addr, mask->hdr.dst_addr, lip);
273         CXGBE_FILL_FS_MEMCPY(val->hdr.src_addr, mask->hdr.src_addr, fip);
274
275         return 0;
276 }
277
278 static int
279 cxgbe_rtef_parse_attr(struct rte_flow *flow, const struct rte_flow_attr *attr,
280                       struct rte_flow_error *e)
281 {
282         if (attr->egress)
283                 return rte_flow_error_set(e, ENOTSUP, RTE_FLOW_ERROR_TYPE_ATTR,
284                                           attr, "attribute:<egress> is"
285                                           " not supported !");
286         if (attr->group > 0)
287                 return rte_flow_error_set(e, ENOTSUP, RTE_FLOW_ERROR_TYPE_ATTR,
288                                           attr, "group parameter is"
289                                           " not supported.");
290
291         flow->fidx = attr->priority ? attr->priority - 1 : FILTER_ID_MAX;
292
293         return 0;
294 }
295
296 static inline int check_rxq(struct rte_eth_dev *dev, uint16_t rxq)
297 {
298         struct port_info *pi = ethdev2pinfo(dev);
299
300         if (rxq > pi->n_rx_qsets)
301                 return -EINVAL;
302         return 0;
303 }
304
305 static int cxgbe_validate_fidxondel(struct filter_entry *f, unsigned int fidx)
306 {
307         struct adapter *adap = ethdev2adap(f->dev);
308         struct ch_filter_specification fs = f->fs;
309
310         if (fidx >= adap->tids.nftids) {
311                 dev_err(adap, "invalid flow index %d.\n", fidx);
312                 return -EINVAL;
313         }
314         if (!is_filter_set(&adap->tids, fidx, fs.type)) {
315                 dev_err(adap, "Already free fidx:%d f:%p\n", fidx, f);
316                 return -EINVAL;
317         }
318
319         return 0;
320 }
321
322 static int
323 cxgbe_validate_fidxonadd(struct ch_filter_specification *fs,
324                          struct adapter *adap, unsigned int fidx)
325 {
326         if (is_filter_set(&adap->tids, fidx, fs->type)) {
327                 dev_err(adap, "filter index: %d is busy.\n", fidx);
328                 return -EBUSY;
329         }
330         if (fidx >= adap->tids.nftids) {
331                 dev_err(adap, "filter index (%u) >= max(%u)\n",
332                         fidx, adap->tids.nftids);
333                 return -ERANGE;
334         }
335
336         return 0;
337 }
338
339 static int
340 cxgbe_verify_fidx(struct rte_flow *flow, unsigned int fidx, uint8_t del)
341 {
342         if (flow->fs.cap)
343                 return 0; /* Hash filters */
344         return del ? cxgbe_validate_fidxondel(flow->f, fidx) :
345                 cxgbe_validate_fidxonadd(&flow->fs,
346                                          ethdev2adap(flow->dev), fidx);
347 }
348
349 static int cxgbe_get_fidx(struct rte_flow *flow, unsigned int *fidx)
350 {
351         struct ch_filter_specification *fs = &flow->fs;
352         struct adapter *adap = ethdev2adap(flow->dev);
353
354         /* For tcam get the next available slot, if default value specified */
355         if (flow->fidx == FILTER_ID_MAX) {
356                 int idx;
357
358                 idx = cxgbe_alloc_ftid(adap, fs->type);
359                 if (idx < 0) {
360                         dev_err(adap, "unable to get a filter index in tcam\n");
361                         return -ENOMEM;
362                 }
363                 *fidx = (unsigned int)idx;
364         } else {
365                 *fidx = flow->fidx;
366         }
367
368         return 0;
369 }
370
371 static int
372 ch_rte_parse_atype_switch(const struct rte_flow_action *a,
373                           struct ch_filter_specification *fs,
374                           struct rte_flow_error *e)
375 {
376         const struct rte_flow_action_of_set_vlan_vid *vlanid;
377         const struct rte_flow_action_of_push_vlan *pushvlan;
378         const struct rte_flow_action_phy_port *port;
379
380         switch (a->type) {
381         case RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_VID:
382                 vlanid = (const struct rte_flow_action_of_set_vlan_vid *)
383                           a->conf;
384                 fs->newvlan = VLAN_REWRITE;
385                 fs->vlan = vlanid->vlan_vid;
386                 break;
387         case RTE_FLOW_ACTION_TYPE_OF_PUSH_VLAN:
388                 pushvlan = (const struct rte_flow_action_of_push_vlan *)
389                             a->conf;
390                 if (pushvlan->ethertype != ETHER_TYPE_VLAN)
391                         return rte_flow_error_set(e, EINVAL,
392                                                   RTE_FLOW_ERROR_TYPE_ACTION, a,
393                                                   "only ethertype 0x8100 "
394                                                   "supported for push vlan.");
395                 fs->newvlan = VLAN_INSERT;
396                 break;
397         case RTE_FLOW_ACTION_TYPE_OF_POP_VLAN:
398                 fs->newvlan = VLAN_REMOVE;
399                 break;
400         case RTE_FLOW_ACTION_TYPE_PHY_PORT:
401                 port = (const struct rte_flow_action_phy_port *)a->conf;
402                 fs->eport = port->index;
403                 break;
404         default:
405                 /* We are not supposed to come here */
406                 return rte_flow_error_set(e, EINVAL,
407                                           RTE_FLOW_ERROR_TYPE_ACTION, a,
408                                           "Action not supported");
409         }
410
411         return 0;
412 }
413
414 static int
415 cxgbe_rtef_parse_actions(struct rte_flow *flow,
416                          const struct rte_flow_action action[],
417                          struct rte_flow_error *e)
418 {
419         struct ch_filter_specification *fs = &flow->fs;
420         const struct rte_flow_action_queue *q;
421         const struct rte_flow_action *a;
422         char abit = 0;
423         int ret;
424
425         for (a = action; a->type != RTE_FLOW_ACTION_TYPE_END; a++) {
426                 switch (a->type) {
427                 case RTE_FLOW_ACTION_TYPE_VOID:
428                         continue;
429                 case RTE_FLOW_ACTION_TYPE_DROP:
430                         if (abit++)
431                                 return rte_flow_error_set(e, EINVAL,
432                                                 RTE_FLOW_ERROR_TYPE_ACTION, a,
433                                                 "specify only 1 pass/drop");
434                         fs->action = FILTER_DROP;
435                         break;
436                 case RTE_FLOW_ACTION_TYPE_QUEUE:
437                         q = (const struct rte_flow_action_queue *)a->conf;
438                         if (!q)
439                                 return rte_flow_error_set(e, EINVAL,
440                                                 RTE_FLOW_ERROR_TYPE_ACTION, q,
441                                                 "specify rx queue index");
442                         if (check_rxq(flow->dev, q->index))
443                                 return rte_flow_error_set(e, EINVAL,
444                                                 RTE_FLOW_ERROR_TYPE_ACTION, q,
445                                                 "Invalid rx queue");
446                         if (abit++)
447                                 return rte_flow_error_set(e, EINVAL,
448                                                 RTE_FLOW_ERROR_TYPE_ACTION, a,
449                                                 "specify only 1 pass/drop");
450                         fs->action = FILTER_PASS;
451                         fs->dirsteer = 1;
452                         fs->iq = q->index;
453                         break;
454                 case RTE_FLOW_ACTION_TYPE_COUNT:
455                         fs->hitcnts = 1;
456                         break;
457                 case RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_VID:
458                 case RTE_FLOW_ACTION_TYPE_OF_PUSH_VLAN:
459                 case RTE_FLOW_ACTION_TYPE_OF_POP_VLAN:
460                 case RTE_FLOW_ACTION_TYPE_PHY_PORT:
461                         /* We allow multiple switch actions, but switch is
462                          * not compatible with either queue or drop
463                          */
464                         if (abit++ && fs->action != FILTER_SWITCH)
465                                 return rte_flow_error_set(e, EINVAL,
466                                                 RTE_FLOW_ERROR_TYPE_ACTION, a,
467                                                 "overlapping action specified");
468                         ret = ch_rte_parse_atype_switch(a, fs, e);
469                         if (ret)
470                                 return ret;
471                         fs->action = FILTER_SWITCH;
472                         break;
473                 default:
474                         /* Not supported action : return error */
475                         return rte_flow_error_set(e, ENOTSUP,
476                                                   RTE_FLOW_ERROR_TYPE_ACTION,
477                                                   a, "Action not supported");
478                 }
479         }
480
481         return 0;
482 }
483
484 struct chrte_fparse parseitem[] = {
485         [RTE_FLOW_ITEM_TYPE_ETH] = {
486                 .fptr  = ch_rte_parsetype_eth,
487                 .dmask = &(const struct rte_flow_item_eth){
488                         .dst.addr_bytes = "\xff\xff\xff\xff\xff\xff",
489                         .src.addr_bytes = "\x00\x00\x00\x00\x00\x00",
490                         .type = 0xffff,
491                 }
492         },
493
494         [RTE_FLOW_ITEM_TYPE_PHY_PORT] = {
495                 .fptr = ch_rte_parsetype_port,
496                 .dmask = &(const struct rte_flow_item_phy_port){
497                         .index = 0x7,
498                 }
499         },
500
501         [RTE_FLOW_ITEM_TYPE_IPV4] = {
502                 .fptr  = ch_rte_parsetype_ipv4,
503                 .dmask = &rte_flow_item_ipv4_mask,
504         },
505
506         [RTE_FLOW_ITEM_TYPE_IPV6] = {
507                 .fptr  = ch_rte_parsetype_ipv6,
508                 .dmask = &rte_flow_item_ipv6_mask,
509         },
510
511         [RTE_FLOW_ITEM_TYPE_UDP] = {
512                 .fptr  = ch_rte_parsetype_udp,
513                 .dmask = &rte_flow_item_udp_mask,
514         },
515
516         [RTE_FLOW_ITEM_TYPE_TCP] = {
517                 .fptr  = ch_rte_parsetype_tcp,
518                 .dmask = &rte_flow_item_tcp_mask,
519         },
520 };
521
522 static int
523 cxgbe_rtef_parse_items(struct rte_flow *flow,
524                        const struct rte_flow_item items[],
525                        struct rte_flow_error *e)
526 {
527         struct adapter *adap = ethdev2adap(flow->dev);
528         const struct rte_flow_item *i;
529         char repeat[ARRAY_SIZE(parseitem)] = {0};
530
531         for (i = items; i->type != RTE_FLOW_ITEM_TYPE_END; i++) {
532                 struct chrte_fparse *idx;
533                 int ret;
534
535                 if (i->type >= ARRAY_SIZE(parseitem))
536                         return rte_flow_error_set(e, ENOTSUP,
537                                                   RTE_FLOW_ERROR_TYPE_ITEM,
538                                                   i, "Item not supported");
539
540                 switch (i->type) {
541                 case RTE_FLOW_ITEM_TYPE_VOID:
542                         continue;
543                 default:
544                         /* check if item is repeated */
545                         if (repeat[i->type])
546                                 return rte_flow_error_set(e, ENOTSUP,
547                                                 RTE_FLOW_ERROR_TYPE_ITEM, i,
548                                                 "parse items cannot be repeated (except void)");
549                         repeat[i->type] = 1;
550
551                         /* validate the item */
552                         ret = cxgbe_validate_item(i, e);
553                         if (ret)
554                                 return ret;
555
556                         idx = &flow->item_parser[i->type];
557                         if (!idx || !idx->fptr) {
558                                 return rte_flow_error_set(e, ENOTSUP,
559                                                 RTE_FLOW_ERROR_TYPE_ITEM, i,
560                                                 "Item not supported");
561                         } else {
562                                 ret = idx->fptr(idx->dmask, i, &flow->fs, e);
563                                 if (ret)
564                                         return ret;
565                         }
566                 }
567         }
568
569         cxgbe_fill_filter_region(adap, &flow->fs);
570
571         return 0;
572 }
573
574 static int
575 cxgbe_flow_parse(struct rte_flow *flow,
576                  const struct rte_flow_attr *attr,
577                  const struct rte_flow_item item[],
578                  const struct rte_flow_action action[],
579                  struct rte_flow_error *e)
580 {
581         int ret;
582         /* parse user request into ch_filter_specification */
583         ret = cxgbe_rtef_parse_attr(flow, attr, e);
584         if (ret)
585                 return ret;
586         ret = cxgbe_rtef_parse_items(flow, item, e);
587         if (ret)
588                 return ret;
589         return cxgbe_rtef_parse_actions(flow, action, e);
590 }
591
592 static int __cxgbe_flow_create(struct rte_eth_dev *dev, struct rte_flow *flow)
593 {
594         struct ch_filter_specification *fs = &flow->fs;
595         struct adapter *adap = ethdev2adap(dev);
596         struct tid_info *t = &adap->tids;
597         struct filter_ctx ctx;
598         unsigned int fidx;
599         int err;
600
601         if (cxgbe_get_fidx(flow, &fidx))
602                 return -ENOMEM;
603         if (cxgbe_verify_fidx(flow, fidx, 0))
604                 return -1;
605
606         t4_init_completion(&ctx.completion);
607         /* go create the filter */
608         err = cxgbe_set_filter(dev, fidx, fs, &ctx);
609         if (err) {
610                 dev_err(adap, "Error %d while creating filter.\n", err);
611                 return err;
612         }
613
614         /* Poll the FW for reply */
615         err = cxgbe_poll_for_completion(&adap->sge.fw_evtq,
616                                         CXGBE_FLOW_POLL_US,
617                                         CXGBE_FLOW_POLL_CNT,
618                                         &ctx.completion);
619         if (err) {
620                 dev_err(adap, "Filter set operation timed out (%d)\n", err);
621                 return err;
622         }
623         if (ctx.result) {
624                 dev_err(adap, "Hardware error %d while creating the filter.\n",
625                         ctx.result);
626                 return ctx.result;
627         }
628
629         if (fs->cap) { /* to destroy the filter */
630                 flow->fidx = ctx.tid;
631                 flow->f = lookup_tid(t, ctx.tid);
632         } else {
633                 flow->fidx = fidx;
634                 flow->f = &adap->tids.ftid_tab[fidx];
635         }
636
637         return 0;
638 }
639
640 static struct rte_flow *
641 cxgbe_flow_create(struct rte_eth_dev *dev,
642                   const struct rte_flow_attr *attr,
643                   const struct rte_flow_item item[],
644                   const struct rte_flow_action action[],
645                   struct rte_flow_error *e)
646 {
647         struct rte_flow *flow;
648         int ret;
649
650         flow = t4_os_alloc(sizeof(struct rte_flow));
651         if (!flow) {
652                 rte_flow_error_set(e, ENOMEM, RTE_FLOW_ERROR_TYPE_HANDLE,
653                                    NULL, "Unable to allocate memory for"
654                                    " filter_entry");
655                 return NULL;
656         }
657
658         flow->item_parser = parseitem;
659         flow->dev = dev;
660         flow->fs.private = (void *)flow;
661
662         if (cxgbe_flow_parse(flow, attr, item, action, e)) {
663                 t4_os_free(flow);
664                 return NULL;
665         }
666
667         /* go, interact with cxgbe_filter */
668         ret = __cxgbe_flow_create(dev, flow);
669         if (ret) {
670                 rte_flow_error_set(e, ret, RTE_FLOW_ERROR_TYPE_HANDLE,
671                                    NULL, "Unable to create flow rule");
672                 t4_os_free(flow);
673                 return NULL;
674         }
675
676         flow->f->private = flow; /* Will be used during flush */
677
678         return flow;
679 }
680
681 static int __cxgbe_flow_destroy(struct rte_eth_dev *dev, struct rte_flow *flow)
682 {
683         struct adapter *adap = ethdev2adap(dev);
684         struct filter_entry *f = flow->f;
685         struct ch_filter_specification *fs;
686         struct filter_ctx ctx;
687         int err;
688
689         fs = &f->fs;
690         if (cxgbe_verify_fidx(flow, flow->fidx, 1))
691                 return -1;
692
693         t4_init_completion(&ctx.completion);
694         err = cxgbe_del_filter(dev, flow->fidx, fs, &ctx);
695         if (err) {
696                 dev_err(adap, "Error %d while deleting filter.\n", err);
697                 return err;
698         }
699
700         /* Poll the FW for reply */
701         err = cxgbe_poll_for_completion(&adap->sge.fw_evtq,
702                                         CXGBE_FLOW_POLL_US,
703                                         CXGBE_FLOW_POLL_CNT,
704                                         &ctx.completion);
705         if (err) {
706                 dev_err(adap, "Filter delete operation timed out (%d)\n", err);
707                 return err;
708         }
709         if (ctx.result) {
710                 dev_err(adap, "Hardware error %d while deleting the filter.\n",
711                         ctx.result);
712                 return ctx.result;
713         }
714
715         fs = &flow->fs;
716         if (fs->mask.macidx) {
717                 struct port_info *pi = (struct port_info *)
718                                         (dev->data->dev_private);
719                 int ret;
720
721                 ret = cxgbe_mpstcam_remove(pi, fs->val.macidx);
722                 if (!ret)
723                         return ret;
724         }
725
726         return 0;
727 }
728
729 static int
730 cxgbe_flow_destroy(struct rte_eth_dev *dev, struct rte_flow *flow,
731                    struct rte_flow_error *e)
732 {
733         int ret;
734
735         ret = __cxgbe_flow_destroy(dev, flow);
736         if (ret)
737                 return rte_flow_error_set(e, ret, RTE_FLOW_ERROR_TYPE_HANDLE,
738                                           flow, "error destroying filter.");
739         t4_os_free(flow);
740         return 0;
741 }
742
743 static int __cxgbe_flow_query(struct rte_flow *flow, u64 *count,
744                               u64 *byte_count)
745 {
746         struct adapter *adap = ethdev2adap(flow->dev);
747         struct ch_filter_specification fs = flow->f->fs;
748         unsigned int fidx = flow->fidx;
749         int ret = 0;
750
751         ret = cxgbe_get_filter_count(adap, fidx, count, fs.cap, 0);
752         if (ret)
753                 return ret;
754         return cxgbe_get_filter_count(adap, fidx, byte_count, fs.cap, 1);
755 }
756
757 static int
758 cxgbe_flow_query(struct rte_eth_dev *dev, struct rte_flow *flow,
759                  const struct rte_flow_action *action, void *data,
760                  struct rte_flow_error *e)
761 {
762         struct ch_filter_specification fs;
763         struct rte_flow_query_count *c;
764         struct filter_entry *f;
765         int ret;
766
767         RTE_SET_USED(dev);
768
769         f = flow->f;
770         fs = f->fs;
771
772         if (action->type != RTE_FLOW_ACTION_TYPE_COUNT)
773                 return rte_flow_error_set(e, ENOTSUP,
774                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
775                                           "only count supported for query");
776
777         /*
778          * This is a valid operation, Since we are allowed to do chelsio
779          * specific operations in rte side of our code but not vise-versa
780          *
781          * So, fs can be queried/modified here BUT rte_flow_query_count
782          * cannot be worked on by the lower layer since we want to maintain
783          * it as rte_flow agnostic.
784          */
785         if (!fs.hitcnts)
786                 return rte_flow_error_set(e, EINVAL, RTE_FLOW_ERROR_TYPE_ACTION,
787                                           &fs, "filter hit counters were not"
788                                           " enabled during filter creation");
789
790         c = (struct rte_flow_query_count *)data;
791         ret = __cxgbe_flow_query(flow, &c->hits, &c->bytes);
792         if (ret)
793                 return rte_flow_error_set(e, -ret, RTE_FLOW_ERROR_TYPE_ACTION,
794                                           f, "cxgbe pmd failed to"
795                                           " perform query");
796
797         /* Query was successful */
798         c->bytes_set = 1;
799         c->hits_set = 1;
800
801         return 0; /* success / partial_success */
802 }
803
804 static int
805 cxgbe_flow_validate(struct rte_eth_dev *dev,
806                     const struct rte_flow_attr *attr,
807                     const struct rte_flow_item item[],
808                     const struct rte_flow_action action[],
809                     struct rte_flow_error *e)
810 {
811         struct adapter *adap = ethdev2adap(dev);
812         struct rte_flow *flow;
813         unsigned int fidx;
814         int ret;
815
816         flow = t4_os_alloc(sizeof(struct rte_flow));
817         if (!flow)
818                 return rte_flow_error_set(e, ENOMEM, RTE_FLOW_ERROR_TYPE_HANDLE,
819                                 NULL,
820                                 "Unable to allocate memory for filter_entry");
821
822         flow->item_parser = parseitem;
823         flow->dev = dev;
824
825         ret = cxgbe_flow_parse(flow, attr, item, action, e);
826         if (ret) {
827                 t4_os_free(flow);
828                 return ret;
829         }
830
831         if (validate_filter(adap, &flow->fs)) {
832                 t4_os_free(flow);
833                 return rte_flow_error_set(e, EINVAL, RTE_FLOW_ERROR_TYPE_HANDLE,
834                                 NULL,
835                                 "validation failed. Check f/w config file.");
836         }
837
838         if (cxgbe_get_fidx(flow, &fidx)) {
839                 t4_os_free(flow);
840                 return rte_flow_error_set(e, ENOMEM, RTE_FLOW_ERROR_TYPE_HANDLE,
841                                           NULL, "no memory in tcam.");
842         }
843
844         if (cxgbe_verify_fidx(flow, fidx, 0)) {
845                 t4_os_free(flow);
846                 return rte_flow_error_set(e, EINVAL, RTE_FLOW_ERROR_TYPE_HANDLE,
847                                           NULL, "validation failed");
848         }
849
850         t4_os_free(flow);
851         return 0;
852 }
853
854 /*
855  * @ret : > 0 filter destroyed succsesfully
856  *        < 0 error destroying filter
857  *        == 1 filter not active / not found
858  */
859 static int
860 cxgbe_check_n_destroy(struct filter_entry *f, struct rte_eth_dev *dev,
861                       struct rte_flow_error *e)
862 {
863         if (f && (f->valid || f->pending) &&
864             f->dev == dev && /* Only if user has asked for this port */
865              f->private) /* We (rte_flow) created this filter */
866                 return cxgbe_flow_destroy(dev, (struct rte_flow *)f->private,
867                                           e);
868         return 1;
869 }
870
871 static int cxgbe_flow_flush(struct rte_eth_dev *dev, struct rte_flow_error *e)
872 {
873         struct adapter *adap = ethdev2adap(dev);
874         unsigned int i;
875         int ret = 0;
876
877         if (adap->tids.ftid_tab) {
878                 struct filter_entry *f = &adap->tids.ftid_tab[0];
879
880                 for (i = 0; i < adap->tids.nftids; i++, f++) {
881                         ret = cxgbe_check_n_destroy(f, dev, e);
882                         if (ret < 0)
883                                 goto out;
884                 }
885         }
886
887         if (is_hashfilter(adap) && adap->tids.tid_tab) {
888                 struct filter_entry *f;
889
890                 for (i = adap->tids.hash_base; i <= adap->tids.ntids; i++) {
891                         f = (struct filter_entry *)adap->tids.tid_tab[i];
892
893                         ret = cxgbe_check_n_destroy(f, dev, e);
894                         if (ret < 0)
895                                 goto out;
896                 }
897         }
898
899 out:
900         return ret >= 0 ? 0 : ret;
901 }
902
903 static const struct rte_flow_ops cxgbe_flow_ops = {
904         .validate       = cxgbe_flow_validate,
905         .create         = cxgbe_flow_create,
906         .destroy        = cxgbe_flow_destroy,
907         .flush          = cxgbe_flow_flush,
908         .query          = cxgbe_flow_query,
909         .isolate        = NULL,
910 };
911
912 int
913 cxgbe_dev_filter_ctrl(struct rte_eth_dev *dev,
914                       enum rte_filter_type filter_type,
915                       enum rte_filter_op filter_op,
916                       void *arg)
917 {
918         int ret = 0;
919
920         RTE_SET_USED(dev);
921         switch (filter_type) {
922         case RTE_ETH_FILTER_GENERIC:
923                 if (filter_op != RTE_ETH_FILTER_GET)
924                         return -EINVAL;
925                 *(const void **)arg = &cxgbe_flow_ops;
926                 break;
927         default:
928                 ret = -ENOTSUP;
929                 break;
930         }
931         return ret;
932 }