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 static int avp_dev_configure(struct rte_eth_dev *dev);
64 static void avp_dev_info_get(struct rte_eth_dev *dev,
65 struct rte_eth_dev_info *dev_info);
66 static void avp_vlan_offload_set(struct rte_eth_dev *dev, int mask);
67 static int avp_dev_link_update(struct rte_eth_dev *dev,
68 __rte_unused int wait_to_complete);
69 static int avp_dev_rx_queue_setup(struct rte_eth_dev *dev,
72 unsigned int socket_id,
73 const struct rte_eth_rxconf *rx_conf,
74 struct rte_mempool *pool);
76 static int avp_dev_tx_queue_setup(struct rte_eth_dev *dev,
79 unsigned int socket_id,
80 const struct rte_eth_txconf *tx_conf);
82 static uint16_t avp_recv_scattered_pkts(void *rx_queue,
83 struct rte_mbuf **rx_pkts,
86 static uint16_t avp_recv_pkts(void *rx_queue,
87 struct rte_mbuf **rx_pkts,
90 static uint16_t avp_xmit_scattered_pkts(void *tx_queue,
91 struct rte_mbuf **tx_pkts,
94 static uint16_t avp_xmit_pkts(void *tx_queue,
95 struct rte_mbuf **tx_pkts,
98 static void avp_dev_rx_queue_release(void *rxq);
99 static void avp_dev_tx_queue_release(void *txq);
102 #define AVP_DEV_TO_PCI(eth_dev) RTE_DEV_TO_PCI((eth_dev)->device)
105 #define AVP_MAX_RX_BURST 64
106 #define AVP_MAX_TX_BURST 64
107 #define AVP_MAX_MAC_ADDRS 1
108 #define AVP_MIN_RX_BUFSIZE ETHER_MIN_LEN
112 * Defines the number of microseconds to wait before checking the response
113 * queue for completion.
115 #define AVP_REQUEST_DELAY_USECS (5000)
118 * Defines the number times to check the response queue for completion before
119 * declaring a timeout.
121 #define AVP_MAX_REQUEST_RETRY (100)
123 /* Defines the current PCI driver version number */
124 #define AVP_DPDK_DRIVER_VERSION RTE_AVP_CURRENT_GUEST_VERSION
127 * The set of PCI devices this driver supports
129 static const struct rte_pci_id pci_id_avp_map[] = {
130 { .vendor_id = RTE_AVP_PCI_VENDOR_ID,
131 .device_id = RTE_AVP_PCI_DEVICE_ID,
132 .subsystem_vendor_id = RTE_AVP_PCI_SUB_VENDOR_ID,
133 .subsystem_device_id = RTE_AVP_PCI_SUB_DEVICE_ID,
134 .class_id = RTE_CLASS_ANY_ID,
137 { .vendor_id = 0, /* sentinel */
142 * dev_ops for avp, bare necessities for basic operation
144 static const struct eth_dev_ops avp_eth_dev_ops = {
145 .dev_configure = avp_dev_configure,
146 .dev_infos_get = avp_dev_info_get,
147 .vlan_offload_set = avp_vlan_offload_set,
148 .link_update = avp_dev_link_update,
149 .rx_queue_setup = avp_dev_rx_queue_setup,
150 .rx_queue_release = avp_dev_rx_queue_release,
151 .tx_queue_setup = avp_dev_tx_queue_setup,
152 .tx_queue_release = avp_dev_tx_queue_release,
155 /**@{ AVP device flags */
156 #define AVP_F_PROMISC (1 << 1)
157 #define AVP_F_CONFIGURED (1 << 2)
158 #define AVP_F_LINKUP (1 << 3)
161 /* Ethernet device validation marker */
162 #define AVP_ETHDEV_MAGIC 0x92972862
165 * Defines the AVP device attributes which are attached to an RTE ethernet
169 uint32_t magic; /**< Memory validation marker */
170 uint64_t device_id; /**< Unique system identifier */
171 struct ether_addr ethaddr; /**< Host specified MAC address */
172 struct rte_eth_dev_data *dev_data;
173 /**< Back pointer to ethernet device data */
174 volatile uint32_t flags; /**< Device operational flags */
175 uint8_t port_id; /**< Ethernet port identifier */
176 struct rte_mempool *pool; /**< pkt mbuf mempool */
177 unsigned int guest_mbuf_size; /**< local pool mbuf size */
178 unsigned int host_mbuf_size; /**< host mbuf size */
179 unsigned int max_rx_pkt_len; /**< maximum receive unit */
180 uint32_t host_features; /**< Supported feature bitmap */
181 uint32_t features; /**< Enabled feature bitmap */
182 unsigned int num_tx_queues; /**< Negotiated number of transmit queues */
183 unsigned int max_tx_queues; /**< Maximum number of transmit queues */
184 unsigned int num_rx_queues; /**< Negotiated number of receive queues */
185 unsigned int max_rx_queues; /**< Maximum number of receive queues */
187 struct rte_avp_fifo *tx_q[RTE_AVP_MAX_QUEUES]; /**< TX queue */
188 struct rte_avp_fifo *rx_q[RTE_AVP_MAX_QUEUES]; /**< RX queue */
189 struct rte_avp_fifo *alloc_q[RTE_AVP_MAX_QUEUES];
190 /**< Allocated mbufs queue */
191 struct rte_avp_fifo *free_q[RTE_AVP_MAX_QUEUES];
192 /**< To be freed mbufs queue */
194 /* For request & response */
195 struct rte_avp_fifo *req_q; /**< Request queue */
196 struct rte_avp_fifo *resp_q; /**< Response queue */
197 void *host_sync_addr; /**< (host) Req/Resp Mem address */
198 void *sync_addr; /**< Req/Resp Mem address */
199 void *host_mbuf_addr; /**< (host) MBUF pool start address */
200 void *mbuf_addr; /**< MBUF pool start address */
201 } __rte_cache_aligned;
203 /* RTE ethernet private data */
206 } __rte_cache_aligned;
209 /* 32-bit MMIO register write */
210 #define AVP_WRITE32(_value, _addr) rte_write32_relaxed((_value), (_addr))
212 /* 32-bit MMIO register read */
213 #define AVP_READ32(_addr) rte_read32_relaxed((_addr))
215 /* Macro to cast the ethernet device private data to a AVP object */
216 #define AVP_DEV_PRIVATE_TO_HW(adapter) \
217 (&((struct avp_adapter *)adapter)->avp)
220 * Defines the structure of a AVP device queue for the purpose of handling the
221 * receive and transmit burst callback functions
224 struct rte_eth_dev_data *dev_data;
225 /**< Backpointer to ethernet device data */
226 struct avp_dev *avp; /**< Backpointer to AVP device */
228 /**< Queue identifier used for indexing current queue */
230 /**< Base queue identifier for queue servicing */
231 uint16_t queue_limit;
232 /**< Maximum queue identifier for queue servicing */
239 /* send a request and wait for a response
241 * @warning must be called while holding the avp->lock spinlock.
244 avp_dev_process_request(struct avp_dev *avp, struct rte_avp_request *request)
246 unsigned int retry = AVP_MAX_REQUEST_RETRY;
247 void *resp_addr = NULL;
251 PMD_DRV_LOG(DEBUG, "Sending request %u to host\n", request->req_id);
253 request->result = -ENOTSUP;
255 /* Discard any stale responses before starting a new request */
256 while (avp_fifo_get(avp->resp_q, (void **)&resp_addr, 1))
257 PMD_DRV_LOG(DEBUG, "Discarding stale response\n");
259 rte_memcpy(avp->sync_addr, request, sizeof(*request));
260 count = avp_fifo_put(avp->req_q, &avp->host_sync_addr, 1);
262 PMD_DRV_LOG(ERR, "Cannot send request %u to host\n",
269 /* wait for a response */
270 usleep(AVP_REQUEST_DELAY_USECS);
272 count = avp_fifo_count(avp->resp_q);
274 /* response received */
278 if ((count < 1) && (retry == 0)) {
279 PMD_DRV_LOG(ERR, "Timeout while waiting for a response for %u\n",
286 /* retrieve the response */
287 count = avp_fifo_get(avp->resp_q, (void **)&resp_addr, 1);
288 if ((count != 1) || (resp_addr != avp->host_sync_addr)) {
289 PMD_DRV_LOG(ERR, "Invalid response from host, count=%u resp=%p host_sync_addr=%p\n",
290 count, resp_addr, avp->host_sync_addr);
295 /* copy to user buffer */
296 rte_memcpy(request, avp->sync_addr, sizeof(*request));
299 PMD_DRV_LOG(DEBUG, "Result %d received for request %u\n",
300 request->result, request->req_id);
307 avp_dev_ctrl_set_config(struct rte_eth_dev *eth_dev,
308 struct rte_avp_device_config *config)
310 struct avp_dev *avp = AVP_DEV_PRIVATE_TO_HW(eth_dev->data->dev_private);
311 struct rte_avp_request request;
314 /* setup a configure request */
315 memset(&request, 0, sizeof(request));
316 request.req_id = RTE_AVP_REQ_CFG_DEVICE;
317 memcpy(&request.config, config, sizeof(request.config));
319 ret = avp_dev_process_request(avp, &request);
321 return ret == 0 ? request.result : ret;
324 /* translate from host mbuf virtual address to guest virtual address */
326 avp_dev_translate_buffer(struct avp_dev *avp, void *host_mbuf_address)
328 return RTE_PTR_ADD(RTE_PTR_SUB(host_mbuf_address,
329 (uintptr_t)avp->host_mbuf_addr),
330 (uintptr_t)avp->mbuf_addr);
333 /* translate from host physical address to guest virtual address */
335 avp_dev_translate_address(struct rte_eth_dev *eth_dev,
336 phys_addr_t host_phys_addr)
338 struct rte_pci_device *pci_dev = AVP_DEV_TO_PCI(eth_dev);
339 struct rte_mem_resource *resource;
340 struct rte_avp_memmap_info *info;
341 struct rte_avp_memmap *map;
346 addr = pci_dev->mem_resource[RTE_AVP_PCI_MEMORY_BAR].addr;
347 resource = &pci_dev->mem_resource[RTE_AVP_PCI_MEMMAP_BAR];
348 info = (struct rte_avp_memmap_info *)resource->addr;
351 for (i = 0; i < info->nb_maps; i++) {
352 /* search all segments looking for a matching address */
353 map = &info->maps[i];
355 if ((host_phys_addr >= map->phys_addr) &&
356 (host_phys_addr < (map->phys_addr + map->length))) {
357 /* address is within this segment */
358 offset += (host_phys_addr - map->phys_addr);
359 addr = RTE_PTR_ADD(addr, offset);
361 PMD_DRV_LOG(DEBUG, "Translating host physical 0x%" PRIx64 " to guest virtual 0x%p\n",
362 host_phys_addr, addr);
366 offset += map->length;
372 /* verify that the incoming device version is compatible with our version */
374 avp_dev_version_check(uint32_t version)
376 uint32_t driver = RTE_AVP_STRIP_MINOR_VERSION(AVP_DPDK_DRIVER_VERSION);
377 uint32_t device = RTE_AVP_STRIP_MINOR_VERSION(version);
379 if (device <= driver) {
380 /* the host driver version is less than or equal to ours */
387 /* verify that memory regions have expected version and validation markers */
389 avp_dev_check_regions(struct rte_eth_dev *eth_dev)
391 struct rte_pci_device *pci_dev = AVP_DEV_TO_PCI(eth_dev);
392 struct rte_avp_memmap_info *memmap;
393 struct rte_avp_device_info *info;
394 struct rte_mem_resource *resource;
397 /* Dump resource info for debug */
398 for (i = 0; i < PCI_MAX_RESOURCE; i++) {
399 resource = &pci_dev->mem_resource[i];
400 if ((resource->phys_addr == 0) || (resource->len == 0))
403 PMD_DRV_LOG(DEBUG, "resource[%u]: phys=0x%" PRIx64 " len=%" PRIu64 " addr=%p\n",
404 i, resource->phys_addr,
405 resource->len, resource->addr);
408 case RTE_AVP_PCI_MEMMAP_BAR:
409 memmap = (struct rte_avp_memmap_info *)resource->addr;
410 if ((memmap->magic != RTE_AVP_MEMMAP_MAGIC) ||
411 (memmap->version != RTE_AVP_MEMMAP_VERSION)) {
412 PMD_DRV_LOG(ERR, "Invalid memmap magic 0x%08x and version %u\n",
413 memmap->magic, memmap->version);
418 case RTE_AVP_PCI_DEVICE_BAR:
419 info = (struct rte_avp_device_info *)resource->addr;
420 if ((info->magic != RTE_AVP_DEVICE_MAGIC) ||
421 avp_dev_version_check(info->version)) {
422 PMD_DRV_LOG(ERR, "Invalid device info magic 0x%08x or version 0x%08x > 0x%08x\n",
423 info->magic, info->version,
424 AVP_DPDK_DRIVER_VERSION);
429 case RTE_AVP_PCI_MEMORY_BAR:
430 case RTE_AVP_PCI_MMIO_BAR:
431 if (resource->addr == NULL) {
432 PMD_DRV_LOG(ERR, "Missing address space for BAR%u\n",
438 case RTE_AVP_PCI_MSIX_BAR:
440 /* no validation required */
449 _avp_set_rx_queue_mappings(struct rte_eth_dev *eth_dev, uint16_t rx_queue_id)
451 struct avp_dev *avp =
452 AVP_DEV_PRIVATE_TO_HW(eth_dev->data->dev_private);
453 struct avp_queue *rxq;
454 uint16_t queue_count;
457 rxq = (struct avp_queue *)eth_dev->data->rx_queues[rx_queue_id];
460 * Must map all AVP fifos as evenly as possible between the configured
461 * device queues. Each device queue will service a subset of the AVP
462 * fifos. If there is an odd number of device queues the first set of
463 * device queues will get the extra AVP fifos.
465 queue_count = avp->num_rx_queues / eth_dev->data->nb_rx_queues;
466 remainder = avp->num_rx_queues % eth_dev->data->nb_rx_queues;
467 if (rx_queue_id < remainder) {
468 /* these queues must service one extra FIFO */
469 rxq->queue_base = rx_queue_id * (queue_count + 1);
470 rxq->queue_limit = rxq->queue_base + (queue_count + 1) - 1;
472 /* these queues service the regular number of FIFO */
473 rxq->queue_base = ((remainder * (queue_count + 1)) +
474 ((rx_queue_id - remainder) * queue_count));
475 rxq->queue_limit = rxq->queue_base + queue_count - 1;
478 PMD_DRV_LOG(DEBUG, "rxq %u at %p base %u limit %u\n",
479 rx_queue_id, rxq, rxq->queue_base, rxq->queue_limit);
481 rxq->queue_id = rxq->queue_base;
485 _avp_set_queue_counts(struct rte_eth_dev *eth_dev)
487 struct rte_pci_device *pci_dev = AVP_DEV_TO_PCI(eth_dev);
488 struct avp_dev *avp = AVP_DEV_PRIVATE_TO_HW(eth_dev->data->dev_private);
489 struct rte_avp_device_info *host_info;
492 addr = pci_dev->mem_resource[RTE_AVP_PCI_DEVICE_BAR].addr;
493 host_info = (struct rte_avp_device_info *)addr;
496 * the transmit direction is not negotiated beyond respecting the max
497 * number of queues because the host can handle arbitrary guest tx
498 * queues (host rx queues).
500 avp->num_tx_queues = eth_dev->data->nb_tx_queues;
503 * the receive direction is more restrictive. The host requires a
504 * minimum number of guest rx queues (host tx queues) therefore
505 * negotiate a value that is at least as large as the host minimum
506 * requirement. If the host and guest values are not identical then a
507 * mapping will be established in the receive_queue_setup function.
509 avp->num_rx_queues = RTE_MAX(host_info->min_rx_queues,
510 eth_dev->data->nb_rx_queues);
512 PMD_DRV_LOG(DEBUG, "Requesting %u Tx and %u Rx queues from host\n",
513 avp->num_tx_queues, avp->num_rx_queues);
517 * create a AVP device using the supplied device info by first translating it
518 * to guest address space(s).
521 avp_dev_create(struct rte_pci_device *pci_dev,
522 struct rte_eth_dev *eth_dev)
524 struct avp_dev *avp = AVP_DEV_PRIVATE_TO_HW(eth_dev->data->dev_private);
525 struct rte_avp_device_info *host_info;
526 struct rte_mem_resource *resource;
529 resource = &pci_dev->mem_resource[RTE_AVP_PCI_DEVICE_BAR];
530 if (resource->addr == NULL) {
531 PMD_DRV_LOG(ERR, "BAR%u is not mapped\n",
532 RTE_AVP_PCI_DEVICE_BAR);
535 host_info = (struct rte_avp_device_info *)resource->addr;
537 if ((host_info->magic != RTE_AVP_DEVICE_MAGIC) ||
538 avp_dev_version_check(host_info->version)) {
539 PMD_DRV_LOG(ERR, "Invalid AVP PCI device, magic 0x%08x version 0x%08x > 0x%08x\n",
540 host_info->magic, host_info->version,
541 AVP_DPDK_DRIVER_VERSION);
545 PMD_DRV_LOG(DEBUG, "AVP host device is v%u.%u.%u\n",
546 RTE_AVP_GET_RELEASE_VERSION(host_info->version),
547 RTE_AVP_GET_MAJOR_VERSION(host_info->version),
548 RTE_AVP_GET_MINOR_VERSION(host_info->version));
550 PMD_DRV_LOG(DEBUG, "AVP host supports %u to %u TX queue(s)\n",
551 host_info->min_tx_queues, host_info->max_tx_queues);
552 PMD_DRV_LOG(DEBUG, "AVP host supports %u to %u RX queue(s)\n",
553 host_info->min_rx_queues, host_info->max_rx_queues);
554 PMD_DRV_LOG(DEBUG, "AVP host supports features 0x%08x\n",
555 host_info->features);
557 if (avp->magic != AVP_ETHDEV_MAGIC) {
559 * First time initialization (i.e., not during a VM
562 memset(avp, 0, sizeof(*avp));
563 avp->magic = AVP_ETHDEV_MAGIC;
564 avp->dev_data = eth_dev->data;
565 avp->port_id = eth_dev->data->port_id;
566 avp->host_mbuf_size = host_info->mbuf_size;
567 avp->host_features = host_info->features;
568 memcpy(&avp->ethaddr.addr_bytes[0],
569 host_info->ethaddr, ETHER_ADDR_LEN);
570 /* adjust max values to not exceed our max */
572 RTE_MIN(host_info->max_tx_queues, RTE_AVP_MAX_QUEUES);
574 RTE_MIN(host_info->max_rx_queues, RTE_AVP_MAX_QUEUES);
576 /* Re-attaching during migration */
578 /* TODO... requires validation of host values */
579 if ((host_info->features & avp->features) != avp->features) {
580 PMD_DRV_LOG(ERR, "AVP host features mismatched; 0x%08x, host=0x%08x\n",
581 avp->features, host_info->features);
582 /* this should not be possible; continue for now */
586 /* the device id is allowed to change over migrations */
587 avp->device_id = host_info->device_id;
589 /* translate incoming host addresses to guest address space */
590 PMD_DRV_LOG(DEBUG, "AVP first host tx queue at 0x%" PRIx64 "\n",
592 PMD_DRV_LOG(DEBUG, "AVP first host alloc queue at 0x%" PRIx64 "\n",
593 host_info->alloc_phys);
594 for (i = 0; i < avp->max_tx_queues; i++) {
595 avp->tx_q[i] = avp_dev_translate_address(eth_dev,
596 host_info->tx_phys + (i * host_info->tx_size));
598 avp->alloc_q[i] = avp_dev_translate_address(eth_dev,
599 host_info->alloc_phys + (i * host_info->alloc_size));
602 PMD_DRV_LOG(DEBUG, "AVP first host rx queue at 0x%" PRIx64 "\n",
604 PMD_DRV_LOG(DEBUG, "AVP first host free queue at 0x%" PRIx64 "\n",
605 host_info->free_phys);
606 for (i = 0; i < avp->max_rx_queues; i++) {
607 avp->rx_q[i] = avp_dev_translate_address(eth_dev,
608 host_info->rx_phys + (i * host_info->rx_size));
609 avp->free_q[i] = avp_dev_translate_address(eth_dev,
610 host_info->free_phys + (i * host_info->free_size));
613 PMD_DRV_LOG(DEBUG, "AVP host request queue at 0x%" PRIx64 "\n",
614 host_info->req_phys);
615 PMD_DRV_LOG(DEBUG, "AVP host response queue at 0x%" PRIx64 "\n",
616 host_info->resp_phys);
617 PMD_DRV_LOG(DEBUG, "AVP host sync address at 0x%" PRIx64 "\n",
618 host_info->sync_phys);
619 PMD_DRV_LOG(DEBUG, "AVP host mbuf address at 0x%" PRIx64 "\n",
620 host_info->mbuf_phys);
621 avp->req_q = avp_dev_translate_address(eth_dev, host_info->req_phys);
622 avp->resp_q = avp_dev_translate_address(eth_dev, host_info->resp_phys);
624 avp_dev_translate_address(eth_dev, host_info->sync_phys);
626 avp_dev_translate_address(eth_dev, host_info->mbuf_phys);
629 * store the host mbuf virtual address so that we can calculate
630 * relative offsets for each mbuf as they are processed
632 avp->host_mbuf_addr = host_info->mbuf_va;
633 avp->host_sync_addr = host_info->sync_va;
636 * store the maximum packet length that is supported by the host.
638 avp->max_rx_pkt_len = host_info->max_rx_pkt_len;
639 PMD_DRV_LOG(DEBUG, "AVP host max receive packet length is %u\n",
640 host_info->max_rx_pkt_len);
646 * This function is based on probe() function in avp_pci.c
647 * It returns 0 on success.
650 eth_avp_dev_init(struct rte_eth_dev *eth_dev)
652 struct avp_dev *avp =
653 AVP_DEV_PRIVATE_TO_HW(eth_dev->data->dev_private);
654 struct rte_pci_device *pci_dev;
657 pci_dev = AVP_DEV_TO_PCI(eth_dev);
658 eth_dev->dev_ops = &avp_eth_dev_ops;
659 eth_dev->rx_pkt_burst = &avp_recv_pkts;
660 eth_dev->tx_pkt_burst = &avp_xmit_pkts;
662 if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
664 * no setup required on secondary processes. All data is saved
665 * in dev_private by the primary process. All resource should
666 * be mapped to the same virtual address so all pointers should
669 if (eth_dev->data->scattered_rx) {
670 PMD_DRV_LOG(NOTICE, "AVP device configured for chained mbufs\n");
671 eth_dev->rx_pkt_burst = avp_recv_scattered_pkts;
672 eth_dev->tx_pkt_burst = avp_xmit_scattered_pkts;
677 rte_eth_copy_pci_info(eth_dev, pci_dev);
679 eth_dev->data->dev_flags |= RTE_ETH_DEV_DETACHABLE;
681 /* Check BAR resources */
682 ret = avp_dev_check_regions(eth_dev);
684 PMD_DRV_LOG(ERR, "Failed to validate BAR resources, ret=%d\n",
689 /* Handle each subtype */
690 ret = avp_dev_create(pci_dev, eth_dev);
692 PMD_DRV_LOG(ERR, "Failed to create device, ret=%d\n", ret);
696 /* Allocate memory for storing MAC addresses */
697 eth_dev->data->mac_addrs = rte_zmalloc("avp_ethdev", ETHER_ADDR_LEN, 0);
698 if (eth_dev->data->mac_addrs == NULL) {
699 PMD_DRV_LOG(ERR, "Failed to allocate %d bytes needed to store MAC addresses\n",
704 /* Get a mac from device config */
705 ether_addr_copy(&avp->ethaddr, ð_dev->data->mac_addrs[0]);
711 eth_avp_dev_uninit(struct rte_eth_dev *eth_dev)
713 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
716 if (eth_dev->data == NULL)
719 if (eth_dev->data->mac_addrs != NULL) {
720 rte_free(eth_dev->data->mac_addrs);
721 eth_dev->data->mac_addrs = NULL;
728 static struct eth_driver rte_avp_pmd = {
730 .id_table = pci_id_avp_map,
731 .drv_flags = RTE_PCI_DRV_NEED_MAPPING,
732 .probe = rte_eth_dev_pci_probe,
733 .remove = rte_eth_dev_pci_remove,
735 .eth_dev_init = eth_avp_dev_init,
736 .eth_dev_uninit = eth_avp_dev_uninit,
737 .dev_private_size = sizeof(struct avp_adapter),
741 avp_dev_enable_scattered(struct rte_eth_dev *eth_dev,
744 unsigned int max_rx_pkt_len;
746 max_rx_pkt_len = eth_dev->data->dev_conf.rxmode.max_rx_pkt_len;
748 if ((max_rx_pkt_len > avp->guest_mbuf_size) ||
749 (max_rx_pkt_len > avp->host_mbuf_size)) {
751 * If the guest MTU is greater than either the host or guest
752 * buffers then chained mbufs have to be enabled in the TX
753 * direction. It is assumed that the application will not need
754 * to send packets larger than their max_rx_pkt_len (MRU).
759 if ((avp->max_rx_pkt_len > avp->guest_mbuf_size) ||
760 (avp->max_rx_pkt_len > avp->host_mbuf_size)) {
762 * If the host MRU is greater than its own mbuf size or the
763 * guest mbuf size then chained mbufs have to be enabled in the
773 avp_dev_rx_queue_setup(struct rte_eth_dev *eth_dev,
774 uint16_t rx_queue_id,
776 unsigned int socket_id,
777 const struct rte_eth_rxconf *rx_conf,
778 struct rte_mempool *pool)
780 struct avp_dev *avp = AVP_DEV_PRIVATE_TO_HW(eth_dev->data->dev_private);
781 struct rte_pktmbuf_pool_private *mbp_priv;
782 struct avp_queue *rxq;
784 if (rx_queue_id >= eth_dev->data->nb_rx_queues) {
785 PMD_DRV_LOG(ERR, "RX queue id is out of range: rx_queue_id=%u, nb_rx_queues=%u\n",
786 rx_queue_id, eth_dev->data->nb_rx_queues);
790 /* Save mbuf pool pointer */
793 /* Save the local mbuf size */
794 mbp_priv = rte_mempool_get_priv(pool);
795 avp->guest_mbuf_size = (uint16_t)(mbp_priv->mbuf_data_room_size);
796 avp->guest_mbuf_size -= RTE_PKTMBUF_HEADROOM;
798 if (avp_dev_enable_scattered(eth_dev, avp)) {
799 if (!eth_dev->data->scattered_rx) {
800 PMD_DRV_LOG(NOTICE, "AVP device configured for chained mbufs\n");
801 eth_dev->data->scattered_rx = 1;
802 eth_dev->rx_pkt_burst = avp_recv_scattered_pkts;
803 eth_dev->tx_pkt_burst = avp_xmit_scattered_pkts;
807 PMD_DRV_LOG(DEBUG, "AVP max_rx_pkt_len=(%u,%u) mbuf_size=(%u,%u)\n",
809 eth_dev->data->dev_conf.rxmode.max_rx_pkt_len,
811 avp->guest_mbuf_size);
813 /* allocate a queue object */
814 rxq = rte_zmalloc_socket("ethdev RX queue", sizeof(struct avp_queue),
815 RTE_CACHE_LINE_SIZE, socket_id);
817 PMD_DRV_LOG(ERR, "Failed to allocate new Rx queue object\n");
821 /* save back pointers to AVP and Ethernet devices */
823 rxq->dev_data = eth_dev->data;
824 eth_dev->data->rx_queues[rx_queue_id] = (void *)rxq;
826 /* setup the queue receive mapping for the current queue. */
827 _avp_set_rx_queue_mappings(eth_dev, rx_queue_id);
829 PMD_DRV_LOG(DEBUG, "Rx queue %u setup at %p\n", rx_queue_id, rxq);
837 avp_dev_tx_queue_setup(struct rte_eth_dev *eth_dev,
838 uint16_t tx_queue_id,
840 unsigned int socket_id,
841 const struct rte_eth_txconf *tx_conf)
843 struct avp_dev *avp = AVP_DEV_PRIVATE_TO_HW(eth_dev->data->dev_private);
844 struct avp_queue *txq;
846 if (tx_queue_id >= eth_dev->data->nb_tx_queues) {
847 PMD_DRV_LOG(ERR, "TX queue id is out of range: tx_queue_id=%u, nb_tx_queues=%u\n",
848 tx_queue_id, eth_dev->data->nb_tx_queues);
852 /* allocate a queue object */
853 txq = rte_zmalloc_socket("ethdev TX queue", sizeof(struct avp_queue),
854 RTE_CACHE_LINE_SIZE, socket_id);
856 PMD_DRV_LOG(ERR, "Failed to allocate new Tx queue object\n");
860 /* only the configured set of transmit queues are used */
861 txq->queue_id = tx_queue_id;
862 txq->queue_base = tx_queue_id;
863 txq->queue_limit = tx_queue_id;
865 /* save back pointers to AVP and Ethernet devices */
867 txq->dev_data = eth_dev->data;
868 eth_dev->data->tx_queues[tx_queue_id] = (void *)txq;
870 PMD_DRV_LOG(DEBUG, "Tx queue %u setup at %p\n", tx_queue_id, txq);
878 _avp_cmp_ether_addr(struct ether_addr *a, struct ether_addr *b)
880 uint16_t *_a = (uint16_t *)&a->addr_bytes[0];
881 uint16_t *_b = (uint16_t *)&b->addr_bytes[0];
882 return (_a[0] ^ _b[0]) | (_a[1] ^ _b[1]) | (_a[2] ^ _b[2]);
886 _avp_mac_filter(struct avp_dev *avp, struct rte_mbuf *m)
888 struct ether_hdr *eth = rte_pktmbuf_mtod(m, struct ether_hdr *);
890 if (likely(_avp_cmp_ether_addr(&avp->ethaddr, ð->d_addr) == 0)) {
891 /* allow all packets destined to our address */
895 if (likely(is_broadcast_ether_addr(ð->d_addr))) {
896 /* allow all broadcast packets */
900 if (likely(is_multicast_ether_addr(ð->d_addr))) {
901 /* allow all multicast packets */
905 if (avp->flags & AVP_F_PROMISC) {
906 /* allow all packets when in promiscuous mode */
913 #ifdef RTE_LIBRTE_AVP_DEBUG_BUFFERS
915 __avp_dev_buffer_sanity_check(struct avp_dev *avp, struct rte_avp_desc *buf)
917 struct rte_avp_desc *first_buf;
918 struct rte_avp_desc *pkt_buf;
919 unsigned int pkt_len;
920 unsigned int nb_segs;
924 first_buf = avp_dev_translate_buffer(avp, buf);
928 nb_segs = first_buf->nb_segs;
930 /* Adjust pointers for guest addressing */
931 pkt_buf = avp_dev_translate_buffer(avp, buf);
933 rte_panic("bad buffer: segment %u has an invalid address %p\n",
935 pkt_data = avp_dev_translate_buffer(avp, pkt_buf->data);
936 if (pkt_data == NULL)
937 rte_panic("bad buffer: segment %u has a NULL data pointer\n",
939 if (pkt_buf->data_len == 0)
940 rte_panic("bad buffer: segment %u has 0 data length\n",
942 pkt_len += pkt_buf->data_len;
946 } while (nb_segs && (buf = pkt_buf->next) != NULL);
949 rte_panic("bad buffer: expected %u segments found %u\n",
950 first_buf->nb_segs, (first_buf->nb_segs - nb_segs));
951 if (pkt_len != first_buf->pkt_len)
952 rte_panic("bad buffer: expected length %u found %u\n",
953 first_buf->pkt_len, pkt_len);
956 #define avp_dev_buffer_sanity_check(a, b) \
957 __avp_dev_buffer_sanity_check((a), (b))
959 #else /* RTE_LIBRTE_AVP_DEBUG_BUFFERS */
961 #define avp_dev_buffer_sanity_check(a, b) do {} while (0)
966 * Copy a host buffer chain to a set of mbufs. This function assumes that
967 * there exactly the required number of mbufs to copy all source bytes.
969 static inline struct rte_mbuf *
970 avp_dev_copy_from_buffers(struct avp_dev *avp,
971 struct rte_avp_desc *buf,
972 struct rte_mbuf **mbufs,
975 struct rte_mbuf *m_previous = NULL;
976 struct rte_avp_desc *pkt_buf;
977 unsigned int total_length = 0;
978 unsigned int copy_length;
979 unsigned int src_offset;
986 avp_dev_buffer_sanity_check(avp, buf);
988 /* setup the first source buffer */
989 pkt_buf = avp_dev_translate_buffer(avp, buf);
990 pkt_data = avp_dev_translate_buffer(avp, pkt_buf->data);
991 total_length = pkt_buf->pkt_len;
994 if (pkt_buf->ol_flags & RTE_AVP_RX_VLAN_PKT) {
995 ol_flags = PKT_RX_VLAN_PKT;
996 vlan_tci = pkt_buf->vlan_tci;
1002 for (i = 0; (i < count) && (buf != NULL); i++) {
1003 /* fill each destination buffer */
1006 if (m_previous != NULL)
1007 m_previous->next = m;
1013 * Copy as many source buffers as will fit in the
1014 * destination buffer.
1016 copy_length = RTE_MIN((avp->guest_mbuf_size -
1017 rte_pktmbuf_data_len(m)),
1018 (pkt_buf->data_len -
1020 rte_memcpy(RTE_PTR_ADD(rte_pktmbuf_mtod(m, void *),
1021 rte_pktmbuf_data_len(m)),
1022 RTE_PTR_ADD(pkt_data, src_offset),
1024 rte_pktmbuf_data_len(m) += copy_length;
1025 src_offset += copy_length;
1027 if (likely(src_offset == pkt_buf->data_len)) {
1028 /* need a new source buffer */
1029 buf = pkt_buf->next;
1031 pkt_buf = avp_dev_translate_buffer(
1033 pkt_data = avp_dev_translate_buffer(
1034 avp, pkt_buf->data);
1039 if (unlikely(rte_pktmbuf_data_len(m) ==
1040 avp->guest_mbuf_size)) {
1041 /* need a new destination mbuf */
1045 } while (buf != NULL);
1049 m->ol_flags = ol_flags;
1051 rte_pktmbuf_pkt_len(m) = total_length;
1052 m->vlan_tci = vlan_tci;
1054 __rte_mbuf_sanity_check(m, 1);
1060 avp_recv_scattered_pkts(void *rx_queue,
1061 struct rte_mbuf **rx_pkts,
1064 struct avp_queue *rxq = (struct avp_queue *)rx_queue;
1065 struct rte_avp_desc *avp_bufs[AVP_MAX_RX_BURST];
1066 struct rte_mbuf *mbufs[RTE_AVP_MAX_MBUF_SEGMENTS];
1067 struct avp_dev *avp = rxq->avp;
1068 struct rte_avp_desc *pkt_buf;
1069 struct rte_avp_fifo *free_q;
1070 struct rte_avp_fifo *rx_q;
1071 struct rte_avp_desc *buf;
1072 unsigned int count, avail, n;
1073 unsigned int guest_mbuf_size;
1075 unsigned int required;
1076 unsigned int buf_len;
1077 unsigned int port_id;
1080 guest_mbuf_size = avp->guest_mbuf_size;
1081 port_id = avp->port_id;
1082 rx_q = avp->rx_q[rxq->queue_id];
1083 free_q = avp->free_q[rxq->queue_id];
1085 /* setup next queue to service */
1086 rxq->queue_id = (rxq->queue_id < rxq->queue_limit) ?
1087 (rxq->queue_id + 1) : rxq->queue_base;
1089 /* determine how many slots are available in the free queue */
1090 count = avp_fifo_free_count(free_q);
1092 /* determine how many packets are available in the rx queue */
1093 avail = avp_fifo_count(rx_q);
1095 /* determine how many packets can be received */
1096 count = RTE_MIN(count, avail);
1097 count = RTE_MIN(count, nb_pkts);
1098 count = RTE_MIN(count, (unsigned int)AVP_MAX_RX_BURST);
1100 if (unlikely(count == 0)) {
1101 /* no free buffers, or no buffers on the rx queue */
1105 /* retrieve pending packets */
1106 n = avp_fifo_get(rx_q, (void **)&avp_bufs, count);
1107 PMD_RX_LOG(DEBUG, "Receiving %u packets from Rx queue at %p\n",
1111 for (i = 0; i < n; i++) {
1112 /* prefetch next entry while processing current one */
1114 pkt_buf = avp_dev_translate_buffer(avp,
1116 rte_prefetch0(pkt_buf);
1120 /* Peek into the first buffer to determine the total length */
1121 pkt_buf = avp_dev_translate_buffer(avp, buf);
1122 buf_len = pkt_buf->pkt_len;
1124 /* Allocate enough mbufs to receive the entire packet */
1125 required = (buf_len + guest_mbuf_size - 1) / guest_mbuf_size;
1126 if (rte_pktmbuf_alloc_bulk(avp->pool, mbufs, required)) {
1127 rxq->dev_data->rx_mbuf_alloc_failed++;
1131 /* Copy the data from the buffers to our mbufs */
1132 m = avp_dev_copy_from_buffers(avp, buf, mbufs, required);
1137 if (_avp_mac_filter(avp, m) != 0) {
1138 /* silently discard packets not destined to our MAC */
1139 rte_pktmbuf_free(m);
1143 /* return new mbuf to caller */
1144 rx_pkts[count++] = m;
1145 rxq->bytes += buf_len;
1148 rxq->packets += count;
1150 /* return the buffers to the free queue */
1151 avp_fifo_put(free_q, (void **)&avp_bufs[0], n);
1158 avp_recv_pkts(void *rx_queue,
1159 struct rte_mbuf **rx_pkts,
1162 struct avp_queue *rxq = (struct avp_queue *)rx_queue;
1163 struct rte_avp_desc *avp_bufs[AVP_MAX_RX_BURST];
1164 struct avp_dev *avp = rxq->avp;
1165 struct rte_avp_desc *pkt_buf;
1166 struct rte_avp_fifo *free_q;
1167 struct rte_avp_fifo *rx_q;
1168 unsigned int count, avail, n;
1169 unsigned int pkt_len;
1174 rx_q = avp->rx_q[rxq->queue_id];
1175 free_q = avp->free_q[rxq->queue_id];
1177 /* setup next queue to service */
1178 rxq->queue_id = (rxq->queue_id < rxq->queue_limit) ?
1179 (rxq->queue_id + 1) : rxq->queue_base;
1181 /* determine how many slots are available in the free queue */
1182 count = avp_fifo_free_count(free_q);
1184 /* determine how many packets are available in the rx queue */
1185 avail = avp_fifo_count(rx_q);
1187 /* determine how many packets can be received */
1188 count = RTE_MIN(count, avail);
1189 count = RTE_MIN(count, nb_pkts);
1190 count = RTE_MIN(count, (unsigned int)AVP_MAX_RX_BURST);
1192 if (unlikely(count == 0)) {
1193 /* no free buffers, or no buffers on the rx queue */
1197 /* retrieve pending packets */
1198 n = avp_fifo_get(rx_q, (void **)&avp_bufs, count);
1199 PMD_RX_LOG(DEBUG, "Receiving %u packets from Rx queue at %p\n",
1203 for (i = 0; i < n; i++) {
1204 /* prefetch next entry while processing current one */
1206 pkt_buf = avp_dev_translate_buffer(avp,
1208 rte_prefetch0(pkt_buf);
1211 /* Adjust host pointers for guest addressing */
1212 pkt_buf = avp_dev_translate_buffer(avp, avp_bufs[i]);
1213 pkt_data = avp_dev_translate_buffer(avp, pkt_buf->data);
1214 pkt_len = pkt_buf->pkt_len;
1216 if (unlikely((pkt_len > avp->guest_mbuf_size) ||
1217 (pkt_buf->nb_segs > 1))) {
1219 * application should be using the scattered receive
1226 /* process each packet to be transmitted */
1227 m = rte_pktmbuf_alloc(avp->pool);
1228 if (unlikely(m == NULL)) {
1229 rxq->dev_data->rx_mbuf_alloc_failed++;
1233 /* copy data out of the host buffer to our buffer */
1234 m->data_off = RTE_PKTMBUF_HEADROOM;
1235 rte_memcpy(rte_pktmbuf_mtod(m, void *), pkt_data, pkt_len);
1237 /* initialize the local mbuf */
1238 rte_pktmbuf_data_len(m) = pkt_len;
1239 rte_pktmbuf_pkt_len(m) = pkt_len;
1240 m->port = avp->port_id;
1242 if (pkt_buf->ol_flags & RTE_AVP_RX_VLAN_PKT) {
1243 m->ol_flags = PKT_RX_VLAN_PKT;
1244 m->vlan_tci = pkt_buf->vlan_tci;
1247 if (_avp_mac_filter(avp, m) != 0) {
1248 /* silently discard packets not destined to our MAC */
1249 rte_pktmbuf_free(m);
1253 /* return new mbuf to caller */
1254 rx_pkts[count++] = m;
1255 rxq->bytes += pkt_len;
1258 rxq->packets += count;
1260 /* return the buffers to the free queue */
1261 avp_fifo_put(free_q, (void **)&avp_bufs[0], n);
1267 * Copy a chained mbuf to a set of host buffers. This function assumes that
1268 * there are sufficient destination buffers to contain the entire source
1271 static inline uint16_t
1272 avp_dev_copy_to_buffers(struct avp_dev *avp,
1273 struct rte_mbuf *mbuf,
1274 struct rte_avp_desc **buffers,
1277 struct rte_avp_desc *previous_buf = NULL;
1278 struct rte_avp_desc *first_buf = NULL;
1279 struct rte_avp_desc *pkt_buf;
1280 struct rte_avp_desc *buf;
1281 size_t total_length;
1288 __rte_mbuf_sanity_check(mbuf, 1);
1292 total_length = rte_pktmbuf_pkt_len(m);
1293 for (i = 0; (i < count) && (m != NULL); i++) {
1294 /* fill each destination buffer */
1297 if (i < count - 1) {
1298 /* prefetch next entry while processing this one */
1299 pkt_buf = avp_dev_translate_buffer(avp, buffers[i + 1]);
1300 rte_prefetch0(pkt_buf);
1303 /* Adjust pointers for guest addressing */
1304 pkt_buf = avp_dev_translate_buffer(avp, buf);
1305 pkt_data = avp_dev_translate_buffer(avp, pkt_buf->data);
1307 /* setup the buffer chain */
1308 if (previous_buf != NULL)
1309 previous_buf->next = buf;
1311 first_buf = pkt_buf;
1313 previous_buf = pkt_buf;
1317 * copy as many source mbuf segments as will fit in the
1318 * destination buffer.
1320 copy_length = RTE_MIN((avp->host_mbuf_size -
1322 (rte_pktmbuf_data_len(m) -
1324 rte_memcpy(RTE_PTR_ADD(pkt_data, pkt_buf->data_len),
1325 RTE_PTR_ADD(rte_pktmbuf_mtod(m, void *),
1328 pkt_buf->data_len += copy_length;
1329 src_offset += copy_length;
1331 if (likely(src_offset == rte_pktmbuf_data_len(m))) {
1332 /* need a new source buffer */
1337 if (unlikely(pkt_buf->data_len ==
1338 avp->host_mbuf_size)) {
1339 /* need a new destination buffer */
1343 } while (m != NULL);
1346 first_buf->nb_segs = count;
1347 first_buf->pkt_len = total_length;
1349 if (mbuf->ol_flags & PKT_TX_VLAN_PKT) {
1350 first_buf->ol_flags |= RTE_AVP_TX_VLAN_PKT;
1351 first_buf->vlan_tci = mbuf->vlan_tci;
1354 avp_dev_buffer_sanity_check(avp, buffers[0]);
1356 return total_length;
1361 avp_xmit_scattered_pkts(void *tx_queue,
1362 struct rte_mbuf **tx_pkts,
1365 struct rte_avp_desc *avp_bufs[(AVP_MAX_TX_BURST *
1366 RTE_AVP_MAX_MBUF_SEGMENTS)];
1367 struct avp_queue *txq = (struct avp_queue *)tx_queue;
1368 struct rte_avp_desc *tx_bufs[AVP_MAX_TX_BURST];
1369 struct avp_dev *avp = txq->avp;
1370 struct rte_avp_fifo *alloc_q;
1371 struct rte_avp_fifo *tx_q;
1372 unsigned int count, avail, n;
1373 unsigned int orig_nb_pkts;
1375 unsigned int required;
1376 unsigned int segments;
1377 unsigned int tx_bytes;
1380 orig_nb_pkts = nb_pkts;
1381 tx_q = avp->tx_q[txq->queue_id];
1382 alloc_q = avp->alloc_q[txq->queue_id];
1384 /* limit the number of transmitted packets to the max burst size */
1385 if (unlikely(nb_pkts > AVP_MAX_TX_BURST))
1386 nb_pkts = AVP_MAX_TX_BURST;
1388 /* determine how many buffers are available to copy into */
1389 avail = avp_fifo_count(alloc_q);
1390 if (unlikely(avail > (AVP_MAX_TX_BURST *
1391 RTE_AVP_MAX_MBUF_SEGMENTS)))
1392 avail = AVP_MAX_TX_BURST * RTE_AVP_MAX_MBUF_SEGMENTS;
1394 /* determine how many slots are available in the transmit queue */
1395 count = avp_fifo_free_count(tx_q);
1397 /* determine how many packets can be sent */
1398 nb_pkts = RTE_MIN(count, nb_pkts);
1400 /* determine how many packets will fit in the available buffers */
1403 for (i = 0; i < nb_pkts; i++) {
1405 if (likely(i < (unsigned int)nb_pkts - 1)) {
1406 /* prefetch next entry while processing this one */
1407 rte_prefetch0(tx_pkts[i + 1]);
1409 required = (rte_pktmbuf_pkt_len(m) + avp->host_mbuf_size - 1) /
1410 avp->host_mbuf_size;
1412 if (unlikely((required == 0) ||
1413 (required > RTE_AVP_MAX_MBUF_SEGMENTS)))
1415 else if (unlikely(required + segments > avail))
1417 segments += required;
1422 if (unlikely(nb_pkts == 0)) {
1423 /* no available buffers, or no space on the tx queue */
1424 txq->errors += orig_nb_pkts;
1428 PMD_TX_LOG(DEBUG, "Sending %u packets on Tx queue at %p\n",
1431 /* retrieve sufficient send buffers */
1432 n = avp_fifo_get(alloc_q, (void **)&avp_bufs, segments);
1433 if (unlikely(n != segments)) {
1434 PMD_TX_LOG(DEBUG, "Failed to allocate buffers "
1435 "n=%u, segments=%u, orig=%u\n",
1436 n, segments, orig_nb_pkts);
1437 txq->errors += orig_nb_pkts;
1443 for (i = 0; i < nb_pkts; i++) {
1444 /* process each packet to be transmitted */
1447 /* determine how many buffers are required for this packet */
1448 required = (rte_pktmbuf_pkt_len(m) + avp->host_mbuf_size - 1) /
1449 avp->host_mbuf_size;
1451 tx_bytes += avp_dev_copy_to_buffers(avp, m,
1452 &avp_bufs[count], required);
1453 tx_bufs[i] = avp_bufs[count];
1456 /* free the original mbuf */
1457 rte_pktmbuf_free(m);
1460 txq->packets += nb_pkts;
1461 txq->bytes += tx_bytes;
1463 #ifdef RTE_LIBRTE_AVP_DEBUG_BUFFERS
1464 for (i = 0; i < nb_pkts; i++)
1465 avp_dev_buffer_sanity_check(avp, tx_bufs[i]);
1468 /* send the packets */
1469 n = avp_fifo_put(tx_q, (void **)&tx_bufs[0], nb_pkts);
1470 if (unlikely(n != orig_nb_pkts))
1471 txq->errors += (orig_nb_pkts - n);
1478 avp_xmit_pkts(void *tx_queue, struct rte_mbuf **tx_pkts, uint16_t nb_pkts)
1480 struct avp_queue *txq = (struct avp_queue *)tx_queue;
1481 struct rte_avp_desc *avp_bufs[AVP_MAX_TX_BURST];
1482 struct avp_dev *avp = txq->avp;
1483 struct rte_avp_desc *pkt_buf;
1484 struct rte_avp_fifo *alloc_q;
1485 struct rte_avp_fifo *tx_q;
1486 unsigned int count, avail, n;
1488 unsigned int pkt_len;
1489 unsigned int tx_bytes;
1493 tx_q = avp->tx_q[txq->queue_id];
1494 alloc_q = avp->alloc_q[txq->queue_id];
1496 /* limit the number of transmitted packets to the max burst size */
1497 if (unlikely(nb_pkts > AVP_MAX_TX_BURST))
1498 nb_pkts = AVP_MAX_TX_BURST;
1500 /* determine how many buffers are available to copy into */
1501 avail = avp_fifo_count(alloc_q);
1503 /* determine how many slots are available in the transmit queue */
1504 count = avp_fifo_free_count(tx_q);
1506 /* determine how many packets can be sent */
1507 count = RTE_MIN(count, avail);
1508 count = RTE_MIN(count, nb_pkts);
1510 if (unlikely(count == 0)) {
1511 /* no available buffers, or no space on the tx queue */
1512 txq->errors += nb_pkts;
1516 PMD_TX_LOG(DEBUG, "Sending %u packets on Tx queue at %p\n",
1519 /* retrieve sufficient send buffers */
1520 n = avp_fifo_get(alloc_q, (void **)&avp_bufs, count);
1521 if (unlikely(n != count)) {
1527 for (i = 0; i < count; i++) {
1528 /* prefetch next entry while processing the current one */
1529 if (i < count - 1) {
1530 pkt_buf = avp_dev_translate_buffer(avp,
1532 rte_prefetch0(pkt_buf);
1535 /* process each packet to be transmitted */
1538 /* Adjust pointers for guest addressing */
1539 pkt_buf = avp_dev_translate_buffer(avp, avp_bufs[i]);
1540 pkt_data = avp_dev_translate_buffer(avp, pkt_buf->data);
1541 pkt_len = rte_pktmbuf_pkt_len(m);
1543 if (unlikely((pkt_len > avp->guest_mbuf_size) ||
1544 (pkt_len > avp->host_mbuf_size))) {
1546 * application should be using the scattered transmit
1547 * function; send it truncated to avoid the performance
1548 * hit of having to manage returning the already
1549 * allocated buffer to the free list. This should not
1550 * happen since the application should have set the
1551 * max_rx_pkt_len based on its MTU and it should be
1552 * policing its own packet sizes.
1555 pkt_len = RTE_MIN(avp->guest_mbuf_size,
1556 avp->host_mbuf_size);
1559 /* copy data out of our mbuf and into the AVP buffer */
1560 rte_memcpy(pkt_data, rte_pktmbuf_mtod(m, void *), pkt_len);
1561 pkt_buf->pkt_len = pkt_len;
1562 pkt_buf->data_len = pkt_len;
1563 pkt_buf->nb_segs = 1;
1564 pkt_buf->next = NULL;
1566 if (m->ol_flags & PKT_TX_VLAN_PKT) {
1567 pkt_buf->ol_flags |= RTE_AVP_TX_VLAN_PKT;
1568 pkt_buf->vlan_tci = m->vlan_tci;
1571 tx_bytes += pkt_len;
1573 /* free the original mbuf */
1574 rte_pktmbuf_free(m);
1577 txq->packets += count;
1578 txq->bytes += tx_bytes;
1580 /* send the packets */
1581 n = avp_fifo_put(tx_q, (void **)&avp_bufs[0], count);
1587 avp_dev_rx_queue_release(void *rx_queue)
1589 struct avp_queue *rxq = (struct avp_queue *)rx_queue;
1590 struct avp_dev *avp = rxq->avp;
1591 struct rte_eth_dev_data *data = avp->dev_data;
1594 for (i = 0; i < avp->num_rx_queues; i++) {
1595 if (data->rx_queues[i] == rxq)
1596 data->rx_queues[i] = NULL;
1601 avp_dev_tx_queue_release(void *tx_queue)
1603 struct avp_queue *txq = (struct avp_queue *)tx_queue;
1604 struct avp_dev *avp = txq->avp;
1605 struct rte_eth_dev_data *data = avp->dev_data;
1608 for (i = 0; i < avp->num_tx_queues; i++) {
1609 if (data->tx_queues[i] == txq)
1610 data->tx_queues[i] = NULL;
1615 avp_dev_configure(struct rte_eth_dev *eth_dev)
1617 struct rte_pci_device *pci_dev = AVP_DEV_TO_PCI(eth_dev);
1618 struct avp_dev *avp = AVP_DEV_PRIVATE_TO_HW(eth_dev->data->dev_private);
1619 struct rte_avp_device_info *host_info;
1620 struct rte_avp_device_config config;
1625 addr = pci_dev->mem_resource[RTE_AVP_PCI_DEVICE_BAR].addr;
1626 host_info = (struct rte_avp_device_info *)addr;
1628 /* Setup required number of queues */
1629 _avp_set_queue_counts(eth_dev);
1631 mask = (ETH_VLAN_STRIP_MASK |
1632 ETH_VLAN_FILTER_MASK |
1633 ETH_VLAN_EXTEND_MASK);
1634 avp_vlan_offload_set(eth_dev, mask);
1636 /* update device config */
1637 memset(&config, 0, sizeof(config));
1638 config.device_id = host_info->device_id;
1639 config.driver_type = RTE_AVP_DRIVER_TYPE_DPDK;
1640 config.driver_version = AVP_DPDK_DRIVER_VERSION;
1641 config.features = avp->features;
1642 config.num_tx_queues = avp->num_tx_queues;
1643 config.num_rx_queues = avp->num_rx_queues;
1645 ret = avp_dev_ctrl_set_config(eth_dev, &config);
1647 PMD_DRV_LOG(ERR, "Config request failed by host, ret=%d\n",
1652 avp->flags |= AVP_F_CONFIGURED;
1661 avp_dev_link_update(struct rte_eth_dev *eth_dev,
1662 __rte_unused int wait_to_complete)
1664 struct avp_dev *avp = AVP_DEV_PRIVATE_TO_HW(eth_dev->data->dev_private);
1665 struct rte_eth_link *link = ð_dev->data->dev_link;
1667 link->link_speed = ETH_SPEED_NUM_10G;
1668 link->link_duplex = ETH_LINK_FULL_DUPLEX;
1669 link->link_status = !!(avp->flags & AVP_F_LINKUP);
1676 avp_dev_info_get(struct rte_eth_dev *eth_dev,
1677 struct rte_eth_dev_info *dev_info)
1679 struct avp_dev *avp = AVP_DEV_PRIVATE_TO_HW(eth_dev->data->dev_private);
1681 dev_info->driver_name = "rte_avp_pmd";
1682 dev_info->pci_dev = RTE_DEV_TO_PCI(eth_dev->device);
1683 dev_info->max_rx_queues = avp->max_rx_queues;
1684 dev_info->max_tx_queues = avp->max_tx_queues;
1685 dev_info->min_rx_bufsize = AVP_MIN_RX_BUFSIZE;
1686 dev_info->max_rx_pktlen = avp->max_rx_pkt_len;
1687 dev_info->max_mac_addrs = AVP_MAX_MAC_ADDRS;
1688 if (avp->host_features & RTE_AVP_FEATURE_VLAN_OFFLOAD) {
1689 dev_info->rx_offload_capa = DEV_RX_OFFLOAD_VLAN_STRIP;
1690 dev_info->tx_offload_capa = DEV_TX_OFFLOAD_VLAN_INSERT;
1695 avp_vlan_offload_set(struct rte_eth_dev *eth_dev, int mask)
1697 struct avp_dev *avp = AVP_DEV_PRIVATE_TO_HW(eth_dev->data->dev_private);
1699 if (mask & ETH_VLAN_STRIP_MASK) {
1700 if (avp->host_features & RTE_AVP_FEATURE_VLAN_OFFLOAD) {
1701 if (eth_dev->data->dev_conf.rxmode.hw_vlan_strip)
1702 avp->features |= RTE_AVP_FEATURE_VLAN_OFFLOAD;
1704 avp->features &= ~RTE_AVP_FEATURE_VLAN_OFFLOAD;
1706 PMD_DRV_LOG(ERR, "VLAN strip offload not supported\n");
1710 if (mask & ETH_VLAN_FILTER_MASK) {
1711 if (eth_dev->data->dev_conf.rxmode.hw_vlan_filter)
1712 PMD_DRV_LOG(ERR, "VLAN filter offload not supported\n");
1715 if (mask & ETH_VLAN_EXTEND_MASK) {
1716 if (eth_dev->data->dev_conf.rxmode.hw_vlan_extend)
1717 PMD_DRV_LOG(ERR, "VLAN extend offload not supported\n");
1721 RTE_PMD_REGISTER_PCI(net_avp, rte_avp_pmd.pci_drv);
1722 RTE_PMD_REGISTER_PCI_TABLE(net_avp, pci_id_avp_map);