remove trailing whitespaces
[dpdk.git] / lib / librte_pmd_virtio / virtio_rxtx.c
index 2dd2102..171cbc2 100644 (file)
@@ -1,13 +1,13 @@
 /*-
  *   BSD LICENSE
- * 
+ *
  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
  *   All rights reserved.
- * 
+ *
  *   Redistribution and use in source and binary forms, with or without
  *   modification, are permitted provided that the following conditions
  *   are met:
- * 
+ *
  *     * Redistributions of source code must retain the above copyright
  *       notice, this list of conditions and the following disclaimer.
  *     * Redistributions in binary form must reproduce the above copyright
@@ -17,7 +17,7 @@
  *     * Neither the name of Intel Corporation nor the names of its
  *       contributors may be used to endorse or promote products derived
  *       from this software without specific prior written permission.
- * 
+ *
  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
@@ -82,14 +82,14 @@ virtio_dev_vring_start(struct rte_eth_dev *dev, struct virtqueue *vq, int queue_
        PMD_INIT_FUNC_TRACE();
 
        /*
-       * Reinitialise since virtio port might have been stopped and restarted
-       */
+        * Reinitialise since virtio port might have been stopped and restarted
+        */
        memset(vq->vq_ring_virt_mem, 0, vq->vq_ring_size);
        vring_init(vr, size, ring_mem, vq->vq_alignment);
        vq->vq_used_cons_idx = 0;
        vq->vq_desc_head_idx = 0;
        vq->vq_avail_idx = 0;
-       vq->vq_desc_tail_idx = vq->vq_nentries - 1;
+       vq->vq_desc_tail_idx = (uint16_t)(vq->vq_nentries - 1);
        vq->vq_free_cnt = vq->vq_nentries;
        memset(vq->vq_descx, 0, sizeof(struct vq_desc_extra) * vq->vq_nentries);
 
@@ -110,36 +110,60 @@ virtio_dev_vring_start(struct rte_eth_dev *dev, struct virtqueue *vq, int queue_
        /* Only rx virtqueue needs mbufs to be allocated at initialization */
        if (queue_type == VTNET_RQ) {
                if (vq->mpool == NULL)
-                               rte_exit(EXIT_FAILURE, "Cannot allocate initial mbufs for rx virtqueue\n");
-                /* Allocate blank mbufs for the each rx descriptor */
+                       rte_exit(EXIT_FAILURE,
+                       "Cannot allocate initial mbufs for rx virtqueue\n");
+
+               /* Allocate blank mbufs for the each rx descriptor */
                nbufs = 0;
                error = ENOSPC;
                while (!virtqueue_full(vq)) {
                        m = rte_rxmbuf_alloc(vq->mpool);
                        if (m == NULL)
                                break;
+
                        /******************************************
                        *         Enqueue allocated buffers        *
                        *******************************************/
                        error = virtqueue_enqueue_recv_refill(vq, m);
+
                        if (error) {
-                               rte_pktmbuf_free_seg(m);
+                               rte_pktmbuf_free_seg(m);
                                break;
                        }
                        nbufs++;
                }
+
                vq_update_avail_idx(vq);
+
                PMD_INIT_LOG(DEBUG, "Allocated %d bufs\n", nbufs);
-               VIRTIO_WRITE_REG_2(vq->hw, VIRTIO_PCI_QUEUE_SEL, VTNET_SQ_RQ_QUEUE_IDX);
+
+               VIRTIO_WRITE_REG_2(vq->hw, VIRTIO_PCI_QUEUE_SEL,
+                       vq->vq_queue_index);
+               VIRTIO_WRITE_REG_4(vq->hw, VIRTIO_PCI_QUEUE_PFN,
+                       vq->mz->phys_addr >> VIRTIO_PCI_QUEUE_ADDR_SHIFT);
+       } else if (queue_type == VTNET_TQ) {
+               VIRTIO_WRITE_REG_2(vq->hw, VIRTIO_PCI_QUEUE_SEL,
+                       vq->vq_queue_index);
                VIRTIO_WRITE_REG_4(vq->hw, VIRTIO_PCI_QUEUE_PFN,
                        vq->mz->phys_addr >> VIRTIO_PCI_QUEUE_ADDR_SHIFT);
        } else {
-               VIRTIO_WRITE_REG_2(vq->hw, VIRTIO_PCI_QUEUE_SEL, VTNET_SQ_TQ_QUEUE_IDX);
+               VIRTIO_WRITE_REG_2(vq->hw, VIRTIO_PCI_QUEUE_SEL,
+                       vq->vq_queue_index);
                VIRTIO_WRITE_REG_4(vq->hw, VIRTIO_PCI_QUEUE_PFN,
                        vq->mz->phys_addr >> VIRTIO_PCI_QUEUE_ADDR_SHIFT);
        }
 }
 
+void
+virtio_dev_cq_start(struct rte_eth_dev *dev)
+{
+       struct virtio_hw *hw
+               = VIRTIO_DEV_PRIVATE_TO_HW(dev->data->dev_private);
+
+       virtio_dev_vring_start(dev, hw->cvq, VTNET_CQ);
+       VIRTQUEUE_DUMP((struct virtqueue *)hw->cvq);
+}
+
 void
 virtio_dev_rxtx_start(struct rte_eth_dev *dev)
 {
@@ -150,15 +174,20 @@ virtio_dev_rxtx_start(struct rte_eth_dev *dev)
         * -    Allocate blank mbufs for the each rx descriptor
         *
         */
+       int i;
        PMD_INIT_FUNC_TRACE();
 
-       /* Start rx vring: by default we have 1 rx virtqueue. */
-       virtio_dev_vring_start(dev, dev->data->rx_queues[0], VTNET_RQ);
-       VIRTQUEUE_DUMP((struct virtqueue *)dev->data->rx_queues[0]);
+       /* Start rx vring. */
+       for (i = 0; i < dev->data->nb_rx_queues; i++) {
+               virtio_dev_vring_start(dev, dev->data->rx_queues[i], VTNET_RQ);
+               VIRTQUEUE_DUMP((struct virtqueue *)dev->data->rx_queues[i]);
+       }
 
-       /* Start tx vring: by default we have 1 tx virtqueue. */
-       virtio_dev_vring_start(dev, dev->data->tx_queues[0], VTNET_TQ);
-       VIRTQUEUE_DUMP((struct virtqueue *)dev->data->tx_queues[0]);
+       /* Start tx vring. */
+       for (i = 0; i < dev->data->nb_tx_queues; i++) {
+               virtio_dev_vring_start(dev, dev->data->tx_queues[i], VTNET_TQ);
+               VIRTQUEUE_DUMP((struct virtqueue *)dev->data->tx_queues[i]);
+       }
 }
 
 int
@@ -169,7 +198,7 @@ virtio_dev_rx_queue_setup(struct rte_eth_dev *dev,
                        __rte_unused const struct rte_eth_rxconf *rx_conf,
                        struct rte_mempool *mp)
 {
-       uint8_t vtpci_queue_idx = VTNET_SQ_RQ_QUEUE_IDX;
+       uint8_t vtpci_queue_idx = 2 * queue_idx + VTNET_SQ_RQ_QUEUE_IDX;
        struct virtqueue *vq;
        int ret;
 
@@ -180,6 +209,7 @@ virtio_dev_rx_queue_setup(struct rte_eth_dev *dev,
                PMD_INIT_LOG(ERR, "tvq initialization failed\n");
                return ret;
        }
+
        /* Create mempool for rx mbuf allocation */
        vq->mpool = mp;
 
@@ -201,7 +231,7 @@ virtio_dev_tx_queue_setup(struct rte_eth_dev *dev,
                        unsigned int socket_id,
                        __rte_unused const struct rte_eth_txconf *tx_conf)
 {
-       uint8_t vtpci_queue_idx = VTNET_SQ_TQ_QUEUE_IDX;
+       uint8_t vtpci_queue_idx = 2 * queue_idx + VTNET_SQ_TQ_QUEUE_IDX;
        struct virtqueue *vq;
        int ret;
 
@@ -228,7 +258,7 @@ virtio_discard_rxbuf(struct virtqueue *vq, struct rte_mbuf *m)
        error = virtqueue_enqueue_recv_refill(vq, m);
        if (unlikely(error)) {
                RTE_LOG(ERR, PMD, "cannot requeue discarded mbuf");
-               rte_pktmbuf_free_seg(m);
+               rte_pktmbuf_free_seg(m);
        }
 }
 
@@ -254,30 +284,44 @@ virtio_recv_pkts(void *rx_queue, struct rte_mbuf **rx_pkts, uint16_t nb_pkts)
        num = (uint16_t)(likely(num <= VIRTIO_MBUF_BURST_SZ) ? num : VIRTIO_MBUF_BURST_SZ);
        if (likely(num > DESC_PER_CACHELINE))
                num = num - ((rxvq->vq_used_cons_idx + num) % DESC_PER_CACHELINE);
+
        if(num == 0) return 0;
+
        num = virtqueue_dequeue_burst_rx(rxvq, rcv_pkts, len, num);
        PMD_RX_LOG(DEBUG, "used:%d dequeue:%d\n", nb_used, num);
        for (i = 0; i < num ; i ++) {
                rxm = rcv_pkts[i];
+
                PMD_RX_LOG(DEBUG, "packet len:%d\n", len[i]);
-               if (unlikely(len[i] < (uint32_t)hw->vtnet_hdr_size + ETHER_HDR_LEN)) {
+
+               if (unlikely(len[i]
+                       < (uint32_t)hw->vtnet_hdr_size + ETHER_HDR_LEN)) {
                        PMD_RX_LOG(ERR, "Packet drop\n");
                        nb_enqueued++;
                        virtio_discard_rxbuf(rxvq, rxm);
                        hw->eth_stats.ierrors++;
                        continue;
                }
+
                rxm->pkt.in_port = rxvq->port_id;
                rxm->pkt.data = (char *)rxm->buf_addr + RTE_PKTMBUF_HEADROOM;
                rxm->pkt.nb_segs = 1;
                rxm->pkt.next = NULL;
-               rxm->pkt.pkt_len  = (uint32_t)(len[i] - sizeof(struct virtio_net_hdr));
-               rxm->pkt.data_len = (uint16_t)(len[i] - sizeof(struct virtio_net_hdr));
+               rxm->pkt.pkt_len  = (uint32_t)(len[i]
+                       - sizeof(struct virtio_net_hdr));
+               rxm->pkt.data_len = (uint16_t)(len[i]
+                       - sizeof(struct virtio_net_hdr));
+
                VIRTIO_DUMP_PACKET(rxm, rxm->pkt.data_len);
+
                rx_pkts[nb_rx++] = rxm;
                hw->eth_stats.ibytes += len[i] - sizeof(struct virtio_net_hdr);
+               hw->eth_stats.q_ibytes[rxvq->queue_id] += len[i]
+                       - sizeof(struct virtio_net_hdr);
        }
+
        hw->eth_stats.ipackets += nb_rx;
+       hw->eth_stats.q_ipackets[rxvq->queue_id] += nb_rx;
 
        /* Allocate new mbuf for the used descriptor */
        error = ENOSPC;
@@ -289,19 +333,20 @@ virtio_recv_pkts(void *rx_queue, struct rte_mbuf **rx_pkts, uint16_t nb_pkts)
                }
                error = virtqueue_enqueue_recv_refill(rxvq, new_mbuf);
                if (unlikely(error)) {
-                       rte_pktmbuf_free_seg(new_mbuf);
+                       rte_pktmbuf_free_seg(new_mbuf);
                        break;
                }
                nb_enqueued ++;
        }
-       if(likely(nb_enqueued)) {
-               if(unlikely(virtqueue_kick_prepare(rxvq))) {
-                       virtqueue_notify(rxvq);
+       if (likely(nb_enqueued)) {
+               if (unlikely(virtqueue_kick_prepare(rxvq))) {
+                       virtqueue_notify(rxvq);
                        PMD_RX_LOG(DEBUG, "Notified\n");
                }
        }
+
        vq_update_avail_idx(rxvq);
-       
+
        return (nb_rx);
 }
 
@@ -332,6 +377,7 @@ virtio_xmit_pkts(void *tx_queue, struct rte_mbuf **tx_pkts, uint16_t nb_pkts)
                        virtqueue_dequeue_pkt_tx(txvq);
                        num--;
                }
+
                if(!virtqueue_full(txvq)) {
                        txm = tx_pkts[nb_tx];
                        /* Enqueue Packet buffers */
@@ -345,8 +391,10 @@ virtio_xmit_pkts(void *tx_queue, struct rte_mbuf **tx_pkts, uint16_t nb_pkts)
                                        PMD_TX_LOG(ERR, "virtqueue_enqueue error: %d\n", error);
                                break;
                        }
-                       nb_tx++;
+                       nb_tx++;
                        hw->eth_stats.obytes += txm->pkt.data_len;
+                       hw->eth_stats.q_obytes[txvq->queue_id]
+                               += txm->pkt.data_len;
                } else {
                        PMD_TX_LOG(ERR, "No free tx descriptors to transmit\n");
                        break;
@@ -355,10 +403,12 @@ virtio_xmit_pkts(void *tx_queue, struct rte_mbuf **tx_pkts, uint16_t nb_pkts)
        vq_update_avail_idx(txvq);
 
        hw->eth_stats.opackets += nb_tx;
+       hw->eth_stats.q_opackets[txvq->queue_id] += nb_tx;
 
        if(unlikely(virtqueue_kick_prepare(txvq))) {
-               virtqueue_notify(txvq);
+               virtqueue_notify(txvq);
                PMD_TX_LOG(DEBUG, "Notified backend after xmit\n");
        }
+
        return (nb_tx);
 }