mbx.c 9.2 KB

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