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_VINTSOU_BUN BIT(0)
22 #define ADF_VINTSOU_PF2VF BIT(1)
24 static struct workqueue_struct *adf_vf_stop_wq;
26 struct adf_vf_stop_data {
27 struct adf_accel_dev *accel_dev;
28 struct work_struct work;
31 static int adf_enable_msi(struct adf_accel_dev *accel_dev)
33 struct adf_accel_pci *pci_dev_info = &accel_dev->accel_pci_dev;
34 int stat = pci_enable_msi(pci_dev_info->pci_dev);
37 dev_err(&GET_DEV(accel_dev),
38 "Failed to enable MSI interrupts\n");
42 accel_dev->vf.irq_name = kzalloc(ADF_MAX_MSIX_VECTOR_NAME, GFP_KERNEL);
43 if (!accel_dev->vf.irq_name)
49 static void adf_disable_msi(struct adf_accel_dev *accel_dev)
51 struct pci_dev *pdev = accel_to_pci_dev(accel_dev);
53 kfree(accel_dev->vf.irq_name);
54 pci_disable_msi(pdev);
57 static void adf_dev_stop_async(struct work_struct *work)
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;
63 adf_dev_stop(accel_dev);
64 adf_dev_shutdown(accel_dev);
66 /* Re-enable PF2VF interrupts */
67 adf_enable_pf2vf_interrupts(accel_dev);
71 static void adf_pf2vf_bh_handler(void *data)
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;
80 /* Read the message from PF */
81 msg = ADF_CSR_RD(pmisc_bar_addr, hw_data->get_pf2vf_offset(0));
82 if (!(msg & ADF_PF2VF_INT)) {
83 dev_info(&GET_DEV(accel_dev),
84 "Spurious PF2VF interrupt, msg %X. Ignored\n", msg);
88 if (!(msg & ADF_PF2VF_MSGORIGIN_SYSTEM))
89 /* Ignore legacy non-system (non-kernel) PF2VF messages */
92 switch ((msg & ADF_PF2VF_MSGTYPE_MASK) >> ADF_PF2VF_MSGTYPE_SHIFT) {
93 case ADF_PF2VF_MSGTYPE_RESTARTING: {
94 struct adf_vf_stop_data *stop_data;
96 dev_dbg(&GET_DEV(accel_dev),
97 "Restarting msg received from PF 0x%x\n", msg);
99 clear_bit(ADF_STATUS_PF_RUNNING, &accel_dev->status);
101 stop_data = kzalloc(sizeof(*stop_data), GFP_ATOMIC);
103 dev_err(&GET_DEV(accel_dev),
104 "Couldn't schedule stop for vf_%d\n",
105 accel_dev->accel_id);
108 stop_data->accel_dev = accel_dev;
109 INIT_WORK(&stop_data->work, adf_dev_stop_async);
110 queue_work(adf_vf_stop_wq, &stop_data->work);
111 /* To ack, clear the PF2VFINT bit */
112 msg &= ~ADF_PF2VF_INT;
113 ADF_CSR_WR(pmisc_bar_addr, hw_data->get_pf2vf_offset(0), msg);
116 case ADF_PF2VF_MSGTYPE_VERSION_RESP:
117 dev_dbg(&GET_DEV(accel_dev),
118 "Version resp received from PF 0x%x\n", msg);
119 accel_dev->vf.pf_version =
120 (msg & ADF_PF2VF_VERSION_RESP_VERS_MASK) >>
121 ADF_PF2VF_VERSION_RESP_VERS_SHIFT;
122 accel_dev->vf.compatible =
123 (msg & ADF_PF2VF_VERSION_RESP_RESULT_MASK) >>
124 ADF_PF2VF_VERSION_RESP_RESULT_SHIFT;
125 complete(&accel_dev->vf.iov_msg_completion);
131 /* To ack, clear the PF2VFINT bit */
132 msg &= ~ADF_PF2VF_INT;
133 ADF_CSR_WR(pmisc_bar_addr, hw_data->get_pf2vf_offset(0), msg);
136 /* Re-enable PF2VF interrupts */
137 adf_enable_pf2vf_interrupts(accel_dev);
140 dev_err(&GET_DEV(accel_dev),
141 "Unknown message from PF (0x%x); leaving PF2VF ints disabled\n",
145 static int adf_setup_pf2vf_bh(struct adf_accel_dev *accel_dev)
147 tasklet_init(&accel_dev->vf.pf2vf_bh_tasklet,
148 (void *)adf_pf2vf_bh_handler, (unsigned long)accel_dev);
150 mutex_init(&accel_dev->vf.vf2pf_lock);
154 static void adf_cleanup_pf2vf_bh(struct adf_accel_dev *accel_dev)
156 tasklet_disable(&accel_dev->vf.pf2vf_bh_tasklet);
157 tasklet_kill(&accel_dev->vf.pf2vf_bh_tasklet);
158 mutex_destroy(&accel_dev->vf.vf2pf_lock);
161 static irqreturn_t adf_isr(int irq, void *privdata)
163 struct adf_accel_dev *accel_dev = privdata;
164 struct adf_hw_device_data *hw_data = accel_dev->hw_device;
165 struct adf_bar *pmisc =
166 &GET_BARS(accel_dev)[hw_data->get_misc_bar_id(hw_data)];
167 void __iomem *pmisc_bar_addr = pmisc->virt_addr;
168 bool handled = false;
171 /* Read VF INT source CSR to determine the source of VF interrupt */
172 v_int = ADF_CSR_RD(pmisc_bar_addr, ADF_VINTSOU_OFFSET);
174 /* Check for PF2VF interrupt */
175 if (v_int & ADF_VINTSOU_PF2VF) {
176 /* Disable PF to VF interrupt */
177 adf_disable_pf2vf_interrupts(accel_dev);
179 /* Schedule tasklet to handle interrupt BH */
180 tasklet_hi_schedule(&accel_dev->vf.pf2vf_bh_tasklet);
184 /* Check bundle interrupt */
185 if (v_int & ADF_VINTSOU_BUN) {
186 struct adf_etr_data *etr_data = accel_dev->transport;
187 struct adf_etr_bank_data *bank = &etr_data->banks[0];
189 /* Disable Flag and Coalesce Ring Interrupts */
190 WRITE_CSR_INT_FLAG_AND_COL(bank->csr_addr, bank->bank_number,
192 tasklet_hi_schedule(&bank->resp_handler);
196 return handled ? IRQ_HANDLED : IRQ_NONE;
199 static int adf_request_msi_irq(struct adf_accel_dev *accel_dev)
201 struct pci_dev *pdev = accel_to_pci_dev(accel_dev);
205 snprintf(accel_dev->vf.irq_name, ADF_MAX_MSIX_VECTOR_NAME,
206 "qat_%02x:%02d.%02d", pdev->bus->number, PCI_SLOT(pdev->devfn),
207 PCI_FUNC(pdev->devfn));
208 ret = request_irq(pdev->irq, adf_isr, 0, accel_dev->vf.irq_name,
211 dev_err(&GET_DEV(accel_dev), "failed to enable irq for %s\n",
212 accel_dev->vf.irq_name);
215 cpu = accel_dev->accel_id % num_online_cpus();
216 irq_set_affinity_hint(pdev->irq, get_cpu_mask(cpu));
221 static int adf_setup_bh(struct adf_accel_dev *accel_dev)
223 struct adf_etr_data *priv_data = accel_dev->transport;
225 tasklet_init(&priv_data->banks[0].resp_handler, adf_response_handler,
226 (unsigned long)priv_data->banks);
230 static void adf_cleanup_bh(struct adf_accel_dev *accel_dev)
232 struct adf_etr_data *priv_data = accel_dev->transport;
234 tasklet_disable(&priv_data->banks[0].resp_handler);
235 tasklet_kill(&priv_data->banks[0].resp_handler);
239 * adf_vf_isr_resource_free() - Free IRQ for acceleration device
240 * @accel_dev: Pointer to acceleration device.
242 * Function frees interrupts for acceleration device virtual function.
244 void adf_vf_isr_resource_free(struct adf_accel_dev *accel_dev)
246 struct pci_dev *pdev = accel_to_pci_dev(accel_dev);
248 irq_set_affinity_hint(pdev->irq, NULL);
249 free_irq(pdev->irq, (void *)accel_dev);
250 adf_cleanup_bh(accel_dev);
251 adf_cleanup_pf2vf_bh(accel_dev);
252 adf_disable_msi(accel_dev);
254 EXPORT_SYMBOL_GPL(adf_vf_isr_resource_free);
257 * adf_vf_isr_resource_alloc() - Allocate IRQ for acceleration device
258 * @accel_dev: Pointer to acceleration device.
260 * Function allocates interrupts for acceleration device virtual function.
262 * Return: 0 on success, error code otherwise.
264 int adf_vf_isr_resource_alloc(struct adf_accel_dev *accel_dev)
266 if (adf_enable_msi(accel_dev))
269 if (adf_setup_pf2vf_bh(accel_dev))
270 goto err_disable_msi;
272 if (adf_setup_bh(accel_dev))
273 goto err_cleanup_pf2vf_bh;
275 if (adf_request_msi_irq(accel_dev))
281 adf_cleanup_bh(accel_dev);
283 err_cleanup_pf2vf_bh:
284 adf_cleanup_pf2vf_bh(accel_dev);
287 adf_disable_msi(accel_dev);
292 EXPORT_SYMBOL_GPL(adf_vf_isr_resource_alloc);
294 int __init adf_init_vf_wq(void)
296 adf_vf_stop_wq = alloc_workqueue("adf_vf_stop_wq", WQ_MEM_RECLAIM, 0);
298 return !adf_vf_stop_wq ? -EFAULT : 0;
301 void adf_exit_vf_wq(void)
304 destroy_workqueue(adf_vf_stop_wq);
306 adf_vf_stop_wq = NULL;