e9aac27148ca10631c56eb94324b00207ce5250b
[deb_dpdk.git] / drivers / net / bnxt / bnxt_filter.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) Broadcom Limited.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Broadcom Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #include <sys/queue.h>
35
36 #include <rte_log.h>
37 #include <rte_malloc.h>
38
39 #include "bnxt.h"
40 #include "bnxt_filter.h"
41 #include "bnxt_hwrm.h"
42 #include "bnxt_vnic.h"
43 #include "hsi_struct_def_dpdk.h"
44
45 /*
46  * Filter Functions
47  */
48
49 struct bnxt_filter_info *bnxt_alloc_filter(struct bnxt *bp)
50 {
51         struct bnxt_filter_info *filter;
52
53         /* Find the 1st unused filter from the free_filter_list pool*/
54         filter = STAILQ_FIRST(&bp->free_filter_list);
55         if (!filter) {
56                 RTE_LOG(ERR, PMD, "No more free filter resources\n");
57                 return NULL;
58         }
59         STAILQ_REMOVE_HEAD(&bp->free_filter_list, next);
60
61         /* Default to L2 MAC Addr filter */
62         filter->flags = HWRM_CFA_L2_FILTER_ALLOC_INPUT_FLAGS_PATH_RX;
63         filter->enables = HWRM_CFA_L2_FILTER_ALLOC_INPUT_ENABLES_L2_ADDR |
64                         HWRM_CFA_L2_FILTER_ALLOC_INPUT_ENABLES_L2_ADDR_MASK;
65         memcpy(filter->l2_addr, bp->eth_dev->data->mac_addrs->addr_bytes,
66                ETHER_ADDR_LEN);
67         memset(filter->l2_addr_mask, 0xff, ETHER_ADDR_LEN);
68         return filter;
69 }
70
71 struct bnxt_filter_info *bnxt_alloc_vf_filter(struct bnxt *bp, uint16_t vf)
72 {
73         struct bnxt_filter_info *filter;
74
75         filter = rte_zmalloc("bnxt_vf_filter_info", sizeof(*filter), 0);
76         if (!filter) {
77                 RTE_LOG(ERR, PMD, "Failed to alloc memory for VF %hu filters\n",
78                         vf);
79                 return NULL;
80         }
81
82         filter->fw_l2_filter_id = UINT64_MAX;
83         STAILQ_INSERT_TAIL(&bp->pf.vf_info[vf].filter, filter, next);
84         return filter;
85 }
86
87 void bnxt_init_filters(struct bnxt *bp)
88 {
89         struct bnxt_filter_info *filter;
90         int i, max_filters;
91
92         max_filters = bp->max_l2_ctx;
93         STAILQ_INIT(&bp->free_filter_list);
94         for (i = 0; i < max_filters; i++) {
95                 filter = &bp->filter_info[i];
96                 filter->fw_l2_filter_id = -1;
97                 STAILQ_INSERT_TAIL(&bp->free_filter_list, filter, next);
98         }
99 }
100
101 void bnxt_free_all_filters(struct bnxt *bp)
102 {
103         struct bnxt_vnic_info *vnic;
104         struct bnxt_filter_info *filter, *temp_filter;
105         int i;
106
107         for (i = 0; i < MAX_FF_POOLS; i++) {
108                 STAILQ_FOREACH(vnic, &bp->ff_pool[i], next) {
109                         filter = STAILQ_FIRST(&vnic->filter);
110                         while (filter) {
111                                 temp_filter = STAILQ_NEXT(filter, next);
112                                 STAILQ_REMOVE(&vnic->filter, filter,
113                                               bnxt_filter_info, next);
114                                 STAILQ_INSERT_TAIL(&bp->free_filter_list,
115                                                    filter, next);
116                                 filter = temp_filter;
117                         }
118                         STAILQ_INIT(&vnic->filter);
119                 }
120         }
121
122         for (i = 0; i < bp->pf.max_vfs; i++) {
123                 STAILQ_FOREACH(filter, &bp->pf.vf_info[i].filter, next) {
124                         bnxt_hwrm_clear_filter(bp, filter);
125                 }
126         }
127 }
128
129 void bnxt_free_filter_mem(struct bnxt *bp)
130 {
131         struct bnxt_filter_info *filter;
132         uint16_t max_filters, i;
133         int rc = 0;
134
135         if (bp->filter_info == NULL)
136                 return;
137
138         /* Ensure that all filters are freed */
139         max_filters = bp->max_l2_ctx;
140         for (i = 0; i < max_filters; i++) {
141                 filter = &bp->filter_info[i];
142                 if (filter->fw_l2_filter_id != ((uint64_t)-1)) {
143                         RTE_LOG(ERR, PMD, "HWRM filter is not freed??\n");
144                         /* Call HWRM to try to free filter again */
145                         rc = bnxt_hwrm_clear_filter(bp, filter);
146                         if (rc)
147                                 RTE_LOG(ERR, PMD,
148                                        "HWRM filter cannot be freed rc = %d\n",
149                                         rc);
150                 }
151                 filter->fw_l2_filter_id = UINT64_MAX;
152         }
153         STAILQ_INIT(&bp->free_filter_list);
154
155         rte_free(bp->filter_info);
156         bp->filter_info = NULL;
157 }
158
159 int bnxt_alloc_filter_mem(struct bnxt *bp)
160 {
161         struct bnxt_filter_info *filter_mem;
162         uint16_t max_filters;
163
164         max_filters = bp->max_l2_ctx;
165         /* Allocate memory for VNIC pool and filter pool */
166         filter_mem = rte_zmalloc("bnxt_filter_info",
167                                  max_filters * sizeof(struct bnxt_filter_info),
168                                  0);
169         if (filter_mem == NULL) {
170                 RTE_LOG(ERR, PMD, "Failed to alloc memory for %d filters",
171                         max_filters);
172                 return -ENOMEM;
173         }
174         bp->filter_info = filter_mem;
175         return 0;
176 }