net/txgbe: add VF base code
[dpdk.git] / drivers / net / txgbe / txgbe_ethdev_vf.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2015-2020
3  */
4
5 #include <sys/queue.h>
6 #include <stdio.h>
7 #include <errno.h>
8 #include <stdint.h>
9 #include <string.h>
10 #include <rte_log.h>
11 #include <ethdev_pci.h>
12
13 #include "txgbe_logs.h"
14 #include "base/txgbe.h"
15 #include "txgbe_ethdev.h"
16 #include "txgbe_rxtx.h"
17
18 static int txgbevf_dev_close(struct rte_eth_dev *dev);
19 static void txgbevf_intr_disable(struct rte_eth_dev *dev);
20 static void txgbevf_intr_enable(struct rte_eth_dev *dev);
21
22 /*
23  * The set of PCI devices this driver supports (for VF)
24  */
25 static const struct rte_pci_id pci_id_txgbevf_map[] = {
26         { RTE_PCI_DEVICE(PCI_VENDOR_ID_WANGXUN, TXGBE_DEV_ID_RAPTOR_VF) },
27         { RTE_PCI_DEVICE(PCI_VENDOR_ID_WANGXUN, TXGBE_DEV_ID_RAPTOR_VF_HV) },
28         { .vendor_id = 0, /* sentinel */ },
29 };
30
31 static const struct eth_dev_ops txgbevf_eth_dev_ops;
32
33 /*
34  * Negotiate mailbox API version with the PF.
35  * After reset API version is always set to the basic one (txgbe_mbox_api_10).
36  * Then we try to negotiate starting with the most recent one.
37  * If all negotiation attempts fail, then we will proceed with
38  * the default one (txgbe_mbox_api_10).
39  */
40 static void
41 txgbevf_negotiate_api(struct txgbe_hw *hw)
42 {
43         int32_t i;
44
45         /* start with highest supported, proceed down */
46         static const int sup_ver[] = {
47                 txgbe_mbox_api_13,
48                 txgbe_mbox_api_12,
49                 txgbe_mbox_api_11,
50                 txgbe_mbox_api_10,
51         };
52
53         for (i = 0; i < ARRAY_SIZE(sup_ver); i++) {
54                 if (txgbevf_negotiate_api_version(hw, sup_ver[i]) == 0)
55                         break;
56         }
57 }
58
59 /*
60  * Virtual Function device init
61  */
62 static int
63 eth_txgbevf_dev_init(struct rte_eth_dev *eth_dev)
64 {
65         int err;
66         uint32_t tc, tcs;
67         struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(eth_dev);
68         struct txgbe_hw *hw = TXGBE_DEV_HW(eth_dev);
69
70         PMD_INIT_FUNC_TRACE();
71
72         eth_dev->dev_ops = &txgbevf_eth_dev_ops;
73
74         /* for secondary processes, we don't initialise any further as primary
75          * has already done this work. Only check we don't need a different
76          * RX function
77          */
78         if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
79                 struct txgbe_tx_queue *txq;
80                 uint16_t nb_tx_queues = eth_dev->data->nb_tx_queues;
81                 /* TX queue function in primary, set by last queue initialized
82                  * Tx queue may not initialized by primary process
83                  */
84                 if (eth_dev->data->tx_queues) {
85                         txq = eth_dev->data->tx_queues[nb_tx_queues - 1];
86                         txgbe_set_tx_function(eth_dev, txq);
87                 } else {
88                         /* Use default TX function if we get here */
89                         PMD_INIT_LOG(NOTICE,
90                                      "No TX queues configured yet. Using default TX function.");
91                 }
92
93                 txgbe_set_rx_function(eth_dev);
94
95                 return 0;
96         }
97
98         rte_eth_copy_pci_info(eth_dev, pci_dev);
99
100         hw->device_id = pci_dev->id.device_id;
101         hw->vendor_id = pci_dev->id.vendor_id;
102         hw->subsystem_device_id = pci_dev->id.subsystem_device_id;
103         hw->subsystem_vendor_id = pci_dev->id.subsystem_vendor_id;
104         hw->hw_addr = (void *)pci_dev->mem_resource[0].addr;
105
106         /* Initialize the shared code (base driver) */
107         err = txgbe_init_shared_code(hw);
108         if (err != 0) {
109                 PMD_INIT_LOG(ERR,
110                         "Shared code init failed for txgbevf: %d", err);
111                 return -EIO;
112         }
113
114         /* init_mailbox_params */
115         hw->mbx.init_params(hw);
116
117         /* Disable the interrupts for VF */
118         txgbevf_intr_disable(eth_dev);
119
120         hw->mac.num_rar_entries = 128; /* The MAX of the underlying PF */
121         err = hw->mac.reset_hw(hw);
122
123         /*
124          * The VF reset operation returns the TXGBE_ERR_INVALID_MAC_ADDR when
125          * the underlying PF driver has not assigned a MAC address to the VF.
126          * In this case, assign a random MAC address.
127          */
128         if (err != 0 && err != TXGBE_ERR_INVALID_MAC_ADDR) {
129                 PMD_INIT_LOG(ERR, "VF Initialization Failure: %d", err);
130                 /*
131                  * This error code will be propagated to the app by
132                  * rte_eth_dev_reset, so use a public error code rather than
133                  * the internal-only TXGBE_ERR_RESET_FAILED
134                  */
135                 return -EAGAIN;
136         }
137
138         /* negotiate mailbox API version to use with the PF. */
139         txgbevf_negotiate_api(hw);
140
141         /* Get Rx/Tx queue count via mailbox, which is ready after reset_hw */
142         txgbevf_get_queues(hw, &tcs, &tc);
143
144         txgbevf_intr_enable(eth_dev);
145
146         return 0;
147 }
148
149 /* Virtual Function device uninit */
150 static int
151 eth_txgbevf_dev_uninit(struct rte_eth_dev *eth_dev)
152 {
153         PMD_INIT_FUNC_TRACE();
154
155         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
156                 return 0;
157
158         txgbevf_dev_close(eth_dev);
159
160         return 0;
161 }
162
163 static int eth_txgbevf_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
164         struct rte_pci_device *pci_dev)
165 {
166         return rte_eth_dev_pci_generic_probe(pci_dev,
167                 sizeof(struct txgbe_adapter), eth_txgbevf_dev_init);
168 }
169
170 static int eth_txgbevf_pci_remove(struct rte_pci_device *pci_dev)
171 {
172         return rte_eth_dev_pci_generic_remove(pci_dev, eth_txgbevf_dev_uninit);
173 }
174
175 /*
176  * virtual function driver struct
177  */
178 static struct rte_pci_driver rte_txgbevf_pmd = {
179         .id_table = pci_id_txgbevf_map,
180         .drv_flags = RTE_PCI_DRV_NEED_MAPPING,
181         .probe = eth_txgbevf_pci_probe,
182         .remove = eth_txgbevf_pci_remove,
183 };
184
185 /*
186  * Virtual Function operations
187  */
188 static void
189 txgbevf_intr_disable(struct rte_eth_dev *dev)
190 {
191         struct txgbe_interrupt *intr = TXGBE_DEV_INTR(dev);
192         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
193
194         PMD_INIT_FUNC_TRACE();
195
196         /* Clear interrupt mask to stop from interrupts being generated */
197         wr32(hw, TXGBE_VFIMS, TXGBE_VFIMS_MASK);
198
199         txgbe_flush(hw);
200
201         /* Clear mask value. */
202         intr->mask_misc = TXGBE_VFIMS_MASK;
203 }
204
205 static void
206 txgbevf_intr_enable(struct rte_eth_dev *dev)
207 {
208         struct txgbe_interrupt *intr = TXGBE_DEV_INTR(dev);
209         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
210
211         PMD_INIT_FUNC_TRACE();
212
213         /* VF enable interrupt autoclean */
214         wr32(hw, TXGBE_VFIMC, TXGBE_VFIMC_MASK);
215
216         txgbe_flush(hw);
217
218         intr->mask_misc = 0;
219 }
220
221 static int
222 txgbevf_dev_close(struct rte_eth_dev *dev)
223 {
224         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
225         PMD_INIT_FUNC_TRACE();
226         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
227                 return 0;
228
229         hw->mac.reset_hw(hw);
230
231         txgbe_dev_free_queues(dev);
232
233         /* Disable the interrupts for VF */
234         txgbevf_intr_disable(dev);
235
236         return 0;
237 }
238
239 /*
240  * dev_ops for virtual function, bare necessities for basic vf
241  * operation have been implemented
242  */
243 static const struct eth_dev_ops txgbevf_eth_dev_ops = {
244 };
245
246 RTE_PMD_REGISTER_PCI(net_txgbe_vf, rte_txgbevf_pmd);
247 RTE_PMD_REGISTER_PCI_TABLE(net_txgbe_vf, pci_id_txgbevf_map);
248 RTE_PMD_REGISTER_KMOD_DEP(net_txgbe_vf, "* igb_uio | vfio-pci");