4 * Copyright (c) 2013-2017, Wind River Systems, Inc.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
9 * 1) Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
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.
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.
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.
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>
47 #include <rte_ether.h>
48 #include <rte_common.h>
49 #include <rte_cycles.h>
50 #include <rte_byteorder.h>
52 #include <rte_memory.h>
56 #include "rte_avp_common.h"
57 #include "rte_avp_fifo.h"
63 #define AVP_DEV_TO_PCI(eth_dev) RTE_DEV_TO_PCI((eth_dev)->device)
66 #define AVP_MAX_MAC_ADDRS 1
67 #define AVP_MIN_RX_BUFSIZE ETHER_MIN_LEN
71 * Defines the number of microseconds to wait before checking the response
72 * queue for completion.
74 #define AVP_REQUEST_DELAY_USECS (5000)
77 * Defines the number times to check the response queue for completion before
78 * declaring a timeout.
80 #define AVP_MAX_REQUEST_RETRY (100)
82 /* Defines the current PCI driver version number */
83 #define AVP_DPDK_DRIVER_VERSION RTE_AVP_CURRENT_GUEST_VERSION
86 * The set of PCI devices this driver supports
88 static const struct rte_pci_id pci_id_avp_map[] = {
89 { .vendor_id = RTE_AVP_PCI_VENDOR_ID,
90 .device_id = RTE_AVP_PCI_DEVICE_ID,
91 .subsystem_vendor_id = RTE_AVP_PCI_SUB_VENDOR_ID,
92 .subsystem_device_id = RTE_AVP_PCI_SUB_DEVICE_ID,
93 .class_id = RTE_CLASS_ANY_ID,
96 { .vendor_id = 0, /* sentinel */
101 /**@{ AVP device flags */
102 #define AVP_F_PROMISC (1 << 1)
103 #define AVP_F_CONFIGURED (1 << 2)
104 #define AVP_F_LINKUP (1 << 3)
107 /* Ethernet device validation marker */
108 #define AVP_ETHDEV_MAGIC 0x92972862
111 * Defines the AVP device attributes which are attached to an RTE ethernet
115 uint32_t magic; /**< Memory validation marker */
116 uint64_t device_id; /**< Unique system identifier */
117 struct ether_addr ethaddr; /**< Host specified MAC address */
118 struct rte_eth_dev_data *dev_data;
119 /**< Back pointer to ethernet device data */
120 volatile uint32_t flags; /**< Device operational flags */
121 uint8_t port_id; /**< Ethernet port identifier */
122 struct rte_mempool *pool; /**< pkt mbuf mempool */
123 unsigned int guest_mbuf_size; /**< local pool mbuf size */
124 unsigned int host_mbuf_size; /**< host mbuf size */
125 unsigned int max_rx_pkt_len; /**< maximum receive unit */
126 uint32_t host_features; /**< Supported feature bitmap */
127 uint32_t features; /**< Enabled feature bitmap */
128 unsigned int num_tx_queues; /**< Negotiated number of transmit queues */
129 unsigned int max_tx_queues; /**< Maximum number of transmit queues */
130 unsigned int num_rx_queues; /**< Negotiated number of receive queues */
131 unsigned int max_rx_queues; /**< Maximum number of receive queues */
133 struct rte_avp_fifo *tx_q[RTE_AVP_MAX_QUEUES]; /**< TX queue */
134 struct rte_avp_fifo *rx_q[RTE_AVP_MAX_QUEUES]; /**< RX queue */
135 struct rte_avp_fifo *alloc_q[RTE_AVP_MAX_QUEUES];
136 /**< Allocated mbufs queue */
137 struct rte_avp_fifo *free_q[RTE_AVP_MAX_QUEUES];
138 /**< To be freed mbufs queue */
140 /* For request & response */
141 struct rte_avp_fifo *req_q; /**< Request queue */
142 struct rte_avp_fifo *resp_q; /**< Response queue */
143 void *host_sync_addr; /**< (host) Req/Resp Mem address */
144 void *sync_addr; /**< Req/Resp Mem address */
145 void *host_mbuf_addr; /**< (host) MBUF pool start address */
146 void *mbuf_addr; /**< MBUF pool start address */
147 } __rte_cache_aligned;
149 /* RTE ethernet private data */
152 } __rte_cache_aligned;
155 /* 32-bit MMIO register write */
156 #define AVP_WRITE32(_value, _addr) rte_write32_relaxed((_value), (_addr))
158 /* 32-bit MMIO register read */
159 #define AVP_READ32(_addr) rte_read32_relaxed((_addr))
161 /* Macro to cast the ethernet device private data to a AVP object */
162 #define AVP_DEV_PRIVATE_TO_HW(adapter) \
163 (&((struct avp_adapter *)adapter)->avp)
166 * Defines the structure of a AVP device queue for the purpose of handling the
167 * receive and transmit burst callback functions
170 struct rte_eth_dev_data *dev_data;
171 /**< Backpointer to ethernet device data */
172 struct avp_dev *avp; /**< Backpointer to AVP device */
174 /**< Queue identifier used for indexing current queue */
176 /**< Base queue identifier for queue servicing */
177 uint16_t queue_limit;
178 /**< Maximum queue identifier for queue servicing */
185 /* translate from host physical address to guest virtual address */
187 avp_dev_translate_address(struct rte_eth_dev *eth_dev,
188 phys_addr_t host_phys_addr)
190 struct rte_pci_device *pci_dev = AVP_DEV_TO_PCI(eth_dev);
191 struct rte_mem_resource *resource;
192 struct rte_avp_memmap_info *info;
193 struct rte_avp_memmap *map;
198 addr = pci_dev->mem_resource[RTE_AVP_PCI_MEMORY_BAR].addr;
199 resource = &pci_dev->mem_resource[RTE_AVP_PCI_MEMMAP_BAR];
200 info = (struct rte_avp_memmap_info *)resource->addr;
203 for (i = 0; i < info->nb_maps; i++) {
204 /* search all segments looking for a matching address */
205 map = &info->maps[i];
207 if ((host_phys_addr >= map->phys_addr) &&
208 (host_phys_addr < (map->phys_addr + map->length))) {
209 /* address is within this segment */
210 offset += (host_phys_addr - map->phys_addr);
211 addr = RTE_PTR_ADD(addr, offset);
213 PMD_DRV_LOG(DEBUG, "Translating host physical 0x%" PRIx64 " to guest virtual 0x%p\n",
214 host_phys_addr, addr);
218 offset += map->length;
224 /* verify that the incoming device version is compatible with our version */
226 avp_dev_version_check(uint32_t version)
228 uint32_t driver = RTE_AVP_STRIP_MINOR_VERSION(AVP_DPDK_DRIVER_VERSION);
229 uint32_t device = RTE_AVP_STRIP_MINOR_VERSION(version);
231 if (device <= driver) {
232 /* the host driver version is less than or equal to ours */
239 /* verify that memory regions have expected version and validation markers */
241 avp_dev_check_regions(struct rte_eth_dev *eth_dev)
243 struct rte_pci_device *pci_dev = AVP_DEV_TO_PCI(eth_dev);
244 struct rte_avp_memmap_info *memmap;
245 struct rte_avp_device_info *info;
246 struct rte_mem_resource *resource;
249 /* Dump resource info for debug */
250 for (i = 0; i < PCI_MAX_RESOURCE; i++) {
251 resource = &pci_dev->mem_resource[i];
252 if ((resource->phys_addr == 0) || (resource->len == 0))
255 PMD_DRV_LOG(DEBUG, "resource[%u]: phys=0x%" PRIx64 " len=%" PRIu64 " addr=%p\n",
256 i, resource->phys_addr,
257 resource->len, resource->addr);
260 case RTE_AVP_PCI_MEMMAP_BAR:
261 memmap = (struct rte_avp_memmap_info *)resource->addr;
262 if ((memmap->magic != RTE_AVP_MEMMAP_MAGIC) ||
263 (memmap->version != RTE_AVP_MEMMAP_VERSION)) {
264 PMD_DRV_LOG(ERR, "Invalid memmap magic 0x%08x and version %u\n",
265 memmap->magic, memmap->version);
270 case RTE_AVP_PCI_DEVICE_BAR:
271 info = (struct rte_avp_device_info *)resource->addr;
272 if ((info->magic != RTE_AVP_DEVICE_MAGIC) ||
273 avp_dev_version_check(info->version)) {
274 PMD_DRV_LOG(ERR, "Invalid device info magic 0x%08x or version 0x%08x > 0x%08x\n",
275 info->magic, info->version,
276 AVP_DPDK_DRIVER_VERSION);
281 case RTE_AVP_PCI_MEMORY_BAR:
282 case RTE_AVP_PCI_MMIO_BAR:
283 if (resource->addr == NULL) {
284 PMD_DRV_LOG(ERR, "Missing address space for BAR%u\n",
290 case RTE_AVP_PCI_MSIX_BAR:
292 /* no validation required */
301 * create a AVP device using the supplied device info by first translating it
302 * to guest address space(s).
305 avp_dev_create(struct rte_pci_device *pci_dev,
306 struct rte_eth_dev *eth_dev)
308 struct avp_dev *avp = AVP_DEV_PRIVATE_TO_HW(eth_dev->data->dev_private);
309 struct rte_avp_device_info *host_info;
310 struct rte_mem_resource *resource;
313 resource = &pci_dev->mem_resource[RTE_AVP_PCI_DEVICE_BAR];
314 if (resource->addr == NULL) {
315 PMD_DRV_LOG(ERR, "BAR%u is not mapped\n",
316 RTE_AVP_PCI_DEVICE_BAR);
319 host_info = (struct rte_avp_device_info *)resource->addr;
321 if ((host_info->magic != RTE_AVP_DEVICE_MAGIC) ||
322 avp_dev_version_check(host_info->version)) {
323 PMD_DRV_LOG(ERR, "Invalid AVP PCI device, magic 0x%08x version 0x%08x > 0x%08x\n",
324 host_info->magic, host_info->version,
325 AVP_DPDK_DRIVER_VERSION);
329 PMD_DRV_LOG(DEBUG, "AVP host device is v%u.%u.%u\n",
330 RTE_AVP_GET_RELEASE_VERSION(host_info->version),
331 RTE_AVP_GET_MAJOR_VERSION(host_info->version),
332 RTE_AVP_GET_MINOR_VERSION(host_info->version));
334 PMD_DRV_LOG(DEBUG, "AVP host supports %u to %u TX queue(s)\n",
335 host_info->min_tx_queues, host_info->max_tx_queues);
336 PMD_DRV_LOG(DEBUG, "AVP host supports %u to %u RX queue(s)\n",
337 host_info->min_rx_queues, host_info->max_rx_queues);
338 PMD_DRV_LOG(DEBUG, "AVP host supports features 0x%08x\n",
339 host_info->features);
341 if (avp->magic != AVP_ETHDEV_MAGIC) {
343 * First time initialization (i.e., not during a VM
346 memset(avp, 0, sizeof(*avp));
347 avp->magic = AVP_ETHDEV_MAGIC;
348 avp->dev_data = eth_dev->data;
349 avp->port_id = eth_dev->data->port_id;
350 avp->host_mbuf_size = host_info->mbuf_size;
351 avp->host_features = host_info->features;
352 memcpy(&avp->ethaddr.addr_bytes[0],
353 host_info->ethaddr, ETHER_ADDR_LEN);
354 /* adjust max values to not exceed our max */
356 RTE_MIN(host_info->max_tx_queues, RTE_AVP_MAX_QUEUES);
358 RTE_MIN(host_info->max_rx_queues, RTE_AVP_MAX_QUEUES);
360 /* Re-attaching during migration */
362 /* TODO... requires validation of host values */
363 if ((host_info->features & avp->features) != avp->features) {
364 PMD_DRV_LOG(ERR, "AVP host features mismatched; 0x%08x, host=0x%08x\n",
365 avp->features, host_info->features);
366 /* this should not be possible; continue for now */
370 /* the device id is allowed to change over migrations */
371 avp->device_id = host_info->device_id;
373 /* translate incoming host addresses to guest address space */
374 PMD_DRV_LOG(DEBUG, "AVP first host tx queue at 0x%" PRIx64 "\n",
376 PMD_DRV_LOG(DEBUG, "AVP first host alloc queue at 0x%" PRIx64 "\n",
377 host_info->alloc_phys);
378 for (i = 0; i < avp->max_tx_queues; i++) {
379 avp->tx_q[i] = avp_dev_translate_address(eth_dev,
380 host_info->tx_phys + (i * host_info->tx_size));
382 avp->alloc_q[i] = avp_dev_translate_address(eth_dev,
383 host_info->alloc_phys + (i * host_info->alloc_size));
386 PMD_DRV_LOG(DEBUG, "AVP first host rx queue at 0x%" PRIx64 "\n",
388 PMD_DRV_LOG(DEBUG, "AVP first host free queue at 0x%" PRIx64 "\n",
389 host_info->free_phys);
390 for (i = 0; i < avp->max_rx_queues; i++) {
391 avp->rx_q[i] = avp_dev_translate_address(eth_dev,
392 host_info->rx_phys + (i * host_info->rx_size));
393 avp->free_q[i] = avp_dev_translate_address(eth_dev,
394 host_info->free_phys + (i * host_info->free_size));
397 PMD_DRV_LOG(DEBUG, "AVP host request queue at 0x%" PRIx64 "\n",
398 host_info->req_phys);
399 PMD_DRV_LOG(DEBUG, "AVP host response queue at 0x%" PRIx64 "\n",
400 host_info->resp_phys);
401 PMD_DRV_LOG(DEBUG, "AVP host sync address at 0x%" PRIx64 "\n",
402 host_info->sync_phys);
403 PMD_DRV_LOG(DEBUG, "AVP host mbuf address at 0x%" PRIx64 "\n",
404 host_info->mbuf_phys);
405 avp->req_q = avp_dev_translate_address(eth_dev, host_info->req_phys);
406 avp->resp_q = avp_dev_translate_address(eth_dev, host_info->resp_phys);
408 avp_dev_translate_address(eth_dev, host_info->sync_phys);
410 avp_dev_translate_address(eth_dev, host_info->mbuf_phys);
413 * store the host mbuf virtual address so that we can calculate
414 * relative offsets for each mbuf as they are processed
416 avp->host_mbuf_addr = host_info->mbuf_va;
417 avp->host_sync_addr = host_info->sync_va;
420 * store the maximum packet length that is supported by the host.
422 avp->max_rx_pkt_len = host_info->max_rx_pkt_len;
423 PMD_DRV_LOG(DEBUG, "AVP host max receive packet length is %u\n",
424 host_info->max_rx_pkt_len);
430 * This function is based on probe() function in avp_pci.c
431 * It returns 0 on success.
434 eth_avp_dev_init(struct rte_eth_dev *eth_dev)
436 struct avp_dev *avp =
437 AVP_DEV_PRIVATE_TO_HW(eth_dev->data->dev_private);
438 struct rte_pci_device *pci_dev;
441 pci_dev = AVP_DEV_TO_PCI(eth_dev);
443 if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
445 * no setup required on secondary processes. All data is saved
446 * in dev_private by the primary process. All resource should
447 * be mapped to the same virtual address so all pointers should
453 rte_eth_copy_pci_info(eth_dev, pci_dev);
455 eth_dev->data->dev_flags |= RTE_ETH_DEV_DETACHABLE;
457 /* Check BAR resources */
458 ret = avp_dev_check_regions(eth_dev);
460 PMD_DRV_LOG(ERR, "Failed to validate BAR resources, ret=%d\n",
465 /* Handle each subtype */
466 ret = avp_dev_create(pci_dev, eth_dev);
468 PMD_DRV_LOG(ERR, "Failed to create device, ret=%d\n", ret);
472 /* Allocate memory for storing MAC addresses */
473 eth_dev->data->mac_addrs = rte_zmalloc("avp_ethdev", ETHER_ADDR_LEN, 0);
474 if (eth_dev->data->mac_addrs == NULL) {
475 PMD_DRV_LOG(ERR, "Failed to allocate %d bytes needed to store MAC addresses\n",
480 /* Get a mac from device config */
481 ether_addr_copy(&avp->ethaddr, ð_dev->data->mac_addrs[0]);
487 eth_avp_dev_uninit(struct rte_eth_dev *eth_dev)
489 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
492 if (eth_dev->data == NULL)
495 if (eth_dev->data->mac_addrs != NULL) {
496 rte_free(eth_dev->data->mac_addrs);
497 eth_dev->data->mac_addrs = NULL;
504 static struct eth_driver rte_avp_pmd = {
506 .id_table = pci_id_avp_map,
507 .drv_flags = RTE_PCI_DRV_NEED_MAPPING,
508 .probe = rte_eth_dev_pci_probe,
509 .remove = rte_eth_dev_pci_remove,
511 .eth_dev_init = eth_avp_dev_init,
512 .eth_dev_uninit = eth_avp_dev_uninit,
513 .dev_private_size = sizeof(struct avp_adapter),
516 RTE_PMD_REGISTER_PCI(net_avp, rte_avp_pmd.pci_drv);
517 RTE_PMD_REGISTER_PCI_TABLE(net_avp, pci_id_avp_map);