e1000_mbx.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. /* Intel(R) Gigabit Ethernet Linux driver
  2. * Copyright(c) 2007-2014 Intel Corporation.
  3. *
  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. *
  8. * This program is distributed in the hope it will be useful, but WITHOUT
  9. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. * more details.
  12. *
  13. * You should have received a copy of the GNU General Public License along with
  14. * this program; if not, see <http://www.gnu.org/licenses/>.
  15. *
  16. * The full GNU General Public License is included in this distribution in
  17. * the file called "COPYING".
  18. *
  19. * Contact Information:
  20. * e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
  21. * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  22. */
  23. #include "e1000_mbx.h"
  24. /**
  25. * igb_read_mbx - Reads a message from the mailbox
  26. * @hw: pointer to the HW structure
  27. * @msg: The message buffer
  28. * @size: Length of buffer
  29. * @mbx_id: id of mailbox to read
  30. *
  31. * returns SUCCESS if it successfully read message from buffer
  32. **/
  33. s32 igb_read_mbx(struct e1000_hw *hw, u32 *msg, u16 size, u16 mbx_id)
  34. {
  35. struct e1000_mbx_info *mbx = &hw->mbx;
  36. s32 ret_val = -E1000_ERR_MBX;
  37. /* limit read to size of mailbox */
  38. if (size > mbx->size)
  39. size = mbx->size;
  40. if (mbx->ops.read)
  41. ret_val = mbx->ops.read(hw, msg, size, mbx_id);
  42. return ret_val;
  43. }
  44. /**
  45. * igb_write_mbx - Write a message to the mailbox
  46. * @hw: pointer to the HW structure
  47. * @msg: The message buffer
  48. * @size: Length of buffer
  49. * @mbx_id: id of mailbox to write
  50. *
  51. * returns SUCCESS if it successfully copied message into the buffer
  52. **/
  53. s32 igb_write_mbx(struct e1000_hw *hw, u32 *msg, u16 size, u16 mbx_id)
  54. {
  55. struct e1000_mbx_info *mbx = &hw->mbx;
  56. s32 ret_val = 0;
  57. if (size > mbx->size)
  58. ret_val = -E1000_ERR_MBX;
  59. else if (mbx->ops.write)
  60. ret_val = mbx->ops.write(hw, msg, size, mbx_id);
  61. return ret_val;
  62. }
  63. /**
  64. * igb_check_for_msg - checks to see if someone sent us mail
  65. * @hw: pointer to the HW structure
  66. * @mbx_id: id of mailbox to check
  67. *
  68. * returns SUCCESS if the Status bit was found or else ERR_MBX
  69. **/
  70. s32 igb_check_for_msg(struct e1000_hw *hw, u16 mbx_id)
  71. {
  72. struct e1000_mbx_info *mbx = &hw->mbx;
  73. s32 ret_val = -E1000_ERR_MBX;
  74. if (mbx->ops.check_for_msg)
  75. ret_val = mbx->ops.check_for_msg(hw, mbx_id);
  76. return ret_val;
  77. }
  78. /**
  79. * igb_check_for_ack - checks to see if someone sent us ACK
  80. * @hw: pointer to the HW structure
  81. * @mbx_id: id of mailbox to check
  82. *
  83. * returns SUCCESS if the Status bit was found or else ERR_MBX
  84. **/
  85. s32 igb_check_for_ack(struct e1000_hw *hw, u16 mbx_id)
  86. {
  87. struct e1000_mbx_info *mbx = &hw->mbx;
  88. s32 ret_val = -E1000_ERR_MBX;
  89. if (mbx->ops.check_for_ack)
  90. ret_val = mbx->ops.check_for_ack(hw, mbx_id);
  91. return ret_val;
  92. }
  93. /**
  94. * igb_check_for_rst - checks to see if other side has reset
  95. * @hw: pointer to the HW structure
  96. * @mbx_id: id of mailbox to check
  97. *
  98. * returns SUCCESS if the Status bit was found or else ERR_MBX
  99. **/
  100. s32 igb_check_for_rst(struct e1000_hw *hw, u16 mbx_id)
  101. {
  102. struct e1000_mbx_info *mbx = &hw->mbx;
  103. s32 ret_val = -E1000_ERR_MBX;
  104. if (mbx->ops.check_for_rst)
  105. ret_val = mbx->ops.check_for_rst(hw, mbx_id);
  106. return ret_val;
  107. }
  108. /**
  109. * igb_poll_for_msg - Wait for message notification
  110. * @hw: pointer to the HW structure
  111. * @mbx_id: id of mailbox to write
  112. *
  113. * returns SUCCESS if it successfully received a message notification
  114. **/
  115. static s32 igb_poll_for_msg(struct e1000_hw *hw, u16 mbx_id)
  116. {
  117. struct e1000_mbx_info *mbx = &hw->mbx;
  118. int countdown = mbx->timeout;
  119. if (!countdown || !mbx->ops.check_for_msg)
  120. goto out;
  121. while (countdown && mbx->ops.check_for_msg(hw, mbx_id)) {
  122. countdown--;
  123. if (!countdown)
  124. break;
  125. udelay(mbx->usec_delay);
  126. }
  127. /* if we failed, all future posted messages fail until reset */
  128. if (!countdown)
  129. mbx->timeout = 0;
  130. out:
  131. return countdown ? 0 : -E1000_ERR_MBX;
  132. }
  133. /**
  134. * igb_poll_for_ack - Wait for message acknowledgement
  135. * @hw: pointer to the HW structure
  136. * @mbx_id: id of mailbox to write
  137. *
  138. * returns SUCCESS if it successfully received a message acknowledgement
  139. **/
  140. static s32 igb_poll_for_ack(struct e1000_hw *hw, u16 mbx_id)
  141. {
  142. struct e1000_mbx_info *mbx = &hw->mbx;
  143. int countdown = mbx->timeout;
  144. if (!countdown || !mbx->ops.check_for_ack)
  145. goto out;
  146. while (countdown && mbx->ops.check_for_ack(hw, mbx_id)) {
  147. countdown--;
  148. if (!countdown)
  149. break;
  150. udelay(mbx->usec_delay);
  151. }
  152. /* if we failed, all future posted messages fail until reset */
  153. if (!countdown)
  154. mbx->timeout = 0;
  155. out:
  156. return countdown ? 0 : -E1000_ERR_MBX;
  157. }
  158. /**
  159. * igb_read_posted_mbx - Wait for message notification and receive message
  160. * @hw: pointer to the HW structure
  161. * @msg: The message buffer
  162. * @size: Length of buffer
  163. * @mbx_id: id of mailbox to write
  164. *
  165. * returns SUCCESS if it successfully received a message notification and
  166. * copied it into the receive buffer.
  167. **/
  168. static s32 igb_read_posted_mbx(struct e1000_hw *hw, u32 *msg, u16 size,
  169. u16 mbx_id)
  170. {
  171. struct e1000_mbx_info *mbx = &hw->mbx;
  172. s32 ret_val = -E1000_ERR_MBX;
  173. if (!mbx->ops.read)
  174. goto out;
  175. ret_val = igb_poll_for_msg(hw, mbx_id);
  176. if (!ret_val)
  177. ret_val = mbx->ops.read(hw, msg, size, mbx_id);
  178. out:
  179. return ret_val;
  180. }
  181. /**
  182. * igb_write_posted_mbx - Write a message to the mailbox, wait for ack
  183. * @hw: pointer to the HW structure
  184. * @msg: The message buffer
  185. * @size: Length of buffer
  186. * @mbx_id: id of mailbox to write
  187. *
  188. * returns SUCCESS if it successfully copied message into the buffer and
  189. * received an ack to that message within delay * timeout period
  190. **/
  191. static s32 igb_write_posted_mbx(struct e1000_hw *hw, u32 *msg, u16 size,
  192. u16 mbx_id)
  193. {
  194. struct e1000_mbx_info *mbx = &hw->mbx;
  195. s32 ret_val = -E1000_ERR_MBX;
  196. /* exit if either we can't write or there isn't a defined timeout */
  197. if (!mbx->ops.write || !mbx->timeout)
  198. goto out;
  199. /* send msg */
  200. ret_val = mbx->ops.write(hw, msg, size, mbx_id);
  201. /* if msg sent wait until we receive an ack */
  202. if (!ret_val)
  203. ret_val = igb_poll_for_ack(hw, mbx_id);
  204. out:
  205. return ret_val;
  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 & BIT(vf_number)) {
  261. ret_val = 0;
  262. wr32(E1000_VFLRE, BIT(vf_number));
  263. hw->mbx.stats.rsts++;
  264. }
  265. return ret_val;
  266. }
  267. /**
  268. * igb_obtain_mbx_lock_pf - obtain mailbox lock
  269. * @hw: pointer to the HW structure
  270. * @vf_number: the VF index
  271. *
  272. * return SUCCESS if we obtained the mailbox lock
  273. **/
  274. static s32 igb_obtain_mbx_lock_pf(struct e1000_hw *hw, u16 vf_number)
  275. {
  276. s32 ret_val = -E1000_ERR_MBX;
  277. u32 p2v_mailbox;
  278. int count = 10;
  279. do {
  280. /* Take ownership of the buffer */
  281. wr32(E1000_P2VMAILBOX(vf_number), E1000_P2VMAILBOX_PFU);
  282. /* reserve mailbox for vf use */
  283. p2v_mailbox = rd32(E1000_P2VMAILBOX(vf_number));
  284. if (p2v_mailbox & E1000_P2VMAILBOX_PFU) {
  285. ret_val = 0;
  286. break;
  287. }
  288. udelay(1000);
  289. } while (count-- > 0);
  290. return ret_val;
  291. }
  292. /**
  293. * igb_write_mbx_pf - Places a message in the mailbox
  294. * @hw: pointer to the HW structure
  295. * @msg: The message buffer
  296. * @size: Length of buffer
  297. * @vf_number: the VF index
  298. *
  299. * returns SUCCESS if it successfully copied message into the buffer
  300. **/
  301. static s32 igb_write_mbx_pf(struct e1000_hw *hw, u32 *msg, u16 size,
  302. u16 vf_number)
  303. {
  304. s32 ret_val;
  305. u16 i;
  306. /* lock the mailbox to prevent pf/vf race condition */
  307. ret_val = igb_obtain_mbx_lock_pf(hw, vf_number);
  308. if (ret_val)
  309. goto out_no_write;
  310. /* flush msg and acks as we are overwriting the message buffer */
  311. igb_check_for_msg_pf(hw, vf_number);
  312. igb_check_for_ack_pf(hw, vf_number);
  313. /* copy the caller specified message to the mailbox memory buffer */
  314. for (i = 0; i < size; i++)
  315. array_wr32(E1000_VMBMEM(vf_number), i, msg[i]);
  316. /* Interrupt VF to tell it a message has been sent and release buffer*/
  317. wr32(E1000_P2VMAILBOX(vf_number), E1000_P2VMAILBOX_STS);
  318. /* update stats */
  319. hw->mbx.stats.msgs_tx++;
  320. out_no_write:
  321. return ret_val;
  322. }
  323. /**
  324. * igb_read_mbx_pf - Read a message from the mailbox
  325. * @hw: pointer to the HW structure
  326. * @msg: The message buffer
  327. * @size: Length of buffer
  328. * @vf_number: the VF index
  329. *
  330. * This function copies a message from the mailbox buffer to the caller's
  331. * memory buffer. The presumption is that the caller knows that there was
  332. * a message due to a VF request so no polling for message is needed.
  333. **/
  334. static s32 igb_read_mbx_pf(struct e1000_hw *hw, u32 *msg, u16 size,
  335. u16 vf_number)
  336. {
  337. s32 ret_val;
  338. u16 i;
  339. /* lock the mailbox to prevent pf/vf race condition */
  340. ret_val = igb_obtain_mbx_lock_pf(hw, vf_number);
  341. if (ret_val)
  342. goto out_no_read;
  343. /* copy the message to the mailbox memory buffer */
  344. for (i = 0; i < size; i++)
  345. msg[i] = array_rd32(E1000_VMBMEM(vf_number), i);
  346. /* Acknowledge the message and release buffer */
  347. wr32(E1000_P2VMAILBOX(vf_number), E1000_P2VMAILBOX_ACK);
  348. /* update stats */
  349. hw->mbx.stats.msgs_rx++;
  350. out_no_read:
  351. return ret_val;
  352. }
  353. /**
  354. * e1000_init_mbx_params_pf - set initial values for pf mailbox
  355. * @hw: pointer to the HW structure
  356. *
  357. * Initializes the hw->mbx struct to correct values for pf mailbox
  358. */
  359. s32 igb_init_mbx_params_pf(struct e1000_hw *hw)
  360. {
  361. struct e1000_mbx_info *mbx = &hw->mbx;
  362. mbx->timeout = 0;
  363. mbx->usec_delay = 0;
  364. mbx->size = E1000_VFMAILBOX_SIZE;
  365. mbx->ops.read = igb_read_mbx_pf;
  366. mbx->ops.write = igb_write_mbx_pf;
  367. mbx->ops.read_posted = igb_read_posted_mbx;
  368. mbx->ops.write_posted = igb_write_posted_mbx;
  369. mbx->ops.check_for_msg = igb_check_for_msg_pf;
  370. mbx->ops.check_for_ack = igb_check_for_ack_pf;
  371. mbx->ops.check_for_rst = igb_check_for_rst_pf;
  372. mbx->stats.msgs_tx = 0;
  373. mbx->stats.msgs_rx = 0;
  374. mbx->stats.reqs = 0;
  375. mbx->stats.acks = 0;
  376. mbx->stats.rsts = 0;
  377. return 0;
  378. }