mbx.c 8.0 KB

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