vf.c 13 KB

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