fix(core): QAT initialization
[csit.git] / resources / libraries / python / QATUtil.py
1 # Copyright (c) 2023 Cisco and/or its affiliates.
2 # Licensed under the Apache License, Version 2.0 (the "License");
3 # you may not use this file except in compliance with the License.
4 # You may obtain a copy of the License at:
5 #
6 #     http://www.apache.org/licenses/LICENSE-2.0
7 #
8 # Unless required by applicable law or agreed to in writing, software
9 # distributed under the License is distributed on an "AS IS" BASIS,
10 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 # See the License for the specific language governing permissions and
12 # limitations under the License.
13
14 """QAT util library."""
15
16 from resources.libraries.python.DUTSetup import DUTSetup
17 from resources.libraries.python.topology import Topology
18 from resources.libraries.python.VPPUtil import VPPUtil
19
20
21 class QATUtil:
22     """Contains methods for setting up QATs."""
23
24     @staticmethod
25     def crypto_device_verify(node, crypto_type, numvfs, force_init=False):
26         """Verify if Crypto QAT device virtual functions are initialized on all
27         DUTs. If parameter force initialization is set to True, then try to
28         initialize or remove VFs on QAT.
29
30         :param node: DUT node.
31         :crypto_type: Crypto device type - HW_DH895xcc, HW_C3xxx, HW_C4xxx
32                       or HW_4xxx.
33         :param numvfs: Number of VFs to initialize, 0 - disable the VFs.
34         :param force_init: If True then try to initialize to specific value.
35         :type node: dict
36         :type crypto_type: string
37         :type numvfs: int
38         :type force_init: bool
39         :returns: nothing
40         :raises RuntimeError: If QAT VFs are not created and force init is set
41                               to False.
42         """
43         pci_addr = Topology.get_cryptodev(node)
44         sriov_numvfs = DUTSetup.get_sriov_numvfs(node, pci_addr)
45
46         if sriov_numvfs != numvfs:
47             if force_init:
48                 # QAT is not initialized and we want to initialize with numvfs
49                 QATUtil.crypto_device_init(node, crypto_type, numvfs)
50             else:
51                 raise RuntimeError(
52                     f"QAT device failed to create VFs on {node[u'host']}"
53                 )
54
55     @staticmethod
56     def crypto_device_init(node, crypto_type, numvfs):
57         """Init Crypto QAT device virtual functions on DUT.
58
59         :param node: DUT node.
60         :crypto_type: Crypto device type - HW_DH895xcc, HW_C3xxx, HW_C4xxx
61                       or HW_4xxx.
62         :param numvfs: Number of VFs to initialize, 0 - disable the VFs.
63         :type node: dict
64         :type crypto_type: string
65         :type numvfs: int
66         :returns: nothing
67         :raises RuntimeError: If failed to stop VPP or QAT failed to initialize.
68         """
69         if crypto_type == u"HW_DH895xcc":
70             kernel_mod = u"qat_dh895xcc"
71             kernel_drv = u"dh895xcc"
72         elif crypto_type == u"HW_C3xxx":
73             kernel_mod = u"qat_c3xxx"
74             kernel_drv = u"c3xxx"
75         elif crypto_type == u"HW_C4xxx":
76             kernel_mod = u"qat_c4xxx"
77             kernel_drv = u"c4xxx"
78         elif crypto_type == u"HW_4xxx":
79             kernel_mod = u"qat_4xxx"
80             kernel_drv = u"4xxx"
81         else:
82             raise RuntimeError(
83                 f"Unsupported crypto device type on {node[u'host']}"
84             )
85
86         pci_addr = Topology.get_cryptodev(node)
87
88         # QAT device must be re-bound to kernel driver before initialization.
89         DUTSetup.verify_kernel_module(node, kernel_mod, force_load=True)
90
91         # Stop VPP to prevent deadlock.
92         VPPUtil.stop_vpp_service(node)
93
94         current_driver = DUTSetup.get_pci_dev_driver(
95             node, pci_addr.replace(u":", r"\:")
96         )
97         if current_driver is not None:
98             DUTSetup.pci_driver_unbind(node, pci_addr)
99
100         # Bind to kernel driver.
101         DUTSetup.pci_driver_bind(node, pci_addr, kernel_drv)
102
103         # Initialize QAT VFs.
104         if numvfs > 0:
105             DUTSetup.set_sriov_numvfs(node, pci_addr, numvfs)