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>
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"
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"
20 #define ADF_VINTSOU_OFFSET 0x204
21 #define ADF_VINTMSK_OFFSET 0x208
22 #define ADF_VINTSOU_BUN BIT(0)
23 #define ADF_VINTSOU_PF2VF BIT(1)
25 static struct workqueue_struct *adf_vf_stop_wq;
27 struct adf_vf_stop_data {
28 struct adf_accel_dev *accel_dev;
29 struct work_struct work;
32 void adf_enable_pf2vf_interrupts(struct adf_accel_dev *accel_dev)
34 struct adf_accel_pci *pci_info = &accel_dev->accel_pci_dev;
35 struct adf_hw_device_data *hw_data = accel_dev->hw_device;
36 void __iomem *pmisc_bar_addr =
37 pci_info->pci_bars[hw_data->get_misc_bar_id(hw_data)].virt_addr;
39 ADF_CSR_WR(pmisc_bar_addr, ADF_VINTMSK_OFFSET, 0x0);
42 void adf_disable_pf2vf_interrupts(struct adf_accel_dev *accel_dev)
44 struct adf_accel_pci *pci_info = &accel_dev->accel_pci_dev;
45 struct adf_hw_device_data *hw_data = accel_dev->hw_device;
46 void __iomem *pmisc_bar_addr =
47 pci_info->pci_bars[hw_data->get_misc_bar_id(hw_data)].virt_addr;
49 ADF_CSR_WR(pmisc_bar_addr, ADF_VINTMSK_OFFSET, 0x2);
51 EXPORT_SYMBOL_GPL(adf_disable_pf2vf_interrupts);
53 static int adf_enable_msi(struct adf_accel_dev *accel_dev)
55 struct adf_accel_pci *pci_dev_info = &accel_dev->accel_pci_dev;
56 int stat = pci_alloc_irq_vectors(pci_dev_info->pci_dev, 1, 1,
58 if (unlikely(stat < 0)) {
59 dev_err(&GET_DEV(accel_dev),
60 "Failed to enable MSI interrupt: %d\n", stat);
67 static void adf_disable_msi(struct adf_accel_dev *accel_dev)
69 struct pci_dev *pdev = accel_to_pci_dev(accel_dev);
71 pci_free_irq_vectors(pdev);
74 static void adf_dev_stop_async(struct work_struct *work)
76 struct adf_vf_stop_data *stop_data =
77 container_of(work, struct adf_vf_stop_data, work);
78 struct adf_accel_dev *accel_dev = stop_data->accel_dev;
80 adf_dev_stop(accel_dev);
81 adf_dev_shutdown(accel_dev);
83 /* Re-enable PF2VF interrupts */
84 adf_enable_pf2vf_interrupts(accel_dev);
88 static void adf_pf2vf_bh_handler(void *data)
90 struct adf_accel_dev *accel_dev = data;
91 struct adf_hw_device_data *hw_data = accel_dev->hw_device;
92 struct adf_bar *pmisc =
93 &GET_BARS(accel_dev)[hw_data->get_misc_bar_id(hw_data)];
94 void __iomem *pmisc_bar_addr = pmisc->virt_addr;
97 /* Read the message from PF */
98 msg = ADF_CSR_RD(pmisc_bar_addr, hw_data->get_pf2vf_offset(0));
99 if (!(msg & ADF_PF2VF_INT)) {
100 dev_info(&GET_DEV(accel_dev),
101 "Spurious PF2VF interrupt, msg %X. Ignored\n", msg);
105 if (!(msg & ADF_PF2VF_MSGORIGIN_SYSTEM))
106 /* Ignore legacy non-system (non-kernel) PF2VF messages */
109 switch ((msg & ADF_PF2VF_MSGTYPE_MASK) >> ADF_PF2VF_MSGTYPE_SHIFT) {
110 case ADF_PF2VF_MSGTYPE_RESTARTING: {
111 struct adf_vf_stop_data *stop_data;
113 dev_dbg(&GET_DEV(accel_dev),
114 "Restarting msg received from PF 0x%x\n", msg);
116 clear_bit(ADF_STATUS_PF_RUNNING, &accel_dev->status);
118 stop_data = kzalloc(sizeof(*stop_data), GFP_ATOMIC);
120 dev_err(&GET_DEV(accel_dev),
121 "Couldn't schedule stop for vf_%d\n",
122 accel_dev->accel_id);
125 stop_data->accel_dev = accel_dev;
126 INIT_WORK(&stop_data->work, adf_dev_stop_async);
127 queue_work(adf_vf_stop_wq, &stop_data->work);
128 /* To ack, clear the PF2VFINT bit */
129 msg &= ~ADF_PF2VF_INT;
130 ADF_CSR_WR(pmisc_bar_addr, hw_data->get_pf2vf_offset(0), msg);
133 case ADF_PF2VF_MSGTYPE_VERSION_RESP:
134 dev_dbg(&GET_DEV(accel_dev),
135 "Version resp received from PF 0x%x\n", msg);
136 accel_dev->vf.pf_version =
137 (msg & ADF_PF2VF_VERSION_RESP_VERS_MASK) >>
138 ADF_PF2VF_VERSION_RESP_VERS_SHIFT;
139 accel_dev->vf.compatible =
140 (msg & ADF_PF2VF_VERSION_RESP_RESULT_MASK) >>
141 ADF_PF2VF_VERSION_RESP_RESULT_SHIFT;
142 complete(&accel_dev->vf.iov_msg_completion);
148 /* To ack, clear the PF2VFINT bit */
149 msg &= ~ADF_PF2VF_INT;
150 ADF_CSR_WR(pmisc_bar_addr, hw_data->get_pf2vf_offset(0), msg);
153 /* Re-enable PF2VF interrupts */
154 adf_enable_pf2vf_interrupts(accel_dev);
157 dev_err(&GET_DEV(accel_dev),
158 "Unknown message from PF (0x%x); leaving PF2VF ints disabled\n",
162 static int adf_setup_pf2vf_bh(struct adf_accel_dev *accel_dev)
164 tasklet_init(&accel_dev->vf.pf2vf_bh_tasklet,
165 (void *)adf_pf2vf_bh_handler, (unsigned long)accel_dev);
167 mutex_init(&accel_dev->vf.vf2pf_lock);
171 static void adf_cleanup_pf2vf_bh(struct adf_accel_dev *accel_dev)
173 tasklet_disable(&accel_dev->vf.pf2vf_bh_tasklet);
174 tasklet_kill(&accel_dev->vf.pf2vf_bh_tasklet);
175 mutex_destroy(&accel_dev->vf.vf2pf_lock);
178 static irqreturn_t adf_isr(int irq, void *privdata)
180 struct adf_accel_dev *accel_dev = privdata;
181 struct adf_hw_device_data *hw_data = accel_dev->hw_device;
182 struct adf_hw_csr_ops *csr_ops = &hw_data->csr_ops;
183 struct adf_bar *pmisc =
184 &GET_BARS(accel_dev)[hw_data->get_misc_bar_id(hw_data)];
185 void __iomem *pmisc_bar_addr = pmisc->virt_addr;
186 bool handled = false;
189 /* Read VF INT source CSR to determine the source of VF interrupt */
190 v_int = ADF_CSR_RD(pmisc_bar_addr, ADF_VINTSOU_OFFSET);
192 /* Read VF INT mask CSR to determine which sources are masked */
193 v_mask = ADF_CSR_RD(pmisc_bar_addr, ADF_VINTMSK_OFFSET);
196 * Recompute v_int ignoring sources that are masked. This is to
197 * avoid rescheduling the tasklet for interrupts already handled
201 /* Check for PF2VF interrupt */
202 if (v_int & ADF_VINTSOU_PF2VF) {
203 /* Disable PF to VF interrupt */
204 adf_disable_pf2vf_interrupts(accel_dev);
206 /* Schedule tasklet to handle interrupt BH */
207 tasklet_hi_schedule(&accel_dev->vf.pf2vf_bh_tasklet);
211 /* Check bundle interrupt */
212 if (v_int & ADF_VINTSOU_BUN) {
213 struct adf_etr_data *etr_data = accel_dev->transport;
214 struct adf_etr_bank_data *bank = &etr_data->banks[0];
216 /* Disable Flag and Coalesce Ring Interrupts */
217 csr_ops->write_csr_int_flag_and_col(bank->csr_addr,
218 bank->bank_number, 0);
219 tasklet_hi_schedule(&bank->resp_handler);
223 return handled ? IRQ_HANDLED : IRQ_NONE;
226 static int adf_request_msi_irq(struct adf_accel_dev *accel_dev)
228 struct pci_dev *pdev = accel_to_pci_dev(accel_dev);
232 snprintf(accel_dev->vf.irq_name, ADF_MAX_MSIX_VECTOR_NAME,
233 "qat_%02x:%02d.%02d", pdev->bus->number, PCI_SLOT(pdev->devfn),
234 PCI_FUNC(pdev->devfn));
235 ret = request_irq(pdev->irq, adf_isr, 0, accel_dev->vf.irq_name,
238 dev_err(&GET_DEV(accel_dev), "failed to enable irq for %s\n",
239 accel_dev->vf.irq_name);
242 cpu = accel_dev->accel_id % num_online_cpus();
243 irq_set_affinity_hint(pdev->irq, get_cpu_mask(cpu));
244 accel_dev->vf.irq_enabled = true;
249 static int adf_setup_bh(struct adf_accel_dev *accel_dev)
251 struct adf_etr_data *priv_data = accel_dev->transport;
253 tasklet_init(&priv_data->banks[0].resp_handler, adf_response_handler,
254 (unsigned long)priv_data->banks);
258 static void adf_cleanup_bh(struct adf_accel_dev *accel_dev)
260 struct adf_etr_data *priv_data = accel_dev->transport;
262 tasklet_disable(&priv_data->banks[0].resp_handler);
263 tasklet_kill(&priv_data->banks[0].resp_handler);
267 * adf_vf_isr_resource_free() - Free IRQ for acceleration device
268 * @accel_dev: Pointer to acceleration device.
270 * Function frees interrupts for acceleration device virtual function.
272 void adf_vf_isr_resource_free(struct adf_accel_dev *accel_dev)
274 struct pci_dev *pdev = accel_to_pci_dev(accel_dev);
276 if (accel_dev->vf.irq_enabled) {
277 irq_set_affinity_hint(pdev->irq, NULL);
278 free_irq(pdev->irq, accel_dev);
280 adf_cleanup_bh(accel_dev);
281 adf_cleanup_pf2vf_bh(accel_dev);
282 adf_disable_msi(accel_dev);
284 EXPORT_SYMBOL_GPL(adf_vf_isr_resource_free);
287 * adf_vf_isr_resource_alloc() - Allocate IRQ for acceleration device
288 * @accel_dev: Pointer to acceleration device.
290 * Function allocates interrupts for acceleration device virtual function.
292 * Return: 0 on success, error code otherwise.
294 int adf_vf_isr_resource_alloc(struct adf_accel_dev *accel_dev)
296 if (adf_enable_msi(accel_dev))
299 if (adf_setup_pf2vf_bh(accel_dev))
300 goto err_disable_msi;
302 if (adf_setup_bh(accel_dev))
303 goto err_cleanup_pf2vf_bh;
305 if (adf_request_msi_irq(accel_dev))
311 adf_cleanup_bh(accel_dev);
313 err_cleanup_pf2vf_bh:
314 adf_cleanup_pf2vf_bh(accel_dev);
317 adf_disable_msi(accel_dev);
322 EXPORT_SYMBOL_GPL(adf_vf_isr_resource_alloc);
325 * adf_flush_vf_wq() - Flush workqueue for VF
326 * @accel_dev: Pointer to acceleration device.
328 * Function disables the PF/VF interrupts on the VF so that no new messages
329 * are received and flushes the workqueue 'adf_vf_stop_wq'.
333 void adf_flush_vf_wq(struct adf_accel_dev *accel_dev)
335 adf_disable_pf2vf_interrupts(accel_dev);
337 flush_workqueue(adf_vf_stop_wq);
339 EXPORT_SYMBOL_GPL(adf_flush_vf_wq);
342 * adf_init_vf_wq() - Init workqueue for VF
344 * Function init workqueue 'adf_vf_stop_wq' for VF.
346 * Return: 0 on success, error code otherwise.
348 int __init adf_init_vf_wq(void)
350 adf_vf_stop_wq = alloc_workqueue("adf_vf_stop_wq", WQ_MEM_RECLAIM, 0);
352 return !adf_vf_stop_wq ? -EFAULT : 0;
355 void adf_exit_vf_wq(void)
358 destroy_workqueue(adf_vf_stop_wq);
360 adf_vf_stop_wq = NULL;