/drivers/net/ethernet/intel/igbvf/vf.c

http://github.com/mirrors/linux · C · 423 lines · 217 code · 69 blank · 137 comment · 35 complexity · 09b1b626317bc15fff478011f37f80b2 MD5 · raw file

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