e1000_mbx.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. /*******************************************************************************
  2. Intel(R) Gigabit Ethernet Linux driver
  3. Copyright(c) 2007-2013 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 "e1000_mbx.h"
  21. /**
  22. * igb_read_mbx - Reads a message from the mailbox
  23. * @hw: pointer to the HW structure
  24. * @msg: The message buffer
  25. * @size: Length of buffer
  26. * @mbx_id: id of mailbox to read
  27. *
  28. * returns SUCCESS if it successfully read message from buffer
  29. **/
  30. s32 igb_read_mbx(struct e1000_hw *hw, u32 *msg, u16 size, u16 mbx_id)
  31. {
  32. struct e1000_mbx_info *mbx = &hw->mbx;
  33. s32 ret_val = -E1000_ERR_MBX;
  34. /* limit read to size of mailbox */
  35. if (size > mbx->size)
  36. size = mbx->size;
  37. if (mbx->ops.read)
  38. ret_val = mbx->ops.read(hw, msg, size, mbx_id);
  39. return ret_val;
  40. }
  41. /**
  42. * igb_write_mbx - Write a message to the mailbox
  43. * @hw: pointer to the HW structure
  44. * @msg: The message buffer
  45. * @size: Length of buffer
  46. * @mbx_id: id of mailbox to write
  47. *
  48. * returns SUCCESS if it successfully copied message into the buffer
  49. **/
  50. s32 igb_write_mbx(struct e1000_hw *hw, u32 *msg, u16 size, u16 mbx_id)
  51. {
  52. struct e1000_mbx_info *mbx = &hw->mbx;
  53. s32 ret_val = 0;
  54. if (size > mbx->size)
  55. ret_val = -E1000_ERR_MBX;
  56. else if (mbx->ops.write)
  57. ret_val = mbx->ops.write(hw, msg, size, mbx_id);
  58. return ret_val;
  59. }
  60. /**
  61. * igb_check_for_msg - checks to see if someone sent us mail
  62. * @hw: pointer to the HW structure
  63. * @mbx_id: id of mailbox to check
  64. *
  65. * returns SUCCESS if the Status bit was found or else ERR_MBX
  66. **/
  67. s32 igb_check_for_msg(struct e1000_hw *hw, u16 mbx_id)
  68. {
  69. struct e1000_mbx_info *mbx = &hw->mbx;
  70. s32 ret_val = -E1000_ERR_MBX;
  71. if (mbx->ops.check_for_msg)
  72. ret_val = mbx->ops.check_for_msg(hw, mbx_id);
  73. return ret_val;
  74. }
  75. /**
  76. * igb_check_for_ack - checks to see if someone sent us ACK
  77. * @hw: pointer to the HW structure
  78. * @mbx_id: id of mailbox to check
  79. *
  80. * returns SUCCESS if the Status bit was found or else ERR_MBX
  81. **/
  82. s32 igb_check_for_ack(struct e1000_hw *hw, u16 mbx_id)
  83. {
  84. struct e1000_mbx_info *mbx = &hw->mbx;
  85. s32 ret_val = -E1000_ERR_MBX;
  86. if (mbx->ops.check_for_ack)
  87. ret_val = mbx->ops.check_for_ack(hw, mbx_id);
  88. return ret_val;
  89. }
  90. /**
  91. * igb_check_for_rst - checks to see if other side has reset
  92. * @hw: pointer to the HW structure
  93. * @mbx_id: id of mailbox to check
  94. *
  95. * returns SUCCESS if the Status bit was found or else ERR_MBX
  96. **/
  97. s32 igb_check_for_rst(struct e1000_hw *hw, u16 mbx_id)
  98. {
  99. struct e1000_mbx_info *mbx = &hw->mbx;
  100. s32 ret_val = -E1000_ERR_MBX;
  101. if (mbx->ops.check_for_rst)
  102. ret_val = mbx->ops.check_for_rst(hw, mbx_id);
  103. return ret_val;
  104. }
  105. /**
  106. * igb_poll_for_msg - Wait for message notification
  107. * @hw: pointer to the HW structure
  108. * @mbx_id: id of mailbox to write
  109. *
  110. * returns SUCCESS if it successfully received a message notification
  111. **/
  112. static s32 igb_poll_for_msg(struct e1000_hw *hw, u16 mbx_id)
  113. {
  114. struct e1000_mbx_info *mbx = &hw->mbx;
  115. int countdown = mbx->timeout;
  116. if (!countdown || !mbx->ops.check_for_msg)
  117. goto out;
  118. while (countdown && mbx->ops.check_for_msg(hw, mbx_id)) {
  119. countdown--;
  120. if (!countdown)
  121. break;
  122. udelay(mbx->usec_delay);
  123. }
  124. /* if we failed, all future posted messages fail until reset */
  125. if (!countdown)
  126. mbx->timeout = 0;
  127. out:
  128. return countdown ? 0 : -E1000_ERR_MBX;
  129. }
  130. /**
  131. * igb_poll_for_ack - Wait for message acknowledgement
  132. * @hw: pointer to the HW structure
  133. * @mbx_id: id of mailbox to write
  134. *
  135. * returns SUCCESS if it successfully received a message acknowledgement
  136. **/
  137. static s32 igb_poll_for_ack(struct e1000_hw *hw, u16 mbx_id)
  138. {
  139. struct e1000_mbx_info *mbx = &hw->mbx;
  140. int countdown = mbx->timeout;
  141. if (!countdown || !mbx->ops.check_for_ack)
  142. goto out;
  143. while (countdown && mbx->ops.check_for_ack(hw, mbx_id)) {
  144. countdown--;
  145. if (!countdown)
  146. break;
  147. udelay(mbx->usec_delay);
  148. }
  149. /* if we failed, all future posted messages fail until reset */
  150. if (!countdown)
  151. mbx->timeout = 0;
  152. out:
  153. return countdown ? 0 : -E1000_ERR_MBX;
  154. }
  155. /**
  156. * igb_read_posted_mbx - Wait for message notification and receive message
  157. * @hw: pointer to the HW structure
  158. * @msg: The message buffer
  159. * @size: Length of buffer
  160. * @mbx_id: id of mailbox to write
  161. *
  162. * returns SUCCESS if it successfully received a message notification and
  163. * copied it into the receive buffer.
  164. **/
  165. static s32 igb_read_posted_mbx(struct e1000_hw *hw, u32 *msg, u16 size,
  166. u16 mbx_id)
  167. {
  168. struct e1000_mbx_info *mbx = &hw->mbx;
  169. s32 ret_val = -E1000_ERR_MBX;
  170. if (!mbx->ops.read)
  171. goto out;
  172. ret_val = igb_poll_for_msg(hw, mbx_id);
  173. if (!ret_val)
  174. ret_val = mbx->ops.read(hw, msg, size, mbx_id);
  175. out:
  176. return ret_val;
  177. }
  178. /**
  179. * igb_write_posted_mbx - Write a message to the mailbox, wait for ack
  180. * @hw: pointer to the HW structure
  181. * @msg: The message buffer
  182. * @size: Length of buffer
  183. * @mbx_id: id of mailbox to write
  184. *
  185. * returns SUCCESS if it successfully copied message into the buffer and
  186. * received an ack to that message within delay * timeout period
  187. **/
  188. static s32 igb_write_posted_mbx(struct e1000_hw *hw, u32 *msg, u16 size,
  189. u16 mbx_id)
  190. {
  191. struct e1000_mbx_info *mbx = &hw->mbx;
  192. s32 ret_val = -E1000_ERR_MBX;
  193. /* exit if either we can't write or there isn't a defined timeout */
  194. if (!mbx->ops.write || !mbx->timeout)
  195. goto out;
  196. /* send msg */
  197. ret_val = mbx->ops.write(hw, msg, size, mbx_id);
  198. /* if msg sent wait until we receive an ack */
  199. if (!ret_val)
  200. ret_val = igb_poll_for_ack(hw, mbx_id);
  201. out:
  202. return ret_val;
  203. }
  204. static s32 igb_check_for_bit_pf(struct e1000_hw *hw, u32 mask)
  205. {
  206. u32 mbvficr = rd32(E1000_MBVFICR);
  207. s32 ret_val = -E1000_ERR_MBX;
  208. if (mbvficr & mask) {
  209. ret_val = 0;
  210. wr32(E1000_MBVFICR, mask);
  211. }
  212. return ret_val;
  213. }
  214. /**
  215. * igb_check_for_msg_pf - checks to see if the VF has sent mail
  216. * @hw: pointer to the HW structure
  217. * @vf_number: the VF index
  218. *
  219. * returns SUCCESS if the VF has set the Status bit or else ERR_MBX
  220. **/
  221. static s32 igb_check_for_msg_pf(struct e1000_hw *hw, u16 vf_number)
  222. {
  223. s32 ret_val = -E1000_ERR_MBX;
  224. if (!igb_check_for_bit_pf(hw, E1000_MBVFICR_VFREQ_VF1 << vf_number)) {
  225. ret_val = 0;
  226. hw->mbx.stats.reqs++;
  227. }
  228. return ret_val;
  229. }
  230. /**
  231. * igb_check_for_ack_pf - checks to see if the VF has ACKed
  232. * @hw: pointer to the HW structure
  233. * @vf_number: the VF index
  234. *
  235. * returns SUCCESS if the VF has set the Status bit or else ERR_MBX
  236. **/
  237. static s32 igb_check_for_ack_pf(struct e1000_hw *hw, u16 vf_number)
  238. {
  239. s32 ret_val = -E1000_ERR_MBX;
  240. if (!igb_check_for_bit_pf(hw, E1000_MBVFICR_VFACK_VF1 << vf_number)) {
  241. ret_val = 0;
  242. hw->mbx.stats.acks++;
  243. }
  244. return ret_val;
  245. }
  246. /**
  247. * igb_check_for_rst_pf - checks to see if the VF has reset
  248. * @hw: pointer to the HW structure
  249. * @vf_number: the VF index
  250. *
  251. * returns SUCCESS if the VF has set the Status bit or else ERR_MBX
  252. **/
  253. static s32 igb_check_for_rst_pf(struct e1000_hw *hw, u16 vf_number)
  254. {
  255. u32 vflre = rd32(E1000_VFLRE);
  256. s32 ret_val = -E1000_ERR_MBX;
  257. if (vflre & (1 << vf_number)) {
  258. ret_val = 0;
  259. wr32(E1000_VFLRE, (1 << vf_number));
  260. hw->mbx.stats.rsts++;
  261. }
  262. return ret_val;
  263. }
  264. /**
  265. * igb_obtain_mbx_lock_pf - obtain mailbox lock
  266. * @hw: pointer to the HW structure
  267. * @vf_number: the VF index
  268. *
  269. * return SUCCESS if we obtained the mailbox lock
  270. **/
  271. static s32 igb_obtain_mbx_lock_pf(struct e1000_hw *hw, u16 vf_number)
  272. {
  273. s32 ret_val = -E1000_ERR_MBX;
  274. u32 p2v_mailbox;
  275. /* Take ownership of the buffer */
  276. wr32(E1000_P2VMAILBOX(vf_number), E1000_P2VMAILBOX_PFU);
  277. /* reserve mailbox for vf use */
  278. p2v_mailbox = rd32(E1000_P2VMAILBOX(vf_number));
  279. if (p2v_mailbox & E1000_P2VMAILBOX_PFU)
  280. ret_val = 0;
  281. return ret_val;
  282. }
  283. /**
  284. * igb_write_mbx_pf - Places a message in the mailbox
  285. * @hw: pointer to the HW structure
  286. * @msg: The message buffer
  287. * @size: Length of buffer
  288. * @vf_number: the VF index
  289. *
  290. * returns SUCCESS if it successfully copied message into the buffer
  291. **/
  292. static s32 igb_write_mbx_pf(struct e1000_hw *hw, u32 *msg, u16 size,
  293. u16 vf_number)
  294. {
  295. s32 ret_val;
  296. u16 i;
  297. /* lock the mailbox to prevent pf/vf race condition */
  298. ret_val = igb_obtain_mbx_lock_pf(hw, vf_number);
  299. if (ret_val)
  300. goto out_no_write;
  301. /* flush msg and acks as we are overwriting the message buffer */
  302. igb_check_for_msg_pf(hw, vf_number);
  303. igb_check_for_ack_pf(hw, vf_number);
  304. /* copy the caller specified message to the mailbox memory buffer */
  305. for (i = 0; i < size; i++)
  306. array_wr32(E1000_VMBMEM(vf_number), i, msg[i]);
  307. /* Interrupt VF to tell it a message has been sent and release buffer*/
  308. wr32(E1000_P2VMAILBOX(vf_number), E1000_P2VMAILBOX_STS);
  309. /* update stats */
  310. hw->mbx.stats.msgs_tx++;
  311. out_no_write:
  312. return ret_val;
  313. }
  314. /**
  315. * igb_read_mbx_pf - Read a message from the mailbox
  316. * @hw: pointer to the HW structure
  317. * @msg: The message buffer
  318. * @size: Length of buffer
  319. * @vf_number: the VF index
  320. *
  321. * This function copies a message from the mailbox buffer to the caller's
  322. * memory buffer. The presumption is that the caller knows that there was
  323. * a message due to a VF request so no polling for message is needed.
  324. **/
  325. static s32 igb_read_mbx_pf(struct e1000_hw *hw, u32 *msg, u16 size,
  326. u16 vf_number)
  327. {
  328. s32 ret_val;
  329. u16 i;
  330. /* lock the mailbox to prevent pf/vf race condition */
  331. ret_val = igb_obtain_mbx_lock_pf(hw, vf_number);
  332. if (ret_val)
  333. goto out_no_read;
  334. /* copy the message to the mailbox memory buffer */
  335. for (i = 0; i < size; i++)
  336. msg[i] = array_rd32(E1000_VMBMEM(vf_number), i);
  337. /* Acknowledge the message and release buffer */
  338. wr32(E1000_P2VMAILBOX(vf_number), E1000_P2VMAILBOX_ACK);
  339. /* update stats */
  340. hw->mbx.stats.msgs_rx++;
  341. out_no_read:
  342. return ret_val;
  343. }
  344. /**
  345. * e1000_init_mbx_params_pf - set initial values for pf mailbox
  346. * @hw: pointer to the HW structure
  347. *
  348. * Initializes the hw->mbx struct to correct values for pf mailbox
  349. */
  350. s32 igb_init_mbx_params_pf(struct e1000_hw *hw)
  351. {
  352. struct e1000_mbx_info *mbx = &hw->mbx;
  353. mbx->timeout = 0;
  354. mbx->usec_delay = 0;
  355. mbx->size = E1000_VFMAILBOX_SIZE;
  356. mbx->ops.read = igb_read_mbx_pf;
  357. mbx->ops.write = igb_write_mbx_pf;
  358. mbx->ops.read_posted = igb_read_posted_mbx;
  359. mbx->ops.write_posted = igb_write_posted_mbx;
  360. mbx->ops.check_for_msg = igb_check_for_msg_pf;
  361. mbx->ops.check_for_ack = igb_check_for_ack_pf;
  362. mbx->ops.check_for_rst = igb_check_for_rst_pf;
  363. mbx->stats.msgs_tx = 0;
  364. mbx->stats.msgs_rx = 0;
  365. mbx->stats.reqs = 0;
  366. mbx->stats.acks = 0;
  367. mbx->stats.rsts = 0;
  368. return 0;
  369. }