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"
9
adf_ae_fw_load(struct adf_accel_dev * accel_dev)10 int adf_ae_fw_load(struct adf_accel_dev *accel_dev)
11 {
12 struct adf_fw_loader_data *loader_data = accel_dev->fw_loader;
13 struct adf_hw_device_data *hw_device = accel_dev->hw_device;
14 void *uof_addr, *mmp_addr;
15 u32 uof_size, mmp_size;
16
17 if (!hw_device->fw_name)
18 return 0;
19
20 if (request_firmware(&loader_data->mmp_fw, hw_device->fw_mmp_name,
21 &accel_dev->accel_pci_dev.pci_dev->dev)) {
22 dev_err(&GET_DEV(accel_dev), "Failed to load MMP firmware %s\n",
23 hw_device->fw_mmp_name);
24 return -EFAULT;
25 }
26 if (request_firmware(&loader_data->uof_fw, hw_device->fw_name,
27 &accel_dev->accel_pci_dev.pci_dev->dev)) {
28 dev_err(&GET_DEV(accel_dev), "Failed to load UOF firmware %s\n",
29 hw_device->fw_name);
30 goto out_err;
31 }
32
33 uof_size = loader_data->uof_fw->size;
34 uof_addr = (void *)loader_data->uof_fw->data;
35 mmp_size = loader_data->mmp_fw->size;
36 mmp_addr = (void *)loader_data->mmp_fw->data;
37 if (qat_uclo_wr_mimage(loader_data->fw_loader, mmp_addr, mmp_size)) {
38 dev_err(&GET_DEV(accel_dev), "Failed to load MMP\n");
39 goto out_err;
40 }
41 if (qat_uclo_map_obj(loader_data->fw_loader, uof_addr, uof_size)) {
42 dev_err(&GET_DEV(accel_dev), "Failed to map FW\n");
43 goto out_err;
44 }
45 if (qat_uclo_wr_all_uimage(loader_data->fw_loader)) {
46 dev_err(&GET_DEV(accel_dev), "Failed to load UOF\n");
47 goto out_err;
48 }
49 return 0;
50
51 out_err:
52 adf_ae_fw_release(accel_dev);
53 return -EFAULT;
54 }
55
adf_ae_fw_release(struct adf_accel_dev * accel_dev)56 void adf_ae_fw_release(struct adf_accel_dev *accel_dev)
57 {
58 struct adf_fw_loader_data *loader_data = accel_dev->fw_loader;
59 struct adf_hw_device_data *hw_device = accel_dev->hw_device;
60
61 if (!hw_device->fw_name)
62 return;
63
64 qat_uclo_del_uof_obj(loader_data->fw_loader);
65 qat_hal_deinit(loader_data->fw_loader);
66 release_firmware(loader_data->uof_fw);
67 release_firmware(loader_data->mmp_fw);
68 loader_data->uof_fw = NULL;
69 loader_data->mmp_fw = NULL;
70 loader_data->fw_loader = NULL;
71 }
72
adf_ae_start(struct adf_accel_dev * accel_dev)73 int adf_ae_start(struct adf_accel_dev *accel_dev)
74 {
75 struct adf_fw_loader_data *loader_data = accel_dev->fw_loader;
76 struct adf_hw_device_data *hw_data = accel_dev->hw_device;
77 u32 ae_ctr, ae, max_aes = GET_MAX_ACCELENGINES(accel_dev);
78
79 if (!hw_data->fw_name)
80 return 0;
81
82 for (ae = 0, ae_ctr = 0; ae < max_aes; ae++) {
83 if (hw_data->ae_mask & (1 << ae)) {
84 qat_hal_start(loader_data->fw_loader, ae, 0xFF);
85 ae_ctr++;
86 }
87 }
88 dev_info(&GET_DEV(accel_dev),
89 "qat_dev%d started %d acceleration engines\n",
90 accel_dev->accel_id, ae_ctr);
91 return 0;
92 }
93
adf_ae_stop(struct adf_accel_dev * accel_dev)94 int adf_ae_stop(struct adf_accel_dev *accel_dev)
95 {
96 struct adf_fw_loader_data *loader_data = accel_dev->fw_loader;
97 struct adf_hw_device_data *hw_data = accel_dev->hw_device;
98 u32 ae_ctr, ae, max_aes = GET_MAX_ACCELENGINES(accel_dev);
99
100 if (!hw_data->fw_name)
101 return 0;
102
103 for (ae = 0, ae_ctr = 0; ae < max_aes; ae++) {
104 if (hw_data->ae_mask & (1 << ae)) {
105 qat_hal_stop(loader_data->fw_loader, ae, 0xFF);
106 ae_ctr++;
107 }
108 }
109 dev_info(&GET_DEV(accel_dev),
110 "qat_dev%d stopped %d acceleration engines\n",
111 accel_dev->accel_id, ae_ctr);
112 return 0;
113 }
114
adf_ae_reset(struct adf_accel_dev * accel_dev,int ae)115 static int adf_ae_reset(struct adf_accel_dev *accel_dev, int ae)
116 {
117 struct adf_fw_loader_data *loader_data = accel_dev->fw_loader;
118
119 qat_hal_reset(loader_data->fw_loader);
120 if (qat_hal_clr_reset(loader_data->fw_loader))
121 return -EFAULT;
122
123 return 0;
124 }
125
adf_ae_init(struct adf_accel_dev * accel_dev)126 int adf_ae_init(struct adf_accel_dev *accel_dev)
127 {
128 struct adf_fw_loader_data *loader_data;
129 struct adf_hw_device_data *hw_device = accel_dev->hw_device;
130
131 if (!hw_device->fw_name)
132 return 0;
133
134 loader_data = kzalloc(sizeof(*loader_data), GFP_KERNEL);
135 if (!loader_data)
136 return -ENOMEM;
137
138 accel_dev->fw_loader = loader_data;
139 if (qat_hal_init(accel_dev)) {
140 dev_err(&GET_DEV(accel_dev), "Failed to init the AEs\n");
141 kfree(loader_data);
142 return -EFAULT;
143 }
144 if (adf_ae_reset(accel_dev, 0)) {
145 dev_err(&GET_DEV(accel_dev), "Failed to reset the AEs\n");
146 qat_hal_deinit(loader_data->fw_loader);
147 kfree(loader_data);
148 return -EFAULT;
149 }
150 return 0;
151 }
152
adf_ae_shutdown(struct adf_accel_dev * accel_dev)153 int adf_ae_shutdown(struct adf_accel_dev *accel_dev)
154 {
155 struct adf_fw_loader_data *loader_data = accel_dev->fw_loader;
156 struct adf_hw_device_data *hw_device = accel_dev->hw_device;
157
158 if (!hw_device->fw_name)
159 return 0;
160
161 qat_hal_deinit(loader_data->fw_loader);
162 kfree(accel_dev->fw_loader);
163 accel_dev->fw_loader = NULL;
164 return 0;
165 }
166