i40e: set up and initialize flow director
[dpdk.git] / lib / librte_pmd_i40e / i40e_fdir.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
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 Intel 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 #include <stdio.h>
36 #include <errno.h>
37 #include <stdint.h>
38 #include <string.h>
39 #include <unistd.h>
40 #include <stdarg.h>
41
42 #include <rte_ether.h>
43 #include <rte_ethdev.h>
44 #include <rte_log.h>
45 #include <rte_memzone.h>
46 #include <rte_malloc.h>
47
48 #include "i40e_logs.h"
49 #include "i40e/i40e_type.h"
50 #include "i40e_ethdev.h"
51 #include "i40e_rxtx.h"
52
53 #define I40E_FDIR_MZ_NAME          "FDIR_MEMZONE"
54 #define I40E_FDIR_PKT_LEN                   512
55
56
57 /* Wait count and interval for fdir filter flush */
58 #define I40E_FDIR_FLUSH_RETRY       50
59 #define I40E_FDIR_FLUSH_INTERVAL_MS 5
60
61 #define I40E_COUNTER_PF           2
62 /* Statistic counter index for one pf */
63 #define I40E_COUNTER_INDEX_FDIR(pf_id)   (0 + (pf_id) * I40E_COUNTER_PF)
64 #define I40E_FLX_OFFSET_IN_FIELD_VECTOR   50
65
66 static int i40e_fdir_rx_queue_init(struct i40e_rx_queue *rxq);
67 static int i40e_fdir_flush(struct rte_eth_dev *dev);
68
69 static int
70 i40e_fdir_rx_queue_init(struct i40e_rx_queue *rxq)
71 {
72         struct i40e_hw *hw = I40E_VSI_TO_HW(rxq->vsi);
73         struct i40e_hmc_obj_rxq rx_ctx;
74         int err = I40E_SUCCESS;
75
76         memset(&rx_ctx, 0, sizeof(struct i40e_hmc_obj_rxq));
77         /* Init the RX queue in hardware */
78         rx_ctx.dbuff = I40E_RXBUF_SZ_1024 >> I40E_RXQ_CTX_DBUFF_SHIFT;
79         rx_ctx.hbuff = 0;
80         rx_ctx.base = rxq->rx_ring_phys_addr / I40E_QUEUE_BASE_ADDR_UNIT;
81         rx_ctx.qlen = rxq->nb_rx_desc;
82 #ifndef RTE_LIBRTE_I40E_16BYTE_RX_DESC
83         rx_ctx.dsize = 1;
84 #endif
85         rx_ctx.dtype = i40e_header_split_none;
86         rx_ctx.hsplit_0 = I40E_HEADER_SPLIT_NONE;
87         rx_ctx.rxmax = ETHER_MAX_LEN;
88         rx_ctx.tphrdesc_ena = 1;
89         rx_ctx.tphwdesc_ena = 1;
90         rx_ctx.tphdata_ena = 1;
91         rx_ctx.tphhead_ena = 1;
92         rx_ctx.lrxqthresh = 2;
93         rx_ctx.crcstrip = 0;
94         rx_ctx.l2tsel = 1;
95         rx_ctx.showiv = 1;
96         rx_ctx.prefena = 1;
97
98         err = i40e_clear_lan_rx_queue_context(hw, rxq->reg_idx);
99         if (err != I40E_SUCCESS) {
100                 PMD_DRV_LOG(ERR, "Failed to clear FDIR RX queue context.");
101                 return err;
102         }
103         err = i40e_set_lan_rx_queue_context(hw, rxq->reg_idx, &rx_ctx);
104         if (err != I40E_SUCCESS) {
105                 PMD_DRV_LOG(ERR, "Failed to set FDIR RX queue context.");
106                 return err;
107         }
108         rxq->qrx_tail = hw->hw_addr +
109                 I40E_QRX_TAIL(rxq->vsi->base_queue);
110
111         rte_wmb();
112         /* Init the RX tail regieter. */
113         I40E_PCI_REG_WRITE(rxq->qrx_tail, 0);
114         I40E_PCI_REG_WRITE(rxq->qrx_tail, rxq->nb_rx_desc - 1);
115
116         return err;
117 }
118
119 /*
120  * i40e_fdir_setup - reserve and initialize the Flow Director resources
121  * @pf: board private structure
122  */
123 int
124 i40e_fdir_setup(struct i40e_pf *pf)
125 {
126         struct i40e_hw *hw = I40E_PF_TO_HW(pf);
127         struct i40e_vsi *vsi;
128         int err = I40E_SUCCESS;
129         char z_name[RTE_MEMZONE_NAMESIZE];
130         const struct rte_memzone *mz = NULL;
131         struct rte_eth_dev *eth_dev = pf->adapter->eth_dev;
132
133         PMD_DRV_LOG(INFO, "FDIR HW Capabilities: num_filters_guaranteed = %u,"
134                         " num_filters_best_effort = %u.",
135                         hw->func_caps.fd_filters_guaranteed,
136                         hw->func_caps.fd_filters_best_effort);
137
138         vsi = pf->fdir.fdir_vsi;
139         if (vsi) {
140                 PMD_DRV_LOG(ERR, "FDIR vsi pointer needs "
141                                  "to be null before creation.");
142                 return I40E_ERR_BAD_PTR;
143         }
144         /* make new FDIR VSI */
145         vsi = i40e_vsi_setup(pf, I40E_VSI_FDIR, pf->main_vsi, 0);
146         if (!vsi) {
147                 PMD_DRV_LOG(ERR, "Couldn't create FDIR VSI.");
148                 return I40E_ERR_NO_AVAILABLE_VSI;
149         }
150         pf->fdir.fdir_vsi = vsi;
151
152         /*Fdir tx queue setup*/
153         err = i40e_fdir_setup_tx_resources(pf);
154         if (err) {
155                 PMD_DRV_LOG(ERR, "Failed to setup FDIR TX resources.");
156                 goto fail_setup_tx;
157         }
158
159         /*Fdir rx queue setup*/
160         err = i40e_fdir_setup_rx_resources(pf);
161         if (err) {
162                 PMD_DRV_LOG(ERR, "Failed to setup FDIR RX resources.");
163                 goto fail_setup_rx;
164         }
165
166         err = i40e_tx_queue_init(pf->fdir.txq);
167         if (err) {
168                 PMD_DRV_LOG(ERR, "Failed to do FDIR TX initialization.");
169                 goto fail_mem;
170         }
171
172         /* need switch on before dev start*/
173         err = i40e_switch_tx_queue(hw, vsi->base_queue, TRUE);
174         if (err) {
175                 PMD_DRV_LOG(ERR, "Failed to do fdir TX switch on.");
176                 goto fail_mem;
177         }
178
179         /* Init the rx queue in hardware */
180         err = i40e_fdir_rx_queue_init(pf->fdir.rxq);
181         if (err) {
182                 PMD_DRV_LOG(ERR, "Failed to do FDIR RX initialization.");
183                 goto fail_mem;
184         }
185
186         /* switch on rx queue */
187         err = i40e_switch_rx_queue(hw, vsi->base_queue, TRUE);
188         if (err) {
189                 PMD_DRV_LOG(ERR, "Failed to do FDIR RX switch on.");
190                 goto fail_mem;
191         }
192
193         /* reserve memory for the fdir programming packet */
194         snprintf(z_name, sizeof(z_name), "%s_%s_%d",
195                         eth_dev->driver->pci_drv.name,
196                         I40E_FDIR_MZ_NAME,
197                         eth_dev->data->port_id);
198         mz = i40e_memzone_reserve(z_name, I40E_FDIR_PKT_LEN, SOCKET_ID_ANY);
199         if (!mz) {
200                 PMD_DRV_LOG(ERR, "Cannot init memzone for "
201                                  "flow director program packet.");
202                 err = I40E_ERR_NO_MEMORY;
203                 goto fail_mem;
204         }
205         pf->fdir.prg_pkt = mz->addr;
206 #ifdef RTE_LIBRTE_XEN_DOM0
207         pf->fdir.dma_addr = rte_mem_phy2mch(mz->memseg_id, mz->phys_addr);
208 #else
209         pf->fdir.dma_addr = (uint64_t)mz->phys_addr;
210 #endif
211         pf->fdir.match_counter_index = I40E_COUNTER_INDEX_FDIR(hw->pf_id);
212         PMD_DRV_LOG(INFO, "FDIR setup successfully, with programming queue %u.",
213                     vsi->base_queue);
214         return I40E_SUCCESS;
215
216 fail_mem:
217         i40e_dev_rx_queue_release(pf->fdir.rxq);
218         pf->fdir.rxq = NULL;
219 fail_setup_rx:
220         i40e_dev_tx_queue_release(pf->fdir.txq);
221         pf->fdir.txq = NULL;
222 fail_setup_tx:
223         i40e_vsi_release(vsi);
224         pf->fdir.fdir_vsi = NULL;
225         return err;
226 }
227
228 /* check whether the flow director table in empty */
229 static inline int
230 i40e_fdir_empty(struct i40e_hw *hw)
231 {
232         uint32_t guarant_cnt, best_cnt;
233
234         guarant_cnt = (uint32_t)((I40E_READ_REG(hw, I40E_PFQF_FDSTAT) &
235                                  I40E_PFQF_FDSTAT_GUARANT_CNT_MASK) >>
236                                  I40E_PFQF_FDSTAT_GUARANT_CNT_SHIFT);
237         best_cnt = (uint32_t)((I40E_READ_REG(hw, I40E_PFQF_FDSTAT) &
238                               I40E_PFQF_FDSTAT_BEST_CNT_MASK) >>
239                               I40E_PFQF_FDSTAT_BEST_CNT_SHIFT);
240         if (best_cnt + guarant_cnt > 0)
241                 return -1;
242
243         return 0;
244 }
245
246 /*
247  * Configure flow director related setting
248  */
249 int
250 i40e_fdir_configure(struct rte_eth_dev *dev)
251 {
252         struct i40e_pf *pf = I40E_DEV_PRIVATE_TO_PF(dev->data->dev_private);
253         struct i40e_hw *hw = I40E_DEV_PRIVATE_TO_HW(dev->data->dev_private);
254         uint32_t val;
255         int ret = 0;
256
257         /*
258         * configuration need to be done before
259         * flow director filters are added
260         * If filters exist, flush them.
261         */
262         if (i40e_fdir_empty(hw) < 0) {
263                 ret = i40e_fdir_flush(dev);
264                 if (ret) {
265                         PMD_DRV_LOG(ERR, "failed to flush fdir table.");
266                         return ret;
267                 }
268         }
269
270         val = I40E_READ_REG(hw, I40E_PFQF_CTL_0);
271         if ((pf->flags & I40E_FLAG_FDIR) &&
272                 dev->data->dev_conf.fdir_conf.mode == RTE_FDIR_MODE_PERFECT) {
273                 /* enable FDIR filter */
274                 val |= I40E_PFQF_CTL_0_FD_ENA_MASK;
275                 I40E_WRITE_REG(hw, I40E_PFQF_CTL_0, val);
276         } else {
277                 /* disable FDIR filter */
278                 val &= ~I40E_PFQF_CTL_0_FD_ENA_MASK;
279                 I40E_WRITE_REG(hw, I40E_PFQF_CTL_0, val);
280                 pf->flags &= ~I40E_FLAG_FDIR;
281         }
282
283         return ret;
284 }
285
286 /*
287  * i40e_fdir_flush - clear all filters of Flow Director table
288  * @pf: board private structure
289  */
290 static int
291 i40e_fdir_flush(struct rte_eth_dev *dev)
292 {
293         struct i40e_pf *pf = I40E_DEV_PRIVATE_TO_PF(dev->data->dev_private);
294         struct i40e_hw *hw = I40E_PF_TO_HW(pf);
295         uint32_t reg;
296         uint16_t guarant_cnt, best_cnt;
297         int i;
298
299         I40E_WRITE_REG(hw, I40E_PFQF_CTL_1, I40E_PFQF_CTL_1_CLEARFDTABLE_MASK);
300         I40E_WRITE_FLUSH(hw);
301
302         for (i = 0; i < I40E_FDIR_FLUSH_RETRY; i++) {
303                 rte_delay_ms(I40E_FDIR_FLUSH_INTERVAL_MS);
304                 reg = I40E_READ_REG(hw, I40E_PFQF_CTL_1);
305                 if (!(reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK))
306                         break;
307         }
308         if (i >= I40E_FDIR_FLUSH_RETRY) {
309                 PMD_DRV_LOG(ERR, "FD table did not flush, may need more time.");
310                 return -ETIMEDOUT;
311         }
312         guarant_cnt = (uint16_t)((I40E_READ_REG(hw, I40E_PFQF_FDSTAT) &
313                                 I40E_PFQF_FDSTAT_GUARANT_CNT_MASK) >>
314                                 I40E_PFQF_FDSTAT_GUARANT_CNT_SHIFT);
315         best_cnt = (uint16_t)((I40E_READ_REG(hw, I40E_PFQF_FDSTAT) &
316                                 I40E_PFQF_FDSTAT_BEST_CNT_MASK) >>
317                                 I40E_PFQF_FDSTAT_BEST_CNT_SHIFT);
318         if (guarant_cnt != 0 || best_cnt != 0) {
319                 PMD_DRV_LOG(ERR, "Failed to flush FD table.");
320                 return -ENOSYS;
321         } else
322                 PMD_DRV_LOG(INFO, "FD table Flush success.");
323         return 0;
324 }