Merge tag 'renesas-dt-fixes2-for-v4.5' of git://git.kernel.org/pub/scm/linux/kernel...
[deliverable/linux.git] / drivers / crypto / qat / qat_common / adf_vf_isr.c
1 /*
2 This file is provided under a dual BSD/GPLv2 license. When using or
3 redistributing this file, you may do so under either license.
4
5 GPL LICENSE SUMMARY
6 Copyright(c) 2014 Intel Corporation.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of version 2 of the GNU General Public License as
9 published by the Free Software Foundation.
10
11 This program is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 Contact Information:
17 qat-linux@intel.com
18
19 BSD LICENSE
20 Copyright(c) 2014 Intel Corporation.
21 Redistribution and use in source and binary forms, with or without
22 modification, are permitted provided that the following conditions
23 are met:
24
25 * Redistributions of source code must retain the above copyright
26 notice, this list of conditions and the following disclaimer.
27 * Redistributions in binary form must reproduce the above copyright
28 notice, this list of conditions and the following disclaimer in
29 the documentation and/or other materials provided with the
30 distribution.
31 * Neither the name of Intel Corporation nor the names of its
32 contributors may be used to endorse or promote products derived
33 from this software without specific prior written permission.
34
35 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
36 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
38 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
39 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
43 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
45 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
46 */
47 #include <linux/kernel.h>
48 #include <linux/init.h>
49 #include <linux/types.h>
50 #include <linux/pci.h>
51 #include <linux/slab.h>
52 #include <linux/errno.h>
53 #include <linux/interrupt.h>
54 #include "adf_accel_devices.h"
55 #include "adf_common_drv.h"
56 #include "adf_cfg.h"
57 #include "adf_cfg_strings.h"
58 #include "adf_cfg_common.h"
59 #include "adf_transport_access_macros.h"
60 #include "adf_transport_internal.h"
61 #include "adf_pf2vf_msg.h"
62
63 #define ADF_VINTSOU_OFFSET 0x204
64 #define ADF_VINTSOU_BUN BIT(0)
65 #define ADF_VINTSOU_PF2VF BIT(1)
66
67 static int adf_enable_msi(struct adf_accel_dev *accel_dev)
68 {
69 struct adf_accel_pci *pci_dev_info = &accel_dev->accel_pci_dev;
70 int stat = pci_enable_msi(pci_dev_info->pci_dev);
71
72 if (stat) {
73 dev_err(&GET_DEV(accel_dev),
74 "Failed to enable MSI interrupts\n");
75 return stat;
76 }
77
78 accel_dev->vf.irq_name = kzalloc(ADF_MAX_MSIX_VECTOR_NAME, GFP_KERNEL);
79 if (!accel_dev->vf.irq_name)
80 return -ENOMEM;
81
82 return stat;
83 }
84
85 static void adf_disable_msi(struct adf_accel_dev *accel_dev)
86 {
87 struct pci_dev *pdev = accel_to_pci_dev(accel_dev);
88
89 kfree(accel_dev->vf.irq_name);
90 pci_disable_msi(pdev);
91 }
92
93 static void adf_pf2vf_bh_handler(void *data)
94 {
95 struct adf_accel_dev *accel_dev = data;
96 struct adf_hw_device_data *hw_data = accel_dev->hw_device;
97 struct adf_bar *pmisc =
98 &GET_BARS(accel_dev)[hw_data->get_misc_bar_id(hw_data)];
99 void __iomem *pmisc_bar_addr = pmisc->virt_addr;
100 u32 msg;
101
102 /* Read the message from PF */
103 msg = ADF_CSR_RD(pmisc_bar_addr, hw_data->get_pf2vf_offset(0));
104
105 if (!(msg & ADF_PF2VF_MSGORIGIN_SYSTEM))
106 /* Ignore legacy non-system (non-kernel) PF2VF messages */
107 goto err;
108
109 switch ((msg & ADF_PF2VF_MSGTYPE_MASK) >> ADF_PF2VF_MSGTYPE_SHIFT) {
110 case ADF_PF2VF_MSGTYPE_RESTARTING:
111 dev_dbg(&GET_DEV(accel_dev),
112 "Restarting msg received from PF 0x%x\n", msg);
113 adf_dev_stop(accel_dev);
114 break;
115 case ADF_PF2VF_MSGTYPE_VERSION_RESP:
116 dev_dbg(&GET_DEV(accel_dev),
117 "Version resp received from PF 0x%x\n", msg);
118 accel_dev->vf.pf_version =
119 (msg & ADF_PF2VF_VERSION_RESP_VERS_MASK) >>
120 ADF_PF2VF_VERSION_RESP_VERS_SHIFT;
121 accel_dev->vf.compatible =
122 (msg & ADF_PF2VF_VERSION_RESP_RESULT_MASK) >>
123 ADF_PF2VF_VERSION_RESP_RESULT_SHIFT;
124 complete(&accel_dev->vf.iov_msg_completion);
125 break;
126 default:
127 goto err;
128 }
129
130 /* To ack, clear the PF2VFINT bit */
131 msg &= ~BIT(0);
132 ADF_CSR_WR(pmisc_bar_addr, hw_data->get_pf2vf_offset(0), msg);
133
134 /* Re-enable PF2VF interrupts */
135 adf_enable_pf2vf_interrupts(accel_dev);
136 return;
137 err:
138 dev_err(&GET_DEV(accel_dev),
139 "Unknown message from PF (0x%x); leaving PF2VF ints disabled\n",
140 msg);
141 }
142
143 static int adf_setup_pf2vf_bh(struct adf_accel_dev *accel_dev)
144 {
145 tasklet_init(&accel_dev->vf.pf2vf_bh_tasklet,
146 (void *)adf_pf2vf_bh_handler, (unsigned long)accel_dev);
147
148 mutex_init(&accel_dev->vf.vf2pf_lock);
149 return 0;
150 }
151
152 static void adf_cleanup_pf2vf_bh(struct adf_accel_dev *accel_dev)
153 {
154 tasklet_disable(&accel_dev->vf.pf2vf_bh_tasklet);
155 tasklet_kill(&accel_dev->vf.pf2vf_bh_tasklet);
156 mutex_destroy(&accel_dev->vf.vf2pf_lock);
157 }
158
159 static irqreturn_t adf_isr(int irq, void *privdata)
160 {
161 struct adf_accel_dev *accel_dev = privdata;
162 struct adf_hw_device_data *hw_data = accel_dev->hw_device;
163 struct adf_bar *pmisc =
164 &GET_BARS(accel_dev)[hw_data->get_misc_bar_id(hw_data)];
165 void __iomem *pmisc_bar_addr = pmisc->virt_addr;
166 u32 v_int;
167
168 /* Read VF INT source CSR to determine the source of VF interrupt */
169 v_int = ADF_CSR_RD(pmisc_bar_addr, ADF_VINTSOU_OFFSET);
170
171 /* Check for PF2VF interrupt */
172 if (v_int & ADF_VINTSOU_PF2VF) {
173 /* Disable PF to VF interrupt */
174 adf_disable_pf2vf_interrupts(accel_dev);
175
176 /* Schedule tasklet to handle interrupt BH */
177 tasklet_hi_schedule(&accel_dev->vf.pf2vf_bh_tasklet);
178 return IRQ_HANDLED;
179 }
180
181 /* Check bundle interrupt */
182 if (v_int & ADF_VINTSOU_BUN) {
183 struct adf_etr_data *etr_data = accel_dev->transport;
184 struct adf_etr_bank_data *bank = &etr_data->banks[0];
185
186 /* Disable Flag and Coalesce Ring Interrupts */
187 WRITE_CSR_INT_FLAG_AND_COL(bank->csr_addr, bank->bank_number,
188 0);
189 tasklet_hi_schedule(&bank->resp_handler);
190 return IRQ_HANDLED;
191 }
192
193 return IRQ_NONE;
194 }
195
196 static int adf_request_msi_irq(struct adf_accel_dev *accel_dev)
197 {
198 struct pci_dev *pdev = accel_to_pci_dev(accel_dev);
199 unsigned int cpu;
200 int ret;
201
202 snprintf(accel_dev->vf.irq_name, ADF_MAX_MSIX_VECTOR_NAME,
203 "qat_%02x:%02d.%02d", pdev->bus->number, PCI_SLOT(pdev->devfn),
204 PCI_FUNC(pdev->devfn));
205 ret = request_irq(pdev->irq, adf_isr, 0, accel_dev->vf.irq_name,
206 (void *)accel_dev);
207 if (ret) {
208 dev_err(&GET_DEV(accel_dev), "failed to enable irq for %s\n",
209 accel_dev->vf.irq_name);
210 return ret;
211 }
212 cpu = accel_dev->accel_id % num_online_cpus();
213 irq_set_affinity_hint(pdev->irq, get_cpu_mask(cpu));
214
215 return ret;
216 }
217
218 static int adf_setup_bh(struct adf_accel_dev *accel_dev)
219 {
220 struct adf_etr_data *priv_data = accel_dev->transport;
221
222 tasklet_init(&priv_data->banks[0].resp_handler, adf_response_handler,
223 (unsigned long)priv_data->banks);
224 return 0;
225 }
226
227 static void adf_cleanup_bh(struct adf_accel_dev *accel_dev)
228 {
229 struct adf_etr_data *priv_data = accel_dev->transport;
230
231 tasklet_disable(&priv_data->banks[0].resp_handler);
232 tasklet_kill(&priv_data->banks[0].resp_handler);
233 }
234
235 /**
236 * adf_vf_isr_resource_free() - Free IRQ for acceleration device
237 * @accel_dev: Pointer to acceleration device.
238 *
239 * Function frees interrupts for acceleration device virtual function.
240 */
241 void adf_vf_isr_resource_free(struct adf_accel_dev *accel_dev)
242 {
243 struct pci_dev *pdev = accel_to_pci_dev(accel_dev);
244
245 irq_set_affinity_hint(pdev->irq, NULL);
246 free_irq(pdev->irq, (void *)accel_dev);
247 adf_cleanup_bh(accel_dev);
248 adf_cleanup_pf2vf_bh(accel_dev);
249 adf_disable_msi(accel_dev);
250 }
251 EXPORT_SYMBOL_GPL(adf_vf_isr_resource_free);
252
253 /**
254 * adf_vf_isr_resource_alloc() - Allocate IRQ for acceleration device
255 * @accel_dev: Pointer to acceleration device.
256 *
257 * Function allocates interrupts for acceleration device virtual function.
258 *
259 * Return: 0 on success, error code otherwise.
260 */
261 int adf_vf_isr_resource_alloc(struct adf_accel_dev *accel_dev)
262 {
263 if (adf_enable_msi(accel_dev))
264 goto err_out;
265
266 if (adf_setup_pf2vf_bh(accel_dev))
267 goto err_out;
268
269 if (adf_setup_bh(accel_dev))
270 goto err_out;
271
272 if (adf_request_msi_irq(accel_dev))
273 goto err_out;
274
275 return 0;
276 err_out:
277 adf_vf_isr_resource_free(accel_dev);
278 return -EFAULT;
279 }
280 EXPORT_SYMBOL_GPL(adf_vf_isr_resource_alloc);
This page took 0.039462 seconds and 6 git commands to generate.