vf.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. /*******************************************************************************
  2. Intel(R) 82576 Virtual Function Linux driver
  3. Copyright(c) 2009 - 2012 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, see <http://www.gnu.org/licenses/>.
  13. The full GNU General Public License is included in this distribution in
  14. the file called "COPYING".
  15. Contact Information:
  16. e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
  17. Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  18. *******************************************************************************/
  19. #include "vf.h"
  20. static s32 e1000_check_for_link_vf(struct e1000_hw *hw);
  21. static s32 e1000_get_link_up_info_vf(struct e1000_hw *hw, u16 *speed,
  22. u16 *duplex);
  23. static s32 e1000_init_hw_vf(struct e1000_hw *hw);
  24. static s32 e1000_reset_hw_vf(struct e1000_hw *hw);
  25. static void e1000_update_mc_addr_list_vf(struct e1000_hw *hw, u8 *,
  26. u32, u32, u32);
  27. static void e1000_rar_set_vf(struct e1000_hw *, u8 *, u32);
  28. static s32 e1000_read_mac_addr_vf(struct e1000_hw *);
  29. static s32 e1000_set_vfta_vf(struct e1000_hw *, u16, bool);
  30. /**
  31. * e1000_init_mac_params_vf - Inits MAC params
  32. * @hw: pointer to the HW structure
  33. **/
  34. static s32 e1000_init_mac_params_vf(struct e1000_hw *hw)
  35. {
  36. struct e1000_mac_info *mac = &hw->mac;
  37. /* VF's have no MTA Registers - PF feature only */
  38. mac->mta_reg_count = 128;
  39. /* VF's have no access to RAR entries */
  40. mac->rar_entry_count = 1;
  41. /* Function pointers */
  42. /* reset */
  43. mac->ops.reset_hw = e1000_reset_hw_vf;
  44. /* hw initialization */
  45. mac->ops.init_hw = e1000_init_hw_vf;
  46. /* check for link */
  47. mac->ops.check_for_link = e1000_check_for_link_vf;
  48. /* link info */
  49. mac->ops.get_link_up_info = e1000_get_link_up_info_vf;
  50. /* multicast address update */
  51. mac->ops.update_mc_addr_list = e1000_update_mc_addr_list_vf;
  52. /* set mac address */
  53. mac->ops.rar_set = e1000_rar_set_vf;
  54. /* read mac address */
  55. mac->ops.read_mac_addr = e1000_read_mac_addr_vf;
  56. /* set vlan filter table array */
  57. mac->ops.set_vfta = e1000_set_vfta_vf;
  58. return E1000_SUCCESS;
  59. }
  60. /**
  61. * e1000_init_function_pointers_vf - Inits function pointers
  62. * @hw: pointer to the HW structure
  63. **/
  64. void e1000_init_function_pointers_vf(struct e1000_hw *hw)
  65. {
  66. hw->mac.ops.init_params = e1000_init_mac_params_vf;
  67. hw->mbx.ops.init_params = e1000_init_mbx_params_vf;
  68. }
  69. /**
  70. * e1000_get_link_up_info_vf - Gets link info.
  71. * @hw: pointer to the HW structure
  72. * @speed: pointer to 16 bit value to store link speed.
  73. * @duplex: pointer to 16 bit value to store duplex.
  74. *
  75. * Since we cannot read the PHY and get accurate link info, we must rely upon
  76. * the status register's data which is often stale and inaccurate.
  77. **/
  78. static s32 e1000_get_link_up_info_vf(struct e1000_hw *hw, u16 *speed,
  79. u16 *duplex)
  80. {
  81. s32 status;
  82. status = er32(STATUS);
  83. if (status & E1000_STATUS_SPEED_1000)
  84. *speed = SPEED_1000;
  85. else if (status & E1000_STATUS_SPEED_100)
  86. *speed = SPEED_100;
  87. else
  88. *speed = SPEED_10;
  89. if (status & E1000_STATUS_FD)
  90. *duplex = FULL_DUPLEX;
  91. else
  92. *duplex = HALF_DUPLEX;
  93. return E1000_SUCCESS;
  94. }
  95. /**
  96. * e1000_reset_hw_vf - Resets the HW
  97. * @hw: pointer to the HW structure
  98. *
  99. * VF's provide a function level reset. This is done using bit 26 of ctrl_reg.
  100. * This is all the reset we can perform on a VF.
  101. **/
  102. static s32 e1000_reset_hw_vf(struct e1000_hw *hw)
  103. {
  104. struct e1000_mbx_info *mbx = &hw->mbx;
  105. u32 timeout = E1000_VF_INIT_TIMEOUT;
  106. u32 ret_val = -E1000_ERR_MAC_INIT;
  107. u32 msgbuf[3];
  108. u8 *addr = (u8 *)(&msgbuf[1]);
  109. u32 ctrl;
  110. /* assert VF queue/interrupt reset */
  111. ctrl = er32(CTRL);
  112. ew32(CTRL, ctrl | E1000_CTRL_RST);
  113. /* we cannot initialize while the RSTI / RSTD bits are asserted */
  114. while (!mbx->ops.check_for_rst(hw) && timeout) {
  115. timeout--;
  116. udelay(5);
  117. }
  118. if (timeout) {
  119. /* mailbox timeout can now become active */
  120. mbx->timeout = E1000_VF_MBX_INIT_TIMEOUT;
  121. /* notify PF of VF reset completion */
  122. msgbuf[0] = E1000_VF_RESET;
  123. mbx->ops.write_posted(hw, msgbuf, 1);
  124. msleep(10);
  125. /* set our "perm_addr" based on info provided by PF */
  126. ret_val = mbx->ops.read_posted(hw, msgbuf, 3);
  127. if (!ret_val) {
  128. if (msgbuf[0] == (E1000_VF_RESET |
  129. E1000_VT_MSGTYPE_ACK))
  130. memcpy(hw->mac.perm_addr, addr, ETH_ALEN);
  131. else
  132. ret_val = -E1000_ERR_MAC_INIT;
  133. }
  134. }
  135. return ret_val;
  136. }
  137. /**
  138. * e1000_init_hw_vf - Inits the HW
  139. * @hw: pointer to the HW structure
  140. *
  141. * Not much to do here except clear the PF Reset indication if there is one.
  142. **/
  143. static s32 e1000_init_hw_vf(struct e1000_hw *hw)
  144. {
  145. /* attempt to set and restore our mac address */
  146. e1000_rar_set_vf(hw, hw->mac.addr, 0);
  147. return E1000_SUCCESS;
  148. }
  149. /**
  150. * e1000_hash_mc_addr_vf - Generate a multicast hash value
  151. * @hw: pointer to the HW structure
  152. * @mc_addr: pointer to a multicast address
  153. *
  154. * Generates a multicast address hash value which is used to determine
  155. * the multicast filter table array address and new table value. See
  156. * e1000_mta_set_generic()
  157. **/
  158. static u32 e1000_hash_mc_addr_vf(struct e1000_hw *hw, u8 *mc_addr)
  159. {
  160. u32 hash_value, hash_mask;
  161. u8 bit_shift = 0;
  162. /* Register count multiplied by bits per register */
  163. hash_mask = (hw->mac.mta_reg_count * 32) - 1;
  164. /* The bit_shift is the number of left-shifts
  165. * where 0xFF would still fall within the hash mask.
  166. */
  167. while (hash_mask >> bit_shift != 0xFF)
  168. bit_shift++;
  169. hash_value = hash_mask & (((mc_addr[4] >> (8 - bit_shift)) |
  170. (((u16)mc_addr[5]) << bit_shift)));
  171. return hash_value;
  172. }
  173. /**
  174. * e1000_update_mc_addr_list_vf - Update Multicast addresses
  175. * @hw: pointer to the HW structure
  176. * @mc_addr_list: array of multicast addresses to program
  177. * @mc_addr_count: number of multicast addresses to program
  178. * @rar_used_count: the first RAR register free to program
  179. * @rar_count: total number of supported Receive Address Registers
  180. *
  181. * Updates the Receive Address Registers and Multicast Table Array.
  182. * The caller must have a packed mc_addr_list of multicast addresses.
  183. * The parameter rar_count will usually be hw->mac.rar_entry_count
  184. * unless there are workarounds that change this.
  185. **/
  186. static void e1000_update_mc_addr_list_vf(struct e1000_hw *hw,
  187. u8 *mc_addr_list, u32 mc_addr_count,
  188. u32 rar_used_count, u32 rar_count)
  189. {
  190. struct e1000_mbx_info *mbx = &hw->mbx;
  191. u32 msgbuf[E1000_VFMAILBOX_SIZE];
  192. u16 *hash_list = (u16 *)&msgbuf[1];
  193. u32 hash_value;
  194. u32 cnt, i;
  195. /* Each entry in the list uses 1 16 bit word. We have 30
  196. * 16 bit words available in our HW msg buffer (minus 1 for the
  197. * msg type). That's 30 hash values if we pack 'em right. If
  198. * there are more than 30 MC addresses to add then punt the
  199. * extras for now and then add code to handle more than 30 later.
  200. * It would be unusual for a server to request that many multi-cast
  201. * addresses except for in large enterprise network environments.
  202. */
  203. cnt = (mc_addr_count > 30) ? 30 : mc_addr_count;
  204. msgbuf[0] = E1000_VF_SET_MULTICAST;
  205. msgbuf[0] |= cnt << E1000_VT_MSGINFO_SHIFT;
  206. for (i = 0; i < cnt; i++) {
  207. hash_value = e1000_hash_mc_addr_vf(hw, mc_addr_list);
  208. hash_list[i] = hash_value & 0x0FFFF;
  209. mc_addr_list += ETH_ALEN;
  210. }
  211. mbx->ops.write_posted(hw, msgbuf, E1000_VFMAILBOX_SIZE);
  212. }
  213. /**
  214. * e1000_set_vfta_vf - Set/Unset vlan filter table address
  215. * @hw: pointer to the HW structure
  216. * @vid: determines the vfta register and bit to set/unset
  217. * @set: if true then set bit, else clear bit
  218. **/
  219. static s32 e1000_set_vfta_vf(struct e1000_hw *hw, u16 vid, bool set)
  220. {
  221. struct e1000_mbx_info *mbx = &hw->mbx;
  222. u32 msgbuf[2];
  223. s32 err;
  224. msgbuf[0] = E1000_VF_SET_VLAN;
  225. msgbuf[1] = vid;
  226. /* Setting the 8 bit field MSG INFO to true indicates "add" */
  227. if (set)
  228. msgbuf[0] |= BIT(E1000_VT_MSGINFO_SHIFT);
  229. mbx->ops.write_posted(hw, msgbuf, 2);
  230. err = mbx->ops.read_posted(hw, msgbuf, 2);
  231. msgbuf[0] &= ~E1000_VT_MSGTYPE_CTS;
  232. /* if nacked the vlan was rejected */
  233. if (!err && (msgbuf[0] == (E1000_VF_SET_VLAN | E1000_VT_MSGTYPE_NACK)))
  234. err = -E1000_ERR_MAC_INIT;
  235. return err;
  236. }
  237. /**
  238. * e1000_rlpml_set_vf - Set the maximum receive packet length
  239. * @hw: pointer to the HW structure
  240. * @max_size: value to assign to max frame size
  241. **/
  242. void e1000_rlpml_set_vf(struct e1000_hw *hw, u16 max_size)
  243. {
  244. struct e1000_mbx_info *mbx = &hw->mbx;
  245. u32 msgbuf[2];
  246. msgbuf[0] = E1000_VF_SET_LPE;
  247. msgbuf[1] = max_size;
  248. mbx->ops.write_posted(hw, msgbuf, 2);
  249. }
  250. /**
  251. * e1000_rar_set_vf - set device MAC address
  252. * @hw: pointer to the HW structure
  253. * @addr: pointer to the receive address
  254. * @index: receive address array register
  255. **/
  256. static void e1000_rar_set_vf(struct e1000_hw *hw, u8 *addr, u32 index)
  257. {
  258. struct e1000_mbx_info *mbx = &hw->mbx;
  259. u32 msgbuf[3];
  260. u8 *msg_addr = (u8 *)(&msgbuf[1]);
  261. s32 ret_val;
  262. memset(msgbuf, 0, 12);
  263. msgbuf[0] = E1000_VF_SET_MAC_ADDR;
  264. memcpy(msg_addr, addr, ETH_ALEN);
  265. ret_val = mbx->ops.write_posted(hw, msgbuf, 3);
  266. if (!ret_val)
  267. ret_val = mbx->ops.read_posted(hw, msgbuf, 3);
  268. msgbuf[0] &= ~E1000_VT_MSGTYPE_CTS;
  269. /* if nacked the address was rejected, use "perm_addr" */
  270. if (!ret_val &&
  271. (msgbuf[0] == (E1000_VF_SET_MAC_ADDR | E1000_VT_MSGTYPE_NACK)))
  272. e1000_read_mac_addr_vf(hw);
  273. }
  274. /**
  275. * e1000_read_mac_addr_vf - Read device MAC address
  276. * @hw: pointer to the HW structure
  277. **/
  278. static s32 e1000_read_mac_addr_vf(struct e1000_hw *hw)
  279. {
  280. memcpy(hw->mac.addr, hw->mac.perm_addr, ETH_ALEN);
  281. return E1000_SUCCESS;
  282. }
  283. /**
  284. * e1000_check_for_link_vf - Check for link for a virtual interface
  285. * @hw: pointer to the HW structure
  286. *
  287. * Checks to see if the underlying PF is still talking to the VF and
  288. * if it is then it reports the link state to the hardware, otherwise
  289. * it reports link down and returns an error.
  290. **/
  291. static s32 e1000_check_for_link_vf(struct e1000_hw *hw)
  292. {
  293. struct e1000_mbx_info *mbx = &hw->mbx;
  294. struct e1000_mac_info *mac = &hw->mac;
  295. s32 ret_val = E1000_SUCCESS;
  296. u32 in_msg = 0;
  297. /* We only want to run this if there has been a rst asserted.
  298. * in this case that could mean a link change, device reset,
  299. * or a virtual function reset
  300. */
  301. /* If we were hit with a reset or timeout drop the link */
  302. if (!mbx->ops.check_for_rst(hw) || !mbx->timeout)
  303. mac->get_link_status = true;
  304. if (!mac->get_link_status)
  305. goto out;
  306. /* if link status is down no point in checking to see if PF is up */
  307. if (!(er32(STATUS) & E1000_STATUS_LU))
  308. goto out;
  309. /* if the read failed it could just be a mailbox collision, best wait
  310. * until we are called again and don't report an error
  311. */
  312. if (mbx->ops.read(hw, &in_msg, 1))
  313. goto out;
  314. /* if incoming message isn't clear to send we are waiting on response */
  315. if (!(in_msg & E1000_VT_MSGTYPE_CTS)) {
  316. /* msg is not CTS and is NACK we must have lost CTS status */
  317. if (in_msg & E1000_VT_MSGTYPE_NACK)
  318. ret_val = -E1000_ERR_MAC_INIT;
  319. goto out;
  320. }
  321. /* the PF is talking, if we timed out in the past we reinit */
  322. if (!mbx->timeout) {
  323. ret_val = -E1000_ERR_MAC_INIT;
  324. goto out;
  325. }
  326. /* if we passed all the tests above then the link is up and we no
  327. * longer need to check for link
  328. */
  329. mac->get_link_status = false;
  330. out:
  331. return ret_val;
  332. }