mbx.c 8.9 KB

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