summaryrefslogtreecommitdiffstats
path: root/src/plugins/dev_iavf/queue.c
blob: 113c0dbdfc7cb22caacc021e3e35602b84902002 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/* SPDX-License-Identifier: Apache-2.0
 * Copyright (c) 2023 Cisco Systems, Inc.
 */

#include <vnet/vnet.h>
#include <vnet/dev/dev.h>
#include <vnet/dev/pci.h>
#include <vnet/dev/counters.h>
#include <vppinfra/ring.h>
#include <dev_iavf/iavf.h>
#include <dev_iavf/virtchnl.h>
#include <dev_iavf/virtchnl_funcs.h>
#include <vnet/ethernet/ethernet.h>
#include <vnet/plugin/plugin.h>
#include <vpp/app/version.h>

VLIB_REGISTER_LOG_CLASS (iavf_log, static) = {
  .class_name = "iavf",
  .subclass_name = "queue",
};

vnet_dev_rv_t
iavf_rx_queue_alloc (vlib_main_t *vm, vnet_dev_rx_queue_t *rxq)
{
  vnet_dev_port_t *port = rxq->port;
  vnet_dev_t *dev = port->dev;
  iavf_device_t *ad = vnet_dev_get_data (dev);
  iavf_rxq_t *arq = vnet_dev_get_rx_queue_data (rxq);
  vnet_dev_rv_t rv;

  arq->buffer_indices = clib_mem_alloc_aligned (
    rxq->size * sizeof (arq->buffer_indices[0]), CLIB_CACHE_LINE_BYTES);

  if ((rv =
	 vnet_dev_dma_mem_alloc (vm, dev, sizeof (iavf_rx_desc_t) * rxq->size,
				 0, (void **) &arq->descs)))
    return rv;

  arq->qrx_tail = ad->bar0 + IAVF_QRX_TAIL (rxq->queue_id);

  log_debug (dev, "queue %u alocated", rxq->queue_id);
  return rv;
}

void
iavf_rx_queue_free (vlib_main_t *vm, vnet_dev_rx_queue_t *rxq)
{
  vnet_dev_t *dev = rxq->port->dev;
  iavf_rxq_t *aq = vnet_dev_get_rx_queue_data (rxq);

  log_debug (dev, "queue %u", rxq->queue_id);

  vnet_dev_dma_mem_free (vm, dev, aq->descs);

  foreach_pointer (p, aq->buffer_indices)
    if (p)
      clib_mem_free (p);
}

vnet_dev_rv_t
iavf_tx_queue_alloc (vlib_main_t *vm, vnet_dev_tx_queue_t *txq)
{
  vnet_dev_t *dev = txq->port->dev;
  iavf_device_t *ad = vnet_dev_get_data (dev);
  iavf_txq_t *atq = vnet_dev_get_tx_queue_data (txq);
  vnet_dev_rv_t rv;

  if ((rv =
	 vnet_dev_dma_mem_alloc (vm, dev, sizeof (iavf_tx_desc_t) * txq->size,
				 0, (void **) &atq->descs)))
    return rv;

  clib_ring_new_aligned (atq->rs_slots, 32, CLIB_CACHE_LINE_BYTES);
  atq->buffer_indices = clib_mem_alloc_aligned (
    txq->size * sizeof (atq->buffer_indices[0]), CLIB_CACHE_LINE_BYTES);
  atq->tmp_descs = clib_mem_alloc_aligned (
    sizeof (atq->tmp_descs[0]) * txq->size, CLIB_CACHE_LINE_BYTES);
  atq->tmp_bufs = clib_mem_alloc_aligned (
    sizeof (atq->tmp_bufs[0]) * txq->size, CLIB_CACHE_LINE_BYTES);

  atq->qtx_tail = ad->bar0 + IAVF_QTX_TAIL (txq->queue_id);

  log_debug (dev, "queue %u alocated", txq->queue_id);
  return VNET_DEV_OK;
}

void
iavf_tx_queue_free (vlib_main_t *vm, vnet_dev_tx_queue_t *txq)
{
  vnet_dev_t *dev = txq->port->dev;
  iavf_txq_t *atq = vnet_dev_get_tx_queue_data (txq);
  iavf_txq_t *aq = vnet_dev_get_tx_queue_data (txq);

  log_debug (dev, "queue %u", txq->queue_id);
  vnet_dev_dma_mem_free (vm, dev, aq->descs);
  clib_ring_free (atq->rs_slots);

  foreach_pointer (p, aq->tmp_descs, aq->tmp_bufs, aq->buffer_indices)
    if (p)
      clib_mem_free (p);
}

vnet_dev_rv_t
iavf_rx_queue_start (vlib_main_t *vm, vnet_dev_rx_queue_t *rxq)
{
  vnet_dev_t *dev = rxq->port->dev;
  iavf_rxq_t *arq = vnet_dev_get_rx_queue_data (rxq);
  iavf_rx_desc_t *d = arq->descs;
  u32 n_enq, *bi = arq->buffer_indices;
  u8 bpi = vnet_dev_get_rx_queue_buffer_pool_index (rxq);

  n_enq = vlib_buffer_alloc_from_pool (vm, bi, rxq->size - 8, bpi);

  if (n_enq < 8)
    {
      if (n_enq)
	vlib_buffer_free (vm, bi, n_enq);
      return VNET_DEV_ERR_BUFFER_ALLOC_FAIL;
    }

  for (u32 i = 0; i < n_enq; i++)
    {
      vlib_buffer_t *b = vlib_get_buffer (vm, bi[i]);
      u64 dma_addr = vnet_dev_get_dma_addr (vm, dev, b->data);
      d[i] = (iavf_rx_desc_t){ .addr = dma_addr };
    }

  arq->n_enqueued = n_enq;
  arq->next = 0;
  __atomic_store_n (arq->qrx_tail, n_enq, __ATOMIC_RELEASE);
  return VNET_DEV_OK;
}

void
iavf_rx_queue_stop (vlib_main_t *vm, vnet_dev_rx_queue_t *rxq)
{
  iavf_rxq_t *arq = vnet_dev_get_rx_queue_data (rxq);

  __atomic_store_n (arq->qrx_tail, 0, __ATOMIC_RELAXED);
  if (arq->n_enqueued)
    {
      vlib_buffer_free_from_ring_no_next (vm, arq->buffer_indices, arq->next,
					  rxq->size, arq->n_enqueued);
      log_debug (rxq->port->dev, "%u buffers freed from rx queue %u",
		 arq->n_enqueued, rxq->queue_id);
    }
  arq->n_enqueued = arq->next = 0;
}

vnet_dev_rv_t
iavf_tx_queue_start (vlib_main_t *vm, vnet_dev_tx_queue_t *txq)
{
  iavf_txq_t *atq = vnet_dev_get_tx_queue_data (txq);
  atq->next = 0;
  atq->n_enqueued = 0;
  clib_ring_reset (atq->rs_slots);
  __atomic_store_n (atq->qtx_tail, 0, __ATOMIC_RELAXED);
  return VNET_DEV_OK;
}

void
iavf_tx_queue_stop (vlib_main_t *vm, vnet_dev_tx_queue_t *txq)
{
  iavf_txq_t *atq = vnet_dev_get_tx_queue_data (txq);

  log_debug (txq->port->dev, "queue %u", txq->queue_id);

  __atomic_store_n (atq->qtx_tail, 0, __ATOMIC_RELAXED);
  if (atq->n_enqueued)
    {
      vlib_buffer_free_from_ring_no_next (vm, atq->buffer_indices,
					  atq->next - atq->n_enqueued,
					  txq->size, atq->n_enqueued);
      log_debug (txq->port->dev, "%u buffers freed from tx queue %u",
		 atq->n_enqueued, txq->queue_id);
    }
  atq->n_enqueued = atq->next = 0;
}