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