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