ethdev: make driver-only headers private
[dpdk.git] / drivers / net / ixgbe / ixgbe_pf.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2016 Intel Corporation
3  */
4
5 #include <stdio.h>
6 #include <errno.h>
7 #include <stdint.h>
8 #include <stdlib.h>
9 #include <unistd.h>
10 #include <stdarg.h>
11 #include <inttypes.h>
12
13 #include <rte_interrupts.h>
14 #include <rte_log.h>
15 #include <rte_debug.h>
16 #include <rte_eal.h>
17 #include <rte_ether.h>
18 #include <ethdev_driver.h>
19 #include <rte_memcpy.h>
20 #include <rte_malloc.h>
21 #include <rte_random.h>
22
23 #include "base/ixgbe_common.h"
24 #include "ixgbe_ethdev.h"
25 #include "rte_pmd_ixgbe.h"
26
27 #define IXGBE_MAX_VFTA     (128)
28 #define IXGBE_VF_MSG_SIZE_DEFAULT 1
29 #define IXGBE_VF_GET_QUEUE_MSG_SIZE 5
30 #define IXGBE_ETHERTYPE_FLOW_CTRL 0x8808
31
32 static inline uint16_t
33 dev_num_vf(struct rte_eth_dev *eth_dev)
34 {
35         struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(eth_dev);
36
37         return pci_dev->max_vfs;
38 }
39
40 static inline
41 int ixgbe_vf_perm_addr_gen(struct rte_eth_dev *dev, uint16_t vf_num)
42 {
43         unsigned char vf_mac_addr[RTE_ETHER_ADDR_LEN];
44         struct ixgbe_vf_info *vfinfo =
45                 *IXGBE_DEV_PRIVATE_TO_P_VFDATA(dev->data->dev_private);
46         uint16_t vfn;
47
48         for (vfn = 0; vfn < vf_num; vfn++) {
49                 rte_eth_random_addr(vf_mac_addr);
50                 /* keep the random address as default */
51                 memcpy(vfinfo[vfn].vf_mac_addresses, vf_mac_addr,
52                            RTE_ETHER_ADDR_LEN);
53         }
54
55         return 0;
56 }
57
58 static inline int
59 ixgbe_mb_intr_setup(struct rte_eth_dev *dev)
60 {
61         struct ixgbe_interrupt *intr =
62                 IXGBE_DEV_PRIVATE_TO_INTR(dev->data->dev_private);
63
64         intr->mask |= IXGBE_EICR_MAILBOX;
65
66         return 0;
67 }
68
69 int ixgbe_pf_host_init(struct rte_eth_dev *eth_dev)
70 {
71         struct ixgbe_vf_info **vfinfo =
72                 IXGBE_DEV_PRIVATE_TO_P_VFDATA(eth_dev->data->dev_private);
73         struct ixgbe_mirror_info *mirror_info =
74         IXGBE_DEV_PRIVATE_TO_PFDATA(eth_dev->data->dev_private);
75         struct ixgbe_uta_info *uta_info =
76         IXGBE_DEV_PRIVATE_TO_UTA(eth_dev->data->dev_private);
77         struct ixgbe_hw *hw =
78                 IXGBE_DEV_PRIVATE_TO_HW(eth_dev->data->dev_private);
79         uint16_t vf_num;
80         uint8_t nb_queue;
81         int ret = 0;
82
83         PMD_INIT_FUNC_TRACE();
84
85         RTE_ETH_DEV_SRIOV(eth_dev).active = 0;
86         vf_num = dev_num_vf(eth_dev);
87         if (vf_num == 0)
88                 return ret;
89
90         *vfinfo = rte_zmalloc("vf_info", sizeof(struct ixgbe_vf_info) * vf_num, 0);
91         if (*vfinfo == NULL) {
92                 PMD_INIT_LOG(ERR,
93                         "Cannot allocate memory for private VF data");
94                 return -ENOMEM;
95         }
96
97         ret = rte_eth_switch_domain_alloc(&(*vfinfo)->switch_domain_id);
98         if (ret) {
99                 PMD_INIT_LOG(ERR,
100                         "failed to allocate switch domain for device %d", ret);
101                 rte_free(*vfinfo);
102                 *vfinfo = NULL;
103                 return ret;
104         }
105
106         memset(mirror_info, 0, sizeof(struct ixgbe_mirror_info));
107         memset(uta_info, 0, sizeof(struct ixgbe_uta_info));
108         hw->mac.mc_filter_type = 0;
109
110         if (vf_num >= ETH_32_POOLS) {
111                 nb_queue = 2;
112                 RTE_ETH_DEV_SRIOV(eth_dev).active = ETH_64_POOLS;
113         } else if (vf_num >= ETH_16_POOLS) {
114                 nb_queue = 4;
115                 RTE_ETH_DEV_SRIOV(eth_dev).active = ETH_32_POOLS;
116         } else {
117                 nb_queue = 8;
118                 RTE_ETH_DEV_SRIOV(eth_dev).active = ETH_16_POOLS;
119         }
120
121         RTE_ETH_DEV_SRIOV(eth_dev).nb_q_per_pool = nb_queue;
122         RTE_ETH_DEV_SRIOV(eth_dev).def_vmdq_idx = vf_num;
123         RTE_ETH_DEV_SRIOV(eth_dev).def_pool_q_idx = (uint16_t)(vf_num * nb_queue);
124
125         ixgbe_vf_perm_addr_gen(eth_dev, vf_num);
126
127         /* init_mailbox_params */
128         hw->mbx.ops.init_params(hw);
129
130         /* set mb interrupt mask */
131         ixgbe_mb_intr_setup(eth_dev);
132
133         return ret;
134 }
135
136 void ixgbe_pf_host_uninit(struct rte_eth_dev *eth_dev)
137 {
138         struct ixgbe_vf_info **vfinfo;
139         uint16_t vf_num;
140         int ret;
141
142         PMD_INIT_FUNC_TRACE();
143
144         RTE_ETH_DEV_SRIOV(eth_dev).active = 0;
145         RTE_ETH_DEV_SRIOV(eth_dev).nb_q_per_pool = 0;
146         RTE_ETH_DEV_SRIOV(eth_dev).def_vmdq_idx = 0;
147         RTE_ETH_DEV_SRIOV(eth_dev).def_pool_q_idx = 0;
148
149         vf_num = dev_num_vf(eth_dev);
150         if (vf_num == 0)
151                 return;
152
153         vfinfo = IXGBE_DEV_PRIVATE_TO_P_VFDATA(eth_dev->data->dev_private);
154         if (*vfinfo == NULL)
155                 return;
156
157         ret = rte_eth_switch_domain_free((*vfinfo)->switch_domain_id);
158         if (ret)
159                 PMD_INIT_LOG(WARNING, "failed to free switch domain: %d", ret);
160
161         rte_free(*vfinfo);
162         *vfinfo = NULL;
163 }
164
165 static void
166 ixgbe_add_tx_flow_control_drop_filter(struct rte_eth_dev *eth_dev)
167 {
168         struct ixgbe_hw *hw =
169                 IXGBE_DEV_PRIVATE_TO_HW(eth_dev->data->dev_private);
170         struct ixgbe_filter_info *filter_info =
171                 IXGBE_DEV_PRIVATE_TO_FILTER_INFO(eth_dev->data->dev_private);
172         uint16_t vf_num;
173         int i;
174         struct ixgbe_ethertype_filter ethertype_filter;
175
176         if (!hw->mac.ops.set_ethertype_anti_spoofing) {
177                 PMD_DRV_LOG(INFO, "ether type anti-spoofing is not supported.\n");
178                 return;
179         }
180
181         i = ixgbe_ethertype_filter_lookup(filter_info,
182                                           IXGBE_ETHERTYPE_FLOW_CTRL);
183         if (i >= 0) {
184                 PMD_DRV_LOG(ERR, "A ether type filter entity for flow control already exists!\n");
185                 return;
186         }
187
188         ethertype_filter.ethertype = IXGBE_ETHERTYPE_FLOW_CTRL;
189         ethertype_filter.etqf = IXGBE_ETQF_FILTER_EN |
190                                 IXGBE_ETQF_TX_ANTISPOOF |
191                                 IXGBE_ETHERTYPE_FLOW_CTRL;
192         ethertype_filter.etqs = 0;
193         ethertype_filter.conf = TRUE;
194         i = ixgbe_ethertype_filter_insert(filter_info,
195                                           &ethertype_filter);
196         if (i < 0) {
197                 PMD_DRV_LOG(ERR, "Cannot find an unused ether type filter 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 | IXGBE_GPIE_PBA_SUPPORT;
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         ixgbe_vlan_hw_strip_config(dev);
352 }
353
354 static inline void
355 ixgbe_vf_reset_event(struct rte_eth_dev *dev, uint16_t vf)
356 {
357         struct ixgbe_hw *hw =
358                 IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
359         struct ixgbe_vf_info *vfinfo =
360                 *(IXGBE_DEV_PRIVATE_TO_P_VFDATA(dev->data->dev_private));
361         int rar_entry = hw->mac.num_rar_entries - (vf + 1);
362         uint32_t vmolr = IXGBE_READ_REG(hw, IXGBE_VMOLR(vf));
363
364         vmolr |= (IXGBE_VMOLR_ROPE |
365                         IXGBE_VMOLR_BAM | IXGBE_VMOLR_AUPE);
366         IXGBE_WRITE_REG(hw, IXGBE_VMOLR(vf), vmolr);
367
368         IXGBE_WRITE_REG(hw, IXGBE_VMVIR(vf), 0);
369
370         /* reset multicast table array for vf */
371         vfinfo[vf].num_vf_mc_hashes = 0;
372
373         /* reset rx mode */
374         set_rx_mode(dev);
375
376         hw->mac.ops.clear_rar(hw, rar_entry);
377 }
378
379 static inline void
380 ixgbe_vf_reset_msg(struct rte_eth_dev *dev, uint16_t vf)
381 {
382         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
383         uint32_t reg;
384         uint32_t reg_offset, vf_shift;
385         const uint8_t VFRE_SHIFT = 5;  /* VFRE 32 bits per slot */
386         const uint8_t VFRE_MASK = (uint8_t)((1U << VFRE_SHIFT) - 1);
387         uint8_t  nb_q_per_pool;
388         int i;
389
390         vf_shift = vf & VFRE_MASK;
391         reg_offset = (vf >> VFRE_SHIFT) > 0 ? 1 : 0;
392
393         /* enable transmit for vf */
394         reg = IXGBE_READ_REG(hw, IXGBE_VFTE(reg_offset));
395         reg |= (reg | (1 << vf_shift));
396         IXGBE_WRITE_REG(hw, IXGBE_VFTE(reg_offset), reg);
397
398         /* enable all queue drop for IOV */
399         nb_q_per_pool = RTE_ETH_DEV_SRIOV(dev).nb_q_per_pool;
400         for (i = vf * nb_q_per_pool; i < (vf + 1) * nb_q_per_pool; i++) {
401                 IXGBE_WRITE_FLUSH(hw);
402                 reg = IXGBE_QDE_ENABLE | IXGBE_QDE_WRITE;
403                 reg |= i << IXGBE_QDE_IDX_SHIFT;
404                 IXGBE_WRITE_REG(hw, IXGBE_QDE, reg);
405         }
406
407         /* enable receive for vf */
408         reg = IXGBE_READ_REG(hw, IXGBE_VFRE(reg_offset));
409         reg |= (reg | (1 << vf_shift));
410         IXGBE_WRITE_REG(hw, IXGBE_VFRE(reg_offset), reg);
411
412         /* Enable counting of spoofed packets in the SSVPC register */
413         reg = IXGBE_READ_REG(hw, IXGBE_VMECM(reg_offset));
414         reg |= (1 << vf_shift);
415         IXGBE_WRITE_REG(hw, IXGBE_VMECM(reg_offset), reg);
416
417         ixgbe_vf_reset_event(dev, vf);
418 }
419
420 static int
421 ixgbe_disable_vf_mc_promisc(struct rte_eth_dev *dev, uint32_t vf)
422 {
423         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
424         uint32_t vmolr;
425
426         vmolr = IXGBE_READ_REG(hw, IXGBE_VMOLR(vf));
427
428         PMD_DRV_LOG(INFO, "VF %u: disabling multicast promiscuous\n", vf);
429
430         vmolr &= ~IXGBE_VMOLR_MPE;
431
432         IXGBE_WRITE_REG(hw, IXGBE_VMOLR(vf), vmolr);
433
434         return 0;
435 }
436
437 static int
438 ixgbe_vf_reset(struct rte_eth_dev *dev, uint16_t vf, uint32_t *msgbuf)
439 {
440         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
441         struct ixgbe_vf_info *vfinfo =
442                 *(IXGBE_DEV_PRIVATE_TO_P_VFDATA(dev->data->dev_private));
443         unsigned char *vf_mac = vfinfo[vf].vf_mac_addresses;
444         int rar_entry = hw->mac.num_rar_entries - (vf + 1);
445         uint8_t *new_mac = (uint8_t *)(&msgbuf[1]);
446
447         ixgbe_vf_reset_msg(dev, vf);
448
449         hw->mac.ops.set_rar(hw, rar_entry, vf_mac, vf, IXGBE_RAH_AV);
450
451         /* Disable multicast promiscuous at reset */
452         ixgbe_disable_vf_mc_promisc(dev, vf);
453
454         /* reply to reset with ack and vf mac address */
455         msgbuf[0] = IXGBE_VF_RESET | IXGBE_VT_MSGTYPE_ACK;
456         rte_memcpy(new_mac, vf_mac, RTE_ETHER_ADDR_LEN);
457         /*
458          * Piggyback the multicast filter type so VF can compute the
459          * correct vectors
460          */
461         msgbuf[3] = hw->mac.mc_filter_type;
462         ixgbe_write_mbx(hw, msgbuf, IXGBE_VF_PERMADDR_MSG_LEN, vf);
463
464         return 0;
465 }
466
467 static int
468 ixgbe_vf_set_mac_addr(struct rte_eth_dev *dev, uint32_t vf, uint32_t *msgbuf)
469 {
470         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
471         struct ixgbe_vf_info *vfinfo =
472                 *(IXGBE_DEV_PRIVATE_TO_P_VFDATA(dev->data->dev_private));
473         int rar_entry = hw->mac.num_rar_entries - (vf + 1);
474         uint8_t *new_mac = (uint8_t *)(&msgbuf[1]);
475
476         if (rte_is_valid_assigned_ether_addr(
477                         (struct rte_ether_addr *)new_mac)) {
478                 rte_memcpy(vfinfo[vf].vf_mac_addresses, new_mac, 6);
479                 return hw->mac.ops.set_rar(hw, rar_entry, new_mac, vf, IXGBE_RAH_AV);
480         }
481         return -1;
482 }
483
484 static int
485 ixgbe_vf_set_multicast(struct rte_eth_dev *dev, uint32_t vf, uint32_t *msgbuf)
486 {
487         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
488         struct ixgbe_vf_info *vfinfo =
489                 *(IXGBE_DEV_PRIVATE_TO_P_VFDATA(dev->data->dev_private));
490         int nb_entries = (msgbuf[0] & IXGBE_VT_MSGINFO_MASK) >>
491                 IXGBE_VT_MSGINFO_SHIFT;
492         uint16_t *hash_list = (uint16_t *)&msgbuf[1];
493         uint32_t mta_idx;
494         uint32_t mta_shift;
495         const uint32_t IXGBE_MTA_INDEX_MASK = 0x7F;
496         const uint32_t IXGBE_MTA_BIT_SHIFT = 5;
497         const uint32_t IXGBE_MTA_BIT_MASK = (0x1 << IXGBE_MTA_BIT_SHIFT) - 1;
498         uint32_t reg_val;
499         int i;
500         u32 vmolr = IXGBE_READ_REG(hw, IXGBE_VMOLR(vf));
501
502         /* Disable multicast promiscuous first */
503         ixgbe_disable_vf_mc_promisc(dev, vf);
504
505         /* only so many hash values supported */
506         nb_entries = RTE_MIN(nb_entries, IXGBE_MAX_VF_MC_ENTRIES);
507
508         /* store the mc entries  */
509         vfinfo->num_vf_mc_hashes = (uint16_t)nb_entries;
510         for (i = 0; i < nb_entries; i++) {
511                 vfinfo->vf_mc_hashes[i] = hash_list[i];
512         }
513
514         if (nb_entries == 0) {
515                 vmolr &= ~IXGBE_VMOLR_ROMPE;
516                 IXGBE_WRITE_REG(hw, IXGBE_VMOLR(vf), vmolr);
517                 return 0;
518         }
519
520         for (i = 0; i < vfinfo->num_vf_mc_hashes; i++) {
521                 mta_idx = (vfinfo->vf_mc_hashes[i] >> IXGBE_MTA_BIT_SHIFT)
522                                 & IXGBE_MTA_INDEX_MASK;
523                 mta_shift = vfinfo->vf_mc_hashes[i] & IXGBE_MTA_BIT_MASK;
524                 reg_val = IXGBE_READ_REG(hw, IXGBE_MTA(mta_idx));
525                 reg_val |= (1 << mta_shift);
526                 IXGBE_WRITE_REG(hw, IXGBE_MTA(mta_idx), reg_val);
527         }
528
529         vmolr |= IXGBE_VMOLR_ROMPE;
530         IXGBE_WRITE_REG(hw, IXGBE_VMOLR(vf), vmolr);
531
532         return 0;
533 }
534
535 static int
536 ixgbe_vf_set_vlan(struct rte_eth_dev *dev, uint32_t vf, uint32_t *msgbuf)
537 {
538         int add, vid;
539         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
540         struct ixgbe_vf_info *vfinfo =
541                 *(IXGBE_DEV_PRIVATE_TO_P_VFDATA(dev->data->dev_private));
542
543         add = (msgbuf[0] & IXGBE_VT_MSGINFO_MASK)
544                 >> IXGBE_VT_MSGINFO_SHIFT;
545         vid = (msgbuf[1] & IXGBE_VLVF_VLANID_MASK);
546
547         if (add)
548                 vfinfo[vf].vlan_count++;
549         else if (vfinfo[vf].vlan_count)
550                 vfinfo[vf].vlan_count--;
551         return hw->mac.ops.set_vfta(hw, vid, vf, (bool)add, false);
552 }
553
554 static int
555 ixgbe_set_vf_lpe(struct rte_eth_dev *dev, uint32_t vf, uint32_t *msgbuf)
556 {
557         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
558         uint32_t max_frame = msgbuf[1];
559         uint32_t max_frs;
560         uint32_t hlreg0;
561
562         /* X540 and X550 support jumbo frames in IOV mode */
563         if (hw->mac.type != ixgbe_mac_X540 &&
564                 hw->mac.type != ixgbe_mac_X550 &&
565                 hw->mac.type != ixgbe_mac_X550EM_x &&
566                 hw->mac.type != ixgbe_mac_X550EM_a) {
567                 struct ixgbe_vf_info *vfinfo =
568                         *IXGBE_DEV_PRIVATE_TO_P_VFDATA(dev->data->dev_private);
569
570                 switch (vfinfo[vf].api_version) {
571                 case ixgbe_mbox_api_11:
572                 case ixgbe_mbox_api_12:
573                 case ixgbe_mbox_api_13:
574                          /**
575                           * Version 1.1&1.2&1.3 supports jumbo frames on VFs
576                           * if PF has jumbo frames enabled which means legacy
577                           * VFs are disabled.
578                           */
579                         if (dev->data->dev_conf.rxmode.max_rx_pkt_len >
580                             IXGBE_ETH_MAX_LEN)
581                                 break;
582                         /* fall through */
583                 default:
584                         /**
585                          * If the PF or VF are running w/ jumbo frames enabled,
586                          * we return -1 as we cannot support jumbo frames on
587                          * legacy VFs.
588                          */
589                         if (max_frame > IXGBE_ETH_MAX_LEN ||
590                             dev->data->dev_conf.rxmode.max_rx_pkt_len >
591                             IXGBE_ETH_MAX_LEN)
592                                 return -1;
593                         break;
594                 }
595         }
596
597         if (max_frame < RTE_ETHER_MIN_LEN ||
598                         max_frame > RTE_ETHER_MAX_JUMBO_FRAME_LEN)
599                 return -1;
600
601         max_frs = (IXGBE_READ_REG(hw, IXGBE_MAXFRS) &
602                    IXGBE_MHADD_MFS_MASK) >> IXGBE_MHADD_MFS_SHIFT;
603         if (max_frs < max_frame) {
604                 hlreg0 = IXGBE_READ_REG(hw, IXGBE_HLREG0);
605                 if (max_frame > IXGBE_ETH_MAX_LEN) {
606                         dev->data->dev_conf.rxmode.offloads |=
607                                 DEV_RX_OFFLOAD_JUMBO_FRAME;
608                         hlreg0 |= IXGBE_HLREG0_JUMBOEN;
609                 } else {
610                         dev->data->dev_conf.rxmode.offloads &=
611                                 ~DEV_RX_OFFLOAD_JUMBO_FRAME;
612                         hlreg0 &= ~IXGBE_HLREG0_JUMBOEN;
613                 }
614                 IXGBE_WRITE_REG(hw, IXGBE_HLREG0, hlreg0);
615
616                 max_frs = max_frame << IXGBE_MHADD_MFS_SHIFT;
617                 IXGBE_WRITE_REG(hw, IXGBE_MAXFRS, max_frs);
618         }
619
620         return 0;
621 }
622
623 static int
624 ixgbe_negotiate_vf_api(struct rte_eth_dev *dev, uint32_t vf, uint32_t *msgbuf)
625 {
626         uint32_t api_version = msgbuf[1];
627         struct ixgbe_vf_info *vfinfo =
628                 *IXGBE_DEV_PRIVATE_TO_P_VFDATA(dev->data->dev_private);
629
630         switch (api_version) {
631         case ixgbe_mbox_api_10:
632         case ixgbe_mbox_api_11:
633         case ixgbe_mbox_api_12:
634         case ixgbe_mbox_api_13:
635                 vfinfo[vf].api_version = (uint8_t)api_version;
636                 return 0;
637         default:
638                 break;
639         }
640
641         PMD_DRV_LOG(ERR, "Negotiate invalid api version %u from VF %d\n",
642                 api_version, vf);
643
644         return -1;
645 }
646
647 static int
648 ixgbe_get_vf_queues(struct rte_eth_dev *dev, uint32_t vf, uint32_t *msgbuf)
649 {
650         struct ixgbe_vf_info *vfinfo =
651                 *IXGBE_DEV_PRIVATE_TO_P_VFDATA(dev->data->dev_private);
652         uint32_t default_q = vf * RTE_ETH_DEV_SRIOV(dev).nb_q_per_pool;
653         struct rte_eth_conf *eth_conf;
654         struct rte_eth_vmdq_dcb_tx_conf *vmdq_dcb_tx_conf;
655         u8 num_tcs;
656         struct ixgbe_hw *hw;
657         u32 vmvir;
658 #define IXGBE_VMVIR_VLANA_MASK          0xC0000000
659 #define IXGBE_VMVIR_VLAN_VID_MASK       0x00000FFF
660 #define IXGBE_VMVIR_VLAN_UP_MASK        0x0000E000
661 #define VLAN_PRIO_SHIFT                 13
662         u32 vlana;
663         u32 vid;
664         u32 user_priority;
665
666         /* Verify if the PF supports the mbox APIs version or not */
667         switch (vfinfo[vf].api_version) {
668         case ixgbe_mbox_api_20:
669         case ixgbe_mbox_api_11:
670         case ixgbe_mbox_api_12:
671         case ixgbe_mbox_api_13:
672                 break;
673         default:
674                 return -1;
675         }
676
677         /* Notify VF of Rx and Tx queue number */
678         msgbuf[IXGBE_VF_RX_QUEUES] = RTE_ETH_DEV_SRIOV(dev).nb_q_per_pool;
679         msgbuf[IXGBE_VF_TX_QUEUES] = RTE_ETH_DEV_SRIOV(dev).nb_q_per_pool;
680
681         /* Notify VF of default queue */
682         msgbuf[IXGBE_VF_DEF_QUEUE] = default_q;
683
684         /* Notify VF of number of DCB traffic classes */
685         eth_conf = &dev->data->dev_conf;
686         switch (eth_conf->txmode.mq_mode) {
687         case ETH_MQ_TX_NONE:
688         case ETH_MQ_TX_DCB:
689                 PMD_DRV_LOG(ERR, "PF must work with virtualization for VF %u"
690                         ", but its tx mode = %d\n", vf,
691                         eth_conf->txmode.mq_mode);
692                 return -1;
693
694         case ETH_MQ_TX_VMDQ_DCB:
695                 vmdq_dcb_tx_conf = &eth_conf->tx_adv_conf.vmdq_dcb_tx_conf;
696                 switch (vmdq_dcb_tx_conf->nb_queue_pools) {
697                 case ETH_16_POOLS:
698                         num_tcs = ETH_8_TCS;
699                         break;
700                 case ETH_32_POOLS:
701                         num_tcs = ETH_4_TCS;
702                         break;
703                 default:
704                         return -1;
705                 }
706                 break;
707
708         /* ETH_MQ_TX_VMDQ_ONLY,  DCB not enabled */
709         case ETH_MQ_TX_VMDQ_ONLY:
710                 hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
711                 vmvir = IXGBE_READ_REG(hw, IXGBE_VMVIR(vf));
712                 vlana = vmvir & IXGBE_VMVIR_VLANA_MASK;
713                 vid = vmvir & IXGBE_VMVIR_VLAN_VID_MASK;
714                 user_priority =
715                         (vmvir & IXGBE_VMVIR_VLAN_UP_MASK) >> VLAN_PRIO_SHIFT;
716                 if ((vlana == IXGBE_VMVIR_VLANA_DEFAULT) &&
717                         ((vid !=  0) || (user_priority != 0)))
718                         num_tcs = 1;
719                 else
720                         num_tcs = 0;
721                 break;
722
723         default:
724                 PMD_DRV_LOG(ERR, "PF work with invalid mode = %d\n",
725                         eth_conf->txmode.mq_mode);
726                 return -1;
727         }
728         msgbuf[IXGBE_VF_TRANS_VLAN] = num_tcs;
729
730         return 0;
731 }
732
733 static int
734 ixgbe_set_vf_mc_promisc(struct rte_eth_dev *dev, uint32_t vf, uint32_t *msgbuf)
735 {
736         struct ixgbe_vf_info *vfinfo =
737                 *(IXGBE_DEV_PRIVATE_TO_P_VFDATA(dev->data->dev_private));
738         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
739         int xcast_mode = msgbuf[1];     /* msgbuf contains the flag to enable */
740         u32 vmolr, fctrl, disable, enable;
741
742         switch (vfinfo[vf].api_version) {
743         case ixgbe_mbox_api_12:
744                 /* promisc introduced in 1.3 version */
745                 if (xcast_mode == IXGBEVF_XCAST_MODE_PROMISC)
746                         return -EOPNOTSUPP;
747                 break;
748                 /* Fall threw */
749         case ixgbe_mbox_api_13:
750                 break;
751         default:
752                 return -1;
753         }
754
755         if (vfinfo[vf].xcast_mode == xcast_mode)
756                 goto out;
757
758         switch (xcast_mode) {
759         case IXGBEVF_XCAST_MODE_NONE:
760                 disable = IXGBE_VMOLR_BAM | IXGBE_VMOLR_ROMPE |
761                           IXGBE_VMOLR_MPE | IXGBE_VMOLR_UPE | IXGBE_VMOLR_VPE;
762                 enable = 0;
763                 break;
764         case IXGBEVF_XCAST_MODE_MULTI:
765                 disable = IXGBE_VMOLR_MPE | IXGBE_VMOLR_UPE | IXGBE_VMOLR_VPE;
766                 enable = IXGBE_VMOLR_BAM | IXGBE_VMOLR_ROMPE;
767                 break;
768         case IXGBEVF_XCAST_MODE_ALLMULTI:
769                 disable = IXGBE_VMOLR_UPE | IXGBE_VMOLR_VPE;
770                 enable = IXGBE_VMOLR_BAM | IXGBE_VMOLR_ROMPE | IXGBE_VMOLR_MPE;
771                 break;
772         case IXGBEVF_XCAST_MODE_PROMISC:
773                 if (hw->mac.type <= ixgbe_mac_82599EB)
774                         return -1;
775
776                 fctrl = IXGBE_READ_REG(hw, IXGBE_FCTRL);
777                 if (!(fctrl & IXGBE_FCTRL_UPE)) {
778                         /* VF promisc requires PF in promisc */
779                         PMD_DRV_LOG(ERR,
780                                "Enabling VF promisc requires PF in promisc\n");
781                         return -1;
782                 }
783
784                 disable = 0;
785                 enable = IXGBE_VMOLR_BAM | IXGBE_VMOLR_ROMPE |
786                          IXGBE_VMOLR_MPE | IXGBE_VMOLR_UPE | IXGBE_VMOLR_VPE;
787                 break;
788         default:
789                 return -1;
790         }
791
792         vmolr = IXGBE_READ_REG(hw, IXGBE_VMOLR(vf));
793         vmolr &= ~disable;
794         vmolr |= enable;
795         IXGBE_WRITE_REG(hw, IXGBE_VMOLR(vf), vmolr);
796         vfinfo[vf].xcast_mode = xcast_mode;
797
798 out:
799         msgbuf[1] = xcast_mode;
800
801         return 0;
802 }
803
804 static int
805 ixgbe_set_vf_macvlan_msg(struct rte_eth_dev *dev, uint32_t vf, uint32_t *msgbuf)
806 {
807         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
808         struct ixgbe_vf_info *vf_info =
809                 *(IXGBE_DEV_PRIVATE_TO_P_VFDATA(dev->data->dev_private));
810         uint8_t *new_mac = (uint8_t *)(&msgbuf[1]);
811         int index = (msgbuf[0] & IXGBE_VT_MSGINFO_MASK) >>
812                     IXGBE_VT_MSGINFO_SHIFT;
813
814         if (index) {
815                 if (!rte_is_valid_assigned_ether_addr(
816                         (struct rte_ether_addr *)new_mac)) {
817                         PMD_DRV_LOG(ERR, "set invalid mac vf:%d\n", vf);
818                         return -1;
819                 }
820
821                 vf_info[vf].mac_count++;
822
823                 hw->mac.ops.set_rar(hw, vf_info[vf].mac_count,
824                                 new_mac, vf, IXGBE_RAH_AV);
825         } else {
826                 if (vf_info[vf].mac_count) {
827                         hw->mac.ops.clear_rar(hw, vf_info[vf].mac_count);
828                         vf_info[vf].mac_count = 0;
829                 }
830         }
831         return 0;
832 }
833
834 static int
835 ixgbe_rcv_msg_from_vf(struct rte_eth_dev *dev, uint16_t vf)
836 {
837         uint16_t mbx_size = IXGBE_VFMAILBOX_SIZE;
838         uint16_t msg_size = IXGBE_VF_MSG_SIZE_DEFAULT;
839         uint32_t msgbuf[IXGBE_VFMAILBOX_SIZE];
840         int32_t retval;
841         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
842         struct ixgbe_vf_info *vfinfo =
843                 *IXGBE_DEV_PRIVATE_TO_P_VFDATA(dev->data->dev_private);
844         struct rte_pmd_ixgbe_mb_event_param ret_param;
845
846         retval = ixgbe_read_mbx(hw, msgbuf, mbx_size, vf);
847         if (retval) {
848                 PMD_DRV_LOG(ERR, "Error mbx recv msg from VF %d", vf);
849                 return retval;
850         }
851
852         /* do nothing with the message already been processed */
853         if (msgbuf[0] & (IXGBE_VT_MSGTYPE_ACK | IXGBE_VT_MSGTYPE_NACK))
854                 return retval;
855
856         /* flush the ack before we write any messages back */
857         IXGBE_WRITE_FLUSH(hw);
858
859         /**
860          * initialise structure to send to user application
861          * will return response from user in retval field
862          */
863         ret_param.retval = RTE_PMD_IXGBE_MB_EVENT_PROCEED;
864         ret_param.vfid = vf;
865         ret_param.msg_type = msgbuf[0] & 0xFFFF;
866         ret_param.msg = (void *)msgbuf;
867
868         /* perform VF reset */
869         if (msgbuf[0] == IXGBE_VF_RESET) {
870                 int ret = ixgbe_vf_reset(dev, vf, msgbuf);
871
872                 vfinfo[vf].clear_to_send = true;
873
874                 /* notify application about VF reset */
875                 rte_eth_dev_callback_process(dev, RTE_ETH_EVENT_VF_MBOX,
876                                               &ret_param);
877                 return ret;
878         }
879
880         /**
881          * ask user application if we allowed to perform those functions
882          * if we get ret_param.retval == RTE_PMD_IXGBE_MB_EVENT_PROCEED
883          * then business as usual,
884          * if 0, do nothing and send ACK to VF
885          * if ret_param.retval > 1, do nothing and send NAK to VF
886          */
887         rte_eth_dev_callback_process(dev, RTE_ETH_EVENT_VF_MBOX, &ret_param);
888
889         retval = ret_param.retval;
890
891         /* check & process VF to PF mailbox message */
892         switch ((msgbuf[0] & 0xFFFF)) {
893         case IXGBE_VF_SET_MAC_ADDR:
894                 if (retval == RTE_PMD_IXGBE_MB_EVENT_PROCEED)
895                         retval = ixgbe_vf_set_mac_addr(dev, vf, msgbuf);
896                 break;
897         case IXGBE_VF_SET_MULTICAST:
898                 if (retval == RTE_PMD_IXGBE_MB_EVENT_PROCEED)
899                         retval = ixgbe_vf_set_multicast(dev, vf, msgbuf);
900                 break;
901         case IXGBE_VF_SET_LPE:
902                 if (retval == RTE_PMD_IXGBE_MB_EVENT_PROCEED)
903                         retval = ixgbe_set_vf_lpe(dev, vf, msgbuf);
904                 break;
905         case IXGBE_VF_SET_VLAN:
906                 if (retval == RTE_PMD_IXGBE_MB_EVENT_PROCEED)
907                         retval = ixgbe_vf_set_vlan(dev, vf, msgbuf);
908                 break;
909         case IXGBE_VF_API_NEGOTIATE:
910                 retval = ixgbe_negotiate_vf_api(dev, vf, msgbuf);
911                 break;
912         case IXGBE_VF_GET_QUEUES:
913                 retval = ixgbe_get_vf_queues(dev, vf, msgbuf);
914                 msg_size = IXGBE_VF_GET_QUEUE_MSG_SIZE;
915                 break;
916         case IXGBE_VF_UPDATE_XCAST_MODE:
917                 if (retval == RTE_PMD_IXGBE_MB_EVENT_PROCEED)
918                         retval = ixgbe_set_vf_mc_promisc(dev, vf, msgbuf);
919                 break;
920         case IXGBE_VF_SET_MACVLAN:
921                 if (retval == RTE_PMD_IXGBE_MB_EVENT_PROCEED)
922                         retval = ixgbe_set_vf_macvlan_msg(dev, vf, msgbuf);
923                 break;
924         default:
925                 PMD_DRV_LOG(DEBUG, "Unhandled Msg %8.8x", (unsigned)msgbuf[0]);
926                 retval = IXGBE_ERR_MBX;
927                 break;
928         }
929
930         /* response the VF according to the message process result */
931         if (retval)
932                 msgbuf[0] |= IXGBE_VT_MSGTYPE_NACK;
933         else
934                 msgbuf[0] |= IXGBE_VT_MSGTYPE_ACK;
935
936         msgbuf[0] |= IXGBE_VT_MSGTYPE_CTS;
937
938         ixgbe_write_mbx(hw, msgbuf, msg_size, vf);
939
940         return retval;
941 }
942
943 static inline void
944 ixgbe_rcv_ack_from_vf(struct rte_eth_dev *dev, uint16_t vf)
945 {
946         uint32_t msg = IXGBE_VT_MSGTYPE_NACK;
947         struct ixgbe_hw *hw =
948                 IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
949         struct ixgbe_vf_info *vfinfo =
950                 *IXGBE_DEV_PRIVATE_TO_P_VFDATA(dev->data->dev_private);
951
952         if (!vfinfo[vf].clear_to_send)
953                 ixgbe_write_mbx(hw, &msg, 1, vf);
954 }
955
956 void ixgbe_pf_mbx_process(struct rte_eth_dev *eth_dev)
957 {
958         uint16_t vf;
959         struct ixgbe_hw *hw =
960                 IXGBE_DEV_PRIVATE_TO_HW(eth_dev->data->dev_private);
961
962         for (vf = 0; vf < dev_num_vf(eth_dev); vf++) {
963                 /* check & process vf function level reset */
964                 if (!ixgbe_check_for_rst(hw, vf))
965                         ixgbe_vf_reset_event(eth_dev, vf);
966
967                 /* check & process vf mailbox messages */
968                 if (!ixgbe_check_for_msg(hw, vf))
969                         ixgbe_rcv_msg_from_vf(eth_dev, vf);
970
971                 /* check & process acks from vf */
972                 if (!ixgbe_check_for_ack(hw, vf))
973                         ixgbe_rcv_ack_from_vf(eth_dev, vf);
974         }
975 }