mbx.c 8.7 KB

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