net/sfc: provide a way to check if filter is supported
[dpdk.git] / drivers / net / sfc / sfc_filter.c
1 /*-
2  * Copyright (c) 2017 Solarflare Communications Inc.
3  * All rights reserved.
4  *
5  * This software was jointly developed between OKTET Labs (under contract
6  * for Solarflare) and Solarflare Communications, Inc.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright notice,
12  *    this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright notice,
14  *    this list of conditions and the following disclaimer in the documentation
15  *    and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
19  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
21  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
24  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
27  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29
30 #include <rte_common.h>
31
32 #include "efx.h"
33
34 #include "sfc.h"
35 #include "sfc_log.h"
36
37 boolean_t
38 sfc_filter_is_match_supported(struct sfc_adapter *sa, uint32_t match)
39 {
40         struct sfc_filter *filter = &sa->filter;
41         size_t i;
42
43         for (i = 0; i < filter->supported_match_num; ++i) {
44                 if (match == filter->supported_match[i])
45                         return B_TRUE;
46         }
47
48         return B_FALSE;
49 }
50
51 static int
52 sfc_filter_cache_match_supported(struct sfc_adapter *sa)
53 {
54         struct sfc_filter *filter = &sa->filter;
55         size_t num = filter->supported_match_num;
56         uint32_t *buf = filter->supported_match;
57         unsigned int retry;
58         int rc;
59
60         /* Just a guess of possibly sufficient entries */
61         if (num == 0)
62                 num = 16;
63
64         for (retry = 0; retry < 2; ++retry) {
65                 if (num != filter->supported_match_num) {
66                         rc = ENOMEM;
67                         buf = rte_realloc(buf, num * sizeof(*buf), 0);
68                         if (buf == NULL)
69                                 goto fail_realloc;
70                 }
71
72                 rc = efx_filter_supported_filters(sa->nic, buf, num, &num);
73                 if (rc == 0) {
74                         filter->supported_match_num = num;
75                         filter->supported_match = buf;
76
77                         return 0;
78                 } else if (rc != ENOSPC) {
79                         goto fail_efx_filter_supported_filters;
80                 }
81         }
82
83         SFC_ASSERT(rc == ENOSPC);
84
85 fail_efx_filter_supported_filters:
86 fail_realloc:
87         /* Original pointer is not freed by rte_realloc() on failure */
88         rte_free(buf);
89         filter->supported_match = NULL;
90         filter->supported_match_num = 0;
91         return rc;
92 }
93
94 int
95 sfc_filter_attach(struct sfc_adapter *sa)
96 {
97         int rc;
98
99         sfc_log_init(sa, "entry");
100
101         rc = efx_filter_init(sa->nic);
102         if (rc != 0)
103                 goto fail_filter_init;
104
105         rc = sfc_filter_cache_match_supported(sa);
106         if (rc != 0)
107                 goto fail_cache_match_supported;
108
109         efx_filter_fini(sa->nic);
110
111         sfc_log_init(sa, "done");
112
113         return 0;
114
115 fail_cache_match_supported:
116         efx_filter_fini(sa->nic);
117
118 fail_filter_init:
119         sfc_log_init(sa, "failed %d", rc);
120         return rc;
121 }
122
123 void
124 sfc_filter_detach(struct sfc_adapter *sa)
125 {
126         struct sfc_filter *filter = &sa->filter;
127
128         sfc_log_init(sa, "entry");
129
130         rte_free(filter->supported_match);
131         filter->supported_match = NULL;
132         filter->supported_match_num = 0;
133
134         sfc_log_init(sa, "done");
135 }