enic: move to drivers/net/
[dpdk.git] / drivers / net / enic / enic_clsf.c
1 /*
2  * Copyright 2008-2014 Cisco Systems, Inc.  All rights reserved.
3  * Copyright 2007 Nuova Systems, Inc.  All rights reserved.
4  *
5  * Copyright (c) 2014, Cisco Systems, Inc.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  *
33  */
34 #ident "$Id$"
35
36 #include <libgen.h>
37
38 #include <rte_ethdev.h>
39 #include <rte_malloc.h>
40 #include <rte_hash.h>
41 #include <rte_byteorder.h>
42
43 #include "enic_compat.h"
44 #include "enic.h"
45 #include "wq_enet_desc.h"
46 #include "rq_enet_desc.h"
47 #include "cq_enet_desc.h"
48 #include "vnic_enet.h"
49 #include "vnic_dev.h"
50 #include "vnic_wq.h"
51 #include "vnic_rq.h"
52 #include "vnic_cq.h"
53 #include "vnic_intr.h"
54 #include "vnic_nic.h"
55
56 #ifdef RTE_MACHINE_CPUFLAG_SSE4_2
57 #include <rte_hash_crc.h>
58 #define DEFAULT_HASH_FUNC       rte_hash_crc
59 #else
60 #include <rte_jhash.h>
61 #define DEFAULT_HASH_FUNC       rte_jhash
62 #endif
63
64 #define SOCKET_0                0
65 #define ENICPMD_CLSF_HASH_ENTRIES       ENICPMD_FDIR_MAX
66 #define ENICPMD_CLSF_BUCKET_ENTRIES     4
67
68 void enic_fdir_stats_get(struct enic *enic, struct rte_eth_fdir_stats *stats)
69 {
70         *stats = enic->fdir.stats;
71 }
72
73 int enic_fdir_del_fltr(struct enic *enic, struct rte_eth_fdir_filter *params)
74 {
75         int32_t pos;
76         struct enic_fdir_node *key;
77         /* See if the key is in the table */
78         pos = rte_hash_del_key(enic->fdir.hash, params);
79         switch (pos) {
80         case -EINVAL:
81         case -ENOENT:
82                 enic->fdir.stats.f_remove++;
83                 return -EINVAL;
84         default:
85                 /* The entry is present in the table */
86                 key = enic->fdir.nodes[pos];
87
88                 /* Delete the filter */
89                 vnic_dev_classifier(enic->vdev, CLSF_DEL,
90                         &key->fltr_id, NULL);
91                 rte_free(key);
92                 enic->fdir.nodes[pos] = NULL;
93                 enic->fdir.stats.free++;
94                 enic->fdir.stats.remove++;
95                 break;
96         }
97         return 0;
98 }
99
100 int enic_fdir_add_fltr(struct enic *enic, struct rte_eth_fdir_filter *params)
101 {
102         struct enic_fdir_node *key;
103         struct filter fltr = {0};
104         int32_t pos;
105         u8 do_free = 0;
106         u16 old_fltr_id = 0;
107         u32 flowtype_supported;
108         u16 flex_bytes;
109         u16 queue;
110
111         flowtype_supported = (
112                 (RTE_ETH_FLOW_NONFRAG_IPV4_TCP == params->input.flow_type) ||
113                 (RTE_ETH_FLOW_NONFRAG_IPV4_UDP == params->input.flow_type));
114
115         flex_bytes = ((params->input.flow_ext.flexbytes[1] << 8 & 0xFF00) |
116                 (params->input.flow_ext.flexbytes[0] & 0xFF));
117
118         if (!enic->fdir.hash ||
119                 (params->input.flow_ext.vlan_tci & 0xFFF) ||
120                 !flowtype_supported || flex_bytes ||
121                 params->action.behavior /* drop */) {
122                 enic->fdir.stats.f_add++;
123                 return -ENOTSUP;
124         }
125
126         queue = params->action.rx_queue;
127         /* See if the key is already there in the table */
128         pos = rte_hash_del_key(enic->fdir.hash, params);
129         switch (pos) {
130         case -EINVAL:
131                 enic->fdir.stats.f_add++;
132                 return -EINVAL;
133         case -ENOENT:
134                 /* Add a new classifier entry */
135                 if (!enic->fdir.stats.free) {
136                         enic->fdir.stats.f_add++;
137                         return -ENOSPC;
138                 }
139                 key = rte_zmalloc("enic_fdir_node",
140                                   sizeof(struct enic_fdir_node), 0);
141                 if (!key) {
142                         enic->fdir.stats.f_add++;
143                         return -ENOMEM;
144                 }
145                 break;
146         default:
147                 /* The entry is already present in the table.
148                  * Check if there is a change in queue
149                  */
150                 key = enic->fdir.nodes[pos];
151                 enic->fdir.nodes[pos] = NULL;
152                 if (unlikely(key->rq_index == queue)) {
153                         /* Nothing to be done */
154                         pos = rte_hash_add_key(enic->fdir.hash, params);
155                         enic->fdir.nodes[pos] = key;
156                         enic->fdir.stats.f_add++;
157                         dev_warning(enic,
158                                 "FDIR rule is already present\n");
159                         return 0;
160                 }
161
162                 if (likely(enic->fdir.stats.free)) {
163                         /* Add the filter and then delete the old one.
164                          * This is to avoid packets from going into the
165                          * default queue during the window between
166                          * delete and add
167                          */
168                         do_free = 1;
169                         old_fltr_id = key->fltr_id;
170                 } else {
171                         /* No free slots in the classifier.
172                          * Delete the filter and add the modified one later
173                          */
174                         vnic_dev_classifier(enic->vdev, CLSF_DEL,
175                                 &key->fltr_id, NULL);
176                         enic->fdir.stats.free++;
177                 }
178
179                 break;
180         }
181
182         key->filter = *params;
183         key->rq_index = queue;
184
185         fltr.type = FILTER_IPV4_5TUPLE;
186         fltr.u.ipv4.src_addr = rte_be_to_cpu_32(
187                 params->input.flow.ip4_flow.src_ip);
188         fltr.u.ipv4.dst_addr = rte_be_to_cpu_32(
189                 params->input.flow.ip4_flow.dst_ip);
190         fltr.u.ipv4.src_port = rte_be_to_cpu_16(
191                 params->input.flow.udp4_flow.src_port);
192         fltr.u.ipv4.dst_port = rte_be_to_cpu_16(
193                 params->input.flow.udp4_flow.dst_port);
194
195         if (RTE_ETH_FLOW_NONFRAG_IPV4_TCP == params->input.flow_type)
196                 fltr.u.ipv4.protocol = PROTO_TCP;
197         else
198                 fltr.u.ipv4.protocol = PROTO_UDP;
199
200         fltr.u.ipv4.flags = FILTER_FIELDS_IPV4_5TUPLE;
201
202         if (!vnic_dev_classifier(enic->vdev, CLSF_ADD, &queue, &fltr)) {
203                 key->fltr_id = queue;
204         } else {
205                 dev_err(enic, "Add classifier entry failed\n");
206                 enic->fdir.stats.f_add++;
207                 rte_free(key);
208                 return -1;
209         }
210
211         if (do_free)
212                 vnic_dev_classifier(enic->vdev, CLSF_DEL, &old_fltr_id, NULL);
213         else{
214                 enic->fdir.stats.free--;
215                 enic->fdir.stats.add++;
216         }
217
218         pos = rte_hash_add_key(enic->fdir.hash, (void *)key);
219         enic->fdir.nodes[pos] = key;
220         return 0;
221 }
222
223 void enic_clsf_destroy(struct enic *enic)
224 {
225         u32 index;
226         struct enic_fdir_node *key;
227         /* delete classifier entries */
228         for (index = 0; index < ENICPMD_FDIR_MAX; index++) {
229                 key = enic->fdir.nodes[index];
230                 if (key) {
231                         vnic_dev_classifier(enic->vdev, CLSF_DEL,
232                                 &key->fltr_id, NULL);
233                         rte_free(key);
234                 }
235         }
236
237         if (enic->fdir.hash) {
238                 rte_hash_free(enic->fdir.hash);
239                 enic->fdir.hash = NULL;
240         }
241 }
242
243 int enic_clsf_init(struct enic *enic)
244 {
245         struct rte_hash_parameters hash_params = {
246                 .name = "enicpmd_clsf_hash",
247                 .entries = ENICPMD_CLSF_HASH_ENTRIES,
248                 .bucket_entries = ENICPMD_CLSF_BUCKET_ENTRIES,
249                 .key_len = RTE_HASH_KEY_LENGTH_MAX,
250                 .hash_func = DEFAULT_HASH_FUNC,
251                 .hash_func_init_val = 0,
252                 .socket_id = SOCKET_0,
253         };
254
255         enic->fdir.hash = rte_hash_create(&hash_params);
256         memset(&enic->fdir.stats, 0, sizeof(enic->fdir.stats));
257         enic->fdir.stats.free = ENICPMD_FDIR_MAX;
258         return (NULL == enic->fdir.hash);
259 }