net/ice: support device and queue 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
23 static const struct rte_pci_id pci_id_ice_map[] = {
24         { RTE_PCI_DEVICE(ICE_INTEL_VENDOR_ID, ICE_DEV_ID_E810C_BACKPLANE) },
25         { RTE_PCI_DEVICE(ICE_INTEL_VENDOR_ID, ICE_DEV_ID_E810C_QSFP) },
26         { RTE_PCI_DEVICE(ICE_INTEL_VENDOR_ID, ICE_DEV_ID_E810C_SFP) },
27         { .vendor_id = 0, /* sentinel */ },
28 };
29
30 static const struct eth_dev_ops ice_eth_dev_ops = {
31         .dev_configure                = ice_dev_configure,
32         .dev_start                    = ice_dev_start,
33         .dev_stop                     = ice_dev_stop,
34         .dev_close                    = ice_dev_close,
35         .dev_reset                    = ice_dev_reset,
36         .rx_queue_start               = ice_rx_queue_start,
37         .rx_queue_stop                = ice_rx_queue_stop,
38         .tx_queue_start               = ice_tx_queue_start,
39         .tx_queue_stop                = ice_tx_queue_stop,
40         .rx_queue_setup               = ice_rx_queue_setup,
41         .rx_queue_release             = ice_rx_queue_release,
42         .tx_queue_setup               = ice_tx_queue_setup,
43         .tx_queue_release             = ice_tx_queue_release,
44 };
45
46 static void
47 ice_init_controlq_parameter(struct ice_hw *hw)
48 {
49         /* fields for adminq */
50         hw->adminq.num_rq_entries = ICE_ADMINQ_LEN;
51         hw->adminq.num_sq_entries = ICE_ADMINQ_LEN;
52         hw->adminq.rq_buf_size = ICE_ADMINQ_BUF_SZ;
53         hw->adminq.sq_buf_size = ICE_ADMINQ_BUF_SZ;
54
55         /* fields for mailboxq, DPDK used as PF host */
56         hw->mailboxq.num_rq_entries = ICE_MAILBOXQ_LEN;
57         hw->mailboxq.num_sq_entries = ICE_MAILBOXQ_LEN;
58         hw->mailboxq.rq_buf_size = ICE_MAILBOXQ_BUF_SZ;
59         hw->mailboxq.sq_buf_size = ICE_MAILBOXQ_BUF_SZ;
60 }
61
62 static int
63 ice_check_qp_num(const char *key, const char *qp_value,
64                  __rte_unused void *opaque)
65 {
66         char *end = NULL;
67         int num = 0;
68
69         while (isblank(*qp_value))
70                 qp_value++;
71
72         num = strtoul(qp_value, &end, 10);
73
74         if (!num || (*end == '-') || errno) {
75                 PMD_DRV_LOG(WARNING, "invalid value:\"%s\" for key:\"%s\", "
76                             "value must be > 0",
77                             qp_value, key);
78                 return -1;
79         }
80
81         return num;
82 }
83
84 static int
85 ice_config_max_queue_pair_num(struct rte_devargs *devargs)
86 {
87         struct rte_kvargs *kvlist;
88         const char *queue_num_key = ICE_MAX_QP_NUM;
89         int ret;
90
91         if (!devargs)
92                 return 0;
93
94         kvlist = rte_kvargs_parse(devargs->args, NULL);
95         if (!kvlist)
96                 return 0;
97
98         if (!rte_kvargs_count(kvlist, queue_num_key)) {
99                 rte_kvargs_free(kvlist);
100                 return 0;
101         }
102
103         if (rte_kvargs_process(kvlist, queue_num_key,
104                                ice_check_qp_num, NULL) < 0) {
105                 rte_kvargs_free(kvlist);
106                 return 0;
107         }
108         ret = rte_kvargs_process(kvlist, queue_num_key,
109                                  ice_check_qp_num, NULL);
110         rte_kvargs_free(kvlist);
111
112         return ret;
113 }
114
115 static int
116 ice_res_pool_init(struct ice_res_pool_info *pool, uint32_t base,
117                   uint32_t num)
118 {
119         struct pool_entry *entry;
120
121         if (!pool || !num)
122                 return -EINVAL;
123
124         entry = rte_zmalloc(NULL, sizeof(*entry), 0);
125         if (!entry) {
126                 PMD_INIT_LOG(ERR,
127                              "Failed to allocate memory for resource pool");
128                 return -ENOMEM;
129         }
130
131         /* queue heap initialize */
132         pool->num_free = num;
133         pool->num_alloc = 0;
134         pool->base = base;
135         LIST_INIT(&pool->alloc_list);
136         LIST_INIT(&pool->free_list);
137
138         /* Initialize element  */
139         entry->base = 0;
140         entry->len = num;
141
142         LIST_INSERT_HEAD(&pool->free_list, entry, next);
143         return 0;
144 }
145
146 static int
147 ice_res_pool_alloc(struct ice_res_pool_info *pool,
148                    uint16_t num)
149 {
150         struct pool_entry *entry, *valid_entry;
151
152         if (!pool || !num) {
153                 PMD_INIT_LOG(ERR, "Invalid parameter");
154                 return -EINVAL;
155         }
156
157         if (pool->num_free < num) {
158                 PMD_INIT_LOG(ERR, "No resource. ask:%u, available:%u",
159                              num, pool->num_free);
160                 return -ENOMEM;
161         }
162
163         valid_entry = NULL;
164         /* Lookup  in free list and find most fit one */
165         LIST_FOREACH(entry, &pool->free_list, next) {
166                 if (entry->len >= num) {
167                         /* Find best one */
168                         if (entry->len == num) {
169                                 valid_entry = entry;
170                                 break;
171                         }
172                         if (!valid_entry ||
173                             valid_entry->len > entry->len)
174                                 valid_entry = entry;
175                 }
176         }
177
178         /* Not find one to satisfy the request, return */
179         if (!valid_entry) {
180                 PMD_INIT_LOG(ERR, "No valid entry found");
181                 return -ENOMEM;
182         }
183         /**
184          * The entry have equal queue number as requested,
185          * remove it from alloc_list.
186          */
187         if (valid_entry->len == num) {
188                 LIST_REMOVE(valid_entry, next);
189         } else {
190                 /**
191                  * The entry have more numbers than requested,
192                  * create a new entry for alloc_list and minus its
193                  * queue base and number in free_list.
194                  */
195                 entry = rte_zmalloc(NULL, sizeof(*entry), 0);
196                 if (!entry) {
197                         PMD_INIT_LOG(ERR,
198                                      "Failed to allocate memory for "
199                                      "resource pool");
200                         return -ENOMEM;
201                 }
202                 entry->base = valid_entry->base;
203                 entry->len = num;
204                 valid_entry->base += num;
205                 valid_entry->len -= num;
206                 valid_entry = entry;
207         }
208
209         /* Insert it into alloc list, not sorted */
210         LIST_INSERT_HEAD(&pool->alloc_list, valid_entry, next);
211
212         pool->num_free -= valid_entry->len;
213         pool->num_alloc += valid_entry->len;
214
215         return valid_entry->base + pool->base;
216 }
217
218 static void
219 ice_res_pool_destroy(struct ice_res_pool_info *pool)
220 {
221         struct pool_entry *entry, *next_entry;
222
223         if (!pool)
224                 return;
225
226         for (entry = LIST_FIRST(&pool->alloc_list);
227              entry && (next_entry = LIST_NEXT(entry, next), 1);
228              entry = next_entry) {
229                 LIST_REMOVE(entry, next);
230                 rte_free(entry);
231         }
232
233         for (entry = LIST_FIRST(&pool->free_list);
234              entry && (next_entry = LIST_NEXT(entry, next), 1);
235              entry = next_entry) {
236                 LIST_REMOVE(entry, next);
237                 rte_free(entry);
238         }
239
240         pool->num_free = 0;
241         pool->num_alloc = 0;
242         pool->base = 0;
243         LIST_INIT(&pool->alloc_list);
244         LIST_INIT(&pool->free_list);
245 }
246
247 static void
248 ice_vsi_config_default_rss(struct ice_aqc_vsi_props *info)
249 {
250         /* Set VSI LUT selection */
251         info->q_opt_rss = ICE_AQ_VSI_Q_OPT_RSS_LUT_VSI &
252                           ICE_AQ_VSI_Q_OPT_RSS_LUT_M;
253         /* Set Hash scheme */
254         info->q_opt_rss |= ICE_AQ_VSI_Q_OPT_RSS_TPLZ &
255                            ICE_AQ_VSI_Q_OPT_RSS_HASH_M;
256         /* enable TC */
257         info->q_opt_tc = ICE_AQ_VSI_Q_OPT_TC_OVR_M;
258 }
259
260 static enum ice_status
261 ice_vsi_config_tc_queue_mapping(struct ice_vsi *vsi,
262                                 struct ice_aqc_vsi_props *info,
263                                 uint8_t enabled_tcmap)
264 {
265         uint16_t bsf, qp_idx;
266
267         /* default tc 0 now. Multi-TC supporting need to be done later.
268          * Configure TC and queue mapping parameters, for enabled TC,
269          * allocate qpnum_per_tc queues to this traffic.
270          */
271         if (enabled_tcmap != 0x01) {
272                 PMD_INIT_LOG(ERR, "only TC0 is supported");
273                 return -ENOTSUP;
274         }
275
276         vsi->nb_qps = RTE_MIN(vsi->nb_qps, ICE_MAX_Q_PER_TC);
277         bsf = rte_bsf32(vsi->nb_qps);
278         /* Adjust the queue number to actual queues that can be applied */
279         vsi->nb_qps = 0x1 << bsf;
280
281         qp_idx = 0;
282         /* Set tc and queue mapping with VSI */
283         info->tc_mapping[0] = rte_cpu_to_le_16((qp_idx <<
284                                                 ICE_AQ_VSI_TC_Q_OFFSET_S) |
285                                                (bsf << ICE_AQ_VSI_TC_Q_NUM_S));
286
287         /* Associate queue number with VSI */
288         info->mapping_flags |= rte_cpu_to_le_16(ICE_AQ_VSI_Q_MAP_CONTIG);
289         info->q_mapping[0] = rte_cpu_to_le_16(vsi->base_queue);
290         info->q_mapping[1] = rte_cpu_to_le_16(vsi->nb_qps);
291         info->valid_sections |=
292                 rte_cpu_to_le_16(ICE_AQ_VSI_PROP_RXQ_MAP_VALID);
293         /* Set the info.ingress_table and info.egress_table
294          * for UP translate table. Now just set it to 1:1 map by default
295          * -- 0b 111 110 101 100 011 010 001 000 == 0xFAC688
296          */
297 #define ICE_TC_QUEUE_TABLE_DFLT 0x00FAC688
298         info->ingress_table  = rte_cpu_to_le_32(ICE_TC_QUEUE_TABLE_DFLT);
299         info->egress_table   = rte_cpu_to_le_32(ICE_TC_QUEUE_TABLE_DFLT);
300         info->outer_up_table = rte_cpu_to_le_32(ICE_TC_QUEUE_TABLE_DFLT);
301         return 0;
302 }
303
304 static int
305 ice_init_mac_address(struct rte_eth_dev *dev)
306 {
307         struct ice_hw *hw = ICE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
308
309         if (!is_unicast_ether_addr
310                 ((struct ether_addr *)hw->port_info[0].mac.lan_addr)) {
311                 PMD_INIT_LOG(ERR, "Invalid MAC address");
312                 return -EINVAL;
313         }
314
315         ether_addr_copy((struct ether_addr *)hw->port_info[0].mac.lan_addr,
316                         (struct ether_addr *)hw->port_info[0].mac.perm_addr);
317
318         dev->data->mac_addrs = rte_zmalloc(NULL, sizeof(struct ether_addr), 0);
319         if (!dev->data->mac_addrs) {
320                 PMD_INIT_LOG(ERR,
321                              "Failed to allocate memory to store mac address");
322                 return -ENOMEM;
323         }
324         /* store it to dev data */
325         ether_addr_copy((struct ether_addr *)hw->port_info[0].mac.perm_addr,
326                         &dev->data->mac_addrs[0]);
327         return 0;
328 }
329
330 /*  Initialize SW parameters of PF */
331 static int
332 ice_pf_sw_init(struct rte_eth_dev *dev)
333 {
334         struct ice_pf *pf = ICE_DEV_PRIVATE_TO_PF(dev->data->dev_private);
335         struct ice_hw *hw = ICE_PF_TO_HW(pf);
336
337         if (ice_config_max_queue_pair_num(dev->device->devargs) > 0)
338                 pf->lan_nb_qp_max =
339                         ice_config_max_queue_pair_num(dev->device->devargs);
340         else
341                 pf->lan_nb_qp_max =
342                         (uint16_t)RTE_MIN(hw->func_caps.common_cap.num_txq,
343                                           hw->func_caps.common_cap.num_rxq);
344
345         pf->lan_nb_qps = pf->lan_nb_qp_max;
346
347         return 0;
348 }
349
350 static struct ice_vsi *
351 ice_setup_vsi(struct ice_pf *pf, enum ice_vsi_type type)
352 {
353         struct ice_hw *hw = ICE_PF_TO_HW(pf);
354         struct ice_vsi *vsi = NULL;
355         struct ice_vsi_ctx vsi_ctx;
356         int ret;
357         uint16_t max_txqs[ICE_MAX_TRAFFIC_CLASS] = { 0 };
358         uint8_t tc_bitmap = 0x1;
359
360         /* hw->num_lports = 1 in NIC mode */
361         vsi = rte_zmalloc(NULL, sizeof(struct ice_vsi), 0);
362         if (!vsi)
363                 return NULL;
364
365         vsi->idx = pf->next_vsi_idx;
366         pf->next_vsi_idx++;
367         vsi->type = type;
368         vsi->adapter = ICE_PF_TO_ADAPTER(pf);
369         vsi->max_macaddrs = ICE_NUM_MACADDR_MAX;
370         vsi->vlan_anti_spoof_on = 0;
371         vsi->vlan_filter_on = 1;
372         TAILQ_INIT(&vsi->mac_list);
373         TAILQ_INIT(&vsi->vlan_list);
374
375         memset(&vsi_ctx, 0, sizeof(vsi_ctx));
376         /* base_queue in used in queue mapping of VSI add/update command.
377          * Suppose vsi->base_queue is 0 now, don't consider SRIOV, VMDQ
378          * cases in the first stage. Only Main VSI.
379          */
380         vsi->base_queue = 0;
381         switch (type) {
382         case ICE_VSI_PF:
383                 vsi->nb_qps = pf->lan_nb_qps;
384                 ice_vsi_config_default_rss(&vsi_ctx.info);
385                 vsi_ctx.alloc_from_pool = true;
386                 vsi_ctx.flags = ICE_AQ_VSI_TYPE_PF;
387                 /* switch_id is queried by get_switch_config aq, which is done
388                  * by ice_init_hw
389                  */
390                 vsi_ctx.info.sw_id = hw->port_info->sw_id;
391                 vsi_ctx.info.sw_flags2 = ICE_AQ_VSI_SW_FLAG_LAN_ENA;
392                 /* Allow all untagged or tagged packets */
393                 vsi_ctx.info.vlan_flags = ICE_AQ_VSI_VLAN_MODE_ALL;
394                 vsi_ctx.info.vlan_flags |= ICE_AQ_VSI_VLAN_EMOD_NOTHING;
395                 vsi_ctx.info.q_opt_rss = ICE_AQ_VSI_Q_OPT_RSS_LUT_PF |
396                                          ICE_AQ_VSI_Q_OPT_RSS_TPLZ;
397                 /* Enable VLAN/UP trip */
398                 ret = ice_vsi_config_tc_queue_mapping(vsi,
399                                                       &vsi_ctx.info,
400                                                       ICE_DEFAULT_TCMAP);
401                 if (ret) {
402                         PMD_INIT_LOG(ERR,
403                                      "tc queue mapping with vsi failed, "
404                                      "err = %d",
405                                      ret);
406                         goto fail_mem;
407                 }
408
409                 break;
410         default:
411                 /* for other types of VSI */
412                 PMD_INIT_LOG(ERR, "other types of VSI not supported");
413                 goto fail_mem;
414         }
415
416         /* VF has MSIX interrupt in VF range, don't allocate here */
417         if (type == ICE_VSI_PF) {
418                 ret = ice_res_pool_alloc(&pf->msix_pool,
419                                          RTE_MIN(vsi->nb_qps,
420                                                  RTE_MAX_RXTX_INTR_VEC_ID));
421                 if (ret < 0) {
422                         PMD_INIT_LOG(ERR, "VSI MAIN %d get heap failed %d",
423                                      vsi->vsi_id, ret);
424                 }
425                 vsi->msix_intr = ret;
426                 vsi->nb_msix = RTE_MIN(vsi->nb_qps, RTE_MAX_RXTX_INTR_VEC_ID);
427         } else {
428                 vsi->msix_intr = 0;
429                 vsi->nb_msix = 0;
430         }
431         ret = ice_add_vsi(hw, vsi->idx, &vsi_ctx, NULL);
432         if (ret != ICE_SUCCESS) {
433                 PMD_INIT_LOG(ERR, "add vsi failed, err = %d", ret);
434                 goto fail_mem;
435         }
436         /* store vsi information is SW structure */
437         vsi->vsi_id = vsi_ctx.vsi_num;
438         vsi->info = vsi_ctx.info;
439         pf->vsis_allocated = vsi_ctx.vsis_allocd;
440         pf->vsis_unallocated = vsi_ctx.vsis_unallocated;
441
442         /* At the beginning, only TC0. */
443         /* What we need here is the maximam number of the TX queues.
444          * Currently vsi->nb_qps means it.
445          * Correct it if any change.
446          */
447         max_txqs[0] = vsi->nb_qps;
448         ret = ice_cfg_vsi_lan(hw->port_info, vsi->idx,
449                               tc_bitmap, max_txqs);
450         if (ret != ICE_SUCCESS)
451                 PMD_INIT_LOG(ERR, "Failed to config vsi sched");
452
453         return vsi;
454 fail_mem:
455         rte_free(vsi);
456         pf->next_vsi_idx--;
457         return NULL;
458 }
459
460 static int
461 ice_pf_setup(struct ice_pf *pf)
462 {
463         struct ice_vsi *vsi;
464
465         /* Clear all stats counters */
466         pf->offset_loaded = FALSE;
467         memset(&pf->stats, 0, sizeof(struct ice_hw_port_stats));
468         memset(&pf->stats_offset, 0, sizeof(struct ice_hw_port_stats));
469         memset(&pf->internal_stats, 0, sizeof(struct ice_eth_stats));
470         memset(&pf->internal_stats_offset, 0, sizeof(struct ice_eth_stats));
471
472         vsi = ice_setup_vsi(pf, ICE_VSI_PF);
473         if (!vsi) {
474                 PMD_INIT_LOG(ERR, "Failed to add vsi for PF");
475                 return -EINVAL;
476         }
477
478         pf->main_vsi = vsi;
479
480         return 0;
481 }
482
483 static int
484 ice_dev_init(struct rte_eth_dev *dev)
485 {
486         struct rte_pci_device *pci_dev;
487         struct ice_hw *hw = ICE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
488         struct ice_pf *pf = ICE_DEV_PRIVATE_TO_PF(dev->data->dev_private);
489         int ret;
490
491         dev->dev_ops = &ice_eth_dev_ops;
492
493         pci_dev = RTE_DEV_TO_PCI(dev->device);
494
495         pf->adapter = ICE_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
496         pf->adapter->eth_dev = dev;
497         pf->dev_data = dev->data;
498         hw->back = pf->adapter;
499         hw->hw_addr = (uint8_t *)pci_dev->mem_resource[0].addr;
500         hw->vendor_id = pci_dev->id.vendor_id;
501         hw->device_id = pci_dev->id.device_id;
502         hw->subsystem_vendor_id = pci_dev->id.subsystem_vendor_id;
503         hw->subsystem_device_id = pci_dev->id.subsystem_device_id;
504         hw->bus.device = pci_dev->addr.devid;
505         hw->bus.func = pci_dev->addr.function;
506
507         ice_init_controlq_parameter(hw);
508
509         ret = ice_init_hw(hw);
510         if (ret) {
511                 PMD_INIT_LOG(ERR, "Failed to initialize HW");
512                 return -EINVAL;
513         }
514
515         PMD_INIT_LOG(INFO, "FW %d.%d.%05d API %d.%d",
516                      hw->fw_maj_ver, hw->fw_min_ver, hw->fw_build,
517                      hw->api_maj_ver, hw->api_min_ver);
518
519         ice_pf_sw_init(dev);
520         ret = ice_init_mac_address(dev);
521         if (ret) {
522                 PMD_INIT_LOG(ERR, "Failed to initialize mac address");
523                 goto err_init_mac;
524         }
525
526         ret = ice_res_pool_init(&pf->msix_pool, 1,
527                                 hw->func_caps.common_cap.num_msix_vectors - 1);
528         if (ret) {
529                 PMD_INIT_LOG(ERR, "Failed to init MSIX pool");
530                 goto err_msix_pool_init;
531         }
532
533         ret = ice_pf_setup(pf);
534         if (ret) {
535                 PMD_INIT_LOG(ERR, "Failed to setup PF");
536                 goto err_pf_setup;
537         }
538
539         return 0;
540
541 err_pf_setup:
542         ice_res_pool_destroy(&pf->msix_pool);
543 err_msix_pool_init:
544         rte_free(dev->data->mac_addrs);
545 err_init_mac:
546         ice_sched_cleanup_all(hw);
547         rte_free(hw->port_info);
548         ice_shutdown_all_ctrlq(hw);
549
550         return ret;
551 }
552
553 static int
554 ice_release_vsi(struct ice_vsi *vsi)
555 {
556         struct ice_hw *hw;
557         struct ice_vsi_ctx vsi_ctx;
558         enum ice_status ret;
559
560         if (!vsi)
561                 return 0;
562
563         hw = ICE_VSI_TO_HW(vsi);
564
565         memset(&vsi_ctx, 0, sizeof(vsi_ctx));
566
567         vsi_ctx.vsi_num = vsi->vsi_id;
568         vsi_ctx.info = vsi->info;
569         ret = ice_free_vsi(hw, vsi->idx, &vsi_ctx, false, NULL);
570         if (ret != ICE_SUCCESS) {
571                 PMD_INIT_LOG(ERR, "Failed to free vsi by aq, %u", vsi->vsi_id);
572                 rte_free(vsi);
573                 return -1;
574         }
575
576         rte_free(vsi);
577         return 0;
578 }
579
580 static void
581 ice_dev_stop(struct rte_eth_dev *dev)
582 {
583         struct rte_eth_dev_data *data = dev->data;
584         struct ice_pf *pf = ICE_DEV_PRIVATE_TO_PF(dev->data->dev_private);
585         uint16_t i;
586
587         /* avoid stopping again */
588         if (pf->adapter_stopped)
589                 return;
590
591         /* stop and clear all Rx queues */
592         for (i = 0; i < data->nb_rx_queues; i++)
593                 ice_rx_queue_stop(dev, i);
594
595         /* stop and clear all Tx queues */
596         for (i = 0; i < data->nb_tx_queues; i++)
597                 ice_tx_queue_stop(dev, i);
598
599         /* Clear all queues and release mbufs */
600         ice_clear_queues(dev);
601
602         pf->adapter_stopped = true;
603 }
604
605 static void
606 ice_dev_close(struct rte_eth_dev *dev)
607 {
608         struct ice_pf *pf = ICE_DEV_PRIVATE_TO_PF(dev->data->dev_private);
609         struct ice_hw *hw = ICE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
610
611         ice_dev_stop(dev);
612
613         /* release all queue resource */
614         ice_free_queues(dev);
615
616         ice_res_pool_destroy(&pf->msix_pool);
617         ice_release_vsi(pf->main_vsi);
618
619         ice_shutdown_all_ctrlq(hw);
620 }
621
622 static int
623 ice_dev_uninit(struct rte_eth_dev *dev)
624 {
625         struct ice_hw *hw = ICE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
626         struct ice_pf *pf = ICE_DEV_PRIVATE_TO_PF(dev->data->dev_private);
627
628         ice_dev_close(dev);
629
630         dev->dev_ops = NULL;
631         dev->rx_pkt_burst = NULL;
632         dev->tx_pkt_burst = NULL;
633
634         rte_free(dev->data->mac_addrs);
635         dev->data->mac_addrs = NULL;
636
637         ice_release_vsi(pf->main_vsi);
638         ice_sched_cleanup_all(hw);
639         rte_free(hw->port_info);
640         ice_shutdown_all_ctrlq(hw);
641
642         return 0;
643 }
644
645 static int
646 ice_dev_configure(__rte_unused struct rte_eth_dev *dev)
647 {
648         struct ice_adapter *ad =
649                 ICE_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
650
651         /* Initialize to TRUE. If any of Rx queues doesn't meet the
652          * bulk allocation or vector Rx preconditions we will reset it.
653          */
654         ad->rx_bulk_alloc_allowed = true;
655         ad->tx_simple_allowed = true;
656
657         return 0;
658 }
659
660 static int ice_init_rss(struct ice_pf *pf)
661 {
662         struct ice_hw *hw = ICE_PF_TO_HW(pf);
663         struct ice_vsi *vsi = pf->main_vsi;
664         struct rte_eth_dev *dev = pf->adapter->eth_dev;
665         struct rte_eth_rss_conf *rss_conf;
666         struct ice_aqc_get_set_rss_keys key;
667         uint16_t i, nb_q;
668         int ret = 0;
669
670         rss_conf = &dev->data->dev_conf.rx_adv_conf.rss_conf;
671         nb_q = dev->data->nb_rx_queues;
672         vsi->rss_key_size = ICE_AQC_GET_SET_RSS_KEY_DATA_RSS_KEY_SIZE;
673         vsi->rss_lut_size = hw->func_caps.common_cap.rss_table_size;
674
675         if (!vsi->rss_key)
676                 vsi->rss_key = rte_zmalloc(NULL,
677                                            vsi->rss_key_size, 0);
678         if (!vsi->rss_lut)
679                 vsi->rss_lut = rte_zmalloc(NULL,
680                                            vsi->rss_lut_size, 0);
681
682         /* configure RSS key */
683         if (!rss_conf->rss_key) {
684                 /* Calculate the default hash key */
685                 for (i = 0; i <= vsi->rss_key_size; i++)
686                         vsi->rss_key[i] = (uint8_t)rte_rand();
687         } else {
688                 rte_memcpy(vsi->rss_key, rss_conf->rss_key,
689                            RTE_MIN(rss_conf->rss_key_len,
690                                    vsi->rss_key_size));
691         }
692         rte_memcpy(key.standard_rss_key, vsi->rss_key, vsi->rss_key_size);
693         ret = ice_aq_set_rss_key(hw, vsi->idx, &key);
694         if (ret)
695                 return -EINVAL;
696
697         /* init RSS LUT table */
698         for (i = 0; i < vsi->rss_lut_size; i++)
699                 vsi->rss_lut[i] = i % nb_q;
700
701         ret = ice_aq_set_rss_lut(hw, vsi->idx,
702                                  ICE_AQC_GSET_RSS_LUT_TABLE_TYPE_PF,
703                                  vsi->rss_lut, vsi->rss_lut_size);
704         if (ret)
705                 return -EINVAL;
706
707         return 0;
708 }
709
710 static int
711 ice_dev_start(struct rte_eth_dev *dev)
712 {
713         struct rte_eth_dev_data *data = dev->data;
714         struct ice_hw *hw = ICE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
715         struct ice_pf *pf = ICE_DEV_PRIVATE_TO_PF(dev->data->dev_private);
716         uint16_t nb_rxq = 0;
717         uint16_t nb_txq, i;
718         int ret;
719
720         /* program Tx queues' context in hardware */
721         for (nb_txq = 0; nb_txq < data->nb_tx_queues; nb_txq++) {
722                 ret = ice_tx_queue_start(dev, nb_txq);
723                 if (ret) {
724                         PMD_DRV_LOG(ERR, "fail to start Tx queue %u", nb_txq);
725                         goto tx_err;
726                 }
727         }
728
729         /* program Rx queues' context in hardware*/
730         for (nb_rxq = 0; nb_rxq < data->nb_rx_queues; nb_rxq++) {
731                 ret = ice_rx_queue_start(dev, nb_rxq);
732                 if (ret) {
733                         PMD_DRV_LOG(ERR, "fail to start Rx queue %u", nb_rxq);
734                         goto rx_err;
735                 }
736         }
737
738         ret = ice_init_rss(pf);
739         if (ret) {
740                 PMD_DRV_LOG(ERR, "Failed to enable rss for PF");
741                 goto rx_err;
742         }
743
744         ret = ice_aq_set_event_mask(hw, hw->port_info->lport,
745                                     ((u16)(ICE_AQ_LINK_EVENT_LINK_FAULT |
746                                      ICE_AQ_LINK_EVENT_PHY_TEMP_ALARM |
747                                      ICE_AQ_LINK_EVENT_EXCESSIVE_ERRORS |
748                                      ICE_AQ_LINK_EVENT_SIGNAL_DETECT |
749                                      ICE_AQ_LINK_EVENT_AN_COMPLETED |
750                                      ICE_AQ_LINK_EVENT_PORT_TX_SUSPENDED)),
751                                      NULL);
752         if (ret != ICE_SUCCESS)
753                 PMD_DRV_LOG(WARNING, "Fail to set phy mask");
754
755         pf->adapter_stopped = false;
756
757         return 0;
758
759         /* stop the started queues if failed to start all queues */
760 rx_err:
761         for (i = 0; i < nb_rxq; i++)
762                 ice_rx_queue_stop(dev, i);
763 tx_err:
764         for (i = 0; i < nb_txq; i++)
765                 ice_tx_queue_stop(dev, i);
766
767         return -EIO;
768 }
769
770 static int
771 ice_dev_reset(struct rte_eth_dev *dev)
772 {
773         int ret;
774
775         if (dev->data->sriov.active)
776                 return -ENOTSUP;
777
778         ret = ice_dev_uninit(dev);
779         if (ret) {
780                 PMD_INIT_LOG(ERR, "failed to uninit device, status = %d", ret);
781                 return -ENXIO;
782         }
783
784         ret = ice_dev_init(dev);
785         if (ret) {
786                 PMD_INIT_LOG(ERR, "failed to init device, status = %d", ret);
787                 return -ENXIO;
788         }
789
790         return 0;
791 }
792
793 static int
794 ice_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
795               struct rte_pci_device *pci_dev)
796 {
797         return rte_eth_dev_pci_generic_probe(pci_dev,
798                                              sizeof(struct ice_adapter),
799                                              ice_dev_init);
800 }
801
802 static int
803 ice_pci_remove(struct rte_pci_device *pci_dev)
804 {
805         return rte_eth_dev_pci_generic_remove(pci_dev, ice_dev_uninit);
806 }
807
808 static struct rte_pci_driver rte_ice_pmd = {
809         .id_table = pci_id_ice_map,
810         .drv_flags = RTE_PCI_DRV_NEED_MAPPING | RTE_PCI_DRV_INTR_LSC |
811                      RTE_PCI_DRV_IOVA_AS_VA,
812         .probe = ice_pci_probe,
813         .remove = ice_pci_remove,
814 };
815
816 /**
817  * Driver initialization routine.
818  * Invoked once at EAL init time.
819  * Register itself as the [Poll Mode] Driver of PCI devices.
820  */
821 RTE_PMD_REGISTER_PCI(net_ice, rte_ice_pmd);
822 RTE_PMD_REGISTER_PCI_TABLE(net_ice, pci_id_ice_map);
823 RTE_PMD_REGISTER_KMOD_DEP(net_ice, "* igb_uio | uio_pci_generic | vfio-pci");
824 RTE_PMD_REGISTER_PARAM_STRING(net_ice,
825                               ICE_MAX_QP_NUM "=<int>");
826
827 RTE_INIT(ice_init_log)
828 {
829         ice_logtype_init = rte_log_register("pmd.net.ice.init");
830         if (ice_logtype_init >= 0)
831                 rte_log_set_level(ice_logtype_init, RTE_LOG_NOTICE);
832         ice_logtype_driver = rte_log_register("pmd.net.ice.driver");
833         if (ice_logtype_driver >= 0)
834                 rte_log_set_level(ice_logtype_driver, RTE_LOG_NOTICE);
835 }