e1000_mbx.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. /*******************************************************************************
  2. Intel(R) Gigabit Ethernet Linux driver
  3. Copyright(c) 2007-2009 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 successfuly 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 (mbx->ops.check_for_msg(hw, mbx_id)) {
  119. countdown--;
  120. if (!countdown)
  121. break;
  122. udelay(mbx->usec_delay);
  123. }
  124. out:
  125. return countdown ? 0 : -E1000_ERR_MBX;
  126. }
  127. /**
  128. * igb_poll_for_ack - Wait for message acknowledgement
  129. * @hw: pointer to the HW structure
  130. * @mbx_id: id of mailbox to write
  131. *
  132. * returns SUCCESS if it successfully received a message acknowledgement
  133. **/
  134. static s32 igb_poll_for_ack(struct e1000_hw *hw, u16 mbx_id)
  135. {
  136. struct e1000_mbx_info *mbx = &hw->mbx;
  137. int countdown = mbx->timeout;
  138. if (!countdown || !mbx->ops.check_for_ack)
  139. goto out;
  140. while (mbx->ops.check_for_ack(hw, mbx_id)) {
  141. countdown--;
  142. if (!countdown)
  143. break;
  144. udelay(mbx->usec_delay);
  145. }
  146. out:
  147. return countdown ? 0 : -E1000_ERR_MBX;
  148. }
  149. /**
  150. * igb_read_posted_mbx - Wait for message notification and receive message
  151. * @hw: pointer to the HW structure
  152. * @msg: The message buffer
  153. * @size: Length of buffer
  154. * @mbx_id: id of mailbox to write
  155. *
  156. * returns SUCCESS if it successfully received a message notification and
  157. * copied it into the receive buffer.
  158. **/
  159. static s32 igb_read_posted_mbx(struct e1000_hw *hw, u32 *msg, u16 size, u16 mbx_id)
  160. {
  161. struct e1000_mbx_info *mbx = &hw->mbx;
  162. s32 ret_val = -E1000_ERR_MBX;
  163. if (!mbx->ops.read)
  164. goto out;
  165. ret_val = igb_poll_for_msg(hw, mbx_id);
  166. if (!ret_val)
  167. ret_val = mbx->ops.read(hw, msg, size, mbx_id);
  168. out:
  169. return ret_val;
  170. }
  171. /**
  172. * igb_write_posted_mbx - Write a message to the mailbox, wait for ack
  173. * @hw: pointer to the HW structure
  174. * @msg: The message buffer
  175. * @size: Length of buffer
  176. * @mbx_id: id of mailbox to write
  177. *
  178. * returns SUCCESS if it successfully copied message into the buffer and
  179. * received an ack to that message within delay * timeout period
  180. **/
  181. static s32 igb_write_posted_mbx(struct e1000_hw *hw, u32 *msg, u16 size, u16 mbx_id)
  182. {
  183. struct e1000_mbx_info *mbx = &hw->mbx;
  184. s32 ret_val = 0;
  185. if (!mbx->ops.write)
  186. goto out;
  187. /* send msg*/
  188. ret_val = mbx->ops.write(hw, msg, size, mbx_id);
  189. /* if msg sent wait until we receive an ack */
  190. if (!ret_val)
  191. ret_val = igb_poll_for_ack(hw, mbx_id);
  192. out:
  193. return ret_val;
  194. }
  195. static s32 igb_check_for_bit_pf(struct e1000_hw *hw, u32 mask)
  196. {
  197. u32 mbvficr = rd32(E1000_MBVFICR);
  198. s32 ret_val = -E1000_ERR_MBX;
  199. if (mbvficr & mask) {
  200. ret_val = 0;
  201. wr32(E1000_MBVFICR, mask);
  202. }
  203. return ret_val;
  204. }
  205. /**
  206. * igb_check_for_msg_pf - checks to see if the VF has sent mail
  207. * @hw: pointer to the HW structure
  208. * @vf_number: the VF index
  209. *
  210. * returns SUCCESS if the VF has set the Status bit or else ERR_MBX
  211. **/
  212. static s32 igb_check_for_msg_pf(struct e1000_hw *hw, u16 vf_number)
  213. {
  214. s32 ret_val = -E1000_ERR_MBX;
  215. if (!igb_check_for_bit_pf(hw, E1000_MBVFICR_VFREQ_VF1 << vf_number)) {
  216. ret_val = 0;
  217. hw->mbx.stats.reqs++;
  218. }
  219. return ret_val;
  220. }
  221. /**
  222. * igb_check_for_ack_pf - checks to see if the VF has ACKed
  223. * @hw: pointer to the HW structure
  224. * @vf_number: the VF index
  225. *
  226. * returns SUCCESS if the VF has set the Status bit or else ERR_MBX
  227. **/
  228. static s32 igb_check_for_ack_pf(struct e1000_hw *hw, u16 vf_number)
  229. {
  230. s32 ret_val = -E1000_ERR_MBX;
  231. if (!igb_check_for_bit_pf(hw, E1000_MBVFICR_VFACK_VF1 << vf_number)) {
  232. ret_val = 0;
  233. hw->mbx.stats.acks++;
  234. }
  235. return ret_val;
  236. }
  237. /**
  238. * igb_check_for_rst_pf - checks to see if the VF has reset
  239. * @hw: pointer to the HW structure
  240. * @vf_number: the VF index
  241. *
  242. * returns SUCCESS if the VF has set the Status bit or else ERR_MBX
  243. **/
  244. static s32 igb_check_for_rst_pf(struct e1000_hw *hw, u16 vf_number)
  245. {
  246. u32 vflre = rd32(E1000_VFLRE);
  247. s32 ret_val = -E1000_ERR_MBX;
  248. if (vflre & (1 << vf_number)) {
  249. ret_val = 0;
  250. wr32(E1000_VFLRE, (1 << vf_number));
  251. hw->mbx.stats.rsts++;
  252. }
  253. return ret_val;
  254. }
  255. /**
  256. * igb_write_mbx_pf - Places a message in the mailbox
  257. * @hw: pointer to the HW structure
  258. * @msg: The message buffer
  259. * @size: Length of buffer
  260. * @vf_number: the VF index
  261. *
  262. * returns SUCCESS if it successfully copied message into the buffer
  263. **/
  264. static s32 igb_write_mbx_pf(struct e1000_hw *hw, u32 *msg, u16 size,
  265. u16 vf_number)
  266. {
  267. u32 p2v_mailbox;
  268. s32 ret_val = 0;
  269. u16 i;
  270. /* Take ownership of the buffer */
  271. wr32(E1000_P2VMAILBOX(vf_number), E1000_P2VMAILBOX_PFU);
  272. /* Make sure we have ownership now... */
  273. p2v_mailbox = rd32(E1000_P2VMAILBOX(vf_number));
  274. if (!(p2v_mailbox & E1000_P2VMAILBOX_PFU)) {
  275. /* failed to grab ownership */
  276. ret_val = -E1000_ERR_MBX;
  277. goto out_no_write;
  278. }
  279. /*
  280. * flush any ack or msg which may already be in the queue
  281. * as they are likely the result of an error
  282. */
  283. igb_check_for_ack_pf(hw, vf_number);
  284. igb_check_for_msg_pf(hw, vf_number);
  285. /* copy the caller specified message to the mailbox memory buffer */
  286. for (i = 0; i < size; i++)
  287. array_wr32(E1000_VMBMEM(vf_number), i, msg[i]);
  288. /* Interrupt VF to tell it a message has been sent and release buffer*/
  289. wr32(E1000_P2VMAILBOX(vf_number), E1000_P2VMAILBOX_STS);
  290. /* update stats */
  291. hw->mbx.stats.msgs_tx++;
  292. out_no_write:
  293. return ret_val;
  294. }
  295. /**
  296. * igb_read_mbx_pf - Read a message from the mailbox
  297. * @hw: pointer to the HW structure
  298. * @msg: The message buffer
  299. * @size: Length of buffer
  300. * @vf_number: the VF index
  301. *
  302. * This function copies a message from the mailbox buffer to the caller's
  303. * memory buffer. The presumption is that the caller knows that there was
  304. * a message due to a VF request so no polling for message is needed.
  305. **/
  306. static s32 igb_read_mbx_pf(struct e1000_hw *hw, u32 *msg, u16 size,
  307. u16 vf_number)
  308. {
  309. u32 p2v_mailbox;
  310. s32 ret_val = 0;
  311. u16 i;
  312. /* Take ownership of the buffer */
  313. wr32(E1000_P2VMAILBOX(vf_number), E1000_P2VMAILBOX_PFU);
  314. /* Make sure we have ownership now... */
  315. p2v_mailbox = rd32(E1000_P2VMAILBOX(vf_number));
  316. if (!(p2v_mailbox & E1000_P2VMAILBOX_PFU)) {
  317. /* failed to grab ownership */
  318. ret_val = -E1000_ERR_MBX;
  319. goto out_no_read;
  320. }
  321. /* copy the message to the mailbox memory buffer */
  322. for (i = 0; i < size; i++)
  323. msg[i] = array_rd32(E1000_VMBMEM(vf_number), i);
  324. /* Acknowledge the message and release buffer */
  325. wr32(E1000_P2VMAILBOX(vf_number), E1000_P2VMAILBOX_ACK);
  326. /* update stats */
  327. hw->mbx.stats.msgs_rx++;
  328. ret_val = 0;
  329. out_no_read:
  330. return ret_val;
  331. }
  332. /**
  333. * e1000_init_mbx_params_pf - set initial values for pf mailbox
  334. * @hw: pointer to the HW structure
  335. *
  336. * Initializes the hw->mbx struct to correct values for pf mailbox
  337. */
  338. s32 igb_init_mbx_params_pf(struct e1000_hw *hw)
  339. {
  340. struct e1000_mbx_info *mbx = &hw->mbx;
  341. if (hw->mac.type == e1000_82576) {
  342. mbx->timeout = 0;
  343. mbx->usec_delay = 0;
  344. mbx->size = E1000_VFMAILBOX_SIZE;
  345. mbx->ops.read = igb_read_mbx_pf;
  346. mbx->ops.write = igb_write_mbx_pf;
  347. mbx->ops.read_posted = igb_read_posted_mbx;
  348. mbx->ops.write_posted = igb_write_posted_mbx;
  349. mbx->ops.check_for_msg = igb_check_for_msg_pf;
  350. mbx->ops.check_for_ack = igb_check_for_ack_pf;
  351. mbx->ops.check_for_rst = igb_check_for_rst_pf;
  352. mbx->stats.msgs_tx = 0;
  353. mbx->stats.msgs_rx = 0;
  354. mbx->stats.reqs = 0;
  355. mbx->stats.acks = 0;
  356. mbx->stats.rsts = 0;
  357. }
  358. return 0;
  359. }