e1000_mbx.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  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 (!mbx->ops.check_for_msg)
  117. goto out;
  118. while (mbx->ops.check_for_msg(hw, mbx_id)) {
  119. if (!countdown)
  120. break;
  121. countdown--;
  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 (!mbx->ops.check_for_ack)
  139. goto out;
  140. while (mbx->ops.check_for_ack(hw, mbx_id)) {
  141. if (!countdown)
  142. break;
  143. countdown--;
  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. 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. 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. /**
  196. * e1000_init_mbx_ops_generic - Initialize NVM function pointers
  197. * @hw: pointer to the HW structure
  198. *
  199. * Setups up the function pointers to no-op functions
  200. **/
  201. void e1000_init_mbx_ops_generic(struct e1000_hw *hw)
  202. {
  203. struct e1000_mbx_info *mbx = &hw->mbx;
  204. mbx->ops.read_posted = igb_read_posted_mbx;
  205. mbx->ops.write_posted = igb_write_posted_mbx;
  206. }
  207. static s32 igb_check_for_bit_pf(struct e1000_hw *hw, u32 mask)
  208. {
  209. u32 mbvficr = rd32(E1000_MBVFICR);
  210. s32 ret_val = -E1000_ERR_MBX;
  211. if (mbvficr & mask) {
  212. ret_val = 0;
  213. wr32(E1000_MBVFICR, mask);
  214. }
  215. return ret_val;
  216. }
  217. /**
  218. * igb_check_for_msg_pf - checks to see if the VF has sent mail
  219. * @hw: pointer to the HW structure
  220. * @vf_number: the VF index
  221. *
  222. * returns SUCCESS if the VF has set the Status bit or else ERR_MBX
  223. **/
  224. static s32 igb_check_for_msg_pf(struct e1000_hw *hw, u16 vf_number)
  225. {
  226. s32 ret_val = -E1000_ERR_MBX;
  227. if (!igb_check_for_bit_pf(hw, E1000_MBVFICR_VFREQ_VF1 << vf_number)) {
  228. ret_val = 0;
  229. hw->mbx.stats.reqs++;
  230. }
  231. return ret_val;
  232. }
  233. /**
  234. * igb_check_for_ack_pf - checks to see if the VF has ACKed
  235. * @hw: pointer to the HW structure
  236. * @vf_number: the VF index
  237. *
  238. * returns SUCCESS if the VF has set the Status bit or else ERR_MBX
  239. **/
  240. static s32 igb_check_for_ack_pf(struct e1000_hw *hw, u16 vf_number)
  241. {
  242. s32 ret_val = -E1000_ERR_MBX;
  243. if (!igb_check_for_bit_pf(hw, E1000_MBVFICR_VFACK_VF1 << vf_number)) {
  244. ret_val = 0;
  245. hw->mbx.stats.acks++;
  246. }
  247. return ret_val;
  248. }
  249. /**
  250. * igb_check_for_rst_pf - checks to see if the VF has reset
  251. * @hw: pointer to the HW structure
  252. * @vf_number: the VF index
  253. *
  254. * returns SUCCESS if the VF has set the Status bit or else ERR_MBX
  255. **/
  256. static s32 igb_check_for_rst_pf(struct e1000_hw *hw, u16 vf_number)
  257. {
  258. u32 vflre = rd32(E1000_VFLRE);
  259. s32 ret_val = -E1000_ERR_MBX;
  260. if (vflre & (1 << vf_number)) {
  261. ret_val = 0;
  262. wr32(E1000_VFLRE, (1 << vf_number));
  263. hw->mbx.stats.rsts++;
  264. }
  265. return ret_val;
  266. }
  267. /**
  268. * igb_write_mbx_pf - Places a message in the mailbox
  269. * @hw: pointer to the HW structure
  270. * @msg: The message buffer
  271. * @size: Length of buffer
  272. * @vf_number: the VF index
  273. *
  274. * returns SUCCESS if it successfully copied message into the buffer
  275. **/
  276. static s32 igb_write_mbx_pf(struct e1000_hw *hw, u32 *msg, u16 size,
  277. u16 vf_number)
  278. {
  279. u32 p2v_mailbox;
  280. s32 ret_val = 0;
  281. u16 i;
  282. /* Take ownership of the buffer */
  283. wr32(E1000_P2VMAILBOX(vf_number), E1000_P2VMAILBOX_PFU);
  284. /* Make sure we have ownership now... */
  285. p2v_mailbox = rd32(E1000_P2VMAILBOX(vf_number));
  286. if (!(p2v_mailbox & E1000_P2VMAILBOX_PFU)) {
  287. /* failed to grab ownership */
  288. ret_val = -E1000_ERR_MBX;
  289. goto out_no_write;
  290. }
  291. /*
  292. * flush any ack or msg which may already be in the queue
  293. * as they are likely the result of an error
  294. */
  295. igb_check_for_ack_pf(hw, vf_number);
  296. igb_check_for_msg_pf(hw, vf_number);
  297. /* copy the caller specified message to the mailbox memory buffer */
  298. for (i = 0; i < size; i++)
  299. array_wr32(E1000_VMBMEM(vf_number), i, msg[i]);
  300. /* Interrupt VF to tell it a message has been sent and release buffer*/
  301. wr32(E1000_P2VMAILBOX(vf_number), E1000_P2VMAILBOX_STS);
  302. /* update stats */
  303. hw->mbx.stats.msgs_tx++;
  304. out_no_write:
  305. return ret_val;
  306. }
  307. /**
  308. * igb_read_mbx_pf - Read a message from the mailbox
  309. * @hw: pointer to the HW structure
  310. * @msg: The message buffer
  311. * @size: Length of buffer
  312. * @vf_number: the VF index
  313. *
  314. * This function copies a message from the mailbox buffer to the caller's
  315. * memory buffer. The presumption is that the caller knows that there was
  316. * a message due to a VF request so no polling for message is needed.
  317. **/
  318. static s32 igb_read_mbx_pf(struct e1000_hw *hw, u32 *msg, u16 size,
  319. u16 vf_number)
  320. {
  321. u32 p2v_mailbox;
  322. s32 ret_val = 0;
  323. u16 i;
  324. /* Take ownership of the buffer */
  325. wr32(E1000_P2VMAILBOX(vf_number), E1000_P2VMAILBOX_PFU);
  326. /* Make sure we have ownership now... */
  327. p2v_mailbox = rd32(E1000_P2VMAILBOX(vf_number));
  328. if (!(p2v_mailbox & E1000_P2VMAILBOX_PFU)) {
  329. /* failed to grab ownership */
  330. ret_val = -E1000_ERR_MBX;
  331. goto out_no_read;
  332. }
  333. /* copy the message to the mailbox memory buffer */
  334. for (i = 0; i < size; i++)
  335. msg[i] = array_rd32(E1000_VMBMEM(vf_number), i);
  336. /* Acknowledge the message and release buffer */
  337. wr32(E1000_P2VMAILBOX(vf_number), E1000_P2VMAILBOX_ACK);
  338. /* update stats */
  339. hw->mbx.stats.msgs_rx++;
  340. ret_val = 0;
  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. if (hw->mac.type == e1000_82576) {
  354. mbx->timeout = 0;
  355. mbx->usec_delay = 0;
  356. mbx->size = E1000_VFMAILBOX_SIZE;
  357. mbx->ops.read = igb_read_mbx_pf;
  358. mbx->ops.write = igb_write_mbx_pf;
  359. mbx->ops.read_posted = igb_read_posted_mbx;
  360. mbx->ops.write_posted = igb_write_posted_mbx;
  361. mbx->ops.check_for_msg = igb_check_for_msg_pf;
  362. mbx->ops.check_for_ack = igb_check_for_ack_pf;
  363. mbx->ops.check_for_rst = igb_check_for_rst_pf;
  364. mbx->stats.msgs_tx = 0;
  365. mbx->stats.msgs_rx = 0;
  366. mbx->stats.reqs = 0;
  367. mbx->stats.acks = 0;
  368. mbx->stats.rsts = 0;
  369. }
  370. return 0;
  371. }