ixgbe_mbx.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. /*******************************************************************************
  2. Intel 10 Gigabit PCI Express Linux driver
  3. Copyright(c) 1999 - 2014 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.read)
  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.write)
  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.check_for_msg)
  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.check_for_ack)
  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.check_for_rst)
  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.check_for_msg)
  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.check_for_ack)
  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.read)
  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.write || !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. vflre = IXGBE_READ_REG(hw, IXGBE_VFLREC(reg_offset));
  259. break;
  260. default:
  261. break;
  262. }
  263. if (vflre & (1 << vf_shift)) {
  264. IXGBE_WRITE_REG(hw, IXGBE_VFLREC(reg_offset), (1 << vf_shift));
  265. hw->mbx.stats.rsts++;
  266. return 0;
  267. }
  268. return IXGBE_ERR_MBX;
  269. }
  270. /**
  271. * ixgbe_obtain_mbx_lock_pf - obtain mailbox lock
  272. * @hw: pointer to the HW structure
  273. * @vf_number: the VF index
  274. *
  275. * return SUCCESS if we obtained the mailbox lock
  276. **/
  277. static s32 ixgbe_obtain_mbx_lock_pf(struct ixgbe_hw *hw, u16 vf_number)
  278. {
  279. u32 p2v_mailbox;
  280. /* Take ownership of the buffer */
  281. IXGBE_WRITE_REG(hw, IXGBE_PFMAILBOX(vf_number), IXGBE_PFMAILBOX_PFU);
  282. /* reserve mailbox for vf use */
  283. p2v_mailbox = IXGBE_READ_REG(hw, IXGBE_PFMAILBOX(vf_number));
  284. if (p2v_mailbox & IXGBE_PFMAILBOX_PFU)
  285. return 0;
  286. return IXGBE_ERR_MBX;
  287. }
  288. /**
  289. * ixgbe_write_mbx_pf - Places a message in the mailbox
  290. * @hw: pointer to the HW structure
  291. * @msg: The message buffer
  292. * @size: Length of buffer
  293. * @vf_number: the VF index
  294. *
  295. * returns SUCCESS if it successfully copied message into the buffer
  296. **/
  297. static s32 ixgbe_write_mbx_pf(struct ixgbe_hw *hw, u32 *msg, u16 size,
  298. u16 vf_number)
  299. {
  300. s32 ret_val;
  301. u16 i;
  302. /* lock the mailbox to prevent pf/vf race condition */
  303. ret_val = ixgbe_obtain_mbx_lock_pf(hw, vf_number);
  304. if (ret_val)
  305. return ret_val;
  306. /* flush msg and acks as we are overwriting the message buffer */
  307. ixgbe_check_for_msg_pf(hw, vf_number);
  308. ixgbe_check_for_ack_pf(hw, vf_number);
  309. /* copy the caller specified message to the mailbox memory buffer */
  310. for (i = 0; i < size; i++)
  311. IXGBE_WRITE_REG_ARRAY(hw, IXGBE_PFMBMEM(vf_number), i, msg[i]);
  312. /* Interrupt VF to tell it a message has been sent and release buffer*/
  313. IXGBE_WRITE_REG(hw, IXGBE_PFMAILBOX(vf_number), IXGBE_PFMAILBOX_STS);
  314. /* update stats */
  315. hw->mbx.stats.msgs_tx++;
  316. return 0;
  317. }
  318. /**
  319. * ixgbe_read_mbx_pf - Read a message from the mailbox
  320. * @hw: pointer to the HW structure
  321. * @msg: The message buffer
  322. * @size: Length of buffer
  323. * @vf_number: the VF index
  324. *
  325. * This function copies a message from the mailbox buffer to the caller's
  326. * memory buffer. The presumption is that the caller knows that there was
  327. * a message due to a VF request so no polling for message is needed.
  328. **/
  329. static s32 ixgbe_read_mbx_pf(struct ixgbe_hw *hw, u32 *msg, u16 size,
  330. u16 vf_number)
  331. {
  332. s32 ret_val;
  333. u16 i;
  334. /* lock the mailbox to prevent pf/vf race condition */
  335. ret_val = ixgbe_obtain_mbx_lock_pf(hw, vf_number);
  336. if (ret_val)
  337. return ret_val;
  338. /* copy the message to the mailbox memory buffer */
  339. for (i = 0; i < size; i++)
  340. msg[i] = IXGBE_READ_REG_ARRAY(hw, IXGBE_PFMBMEM(vf_number), i);
  341. /* Acknowledge the message and release buffer */
  342. IXGBE_WRITE_REG(hw, IXGBE_PFMAILBOX(vf_number), IXGBE_PFMAILBOX_ACK);
  343. /* update stats */
  344. hw->mbx.stats.msgs_rx++;
  345. return 0;
  346. }
  347. #ifdef CONFIG_PCI_IOV
  348. /**
  349. * ixgbe_init_mbx_params_pf - set initial values for pf mailbox
  350. * @hw: pointer to the HW structure
  351. *
  352. * Initializes the hw->mbx struct to correct values for pf mailbox
  353. */
  354. void ixgbe_init_mbx_params_pf(struct ixgbe_hw *hw)
  355. {
  356. struct ixgbe_mbx_info *mbx = &hw->mbx;
  357. if (hw->mac.type != ixgbe_mac_82599EB &&
  358. hw->mac.type != ixgbe_mac_X550 &&
  359. hw->mac.type != ixgbe_mac_X550EM_x &&
  360. hw->mac.type != ixgbe_mac_X540)
  361. return;
  362. mbx->timeout = 0;
  363. mbx->usec_delay = 0;
  364. mbx->stats.msgs_tx = 0;
  365. mbx->stats.msgs_rx = 0;
  366. mbx->stats.reqs = 0;
  367. mbx->stats.acks = 0;
  368. mbx->stats.rsts = 0;
  369. mbx->size = IXGBE_VFMAILBOX_SIZE;
  370. }
  371. #endif /* CONFIG_PCI_IOV */
  372. struct ixgbe_mbx_operations mbx_ops_generic = {
  373. .read = ixgbe_read_mbx_pf,
  374. .write = ixgbe_write_mbx_pf,
  375. .read_posted = ixgbe_read_posted_mbx,
  376. .write_posted = ixgbe_write_posted_mbx,
  377. .check_for_msg = ixgbe_check_for_msg_pf,
  378. .check_for_ack = ixgbe_check_for_ack_pf,
  379. .check_for_rst = ixgbe_check_for_rst_pf,
  380. };