mbx.c 8.9 KB

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