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,
288 					     sizeof(msgbuf) / sizeof(u32));
289 	if (!ret_val) {
290 		msgbuf[0] &= ~IXGBE_VT_MSGTYPE_CTS;
291 
292 		if (msgbuf[0] == (msgbuf_chk | IXGBE_VT_MSGTYPE_NACK))
293 			return -ENOMEM;
294 	}
295 
296 	return ret_val;
297 }
298 
299 static s32 ixgbevf_hv_set_uc_addr_vf(struct ixgbe_hw *hw, u32 index, u8 *addr)
300 {
301 	return -EOPNOTSUPP;
302 }
303 
304 /**
305  * ixgbevf_get_reta_locked - get the RSS redirection table (RETA) contents.
306  * @adapter: pointer to the port handle
307  * @reta: buffer to fill with RETA contents.
308  * @num_rx_queues: Number of Rx queues configured for this port
309  *
310  * The "reta" buffer should be big enough to contain 32 registers.
311  *
312  * Returns: 0 on success.
313  *          if API doesn't support this operation - (-EOPNOTSUPP).
314  */
315 int ixgbevf_get_reta_locked(struct ixgbe_hw *hw, u32 *reta, int num_rx_queues)
316 {
317 	int err, i, j;
318 	u32 msgbuf[IXGBE_VFMAILBOX_SIZE];
319 	u32 *hw_reta = &msgbuf[1];
320 	u32 mask = 0;
321 
322 	/* We have to use a mailbox for 82599 and x540 devices only.
323 	 * For these devices RETA has 128 entries.
324 	 * Also these VFs support up to 4 RSS queues. Therefore PF will compress
325 	 * 16 RETA entries in each DWORD giving 2 bits to each entry.
326 	 */
327 	int dwords = IXGBEVF_82599_RETA_SIZE / 16;
328 
329 	/* We support the RSS querying for 82599 and x540 devices only.
330 	 * Thus return an error if API doesn't support RETA querying or querying
331 	 * is not supported for this device type.
332 	 */
333 	switch (hw->api_version) {
334 	case ixgbe_mbox_api_13:
335 	case ixgbe_mbox_api_12:
336 		if (hw->mac.type >= ixgbe_mac_X550_vf)
337 			break;
338 	default:
339 		return -EOPNOTSUPP;
340 	}
341 
342 	msgbuf[0] = IXGBE_VF_GET_RETA;
343 
344 	err = hw->mbx.ops.write_posted(hw, msgbuf, 1);
345 
346 	if (err)
347 		return err;
348 
349 	err = hw->mbx.ops.read_posted(hw, msgbuf, dwords + 1);
350 
351 	if (err)
352 		return err;
353 
354 	msgbuf[0] &= ~IXGBE_VT_MSGTYPE_CTS;
355 
356 	/* If the operation has been refused by a PF return -EPERM */
357 	if (msgbuf[0] == (IXGBE_VF_GET_RETA | IXGBE_VT_MSGTYPE_NACK))
358 		return -EPERM;
359 
360 	/* If we didn't get an ACK there must have been
361 	 * some sort of mailbox error so we should treat it
362 	 * as such.
363 	 */
364 	if (msgbuf[0] != (IXGBE_VF_GET_RETA | IXGBE_VT_MSGTYPE_ACK))
365 		return IXGBE_ERR_MBX;
366 
367 	/* ixgbevf doesn't support more than 2 queues at the moment */
368 	if (num_rx_queues > 1)
369 		mask = 0x1;
370 
371 	for (i = 0; i < dwords; i++)
372 		for (j = 0; j < 16; j++)
373 			reta[i * 16 + j] = (hw_reta[i] >> (2 * j)) & mask;
374 
375 	return 0;
376 }
377 
378 /**
379  * ixgbevf_get_rss_key_locked - get the RSS Random Key
380  * @hw: pointer to the HW structure
381  * @rss_key: buffer to fill with RSS Hash Key contents.
382  *
383  * The "rss_key" buffer should be big enough to contain 10 registers.
384  *
385  * Returns: 0 on success.
386  *          if API doesn't support this operation - (-EOPNOTSUPP).
387  */
388 int ixgbevf_get_rss_key_locked(struct ixgbe_hw *hw, u8 *rss_key)
389 {
390 	int err;
391 	u32 msgbuf[IXGBE_VFMAILBOX_SIZE];
392 
393 	/* We currently support the RSS Random Key retrieval for 82599 and x540
394 	 * devices only.
395 	 *
396 	 * Thus return an error if API doesn't support RSS Random Key retrieval
397 	 * or if the operation is not supported for this device type.
398 	 */
399 	switch (hw->api_version) {
400 	case ixgbe_mbox_api_13:
401 	case ixgbe_mbox_api_12:
402 		if (hw->mac.type >= ixgbe_mac_X550_vf)
403 			break;
404 	default:
405 		return -EOPNOTSUPP;
406 	}
407 
408 	msgbuf[0] = IXGBE_VF_GET_RSS_KEY;
409 	err = hw->mbx.ops.write_posted(hw, msgbuf, 1);
410 
411 	if (err)
412 		return err;
413 
414 	err = hw->mbx.ops.read_posted(hw, msgbuf, 11);
415 
416 	if (err)
417 		return err;
418 
419 	msgbuf[0] &= ~IXGBE_VT_MSGTYPE_CTS;
420 
421 	/* If the operation has been refused by a PF return -EPERM */
422 	if (msgbuf[0] == (IXGBE_VF_GET_RETA | IXGBE_VT_MSGTYPE_NACK))
423 		return -EPERM;
424 
425 	/* If we didn't get an ACK there must have been
426 	 * some sort of mailbox error so we should treat it
427 	 * as such.
428 	 */
429 	if (msgbuf[0] != (IXGBE_VF_GET_RSS_KEY | IXGBE_VT_MSGTYPE_ACK))
430 		return IXGBE_ERR_MBX;
431 
432 	memcpy(rss_key, msgbuf + 1, IXGBEVF_RSS_HASH_KEY_SIZE);
433 
434 	return 0;
435 }
436 
437 /**
438  *  ixgbevf_set_rar_vf - set device MAC address
439  *  @hw: pointer to hardware structure
440  *  @index: Receive address register to write
441  *  @addr: Address to put into receive address register
442  *  @vmdq: Unused in this implementation
443  **/
444 static s32 ixgbevf_set_rar_vf(struct ixgbe_hw *hw, u32 index, u8 *addr,
445 			      u32 vmdq)
446 {
447 	u32 msgbuf[3];
448 	u8 *msg_addr = (u8 *)(&msgbuf[1]);
449 	s32 ret_val;
450 
451 	memset(msgbuf, 0, sizeof(msgbuf));
452 	msgbuf[0] = IXGBE_VF_SET_MAC_ADDR;
453 	ether_addr_copy(msg_addr, addr);
454 
455 	ret_val = ixgbevf_write_msg_read_ack(hw, msgbuf, msgbuf,
456 					     sizeof(msgbuf) / sizeof(u32));
457 
458 	msgbuf[0] &= ~IXGBE_VT_MSGTYPE_CTS;
459 
460 	/* if nacked the address was rejected, use "perm_addr" */
461 	if (!ret_val &&
462 	    (msgbuf[0] == (IXGBE_VF_SET_MAC_ADDR | IXGBE_VT_MSGTYPE_NACK))) {
463 		ixgbevf_get_mac_addr_vf(hw, hw->mac.addr);
464 		return IXGBE_ERR_MBX;
465 	}
466 
467 	return ret_val;
468 }
469 
470 /**
471  *  ixgbevf_hv_set_rar_vf - set device MAC address Hyper-V variant
472  *  @hw: pointer to hardware structure
473  *  @index: Receive address register to write
474  *  @addr: Address to put into receive address register
475  *  @vmdq: Unused in this implementation
476  *
477  * We don't really allow setting the device MAC address. However,
478  * if the address being set is the permanent MAC address we will
479  * permit that.
480  **/
481 static s32 ixgbevf_hv_set_rar_vf(struct ixgbe_hw *hw, u32 index, u8 *addr,
482 				 u32 vmdq)
483 {
484 	if (ether_addr_equal(addr, hw->mac.perm_addr))
485 		return 0;
486 
487 	return -EOPNOTSUPP;
488 }
489 
490 /**
491  *  ixgbevf_update_mc_addr_list_vf - Update Multicast addresses
492  *  @hw: pointer to the HW structure
493  *  @netdev: pointer to net device structure
494  *
495  *  Updates the Multicast Table Array.
496  **/
497 static s32 ixgbevf_update_mc_addr_list_vf(struct ixgbe_hw *hw,
498 					  struct net_device *netdev)
499 {
500 	struct netdev_hw_addr *ha;
501 	u32 msgbuf[IXGBE_VFMAILBOX_SIZE];
502 	u16 *vector_list = (u16 *)&msgbuf[1];
503 	u32 cnt, i;
504 
505 	/* Each entry in the list uses 1 16 bit word.  We have 30
506 	 * 16 bit words available in our HW msg buffer (minus 1 for the
507 	 * msg type).  That's 30 hash values if we pack 'em right.  If
508 	 * there are more than 30 MC addresses to add then punt the
509 	 * extras for now and then add code to handle more than 30 later.
510 	 * It would be unusual for a server to request that many multi-cast
511 	 * addresses except for in large enterprise network environments.
512 	 */
513 
514 	cnt = netdev_mc_count(netdev);
515 	if (cnt > 30)
516 		cnt = 30;
517 	msgbuf[0] = IXGBE_VF_SET_MULTICAST;
518 	msgbuf[0] |= cnt << IXGBE_VT_MSGINFO_SHIFT;
519 
520 	i = 0;
521 	netdev_for_each_mc_addr(ha, netdev) {
522 		if (i == cnt)
523 			break;
524 		if (is_link_local_ether_addr(ha->addr))
525 			continue;
526 
527 		vector_list[i++] = ixgbevf_mta_vector(hw, ha->addr);
528 	}
529 
530 	ixgbevf_write_msg_read_ack(hw, msgbuf, msgbuf, IXGBE_VFMAILBOX_SIZE);
531 
532 	return 0;
533 }
534 
535 /**
536  * Hyper-V variant - just a stub.
537  */
538 static s32 ixgbevf_hv_update_mc_addr_list_vf(struct ixgbe_hw *hw,
539 					     struct net_device *netdev)
540 {
541 	return -EOPNOTSUPP;
542 }
543 
544 /**
545  *  ixgbevf_update_xcast_mode - Update Multicast mode
546  *  @hw: pointer to the HW structure
547  *  @xcast_mode: new multicast mode
548  *
549  *  Updates the Multicast Mode of VF.
550  **/
551 static s32 ixgbevf_update_xcast_mode(struct ixgbe_hw *hw, int xcast_mode)
552 {
553 	u32 msgbuf[2];
554 	s32 err;
555 
556 	switch (hw->api_version) {
557 	case ixgbe_mbox_api_12:
558 		/* promisc introduced in 1.3 version */
559 		if (xcast_mode == IXGBEVF_XCAST_MODE_PROMISC)
560 			return -EOPNOTSUPP;
561 		/* Fall threw */
562 	case ixgbe_mbox_api_13:
563 		break;
564 	default:
565 		return -EOPNOTSUPP;
566 	}
567 
568 	msgbuf[0] = IXGBE_VF_UPDATE_XCAST_MODE;
569 	msgbuf[1] = xcast_mode;
570 
571 	err = ixgbevf_write_msg_read_ack(hw, msgbuf, msgbuf,
572 					 sizeof(msgbuf) / sizeof(u32));
573 	if (err)
574 		return err;
575 
576 	msgbuf[0] &= ~IXGBE_VT_MSGTYPE_CTS;
577 	if (msgbuf[0] == (IXGBE_VF_UPDATE_XCAST_MODE | IXGBE_VT_MSGTYPE_NACK))
578 		return -EPERM;
579 
580 	return 0;
581 }
582 
583 /**
584  * Hyper-V variant - just a stub.
585  */
586 static s32 ixgbevf_hv_update_xcast_mode(struct ixgbe_hw *hw, int xcast_mode)
587 {
588 	return -EOPNOTSUPP;
589 }
590 
591 /**
592  *  ixgbevf_set_vfta_vf - Set/Unset VLAN filter table address
593  *  @hw: pointer to the HW structure
594  *  @vlan: 12 bit VLAN ID
595  *  @vind: unused by VF drivers
596  *  @vlan_on: if true then set bit, else clear bit
597  **/
598 static s32 ixgbevf_set_vfta_vf(struct ixgbe_hw *hw, u32 vlan, u32 vind,
599 			       bool vlan_on)
600 {
601 	u32 msgbuf[2];
602 	s32 err;
603 
604 	msgbuf[0] = IXGBE_VF_SET_VLAN;
605 	msgbuf[1] = vlan;
606 	/* Setting the 8 bit field MSG INFO to TRUE indicates "add" */
607 	msgbuf[0] |= vlan_on << IXGBE_VT_MSGINFO_SHIFT;
608 
609 	err = ixgbevf_write_msg_read_ack(hw, msgbuf, msgbuf,
610 					 sizeof(msgbuf) / sizeof(u32));
611 	if (err)
612 		goto mbx_err;
613 
614 	/* remove extra bits from the message */
615 	msgbuf[0] &= ~IXGBE_VT_MSGTYPE_CTS;
616 	msgbuf[0] &= ~(0xFF << IXGBE_VT_MSGINFO_SHIFT);
617 
618 	if (msgbuf[0] != (IXGBE_VF_SET_VLAN | IXGBE_VT_MSGTYPE_ACK))
619 		err = IXGBE_ERR_INVALID_ARGUMENT;
620 
621 mbx_err:
622 	return err;
623 }
624 
625 /**
626  * Hyper-V variant - just a stub.
627  */
628 static s32 ixgbevf_hv_set_vfta_vf(struct ixgbe_hw *hw, u32 vlan, u32 vind,
629 				  bool vlan_on)
630 {
631 	return -EOPNOTSUPP;
632 }
633 
634 /**
635  *  ixgbevf_setup_mac_link_vf - Setup MAC link settings
636  *  @hw: pointer to hardware structure
637  *  @speed: Unused in this implementation
638  *  @autoneg: Unused in this implementation
639  *  @autoneg_wait_to_complete: Unused in this implementation
640  *
641  *  Do nothing and return success.  VF drivers are not allowed to change
642  *  global settings.  Maintained for driver compatibility.
643  **/
644 static s32 ixgbevf_setup_mac_link_vf(struct ixgbe_hw *hw,
645 				     ixgbe_link_speed speed, bool autoneg,
646 				     bool autoneg_wait_to_complete)
647 {
648 	return 0;
649 }
650 
651 /**
652  *  ixgbevf_check_mac_link_vf - Get link/speed status
653  *  @hw: pointer to hardware structure
654  *  @speed: pointer to link speed
655  *  @link_up: true is link is up, false otherwise
656  *  @autoneg_wait_to_complete: true when waiting for completion is needed
657  *
658  *  Reads the links register to determine if link is up and the current speed
659  **/
660 static s32 ixgbevf_check_mac_link_vf(struct ixgbe_hw *hw,
661 				     ixgbe_link_speed *speed,
662 				     bool *link_up,
663 				     bool autoneg_wait_to_complete)
664 {
665 	struct ixgbe_mbx_info *mbx = &hw->mbx;
666 	struct ixgbe_mac_info *mac = &hw->mac;
667 	s32 ret_val = 0;
668 	u32 links_reg;
669 	u32 in_msg = 0;
670 
671 	/* If we were hit with a reset drop the link */
672 	if (!mbx->ops.check_for_rst(hw) || !mbx->timeout)
673 		mac->get_link_status = true;
674 
675 	if (!mac->get_link_status)
676 		goto out;
677 
678 	/* if link status is down no point in checking to see if pf is up */
679 	links_reg = IXGBE_READ_REG(hw, IXGBE_VFLINKS);
680 	if (!(links_reg & IXGBE_LINKS_UP))
681 		goto out;
682 
683 	/* for SFP+ modules and DA cables on 82599 it can take up to 500usecs
684 	 * before the link status is correct
685 	 */
686 	if (mac->type == ixgbe_mac_82599_vf) {
687 		int i;
688 
689 		for (i = 0; i < 5; i++) {
690 			udelay(100);
691 			links_reg = IXGBE_READ_REG(hw, IXGBE_VFLINKS);
692 
693 			if (!(links_reg & IXGBE_LINKS_UP))
694 				goto out;
695 		}
696 	}
697 
698 	switch (links_reg & IXGBE_LINKS_SPEED_82599) {
699 	case IXGBE_LINKS_SPEED_10G_82599:
700 		*speed = IXGBE_LINK_SPEED_10GB_FULL;
701 		break;
702 	case IXGBE_LINKS_SPEED_1G_82599:
703 		*speed = IXGBE_LINK_SPEED_1GB_FULL;
704 		break;
705 	case IXGBE_LINKS_SPEED_100_82599:
706 		*speed = IXGBE_LINK_SPEED_100_FULL;
707 		break;
708 	}
709 
710 	/* if the read failed it could just be a mailbox collision, best wait
711 	 * until we are called again and don't report an error
712 	 */
713 	if (mbx->ops.read(hw, &in_msg, 1))
714 		goto out;
715 
716 	if (!(in_msg & IXGBE_VT_MSGTYPE_CTS)) {
717 		/* msg is not CTS and is NACK we must have lost CTS status */
718 		if (in_msg & IXGBE_VT_MSGTYPE_NACK)
719 			ret_val = -1;
720 		goto out;
721 	}
722 
723 	/* the pf is talking, if we timed out in the past we reinit */
724 	if (!mbx->timeout) {
725 		ret_val = -1;
726 		goto out;
727 	}
728 
729 	/* if we passed all the tests above then the link is up and we no
730 	 * longer need to check for link
731 	 */
732 	mac->get_link_status = false;
733 
734 out:
735 	*link_up = !mac->get_link_status;
736 	return ret_val;
737 }
738 
739 /**
740  * Hyper-V variant; there is no mailbox communication.
741  */
742 static s32 ixgbevf_hv_check_mac_link_vf(struct ixgbe_hw *hw,
743 					ixgbe_link_speed *speed,
744 					bool *link_up,
745 					bool autoneg_wait_to_complete)
746 {
747 	struct ixgbe_mbx_info *mbx = &hw->mbx;
748 	struct ixgbe_mac_info *mac = &hw->mac;
749 	u32 links_reg;
750 
751 	/* If we were hit with a reset drop the link */
752 	if (!mbx->ops.check_for_rst(hw) || !mbx->timeout)
753 		mac->get_link_status = true;
754 
755 	if (!mac->get_link_status)
756 		goto out;
757 
758 	/* if link status is down no point in checking to see if pf is up */
759 	links_reg = IXGBE_READ_REG(hw, IXGBE_VFLINKS);
760 	if (!(links_reg & IXGBE_LINKS_UP))
761 		goto out;
762 
763 	/* for SFP+ modules and DA cables on 82599 it can take up to 500usecs
764 	 * before the link status is correct
765 	 */
766 	if (mac->type == ixgbe_mac_82599_vf) {
767 		int i;
768 
769 		for (i = 0; i < 5; i++) {
770 			udelay(100);
771 			links_reg = IXGBE_READ_REG(hw, IXGBE_VFLINKS);
772 
773 			if (!(links_reg & IXGBE_LINKS_UP))
774 				goto out;
775 		}
776 	}
777 
778 	switch (links_reg & IXGBE_LINKS_SPEED_82599) {
779 	case IXGBE_LINKS_SPEED_10G_82599:
780 		*speed = IXGBE_LINK_SPEED_10GB_FULL;
781 		break;
782 	case IXGBE_LINKS_SPEED_1G_82599:
783 		*speed = IXGBE_LINK_SPEED_1GB_FULL;
784 		break;
785 	case IXGBE_LINKS_SPEED_100_82599:
786 		*speed = IXGBE_LINK_SPEED_100_FULL;
787 		break;
788 	}
789 
790 	/* if we passed all the tests above then the link is up and we no
791 	 * longer need to check for link
792 	 */
793 	mac->get_link_status = false;
794 
795 out:
796 	*link_up = !mac->get_link_status;
797 	return 0;
798 }
799 
800 /**
801  *  ixgbevf_set_rlpml_vf - Set the maximum receive packet length
802  *  @hw: pointer to the HW structure
803  *  @max_size: value to assign to max frame size
804  **/
805 static s32 ixgbevf_set_rlpml_vf(struct ixgbe_hw *hw, u16 max_size)
806 {
807 	u32 msgbuf[2];
808 	s32 ret_val;
809 
810 	msgbuf[0] = IXGBE_VF_SET_LPE;
811 	msgbuf[1] = max_size;
812 
813 	ret_val = ixgbevf_write_msg_read_ack(hw, msgbuf, msgbuf,
814 					     sizeof(msgbuf) / sizeof(u32));
815 	if (ret_val)
816 		return ret_val;
817 	if ((msgbuf[0] & IXGBE_VF_SET_LPE) &&
818 	    (msgbuf[0] & IXGBE_VT_MSGTYPE_NACK))
819 		return IXGBE_ERR_MBX;
820 
821 	return 0;
822 }
823 
824 /**
825  * ixgbevf_hv_set_rlpml_vf - Set the maximum receive packet length
826  * @hw: pointer to the HW structure
827  * @max_size: value to assign to max frame size
828  * Hyper-V variant.
829  **/
830 static s32 ixgbevf_hv_set_rlpml_vf(struct ixgbe_hw *hw, u16 max_size)
831 {
832 	u32 reg;
833 
834 	/* If we are on Hyper-V, we implement this functionality
835 	 * differently.
836 	 */
837 	reg =  IXGBE_READ_REG(hw, IXGBE_VFRXDCTL(0));
838 	/* CRC == 4 */
839 	reg |= ((max_size + 4) | IXGBE_RXDCTL_RLPML_EN);
840 	IXGBE_WRITE_REG(hw, IXGBE_VFRXDCTL(0), reg);
841 
842 	return 0;
843 }
844 
845 /**
846  *  ixgbevf_negotiate_api_version_vf - Negotiate supported API version
847  *  @hw: pointer to the HW structure
848  *  @api: integer containing requested API version
849  **/
850 static int ixgbevf_negotiate_api_version_vf(struct ixgbe_hw *hw, int api)
851 {
852 	int err;
853 	u32 msg[3];
854 
855 	/* Negotiate the mailbox API version */
856 	msg[0] = IXGBE_VF_API_NEGOTIATE;
857 	msg[1] = api;
858 	msg[2] = 0;
859 
860 	err = ixgbevf_write_msg_read_ack(hw, msg, msg,
861 					 sizeof(msg) / sizeof(u32));
862 	if (!err) {
863 		msg[0] &= ~IXGBE_VT_MSGTYPE_CTS;
864 
865 		/* Store value and return 0 on success */
866 		if (msg[0] == (IXGBE_VF_API_NEGOTIATE | IXGBE_VT_MSGTYPE_ACK)) {
867 			hw->api_version = api;
868 			return 0;
869 		}
870 
871 		err = IXGBE_ERR_INVALID_ARGUMENT;
872 	}
873 
874 	return err;
875 }
876 
877 /**
878  *  ixgbevf_hv_negotiate_api_version_vf - Negotiate supported API version
879  *  @hw: pointer to the HW structure
880  *  @api: integer containing requested API version
881  *  Hyper-V version - only ixgbe_mbox_api_10 supported.
882  **/
883 static int ixgbevf_hv_negotiate_api_version_vf(struct ixgbe_hw *hw, int api)
884 {
885 	/* Hyper-V only supports api version ixgbe_mbox_api_10 */
886 	if (api != ixgbe_mbox_api_10)
887 		return IXGBE_ERR_INVALID_ARGUMENT;
888 
889 	return 0;
890 }
891 
892 int ixgbevf_get_queues(struct ixgbe_hw *hw, unsigned int *num_tcs,
893 		       unsigned int *default_tc)
894 {
895 	int err;
896 	u32 msg[5];
897 
898 	/* do nothing if API doesn't support ixgbevf_get_queues */
899 	switch (hw->api_version) {
900 	case ixgbe_mbox_api_11:
901 	case ixgbe_mbox_api_12:
902 	case ixgbe_mbox_api_13:
903 		break;
904 	default:
905 		return 0;
906 	}
907 
908 	/* Fetch queue configuration from the PF */
909 	msg[0] = IXGBE_VF_GET_QUEUE;
910 	msg[1] = msg[2] = msg[3] = msg[4] = 0;
911 
912 	err = ixgbevf_write_msg_read_ack(hw, msg, msg,
913 					 sizeof(msg) / sizeof(u32));
914 	if (!err) {
915 		msg[0] &= ~IXGBE_VT_MSGTYPE_CTS;
916 
917 		/* if we we didn't get an ACK there must have been
918 		 * some sort of mailbox error so we should treat it
919 		 * as such
920 		 */
921 		if (msg[0] != (IXGBE_VF_GET_QUEUE | IXGBE_VT_MSGTYPE_ACK))
922 			return IXGBE_ERR_MBX;
923 
924 		/* record and validate values from message */
925 		hw->mac.max_tx_queues = msg[IXGBE_VF_TX_QUEUES];
926 		if (hw->mac.max_tx_queues == 0 ||
927 		    hw->mac.max_tx_queues > IXGBE_VF_MAX_TX_QUEUES)
928 			hw->mac.max_tx_queues = IXGBE_VF_MAX_TX_QUEUES;
929 
930 		hw->mac.max_rx_queues = msg[IXGBE_VF_RX_QUEUES];
931 		if (hw->mac.max_rx_queues == 0 ||
932 		    hw->mac.max_rx_queues > IXGBE_VF_MAX_RX_QUEUES)
933 			hw->mac.max_rx_queues = IXGBE_VF_MAX_RX_QUEUES;
934 
935 		*num_tcs = msg[IXGBE_VF_TRANS_VLAN];
936 		/* in case of unknown state assume we cannot tag frames */
937 		if (*num_tcs > hw->mac.max_rx_queues)
938 			*num_tcs = 1;
939 
940 		*default_tc = msg[IXGBE_VF_DEF_QUEUE];
941 		/* default to queue 0 on out-of-bounds queue number */
942 		if (*default_tc >= hw->mac.max_tx_queues)
943 			*default_tc = 0;
944 	}
945 
946 	return err;
947 }
948 
949 static const struct ixgbe_mac_operations ixgbevf_mac_ops = {
950 	.init_hw		= ixgbevf_init_hw_vf,
951 	.reset_hw		= ixgbevf_reset_hw_vf,
952 	.start_hw		= ixgbevf_start_hw_vf,
953 	.get_mac_addr		= ixgbevf_get_mac_addr_vf,
954 	.stop_adapter		= ixgbevf_stop_hw_vf,
955 	.setup_link		= ixgbevf_setup_mac_link_vf,
956 	.check_link		= ixgbevf_check_mac_link_vf,
957 	.negotiate_api_version	= ixgbevf_negotiate_api_version_vf,
958 	.set_rar		= ixgbevf_set_rar_vf,
959 	.update_mc_addr_list	= ixgbevf_update_mc_addr_list_vf,
960 	.update_xcast_mode	= ixgbevf_update_xcast_mode,
961 	.set_uc_addr		= ixgbevf_set_uc_addr_vf,
962 	.set_vfta		= ixgbevf_set_vfta_vf,
963 	.set_rlpml		= ixgbevf_set_rlpml_vf,
964 };
965 
966 static const struct ixgbe_mac_operations ixgbevf_hv_mac_ops = {
967 	.init_hw		= ixgbevf_init_hw_vf,
968 	.reset_hw		= ixgbevf_hv_reset_hw_vf,
969 	.start_hw		= ixgbevf_start_hw_vf,
970 	.get_mac_addr		= ixgbevf_get_mac_addr_vf,
971 	.stop_adapter		= ixgbevf_stop_hw_vf,
972 	.setup_link		= ixgbevf_setup_mac_link_vf,
973 	.check_link		= ixgbevf_hv_check_mac_link_vf,
974 	.negotiate_api_version	= ixgbevf_hv_negotiate_api_version_vf,
975 	.set_rar		= ixgbevf_hv_set_rar_vf,
976 	.update_mc_addr_list	= ixgbevf_hv_update_mc_addr_list_vf,
977 	.update_xcast_mode	= ixgbevf_hv_update_xcast_mode,
978 	.set_uc_addr		= ixgbevf_hv_set_uc_addr_vf,
979 	.set_vfta		= ixgbevf_hv_set_vfta_vf,
980 	.set_rlpml		= ixgbevf_hv_set_rlpml_vf,
981 };
982 
983 const struct ixgbevf_info ixgbevf_82599_vf_info = {
984 	.mac = ixgbe_mac_82599_vf,
985 	.mac_ops = &ixgbevf_mac_ops,
986 };
987 
988 const struct ixgbevf_info ixgbevf_82599_vf_hv_info = {
989 	.mac = ixgbe_mac_82599_vf,
990 	.mac_ops = &ixgbevf_hv_mac_ops,
991 };
992 
993 const struct ixgbevf_info ixgbevf_X540_vf_info = {
994 	.mac = ixgbe_mac_X540_vf,
995 	.mac_ops = &ixgbevf_mac_ops,
996 };
997 
998 const struct ixgbevf_info ixgbevf_X540_vf_hv_info = {
999 	.mac = ixgbe_mac_X540_vf,
1000 	.mac_ops = &ixgbevf_hv_mac_ops,
1001 };
1002 
1003 const struct ixgbevf_info ixgbevf_X550_vf_info = {
1004 	.mac = ixgbe_mac_X550_vf,
1005 	.mac_ops = &ixgbevf_mac_ops,
1006 };
1007 
1008 const struct ixgbevf_info ixgbevf_X550_vf_hv_info = {
1009 	.mac = ixgbe_mac_X550_vf,
1010 	.mac_ops = &ixgbevf_hv_mac_ops,
1011 };
1012 
1013 const struct ixgbevf_info ixgbevf_X550EM_x_vf_info = {
1014 	.mac = ixgbe_mac_X550EM_x_vf,
1015 	.mac_ops = &ixgbevf_mac_ops,
1016 };
1017 
1018 const struct ixgbevf_info ixgbevf_X550EM_x_vf_hv_info = {
1019 	.mac = ixgbe_mac_X550EM_x_vf,
1020 	.mac_ops = &ixgbevf_hv_mac_ops,
1021 };
1022 
1023 const struct ixgbevf_info ixgbevf_x550em_a_vf_info = {
1024 	.mac = ixgbe_mac_x550em_a_vf,
1025 	.mac_ops = &ixgbevf_mac_ops,
1026 };
1027