net/liquidio: add API for VF FLR
[dpdk.git] / drivers / net / liquidio / lio_ethdev.c
1 /*
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2017 Cavium, Inc.. 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 Cavium, Inc. 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(S) 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 <rte_ethdev.h>
35 #include <rte_cycles.h>
36 #include <rte_malloc.h>
37 #include <rte_alarm.h>
38
39 #include "lio_logs.h"
40 #include "lio_23xx_vf.h"
41 #include "lio_ethdev.h"
42
43 static void
44 lio_check_pf_hs_response(void *lio_dev)
45 {
46         struct lio_device *dev = lio_dev;
47
48         /* check till response arrives */
49         if (dev->pfvf_hsword.coproc_tics_per_us)
50                 return;
51
52         cn23xx_vf_handle_mbox(dev);
53
54         rte_eal_alarm_set(1, lio_check_pf_hs_response, lio_dev);
55 }
56
57 /**
58  * \brief Identify the LIO device and to map the BAR address space
59  * @param lio_dev lio device
60  */
61 static int
62 lio_chip_specific_setup(struct lio_device *lio_dev)
63 {
64         struct rte_pci_device *pdev = lio_dev->pci_dev;
65         uint32_t dev_id = pdev->id.device_id;
66         const char *s;
67         int ret = 1;
68
69         switch (dev_id) {
70         case LIO_CN23XX_VF_VID:
71                 lio_dev->chip_id = LIO_CN23XX_VF_VID;
72                 ret = cn23xx_vf_setup_device(lio_dev);
73                 s = "CN23XX VF";
74                 break;
75         default:
76                 s = "?";
77                 lio_dev_err(lio_dev, "Unsupported Chip\n");
78         }
79
80         if (!ret)
81                 lio_dev_info(lio_dev, "DEVICE : %s\n", s);
82
83         return ret;
84 }
85
86 static int
87 lio_first_time_init(struct lio_device *lio_dev,
88                     struct rte_pci_device *pdev)
89 {
90         int dpdk_queues;
91
92         PMD_INIT_FUNC_TRACE();
93
94         /* set dpdk specific pci device pointer */
95         lio_dev->pci_dev = pdev;
96
97         /* Identify the LIO type and set device ops */
98         if (lio_chip_specific_setup(lio_dev)) {
99                 lio_dev_err(lio_dev, "Chip specific setup failed\n");
100                 return -1;
101         }
102
103         if (lio_dev->fn_list.setup_mbox(lio_dev)) {
104                 lio_dev_err(lio_dev, "Mailbox setup failed\n");
105                 goto error;
106         }
107
108         /* Check PF response */
109         lio_check_pf_hs_response((void *)lio_dev);
110
111         /* Do handshake and exit if incompatible PF driver */
112         if (cn23xx_pfvf_handshake(lio_dev))
113                 goto error;
114
115         /* Initial reset */
116         cn23xx_vf_ask_pf_to_do_flr(lio_dev);
117         /* Wait for FLR for 100ms per SRIOV specification */
118         rte_delay_ms(100);
119
120         if (cn23xx_vf_set_io_queues_off(lio_dev)) {
121                 lio_dev_err(lio_dev, "Setting io queues off failed\n");
122                 goto error;
123         }
124
125         if (lio_dev->fn_list.setup_device_regs(lio_dev)) {
126                 lio_dev_err(lio_dev, "Failed to configure device registers\n");
127                 goto error;
128         }
129
130         dpdk_queues = (int)lio_dev->sriov_info.rings_per_vf;
131
132         lio_dev->max_tx_queues = dpdk_queues;
133         lio_dev->max_rx_queues = dpdk_queues;
134
135         return 0;
136
137 error:
138         if (lio_dev->mbox[0])
139                 lio_dev->fn_list.free_mbox(lio_dev);
140
141         return -1;
142 }
143
144 static int
145 lio_eth_dev_uninit(struct rte_eth_dev *eth_dev)
146 {
147         PMD_INIT_FUNC_TRACE();
148
149         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
150                 return -EPERM;
151
152         rte_free(eth_dev->data->mac_addrs);
153         eth_dev->data->mac_addrs = NULL;
154
155         return 0;
156 }
157
158 static int
159 lio_eth_dev_init(struct rte_eth_dev *eth_dev)
160 {
161         struct rte_pci_device *pdev = RTE_DEV_TO_PCI(eth_dev->device);
162         struct lio_device *lio_dev = LIO_DEV(eth_dev);
163
164         PMD_INIT_FUNC_TRACE();
165
166         /* Primary does the initialization. */
167         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
168                 return 0;
169
170         rte_eth_copy_pci_info(eth_dev, pdev);
171         eth_dev->data->dev_flags |= RTE_ETH_DEV_DETACHABLE;
172
173         if (pdev->mem_resource[0].addr) {
174                 lio_dev->hw_addr = pdev->mem_resource[0].addr;
175         } else {
176                 PMD_INIT_LOG(ERR, "ERROR: Failed to map BAR0\n");
177                 return -ENODEV;
178         }
179
180         lio_dev->eth_dev = eth_dev;
181         /* set lio device print string */
182         snprintf(lio_dev->dev_string, sizeof(lio_dev->dev_string),
183                  "%s[%02x:%02x.%x]", pdev->driver->driver.name,
184                  pdev->addr.bus, pdev->addr.devid, pdev->addr.function);
185
186         lio_dev->port_id = eth_dev->data->port_id;
187
188         if (lio_first_time_init(lio_dev, pdev)) {
189                 lio_dev_err(lio_dev, "Device init failed\n");
190                 return -EINVAL;
191         }
192
193         eth_dev->data->mac_addrs = rte_zmalloc("lio", ETHER_ADDR_LEN, 0);
194         if (eth_dev->data->mac_addrs == NULL) {
195                 lio_dev_err(lio_dev,
196                             "MAC addresses memory allocation failed\n");
197                 return -ENOMEM;
198         }
199
200         return 0;
201 }
202
203 /* Set of PCI devices this driver supports */
204 static const struct rte_pci_id pci_id_liovf_map[] = {
205         { RTE_PCI_DEVICE(PCI_VENDOR_ID_CAVIUM, LIO_CN23XX_VF_VID) },
206         { .vendor_id = 0, /* sentinel */ }
207 };
208
209 static struct eth_driver rte_liovf_pmd = {
210         .pci_drv = {
211                 .id_table       = pci_id_liovf_map,
212                 .drv_flags      = RTE_PCI_DRV_NEED_MAPPING,
213                 .probe          = rte_eth_dev_pci_probe,
214                 .remove         = rte_eth_dev_pci_remove,
215         },
216         .eth_dev_init           = lio_eth_dev_init,
217         .eth_dev_uninit         = lio_eth_dev_uninit,
218         .dev_private_size       = sizeof(struct lio_device),
219 };
220
221 RTE_PMD_REGISTER_PCI(net_liovf, rte_liovf_pmd.pci_drv);
222 RTE_PMD_REGISTER_PCI_TABLE(net_liovf, pci_id_liovf_map);
223 RTE_PMD_REGISTER_KMOD_DEP(net_liovf, "* igb_uio | vfio");