ixgbe_mbx.c 12 KB

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