WIP FPC-III support
[linux/fpc-iii.git] / drivers / crypto / qat / qat_common / adf_accel_engine.c
blobca4eae8cdd0b3ad6061ee897762c3dd038b10cfa
1 // SPDX-License-Identifier: (BSD-3-Clause OR GPL-2.0-only)
2 /* Copyright(c) 2014 - 2020 Intel Corporation */
3 #include <linux/firmware.h>
4 #include <linux/pci.h>
5 #include "adf_cfg.h"
6 #include "adf_accel_devices.h"
7 #include "adf_common_drv.h"
8 #include "icp_qat_uclo.h"
10 static int adf_ae_fw_load_images(struct adf_accel_dev *accel_dev, void *fw_addr,
11 u32 fw_size)
13 struct adf_fw_loader_data *loader_data = accel_dev->fw_loader;
14 struct adf_hw_device_data *hw_device = accel_dev->hw_device;
15 struct icp_qat_fw_loader_handle *loader;
16 char *obj_name;
17 u32 num_objs;
18 u32 ae_mask;
19 int i;
21 loader = loader_data->fw_loader;
22 num_objs = hw_device->uof_get_num_objs();
24 for (i = 0; i < num_objs; i++) {
25 obj_name = hw_device->uof_get_name(i);
26 ae_mask = hw_device->uof_get_ae_mask(i);
28 if (qat_uclo_set_cfg_ae_mask(loader, ae_mask)) {
29 dev_err(&GET_DEV(accel_dev),
30 "Invalid mask for UOF image\n");
31 goto out_err;
33 if (qat_uclo_map_obj(loader, fw_addr, fw_size, obj_name)) {
34 dev_err(&GET_DEV(accel_dev),
35 "Failed to map UOF firmware\n");
36 goto out_err;
38 if (qat_uclo_wr_all_uimage(loader)) {
39 dev_err(&GET_DEV(accel_dev),
40 "Failed to load UOF firmware\n");
41 goto out_err;
43 qat_uclo_del_obj(loader);
46 return 0;
48 out_err:
49 adf_ae_fw_release(accel_dev);
50 return -EFAULT;
53 int adf_ae_fw_load(struct adf_accel_dev *accel_dev)
55 struct adf_fw_loader_data *loader_data = accel_dev->fw_loader;
56 struct adf_hw_device_data *hw_device = accel_dev->hw_device;
57 void *fw_addr, *mmp_addr;
58 u32 fw_size, mmp_size;
60 if (!hw_device->fw_name)
61 return 0;
63 if (request_firmware(&loader_data->mmp_fw, hw_device->fw_mmp_name,
64 &accel_dev->accel_pci_dev.pci_dev->dev)) {
65 dev_err(&GET_DEV(accel_dev), "Failed to load MMP firmware %s\n",
66 hw_device->fw_mmp_name);
67 return -EFAULT;
69 if (request_firmware(&loader_data->uof_fw, hw_device->fw_name,
70 &accel_dev->accel_pci_dev.pci_dev->dev)) {
71 dev_err(&GET_DEV(accel_dev), "Failed to load UOF firmware %s\n",
72 hw_device->fw_name);
73 goto out_err;
76 fw_size = loader_data->uof_fw->size;
77 fw_addr = (void *)loader_data->uof_fw->data;
78 mmp_size = loader_data->mmp_fw->size;
79 mmp_addr = (void *)loader_data->mmp_fw->data;
81 if (qat_uclo_wr_mimage(loader_data->fw_loader, mmp_addr, mmp_size)) {
82 dev_err(&GET_DEV(accel_dev), "Failed to load MMP\n");
83 goto out_err;
86 if (hw_device->uof_get_num_objs)
87 return adf_ae_fw_load_images(accel_dev, fw_addr, fw_size);
89 if (qat_uclo_map_obj(loader_data->fw_loader, fw_addr, fw_size, NULL)) {
90 dev_err(&GET_DEV(accel_dev), "Failed to map FW\n");
91 goto out_err;
93 if (qat_uclo_wr_all_uimage(loader_data->fw_loader)) {
94 dev_err(&GET_DEV(accel_dev), "Failed to load UOF\n");
95 goto out_err;
97 return 0;
99 out_err:
100 adf_ae_fw_release(accel_dev);
101 return -EFAULT;
104 void adf_ae_fw_release(struct adf_accel_dev *accel_dev)
106 struct adf_fw_loader_data *loader_data = accel_dev->fw_loader;
107 struct adf_hw_device_data *hw_device = accel_dev->hw_device;
109 if (!hw_device->fw_name)
110 return;
112 qat_uclo_del_obj(loader_data->fw_loader);
113 qat_hal_deinit(loader_data->fw_loader);
114 release_firmware(loader_data->uof_fw);
115 release_firmware(loader_data->mmp_fw);
116 loader_data->uof_fw = NULL;
117 loader_data->mmp_fw = NULL;
118 loader_data->fw_loader = NULL;
121 int adf_ae_start(struct adf_accel_dev *accel_dev)
123 struct adf_fw_loader_data *loader_data = accel_dev->fw_loader;
124 struct adf_hw_device_data *hw_data = accel_dev->hw_device;
125 u32 ae_ctr;
127 if (!hw_data->fw_name)
128 return 0;
130 ae_ctr = qat_hal_start(loader_data->fw_loader);
131 dev_info(&GET_DEV(accel_dev),
132 "qat_dev%d started %d acceleration engines\n",
133 accel_dev->accel_id, ae_ctr);
134 return 0;
137 int adf_ae_stop(struct adf_accel_dev *accel_dev)
139 struct adf_fw_loader_data *loader_data = accel_dev->fw_loader;
140 struct adf_hw_device_data *hw_data = accel_dev->hw_device;
141 u32 ae_ctr, ae, max_aes = GET_MAX_ACCELENGINES(accel_dev);
143 if (!hw_data->fw_name)
144 return 0;
146 for (ae = 0, ae_ctr = 0; ae < max_aes; ae++) {
147 if (hw_data->ae_mask & (1 << ae)) {
148 qat_hal_stop(loader_data->fw_loader, ae, 0xFF);
149 ae_ctr++;
152 dev_info(&GET_DEV(accel_dev),
153 "qat_dev%d stopped %d acceleration engines\n",
154 accel_dev->accel_id, ae_ctr);
155 return 0;
158 static int adf_ae_reset(struct adf_accel_dev *accel_dev, int ae)
160 struct adf_fw_loader_data *loader_data = accel_dev->fw_loader;
162 qat_hal_reset(loader_data->fw_loader);
163 if (qat_hal_clr_reset(loader_data->fw_loader))
164 return -EFAULT;
166 return 0;
169 int adf_ae_init(struct adf_accel_dev *accel_dev)
171 struct adf_fw_loader_data *loader_data;
172 struct adf_hw_device_data *hw_device = accel_dev->hw_device;
174 if (!hw_device->fw_name)
175 return 0;
177 loader_data = kzalloc(sizeof(*loader_data), GFP_KERNEL);
178 if (!loader_data)
179 return -ENOMEM;
181 accel_dev->fw_loader = loader_data;
182 if (qat_hal_init(accel_dev)) {
183 dev_err(&GET_DEV(accel_dev), "Failed to init the AEs\n");
184 kfree(loader_data);
185 return -EFAULT;
187 if (adf_ae_reset(accel_dev, 0)) {
188 dev_err(&GET_DEV(accel_dev), "Failed to reset the AEs\n");
189 qat_hal_deinit(loader_data->fw_loader);
190 kfree(loader_data);
191 return -EFAULT;
193 return 0;
196 int adf_ae_shutdown(struct adf_accel_dev *accel_dev)
198 struct adf_fw_loader_data *loader_data = accel_dev->fw_loader;
199 struct adf_hw_device_data *hw_device = accel_dev->hw_device;
201 if (!hw_device->fw_name)
202 return 0;
204 qat_hal_deinit(loader_data->fw_loader);
205 kfree(accel_dev->fw_loader);
206 accel_dev->fw_loader = NULL;
207 return 0;