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