1 /*******************************************************************************
2 
3   Intel 82599 Virtual Function driver
4   Copyright(c) 1999 - 2015 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 #include "ixgbevf.h"
29 
30 /* On Hyper-V, to reset, we need to read from this offset
31  * from the PCI config space. This is the mechanism used on
32  * Hyper-V to support PF/VF communication.
33  */
34 #define IXGBE_HV_RESET_OFFSET           0x201
35 
36 static inline s32 ixgbevf_write_msg_read_ack(struct ixgbe_hw *hw, u32 *msg,
37 					     u32 *retmsg, u16 size)
38 {
39 	struct ixgbe_mbx_info *mbx = &hw->mbx;
40 	s32 retval = mbx->ops.write_posted(hw, msg, size);
41 
42 	if (retval)
43 		return retval;
44 
45 	return mbx->ops.read_posted(hw, retmsg, size);
46 }
47 
48 /**
49  *  ixgbevf_start_hw_vf - Prepare hardware for Tx/Rx
50  *  @hw: pointer to hardware structure
51  *
52  *  Starts the hardware by filling the bus info structure and media type, clears
53  *  all on chip counters, initializes receive address registers, multicast
54  *  table, VLAN filter table, calls routine to set up link and flow control
55  *  settings, and leaves transmit and receive units disabled and uninitialized
56  **/
57 static s32 ixgbevf_start_hw_vf(struct ixgbe_hw *hw)
58 {
59 	/* Clear adapter stopped flag */
60 	hw->adapter_stopped = false;
61 
62 	return 0;
63 }
64 
65 /**
66  *  ixgbevf_init_hw_vf - virtual function hardware initialization
67  *  @hw: pointer to hardware structure
68  *
69  *  Initialize the hardware by resetting the hardware and then starting
70  *  the hardware
71  **/
72 static s32 ixgbevf_init_hw_vf(struct ixgbe_hw *hw)
73 {
74 	s32 status = hw->mac.ops.start_hw(hw);
75 
76 	hw->mac.ops.get_mac_addr(hw, hw->mac.addr);
77 
78 	return status;
79 }
80 
81 /**
82  *  ixgbevf_reset_hw_vf - Performs hardware reset
83  *  @hw: pointer to hardware structure
84  *
85  *  Resets the hardware by resetting the transmit and receive units, masks and
86  *  clears all interrupts.
87  **/
88 static s32 ixgbevf_reset_hw_vf(struct ixgbe_hw *hw)
89 {
90 	struct ixgbe_mbx_info *mbx = &hw->mbx;
91 	u32 timeout = IXGBE_VF_INIT_TIMEOUT;
92 	s32 ret_val = IXGBE_ERR_INVALID_MAC_ADDR;
93 	u32 msgbuf[IXGBE_VF_PERMADDR_MSG_LEN];
94 	u8 *addr = (u8 *)(&msgbuf[1]);
95 
96 	/* Call adapter stop to disable tx/rx and clear interrupts */
97 	hw->mac.ops.stop_adapter(hw);
98 
99 	/* reset the api version */
100 	hw->api_version = ixgbe_mbox_api_10;
101 
102 	IXGBE_WRITE_REG(hw, IXGBE_VFCTRL, IXGBE_CTRL_RST);
103 	IXGBE_WRITE_FLUSH(hw);
104 
105 	/* we cannot reset while the RSTI / RSTD bits are asserted */
106 	while (!mbx->ops.check_for_rst(hw) && timeout) {
107 		timeout--;
108 		udelay(5);
109 	}
110 
111 	if (!timeout)
112 		return IXGBE_ERR_RESET_FAILED;
113 
114 	/* mailbox timeout can now become active */
115 	mbx->timeout = IXGBE_VF_MBX_INIT_TIMEOUT;
116 
117 	msgbuf[0] = IXGBE_VF_RESET;
118 	mbx->ops.write_posted(hw, msgbuf, 1);
119 
120 	mdelay(10);
121 
122 	/* set our "perm_addr" based on info provided by PF
123 	 * also set up the mc_filter_type which is piggy backed
124 	 * on the mac address in word 3
125 	 */
126 	ret_val = mbx->ops.read_posted(hw, msgbuf, IXGBE_VF_PERMADDR_MSG_LEN);
127 	if (ret_val)
128 		return ret_val;
129 
130 	/* New versions of the PF may NACK the reset return message
131 	 * to indicate that no MAC address has yet been assigned for
132 	 * the VF.
133 	 */
134 	if (msgbuf[0] != (IXGBE_VF_RESET | IXGBE_VT_MSGTYPE_ACK) &&
135 	    msgbuf[0] != (IXGBE_VF_RESET | IXGBE_VT_MSGTYPE_NACK))
136 		return IXGBE_ERR_INVALID_MAC_ADDR;
137 
138 	if (msgbuf[0] == (IXGBE_VF_RESET | IXGBE_VT_MSGTYPE_ACK))
139 		ether_addr_copy(hw->mac.perm_addr, addr);
140 
141 	hw->mac.mc_filter_type = msgbuf[IXGBE_VF_MC_TYPE_WORD];
142 
143 	return 0;
144 }
145 
146 /**
147  * Hyper-V variant; the VF/PF communication is through the PCI
148  * config space.
149  */
150 static s32 ixgbevf_hv_reset_hw_vf(struct ixgbe_hw *hw)
151 {
152 #if IS_ENABLED(CONFIG_PCI_MMCONFIG)
153 	struct ixgbevf_adapter *adapter = hw->back;
154 	int i;
155 
156 	for (i = 0; i < 6; i++)
157 		pci_read_config_byte(adapter->pdev,
158 				     (i + IXGBE_HV_RESET_OFFSET),
159 				     &hw->mac.perm_addr[i]);
160 	return 0;
161 #else
162 	pr_err("PCI_MMCONFIG needs to be enabled for Hyper-V\n");
163 	return -EOPNOTSUPP;
164 #endif
165 }
166 
167 /**
168  *  ixgbevf_stop_hw_vf - Generic stop Tx/Rx units
169  *  @hw: pointer to hardware structure
170  *
171  *  Sets the adapter_stopped flag within ixgbe_hw struct. Clears interrupts,
172  *  disables transmit and receive units. The adapter_stopped flag is used by
173  *  the shared code and drivers to determine if the adapter is in a stopped
174  *  state and should not touch the hardware.
175  **/
176 static s32 ixgbevf_stop_hw_vf(struct ixgbe_hw *hw)
177 {
178 	u32 number_of_queues;
179 	u32 reg_val;
180 	u16 i;
181 
182 	/* Set the adapter_stopped flag so other driver functions stop touching
183 	 * the hardware
184 	 */
185 	hw->adapter_stopped = true;
186 
187 	/* Disable the receive unit by stopped each queue */
188 	number_of_queues = hw->mac.max_rx_queues;
189 	for (i = 0; i < number_of_queues; i++) {
190 		reg_val = IXGBE_READ_REG(hw, IXGBE_VFRXDCTL(i));
191 		if (reg_val & IXGBE_RXDCTL_ENABLE) {
192 			reg_val &= ~IXGBE_RXDCTL_ENABLE;
193 			IXGBE_WRITE_REG(hw, IXGBE_VFRXDCTL(i), reg_val);
194 		}
195 	}
196 
197 	IXGBE_WRITE_FLUSH(hw);
198 
199 	/* Clear interrupt mask to stop from interrupts being generated */
200 	IXGBE_WRITE_REG(hw, IXGBE_VTEIMC, IXGBE_VF_IRQ_CLEAR_MASK);
201 
202 	/* Clear any pending interrupts */
203 	IXGBE_READ_REG(hw, IXGBE_VTEICR);
204 
205 	/* Disable the transmit unit.  Each queue must be disabled. */
206 	number_of_queues = hw->mac.max_tx_queues;
207 	for (i = 0; i < number_of_queues; i++) {
208 		reg_val = IXGBE_READ_REG(hw, IXGBE_VFTXDCTL(i));
209 		if (reg_val & IXGBE_TXDCTL_ENABLE) {
210 			reg_val &= ~IXGBE_TXDCTL_ENABLE;
211 			IXGBE_WRITE_REG(hw, IXGBE_VFTXDCTL(i), reg_val);
212 		}
213 	}
214 
215 	return 0;
216 }
217 
218 /**
219  *  ixgbevf_mta_vector - Determines bit-vector in multicast table to set
220  *  @hw: pointer to hardware structure
221  *  @mc_addr: the multicast address
222  *
223  *  Extracts the 12 bits, from a multicast address, to determine which
224  *  bit-vector to set in the multicast table. The hardware uses 12 bits, from
225  *  incoming Rx multicast addresses, to determine the bit-vector to check in
226  *  the MTA. Which of the 4 combination, of 12-bits, the hardware uses is set
227  *  by the MO field of the MCSTCTRL. The MO field is set during initialization
228  *  to mc_filter_type.
229  **/
230 static s32 ixgbevf_mta_vector(struct ixgbe_hw *hw, u8 *mc_addr)
231 {
232 	u32 vector = 0;
233 
234 	switch (hw->mac.mc_filter_type) {
235 	case 0:   /* use bits [47:36] of the address */
236 		vector = ((mc_addr[4] >> 4) | (((u16)mc_addr[5]) << 4));
237 		break;
238 	case 1:   /* use bits [46:35] of the address */
239 		vector = ((mc_addr[4] >> 3) | (((u16)mc_addr[5]) << 5));
240 		break;
241 	case 2:   /* use bits [45:34] of the address */
242 		vector = ((mc_addr[4] >> 2) | (((u16)mc_addr[5]) << 6));
243 		break;
244 	case 3:   /* use bits [43:32] of the address */
245 		vector = ((mc_addr[4]) | (((u16)mc_addr[5]) << 8));
246 		break;
247 	default:  /* Invalid mc_filter_type */
248 		break;
249 	}
250 
251 	/* vector can only be 12-bits or boundary will be exceeded */
252 	vector &= 0xFFF;
253 	return vector;
254 }
255 
256 /**
257  *  ixgbevf_get_mac_addr_vf - Read device MAC address
258  *  @hw: pointer to the HW structure
259  *  @mac_addr: pointer to storage for retrieved MAC address
260  **/
261 static s32 ixgbevf_get_mac_addr_vf(struct ixgbe_hw *hw, u8 *mac_addr)
262 {
263 	ether_addr_copy(mac_addr, hw->mac.perm_addr);
264 
265 	return 0;
266 }
267 
268 static s32 ixgbevf_set_uc_addr_vf(struct ixgbe_hw *hw, u32 index, u8 *addr)
269 {
270 	u32 msgbuf[3], msgbuf_chk;
271 	u8 *msg_addr = (u8 *)(&msgbuf[1]);
272 	s32 ret_val;
273 
274 	memset(msgbuf, 0, sizeof(msgbuf));
275 	/* If index is one then this is the start of a new list and needs
276 	 * indication to the PF so it can do it's own list management.
277 	 * If it is zero then that tells the PF to just clear all of
278 	 * this VF's macvlans and there is no new list.
279 	 */
280 	msgbuf[0] |= index << IXGBE_VT_MSGINFO_SHIFT;
281 	msgbuf[0] |= IXGBE_VF_SET_MACVLAN;
282 	msgbuf_chk = msgbuf[0];
283 
284 	if (addr)
285 		ether_addr_copy(msg_addr, addr);
286 
287 	ret_val = ixgbevf_write_msg_read_ack(hw, msgbuf, msgbuf, 3);
288 	if (!ret_val) {
289 		msgbuf[0] &= ~IXGBE_VT_MSGTYPE_CTS;
290 
291 		if (msgbuf[0] == (msgbuf_chk | IXGBE_VT_MSGTYPE_NACK))
292 			return -ENOMEM;
293 	}
294 
295 	return ret_val;
296 }
297 
298 static s32 ixgbevf_hv_set_uc_addr_vf(struct ixgbe_hw *hw, u32 index, u8 *addr)
299 {
300 	return -EOPNOTSUPP;
301 }
302 
303 /**
304  * ixgbevf_get_reta_locked - get the RSS redirection table (RETA) contents.
305  * @adapter: pointer to the port handle
306  * @reta: buffer to fill with RETA contents.
307  * @num_rx_queues: Number of Rx queues configured for this port
308  *
309  * The "reta" buffer should be big enough to contain 32 registers.
310  *
311  * Returns: 0 on success.
312  *          if API doesn't support this operation - (-EOPNOTSUPP).
313  */
314 int ixgbevf_get_reta_locked(struct ixgbe_hw *hw, u32 *reta, int num_rx_queues)
315 {
316 	int err, i, j;
317 	u32 msgbuf[IXGBE_VFMAILBOX_SIZE];
318 	u32 *hw_reta = &msgbuf[1];
319 	u32 mask = 0;
320 
321 	/* We have to use a mailbox for 82599 and x540 devices only.
322 	 * For these devices RETA has 128 entries.
323 	 * Also these VFs support up to 4 RSS queues. Therefore PF will compress
324 	 * 16 RETA entries in each DWORD giving 2 bits to each entry.
325 	 */
326 	int dwords = IXGBEVF_82599_RETA_SIZE / 16;
327 
328 	/* We support the RSS querying for 82599 and x540 devices only.
329 	 * Thus return an error if API doesn't support RETA querying or querying
330 	 * is not supported for this device type.
331 	 */
332 	if (hw->api_version != ixgbe_mbox_api_12 ||
333 	    hw->mac.type >= ixgbe_mac_X550_vf)
334 		return -EOPNOTSUPP;
335 
336 	msgbuf[0] = IXGBE_VF_GET_RETA;
337 
338 	err = hw->mbx.ops.write_posted(hw, msgbuf, 1);
339 
340 	if (err)
341 		return err;
342 
343 	err = hw->mbx.ops.read_posted(hw, msgbuf, dwords + 1);
344 
345 	if (err)
346 		return err;
347 
348 	msgbuf[0] &= ~IXGBE_VT_MSGTYPE_CTS;
349 
350 	/* If the operation has been refused by a PF return -EPERM */
351 	if (msgbuf[0] == (IXGBE_VF_GET_RETA | IXGBE_VT_MSGTYPE_NACK))
352 		return -EPERM;
353 
354 	/* If we didn't get an ACK there must have been
355 	 * some sort of mailbox error so we should treat it
356 	 * as such.
357 	 */
358 	if (msgbuf[0] != (IXGBE_VF_GET_RETA | IXGBE_VT_MSGTYPE_ACK))
359 		return IXGBE_ERR_MBX;
360 
361 	/* ixgbevf doesn't support more than 2 queues at the moment */
362 	if (num_rx_queues > 1)
363 		mask = 0x1;
364 
365 	for (i = 0; i < dwords; i++)
366 		for (j = 0; j < 16; j++)
367 			reta[i * 16 + j] = (hw_reta[i] >> (2 * j)) & mask;
368 
369 	return 0;
370 }
371 
372 /**
373  * ixgbevf_get_rss_key_locked - get the RSS Random Key
374  * @hw: pointer to the HW structure
375  * @rss_key: buffer to fill with RSS Hash Key contents.
376  *
377  * The "rss_key" buffer should be big enough to contain 10 registers.
378  *
379  * Returns: 0 on success.
380  *          if API doesn't support this operation - (-EOPNOTSUPP).
381  */
382 int ixgbevf_get_rss_key_locked(struct ixgbe_hw *hw, u8 *rss_key)
383 {
384 	int err;
385 	u32 msgbuf[IXGBE_VFMAILBOX_SIZE];
386 
387 	/* We currently support the RSS Random Key retrieval for 82599 and x540
388 	 * devices only.
389 	 *
390 	 * Thus return an error if API doesn't support RSS Random Key retrieval
391 	 * or if the operation is not supported for this device type.
392 	 */
393 	if (hw->api_version != ixgbe_mbox_api_12 ||
394 	    hw->mac.type >= ixgbe_mac_X550_vf)
395 		return -EOPNOTSUPP;
396 
397 	msgbuf[0] = IXGBE_VF_GET_RSS_KEY;
398 	err = hw->mbx.ops.write_posted(hw, msgbuf, 1);
399 
400 	if (err)
401 		return err;
402 
403 	err = hw->mbx.ops.read_posted(hw, msgbuf, 11);
404 
405 	if (err)
406 		return err;
407 
408 	msgbuf[0] &= ~IXGBE_VT_MSGTYPE_CTS;
409 
410 	/* If the operation has been refused by a PF return -EPERM */
411 	if (msgbuf[0] == (IXGBE_VF_GET_RETA | IXGBE_VT_MSGTYPE_NACK))
412 		return -EPERM;
413 
414 	/* If we didn't get an ACK there must have been
415 	 * some sort of mailbox error so we should treat it
416 	 * as such.
417 	 */
418 	if (msgbuf[0] != (IXGBE_VF_GET_RSS_KEY | IXGBE_VT_MSGTYPE_ACK))
419 		return IXGBE_ERR_MBX;
420 
421 	memcpy(rss_key, msgbuf + 1, IXGBEVF_RSS_HASH_KEY_SIZE);
422 
423 	return 0;
424 }
425 
426 /**
427  *  ixgbevf_set_rar_vf - set device MAC address
428  *  @hw: pointer to hardware structure
429  *  @index: Receive address register to write
430  *  @addr: Address to put into receive address register
431  *  @vmdq: Unused in this implementation
432  **/
433 static s32 ixgbevf_set_rar_vf(struct ixgbe_hw *hw, u32 index, u8 *addr,
434 			      u32 vmdq)
435 {
436 	u32 msgbuf[3];
437 	u8 *msg_addr = (u8 *)(&msgbuf[1]);
438 	s32 ret_val;
439 
440 	memset(msgbuf, 0, sizeof(msgbuf));
441 	msgbuf[0] = IXGBE_VF_SET_MAC_ADDR;
442 	ether_addr_copy(msg_addr, addr);
443 
444 	ret_val = ixgbevf_write_msg_read_ack(hw, msgbuf, msgbuf, 2);
445 
446 	msgbuf[0] &= ~IXGBE_VT_MSGTYPE_CTS;
447 
448 	/* if nacked the address was rejected, use "perm_addr" */
449 	if (!ret_val &&
450 	    (msgbuf[0] == (IXGBE_VF_SET_MAC_ADDR | IXGBE_VT_MSGTYPE_NACK))) {
451 		ixgbevf_get_mac_addr_vf(hw, hw->mac.addr);
452 		return IXGBE_ERR_MBX;
453 	}
454 
455 	return ret_val;
456 }
457 
458 /**
459  *  ixgbevf_hv_set_rar_vf - set device MAC address Hyper-V variant
460  *  @hw: pointer to hardware structure
461  *  @index: Receive address register to write
462  *  @addr: Address to put into receive address register
463  *  @vmdq: Unused in this implementation
464  *
465  * We don't really allow setting the device MAC address. However,
466  * if the address being set is the permanent MAC address we will
467  * permit that.
468  **/
469 static s32 ixgbevf_hv_set_rar_vf(struct ixgbe_hw *hw, u32 index, u8 *addr,
470 				 u32 vmdq)
471 {
472 	if (ether_addr_equal(addr, hw->mac.perm_addr))
473 		return 0;
474 
475 	return -EOPNOTSUPP;
476 }
477 
478 /**
479  *  ixgbevf_update_mc_addr_list_vf - Update Multicast addresses
480  *  @hw: pointer to the HW structure
481  *  @netdev: pointer to net device structure
482  *
483  *  Updates the Multicast Table Array.
484  **/
485 static s32 ixgbevf_update_mc_addr_list_vf(struct ixgbe_hw *hw,
486 					  struct net_device *netdev)
487 {
488 	struct netdev_hw_addr *ha;
489 	u32 msgbuf[IXGBE_VFMAILBOX_SIZE];
490 	u16 *vector_list = (u16 *)&msgbuf[1];
491 	u32 cnt, i;
492 
493 	/* Each entry in the list uses 1 16 bit word.  We have 30
494 	 * 16 bit words available in our HW msg buffer (minus 1 for the
495 	 * msg type).  That's 30 hash values if we pack 'em right.  If
496 	 * there are more than 30 MC addresses to add then punt the
497 	 * extras for now and then add code to handle more than 30 later.
498 	 * It would be unusual for a server to request that many multi-cast
499 	 * addresses except for in large enterprise network environments.
500 	 */
501 
502 	cnt = netdev_mc_count(netdev);
503 	if (cnt > 30)
504 		cnt = 30;
505 	msgbuf[0] = IXGBE_VF_SET_MULTICAST;
506 	msgbuf[0] |= cnt << IXGBE_VT_MSGINFO_SHIFT;
507 
508 	i = 0;
509 	netdev_for_each_mc_addr(ha, netdev) {
510 		if (i == cnt)
511 			break;
512 		if (is_link_local_ether_addr(ha->addr))
513 			continue;
514 
515 		vector_list[i++] = ixgbevf_mta_vector(hw, ha->addr);
516 	}
517 
518 	ixgbevf_write_msg_read_ack(hw, msgbuf, msgbuf, IXGBE_VFMAILBOX_SIZE);
519 
520 	return 0;
521 }
522 
523 /**
524  * Hyper-V variant - just a stub.
525  */
526 static s32 ixgbevf_hv_update_mc_addr_list_vf(struct ixgbe_hw *hw,
527 					     struct net_device *netdev)
528 {
529 	return -EOPNOTSUPP;
530 }
531 
532 /**
533  *  ixgbevf_update_xcast_mode - Update Multicast mode
534  *  @hw: pointer to the HW structure
535  *  @xcast_mode: new multicast mode
536  *
537  *  Updates the Multicast Mode of VF.
538  **/
539 static s32 ixgbevf_update_xcast_mode(struct ixgbe_hw *hw, int xcast_mode)
540 {
541 	u32 msgbuf[2];
542 	s32 err;
543 
544 	switch (hw->api_version) {
545 	case ixgbe_mbox_api_12:
546 		break;
547 	default:
548 		return -EOPNOTSUPP;
549 	}
550 
551 	msgbuf[0] = IXGBE_VF_UPDATE_XCAST_MODE;
552 	msgbuf[1] = xcast_mode;
553 
554 	err = ixgbevf_write_msg_read_ack(hw, msgbuf, msgbuf, 2);
555 	if (err)
556 		return err;
557 
558 	msgbuf[0] &= ~IXGBE_VT_MSGTYPE_CTS;
559 	if (msgbuf[0] == (IXGBE_VF_UPDATE_XCAST_MODE | IXGBE_VT_MSGTYPE_NACK))
560 		return -EPERM;
561 
562 	return 0;
563 }
564 
565 /**
566  * Hyper-V variant - just a stub.
567  */
568 static s32 ixgbevf_hv_update_xcast_mode(struct ixgbe_hw *hw, int xcast_mode)
569 {
570 	return -EOPNOTSUPP;
571 }
572 
573 /**
574  *  ixgbevf_set_vfta_vf - Set/Unset VLAN filter table address
575  *  @hw: pointer to the HW structure
576  *  @vlan: 12 bit VLAN ID
577  *  @vind: unused by VF drivers
578  *  @vlan_on: if true then set bit, else clear bit
579  **/
580 static s32 ixgbevf_set_vfta_vf(struct ixgbe_hw *hw, u32 vlan, u32 vind,
581 			       bool vlan_on)
582 {
583 	u32 msgbuf[2];
584 	s32 err;
585 
586 	msgbuf[0] = IXGBE_VF_SET_VLAN;
587 	msgbuf[1] = vlan;
588 	/* Setting the 8 bit field MSG INFO to TRUE indicates "add" */
589 	msgbuf[0] |= vlan_on << IXGBE_VT_MSGINFO_SHIFT;
590 
591 	err = ixgbevf_write_msg_read_ack(hw, msgbuf, msgbuf, 2);
592 	if (err)
593 		goto mbx_err;
594 
595 	/* remove extra bits from the message */
596 	msgbuf[0] &= ~IXGBE_VT_MSGTYPE_CTS;
597 	msgbuf[0] &= ~(0xFF << IXGBE_VT_MSGINFO_SHIFT);
598 
599 	if (msgbuf[0] != (IXGBE_VF_SET_VLAN | IXGBE_VT_MSGTYPE_ACK))
600 		err = IXGBE_ERR_INVALID_ARGUMENT;
601 
602 mbx_err:
603 	return err;
604 }
605 
606 /**
607  * Hyper-V variant - just a stub.
608  */
609 static s32 ixgbevf_hv_set_vfta_vf(struct ixgbe_hw *hw, u32 vlan, u32 vind,
610 				  bool vlan_on)
611 {
612 	return -EOPNOTSUPP;
613 }
614 
615 /**
616  *  ixgbevf_setup_mac_link_vf - Setup MAC link settings
617  *  @hw: pointer to hardware structure
618  *  @speed: Unused in this implementation
619  *  @autoneg: Unused in this implementation
620  *  @autoneg_wait_to_complete: Unused in this implementation
621  *
622  *  Do nothing and return success.  VF drivers are not allowed to change
623  *  global settings.  Maintained for driver compatibility.
624  **/
625 static s32 ixgbevf_setup_mac_link_vf(struct ixgbe_hw *hw,
626 				     ixgbe_link_speed speed, bool autoneg,
627 				     bool autoneg_wait_to_complete)
628 {
629 	return 0;
630 }
631 
632 /**
633  *  ixgbevf_check_mac_link_vf - Get link/speed status
634  *  @hw: pointer to hardware structure
635  *  @speed: pointer to link speed
636  *  @link_up: true is link is up, false otherwise
637  *  @autoneg_wait_to_complete: true when waiting for completion is needed
638  *
639  *  Reads the links register to determine if link is up and the current speed
640  **/
641 static s32 ixgbevf_check_mac_link_vf(struct ixgbe_hw *hw,
642 				     ixgbe_link_speed *speed,
643 				     bool *link_up,
644 				     bool autoneg_wait_to_complete)
645 {
646 	struct ixgbe_mbx_info *mbx = &hw->mbx;
647 	struct ixgbe_mac_info *mac = &hw->mac;
648 	s32 ret_val = 0;
649 	u32 links_reg;
650 	u32 in_msg = 0;
651 
652 	/* If we were hit with a reset drop the link */
653 	if (!mbx->ops.check_for_rst(hw) || !mbx->timeout)
654 		mac->get_link_status = true;
655 
656 	if (!mac->get_link_status)
657 		goto out;
658 
659 	/* if link status is down no point in checking to see if pf is up */
660 	links_reg = IXGBE_READ_REG(hw, IXGBE_VFLINKS);
661 	if (!(links_reg & IXGBE_LINKS_UP))
662 		goto out;
663 
664 	/* for SFP+ modules and DA cables on 82599 it can take up to 500usecs
665 	 * before the link status is correct
666 	 */
667 	if (mac->type == ixgbe_mac_82599_vf) {
668 		int i;
669 
670 		for (i = 0; i < 5; i++) {
671 			udelay(100);
672 			links_reg = IXGBE_READ_REG(hw, IXGBE_VFLINKS);
673 
674 			if (!(links_reg & IXGBE_LINKS_UP))
675 				goto out;
676 		}
677 	}
678 
679 	switch (links_reg & IXGBE_LINKS_SPEED_82599) {
680 	case IXGBE_LINKS_SPEED_10G_82599:
681 		*speed = IXGBE_LINK_SPEED_10GB_FULL;
682 		break;
683 	case IXGBE_LINKS_SPEED_1G_82599:
684 		*speed = IXGBE_LINK_SPEED_1GB_FULL;
685 		break;
686 	case IXGBE_LINKS_SPEED_100_82599:
687 		*speed = IXGBE_LINK_SPEED_100_FULL;
688 		break;
689 	}
690 
691 	/* if the read failed it could just be a mailbox collision, best wait
692 	 * until we are called again and don't report an error
693 	 */
694 	if (mbx->ops.read(hw, &in_msg, 1))
695 		goto out;
696 
697 	if (!(in_msg & IXGBE_VT_MSGTYPE_CTS)) {
698 		/* msg is not CTS and is NACK we must have lost CTS status */
699 		if (in_msg & IXGBE_VT_MSGTYPE_NACK)
700 			ret_val = -1;
701 		goto out;
702 	}
703 
704 	/* the pf is talking, if we timed out in the past we reinit */
705 	if (!mbx->timeout) {
706 		ret_val = -1;
707 		goto out;
708 	}
709 
710 	/* if we passed all the tests above then the link is up and we no
711 	 * longer need to check for link
712 	 */
713 	mac->get_link_status = false;
714 
715 out:
716 	*link_up = !mac->get_link_status;
717 	return ret_val;
718 }
719 
720 /**
721  * Hyper-V variant; there is no mailbox communication.
722  */
723 static s32 ixgbevf_hv_check_mac_link_vf(struct ixgbe_hw *hw,
724 					ixgbe_link_speed *speed,
725 					bool *link_up,
726 					bool autoneg_wait_to_complete)
727 {
728 	struct ixgbe_mbx_info *mbx = &hw->mbx;
729 	struct ixgbe_mac_info *mac = &hw->mac;
730 	u32 links_reg;
731 
732 	/* If we were hit with a reset drop the link */
733 	if (!mbx->ops.check_for_rst(hw) || !mbx->timeout)
734 		mac->get_link_status = true;
735 
736 	if (!mac->get_link_status)
737 		goto out;
738 
739 	/* if link status is down no point in checking to see if pf is up */
740 	links_reg = IXGBE_READ_REG(hw, IXGBE_VFLINKS);
741 	if (!(links_reg & IXGBE_LINKS_UP))
742 		goto out;
743 
744 	/* for SFP+ modules and DA cables on 82599 it can take up to 500usecs
745 	 * before the link status is correct
746 	 */
747 	if (mac->type == ixgbe_mac_82599_vf) {
748 		int i;
749 
750 		for (i = 0; i < 5; i++) {
751 			udelay(100);
752 			links_reg = IXGBE_READ_REG(hw, IXGBE_VFLINKS);
753 
754 			if (!(links_reg & IXGBE_LINKS_UP))
755 				goto out;
756 		}
757 	}
758 
759 	switch (links_reg & IXGBE_LINKS_SPEED_82599) {
760 	case IXGBE_LINKS_SPEED_10G_82599:
761 		*speed = IXGBE_LINK_SPEED_10GB_FULL;
762 		break;
763 	case IXGBE_LINKS_SPEED_1G_82599:
764 		*speed = IXGBE_LINK_SPEED_1GB_FULL;
765 		break;
766 	case IXGBE_LINKS_SPEED_100_82599:
767 		*speed = IXGBE_LINK_SPEED_100_FULL;
768 		break;
769 	}
770 
771 	/* if we passed all the tests above then the link is up and we no
772 	 * longer need to check for link
773 	 */
774 	mac->get_link_status = false;
775 
776 out:
777 	*link_up = !mac->get_link_status;
778 	return 0;
779 }
780 
781 /**
782  *  ixgbevf_set_rlpml_vf - Set the maximum receive packet length
783  *  @hw: pointer to the HW structure
784  *  @max_size: value to assign to max frame size
785  **/
786 static s32 ixgbevf_set_rlpml_vf(struct ixgbe_hw *hw, u16 max_size)
787 {
788 	u32 msgbuf[2];
789 	s32 ret_val;
790 
791 	msgbuf[0] = IXGBE_VF_SET_LPE;
792 	msgbuf[1] = max_size;
793 
794 	ret_val = ixgbevf_write_msg_read_ack(hw, msgbuf, msgbuf, 2);
795 	if (ret_val)
796 		return ret_val;
797 	if ((msgbuf[0] & IXGBE_VF_SET_LPE) &&
798 	    (msgbuf[0] & IXGBE_VT_MSGTYPE_NACK))
799 		return IXGBE_ERR_MBX;
800 
801 	return 0;
802 }
803 
804 /**
805  * ixgbevf_hv_set_rlpml_vf - Set the maximum receive packet length
806  * @hw: pointer to the HW structure
807  * @max_size: value to assign to max frame size
808  * Hyper-V variant.
809  **/
810 static s32 ixgbevf_hv_set_rlpml_vf(struct ixgbe_hw *hw, u16 max_size)
811 {
812 	u32 reg;
813 
814 	/* If we are on Hyper-V, we implement this functionality
815 	 * differently.
816 	 */
817 	reg =  IXGBE_READ_REG(hw, IXGBE_VFRXDCTL(0));
818 	/* CRC == 4 */
819 	reg |= ((max_size + 4) | IXGBE_RXDCTL_RLPML_EN);
820 	IXGBE_WRITE_REG(hw, IXGBE_VFRXDCTL(0), reg);
821 
822 	return 0;
823 }
824 
825 /**
826  *  ixgbevf_negotiate_api_version_vf - Negotiate supported API version
827  *  @hw: pointer to the HW structure
828  *  @api: integer containing requested API version
829  **/
830 static int ixgbevf_negotiate_api_version_vf(struct ixgbe_hw *hw, int api)
831 {
832 	int err;
833 	u32 msg[3];
834 
835 	/* Negotiate the mailbox API version */
836 	msg[0] = IXGBE_VF_API_NEGOTIATE;
837 	msg[1] = api;
838 	msg[2] = 0;
839 
840 	err = ixgbevf_write_msg_read_ack(hw, msg, msg, 3);
841 	if (!err) {
842 		msg[0] &= ~IXGBE_VT_MSGTYPE_CTS;
843 
844 		/* Store value and return 0 on success */
845 		if (msg[0] == (IXGBE_VF_API_NEGOTIATE | IXGBE_VT_MSGTYPE_ACK)) {
846 			hw->api_version = api;
847 			return 0;
848 		}
849 
850 		err = IXGBE_ERR_INVALID_ARGUMENT;
851 	}
852 
853 	return err;
854 }
855 
856 /**
857  *  ixgbevf_hv_negotiate_api_version_vf - Negotiate supported API version
858  *  @hw: pointer to the HW structure
859  *  @api: integer containing requested API version
860  *  Hyper-V version - only ixgbe_mbox_api_10 supported.
861  **/
862 static int ixgbevf_hv_negotiate_api_version_vf(struct ixgbe_hw *hw, int api)
863 {
864 	/* Hyper-V only supports api version ixgbe_mbox_api_10 */
865 	if (api != ixgbe_mbox_api_10)
866 		return IXGBE_ERR_INVALID_ARGUMENT;
867 
868 	return 0;
869 }
870 
871 int ixgbevf_get_queues(struct ixgbe_hw *hw, unsigned int *num_tcs,
872 		       unsigned int *default_tc)
873 {
874 	int err;
875 	u32 msg[5];
876 
877 	/* do nothing if API doesn't support ixgbevf_get_queues */
878 	switch (hw->api_version) {
879 	case ixgbe_mbox_api_11:
880 	case ixgbe_mbox_api_12:
881 		break;
882 	default:
883 		return 0;
884 	}
885 
886 	/* Fetch queue configuration from the PF */
887 	msg[0] = IXGBE_VF_GET_QUEUE;
888 	msg[1] = msg[2] = msg[3] = msg[4] = 0;
889 
890 	err = ixgbevf_write_msg_read_ack(hw, msg, msg, 5);
891 	if (!err) {
892 		msg[0] &= ~IXGBE_VT_MSGTYPE_CTS;
893 
894 		/* if we we didn't get an ACK there must have been
895 		 * some sort of mailbox error so we should treat it
896 		 * as such
897 		 */
898 		if (msg[0] != (IXGBE_VF_GET_QUEUE | IXGBE_VT_MSGTYPE_ACK))
899 			return IXGBE_ERR_MBX;
900 
901 		/* record and validate values from message */
902 		hw->mac.max_tx_queues = msg[IXGBE_VF_TX_QUEUES];
903 		if (hw->mac.max_tx_queues == 0 ||
904 		    hw->mac.max_tx_queues > IXGBE_VF_MAX_TX_QUEUES)
905 			hw->mac.max_tx_queues = IXGBE_VF_MAX_TX_QUEUES;
906 
907 		hw->mac.max_rx_queues = msg[IXGBE_VF_RX_QUEUES];
908 		if (hw->mac.max_rx_queues == 0 ||
909 		    hw->mac.max_rx_queues > IXGBE_VF_MAX_RX_QUEUES)
910 			hw->mac.max_rx_queues = IXGBE_VF_MAX_RX_QUEUES;
911 
912 		*num_tcs = msg[IXGBE_VF_TRANS_VLAN];
913 		/* in case of unknown state assume we cannot tag frames */
914 		if (*num_tcs > hw->mac.max_rx_queues)
915 			*num_tcs = 1;
916 
917 		*default_tc = msg[IXGBE_VF_DEF_QUEUE];
918 		/* default to queue 0 on out-of-bounds queue number */
919 		if (*default_tc >= hw->mac.max_tx_queues)
920 			*default_tc = 0;
921 	}
922 
923 	return err;
924 }
925 
926 static const struct ixgbe_mac_operations ixgbevf_mac_ops = {
927 	.init_hw		= ixgbevf_init_hw_vf,
928 	.reset_hw		= ixgbevf_reset_hw_vf,
929 	.start_hw		= ixgbevf_start_hw_vf,
930 	.get_mac_addr		= ixgbevf_get_mac_addr_vf,
931 	.stop_adapter		= ixgbevf_stop_hw_vf,
932 	.setup_link		= ixgbevf_setup_mac_link_vf,
933 	.check_link		= ixgbevf_check_mac_link_vf,
934 	.negotiate_api_version	= ixgbevf_negotiate_api_version_vf,
935 	.set_rar		= ixgbevf_set_rar_vf,
936 	.update_mc_addr_list	= ixgbevf_update_mc_addr_list_vf,
937 	.update_xcast_mode	= ixgbevf_update_xcast_mode,
938 	.set_uc_addr		= ixgbevf_set_uc_addr_vf,
939 	.set_vfta		= ixgbevf_set_vfta_vf,
940 	.set_rlpml		= ixgbevf_set_rlpml_vf,
941 };
942 
943 static const struct ixgbe_mac_operations ixgbevf_hv_mac_ops = {
944 	.init_hw		= ixgbevf_init_hw_vf,
945 	.reset_hw		= ixgbevf_hv_reset_hw_vf,
946 	.start_hw		= ixgbevf_start_hw_vf,
947 	.get_mac_addr		= ixgbevf_get_mac_addr_vf,
948 	.stop_adapter		= ixgbevf_stop_hw_vf,
949 	.setup_link		= ixgbevf_setup_mac_link_vf,
950 	.check_link		= ixgbevf_hv_check_mac_link_vf,
951 	.negotiate_api_version	= ixgbevf_hv_negotiate_api_version_vf,
952 	.set_rar		= ixgbevf_hv_set_rar_vf,
953 	.update_mc_addr_list	= ixgbevf_hv_update_mc_addr_list_vf,
954 	.update_xcast_mode	= ixgbevf_hv_update_xcast_mode,
955 	.set_uc_addr		= ixgbevf_hv_set_uc_addr_vf,
956 	.set_vfta		= ixgbevf_hv_set_vfta_vf,
957 	.set_rlpml		= ixgbevf_hv_set_rlpml_vf,
958 };
959 
960 const struct ixgbevf_info ixgbevf_82599_vf_info = {
961 	.mac = ixgbe_mac_82599_vf,
962 	.mac_ops = &ixgbevf_mac_ops,
963 };
964 
965 const struct ixgbevf_info ixgbevf_82599_vf_hv_info = {
966 	.mac = ixgbe_mac_82599_vf,
967 	.mac_ops = &ixgbevf_hv_mac_ops,
968 };
969 
970 const struct ixgbevf_info ixgbevf_X540_vf_info = {
971 	.mac = ixgbe_mac_X540_vf,
972 	.mac_ops = &ixgbevf_mac_ops,
973 };
974 
975 const struct ixgbevf_info ixgbevf_X540_vf_hv_info = {
976 	.mac = ixgbe_mac_X540_vf,
977 	.mac_ops = &ixgbevf_hv_mac_ops,
978 };
979 
980 const struct ixgbevf_info ixgbevf_X550_vf_info = {
981 	.mac = ixgbe_mac_X550_vf,
982 	.mac_ops = &ixgbevf_mac_ops,
983 };
984 
985 const struct ixgbevf_info ixgbevf_X550_vf_hv_info = {
986 	.mac = ixgbe_mac_X550_vf,
987 	.mac_ops = &ixgbevf_hv_mac_ops,
988 };
989 
990 const struct ixgbevf_info ixgbevf_X550EM_x_vf_info = {
991 	.mac = ixgbe_mac_X550EM_x_vf,
992 	.mac_ops = &ixgbevf_mac_ops,
993 };
994 
995 const struct ixgbevf_info ixgbevf_X550EM_x_vf_hv_info = {
996 	.mac = ixgbe_mac_X550EM_x_vf,
997 	.mac_ops = &ixgbevf_hv_mac_ops,
998 };
999 
1000 const struct ixgbevf_info ixgbevf_x550em_a_vf_info = {
1001 	.mac = ixgbe_mac_x550em_a_vf,
1002 	.mac_ops = &ixgbevf_mac_ops,
1003 };
1004