net/ice: support VLAN ops
[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_ethdev_pci.h>
6
7 #include "base/ice_sched.h"
8 #include "ice_ethdev.h"
9 #include "ice_rxtx.h"
10
11 #define ICE_MAX_QP_NUM "max_queue_pair_num"
12 #define ICE_DFLT_OUTER_TAG_TYPE ICE_AQ_VSI_OUTER_TAG_VLAN_9100
13
14 int ice_logtype_init;
15 int ice_logtype_driver;
16
17 static int ice_dev_configure(struct rte_eth_dev *dev);
18 static int ice_dev_start(struct rte_eth_dev *dev);
19 static void ice_dev_stop(struct rte_eth_dev *dev);
20 static void ice_dev_close(struct rte_eth_dev *dev);
21 static int ice_dev_reset(struct rte_eth_dev *dev);
22 static void ice_dev_info_get(struct rte_eth_dev *dev,
23                              struct rte_eth_dev_info *dev_info);
24 static int ice_link_update(struct rte_eth_dev *dev,
25                            int wait_to_complete);
26 static int ice_mtu_set(struct rte_eth_dev *dev, uint16_t mtu);
27 static int ice_vlan_offload_set(struct rte_eth_dev *dev, int mask);
28 static int ice_vlan_tpid_set(struct rte_eth_dev *dev,
29                              enum rte_vlan_type vlan_type,
30                              uint16_t tpid);
31 static int ice_vlan_filter_set(struct rte_eth_dev *dev,
32                                uint16_t vlan_id,
33                                int on);
34 static int ice_macaddr_set(struct rte_eth_dev *dev,
35                            struct ether_addr *mac_addr);
36 static int ice_macaddr_add(struct rte_eth_dev *dev,
37                            struct ether_addr *mac_addr,
38                            __rte_unused uint32_t index,
39                            uint32_t pool);
40 static void ice_macaddr_remove(struct rte_eth_dev *dev, uint32_t index);
41 static int ice_vlan_pvid_set(struct rte_eth_dev *dev,
42                              uint16_t pvid, int on);
43
44 static const struct rte_pci_id pci_id_ice_map[] = {
45         { RTE_PCI_DEVICE(ICE_INTEL_VENDOR_ID, ICE_DEV_ID_E810C_BACKPLANE) },
46         { RTE_PCI_DEVICE(ICE_INTEL_VENDOR_ID, ICE_DEV_ID_E810C_QSFP) },
47         { RTE_PCI_DEVICE(ICE_INTEL_VENDOR_ID, ICE_DEV_ID_E810C_SFP) },
48         { .vendor_id = 0, /* sentinel */ },
49 };
50
51 static const struct eth_dev_ops ice_eth_dev_ops = {
52         .dev_configure                = ice_dev_configure,
53         .dev_start                    = ice_dev_start,
54         .dev_stop                     = ice_dev_stop,
55         .dev_close                    = ice_dev_close,
56         .dev_reset                    = ice_dev_reset,
57         .rx_queue_start               = ice_rx_queue_start,
58         .rx_queue_stop                = ice_rx_queue_stop,
59         .tx_queue_start               = ice_tx_queue_start,
60         .tx_queue_stop                = ice_tx_queue_stop,
61         .rx_queue_setup               = ice_rx_queue_setup,
62         .rx_queue_release             = ice_rx_queue_release,
63         .tx_queue_setup               = ice_tx_queue_setup,
64         .tx_queue_release             = ice_tx_queue_release,
65         .dev_infos_get                = ice_dev_info_get,
66         .dev_supported_ptypes_get     = ice_dev_supported_ptypes_get,
67         .link_update                  = ice_link_update,
68         .mtu_set                      = ice_mtu_set,
69         .mac_addr_set                 = ice_macaddr_set,
70         .mac_addr_add                 = ice_macaddr_add,
71         .mac_addr_remove              = ice_macaddr_remove,
72         .vlan_filter_set              = ice_vlan_filter_set,
73         .vlan_offload_set             = ice_vlan_offload_set,
74         .vlan_tpid_set                = ice_vlan_tpid_set,
75         .vlan_pvid_set                = ice_vlan_pvid_set,
76         .rxq_info_get                 = ice_rxq_info_get,
77         .txq_info_get                 = ice_txq_info_get,
78         .rx_queue_count               = ice_rx_queue_count,
79 };
80
81 static void
82 ice_init_controlq_parameter(struct ice_hw *hw)
83 {
84         /* fields for adminq */
85         hw->adminq.num_rq_entries = ICE_ADMINQ_LEN;
86         hw->adminq.num_sq_entries = ICE_ADMINQ_LEN;
87         hw->adminq.rq_buf_size = ICE_ADMINQ_BUF_SZ;
88         hw->adminq.sq_buf_size = ICE_ADMINQ_BUF_SZ;
89
90         /* fields for mailboxq, DPDK used as PF host */
91         hw->mailboxq.num_rq_entries = ICE_MAILBOXQ_LEN;
92         hw->mailboxq.num_sq_entries = ICE_MAILBOXQ_LEN;
93         hw->mailboxq.rq_buf_size = ICE_MAILBOXQ_BUF_SZ;
94         hw->mailboxq.sq_buf_size = ICE_MAILBOXQ_BUF_SZ;
95 }
96
97 static int
98 ice_check_qp_num(const char *key, const char *qp_value,
99                  __rte_unused void *opaque)
100 {
101         char *end = NULL;
102         int num = 0;
103
104         while (isblank(*qp_value))
105                 qp_value++;
106
107         num = strtoul(qp_value, &end, 10);
108
109         if (!num || (*end == '-') || errno) {
110                 PMD_DRV_LOG(WARNING, "invalid value:\"%s\" for key:\"%s\", "
111                             "value must be > 0",
112                             qp_value, key);
113                 return -1;
114         }
115
116         return num;
117 }
118
119 static int
120 ice_config_max_queue_pair_num(struct rte_devargs *devargs)
121 {
122         struct rte_kvargs *kvlist;
123         const char *queue_num_key = ICE_MAX_QP_NUM;
124         int ret;
125
126         if (!devargs)
127                 return 0;
128
129         kvlist = rte_kvargs_parse(devargs->args, NULL);
130         if (!kvlist)
131                 return 0;
132
133         if (!rte_kvargs_count(kvlist, queue_num_key)) {
134                 rte_kvargs_free(kvlist);
135                 return 0;
136         }
137
138         if (rte_kvargs_process(kvlist, queue_num_key,
139                                ice_check_qp_num, NULL) < 0) {
140                 rte_kvargs_free(kvlist);
141                 return 0;
142         }
143         ret = rte_kvargs_process(kvlist, queue_num_key,
144                                  ice_check_qp_num, NULL);
145         rte_kvargs_free(kvlist);
146
147         return ret;
148 }
149
150 static int
151 ice_res_pool_init(struct ice_res_pool_info *pool, uint32_t base,
152                   uint32_t num)
153 {
154         struct pool_entry *entry;
155
156         if (!pool || !num)
157                 return -EINVAL;
158
159         entry = rte_zmalloc(NULL, sizeof(*entry), 0);
160         if (!entry) {
161                 PMD_INIT_LOG(ERR,
162                              "Failed to allocate memory for resource pool");
163                 return -ENOMEM;
164         }
165
166         /* queue heap initialize */
167         pool->num_free = num;
168         pool->num_alloc = 0;
169         pool->base = base;
170         LIST_INIT(&pool->alloc_list);
171         LIST_INIT(&pool->free_list);
172
173         /* Initialize element  */
174         entry->base = 0;
175         entry->len = num;
176
177         LIST_INSERT_HEAD(&pool->free_list, entry, next);
178         return 0;
179 }
180
181 static int
182 ice_res_pool_alloc(struct ice_res_pool_info *pool,
183                    uint16_t num)
184 {
185         struct pool_entry *entry, *valid_entry;
186
187         if (!pool || !num) {
188                 PMD_INIT_LOG(ERR, "Invalid parameter");
189                 return -EINVAL;
190         }
191
192         if (pool->num_free < num) {
193                 PMD_INIT_LOG(ERR, "No resource. ask:%u, available:%u",
194                              num, pool->num_free);
195                 return -ENOMEM;
196         }
197
198         valid_entry = NULL;
199         /* Lookup  in free list and find most fit one */
200         LIST_FOREACH(entry, &pool->free_list, next) {
201                 if (entry->len >= num) {
202                         /* Find best one */
203                         if (entry->len == num) {
204                                 valid_entry = entry;
205                                 break;
206                         }
207                         if (!valid_entry ||
208                             valid_entry->len > entry->len)
209                                 valid_entry = entry;
210                 }
211         }
212
213         /* Not find one to satisfy the request, return */
214         if (!valid_entry) {
215                 PMD_INIT_LOG(ERR, "No valid entry found");
216                 return -ENOMEM;
217         }
218         /**
219          * The entry have equal queue number as requested,
220          * remove it from alloc_list.
221          */
222         if (valid_entry->len == num) {
223                 LIST_REMOVE(valid_entry, next);
224         } else {
225                 /**
226                  * The entry have more numbers than requested,
227                  * create a new entry for alloc_list and minus its
228                  * queue base and number in free_list.
229                  */
230                 entry = rte_zmalloc(NULL, sizeof(*entry), 0);
231                 if (!entry) {
232                         PMD_INIT_LOG(ERR,
233                                      "Failed to allocate memory for "
234                                      "resource pool");
235                         return -ENOMEM;
236                 }
237                 entry->base = valid_entry->base;
238                 entry->len = num;
239                 valid_entry->base += num;
240                 valid_entry->len -= num;
241                 valid_entry = entry;
242         }
243
244         /* Insert it into alloc list, not sorted */
245         LIST_INSERT_HEAD(&pool->alloc_list, valid_entry, next);
246
247         pool->num_free -= valid_entry->len;
248         pool->num_alloc += valid_entry->len;
249
250         return valid_entry->base + pool->base;
251 }
252
253 static void
254 ice_res_pool_destroy(struct ice_res_pool_info *pool)
255 {
256         struct pool_entry *entry, *next_entry;
257
258         if (!pool)
259                 return;
260
261         for (entry = LIST_FIRST(&pool->alloc_list);
262              entry && (next_entry = LIST_NEXT(entry, next), 1);
263              entry = next_entry) {
264                 LIST_REMOVE(entry, next);
265                 rte_free(entry);
266         }
267
268         for (entry = LIST_FIRST(&pool->free_list);
269              entry && (next_entry = LIST_NEXT(entry, next), 1);
270              entry = next_entry) {
271                 LIST_REMOVE(entry, next);
272                 rte_free(entry);
273         }
274
275         pool->num_free = 0;
276         pool->num_alloc = 0;
277         pool->base = 0;
278         LIST_INIT(&pool->alloc_list);
279         LIST_INIT(&pool->free_list);
280 }
281
282 static void
283 ice_vsi_config_default_rss(struct ice_aqc_vsi_props *info)
284 {
285         /* Set VSI LUT selection */
286         info->q_opt_rss = ICE_AQ_VSI_Q_OPT_RSS_LUT_VSI &
287                           ICE_AQ_VSI_Q_OPT_RSS_LUT_M;
288         /* Set Hash scheme */
289         info->q_opt_rss |= ICE_AQ_VSI_Q_OPT_RSS_TPLZ &
290                            ICE_AQ_VSI_Q_OPT_RSS_HASH_M;
291         /* enable TC */
292         info->q_opt_tc = ICE_AQ_VSI_Q_OPT_TC_OVR_M;
293 }
294
295 static enum ice_status
296 ice_vsi_config_tc_queue_mapping(struct ice_vsi *vsi,
297                                 struct ice_aqc_vsi_props *info,
298                                 uint8_t enabled_tcmap)
299 {
300         uint16_t bsf, qp_idx;
301
302         /* default tc 0 now. Multi-TC supporting need to be done later.
303          * Configure TC and queue mapping parameters, for enabled TC,
304          * allocate qpnum_per_tc queues to this traffic.
305          */
306         if (enabled_tcmap != 0x01) {
307                 PMD_INIT_LOG(ERR, "only TC0 is supported");
308                 return -ENOTSUP;
309         }
310
311         vsi->nb_qps = RTE_MIN(vsi->nb_qps, ICE_MAX_Q_PER_TC);
312         bsf = rte_bsf32(vsi->nb_qps);
313         /* Adjust the queue number to actual queues that can be applied */
314         vsi->nb_qps = 0x1 << bsf;
315
316         qp_idx = 0;
317         /* Set tc and queue mapping with VSI */
318         info->tc_mapping[0] = rte_cpu_to_le_16((qp_idx <<
319                                                 ICE_AQ_VSI_TC_Q_OFFSET_S) |
320                                                (bsf << ICE_AQ_VSI_TC_Q_NUM_S));
321
322         /* Associate queue number with VSI */
323         info->mapping_flags |= rte_cpu_to_le_16(ICE_AQ_VSI_Q_MAP_CONTIG);
324         info->q_mapping[0] = rte_cpu_to_le_16(vsi->base_queue);
325         info->q_mapping[1] = rte_cpu_to_le_16(vsi->nb_qps);
326         info->valid_sections |=
327                 rte_cpu_to_le_16(ICE_AQ_VSI_PROP_RXQ_MAP_VALID);
328         /* Set the info.ingress_table and info.egress_table
329          * for UP translate table. Now just set it to 1:1 map by default
330          * -- 0b 111 110 101 100 011 010 001 000 == 0xFAC688
331          */
332 #define ICE_TC_QUEUE_TABLE_DFLT 0x00FAC688
333         info->ingress_table  = rte_cpu_to_le_32(ICE_TC_QUEUE_TABLE_DFLT);
334         info->egress_table   = rte_cpu_to_le_32(ICE_TC_QUEUE_TABLE_DFLT);
335         info->outer_up_table = rte_cpu_to_le_32(ICE_TC_QUEUE_TABLE_DFLT);
336         return 0;
337 }
338
339 static int
340 ice_init_mac_address(struct rte_eth_dev *dev)
341 {
342         struct ice_hw *hw = ICE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
343
344         if (!is_unicast_ether_addr
345                 ((struct ether_addr *)hw->port_info[0].mac.lan_addr)) {
346                 PMD_INIT_LOG(ERR, "Invalid MAC address");
347                 return -EINVAL;
348         }
349
350         ether_addr_copy((struct ether_addr *)hw->port_info[0].mac.lan_addr,
351                         (struct ether_addr *)hw->port_info[0].mac.perm_addr);
352
353         dev->data->mac_addrs = rte_zmalloc(NULL, sizeof(struct ether_addr), 0);
354         if (!dev->data->mac_addrs) {
355                 PMD_INIT_LOG(ERR,
356                              "Failed to allocate memory to store mac address");
357                 return -ENOMEM;
358         }
359         /* store it to dev data */
360         ether_addr_copy((struct ether_addr *)hw->port_info[0].mac.perm_addr,
361                         &dev->data->mac_addrs[0]);
362         return 0;
363 }
364
365 /* Find out specific MAC filter */
366 static struct ice_mac_filter *
367 ice_find_mac_filter(struct ice_vsi *vsi, struct ether_addr *macaddr)
368 {
369         struct ice_mac_filter *f;
370
371         TAILQ_FOREACH(f, &vsi->mac_list, next) {
372                 if (is_same_ether_addr(macaddr, &f->mac_info.mac_addr))
373                         return f;
374         }
375
376         return NULL;
377 }
378
379 static int
380 ice_add_mac_filter(struct ice_vsi *vsi, struct ether_addr *mac_addr)
381 {
382         struct ice_fltr_list_entry *m_list_itr = NULL;
383         struct ice_mac_filter *f;
384         struct LIST_HEAD_TYPE list_head;
385         struct ice_hw *hw = ICE_VSI_TO_HW(vsi);
386         int ret = 0;
387
388         /* If it's added and configured, return */
389         f = ice_find_mac_filter(vsi, mac_addr);
390         if (f) {
391                 PMD_DRV_LOG(INFO, "This MAC filter already exists.");
392                 return 0;
393         }
394
395         INIT_LIST_HEAD(&list_head);
396
397         m_list_itr = (struct ice_fltr_list_entry *)
398                 ice_malloc(hw, sizeof(*m_list_itr));
399         if (!m_list_itr) {
400                 ret = -ENOMEM;
401                 goto DONE;
402         }
403         ice_memcpy(m_list_itr->fltr_info.l_data.mac.mac_addr,
404                    mac_addr, ETH_ALEN, ICE_NONDMA_TO_NONDMA);
405         m_list_itr->fltr_info.src_id = ICE_SRC_ID_VSI;
406         m_list_itr->fltr_info.fltr_act = ICE_FWD_TO_VSI;
407         m_list_itr->fltr_info.lkup_type = ICE_SW_LKUP_MAC;
408         m_list_itr->fltr_info.flag = ICE_FLTR_TX;
409         m_list_itr->fltr_info.vsi_handle = vsi->idx;
410
411         LIST_ADD(&m_list_itr->list_entry, &list_head);
412
413         /* Add the mac */
414         ret = ice_add_mac(hw, &list_head);
415         if (ret != ICE_SUCCESS) {
416                 PMD_DRV_LOG(ERR, "Failed to add MAC filter");
417                 ret = -EINVAL;
418                 goto DONE;
419         }
420         /* Add the mac addr into mac list */
421         f = rte_zmalloc(NULL, sizeof(*f), 0);
422         if (!f) {
423                 PMD_DRV_LOG(ERR, "failed to allocate memory");
424                 ret = -ENOMEM;
425                 goto DONE;
426         }
427         rte_memcpy(&f->mac_info.mac_addr, mac_addr, ETH_ADDR_LEN);
428         TAILQ_INSERT_TAIL(&vsi->mac_list, f, next);
429         vsi->mac_num++;
430
431         ret = 0;
432
433 DONE:
434         rte_free(m_list_itr);
435         return ret;
436 }
437
438 static int
439 ice_remove_mac_filter(struct ice_vsi *vsi, struct ether_addr *mac_addr)
440 {
441         struct ice_fltr_list_entry *m_list_itr = NULL;
442         struct ice_mac_filter *f;
443         struct LIST_HEAD_TYPE list_head;
444         struct ice_hw *hw = ICE_VSI_TO_HW(vsi);
445         int ret = 0;
446
447         /* Can't find it, return an error */
448         f = ice_find_mac_filter(vsi, mac_addr);
449         if (!f)
450                 return -EINVAL;
451
452         INIT_LIST_HEAD(&list_head);
453
454         m_list_itr = (struct ice_fltr_list_entry *)
455                 ice_malloc(hw, sizeof(*m_list_itr));
456         if (!m_list_itr) {
457                 ret = -ENOMEM;
458                 goto DONE;
459         }
460         ice_memcpy(m_list_itr->fltr_info.l_data.mac.mac_addr,
461                    mac_addr, ETH_ALEN, ICE_NONDMA_TO_NONDMA);
462         m_list_itr->fltr_info.src_id = ICE_SRC_ID_VSI;
463         m_list_itr->fltr_info.fltr_act = ICE_FWD_TO_VSI;
464         m_list_itr->fltr_info.lkup_type = ICE_SW_LKUP_MAC;
465         m_list_itr->fltr_info.flag = ICE_FLTR_TX;
466         m_list_itr->fltr_info.vsi_handle = vsi->idx;
467
468         LIST_ADD(&m_list_itr->list_entry, &list_head);
469
470         /* remove the mac filter */
471         ret = ice_remove_mac(hw, &list_head);
472         if (ret != ICE_SUCCESS) {
473                 PMD_DRV_LOG(ERR, "Failed to remove MAC filter");
474                 ret = -EINVAL;
475                 goto DONE;
476         }
477
478         /* Remove the mac addr from mac list */
479         TAILQ_REMOVE(&vsi->mac_list, f, next);
480         rte_free(f);
481         vsi->mac_num--;
482
483         ret = 0;
484 DONE:
485         rte_free(m_list_itr);
486         return ret;
487 }
488
489 /* Find out specific VLAN filter */
490 static struct ice_vlan_filter *
491 ice_find_vlan_filter(struct ice_vsi *vsi, uint16_t vlan_id)
492 {
493         struct ice_vlan_filter *f;
494
495         TAILQ_FOREACH(f, &vsi->vlan_list, next) {
496                 if (vlan_id == f->vlan_info.vlan_id)
497                         return f;
498         }
499
500         return NULL;
501 }
502
503 static int
504 ice_add_vlan_filter(struct ice_vsi *vsi, uint16_t vlan_id)
505 {
506         struct ice_fltr_list_entry *v_list_itr = NULL;
507         struct ice_vlan_filter *f;
508         struct LIST_HEAD_TYPE list_head;
509         struct ice_hw *hw = ICE_VSI_TO_HW(vsi);
510         int ret = 0;
511
512         if (!vsi || vlan_id > ETHER_MAX_VLAN_ID)
513                 return -EINVAL;
514
515         /* If it's added and configured, return. */
516         f = ice_find_vlan_filter(vsi, vlan_id);
517         if (f) {
518                 PMD_DRV_LOG(INFO, "This VLAN filter already exists.");
519                 return 0;
520         }
521
522         if (!vsi->vlan_anti_spoof_on && !vsi->vlan_filter_on)
523                 return 0;
524
525         INIT_LIST_HEAD(&list_head);
526
527         v_list_itr = (struct ice_fltr_list_entry *)
528                       ice_malloc(hw, sizeof(*v_list_itr));
529         if (!v_list_itr) {
530                 ret = -ENOMEM;
531                 goto DONE;
532         }
533         v_list_itr->fltr_info.l_data.vlan.vlan_id = vlan_id;
534         v_list_itr->fltr_info.src_id = ICE_SRC_ID_VSI;
535         v_list_itr->fltr_info.fltr_act = ICE_FWD_TO_VSI;
536         v_list_itr->fltr_info.lkup_type = ICE_SW_LKUP_VLAN;
537         v_list_itr->fltr_info.flag = ICE_FLTR_TX;
538         v_list_itr->fltr_info.vsi_handle = vsi->idx;
539
540         LIST_ADD(&v_list_itr->list_entry, &list_head);
541
542         /* Add the vlan */
543         ret = ice_add_vlan(hw, &list_head);
544         if (ret != ICE_SUCCESS) {
545                 PMD_DRV_LOG(ERR, "Failed to add VLAN filter");
546                 ret = -EINVAL;
547                 goto DONE;
548         }
549
550         /* Add vlan into vlan list */
551         f = rte_zmalloc(NULL, sizeof(*f), 0);
552         if (!f) {
553                 PMD_DRV_LOG(ERR, "failed to allocate memory");
554                 ret = -ENOMEM;
555                 goto DONE;
556         }
557         f->vlan_info.vlan_id = vlan_id;
558         TAILQ_INSERT_TAIL(&vsi->vlan_list, f, next);
559         vsi->vlan_num++;
560
561         ret = 0;
562
563 DONE:
564         rte_free(v_list_itr);
565         return ret;
566 }
567
568 static int
569 ice_remove_vlan_filter(struct ice_vsi *vsi, uint16_t vlan_id)
570 {
571         struct ice_fltr_list_entry *v_list_itr = NULL;
572         struct ice_vlan_filter *f;
573         struct LIST_HEAD_TYPE list_head;
574         struct ice_hw *hw = ICE_VSI_TO_HW(vsi);
575         int ret = 0;
576
577         /**
578          * Vlan 0 is the generic filter for untagged packets
579          * and can't be removed.
580          */
581         if (!vsi || vlan_id == 0 || vlan_id > ETHER_MAX_VLAN_ID)
582                 return -EINVAL;
583
584         /* Can't find it, return an error */
585         f = ice_find_vlan_filter(vsi, vlan_id);
586         if (!f)
587                 return -EINVAL;
588
589         INIT_LIST_HEAD(&list_head);
590
591         v_list_itr = (struct ice_fltr_list_entry *)
592                       ice_malloc(hw, sizeof(*v_list_itr));
593         if (!v_list_itr) {
594                 ret = -ENOMEM;
595                 goto DONE;
596         }
597
598         v_list_itr->fltr_info.l_data.vlan.vlan_id = vlan_id;
599         v_list_itr->fltr_info.src_id = ICE_SRC_ID_VSI;
600         v_list_itr->fltr_info.fltr_act = ICE_FWD_TO_VSI;
601         v_list_itr->fltr_info.lkup_type = ICE_SW_LKUP_VLAN;
602         v_list_itr->fltr_info.flag = ICE_FLTR_TX;
603         v_list_itr->fltr_info.vsi_handle = vsi->idx;
604
605         LIST_ADD(&v_list_itr->list_entry, &list_head);
606
607         /* remove the vlan filter */
608         ret = ice_remove_vlan(hw, &list_head);
609         if (ret != ICE_SUCCESS) {
610                 PMD_DRV_LOG(ERR, "Failed to remove VLAN filter");
611                 ret = -EINVAL;
612                 goto DONE;
613         }
614
615         /* Remove the vlan id from vlan list */
616         TAILQ_REMOVE(&vsi->vlan_list, f, next);
617         rte_free(f);
618         vsi->vlan_num--;
619
620         ret = 0;
621 DONE:
622         rte_free(v_list_itr);
623         return ret;
624 }
625
626 static int
627 ice_remove_all_mac_vlan_filters(struct ice_vsi *vsi)
628 {
629         struct ice_mac_filter *m_f;
630         struct ice_vlan_filter *v_f;
631         int ret = 0;
632
633         if (!vsi || !vsi->mac_num)
634                 return -EINVAL;
635
636         TAILQ_FOREACH(m_f, &vsi->mac_list, next) {
637                 ret = ice_remove_mac_filter(vsi, &m_f->mac_info.mac_addr);
638                 if (ret != ICE_SUCCESS) {
639                         ret = -EINVAL;
640                         goto DONE;
641                 }
642         }
643
644         if (vsi->vlan_num == 0)
645                 return 0;
646
647         TAILQ_FOREACH(v_f, &vsi->vlan_list, next) {
648                 ret = ice_remove_vlan_filter(vsi, v_f->vlan_info.vlan_id);
649                 if (ret != ICE_SUCCESS) {
650                         ret = -EINVAL;
651                         goto DONE;
652                 }
653         }
654
655 DONE:
656         return ret;
657 }
658
659 static int
660 ice_vsi_config_qinq_insertion(struct ice_vsi *vsi, bool on)
661 {
662         struct ice_hw *hw = ICE_VSI_TO_HW(vsi);
663         struct ice_vsi_ctx ctxt;
664         uint8_t qinq_flags;
665         int ret = 0;
666
667         /* Check if it has been already on or off */
668         if (vsi->info.valid_sections &
669                 rte_cpu_to_le_16(ICE_AQ_VSI_PROP_OUTER_TAG_VALID)) {
670                 if (on) {
671                         if ((vsi->info.outer_tag_flags &
672                              ICE_AQ_VSI_OUTER_TAG_ACCEPT_HOST) ==
673                             ICE_AQ_VSI_OUTER_TAG_ACCEPT_HOST)
674                                 return 0; /* already on */
675                 } else {
676                         if (!(vsi->info.outer_tag_flags &
677                               ICE_AQ_VSI_OUTER_TAG_ACCEPT_HOST))
678                                 return 0; /* already off */
679                 }
680         }
681
682         if (on)
683                 qinq_flags = ICE_AQ_VSI_OUTER_TAG_ACCEPT_HOST;
684         else
685                 qinq_flags = 0;
686         /* clear global insertion and use per packet insertion */
687         vsi->info.outer_tag_flags &= ~(ICE_AQ_VSI_OUTER_TAG_INSERT);
688         vsi->info.outer_tag_flags &= ~(ICE_AQ_VSI_OUTER_TAG_ACCEPT_HOST);
689         vsi->info.outer_tag_flags |= qinq_flags;
690         /* use default vlan type 0x8100 */
691         vsi->info.outer_tag_flags &= ~(ICE_AQ_VSI_OUTER_TAG_TYPE_M);
692         vsi->info.outer_tag_flags |= ICE_DFLT_OUTER_TAG_TYPE <<
693                                      ICE_AQ_VSI_OUTER_TAG_TYPE_S;
694         (void)rte_memcpy(&ctxt.info, &vsi->info, sizeof(vsi->info));
695         ctxt.info.valid_sections =
696                         rte_cpu_to_le_16(ICE_AQ_VSI_PROP_OUTER_TAG_VALID);
697         ctxt.vsi_num = vsi->vsi_id;
698         ret = ice_update_vsi(hw, vsi->idx, &ctxt, NULL);
699         if (ret) {
700                 PMD_DRV_LOG(INFO,
701                             "Update VSI failed to %s qinq stripping",
702                             on ? "enable" : "disable");
703                 return -EINVAL;
704         }
705
706         vsi->info.valid_sections |=
707                 rte_cpu_to_le_16(ICE_AQ_VSI_PROP_OUTER_TAG_VALID);
708
709         return ret;
710 }
711
712 static int
713 ice_vsi_config_qinq_stripping(struct ice_vsi *vsi, bool on)
714 {
715         struct ice_hw *hw = ICE_VSI_TO_HW(vsi);
716         struct ice_vsi_ctx ctxt;
717         uint8_t qinq_flags;
718         int ret = 0;
719
720         /* Check if it has been already on or off */
721         if (vsi->info.valid_sections &
722                 rte_cpu_to_le_16(ICE_AQ_VSI_PROP_OUTER_TAG_VALID)) {
723                 if (on) {
724                         if ((vsi->info.outer_tag_flags &
725                              ICE_AQ_VSI_OUTER_TAG_MODE_M) ==
726                             ICE_AQ_VSI_OUTER_TAG_COPY)
727                                 return 0; /* already on */
728                 } else {
729                         if ((vsi->info.outer_tag_flags &
730                              ICE_AQ_VSI_OUTER_TAG_MODE_M) ==
731                             ICE_AQ_VSI_OUTER_TAG_NOTHING)
732                                 return 0; /* already off */
733                 }
734         }
735
736         if (on)
737                 qinq_flags = ICE_AQ_VSI_OUTER_TAG_COPY;
738         else
739                 qinq_flags = ICE_AQ_VSI_OUTER_TAG_NOTHING;
740         vsi->info.outer_tag_flags &= ~(ICE_AQ_VSI_OUTER_TAG_MODE_M);
741         vsi->info.outer_tag_flags |= qinq_flags;
742         /* use default vlan type 0x8100 */
743         vsi->info.outer_tag_flags &= ~(ICE_AQ_VSI_OUTER_TAG_TYPE_M);
744         vsi->info.outer_tag_flags |= ICE_DFLT_OUTER_TAG_TYPE <<
745                                      ICE_AQ_VSI_OUTER_TAG_TYPE_S;
746         (void)rte_memcpy(&ctxt.info, &vsi->info, sizeof(vsi->info));
747         ctxt.info.valid_sections =
748                         rte_cpu_to_le_16(ICE_AQ_VSI_PROP_OUTER_TAG_VALID);
749         ctxt.vsi_num = vsi->vsi_id;
750         ret = ice_update_vsi(hw, vsi->idx, &ctxt, NULL);
751         if (ret) {
752                 PMD_DRV_LOG(INFO,
753                             "Update VSI failed to %s qinq stripping",
754                             on ? "enable" : "disable");
755                 return -EINVAL;
756         }
757
758         vsi->info.valid_sections |=
759                 rte_cpu_to_le_16(ICE_AQ_VSI_PROP_OUTER_TAG_VALID);
760
761         return ret;
762 }
763
764 static int
765 ice_vsi_config_double_vlan(struct ice_vsi *vsi, int on)
766 {
767         int ret;
768
769         ret = ice_vsi_config_qinq_stripping(vsi, on);
770         if (ret)
771                 PMD_DRV_LOG(ERR, "Fail to set qinq stripping - %d", ret);
772
773         ret = ice_vsi_config_qinq_insertion(vsi, on);
774         if (ret)
775                 PMD_DRV_LOG(ERR, "Fail to set qinq insertion - %d", ret);
776
777         return ret;
778 }
779
780 /* Enable IRQ0 */
781 static void
782 ice_pf_enable_irq0(struct ice_hw *hw)
783 {
784         /* reset the registers */
785         ICE_WRITE_REG(hw, PFINT_OICR_ENA, 0);
786         ICE_READ_REG(hw, PFINT_OICR);
787
788 #ifdef ICE_LSE_SPT
789         ICE_WRITE_REG(hw, PFINT_OICR_ENA,
790                       (uint32_t)(PFINT_OICR_ENA_INT_ENA_M &
791                                  (~PFINT_OICR_LINK_STAT_CHANGE_M)));
792
793         ICE_WRITE_REG(hw, PFINT_OICR_CTL,
794                       (0 & PFINT_OICR_CTL_MSIX_INDX_M) |
795                       ((0 << PFINT_OICR_CTL_ITR_INDX_S) &
796                        PFINT_OICR_CTL_ITR_INDX_M) |
797                       PFINT_OICR_CTL_CAUSE_ENA_M);
798
799         ICE_WRITE_REG(hw, PFINT_FW_CTL,
800                       (0 & PFINT_FW_CTL_MSIX_INDX_M) |
801                       ((0 << PFINT_FW_CTL_ITR_INDX_S) &
802                        PFINT_FW_CTL_ITR_INDX_M) |
803                       PFINT_FW_CTL_CAUSE_ENA_M);
804 #else
805         ICE_WRITE_REG(hw, PFINT_OICR_ENA, PFINT_OICR_ENA_INT_ENA_M);
806 #endif
807
808         ICE_WRITE_REG(hw, GLINT_DYN_CTL(0),
809                       GLINT_DYN_CTL_INTENA_M |
810                       GLINT_DYN_CTL_CLEARPBA_M |
811                       GLINT_DYN_CTL_ITR_INDX_M);
812
813         ice_flush(hw);
814 }
815
816 /* Disable IRQ0 */
817 static void
818 ice_pf_disable_irq0(struct ice_hw *hw)
819 {
820         /* Disable all interrupt types */
821         ICE_WRITE_REG(hw, GLINT_DYN_CTL(0), GLINT_DYN_CTL_WB_ON_ITR_M);
822         ice_flush(hw);
823 }
824
825 #ifdef ICE_LSE_SPT
826 static void
827 ice_handle_aq_msg(struct rte_eth_dev *dev)
828 {
829         struct ice_hw *hw = ICE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
830         struct ice_ctl_q_info *cq = &hw->adminq;
831         struct ice_rq_event_info event;
832         uint16_t pending, opcode;
833         int ret;
834
835         event.buf_len = ICE_AQ_MAX_BUF_LEN;
836         event.msg_buf = rte_zmalloc(NULL, event.buf_len, 0);
837         if (!event.msg_buf) {
838                 PMD_DRV_LOG(ERR, "Failed to allocate mem");
839                 return;
840         }
841
842         pending = 1;
843         while (pending) {
844                 ret = ice_clean_rq_elem(hw, cq, &event, &pending);
845
846                 if (ret != ICE_SUCCESS) {
847                         PMD_DRV_LOG(INFO,
848                                     "Failed to read msg from AdminQ, "
849                                     "adminq_err: %u",
850                                     hw->adminq.sq_last_status);
851                         break;
852                 }
853                 opcode = rte_le_to_cpu_16(event.desc.opcode);
854
855                 switch (opcode) {
856                 case ice_aqc_opc_get_link_status:
857                         ret = ice_link_update(dev, 0);
858                         if (!ret)
859                                 _rte_eth_dev_callback_process
860                                         (dev, RTE_ETH_EVENT_INTR_LSC, NULL);
861                         break;
862                 default:
863                         PMD_DRV_LOG(DEBUG, "Request %u is not supported yet",
864                                     opcode);
865                         break;
866                 }
867         }
868         rte_free(event.msg_buf);
869 }
870 #endif
871
872 /**
873  * Interrupt handler triggered by NIC for handling
874  * specific interrupt.
875  *
876  * @param handle
877  *  Pointer to interrupt handle.
878  * @param param
879  *  The address of parameter (struct rte_eth_dev *) regsitered before.
880  *
881  * @return
882  *  void
883  */
884 static void
885 ice_interrupt_handler(void *param)
886 {
887         struct rte_eth_dev *dev = (struct rte_eth_dev *)param;
888         struct ice_hw *hw = ICE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
889         uint32_t oicr;
890         uint32_t reg;
891         uint8_t pf_num;
892         uint8_t event;
893         uint16_t queue;
894 #ifdef ICE_LSE_SPT
895         uint32_t int_fw_ctl;
896 #endif
897
898         /* Disable interrupt */
899         ice_pf_disable_irq0(hw);
900
901         /* read out interrupt causes */
902         oicr = ICE_READ_REG(hw, PFINT_OICR);
903 #ifdef ICE_LSE_SPT
904         int_fw_ctl = ICE_READ_REG(hw, PFINT_FW_CTL);
905 #endif
906
907         /* No interrupt event indicated */
908         if (!(oicr & PFINT_OICR_INTEVENT_M)) {
909                 PMD_DRV_LOG(INFO, "No interrupt event");
910                 goto done;
911         }
912
913 #ifdef ICE_LSE_SPT
914         if (int_fw_ctl & PFINT_FW_CTL_INTEVENT_M) {
915                 PMD_DRV_LOG(INFO, "FW_CTL: link state change event");
916                 ice_handle_aq_msg(dev);
917         }
918 #else
919         if (oicr & PFINT_OICR_LINK_STAT_CHANGE_M) {
920                 PMD_DRV_LOG(INFO, "OICR: link state change event");
921                 ice_link_update(dev, 0);
922         }
923 #endif
924
925         if (oicr & PFINT_OICR_MAL_DETECT_M) {
926                 PMD_DRV_LOG(WARNING, "OICR: MDD event");
927                 reg = ICE_READ_REG(hw, GL_MDET_TX_PQM);
928                 if (reg & GL_MDET_TX_PQM_VALID_M) {
929                         pf_num = (reg & GL_MDET_TX_PQM_PF_NUM_M) >>
930                                  GL_MDET_TX_PQM_PF_NUM_S;
931                         event = (reg & GL_MDET_TX_PQM_MAL_TYPE_M) >>
932                                 GL_MDET_TX_PQM_MAL_TYPE_S;
933                         queue = (reg & GL_MDET_TX_PQM_QNUM_M) >>
934                                 GL_MDET_TX_PQM_QNUM_S;
935
936                         PMD_DRV_LOG(WARNING, "Malicious Driver Detection event "
937                                     "%d by PQM on TX queue %d PF# %d",
938                                     event, queue, pf_num);
939                 }
940
941                 reg = ICE_READ_REG(hw, GL_MDET_TX_TCLAN);
942                 if (reg & GL_MDET_TX_TCLAN_VALID_M) {
943                         pf_num = (reg & GL_MDET_TX_TCLAN_PF_NUM_M) >>
944                                  GL_MDET_TX_TCLAN_PF_NUM_S;
945                         event = (reg & GL_MDET_TX_TCLAN_MAL_TYPE_M) >>
946                                 GL_MDET_TX_TCLAN_MAL_TYPE_S;
947                         queue = (reg & GL_MDET_TX_TCLAN_QNUM_M) >>
948                                 GL_MDET_TX_TCLAN_QNUM_S;
949
950                         PMD_DRV_LOG(WARNING, "Malicious Driver Detection event "
951                                     "%d by TCLAN on TX queue %d PF# %d",
952                                     event, queue, pf_num);
953                 }
954         }
955 done:
956         /* Enable interrupt */
957         ice_pf_enable_irq0(hw);
958         rte_intr_enable(dev->intr_handle);
959 }
960
961 /*  Initialize SW parameters of PF */
962 static int
963 ice_pf_sw_init(struct rte_eth_dev *dev)
964 {
965         struct ice_pf *pf = ICE_DEV_PRIVATE_TO_PF(dev->data->dev_private);
966         struct ice_hw *hw = ICE_PF_TO_HW(pf);
967
968         if (ice_config_max_queue_pair_num(dev->device->devargs) > 0)
969                 pf->lan_nb_qp_max =
970                         ice_config_max_queue_pair_num(dev->device->devargs);
971         else
972                 pf->lan_nb_qp_max =
973                         (uint16_t)RTE_MIN(hw->func_caps.common_cap.num_txq,
974                                           hw->func_caps.common_cap.num_rxq);
975
976         pf->lan_nb_qps = pf->lan_nb_qp_max;
977
978         return 0;
979 }
980
981 static struct ice_vsi *
982 ice_setup_vsi(struct ice_pf *pf, enum ice_vsi_type type)
983 {
984         struct ice_hw *hw = ICE_PF_TO_HW(pf);
985         struct ice_vsi *vsi = NULL;
986         struct ice_vsi_ctx vsi_ctx;
987         int ret;
988         struct ether_addr broadcast = {
989                 .addr_bytes = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff} };
990         struct ether_addr mac_addr;
991         uint16_t max_txqs[ICE_MAX_TRAFFIC_CLASS] = { 0 };
992         uint8_t tc_bitmap = 0x1;
993
994         /* hw->num_lports = 1 in NIC mode */
995         vsi = rte_zmalloc(NULL, sizeof(struct ice_vsi), 0);
996         if (!vsi)
997                 return NULL;
998
999         vsi->idx = pf->next_vsi_idx;
1000         pf->next_vsi_idx++;
1001         vsi->type = type;
1002         vsi->adapter = ICE_PF_TO_ADAPTER(pf);
1003         vsi->max_macaddrs = ICE_NUM_MACADDR_MAX;
1004         vsi->vlan_anti_spoof_on = 0;
1005         vsi->vlan_filter_on = 1;
1006         TAILQ_INIT(&vsi->mac_list);
1007         TAILQ_INIT(&vsi->vlan_list);
1008
1009         memset(&vsi_ctx, 0, sizeof(vsi_ctx));
1010         /* base_queue in used in queue mapping of VSI add/update command.
1011          * Suppose vsi->base_queue is 0 now, don't consider SRIOV, VMDQ
1012          * cases in the first stage. Only Main VSI.
1013          */
1014         vsi->base_queue = 0;
1015         switch (type) {
1016         case ICE_VSI_PF:
1017                 vsi->nb_qps = pf->lan_nb_qps;
1018                 ice_vsi_config_default_rss(&vsi_ctx.info);
1019                 vsi_ctx.alloc_from_pool = true;
1020                 vsi_ctx.flags = ICE_AQ_VSI_TYPE_PF;
1021                 /* switch_id is queried by get_switch_config aq, which is done
1022                  * by ice_init_hw
1023                  */
1024                 vsi_ctx.info.sw_id = hw->port_info->sw_id;
1025                 vsi_ctx.info.sw_flags2 = ICE_AQ_VSI_SW_FLAG_LAN_ENA;
1026                 /* Allow all untagged or tagged packets */
1027                 vsi_ctx.info.vlan_flags = ICE_AQ_VSI_VLAN_MODE_ALL;
1028                 vsi_ctx.info.vlan_flags |= ICE_AQ_VSI_VLAN_EMOD_NOTHING;
1029                 vsi_ctx.info.q_opt_rss = ICE_AQ_VSI_Q_OPT_RSS_LUT_PF |
1030                                          ICE_AQ_VSI_Q_OPT_RSS_TPLZ;
1031                 /* Enable VLAN/UP trip */
1032                 ret = ice_vsi_config_tc_queue_mapping(vsi,
1033                                                       &vsi_ctx.info,
1034                                                       ICE_DEFAULT_TCMAP);
1035                 if (ret) {
1036                         PMD_INIT_LOG(ERR,
1037                                      "tc queue mapping with vsi failed, "
1038                                      "err = %d",
1039                                      ret);
1040                         goto fail_mem;
1041                 }
1042
1043                 break;
1044         default:
1045                 /* for other types of VSI */
1046                 PMD_INIT_LOG(ERR, "other types of VSI not supported");
1047                 goto fail_mem;
1048         }
1049
1050         /* VF has MSIX interrupt in VF range, don't allocate here */
1051         if (type == ICE_VSI_PF) {
1052                 ret = ice_res_pool_alloc(&pf->msix_pool,
1053                                          RTE_MIN(vsi->nb_qps,
1054                                                  RTE_MAX_RXTX_INTR_VEC_ID));
1055                 if (ret < 0) {
1056                         PMD_INIT_LOG(ERR, "VSI MAIN %d get heap failed %d",
1057                                      vsi->vsi_id, ret);
1058                 }
1059                 vsi->msix_intr = ret;
1060                 vsi->nb_msix = RTE_MIN(vsi->nb_qps, RTE_MAX_RXTX_INTR_VEC_ID);
1061         } else {
1062                 vsi->msix_intr = 0;
1063                 vsi->nb_msix = 0;
1064         }
1065         ret = ice_add_vsi(hw, vsi->idx, &vsi_ctx, NULL);
1066         if (ret != ICE_SUCCESS) {
1067                 PMD_INIT_LOG(ERR, "add vsi failed, err = %d", ret);
1068                 goto fail_mem;
1069         }
1070         /* store vsi information is SW structure */
1071         vsi->vsi_id = vsi_ctx.vsi_num;
1072         vsi->info = vsi_ctx.info;
1073         pf->vsis_allocated = vsi_ctx.vsis_allocd;
1074         pf->vsis_unallocated = vsi_ctx.vsis_unallocated;
1075
1076         /* MAC configuration */
1077         rte_memcpy(pf->dev_addr.addr_bytes,
1078                    hw->port_info->mac.perm_addr,
1079                    ETH_ADDR_LEN);
1080
1081         rte_memcpy(&mac_addr, &pf->dev_addr, ETHER_ADDR_LEN);
1082         ret = ice_add_mac_filter(vsi, &mac_addr);
1083         if (ret != ICE_SUCCESS)
1084                 PMD_INIT_LOG(ERR, "Failed to add dflt MAC filter");
1085
1086         rte_memcpy(&mac_addr, &broadcast, ETHER_ADDR_LEN);
1087         ret = ice_add_mac_filter(vsi, &mac_addr);
1088         if (ret != ICE_SUCCESS)
1089                 PMD_INIT_LOG(ERR, "Failed to add MAC filter");
1090
1091         /* At the beginning, only TC0. */
1092         /* What we need here is the maximam number of the TX queues.
1093          * Currently vsi->nb_qps means it.
1094          * Correct it if any change.
1095          */
1096         max_txqs[0] = vsi->nb_qps;
1097         ret = ice_cfg_vsi_lan(hw->port_info, vsi->idx,
1098                               tc_bitmap, max_txqs);
1099         if (ret != ICE_SUCCESS)
1100                 PMD_INIT_LOG(ERR, "Failed to config vsi sched");
1101
1102         return vsi;
1103 fail_mem:
1104         rte_free(vsi);
1105         pf->next_vsi_idx--;
1106         return NULL;
1107 }
1108
1109 static int
1110 ice_pf_setup(struct ice_pf *pf)
1111 {
1112         struct ice_vsi *vsi;
1113
1114         /* Clear all stats counters */
1115         pf->offset_loaded = FALSE;
1116         memset(&pf->stats, 0, sizeof(struct ice_hw_port_stats));
1117         memset(&pf->stats_offset, 0, sizeof(struct ice_hw_port_stats));
1118         memset(&pf->internal_stats, 0, sizeof(struct ice_eth_stats));
1119         memset(&pf->internal_stats_offset, 0, sizeof(struct ice_eth_stats));
1120
1121         vsi = ice_setup_vsi(pf, ICE_VSI_PF);
1122         if (!vsi) {
1123                 PMD_INIT_LOG(ERR, "Failed to add vsi for PF");
1124                 return -EINVAL;
1125         }
1126
1127         pf->main_vsi = vsi;
1128
1129         return 0;
1130 }
1131
1132 static int
1133 ice_dev_init(struct rte_eth_dev *dev)
1134 {
1135         struct rte_pci_device *pci_dev;
1136         struct rte_intr_handle *intr_handle;
1137         struct ice_hw *hw = ICE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1138         struct ice_pf *pf = ICE_DEV_PRIVATE_TO_PF(dev->data->dev_private);
1139         struct ice_vsi *vsi;
1140         int ret;
1141
1142         dev->dev_ops = &ice_eth_dev_ops;
1143         dev->rx_pkt_burst = ice_recv_pkts;
1144         dev->tx_pkt_burst = ice_xmit_pkts;
1145         dev->tx_pkt_prepare = ice_prep_pkts;
1146
1147         ice_set_default_ptype_table(dev);
1148         pci_dev = RTE_DEV_TO_PCI(dev->device);
1149         intr_handle = &pci_dev->intr_handle;
1150
1151         pf->adapter = ICE_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
1152         pf->adapter->eth_dev = dev;
1153         pf->dev_data = dev->data;
1154         hw->back = pf->adapter;
1155         hw->hw_addr = (uint8_t *)pci_dev->mem_resource[0].addr;
1156         hw->vendor_id = pci_dev->id.vendor_id;
1157         hw->device_id = pci_dev->id.device_id;
1158         hw->subsystem_vendor_id = pci_dev->id.subsystem_vendor_id;
1159         hw->subsystem_device_id = pci_dev->id.subsystem_device_id;
1160         hw->bus.device = pci_dev->addr.devid;
1161         hw->bus.func = pci_dev->addr.function;
1162
1163         ice_init_controlq_parameter(hw);
1164
1165         ret = ice_init_hw(hw);
1166         if (ret) {
1167                 PMD_INIT_LOG(ERR, "Failed to initialize HW");
1168                 return -EINVAL;
1169         }
1170
1171         PMD_INIT_LOG(INFO, "FW %d.%d.%05d API %d.%d",
1172                      hw->fw_maj_ver, hw->fw_min_ver, hw->fw_build,
1173                      hw->api_maj_ver, hw->api_min_ver);
1174
1175         ice_pf_sw_init(dev);
1176         ret = ice_init_mac_address(dev);
1177         if (ret) {
1178                 PMD_INIT_LOG(ERR, "Failed to initialize mac address");
1179                 goto err_init_mac;
1180         }
1181
1182         ret = ice_res_pool_init(&pf->msix_pool, 1,
1183                                 hw->func_caps.common_cap.num_msix_vectors - 1);
1184         if (ret) {
1185                 PMD_INIT_LOG(ERR, "Failed to init MSIX pool");
1186                 goto err_msix_pool_init;
1187         }
1188
1189         ret = ice_pf_setup(pf);
1190         if (ret) {
1191                 PMD_INIT_LOG(ERR, "Failed to setup PF");
1192                 goto err_pf_setup;
1193         }
1194
1195         vsi = pf->main_vsi;
1196
1197         /* Disable double vlan by default */
1198         ice_vsi_config_double_vlan(vsi, FALSE);
1199
1200         /* register callback func to eal lib */
1201         rte_intr_callback_register(intr_handle,
1202                                    ice_interrupt_handler, dev);
1203
1204         ice_pf_enable_irq0(hw);
1205
1206         /* enable uio intr after callback register */
1207         rte_intr_enable(intr_handle);
1208
1209         return 0;
1210
1211 err_pf_setup:
1212         ice_res_pool_destroy(&pf->msix_pool);
1213 err_msix_pool_init:
1214         rte_free(dev->data->mac_addrs);
1215 err_init_mac:
1216         ice_sched_cleanup_all(hw);
1217         rte_free(hw->port_info);
1218         ice_shutdown_all_ctrlq(hw);
1219
1220         return ret;
1221 }
1222
1223 static int
1224 ice_release_vsi(struct ice_vsi *vsi)
1225 {
1226         struct ice_hw *hw;
1227         struct ice_vsi_ctx vsi_ctx;
1228         enum ice_status ret;
1229
1230         if (!vsi)
1231                 return 0;
1232
1233         hw = ICE_VSI_TO_HW(vsi);
1234
1235         ice_remove_all_mac_vlan_filters(vsi);
1236
1237         memset(&vsi_ctx, 0, sizeof(vsi_ctx));
1238
1239         vsi_ctx.vsi_num = vsi->vsi_id;
1240         vsi_ctx.info = vsi->info;
1241         ret = ice_free_vsi(hw, vsi->idx, &vsi_ctx, false, NULL);
1242         if (ret != ICE_SUCCESS) {
1243                 PMD_INIT_LOG(ERR, "Failed to free vsi by aq, %u", vsi->vsi_id);
1244                 rte_free(vsi);
1245                 return -1;
1246         }
1247
1248         rte_free(vsi);
1249         return 0;
1250 }
1251
1252 static void
1253 ice_dev_stop(struct rte_eth_dev *dev)
1254 {
1255         struct rte_eth_dev_data *data = dev->data;
1256         struct ice_pf *pf = ICE_DEV_PRIVATE_TO_PF(dev->data->dev_private);
1257         struct rte_pci_device *pci_dev = ICE_DEV_TO_PCI(dev);
1258         struct rte_intr_handle *intr_handle = &pci_dev->intr_handle;
1259         uint16_t i;
1260
1261         /* avoid stopping again */
1262         if (pf->adapter_stopped)
1263                 return;
1264
1265         /* stop and clear all Rx queues */
1266         for (i = 0; i < data->nb_rx_queues; i++)
1267                 ice_rx_queue_stop(dev, i);
1268
1269         /* stop and clear all Tx queues */
1270         for (i = 0; i < data->nb_tx_queues; i++)
1271                 ice_tx_queue_stop(dev, i);
1272
1273         /* Clear all queues and release mbufs */
1274         ice_clear_queues(dev);
1275
1276         /* Clean datapath event and queue/vec mapping */
1277         rte_intr_efd_disable(intr_handle);
1278         if (intr_handle->intr_vec) {
1279                 rte_free(intr_handle->intr_vec);
1280                 intr_handle->intr_vec = NULL;
1281         }
1282
1283         pf->adapter_stopped = true;
1284 }
1285
1286 static void
1287 ice_dev_close(struct rte_eth_dev *dev)
1288 {
1289         struct ice_pf *pf = ICE_DEV_PRIVATE_TO_PF(dev->data->dev_private);
1290         struct ice_hw *hw = ICE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1291
1292         ice_dev_stop(dev);
1293
1294         /* release all queue resource */
1295         ice_free_queues(dev);
1296
1297         ice_res_pool_destroy(&pf->msix_pool);
1298         ice_release_vsi(pf->main_vsi);
1299
1300         ice_shutdown_all_ctrlq(hw);
1301 }
1302
1303 static int
1304 ice_dev_uninit(struct rte_eth_dev *dev)
1305 {
1306         struct ice_hw *hw = ICE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1307         struct ice_pf *pf = ICE_DEV_PRIVATE_TO_PF(dev->data->dev_private);
1308         struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
1309         struct rte_intr_handle *intr_handle = &pci_dev->intr_handle;
1310
1311         ice_dev_close(dev);
1312
1313         dev->dev_ops = NULL;
1314         dev->rx_pkt_burst = NULL;
1315         dev->tx_pkt_burst = NULL;
1316
1317         rte_free(dev->data->mac_addrs);
1318         dev->data->mac_addrs = NULL;
1319
1320         /* disable uio intr before callback unregister */
1321         rte_intr_disable(intr_handle);
1322
1323         /* register callback func to eal lib */
1324         rte_intr_callback_unregister(intr_handle,
1325                                      ice_interrupt_handler, dev);
1326
1327         ice_release_vsi(pf->main_vsi);
1328         ice_sched_cleanup_all(hw);
1329         rte_free(hw->port_info);
1330         ice_shutdown_all_ctrlq(hw);
1331
1332         return 0;
1333 }
1334
1335 static int
1336 ice_dev_configure(__rte_unused struct rte_eth_dev *dev)
1337 {
1338         struct ice_adapter *ad =
1339                 ICE_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
1340
1341         /* Initialize to TRUE. If any of Rx queues doesn't meet the
1342          * bulk allocation or vector Rx preconditions we will reset it.
1343          */
1344         ad->rx_bulk_alloc_allowed = true;
1345         ad->tx_simple_allowed = true;
1346
1347         return 0;
1348 }
1349
1350 static int ice_init_rss(struct ice_pf *pf)
1351 {
1352         struct ice_hw *hw = ICE_PF_TO_HW(pf);
1353         struct ice_vsi *vsi = pf->main_vsi;
1354         struct rte_eth_dev *dev = pf->adapter->eth_dev;
1355         struct rte_eth_rss_conf *rss_conf;
1356         struct ice_aqc_get_set_rss_keys key;
1357         uint16_t i, nb_q;
1358         int ret = 0;
1359
1360         rss_conf = &dev->data->dev_conf.rx_adv_conf.rss_conf;
1361         nb_q = dev->data->nb_rx_queues;
1362         vsi->rss_key_size = ICE_AQC_GET_SET_RSS_KEY_DATA_RSS_KEY_SIZE;
1363         vsi->rss_lut_size = hw->func_caps.common_cap.rss_table_size;
1364
1365         if (!vsi->rss_key)
1366                 vsi->rss_key = rte_zmalloc(NULL,
1367                                            vsi->rss_key_size, 0);
1368         if (!vsi->rss_lut)
1369                 vsi->rss_lut = rte_zmalloc(NULL,
1370                                            vsi->rss_lut_size, 0);
1371
1372         /* configure RSS key */
1373         if (!rss_conf->rss_key) {
1374                 /* Calculate the default hash key */
1375                 for (i = 0; i <= vsi->rss_key_size; i++)
1376                         vsi->rss_key[i] = (uint8_t)rte_rand();
1377         } else {
1378                 rte_memcpy(vsi->rss_key, rss_conf->rss_key,
1379                            RTE_MIN(rss_conf->rss_key_len,
1380                                    vsi->rss_key_size));
1381         }
1382         rte_memcpy(key.standard_rss_key, vsi->rss_key, vsi->rss_key_size);
1383         ret = ice_aq_set_rss_key(hw, vsi->idx, &key);
1384         if (ret)
1385                 return -EINVAL;
1386
1387         /* init RSS LUT table */
1388         for (i = 0; i < vsi->rss_lut_size; i++)
1389                 vsi->rss_lut[i] = i % nb_q;
1390
1391         ret = ice_aq_set_rss_lut(hw, vsi->idx,
1392                                  ICE_AQC_GSET_RSS_LUT_TABLE_TYPE_PF,
1393                                  vsi->rss_lut, vsi->rss_lut_size);
1394         if (ret)
1395                 return -EINVAL;
1396
1397         return 0;
1398 }
1399
1400 static int
1401 ice_dev_start(struct rte_eth_dev *dev)
1402 {
1403         struct rte_eth_dev_data *data = dev->data;
1404         struct ice_hw *hw = ICE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1405         struct ice_pf *pf = ICE_DEV_PRIVATE_TO_PF(dev->data->dev_private);
1406         uint16_t nb_rxq = 0;
1407         uint16_t nb_txq, i;
1408         int ret;
1409
1410         /* program Tx queues' context in hardware */
1411         for (nb_txq = 0; nb_txq < data->nb_tx_queues; nb_txq++) {
1412                 ret = ice_tx_queue_start(dev, nb_txq);
1413                 if (ret) {
1414                         PMD_DRV_LOG(ERR, "fail to start Tx queue %u", nb_txq);
1415                         goto tx_err;
1416                 }
1417         }
1418
1419         /* program Rx queues' context in hardware*/
1420         for (nb_rxq = 0; nb_rxq < data->nb_rx_queues; nb_rxq++) {
1421                 ret = ice_rx_queue_start(dev, nb_rxq);
1422                 if (ret) {
1423                         PMD_DRV_LOG(ERR, "fail to start Rx queue %u", nb_rxq);
1424                         goto rx_err;
1425                 }
1426         }
1427
1428         ret = ice_init_rss(pf);
1429         if (ret) {
1430                 PMD_DRV_LOG(ERR, "Failed to enable rss for PF");
1431                 goto rx_err;
1432         }
1433
1434         ice_set_rx_function(dev);
1435
1436         ret = ice_aq_set_event_mask(hw, hw->port_info->lport,
1437                                     ((u16)(ICE_AQ_LINK_EVENT_LINK_FAULT |
1438                                      ICE_AQ_LINK_EVENT_PHY_TEMP_ALARM |
1439                                      ICE_AQ_LINK_EVENT_EXCESSIVE_ERRORS |
1440                                      ICE_AQ_LINK_EVENT_SIGNAL_DETECT |
1441                                      ICE_AQ_LINK_EVENT_AN_COMPLETED |
1442                                      ICE_AQ_LINK_EVENT_PORT_TX_SUSPENDED)),
1443                                      NULL);
1444         if (ret != ICE_SUCCESS)
1445                 PMD_DRV_LOG(WARNING, "Fail to set phy mask");
1446
1447         /* Call get_link_info aq commond to enable/disable LSE */
1448         ice_link_update(dev, 0);
1449
1450         pf->adapter_stopped = false;
1451
1452         return 0;
1453
1454         /* stop the started queues if failed to start all queues */
1455 rx_err:
1456         for (i = 0; i < nb_rxq; i++)
1457                 ice_rx_queue_stop(dev, i);
1458 tx_err:
1459         for (i = 0; i < nb_txq; i++)
1460                 ice_tx_queue_stop(dev, i);
1461
1462         return -EIO;
1463 }
1464
1465 static int
1466 ice_dev_reset(struct rte_eth_dev *dev)
1467 {
1468         int ret;
1469
1470         if (dev->data->sriov.active)
1471                 return -ENOTSUP;
1472
1473         ret = ice_dev_uninit(dev);
1474         if (ret) {
1475                 PMD_INIT_LOG(ERR, "failed to uninit device, status = %d", ret);
1476                 return -ENXIO;
1477         }
1478
1479         ret = ice_dev_init(dev);
1480         if (ret) {
1481                 PMD_INIT_LOG(ERR, "failed to init device, status = %d", ret);
1482                 return -ENXIO;
1483         }
1484
1485         return 0;
1486 }
1487
1488 static void
1489 ice_dev_info_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *dev_info)
1490 {
1491         struct ice_pf *pf = ICE_DEV_PRIVATE_TO_PF(dev->data->dev_private);
1492         struct ice_hw *hw = ICE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1493         struct ice_vsi *vsi = pf->main_vsi;
1494         struct rte_pci_device *pci_dev = RTE_DEV_TO_PCI(dev->device);
1495
1496         dev_info->min_rx_bufsize = ICE_BUF_SIZE_MIN;
1497         dev_info->max_rx_pktlen = ICE_FRAME_SIZE_MAX;
1498         dev_info->max_rx_queues = vsi->nb_qps;
1499         dev_info->max_tx_queues = vsi->nb_qps;
1500         dev_info->max_mac_addrs = vsi->max_macaddrs;
1501         dev_info->max_vfs = pci_dev->max_vfs;
1502
1503         dev_info->rx_offload_capa =
1504                 DEV_RX_OFFLOAD_VLAN_STRIP |
1505                 DEV_RX_OFFLOAD_IPV4_CKSUM |
1506                 DEV_RX_OFFLOAD_UDP_CKSUM |
1507                 DEV_RX_OFFLOAD_TCP_CKSUM |
1508                 DEV_RX_OFFLOAD_QINQ_STRIP |
1509                 DEV_RX_OFFLOAD_OUTER_IPV4_CKSUM |
1510                 DEV_RX_OFFLOAD_VLAN_EXTEND |
1511                 DEV_RX_OFFLOAD_JUMBO_FRAME |
1512                 DEV_RX_OFFLOAD_KEEP_CRC |
1513                 DEV_RX_OFFLOAD_VLAN_FILTER;
1514         dev_info->tx_offload_capa =
1515                 DEV_TX_OFFLOAD_VLAN_INSERT |
1516                 DEV_TX_OFFLOAD_QINQ_INSERT |
1517                 DEV_TX_OFFLOAD_IPV4_CKSUM |
1518                 DEV_TX_OFFLOAD_UDP_CKSUM |
1519                 DEV_TX_OFFLOAD_TCP_CKSUM |
1520                 DEV_TX_OFFLOAD_SCTP_CKSUM |
1521                 DEV_TX_OFFLOAD_OUTER_IPV4_CKSUM |
1522                 DEV_TX_OFFLOAD_TCP_TSO |
1523                 DEV_TX_OFFLOAD_MULTI_SEGS;
1524         dev_info->rx_queue_offload_capa = 0;
1525         dev_info->tx_queue_offload_capa = 0;
1526
1527         dev_info->reta_size = hw->func_caps.common_cap.rss_table_size;
1528         dev_info->hash_key_size = (VSIQF_HKEY_MAX_INDEX + 1) * sizeof(uint32_t);
1529
1530         dev_info->default_rxconf = (struct rte_eth_rxconf) {
1531                 .rx_thresh = {
1532                         .pthresh = ICE_DEFAULT_RX_PTHRESH,
1533                         .hthresh = ICE_DEFAULT_RX_HTHRESH,
1534                         .wthresh = ICE_DEFAULT_RX_WTHRESH,
1535                 },
1536                 .rx_free_thresh = ICE_DEFAULT_RX_FREE_THRESH,
1537                 .rx_drop_en = 0,
1538                 .offloads = 0,
1539         };
1540
1541         dev_info->default_txconf = (struct rte_eth_txconf) {
1542                 .tx_thresh = {
1543                         .pthresh = ICE_DEFAULT_TX_PTHRESH,
1544                         .hthresh = ICE_DEFAULT_TX_HTHRESH,
1545                         .wthresh = ICE_DEFAULT_TX_WTHRESH,
1546                 },
1547                 .tx_free_thresh = ICE_DEFAULT_TX_FREE_THRESH,
1548                 .tx_rs_thresh = ICE_DEFAULT_TX_RSBIT_THRESH,
1549                 .offloads = 0,
1550         };
1551
1552         dev_info->rx_desc_lim = (struct rte_eth_desc_lim) {
1553                 .nb_max = ICE_MAX_RING_DESC,
1554                 .nb_min = ICE_MIN_RING_DESC,
1555                 .nb_align = ICE_ALIGN_RING_DESC,
1556         };
1557
1558         dev_info->tx_desc_lim = (struct rte_eth_desc_lim) {
1559                 .nb_max = ICE_MAX_RING_DESC,
1560                 .nb_min = ICE_MIN_RING_DESC,
1561                 .nb_align = ICE_ALIGN_RING_DESC,
1562         };
1563
1564         dev_info->speed_capa = ETH_LINK_SPEED_10M |
1565                                ETH_LINK_SPEED_100M |
1566                                ETH_LINK_SPEED_1G |
1567                                ETH_LINK_SPEED_2_5G |
1568                                ETH_LINK_SPEED_5G |
1569                                ETH_LINK_SPEED_10G |
1570                                ETH_LINK_SPEED_20G |
1571                                ETH_LINK_SPEED_25G |
1572                                ETH_LINK_SPEED_40G;
1573
1574         dev_info->nb_rx_queues = dev->data->nb_rx_queues;
1575         dev_info->nb_tx_queues = dev->data->nb_tx_queues;
1576
1577         dev_info->default_rxportconf.burst_size = ICE_RX_MAX_BURST;
1578         dev_info->default_txportconf.burst_size = ICE_TX_MAX_BURST;
1579         dev_info->default_rxportconf.nb_queues = 1;
1580         dev_info->default_txportconf.nb_queues = 1;
1581         dev_info->default_rxportconf.ring_size = ICE_BUF_SIZE_MIN;
1582         dev_info->default_txportconf.ring_size = ICE_BUF_SIZE_MIN;
1583 }
1584
1585 static inline int
1586 ice_atomic_read_link_status(struct rte_eth_dev *dev,
1587                             struct rte_eth_link *link)
1588 {
1589         struct rte_eth_link *dst = link;
1590         struct rte_eth_link *src = &dev->data->dev_link;
1591
1592         if (rte_atomic64_cmpset((uint64_t *)dst, *(uint64_t *)dst,
1593                                 *(uint64_t *)src) == 0)
1594                 return -1;
1595
1596         return 0;
1597 }
1598
1599 static inline int
1600 ice_atomic_write_link_status(struct rte_eth_dev *dev,
1601                              struct rte_eth_link *link)
1602 {
1603         struct rte_eth_link *dst = &dev->data->dev_link;
1604         struct rte_eth_link *src = link;
1605
1606         if (rte_atomic64_cmpset((uint64_t *)dst, *(uint64_t *)dst,
1607                                 *(uint64_t *)src) == 0)
1608                 return -1;
1609
1610         return 0;
1611 }
1612
1613 static int
1614 ice_link_update(struct rte_eth_dev *dev, __rte_unused int wait_to_complete)
1615 {
1616 #define CHECK_INTERVAL 100  /* 100ms */
1617 #define MAX_REPEAT_TIME 10  /* 1s (10 * 100ms) in total */
1618         struct ice_hw *hw = ICE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1619         struct ice_link_status link_status;
1620         struct rte_eth_link link, old;
1621         int status;
1622         unsigned int rep_cnt = MAX_REPEAT_TIME;
1623         bool enable_lse = dev->data->dev_conf.intr_conf.lsc ? true : false;
1624
1625         memset(&link, 0, sizeof(link));
1626         memset(&old, 0, sizeof(old));
1627         memset(&link_status, 0, sizeof(link_status));
1628         ice_atomic_read_link_status(dev, &old);
1629
1630         do {
1631                 /* Get link status information from hardware */
1632                 status = ice_aq_get_link_info(hw->port_info, enable_lse,
1633                                               &link_status, NULL);
1634                 if (status != ICE_SUCCESS) {
1635                         link.link_speed = ETH_SPEED_NUM_100M;
1636                         link.link_duplex = ETH_LINK_FULL_DUPLEX;
1637                         PMD_DRV_LOG(ERR, "Failed to get link info");
1638                         goto out;
1639                 }
1640
1641                 link.link_status = link_status.link_info & ICE_AQ_LINK_UP;
1642                 if (!wait_to_complete || link.link_status)
1643                         break;
1644
1645                 rte_delay_ms(CHECK_INTERVAL);
1646         } while (--rep_cnt);
1647
1648         if (!link.link_status)
1649                 goto out;
1650
1651         /* Full-duplex operation at all supported speeds */
1652         link.link_duplex = ETH_LINK_FULL_DUPLEX;
1653
1654         /* Parse the link status */
1655         switch (link_status.link_speed) {
1656         case ICE_AQ_LINK_SPEED_10MB:
1657                 link.link_speed = ETH_SPEED_NUM_10M;
1658                 break;
1659         case ICE_AQ_LINK_SPEED_100MB:
1660                 link.link_speed = ETH_SPEED_NUM_100M;
1661                 break;
1662         case ICE_AQ_LINK_SPEED_1000MB:
1663                 link.link_speed = ETH_SPEED_NUM_1G;
1664                 break;
1665         case ICE_AQ_LINK_SPEED_2500MB:
1666                 link.link_speed = ETH_SPEED_NUM_2_5G;
1667                 break;
1668         case ICE_AQ_LINK_SPEED_5GB:
1669                 link.link_speed = ETH_SPEED_NUM_5G;
1670                 break;
1671         case ICE_AQ_LINK_SPEED_10GB:
1672                 link.link_speed = ETH_SPEED_NUM_10G;
1673                 break;
1674         case ICE_AQ_LINK_SPEED_20GB:
1675                 link.link_speed = ETH_SPEED_NUM_20G;
1676                 break;
1677         case ICE_AQ_LINK_SPEED_25GB:
1678                 link.link_speed = ETH_SPEED_NUM_25G;
1679                 break;
1680         case ICE_AQ_LINK_SPEED_40GB:
1681                 link.link_speed = ETH_SPEED_NUM_40G;
1682                 break;
1683         case ICE_AQ_LINK_SPEED_UNKNOWN:
1684         default:
1685                 PMD_DRV_LOG(ERR, "Unknown link speed");
1686                 link.link_speed = ETH_SPEED_NUM_NONE;
1687                 break;
1688         }
1689
1690         link.link_autoneg = !(dev->data->dev_conf.link_speeds &
1691                               ETH_LINK_SPEED_FIXED);
1692
1693 out:
1694         ice_atomic_write_link_status(dev, &link);
1695         if (link.link_status == old.link_status)
1696                 return -1;
1697
1698         return 0;
1699 }
1700
1701 static int
1702 ice_mtu_set(struct rte_eth_dev *dev, uint16_t mtu)
1703 {
1704         struct ice_pf *pf = ICE_DEV_PRIVATE_TO_PF(dev->data->dev_private);
1705         struct rte_eth_dev_data *dev_data = pf->dev_data;
1706         uint32_t frame_size = mtu + ETHER_HDR_LEN
1707                               + ETHER_CRC_LEN + ICE_VLAN_TAG_SIZE;
1708
1709         /* check if mtu is within the allowed range */
1710         if (mtu < ETHER_MIN_MTU || frame_size > ICE_FRAME_SIZE_MAX)
1711                 return -EINVAL;
1712
1713         /* mtu setting is forbidden if port is start */
1714         if (dev_data->dev_started) {
1715                 PMD_DRV_LOG(ERR,
1716                             "port %d must be stopped before configuration",
1717                             dev_data->port_id);
1718                 return -EBUSY;
1719         }
1720
1721         if (frame_size > ETHER_MAX_LEN)
1722                 dev_data->dev_conf.rxmode.offloads |=
1723                         DEV_RX_OFFLOAD_JUMBO_FRAME;
1724         else
1725                 dev_data->dev_conf.rxmode.offloads &=
1726                         ~DEV_RX_OFFLOAD_JUMBO_FRAME;
1727
1728         dev_data->dev_conf.rxmode.max_rx_pkt_len = frame_size;
1729
1730         return 0;
1731 }
1732
1733 static int ice_macaddr_set(struct rte_eth_dev *dev,
1734                            struct ether_addr *mac_addr)
1735 {
1736         struct ice_hw *hw = ICE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1737         struct ice_pf *pf = ICE_DEV_PRIVATE_TO_PF(dev->data->dev_private);
1738         struct ice_vsi *vsi = pf->main_vsi;
1739         struct ice_mac_filter *f;
1740         uint8_t flags = 0;
1741         int ret;
1742
1743         if (!is_valid_assigned_ether_addr(mac_addr)) {
1744                 PMD_DRV_LOG(ERR, "Tried to set invalid MAC address.");
1745                 return -EINVAL;
1746         }
1747
1748         TAILQ_FOREACH(f, &vsi->mac_list, next) {
1749                 if (is_same_ether_addr(&pf->dev_addr, &f->mac_info.mac_addr))
1750                         break;
1751         }
1752
1753         if (!f) {
1754                 PMD_DRV_LOG(ERR, "Failed to find filter for default mac");
1755                 return -EIO;
1756         }
1757
1758         ret = ice_remove_mac_filter(vsi, &f->mac_info.mac_addr);
1759         if (ret != ICE_SUCCESS) {
1760                 PMD_DRV_LOG(ERR, "Failed to delete mac filter");
1761                 return -EIO;
1762         }
1763         ret = ice_add_mac_filter(vsi, mac_addr);
1764         if (ret != ICE_SUCCESS) {
1765                 PMD_DRV_LOG(ERR, "Failed to add mac filter");
1766                 return -EIO;
1767         }
1768         memcpy(&pf->dev_addr, mac_addr, ETH_ADDR_LEN);
1769
1770         flags = ICE_AQC_MAN_MAC_UPDATE_LAA_WOL;
1771         ret = ice_aq_manage_mac_write(hw, mac_addr->addr_bytes, flags, NULL);
1772         if (ret != ICE_SUCCESS)
1773                 PMD_DRV_LOG(ERR, "Failed to set manage mac");
1774
1775         return 0;
1776 }
1777
1778 /* Add a MAC address, and update filters */
1779 static int
1780 ice_macaddr_add(struct rte_eth_dev *dev,
1781                 struct ether_addr *mac_addr,
1782                 __rte_unused uint32_t index,
1783                 __rte_unused uint32_t pool)
1784 {
1785         struct ice_pf *pf = ICE_DEV_PRIVATE_TO_PF(dev->data->dev_private);
1786         struct ice_vsi *vsi = pf->main_vsi;
1787         int ret;
1788
1789         ret = ice_add_mac_filter(vsi, mac_addr);
1790         if (ret != ICE_SUCCESS) {
1791                 PMD_DRV_LOG(ERR, "Failed to add MAC filter");
1792                 return -EINVAL;
1793         }
1794
1795         return ICE_SUCCESS;
1796 }
1797
1798 /* Remove a MAC address, and update filters */
1799 static void
1800 ice_macaddr_remove(struct rte_eth_dev *dev, uint32_t index)
1801 {
1802         struct ice_pf *pf = ICE_DEV_PRIVATE_TO_PF(dev->data->dev_private);
1803         struct ice_vsi *vsi = pf->main_vsi;
1804         struct rte_eth_dev_data *data = dev->data;
1805         struct ether_addr *macaddr;
1806         int ret;
1807
1808         macaddr = &data->mac_addrs[index];
1809         ret = ice_remove_mac_filter(vsi, macaddr);
1810         if (ret) {
1811                 PMD_DRV_LOG(ERR, "Failed to remove MAC filter");
1812                 return;
1813         }
1814 }
1815
1816 static int
1817 ice_vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
1818 {
1819         struct ice_pf *pf = ICE_DEV_PRIVATE_TO_PF(dev->data->dev_private);
1820         struct ice_vsi *vsi = pf->main_vsi;
1821         int ret;
1822
1823         PMD_INIT_FUNC_TRACE();
1824
1825         if (on) {
1826                 ret = ice_add_vlan_filter(vsi, vlan_id);
1827                 if (ret < 0) {
1828                         PMD_DRV_LOG(ERR, "Failed to add vlan filter");
1829                         return -EINVAL;
1830                 }
1831         } else {
1832                 ret = ice_remove_vlan_filter(vsi, vlan_id);
1833                 if (ret < 0) {
1834                         PMD_DRV_LOG(ERR, "Failed to remove vlan filter");
1835                         return -EINVAL;
1836                 }
1837         }
1838
1839         return 0;
1840 }
1841
1842 /* Configure vlan filter on or off */
1843 static int
1844 ice_vsi_config_vlan_filter(struct ice_vsi *vsi, bool on)
1845 {
1846         struct ice_hw *hw = ICE_VSI_TO_HW(vsi);
1847         struct ice_vsi_ctx ctxt;
1848         uint8_t sec_flags, sw_flags2;
1849         int ret = 0;
1850
1851         sec_flags = ICE_AQ_VSI_SEC_TX_VLAN_PRUNE_ENA <<
1852                     ICE_AQ_VSI_SEC_TX_PRUNE_ENA_S;
1853         sw_flags2 = ICE_AQ_VSI_SW_FLAG_RX_VLAN_PRUNE_ENA;
1854
1855         if (on) {
1856                 vsi->info.sec_flags |= sec_flags;
1857                 vsi->info.sw_flags2 |= sw_flags2;
1858         } else {
1859                 vsi->info.sec_flags &= ~sec_flags;
1860                 vsi->info.sw_flags2 &= ~sw_flags2;
1861         }
1862         vsi->info.sw_id = hw->port_info->sw_id;
1863         (void)rte_memcpy(&ctxt.info, &vsi->info, sizeof(vsi->info));
1864         ctxt.info.valid_sections =
1865                 rte_cpu_to_le_16(ICE_AQ_VSI_PROP_SW_VALID |
1866                                  ICE_AQ_VSI_PROP_SECURITY_VALID);
1867         ctxt.vsi_num = vsi->vsi_id;
1868
1869         ret = ice_update_vsi(hw, vsi->idx, &ctxt, NULL);
1870         if (ret) {
1871                 PMD_DRV_LOG(INFO, "Update VSI failed to %s vlan rx pruning",
1872                             on ? "enable" : "disable");
1873                 ret = -EINVAL;
1874         } else {
1875                 vsi->info.valid_sections |=
1876                         rte_cpu_to_le_16(ICE_AQ_VSI_PROP_SW_VALID |
1877                                          ICE_AQ_VSI_PROP_SECURITY_VALID);
1878         }
1879
1880         return ret;
1881 }
1882
1883 static int
1884 ice_vsi_config_vlan_stripping(struct ice_vsi *vsi, bool on)
1885 {
1886         struct ice_hw *hw = ICE_VSI_TO_HW(vsi);
1887         struct ice_vsi_ctx ctxt;
1888         uint8_t vlan_flags;
1889         int ret = 0;
1890
1891         /* Check if it has been already on or off */
1892         if (vsi->info.valid_sections &
1893                 rte_cpu_to_le_16(ICE_AQ_VSI_PROP_VLAN_VALID)) {
1894                 if (on) {
1895                         if ((vsi->info.vlan_flags &
1896                              ICE_AQ_VSI_VLAN_EMOD_M) ==
1897                             ICE_AQ_VSI_VLAN_EMOD_STR_BOTH)
1898                                 return 0; /* already on */
1899                 } else {
1900                         if ((vsi->info.vlan_flags &
1901                              ICE_AQ_VSI_VLAN_EMOD_M) ==
1902                             ICE_AQ_VSI_VLAN_EMOD_NOTHING)
1903                                 return 0; /* already off */
1904                 }
1905         }
1906
1907         if (on)
1908                 vlan_flags = ICE_AQ_VSI_VLAN_EMOD_STR_BOTH;
1909         else
1910                 vlan_flags = ICE_AQ_VSI_VLAN_EMOD_NOTHING;
1911         vsi->info.vlan_flags &= ~(ICE_AQ_VSI_VLAN_EMOD_M);
1912         vsi->info.vlan_flags |= vlan_flags;
1913         (void)rte_memcpy(&ctxt.info, &vsi->info, sizeof(vsi->info));
1914         ctxt.info.valid_sections =
1915                 rte_cpu_to_le_16(ICE_AQ_VSI_PROP_VLAN_VALID);
1916         ctxt.vsi_num = vsi->vsi_id;
1917         ret = ice_update_vsi(hw, vsi->idx, &ctxt, NULL);
1918         if (ret) {
1919                 PMD_DRV_LOG(INFO, "Update VSI failed to %s vlan stripping",
1920                             on ? "enable" : "disable");
1921                 return -EINVAL;
1922         }
1923
1924         vsi->info.valid_sections |=
1925                 rte_cpu_to_le_16(ICE_AQ_VSI_PROP_VLAN_VALID);
1926
1927         return ret;
1928 }
1929
1930 static int
1931 ice_vlan_offload_set(struct rte_eth_dev *dev, int mask)
1932 {
1933         struct ice_pf *pf = ICE_DEV_PRIVATE_TO_PF(dev->data->dev_private);
1934         struct ice_vsi *vsi = pf->main_vsi;
1935         struct rte_eth_rxmode *rxmode;
1936
1937         rxmode = &dev->data->dev_conf.rxmode;
1938         if (mask & ETH_VLAN_FILTER_MASK) {
1939                 if (rxmode->offloads & DEV_RX_OFFLOAD_VLAN_FILTER)
1940                         ice_vsi_config_vlan_filter(vsi, TRUE);
1941                 else
1942                         ice_vsi_config_vlan_filter(vsi, FALSE);
1943         }
1944
1945         if (mask & ETH_VLAN_STRIP_MASK) {
1946                 if (rxmode->offloads & DEV_RX_OFFLOAD_VLAN_STRIP)
1947                         ice_vsi_config_vlan_stripping(vsi, TRUE);
1948                 else
1949                         ice_vsi_config_vlan_stripping(vsi, FALSE);
1950         }
1951
1952         if (mask & ETH_VLAN_EXTEND_MASK) {
1953                 if (rxmode->offloads & DEV_RX_OFFLOAD_VLAN_EXTEND)
1954                         ice_vsi_config_double_vlan(vsi, TRUE);
1955                 else
1956                         ice_vsi_config_double_vlan(vsi, FALSE);
1957         }
1958
1959         return 0;
1960 }
1961
1962 static int
1963 ice_vlan_tpid_set(struct rte_eth_dev *dev,
1964                   enum rte_vlan_type vlan_type,
1965                   uint16_t tpid)
1966 {
1967         struct ice_hw *hw = ICE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1968         uint64_t reg_r = 0, reg_w = 0;
1969         uint16_t reg_id = 0;
1970         int ret = 0;
1971         int qinq = dev->data->dev_conf.rxmode.offloads &
1972                    DEV_RX_OFFLOAD_VLAN_EXTEND;
1973
1974         switch (vlan_type) {
1975         case ETH_VLAN_TYPE_OUTER:
1976                 if (qinq)
1977                         reg_id = 3;
1978                 else
1979                         reg_id = 5;
1980         break;
1981         case ETH_VLAN_TYPE_INNER:
1982                 if (qinq) {
1983                         reg_id = 5;
1984                 } else {
1985                         PMD_DRV_LOG(ERR,
1986                                     "Unsupported vlan type in single vlan.");
1987                         return -EINVAL;
1988                 }
1989                 break;
1990         default:
1991                 PMD_DRV_LOG(ERR, "Unsupported vlan type %d", vlan_type);
1992                 return -EINVAL;
1993         }
1994         reg_r = ICE_READ_REG(hw, GL_SWT_L2TAGCTRL(reg_id));
1995         PMD_DRV_LOG(DEBUG, "Debug read from ICE GL_SWT_L2TAGCTRL[%d]: "
1996                     "0x%08"PRIx64"", reg_id, reg_r);
1997
1998         reg_w = reg_r & (~(GL_SWT_L2TAGCTRL_ETHERTYPE_M));
1999         reg_w |= ((uint64_t)tpid << GL_SWT_L2TAGCTRL_ETHERTYPE_S);
2000         if (reg_r == reg_w) {
2001                 PMD_DRV_LOG(DEBUG, "No need to write");
2002                 return 0;
2003         }
2004
2005         ICE_WRITE_REG(hw, GL_SWT_L2TAGCTRL(reg_id), reg_w);
2006         PMD_DRV_LOG(DEBUG, "Debug write 0x%08"PRIx64" to "
2007                     "ICE GL_SWT_L2TAGCTRL[%d]", reg_w, reg_id);
2008
2009         return ret;
2010 }
2011
2012 static int
2013 ice_vsi_vlan_pvid_set(struct ice_vsi *vsi, struct ice_vsi_vlan_pvid_info *info)
2014 {
2015         struct ice_hw *hw;
2016         struct ice_vsi_ctx ctxt;
2017         uint8_t vlan_flags = 0;
2018         int ret;
2019
2020         if (!vsi || !info) {
2021                 PMD_DRV_LOG(ERR, "invalid parameters");
2022                 return -EINVAL;
2023         }
2024
2025         if (info->on) {
2026                 vsi->info.pvid = info->config.pvid;
2027                 /**
2028                  * If insert pvid is enabled, only tagged pkts are
2029                  * allowed to be sent out.
2030                  */
2031                 vlan_flags = ICE_AQ_VSI_PVLAN_INSERT_PVID |
2032                              ICE_AQ_VSI_VLAN_MODE_UNTAGGED;
2033         } else {
2034                 vsi->info.pvid = 0;
2035                 if (info->config.reject.tagged == 0)
2036                         vlan_flags |= ICE_AQ_VSI_VLAN_MODE_TAGGED;
2037
2038                 if (info->config.reject.untagged == 0)
2039                         vlan_flags |= ICE_AQ_VSI_VLAN_MODE_UNTAGGED;
2040         }
2041         vsi->info.vlan_flags &= ~(ICE_AQ_VSI_PVLAN_INSERT_PVID |
2042                                   ICE_AQ_VSI_VLAN_MODE_M);
2043         vsi->info.vlan_flags |= vlan_flags;
2044         memset(&ctxt, 0, sizeof(ctxt));
2045         rte_memcpy(&ctxt.info, &vsi->info, sizeof(vsi->info));
2046         ctxt.info.valid_sections =
2047                 rte_cpu_to_le_16(ICE_AQ_VSI_PROP_VLAN_VALID);
2048         ctxt.vsi_num = vsi->vsi_id;
2049
2050         hw = ICE_VSI_TO_HW(vsi);
2051         ret = ice_update_vsi(hw, vsi->idx, &ctxt, NULL);
2052         if (ret != ICE_SUCCESS) {
2053                 PMD_DRV_LOG(ERR,
2054                             "update VSI for VLAN insert failed, err %d",
2055                             ret);
2056                 return -EINVAL;
2057         }
2058
2059         vsi->info.valid_sections |=
2060                 rte_cpu_to_le_16(ICE_AQ_VSI_PROP_VLAN_VALID);
2061
2062         return ret;
2063 }
2064
2065 static int
2066 ice_vlan_pvid_set(struct rte_eth_dev *dev, uint16_t pvid, int on)
2067 {
2068         struct ice_pf *pf = ICE_DEV_PRIVATE_TO_PF(dev->data->dev_private);
2069         struct ice_vsi *vsi = pf->main_vsi;
2070         struct rte_eth_dev_data *data = pf->dev_data;
2071         struct ice_vsi_vlan_pvid_info info;
2072         int ret;
2073
2074         memset(&info, 0, sizeof(info));
2075         info.on = on;
2076         if (info.on) {
2077                 info.config.pvid = pvid;
2078         } else {
2079                 info.config.reject.tagged =
2080                         data->dev_conf.txmode.hw_vlan_reject_tagged;
2081                 info.config.reject.untagged =
2082                         data->dev_conf.txmode.hw_vlan_reject_untagged;
2083         }
2084
2085         ret = ice_vsi_vlan_pvid_set(vsi, &info);
2086         if (ret < 0) {
2087                 PMD_DRV_LOG(ERR, "Failed to set pvid.");
2088                 return -EINVAL;
2089         }
2090
2091         return 0;
2092 }
2093
2094 static int
2095 ice_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
2096               struct rte_pci_device *pci_dev)
2097 {
2098         return rte_eth_dev_pci_generic_probe(pci_dev,
2099                                              sizeof(struct ice_adapter),
2100                                              ice_dev_init);
2101 }
2102
2103 static int
2104 ice_pci_remove(struct rte_pci_device *pci_dev)
2105 {
2106         return rte_eth_dev_pci_generic_remove(pci_dev, ice_dev_uninit);
2107 }
2108
2109 static struct rte_pci_driver rte_ice_pmd = {
2110         .id_table = pci_id_ice_map,
2111         .drv_flags = RTE_PCI_DRV_NEED_MAPPING | RTE_PCI_DRV_INTR_LSC |
2112                      RTE_PCI_DRV_IOVA_AS_VA,
2113         .probe = ice_pci_probe,
2114         .remove = ice_pci_remove,
2115 };
2116
2117 /**
2118  * Driver initialization routine.
2119  * Invoked once at EAL init time.
2120  * Register itself as the [Poll Mode] Driver of PCI devices.
2121  */
2122 RTE_PMD_REGISTER_PCI(net_ice, rte_ice_pmd);
2123 RTE_PMD_REGISTER_PCI_TABLE(net_ice, pci_id_ice_map);
2124 RTE_PMD_REGISTER_KMOD_DEP(net_ice, "* igb_uio | uio_pci_generic | vfio-pci");
2125 RTE_PMD_REGISTER_PARAM_STRING(net_ice,
2126                               ICE_MAX_QP_NUM "=<int>");
2127
2128 RTE_INIT(ice_init_log)
2129 {
2130         ice_logtype_init = rte_log_register("pmd.net.ice.init");
2131         if (ice_logtype_init >= 0)
2132                 rte_log_set_level(ice_logtype_init, RTE_LOG_NOTICE);
2133         ice_logtype_driver = rte_log_register("pmd.net.ice.driver");
2134         if (ice_logtype_driver >= 0)
2135                 rte_log_set_level(ice_logtype_driver, RTE_LOG_NOTICE);
2136 }