mbx.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright(c) 1999 - 2018 Intel Corporation. */
  3. #include "mbx.h"
  4. #include "ixgbevf.h"
  5. /**
  6. * ixgbevf_poll_for_msg - Wait for message notification
  7. * @hw: pointer to the HW structure
  8. *
  9. * returns 0 if it successfully received a message notification
  10. **/
  11. static s32 ixgbevf_poll_for_msg(struct ixgbe_hw *hw)
  12. {
  13. struct ixgbe_mbx_info *mbx = &hw->mbx;
  14. int countdown = mbx->timeout;
  15. while (countdown && mbx->ops.check_for_msg(hw)) {
  16. countdown--;
  17. udelay(mbx->udelay);
  18. }
  19. /* if we failed, all future posted messages fail until reset */
  20. if (!countdown)
  21. mbx->timeout = 0;
  22. return countdown ? 0 : IXGBE_ERR_MBX;
  23. }
  24. /**
  25. * ixgbevf_poll_for_ack - Wait for message acknowledgment
  26. * @hw: pointer to the HW structure
  27. *
  28. * returns 0 if it successfully received a message acknowledgment
  29. **/
  30. static s32 ixgbevf_poll_for_ack(struct ixgbe_hw *hw)
  31. {
  32. struct ixgbe_mbx_info *mbx = &hw->mbx;
  33. int countdown = mbx->timeout;
  34. while (countdown && mbx->ops.check_for_ack(hw)) {
  35. countdown--;
  36. udelay(mbx->udelay);
  37. }
  38. /* if we failed, all future posted messages fail until reset */
  39. if (!countdown)
  40. mbx->timeout = 0;
  41. return countdown ? 0 : IXGBE_ERR_MBX;
  42. }
  43. /**
  44. * ixgbevf_read_posted_mbx - Wait for message notification and receive message
  45. * @hw: pointer to the HW structure
  46. * @msg: The message buffer
  47. * @size: Length of buffer
  48. *
  49. * returns 0 if it successfully received a message notification and
  50. * copied it into the receive buffer.
  51. **/
  52. static s32 ixgbevf_read_posted_mbx(struct ixgbe_hw *hw, u32 *msg, u16 size)
  53. {
  54. struct ixgbe_mbx_info *mbx = &hw->mbx;
  55. s32 ret_val = IXGBE_ERR_MBX;
  56. if (!mbx->ops.read)
  57. goto out;
  58. ret_val = ixgbevf_poll_for_msg(hw);
  59. /* if ack received read message, otherwise we timed out */
  60. if (!ret_val)
  61. ret_val = mbx->ops.read(hw, msg, size);
  62. out:
  63. return ret_val;
  64. }
  65. /**
  66. * ixgbevf_write_posted_mbx - Write a message to the mailbox, wait for ack
  67. * @hw: pointer to the HW structure
  68. * @msg: The message buffer
  69. * @size: Length of buffer
  70. *
  71. * returns 0 if it successfully copied message into the buffer and
  72. * received an ack to that message within delay * timeout period
  73. **/
  74. static s32 ixgbevf_write_posted_mbx(struct ixgbe_hw *hw, u32 *msg, u16 size)
  75. {
  76. struct ixgbe_mbx_info *mbx = &hw->mbx;
  77. s32 ret_val = IXGBE_ERR_MBX;
  78. /* exit if either we can't write or there isn't a defined timeout */
  79. if (!mbx->ops.write || !mbx->timeout)
  80. goto out;
  81. /* send msg */
  82. ret_val = mbx->ops.write(hw, msg, size);
  83. /* if msg sent wait until we receive an ack */
  84. if (!ret_val)
  85. ret_val = ixgbevf_poll_for_ack(hw);
  86. out:
  87. return ret_val;
  88. }
  89. /**
  90. * ixgbevf_read_v2p_mailbox - read v2p mailbox
  91. * @hw: pointer to the HW structure
  92. *
  93. * This function is used to read the v2p mailbox without losing the read to
  94. * clear status bits.
  95. **/
  96. static u32 ixgbevf_read_v2p_mailbox(struct ixgbe_hw *hw)
  97. {
  98. u32 v2p_mailbox = IXGBE_READ_REG(hw, IXGBE_VFMAILBOX);
  99. v2p_mailbox |= hw->mbx.v2p_mailbox;
  100. hw->mbx.v2p_mailbox |= v2p_mailbox & IXGBE_VFMAILBOX_R2C_BITS;
  101. return v2p_mailbox;
  102. }
  103. /**
  104. * ixgbevf_check_for_bit_vf - Determine if a status bit was set
  105. * @hw: pointer to the HW structure
  106. * @mask: bitmask for bits to be tested and cleared
  107. *
  108. * This function is used to check for the read to clear bits within
  109. * the V2P mailbox.
  110. **/
  111. static s32 ixgbevf_check_for_bit_vf(struct ixgbe_hw *hw, u32 mask)
  112. {
  113. u32 v2p_mailbox = ixgbevf_read_v2p_mailbox(hw);
  114. s32 ret_val = IXGBE_ERR_MBX;
  115. if (v2p_mailbox & mask)
  116. ret_val = 0;
  117. hw->mbx.v2p_mailbox &= ~mask;
  118. return ret_val;
  119. }
  120. /**
  121. * ixgbevf_check_for_msg_vf - checks to see if the PF has sent mail
  122. * @hw: pointer to the HW structure
  123. *
  124. * returns 0 if the PF has set the Status bit or else ERR_MBX
  125. **/
  126. static s32 ixgbevf_check_for_msg_vf(struct ixgbe_hw *hw)
  127. {
  128. s32 ret_val = IXGBE_ERR_MBX;
  129. if (!ixgbevf_check_for_bit_vf(hw, IXGBE_VFMAILBOX_PFSTS)) {
  130. ret_val = 0;
  131. hw->mbx.stats.reqs++;
  132. }
  133. return ret_val;
  134. }
  135. /**
  136. * ixgbevf_check_for_ack_vf - checks to see if the PF has ACK'd
  137. * @hw: pointer to the HW structure
  138. *
  139. * returns 0 if the PF has set the ACK bit or else ERR_MBX
  140. **/
  141. static s32 ixgbevf_check_for_ack_vf(struct ixgbe_hw *hw)
  142. {
  143. s32 ret_val = IXGBE_ERR_MBX;
  144. if (!ixgbevf_check_for_bit_vf(hw, IXGBE_VFMAILBOX_PFACK)) {
  145. ret_val = 0;
  146. hw->mbx.stats.acks++;
  147. }
  148. return ret_val;
  149. }
  150. /**
  151. * ixgbevf_check_for_rst_vf - checks to see if the PF has reset
  152. * @hw: pointer to the HW structure
  153. *
  154. * returns true if the PF has set the reset done bit or else false
  155. **/
  156. static s32 ixgbevf_check_for_rst_vf(struct ixgbe_hw *hw)
  157. {
  158. s32 ret_val = IXGBE_ERR_MBX;
  159. if (!ixgbevf_check_for_bit_vf(hw, (IXGBE_VFMAILBOX_RSTD |
  160. IXGBE_VFMAILBOX_RSTI))) {
  161. ret_val = 0;
  162. hw->mbx.stats.rsts++;
  163. }
  164. return ret_val;
  165. }
  166. /**
  167. * ixgbevf_obtain_mbx_lock_vf - obtain mailbox lock
  168. * @hw: pointer to the HW structure
  169. *
  170. * return 0 if we obtained the mailbox lock
  171. **/
  172. static s32 ixgbevf_obtain_mbx_lock_vf(struct ixgbe_hw *hw)
  173. {
  174. s32 ret_val = IXGBE_ERR_MBX;
  175. /* Take ownership of the buffer */
  176. IXGBE_WRITE_REG(hw, IXGBE_VFMAILBOX, IXGBE_VFMAILBOX_VFU);
  177. /* reserve mailbox for VF use */
  178. if (ixgbevf_read_v2p_mailbox(hw) & IXGBE_VFMAILBOX_VFU)
  179. ret_val = 0;
  180. return ret_val;
  181. }
  182. /**
  183. * ixgbevf_write_mbx_vf - Write a message to the mailbox
  184. * @hw: pointer to the HW structure
  185. * @msg: The message buffer
  186. * @size: Length of buffer
  187. *
  188. * returns 0 if it successfully copied message into the buffer
  189. **/
  190. static s32 ixgbevf_write_mbx_vf(struct ixgbe_hw *hw, u32 *msg, u16 size)
  191. {
  192. s32 ret_val;
  193. u16 i;
  194. /* lock the mailbox to prevent PF/VF race condition */
  195. ret_val = ixgbevf_obtain_mbx_lock_vf(hw);
  196. if (ret_val)
  197. goto out_no_write;
  198. /* flush msg and acks as we are overwriting the message buffer */
  199. ixgbevf_check_for_msg_vf(hw);
  200. ixgbevf_check_for_ack_vf(hw);
  201. /* copy the caller specified message to the mailbox memory buffer */
  202. for (i = 0; i < size; i++)
  203. IXGBE_WRITE_REG_ARRAY(hw, IXGBE_VFMBMEM, i, msg[i]);
  204. /* update stats */
  205. hw->mbx.stats.msgs_tx++;
  206. /* Drop VFU and interrupt the PF to tell it a message has been sent */
  207. IXGBE_WRITE_REG(hw, IXGBE_VFMAILBOX, IXGBE_VFMAILBOX_REQ);
  208. out_no_write:
  209. return ret_val;
  210. }
  211. /**
  212. * ixgbevf_read_mbx_vf - Reads a message from the inbox intended for VF
  213. * @hw: pointer to the HW structure
  214. * @msg: The message buffer
  215. * @size: Length of buffer
  216. *
  217. * returns 0 if it successfully read message from buffer
  218. **/
  219. static s32 ixgbevf_read_mbx_vf(struct ixgbe_hw *hw, u32 *msg, u16 size)
  220. {
  221. s32 ret_val = 0;
  222. u16 i;
  223. /* lock the mailbox to prevent PF/VF race condition */
  224. ret_val = ixgbevf_obtain_mbx_lock_vf(hw);
  225. if (ret_val)
  226. goto out_no_read;
  227. /* copy the message from the mailbox memory buffer */
  228. for (i = 0; i < size; i++)
  229. msg[i] = IXGBE_READ_REG_ARRAY(hw, IXGBE_VFMBMEM, i);
  230. /* Acknowledge receipt and release mailbox, then we're done */
  231. IXGBE_WRITE_REG(hw, IXGBE_VFMAILBOX, IXGBE_VFMAILBOX_ACK);
  232. /* update stats */
  233. hw->mbx.stats.msgs_rx++;
  234. out_no_read:
  235. return ret_val;
  236. }
  237. /**
  238. * ixgbevf_init_mbx_params_vf - set initial values for VF mailbox
  239. * @hw: pointer to the HW structure
  240. *
  241. * Initializes the hw->mbx struct to correct values for VF mailbox
  242. */
  243. static s32 ixgbevf_init_mbx_params_vf(struct ixgbe_hw *hw)
  244. {
  245. struct ixgbe_mbx_info *mbx = &hw->mbx;
  246. /* start mailbox as timed out and let the reset_hw call set the timeout
  247. * value to begin communications
  248. */
  249. mbx->timeout = 0;
  250. mbx->udelay = IXGBE_VF_MBX_INIT_DELAY;
  251. mbx->size = IXGBE_VFMAILBOX_SIZE;
  252. mbx->stats.msgs_tx = 0;
  253. mbx->stats.msgs_rx = 0;
  254. mbx->stats.reqs = 0;
  255. mbx->stats.acks = 0;
  256. mbx->stats.rsts = 0;
  257. return 0;
  258. }
  259. const struct ixgbe_mbx_operations ixgbevf_mbx_ops = {
  260. .init_params = ixgbevf_init_mbx_params_vf,
  261. .read = ixgbevf_read_mbx_vf,
  262. .write = ixgbevf_write_mbx_vf,
  263. .read_posted = ixgbevf_read_posted_mbx,
  264. .write_posted = ixgbevf_write_posted_mbx,
  265. .check_for_msg = ixgbevf_check_for_msg_vf,
  266. .check_for_ack = ixgbevf_check_for_ack_vf,
  267. .check_for_rst = ixgbevf_check_for_rst_vf,
  268. };
  269. /* Mailbox operations when running on Hyper-V.
  270. * On Hyper-V, PF/VF communication is not through the
  271. * hardware mailbox; this communication is through
  272. * a software mediated path.
  273. * Most mail box operations are noop while running on
  274. * Hyper-V.
  275. */
  276. const struct ixgbe_mbx_operations ixgbevf_hv_mbx_ops = {
  277. .init_params = ixgbevf_init_mbx_params_vf,
  278. .check_for_rst = ixgbevf_check_for_rst_vf,
  279. };