vlib rename vlib_frame_args(...) to vlib_frame_scalar_args(..)
[vpp.git] / src / plugins / marvell / pp2 / 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 <stdint.h>
19 #include <net/if.h>
20 #include <sys/ioctl.h>
21 #include <sys/uio.h>
22
23 #include <vlib/vlib.h>
24 #include <vlib/unix/unix.h>
25 #include <vnet/ethernet/ethernet.h>
26 #include <vnet/devices/devices.h>
27
28 #include <marvell/pp2/pp2.h>
29
30 uword
31 mrvl_pp2_interface_tx (vlib_main_t * vm,
32                        vlib_node_runtime_t * node, vlib_frame_t * frame)
33 {
34   mrvl_pp2_main_t *ppm = &mrvl_pp2_main;
35   vnet_interface_output_runtime_t *rd = (void *) node->runtime_data;
36   mrvl_pp2_if_t *ppif = pool_elt_at_index (ppm->interfaces, rd->dev_instance);
37   u32 thread_index = vm->thread_index;
38   mrvl_pp2_per_thread_data_t *ptd =
39     vec_elt_at_index (ppm->per_thread_data, thread_index);
40   u8 qid = thread_index;
41   mrvl_pp2_outq_t *outq = vec_elt_at_index (ppif->outqs, qid);
42   u32 *buffers = vlib_frame_vector_args (frame);
43   u16 n_desc = frame->n_vectors, n_left = n_desc, n_sent = n_desc, n_done;
44   struct pp2_ppio_desc *d;
45   u16 mask = outq->size - 1;
46
47   if (PREDICT_FALSE (pp2_ppio_get_num_outq_done (ppif->ppio, ptd->hif, qid,
48                                                  &n_done)))
49     {
50       n_done = 0;
51       vlib_error_count (vm, node->node_index,
52                         MRVL_PP2_TX_ERROR_PPIO_GET_NUM_OUTQ_DONE, 1);
53     }
54
55   if (n_done)
56     {
57       u16 n_free = clib_min (n_done, outq->size - (outq->tail & mask));
58       vlib_buffer_free (vm, outq->buffers + (outq->tail & mask), n_free);
59       if (PREDICT_FALSE (n_free < n_done))
60         vlib_buffer_free (vm, outq->buffers, n_done - n_free);
61       outq->tail += n_done;
62     }
63
64   vec_validate_aligned (ptd->descs, n_left, CLIB_CACHE_LINE_BYTES);
65   d = ptd->descs;
66   while (n_left)
67     {
68       u32 bi0 = buffers[0];
69       vlib_buffer_t *b0 = vlib_get_buffer (vm, bi0);
70       u64 paddr = vlib_buffer_get_pa (vm, b0);
71
72       pp2_ppio_outq_desc_reset (d);
73       pp2_ppio_outq_desc_set_phys_addr (d, paddr + b0->current_data);
74       pp2_ppio_outq_desc_set_pkt_offset (d, 0);
75       pp2_ppio_outq_desc_set_pkt_len (d, b0->current_length);
76       d++;
77       buffers++;
78       n_left--;
79     }
80
81   if (pp2_ppio_send (ppif->ppio, ptd->hif, qid, ptd->descs, &n_sent))
82     {
83       n_sent = 0;
84       vlib_error_count (vm, node->node_index, MRVL_PP2_TX_ERROR_PPIO_SEND, 1);
85     }
86
87   /* free unsent buffers */
88   if (PREDICT_FALSE (n_sent != n_desc))
89     {
90       vlib_buffer_free (vm, vlib_frame_vector_args (frame) + n_sent,
91                         frame->n_vectors - n_sent);
92       vlib_error_count (vm, node->node_index, MRVL_PP2_TX_ERROR_NO_FREE_SLOTS,
93                         frame->n_vectors - n_sent);
94     }
95
96   /* store buffer index for each enqueued packet into the ring
97      so we can know what to free after packet is sent */
98   if (n_sent)
99     {
100       u16 slot = outq->head & mask;
101       buffers = vlib_frame_vector_args (frame);
102       u16 n_copy = clib_min (outq->size - slot, n_sent);
103
104       clib_memcpy (outq->buffers + slot, buffers, n_copy * sizeof (u32));
105       if (PREDICT_FALSE (n_copy < n_sent))
106         clib_memcpy (outq->buffers, buffers + n_copy,
107                      (n_sent - n_copy) * sizeof (u32));
108
109       outq->head += n_sent;
110     }
111
112   return n_sent;
113 }
114
115 /*
116  * fd.io coding-style-patch-verification: ON
117  *
118  * Local Variables:
119  * eval: (c-set-style "gnu")
120  * End:
121  */