replace bzero with memset
[dpdk.git] / drivers / net / xenvirt / rte_eth_xenvirt.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2015 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #include <stdint.h>
35 #include <unistd.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <sys/types.h>
39 #include <sys/mman.h>
40 #include <errno.h>
41 #include <sys/user.h>
42 #include <linux/binfmts.h>
43 #include <xen/xen-compat.h>
44 #if __XEN_LATEST_INTERFACE_VERSION__ < 0x00040200
45 #include <xs.h>
46 #else
47 #include <xenstore.h>
48 #endif
49 #include <linux/virtio_ring.h>
50
51 #include <rte_mbuf.h>
52 #include <rte_ethdev.h>
53 #include <rte_malloc.h>
54 #include <rte_memcpy.h>
55 #include <rte_string_fns.h>
56 #include <rte_dev.h>
57 #include <cmdline_parse.h>
58 #include <cmdline_parse_etheraddr.h>
59
60 #include "rte_xen_lib.h"
61 #include "virtqueue.h"
62 #include "rte_eth_xenvirt.h"
63
64 #define VQ_DESC_NUM 256
65 #define VIRTIO_MBUF_BURST_SZ 64
66
67 /* virtio_idx is increased after new device is created.*/
68 static int virtio_idx = 0;
69
70 static const char *drivername = "xen virtio PMD";
71
72 static struct rte_eth_link pmd_link = {
73                 .link_speed = 10000,
74                 .link_duplex = ETH_LINK_FULL_DUPLEX,
75                 .link_status = 0
76 };
77
78 static void
79 eth_xenvirt_free_queues(struct rte_eth_dev *dev);
80
81 static inline struct rte_mbuf *
82 rte_rxmbuf_alloc(struct rte_mempool *mp)
83 {
84         struct rte_mbuf *m;
85
86         m = __rte_mbuf_raw_alloc(mp);
87         __rte_mbuf_sanity_check_raw(m, 0);
88
89         return m;
90 }
91
92
93 static uint16_t
94 eth_xenvirt_rx(void *q, struct rte_mbuf **rx_pkts, uint16_t nb_pkts)
95 {
96         struct virtqueue *rxvq = q;
97         struct rte_mbuf *rxm, *new_mbuf;
98         uint16_t nb_used, num;
99         uint32_t len[VIRTIO_MBUF_BURST_SZ];
100         uint32_t i;
101         struct pmd_internals *pi = rxvq->internals;
102
103         nb_used = VIRTQUEUE_NUSED(rxvq);
104
105         rte_smp_rmb();
106         num = (uint16_t)(likely(nb_used <= nb_pkts) ? nb_used : nb_pkts);
107         num = (uint16_t)(likely(num <= VIRTIO_MBUF_BURST_SZ) ? num : VIRTIO_MBUF_BURST_SZ);
108         if (unlikely(num == 0)) return 0;
109
110         num = virtqueue_dequeue_burst(rxvq, rx_pkts, len, num);
111         PMD_RX_LOG(DEBUG, "used:%d dequeue:%d\n", nb_used, num);
112         for (i = 0; i < num ; i ++) {
113                 rxm = rx_pkts[i];
114                 PMD_RX_LOG(DEBUG, "packet len:%d\n", len[i]);
115                 rxm->next = NULL;
116                 rxm->data_off = RTE_PKTMBUF_HEADROOM;
117                 rxm->data_len = (uint16_t)(len[i] - sizeof(struct virtio_net_hdr));
118                 rxm->nb_segs = 1;
119                 rxm->port = pi->port_id;
120                 rxm->pkt_len  = (uint32_t)(len[i] - sizeof(struct virtio_net_hdr));
121         }
122         /* allocate new mbuf for the used descriptor */
123         while (likely(!virtqueue_full(rxvq))) {
124                 new_mbuf = rte_rxmbuf_alloc(rxvq->mpool);
125                 if (unlikely(new_mbuf == NULL)) {
126                         break;
127                 }
128                 if (unlikely(virtqueue_enqueue_recv_refill(rxvq, new_mbuf))) {
129                         rte_pktmbuf_free_seg(new_mbuf);
130                         break;
131                 }
132         }
133         pi->eth_stats.ipackets += num;
134         return num;
135 }
136
137 static uint16_t
138 eth_xenvirt_tx(void *tx_queue, struct rte_mbuf **tx_pkts, uint16_t nb_pkts)
139 {
140         struct virtqueue *txvq = tx_queue;
141         struct rte_mbuf *txm;
142         uint16_t nb_used, nb_tx, num, i;
143         int error;
144         uint32_t len[VIRTIO_MBUF_BURST_SZ];
145         struct rte_mbuf *snd_pkts[VIRTIO_MBUF_BURST_SZ];
146         struct pmd_internals *pi = txvq->internals;
147
148         nb_tx = 0;
149
150         if (unlikely(nb_pkts == 0))
151                 return 0;
152
153         PMD_TX_LOG(DEBUG, "%d packets to xmit", nb_pkts);
154         nb_used = VIRTQUEUE_NUSED(txvq);
155
156         rte_smp_rmb();
157
158         num = (uint16_t)(likely(nb_used <= VIRTIO_MBUF_BURST_SZ) ? nb_used : VIRTIO_MBUF_BURST_SZ);
159         num = virtqueue_dequeue_burst(txvq, snd_pkts, len, num);
160
161         for (i = 0; i < num ; i ++) {
162                 /* mergable not supported, one segment only */
163                 rte_pktmbuf_free_seg(snd_pkts[i]);
164         }
165
166         while (nb_tx < nb_pkts) {
167                 if (likely(!virtqueue_full(txvq))) {
168                 /* TODO drop tx_pkts if it contains multiple segments */
169                         txm = tx_pkts[nb_tx];
170                         error = virtqueue_enqueue_xmit(txvq, txm);
171                         if (unlikely(error)) {
172                                 if (error == ENOSPC)
173                                         PMD_TX_LOG(ERR, "virtqueue_enqueue Free count = 0\n");
174                                 else if (error == EMSGSIZE)
175                                         PMD_TX_LOG(ERR, "virtqueue_enqueue Free count < 1\n");
176                                 else
177                                         PMD_TX_LOG(ERR, "virtqueue_enqueue error: %d\n", error);
178                                 break;
179                         }
180                         nb_tx++;
181                 } else {
182                         PMD_TX_LOG(ERR, "No free tx descriptors to transmit\n");
183                         /* virtqueue_notify not needed in our para-virt solution */
184                         break;
185                 }
186         }
187         pi->eth_stats.opackets += nb_tx;
188         return nb_tx;
189 }
190
191 static int
192 eth_dev_configure(struct rte_eth_dev *dev __rte_unused)
193 {
194         RTE_LOG(ERR, PMD, "%s\n", __func__);
195         return 0;
196 }
197
198 /*
199  * Create a shared page between guest and host.
200  * Host monitors this page if it is cleared on unmap, and then
201  * do necessary clean up.
202  */
203 static void
204 gntalloc_vring_flag(int vtidx)
205 {
206         char key_str[PATH_MAX];
207         char val_str[PATH_MAX];
208         uint32_t gref_tmp;
209         void *ptr;
210
211         if (grefwatch_from_alloc(&gref_tmp, &ptr)) {
212                 RTE_LOG(ERR, PMD, "grefwatch_from_alloc error\n");
213                 exit(0);
214         }
215
216         *(uint8_t *)ptr = MAP_FLAG;
217         snprintf(val_str, sizeof(val_str), "%u", gref_tmp);
218         snprintf(key_str, sizeof(key_str),
219                 DPDK_XENSTORE_PATH"%d"VRING_FLAG_STR, vtidx);
220         xenstore_write(key_str, val_str);
221 }
222
223 /*
224  * Notify host this virtio device is started.
225  * Host could start polling this device.
226  */
227 static void
228 dev_start_notify(int vtidx)
229 {
230         char key_str[PATH_MAX];
231         char val_str[PATH_MAX];
232
233         RTE_LOG(INFO, PMD, "%s: virtio %d is started\n", __func__, vtidx);
234         gntalloc_vring_flag(vtidx);
235
236         snprintf(key_str, sizeof(key_str), "%s%s%d",
237                 DPDK_XENSTORE_PATH, EVENT_TYPE_START_STR,
238                         vtidx);
239         snprintf(val_str, sizeof(val_str), "1");
240         xenstore_write(key_str, val_str);
241 }
242
243 /*
244  * Notify host this virtio device is stopped.
245  * Host could stop polling this device.
246  */
247 static void
248 dev_stop_notify(int vtidx)
249 {
250         RTE_SET_USED(vtidx);
251 }
252
253
254 static int
255 update_mac_address(struct ether_addr *mac_addrs, int vtidx)
256 {
257         char key_str[PATH_MAX];
258         char val_str[PATH_MAX];
259         int rv;
260
261         if (mac_addrs == NULL) {
262                 RTE_LOG(ERR, PMD, "%s: NULL pointer mac specified\n", __func__);
263                 return -1;
264         }
265         rv = snprintf(key_str, sizeof(key_str),
266                         DPDK_XENSTORE_PATH"%d_ether_addr", vtidx);
267         if (rv == -1)
268                 return rv;
269         rv = snprintf(val_str, sizeof(val_str), "%02x:%02x:%02x:%02x:%02x:%02x",
270                         mac_addrs->addr_bytes[0],
271                         mac_addrs->addr_bytes[1],
272                         mac_addrs->addr_bytes[2],
273                         mac_addrs->addr_bytes[3],
274                         mac_addrs->addr_bytes[4],
275                         mac_addrs->addr_bytes[5]);
276         if (rv == -1)
277                 return rv;
278         if (xenstore_write(key_str, val_str))
279                 return rv;
280         return 0;
281 }
282
283
284 static int
285 eth_dev_start(struct rte_eth_dev *dev)
286 {
287         struct virtqueue *rxvq = dev->data->rx_queues[0];
288         struct virtqueue *txvq = dev->data->tx_queues[0];
289         struct rte_mbuf *m;
290         struct pmd_internals *pi = (struct pmd_internals *)dev->data->dev_private;
291         int rv;
292
293         dev->data->dev_link.link_status = 1;
294         while (!virtqueue_full(rxvq)) {
295                 m = rte_rxmbuf_alloc(rxvq->mpool);
296                 if (m == NULL)
297                         break;
298                 /* Enqueue allocated buffers. */
299                 if (virtqueue_enqueue_recv_refill(rxvq, m)) {
300                         rte_pktmbuf_free_seg(m);
301                         break;
302                 }
303         }
304
305         rxvq->internals = pi;
306         txvq->internals = pi;
307
308         rv = update_mac_address(dev->data->mac_addrs, pi->virtio_idx);
309         if (rv)
310                 return -1;
311         dev_start_notify(pi->virtio_idx);
312
313         return 0;
314 }
315
316 static void
317 eth_dev_stop(struct rte_eth_dev *dev)
318 {
319         struct pmd_internals *pi = (struct pmd_internals *)dev->data->dev_private;
320
321         dev->data->dev_link.link_status = 0;
322         dev_stop_notify(pi->virtio_idx);
323 }
324
325 /*
326  * Notify host this virtio device is closed.
327  * Host could do necessary clean up to this device.
328  */
329 static void
330 eth_dev_close(struct rte_eth_dev *dev)
331 {
332         eth_xenvirt_free_queues(dev);
333 }
334
335 static void
336 eth_dev_info(struct rte_eth_dev *dev,
337                 struct rte_eth_dev_info *dev_info)
338 {
339         struct pmd_internals *internals = dev->data->dev_private;
340
341         RTE_SET_USED(internals);
342         dev_info->driver_name = drivername;
343         dev_info->max_mac_addrs = 1;
344         dev_info->max_rx_pktlen = (uint32_t)2048;
345         dev_info->max_rx_queues = (uint16_t)1;
346         dev_info->max_tx_queues = (uint16_t)1;
347         dev_info->min_rx_bufsize = 0;
348         dev_info->pci_dev = NULL;
349 }
350
351 static void
352 eth_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
353 {
354         struct pmd_internals *internals = dev->data->dev_private;
355         if(stats)
356                 rte_memcpy(stats, &internals->eth_stats, sizeof(*stats));
357 }
358
359 static void
360 eth_stats_reset(struct rte_eth_dev *dev)
361 {
362         struct pmd_internals *internals = dev->data->dev_private;
363         /* Reset software totals */
364         memset(&internals->eth_stats, 0, sizeof(internals->eth_stats));
365 }
366
367 static void
368 eth_queue_release(void *q)
369 {
370         rte_free(q);
371 }
372
373 static int
374 eth_link_update(struct rte_eth_dev *dev __rte_unused,
375                 int wait_to_complete __rte_unused)
376 {
377         return 0;
378 }
379
380 /*
381  * Create shared vring between guest and host.
382  * Memory is allocated through grant alloc driver, so it is not physical continuous.
383  */
384 static void *
385 gntalloc_vring_create(int queue_type, uint32_t size, int vtidx)
386 {
387         char key_str[PATH_MAX] = {0};
388         char val_str[PATH_MAX] = {0};
389         void *va = NULL;
390         int pg_size;
391         uint32_t pg_num;
392         uint32_t *gref_arr = NULL;
393         phys_addr_t *pa_arr = NULL;
394         uint64_t start_index;
395         int rv;
396
397         pg_size = getpagesize();
398         size    = RTE_ALIGN_CEIL(size, pg_size);
399         pg_num  = size / pg_size;
400
401         gref_arr = calloc(pg_num, sizeof(gref_arr[0]));
402         pa_arr  = calloc(pg_num, sizeof(pa_arr[0]));
403
404         if (gref_arr == NULL || pa_arr == NULL) {
405                 RTE_LOG(ERR, PMD, "%s: calloc failed\n", __func__);
406                 goto out;
407         }
408
409         va  = gntalloc(size, gref_arr, &start_index);
410         if (va == NULL) {
411                 RTE_LOG(ERR, PMD, "%s: gntalloc failed\n", __func__);
412                 goto out;
413         }
414
415         if (get_phys_map(va, pa_arr, pg_num, pg_size))
416                 goto out;
417
418         /* write in xenstore gref and pfn for each page of vring */
419         if (grant_node_create(pg_num, gref_arr, pa_arr, val_str, sizeof(val_str))) {
420                 gntfree(va, size, start_index);
421                 va = NULL;
422                 goto out;
423         }
424
425         if (queue_type == VTNET_RQ)
426                 rv = snprintf(key_str, sizeof(key_str), DPDK_XENSTORE_PATH"%d"RXVRING_XENSTORE_STR, vtidx);
427         else
428                 rv = snprintf(key_str, sizeof(key_str), DPDK_XENSTORE_PATH"%d"TXVRING_XENSTORE_STR, vtidx);
429         if (rv == -1 || xenstore_write(key_str, val_str) == -1) {
430                 gntfree(va, size, start_index);
431                 va = NULL;
432         }
433 out:
434         if (pa_arr)
435                 free(pa_arr);
436         if (gref_arr)
437                 free(gref_arr);
438
439         return va;
440 }
441
442
443
444 static struct virtqueue *
445 virtio_queue_setup(struct rte_eth_dev *dev, int queue_type)
446 {
447         struct virtqueue *vq = NULL;
448         uint16_t vq_size = VQ_DESC_NUM;
449         int i = 0;
450         char vq_name[VIRTQUEUE_MAX_NAME_SZ];
451         size_t size;
452         struct vring *vr;
453
454         /* Allocate memory for virtqueue. */
455         if (queue_type == VTNET_RQ) {
456                 snprintf(vq_name, sizeof(vq_name), "port%d_rvq",
457                                 dev->data->port_id);
458                 vq = rte_zmalloc(vq_name, sizeof(struct virtqueue) +
459                         vq_size * sizeof(struct vq_desc_extra), RTE_CACHE_LINE_SIZE);
460                 if (vq == NULL) {
461                         RTE_LOG(ERR, PMD, "%s: unabled to allocate virtqueue\n", __func__);
462                         return NULL;
463                 }
464                 memcpy(vq->vq_name, vq_name, sizeof(vq->vq_name));
465         } else if(queue_type == VTNET_TQ) {
466                 snprintf(vq_name, sizeof(vq_name), "port%d_tvq",
467                         dev->data->port_id);
468                 vq = rte_zmalloc(vq_name, sizeof(struct virtqueue) +
469                         vq_size * sizeof(struct vq_desc_extra), RTE_CACHE_LINE_SIZE);
470                 if (vq == NULL) {
471                         RTE_LOG(ERR, PMD, "%s: unabled to allocate virtqueue\n", __func__);
472                         return NULL;
473                 }
474                 memcpy(vq->vq_name, vq_name, sizeof(vq->vq_name));
475         }
476
477         memcpy(vq->vq_name, vq_name, sizeof(vq->vq_name));
478
479         vq->vq_alignment = VIRTIO_PCI_VRING_ALIGN;
480         vq->vq_nentries = vq_size;
481         vq->vq_free_cnt = vq_size;
482         /* Calcuate vring size according to virtio spec */
483         size = vring_size(vq_size, VIRTIO_PCI_VRING_ALIGN);
484         vq->vq_ring_size = RTE_ALIGN_CEIL(size, VIRTIO_PCI_VRING_ALIGN);
485         /* Allocate memory for virtio vring through gntalloc driver*/
486         vq->vq_ring_virt_mem = gntalloc_vring_create(queue_type, vq->vq_ring_size,
487                 ((struct pmd_internals *)dev->data->dev_private)->virtio_idx);
488         memset(vq->vq_ring_virt_mem, 0, vq->vq_ring_size);
489         vr = &vq->vq_ring;
490         vring_init(vr, vq_size, vq->vq_ring_virt_mem, vq->vq_alignment);
491         /*
492          * Locally maintained last consumed index, this idex trails
493          * vq_ring.used->idx.
494          */
495         vq->vq_used_cons_idx = 0;
496         vq->vq_desc_head_idx = 0;
497         vq->vq_free_cnt = vq->vq_nentries;
498         memset(vq->vq_descx, 0, sizeof(struct vq_desc_extra) * vq->vq_nentries);
499
500         /* Chain all the descriptors in the ring with an END */
501         for (i = 0; i < vq_size - 1; i++)
502                 vr->desc[i].next = (uint16_t)(i + 1);
503         vr->desc[i].next = VQ_RING_DESC_CHAIN_END;
504
505         return vq;
506 }
507
508 static int
509 eth_rx_queue_setup(struct rte_eth_dev *dev,uint16_t rx_queue_id,
510                                 uint16_t nb_rx_desc __rte_unused,
511                                 unsigned int socket_id __rte_unused,
512                                 const struct rte_eth_rxconf *rx_conf __rte_unused,
513                                 struct rte_mempool *mb_pool)
514 {
515         struct virtqueue *vq;
516         vq = dev->data->rx_queues[rx_queue_id] = virtio_queue_setup(dev, VTNET_RQ);
517         vq->mpool = mb_pool;
518         return 0;
519 }
520
521 static int
522 eth_tx_queue_setup(struct rte_eth_dev *dev, uint16_t tx_queue_id,
523                                 uint16_t nb_tx_desc __rte_unused,
524                                 unsigned int socket_id __rte_unused,
525                                 const struct rte_eth_txconf *tx_conf __rte_unused)
526 {
527         dev->data->tx_queues[tx_queue_id] = virtio_queue_setup(dev, VTNET_TQ);
528         return 0;
529 }
530
531 static void
532 eth_xenvirt_free_queues(struct rte_eth_dev *dev)
533 {
534         int i;
535
536         for (i = 0; i < dev->data->nb_rx_queues; i++) {
537                 eth_queue_release(dev->data->rx_queues[i]);
538                 dev->data->rx_queues[i] = NULL;
539         }
540         dev->data->nb_rx_queues = 0;
541
542         for (i = 0; i < dev->data->nb_tx_queues; i++) {
543                 eth_queue_release(dev->data->tx_queues[i]);
544                 dev->data->tx_queues[i] = NULL;
545         }
546         dev->data->nb_tx_queues = 0;
547 }
548
549 static const struct eth_dev_ops ops = {
550         .dev_start = eth_dev_start,
551         .dev_stop = eth_dev_stop,
552         .dev_close = eth_dev_close,
553         .dev_configure = eth_dev_configure,
554         .dev_infos_get = eth_dev_info,
555         .rx_queue_setup = eth_rx_queue_setup,
556         .tx_queue_setup = eth_tx_queue_setup,
557         .rx_queue_release = eth_queue_release,
558         .tx_queue_release = eth_queue_release,
559         .link_update = eth_link_update,
560         .stats_get = eth_stats_get,
561         .stats_reset = eth_stats_reset,
562 };
563
564
565 static int
566 rte_eth_xenvirt_parse_args(struct xenvirt_dict *dict,
567                         const char *name, const char *params)
568 {
569         int i;
570         char *pairs[RTE_ETH_XENVIRT_MAX_ARGS];
571         int num_of_pairs;
572         char *pair[2];
573         char *args;
574         int ret = -1;
575
576         if (params == NULL)
577                 return 0;
578
579         args = rte_zmalloc(NULL, strlen(params) + 1, RTE_CACHE_LINE_SIZE);
580         if (args == NULL) {
581                 RTE_LOG(ERR, PMD, "Couldn't parse %s device \n", name);
582                 return -1;
583         }
584         rte_memcpy(args, params, strlen(params));
585
586         num_of_pairs = rte_strsplit(args, strnlen(args, MAX_ARG_STRLEN),
587                                         pairs,
588                                         RTE_ETH_XENVIRT_MAX_ARGS ,
589                                         RTE_ETH_XENVIRT_PAIRS_DELIM);
590
591         for (i = 0; i < num_of_pairs; i++) {
592                 pair[0] = NULL;
593                 pair[1] = NULL;
594                 rte_strsplit(pairs[i], strnlen(pairs[i], MAX_ARG_STRLEN),
595                                         pair, 2,
596                                         RTE_ETH_XENVIRT_KEY_VALUE_DELIM);
597
598                 if (pair[0] == NULL || pair[1] == NULL || pair[0][0] == 0
599                         || pair[1][0] == 0) {
600                         RTE_LOG(ERR, PMD,
601                                 "Couldn't parse %s device,"
602                                 "wrong key or value \n", name);
603                         goto err;
604                 }
605
606                 if (!strncmp(pair[0], RTE_ETH_XENVIRT_MAC_PARAM,
607                                 sizeof(RTE_ETH_XENVIRT_MAC_PARAM))) {
608                         if (cmdline_parse_etheraddr(NULL,
609                                                     pair[1],
610                                                     &dict->addr,
611                                                     sizeof(dict->addr)) < 0) {
612                                 RTE_LOG(ERR, PMD,
613                                         "Invalid %s device ether address\n",
614                                         name);
615                                 goto err;
616                         }
617
618                         dict->addr_valid = 1;
619                 }
620         }
621
622         ret = 0;
623 err:
624         rte_free(args);
625         return ret;
626 }
627
628 enum dev_action {
629         DEV_CREATE,
630         DEV_ATTACH
631 };
632
633
634 static int
635 eth_dev_xenvirt_create(const char *name, const char *params,
636                 const unsigned numa_node,
637                 enum dev_action action)
638 {
639         struct rte_eth_dev_data *data = NULL;
640         struct pmd_internals *internals = NULL;
641         struct rte_eth_dev *eth_dev = NULL;
642         struct xenvirt_dict dict;
643
644         memset(&dict, 0, sizeof(struct xenvirt_dict));
645
646         RTE_LOG(INFO, PMD, "Creating virtio rings backed ethdev on numa socket %u\n",
647                         numa_node);
648         RTE_SET_USED(action);
649
650         if (rte_eth_xenvirt_parse_args(&dict, name, params) < 0) {
651                 RTE_LOG(ERR, PMD, "%s: Failed to parse ethdev parameters\n", __func__);
652                 return -1;
653         }
654
655         /* now do all data allocation - for eth_dev structure, dummy pci driver
656          * and internal (private) data
657          */
658         data = rte_zmalloc_socket(name, sizeof(*data), 0, numa_node);
659         if (data == NULL)
660                 goto err;
661
662         internals = rte_zmalloc_socket(name, sizeof(*internals), 0, numa_node);
663         if (internals == NULL)
664                 goto err;
665
666         /* reserve an ethdev entry */
667         eth_dev = rte_eth_dev_allocate(name, RTE_ETH_DEV_VIRTUAL);
668         if (eth_dev == NULL)
669                 goto err;
670
671         data->dev_private = internals;
672         data->port_id = eth_dev->data->port_id;
673         data->nb_rx_queues = (uint16_t)1;
674         data->nb_tx_queues = (uint16_t)1;
675         data->dev_link = pmd_link;
676         data->mac_addrs = rte_zmalloc("xen_virtio", ETHER_ADDR_LEN, 0);
677
678         if(dict.addr_valid)
679                 memcpy(&data->mac_addrs->addr_bytes, &dict.addr, sizeof(struct ether_addr));
680         else
681                 eth_random_addr(&data->mac_addrs->addr_bytes[0]);
682
683         eth_dev->data = data;
684         eth_dev->dev_ops = &ops;
685
686         eth_dev->data->dev_flags = RTE_PCI_DRV_DETACHABLE;
687         eth_dev->data->kdrv = RTE_KDRV_NONE;
688         eth_dev->data->drv_name = drivername;
689         eth_dev->driver = NULL;
690         eth_dev->data->numa_node = numa_node;
691
692         eth_dev->rx_pkt_burst = eth_xenvirt_rx;
693         eth_dev->tx_pkt_burst = eth_xenvirt_tx;
694
695         internals->virtio_idx = virtio_idx++;
696         internals->port_id = eth_dev->data->port_id;
697
698         return 0;
699
700 err:
701         rte_free(data);
702         rte_free(internals);
703
704         return -1;
705 }
706
707
708 static int
709 eth_dev_xenvirt_free(const char *name, const unsigned numa_node)
710 {
711         struct rte_eth_dev *eth_dev = NULL;
712
713         RTE_LOG(DEBUG, PMD,
714                 "Free virtio rings backed ethdev on numa socket %u\n",
715                 numa_node);
716
717         /* find an ethdev entry */
718         eth_dev = rte_eth_dev_allocated(name);
719         if (eth_dev == NULL)
720                 return -1;
721
722         if (eth_dev->data->dev_started == 1) {
723                 eth_dev_stop(eth_dev);
724                 eth_dev_close(eth_dev);
725         }
726
727         eth_dev->rx_pkt_burst = NULL;
728         eth_dev->tx_pkt_burst = NULL;
729         eth_dev->dev_ops = NULL;
730
731         rte_free(eth_dev->data);
732         rte_free(eth_dev->data->dev_private);
733         rte_free(eth_dev->data->mac_addrs);
734
735         virtio_idx--;
736
737         return 0;
738 }
739
740 /*TODO: Support multiple process model */
741 static int
742 rte_pmd_xenvirt_devinit(const char *name, const char *params)
743 {
744         if (virtio_idx == 0) {
745                 if (xenstore_init() != 0) {
746                         RTE_LOG(ERR, PMD, "%s: xenstore init failed\n", __func__);
747                         return -1;
748                 }
749                 if (gntalloc_open() != 0) {
750                         RTE_LOG(ERR, PMD, "%s: grant init failed\n", __func__);
751                         return -1;
752                 }
753         }
754         eth_dev_xenvirt_create(name, params, rte_socket_id(), DEV_CREATE);
755         return 0;
756 }
757
758 static int
759 rte_pmd_xenvirt_devuninit(const char *name)
760 {
761         eth_dev_xenvirt_free(name, rte_socket_id());
762
763         if (virtio_idx == 0) {
764                 if (xenstore_uninit() != 0)
765                         RTE_LOG(ERR, PMD, "%s: xenstore uninit failed\n", __func__);
766
767                 gntalloc_close();
768         }
769         return 0;
770 }
771
772 static struct rte_driver pmd_xenvirt_drv = {
773         .name = "eth_xenvirt",
774         .type = PMD_VDEV,
775         .init = rte_pmd_xenvirt_devinit,
776         .uninit = rte_pmd_xenvirt_devuninit,
777 };
778
779 PMD_REGISTER_DRIVER(pmd_xenvirt_drv);