avf: chained buffers tx support
[vpp.git] / src / plugins / avf / output.c
1 /*
2  *------------------------------------------------------------------
3  * Copyright (c) 2018 Cisco and/or its affiliates.
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *------------------------------------------------------------------
16  */
17
18 #include <vlib/vlib.h>
19 #include <vlib/unix/unix.h>
20 #include <vlib/pci/pci.h>
21 #include <vnet/ethernet/ethernet.h>
22 #include <vnet/devices/devices.h>
23
24 #include <avf/avf.h>
25
26 static_always_inline u8
27 avf_tx_desc_get_dtyp (avf_tx_desc_t * d)
28 {
29   return d->qword[1] & 0x0f;
30 }
31
32 static_always_inline u16
33 avf_tx_enqueue (vlib_main_t * vm, avf_txq_t * txq, u32 * buffers,
34                 u32 n_packets, int use_va_dma)
35 {
36   u16 next = txq->next;
37   u64 bits = (AVF_TXD_CMD_EOP | AVF_TXD_CMD_RS | AVF_TXD_CMD_RSV);
38   u16 n_desc = 0;
39   u16 n_desc_left, n_packets_left = n_packets;
40   u16 mask = txq->size - 1;
41   vlib_buffer_t *b[4];
42   avf_tx_desc_t *d = txq->descs + next;
43
44   /* avoid ring wrap */
45   n_desc_left = txq->size - clib_max (txq->next, txq->n_enqueued + 8);
46
47   while (n_packets_left && n_desc_left)
48     {
49       u32 or_flags;
50       if (n_packets_left < 8 || n_desc_left < 4)
51         goto one_by_one;
52
53       vlib_prefetch_buffer_with_index (vm, buffers[4], LOAD);
54       vlib_prefetch_buffer_with_index (vm, buffers[5], LOAD);
55       vlib_prefetch_buffer_with_index (vm, buffers[6], LOAD);
56       vlib_prefetch_buffer_with_index (vm, buffers[7], LOAD);
57
58       b[0] = vlib_get_buffer (vm, buffers[0]);
59       b[1] = vlib_get_buffer (vm, buffers[1]);
60       b[2] = vlib_get_buffer (vm, buffers[2]);
61       b[3] = vlib_get_buffer (vm, buffers[3]);
62
63       or_flags = b[0]->flags | b[1]->flags | b[2]->flags | b[3]->flags;
64
65       if (or_flags & VLIB_BUFFER_NEXT_PRESENT)
66         goto one_by_one;
67
68       clib_memcpy_fast (txq->bufs + next, buffers, sizeof (u32) * 4);
69
70       if (use_va_dma)
71         {
72           d[0].qword[0] = vlib_buffer_get_current_va (b[0]);
73           d[1].qword[0] = vlib_buffer_get_current_va (b[1]);
74           d[2].qword[0] = vlib_buffer_get_current_va (b[2]);
75           d[3].qword[0] = vlib_buffer_get_current_va (b[3]);
76         }
77       else
78         {
79           d[0].qword[0] = vlib_buffer_get_current_pa (vm, b[0]);
80           d[1].qword[0] = vlib_buffer_get_current_pa (vm, b[1]);
81           d[2].qword[0] = vlib_buffer_get_current_pa (vm, b[2]);
82           d[3].qword[0] = vlib_buffer_get_current_pa (vm, b[3]);
83         }
84
85       d[0].qword[1] = ((u64) b[0]->current_length) << 34 | bits;
86       d[1].qword[1] = ((u64) b[1]->current_length) << 34 | bits;
87       d[2].qword[1] = ((u64) b[2]->current_length) << 34 | bits;
88       d[3].qword[1] = ((u64) b[3]->current_length) << 34 | bits;
89
90       next += 4;
91       n_desc += 4;
92       buffers += 4;
93       n_packets_left -= 4;
94       n_desc_left -= 4;
95       d += 4;
96       continue;
97
98     one_by_one:
99       txq->bufs[next] = buffers[0];
100       b[0] = vlib_get_buffer (vm, buffers[0]);
101
102       if (use_va_dma)
103         d[0].qword[0] = vlib_buffer_get_current_va (b[0]);
104       else
105         d[0].qword[0] = vlib_buffer_get_current_pa (vm, b[0]);
106
107       d[0].qword[1] = (((u64) b[0]->current_length) << 34) | bits;
108
109       next += 1;
110       n_desc += 1;
111       buffers += 1;
112       n_packets_left -= 1;
113       n_desc_left -= 1;
114       d += 1;
115     }
116
117   CLIB_MEMORY_BARRIER ();
118   *(txq->qtx_tail) = txq->next = next & mask;
119   txq->n_enqueued += n_desc;
120   return n_packets - n_packets_left;
121 }
122
123 VNET_DEVICE_CLASS_TX_FN (avf_device_class) (vlib_main_t * vm,
124                                             vlib_node_runtime_t * node,
125                                             vlib_frame_t * frame)
126 {
127   avf_main_t *am = &avf_main;
128   vnet_interface_output_runtime_t *rd = (void *) node->runtime_data;
129   avf_device_t *ad = pool_elt_at_index (am->devices, rd->dev_instance);
130   u32 thread_index = vm->thread_index;
131   u8 qid = thread_index;
132   avf_txq_t *txq = vec_elt_at_index (ad->txqs, qid % ad->num_queue_pairs);
133   u32 *buffers = vlib_frame_vector_args (frame);
134   u16 n_enq, n_left;
135   u16 n_retry = 5;
136
137   clib_spinlock_lock_if_init (&txq->lock);
138
139   n_left = frame->n_vectors;
140
141 retry:
142   /* release consumed bufs */
143   if (txq->n_enqueued)
144     {
145       avf_tx_desc_t *d0;
146       u16 first, slot, n_free = 0, mask = txq->size - 1;
147       first = slot = (txq->next - txq->n_enqueued) & mask;
148       d0 = txq->descs + slot;
149       while (n_free < txq->n_enqueued && avf_tx_desc_get_dtyp (d0) == 0x0F)
150         {
151           n_free++;
152           slot = (slot + 1) & mask;
153           d0 = txq->descs + slot;
154         }
155
156       if (n_free)
157         {
158           txq->n_enqueued -= n_free;
159           vlib_buffer_free_from_ring (vm, txq->bufs, first, txq->size,
160                                       n_free);
161         }
162     }
163
164   if (ad->flags & AVF_DEVICE_F_VA_DMA)
165     n_enq = avf_tx_enqueue (vm, txq, buffers, n_left, 1);
166   else
167     n_enq = avf_tx_enqueue (vm, txq, buffers, n_left, 0);
168
169   n_left -= n_enq;
170
171   if (n_left)
172     {
173       buffers += n_enq;
174
175       if (n_retry--)
176         goto retry;
177
178       vlib_buffer_free (vm, buffers, n_left);
179       vlib_error_count (vm, node->node_index,
180                         AVF_TX_ERROR_NO_FREE_SLOTS, n_left);
181     }
182
183   clib_spinlock_unlock_if_init (&txq->lock);
184
185   return frame->n_vectors - n_left;
186 }
187
188 /*
189  * fd.io coding-style-patch-verification: ON
190  *
191  * Local Variables:
192  * eval: (c-set-style "gnu")
193  * End:
194  */