net/cxgbe: fix slot allocation for IPv6 flows
[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 "base/common.h"
6 #include "cxgbe_flow.h"
7
8 #define __CXGBE_FILL_FS(__v, __m, fs, elem, e) \
9 do { \
10         if ((fs)->mask.elem && ((fs)->val.elem != (__v))) \
11                 return rte_flow_error_set(e, EINVAL, RTE_FLOW_ERROR_TYPE_ITEM, \
12                                           NULL, "Redefined match item with" \
13                                           " different values found"); \
14         (fs)->val.elem = (__v); \
15         (fs)->mask.elem = (__m); \
16 } while (0)
17
18 #define __CXGBE_FILL_FS_MEMCPY(__v, __m, fs, elem) \
19 do { \
20         memcpy(&(fs)->val.elem, &(__v), sizeof(__v)); \
21         memcpy(&(fs)->mask.elem, &(__m), sizeof(__m)); \
22 } while (0)
23
24 #define CXGBE_FILL_FS(v, m, elem) \
25         __CXGBE_FILL_FS(v, m, fs, elem, e)
26
27 #define CXGBE_FILL_FS_MEMCPY(v, m, elem) \
28         __CXGBE_FILL_FS_MEMCPY(v, m, fs, elem)
29
30 static int
31 cxgbe_validate_item(const struct rte_flow_item *i, struct rte_flow_error *e)
32 {
33         /* rte_flow specification does not allow it. */
34         if (!i->spec && (i->mask ||  i->last))
35                 return rte_flow_error_set(e, EINVAL, RTE_FLOW_ERROR_TYPE_ITEM,
36                                    i, "last or mask given without spec");
37         /*
38          * We don't support it.
39          * Although, we can support values in last as 0's or last == spec.
40          * But this will not provide user with any additional functionality
41          * and will only increase the complexity for us.
42          */
43         if (i->last)
44                 return rte_flow_error_set(e, ENOTSUP, RTE_FLOW_ERROR_TYPE_ITEM,
45                                    i, "last is not supported by chelsio pmd");
46         return 0;
47 }
48
49 static void
50 cxgbe_fill_filter_region(struct adapter *adap,
51                          struct ch_filter_specification *fs)
52 {
53         struct tp_params *tp = &adap->params.tp;
54         u64 hash_filter_mask = tp->hash_filter_mask;
55         u64 ntuple_mask = 0;
56
57         fs->cap = 0;
58
59         if (!is_hashfilter(adap))
60                 return;
61
62         if (fs->type) {
63                 uint8_t biton[16] = {0xff, 0xff, 0xff, 0xff,
64                                      0xff, 0xff, 0xff, 0xff,
65                                      0xff, 0xff, 0xff, 0xff,
66                                      0xff, 0xff, 0xff, 0xff};
67                 uint8_t bitoff[16] = {0};
68
69                 if (!memcmp(fs->val.lip, bitoff, sizeof(bitoff)) ||
70                     !memcmp(fs->val.fip, bitoff, sizeof(bitoff)) ||
71                     memcmp(fs->mask.lip, biton, sizeof(biton)) ||
72                     memcmp(fs->mask.fip, biton, sizeof(biton)))
73                         return;
74         } else {
75                 uint32_t biton  = 0xffffffff;
76                 uint32_t bitoff = 0x0U;
77
78                 if (!memcmp(fs->val.lip, &bitoff, sizeof(bitoff)) ||
79                     !memcmp(fs->val.fip, &bitoff, sizeof(bitoff)) ||
80                     memcmp(fs->mask.lip, &biton, sizeof(biton)) ||
81                     memcmp(fs->mask.fip, &biton, sizeof(biton)))
82                         return;
83         }
84
85         if (!fs->val.lport || fs->mask.lport != 0xffff)
86                 return;
87         if (!fs->val.fport || fs->mask.fport != 0xffff)
88                 return;
89
90         if (tp->protocol_shift >= 0)
91                 ntuple_mask |= (u64)fs->mask.proto << tp->protocol_shift;
92         if (tp->ethertype_shift >= 0)
93                 ntuple_mask |= (u64)fs->mask.ethtype << tp->ethertype_shift;
94         if (tp->port_shift >= 0)
95                 ntuple_mask |= (u64)fs->mask.iport << tp->port_shift;
96         if (tp->macmatch_shift >= 0)
97                 ntuple_mask |= (u64)fs->mask.macidx << tp->macmatch_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_eth(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_eth *spec = item->spec;
111         const struct rte_flow_item_eth *umask = item->mask;
112         const struct rte_flow_item_eth *mask;
113
114         /* If user has not given any mask, then use chelsio supported mask. */
115         mask = umask ? umask : (const struct rte_flow_item_eth *)dmask;
116
117         /* we don't support SRC_MAC filtering*/
118         if (!rte_is_zero_ether_addr(&mask->src))
119                 return rte_flow_error_set(e, ENOTSUP, RTE_FLOW_ERROR_TYPE_ITEM,
120                                           item,
121                                           "src mac filtering not supported");
122
123         if (!rte_is_zero_ether_addr(&mask->dst)) {
124                 const u8 *addr = (const u8 *)&spec->dst.addr_bytes[0];
125                 const u8 *m = (const u8 *)&mask->dst.addr_bytes[0];
126                 struct rte_flow *flow = (struct rte_flow *)fs->private;
127                 struct port_info *pi = (struct port_info *)
128                                         (flow->dev->data->dev_private);
129                 int idx;
130
131                 idx = cxgbe_mpstcam_alloc(pi, addr, m);
132                 if (idx <= 0)
133                         return rte_flow_error_set(e, idx,
134                                                   RTE_FLOW_ERROR_TYPE_ITEM,
135                                                   NULL, "unable to allocate mac"
136                                                   " entry in h/w");
137                 CXGBE_FILL_FS(idx, 0x1ff, macidx);
138         }
139
140         CXGBE_FILL_FS(be16_to_cpu(spec->type),
141                       be16_to_cpu(mask->type), ethtype);
142         return 0;
143 }
144
145 static int
146 ch_rte_parsetype_port(const void *dmask, const struct rte_flow_item *item,
147                       struct ch_filter_specification *fs,
148                       struct rte_flow_error *e)
149 {
150         const struct rte_flow_item_phy_port *val = item->spec;
151         const struct rte_flow_item_phy_port *umask = item->mask;
152         const struct rte_flow_item_phy_port *mask;
153
154         mask = umask ? umask : (const struct rte_flow_item_phy_port *)dmask;
155
156         if (val->index > 0x7)
157                 return rte_flow_error_set(e, EINVAL, RTE_FLOW_ERROR_TYPE_ITEM,
158                                           item,
159                                           "port index upto 0x7 is supported");
160
161         CXGBE_FILL_FS(val->index, mask->index, iport);
162
163         return 0;
164 }
165
166 static int
167 ch_rte_parsetype_udp(const void *dmask, const struct rte_flow_item *item,
168                      struct ch_filter_specification *fs,
169                      struct rte_flow_error *e)
170 {
171         const struct rte_flow_item_udp *val = item->spec;
172         const struct rte_flow_item_udp *umask = item->mask;
173         const struct rte_flow_item_udp *mask;
174
175         mask = umask ? umask : (const struct rte_flow_item_udp *)dmask;
176
177         if (mask->hdr.dgram_len || mask->hdr.dgram_cksum)
178                 return rte_flow_error_set(e, ENOTSUP, RTE_FLOW_ERROR_TYPE_ITEM,
179                                           item,
180                                           "udp: only src/dst port supported");
181
182         CXGBE_FILL_FS(IPPROTO_UDP, 0xff, proto);
183         if (!val)
184                 return 0;
185         CXGBE_FILL_FS(be16_to_cpu(val->hdr.src_port),
186                       be16_to_cpu(mask->hdr.src_port), fport);
187         CXGBE_FILL_FS(be16_to_cpu(val->hdr.dst_port),
188                       be16_to_cpu(mask->hdr.dst_port), lport);
189         return 0;
190 }
191
192 static int
193 ch_rte_parsetype_tcp(const void *dmask, const struct rte_flow_item *item,
194                      struct ch_filter_specification *fs,
195                      struct rte_flow_error *e)
196 {
197         const struct rte_flow_item_tcp *val = item->spec;
198         const struct rte_flow_item_tcp *umask = item->mask;
199         const struct rte_flow_item_tcp *mask;
200
201         mask = umask ? umask : (const struct rte_flow_item_tcp *)dmask;
202
203         if (mask->hdr.sent_seq || mask->hdr.recv_ack || mask->hdr.data_off ||
204             mask->hdr.tcp_flags || mask->hdr.rx_win || mask->hdr.cksum ||
205             mask->hdr.tcp_urp)
206                 return rte_flow_error_set(e, ENOTSUP, RTE_FLOW_ERROR_TYPE_ITEM,
207                                           item,
208                                           "tcp: only src/dst port supported");
209
210         CXGBE_FILL_FS(IPPROTO_TCP, 0xff, proto);
211         if (!val)
212                 return 0;
213         CXGBE_FILL_FS(be16_to_cpu(val->hdr.src_port),
214                       be16_to_cpu(mask->hdr.src_port), fport);
215         CXGBE_FILL_FS(be16_to_cpu(val->hdr.dst_port),
216                       be16_to_cpu(mask->hdr.dst_port), lport);
217         return 0;
218 }
219
220 static int
221 ch_rte_parsetype_ipv4(const void *dmask, const struct rte_flow_item *item,
222                       struct ch_filter_specification *fs,
223                       struct rte_flow_error *e)
224 {
225         const struct rte_flow_item_ipv4 *val = item->spec;
226         const struct rte_flow_item_ipv4 *umask = item->mask;
227         const struct rte_flow_item_ipv4 *mask;
228
229         mask = umask ? umask : (const struct rte_flow_item_ipv4 *)dmask;
230
231         if (mask->hdr.time_to_live || mask->hdr.type_of_service)
232                 return rte_flow_error_set(e, ENOTSUP, RTE_FLOW_ERROR_TYPE_ITEM,
233                                           item, "ttl/tos are not supported");
234
235         fs->type = FILTER_TYPE_IPV4;
236         CXGBE_FILL_FS(RTE_ETHER_TYPE_IPV4, 0xffff, ethtype);
237         if (!val)
238                 return 0; /* ipv4 wild card */
239
240         CXGBE_FILL_FS(val->hdr.next_proto_id, mask->hdr.next_proto_id, proto);
241         CXGBE_FILL_FS_MEMCPY(val->hdr.dst_addr, mask->hdr.dst_addr, lip);
242         CXGBE_FILL_FS_MEMCPY(val->hdr.src_addr, mask->hdr.src_addr, fip);
243
244         return 0;
245 }
246
247 static int
248 ch_rte_parsetype_ipv6(const void *dmask, const struct rte_flow_item *item,
249                       struct ch_filter_specification *fs,
250                       struct rte_flow_error *e)
251 {
252         const struct rte_flow_item_ipv6 *val = item->spec;
253         const struct rte_flow_item_ipv6 *umask = item->mask;
254         const struct rte_flow_item_ipv6 *mask;
255
256         mask = umask ? umask : (const struct rte_flow_item_ipv6 *)dmask;
257
258         if (mask->hdr.vtc_flow ||
259             mask->hdr.payload_len || mask->hdr.hop_limits)
260                 return rte_flow_error_set(e, ENOTSUP, RTE_FLOW_ERROR_TYPE_ITEM,
261                                           item,
262                                           "tc/flow/hop are not supported");
263
264         fs->type = FILTER_TYPE_IPV6;
265         CXGBE_FILL_FS(RTE_ETHER_TYPE_IPV6, 0xffff, ethtype);
266         if (!val)
267                 return 0; /* ipv6 wild card */
268
269         CXGBE_FILL_FS(val->hdr.proto, mask->hdr.proto, proto);
270         CXGBE_FILL_FS_MEMCPY(val->hdr.dst_addr, mask->hdr.dst_addr, lip);
271         CXGBE_FILL_FS_MEMCPY(val->hdr.src_addr, mask->hdr.src_addr, fip);
272
273         return 0;
274 }
275
276 static int
277 cxgbe_rtef_parse_attr(struct rte_flow *flow, const struct rte_flow_attr *attr,
278                       struct rte_flow_error *e)
279 {
280         if (attr->egress)
281                 return rte_flow_error_set(e, ENOTSUP, RTE_FLOW_ERROR_TYPE_ATTR,
282                                           attr, "attribute:<egress> is"
283                                           " not supported !");
284         if (attr->group > 0)
285                 return rte_flow_error_set(e, ENOTSUP, RTE_FLOW_ERROR_TYPE_ATTR,
286                                           attr, "group parameter is"
287                                           " not supported.");
288
289         flow->fidx = attr->priority ? attr->priority - 1 : FILTER_ID_MAX;
290
291         return 0;
292 }
293
294 static inline int check_rxq(struct rte_eth_dev *dev, uint16_t rxq)
295 {
296         struct port_info *pi = ethdev2pinfo(dev);
297
298         if (rxq > pi->n_rx_qsets)
299                 return -EINVAL;
300         return 0;
301 }
302
303 static int cxgbe_validate_fidxondel(struct filter_entry *f, unsigned int fidx)
304 {
305         struct adapter *adap = ethdev2adap(f->dev);
306         struct ch_filter_specification fs = f->fs;
307         u8 nentries;
308
309         if (fidx >= adap->tids.nftids) {
310                 dev_err(adap, "invalid flow index %d.\n", fidx);
311                 return -EINVAL;
312         }
313
314         nentries = cxgbe_filter_slots(adap, fs.type);
315         if (!cxgbe_is_filter_set(&adap->tids, fidx, nentries)) {
316                 dev_err(adap, "Already free fidx:%d f:%p\n", fidx, f);
317                 return -EINVAL;
318         }
319
320         return 0;
321 }
322
323 static int
324 cxgbe_validate_fidxonadd(struct ch_filter_specification *fs,
325                          struct adapter *adap, unsigned int fidx)
326 {
327         u8 nentries;
328
329         nentries = cxgbe_filter_slots(adap, fs->type);
330         if (cxgbe_is_filter_set(&adap->tids, fidx, nentries)) {
331                 dev_err(adap, "filter index: %d is busy.\n", fidx);
332                 return -EBUSY;
333         }
334
335         if (fidx >= adap->tids.nftids) {
336                 dev_err(adap, "filter index (%u) >= max(%u)\n",
337                         fidx, adap->tids.nftids);
338                 return -ERANGE;
339         }
340
341         return 0;
342 }
343
344 static int
345 cxgbe_verify_fidx(struct rte_flow *flow, unsigned int fidx, uint8_t del)
346 {
347         if (flow->fs.cap)
348                 return 0; /* Hash filters */
349         return del ? cxgbe_validate_fidxondel(flow->f, fidx) :
350                 cxgbe_validate_fidxonadd(&flow->fs,
351                                          ethdev2adap(flow->dev), fidx);
352 }
353
354 static int cxgbe_get_fidx(struct rte_flow *flow, unsigned int *fidx)
355 {
356         struct ch_filter_specification *fs = &flow->fs;
357         struct adapter *adap = ethdev2adap(flow->dev);
358
359         /* For tcam get the next available slot, if default value specified */
360         if (flow->fidx == FILTER_ID_MAX) {
361                 u8 nentries;
362                 int idx;
363
364                 nentries = cxgbe_filter_slots(adap, fs->type);
365                 idx = cxgbe_alloc_ftid(adap, nentries);
366                 if (idx < 0) {
367                         dev_err(adap, "unable to get a filter index in tcam\n");
368                         return -ENOMEM;
369                 }
370                 *fidx = (unsigned int)idx;
371         } else {
372                 *fidx = flow->fidx;
373         }
374
375         return 0;
376 }
377
378 static int
379 cxgbe_get_flow_item_index(const struct rte_flow_item items[], u32 type)
380 {
381         const struct rte_flow_item *i;
382         int j, index = -ENOENT;
383
384         for (i = items, j = 0; i->type != RTE_FLOW_ITEM_TYPE_END; i++, j++) {
385                 if (i->type == type) {
386                         index = j;
387                         break;
388                 }
389         }
390
391         return index;
392 }
393
394 static int
395 ch_rte_parse_nat(uint8_t nmode, struct ch_filter_specification *fs)
396 {
397         /* nmode:
398          * BIT_0 = [src_ip],   BIT_1 = [dst_ip]
399          * BIT_2 = [src_port], BIT_3 = [dst_port]
400          *
401          * Only below cases are supported as per our spec.
402          */
403         switch (nmode) {
404         case 0:  /* 0000b */
405                 fs->nat_mode = NAT_MODE_NONE;
406                 break;
407         case 2:  /* 0010b */
408                 fs->nat_mode = NAT_MODE_DIP;
409                 break;
410         case 5:  /* 0101b */
411                 fs->nat_mode = NAT_MODE_SIP_SP;
412                 break;
413         case 7:  /* 0111b */
414                 fs->nat_mode = NAT_MODE_DIP_SIP_SP;
415                 break;
416         case 10: /* 1010b */
417                 fs->nat_mode = NAT_MODE_DIP_DP;
418                 break;
419         case 11: /* 1011b */
420                 fs->nat_mode = NAT_MODE_DIP_DP_SIP;
421                 break;
422         case 14: /* 1110b */
423                 fs->nat_mode = NAT_MODE_DIP_DP_SP;
424                 break;
425         case 15: /* 1111b */
426                 fs->nat_mode = NAT_MODE_ALL;
427                 break;
428         default:
429                 return -EINVAL;
430         }
431
432         return 0;
433 }
434
435 static int
436 ch_rte_parse_atype_switch(const struct rte_flow_action *a,
437                           const struct rte_flow_item items[],
438                           uint8_t *nmode,
439                           struct ch_filter_specification *fs,
440                           struct rte_flow_error *e)
441 {
442         const struct rte_flow_action_of_set_vlan_vid *vlanid;
443         const struct rte_flow_action_of_push_vlan *pushvlan;
444         const struct rte_flow_action_set_ipv4 *ipv4;
445         const struct rte_flow_action_set_ipv6 *ipv6;
446         const struct rte_flow_action_set_tp *tp_port;
447         const struct rte_flow_action_phy_port *port;
448         int item_index;
449
450         switch (a->type) {
451         case RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_VID:
452                 vlanid = (const struct rte_flow_action_of_set_vlan_vid *)
453                           a->conf;
454                 fs->newvlan = VLAN_REWRITE;
455                 fs->vlan = vlanid->vlan_vid;
456                 break;
457         case RTE_FLOW_ACTION_TYPE_OF_PUSH_VLAN:
458                 pushvlan = (const struct rte_flow_action_of_push_vlan *)
459                             a->conf;
460                 if (pushvlan->ethertype != RTE_ETHER_TYPE_VLAN)
461                         return rte_flow_error_set(e, EINVAL,
462                                                   RTE_FLOW_ERROR_TYPE_ACTION, a,
463                                                   "only ethertype 0x8100 "
464                                                   "supported for push vlan.");
465                 fs->newvlan = VLAN_INSERT;
466                 break;
467         case RTE_FLOW_ACTION_TYPE_OF_POP_VLAN:
468                 fs->newvlan = VLAN_REMOVE;
469                 break;
470         case RTE_FLOW_ACTION_TYPE_PHY_PORT:
471                 port = (const struct rte_flow_action_phy_port *)a->conf;
472                 fs->eport = port->index;
473                 break;
474         case RTE_FLOW_ACTION_TYPE_SET_IPV4_SRC:
475                 item_index = cxgbe_get_flow_item_index(items,
476                                                        RTE_FLOW_ITEM_TYPE_IPV4);
477                 if (item_index < 0)
478                         return rte_flow_error_set(e, EINVAL,
479                                                   RTE_FLOW_ERROR_TYPE_ACTION, a,
480                                                   "No RTE_FLOW_ITEM_TYPE_IPV4 "
481                                                   "found.");
482
483                 ipv4 = (const struct rte_flow_action_set_ipv4 *)a->conf;
484                 memcpy(fs->nat_fip, &ipv4->ipv4_addr, sizeof(ipv4->ipv4_addr));
485                 *nmode |= 1 << 0;
486                 break;
487         case RTE_FLOW_ACTION_TYPE_SET_IPV4_DST:
488                 item_index = cxgbe_get_flow_item_index(items,
489                                                        RTE_FLOW_ITEM_TYPE_IPV4);
490                 if (item_index < 0)
491                         return rte_flow_error_set(e, EINVAL,
492                                                   RTE_FLOW_ERROR_TYPE_ACTION, a,
493                                                   "No RTE_FLOW_ITEM_TYPE_IPV4 "
494                                                   "found.");
495
496                 ipv4 = (const struct rte_flow_action_set_ipv4 *)a->conf;
497                 memcpy(fs->nat_lip, &ipv4->ipv4_addr, sizeof(ipv4->ipv4_addr));
498                 *nmode |= 1 << 1;
499                 break;
500         case RTE_FLOW_ACTION_TYPE_SET_IPV6_SRC:
501                 item_index = cxgbe_get_flow_item_index(items,
502                                                        RTE_FLOW_ITEM_TYPE_IPV6);
503                 if (item_index < 0)
504                         return rte_flow_error_set(e, EINVAL,
505                                                   RTE_FLOW_ERROR_TYPE_ACTION, a,
506                                                   "No RTE_FLOW_ITEM_TYPE_IPV6 "
507                                                   "found.");
508
509                 ipv6 = (const struct rte_flow_action_set_ipv6 *)a->conf;
510                 memcpy(fs->nat_fip, ipv6->ipv6_addr, sizeof(ipv6->ipv6_addr));
511                 *nmode |= 1 << 0;
512                 break;
513         case RTE_FLOW_ACTION_TYPE_SET_IPV6_DST:
514                 item_index = cxgbe_get_flow_item_index(items,
515                                                        RTE_FLOW_ITEM_TYPE_IPV6);
516                 if (item_index < 0)
517                         return rte_flow_error_set(e, EINVAL,
518                                                   RTE_FLOW_ERROR_TYPE_ACTION, a,
519                                                   "No RTE_FLOW_ITEM_TYPE_IPV6 "
520                                                   "found.");
521
522                 ipv6 = (const struct rte_flow_action_set_ipv6 *)a->conf;
523                 memcpy(fs->nat_lip, ipv6->ipv6_addr, sizeof(ipv6->ipv6_addr));
524                 *nmode |= 1 << 1;
525                 break;
526         case RTE_FLOW_ACTION_TYPE_SET_TP_SRC:
527                 item_index = cxgbe_get_flow_item_index(items,
528                                                        RTE_FLOW_ITEM_TYPE_TCP);
529                 if (item_index < 0) {
530                         item_index =
531                                 cxgbe_get_flow_item_index(items,
532                                                 RTE_FLOW_ITEM_TYPE_UDP);
533                         if (item_index < 0)
534                                 return rte_flow_error_set(e, EINVAL,
535                                                 RTE_FLOW_ERROR_TYPE_ACTION, a,
536                                                 "No RTE_FLOW_ITEM_TYPE_TCP or "
537                                                 "RTE_FLOW_ITEM_TYPE_UDP found");
538                 }
539
540                 tp_port = (const struct rte_flow_action_set_tp *)a->conf;
541                 fs->nat_fport = be16_to_cpu(tp_port->port);
542                 *nmode |= 1 << 2;
543                 break;
544         case RTE_FLOW_ACTION_TYPE_SET_TP_DST:
545                 item_index = cxgbe_get_flow_item_index(items,
546                                                        RTE_FLOW_ITEM_TYPE_TCP);
547                 if (item_index < 0) {
548                         item_index =
549                                 cxgbe_get_flow_item_index(items,
550                                                 RTE_FLOW_ITEM_TYPE_UDP);
551                         if (item_index < 0)
552                                 return rte_flow_error_set(e, EINVAL,
553                                                 RTE_FLOW_ERROR_TYPE_ACTION, a,
554                                                 "No RTE_FLOW_ITEM_TYPE_TCP or "
555                                                 "RTE_FLOW_ITEM_TYPE_UDP found");
556                 }
557
558                 tp_port = (const struct rte_flow_action_set_tp *)a->conf;
559                 fs->nat_lport = be16_to_cpu(tp_port->port);
560                 *nmode |= 1 << 3;
561                 break;
562         case RTE_FLOW_ACTION_TYPE_MAC_SWAP:
563                 item_index = cxgbe_get_flow_item_index(items,
564                                                        RTE_FLOW_ITEM_TYPE_ETH);
565                 if (item_index < 0)
566                         return rte_flow_error_set(e, EINVAL,
567                                                   RTE_FLOW_ERROR_TYPE_ACTION, a,
568                                                   "No RTE_FLOW_ITEM_TYPE_ETH "
569                                                   "found");
570                 fs->swapmac = 1;
571                 break;
572         default:
573                 /* We are not supposed to come here */
574                 return rte_flow_error_set(e, EINVAL,
575                                           RTE_FLOW_ERROR_TYPE_ACTION, a,
576                                           "Action not supported");
577         }
578
579         return 0;
580 }
581
582 static int
583 cxgbe_rtef_parse_actions(struct rte_flow *flow,
584                          const struct rte_flow_item items[],
585                          const struct rte_flow_action action[],
586                          struct rte_flow_error *e)
587 {
588         struct ch_filter_specification *fs = &flow->fs;
589         uint8_t nmode = 0, nat_ipv4 = 0, nat_ipv6 = 0;
590         const struct rte_flow_action_queue *q;
591         const struct rte_flow_action *a;
592         char abit = 0;
593         int ret;
594
595         for (a = action; a->type != RTE_FLOW_ACTION_TYPE_END; a++) {
596                 switch (a->type) {
597                 case RTE_FLOW_ACTION_TYPE_VOID:
598                         continue;
599                 case RTE_FLOW_ACTION_TYPE_DROP:
600                         if (abit++)
601                                 return rte_flow_error_set(e, EINVAL,
602                                                 RTE_FLOW_ERROR_TYPE_ACTION, a,
603                                                 "specify only 1 pass/drop");
604                         fs->action = FILTER_DROP;
605                         break;
606                 case RTE_FLOW_ACTION_TYPE_QUEUE:
607                         q = (const struct rte_flow_action_queue *)a->conf;
608                         if (!q)
609                                 return rte_flow_error_set(e, EINVAL,
610                                                 RTE_FLOW_ERROR_TYPE_ACTION, q,
611                                                 "specify rx queue index");
612                         if (check_rxq(flow->dev, q->index))
613                                 return rte_flow_error_set(e, EINVAL,
614                                                 RTE_FLOW_ERROR_TYPE_ACTION, q,
615                                                 "Invalid rx queue");
616                         if (abit++)
617                                 return rte_flow_error_set(e, EINVAL,
618                                                 RTE_FLOW_ERROR_TYPE_ACTION, a,
619                                                 "specify only 1 pass/drop");
620                         fs->action = FILTER_PASS;
621                         fs->dirsteer = 1;
622                         fs->iq = q->index;
623                         break;
624                 case RTE_FLOW_ACTION_TYPE_COUNT:
625                         fs->hitcnts = 1;
626                         break;
627                 case RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_VID:
628                 case RTE_FLOW_ACTION_TYPE_OF_PUSH_VLAN:
629                 case RTE_FLOW_ACTION_TYPE_OF_POP_VLAN:
630                 case RTE_FLOW_ACTION_TYPE_PHY_PORT:
631                 case RTE_FLOW_ACTION_TYPE_MAC_SWAP:
632                 case RTE_FLOW_ACTION_TYPE_SET_IPV4_SRC:
633                 case RTE_FLOW_ACTION_TYPE_SET_IPV4_DST:
634                         nat_ipv4++;
635                         goto action_switch;
636                 case RTE_FLOW_ACTION_TYPE_SET_IPV6_SRC:
637                 case RTE_FLOW_ACTION_TYPE_SET_IPV6_DST:
638                         nat_ipv6++;
639                         goto action_switch;
640                 case RTE_FLOW_ACTION_TYPE_SET_TP_SRC:
641                 case RTE_FLOW_ACTION_TYPE_SET_TP_DST:
642 action_switch:
643                         /* We allow multiple switch actions, but switch is
644                          * not compatible with either queue or drop
645                          */
646                         if (abit++ && fs->action != FILTER_SWITCH)
647                                 return rte_flow_error_set(e, EINVAL,
648                                                 RTE_FLOW_ERROR_TYPE_ACTION, a,
649                                                 "overlapping action specified");
650                         if (nat_ipv4 && nat_ipv6)
651                                 return rte_flow_error_set(e, EINVAL,
652                                         RTE_FLOW_ERROR_TYPE_ACTION, a,
653                                         "Can't have one address ipv4 and the"
654                                         " other ipv6");
655
656                         ret = ch_rte_parse_atype_switch(a, items, &nmode, fs,
657                                                         e);
658                         if (ret)
659                                 return ret;
660                         fs->action = FILTER_SWITCH;
661                         break;
662                 default:
663                         /* Not supported action : return error */
664                         return rte_flow_error_set(e, ENOTSUP,
665                                                   RTE_FLOW_ERROR_TYPE_ACTION,
666                                                   a, "Action not supported");
667                 }
668         }
669
670         if (ch_rte_parse_nat(nmode, fs))
671                 return rte_flow_error_set(e, EINVAL,
672                                           RTE_FLOW_ERROR_TYPE_ACTION, a,
673                                           "invalid settings for swich action");
674         return 0;
675 }
676
677 static struct chrte_fparse parseitem[] = {
678         [RTE_FLOW_ITEM_TYPE_ETH] = {
679                 .fptr  = ch_rte_parsetype_eth,
680                 .dmask = &(const struct rte_flow_item_eth){
681                         .dst.addr_bytes = "\xff\xff\xff\xff\xff\xff",
682                         .src.addr_bytes = "\x00\x00\x00\x00\x00\x00",
683                         .type = 0xffff,
684                 }
685         },
686
687         [RTE_FLOW_ITEM_TYPE_PHY_PORT] = {
688                 .fptr = ch_rte_parsetype_port,
689                 .dmask = &(const struct rte_flow_item_phy_port){
690                         .index = 0x7,
691                 }
692         },
693
694         [RTE_FLOW_ITEM_TYPE_IPV4] = {
695                 .fptr  = ch_rte_parsetype_ipv4,
696                 .dmask = &rte_flow_item_ipv4_mask,
697         },
698
699         [RTE_FLOW_ITEM_TYPE_IPV6] = {
700                 .fptr  = ch_rte_parsetype_ipv6,
701                 .dmask = &rte_flow_item_ipv6_mask,
702         },
703
704         [RTE_FLOW_ITEM_TYPE_UDP] = {
705                 .fptr  = ch_rte_parsetype_udp,
706                 .dmask = &rte_flow_item_udp_mask,
707         },
708
709         [RTE_FLOW_ITEM_TYPE_TCP] = {
710                 .fptr  = ch_rte_parsetype_tcp,
711                 .dmask = &rte_flow_item_tcp_mask,
712         },
713 };
714
715 static int
716 cxgbe_rtef_parse_items(struct rte_flow *flow,
717                        const struct rte_flow_item items[],
718                        struct rte_flow_error *e)
719 {
720         struct adapter *adap = ethdev2adap(flow->dev);
721         const struct rte_flow_item *i;
722         char repeat[ARRAY_SIZE(parseitem)] = {0};
723
724         for (i = items; i->type != RTE_FLOW_ITEM_TYPE_END; i++) {
725                 struct chrte_fparse *idx;
726                 int ret;
727
728                 if (i->type >= ARRAY_SIZE(parseitem))
729                         return rte_flow_error_set(e, ENOTSUP,
730                                                   RTE_FLOW_ERROR_TYPE_ITEM,
731                                                   i, "Item not supported");
732
733                 switch (i->type) {
734                 case RTE_FLOW_ITEM_TYPE_VOID:
735                         continue;
736                 default:
737                         /* check if item is repeated */
738                         if (repeat[i->type])
739                                 return rte_flow_error_set(e, ENOTSUP,
740                                                 RTE_FLOW_ERROR_TYPE_ITEM, i,
741                                                 "parse items cannot be repeated (except void)");
742                         repeat[i->type] = 1;
743
744                         /* No spec found for this pattern item. Skip it */
745                         if (!i->spec)
746                                 break;
747
748                         /* validate the item */
749                         ret = cxgbe_validate_item(i, e);
750                         if (ret)
751                                 return ret;
752
753                         idx = &flow->item_parser[i->type];
754                         if (!idx || !idx->fptr) {
755                                 return rte_flow_error_set(e, ENOTSUP,
756                                                 RTE_FLOW_ERROR_TYPE_ITEM, i,
757                                                 "Item not supported");
758                         } else {
759                                 ret = idx->fptr(idx->dmask, i, &flow->fs, e);
760                                 if (ret)
761                                         return ret;
762                         }
763                 }
764         }
765
766         cxgbe_fill_filter_region(adap, &flow->fs);
767
768         return 0;
769 }
770
771 static int
772 cxgbe_flow_parse(struct rte_flow *flow,
773                  const struct rte_flow_attr *attr,
774                  const struct rte_flow_item item[],
775                  const struct rte_flow_action action[],
776                  struct rte_flow_error *e)
777 {
778         int ret;
779         /* parse user request into ch_filter_specification */
780         ret = cxgbe_rtef_parse_attr(flow, attr, e);
781         if (ret)
782                 return ret;
783         ret = cxgbe_rtef_parse_items(flow, item, e);
784         if (ret)
785                 return ret;
786         return cxgbe_rtef_parse_actions(flow, item, action, e);
787 }
788
789 static int __cxgbe_flow_create(struct rte_eth_dev *dev, struct rte_flow *flow)
790 {
791         struct ch_filter_specification *fs = &flow->fs;
792         struct adapter *adap = ethdev2adap(dev);
793         struct tid_info *t = &adap->tids;
794         struct filter_ctx ctx;
795         unsigned int fidx;
796         int err;
797
798         if (cxgbe_get_fidx(flow, &fidx))
799                 return -ENOMEM;
800         if (cxgbe_verify_fidx(flow, fidx, 0))
801                 return -1;
802
803         t4_init_completion(&ctx.completion);
804         /* go create the filter */
805         err = cxgbe_set_filter(dev, fidx, fs, &ctx);
806         if (err) {
807                 dev_err(adap, "Error %d while creating filter.\n", err);
808                 return err;
809         }
810
811         /* Poll the FW for reply */
812         err = cxgbe_poll_for_completion(&adap->sge.fw_evtq,
813                                         CXGBE_FLOW_POLL_MS,
814                                         CXGBE_FLOW_POLL_CNT,
815                                         &ctx.completion);
816         if (err) {
817                 dev_err(adap, "Filter set operation timed out (%d)\n", err);
818                 return err;
819         }
820         if (ctx.result) {
821                 dev_err(adap, "Hardware error %d while creating the filter.\n",
822                         ctx.result);
823                 return ctx.result;
824         }
825
826         if (fs->cap) { /* to destroy the filter */
827                 flow->fidx = ctx.tid;
828                 flow->f = lookup_tid(t, ctx.tid);
829         } else {
830                 flow->fidx = fidx;
831                 flow->f = &adap->tids.ftid_tab[fidx];
832         }
833
834         return 0;
835 }
836
837 static struct rte_flow *
838 cxgbe_flow_create(struct rte_eth_dev *dev,
839                   const struct rte_flow_attr *attr,
840                   const struct rte_flow_item item[],
841                   const struct rte_flow_action action[],
842                   struct rte_flow_error *e)
843 {
844         struct rte_flow *flow;
845         int ret;
846
847         flow = t4_os_alloc(sizeof(struct rte_flow));
848         if (!flow) {
849                 rte_flow_error_set(e, ENOMEM, RTE_FLOW_ERROR_TYPE_HANDLE,
850                                    NULL, "Unable to allocate memory for"
851                                    " filter_entry");
852                 return NULL;
853         }
854
855         flow->item_parser = parseitem;
856         flow->dev = dev;
857         flow->fs.private = (void *)flow;
858
859         if (cxgbe_flow_parse(flow, attr, item, action, e)) {
860                 t4_os_free(flow);
861                 return NULL;
862         }
863
864         /* go, interact with cxgbe_filter */
865         ret = __cxgbe_flow_create(dev, flow);
866         if (ret) {
867                 rte_flow_error_set(e, ret, RTE_FLOW_ERROR_TYPE_HANDLE,
868                                    NULL, "Unable to create flow rule");
869                 t4_os_free(flow);
870                 return NULL;
871         }
872
873         flow->f->private = flow; /* Will be used during flush */
874
875         return flow;
876 }
877
878 static int __cxgbe_flow_destroy(struct rte_eth_dev *dev, struct rte_flow *flow)
879 {
880         struct adapter *adap = ethdev2adap(dev);
881         struct filter_entry *f = flow->f;
882         struct ch_filter_specification *fs;
883         struct filter_ctx ctx;
884         int err;
885
886         fs = &f->fs;
887         if (cxgbe_verify_fidx(flow, flow->fidx, 1))
888                 return -1;
889
890         t4_init_completion(&ctx.completion);
891         err = cxgbe_del_filter(dev, flow->fidx, fs, &ctx);
892         if (err) {
893                 dev_err(adap, "Error %d while deleting filter.\n", err);
894                 return err;
895         }
896
897         /* Poll the FW for reply */
898         err = cxgbe_poll_for_completion(&adap->sge.fw_evtq,
899                                         CXGBE_FLOW_POLL_MS,
900                                         CXGBE_FLOW_POLL_CNT,
901                                         &ctx.completion);
902         if (err) {
903                 dev_err(adap, "Filter delete operation timed out (%d)\n", err);
904                 return err;
905         }
906         if (ctx.result) {
907                 dev_err(adap, "Hardware error %d while deleting the filter.\n",
908                         ctx.result);
909                 return ctx.result;
910         }
911
912         fs = &flow->fs;
913         if (fs->mask.macidx) {
914                 struct port_info *pi = (struct port_info *)
915                                         (dev->data->dev_private);
916                 int ret;
917
918                 ret = cxgbe_mpstcam_remove(pi, fs->val.macidx);
919                 if (!ret)
920                         return ret;
921         }
922
923         return 0;
924 }
925
926 static int
927 cxgbe_flow_destroy(struct rte_eth_dev *dev, struct rte_flow *flow,
928                    struct rte_flow_error *e)
929 {
930         int ret;
931
932         ret = __cxgbe_flow_destroy(dev, flow);
933         if (ret)
934                 return rte_flow_error_set(e, ret, RTE_FLOW_ERROR_TYPE_HANDLE,
935                                           flow, "error destroying filter.");
936         t4_os_free(flow);
937         return 0;
938 }
939
940 static int __cxgbe_flow_query(struct rte_flow *flow, u64 *count,
941                               u64 *byte_count)
942 {
943         struct adapter *adap = ethdev2adap(flow->dev);
944         struct ch_filter_specification fs = flow->f->fs;
945         unsigned int fidx = flow->fidx;
946         int ret = 0;
947
948         ret = cxgbe_get_filter_count(adap, fidx, count, fs.cap, 0);
949         if (ret)
950                 return ret;
951         return cxgbe_get_filter_count(adap, fidx, byte_count, fs.cap, 1);
952 }
953
954 static int
955 cxgbe_flow_query(struct rte_eth_dev *dev, struct rte_flow *flow,
956                  const struct rte_flow_action *action, void *data,
957                  struct rte_flow_error *e)
958 {
959         struct adapter *adap = ethdev2adap(flow->dev);
960         struct ch_filter_specification fs;
961         struct rte_flow_query_count *c;
962         struct filter_entry *f;
963         int ret;
964
965         RTE_SET_USED(dev);
966
967         f = flow->f;
968         fs = f->fs;
969
970         if (action->type != RTE_FLOW_ACTION_TYPE_COUNT)
971                 return rte_flow_error_set(e, ENOTSUP,
972                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
973                                           "only count supported for query");
974
975         /*
976          * This is a valid operation, Since we are allowed to do chelsio
977          * specific operations in rte side of our code but not vise-versa
978          *
979          * So, fs can be queried/modified here BUT rte_flow_query_count
980          * cannot be worked on by the lower layer since we want to maintain
981          * it as rte_flow agnostic.
982          */
983         if (!fs.hitcnts)
984                 return rte_flow_error_set(e, EINVAL, RTE_FLOW_ERROR_TYPE_ACTION,
985                                           &fs, "filter hit counters were not"
986                                           " enabled during filter creation");
987
988         c = (struct rte_flow_query_count *)data;
989         ret = __cxgbe_flow_query(flow, &c->hits, &c->bytes);
990         if (ret)
991                 return rte_flow_error_set(e, -ret, RTE_FLOW_ERROR_TYPE_ACTION,
992                                           f, "cxgbe pmd failed to"
993                                           " perform query");
994
995         /* Query was successful */
996         c->bytes_set = 1;
997         c->hits_set = 1;
998         if (c->reset)
999                 cxgbe_clear_filter_count(adap, flow->fidx, f->fs.cap, true);
1000
1001         return 0; /* success / partial_success */
1002 }
1003
1004 static int
1005 cxgbe_flow_validate(struct rte_eth_dev *dev,
1006                     const struct rte_flow_attr *attr,
1007                     const struct rte_flow_item item[],
1008                     const struct rte_flow_action action[],
1009                     struct rte_flow_error *e)
1010 {
1011         struct adapter *adap = ethdev2adap(dev);
1012         struct rte_flow *flow;
1013         unsigned int fidx;
1014         int ret;
1015
1016         flow = t4_os_alloc(sizeof(struct rte_flow));
1017         if (!flow)
1018                 return rte_flow_error_set(e, ENOMEM, RTE_FLOW_ERROR_TYPE_HANDLE,
1019                                 NULL,
1020                                 "Unable to allocate memory for filter_entry");
1021
1022         flow->item_parser = parseitem;
1023         flow->dev = dev;
1024
1025         ret = cxgbe_flow_parse(flow, attr, item, action, e);
1026         if (ret) {
1027                 t4_os_free(flow);
1028                 return ret;
1029         }
1030
1031         if (cxgbe_validate_filter(adap, &flow->fs)) {
1032                 t4_os_free(flow);
1033                 return rte_flow_error_set(e, EINVAL, RTE_FLOW_ERROR_TYPE_HANDLE,
1034                                 NULL,
1035                                 "validation failed. Check f/w config file.");
1036         }
1037
1038         if (cxgbe_get_fidx(flow, &fidx)) {
1039                 t4_os_free(flow);
1040                 return rte_flow_error_set(e, ENOMEM, RTE_FLOW_ERROR_TYPE_HANDLE,
1041                                           NULL, "no memory in tcam.");
1042         }
1043
1044         if (cxgbe_verify_fidx(flow, fidx, 0)) {
1045                 t4_os_free(flow);
1046                 return rte_flow_error_set(e, EINVAL, RTE_FLOW_ERROR_TYPE_HANDLE,
1047                                           NULL, "validation failed");
1048         }
1049
1050         t4_os_free(flow);
1051         return 0;
1052 }
1053
1054 /*
1055  * @ret : > 0 filter destroyed succsesfully
1056  *        < 0 error destroying filter
1057  *        == 1 filter not active / not found
1058  */
1059 static int
1060 cxgbe_check_n_destroy(struct filter_entry *f, struct rte_eth_dev *dev,
1061                       struct rte_flow_error *e)
1062 {
1063         if (f && (f->valid || f->pending) &&
1064             f->dev == dev && /* Only if user has asked for this port */
1065              f->private) /* We (rte_flow) created this filter */
1066                 return cxgbe_flow_destroy(dev, (struct rte_flow *)f->private,
1067                                           e);
1068         return 1;
1069 }
1070
1071 static int cxgbe_flow_flush(struct rte_eth_dev *dev, struct rte_flow_error *e)
1072 {
1073         struct adapter *adap = ethdev2adap(dev);
1074         unsigned int i;
1075         int ret = 0;
1076
1077         if (adap->tids.ftid_tab) {
1078                 struct filter_entry *f = &adap->tids.ftid_tab[0];
1079
1080                 for (i = 0; i < adap->tids.nftids; i++, f++) {
1081                         ret = cxgbe_check_n_destroy(f, dev, e);
1082                         if (ret < 0)
1083                                 goto out;
1084                 }
1085         }
1086
1087         if (is_hashfilter(adap) && adap->tids.tid_tab) {
1088                 struct filter_entry *f;
1089
1090                 for (i = adap->tids.hash_base; i <= adap->tids.ntids; i++) {
1091                         f = (struct filter_entry *)adap->tids.tid_tab[i];
1092
1093                         ret = cxgbe_check_n_destroy(f, dev, e);
1094                         if (ret < 0)
1095                                 goto out;
1096                 }
1097         }
1098
1099 out:
1100         return ret >= 0 ? 0 : ret;
1101 }
1102
1103 static const struct rte_flow_ops cxgbe_flow_ops = {
1104         .validate       = cxgbe_flow_validate,
1105         .create         = cxgbe_flow_create,
1106         .destroy        = cxgbe_flow_destroy,
1107         .flush          = cxgbe_flow_flush,
1108         .query          = cxgbe_flow_query,
1109         .isolate        = NULL,
1110 };
1111
1112 int
1113 cxgbe_dev_filter_ctrl(struct rte_eth_dev *dev,
1114                       enum rte_filter_type filter_type,
1115                       enum rte_filter_op filter_op,
1116                       void *arg)
1117 {
1118         int ret = 0;
1119
1120         RTE_SET_USED(dev);
1121         switch (filter_type) {
1122         case RTE_ETH_FILTER_GENERIC:
1123                 if (filter_op != RTE_ETH_FILTER_GET)
1124                         return -EINVAL;
1125                 *(const void **)arg = &cxgbe_flow_ops;
1126                 break;
1127         default:
1128                 ret = -ENOTSUP;
1129                 break;
1130         }
1131         return ret;
1132 }