net/avp: support driver registration
[dpdk.git] / drivers / net / avp / avp_ethdev.c
1 /*
2  *   BSD LICENSE
3  *
4  * Copyright (c) 2013-2017, Wind River Systems, Inc.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * 1) Redistributions of source code must retain the above copyright notice,
10  * this list of conditions and the following disclaimer.
11  *
12  * 2) Redistributions in binary form must reproduce the above copyright notice,
13  * this list of conditions and the following disclaimer in the documentation
14  * and/or other materials provided with the distribution.
15  *
16  * 3) Neither the name of Wind River Systems nor the names of its contributors
17  * may be used to endorse or promote products derived from this software
18  * without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
24  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32
33 #include <stdint.h>
34 #include <string.h>
35 #include <stdio.h>
36 #include <errno.h>
37 #include <unistd.h>
38
39 #include <rte_ethdev.h>
40 #include <rte_memcpy.h>
41 #include <rte_string_fns.h>
42 #include <rte_memzone.h>
43 #include <rte_malloc.h>
44 #include <rte_atomic.h>
45 #include <rte_branch_prediction.h>
46 #include <rte_pci.h>
47 #include <rte_ether.h>
48 #include <rte_common.h>
49 #include <rte_cycles.h>
50 #include <rte_byteorder.h>
51 #include <rte_dev.h>
52 #include <rte_memory.h>
53 #include <rte_eal.h>
54
55 #include "rte_avp_common.h"
56 #include "rte_avp_fifo.h"
57
58 #include "avp_logs.h"
59
60
61
62 #define AVP_DEV_TO_PCI(eth_dev) RTE_DEV_TO_PCI((eth_dev)->device)
63
64
65 #define AVP_MAX_MAC_ADDRS 1
66 #define AVP_MIN_RX_BUFSIZE ETHER_MIN_LEN
67
68
69 /*
70  * Defines the number of microseconds to wait before checking the response
71  * queue for completion.
72  */
73 #define AVP_REQUEST_DELAY_USECS (5000)
74
75 /*
76  * Defines the number times to check the response queue for completion before
77  * declaring a timeout.
78  */
79 #define AVP_MAX_REQUEST_RETRY (100)
80
81 /* Defines the current PCI driver version number */
82 #define AVP_DPDK_DRIVER_VERSION RTE_AVP_CURRENT_GUEST_VERSION
83
84 /*
85  * The set of PCI devices this driver supports
86  */
87 static const struct rte_pci_id pci_id_avp_map[] = {
88         { .vendor_id = RTE_AVP_PCI_VENDOR_ID,
89           .device_id = RTE_AVP_PCI_DEVICE_ID,
90           .subsystem_vendor_id = RTE_AVP_PCI_SUB_VENDOR_ID,
91           .subsystem_device_id = RTE_AVP_PCI_SUB_DEVICE_ID,
92           .class_id = RTE_CLASS_ANY_ID,
93         },
94
95         { .vendor_id = 0, /* sentinel */
96         },
97 };
98
99
100 /*
101  * Defines the AVP device attributes which are attached to an RTE ethernet
102  * device
103  */
104 struct avp_dev {
105         uint32_t magic; /**< Memory validation marker */
106         uint64_t device_id; /**< Unique system identifier */
107         struct ether_addr ethaddr; /**< Host specified MAC address */
108         struct rte_eth_dev_data *dev_data;
109         /**< Back pointer to ethernet device data */
110         volatile uint32_t flags; /**< Device operational flags */
111         uint8_t port_id; /**< Ethernet port identifier */
112         struct rte_mempool *pool; /**< pkt mbuf mempool */
113         unsigned int guest_mbuf_size; /**< local pool mbuf size */
114         unsigned int host_mbuf_size; /**< host mbuf size */
115         unsigned int max_rx_pkt_len; /**< maximum receive unit */
116         uint32_t host_features; /**< Supported feature bitmap */
117         uint32_t features; /**< Enabled feature bitmap */
118         unsigned int num_tx_queues; /**< Negotiated number of transmit queues */
119         unsigned int max_tx_queues; /**< Maximum number of transmit queues */
120         unsigned int num_rx_queues; /**< Negotiated number of receive queues */
121         unsigned int max_rx_queues; /**< Maximum number of receive queues */
122
123         struct rte_avp_fifo *tx_q[RTE_AVP_MAX_QUEUES]; /**< TX queue */
124         struct rte_avp_fifo *rx_q[RTE_AVP_MAX_QUEUES]; /**< RX queue */
125         struct rte_avp_fifo *alloc_q[RTE_AVP_MAX_QUEUES];
126         /**< Allocated mbufs queue */
127         struct rte_avp_fifo *free_q[RTE_AVP_MAX_QUEUES];
128         /**< To be freed mbufs queue */
129
130         /* For request & response */
131         struct rte_avp_fifo *req_q; /**< Request queue */
132         struct rte_avp_fifo *resp_q; /**< Response queue */
133         void *host_sync_addr; /**< (host) Req/Resp Mem address */
134         void *sync_addr; /**< Req/Resp Mem address */
135         void *host_mbuf_addr; /**< (host) MBUF pool start address */
136         void *mbuf_addr; /**< MBUF pool start address */
137 } __rte_cache_aligned;
138
139 /* RTE ethernet private data */
140 struct avp_adapter {
141         struct avp_dev avp;
142 } __rte_cache_aligned;
143
144 /* Macro to cast the ethernet device private data to a AVP object */
145 #define AVP_DEV_PRIVATE_TO_HW(adapter) \
146         (&((struct avp_adapter *)adapter)->avp)
147
148 /*
149  * This function is based on probe() function in avp_pci.c
150  * It returns 0 on success.
151  */
152 static int
153 eth_avp_dev_init(struct rte_eth_dev *eth_dev)
154 {
155         struct rte_pci_device *pci_dev;
156
157         pci_dev = AVP_DEV_TO_PCI(eth_dev);
158
159         if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
160                 /*
161                  * no setup required on secondary processes.  All data is saved
162                  * in dev_private by the primary process. All resource should
163                  * be mapped to the same virtual address so all pointers should
164                  * be valid.
165                  */
166                 return 0;
167         }
168
169         rte_eth_copy_pci_info(eth_dev, pci_dev);
170
171         eth_dev->data->dev_flags |= RTE_ETH_DEV_DETACHABLE;
172
173         return 0;
174 }
175
176 static int
177 eth_avp_dev_uninit(struct rte_eth_dev *eth_dev)
178 {
179         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
180                 return -EPERM;
181
182         if (eth_dev->data == NULL)
183                 return 0;
184
185         return 0;
186 }
187
188
189 static struct eth_driver rte_avp_pmd = {
190         {
191                 .id_table = pci_id_avp_map,
192                 .drv_flags = RTE_PCI_DRV_NEED_MAPPING,
193                 .probe = rte_eth_dev_pci_probe,
194                 .remove = rte_eth_dev_pci_remove,
195         },
196         .eth_dev_init = eth_avp_dev_init,
197         .eth_dev_uninit = eth_avp_dev_uninit,
198         .dev_private_size = sizeof(struct avp_adapter),
199 };
200
201 RTE_PMD_REGISTER_PCI(net_avp, rte_avp_pmd.pci_drv);
202 RTE_PMD_REGISTER_PCI_TABLE(net_avp, pci_id_avp_map);