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