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