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