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