mbx.c 8.0 KB

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