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:
6 # http://www.apache.org/licenses/LICENSE-2.0
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.
14 """QAT util library."""
16 from resources.libraries.python.DUTSetup import DUTSetup
17 from resources.libraries.python.topology import Topology
18 from resources.libraries.python.VPPUtil import VPPUtil
22 """Contains methods for setting up QATs."""
25 def crypto_device_verify(node, crypto_type, numvfs, force_init=True):
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.
30 :param node: DUT node.
31 :crypto_type: Crypto device type - HW_DH895xcc, HW_C3xxx, HW_C4xxx
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.
36 :type crypto_type: string
38 :type force_init: bool
40 :raises RuntimeError: If QAT VFs are not created and force init is set
43 pci_addr = Topology.get_cryptodev(node)
46 # QAT is not initialized and we want to initialize with numvfs.
47 QATUtil.crypto_device_init(node, crypto_type, numvfs)
50 f"QAT device failed to create VFs on {node[u'host']}"
53 # QAT VF devices must be re-bound to vfio-pci driver before use.
54 pci_addr = Topology.get_cryptodev(node)
55 for i in range(numvfs):
56 DUTSetup.pci_vf_driver_unbind(node, pci_addr, i)
57 DUTSetup.pci_vf_driver_bind(node, pci_addr, i, "vfio-pci")
60 def crypto_device_init(node, crypto_type, numvfs):
61 """Init Crypto QAT device virtual functions on DUT.
63 :param node: DUT node.
64 :crypto_type: Crypto device type - HW_DH895xcc, HW_C3xxx, HW_C4xxx
66 :param numvfs: Number of VFs to initialize, 0 - disable the VFs.
68 :type crypto_type: string
71 :raises RuntimeError: If failed to stop VPP or QAT failed to initialize.
73 if crypto_type == u"HW_DH895xcc":
74 kernel_mod = u"qat_dh895xcc"
75 kernel_drv = u"dh895xcc"
76 elif crypto_type == u"HW_C3xxx":
77 kernel_mod = u"qat_c3xxx"
79 elif crypto_type == u"HW_C4xxx":
80 kernel_mod = u"qat_c4xxx"
82 elif crypto_type == u"HW_4xxx":
83 kernel_mod = u"qat_4xxx"
87 f"Unsupported crypto device type on {node[u'host']}"
90 pci_addr = Topology.get_cryptodev(node)
92 # QAT device must be re-bound to kernel driver before initialization.
93 DUTSetup.verify_kernel_module(node, kernel_mod, force_load=True)
95 # Stop VPP to prevent deadlock.
96 VPPUtil.stop_vpp_service(node)
98 current_driver = DUTSetup.get_pci_dev_driver(
99 node, pci_addr.replace(u":", r"\:")
101 if current_driver is not None:
102 DUTSetup.pci_driver_unbind(node, pci_addr)
104 # Bind to kernel driver.
105 DUTSetup.pci_driver_bind(node, pci_addr, kernel_drv)
107 # Initialize QAT VFs.
109 DUTSetup.set_sriov_numvfs(node, pci_addr, numvfs)