mbx.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright(c) 1999 - 2018 Intel Corporation. */
  3. #include "mbx.h"
  4. #include "ixgbevf.h"
  5. /**
  6. * ixgbevf_poll_for_msg - Wait for message notification
  7. * @hw: pointer to the HW structure
  8. *
  9. * returns 0 if it successfully received a message notification
  10. **/
  11. static s32 ixgbevf_poll_for_msg(struct ixgbe_hw *hw)
  12. {
  13. struct ixgbe_mbx_info *mbx = &hw->mbx;
  14. int countdown = mbx->timeout;
  15. if (!countdown || !mbx->ops.check_for_msg)
  16. return IXGBE_ERR_CONFIG;
  17. while (countdown && mbx->ops.check_for_msg(hw)) {
  18. countdown--;
  19. udelay(mbx->udelay);
  20. }
  21. return countdown ? 0 : IXGBE_ERR_TIMEOUT;
  22. }
  23. /**
  24. * ixgbevf_poll_for_ack - Wait for message acknowledgment
  25. * @hw: pointer to the HW structure
  26. *
  27. * returns 0 if it successfully received a message acknowledgment
  28. **/
  29. static s32 ixgbevf_poll_for_ack(struct ixgbe_hw *hw)
  30. {
  31. struct ixgbe_mbx_info *mbx = &hw->mbx;
  32. int countdown = mbx->timeout;
  33. if (!countdown || !mbx->ops.check_for_ack)
  34. return IXGBE_ERR_CONFIG;
  35. while (countdown && mbx->ops.check_for_ack(hw)) {
  36. countdown--;
  37. udelay(mbx->udelay);
  38. }
  39. return countdown ? 0 : IXGBE_ERR_TIMEOUT;
  40. }
  41. /**
  42. * ixgbevf_read_mailbox_vf - read VF's mailbox register
  43. * @hw: pointer to the HW structure
  44. *
  45. * This function is used to read the mailbox register dedicated for VF without
  46. * losing the read to clear status bits.
  47. **/
  48. static u32 ixgbevf_read_mailbox_vf(struct ixgbe_hw *hw)
  49. {
  50. u32 vf_mailbox = IXGBE_READ_REG(hw, IXGBE_VFMAILBOX);
  51. vf_mailbox |= hw->mbx.vf_mailbox;
  52. hw->mbx.vf_mailbox |= vf_mailbox & IXGBE_VFMAILBOX_R2C_BITS;
  53. return vf_mailbox;
  54. }
  55. /**
  56. * ixgbevf_clear_msg_vf - clear PF status bit
  57. * @hw: pointer to the HW structure
  58. *
  59. * This function is used to clear PFSTS bit in the VFMAILBOX register
  60. **/
  61. static void ixgbevf_clear_msg_vf(struct ixgbe_hw *hw)
  62. {
  63. u32 vf_mailbox = ixgbevf_read_mailbox_vf(hw);
  64. if (vf_mailbox & IXGBE_VFMAILBOX_PFSTS) {
  65. hw->mbx.stats.reqs++;
  66. hw->mbx.vf_mailbox &= ~IXGBE_VFMAILBOX_PFSTS;
  67. }
  68. }
  69. /**
  70. * ixgbevf_clear_ack_vf - clear PF ACK bit
  71. * @hw: pointer to the HW structure
  72. *
  73. * This function is used to clear PFACK bit in the VFMAILBOX register
  74. **/
  75. static void ixgbevf_clear_ack_vf(struct ixgbe_hw *hw)
  76. {
  77. u32 vf_mailbox = ixgbevf_read_mailbox_vf(hw);
  78. if (vf_mailbox & IXGBE_VFMAILBOX_PFACK) {
  79. hw->mbx.stats.acks++;
  80. hw->mbx.vf_mailbox &= ~IXGBE_VFMAILBOX_PFACK;
  81. }
  82. }
  83. /**
  84. * ixgbevf_clear_rst_vf - clear PF reset bit
  85. * @hw: pointer to the HW structure
  86. *
  87. * This function is used to clear reset indication and reset done bit in
  88. * VFMAILBOX register after reset the shared resources and the reset sequence.
  89. **/
  90. static void ixgbevf_clear_rst_vf(struct ixgbe_hw *hw)
  91. {
  92. u32 vf_mailbox = ixgbevf_read_mailbox_vf(hw);
  93. if (vf_mailbox & (IXGBE_VFMAILBOX_RSTI | IXGBE_VFMAILBOX_RSTD)) {
  94. hw->mbx.stats.rsts++;
  95. hw->mbx.vf_mailbox &= ~(IXGBE_VFMAILBOX_RSTI |
  96. IXGBE_VFMAILBOX_RSTD);
  97. }
  98. }
  99. /**
  100. * ixgbevf_check_for_bit_vf - Determine if a status bit was set
  101. * @hw: pointer to the HW structure
  102. * @mask: bitmask for bits to be tested and cleared
  103. *
  104. * This function is used to check for the read to clear bits within
  105. * the V2P mailbox.
  106. **/
  107. static s32 ixgbevf_check_for_bit_vf(struct ixgbe_hw *hw, u32 mask)
  108. {
  109. u32 vf_mailbox = ixgbevf_read_mailbox_vf(hw);
  110. s32 ret_val = IXGBE_ERR_MBX;
  111. if (vf_mailbox & mask)
  112. ret_val = 0;
  113. return ret_val;
  114. }
  115. /**
  116. * ixgbevf_check_for_msg_vf - checks to see if the PF has sent mail
  117. * @hw: pointer to the HW structure
  118. *
  119. * returns 0 if the PF has set the Status bit or else ERR_MBX
  120. **/
  121. static s32 ixgbevf_check_for_msg_vf(struct ixgbe_hw *hw)
  122. {
  123. s32 ret_val = IXGBE_ERR_MBX;
  124. if (!ixgbevf_check_for_bit_vf(hw, IXGBE_VFMAILBOX_PFSTS)) {
  125. ret_val = 0;
  126. hw->mbx.stats.reqs++;
  127. }
  128. return ret_val;
  129. }
  130. /**
  131. * ixgbevf_check_for_ack_vf - checks to see if the PF has ACK'd
  132. * @hw: pointer to the HW structure
  133. *
  134. * returns 0 if the PF has set the ACK bit or else ERR_MBX
  135. **/
  136. static s32 ixgbevf_check_for_ack_vf(struct ixgbe_hw *hw)
  137. {
  138. s32 ret_val = IXGBE_ERR_MBX;
  139. if (!ixgbevf_check_for_bit_vf(hw, IXGBE_VFMAILBOX_PFACK)) {
  140. ret_val = 0;
  141. ixgbevf_clear_ack_vf(hw);
  142. hw->mbx.stats.acks++;
  143. }
  144. return ret_val;
  145. }
  146. /**
  147. * ixgbevf_check_for_rst_vf - checks to see if the PF has reset
  148. * @hw: pointer to the HW structure
  149. *
  150. * returns true if the PF has set the reset done bit or else false
  151. **/
  152. static s32 ixgbevf_check_for_rst_vf(struct ixgbe_hw *hw)
  153. {
  154. s32 ret_val = IXGBE_ERR_MBX;
  155. if (!ixgbevf_check_for_bit_vf(hw, (IXGBE_VFMAILBOX_RSTD |
  156. IXGBE_VFMAILBOX_RSTI))) {
  157. ret_val = 0;
  158. ixgbevf_clear_rst_vf(hw);
  159. hw->mbx.stats.rsts++;
  160. }
  161. return ret_val;
  162. }
  163. /**
  164. * ixgbevf_obtain_mbx_lock_vf - obtain mailbox lock
  165. * @hw: pointer to the HW structure
  166. *
  167. * return 0 if we obtained the mailbox lock
  168. **/
  169. static s32 ixgbevf_obtain_mbx_lock_vf(struct ixgbe_hw *hw)
  170. {
  171. struct ixgbe_mbx_info *mbx = &hw->mbx;
  172. s32 ret_val = IXGBE_ERR_CONFIG;
  173. int countdown = mbx->timeout;
  174. u32 vf_mailbox;
  175. if (!mbx->timeout)
  176. return ret_val;
  177. while (countdown--) {
  178. /* Reserve mailbox for VF use */
  179. vf_mailbox = ixgbevf_read_mailbox_vf(hw);
  180. vf_mailbox |= IXGBE_VFMAILBOX_VFU;
  181. IXGBE_WRITE_REG(hw, IXGBE_VFMAILBOX, vf_mailbox);
  182. /* Verify that VF is the owner of the lock */
  183. if (ixgbevf_read_mailbox_vf(hw) & IXGBE_VFMAILBOX_VFU) {
  184. ret_val = 0;
  185. break;
  186. }
  187. /* Wait a bit before trying again */
  188. udelay(mbx->udelay);
  189. }
  190. if (ret_val)
  191. ret_val = IXGBE_ERR_TIMEOUT;
  192. return ret_val;
  193. }
  194. /**
  195. * ixgbevf_release_mbx_lock_vf - release mailbox lock
  196. * @hw: pointer to the HW structure
  197. **/
  198. static void ixgbevf_release_mbx_lock_vf(struct ixgbe_hw *hw)
  199. {
  200. u32 vf_mailbox;
  201. /* Return ownership of the buffer */
  202. vf_mailbox = ixgbevf_read_mailbox_vf(hw);
  203. vf_mailbox &= ~IXGBE_VFMAILBOX_VFU;
  204. IXGBE_WRITE_REG(hw, IXGBE_VFMAILBOX, vf_mailbox);
  205. }
  206. /**
  207. * ixgbevf_release_mbx_lock_vf_legacy - release mailbox lock
  208. * @hw: pointer to the HW structure
  209. **/
  210. static void ixgbevf_release_mbx_lock_vf_legacy(struct ixgbe_hw *__always_unused hw)
  211. {
  212. }
  213. /**
  214. * ixgbevf_write_mbx_vf - Write a message to the mailbox
  215. * @hw: pointer to the HW structure
  216. * @msg: The message buffer
  217. * @size: Length of buffer
  218. *
  219. * returns 0 if it successfully copied message into the buffer
  220. **/
  221. static s32 ixgbevf_write_mbx_vf(struct ixgbe_hw *hw, u32 *msg, u16 size)
  222. {
  223. u32 vf_mailbox;
  224. s32 ret_val;
  225. u16 i;
  226. /* lock the mailbox to prevent PF/VF race condition */
  227. ret_val = ixgbevf_obtain_mbx_lock_vf(hw);
  228. if (ret_val)
  229. goto out_no_write;
  230. /* flush msg and acks as we are overwriting the message buffer */
  231. ixgbevf_clear_msg_vf(hw);
  232. ixgbevf_clear_ack_vf(hw);
  233. /* copy the caller specified message to the mailbox memory buffer */
  234. for (i = 0; i < size; i++)
  235. IXGBE_WRITE_REG_ARRAY(hw, IXGBE_VFMBMEM, i, msg[i]);
  236. /* update stats */
  237. hw->mbx.stats.msgs_tx++;
  238. /* interrupt the PF to tell it a message has been sent */
  239. vf_mailbox = ixgbevf_read_mailbox_vf(hw);
  240. vf_mailbox |= IXGBE_VFMAILBOX_REQ;
  241. IXGBE_WRITE_REG(hw, IXGBE_VFMAILBOX, vf_mailbox);
  242. /* if msg sent wait until we receive an ack */
  243. ret_val = ixgbevf_poll_for_ack(hw);
  244. out_no_write:
  245. hw->mbx.ops.release(hw);
  246. return ret_val;
  247. }
  248. /**
  249. * ixgbevf_write_mbx_vf_legacy - Write a message to the mailbox
  250. * @hw: pointer to the HW structure
  251. * @msg: The message buffer
  252. * @size: Length of buffer
  253. *
  254. * returns 0 if it successfully copied message into the buffer
  255. **/
  256. static s32 ixgbevf_write_mbx_vf_legacy(struct ixgbe_hw *hw, u32 *msg, u16 size)
  257. {
  258. s32 ret_val;
  259. u16 i;
  260. /* lock the mailbox to prevent PF/VF race condition */
  261. ret_val = ixgbevf_obtain_mbx_lock_vf(hw);
  262. if (ret_val)
  263. goto out_no_write;
  264. /* flush msg and acks as we are overwriting the message buffer */
  265. ixgbevf_check_for_msg_vf(hw);
  266. ixgbevf_clear_msg_vf(hw);
  267. ixgbevf_check_for_ack_vf(hw);
  268. ixgbevf_clear_ack_vf(hw);
  269. /* copy the caller specified message to the mailbox memory buffer */
  270. for (i = 0; i < size; i++)
  271. IXGBE_WRITE_REG_ARRAY(hw, IXGBE_VFMBMEM, i, msg[i]);
  272. /* update stats */
  273. hw->mbx.stats.msgs_tx++;
  274. /* Drop VFU and interrupt the PF to tell it a message has been sent */
  275. IXGBE_WRITE_REG(hw, IXGBE_VFMAILBOX, IXGBE_VFMAILBOX_REQ);
  276. out_no_write:
  277. return ret_val;
  278. }
  279. /**
  280. * ixgbevf_read_mbx_vf - Reads a message from the inbox intended for VF
  281. * @hw: pointer to the HW structure
  282. * @msg: The message buffer
  283. * @size: Length of buffer
  284. *
  285. * returns 0 if it successfully read message from buffer
  286. **/
  287. static s32 ixgbevf_read_mbx_vf(struct ixgbe_hw *hw, u32 *msg, u16 size)
  288. {
  289. u32 vf_mailbox;
  290. s32 ret_val;
  291. u16 i;
  292. /* check if there is a message from PF */
  293. ret_val = ixgbevf_check_for_msg_vf(hw);
  294. if (ret_val)
  295. return ret_val;
  296. ixgbevf_clear_msg_vf(hw);
  297. /* copy the message from the mailbox memory buffer */
  298. for (i = 0; i < size; i++)
  299. msg[i] = IXGBE_READ_REG_ARRAY(hw, IXGBE_VFMBMEM, i);
  300. /* Acknowledge receipt */
  301. vf_mailbox = ixgbevf_read_mailbox_vf(hw);
  302. vf_mailbox |= IXGBE_VFMAILBOX_ACK;
  303. IXGBE_WRITE_REG(hw, IXGBE_VFMAILBOX, vf_mailbox);
  304. /* update stats */
  305. hw->mbx.stats.msgs_rx++;
  306. return ret_val;
  307. }
  308. /**
  309. * ixgbevf_read_mbx_vf_legacy - Reads a message from the inbox intended for VF
  310. * @hw: pointer to the HW structure
  311. * @msg: The message buffer
  312. * @size: Length of buffer
  313. *
  314. * returns 0 if it successfully read message from buffer
  315. **/
  316. static s32 ixgbevf_read_mbx_vf_legacy(struct ixgbe_hw *hw, u32 *msg, u16 size)
  317. {
  318. s32 ret_val = 0;
  319. u16 i;
  320. /* lock the mailbox to prevent PF/VF race condition */
  321. ret_val = ixgbevf_obtain_mbx_lock_vf(hw);
  322. if (ret_val)
  323. goto out_no_read;
  324. /* copy the message from the mailbox memory buffer */
  325. for (i = 0; i < size; i++)
  326. msg[i] = IXGBE_READ_REG_ARRAY(hw, IXGBE_VFMBMEM, i);
  327. /* Acknowledge receipt and release mailbox, then we're done */
  328. IXGBE_WRITE_REG(hw, IXGBE_VFMAILBOX, IXGBE_VFMAILBOX_ACK);
  329. /* update stats */
  330. hw->mbx.stats.msgs_rx++;
  331. out_no_read:
  332. return ret_val;
  333. }
  334. /**
  335. * ixgbevf_init_mbx_params_vf - set initial values for VF mailbox
  336. * @hw: pointer to the HW structure
  337. *
  338. * Initializes the hw->mbx struct to correct values for VF mailbox
  339. */
  340. static s32 ixgbevf_init_mbx_params_vf(struct ixgbe_hw *hw)
  341. {
  342. struct ixgbe_mbx_info *mbx = &hw->mbx;
  343. /* start mailbox as timed out and let the reset_hw call set the timeout
  344. * value to begin communications
  345. */
  346. mbx->timeout = IXGBE_VF_MBX_INIT_TIMEOUT;
  347. mbx->udelay = IXGBE_VF_MBX_INIT_DELAY;
  348. mbx->size = IXGBE_VFMAILBOX_SIZE;
  349. mbx->stats.msgs_tx = 0;
  350. mbx->stats.msgs_rx = 0;
  351. mbx->stats.reqs = 0;
  352. mbx->stats.acks = 0;
  353. mbx->stats.rsts = 0;
  354. return 0;
  355. }
  356. /**
  357. * ixgbevf_poll_mbx - Wait for message and read it from the mailbox
  358. * @hw: pointer to the HW structure
  359. * @msg: The message buffer
  360. * @size: Length of buffer
  361. *
  362. * returns 0 if it successfully read message from buffer
  363. **/
  364. s32 ixgbevf_poll_mbx(struct ixgbe_hw *hw, u32 *msg, u16 size)
  365. {
  366. struct ixgbe_mbx_info *mbx = &hw->mbx;
  367. s32 ret_val = IXGBE_ERR_CONFIG;
  368. if (!mbx->ops.read || !mbx->ops.check_for_msg || !mbx->timeout)
  369. return ret_val;
  370. /* limit read to size of mailbox */
  371. if (size > mbx->size)
  372. size = mbx->size;
  373. ret_val = ixgbevf_poll_for_msg(hw);
  374. /* if ack received read message, otherwise we timed out */
  375. if (!ret_val)
  376. ret_val = mbx->ops.read(hw, msg, size);
  377. return ret_val;
  378. }
  379. /**
  380. * ixgbevf_write_mbx - Write a message to the mailbox and wait for ACK
  381. * @hw: pointer to the HW structure
  382. * @msg: The message buffer
  383. * @size: Length of buffer
  384. *
  385. * returns 0 if it successfully copied message into the buffer and
  386. * received an ACK to that message within specified period
  387. **/
  388. s32 ixgbevf_write_mbx(struct ixgbe_hw *hw, u32 *msg, u16 size)
  389. {
  390. struct ixgbe_mbx_info *mbx = &hw->mbx;
  391. s32 ret_val = IXGBE_ERR_CONFIG;
  392. /**
  393. * exit if either we can't write, release
  394. * or there is no timeout defined
  395. */
  396. if (!mbx->ops.write || !mbx->ops.check_for_ack || !mbx->ops.release ||
  397. !mbx->timeout)
  398. return ret_val;
  399. if (size > mbx->size)
  400. ret_val = IXGBE_ERR_PARAM;
  401. else
  402. ret_val = mbx->ops.write(hw, msg, size);
  403. return ret_val;
  404. }
  405. const struct ixgbe_mbx_operations ixgbevf_mbx_ops = {
  406. .init_params = ixgbevf_init_mbx_params_vf,
  407. .release = ixgbevf_release_mbx_lock_vf,
  408. .read = ixgbevf_read_mbx_vf,
  409. .write = ixgbevf_write_mbx_vf,
  410. .check_for_msg = ixgbevf_check_for_msg_vf,
  411. .check_for_ack = ixgbevf_check_for_ack_vf,
  412. .check_for_rst = ixgbevf_check_for_rst_vf,
  413. };
  414. const struct ixgbe_mbx_operations ixgbevf_mbx_ops_legacy = {
  415. .init_params = ixgbevf_init_mbx_params_vf,
  416. .release = ixgbevf_release_mbx_lock_vf_legacy,
  417. .read = ixgbevf_read_mbx_vf_legacy,
  418. .write = ixgbevf_write_mbx_vf_legacy,
  419. .check_for_msg = ixgbevf_check_for_msg_vf,
  420. .check_for_ack = ixgbevf_check_for_ack_vf,
  421. .check_for_rst = ixgbevf_check_for_rst_vf,
  422. };
  423. /* Mailbox operations when running on Hyper-V.
  424. * On Hyper-V, PF/VF communication is not through the
  425. * hardware mailbox; this communication is through
  426. * a software mediated path.
  427. * Most mail box operations are noop while running on
  428. * Hyper-V.
  429. */
  430. const struct ixgbe_mbx_operations ixgbevf_hv_mbx_ops = {
  431. .init_params = ixgbevf_init_mbx_params_vf,
  432. .check_for_rst = ixgbevf_check_for_rst_vf,
  433. };