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