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