i40e: fix stats reset
[dpdk.git] / drivers / net / i40e / i40e_ethdev.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2015 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 #include <inttypes.h>
42 #include <assert.h>
43
44 #include <rte_string_fns.h>
45 #include <rte_pci.h>
46 #include <rte_ether.h>
47 #include <rte_ethdev.h>
48 #include <rte_memzone.h>
49 #include <rte_malloc.h>
50 #include <rte_memcpy.h>
51 #include <rte_alarm.h>
52 #include <rte_dev.h>
53 #include <rte_eth_ctrl.h>
54
55 #include "i40e_logs.h"
56 #include "base/i40e_prototype.h"
57 #include "base/i40e_adminq_cmd.h"
58 #include "base/i40e_type.h"
59 #include "base/i40e_register.h"
60 #include "base/i40e_dcb.h"
61 #include "i40e_ethdev.h"
62 #include "i40e_rxtx.h"
63 #include "i40e_pf.h"
64
65 /* Maximun number of MAC addresses */
66 #define I40E_NUM_MACADDR_MAX       64
67 #define I40E_CLEAR_PXE_WAIT_MS     200
68
69 /* Maximun number of capability elements */
70 #define I40E_MAX_CAP_ELE_NUM       128
71
72 /* Wait count and inteval */
73 #define I40E_CHK_Q_ENA_COUNT       1000
74 #define I40E_CHK_Q_ENA_INTERVAL_US 1000
75
76 /* Maximun number of VSI */
77 #define I40E_MAX_NUM_VSIS          (384UL)
78
79 #define I40E_PRE_TX_Q_CFG_WAIT_US       10 /* 10 us */
80
81 /* Flow control default timer */
82 #define I40E_DEFAULT_PAUSE_TIME 0xFFFFU
83
84 /* Flow control default high water */
85 #define I40E_DEFAULT_HIGH_WATER (0x1C40/1024)
86
87 /* Flow control default low water */
88 #define I40E_DEFAULT_LOW_WATER  (0x1A40/1024)
89
90 /* Flow control enable fwd bit */
91 #define I40E_PRTMAC_FWD_CTRL   0x00000001
92
93 /* Receive Packet Buffer size */
94 #define I40E_RXPBSIZE (968 * 1024)
95
96 /* Kilobytes shift */
97 #define I40E_KILOSHIFT 10
98
99 /* Receive Average Packet Size in Byte*/
100 #define I40E_PACKET_AVERAGE_SIZE 128
101
102 /* Mask of PF interrupt causes */
103 #define I40E_PFINT_ICR0_ENA_MASK ( \
104                 I40E_PFINT_ICR0_ENA_ECC_ERR_MASK | \
105                 I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK | \
106                 I40E_PFINT_ICR0_ENA_GRST_MASK | \
107                 I40E_PFINT_ICR0_ENA_PCI_EXCEPTION_MASK | \
108                 I40E_PFINT_ICR0_ENA_STORM_DETECT_MASK | \
109                 I40E_PFINT_ICR0_ENA_LINK_STAT_CHANGE_MASK | \
110                 I40E_PFINT_ICR0_ENA_HMC_ERR_MASK | \
111                 I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK | \
112                 I40E_PFINT_ICR0_ENA_VFLR_MASK | \
113                 I40E_PFINT_ICR0_ENA_ADMINQ_MASK)
114
115 #define I40E_FLOW_TYPES ( \
116         (1UL << RTE_ETH_FLOW_FRAG_IPV4) | \
117         (1UL << RTE_ETH_FLOW_NONFRAG_IPV4_TCP) | \
118         (1UL << RTE_ETH_FLOW_NONFRAG_IPV4_UDP) | \
119         (1UL << RTE_ETH_FLOW_NONFRAG_IPV4_SCTP) | \
120         (1UL << RTE_ETH_FLOW_NONFRAG_IPV4_OTHER) | \
121         (1UL << RTE_ETH_FLOW_FRAG_IPV6) | \
122         (1UL << RTE_ETH_FLOW_NONFRAG_IPV6_TCP) | \
123         (1UL << RTE_ETH_FLOW_NONFRAG_IPV6_UDP) | \
124         (1UL << RTE_ETH_FLOW_NONFRAG_IPV6_SCTP) | \
125         (1UL << RTE_ETH_FLOW_NONFRAG_IPV6_OTHER) | \
126         (1UL << RTE_ETH_FLOW_L2_PAYLOAD))
127
128 #define I40E_PTP_40GB_INCVAL  0x0199999999ULL
129 #define I40E_PTP_10GB_INCVAL  0x0333333333ULL
130 #define I40E_PTP_1GB_INCVAL   0x2000000000ULL
131 #define I40E_PRTTSYN_TSYNENA  0x80000000
132 #define I40E_PRTTSYN_TSYNTYPE 0x0e000000
133
134 #define I40E_MAX_PERCENT            100
135 #define I40E_DEFAULT_DCB_APP_NUM    1
136 #define I40E_DEFAULT_DCB_APP_PRIO   3
137
138 #define I40E_PRTQF_FD_INSET(_i, _j)  (0x00250000 + ((_i) * 64 + (_j) * 32))
139 #define I40E_GLQF_FD_MSK(_i, _j)     (0x00267200 + ((_i) * 4 + (_j) * 8))
140 #define I40E_GLQF_FD_MSK_FIELD       0x0000FFFF
141 #define I40E_GLQF_HASH_INSET(_i, _j) (0x00267600 + ((_i) * 4 + (_j) * 8))
142 #define I40E_GLQF_HASH_MSK(_i, _j)   (0x00267A00 + ((_i) * 4 + (_j) * 8))
143 #define I40E_GLQF_HASH_MSK_FIELD      0x0000FFFF
144
145 #define I40E_INSET_NONE            0x00000000000000000ULL
146
147 /* bit0 ~ bit 7 */
148 #define I40E_INSET_DMAC            0x0000000000000001ULL
149 #define I40E_INSET_SMAC            0x0000000000000002ULL
150 #define I40E_INSET_VLAN_OUTER      0x0000000000000004ULL
151 #define I40E_INSET_VLAN_INNER      0x0000000000000008ULL
152 #define I40E_INSET_VLAN_TUNNEL     0x0000000000000010ULL
153
154 /* bit 8 ~ bit 15 */
155 #define I40E_INSET_IPV4_SRC        0x0000000000000100ULL
156 #define I40E_INSET_IPV4_DST        0x0000000000000200ULL
157 #define I40E_INSET_IPV6_SRC        0x0000000000000400ULL
158 #define I40E_INSET_IPV6_DST        0x0000000000000800ULL
159 #define I40E_INSET_SRC_PORT        0x0000000000001000ULL
160 #define I40E_INSET_DST_PORT        0x0000000000002000ULL
161 #define I40E_INSET_SCTP_VT         0x0000000000004000ULL
162
163 /* bit 16 ~ bit 31 */
164 #define I40E_INSET_IPV4_TOS        0x0000000000010000ULL
165 #define I40E_INSET_IPV4_PROTO      0x0000000000020000ULL
166 #define I40E_INSET_IPV4_TTL        0x0000000000040000ULL
167 #define I40E_INSET_IPV6_TC         0x0000000000080000ULL
168 #define I40E_INSET_IPV6_FLOW       0x0000000000100000ULL
169 #define I40E_INSET_IPV6_NEXT_HDR   0x0000000000200000ULL
170 #define I40E_INSET_IPV6_HOP_LIMIT  0x0000000000400000ULL
171 #define I40E_INSET_TCP_FLAGS       0x0000000000800000ULL
172
173 /* bit 32 ~ bit 47, tunnel fields */
174 #define I40E_INSET_TUNNEL_IPV4_DST       0x0000000100000000ULL
175 #define I40E_INSET_TUNNEL_IPV6_DST       0x0000000200000000ULL
176 #define I40E_INSET_TUNNEL_DMAC           0x0000000400000000ULL
177 #define I40E_INSET_TUNNEL_SRC_PORT       0x0000000800000000ULL
178 #define I40E_INSET_TUNNEL_DST_PORT       0x0000001000000000ULL
179 #define I40E_INSET_TUNNEL_ID             0x0000002000000000ULL
180
181 /* bit 48 ~ bit 55 */
182 #define I40E_INSET_LAST_ETHER_TYPE 0x0001000000000000ULL
183
184 /* bit 56 ~ bit 63, Flex Payload */
185 #define I40E_INSET_FLEX_PAYLOAD_W1 0x0100000000000000ULL
186 #define I40E_INSET_FLEX_PAYLOAD_W2 0x0200000000000000ULL
187 #define I40E_INSET_FLEX_PAYLOAD_W3 0x0400000000000000ULL
188 #define I40E_INSET_FLEX_PAYLOAD_W4 0x0800000000000000ULL
189 #define I40E_INSET_FLEX_PAYLOAD_W5 0x1000000000000000ULL
190 #define I40E_INSET_FLEX_PAYLOAD_W6 0x2000000000000000ULL
191 #define I40E_INSET_FLEX_PAYLOAD_W7 0x4000000000000000ULL
192 #define I40E_INSET_FLEX_PAYLOAD_W8 0x8000000000000000ULL
193 #define I40E_INSET_FLEX_PAYLOAD \
194         (I40E_INSET_FLEX_PAYLOAD_W1 | I40E_INSET_FLEX_PAYLOAD_W2 | \
195         I40E_INSET_FLEX_PAYLOAD_W3 | I40E_INSET_FLEX_PAYLOAD_W3 | \
196         I40E_INSET_FLEX_PAYLOAD_W5 | I40E_INSET_FLEX_PAYLOAD_W6 | \
197         I40E_INSET_FLEX_PAYLOAD_W7 | I40E_INSET_FLEX_PAYLOAD_W8)
198
199 /**
200  * Below are values for writing un-exposed registers suggested
201  * by silicon experts
202  */
203 /* Destination MAC address */
204 #define I40E_REG_INSET_L2_DMAC                   0xE000000000000000ULL
205 /* Source MAC address */
206 #define I40E_REG_INSET_L2_SMAC                   0x1C00000000000000ULL
207 /* VLAN tag in the outer L2 header */
208 #define I40E_REG_INSET_L2_OUTER_VLAN             0x0000000000800000ULL
209 /* VLAN tag in the inner L2 header */
210 #define I40E_REG_INSET_L2_INNER_VLAN             0x0000000001000000ULL
211 /* Source IPv4 address */
212 #define I40E_REG_INSET_L3_SRC_IP4                0x0001800000000000ULL
213 /* Destination IPv4 address */
214 #define I40E_REG_INSET_L3_DST_IP4                0x0000001800000000ULL
215 /* IPv4 Type of Service (TOS) */
216 #define I40E_REG_INSET_L3_IP4_TOS                0x0040000000000000ULL
217 /* IPv4 Protocol */
218 #define I40E_REG_INSET_L3_IP4_PROTO              0x0004000000000000ULL
219 /* Source IPv6 address */
220 #define I40E_REG_INSET_L3_SRC_IP6                0x0007F80000000000ULL
221 /* Destination IPv6 address */
222 #define I40E_REG_INSET_L3_DST_IP6                0x000007F800000000ULL
223 /* IPv6 Traffic Class (TC) */
224 #define I40E_REG_INSET_L3_IP6_TC                 0x0040000000000000ULL
225 /* IPv6 Next Header */
226 #define I40E_REG_INSET_L3_IP6_NEXT_HDR           0x0008000000000000ULL
227 /* Source L4 port */
228 #define I40E_REG_INSET_L4_SRC_PORT               0x0000000400000000ULL
229 /* Destination L4 port */
230 #define I40E_REG_INSET_L4_DST_PORT               0x0000000200000000ULL
231 /* SCTP verification tag */
232 #define I40E_REG_INSET_L4_SCTP_VERIFICATION_TAG  0x0000000180000000ULL
233 /* Inner destination MAC address (MAC-in-UDP/MAC-in-GRE)*/
234 #define I40E_REG_INSET_TUNNEL_L2_INNER_DST_MAC   0x0000000001C00000ULL
235 /* Source port of tunneling UDP */
236 #define I40E_REG_INSET_TUNNEL_L4_UDP_SRC_PORT    0x0000000000200000ULL
237 /* Destination port of tunneling UDP */
238 #define I40E_REG_INSET_TUNNEL_L4_UDP_DST_PORT    0x0000000000100000ULL
239 /* UDP Tunneling ID, NVGRE/GRE key */
240 #define I40E_REG_INSET_TUNNEL_ID                 0x00000000000C0000ULL
241 /* Last ether type */
242 #define I40E_REG_INSET_LAST_ETHER_TYPE           0x0000000000004000ULL
243 /* Tunneling outer destination IPv4 address */
244 #define I40E_REG_INSET_TUNNEL_L3_DST_IP4         0x00000000000000C0ULL
245 /* Tunneling outer destination IPv6 address */
246 #define I40E_REG_INSET_TUNNEL_L3_DST_IP6         0x0000000000003FC0ULL
247 /* 1st word of flex payload */
248 #define I40E_REG_INSET_FLEX_PAYLOAD_WORD1        0x0000000000002000ULL
249 /* 2nd word of flex payload */
250 #define I40E_REG_INSET_FLEX_PAYLOAD_WORD2        0x0000000000001000ULL
251 /* 3rd word of flex payload */
252 #define I40E_REG_INSET_FLEX_PAYLOAD_WORD3        0x0000000000000800ULL
253 /* 4th word of flex payload */
254 #define I40E_REG_INSET_FLEX_PAYLOAD_WORD4        0x0000000000000400ULL
255 /* 5th word of flex payload */
256 #define I40E_REG_INSET_FLEX_PAYLOAD_WORD5        0x0000000000000200ULL
257 /* 6th word of flex payload */
258 #define I40E_REG_INSET_FLEX_PAYLOAD_WORD6        0x0000000000000100ULL
259 /* 7th word of flex payload */
260 #define I40E_REG_INSET_FLEX_PAYLOAD_WORD7        0x0000000000000080ULL
261 /* 8th word of flex payload */
262 #define I40E_REG_INSET_FLEX_PAYLOAD_WORD8        0x0000000000000040ULL
263
264 #define I40E_REG_INSET_MASK_DEFAULT              0x0000000000000000ULL
265
266 #define I40E_TRANSLATE_INSET 0
267 #define I40E_TRANSLATE_REG   1
268
269 #define I40E_INSET_IPV4_TOS_MASK      0x0009FF00UL
270 #define I40E_INSET_IPV4_PROTO_MASK    0x000DFF00UL
271 #define I40E_INSET_IPV6_TC_MASK       0x0009F00FUL
272 #define I40E_INSET_IPV6_NEXT_HDR_MASK 0x000C00FFUL
273
274 static int eth_i40e_dev_init(struct rte_eth_dev *eth_dev);
275 static int eth_i40e_dev_uninit(struct rte_eth_dev *eth_dev);
276 static int i40e_dev_configure(struct rte_eth_dev *dev);
277 static int i40e_dev_start(struct rte_eth_dev *dev);
278 static void i40e_dev_stop(struct rte_eth_dev *dev);
279 static void i40e_dev_close(struct rte_eth_dev *dev);
280 static void i40e_dev_promiscuous_enable(struct rte_eth_dev *dev);
281 static void i40e_dev_promiscuous_disable(struct rte_eth_dev *dev);
282 static void i40e_dev_allmulticast_enable(struct rte_eth_dev *dev);
283 static void i40e_dev_allmulticast_disable(struct rte_eth_dev *dev);
284 static int i40e_dev_set_link_up(struct rte_eth_dev *dev);
285 static int i40e_dev_set_link_down(struct rte_eth_dev *dev);
286 static void i40e_dev_stats_get(struct rte_eth_dev *dev,
287                                struct rte_eth_stats *stats);
288 static int i40e_dev_xstats_get(struct rte_eth_dev *dev,
289                                struct rte_eth_xstats *xstats, unsigned n);
290 static void i40e_dev_stats_reset(struct rte_eth_dev *dev);
291 static int i40e_dev_queue_stats_mapping_set(struct rte_eth_dev *dev,
292                                             uint16_t queue_id,
293                                             uint8_t stat_idx,
294                                             uint8_t is_rx);
295 static void i40e_dev_info_get(struct rte_eth_dev *dev,
296                               struct rte_eth_dev_info *dev_info);
297 static int i40e_vlan_filter_set(struct rte_eth_dev *dev,
298                                 uint16_t vlan_id,
299                                 int on);
300 static void i40e_vlan_tpid_set(struct rte_eth_dev *dev, uint16_t tpid);
301 static void i40e_vlan_offload_set(struct rte_eth_dev *dev, int mask);
302 static void i40e_vlan_strip_queue_set(struct rte_eth_dev *dev,
303                                       uint16_t queue,
304                                       int on);
305 static int i40e_vlan_pvid_set(struct rte_eth_dev *dev, uint16_t pvid, int on);
306 static int i40e_dev_led_on(struct rte_eth_dev *dev);
307 static int i40e_dev_led_off(struct rte_eth_dev *dev);
308 static int i40e_flow_ctrl_get(struct rte_eth_dev *dev,
309                               struct rte_eth_fc_conf *fc_conf);
310 static int i40e_flow_ctrl_set(struct rte_eth_dev *dev,
311                               struct rte_eth_fc_conf *fc_conf);
312 static int i40e_priority_flow_ctrl_set(struct rte_eth_dev *dev,
313                                        struct rte_eth_pfc_conf *pfc_conf);
314 static void i40e_macaddr_add(struct rte_eth_dev *dev,
315                           struct ether_addr *mac_addr,
316                           uint32_t index,
317                           uint32_t pool);
318 static void i40e_macaddr_remove(struct rte_eth_dev *dev, uint32_t index);
319 static int i40e_dev_rss_reta_update(struct rte_eth_dev *dev,
320                                     struct rte_eth_rss_reta_entry64 *reta_conf,
321                                     uint16_t reta_size);
322 static int i40e_dev_rss_reta_query(struct rte_eth_dev *dev,
323                                    struct rte_eth_rss_reta_entry64 *reta_conf,
324                                    uint16_t reta_size);
325
326 static int i40e_get_cap(struct i40e_hw *hw);
327 static int i40e_pf_parameter_init(struct rte_eth_dev *dev);
328 static int i40e_pf_setup(struct i40e_pf *pf);
329 static int i40e_dev_rxtx_init(struct i40e_pf *pf);
330 static int i40e_vmdq_setup(struct rte_eth_dev *dev);
331 static int i40e_dcb_init_configure(struct rte_eth_dev *dev, bool sw_dcb);
332 static int i40e_dcb_setup(struct rte_eth_dev *dev);
333 static void i40e_stat_update_32(struct i40e_hw *hw, uint32_t reg,
334                 bool offset_loaded, uint64_t *offset, uint64_t *stat);
335 static void i40e_stat_update_48(struct i40e_hw *hw,
336                                uint32_t hireg,
337                                uint32_t loreg,
338                                bool offset_loaded,
339                                uint64_t *offset,
340                                uint64_t *stat);
341 static void i40e_pf_config_irq0(struct i40e_hw *hw, bool no_queue);
342 static void i40e_dev_interrupt_handler(
343                 __rte_unused struct rte_intr_handle *handle, void *param);
344 static int i40e_res_pool_init(struct i40e_res_pool_info *pool,
345                                 uint32_t base, uint32_t num);
346 static void i40e_res_pool_destroy(struct i40e_res_pool_info *pool);
347 static int i40e_res_pool_free(struct i40e_res_pool_info *pool,
348                         uint32_t base);
349 static int i40e_res_pool_alloc(struct i40e_res_pool_info *pool,
350                         uint16_t num);
351 static int i40e_dev_init_vlan(struct rte_eth_dev *dev);
352 static int i40e_veb_release(struct i40e_veb *veb);
353 static struct i40e_veb *i40e_veb_setup(struct i40e_pf *pf,
354                                                 struct i40e_vsi *vsi);
355 static int i40e_pf_config_mq_rx(struct i40e_pf *pf);
356 static int i40e_vsi_config_double_vlan(struct i40e_vsi *vsi, int on);
357 static inline int i40e_find_all_vlan_for_mac(struct i40e_vsi *vsi,
358                                              struct i40e_macvlan_filter *mv_f,
359                                              int num,
360                                              struct ether_addr *addr);
361 static inline int i40e_find_all_mac_for_vlan(struct i40e_vsi *vsi,
362                                              struct i40e_macvlan_filter *mv_f,
363                                              int num,
364                                              uint16_t vlan);
365 static int i40e_vsi_remove_all_macvlan_filter(struct i40e_vsi *vsi);
366 static int i40e_dev_rss_hash_update(struct rte_eth_dev *dev,
367                                     struct rte_eth_rss_conf *rss_conf);
368 static int i40e_dev_rss_hash_conf_get(struct rte_eth_dev *dev,
369                                       struct rte_eth_rss_conf *rss_conf);
370 static int i40e_dev_udp_tunnel_add(struct rte_eth_dev *dev,
371                                 struct rte_eth_udp_tunnel *udp_tunnel);
372 static int i40e_dev_udp_tunnel_del(struct rte_eth_dev *dev,
373                                 struct rte_eth_udp_tunnel *udp_tunnel);
374 static int i40e_ethertype_filter_set(struct i40e_pf *pf,
375                         struct rte_eth_ethertype_filter *filter,
376                         bool add);
377 static int i40e_ethertype_filter_handle(struct rte_eth_dev *dev,
378                                 enum rte_filter_op filter_op,
379                                 void *arg);
380 static int i40e_dev_filter_ctrl(struct rte_eth_dev *dev,
381                                 enum rte_filter_type filter_type,
382                                 enum rte_filter_op filter_op,
383                                 void *arg);
384 static int i40e_dev_get_dcb_info(struct rte_eth_dev *dev,
385                                   struct rte_eth_dcb_info *dcb_info);
386 static void i40e_configure_registers(struct i40e_hw *hw);
387 static void i40e_hw_init(struct i40e_hw *hw);
388 static int i40e_config_qinq(struct i40e_hw *hw, struct i40e_vsi *vsi);
389 static int i40e_mirror_rule_set(struct rte_eth_dev *dev,
390                         struct rte_eth_mirror_conf *mirror_conf,
391                         uint8_t sw_id, uint8_t on);
392 static int i40e_mirror_rule_reset(struct rte_eth_dev *dev, uint8_t sw_id);
393
394 static int i40e_timesync_enable(struct rte_eth_dev *dev);
395 static int i40e_timesync_disable(struct rte_eth_dev *dev);
396 static int i40e_timesync_read_rx_timestamp(struct rte_eth_dev *dev,
397                                            struct timespec *timestamp,
398                                            uint32_t flags);
399 static int i40e_timesync_read_tx_timestamp(struct rte_eth_dev *dev,
400                                            struct timespec *timestamp);
401 static void i40e_read_stats_registers(struct i40e_pf *pf, struct i40e_hw *hw);
402 static int i40e_dev_rx_queue_intr_enable(struct rte_eth_dev *dev,
403                                          uint16_t queue_id);
404 static int i40e_dev_rx_queue_intr_disable(struct rte_eth_dev *dev,
405                                           uint16_t queue_id);
406
407 static const struct rte_pci_id pci_id_i40e_map[] = {
408 #define RTE_PCI_DEV_ID_DECL_I40E(vend, dev) {RTE_PCI_DEVICE(vend, dev)},
409 #include "rte_pci_dev_ids.h"
410 { .vendor_id = 0, /* sentinel */ },
411 };
412
413 static const struct eth_dev_ops i40e_eth_dev_ops = {
414         .dev_configure                = i40e_dev_configure,
415         .dev_start                    = i40e_dev_start,
416         .dev_stop                     = i40e_dev_stop,
417         .dev_close                    = i40e_dev_close,
418         .promiscuous_enable           = i40e_dev_promiscuous_enable,
419         .promiscuous_disable          = i40e_dev_promiscuous_disable,
420         .allmulticast_enable          = i40e_dev_allmulticast_enable,
421         .allmulticast_disable         = i40e_dev_allmulticast_disable,
422         .dev_set_link_up              = i40e_dev_set_link_up,
423         .dev_set_link_down            = i40e_dev_set_link_down,
424         .link_update                  = i40e_dev_link_update,
425         .stats_get                    = i40e_dev_stats_get,
426         .xstats_get                   = i40e_dev_xstats_get,
427         .stats_reset                  = i40e_dev_stats_reset,
428         .xstats_reset                 = i40e_dev_stats_reset,
429         .queue_stats_mapping_set      = i40e_dev_queue_stats_mapping_set,
430         .dev_infos_get                = i40e_dev_info_get,
431         .vlan_filter_set              = i40e_vlan_filter_set,
432         .vlan_tpid_set                = i40e_vlan_tpid_set,
433         .vlan_offload_set             = i40e_vlan_offload_set,
434         .vlan_strip_queue_set         = i40e_vlan_strip_queue_set,
435         .vlan_pvid_set                = i40e_vlan_pvid_set,
436         .rx_queue_start               = i40e_dev_rx_queue_start,
437         .rx_queue_stop                = i40e_dev_rx_queue_stop,
438         .tx_queue_start               = i40e_dev_tx_queue_start,
439         .tx_queue_stop                = i40e_dev_tx_queue_stop,
440         .rx_queue_setup               = i40e_dev_rx_queue_setup,
441         .rx_queue_intr_enable         = i40e_dev_rx_queue_intr_enable,
442         .rx_queue_intr_disable        = i40e_dev_rx_queue_intr_disable,
443         .rx_queue_release             = i40e_dev_rx_queue_release,
444         .rx_queue_count               = i40e_dev_rx_queue_count,
445         .rx_descriptor_done           = i40e_dev_rx_descriptor_done,
446         .tx_queue_setup               = i40e_dev_tx_queue_setup,
447         .tx_queue_release             = i40e_dev_tx_queue_release,
448         .dev_led_on                   = i40e_dev_led_on,
449         .dev_led_off                  = i40e_dev_led_off,
450         .flow_ctrl_get                = i40e_flow_ctrl_get,
451         .flow_ctrl_set                = i40e_flow_ctrl_set,
452         .priority_flow_ctrl_set       = i40e_priority_flow_ctrl_set,
453         .mac_addr_add                 = i40e_macaddr_add,
454         .mac_addr_remove              = i40e_macaddr_remove,
455         .reta_update                  = i40e_dev_rss_reta_update,
456         .reta_query                   = i40e_dev_rss_reta_query,
457         .rss_hash_update              = i40e_dev_rss_hash_update,
458         .rss_hash_conf_get            = i40e_dev_rss_hash_conf_get,
459         .udp_tunnel_add               = i40e_dev_udp_tunnel_add,
460         .udp_tunnel_del               = i40e_dev_udp_tunnel_del,
461         .filter_ctrl                  = i40e_dev_filter_ctrl,
462         .rxq_info_get                 = i40e_rxq_info_get,
463         .txq_info_get                 = i40e_txq_info_get,
464         .mirror_rule_set              = i40e_mirror_rule_set,
465         .mirror_rule_reset            = i40e_mirror_rule_reset,
466         .timesync_enable              = i40e_timesync_enable,
467         .timesync_disable             = i40e_timesync_disable,
468         .timesync_read_rx_timestamp   = i40e_timesync_read_rx_timestamp,
469         .timesync_read_tx_timestamp   = i40e_timesync_read_tx_timestamp,
470         .get_dcb_info                 = i40e_dev_get_dcb_info,
471 };
472
473 /* store statistics names and its offset in stats structure */
474 struct rte_i40e_xstats_name_off {
475         char name[RTE_ETH_XSTATS_NAME_SIZE];
476         unsigned offset;
477 };
478
479 static const struct rte_i40e_xstats_name_off rte_i40e_stats_strings[] = {
480         {"rx_unicast_packets", offsetof(struct i40e_eth_stats, rx_unicast)},
481         {"rx_multicast_packets", offsetof(struct i40e_eth_stats, rx_multicast)},
482         {"rx_broadcast_packets", offsetof(struct i40e_eth_stats, rx_broadcast)},
483         {"rx_dropped", offsetof(struct i40e_eth_stats, rx_discards)},
484         {"rx_unknown_protocol_packets", offsetof(struct i40e_eth_stats,
485                 rx_unknown_protocol)},
486         {"tx_unicast_packets", offsetof(struct i40e_eth_stats, tx_unicast)},
487         {"tx_multicast_packets", offsetof(struct i40e_eth_stats, tx_multicast)},
488         {"tx_broadcast_packets", offsetof(struct i40e_eth_stats, tx_broadcast)},
489         {"tx_dropped", offsetof(struct i40e_eth_stats, tx_discards)},
490 };
491
492 static const struct rte_i40e_xstats_name_off rte_i40e_hw_port_strings[] = {
493         {"tx_link_down_dropped", offsetof(struct i40e_hw_port_stats,
494                 tx_dropped_link_down)},
495         {"rx_crc_errors", offsetof(struct i40e_hw_port_stats, crc_errors)},
496         {"rx_illegal_byte_errors", offsetof(struct i40e_hw_port_stats,
497                 illegal_bytes)},
498         {"rx_error_bytes", offsetof(struct i40e_hw_port_stats, error_bytes)},
499         {"mac_local_errors", offsetof(struct i40e_hw_port_stats,
500                 mac_local_faults)},
501         {"mac_remote_errors", offsetof(struct i40e_hw_port_stats,
502                 mac_remote_faults)},
503         {"rx_length_errors", offsetof(struct i40e_hw_port_stats,
504                 rx_length_errors)},
505         {"tx_xon_packets", offsetof(struct i40e_hw_port_stats, link_xon_tx)},
506         {"rx_xon_packets", offsetof(struct i40e_hw_port_stats, link_xon_rx)},
507         {"tx_xoff_packets", offsetof(struct i40e_hw_port_stats, link_xoff_tx)},
508         {"rx_xoff_packets", offsetof(struct i40e_hw_port_stats, link_xoff_rx)},
509         {"rx_size_64_packets", offsetof(struct i40e_hw_port_stats, rx_size_64)},
510         {"rx_size_65_to_127_packets", offsetof(struct i40e_hw_port_stats,
511                 rx_size_127)},
512         {"rx_size_128_to_255_packets", offsetof(struct i40e_hw_port_stats,
513                 rx_size_255)},
514         {"rx_size_256_to_511_packets", offsetof(struct i40e_hw_port_stats,
515                 rx_size_511)},
516         {"rx_size_512_to_1023_packets", offsetof(struct i40e_hw_port_stats,
517                 rx_size_1023)},
518         {"rx_size_1024_to_1522_packets", offsetof(struct i40e_hw_port_stats,
519                 rx_size_1522)},
520         {"rx_size_1523_to_max_packets", offsetof(struct i40e_hw_port_stats,
521                 rx_size_big)},
522         {"rx_undersized_errors", offsetof(struct i40e_hw_port_stats,
523                 rx_undersize)},
524         {"rx_oversize_errors", offsetof(struct i40e_hw_port_stats,
525                 rx_oversize)},
526         {"rx_mac_short_dropped", offsetof(struct i40e_hw_port_stats,
527                 mac_short_packet_dropped)},
528         {"rx_fragmented_errors", offsetof(struct i40e_hw_port_stats,
529                 rx_fragments)},
530         {"rx_jabber_errors", offsetof(struct i40e_hw_port_stats, rx_jabber)},
531         {"tx_size_64_packets", offsetof(struct i40e_hw_port_stats, tx_size_64)},
532         {"tx_size_65_to_127_packets", offsetof(struct i40e_hw_port_stats,
533                 tx_size_127)},
534         {"tx_size_128_to_255_packets", offsetof(struct i40e_hw_port_stats,
535                 tx_size_255)},
536         {"tx_size_256_to_511_packets", offsetof(struct i40e_hw_port_stats,
537                 tx_size_511)},
538         {"tx_size_512_to_1023_packets", offsetof(struct i40e_hw_port_stats,
539                 tx_size_1023)},
540         {"tx_size_1024_to_1522_packets", offsetof(struct i40e_hw_port_stats,
541                 tx_size_1522)},
542         {"tx_size_1523_to_max_packets", offsetof(struct i40e_hw_port_stats,
543                 tx_size_big)},
544         {"rx_flow_director_atr_match_packets",
545                 offsetof(struct i40e_hw_port_stats, fd_atr_match)},
546         {"rx_flow_director_sb_match_packets",
547                 offsetof(struct i40e_hw_port_stats, fd_sb_match)},
548         {"tx_low_power_idle_status", offsetof(struct i40e_hw_port_stats,
549                 tx_lpi_status)},
550         {"rx_low_power_idle_status", offsetof(struct i40e_hw_port_stats,
551                 rx_lpi_status)},
552         {"tx_low_power_idle_count", offsetof(struct i40e_hw_port_stats,
553                 tx_lpi_count)},
554         {"rx_low_power_idle_count", offsetof(struct i40e_hw_port_stats,
555                 rx_lpi_count)},
556 };
557
558 /* Q Stats: 5 stats are exposed for each queue, implemented in xstats_get() */
559 #define I40E_NB_HW_PORT_Q_STATS (8 * 5)
560
561 #define I40E_NB_ETH_XSTATS (sizeof(rte_i40e_stats_strings) / \
562                 sizeof(rte_i40e_stats_strings[0]))
563 #define I40E_NB_HW_PORT_XSTATS (sizeof(rte_i40e_hw_port_strings) / \
564                 sizeof(rte_i40e_hw_port_strings[0]))
565 #define I40E_NB_XSTATS (I40E_NB_ETH_XSTATS + I40E_NB_HW_PORT_XSTATS + \
566                 I40E_NB_HW_PORT_Q_STATS)
567
568 static struct eth_driver rte_i40e_pmd = {
569         .pci_drv = {
570                 .name = "rte_i40e_pmd",
571                 .id_table = pci_id_i40e_map,
572                 .drv_flags = RTE_PCI_DRV_NEED_MAPPING | RTE_PCI_DRV_INTR_LSC |
573                         RTE_PCI_DRV_DETACHABLE,
574         },
575         .eth_dev_init = eth_i40e_dev_init,
576         .eth_dev_uninit = eth_i40e_dev_uninit,
577         .dev_private_size = sizeof(struct i40e_adapter),
578 };
579
580 static inline int
581 rte_i40e_dev_atomic_read_link_status(struct rte_eth_dev *dev,
582                                      struct rte_eth_link *link)
583 {
584         struct rte_eth_link *dst = link;
585         struct rte_eth_link *src = &(dev->data->dev_link);
586
587         if (rte_atomic64_cmpset((uint64_t *)dst, *(uint64_t *)dst,
588                                         *(uint64_t *)src) == 0)
589                 return -1;
590
591         return 0;
592 }
593
594 static inline int
595 rte_i40e_dev_atomic_write_link_status(struct rte_eth_dev *dev,
596                                       struct rte_eth_link *link)
597 {
598         struct rte_eth_link *dst = &(dev->data->dev_link);
599         struct rte_eth_link *src = link;
600
601         if (rte_atomic64_cmpset((uint64_t *)dst, *(uint64_t *)dst,
602                                         *(uint64_t *)src) == 0)
603                 return -1;
604
605         return 0;
606 }
607
608 /*
609  * Driver initialization routine.
610  * Invoked once at EAL init time.
611  * Register itself as the [Poll Mode] Driver of PCI IXGBE devices.
612  */
613 static int
614 rte_i40e_pmd_init(const char *name __rte_unused,
615                   const char *params __rte_unused)
616 {
617         PMD_INIT_FUNC_TRACE();
618         rte_eth_driver_register(&rte_i40e_pmd);
619
620         return 0;
621 }
622
623 static struct rte_driver rte_i40e_driver = {
624         .type = PMD_PDEV,
625         .init = rte_i40e_pmd_init,
626 };
627
628 PMD_REGISTER_DRIVER(rte_i40e_driver);
629
630 /*
631  * Initialize registers for flexible payload, which should be set by NVM.
632  * This should be removed from code once it is fixed in NVM.
633  */
634 #ifndef I40E_GLQF_ORT
635 #define I40E_GLQF_ORT(_i)    (0x00268900 + ((_i) * 4))
636 #endif
637 #ifndef I40E_GLQF_PIT
638 #define I40E_GLQF_PIT(_i)    (0x00268C80 + ((_i) * 4))
639 #endif
640
641 static inline void i40e_flex_payload_reg_init(struct i40e_hw *hw)
642 {
643         I40E_WRITE_REG(hw, I40E_GLQF_ORT(18), 0x00000030);
644         I40E_WRITE_REG(hw, I40E_GLQF_ORT(19), 0x00000030);
645         I40E_WRITE_REG(hw, I40E_GLQF_ORT(26), 0x0000002B);
646         I40E_WRITE_REG(hw, I40E_GLQF_ORT(30), 0x0000002B);
647         I40E_WRITE_REG(hw, I40E_GLQF_ORT(33), 0x000000E0);
648         I40E_WRITE_REG(hw, I40E_GLQF_ORT(34), 0x000000E3);
649         I40E_WRITE_REG(hw, I40E_GLQF_ORT(35), 0x000000E6);
650         I40E_WRITE_REG(hw, I40E_GLQF_ORT(20), 0x00000031);
651         I40E_WRITE_REG(hw, I40E_GLQF_ORT(23), 0x00000031);
652         I40E_WRITE_REG(hw, I40E_GLQF_ORT(63), 0x0000002D);
653
654         /* GLQF_PIT Registers */
655         I40E_WRITE_REG(hw, I40E_GLQF_PIT(16), 0x00007480);
656         I40E_WRITE_REG(hw, I40E_GLQF_PIT(17), 0x00007440);
657 }
658
659 #define I40E_FLOW_CONTROL_ETHERTYPE  0x8808
660
661 /*
662  * Add a ethertype filter to drop all flow control frames transmitted
663  * from VSIs.
664 */
665 static void
666 i40e_add_tx_flow_control_drop_filter(struct i40e_pf *pf)
667 {
668         struct i40e_hw *hw = I40E_PF_TO_HW(pf);
669         uint16_t flags = I40E_AQC_ADD_CONTROL_PACKET_FLAGS_IGNORE_MAC |
670                         I40E_AQC_ADD_CONTROL_PACKET_FLAGS_DROP |
671                         I40E_AQC_ADD_CONTROL_PACKET_FLAGS_TX;
672         int ret;
673
674         ret = i40e_aq_add_rem_control_packet_filter(hw, NULL,
675                                 I40E_FLOW_CONTROL_ETHERTYPE, flags,
676                                 pf->main_vsi_seid, 0,
677                                 TRUE, NULL, NULL);
678         if (ret)
679                 PMD_INIT_LOG(ERR, "Failed to add filter to drop flow control "
680                                   " frames from VSIs.");
681 }
682
683 static int
684 eth_i40e_dev_init(struct rte_eth_dev *dev)
685 {
686         struct rte_pci_device *pci_dev;
687         struct i40e_pf *pf = I40E_DEV_PRIVATE_TO_PF(dev->data->dev_private);
688         struct i40e_hw *hw = I40E_DEV_PRIVATE_TO_HW(dev->data->dev_private);
689         struct i40e_vsi *vsi;
690         int ret;
691         uint32_t len;
692         uint8_t aq_fail = 0;
693
694         PMD_INIT_FUNC_TRACE();
695
696         dev->dev_ops = &i40e_eth_dev_ops;
697         dev->rx_pkt_burst = i40e_recv_pkts;
698         dev->tx_pkt_burst = i40e_xmit_pkts;
699
700         /* for secondary processes, we don't initialise any further as primary
701          * has already done this work. Only check we don't need a different
702          * RX function */
703         if (rte_eal_process_type() != RTE_PROC_PRIMARY){
704                 i40e_set_rx_function(dev);
705                 i40e_set_tx_function(dev);
706                 return 0;
707         }
708         pci_dev = dev->pci_dev;
709
710         rte_eth_copy_pci_info(dev, pci_dev);
711
712         pf->adapter = I40E_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
713         pf->adapter->eth_dev = dev;
714         pf->dev_data = dev->data;
715
716         hw->back = I40E_PF_TO_ADAPTER(pf);
717         hw->hw_addr = (uint8_t *)(pci_dev->mem_resource[0].addr);
718         if (!hw->hw_addr) {
719                 PMD_INIT_LOG(ERR, "Hardware is not available, "
720                              "as address is NULL");
721                 return -ENODEV;
722         }
723
724         hw->vendor_id = pci_dev->id.vendor_id;
725         hw->device_id = pci_dev->id.device_id;
726         hw->subsystem_vendor_id = pci_dev->id.subsystem_vendor_id;
727         hw->subsystem_device_id = pci_dev->id.subsystem_device_id;
728         hw->bus.device = pci_dev->addr.devid;
729         hw->bus.func = pci_dev->addr.function;
730         hw->adapter_stopped = 0;
731
732         /* Make sure all is clean before doing PF reset */
733         i40e_clear_hw(hw);
734
735         /* Initialize the hardware */
736         i40e_hw_init(hw);
737
738         /* Reset here to make sure all is clean for each PF */
739         ret = i40e_pf_reset(hw);
740         if (ret) {
741                 PMD_INIT_LOG(ERR, "Failed to reset pf: %d", ret);
742                 return ret;
743         }
744
745         /* Initialize the shared code (base driver) */
746         ret = i40e_init_shared_code(hw);
747         if (ret) {
748                 PMD_INIT_LOG(ERR, "Failed to init shared code (base driver): %d", ret);
749                 return ret;
750         }
751
752         /*
753          * To work around the NVM issue,initialize registers
754          * for flexible payload by software.
755          * It should be removed once issues are fixed in NVM.
756          */
757         i40e_flex_payload_reg_init(hw);
758
759         /* Initialize the parameters for adminq */
760         i40e_init_adminq_parameter(hw);
761         ret = i40e_init_adminq(hw);
762         if (ret != I40E_SUCCESS) {
763                 PMD_INIT_LOG(ERR, "Failed to init adminq: %d", ret);
764                 return -EIO;
765         }
766         PMD_INIT_LOG(INFO, "FW %d.%d API %d.%d NVM %02d.%02d.%02d eetrack %04x",
767                      hw->aq.fw_maj_ver, hw->aq.fw_min_ver,
768                      hw->aq.api_maj_ver, hw->aq.api_min_ver,
769                      ((hw->nvm.version >> 12) & 0xf),
770                      ((hw->nvm.version >> 4) & 0xff),
771                      (hw->nvm.version & 0xf), hw->nvm.eetrack);
772
773         /* Clear PXE mode */
774         i40e_clear_pxe_mode(hw);
775
776         /*
777          * On X710, performance number is far from the expectation on recent
778          * firmware versions. The fix for this issue may not be integrated in
779          * the following firmware version. So the workaround in software driver
780          * is needed. It needs to modify the initial values of 3 internal only
781          * registers. Note that the workaround can be removed when it is fixed
782          * in firmware in the future.
783          */
784         i40e_configure_registers(hw);
785
786         /* Get hw capabilities */
787         ret = i40e_get_cap(hw);
788         if (ret != I40E_SUCCESS) {
789                 PMD_INIT_LOG(ERR, "Failed to get capabilities: %d", ret);
790                 goto err_get_capabilities;
791         }
792
793         /* Initialize parameters for PF */
794         ret = i40e_pf_parameter_init(dev);
795         if (ret != 0) {
796                 PMD_INIT_LOG(ERR, "Failed to do parameter init: %d", ret);
797                 goto err_parameter_init;
798         }
799
800         /* Initialize the queue management */
801         ret = i40e_res_pool_init(&pf->qp_pool, 0, hw->func_caps.num_tx_qp);
802         if (ret < 0) {
803                 PMD_INIT_LOG(ERR, "Failed to init queue pool");
804                 goto err_qp_pool_init;
805         }
806         ret = i40e_res_pool_init(&pf->msix_pool, 1,
807                                 hw->func_caps.num_msix_vectors - 1);
808         if (ret < 0) {
809                 PMD_INIT_LOG(ERR, "Failed to init MSIX pool");
810                 goto err_msix_pool_init;
811         }
812
813         /* Initialize lan hmc */
814         ret = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
815                                 hw->func_caps.num_rx_qp, 0, 0);
816         if (ret != I40E_SUCCESS) {
817                 PMD_INIT_LOG(ERR, "Failed to init lan hmc: %d", ret);
818                 goto err_init_lan_hmc;
819         }
820
821         /* Configure lan hmc */
822         ret = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
823         if (ret != I40E_SUCCESS) {
824                 PMD_INIT_LOG(ERR, "Failed to configure lan hmc: %d", ret);
825                 goto err_configure_lan_hmc;
826         }
827
828         /* Get and check the mac address */
829         i40e_get_mac_addr(hw, hw->mac.addr);
830         if (i40e_validate_mac_addr(hw->mac.addr) != I40E_SUCCESS) {
831                 PMD_INIT_LOG(ERR, "mac address is not valid");
832                 ret = -EIO;
833                 goto err_get_mac_addr;
834         }
835         /* Copy the permanent MAC address */
836         ether_addr_copy((struct ether_addr *) hw->mac.addr,
837                         (struct ether_addr *) hw->mac.perm_addr);
838
839         /* Disable flow control */
840         hw->fc.requested_mode = I40E_FC_NONE;
841         i40e_set_fc(hw, &aq_fail, TRUE);
842
843         /* PF setup, which includes VSI setup */
844         ret = i40e_pf_setup(pf);
845         if (ret) {
846                 PMD_INIT_LOG(ERR, "Failed to setup pf switch: %d", ret);
847                 goto err_setup_pf_switch;
848         }
849
850         vsi = pf->main_vsi;
851
852         /* Disable double vlan by default */
853         i40e_vsi_config_double_vlan(vsi, FALSE);
854
855         if (!vsi->max_macaddrs)
856                 len = ETHER_ADDR_LEN;
857         else
858                 len = ETHER_ADDR_LEN * vsi->max_macaddrs;
859
860         /* Should be after VSI initialized */
861         dev->data->mac_addrs = rte_zmalloc("i40e", len, 0);
862         if (!dev->data->mac_addrs) {
863                 PMD_INIT_LOG(ERR, "Failed to allocated memory "
864                                         "for storing mac address");
865                 goto err_mac_alloc;
866         }
867         ether_addr_copy((struct ether_addr *)hw->mac.perm_addr,
868                                         &dev->data->mac_addrs[0]);
869
870         /* initialize pf host driver to setup SRIOV resource if applicable */
871         i40e_pf_host_init(dev);
872
873         /* register callback func to eal lib */
874         rte_intr_callback_register(&(pci_dev->intr_handle),
875                 i40e_dev_interrupt_handler, (void *)dev);
876
877         /* configure and enable device interrupt */
878         i40e_pf_config_irq0(hw, TRUE);
879         i40e_pf_enable_irq0(hw);
880
881         /* enable uio intr after callback register */
882         rte_intr_enable(&(pci_dev->intr_handle));
883         /*
884          * Add an ethertype filter to drop all flow control frames transmitted
885          * from VSIs. By doing so, we stop VF from sending out PAUSE or PFC
886          * frames to wire.
887          */
888         i40e_add_tx_flow_control_drop_filter(pf);
889
890         /* initialize mirror rule list */
891         TAILQ_INIT(&pf->mirror_list);
892
893         /* Init dcb to sw mode by default */
894         ret = i40e_dcb_init_configure(dev, TRUE);
895         if (ret != I40E_SUCCESS) {
896                 PMD_INIT_LOG(INFO, "Failed to init dcb.");
897                 pf->flags &= ~I40E_FLAG_DCB;
898         }
899
900         return 0;
901
902 err_mac_alloc:
903         i40e_vsi_release(pf->main_vsi);
904 err_setup_pf_switch:
905 err_get_mac_addr:
906 err_configure_lan_hmc:
907         (void)i40e_shutdown_lan_hmc(hw);
908 err_init_lan_hmc:
909         i40e_res_pool_destroy(&pf->msix_pool);
910 err_msix_pool_init:
911         i40e_res_pool_destroy(&pf->qp_pool);
912 err_qp_pool_init:
913 err_parameter_init:
914 err_get_capabilities:
915         (void)i40e_shutdown_adminq(hw);
916
917         return ret;
918 }
919
920 static int
921 eth_i40e_dev_uninit(struct rte_eth_dev *dev)
922 {
923         struct rte_pci_device *pci_dev;
924         struct i40e_hw *hw;
925         struct i40e_filter_control_settings settings;
926         int ret;
927         uint8_t aq_fail = 0;
928
929         PMD_INIT_FUNC_TRACE();
930
931         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
932                 return 0;
933
934         hw = I40E_DEV_PRIVATE_TO_HW(dev->data->dev_private);
935         pci_dev = dev->pci_dev;
936
937         if (hw->adapter_stopped == 0)
938                 i40e_dev_close(dev);
939
940         dev->dev_ops = NULL;
941         dev->rx_pkt_burst = NULL;
942         dev->tx_pkt_burst = NULL;
943
944         /* Disable LLDP */
945         ret = i40e_aq_stop_lldp(hw, true, NULL);
946         if (ret != I40E_SUCCESS) /* Its failure can be ignored */
947                 PMD_INIT_LOG(INFO, "Failed to stop lldp");
948
949         /* Clear PXE mode */
950         i40e_clear_pxe_mode(hw);
951
952         /* Unconfigure filter control */
953         memset(&settings, 0, sizeof(settings));
954         ret = i40e_set_filter_control(hw, &settings);
955         if (ret)
956                 PMD_INIT_LOG(WARNING, "setup_pf_filter_control failed: %d",
957                                         ret);
958
959         /* Disable flow control */
960         hw->fc.requested_mode = I40E_FC_NONE;
961         i40e_set_fc(hw, &aq_fail, TRUE);
962
963         /* uninitialize pf host driver */
964         i40e_pf_host_uninit(dev);
965
966         rte_free(dev->data->mac_addrs);
967         dev->data->mac_addrs = NULL;
968
969         /* disable uio intr before callback unregister */
970         rte_intr_disable(&(pci_dev->intr_handle));
971
972         /* register callback func to eal lib */
973         rte_intr_callback_unregister(&(pci_dev->intr_handle),
974                 i40e_dev_interrupt_handler, (void *)dev);
975
976         return 0;
977 }
978
979 static int
980 i40e_dev_configure(struct rte_eth_dev *dev)
981 {
982         struct i40e_adapter *ad =
983                 I40E_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
984         struct i40e_pf *pf = I40E_DEV_PRIVATE_TO_PF(dev->data->dev_private);
985         enum rte_eth_rx_mq_mode mq_mode = dev->data->dev_conf.rxmode.mq_mode;
986         int i, ret;
987
988         /* Initialize to TRUE. If any of Rx queues doesn't meet the
989          * bulk allocation or vector Rx preconditions we will reset it.
990          */
991         ad->rx_bulk_alloc_allowed = true;
992         ad->rx_vec_allowed = true;
993         ad->tx_simple_allowed = true;
994         ad->tx_vec_allowed = true;
995
996         if (dev->data->dev_conf.fdir_conf.mode == RTE_FDIR_MODE_PERFECT) {
997                 ret = i40e_fdir_setup(pf);
998                 if (ret != I40E_SUCCESS) {
999                         PMD_DRV_LOG(ERR, "Failed to setup flow director.");
1000                         return -ENOTSUP;
1001                 }
1002                 ret = i40e_fdir_configure(dev);
1003                 if (ret < 0) {
1004                         PMD_DRV_LOG(ERR, "failed to configure fdir.");
1005                         goto err;
1006                 }
1007         } else
1008                 i40e_fdir_teardown(pf);
1009
1010         ret = i40e_dev_init_vlan(dev);
1011         if (ret < 0)
1012                 goto err;
1013
1014         /* VMDQ setup.
1015          *  Needs to move VMDQ setting out of i40e_pf_config_mq_rx() as VMDQ and
1016          *  RSS setting have different requirements.
1017          *  General PMD driver call sequence are NIC init, configure,
1018          *  rx/tx_queue_setup and dev_start. In rx/tx_queue_setup() function, it
1019          *  will try to lookup the VSI that specific queue belongs to if VMDQ
1020          *  applicable. So, VMDQ setting has to be done before
1021          *  rx/tx_queue_setup(). This function is good  to place vmdq_setup.
1022          *  For RSS setting, it will try to calculate actual configured RX queue
1023          *  number, which will be available after rx_queue_setup(). dev_start()
1024          *  function is good to place RSS setup.
1025          */
1026         if (mq_mode & ETH_MQ_RX_VMDQ_FLAG) {
1027                 ret = i40e_vmdq_setup(dev);
1028                 if (ret)
1029                         goto err;
1030         }
1031
1032         if (mq_mode & ETH_MQ_RX_DCB_FLAG) {
1033                 ret = i40e_dcb_setup(dev);
1034                 if (ret) {
1035                         PMD_DRV_LOG(ERR, "failed to configure DCB.");
1036                         goto err_dcb;
1037                 }
1038         }
1039
1040         return 0;
1041
1042 err_dcb:
1043         /* need to release vmdq resource if exists */
1044         for (i = 0; i < pf->nb_cfg_vmdq_vsi; i++) {
1045                 i40e_vsi_release(pf->vmdq[i].vsi);
1046                 pf->vmdq[i].vsi = NULL;
1047         }
1048         rte_free(pf->vmdq);
1049         pf->vmdq = NULL;
1050 err:
1051         /* need to release fdir resource if exists */
1052         i40e_fdir_teardown(pf);
1053         return ret;
1054 }
1055
1056 void
1057 i40e_vsi_queues_unbind_intr(struct i40e_vsi *vsi)
1058 {
1059         struct rte_eth_dev *dev = vsi->adapter->eth_dev;
1060         struct rte_intr_handle *intr_handle = &dev->pci_dev->intr_handle;
1061         struct i40e_hw *hw = I40E_VSI_TO_HW(vsi);
1062         uint16_t msix_vect = vsi->msix_intr;
1063         uint16_t i;
1064
1065         for (i = 0; i < vsi->nb_qps; i++) {
1066                 I40E_WRITE_REG(hw, I40E_QINT_TQCTL(vsi->base_queue + i), 0);
1067                 I40E_WRITE_REG(hw, I40E_QINT_RQCTL(vsi->base_queue + i), 0);
1068                 rte_wmb();
1069         }
1070
1071         if (vsi->type != I40E_VSI_SRIOV) {
1072                 if (!rte_intr_allow_others(intr_handle)) {
1073                         I40E_WRITE_REG(hw, I40E_PFINT_LNKLST0,
1074                                        I40E_PFINT_LNKLST0_FIRSTQ_INDX_MASK);
1075                         I40E_WRITE_REG(hw,
1076                                        I40E_PFINT_ITR0(I40E_ITR_INDEX_DEFAULT),
1077                                        0);
1078                 } else {
1079                         I40E_WRITE_REG(hw, I40E_PFINT_LNKLSTN(msix_vect - 1),
1080                                        I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK);
1081                         I40E_WRITE_REG(hw,
1082                                        I40E_PFINT_ITRN(I40E_ITR_INDEX_DEFAULT,
1083                                                        msix_vect - 1), 0);
1084                 }
1085         } else {
1086                 uint32_t reg;
1087                 reg = (hw->func_caps.num_msix_vectors_vf - 1) *
1088                         vsi->user_param + (msix_vect - 1);
1089
1090                 I40E_WRITE_REG(hw, I40E_VPINT_LNKLSTN(reg),
1091                                I40E_VPINT_LNKLSTN_FIRSTQ_INDX_MASK);
1092         }
1093         I40E_WRITE_FLUSH(hw);
1094 }
1095
1096 static void
1097 __vsi_queues_bind_intr(struct i40e_vsi *vsi, uint16_t msix_vect,
1098                        int base_queue, int nb_queue)
1099 {
1100         int i;
1101         uint32_t val;
1102         struct i40e_hw *hw = I40E_VSI_TO_HW(vsi);
1103
1104         /* Bind all RX queues to allocated MSIX interrupt */
1105         for (i = 0; i < nb_queue; i++) {
1106                 val = (msix_vect << I40E_QINT_RQCTL_MSIX_INDX_SHIFT) |
1107                         I40E_QINT_RQCTL_ITR_INDX_MASK |
1108                         ((base_queue + i + 1) <<
1109                          I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT) |
1110                         (0 << I40E_QINT_RQCTL_NEXTQ_TYPE_SHIFT) |
1111                         I40E_QINT_RQCTL_CAUSE_ENA_MASK;
1112
1113                 if (i == nb_queue - 1)
1114                         val |= I40E_QINT_RQCTL_NEXTQ_INDX_MASK;
1115                 I40E_WRITE_REG(hw, I40E_QINT_RQCTL(base_queue + i), val);
1116         }
1117
1118         /* Write first RX queue to Link list register as the head element */
1119         if (vsi->type != I40E_VSI_SRIOV) {
1120                 uint16_t interval =
1121                         i40e_calc_itr_interval(RTE_LIBRTE_I40E_ITR_INTERVAL);
1122
1123                 if (msix_vect == I40E_MISC_VEC_ID) {
1124                         I40E_WRITE_REG(hw, I40E_PFINT_LNKLST0,
1125                                        (base_queue <<
1126                                         I40E_PFINT_LNKLST0_FIRSTQ_INDX_SHIFT) |
1127                                        (0x0 <<
1128                                         I40E_PFINT_LNKLST0_FIRSTQ_TYPE_SHIFT));
1129                         I40E_WRITE_REG(hw,
1130                                        I40E_PFINT_ITR0(I40E_ITR_INDEX_DEFAULT),
1131                                        interval);
1132                 } else {
1133                         I40E_WRITE_REG(hw, I40E_PFINT_LNKLSTN(msix_vect - 1),
1134                                        (base_queue <<
1135                                         I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT) |
1136                                        (0x0 <<
1137                                         I40E_PFINT_LNKLSTN_FIRSTQ_TYPE_SHIFT));
1138                         I40E_WRITE_REG(hw,
1139                                        I40E_PFINT_ITRN(I40E_ITR_INDEX_DEFAULT,
1140                                                        msix_vect - 1),
1141                                        interval);
1142                 }
1143         } else {
1144                 uint32_t reg;
1145
1146                 if (msix_vect == I40E_MISC_VEC_ID) {
1147                         I40E_WRITE_REG(hw,
1148                                        I40E_VPINT_LNKLST0(vsi->user_param),
1149                                        (base_queue <<
1150                                         I40E_VPINT_LNKLST0_FIRSTQ_INDX_SHIFT) |
1151                                        (0x0 <<
1152                                         I40E_VPINT_LNKLST0_FIRSTQ_TYPE_SHIFT));
1153                 } else {
1154                         /* num_msix_vectors_vf needs to minus irq0 */
1155                         reg = (hw->func_caps.num_msix_vectors_vf - 1) *
1156                                 vsi->user_param + (msix_vect - 1);
1157
1158                         I40E_WRITE_REG(hw, I40E_VPINT_LNKLSTN(reg),
1159                                        (base_queue <<
1160                                         I40E_VPINT_LNKLSTN_FIRSTQ_INDX_SHIFT) |
1161                                        (0x0 <<
1162                                         I40E_VPINT_LNKLSTN_FIRSTQ_TYPE_SHIFT));
1163                 }
1164         }
1165
1166         I40E_WRITE_FLUSH(hw);
1167 }
1168
1169 void
1170 i40e_vsi_queues_bind_intr(struct i40e_vsi *vsi)
1171 {
1172         struct rte_eth_dev *dev = vsi->adapter->eth_dev;
1173         struct rte_intr_handle *intr_handle = &dev->pci_dev->intr_handle;
1174         struct i40e_hw *hw = I40E_VSI_TO_HW(vsi);
1175         uint16_t msix_vect = vsi->msix_intr;
1176         uint16_t nb_msix = RTE_MIN(vsi->nb_msix, intr_handle->nb_efd);
1177         uint16_t queue_idx = 0;
1178         int record = 0;
1179         uint32_t val;
1180         int i;
1181
1182         for (i = 0; i < vsi->nb_qps; i++) {
1183                 I40E_WRITE_REG(hw, I40E_QINT_TQCTL(vsi->base_queue + i), 0);
1184                 I40E_WRITE_REG(hw, I40E_QINT_RQCTL(vsi->base_queue + i), 0);
1185         }
1186
1187         /* INTENA flag is not auto-cleared for interrupt */
1188         val = I40E_READ_REG(hw, I40E_GLINT_CTL);
1189         val |= I40E_GLINT_CTL_DIS_AUTOMASK_PF0_MASK |
1190                 I40E_GLINT_CTL_DIS_AUTOMASK_N_MASK |
1191                 I40E_GLINT_CTL_DIS_AUTOMASK_VF0_MASK;
1192         I40E_WRITE_REG(hw, I40E_GLINT_CTL, val);
1193
1194         /* VF bind interrupt */
1195         if (vsi->type == I40E_VSI_SRIOV) {
1196                 __vsi_queues_bind_intr(vsi, msix_vect,
1197                                        vsi->base_queue, vsi->nb_qps);
1198                 return;
1199         }
1200
1201         /* PF & VMDq bind interrupt */
1202         if (rte_intr_dp_is_en(intr_handle)) {
1203                 if (vsi->type == I40E_VSI_MAIN) {
1204                         queue_idx = 0;
1205                         record = 1;
1206                 } else if (vsi->type == I40E_VSI_VMDQ2) {
1207                         struct i40e_vsi *main_vsi =
1208                                 I40E_DEV_PRIVATE_TO_MAIN_VSI(vsi->adapter);
1209                         queue_idx = vsi->base_queue - main_vsi->nb_qps;
1210                         record = 1;
1211                 }
1212         }
1213
1214         for (i = 0; i < vsi->nb_used_qps; i++) {
1215                 if (nb_msix <= 1) {
1216                         if (!rte_intr_allow_others(intr_handle))
1217                                 /* allow to share MISC_VEC_ID */
1218                                 msix_vect = I40E_MISC_VEC_ID;
1219
1220                         /* no enough msix_vect, map all to one */
1221                         __vsi_queues_bind_intr(vsi, msix_vect,
1222                                                vsi->base_queue + i,
1223                                                vsi->nb_used_qps - i);
1224                         for (; !!record && i < vsi->nb_used_qps; i++)
1225                                 intr_handle->intr_vec[queue_idx + i] =
1226                                         msix_vect;
1227                         break;
1228                 }
1229                 /* 1:1 queue/msix_vect mapping */
1230                 __vsi_queues_bind_intr(vsi, msix_vect,
1231                                        vsi->base_queue + i, 1);
1232                 if (!!record)
1233                         intr_handle->intr_vec[queue_idx + i] = msix_vect;
1234
1235                 msix_vect++;
1236                 nb_msix--;
1237         }
1238 }
1239
1240 static void
1241 i40e_vsi_enable_queues_intr(struct i40e_vsi *vsi)
1242 {
1243         struct rte_eth_dev *dev = vsi->adapter->eth_dev;
1244         struct rte_intr_handle *intr_handle = &dev->pci_dev->intr_handle;
1245         struct i40e_hw *hw = I40E_VSI_TO_HW(vsi);
1246         uint16_t interval = i40e_calc_itr_interval(\
1247                 RTE_LIBRTE_I40E_ITR_INTERVAL);
1248         uint16_t msix_intr, i;
1249
1250         if (rte_intr_allow_others(intr_handle))
1251                 for (i = 0; i < vsi->nb_msix; i++) {
1252                         msix_intr = vsi->msix_intr + i;
1253                         I40E_WRITE_REG(hw, I40E_PFINT_DYN_CTLN(msix_intr - 1),
1254                                 I40E_PFINT_DYN_CTLN_INTENA_MASK |
1255                                 I40E_PFINT_DYN_CTLN_CLEARPBA_MASK |
1256                                 (0 << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT) |
1257                                 (interval <<
1258                                  I40E_PFINT_DYN_CTLN_INTERVAL_SHIFT));
1259                 }
1260         else
1261                 I40E_WRITE_REG(hw, I40E_PFINT_DYN_CTL0,
1262                                I40E_PFINT_DYN_CTL0_INTENA_MASK |
1263                                I40E_PFINT_DYN_CTL0_CLEARPBA_MASK |
1264                                (0 << I40E_PFINT_DYN_CTL0_ITR_INDX_SHIFT) |
1265                                (interval <<
1266                                 I40E_PFINT_DYN_CTL0_INTERVAL_SHIFT));
1267
1268         I40E_WRITE_FLUSH(hw);
1269 }
1270
1271 static void
1272 i40e_vsi_disable_queues_intr(struct i40e_vsi *vsi)
1273 {
1274         struct rte_eth_dev *dev = vsi->adapter->eth_dev;
1275         struct rte_intr_handle *intr_handle = &dev->pci_dev->intr_handle;
1276         struct i40e_hw *hw = I40E_VSI_TO_HW(vsi);
1277         uint16_t msix_intr, i;
1278
1279         if (rte_intr_allow_others(intr_handle))
1280                 for (i = 0; i < vsi->nb_msix; i++) {
1281                         msix_intr = vsi->msix_intr + i;
1282                         I40E_WRITE_REG(hw, I40E_PFINT_DYN_CTLN(msix_intr - 1),
1283                                        0);
1284                 }
1285         else
1286                 I40E_WRITE_REG(hw, I40E_PFINT_DYN_CTL0, 0);
1287
1288         I40E_WRITE_FLUSH(hw);
1289 }
1290
1291 static inline uint8_t
1292 i40e_parse_link_speed(uint16_t eth_link_speed)
1293 {
1294         uint8_t link_speed = I40E_LINK_SPEED_UNKNOWN;
1295
1296         switch (eth_link_speed) {
1297         case ETH_LINK_SPEED_40G:
1298                 link_speed = I40E_LINK_SPEED_40GB;
1299                 break;
1300         case ETH_LINK_SPEED_20G:
1301                 link_speed = I40E_LINK_SPEED_20GB;
1302                 break;
1303         case ETH_LINK_SPEED_10G:
1304                 link_speed = I40E_LINK_SPEED_10GB;
1305                 break;
1306         case ETH_LINK_SPEED_1000:
1307                 link_speed = I40E_LINK_SPEED_1GB;
1308                 break;
1309         case ETH_LINK_SPEED_100:
1310                 link_speed = I40E_LINK_SPEED_100MB;
1311                 break;
1312         }
1313
1314         return link_speed;
1315 }
1316
1317 static int
1318 i40e_phy_conf_link(struct i40e_hw *hw, uint8_t abilities, uint8_t force_speed)
1319 {
1320         enum i40e_status_code status;
1321         struct i40e_aq_get_phy_abilities_resp phy_ab;
1322         struct i40e_aq_set_phy_config phy_conf;
1323         const uint8_t mask = I40E_AQ_PHY_FLAG_PAUSE_TX |
1324                         I40E_AQ_PHY_FLAG_PAUSE_RX |
1325                         I40E_AQ_PHY_FLAG_LOW_POWER;
1326         const uint8_t advt = I40E_LINK_SPEED_40GB |
1327                         I40E_LINK_SPEED_10GB |
1328                         I40E_LINK_SPEED_1GB |
1329                         I40E_LINK_SPEED_100MB;
1330         int ret = -ENOTSUP;
1331
1332         /* Skip it on 40G interfaces, as a workaround for the link issue */
1333         if (i40e_is_40G_device(hw->device_id))
1334                 return I40E_SUCCESS;
1335
1336         status = i40e_aq_get_phy_capabilities(hw, false, false, &phy_ab,
1337                                               NULL);
1338         if (status)
1339                 return ret;
1340
1341         memset(&phy_conf, 0, sizeof(phy_conf));
1342
1343         /* bits 0-2 use the values from get_phy_abilities_resp */
1344         abilities &= ~mask;
1345         abilities |= phy_ab.abilities & mask;
1346
1347         /* update ablities and speed */
1348         if (abilities & I40E_AQ_PHY_AN_ENABLED)
1349                 phy_conf.link_speed = advt;
1350         else
1351                 phy_conf.link_speed = force_speed;
1352
1353         phy_conf.abilities = abilities;
1354
1355         /* use get_phy_abilities_resp value for the rest */
1356         phy_conf.phy_type = phy_ab.phy_type;
1357         phy_conf.eee_capability = phy_ab.eee_capability;
1358         phy_conf.eeer = phy_ab.eeer_val;
1359         phy_conf.low_power_ctrl = phy_ab.d3_lpan;
1360
1361         PMD_DRV_LOG(DEBUG, "\tCurrent: abilities %x, link_speed %x",
1362                     phy_ab.abilities, phy_ab.link_speed);
1363         PMD_DRV_LOG(DEBUG, "\tConfig:  abilities %x, link_speed %x",
1364                     phy_conf.abilities, phy_conf.link_speed);
1365
1366         status = i40e_aq_set_phy_config(hw, &phy_conf, NULL);
1367         if (status)
1368                 return ret;
1369
1370         return I40E_SUCCESS;
1371 }
1372
1373 static int
1374 i40e_apply_link_speed(struct rte_eth_dev *dev)
1375 {
1376         uint8_t speed;
1377         uint8_t abilities = 0;
1378         struct i40e_hw *hw = I40E_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1379         struct rte_eth_conf *conf = &dev->data->dev_conf;
1380
1381         speed = i40e_parse_link_speed(conf->link_speed);
1382         abilities |= I40E_AQ_PHY_ENABLE_ATOMIC_LINK;
1383         if (conf->link_speed == ETH_LINK_SPEED_AUTONEG)
1384                 abilities |= I40E_AQ_PHY_AN_ENABLED;
1385         else
1386                 abilities |= I40E_AQ_PHY_LINK_ENABLED;
1387
1388         return i40e_phy_conf_link(hw, abilities, speed);
1389 }
1390
1391 static int
1392 i40e_dev_start(struct rte_eth_dev *dev)
1393 {
1394         struct i40e_pf *pf = I40E_DEV_PRIVATE_TO_PF(dev->data->dev_private);
1395         struct i40e_hw *hw = I40E_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1396         struct i40e_vsi *main_vsi = pf->main_vsi;
1397         int ret, i;
1398         struct rte_intr_handle *intr_handle = &dev->pci_dev->intr_handle;
1399         uint32_t intr_vector = 0;
1400
1401         hw->adapter_stopped = 0;
1402
1403         if ((dev->data->dev_conf.link_duplex != ETH_LINK_AUTONEG_DUPLEX) &&
1404                 (dev->data->dev_conf.link_duplex != ETH_LINK_FULL_DUPLEX)) {
1405                 PMD_INIT_LOG(ERR, "Invalid link_duplex (%hu) for port %hhu",
1406                              dev->data->dev_conf.link_duplex,
1407                              dev->data->port_id);
1408                 return -EINVAL;
1409         }
1410
1411         rte_intr_disable(intr_handle);
1412
1413         if ((rte_intr_cap_multiple(intr_handle) ||
1414              !RTE_ETH_DEV_SRIOV(dev).active) &&
1415             dev->data->dev_conf.intr_conf.rxq != 0) {
1416                 intr_vector = dev->data->nb_rx_queues;
1417                 if (rte_intr_efd_enable(intr_handle, intr_vector))
1418                         return -1;
1419         }
1420
1421         if (rte_intr_dp_is_en(intr_handle) && !intr_handle->intr_vec) {
1422                 intr_handle->intr_vec =
1423                         rte_zmalloc("intr_vec",
1424                                     dev->data->nb_rx_queues * sizeof(int),
1425                                     0);
1426                 if (!intr_handle->intr_vec) {
1427                         PMD_INIT_LOG(ERR, "Failed to allocate %d rx_queues"
1428                                      " intr_vec\n", dev->data->nb_rx_queues);
1429                         return -ENOMEM;
1430                 }
1431         }
1432
1433         /* Initialize VSI */
1434         ret = i40e_dev_rxtx_init(pf);
1435         if (ret != I40E_SUCCESS) {
1436                 PMD_DRV_LOG(ERR, "Failed to init rx/tx queues");
1437                 goto err_up;
1438         }
1439
1440         /* Map queues with MSIX interrupt */
1441         main_vsi->nb_used_qps = dev->data->nb_rx_queues -
1442                 pf->nb_cfg_vmdq_vsi * RTE_LIBRTE_I40E_QUEUE_NUM_PER_VM;
1443         i40e_vsi_queues_bind_intr(main_vsi);
1444         i40e_vsi_enable_queues_intr(main_vsi);
1445
1446         /* Map VMDQ VSI queues with MSIX interrupt */
1447         for (i = 0; i < pf->nb_cfg_vmdq_vsi; i++) {
1448                 pf->vmdq[i].vsi->nb_used_qps = RTE_LIBRTE_I40E_QUEUE_NUM_PER_VM;
1449                 i40e_vsi_queues_bind_intr(pf->vmdq[i].vsi);
1450                 i40e_vsi_enable_queues_intr(pf->vmdq[i].vsi);
1451         }
1452
1453         /* enable FDIR MSIX interrupt */
1454         if (pf->fdir.fdir_vsi) {
1455                 i40e_vsi_queues_bind_intr(pf->fdir.fdir_vsi);
1456                 i40e_vsi_enable_queues_intr(pf->fdir.fdir_vsi);
1457         }
1458
1459         /* Enable all queues which have been configured */
1460         ret = i40e_dev_switch_queues(pf, TRUE);
1461         if (ret != I40E_SUCCESS) {
1462                 PMD_DRV_LOG(ERR, "Failed to enable VSI");
1463                 goto err_up;
1464         }
1465
1466         /* Enable receiving broadcast packets */
1467         ret = i40e_aq_set_vsi_broadcast(hw, main_vsi->seid, true, NULL);
1468         if (ret != I40E_SUCCESS)
1469                 PMD_DRV_LOG(INFO, "fail to set vsi broadcast");
1470
1471         for (i = 0; i < pf->nb_cfg_vmdq_vsi; i++) {
1472                 ret = i40e_aq_set_vsi_broadcast(hw, pf->vmdq[i].vsi->seid,
1473                                                 true, NULL);
1474                 if (ret != I40E_SUCCESS)
1475                         PMD_DRV_LOG(INFO, "fail to set vsi broadcast");
1476         }
1477
1478         /* Apply link configure */
1479         ret = i40e_apply_link_speed(dev);
1480         if (I40E_SUCCESS != ret) {
1481                 PMD_DRV_LOG(ERR, "Fail to apply link setting");
1482                 goto err_up;
1483         }
1484
1485         if (!rte_intr_allow_others(intr_handle)) {
1486                 rte_intr_callback_unregister(intr_handle,
1487                                              i40e_dev_interrupt_handler,
1488                                              (void *)dev);
1489                 /* configure and enable device interrupt */
1490                 i40e_pf_config_irq0(hw, FALSE);
1491                 i40e_pf_enable_irq0(hw);
1492
1493                 if (dev->data->dev_conf.intr_conf.lsc != 0)
1494                         PMD_INIT_LOG(INFO, "lsc won't enable because of"
1495                                      " no intr multiplex\n");
1496         }
1497
1498         /* enable uio intr after callback register */
1499         rte_intr_enable(intr_handle);
1500
1501         return I40E_SUCCESS;
1502
1503 err_up:
1504         i40e_dev_switch_queues(pf, FALSE);
1505         i40e_dev_clear_queues(dev);
1506
1507         return ret;
1508 }
1509
1510 static void
1511 i40e_dev_stop(struct rte_eth_dev *dev)
1512 {
1513         struct i40e_pf *pf = I40E_DEV_PRIVATE_TO_PF(dev->data->dev_private);
1514         struct i40e_vsi *main_vsi = pf->main_vsi;
1515         struct i40e_mirror_rule *p_mirror;
1516         struct rte_intr_handle *intr_handle = &dev->pci_dev->intr_handle;
1517         int i;
1518
1519         /* Disable all queues */
1520         i40e_dev_switch_queues(pf, FALSE);
1521
1522         /* un-map queues with interrupt registers */
1523         i40e_vsi_disable_queues_intr(main_vsi);
1524         i40e_vsi_queues_unbind_intr(main_vsi);
1525
1526         for (i = 0; i < pf->nb_cfg_vmdq_vsi; i++) {
1527                 i40e_vsi_disable_queues_intr(pf->vmdq[i].vsi);
1528                 i40e_vsi_queues_unbind_intr(pf->vmdq[i].vsi);
1529         }
1530
1531         if (pf->fdir.fdir_vsi) {
1532                 i40e_vsi_queues_unbind_intr(pf->fdir.fdir_vsi);
1533                 i40e_vsi_disable_queues_intr(pf->fdir.fdir_vsi);
1534         }
1535         /* Clear all queues and release memory */
1536         i40e_dev_clear_queues(dev);
1537
1538         /* Set link down */
1539         i40e_dev_set_link_down(dev);
1540
1541         /* Remove all mirror rules */
1542         while ((p_mirror = TAILQ_FIRST(&pf->mirror_list))) {
1543                 TAILQ_REMOVE(&pf->mirror_list, p_mirror, rules);
1544                 rte_free(p_mirror);
1545         }
1546         pf->nb_mirror_rule = 0;
1547
1548         if (!rte_intr_allow_others(intr_handle))
1549                 /* resume to the default handler */
1550                 rte_intr_callback_register(intr_handle,
1551                                            i40e_dev_interrupt_handler,
1552                                            (void *)dev);
1553
1554         /* Clean datapath event and queue/vec mapping */
1555         rte_intr_efd_disable(intr_handle);
1556         if (intr_handle->intr_vec) {
1557                 rte_free(intr_handle->intr_vec);
1558                 intr_handle->intr_vec = NULL;
1559         }
1560 }
1561
1562 static void
1563 i40e_dev_close(struct rte_eth_dev *dev)
1564 {
1565         struct i40e_pf *pf = I40E_DEV_PRIVATE_TO_PF(dev->data->dev_private);
1566         struct i40e_hw *hw = I40E_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1567         uint32_t reg;
1568         int i;
1569
1570         PMD_INIT_FUNC_TRACE();
1571
1572         i40e_dev_stop(dev);
1573         hw->adapter_stopped = 1;
1574         i40e_dev_free_queues(dev);
1575
1576         /* Disable interrupt */
1577         i40e_pf_disable_irq0(hw);
1578         rte_intr_disable(&(dev->pci_dev->intr_handle));
1579
1580         /* shutdown and destroy the HMC */
1581         i40e_shutdown_lan_hmc(hw);
1582
1583         /* release all the existing VSIs and VEBs */
1584         i40e_fdir_teardown(pf);
1585         i40e_vsi_release(pf->main_vsi);
1586
1587         for (i = 0; i < pf->nb_cfg_vmdq_vsi; i++) {
1588                 i40e_vsi_release(pf->vmdq[i].vsi);
1589                 pf->vmdq[i].vsi = NULL;
1590         }
1591
1592         rte_free(pf->vmdq);
1593         pf->vmdq = NULL;
1594
1595         /* shutdown the adminq */
1596         i40e_aq_queue_shutdown(hw, true);
1597         i40e_shutdown_adminq(hw);
1598
1599         i40e_res_pool_destroy(&pf->qp_pool);
1600         i40e_res_pool_destroy(&pf->msix_pool);
1601
1602         /* force a PF reset to clean anything leftover */
1603         reg = I40E_READ_REG(hw, I40E_PFGEN_CTRL);
1604         I40E_WRITE_REG(hw, I40E_PFGEN_CTRL,
1605                         (reg | I40E_PFGEN_CTRL_PFSWR_MASK));
1606         I40E_WRITE_FLUSH(hw);
1607 }
1608
1609 static void
1610 i40e_dev_promiscuous_enable(struct rte_eth_dev *dev)
1611 {
1612         struct i40e_pf *pf = I40E_DEV_PRIVATE_TO_PF(dev->data->dev_private);
1613         struct i40e_hw *hw = I40E_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1614         struct i40e_vsi *vsi = pf->main_vsi;
1615         int status;
1616
1617         status = i40e_aq_set_vsi_unicast_promiscuous(hw, vsi->seid,
1618                                                         true, NULL);
1619         if (status != I40E_SUCCESS)
1620                 PMD_DRV_LOG(ERR, "Failed to enable unicast promiscuous");
1621
1622         status = i40e_aq_set_vsi_multicast_promiscuous(hw, vsi->seid,
1623                                                         TRUE, NULL);
1624         if (status != I40E_SUCCESS)
1625                 PMD_DRV_LOG(ERR, "Failed to enable multicast promiscuous");
1626
1627 }
1628
1629 static void
1630 i40e_dev_promiscuous_disable(struct rte_eth_dev *dev)
1631 {
1632         struct i40e_pf *pf = I40E_DEV_PRIVATE_TO_PF(dev->data->dev_private);
1633         struct i40e_hw *hw = I40E_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1634         struct i40e_vsi *vsi = pf->main_vsi;
1635         int status;
1636
1637         status = i40e_aq_set_vsi_unicast_promiscuous(hw, vsi->seid,
1638                                                         false, NULL);
1639         if (status != I40E_SUCCESS)
1640                 PMD_DRV_LOG(ERR, "Failed to disable unicast promiscuous");
1641
1642         status = i40e_aq_set_vsi_multicast_promiscuous(hw, vsi->seid,
1643                                                         false, NULL);
1644         if (status != I40E_SUCCESS)
1645                 PMD_DRV_LOG(ERR, "Failed to disable multicast promiscuous");
1646 }
1647
1648 static void
1649 i40e_dev_allmulticast_enable(struct rte_eth_dev *dev)
1650 {
1651         struct i40e_pf *pf = I40E_DEV_PRIVATE_TO_PF(dev->data->dev_private);
1652         struct i40e_hw *hw = I40E_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1653         struct i40e_vsi *vsi = pf->main_vsi;
1654         int ret;
1655
1656         ret = i40e_aq_set_vsi_multicast_promiscuous(hw, vsi->seid, TRUE, NULL);
1657         if (ret != I40E_SUCCESS)
1658                 PMD_DRV_LOG(ERR, "Failed to enable multicast promiscuous");
1659 }
1660
1661 static void
1662 i40e_dev_allmulticast_disable(struct rte_eth_dev *dev)
1663 {
1664         struct i40e_pf *pf = I40E_DEV_PRIVATE_TO_PF(dev->data->dev_private);
1665         struct i40e_hw *hw = I40E_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1666         struct i40e_vsi *vsi = pf->main_vsi;
1667         int ret;
1668
1669         if (dev->data->promiscuous == 1)
1670                 return; /* must remain in all_multicast mode */
1671
1672         ret = i40e_aq_set_vsi_multicast_promiscuous(hw,
1673                                 vsi->seid, FALSE, NULL);
1674         if (ret != I40E_SUCCESS)
1675                 PMD_DRV_LOG(ERR, "Failed to disable multicast promiscuous");
1676 }
1677
1678 /*
1679  * Set device link up.
1680  */
1681 static int
1682 i40e_dev_set_link_up(struct rte_eth_dev *dev)
1683 {
1684         /* re-apply link speed setting */
1685         return i40e_apply_link_speed(dev);
1686 }
1687
1688 /*
1689  * Set device link down.
1690  */
1691 static int
1692 i40e_dev_set_link_down(__rte_unused struct rte_eth_dev *dev)
1693 {
1694         uint8_t speed = I40E_LINK_SPEED_UNKNOWN;
1695         uint8_t abilities = I40E_AQ_PHY_ENABLE_ATOMIC_LINK;
1696         struct i40e_hw *hw = I40E_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1697
1698         return i40e_phy_conf_link(hw, abilities, speed);
1699 }
1700
1701 int
1702 i40e_dev_link_update(struct rte_eth_dev *dev,
1703                      int wait_to_complete)
1704 {
1705 #define CHECK_INTERVAL 100  /* 100ms */
1706 #define MAX_REPEAT_TIME 10  /* 1s (10 * 100ms) in total */
1707         struct i40e_hw *hw = I40E_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1708         struct i40e_link_status link_status;
1709         struct rte_eth_link link, old;
1710         int status;
1711         unsigned rep_cnt = MAX_REPEAT_TIME;
1712
1713         memset(&link, 0, sizeof(link));
1714         memset(&old, 0, sizeof(old));
1715         memset(&link_status, 0, sizeof(link_status));
1716         rte_i40e_dev_atomic_read_link_status(dev, &old);
1717
1718         do {
1719                 /* Get link status information from hardware */
1720                 status = i40e_aq_get_link_info(hw, false, &link_status, NULL);
1721                 if (status != I40E_SUCCESS) {
1722                         link.link_speed = ETH_LINK_SPEED_100;
1723                         link.link_duplex = ETH_LINK_FULL_DUPLEX;
1724                         PMD_DRV_LOG(ERR, "Failed to get link info");
1725                         goto out;
1726                 }
1727
1728                 link.link_status = link_status.link_info & I40E_AQ_LINK_UP;
1729                 if (!wait_to_complete)
1730                         break;
1731
1732                 rte_delay_ms(CHECK_INTERVAL);
1733         } while (!link.link_status && rep_cnt--);
1734
1735         if (!link.link_status)
1736                 goto out;
1737
1738         /* i40e uses full duplex only */
1739         link.link_duplex = ETH_LINK_FULL_DUPLEX;
1740
1741         /* Parse the link status */
1742         switch (link_status.link_speed) {
1743         case I40E_LINK_SPEED_100MB:
1744                 link.link_speed = ETH_LINK_SPEED_100;
1745                 break;
1746         case I40E_LINK_SPEED_1GB:
1747                 link.link_speed = ETH_LINK_SPEED_1000;
1748                 break;
1749         case I40E_LINK_SPEED_10GB:
1750                 link.link_speed = ETH_LINK_SPEED_10G;
1751                 break;
1752         case I40E_LINK_SPEED_20GB:
1753                 link.link_speed = ETH_LINK_SPEED_20G;
1754                 break;
1755         case I40E_LINK_SPEED_40GB:
1756                 link.link_speed = ETH_LINK_SPEED_40G;
1757                 break;
1758         default:
1759                 link.link_speed = ETH_LINK_SPEED_100;
1760                 break;
1761         }
1762
1763 out:
1764         rte_i40e_dev_atomic_write_link_status(dev, &link);
1765         if (link.link_status == old.link_status)
1766                 return -1;
1767
1768         return 0;
1769 }
1770
1771 /* Get all the statistics of a VSI */
1772 void
1773 i40e_update_vsi_stats(struct i40e_vsi *vsi)
1774 {
1775         struct i40e_eth_stats *oes = &vsi->eth_stats_offset;
1776         struct i40e_eth_stats *nes = &vsi->eth_stats;
1777         struct i40e_hw *hw = I40E_VSI_TO_HW(vsi);
1778         int idx = rte_le_to_cpu_16(vsi->info.stat_counter_idx);
1779
1780         i40e_stat_update_48(hw, I40E_GLV_GORCH(idx), I40E_GLV_GORCL(idx),
1781                             vsi->offset_loaded, &oes->rx_bytes,
1782                             &nes->rx_bytes);
1783         i40e_stat_update_48(hw, I40E_GLV_UPRCH(idx), I40E_GLV_UPRCL(idx),
1784                             vsi->offset_loaded, &oes->rx_unicast,
1785                             &nes->rx_unicast);
1786         i40e_stat_update_48(hw, I40E_GLV_MPRCH(idx), I40E_GLV_MPRCL(idx),
1787                             vsi->offset_loaded, &oes->rx_multicast,
1788                             &nes->rx_multicast);
1789         i40e_stat_update_48(hw, I40E_GLV_BPRCH(idx), I40E_GLV_BPRCL(idx),
1790                             vsi->offset_loaded, &oes->rx_broadcast,
1791                             &nes->rx_broadcast);
1792         i40e_stat_update_32(hw, I40E_GLV_RDPC(idx), vsi->offset_loaded,
1793                             &oes->rx_discards, &nes->rx_discards);
1794         /* GLV_REPC not supported */
1795         /* GLV_RMPC not supported */
1796         i40e_stat_update_32(hw, I40E_GLV_RUPP(idx), vsi->offset_loaded,
1797                             &oes->rx_unknown_protocol,
1798                             &nes->rx_unknown_protocol);
1799         i40e_stat_update_48(hw, I40E_GLV_GOTCH(idx), I40E_GLV_GOTCL(idx),
1800                             vsi->offset_loaded, &oes->tx_bytes,
1801                             &nes->tx_bytes);
1802         i40e_stat_update_48(hw, I40E_GLV_UPTCH(idx), I40E_GLV_UPTCL(idx),
1803                             vsi->offset_loaded, &oes->tx_unicast,
1804                             &nes->tx_unicast);
1805         i40e_stat_update_48(hw, I40E_GLV_MPTCH(idx), I40E_GLV_MPTCL(idx),
1806                             vsi->offset_loaded, &oes->tx_multicast,
1807                             &nes->tx_multicast);
1808         i40e_stat_update_48(hw, I40E_GLV_BPTCH(idx), I40E_GLV_BPTCL(idx),
1809                             vsi->offset_loaded,  &oes->tx_broadcast,
1810                             &nes->tx_broadcast);
1811         /* GLV_TDPC not supported */
1812         i40e_stat_update_32(hw, I40E_GLV_TEPC(idx), vsi->offset_loaded,
1813                             &oes->tx_errors, &nes->tx_errors);
1814         vsi->offset_loaded = true;
1815
1816         PMD_DRV_LOG(DEBUG, "***************** VSI[%u] stats start *******************",
1817                     vsi->vsi_id);
1818         PMD_DRV_LOG(DEBUG, "rx_bytes:            %"PRIu64"", nes->rx_bytes);
1819         PMD_DRV_LOG(DEBUG, "rx_unicast:          %"PRIu64"", nes->rx_unicast);
1820         PMD_DRV_LOG(DEBUG, "rx_multicast:        %"PRIu64"", nes->rx_multicast);
1821         PMD_DRV_LOG(DEBUG, "rx_broadcast:        %"PRIu64"", nes->rx_broadcast);
1822         PMD_DRV_LOG(DEBUG, "rx_discards:         %"PRIu64"", nes->rx_discards);
1823         PMD_DRV_LOG(DEBUG, "rx_unknown_protocol: %"PRIu64"",
1824                     nes->rx_unknown_protocol);
1825         PMD_DRV_LOG(DEBUG, "tx_bytes:            %"PRIu64"", nes->tx_bytes);
1826         PMD_DRV_LOG(DEBUG, "tx_unicast:          %"PRIu64"", nes->tx_unicast);
1827         PMD_DRV_LOG(DEBUG, "tx_multicast:        %"PRIu64"", nes->tx_multicast);
1828         PMD_DRV_LOG(DEBUG, "tx_broadcast:        %"PRIu64"", nes->tx_broadcast);
1829         PMD_DRV_LOG(DEBUG, "tx_discards:         %"PRIu64"", nes->tx_discards);
1830         PMD_DRV_LOG(DEBUG, "tx_errors:           %"PRIu64"", nes->tx_errors);
1831         PMD_DRV_LOG(DEBUG, "***************** VSI[%u] stats end *******************",
1832                     vsi->vsi_id);
1833 }
1834
1835 static void
1836 i40e_read_stats_registers(struct i40e_pf *pf, struct i40e_hw *hw)
1837 {
1838         unsigned int i;
1839         struct i40e_hw_port_stats *ns = &pf->stats; /* new stats */
1840         struct i40e_hw_port_stats *os = &pf->stats_offset; /* old stats */
1841         /* Get statistics of struct i40e_eth_stats */
1842         i40e_stat_update_48(hw, I40E_GLPRT_GORCH(hw->port),
1843                             I40E_GLPRT_GORCL(hw->port),
1844                             pf->offset_loaded, &os->eth.rx_bytes,
1845                             &ns->eth.rx_bytes);
1846         i40e_stat_update_48(hw, I40E_GLPRT_UPRCH(hw->port),
1847                             I40E_GLPRT_UPRCL(hw->port),
1848                             pf->offset_loaded, &os->eth.rx_unicast,
1849                             &ns->eth.rx_unicast);
1850         i40e_stat_update_48(hw, I40E_GLPRT_MPRCH(hw->port),
1851                             I40E_GLPRT_MPRCL(hw->port),
1852                             pf->offset_loaded, &os->eth.rx_multicast,
1853                             &ns->eth.rx_multicast);
1854         i40e_stat_update_48(hw, I40E_GLPRT_BPRCH(hw->port),
1855                             I40E_GLPRT_BPRCL(hw->port),
1856                             pf->offset_loaded, &os->eth.rx_broadcast,
1857                             &ns->eth.rx_broadcast);
1858         i40e_stat_update_32(hw, I40E_GLPRT_RDPC(hw->port),
1859                             pf->offset_loaded, &os->eth.rx_discards,
1860                             &ns->eth.rx_discards);
1861         /* GLPRT_REPC not supported */
1862         /* GLPRT_RMPC not supported */
1863         i40e_stat_update_32(hw, I40E_GLPRT_RUPP(hw->port),
1864                             pf->offset_loaded,
1865                             &os->eth.rx_unknown_protocol,
1866                             &ns->eth.rx_unknown_protocol);
1867         i40e_stat_update_48(hw, I40E_GLPRT_GOTCH(hw->port),
1868                             I40E_GLPRT_GOTCL(hw->port),
1869                             pf->offset_loaded, &os->eth.tx_bytes,
1870                             &ns->eth.tx_bytes);
1871         i40e_stat_update_48(hw, I40E_GLPRT_UPTCH(hw->port),
1872                             I40E_GLPRT_UPTCL(hw->port),
1873                             pf->offset_loaded, &os->eth.tx_unicast,
1874                             &ns->eth.tx_unicast);
1875         i40e_stat_update_48(hw, I40E_GLPRT_MPTCH(hw->port),
1876                             I40E_GLPRT_MPTCL(hw->port),
1877                             pf->offset_loaded, &os->eth.tx_multicast,
1878                             &ns->eth.tx_multicast);
1879         i40e_stat_update_48(hw, I40E_GLPRT_BPTCH(hw->port),
1880                             I40E_GLPRT_BPTCL(hw->port),
1881                             pf->offset_loaded, &os->eth.tx_broadcast,
1882                             &ns->eth.tx_broadcast);
1883         /* GLPRT_TEPC not supported */
1884
1885         /* additional port specific stats */
1886         i40e_stat_update_32(hw, I40E_GLPRT_TDOLD(hw->port),
1887                             pf->offset_loaded, &os->tx_dropped_link_down,
1888                             &ns->tx_dropped_link_down);
1889         i40e_stat_update_32(hw, I40E_GLPRT_CRCERRS(hw->port),
1890                             pf->offset_loaded, &os->crc_errors,
1891                             &ns->crc_errors);
1892         i40e_stat_update_32(hw, I40E_GLPRT_ILLERRC(hw->port),
1893                             pf->offset_loaded, &os->illegal_bytes,
1894                             &ns->illegal_bytes);
1895         /* GLPRT_ERRBC not supported */
1896         i40e_stat_update_32(hw, I40E_GLPRT_MLFC(hw->port),
1897                             pf->offset_loaded, &os->mac_local_faults,
1898                             &ns->mac_local_faults);
1899         i40e_stat_update_32(hw, I40E_GLPRT_MRFC(hw->port),
1900                             pf->offset_loaded, &os->mac_remote_faults,
1901                             &ns->mac_remote_faults);
1902         i40e_stat_update_32(hw, I40E_GLPRT_RLEC(hw->port),
1903                             pf->offset_loaded, &os->rx_length_errors,
1904                             &ns->rx_length_errors);
1905         i40e_stat_update_32(hw, I40E_GLPRT_LXONRXC(hw->port),
1906                             pf->offset_loaded, &os->link_xon_rx,
1907                             &ns->link_xon_rx);
1908         i40e_stat_update_32(hw, I40E_GLPRT_LXOFFRXC(hw->port),
1909                             pf->offset_loaded, &os->link_xoff_rx,
1910                             &ns->link_xoff_rx);
1911         for (i = 0; i < 8; i++) {
1912                 i40e_stat_update_32(hw, I40E_GLPRT_PXONRXC(hw->port, i),
1913                                     pf->offset_loaded,
1914                                     &os->priority_xon_rx[i],
1915                                     &ns->priority_xon_rx[i]);
1916                 i40e_stat_update_32(hw, I40E_GLPRT_PXOFFRXC(hw->port, i),
1917                                     pf->offset_loaded,
1918                                     &os->priority_xoff_rx[i],
1919                                     &ns->priority_xoff_rx[i]);
1920         }
1921         i40e_stat_update_32(hw, I40E_GLPRT_LXONTXC(hw->port),
1922                             pf->offset_loaded, &os->link_xon_tx,
1923                             &ns->link_xon_tx);
1924         i40e_stat_update_32(hw, I40E_GLPRT_LXOFFTXC(hw->port),
1925                             pf->offset_loaded, &os->link_xoff_tx,
1926                             &ns->link_xoff_tx);
1927         for (i = 0; i < 8; i++) {
1928                 i40e_stat_update_32(hw, I40E_GLPRT_PXONTXC(hw->port, i),
1929                                     pf->offset_loaded,
1930                                     &os->priority_xon_tx[i],
1931                                     &ns->priority_xon_tx[i]);
1932                 i40e_stat_update_32(hw, I40E_GLPRT_PXOFFTXC(hw->port, i),
1933                                     pf->offset_loaded,
1934                                     &os->priority_xoff_tx[i],
1935                                     &ns->priority_xoff_tx[i]);
1936                 i40e_stat_update_32(hw, I40E_GLPRT_RXON2OFFCNT(hw->port, i),
1937                                     pf->offset_loaded,
1938                                     &os->priority_xon_2_xoff[i],
1939                                     &ns->priority_xon_2_xoff[i]);
1940         }
1941         i40e_stat_update_48(hw, I40E_GLPRT_PRC64H(hw->port),
1942                             I40E_GLPRT_PRC64L(hw->port),
1943                             pf->offset_loaded, &os->rx_size_64,
1944                             &ns->rx_size_64);
1945         i40e_stat_update_48(hw, I40E_GLPRT_PRC127H(hw->port),
1946                             I40E_GLPRT_PRC127L(hw->port),
1947                             pf->offset_loaded, &os->rx_size_127,
1948                             &ns->rx_size_127);
1949         i40e_stat_update_48(hw, I40E_GLPRT_PRC255H(hw->port),
1950                             I40E_GLPRT_PRC255L(hw->port),
1951                             pf->offset_loaded, &os->rx_size_255,
1952                             &ns->rx_size_255);
1953         i40e_stat_update_48(hw, I40E_GLPRT_PRC511H(hw->port),
1954                             I40E_GLPRT_PRC511L(hw->port),
1955                             pf->offset_loaded, &os->rx_size_511,
1956                             &ns->rx_size_511);
1957         i40e_stat_update_48(hw, I40E_GLPRT_PRC1023H(hw->port),
1958                             I40E_GLPRT_PRC1023L(hw->port),
1959                             pf->offset_loaded, &os->rx_size_1023,
1960                             &ns->rx_size_1023);
1961         i40e_stat_update_48(hw, I40E_GLPRT_PRC1522H(hw->port),
1962                             I40E_GLPRT_PRC1522L(hw->port),
1963                             pf->offset_loaded, &os->rx_size_1522,
1964                             &ns->rx_size_1522);
1965         i40e_stat_update_48(hw, I40E_GLPRT_PRC9522H(hw->port),
1966                             I40E_GLPRT_PRC9522L(hw->port),
1967                             pf->offset_loaded, &os->rx_size_big,
1968                             &ns->rx_size_big);
1969         i40e_stat_update_32(hw, I40E_GLPRT_RUC(hw->port),
1970                             pf->offset_loaded, &os->rx_undersize,
1971                             &ns->rx_undersize);
1972         i40e_stat_update_32(hw, I40E_GLPRT_RFC(hw->port),
1973                             pf->offset_loaded, &os->rx_fragments,
1974                             &ns->rx_fragments);
1975         i40e_stat_update_32(hw, I40E_GLPRT_ROC(hw->port),
1976                             pf->offset_loaded, &os->rx_oversize,
1977                             &ns->rx_oversize);
1978         i40e_stat_update_32(hw, I40E_GLPRT_RJC(hw->port),
1979                             pf->offset_loaded, &os->rx_jabber,
1980                             &ns->rx_jabber);
1981         i40e_stat_update_48(hw, I40E_GLPRT_PTC64H(hw->port),
1982                             I40E_GLPRT_PTC64L(hw->port),
1983                             pf->offset_loaded, &os->tx_size_64,
1984                             &ns->tx_size_64);
1985         i40e_stat_update_48(hw, I40E_GLPRT_PTC127H(hw->port),
1986                             I40E_GLPRT_PTC127L(hw->port),
1987                             pf->offset_loaded, &os->tx_size_127,
1988                             &ns->tx_size_127);
1989         i40e_stat_update_48(hw, I40E_GLPRT_PTC255H(hw->port),
1990                             I40E_GLPRT_PTC255L(hw->port),
1991                             pf->offset_loaded, &os->tx_size_255,
1992                             &ns->tx_size_255);
1993         i40e_stat_update_48(hw, I40E_GLPRT_PTC511H(hw->port),
1994                             I40E_GLPRT_PTC511L(hw->port),
1995                             pf->offset_loaded, &os->tx_size_511,
1996                             &ns->tx_size_511);
1997         i40e_stat_update_48(hw, I40E_GLPRT_PTC1023H(hw->port),
1998                             I40E_GLPRT_PTC1023L(hw->port),
1999                             pf->offset_loaded, &os->tx_size_1023,
2000                             &ns->tx_size_1023);
2001         i40e_stat_update_48(hw, I40E_GLPRT_PTC1522H(hw->port),
2002                             I40E_GLPRT_PTC1522L(hw->port),
2003                             pf->offset_loaded, &os->tx_size_1522,
2004                             &ns->tx_size_1522);
2005         i40e_stat_update_48(hw, I40E_GLPRT_PTC9522H(hw->port),
2006                             I40E_GLPRT_PTC9522L(hw->port),
2007                             pf->offset_loaded, &os->tx_size_big,
2008                             &ns->tx_size_big);
2009         i40e_stat_update_32(hw, I40E_GLQF_PCNT(pf->fdir.match_counter_index),
2010                            pf->offset_loaded,
2011                            &os->fd_sb_match, &ns->fd_sb_match);
2012         /* GLPRT_MSPDC not supported */
2013         /* GLPRT_XEC not supported */
2014
2015         pf->offset_loaded = true;
2016
2017         if (pf->main_vsi)
2018                 i40e_update_vsi_stats(pf->main_vsi);
2019 }
2020
2021 /* Get all statistics of a port */
2022 static void
2023 i40e_dev_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
2024 {
2025         struct i40e_pf *pf = I40E_DEV_PRIVATE_TO_PF(dev->data->dev_private);
2026         struct i40e_hw *hw = I40E_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2027         struct i40e_hw_port_stats *ns = &pf->stats; /* new stats */
2028         unsigned i;
2029
2030         /* call read registers - updates values, now write them to struct */
2031         i40e_read_stats_registers(pf, hw);
2032
2033         stats->ipackets = pf->main_vsi->eth_stats.rx_unicast +
2034                         pf->main_vsi->eth_stats.rx_multicast +
2035                         pf->main_vsi->eth_stats.rx_broadcast -
2036                         pf->main_vsi->eth_stats.rx_discards;
2037         stats->opackets = pf->main_vsi->eth_stats.tx_unicast +
2038                         pf->main_vsi->eth_stats.tx_multicast +
2039                         pf->main_vsi->eth_stats.tx_broadcast;
2040         stats->ibytes   = pf->main_vsi->eth_stats.rx_bytes;
2041         stats->obytes   = pf->main_vsi->eth_stats.tx_bytes;
2042         stats->oerrors  = ns->eth.tx_errors +
2043                         pf->main_vsi->eth_stats.tx_errors;
2044         stats->imcasts  = pf->main_vsi->eth_stats.rx_multicast;
2045         stats->fdirmatch = ns->fd_sb_match;
2046
2047         /* Rx Errors */
2048         stats->ibadcrc  = ns->crc_errors;
2049         stats->ibadlen  = ns->rx_length_errors + ns->rx_undersize +
2050                         ns->rx_oversize + ns->rx_fragments + ns->rx_jabber;
2051         stats->imissed  = ns->eth.rx_discards +
2052                         pf->main_vsi->eth_stats.rx_discards;
2053         stats->ierrors  = stats->ibadcrc + stats->ibadlen + stats->imissed;
2054
2055         PMD_DRV_LOG(DEBUG, "***************** PF stats start *******************");
2056         PMD_DRV_LOG(DEBUG, "rx_bytes:            %"PRIu64"", ns->eth.rx_bytes);
2057         PMD_DRV_LOG(DEBUG, "rx_unicast:          %"PRIu64"", ns->eth.rx_unicast);
2058         PMD_DRV_LOG(DEBUG, "rx_multicast:        %"PRIu64"", ns->eth.rx_multicast);
2059         PMD_DRV_LOG(DEBUG, "rx_broadcast:        %"PRIu64"", ns->eth.rx_broadcast);
2060         PMD_DRV_LOG(DEBUG, "rx_discards:         %"PRIu64"", ns->eth.rx_discards);
2061         PMD_DRV_LOG(DEBUG, "rx_unknown_protocol: %"PRIu64"",
2062                     ns->eth.rx_unknown_protocol);
2063         PMD_DRV_LOG(DEBUG, "tx_bytes:            %"PRIu64"", ns->eth.tx_bytes);
2064         PMD_DRV_LOG(DEBUG, "tx_unicast:          %"PRIu64"", ns->eth.tx_unicast);
2065         PMD_DRV_LOG(DEBUG, "tx_multicast:        %"PRIu64"", ns->eth.tx_multicast);
2066         PMD_DRV_LOG(DEBUG, "tx_broadcast:        %"PRIu64"", ns->eth.tx_broadcast);
2067         PMD_DRV_LOG(DEBUG, "tx_discards:         %"PRIu64"", ns->eth.tx_discards);
2068         PMD_DRV_LOG(DEBUG, "tx_errors:           %"PRIu64"", ns->eth.tx_errors);
2069
2070         PMD_DRV_LOG(DEBUG, "tx_dropped_link_down:     %"PRIu64"",
2071                     ns->tx_dropped_link_down);
2072         PMD_DRV_LOG(DEBUG, "crc_errors:               %"PRIu64"", ns->crc_errors);
2073         PMD_DRV_LOG(DEBUG, "illegal_bytes:            %"PRIu64"",
2074                     ns->illegal_bytes);
2075         PMD_DRV_LOG(DEBUG, "error_bytes:              %"PRIu64"", ns->error_bytes);
2076         PMD_DRV_LOG(DEBUG, "mac_local_faults:         %"PRIu64"",
2077                     ns->mac_local_faults);
2078         PMD_DRV_LOG(DEBUG, "mac_remote_faults:        %"PRIu64"",
2079                     ns->mac_remote_faults);
2080         PMD_DRV_LOG(DEBUG, "rx_length_errors:         %"PRIu64"",
2081                     ns->rx_length_errors);
2082         PMD_DRV_LOG(DEBUG, "link_xon_rx:              %"PRIu64"", ns->link_xon_rx);
2083         PMD_DRV_LOG(DEBUG, "link_xoff_rx:             %"PRIu64"", ns->link_xoff_rx);
2084         for (i = 0; i < 8; i++) {
2085                 PMD_DRV_LOG(DEBUG, "priority_xon_rx[%d]:      %"PRIu64"",
2086                                 i, ns->priority_xon_rx[i]);
2087                 PMD_DRV_LOG(DEBUG, "priority_xoff_rx[%d]:     %"PRIu64"",
2088                                 i, ns->priority_xoff_rx[i]);
2089         }
2090         PMD_DRV_LOG(DEBUG, "link_xon_tx:              %"PRIu64"", ns->link_xon_tx);
2091         PMD_DRV_LOG(DEBUG, "link_xoff_tx:             %"PRIu64"", ns->link_xoff_tx);
2092         for (i = 0; i < 8; i++) {
2093                 PMD_DRV_LOG(DEBUG, "priority_xon_tx[%d]:      %"PRIu64"",
2094                                 i, ns->priority_xon_tx[i]);
2095                 PMD_DRV_LOG(DEBUG, "priority_xoff_tx[%d]:     %"PRIu64"",
2096                                 i, ns->priority_xoff_tx[i]);
2097                 PMD_DRV_LOG(DEBUG, "priority_xon_2_xoff[%d]:  %"PRIu64"",
2098                                 i, ns->priority_xon_2_xoff[i]);
2099         }
2100         PMD_DRV_LOG(DEBUG, "rx_size_64:               %"PRIu64"", ns->rx_size_64);
2101         PMD_DRV_LOG(DEBUG, "rx_size_127:              %"PRIu64"", ns->rx_size_127);
2102         PMD_DRV_LOG(DEBUG, "rx_size_255:              %"PRIu64"", ns->rx_size_255);
2103         PMD_DRV_LOG(DEBUG, "rx_size_511:              %"PRIu64"", ns->rx_size_511);
2104         PMD_DRV_LOG(DEBUG, "rx_size_1023:             %"PRIu64"", ns->rx_size_1023);
2105         PMD_DRV_LOG(DEBUG, "rx_size_1522:             %"PRIu64"", ns->rx_size_1522);
2106         PMD_DRV_LOG(DEBUG, "rx_size_big:              %"PRIu64"", ns->rx_size_big);
2107         PMD_DRV_LOG(DEBUG, "rx_undersize:             %"PRIu64"", ns->rx_undersize);
2108         PMD_DRV_LOG(DEBUG, "rx_fragments:             %"PRIu64"", ns->rx_fragments);
2109         PMD_DRV_LOG(DEBUG, "rx_oversize:              %"PRIu64"", ns->rx_oversize);
2110         PMD_DRV_LOG(DEBUG, "rx_jabber:                %"PRIu64"", ns->rx_jabber);
2111         PMD_DRV_LOG(DEBUG, "tx_size_64:               %"PRIu64"", ns->tx_size_64);
2112         PMD_DRV_LOG(DEBUG, "tx_size_127:              %"PRIu64"", ns->tx_size_127);
2113         PMD_DRV_LOG(DEBUG, "tx_size_255:              %"PRIu64"", ns->tx_size_255);
2114         PMD_DRV_LOG(DEBUG, "tx_size_511:              %"PRIu64"", ns->tx_size_511);
2115         PMD_DRV_LOG(DEBUG, "tx_size_1023:             %"PRIu64"", ns->tx_size_1023);
2116         PMD_DRV_LOG(DEBUG, "tx_size_1522:             %"PRIu64"", ns->tx_size_1522);
2117         PMD_DRV_LOG(DEBUG, "tx_size_big:              %"PRIu64"", ns->tx_size_big);
2118         PMD_DRV_LOG(DEBUG, "mac_short_packet_dropped: %"PRIu64"",
2119                         ns->mac_short_packet_dropped);
2120         PMD_DRV_LOG(DEBUG, "checksum_error:           %"PRIu64"",
2121                     ns->checksum_error);
2122         PMD_DRV_LOG(DEBUG, "fdir_match:               %"PRIu64"", ns->fd_sb_match);
2123         PMD_DRV_LOG(DEBUG, "***************** PF stats end ********************");
2124 }
2125
2126 /* Reset the statistics */
2127 static void
2128 i40e_dev_stats_reset(struct rte_eth_dev *dev)
2129 {
2130         struct i40e_pf *pf = I40E_DEV_PRIVATE_TO_PF(dev->data->dev_private);
2131         struct i40e_hw *hw = I40E_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2132
2133         /* Mark PF and VSI stats to update the offset, aka "reset" */
2134         pf->offset_loaded = false;
2135         if (pf->main_vsi)
2136                 pf->main_vsi->offset_loaded = false;
2137
2138         /* read the stats, reading current register values into offset */
2139         i40e_read_stats_registers(pf, hw);
2140 }
2141
2142 static int
2143 i40e_dev_xstats_get(struct rte_eth_dev *dev, struct rte_eth_xstats *xstats,
2144                     unsigned n)
2145 {
2146         struct i40e_pf *pf = I40E_DEV_PRIVATE_TO_PF(dev->data->dev_private);
2147         struct i40e_hw *hw = I40E_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2148         unsigned i, count = 0;
2149         struct i40e_hw_port_stats *hw_stats = &pf->stats;
2150
2151         if (n < I40E_NB_XSTATS)
2152                 return I40E_NB_XSTATS;
2153
2154         i40e_read_stats_registers(pf, hw);
2155
2156         /* Reset */
2157         if (xstats == NULL)
2158                 return 0;
2159
2160         /* Get stats from i40e_eth_stats struct */
2161         for (i = 0; i < I40E_NB_ETH_XSTATS; i++) {
2162                 snprintf(xstats[count].name, sizeof(xstats[count].name),
2163                          "%s", rte_i40e_stats_strings[i].name);
2164                 xstats[count].value = *(uint64_t *)(((char *)&hw_stats->eth) +
2165                         rte_i40e_stats_strings[i].offset);
2166                 count++;
2167         }
2168
2169         /* Get individiual stats from i40e_hw_port struct */
2170         for (i = 0; i < I40E_NB_HW_PORT_XSTATS; i++) {
2171                 snprintf(xstats[count].name, sizeof(xstats[count].name),
2172                          "%s", rte_i40e_hw_port_strings[i].name);
2173                 xstats[count].value = *(uint64_t *)(((char *)hw_stats) +
2174                                 rte_i40e_hw_port_strings[i].offset);
2175                 count++;
2176         }
2177
2178         /* Get per-queue stats from i40e_hw_port struct */
2179         for (i = 0; i < 8; i++) {
2180                 snprintf(xstats[count].name, sizeof(xstats[count].name),
2181                          "rx_q%u_xon_priority_packets", i);
2182                 xstats[count].value = *(uint64_t *)(((char *)hw_stats) +
2183                                 offsetof(struct i40e_hw_port_stats,
2184                                          priority_xon_rx[i]));
2185                 count++;
2186
2187                 snprintf(xstats[count].name, sizeof(xstats[count].name),
2188                          "rx_q%u_xoff_priority_packets", i);
2189                 xstats[count].value = *(uint64_t *)(((char *)hw_stats) +
2190                                 offsetof(struct i40e_hw_port_stats,
2191                                          priority_xoff_rx[i]));
2192                 count++;
2193
2194                 snprintf(xstats[count].name, sizeof(xstats[count].name),
2195                          "tx_q%u_xon_priority_packets", i);
2196                 xstats[count].value = *(uint64_t *)(((char *)hw_stats) +
2197                                 offsetof(struct i40e_hw_port_stats,
2198                                          priority_xon_tx[i]));
2199                 count++;
2200
2201                 snprintf(xstats[count].name, sizeof(xstats[count].name),
2202                          "tx_q%u_xoff_priority_packets", i);
2203                 xstats[count].value = *(uint64_t *)(((char *)hw_stats) +
2204                                 offsetof(struct i40e_hw_port_stats,
2205                                          priority_xoff_tx[i]));
2206                 count++;
2207
2208                 snprintf(xstats[count].name, sizeof(xstats[count].name),
2209                          "xx_q%u_xon_to_xoff_priority_packets", i);
2210                 xstats[count].value = *(uint64_t *)(((char *)hw_stats) +
2211                                 offsetof(struct i40e_hw_port_stats,
2212                                          priority_xon_2_xoff[i]));
2213                 count++;
2214         }
2215
2216         return I40E_NB_XSTATS;
2217 }
2218
2219 static int
2220 i40e_dev_queue_stats_mapping_set(__rte_unused struct rte_eth_dev *dev,
2221                                  __rte_unused uint16_t queue_id,
2222                                  __rte_unused uint8_t stat_idx,
2223                                  __rte_unused uint8_t is_rx)
2224 {
2225         PMD_INIT_FUNC_TRACE();
2226
2227         return -ENOSYS;
2228 }
2229
2230 static void
2231 i40e_dev_info_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *dev_info)
2232 {
2233         struct i40e_pf *pf = I40E_DEV_PRIVATE_TO_PF(dev->data->dev_private);
2234         struct i40e_vsi *vsi = pf->main_vsi;
2235
2236         dev_info->max_rx_queues = vsi->nb_qps;
2237         dev_info->max_tx_queues = vsi->nb_qps;
2238         dev_info->min_rx_bufsize = I40E_BUF_SIZE_MIN;
2239         dev_info->max_rx_pktlen = I40E_FRAME_SIZE_MAX;
2240         dev_info->max_mac_addrs = vsi->max_macaddrs;
2241         dev_info->max_vfs = dev->pci_dev->max_vfs;
2242         dev_info->rx_offload_capa =
2243                 DEV_RX_OFFLOAD_VLAN_STRIP |
2244                 DEV_RX_OFFLOAD_QINQ_STRIP |
2245                 DEV_RX_OFFLOAD_IPV4_CKSUM |
2246                 DEV_RX_OFFLOAD_UDP_CKSUM |
2247                 DEV_RX_OFFLOAD_TCP_CKSUM;
2248         dev_info->tx_offload_capa =
2249                 DEV_TX_OFFLOAD_VLAN_INSERT |
2250                 DEV_TX_OFFLOAD_QINQ_INSERT |
2251                 DEV_TX_OFFLOAD_IPV4_CKSUM |
2252                 DEV_TX_OFFLOAD_UDP_CKSUM |
2253                 DEV_TX_OFFLOAD_TCP_CKSUM |
2254                 DEV_TX_OFFLOAD_SCTP_CKSUM |
2255                 DEV_TX_OFFLOAD_OUTER_IPV4_CKSUM |
2256                 DEV_TX_OFFLOAD_TCP_TSO;
2257         dev_info->hash_key_size = (I40E_PFQF_HKEY_MAX_INDEX + 1) *
2258                                                 sizeof(uint32_t);
2259         dev_info->reta_size = pf->hash_lut_size;
2260         dev_info->flow_type_rss_offloads = I40E_RSS_OFFLOAD_ALL;
2261
2262         dev_info->default_rxconf = (struct rte_eth_rxconf) {
2263                 .rx_thresh = {
2264                         .pthresh = I40E_DEFAULT_RX_PTHRESH,
2265                         .hthresh = I40E_DEFAULT_RX_HTHRESH,
2266                         .wthresh = I40E_DEFAULT_RX_WTHRESH,
2267                 },
2268                 .rx_free_thresh = I40E_DEFAULT_RX_FREE_THRESH,
2269                 .rx_drop_en = 0,
2270         };
2271
2272         dev_info->default_txconf = (struct rte_eth_txconf) {
2273                 .tx_thresh = {
2274                         .pthresh = I40E_DEFAULT_TX_PTHRESH,
2275                         .hthresh = I40E_DEFAULT_TX_HTHRESH,
2276                         .wthresh = I40E_DEFAULT_TX_WTHRESH,
2277                 },
2278                 .tx_free_thresh = I40E_DEFAULT_TX_FREE_THRESH,
2279                 .tx_rs_thresh = I40E_DEFAULT_TX_RSBIT_THRESH,
2280                 .txq_flags = ETH_TXQ_FLAGS_NOMULTSEGS |
2281                                 ETH_TXQ_FLAGS_NOOFFLOADS,
2282         };
2283
2284         dev_info->rx_desc_lim = (struct rte_eth_desc_lim) {
2285                 .nb_max = I40E_MAX_RING_DESC,
2286                 .nb_min = I40E_MIN_RING_DESC,
2287                 .nb_align = I40E_ALIGN_RING_DESC,
2288         };
2289
2290         dev_info->tx_desc_lim = (struct rte_eth_desc_lim) {
2291                 .nb_max = I40E_MAX_RING_DESC,
2292                 .nb_min = I40E_MIN_RING_DESC,
2293                 .nb_align = I40E_ALIGN_RING_DESC,
2294         };
2295
2296         if (pf->flags & I40E_FLAG_VMDQ) {
2297                 dev_info->max_vmdq_pools = pf->max_nb_vmdq_vsi;
2298                 dev_info->vmdq_queue_base = dev_info->max_rx_queues;
2299                 dev_info->vmdq_queue_num = pf->vmdq_nb_qps *
2300                                                 pf->max_nb_vmdq_vsi;
2301                 dev_info->vmdq_pool_base = I40E_VMDQ_POOL_BASE;
2302                 dev_info->max_rx_queues += dev_info->vmdq_queue_num;
2303                 dev_info->max_tx_queues += dev_info->vmdq_queue_num;
2304         }
2305 }
2306
2307 static int
2308 i40e_vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
2309 {
2310         struct i40e_pf *pf = I40E_DEV_PRIVATE_TO_PF(dev->data->dev_private);
2311         struct i40e_vsi *vsi = pf->main_vsi;
2312         PMD_INIT_FUNC_TRACE();
2313
2314         if (on)
2315                 return i40e_vsi_add_vlan(vsi, vlan_id);
2316         else
2317                 return i40e_vsi_delete_vlan(vsi, vlan_id);
2318 }
2319
2320 static void
2321 i40e_vlan_tpid_set(__rte_unused struct rte_eth_dev *dev,
2322                    __rte_unused uint16_t tpid)
2323 {
2324         PMD_INIT_FUNC_TRACE();
2325 }
2326
2327 static void
2328 i40e_vlan_offload_set(struct rte_eth_dev *dev, int mask)
2329 {
2330         struct i40e_pf *pf = I40E_DEV_PRIVATE_TO_PF(dev->data->dev_private);
2331         struct i40e_vsi *vsi = pf->main_vsi;
2332
2333         if (mask & ETH_VLAN_STRIP_MASK) {
2334                 /* Enable or disable VLAN stripping */
2335                 if (dev->data->dev_conf.rxmode.hw_vlan_strip)
2336                         i40e_vsi_config_vlan_stripping(vsi, TRUE);
2337                 else
2338                         i40e_vsi_config_vlan_stripping(vsi, FALSE);
2339         }
2340
2341         if (mask & ETH_VLAN_EXTEND_MASK) {
2342                 if (dev->data->dev_conf.rxmode.hw_vlan_extend)
2343                         i40e_vsi_config_double_vlan(vsi, TRUE);
2344                 else
2345                         i40e_vsi_config_double_vlan(vsi, FALSE);
2346         }
2347 }
2348
2349 static void
2350 i40e_vlan_strip_queue_set(__rte_unused struct rte_eth_dev *dev,
2351                           __rte_unused uint16_t queue,
2352                           __rte_unused int on)
2353 {
2354         PMD_INIT_FUNC_TRACE();
2355 }
2356
2357 static int
2358 i40e_vlan_pvid_set(struct rte_eth_dev *dev, uint16_t pvid, int on)
2359 {
2360         struct i40e_pf *pf = I40E_DEV_PRIVATE_TO_PF(dev->data->dev_private);
2361         struct i40e_vsi *vsi = pf->main_vsi;
2362         struct rte_eth_dev_data *data = I40E_VSI_TO_DEV_DATA(vsi);
2363         struct i40e_vsi_vlan_pvid_info info;
2364
2365         memset(&info, 0, sizeof(info));
2366         info.on = on;
2367         if (info.on)
2368                 info.config.pvid = pvid;
2369         else {
2370                 info.config.reject.tagged =
2371                                 data->dev_conf.txmode.hw_vlan_reject_tagged;
2372                 info.config.reject.untagged =
2373                                 data->dev_conf.txmode.hw_vlan_reject_untagged;
2374         }
2375
2376         return i40e_vsi_vlan_pvid_set(vsi, &info);
2377 }
2378
2379 static int
2380 i40e_dev_led_on(struct rte_eth_dev *dev)
2381 {
2382         struct i40e_hw *hw = I40E_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2383         uint32_t mode = i40e_led_get(hw);
2384
2385         if (mode == 0)
2386                 i40e_led_set(hw, 0xf, true); /* 0xf means led always true */
2387
2388         return 0;
2389 }
2390
2391 static int
2392 i40e_dev_led_off(struct rte_eth_dev *dev)
2393 {
2394         struct i40e_hw *hw = I40E_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2395         uint32_t mode = i40e_led_get(hw);
2396
2397         if (mode != 0)
2398                 i40e_led_set(hw, 0, false);
2399
2400         return 0;
2401 }
2402
2403 static int
2404 i40e_flow_ctrl_get(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
2405 {
2406         struct i40e_hw *hw = I40E_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2407         struct i40e_pf *pf = I40E_DEV_PRIVATE_TO_PF(dev->data->dev_private);
2408
2409         fc_conf->pause_time = pf->fc_conf.pause_time;
2410         fc_conf->high_water =  pf->fc_conf.high_water[I40E_MAX_TRAFFIC_CLASS];
2411         fc_conf->low_water = pf->fc_conf.low_water[I40E_MAX_TRAFFIC_CLASS];
2412
2413          /* Return current mode according to actual setting*/
2414         switch (hw->fc.current_mode) {
2415         case I40E_FC_FULL:
2416                 fc_conf->mode = RTE_FC_FULL;
2417                 break;
2418         case I40E_FC_TX_PAUSE:
2419                 fc_conf->mode = RTE_FC_TX_PAUSE;
2420                 break;
2421         case I40E_FC_RX_PAUSE:
2422                 fc_conf->mode = RTE_FC_RX_PAUSE;
2423                 break;
2424         case I40E_FC_NONE:
2425         default:
2426                 fc_conf->mode = RTE_FC_NONE;
2427         };
2428
2429         return 0;
2430 }
2431
2432 static int
2433 i40e_flow_ctrl_set(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
2434 {
2435         uint32_t mflcn_reg, fctrl_reg, reg;
2436         uint32_t max_high_water;
2437         uint8_t i, aq_failure;
2438         int err;
2439         struct i40e_hw *hw;
2440         struct i40e_pf *pf;
2441         enum i40e_fc_mode rte_fcmode_2_i40e_fcmode[] = {
2442                 [RTE_FC_NONE] = I40E_FC_NONE,
2443                 [RTE_FC_RX_PAUSE] = I40E_FC_RX_PAUSE,
2444                 [RTE_FC_TX_PAUSE] = I40E_FC_TX_PAUSE,
2445                 [RTE_FC_FULL] = I40E_FC_FULL
2446         };
2447
2448         /* high_water field in the rte_eth_fc_conf using the kilobytes unit */
2449
2450         max_high_water = I40E_RXPBSIZE >> I40E_KILOSHIFT;
2451         if ((fc_conf->high_water > max_high_water) ||
2452                         (fc_conf->high_water < fc_conf->low_water)) {
2453                 PMD_INIT_LOG(ERR, "Invalid high/low water setup value in KB, "
2454                         "High_water must <= %d.", max_high_water);
2455                 return -EINVAL;
2456         }
2457
2458         hw = I40E_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2459         pf = I40E_DEV_PRIVATE_TO_PF(dev->data->dev_private);
2460         hw->fc.requested_mode = rte_fcmode_2_i40e_fcmode[fc_conf->mode];
2461
2462         pf->fc_conf.pause_time = fc_conf->pause_time;
2463         pf->fc_conf.high_water[I40E_MAX_TRAFFIC_CLASS] = fc_conf->high_water;
2464         pf->fc_conf.low_water[I40E_MAX_TRAFFIC_CLASS] = fc_conf->low_water;
2465
2466         PMD_INIT_FUNC_TRACE();
2467
2468         /* All the link flow control related enable/disable register
2469          * configuration is handle by the F/W
2470          */
2471         err = i40e_set_fc(hw, &aq_failure, true);
2472         if (err < 0)
2473                 return -ENOSYS;
2474
2475         if (i40e_is_40G_device(hw->device_id)) {
2476                 /* Configure flow control refresh threshold,
2477                  * the value for stat_tx_pause_refresh_timer[8]
2478                  * is used for global pause operation.
2479                  */
2480
2481                 I40E_WRITE_REG(hw,
2482                                I40E_PRTMAC_HSEC_CTL_TX_PAUSE_REFRESH_TIMER(8),
2483                                pf->fc_conf.pause_time);
2484
2485                 /* configure the timer value included in transmitted pause
2486                  * frame,
2487                  * the value for stat_tx_pause_quanta[8] is used for global
2488                  * pause operation
2489                  */
2490                 I40E_WRITE_REG(hw, I40E_PRTMAC_HSEC_CTL_TX_PAUSE_QUANTA(8),
2491                                pf->fc_conf.pause_time);
2492
2493                 fctrl_reg = I40E_READ_REG(hw,
2494                                           I40E_PRTMAC_HSEC_CTL_RX_FORWARD_CONTROL);
2495
2496                 if (fc_conf->mac_ctrl_frame_fwd != 0)
2497                         fctrl_reg |= I40E_PRTMAC_FWD_CTRL;
2498                 else
2499                         fctrl_reg &= ~I40E_PRTMAC_FWD_CTRL;
2500
2501                 I40E_WRITE_REG(hw, I40E_PRTMAC_HSEC_CTL_RX_FORWARD_CONTROL,
2502                                fctrl_reg);
2503         } else {
2504                 /* Configure pause time (2 TCs per register) */
2505                 reg = (uint32_t)pf->fc_conf.pause_time * (uint32_t)0x00010001;
2506                 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS / 2; i++)
2507                         I40E_WRITE_REG(hw, I40E_PRTDCB_FCTTVN(i), reg);
2508
2509                 /* Configure flow control refresh threshold value */
2510                 I40E_WRITE_REG(hw, I40E_PRTDCB_FCRTV,
2511                                pf->fc_conf.pause_time / 2);
2512
2513                 mflcn_reg = I40E_READ_REG(hw, I40E_PRTDCB_MFLCN);
2514
2515                 /* set or clear MFLCN.PMCF & MFLCN.DPF bits
2516                  *depending on configuration
2517                  */
2518                 if (fc_conf->mac_ctrl_frame_fwd != 0) {
2519                         mflcn_reg |= I40E_PRTDCB_MFLCN_PMCF_MASK;
2520                         mflcn_reg &= ~I40E_PRTDCB_MFLCN_DPF_MASK;
2521                 } else {
2522                         mflcn_reg &= ~I40E_PRTDCB_MFLCN_PMCF_MASK;
2523                         mflcn_reg |= I40E_PRTDCB_MFLCN_DPF_MASK;
2524                 }
2525
2526                 I40E_WRITE_REG(hw, I40E_PRTDCB_MFLCN, mflcn_reg);
2527         }
2528
2529         /* config the water marker both based on the packets and bytes */
2530         I40E_WRITE_REG(hw, I40E_GLRPB_PHW,
2531                        (pf->fc_conf.high_water[I40E_MAX_TRAFFIC_CLASS]
2532                        << I40E_KILOSHIFT) / I40E_PACKET_AVERAGE_SIZE);
2533         I40E_WRITE_REG(hw, I40E_GLRPB_PLW,
2534                        (pf->fc_conf.low_water[I40E_MAX_TRAFFIC_CLASS]
2535                        << I40E_KILOSHIFT) / I40E_PACKET_AVERAGE_SIZE);
2536         I40E_WRITE_REG(hw, I40E_GLRPB_GHW,
2537                        pf->fc_conf.high_water[I40E_MAX_TRAFFIC_CLASS]
2538                        << I40E_KILOSHIFT);
2539         I40E_WRITE_REG(hw, I40E_GLRPB_GLW,
2540                        pf->fc_conf.low_water[I40E_MAX_TRAFFIC_CLASS]
2541                        << I40E_KILOSHIFT);
2542
2543         I40E_WRITE_FLUSH(hw);
2544
2545         return 0;
2546 }
2547
2548 static int
2549 i40e_priority_flow_ctrl_set(__rte_unused struct rte_eth_dev *dev,
2550                             __rte_unused struct rte_eth_pfc_conf *pfc_conf)
2551 {
2552         PMD_INIT_FUNC_TRACE();
2553
2554         return -ENOSYS;
2555 }
2556
2557 /* Add a MAC address, and update filters */
2558 static void
2559 i40e_macaddr_add(struct rte_eth_dev *dev,
2560                  struct ether_addr *mac_addr,
2561                  __rte_unused uint32_t index,
2562                  uint32_t pool)
2563 {
2564         struct i40e_pf *pf = I40E_DEV_PRIVATE_TO_PF(dev->data->dev_private);
2565         struct i40e_mac_filter_info mac_filter;
2566         struct i40e_vsi *vsi;
2567         int ret;
2568
2569         /* If VMDQ not enabled or configured, return */
2570         if (pool != 0 && (!(pf->flags | I40E_FLAG_VMDQ) || !pf->nb_cfg_vmdq_vsi)) {
2571                 PMD_DRV_LOG(ERR, "VMDQ not %s, can't set mac to pool %u",
2572                         pf->flags | I40E_FLAG_VMDQ ? "configured" : "enabled",
2573                         pool);
2574                 return;
2575         }
2576
2577         if (pool > pf->nb_cfg_vmdq_vsi) {
2578                 PMD_DRV_LOG(ERR, "Pool number %u invalid. Max pool is %u",
2579                                 pool, pf->nb_cfg_vmdq_vsi);
2580                 return;
2581         }
2582
2583         (void)rte_memcpy(&mac_filter.mac_addr, mac_addr, ETHER_ADDR_LEN);
2584         mac_filter.filter_type = RTE_MACVLAN_PERFECT_MATCH;
2585
2586         if (pool == 0)
2587                 vsi = pf->main_vsi;
2588         else
2589                 vsi = pf->vmdq[pool - 1].vsi;
2590
2591         ret = i40e_vsi_add_mac(vsi, &mac_filter);
2592         if (ret != I40E_SUCCESS) {
2593                 PMD_DRV_LOG(ERR, "Failed to add MACVLAN filter");
2594                 return;
2595         }
2596 }
2597
2598 /* Remove a MAC address, and update filters */
2599 static void
2600 i40e_macaddr_remove(struct rte_eth_dev *dev, uint32_t index)
2601 {
2602         struct i40e_pf *pf = I40E_DEV_PRIVATE_TO_PF(dev->data->dev_private);
2603         struct i40e_vsi *vsi;
2604         struct rte_eth_dev_data *data = dev->data;
2605         struct ether_addr *macaddr;
2606         int ret;
2607         uint32_t i;
2608         uint64_t pool_sel;
2609
2610         macaddr = &(data->mac_addrs[index]);
2611
2612         pool_sel = dev->data->mac_pool_sel[index];
2613
2614         for (i = 0; i < sizeof(pool_sel) * CHAR_BIT; i++) {
2615                 if (pool_sel & (1ULL << i)) {
2616                         if (i == 0)
2617                                 vsi = pf->main_vsi;
2618                         else {
2619                                 /* No VMDQ pool enabled or configured */
2620                                 if (!(pf->flags | I40E_FLAG_VMDQ) ||
2621                                         (i > pf->nb_cfg_vmdq_vsi)) {
2622                                         PMD_DRV_LOG(ERR, "No VMDQ pool enabled"
2623                                                         "/configured");
2624                                         return;
2625                                 }
2626                                 vsi = pf->vmdq[i - 1].vsi;
2627                         }
2628                         ret = i40e_vsi_delete_mac(vsi, macaddr);
2629
2630                         if (ret) {
2631                                 PMD_DRV_LOG(ERR, "Failed to remove MACVLAN filter");
2632                                 return;
2633                         }
2634                 }
2635         }
2636 }
2637
2638 /* Set perfect match or hash match of MAC and VLAN for a VF */
2639 static int
2640 i40e_vf_mac_filter_set(struct i40e_pf *pf,
2641                  struct rte_eth_mac_filter *filter,
2642                  bool add)
2643 {
2644         struct i40e_hw *hw;
2645         struct i40e_mac_filter_info mac_filter;
2646         struct ether_addr old_mac;
2647         struct ether_addr *new_mac;
2648         struct i40e_pf_vf *vf = NULL;
2649         uint16_t vf_id;
2650         int ret;
2651
2652         if (pf == NULL) {
2653                 PMD_DRV_LOG(ERR, "Invalid PF argument.");
2654                 return -EINVAL;
2655         }
2656         hw = I40E_PF_TO_HW(pf);
2657
2658         if (filter == NULL) {
2659                 PMD_DRV_LOG(ERR, "Invalid mac filter argument.");
2660                 return -EINVAL;
2661         }
2662
2663         new_mac = &filter->mac_addr;
2664
2665         if (is_zero_ether_addr(new_mac)) {
2666                 PMD_DRV_LOG(ERR, "Invalid ethernet address.");
2667                 return -EINVAL;
2668         }
2669
2670         vf_id = filter->dst_id;
2671
2672         if (vf_id > pf->vf_num - 1 || !pf->vfs) {
2673                 PMD_DRV_LOG(ERR, "Invalid argument.");
2674                 return -EINVAL;
2675         }
2676         vf = &pf->vfs[vf_id];
2677
2678         if (add && is_same_ether_addr(new_mac, &(pf->dev_addr))) {
2679                 PMD_DRV_LOG(INFO, "Ignore adding permanent MAC address.");
2680                 return -EINVAL;
2681         }
2682
2683         if (add) {
2684                 (void)rte_memcpy(&old_mac, hw->mac.addr, ETHER_ADDR_LEN);
2685                 (void)rte_memcpy(hw->mac.addr, new_mac->addr_bytes,
2686                                 ETHER_ADDR_LEN);
2687                 (void)rte_memcpy(&mac_filter.mac_addr, &filter->mac_addr,
2688                                  ETHER_ADDR_LEN);
2689
2690                 mac_filter.filter_type = filter->filter_type;
2691                 ret = i40e_vsi_add_mac(vf->vsi, &mac_filter);
2692                 if (ret != I40E_SUCCESS) {
2693                         PMD_DRV_LOG(ERR, "Failed to add MAC filter.");
2694                         return -1;
2695                 }
2696                 ether_addr_copy(new_mac, &pf->dev_addr);
2697         } else {
2698                 (void)rte_memcpy(hw->mac.addr, hw->mac.perm_addr,
2699                                 ETHER_ADDR_LEN);
2700                 ret = i40e_vsi_delete_mac(vf->vsi, &filter->mac_addr);
2701                 if (ret != I40E_SUCCESS) {
2702                         PMD_DRV_LOG(ERR, "Failed to delete MAC filter.");
2703                         return -1;
2704                 }
2705
2706                 /* Clear device address as it has been removed */
2707                 if (is_same_ether_addr(&(pf->dev_addr), new_mac))
2708                         memset(&pf->dev_addr, 0, sizeof(struct ether_addr));
2709         }
2710
2711         return 0;
2712 }
2713
2714 /* MAC filter handle */
2715 static int
2716 i40e_mac_filter_handle(struct rte_eth_dev *dev, enum rte_filter_op filter_op,
2717                 void *arg)
2718 {
2719         struct i40e_pf *pf = I40E_DEV_PRIVATE_TO_PF(dev->data->dev_private);
2720         struct rte_eth_mac_filter *filter;
2721         struct i40e_hw *hw = I40E_PF_TO_HW(pf);
2722         int ret = I40E_NOT_SUPPORTED;
2723
2724         filter = (struct rte_eth_mac_filter *)(arg);
2725
2726         switch (filter_op) {
2727         case RTE_ETH_FILTER_NOP:
2728                 ret = I40E_SUCCESS;
2729                 break;
2730         case RTE_ETH_FILTER_ADD:
2731                 i40e_pf_disable_irq0(hw);
2732                 if (filter->is_vf)
2733                         ret = i40e_vf_mac_filter_set(pf, filter, 1);
2734                 i40e_pf_enable_irq0(hw);
2735                 break;
2736         case RTE_ETH_FILTER_DELETE:
2737                 i40e_pf_disable_irq0(hw);
2738                 if (filter->is_vf)
2739                         ret = i40e_vf_mac_filter_set(pf, filter, 0);
2740                 i40e_pf_enable_irq0(hw);
2741                 break;
2742         default:
2743                 PMD_DRV_LOG(ERR, "unknown operation %u", filter_op);
2744                 ret = I40E_ERR_PARAM;
2745                 break;
2746         }
2747
2748         return ret;
2749 }
2750
2751 static int
2752 i40e_get_rss_lut(struct i40e_vsi *vsi, uint8_t *lut, uint16_t lut_size)
2753 {
2754         struct i40e_pf *pf = I40E_VSI_TO_PF(vsi);
2755         struct i40e_hw *hw = I40E_VSI_TO_HW(vsi);
2756         int ret;
2757
2758         if (!lut)
2759                 return -EINVAL;
2760
2761         if (pf->flags & I40E_FLAG_RSS_AQ_CAPABLE) {
2762                 ret = i40e_aq_get_rss_lut(hw, vsi->vsi_id, TRUE,
2763                                           lut, lut_size);
2764                 if (ret) {
2765                         PMD_DRV_LOG(ERR, "Failed to get RSS lookup table");
2766                         return ret;
2767                 }
2768         } else {
2769                 uint32_t *lut_dw = (uint32_t *)lut;
2770                 uint16_t i, lut_size_dw = lut_size / 4;
2771
2772                 for (i = 0; i < lut_size_dw; i++)
2773                         lut_dw[i] = I40E_READ_REG(hw, I40E_PFQF_HLUT(i));
2774         }
2775
2776         return 0;
2777 }
2778
2779 static int
2780 i40e_set_rss_lut(struct i40e_vsi *vsi, uint8_t *lut, uint16_t lut_size)
2781 {
2782         struct i40e_pf *pf = I40E_VSI_TO_PF(vsi);
2783         struct i40e_hw *hw = I40E_VSI_TO_HW(vsi);
2784         int ret;
2785
2786         if (!vsi || !lut)
2787                 return -EINVAL;
2788
2789         if (pf->flags & I40E_FLAG_RSS_AQ_CAPABLE) {
2790                 ret = i40e_aq_set_rss_lut(hw, vsi->vsi_id, TRUE,
2791                                           lut, lut_size);
2792                 if (ret) {
2793                         PMD_DRV_LOG(ERR, "Failed to set RSS lookup table");
2794                         return ret;
2795                 }
2796         } else {
2797                 uint32_t *lut_dw = (uint32_t *)lut;
2798                 uint16_t i, lut_size_dw = lut_size / 4;
2799
2800                 for (i = 0; i < lut_size_dw; i++)
2801                         I40E_WRITE_REG(hw, I40E_PFQF_HLUT(i), lut_dw[i]);
2802                 I40E_WRITE_FLUSH(hw);
2803         }
2804
2805         return 0;
2806 }
2807
2808 static int
2809 i40e_dev_rss_reta_update(struct rte_eth_dev *dev,
2810                          struct rte_eth_rss_reta_entry64 *reta_conf,
2811                          uint16_t reta_size)
2812 {
2813         struct i40e_pf *pf = I40E_DEV_PRIVATE_TO_PF(dev->data->dev_private);
2814         uint16_t i, lut_size = pf->hash_lut_size;
2815         uint16_t idx, shift;
2816         uint8_t *lut;
2817         int ret;
2818
2819         if (reta_size != lut_size ||
2820                 reta_size > ETH_RSS_RETA_SIZE_512) {
2821                 PMD_DRV_LOG(ERR, "The size of hash lookup table configured "
2822                         "(%d) doesn't match the number hardware can supported "
2823                                         "(%d)\n", reta_size, lut_size);
2824                 return -EINVAL;
2825         }
2826
2827         lut = rte_zmalloc("i40e_rss_lut", reta_size, 0);
2828         if (!lut) {
2829                 PMD_DRV_LOG(ERR, "No memory can be allocated");
2830                 return -ENOMEM;
2831         }
2832         ret = i40e_get_rss_lut(pf->main_vsi, lut, reta_size);
2833         if (ret)
2834                 goto out;
2835         for (i = 0; i < reta_size; i++) {
2836                 idx = i / RTE_RETA_GROUP_SIZE;
2837                 shift = i % RTE_RETA_GROUP_SIZE;
2838                 if (reta_conf[idx].mask & (1ULL << shift))
2839                         lut[i] = reta_conf[idx].reta[shift];
2840         }
2841         ret = i40e_set_rss_lut(pf->main_vsi, lut, reta_size);
2842
2843 out:
2844         rte_free(lut);
2845
2846         return ret;
2847 }
2848
2849 static int
2850 i40e_dev_rss_reta_query(struct rte_eth_dev *dev,
2851                         struct rte_eth_rss_reta_entry64 *reta_conf,
2852                         uint16_t reta_size)
2853 {
2854         struct i40e_pf *pf = I40E_DEV_PRIVATE_TO_PF(dev->data->dev_private);
2855         uint16_t i, lut_size = pf->hash_lut_size;
2856         uint16_t idx, shift;
2857         uint8_t *lut;
2858         int ret;
2859
2860         if (reta_size != lut_size ||
2861                 reta_size > ETH_RSS_RETA_SIZE_512) {
2862                 PMD_DRV_LOG(ERR, "The size of hash lookup table configured "
2863                         "(%d) doesn't match the number hardware can supported "
2864                                         "(%d)\n", reta_size, lut_size);
2865                 return -EINVAL;
2866         }
2867
2868         lut = rte_zmalloc("i40e_rss_lut", reta_size, 0);
2869         if (!lut) {
2870                 PMD_DRV_LOG(ERR, "No memory can be allocated");
2871                 return -ENOMEM;
2872         }
2873
2874         ret = i40e_get_rss_lut(pf->main_vsi, lut, reta_size);
2875         if (ret)
2876                 goto out;
2877         for (i = 0; i < reta_size; i++) {
2878                 idx = i / RTE_RETA_GROUP_SIZE;
2879                 shift = i % RTE_RETA_GROUP_SIZE;
2880                 if (reta_conf[idx].mask & (1ULL << shift))
2881                         reta_conf[idx].reta[shift] = lut[i];
2882         }
2883
2884 out:
2885         rte_free(lut);
2886
2887         return ret;
2888 }
2889
2890 /**
2891  * i40e_allocate_dma_mem_d - specific memory alloc for shared code (base driver)
2892  * @hw:   pointer to the HW structure
2893  * @mem:  pointer to mem struct to fill out
2894  * @size: size of memory requested
2895  * @alignment: what to align the allocation to
2896  **/
2897 enum i40e_status_code
2898 i40e_allocate_dma_mem_d(__attribute__((unused)) struct i40e_hw *hw,
2899                         struct i40e_dma_mem *mem,
2900                         u64 size,
2901                         u32 alignment)
2902 {
2903         const struct rte_memzone *mz = NULL;
2904         char z_name[RTE_MEMZONE_NAMESIZE];
2905
2906         if (!mem)
2907                 return I40E_ERR_PARAM;
2908
2909         snprintf(z_name, sizeof(z_name), "i40e_dma_%"PRIu64, rte_rand());
2910 #ifdef RTE_LIBRTE_XEN_DOM0
2911         mz = rte_memzone_reserve_bounded(z_name, size, SOCKET_ID_ANY, 0,
2912                                          alignment, RTE_PGSIZE_2M);
2913 #else
2914         mz = rte_memzone_reserve_aligned(z_name, size, SOCKET_ID_ANY, 0,
2915                                          alignment);
2916 #endif
2917         if (!mz)
2918                 return I40E_ERR_NO_MEMORY;
2919
2920         mem->size = size;
2921         mem->va = mz->addr;
2922 #ifdef RTE_LIBRTE_XEN_DOM0
2923         mem->pa = rte_mem_phy2mch(mz->memseg_id, mz->phys_addr);
2924 #else
2925         mem->pa = mz->phys_addr;
2926 #endif
2927         mem->zone = (const void *)mz;
2928         PMD_DRV_LOG(DEBUG, "memzone %s allocated with physical address: "
2929                     "%"PRIu64, mz->name, mem->pa);
2930
2931         return I40E_SUCCESS;
2932 }
2933
2934 /**
2935  * i40e_free_dma_mem_d - specific memory free for shared code (base driver)
2936  * @hw:   pointer to the HW structure
2937  * @mem:  ptr to mem struct to free
2938  **/
2939 enum i40e_status_code
2940 i40e_free_dma_mem_d(__attribute__((unused)) struct i40e_hw *hw,
2941                     struct i40e_dma_mem *mem)
2942 {
2943         if (!mem)
2944                 return I40E_ERR_PARAM;
2945
2946         PMD_DRV_LOG(DEBUG, "memzone %s to be freed with physical address: "
2947                     "%"PRIu64, ((const struct rte_memzone *)mem->zone)->name,
2948                     mem->pa);
2949         rte_memzone_free((const struct rte_memzone *)mem->zone);
2950         mem->zone = NULL;
2951         mem->va = NULL;
2952         mem->pa = (u64)0;
2953
2954         return I40E_SUCCESS;
2955 }
2956
2957 /**
2958  * i40e_allocate_virt_mem_d - specific memory alloc for shared code (base driver)
2959  * @hw:   pointer to the HW structure
2960  * @mem:  pointer to mem struct to fill out
2961  * @size: size of memory requested
2962  **/
2963 enum i40e_status_code
2964 i40e_allocate_virt_mem_d(__attribute__((unused)) struct i40e_hw *hw,
2965                          struct i40e_virt_mem *mem,
2966                          u32 size)
2967 {
2968         if (!mem)
2969                 return I40E_ERR_PARAM;
2970
2971         mem->size = size;
2972         mem->va = rte_zmalloc("i40e", size, 0);
2973
2974         if (mem->va)
2975                 return I40E_SUCCESS;
2976         else
2977                 return I40E_ERR_NO_MEMORY;
2978 }
2979
2980 /**
2981  * i40e_free_virt_mem_d - specific memory free for shared code (base driver)
2982  * @hw:   pointer to the HW structure
2983  * @mem:  pointer to mem struct to free
2984  **/
2985 enum i40e_status_code
2986 i40e_free_virt_mem_d(__attribute__((unused)) struct i40e_hw *hw,
2987                      struct i40e_virt_mem *mem)
2988 {
2989         if (!mem)
2990                 return I40E_ERR_PARAM;
2991
2992         rte_free(mem->va);
2993         mem->va = NULL;
2994
2995         return I40E_SUCCESS;
2996 }
2997
2998 void
2999 i40e_init_spinlock_d(struct i40e_spinlock *sp)
3000 {
3001         rte_spinlock_init(&sp->spinlock);
3002 }
3003
3004 void
3005 i40e_acquire_spinlock_d(struct i40e_spinlock *sp)
3006 {
3007         rte_spinlock_lock(&sp->spinlock);
3008 }
3009
3010 void
3011 i40e_release_spinlock_d(struct i40e_spinlock *sp)
3012 {
3013         rte_spinlock_unlock(&sp->spinlock);
3014 }
3015
3016 void
3017 i40e_destroy_spinlock_d(__attribute__((unused)) struct i40e_spinlock *sp)
3018 {
3019         return;
3020 }
3021
3022 /**
3023  * Get the hardware capabilities, which will be parsed
3024  * and saved into struct i40e_hw.
3025  */
3026 static int
3027 i40e_get_cap(struct i40e_hw *hw)
3028 {
3029         struct i40e_aqc_list_capabilities_element_resp *buf;
3030         uint16_t len, size = 0;
3031         int ret;
3032
3033         /* Calculate a huge enough buff for saving response data temporarily */
3034         len = sizeof(struct i40e_aqc_list_capabilities_element_resp) *
3035                                                 I40E_MAX_CAP_ELE_NUM;
3036         buf = rte_zmalloc("i40e", len, 0);
3037         if (!buf) {
3038                 PMD_DRV_LOG(ERR, "Failed to allocate memory");
3039                 return I40E_ERR_NO_MEMORY;
3040         }
3041
3042         /* Get, parse the capabilities and save it to hw */
3043         ret = i40e_aq_discover_capabilities(hw, buf, len, &size,
3044                         i40e_aqc_opc_list_func_capabilities, NULL);
3045         if (ret != I40E_SUCCESS)
3046                 PMD_DRV_LOG(ERR, "Failed to discover capabilities");
3047
3048         /* Free the temporary buffer after being used */
3049         rte_free(buf);
3050
3051         return ret;
3052 }
3053
3054 static int
3055 i40e_pf_parameter_init(struct rte_eth_dev *dev)
3056 {
3057         struct i40e_pf *pf = I40E_DEV_PRIVATE_TO_PF(dev->data->dev_private);
3058         struct i40e_hw *hw = I40E_PF_TO_HW(pf);
3059         uint16_t qp_count = 0, vsi_count = 0;
3060
3061         if (dev->pci_dev->max_vfs && !hw->func_caps.sr_iov_1_1) {
3062                 PMD_INIT_LOG(ERR, "HW configuration doesn't support SRIOV");
3063                 return -EINVAL;
3064         }
3065         /* Add the parameter init for LFC */
3066         pf->fc_conf.pause_time = I40E_DEFAULT_PAUSE_TIME;
3067         pf->fc_conf.high_water[I40E_MAX_TRAFFIC_CLASS] = I40E_DEFAULT_HIGH_WATER;
3068         pf->fc_conf.low_water[I40E_MAX_TRAFFIC_CLASS] = I40E_DEFAULT_LOW_WATER;
3069
3070         pf->flags = I40E_FLAG_HEADER_SPLIT_DISABLED;
3071         pf->max_num_vsi = hw->func_caps.num_vsis;
3072         pf->lan_nb_qp_max = RTE_LIBRTE_I40E_QUEUE_NUM_PER_PF;
3073         pf->vmdq_nb_qp_max = RTE_LIBRTE_I40E_QUEUE_NUM_PER_VM;
3074         pf->vf_nb_qp_max = RTE_LIBRTE_I40E_QUEUE_NUM_PER_VF;
3075
3076         /* FDir queue/VSI allocation */
3077         pf->fdir_qp_offset = 0;
3078         if (hw->func_caps.fd) {
3079                 pf->flags |= I40E_FLAG_FDIR;
3080                 pf->fdir_nb_qps = I40E_DEFAULT_QP_NUM_FDIR;
3081         } else {
3082                 pf->fdir_nb_qps = 0;
3083         }
3084         qp_count += pf->fdir_nb_qps;
3085         vsi_count += 1;
3086
3087         /* LAN queue/VSI allocation */
3088         pf->lan_qp_offset = pf->fdir_qp_offset + pf->fdir_nb_qps;
3089         if (!hw->func_caps.rss) {
3090                 pf->lan_nb_qps = 1;
3091         } else {
3092                 pf->flags |= I40E_FLAG_RSS;
3093                 if (hw->mac.type == I40E_MAC_X722)
3094                         pf->flags |= I40E_FLAG_RSS_AQ_CAPABLE;
3095                 pf->lan_nb_qps = pf->lan_nb_qp_max;
3096         }
3097         qp_count += pf->lan_nb_qps;
3098         vsi_count += 1;
3099
3100         /* VF queue/VSI allocation */
3101         pf->vf_qp_offset = pf->lan_qp_offset + pf->lan_nb_qps;
3102         if (hw->func_caps.sr_iov_1_1 && dev->pci_dev->max_vfs) {
3103                 pf->flags |= I40E_FLAG_SRIOV;
3104                 pf->vf_nb_qps = RTE_LIBRTE_I40E_QUEUE_NUM_PER_VF;
3105                 pf->vf_num = dev->pci_dev->max_vfs;
3106                 PMD_DRV_LOG(DEBUG, "%u VF VSIs, %u queues per VF VSI, "
3107                             "in total %u queues", pf->vf_num, pf->vf_nb_qps,
3108                             pf->vf_nb_qps * pf->vf_num);
3109         } else {
3110                 pf->vf_nb_qps = 0;
3111                 pf->vf_num = 0;
3112         }
3113         qp_count += pf->vf_nb_qps * pf->vf_num;
3114         vsi_count += pf->vf_num;
3115
3116         /* VMDq queue/VSI allocation */
3117         pf->vmdq_qp_offset = pf->vf_qp_offset + pf->vf_nb_qps * pf->vf_num;
3118         pf->vmdq_nb_qps = 0;
3119         pf->max_nb_vmdq_vsi = 0;
3120         if (hw->func_caps.vmdq) {
3121                 if (qp_count < hw->func_caps.num_tx_qp) {
3122                         pf->max_nb_vmdq_vsi = (hw->func_caps.num_tx_qp -
3123                                 qp_count) / pf->vmdq_nb_qp_max;
3124
3125                         /* Limit the maximum number of VMDq vsi to the maximum
3126                          * ethdev can support
3127                          */
3128                         pf->max_nb_vmdq_vsi = RTE_MIN(pf->max_nb_vmdq_vsi,
3129                                 ETH_64_POOLS);
3130                         if (pf->max_nb_vmdq_vsi) {
3131                                 pf->flags |= I40E_FLAG_VMDQ;
3132                                 pf->vmdq_nb_qps = pf->vmdq_nb_qp_max;
3133                                 PMD_DRV_LOG(DEBUG, "%u VMDQ VSIs, %u queues "
3134                                             "per VMDQ VSI, in total %u queues",
3135                                             pf->max_nb_vmdq_vsi,
3136                                             pf->vmdq_nb_qps, pf->vmdq_nb_qps *
3137                                             pf->max_nb_vmdq_vsi);
3138                         } else {
3139                                 PMD_DRV_LOG(INFO, "No enough queues left for "
3140                                             "VMDq");
3141                         }
3142                 } else {
3143                         PMD_DRV_LOG(INFO, "No queue left for VMDq");
3144                 }
3145         }
3146         qp_count += pf->vmdq_nb_qps * pf->max_nb_vmdq_vsi;
3147         vsi_count += pf->max_nb_vmdq_vsi;
3148
3149         if (hw->func_caps.dcb)
3150                 pf->flags |= I40E_FLAG_DCB;
3151
3152         if (qp_count > hw->func_caps.num_tx_qp) {
3153                 PMD_DRV_LOG(ERR, "Failed to allocate %u queues, which exceeds "
3154                             "the hardware maximum %u", qp_count,
3155                             hw->func_caps.num_tx_qp);
3156                 return -EINVAL;
3157         }
3158         if (vsi_count > hw->func_caps.num_vsis) {
3159                 PMD_DRV_LOG(ERR, "Failed to allocate %u VSIs, which exceeds "
3160                             "the hardware maximum %u", vsi_count,
3161                             hw->func_caps.num_vsis);
3162                 return -EINVAL;
3163         }
3164
3165         return 0;
3166 }
3167
3168 static int
3169 i40e_pf_get_switch_config(struct i40e_pf *pf)
3170 {
3171         struct i40e_hw *hw = I40E_PF_TO_HW(pf);
3172         struct i40e_aqc_get_switch_config_resp *switch_config;
3173         struct i40e_aqc_switch_config_element_resp *element;
3174         uint16_t start_seid = 0, num_reported;
3175         int ret;
3176
3177         switch_config = (struct i40e_aqc_get_switch_config_resp *)\
3178                         rte_zmalloc("i40e", I40E_AQ_LARGE_BUF, 0);
3179         if (!switch_config) {
3180                 PMD_DRV_LOG(ERR, "Failed to allocated memory");
3181                 return -ENOMEM;
3182         }
3183
3184         /* Get the switch configurations */
3185         ret = i40e_aq_get_switch_config(hw, switch_config,
3186                 I40E_AQ_LARGE_BUF, &start_seid, NULL);
3187         if (ret != I40E_SUCCESS) {
3188                 PMD_DRV_LOG(ERR, "Failed to get switch configurations");
3189                 goto fail;
3190         }
3191         num_reported = rte_le_to_cpu_16(switch_config->header.num_reported);
3192         if (num_reported != 1) { /* The number should be 1 */
3193                 PMD_DRV_LOG(ERR, "Wrong number of switch config reported");
3194                 goto fail;
3195         }
3196
3197         /* Parse the switch configuration elements */
3198         element = &(switch_config->element[0]);
3199         if (element->element_type == I40E_SWITCH_ELEMENT_TYPE_VSI) {
3200                 pf->mac_seid = rte_le_to_cpu_16(element->uplink_seid);
3201                 pf->main_vsi_seid = rte_le_to_cpu_16(element->seid);
3202         } else
3203                 PMD_DRV_LOG(INFO, "Unknown element type");
3204
3205 fail:
3206         rte_free(switch_config);
3207
3208         return ret;
3209 }
3210
3211 static int
3212 i40e_res_pool_init (struct i40e_res_pool_info *pool, uint32_t base,
3213                         uint32_t num)
3214 {
3215         struct pool_entry *entry;
3216
3217         if (pool == NULL || num == 0)
3218                 return -EINVAL;
3219
3220         entry = rte_zmalloc("i40e", sizeof(*entry), 0);
3221         if (entry == NULL) {
3222                 PMD_DRV_LOG(ERR, "Failed to allocate memory for resource pool");
3223                 return -ENOMEM;
3224         }
3225
3226         /* queue heap initialize */
3227         pool->num_free = num;
3228         pool->num_alloc = 0;
3229         pool->base = base;
3230         LIST_INIT(&pool->alloc_list);
3231         LIST_INIT(&pool->free_list);
3232
3233         /* Initialize element  */
3234         entry->base = 0;
3235         entry->len = num;
3236
3237         LIST_INSERT_HEAD(&pool->free_list, entry, next);
3238         return 0;
3239 }
3240
3241 static void
3242 i40e_res_pool_destroy(struct i40e_res_pool_info *pool)
3243 {
3244         struct pool_entry *entry;
3245
3246         if (pool == NULL)
3247                 return;
3248
3249         LIST_FOREACH(entry, &pool->alloc_list, next) {
3250                 LIST_REMOVE(entry, next);
3251                 rte_free(entry);
3252         }
3253
3254         LIST_FOREACH(entry, &pool->free_list, next) {
3255                 LIST_REMOVE(entry, next);
3256                 rte_free(entry);
3257         }
3258
3259         pool->num_free = 0;
3260         pool->num_alloc = 0;
3261         pool->base = 0;
3262         LIST_INIT(&pool->alloc_list);
3263         LIST_INIT(&pool->free_list);
3264 }
3265
3266 static int
3267 i40e_res_pool_free(struct i40e_res_pool_info *pool,
3268                        uint32_t base)
3269 {
3270         struct pool_entry *entry, *next, *prev, *valid_entry = NULL;
3271         uint32_t pool_offset;
3272         int insert;
3273
3274         if (pool == NULL) {
3275                 PMD_DRV_LOG(ERR, "Invalid parameter");
3276                 return -EINVAL;
3277         }
3278
3279         pool_offset = base - pool->base;
3280         /* Lookup in alloc list */
3281         LIST_FOREACH(entry, &pool->alloc_list, next) {
3282                 if (entry->base == pool_offset) {
3283                         valid_entry = entry;
3284                         LIST_REMOVE(entry, next);
3285                         break;
3286                 }
3287         }
3288
3289         /* Not find, return */
3290         if (valid_entry == NULL) {
3291                 PMD_DRV_LOG(ERR, "Failed to find entry");
3292                 return -EINVAL;
3293         }
3294
3295         /**
3296          * Found it, move it to free list  and try to merge.
3297          * In order to make merge easier, always sort it by qbase.
3298          * Find adjacent prev and last entries.
3299          */
3300         prev = next = NULL;
3301         LIST_FOREACH(entry, &pool->free_list, next) {
3302                 if (entry->base > valid_entry->base) {
3303                         next = entry;
3304                         break;
3305                 }
3306                 prev = entry;
3307         }
3308
3309         insert = 0;
3310         /* Try to merge with next one*/
3311         if (next != NULL) {
3312                 /* Merge with next one */
3313                 if (valid_entry->base + valid_entry->len == next->base) {
3314                         next->base = valid_entry->base;
3315                         next->len += valid_entry->len;
3316                         rte_free(valid_entry);
3317                         valid_entry = next;
3318                         insert = 1;
3319                 }
3320         }
3321
3322         if (prev != NULL) {
3323                 /* Merge with previous one */
3324                 if (prev->base + prev->len == valid_entry->base) {
3325                         prev->len += valid_entry->len;
3326                         /* If it merge with next one, remove next node */
3327                         if (insert == 1) {
3328                                 LIST_REMOVE(valid_entry, next);
3329                                 rte_free(valid_entry);
3330                         } else {
3331                                 rte_free(valid_entry);
3332                                 insert = 1;
3333                         }
3334                 }
3335         }
3336
3337         /* Not find any entry to merge, insert */
3338         if (insert == 0) {
3339                 if (prev != NULL)
3340                         LIST_INSERT_AFTER(prev, valid_entry, next);
3341                 else if (next != NULL)
3342                         LIST_INSERT_BEFORE(next, valid_entry, next);
3343                 else /* It's empty list, insert to head */
3344                         LIST_INSERT_HEAD(&pool->free_list, valid_entry, next);
3345         }
3346
3347         pool->num_free += valid_entry->len;
3348         pool->num_alloc -= valid_entry->len;
3349
3350         return 0;
3351 }
3352
3353 static int
3354 i40e_res_pool_alloc(struct i40e_res_pool_info *pool,
3355                        uint16_t num)
3356 {
3357         struct pool_entry *entry, *valid_entry;
3358
3359         if (pool == NULL || num == 0) {
3360                 PMD_DRV_LOG(ERR, "Invalid parameter");
3361                 return -EINVAL;
3362         }
3363
3364         if (pool->num_free < num) {
3365                 PMD_DRV_LOG(ERR, "No resource. ask:%u, available:%u",
3366                             num, pool->num_free);
3367                 return -ENOMEM;
3368         }
3369
3370         valid_entry = NULL;
3371         /* Lookup  in free list and find most fit one */
3372         LIST_FOREACH(entry, &pool->free_list, next) {
3373                 if (entry->len >= num) {
3374                         /* Find best one */
3375                         if (entry->len == num) {
3376                                 valid_entry = entry;
3377                                 break;
3378                         }
3379                         if (valid_entry == NULL || valid_entry->len > entry->len)
3380                                 valid_entry = entry;
3381                 }
3382         }
3383
3384         /* Not find one to satisfy the request, return */
3385         if (valid_entry == NULL) {
3386                 PMD_DRV_LOG(ERR, "No valid entry found");
3387                 return -ENOMEM;
3388         }
3389         /**
3390          * The entry have equal queue number as requested,
3391          * remove it from alloc_list.
3392          */
3393         if (valid_entry->len == num) {
3394                 LIST_REMOVE(valid_entry, next);
3395         } else {
3396                 /**
3397                  * The entry have more numbers than requested,
3398                  * create a new entry for alloc_list and minus its
3399                  * queue base and number in free_list.
3400                  */
3401                 entry = rte_zmalloc("res_pool", sizeof(*entry), 0);
3402                 if (entry == NULL) {
3403                         PMD_DRV_LOG(ERR, "Failed to allocate memory for "
3404                                     "resource pool");
3405                         return -ENOMEM;
3406                 }
3407                 entry->base = valid_entry->base;
3408                 entry->len = num;
3409                 valid_entry->base += num;
3410                 valid_entry->len -= num;
3411                 valid_entry = entry;
3412         }
3413
3414         /* Insert it into alloc list, not sorted */
3415         LIST_INSERT_HEAD(&pool->alloc_list, valid_entry, next);
3416
3417         pool->num_free -= valid_entry->len;
3418         pool->num_alloc += valid_entry->len;
3419
3420         return (valid_entry->base + pool->base);
3421 }
3422
3423 /**
3424  * bitmap_is_subset - Check whether src2 is subset of src1
3425  **/
3426 static inline int
3427 bitmap_is_subset(uint8_t src1, uint8_t src2)
3428 {
3429         return !((src1 ^ src2) & src2);
3430 }
3431
3432 static enum i40e_status_code
3433 validate_tcmap_parameter(struct i40e_vsi *vsi, uint8_t enabled_tcmap)
3434 {
3435         struct i40e_hw *hw = I40E_VSI_TO_HW(vsi);
3436
3437         /* If DCB is not supported, only default TC is supported */
3438         if (!hw->func_caps.dcb && enabled_tcmap != I40E_DEFAULT_TCMAP) {
3439                 PMD_DRV_LOG(ERR, "DCB is not enabled, only TC0 is supported");
3440                 return I40E_NOT_SUPPORTED;
3441         }
3442
3443         if (!bitmap_is_subset(hw->func_caps.enabled_tcmap, enabled_tcmap)) {
3444                 PMD_DRV_LOG(ERR, "Enabled TC map 0x%x not applicable to "
3445                             "HW support 0x%x", hw->func_caps.enabled_tcmap,
3446                             enabled_tcmap);
3447                 return I40E_NOT_SUPPORTED;
3448         }
3449         return I40E_SUCCESS;
3450 }
3451
3452 int
3453 i40e_vsi_vlan_pvid_set(struct i40e_vsi *vsi,
3454                                 struct i40e_vsi_vlan_pvid_info *info)
3455 {
3456         struct i40e_hw *hw;
3457         struct i40e_vsi_context ctxt;
3458         uint8_t vlan_flags = 0;
3459         int ret;
3460
3461         if (vsi == NULL || info == NULL) {
3462                 PMD_DRV_LOG(ERR, "invalid parameters");
3463                 return I40E_ERR_PARAM;
3464         }
3465
3466         if (info->on) {
3467                 vsi->info.pvid = info->config.pvid;
3468                 /**
3469                  * If insert pvid is enabled, only tagged pkts are
3470                  * allowed to be sent out.
3471                  */
3472                 vlan_flags |= I40E_AQ_VSI_PVLAN_INSERT_PVID |
3473                                 I40E_AQ_VSI_PVLAN_MODE_TAGGED;
3474         } else {
3475                 vsi->info.pvid = 0;
3476                 if (info->config.reject.tagged == 0)
3477                         vlan_flags |= I40E_AQ_VSI_PVLAN_MODE_TAGGED;
3478
3479                 if (info->config.reject.untagged == 0)
3480                         vlan_flags |= I40E_AQ_VSI_PVLAN_MODE_UNTAGGED;
3481         }
3482         vsi->info.port_vlan_flags &= ~(I40E_AQ_VSI_PVLAN_INSERT_PVID |
3483                                         I40E_AQ_VSI_PVLAN_MODE_MASK);
3484         vsi->info.port_vlan_flags |= vlan_flags;
3485         vsi->info.valid_sections =
3486                 rte_cpu_to_le_16(I40E_AQ_VSI_PROP_VLAN_VALID);
3487         memset(&ctxt, 0, sizeof(ctxt));
3488         (void)rte_memcpy(&ctxt.info, &vsi->info, sizeof(vsi->info));
3489         ctxt.seid = vsi->seid;
3490
3491         hw = I40E_VSI_TO_HW(vsi);
3492         ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
3493         if (ret != I40E_SUCCESS)
3494                 PMD_DRV_LOG(ERR, "Failed to update VSI params");
3495
3496         return ret;
3497 }
3498
3499 static int
3500 i40e_vsi_update_tc_bandwidth(struct i40e_vsi *vsi, uint8_t enabled_tcmap)
3501 {
3502         struct i40e_hw *hw = I40E_VSI_TO_HW(vsi);
3503         int i, ret;
3504         struct i40e_aqc_configure_vsi_tc_bw_data tc_bw_data;
3505
3506         ret = validate_tcmap_parameter(vsi, enabled_tcmap);
3507         if (ret != I40E_SUCCESS)
3508                 return ret;
3509
3510         if (!vsi->seid) {
3511                 PMD_DRV_LOG(ERR, "seid not valid");
3512                 return -EINVAL;
3513         }
3514
3515         memset(&tc_bw_data, 0, sizeof(tc_bw_data));
3516         tc_bw_data.tc_valid_bits = enabled_tcmap;
3517         for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
3518                 tc_bw_data.tc_bw_credits[i] =
3519                         (enabled_tcmap & (1 << i)) ? 1 : 0;
3520
3521         ret = i40e_aq_config_vsi_tc_bw(hw, vsi->seid, &tc_bw_data, NULL);
3522         if (ret != I40E_SUCCESS) {
3523                 PMD_DRV_LOG(ERR, "Failed to configure TC BW");
3524                 return ret;
3525         }
3526
3527         (void)rte_memcpy(vsi->info.qs_handle, tc_bw_data.qs_handles,
3528                                         sizeof(vsi->info.qs_handle));
3529         return I40E_SUCCESS;
3530 }
3531
3532 static enum i40e_status_code
3533 i40e_vsi_config_tc_queue_mapping(struct i40e_vsi *vsi,
3534                                  struct i40e_aqc_vsi_properties_data *info,
3535                                  uint8_t enabled_tcmap)
3536 {
3537         enum i40e_status_code ret;
3538         int i, total_tc = 0;
3539         uint16_t qpnum_per_tc, bsf, qp_idx;
3540
3541         ret = validate_tcmap_parameter(vsi, enabled_tcmap);
3542         if (ret != I40E_SUCCESS)
3543                 return ret;
3544
3545         for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
3546                 if (enabled_tcmap & (1 << i))
3547                         total_tc++;
3548         vsi->enabled_tc = enabled_tcmap;
3549
3550         /* Number of queues per enabled TC */
3551         qpnum_per_tc = i40e_align_floor(vsi->nb_qps / total_tc);
3552         qpnum_per_tc = RTE_MIN(qpnum_per_tc, I40E_MAX_Q_PER_TC);
3553         bsf = rte_bsf32(qpnum_per_tc);
3554
3555         /* Adjust the queue number to actual queues that can be applied */
3556         if (!(vsi->type == I40E_VSI_MAIN && total_tc == 1))
3557                 vsi->nb_qps = qpnum_per_tc * total_tc;
3558
3559         /**
3560          * Configure TC and queue mapping parameters, for enabled TC,
3561          * allocate qpnum_per_tc queues to this traffic. For disabled TC,
3562          * default queue will serve it.
3563          */
3564         qp_idx = 0;
3565         for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
3566                 if (vsi->enabled_tc & (1 << i)) {
3567                         info->tc_mapping[i] = rte_cpu_to_le_16((qp_idx <<
3568                                         I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
3569                                 (bsf << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT));
3570                         qp_idx += qpnum_per_tc;
3571                 } else
3572                         info->tc_mapping[i] = 0;
3573         }
3574
3575         /* Associate queue number with VSI */
3576         if (vsi->type == I40E_VSI_SRIOV) {
3577                 info->mapping_flags |=
3578                         rte_cpu_to_le_16(I40E_AQ_VSI_QUE_MAP_NONCONTIG);
3579                 for (i = 0; i < vsi->nb_qps; i++)
3580                         info->queue_mapping[i] =
3581                                 rte_cpu_to_le_16(vsi->base_queue + i);
3582         } else {
3583                 info->mapping_flags |=
3584                         rte_cpu_to_le_16(I40E_AQ_VSI_QUE_MAP_CONTIG);
3585                 info->queue_mapping[0] = rte_cpu_to_le_16(vsi->base_queue);
3586         }
3587         info->valid_sections |=
3588                 rte_cpu_to_le_16(I40E_AQ_VSI_PROP_QUEUE_MAP_VALID);
3589
3590         return I40E_SUCCESS;
3591 }
3592
3593 static int
3594 i40e_veb_release(struct i40e_veb *veb)
3595 {
3596         struct i40e_vsi *vsi;
3597         struct i40e_hw *hw;
3598
3599         if (veb == NULL || veb->associate_vsi == NULL)
3600                 return -EINVAL;
3601
3602         if (!TAILQ_EMPTY(&veb->head)) {
3603                 PMD_DRV_LOG(ERR, "VEB still has VSI attached, can't remove");
3604                 return -EACCES;
3605         }
3606
3607         vsi = veb->associate_vsi;
3608         hw = I40E_VSI_TO_HW(vsi);
3609
3610         vsi->uplink_seid = veb->uplink_seid;
3611         i40e_aq_delete_element(hw, veb->seid, NULL);
3612         rte_free(veb);
3613         vsi->veb = NULL;
3614         return I40E_SUCCESS;
3615 }
3616
3617 /* Setup a veb */
3618 static struct i40e_veb *
3619 i40e_veb_setup(struct i40e_pf *pf, struct i40e_vsi *vsi)
3620 {
3621         struct i40e_veb *veb;
3622         int ret;
3623         struct i40e_hw *hw;
3624
3625         if (NULL == pf || vsi == NULL) {
3626                 PMD_DRV_LOG(ERR, "veb setup failed, "
3627                             "associated VSI shouldn't null");
3628                 return NULL;
3629         }
3630         hw = I40E_PF_TO_HW(pf);
3631
3632         veb = rte_zmalloc("i40e_veb", sizeof(struct i40e_veb), 0);
3633         if (!veb) {
3634                 PMD_DRV_LOG(ERR, "Failed to allocate memory for veb");
3635                 goto fail;
3636         }
3637
3638         veb->associate_vsi = vsi;
3639         TAILQ_INIT(&veb->head);
3640         veb->uplink_seid = vsi->uplink_seid;
3641
3642         ret = i40e_aq_add_veb(hw, veb->uplink_seid, vsi->seid,
3643                 I40E_DEFAULT_TCMAP, false, false, &veb->seid, NULL);
3644
3645         if (ret != I40E_SUCCESS) {
3646                 PMD_DRV_LOG(ERR, "Add veb failed, aq_err: %d",
3647                             hw->aq.asq_last_status);
3648                 goto fail;
3649         }
3650
3651         /* get statistics index */
3652         ret = i40e_aq_get_veb_parameters(hw, veb->seid, NULL, NULL,
3653                                 &veb->stats_idx, NULL, NULL, NULL);
3654         if (ret != I40E_SUCCESS) {
3655                 PMD_DRV_LOG(ERR, "Get veb statics index failed, aq_err: %d",
3656                             hw->aq.asq_last_status);
3657                 goto fail;
3658         }
3659
3660         /* Get VEB bandwidth, to be implemented */
3661         /* Now associated vsi binding to the VEB, set uplink to this VEB */
3662         vsi->uplink_seid = veb->seid;
3663
3664         return veb;
3665 fail:
3666         rte_free(veb);
3667         return NULL;
3668 }
3669
3670 int
3671 i40e_vsi_release(struct i40e_vsi *vsi)
3672 {
3673         struct i40e_pf *pf;
3674         struct i40e_hw *hw;
3675         struct i40e_vsi_list *vsi_list;
3676         int ret;
3677         struct i40e_mac_filter *f;
3678
3679         if (!vsi)
3680                 return I40E_SUCCESS;
3681
3682         pf = I40E_VSI_TO_PF(vsi);
3683         hw = I40E_VSI_TO_HW(vsi);
3684
3685         /* VSI has child to attach, release child first */
3686         if (vsi->veb) {
3687                 TAILQ_FOREACH(vsi_list, &vsi->veb->head, list) {
3688                         if (i40e_vsi_release(vsi_list->vsi) != I40E_SUCCESS)
3689                                 return -1;
3690                         TAILQ_REMOVE(&vsi->veb->head, vsi_list, list);
3691                 }
3692                 i40e_veb_release(vsi->veb);
3693         }
3694
3695         /* Remove all macvlan filters of the VSI */
3696         i40e_vsi_remove_all_macvlan_filter(vsi);
3697         TAILQ_FOREACH(f, &vsi->mac_list, next)
3698                 rte_free(f);
3699
3700         if (vsi->type != I40E_VSI_MAIN) {
3701                 /* Remove vsi from parent's sibling list */
3702                 if (vsi->parent_vsi == NULL || vsi->parent_vsi->veb == NULL) {
3703                         PMD_DRV_LOG(ERR, "VSI's parent VSI is NULL");
3704                         return I40E_ERR_PARAM;
3705                 }
3706                 TAILQ_REMOVE(&vsi->parent_vsi->veb->head,
3707                                 &vsi->sib_vsi_list, list);
3708
3709                 /* Remove all switch element of the VSI */
3710                 ret = i40e_aq_delete_element(hw, vsi->seid, NULL);
3711                 if (ret != I40E_SUCCESS)
3712                         PMD_DRV_LOG(ERR, "Failed to delete element");
3713         }
3714         i40e_res_pool_free(&pf->qp_pool, vsi->base_queue);
3715
3716         if (vsi->type != I40E_VSI_SRIOV)
3717                 i40e_res_pool_free(&pf->msix_pool, vsi->msix_intr);
3718         rte_free(vsi);
3719
3720         return I40E_SUCCESS;
3721 }
3722
3723 static int
3724 i40e_update_default_filter_setting(struct i40e_vsi *vsi)
3725 {
3726         struct i40e_hw *hw = I40E_VSI_TO_HW(vsi);
3727         struct i40e_aqc_remove_macvlan_element_data def_filter;
3728         struct i40e_mac_filter_info filter;
3729         int ret;
3730
3731         if (vsi->type != I40E_VSI_MAIN)
3732                 return I40E_ERR_CONFIG;
3733         memset(&def_filter, 0, sizeof(def_filter));
3734         (void)rte_memcpy(def_filter.mac_addr, hw->mac.perm_addr,
3735                                         ETH_ADDR_LEN);
3736         def_filter.vlan_tag = 0;
3737         def_filter.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH |
3738                                 I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
3739         ret = i40e_aq_remove_macvlan(hw, vsi->seid, &def_filter, 1, NULL);
3740         if (ret != I40E_SUCCESS) {
3741                 struct i40e_mac_filter *f;
3742                 struct ether_addr *mac;
3743
3744                 PMD_DRV_LOG(WARNING, "Cannot remove the default "
3745                             "macvlan filter");
3746                 /* It needs to add the permanent mac into mac list */
3747                 f = rte_zmalloc("macv_filter", sizeof(*f), 0);
3748                 if (f == NULL) {
3749                         PMD_DRV_LOG(ERR, "failed to allocate memory");
3750                         return I40E_ERR_NO_MEMORY;
3751                 }
3752                 mac = &f->mac_info.mac_addr;
3753                 (void)rte_memcpy(&mac->addr_bytes, hw->mac.perm_addr,
3754                                 ETH_ADDR_LEN);
3755                 f->mac_info.filter_type = RTE_MACVLAN_PERFECT_MATCH;
3756                 TAILQ_INSERT_TAIL(&vsi->mac_list, f, next);
3757                 vsi->mac_num++;
3758
3759                 return ret;
3760         }
3761         (void)rte_memcpy(&filter.mac_addr,
3762                 (struct ether_addr *)(hw->mac.perm_addr), ETH_ADDR_LEN);
3763         filter.filter_type = RTE_MACVLAN_PERFECT_MATCH;
3764         return i40e_vsi_add_mac(vsi, &filter);
3765 }
3766
3767 static int
3768 i40e_vsi_dump_bw_config(struct i40e_vsi *vsi)
3769 {
3770         struct i40e_aqc_query_vsi_bw_config_resp bw_config;
3771         struct i40e_aqc_query_vsi_ets_sla_config_resp ets_sla_config;
3772         struct i40e_hw *hw = &vsi->adapter->hw;
3773         i40e_status ret;
3774         int i;
3775
3776         memset(&bw_config, 0, sizeof(bw_config));
3777         ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid, &bw_config, NULL);
3778         if (ret != I40E_SUCCESS) {
3779                 PMD_DRV_LOG(ERR, "VSI failed to get bandwidth configuration %u",
3780                             hw->aq.asq_last_status);
3781                 return ret;
3782         }
3783
3784         memset(&ets_sla_config, 0, sizeof(ets_sla_config));
3785         ret = i40e_aq_query_vsi_ets_sla_config(hw, vsi->seid,
3786                                         &ets_sla_config, NULL);
3787         if (ret != I40E_SUCCESS) {
3788                 PMD_DRV_LOG(ERR, "VSI failed to get TC bandwdith "
3789                             "configuration %u", hw->aq.asq_last_status);
3790                 return ret;
3791         }
3792
3793         /* Not store the info yet, just print out */
3794         PMD_DRV_LOG(INFO, "VSI bw limit:%u", bw_config.port_bw_limit);
3795         PMD_DRV_LOG(INFO, "VSI max_bw:%u", bw_config.max_bw);
3796         for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
3797                 PMD_DRV_LOG(INFO, "\tVSI TC%u:share credits %u", i,
3798                             ets_sla_config.share_credits[i]);
3799                 PMD_DRV_LOG(INFO, "\tVSI TC%u:credits %u", i,
3800                             rte_le_to_cpu_16(ets_sla_config.credits[i]));
3801                 PMD_DRV_LOG(INFO, "\tVSI TC%u: max credits: %u", i,
3802                             rte_le_to_cpu_16(ets_sla_config.credits[i / 4]) >>
3803                             (i * 4));
3804         }
3805
3806         return 0;
3807 }
3808
3809 /* Setup a VSI */
3810 struct i40e_vsi *
3811 i40e_vsi_setup(struct i40e_pf *pf,
3812                enum i40e_vsi_type type,
3813                struct i40e_vsi *uplink_vsi,
3814                uint16_t user_param)
3815 {
3816         struct i40e_hw *hw = I40E_PF_TO_HW(pf);
3817         struct i40e_vsi *vsi;
3818         struct i40e_mac_filter_info filter;
3819         int ret;
3820         struct i40e_vsi_context ctxt;
3821         struct ether_addr broadcast =
3822                 {.addr_bytes = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff}};
3823
3824         if (type != I40E_VSI_MAIN && uplink_vsi == NULL) {
3825                 PMD_DRV_LOG(ERR, "VSI setup failed, "
3826                             "VSI link shouldn't be NULL");
3827                 return NULL;
3828         }
3829
3830         if (type == I40E_VSI_MAIN && uplink_vsi != NULL) {
3831                 PMD_DRV_LOG(ERR, "VSI setup failed, MAIN VSI "
3832                             "uplink VSI should be NULL");
3833                 return NULL;
3834         }
3835
3836         /* If uplink vsi didn't setup VEB, create one first */
3837         if (type != I40E_VSI_MAIN && uplink_vsi->veb == NULL) {
3838                 uplink_vsi->veb = i40e_veb_setup(pf, uplink_vsi);
3839
3840                 if (NULL == uplink_vsi->veb) {
3841                         PMD_DRV_LOG(ERR, "VEB setup failed");
3842                         return NULL;
3843                 }
3844         }
3845
3846         vsi = rte_zmalloc("i40e_vsi", sizeof(struct i40e_vsi), 0);
3847         if (!vsi) {
3848                 PMD_DRV_LOG(ERR, "Failed to allocate memory for vsi");
3849                 return NULL;
3850         }
3851         TAILQ_INIT(&vsi->mac_list);
3852         vsi->type = type;
3853         vsi->adapter = I40E_PF_TO_ADAPTER(pf);
3854         vsi->max_macaddrs = I40E_NUM_MACADDR_MAX;
3855         vsi->parent_vsi = uplink_vsi;
3856         vsi->user_param = user_param;
3857         /* Allocate queues */
3858         switch (vsi->type) {
3859         case I40E_VSI_MAIN  :
3860                 vsi->nb_qps = pf->lan_nb_qps;
3861                 break;
3862         case I40E_VSI_SRIOV :
3863                 vsi->nb_qps = pf->vf_nb_qps;
3864                 break;
3865         case I40E_VSI_VMDQ2:
3866                 vsi->nb_qps = pf->vmdq_nb_qps;
3867                 break;
3868         case I40E_VSI_FDIR:
3869                 vsi->nb_qps = pf->fdir_nb_qps;
3870                 break;
3871         default:
3872                 goto fail_mem;
3873         }
3874         /*
3875          * The filter status descriptor is reported in rx queue 0,
3876          * while the tx queue for fdir filter programming has no
3877          * such constraints, can be non-zero queues.
3878          * To simplify it, choose FDIR vsi use queue 0 pair.
3879          * To make sure it will use queue 0 pair, queue allocation
3880          * need be done before this function is called
3881          */
3882         if (type != I40E_VSI_FDIR) {
3883                 ret = i40e_res_pool_alloc(&pf->qp_pool, vsi->nb_qps);
3884                         if (ret < 0) {
3885                                 PMD_DRV_LOG(ERR, "VSI %d allocate queue failed %d",
3886                                                 vsi->seid, ret);
3887                                 goto fail_mem;
3888                         }
3889                         vsi->base_queue = ret;
3890         } else
3891                 vsi->base_queue = I40E_FDIR_QUEUE_ID;
3892
3893         /* VF has MSIX interrupt in VF range, don't allocate here */
3894         if (type == I40E_VSI_MAIN) {
3895                 ret = i40e_res_pool_alloc(&pf->msix_pool,
3896                                           RTE_MIN(vsi->nb_qps,
3897                                                   RTE_MAX_RXTX_INTR_VEC_ID));
3898                 if (ret < 0) {
3899                         PMD_DRV_LOG(ERR, "VSI MAIN %d get heap failed %d",
3900                                     vsi->seid, ret);
3901                         goto fail_queue_alloc;
3902                 }
3903                 vsi->msix_intr = ret;
3904                 vsi->nb_msix = RTE_MIN(vsi->nb_qps, RTE_MAX_RXTX_INTR_VEC_ID);
3905         } else if (type != I40E_VSI_SRIOV) {
3906                 ret = i40e_res_pool_alloc(&pf->msix_pool, 1);
3907                 if (ret < 0) {
3908                         PMD_DRV_LOG(ERR, "VSI %d get heap failed %d", vsi->seid, ret);
3909                         goto fail_queue_alloc;
3910                 }
3911                 vsi->msix_intr = ret;
3912                 vsi->nb_msix = 1;
3913         } else {
3914                 vsi->msix_intr = 0;
3915                 vsi->nb_msix = 0;
3916         }
3917
3918         /* Add VSI */
3919         if (type == I40E_VSI_MAIN) {
3920                 /* For main VSI, no need to add since it's default one */
3921                 vsi->uplink_seid = pf->mac_seid;
3922                 vsi->seid = pf->main_vsi_seid;
3923                 /* Bind queues with specific MSIX interrupt */
3924                 /**
3925                  * Needs 2 interrupt at least, one for misc cause which will
3926                  * enabled from OS side, Another for queues binding the
3927                  * interrupt from device side only.
3928                  */
3929
3930                 /* Get default VSI parameters from hardware */
3931                 memset(&ctxt, 0, sizeof(ctxt));
3932                 ctxt.seid = vsi->seid;
3933                 ctxt.pf_num = hw->pf_id;
3934                 ctxt.uplink_seid = vsi->uplink_seid;
3935                 ctxt.vf_num = 0;
3936                 ret = i40e_aq_get_vsi_params(hw, &ctxt, NULL);
3937                 if (ret != I40E_SUCCESS) {
3938                         PMD_DRV_LOG(ERR, "Failed to get VSI params");
3939                         goto fail_msix_alloc;
3940                 }
3941                 (void)rte_memcpy(&vsi->info, &ctxt.info,
3942                         sizeof(struct i40e_aqc_vsi_properties_data));
3943                 vsi->vsi_id = ctxt.vsi_number;
3944                 vsi->info.valid_sections = 0;
3945
3946                 /* Configure tc, enabled TC0 only */
3947                 if (i40e_vsi_update_tc_bandwidth(vsi, I40E_DEFAULT_TCMAP) !=
3948                         I40E_SUCCESS) {
3949                         PMD_DRV_LOG(ERR, "Failed to update TC bandwidth");
3950                         goto fail_msix_alloc;
3951                 }
3952
3953                 /* TC, queue mapping */
3954                 memset(&ctxt, 0, sizeof(ctxt));
3955                 vsi->info.valid_sections |=
3956                         rte_cpu_to_le_16(I40E_AQ_VSI_PROP_VLAN_VALID);
3957                 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
3958                                         I40E_AQ_VSI_PVLAN_EMOD_STR_BOTH;
3959                 (void)rte_memcpy(&ctxt.info, &vsi->info,
3960                         sizeof(struct i40e_aqc_vsi_properties_data));
3961                 ret = i40e_vsi_config_tc_queue_mapping(vsi, &ctxt.info,
3962                                                 I40E_DEFAULT_TCMAP);
3963                 if (ret != I40E_SUCCESS) {
3964                         PMD_DRV_LOG(ERR, "Failed to configure "
3965                                     "TC queue mapping");
3966                         goto fail_msix_alloc;
3967                 }
3968                 ctxt.seid = vsi->seid;
3969                 ctxt.pf_num = hw->pf_id;
3970                 ctxt.uplink_seid = vsi->uplink_seid;
3971                 ctxt.vf_num = 0;
3972
3973                 /* Update VSI parameters */
3974                 ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
3975                 if (ret != I40E_SUCCESS) {
3976                         PMD_DRV_LOG(ERR, "Failed to update VSI params");
3977                         goto fail_msix_alloc;
3978                 }
3979
3980                 (void)rte_memcpy(&vsi->info.tc_mapping, &ctxt.info.tc_mapping,
3981                                                 sizeof(vsi->info.tc_mapping));
3982                 (void)rte_memcpy(&vsi->info.queue_mapping,
3983                                 &ctxt.info.queue_mapping,
3984                         sizeof(vsi->info.queue_mapping));
3985                 vsi->info.mapping_flags = ctxt.info.mapping_flags;
3986                 vsi->info.valid_sections = 0;
3987
3988                 (void)rte_memcpy(pf->dev_addr.addr_bytes, hw->mac.perm_addr,
3989                                 ETH_ADDR_LEN);
3990
3991                 /**
3992                  * Updating default filter settings are necessary to prevent
3993                  * reception of tagged packets.
3994                  * Some old firmware configurations load a default macvlan
3995                  * filter which accepts both tagged and untagged packets.
3996                  * The updating is to use a normal filter instead if needed.
3997                  * For NVM 4.2.2 or after, the updating is not needed anymore.
3998                  * The firmware with correct configurations load the default
3999                  * macvlan filter which is expected and cannot be removed.
4000                  */
4001                 i40e_update_default_filter_setting(vsi);
4002                 i40e_config_qinq(hw, vsi);
4003         } else if (type == I40E_VSI_SRIOV) {
4004                 memset(&ctxt, 0, sizeof(ctxt));
4005                 /**
4006                  * For other VSI, the uplink_seid equals to uplink VSI's
4007                  * uplink_seid since they share same VEB
4008                  */
4009                 vsi->uplink_seid = uplink_vsi->uplink_seid;
4010                 ctxt.pf_num = hw->pf_id;
4011                 ctxt.vf_num = hw->func_caps.vf_base_id + user_param;
4012                 ctxt.uplink_seid = vsi->uplink_seid;
4013                 ctxt.connection_type = 0x1;
4014                 ctxt.flags = I40E_AQ_VSI_TYPE_VF;
4015
4016                 /**
4017                  * Do not configure switch ID to enable VEB switch by
4018                  * I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB. Because in Fortville,
4019                  * if the source mac address of packet sent from VF is not
4020                  * listed in the VEB's mac table, the VEB will switch the
4021                  * packet back to the VF. Need to enable it when HW issue
4022                  * is fixed.
4023                  */
4024
4025                 /* Configure port/vlan */
4026                 ctxt.info.valid_sections |=
4027                         rte_cpu_to_le_16(I40E_AQ_VSI_PROP_VLAN_VALID);
4028                 ctxt.info.port_vlan_flags |= I40E_AQ_VSI_PVLAN_MODE_ALL;
4029                 ret = i40e_vsi_config_tc_queue_mapping(vsi, &ctxt.info,
4030                                                 I40E_DEFAULT_TCMAP);
4031                 if (ret != I40E_SUCCESS) {
4032                         PMD_DRV_LOG(ERR, "Failed to configure "
4033                                     "TC queue mapping");
4034                         goto fail_msix_alloc;
4035                 }
4036                 ctxt.info.up_enable_bits = I40E_DEFAULT_TCMAP;
4037                 ctxt.info.valid_sections |=
4038                         rte_cpu_to_le_16(I40E_AQ_VSI_PROP_SCHED_VALID);
4039                 /**
4040                  * Since VSI is not created yet, only configure parameter,
4041                  * will add vsi below.
4042                  */
4043
4044                 i40e_config_qinq(hw, vsi);
4045         } else if (type == I40E_VSI_VMDQ2) {
4046                 memset(&ctxt, 0, sizeof(ctxt));
4047                 /*
4048                  * For other VSI, the uplink_seid equals to uplink VSI's
4049                  * uplink_seid since they share same VEB
4050                  */
4051                 vsi->uplink_seid = uplink_vsi->uplink_seid;
4052                 ctxt.pf_num = hw->pf_id;
4053                 ctxt.vf_num = 0;
4054                 ctxt.uplink_seid = vsi->uplink_seid;
4055                 ctxt.connection_type = 0x1;
4056                 ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
4057
4058                 ctxt.info.valid_sections |=
4059                                 rte_cpu_to_le_16(I40E_AQ_VSI_PROP_SWITCH_VALID);
4060                 /* user_param carries flag to enable loop back */
4061                 if (user_param) {
4062                         ctxt.info.switch_id =
4063                         rte_cpu_to_le_16(I40E_AQ_VSI_SW_ID_FLAG_LOCAL_LB);
4064                         ctxt.info.switch_id |=
4065                         rte_cpu_to_le_16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
4066                 }
4067
4068                 /* Configure port/vlan */
4069                 ctxt.info.valid_sections |=
4070                         rte_cpu_to_le_16(I40E_AQ_VSI_PROP_VLAN_VALID);
4071                 ctxt.info.port_vlan_flags |= I40E_AQ_VSI_PVLAN_MODE_ALL;
4072                 ret = i40e_vsi_config_tc_queue_mapping(vsi, &ctxt.info,
4073                                                 I40E_DEFAULT_TCMAP);
4074                 if (ret != I40E_SUCCESS) {
4075                         PMD_DRV_LOG(ERR, "Failed to configure "
4076                                         "TC queue mapping");
4077                         goto fail_msix_alloc;
4078                 }
4079                 ctxt.info.up_enable_bits = I40E_DEFAULT_TCMAP;
4080                 ctxt.info.valid_sections |=
4081                         rte_cpu_to_le_16(I40E_AQ_VSI_PROP_SCHED_VALID);
4082         } else if (type == I40E_VSI_FDIR) {
4083                 memset(&ctxt, 0, sizeof(ctxt));
4084                 vsi->uplink_seid = uplink_vsi->uplink_seid;
4085                 ctxt.pf_num = hw->pf_id;
4086                 ctxt.vf_num = 0;
4087                 ctxt.uplink_seid = vsi->uplink_seid;
4088                 ctxt.connection_type = 0x1;     /* regular data port */
4089                 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
4090                 ret = i40e_vsi_config_tc_queue_mapping(vsi, &ctxt.info,
4091                                                 I40E_DEFAULT_TCMAP);
4092                 if (ret != I40E_SUCCESS) {
4093                         PMD_DRV_LOG(ERR, "Failed to configure "
4094                                         "TC queue mapping.");
4095                         goto fail_msix_alloc;
4096                 }
4097                 ctxt.info.up_enable_bits = I40E_DEFAULT_TCMAP;
4098                 ctxt.info.valid_sections |=
4099                         rte_cpu_to_le_16(I40E_AQ_VSI_PROP_SCHED_VALID);
4100         } else {
4101                 PMD_DRV_LOG(ERR, "VSI: Not support other type VSI yet");
4102                 goto fail_msix_alloc;
4103         }
4104
4105         if (vsi->type != I40E_VSI_MAIN) {
4106                 ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
4107                 if (ret != I40E_SUCCESS) {
4108                         PMD_DRV_LOG(ERR, "add vsi failed, aq_err=%d",
4109                                     hw->aq.asq_last_status);
4110                         goto fail_msix_alloc;
4111                 }
4112                 memcpy(&vsi->info, &ctxt.info, sizeof(ctxt.info));
4113                 vsi->info.valid_sections = 0;
4114                 vsi->seid = ctxt.seid;
4115                 vsi->vsi_id = ctxt.vsi_number;
4116                 vsi->sib_vsi_list.vsi = vsi;
4117                 TAILQ_INSERT_TAIL(&uplink_vsi->veb->head,
4118                                 &vsi->sib_vsi_list, list);
4119         }
4120
4121         /* MAC/VLAN configuration */
4122         (void)rte_memcpy(&filter.mac_addr, &broadcast, ETHER_ADDR_LEN);
4123         filter.filter_type = RTE_MACVLAN_PERFECT_MATCH;
4124
4125         ret = i40e_vsi_add_mac(vsi, &filter);
4126         if (ret != I40E_SUCCESS) {
4127                 PMD_DRV_LOG(ERR, "Failed to add MACVLAN filter");
4128                 goto fail_msix_alloc;
4129         }
4130
4131         /* Get VSI BW information */
4132         i40e_vsi_dump_bw_config(vsi);
4133         return vsi;
4134 fail_msix_alloc:
4135         i40e_res_pool_free(&pf->msix_pool,vsi->msix_intr);
4136 fail_queue_alloc:
4137         i40e_res_pool_free(&pf->qp_pool,vsi->base_queue);
4138 fail_mem:
4139         rte_free(vsi);
4140         return NULL;
4141 }
4142
4143 /* Configure vlan stripping on or off */
4144 int
4145 i40e_vsi_config_vlan_stripping(struct i40e_vsi *vsi, bool on)
4146 {
4147         struct i40e_hw *hw = I40E_VSI_TO_HW(vsi);
4148         struct i40e_vsi_context ctxt;
4149         uint8_t vlan_flags;
4150         int ret = I40E_SUCCESS;
4151
4152         /* Check if it has been already on or off */
4153         if (vsi->info.valid_sections &
4154                 rte_cpu_to_le_16(I40E_AQ_VSI_PROP_VLAN_VALID)) {
4155                 if (on) {
4156                         if ((vsi->info.port_vlan_flags &
4157                                 I40E_AQ_VSI_PVLAN_EMOD_MASK) == 0)
4158                                 return 0; /* already on */
4159                 } else {
4160                         if ((vsi->info.port_vlan_flags &
4161                                 I40E_AQ_VSI_PVLAN_EMOD_MASK) ==
4162                                 I40E_AQ_VSI_PVLAN_EMOD_MASK)
4163                                 return 0; /* already off */
4164                 }
4165         }
4166
4167         if (on)
4168                 vlan_flags = I40E_AQ_VSI_PVLAN_EMOD_STR_BOTH;
4169         else
4170                 vlan_flags = I40E_AQ_VSI_PVLAN_EMOD_NOTHING;
4171         vsi->info.valid_sections =
4172                 rte_cpu_to_le_16(I40E_AQ_VSI_PROP_VLAN_VALID);
4173         vsi->info.port_vlan_flags &= ~(I40E_AQ_VSI_PVLAN_EMOD_MASK);
4174         vsi->info.port_vlan_flags |= vlan_flags;
4175         ctxt.seid = vsi->seid;
4176         (void)rte_memcpy(&ctxt.info, &vsi->info, sizeof(vsi->info));
4177         ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
4178         if (ret)
4179                 PMD_DRV_LOG(INFO, "Update VSI failed to %s vlan stripping",
4180                             on ? "enable" : "disable");
4181
4182         return ret;
4183 }
4184
4185 static int
4186 i40e_dev_init_vlan(struct rte_eth_dev *dev)
4187 {
4188         struct rte_eth_dev_data *data = dev->data;
4189         int ret;
4190
4191         /* Apply vlan offload setting */
4192         i40e_vlan_offload_set(dev, ETH_VLAN_STRIP_MASK);
4193
4194         /* Apply double-vlan setting, not implemented yet */
4195
4196         /* Apply pvid setting */
4197         ret = i40e_vlan_pvid_set(dev, data->dev_conf.txmode.pvid,
4198                                 data->dev_conf.txmode.hw_vlan_insert_pvid);
4199         if (ret)
4200                 PMD_DRV_LOG(INFO, "Failed to update VSI params");
4201
4202         return ret;
4203 }
4204
4205 static int
4206 i40e_vsi_config_double_vlan(struct i40e_vsi *vsi, int on)
4207 {
4208         struct i40e_hw *hw = I40E_VSI_TO_HW(vsi);
4209
4210         return i40e_aq_set_port_parameters(hw, vsi->seid, 0, 1, on, NULL);
4211 }
4212
4213 static int
4214 i40e_update_flow_control(struct i40e_hw *hw)
4215 {
4216 #define I40E_LINK_PAUSE_RXTX (I40E_AQ_LINK_PAUSE_RX | I40E_AQ_LINK_PAUSE_TX)
4217         struct i40e_link_status link_status;
4218         uint32_t rxfc = 0, txfc = 0, reg;
4219         uint8_t an_info;
4220         int ret;
4221
4222         memset(&link_status, 0, sizeof(link_status));
4223         ret = i40e_aq_get_link_info(hw, FALSE, &link_status, NULL);
4224         if (ret != I40E_SUCCESS) {
4225                 PMD_DRV_LOG(ERR, "Failed to get link status information");
4226                 goto write_reg; /* Disable flow control */
4227         }
4228
4229         an_info = hw->phy.link_info.an_info;
4230         if (!(an_info & I40E_AQ_AN_COMPLETED)) {
4231                 PMD_DRV_LOG(INFO, "Link auto negotiation not completed");
4232                 ret = I40E_ERR_NOT_READY;
4233                 goto write_reg; /* Disable flow control */
4234         }
4235         /**
4236          * If link auto negotiation is enabled, flow control needs to
4237          * be configured according to it
4238          */
4239         switch (an_info & I40E_LINK_PAUSE_RXTX) {
4240         case I40E_LINK_PAUSE_RXTX:
4241                 rxfc = 1;
4242                 txfc = 1;
4243                 hw->fc.current_mode = I40E_FC_FULL;
4244                 break;
4245         case I40E_AQ_LINK_PAUSE_RX:
4246                 rxfc = 1;
4247                 hw->fc.current_mode = I40E_FC_RX_PAUSE;
4248                 break;
4249         case I40E_AQ_LINK_PAUSE_TX:
4250                 txfc = 1;
4251                 hw->fc.current_mode = I40E_FC_TX_PAUSE;
4252                 break;
4253         default:
4254                 hw->fc.current_mode = I40E_FC_NONE;
4255                 break;
4256         }
4257
4258 write_reg:
4259         I40E_WRITE_REG(hw, I40E_PRTDCB_FCCFG,
4260                 txfc << I40E_PRTDCB_FCCFG_TFCE_SHIFT);
4261         reg = I40E_READ_REG(hw, I40E_PRTDCB_MFLCN);
4262         reg &= ~I40E_PRTDCB_MFLCN_RFCE_MASK;
4263         reg |= rxfc << I40E_PRTDCB_MFLCN_RFCE_SHIFT;
4264         I40E_WRITE_REG(hw, I40E_PRTDCB_MFLCN, reg);
4265
4266         return ret;
4267 }
4268
4269 /* PF setup */
4270 static int
4271 i40e_pf_setup(struct i40e_pf *pf)
4272 {
4273         struct i40e_hw *hw = I40E_PF_TO_HW(pf);
4274         struct i40e_filter_control_settings settings;
4275         struct i40e_vsi *vsi;
4276         int ret;
4277
4278         /* Clear all stats counters */
4279         pf->offset_loaded = FALSE;
4280         memset(&pf->stats, 0, sizeof(struct i40e_hw_port_stats));
4281         memset(&pf->stats_offset, 0, sizeof(struct i40e_hw_port_stats));
4282
4283         ret = i40e_pf_get_switch_config(pf);
4284         if (ret != I40E_SUCCESS) {
4285                 PMD_DRV_LOG(ERR, "Could not get switch config, err %d", ret);
4286                 return ret;
4287         }
4288         if (pf->flags & I40E_FLAG_FDIR) {
4289                 /* make queue allocated first, let FDIR use queue pair 0*/
4290                 ret = i40e_res_pool_alloc(&pf->qp_pool, I40E_DEFAULT_QP_NUM_FDIR);
4291                 if (ret != I40E_FDIR_QUEUE_ID) {
4292                         PMD_DRV_LOG(ERR, "queue allocation fails for FDIR :"
4293                                     " ret =%d", ret);
4294                         pf->flags &= ~I40E_FLAG_FDIR;
4295                 }
4296         }
4297         /*  main VSI setup */
4298         vsi = i40e_vsi_setup(pf, I40E_VSI_MAIN, NULL, 0);
4299         if (!vsi) {
4300                 PMD_DRV_LOG(ERR, "Setup of main vsi failed");
4301                 return I40E_ERR_NOT_READY;
4302         }
4303         pf->main_vsi = vsi;
4304
4305         /* Configure filter control */
4306         memset(&settings, 0, sizeof(settings));
4307         if (hw->func_caps.rss_table_size == ETH_RSS_RETA_SIZE_128)
4308                 settings.hash_lut_size = I40E_HASH_LUT_SIZE_128;
4309         else if (hw->func_caps.rss_table_size == ETH_RSS_RETA_SIZE_512)
4310                 settings.hash_lut_size = I40E_HASH_LUT_SIZE_512;
4311         else {
4312                 PMD_DRV_LOG(ERR, "Hash lookup table size (%u) not supported\n",
4313                                                 hw->func_caps.rss_table_size);
4314                 return I40E_ERR_PARAM;
4315         }
4316         PMD_DRV_LOG(INFO, "Hardware capability of hash lookup table "
4317                         "size: %u\n", hw->func_caps.rss_table_size);
4318         pf->hash_lut_size = hw->func_caps.rss_table_size;
4319
4320         /* Enable ethtype and macvlan filters */
4321         settings.enable_ethtype = TRUE;
4322         settings.enable_macvlan = TRUE;
4323         ret = i40e_set_filter_control(hw, &settings);
4324         if (ret)
4325                 PMD_INIT_LOG(WARNING, "setup_pf_filter_control failed: %d",
4326                                                                 ret);
4327
4328         /* Update flow control according to the auto negotiation */
4329         i40e_update_flow_control(hw);
4330
4331         return I40E_SUCCESS;
4332 }
4333
4334 int
4335 i40e_switch_tx_queue(struct i40e_hw *hw, uint16_t q_idx, bool on)
4336 {
4337         uint32_t reg;
4338         uint16_t j;
4339
4340         /**
4341          * Set or clear TX Queue Disable flags,
4342          * which is required by hardware.
4343          */
4344         i40e_pre_tx_queue_cfg(hw, q_idx, on);
4345         rte_delay_us(I40E_PRE_TX_Q_CFG_WAIT_US);
4346
4347         /* Wait until the request is finished */
4348         for (j = 0; j < I40E_CHK_Q_ENA_COUNT; j++) {
4349                 rte_delay_us(I40E_CHK_Q_ENA_INTERVAL_US);
4350                 reg = I40E_READ_REG(hw, I40E_QTX_ENA(q_idx));
4351                 if (!(((reg >> I40E_QTX_ENA_QENA_REQ_SHIFT) & 0x1) ^
4352                         ((reg >> I40E_QTX_ENA_QENA_STAT_SHIFT)
4353                                                         & 0x1))) {
4354                         break;
4355                 }
4356         }
4357         if (on) {
4358                 if (reg & I40E_QTX_ENA_QENA_STAT_MASK)
4359                         return I40E_SUCCESS; /* already on, skip next steps */
4360
4361                 I40E_WRITE_REG(hw, I40E_QTX_HEAD(q_idx), 0);
4362                 reg |= I40E_QTX_ENA_QENA_REQ_MASK;
4363         } else {
4364                 if (!(reg & I40E_QTX_ENA_QENA_STAT_MASK))
4365                         return I40E_SUCCESS; /* already off, skip next steps */
4366                 reg &= ~I40E_QTX_ENA_QENA_REQ_MASK;
4367         }
4368         /* Write the register */
4369         I40E_WRITE_REG(hw, I40E_QTX_ENA(q_idx), reg);
4370         /* Check the result */
4371         for (j = 0; j < I40E_CHK_Q_ENA_COUNT; j++) {
4372                 rte_delay_us(I40E_CHK_Q_ENA_INTERVAL_US);
4373                 reg = I40E_READ_REG(hw, I40E_QTX_ENA(q_idx));
4374                 if (on) {
4375                         if ((reg & I40E_QTX_ENA_QENA_REQ_MASK) &&
4376                                 (reg & I40E_QTX_ENA_QENA_STAT_MASK))
4377                                 break;
4378                 } else {
4379                         if (!(reg & I40E_QTX_ENA_QENA_REQ_MASK) &&
4380                                 !(reg & I40E_QTX_ENA_QENA_STAT_MASK))
4381                                 break;
4382                 }
4383         }
4384         /* Check if it is timeout */
4385         if (j >= I40E_CHK_Q_ENA_COUNT) {
4386                 PMD_DRV_LOG(ERR, "Failed to %s tx queue[%u]",
4387                             (on ? "enable" : "disable"), q_idx);
4388                 return I40E_ERR_TIMEOUT;
4389         }
4390
4391         return I40E_SUCCESS;
4392 }
4393
4394 /* Swith on or off the tx queues */
4395 static int
4396 i40e_dev_switch_tx_queues(struct i40e_pf *pf, bool on)
4397 {
4398         struct rte_eth_dev_data *dev_data = pf->dev_data;
4399         struct i40e_tx_queue *txq;
4400         struct rte_eth_dev *dev = pf->adapter->eth_dev;
4401         uint16_t i;
4402         int ret;
4403
4404         for (i = 0; i < dev_data->nb_tx_queues; i++) {
4405                 txq = dev_data->tx_queues[i];
4406                 /* Don't operate the queue if not configured or
4407                  * if starting only per queue */
4408                 if (!txq || !txq->q_set || (on && txq->tx_deferred_start))
4409                         continue;
4410                 if (on)
4411                         ret = i40e_dev_tx_queue_start(dev, i);
4412                 else
4413                         ret = i40e_dev_tx_queue_stop(dev, i);
4414                 if ( ret != I40E_SUCCESS)
4415                         return ret;
4416         }
4417
4418         return I40E_SUCCESS;
4419 }
4420
4421 int
4422 i40e_switch_rx_queue(struct i40e_hw *hw, uint16_t q_idx, bool on)
4423 {
4424         uint32_t reg;
4425         uint16_t j;
4426
4427         /* Wait until the request is finished */
4428         for (j = 0; j < I40E_CHK_Q_ENA_COUNT; j++) {
4429                 rte_delay_us(I40E_CHK_Q_ENA_INTERVAL_US);
4430                 reg = I40E_READ_REG(hw, I40E_QRX_ENA(q_idx));
4431                 if (!((reg >> I40E_QRX_ENA_QENA_REQ_SHIFT) & 0x1) ^
4432                         ((reg >> I40E_QRX_ENA_QENA_STAT_SHIFT) & 0x1))
4433                         break;
4434         }
4435
4436         if (on) {
4437                 if (reg & I40E_QRX_ENA_QENA_STAT_MASK)
4438                         return I40E_SUCCESS; /* Already on, skip next steps */
4439                 reg |= I40E_QRX_ENA_QENA_REQ_MASK;
4440         } else {
4441                 if (!(reg & I40E_QRX_ENA_QENA_STAT_MASK))
4442                         return I40E_SUCCESS; /* Already off, skip next steps */
4443                 reg &= ~I40E_QRX_ENA_QENA_REQ_MASK;
4444         }
4445
4446         /* Write the register */
4447         I40E_WRITE_REG(hw, I40E_QRX_ENA(q_idx), reg);
4448         /* Check the result */
4449         for (j = 0; j < I40E_CHK_Q_ENA_COUNT; j++) {
4450                 rte_delay_us(I40E_CHK_Q_ENA_INTERVAL_US);
4451                 reg = I40E_READ_REG(hw, I40E_QRX_ENA(q_idx));
4452                 if (on) {
4453                         if ((reg & I40E_QRX_ENA_QENA_REQ_MASK) &&
4454                                 (reg & I40E_QRX_ENA_QENA_STAT_MASK))
4455                                 break;
4456                 } else {
4457                         if (!(reg & I40E_QRX_ENA_QENA_REQ_MASK) &&
4458                                 !(reg & I40E_QRX_ENA_QENA_STAT_MASK))
4459                                 break;
4460                 }
4461         }
4462
4463         /* Check if it is timeout */
4464         if (j >= I40E_CHK_Q_ENA_COUNT) {
4465                 PMD_DRV_LOG(ERR, "Failed to %s rx queue[%u]",
4466                             (on ? "enable" : "disable"), q_idx);
4467                 return I40E_ERR_TIMEOUT;
4468         }
4469
4470         return I40E_SUCCESS;
4471 }
4472 /* Switch on or off the rx queues */
4473 static int
4474 i40e_dev_switch_rx_queues(struct i40e_pf *pf, bool on)
4475 {
4476         struct rte_eth_dev_data *dev_data = pf->dev_data;
4477         struct i40e_rx_queue *rxq;
4478         struct rte_eth_dev *dev = pf->adapter->eth_dev;
4479         uint16_t i;
4480         int ret;
4481
4482         for (i = 0; i < dev_data->nb_rx_queues; i++) {
4483                 rxq = dev_data->rx_queues[i];
4484                 /* Don't operate the queue if not configured or
4485                  * if starting only per queue */
4486                 if (!rxq || !rxq->q_set || (on && rxq->rx_deferred_start))
4487                         continue;
4488                 if (on)
4489                         ret = i40e_dev_rx_queue_start(dev, i);
4490                 else
4491                         ret = i40e_dev_rx_queue_stop(dev, i);
4492                 if (ret != I40E_SUCCESS)
4493                         return ret;
4494         }
4495
4496         return I40E_SUCCESS;
4497 }
4498
4499 /* Switch on or off all the rx/tx queues */
4500 int
4501 i40e_dev_switch_queues(struct i40e_pf *pf, bool on)
4502 {
4503         int ret;
4504
4505         if (on) {
4506                 /* enable rx queues before enabling tx queues */
4507                 ret = i40e_dev_switch_rx_queues(pf, on);
4508                 if (ret) {
4509                         PMD_DRV_LOG(ERR, "Failed to switch rx queues");
4510                         return ret;
4511                 }
4512                 ret = i40e_dev_switch_tx_queues(pf, on);
4513         } else {
4514                 /* Stop tx queues before stopping rx queues */
4515                 ret = i40e_dev_switch_tx_queues(pf, on);
4516                 if (ret) {
4517                         PMD_DRV_LOG(ERR, "Failed to switch tx queues");
4518                         return ret;
4519                 }
4520                 ret = i40e_dev_switch_rx_queues(pf, on);
4521         }
4522
4523         return ret;
4524 }
4525
4526 /* Initialize VSI for TX */
4527 static int
4528 i40e_dev_tx_init(struct i40e_pf *pf)
4529 {
4530         struct rte_eth_dev_data *data = pf->dev_data;
4531         uint16_t i;
4532         uint32_t ret = I40E_SUCCESS;
4533         struct i40e_tx_queue *txq;
4534
4535         for (i = 0; i < data->nb_tx_queues; i++) {
4536                 txq = data->tx_queues[i];
4537                 if (!txq || !txq->q_set)
4538                         continue;
4539                 ret = i40e_tx_queue_init(txq);
4540                 if (ret != I40E_SUCCESS)
4541                         break;
4542         }
4543         if (ret == I40E_SUCCESS)
4544                 i40e_set_tx_function(container_of(pf, struct i40e_adapter, pf)
4545                                      ->eth_dev);
4546
4547         return ret;
4548 }
4549
4550 /* Initialize VSI for RX */
4551 static int
4552 i40e_dev_rx_init(struct i40e_pf *pf)
4553 {
4554         struct rte_eth_dev_data *data = pf->dev_data;
4555         int ret = I40E_SUCCESS;
4556         uint16_t i;
4557         struct i40e_rx_queue *rxq;
4558
4559         i40e_pf_config_mq_rx(pf);
4560         for (i = 0; i < data->nb_rx_queues; i++) {
4561                 rxq = data->rx_queues[i];
4562                 if (!rxq || !rxq->q_set)
4563                         continue;
4564
4565                 ret = i40e_rx_queue_init(rxq);
4566                 if (ret != I40E_SUCCESS) {
4567                         PMD_DRV_LOG(ERR, "Failed to do RX queue "
4568                                     "initialization");
4569                         break;
4570                 }
4571         }
4572         if (ret == I40E_SUCCESS)
4573                 i40e_set_rx_function(container_of(pf, struct i40e_adapter, pf)
4574                                      ->eth_dev);
4575
4576         return ret;
4577 }
4578
4579 static int
4580 i40e_dev_rxtx_init(struct i40e_pf *pf)
4581 {
4582         int err;
4583
4584         err = i40e_dev_tx_init(pf);
4585         if (err) {
4586                 PMD_DRV_LOG(ERR, "Failed to do TX initialization");
4587                 return err;
4588         }
4589         err = i40e_dev_rx_init(pf);
4590         if (err) {
4591                 PMD_DRV_LOG(ERR, "Failed to do RX initialization");
4592                 return err;
4593         }
4594
4595         return err;
4596 }
4597
4598 static int
4599 i40e_vmdq_setup(struct rte_eth_dev *dev)
4600 {
4601         struct rte_eth_conf *conf = &dev->data->dev_conf;
4602         struct i40e_pf *pf = I40E_DEV_PRIVATE_TO_PF(dev->data->dev_private);
4603         int i, err, conf_vsis, j, loop;
4604         struct i40e_vsi *vsi;
4605         struct i40e_vmdq_info *vmdq_info;
4606         struct rte_eth_vmdq_rx_conf *vmdq_conf;
4607         struct i40e_hw *hw = I40E_PF_TO_HW(pf);
4608
4609         /*
4610          * Disable interrupt to avoid message from VF. Furthermore, it will
4611          * avoid race condition in VSI creation/destroy.
4612          */
4613         i40e_pf_disable_irq0(hw);
4614
4615         if ((pf->flags & I40E_FLAG_VMDQ) == 0) {
4616                 PMD_INIT_LOG(ERR, "FW doesn't support VMDQ");
4617                 return -ENOTSUP;
4618         }
4619
4620         conf_vsis = conf->rx_adv_conf.vmdq_rx_conf.nb_queue_pools;
4621         if (conf_vsis > pf->max_nb_vmdq_vsi) {
4622                 PMD_INIT_LOG(ERR, "VMDQ config: %u, max support:%u",
4623                         conf->rx_adv_conf.vmdq_rx_conf.nb_queue_pools,
4624                         pf->max_nb_vmdq_vsi);
4625                 return -ENOTSUP;
4626         }
4627
4628         if (pf->vmdq != NULL) {
4629                 PMD_INIT_LOG(INFO, "VMDQ already configured");
4630                 return 0;
4631         }
4632
4633         pf->vmdq = rte_zmalloc("vmdq_info_struct",
4634                                 sizeof(*vmdq_info) * conf_vsis, 0);
4635
4636         if (pf->vmdq == NULL) {
4637                 PMD_INIT_LOG(ERR, "Failed to allocate memory");
4638                 return -ENOMEM;
4639         }
4640
4641         vmdq_conf = &conf->rx_adv_conf.vmdq_rx_conf;
4642
4643         /* Create VMDQ VSI */
4644         for (i = 0; i < conf_vsis; i++) {
4645                 vsi = i40e_vsi_setup(pf, I40E_VSI_VMDQ2, pf->main_vsi,
4646                                 vmdq_conf->enable_loop_back);
4647                 if (vsi == NULL) {
4648                         PMD_INIT_LOG(ERR, "Failed to create VMDQ VSI");
4649                         err = -1;
4650                         goto err_vsi_setup;
4651                 }
4652                 vmdq_info = &pf->vmdq[i];
4653                 vmdq_info->pf = pf;
4654                 vmdq_info->vsi = vsi;
4655         }
4656         pf->nb_cfg_vmdq_vsi = conf_vsis;
4657
4658         /* Configure Vlan */
4659         loop = sizeof(vmdq_conf->pool_map[0].pools) * CHAR_BIT;
4660         for (i = 0; i < vmdq_conf->nb_pool_maps; i++) {
4661                 for (j = 0; j < loop && j < pf->nb_cfg_vmdq_vsi; j++) {
4662                         if (vmdq_conf->pool_map[i].pools & (1UL << j)) {
4663                                 PMD_INIT_LOG(INFO, "Add vlan %u to vmdq pool %u",
4664                                         vmdq_conf->pool_map[i].vlan_id, j);
4665
4666                                 err = i40e_vsi_add_vlan(pf->vmdq[j].vsi,
4667                                                 vmdq_conf->pool_map[i].vlan_id);
4668                                 if (err) {
4669                                         PMD_INIT_LOG(ERR, "Failed to add vlan");
4670                                         err = -1;
4671                                         goto err_vsi_setup;
4672                                 }
4673                         }
4674                 }
4675         }
4676
4677         i40e_pf_enable_irq0(hw);
4678
4679         return 0;
4680
4681 err_vsi_setup:
4682         for (i = 0; i < conf_vsis; i++)
4683                 if (pf->vmdq[i].vsi == NULL)
4684                         break;
4685                 else
4686                         i40e_vsi_release(pf->vmdq[i].vsi);
4687
4688         rte_free(pf->vmdq);
4689         pf->vmdq = NULL;
4690         i40e_pf_enable_irq0(hw);
4691         return err;
4692 }
4693
4694 static void
4695 i40e_stat_update_32(struct i40e_hw *hw,
4696                    uint32_t reg,
4697                    bool offset_loaded,
4698                    uint64_t *offset,
4699                    uint64_t *stat)
4700 {
4701         uint64_t new_data;
4702
4703         new_data = (uint64_t)I40E_READ_REG(hw, reg);
4704         if (!offset_loaded)
4705                 *offset = new_data;
4706
4707         if (new_data >= *offset)
4708                 *stat = (uint64_t)(new_data - *offset);
4709         else
4710                 *stat = (uint64_t)((new_data +
4711                         ((uint64_t)1 << I40E_32_BIT_WIDTH)) - *offset);
4712 }
4713
4714 static void
4715 i40e_stat_update_48(struct i40e_hw *hw,
4716                    uint32_t hireg,
4717                    uint32_t loreg,
4718                    bool offset_loaded,
4719                    uint64_t *offset,
4720                    uint64_t *stat)
4721 {
4722         uint64_t new_data;
4723
4724         new_data = (uint64_t)I40E_READ_REG(hw, loreg);
4725         new_data |= ((uint64_t)(I40E_READ_REG(hw, hireg) &
4726                         I40E_16_BIT_MASK)) << I40E_32_BIT_WIDTH;
4727
4728         if (!offset_loaded)
4729                 *offset = new_data;
4730
4731         if (new_data >= *offset)
4732                 *stat = new_data - *offset;
4733         else
4734                 *stat = (uint64_t)((new_data +
4735                         ((uint64_t)1 << I40E_48_BIT_WIDTH)) - *offset);
4736
4737         *stat &= I40E_48_BIT_MASK;
4738 }
4739
4740 /* Disable IRQ0 */
4741 void
4742 i40e_pf_disable_irq0(struct i40e_hw *hw)
4743 {
4744         /* Disable all interrupt types */
4745         I40E_WRITE_REG(hw, I40E_PFINT_DYN_CTL0, 0);
4746         I40E_WRITE_FLUSH(hw);
4747 }
4748
4749 /* Enable IRQ0 */
4750 void
4751 i40e_pf_enable_irq0(struct i40e_hw *hw)
4752 {
4753         I40E_WRITE_REG(hw, I40E_PFINT_DYN_CTL0,
4754                 I40E_PFINT_DYN_CTL0_INTENA_MASK |
4755                 I40E_PFINT_DYN_CTL0_CLEARPBA_MASK |
4756                 I40E_PFINT_DYN_CTL0_ITR_INDX_MASK);
4757         I40E_WRITE_FLUSH(hw);
4758 }
4759
4760 static void
4761 i40e_pf_config_irq0(struct i40e_hw *hw, bool no_queue)
4762 {
4763         /* read pending request and disable first */
4764         i40e_pf_disable_irq0(hw);
4765         I40E_WRITE_REG(hw, I40E_PFINT_ICR0_ENA, I40E_PFINT_ICR0_ENA_MASK);
4766         I40E_WRITE_REG(hw, I40E_PFINT_STAT_CTL0,
4767                 I40E_PFINT_STAT_CTL0_OTHER_ITR_INDX_MASK);
4768
4769         if (no_queue)
4770                 /* Link no queues with irq0 */
4771                 I40E_WRITE_REG(hw, I40E_PFINT_LNKLST0,
4772                                I40E_PFINT_LNKLST0_FIRSTQ_INDX_MASK);
4773 }
4774
4775 static void
4776 i40e_dev_handle_vfr_event(struct rte_eth_dev *dev)
4777 {
4778         struct i40e_hw *hw = I40E_DEV_PRIVATE_TO_HW(dev->data->dev_private);
4779         struct i40e_pf *pf = I40E_DEV_PRIVATE_TO_PF(dev->data->dev_private);
4780         int i;
4781         uint16_t abs_vf_id;
4782         uint32_t index, offset, val;
4783
4784         if (!pf->vfs)
4785                 return;
4786         /**
4787          * Try to find which VF trigger a reset, use absolute VF id to access
4788          * since the reg is global register.
4789          */
4790         for (i = 0; i < pf->vf_num; i++) {
4791                 abs_vf_id = hw->func_caps.vf_base_id + i;
4792                 index = abs_vf_id / I40E_UINT32_BIT_SIZE;
4793                 offset = abs_vf_id % I40E_UINT32_BIT_SIZE;
4794                 val = I40E_READ_REG(hw, I40E_GLGEN_VFLRSTAT(index));
4795                 /* VFR event occured */
4796                 if (val & (0x1 << offset)) {
4797                         int ret;
4798
4799                         /* Clear the event first */
4800                         I40E_WRITE_REG(hw, I40E_GLGEN_VFLRSTAT(index),
4801                                                         (0x1 << offset));
4802                         PMD_DRV_LOG(INFO, "VF %u reset occured", abs_vf_id);
4803                         /**
4804                          * Only notify a VF reset event occured,
4805                          * don't trigger another SW reset
4806                          */
4807                         ret = i40e_pf_host_vf_reset(&pf->vfs[i], 0);
4808                         if (ret != I40E_SUCCESS)
4809                                 PMD_DRV_LOG(ERR, "Failed to do VF reset");
4810                 }
4811         }
4812 }
4813
4814 static void
4815 i40e_dev_handle_aq_msg(struct rte_eth_dev *dev)
4816 {
4817         struct i40e_hw *hw = I40E_DEV_PRIVATE_TO_HW(dev->data->dev_private);
4818         struct i40e_arq_event_info info;
4819         uint16_t pending, opcode;
4820         int ret;
4821
4822         info.buf_len = I40E_AQ_BUF_SZ;
4823         info.msg_buf = rte_zmalloc("msg_buffer", info.buf_len, 0);
4824         if (!info.msg_buf) {
4825                 PMD_DRV_LOG(ERR, "Failed to allocate mem");
4826                 return;
4827         }
4828
4829         pending = 1;
4830         while (pending) {
4831                 ret = i40e_clean_arq_element(hw, &info, &pending);
4832
4833                 if (ret != I40E_SUCCESS) {
4834                         PMD_DRV_LOG(INFO, "Failed to read msg from AdminQ, "
4835                                     "aq_err: %u", hw->aq.asq_last_status);
4836                         break;
4837                 }
4838                 opcode = rte_le_to_cpu_16(info.desc.opcode);
4839
4840                 switch (opcode) {
4841                 case i40e_aqc_opc_send_msg_to_pf:
4842                         /* Refer to i40e_aq_send_msg_to_pf() for argument layout*/
4843                         i40e_pf_host_handle_vf_msg(dev,
4844                                         rte_le_to_cpu_16(info.desc.retval),
4845                                         rte_le_to_cpu_32(info.desc.cookie_high),
4846                                         rte_le_to_cpu_32(info.desc.cookie_low),
4847                                         info.msg_buf,
4848                                         info.msg_len);
4849                         break;
4850                 default:
4851                         PMD_DRV_LOG(ERR, "Request %u is not supported yet",
4852                                     opcode);
4853                         break;
4854                 }
4855         }
4856         rte_free(info.msg_buf);
4857 }
4858
4859 /*
4860  * Interrupt handler is registered as the alarm callback for handling LSC
4861  * interrupt in a definite of time, in order to wait the NIC into a stable
4862  * state. Currently it waits 1 sec in i40e for the link up interrupt, and
4863  * no need for link down interrupt.
4864  */
4865 static void
4866 i40e_dev_interrupt_delayed_handler(void *param)
4867 {
4868         struct rte_eth_dev *dev = (struct rte_eth_dev *)param;
4869         struct i40e_hw *hw = I40E_DEV_PRIVATE_TO_HW(dev->data->dev_private);
4870         uint32_t icr0;
4871
4872         /* read interrupt causes again */
4873         icr0 = I40E_READ_REG(hw, I40E_PFINT_ICR0);
4874
4875 #ifdef RTE_LIBRTE_I40E_DEBUG_DRIVER
4876         if (icr0 & I40E_PFINT_ICR0_ECC_ERR_MASK)
4877                 PMD_DRV_LOG(ERR, "ICR0: unrecoverable ECC error\n");
4878         if (icr0 & I40E_PFINT_ICR0_MAL_DETECT_MASK)
4879                 PMD_DRV_LOG(ERR, "ICR0: malicious programming detected\n");
4880         if (icr0 & I40E_PFINT_ICR0_GRST_MASK)
4881                 PMD_DRV_LOG(INFO, "ICR0: global reset requested\n");
4882         if (icr0 & I40E_PFINT_ICR0_PCI_EXCEPTION_MASK)
4883                 PMD_DRV_LOG(INFO, "ICR0: PCI exception\n activated\n");
4884         if (icr0 & I40E_PFINT_ICR0_STORM_DETECT_MASK)
4885                 PMD_DRV_LOG(INFO, "ICR0: a change in the storm control "
4886                                                                 "state\n");
4887         if (icr0 & I40E_PFINT_ICR0_HMC_ERR_MASK)
4888                 PMD_DRV_LOG(ERR, "ICR0: HMC error\n");
4889         if (icr0 & I40E_PFINT_ICR0_PE_CRITERR_MASK)
4890                 PMD_DRV_LOG(ERR, "ICR0: protocol engine critical error\n");
4891 #endif /* RTE_LIBRTE_I40E_DEBUG_DRIVER */
4892
4893         if (icr0 & I40E_PFINT_ICR0_VFLR_MASK) {
4894                 PMD_DRV_LOG(INFO, "INT:VF reset detected\n");
4895                 i40e_dev_handle_vfr_event(dev);
4896         }
4897         if (icr0 & I40E_PFINT_ICR0_ADMINQ_MASK) {
4898                 PMD_DRV_LOG(INFO, "INT:ADMINQ event\n");
4899                 i40e_dev_handle_aq_msg(dev);
4900         }
4901
4902         /* handle the link up interrupt in an alarm callback */
4903         i40e_dev_link_update(dev, 0);
4904         _rte_eth_dev_callback_process(dev, RTE_ETH_EVENT_INTR_LSC);
4905
4906         i40e_pf_enable_irq0(hw);
4907         rte_intr_enable(&(dev->pci_dev->intr_handle));
4908 }
4909
4910 /**
4911  * Interrupt handler triggered by NIC  for handling
4912  * specific interrupt.
4913  *
4914  * @param handle
4915  *  Pointer to interrupt handle.
4916  * @param param
4917  *  The address of parameter (struct rte_eth_dev *) regsitered before.
4918  *
4919  * @return
4920  *  void
4921  */
4922 static void
4923 i40e_dev_interrupt_handler(__rte_unused struct rte_intr_handle *handle,
4924                            void *param)
4925 {
4926         struct rte_eth_dev *dev = (struct rte_eth_dev *)param;
4927         struct i40e_hw *hw = I40E_DEV_PRIVATE_TO_HW(dev->data->dev_private);
4928         uint32_t icr0;
4929
4930         /* Disable interrupt */
4931         i40e_pf_disable_irq0(hw);
4932
4933         /* read out interrupt causes */
4934         icr0 = I40E_READ_REG(hw, I40E_PFINT_ICR0);
4935
4936         /* No interrupt event indicated */
4937         if (!(icr0 & I40E_PFINT_ICR0_INTEVENT_MASK)) {
4938                 PMD_DRV_LOG(INFO, "No interrupt event");
4939                 goto done;
4940         }
4941 #ifdef RTE_LIBRTE_I40E_DEBUG_DRIVER
4942         if (icr0 & I40E_PFINT_ICR0_ECC_ERR_MASK)
4943                 PMD_DRV_LOG(ERR, "ICR0: unrecoverable ECC error");
4944         if (icr0 & I40E_PFINT_ICR0_MAL_DETECT_MASK)
4945                 PMD_DRV_LOG(ERR, "ICR0: malicious programming detected");
4946         if (icr0 & I40E_PFINT_ICR0_GRST_MASK)
4947                 PMD_DRV_LOG(INFO, "ICR0: global reset requested");
4948         if (icr0 & I40E_PFINT_ICR0_PCI_EXCEPTION_MASK)
4949                 PMD_DRV_LOG(INFO, "ICR0: PCI exception activated");
4950         if (icr0 & I40E_PFINT_ICR0_STORM_DETECT_MASK)
4951                 PMD_DRV_LOG(INFO, "ICR0: a change in the storm control state");
4952         if (icr0 & I40E_PFINT_ICR0_HMC_ERR_MASK)
4953                 PMD_DRV_LOG(ERR, "ICR0: HMC error");
4954         if (icr0 & I40E_PFINT_ICR0_PE_CRITERR_MASK)
4955                 PMD_DRV_LOG(ERR, "ICR0: protocol engine critical error");
4956 #endif /* RTE_LIBRTE_I40E_DEBUG_DRIVER */
4957
4958         if (icr0 & I40E_PFINT_ICR0_VFLR_MASK) {
4959                 PMD_DRV_LOG(INFO, "ICR0: VF reset detected");
4960                 i40e_dev_handle_vfr_event(dev);
4961         }
4962         if (icr0 & I40E_PFINT_ICR0_ADMINQ_MASK) {
4963                 PMD_DRV_LOG(INFO, "ICR0: adminq event");
4964                 i40e_dev_handle_aq_msg(dev);
4965         }
4966
4967         /* Link Status Change interrupt */
4968         if (icr0 & I40E_PFINT_ICR0_LINK_STAT_CHANGE_MASK) {
4969 #define I40E_US_PER_SECOND 1000000
4970                 struct rte_eth_link link;
4971
4972                 PMD_DRV_LOG(INFO, "ICR0: link status changed\n");
4973                 memset(&link, 0, sizeof(link));
4974                 rte_i40e_dev_atomic_read_link_status(dev, &link);
4975                 i40e_dev_link_update(dev, 0);
4976
4977                 /*
4978                  * For link up interrupt, it needs to wait 1 second to let the
4979                  * hardware be a stable state. Otherwise several consecutive
4980                  * interrupts can be observed.
4981                  * For link down interrupt, no need to wait.
4982                  */
4983                 if (!link.link_status && rte_eal_alarm_set(I40E_US_PER_SECOND,
4984                         i40e_dev_interrupt_delayed_handler, (void *)dev) >= 0)
4985                         return;
4986                 else
4987                         _rte_eth_dev_callback_process(dev,
4988                                 RTE_ETH_EVENT_INTR_LSC);
4989         }
4990
4991 done:
4992         /* Enable interrupt */
4993         i40e_pf_enable_irq0(hw);
4994         rte_intr_enable(&(dev->pci_dev->intr_handle));
4995 }
4996
4997 static int
4998 i40e_add_macvlan_filters(struct i40e_vsi *vsi,
4999                          struct i40e_macvlan_filter *filter,
5000                          int total)
5001 {
5002         int ele_num, ele_buff_size;
5003         int num, actual_num, i;
5004         uint16_t flags;
5005         int ret = I40E_SUCCESS;
5006         struct i40e_hw *hw = I40E_VSI_TO_HW(vsi);
5007         struct i40e_aqc_add_macvlan_element_data *req_list;
5008
5009         if (filter == NULL  || total == 0)
5010                 return I40E_ERR_PARAM;
5011         ele_num = hw->aq.asq_buf_size / sizeof(*req_list);
5012         ele_buff_size = hw->aq.asq_buf_size;
5013
5014         req_list = rte_zmalloc("macvlan_add", ele_buff_size, 0);
5015         if (req_list == NULL) {
5016                 PMD_DRV_LOG(ERR, "Fail to allocate memory");
5017                 return I40E_ERR_NO_MEMORY;
5018         }
5019
5020         num = 0;
5021         do {
5022                 actual_num = (num + ele_num > total) ? (total - num) : ele_num;
5023                 memset(req_list, 0, ele_buff_size);
5024
5025                 for (i = 0; i < actual_num; i++) {
5026                         (void)rte_memcpy(req_list[i].mac_addr,
5027                                 &filter[num + i].macaddr, ETH_ADDR_LEN);
5028                         req_list[i].vlan_tag =
5029                                 rte_cpu_to_le_16(filter[num + i].vlan_id);
5030
5031                         switch (filter[num + i].filter_type) {
5032                         case RTE_MAC_PERFECT_MATCH:
5033                                 flags = I40E_AQC_MACVLAN_ADD_PERFECT_MATCH |
5034                                         I40E_AQC_MACVLAN_ADD_IGNORE_VLAN;
5035                                 break;
5036                         case RTE_MACVLAN_PERFECT_MATCH:
5037                                 flags = I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
5038                                 break;
5039                         case RTE_MAC_HASH_MATCH:
5040                                 flags = I40E_AQC_MACVLAN_ADD_HASH_MATCH |
5041                                         I40E_AQC_MACVLAN_ADD_IGNORE_VLAN;
5042                                 break;
5043                         case RTE_MACVLAN_HASH_MATCH:
5044                                 flags = I40E_AQC_MACVLAN_ADD_HASH_MATCH;
5045                                 break;
5046                         default:
5047                                 PMD_DRV_LOG(ERR, "Invalid MAC match type\n");
5048                                 ret = I40E_ERR_PARAM;
5049                                 goto DONE;
5050                         }
5051
5052                         req_list[i].queue_number = 0;
5053
5054                         req_list[i].flags = rte_cpu_to_le_16(flags);
5055                 }
5056
5057                 ret = i40e_aq_add_macvlan(hw, vsi->seid, req_list,
5058                                                 actual_num, NULL);
5059                 if (ret != I40E_SUCCESS) {
5060                         PMD_DRV_LOG(ERR, "Failed to add macvlan filter");
5061                         goto DONE;
5062                 }
5063                 num += actual_num;
5064         } while (num < total);
5065
5066 DONE:
5067         rte_free(req_list);
5068         return ret;
5069 }
5070
5071 static int
5072 i40e_remove_macvlan_filters(struct i40e_vsi *vsi,
5073                             struct i40e_macvlan_filter *filter,
5074                             int total)
5075 {
5076         int ele_num, ele_buff_size;
5077         int num, actual_num, i;
5078         uint16_t flags;
5079         int ret = I40E_SUCCESS;
5080         struct i40e_hw *hw = I40E_VSI_TO_HW(vsi);
5081         struct i40e_aqc_remove_macvlan_element_data *req_list;
5082
5083         if (filter == NULL  || total == 0)
5084                 return I40E_ERR_PARAM;
5085
5086         ele_num = hw->aq.asq_buf_size / sizeof(*req_list);
5087         ele_buff_size = hw->aq.asq_buf_size;
5088
5089         req_list = rte_zmalloc("macvlan_remove", ele_buff_size, 0);
5090         if (req_list == NULL) {
5091                 PMD_DRV_LOG(ERR, "Fail to allocate memory");
5092                 return I40E_ERR_NO_MEMORY;
5093         }
5094
5095         num = 0;
5096         do {
5097                 actual_num = (num + ele_num > total) ? (total - num) : ele_num;
5098                 memset(req_list, 0, ele_buff_size);
5099
5100                 for (i = 0; i < actual_num; i++) {
5101                         (void)rte_memcpy(req_list[i].mac_addr,
5102                                 &filter[num + i].macaddr, ETH_ADDR_LEN);
5103                         req_list[i].vlan_tag =
5104                                 rte_cpu_to_le_16(filter[num + i].vlan_id);
5105
5106                         switch (filter[num + i].filter_type) {
5107                         case RTE_MAC_PERFECT_MATCH:
5108                                 flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH |
5109                                         I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
5110                                 break;
5111                         case RTE_MACVLAN_PERFECT_MATCH:
5112                                 flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
5113                                 break;
5114                         case RTE_MAC_HASH_MATCH:
5115                                 flags = I40E_AQC_MACVLAN_DEL_HASH_MATCH |
5116                                         I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
5117                                 break;
5118                         case RTE_MACVLAN_HASH_MATCH:
5119                                 flags = I40E_AQC_MACVLAN_DEL_HASH_MATCH;
5120                                 break;
5121                         default:
5122                                 PMD_DRV_LOG(ERR, "Invalid MAC filter type\n");
5123                                 ret = I40E_ERR_PARAM;
5124                                 goto DONE;
5125                         }
5126                         req_list[i].flags = rte_cpu_to_le_16(flags);
5127                 }
5128
5129                 ret = i40e_aq_remove_macvlan(hw, vsi->seid, req_list,
5130                                                 actual_num, NULL);
5131                 if (ret != I40E_SUCCESS) {
5132                         PMD_DRV_LOG(ERR, "Failed to remove macvlan filter");
5133                         goto DONE;
5134                 }
5135                 num += actual_num;
5136         } while (num < total);
5137
5138 DONE:
5139         rte_free(req_list);
5140         return ret;
5141 }
5142
5143 /* Find out specific MAC filter */
5144 static struct i40e_mac_filter *
5145 i40e_find_mac_filter(struct i40e_vsi *vsi,
5146                          struct ether_addr *macaddr)
5147 {
5148         struct i40e_mac_filter *f;
5149
5150         TAILQ_FOREACH(f, &vsi->mac_list, next) {
5151                 if (is_same_ether_addr(macaddr, &f->mac_info.mac_addr))
5152                         return f;
5153         }
5154
5155         return NULL;
5156 }
5157
5158 static bool
5159 i40e_find_vlan_filter(struct i40e_vsi *vsi,
5160                          uint16_t vlan_id)
5161 {
5162         uint32_t vid_idx, vid_bit;
5163
5164         if (vlan_id > ETH_VLAN_ID_MAX)
5165                 return 0;
5166
5167         vid_idx = I40E_VFTA_IDX(vlan_id);
5168         vid_bit = I40E_VFTA_BIT(vlan_id);
5169
5170         if (vsi->vfta[vid_idx] & vid_bit)
5171                 return 1;
5172         else
5173                 return 0;
5174 }
5175
5176 static void
5177 i40e_set_vlan_filter(struct i40e_vsi *vsi,
5178                          uint16_t vlan_id, bool on)
5179 {
5180         uint32_t vid_idx, vid_bit;
5181
5182         if (vlan_id > ETH_VLAN_ID_MAX)
5183                 return;
5184
5185         vid_idx = I40E_VFTA_IDX(vlan_id);
5186         vid_bit = I40E_VFTA_BIT(vlan_id);
5187
5188         if (on)
5189                 vsi->vfta[vid_idx] |= vid_bit;
5190         else
5191                 vsi->vfta[vid_idx] &= ~vid_bit;
5192 }
5193
5194 /**
5195  * Find all vlan options for specific mac addr,
5196  * return with actual vlan found.
5197  */
5198 static inline int
5199 i40e_find_all_vlan_for_mac(struct i40e_vsi *vsi,
5200                            struct i40e_macvlan_filter *mv_f,
5201                            int num, struct ether_addr *addr)
5202 {
5203         int i;
5204         uint32_t j, k;
5205
5206         /**
5207          * Not to use i40e_find_vlan_filter to decrease the loop time,
5208          * although the code looks complex.
5209           */
5210         if (num < vsi->vlan_num)
5211                 return I40E_ERR_PARAM;
5212
5213         i = 0;
5214         for (j = 0; j < I40E_VFTA_SIZE; j++) {
5215                 if (vsi->vfta[j]) {
5216                         for (k = 0; k < I40E_UINT32_BIT_SIZE; k++) {
5217                                 if (vsi->vfta[j] & (1 << k)) {
5218                                         if (i > num - 1) {
5219                                                 PMD_DRV_LOG(ERR, "vlan number "
5220                                                             "not match");
5221                                                 return I40E_ERR_PARAM;
5222                                         }
5223                                         (void)rte_memcpy(&mv_f[i].macaddr,
5224                                                         addr, ETH_ADDR_LEN);
5225                                         mv_f[i].vlan_id =
5226                                                 j * I40E_UINT32_BIT_SIZE + k;
5227                                         i++;
5228                                 }
5229                         }
5230                 }
5231         }
5232         return I40E_SUCCESS;
5233 }
5234
5235 static inline int
5236 i40e_find_all_mac_for_vlan(struct i40e_vsi *vsi,
5237                            struct i40e_macvlan_filter *mv_f,
5238                            int num,
5239                            uint16_t vlan)
5240 {
5241         int i = 0;
5242         struct i40e_mac_filter *f;
5243
5244         if (num < vsi->mac_num)
5245                 return I40E_ERR_PARAM;
5246
5247         TAILQ_FOREACH(f, &vsi->mac_list, next) {
5248                 if (i > num - 1) {
5249                         PMD_DRV_LOG(ERR, "buffer number not match");
5250                         return I40E_ERR_PARAM;
5251                 }
5252                 (void)rte_memcpy(&mv_f[i].macaddr, &f->mac_info.mac_addr,
5253                                 ETH_ADDR_LEN);
5254                 mv_f[i].vlan_id = vlan;
5255                 mv_f[i].filter_type = f->mac_info.filter_type;
5256                 i++;
5257         }
5258
5259         return I40E_SUCCESS;
5260 }
5261
5262 static int
5263 i40e_vsi_remove_all_macvlan_filter(struct i40e_vsi *vsi)
5264 {
5265         int i, num;
5266         struct i40e_mac_filter *f;
5267         struct i40e_macvlan_filter *mv_f;
5268         int ret = I40E_SUCCESS;
5269
5270         if (vsi == NULL || vsi->mac_num == 0)
5271                 return I40E_ERR_PARAM;
5272
5273         /* Case that no vlan is set */
5274         if (vsi->vlan_num == 0)
5275                 num = vsi->mac_num;
5276         else
5277                 num = vsi->mac_num * vsi->vlan_num;
5278
5279         mv_f = rte_zmalloc("macvlan_data", num * sizeof(*mv_f), 0);
5280         if (mv_f == NULL) {
5281                 PMD_DRV_LOG(ERR, "failed to allocate memory");
5282                 return I40E_ERR_NO_MEMORY;
5283         }
5284
5285         i = 0;
5286         if (vsi->vlan_num == 0) {
5287                 TAILQ_FOREACH(f, &vsi->mac_list, next) {
5288                         (void)rte_memcpy(&mv_f[i].macaddr,
5289                                 &f->mac_info.mac_addr, ETH_ADDR_LEN);
5290                         mv_f[i].vlan_id = 0;
5291                         i++;
5292                 }
5293         } else {
5294                 TAILQ_FOREACH(f, &vsi->mac_list, next) {
5295                         ret = i40e_find_all_vlan_for_mac(vsi,&mv_f[i],
5296                                         vsi->vlan_num, &f->mac_info.mac_addr);
5297                         if (ret != I40E_SUCCESS)
5298                                 goto DONE;
5299                         i += vsi->vlan_num;
5300                 }
5301         }
5302
5303         ret = i40e_remove_macvlan_filters(vsi, mv_f, num);
5304 DONE:
5305         rte_free(mv_f);
5306
5307         return ret;
5308 }
5309
5310 int
5311 i40e_vsi_add_vlan(struct i40e_vsi *vsi, uint16_t vlan)
5312 {
5313         struct i40e_macvlan_filter *mv_f;
5314         int mac_num;
5315         int ret = I40E_SUCCESS;
5316
5317         if (!vsi || vlan > ETHER_MAX_VLAN_ID)
5318                 return I40E_ERR_PARAM;
5319
5320         /* If it's already set, just return */
5321         if (i40e_find_vlan_filter(vsi,vlan))
5322                 return I40E_SUCCESS;
5323
5324         mac_num = vsi->mac_num;
5325
5326         if (mac_num == 0) {
5327                 PMD_DRV_LOG(ERR, "Error! VSI doesn't have a mac addr");
5328                 return I40E_ERR_PARAM;
5329         }
5330
5331         mv_f = rte_zmalloc("macvlan_data", mac_num * sizeof(*mv_f), 0);
5332
5333         if (mv_f == NULL) {
5334                 PMD_DRV_LOG(ERR, "failed to allocate memory");
5335                 return I40E_ERR_NO_MEMORY;
5336         }
5337
5338         ret = i40e_find_all_mac_for_vlan(vsi, mv_f, mac_num, vlan);
5339
5340         if (ret != I40E_SUCCESS)
5341                 goto DONE;
5342
5343         ret = i40e_add_macvlan_filters(vsi, mv_f, mac_num);
5344
5345         if (ret != I40E_SUCCESS)
5346                 goto DONE;
5347
5348         i40e_set_vlan_filter(vsi, vlan, 1);
5349
5350         vsi->vlan_num++;
5351         ret = I40E_SUCCESS;
5352 DONE:
5353         rte_free(mv_f);
5354         return ret;
5355 }
5356
5357 int
5358 i40e_vsi_delete_vlan(struct i40e_vsi *vsi, uint16_t vlan)
5359 {
5360         struct i40e_macvlan_filter *mv_f;
5361         int mac_num;
5362         int ret = I40E_SUCCESS;
5363
5364         /**
5365          * Vlan 0 is the generic filter for untagged packets
5366          * and can't be removed.
5367          */
5368         if (!vsi || vlan == 0 || vlan > ETHER_MAX_VLAN_ID)
5369                 return I40E_ERR_PARAM;
5370
5371         /* If can't find it, just return */
5372         if (!i40e_find_vlan_filter(vsi, vlan))
5373                 return I40E_ERR_PARAM;
5374
5375         mac_num = vsi->mac_num;
5376
5377         if (mac_num == 0) {
5378                 PMD_DRV_LOG(ERR, "Error! VSI doesn't have a mac addr");
5379                 return I40E_ERR_PARAM;
5380         }
5381
5382         mv_f = rte_zmalloc("macvlan_data", mac_num * sizeof(*mv_f), 0);
5383
5384         if (mv_f == NULL) {
5385                 PMD_DRV_LOG(ERR, "failed to allocate memory");
5386                 return I40E_ERR_NO_MEMORY;
5387         }
5388
5389         ret = i40e_find_all_mac_for_vlan(vsi, mv_f, mac_num, vlan);
5390
5391         if (ret != I40E_SUCCESS)
5392                 goto DONE;
5393
5394         ret = i40e_remove_macvlan_filters(vsi, mv_f, mac_num);
5395
5396         if (ret != I40E_SUCCESS)
5397                 goto DONE;
5398
5399         /* This is last vlan to remove, replace all mac filter with vlan 0 */
5400         if (vsi->vlan_num == 1) {
5401                 ret = i40e_find_all_mac_for_vlan(vsi, mv_f, mac_num, 0);
5402                 if (ret != I40E_SUCCESS)
5403                         goto DONE;
5404
5405                 ret = i40e_add_macvlan_filters(vsi, mv_f, mac_num);
5406                 if (ret != I40E_SUCCESS)
5407                         goto DONE;
5408         }
5409
5410         i40e_set_vlan_filter(vsi, vlan, 0);
5411
5412         vsi->vlan_num--;
5413         ret = I40E_SUCCESS;
5414 DONE:
5415         rte_free(mv_f);
5416         return ret;
5417 }
5418
5419 int
5420 i40e_vsi_add_mac(struct i40e_vsi *vsi, struct i40e_mac_filter_info *mac_filter)
5421 {
5422         struct i40e_mac_filter *f;
5423         struct i40e_macvlan_filter *mv_f;
5424         int i, vlan_num = 0;
5425         int ret = I40E_SUCCESS;
5426
5427         /* If it's add and we've config it, return */
5428         f = i40e_find_mac_filter(vsi, &mac_filter->mac_addr);
5429         if (f != NULL)
5430                 return I40E_SUCCESS;
5431         if ((mac_filter->filter_type == RTE_MACVLAN_PERFECT_MATCH) ||
5432                 (mac_filter->filter_type == RTE_MACVLAN_HASH_MATCH)) {
5433
5434                 /**
5435                  * If vlan_num is 0, that's the first time to add mac,
5436                  * set mask for vlan_id 0.
5437                  */
5438                 if (vsi->vlan_num == 0) {
5439                         i40e_set_vlan_filter(vsi, 0, 1);
5440                         vsi->vlan_num = 1;
5441                 }
5442                 vlan_num = vsi->vlan_num;
5443         } else if ((mac_filter->filter_type == RTE_MAC_PERFECT_MATCH) ||
5444                         (mac_filter->filter_type == RTE_MAC_HASH_MATCH))
5445                 vlan_num = 1;
5446
5447         mv_f = rte_zmalloc("macvlan_data", vlan_num * sizeof(*mv_f), 0);
5448         if (mv_f == NULL) {
5449                 PMD_DRV_LOG(ERR, "failed to allocate memory");
5450                 return I40E_ERR_NO_MEMORY;
5451         }
5452
5453         for (i = 0; i < vlan_num; i++) {
5454                 mv_f[i].filter_type = mac_filter->filter_type;
5455                 (void)rte_memcpy(&mv_f[i].macaddr, &mac_filter->mac_addr,
5456                                 ETH_ADDR_LEN);
5457         }
5458
5459         if (mac_filter->filter_type == RTE_MACVLAN_PERFECT_MATCH ||
5460                 mac_filter->filter_type == RTE_MACVLAN_HASH_MATCH) {
5461                 ret = i40e_find_all_vlan_for_mac(vsi, mv_f, vlan_num,
5462                                         &mac_filter->mac_addr);
5463                 if (ret != I40E_SUCCESS)
5464                         goto DONE;
5465         }
5466
5467         ret = i40e_add_macvlan_filters(vsi, mv_f, vlan_num);
5468         if (ret != I40E_SUCCESS)
5469                 goto DONE;
5470
5471         /* Add the mac addr into mac list */
5472         f = rte_zmalloc("macv_filter", sizeof(*f), 0);
5473         if (f == NULL) {
5474                 PMD_DRV_LOG(ERR, "failed to allocate memory");
5475                 ret = I40E_ERR_NO_MEMORY;
5476                 goto DONE;
5477         }
5478         (void)rte_memcpy(&f->mac_info.mac_addr, &mac_filter->mac_addr,
5479                         ETH_ADDR_LEN);
5480         f->mac_info.filter_type = mac_filter->filter_type;
5481         TAILQ_INSERT_TAIL(&vsi->mac_list, f, next);
5482         vsi->mac_num++;
5483
5484         ret = I40E_SUCCESS;
5485 DONE:
5486         rte_free(mv_f);
5487
5488         return ret;
5489 }
5490
5491 int
5492 i40e_vsi_delete_mac(struct i40e_vsi *vsi, struct ether_addr *addr)
5493 {
5494         struct i40e_mac_filter *f;
5495         struct i40e_macvlan_filter *mv_f;
5496         int i, vlan_num;
5497         enum rte_mac_filter_type filter_type;
5498         int ret = I40E_SUCCESS;
5499
5500         /* Can't find it, return an error */
5501         f = i40e_find_mac_filter(vsi, addr);
5502         if (f == NULL)
5503                 return I40E_ERR_PARAM;
5504
5505         vlan_num = vsi->vlan_num;
5506         filter_type = f->mac_info.filter_type;
5507         if (filter_type == RTE_MACVLAN_PERFECT_MATCH ||
5508                 filter_type == RTE_MACVLAN_HASH_MATCH) {
5509                 if (vlan_num == 0) {
5510                         PMD_DRV_LOG(ERR, "VLAN number shouldn't be 0\n");
5511                         return I40E_ERR_PARAM;
5512                 }
5513         } else if (filter_type == RTE_MAC_PERFECT_MATCH ||
5514                         filter_type == RTE_MAC_HASH_MATCH)
5515                 vlan_num = 1;
5516
5517         mv_f = rte_zmalloc("macvlan_data", vlan_num * sizeof(*mv_f), 0);
5518         if (mv_f == NULL) {
5519                 PMD_DRV_LOG(ERR, "failed to allocate memory");
5520                 return I40E_ERR_NO_MEMORY;
5521         }
5522
5523         for (i = 0; i < vlan_num; i++) {
5524                 mv_f[i].filter_type = filter_type;
5525                 (void)rte_memcpy(&mv_f[i].macaddr, &f->mac_info.mac_addr,
5526                                 ETH_ADDR_LEN);
5527         }
5528         if (filter_type == RTE_MACVLAN_PERFECT_MATCH ||
5529                         filter_type == RTE_MACVLAN_HASH_MATCH) {
5530                 ret = i40e_find_all_vlan_for_mac(vsi, mv_f, vlan_num, addr);
5531                 if (ret != I40E_SUCCESS)
5532                         goto DONE;
5533         }
5534
5535         ret = i40e_remove_macvlan_filters(vsi, mv_f, vlan_num);
5536         if (ret != I40E_SUCCESS)
5537                 goto DONE;
5538
5539         /* Remove the mac addr into mac list */
5540         TAILQ_REMOVE(&vsi->mac_list, f, next);
5541         rte_free(f);
5542         vsi->mac_num--;
5543
5544         ret = I40E_SUCCESS;
5545 DONE:
5546         rte_free(mv_f);
5547         return ret;
5548 }
5549
5550 /* Configure hash enable flags for RSS */
5551 uint64_t
5552 i40e_config_hena(uint64_t flags)
5553 {
5554         uint64_t hena = 0;
5555
5556         if (!flags)
5557                 return hena;
5558
5559         if (flags & ETH_RSS_FRAG_IPV4)
5560                 hena |= 1ULL << I40E_FILTER_PCTYPE_FRAG_IPV4;
5561         if (flags & ETH_RSS_NONFRAG_IPV4_TCP)
5562                 hena |= 1ULL << I40E_FILTER_PCTYPE_NONF_IPV4_TCP;
5563         if (flags & ETH_RSS_NONFRAG_IPV4_UDP)
5564                 hena |= 1ULL << I40E_FILTER_PCTYPE_NONF_IPV4_UDP;
5565         if (flags & ETH_RSS_NONFRAG_IPV4_SCTP)
5566                 hena |= 1ULL << I40E_FILTER_PCTYPE_NONF_IPV4_SCTP;
5567         if (flags & ETH_RSS_NONFRAG_IPV4_OTHER)
5568                 hena |= 1ULL << I40E_FILTER_PCTYPE_NONF_IPV4_OTHER;
5569         if (flags & ETH_RSS_FRAG_IPV6)
5570                 hena |= 1ULL << I40E_FILTER_PCTYPE_FRAG_IPV6;
5571         if (flags & ETH_RSS_NONFRAG_IPV6_TCP)
5572                 hena |= 1ULL << I40E_FILTER_PCTYPE_NONF_IPV6_TCP;
5573         if (flags & ETH_RSS_NONFRAG_IPV6_UDP)
5574                 hena |= 1ULL << I40E_FILTER_PCTYPE_NONF_IPV6_UDP;
5575         if (flags & ETH_RSS_NONFRAG_IPV6_SCTP)
5576                 hena |= 1ULL << I40E_FILTER_PCTYPE_NONF_IPV6_SCTP;
5577         if (flags & ETH_RSS_NONFRAG_IPV6_OTHER)
5578                 hena |= 1ULL << I40E_FILTER_PCTYPE_NONF_IPV6_OTHER;
5579         if (flags & ETH_RSS_L2_PAYLOAD)
5580                 hena |= 1ULL << I40E_FILTER_PCTYPE_L2_PAYLOAD;
5581
5582         return hena;
5583 }
5584
5585 /* Parse the hash enable flags */
5586 uint64_t
5587 i40e_parse_hena(uint64_t flags)
5588 {
5589         uint64_t rss_hf = 0;
5590
5591         if (!flags)
5592                 return rss_hf;
5593         if (flags & (1ULL << I40E_FILTER_PCTYPE_FRAG_IPV4))
5594                 rss_hf |= ETH_RSS_FRAG_IPV4;
5595         if (flags & (1ULL << I40E_FILTER_PCTYPE_NONF_IPV4_TCP))
5596                 rss_hf |= ETH_RSS_NONFRAG_IPV4_TCP;
5597         if (flags & (1ULL << I40E_FILTER_PCTYPE_NONF_IPV4_UDP))
5598                 rss_hf |= ETH_RSS_NONFRAG_IPV4_UDP;
5599         if (flags & (1ULL << I40E_FILTER_PCTYPE_NONF_IPV4_SCTP))
5600                 rss_hf |= ETH_RSS_NONFRAG_IPV4_SCTP;
5601         if (flags & (1ULL << I40E_FILTER_PCTYPE_NONF_IPV4_OTHER))
5602                 rss_hf |= ETH_RSS_NONFRAG_IPV4_OTHER;
5603         if (flags & (1ULL << I40E_FILTER_PCTYPE_FRAG_IPV6))
5604                 rss_hf |= ETH_RSS_FRAG_IPV6;
5605         if (flags & (1ULL << I40E_FILTER_PCTYPE_NONF_IPV6_TCP))
5606                 rss_hf |= ETH_RSS_NONFRAG_IPV6_TCP;
5607         if (flags & (1ULL << I40E_FILTER_PCTYPE_NONF_IPV6_UDP))
5608                 rss_hf |= ETH_RSS_NONFRAG_IPV6_UDP;
5609         if (flags & (1ULL << I40E_FILTER_PCTYPE_NONF_IPV6_SCTP))
5610                 rss_hf |= ETH_RSS_NONFRAG_IPV6_SCTP;
5611         if (flags & (1ULL << I40E_FILTER_PCTYPE_NONF_IPV6_OTHER))
5612                 rss_hf |= ETH_RSS_NONFRAG_IPV6_OTHER;
5613         if (flags & (1ULL << I40E_FILTER_PCTYPE_L2_PAYLOAD))
5614                 rss_hf |= ETH_RSS_L2_PAYLOAD;
5615
5616         return rss_hf;
5617 }
5618
5619 /* Disable RSS */
5620 static void
5621 i40e_pf_disable_rss(struct i40e_pf *pf)
5622 {
5623         struct i40e_hw *hw = I40E_PF_TO_HW(pf);
5624         uint64_t hena;
5625
5626         hena = (uint64_t)I40E_READ_REG(hw, I40E_PFQF_HENA(0));
5627         hena |= ((uint64_t)I40E_READ_REG(hw, I40E_PFQF_HENA(1))) << 32;
5628         hena &= ~I40E_RSS_HENA_ALL;
5629         I40E_WRITE_REG(hw, I40E_PFQF_HENA(0), (uint32_t)hena);
5630         I40E_WRITE_REG(hw, I40E_PFQF_HENA(1), (uint32_t)(hena >> 32));
5631         I40E_WRITE_FLUSH(hw);
5632 }
5633
5634 static int
5635 i40e_set_rss_key(struct i40e_vsi *vsi, uint8_t *key, uint8_t key_len)
5636 {
5637         struct i40e_pf *pf = I40E_VSI_TO_PF(vsi);
5638         struct i40e_hw *hw = I40E_VSI_TO_HW(vsi);
5639         int ret = 0;
5640
5641         if (!key || key_len != ((I40E_PFQF_HKEY_MAX_INDEX + 1) *
5642                 sizeof(uint32_t)))
5643                 return -EINVAL;
5644
5645         if (pf->flags & I40E_FLAG_RSS_AQ_CAPABLE) {
5646                 struct i40e_aqc_get_set_rss_key_data *key_dw =
5647                         (struct i40e_aqc_get_set_rss_key_data *)key;
5648
5649                 ret = i40e_aq_set_rss_key(hw, vsi->vsi_id, key_dw);
5650                 if (ret)
5651                         PMD_INIT_LOG(ERR, "Failed to configure RSS key "
5652                                      "via AQ");
5653         } else {
5654                 uint32_t *hash_key = (uint32_t *)key;
5655                 uint16_t i;
5656
5657                 for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
5658                         I40E_WRITE_REG(hw, I40E_PFQF_HKEY(i), hash_key[i]);
5659                 I40E_WRITE_FLUSH(hw);
5660         }
5661
5662         return ret;
5663 }
5664
5665 static int
5666 i40e_get_rss_key(struct i40e_vsi *vsi, uint8_t *key, uint8_t *key_len)
5667 {
5668         struct i40e_pf *pf = I40E_VSI_TO_PF(vsi);
5669         struct i40e_hw *hw = I40E_VSI_TO_HW(vsi);
5670         int ret;
5671
5672         if (!key || !key_len)
5673                 return -EINVAL;
5674
5675         if (pf->flags & I40E_FLAG_RSS_AQ_CAPABLE) {
5676                 ret = i40e_aq_get_rss_key(hw, vsi->vsi_id,
5677                         (struct i40e_aqc_get_set_rss_key_data *)key);
5678                 if (ret) {
5679                         PMD_INIT_LOG(ERR, "Failed to get RSS key via AQ");
5680                         return ret;
5681                 }
5682         } else {
5683                 uint32_t *key_dw = (uint32_t *)key;
5684                 uint16_t i;
5685
5686                 for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
5687                         key_dw[i] = I40E_READ_REG(hw, I40E_PFQF_HKEY(i));
5688         }
5689         *key_len = (I40E_PFQF_HKEY_MAX_INDEX + 1) * sizeof(uint32_t);
5690
5691         return 0;
5692 }
5693
5694 static int
5695 i40e_hw_rss_hash_set(struct i40e_pf *pf, struct rte_eth_rss_conf *rss_conf)
5696 {
5697         struct i40e_hw *hw = I40E_PF_TO_HW(pf);
5698         uint64_t rss_hf;
5699         uint64_t hena;
5700         int ret;
5701
5702         ret = i40e_set_rss_key(pf->main_vsi, rss_conf->rss_key,
5703                                rss_conf->rss_key_len);
5704         if (ret)
5705                 return ret;
5706
5707         rss_hf = rss_conf->rss_hf;
5708         hena = (uint64_t)I40E_READ_REG(hw, I40E_PFQF_HENA(0));
5709         hena |= ((uint64_t)I40E_READ_REG(hw, I40E_PFQF_HENA(1))) << 32;
5710         hena &= ~I40E_RSS_HENA_ALL;
5711         hena |= i40e_config_hena(rss_hf);
5712         I40E_WRITE_REG(hw, I40E_PFQF_HENA(0), (uint32_t)hena);
5713         I40E_WRITE_REG(hw, I40E_PFQF_HENA(1), (uint32_t)(hena >> 32));
5714         I40E_WRITE_FLUSH(hw);
5715
5716         return 0;
5717 }
5718
5719 static int
5720 i40e_dev_rss_hash_update(struct rte_eth_dev *dev,
5721                          struct rte_eth_rss_conf *rss_conf)
5722 {
5723         struct i40e_pf *pf = I40E_DEV_PRIVATE_TO_PF(dev->data->dev_private);
5724         struct i40e_hw *hw = I40E_DEV_PRIVATE_TO_HW(dev->data->dev_private);
5725         uint64_t rss_hf = rss_conf->rss_hf & I40E_RSS_OFFLOAD_ALL;
5726         uint64_t hena;
5727
5728         hena = (uint64_t)I40E_READ_REG(hw, I40E_PFQF_HENA(0));
5729         hena |= ((uint64_t)I40E_READ_REG(hw, I40E_PFQF_HENA(1))) << 32;
5730         if (!(hena & I40E_RSS_HENA_ALL)) { /* RSS disabled */
5731                 if (rss_hf != 0) /* Enable RSS */
5732                         return -EINVAL;
5733                 return 0; /* Nothing to do */
5734         }
5735         /* RSS enabled */
5736         if (rss_hf == 0) /* Disable RSS */
5737                 return -EINVAL;
5738
5739         return i40e_hw_rss_hash_set(pf, rss_conf);
5740 }
5741
5742 static int
5743 i40e_dev_rss_hash_conf_get(struct rte_eth_dev *dev,
5744                            struct rte_eth_rss_conf *rss_conf)
5745 {
5746         struct i40e_pf *pf = I40E_DEV_PRIVATE_TO_PF(dev->data->dev_private);
5747         struct i40e_hw *hw = I40E_DEV_PRIVATE_TO_HW(dev->data->dev_private);
5748         uint64_t hena;
5749
5750         i40e_get_rss_key(pf->main_vsi, rss_conf->rss_key,
5751                          &rss_conf->rss_key_len);
5752
5753         hena = (uint64_t)I40E_READ_REG(hw, I40E_PFQF_HENA(0));
5754         hena |= ((uint64_t)I40E_READ_REG(hw, I40E_PFQF_HENA(1))) << 32;
5755         rss_conf->rss_hf = i40e_parse_hena(hena);
5756
5757         return 0;
5758 }
5759
5760 static int
5761 i40e_dev_get_filter_type(uint16_t filter_type, uint16_t *flag)
5762 {
5763         switch (filter_type) {
5764         case RTE_TUNNEL_FILTER_IMAC_IVLAN:
5765                 *flag = I40E_AQC_ADD_CLOUD_FILTER_IMAC_IVLAN;
5766                 break;
5767         case RTE_TUNNEL_FILTER_IMAC_IVLAN_TENID:
5768                 *flag = I40E_AQC_ADD_CLOUD_FILTER_IMAC_IVLAN_TEN_ID;
5769                 break;
5770         case RTE_TUNNEL_FILTER_IMAC_TENID:
5771                 *flag = I40E_AQC_ADD_CLOUD_FILTER_IMAC_TEN_ID;
5772                 break;
5773         case RTE_TUNNEL_FILTER_OMAC_TENID_IMAC:
5774                 *flag = I40E_AQC_ADD_CLOUD_FILTER_OMAC_TEN_ID_IMAC;
5775                 break;
5776         case ETH_TUNNEL_FILTER_IMAC:
5777                 *flag = I40E_AQC_ADD_CLOUD_FILTER_IMAC;
5778                 break;
5779         default:
5780                 PMD_DRV_LOG(ERR, "invalid tunnel filter type");
5781                 return -EINVAL;
5782         }
5783
5784         return 0;
5785 }
5786
5787 static int
5788 i40e_dev_tunnel_filter_set(struct i40e_pf *pf,
5789                         struct rte_eth_tunnel_filter_conf *tunnel_filter,
5790                         uint8_t add)
5791 {
5792         uint16_t ip_type;
5793         uint8_t tun_type = 0;
5794         int val, ret = 0;
5795         struct i40e_hw *hw = I40E_PF_TO_HW(pf);
5796         struct i40e_vsi *vsi = pf->main_vsi;
5797         struct i40e_aqc_add_remove_cloud_filters_element_data  *cld_filter;
5798         struct i40e_aqc_add_remove_cloud_filters_element_data  *pfilter;
5799
5800         cld_filter = rte_zmalloc("tunnel_filter",
5801                 sizeof(struct i40e_aqc_add_remove_cloud_filters_element_data),
5802                 0);
5803
5804         if (NULL == cld_filter) {
5805                 PMD_DRV_LOG(ERR, "Failed to alloc memory.");
5806                 return -EINVAL;
5807         }
5808         pfilter = cld_filter;
5809
5810         (void)rte_memcpy(&pfilter->outer_mac, tunnel_filter->outer_mac,
5811                         sizeof(struct ether_addr));
5812         (void)rte_memcpy(&pfilter->inner_mac, tunnel_filter->inner_mac,
5813                         sizeof(struct ether_addr));
5814
5815         pfilter->inner_vlan = tunnel_filter->inner_vlan;
5816         if (tunnel_filter->ip_type == RTE_TUNNEL_IPTYPE_IPV4) {
5817                 ip_type = I40E_AQC_ADD_CLOUD_FLAGS_IPV4;
5818                 (void)rte_memcpy(&pfilter->ipaddr.v4.data,
5819                                 &tunnel_filter->ip_addr,
5820                                 sizeof(pfilter->ipaddr.v4.data));
5821         } else {
5822                 ip_type = I40E_AQC_ADD_CLOUD_FLAGS_IPV6;
5823                 (void)rte_memcpy(&pfilter->ipaddr.v6.data,
5824                                 &tunnel_filter->ip_addr,
5825                                 sizeof(pfilter->ipaddr.v6.data));
5826         }
5827
5828         /* check tunneled type */
5829         switch (tunnel_filter->tunnel_type) {
5830         case RTE_TUNNEL_TYPE_VXLAN:
5831                 tun_type = I40E_AQC_ADD_CLOUD_TNL_TYPE_XVLAN;
5832                 break;
5833         case RTE_TUNNEL_TYPE_NVGRE:
5834                 tun_type = I40E_AQC_ADD_CLOUD_TNL_TYPE_NVGRE_OMAC;
5835                 break;
5836         default:
5837                 /* Other tunnel types is not supported. */
5838                 PMD_DRV_LOG(ERR, "tunnel type is not supported.");
5839                 rte_free(cld_filter);
5840                 return -EINVAL;
5841         }
5842
5843         val = i40e_dev_get_filter_type(tunnel_filter->filter_type,
5844                                                 &pfilter->flags);
5845         if (val < 0) {
5846                 rte_free(cld_filter);
5847                 return -EINVAL;
5848         }
5849
5850         pfilter->flags |= I40E_AQC_ADD_CLOUD_FLAGS_TO_QUEUE | ip_type |
5851                 (tun_type << I40E_AQC_ADD_CLOUD_TNL_TYPE_SHIFT);
5852         pfilter->tenant_id = tunnel_filter->tenant_id;
5853         pfilter->queue_number = tunnel_filter->queue_id;
5854
5855         if (add)
5856                 ret = i40e_aq_add_cloud_filters(hw, vsi->seid, cld_filter, 1);
5857         else
5858                 ret = i40e_aq_remove_cloud_filters(hw, vsi->seid,
5859                                                 cld_filter, 1);
5860
5861         rte_free(cld_filter);
5862         return ret;
5863 }
5864
5865 static int
5866 i40e_get_vxlan_port_idx(struct i40e_pf *pf, uint16_t port)
5867 {
5868         uint8_t i;
5869
5870         for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
5871                 if (pf->vxlan_ports[i] == port)
5872                         return i;
5873         }
5874
5875         return -1;
5876 }
5877
5878 static int
5879 i40e_add_vxlan_port(struct i40e_pf *pf, uint16_t port)
5880 {
5881         int  idx, ret;
5882         uint8_t filter_idx;
5883         struct i40e_hw *hw = I40E_PF_TO_HW(pf);
5884
5885         idx = i40e_get_vxlan_port_idx(pf, port);
5886
5887         /* Check if port already exists */
5888         if (idx >= 0) {
5889                 PMD_DRV_LOG(ERR, "Port %d already offloaded", port);
5890                 return -EINVAL;
5891         }
5892
5893         /* Now check if there is space to add the new port */
5894         idx = i40e_get_vxlan_port_idx(pf, 0);
5895         if (idx < 0) {
5896                 PMD_DRV_LOG(ERR, "Maximum number of UDP ports reached,"
5897                         "not adding port %d", port);
5898                 return -ENOSPC;
5899         }
5900
5901         ret =  i40e_aq_add_udp_tunnel(hw, port, I40E_AQC_TUNNEL_TYPE_VXLAN,
5902                                         &filter_idx, NULL);
5903         if (ret < 0) {
5904                 PMD_DRV_LOG(ERR, "Failed to add VXLAN UDP port %d", port);
5905                 return -1;
5906         }
5907
5908         PMD_DRV_LOG(INFO, "Added port %d with AQ command with index %d",
5909                          port,  filter_idx);
5910
5911         /* New port: add it and mark its index in the bitmap */
5912         pf->vxlan_ports[idx] = port;
5913         pf->vxlan_bitmap |= (1 << idx);
5914
5915         if (!(pf->flags & I40E_FLAG_VXLAN))
5916                 pf->flags |= I40E_FLAG_VXLAN;
5917
5918         return 0;
5919 }
5920
5921 static int
5922 i40e_del_vxlan_port(struct i40e_pf *pf, uint16_t port)
5923 {
5924         int idx;
5925         struct i40e_hw *hw = I40E_PF_TO_HW(pf);
5926
5927         if (!(pf->flags & I40E_FLAG_VXLAN)) {
5928                 PMD_DRV_LOG(ERR, "VXLAN UDP port was not configured.");
5929                 return -EINVAL;
5930         }
5931
5932         idx = i40e_get_vxlan_port_idx(pf, port);
5933
5934         if (idx < 0) {
5935                 PMD_DRV_LOG(ERR, "Port %d doesn't exist", port);
5936                 return -EINVAL;
5937         }
5938
5939         if (i40e_aq_del_udp_tunnel(hw, idx, NULL) < 0) {
5940                 PMD_DRV_LOG(ERR, "Failed to delete VXLAN UDP port %d", port);
5941                 return -1;
5942         }
5943
5944         PMD_DRV_LOG(INFO, "Deleted port %d with AQ command with index %d",
5945                         port, idx);
5946
5947         pf->vxlan_ports[idx] = 0;
5948         pf->vxlan_bitmap &= ~(1 << idx);
5949
5950         if (!pf->vxlan_bitmap)
5951                 pf->flags &= ~I40E_FLAG_VXLAN;
5952
5953         return 0;
5954 }
5955
5956 /* Add UDP tunneling port */
5957 static int
5958 i40e_dev_udp_tunnel_add(struct rte_eth_dev *dev,
5959                         struct rte_eth_udp_tunnel *udp_tunnel)
5960 {
5961         int ret = 0;
5962         struct i40e_pf *pf = I40E_DEV_PRIVATE_TO_PF(dev->data->dev_private);
5963
5964         if (udp_tunnel == NULL)
5965                 return -EINVAL;
5966
5967         switch (udp_tunnel->prot_type) {
5968         case RTE_TUNNEL_TYPE_VXLAN:
5969                 ret = i40e_add_vxlan_port(pf, udp_tunnel->udp_port);
5970                 break;
5971
5972         case RTE_TUNNEL_TYPE_GENEVE:
5973         case RTE_TUNNEL_TYPE_TEREDO:
5974                 PMD_DRV_LOG(ERR, "Tunnel type is not supported now.");
5975                 ret = -1;
5976                 break;
5977
5978         default:
5979                 PMD_DRV_LOG(ERR, "Invalid tunnel type");
5980                 ret = -1;
5981                 break;
5982         }
5983
5984         return ret;
5985 }
5986
5987 /* Remove UDP tunneling port */
5988 static int
5989 i40e_dev_udp_tunnel_del(struct rte_eth_dev *dev,
5990                         struct rte_eth_udp_tunnel *udp_tunnel)
5991 {
5992         int ret = 0;
5993         struct i40e_pf *pf = I40E_DEV_PRIVATE_TO_PF(dev->data->dev_private);
5994
5995         if (udp_tunnel == NULL)
5996                 return -EINVAL;
5997
5998         switch (udp_tunnel->prot_type) {
5999         case RTE_TUNNEL_TYPE_VXLAN:
6000                 ret = i40e_del_vxlan_port(pf, udp_tunnel->udp_port);
6001                 break;
6002         case RTE_TUNNEL_TYPE_GENEVE:
6003         case RTE_TUNNEL_TYPE_TEREDO:
6004                 PMD_DRV_LOG(ERR, "Tunnel type is not supported now.");
6005                 ret = -1;
6006                 break;
6007         default:
6008                 PMD_DRV_LOG(ERR, "Invalid tunnel type");
6009                 ret = -1;
6010                 break;
6011         }
6012
6013         return ret;
6014 }
6015
6016 /* Calculate the maximum number of contiguous PF queues that are configured */
6017 static int
6018 i40e_pf_calc_configured_queues_num(struct i40e_pf *pf)
6019 {
6020         struct rte_eth_dev_data *data = pf->dev_data;
6021         int i, num;
6022         struct i40e_rx_queue *rxq;
6023
6024         num = 0;
6025         for (i = 0; i < pf->lan_nb_qps; i++) {
6026                 rxq = data->rx_queues[i];
6027                 if (rxq && rxq->q_set)
6028                         num++;
6029                 else
6030                         break;
6031         }
6032
6033         return num;
6034 }
6035
6036 /* Configure RSS */
6037 static int
6038 i40e_pf_config_rss(struct i40e_pf *pf)
6039 {
6040         struct i40e_hw *hw = I40E_PF_TO_HW(pf);
6041         struct rte_eth_rss_conf rss_conf;
6042         uint32_t i, lut = 0;
6043         uint16_t j, num;
6044
6045         /*
6046          * If both VMDQ and RSS enabled, not all of PF queues are configured.
6047          * It's necessary to calulate the actual PF queues that are configured.
6048          */
6049         if (pf->dev_data->dev_conf.rxmode.mq_mode & ETH_MQ_RX_VMDQ_FLAG)
6050                 num = i40e_pf_calc_configured_queues_num(pf);
6051         else
6052                 num = pf->dev_data->nb_rx_queues;
6053
6054         num = RTE_MIN(num, I40E_MAX_Q_PER_TC);
6055         PMD_INIT_LOG(INFO, "Max of contiguous %u PF queues are configured",
6056                         num);
6057
6058         if (num == 0) {
6059                 PMD_INIT_LOG(ERR, "No PF queues are configured to enable RSS");
6060                 return -ENOTSUP;
6061         }
6062
6063         for (i = 0, j = 0; i < hw->func_caps.rss_table_size; i++, j++) {
6064                 if (j == num)
6065                         j = 0;
6066                 lut = (lut << 8) | (j & ((0x1 <<
6067                         hw->func_caps.rss_table_entry_width) - 1));
6068                 if ((i & 3) == 3)
6069                         I40E_WRITE_REG(hw, I40E_PFQF_HLUT(i >> 2), lut);
6070         }
6071
6072         rss_conf = pf->dev_data->dev_conf.rx_adv_conf.rss_conf;
6073         if ((rss_conf.rss_hf & I40E_RSS_OFFLOAD_ALL) == 0) {
6074                 i40e_pf_disable_rss(pf);
6075                 return 0;
6076         }
6077         if (rss_conf.rss_key == NULL || rss_conf.rss_key_len <
6078                 (I40E_PFQF_HKEY_MAX_INDEX + 1) * sizeof(uint32_t)) {
6079                 /* Random default keys */
6080                 static uint32_t rss_key_default[] = {0x6b793944,
6081                         0x23504cb5, 0x5bea75b6, 0x309f4f12, 0x3dc0a2b8,
6082                         0x024ddcdf, 0x339b8ca0, 0x4c4af64a, 0x34fac605,
6083                         0x55d85839, 0x3a58997d, 0x2ec938e1, 0x66031581};
6084
6085                 rss_conf.rss_key = (uint8_t *)rss_key_default;
6086                 rss_conf.rss_key_len = (I40E_PFQF_HKEY_MAX_INDEX + 1) *
6087                                                         sizeof(uint32_t);
6088         }
6089
6090         return i40e_hw_rss_hash_set(pf, &rss_conf);
6091 }
6092
6093 static int
6094 i40e_tunnel_filter_param_check(struct i40e_pf *pf,
6095                                struct rte_eth_tunnel_filter_conf *filter)
6096 {
6097         if (pf == NULL || filter == NULL) {
6098                 PMD_DRV_LOG(ERR, "Invalid parameter");
6099                 return -EINVAL;
6100         }
6101
6102         if (filter->queue_id >= pf->dev_data->nb_rx_queues) {
6103                 PMD_DRV_LOG(ERR, "Invalid queue ID");
6104                 return -EINVAL;
6105         }
6106
6107         if (filter->inner_vlan > ETHER_MAX_VLAN_ID) {
6108                 PMD_DRV_LOG(ERR, "Invalid inner VLAN ID");
6109                 return -EINVAL;
6110         }
6111
6112         if ((filter->filter_type & ETH_TUNNEL_FILTER_OMAC) &&
6113                 (is_zero_ether_addr(filter->outer_mac))) {
6114                 PMD_DRV_LOG(ERR, "Cannot add NULL outer MAC address");
6115                 return -EINVAL;
6116         }
6117
6118         if ((filter->filter_type & ETH_TUNNEL_FILTER_IMAC) &&
6119                 (is_zero_ether_addr(filter->inner_mac))) {
6120                 PMD_DRV_LOG(ERR, "Cannot add NULL inner MAC address");
6121                 return -EINVAL;
6122         }
6123
6124         return 0;
6125 }
6126
6127 #define I40E_GL_PRS_FVBM_MSK_ENA 0x80000000
6128 #define I40E_GL_PRS_FVBM(_i)     (0x00269760 + ((_i) * 4))
6129 static int
6130 i40e_dev_set_gre_key_len(struct i40e_hw *hw, uint8_t len)
6131 {
6132         uint32_t val, reg;
6133         int ret = -EINVAL;
6134
6135         val = I40E_READ_REG(hw, I40E_GL_PRS_FVBM(2));
6136         PMD_DRV_LOG(DEBUG, "Read original GL_PRS_FVBM with 0x%08x\n", val);
6137
6138         if (len == 3) {
6139                 reg = val | I40E_GL_PRS_FVBM_MSK_ENA;
6140         } else if (len == 4) {
6141                 reg = val & ~I40E_GL_PRS_FVBM_MSK_ENA;
6142         } else {
6143                 PMD_DRV_LOG(ERR, "Unsupported GRE key length of %u", len);
6144                 return ret;
6145         }
6146
6147         if (reg != val) {
6148                 ret = i40e_aq_debug_write_register(hw, I40E_GL_PRS_FVBM(2),
6149                                                    reg, NULL);
6150                 if (ret != 0)
6151                         return ret;
6152         } else {
6153                 ret = 0;
6154         }
6155         PMD_DRV_LOG(DEBUG, "Read modified GL_PRS_FVBM with 0x%08x\n",
6156                     I40E_READ_REG(hw, I40E_GL_PRS_FVBM(2)));
6157
6158         return ret;
6159 }
6160
6161 static int
6162 i40e_dev_global_config_set(struct i40e_hw *hw, struct rte_eth_global_cfg *cfg)
6163 {
6164         int ret = -EINVAL;
6165
6166         if (!hw || !cfg)
6167                 return -EINVAL;
6168
6169         switch (cfg->cfg_type) {
6170         case RTE_ETH_GLOBAL_CFG_TYPE_GRE_KEY_LEN:
6171                 ret = i40e_dev_set_gre_key_len(hw, cfg->cfg.gre_key_len);
6172                 break;
6173         default:
6174                 PMD_DRV_LOG(ERR, "Unknown config type %u", cfg->cfg_type);
6175                 break;
6176         }
6177
6178         return ret;
6179 }
6180
6181 static int
6182 i40e_filter_ctrl_global_config(struct rte_eth_dev *dev,
6183                                enum rte_filter_op filter_op,
6184                                void *arg)
6185 {
6186         struct i40e_hw *hw = I40E_DEV_PRIVATE_TO_HW(dev->data->dev_private);
6187         int ret = I40E_ERR_PARAM;
6188
6189         switch (filter_op) {
6190         case RTE_ETH_FILTER_SET:
6191                 ret = i40e_dev_global_config_set(hw,
6192                         (struct rte_eth_global_cfg *)arg);
6193                 break;
6194         default:
6195                 PMD_DRV_LOG(ERR, "unknown operation %u", filter_op);
6196                 break;
6197         }
6198
6199         return ret;
6200 }
6201
6202 static int
6203 i40e_tunnel_filter_handle(struct rte_eth_dev *dev,
6204                           enum rte_filter_op filter_op,
6205                           void *arg)
6206 {
6207         struct rte_eth_tunnel_filter_conf *filter;
6208         struct i40e_pf *pf = I40E_DEV_PRIVATE_TO_PF(dev->data->dev_private);
6209         int ret = I40E_SUCCESS;
6210
6211         filter = (struct rte_eth_tunnel_filter_conf *)(arg);
6212
6213         if (i40e_tunnel_filter_param_check(pf, filter) < 0)
6214                 return I40E_ERR_PARAM;
6215
6216         switch (filter_op) {
6217         case RTE_ETH_FILTER_NOP:
6218                 if (!(pf->flags & I40E_FLAG_VXLAN))
6219                         ret = I40E_NOT_SUPPORTED;
6220                 break;
6221         case RTE_ETH_FILTER_ADD:
6222                 ret = i40e_dev_tunnel_filter_set(pf, filter, 1);
6223                 break;
6224         case RTE_ETH_FILTER_DELETE:
6225                 ret = i40e_dev_tunnel_filter_set(pf, filter, 0);
6226                 break;
6227         default:
6228                 PMD_DRV_LOG(ERR, "unknown operation %u", filter_op);
6229                 ret = I40E_ERR_PARAM;
6230                 break;
6231         }
6232
6233         return ret;
6234 }
6235
6236 static int
6237 i40e_pf_config_mq_rx(struct i40e_pf *pf)
6238 {
6239         int ret = 0;
6240         enum rte_eth_rx_mq_mode mq_mode = pf->dev_data->dev_conf.rxmode.mq_mode;
6241
6242         /* RSS setup */
6243         if (mq_mode & ETH_MQ_RX_RSS_FLAG)
6244                 ret = i40e_pf_config_rss(pf);
6245         else
6246                 i40e_pf_disable_rss(pf);
6247
6248         return ret;
6249 }
6250
6251 /* Get the symmetric hash enable configurations per port */
6252 static void
6253 i40e_get_symmetric_hash_enable_per_port(struct i40e_hw *hw, uint8_t *enable)
6254 {
6255         uint32_t reg = I40E_READ_REG(hw, I40E_PRTQF_CTL_0);
6256
6257         *enable = reg & I40E_PRTQF_CTL_0_HSYM_ENA_MASK ? 1 : 0;
6258 }
6259
6260 /* Set the symmetric hash enable configurations per port */
6261 static void
6262 i40e_set_symmetric_hash_enable_per_port(struct i40e_hw *hw, uint8_t enable)
6263 {
6264         uint32_t reg = I40E_READ_REG(hw, I40E_PRTQF_CTL_0);
6265
6266         if (enable > 0) {
6267                 if (reg & I40E_PRTQF_CTL_0_HSYM_ENA_MASK) {
6268                         PMD_DRV_LOG(INFO, "Symmetric hash has already "
6269                                                         "been enabled");
6270                         return;
6271                 }
6272                 reg |= I40E_PRTQF_CTL_0_HSYM_ENA_MASK;
6273         } else {
6274                 if (!(reg & I40E_PRTQF_CTL_0_HSYM_ENA_MASK)) {
6275                         PMD_DRV_LOG(INFO, "Symmetric hash has already "
6276                                                         "been disabled");
6277                         return;
6278                 }
6279                 reg &= ~I40E_PRTQF_CTL_0_HSYM_ENA_MASK;
6280         }
6281         I40E_WRITE_REG(hw, I40E_PRTQF_CTL_0, reg);
6282         I40E_WRITE_FLUSH(hw);
6283 }
6284
6285 /*
6286  * Get global configurations of hash function type and symmetric hash enable
6287  * per flow type (pctype). Note that global configuration means it affects all
6288  * the ports on the same NIC.
6289  */
6290 static int
6291 i40e_get_hash_filter_global_config(struct i40e_hw *hw,
6292                                    struct rte_eth_hash_global_conf *g_cfg)
6293 {
6294         uint32_t reg, mask = I40E_FLOW_TYPES;
6295         uint16_t i;
6296         enum i40e_filter_pctype pctype;
6297
6298         memset(g_cfg, 0, sizeof(*g_cfg));
6299         reg = I40E_READ_REG(hw, I40E_GLQF_CTL);
6300         if (reg & I40E_GLQF_CTL_HTOEP_MASK)
6301                 g_cfg->hash_func = RTE_ETH_HASH_FUNCTION_TOEPLITZ;
6302         else
6303                 g_cfg->hash_func = RTE_ETH_HASH_FUNCTION_SIMPLE_XOR;
6304         PMD_DRV_LOG(DEBUG, "Hash function is %s",
6305                 (reg & I40E_GLQF_CTL_HTOEP_MASK) ? "Toeplitz" : "Simple XOR");
6306
6307         for (i = 0; mask && i < RTE_ETH_FLOW_MAX; i++) {
6308                 if (!(mask & (1UL << i)))
6309                         continue;
6310                 mask &= ~(1UL << i);
6311                 /* Bit set indicats the coresponding flow type is supported */
6312                 g_cfg->valid_bit_mask[0] |= (1UL << i);
6313                 pctype = i40e_flowtype_to_pctype(i);
6314                 reg = I40E_READ_REG(hw, I40E_GLQF_HSYM(pctype));
6315                 if (reg & I40E_GLQF_HSYM_SYMH_ENA_MASK)
6316                         g_cfg->sym_hash_enable_mask[0] |= (1UL << i);
6317         }
6318
6319         return 0;
6320 }
6321
6322 static int
6323 i40e_hash_global_config_check(struct rte_eth_hash_global_conf *g_cfg)
6324 {
6325         uint32_t i;
6326         uint32_t mask0, i40e_mask = I40E_FLOW_TYPES;
6327
6328         if (g_cfg->hash_func != RTE_ETH_HASH_FUNCTION_TOEPLITZ &&
6329                 g_cfg->hash_func != RTE_ETH_HASH_FUNCTION_SIMPLE_XOR &&
6330                 g_cfg->hash_func != RTE_ETH_HASH_FUNCTION_DEFAULT) {
6331                 PMD_DRV_LOG(ERR, "Unsupported hash function type %d",
6332                                                 g_cfg->hash_func);
6333                 return -EINVAL;
6334         }
6335
6336         /*
6337          * As i40e supports less than 32 flow types, only first 32 bits need to
6338          * be checked.
6339          */
6340         mask0 = g_cfg->valid_bit_mask[0];
6341         for (i = 0; i < RTE_SYM_HASH_MASK_ARRAY_SIZE; i++) {
6342                 if (i == 0) {
6343                         /* Check if any unsupported flow type configured */
6344                         if ((mask0 | i40e_mask) ^ i40e_mask)
6345                                 goto mask_err;
6346                 } else {
6347                         if (g_cfg->valid_bit_mask[i])
6348                                 goto mask_err;
6349                 }
6350         }
6351
6352         return 0;
6353
6354 mask_err:
6355         PMD_DRV_LOG(ERR, "i40e unsupported flow type bit(s) configured");
6356
6357         return -EINVAL;
6358 }
6359
6360 /*
6361  * Set global configurations of hash function type and symmetric hash enable
6362  * per flow type (pctype). Note any modifying global configuration will affect
6363  * all the ports on the same NIC.
6364  */
6365 static int
6366 i40e_set_hash_filter_global_config(struct i40e_hw *hw,
6367                                    struct rte_eth_hash_global_conf *g_cfg)
6368 {
6369         int ret;
6370         uint16_t i;
6371         uint32_t reg;
6372         uint32_t mask0 = g_cfg->valid_bit_mask[0];
6373         enum i40e_filter_pctype pctype;
6374
6375         /* Check the input parameters */
6376         ret = i40e_hash_global_config_check(g_cfg);
6377         if (ret < 0)
6378                 return ret;
6379
6380         for (i = 0; mask0 && i < UINT32_BIT; i++) {
6381                 if (!(mask0 & (1UL << i)))
6382                         continue;
6383                 mask0 &= ~(1UL << i);
6384                 pctype = i40e_flowtype_to_pctype(i);
6385                 reg = (g_cfg->sym_hash_enable_mask[0] & (1UL << i)) ?
6386                                 I40E_GLQF_HSYM_SYMH_ENA_MASK : 0;
6387                 I40E_WRITE_REG(hw, I40E_GLQF_HSYM(pctype), reg);
6388         }
6389
6390         reg = I40E_READ_REG(hw, I40E_GLQF_CTL);
6391         if (g_cfg->hash_func == RTE_ETH_HASH_FUNCTION_TOEPLITZ) {
6392                 /* Toeplitz */
6393                 if (reg & I40E_GLQF_CTL_HTOEP_MASK) {
6394                         PMD_DRV_LOG(DEBUG, "Hash function already set to "
6395                                                                 "Toeplitz");
6396                         goto out;
6397                 }
6398                 reg |= I40E_GLQF_CTL_HTOEP_MASK;
6399         } else if (g_cfg->hash_func == RTE_ETH_HASH_FUNCTION_SIMPLE_XOR) {
6400                 /* Simple XOR */
6401                 if (!(reg & I40E_GLQF_CTL_HTOEP_MASK)) {
6402                         PMD_DRV_LOG(DEBUG, "Hash function already set to "
6403                                                         "Simple XOR");
6404                         goto out;
6405                 }
6406                 reg &= ~I40E_GLQF_CTL_HTOEP_MASK;
6407         } else
6408                 /* Use the default, and keep it as it is */
6409                 goto out;
6410
6411         I40E_WRITE_REG(hw, I40E_GLQF_CTL, reg);
6412
6413 out:
6414         I40E_WRITE_FLUSH(hw);
6415
6416         return 0;
6417 }
6418
6419 /**
6420  * Valid input sets for hash and flow director filters per PCTYPE
6421  */
6422 static uint64_t
6423 i40e_get_valid_input_set(enum i40e_filter_pctype pctype,
6424                 enum rte_filter_type filter)
6425 {
6426         uint64_t valid;
6427
6428         static const uint64_t valid_hash_inset_table[] = {
6429                 [I40E_FILTER_PCTYPE_FRAG_IPV4] =
6430                         I40E_INSET_DMAC | I40E_INSET_SMAC |
6431                         I40E_INSET_VLAN_OUTER | I40E_INSET_VLAN_INNER |
6432                         I40E_INSET_VLAN_TUNNEL | I40E_INSET_IPV4_SRC |
6433                         I40E_INSET_IPV4_DST | I40E_INSET_IPV4_TOS |
6434                         I40E_INSET_IPV4_PROTO | I40E_INSET_IPV4_TTL |
6435                         I40E_INSET_TUNNEL_DMAC | I40E_INSET_TUNNEL_ID |
6436                         I40E_INSET_FLEX_PAYLOAD,
6437                 [I40E_FILTER_PCTYPE_NONF_IPV4_UDP] =
6438                         I40E_INSET_DMAC | I40E_INSET_SMAC |
6439                         I40E_INSET_VLAN_OUTER | I40E_INSET_VLAN_INNER |
6440                         I40E_INSET_VLAN_TUNNEL | I40E_INSET_IPV4_TOS |
6441                         I40E_INSET_IPV4_PROTO | I40E_INSET_IPV4_TTL |
6442                         I40E_INSET_TUNNEL_DMAC | I40E_INSET_TUNNEL_ID |
6443                         I40E_INSET_IPV4_SRC | I40E_INSET_IPV4_DST |
6444                         I40E_INSET_SRC_PORT | I40E_INSET_DST_PORT |
6445                         I40E_INSET_FLEX_PAYLOAD,
6446                 [I40E_FILTER_PCTYPE_NONF_IPV4_TCP] =
6447                         I40E_INSET_DMAC | I40E_INSET_SMAC |
6448                         I40E_INSET_VLAN_OUTER | I40E_INSET_VLAN_INNER |
6449                         I40E_INSET_VLAN_TUNNEL | I40E_INSET_IPV4_TOS |
6450                         I40E_INSET_IPV4_PROTO | I40E_INSET_IPV4_TTL |
6451                         I40E_INSET_TUNNEL_DMAC | I40E_INSET_TUNNEL_ID |
6452                         I40E_INSET_IPV4_SRC | I40E_INSET_IPV4_DST |
6453                         I40E_INSET_SRC_PORT | I40E_INSET_DST_PORT |
6454                         I40E_INSET_TCP_FLAGS | I40E_INSET_FLEX_PAYLOAD,
6455                 [I40E_FILTER_PCTYPE_NONF_IPV4_SCTP] =
6456                         I40E_INSET_DMAC | I40E_INSET_SMAC |
6457                         I40E_INSET_VLAN_OUTER | I40E_INSET_VLAN_INNER |
6458                         I40E_INSET_VLAN_TUNNEL | I40E_INSET_IPV4_TOS |
6459                         I40E_INSET_IPV4_PROTO | I40E_INSET_IPV4_TTL |
6460                         I40E_INSET_TUNNEL_DMAC | I40E_INSET_TUNNEL_ID |
6461                         I40E_INSET_IPV4_SRC | I40E_INSET_IPV4_DST |
6462                         I40E_INSET_SRC_PORT | I40E_INSET_DST_PORT |
6463                         I40E_INSET_SCTP_VT | I40E_INSET_FLEX_PAYLOAD,
6464                 [I40E_FILTER_PCTYPE_NONF_IPV4_OTHER] =
6465                         I40E_INSET_DMAC | I40E_INSET_SMAC |
6466                         I40E_INSET_VLAN_OUTER | I40E_INSET_VLAN_INNER |
6467                         I40E_INSET_VLAN_TUNNEL | I40E_INSET_IPV4_TOS |
6468                         I40E_INSET_IPV4_PROTO | I40E_INSET_IPV4_TTL |
6469                         I40E_INSET_TUNNEL_DMAC | I40E_INSET_TUNNEL_ID |
6470                         I40E_INSET_IPV4_SRC | I40E_INSET_IPV4_DST |
6471                         I40E_INSET_FLEX_PAYLOAD,
6472                 [I40E_FILTER_PCTYPE_FRAG_IPV6] =
6473                         I40E_INSET_DMAC | I40E_INSET_SMAC |
6474                         I40E_INSET_VLAN_OUTER | I40E_INSET_VLAN_INNER |
6475                         I40E_INSET_VLAN_TUNNEL | I40E_INSET_IPV6_TC |
6476                         I40E_INSET_IPV6_FLOW | I40E_INSET_IPV6_NEXT_HDR |
6477                         I40E_INSET_IPV6_HOP_LIMIT | I40E_INSET_TUNNEL_DMAC |
6478                         I40E_INSET_TUNNEL_ID | I40E_INSET_IPV6_SRC |
6479                         I40E_INSET_IPV6_DST | I40E_INSET_FLEX_PAYLOAD,
6480                 [I40E_FILTER_PCTYPE_NONF_IPV6_UDP] =
6481                         I40E_INSET_DMAC | I40E_INSET_SMAC |
6482                         I40E_INSET_VLAN_OUTER | I40E_INSET_VLAN_INNER |
6483                         I40E_INSET_VLAN_TUNNEL | I40E_INSET_IPV6_TC |
6484                         I40E_INSET_IPV6_FLOW | I40E_INSET_IPV6_NEXT_HDR |
6485                         I40E_INSET_IPV6_HOP_LIMIT | I40E_INSET_IPV6_SRC |
6486                         I40E_INSET_IPV6_DST | I40E_INSET_SRC_PORT |
6487                         I40E_INSET_DST_PORT | I40E_INSET_FLEX_PAYLOAD,
6488                 [I40E_FILTER_PCTYPE_NONF_IPV6_TCP] =
6489                         I40E_INSET_DMAC | I40E_INSET_SMAC |
6490                         I40E_INSET_VLAN_OUTER | I40E_INSET_VLAN_INNER |
6491                         I40E_INSET_VLAN_TUNNEL | I40E_INSET_IPV6_TC |
6492                         I40E_INSET_IPV6_FLOW | I40E_INSET_IPV6_NEXT_HDR |
6493                         I40E_INSET_IPV6_HOP_LIMIT | I40E_INSET_IPV6_SRC |
6494                         I40E_INSET_IPV6_DST | I40E_INSET_SRC_PORT |
6495                         I40E_INSET_DST_PORT | I40E_INSET_TCP_FLAGS |
6496                         I40E_INSET_FLEX_PAYLOAD,
6497                 [I40E_FILTER_PCTYPE_NONF_IPV6_SCTP] =
6498                         I40E_INSET_DMAC | I40E_INSET_SMAC |
6499                         I40E_INSET_VLAN_OUTER | I40E_INSET_VLAN_INNER |
6500                         I40E_INSET_VLAN_TUNNEL | I40E_INSET_IPV6_TC |
6501                         I40E_INSET_IPV6_FLOW | I40E_INSET_IPV6_NEXT_HDR |
6502                         I40E_INSET_IPV6_HOP_LIMIT | I40E_INSET_IPV6_SRC |
6503                         I40E_INSET_IPV6_DST | I40E_INSET_SRC_PORT |
6504                         I40E_INSET_DST_PORT | I40E_INSET_SCTP_VT |
6505                         I40E_INSET_FLEX_PAYLOAD,
6506                 [I40E_FILTER_PCTYPE_NONF_IPV6_OTHER] =
6507                         I40E_INSET_DMAC | I40E_INSET_SMAC |
6508                         I40E_INSET_VLAN_OUTER | I40E_INSET_VLAN_INNER |
6509                         I40E_INSET_VLAN_TUNNEL | I40E_INSET_IPV6_TC |
6510                         I40E_INSET_IPV6_FLOW | I40E_INSET_IPV6_NEXT_HDR |
6511                         I40E_INSET_IPV6_HOP_LIMIT | I40E_INSET_IPV6_SRC |
6512                         I40E_INSET_IPV6_DST | I40E_INSET_TUNNEL_ID |
6513                         I40E_INSET_FLEX_PAYLOAD,
6514                 [I40E_FILTER_PCTYPE_L2_PAYLOAD] =
6515                         I40E_INSET_DMAC | I40E_INSET_SMAC |
6516                         I40E_INSET_VLAN_OUTER | I40E_INSET_VLAN_INNER |
6517                         I40E_INSET_VLAN_TUNNEL | I40E_INSET_LAST_ETHER_TYPE |
6518                         I40E_INSET_FLEX_PAYLOAD,
6519         };
6520
6521         /**
6522          * Flow director supports only fields defined in
6523          * union rte_eth_fdir_flow.
6524          */
6525         static const uint64_t valid_fdir_inset_table[] = {
6526                 [I40E_FILTER_PCTYPE_FRAG_IPV4] =
6527                 I40E_INSET_IPV4_SRC | I40E_INSET_IPV4_DST |
6528                 I40E_INSET_FLEX_PAYLOAD,
6529                 [I40E_FILTER_PCTYPE_NONF_IPV4_UDP] =
6530                 I40E_INSET_IPV4_SRC | I40E_INSET_IPV4_DST |
6531                 I40E_INSET_SRC_PORT | I40E_INSET_DST_PORT |
6532                 I40E_INSET_FLEX_PAYLOAD,
6533                 [I40E_FILTER_PCTYPE_NONF_IPV4_TCP] =
6534                 I40E_INSET_IPV4_SRC | I40E_INSET_IPV4_DST |
6535                 I40E_INSET_SRC_PORT | I40E_INSET_DST_PORT |
6536                 I40E_INSET_FLEX_PAYLOAD,
6537                 [I40E_FILTER_PCTYPE_NONF_IPV4_SCTP] =
6538                 I40E_INSET_IPV4_SRC | I40E_INSET_IPV4_DST |
6539                 I40E_INSET_SRC_PORT | I40E_INSET_DST_PORT |
6540                 I40E_INSET_SCTP_VT | I40E_INSET_FLEX_PAYLOAD,
6541                 [I40E_FILTER_PCTYPE_NONF_IPV4_OTHER] =
6542                 I40E_INSET_IPV4_SRC | I40E_INSET_IPV4_DST |
6543                 I40E_INSET_FLEX_PAYLOAD,
6544                 [I40E_FILTER_PCTYPE_FRAG_IPV6] =
6545                 I40E_INSET_IPV6_SRC | I40E_INSET_IPV6_DST |
6546                 I40E_INSET_FLEX_PAYLOAD,
6547                 [I40E_FILTER_PCTYPE_NONF_IPV6_UDP] =
6548                 I40E_INSET_IPV6_SRC | I40E_INSET_IPV6_DST |
6549                 I40E_INSET_SRC_PORT | I40E_INSET_DST_PORT |
6550                 I40E_INSET_FLEX_PAYLOAD,
6551                 [I40E_FILTER_PCTYPE_NONF_IPV6_TCP] =
6552                 I40E_INSET_IPV6_SRC | I40E_INSET_IPV6_DST |
6553                 I40E_INSET_SRC_PORT | I40E_INSET_DST_PORT |
6554                 I40E_INSET_FLEX_PAYLOAD,
6555                 [I40E_FILTER_PCTYPE_NONF_IPV6_SCTP] =
6556                 I40E_INSET_IPV6_SRC | I40E_INSET_IPV6_DST |
6557                 I40E_INSET_SRC_PORT | I40E_INSET_DST_PORT |
6558                 I40E_INSET_SCTP_VT | I40E_INSET_FLEX_PAYLOAD,
6559                 [I40E_FILTER_PCTYPE_NONF_IPV6_OTHER] =
6560                 I40E_INSET_IPV6_SRC | I40E_INSET_IPV6_DST |
6561                 I40E_INSET_FLEX_PAYLOAD,
6562                 [I40E_FILTER_PCTYPE_L2_PAYLOAD] =
6563                 I40E_INSET_LAST_ETHER_TYPE | I40E_INSET_FLEX_PAYLOAD,
6564         };
6565
6566         if (pctype > I40E_FILTER_PCTYPE_L2_PAYLOAD)
6567                 return 0;
6568         if (filter == RTE_ETH_FILTER_HASH)
6569                 valid = valid_hash_inset_table[pctype];
6570         else
6571                 valid = valid_fdir_inset_table[pctype];
6572
6573         return valid;
6574 }
6575
6576 /**
6577  * Validate if the input set is allowed for a specific PCTYPE
6578  */
6579 static int
6580 i40e_validate_input_set(enum i40e_filter_pctype pctype,
6581                 enum rte_filter_type filter, uint64_t inset)
6582 {
6583         uint64_t valid;
6584
6585         valid = i40e_get_valid_input_set(pctype, filter);
6586         if (inset & (~valid))
6587                 return -EINVAL;
6588
6589         return 0;
6590 }
6591
6592 /* default input set fields combination per pctype */
6593 static uint64_t
6594 i40e_get_default_input_set(uint16_t pctype)
6595 {
6596         static const uint64_t default_inset_table[] = {
6597                 [I40E_FILTER_PCTYPE_FRAG_IPV4] =
6598                         I40E_INSET_IPV4_SRC | I40E_INSET_IPV4_DST,
6599                 [I40E_FILTER_PCTYPE_NONF_IPV4_UDP] =
6600                         I40E_INSET_IPV4_SRC | I40E_INSET_IPV4_DST |
6601                         I40E_INSET_SRC_PORT | I40E_INSET_DST_PORT,
6602                 [I40E_FILTER_PCTYPE_NONF_IPV4_TCP] =
6603                         I40E_INSET_IPV4_SRC | I40E_INSET_IPV4_DST |
6604                         I40E_INSET_SRC_PORT | I40E_INSET_DST_PORT,
6605                 [I40E_FILTER_PCTYPE_NONF_IPV4_SCTP] =
6606                         I40E_INSET_IPV4_SRC | I40E_INSET_IPV4_DST |
6607                         I40E_INSET_SRC_PORT | I40E_INSET_DST_PORT |
6608                         I40E_INSET_SCTP_VT,
6609                 [I40E_FILTER_PCTYPE_NONF_IPV4_OTHER] =
6610                         I40E_INSET_IPV4_SRC | I40E_INSET_IPV4_DST,
6611                 [I40E_FILTER_PCTYPE_FRAG_IPV6] =
6612                         I40E_INSET_IPV6_SRC | I40E_INSET_IPV6_DST,
6613                 [I40E_FILTER_PCTYPE_NONF_IPV6_UDP] =
6614                         I40E_INSET_IPV6_SRC | I40E_INSET_IPV6_DST |
6615                         I40E_INSET_SRC_PORT | I40E_INSET_DST_PORT,
6616                 [I40E_FILTER_PCTYPE_NONF_IPV6_TCP] =
6617                         I40E_INSET_IPV6_SRC | I40E_INSET_IPV6_DST |
6618                         I40E_INSET_SRC_PORT | I40E_INSET_DST_PORT,
6619                 [I40E_FILTER_PCTYPE_NONF_IPV6_SCTP] =
6620                         I40E_INSET_IPV6_SRC | I40E_INSET_IPV6_DST |
6621                         I40E_INSET_SRC_PORT | I40E_INSET_DST_PORT |
6622                         I40E_INSET_SCTP_VT,
6623                 [I40E_FILTER_PCTYPE_NONF_IPV6_OTHER] =
6624                         I40E_INSET_IPV6_SRC | I40E_INSET_IPV6_DST,
6625                 [I40E_FILTER_PCTYPE_L2_PAYLOAD] =
6626                         I40E_INSET_LAST_ETHER_TYPE,
6627         };
6628
6629         if (pctype > I40E_FILTER_PCTYPE_L2_PAYLOAD)
6630                 return 0;
6631
6632         return default_inset_table[pctype];
6633 }
6634
6635 /**
6636  * Parse the input set from index to logical bit masks
6637  */
6638 static int
6639 i40e_parse_input_set(uint64_t *inset,
6640                      enum i40e_filter_pctype pctype,
6641                      enum rte_eth_input_set_field *field,
6642                      uint16_t size)
6643 {
6644         uint16_t i, j;
6645         int ret = -EINVAL;
6646
6647         static const struct {
6648                 enum rte_eth_input_set_field field;
6649                 uint64_t inset;
6650         } inset_convert_table[] = {
6651                 {RTE_ETH_INPUT_SET_NONE, I40E_INSET_NONE},
6652                 {RTE_ETH_INPUT_SET_L2_SRC_MAC, I40E_INSET_SMAC},
6653                 {RTE_ETH_INPUT_SET_L2_DST_MAC, I40E_INSET_DMAC},
6654                 {RTE_ETH_INPUT_SET_L2_OUTER_VLAN, I40E_INSET_VLAN_OUTER},
6655                 {RTE_ETH_INPUT_SET_L2_INNER_VLAN, I40E_INSET_VLAN_INNER},
6656                 {RTE_ETH_INPUT_SET_L2_ETHERTYPE, I40E_INSET_LAST_ETHER_TYPE},
6657                 {RTE_ETH_INPUT_SET_L3_SRC_IP4, I40E_INSET_IPV4_SRC},
6658                 {RTE_ETH_INPUT_SET_L3_DST_IP4, I40E_INSET_IPV4_DST},
6659                 {RTE_ETH_INPUT_SET_L3_IP4_TOS, I40E_INSET_IPV4_TOS},
6660                 {RTE_ETH_INPUT_SET_L3_IP4_PROTO, I40E_INSET_IPV4_PROTO},
6661                 {RTE_ETH_INPUT_SET_L3_SRC_IP6, I40E_INSET_IPV6_SRC},
6662                 {RTE_ETH_INPUT_SET_L3_DST_IP6, I40E_INSET_IPV6_DST},
6663                 {RTE_ETH_INPUT_SET_L3_IP6_TC, I40E_INSET_IPV6_TC},
6664                 {RTE_ETH_INPUT_SET_L3_IP6_NEXT_HEADER,
6665                         I40E_INSET_IPV6_NEXT_HDR},
6666                 {RTE_ETH_INPUT_SET_L4_UDP_SRC_PORT, I40E_INSET_SRC_PORT},
6667                 {RTE_ETH_INPUT_SET_L4_TCP_SRC_PORT, I40E_INSET_SRC_PORT},
6668                 {RTE_ETH_INPUT_SET_L4_SCTP_SRC_PORT, I40E_INSET_SRC_PORT},
6669                 {RTE_ETH_INPUT_SET_L4_UDP_DST_PORT, I40E_INSET_DST_PORT},
6670                 {RTE_ETH_INPUT_SET_L4_TCP_DST_PORT, I40E_INSET_DST_PORT},
6671                 {RTE_ETH_INPUT_SET_L4_SCTP_DST_PORT, I40E_INSET_DST_PORT},
6672                 {RTE_ETH_INPUT_SET_L4_SCTP_VERIFICATION_TAG,
6673                         I40E_INSET_SCTP_VT},
6674                 {RTE_ETH_INPUT_SET_TUNNEL_L2_INNER_DST_MAC,
6675                         I40E_INSET_TUNNEL_DMAC},
6676                 {RTE_ETH_INPUT_SET_TUNNEL_L2_INNER_VLAN,
6677                         I40E_INSET_VLAN_TUNNEL},
6678                 {RTE_ETH_INPUT_SET_TUNNEL_L4_UDP_KEY,
6679                         I40E_INSET_TUNNEL_ID},
6680                 {RTE_ETH_INPUT_SET_TUNNEL_GRE_KEY, I40E_INSET_TUNNEL_ID},
6681                 {RTE_ETH_INPUT_SET_FLEX_PAYLOAD_1ST_WORD,
6682                         I40E_INSET_FLEX_PAYLOAD_W1},
6683                 {RTE_ETH_INPUT_SET_FLEX_PAYLOAD_2ND_WORD,
6684                         I40E_INSET_FLEX_PAYLOAD_W2},
6685                 {RTE_ETH_INPUT_SET_FLEX_PAYLOAD_3RD_WORD,
6686                         I40E_INSET_FLEX_PAYLOAD_W3},
6687                 {RTE_ETH_INPUT_SET_FLEX_PAYLOAD_4TH_WORD,
6688                         I40E_INSET_FLEX_PAYLOAD_W4},
6689                 {RTE_ETH_INPUT_SET_FLEX_PAYLOAD_5TH_WORD,
6690                         I40E_INSET_FLEX_PAYLOAD_W5},
6691                 {RTE_ETH_INPUT_SET_FLEX_PAYLOAD_6TH_WORD,
6692                         I40E_INSET_FLEX_PAYLOAD_W6},
6693                 {RTE_ETH_INPUT_SET_FLEX_PAYLOAD_7TH_WORD,
6694                         I40E_INSET_FLEX_PAYLOAD_W7},
6695                 {RTE_ETH_INPUT_SET_FLEX_PAYLOAD_8TH_WORD,
6696                         I40E_INSET_FLEX_PAYLOAD_W8},
6697         };
6698
6699         if (!inset || !field || size > RTE_ETH_INSET_SIZE_MAX)
6700                 return ret;
6701
6702         /* Only one item allowed for default or all */
6703         if (size == 1) {
6704                 if (field[0] == RTE_ETH_INPUT_SET_DEFAULT) {
6705                         *inset = i40e_get_default_input_set(pctype);
6706                         return 0;
6707                 } else if (field[0] == RTE_ETH_INPUT_SET_NONE) {
6708                         *inset = I40E_INSET_NONE;
6709                         return 0;
6710                 }
6711         }
6712
6713         for (i = 0, *inset = 0; i < size; i++) {
6714                 for (j = 0; j < RTE_DIM(inset_convert_table); j++) {
6715                         if (field[i] == inset_convert_table[j].field) {
6716                                 *inset |= inset_convert_table[j].inset;
6717                                 break;
6718                         }
6719                 }
6720
6721                 /* It contains unsupported input set, return immediately */
6722                 if (j == RTE_DIM(inset_convert_table))
6723                         return ret;
6724         }
6725
6726         return 0;
6727 }
6728
6729 /**
6730  * Translate the input set from bit masks to register aware bit masks
6731  * and vice versa
6732  */
6733 static uint64_t
6734 i40e_translate_input_set_reg(uint64_t input)
6735 {
6736         uint64_t val = 0;
6737         uint16_t i;
6738
6739         static const struct {
6740                 uint64_t inset;
6741                 uint64_t inset_reg;
6742         } inset_map[] = {
6743                 {I40E_INSET_DMAC, I40E_REG_INSET_L2_DMAC},
6744                 {I40E_INSET_SMAC, I40E_REG_INSET_L2_SMAC},
6745                 {I40E_INSET_VLAN_OUTER, I40E_REG_INSET_L2_OUTER_VLAN},
6746                 {I40E_INSET_VLAN_INNER, I40E_REG_INSET_L2_INNER_VLAN},
6747                 {I40E_INSET_LAST_ETHER_TYPE, I40E_REG_INSET_LAST_ETHER_TYPE},
6748                 {I40E_INSET_IPV4_SRC, I40E_REG_INSET_L3_SRC_IP4},
6749                 {I40E_INSET_IPV4_DST, I40E_REG_INSET_L3_DST_IP4},
6750                 {I40E_INSET_IPV4_TOS, I40E_REG_INSET_L3_IP4_TOS},
6751                 {I40E_INSET_IPV4_PROTO, I40E_REG_INSET_L3_IP4_PROTO},
6752                 {I40E_INSET_IPV6_SRC, I40E_REG_INSET_L3_SRC_IP6},
6753                 {I40E_INSET_IPV6_DST, I40E_REG_INSET_L3_DST_IP6},
6754                 {I40E_INSET_IPV6_TC, I40E_REG_INSET_L3_IP6_TC},
6755                 {I40E_INSET_IPV6_NEXT_HDR, I40E_REG_INSET_L3_IP6_NEXT_HDR},
6756                 {I40E_INSET_SRC_PORT, I40E_REG_INSET_L4_SRC_PORT},
6757                 {I40E_INSET_DST_PORT, I40E_REG_INSET_L4_DST_PORT},
6758                 {I40E_INSET_SCTP_VT, I40E_REG_INSET_L4_SCTP_VERIFICATION_TAG},
6759                 {I40E_INSET_TUNNEL_ID, I40E_REG_INSET_TUNNEL_ID},
6760                 {I40E_INSET_TUNNEL_DMAC,
6761                         I40E_REG_INSET_TUNNEL_L2_INNER_DST_MAC},
6762                 {I40E_INSET_TUNNEL_IPV4_DST, I40E_REG_INSET_TUNNEL_L3_DST_IP4},
6763                 {I40E_INSET_TUNNEL_IPV6_DST, I40E_REG_INSET_TUNNEL_L3_DST_IP6},
6764                 {I40E_INSET_TUNNEL_SRC_PORT,
6765                         I40E_REG_INSET_TUNNEL_L4_UDP_SRC_PORT},
6766                 {I40E_INSET_TUNNEL_DST_PORT,
6767                         I40E_REG_INSET_TUNNEL_L4_UDP_DST_PORT},
6768                 {I40E_INSET_TUNNEL_ID, I40E_REG_INSET_TUNNEL_ID},
6769                 {I40E_INSET_FLEX_PAYLOAD_W1, I40E_REG_INSET_FLEX_PAYLOAD_WORD1},
6770                 {I40E_INSET_FLEX_PAYLOAD_W2, I40E_REG_INSET_FLEX_PAYLOAD_WORD2},
6771                 {I40E_INSET_FLEX_PAYLOAD_W3, I40E_REG_INSET_FLEX_PAYLOAD_WORD3},
6772                 {I40E_INSET_FLEX_PAYLOAD_W4, I40E_REG_INSET_FLEX_PAYLOAD_WORD4},
6773                 {I40E_INSET_FLEX_PAYLOAD_W5, I40E_REG_INSET_FLEX_PAYLOAD_WORD5},
6774                 {I40E_INSET_FLEX_PAYLOAD_W6, I40E_REG_INSET_FLEX_PAYLOAD_WORD6},
6775                 {I40E_INSET_FLEX_PAYLOAD_W7, I40E_REG_INSET_FLEX_PAYLOAD_WORD7},
6776                 {I40E_INSET_FLEX_PAYLOAD_W8, I40E_REG_INSET_FLEX_PAYLOAD_WORD8},
6777         };
6778
6779         if (input == 0)
6780                 return val;
6781
6782         /* Translate input set to register aware inset */
6783         for (i = 0; i < RTE_DIM(inset_map); i++) {
6784                 if (input & inset_map[i].inset)
6785                         val |= inset_map[i].inset_reg;
6786         }
6787
6788         return val;
6789 }
6790
6791 static uint8_t
6792 i40e_generate_inset_mask_reg(uint64_t inset, uint32_t *mask, uint8_t nb_elem)
6793 {
6794         uint8_t i, idx = 0;
6795
6796         static const struct {
6797                 uint64_t inset;
6798                 uint32_t mask;
6799         } inset_mask_map[] = {
6800                 {I40E_INSET_IPV4_TOS, I40E_INSET_IPV4_TOS_MASK},
6801                 {I40E_INSET_IPV4_PROTO, I40E_INSET_IPV4_PROTO_MASK},
6802                 {I40E_INSET_IPV6_TC, I40E_INSET_IPV6_TC_MASK},
6803                 {I40E_INSET_IPV6_NEXT_HDR, I40E_INSET_IPV6_NEXT_HDR_MASK},
6804         };
6805
6806         if (!inset || !mask || !nb_elem)
6807                 return 0;
6808
6809         if (!inset && nb_elem >= I40E_INSET_MASK_NUM_REG) {
6810                 for (i = 0; i < I40E_INSET_MASK_NUM_REG; i++)
6811                         mask[i] = 0;
6812                 return I40E_INSET_MASK_NUM_REG;
6813         }
6814
6815         for (i = 0, idx = 0; i < RTE_DIM(inset_mask_map); i++) {
6816                 if (idx >= nb_elem)
6817                         break;
6818                 if (inset & inset_mask_map[i].inset) {
6819                         mask[idx] = inset_mask_map[i].mask;
6820                         idx++;
6821                 }
6822         }
6823
6824         return idx;
6825 }
6826
6827 static uint64_t
6828 i40e_get_reg_inset(struct i40e_hw *hw, enum rte_filter_type filter,
6829                             enum i40e_filter_pctype pctype)
6830 {
6831         uint64_t reg = 0;
6832
6833         if (filter == RTE_ETH_FILTER_HASH) {
6834                 reg = I40E_READ_REG(hw, I40E_GLQF_HASH_INSET(1, pctype));
6835                 reg <<= I40E_32_BIT_WIDTH;
6836                 reg |= I40E_READ_REG(hw, I40E_GLQF_HASH_INSET(0, pctype));
6837         } else if (filter == RTE_ETH_FILTER_FDIR) {
6838                 reg = I40E_READ_REG(hw, I40E_PRTQF_FD_INSET(pctype, 1));
6839                 reg <<= I40E_32_BIT_WIDTH;
6840                 reg |= I40E_READ_REG(hw, I40E_PRTQF_FD_INSET(pctype, 0));
6841         }
6842
6843         return reg;
6844 }
6845
6846 static void
6847 i40e_check_write_reg(struct i40e_hw *hw, uint32_t addr, uint32_t val)
6848 {
6849         uint32_t reg = I40E_READ_REG(hw, addr);
6850
6851         PMD_DRV_LOG(DEBUG, "[0x%08x] original: 0x%08x\n", addr, reg);
6852         if (reg != val)
6853                 I40E_WRITE_REG(hw, addr, val);
6854         PMD_DRV_LOG(DEBUG, "[0x%08x] after: 0x%08x\n", addr,
6855                     (uint32_t)I40E_READ_REG(hw, addr));
6856 }
6857
6858 static int
6859 i40e_set_hash_inset_mask(struct i40e_hw *hw,
6860                          enum i40e_filter_pctype pctype,
6861                          enum rte_filter_input_set_op op,
6862                          uint32_t *mask_reg,
6863                          uint8_t num)
6864 {
6865         uint32_t reg;
6866         uint8_t i;
6867
6868         if (!mask_reg || num > RTE_ETH_INPUT_SET_SELECT)
6869                 return -EINVAL;
6870
6871         if (op == RTE_ETH_INPUT_SET_SELECT) {
6872                 for (i = 0; i < I40E_INSET_MASK_NUM_REG; i++) {
6873                         i40e_check_write_reg(hw, I40E_GLQF_HASH_MSK(i, pctype),
6874                                              0);
6875                         if (i >= num)
6876                                 continue;
6877                         i40e_check_write_reg(hw, I40E_GLQF_HASH_MSK(i, pctype),
6878                                              mask_reg[i]);
6879                 }
6880         } else if (op == RTE_ETH_INPUT_SET_ADD) {
6881                 uint8_t j, count = 0;
6882
6883                 for (i = 0; i < I40E_INSET_MASK_NUM_REG; i++) {
6884                         reg = I40E_READ_REG(hw, I40E_GLQF_HASH_MSK(i, pctype));
6885                         if (reg & I40E_GLQF_HASH_MSK_FIELD)
6886                                 count++;
6887                 }
6888                 if (count + num > I40E_INSET_MASK_NUM_REG)
6889                         return -EINVAL;
6890
6891                 for (i = count, j = 0; i < I40E_INSET_MASK_NUM_REG; i++, j++)
6892                         i40e_check_write_reg(hw, I40E_GLQF_HASH_MSK(i, pctype),
6893                                              mask_reg[j]);
6894         }
6895
6896         return 0;
6897 }
6898
6899 static int
6900 i40e_set_fd_inset_mask(struct i40e_hw *hw,
6901                        enum i40e_filter_pctype pctype,
6902                        enum rte_filter_input_set_op op,
6903                        uint32_t *mask_reg,
6904                        uint8_t num)
6905 {
6906         uint32_t reg;
6907         uint8_t i;
6908
6909         if (!mask_reg || num > RTE_ETH_INPUT_SET_SELECT)
6910                 return -EINVAL;
6911
6912         if (op == RTE_ETH_INPUT_SET_SELECT) {
6913                 for (i = 0; i < I40E_INSET_MASK_NUM_REG; i++) {
6914                         i40e_check_write_reg(hw, I40E_GLQF_FD_MSK(i, pctype),
6915                                              0);
6916                         if (i >= num)
6917                                 continue;
6918                         i40e_check_write_reg(hw, I40E_GLQF_FD_MSK(i, pctype),
6919                                              mask_reg[i]);
6920                 }
6921         } else if (op == RTE_ETH_INPUT_SET_ADD) {
6922                 uint8_t j, count = 0;
6923
6924                 for (i = 0; i < I40E_INSET_MASK_NUM_REG; i++) {
6925                         reg = I40E_READ_REG(hw, I40E_GLQF_FD_MSK(i, pctype));
6926                         if (reg & I40E_GLQF_FD_MSK_FIELD)
6927                                 count++;
6928                 }
6929                 if (count + num > I40E_INSET_MASK_NUM_REG)
6930                         return -EINVAL;
6931
6932                 for (i = count, j = 0; i < I40E_INSET_MASK_NUM_REG; i++, j++)
6933                         i40e_check_write_reg(hw, I40E_GLQF_FD_MSK(i, pctype),
6934                                              mask_reg[j]);
6935         }
6936
6937         return 0;
6938 }
6939
6940 int
6941 i40e_filter_inset_select(struct i40e_hw *hw,
6942                          struct rte_eth_input_set_conf *conf,
6943                          enum rte_filter_type filter)
6944 {
6945         enum i40e_filter_pctype pctype;
6946         uint64_t inset_reg = 0, input_set;
6947         uint32_t mask_reg[I40E_INSET_MASK_NUM_REG];
6948         uint8_t num;
6949         int ret;
6950
6951         if (!hw || !conf) {
6952                 PMD_DRV_LOG(ERR, "Invalid pointer");
6953                 return -EFAULT;
6954         }
6955
6956         pctype = i40e_flowtype_to_pctype(conf->flow_type);
6957         if (pctype == 0 || pctype > I40E_FILTER_PCTYPE_L2_PAYLOAD) {
6958                 PMD_DRV_LOG(ERR, "Not supported flow type (%u)",
6959                             conf->flow_type);
6960                 return -EINVAL;
6961         }
6962         if (filter != RTE_ETH_FILTER_HASH && filter != RTE_ETH_FILTER_FDIR) {
6963                 PMD_DRV_LOG(ERR, "Not supported filter type (%u)", filter);
6964                 return -EINVAL;
6965         }
6966
6967         ret = i40e_parse_input_set(&input_set, pctype, conf->field,
6968                                    conf->inset_size);
6969         if (ret) {
6970                 PMD_DRV_LOG(ERR, "Failed to parse input set");
6971                 return -EINVAL;
6972         }
6973         if (i40e_validate_input_set(pctype, filter, input_set) != 0) {
6974                 PMD_DRV_LOG(ERR, "Invalid input set");
6975                 return -EINVAL;
6976         }
6977
6978         if (conf->op == RTE_ETH_INPUT_SET_ADD) {
6979                 inset_reg |= i40e_get_reg_inset(hw, filter, pctype);
6980         } else if (conf->op != RTE_ETH_INPUT_SET_SELECT) {
6981                 PMD_DRV_LOG(ERR, "Unsupported input set operation");
6982                 return -EINVAL;
6983         }
6984         num = i40e_generate_inset_mask_reg(input_set, mask_reg,
6985                                            I40E_INSET_MASK_NUM_REG);
6986         inset_reg |= i40e_translate_input_set_reg(input_set);
6987
6988         if (filter == RTE_ETH_FILTER_HASH) {
6989                 ret = i40e_set_hash_inset_mask(hw, pctype, conf->op, mask_reg,
6990                                                num);
6991                 if (ret)
6992                         return -EINVAL;
6993
6994                 i40e_check_write_reg(hw, I40E_GLQF_HASH_INSET(0, pctype),
6995                                       (uint32_t)(inset_reg & UINT32_MAX));
6996                 i40e_check_write_reg(hw, I40E_GLQF_HASH_INSET(1, pctype),
6997                                      (uint32_t)((inset_reg >>
6998                                      I40E_32_BIT_WIDTH) & UINT32_MAX));
6999         } else if (filter == RTE_ETH_FILTER_FDIR) {
7000                 ret = i40e_set_fd_inset_mask(hw, pctype, conf->op, mask_reg,
7001                                              num);
7002                 if (ret)
7003                         return -EINVAL;
7004
7005                 i40e_check_write_reg(hw, I40E_PRTQF_FD_INSET(pctype, 0),
7006                                       (uint32_t)(inset_reg & UINT32_MAX));
7007                 i40e_check_write_reg(hw, I40E_PRTQF_FD_INSET(pctype, 1),
7008                                      (uint32_t)((inset_reg >>
7009                                      I40E_32_BIT_WIDTH) & UINT32_MAX));
7010         } else {
7011                 PMD_DRV_LOG(ERR, "Not supported filter type (%u)", filter);
7012                 return -EINVAL;
7013         }
7014         I40E_WRITE_FLUSH(hw);
7015
7016         return 0;
7017 }
7018
7019 static int
7020 i40e_hash_filter_get(struct i40e_hw *hw, struct rte_eth_hash_filter_info *info)
7021 {
7022         int ret = 0;
7023
7024         if (!hw || !info) {
7025                 PMD_DRV_LOG(ERR, "Invalid pointer");
7026                 return -EFAULT;
7027         }
7028
7029         switch (info->info_type) {
7030         case RTE_ETH_HASH_FILTER_SYM_HASH_ENA_PER_PORT:
7031                 i40e_get_symmetric_hash_enable_per_port(hw,
7032                                         &(info->info.enable));
7033                 break;
7034         case RTE_ETH_HASH_FILTER_GLOBAL_CONFIG:
7035                 ret = i40e_get_hash_filter_global_config(hw,
7036                                 &(info->info.global_conf));
7037                 break;
7038         default:
7039                 PMD_DRV_LOG(ERR, "Hash filter info type (%d) not supported",
7040                                                         info->info_type);
7041                 ret = -EINVAL;
7042                 break;
7043         }
7044
7045         return ret;
7046 }
7047
7048 static int
7049 i40e_hash_filter_set(struct i40e_hw *hw, struct rte_eth_hash_filter_info *info)
7050 {
7051         int ret = 0;
7052
7053         if (!hw || !info) {
7054                 PMD_DRV_LOG(ERR, "Invalid pointer");
7055                 return -EFAULT;
7056         }
7057
7058         switch (info->info_type) {
7059         case RTE_ETH_HASH_FILTER_SYM_HASH_ENA_PER_PORT:
7060                 i40e_set_symmetric_hash_enable_per_port(hw, info->info.enable);
7061                 break;
7062         case RTE_ETH_HASH_FILTER_GLOBAL_CONFIG:
7063                 ret = i40e_set_hash_filter_global_config(hw,
7064                                 &(info->info.global_conf));
7065                 break;
7066         case RTE_ETH_HASH_FILTER_INPUT_SET_SELECT:
7067                 ret = i40e_filter_inset_select(hw,
7068                                                &(info->info.input_set_conf),
7069                                                RTE_ETH_FILTER_HASH);
7070                 break;
7071
7072         default:
7073                 PMD_DRV_LOG(ERR, "Hash filter info type (%d) not supported",
7074                                                         info->info_type);
7075                 ret = -EINVAL;
7076                 break;
7077         }
7078
7079         return ret;
7080 }
7081
7082 /* Operations for hash function */
7083 static int
7084 i40e_hash_filter_ctrl(struct rte_eth_dev *dev,
7085                       enum rte_filter_op filter_op,
7086                       void *arg)
7087 {
7088         struct i40e_hw *hw = I40E_DEV_PRIVATE_TO_HW(dev->data->dev_private);
7089         int ret = 0;
7090
7091         switch (filter_op) {
7092         case RTE_ETH_FILTER_NOP:
7093                 break;
7094         case RTE_ETH_FILTER_GET:
7095                 ret = i40e_hash_filter_get(hw,
7096                         (struct rte_eth_hash_filter_info *)arg);
7097                 break;
7098         case RTE_ETH_FILTER_SET:
7099                 ret = i40e_hash_filter_set(hw,
7100                         (struct rte_eth_hash_filter_info *)arg);
7101                 break;
7102         default:
7103                 PMD_DRV_LOG(WARNING, "Filter operation (%d) not supported",
7104                                                                 filter_op);
7105                 ret = -ENOTSUP;
7106                 break;
7107         }
7108
7109         return ret;
7110 }
7111
7112 /*
7113  * Configure ethertype filter, which can director packet by filtering
7114  * with mac address and ether_type or only ether_type
7115  */
7116 static int
7117 i40e_ethertype_filter_set(struct i40e_pf *pf,
7118                         struct rte_eth_ethertype_filter *filter,
7119                         bool add)
7120 {
7121         struct i40e_hw *hw = I40E_PF_TO_HW(pf);
7122         struct i40e_control_filter_stats stats;
7123         uint16_t flags = 0;
7124         int ret;
7125
7126         if (filter->queue >= pf->dev_data->nb_rx_queues) {
7127                 PMD_DRV_LOG(ERR, "Invalid queue ID");
7128                 return -EINVAL;
7129         }
7130         if (filter->ether_type == ETHER_TYPE_IPv4 ||
7131                 filter->ether_type == ETHER_TYPE_IPv6) {
7132                 PMD_DRV_LOG(ERR, "unsupported ether_type(0x%04x) in"
7133                         " control packet filter.", filter->ether_type);
7134                 return -EINVAL;
7135         }
7136         if (filter->ether_type == ETHER_TYPE_VLAN)
7137                 PMD_DRV_LOG(WARNING, "filter vlan ether_type in first tag is"
7138                         " not supported.");
7139
7140         if (!(filter->flags & RTE_ETHTYPE_FLAGS_MAC))
7141                 flags |= I40E_AQC_ADD_CONTROL_PACKET_FLAGS_IGNORE_MAC;
7142         if (filter->flags & RTE_ETHTYPE_FLAGS_DROP)
7143                 flags |= I40E_AQC_ADD_CONTROL_PACKET_FLAGS_DROP;
7144         flags |= I40E_AQC_ADD_CONTROL_PACKET_FLAGS_TO_QUEUE;
7145
7146         memset(&stats, 0, sizeof(stats));
7147         ret = i40e_aq_add_rem_control_packet_filter(hw,
7148                         filter->mac_addr.addr_bytes,
7149                         filter->ether_type, flags,
7150                         pf->main_vsi->seid,
7151                         filter->queue, add, &stats, NULL);
7152
7153         PMD_DRV_LOG(INFO, "add/rem control packet filter, return %d,"
7154                          " mac_etype_used = %u, etype_used = %u,"
7155                          " mac_etype_free = %u, etype_free = %u\n",
7156                          ret, stats.mac_etype_used, stats.etype_used,
7157                          stats.mac_etype_free, stats.etype_free);
7158         if (ret < 0)
7159                 return -ENOSYS;
7160         return 0;
7161 }
7162
7163 /*
7164  * Handle operations for ethertype filter.
7165  */
7166 static int
7167 i40e_ethertype_filter_handle(struct rte_eth_dev *dev,
7168                                 enum rte_filter_op filter_op,
7169                                 void *arg)
7170 {
7171         struct i40e_pf *pf = I40E_DEV_PRIVATE_TO_PF(dev->data->dev_private);
7172         int ret = 0;
7173
7174         if (filter_op == RTE_ETH_FILTER_NOP)
7175                 return ret;
7176
7177         if (arg == NULL) {
7178                 PMD_DRV_LOG(ERR, "arg shouldn't be NULL for operation %u",
7179                             filter_op);
7180                 return -EINVAL;
7181         }
7182
7183         switch (filter_op) {
7184         case RTE_ETH_FILTER_ADD:
7185                 ret = i40e_ethertype_filter_set(pf,
7186                         (struct rte_eth_ethertype_filter *)arg,
7187                         TRUE);
7188                 break;
7189         case RTE_ETH_FILTER_DELETE:
7190                 ret = i40e_ethertype_filter_set(pf,
7191                         (struct rte_eth_ethertype_filter *)arg,
7192                         FALSE);
7193                 break;
7194         default:
7195                 PMD_DRV_LOG(ERR, "unsupported operation %u\n", filter_op);
7196                 ret = -ENOSYS;
7197                 break;
7198         }
7199         return ret;
7200 }
7201
7202 static int
7203 i40e_dev_filter_ctrl(struct rte_eth_dev *dev,
7204                      enum rte_filter_type filter_type,
7205                      enum rte_filter_op filter_op,
7206                      void *arg)
7207 {
7208         int ret = 0;
7209
7210         if (dev == NULL)
7211                 return -EINVAL;
7212
7213         switch (filter_type) {
7214         case RTE_ETH_FILTER_NONE:
7215                 /* For global configuration */
7216                 ret = i40e_filter_ctrl_global_config(dev, filter_op, arg);
7217                 break;
7218         case RTE_ETH_FILTER_HASH:
7219                 ret = i40e_hash_filter_ctrl(dev, filter_op, arg);
7220                 break;
7221         case RTE_ETH_FILTER_MACVLAN:
7222                 ret = i40e_mac_filter_handle(dev, filter_op, arg);
7223                 break;
7224         case RTE_ETH_FILTER_ETHERTYPE:
7225                 ret = i40e_ethertype_filter_handle(dev, filter_op, arg);
7226                 break;
7227         case RTE_ETH_FILTER_TUNNEL:
7228                 ret = i40e_tunnel_filter_handle(dev, filter_op, arg);
7229                 break;
7230         case RTE_ETH_FILTER_FDIR:
7231                 ret = i40e_fdir_ctrl_func(dev, filter_op, arg);
7232                 break;
7233         default:
7234                 PMD_DRV_LOG(WARNING, "Filter type (%d) not supported",
7235                                                         filter_type);
7236                 ret = -EINVAL;
7237                 break;
7238         }
7239
7240         return ret;
7241 }
7242
7243 /*
7244  * As some registers wouldn't be reset unless a global hardware reset,
7245  * hardware initialization is needed to put those registers into an
7246  * expected initial state.
7247  */
7248 static void
7249 i40e_hw_init(struct i40e_hw *hw)
7250 {
7251         /* clear the PF Queue Filter control register */
7252         I40E_WRITE_REG(hw, I40E_PFQF_CTL_0, 0);
7253
7254         /* Disable symmetric hash per port */
7255         i40e_set_symmetric_hash_enable_per_port(hw, 0);
7256 }
7257
7258 enum i40e_filter_pctype
7259 i40e_flowtype_to_pctype(uint16_t flow_type)
7260 {
7261         static const enum i40e_filter_pctype pctype_table[] = {
7262                 [RTE_ETH_FLOW_FRAG_IPV4] = I40E_FILTER_PCTYPE_FRAG_IPV4,
7263                 [RTE_ETH_FLOW_NONFRAG_IPV4_UDP] =
7264                         I40E_FILTER_PCTYPE_NONF_IPV4_UDP,
7265                 [RTE_ETH_FLOW_NONFRAG_IPV4_TCP] =
7266                         I40E_FILTER_PCTYPE_NONF_IPV4_TCP,
7267                 [RTE_ETH_FLOW_NONFRAG_IPV4_SCTP] =
7268                         I40E_FILTER_PCTYPE_NONF_IPV4_SCTP,
7269                 [RTE_ETH_FLOW_NONFRAG_IPV4_OTHER] =
7270                         I40E_FILTER_PCTYPE_NONF_IPV4_OTHER,
7271                 [RTE_ETH_FLOW_FRAG_IPV6] = I40E_FILTER_PCTYPE_FRAG_IPV6,
7272                 [RTE_ETH_FLOW_NONFRAG_IPV6_UDP] =
7273                         I40E_FILTER_PCTYPE_NONF_IPV6_UDP,
7274                 [RTE_ETH_FLOW_NONFRAG_IPV6_TCP] =
7275                         I40E_FILTER_PCTYPE_NONF_IPV6_TCP,
7276                 [RTE_ETH_FLOW_NONFRAG_IPV6_SCTP] =
7277                         I40E_FILTER_PCTYPE_NONF_IPV6_SCTP,
7278                 [RTE_ETH_FLOW_NONFRAG_IPV6_OTHER] =
7279                         I40E_FILTER_PCTYPE_NONF_IPV6_OTHER,
7280                 [RTE_ETH_FLOW_L2_PAYLOAD] = I40E_FILTER_PCTYPE_L2_PAYLOAD,
7281         };
7282
7283         return pctype_table[flow_type];
7284 }
7285
7286 uint16_t
7287 i40e_pctype_to_flowtype(enum i40e_filter_pctype pctype)
7288 {
7289         static const uint16_t flowtype_table[] = {
7290                 [I40E_FILTER_PCTYPE_FRAG_IPV4] = RTE_ETH_FLOW_FRAG_IPV4,
7291                 [I40E_FILTER_PCTYPE_NONF_IPV4_UDP] =
7292                         RTE_ETH_FLOW_NONFRAG_IPV4_UDP,
7293                 [I40E_FILTER_PCTYPE_NONF_IPV4_TCP] =
7294                         RTE_ETH_FLOW_NONFRAG_IPV4_TCP,
7295                 [I40E_FILTER_PCTYPE_NONF_IPV4_SCTP] =
7296                         RTE_ETH_FLOW_NONFRAG_IPV4_SCTP,
7297                 [I40E_FILTER_PCTYPE_NONF_IPV4_OTHER] =
7298                         RTE_ETH_FLOW_NONFRAG_IPV4_OTHER,
7299                 [I40E_FILTER_PCTYPE_FRAG_IPV6] = RTE_ETH_FLOW_FRAG_IPV6,
7300                 [I40E_FILTER_PCTYPE_NONF_IPV6_UDP] =
7301                         RTE_ETH_FLOW_NONFRAG_IPV6_UDP,
7302                 [I40E_FILTER_PCTYPE_NONF_IPV6_TCP] =
7303                         RTE_ETH_FLOW_NONFRAG_IPV6_TCP,
7304                 [I40E_FILTER_PCTYPE_NONF_IPV6_SCTP] =
7305                         RTE_ETH_FLOW_NONFRAG_IPV6_SCTP,
7306                 [I40E_FILTER_PCTYPE_NONF_IPV6_OTHER] =
7307                         RTE_ETH_FLOW_NONFRAG_IPV6_OTHER,
7308                 [I40E_FILTER_PCTYPE_L2_PAYLOAD] = RTE_ETH_FLOW_L2_PAYLOAD,
7309         };
7310
7311         return flowtype_table[pctype];
7312 }
7313
7314 /*
7315  * On X710, performance number is far from the expectation on recent firmware
7316  * versions; on XL710, performance number is also far from the expectation on
7317  * recent firmware versions, if promiscuous mode is disabled, or promiscuous
7318  * mode is enabled and port MAC address is equal to the packet destination MAC
7319  * address. The fix for this issue may not be integrated in the following
7320  * firmware version. So the workaround in software driver is needed. It needs
7321  * to modify the initial values of 3 internal only registers for both X710 and
7322  * XL710. Note that the values for X710 or XL710 could be different, and the
7323  * workaround can be removed when it is fixed in firmware in the future.
7324  */
7325
7326 /* For both X710 and XL710 */
7327 #define I40E_GL_SWR_PRI_JOIN_MAP_0_VALUE 0x10000200
7328 #define I40E_GL_SWR_PRI_JOIN_MAP_0       0x26CE00
7329
7330 #define I40E_GL_SWR_PRI_JOIN_MAP_2_VALUE 0x011f0200
7331 #define I40E_GL_SWR_PRI_JOIN_MAP_2       0x26CE08
7332
7333 /* For X710 */
7334 #define I40E_GL_SWR_PM_UP_THR_EF_VALUE   0x03030303
7335 /* For XL710 */
7336 #define I40E_GL_SWR_PM_UP_THR_SF_VALUE   0x06060606
7337 #define I40E_GL_SWR_PM_UP_THR            0x269FBC
7338
7339 static void
7340 i40e_configure_registers(struct i40e_hw *hw)
7341 {
7342         static struct {
7343                 uint32_t addr;
7344                 uint64_t val;
7345         } reg_table[] = {
7346                 {I40E_GL_SWR_PRI_JOIN_MAP_0, I40E_GL_SWR_PRI_JOIN_MAP_0_VALUE},
7347                 {I40E_GL_SWR_PRI_JOIN_MAP_2, I40E_GL_SWR_PRI_JOIN_MAP_2_VALUE},
7348                 {I40E_GL_SWR_PM_UP_THR, 0}, /* Compute value dynamically */
7349         };
7350         uint64_t reg;
7351         uint32_t i;
7352         int ret;
7353
7354         for (i = 0; i < RTE_DIM(reg_table); i++) {
7355                 if (reg_table[i].addr == I40E_GL_SWR_PM_UP_THR) {
7356                         if (i40e_is_40G_device(hw->device_id)) /* For XL710 */
7357                                 reg_table[i].val =
7358                                         I40E_GL_SWR_PM_UP_THR_SF_VALUE;
7359                         else /* For X710 */
7360                                 reg_table[i].val =
7361                                         I40E_GL_SWR_PM_UP_THR_EF_VALUE;
7362                 }
7363
7364                 ret = i40e_aq_debug_read_register(hw, reg_table[i].addr,
7365                                                         &reg, NULL);
7366                 if (ret < 0) {
7367                         PMD_DRV_LOG(ERR, "Failed to read from 0x%"PRIx32,
7368                                                         reg_table[i].addr);
7369                         break;
7370                 }
7371                 PMD_DRV_LOG(DEBUG, "Read from 0x%"PRIx32": 0x%"PRIx64,
7372                                                 reg_table[i].addr, reg);
7373                 if (reg == reg_table[i].val)
7374                         continue;
7375
7376                 ret = i40e_aq_debug_write_register(hw, reg_table[i].addr,
7377                                                 reg_table[i].val, NULL);
7378                 if (ret < 0) {
7379                         PMD_DRV_LOG(ERR, "Failed to write 0x%"PRIx64" to the "
7380                                 "address of 0x%"PRIx32, reg_table[i].val,
7381                                                         reg_table[i].addr);
7382                         break;
7383                 }
7384                 PMD_DRV_LOG(DEBUG, "Write 0x%"PRIx64" to the address of "
7385                         "0x%"PRIx32, reg_table[i].val, reg_table[i].addr);
7386         }
7387 }
7388
7389 #define I40E_VSI_TSR(_i)            (0x00050800 + ((_i) * 4))
7390 #define I40E_VSI_TSR_QINQ_CONFIG    0xc030
7391 #define I40E_VSI_L2TAGSTXVALID(_i)  (0x00042800 + ((_i) * 4))
7392 #define I40E_VSI_L2TAGSTXVALID_QINQ 0xab
7393 static int
7394 i40e_config_qinq(struct i40e_hw *hw, struct i40e_vsi *vsi)
7395 {
7396         uint32_t reg;
7397         int ret;
7398
7399         if (vsi->vsi_id >= I40E_MAX_NUM_VSIS) {
7400                 PMD_DRV_LOG(ERR, "VSI ID exceeds the maximum");
7401                 return -EINVAL;
7402         }
7403
7404         /* Configure for double VLAN RX stripping */
7405         reg = I40E_READ_REG(hw, I40E_VSI_TSR(vsi->vsi_id));
7406         if ((reg & I40E_VSI_TSR_QINQ_CONFIG) != I40E_VSI_TSR_QINQ_CONFIG) {
7407                 reg |= I40E_VSI_TSR_QINQ_CONFIG;
7408                 ret = i40e_aq_debug_write_register(hw,
7409                                                    I40E_VSI_TSR(vsi->vsi_id),
7410                                                    reg, NULL);
7411                 if (ret < 0) {
7412                         PMD_DRV_LOG(ERR, "Failed to update VSI_TSR[%d]",
7413                                     vsi->vsi_id);
7414                         return I40E_ERR_CONFIG;
7415                 }
7416         }
7417
7418         /* Configure for double VLAN TX insertion */
7419         reg = I40E_READ_REG(hw, I40E_VSI_L2TAGSTXVALID(vsi->vsi_id));
7420         if ((reg & 0xff) != I40E_VSI_L2TAGSTXVALID_QINQ) {
7421                 reg = I40E_VSI_L2TAGSTXVALID_QINQ;
7422                 ret = i40e_aq_debug_write_register(hw,
7423                                                    I40E_VSI_L2TAGSTXVALID(
7424                                                    vsi->vsi_id), reg, NULL);
7425                 if (ret < 0) {
7426                         PMD_DRV_LOG(ERR, "Failed to update "
7427                                 "VSI_L2TAGSTXVALID[%d]", vsi->vsi_id);
7428                         return I40E_ERR_CONFIG;
7429                 }
7430         }
7431
7432         return 0;
7433 }
7434
7435 /**
7436  * i40e_aq_add_mirror_rule
7437  * @hw: pointer to the hardware structure
7438  * @seid: VEB seid to add mirror rule to
7439  * @dst_id: destination vsi seid
7440  * @entries: Buffer which contains the entities to be mirrored
7441  * @count: number of entities contained in the buffer
7442  * @rule_id:the rule_id of the rule to be added
7443  *
7444  * Add a mirror rule for a given veb.
7445  *
7446  **/
7447 static enum i40e_status_code
7448 i40e_aq_add_mirror_rule(struct i40e_hw *hw,
7449                         uint16_t seid, uint16_t dst_id,
7450                         uint16_t rule_type, uint16_t *entries,
7451                         uint16_t count, uint16_t *rule_id)
7452 {
7453         struct i40e_aq_desc desc;
7454         struct i40e_aqc_add_delete_mirror_rule cmd;
7455         struct i40e_aqc_add_delete_mirror_rule_completion *resp =
7456                 (struct i40e_aqc_add_delete_mirror_rule_completion *)
7457                 &desc.params.raw;
7458         uint16_t buff_len;
7459         enum i40e_status_code status;
7460
7461         i40e_fill_default_direct_cmd_desc(&desc,
7462                                           i40e_aqc_opc_add_mirror_rule);
7463         memset(&cmd, 0, sizeof(cmd));
7464
7465         buff_len = sizeof(uint16_t) * count;
7466         desc.datalen = rte_cpu_to_le_16(buff_len);
7467         if (buff_len > 0)
7468                 desc.flags |= rte_cpu_to_le_16(
7469                         (uint16_t)(I40E_AQ_FLAG_BUF | I40E_AQ_FLAG_RD));
7470         cmd.rule_type = rte_cpu_to_le_16(rule_type <<
7471                                 I40E_AQC_MIRROR_RULE_TYPE_SHIFT);
7472         cmd.num_entries = rte_cpu_to_le_16(count);
7473         cmd.seid = rte_cpu_to_le_16(seid);
7474         cmd.destination = rte_cpu_to_le_16(dst_id);
7475
7476         rte_memcpy(&desc.params.raw, &cmd, sizeof(cmd));
7477         status = i40e_asq_send_command(hw, &desc, entries, buff_len, NULL);
7478         PMD_DRV_LOG(INFO, "i40e_aq_add_mirror_rule, aq_status %d,"
7479                          "rule_id = %u"
7480                          " mirror_rules_used = %u, mirror_rules_free = %u,",
7481                          hw->aq.asq_last_status, resp->rule_id,
7482                          resp->mirror_rules_used, resp->mirror_rules_free);
7483         *rule_id = rte_le_to_cpu_16(resp->rule_id);
7484
7485         return status;
7486 }
7487
7488 /**
7489  * i40e_aq_del_mirror_rule
7490  * @hw: pointer to the hardware structure
7491  * @seid: VEB seid to add mirror rule to
7492  * @entries: Buffer which contains the entities to be mirrored
7493  * @count: number of entities contained in the buffer
7494  * @rule_id:the rule_id of the rule to be delete
7495  *
7496  * Delete a mirror rule for a given veb.
7497  *
7498  **/
7499 static enum i40e_status_code
7500 i40e_aq_del_mirror_rule(struct i40e_hw *hw,
7501                 uint16_t seid, uint16_t rule_type, uint16_t *entries,
7502                 uint16_t count, uint16_t rule_id)
7503 {
7504         struct i40e_aq_desc desc;
7505         struct i40e_aqc_add_delete_mirror_rule cmd;
7506         uint16_t buff_len = 0;
7507         enum i40e_status_code status;
7508         void *buff = NULL;
7509
7510         i40e_fill_default_direct_cmd_desc(&desc,
7511                                           i40e_aqc_opc_delete_mirror_rule);
7512         memset(&cmd, 0, sizeof(cmd));
7513         if (rule_type == I40E_AQC_MIRROR_RULE_TYPE_VLAN) {
7514                 desc.flags |= rte_cpu_to_le_16((uint16_t)(I40E_AQ_FLAG_BUF |
7515                                                           I40E_AQ_FLAG_RD));
7516                 cmd.num_entries = count;
7517                 buff_len = sizeof(uint16_t) * count;
7518                 desc.datalen = rte_cpu_to_le_16(buff_len);
7519                 buff = (void *)entries;
7520         } else
7521                 /* rule id is filled in destination field for deleting mirror rule */
7522                 cmd.destination = rte_cpu_to_le_16(rule_id);
7523
7524         cmd.rule_type = rte_cpu_to_le_16(rule_type <<
7525                                 I40E_AQC_MIRROR_RULE_TYPE_SHIFT);
7526         cmd.seid = rte_cpu_to_le_16(seid);
7527
7528         rte_memcpy(&desc.params.raw, &cmd, sizeof(cmd));
7529         status = i40e_asq_send_command(hw, &desc, buff, buff_len, NULL);
7530
7531         return status;
7532 }
7533
7534 /**
7535  * i40e_mirror_rule_set
7536  * @dev: pointer to the hardware structure
7537  * @mirror_conf: mirror rule info
7538  * @sw_id: mirror rule's sw_id
7539  * @on: enable/disable
7540  *
7541  * set a mirror rule.
7542  *
7543  **/
7544 static int
7545 i40e_mirror_rule_set(struct rte_eth_dev *dev,
7546                         struct rte_eth_mirror_conf *mirror_conf,
7547                         uint8_t sw_id, uint8_t on)
7548 {
7549         struct i40e_pf *pf = I40E_DEV_PRIVATE_TO_PF(dev->data->dev_private);
7550         struct i40e_hw *hw = I40E_DEV_PRIVATE_TO_HW(dev->data->dev_private);
7551         struct i40e_mirror_rule *it, *mirr_rule = NULL;
7552         struct i40e_mirror_rule *parent = NULL;
7553         uint16_t seid, dst_seid, rule_id;
7554         uint16_t i, j = 0;
7555         int ret;
7556
7557         PMD_DRV_LOG(DEBUG, "i40e_mirror_rule_set: sw_id = %d.", sw_id);
7558
7559         if (pf->main_vsi->veb == NULL || pf->vfs == NULL) {
7560                 PMD_DRV_LOG(ERR, "mirror rule can not be configured"
7561                         " without veb or vfs.");
7562                 return -ENOSYS;
7563         }
7564         if (pf->nb_mirror_rule > I40E_MAX_MIRROR_RULES) {
7565                 PMD_DRV_LOG(ERR, "mirror table is full.");
7566                 return -ENOSPC;
7567         }
7568         if (mirror_conf->dst_pool > pf->vf_num) {
7569                 PMD_DRV_LOG(ERR, "invalid destination pool %u.",
7570                                  mirror_conf->dst_pool);
7571                 return -EINVAL;
7572         }
7573
7574         seid = pf->main_vsi->veb->seid;
7575
7576         TAILQ_FOREACH(it, &pf->mirror_list, rules) {
7577                 if (sw_id <= it->index) {
7578                         mirr_rule = it;
7579                         break;
7580                 }
7581                 parent = it;
7582         }
7583         if (mirr_rule && sw_id == mirr_rule->index) {
7584                 if (on) {
7585                         PMD_DRV_LOG(ERR, "mirror rule exists.");
7586                         return -EEXIST;
7587                 } else {
7588                         ret = i40e_aq_del_mirror_rule(hw, seid,
7589                                         mirr_rule->rule_type,
7590                                         mirr_rule->entries,
7591                                         mirr_rule->num_entries, mirr_rule->id);
7592                         if (ret < 0) {
7593                                 PMD_DRV_LOG(ERR, "failed to remove mirror rule:"
7594                                                    " ret = %d, aq_err = %d.",
7595                                                    ret, hw->aq.asq_last_status);
7596                                 return -ENOSYS;
7597                         }
7598                         TAILQ_REMOVE(&pf->mirror_list, mirr_rule, rules);
7599                         rte_free(mirr_rule);
7600                         pf->nb_mirror_rule--;
7601                         return 0;
7602                 }
7603         } else if (!on) {
7604                 PMD_DRV_LOG(ERR, "mirror rule doesn't exist.");
7605                 return -ENOENT;
7606         }
7607
7608         mirr_rule = rte_zmalloc("i40e_mirror_rule",
7609                                 sizeof(struct i40e_mirror_rule) , 0);
7610         if (!mirr_rule) {
7611                 PMD_DRV_LOG(ERR, "failed to allocate memory");
7612                 return I40E_ERR_NO_MEMORY;
7613         }
7614         switch (mirror_conf->rule_type) {
7615         case ETH_MIRROR_VLAN:
7616                 for (i = 0, j = 0; i < ETH_MIRROR_MAX_VLANS; i++) {
7617                         if (mirror_conf->vlan.vlan_mask & (1ULL << i)) {
7618                                 mirr_rule->entries[j] =
7619                                         mirror_conf->vlan.vlan_id[i];
7620                                 j++;
7621                         }
7622                 }
7623                 if (j == 0) {
7624                         PMD_DRV_LOG(ERR, "vlan is not specified.");
7625                         rte_free(mirr_rule);
7626                         return -EINVAL;
7627                 }
7628                 mirr_rule->rule_type = I40E_AQC_MIRROR_RULE_TYPE_VLAN;
7629                 break;
7630         case ETH_MIRROR_VIRTUAL_POOL_UP:
7631         case ETH_MIRROR_VIRTUAL_POOL_DOWN:
7632                 /* check if the specified pool bit is out of range */
7633                 if (mirror_conf->pool_mask > (uint64_t)(1ULL << (pf->vf_num + 1))) {
7634                         PMD_DRV_LOG(ERR, "pool mask is out of range.");
7635                         rte_free(mirr_rule);
7636                         return -EINVAL;
7637                 }
7638                 for (i = 0, j = 0; i < pf->vf_num; i++) {
7639                         if (mirror_conf->pool_mask & (1ULL << i)) {
7640                                 mirr_rule->entries[j] = pf->vfs[i].vsi->seid;
7641                                 j++;
7642                         }
7643                 }
7644                 if (mirror_conf->pool_mask & (1ULL << pf->vf_num)) {
7645                         /* add pf vsi to entries */
7646                         mirr_rule->entries[j] = pf->main_vsi_seid;
7647                         j++;
7648                 }
7649                 if (j == 0) {
7650                         PMD_DRV_LOG(ERR, "pool is not specified.");
7651                         rte_free(mirr_rule);
7652                         return -EINVAL;
7653                 }
7654                 /* egress and ingress in aq commands means from switch but not port */
7655                 mirr_rule->rule_type =
7656                         (mirror_conf->rule_type == ETH_MIRROR_VIRTUAL_POOL_UP) ?
7657                         I40E_AQC_MIRROR_RULE_TYPE_VPORT_EGRESS :
7658                         I40E_AQC_MIRROR_RULE_TYPE_VPORT_INGRESS;
7659                 break;
7660         case ETH_MIRROR_UPLINK_PORT:
7661                 /* egress and ingress in aq commands means from switch but not port*/
7662                 mirr_rule->rule_type = I40E_AQC_MIRROR_RULE_TYPE_ALL_EGRESS;
7663                 break;
7664         case ETH_MIRROR_DOWNLINK_PORT:
7665                 mirr_rule->rule_type = I40E_AQC_MIRROR_RULE_TYPE_ALL_INGRESS;
7666                 break;
7667         default:
7668                 PMD_DRV_LOG(ERR, "unsupported mirror type %d.",
7669                         mirror_conf->rule_type);
7670                 rte_free(mirr_rule);
7671                 return -EINVAL;
7672         }
7673
7674         /* If the dst_pool is equal to vf_num, consider it as PF */
7675         if (mirror_conf->dst_pool == pf->vf_num)
7676                 dst_seid = pf->main_vsi_seid;
7677         else
7678                 dst_seid = pf->vfs[mirror_conf->dst_pool].vsi->seid;
7679
7680         ret = i40e_aq_add_mirror_rule(hw, seid, dst_seid,
7681                                       mirr_rule->rule_type, mirr_rule->entries,
7682                                       j, &rule_id);
7683         if (ret < 0) {
7684                 PMD_DRV_LOG(ERR, "failed to add mirror rule:"
7685                                    " ret = %d, aq_err = %d.",
7686                                    ret, hw->aq.asq_last_status);
7687                 rte_free(mirr_rule);
7688                 return -ENOSYS;
7689         }
7690
7691         mirr_rule->index = sw_id;
7692         mirr_rule->num_entries = j;
7693         mirr_rule->id = rule_id;
7694         mirr_rule->dst_vsi_seid = dst_seid;
7695
7696         if (parent)
7697                 TAILQ_INSERT_AFTER(&pf->mirror_list, parent, mirr_rule, rules);
7698         else
7699                 TAILQ_INSERT_HEAD(&pf->mirror_list, mirr_rule, rules);
7700
7701         pf->nb_mirror_rule++;
7702         return 0;
7703 }
7704
7705 /**
7706  * i40e_mirror_rule_reset
7707  * @dev: pointer to the device
7708  * @sw_id: mirror rule's sw_id
7709  *
7710  * reset a mirror rule.
7711  *
7712  **/
7713 static int
7714 i40e_mirror_rule_reset(struct rte_eth_dev *dev, uint8_t sw_id)
7715 {
7716         struct i40e_pf *pf = I40E_DEV_PRIVATE_TO_PF(dev->data->dev_private);
7717         struct i40e_hw *hw = I40E_DEV_PRIVATE_TO_HW(dev->data->dev_private);
7718         struct i40e_mirror_rule *it, *mirr_rule = NULL;
7719         uint16_t seid;
7720         int ret;
7721
7722         PMD_DRV_LOG(DEBUG, "i40e_mirror_rule_reset: sw_id = %d.", sw_id);
7723
7724         seid = pf->main_vsi->veb->seid;
7725
7726         TAILQ_FOREACH(it, &pf->mirror_list, rules) {
7727                 if (sw_id == it->index) {
7728                         mirr_rule = it;
7729                         break;
7730                 }
7731         }
7732         if (mirr_rule) {
7733                 ret = i40e_aq_del_mirror_rule(hw, seid,
7734                                 mirr_rule->rule_type,
7735                                 mirr_rule->entries,
7736                                 mirr_rule->num_entries, mirr_rule->id);
7737                 if (ret < 0) {
7738                         PMD_DRV_LOG(ERR, "failed to remove mirror rule:"
7739                                            " status = %d, aq_err = %d.",
7740                                            ret, hw->aq.asq_last_status);
7741                         return -ENOSYS;
7742                 }
7743                 TAILQ_REMOVE(&pf->mirror_list, mirr_rule, rules);
7744                 rte_free(mirr_rule);
7745                 pf->nb_mirror_rule--;
7746         } else {
7747                 PMD_DRV_LOG(ERR, "mirror rule doesn't exist.");
7748                 return -ENOENT;
7749         }
7750         return 0;
7751 }
7752
7753 static int
7754 i40e_timesync_enable(struct rte_eth_dev *dev)
7755 {
7756         struct i40e_hw *hw = I40E_DEV_PRIVATE_TO_HW(dev->data->dev_private);
7757         struct rte_eth_link *link = &dev->data->dev_link;
7758         uint32_t tsync_ctl_l;
7759         uint32_t tsync_ctl_h;
7760         uint32_t tsync_inc_l;
7761         uint32_t tsync_inc_h;
7762
7763         switch (link->link_speed) {
7764         case ETH_LINK_SPEED_40G:
7765                 tsync_inc_l = I40E_PTP_40GB_INCVAL & 0xFFFFFFFF;
7766                 tsync_inc_h = I40E_PTP_40GB_INCVAL >> 32;
7767                 break;
7768         case ETH_LINK_SPEED_10G:
7769                 tsync_inc_l = I40E_PTP_10GB_INCVAL & 0xFFFFFFFF;
7770                 tsync_inc_h = I40E_PTP_10GB_INCVAL >> 32;
7771                 break;
7772         case ETH_LINK_SPEED_1000:
7773                 tsync_inc_l = I40E_PTP_1GB_INCVAL & 0xFFFFFFFF;
7774                 tsync_inc_h = I40E_PTP_1GB_INCVAL >> 32;
7775                 break;
7776         default:
7777                 tsync_inc_l = 0x0;
7778                 tsync_inc_h = 0x0;
7779         }
7780
7781         /* Clear timesync registers. */
7782         I40E_READ_REG(hw, I40E_PRTTSYN_STAT_0);
7783         I40E_READ_REG(hw, I40E_PRTTSYN_TXTIME_H);
7784         I40E_READ_REG(hw, I40E_PRTTSYN_RXTIME_L(0));
7785         I40E_READ_REG(hw, I40E_PRTTSYN_RXTIME_L(1));
7786         I40E_READ_REG(hw, I40E_PRTTSYN_RXTIME_L(2));
7787         I40E_READ_REG(hw, I40E_PRTTSYN_RXTIME_L(3));
7788         I40E_READ_REG(hw, I40E_PRTTSYN_TXTIME_H);
7789
7790         /* Set the timesync increment value. */
7791         I40E_WRITE_REG(hw, I40E_PRTTSYN_INC_L, tsync_inc_l);
7792         I40E_WRITE_REG(hw, I40E_PRTTSYN_INC_H, tsync_inc_h);
7793
7794         /* Enable timestamping of PTP packets. */
7795         tsync_ctl_l = I40E_READ_REG(hw, I40E_PRTTSYN_CTL0);
7796         tsync_ctl_l |= I40E_PRTTSYN_TSYNENA;
7797
7798         tsync_ctl_h = I40E_READ_REG(hw, I40E_PRTTSYN_CTL1);
7799         tsync_ctl_h |= I40E_PRTTSYN_TSYNENA;
7800         tsync_ctl_h |= I40E_PRTTSYN_TSYNTYPE;
7801
7802         I40E_WRITE_REG(hw, I40E_PRTTSYN_CTL0, tsync_ctl_l);
7803         I40E_WRITE_REG(hw, I40E_PRTTSYN_CTL1, tsync_ctl_h);
7804
7805         return 0;
7806 }
7807
7808 static int
7809 i40e_timesync_disable(struct rte_eth_dev *dev)
7810 {
7811         struct i40e_hw *hw = I40E_DEV_PRIVATE_TO_HW(dev->data->dev_private);
7812         uint32_t tsync_ctl_l;
7813         uint32_t tsync_ctl_h;
7814
7815         /* Disable timestamping of transmitted PTP packets. */
7816         tsync_ctl_l = I40E_READ_REG(hw, I40E_PRTTSYN_CTL0);
7817         tsync_ctl_l &= ~I40E_PRTTSYN_TSYNENA;
7818
7819         tsync_ctl_h = I40E_READ_REG(hw, I40E_PRTTSYN_CTL1);
7820         tsync_ctl_h &= ~I40E_PRTTSYN_TSYNENA;
7821
7822         I40E_WRITE_REG(hw, I40E_PRTTSYN_CTL0, tsync_ctl_l);
7823         I40E_WRITE_REG(hw, I40E_PRTTSYN_CTL1, tsync_ctl_h);
7824
7825         /* Set the timesync increment value. */
7826         I40E_WRITE_REG(hw, I40E_PRTTSYN_INC_L, 0x0);
7827         I40E_WRITE_REG(hw, I40E_PRTTSYN_INC_H, 0x0);
7828
7829         return 0;
7830 }
7831
7832 static int
7833 i40e_timesync_read_rx_timestamp(struct rte_eth_dev *dev,
7834                                 struct timespec *timestamp, uint32_t flags)
7835 {
7836         struct i40e_hw *hw = I40E_DEV_PRIVATE_TO_HW(dev->data->dev_private);
7837         uint32_t sync_status;
7838         uint32_t rx_stmpl;
7839         uint32_t rx_stmph;
7840         uint32_t index = flags & 0x03;
7841
7842         sync_status = I40E_READ_REG(hw, I40E_PRTTSYN_STAT_1);
7843         if ((sync_status & (1 << index)) == 0)
7844                 return -EINVAL;
7845
7846         rx_stmpl = I40E_READ_REG(hw, I40E_PRTTSYN_RXTIME_L(index));
7847         rx_stmph = I40E_READ_REG(hw, I40E_PRTTSYN_RXTIME_H(index));
7848
7849         timestamp->tv_sec = (uint64_t)(((uint64_t)rx_stmph << 32) | rx_stmpl);
7850         timestamp->tv_nsec = 0;
7851
7852         return  0;
7853 }
7854
7855 static int
7856 i40e_timesync_read_tx_timestamp(struct rte_eth_dev *dev,
7857                                 struct timespec *timestamp)
7858 {
7859         struct i40e_hw *hw = I40E_DEV_PRIVATE_TO_HW(dev->data->dev_private);
7860         uint32_t sync_status;
7861         uint32_t tx_stmpl;
7862         uint32_t tx_stmph;
7863
7864         sync_status = I40E_READ_REG(hw, I40E_PRTTSYN_STAT_0);
7865         if ((sync_status & I40E_PRTTSYN_STAT_0_TXTIME_MASK) == 0)
7866                 return -EINVAL;
7867
7868         tx_stmpl = I40E_READ_REG(hw, I40E_PRTTSYN_TXTIME_L);
7869         tx_stmph = I40E_READ_REG(hw, I40E_PRTTSYN_TXTIME_H);
7870
7871         timestamp->tv_sec = (uint64_t)(((uint64_t)tx_stmph << 32) | tx_stmpl);
7872         timestamp->tv_nsec = 0;
7873
7874         return  0;
7875 }
7876
7877 /*
7878  * i40e_parse_dcb_configure - parse dcb configure from user
7879  * @dev: the device being configured
7880  * @dcb_cfg: pointer of the result of parse
7881  * @*tc_map: bit map of enabled traffic classes
7882  *
7883  * Returns 0 on success, negative value on failure
7884  */
7885 static int
7886 i40e_parse_dcb_configure(struct rte_eth_dev *dev,
7887                          struct i40e_dcbx_config *dcb_cfg,
7888                          uint8_t *tc_map)
7889 {
7890         struct rte_eth_dcb_rx_conf *dcb_rx_conf;
7891         uint8_t i, tc_bw, bw_lf;
7892
7893         memset(dcb_cfg, 0, sizeof(struct i40e_dcbx_config));
7894
7895         dcb_rx_conf = &dev->data->dev_conf.rx_adv_conf.dcb_rx_conf;
7896         if (dcb_rx_conf->nb_tcs > I40E_MAX_TRAFFIC_CLASS) {
7897                 PMD_INIT_LOG(ERR, "number of tc exceeds max.");
7898                 return -EINVAL;
7899         }
7900
7901         /* assume each tc has the same bw */
7902         tc_bw = I40E_MAX_PERCENT / dcb_rx_conf->nb_tcs;
7903         for (i = 0; i < dcb_rx_conf->nb_tcs; i++)
7904                 dcb_cfg->etscfg.tcbwtable[i] = tc_bw;
7905         /* to ensure the sum of tcbw is equal to 100 */
7906         bw_lf = I40E_MAX_PERCENT % dcb_rx_conf->nb_tcs;
7907         for (i = 0; i < bw_lf; i++)
7908                 dcb_cfg->etscfg.tcbwtable[i]++;
7909
7910         /* assume each tc has the same Transmission Selection Algorithm */
7911         for (i = 0; i < dcb_rx_conf->nb_tcs; i++)
7912                 dcb_cfg->etscfg.tsatable[i] = I40E_IEEE_TSA_ETS;
7913
7914         for (i = 0; i < I40E_MAX_USER_PRIORITY; i++)
7915                 dcb_cfg->etscfg.prioritytable[i] =
7916                                 dcb_rx_conf->dcb_tc[i];
7917
7918         /* FW needs one App to configure HW */
7919         dcb_cfg->numapps = I40E_DEFAULT_DCB_APP_NUM;
7920         dcb_cfg->app[0].selector = I40E_APP_SEL_ETHTYPE;
7921         dcb_cfg->app[0].priority = I40E_DEFAULT_DCB_APP_PRIO;
7922         dcb_cfg->app[0].protocolid = I40E_APP_PROTOID_FCOE;
7923
7924         if (dcb_rx_conf->nb_tcs == 0)
7925                 *tc_map = 1; /* tc0 only */
7926         else
7927                 *tc_map = RTE_LEN2MASK(dcb_rx_conf->nb_tcs, uint8_t);
7928
7929         if (dev->data->dev_conf.dcb_capability_en & ETH_DCB_PFC_SUPPORT) {
7930                 dcb_cfg->pfc.willing = 0;
7931                 dcb_cfg->pfc.pfccap = I40E_MAX_TRAFFIC_CLASS;
7932                 dcb_cfg->pfc.pfcenable = *tc_map;
7933         }
7934         return 0;
7935 }
7936
7937 /*
7938  * i40e_vsi_get_bw_info - Query VSI BW Information
7939  * @vsi: the VSI being queried
7940  *
7941  * Returns 0 on success, negative value on failure
7942  */
7943 static enum i40e_status_code
7944 i40e_vsi_get_bw_info(struct i40e_vsi *vsi)
7945 {
7946         struct i40e_aqc_query_vsi_ets_sla_config_resp bw_ets_config = {0};
7947         struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
7948         struct i40e_hw *hw = I40E_VSI_TO_HW(vsi);
7949         enum i40e_status_code ret;
7950         int i;
7951         uint32_t tc_bw_max;
7952
7953         /* Get the VSI level BW configuration */
7954         ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid, &bw_config, NULL);
7955         if (ret) {
7956                 PMD_INIT_LOG(ERR,
7957                          "couldn't get PF vsi bw config, err %s aq_err %s\n",
7958                          i40e_stat_str(hw, ret),
7959                          i40e_aq_str(hw, hw->aq.asq_last_status));
7960                 return ret;
7961         }
7962
7963         /* Get the VSI level BW configuration per TC */
7964         ret = i40e_aq_query_vsi_ets_sla_config(hw, vsi->seid, &bw_ets_config,
7965                                                   NULL);
7966         if (ret) {
7967                 PMD_INIT_LOG(ERR,
7968                          "couldn't get PF vsi ets bw config, err %s aq_err %s\n",
7969                          i40e_stat_str(hw, ret),
7970                          i40e_aq_str(hw, hw->aq.asq_last_status));
7971                 return ret;
7972         }
7973
7974         if (bw_config.tc_valid_bits != bw_ets_config.tc_valid_bits) {
7975                 PMD_INIT_LOG(WARNING,
7976                          "Enabled TCs mismatch from querying VSI BW info"
7977                          " 0x%08x 0x%08x\n", bw_config.tc_valid_bits,
7978                          bw_ets_config.tc_valid_bits);
7979                 /* Still continuing */
7980         }
7981
7982         vsi->bw_info.bw_limit = rte_le_to_cpu_16(bw_config.port_bw_limit);
7983         vsi->bw_info.bw_max_quanta = bw_config.max_bw;
7984         tc_bw_max = rte_le_to_cpu_16(bw_ets_config.tc_bw_max[0]) |
7985                     (rte_le_to_cpu_16(bw_ets_config.tc_bw_max[1]) << 16);
7986         for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
7987                 vsi->bw_info.bw_ets_share_credits[i] =
7988                                 bw_ets_config.share_credits[i];
7989                 vsi->bw_info.bw_ets_limit_credits[i] =
7990                                 rte_le_to_cpu_16(bw_ets_config.credits[i]);
7991                 /* 3 bits out of 4 for each TC */
7992                 vsi->bw_info.bw_ets_max_quanta[i] =
7993                         (uint8_t)((tc_bw_max >> (i * 4)) & 0x7);
7994                 PMD_INIT_LOG(DEBUG,
7995                          "%s: vsi seid = %d, TC = %d, qset = 0x%x\n",
7996                          __func__, vsi->seid, i, bw_config.qs_handles[i]);
7997         }
7998
7999         return ret;
8000 }
8001
8002 static enum i40e_status_code
8003 i40e_vsi_update_queue_mapping(struct i40e_vsi *vsi,
8004                               struct i40e_aqc_vsi_properties_data *info,
8005                               uint8_t enabled_tcmap)
8006 {
8007         enum i40e_status_code ret;
8008         int i, total_tc = 0;
8009         uint16_t qpnum_per_tc, bsf, qp_idx;
8010         struct rte_eth_dev_data *dev_data = I40E_VSI_TO_DEV_DATA(vsi);
8011
8012         ret = validate_tcmap_parameter(vsi, enabled_tcmap);
8013         if (ret != I40E_SUCCESS)
8014                 return ret;
8015
8016         for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
8017                 if (enabled_tcmap & (1 << i))
8018                         total_tc++;
8019         }
8020         if (total_tc == 0)
8021                 total_tc = 1;
8022         vsi->enabled_tc = enabled_tcmap;
8023
8024         qpnum_per_tc = dev_data->nb_rx_queues / total_tc;
8025         /* Number of queues per enabled TC */
8026         if (qpnum_per_tc == 0) {
8027                 PMD_INIT_LOG(ERR, " number of queues is less that tcs.");
8028                 return I40E_ERR_INVALID_QP_ID;
8029         }
8030         qpnum_per_tc = RTE_MIN(i40e_align_floor(qpnum_per_tc),
8031                                 I40E_MAX_Q_PER_TC);
8032         bsf = rte_bsf32(qpnum_per_tc);
8033
8034         /**
8035          * Configure TC and queue mapping parameters, for enabled TC,
8036          * allocate qpnum_per_tc queues to this traffic. For disabled TC,
8037          * default queue will serve it.
8038          */
8039         qp_idx = 0;
8040         for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
8041                 if (vsi->enabled_tc & (1 << i)) {
8042                         info->tc_mapping[i] = rte_cpu_to_le_16((qp_idx <<
8043                                         I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
8044                                 (bsf << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT));
8045                         qp_idx += qpnum_per_tc;
8046                 } else
8047                         info->tc_mapping[i] = 0;
8048         }
8049
8050         /* Associate queue number with VSI, Keep vsi->nb_qps unchanged */
8051         if (vsi->type == I40E_VSI_SRIOV) {
8052                 info->mapping_flags |=
8053                         rte_cpu_to_le_16(I40E_AQ_VSI_QUE_MAP_NONCONTIG);
8054                 for (i = 0; i < vsi->nb_qps; i++)
8055                         info->queue_mapping[i] =
8056                                 rte_cpu_to_le_16(vsi->base_queue + i);
8057         } else {
8058                 info->mapping_flags |=
8059                         rte_cpu_to_le_16(I40E_AQ_VSI_QUE_MAP_CONTIG);
8060                 info->queue_mapping[0] = rte_cpu_to_le_16(vsi->base_queue);
8061         }
8062         info->valid_sections |=
8063                 rte_cpu_to_le_16(I40E_AQ_VSI_PROP_QUEUE_MAP_VALID);
8064
8065         return I40E_SUCCESS;
8066 }
8067
8068 /*
8069  * i40e_vsi_config_tc - Configure VSI tc setting for given TC map
8070  * @vsi: VSI to be configured
8071  * @tc_map: enabled TC bitmap
8072  *
8073  * Returns 0 on success, negative value on failure
8074  */
8075 static enum i40e_status_code
8076 i40e_vsi_config_tc(struct i40e_vsi *vsi, u8 tc_map)
8077 {
8078         struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
8079         struct i40e_vsi_context ctxt;
8080         struct i40e_hw *hw = I40E_VSI_TO_HW(vsi);
8081         enum i40e_status_code ret = I40E_SUCCESS;
8082         int i;
8083
8084         /* Check if enabled_tc is same as existing or new TCs */
8085         if (vsi->enabled_tc == tc_map)
8086                 return ret;
8087
8088         /* configure tc bandwidth */
8089         memset(&bw_data, 0, sizeof(bw_data));
8090         bw_data.tc_valid_bits = tc_map;
8091         /* Enable ETS TCs with equal BW Share for now across all VSIs */
8092         for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
8093                 if (tc_map & BIT_ULL(i))
8094                         bw_data.tc_bw_credits[i] = 1;
8095         }
8096         ret = i40e_aq_config_vsi_tc_bw(hw, vsi->seid, &bw_data, NULL);
8097         if (ret) {
8098                 PMD_INIT_LOG(ERR, "AQ command Config VSI BW allocation"
8099                         " per TC failed = %d",
8100                         hw->aq.asq_last_status);
8101                 goto out;
8102         }
8103         for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
8104                 vsi->info.qs_handle[i] = bw_data.qs_handles[i];
8105
8106         /* Update Queue Pairs Mapping for currently enabled UPs */
8107         ctxt.seid = vsi->seid;
8108         ctxt.pf_num = hw->pf_id;
8109         ctxt.vf_num = 0;
8110         ctxt.uplink_seid = vsi->uplink_seid;
8111         ctxt.info = vsi->info;
8112         i40e_get_cap(hw);
8113         ret = i40e_vsi_update_queue_mapping(vsi, &ctxt.info, tc_map);
8114         if (ret)
8115                 goto out;
8116
8117         /* Update the VSI after updating the VSI queue-mapping information */
8118         ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
8119         if (ret) {
8120                 PMD_INIT_LOG(ERR, "Failed to configure "
8121                             "TC queue mapping = %d",
8122                             hw->aq.asq_last_status);
8123                 goto out;
8124         }
8125         /* update the local VSI info with updated queue map */
8126         (void)rte_memcpy(&vsi->info.tc_mapping, &ctxt.info.tc_mapping,
8127                                         sizeof(vsi->info.tc_mapping));
8128         (void)rte_memcpy(&vsi->info.queue_mapping,
8129                         &ctxt.info.queue_mapping,
8130                 sizeof(vsi->info.queue_mapping));
8131         vsi->info.mapping_flags = ctxt.info.mapping_flags;
8132         vsi->info.valid_sections = 0;
8133
8134         /* Update current VSI BW information */
8135         ret = i40e_vsi_get_bw_info(vsi);
8136         if (ret) {
8137                 PMD_INIT_LOG(ERR,
8138                          "Failed updating vsi bw info, err %s aq_err %s",
8139                          i40e_stat_str(hw, ret),
8140                          i40e_aq_str(hw, hw->aq.asq_last_status));
8141                 goto out;
8142         }
8143
8144         vsi->enabled_tc = tc_map;
8145
8146 out:
8147         return ret;
8148 }
8149
8150 /*
8151  * i40e_dcb_hw_configure - program the dcb setting to hw
8152  * @pf: pf the configuration is taken on
8153  * @new_cfg: new configuration
8154  * @tc_map: enabled TC bitmap
8155  *
8156  * Returns 0 on success, negative value on failure
8157  */
8158 static enum i40e_status_code
8159 i40e_dcb_hw_configure(struct i40e_pf *pf,
8160                       struct i40e_dcbx_config *new_cfg,
8161                       uint8_t tc_map)
8162 {
8163         struct i40e_hw *hw = I40E_PF_TO_HW(pf);
8164         struct i40e_dcbx_config *old_cfg = &hw->local_dcbx_config;
8165         struct i40e_vsi *main_vsi = pf->main_vsi;
8166         struct i40e_vsi_list *vsi_list;
8167         enum i40e_status_code ret;
8168         int i;
8169         uint32_t val;
8170
8171         /* Use the FW API if FW > v4.4*/
8172         if (!((hw->aq.fw_maj_ver == 4) && (hw->aq.fw_min_ver >= 4))) {
8173                 PMD_INIT_LOG(ERR, "FW < v4.4, can not use FW LLDP API"
8174                                   " to configure DCB");
8175                 return I40E_ERR_FIRMWARE_API_VERSION;
8176         }
8177
8178         /* Check if need reconfiguration */
8179         if (!memcmp(new_cfg, old_cfg, sizeof(struct i40e_dcbx_config))) {
8180                 PMD_INIT_LOG(ERR, "No Change in DCB Config required.");
8181                 return I40E_SUCCESS;
8182         }
8183
8184         /* Copy the new config to the current config */
8185         *old_cfg = *new_cfg;
8186         old_cfg->etsrec = old_cfg->etscfg;
8187         ret = i40e_set_dcb_config(hw);
8188         if (ret) {
8189                 PMD_INIT_LOG(ERR,
8190                          "Set DCB Config failed, err %s aq_err %s\n",
8191                          i40e_stat_str(hw, ret),
8192                          i40e_aq_str(hw, hw->aq.asq_last_status));
8193                 return ret;
8194         }
8195         /* set receive Arbiter to RR mode and ETS scheme by default */
8196         for (i = 0; i <= I40E_PRTDCB_RETSTCC_MAX_INDEX; i++) {
8197                 val = I40E_READ_REG(hw, I40E_PRTDCB_RETSTCC(i));
8198                 val &= ~(I40E_PRTDCB_RETSTCC_BWSHARE_MASK     |
8199                          I40E_PRTDCB_RETSTCC_UPINTC_MODE_MASK |
8200                          I40E_PRTDCB_RETSTCC_ETSTC_SHIFT);
8201                 val |= ((uint32_t)old_cfg->etscfg.tcbwtable[i] <<
8202                         I40E_PRTDCB_RETSTCC_BWSHARE_SHIFT) &
8203                          I40E_PRTDCB_RETSTCC_BWSHARE_MASK;
8204                 val |= ((uint32_t)1 << I40E_PRTDCB_RETSTCC_UPINTC_MODE_SHIFT) &
8205                          I40E_PRTDCB_RETSTCC_UPINTC_MODE_MASK;
8206                 val |= ((uint32_t)1 << I40E_PRTDCB_RETSTCC_ETSTC_SHIFT) &
8207                          I40E_PRTDCB_RETSTCC_ETSTC_MASK;
8208                 I40E_WRITE_REG(hw, I40E_PRTDCB_RETSTCC(i), val);
8209         }
8210         /* get local mib to check whether it is configured correctly */
8211         /* IEEE mode */
8212         hw->local_dcbx_config.dcbx_mode = I40E_DCBX_MODE_IEEE;
8213         /* Get Local DCB Config */
8214         i40e_aq_get_dcb_config(hw, I40E_AQ_LLDP_MIB_LOCAL, 0,
8215                                      &hw->local_dcbx_config);
8216
8217         /* Update each VSI */
8218         i40e_vsi_config_tc(main_vsi, tc_map);
8219         if (main_vsi->veb) {
8220                 TAILQ_FOREACH(vsi_list, &main_vsi->veb->head, list) {
8221                         /* Beside main VSI, only enable default
8222                          * TC for other VSIs
8223                          */
8224                         ret = i40e_vsi_config_tc(vsi_list->vsi,
8225                                                 I40E_DEFAULT_TCMAP);
8226                         if (ret)
8227                                 PMD_INIT_LOG(WARNING,
8228                                          "Failed configuring TC for VSI seid=%d\n",
8229                                          vsi_list->vsi->seid);
8230                         /* continue */
8231                 }
8232         }
8233         return I40E_SUCCESS;
8234 }
8235
8236 /*
8237  * i40e_dcb_init_configure - initial dcb config
8238  * @dev: device being configured
8239  * @sw_dcb: indicate whether dcb is sw configured or hw offload
8240  *
8241  * Returns 0 on success, negative value on failure
8242  */
8243 static int
8244 i40e_dcb_init_configure(struct rte_eth_dev *dev, bool sw_dcb)
8245 {
8246         struct i40e_pf *pf = I40E_DEV_PRIVATE_TO_PF(dev->data->dev_private);
8247         struct i40e_hw *hw = I40E_DEV_PRIVATE_TO_HW(dev->data->dev_private);
8248         int ret = 0;
8249
8250         if ((pf->flags & I40E_FLAG_DCB) == 0) {
8251                 PMD_INIT_LOG(ERR, "HW doesn't support DCB");
8252                 return -ENOTSUP;
8253         }
8254
8255         /* DCB initialization:
8256          * Update DCB configuration from the Firmware and configure
8257          * LLDP MIB change event.
8258          */
8259         if (sw_dcb == TRUE) {
8260                 ret = i40e_aq_stop_lldp(hw, TRUE, NULL);
8261                 if (ret != I40E_SUCCESS)
8262                         PMD_INIT_LOG(DEBUG, "Failed to stop lldp");
8263
8264                 ret = i40e_init_dcb(hw);
8265                 /* if sw_dcb, lldp agent is stopped, the return from
8266                  * i40e_init_dcb we expect is failure with I40E_AQ_RC_EPERM
8267                  * adminq status.
8268                  */
8269                 if (ret != I40E_SUCCESS &&
8270                     hw->aq.asq_last_status == I40E_AQ_RC_EPERM) {
8271                         memset(&hw->local_dcbx_config, 0,
8272                                 sizeof(struct i40e_dcbx_config));
8273                         /* set dcb default configuration */
8274                         hw->local_dcbx_config.etscfg.willing = 0;
8275                         hw->local_dcbx_config.etscfg.maxtcs = 0;
8276                         hw->local_dcbx_config.etscfg.tcbwtable[0] = 100;
8277                         hw->local_dcbx_config.etscfg.tsatable[0] =
8278                                                 I40E_IEEE_TSA_ETS;
8279                         hw->local_dcbx_config.etsrec =
8280                                 hw->local_dcbx_config.etscfg;
8281                         hw->local_dcbx_config.pfc.willing = 0;
8282                         hw->local_dcbx_config.pfc.pfccap =
8283                                                 I40E_MAX_TRAFFIC_CLASS;
8284                         /* FW needs one App to configure HW */
8285                         hw->local_dcbx_config.numapps = 1;
8286                         hw->local_dcbx_config.app[0].selector =
8287                                                 I40E_APP_SEL_ETHTYPE;
8288                         hw->local_dcbx_config.app[0].priority = 3;
8289                         hw->local_dcbx_config.app[0].protocolid =
8290                                                 I40E_APP_PROTOID_FCOE;
8291                         ret = i40e_set_dcb_config(hw);
8292                         if (ret) {
8293                                 PMD_INIT_LOG(ERR, "default dcb config fails."
8294                                         " err = %d, aq_err = %d.", ret,
8295                                           hw->aq.asq_last_status);
8296                                 return -ENOSYS;
8297                         }
8298                 } else {
8299                         PMD_INIT_LOG(ERR, "DCBX configuration failed, err = %d,"
8300                                           " aq_err = %d.", ret,
8301                                           hw->aq.asq_last_status);
8302                         return -ENOTSUP;
8303                 }
8304         } else {
8305                 ret = i40e_aq_start_lldp(hw, NULL);
8306                 if (ret != I40E_SUCCESS)
8307                         PMD_INIT_LOG(DEBUG, "Failed to start lldp");
8308
8309                 ret = i40e_init_dcb(hw);
8310                 if (!ret) {
8311                         if (hw->dcbx_status == I40E_DCBX_STATUS_DISABLED) {
8312                                 PMD_INIT_LOG(ERR, "HW doesn't support"
8313                                                   " DCBX offload.");
8314                                 return -ENOTSUP;
8315                         }
8316                 } else {
8317                         PMD_INIT_LOG(ERR, "DCBX configuration failed, err = %d,"
8318                                           " aq_err = %d.", ret,
8319                                           hw->aq.asq_last_status);
8320                         return -ENOTSUP;
8321                 }
8322         }
8323         return 0;
8324 }
8325
8326 /*
8327  * i40e_dcb_setup - setup dcb related config
8328  * @dev: device being configured
8329  *
8330  * Returns 0 on success, negative value on failure
8331  */
8332 static int
8333 i40e_dcb_setup(struct rte_eth_dev *dev)
8334 {
8335         struct i40e_pf *pf = I40E_DEV_PRIVATE_TO_PF(dev->data->dev_private);
8336         struct i40e_dcbx_config dcb_cfg;
8337         uint8_t tc_map = 0;
8338         int ret = 0;
8339
8340         if ((pf->flags & I40E_FLAG_DCB) == 0) {
8341                 PMD_INIT_LOG(ERR, "HW doesn't support DCB");
8342                 return -ENOTSUP;
8343         }
8344
8345         if (pf->vf_num != 0 ||
8346             (dev->data->dev_conf.rxmode.mq_mode & ETH_MQ_RX_VMDQ_FLAG))
8347                 PMD_INIT_LOG(DEBUG, " DCB only works on main vsi.");
8348
8349         ret = i40e_parse_dcb_configure(dev, &dcb_cfg, &tc_map);
8350         if (ret) {
8351                 PMD_INIT_LOG(ERR, "invalid dcb config");
8352                 return -EINVAL;
8353         }
8354         ret = i40e_dcb_hw_configure(pf, &dcb_cfg, tc_map);
8355         if (ret) {
8356                 PMD_INIT_LOG(ERR, "dcb sw configure fails");
8357                 return -ENOSYS;
8358         }
8359
8360         return 0;
8361 }
8362
8363 static int
8364 i40e_dev_get_dcb_info(struct rte_eth_dev *dev,
8365                       struct rte_eth_dcb_info *dcb_info)
8366 {
8367         struct i40e_pf *pf = I40E_DEV_PRIVATE_TO_PF(dev->data->dev_private);
8368         struct i40e_hw *hw = I40E_DEV_PRIVATE_TO_HW(dev->data->dev_private);
8369         struct i40e_vsi *vsi = pf->main_vsi;
8370         struct i40e_dcbx_config *dcb_cfg = &hw->local_dcbx_config;
8371         uint16_t bsf, tc_mapping;
8372         int i;
8373
8374         if (dev->data->dev_conf.rxmode.mq_mode & ETH_MQ_RX_DCB_FLAG)
8375                 dcb_info->nb_tcs = rte_bsf32(vsi->enabled_tc + 1);
8376         else
8377                 dcb_info->nb_tcs = 1;
8378         for (i = 0; i < I40E_MAX_USER_PRIORITY; i++)
8379                 dcb_info->prio_tc[i] = dcb_cfg->etscfg.prioritytable[i];
8380         for (i = 0; i < dcb_info->nb_tcs; i++)
8381                 dcb_info->tc_bws[i] = dcb_cfg->etscfg.tcbwtable[i];
8382
8383         for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
8384                 if (vsi->enabled_tc & (1 << i)) {
8385                         tc_mapping = rte_le_to_cpu_16(vsi->info.tc_mapping[i]);
8386                         /* only main vsi support multi TCs */
8387                         dcb_info->tc_queue.tc_rxq[0][i].base =
8388                                 (tc_mapping & I40E_AQ_VSI_TC_QUE_OFFSET_MASK) >>
8389                                 I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT;
8390                         dcb_info->tc_queue.tc_txq[0][i].base =
8391                                 dcb_info->tc_queue.tc_rxq[0][i].base;
8392                         bsf = (tc_mapping & I40E_AQ_VSI_TC_QUE_NUMBER_MASK) >>
8393                                 I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT;
8394                         dcb_info->tc_queue.tc_rxq[0][i].nb_queue = 1 << bsf;
8395                         dcb_info->tc_queue.tc_txq[0][i].nb_queue =
8396                                 dcb_info->tc_queue.tc_rxq[0][i].nb_queue;
8397                 }
8398         }
8399
8400         return 0;
8401 }
8402
8403 static int
8404 i40e_dev_rx_queue_intr_enable(struct rte_eth_dev *dev, uint16_t queue_id)
8405 {
8406         struct rte_intr_handle *intr_handle = &dev->pci_dev->intr_handle;
8407         struct i40e_hw *hw = I40E_DEV_PRIVATE_TO_HW(dev->data->dev_private);
8408         uint16_t interval =
8409                 i40e_calc_itr_interval(RTE_LIBRTE_I40E_ITR_INTERVAL);
8410         uint16_t msix_intr;
8411
8412         msix_intr = intr_handle->intr_vec[queue_id];
8413         if (msix_intr == I40E_MISC_VEC_ID)
8414                 I40E_WRITE_REG(hw, I40E_PFINT_DYN_CTL0,
8415                                I40E_PFINT_DYN_CTLN_INTENA_MASK |
8416                                I40E_PFINT_DYN_CTLN_CLEARPBA_MASK |
8417                                (0 << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT) |
8418                                (interval <<
8419                                 I40E_PFINT_DYN_CTLN_INTERVAL_SHIFT));
8420         else
8421                 I40E_WRITE_REG(hw,
8422                                I40E_PFINT_DYN_CTLN(msix_intr -
8423                                                    I40E_RX_VEC_START),
8424                                I40E_PFINT_DYN_CTLN_INTENA_MASK |
8425                                I40E_PFINT_DYN_CTLN_CLEARPBA_MASK |
8426                                (0 << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT) |
8427                                (interval <<
8428                                 I40E_PFINT_DYN_CTLN_INTERVAL_SHIFT));
8429
8430         I40E_WRITE_FLUSH(hw);
8431         rte_intr_enable(&dev->pci_dev->intr_handle);
8432
8433         return 0;
8434 }
8435
8436 static int
8437 i40e_dev_rx_queue_intr_disable(struct rte_eth_dev *dev, uint16_t queue_id)
8438 {
8439         struct rte_intr_handle *intr_handle = &dev->pci_dev->intr_handle;
8440         struct i40e_hw *hw = I40E_DEV_PRIVATE_TO_HW(dev->data->dev_private);
8441         uint16_t msix_intr;
8442
8443         msix_intr = intr_handle->intr_vec[queue_id];
8444         if (msix_intr == I40E_MISC_VEC_ID)
8445                 I40E_WRITE_REG(hw, I40E_PFINT_DYN_CTL0, 0);
8446         else
8447                 I40E_WRITE_REG(hw,
8448                                I40E_PFINT_DYN_CTLN(msix_intr -
8449                                                    I40E_RX_VEC_START),
8450                                0);
8451         I40E_WRITE_FLUSH(hw);
8452
8453         return 0;
8454 }