net/txgbe: add PF module configure for SRIOV
[dpdk.git] / drivers / net / txgbe / txgbe_pf.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2015-2020
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 <rte_ethdev_driver.h>
19 #include <rte_memcpy.h>
20 #include <rte_malloc.h>
21 #include <rte_random.h>
22 #include <rte_bus_pci.h>
23
24 #include "base/txgbe.h"
25 #include "txgbe_ethdev.h"
26 #include "rte_pmd_txgbe.h"
27
28 #define TXGBE_MAX_VFTA     (128)
29 #define TXGBE_VF_MSG_SIZE_DEFAULT 1
30 #define TXGBE_VF_GET_QUEUE_MSG_SIZE 5
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 txgbe_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 txgbe_vf_info *vfinfo = *TXGBE_DEV_VFDATA(dev);
45         uint16_t vfn;
46
47         for (vfn = 0; vfn < vf_num; vfn++) {
48                 rte_eth_random_addr(vf_mac_addr);
49                 /* keep the random address as default */
50                 memcpy(vfinfo[vfn].vf_mac_addresses, vf_mac_addr,
51                            RTE_ETHER_ADDR_LEN);
52         }
53
54         return 0;
55 }
56
57 static inline int
58 txgbe_mb_intr_setup(struct rte_eth_dev *dev)
59 {
60         struct txgbe_interrupt *intr = TXGBE_DEV_INTR(dev);
61
62         intr->mask_misc |= TXGBE_ICRMISC_VFMBX;
63
64         return 0;
65 }
66
67 void txgbe_pf_host_init(struct rte_eth_dev *eth_dev)
68 {
69         struct txgbe_vf_info **vfinfo = TXGBE_DEV_VFDATA(eth_dev);
70         struct txgbe_mirror_info *mirror_info = TXGBE_DEV_MR_INFO(eth_dev);
71         struct txgbe_uta_info *uta_info = TXGBE_DEV_UTA_INFO(eth_dev);
72         struct txgbe_hw *hw = TXGBE_DEV_HW(eth_dev);
73         uint16_t vf_num;
74         uint8_t nb_queue;
75
76         PMD_INIT_FUNC_TRACE();
77
78         RTE_ETH_DEV_SRIOV(eth_dev).active = 0;
79         vf_num = dev_num_vf(eth_dev);
80         if (vf_num == 0)
81                 return;
82
83         *vfinfo = rte_zmalloc("vf_info",
84                         sizeof(struct txgbe_vf_info) * vf_num, 0);
85         if (*vfinfo == NULL)
86                 rte_panic("Cannot allocate memory for private VF data\n");
87
88         rte_eth_switch_domain_alloc(&(*vfinfo)->switch_domain_id);
89
90         memset(mirror_info, 0, sizeof(struct txgbe_mirror_info));
91         memset(uta_info, 0, sizeof(struct txgbe_uta_info));
92         hw->mac.mc_filter_type = 0;
93
94         if (vf_num >= ETH_32_POOLS) {
95                 nb_queue = 2;
96                 RTE_ETH_DEV_SRIOV(eth_dev).active = ETH_64_POOLS;
97         } else if (vf_num >= ETH_16_POOLS) {
98                 nb_queue = 4;
99                 RTE_ETH_DEV_SRIOV(eth_dev).active = ETH_32_POOLS;
100         } else {
101                 nb_queue = 8;
102                 RTE_ETH_DEV_SRIOV(eth_dev).active = ETH_16_POOLS;
103         }
104
105         RTE_ETH_DEV_SRIOV(eth_dev).nb_q_per_pool = nb_queue;
106         RTE_ETH_DEV_SRIOV(eth_dev).def_vmdq_idx = vf_num;
107         RTE_ETH_DEV_SRIOV(eth_dev).def_pool_q_idx =
108                         (uint16_t)(vf_num * nb_queue);
109
110         txgbe_vf_perm_addr_gen(eth_dev, vf_num);
111
112         /* init_mailbox_params */
113         hw->mbx.init_params(hw);
114
115         /* set mb interrupt mask */
116         txgbe_mb_intr_setup(eth_dev);
117 }
118
119 void txgbe_pf_host_uninit(struct rte_eth_dev *eth_dev)
120 {
121         struct txgbe_vf_info **vfinfo;
122         uint16_t vf_num;
123         int ret;
124
125         PMD_INIT_FUNC_TRACE();
126
127         RTE_ETH_DEV_SRIOV(eth_dev).active = 0;
128         RTE_ETH_DEV_SRIOV(eth_dev).nb_q_per_pool = 0;
129         RTE_ETH_DEV_SRIOV(eth_dev).def_vmdq_idx = 0;
130         RTE_ETH_DEV_SRIOV(eth_dev).def_pool_q_idx = 0;
131
132         vf_num = dev_num_vf(eth_dev);
133         if (vf_num == 0)
134                 return;
135
136         vfinfo = TXGBE_DEV_VFDATA(eth_dev);
137         if (*vfinfo == NULL)
138                 return;
139
140         ret = rte_eth_switch_domain_free((*vfinfo)->switch_domain_id);
141         if (ret)
142                 PMD_INIT_LOG(WARNING, "failed to free switch domain: %d", ret);
143
144         rte_free(*vfinfo);
145         *vfinfo = NULL;
146 }
147
148 static void
149 txgbe_add_tx_flow_control_drop_filter(struct rte_eth_dev *eth_dev)
150 {
151         struct txgbe_hw *hw = TXGBE_DEV_HW(eth_dev);
152         struct txgbe_filter_info *filter_info = TXGBE_DEV_FILTER(eth_dev);
153         uint16_t vf_num;
154         int i;
155         struct txgbe_ethertype_filter ethertype_filter;
156
157         if (!hw->mac.set_ethertype_anti_spoofing) {
158                 PMD_DRV_LOG(INFO, "ether type anti-spoofing is not supported.\n");
159                 return;
160         }
161
162         i = txgbe_ethertype_filter_lookup(filter_info,
163                                           TXGBE_ETHERTYPE_FLOW_CTRL);
164         if (i >= 0) {
165                 PMD_DRV_LOG(ERR, "A ether type filter entity for flow control already exists!\n");
166                 return;
167         }
168
169         ethertype_filter.ethertype = TXGBE_ETHERTYPE_FLOW_CTRL;
170         ethertype_filter.etqf = TXGBE_ETFLT_ENA |
171                                 TXGBE_ETFLT_TXAS |
172                                 TXGBE_ETHERTYPE_FLOW_CTRL;
173         ethertype_filter.etqs = 0;
174         ethertype_filter.conf = TRUE;
175         i = txgbe_ethertype_filter_insert(filter_info,
176                                           &ethertype_filter);
177         if (i < 0) {
178                 PMD_DRV_LOG(ERR, "Cannot find an unused ether type filter entity for flow control.\n");
179                 return;
180         }
181
182         wr32(hw, TXGBE_ETFLT(i),
183                         (TXGBE_ETFLT_ENA |
184                         TXGBE_ETFLT_TXAS |
185                         TXGBE_ETHERTYPE_FLOW_CTRL));
186
187         vf_num = dev_num_vf(eth_dev);
188         for (i = 0; i < vf_num; i++)
189                 hw->mac.set_ethertype_anti_spoofing(hw, true, i);
190 }
191
192 int txgbe_pf_host_configure(struct rte_eth_dev *eth_dev)
193 {
194         uint32_t vtctl, fcrth;
195         uint32_t vfre_slot, vfre_offset;
196         uint16_t vf_num;
197         const uint8_t VFRE_SHIFT = 5;  /* VFRE 32 bits per slot */
198         const uint8_t VFRE_MASK = (uint8_t)((1U << VFRE_SHIFT) - 1);
199         struct txgbe_hw *hw = TXGBE_DEV_HW(eth_dev);
200         uint32_t gpie;
201         uint32_t gcr_ext;
202         uint32_t vlanctrl;
203         int i;
204
205         vf_num = dev_num_vf(eth_dev);
206         if (vf_num == 0)
207                 return -1;
208
209         /* enable VMDq and set the default pool for PF */
210         vtctl = rd32(hw, TXGBE_POOLCTL);
211         vtctl &= ~TXGBE_POOLCTL_DEFPL_MASK;
212         vtctl |= TXGBE_POOLCTL_DEFPL(RTE_ETH_DEV_SRIOV(eth_dev).def_vmdq_idx);
213         vtctl |= TXGBE_POOLCTL_RPLEN;
214         wr32(hw, TXGBE_POOLCTL, vtctl);
215
216         vfre_offset = vf_num & VFRE_MASK;
217         vfre_slot = (vf_num >> VFRE_SHIFT) > 0 ? 1 : 0;
218
219         /* Enable pools reserved to PF only */
220         wr32(hw, TXGBE_POOLRXENA(vfre_slot), (~0U) << vfre_offset);
221         wr32(hw, TXGBE_POOLRXENA(vfre_slot ^ 1), vfre_slot - 1);
222         wr32(hw, TXGBE_POOLTXENA(vfre_slot), (~0U) << vfre_offset);
223         wr32(hw, TXGBE_POOLTXENA(vfre_slot ^ 1), vfre_slot - 1);
224
225         wr32(hw, TXGBE_PSRCTL, TXGBE_PSRCTL_LBENA);
226
227         /* clear VMDq map to perment rar 0 */
228         hw->mac.clear_vmdq(hw, 0, BIT_MASK32);
229
230         /* clear VMDq map to scan rar 127 */
231         wr32(hw, TXGBE_ETHADDRIDX, hw->mac.num_rar_entries);
232         wr32(hw, TXGBE_ETHADDRASSL, 0);
233         wr32(hw, TXGBE_ETHADDRASSH, 0);
234
235         /* set VMDq map to default PF pool */
236         hw->mac.set_vmdq(hw, 0, RTE_ETH_DEV_SRIOV(eth_dev).def_vmdq_idx);
237
238         /*
239          * SW msut set PORTCTL.VT_Mode the same as GPIE.VT_Mode
240          */
241         gpie = rd32(hw, TXGBE_GPIE);
242         gpie |= TXGBE_GPIE_MSIX;
243         gcr_ext = rd32(hw, TXGBE_PORTCTL);
244         gcr_ext &= ~TXGBE_PORTCTL_NUMVT_MASK;
245
246         switch (RTE_ETH_DEV_SRIOV(eth_dev).active) {
247         case ETH_64_POOLS:
248                 gcr_ext |= TXGBE_PORTCTL_NUMVT_64;
249                 break;
250         case ETH_32_POOLS:
251                 gcr_ext |= TXGBE_PORTCTL_NUMVT_32;
252                 break;
253         case ETH_16_POOLS:
254                 gcr_ext |= TXGBE_PORTCTL_NUMVT_16;
255                 break;
256         }
257
258         wr32(hw, TXGBE_PORTCTL, gcr_ext);
259         wr32(hw, TXGBE_GPIE, gpie);
260
261         /*
262          * enable vlan filtering and allow all vlan tags through
263          */
264         vlanctrl = rd32(hw, TXGBE_VLANCTL);
265         vlanctrl |= TXGBE_VLANCTL_VFE; /* enable vlan filters */
266         wr32(hw, TXGBE_VLANCTL, vlanctrl);
267
268         /* enable all vlan filters */
269         for (i = 0; i < TXGBE_MAX_VFTA; i++)
270                 wr32(hw, TXGBE_VLANTBL(i), 0xFFFFFFFF);
271
272         /* Enable MAC Anti-Spoofing */
273         hw->mac.set_mac_anti_spoofing(hw, FALSE, vf_num);
274
275         /* set flow control threshold to max to avoid tx switch hang */
276         for (i = 0; i < TXGBE_DCB_TC_MAX; i++) {
277                 wr32(hw, TXGBE_FCWTRLO(i), 0);
278                 fcrth = rd32(hw, TXGBE_PBRXSIZE(i)) - 32;
279                 wr32(hw, TXGBE_FCWTRHI(i), fcrth);
280         }
281
282         txgbe_add_tx_flow_control_drop_filter(eth_dev);
283
284         return 0;
285 }
286
287 static void
288 txgbe_set_rx_mode(struct rte_eth_dev *eth_dev)
289 {
290         struct rte_eth_dev_data *dev_data = eth_dev->data;
291         struct txgbe_hw *hw = TXGBE_DEV_HW(eth_dev);
292         u32 fctrl, vmolr;
293         uint16_t vfn = dev_num_vf(eth_dev);
294
295         /* disable store-bad-packets */
296         wr32m(hw, TXGBE_SECRXCTL, TXGBE_SECRXCTL_SAVEBAD, 0);
297
298         /* Check for Promiscuous and All Multicast modes */
299         fctrl = rd32m(hw, TXGBE_PSRCTL,
300                         ~(TXGBE_PSRCTL_UCP | TXGBE_PSRCTL_MCP));
301         fctrl |= TXGBE_PSRCTL_BCA |
302                  TXGBE_PSRCTL_MCHFENA;
303
304         vmolr = rd32m(hw, TXGBE_POOLETHCTL(vfn),
305                         ~(TXGBE_POOLETHCTL_UCP |
306                           TXGBE_POOLETHCTL_MCP |
307                           TXGBE_POOLETHCTL_UCHA |
308                           TXGBE_POOLETHCTL_MCHA));
309         vmolr |= TXGBE_POOLETHCTL_BCA |
310                  TXGBE_POOLETHCTL_UTA |
311                  TXGBE_POOLETHCTL_VLA;
312
313         if (dev_data->promiscuous) {
314                 fctrl |= TXGBE_PSRCTL_UCP |
315                          TXGBE_PSRCTL_MCP;
316                 /* pf don't want packets routing to vf, so clear UPE */
317                 vmolr |= TXGBE_POOLETHCTL_MCP;
318         } else if (dev_data->all_multicast) {
319                 fctrl |= TXGBE_PSRCTL_MCP;
320                 vmolr |= TXGBE_POOLETHCTL_MCP;
321         } else {
322                 vmolr |= TXGBE_POOLETHCTL_UCHA;
323                 vmolr |= TXGBE_POOLETHCTL_MCHA;
324         }
325
326         wr32(hw, TXGBE_POOLETHCTL(vfn), vmolr);
327
328         wr32(hw, TXGBE_PSRCTL, fctrl);
329
330         txgbe_vlan_hw_strip_config(eth_dev);
331 }
332
333 static inline void
334 txgbe_vf_reset_event(struct rte_eth_dev *eth_dev, uint16_t vf)
335 {
336         struct txgbe_hw *hw = TXGBE_DEV_HW(eth_dev);
337         struct txgbe_vf_info *vfinfo = *(TXGBE_DEV_VFDATA(eth_dev));
338         int rar_entry = hw->mac.num_rar_entries - (vf + 1);
339         uint32_t vmolr = rd32(hw, TXGBE_POOLETHCTL(vf));
340
341         vmolr |= (TXGBE_POOLETHCTL_UCHA |
342                         TXGBE_POOLETHCTL_BCA | TXGBE_POOLETHCTL_UTA);
343         wr32(hw, TXGBE_POOLETHCTL(vf), vmolr);
344
345         wr32(hw, TXGBE_POOLTAG(vf), 0);
346
347         /* reset multicast table array for vf */
348         vfinfo[vf].num_vf_mc_hashes = 0;
349
350         /* reset rx mode */
351         txgbe_set_rx_mode(eth_dev);
352
353         hw->mac.clear_rar(hw, rar_entry);
354 }
355
356 static inline void
357 txgbe_vf_reset_msg(struct rte_eth_dev *eth_dev, uint16_t vf)
358 {
359         struct txgbe_hw *hw = TXGBE_DEV_HW(eth_dev);
360         uint32_t reg;
361         uint32_t reg_offset, vf_shift;
362         const uint8_t VFRE_SHIFT = 5;  /* VFRE 32 bits per slot */
363         const uint8_t VFRE_MASK = (uint8_t)((1U << VFRE_SHIFT) - 1);
364         uint8_t  nb_q_per_pool;
365         int i;
366
367         vf_shift = vf & VFRE_MASK;
368         reg_offset = (vf >> VFRE_SHIFT) > 0 ? 1 : 0;
369
370         /* enable transmit for vf */
371         reg = rd32(hw, TXGBE_POOLTXENA(reg_offset));
372         reg |= (reg | (1 << vf_shift));
373         wr32(hw, TXGBE_POOLTXENA(reg_offset), reg);
374
375         /* enable all queue drop for IOV */
376         nb_q_per_pool = RTE_ETH_DEV_SRIOV(eth_dev).nb_q_per_pool;
377         for (i = vf * nb_q_per_pool; i < (vf + 1) * nb_q_per_pool; i++) {
378                 txgbe_flush(hw);
379                 reg = 1 << (i % 32);
380                 wr32m(hw, TXGBE_QPRXDROP(i / 32), reg, reg);
381         }
382
383         /* enable receive for vf */
384         reg = rd32(hw, TXGBE_POOLRXENA(reg_offset));
385         reg |= (reg | (1 << vf_shift));
386         wr32(hw, TXGBE_POOLRXENA(reg_offset), reg);
387
388         txgbe_vf_reset_event(eth_dev, vf);
389 }
390
391 static int
392 txgbe_disable_vf_mc_promisc(struct rte_eth_dev *eth_dev, uint32_t vf)
393 {
394         struct txgbe_hw *hw = TXGBE_DEV_HW(eth_dev);
395         uint32_t vmolr;
396
397         vmolr = rd32(hw, TXGBE_POOLETHCTL(vf));
398
399         PMD_DRV_LOG(INFO, "VF %u: disabling multicast promiscuous\n", vf);
400
401         vmolr &= ~TXGBE_POOLETHCTL_MCP;
402
403         wr32(hw, TXGBE_POOLETHCTL(vf), vmolr);
404
405         return 0;
406 }
407
408 static int
409 txgbe_vf_reset(struct rte_eth_dev *eth_dev, uint16_t vf, uint32_t *msgbuf)
410 {
411         struct txgbe_hw *hw = TXGBE_DEV_HW(eth_dev);
412         struct txgbe_vf_info *vfinfo = *(TXGBE_DEV_VFDATA(eth_dev));
413         unsigned char *vf_mac = vfinfo[vf].vf_mac_addresses;
414         int rar_entry = hw->mac.num_rar_entries - (vf + 1);
415         uint8_t *new_mac = (uint8_t *)(&msgbuf[1]);
416
417         txgbe_vf_reset_msg(eth_dev, vf);
418
419         hw->mac.set_rar(hw, rar_entry, vf_mac, vf, true);
420
421         /* Disable multicast promiscuous at reset */
422         txgbe_disable_vf_mc_promisc(eth_dev, vf);
423
424         /* reply to reset with ack and vf mac address */
425         msgbuf[0] = TXGBE_VF_RESET | TXGBE_VT_MSGTYPE_ACK;
426         rte_memcpy(new_mac, vf_mac, RTE_ETHER_ADDR_LEN);
427         /*
428          * Piggyback the multicast filter type so VF can compute the
429          * correct vectors
430          */
431         msgbuf[3] = hw->mac.mc_filter_type;
432         txgbe_write_mbx(hw, msgbuf, TXGBE_VF_PERMADDR_MSG_LEN, vf);
433
434         return 0;
435 }
436
437 static int
438 txgbe_vf_set_mac_addr(struct rte_eth_dev *eth_dev,
439                 uint32_t vf, uint32_t *msgbuf)
440 {
441         struct txgbe_hw *hw = TXGBE_DEV_HW(eth_dev);
442         struct txgbe_vf_info *vfinfo = *(TXGBE_DEV_VFDATA(eth_dev));
443         int rar_entry = hw->mac.num_rar_entries - (vf + 1);
444         uint8_t *new_mac = (uint8_t *)(&msgbuf[1]);
445         struct rte_ether_addr *ea = (struct rte_ether_addr *)new_mac;
446
447         if (rte_is_valid_assigned_ether_addr(ea)) {
448                 rte_memcpy(vfinfo[vf].vf_mac_addresses, new_mac, 6);
449                 return hw->mac.set_rar(hw, rar_entry, new_mac, vf, true);
450         }
451         return -1;
452 }
453
454 static int
455 txgbe_vf_set_multicast(struct rte_eth_dev *eth_dev,
456                 uint32_t vf, uint32_t *msgbuf)
457 {
458         struct txgbe_hw *hw = TXGBE_DEV_HW(eth_dev);
459         struct txgbe_vf_info *vfinfo = *(TXGBE_DEV_VFDATA(eth_dev));
460         int nb_entries = (msgbuf[0] & TXGBE_VT_MSGINFO_MASK) >>
461                 TXGBE_VT_MSGINFO_SHIFT;
462         uint16_t *hash_list = (uint16_t *)&msgbuf[1];
463         uint32_t mta_idx;
464         uint32_t mta_shift;
465         const uint32_t TXGBE_MTA_INDEX_MASK = 0x7F;
466         const uint32_t TXGBE_MTA_BIT_SHIFT = 5;
467         const uint32_t TXGBE_MTA_BIT_MASK = (0x1 << TXGBE_MTA_BIT_SHIFT) - 1;
468         uint32_t reg_val;
469         int i;
470         u32 vmolr = rd32(hw, TXGBE_POOLETHCTL(vf));
471
472         /* Disable multicast promiscuous first */
473         txgbe_disable_vf_mc_promisc(eth_dev, vf);
474
475         /* only so many hash values supported */
476         nb_entries = RTE_MIN(nb_entries, TXGBE_MAX_VF_MC_ENTRIES);
477
478         /* store the mc entries  */
479         vfinfo->num_vf_mc_hashes = (uint16_t)nb_entries;
480         for (i = 0; i < nb_entries; i++)
481                 vfinfo->vf_mc_hashes[i] = hash_list[i];
482
483         if (nb_entries == 0) {
484                 vmolr &= ~TXGBE_POOLETHCTL_MCHA;
485                 wr32(hw, TXGBE_POOLETHCTL(vf), vmolr);
486                 return 0;
487         }
488
489         for (i = 0; i < vfinfo->num_vf_mc_hashes; i++) {
490                 mta_idx = (vfinfo->vf_mc_hashes[i] >> TXGBE_MTA_BIT_SHIFT)
491                                 & TXGBE_MTA_INDEX_MASK;
492                 mta_shift = vfinfo->vf_mc_hashes[i] & TXGBE_MTA_BIT_MASK;
493                 reg_val = rd32(hw, TXGBE_MCADDRTBL(mta_idx));
494                 reg_val |= (1 << mta_shift);
495                 wr32(hw, TXGBE_MCADDRTBL(mta_idx), reg_val);
496         }
497
498         vmolr |= TXGBE_POOLETHCTL_MCHA;
499         wr32(hw, TXGBE_POOLETHCTL(vf), vmolr);
500
501         return 0;
502 }
503
504 static int
505 txgbe_vf_set_vlan(struct rte_eth_dev *eth_dev, uint32_t vf, uint32_t *msgbuf)
506 {
507         int add, vid;
508         struct txgbe_hw *hw = TXGBE_DEV_HW(eth_dev);
509         struct txgbe_vf_info *vfinfo = *(TXGBE_DEV_VFDATA(eth_dev));
510
511         add = (msgbuf[0] & TXGBE_VT_MSGINFO_MASK)
512                 >> TXGBE_VT_MSGINFO_SHIFT;
513         vid = TXGBE_PSRVLAN_VID(msgbuf[1]);
514
515         if (add)
516                 vfinfo[vf].vlan_count++;
517         else if (vfinfo[vf].vlan_count)
518                 vfinfo[vf].vlan_count--;
519         return hw->mac.set_vfta(hw, vid, vf, (bool)add, false);
520 }
521
522 static int
523 txgbe_set_vf_lpe(struct rte_eth_dev *eth_dev,
524                 __rte_unused uint32_t vf, uint32_t *msgbuf)
525 {
526         struct txgbe_hw *hw = TXGBE_DEV_HW(eth_dev);
527         uint32_t max_frame = msgbuf[1];
528         uint32_t max_frs;
529
530         if (max_frame < RTE_ETHER_MIN_LEN ||
531                         max_frame > RTE_ETHER_MAX_JUMBO_FRAME_LEN)
532                 return -1;
533
534         max_frs = rd32m(hw, TXGBE_FRMSZ, TXGBE_FRMSZ_MAX_MASK);
535         if (max_frs < max_frame) {
536                 wr32m(hw, TXGBE_FRMSZ, TXGBE_FRMSZ_MAX_MASK,
537                         TXGBE_FRMSZ_MAX(max_frame));
538         }
539
540         return 0;
541 }
542
543 static int
544 txgbe_negotiate_vf_api(struct rte_eth_dev *eth_dev,
545                 uint32_t vf, uint32_t *msgbuf)
546 {
547         uint32_t api_version = msgbuf[1];
548         struct txgbe_vf_info *vfinfo = *TXGBE_DEV_VFDATA(eth_dev);
549
550         switch (api_version) {
551         case txgbe_mbox_api_10:
552         case txgbe_mbox_api_11:
553         case txgbe_mbox_api_12:
554         case txgbe_mbox_api_13:
555                 vfinfo[vf].api_version = (uint8_t)api_version;
556                 return 0;
557         default:
558                 break;
559         }
560
561         PMD_DRV_LOG(ERR, "Negotiate invalid api version %u from VF %d\n",
562                 api_version, vf);
563
564         return -1;
565 }
566
567 static int
568 txgbe_get_vf_queues(struct rte_eth_dev *eth_dev, uint32_t vf, uint32_t *msgbuf)
569 {
570         struct txgbe_vf_info *vfinfo = *TXGBE_DEV_VFDATA(eth_dev);
571         uint32_t default_q = vf * RTE_ETH_DEV_SRIOV(eth_dev).nb_q_per_pool;
572         struct rte_eth_conf *eth_conf;
573         struct rte_eth_vmdq_dcb_tx_conf *vmdq_dcb_tx_conf;
574         u8 num_tcs;
575         struct txgbe_hw *hw;
576         u32 vmvir;
577         u32 vlana;
578         u32 vid;
579         u32 user_priority;
580
581         /* Verify if the PF supports the mbox APIs version or not */
582         switch (vfinfo[vf].api_version) {
583         case txgbe_mbox_api_20:
584         case txgbe_mbox_api_11:
585         case txgbe_mbox_api_12:
586         case txgbe_mbox_api_13:
587                 break;
588         default:
589                 return -1;
590         }
591
592         /* Notify VF of Rx and Tx queue number */
593         msgbuf[TXGBE_VF_RX_QUEUES] = RTE_ETH_DEV_SRIOV(eth_dev).nb_q_per_pool;
594         msgbuf[TXGBE_VF_TX_QUEUES] = RTE_ETH_DEV_SRIOV(eth_dev).nb_q_per_pool;
595
596         /* Notify VF of default queue */
597         msgbuf[TXGBE_VF_DEF_QUEUE] = default_q;
598
599         /* Notify VF of number of DCB traffic classes */
600         eth_conf = &eth_dev->data->dev_conf;
601         switch (eth_conf->txmode.mq_mode) {
602         case ETH_MQ_TX_NONE:
603         case ETH_MQ_TX_DCB:
604                 PMD_DRV_LOG(ERR, "PF must work with virtualization for VF %u"
605                         ", but its tx mode = %d\n", vf,
606                         eth_conf->txmode.mq_mode);
607                 return -1;
608
609         case ETH_MQ_TX_VMDQ_DCB:
610                 vmdq_dcb_tx_conf = &eth_conf->tx_adv_conf.vmdq_dcb_tx_conf;
611                 switch (vmdq_dcb_tx_conf->nb_queue_pools) {
612                 case ETH_16_POOLS:
613                         num_tcs = ETH_8_TCS;
614                         break;
615                 case ETH_32_POOLS:
616                         num_tcs = ETH_4_TCS;
617                         break;
618                 default:
619                         return -1;
620                 }
621                 break;
622
623         /* ETH_MQ_TX_VMDQ_ONLY,  DCB not enabled */
624         case ETH_MQ_TX_VMDQ_ONLY:
625                 hw = TXGBE_DEV_HW(eth_dev);
626                 vmvir = rd32(hw, TXGBE_POOLTAG(vf));
627                 vlana = vmvir & TXGBE_POOLTAG_ACT_MASK;
628                 vid = vmvir & TXGBE_POOLTAG_VTAG_MASK;
629                 user_priority =
630                         TXGBD_POOLTAG_VTAG_UP(vmvir);
631                 if (vlana == TXGBE_POOLTAG_ACT_ALWAYS &&
632                         (vid !=  0 || user_priority != 0))
633                         num_tcs = 1;
634                 else
635                         num_tcs = 0;
636                 break;
637
638         default:
639                 PMD_DRV_LOG(ERR, "PF work with invalid mode = %d\n",
640                         eth_conf->txmode.mq_mode);
641                 return -1;
642         }
643         msgbuf[TXGBE_VF_TRANS_VLAN] = num_tcs;
644
645         return 0;
646 }
647
648 static int
649 txgbe_set_vf_mc_promisc(struct rte_eth_dev *eth_dev,
650                 uint32_t vf, uint32_t *msgbuf)
651 {
652         struct txgbe_vf_info *vfinfo = *(TXGBE_DEV_VFDATA(eth_dev));
653         struct txgbe_hw *hw = TXGBE_DEV_HW(eth_dev);
654         int xcast_mode = msgbuf[1];     /* msgbuf contains the flag to enable */
655         u32 vmolr, fctrl, disable, enable;
656
657         switch (vfinfo[vf].api_version) {
658         case txgbe_mbox_api_12:
659                 /* promisc introduced in 1.3 version */
660                 if (xcast_mode == TXGBEVF_XCAST_MODE_PROMISC)
661                         return -EOPNOTSUPP;
662                 break;
663                 /* Fall threw */
664         case txgbe_mbox_api_13:
665                 break;
666         default:
667                 return -1;
668         }
669
670         if (vfinfo[vf].xcast_mode == xcast_mode)
671                 goto out;
672
673         switch (xcast_mode) {
674         case TXGBEVF_XCAST_MODE_NONE:
675                 disable = TXGBE_POOLETHCTL_BCA | TXGBE_POOLETHCTL_MCHA |
676                           TXGBE_POOLETHCTL_MCP | TXGBE_POOLETHCTL_UCP |
677                           TXGBE_POOLETHCTL_VLP;
678                 enable = 0;
679                 break;
680         case TXGBEVF_XCAST_MODE_MULTI:
681                 disable = TXGBE_POOLETHCTL_MCP | TXGBE_POOLETHCTL_UCP |
682                           TXGBE_POOLETHCTL_VLP;
683                 enable = TXGBE_POOLETHCTL_BCA | TXGBE_POOLETHCTL_MCHA;
684                 break;
685         case TXGBEVF_XCAST_MODE_ALLMULTI:
686                 disable = TXGBE_POOLETHCTL_UCP | TXGBE_POOLETHCTL_VLP;
687                 enable = TXGBE_POOLETHCTL_BCA | TXGBE_POOLETHCTL_MCHA |
688                          TXGBE_POOLETHCTL_MCP;
689                 break;
690         case TXGBEVF_XCAST_MODE_PROMISC:
691                 fctrl = rd32(hw, TXGBE_PSRCTL);
692                 if (!(fctrl & TXGBE_PSRCTL_UCP)) {
693                         /* VF promisc requires PF in promisc */
694                         PMD_DRV_LOG(ERR,
695                                "Enabling VF promisc requires PF in promisc\n");
696                         return -1;
697                 }
698
699                 disable = 0;
700                 enable = TXGBE_POOLETHCTL_BCA | TXGBE_POOLETHCTL_MCHA |
701                          TXGBE_POOLETHCTL_MCP | TXGBE_POOLETHCTL_UCP |
702                          TXGBE_POOLETHCTL_VLP;
703                 break;
704         default:
705                 return -1;
706         }
707
708         vmolr = rd32(hw, TXGBE_POOLETHCTL(vf));
709         vmolr &= ~disable;
710         vmolr |= enable;
711         wr32(hw, TXGBE_POOLETHCTL(vf), vmolr);
712         vfinfo[vf].xcast_mode = xcast_mode;
713
714 out:
715         msgbuf[1] = xcast_mode;
716
717         return 0;
718 }
719
720 static int
721 txgbe_set_vf_macvlan_msg(struct rte_eth_dev *dev, uint32_t vf, uint32_t *msgbuf)
722 {
723         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
724         struct txgbe_vf_info *vf_info = *(TXGBE_DEV_VFDATA(dev));
725         uint8_t *new_mac = (uint8_t *)(&msgbuf[1]);
726         struct rte_ether_addr *ea = (struct rte_ether_addr *)new_mac;
727         int index = (msgbuf[0] & TXGBE_VT_MSGINFO_MASK) >>
728                     TXGBE_VT_MSGINFO_SHIFT;
729
730         if (index) {
731                 if (!rte_is_valid_assigned_ether_addr(ea)) {
732                         PMD_DRV_LOG(ERR, "set invalid mac vf:%d\n", vf);
733                         return -1;
734                 }
735
736                 vf_info[vf].mac_count++;
737
738                 hw->mac.set_rar(hw, vf_info[vf].mac_count,
739                                 new_mac, vf, true);
740         } else {
741                 if (vf_info[vf].mac_count) {
742                         hw->mac.clear_rar(hw, vf_info[vf].mac_count);
743                         vf_info[vf].mac_count = 0;
744                 }
745         }
746         return 0;
747 }
748
749 static int
750 txgbe_rcv_msg_from_vf(struct rte_eth_dev *eth_dev, uint16_t vf)
751 {
752         uint16_t mbx_size = TXGBE_P2VMBX_SIZE;
753         uint16_t msg_size = TXGBE_VF_MSG_SIZE_DEFAULT;
754         uint32_t msgbuf[TXGBE_P2VMBX_SIZE];
755         int32_t retval;
756         struct txgbe_hw *hw = TXGBE_DEV_HW(eth_dev);
757         struct txgbe_vf_info *vfinfo = *TXGBE_DEV_VFDATA(eth_dev);
758         struct rte_pmd_txgbe_mb_event_param ret_param;
759
760         retval = txgbe_read_mbx(hw, msgbuf, mbx_size, vf);
761         if (retval) {
762                 PMD_DRV_LOG(ERR, "Error mbx recv msg from VF %d", vf);
763                 return retval;
764         }
765
766         /* do nothing with the message already been processed */
767         if (msgbuf[0] & (TXGBE_VT_MSGTYPE_ACK | TXGBE_VT_MSGTYPE_NACK))
768                 return retval;
769
770         /* flush the ack before we write any messages back */
771         txgbe_flush(hw);
772
773         /**
774          * initialise structure to send to user application
775          * will return response from user in retval field
776          */
777         ret_param.retval = RTE_PMD_TXGBE_MB_EVENT_PROCEED;
778         ret_param.vfid = vf;
779         ret_param.msg_type = msgbuf[0] & 0xFFFF;
780         ret_param.msg = (void *)msgbuf;
781
782         /* perform VF reset */
783         if (msgbuf[0] == TXGBE_VF_RESET) {
784                 int ret = txgbe_vf_reset(eth_dev, vf, msgbuf);
785
786                 vfinfo[vf].clear_to_send = true;
787
788                 /* notify application about VF reset */
789                 rte_eth_dev_callback_process(eth_dev, RTE_ETH_EVENT_VF_MBOX,
790                                               &ret_param);
791                 return ret;
792         }
793
794         /**
795          * ask user application if we allowed to perform those functions
796          * if we get ret_param.retval == RTE_PMD_TXGBE_MB_EVENT_PROCEED
797          * then business as usual,
798          * if 0, do nothing and send ACK to VF
799          * if ret_param.retval > 1, do nothing and send NAK to VF
800          */
801         rte_eth_dev_callback_process(eth_dev, RTE_ETH_EVENT_VF_MBOX,
802                                       &ret_param);
803
804         retval = ret_param.retval;
805
806         /* check & process VF to PF mailbox message */
807         switch ((msgbuf[0] & 0xFFFF)) {
808         case TXGBE_VF_SET_MAC_ADDR:
809                 if (retval == RTE_PMD_TXGBE_MB_EVENT_PROCEED)
810                         retval = txgbe_vf_set_mac_addr(eth_dev, vf, msgbuf);
811                 break;
812         case TXGBE_VF_SET_MULTICAST:
813                 if (retval == RTE_PMD_TXGBE_MB_EVENT_PROCEED)
814                         retval = txgbe_vf_set_multicast(eth_dev, vf, msgbuf);
815                 break;
816         case TXGBE_VF_SET_LPE:
817                 if (retval == RTE_PMD_TXGBE_MB_EVENT_PROCEED)
818                         retval = txgbe_set_vf_lpe(eth_dev, vf, msgbuf);
819                 break;
820         case TXGBE_VF_SET_VLAN:
821                 if (retval == RTE_PMD_TXGBE_MB_EVENT_PROCEED)
822                         retval = txgbe_vf_set_vlan(eth_dev, vf, msgbuf);
823                 break;
824         case TXGBE_VF_API_NEGOTIATE:
825                 retval = txgbe_negotiate_vf_api(eth_dev, vf, msgbuf);
826                 break;
827         case TXGBE_VF_GET_QUEUES:
828                 retval = txgbe_get_vf_queues(eth_dev, vf, msgbuf);
829                 msg_size = TXGBE_VF_GET_QUEUE_MSG_SIZE;
830                 break;
831         case TXGBE_VF_UPDATE_XCAST_MODE:
832                 if (retval == RTE_PMD_TXGBE_MB_EVENT_PROCEED)
833                         retval = txgbe_set_vf_mc_promisc(eth_dev, vf, msgbuf);
834                 break;
835         case TXGBE_VF_SET_MACVLAN:
836                 if (retval == RTE_PMD_TXGBE_MB_EVENT_PROCEED)
837                         retval = txgbe_set_vf_macvlan_msg(eth_dev, vf, msgbuf);
838                 break;
839         default:
840                 PMD_DRV_LOG(DEBUG, "Unhandled Msg %8.8x", (uint32_t)msgbuf[0]);
841                 retval = TXGBE_ERR_MBX;
842                 break;
843         }
844
845         /* response the VF according to the message process result */
846         if (retval)
847                 msgbuf[0] |= TXGBE_VT_MSGTYPE_NACK;
848         else
849                 msgbuf[0] |= TXGBE_VT_MSGTYPE_ACK;
850
851         msgbuf[0] |= TXGBE_VT_MSGTYPE_CTS;
852
853         txgbe_write_mbx(hw, msgbuf, msg_size, vf);
854
855         return retval;
856 }
857
858 static inline void
859 txgbe_rcv_ack_from_vf(struct rte_eth_dev *eth_dev, uint16_t vf)
860 {
861         uint32_t msg = TXGBE_VT_MSGTYPE_NACK;
862         struct txgbe_hw *hw = TXGBE_DEV_HW(eth_dev);
863         struct txgbe_vf_info *vfinfo = *TXGBE_DEV_VFDATA(eth_dev);
864
865         if (!vfinfo[vf].clear_to_send)
866                 txgbe_write_mbx(hw, &msg, 1, vf);
867 }
868
869 void txgbe_pf_mbx_process(struct rte_eth_dev *eth_dev)
870 {
871         uint16_t vf;
872         struct txgbe_hw *hw = TXGBE_DEV_HW(eth_dev);
873
874         for (vf = 0; vf < dev_num_vf(eth_dev); vf++) {
875                 /* check & process vf function level reset */
876                 if (!txgbe_check_for_rst(hw, vf))
877                         txgbe_vf_reset_event(eth_dev, vf);
878
879                 /* check & process vf mailbox messages */
880                 if (!txgbe_check_for_msg(hw, vf))
881                         txgbe_rcv_msg_from_vf(eth_dev, vf);
882
883                 /* check & process acks from vf */
884                 if (!txgbe_check_for_ack(hw, vf))
885                         txgbe_rcv_ack_from_vf(eth_dev, vf);
886         }
887 }