e1000_mbx.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. /* Intel(R) Gigabit Ethernet Linux driver
  2. * Copyright(c) 2007-2014 Intel Corporation.
  3. *
  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. *
  8. * This program is distributed in the hope it will be useful, but WITHOUT
  9. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. * more details.
  12. *
  13. * You should have received a copy of the GNU General Public License along with
  14. * this program; if not, see <http://www.gnu.org/licenses/>.
  15. *
  16. * The full GNU General Public License is included in this distribution in
  17. * the file called "COPYING".
  18. *
  19. * Contact Information:
  20. * e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
  21. * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  22. */
  23. #include "e1000_mbx.h"
  24. /**
  25. * igb_read_mbx - Reads a message from the mailbox
  26. * @hw: pointer to the HW structure
  27. * @msg: The message buffer
  28. * @size: Length of buffer
  29. * @mbx_id: id of mailbox to read
  30. *
  31. * returns SUCCESS if it successfully read message from buffer
  32. **/
  33. s32 igb_read_mbx(struct e1000_hw *hw, u32 *msg, u16 size, u16 mbx_id,
  34. bool unlock)
  35. {
  36. struct e1000_mbx_info *mbx = &hw->mbx;
  37. s32 ret_val = -E1000_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, unlock);
  43. return ret_val;
  44. }
  45. /**
  46. * igb_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 igb_write_mbx(struct e1000_hw *hw, u32 *msg, u16 size, u16 mbx_id)
  55. {
  56. struct e1000_mbx_info *mbx = &hw->mbx;
  57. s32 ret_val = 0;
  58. if (size > mbx->size)
  59. ret_val = -E1000_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. * igb_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 igb_check_for_msg(struct e1000_hw *hw, u16 mbx_id)
  72. {
  73. struct e1000_mbx_info *mbx = &hw->mbx;
  74. s32 ret_val = -E1000_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. * igb_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 igb_check_for_ack(struct e1000_hw *hw, u16 mbx_id)
  87. {
  88. struct e1000_mbx_info *mbx = &hw->mbx;
  89. s32 ret_val = -E1000_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. * igb_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 igb_check_for_rst(struct e1000_hw *hw, u16 mbx_id)
  102. {
  103. struct e1000_mbx_info *mbx = &hw->mbx;
  104. s32 ret_val = -E1000_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. * igb_unlock_mbx - unlock the mailbox
  111. * @hw: pointer to the HW structure
  112. * @mbx_id: id of mailbox to check
  113. *
  114. * returns SUCCESS if the mailbox was unlocked or else ERR_MBX
  115. **/
  116. s32 igb_unlock_mbx(struct e1000_hw *hw, u16 mbx_id)
  117. {
  118. struct e1000_mbx_info *mbx = &hw->mbx;
  119. s32 ret_val = -E1000_ERR_MBX;
  120. if (mbx->ops.unlock)
  121. ret_val = mbx->ops.unlock(hw, mbx_id);
  122. return ret_val;
  123. }
  124. /**
  125. * igb_poll_for_msg - Wait for message notification
  126. * @hw: pointer to the HW structure
  127. * @mbx_id: id of mailbox to write
  128. *
  129. * returns SUCCESS if it successfully received a message notification
  130. **/
  131. static s32 igb_poll_for_msg(struct e1000_hw *hw, u16 mbx_id)
  132. {
  133. struct e1000_mbx_info *mbx = &hw->mbx;
  134. int countdown = mbx->timeout;
  135. if (!countdown || !mbx->ops.check_for_msg)
  136. goto out;
  137. while (countdown && mbx->ops.check_for_msg(hw, mbx_id)) {
  138. countdown--;
  139. if (!countdown)
  140. break;
  141. udelay(mbx->usec_delay);
  142. }
  143. /* if we failed, all future posted messages fail until reset */
  144. if (!countdown)
  145. mbx->timeout = 0;
  146. out:
  147. return countdown ? 0 : -E1000_ERR_MBX;
  148. }
  149. /**
  150. * igb_poll_for_ack - Wait for message acknowledgement
  151. * @hw: pointer to the HW structure
  152. * @mbx_id: id of mailbox to write
  153. *
  154. * returns SUCCESS if it successfully received a message acknowledgement
  155. **/
  156. static s32 igb_poll_for_ack(struct e1000_hw *hw, u16 mbx_id)
  157. {
  158. struct e1000_mbx_info *mbx = &hw->mbx;
  159. int countdown = mbx->timeout;
  160. if (!countdown || !mbx->ops.check_for_ack)
  161. goto out;
  162. while (countdown && mbx->ops.check_for_ack(hw, mbx_id)) {
  163. countdown--;
  164. if (!countdown)
  165. break;
  166. udelay(mbx->usec_delay);
  167. }
  168. /* if we failed, all future posted messages fail until reset */
  169. if (!countdown)
  170. mbx->timeout = 0;
  171. out:
  172. return countdown ? 0 : -E1000_ERR_MBX;
  173. }
  174. /**
  175. * igb_read_posted_mbx - Wait for message notification and receive message
  176. * @hw: pointer to the HW structure
  177. * @msg: The message buffer
  178. * @size: Length of buffer
  179. * @mbx_id: id of mailbox to write
  180. *
  181. * returns SUCCESS if it successfully received a message notification and
  182. * copied it into the receive buffer.
  183. **/
  184. static s32 igb_read_posted_mbx(struct e1000_hw *hw, u32 *msg, u16 size,
  185. u16 mbx_id)
  186. {
  187. struct e1000_mbx_info *mbx = &hw->mbx;
  188. s32 ret_val = -E1000_ERR_MBX;
  189. if (!mbx->ops.read)
  190. goto out;
  191. ret_val = igb_poll_for_msg(hw, mbx_id);
  192. if (!ret_val)
  193. ret_val = mbx->ops.read(hw, msg, size, mbx_id, true);
  194. out:
  195. return ret_val;
  196. }
  197. /**
  198. * igb_write_posted_mbx - Write a message to the mailbox, wait for ack
  199. * @hw: pointer to the HW structure
  200. * @msg: The message buffer
  201. * @size: Length of buffer
  202. * @mbx_id: id of mailbox to write
  203. *
  204. * returns SUCCESS if it successfully copied message into the buffer and
  205. * received an ack to that message within delay * timeout period
  206. **/
  207. static s32 igb_write_posted_mbx(struct e1000_hw *hw, u32 *msg, u16 size,
  208. u16 mbx_id)
  209. {
  210. struct e1000_mbx_info *mbx = &hw->mbx;
  211. s32 ret_val = -E1000_ERR_MBX;
  212. /* exit if either we can't write or there isn't a defined timeout */
  213. if (!mbx->ops.write || !mbx->timeout)
  214. goto out;
  215. /* send msg */
  216. ret_val = mbx->ops.write(hw, msg, size, mbx_id);
  217. /* if msg sent wait until we receive an ack */
  218. if (!ret_val)
  219. ret_val = igb_poll_for_ack(hw, mbx_id);
  220. out:
  221. return ret_val;
  222. }
  223. static s32 igb_check_for_bit_pf(struct e1000_hw *hw, u32 mask)
  224. {
  225. u32 mbvficr = rd32(E1000_MBVFICR);
  226. s32 ret_val = -E1000_ERR_MBX;
  227. if (mbvficr & mask) {
  228. ret_val = 0;
  229. wr32(E1000_MBVFICR, mask);
  230. }
  231. return ret_val;
  232. }
  233. /**
  234. * igb_check_for_msg_pf - checks to see if the VF has sent mail
  235. * @hw: pointer to the HW structure
  236. * @vf_number: the VF index
  237. *
  238. * returns SUCCESS if the VF has set the Status bit or else ERR_MBX
  239. **/
  240. static s32 igb_check_for_msg_pf(struct e1000_hw *hw, u16 vf_number)
  241. {
  242. s32 ret_val = -E1000_ERR_MBX;
  243. if (!igb_check_for_bit_pf(hw, E1000_MBVFICR_VFREQ_VF1 << vf_number)) {
  244. ret_val = 0;
  245. hw->mbx.stats.reqs++;
  246. }
  247. return ret_val;
  248. }
  249. /**
  250. * igb_check_for_ack_pf - checks to see if the VF has ACKed
  251. * @hw: pointer to the HW structure
  252. * @vf_number: the VF index
  253. *
  254. * returns SUCCESS if the VF has set the Status bit or else ERR_MBX
  255. **/
  256. static s32 igb_check_for_ack_pf(struct e1000_hw *hw, u16 vf_number)
  257. {
  258. s32 ret_val = -E1000_ERR_MBX;
  259. if (!igb_check_for_bit_pf(hw, E1000_MBVFICR_VFACK_VF1 << vf_number)) {
  260. ret_val = 0;
  261. hw->mbx.stats.acks++;
  262. }
  263. return ret_val;
  264. }
  265. /**
  266. * igb_check_for_rst_pf - checks to see if the VF has reset
  267. * @hw: pointer to the HW structure
  268. * @vf_number: the VF index
  269. *
  270. * returns SUCCESS if the VF has set the Status bit or else ERR_MBX
  271. **/
  272. static s32 igb_check_for_rst_pf(struct e1000_hw *hw, u16 vf_number)
  273. {
  274. u32 vflre = rd32(E1000_VFLRE);
  275. s32 ret_val = -E1000_ERR_MBX;
  276. if (vflre & BIT(vf_number)) {
  277. ret_val = 0;
  278. wr32(E1000_VFLRE, BIT(vf_number));
  279. hw->mbx.stats.rsts++;
  280. }
  281. return ret_val;
  282. }
  283. /**
  284. * igb_obtain_mbx_lock_pf - obtain mailbox lock
  285. * @hw: pointer to the HW structure
  286. * @vf_number: the VF index
  287. *
  288. * return SUCCESS if we obtained the mailbox lock
  289. **/
  290. static s32 igb_obtain_mbx_lock_pf(struct e1000_hw *hw, u16 vf_number)
  291. {
  292. s32 ret_val = -E1000_ERR_MBX;
  293. u32 p2v_mailbox;
  294. int count = 10;
  295. do {
  296. /* Take ownership of the buffer */
  297. wr32(E1000_P2VMAILBOX(vf_number), E1000_P2VMAILBOX_PFU);
  298. /* reserve mailbox for vf use */
  299. p2v_mailbox = rd32(E1000_P2VMAILBOX(vf_number));
  300. if (p2v_mailbox & E1000_P2VMAILBOX_PFU) {
  301. ret_val = 0;
  302. break;
  303. }
  304. udelay(1000);
  305. } while (count-- > 0);
  306. return ret_val;
  307. }
  308. /**
  309. * igb_release_mbx_lock_pf - release mailbox lock
  310. * @hw: pointer to the HW structure
  311. * @vf_number: the VF index
  312. *
  313. * return SUCCESS if we released the mailbox lock
  314. **/
  315. static s32 igb_release_mbx_lock_pf(struct e1000_hw *hw, u16 vf_number)
  316. {
  317. u32 p2v_mailbox;
  318. /* drop PF lock of mailbox, if set */
  319. p2v_mailbox = rd32(E1000_P2VMAILBOX(vf_number));
  320. if (p2v_mailbox & E1000_P2VMAILBOX_PFU)
  321. wr32(E1000_P2VMAILBOX(vf_number),
  322. p2v_mailbox & ~E1000_P2VMAILBOX_PFU);
  323. return 0;
  324. }
  325. /**
  326. * igb_write_mbx_pf - Places a message in the mailbox
  327. * @hw: pointer to the HW structure
  328. * @msg: The message buffer
  329. * @size: Length of buffer
  330. * @vf_number: the VF index
  331. *
  332. * returns SUCCESS if it successfully copied message into the buffer
  333. **/
  334. static s32 igb_write_mbx_pf(struct e1000_hw *hw, u32 *msg, u16 size,
  335. u16 vf_number)
  336. {
  337. s32 ret_val;
  338. u16 i;
  339. /* lock the mailbox to prevent pf/vf race condition */
  340. ret_val = igb_obtain_mbx_lock_pf(hw, vf_number);
  341. if (ret_val)
  342. goto out_no_write;
  343. /* flush msg and acks as we are overwriting the message buffer */
  344. igb_check_for_msg_pf(hw, vf_number);
  345. igb_check_for_ack_pf(hw, vf_number);
  346. /* copy the caller specified message to the mailbox memory buffer */
  347. for (i = 0; i < size; i++)
  348. array_wr32(E1000_VMBMEM(vf_number), i, msg[i]);
  349. /* Interrupt VF to tell it a message has been sent and release buffer*/
  350. wr32(E1000_P2VMAILBOX(vf_number), E1000_P2VMAILBOX_STS);
  351. /* update stats */
  352. hw->mbx.stats.msgs_tx++;
  353. out_no_write:
  354. return ret_val;
  355. }
  356. /**
  357. * igb_read_mbx_pf - Read a message from the mailbox
  358. * @hw: pointer to the HW structure
  359. * @msg: The message buffer
  360. * @size: Length of buffer
  361. * @vf_number: the VF index
  362. * @unlock: unlock the mailbox when done?
  363. *
  364. * This function copies a message from the mailbox buffer to the caller's
  365. * memory buffer. The presumption is that the caller knows that there was
  366. * a message due to a VF request so no polling for message is needed.
  367. **/
  368. static s32 igb_read_mbx_pf(struct e1000_hw *hw, u32 *msg, u16 size,
  369. u16 vf_number, bool unlock)
  370. {
  371. s32 ret_val;
  372. u16 i;
  373. /* lock the mailbox to prevent pf/vf race condition */
  374. ret_val = igb_obtain_mbx_lock_pf(hw, vf_number);
  375. if (ret_val)
  376. goto out_no_read;
  377. /* copy the message to the mailbox memory buffer */
  378. for (i = 0; i < size; i++)
  379. msg[i] = array_rd32(E1000_VMBMEM(vf_number), i);
  380. /* Acknowledge the message and release mailbox lock (or not) */
  381. if (unlock)
  382. wr32(E1000_P2VMAILBOX(vf_number), E1000_P2VMAILBOX_ACK);
  383. else
  384. wr32(E1000_P2VMAILBOX(vf_number),
  385. E1000_P2VMAILBOX_ACK | E1000_P2VMAILBOX_PFU);
  386. /* update stats */
  387. hw->mbx.stats.msgs_rx++;
  388. out_no_read:
  389. return ret_val;
  390. }
  391. /**
  392. * e1000_init_mbx_params_pf - set initial values for pf mailbox
  393. * @hw: pointer to the HW structure
  394. *
  395. * Initializes the hw->mbx struct to correct values for pf mailbox
  396. */
  397. s32 igb_init_mbx_params_pf(struct e1000_hw *hw)
  398. {
  399. struct e1000_mbx_info *mbx = &hw->mbx;
  400. mbx->timeout = 0;
  401. mbx->usec_delay = 0;
  402. mbx->size = E1000_VFMAILBOX_SIZE;
  403. mbx->ops.read = igb_read_mbx_pf;
  404. mbx->ops.write = igb_write_mbx_pf;
  405. mbx->ops.read_posted = igb_read_posted_mbx;
  406. mbx->ops.write_posted = igb_write_posted_mbx;
  407. mbx->ops.check_for_msg = igb_check_for_msg_pf;
  408. mbx->ops.check_for_ack = igb_check_for_ack_pf;
  409. mbx->ops.check_for_rst = igb_check_for_rst_pf;
  410. mbx->ops.unlock = igb_release_mbx_lock_pf;
  411. mbx->stats.msgs_tx = 0;
  412. mbx->stats.msgs_rx = 0;
  413. mbx->stats.reqs = 0;
  414. mbx->stats.acks = 0;
  415. mbx->stats.rsts = 0;
  416. return 0;
  417. }