net/avp: add queue setup and release
[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 #include <rte_io.h>
55
56 #include "rte_avp_common.h"
57 #include "rte_avp_fifo.h"
58
59 #include "avp_logs.h"
60
61
62
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,
70                                   uint16_t rx_queue_id,
71                                   uint16_t nb_rx_desc,
72                                   unsigned int socket_id,
73                                   const struct rte_eth_rxconf *rx_conf,
74                                   struct rte_mempool *pool);
75
76 static int avp_dev_tx_queue_setup(struct rte_eth_dev *dev,
77                                   uint16_t tx_queue_id,
78                                   uint16_t nb_tx_desc,
79                                   unsigned int socket_id,
80                                   const struct rte_eth_txconf *tx_conf);
81
82 static void avp_dev_rx_queue_release(void *rxq);
83 static void avp_dev_tx_queue_release(void *txq);
84 #define AVP_DEV_TO_PCI(eth_dev) RTE_DEV_TO_PCI((eth_dev)->device)
85
86
87 #define AVP_MAX_MAC_ADDRS 1
88 #define AVP_MIN_RX_BUFSIZE ETHER_MIN_LEN
89
90
91 /*
92  * Defines the number of microseconds to wait before checking the response
93  * queue for completion.
94  */
95 #define AVP_REQUEST_DELAY_USECS (5000)
96
97 /*
98  * Defines the number times to check the response queue for completion before
99  * declaring a timeout.
100  */
101 #define AVP_MAX_REQUEST_RETRY (100)
102
103 /* Defines the current PCI driver version number */
104 #define AVP_DPDK_DRIVER_VERSION RTE_AVP_CURRENT_GUEST_VERSION
105
106 /*
107  * The set of PCI devices this driver supports
108  */
109 static const struct rte_pci_id pci_id_avp_map[] = {
110         { .vendor_id = RTE_AVP_PCI_VENDOR_ID,
111           .device_id = RTE_AVP_PCI_DEVICE_ID,
112           .subsystem_vendor_id = RTE_AVP_PCI_SUB_VENDOR_ID,
113           .subsystem_device_id = RTE_AVP_PCI_SUB_DEVICE_ID,
114           .class_id = RTE_CLASS_ANY_ID,
115         },
116
117         { .vendor_id = 0, /* sentinel */
118         },
119 };
120
121 /*
122  * dev_ops for avp, bare necessities for basic operation
123  */
124 static const struct eth_dev_ops avp_eth_dev_ops = {
125         .dev_configure       = avp_dev_configure,
126         .dev_infos_get       = avp_dev_info_get,
127         .vlan_offload_set    = avp_vlan_offload_set,
128         .link_update         = avp_dev_link_update,
129         .rx_queue_setup      = avp_dev_rx_queue_setup,
130         .rx_queue_release    = avp_dev_rx_queue_release,
131         .tx_queue_setup      = avp_dev_tx_queue_setup,
132         .tx_queue_release    = avp_dev_tx_queue_release,
133 };
134
135 /**@{ AVP device flags */
136 #define AVP_F_PROMISC (1 << 1)
137 #define AVP_F_CONFIGURED (1 << 2)
138 #define AVP_F_LINKUP (1 << 3)
139 /**@} */
140
141 /* Ethernet device validation marker */
142 #define AVP_ETHDEV_MAGIC 0x92972862
143
144 /*
145  * Defines the AVP device attributes which are attached to an RTE ethernet
146  * device
147  */
148 struct avp_dev {
149         uint32_t magic; /**< Memory validation marker */
150         uint64_t device_id; /**< Unique system identifier */
151         struct ether_addr ethaddr; /**< Host specified MAC address */
152         struct rte_eth_dev_data *dev_data;
153         /**< Back pointer to ethernet device data */
154         volatile uint32_t flags; /**< Device operational flags */
155         uint8_t port_id; /**< Ethernet port identifier */
156         struct rte_mempool *pool; /**< pkt mbuf mempool */
157         unsigned int guest_mbuf_size; /**< local pool mbuf size */
158         unsigned int host_mbuf_size; /**< host mbuf size */
159         unsigned int max_rx_pkt_len; /**< maximum receive unit */
160         uint32_t host_features; /**< Supported feature bitmap */
161         uint32_t features; /**< Enabled feature bitmap */
162         unsigned int num_tx_queues; /**< Negotiated number of transmit queues */
163         unsigned int max_tx_queues; /**< Maximum number of transmit queues */
164         unsigned int num_rx_queues; /**< Negotiated number of receive queues */
165         unsigned int max_rx_queues; /**< Maximum number of receive queues */
166
167         struct rte_avp_fifo *tx_q[RTE_AVP_MAX_QUEUES]; /**< TX queue */
168         struct rte_avp_fifo *rx_q[RTE_AVP_MAX_QUEUES]; /**< RX queue */
169         struct rte_avp_fifo *alloc_q[RTE_AVP_MAX_QUEUES];
170         /**< Allocated mbufs queue */
171         struct rte_avp_fifo *free_q[RTE_AVP_MAX_QUEUES];
172         /**< To be freed mbufs queue */
173
174         /* For request & response */
175         struct rte_avp_fifo *req_q; /**< Request queue */
176         struct rte_avp_fifo *resp_q; /**< Response queue */
177         void *host_sync_addr; /**< (host) Req/Resp Mem address */
178         void *sync_addr; /**< Req/Resp Mem address */
179         void *host_mbuf_addr; /**< (host) MBUF pool start address */
180         void *mbuf_addr; /**< MBUF pool start address */
181 } __rte_cache_aligned;
182
183 /* RTE ethernet private data */
184 struct avp_adapter {
185         struct avp_dev avp;
186 } __rte_cache_aligned;
187
188
189 /* 32-bit MMIO register write */
190 #define AVP_WRITE32(_value, _addr) rte_write32_relaxed((_value), (_addr))
191
192 /* 32-bit MMIO register read */
193 #define AVP_READ32(_addr) rte_read32_relaxed((_addr))
194
195 /* Macro to cast the ethernet device private data to a AVP object */
196 #define AVP_DEV_PRIVATE_TO_HW(adapter) \
197         (&((struct avp_adapter *)adapter)->avp)
198
199 /*
200  * Defines the structure of a AVP device queue for the purpose of handling the
201  * receive and transmit burst callback functions
202  */
203 struct avp_queue {
204         struct rte_eth_dev_data *dev_data;
205         /**< Backpointer to ethernet device data */
206         struct avp_dev *avp; /**< Backpointer to AVP device */
207         uint16_t queue_id;
208         /**< Queue identifier used for indexing current queue */
209         uint16_t queue_base;
210         /**< Base queue identifier for queue servicing */
211         uint16_t queue_limit;
212         /**< Maximum queue identifier for queue servicing */
213
214         uint64_t packets;
215         uint64_t bytes;
216         uint64_t errors;
217 };
218
219 /* send a request and wait for a response
220  *
221  * @warning must be called while holding the avp->lock spinlock.
222  */
223 static int
224 avp_dev_process_request(struct avp_dev *avp, struct rte_avp_request *request)
225 {
226         unsigned int retry = AVP_MAX_REQUEST_RETRY;
227         void *resp_addr = NULL;
228         unsigned int count;
229         int ret;
230
231         PMD_DRV_LOG(DEBUG, "Sending request %u to host\n", request->req_id);
232
233         request->result = -ENOTSUP;
234
235         /* Discard any stale responses before starting a new request */
236         while (avp_fifo_get(avp->resp_q, (void **)&resp_addr, 1))
237                 PMD_DRV_LOG(DEBUG, "Discarding stale response\n");
238
239         rte_memcpy(avp->sync_addr, request, sizeof(*request));
240         count = avp_fifo_put(avp->req_q, &avp->host_sync_addr, 1);
241         if (count < 1) {
242                 PMD_DRV_LOG(ERR, "Cannot send request %u to host\n",
243                             request->req_id);
244                 ret = -EBUSY;
245                 goto done;
246         }
247
248         while (retry--) {
249                 /* wait for a response */
250                 usleep(AVP_REQUEST_DELAY_USECS);
251
252                 count = avp_fifo_count(avp->resp_q);
253                 if (count >= 1) {
254                         /* response received */
255                         break;
256                 }
257
258                 if ((count < 1) && (retry == 0)) {
259                         PMD_DRV_LOG(ERR, "Timeout while waiting for a response for %u\n",
260                                     request->req_id);
261                         ret = -ETIME;
262                         goto done;
263                 }
264         }
265
266         /* retrieve the response */
267         count = avp_fifo_get(avp->resp_q, (void **)&resp_addr, 1);
268         if ((count != 1) || (resp_addr != avp->host_sync_addr)) {
269                 PMD_DRV_LOG(ERR, "Invalid response from host, count=%u resp=%p host_sync_addr=%p\n",
270                             count, resp_addr, avp->host_sync_addr);
271                 ret = -ENODATA;
272                 goto done;
273         }
274
275         /* copy to user buffer */
276         rte_memcpy(request, avp->sync_addr, sizeof(*request));
277         ret = 0;
278
279         PMD_DRV_LOG(DEBUG, "Result %d received for request %u\n",
280                     request->result, request->req_id);
281
282 done:
283         return ret;
284 }
285
286 static int
287 avp_dev_ctrl_set_config(struct rte_eth_dev *eth_dev,
288                         struct rte_avp_device_config *config)
289 {
290         struct avp_dev *avp = AVP_DEV_PRIVATE_TO_HW(eth_dev->data->dev_private);
291         struct rte_avp_request request;
292         int ret;
293
294         /* setup a configure request */
295         memset(&request, 0, sizeof(request));
296         request.req_id = RTE_AVP_REQ_CFG_DEVICE;
297         memcpy(&request.config, config, sizeof(request.config));
298
299         ret = avp_dev_process_request(avp, &request);
300
301         return ret == 0 ? request.result : ret;
302 }
303
304 /* translate from host physical address to guest virtual address */
305 static void *
306 avp_dev_translate_address(struct rte_eth_dev *eth_dev,
307                           phys_addr_t host_phys_addr)
308 {
309         struct rte_pci_device *pci_dev = AVP_DEV_TO_PCI(eth_dev);
310         struct rte_mem_resource *resource;
311         struct rte_avp_memmap_info *info;
312         struct rte_avp_memmap *map;
313         off_t offset;
314         void *addr;
315         unsigned int i;
316
317         addr = pci_dev->mem_resource[RTE_AVP_PCI_MEMORY_BAR].addr;
318         resource = &pci_dev->mem_resource[RTE_AVP_PCI_MEMMAP_BAR];
319         info = (struct rte_avp_memmap_info *)resource->addr;
320
321         offset = 0;
322         for (i = 0; i < info->nb_maps; i++) {
323                 /* search all segments looking for a matching address */
324                 map = &info->maps[i];
325
326                 if ((host_phys_addr >= map->phys_addr) &&
327                         (host_phys_addr < (map->phys_addr + map->length))) {
328                         /* address is within this segment */
329                         offset += (host_phys_addr - map->phys_addr);
330                         addr = RTE_PTR_ADD(addr, offset);
331
332                         PMD_DRV_LOG(DEBUG, "Translating host physical 0x%" PRIx64 " to guest virtual 0x%p\n",
333                                     host_phys_addr, addr);
334
335                         return addr;
336                 }
337                 offset += map->length;
338         }
339
340         return NULL;
341 }
342
343 /* verify that the incoming device version is compatible with our version */
344 static int
345 avp_dev_version_check(uint32_t version)
346 {
347         uint32_t driver = RTE_AVP_STRIP_MINOR_VERSION(AVP_DPDK_DRIVER_VERSION);
348         uint32_t device = RTE_AVP_STRIP_MINOR_VERSION(version);
349
350         if (device <= driver) {
351                 /* the host driver version is less than or equal to ours */
352                 return 0;
353         }
354
355         return 1;
356 }
357
358 /* verify that memory regions have expected version and validation markers */
359 static int
360 avp_dev_check_regions(struct rte_eth_dev *eth_dev)
361 {
362         struct rte_pci_device *pci_dev = AVP_DEV_TO_PCI(eth_dev);
363         struct rte_avp_memmap_info *memmap;
364         struct rte_avp_device_info *info;
365         struct rte_mem_resource *resource;
366         unsigned int i;
367
368         /* Dump resource info for debug */
369         for (i = 0; i < PCI_MAX_RESOURCE; i++) {
370                 resource = &pci_dev->mem_resource[i];
371                 if ((resource->phys_addr == 0) || (resource->len == 0))
372                         continue;
373
374                 PMD_DRV_LOG(DEBUG, "resource[%u]: phys=0x%" PRIx64 " len=%" PRIu64 " addr=%p\n",
375                             i, resource->phys_addr,
376                             resource->len, resource->addr);
377
378                 switch (i) {
379                 case RTE_AVP_PCI_MEMMAP_BAR:
380                         memmap = (struct rte_avp_memmap_info *)resource->addr;
381                         if ((memmap->magic != RTE_AVP_MEMMAP_MAGIC) ||
382                             (memmap->version != RTE_AVP_MEMMAP_VERSION)) {
383                                 PMD_DRV_LOG(ERR, "Invalid memmap magic 0x%08x and version %u\n",
384                                             memmap->magic, memmap->version);
385                                 return -EINVAL;
386                         }
387                         break;
388
389                 case RTE_AVP_PCI_DEVICE_BAR:
390                         info = (struct rte_avp_device_info *)resource->addr;
391                         if ((info->magic != RTE_AVP_DEVICE_MAGIC) ||
392                             avp_dev_version_check(info->version)) {
393                                 PMD_DRV_LOG(ERR, "Invalid device info magic 0x%08x or version 0x%08x > 0x%08x\n",
394                                             info->magic, info->version,
395                                             AVP_DPDK_DRIVER_VERSION);
396                                 return -EINVAL;
397                         }
398                         break;
399
400                 case RTE_AVP_PCI_MEMORY_BAR:
401                 case RTE_AVP_PCI_MMIO_BAR:
402                         if (resource->addr == NULL) {
403                                 PMD_DRV_LOG(ERR, "Missing address space for BAR%u\n",
404                                             i);
405                                 return -EINVAL;
406                         }
407                         break;
408
409                 case RTE_AVP_PCI_MSIX_BAR:
410                 default:
411                         /* no validation required */
412                         break;
413                 }
414         }
415
416         return 0;
417 }
418
419 static void
420 _avp_set_rx_queue_mappings(struct rte_eth_dev *eth_dev, uint16_t rx_queue_id)
421 {
422         struct avp_dev *avp =
423                 AVP_DEV_PRIVATE_TO_HW(eth_dev->data->dev_private);
424         struct avp_queue *rxq;
425         uint16_t queue_count;
426         uint16_t remainder;
427
428         rxq = (struct avp_queue *)eth_dev->data->rx_queues[rx_queue_id];
429
430         /*
431          * Must map all AVP fifos as evenly as possible between the configured
432          * device queues.  Each device queue will service a subset of the AVP
433          * fifos. If there is an odd number of device queues the first set of
434          * device queues will get the extra AVP fifos.
435          */
436         queue_count = avp->num_rx_queues / eth_dev->data->nb_rx_queues;
437         remainder = avp->num_rx_queues % eth_dev->data->nb_rx_queues;
438         if (rx_queue_id < remainder) {
439                 /* these queues must service one extra FIFO */
440                 rxq->queue_base = rx_queue_id * (queue_count + 1);
441                 rxq->queue_limit = rxq->queue_base + (queue_count + 1) - 1;
442         } else {
443                 /* these queues service the regular number of FIFO */
444                 rxq->queue_base = ((remainder * (queue_count + 1)) +
445                                    ((rx_queue_id - remainder) * queue_count));
446                 rxq->queue_limit = rxq->queue_base + queue_count - 1;
447         }
448
449         PMD_DRV_LOG(DEBUG, "rxq %u at %p base %u limit %u\n",
450                     rx_queue_id, rxq, rxq->queue_base, rxq->queue_limit);
451
452         rxq->queue_id = rxq->queue_base;
453 }
454
455 static void
456 _avp_set_queue_counts(struct rte_eth_dev *eth_dev)
457 {
458         struct rte_pci_device *pci_dev = AVP_DEV_TO_PCI(eth_dev);
459         struct avp_dev *avp = AVP_DEV_PRIVATE_TO_HW(eth_dev->data->dev_private);
460         struct rte_avp_device_info *host_info;
461         void *addr;
462
463         addr = pci_dev->mem_resource[RTE_AVP_PCI_DEVICE_BAR].addr;
464         host_info = (struct rte_avp_device_info *)addr;
465
466         /*
467          * the transmit direction is not negotiated beyond respecting the max
468          * number of queues because the host can handle arbitrary guest tx
469          * queues (host rx queues).
470          */
471         avp->num_tx_queues = eth_dev->data->nb_tx_queues;
472
473         /*
474          * the receive direction is more restrictive.  The host requires a
475          * minimum number of guest rx queues (host tx queues) therefore
476          * negotiate a value that is at least as large as the host minimum
477          * requirement.  If the host and guest values are not identical then a
478          * mapping will be established in the receive_queue_setup function.
479          */
480         avp->num_rx_queues = RTE_MAX(host_info->min_rx_queues,
481                                      eth_dev->data->nb_rx_queues);
482
483         PMD_DRV_LOG(DEBUG, "Requesting %u Tx and %u Rx queues from host\n",
484                     avp->num_tx_queues, avp->num_rx_queues);
485 }
486
487 /*
488  * create a AVP device using the supplied device info by first translating it
489  * to guest address space(s).
490  */
491 static int
492 avp_dev_create(struct rte_pci_device *pci_dev,
493                struct rte_eth_dev *eth_dev)
494 {
495         struct avp_dev *avp = AVP_DEV_PRIVATE_TO_HW(eth_dev->data->dev_private);
496         struct rte_avp_device_info *host_info;
497         struct rte_mem_resource *resource;
498         unsigned int i;
499
500         resource = &pci_dev->mem_resource[RTE_AVP_PCI_DEVICE_BAR];
501         if (resource->addr == NULL) {
502                 PMD_DRV_LOG(ERR, "BAR%u is not mapped\n",
503                             RTE_AVP_PCI_DEVICE_BAR);
504                 return -EFAULT;
505         }
506         host_info = (struct rte_avp_device_info *)resource->addr;
507
508         if ((host_info->magic != RTE_AVP_DEVICE_MAGIC) ||
509                 avp_dev_version_check(host_info->version)) {
510                 PMD_DRV_LOG(ERR, "Invalid AVP PCI device, magic 0x%08x version 0x%08x > 0x%08x\n",
511                             host_info->magic, host_info->version,
512                             AVP_DPDK_DRIVER_VERSION);
513                 return -EINVAL;
514         }
515
516         PMD_DRV_LOG(DEBUG, "AVP host device is v%u.%u.%u\n",
517                     RTE_AVP_GET_RELEASE_VERSION(host_info->version),
518                     RTE_AVP_GET_MAJOR_VERSION(host_info->version),
519                     RTE_AVP_GET_MINOR_VERSION(host_info->version));
520
521         PMD_DRV_LOG(DEBUG, "AVP host supports %u to %u TX queue(s)\n",
522                     host_info->min_tx_queues, host_info->max_tx_queues);
523         PMD_DRV_LOG(DEBUG, "AVP host supports %u to %u RX queue(s)\n",
524                     host_info->min_rx_queues, host_info->max_rx_queues);
525         PMD_DRV_LOG(DEBUG, "AVP host supports features 0x%08x\n",
526                     host_info->features);
527
528         if (avp->magic != AVP_ETHDEV_MAGIC) {
529                 /*
530                  * First time initialization (i.e., not during a VM
531                  * migration)
532                  */
533                 memset(avp, 0, sizeof(*avp));
534                 avp->magic = AVP_ETHDEV_MAGIC;
535                 avp->dev_data = eth_dev->data;
536                 avp->port_id = eth_dev->data->port_id;
537                 avp->host_mbuf_size = host_info->mbuf_size;
538                 avp->host_features = host_info->features;
539                 memcpy(&avp->ethaddr.addr_bytes[0],
540                        host_info->ethaddr, ETHER_ADDR_LEN);
541                 /* adjust max values to not exceed our max */
542                 avp->max_tx_queues =
543                         RTE_MIN(host_info->max_tx_queues, RTE_AVP_MAX_QUEUES);
544                 avp->max_rx_queues =
545                         RTE_MIN(host_info->max_rx_queues, RTE_AVP_MAX_QUEUES);
546         } else {
547                 /* Re-attaching during migration */
548
549                 /* TODO... requires validation of host values */
550                 if ((host_info->features & avp->features) != avp->features) {
551                         PMD_DRV_LOG(ERR, "AVP host features mismatched; 0x%08x, host=0x%08x\n",
552                                     avp->features, host_info->features);
553                         /* this should not be possible; continue for now */
554                 }
555         }
556
557         /* the device id is allowed to change over migrations */
558         avp->device_id = host_info->device_id;
559
560         /* translate incoming host addresses to guest address space */
561         PMD_DRV_LOG(DEBUG, "AVP first host tx queue at 0x%" PRIx64 "\n",
562                     host_info->tx_phys);
563         PMD_DRV_LOG(DEBUG, "AVP first host alloc queue at 0x%" PRIx64 "\n",
564                     host_info->alloc_phys);
565         for (i = 0; i < avp->max_tx_queues; i++) {
566                 avp->tx_q[i] = avp_dev_translate_address(eth_dev,
567                         host_info->tx_phys + (i * host_info->tx_size));
568
569                 avp->alloc_q[i] = avp_dev_translate_address(eth_dev,
570                         host_info->alloc_phys + (i * host_info->alloc_size));
571         }
572
573         PMD_DRV_LOG(DEBUG, "AVP first host rx queue at 0x%" PRIx64 "\n",
574                     host_info->rx_phys);
575         PMD_DRV_LOG(DEBUG, "AVP first host free queue at 0x%" PRIx64 "\n",
576                     host_info->free_phys);
577         for (i = 0; i < avp->max_rx_queues; i++) {
578                 avp->rx_q[i] = avp_dev_translate_address(eth_dev,
579                         host_info->rx_phys + (i * host_info->rx_size));
580                 avp->free_q[i] = avp_dev_translate_address(eth_dev,
581                         host_info->free_phys + (i * host_info->free_size));
582         }
583
584         PMD_DRV_LOG(DEBUG, "AVP host request queue at 0x%" PRIx64 "\n",
585                     host_info->req_phys);
586         PMD_DRV_LOG(DEBUG, "AVP host response queue at 0x%" PRIx64 "\n",
587                     host_info->resp_phys);
588         PMD_DRV_LOG(DEBUG, "AVP host sync address at 0x%" PRIx64 "\n",
589                     host_info->sync_phys);
590         PMD_DRV_LOG(DEBUG, "AVP host mbuf address at 0x%" PRIx64 "\n",
591                     host_info->mbuf_phys);
592         avp->req_q = avp_dev_translate_address(eth_dev, host_info->req_phys);
593         avp->resp_q = avp_dev_translate_address(eth_dev, host_info->resp_phys);
594         avp->sync_addr =
595                 avp_dev_translate_address(eth_dev, host_info->sync_phys);
596         avp->mbuf_addr =
597                 avp_dev_translate_address(eth_dev, host_info->mbuf_phys);
598
599         /*
600          * store the host mbuf virtual address so that we can calculate
601          * relative offsets for each mbuf as they are processed
602          */
603         avp->host_mbuf_addr = host_info->mbuf_va;
604         avp->host_sync_addr = host_info->sync_va;
605
606         /*
607          * store the maximum packet length that is supported by the host.
608          */
609         avp->max_rx_pkt_len = host_info->max_rx_pkt_len;
610         PMD_DRV_LOG(DEBUG, "AVP host max receive packet length is %u\n",
611                                 host_info->max_rx_pkt_len);
612
613         return 0;
614 }
615
616 /*
617  * This function is based on probe() function in avp_pci.c
618  * It returns 0 on success.
619  */
620 static int
621 eth_avp_dev_init(struct rte_eth_dev *eth_dev)
622 {
623         struct avp_dev *avp =
624                 AVP_DEV_PRIVATE_TO_HW(eth_dev->data->dev_private);
625         struct rte_pci_device *pci_dev;
626         int ret;
627
628         pci_dev = AVP_DEV_TO_PCI(eth_dev);
629         eth_dev->dev_ops = &avp_eth_dev_ops;
630
631         if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
632                 /*
633                  * no setup required on secondary processes.  All data is saved
634                  * in dev_private by the primary process. All resource should
635                  * be mapped to the same virtual address so all pointers should
636                  * be valid.
637                  */
638                 return 0;
639         }
640
641         rte_eth_copy_pci_info(eth_dev, pci_dev);
642
643         eth_dev->data->dev_flags |= RTE_ETH_DEV_DETACHABLE;
644
645         /* Check BAR resources */
646         ret = avp_dev_check_regions(eth_dev);
647         if (ret < 0) {
648                 PMD_DRV_LOG(ERR, "Failed to validate BAR resources, ret=%d\n",
649                             ret);
650                 return ret;
651         }
652
653         /* Handle each subtype */
654         ret = avp_dev_create(pci_dev, eth_dev);
655         if (ret < 0) {
656                 PMD_DRV_LOG(ERR, "Failed to create device, ret=%d\n", ret);
657                 return ret;
658         }
659
660         /* Allocate memory for storing MAC addresses */
661         eth_dev->data->mac_addrs = rte_zmalloc("avp_ethdev", ETHER_ADDR_LEN, 0);
662         if (eth_dev->data->mac_addrs == NULL) {
663                 PMD_DRV_LOG(ERR, "Failed to allocate %d bytes needed to store MAC addresses\n",
664                             ETHER_ADDR_LEN);
665                 return -ENOMEM;
666         }
667
668         /* Get a mac from device config */
669         ether_addr_copy(&avp->ethaddr, &eth_dev->data->mac_addrs[0]);
670
671         return 0;
672 }
673
674 static int
675 eth_avp_dev_uninit(struct rte_eth_dev *eth_dev)
676 {
677         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
678                 return -EPERM;
679
680         if (eth_dev->data == NULL)
681                 return 0;
682
683         if (eth_dev->data->mac_addrs != NULL) {
684                 rte_free(eth_dev->data->mac_addrs);
685                 eth_dev->data->mac_addrs = NULL;
686         }
687
688         return 0;
689 }
690
691
692 static struct eth_driver rte_avp_pmd = {
693         {
694                 .id_table = pci_id_avp_map,
695                 .drv_flags = RTE_PCI_DRV_NEED_MAPPING,
696                 .probe = rte_eth_dev_pci_probe,
697                 .remove = rte_eth_dev_pci_remove,
698         },
699         .eth_dev_init = eth_avp_dev_init,
700         .eth_dev_uninit = eth_avp_dev_uninit,
701         .dev_private_size = sizeof(struct avp_adapter),
702 };
703
704 static int
705 avp_dev_rx_queue_setup(struct rte_eth_dev *eth_dev,
706                        uint16_t rx_queue_id,
707                        uint16_t nb_rx_desc,
708                        unsigned int socket_id,
709                        const struct rte_eth_rxconf *rx_conf,
710                        struct rte_mempool *pool)
711 {
712         struct avp_dev *avp = AVP_DEV_PRIVATE_TO_HW(eth_dev->data->dev_private);
713         struct rte_pktmbuf_pool_private *mbp_priv;
714         struct avp_queue *rxq;
715
716         if (rx_queue_id >= eth_dev->data->nb_rx_queues) {
717                 PMD_DRV_LOG(ERR, "RX queue id is out of range: rx_queue_id=%u, nb_rx_queues=%u\n",
718                             rx_queue_id, eth_dev->data->nb_rx_queues);
719                 return -EINVAL;
720         }
721
722         /* Save mbuf pool pointer */
723         avp->pool = pool;
724
725         /* Save the local mbuf size */
726         mbp_priv = rte_mempool_get_priv(pool);
727         avp->guest_mbuf_size = (uint16_t)(mbp_priv->mbuf_data_room_size);
728         avp->guest_mbuf_size -= RTE_PKTMBUF_HEADROOM;
729
730         PMD_DRV_LOG(DEBUG, "AVP max_rx_pkt_len=(%u,%u) mbuf_size=(%u,%u)\n",
731                     avp->max_rx_pkt_len,
732                     eth_dev->data->dev_conf.rxmode.max_rx_pkt_len,
733                     avp->host_mbuf_size,
734                     avp->guest_mbuf_size);
735
736         /* allocate a queue object */
737         rxq = rte_zmalloc_socket("ethdev RX queue", sizeof(struct avp_queue),
738                                  RTE_CACHE_LINE_SIZE, socket_id);
739         if (rxq == NULL) {
740                 PMD_DRV_LOG(ERR, "Failed to allocate new Rx queue object\n");
741                 return -ENOMEM;
742         }
743
744         /* save back pointers to AVP and Ethernet devices */
745         rxq->avp = avp;
746         rxq->dev_data = eth_dev->data;
747         eth_dev->data->rx_queues[rx_queue_id] = (void *)rxq;
748
749         /* setup the queue receive mapping for the current queue. */
750         _avp_set_rx_queue_mappings(eth_dev, rx_queue_id);
751
752         PMD_DRV_LOG(DEBUG, "Rx queue %u setup at %p\n", rx_queue_id, rxq);
753
754         (void)nb_rx_desc;
755         (void)rx_conf;
756         return 0;
757 }
758
759 static int
760 avp_dev_tx_queue_setup(struct rte_eth_dev *eth_dev,
761                        uint16_t tx_queue_id,
762                        uint16_t nb_tx_desc,
763                        unsigned int socket_id,
764                        const struct rte_eth_txconf *tx_conf)
765 {
766         struct avp_dev *avp = AVP_DEV_PRIVATE_TO_HW(eth_dev->data->dev_private);
767         struct avp_queue *txq;
768
769         if (tx_queue_id >= eth_dev->data->nb_tx_queues) {
770                 PMD_DRV_LOG(ERR, "TX queue id is out of range: tx_queue_id=%u, nb_tx_queues=%u\n",
771                             tx_queue_id, eth_dev->data->nb_tx_queues);
772                 return -EINVAL;
773         }
774
775         /* allocate a queue object */
776         txq = rte_zmalloc_socket("ethdev TX queue", sizeof(struct avp_queue),
777                                  RTE_CACHE_LINE_SIZE, socket_id);
778         if (txq == NULL) {
779                 PMD_DRV_LOG(ERR, "Failed to allocate new Tx queue object\n");
780                 return -ENOMEM;
781         }
782
783         /* only the configured set of transmit queues are used */
784         txq->queue_id = tx_queue_id;
785         txq->queue_base = tx_queue_id;
786         txq->queue_limit = tx_queue_id;
787
788         /* save back pointers to AVP and Ethernet devices */
789         txq->avp = avp;
790         txq->dev_data = eth_dev->data;
791         eth_dev->data->tx_queues[tx_queue_id] = (void *)txq;
792
793         PMD_DRV_LOG(DEBUG, "Tx queue %u setup at %p\n", tx_queue_id, txq);
794
795         (void)nb_tx_desc;
796         (void)tx_conf;
797         return 0;
798 }
799
800 static void
801 avp_dev_rx_queue_release(void *rx_queue)
802 {
803         struct avp_queue *rxq = (struct avp_queue *)rx_queue;
804         struct avp_dev *avp = rxq->avp;
805         struct rte_eth_dev_data *data = avp->dev_data;
806         unsigned int i;
807
808         for (i = 0; i < avp->num_rx_queues; i++) {
809                 if (data->rx_queues[i] == rxq)
810                         data->rx_queues[i] = NULL;
811         }
812 }
813
814 static void
815 avp_dev_tx_queue_release(void *tx_queue)
816 {
817         struct avp_queue *txq = (struct avp_queue *)tx_queue;
818         struct avp_dev *avp = txq->avp;
819         struct rte_eth_dev_data *data = avp->dev_data;
820         unsigned int i;
821
822         for (i = 0; i < avp->num_tx_queues; i++) {
823                 if (data->tx_queues[i] == txq)
824                         data->tx_queues[i] = NULL;
825         }
826 }
827
828 static int
829 avp_dev_configure(struct rte_eth_dev *eth_dev)
830 {
831         struct rte_pci_device *pci_dev = AVP_DEV_TO_PCI(eth_dev);
832         struct avp_dev *avp = AVP_DEV_PRIVATE_TO_HW(eth_dev->data->dev_private);
833         struct rte_avp_device_info *host_info;
834         struct rte_avp_device_config config;
835         int mask = 0;
836         void *addr;
837         int ret;
838
839         addr = pci_dev->mem_resource[RTE_AVP_PCI_DEVICE_BAR].addr;
840         host_info = (struct rte_avp_device_info *)addr;
841
842         /* Setup required number of queues */
843         _avp_set_queue_counts(eth_dev);
844
845         mask = (ETH_VLAN_STRIP_MASK |
846                 ETH_VLAN_FILTER_MASK |
847                 ETH_VLAN_EXTEND_MASK);
848         avp_vlan_offload_set(eth_dev, mask);
849
850         /* update device config */
851         memset(&config, 0, sizeof(config));
852         config.device_id = host_info->device_id;
853         config.driver_type = RTE_AVP_DRIVER_TYPE_DPDK;
854         config.driver_version = AVP_DPDK_DRIVER_VERSION;
855         config.features = avp->features;
856         config.num_tx_queues = avp->num_tx_queues;
857         config.num_rx_queues = avp->num_rx_queues;
858
859         ret = avp_dev_ctrl_set_config(eth_dev, &config);
860         if (ret < 0) {
861                 PMD_DRV_LOG(ERR, "Config request failed by host, ret=%d\n",
862                             ret);
863                 goto unlock;
864         }
865
866         avp->flags |= AVP_F_CONFIGURED;
867         ret = 0;
868
869 unlock:
870         return ret;
871 }
872
873
874 static int
875 avp_dev_link_update(struct rte_eth_dev *eth_dev,
876                                         __rte_unused int wait_to_complete)
877 {
878         struct avp_dev *avp = AVP_DEV_PRIVATE_TO_HW(eth_dev->data->dev_private);
879         struct rte_eth_link *link = &eth_dev->data->dev_link;
880
881         link->link_speed = ETH_SPEED_NUM_10G;
882         link->link_duplex = ETH_LINK_FULL_DUPLEX;
883         link->link_status = !!(avp->flags & AVP_F_LINKUP);
884
885         return -1;
886 }
887
888
889 static void
890 avp_dev_info_get(struct rte_eth_dev *eth_dev,
891                  struct rte_eth_dev_info *dev_info)
892 {
893         struct avp_dev *avp = AVP_DEV_PRIVATE_TO_HW(eth_dev->data->dev_private);
894
895         dev_info->driver_name = "rte_avp_pmd";
896         dev_info->pci_dev = RTE_DEV_TO_PCI(eth_dev->device);
897         dev_info->max_rx_queues = avp->max_rx_queues;
898         dev_info->max_tx_queues = avp->max_tx_queues;
899         dev_info->min_rx_bufsize = AVP_MIN_RX_BUFSIZE;
900         dev_info->max_rx_pktlen = avp->max_rx_pkt_len;
901         dev_info->max_mac_addrs = AVP_MAX_MAC_ADDRS;
902         if (avp->host_features & RTE_AVP_FEATURE_VLAN_OFFLOAD) {
903                 dev_info->rx_offload_capa = DEV_RX_OFFLOAD_VLAN_STRIP;
904                 dev_info->tx_offload_capa = DEV_TX_OFFLOAD_VLAN_INSERT;
905         }
906 }
907
908 static void
909 avp_vlan_offload_set(struct rte_eth_dev *eth_dev, int mask)
910 {
911         struct avp_dev *avp = AVP_DEV_PRIVATE_TO_HW(eth_dev->data->dev_private);
912
913         if (mask & ETH_VLAN_STRIP_MASK) {
914                 if (avp->host_features & RTE_AVP_FEATURE_VLAN_OFFLOAD) {
915                         if (eth_dev->data->dev_conf.rxmode.hw_vlan_strip)
916                                 avp->features |= RTE_AVP_FEATURE_VLAN_OFFLOAD;
917                         else
918                                 avp->features &= ~RTE_AVP_FEATURE_VLAN_OFFLOAD;
919                 } else {
920                         PMD_DRV_LOG(ERR, "VLAN strip offload not supported\n");
921                 }
922         }
923
924         if (mask & ETH_VLAN_FILTER_MASK) {
925                 if (eth_dev->data->dev_conf.rxmode.hw_vlan_filter)
926                         PMD_DRV_LOG(ERR, "VLAN filter offload not supported\n");
927         }
928
929         if (mask & ETH_VLAN_EXTEND_MASK) {
930                 if (eth_dev->data->dev_conf.rxmode.hw_vlan_extend)
931                         PMD_DRV_LOG(ERR, "VLAN extend offload not supported\n");
932         }
933 }
934
935 RTE_PMD_REGISTER_PCI(net_avp, rte_avp_pmd.pci_drv);
936 RTE_PMD_REGISTER_PCI_TABLE(net_avp, pci_id_avp_map);