e1000_mbx.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright(c) 2007 - 2018 Intel Corporation. */
  3. #include "e1000_mbx.h"
  4. /**
  5. * igb_read_mbx - Reads a message from the mailbox
  6. * @hw: pointer to the HW structure
  7. * @msg: The message buffer
  8. * @size: Length of buffer
  9. * @mbx_id: id of mailbox to read
  10. *
  11. * returns SUCCESS if it successfully read message from buffer
  12. **/
  13. s32 igb_read_mbx(struct e1000_hw *hw, u32 *msg, u16 size, u16 mbx_id,
  14. bool unlock)
  15. {
  16. struct e1000_mbx_info *mbx = &hw->mbx;
  17. s32 ret_val = -E1000_ERR_MBX;
  18. /* limit read to size of mailbox */
  19. if (size > mbx->size)
  20. size = mbx->size;
  21. if (mbx->ops.read)
  22. ret_val = mbx->ops.read(hw, msg, size, mbx_id, unlock);
  23. return ret_val;
  24. }
  25. /**
  26. * igb_write_mbx - Write a message to the mailbox
  27. * @hw: pointer to the HW structure
  28. * @msg: The message buffer
  29. * @size: Length of buffer
  30. * @mbx_id: id of mailbox to write
  31. *
  32. * returns SUCCESS if it successfully copied message into the buffer
  33. **/
  34. s32 igb_write_mbx(struct e1000_hw *hw, u32 *msg, u16 size, u16 mbx_id)
  35. {
  36. struct e1000_mbx_info *mbx = &hw->mbx;
  37. s32 ret_val = 0;
  38. if (size > mbx->size)
  39. ret_val = -E1000_ERR_MBX;
  40. else if (mbx->ops.write)
  41. ret_val = mbx->ops.write(hw, msg, size, mbx_id);
  42. return ret_val;
  43. }
  44. /**
  45. * igb_check_for_msg - checks to see if someone sent us mail
  46. * @hw: pointer to the HW structure
  47. * @mbx_id: id of mailbox to check
  48. *
  49. * returns SUCCESS if the Status bit was found or else ERR_MBX
  50. **/
  51. s32 igb_check_for_msg(struct e1000_hw *hw, u16 mbx_id)
  52. {
  53. struct e1000_mbx_info *mbx = &hw->mbx;
  54. s32 ret_val = -E1000_ERR_MBX;
  55. if (mbx->ops.check_for_msg)
  56. ret_val = mbx->ops.check_for_msg(hw, mbx_id);
  57. return ret_val;
  58. }
  59. /**
  60. * igb_check_for_ack - checks to see if someone sent us ACK
  61. * @hw: pointer to the HW structure
  62. * @mbx_id: id of mailbox to check
  63. *
  64. * returns SUCCESS if the Status bit was found or else ERR_MBX
  65. **/
  66. s32 igb_check_for_ack(struct e1000_hw *hw, u16 mbx_id)
  67. {
  68. struct e1000_mbx_info *mbx = &hw->mbx;
  69. s32 ret_val = -E1000_ERR_MBX;
  70. if (mbx->ops.check_for_ack)
  71. ret_val = mbx->ops.check_for_ack(hw, mbx_id);
  72. return ret_val;
  73. }
  74. /**
  75. * igb_check_for_rst - checks to see if other side has reset
  76. * @hw: pointer to the HW structure
  77. * @mbx_id: id of mailbox to check
  78. *
  79. * returns SUCCESS if the Status bit was found or else ERR_MBX
  80. **/
  81. s32 igb_check_for_rst(struct e1000_hw *hw, u16 mbx_id)
  82. {
  83. struct e1000_mbx_info *mbx = &hw->mbx;
  84. s32 ret_val = -E1000_ERR_MBX;
  85. if (mbx->ops.check_for_rst)
  86. ret_val = mbx->ops.check_for_rst(hw, mbx_id);
  87. return ret_val;
  88. }
  89. /**
  90. * igb_unlock_mbx - unlock the mailbox
  91. * @hw: pointer to the HW structure
  92. * @mbx_id: id of mailbox to check
  93. *
  94. * returns SUCCESS if the mailbox was unlocked or else ERR_MBX
  95. **/
  96. s32 igb_unlock_mbx(struct e1000_hw *hw, u16 mbx_id)
  97. {
  98. struct e1000_mbx_info *mbx = &hw->mbx;
  99. s32 ret_val = -E1000_ERR_MBX;
  100. if (mbx->ops.unlock)
  101. ret_val = mbx->ops.unlock(hw, mbx_id);
  102. return ret_val;
  103. }
  104. /**
  105. * igb_poll_for_msg - Wait for message notification
  106. * @hw: pointer to the HW structure
  107. * @mbx_id: id of mailbox to write
  108. *
  109. * returns SUCCESS if it successfully received a message notification
  110. **/
  111. static s32 igb_poll_for_msg(struct e1000_hw *hw, u16 mbx_id)
  112. {
  113. struct e1000_mbx_info *mbx = &hw->mbx;
  114. int countdown = mbx->timeout;
  115. if (!countdown || !mbx->ops.check_for_msg)
  116. goto out;
  117. while (countdown && mbx->ops.check_for_msg(hw, mbx_id)) {
  118. countdown--;
  119. if (!countdown)
  120. break;
  121. udelay(mbx->usec_delay);
  122. }
  123. /* if we failed, all future posted messages fail until reset */
  124. if (!countdown)
  125. mbx->timeout = 0;
  126. out:
  127. return countdown ? 0 : -E1000_ERR_MBX;
  128. }
  129. /**
  130. * igb_poll_for_ack - Wait for message acknowledgement
  131. * @hw: pointer to the HW structure
  132. * @mbx_id: id of mailbox to write
  133. *
  134. * returns SUCCESS if it successfully received a message acknowledgement
  135. **/
  136. static s32 igb_poll_for_ack(struct e1000_hw *hw, u16 mbx_id)
  137. {
  138. struct e1000_mbx_info *mbx = &hw->mbx;
  139. int countdown = mbx->timeout;
  140. if (!countdown || !mbx->ops.check_for_ack)
  141. goto out;
  142. while (countdown && mbx->ops.check_for_ack(hw, mbx_id)) {
  143. countdown--;
  144. if (!countdown)
  145. break;
  146. udelay(mbx->usec_delay);
  147. }
  148. /* if we failed, all future posted messages fail until reset */
  149. if (!countdown)
  150. mbx->timeout = 0;
  151. out:
  152. return countdown ? 0 : -E1000_ERR_MBX;
  153. }
  154. /**
  155. * igb_read_posted_mbx - Wait for message notification and receive message
  156. * @hw: pointer to the HW structure
  157. * @msg: The message buffer
  158. * @size: Length of buffer
  159. * @mbx_id: id of mailbox to write
  160. *
  161. * returns SUCCESS if it successfully received a message notification and
  162. * copied it into the receive buffer.
  163. **/
  164. static s32 igb_read_posted_mbx(struct e1000_hw *hw, u32 *msg, u16 size,
  165. 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, true);
  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,
  188. u16 mbx_id)
  189. {
  190. struct e1000_mbx_info *mbx = &hw->mbx;
  191. s32 ret_val = -E1000_ERR_MBX;
  192. /* exit if either we can't write or there isn't a defined timeout */
  193. if (!mbx->ops.write || !mbx->timeout)
  194. goto out;
  195. /* send msg */
  196. ret_val = mbx->ops.write(hw, msg, size, mbx_id);
  197. /* if msg sent wait until we receive an ack */
  198. if (!ret_val)
  199. ret_val = igb_poll_for_ack(hw, mbx_id);
  200. out:
  201. return ret_val;
  202. }
  203. static s32 igb_check_for_bit_pf(struct e1000_hw *hw, u32 mask)
  204. {
  205. u32 mbvficr = rd32(E1000_MBVFICR);
  206. s32 ret_val = -E1000_ERR_MBX;
  207. if (mbvficr & mask) {
  208. ret_val = 0;
  209. wr32(E1000_MBVFICR, mask);
  210. }
  211. return ret_val;
  212. }
  213. /**
  214. * igb_check_for_msg_pf - checks to see if the VF has sent mail
  215. * @hw: pointer to the HW structure
  216. * @vf_number: the VF index
  217. *
  218. * returns SUCCESS if the VF has set the Status bit or else ERR_MBX
  219. **/
  220. static s32 igb_check_for_msg_pf(struct e1000_hw *hw, u16 vf_number)
  221. {
  222. s32 ret_val = -E1000_ERR_MBX;
  223. if (!igb_check_for_bit_pf(hw, E1000_MBVFICR_VFREQ_VF1 << vf_number)) {
  224. ret_val = 0;
  225. hw->mbx.stats.reqs++;
  226. }
  227. return ret_val;
  228. }
  229. /**
  230. * igb_check_for_ack_pf - checks to see if the VF has ACKed
  231. * @hw: pointer to the HW structure
  232. * @vf_number: the VF index
  233. *
  234. * returns SUCCESS if the VF has set the Status bit or else ERR_MBX
  235. **/
  236. static s32 igb_check_for_ack_pf(struct e1000_hw *hw, u16 vf_number)
  237. {
  238. s32 ret_val = -E1000_ERR_MBX;
  239. if (!igb_check_for_bit_pf(hw, E1000_MBVFICR_VFACK_VF1 << vf_number)) {
  240. ret_val = 0;
  241. hw->mbx.stats.acks++;
  242. }
  243. return ret_val;
  244. }
  245. /**
  246. * igb_check_for_rst_pf - checks to see if the VF has reset
  247. * @hw: pointer to the HW structure
  248. * @vf_number: the VF index
  249. *
  250. * returns SUCCESS if the VF has set the Status bit or else ERR_MBX
  251. **/
  252. static s32 igb_check_for_rst_pf(struct e1000_hw *hw, u16 vf_number)
  253. {
  254. u32 vflre = rd32(E1000_VFLRE);
  255. s32 ret_val = -E1000_ERR_MBX;
  256. if (vflre & BIT(vf_number)) {
  257. ret_val = 0;
  258. wr32(E1000_VFLRE, BIT(vf_number));
  259. hw->mbx.stats.rsts++;
  260. }
  261. return ret_val;
  262. }
  263. /**
  264. * igb_obtain_mbx_lock_pf - obtain mailbox lock
  265. * @hw: pointer to the HW structure
  266. * @vf_number: the VF index
  267. *
  268. * return SUCCESS if we obtained the mailbox lock
  269. **/
  270. static s32 igb_obtain_mbx_lock_pf(struct e1000_hw *hw, u16 vf_number)
  271. {
  272. s32 ret_val = -E1000_ERR_MBX;
  273. u32 p2v_mailbox;
  274. int count = 10;
  275. do {
  276. /* Take ownership of the buffer */
  277. wr32(E1000_P2VMAILBOX(vf_number), E1000_P2VMAILBOX_PFU);
  278. /* reserve mailbox for vf use */
  279. p2v_mailbox = rd32(E1000_P2VMAILBOX(vf_number));
  280. if (p2v_mailbox & E1000_P2VMAILBOX_PFU) {
  281. ret_val = 0;
  282. break;
  283. }
  284. udelay(1000);
  285. } while (count-- > 0);
  286. return ret_val;
  287. }
  288. /**
  289. * igb_release_mbx_lock_pf - release mailbox lock
  290. * @hw: pointer to the HW structure
  291. * @vf_number: the VF index
  292. *
  293. * return SUCCESS if we released the mailbox lock
  294. **/
  295. static s32 igb_release_mbx_lock_pf(struct e1000_hw *hw, u16 vf_number)
  296. {
  297. u32 p2v_mailbox;
  298. /* drop PF lock of mailbox, if set */
  299. p2v_mailbox = rd32(E1000_P2VMAILBOX(vf_number));
  300. if (p2v_mailbox & E1000_P2VMAILBOX_PFU)
  301. wr32(E1000_P2VMAILBOX(vf_number),
  302. p2v_mailbox & ~E1000_P2VMAILBOX_PFU);
  303. return 0;
  304. }
  305. /**
  306. * igb_write_mbx_pf - Places a message in the mailbox
  307. * @hw: pointer to the HW structure
  308. * @msg: The message buffer
  309. * @size: Length of buffer
  310. * @vf_number: the VF index
  311. *
  312. * returns SUCCESS if it successfully copied message into the buffer
  313. **/
  314. static s32 igb_write_mbx_pf(struct e1000_hw *hw, u32 *msg, u16 size,
  315. u16 vf_number)
  316. {
  317. s32 ret_val;
  318. u16 i;
  319. /* lock the mailbox to prevent pf/vf race condition */
  320. ret_val = igb_obtain_mbx_lock_pf(hw, vf_number);
  321. if (ret_val)
  322. goto out_no_write;
  323. /* flush msg and acks as we are overwriting the message buffer */
  324. igb_check_for_msg_pf(hw, vf_number);
  325. igb_check_for_ack_pf(hw, vf_number);
  326. /* copy the caller specified message to the mailbox memory buffer */
  327. for (i = 0; i < size; i++)
  328. array_wr32(E1000_VMBMEM(vf_number), i, msg[i]);
  329. /* Interrupt VF to tell it a message has been sent and release buffer*/
  330. wr32(E1000_P2VMAILBOX(vf_number), E1000_P2VMAILBOX_STS);
  331. /* update stats */
  332. hw->mbx.stats.msgs_tx++;
  333. out_no_write:
  334. return ret_val;
  335. }
  336. /**
  337. * igb_read_mbx_pf - Read a message from the mailbox
  338. * @hw: pointer to the HW structure
  339. * @msg: The message buffer
  340. * @size: Length of buffer
  341. * @vf_number: the VF index
  342. * @unlock: unlock the mailbox when done?
  343. *
  344. * This function copies a message from the mailbox buffer to the caller's
  345. * memory buffer. The presumption is that the caller knows that there was
  346. * a message due to a VF request so no polling for message is needed.
  347. **/
  348. static s32 igb_read_mbx_pf(struct e1000_hw *hw, u32 *msg, u16 size,
  349. u16 vf_number, bool unlock)
  350. {
  351. s32 ret_val;
  352. u16 i;
  353. /* lock the mailbox to prevent pf/vf race condition */
  354. ret_val = igb_obtain_mbx_lock_pf(hw, vf_number);
  355. if (ret_val)
  356. goto out_no_read;
  357. /* copy the message to the mailbox memory buffer */
  358. for (i = 0; i < size; i++)
  359. msg[i] = array_rd32(E1000_VMBMEM(vf_number), i);
  360. /* Acknowledge the message and release mailbox lock (or not) */
  361. if (unlock)
  362. wr32(E1000_P2VMAILBOX(vf_number), E1000_P2VMAILBOX_ACK);
  363. else
  364. wr32(E1000_P2VMAILBOX(vf_number),
  365. E1000_P2VMAILBOX_ACK | E1000_P2VMAILBOX_PFU);
  366. /* update stats */
  367. hw->mbx.stats.msgs_rx++;
  368. out_no_read:
  369. return ret_val;
  370. }
  371. /**
  372. * e1000_init_mbx_params_pf - set initial values for pf mailbox
  373. * @hw: pointer to the HW structure
  374. *
  375. * Initializes the hw->mbx struct to correct values for pf mailbox
  376. */
  377. s32 igb_init_mbx_params_pf(struct e1000_hw *hw)
  378. {
  379. struct e1000_mbx_info *mbx = &hw->mbx;
  380. mbx->timeout = 0;
  381. mbx->usec_delay = 0;
  382. mbx->size = E1000_VFMAILBOX_SIZE;
  383. mbx->ops.read = igb_read_mbx_pf;
  384. mbx->ops.write = igb_write_mbx_pf;
  385. mbx->ops.read_posted = igb_read_posted_mbx;
  386. mbx->ops.write_posted = igb_write_posted_mbx;
  387. mbx->ops.check_for_msg = igb_check_for_msg_pf;
  388. mbx->ops.check_for_ack = igb_check_for_ack_pf;
  389. mbx->ops.check_for_rst = igb_check_for_rst_pf;
  390. mbx->ops.unlock = igb_release_mbx_lock_pf;
  391. mbx->stats.msgs_tx = 0;
  392. mbx->stats.msgs_rx = 0;
  393. mbx->stats.reqs = 0;
  394. mbx->stats.acks = 0;
  395. mbx->stats.rsts = 0;
  396. return 0;
  397. }