vf.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. /*******************************************************************************
  2. Intel(R) 82576 Virtual Function Linux driver
  3. Copyright(c) 2009 - 2010 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, write to the Free Software Foundation, Inc.,
  13. 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  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_vfta_vf(struct e1000_hw *, u16, bool);
  31. /**
  32. * e1000_init_mac_params_vf - Inits MAC params
  33. * @hw: pointer to the HW structure
  34. **/
  35. static s32 e1000_init_mac_params_vf(struct e1000_hw *hw)
  36. {
  37. struct e1000_mac_info *mac = &hw->mac;
  38. /* VF's have no MTA Registers - PF feature only */
  39. mac->mta_reg_count = 128;
  40. /* VF's have no access to RAR entries */
  41. mac->rar_entry_count = 1;
  42. /* Function pointers */
  43. /* reset */
  44. mac->ops.reset_hw = e1000_reset_hw_vf;
  45. /* hw initialization */
  46. mac->ops.init_hw = e1000_init_hw_vf;
  47. /* check for link */
  48. mac->ops.check_for_link = e1000_check_for_link_vf;
  49. /* link info */
  50. mac->ops.get_link_up_info = e1000_get_link_up_info_vf;
  51. /* multicast address update */
  52. mac->ops.update_mc_addr_list = e1000_update_mc_addr_list_vf;
  53. /* set mac address */
  54. mac->ops.rar_set = e1000_rar_set_vf;
  55. /* read mac address */
  56. mac->ops.read_mac_addr = e1000_read_mac_addr_vf;
  57. /* set vlan filter table array */
  58. mac->ops.set_vfta = e1000_set_vfta_vf;
  59. return E1000_SUCCESS;
  60. }
  61. /**
  62. * e1000_init_function_pointers_vf - Inits function pointers
  63. * @hw: pointer to the HW structure
  64. **/
  65. void e1000_init_function_pointers_vf(struct e1000_hw *hw)
  66. {
  67. hw->mac.ops.init_params = e1000_init_mac_params_vf;
  68. hw->mbx.ops.init_params = e1000_init_mbx_params_vf;
  69. }
  70. /**
  71. * e1000_get_link_up_info_vf - Gets link info.
  72. * @hw: pointer to the HW structure
  73. * @speed: pointer to 16 bit value to store link speed.
  74. * @duplex: pointer to 16 bit value to store duplex.
  75. *
  76. * Since we cannot read the PHY and get accurate link info, we must rely upon
  77. * the status register's data which is often stale and inaccurate.
  78. **/
  79. static s32 e1000_get_link_up_info_vf(struct e1000_hw *hw, u16 *speed,
  80. u16 *duplex)
  81. {
  82. s32 status;
  83. status = er32(STATUS);
  84. if (status & E1000_STATUS_SPEED_1000)
  85. *speed = SPEED_1000;
  86. else if (status & E1000_STATUS_SPEED_100)
  87. *speed = SPEED_100;
  88. else
  89. *speed = SPEED_10;
  90. if (status & E1000_STATUS_FD)
  91. *duplex = FULL_DUPLEX;
  92. else
  93. *duplex = HALF_DUPLEX;
  94. return E1000_SUCCESS;
  95. }
  96. /**
  97. * e1000_reset_hw_vf - Resets the HW
  98. * @hw: pointer to the HW structure
  99. *
  100. * VF's provide a function level reset. This is done using bit 26 of ctrl_reg.
  101. * This is all the reset we can perform on a VF.
  102. **/
  103. static s32 e1000_reset_hw_vf(struct e1000_hw *hw)
  104. {
  105. struct e1000_mbx_info *mbx = &hw->mbx;
  106. u32 timeout = E1000_VF_INIT_TIMEOUT;
  107. u32 ret_val = -E1000_ERR_MAC_INIT;
  108. u32 msgbuf[3];
  109. u8 *addr = (u8 *)(&msgbuf[1]);
  110. u32 ctrl;
  111. /* assert vf queue/interrupt reset */
  112. ctrl = er32(CTRL);
  113. ew32(CTRL, ctrl | E1000_CTRL_RST);
  114. /* we cannot initialize while the RSTI / RSTD bits are asserted */
  115. while (!mbx->ops.check_for_rst(hw) && timeout) {
  116. timeout--;
  117. udelay(5);
  118. }
  119. if (timeout) {
  120. /* mailbox timeout can now become active */
  121. mbx->timeout = E1000_VF_MBX_INIT_TIMEOUT;
  122. /* notify pf of vf reset completion */
  123. msgbuf[0] = E1000_VF_RESET;
  124. mbx->ops.write_posted(hw, msgbuf, 1);
  125. msleep(10);
  126. /* set our "perm_addr" based on info provided by PF */
  127. ret_val = mbx->ops.read_posted(hw, msgbuf, 3);
  128. if (!ret_val) {
  129. if (msgbuf[0] == (E1000_VF_RESET | E1000_VT_MSGTYPE_ACK))
  130. memcpy(hw->mac.perm_addr, addr, 6);
  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. /*
  165. * The bit_shift is the number of left-shifts
  166. * where 0xFF would still fall within the hash mask.
  167. */
  168. while (hash_mask >> bit_shift != 0xFF)
  169. bit_shift++;
  170. hash_value = hash_mask & (((mc_addr[4] >> (8 - bit_shift)) |
  171. (((u16) mc_addr[5]) << bit_shift)));
  172. return hash_value;
  173. }
  174. /**
  175. * e1000_update_mc_addr_list_vf - Update Multicast addresses
  176. * @hw: pointer to the HW structure
  177. * @mc_addr_list: array of multicast addresses to program
  178. * @mc_addr_count: number of multicast addresses to program
  179. * @rar_used_count: the first RAR register free to program
  180. * @rar_count: total number of supported Receive Address Registers
  181. *
  182. * Updates the Receive Address Registers and Multicast Table Array.
  183. * The caller must have a packed mc_addr_list of multicast addresses.
  184. * The parameter rar_count will usually be hw->mac.rar_entry_count
  185. * unless there are workarounds that change this.
  186. **/
  187. static void e1000_update_mc_addr_list_vf(struct e1000_hw *hw,
  188. u8 *mc_addr_list, u32 mc_addr_count,
  189. u32 rar_used_count, u32 rar_count)
  190. {
  191. struct e1000_mbx_info *mbx = &hw->mbx;
  192. u32 msgbuf[E1000_VFMAILBOX_SIZE];
  193. u16 *hash_list = (u16 *)&msgbuf[1];
  194. u32 hash_value;
  195. u32 cnt, i;
  196. /* Each entry in the list uses 1 16 bit word. We have 30
  197. * 16 bit words available in our HW msg buffer (minus 1 for the
  198. * msg type). That's 30 hash values if we pack 'em right. If
  199. * there are more than 30 MC addresses to add then punt the
  200. * extras for now and then add code to handle more than 30 later.
  201. * It would be unusual for a server to request that many multi-cast
  202. * addresses except for in large enterprise network environments.
  203. */
  204. cnt = (mc_addr_count > 30) ? 30 : mc_addr_count;
  205. msgbuf[0] = E1000_VF_SET_MULTICAST;
  206. msgbuf[0] |= cnt << E1000_VT_MSGINFO_SHIFT;
  207. for (i = 0; i < cnt; i++) {
  208. hash_value = e1000_hash_mc_addr_vf(hw, mc_addr_list);
  209. hash_list[i] = hash_value & 0x0FFFF;
  210. mc_addr_list += ETH_ADDR_LEN;
  211. }
  212. mbx->ops.write_posted(hw, msgbuf, E1000_VFMAILBOX_SIZE);
  213. }
  214. /**
  215. * e1000_set_vfta_vf - Set/Unset vlan filter table address
  216. * @hw: pointer to the HW structure
  217. * @vid: determines the vfta register and bit to set/unset
  218. * @set: if true then set bit, else clear bit
  219. **/
  220. static s32 e1000_set_vfta_vf(struct e1000_hw *hw, u16 vid, bool set)
  221. {
  222. struct e1000_mbx_info *mbx = &hw->mbx;
  223. u32 msgbuf[2];
  224. s32 err;
  225. msgbuf[0] = E1000_VF_SET_VLAN;
  226. msgbuf[1] = vid;
  227. /* Setting the 8 bit field MSG INFO to true indicates "add" */
  228. if (set)
  229. msgbuf[0] |= 1 << E1000_VT_MSGINFO_SHIFT;
  230. mbx->ops.write_posted(hw, msgbuf, 2);
  231. err = mbx->ops.read_posted(hw, msgbuf, 2);
  232. msgbuf[0] &= ~E1000_VT_MSGTYPE_CTS;
  233. /* if nacked the vlan was rejected */
  234. if (!err && (msgbuf[0] == (E1000_VF_SET_VLAN | E1000_VT_MSGTYPE_NACK)))
  235. err = -E1000_ERR_MAC_INIT;
  236. return err;
  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, 6);
  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. int i;
  281. for (i = 0; i < ETH_ADDR_LEN; i++)
  282. hw->mac.addr[i] = hw->mac.perm_addr[i];
  283. return E1000_SUCCESS;
  284. }
  285. /**
  286. * e1000_check_for_link_vf - Check for link for a virtual interface
  287. * @hw: pointer to the HW structure
  288. *
  289. * Checks to see if the underlying PF is still talking to the VF and
  290. * if it is then it reports the link state to the hardware, otherwise
  291. * it reports link down and returns an error.
  292. **/
  293. static s32 e1000_check_for_link_vf(struct e1000_hw *hw)
  294. {
  295. struct e1000_mbx_info *mbx = &hw->mbx;
  296. struct e1000_mac_info *mac = &hw->mac;
  297. s32 ret_val = E1000_SUCCESS;
  298. u32 in_msg = 0;
  299. /*
  300. * We only want to run this if there has been a rst asserted.
  301. * in this case that could mean a link change, device reset,
  302. * or a virtual function reset
  303. */
  304. /* If we were hit with a reset or timeout drop the link */
  305. if (!mbx->ops.check_for_rst(hw) || !mbx->timeout)
  306. mac->get_link_status = true;
  307. if (!mac->get_link_status)
  308. goto out;
  309. /* if link status is down no point in checking to see if pf is up */
  310. if (!(er32(STATUS) & E1000_STATUS_LU))
  311. goto out;
  312. /* if the read failed it could just be a mailbox collision, best wait
  313. * until we are called again and don't report an error */
  314. if (mbx->ops.read(hw, &in_msg, 1))
  315. goto out;
  316. /* if incoming message isn't clear to send we are waiting on response */
  317. if (!(in_msg & E1000_VT_MSGTYPE_CTS)) {
  318. /* message is not CTS and is NACK we must have lost CTS status */
  319. if (in_msg & E1000_VT_MSGTYPE_NACK)
  320. ret_val = -E1000_ERR_MAC_INIT;
  321. goto out;
  322. }
  323. /* the pf is talking, if we timed out in the past we reinit */
  324. if (!mbx->timeout) {
  325. ret_val = -E1000_ERR_MAC_INIT;
  326. goto out;
  327. }
  328. /* if we passed all the tests above then the link is up and we no
  329. * longer need to check for link */
  330. mac->get_link_status = false;
  331. out:
  332. return ret_val;
  333. }