09440ccb4c0e0dc4cf98341cdb40d3dbcc55eb68
[deb_dpdk.git] / drivers / net / ixgbe / ixgbe_pf.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2016 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 "base/ixgbe_common.h"
53 #include "ixgbe_ethdev.h"
54 #include "rte_pmd_ixgbe.h"
55
56 #define IXGBE_MAX_VFTA     (128)
57 #define IXGBE_VF_MSG_SIZE_DEFAULT 1
58 #define IXGBE_VF_GET_QUEUE_MSG_SIZE 5
59 #define IXGBE_ETHERTYPE_FLOW_CTRL 0x8808
60
61 static inline uint16_t
62 dev_num_vf(struct rte_eth_dev *eth_dev)
63 {
64         return eth_dev->pci_dev->max_vfs;
65 }
66
67 static inline
68 int ixgbe_vf_perm_addr_gen(struct rte_eth_dev *dev, uint16_t vf_num)
69 {
70         unsigned char vf_mac_addr[ETHER_ADDR_LEN];
71         struct ixgbe_vf_info *vfinfo =
72                 *IXGBE_DEV_PRIVATE_TO_P_VFDATA(dev->data->dev_private);
73         uint16_t vfn;
74
75         for (vfn = 0; vfn < vf_num; vfn++) {
76                 eth_random_addr(vf_mac_addr);
77                 /* keep the random address as default */
78                 memcpy(vfinfo[vfn].vf_mac_addresses, vf_mac_addr,
79                            ETHER_ADDR_LEN);
80         }
81
82         return 0;
83 }
84
85 static inline int
86 ixgbe_mb_intr_setup(struct rte_eth_dev *dev)
87 {
88         struct ixgbe_interrupt *intr =
89                 IXGBE_DEV_PRIVATE_TO_INTR(dev->data->dev_private);
90
91         intr->mask |= IXGBE_EICR_MAILBOX;
92
93         return 0;
94 }
95
96 void ixgbe_pf_host_init(struct rte_eth_dev *eth_dev)
97 {
98         struct ixgbe_vf_info **vfinfo =
99                 IXGBE_DEV_PRIVATE_TO_P_VFDATA(eth_dev->data->dev_private);
100         struct ixgbe_mirror_info *mirror_info =
101         IXGBE_DEV_PRIVATE_TO_PFDATA(eth_dev->data->dev_private);
102         struct ixgbe_uta_info *uta_info =
103         IXGBE_DEV_PRIVATE_TO_UTA(eth_dev->data->dev_private);
104         struct ixgbe_hw *hw =
105                 IXGBE_DEV_PRIVATE_TO_HW(eth_dev->data->dev_private);
106         uint16_t vf_num;
107         uint8_t nb_queue;
108
109         PMD_INIT_FUNC_TRACE();
110
111         RTE_ETH_DEV_SRIOV(eth_dev).active = 0;
112         vf_num = dev_num_vf(eth_dev);
113         if (vf_num == 0)
114                 return;
115
116         *vfinfo = rte_zmalloc("vf_info", sizeof(struct ixgbe_vf_info) * vf_num, 0);
117         if (*vfinfo == NULL)
118                 rte_panic("Cannot allocate memory for private VF data\n");
119
120         memset(mirror_info, 0, sizeof(struct ixgbe_mirror_info));
121         memset(uta_info, 0, sizeof(struct ixgbe_uta_info));
122         hw->mac.mc_filter_type = 0;
123
124         if (vf_num >= ETH_32_POOLS) {
125                 nb_queue = 2;
126                 RTE_ETH_DEV_SRIOV(eth_dev).active = ETH_64_POOLS;
127         } else if (vf_num >= ETH_16_POOLS) {
128                 nb_queue = 4;
129                 RTE_ETH_DEV_SRIOV(eth_dev).active = ETH_32_POOLS;
130         } else {
131                 nb_queue = 8;
132                 RTE_ETH_DEV_SRIOV(eth_dev).active = ETH_16_POOLS;
133         }
134
135         RTE_ETH_DEV_SRIOV(eth_dev).nb_q_per_pool = nb_queue;
136         RTE_ETH_DEV_SRIOV(eth_dev).def_vmdq_idx = vf_num;
137         RTE_ETH_DEV_SRIOV(eth_dev).def_pool_q_idx = (uint16_t)(vf_num * nb_queue);
138
139         ixgbe_vf_perm_addr_gen(eth_dev, vf_num);
140
141         /* init_mailbox_params */
142         hw->mbx.ops.init_params(hw);
143
144         /* set mb interrupt mask */
145         ixgbe_mb_intr_setup(eth_dev);
146 }
147
148 void ixgbe_pf_host_uninit(struct rte_eth_dev *eth_dev)
149 {
150         struct ixgbe_vf_info **vfinfo;
151         uint16_t vf_num;
152
153         PMD_INIT_FUNC_TRACE();
154
155         vfinfo = IXGBE_DEV_PRIVATE_TO_P_VFDATA(eth_dev->data->dev_private);
156
157         RTE_ETH_DEV_SRIOV(eth_dev).active = 0;
158         RTE_ETH_DEV_SRIOV(eth_dev).nb_q_per_pool = 0;
159         RTE_ETH_DEV_SRIOV(eth_dev).def_vmdq_idx = 0;
160         RTE_ETH_DEV_SRIOV(eth_dev).def_pool_q_idx = 0;
161
162         vf_num = dev_num_vf(eth_dev);
163         if (vf_num == 0)
164                 return;
165
166         rte_free(*vfinfo);
167         *vfinfo = NULL;
168 }
169
170 static void
171 ixgbe_add_tx_flow_control_drop_filter(struct rte_eth_dev *eth_dev)
172 {
173         struct ixgbe_hw *hw =
174                 IXGBE_DEV_PRIVATE_TO_HW(eth_dev->data->dev_private);
175         struct ixgbe_filter_info *filter_info =
176                 IXGBE_DEV_PRIVATE_TO_FILTER_INFO(eth_dev->data->dev_private);
177         uint16_t vf_num;
178         int i;
179
180         if (!hw->mac.ops.set_ethertype_anti_spoofing) {
181                 RTE_LOG(INFO, PMD, "ether type anti-spoofing is not"
182                         " supported.\n");
183                 return;
184         }
185
186         /* occupy an entity of ether type filter */
187         for (i = 0; i < IXGBE_MAX_ETQF_FILTERS; i++) {
188                 if (!(filter_info->ethertype_mask & (1 << i))) {
189                         filter_info->ethertype_mask |= 1 << i;
190                         filter_info->ethertype_filters[i] =
191                                 IXGBE_ETHERTYPE_FLOW_CTRL;
192                         break;
193                 }
194         }
195         if (i == IXGBE_MAX_ETQF_FILTERS) {
196                 RTE_LOG(ERR, PMD, "Cannot find an unused ether type filter"
197                         " entity for flow control.\n");
198                 return;
199         }
200
201         IXGBE_WRITE_REG(hw, IXGBE_ETQF(i),
202                         (IXGBE_ETQF_FILTER_EN |
203                         IXGBE_ETQF_TX_ANTISPOOF |
204                         IXGBE_ETHERTYPE_FLOW_CTRL));
205
206         vf_num = dev_num_vf(eth_dev);
207         for (i = 0; i < vf_num; i++)
208                 hw->mac.ops.set_ethertype_anti_spoofing(hw, true, i);
209 }
210
211 int ixgbe_pf_host_configure(struct rte_eth_dev *eth_dev)
212 {
213         uint32_t vtctl, fcrth;
214         uint32_t vfre_slot, vfre_offset;
215         uint16_t vf_num;
216         const uint8_t VFRE_SHIFT = 5;  /* VFRE 32 bits per slot */
217         const uint8_t VFRE_MASK = (uint8_t)((1U << VFRE_SHIFT) - 1);
218         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(eth_dev->data->dev_private);
219         uint32_t gpie, gcr_ext;
220         uint32_t vlanctrl;
221         int i;
222
223         vf_num = dev_num_vf(eth_dev);
224         if (vf_num == 0)
225                 return -1;
226
227         /* enable VMDq and set the default pool for PF */
228         vtctl = IXGBE_READ_REG(hw, IXGBE_VT_CTL);
229         vtctl |= IXGBE_VMD_CTL_VMDQ_EN;
230         vtctl &= ~IXGBE_VT_CTL_POOL_MASK;
231         vtctl |= RTE_ETH_DEV_SRIOV(eth_dev).def_vmdq_idx
232                 << IXGBE_VT_CTL_POOL_SHIFT;
233         vtctl |= IXGBE_VT_CTL_REPLEN;
234         IXGBE_WRITE_REG(hw, IXGBE_VT_CTL, vtctl);
235
236         vfre_offset = vf_num & VFRE_MASK;
237         vfre_slot = (vf_num >> VFRE_SHIFT) > 0 ? 1 : 0;
238
239         /* Enable pools reserved to PF only */
240         IXGBE_WRITE_REG(hw, IXGBE_VFRE(vfre_slot), (~0U) << vfre_offset);
241         IXGBE_WRITE_REG(hw, IXGBE_VFRE(vfre_slot ^ 1), vfre_slot - 1);
242         IXGBE_WRITE_REG(hw, IXGBE_VFTE(vfre_slot), (~0U) << vfre_offset);
243         IXGBE_WRITE_REG(hw, IXGBE_VFTE(vfre_slot ^ 1), vfre_slot - 1);
244
245         /* PFDMA Tx General Switch Control Enables VMDQ loopback */
246         IXGBE_WRITE_REG(hw, IXGBE_PFDTXGSWC, IXGBE_PFDTXGSWC_VT_LBEN);
247
248         /* clear VMDq map to perment rar 0 */
249         hw->mac.ops.clear_vmdq(hw, 0, IXGBE_CLEAR_VMDQ_ALL);
250
251         /* clear VMDq map to scan rar 127 */
252         IXGBE_WRITE_REG(hw, IXGBE_MPSAR_LO(hw->mac.num_rar_entries), 0);
253         IXGBE_WRITE_REG(hw, IXGBE_MPSAR_HI(hw->mac.num_rar_entries), 0);
254
255         /* set VMDq map to default PF pool */
256         hw->mac.ops.set_vmdq(hw, 0, RTE_ETH_DEV_SRIOV(eth_dev).def_vmdq_idx);
257
258         /*
259          * SW msut set GCR_EXT.VT_Mode the same as GPIE.VT_Mode
260          */
261         gcr_ext = IXGBE_READ_REG(hw, IXGBE_GCR_EXT);
262         gcr_ext &= ~IXGBE_GCR_EXT_VT_MODE_MASK;
263
264         gpie = IXGBE_READ_REG(hw, IXGBE_GPIE);
265         gpie &= ~IXGBE_GPIE_VTMODE_MASK;
266         gpie |= IXGBE_GPIE_MSIX_MODE;
267
268         switch (RTE_ETH_DEV_SRIOV(eth_dev).active) {
269         case ETH_64_POOLS:
270                 gcr_ext |= IXGBE_GCR_EXT_VT_MODE_64;
271                 gpie |= IXGBE_GPIE_VTMODE_64;
272                 break;
273         case ETH_32_POOLS:
274                 gcr_ext |= IXGBE_GCR_EXT_VT_MODE_32;
275                 gpie |= IXGBE_GPIE_VTMODE_32;
276                 break;
277         case ETH_16_POOLS:
278                 gcr_ext |= IXGBE_GCR_EXT_VT_MODE_16;
279                 gpie |= IXGBE_GPIE_VTMODE_16;
280                 break;
281         }
282
283         IXGBE_WRITE_REG(hw, IXGBE_GCR_EXT, gcr_ext);
284         IXGBE_WRITE_REG(hw, IXGBE_GPIE, gpie);
285
286         /*
287          * enable vlan filtering and allow all vlan tags through
288          */
289         vlanctrl = IXGBE_READ_REG(hw, IXGBE_VLNCTRL);
290         vlanctrl |= IXGBE_VLNCTRL_VFE; /* enable vlan filters */
291         IXGBE_WRITE_REG(hw, IXGBE_VLNCTRL, vlanctrl);
292
293         /* VFTA - enable all vlan filters */
294         for (i = 0; i < IXGBE_MAX_VFTA; i++)
295                 IXGBE_WRITE_REG(hw, IXGBE_VFTA(i), 0xFFFFFFFF);
296
297         /* Enable MAC Anti-Spoofing */
298         hw->mac.ops.set_mac_anti_spoofing(hw, FALSE, vf_num);
299
300         /* set flow control threshold to max to avoid tx switch hang */
301         for (i = 0; i < IXGBE_DCB_MAX_TRAFFIC_CLASS; i++) {
302                 IXGBE_WRITE_REG(hw, IXGBE_FCRTL_82599(i), 0);
303                 fcrth = IXGBE_READ_REG(hw, IXGBE_RXPBSIZE(i)) - 32;
304                 IXGBE_WRITE_REG(hw, IXGBE_FCRTH_82599(i), fcrth);
305         }
306
307         ixgbe_add_tx_flow_control_drop_filter(eth_dev);
308
309         return 0;
310 }
311
312 static void
313 set_rx_mode(struct rte_eth_dev *dev)
314 {
315         struct rte_eth_dev_data *dev_data = dev->data;
316         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
317         u32 fctrl, vmolr = IXGBE_VMOLR_BAM | IXGBE_VMOLR_AUPE;
318         uint16_t vfn = dev_num_vf(dev);
319
320         /* Check for Promiscuous and All Multicast modes */
321         fctrl = IXGBE_READ_REG(hw, IXGBE_FCTRL);
322
323         /* set all bits that we expect to always be set */
324         fctrl &= ~IXGBE_FCTRL_SBP; /* disable store-bad-packets */
325         fctrl |= IXGBE_FCTRL_BAM;
326
327         /* clear the bits we are changing the status of */
328         fctrl &= ~(IXGBE_FCTRL_UPE | IXGBE_FCTRL_MPE);
329
330         if (dev_data->promiscuous) {
331                 fctrl |= (IXGBE_FCTRL_UPE | IXGBE_FCTRL_MPE);
332                 vmolr |= (IXGBE_VMOLR_ROPE | IXGBE_VMOLR_MPE);
333         } else {
334                 if (dev_data->all_multicast) {
335                         fctrl |= IXGBE_FCTRL_MPE;
336                         vmolr |= IXGBE_VMOLR_MPE;
337                 } else {
338                         vmolr |= IXGBE_VMOLR_ROMPE;
339                 }
340         }
341
342         if (hw->mac.type != ixgbe_mac_82598EB) {
343                 vmolr |= IXGBE_READ_REG(hw, IXGBE_VMOLR(vfn)) &
344                          ~(IXGBE_VMOLR_MPE | IXGBE_VMOLR_ROMPE |
345                            IXGBE_VMOLR_ROPE);
346                 IXGBE_WRITE_REG(hw, IXGBE_VMOLR(vfn), vmolr);
347         }
348
349         IXGBE_WRITE_REG(hw, IXGBE_FCTRL, fctrl);
350
351         if (dev->data->dev_conf.rxmode.hw_vlan_strip)
352                 ixgbe_vlan_hw_strip_enable_all(dev);
353         else
354                 ixgbe_vlan_hw_strip_disable_all(dev);
355 }
356
357 static inline void
358 ixgbe_vf_reset_event(struct rte_eth_dev *dev, uint16_t vf)
359 {
360         struct ixgbe_hw *hw =
361                 IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
362         struct ixgbe_vf_info *vfinfo =
363                 *(IXGBE_DEV_PRIVATE_TO_P_VFDATA(dev->data->dev_private));
364         int rar_entry = hw->mac.num_rar_entries - (vf + 1);
365         uint32_t vmolr = IXGBE_READ_REG(hw, IXGBE_VMOLR(vf));
366
367         vmolr |= (IXGBE_VMOLR_ROPE | IXGBE_VMOLR_ROMPE |
368                         IXGBE_VMOLR_BAM | IXGBE_VMOLR_AUPE);
369         IXGBE_WRITE_REG(hw, IXGBE_VMOLR(vf), vmolr);
370
371         IXGBE_WRITE_REG(hw, IXGBE_VMVIR(vf), 0);
372
373         /* reset multicast table array for vf */
374         vfinfo[vf].num_vf_mc_hashes = 0;
375
376         /* reset rx mode */
377         set_rx_mode(dev);
378
379         hw->mac.ops.clear_rar(hw, rar_entry);
380 }
381
382 static inline void
383 ixgbe_vf_reset_msg(struct rte_eth_dev *dev, uint16_t vf)
384 {
385         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
386         uint32_t reg;
387         uint32_t reg_offset, vf_shift;
388         const uint8_t VFRE_SHIFT = 5;  /* VFRE 32 bits per slot */
389         const uint8_t VFRE_MASK = (uint8_t)((1U << VFRE_SHIFT) - 1);
390         uint8_t  nb_q_per_pool;
391         int i;
392
393         vf_shift = vf & VFRE_MASK;
394         reg_offset = (vf >> VFRE_SHIFT) > 0 ? 1 : 0;
395
396         /* enable transmit for vf */
397         reg = IXGBE_READ_REG(hw, IXGBE_VFTE(reg_offset));
398         reg |= (reg | (1 << vf_shift));
399         IXGBE_WRITE_REG(hw, IXGBE_VFTE(reg_offset), reg);
400
401         /* enable all queue drop for IOV */
402         nb_q_per_pool = RTE_ETH_DEV_SRIOV(dev).nb_q_per_pool;
403         for (i = vf * nb_q_per_pool; i < (vf + 1) * nb_q_per_pool; i++) {
404                 IXGBE_WRITE_FLUSH(hw);
405                 reg = IXGBE_QDE_ENABLE | IXGBE_QDE_WRITE;
406                 reg |= i << IXGBE_QDE_IDX_SHIFT;
407                 IXGBE_WRITE_REG(hw, IXGBE_QDE, reg);
408         }
409
410         /* enable receive for vf */
411         reg = IXGBE_READ_REG(hw, IXGBE_VFRE(reg_offset));
412         reg |= (reg | (1 << vf_shift));
413         IXGBE_WRITE_REG(hw, IXGBE_VFRE(reg_offset), reg);
414
415         /* Enable counting of spoofed packets in the SSVPC register */
416         reg = IXGBE_READ_REG(hw, IXGBE_VMECM(reg_offset));
417         reg |= (1 << vf_shift);
418         IXGBE_WRITE_REG(hw, IXGBE_VMECM(reg_offset), reg);
419
420         ixgbe_vf_reset_event(dev, vf);
421 }
422
423 static int
424 ixgbe_enable_vf_mc_promisc(struct rte_eth_dev *dev, uint32_t vf)
425 {
426         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
427         uint32_t vmolr;
428
429         vmolr = IXGBE_READ_REG(hw, IXGBE_VMOLR(vf));
430
431         RTE_LOG(INFO, PMD, "VF %u: enabling multicast promiscuous\n", vf);
432
433         vmolr |= IXGBE_VMOLR_MPE;
434
435         IXGBE_WRITE_REG(hw, IXGBE_VMOLR(vf), vmolr);
436
437         return 0;
438 }
439
440 static int
441 ixgbe_disable_vf_mc_promisc(struct rte_eth_dev *dev, uint32_t vf)
442 {
443         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
444         uint32_t vmolr;
445
446         vmolr = IXGBE_READ_REG(hw, IXGBE_VMOLR(vf));
447
448         RTE_LOG(INFO, PMD, "VF %u: disabling multicast promiscuous\n", vf);
449
450         vmolr &= ~IXGBE_VMOLR_MPE;
451
452         IXGBE_WRITE_REG(hw, IXGBE_VMOLR(vf), vmolr);
453
454         return 0;
455 }
456
457 static int
458 ixgbe_vf_reset(struct rte_eth_dev *dev, uint16_t vf, uint32_t *msgbuf)
459 {
460         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
461         struct ixgbe_vf_info *vfinfo =
462                 *(IXGBE_DEV_PRIVATE_TO_P_VFDATA(dev->data->dev_private));
463         unsigned char *vf_mac = vfinfo[vf].vf_mac_addresses;
464         int rar_entry = hw->mac.num_rar_entries - (vf + 1);
465         uint8_t *new_mac = (uint8_t *)(&msgbuf[1]);
466
467         ixgbe_vf_reset_msg(dev, vf);
468
469         hw->mac.ops.set_rar(hw, rar_entry, vf_mac, vf, IXGBE_RAH_AV);
470
471         /* Disable multicast promiscuous at reset */
472         ixgbe_disable_vf_mc_promisc(dev, vf);
473
474         /* reply to reset with ack and vf mac address */
475         msgbuf[0] = IXGBE_VF_RESET | IXGBE_VT_MSGTYPE_ACK;
476         rte_memcpy(new_mac, vf_mac, ETHER_ADDR_LEN);
477         /*
478          * Piggyback the multicast filter type so VF can compute the
479          * correct vectors
480          */
481         msgbuf[3] = hw->mac.mc_filter_type;
482         ixgbe_write_mbx(hw, msgbuf, IXGBE_VF_PERMADDR_MSG_LEN, vf);
483
484         return 0;
485 }
486
487 static int
488 ixgbe_vf_set_mac_addr(struct rte_eth_dev *dev, uint32_t vf, uint32_t *msgbuf)
489 {
490         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
491         struct ixgbe_vf_info *vfinfo =
492                 *(IXGBE_DEV_PRIVATE_TO_P_VFDATA(dev->data->dev_private));
493         int rar_entry = hw->mac.num_rar_entries - (vf + 1);
494         uint8_t *new_mac = (uint8_t *)(&msgbuf[1]);
495
496         if (is_valid_assigned_ether_addr((struct ether_addr *)new_mac)) {
497                 rte_memcpy(vfinfo[vf].vf_mac_addresses, new_mac, 6);
498                 return hw->mac.ops.set_rar(hw, rar_entry, new_mac, vf, IXGBE_RAH_AV);
499         }
500         return -1;
501 }
502
503 static int
504 ixgbe_vf_set_multicast(struct rte_eth_dev *dev, __rte_unused uint32_t vf, uint32_t *msgbuf)
505 {
506         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
507         struct ixgbe_vf_info *vfinfo =
508                 *(IXGBE_DEV_PRIVATE_TO_P_VFDATA(dev->data->dev_private));
509         int nb_entries = (msgbuf[0] & IXGBE_VT_MSGINFO_MASK) >>
510                 IXGBE_VT_MSGINFO_SHIFT;
511         uint16_t *hash_list = (uint16_t *)&msgbuf[1];
512         uint32_t mta_idx;
513         uint32_t mta_shift;
514         const uint32_t IXGBE_MTA_INDEX_MASK = 0x7F;
515         const uint32_t IXGBE_MTA_BIT_SHIFT = 5;
516         const uint32_t IXGBE_MTA_BIT_MASK = (0x1 << IXGBE_MTA_BIT_SHIFT) - 1;
517         uint32_t reg_val;
518         int i;
519
520         /* Disable multicast promiscuous first */
521         ixgbe_disable_vf_mc_promisc(dev, vf);
522
523         /* only so many hash values supported */
524         nb_entries = RTE_MIN(nb_entries, IXGBE_MAX_VF_MC_ENTRIES);
525
526         /* store the mc entries  */
527         vfinfo->num_vf_mc_hashes = (uint16_t)nb_entries;
528         for (i = 0; i < nb_entries; i++) {
529                 vfinfo->vf_mc_hashes[i] = hash_list[i];
530         }
531
532         for (i = 0; i < vfinfo->num_vf_mc_hashes; i++) {
533                 mta_idx = (vfinfo->vf_mc_hashes[i] >> IXGBE_MTA_BIT_SHIFT)
534                                 & IXGBE_MTA_INDEX_MASK;
535                 mta_shift = vfinfo->vf_mc_hashes[i] & IXGBE_MTA_BIT_MASK;
536                 reg_val = IXGBE_READ_REG(hw, IXGBE_MTA(mta_idx));
537                 reg_val |= (1 << mta_shift);
538                 IXGBE_WRITE_REG(hw, IXGBE_MTA(mta_idx), reg_val);
539         }
540
541         return 0;
542 }
543
544 static int
545 ixgbe_vf_set_vlan(struct rte_eth_dev *dev, uint32_t vf, uint32_t *msgbuf)
546 {
547         int add, vid;
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         add = (msgbuf[0] & IXGBE_VT_MSGINFO_MASK)
553                 >> IXGBE_VT_MSGINFO_SHIFT;
554         vid = (msgbuf[1] & IXGBE_VLVF_VLANID_MASK);
555
556         if (add)
557                 vfinfo[vf].vlan_count++;
558         else if (vfinfo[vf].vlan_count)
559                 vfinfo[vf].vlan_count--;
560         return hw->mac.ops.set_vfta(hw, vid, vf, (bool)add, false);
561 }
562
563 static int
564 ixgbe_set_vf_lpe(struct rte_eth_dev *dev, __rte_unused uint32_t vf, uint32_t *msgbuf)
565 {
566         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
567         uint32_t new_mtu = msgbuf[1];
568         uint32_t max_frs;
569         int max_frame = new_mtu + ETHER_HDR_LEN + ETHER_CRC_LEN;
570
571         /* X540 and X550 support jumbo frames in IOV mode */
572         if (hw->mac.type != ixgbe_mac_X540 &&
573                 hw->mac.type != ixgbe_mac_X550 &&
574                 hw->mac.type != ixgbe_mac_X550EM_x &&
575                 hw->mac.type != ixgbe_mac_X550EM_a)
576                 return -1;
577
578         if ((max_frame < ETHER_MIN_LEN) || (max_frame > ETHER_MAX_JUMBO_FRAME_LEN))
579                 return -1;
580
581         max_frs = (IXGBE_READ_REG(hw, IXGBE_MAXFRS) &
582                    IXGBE_MHADD_MFS_MASK) >> IXGBE_MHADD_MFS_SHIFT;
583         if (max_frs < new_mtu) {
584                 max_frs = new_mtu << IXGBE_MHADD_MFS_SHIFT;
585                 IXGBE_WRITE_REG(hw, IXGBE_MAXFRS, max_frs);
586         }
587
588         return 0;
589 }
590
591 static int
592 ixgbe_negotiate_vf_api(struct rte_eth_dev *dev, uint32_t vf, uint32_t *msgbuf)
593 {
594         uint32_t api_version = msgbuf[1];
595         struct ixgbe_vf_info *vfinfo =
596                 *IXGBE_DEV_PRIVATE_TO_P_VFDATA(dev->data->dev_private);
597
598         switch (api_version) {
599         case ixgbe_mbox_api_10:
600         case ixgbe_mbox_api_11:
601         case ixgbe_mbox_api_12:
602                 vfinfo[vf].api_version = (uint8_t)api_version;
603                 return 0;
604         default:
605                 break;
606         }
607
608         RTE_LOG(ERR, PMD, "Negotiate invalid api version %u from VF %d\n",
609                 api_version, vf);
610
611         return -1;
612 }
613
614 static int
615 ixgbe_get_vf_queues(struct rte_eth_dev *dev, uint32_t vf, uint32_t *msgbuf)
616 {
617         struct ixgbe_vf_info *vfinfo =
618                 *IXGBE_DEV_PRIVATE_TO_P_VFDATA(dev->data->dev_private);
619         uint32_t default_q = vf * RTE_ETH_DEV_SRIOV(dev).nb_q_per_pool;
620
621         /* Verify if the PF supports the mbox APIs version or not */
622         switch (vfinfo[vf].api_version) {
623         case ixgbe_mbox_api_20:
624         case ixgbe_mbox_api_11:
625         case ixgbe_mbox_api_12:
626                 break;
627         default:
628                 return -1;
629         }
630
631         /* Notify VF of Rx and Tx queue number */
632         msgbuf[IXGBE_VF_RX_QUEUES] = RTE_ETH_DEV_SRIOV(dev).nb_q_per_pool;
633         msgbuf[IXGBE_VF_TX_QUEUES] = RTE_ETH_DEV_SRIOV(dev).nb_q_per_pool;
634
635         /* Notify VF of default queue */
636         msgbuf[IXGBE_VF_DEF_QUEUE] = default_q;
637
638         /*
639          * FIX ME if it needs fill msgbuf[IXGBE_VF_TRANS_VLAN]
640          * for VLAN strip or VMDQ_DCB or VMDQ_DCB_RSS
641          */
642
643         return 0;
644 }
645
646 static int
647 ixgbe_set_vf_mc_promisc(struct rte_eth_dev *dev, uint32_t vf, uint32_t *msgbuf)
648 {
649         struct ixgbe_vf_info *vfinfo =
650                 *(IXGBE_DEV_PRIVATE_TO_P_VFDATA(dev->data->dev_private));
651         bool enable = !!msgbuf[1];      /* msgbuf contains the flag to enable */
652
653         switch (vfinfo[vf].api_version) {
654         case ixgbe_mbox_api_12:
655                 break;
656         default:
657                 return -1;
658         }
659
660         if (enable)
661                 return ixgbe_enable_vf_mc_promisc(dev, vf);
662         else
663                 return ixgbe_disable_vf_mc_promisc(dev, vf);
664 }
665
666 static int
667 ixgbe_rcv_msg_from_vf(struct rte_eth_dev *dev, uint16_t vf)
668 {
669         uint16_t mbx_size = IXGBE_VFMAILBOX_SIZE;
670         uint16_t msg_size = IXGBE_VF_MSG_SIZE_DEFAULT;
671         uint32_t msgbuf[IXGBE_VFMAILBOX_SIZE];
672         int32_t retval;
673         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
674         struct ixgbe_vf_info *vfinfo =
675                 *IXGBE_DEV_PRIVATE_TO_P_VFDATA(dev->data->dev_private);
676         struct rte_pmd_ixgbe_mb_event_param cb_param;
677
678         retval = ixgbe_read_mbx(hw, msgbuf, mbx_size, vf);
679         if (retval) {
680                 PMD_DRV_LOG(ERR, "Error mbx recv msg from VF %d", vf);
681                 return retval;
682         }
683
684         /* do nothing with the message already been processed */
685         if (msgbuf[0] & (IXGBE_VT_MSGTYPE_ACK | IXGBE_VT_MSGTYPE_NACK))
686                 return retval;
687
688         /* flush the ack before we write any messages back */
689         IXGBE_WRITE_FLUSH(hw);
690
691         /**
692          * initialise structure to send to user application
693          * will return response from user in retval field
694          */
695         cb_param.retval = RTE_PMD_IXGBE_MB_EVENT_PROCEED;
696         cb_param.vfid = vf;
697         cb_param.msg_type = msgbuf[0] & 0xFFFF;
698         cb_param.msg = (void *)msgbuf;
699
700         /* perform VF reset */
701         if (msgbuf[0] == IXGBE_VF_RESET) {
702                 int ret = ixgbe_vf_reset(dev, vf, msgbuf);
703
704                 vfinfo[vf].clear_to_send = true;
705
706                 /* notify application about VF reset */
707                 _rte_eth_dev_callback_process(dev, RTE_ETH_EVENT_VF_MBOX, &cb_param);
708                 return ret;
709         }
710
711         /**
712          * ask user application if we allowed to perform those functions
713          * if we get cb_param.retval == RTE_PMD_IXGBE_MB_EVENT_PROCEED
714          * then business as usual,
715          * if 0, do nothing and send ACK to VF
716          * if cb_param.retval > 1, do nothing and send NAK to VF
717          */
718         _rte_eth_dev_callback_process(dev, RTE_ETH_EVENT_VF_MBOX, &cb_param);
719
720         retval = cb_param.retval;
721
722         /* check & process VF to PF mailbox message */
723         switch ((msgbuf[0] & 0xFFFF)) {
724         case IXGBE_VF_SET_MAC_ADDR:
725                 if (retval == RTE_PMD_IXGBE_MB_EVENT_PROCEED)
726                         retval = ixgbe_vf_set_mac_addr(dev, vf, msgbuf);
727                 break;
728         case IXGBE_VF_SET_MULTICAST:
729                 if (retval == RTE_PMD_IXGBE_MB_EVENT_PROCEED)
730                         retval = ixgbe_vf_set_multicast(dev, vf, msgbuf);
731                 break;
732         case IXGBE_VF_SET_LPE:
733                 if (retval == RTE_PMD_IXGBE_MB_EVENT_PROCEED)
734                         retval = ixgbe_set_vf_lpe(dev, vf, msgbuf);
735                 break;
736         case IXGBE_VF_SET_VLAN:
737                 if (retval == RTE_PMD_IXGBE_MB_EVENT_PROCEED)
738                         retval = ixgbe_vf_set_vlan(dev, vf, msgbuf);
739                 break;
740         case IXGBE_VF_API_NEGOTIATE:
741                 retval = ixgbe_negotiate_vf_api(dev, vf, msgbuf);
742                 break;
743         case IXGBE_VF_GET_QUEUES:
744                 retval = ixgbe_get_vf_queues(dev, vf, msgbuf);
745                 msg_size = IXGBE_VF_GET_QUEUE_MSG_SIZE;
746                 break;
747         case IXGBE_VF_UPDATE_XCAST_MODE:
748                 if (retval == RTE_PMD_IXGBE_MB_EVENT_PROCEED)
749                         retval = ixgbe_set_vf_mc_promisc(dev, vf, msgbuf);
750                 break;
751         default:
752                 PMD_DRV_LOG(DEBUG, "Unhandled Msg %8.8x", (unsigned)msgbuf[0]);
753                 retval = IXGBE_ERR_MBX;
754                 break;
755         }
756
757         /* response the VF according to the message process result */
758         if (retval)
759                 msgbuf[0] |= IXGBE_VT_MSGTYPE_NACK;
760         else
761                 msgbuf[0] |= IXGBE_VT_MSGTYPE_ACK;
762
763         msgbuf[0] |= IXGBE_VT_MSGTYPE_CTS;
764
765         ixgbe_write_mbx(hw, msgbuf, msg_size, vf);
766
767         return retval;
768 }
769
770 static inline void
771 ixgbe_rcv_ack_from_vf(struct rte_eth_dev *dev, uint16_t vf)
772 {
773         uint32_t msg = IXGBE_VT_MSGTYPE_NACK;
774         struct ixgbe_hw *hw =
775                 IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
776         struct ixgbe_vf_info *vfinfo =
777                 *IXGBE_DEV_PRIVATE_TO_P_VFDATA(dev->data->dev_private);
778
779         if (!vfinfo[vf].clear_to_send)
780                 ixgbe_write_mbx(hw, &msg, 1, vf);
781 }
782
783 void ixgbe_pf_mbx_process(struct rte_eth_dev *eth_dev)
784 {
785         uint16_t vf;
786         struct ixgbe_hw *hw =
787                 IXGBE_DEV_PRIVATE_TO_HW(eth_dev->data->dev_private);
788
789         for (vf = 0; vf < dev_num_vf(eth_dev); vf++) {
790                 /* check & process vf function level reset */
791                 if (!ixgbe_check_for_rst(hw, vf))
792                         ixgbe_vf_reset_event(eth_dev, vf);
793
794                 /* check & process vf mailbox messages */
795                 if (!ixgbe_check_for_msg(hw, vf))
796                         ixgbe_rcv_msg_from_vf(eth_dev, vf);
797
798                 /* check & process acks from vf */
799                 if (!ixgbe_check_for_ack(hw, vf))
800                         ixgbe_rcv_ack_from_vf(eth_dev, vf);
801         }
802 }