builtinurl: mark api as deprecated
[vpp.git] / src / plugins / marvell / pp2 / pp2.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 <sys/types.h>
19 #include <sys/stat.h>
20 #include <fcntl.h>
21 #include <sys/ioctl.h>
22
23 #include <vlib/vlib.h>
24 #include <vlib/unix/unix.h>
25 #include <vnet/plugin/plugin.h>
26 #include <marvell/pp2/pp2.h>
27 #include <vnet/interface/rx_queue_funcs.h>
28
29 /* size of DMA memory used by musdk (not used for buffers) */
30 #define MV_SYS_DMA_MEM_SZ               (2 << 20)
31 /* number of HIFs reserved (first X) */
32 #define NUM_HIFS_RSVD                   4
33 /* number of buffer pools reserved (first X) */
34 #define NUM_BPOOLS_RSVD                 7
35
36 mrvl_pp2_main_t mrvl_pp2_main;
37 extern vnet_device_class_t ppa2_device_class;
38
39 static void
40 mrvl_pp2_main_deinit ()
41 {
42   mrvl_pp2_main_t *ppm = &mrvl_pp2_main;
43   int i;
44   vec_foreach_index (i, ppm->per_thread_data)
45   {
46     mrvl_pp2_per_thread_data_t *ptd = vec_elt_at_index (ppm->per_thread_data,
47                                                         i);
48     if (ptd->hif)
49       pp2_hif_deinit (ptd->hif);
50     vec_free (ptd->descs);
51   }
52   vec_free (ppm->per_thread_data);
53   pp2_deinit ();
54   mv_sys_dma_mem_destroy ();
55 }
56
57 static clib_error_t *
58 mrvl_pp2_main_init ()
59 {
60   mrvl_pp2_main_t *ppm = &mrvl_pp2_main;
61   vlib_thread_main_t *tm = vlib_get_thread_main ();
62   clib_error_t *err = 0;
63   struct pp2_init_params init_params = { 0 };
64   int i, rv;
65   u8 *s = 0;
66
67   rv = mv_sys_dma_mem_init (MV_SYS_DMA_MEM_SZ);
68   if (rv)
69     return clib_error_return (0, "mv_sys_dma_mem_init failed, rv = %u", rv);
70
71   init_params.hif_reserved_map = ((1 << NUM_HIFS_RSVD) - 1);
72   init_params.bm_pool_reserved_map = ((1 << NUM_BPOOLS_RSVD) - 1);
73   rv = pp2_init (&init_params);
74   if (rv)
75     {
76       err = clib_error_return (0, "mrvl_pp2_init failed, rv = %u", rv);
77       goto done;
78     }
79
80   vec_validate_aligned (ppm->per_thread_data, tm->n_vlib_mains - 1,
81                         CLIB_CACHE_LINE_BYTES);
82
83   vec_foreach_index (i, ppm->per_thread_data)
84   {
85     mrvl_pp2_per_thread_data_t *ptd = vec_elt_at_index (ppm->per_thread_data,
86                                                         i);
87     struct pp2_hif_params hif_params = { 0 };
88     vec_reset_length (s);
89     s = format (s, "hif-%d%c", NUM_HIFS_RSVD + i, 0);
90     hif_params.match = (char *) s;
91     hif_params.out_size = 2048; /* FIXME */
92     if (pp2_hif_init (&hif_params, &ptd->hif))
93       {
94         err = clib_error_return (0, "hif '%s' init failed", s);
95         goto done;
96       }
97   }
98
99 done:
100   if (err)
101     mrvl_pp2_main_deinit ();
102   vec_free (s);
103   return err;
104 }
105
106 static u32
107 mrvl_pp2_eth_flag_change (vnet_main_t * vnm, vnet_hw_interface_t * hi,
108                           u32 flags)
109 {
110   /* nothing for now */
111   return 0;
112 }
113
114 void
115 mrvl_pp2_delete_if (mrvl_pp2_if_t * ppif)
116 {
117   vlib_main_t *vm = vlib_get_main ();
118   vnet_main_t *vnm = vnet_get_main ();
119   mrvl_pp2_main_t *ppm = &mrvl_pp2_main;
120   mrvl_pp2_outq_t *outq;
121   mrvl_pp2_inq_t *inq;
122
123   if (ppif->hw_if_index != ~0)
124       ethernet_delete_interface (vnm, ppif->hw_if_index);
125
126   if (ppif->ppio)
127     {
128       pp2_ppio_disable (ppif->ppio);
129       pp2_ppio_deinit (ppif->ppio);
130     }
131
132   /* free buffers hanging in the tx ring */
133   vec_foreach (outq, ppif->outqs)
134     {
135       while (outq->tail < outq->head)
136         {
137           u16 slot = outq->tail & (outq->size - 1);
138           vlib_buffer_free (vm, outq->buffers + slot, 1);
139           outq->tail++;
140         }
141       vec_free (outq->buffers);
142     }
143   vec_free (ppif->outqs);
144
145   /* free buffers hangin in the rx buffer pool */
146   vec_foreach (inq, ppif->inqs)
147     if (inq->bpool)
148       {
149         u32 n_bufs = 0;
150         pp2_bpool_get_num_buffs (inq->bpool, &n_bufs);
151         while (n_bufs--)
152           {
153             struct pp2_buff_inf binf;
154             if (pp2_bpool_get_buff (ppm->per_thread_data[0].hif, inq->bpool,
155                                     &binf) == 0)
156               {
157                  u32 bi = binf.cookie;
158                  vlib_buffer_free (vm, &bi, 1);
159               }
160           }
161         pp2_bpool_deinit (inq->bpool);
162       }
163   vec_free (ppif->inqs);
164
165
166   pool_put (ppm->interfaces, ppif);
167
168   if (pool_elts (ppm->interfaces) == 0)
169     mrvl_pp2_main_deinit ();
170 }
171
172 void
173 mrvl_pp2_create_if (mrvl_pp2_create_if_args_t * args)
174 {
175   vlib_main_t *vm = vlib_get_main ();
176   vnet_main_t *vnm = vnet_get_main ();
177   vlib_thread_main_t *tm = vlib_get_thread_main ();
178   vnet_eth_interface_registration_t eir = {};
179   mrvl_pp2_main_t *ppm = &mrvl_pp2_main;
180   struct pp2_bpool_params bpool_params = { 0 };
181   struct pp2_ppio_params ppio_params = { 0 };
182   struct pp2_ppio_inq_params inq_params = { 0 };
183   vnet_sw_interface_t *sw;
184   mrvl_pp2_if_t *ppif = 0;
185   u8 pp2_id, port_id, *s = 0;
186   eth_addr_t mac_addr;
187   u8 n_outqs, n_inqs = 1;
188   int i;
189
190   if (tm->n_vlib_mains > PP2_PPIO_MAX_NUM_OUTQS)
191     {
192       args->rv = VNET_API_ERROR_INIT_FAILED;
193       args->error = clib_error_return (0, "number of threads (main + workers)"
194                                        " is bigger than number of output "
195                                        "queues (%u)", PP2_PPIO_MAX_NUM_OUTQS);
196       return;
197     }
198   n_outqs = tm->n_vlib_mains;
199
200   /* defaults */
201   args->tx_q_sz = args->tx_q_sz ? args->tx_q_sz : 2 * VLIB_FRAME_SIZE;
202   args->rx_q_sz = args->rx_q_sz ? args->rx_q_sz : 2 * VLIB_FRAME_SIZE;
203
204   if (vec_len (ppm->per_thread_data) == 0)
205     {
206       if ((args->error = mrvl_pp2_main_init ()) != 0)
207         {
208           args->rv = VNET_API_ERROR_INIT_FAILED;
209           return;
210         }
211     }
212
213   pool_get_zero (ppm->interfaces, ppif);
214   ppif->dev_instance = ppif - ppm->interfaces;
215   ppif->hw_if_index = ~0;
216   vec_validate_aligned (ppif->inqs, n_inqs - 1, CLIB_CACHE_LINE_BYTES);
217   vec_validate_aligned (ppif->outqs, n_outqs - 1, CLIB_CACHE_LINE_BYTES);
218
219   for (i = 0; i < n_inqs; i++)
220     {
221       mrvl_pp2_inq_t *inq = vec_elt_at_index (ppif->inqs, i);
222       inq->size = args->rx_q_sz;
223     }
224   for (i = 0; i < n_outqs; i++)
225     {
226       mrvl_pp2_outq_t *outq = vec_elt_at_index (ppif->outqs, i);
227       outq->size = args->tx_q_sz;
228       vec_validate_aligned (outq->buffers, outq->size, CLIB_CACHE_LINE_BYTES);
229     }
230
231   if (pp2_netdev_get_ppio_info ((char *) args->name, &pp2_id, &port_id))
232     {
233       args->rv = VNET_API_ERROR_INVALID_INTERFACE;
234       args->error = clib_error_return (0, "Invalid interface '%s'",
235                                        args->name);
236       goto error;
237     }
238
239   /* FIXME bpool bit select per pp */
240   s = format (s, "pool-%d:%d%c", pp2_id, pp2_id + 8, 0);
241   bpool_params.match = (char *) s;
242   bpool_params.buff_len = vlib_buffer_get_default_data_size (vm);
243   /* FIXME +64 ? */
244   if (pp2_bpool_init (&bpool_params, &ppif->inqs[0].bpool))
245     {
246       args->rv = VNET_API_ERROR_INIT_FAILED;
247       args->error = clib_error_return (0, "bpool '%s' init failed", s);
248       goto error;
249     }
250   vec_reset_length (s);
251
252   s = format (s, "ppio-%d:%d%c", pp2_id, port_id, 0);
253   ppio_params.match = (char *) s;
254   ppio_params.type = PP2_PPIO_T_NIC;
255   inq_params.size = args->rx_q_sz;
256   ppio_params.inqs_params.num_tcs = 1;
257   ppio_params.inqs_params.tcs_params[0].pkt_offset = 0;
258   ppio_params.inqs_params.tcs_params[0].num_in_qs = n_inqs;
259   ppio_params.inqs_params.tcs_params[0].inqs_params = &inq_params;
260   ppio_params.inqs_params.tcs_params[0].pools[0][0] = ppif->inqs[0].bpool;
261   ppio_params.outqs_params.num_outqs = n_outqs;
262   for (i = 0; i < n_outqs; i++)
263     {
264       ppio_params.outqs_params.outqs_params[i].weight = 1;
265       ppio_params.outqs_params.outqs_params[i].size = args->tx_q_sz;
266     }
267   if (pp2_ppio_init (&ppio_params, &ppif->ppio))
268     {
269       args->rv = VNET_API_ERROR_INIT_FAILED;
270       args->error = clib_error_return (0, "ppio '%s' init failed", s);
271       goto error;
272     }
273   vec_reset_length (s);
274
275   if (pp2_ppio_get_mac_addr (ppif->ppio, mac_addr))
276     {
277       args->rv = VNET_API_ERROR_INIT_FAILED;
278       args->error =
279         clib_error_return (0, "%s: pp2_ppio_get_mac_addr failed", s);
280       goto error;
281     }
282
283   eir.dev_class_index = mrvl_pp2_device_class.index;
284   eir.dev_instance = ppif->dev_instance;
285   eir.address = mac_addr;
286   eir.cb.flag_change = mrvl_pp2_eth_flag_change;
287   ppif->hw_if_index = vnet_eth_register_interface (vnm, &eir);
288
289   sw = vnet_get_hw_sw_interface (vnm, ppif->hw_if_index);
290   ppif->sw_if_index = sw->sw_if_index;
291   ppif->per_interface_next_index = ~0;
292   args->sw_if_index = sw->sw_if_index;
293   vnet_hw_if_set_input_node (vnm, ppif->hw_if_index,
294                              mrvl_pp2_input_node.index);
295   /* FIXME: only one RX queue ? */
296   ppif->inqs[0].queue_index = vnet_hw_if_register_rx_queue (
297     vnm, ppif->hw_if_index, 0, VNET_HW_IF_RXQ_THREAD_ANY);
298
299   vnet_hw_if_set_rx_queue_mode (vnm, ppif->inqs[0].queue_index,
300                                 VNET_HW_IF_RX_MODE_POLLING);
301   vnet_hw_if_update_runtime_data (vnm, ppif->hw_if_index);
302   vnet_hw_interface_set_flags (vnm, ppif->hw_if_index,
303                                VNET_HW_INTERFACE_FLAG_LINK_UP);
304   goto done;
305
306 error:
307   mrvl_pp2_delete_if (ppif);
308 done:
309   vec_free (s);
310 }
311
312 static clib_error_t *
313 mrvl_pp2_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index,
314                                   u32 flags)
315 {
316   mrvl_pp2_main_t *ppm = &mrvl_pp2_main;
317   vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
318   mrvl_pp2_if_t *ppif = pool_elt_at_index (ppm->interfaces, hw->dev_instance);
319   static clib_error_t *error = 0;
320   int is_up = (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) != 0;
321   int rv;
322
323   if (is_up)
324     rv = pp2_ppio_enable (ppif->ppio);
325   else
326     rv = pp2_ppio_disable (ppif->ppio);
327
328   if (rv)
329     return clib_error_return (0, "failed to %s interface",
330                               is_up ? "enable" : "disable");
331
332   if (is_up)
333     ppif->flags |= MRVL_PP2_IF_F_ADMIN_UP;
334   else
335     ppif->flags &= ~MRVL_PP2_IF_F_ADMIN_UP;
336
337   return error;
338 }
339
340 static void
341 mrvl_pp2_clear_interface_counters (u32 instance)
342 {
343   mrvl_pp2_main_t *ppm = &mrvl_pp2_main;
344   mrvl_pp2_if_t *ppif = pool_elt_at_index (ppm->interfaces, instance);
345   struct pp2_ppio_statistics stats;
346
347   pp2_ppio_get_statistics (ppif->ppio, &stats, 1);
348 }
349
350 static void
351 mrvl_pp2_set_interface_next_node (vnet_main_t * vnm, u32 hw_if_index,
352                                   u32 node_index)
353 {
354   mrvl_pp2_main_t *ppm = &mrvl_pp2_main;
355   vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
356   mrvl_pp2_if_t *ppif = pool_elt_at_index (ppm->interfaces, hw->dev_instance);
357
358   /* Shut off redirection */
359   if (node_index == ~0)
360     {
361       ppif->per_interface_next_index = node_index;
362       return;
363     }
364
365   ppif->per_interface_next_index =
366     vlib_node_add_next (vlib_get_main (), mrvl_pp2_input_node.index,
367                         node_index);
368 }
369
370 static char *mrvl_pp2_tx_func_error_strings[] = {
371 #define _(n,s) s,
372   foreach_mrvl_pp2_tx_func_error
373 #undef _
374 };
375
376 VNET_DEVICE_CLASS (mrvl_pp2_device_class,) =
377 {
378   .name = "Marvell PPv2 interface",
379   .format_device_name = format_mrvl_pp2_interface_name,
380   .format_device = format_mrvl_pp2_interface,
381   .tx_function = mrvl_pp2_interface_tx,
382   .tx_function_n_errors = MRVL_PP2_TX_N_ERROR,
383   .tx_function_error_strings = mrvl_pp2_tx_func_error_strings,
384   .admin_up_down_function = mrvl_pp2_interface_admin_up_down,
385   .clear_counters = mrvl_pp2_clear_interface_counters,
386   .rx_redirect_to_node = mrvl_pp2_set_interface_next_node,
387 };
388
389 static clib_error_t *
390 mrvl_pp2_init (vlib_main_t * vm)
391 {
392   return 0;
393 }
394
395 VLIB_INIT_FUNCTION (mrvl_pp2_init);
396
397 /*
398  * fd.io coding-style-patch-verification: ON
399  *
400  * Local Variables:
401  * eval: (c-set-style "gnu")
402  * End:
403  */