vf.c 12 KB

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