ixgbe: configure VF RSS
[dpdk.git] / lib / librte_pmd_ixgbe / ixgbe_pf.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #include <stdio.h>
35 #include <errno.h>
36 #include <stdint.h>
37 #include <stdlib.h>
38 #include <unistd.h>
39 #include <stdarg.h>
40 #include <inttypes.h>
41
42 #include <rte_interrupts.h>
43 #include <rte_log.h>
44 #include <rte_debug.h>
45 #include <rte_eal.h>
46 #include <rte_ether.h>
47 #include <rte_ethdev.h>
48 #include <rte_memcpy.h>
49 #include <rte_malloc.h>
50 #include <rte_random.h>
51
52 #include "ixgbe/ixgbe_common.h"
53 #include "ixgbe_ethdev.h"
54
55 #define IXGBE_MAX_VFTA     (128)
56 #define IXGBE_VF_MSG_SIZE_DEFAULT 1
57 #define IXGBE_VF_GET_QUEUE_MSG_SIZE 5
58
59 static inline uint16_t
60 dev_num_vf(struct rte_eth_dev *eth_dev)
61 {
62         return eth_dev->pci_dev->max_vfs;
63 }
64
65 static inline
66 int ixgbe_vf_perm_addr_gen(struct rte_eth_dev *dev, uint16_t vf_num)
67 {
68         unsigned char vf_mac_addr[ETHER_ADDR_LEN];
69         struct ixgbe_vf_info *vfinfo =
70                 *IXGBE_DEV_PRIVATE_TO_P_VFDATA(dev->data->dev_private);
71         uint16_t vfn;
72
73         for (vfn = 0; vfn < vf_num; vfn++) {
74                 eth_random_addr(vf_mac_addr);
75                 /* keep the random address as default */
76                 memcpy(vfinfo[vfn].vf_mac_addresses, vf_mac_addr,
77                            ETHER_ADDR_LEN);
78         }
79
80         return 0;
81 }
82
83 static inline int
84 ixgbe_mb_intr_setup(struct rte_eth_dev *dev)
85 {
86         struct ixgbe_interrupt *intr =
87                 IXGBE_DEV_PRIVATE_TO_INTR(dev->data->dev_private);
88
89         intr->mask |= IXGBE_EICR_MAILBOX;
90
91         return 0;
92 }
93
94 void ixgbe_pf_host_init(struct rte_eth_dev *eth_dev)
95 {
96         struct ixgbe_vf_info **vfinfo =
97                 IXGBE_DEV_PRIVATE_TO_P_VFDATA(eth_dev->data->dev_private);
98         struct ixgbe_mirror_info *mirror_info =
99         IXGBE_DEV_PRIVATE_TO_PFDATA(eth_dev->data->dev_private);
100         struct ixgbe_uta_info *uta_info =
101         IXGBE_DEV_PRIVATE_TO_UTA(eth_dev->data->dev_private);
102         struct ixgbe_hw *hw =
103                 IXGBE_DEV_PRIVATE_TO_HW(eth_dev->data->dev_private);
104         uint16_t vf_num;
105         uint8_t nb_queue;
106
107         PMD_INIT_FUNC_TRACE();
108
109         RTE_ETH_DEV_SRIOV(eth_dev).active = 0;
110         if (0 == (vf_num = dev_num_vf(eth_dev)))
111                 return;
112
113         *vfinfo = rte_zmalloc("vf_info", sizeof(struct ixgbe_vf_info) * vf_num, 0);
114         if (*vfinfo == NULL)
115                 rte_panic("Cannot allocate memory for private VF data\n");
116
117         memset(mirror_info,0,sizeof(struct ixgbe_mirror_info));
118         memset(uta_info,0,sizeof(struct ixgbe_uta_info));
119         hw->mac.mc_filter_type = 0;
120
121         if (vf_num >= ETH_32_POOLS) {
122                 nb_queue = 2;
123                 RTE_ETH_DEV_SRIOV(eth_dev).active = ETH_64_POOLS;
124         } else if (vf_num >= ETH_16_POOLS) {
125                 nb_queue = 4;
126                 RTE_ETH_DEV_SRIOV(eth_dev).active = ETH_32_POOLS;
127         } else {
128                 nb_queue = 8;
129                 RTE_ETH_DEV_SRIOV(eth_dev).active = ETH_16_POOLS;
130         }
131
132         RTE_ETH_DEV_SRIOV(eth_dev).nb_q_per_pool = nb_queue;
133         RTE_ETH_DEV_SRIOV(eth_dev).def_vmdq_idx = vf_num;
134         RTE_ETH_DEV_SRIOV(eth_dev).def_pool_q_idx = (uint16_t)(vf_num * nb_queue);
135
136         ixgbe_vf_perm_addr_gen(eth_dev, vf_num);
137
138         /* init_mailbox_params */
139         hw->mbx.ops.init_params(hw);
140
141         /* set mb interrupt mask */
142         ixgbe_mb_intr_setup(eth_dev);
143
144         return;
145 }
146
147 int ixgbe_pf_host_configure(struct rte_eth_dev *eth_dev)
148 {
149         uint32_t vtctl, fcrth;
150         uint32_t vfre_slot, vfre_offset;
151         uint16_t vf_num;
152         const uint8_t VFRE_SHIFT = 5;  /* VFRE 32 bits per slot */
153         const uint8_t VFRE_MASK = (uint8_t)((1U << VFRE_SHIFT) - 1);
154         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(eth_dev->data->dev_private);
155         uint32_t gpie, gcr_ext;
156         uint32_t vlanctrl;
157         int i;
158
159         if (0 == (vf_num = dev_num_vf(eth_dev)))
160                 return -1;
161
162         /* enable VMDq and set the default pool for PF */
163         vtctl = IXGBE_READ_REG(hw, IXGBE_VT_CTL);
164         vtctl |= IXGBE_VMD_CTL_VMDQ_EN;
165         vtctl &= ~IXGBE_VT_CTL_POOL_MASK;
166         vtctl |= RTE_ETH_DEV_SRIOV(eth_dev).def_vmdq_idx
167                 << IXGBE_VT_CTL_POOL_SHIFT;
168         vtctl |= IXGBE_VT_CTL_REPLEN;
169         IXGBE_WRITE_REG(hw, IXGBE_VT_CTL, vtctl);
170
171         vfre_offset = vf_num & VFRE_MASK;
172         vfre_slot = (vf_num >> VFRE_SHIFT) > 0 ? 1 : 0;
173
174         /* Enable pools reserved to PF only */
175         IXGBE_WRITE_REG(hw, IXGBE_VFRE(vfre_slot), (~0) << vfre_offset);
176         IXGBE_WRITE_REG(hw, IXGBE_VFRE(vfre_slot ^ 1), vfre_slot - 1);
177         IXGBE_WRITE_REG(hw, IXGBE_VFTE(vfre_slot), (~0) << vfre_offset);
178         IXGBE_WRITE_REG(hw, IXGBE_VFTE(vfre_slot ^ 1), vfre_slot - 1);
179
180         /* PFDMA Tx General Switch Control Enables VMDQ loopback */
181         IXGBE_WRITE_REG(hw, IXGBE_PFDTXGSWC, IXGBE_PFDTXGSWC_VT_LBEN);
182
183         /* clear VMDq map to perment rar 0 */
184         hw->mac.ops.clear_vmdq(hw, 0, IXGBE_CLEAR_VMDQ_ALL);
185
186         /* clear VMDq map to scan rar 127 */
187         IXGBE_WRITE_REG(hw, IXGBE_MPSAR_LO(hw->mac.num_rar_entries), 0);
188         IXGBE_WRITE_REG(hw, IXGBE_MPSAR_HI(hw->mac.num_rar_entries), 0);
189
190         /*
191          * VF RSS can support at most 4 queues for each VF, even if
192          * 8 queues are available for each VF, it need refine to 4
193          * queues here due to this limitation, otherwise no queue
194          * will receive any packet even RSS is enabled.
195          */
196         if (eth_dev->data->dev_conf.rxmode.mq_mode == ETH_MQ_RX_VMDQ_RSS) {
197                 if (RTE_ETH_DEV_SRIOV(eth_dev).nb_q_per_pool == 8) {
198                         RTE_ETH_DEV_SRIOV(eth_dev).active = ETH_32_POOLS;
199                         RTE_ETH_DEV_SRIOV(eth_dev).nb_q_per_pool = 4;
200                         RTE_ETH_DEV_SRIOV(eth_dev).def_pool_q_idx =
201                                 dev_num_vf(eth_dev) * 4;
202                 }
203         }
204
205         /* set VMDq map to default PF pool */
206         hw->mac.ops.set_vmdq(hw, 0, RTE_ETH_DEV_SRIOV(eth_dev).def_vmdq_idx);
207
208         /*
209          * SW msut set GCR_EXT.VT_Mode the same as GPIE.VT_Mode
210          */
211         gcr_ext = IXGBE_READ_REG(hw, IXGBE_GCR_EXT);
212         gcr_ext &= ~IXGBE_GCR_EXT_VT_MODE_MASK;
213
214         gpie = IXGBE_READ_REG(hw, IXGBE_GPIE);
215         gpie &= ~IXGBE_GPIE_VTMODE_MASK;
216         gpie |= IXGBE_GPIE_MSIX_MODE;
217
218         switch (RTE_ETH_DEV_SRIOV(eth_dev).active) {
219         case ETH_64_POOLS:
220                 gcr_ext |= IXGBE_GCR_EXT_VT_MODE_64;
221                 gpie |= IXGBE_GPIE_VTMODE_64;
222                 break;
223         case ETH_32_POOLS:
224                 gcr_ext |= IXGBE_GCR_EXT_VT_MODE_32;
225                 gpie |= IXGBE_GPIE_VTMODE_32;
226                 break;
227         case ETH_16_POOLS:
228                 gcr_ext |= IXGBE_GCR_EXT_VT_MODE_16;
229                 gpie |= IXGBE_GPIE_VTMODE_16;
230                 break;
231         }
232
233         IXGBE_WRITE_REG(hw, IXGBE_GCR_EXT, gcr_ext);
234         IXGBE_WRITE_REG(hw, IXGBE_GPIE, gpie);
235
236         /*
237          * enable vlan filtering and allow all vlan tags through
238          */
239         vlanctrl = IXGBE_READ_REG(hw, IXGBE_VLNCTRL);
240         vlanctrl |= IXGBE_VLNCTRL_VFE ; /* enable vlan filters */
241         IXGBE_WRITE_REG(hw, IXGBE_VLNCTRL, vlanctrl);
242
243         /* VFTA - enable all vlan filters */
244         for (i = 0; i < IXGBE_MAX_VFTA; i++) {
245                 IXGBE_WRITE_REG(hw, IXGBE_VFTA(i), 0xFFFFFFFF);
246         }
247
248         /* Enable MAC Anti-Spoofing */
249         hw->mac.ops.set_mac_anti_spoofing(hw, FALSE, vf_num);
250
251         /* set flow control threshold to max to avoid tx switch hang */
252         for (i = 0; i < IXGBE_DCB_MAX_TRAFFIC_CLASS; i++) {
253                 IXGBE_WRITE_REG(hw, IXGBE_FCRTL_82599(i), 0);
254                 fcrth = IXGBE_READ_REG(hw, IXGBE_RXPBSIZE(i)) - 32;
255                 IXGBE_WRITE_REG(hw, IXGBE_FCRTH_82599(i), fcrth);
256         }
257
258         return 0;
259 }
260
261 static void
262 set_rx_mode(struct rte_eth_dev *dev)
263 {
264         struct rte_eth_dev_data *dev_data =
265                 (struct rte_eth_dev_data*)dev->data->dev_private;
266         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
267         u32 fctrl, vmolr = IXGBE_VMOLR_BAM | IXGBE_VMOLR_AUPE;
268         uint16_t vfn = dev_num_vf(dev);
269
270         /* Check for Promiscuous and All Multicast modes */
271         fctrl = IXGBE_READ_REG(hw, IXGBE_FCTRL);
272
273         /* set all bits that we expect to always be set */
274         fctrl &= ~IXGBE_FCTRL_SBP; /* disable store-bad-packets */
275         fctrl |= IXGBE_FCTRL_BAM;
276
277         /* clear the bits we are changing the status of */
278         fctrl &= ~(IXGBE_FCTRL_UPE | IXGBE_FCTRL_MPE);
279
280         if (dev_data->promiscuous) {
281                 fctrl |= (IXGBE_FCTRL_UPE | IXGBE_FCTRL_MPE);
282                 vmolr |= (IXGBE_VMOLR_ROPE | IXGBE_VMOLR_MPE);
283         } else {
284                 if (dev_data->all_multicast) {
285                         fctrl |= IXGBE_FCTRL_MPE;
286                         vmolr |= IXGBE_VMOLR_MPE;
287                 } else {
288                         vmolr |= IXGBE_VMOLR_ROMPE;
289                 }
290         }
291
292         if (hw->mac.type != ixgbe_mac_82598EB) {
293                 vmolr |= IXGBE_READ_REG(hw, IXGBE_VMOLR(vfn)) &
294                          ~(IXGBE_VMOLR_MPE | IXGBE_VMOLR_ROMPE |
295                            IXGBE_VMOLR_ROPE);
296                 IXGBE_WRITE_REG(hw, IXGBE_VMOLR(vfn), vmolr);
297         }
298
299         IXGBE_WRITE_REG(hw, IXGBE_FCTRL, fctrl);
300
301         if (dev->data->dev_conf.rxmode.hw_vlan_strip)
302                 ixgbe_vlan_hw_strip_enable_all(dev);
303         else
304                 ixgbe_vlan_hw_strip_disable_all(dev);
305 }
306
307 static inline void
308 ixgbe_vf_reset_event(struct rte_eth_dev *dev, uint16_t vf)
309 {
310         struct ixgbe_hw *hw =
311                 IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
312         struct ixgbe_vf_info *vfinfo =
313                 *(IXGBE_DEV_PRIVATE_TO_P_VFDATA(dev->data->dev_private));
314         int rar_entry = hw->mac.num_rar_entries - (vf + 1);
315         uint32_t vmolr = IXGBE_READ_REG(hw, IXGBE_VMOLR(vf));
316
317         vmolr |= (IXGBE_VMOLR_ROPE | IXGBE_VMOLR_ROMPE |
318                         IXGBE_VMOLR_BAM | IXGBE_VMOLR_AUPE);
319         IXGBE_WRITE_REG(hw, IXGBE_VMOLR(vf), vmolr);
320
321         IXGBE_WRITE_REG(hw, IXGBE_VMVIR(vf), 0);
322
323         /* reset multicast table array for vf */
324         vfinfo[vf].num_vf_mc_hashes = 0;
325
326         /* reset rx mode */
327         set_rx_mode(dev);
328
329         hw->mac.ops.clear_rar(hw, rar_entry);
330 }
331
332 static inline void
333 ixgbe_vf_reset_msg(struct rte_eth_dev *dev, uint16_t vf)
334 {
335         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
336         uint32_t reg;
337         uint32_t reg_offset, vf_shift;
338         const uint8_t VFRE_SHIFT = 5;  /* VFRE 32 bits per slot */
339         const uint8_t VFRE_MASK = (uint8_t)((1U << VFRE_SHIFT) - 1);
340
341         vf_shift = vf & VFRE_MASK;
342         reg_offset = (vf >> VFRE_SHIFT) > 0 ? 1 : 0;
343
344         /* enable transmit and receive for vf */
345         reg = IXGBE_READ_REG(hw, IXGBE_VFTE(reg_offset));
346         reg |= (reg | (1 << vf_shift));
347         IXGBE_WRITE_REG(hw, IXGBE_VFTE(reg_offset), reg);
348
349         reg = IXGBE_READ_REG(hw, IXGBE_VFRE(reg_offset));
350         reg |= (reg | (1 << vf_shift));
351         IXGBE_WRITE_REG(hw, IXGBE_VFRE(reg_offset), reg);
352
353         /* Enable counting of spoofed packets in the SSVPC register */
354         reg = IXGBE_READ_REG(hw, IXGBE_VMECM(reg_offset));
355         reg |= (1 << vf_shift);
356         IXGBE_WRITE_REG(hw, IXGBE_VMECM(reg_offset), reg);
357
358         ixgbe_vf_reset_event(dev, vf);
359 }
360
361 static int
362 ixgbe_vf_reset(struct rte_eth_dev *dev, uint16_t vf, uint32_t *msgbuf)
363 {
364         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
365         struct ixgbe_vf_info *vfinfo =
366                 *(IXGBE_DEV_PRIVATE_TO_P_VFDATA(dev->data->dev_private));
367         unsigned char *vf_mac = vfinfo[vf].vf_mac_addresses;
368         int rar_entry = hw->mac.num_rar_entries - (vf + 1);
369         uint8_t *new_mac = (uint8_t *)(&msgbuf[1]);
370
371         ixgbe_vf_reset_msg(dev, vf);
372
373         hw->mac.ops.set_rar(hw, rar_entry, vf_mac, vf, IXGBE_RAH_AV);
374
375         /* reply to reset with ack and vf mac address */
376         msgbuf[0] = IXGBE_VF_RESET | IXGBE_VT_MSGTYPE_ACK;
377         rte_memcpy(new_mac, vf_mac, ETHER_ADDR_LEN);
378         /*
379          * Piggyback the multicast filter type so VF can compute the
380          * correct vectors
381          */
382         msgbuf[3] = hw->mac.mc_filter_type;
383         ixgbe_write_mbx(hw, msgbuf, IXGBE_VF_PERMADDR_MSG_LEN, vf);
384
385         return 0;
386 }
387
388 static int
389 ixgbe_vf_set_mac_addr(struct rte_eth_dev *dev, uint32_t vf, uint32_t *msgbuf)
390 {
391         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
392         struct ixgbe_vf_info *vfinfo =
393                 *(IXGBE_DEV_PRIVATE_TO_P_VFDATA(dev->data->dev_private));
394         int rar_entry = hw->mac.num_rar_entries - (vf + 1);
395         uint8_t *new_mac = (uint8_t *)(&msgbuf[1]);
396
397         if (is_valid_assigned_ether_addr((struct ether_addr*)new_mac)) {
398                 rte_memcpy(vfinfo[vf].vf_mac_addresses, new_mac, 6);
399                 return hw->mac.ops.set_rar(hw, rar_entry, new_mac, vf, IXGBE_RAH_AV);
400         }
401         return -1;
402 }
403
404 static int
405 ixgbe_vf_set_multicast(struct rte_eth_dev *dev, __rte_unused uint32_t vf, uint32_t *msgbuf)
406 {
407         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
408         struct ixgbe_vf_info *vfinfo =
409                 *(IXGBE_DEV_PRIVATE_TO_P_VFDATA(dev->data->dev_private));
410         int nb_entries = (msgbuf[0] & IXGBE_VT_MSGINFO_MASK) >>
411                 IXGBE_VT_MSGINFO_SHIFT;
412         uint16_t *hash_list = (uint16_t *)&msgbuf[1];
413         uint32_t mta_idx;
414         uint32_t mta_shift;
415         const uint32_t IXGBE_MTA_INDEX_MASK = 0x7F;
416         const uint32_t IXGBE_MTA_BIT_SHIFT = 5;
417         const uint32_t IXGBE_MTA_BIT_MASK = (0x1 << IXGBE_MTA_BIT_SHIFT) - 1;
418         uint32_t reg_val;
419         int i;
420
421         /* only so many hash values supported */
422         nb_entries = RTE_MIN(nb_entries, IXGBE_MAX_VF_MC_ENTRIES);
423
424         /* store the mc entries  */
425         vfinfo->num_vf_mc_hashes = (uint16_t)nb_entries;
426         for (i = 0; i < nb_entries; i++) {
427                 vfinfo->vf_mc_hashes[i] = hash_list[i];
428         }
429
430         for (i = 0; i < vfinfo->num_vf_mc_hashes; i++) {
431                 mta_idx = (vfinfo->vf_mc_hashes[i] >> IXGBE_MTA_BIT_SHIFT)
432                                 & IXGBE_MTA_INDEX_MASK;
433                 mta_shift = vfinfo->vf_mc_hashes[i] & IXGBE_MTA_BIT_MASK;
434                 reg_val = IXGBE_READ_REG(hw, IXGBE_MTA(mta_idx));
435                 reg_val |= (1 << mta_shift);
436                 IXGBE_WRITE_REG(hw, IXGBE_MTA(mta_idx), reg_val);
437         }
438
439         return 0;
440 }
441
442 static int
443 ixgbe_vf_set_vlan(struct rte_eth_dev *dev, uint32_t vf, uint32_t *msgbuf)
444 {
445         int add, vid;
446         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
447         struct ixgbe_vf_info *vfinfo =
448                 *(IXGBE_DEV_PRIVATE_TO_P_VFDATA(dev->data->dev_private));
449
450         add = (msgbuf[0] & IXGBE_VT_MSGINFO_MASK)
451                 >> IXGBE_VT_MSGINFO_SHIFT;
452         vid = (msgbuf[1] & IXGBE_VLVF_VLANID_MASK);
453
454         if (add)
455                 vfinfo[vf].vlan_count++;
456         else if (vfinfo[vf].vlan_count)
457                 vfinfo[vf].vlan_count--;
458         return hw->mac.ops.set_vfta(hw, vid, vf, (bool)add);
459 }
460
461 static int
462 ixgbe_set_vf_lpe(struct rte_eth_dev *dev, __rte_unused uint32_t vf, uint32_t *msgbuf)
463 {
464         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
465         uint32_t new_mtu = msgbuf[1];
466         uint32_t max_frs;
467         int max_frame = new_mtu + ETHER_HDR_LEN + ETHER_CRC_LEN;
468
469         /* X540 and X550 support jumbo frames in IOV mode */
470         if (hw->mac.type != ixgbe_mac_X540 &&
471                 hw->mac.type != ixgbe_mac_X550 &&
472                 hw->mac.type != ixgbe_mac_X550EM_x)
473                 return -1;
474
475         if ((max_frame < ETHER_MIN_LEN) || (max_frame > ETHER_MAX_JUMBO_FRAME_LEN))
476                 return -1;
477
478         max_frs = (IXGBE_READ_REG(hw, IXGBE_MAXFRS) &
479                    IXGBE_MHADD_MFS_MASK) >> IXGBE_MHADD_MFS_SHIFT;
480         if (max_frs < new_mtu) {
481                 max_frs = new_mtu << IXGBE_MHADD_MFS_SHIFT;
482                 IXGBE_WRITE_REG(hw, IXGBE_MAXFRS, max_frs);
483         }
484
485         return 0;
486 }
487
488 static int
489 ixgbe_negotiate_vf_api(struct rte_eth_dev *dev, uint32_t vf, uint32_t *msgbuf)
490 {
491         uint32_t api_version = msgbuf[1];
492         struct ixgbe_vf_info *vfinfo =
493                 *IXGBE_DEV_PRIVATE_TO_P_VFDATA(dev->data->dev_private);
494
495         switch (api_version) {
496         case ixgbe_mbox_api_10:
497         case ixgbe_mbox_api_11:
498                 vfinfo[vf].api_version = (uint8_t)api_version;
499                 return 0;
500         default:
501                 break;
502         }
503
504         RTE_LOG(ERR, PMD, "Negotiate invalid api version %u from VF %d\n",
505                 api_version, vf);
506
507         return -1;
508 }
509
510 static int
511 ixgbe_get_vf_queues(struct rte_eth_dev *dev, uint32_t vf, uint32_t *msgbuf)
512 {
513         struct ixgbe_vf_info *vfinfo =
514                 *IXGBE_DEV_PRIVATE_TO_P_VFDATA(dev->data->dev_private);
515         uint32_t default_q = vf * RTE_ETH_DEV_SRIOV(dev).nb_q_per_pool;
516
517         /* Verify if the PF supports the mbox APIs version or not */
518         switch (vfinfo[vf].api_version) {
519         case ixgbe_mbox_api_20:
520         case ixgbe_mbox_api_11:
521                 break;
522         default:
523                 return -1;
524         }
525
526         /* Notify VF of Rx and Tx queue number */
527         msgbuf[IXGBE_VF_RX_QUEUES] = RTE_ETH_DEV_SRIOV(dev).nb_q_per_pool;
528         msgbuf[IXGBE_VF_TX_QUEUES] = RTE_ETH_DEV_SRIOV(dev).nb_q_per_pool;
529
530         /* Notify VF of default queue */
531         msgbuf[IXGBE_VF_DEF_QUEUE] = default_q;
532
533         /*
534          * FIX ME if it needs fill msgbuf[IXGBE_VF_TRANS_VLAN]
535          * for VLAN strip or VMDQ_DCB or VMDQ_DCB_RSS
536          */
537
538         return 0;
539 }
540
541 static int
542 ixgbe_rcv_msg_from_vf(struct rte_eth_dev *dev, uint16_t vf)
543 {
544         uint16_t mbx_size = IXGBE_VFMAILBOX_SIZE;
545         uint16_t msg_size = IXGBE_VF_MSG_SIZE_DEFAULT;
546         uint32_t msgbuf[IXGBE_VFMAILBOX_SIZE];
547         int32_t retval;
548         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
549         struct ixgbe_vf_info *vfinfo =
550                 *IXGBE_DEV_PRIVATE_TO_P_VFDATA(dev->data->dev_private);
551
552         retval = ixgbe_read_mbx(hw, msgbuf, mbx_size, vf);
553         if (retval) {
554                 PMD_DRV_LOG(ERR, "Error mbx recv msg from VF %d", vf);
555                 return retval;
556         }
557
558         /* do nothing with the message already been processed */
559         if (msgbuf[0] & (IXGBE_VT_MSGTYPE_ACK | IXGBE_VT_MSGTYPE_NACK))
560                 return retval;
561
562         /* flush the ack before we write any messages back */
563         IXGBE_WRITE_FLUSH(hw);
564
565         /* perform VF reset */
566         if (msgbuf[0] == IXGBE_VF_RESET) {
567                 int ret = ixgbe_vf_reset(dev, vf, msgbuf);
568                 vfinfo[vf].clear_to_send = true;
569                 return ret;
570         }
571
572         /* check & process VF to PF mailbox message */
573         switch ((msgbuf[0] & 0xFFFF)) {
574         case IXGBE_VF_SET_MAC_ADDR:
575                 retval = ixgbe_vf_set_mac_addr(dev, vf, msgbuf);
576                 break;
577         case IXGBE_VF_SET_MULTICAST:
578                 retval = ixgbe_vf_set_multicast(dev, vf, msgbuf);
579                 break;
580         case IXGBE_VF_SET_LPE:
581                 retval = ixgbe_set_vf_lpe(dev, vf, msgbuf);
582                 break;
583         case IXGBE_VF_SET_VLAN:
584                 retval = ixgbe_vf_set_vlan(dev, vf, msgbuf);
585                 break;
586         case IXGBE_VF_API_NEGOTIATE:
587                 retval = ixgbe_negotiate_vf_api(dev, vf, msgbuf);
588                 break;
589         case IXGBE_VF_GET_QUEUES:
590                 retval = ixgbe_get_vf_queues(dev, vf, msgbuf);
591                 msg_size = IXGBE_VF_GET_QUEUE_MSG_SIZE;
592                 break;
593         default:
594                 PMD_DRV_LOG(DEBUG, "Unhandled Msg %8.8x", (unsigned)msgbuf[0]);
595                 retval = IXGBE_ERR_MBX;
596                 break;
597         }
598
599         /* response the VF according to the message process result */
600         if (retval)
601                 msgbuf[0] |= IXGBE_VT_MSGTYPE_NACK;
602         else
603                 msgbuf[0] |= IXGBE_VT_MSGTYPE_ACK;
604
605         msgbuf[0] |= IXGBE_VT_MSGTYPE_CTS;
606
607         ixgbe_write_mbx(hw, msgbuf, msg_size, vf);
608
609         return retval;
610 }
611
612 static inline void
613 ixgbe_rcv_ack_from_vf(struct rte_eth_dev *dev, uint16_t vf)
614 {
615         uint32_t msg = IXGBE_VT_MSGTYPE_NACK;
616         struct ixgbe_hw *hw =
617                 IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
618         struct ixgbe_vf_info *vfinfo =
619                 *IXGBE_DEV_PRIVATE_TO_P_VFDATA(dev->data->dev_private);
620
621         if (!vfinfo[vf].clear_to_send)
622                 ixgbe_write_mbx(hw, &msg, 1, vf);
623 }
624
625 void ixgbe_pf_mbx_process(struct rte_eth_dev *eth_dev)
626 {
627         uint16_t vf;
628         struct ixgbe_hw *hw =
629                 IXGBE_DEV_PRIVATE_TO_HW(eth_dev->data->dev_private);
630
631         for (vf = 0; vf < dev_num_vf(eth_dev); vf++) {
632                 /* check & process vf function level reset */
633                 if (!ixgbe_check_for_rst(hw, vf))
634                         ixgbe_vf_reset_event(eth_dev, vf);
635
636                 /* check & process vf mailbox messages */
637                 if (!ixgbe_check_for_msg(hw, vf))
638                         ixgbe_rcv_msg_from_vf(eth_dev, vf);
639
640                 /* check & process acks from vf */
641                 if (!ixgbe_check_for_ack(hw, vf))
642                         ixgbe_rcv_ack_from_vf(eth_dev, vf);
643         }
644 }