1 // SPDX-License-Identifier: (BSD-3-Clause OR GPL-2.0-only)
2 /* Copyright(c) 2015 - 2020 Intel Corporation */
3 #include <linux/delay.h>
4 #include "adf_accel_devices.h"
5 #include "adf_common_drv.h"
6 #include "adf_pf2vf_msg.h"
8 #define ADF_DH895XCC_EP_OFFSET 0x3A000
9 #define ADF_DH895XCC_ERRMSK3 (ADF_DH895XCC_EP_OFFSET + 0x1C)
10 #define ADF_DH895XCC_ERRMSK3_VF2PF_L_MASK(vf_mask) ((vf_mask & 0xFFFF) << 9)
11 #define ADF_DH895XCC_ERRMSK5 (ADF_DH895XCC_EP_OFFSET + 0xDC)
12 #define ADF_DH895XCC_ERRMSK5_VF2PF_U_MASK(vf_mask) (vf_mask >> 16)
14 void adf_enable_pf2vf_interrupts(struct adf_accel_dev *accel_dev)
16 struct adf_accel_pci *pci_info = &accel_dev->accel_pci_dev;
17 struct adf_hw_device_data *hw_data = accel_dev->hw_device;
18 void __iomem *pmisc_bar_addr =
19 pci_info->pci_bars[hw_data->get_misc_bar_id(hw_data)].virt_addr;
21 ADF_CSR_WR(pmisc_bar_addr, hw_data->get_vintmsk_offset(0), 0x0);
24 void adf_disable_pf2vf_interrupts(struct adf_accel_dev *accel_dev)
26 struct adf_accel_pci *pci_info = &accel_dev->accel_pci_dev;
27 struct adf_hw_device_data *hw_data = accel_dev->hw_device;
28 void __iomem *pmisc_bar_addr =
29 pci_info->pci_bars[hw_data->get_misc_bar_id(hw_data)].virt_addr;
31 ADF_CSR_WR(pmisc_bar_addr, hw_data->get_vintmsk_offset(0), 0x2);
34 void adf_enable_vf2pf_interrupts(struct adf_accel_dev *accel_dev,
37 struct adf_hw_device_data *hw_data = accel_dev->hw_device;
38 struct adf_bar *pmisc =
39 &GET_BARS(accel_dev)[hw_data->get_misc_bar_id(hw_data)];
40 void __iomem *pmisc_addr = pmisc->virt_addr;
43 /* Enable VF2PF Messaging Ints - VFs 1 through 16 per vf_mask[15:0] */
44 if (vf_mask & 0xFFFF) {
45 reg = ADF_CSR_RD(pmisc_addr, ADF_DH895XCC_ERRMSK3);
46 reg &= ~ADF_DH895XCC_ERRMSK3_VF2PF_L_MASK(vf_mask);
47 ADF_CSR_WR(pmisc_addr, ADF_DH895XCC_ERRMSK3, reg);
50 /* Enable VF2PF Messaging Ints - VFs 17 through 32 per vf_mask[31:16] */
52 reg = ADF_CSR_RD(pmisc_addr, ADF_DH895XCC_ERRMSK5);
53 reg &= ~ADF_DH895XCC_ERRMSK5_VF2PF_U_MASK(vf_mask);
54 ADF_CSR_WR(pmisc_addr, ADF_DH895XCC_ERRMSK5, reg);
58 void adf_disable_vf2pf_interrupts(struct adf_accel_dev *accel_dev, u32 vf_mask)
60 struct adf_hw_device_data *hw_data = accel_dev->hw_device;
61 struct adf_bar *pmisc =
62 &GET_BARS(accel_dev)[hw_data->get_misc_bar_id(hw_data)];
63 void __iomem *pmisc_addr = pmisc->virt_addr;
66 /* Disable VF2PF interrupts for VFs 1 through 16 per vf_mask[15:0] */
67 if (vf_mask & 0xFFFF) {
68 reg = ADF_CSR_RD(pmisc_addr, ADF_DH895XCC_ERRMSK3) |
69 ADF_DH895XCC_ERRMSK3_VF2PF_L_MASK(vf_mask);
70 ADF_CSR_WR(pmisc_addr, ADF_DH895XCC_ERRMSK3, reg);
73 /* Disable VF2PF interrupts for VFs 17 through 32 per vf_mask[31:16] */
75 reg = ADF_CSR_RD(pmisc_addr, ADF_DH895XCC_ERRMSK5) |
76 ADF_DH895XCC_ERRMSK5_VF2PF_U_MASK(vf_mask);
77 ADF_CSR_WR(pmisc_addr, ADF_DH895XCC_ERRMSK5, reg);
81 static int __adf_iov_putmsg(struct adf_accel_dev *accel_dev, u32 msg, u8 vf_nr)
83 struct adf_accel_pci *pci_info = &accel_dev->accel_pci_dev;
84 struct adf_hw_device_data *hw_data = accel_dev->hw_device;
85 void __iomem *pmisc_bar_addr =
86 pci_info->pci_bars[hw_data->get_misc_bar_id(hw_data)].virt_addr;
87 u32 val, pf2vf_offset, count = 0;
88 u32 local_in_use_mask, local_in_use_pattern;
89 u32 remote_in_use_mask, remote_in_use_pattern;
90 struct mutex *lock; /* lock preventing concurrent acces of CSR */
94 if (accel_dev->is_vf) {
95 pf2vf_offset = hw_data->get_pf2vf_offset(0);
96 lock = &accel_dev->vf.vf2pf_lock;
97 local_in_use_mask = ADF_VF2PF_IN_USE_BY_VF_MASK;
98 local_in_use_pattern = ADF_VF2PF_IN_USE_BY_VF;
99 remote_in_use_mask = ADF_PF2VF_IN_USE_BY_PF_MASK;
100 remote_in_use_pattern = ADF_PF2VF_IN_USE_BY_PF;
101 int_bit = ADF_VF2PF_INT;
103 pf2vf_offset = hw_data->get_pf2vf_offset(vf_nr);
104 lock = &accel_dev->pf.vf_info[vf_nr].pf2vf_lock;
105 local_in_use_mask = ADF_PF2VF_IN_USE_BY_PF_MASK;
106 local_in_use_pattern = ADF_PF2VF_IN_USE_BY_PF;
107 remote_in_use_mask = ADF_VF2PF_IN_USE_BY_VF_MASK;
108 remote_in_use_pattern = ADF_VF2PF_IN_USE_BY_VF;
109 int_bit = ADF_PF2VF_INT;
114 /* Check if the PFVF CSR is in use by remote function */
115 val = ADF_CSR_RD(pmisc_bar_addr, pf2vf_offset);
116 if ((val & remote_in_use_mask) == remote_in_use_pattern) {
117 dev_dbg(&GET_DEV(accel_dev),
118 "PFVF CSR in use by remote function\n");
123 msg &= ~local_in_use_mask;
124 msg |= local_in_use_pattern;
126 /* Attempt to get ownership of the PFVF CSR */
127 ADF_CSR_WR(pmisc_bar_addr, pf2vf_offset, msg | int_bit);
129 /* Wait for confirmation from remote func it received the message */
131 msleep(ADF_IOV_MSG_ACK_DELAY);
132 val = ADF_CSR_RD(pmisc_bar_addr, pf2vf_offset);
133 } while ((val & int_bit) && (count++ < ADF_IOV_MSG_ACK_MAX_RETRY));
136 dev_dbg(&GET_DEV(accel_dev), "ACK not received from remote\n");
142 dev_dbg(&GET_DEV(accel_dev),
143 "Collision - PFVF CSR overwritten by remote function\n");
148 /* Finished with the PFVF CSR; relinquish it and leave msg in CSR */
149 ADF_CSR_WR(pmisc_bar_addr, pf2vf_offset, val & ~local_in_use_mask);
156 * adf_iov_putmsg() - send PFVF message
157 * @accel_dev: Pointer to acceleration device.
158 * @msg: Message to send
159 * @vf_nr: VF number to which the message will be sent if on PF, ignored
162 * Function sends a message through the PFVF channel
164 * Return: 0 on success, error code otherwise.
166 int adf_iov_putmsg(struct adf_accel_dev *accel_dev, u32 msg, u8 vf_nr)
172 ret = __adf_iov_putmsg(accel_dev, msg, vf_nr);
174 msleep(ADF_IOV_MSG_RETRY_DELAY);
175 } while (ret && (count++ < ADF_IOV_MSG_MAX_RETRIES));
180 void adf_vf2pf_req_hndl(struct adf_accel_vf_info *vf_info)
182 struct adf_accel_dev *accel_dev = vf_info->accel_dev;
183 struct adf_hw_device_data *hw_data = accel_dev->hw_device;
184 int bar_id = hw_data->get_misc_bar_id(hw_data);
185 struct adf_bar *pmisc = &GET_BARS(accel_dev)[bar_id];
186 void __iomem *pmisc_addr = pmisc->virt_addr;
187 u32 msg, resp = 0, vf_nr = vf_info->vf_nr;
189 /* Read message from the VF */
190 msg = ADF_CSR_RD(pmisc_addr, hw_data->get_pf2vf_offset(vf_nr));
191 if (!(msg & ADF_VF2PF_INT)) {
192 dev_info(&GET_DEV(accel_dev),
193 "Spurious VF2PF interrupt, msg %X. Ignored\n", msg);
197 /* To ACK, clear the VF2PFINT bit */
198 msg &= ~ADF_VF2PF_INT;
199 ADF_CSR_WR(pmisc_addr, hw_data->get_pf2vf_offset(vf_nr), msg);
201 if (!(msg & ADF_VF2PF_MSGORIGIN_SYSTEM))
202 /* Ignore legacy non-system (non-kernel) VF2PF messages */
205 switch ((msg & ADF_VF2PF_MSGTYPE_MASK) >> ADF_VF2PF_MSGTYPE_SHIFT) {
206 case ADF_VF2PF_MSGTYPE_COMPAT_VER_REQ:
208 u8 vf_compat_ver = msg >> ADF_VF2PF_COMPAT_VER_REQ_SHIFT;
210 resp = (ADF_PF2VF_MSGORIGIN_SYSTEM |
211 (ADF_PF2VF_MSGTYPE_VERSION_RESP <<
212 ADF_PF2VF_MSGTYPE_SHIFT) |
213 (ADF_PFVF_COMPATIBILITY_VERSION <<
214 ADF_PF2VF_VERSION_RESP_VERS_SHIFT));
216 dev_dbg(&GET_DEV(accel_dev),
217 "Compatibility Version Request from VF%d vers=%u\n",
218 vf_nr + 1, vf_compat_ver);
220 if (vf_compat_ver < hw_data->min_iov_compat_ver) {
221 dev_err(&GET_DEV(accel_dev),
222 "VF (vers %d) incompatible with PF (vers %d)\n",
223 vf_compat_ver, ADF_PFVF_COMPATIBILITY_VERSION);
224 resp |= ADF_PF2VF_VF_INCOMPATIBLE <<
225 ADF_PF2VF_VERSION_RESP_RESULT_SHIFT;
226 } else if (vf_compat_ver > ADF_PFVF_COMPATIBILITY_VERSION) {
227 dev_err(&GET_DEV(accel_dev),
228 "VF (vers %d) compat with PF (vers %d) unkn.\n",
229 vf_compat_ver, ADF_PFVF_COMPATIBILITY_VERSION);
230 resp |= ADF_PF2VF_VF_COMPAT_UNKNOWN <<
231 ADF_PF2VF_VERSION_RESP_RESULT_SHIFT;
233 dev_dbg(&GET_DEV(accel_dev),
234 "VF (vers %d) compatible with PF (vers %d)\n",
235 vf_compat_ver, ADF_PFVF_COMPATIBILITY_VERSION);
236 resp |= ADF_PF2VF_VF_COMPATIBLE <<
237 ADF_PF2VF_VERSION_RESP_RESULT_SHIFT;
241 case ADF_VF2PF_MSGTYPE_VERSION_REQ:
242 dev_dbg(&GET_DEV(accel_dev),
243 "Legacy VersionRequest received from VF%d 0x%x\n",
245 resp = (ADF_PF2VF_MSGORIGIN_SYSTEM |
246 (ADF_PF2VF_MSGTYPE_VERSION_RESP <<
247 ADF_PF2VF_MSGTYPE_SHIFT) |
248 (ADF_PFVF_COMPATIBILITY_VERSION <<
249 ADF_PF2VF_VERSION_RESP_VERS_SHIFT));
250 resp |= ADF_PF2VF_VF_COMPATIBLE <<
251 ADF_PF2VF_VERSION_RESP_RESULT_SHIFT;
252 /* Set legacy major and minor version num */
253 resp |= 1 << ADF_PF2VF_MAJORVERSION_SHIFT |
254 1 << ADF_PF2VF_MINORVERSION_SHIFT;
256 case ADF_VF2PF_MSGTYPE_INIT:
258 dev_dbg(&GET_DEV(accel_dev),
259 "Init message received from VF%d 0x%x\n",
261 vf_info->init = true;
264 case ADF_VF2PF_MSGTYPE_SHUTDOWN:
266 dev_dbg(&GET_DEV(accel_dev),
267 "Shutdown message received from VF%d 0x%x\n",
269 vf_info->init = false;
276 if (resp && adf_iov_putmsg(accel_dev, resp, vf_nr))
277 dev_err(&GET_DEV(accel_dev), "Failed to send response to VF\n");
280 /* re-enable interrupt on PF from this VF */
281 adf_enable_vf2pf_interrupts(accel_dev, (1 << vf_nr));
284 dev_dbg(&GET_DEV(accel_dev), "Unknown message from VF%d (0x%x);\n",
288 void adf_pf2vf_notify_restarting(struct adf_accel_dev *accel_dev)
290 struct adf_accel_vf_info *vf;
291 u32 msg = (ADF_PF2VF_MSGORIGIN_SYSTEM |
292 (ADF_PF2VF_MSGTYPE_RESTARTING << ADF_PF2VF_MSGTYPE_SHIFT));
293 int i, num_vfs = pci_num_vf(accel_to_pci_dev(accel_dev));
295 for (i = 0, vf = accel_dev->pf.vf_info; i < num_vfs; i++, vf++) {
296 if (vf->init && adf_iov_putmsg(accel_dev, msg, i))
297 dev_err(&GET_DEV(accel_dev),
298 "Failed to send restarting msg to VF%d\n", i);
302 static int adf_vf2pf_request_version(struct adf_accel_dev *accel_dev)
304 unsigned long timeout = msecs_to_jiffies(ADF_IOV_MSG_RESP_TIMEOUT);
305 struct adf_hw_device_data *hw_data = accel_dev->hw_device;
309 msg = ADF_VF2PF_MSGORIGIN_SYSTEM;
310 msg |= ADF_VF2PF_MSGTYPE_COMPAT_VER_REQ << ADF_VF2PF_MSGTYPE_SHIFT;
311 msg |= ADF_PFVF_COMPATIBILITY_VERSION << ADF_VF2PF_COMPAT_VER_REQ_SHIFT;
312 BUILD_BUG_ON(ADF_PFVF_COMPATIBILITY_VERSION > 255);
314 reinit_completion(&accel_dev->vf.iov_msg_completion);
316 /* Send request from VF to PF */
317 ret = adf_iov_putmsg(accel_dev, msg, 0);
319 dev_err(&GET_DEV(accel_dev),
320 "Failed to send Compatibility Version Request.\n");
324 /* Wait for response */
325 if (!wait_for_completion_timeout(&accel_dev->vf.iov_msg_completion,
327 dev_err(&GET_DEV(accel_dev),
328 "IOV request/response message timeout expired\n");
332 /* Response from PF received, check compatibility */
333 switch (accel_dev->vf.compatible) {
334 case ADF_PF2VF_VF_COMPATIBLE:
336 case ADF_PF2VF_VF_COMPAT_UNKNOWN:
337 /* VF is newer than PF and decides whether it is compatible */
338 if (accel_dev->vf.pf_version >= hw_data->min_iov_compat_ver)
341 case ADF_PF2VF_VF_INCOMPATIBLE:
342 dev_err(&GET_DEV(accel_dev),
343 "PF (vers %d) and VF (vers %d) are not compatible\n",
344 accel_dev->vf.pf_version,
345 ADF_PFVF_COMPATIBILITY_VERSION);
348 dev_err(&GET_DEV(accel_dev),
349 "Invalid response from PF; assume not compatible\n");
356 * adf_enable_vf2pf_comms() - Function enables communication from vf to pf
358 * @accel_dev: Pointer to acceleration device virtual function.
360 * Return: 0 on success, error code otherwise.
362 int adf_enable_vf2pf_comms(struct adf_accel_dev *accel_dev)
364 adf_enable_pf2vf_interrupts(accel_dev);
365 return adf_vf2pf_request_version(accel_dev);
367 EXPORT_SYMBOL_GPL(adf_enable_vf2pf_comms);