1 // SPDX-License-Identifier: (BSD-3-Clause OR GPL-2.0-only)
2 /* Copyright(c) 2014 - 2020 Intel Corporation */
3 #include <linux/kernel.h>
4 #include <linux/init.h>
5 #include <linux/types.h>
6 #include <linux/pci.h>
7 #include <linux/slab.h>
8 #include <linux/errno.h>
9 #include <linux/interrupt.h>
10 #include <linux/workqueue.h>
11 #include "adf_accel_devices.h"
12 #include "adf_common_drv.h"
13 #include "adf_cfg.h"
14 #include "adf_cfg_strings.h"
15 #include "adf_cfg_common.h"
16 #include "adf_transport_access_macros.h"
17 #include "adf_transport_internal.h"
18 #include "adf_pf2vf_msg.h"
19
20 #define ADF_VINTSOU_OFFSET 0x204
21 #define ADF_VINTSOU_BUN BIT(0)
22 #define ADF_VINTSOU_PF2VF BIT(1)
23
24 static struct workqueue_struct *adf_vf_stop_wq;
25
26 struct adf_vf_stop_data {
27 struct adf_accel_dev *accel_dev;
28 struct work_struct work;
29 };
30
adf_enable_msi(struct adf_accel_dev * accel_dev)31 static int adf_enable_msi(struct adf_accel_dev *accel_dev)
32 {
33 struct adf_accel_pci *pci_dev_info = &accel_dev->accel_pci_dev;
34 int stat = pci_enable_msi(pci_dev_info->pci_dev);
35
36 if (stat) {
37 dev_err(&GET_DEV(accel_dev),
38 "Failed to enable MSI interrupts\n");
39 return stat;
40 }
41
42 accel_dev->vf.irq_name = kzalloc(ADF_MAX_MSIX_VECTOR_NAME, GFP_KERNEL);
43 if (!accel_dev->vf.irq_name)
44 return -ENOMEM;
45
46 return stat;
47 }
48
adf_disable_msi(struct adf_accel_dev * accel_dev)49 static void adf_disable_msi(struct adf_accel_dev *accel_dev)
50 {
51 struct pci_dev *pdev = accel_to_pci_dev(accel_dev);
52
53 kfree(accel_dev->vf.irq_name);
54 pci_disable_msi(pdev);
55 }
56
adf_dev_stop_async(struct work_struct * work)57 static void adf_dev_stop_async(struct work_struct *work)
58 {
59 struct adf_vf_stop_data *stop_data =
60 container_of(work, struct adf_vf_stop_data, work);
61 struct adf_accel_dev *accel_dev = stop_data->accel_dev;
62
63 adf_dev_stop(accel_dev);
64 adf_dev_shutdown(accel_dev);
65
66 /* Re-enable PF2VF interrupts */
67 adf_enable_pf2vf_interrupts(accel_dev);
68 kfree(stop_data);
69 }
70
adf_pf2vf_bh_handler(void * data)71 static void adf_pf2vf_bh_handler(void *data)
72 {
73 struct adf_accel_dev *accel_dev = data;
74 struct adf_hw_device_data *hw_data = accel_dev->hw_device;
75 struct adf_bar *pmisc =
76 &GET_BARS(accel_dev)[hw_data->get_misc_bar_id(hw_data)];
77 void __iomem *pmisc_bar_addr = pmisc->virt_addr;
78 u32 msg;
79
80 /* Read the message from PF */
81 msg = ADF_CSR_RD(pmisc_bar_addr, hw_data->get_pf2vf_offset(0));
82
83 if (!(msg & ADF_PF2VF_MSGORIGIN_SYSTEM))
84 /* Ignore legacy non-system (non-kernel) PF2VF messages */
85 goto err;
86
87 switch ((msg & ADF_PF2VF_MSGTYPE_MASK) >> ADF_PF2VF_MSGTYPE_SHIFT) {
88 case ADF_PF2VF_MSGTYPE_RESTARTING: {
89 struct adf_vf_stop_data *stop_data;
90
91 dev_dbg(&GET_DEV(accel_dev),
92 "Restarting msg received from PF 0x%x\n", msg);
93
94 clear_bit(ADF_STATUS_PF_RUNNING, &accel_dev->status);
95
96 stop_data = kzalloc(sizeof(*stop_data), GFP_ATOMIC);
97 if (!stop_data) {
98 dev_err(&GET_DEV(accel_dev),
99 "Couldn't schedule stop for vf_%d\n",
100 accel_dev->accel_id);
101 return;
102 }
103 stop_data->accel_dev = accel_dev;
104 INIT_WORK(&stop_data->work, adf_dev_stop_async);
105 queue_work(adf_vf_stop_wq, &stop_data->work);
106 /* To ack, clear the PF2VFINT bit */
107 msg &= ~ADF_PF2VF_INT;
108 ADF_CSR_WR(pmisc_bar_addr, hw_data->get_pf2vf_offset(0), msg);
109 return;
110 }
111 case ADF_PF2VF_MSGTYPE_VERSION_RESP:
112 dev_dbg(&GET_DEV(accel_dev),
113 "Version resp received from PF 0x%x\n", msg);
114 accel_dev->vf.pf_version =
115 (msg & ADF_PF2VF_VERSION_RESP_VERS_MASK) >>
116 ADF_PF2VF_VERSION_RESP_VERS_SHIFT;
117 accel_dev->vf.compatible =
118 (msg & ADF_PF2VF_VERSION_RESP_RESULT_MASK) >>
119 ADF_PF2VF_VERSION_RESP_RESULT_SHIFT;
120 complete(&accel_dev->vf.iov_msg_completion);
121 break;
122 default:
123 goto err;
124 }
125
126 /* To ack, clear the PF2VFINT bit */
127 msg &= ~ADF_PF2VF_INT;
128 ADF_CSR_WR(pmisc_bar_addr, hw_data->get_pf2vf_offset(0), msg);
129
130 /* Re-enable PF2VF interrupts */
131 adf_enable_pf2vf_interrupts(accel_dev);
132 return;
133 err:
134 dev_err(&GET_DEV(accel_dev),
135 "Unknown message from PF (0x%x); leaving PF2VF ints disabled\n",
136 msg);
137 }
138
adf_setup_pf2vf_bh(struct adf_accel_dev * accel_dev)139 static int adf_setup_pf2vf_bh(struct adf_accel_dev *accel_dev)
140 {
141 tasklet_init(&accel_dev->vf.pf2vf_bh_tasklet,
142 (void *)adf_pf2vf_bh_handler, (unsigned long)accel_dev);
143
144 mutex_init(&accel_dev->vf.vf2pf_lock);
145 return 0;
146 }
147
adf_cleanup_pf2vf_bh(struct adf_accel_dev * accel_dev)148 static void adf_cleanup_pf2vf_bh(struct adf_accel_dev *accel_dev)
149 {
150 tasklet_disable(&accel_dev->vf.pf2vf_bh_tasklet);
151 tasklet_kill(&accel_dev->vf.pf2vf_bh_tasklet);
152 mutex_destroy(&accel_dev->vf.vf2pf_lock);
153 }
154
adf_isr(int irq,void * privdata)155 static irqreturn_t adf_isr(int irq, void *privdata)
156 {
157 struct adf_accel_dev *accel_dev = privdata;
158 struct adf_hw_device_data *hw_data = accel_dev->hw_device;
159 struct adf_bar *pmisc =
160 &GET_BARS(accel_dev)[hw_data->get_misc_bar_id(hw_data)];
161 void __iomem *pmisc_bar_addr = pmisc->virt_addr;
162 u32 v_int;
163
164 /* Read VF INT source CSR to determine the source of VF interrupt */
165 v_int = ADF_CSR_RD(pmisc_bar_addr, ADF_VINTSOU_OFFSET);
166
167 /* Check for PF2VF interrupt */
168 if (v_int & ADF_VINTSOU_PF2VF) {
169 /* Disable PF to VF interrupt */
170 adf_disable_pf2vf_interrupts(accel_dev);
171
172 /* Schedule tasklet to handle interrupt BH */
173 tasklet_hi_schedule(&accel_dev->vf.pf2vf_bh_tasklet);
174 return IRQ_HANDLED;
175 }
176
177 /* Check bundle interrupt */
178 if (v_int & ADF_VINTSOU_BUN) {
179 struct adf_etr_data *etr_data = accel_dev->transport;
180 struct adf_etr_bank_data *bank = &etr_data->banks[0];
181
182 /* Disable Flag and Coalesce Ring Interrupts */
183 WRITE_CSR_INT_FLAG_AND_COL(bank->csr_addr, bank->bank_number,
184 0);
185 tasklet_hi_schedule(&bank->resp_handler);
186 return IRQ_HANDLED;
187 }
188
189 return IRQ_NONE;
190 }
191
adf_request_msi_irq(struct adf_accel_dev * accel_dev)192 static int adf_request_msi_irq(struct adf_accel_dev *accel_dev)
193 {
194 struct pci_dev *pdev = accel_to_pci_dev(accel_dev);
195 unsigned int cpu;
196 int ret;
197
198 snprintf(accel_dev->vf.irq_name, ADF_MAX_MSIX_VECTOR_NAME,
199 "qat_%02x:%02d.%02d", pdev->bus->number, PCI_SLOT(pdev->devfn),
200 PCI_FUNC(pdev->devfn));
201 ret = request_irq(pdev->irq, adf_isr, 0, accel_dev->vf.irq_name,
202 (void *)accel_dev);
203 if (ret) {
204 dev_err(&GET_DEV(accel_dev), "failed to enable irq for %s\n",
205 accel_dev->vf.irq_name);
206 return ret;
207 }
208 cpu = accel_dev->accel_id % num_online_cpus();
209 irq_set_affinity_hint(pdev->irq, get_cpu_mask(cpu));
210
211 return ret;
212 }
213
adf_setup_bh(struct adf_accel_dev * accel_dev)214 static int adf_setup_bh(struct adf_accel_dev *accel_dev)
215 {
216 struct adf_etr_data *priv_data = accel_dev->transport;
217
218 tasklet_init(&priv_data->banks[0].resp_handler, adf_response_handler,
219 (unsigned long)priv_data->banks);
220 return 0;
221 }
222
adf_cleanup_bh(struct adf_accel_dev * accel_dev)223 static void adf_cleanup_bh(struct adf_accel_dev *accel_dev)
224 {
225 struct adf_etr_data *priv_data = accel_dev->transport;
226
227 tasklet_disable(&priv_data->banks[0].resp_handler);
228 tasklet_kill(&priv_data->banks[0].resp_handler);
229 }
230
231 /**
232 * adf_vf_isr_resource_free() - Free IRQ for acceleration device
233 * @accel_dev: Pointer to acceleration device.
234 *
235 * Function frees interrupts for acceleration device virtual function.
236 */
adf_vf_isr_resource_free(struct adf_accel_dev * accel_dev)237 void adf_vf_isr_resource_free(struct adf_accel_dev *accel_dev)
238 {
239 struct pci_dev *pdev = accel_to_pci_dev(accel_dev);
240
241 irq_set_affinity_hint(pdev->irq, NULL);
242 free_irq(pdev->irq, (void *)accel_dev);
243 adf_cleanup_bh(accel_dev);
244 adf_cleanup_pf2vf_bh(accel_dev);
245 adf_disable_msi(accel_dev);
246 }
247 EXPORT_SYMBOL_GPL(adf_vf_isr_resource_free);
248
249 /**
250 * adf_vf_isr_resource_alloc() - Allocate IRQ for acceleration device
251 * @accel_dev: Pointer to acceleration device.
252 *
253 * Function allocates interrupts for acceleration device virtual function.
254 *
255 * Return: 0 on success, error code otherwise.
256 */
adf_vf_isr_resource_alloc(struct adf_accel_dev * accel_dev)257 int adf_vf_isr_resource_alloc(struct adf_accel_dev *accel_dev)
258 {
259 if (adf_enable_msi(accel_dev))
260 goto err_out;
261
262 if (adf_setup_pf2vf_bh(accel_dev))
263 goto err_out;
264
265 if (adf_setup_bh(accel_dev))
266 goto err_out;
267
268 if (adf_request_msi_irq(accel_dev))
269 goto err_out;
270
271 return 0;
272 err_out:
273 adf_vf_isr_resource_free(accel_dev);
274 return -EFAULT;
275 }
276 EXPORT_SYMBOL_GPL(adf_vf_isr_resource_alloc);
277
adf_init_vf_wq(void)278 int __init adf_init_vf_wq(void)
279 {
280 adf_vf_stop_wq = alloc_workqueue("adf_vf_stop_wq", WQ_MEM_RECLAIM, 0);
281
282 return !adf_vf_stop_wq ? -EFAULT : 0;
283 }
284
adf_exit_vf_wq(void)285 void adf_exit_vf_wq(void)
286 {
287 if (adf_vf_stop_wq)
288 destroy_workqueue(adf_vf_stop_wq);
289
290 adf_vf_stop_wq = NULL;
291 }
292