PageRenderTime 313ms CodeModel.GetById 30ms RepoModel.GetById 1ms app.codeStats 0ms

/drivers/net/ixgbevf/mbx.c

https://bitbucket.org/slukk/jb-tsm-kernel-4.2
C | 341 lines | 148 code | 58 blank | 135 comment | 17 complexity | 693c1106e14cc4bf3dd9f5f821924d65 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0, AGPL-1.0
  1. /*******************************************************************************
  2. Intel 82599 Virtual Function driver
  3. Copyright(c) 1999 - 2010 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. /**
  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 acknowledgement
  42. * @hw: pointer to the HW structure
  43. *
  44. * returns 0 if it successfully received a message acknowledgement
  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. ret_val = ixgbevf_poll_for_msg(hw);
  73. /* if ack received read message, otherwise we timed out */
  74. if (!ret_val)
  75. ret_val = mbx->ops.read(hw, msg, size);
  76. return ret_val;
  77. }
  78. /**
  79. * ixgbevf_write_posted_mbx - Write a message to the mailbox, wait for ack
  80. * @hw: pointer to the HW structure
  81. * @msg: The message buffer
  82. * @size: Length of buffer
  83. *
  84. * returns 0 if it successfully copied message into the buffer and
  85. * received an ack to that message within delay * timeout period
  86. **/
  87. static s32 ixgbevf_write_posted_mbx(struct ixgbe_hw *hw, u32 *msg, u16 size)
  88. {
  89. struct ixgbe_mbx_info *mbx = &hw->mbx;
  90. s32 ret_val;
  91. /* send msg */
  92. ret_val = mbx->ops.write(hw, msg, size);
  93. /* if msg sent wait until we receive an ack */
  94. if (!ret_val)
  95. ret_val = ixgbevf_poll_for_ack(hw);
  96. return ret_val;
  97. }
  98. /**
  99. * ixgbevf_read_v2p_mailbox - read v2p mailbox
  100. * @hw: pointer to the HW structure
  101. *
  102. * This function is used to read the v2p mailbox without losing the read to
  103. * clear status bits.
  104. **/
  105. static u32 ixgbevf_read_v2p_mailbox(struct ixgbe_hw *hw)
  106. {
  107. u32 v2p_mailbox = IXGBE_READ_REG(hw, IXGBE_VFMAILBOX);
  108. v2p_mailbox |= hw->mbx.v2p_mailbox;
  109. hw->mbx.v2p_mailbox |= v2p_mailbox & IXGBE_VFMAILBOX_R2C_BITS;
  110. return v2p_mailbox;
  111. }
  112. /**
  113. * ixgbevf_check_for_bit_vf - Determine if a status bit was set
  114. * @hw: pointer to the HW structure
  115. * @mask: bitmask for bits to be tested and cleared
  116. *
  117. * This function is used to check for the read to clear bits within
  118. * the V2P mailbox.
  119. **/
  120. static s32 ixgbevf_check_for_bit_vf(struct ixgbe_hw *hw, u32 mask)
  121. {
  122. u32 v2p_mailbox = ixgbevf_read_v2p_mailbox(hw);
  123. s32 ret_val = IXGBE_ERR_MBX;
  124. if (v2p_mailbox & mask)
  125. ret_val = 0;
  126. hw->mbx.v2p_mailbox &= ~mask;
  127. return ret_val;
  128. }
  129. /**
  130. * ixgbevf_check_for_msg_vf - checks to see if the PF has sent mail
  131. * @hw: pointer to the HW structure
  132. *
  133. * returns 0 if the PF has set the Status bit or else ERR_MBX
  134. **/
  135. static s32 ixgbevf_check_for_msg_vf(struct ixgbe_hw *hw)
  136. {
  137. s32 ret_val = IXGBE_ERR_MBX;
  138. if (!ixgbevf_check_for_bit_vf(hw, IXGBE_VFMAILBOX_PFSTS)) {
  139. ret_val = 0;
  140. hw->mbx.stats.reqs++;
  141. }
  142. return ret_val;
  143. }
  144. /**
  145. * ixgbevf_check_for_ack_vf - checks to see if the PF has ACK'd
  146. * @hw: pointer to the HW structure
  147. *
  148. * returns 0 if the PF has set the ACK bit or else ERR_MBX
  149. **/
  150. static s32 ixgbevf_check_for_ack_vf(struct ixgbe_hw *hw)
  151. {
  152. s32 ret_val = IXGBE_ERR_MBX;
  153. if (!ixgbevf_check_for_bit_vf(hw, IXGBE_VFMAILBOX_PFACK)) {
  154. ret_val = 0;
  155. hw->mbx.stats.acks++;
  156. }
  157. return ret_val;
  158. }
  159. /**
  160. * ixgbevf_check_for_rst_vf - checks to see if the PF has reset
  161. * @hw: pointer to the HW structure
  162. *
  163. * returns true if the PF has set the reset done bit or else false
  164. **/
  165. static s32 ixgbevf_check_for_rst_vf(struct ixgbe_hw *hw)
  166. {
  167. s32 ret_val = IXGBE_ERR_MBX;
  168. if (!ixgbevf_check_for_bit_vf(hw, (IXGBE_VFMAILBOX_RSTD |
  169. IXGBE_VFMAILBOX_RSTI))) {
  170. ret_val = 0;
  171. hw->mbx.stats.rsts++;
  172. }
  173. return ret_val;
  174. }
  175. /**
  176. * ixgbevf_obtain_mbx_lock_vf - obtain mailbox lock
  177. * @hw: pointer to the HW structure
  178. *
  179. * return 0 if we obtained the mailbox lock
  180. **/
  181. static s32 ixgbevf_obtain_mbx_lock_vf(struct ixgbe_hw *hw)
  182. {
  183. s32 ret_val = IXGBE_ERR_MBX;
  184. /* Take ownership of the buffer */
  185. IXGBE_WRITE_REG(hw, IXGBE_VFMAILBOX, IXGBE_VFMAILBOX_VFU);
  186. /* reserve mailbox for vf use */
  187. if (ixgbevf_read_v2p_mailbox(hw) & IXGBE_VFMAILBOX_VFU)
  188. ret_val = 0;
  189. return ret_val;
  190. }
  191. /**
  192. * ixgbevf_write_mbx_vf - Write a message to the mailbox
  193. * @hw: pointer to the HW structure
  194. * @msg: The message buffer
  195. * @size: Length of buffer
  196. *
  197. * returns 0 if it successfully copied message into the buffer
  198. **/
  199. static s32 ixgbevf_write_mbx_vf(struct ixgbe_hw *hw, u32 *msg, u16 size)
  200. {
  201. s32 ret_val;
  202. u16 i;
  203. /* lock the mailbox to prevent pf/vf race condition */
  204. ret_val = ixgbevf_obtain_mbx_lock_vf(hw);
  205. if (ret_val)
  206. goto out_no_write;
  207. /* flush msg and acks as we are overwriting the message buffer */
  208. ixgbevf_check_for_msg_vf(hw);
  209. ixgbevf_check_for_ack_vf(hw);
  210. /* copy the caller specified message to the mailbox memory buffer */
  211. for (i = 0; i < size; i++)
  212. IXGBE_WRITE_REG_ARRAY(hw, IXGBE_VFMBMEM, i, msg[i]);
  213. /* update stats */
  214. hw->mbx.stats.msgs_tx++;
  215. /* Drop VFU and interrupt the PF to tell it a message has been sent */
  216. IXGBE_WRITE_REG(hw, IXGBE_VFMAILBOX, IXGBE_VFMAILBOX_REQ);
  217. out_no_write:
  218. return ret_val;
  219. }
  220. /**
  221. * ixgbevf_read_mbx_vf - Reads a message from the inbox intended for vf
  222. * @hw: pointer to the HW structure
  223. * @msg: The message buffer
  224. * @size: Length of buffer
  225. *
  226. * returns 0 if it successfuly read message from buffer
  227. **/
  228. static s32 ixgbevf_read_mbx_vf(struct ixgbe_hw *hw, u32 *msg, u16 size)
  229. {
  230. s32 ret_val = 0;
  231. u16 i;
  232. /* lock the mailbox to prevent pf/vf race condition */
  233. ret_val = ixgbevf_obtain_mbx_lock_vf(hw);
  234. if (ret_val)
  235. goto out_no_read;
  236. /* copy the message from the mailbox memory buffer */
  237. for (i = 0; i < size; i++)
  238. msg[i] = IXGBE_READ_REG_ARRAY(hw, IXGBE_VFMBMEM, i);
  239. /* Acknowledge receipt and release mailbox, then we're done */
  240. IXGBE_WRITE_REG(hw, IXGBE_VFMAILBOX, IXGBE_VFMAILBOX_ACK);
  241. /* update stats */
  242. hw->mbx.stats.msgs_rx++;
  243. out_no_read:
  244. return ret_val;
  245. }
  246. /**
  247. * ixgbevf_init_mbx_params_vf - set initial values for vf mailbox
  248. * @hw: pointer to the HW structure
  249. *
  250. * Initializes the hw->mbx struct to correct values for vf mailbox
  251. */
  252. static s32 ixgbevf_init_mbx_params_vf(struct ixgbe_hw *hw)
  253. {
  254. struct ixgbe_mbx_info *mbx = &hw->mbx;
  255. /* start mailbox as timed out and let the reset_hw call set the timeout
  256. * value to begin communications */
  257. mbx->timeout = 0;
  258. mbx->udelay = IXGBE_VF_MBX_INIT_DELAY;
  259. mbx->size = IXGBE_VFMAILBOX_SIZE;
  260. mbx->stats.msgs_tx = 0;
  261. mbx->stats.msgs_rx = 0;
  262. mbx->stats.reqs = 0;
  263. mbx->stats.acks = 0;
  264. mbx->stats.rsts = 0;
  265. return 0;
  266. }
  267. struct ixgbe_mbx_operations ixgbevf_mbx_ops = {
  268. .init_params = ixgbevf_init_mbx_params_vf,
  269. .read = ixgbevf_read_mbx_vf,
  270. .write = ixgbevf_write_mbx_vf,
  271. .read_posted = ixgbevf_read_posted_mbx,
  272. .write_posted = ixgbevf_write_posted_mbx,
  273. .check_for_msg = ixgbevf_check_for_msg_vf,
  274. .check_for_ack = ixgbevf_check_for_ack_vf,
  275. .check_for_rst = ixgbevf_check_for_rst_vf,
  276. };