e1000_mbx.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  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 (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, u16 mbx_id)
  166. {
  167. struct e1000_mbx_info *mbx = &hw->mbx;
  168. s32 ret_val = -E1000_ERR_MBX;
  169. if (!mbx->ops.read)
  170. goto out;
  171. ret_val = igb_poll_for_msg(hw, mbx_id);
  172. if (!ret_val)
  173. ret_val = mbx->ops.read(hw, msg, size, mbx_id);
  174. out:
  175. return ret_val;
  176. }
  177. /**
  178. * igb_write_posted_mbx - Write a message to the mailbox, wait for ack
  179. * @hw: pointer to the HW structure
  180. * @msg: The message buffer
  181. * @size: Length of buffer
  182. * @mbx_id: id of mailbox to write
  183. *
  184. * returns SUCCESS if it successfully copied message into the buffer and
  185. * received an ack to that message within delay * timeout period
  186. **/
  187. static s32 igb_write_posted_mbx(struct e1000_hw *hw, u32 *msg, u16 size, u16 mbx_id)
  188. {
  189. struct e1000_mbx_info *mbx = &hw->mbx;
  190. s32 ret_val = -E1000_ERR_MBX;
  191. /* exit if either we can't write or there isn't a defined timeout */
  192. if (!mbx->ops.write || !mbx->timeout)
  193. goto out;
  194. /* send msg */
  195. ret_val = mbx->ops.write(hw, msg, size, mbx_id);
  196. /* if msg sent wait until we receive an ack */
  197. if (!ret_val)
  198. ret_val = igb_poll_for_ack(hw, mbx_id);
  199. out:
  200. return ret_val;
  201. }
  202. static s32 igb_check_for_bit_pf(struct e1000_hw *hw, u32 mask)
  203. {
  204. u32 mbvficr = rd32(E1000_MBVFICR);
  205. s32 ret_val = -E1000_ERR_MBX;
  206. if (mbvficr & mask) {
  207. ret_val = 0;
  208. wr32(E1000_MBVFICR, mask);
  209. }
  210. return ret_val;
  211. }
  212. /**
  213. * igb_check_for_msg_pf - checks to see if the VF has sent mail
  214. * @hw: pointer to the HW structure
  215. * @vf_number: the VF index
  216. *
  217. * returns SUCCESS if the VF has set the Status bit or else ERR_MBX
  218. **/
  219. static s32 igb_check_for_msg_pf(struct e1000_hw *hw, u16 vf_number)
  220. {
  221. s32 ret_val = -E1000_ERR_MBX;
  222. if (!igb_check_for_bit_pf(hw, E1000_MBVFICR_VFREQ_VF1 << vf_number)) {
  223. ret_val = 0;
  224. hw->mbx.stats.reqs++;
  225. }
  226. return ret_val;
  227. }
  228. /**
  229. * igb_check_for_ack_pf - checks to see if the VF has ACKed
  230. * @hw: pointer to the HW structure
  231. * @vf_number: the VF index
  232. *
  233. * returns SUCCESS if the VF has set the Status bit or else ERR_MBX
  234. **/
  235. static s32 igb_check_for_ack_pf(struct e1000_hw *hw, u16 vf_number)
  236. {
  237. s32 ret_val = -E1000_ERR_MBX;
  238. if (!igb_check_for_bit_pf(hw, E1000_MBVFICR_VFACK_VF1 << vf_number)) {
  239. ret_val = 0;
  240. hw->mbx.stats.acks++;
  241. }
  242. return ret_val;
  243. }
  244. /**
  245. * igb_check_for_rst_pf - checks to see if the VF has reset
  246. * @hw: pointer to the HW structure
  247. * @vf_number: the VF index
  248. *
  249. * returns SUCCESS if the VF has set the Status bit or else ERR_MBX
  250. **/
  251. static s32 igb_check_for_rst_pf(struct e1000_hw *hw, u16 vf_number)
  252. {
  253. u32 vflre = rd32(E1000_VFLRE);
  254. s32 ret_val = -E1000_ERR_MBX;
  255. if (vflre & (1 << vf_number)) {
  256. ret_val = 0;
  257. wr32(E1000_VFLRE, (1 << vf_number));
  258. hw->mbx.stats.rsts++;
  259. }
  260. return ret_val;
  261. }
  262. /**
  263. * igb_obtain_mbx_lock_pf - obtain mailbox lock
  264. * @hw: pointer to the HW structure
  265. * @vf_number: the VF index
  266. *
  267. * return SUCCESS if we obtained the mailbox lock
  268. **/
  269. static s32 igb_obtain_mbx_lock_pf(struct e1000_hw *hw, u16 vf_number)
  270. {
  271. s32 ret_val = -E1000_ERR_MBX;
  272. u32 p2v_mailbox;
  273. /* Take ownership of the buffer */
  274. wr32(E1000_P2VMAILBOX(vf_number), E1000_P2VMAILBOX_PFU);
  275. /* reserve mailbox for vf use */
  276. p2v_mailbox = rd32(E1000_P2VMAILBOX(vf_number));
  277. if (p2v_mailbox & E1000_P2VMAILBOX_PFU)
  278. ret_val = 0;
  279. return ret_val;
  280. }
  281. /**
  282. * igb_write_mbx_pf - Places a message in the mailbox
  283. * @hw: pointer to the HW structure
  284. * @msg: The message buffer
  285. * @size: Length of buffer
  286. * @vf_number: the VF index
  287. *
  288. * returns SUCCESS if it successfully copied message into the buffer
  289. **/
  290. static s32 igb_write_mbx_pf(struct e1000_hw *hw, u32 *msg, u16 size,
  291. u16 vf_number)
  292. {
  293. s32 ret_val;
  294. u16 i;
  295. /* lock the mailbox to prevent pf/vf race condition */
  296. ret_val = igb_obtain_mbx_lock_pf(hw, vf_number);
  297. if (ret_val)
  298. goto out_no_write;
  299. /* flush msg and acks as we are overwriting the message buffer */
  300. igb_check_for_msg_pf(hw, vf_number);
  301. igb_check_for_ack_pf(hw, vf_number);
  302. /* copy the caller specified message to the mailbox memory buffer */
  303. for (i = 0; i < size; i++)
  304. array_wr32(E1000_VMBMEM(vf_number), i, msg[i]);
  305. /* Interrupt VF to tell it a message has been sent and release buffer*/
  306. wr32(E1000_P2VMAILBOX(vf_number), E1000_P2VMAILBOX_STS);
  307. /* update stats */
  308. hw->mbx.stats.msgs_tx++;
  309. out_no_write:
  310. return ret_val;
  311. }
  312. /**
  313. * igb_read_mbx_pf - Read a message from the mailbox
  314. * @hw: pointer to the HW structure
  315. * @msg: The message buffer
  316. * @size: Length of buffer
  317. * @vf_number: the VF index
  318. *
  319. * This function copies a message from the mailbox buffer to the caller's
  320. * memory buffer. The presumption is that the caller knows that there was
  321. * a message due to a VF request so no polling for message is needed.
  322. **/
  323. static s32 igb_read_mbx_pf(struct e1000_hw *hw, u32 *msg, u16 size,
  324. u16 vf_number)
  325. {
  326. s32 ret_val;
  327. u16 i;
  328. /* lock the mailbox to prevent pf/vf race condition */
  329. ret_val = igb_obtain_mbx_lock_pf(hw, vf_number);
  330. if (ret_val)
  331. goto out_no_read;
  332. /* copy the message to the mailbox memory buffer */
  333. for (i = 0; i < size; i++)
  334. msg[i] = array_rd32(E1000_VMBMEM(vf_number), i);
  335. /* Acknowledge the message and release buffer */
  336. wr32(E1000_P2VMAILBOX(vf_number), E1000_P2VMAILBOX_ACK);
  337. /* update stats */
  338. hw->mbx.stats.msgs_rx++;
  339. out_no_read:
  340. return ret_val;
  341. }
  342. /**
  343. * e1000_init_mbx_params_pf - set initial values for pf mailbox
  344. * @hw: pointer to the HW structure
  345. *
  346. * Initializes the hw->mbx struct to correct values for pf mailbox
  347. */
  348. s32 igb_init_mbx_params_pf(struct e1000_hw *hw)
  349. {
  350. struct e1000_mbx_info *mbx = &hw->mbx;
  351. if (hw->mac.type == e1000_82576) {
  352. mbx->timeout = 0;
  353. mbx->usec_delay = 0;
  354. mbx->size = E1000_VFMAILBOX_SIZE;
  355. mbx->ops.read = igb_read_mbx_pf;
  356. mbx->ops.write = igb_write_mbx_pf;
  357. mbx->ops.read_posted = igb_read_posted_mbx;
  358. mbx->ops.write_posted = igb_write_posted_mbx;
  359. mbx->ops.check_for_msg = igb_check_for_msg_pf;
  360. mbx->ops.check_for_ack = igb_check_for_ack_pf;
  361. mbx->ops.check_for_rst = igb_check_for_rst_pf;
  362. mbx->stats.msgs_tx = 0;
  363. mbx->stats.msgs_rx = 0;
  364. mbx->stats.reqs = 0;
  365. mbx->stats.acks = 0;
  366. mbx->stats.rsts = 0;
  367. }
  368. return 0;
  369. }