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