net/ice: support protocol extraction per Rx queue
[dpdk.git] / drivers / net / ice / ice_ethdev.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2018 Intel Corporation
3  */
4
5 #include <rte_string_fns.h>
6 #include <rte_ethdev_pci.h>
7
8 #include <stdio.h>
9 #include <sys/types.h>
10 #include <sys/stat.h>
11 #include <unistd.h>
12
13 #include "base/ice_sched.h"
14 #include "base/ice_flow.h"
15 #include "base/ice_dcb.h"
16 #include "ice_ethdev.h"
17 #include "ice_rxtx.h"
18 #include "ice_switch_filter.h"
19
20 /* devargs */
21 #define ICE_SAFE_MODE_SUPPORT_ARG "safe-mode-support"
22 #define ICE_PROTO_XTR_ARG         "proto_xtr"
23
24 static const char * const ice_valid_args[] = {
25         ICE_SAFE_MODE_SUPPORT_ARG,
26         ICE_PROTO_XTR_ARG,
27         NULL
28 };
29
30 #define ICE_DFLT_OUTER_TAG_TYPE ICE_AQ_VSI_OUTER_TAG_VLAN_9100
31
32 /* DDP package search path */
33 #define ICE_PKG_FILE_DEFAULT "/lib/firmware/intel/ice/ddp/ice.pkg"
34 #define ICE_PKG_FILE_UPDATES "/lib/firmware/updates/intel/ice/ddp/ice.pkg"
35 #define ICE_PKG_FILE_SEARCH_PATH_DEFAULT "/lib/firmware/intel/ice/ddp/"
36 #define ICE_PKG_FILE_SEARCH_PATH_UPDATES "/lib/firmware/updates/intel/ice/ddp/"
37
38 #define ICE_OS_DEFAULT_PKG_NAME         "ICE OS Default Package"
39 #define ICE_COMMS_PKG_NAME                      "ICE COMMS Package"
40 #define ICE_MAX_PKG_FILENAME_SIZE   256
41
42 int ice_logtype_init;
43 int ice_logtype_driver;
44
45 static int ice_dev_configure(struct rte_eth_dev *dev);
46 static int ice_dev_start(struct rte_eth_dev *dev);
47 static void ice_dev_stop(struct rte_eth_dev *dev);
48 static void ice_dev_close(struct rte_eth_dev *dev);
49 static int ice_dev_reset(struct rte_eth_dev *dev);
50 static int ice_dev_info_get(struct rte_eth_dev *dev,
51                             struct rte_eth_dev_info *dev_info);
52 static int ice_link_update(struct rte_eth_dev *dev,
53                            int wait_to_complete);
54 static int ice_dev_set_link_up(struct rte_eth_dev *dev);
55 static int ice_dev_set_link_down(struct rte_eth_dev *dev);
56
57 static int ice_mtu_set(struct rte_eth_dev *dev, uint16_t mtu);
58 static int ice_vlan_offload_set(struct rte_eth_dev *dev, int mask);
59 static int ice_vlan_tpid_set(struct rte_eth_dev *dev,
60                              enum rte_vlan_type vlan_type,
61                              uint16_t tpid);
62 static int ice_rss_reta_update(struct rte_eth_dev *dev,
63                                struct rte_eth_rss_reta_entry64 *reta_conf,
64                                uint16_t reta_size);
65 static int ice_rss_reta_query(struct rte_eth_dev *dev,
66                               struct rte_eth_rss_reta_entry64 *reta_conf,
67                               uint16_t reta_size);
68 static int ice_rss_hash_update(struct rte_eth_dev *dev,
69                                struct rte_eth_rss_conf *rss_conf);
70 static int ice_rss_hash_conf_get(struct rte_eth_dev *dev,
71                                  struct rte_eth_rss_conf *rss_conf);
72 static int ice_promisc_enable(struct rte_eth_dev *dev);
73 static int ice_promisc_disable(struct rte_eth_dev *dev);
74 static int ice_allmulti_enable(struct rte_eth_dev *dev);
75 static int ice_allmulti_disable(struct rte_eth_dev *dev);
76 static int ice_vlan_filter_set(struct rte_eth_dev *dev,
77                                uint16_t vlan_id,
78                                int on);
79 static int ice_macaddr_set(struct rte_eth_dev *dev,
80                            struct rte_ether_addr *mac_addr);
81 static int ice_macaddr_add(struct rte_eth_dev *dev,
82                            struct rte_ether_addr *mac_addr,
83                            __rte_unused uint32_t index,
84                            uint32_t pool);
85 static void ice_macaddr_remove(struct rte_eth_dev *dev, uint32_t index);
86 static int ice_rx_queue_intr_enable(struct rte_eth_dev *dev,
87                                     uint16_t queue_id);
88 static int ice_rx_queue_intr_disable(struct rte_eth_dev *dev,
89                                      uint16_t queue_id);
90 static int ice_fw_version_get(struct rte_eth_dev *dev, char *fw_version,
91                               size_t fw_size);
92 static int ice_vlan_pvid_set(struct rte_eth_dev *dev,
93                              uint16_t pvid, int on);
94 static int ice_get_eeprom_length(struct rte_eth_dev *dev);
95 static int ice_get_eeprom(struct rte_eth_dev *dev,
96                           struct rte_dev_eeprom_info *eeprom);
97 static int ice_stats_get(struct rte_eth_dev *dev,
98                          struct rte_eth_stats *stats);
99 static int ice_stats_reset(struct rte_eth_dev *dev);
100 static int ice_xstats_get(struct rte_eth_dev *dev,
101                           struct rte_eth_xstat *xstats, unsigned int n);
102 static int ice_xstats_get_names(struct rte_eth_dev *dev,
103                                 struct rte_eth_xstat_name *xstats_names,
104                                 unsigned int limit);
105 static int ice_dev_filter_ctrl(struct rte_eth_dev *dev,
106                         enum rte_filter_type filter_type,
107                         enum rte_filter_op filter_op,
108                         void *arg);
109 static int ice_dev_udp_tunnel_port_add(struct rte_eth_dev *dev,
110                         struct rte_eth_udp_tunnel *udp_tunnel);
111 static int ice_dev_udp_tunnel_port_del(struct rte_eth_dev *dev,
112                         struct rte_eth_udp_tunnel *udp_tunnel);
113
114 static const struct rte_pci_id pci_id_ice_map[] = {
115         { RTE_PCI_DEVICE(ICE_INTEL_VENDOR_ID, ICE_DEV_ID_E810C_BACKPLANE) },
116         { RTE_PCI_DEVICE(ICE_INTEL_VENDOR_ID, ICE_DEV_ID_E810C_QSFP) },
117         { RTE_PCI_DEVICE(ICE_INTEL_VENDOR_ID, ICE_DEV_ID_E810C_SFP) },
118         { .vendor_id = 0, /* sentinel */ },
119 };
120
121 static const struct eth_dev_ops ice_eth_dev_ops = {
122         .dev_configure                = ice_dev_configure,
123         .dev_start                    = ice_dev_start,
124         .dev_stop                     = ice_dev_stop,
125         .dev_close                    = ice_dev_close,
126         .dev_reset                    = ice_dev_reset,
127         .dev_set_link_up              = ice_dev_set_link_up,
128         .dev_set_link_down            = ice_dev_set_link_down,
129         .rx_queue_start               = ice_rx_queue_start,
130         .rx_queue_stop                = ice_rx_queue_stop,
131         .tx_queue_start               = ice_tx_queue_start,
132         .tx_queue_stop                = ice_tx_queue_stop,
133         .rx_queue_setup               = ice_rx_queue_setup,
134         .rx_queue_release             = ice_rx_queue_release,
135         .tx_queue_setup               = ice_tx_queue_setup,
136         .tx_queue_release             = ice_tx_queue_release,
137         .dev_infos_get                = ice_dev_info_get,
138         .dev_supported_ptypes_get     = ice_dev_supported_ptypes_get,
139         .link_update                  = ice_link_update,
140         .mtu_set                      = ice_mtu_set,
141         .mac_addr_set                 = ice_macaddr_set,
142         .mac_addr_add                 = ice_macaddr_add,
143         .mac_addr_remove              = ice_macaddr_remove,
144         .vlan_filter_set              = ice_vlan_filter_set,
145         .vlan_offload_set             = ice_vlan_offload_set,
146         .vlan_tpid_set                = ice_vlan_tpid_set,
147         .reta_update                  = ice_rss_reta_update,
148         .reta_query                   = ice_rss_reta_query,
149         .rss_hash_update              = ice_rss_hash_update,
150         .rss_hash_conf_get            = ice_rss_hash_conf_get,
151         .promiscuous_enable           = ice_promisc_enable,
152         .promiscuous_disable          = ice_promisc_disable,
153         .allmulticast_enable          = ice_allmulti_enable,
154         .allmulticast_disable         = ice_allmulti_disable,
155         .rx_queue_intr_enable         = ice_rx_queue_intr_enable,
156         .rx_queue_intr_disable        = ice_rx_queue_intr_disable,
157         .fw_version_get               = ice_fw_version_get,
158         .vlan_pvid_set                = ice_vlan_pvid_set,
159         .rxq_info_get                 = ice_rxq_info_get,
160         .txq_info_get                 = ice_txq_info_get,
161         .get_eeprom_length            = ice_get_eeprom_length,
162         .get_eeprom                   = ice_get_eeprom,
163         .rx_queue_count               = ice_rx_queue_count,
164         .rx_descriptor_status         = ice_rx_descriptor_status,
165         .tx_descriptor_status         = ice_tx_descriptor_status,
166         .stats_get                    = ice_stats_get,
167         .stats_reset                  = ice_stats_reset,
168         .xstats_get                   = ice_xstats_get,
169         .xstats_get_names             = ice_xstats_get_names,
170         .xstats_reset                 = ice_stats_reset,
171         .filter_ctrl                  = ice_dev_filter_ctrl,
172         .udp_tunnel_port_add          = ice_dev_udp_tunnel_port_add,
173         .udp_tunnel_port_del          = ice_dev_udp_tunnel_port_del,
174 };
175
176 /* store statistics names and its offset in stats structure */
177 struct ice_xstats_name_off {
178         char name[RTE_ETH_XSTATS_NAME_SIZE];
179         unsigned int offset;
180 };
181
182 static const struct ice_xstats_name_off ice_stats_strings[] = {
183         {"rx_unicast_packets", offsetof(struct ice_eth_stats, rx_unicast)},
184         {"rx_multicast_packets", offsetof(struct ice_eth_stats, rx_multicast)},
185         {"rx_broadcast_packets", offsetof(struct ice_eth_stats, rx_broadcast)},
186         {"rx_dropped_packets", offsetof(struct ice_eth_stats, rx_discards)},
187         {"rx_unknown_protocol_packets", offsetof(struct ice_eth_stats,
188                 rx_unknown_protocol)},
189         {"tx_unicast_packets", offsetof(struct ice_eth_stats, tx_unicast)},
190         {"tx_multicast_packets", offsetof(struct ice_eth_stats, tx_multicast)},
191         {"tx_broadcast_packets", offsetof(struct ice_eth_stats, tx_broadcast)},
192         {"tx_dropped_packets", offsetof(struct ice_eth_stats, tx_discards)},
193 };
194
195 #define ICE_NB_ETH_XSTATS (sizeof(ice_stats_strings) / \
196                 sizeof(ice_stats_strings[0]))
197
198 static const struct ice_xstats_name_off ice_hw_port_strings[] = {
199         {"tx_link_down_dropped", offsetof(struct ice_hw_port_stats,
200                 tx_dropped_link_down)},
201         {"rx_crc_errors", offsetof(struct ice_hw_port_stats, crc_errors)},
202         {"rx_illegal_byte_errors", offsetof(struct ice_hw_port_stats,
203                 illegal_bytes)},
204         {"rx_error_bytes", offsetof(struct ice_hw_port_stats, error_bytes)},
205         {"mac_local_errors", offsetof(struct ice_hw_port_stats,
206                 mac_local_faults)},
207         {"mac_remote_errors", offsetof(struct ice_hw_port_stats,
208                 mac_remote_faults)},
209         {"rx_len_errors", offsetof(struct ice_hw_port_stats,
210                 rx_len_errors)},
211         {"tx_xon_packets", offsetof(struct ice_hw_port_stats, link_xon_tx)},
212         {"rx_xon_packets", offsetof(struct ice_hw_port_stats, link_xon_rx)},
213         {"tx_xoff_packets", offsetof(struct ice_hw_port_stats, link_xoff_tx)},
214         {"rx_xoff_packets", offsetof(struct ice_hw_port_stats, link_xoff_rx)},
215         {"rx_size_64_packets", offsetof(struct ice_hw_port_stats, rx_size_64)},
216         {"rx_size_65_to_127_packets", offsetof(struct ice_hw_port_stats,
217                 rx_size_127)},
218         {"rx_size_128_to_255_packets", offsetof(struct ice_hw_port_stats,
219                 rx_size_255)},
220         {"rx_size_256_to_511_packets", offsetof(struct ice_hw_port_stats,
221                 rx_size_511)},
222         {"rx_size_512_to_1023_packets", offsetof(struct ice_hw_port_stats,
223                 rx_size_1023)},
224         {"rx_size_1024_to_1522_packets", offsetof(struct ice_hw_port_stats,
225                 rx_size_1522)},
226         {"rx_size_1523_to_max_packets", offsetof(struct ice_hw_port_stats,
227                 rx_size_big)},
228         {"rx_undersized_errors", offsetof(struct ice_hw_port_stats,
229                 rx_undersize)},
230         {"rx_oversize_errors", offsetof(struct ice_hw_port_stats,
231                 rx_oversize)},
232         {"rx_mac_short_pkt_dropped", offsetof(struct ice_hw_port_stats,
233                 mac_short_pkt_dropped)},
234         {"rx_fragmented_errors", offsetof(struct ice_hw_port_stats,
235                 rx_fragments)},
236         {"rx_jabber_errors", offsetof(struct ice_hw_port_stats, rx_jabber)},
237         {"tx_size_64_packets", offsetof(struct ice_hw_port_stats, tx_size_64)},
238         {"tx_size_65_to_127_packets", offsetof(struct ice_hw_port_stats,
239                 tx_size_127)},
240         {"tx_size_128_to_255_packets", offsetof(struct ice_hw_port_stats,
241                 tx_size_255)},
242         {"tx_size_256_to_511_packets", offsetof(struct ice_hw_port_stats,
243                 tx_size_511)},
244         {"tx_size_512_to_1023_packets", offsetof(struct ice_hw_port_stats,
245                 tx_size_1023)},
246         {"tx_size_1024_to_1522_packets", offsetof(struct ice_hw_port_stats,
247                 tx_size_1522)},
248         {"tx_size_1523_to_max_packets", offsetof(struct ice_hw_port_stats,
249                 tx_size_big)},
250 };
251
252 #define ICE_NB_HW_PORT_XSTATS (sizeof(ice_hw_port_strings) / \
253                 sizeof(ice_hw_port_strings[0]))
254
255 static void
256 ice_init_controlq_parameter(struct ice_hw *hw)
257 {
258         /* fields for adminq */
259         hw->adminq.num_rq_entries = ICE_ADMINQ_LEN;
260         hw->adminq.num_sq_entries = ICE_ADMINQ_LEN;
261         hw->adminq.rq_buf_size = ICE_ADMINQ_BUF_SZ;
262         hw->adminq.sq_buf_size = ICE_ADMINQ_BUF_SZ;
263
264         /* fields for mailboxq, DPDK used as PF host */
265         hw->mailboxq.num_rq_entries = ICE_MAILBOXQ_LEN;
266         hw->mailboxq.num_sq_entries = ICE_MAILBOXQ_LEN;
267         hw->mailboxq.rq_buf_size = ICE_MAILBOXQ_BUF_SZ;
268         hw->mailboxq.sq_buf_size = ICE_MAILBOXQ_BUF_SZ;
269 }
270
271 static int
272 lookup_proto_xtr_type(const char *xtr_name)
273 {
274         static struct {
275                 const char *name;
276                 enum proto_xtr_type type;
277         } xtr_type_map[] = {
278                 { "vlan",      PROTO_XTR_VLAN      },
279                 { "ipv4",      PROTO_XTR_IPV4      },
280                 { "ipv6",      PROTO_XTR_IPV6      },
281                 { "ipv6_flow", PROTO_XTR_IPV6_FLOW },
282                 { "tcp",       PROTO_XTR_TCP       },
283         };
284         uint32_t i;
285
286         for (i = 0; i < RTE_DIM(xtr_type_map); i++) {
287                 if (strcmp(xtr_name, xtr_type_map[i].name) == 0)
288                         return xtr_type_map[i].type;
289         }
290
291         return -1;
292 }
293
294 /*
295  * Parse elem, the elem could be single number/range or '(' ')' group
296  * 1) A single number elem, it's just a simple digit. e.g. 9
297  * 2) A single range elem, two digits with a '-' between. e.g. 2-6
298  * 3) A group elem, combines multiple 1) or 2) with '( )'. e.g (0,2-4,6)
299  *    Within group elem, '-' used for a range separator;
300  *                       ',' used for a single number.
301  */
302 static int
303 parse_queue_set(const char *input, int xtr_type, struct ice_devargs *devargs)
304 {
305         const char *str = input;
306         char *end = NULL;
307         uint32_t min, max;
308         uint32_t idx;
309
310         while (isblank(*str))
311                 str++;
312
313         if (!isdigit(*str) && *str != '(')
314                 return -1;
315
316         /* process single number or single range of number */
317         if (*str != '(') {
318                 errno = 0;
319                 idx = strtoul(str, &end, 10);
320                 if (errno || end == NULL || idx >= ICE_MAX_QUEUE_NUM)
321                         return -1;
322
323                 while (isblank(*end))
324                         end++;
325
326                 min = idx;
327                 max = idx;
328
329                 /* process single <number>-<number> */
330                 if (*end == '-') {
331                         end++;
332                         while (isblank(*end))
333                                 end++;
334                         if (!isdigit(*end))
335                                 return -1;
336
337                         errno = 0;
338                         idx = strtoul(end, &end, 10);
339                         if (errno || end == NULL || idx >= ICE_MAX_QUEUE_NUM)
340                                 return -1;
341
342                         max = idx;
343                         while (isblank(*end))
344                                 end++;
345                 }
346
347                 if (*end != ':')
348                         return -1;
349
350                 for (idx = RTE_MIN(min, max);
351                      idx <= RTE_MAX(min, max); idx++)
352                         devargs->proto_xtr[idx] = xtr_type;
353
354                 return 0;
355         }
356
357         /* process set within bracket */
358         str++;
359         while (isblank(*str))
360                 str++;
361         if (*str == '\0')
362                 return -1;
363
364         min = ICE_MAX_QUEUE_NUM;
365         do {
366                 /* go ahead to the first digit */
367                 while (isblank(*str))
368                         str++;
369                 if (!isdigit(*str))
370                         return -1;
371
372                 /* get the digit value */
373                 errno = 0;
374                 idx = strtoul(str, &end, 10);
375                 if (errno || end == NULL || idx >= ICE_MAX_QUEUE_NUM)
376                         return -1;
377
378                 /* go ahead to separator '-',',' and ')' */
379                 while (isblank(*end))
380                         end++;
381                 if (*end == '-') {
382                         if (min == ICE_MAX_QUEUE_NUM)
383                                 min = idx;
384                         else /* avoid continuous '-' */
385                                 return -1;
386                 } else if (*end == ',' || *end == ')') {
387                         max = idx;
388                         if (min == ICE_MAX_QUEUE_NUM)
389                                 min = idx;
390
391                         for (idx = RTE_MIN(min, max);
392                              idx <= RTE_MAX(min, max); idx++)
393                                 devargs->proto_xtr[idx] = xtr_type;
394
395                         min = ICE_MAX_QUEUE_NUM;
396                 } else {
397                         return -1;
398                 }
399
400                 str = end + 1;
401         } while (*end != ')' && *end != '\0');
402
403         return 0;
404 }
405
406 static int
407 parse_queue_proto_xtr(const char *queues, struct ice_devargs *devargs)
408 {
409         const char *queue_start;
410         uint32_t idx;
411         int xtr_type;
412         char xtr_name[32];
413
414         while (isblank(*queues))
415                 queues++;
416
417         if (*queues != '[') {
418                 xtr_type = lookup_proto_xtr_type(queues);
419                 if (xtr_type < 0)
420                         return -1;
421
422                 memset(devargs->proto_xtr, xtr_type,
423                        sizeof(devargs->proto_xtr));
424
425                 return 0;
426         }
427
428         queues++;
429         do {
430                 while (isblank(*queues))
431                         queues++;
432                 if (*queues == '\0')
433                         return -1;
434
435                 queue_start = queues;
436
437                 /* go across a complete bracket */
438                 if (*queue_start == '(') {
439                         queues += strcspn(queues, ")");
440                         if (*queues != ')')
441                                 return -1;
442                 }
443
444                 /* scan the separator ':' */
445                 queues += strcspn(queues, ":");
446                 if (*queues++ != ':')
447                         return -1;
448                 while (isblank(*queues))
449                         queues++;
450
451                 for (idx = 0; ; idx++) {
452                         if (isblank(queues[idx]) ||
453                             queues[idx] == ',' ||
454                             queues[idx] == ']' ||
455                             queues[idx] == '\0')
456                                 break;
457
458                         if (idx > sizeof(xtr_name) - 2)
459                                 return -1;
460
461                         xtr_name[idx] = queues[idx];
462                 }
463                 xtr_name[idx] = '\0';
464                 xtr_type = lookup_proto_xtr_type(xtr_name);
465                 if (xtr_type < 0)
466                         return -1;
467
468                 queues += idx;
469
470                 while (isblank(*queues) || *queues == ',' || *queues == ']')
471                         queues++;
472
473                 if (parse_queue_set(queue_start, xtr_type, devargs) < 0)
474                         return -1;
475         } while (*queues != '\0');
476
477         return 0;
478 }
479
480 static int
481 handle_proto_xtr_arg(__rte_unused const char *key, const char *value,
482                      void *extra_args)
483 {
484         struct ice_devargs *devargs = extra_args;
485
486         if (value == NULL || extra_args == NULL)
487                 return -EINVAL;
488
489         if (parse_queue_proto_xtr(value, devargs) < 0) {
490                 PMD_DRV_LOG(ERR,
491                             "The protocol extraction parameter is wrong : '%s'",
492                             value);
493                 return -1;
494         }
495
496         return 0;
497 }
498
499 static bool
500 ice_proto_xtr_support(struct ice_hw *hw)
501 {
502 #define FLX_REG(val, fld, idx) \
503         (((val) & GLFLXP_RXDID_FLX_WRD_##idx##_##fld##_M) >> \
504          GLFLXP_RXDID_FLX_WRD_##idx##_##fld##_S)
505         static struct {
506                 uint32_t rxdid;
507                 uint16_t protid_0;
508                 uint16_t protid_1;
509         } xtr_sets[] = {
510                 { ICE_RXDID_COMMS_AUX_VLAN, ICE_PROT_EVLAN_O, ICE_PROT_VLAN_O },
511                 { ICE_RXDID_COMMS_AUX_IPV4, ICE_PROT_IPV4_OF_OR_S,
512                   ICE_PROT_IPV4_OF_OR_S },
513                 { ICE_RXDID_COMMS_AUX_IPV6, ICE_PROT_IPV6_OF_OR_S,
514                   ICE_PROT_IPV6_OF_OR_S },
515                 { ICE_RXDID_COMMS_AUX_IPV6_FLOW, ICE_PROT_IPV6_OF_OR_S,
516                   ICE_PROT_IPV6_OF_OR_S },
517                 { ICE_RXDID_COMMS_AUX_TCP, ICE_PROT_TCP_IL, ICE_PROT_ID_INVAL },
518         };
519         uint32_t i;
520
521         for (i = 0; i < RTE_DIM(xtr_sets); i++) {
522                 uint32_t rxdid = xtr_sets[i].rxdid;
523                 uint32_t v;
524
525                 if (xtr_sets[i].protid_0 != ICE_PROT_ID_INVAL) {
526                         v = ICE_READ_REG(hw, GLFLXP_RXDID_FLX_WRD_4(rxdid));
527
528                         if (FLX_REG(v, PROT_MDID, 4) != xtr_sets[i].protid_0 ||
529                             FLX_REG(v, RXDID_OPCODE, 4) != ICE_RX_OPC_EXTRACT)
530                                 return false;
531                 }
532
533                 if (xtr_sets[i].protid_1 != ICE_PROT_ID_INVAL) {
534                         v = ICE_READ_REG(hw, GLFLXP_RXDID_FLX_WRD_5(rxdid));
535
536                         if (FLX_REG(v, PROT_MDID, 5) != xtr_sets[i].protid_1 ||
537                             FLX_REG(v, RXDID_OPCODE, 5) != ICE_RX_OPC_EXTRACT)
538                                 return false;
539                 }
540         }
541
542         return true;
543 }
544
545 static int
546 ice_res_pool_init(struct ice_res_pool_info *pool, uint32_t base,
547                   uint32_t num)
548 {
549         struct pool_entry *entry;
550
551         if (!pool || !num)
552                 return -EINVAL;
553
554         entry = rte_zmalloc(NULL, sizeof(*entry), 0);
555         if (!entry) {
556                 PMD_INIT_LOG(ERR,
557                              "Failed to allocate memory for resource pool");
558                 return -ENOMEM;
559         }
560
561         /* queue heap initialize */
562         pool->num_free = num;
563         pool->num_alloc = 0;
564         pool->base = base;
565         LIST_INIT(&pool->alloc_list);
566         LIST_INIT(&pool->free_list);
567
568         /* Initialize element  */
569         entry->base = 0;
570         entry->len = num;
571
572         LIST_INSERT_HEAD(&pool->free_list, entry, next);
573         return 0;
574 }
575
576 static int
577 ice_res_pool_alloc(struct ice_res_pool_info *pool,
578                    uint16_t num)
579 {
580         struct pool_entry *entry, *valid_entry;
581
582         if (!pool || !num) {
583                 PMD_INIT_LOG(ERR, "Invalid parameter");
584                 return -EINVAL;
585         }
586
587         if (pool->num_free < num) {
588                 PMD_INIT_LOG(ERR, "No resource. ask:%u, available:%u",
589                              num, pool->num_free);
590                 return -ENOMEM;
591         }
592
593         valid_entry = NULL;
594         /* Lookup  in free list and find most fit one */
595         LIST_FOREACH(entry, &pool->free_list, next) {
596                 if (entry->len >= num) {
597                         /* Find best one */
598                         if (entry->len == num) {
599                                 valid_entry = entry;
600                                 break;
601                         }
602                         if (!valid_entry ||
603                             valid_entry->len > entry->len)
604                                 valid_entry = entry;
605                 }
606         }
607
608         /* Not find one to satisfy the request, return */
609         if (!valid_entry) {
610                 PMD_INIT_LOG(ERR, "No valid entry found");
611                 return -ENOMEM;
612         }
613         /**
614          * The entry have equal queue number as requested,
615          * remove it from alloc_list.
616          */
617         if (valid_entry->len == num) {
618                 LIST_REMOVE(valid_entry, next);
619         } else {
620                 /**
621                  * The entry have more numbers than requested,
622                  * create a new entry for alloc_list and minus its
623                  * queue base and number in free_list.
624                  */
625                 entry = rte_zmalloc(NULL, sizeof(*entry), 0);
626                 if (!entry) {
627                         PMD_INIT_LOG(ERR,
628                                      "Failed to allocate memory for "
629                                      "resource pool");
630                         return -ENOMEM;
631                 }
632                 entry->base = valid_entry->base;
633                 entry->len = num;
634                 valid_entry->base += num;
635                 valid_entry->len -= num;
636                 valid_entry = entry;
637         }
638
639         /* Insert it into alloc list, not sorted */
640         LIST_INSERT_HEAD(&pool->alloc_list, valid_entry, next);
641
642         pool->num_free -= valid_entry->len;
643         pool->num_alloc += valid_entry->len;
644
645         return valid_entry->base + pool->base;
646 }
647
648 static void
649 ice_res_pool_destroy(struct ice_res_pool_info *pool)
650 {
651         struct pool_entry *entry, *next_entry;
652
653         if (!pool)
654                 return;
655
656         for (entry = LIST_FIRST(&pool->alloc_list);
657              entry && (next_entry = LIST_NEXT(entry, next), 1);
658              entry = next_entry) {
659                 LIST_REMOVE(entry, next);
660                 rte_free(entry);
661         }
662
663         for (entry = LIST_FIRST(&pool->free_list);
664              entry && (next_entry = LIST_NEXT(entry, next), 1);
665              entry = next_entry) {
666                 LIST_REMOVE(entry, next);
667                 rte_free(entry);
668         }
669
670         pool->num_free = 0;
671         pool->num_alloc = 0;
672         pool->base = 0;
673         LIST_INIT(&pool->alloc_list);
674         LIST_INIT(&pool->free_list);
675 }
676
677 static void
678 ice_vsi_config_default_rss(struct ice_aqc_vsi_props *info)
679 {
680         /* Set VSI LUT selection */
681         info->q_opt_rss = ICE_AQ_VSI_Q_OPT_RSS_LUT_VSI &
682                           ICE_AQ_VSI_Q_OPT_RSS_LUT_M;
683         /* Set Hash scheme */
684         info->q_opt_rss |= ICE_AQ_VSI_Q_OPT_RSS_TPLZ &
685                            ICE_AQ_VSI_Q_OPT_RSS_HASH_M;
686         /* enable TC */
687         info->q_opt_tc = ICE_AQ_VSI_Q_OPT_TC_OVR_M;
688 }
689
690 static enum ice_status
691 ice_vsi_config_tc_queue_mapping(struct ice_vsi *vsi,
692                                 struct ice_aqc_vsi_props *info,
693                                 uint8_t enabled_tcmap)
694 {
695         uint16_t bsf, qp_idx;
696
697         /* default tc 0 now. Multi-TC supporting need to be done later.
698          * Configure TC and queue mapping parameters, for enabled TC,
699          * allocate qpnum_per_tc queues to this traffic.
700          */
701         if (enabled_tcmap != 0x01) {
702                 PMD_INIT_LOG(ERR, "only TC0 is supported");
703                 return -ENOTSUP;
704         }
705
706         vsi->nb_qps = RTE_MIN(vsi->nb_qps, ICE_MAX_Q_PER_TC);
707         bsf = rte_bsf32(vsi->nb_qps);
708         /* Adjust the queue number to actual queues that can be applied */
709         vsi->nb_qps = 0x1 << bsf;
710
711         qp_idx = 0;
712         /* Set tc and queue mapping with VSI */
713         info->tc_mapping[0] = rte_cpu_to_le_16((qp_idx <<
714                                                 ICE_AQ_VSI_TC_Q_OFFSET_S) |
715                                                (bsf << ICE_AQ_VSI_TC_Q_NUM_S));
716
717         /* Associate queue number with VSI */
718         info->mapping_flags |= rte_cpu_to_le_16(ICE_AQ_VSI_Q_MAP_CONTIG);
719         info->q_mapping[0] = rte_cpu_to_le_16(vsi->base_queue);
720         info->q_mapping[1] = rte_cpu_to_le_16(vsi->nb_qps);
721         info->valid_sections |=
722                 rte_cpu_to_le_16(ICE_AQ_VSI_PROP_RXQ_MAP_VALID);
723         /* Set the info.ingress_table and info.egress_table
724          * for UP translate table. Now just set it to 1:1 map by default
725          * -- 0b 111 110 101 100 011 010 001 000 == 0xFAC688
726          */
727 #define ICE_TC_QUEUE_TABLE_DFLT 0x00FAC688
728         info->ingress_table  = rte_cpu_to_le_32(ICE_TC_QUEUE_TABLE_DFLT);
729         info->egress_table   = rte_cpu_to_le_32(ICE_TC_QUEUE_TABLE_DFLT);
730         info->outer_up_table = rte_cpu_to_le_32(ICE_TC_QUEUE_TABLE_DFLT);
731         return 0;
732 }
733
734 static int
735 ice_init_mac_address(struct rte_eth_dev *dev)
736 {
737         struct ice_hw *hw = ICE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
738
739         if (!rte_is_unicast_ether_addr
740                 ((struct rte_ether_addr *)hw->port_info[0].mac.lan_addr)) {
741                 PMD_INIT_LOG(ERR, "Invalid MAC address");
742                 return -EINVAL;
743         }
744
745         rte_ether_addr_copy(
746                 (struct rte_ether_addr *)hw->port_info[0].mac.lan_addr,
747                 (struct rte_ether_addr *)hw->port_info[0].mac.perm_addr);
748
749         dev->data->mac_addrs =
750                 rte_zmalloc(NULL, sizeof(struct rte_ether_addr), 0);
751         if (!dev->data->mac_addrs) {
752                 PMD_INIT_LOG(ERR,
753                              "Failed to allocate memory to store mac address");
754                 return -ENOMEM;
755         }
756         /* store it to dev data */
757         rte_ether_addr_copy(
758                 (struct rte_ether_addr *)hw->port_info[0].mac.perm_addr,
759                 &dev->data->mac_addrs[0]);
760         return 0;
761 }
762
763 /* Find out specific MAC filter */
764 static struct ice_mac_filter *
765 ice_find_mac_filter(struct ice_vsi *vsi, struct rte_ether_addr *macaddr)
766 {
767         struct ice_mac_filter *f;
768
769         TAILQ_FOREACH(f, &vsi->mac_list, next) {
770                 if (rte_is_same_ether_addr(macaddr, &f->mac_info.mac_addr))
771                         return f;
772         }
773
774         return NULL;
775 }
776
777 static int
778 ice_add_mac_filter(struct ice_vsi *vsi, struct rte_ether_addr *mac_addr)
779 {
780         struct ice_fltr_list_entry *m_list_itr = NULL;
781         struct ice_mac_filter *f;
782         struct LIST_HEAD_TYPE list_head;
783         struct ice_hw *hw = ICE_VSI_TO_HW(vsi);
784         int ret = 0;
785
786         /* If it's added and configured, return */
787         f = ice_find_mac_filter(vsi, mac_addr);
788         if (f) {
789                 PMD_DRV_LOG(INFO, "This MAC filter already exists.");
790                 return 0;
791         }
792
793         INIT_LIST_HEAD(&list_head);
794
795         m_list_itr = (struct ice_fltr_list_entry *)
796                 ice_malloc(hw, sizeof(*m_list_itr));
797         if (!m_list_itr) {
798                 ret = -ENOMEM;
799                 goto DONE;
800         }
801         ice_memcpy(m_list_itr->fltr_info.l_data.mac.mac_addr,
802                    mac_addr, ETH_ALEN, ICE_NONDMA_TO_NONDMA);
803         m_list_itr->fltr_info.src_id = ICE_SRC_ID_VSI;
804         m_list_itr->fltr_info.fltr_act = ICE_FWD_TO_VSI;
805         m_list_itr->fltr_info.lkup_type = ICE_SW_LKUP_MAC;
806         m_list_itr->fltr_info.flag = ICE_FLTR_TX;
807         m_list_itr->fltr_info.vsi_handle = vsi->idx;
808
809         LIST_ADD(&m_list_itr->list_entry, &list_head);
810
811         /* Add the mac */
812         ret = ice_add_mac(hw, &list_head);
813         if (ret != ICE_SUCCESS) {
814                 PMD_DRV_LOG(ERR, "Failed to add MAC filter");
815                 ret = -EINVAL;
816                 goto DONE;
817         }
818         /* Add the mac addr into mac list */
819         f = rte_zmalloc(NULL, sizeof(*f), 0);
820         if (!f) {
821                 PMD_DRV_LOG(ERR, "failed to allocate memory");
822                 ret = -ENOMEM;
823                 goto DONE;
824         }
825         rte_memcpy(&f->mac_info.mac_addr, mac_addr, ETH_ADDR_LEN);
826         TAILQ_INSERT_TAIL(&vsi->mac_list, f, next);
827         vsi->mac_num++;
828
829         ret = 0;
830
831 DONE:
832         rte_free(m_list_itr);
833         return ret;
834 }
835
836 static int
837 ice_remove_mac_filter(struct ice_vsi *vsi, struct rte_ether_addr *mac_addr)
838 {
839         struct ice_fltr_list_entry *m_list_itr = NULL;
840         struct ice_mac_filter *f;
841         struct LIST_HEAD_TYPE list_head;
842         struct ice_hw *hw = ICE_VSI_TO_HW(vsi);
843         int ret = 0;
844
845         /* Can't find it, return an error */
846         f = ice_find_mac_filter(vsi, mac_addr);
847         if (!f)
848                 return -EINVAL;
849
850         INIT_LIST_HEAD(&list_head);
851
852         m_list_itr = (struct ice_fltr_list_entry *)
853                 ice_malloc(hw, sizeof(*m_list_itr));
854         if (!m_list_itr) {
855                 ret = -ENOMEM;
856                 goto DONE;
857         }
858         ice_memcpy(m_list_itr->fltr_info.l_data.mac.mac_addr,
859                    mac_addr, ETH_ALEN, ICE_NONDMA_TO_NONDMA);
860         m_list_itr->fltr_info.src_id = ICE_SRC_ID_VSI;
861         m_list_itr->fltr_info.fltr_act = ICE_FWD_TO_VSI;
862         m_list_itr->fltr_info.lkup_type = ICE_SW_LKUP_MAC;
863         m_list_itr->fltr_info.flag = ICE_FLTR_TX;
864         m_list_itr->fltr_info.vsi_handle = vsi->idx;
865
866         LIST_ADD(&m_list_itr->list_entry, &list_head);
867
868         /* remove the mac filter */
869         ret = ice_remove_mac(hw, &list_head);
870         if (ret != ICE_SUCCESS) {
871                 PMD_DRV_LOG(ERR, "Failed to remove MAC filter");
872                 ret = -EINVAL;
873                 goto DONE;
874         }
875
876         /* Remove the mac addr from mac list */
877         TAILQ_REMOVE(&vsi->mac_list, f, next);
878         rte_free(f);
879         vsi->mac_num--;
880
881         ret = 0;
882 DONE:
883         rte_free(m_list_itr);
884         return ret;
885 }
886
887 /* Find out specific VLAN filter */
888 static struct ice_vlan_filter *
889 ice_find_vlan_filter(struct ice_vsi *vsi, uint16_t vlan_id)
890 {
891         struct ice_vlan_filter *f;
892
893         TAILQ_FOREACH(f, &vsi->vlan_list, next) {
894                 if (vlan_id == f->vlan_info.vlan_id)
895                         return f;
896         }
897
898         return NULL;
899 }
900
901 static int
902 ice_add_vlan_filter(struct ice_vsi *vsi, uint16_t vlan_id)
903 {
904         struct ice_fltr_list_entry *v_list_itr = NULL;
905         struct ice_vlan_filter *f;
906         struct LIST_HEAD_TYPE list_head;
907         struct ice_hw *hw;
908         int ret = 0;
909
910         if (!vsi || vlan_id > RTE_ETHER_MAX_VLAN_ID)
911                 return -EINVAL;
912
913         hw = ICE_VSI_TO_HW(vsi);
914
915         /* If it's added and configured, return. */
916         f = ice_find_vlan_filter(vsi, vlan_id);
917         if (f) {
918                 PMD_DRV_LOG(INFO, "This VLAN filter already exists.");
919                 return 0;
920         }
921
922         if (!vsi->vlan_anti_spoof_on && !vsi->vlan_filter_on)
923                 return 0;
924
925         INIT_LIST_HEAD(&list_head);
926
927         v_list_itr = (struct ice_fltr_list_entry *)
928                       ice_malloc(hw, sizeof(*v_list_itr));
929         if (!v_list_itr) {
930                 ret = -ENOMEM;
931                 goto DONE;
932         }
933         v_list_itr->fltr_info.l_data.vlan.vlan_id = vlan_id;
934         v_list_itr->fltr_info.src_id = ICE_SRC_ID_VSI;
935         v_list_itr->fltr_info.fltr_act = ICE_FWD_TO_VSI;
936         v_list_itr->fltr_info.lkup_type = ICE_SW_LKUP_VLAN;
937         v_list_itr->fltr_info.flag = ICE_FLTR_TX;
938         v_list_itr->fltr_info.vsi_handle = vsi->idx;
939
940         LIST_ADD(&v_list_itr->list_entry, &list_head);
941
942         /* Add the vlan */
943         ret = ice_add_vlan(hw, &list_head);
944         if (ret != ICE_SUCCESS) {
945                 PMD_DRV_LOG(ERR, "Failed to add VLAN filter");
946                 ret = -EINVAL;
947                 goto DONE;
948         }
949
950         /* Add vlan into vlan list */
951         f = rte_zmalloc(NULL, sizeof(*f), 0);
952         if (!f) {
953                 PMD_DRV_LOG(ERR, "failed to allocate memory");
954                 ret = -ENOMEM;
955                 goto DONE;
956         }
957         f->vlan_info.vlan_id = vlan_id;
958         TAILQ_INSERT_TAIL(&vsi->vlan_list, f, next);
959         vsi->vlan_num++;
960
961         ret = 0;
962
963 DONE:
964         rte_free(v_list_itr);
965         return ret;
966 }
967
968 static int
969 ice_remove_vlan_filter(struct ice_vsi *vsi, uint16_t vlan_id)
970 {
971         struct ice_fltr_list_entry *v_list_itr = NULL;
972         struct ice_vlan_filter *f;
973         struct LIST_HEAD_TYPE list_head;
974         struct ice_hw *hw;
975         int ret = 0;
976
977         /**
978          * Vlan 0 is the generic filter for untagged packets
979          * and can't be removed.
980          */
981         if (!vsi || vlan_id == 0 || vlan_id > RTE_ETHER_MAX_VLAN_ID)
982                 return -EINVAL;
983
984         hw = ICE_VSI_TO_HW(vsi);
985
986         /* Can't find it, return an error */
987         f = ice_find_vlan_filter(vsi, vlan_id);
988         if (!f)
989                 return -EINVAL;
990
991         INIT_LIST_HEAD(&list_head);
992
993         v_list_itr = (struct ice_fltr_list_entry *)
994                       ice_malloc(hw, sizeof(*v_list_itr));
995         if (!v_list_itr) {
996                 ret = -ENOMEM;
997                 goto DONE;
998         }
999
1000         v_list_itr->fltr_info.l_data.vlan.vlan_id = vlan_id;
1001         v_list_itr->fltr_info.src_id = ICE_SRC_ID_VSI;
1002         v_list_itr->fltr_info.fltr_act = ICE_FWD_TO_VSI;
1003         v_list_itr->fltr_info.lkup_type = ICE_SW_LKUP_VLAN;
1004         v_list_itr->fltr_info.flag = ICE_FLTR_TX;
1005         v_list_itr->fltr_info.vsi_handle = vsi->idx;
1006
1007         LIST_ADD(&v_list_itr->list_entry, &list_head);
1008
1009         /* remove the vlan filter */
1010         ret = ice_remove_vlan(hw, &list_head);
1011         if (ret != ICE_SUCCESS) {
1012                 PMD_DRV_LOG(ERR, "Failed to remove VLAN filter");
1013                 ret = -EINVAL;
1014                 goto DONE;
1015         }
1016
1017         /* Remove the vlan id from vlan list */
1018         TAILQ_REMOVE(&vsi->vlan_list, f, next);
1019         rte_free(f);
1020         vsi->vlan_num--;
1021
1022         ret = 0;
1023 DONE:
1024         rte_free(v_list_itr);
1025         return ret;
1026 }
1027
1028 static int
1029 ice_remove_all_mac_vlan_filters(struct ice_vsi *vsi)
1030 {
1031         struct ice_mac_filter *m_f;
1032         struct ice_vlan_filter *v_f;
1033         int ret = 0;
1034
1035         if (!vsi || !vsi->mac_num)
1036                 return -EINVAL;
1037
1038         TAILQ_FOREACH(m_f, &vsi->mac_list, next) {
1039                 ret = ice_remove_mac_filter(vsi, &m_f->mac_info.mac_addr);
1040                 if (ret != ICE_SUCCESS) {
1041                         ret = -EINVAL;
1042                         goto DONE;
1043                 }
1044         }
1045
1046         if (vsi->vlan_num == 0)
1047                 return 0;
1048
1049         TAILQ_FOREACH(v_f, &vsi->vlan_list, next) {
1050                 ret = ice_remove_vlan_filter(vsi, v_f->vlan_info.vlan_id);
1051                 if (ret != ICE_SUCCESS) {
1052                         ret = -EINVAL;
1053                         goto DONE;
1054                 }
1055         }
1056
1057 DONE:
1058         return ret;
1059 }
1060
1061 static int
1062 ice_vsi_config_qinq_insertion(struct ice_vsi *vsi, bool on)
1063 {
1064         struct ice_hw *hw = ICE_VSI_TO_HW(vsi);
1065         struct ice_vsi_ctx ctxt;
1066         uint8_t qinq_flags;
1067         int ret = 0;
1068
1069         /* Check if it has been already on or off */
1070         if (vsi->info.valid_sections &
1071                 rte_cpu_to_le_16(ICE_AQ_VSI_PROP_OUTER_TAG_VALID)) {
1072                 if (on) {
1073                         if ((vsi->info.outer_tag_flags &
1074                              ICE_AQ_VSI_OUTER_TAG_ACCEPT_HOST) ==
1075                             ICE_AQ_VSI_OUTER_TAG_ACCEPT_HOST)
1076                                 return 0; /* already on */
1077                 } else {
1078                         if (!(vsi->info.outer_tag_flags &
1079                               ICE_AQ_VSI_OUTER_TAG_ACCEPT_HOST))
1080                                 return 0; /* already off */
1081                 }
1082         }
1083
1084         if (on)
1085                 qinq_flags = ICE_AQ_VSI_OUTER_TAG_ACCEPT_HOST;
1086         else
1087                 qinq_flags = 0;
1088         /* clear global insertion and use per packet insertion */
1089         vsi->info.outer_tag_flags &= ~(ICE_AQ_VSI_OUTER_TAG_INSERT);
1090         vsi->info.outer_tag_flags &= ~(ICE_AQ_VSI_OUTER_TAG_ACCEPT_HOST);
1091         vsi->info.outer_tag_flags |= qinq_flags;
1092         /* use default vlan type 0x8100 */
1093         vsi->info.outer_tag_flags &= ~(ICE_AQ_VSI_OUTER_TAG_TYPE_M);
1094         vsi->info.outer_tag_flags |= ICE_DFLT_OUTER_TAG_TYPE <<
1095                                      ICE_AQ_VSI_OUTER_TAG_TYPE_S;
1096         (void)rte_memcpy(&ctxt.info, &vsi->info, sizeof(vsi->info));
1097         ctxt.info.valid_sections =
1098                         rte_cpu_to_le_16(ICE_AQ_VSI_PROP_OUTER_TAG_VALID);
1099         ctxt.vsi_num = vsi->vsi_id;
1100         ret = ice_update_vsi(hw, vsi->idx, &ctxt, NULL);
1101         if (ret) {
1102                 PMD_DRV_LOG(INFO,
1103                             "Update VSI failed to %s qinq stripping",
1104                             on ? "enable" : "disable");
1105                 return -EINVAL;
1106         }
1107
1108         vsi->info.valid_sections |=
1109                 rte_cpu_to_le_16(ICE_AQ_VSI_PROP_OUTER_TAG_VALID);
1110
1111         return ret;
1112 }
1113
1114 static int
1115 ice_vsi_config_qinq_stripping(struct ice_vsi *vsi, bool on)
1116 {
1117         struct ice_hw *hw = ICE_VSI_TO_HW(vsi);
1118         struct ice_vsi_ctx ctxt;
1119         uint8_t qinq_flags;
1120         int ret = 0;
1121
1122         /* Check if it has been already on or off */
1123         if (vsi->info.valid_sections &
1124                 rte_cpu_to_le_16(ICE_AQ_VSI_PROP_OUTER_TAG_VALID)) {
1125                 if (on) {
1126                         if ((vsi->info.outer_tag_flags &
1127                              ICE_AQ_VSI_OUTER_TAG_MODE_M) ==
1128                             ICE_AQ_VSI_OUTER_TAG_COPY)
1129                                 return 0; /* already on */
1130                 } else {
1131                         if ((vsi->info.outer_tag_flags &
1132                              ICE_AQ_VSI_OUTER_TAG_MODE_M) ==
1133                             ICE_AQ_VSI_OUTER_TAG_NOTHING)
1134                                 return 0; /* already off */
1135                 }
1136         }
1137
1138         if (on)
1139                 qinq_flags = ICE_AQ_VSI_OUTER_TAG_COPY;
1140         else
1141                 qinq_flags = ICE_AQ_VSI_OUTER_TAG_NOTHING;
1142         vsi->info.outer_tag_flags &= ~(ICE_AQ_VSI_OUTER_TAG_MODE_M);
1143         vsi->info.outer_tag_flags |= qinq_flags;
1144         /* use default vlan type 0x8100 */
1145         vsi->info.outer_tag_flags &= ~(ICE_AQ_VSI_OUTER_TAG_TYPE_M);
1146         vsi->info.outer_tag_flags |= ICE_DFLT_OUTER_TAG_TYPE <<
1147                                      ICE_AQ_VSI_OUTER_TAG_TYPE_S;
1148         (void)rte_memcpy(&ctxt.info, &vsi->info, sizeof(vsi->info));
1149         ctxt.info.valid_sections =
1150                         rte_cpu_to_le_16(ICE_AQ_VSI_PROP_OUTER_TAG_VALID);
1151         ctxt.vsi_num = vsi->vsi_id;
1152         ret = ice_update_vsi(hw, vsi->idx, &ctxt, NULL);
1153         if (ret) {
1154                 PMD_DRV_LOG(INFO,
1155                             "Update VSI failed to %s qinq stripping",
1156                             on ? "enable" : "disable");
1157                 return -EINVAL;
1158         }
1159
1160         vsi->info.valid_sections |=
1161                 rte_cpu_to_le_16(ICE_AQ_VSI_PROP_OUTER_TAG_VALID);
1162
1163         return ret;
1164 }
1165
1166 static int
1167 ice_vsi_config_double_vlan(struct ice_vsi *vsi, int on)
1168 {
1169         int ret;
1170
1171         ret = ice_vsi_config_qinq_stripping(vsi, on);
1172         if (ret)
1173                 PMD_DRV_LOG(ERR, "Fail to set qinq stripping - %d", ret);
1174
1175         ret = ice_vsi_config_qinq_insertion(vsi, on);
1176         if (ret)
1177                 PMD_DRV_LOG(ERR, "Fail to set qinq insertion - %d", ret);
1178
1179         return ret;
1180 }
1181
1182 /* Enable IRQ0 */
1183 static void
1184 ice_pf_enable_irq0(struct ice_hw *hw)
1185 {
1186         /* reset the registers */
1187         ICE_WRITE_REG(hw, PFINT_OICR_ENA, 0);
1188         ICE_READ_REG(hw, PFINT_OICR);
1189
1190 #ifdef ICE_LSE_SPT
1191         ICE_WRITE_REG(hw, PFINT_OICR_ENA,
1192                       (uint32_t)(PFINT_OICR_ENA_INT_ENA_M &
1193                                  (~PFINT_OICR_LINK_STAT_CHANGE_M)));
1194
1195         ICE_WRITE_REG(hw, PFINT_OICR_CTL,
1196                       (0 & PFINT_OICR_CTL_MSIX_INDX_M) |
1197                       ((0 << PFINT_OICR_CTL_ITR_INDX_S) &
1198                        PFINT_OICR_CTL_ITR_INDX_M) |
1199                       PFINT_OICR_CTL_CAUSE_ENA_M);
1200
1201         ICE_WRITE_REG(hw, PFINT_FW_CTL,
1202                       (0 & PFINT_FW_CTL_MSIX_INDX_M) |
1203                       ((0 << PFINT_FW_CTL_ITR_INDX_S) &
1204                        PFINT_FW_CTL_ITR_INDX_M) |
1205                       PFINT_FW_CTL_CAUSE_ENA_M);
1206 #else
1207         ICE_WRITE_REG(hw, PFINT_OICR_ENA, PFINT_OICR_ENA_INT_ENA_M);
1208 #endif
1209
1210         ICE_WRITE_REG(hw, GLINT_DYN_CTL(0),
1211                       GLINT_DYN_CTL_INTENA_M |
1212                       GLINT_DYN_CTL_CLEARPBA_M |
1213                       GLINT_DYN_CTL_ITR_INDX_M);
1214
1215         ice_flush(hw);
1216 }
1217
1218 /* Disable IRQ0 */
1219 static void
1220 ice_pf_disable_irq0(struct ice_hw *hw)
1221 {
1222         /* Disable all interrupt types */
1223         ICE_WRITE_REG(hw, GLINT_DYN_CTL(0), GLINT_DYN_CTL_WB_ON_ITR_M);
1224         ice_flush(hw);
1225 }
1226
1227 #ifdef ICE_LSE_SPT
1228 static void
1229 ice_handle_aq_msg(struct rte_eth_dev *dev)
1230 {
1231         struct ice_hw *hw = ICE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1232         struct ice_ctl_q_info *cq = &hw->adminq;
1233         struct ice_rq_event_info event;
1234         uint16_t pending, opcode;
1235         int ret;
1236
1237         event.buf_len = ICE_AQ_MAX_BUF_LEN;
1238         event.msg_buf = rte_zmalloc(NULL, event.buf_len, 0);
1239         if (!event.msg_buf) {
1240                 PMD_DRV_LOG(ERR, "Failed to allocate mem");
1241                 return;
1242         }
1243
1244         pending = 1;
1245         while (pending) {
1246                 ret = ice_clean_rq_elem(hw, cq, &event, &pending);
1247
1248                 if (ret != ICE_SUCCESS) {
1249                         PMD_DRV_LOG(INFO,
1250                                     "Failed to read msg from AdminQ, "
1251                                     "adminq_err: %u",
1252                                     hw->adminq.sq_last_status);
1253                         break;
1254                 }
1255                 opcode = rte_le_to_cpu_16(event.desc.opcode);
1256
1257                 switch (opcode) {
1258                 case ice_aqc_opc_get_link_status:
1259                         ret = ice_link_update(dev, 0);
1260                         if (!ret)
1261                                 _rte_eth_dev_callback_process
1262                                         (dev, RTE_ETH_EVENT_INTR_LSC, NULL);
1263                         break;
1264                 default:
1265                         PMD_DRV_LOG(DEBUG, "Request %u is not supported yet",
1266                                     opcode);
1267                         break;
1268                 }
1269         }
1270         rte_free(event.msg_buf);
1271 }
1272 #endif
1273
1274 /**
1275  * Interrupt handler triggered by NIC for handling
1276  * specific interrupt.
1277  *
1278  * @param handle
1279  *  Pointer to interrupt handle.
1280  * @param param
1281  *  The address of parameter (struct rte_eth_dev *) regsitered before.
1282  *
1283  * @return
1284  *  void
1285  */
1286 static void
1287 ice_interrupt_handler(void *param)
1288 {
1289         struct rte_eth_dev *dev = (struct rte_eth_dev *)param;
1290         struct ice_hw *hw = ICE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1291         uint32_t oicr;
1292         uint32_t reg;
1293         uint8_t pf_num;
1294         uint8_t event;
1295         uint16_t queue;
1296 #ifdef ICE_LSE_SPT
1297         uint32_t int_fw_ctl;
1298 #endif
1299
1300         /* Disable interrupt */
1301         ice_pf_disable_irq0(hw);
1302
1303         /* read out interrupt causes */
1304         oicr = ICE_READ_REG(hw, PFINT_OICR);
1305 #ifdef ICE_LSE_SPT
1306         int_fw_ctl = ICE_READ_REG(hw, PFINT_FW_CTL);
1307 #endif
1308
1309         /* No interrupt event indicated */
1310         if (!(oicr & PFINT_OICR_INTEVENT_M)) {
1311                 PMD_DRV_LOG(INFO, "No interrupt event");
1312                 goto done;
1313         }
1314
1315 #ifdef ICE_LSE_SPT
1316         if (int_fw_ctl & PFINT_FW_CTL_INTEVENT_M) {
1317                 PMD_DRV_LOG(INFO, "FW_CTL: link state change event");
1318                 ice_handle_aq_msg(dev);
1319         }
1320 #else
1321         if (oicr & PFINT_OICR_LINK_STAT_CHANGE_M) {
1322                 PMD_DRV_LOG(INFO, "OICR: link state change event");
1323                 ice_link_update(dev, 0);
1324         }
1325 #endif
1326
1327         if (oicr & PFINT_OICR_MAL_DETECT_M) {
1328                 PMD_DRV_LOG(WARNING, "OICR: MDD event");
1329                 reg = ICE_READ_REG(hw, GL_MDET_TX_PQM);
1330                 if (reg & GL_MDET_TX_PQM_VALID_M) {
1331                         pf_num = (reg & GL_MDET_TX_PQM_PF_NUM_M) >>
1332                                  GL_MDET_TX_PQM_PF_NUM_S;
1333                         event = (reg & GL_MDET_TX_PQM_MAL_TYPE_M) >>
1334                                 GL_MDET_TX_PQM_MAL_TYPE_S;
1335                         queue = (reg & GL_MDET_TX_PQM_QNUM_M) >>
1336                                 GL_MDET_TX_PQM_QNUM_S;
1337
1338                         PMD_DRV_LOG(WARNING, "Malicious Driver Detection event "
1339                                     "%d by PQM on TX queue %d PF# %d",
1340                                     event, queue, pf_num);
1341                 }
1342
1343                 reg = ICE_READ_REG(hw, GL_MDET_TX_TCLAN);
1344                 if (reg & GL_MDET_TX_TCLAN_VALID_M) {
1345                         pf_num = (reg & GL_MDET_TX_TCLAN_PF_NUM_M) >>
1346                                  GL_MDET_TX_TCLAN_PF_NUM_S;
1347                         event = (reg & GL_MDET_TX_TCLAN_MAL_TYPE_M) >>
1348                                 GL_MDET_TX_TCLAN_MAL_TYPE_S;
1349                         queue = (reg & GL_MDET_TX_TCLAN_QNUM_M) >>
1350                                 GL_MDET_TX_TCLAN_QNUM_S;
1351
1352                         PMD_DRV_LOG(WARNING, "Malicious Driver Detection event "
1353                                     "%d by TCLAN on TX queue %d PF# %d",
1354                                     event, queue, pf_num);
1355                 }
1356         }
1357 done:
1358         /* Enable interrupt */
1359         ice_pf_enable_irq0(hw);
1360         rte_intr_ack(dev->intr_handle);
1361 }
1362
1363 /*  Initialize SW parameters of PF */
1364 static int
1365 ice_pf_sw_init(struct rte_eth_dev *dev)
1366 {
1367         struct ice_adapter *ad =
1368                         ICE_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
1369         struct ice_pf *pf = ICE_DEV_PRIVATE_TO_PF(dev->data->dev_private);
1370         struct ice_hw *hw = ICE_PF_TO_HW(pf);
1371
1372         pf->lan_nb_qp_max =
1373                 (uint16_t)RTE_MIN(hw->func_caps.common_cap.num_txq,
1374                                   hw->func_caps.common_cap.num_rxq);
1375
1376         pf->lan_nb_qps = pf->lan_nb_qp_max;
1377
1378         if (ice_proto_xtr_support(hw))
1379                 pf->proto_xtr = rte_zmalloc(NULL, pf->lan_nb_qps, 0);
1380
1381         if (pf->proto_xtr != NULL)
1382                 rte_memcpy(pf->proto_xtr, ad->devargs.proto_xtr,
1383                            RTE_MIN((size_t)pf->lan_nb_qps,
1384                                    sizeof(ad->devargs.proto_xtr)));
1385         else
1386                 PMD_DRV_LOG(NOTICE, "Protocol extraction is disabled");
1387
1388         return 0;
1389 }
1390
1391 static struct ice_vsi *
1392 ice_setup_vsi(struct ice_pf *pf, enum ice_vsi_type type)
1393 {
1394         struct ice_hw *hw = ICE_PF_TO_HW(pf);
1395         struct ice_vsi *vsi = NULL;
1396         struct ice_vsi_ctx vsi_ctx;
1397         int ret;
1398         struct rte_ether_addr broadcast = {
1399                 .addr_bytes = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff} };
1400         struct rte_ether_addr mac_addr;
1401         uint16_t max_txqs[ICE_MAX_TRAFFIC_CLASS] = { 0 };
1402         uint8_t tc_bitmap = 0x1;
1403
1404         /* hw->num_lports = 1 in NIC mode */
1405         vsi = rte_zmalloc(NULL, sizeof(struct ice_vsi), 0);
1406         if (!vsi)
1407                 return NULL;
1408
1409         vsi->idx = pf->next_vsi_idx;
1410         pf->next_vsi_idx++;
1411         vsi->type = type;
1412         vsi->adapter = ICE_PF_TO_ADAPTER(pf);
1413         vsi->max_macaddrs = ICE_NUM_MACADDR_MAX;
1414         vsi->vlan_anti_spoof_on = 0;
1415         vsi->vlan_filter_on = 1;
1416         TAILQ_INIT(&vsi->mac_list);
1417         TAILQ_INIT(&vsi->vlan_list);
1418
1419         /* Be sync with ETH_RSS_RETA_SIZE_x maximum value definition */
1420         pf->hash_lut_size = hw->func_caps.common_cap.rss_table_size >
1421                         ETH_RSS_RETA_SIZE_512 ? ETH_RSS_RETA_SIZE_512 :
1422                         hw->func_caps.common_cap.rss_table_size;
1423         pf->flags |= ICE_FLAG_RSS_AQ_CAPABLE;
1424
1425         memset(&vsi_ctx, 0, sizeof(vsi_ctx));
1426         /* base_queue in used in queue mapping of VSI add/update command.
1427          * Suppose vsi->base_queue is 0 now, don't consider SRIOV, VMDQ
1428          * cases in the first stage. Only Main VSI.
1429          */
1430         vsi->base_queue = 0;
1431         switch (type) {
1432         case ICE_VSI_PF:
1433                 vsi->nb_qps = pf->lan_nb_qps;
1434                 ice_vsi_config_default_rss(&vsi_ctx.info);
1435                 vsi_ctx.alloc_from_pool = true;
1436                 vsi_ctx.flags = ICE_AQ_VSI_TYPE_PF;
1437                 /* switch_id is queried by get_switch_config aq, which is done
1438                  * by ice_init_hw
1439                  */
1440                 vsi_ctx.info.sw_id = hw->port_info->sw_id;
1441                 vsi_ctx.info.sw_flags2 = ICE_AQ_VSI_SW_FLAG_LAN_ENA;
1442                 /* Allow all untagged or tagged packets */
1443                 vsi_ctx.info.vlan_flags = ICE_AQ_VSI_VLAN_MODE_ALL;
1444                 vsi_ctx.info.vlan_flags |= ICE_AQ_VSI_VLAN_EMOD_NOTHING;
1445                 vsi_ctx.info.q_opt_rss = ICE_AQ_VSI_Q_OPT_RSS_LUT_PF |
1446                                          ICE_AQ_VSI_Q_OPT_RSS_TPLZ;
1447                 /* Enable VLAN/UP trip */
1448                 ret = ice_vsi_config_tc_queue_mapping(vsi,
1449                                                       &vsi_ctx.info,
1450                                                       ICE_DEFAULT_TCMAP);
1451                 if (ret) {
1452                         PMD_INIT_LOG(ERR,
1453                                      "tc queue mapping with vsi failed, "
1454                                      "err = %d",
1455                                      ret);
1456                         goto fail_mem;
1457                 }
1458
1459                 break;
1460         default:
1461                 /* for other types of VSI */
1462                 PMD_INIT_LOG(ERR, "other types of VSI not supported");
1463                 goto fail_mem;
1464         }
1465
1466         /* VF has MSIX interrupt in VF range, don't allocate here */
1467         if (type == ICE_VSI_PF) {
1468                 ret = ice_res_pool_alloc(&pf->msix_pool,
1469                                          RTE_MIN(vsi->nb_qps,
1470                                                  RTE_MAX_RXTX_INTR_VEC_ID));
1471                 if (ret < 0) {
1472                         PMD_INIT_LOG(ERR, "VSI MAIN %d get heap failed %d",
1473                                      vsi->vsi_id, ret);
1474                 }
1475                 vsi->msix_intr = ret;
1476                 vsi->nb_msix = RTE_MIN(vsi->nb_qps, RTE_MAX_RXTX_INTR_VEC_ID);
1477         } else {
1478                 vsi->msix_intr = 0;
1479                 vsi->nb_msix = 0;
1480         }
1481         ret = ice_add_vsi(hw, vsi->idx, &vsi_ctx, NULL);
1482         if (ret != ICE_SUCCESS) {
1483                 PMD_INIT_LOG(ERR, "add vsi failed, err = %d", ret);
1484                 goto fail_mem;
1485         }
1486         /* store vsi information is SW structure */
1487         vsi->vsi_id = vsi_ctx.vsi_num;
1488         vsi->info = vsi_ctx.info;
1489         pf->vsis_allocated = vsi_ctx.vsis_allocd;
1490         pf->vsis_unallocated = vsi_ctx.vsis_unallocated;
1491
1492         /* MAC configuration */
1493         rte_memcpy(pf->dev_addr.addr_bytes,
1494                    hw->port_info->mac.perm_addr,
1495                    ETH_ADDR_LEN);
1496
1497         rte_memcpy(&mac_addr, &pf->dev_addr, RTE_ETHER_ADDR_LEN);
1498         ret = ice_add_mac_filter(vsi, &mac_addr);
1499         if (ret != ICE_SUCCESS)
1500                 PMD_INIT_LOG(ERR, "Failed to add dflt MAC filter");
1501
1502         rte_memcpy(&mac_addr, &broadcast, RTE_ETHER_ADDR_LEN);
1503         ret = ice_add_mac_filter(vsi, &mac_addr);
1504         if (ret != ICE_SUCCESS)
1505                 PMD_INIT_LOG(ERR, "Failed to add MAC filter");
1506
1507         /* At the beginning, only TC0. */
1508         /* What we need here is the maximam number of the TX queues.
1509          * Currently vsi->nb_qps means it.
1510          * Correct it if any change.
1511          */
1512         max_txqs[0] = vsi->nb_qps;
1513         ret = ice_cfg_vsi_lan(hw->port_info, vsi->idx,
1514                               tc_bitmap, max_txqs);
1515         if (ret != ICE_SUCCESS)
1516                 PMD_INIT_LOG(ERR, "Failed to config vsi sched");
1517
1518         return vsi;
1519 fail_mem:
1520         rte_free(vsi);
1521         pf->next_vsi_idx--;
1522         return NULL;
1523 }
1524
1525 static int
1526 ice_send_driver_ver(struct ice_hw *hw)
1527 {
1528         struct ice_driver_ver dv;
1529
1530         /* we don't have driver version use 0 for dummy */
1531         dv.major_ver = 0;
1532         dv.minor_ver = 0;
1533         dv.build_ver = 0;
1534         dv.subbuild_ver = 0;
1535         strncpy((char *)dv.driver_string, "dpdk", sizeof(dv.driver_string));
1536
1537         return ice_aq_send_driver_ver(hw, &dv, NULL);
1538 }
1539
1540 static int
1541 ice_pf_setup(struct ice_pf *pf)
1542 {
1543         struct ice_vsi *vsi;
1544
1545         /* Clear all stats counters */
1546         pf->offset_loaded = FALSE;
1547         memset(&pf->stats, 0, sizeof(struct ice_hw_port_stats));
1548         memset(&pf->stats_offset, 0, sizeof(struct ice_hw_port_stats));
1549         memset(&pf->internal_stats, 0, sizeof(struct ice_eth_stats));
1550         memset(&pf->internal_stats_offset, 0, sizeof(struct ice_eth_stats));
1551
1552         vsi = ice_setup_vsi(pf, ICE_VSI_PF);
1553         if (!vsi) {
1554                 PMD_INIT_LOG(ERR, "Failed to add vsi for PF");
1555                 return -EINVAL;
1556         }
1557
1558         pf->main_vsi = vsi;
1559
1560         return 0;
1561 }
1562
1563 /* PCIe configuration space setting */
1564 #define PCI_CFG_SPACE_SIZE          256
1565 #define PCI_CFG_SPACE_EXP_SIZE      4096
1566 #define PCI_EXT_CAP_ID(header)      (int)((header) & 0x0000ffff)
1567 #define PCI_EXT_CAP_NEXT(header)    (((header) >> 20) & 0xffc)
1568 #define PCI_EXT_CAP_ID_DSN          0x03
1569
1570 static int
1571 ice_pci_find_next_ext_capability(struct rte_pci_device *dev, int cap)
1572 {
1573         uint32_t header;
1574         int ttl;
1575         int pos = PCI_CFG_SPACE_SIZE;
1576
1577         /* minimum 8 bytes per capability */
1578         ttl = (PCI_CFG_SPACE_EXP_SIZE - PCI_CFG_SPACE_SIZE) / 8;
1579
1580         if (rte_pci_read_config(dev, &header, 4, pos) < 0) {
1581                 PMD_INIT_LOG(ERR, "ice error reading extended capabilities\n");
1582                 return -1;
1583         }
1584
1585         /*
1586          * If we have no capabilities, this is indicated by cap ID,
1587          * cap version and next pointer all being 0.
1588          */
1589         if (header == 0)
1590                 return 0;
1591
1592         while (ttl-- > 0) {
1593                 if (PCI_EXT_CAP_ID(header) == cap)
1594                         return pos;
1595
1596                 pos = PCI_EXT_CAP_NEXT(header);
1597
1598                 if (pos < PCI_CFG_SPACE_SIZE)
1599                         break;
1600
1601                 if (rte_pci_read_config(dev, &header, 4, pos) < 0) {
1602                         PMD_INIT_LOG(ERR, "ice error reading extended capabilities\n");
1603                         return -1;
1604                 }
1605         }
1606
1607         return 0;
1608 }
1609
1610 /*
1611  * Extract device serial number from PCIe Configuration Space and
1612  * determine the pkg file path according to the DSN.
1613  */
1614 static int
1615 ice_pkg_file_search_path(struct rte_pci_device *pci_dev, char *pkg_file)
1616 {
1617         int pos;
1618         char opt_ddp_filename[ICE_MAX_PKG_FILENAME_SIZE];
1619         uint32_t dsn_low, dsn_high;
1620         memset(opt_ddp_filename, 0, ICE_MAX_PKG_FILENAME_SIZE);
1621
1622         pos = ice_pci_find_next_ext_capability(pci_dev, PCI_EXT_CAP_ID_DSN);
1623
1624         if (pos) {
1625                 rte_pci_read_config(pci_dev, &dsn_low, 4, pos + 4);
1626                 rte_pci_read_config(pci_dev, &dsn_high, 4, pos + 8);
1627                 snprintf(opt_ddp_filename, ICE_MAX_PKG_FILENAME_SIZE,
1628                          "ice-%08x%08x.pkg", dsn_high, dsn_low);
1629         } else {
1630                 PMD_INIT_LOG(ERR, "Failed to read device serial number\n");
1631                 goto fail_dsn;
1632         }
1633
1634         strncpy(pkg_file, ICE_PKG_FILE_SEARCH_PATH_UPDATES,
1635                 ICE_MAX_PKG_FILENAME_SIZE);
1636         if (!access(strcat(pkg_file, opt_ddp_filename), 0))
1637                 return 0;
1638
1639         strncpy(pkg_file, ICE_PKG_FILE_SEARCH_PATH_DEFAULT,
1640                 ICE_MAX_PKG_FILENAME_SIZE);
1641         if (!access(strcat(pkg_file, opt_ddp_filename), 0))
1642                 return 0;
1643
1644 fail_dsn:
1645         strncpy(pkg_file, ICE_PKG_FILE_UPDATES, ICE_MAX_PKG_FILENAME_SIZE);
1646         if (!access(pkg_file, 0))
1647                 return 0;
1648         strncpy(pkg_file, ICE_PKG_FILE_DEFAULT, ICE_MAX_PKG_FILENAME_SIZE);
1649         return 0;
1650 }
1651
1652 static enum ice_pkg_type
1653 ice_load_pkg_type(struct ice_hw *hw)
1654 {
1655         enum ice_pkg_type package_type;
1656
1657         /* store the activated package type (OS default or Comms) */
1658         if (!strncmp((char *)hw->active_pkg_name, ICE_OS_DEFAULT_PKG_NAME,
1659                 ICE_PKG_NAME_SIZE))
1660                 package_type = ICE_PKG_TYPE_OS_DEFAULT;
1661         else if (!strncmp((char *)hw->active_pkg_name, ICE_COMMS_PKG_NAME,
1662                 ICE_PKG_NAME_SIZE))
1663                 package_type = ICE_PKG_TYPE_COMMS;
1664         else
1665                 package_type = ICE_PKG_TYPE_UNKNOWN;
1666
1667         PMD_INIT_LOG(NOTICE, "Active package is: %d.%d.%d.%d, %s",
1668                 hw->active_pkg_ver.major, hw->active_pkg_ver.minor,
1669                 hw->active_pkg_ver.update, hw->active_pkg_ver.draft,
1670                 hw->active_pkg_name);
1671
1672         return package_type;
1673 }
1674
1675 static int ice_load_pkg(struct rte_eth_dev *dev)
1676 {
1677         struct ice_hw *hw = ICE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1678         char pkg_file[ICE_MAX_PKG_FILENAME_SIZE];
1679         int err;
1680         uint8_t *buf;
1681         int buf_len;
1682         FILE *file;
1683         struct stat fstat;
1684         struct rte_pci_device *pci_dev = RTE_DEV_TO_PCI(dev->device);
1685         struct ice_adapter *ad =
1686                 ICE_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
1687
1688         ice_pkg_file_search_path(pci_dev, pkg_file);
1689
1690         file = fopen(pkg_file, "rb");
1691         if (!file)  {
1692                 PMD_INIT_LOG(ERR, "failed to open file: %s\n", pkg_file);
1693                 return -1;
1694         }
1695
1696         err = stat(pkg_file, &fstat);
1697         if (err) {
1698                 PMD_INIT_LOG(ERR, "failed to get file stats\n");
1699                 fclose(file);
1700                 return err;
1701         }
1702
1703         buf_len = fstat.st_size;
1704         buf = rte_malloc(NULL, buf_len, 0);
1705
1706         if (!buf) {
1707                 PMD_INIT_LOG(ERR, "failed to allocate buf of size %d for package\n",
1708                                 buf_len);
1709                 fclose(file);
1710                 return -1;
1711         }
1712
1713         err = fread(buf, buf_len, 1, file);
1714         if (err != 1) {
1715                 PMD_INIT_LOG(ERR, "failed to read package data\n");
1716                 fclose(file);
1717                 err = -1;
1718                 goto fail_exit;
1719         }
1720
1721         fclose(file);
1722
1723         err = ice_copy_and_init_pkg(hw, buf, buf_len);
1724         if (err) {
1725                 PMD_INIT_LOG(ERR, "ice_copy_and_init_hw failed: %d\n", err);
1726                 goto fail_exit;
1727         }
1728
1729         /* store the loaded pkg type info */
1730         ad->active_pkg_type = ice_load_pkg_type(hw);
1731
1732         err = ice_init_hw_tbls(hw);
1733         if (err) {
1734                 PMD_INIT_LOG(ERR, "ice_init_hw_tbls failed: %d\n", err);
1735                 goto fail_init_tbls;
1736         }
1737
1738         return 0;
1739
1740 fail_init_tbls:
1741         rte_free(hw->pkg_copy);
1742 fail_exit:
1743         rte_free(buf);
1744         return err;
1745 }
1746
1747 static void
1748 ice_base_queue_get(struct ice_pf *pf)
1749 {
1750         uint32_t reg;
1751         struct ice_hw *hw = ICE_PF_TO_HW(pf);
1752
1753         reg = ICE_READ_REG(hw, PFLAN_RX_QALLOC);
1754         if (reg & PFLAN_RX_QALLOC_VALID_M) {
1755                 pf->base_queue = reg & PFLAN_RX_QALLOC_FIRSTQ_M;
1756         } else {
1757                 PMD_INIT_LOG(WARNING, "Failed to get Rx base queue"
1758                                         " index");
1759         }
1760 }
1761
1762 static int
1763 parse_bool(const char *key, const char *value, void *args)
1764 {
1765         int *i = (int *)args;
1766         char *end;
1767         int num;
1768
1769         num = strtoul(value, &end, 10);
1770
1771         if (num != 0 && num != 1) {
1772                 PMD_DRV_LOG(WARNING, "invalid value:\"%s\" for key:\"%s\", "
1773                         "value must be 0 or 1",
1774                         value, key);
1775                 return -1;
1776         }
1777
1778         *i = num;
1779         return 0;
1780 }
1781
1782 static int ice_parse_devargs(struct rte_eth_dev *dev)
1783 {
1784         struct ice_adapter *ad =
1785                 ICE_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
1786         struct rte_devargs *devargs = dev->device->devargs;
1787         struct rte_kvargs *kvlist;
1788         int ret;
1789
1790         if (devargs == NULL)
1791                 return 0;
1792
1793         kvlist = rte_kvargs_parse(devargs->args, ice_valid_args);
1794         if (kvlist == NULL) {
1795                 PMD_INIT_LOG(ERR, "Invalid kvargs key\n");
1796                 return -EINVAL;
1797         }
1798
1799         memset(ad->devargs.proto_xtr, PROTO_XTR_NONE,
1800                sizeof(ad->devargs.proto_xtr));
1801
1802         ret = rte_kvargs_process(kvlist, ICE_PROTO_XTR_ARG,
1803                                  &handle_proto_xtr_arg, &ad->devargs);
1804         if (ret)
1805                 goto bail;
1806
1807         ret = rte_kvargs_process(kvlist, ICE_SAFE_MODE_SUPPORT_ARG,
1808                                  &parse_bool, &ad->devargs.safe_mode_support);
1809
1810 bail:
1811         rte_kvargs_free(kvlist);
1812         return ret;
1813 }
1814
1815 /* Forward LLDP packets to default VSI by set switch rules */
1816 static int
1817 ice_vsi_config_sw_lldp(struct ice_vsi *vsi,  bool on)
1818 {
1819         struct ice_hw *hw = ICE_VSI_TO_HW(vsi);
1820         struct ice_fltr_list_entry *s_list_itr = NULL;
1821         struct LIST_HEAD_TYPE list_head;
1822         int ret = 0;
1823
1824         INIT_LIST_HEAD(&list_head);
1825
1826         s_list_itr = (struct ice_fltr_list_entry *)
1827                         ice_malloc(hw, sizeof(*s_list_itr));
1828         if (!s_list_itr)
1829                 return -ENOMEM;
1830         s_list_itr->fltr_info.lkup_type = ICE_SW_LKUP_ETHERTYPE;
1831         s_list_itr->fltr_info.vsi_handle = vsi->idx;
1832         s_list_itr->fltr_info.l_data.ethertype_mac.ethertype =
1833                         RTE_ETHER_TYPE_LLDP;
1834         s_list_itr->fltr_info.fltr_act = ICE_FWD_TO_VSI;
1835         s_list_itr->fltr_info.flag = ICE_FLTR_RX;
1836         s_list_itr->fltr_info.src_id = ICE_SRC_ID_LPORT;
1837         LIST_ADD(&s_list_itr->list_entry, &list_head);
1838         if (on)
1839                 ret = ice_add_eth_mac(hw, &list_head);
1840         else
1841                 ret = ice_remove_eth_mac(hw, &list_head);
1842
1843         rte_free(s_list_itr);
1844         return ret;
1845 }
1846
1847 static int
1848 ice_dev_init(struct rte_eth_dev *dev)
1849 {
1850         struct rte_pci_device *pci_dev;
1851         struct rte_intr_handle *intr_handle;
1852         struct ice_hw *hw = ICE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1853         struct ice_pf *pf = ICE_DEV_PRIVATE_TO_PF(dev->data->dev_private);
1854         struct ice_adapter *ad =
1855                 ICE_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
1856         struct ice_vsi *vsi;
1857         int ret;
1858
1859         dev->dev_ops = &ice_eth_dev_ops;
1860         dev->rx_pkt_burst = ice_recv_pkts;
1861         dev->tx_pkt_burst = ice_xmit_pkts;
1862         dev->tx_pkt_prepare = ice_prep_pkts;
1863
1864         /* for secondary processes, we don't initialise any further as primary
1865          * has already done this work.
1866          */
1867         if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
1868                 ice_set_rx_function(dev);
1869                 ice_set_tx_function(dev);
1870                 return 0;
1871         }
1872
1873         ice_set_default_ptype_table(dev);
1874         pci_dev = RTE_DEV_TO_PCI(dev->device);
1875         intr_handle = &pci_dev->intr_handle;
1876
1877         pf->adapter = ICE_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
1878         pf->adapter->eth_dev = dev;
1879         pf->dev_data = dev->data;
1880         hw->back = pf->adapter;
1881         hw->hw_addr = (uint8_t *)pci_dev->mem_resource[0].addr;
1882         hw->vendor_id = pci_dev->id.vendor_id;
1883         hw->device_id = pci_dev->id.device_id;
1884         hw->subsystem_vendor_id = pci_dev->id.subsystem_vendor_id;
1885         hw->subsystem_device_id = pci_dev->id.subsystem_device_id;
1886         hw->bus.device = pci_dev->addr.devid;
1887         hw->bus.func = pci_dev->addr.function;
1888
1889         ret = ice_parse_devargs(dev);
1890         if (ret) {
1891                 PMD_INIT_LOG(ERR, "Failed to parse devargs");
1892                 return -EINVAL;
1893         }
1894
1895         ice_init_controlq_parameter(hw);
1896
1897         ret = ice_init_hw(hw);
1898         if (ret) {
1899                 PMD_INIT_LOG(ERR, "Failed to initialize HW");
1900                 return -EINVAL;
1901         }
1902
1903         ret = ice_load_pkg(dev);
1904         if (ret) {
1905                 if (ad->devargs.safe_mode_support == 0) {
1906                         PMD_INIT_LOG(ERR, "Failed to load the DDP package,"
1907                                         "Use safe-mode-support=1 to enter Safe Mode");
1908                         return ret;
1909                 }
1910
1911                 PMD_INIT_LOG(WARNING, "Failed to load the DDP package,"
1912                                         "Entering Safe Mode");
1913                 ad->is_safe_mode = 1;
1914         }
1915
1916         PMD_INIT_LOG(INFO, "FW %d.%d.%05d API %d.%d",
1917                      hw->fw_maj_ver, hw->fw_min_ver, hw->fw_build,
1918                      hw->api_maj_ver, hw->api_min_ver);
1919
1920         ice_pf_sw_init(dev);
1921         ret = ice_init_mac_address(dev);
1922         if (ret) {
1923                 PMD_INIT_LOG(ERR, "Failed to initialize mac address");
1924                 goto err_init_mac;
1925         }
1926
1927         ret = ice_res_pool_init(&pf->msix_pool, 1,
1928                                 hw->func_caps.common_cap.num_msix_vectors - 1);
1929         if (ret) {
1930                 PMD_INIT_LOG(ERR, "Failed to init MSIX pool");
1931                 goto err_msix_pool_init;
1932         }
1933
1934         ret = ice_pf_setup(pf);
1935         if (ret) {
1936                 PMD_INIT_LOG(ERR, "Failed to setup PF");
1937                 goto err_pf_setup;
1938         }
1939
1940         ret = ice_send_driver_ver(hw);
1941         if (ret) {
1942                 PMD_INIT_LOG(ERR, "Failed to send driver version");
1943                 goto err_pf_setup;
1944         }
1945
1946         vsi = pf->main_vsi;
1947
1948         /* Disable double vlan by default */
1949         ice_vsi_config_double_vlan(vsi, FALSE);
1950
1951         ret = ice_aq_stop_lldp(hw, TRUE, FALSE, NULL);
1952         if (ret != ICE_SUCCESS)
1953                 PMD_INIT_LOG(DEBUG, "lldp has already stopped\n");
1954         ret = ice_init_dcb(hw, TRUE);
1955         if (ret != ICE_SUCCESS)
1956                 PMD_INIT_LOG(DEBUG, "Failed to init DCB\n");
1957         /* Forward LLDP packets to default VSI */
1958         ret = ice_vsi_config_sw_lldp(vsi, TRUE);
1959         if (ret != ICE_SUCCESS)
1960                 PMD_INIT_LOG(DEBUG, "Failed to cfg lldp\n");
1961         /* register callback func to eal lib */
1962         rte_intr_callback_register(intr_handle,
1963                                    ice_interrupt_handler, dev);
1964
1965         ice_pf_enable_irq0(hw);
1966
1967         /* enable uio intr after callback register */
1968         rte_intr_enable(intr_handle);
1969
1970         /* get base queue pairs index  in the device */
1971         ice_base_queue_get(pf);
1972
1973         TAILQ_INIT(&pf->flow_list);
1974
1975         return 0;
1976
1977 err_pf_setup:
1978         ice_res_pool_destroy(&pf->msix_pool);
1979 err_msix_pool_init:
1980         rte_free(dev->data->mac_addrs);
1981         dev->data->mac_addrs = NULL;
1982 err_init_mac:
1983         ice_sched_cleanup_all(hw);
1984         rte_free(hw->port_info);
1985         ice_shutdown_all_ctrlq(hw);
1986         rte_free(pf->proto_xtr);
1987
1988         return ret;
1989 }
1990
1991 static int
1992 ice_release_vsi(struct ice_vsi *vsi)
1993 {
1994         struct ice_hw *hw;
1995         struct ice_vsi_ctx vsi_ctx;
1996         enum ice_status ret;
1997
1998         if (!vsi)
1999                 return 0;
2000
2001         hw = ICE_VSI_TO_HW(vsi);
2002
2003         ice_remove_all_mac_vlan_filters(vsi);
2004
2005         memset(&vsi_ctx, 0, sizeof(vsi_ctx));
2006
2007         vsi_ctx.vsi_num = vsi->vsi_id;
2008         vsi_ctx.info = vsi->info;
2009         ret = ice_free_vsi(hw, vsi->idx, &vsi_ctx, false, NULL);
2010         if (ret != ICE_SUCCESS) {
2011                 PMD_INIT_LOG(ERR, "Failed to free vsi by aq, %u", vsi->vsi_id);
2012                 rte_free(vsi);
2013                 return -1;
2014         }
2015
2016         rte_free(vsi);
2017         return 0;
2018 }
2019
2020 static void
2021 ice_vsi_disable_queues_intr(struct ice_vsi *vsi)
2022 {
2023         struct rte_eth_dev *dev = vsi->adapter->eth_dev;
2024         struct rte_pci_device *pci_dev = ICE_DEV_TO_PCI(dev);
2025         struct rte_intr_handle *intr_handle = &pci_dev->intr_handle;
2026         struct ice_hw *hw = ICE_VSI_TO_HW(vsi);
2027         uint16_t msix_intr, i;
2028
2029         /* disable interrupt and also clear all the exist config */
2030         for (i = 0; i < vsi->nb_qps; i++) {
2031                 ICE_WRITE_REG(hw, QINT_TQCTL(vsi->base_queue + i), 0);
2032                 ICE_WRITE_REG(hw, QINT_RQCTL(vsi->base_queue + i), 0);
2033                 rte_wmb();
2034         }
2035
2036         if (rte_intr_allow_others(intr_handle))
2037                 /* vfio-pci */
2038                 for (i = 0; i < vsi->nb_msix; i++) {
2039                         msix_intr = vsi->msix_intr + i;
2040                         ICE_WRITE_REG(hw, GLINT_DYN_CTL(msix_intr),
2041                                       GLINT_DYN_CTL_WB_ON_ITR_M);
2042                 }
2043         else
2044                 /* igb_uio */
2045                 ICE_WRITE_REG(hw, GLINT_DYN_CTL(0), GLINT_DYN_CTL_WB_ON_ITR_M);
2046 }
2047
2048 static void
2049 ice_dev_stop(struct rte_eth_dev *dev)
2050 {
2051         struct rte_eth_dev_data *data = dev->data;
2052         struct ice_pf *pf = ICE_DEV_PRIVATE_TO_PF(dev->data->dev_private);
2053         struct ice_vsi *main_vsi = pf->main_vsi;
2054         struct rte_pci_device *pci_dev = ICE_DEV_TO_PCI(dev);
2055         struct rte_intr_handle *intr_handle = &pci_dev->intr_handle;
2056         uint16_t i;
2057
2058         /* avoid stopping again */
2059         if (pf->adapter_stopped)
2060                 return;
2061
2062         /* stop and clear all Rx queues */
2063         for (i = 0; i < data->nb_rx_queues; i++)
2064                 ice_rx_queue_stop(dev, i);
2065
2066         /* stop and clear all Tx queues */
2067         for (i = 0; i < data->nb_tx_queues; i++)
2068                 ice_tx_queue_stop(dev, i);
2069
2070         /* disable all queue interrupts */
2071         ice_vsi_disable_queues_intr(main_vsi);
2072
2073         /* Clear all queues and release mbufs */
2074         ice_clear_queues(dev);
2075
2076         ice_dev_set_link_down(dev);
2077
2078         /* Clean datapath event and queue/vec mapping */
2079         rte_intr_efd_disable(intr_handle);
2080         if (intr_handle->intr_vec) {
2081                 rte_free(intr_handle->intr_vec);
2082                 intr_handle->intr_vec = NULL;
2083         }
2084
2085         pf->adapter_stopped = true;
2086 }
2087
2088 static void
2089 ice_dev_close(struct rte_eth_dev *dev)
2090 {
2091         struct ice_pf *pf = ICE_DEV_PRIVATE_TO_PF(dev->data->dev_private);
2092         struct ice_hw *hw = ICE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2093
2094         /* Since stop will make link down, then the link event will be
2095          * triggered, disable the irq firstly to avoid the port_infoe etc
2096          * resources deallocation causing the interrupt service thread
2097          * crash.
2098          */
2099         ice_pf_disable_irq0(hw);
2100
2101         ice_dev_stop(dev);
2102
2103         /* release all queue resource */
2104         ice_free_queues(dev);
2105
2106         ice_res_pool_destroy(&pf->msix_pool);
2107         ice_release_vsi(pf->main_vsi);
2108         ice_sched_cleanup_all(hw);
2109         rte_free(hw->port_info);
2110         hw->port_info = NULL;
2111         ice_shutdown_all_ctrlq(hw);
2112         rte_free(pf->proto_xtr);
2113         pf->proto_xtr = NULL;
2114 }
2115
2116 static int
2117 ice_dev_uninit(struct rte_eth_dev *dev)
2118 {
2119         struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
2120         struct rte_intr_handle *intr_handle = &pci_dev->intr_handle;
2121         struct ice_pf *pf = ICE_DEV_PRIVATE_TO_PF(dev->data->dev_private);
2122         struct rte_flow *p_flow;
2123
2124         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
2125                 return 0;
2126
2127         ice_dev_close(dev);
2128
2129         dev->dev_ops = NULL;
2130         dev->rx_pkt_burst = NULL;
2131         dev->tx_pkt_burst = NULL;
2132
2133         rte_free(dev->data->mac_addrs);
2134         dev->data->mac_addrs = NULL;
2135
2136         /* disable uio intr before callback unregister */
2137         rte_intr_disable(intr_handle);
2138
2139         /* unregister callback func from eal lib */
2140         rte_intr_callback_unregister(intr_handle,
2141                                      ice_interrupt_handler, dev);
2142
2143         /* Remove all flows */
2144         while ((p_flow = TAILQ_FIRST(&pf->flow_list))) {
2145                 TAILQ_REMOVE(&pf->flow_list, p_flow, node);
2146                 ice_free_switch_filter_rule(p_flow->rule);
2147                 rte_free(p_flow);
2148         }
2149
2150         return 0;
2151 }
2152
2153 static int
2154 ice_dev_configure(struct rte_eth_dev *dev)
2155 {
2156         struct ice_adapter *ad =
2157                 ICE_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
2158
2159         /* Initialize to TRUE. If any of Rx queues doesn't meet the
2160          * bulk allocation or vector Rx preconditions we will reset it.
2161          */
2162         ad->rx_bulk_alloc_allowed = true;
2163         ad->tx_simple_allowed = true;
2164
2165         return 0;
2166 }
2167
2168 static int ice_init_rss(struct ice_pf *pf)
2169 {
2170         struct ice_hw *hw = ICE_PF_TO_HW(pf);
2171         struct ice_vsi *vsi = pf->main_vsi;
2172         struct rte_eth_dev *dev = pf->adapter->eth_dev;
2173         struct rte_eth_rss_conf *rss_conf;
2174         struct ice_aqc_get_set_rss_keys key;
2175         uint16_t i, nb_q;
2176         int ret = 0;
2177         bool is_safe_mode = pf->adapter->is_safe_mode;
2178
2179         rss_conf = &dev->data->dev_conf.rx_adv_conf.rss_conf;
2180         nb_q = dev->data->nb_rx_queues;
2181         vsi->rss_key_size = ICE_AQC_GET_SET_RSS_KEY_DATA_RSS_KEY_SIZE;
2182         vsi->rss_lut_size = pf->hash_lut_size;
2183
2184         if (is_safe_mode) {
2185                 PMD_DRV_LOG(WARNING, "RSS is not supported in safe mode\n");
2186                 return 0;
2187         }
2188
2189         if (!vsi->rss_key)
2190                 vsi->rss_key = rte_zmalloc(NULL,
2191                                            vsi->rss_key_size, 0);
2192         if (!vsi->rss_lut)
2193                 vsi->rss_lut = rte_zmalloc(NULL,
2194                                            vsi->rss_lut_size, 0);
2195
2196         /* configure RSS key */
2197         if (!rss_conf->rss_key) {
2198                 /* Calculate the default hash key */
2199                 for (i = 0; i <= vsi->rss_key_size; i++)
2200                         vsi->rss_key[i] = (uint8_t)rte_rand();
2201         } else {
2202                 rte_memcpy(vsi->rss_key, rss_conf->rss_key,
2203                            RTE_MIN(rss_conf->rss_key_len,
2204                                    vsi->rss_key_size));
2205         }
2206         rte_memcpy(key.standard_rss_key, vsi->rss_key, vsi->rss_key_size);
2207         ret = ice_aq_set_rss_key(hw, vsi->idx, &key);
2208         if (ret)
2209                 return -EINVAL;
2210
2211         /* init RSS LUT table */
2212         for (i = 0; i < vsi->rss_lut_size; i++)
2213                 vsi->rss_lut[i] = i % nb_q;
2214
2215         ret = ice_aq_set_rss_lut(hw, vsi->idx,
2216                                  ICE_AQC_GSET_RSS_LUT_TABLE_TYPE_PF,
2217                                  vsi->rss_lut, vsi->rss_lut_size);
2218         if (ret)
2219                 return -EINVAL;
2220
2221         /* configure RSS for IPv4 with input set IPv4 src/dst */
2222         ret = ice_add_rss_cfg(hw, vsi->idx, ICE_FLOW_HASH_IPV4,
2223                               ICE_FLOW_SEG_HDR_IPV4, 0);
2224         if (ret)
2225                 PMD_DRV_LOG(ERR, "%s IPV4 rss flow fail %d", __func__, ret);
2226
2227         /* configure RSS for IPv6 with input set IPv6 src/dst */
2228         ret = ice_add_rss_cfg(hw, vsi->idx, ICE_FLOW_HASH_IPV6,
2229                               ICE_FLOW_SEG_HDR_IPV6, 0);
2230         if (ret)
2231                 PMD_DRV_LOG(ERR, "%s IPV6 rss flow fail %d", __func__, ret);
2232
2233         /* configure RSS for tcp6 with input set IPv6 src/dst, TCP src/dst */
2234         ret = ice_add_rss_cfg(hw, vsi->idx, ICE_HASH_TCP_IPV6,
2235                               ICE_FLOW_SEG_HDR_TCP | ICE_FLOW_SEG_HDR_IPV6, 0);
2236         if (ret)
2237                 PMD_DRV_LOG(ERR, "%s TCP_IPV6 rss flow fail %d", __func__, ret);
2238
2239         /* configure RSS for udp6 with input set IPv6 src/dst, UDP src/dst */
2240         ret = ice_add_rss_cfg(hw, vsi->idx, ICE_HASH_UDP_IPV6,
2241                               ICE_FLOW_SEG_HDR_UDP | ICE_FLOW_SEG_HDR_IPV6, 0);
2242         if (ret)
2243                 PMD_DRV_LOG(ERR, "%s UDP_IPV6 rss flow fail %d", __func__, ret);
2244
2245         /* configure RSS for sctp6 with input set IPv6 src/dst */
2246         ret = ice_add_rss_cfg(hw, vsi->idx, ICE_FLOW_HASH_IPV6,
2247                               ICE_FLOW_SEG_HDR_SCTP | ICE_FLOW_SEG_HDR_IPV6, 0);
2248         if (ret)
2249                 PMD_DRV_LOG(ERR, "%s SCTP_IPV6 rss flow fail %d",
2250                                 __func__, ret);
2251
2252         /* configure RSS for tcp4 with input set IP src/dst, TCP src/dst */
2253         ret = ice_add_rss_cfg(hw, vsi->idx, ICE_HASH_TCP_IPV4,
2254                               ICE_FLOW_SEG_HDR_TCP | ICE_FLOW_SEG_HDR_IPV4, 0);
2255         if (ret)
2256                 PMD_DRV_LOG(ERR, "%s TCP_IPV4 rss flow fail %d", __func__, ret);
2257
2258         /* configure RSS for udp4 with input set IP src/dst, UDP src/dst */
2259         ret = ice_add_rss_cfg(hw, vsi->idx, ICE_HASH_UDP_IPV4,
2260                               ICE_FLOW_SEG_HDR_UDP | ICE_FLOW_SEG_HDR_IPV4, 0);
2261         if (ret)
2262                 PMD_DRV_LOG(ERR, "%s UDP_IPV4 rss flow fail %d", __func__, ret);
2263
2264         /* configure RSS for sctp4 with input set IP src/dst */
2265         ret = ice_add_rss_cfg(hw, vsi->idx, ICE_FLOW_HASH_IPV4,
2266                               ICE_FLOW_SEG_HDR_SCTP | ICE_FLOW_SEG_HDR_IPV4, 0);
2267         if (ret)
2268                 PMD_DRV_LOG(ERR, "%s SCTP_IPV4 rss flow fail %d",
2269                                 __func__, ret);
2270
2271         return 0;
2272 }
2273
2274 static void
2275 __vsi_queues_bind_intr(struct ice_vsi *vsi, uint16_t msix_vect,
2276                        int base_queue, int nb_queue)
2277 {
2278         struct ice_hw *hw = ICE_VSI_TO_HW(vsi);
2279         uint32_t val, val_tx;
2280         int i;
2281
2282         for (i = 0; i < nb_queue; i++) {
2283                 /*do actual bind*/
2284                 val = (msix_vect & QINT_RQCTL_MSIX_INDX_M) |
2285                       (0 < QINT_RQCTL_ITR_INDX_S) | QINT_RQCTL_CAUSE_ENA_M;
2286                 val_tx = (msix_vect & QINT_TQCTL_MSIX_INDX_M) |
2287                          (0 < QINT_TQCTL_ITR_INDX_S) | QINT_TQCTL_CAUSE_ENA_M;
2288
2289                 PMD_DRV_LOG(INFO, "queue %d is binding to vect %d",
2290                             base_queue + i, msix_vect);
2291                 /* set ITR0 value */
2292                 ICE_WRITE_REG(hw, GLINT_ITR(0, msix_vect), 0x10);
2293                 ICE_WRITE_REG(hw, QINT_RQCTL(base_queue + i), val);
2294                 ICE_WRITE_REG(hw, QINT_TQCTL(base_queue + i), val_tx);
2295         }
2296 }
2297
2298 static void
2299 ice_vsi_queues_bind_intr(struct ice_vsi *vsi)
2300 {
2301         struct rte_eth_dev *dev = vsi->adapter->eth_dev;
2302         struct rte_pci_device *pci_dev = ICE_DEV_TO_PCI(dev);
2303         struct rte_intr_handle *intr_handle = &pci_dev->intr_handle;
2304         struct ice_hw *hw = ICE_VSI_TO_HW(vsi);
2305         uint16_t msix_vect = vsi->msix_intr;
2306         uint16_t nb_msix = RTE_MIN(vsi->nb_msix, intr_handle->nb_efd);
2307         uint16_t queue_idx = 0;
2308         int record = 0;
2309         int i;
2310
2311         /* clear Rx/Tx queue interrupt */
2312         for (i = 0; i < vsi->nb_used_qps; i++) {
2313                 ICE_WRITE_REG(hw, QINT_TQCTL(vsi->base_queue + i), 0);
2314                 ICE_WRITE_REG(hw, QINT_RQCTL(vsi->base_queue + i), 0);
2315         }
2316
2317         /* PF bind interrupt */
2318         if (rte_intr_dp_is_en(intr_handle)) {
2319                 queue_idx = 0;
2320                 record = 1;
2321         }
2322
2323         for (i = 0; i < vsi->nb_used_qps; i++) {
2324                 if (nb_msix <= 1) {
2325                         if (!rte_intr_allow_others(intr_handle))
2326                                 msix_vect = ICE_MISC_VEC_ID;
2327
2328                         /* uio mapping all queue to one msix_vect */
2329                         __vsi_queues_bind_intr(vsi, msix_vect,
2330                                                vsi->base_queue + i,
2331                                                vsi->nb_used_qps - i);
2332
2333                         for (; !!record && i < vsi->nb_used_qps; i++)
2334                                 intr_handle->intr_vec[queue_idx + i] =
2335                                         msix_vect;
2336                         break;
2337                 }
2338
2339                 /* vfio 1:1 queue/msix_vect mapping */
2340                 __vsi_queues_bind_intr(vsi, msix_vect,
2341                                        vsi->base_queue + i, 1);
2342
2343                 if (!!record)
2344                         intr_handle->intr_vec[queue_idx + i] = msix_vect;
2345
2346                 msix_vect++;
2347                 nb_msix--;
2348         }
2349 }
2350
2351 static void
2352 ice_vsi_enable_queues_intr(struct ice_vsi *vsi)
2353 {
2354         struct rte_eth_dev *dev = vsi->adapter->eth_dev;
2355         struct rte_pci_device *pci_dev = ICE_DEV_TO_PCI(dev);
2356         struct rte_intr_handle *intr_handle = &pci_dev->intr_handle;
2357         struct ice_hw *hw = ICE_VSI_TO_HW(vsi);
2358         uint16_t msix_intr, i;
2359
2360         if (rte_intr_allow_others(intr_handle))
2361                 for (i = 0; i < vsi->nb_used_qps; i++) {
2362                         msix_intr = vsi->msix_intr + i;
2363                         ICE_WRITE_REG(hw, GLINT_DYN_CTL(msix_intr),
2364                                       GLINT_DYN_CTL_INTENA_M |
2365                                       GLINT_DYN_CTL_CLEARPBA_M |
2366                                       GLINT_DYN_CTL_ITR_INDX_M |
2367                                       GLINT_DYN_CTL_WB_ON_ITR_M);
2368                 }
2369         else
2370                 ICE_WRITE_REG(hw, GLINT_DYN_CTL(0),
2371                               GLINT_DYN_CTL_INTENA_M |
2372                               GLINT_DYN_CTL_CLEARPBA_M |
2373                               GLINT_DYN_CTL_ITR_INDX_M |
2374                               GLINT_DYN_CTL_WB_ON_ITR_M);
2375 }
2376
2377 static int
2378 ice_rxq_intr_setup(struct rte_eth_dev *dev)
2379 {
2380         struct ice_pf *pf = ICE_DEV_PRIVATE_TO_PF(dev->data->dev_private);
2381         struct rte_pci_device *pci_dev = ICE_DEV_TO_PCI(dev);
2382         struct rte_intr_handle *intr_handle = &pci_dev->intr_handle;
2383         struct ice_vsi *vsi = pf->main_vsi;
2384         uint32_t intr_vector = 0;
2385
2386         rte_intr_disable(intr_handle);
2387
2388         /* check and configure queue intr-vector mapping */
2389         if ((rte_intr_cap_multiple(intr_handle) ||
2390              !RTE_ETH_DEV_SRIOV(dev).active) &&
2391             dev->data->dev_conf.intr_conf.rxq != 0) {
2392                 intr_vector = dev->data->nb_rx_queues;
2393                 if (intr_vector > ICE_MAX_INTR_QUEUE_NUM) {
2394                         PMD_DRV_LOG(ERR, "At most %d intr queues supported",
2395                                     ICE_MAX_INTR_QUEUE_NUM);
2396                         return -ENOTSUP;
2397                 }
2398                 if (rte_intr_efd_enable(intr_handle, intr_vector))
2399                         return -1;
2400         }
2401
2402         if (rte_intr_dp_is_en(intr_handle) && !intr_handle->intr_vec) {
2403                 intr_handle->intr_vec =
2404                 rte_zmalloc(NULL, dev->data->nb_rx_queues * sizeof(int),
2405                             0);
2406                 if (!intr_handle->intr_vec) {
2407                         PMD_DRV_LOG(ERR,
2408                                     "Failed to allocate %d rx_queues intr_vec",
2409                                     dev->data->nb_rx_queues);
2410                         return -ENOMEM;
2411                 }
2412         }
2413
2414         /* Map queues with MSIX interrupt */
2415         vsi->nb_used_qps = dev->data->nb_rx_queues;
2416         ice_vsi_queues_bind_intr(vsi);
2417
2418         /* Enable interrupts for all the queues */
2419         ice_vsi_enable_queues_intr(vsi);
2420
2421         rte_intr_enable(intr_handle);
2422
2423         return 0;
2424 }
2425
2426 static int
2427 ice_dev_start(struct rte_eth_dev *dev)
2428 {
2429         struct rte_eth_dev_data *data = dev->data;
2430         struct ice_hw *hw = ICE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2431         struct ice_pf *pf = ICE_DEV_PRIVATE_TO_PF(dev->data->dev_private);
2432         struct ice_vsi *vsi = pf->main_vsi;
2433         uint16_t nb_rxq = 0;
2434         uint16_t nb_txq, i;
2435         int mask, ret;
2436
2437         /* program Tx queues' context in hardware */
2438         for (nb_txq = 0; nb_txq < data->nb_tx_queues; nb_txq++) {
2439                 ret = ice_tx_queue_start(dev, nb_txq);
2440                 if (ret) {
2441                         PMD_DRV_LOG(ERR, "fail to start Tx queue %u", nb_txq);
2442                         goto tx_err;
2443                 }
2444         }
2445
2446         /* program Rx queues' context in hardware*/
2447         for (nb_rxq = 0; nb_rxq < data->nb_rx_queues; nb_rxq++) {
2448                 ret = ice_rx_queue_start(dev, nb_rxq);
2449                 if (ret) {
2450                         PMD_DRV_LOG(ERR, "fail to start Rx queue %u", nb_rxq);
2451                         goto rx_err;
2452                 }
2453         }
2454
2455         ret = ice_init_rss(pf);
2456         if (ret) {
2457                 PMD_DRV_LOG(ERR, "Failed to enable rss for PF");
2458                 goto rx_err;
2459         }
2460
2461         ice_set_rx_function(dev);
2462         ice_set_tx_function(dev);
2463
2464         mask = ETH_VLAN_STRIP_MASK | ETH_VLAN_FILTER_MASK |
2465                         ETH_VLAN_EXTEND_MASK;
2466         ret = ice_vlan_offload_set(dev, mask);
2467         if (ret) {
2468                 PMD_INIT_LOG(ERR, "Unable to set VLAN offload");
2469                 goto rx_err;
2470         }
2471
2472         /* enable Rx interrput and mapping Rx queue to interrupt vector */
2473         if (ice_rxq_intr_setup(dev))
2474                 return -EIO;
2475
2476         /* Enable receiving broadcast packets and transmitting packets */
2477         ret = ice_set_vsi_promisc(hw, vsi->idx,
2478                                   ICE_PROMISC_BCAST_RX | ICE_PROMISC_BCAST_TX |
2479                                   ICE_PROMISC_UCAST_TX | ICE_PROMISC_MCAST_TX,
2480                                   0);
2481         if (ret != ICE_SUCCESS)
2482                 PMD_DRV_LOG(INFO, "fail to set vsi broadcast");
2483
2484         ret = ice_aq_set_event_mask(hw, hw->port_info->lport,
2485                                     ((u16)(ICE_AQ_LINK_EVENT_LINK_FAULT |
2486                                      ICE_AQ_LINK_EVENT_PHY_TEMP_ALARM |
2487                                      ICE_AQ_LINK_EVENT_EXCESSIVE_ERRORS |
2488                                      ICE_AQ_LINK_EVENT_SIGNAL_DETECT |
2489                                      ICE_AQ_LINK_EVENT_AN_COMPLETED |
2490                                      ICE_AQ_LINK_EVENT_PORT_TX_SUSPENDED)),
2491                                      NULL);
2492         if (ret != ICE_SUCCESS)
2493                 PMD_DRV_LOG(WARNING, "Fail to set phy mask");
2494
2495         ice_dev_set_link_up(dev);
2496
2497         /* Call get_link_info aq commond to enable/disable LSE */
2498         ice_link_update(dev, 0);
2499
2500         pf->adapter_stopped = false;
2501
2502         return 0;
2503
2504         /* stop the started queues if failed to start all queues */
2505 rx_err:
2506         for (i = 0; i < nb_rxq; i++)
2507                 ice_rx_queue_stop(dev, i);
2508 tx_err:
2509         for (i = 0; i < nb_txq; i++)
2510                 ice_tx_queue_stop(dev, i);
2511
2512         return -EIO;
2513 }
2514
2515 static int
2516 ice_dev_reset(struct rte_eth_dev *dev)
2517 {
2518         int ret;
2519
2520         if (dev->data->sriov.active)
2521                 return -ENOTSUP;
2522
2523         ret = ice_dev_uninit(dev);
2524         if (ret) {
2525                 PMD_INIT_LOG(ERR, "failed to uninit device, status = %d", ret);
2526                 return -ENXIO;
2527         }
2528
2529         ret = ice_dev_init(dev);
2530         if (ret) {
2531                 PMD_INIT_LOG(ERR, "failed to init device, status = %d", ret);
2532                 return -ENXIO;
2533         }
2534
2535         return 0;
2536 }
2537
2538 static int
2539 ice_dev_info_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *dev_info)
2540 {
2541         struct ice_pf *pf = ICE_DEV_PRIVATE_TO_PF(dev->data->dev_private);
2542         struct ice_hw *hw = ICE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2543         struct ice_vsi *vsi = pf->main_vsi;
2544         struct rte_pci_device *pci_dev = RTE_DEV_TO_PCI(dev->device);
2545         bool is_safe_mode = pf->adapter->is_safe_mode;
2546         u64 phy_type_low;
2547         u64 phy_type_high;
2548
2549         dev_info->min_rx_bufsize = ICE_BUF_SIZE_MIN;
2550         dev_info->max_rx_pktlen = ICE_FRAME_SIZE_MAX;
2551         dev_info->max_rx_queues = vsi->nb_qps;
2552         dev_info->max_tx_queues = vsi->nb_qps;
2553         dev_info->max_mac_addrs = vsi->max_macaddrs;
2554         dev_info->max_vfs = pci_dev->max_vfs;
2555         dev_info->max_mtu = dev_info->max_rx_pktlen - ICE_ETH_OVERHEAD;
2556         dev_info->min_mtu = RTE_ETHER_MIN_MTU;
2557
2558         dev_info->rx_offload_capa =
2559                 DEV_RX_OFFLOAD_VLAN_STRIP |
2560                 DEV_RX_OFFLOAD_JUMBO_FRAME |
2561                 DEV_RX_OFFLOAD_KEEP_CRC |
2562                 DEV_RX_OFFLOAD_SCATTER |
2563                 DEV_RX_OFFLOAD_VLAN_FILTER;
2564         dev_info->tx_offload_capa =
2565                 DEV_TX_OFFLOAD_VLAN_INSERT |
2566                 DEV_TX_OFFLOAD_TCP_TSO |
2567                 DEV_TX_OFFLOAD_MULTI_SEGS |
2568                 DEV_TX_OFFLOAD_MBUF_FAST_FREE;
2569         dev_info->flow_type_rss_offloads = 0;
2570
2571         if (!is_safe_mode) {
2572                 dev_info->rx_offload_capa |=
2573                         DEV_RX_OFFLOAD_IPV4_CKSUM |
2574                         DEV_RX_OFFLOAD_UDP_CKSUM |
2575                         DEV_RX_OFFLOAD_TCP_CKSUM |
2576                         DEV_RX_OFFLOAD_QINQ_STRIP |
2577                         DEV_RX_OFFLOAD_OUTER_IPV4_CKSUM |
2578                         DEV_RX_OFFLOAD_VLAN_EXTEND;
2579                 dev_info->tx_offload_capa |=
2580                         DEV_TX_OFFLOAD_QINQ_INSERT |
2581                         DEV_TX_OFFLOAD_IPV4_CKSUM |
2582                         DEV_TX_OFFLOAD_UDP_CKSUM |
2583                         DEV_TX_OFFLOAD_TCP_CKSUM |
2584                         DEV_TX_OFFLOAD_SCTP_CKSUM |
2585                         DEV_TX_OFFLOAD_OUTER_IPV4_CKSUM |
2586                         DEV_TX_OFFLOAD_OUTER_UDP_CKSUM;
2587                 dev_info->flow_type_rss_offloads |= ICE_RSS_OFFLOAD_ALL;
2588         }
2589
2590         dev_info->rx_queue_offload_capa = 0;
2591         dev_info->tx_queue_offload_capa = 0;
2592
2593         dev_info->reta_size = pf->hash_lut_size;
2594         dev_info->hash_key_size = (VSIQF_HKEY_MAX_INDEX + 1) * sizeof(uint32_t);
2595
2596         dev_info->default_rxconf = (struct rte_eth_rxconf) {
2597                 .rx_thresh = {
2598                         .pthresh = ICE_DEFAULT_RX_PTHRESH,
2599                         .hthresh = ICE_DEFAULT_RX_HTHRESH,
2600                         .wthresh = ICE_DEFAULT_RX_WTHRESH,
2601                 },
2602                 .rx_free_thresh = ICE_DEFAULT_RX_FREE_THRESH,
2603                 .rx_drop_en = 0,
2604                 .offloads = 0,
2605         };
2606
2607         dev_info->default_txconf = (struct rte_eth_txconf) {
2608                 .tx_thresh = {
2609                         .pthresh = ICE_DEFAULT_TX_PTHRESH,
2610                         .hthresh = ICE_DEFAULT_TX_HTHRESH,
2611                         .wthresh = ICE_DEFAULT_TX_WTHRESH,
2612                 },
2613                 .tx_free_thresh = ICE_DEFAULT_TX_FREE_THRESH,
2614                 .tx_rs_thresh = ICE_DEFAULT_TX_RSBIT_THRESH,
2615                 .offloads = 0,
2616         };
2617
2618         dev_info->rx_desc_lim = (struct rte_eth_desc_lim) {
2619                 .nb_max = ICE_MAX_RING_DESC,
2620                 .nb_min = ICE_MIN_RING_DESC,
2621                 .nb_align = ICE_ALIGN_RING_DESC,
2622         };
2623
2624         dev_info->tx_desc_lim = (struct rte_eth_desc_lim) {
2625                 .nb_max = ICE_MAX_RING_DESC,
2626                 .nb_min = ICE_MIN_RING_DESC,
2627                 .nb_align = ICE_ALIGN_RING_DESC,
2628         };
2629
2630         dev_info->speed_capa = ETH_LINK_SPEED_10M |
2631                                ETH_LINK_SPEED_100M |
2632                                ETH_LINK_SPEED_1G |
2633                                ETH_LINK_SPEED_2_5G |
2634                                ETH_LINK_SPEED_5G |
2635                                ETH_LINK_SPEED_10G |
2636                                ETH_LINK_SPEED_20G |
2637                                ETH_LINK_SPEED_25G;
2638
2639         phy_type_low = hw->port_info->phy.phy_type_low;
2640         phy_type_high = hw->port_info->phy.phy_type_high;
2641
2642         if (ICE_PHY_TYPE_SUPPORT_50G(phy_type_low))
2643                 dev_info->speed_capa |= ETH_LINK_SPEED_50G;
2644
2645         if (ICE_PHY_TYPE_SUPPORT_100G_LOW(phy_type_low) ||
2646                         ICE_PHY_TYPE_SUPPORT_100G_HIGH(phy_type_high))
2647                 dev_info->speed_capa |= ETH_LINK_SPEED_100G;
2648
2649         dev_info->nb_rx_queues = dev->data->nb_rx_queues;
2650         dev_info->nb_tx_queues = dev->data->nb_tx_queues;
2651
2652         dev_info->default_rxportconf.burst_size = ICE_RX_MAX_BURST;
2653         dev_info->default_txportconf.burst_size = ICE_TX_MAX_BURST;
2654         dev_info->default_rxportconf.nb_queues = 1;
2655         dev_info->default_txportconf.nb_queues = 1;
2656         dev_info->default_rxportconf.ring_size = ICE_BUF_SIZE_MIN;
2657         dev_info->default_txportconf.ring_size = ICE_BUF_SIZE_MIN;
2658
2659         return 0;
2660 }
2661
2662 static inline int
2663 ice_atomic_read_link_status(struct rte_eth_dev *dev,
2664                             struct rte_eth_link *link)
2665 {
2666         struct rte_eth_link *dst = link;
2667         struct rte_eth_link *src = &dev->data->dev_link;
2668
2669         if (rte_atomic64_cmpset((uint64_t *)dst, *(uint64_t *)dst,
2670                                 *(uint64_t *)src) == 0)
2671                 return -1;
2672
2673         return 0;
2674 }
2675
2676 static inline int
2677 ice_atomic_write_link_status(struct rte_eth_dev *dev,
2678                              struct rte_eth_link *link)
2679 {
2680         struct rte_eth_link *dst = &dev->data->dev_link;
2681         struct rte_eth_link *src = link;
2682
2683         if (rte_atomic64_cmpset((uint64_t *)dst, *(uint64_t *)dst,
2684                                 *(uint64_t *)src) == 0)
2685                 return -1;
2686
2687         return 0;
2688 }
2689
2690 static int
2691 ice_link_update(struct rte_eth_dev *dev, int wait_to_complete)
2692 {
2693 #define CHECK_INTERVAL 100  /* 100ms */
2694 #define MAX_REPEAT_TIME 10  /* 1s (10 * 100ms) in total */
2695         struct ice_hw *hw = ICE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2696         struct ice_link_status link_status;
2697         struct rte_eth_link link, old;
2698         int status;
2699         unsigned int rep_cnt = MAX_REPEAT_TIME;
2700         bool enable_lse = dev->data->dev_conf.intr_conf.lsc ? true : false;
2701
2702         memset(&link, 0, sizeof(link));
2703         memset(&old, 0, sizeof(old));
2704         memset(&link_status, 0, sizeof(link_status));
2705         ice_atomic_read_link_status(dev, &old);
2706
2707         do {
2708                 /* Get link status information from hardware */
2709                 status = ice_aq_get_link_info(hw->port_info, enable_lse,
2710                                               &link_status, NULL);
2711                 if (status != ICE_SUCCESS) {
2712                         link.link_speed = ETH_SPEED_NUM_100M;
2713                         link.link_duplex = ETH_LINK_FULL_DUPLEX;
2714                         PMD_DRV_LOG(ERR, "Failed to get link info");
2715                         goto out;
2716                 }
2717
2718                 link.link_status = link_status.link_info & ICE_AQ_LINK_UP;
2719                 if (!wait_to_complete || link.link_status)
2720                         break;
2721
2722                 rte_delay_ms(CHECK_INTERVAL);
2723         } while (--rep_cnt);
2724
2725         if (!link.link_status)
2726                 goto out;
2727
2728         /* Full-duplex operation at all supported speeds */
2729         link.link_duplex = ETH_LINK_FULL_DUPLEX;
2730
2731         /* Parse the link status */
2732         switch (link_status.link_speed) {
2733         case ICE_AQ_LINK_SPEED_10MB:
2734                 link.link_speed = ETH_SPEED_NUM_10M;
2735                 break;
2736         case ICE_AQ_LINK_SPEED_100MB:
2737                 link.link_speed = ETH_SPEED_NUM_100M;
2738                 break;
2739         case ICE_AQ_LINK_SPEED_1000MB:
2740                 link.link_speed = ETH_SPEED_NUM_1G;
2741                 break;
2742         case ICE_AQ_LINK_SPEED_2500MB:
2743                 link.link_speed = ETH_SPEED_NUM_2_5G;
2744                 break;
2745         case ICE_AQ_LINK_SPEED_5GB:
2746                 link.link_speed = ETH_SPEED_NUM_5G;
2747                 break;
2748         case ICE_AQ_LINK_SPEED_10GB:
2749                 link.link_speed = ETH_SPEED_NUM_10G;
2750                 break;
2751         case ICE_AQ_LINK_SPEED_20GB:
2752                 link.link_speed = ETH_SPEED_NUM_20G;
2753                 break;
2754         case ICE_AQ_LINK_SPEED_25GB:
2755                 link.link_speed = ETH_SPEED_NUM_25G;
2756                 break;
2757         case ICE_AQ_LINK_SPEED_40GB:
2758                 link.link_speed = ETH_SPEED_NUM_40G;
2759                 break;
2760         case ICE_AQ_LINK_SPEED_50GB:
2761                 link.link_speed = ETH_SPEED_NUM_50G;
2762                 break;
2763         case ICE_AQ_LINK_SPEED_100GB:
2764                 link.link_speed = ETH_SPEED_NUM_100G;
2765                 break;
2766         case ICE_AQ_LINK_SPEED_UNKNOWN:
2767         default:
2768                 PMD_DRV_LOG(ERR, "Unknown link speed");
2769                 link.link_speed = ETH_SPEED_NUM_NONE;
2770                 break;
2771         }
2772
2773         link.link_autoneg = !(dev->data->dev_conf.link_speeds &
2774                               ETH_LINK_SPEED_FIXED);
2775
2776 out:
2777         ice_atomic_write_link_status(dev, &link);
2778         if (link.link_status == old.link_status)
2779                 return -1;
2780
2781         return 0;
2782 }
2783
2784 /* Force the physical link state by getting the current PHY capabilities from
2785  * hardware and setting the PHY config based on the determined capabilities. If
2786  * link changes, link event will be triggered because both the Enable Automatic
2787  * Link Update and LESM Enable bits are set when setting the PHY capabilities.
2788  */
2789 static enum ice_status
2790 ice_force_phys_link_state(struct ice_hw *hw, bool link_up)
2791 {
2792         struct ice_aqc_set_phy_cfg_data cfg = { 0 };
2793         struct ice_aqc_get_phy_caps_data *pcaps;
2794         struct ice_port_info *pi;
2795         enum ice_status status;
2796
2797         if (!hw || !hw->port_info)
2798                 return ICE_ERR_PARAM;
2799
2800         pi = hw->port_info;
2801
2802         pcaps = (struct ice_aqc_get_phy_caps_data *)
2803                 ice_malloc(hw, sizeof(*pcaps));
2804         if (!pcaps)
2805                 return ICE_ERR_NO_MEMORY;
2806
2807         status = ice_aq_get_phy_caps(pi, false, ICE_AQC_REPORT_SW_CFG, pcaps,
2808                                      NULL);
2809         if (status)
2810                 goto out;
2811
2812         /* No change in link */
2813         if (link_up == !!(pcaps->caps & ICE_AQC_PHY_EN_LINK) &&
2814             link_up == !!(pi->phy.link_info.link_info & ICE_AQ_LINK_UP))
2815                 goto out;
2816
2817         cfg.phy_type_low = pcaps->phy_type_low;
2818         cfg.phy_type_high = pcaps->phy_type_high;
2819         cfg.caps = pcaps->caps | ICE_AQ_PHY_ENA_AUTO_LINK_UPDT;
2820         cfg.low_power_ctrl = pcaps->low_power_ctrl;
2821         cfg.eee_cap = pcaps->eee_cap;
2822         cfg.eeer_value = pcaps->eeer_value;
2823         cfg.link_fec_opt = pcaps->link_fec_options;
2824         if (link_up)
2825                 cfg.caps |= ICE_AQ_PHY_ENA_LINK;
2826         else
2827                 cfg.caps &= ~ICE_AQ_PHY_ENA_LINK;
2828
2829         status = ice_aq_set_phy_cfg(hw, pi, &cfg, NULL);
2830
2831 out:
2832         ice_free(hw, pcaps);
2833         return status;
2834 }
2835
2836 static int
2837 ice_dev_set_link_up(struct rte_eth_dev *dev)
2838 {
2839         struct ice_hw *hw = ICE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2840
2841         return ice_force_phys_link_state(hw, true);
2842 }
2843
2844 static int
2845 ice_dev_set_link_down(struct rte_eth_dev *dev)
2846 {
2847         struct ice_hw *hw = ICE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2848
2849         return ice_force_phys_link_state(hw, false);
2850 }
2851
2852 static int
2853 ice_mtu_set(struct rte_eth_dev *dev, uint16_t mtu)
2854 {
2855         struct ice_pf *pf = ICE_DEV_PRIVATE_TO_PF(dev->data->dev_private);
2856         struct rte_eth_dev_data *dev_data = pf->dev_data;
2857         uint32_t frame_size = mtu + ICE_ETH_OVERHEAD;
2858
2859         /* check if mtu is within the allowed range */
2860         if (mtu < RTE_ETHER_MIN_MTU || frame_size > ICE_FRAME_SIZE_MAX)
2861                 return -EINVAL;
2862
2863         /* mtu setting is forbidden if port is start */
2864         if (dev_data->dev_started) {
2865                 PMD_DRV_LOG(ERR,
2866                             "port %d must be stopped before configuration",
2867                             dev_data->port_id);
2868                 return -EBUSY;
2869         }
2870
2871         if (frame_size > RTE_ETHER_MAX_LEN)
2872                 dev_data->dev_conf.rxmode.offloads |=
2873                         DEV_RX_OFFLOAD_JUMBO_FRAME;
2874         else
2875                 dev_data->dev_conf.rxmode.offloads &=
2876                         ~DEV_RX_OFFLOAD_JUMBO_FRAME;
2877
2878         dev_data->dev_conf.rxmode.max_rx_pkt_len = frame_size;
2879
2880         return 0;
2881 }
2882
2883 static int ice_macaddr_set(struct rte_eth_dev *dev,
2884                            struct rte_ether_addr *mac_addr)
2885 {
2886         struct ice_hw *hw = ICE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2887         struct ice_pf *pf = ICE_DEV_PRIVATE_TO_PF(dev->data->dev_private);
2888         struct ice_vsi *vsi = pf->main_vsi;
2889         struct ice_mac_filter *f;
2890         uint8_t flags = 0;
2891         int ret;
2892
2893         if (!rte_is_valid_assigned_ether_addr(mac_addr)) {
2894                 PMD_DRV_LOG(ERR, "Tried to set invalid MAC address.");
2895                 return -EINVAL;
2896         }
2897
2898         TAILQ_FOREACH(f, &vsi->mac_list, next) {
2899                 if (rte_is_same_ether_addr(&pf->dev_addr, &f->mac_info.mac_addr))
2900                         break;
2901         }
2902
2903         if (!f) {
2904                 PMD_DRV_LOG(ERR, "Failed to find filter for default mac");
2905                 return -EIO;
2906         }
2907
2908         ret = ice_remove_mac_filter(vsi, &f->mac_info.mac_addr);
2909         if (ret != ICE_SUCCESS) {
2910                 PMD_DRV_LOG(ERR, "Failed to delete mac filter");
2911                 return -EIO;
2912         }
2913         ret = ice_add_mac_filter(vsi, mac_addr);
2914         if (ret != ICE_SUCCESS) {
2915                 PMD_DRV_LOG(ERR, "Failed to add mac filter");
2916                 return -EIO;
2917         }
2918         memcpy(&pf->dev_addr, mac_addr, ETH_ADDR_LEN);
2919
2920         flags = ICE_AQC_MAN_MAC_UPDATE_LAA_WOL;
2921         ret = ice_aq_manage_mac_write(hw, mac_addr->addr_bytes, flags, NULL);
2922         if (ret != ICE_SUCCESS)
2923                 PMD_DRV_LOG(ERR, "Failed to set manage mac");
2924
2925         return 0;
2926 }
2927
2928 /* Add a MAC address, and update filters */
2929 static int
2930 ice_macaddr_add(struct rte_eth_dev *dev,
2931                 struct rte_ether_addr *mac_addr,
2932                 __rte_unused uint32_t index,
2933                 __rte_unused uint32_t pool)
2934 {
2935         struct ice_pf *pf = ICE_DEV_PRIVATE_TO_PF(dev->data->dev_private);
2936         struct ice_vsi *vsi = pf->main_vsi;
2937         int ret;
2938
2939         ret = ice_add_mac_filter(vsi, mac_addr);
2940         if (ret != ICE_SUCCESS) {
2941                 PMD_DRV_LOG(ERR, "Failed to add MAC filter");
2942                 return -EINVAL;
2943         }
2944
2945         return ICE_SUCCESS;
2946 }
2947
2948 /* Remove a MAC address, and update filters */
2949 static void
2950 ice_macaddr_remove(struct rte_eth_dev *dev, uint32_t index)
2951 {
2952         struct ice_pf *pf = ICE_DEV_PRIVATE_TO_PF(dev->data->dev_private);
2953         struct ice_vsi *vsi = pf->main_vsi;
2954         struct rte_eth_dev_data *data = dev->data;
2955         struct rte_ether_addr *macaddr;
2956         int ret;
2957
2958         macaddr = &data->mac_addrs[index];
2959         ret = ice_remove_mac_filter(vsi, macaddr);
2960         if (ret) {
2961                 PMD_DRV_LOG(ERR, "Failed to remove MAC filter");
2962                 return;
2963         }
2964 }
2965
2966 static int
2967 ice_vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
2968 {
2969         struct ice_pf *pf = ICE_DEV_PRIVATE_TO_PF(dev->data->dev_private);
2970         struct ice_vsi *vsi = pf->main_vsi;
2971         int ret;
2972
2973         PMD_INIT_FUNC_TRACE();
2974
2975         if (on) {
2976                 ret = ice_add_vlan_filter(vsi, vlan_id);
2977                 if (ret < 0) {
2978                         PMD_DRV_LOG(ERR, "Failed to add vlan filter");
2979                         return -EINVAL;
2980                 }
2981         } else {
2982                 ret = ice_remove_vlan_filter(vsi, vlan_id);
2983                 if (ret < 0) {
2984                         PMD_DRV_LOG(ERR, "Failed to remove vlan filter");
2985                         return -EINVAL;
2986                 }
2987         }
2988
2989         return 0;
2990 }
2991
2992 /* Configure vlan filter on or off */
2993 static int
2994 ice_vsi_config_vlan_filter(struct ice_vsi *vsi, bool on)
2995 {
2996         struct ice_hw *hw = ICE_VSI_TO_HW(vsi);
2997         struct ice_vsi_ctx ctxt;
2998         uint8_t sec_flags, sw_flags2;
2999         int ret = 0;
3000
3001         sec_flags = ICE_AQ_VSI_SEC_TX_VLAN_PRUNE_ENA <<
3002                     ICE_AQ_VSI_SEC_TX_PRUNE_ENA_S;
3003         sw_flags2 = ICE_AQ_VSI_SW_FLAG_RX_VLAN_PRUNE_ENA;
3004
3005         if (on) {
3006                 vsi->info.sec_flags |= sec_flags;
3007                 vsi->info.sw_flags2 |= sw_flags2;
3008         } else {
3009                 vsi->info.sec_flags &= ~sec_flags;
3010                 vsi->info.sw_flags2 &= ~sw_flags2;
3011         }
3012         vsi->info.sw_id = hw->port_info->sw_id;
3013         (void)rte_memcpy(&ctxt.info, &vsi->info, sizeof(vsi->info));
3014         ctxt.info.valid_sections =
3015                 rte_cpu_to_le_16(ICE_AQ_VSI_PROP_SW_VALID |
3016                                  ICE_AQ_VSI_PROP_SECURITY_VALID);
3017         ctxt.vsi_num = vsi->vsi_id;
3018
3019         ret = ice_update_vsi(hw, vsi->idx, &ctxt, NULL);
3020         if (ret) {
3021                 PMD_DRV_LOG(INFO, "Update VSI failed to %s vlan rx pruning",
3022                             on ? "enable" : "disable");
3023                 return -EINVAL;
3024         } else {
3025                 vsi->info.valid_sections |=
3026                         rte_cpu_to_le_16(ICE_AQ_VSI_PROP_SW_VALID |
3027                                          ICE_AQ_VSI_PROP_SECURITY_VALID);
3028         }
3029
3030         /* consist with other drivers, allow untagged packet when vlan filter on */
3031         if (on)
3032                 ret = ice_add_vlan_filter(vsi, 0);
3033         else
3034                 ret = ice_remove_vlan_filter(vsi, 0);
3035
3036         return 0;
3037 }
3038
3039 static int
3040 ice_vsi_config_vlan_stripping(struct ice_vsi *vsi, bool on)
3041 {
3042         struct ice_hw *hw = ICE_VSI_TO_HW(vsi);
3043         struct ice_vsi_ctx ctxt;
3044         uint8_t vlan_flags;
3045         int ret = 0;
3046
3047         /* Check if it has been already on or off */
3048         if (vsi->info.valid_sections &
3049                 rte_cpu_to_le_16(ICE_AQ_VSI_PROP_VLAN_VALID)) {
3050                 if (on) {
3051                         if ((vsi->info.vlan_flags &
3052                              ICE_AQ_VSI_VLAN_EMOD_M) ==
3053                             ICE_AQ_VSI_VLAN_EMOD_STR_BOTH)
3054                                 return 0; /* already on */
3055                 } else {
3056                         if ((vsi->info.vlan_flags &
3057                              ICE_AQ_VSI_VLAN_EMOD_M) ==
3058                             ICE_AQ_VSI_VLAN_EMOD_NOTHING)
3059                                 return 0; /* already off */
3060                 }
3061         }
3062
3063         if (on)
3064                 vlan_flags = ICE_AQ_VSI_VLAN_EMOD_STR_BOTH;
3065         else
3066                 vlan_flags = ICE_AQ_VSI_VLAN_EMOD_NOTHING;
3067         vsi->info.vlan_flags &= ~(ICE_AQ_VSI_VLAN_EMOD_M);
3068         vsi->info.vlan_flags |= vlan_flags;
3069         (void)rte_memcpy(&ctxt.info, &vsi->info, sizeof(vsi->info));
3070         ctxt.info.valid_sections =
3071                 rte_cpu_to_le_16(ICE_AQ_VSI_PROP_VLAN_VALID);
3072         ctxt.vsi_num = vsi->vsi_id;
3073         ret = ice_update_vsi(hw, vsi->idx, &ctxt, NULL);
3074         if (ret) {
3075                 PMD_DRV_LOG(INFO, "Update VSI failed to %s vlan stripping",
3076                             on ? "enable" : "disable");
3077                 return -EINVAL;
3078         }
3079
3080         vsi->info.valid_sections |=
3081                 rte_cpu_to_le_16(ICE_AQ_VSI_PROP_VLAN_VALID);
3082
3083         return ret;
3084 }
3085
3086 static int
3087 ice_vlan_offload_set(struct rte_eth_dev *dev, int mask)
3088 {
3089         struct ice_pf *pf = ICE_DEV_PRIVATE_TO_PF(dev->data->dev_private);
3090         struct ice_vsi *vsi = pf->main_vsi;
3091         struct rte_eth_rxmode *rxmode;
3092
3093         rxmode = &dev->data->dev_conf.rxmode;
3094         if (mask & ETH_VLAN_FILTER_MASK) {
3095                 if (rxmode->offloads & DEV_RX_OFFLOAD_VLAN_FILTER)
3096                         ice_vsi_config_vlan_filter(vsi, TRUE);
3097                 else
3098                         ice_vsi_config_vlan_filter(vsi, FALSE);
3099         }
3100
3101         if (mask & ETH_VLAN_STRIP_MASK) {
3102                 if (rxmode->offloads & DEV_RX_OFFLOAD_VLAN_STRIP)
3103                         ice_vsi_config_vlan_stripping(vsi, TRUE);
3104                 else
3105                         ice_vsi_config_vlan_stripping(vsi, FALSE);
3106         }
3107
3108         if (mask & ETH_VLAN_EXTEND_MASK) {
3109                 if (rxmode->offloads & DEV_RX_OFFLOAD_VLAN_EXTEND)
3110                         ice_vsi_config_double_vlan(vsi, TRUE);
3111                 else
3112                         ice_vsi_config_double_vlan(vsi, FALSE);
3113         }
3114
3115         return 0;
3116 }
3117
3118 static int
3119 ice_vlan_tpid_set(struct rte_eth_dev *dev,
3120                   enum rte_vlan_type vlan_type,
3121                   uint16_t tpid)
3122 {
3123         struct ice_hw *hw = ICE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
3124         uint64_t reg_r = 0, reg_w = 0;
3125         uint16_t reg_id = 0;
3126         int ret = 0;
3127         int qinq = dev->data->dev_conf.rxmode.offloads &
3128                    DEV_RX_OFFLOAD_VLAN_EXTEND;
3129
3130         switch (vlan_type) {
3131         case ETH_VLAN_TYPE_OUTER:
3132                 if (qinq)
3133                         reg_id = 3;
3134                 else
3135                         reg_id = 5;
3136                 break;
3137         case ETH_VLAN_TYPE_INNER:
3138                 if (qinq) {
3139                         reg_id = 5;
3140                 } else {
3141                         PMD_DRV_LOG(ERR,
3142                                     "Unsupported vlan type in single vlan.");
3143                         return -EINVAL;
3144                 }
3145                 break;
3146         default:
3147                 PMD_DRV_LOG(ERR, "Unsupported vlan type %d", vlan_type);
3148                 return -EINVAL;
3149         }
3150         reg_r = ICE_READ_REG(hw, GL_SWT_L2TAGCTRL(reg_id));
3151         PMD_DRV_LOG(DEBUG, "Debug read from ICE GL_SWT_L2TAGCTRL[%d]: "
3152                     "0x%08"PRIx64"", reg_id, reg_r);
3153
3154         reg_w = reg_r & (~(GL_SWT_L2TAGCTRL_ETHERTYPE_M));
3155         reg_w |= ((uint64_t)tpid << GL_SWT_L2TAGCTRL_ETHERTYPE_S);
3156         if (reg_r == reg_w) {
3157                 PMD_DRV_LOG(DEBUG, "No need to write");
3158                 return 0;
3159         }
3160
3161         ICE_WRITE_REG(hw, GL_SWT_L2TAGCTRL(reg_id), reg_w);
3162         PMD_DRV_LOG(DEBUG, "Debug write 0x%08"PRIx64" to "
3163                     "ICE GL_SWT_L2TAGCTRL[%d]", reg_w, reg_id);
3164
3165         return ret;
3166 }
3167
3168 static int
3169 ice_get_rss_lut(struct ice_vsi *vsi, uint8_t *lut, uint16_t lut_size)
3170 {
3171         struct ice_pf *pf = ICE_VSI_TO_PF(vsi);
3172         struct ice_hw *hw = ICE_VSI_TO_HW(vsi);
3173         int ret;
3174
3175         if (!lut)
3176                 return -EINVAL;
3177
3178         if (pf->flags & ICE_FLAG_RSS_AQ_CAPABLE) {
3179                 ret = ice_aq_get_rss_lut(hw, vsi->idx, TRUE,
3180                                          lut, lut_size);
3181                 if (ret) {
3182                         PMD_DRV_LOG(ERR, "Failed to get RSS lookup table");
3183                         return -EINVAL;
3184                 }
3185         } else {
3186                 uint64_t *lut_dw = (uint64_t *)lut;
3187                 uint16_t i, lut_size_dw = lut_size / 4;
3188
3189                 for (i = 0; i < lut_size_dw; i++)
3190                         lut_dw[i] = ICE_READ_REG(hw, PFQF_HLUT(i));
3191         }
3192
3193         return 0;
3194 }
3195
3196 static int
3197 ice_set_rss_lut(struct ice_vsi *vsi, uint8_t *lut, uint16_t lut_size)
3198 {
3199         struct ice_pf *pf;
3200         struct ice_hw *hw;
3201         int ret;
3202
3203         if (!vsi || !lut)
3204                 return -EINVAL;
3205
3206         pf = ICE_VSI_TO_PF(vsi);
3207         hw = ICE_VSI_TO_HW(vsi);
3208
3209         if (pf->flags & ICE_FLAG_RSS_AQ_CAPABLE) {
3210                 ret = ice_aq_set_rss_lut(hw, vsi->idx, TRUE,
3211                                          lut, lut_size);
3212                 if (ret) {
3213                         PMD_DRV_LOG(ERR, "Failed to set RSS lookup table");
3214                         return -EINVAL;
3215                 }
3216         } else {
3217                 uint64_t *lut_dw = (uint64_t *)lut;
3218                 uint16_t i, lut_size_dw = lut_size / 4;
3219
3220                 for (i = 0; i < lut_size_dw; i++)
3221                         ICE_WRITE_REG(hw, PFQF_HLUT(i), lut_dw[i]);
3222
3223                 ice_flush(hw);
3224         }
3225
3226         return 0;
3227 }
3228
3229 static int
3230 ice_rss_reta_update(struct rte_eth_dev *dev,
3231                     struct rte_eth_rss_reta_entry64 *reta_conf,
3232                     uint16_t reta_size)
3233 {
3234         struct ice_pf *pf = ICE_DEV_PRIVATE_TO_PF(dev->data->dev_private);
3235         uint16_t i, lut_size = pf->hash_lut_size;
3236         uint16_t idx, shift;
3237         uint8_t *lut;
3238         int ret;
3239
3240         if (reta_size != ICE_AQC_GSET_RSS_LUT_TABLE_SIZE_128 &&
3241             reta_size != ICE_AQC_GSET_RSS_LUT_TABLE_SIZE_512 &&
3242             reta_size != ICE_AQC_GSET_RSS_LUT_TABLE_SIZE_2K) {
3243                 PMD_DRV_LOG(ERR,
3244                             "The size of hash lookup table configured (%d)"
3245                             "doesn't match the number hardware can "
3246                             "supported (128, 512, 2048)",
3247                             reta_size);
3248                 return -EINVAL;
3249         }
3250
3251         /* It MUST use the current LUT size to get the RSS lookup table,
3252          * otherwise if will fail with -100 error code.
3253          */
3254         lut = rte_zmalloc(NULL,  RTE_MAX(reta_size, lut_size), 0);
3255         if (!lut) {
3256                 PMD_DRV_LOG(ERR, "No memory can be allocated");
3257                 return -ENOMEM;
3258         }
3259         ret = ice_get_rss_lut(pf->main_vsi, lut, lut_size);
3260         if (ret)
3261                 goto out;
3262
3263         for (i = 0; i < reta_size; i++) {
3264                 idx = i / RTE_RETA_GROUP_SIZE;
3265                 shift = i % RTE_RETA_GROUP_SIZE;
3266                 if (reta_conf[idx].mask & (1ULL << shift))
3267                         lut[i] = reta_conf[idx].reta[shift];
3268         }
3269         ret = ice_set_rss_lut(pf->main_vsi, lut, reta_size);
3270         if (ret == 0 && lut_size != reta_size) {
3271                 PMD_DRV_LOG(INFO,
3272                             "The size of hash lookup table is changed from (%d) to (%d)",
3273                             lut_size, reta_size);
3274                 pf->hash_lut_size = reta_size;
3275         }
3276
3277 out:
3278         rte_free(lut);
3279
3280         return ret;
3281 }
3282
3283 static int
3284 ice_rss_reta_query(struct rte_eth_dev *dev,
3285                    struct rte_eth_rss_reta_entry64 *reta_conf,
3286                    uint16_t reta_size)
3287 {
3288         struct ice_pf *pf = ICE_DEV_PRIVATE_TO_PF(dev->data->dev_private);
3289         uint16_t i, lut_size = pf->hash_lut_size;
3290         uint16_t idx, shift;
3291         uint8_t *lut;
3292         int ret;
3293
3294         if (reta_size != lut_size) {
3295                 PMD_DRV_LOG(ERR,
3296                             "The size of hash lookup table configured (%d)"
3297                             "doesn't match the number hardware can "
3298                             "supported (%d)",
3299                             reta_size, lut_size);
3300                 return -EINVAL;
3301         }
3302
3303         lut = rte_zmalloc(NULL, reta_size, 0);
3304         if (!lut) {
3305                 PMD_DRV_LOG(ERR, "No memory can be allocated");
3306                 return -ENOMEM;
3307         }
3308
3309         ret = ice_get_rss_lut(pf->main_vsi, lut, reta_size);
3310         if (ret)
3311                 goto out;
3312
3313         for (i = 0; i < reta_size; i++) {
3314                 idx = i / RTE_RETA_GROUP_SIZE;
3315                 shift = i % RTE_RETA_GROUP_SIZE;
3316                 if (reta_conf[idx].mask & (1ULL << shift))
3317                         reta_conf[idx].reta[shift] = lut[i];
3318         }
3319
3320 out:
3321         rte_free(lut);
3322
3323         return ret;
3324 }
3325
3326 static int
3327 ice_set_rss_key(struct ice_vsi *vsi, uint8_t *key, uint8_t key_len)
3328 {
3329         struct ice_hw *hw = ICE_VSI_TO_HW(vsi);
3330         int ret = 0;
3331
3332         if (!key || key_len == 0) {
3333                 PMD_DRV_LOG(DEBUG, "No key to be configured");
3334                 return 0;
3335         } else if (key_len != (VSIQF_HKEY_MAX_INDEX + 1) *
3336                    sizeof(uint32_t)) {
3337                 PMD_DRV_LOG(ERR, "Invalid key length %u", key_len);
3338                 return -EINVAL;
3339         }
3340
3341         struct ice_aqc_get_set_rss_keys *key_dw =
3342                 (struct ice_aqc_get_set_rss_keys *)key;
3343
3344         ret = ice_aq_set_rss_key(hw, vsi->idx, key_dw);
3345         if (ret) {
3346                 PMD_DRV_LOG(ERR, "Failed to configure RSS key via AQ");
3347                 ret = -EINVAL;
3348         }
3349
3350         return ret;
3351 }
3352
3353 static int
3354 ice_get_rss_key(struct ice_vsi *vsi, uint8_t *key, uint8_t *key_len)
3355 {
3356         struct ice_hw *hw = ICE_VSI_TO_HW(vsi);
3357         int ret;
3358
3359         if (!key || !key_len)
3360                 return -EINVAL;
3361
3362         ret = ice_aq_get_rss_key
3363                 (hw, vsi->idx,
3364                  (struct ice_aqc_get_set_rss_keys *)key);
3365         if (ret) {
3366                 PMD_DRV_LOG(ERR, "Failed to get RSS key via AQ");
3367                 return -EINVAL;
3368         }
3369         *key_len = (VSIQF_HKEY_MAX_INDEX + 1) * sizeof(uint32_t);
3370
3371         return 0;
3372 }
3373
3374 static int
3375 ice_rss_hash_update(struct rte_eth_dev *dev,
3376                     struct rte_eth_rss_conf *rss_conf)
3377 {
3378         enum ice_status status = ICE_SUCCESS;
3379         struct ice_pf *pf = ICE_DEV_PRIVATE_TO_PF(dev->data->dev_private);
3380         struct ice_vsi *vsi = pf->main_vsi;
3381
3382         /* set hash key */
3383         status = ice_set_rss_key(vsi, rss_conf->rss_key, rss_conf->rss_key_len);
3384         if (status)
3385                 return status;
3386
3387         /* TODO: hash enable config, ice_add_rss_cfg */
3388         return 0;
3389 }
3390
3391 static int
3392 ice_rss_hash_conf_get(struct rte_eth_dev *dev,
3393                       struct rte_eth_rss_conf *rss_conf)
3394 {
3395         struct ice_pf *pf = ICE_DEV_PRIVATE_TO_PF(dev->data->dev_private);
3396         struct ice_vsi *vsi = pf->main_vsi;
3397
3398         ice_get_rss_key(vsi, rss_conf->rss_key,
3399                         &rss_conf->rss_key_len);
3400
3401         /* TODO: default set to 0 as hf config is not supported now */
3402         rss_conf->rss_hf = 0;
3403         return 0;
3404 }
3405
3406 static int
3407 ice_promisc_enable(struct rte_eth_dev *dev)
3408 {
3409         struct ice_pf *pf = ICE_DEV_PRIVATE_TO_PF(dev->data->dev_private);
3410         struct ice_hw *hw = ICE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
3411         struct ice_vsi *vsi = pf->main_vsi;
3412         enum ice_status status;
3413         uint8_t pmask;
3414         int ret = 0;
3415
3416         pmask = ICE_PROMISC_UCAST_RX | ICE_PROMISC_UCAST_TX |
3417                 ICE_PROMISC_MCAST_RX | ICE_PROMISC_MCAST_TX;
3418
3419         status = ice_set_vsi_promisc(hw, vsi->idx, pmask, 0);
3420         switch (status) {
3421         case ICE_ERR_ALREADY_EXISTS:
3422                 PMD_DRV_LOG(DEBUG, "Promisc mode has already been enabled");
3423         case ICE_SUCCESS:
3424                 break;
3425         default:
3426                 PMD_DRV_LOG(ERR, "Failed to enable promisc, err=%d", status);
3427                 ret = -EAGAIN;
3428         }
3429
3430         return ret;
3431 }
3432
3433 static int
3434 ice_promisc_disable(struct rte_eth_dev *dev)
3435 {
3436         struct ice_pf *pf = ICE_DEV_PRIVATE_TO_PF(dev->data->dev_private);
3437         struct ice_hw *hw = ICE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
3438         struct ice_vsi *vsi = pf->main_vsi;
3439         enum ice_status status;
3440         uint8_t pmask;
3441         int ret = 0;
3442
3443         pmask = ICE_PROMISC_UCAST_RX | ICE_PROMISC_UCAST_TX |
3444                 ICE_PROMISC_MCAST_RX | ICE_PROMISC_MCAST_TX;
3445
3446         status = ice_clear_vsi_promisc(hw, vsi->idx, pmask, 0);
3447         if (status != ICE_SUCCESS) {
3448                 PMD_DRV_LOG(ERR, "Failed to clear promisc, err=%d", status);
3449                 ret = -EAGAIN;
3450         }
3451
3452         return ret;
3453 }
3454
3455 static int
3456 ice_allmulti_enable(struct rte_eth_dev *dev)
3457 {
3458         struct ice_pf *pf = ICE_DEV_PRIVATE_TO_PF(dev->data->dev_private);
3459         struct ice_hw *hw = ICE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
3460         struct ice_vsi *vsi = pf->main_vsi;
3461         enum ice_status status;
3462         uint8_t pmask;
3463         int ret = 0;
3464
3465         pmask = ICE_PROMISC_MCAST_RX | ICE_PROMISC_MCAST_TX;
3466
3467         status = ice_set_vsi_promisc(hw, vsi->idx, pmask, 0);
3468
3469         switch (status) {
3470         case ICE_ERR_ALREADY_EXISTS:
3471                 PMD_DRV_LOG(DEBUG, "Allmulti has already been enabled");
3472         case ICE_SUCCESS:
3473                 break;
3474         default:
3475                 PMD_DRV_LOG(ERR, "Failed to enable allmulti, err=%d", status);
3476                 ret = -EAGAIN;
3477         }
3478
3479         return ret;
3480 }
3481
3482 static int
3483 ice_allmulti_disable(struct rte_eth_dev *dev)
3484 {
3485         struct ice_pf *pf = ICE_DEV_PRIVATE_TO_PF(dev->data->dev_private);
3486         struct ice_hw *hw = ICE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
3487         struct ice_vsi *vsi = pf->main_vsi;
3488         enum ice_status status;
3489         uint8_t pmask;
3490         int ret = 0;
3491
3492         if (dev->data->promiscuous == 1)
3493                 return 0; /* must remain in all_multicast mode */
3494
3495         pmask = ICE_PROMISC_MCAST_RX | ICE_PROMISC_MCAST_TX;
3496
3497         status = ice_clear_vsi_promisc(hw, vsi->idx, pmask, 0);
3498         if (status != ICE_SUCCESS) {
3499                 PMD_DRV_LOG(ERR, "Failed to clear allmulti, err=%d", status);
3500                 ret = -EAGAIN;
3501         }
3502
3503         return ret;
3504 }
3505
3506 static int ice_rx_queue_intr_enable(struct rte_eth_dev *dev,
3507                                     uint16_t queue_id)
3508 {
3509         struct rte_pci_device *pci_dev = ICE_DEV_TO_PCI(dev);
3510         struct rte_intr_handle *intr_handle = &pci_dev->intr_handle;
3511         struct ice_hw *hw = ICE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
3512         uint32_t val;
3513         uint16_t msix_intr;
3514
3515         msix_intr = intr_handle->intr_vec[queue_id];
3516
3517         val = GLINT_DYN_CTL_INTENA_M | GLINT_DYN_CTL_CLEARPBA_M |
3518               GLINT_DYN_CTL_ITR_INDX_M;
3519         val &= ~GLINT_DYN_CTL_WB_ON_ITR_M;
3520
3521         ICE_WRITE_REG(hw, GLINT_DYN_CTL(msix_intr), val);
3522         rte_intr_ack(&pci_dev->intr_handle);
3523
3524         return 0;
3525 }
3526
3527 static int ice_rx_queue_intr_disable(struct rte_eth_dev *dev,
3528                                      uint16_t queue_id)
3529 {
3530         struct rte_pci_device *pci_dev = ICE_DEV_TO_PCI(dev);
3531         struct rte_intr_handle *intr_handle = &pci_dev->intr_handle;
3532         struct ice_hw *hw = ICE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
3533         uint16_t msix_intr;
3534
3535         msix_intr = intr_handle->intr_vec[queue_id];
3536
3537         ICE_WRITE_REG(hw, GLINT_DYN_CTL(msix_intr), GLINT_DYN_CTL_WB_ON_ITR_M);
3538
3539         return 0;
3540 }
3541
3542 static int
3543 ice_fw_version_get(struct rte_eth_dev *dev, char *fw_version, size_t fw_size)
3544 {
3545         struct ice_hw *hw = ICE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
3546         u32 full_ver;
3547         u8 ver, patch;
3548         u16 build;
3549         int ret;
3550
3551         full_ver = hw->nvm.oem_ver;
3552         ver = (u8)(full_ver >> 24);
3553         build = (u16)((full_ver >> 8) & 0xffff);
3554         patch = (u8)(full_ver & 0xff);
3555
3556         ret = snprintf(fw_version, fw_size,
3557                         "%d.%d%d 0x%08x %d.%d.%d",
3558                         ((hw->nvm.ver >> 12) & 0xf),
3559                         ((hw->nvm.ver >> 4) & 0xff),
3560                         (hw->nvm.ver & 0xf), hw->nvm.eetrack,
3561                         ver, build, patch);
3562
3563         /* add the size of '\0' */
3564         ret += 1;
3565         if (fw_size < (u32)ret)
3566                 return ret;
3567         else
3568                 return 0;
3569 }
3570
3571 static int
3572 ice_vsi_vlan_pvid_set(struct ice_vsi *vsi, struct ice_vsi_vlan_pvid_info *info)
3573 {
3574         struct ice_hw *hw;
3575         struct ice_vsi_ctx ctxt;
3576         uint8_t vlan_flags = 0;
3577         int ret;
3578
3579         if (!vsi || !info) {
3580                 PMD_DRV_LOG(ERR, "invalid parameters");
3581                 return -EINVAL;
3582         }
3583
3584         if (info->on) {
3585                 vsi->info.pvid = info->config.pvid;
3586                 /**
3587                  * If insert pvid is enabled, only tagged pkts are
3588                  * allowed to be sent out.
3589                  */
3590                 vlan_flags = ICE_AQ_VSI_PVLAN_INSERT_PVID |
3591                              ICE_AQ_VSI_VLAN_MODE_UNTAGGED;
3592         } else {
3593                 vsi->info.pvid = 0;
3594                 if (info->config.reject.tagged == 0)
3595                         vlan_flags |= ICE_AQ_VSI_VLAN_MODE_TAGGED;
3596
3597                 if (info->config.reject.untagged == 0)
3598                         vlan_flags |= ICE_AQ_VSI_VLAN_MODE_UNTAGGED;
3599         }
3600         vsi->info.vlan_flags &= ~(ICE_AQ_VSI_PVLAN_INSERT_PVID |
3601                                   ICE_AQ_VSI_VLAN_MODE_M);
3602         vsi->info.vlan_flags |= vlan_flags;
3603         memset(&ctxt, 0, sizeof(ctxt));
3604         rte_memcpy(&ctxt.info, &vsi->info, sizeof(vsi->info));
3605         ctxt.info.valid_sections =
3606                 rte_cpu_to_le_16(ICE_AQ_VSI_PROP_VLAN_VALID);
3607         ctxt.vsi_num = vsi->vsi_id;
3608
3609         hw = ICE_VSI_TO_HW(vsi);
3610         ret = ice_update_vsi(hw, vsi->idx, &ctxt, NULL);
3611         if (ret != ICE_SUCCESS) {
3612                 PMD_DRV_LOG(ERR,
3613                             "update VSI for VLAN insert failed, err %d",
3614                             ret);
3615                 return -EINVAL;
3616         }
3617
3618         vsi->info.valid_sections |=
3619                 rte_cpu_to_le_16(ICE_AQ_VSI_PROP_VLAN_VALID);
3620
3621         return ret;
3622 }
3623
3624 static int
3625 ice_vlan_pvid_set(struct rte_eth_dev *dev, uint16_t pvid, int on)
3626 {
3627         struct ice_pf *pf = ICE_DEV_PRIVATE_TO_PF(dev->data->dev_private);
3628         struct ice_vsi *vsi = pf->main_vsi;
3629         struct rte_eth_dev_data *data = pf->dev_data;
3630         struct ice_vsi_vlan_pvid_info info;
3631         int ret;
3632
3633         memset(&info, 0, sizeof(info));
3634         info.on = on;
3635         if (info.on) {
3636                 info.config.pvid = pvid;
3637         } else {
3638                 info.config.reject.tagged =
3639                         data->dev_conf.txmode.hw_vlan_reject_tagged;
3640                 info.config.reject.untagged =
3641                         data->dev_conf.txmode.hw_vlan_reject_untagged;
3642         }
3643
3644         ret = ice_vsi_vlan_pvid_set(vsi, &info);
3645         if (ret < 0) {
3646                 PMD_DRV_LOG(ERR, "Failed to set pvid.");
3647                 return -EINVAL;
3648         }
3649
3650         return 0;
3651 }
3652
3653 static int
3654 ice_get_eeprom_length(struct rte_eth_dev *dev)
3655 {
3656         struct ice_hw *hw = ICE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
3657
3658         /* Convert word count to byte count */
3659         return hw->nvm.sr_words << 1;
3660 }
3661
3662 static int
3663 ice_get_eeprom(struct rte_eth_dev *dev,
3664                struct rte_dev_eeprom_info *eeprom)
3665 {
3666         struct ice_hw *hw = ICE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
3667         uint16_t *data = eeprom->data;
3668         uint16_t first_word, last_word, nwords;
3669         enum ice_status status = ICE_SUCCESS;
3670
3671         first_word = eeprom->offset >> 1;
3672         last_word = (eeprom->offset + eeprom->length - 1) >> 1;
3673         nwords = last_word - first_word + 1;
3674
3675         if (first_word >= hw->nvm.sr_words ||
3676             last_word >= hw->nvm.sr_words) {
3677                 PMD_DRV_LOG(ERR, "Requested EEPROM bytes out of range.");
3678                 return -EINVAL;
3679         }
3680
3681         eeprom->magic = hw->vendor_id | (hw->device_id << 16);
3682
3683         status = ice_read_sr_buf(hw, first_word, &nwords, data);
3684         if (status) {
3685                 PMD_DRV_LOG(ERR, "EEPROM read failed.");
3686                 eeprom->length = sizeof(uint16_t) * nwords;
3687                 return -EIO;
3688         }
3689
3690         return 0;
3691 }
3692
3693 static void
3694 ice_stat_update_32(struct ice_hw *hw,
3695                    uint32_t reg,
3696                    bool offset_loaded,
3697                    uint64_t *offset,
3698                    uint64_t *stat)
3699 {
3700         uint64_t new_data;
3701
3702         new_data = (uint64_t)ICE_READ_REG(hw, reg);
3703         if (!offset_loaded)
3704                 *offset = new_data;
3705
3706         if (new_data >= *offset)
3707                 *stat = (uint64_t)(new_data - *offset);
3708         else
3709                 *stat = (uint64_t)((new_data +
3710                                     ((uint64_t)1 << ICE_32_BIT_WIDTH))
3711                                    - *offset);
3712 }
3713
3714 static void
3715 ice_stat_update_40(struct ice_hw *hw,
3716                    uint32_t hireg,
3717                    uint32_t loreg,
3718                    bool offset_loaded,
3719                    uint64_t *offset,
3720                    uint64_t *stat)
3721 {
3722         uint64_t new_data;
3723
3724         new_data = (uint64_t)ICE_READ_REG(hw, loreg);
3725         new_data |= (uint64_t)(ICE_READ_REG(hw, hireg) & ICE_8_BIT_MASK) <<
3726                     ICE_32_BIT_WIDTH;
3727
3728         if (!offset_loaded)
3729                 *offset = new_data;
3730
3731         if (new_data >= *offset)
3732                 *stat = new_data - *offset;
3733         else
3734                 *stat = (uint64_t)((new_data +
3735                                     ((uint64_t)1 << ICE_40_BIT_WIDTH)) -
3736                                    *offset);
3737
3738         *stat &= ICE_40_BIT_MASK;
3739 }
3740
3741 /* Get all the statistics of a VSI */
3742 static void
3743 ice_update_vsi_stats(struct ice_vsi *vsi)
3744 {
3745         struct ice_eth_stats *oes = &vsi->eth_stats_offset;
3746         struct ice_eth_stats *nes = &vsi->eth_stats;
3747         struct ice_hw *hw = ICE_VSI_TO_HW(vsi);
3748         int idx = rte_le_to_cpu_16(vsi->vsi_id);
3749
3750         ice_stat_update_40(hw, GLV_GORCH(idx), GLV_GORCL(idx),
3751                            vsi->offset_loaded, &oes->rx_bytes,
3752                            &nes->rx_bytes);
3753         ice_stat_update_40(hw, GLV_UPRCH(idx), GLV_UPRCL(idx),
3754                            vsi->offset_loaded, &oes->rx_unicast,
3755                            &nes->rx_unicast);
3756         ice_stat_update_40(hw, GLV_MPRCH(idx), GLV_MPRCL(idx),
3757                            vsi->offset_loaded, &oes->rx_multicast,
3758                            &nes->rx_multicast);
3759         ice_stat_update_40(hw, GLV_BPRCH(idx), GLV_BPRCL(idx),
3760                            vsi->offset_loaded, &oes->rx_broadcast,
3761                            &nes->rx_broadcast);
3762         /* exclude CRC bytes */
3763         nes->rx_bytes -= (nes->rx_unicast + nes->rx_multicast +
3764                           nes->rx_broadcast) * RTE_ETHER_CRC_LEN;
3765
3766         ice_stat_update_32(hw, GLV_RDPC(idx), vsi->offset_loaded,
3767                            &oes->rx_discards, &nes->rx_discards);
3768         /* GLV_REPC not supported */
3769         /* GLV_RMPC not supported */
3770         ice_stat_update_32(hw, GLSWID_RUPP(idx), vsi->offset_loaded,
3771                            &oes->rx_unknown_protocol,
3772                            &nes->rx_unknown_protocol);
3773         ice_stat_update_40(hw, GLV_GOTCH(idx), GLV_GOTCL(idx),
3774                            vsi->offset_loaded, &oes->tx_bytes,
3775                            &nes->tx_bytes);
3776         ice_stat_update_40(hw, GLV_UPTCH(idx), GLV_UPTCL(idx),
3777                            vsi->offset_loaded, &oes->tx_unicast,
3778                            &nes->tx_unicast);
3779         ice_stat_update_40(hw, GLV_MPTCH(idx), GLV_MPTCL(idx),
3780                            vsi->offset_loaded, &oes->tx_multicast,
3781                            &nes->tx_multicast);
3782         ice_stat_update_40(hw, GLV_BPTCH(idx), GLV_BPTCL(idx),
3783                            vsi->offset_loaded,  &oes->tx_broadcast,
3784                            &nes->tx_broadcast);
3785         /* GLV_TDPC not supported */
3786         ice_stat_update_32(hw, GLV_TEPC(idx), vsi->offset_loaded,
3787                            &oes->tx_errors, &nes->tx_errors);
3788         vsi->offset_loaded = true;
3789
3790         PMD_DRV_LOG(DEBUG, "************** VSI[%u] stats start **************",
3791                     vsi->vsi_id);
3792         PMD_DRV_LOG(DEBUG, "rx_bytes:            %"PRIu64"", nes->rx_bytes);
3793         PMD_DRV_LOG(DEBUG, "rx_unicast:          %"PRIu64"", nes->rx_unicast);
3794         PMD_DRV_LOG(DEBUG, "rx_multicast:        %"PRIu64"", nes->rx_multicast);
3795         PMD_DRV_LOG(DEBUG, "rx_broadcast:        %"PRIu64"", nes->rx_broadcast);
3796         PMD_DRV_LOG(DEBUG, "rx_discards:         %"PRIu64"", nes->rx_discards);
3797         PMD_DRV_LOG(DEBUG, "rx_unknown_protocol: %"PRIu64"",
3798                     nes->rx_unknown_protocol);
3799         PMD_DRV_LOG(DEBUG, "tx_bytes:            %"PRIu64"", nes->tx_bytes);
3800         PMD_DRV_LOG(DEBUG, "tx_unicast:          %"PRIu64"", nes->tx_unicast);
3801         PMD_DRV_LOG(DEBUG, "tx_multicast:        %"PRIu64"", nes->tx_multicast);
3802         PMD_DRV_LOG(DEBUG, "tx_broadcast:        %"PRIu64"", nes->tx_broadcast);
3803         PMD_DRV_LOG(DEBUG, "tx_discards:         %"PRIu64"", nes->tx_discards);
3804         PMD_DRV_LOG(DEBUG, "tx_errors:           %"PRIu64"", nes->tx_errors);
3805         PMD_DRV_LOG(DEBUG, "************** VSI[%u] stats end ****************",
3806                     vsi->vsi_id);
3807 }
3808
3809 static void
3810 ice_read_stats_registers(struct ice_pf *pf, struct ice_hw *hw)
3811 {
3812         struct ice_hw_port_stats *ns = &pf->stats; /* new stats */
3813         struct ice_hw_port_stats *os = &pf->stats_offset; /* old stats */
3814
3815         /* Get statistics of struct ice_eth_stats */
3816         ice_stat_update_40(hw, GLPRT_GORCH(hw->port_info->lport),
3817                            GLPRT_GORCL(hw->port_info->lport),
3818                            pf->offset_loaded, &os->eth.rx_bytes,
3819                            &ns->eth.rx_bytes);
3820         ice_stat_update_40(hw, GLPRT_UPRCH(hw->port_info->lport),
3821                            GLPRT_UPRCL(hw->port_info->lport),
3822                            pf->offset_loaded, &os->eth.rx_unicast,
3823                            &ns->eth.rx_unicast);
3824         ice_stat_update_40(hw, GLPRT_MPRCH(hw->port_info->lport),
3825                            GLPRT_MPRCL(hw->port_info->lport),
3826                            pf->offset_loaded, &os->eth.rx_multicast,
3827                            &ns->eth.rx_multicast);
3828         ice_stat_update_40(hw, GLPRT_BPRCH(hw->port_info->lport),
3829                            GLPRT_BPRCL(hw->port_info->lport),
3830                            pf->offset_loaded, &os->eth.rx_broadcast,
3831                            &ns->eth.rx_broadcast);
3832         ice_stat_update_32(hw, PRTRPB_RDPC,
3833                            pf->offset_loaded, &os->eth.rx_discards,
3834                            &ns->eth.rx_discards);
3835
3836         /* Workaround: CRC size should not be included in byte statistics,
3837          * so subtract RTE_ETHER_CRC_LEN from the byte counter for each rx
3838          * packet.
3839          */
3840         ns->eth.rx_bytes -= (ns->eth.rx_unicast + ns->eth.rx_multicast +
3841                              ns->eth.rx_broadcast) * RTE_ETHER_CRC_LEN;
3842
3843         /* GLPRT_REPC not supported */
3844         /* GLPRT_RMPC not supported */
3845         ice_stat_update_32(hw, GLSWID_RUPP(hw->port_info->lport),
3846                            pf->offset_loaded,
3847                            &os->eth.rx_unknown_protocol,
3848                            &ns->eth.rx_unknown_protocol);
3849         ice_stat_update_40(hw, GLPRT_GOTCH(hw->port_info->lport),
3850                            GLPRT_GOTCL(hw->port_info->lport),
3851                            pf->offset_loaded, &os->eth.tx_bytes,
3852                            &ns->eth.tx_bytes);
3853         ice_stat_update_40(hw, GLPRT_UPTCH(hw->port_info->lport),
3854                            GLPRT_UPTCL(hw->port_info->lport),
3855                            pf->offset_loaded, &os->eth.tx_unicast,
3856                            &ns->eth.tx_unicast);
3857         ice_stat_update_40(hw, GLPRT_MPTCH(hw->port_info->lport),
3858                            GLPRT_MPTCL(hw->port_info->lport),
3859                            pf->offset_loaded, &os->eth.tx_multicast,
3860                            &ns->eth.tx_multicast);
3861         ice_stat_update_40(hw, GLPRT_BPTCH(hw->port_info->lport),
3862                            GLPRT_BPTCL(hw->port_info->lport),
3863                            pf->offset_loaded, &os->eth.tx_broadcast,
3864                            &ns->eth.tx_broadcast);
3865         ns->eth.tx_bytes -= (ns->eth.tx_unicast + ns->eth.tx_multicast +
3866                              ns->eth.tx_broadcast) * RTE_ETHER_CRC_LEN;
3867
3868         /* GLPRT_TEPC not supported */
3869
3870         /* additional port specific stats */
3871         ice_stat_update_32(hw, GLPRT_TDOLD(hw->port_info->lport),
3872                            pf->offset_loaded, &os->tx_dropped_link_down,
3873                            &ns->tx_dropped_link_down);
3874         ice_stat_update_32(hw, GLPRT_CRCERRS(hw->port_info->lport),
3875                            pf->offset_loaded, &os->crc_errors,
3876                            &ns->crc_errors);
3877         ice_stat_update_32(hw, GLPRT_ILLERRC(hw->port_info->lport),
3878                            pf->offset_loaded, &os->illegal_bytes,
3879                            &ns->illegal_bytes);
3880         /* GLPRT_ERRBC not supported */
3881         ice_stat_update_32(hw, GLPRT_MLFC(hw->port_info->lport),
3882                            pf->offset_loaded, &os->mac_local_faults,
3883                            &ns->mac_local_faults);
3884         ice_stat_update_32(hw, GLPRT_MRFC(hw->port_info->lport),
3885                            pf->offset_loaded, &os->mac_remote_faults,
3886                            &ns->mac_remote_faults);
3887
3888         ice_stat_update_32(hw, GLPRT_RLEC(hw->port_info->lport),
3889                            pf->offset_loaded, &os->rx_len_errors,
3890                            &ns->rx_len_errors);
3891
3892         ice_stat_update_32(hw, GLPRT_LXONRXC(hw->port_info->lport),
3893                            pf->offset_loaded, &os->link_xon_rx,
3894                            &ns->link_xon_rx);
3895         ice_stat_update_32(hw, GLPRT_LXOFFRXC(hw->port_info->lport),
3896                            pf->offset_loaded, &os->link_xoff_rx,
3897                            &ns->link_xoff_rx);
3898         ice_stat_update_32(hw, GLPRT_LXONTXC(hw->port_info->lport),
3899                            pf->offset_loaded, &os->link_xon_tx,
3900                            &ns->link_xon_tx);
3901         ice_stat_update_32(hw, GLPRT_LXOFFTXC(hw->port_info->lport),
3902                            pf->offset_loaded, &os->link_xoff_tx,
3903                            &ns->link_xoff_tx);
3904         ice_stat_update_40(hw, GLPRT_PRC64H(hw->port_info->lport),
3905                            GLPRT_PRC64L(hw->port_info->lport),
3906                            pf->offset_loaded, &os->rx_size_64,
3907                            &ns->rx_size_64);
3908         ice_stat_update_40(hw, GLPRT_PRC127H(hw->port_info->lport),
3909                            GLPRT_PRC127L(hw->port_info->lport),
3910                            pf->offset_loaded, &os->rx_size_127,
3911                            &ns->rx_size_127);
3912         ice_stat_update_40(hw, GLPRT_PRC255H(hw->port_info->lport),
3913                            GLPRT_PRC255L(hw->port_info->lport),
3914                            pf->offset_loaded, &os->rx_size_255,
3915                            &ns->rx_size_255);
3916         ice_stat_update_40(hw, GLPRT_PRC511H(hw->port_info->lport),
3917                            GLPRT_PRC511L(hw->port_info->lport),
3918                            pf->offset_loaded, &os->rx_size_511,
3919                            &ns->rx_size_511);
3920         ice_stat_update_40(hw, GLPRT_PRC1023H(hw->port_info->lport),
3921                            GLPRT_PRC1023L(hw->port_info->lport),
3922                            pf->offset_loaded, &os->rx_size_1023,
3923                            &ns->rx_size_1023);
3924         ice_stat_update_40(hw, GLPRT_PRC1522H(hw->port_info->lport),
3925                            GLPRT_PRC1522L(hw->port_info->lport),
3926                            pf->offset_loaded, &os->rx_size_1522,
3927                            &ns->rx_size_1522);
3928         ice_stat_update_40(hw, GLPRT_PRC9522H(hw->port_info->lport),
3929                            GLPRT_PRC9522L(hw->port_info->lport),
3930                            pf->offset_loaded, &os->rx_size_big,
3931                            &ns->rx_size_big);
3932         ice_stat_update_32(hw, GLPRT_RUC(hw->port_info->lport),
3933                            pf->offset_loaded, &os->rx_undersize,
3934                            &ns->rx_undersize);
3935         ice_stat_update_32(hw, GLPRT_RFC(hw->port_info->lport),
3936                            pf->offset_loaded, &os->rx_fragments,
3937                            &ns->rx_fragments);
3938         ice_stat_update_32(hw, GLPRT_ROC(hw->port_info->lport),
3939                            pf->offset_loaded, &os->rx_oversize,
3940                            &ns->rx_oversize);
3941         ice_stat_update_32(hw, GLPRT_RJC(hw->port_info->lport),
3942                            pf->offset_loaded, &os->rx_jabber,
3943                            &ns->rx_jabber);
3944         ice_stat_update_40(hw, GLPRT_PTC64H(hw->port_info->lport),
3945                            GLPRT_PTC64L(hw->port_info->lport),
3946                            pf->offset_loaded, &os->tx_size_64,
3947                            &ns->tx_size_64);
3948         ice_stat_update_40(hw, GLPRT_PTC127H(hw->port_info->lport),
3949                            GLPRT_PTC127L(hw->port_info->lport),
3950                            pf->offset_loaded, &os->tx_size_127,
3951                            &ns->tx_size_127);
3952         ice_stat_update_40(hw, GLPRT_PTC255H(hw->port_info->lport),
3953                            GLPRT_PTC255L(hw->port_info->lport),
3954                            pf->offset_loaded, &os->tx_size_255,
3955                            &ns->tx_size_255);
3956         ice_stat_update_40(hw, GLPRT_PTC511H(hw->port_info->lport),
3957                            GLPRT_PTC511L(hw->port_info->lport),
3958                            pf->offset_loaded, &os->tx_size_511,
3959                            &ns->tx_size_511);
3960         ice_stat_update_40(hw, GLPRT_PTC1023H(hw->port_info->lport),
3961                            GLPRT_PTC1023L(hw->port_info->lport),
3962                            pf->offset_loaded, &os->tx_size_1023,
3963                            &ns->tx_size_1023);
3964         ice_stat_update_40(hw, GLPRT_PTC1522H(hw->port_info->lport),
3965                            GLPRT_PTC1522L(hw->port_info->lport),
3966                            pf->offset_loaded, &os->tx_size_1522,
3967                            &ns->tx_size_1522);
3968         ice_stat_update_40(hw, GLPRT_PTC9522H(hw->port_info->lport),
3969                            GLPRT_PTC9522L(hw->port_info->lport),
3970                            pf->offset_loaded, &os->tx_size_big,
3971                            &ns->tx_size_big);
3972
3973         /* GLPRT_MSPDC not supported */
3974         /* GLPRT_XEC not supported */
3975
3976         pf->offset_loaded = true;
3977
3978         if (pf->main_vsi)
3979                 ice_update_vsi_stats(pf->main_vsi);
3980 }
3981
3982 /* Get all statistics of a port */
3983 static int
3984 ice_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
3985 {
3986         struct ice_pf *pf = ICE_DEV_PRIVATE_TO_PF(dev->data->dev_private);
3987         struct ice_hw *hw = ICE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
3988         struct ice_hw_port_stats *ns = &pf->stats; /* new stats */
3989
3990         /* call read registers - updates values, now write them to struct */
3991         ice_read_stats_registers(pf, hw);
3992
3993         stats->ipackets = pf->main_vsi->eth_stats.rx_unicast +
3994                           pf->main_vsi->eth_stats.rx_multicast +
3995                           pf->main_vsi->eth_stats.rx_broadcast -
3996                           pf->main_vsi->eth_stats.rx_discards;
3997         stats->opackets = ns->eth.tx_unicast +
3998                           ns->eth.tx_multicast +
3999                           ns->eth.tx_broadcast;
4000         stats->ibytes   = pf->main_vsi->eth_stats.rx_bytes;
4001         stats->obytes   = ns->eth.tx_bytes;
4002         stats->oerrors  = ns->eth.tx_errors +
4003                           pf->main_vsi->eth_stats.tx_errors;
4004
4005         /* Rx Errors */
4006         stats->imissed  = ns->eth.rx_discards +
4007                           pf->main_vsi->eth_stats.rx_discards;
4008         stats->ierrors  = ns->crc_errors +
4009                           ns->rx_undersize +
4010                           ns->rx_oversize + ns->rx_fragments + ns->rx_jabber;
4011
4012         PMD_DRV_LOG(DEBUG, "*************** PF stats start *****************");
4013         PMD_DRV_LOG(DEBUG, "rx_bytes:   %"PRIu64"", ns->eth.rx_bytes);
4014         PMD_DRV_LOG(DEBUG, "rx_unicast: %"PRIu64"", ns->eth.rx_unicast);
4015         PMD_DRV_LOG(DEBUG, "rx_multicast:%"PRIu64"", ns->eth.rx_multicast);
4016         PMD_DRV_LOG(DEBUG, "rx_broadcast:%"PRIu64"", ns->eth.rx_broadcast);
4017         PMD_DRV_LOG(DEBUG, "rx_discards:%"PRIu64"", ns->eth.rx_discards);
4018         PMD_DRV_LOG(DEBUG, "vsi rx_discards:%"PRIu64"",
4019                     pf->main_vsi->eth_stats.rx_discards);
4020         PMD_DRV_LOG(DEBUG, "rx_unknown_protocol:  %"PRIu64"",
4021                     ns->eth.rx_unknown_protocol);
4022         PMD_DRV_LOG(DEBUG, "tx_bytes:   %"PRIu64"", ns->eth.tx_bytes);
4023         PMD_DRV_LOG(DEBUG, "tx_unicast: %"PRIu64"", ns->eth.tx_unicast);
4024         PMD_DRV_LOG(DEBUG, "tx_multicast:%"PRIu64"", ns->eth.tx_multicast);
4025         PMD_DRV_LOG(DEBUG, "tx_broadcast:%"PRIu64"", ns->eth.tx_broadcast);
4026         PMD_DRV_LOG(DEBUG, "tx_discards:%"PRIu64"", ns->eth.tx_discards);
4027         PMD_DRV_LOG(DEBUG, "vsi tx_discards:%"PRIu64"",
4028                     pf->main_vsi->eth_stats.tx_discards);
4029         PMD_DRV_LOG(DEBUG, "tx_errors:          %"PRIu64"", ns->eth.tx_errors);
4030
4031         PMD_DRV_LOG(DEBUG, "tx_dropped_link_down:       %"PRIu64"",
4032                     ns->tx_dropped_link_down);
4033         PMD_DRV_LOG(DEBUG, "crc_errors: %"PRIu64"", ns->crc_errors);
4034         PMD_DRV_LOG(DEBUG, "illegal_bytes:      %"PRIu64"",
4035                     ns->illegal_bytes);
4036         PMD_DRV_LOG(DEBUG, "error_bytes:        %"PRIu64"", ns->error_bytes);
4037         PMD_DRV_LOG(DEBUG, "mac_local_faults:   %"PRIu64"",
4038                     ns->mac_local_faults);
4039         PMD_DRV_LOG(DEBUG, "mac_remote_faults:  %"PRIu64"",
4040                     ns->mac_remote_faults);
4041         PMD_DRV_LOG(DEBUG, "link_xon_rx:        %"PRIu64"", ns->link_xon_rx);
4042         PMD_DRV_LOG(DEBUG, "link_xoff_rx:       %"PRIu64"", ns->link_xoff_rx);
4043         PMD_DRV_LOG(DEBUG, "link_xon_tx:        %"PRIu64"", ns->link_xon_tx);
4044         PMD_DRV_LOG(DEBUG, "link_xoff_tx:       %"PRIu64"", ns->link_xoff_tx);
4045         PMD_DRV_LOG(DEBUG, "rx_size_64:         %"PRIu64"", ns->rx_size_64);
4046         PMD_DRV_LOG(DEBUG, "rx_size_127:        %"PRIu64"", ns->rx_size_127);
4047         PMD_DRV_LOG(DEBUG, "rx_size_255:        %"PRIu64"", ns->rx_size_255);
4048         PMD_DRV_LOG(DEBUG, "rx_size_511:        %"PRIu64"", ns->rx_size_511);
4049         PMD_DRV_LOG(DEBUG, "rx_size_1023:       %"PRIu64"", ns->rx_size_1023);
4050         PMD_DRV_LOG(DEBUG, "rx_size_1522:       %"PRIu64"", ns->rx_size_1522);
4051         PMD_DRV_LOG(DEBUG, "rx_size_big:        %"PRIu64"", ns->rx_size_big);
4052         PMD_DRV_LOG(DEBUG, "rx_undersize:       %"PRIu64"", ns->rx_undersize);
4053         PMD_DRV_LOG(DEBUG, "rx_fragments:       %"PRIu64"", ns->rx_fragments);
4054         PMD_DRV_LOG(DEBUG, "rx_oversize:        %"PRIu64"", ns->rx_oversize);
4055         PMD_DRV_LOG(DEBUG, "rx_jabber:          %"PRIu64"", ns->rx_jabber);
4056         PMD_DRV_LOG(DEBUG, "tx_size_64:         %"PRIu64"", ns->tx_size_64);
4057         PMD_DRV_LOG(DEBUG, "tx_size_127:        %"PRIu64"", ns->tx_size_127);
4058         PMD_DRV_LOG(DEBUG, "tx_size_255:        %"PRIu64"", ns->tx_size_255);
4059         PMD_DRV_LOG(DEBUG, "tx_size_511:        %"PRIu64"", ns->tx_size_511);
4060         PMD_DRV_LOG(DEBUG, "tx_size_1023:       %"PRIu64"", ns->tx_size_1023);
4061         PMD_DRV_LOG(DEBUG, "tx_size_1522:       %"PRIu64"", ns->tx_size_1522);
4062         PMD_DRV_LOG(DEBUG, "tx_size_big:        %"PRIu64"", ns->tx_size_big);
4063         PMD_DRV_LOG(DEBUG, "rx_len_errors:      %"PRIu64"", ns->rx_len_errors);
4064         PMD_DRV_LOG(DEBUG, "************* PF stats end ****************");
4065         return 0;
4066 }
4067
4068 /* Reset the statistics */
4069 static int
4070 ice_stats_reset(struct rte_eth_dev *dev)
4071 {
4072         struct ice_pf *pf = ICE_DEV_PRIVATE_TO_PF(dev->data->dev_private);
4073         struct ice_hw *hw = ICE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
4074
4075         /* Mark PF and VSI stats to update the offset, aka "reset" */
4076         pf->offset_loaded = false;
4077         if (pf->main_vsi)
4078                 pf->main_vsi->offset_loaded = false;
4079
4080         /* read the stats, reading current register values into offset */
4081         ice_read_stats_registers(pf, hw);
4082
4083         return 0;
4084 }
4085
4086 static uint32_t
4087 ice_xstats_calc_num(void)
4088 {
4089         uint32_t num;
4090
4091         num = ICE_NB_ETH_XSTATS + ICE_NB_HW_PORT_XSTATS;
4092
4093         return num;
4094 }
4095
4096 static int
4097 ice_xstats_get(struct rte_eth_dev *dev, struct rte_eth_xstat *xstats,
4098                unsigned int n)
4099 {
4100         struct ice_pf *pf = ICE_DEV_PRIVATE_TO_PF(dev->data->dev_private);
4101         struct ice_hw *hw = ICE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
4102         unsigned int i;
4103         unsigned int count;
4104         struct ice_hw_port_stats *hw_stats = &pf->stats;
4105
4106         count = ice_xstats_calc_num();
4107         if (n < count)
4108                 return count;
4109
4110         ice_read_stats_registers(pf, hw);
4111
4112         if (!xstats)
4113                 return 0;
4114
4115         count = 0;
4116
4117         /* Get stats from ice_eth_stats struct */
4118         for (i = 0; i < ICE_NB_ETH_XSTATS; i++) {
4119                 xstats[count].value =
4120                         *(uint64_t *)((char *)&hw_stats->eth +
4121                                       ice_stats_strings[i].offset);
4122                 xstats[count].id = count;
4123                 count++;
4124         }
4125
4126         /* Get individiual stats from ice_hw_port struct */
4127         for (i = 0; i < ICE_NB_HW_PORT_XSTATS; i++) {
4128                 xstats[count].value =
4129                         *(uint64_t *)((char *)hw_stats +
4130                                       ice_hw_port_strings[i].offset);
4131                 xstats[count].id = count;
4132                 count++;
4133         }
4134
4135         return count;
4136 }
4137
4138 static int ice_xstats_get_names(__rte_unused struct rte_eth_dev *dev,
4139                                 struct rte_eth_xstat_name *xstats_names,
4140                                 __rte_unused unsigned int limit)
4141 {
4142         unsigned int count = 0;
4143         unsigned int i;
4144
4145         if (!xstats_names)
4146                 return ice_xstats_calc_num();
4147
4148         /* Note: limit checked in rte_eth_xstats_names() */
4149
4150         /* Get stats from ice_eth_stats struct */
4151         for (i = 0; i < ICE_NB_ETH_XSTATS; i++) {
4152                 strlcpy(xstats_names[count].name, ice_stats_strings[i].name,
4153                         sizeof(xstats_names[count].name));
4154                 count++;
4155         }
4156
4157         /* Get individiual stats from ice_hw_port struct */
4158         for (i = 0; i < ICE_NB_HW_PORT_XSTATS; i++) {
4159                 strlcpy(xstats_names[count].name, ice_hw_port_strings[i].name,
4160                         sizeof(xstats_names[count].name));
4161                 count++;
4162         }
4163
4164         return count;
4165 }
4166
4167 static int
4168 ice_dev_filter_ctrl(struct rte_eth_dev *dev,
4169                      enum rte_filter_type filter_type,
4170                      enum rte_filter_op filter_op,
4171                      void *arg)
4172 {
4173         int ret = 0;
4174
4175         if (!dev)
4176                 return -EINVAL;
4177
4178         switch (filter_type) {
4179         case RTE_ETH_FILTER_GENERIC:
4180                 if (filter_op != RTE_ETH_FILTER_GET)
4181                         return -EINVAL;
4182                 *(const void **)arg = &ice_flow_ops;
4183                 break;
4184         default:
4185                 PMD_DRV_LOG(WARNING, "Filter type (%d) not supported",
4186                                         filter_type);
4187                 ret = -EINVAL;
4188                 break;
4189         }
4190
4191         return ret;
4192 }
4193
4194 /* Add UDP tunneling port */
4195 static int
4196 ice_dev_udp_tunnel_port_add(struct rte_eth_dev *dev,
4197                              struct rte_eth_udp_tunnel *udp_tunnel)
4198 {
4199         int ret = 0;
4200         struct ice_hw *hw = ICE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
4201
4202         if (udp_tunnel == NULL)
4203                 return -EINVAL;
4204
4205         switch (udp_tunnel->prot_type) {
4206         case RTE_TUNNEL_TYPE_VXLAN:
4207                 ret = ice_create_tunnel(hw, TNL_VXLAN, udp_tunnel->udp_port);
4208                 break;
4209         default:
4210                 PMD_DRV_LOG(ERR, "Invalid tunnel type");
4211                 ret = -EINVAL;
4212                 break;
4213         }
4214
4215         return ret;
4216 }
4217
4218 /* Delete UDP tunneling port */
4219 static int
4220 ice_dev_udp_tunnel_port_del(struct rte_eth_dev *dev,
4221                              struct rte_eth_udp_tunnel *udp_tunnel)
4222 {
4223         int ret = 0;
4224         struct ice_hw *hw = ICE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
4225
4226         if (udp_tunnel == NULL)
4227                 return -EINVAL;
4228
4229         switch (udp_tunnel->prot_type) {
4230         case RTE_TUNNEL_TYPE_VXLAN:
4231                 ret = ice_destroy_tunnel(hw, udp_tunnel->udp_port, 0);
4232                 break;
4233         default:
4234                 PMD_DRV_LOG(ERR, "Invalid tunnel type");
4235                 ret = -EINVAL;
4236                 break;
4237         }
4238
4239         return ret;
4240 }
4241
4242 static int
4243 ice_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
4244               struct rte_pci_device *pci_dev)
4245 {
4246         return rte_eth_dev_pci_generic_probe(pci_dev,
4247                                              sizeof(struct ice_adapter),
4248                                              ice_dev_init);
4249 }
4250
4251 static int
4252 ice_pci_remove(struct rte_pci_device *pci_dev)
4253 {
4254         return rte_eth_dev_pci_generic_remove(pci_dev, ice_dev_uninit);
4255 }
4256
4257 static struct rte_pci_driver rte_ice_pmd = {
4258         .id_table = pci_id_ice_map,
4259         .drv_flags = RTE_PCI_DRV_NEED_MAPPING | RTE_PCI_DRV_INTR_LSC,
4260         .probe = ice_pci_probe,
4261         .remove = ice_pci_remove,
4262 };
4263
4264 /**
4265  * Driver initialization routine.
4266  * Invoked once at EAL init time.
4267  * Register itself as the [Poll Mode] Driver of PCI devices.
4268  */
4269 RTE_PMD_REGISTER_PCI(net_ice, rte_ice_pmd);
4270 RTE_PMD_REGISTER_PCI_TABLE(net_ice, pci_id_ice_map);
4271 RTE_PMD_REGISTER_KMOD_DEP(net_ice, "* igb_uio | uio_pci_generic | vfio-pci");
4272 RTE_PMD_REGISTER_PARAM_STRING(net_ice,
4273                               ICE_PROTO_XTR_ARG "=[queue:]<vlan|ipv4|ipv6|ipv6_flow|tcp>"
4274                               ICE_SAFE_MODE_SUPPORT_ARG "=<0|1>");
4275
4276 RTE_INIT(ice_init_log)
4277 {
4278         ice_logtype_init = rte_log_register("pmd.net.ice.init");
4279         if (ice_logtype_init >= 0)
4280                 rte_log_set_level(ice_logtype_init, RTE_LOG_NOTICE);
4281         ice_logtype_driver = rte_log_register("pmd.net.ice.driver");
4282         if (ice_logtype_driver >= 0)
4283                 rte_log_set_level(ice_logtype_driver, RTE_LOG_NOTICE);
4284 }