ixgbe_mbx.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. /*******************************************************************************
  2. Intel 10 Gigabit PCI Express Linux driver
  3. Copyright(c) 1999 - 2016 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. Linux NICS <linux.nics@intel.com>
  18. e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
  19. Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  20. *******************************************************************************/
  21. #include <linux/pci.h>
  22. #include <linux/delay.h>
  23. #include "ixgbe.h"
  24. #include "ixgbe_mbx.h"
  25. /**
  26. * ixgbe_read_mbx - Reads a message from 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 read
  31. *
  32. * returns SUCCESS if it successfully read message from buffer
  33. **/
  34. s32 ixgbe_read_mbx(struct ixgbe_hw *hw, u32 *msg, u16 size, u16 mbx_id)
  35. {
  36. struct ixgbe_mbx_info *mbx = &hw->mbx;
  37. /* limit read to size of mailbox */
  38. if (size > mbx->size)
  39. size = mbx->size;
  40. if (!mbx->ops)
  41. return IXGBE_ERR_MBX;
  42. return mbx->ops->read(hw, msg, size, mbx_id);
  43. }
  44. /**
  45. * ixgbe_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 ixgbe_write_mbx(struct ixgbe_hw *hw, u32 *msg, u16 size, u16 mbx_id)
  54. {
  55. struct ixgbe_mbx_info *mbx = &hw->mbx;
  56. if (size > mbx->size)
  57. return IXGBE_ERR_MBX;
  58. if (!mbx->ops)
  59. return IXGBE_ERR_MBX;
  60. return mbx->ops->write(hw, msg, size, mbx_id);
  61. }
  62. /**
  63. * ixgbe_check_for_msg - checks to see if someone sent us mail
  64. * @hw: pointer to the HW structure
  65. * @mbx_id: id of mailbox to check
  66. *
  67. * returns SUCCESS if the Status bit was found or else ERR_MBX
  68. **/
  69. s32 ixgbe_check_for_msg(struct ixgbe_hw *hw, u16 mbx_id)
  70. {
  71. struct ixgbe_mbx_info *mbx = &hw->mbx;
  72. if (!mbx->ops)
  73. return IXGBE_ERR_MBX;
  74. return mbx->ops->check_for_msg(hw, mbx_id);
  75. }
  76. /**
  77. * ixgbe_check_for_ack - checks to see if someone sent us ACK
  78. * @hw: pointer to the HW structure
  79. * @mbx_id: id of mailbox to check
  80. *
  81. * returns SUCCESS if the Status bit was found or else ERR_MBX
  82. **/
  83. s32 ixgbe_check_for_ack(struct ixgbe_hw *hw, u16 mbx_id)
  84. {
  85. struct ixgbe_mbx_info *mbx = &hw->mbx;
  86. if (!mbx->ops)
  87. return IXGBE_ERR_MBX;
  88. return mbx->ops->check_for_ack(hw, mbx_id);
  89. }
  90. /**
  91. * ixgbe_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 ixgbe_check_for_rst(struct ixgbe_hw *hw, u16 mbx_id)
  98. {
  99. struct ixgbe_mbx_info *mbx = &hw->mbx;
  100. if (!mbx->ops)
  101. return IXGBE_ERR_MBX;
  102. return mbx->ops->check_for_rst(hw, mbx_id);
  103. }
  104. /**
  105. * ixgbe_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 ixgbe_poll_for_msg(struct ixgbe_hw *hw, u16 mbx_id)
  112. {
  113. struct ixgbe_mbx_info *mbx = &hw->mbx;
  114. int countdown = mbx->timeout;
  115. if (!countdown || !mbx->ops)
  116. return IXGBE_ERR_MBX;
  117. while (mbx->ops->check_for_msg(hw, mbx_id)) {
  118. countdown--;
  119. if (!countdown)
  120. return IXGBE_ERR_MBX;
  121. udelay(mbx->usec_delay);
  122. }
  123. return 0;
  124. }
  125. /**
  126. * ixgbe_poll_for_ack - Wait for message acknowledgement
  127. * @hw: pointer to the HW structure
  128. * @mbx_id: id of mailbox to write
  129. *
  130. * returns SUCCESS if it successfully received a message acknowledgement
  131. **/
  132. static s32 ixgbe_poll_for_ack(struct ixgbe_hw *hw, u16 mbx_id)
  133. {
  134. struct ixgbe_mbx_info *mbx = &hw->mbx;
  135. int countdown = mbx->timeout;
  136. if (!countdown || !mbx->ops)
  137. return IXGBE_ERR_MBX;
  138. while (mbx->ops->check_for_ack(hw, mbx_id)) {
  139. countdown--;
  140. if (!countdown)
  141. return IXGBE_ERR_MBX;
  142. udelay(mbx->usec_delay);
  143. }
  144. return 0;
  145. }
  146. /**
  147. * ixgbe_read_posted_mbx - Wait for message notification and receive message
  148. * @hw: pointer to the HW structure
  149. * @msg: The message buffer
  150. * @size: Length of buffer
  151. * @mbx_id: id of mailbox to write
  152. *
  153. * returns SUCCESS if it successfully received a message notification and
  154. * copied it into the receive buffer.
  155. **/
  156. static s32 ixgbe_read_posted_mbx(struct ixgbe_hw *hw, u32 *msg, u16 size,
  157. u16 mbx_id)
  158. {
  159. struct ixgbe_mbx_info *mbx = &hw->mbx;
  160. s32 ret_val;
  161. if (!mbx->ops)
  162. return IXGBE_ERR_MBX;
  163. ret_val = ixgbe_poll_for_msg(hw, mbx_id);
  164. if (ret_val)
  165. return ret_val;
  166. /* if ack received read message */
  167. return mbx->ops->read(hw, msg, size, mbx_id);
  168. }
  169. /**
  170. * ixgbe_write_posted_mbx - Write a message to the mailbox, wait for ack
  171. * @hw: pointer to the HW structure
  172. * @msg: The message buffer
  173. * @size: Length of buffer
  174. * @mbx_id: id of mailbox to write
  175. *
  176. * returns SUCCESS if it successfully copied message into the buffer and
  177. * received an ack to that message within delay * timeout period
  178. **/
  179. static s32 ixgbe_write_posted_mbx(struct ixgbe_hw *hw, u32 *msg, u16 size,
  180. u16 mbx_id)
  181. {
  182. struct ixgbe_mbx_info *mbx = &hw->mbx;
  183. s32 ret_val;
  184. /* exit if either we can't write or there isn't a defined timeout */
  185. if (!mbx->ops || !mbx->timeout)
  186. return IXGBE_ERR_MBX;
  187. /* send msg */
  188. ret_val = mbx->ops->write(hw, msg, size, mbx_id);
  189. if (ret_val)
  190. return ret_val;
  191. /* if msg sent wait until we receive an ack */
  192. return ixgbe_poll_for_ack(hw, mbx_id);
  193. }
  194. static s32 ixgbe_check_for_bit_pf(struct ixgbe_hw *hw, u32 mask, s32 index)
  195. {
  196. u32 mbvficr = IXGBE_READ_REG(hw, IXGBE_MBVFICR(index));
  197. if (mbvficr & mask) {
  198. IXGBE_WRITE_REG(hw, IXGBE_MBVFICR(index), mask);
  199. return 0;
  200. }
  201. return IXGBE_ERR_MBX;
  202. }
  203. /**
  204. * ixgbe_check_for_msg_pf - checks to see if the VF has sent mail
  205. * @hw: pointer to the HW structure
  206. * @vf_number: the VF index
  207. *
  208. * returns SUCCESS if the VF has set the Status bit or else ERR_MBX
  209. **/
  210. static s32 ixgbe_check_for_msg_pf(struct ixgbe_hw *hw, u16 vf_number)
  211. {
  212. s32 index = IXGBE_MBVFICR_INDEX(vf_number);
  213. u32 vf_bit = vf_number % 16;
  214. if (!ixgbe_check_for_bit_pf(hw, IXGBE_MBVFICR_VFREQ_VF1 << vf_bit,
  215. index)) {
  216. hw->mbx.stats.reqs++;
  217. return 0;
  218. }
  219. return IXGBE_ERR_MBX;
  220. }
  221. /**
  222. * ixgbe_check_for_ack_pf - checks to see if the VF has ACKed
  223. * @hw: pointer to the HW structure
  224. * @vf_number: the VF index
  225. *
  226. * returns SUCCESS if the VF has set the Status bit or else ERR_MBX
  227. **/
  228. static s32 ixgbe_check_for_ack_pf(struct ixgbe_hw *hw, u16 vf_number)
  229. {
  230. s32 index = IXGBE_MBVFICR_INDEX(vf_number);
  231. u32 vf_bit = vf_number % 16;
  232. if (!ixgbe_check_for_bit_pf(hw, IXGBE_MBVFICR_VFACK_VF1 << vf_bit,
  233. index)) {
  234. hw->mbx.stats.acks++;
  235. return 0;
  236. }
  237. return IXGBE_ERR_MBX;
  238. }
  239. /**
  240. * ixgbe_check_for_rst_pf - checks to see if the VF has reset
  241. * @hw: pointer to the HW structure
  242. * @vf_number: the VF index
  243. *
  244. * returns SUCCESS if the VF has set the Status bit or else ERR_MBX
  245. **/
  246. static s32 ixgbe_check_for_rst_pf(struct ixgbe_hw *hw, u16 vf_number)
  247. {
  248. u32 reg_offset = (vf_number < 32) ? 0 : 1;
  249. u32 vf_shift = vf_number % 32;
  250. u32 vflre = 0;
  251. switch (hw->mac.type) {
  252. case ixgbe_mac_82599EB:
  253. vflre = IXGBE_READ_REG(hw, IXGBE_VFLRE(reg_offset));
  254. break;
  255. case ixgbe_mac_X540:
  256. case ixgbe_mac_X550:
  257. case ixgbe_mac_X550EM_x:
  258. case ixgbe_mac_x550em_a:
  259. vflre = IXGBE_READ_REG(hw, IXGBE_VFLREC(reg_offset));
  260. break;
  261. default:
  262. break;
  263. }
  264. if (vflre & BIT(vf_shift)) {
  265. IXGBE_WRITE_REG(hw, IXGBE_VFLREC(reg_offset), BIT(vf_shift));
  266. hw->mbx.stats.rsts++;
  267. return 0;
  268. }
  269. return IXGBE_ERR_MBX;
  270. }
  271. /**
  272. * ixgbe_obtain_mbx_lock_pf - obtain mailbox lock
  273. * @hw: pointer to the HW structure
  274. * @vf_number: the VF index
  275. *
  276. * return SUCCESS if we obtained the mailbox lock
  277. **/
  278. static s32 ixgbe_obtain_mbx_lock_pf(struct ixgbe_hw *hw, u16 vf_number)
  279. {
  280. u32 p2v_mailbox;
  281. /* Take ownership of the buffer */
  282. IXGBE_WRITE_REG(hw, IXGBE_PFMAILBOX(vf_number), IXGBE_PFMAILBOX_PFU);
  283. /* reserve mailbox for vf use */
  284. p2v_mailbox = IXGBE_READ_REG(hw, IXGBE_PFMAILBOX(vf_number));
  285. if (p2v_mailbox & IXGBE_PFMAILBOX_PFU)
  286. return 0;
  287. return IXGBE_ERR_MBX;
  288. }
  289. /**
  290. * ixgbe_write_mbx_pf - Places a message in the mailbox
  291. * @hw: pointer to the HW structure
  292. * @msg: The message buffer
  293. * @size: Length of buffer
  294. * @vf_number: the VF index
  295. *
  296. * returns SUCCESS if it successfully copied message into the buffer
  297. **/
  298. static s32 ixgbe_write_mbx_pf(struct ixgbe_hw *hw, u32 *msg, u16 size,
  299. u16 vf_number)
  300. {
  301. s32 ret_val;
  302. u16 i;
  303. /* lock the mailbox to prevent pf/vf race condition */
  304. ret_val = ixgbe_obtain_mbx_lock_pf(hw, vf_number);
  305. if (ret_val)
  306. return ret_val;
  307. /* flush msg and acks as we are overwriting the message buffer */
  308. ixgbe_check_for_msg_pf(hw, vf_number);
  309. ixgbe_check_for_ack_pf(hw, vf_number);
  310. /* copy the caller specified message to the mailbox memory buffer */
  311. for (i = 0; i < size; i++)
  312. IXGBE_WRITE_REG_ARRAY(hw, IXGBE_PFMBMEM(vf_number), i, msg[i]);
  313. /* Interrupt VF to tell it a message has been sent and release buffer*/
  314. IXGBE_WRITE_REG(hw, IXGBE_PFMAILBOX(vf_number), IXGBE_PFMAILBOX_STS);
  315. /* update stats */
  316. hw->mbx.stats.msgs_tx++;
  317. return 0;
  318. }
  319. /**
  320. * ixgbe_read_mbx_pf - Read a message from the mailbox
  321. * @hw: pointer to the HW structure
  322. * @msg: The message buffer
  323. * @size: Length of buffer
  324. * @vf_number: the VF index
  325. *
  326. * This function copies a message from the mailbox buffer to the caller's
  327. * memory buffer. The presumption is that the caller knows that there was
  328. * a message due to a VF request so no polling for message is needed.
  329. **/
  330. static s32 ixgbe_read_mbx_pf(struct ixgbe_hw *hw, u32 *msg, u16 size,
  331. u16 vf_number)
  332. {
  333. s32 ret_val;
  334. u16 i;
  335. /* lock the mailbox to prevent pf/vf race condition */
  336. ret_val = ixgbe_obtain_mbx_lock_pf(hw, vf_number);
  337. if (ret_val)
  338. return ret_val;
  339. /* copy the message to the mailbox memory buffer */
  340. for (i = 0; i < size; i++)
  341. msg[i] = IXGBE_READ_REG_ARRAY(hw, IXGBE_PFMBMEM(vf_number), i);
  342. /* Acknowledge the message and release buffer */
  343. IXGBE_WRITE_REG(hw, IXGBE_PFMAILBOX(vf_number), IXGBE_PFMAILBOX_ACK);
  344. /* update stats */
  345. hw->mbx.stats.msgs_rx++;
  346. return 0;
  347. }
  348. #ifdef CONFIG_PCI_IOV
  349. /**
  350. * ixgbe_init_mbx_params_pf - set initial values for pf mailbox
  351. * @hw: pointer to the HW structure
  352. *
  353. * Initializes the hw->mbx struct to correct values for pf mailbox
  354. */
  355. void ixgbe_init_mbx_params_pf(struct ixgbe_hw *hw)
  356. {
  357. struct ixgbe_mbx_info *mbx = &hw->mbx;
  358. if (hw->mac.type != ixgbe_mac_82599EB &&
  359. hw->mac.type != ixgbe_mac_X550 &&
  360. hw->mac.type != ixgbe_mac_X550EM_x &&
  361. hw->mac.type != ixgbe_mac_x550em_a &&
  362. hw->mac.type != ixgbe_mac_X540)
  363. return;
  364. mbx->timeout = 0;
  365. mbx->usec_delay = 0;
  366. mbx->stats.msgs_tx = 0;
  367. mbx->stats.msgs_rx = 0;
  368. mbx->stats.reqs = 0;
  369. mbx->stats.acks = 0;
  370. mbx->stats.rsts = 0;
  371. mbx->size = IXGBE_VFMAILBOX_SIZE;
  372. }
  373. #endif /* CONFIG_PCI_IOV */
  374. const struct ixgbe_mbx_operations mbx_ops_generic = {
  375. .read = ixgbe_read_mbx_pf,
  376. .write = ixgbe_write_mbx_pf,
  377. .read_posted = ixgbe_read_posted_mbx,
  378. .write_posted = ixgbe_write_posted_mbx,
  379. .check_for_msg = ixgbe_check_for_msg_pf,
  380. .check_for_ack = ixgbe_check_for_ack_pf,
  381. .check_for_rst = ixgbe_check_for_rst_pf,
  382. };