net/liquidio: add API to configure device
[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 uint64_t
45 lio_hweight64(uint64_t w)
46 {
47         uint64_t res = w - ((w >> 1) & 0x5555555555555555ul);
48
49         res =
50             (res & 0x3333333333333333ul) + ((res >> 2) & 0x3333333333333333ul);
51         res = (res + (res >> 4)) & 0x0F0F0F0F0F0F0F0Ful;
52         res = res + (res >> 8);
53         res = res + (res >> 16);
54
55         return (res + (res >> 32)) & 0x00000000000000FFul;
56 }
57
58 static int lio_dev_configure(struct rte_eth_dev *eth_dev)
59 {
60         struct lio_device *lio_dev = LIO_DEV(eth_dev);
61         uint16_t timeout = LIO_MAX_CMD_TIMEOUT;
62         int retval, num_iqueues, num_oqueues;
63         uint8_t mac[ETHER_ADDR_LEN], i;
64         struct lio_if_cfg_resp *resp;
65         struct lio_soft_command *sc;
66         union lio_if_cfg if_cfg;
67         uint32_t resp_size;
68
69         PMD_INIT_FUNC_TRACE();
70
71         /* Re-configuring firmware not supported.
72          * Can't change tx/rx queues per port from initial value.
73          */
74         if (lio_dev->port_configured) {
75                 if ((lio_dev->nb_rx_queues != eth_dev->data->nb_rx_queues) ||
76                     (lio_dev->nb_tx_queues != eth_dev->data->nb_tx_queues)) {
77                         lio_dev_err(lio_dev,
78                                     "rxq/txq re-conf not supported. Restart application with new value.\n");
79                         return -ENOTSUP;
80                 }
81                 return 0;
82         }
83
84         lio_dev->nb_rx_queues = eth_dev->data->nb_rx_queues;
85         lio_dev->nb_tx_queues = eth_dev->data->nb_tx_queues;
86
87         resp_size = sizeof(struct lio_if_cfg_resp);
88         sc = lio_alloc_soft_command(lio_dev, 0, resp_size, 0);
89         if (sc == NULL)
90                 return -ENOMEM;
91
92         resp = (struct lio_if_cfg_resp *)sc->virtrptr;
93
94         /* Firmware doesn't have capability to reconfigure the queues,
95          * Claim all queues, and use as many required
96          */
97         if_cfg.if_cfg64 = 0;
98         if_cfg.s.num_iqueues = lio_dev->nb_tx_queues;
99         if_cfg.s.num_oqueues = lio_dev->nb_rx_queues;
100         if_cfg.s.base_queue = 0;
101
102         if_cfg.s.gmx_port_id = lio_dev->pf_num;
103
104         lio_prepare_soft_command(lio_dev, sc, LIO_OPCODE,
105                                  LIO_OPCODE_IF_CFG, 0,
106                                  if_cfg.if_cfg64, 0);
107
108         /* Setting wait time in seconds */
109         sc->wait_time = LIO_MAX_CMD_TIMEOUT / 1000;
110
111         retval = lio_send_soft_command(lio_dev, sc);
112         if (retval == LIO_IQ_SEND_FAILED) {
113                 lio_dev_err(lio_dev, "iq/oq config failed status: %x\n",
114                             retval);
115                 /* Soft instr is freed by driver in case of failure. */
116                 goto nic_config_fail;
117         }
118
119         /* Sleep on a wait queue till the cond flag indicates that the
120          * response arrived or timed-out.
121          */
122         while ((*sc->status_word == LIO_COMPLETION_WORD_INIT) && --timeout) {
123                 lio_process_ordered_list(lio_dev);
124                 rte_delay_ms(1);
125         }
126
127         retval = resp->status;
128         if (retval) {
129                 lio_dev_err(lio_dev, "iq/oq config failed\n");
130                 goto nic_config_fail;
131         }
132
133         lio_swap_8B_data((uint64_t *)(&resp->cfg_info),
134                          sizeof(struct octeon_if_cfg_info) >> 3);
135
136         num_iqueues = lio_hweight64(resp->cfg_info.iqmask);
137         num_oqueues = lio_hweight64(resp->cfg_info.oqmask);
138
139         if (!(num_iqueues) || !(num_oqueues)) {
140                 lio_dev_err(lio_dev,
141                             "Got bad iqueues (%016lx) or oqueues (%016lx) from firmware.\n",
142                             (unsigned long)resp->cfg_info.iqmask,
143                             (unsigned long)resp->cfg_info.oqmask);
144                 goto nic_config_fail;
145         }
146
147         lio_dev_dbg(lio_dev,
148                     "interface %d, iqmask %016lx, oqmask %016lx, numiqueues %d, numoqueues %d\n",
149                     eth_dev->data->port_id,
150                     (unsigned long)resp->cfg_info.iqmask,
151                     (unsigned long)resp->cfg_info.oqmask,
152                     num_iqueues, num_oqueues);
153
154         lio_dev->linfo.num_rxpciq = num_oqueues;
155         lio_dev->linfo.num_txpciq = num_iqueues;
156
157         for (i = 0; i < num_oqueues; i++) {
158                 lio_dev->linfo.rxpciq[i].rxpciq64 =
159                     resp->cfg_info.linfo.rxpciq[i].rxpciq64;
160                 lio_dev_dbg(lio_dev, "index %d OQ %d\n",
161                             i, lio_dev->linfo.rxpciq[i].s.q_no);
162         }
163
164         for (i = 0; i < num_iqueues; i++) {
165                 lio_dev->linfo.txpciq[i].txpciq64 =
166                     resp->cfg_info.linfo.txpciq[i].txpciq64;
167                 lio_dev_dbg(lio_dev, "index %d IQ %d\n",
168                             i, lio_dev->linfo.txpciq[i].s.q_no);
169         }
170
171         lio_dev->linfo.hw_addr = resp->cfg_info.linfo.hw_addr;
172         lio_dev->linfo.gmxport = resp->cfg_info.linfo.gmxport;
173         lio_dev->linfo.link.link_status64 =
174                         resp->cfg_info.linfo.link.link_status64;
175
176         /* 64-bit swap required on LE machines */
177         lio_swap_8B_data(&lio_dev->linfo.hw_addr, 1);
178         for (i = 0; i < ETHER_ADDR_LEN; i++)
179                 mac[i] = *((uint8_t *)(((uint8_t *)&lio_dev->linfo.hw_addr) +
180                                        2 + i));
181
182         /* Copy the permanent MAC address */
183         ether_addr_copy((struct ether_addr *)mac, &eth_dev->data->mac_addrs[0]);
184
185         lio_dev->port_configured = 1;
186
187         lio_free_soft_command(sc);
188
189         return 0;
190
191 nic_config_fail:
192         lio_dev_err(lio_dev, "Failed retval %d\n", retval);
193         lio_free_soft_command(sc);
194         lio_free_instr_queue0(lio_dev);
195
196         return -ENODEV;
197 }
198
199 /* Define our ethernet definitions */
200 static const struct eth_dev_ops liovf_eth_dev_ops = {
201         .dev_configure          = lio_dev_configure,
202 };
203
204 static void
205 lio_check_pf_hs_response(void *lio_dev)
206 {
207         struct lio_device *dev = lio_dev;
208
209         /* check till response arrives */
210         if (dev->pfvf_hsword.coproc_tics_per_us)
211                 return;
212
213         cn23xx_vf_handle_mbox(dev);
214
215         rte_eal_alarm_set(1, lio_check_pf_hs_response, lio_dev);
216 }
217
218 /**
219  * \brief Identify the LIO device and to map the BAR address space
220  * @param lio_dev lio device
221  */
222 static int
223 lio_chip_specific_setup(struct lio_device *lio_dev)
224 {
225         struct rte_pci_device *pdev = lio_dev->pci_dev;
226         uint32_t dev_id = pdev->id.device_id;
227         const char *s;
228         int ret = 1;
229
230         switch (dev_id) {
231         case LIO_CN23XX_VF_VID:
232                 lio_dev->chip_id = LIO_CN23XX_VF_VID;
233                 ret = cn23xx_vf_setup_device(lio_dev);
234                 s = "CN23XX VF";
235                 break;
236         default:
237                 s = "?";
238                 lio_dev_err(lio_dev, "Unsupported Chip\n");
239         }
240
241         if (!ret)
242                 lio_dev_info(lio_dev, "DEVICE : %s\n", s);
243
244         return ret;
245 }
246
247 static int
248 lio_first_time_init(struct lio_device *lio_dev,
249                     struct rte_pci_device *pdev)
250 {
251         int dpdk_queues;
252
253         PMD_INIT_FUNC_TRACE();
254
255         /* set dpdk specific pci device pointer */
256         lio_dev->pci_dev = pdev;
257
258         /* Identify the LIO type and set device ops */
259         if (lio_chip_specific_setup(lio_dev)) {
260                 lio_dev_err(lio_dev, "Chip specific setup failed\n");
261                 return -1;
262         }
263
264         /* Initialize soft command buffer pool */
265         if (lio_setup_sc_buffer_pool(lio_dev)) {
266                 lio_dev_err(lio_dev, "sc buffer pool allocation failed\n");
267                 return -1;
268         }
269
270         /* Initialize lists to manage the requests of different types that
271          * arrive from applications for this lio device.
272          */
273         lio_setup_response_list(lio_dev);
274
275         if (lio_dev->fn_list.setup_mbox(lio_dev)) {
276                 lio_dev_err(lio_dev, "Mailbox setup failed\n");
277                 goto error;
278         }
279
280         /* Check PF response */
281         lio_check_pf_hs_response((void *)lio_dev);
282
283         /* Do handshake and exit if incompatible PF driver */
284         if (cn23xx_pfvf_handshake(lio_dev))
285                 goto error;
286
287         /* Initial reset */
288         cn23xx_vf_ask_pf_to_do_flr(lio_dev);
289         /* Wait for FLR for 100ms per SRIOV specification */
290         rte_delay_ms(100);
291
292         if (cn23xx_vf_set_io_queues_off(lio_dev)) {
293                 lio_dev_err(lio_dev, "Setting io queues off failed\n");
294                 goto error;
295         }
296
297         if (lio_dev->fn_list.setup_device_regs(lio_dev)) {
298                 lio_dev_err(lio_dev, "Failed to configure device registers\n");
299                 goto error;
300         }
301
302         if (lio_setup_instr_queue0(lio_dev)) {
303                 lio_dev_err(lio_dev, "Failed to setup instruction queue 0\n");
304                 goto error;
305         }
306
307         dpdk_queues = (int)lio_dev->sriov_info.rings_per_vf;
308
309         lio_dev->max_tx_queues = dpdk_queues;
310         lio_dev->max_rx_queues = dpdk_queues;
311
312         return 0;
313
314 error:
315         lio_free_sc_buffer_pool(lio_dev);
316         if (lio_dev->mbox[0])
317                 lio_dev->fn_list.free_mbox(lio_dev);
318         if (lio_dev->instr_queue[0])
319                 lio_free_instr_queue0(lio_dev);
320
321         return -1;
322 }
323
324 static int
325 lio_eth_dev_uninit(struct rte_eth_dev *eth_dev)
326 {
327         struct lio_device *lio_dev = LIO_DEV(eth_dev);
328
329         PMD_INIT_FUNC_TRACE();
330
331         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
332                 return -EPERM;
333
334         /* lio_free_sc_buffer_pool */
335         lio_free_sc_buffer_pool(lio_dev);
336
337         rte_free(eth_dev->data->mac_addrs);
338         eth_dev->data->mac_addrs = NULL;
339
340         return 0;
341 }
342
343 static int
344 lio_eth_dev_init(struct rte_eth_dev *eth_dev)
345 {
346         struct rte_pci_device *pdev = RTE_DEV_TO_PCI(eth_dev->device);
347         struct lio_device *lio_dev = LIO_DEV(eth_dev);
348
349         PMD_INIT_FUNC_TRACE();
350
351         /* Primary does the initialization. */
352         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
353                 return 0;
354
355         rte_eth_copy_pci_info(eth_dev, pdev);
356         eth_dev->data->dev_flags |= RTE_ETH_DEV_DETACHABLE;
357
358         if (pdev->mem_resource[0].addr) {
359                 lio_dev->hw_addr = pdev->mem_resource[0].addr;
360         } else {
361                 PMD_INIT_LOG(ERR, "ERROR: Failed to map BAR0\n");
362                 return -ENODEV;
363         }
364
365         lio_dev->eth_dev = eth_dev;
366         /* set lio device print string */
367         snprintf(lio_dev->dev_string, sizeof(lio_dev->dev_string),
368                  "%s[%02x:%02x.%x]", pdev->driver->driver.name,
369                  pdev->addr.bus, pdev->addr.devid, pdev->addr.function);
370
371         lio_dev->port_id = eth_dev->data->port_id;
372
373         if (lio_first_time_init(lio_dev, pdev)) {
374                 lio_dev_err(lio_dev, "Device init failed\n");
375                 return -EINVAL;
376         }
377
378         eth_dev->dev_ops = &liovf_eth_dev_ops;
379         eth_dev->data->mac_addrs = rte_zmalloc("lio", ETHER_ADDR_LEN, 0);
380         if (eth_dev->data->mac_addrs == NULL) {
381                 lio_dev_err(lio_dev,
382                             "MAC addresses memory allocation failed\n");
383                 eth_dev->dev_ops = NULL;
384                 return -ENOMEM;
385         }
386
387         rte_atomic64_set(&lio_dev->status, LIO_DEV_RUNNING);
388         rte_wmb();
389
390         lio_dev->port_configured = 0;
391         /* Always allow unicast packets */
392         lio_dev->ifflags |= LIO_IFFLAG_UNICAST;
393
394         return 0;
395 }
396
397 /* Set of PCI devices this driver supports */
398 static const struct rte_pci_id pci_id_liovf_map[] = {
399         { RTE_PCI_DEVICE(PCI_VENDOR_ID_CAVIUM, LIO_CN23XX_VF_VID) },
400         { .vendor_id = 0, /* sentinel */ }
401 };
402
403 static struct eth_driver rte_liovf_pmd = {
404         .pci_drv = {
405                 .id_table       = pci_id_liovf_map,
406                 .drv_flags      = RTE_PCI_DRV_NEED_MAPPING,
407                 .probe          = rte_eth_dev_pci_probe,
408                 .remove         = rte_eth_dev_pci_remove,
409         },
410         .eth_dev_init           = lio_eth_dev_init,
411         .eth_dev_uninit         = lio_eth_dev_uninit,
412         .dev_private_size       = sizeof(struct lio_device),
413 };
414
415 RTE_PMD_REGISTER_PCI(net_liovf, rte_liovf_pmd.pci_drv);
416 RTE_PMD_REGISTER_PCI_TABLE(net_liovf, pci_id_liovf_map);
417 RTE_PMD_REGISTER_KMOD_DEP(net_liovf, "* igb_uio | vfio");