adf_pf2vf_msg.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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. GPL LICENSE SUMMARY
  5. Copyright(c) 2015 Intel Corporation.
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of version 2 of the GNU General Public License as
  8. published by the Free Software Foundation.
  9. This program is distributed in the hope that it will be useful, but
  10. WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. General Public License for more details.
  13. Contact Information:
  14. qat-linux@intel.com
  15. BSD LICENSE
  16. Copyright(c) 2015 Intel Corporation.
  17. Redistribution and use in source and binary forms, with or without
  18. modification, are permitted provided that the following conditions
  19. are met:
  20. * Redistributions of source code must retain the above copyright
  21. notice, this list of conditions and the following disclaimer.
  22. * Redistributions in binary form must reproduce the above copyright
  23. notice, this list of conditions and the following disclaimer in
  24. the documentation and/or other materials provided with the
  25. distribution.
  26. * Neither the name of Intel Corporation nor the names of its
  27. contributors may be used to endorse or promote products derived
  28. from this software without specific prior written permission.
  29. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  30. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  31. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  32. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  33. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  34. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  35. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  36. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  37. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  38. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  39. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  40. */
  41. #include <linux/delay.h>
  42. #include "adf_accel_devices.h"
  43. #include "adf_common_drv.h"
  44. #include "adf_pf2vf_msg.h"
  45. #define ADF_DH895XCC_EP_OFFSET 0x3A000
  46. #define ADF_DH895XCC_ERRMSK3 (ADF_DH895XCC_EP_OFFSET + 0x1C)
  47. #define ADF_DH895XCC_ERRMSK3_VF2PF_L_MASK(vf_mask) ((vf_mask & 0xFFFF) << 9)
  48. #define ADF_DH895XCC_ERRMSK5 (ADF_DH895XCC_EP_OFFSET + 0xDC)
  49. #define ADF_DH895XCC_ERRMSK5_VF2PF_U_MASK(vf_mask) (vf_mask >> 16)
  50. void adf_enable_pf2vf_interrupts(struct adf_accel_dev *accel_dev)
  51. {
  52. struct adf_accel_pci *pci_info = &accel_dev->accel_pci_dev;
  53. struct adf_hw_device_data *hw_data = accel_dev->hw_device;
  54. void __iomem *pmisc_bar_addr =
  55. pci_info->pci_bars[hw_data->get_misc_bar_id(hw_data)].virt_addr;
  56. ADF_CSR_WR(pmisc_bar_addr, hw_data->get_vintmsk_offset(0), 0x0);
  57. }
  58. void adf_disable_pf2vf_interrupts(struct adf_accel_dev *accel_dev)
  59. {
  60. struct adf_accel_pci *pci_info = &accel_dev->accel_pci_dev;
  61. struct adf_hw_device_data *hw_data = accel_dev->hw_device;
  62. void __iomem *pmisc_bar_addr =
  63. pci_info->pci_bars[hw_data->get_misc_bar_id(hw_data)].virt_addr;
  64. ADF_CSR_WR(pmisc_bar_addr, hw_data->get_vintmsk_offset(0), 0x2);
  65. }
  66. void adf_enable_vf2pf_interrupts(struct adf_accel_dev *accel_dev,
  67. u32 vf_mask)
  68. {
  69. struct adf_hw_device_data *hw_data = accel_dev->hw_device;
  70. struct adf_bar *pmisc =
  71. &GET_BARS(accel_dev)[hw_data->get_misc_bar_id(hw_data)];
  72. void __iomem *pmisc_addr = pmisc->virt_addr;
  73. u32 reg;
  74. /* Enable VF2PF Messaging Ints - VFs 1 through 16 per vf_mask[15:0] */
  75. if (vf_mask & 0xFFFF) {
  76. reg = ADF_CSR_RD(pmisc_addr, ADF_DH895XCC_ERRMSK3);
  77. reg &= ~ADF_DH895XCC_ERRMSK3_VF2PF_L_MASK(vf_mask);
  78. ADF_CSR_WR(pmisc_addr, ADF_DH895XCC_ERRMSK3, reg);
  79. }
  80. /* Enable VF2PF Messaging Ints - VFs 17 through 32 per vf_mask[31:16] */
  81. if (vf_mask >> 16) {
  82. reg = ADF_CSR_RD(pmisc_addr, ADF_DH895XCC_ERRMSK5);
  83. reg &= ~ADF_DH895XCC_ERRMSK5_VF2PF_U_MASK(vf_mask);
  84. ADF_CSR_WR(pmisc_addr, ADF_DH895XCC_ERRMSK5, reg);
  85. }
  86. }
  87. void adf_disable_vf2pf_interrupts(struct adf_accel_dev *accel_dev, u32 vf_mask)
  88. {
  89. struct adf_hw_device_data *hw_data = accel_dev->hw_device;
  90. struct adf_bar *pmisc =
  91. &GET_BARS(accel_dev)[hw_data->get_misc_bar_id(hw_data)];
  92. void __iomem *pmisc_addr = pmisc->virt_addr;
  93. u32 reg;
  94. /* Disable VF2PF interrupts for VFs 1 through 16 per vf_mask[15:0] */
  95. if (vf_mask & 0xFFFF) {
  96. reg = ADF_CSR_RD(pmisc_addr, ADF_DH895XCC_ERRMSK3) |
  97. ADF_DH895XCC_ERRMSK3_VF2PF_L_MASK(vf_mask);
  98. ADF_CSR_WR(pmisc_addr, ADF_DH895XCC_ERRMSK3, reg);
  99. }
  100. /* Disable VF2PF interrupts for VFs 17 through 32 per vf_mask[31:16] */
  101. if (vf_mask >> 16) {
  102. reg = ADF_CSR_RD(pmisc_addr, ADF_DH895XCC_ERRMSK5) |
  103. ADF_DH895XCC_ERRMSK5_VF2PF_U_MASK(vf_mask);
  104. ADF_CSR_WR(pmisc_addr, ADF_DH895XCC_ERRMSK5, reg);
  105. }
  106. }
  107. static int __adf_iov_putmsg(struct adf_accel_dev *accel_dev, u32 msg, u8 vf_nr)
  108. {
  109. struct adf_accel_pci *pci_info = &accel_dev->accel_pci_dev;
  110. struct adf_hw_device_data *hw_data = accel_dev->hw_device;
  111. void __iomem *pmisc_bar_addr =
  112. pci_info->pci_bars[hw_data->get_misc_bar_id(hw_data)].virt_addr;
  113. u32 val, pf2vf_offset, count = 0;
  114. u32 local_in_use_mask, local_in_use_pattern;
  115. u32 remote_in_use_mask, remote_in_use_pattern;
  116. struct mutex *lock; /* lock preventing concurrent acces of CSR */
  117. u32 int_bit;
  118. int ret = 0;
  119. if (accel_dev->is_vf) {
  120. pf2vf_offset = hw_data->get_pf2vf_offset(0);
  121. lock = &accel_dev->vf.vf2pf_lock;
  122. local_in_use_mask = ADF_VF2PF_IN_USE_BY_VF_MASK;
  123. local_in_use_pattern = ADF_VF2PF_IN_USE_BY_VF;
  124. remote_in_use_mask = ADF_PF2VF_IN_USE_BY_PF_MASK;
  125. remote_in_use_pattern = ADF_PF2VF_IN_USE_BY_PF;
  126. int_bit = ADF_VF2PF_INT;
  127. } else {
  128. pf2vf_offset = hw_data->get_pf2vf_offset(vf_nr);
  129. lock = &accel_dev->pf.vf_info[vf_nr].pf2vf_lock;
  130. local_in_use_mask = ADF_PF2VF_IN_USE_BY_PF_MASK;
  131. local_in_use_pattern = ADF_PF2VF_IN_USE_BY_PF;
  132. remote_in_use_mask = ADF_VF2PF_IN_USE_BY_VF_MASK;
  133. remote_in_use_pattern = ADF_VF2PF_IN_USE_BY_VF;
  134. int_bit = ADF_PF2VF_INT;
  135. }
  136. mutex_lock(lock);
  137. /* Check if PF2VF CSR is in use by remote function */
  138. val = ADF_CSR_RD(pmisc_bar_addr, pf2vf_offset);
  139. if ((val & remote_in_use_mask) == remote_in_use_pattern) {
  140. dev_dbg(&GET_DEV(accel_dev),
  141. "PF2VF CSR in use by remote function\n");
  142. ret = -EBUSY;
  143. goto out;
  144. }
  145. /* Attempt to get ownership of PF2VF CSR */
  146. msg &= ~local_in_use_mask;
  147. msg |= local_in_use_pattern;
  148. ADF_CSR_WR(pmisc_bar_addr, pf2vf_offset, msg);
  149. /* Wait in case remote func also attempting to get ownership */
  150. msleep(ADF_IOV_MSG_COLLISION_DETECT_DELAY);
  151. val = ADF_CSR_RD(pmisc_bar_addr, pf2vf_offset);
  152. if ((val & local_in_use_mask) != local_in_use_pattern) {
  153. dev_dbg(&GET_DEV(accel_dev),
  154. "PF2VF CSR in use by remote - collision detected\n");
  155. ret = -EBUSY;
  156. goto out;
  157. }
  158. /*
  159. * This function now owns the PV2VF CSR. The IN_USE_BY pattern must
  160. * remain in the PF2VF CSR for all writes including ACK from remote
  161. * until this local function relinquishes the CSR. Send the message
  162. * by interrupting the remote.
  163. */
  164. ADF_CSR_WR(pmisc_bar_addr, pf2vf_offset, msg | int_bit);
  165. /* Wait for confirmation from remote func it received the message */
  166. do {
  167. msleep(ADF_IOV_MSG_ACK_DELAY);
  168. val = ADF_CSR_RD(pmisc_bar_addr, pf2vf_offset);
  169. } while ((val & int_bit) && (count++ < ADF_IOV_MSG_ACK_MAX_RETRY));
  170. if (val & int_bit) {
  171. dev_dbg(&GET_DEV(accel_dev), "ACK not received from remote\n");
  172. val &= ~int_bit;
  173. ret = -EIO;
  174. }
  175. /* Finished with PF2VF CSR; relinquish it and leave msg in CSR */
  176. ADF_CSR_WR(pmisc_bar_addr, pf2vf_offset, val & ~local_in_use_mask);
  177. out:
  178. mutex_unlock(lock);
  179. return ret;
  180. }
  181. /**
  182. * adf_iov_putmsg() - send PF2VF message
  183. * @accel_dev: Pointer to acceleration device.
  184. * @msg: Message to send
  185. * @vf_nr: VF number to which the message will be sent
  186. *
  187. * Function sends a messge from the PF to a VF
  188. *
  189. * Return: 0 on success, error code otherwise.
  190. */
  191. int adf_iov_putmsg(struct adf_accel_dev *accel_dev, u32 msg, u8 vf_nr)
  192. {
  193. u32 count = 0;
  194. int ret;
  195. do {
  196. ret = __adf_iov_putmsg(accel_dev, msg, vf_nr);
  197. if (ret)
  198. msleep(ADF_IOV_MSG_RETRY_DELAY);
  199. } while (ret && (count++ < ADF_IOV_MSG_MAX_RETRIES));
  200. return ret;
  201. }
  202. EXPORT_SYMBOL_GPL(adf_iov_putmsg);
  203. void adf_vf2pf_req_hndl(struct adf_accel_vf_info *vf_info)
  204. {
  205. struct adf_accel_dev *accel_dev = vf_info->accel_dev;
  206. struct adf_hw_device_data *hw_data = accel_dev->hw_device;
  207. int bar_id = hw_data->get_misc_bar_id(hw_data);
  208. struct adf_bar *pmisc = &GET_BARS(accel_dev)[bar_id];
  209. void __iomem *pmisc_addr = pmisc->virt_addr;
  210. u32 msg, resp = 0, vf_nr = vf_info->vf_nr;
  211. /* Read message from the VF */
  212. msg = ADF_CSR_RD(pmisc_addr, hw_data->get_pf2vf_offset(vf_nr));
  213. /* To ACK, clear the VF2PFINT bit */
  214. msg &= ~ADF_VF2PF_INT;
  215. ADF_CSR_WR(pmisc_addr, hw_data->get_pf2vf_offset(vf_nr), msg);
  216. if (!(msg & ADF_VF2PF_MSGORIGIN_SYSTEM))
  217. /* Ignore legacy non-system (non-kernel) VF2PF messages */
  218. goto err;
  219. switch ((msg & ADF_VF2PF_MSGTYPE_MASK) >> ADF_VF2PF_MSGTYPE_SHIFT) {
  220. case ADF_VF2PF_MSGTYPE_COMPAT_VER_REQ:
  221. {
  222. u8 vf_compat_ver = msg >> ADF_VF2PF_COMPAT_VER_REQ_SHIFT;
  223. resp = (ADF_PF2VF_MSGORIGIN_SYSTEM |
  224. (ADF_PF2VF_MSGTYPE_VERSION_RESP <<
  225. ADF_PF2VF_MSGTYPE_SHIFT) |
  226. (ADF_PFVF_COMPATIBILITY_VERSION <<
  227. ADF_PF2VF_VERSION_RESP_VERS_SHIFT));
  228. dev_dbg(&GET_DEV(accel_dev),
  229. "Compatibility Version Request from VF%d vers=%u\n",
  230. vf_nr + 1, vf_compat_ver);
  231. if (vf_compat_ver < hw_data->min_iov_compat_ver) {
  232. dev_err(&GET_DEV(accel_dev),
  233. "VF (vers %d) incompatible with PF (vers %d)\n",
  234. vf_compat_ver, ADF_PFVF_COMPATIBILITY_VERSION);
  235. resp |= ADF_PF2VF_VF_INCOMPATIBLE <<
  236. ADF_PF2VF_VERSION_RESP_RESULT_SHIFT;
  237. } else if (vf_compat_ver > ADF_PFVF_COMPATIBILITY_VERSION) {
  238. dev_err(&GET_DEV(accel_dev),
  239. "VF (vers %d) compat with PF (vers %d) unkn.\n",
  240. vf_compat_ver, ADF_PFVF_COMPATIBILITY_VERSION);
  241. resp |= ADF_PF2VF_VF_COMPAT_UNKNOWN <<
  242. ADF_PF2VF_VERSION_RESP_RESULT_SHIFT;
  243. } else {
  244. dev_dbg(&GET_DEV(accel_dev),
  245. "VF (vers %d) compatible with PF (vers %d)\n",
  246. vf_compat_ver, ADF_PFVF_COMPATIBILITY_VERSION);
  247. resp |= ADF_PF2VF_VF_COMPATIBLE <<
  248. ADF_PF2VF_VERSION_RESP_RESULT_SHIFT;
  249. }
  250. }
  251. break;
  252. case ADF_VF2PF_MSGTYPE_VERSION_REQ:
  253. dev_dbg(&GET_DEV(accel_dev),
  254. "Legacy VersionRequest received from VF%d 0x%x\n",
  255. vf_nr + 1, msg);
  256. resp = (ADF_PF2VF_MSGORIGIN_SYSTEM |
  257. (ADF_PF2VF_MSGTYPE_VERSION_RESP <<
  258. ADF_PF2VF_MSGTYPE_SHIFT) |
  259. (ADF_PFVF_COMPATIBILITY_VERSION <<
  260. ADF_PF2VF_VERSION_RESP_VERS_SHIFT));
  261. resp |= ADF_PF2VF_VF_COMPATIBLE <<
  262. ADF_PF2VF_VERSION_RESP_RESULT_SHIFT;
  263. /* Set legacy major and minor version num */
  264. resp |= 1 << ADF_PF2VF_MAJORVERSION_SHIFT |
  265. 1 << ADF_PF2VF_MINORVERSION_SHIFT;
  266. break;
  267. case ADF_VF2PF_MSGTYPE_INIT:
  268. {
  269. dev_dbg(&GET_DEV(accel_dev),
  270. "Init message received from VF%d 0x%x\n",
  271. vf_nr + 1, msg);
  272. vf_info->init = true;
  273. }
  274. break;
  275. case ADF_VF2PF_MSGTYPE_SHUTDOWN:
  276. {
  277. dev_dbg(&GET_DEV(accel_dev),
  278. "Shutdown message received from VF%d 0x%x\n",
  279. vf_nr + 1, msg);
  280. vf_info->init = false;
  281. }
  282. break;
  283. default:
  284. goto err;
  285. }
  286. if (resp && adf_iov_putmsg(accel_dev, resp, vf_nr))
  287. dev_err(&GET_DEV(accel_dev), "Failed to send response to VF\n");
  288. /* re-enable interrupt on PF from this VF */
  289. adf_enable_vf2pf_interrupts(accel_dev, (1 << vf_nr));
  290. return;
  291. err:
  292. dev_dbg(&GET_DEV(accel_dev), "Unknown message from VF%d (0x%x);\n",
  293. vf_nr + 1, msg);
  294. }
  295. void adf_pf2vf_notify_restarting(struct adf_accel_dev *accel_dev)
  296. {
  297. struct adf_accel_vf_info *vf;
  298. u32 msg = (ADF_PF2VF_MSGORIGIN_SYSTEM |
  299. (ADF_PF2VF_MSGTYPE_RESTARTING << ADF_PF2VF_MSGTYPE_SHIFT));
  300. int i, num_vfs = pci_num_vf(accel_to_pci_dev(accel_dev));
  301. for (i = 0, vf = accel_dev->pf.vf_info; i < num_vfs; i++, vf++) {
  302. if (vf->init && adf_iov_putmsg(accel_dev, msg, i))
  303. dev_err(&GET_DEV(accel_dev),
  304. "Failed to send restarting msg to VF%d\n", i);
  305. }
  306. }
  307. static int adf_vf2pf_request_version(struct adf_accel_dev *accel_dev)
  308. {
  309. unsigned long timeout = msecs_to_jiffies(ADF_IOV_MSG_RESP_TIMEOUT);
  310. struct adf_hw_device_data *hw_data = accel_dev->hw_device;
  311. u32 msg = 0;
  312. int ret;
  313. msg = ADF_VF2PF_MSGORIGIN_SYSTEM;
  314. msg |= ADF_VF2PF_MSGTYPE_COMPAT_VER_REQ << ADF_VF2PF_MSGTYPE_SHIFT;
  315. msg |= ADF_PFVF_COMPATIBILITY_VERSION << ADF_VF2PF_COMPAT_VER_REQ_SHIFT;
  316. BUILD_BUG_ON(ADF_PFVF_COMPATIBILITY_VERSION > 255);
  317. /* Send request from VF to PF */
  318. ret = adf_iov_putmsg(accel_dev, msg, 0);
  319. if (ret) {
  320. dev_err(&GET_DEV(accel_dev),
  321. "Failed to send Compatibility Version Request.\n");
  322. return ret;
  323. }
  324. /* Wait for response */
  325. if (!wait_for_completion_timeout(&accel_dev->vf.iov_msg_completion,
  326. timeout)) {
  327. dev_err(&GET_DEV(accel_dev),
  328. "IOV request/response message timeout expired\n");
  329. return -EIO;
  330. }
  331. /* Response from PF received, check compatibility */
  332. switch (accel_dev->vf.compatible) {
  333. case ADF_PF2VF_VF_COMPATIBLE:
  334. break;
  335. case ADF_PF2VF_VF_COMPAT_UNKNOWN:
  336. /* VF is newer than PF and decides whether it is compatible */
  337. if (accel_dev->vf.pf_version >= hw_data->min_iov_compat_ver)
  338. break;
  339. /* fall through */
  340. case ADF_PF2VF_VF_INCOMPATIBLE:
  341. dev_err(&GET_DEV(accel_dev),
  342. "PF (vers %d) and VF (vers %d) are not compatible\n",
  343. accel_dev->vf.pf_version,
  344. ADF_PFVF_COMPATIBILITY_VERSION);
  345. return -EINVAL;
  346. default:
  347. dev_err(&GET_DEV(accel_dev),
  348. "Invalid response from PF; assume not compatible\n");
  349. return -EINVAL;
  350. }
  351. return ret;
  352. }
  353. /**
  354. * adf_enable_vf2pf_comms() - Function enables communication from vf to pf
  355. *
  356. * @accel_dev: Pointer to acceleration device virtual function.
  357. *
  358. * Return: 0 on success, error code otherwise.
  359. */
  360. int adf_enable_vf2pf_comms(struct adf_accel_dev *accel_dev)
  361. {
  362. adf_enable_pf2vf_interrupts(accel_dev);
  363. return adf_vf2pf_request_version(accel_dev);
  364. }
  365. EXPORT_SYMBOL_GPL(adf_enable_vf2pf_comms);