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