net/bnxt: fix Tx and Rx burst for secondary process
[dpdk.git] / drivers / net / softnic / rte_eth_softnic.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2017 Intel Corporation
3  */
4
5 #include <stdint.h>
6 #include <stdlib.h>
7 #include <string.h>
8
9 #include <rte_ethdev_driver.h>
10 #include <rte_ethdev_vdev.h>
11 #include <rte_malloc.h>
12 #include <rte_bus_vdev.h>
13 #include <rte_kvargs.h>
14 #include <rte_errno.h>
15 #include <rte_ring.h>
16 #include <rte_sched.h>
17 #include <rte_tm_driver.h>
18
19 #include "rte_eth_softnic.h"
20 #include "rte_eth_softnic_internals.h"
21
22 #define DEV_HARD(p)                                     \
23         (&rte_eth_devices[p->hard.port_id])
24
25 #define PMD_PARAM_SOFT_TM                                       "soft_tm"
26 #define PMD_PARAM_SOFT_TM_RATE                          "soft_tm_rate"
27 #define PMD_PARAM_SOFT_TM_NB_QUEUES                     "soft_tm_nb_queues"
28 #define PMD_PARAM_SOFT_TM_QSIZE0                        "soft_tm_qsize0"
29 #define PMD_PARAM_SOFT_TM_QSIZE1                        "soft_tm_qsize1"
30 #define PMD_PARAM_SOFT_TM_QSIZE2                        "soft_tm_qsize2"
31 #define PMD_PARAM_SOFT_TM_QSIZE3                        "soft_tm_qsize3"
32 #define PMD_PARAM_SOFT_TM_ENQ_BSZ                       "soft_tm_enq_bsz"
33 #define PMD_PARAM_SOFT_TM_DEQ_BSZ                       "soft_tm_deq_bsz"
34
35 #define PMD_PARAM_HARD_NAME                                     "hard_name"
36 #define PMD_PARAM_HARD_TX_QUEUE_ID                      "hard_tx_queue_id"
37
38 static const char *pmd_valid_args[] = {
39         PMD_PARAM_SOFT_TM,
40         PMD_PARAM_SOFT_TM_RATE,
41         PMD_PARAM_SOFT_TM_NB_QUEUES,
42         PMD_PARAM_SOFT_TM_QSIZE0,
43         PMD_PARAM_SOFT_TM_QSIZE1,
44         PMD_PARAM_SOFT_TM_QSIZE2,
45         PMD_PARAM_SOFT_TM_QSIZE3,
46         PMD_PARAM_SOFT_TM_ENQ_BSZ,
47         PMD_PARAM_SOFT_TM_DEQ_BSZ,
48         PMD_PARAM_HARD_NAME,
49         PMD_PARAM_HARD_TX_QUEUE_ID,
50         NULL
51 };
52
53 static const struct rte_eth_dev_info pmd_dev_info = {
54         .min_rx_bufsize = 0,
55         .max_rx_pktlen = UINT32_MAX,
56         .max_rx_queues = UINT16_MAX,
57         .max_tx_queues = UINT16_MAX,
58         .rx_desc_lim = {
59                 .nb_max = UINT16_MAX,
60                 .nb_min = 0,
61                 .nb_align = 1,
62         },
63         .tx_desc_lim = {
64                 .nb_max = UINT16_MAX,
65                 .nb_min = 0,
66                 .nb_align = 1,
67         },
68 };
69
70 static int pmd_softnic_logtype;
71
72 #define PMD_LOG(level, fmt, args...) \
73         rte_log(RTE_LOG_ ## level, pmd_softnic_logtype, \
74                 "%s(): " fmt "\n", __func__, ##args)
75
76 static void
77 pmd_dev_infos_get(struct rte_eth_dev *dev __rte_unused,
78         struct rte_eth_dev_info *dev_info)
79 {
80         memcpy(dev_info, &pmd_dev_info, sizeof(*dev_info));
81 }
82
83 static int
84 pmd_dev_configure(struct rte_eth_dev *dev)
85 {
86         struct pmd_internals *p = dev->data->dev_private;
87         struct rte_eth_dev *hard_dev = DEV_HARD(p);
88
89         if (dev->data->nb_rx_queues > hard_dev->data->nb_rx_queues)
90                 return -1;
91
92         if (p->params.hard.tx_queue_id >= hard_dev->data->nb_tx_queues)
93                 return -1;
94
95         return 0;
96 }
97
98 static int
99 pmd_rx_queue_setup(struct rte_eth_dev *dev,
100         uint16_t rx_queue_id,
101         uint16_t nb_rx_desc __rte_unused,
102         unsigned int socket_id,
103         const struct rte_eth_rxconf *rx_conf __rte_unused,
104         struct rte_mempool *mb_pool __rte_unused)
105 {
106         struct pmd_internals *p = dev->data->dev_private;
107
108         if (p->params.soft.intrusive == 0) {
109                 struct pmd_rx_queue *rxq;
110
111                 rxq = rte_zmalloc_socket(p->params.soft.name,
112                         sizeof(struct pmd_rx_queue), 0, socket_id);
113                 if (rxq == NULL)
114                         return -ENOMEM;
115
116                 rxq->hard.port_id = p->hard.port_id;
117                 rxq->hard.rx_queue_id = rx_queue_id;
118                 dev->data->rx_queues[rx_queue_id] = rxq;
119         } else {
120                 struct rte_eth_dev *hard_dev = DEV_HARD(p);
121                 void *rxq = hard_dev->data->rx_queues[rx_queue_id];
122
123                 if (rxq == NULL)
124                         return -1;
125
126                 dev->data->rx_queues[rx_queue_id] = rxq;
127         }
128         return 0;
129 }
130
131 static int
132 pmd_tx_queue_setup(struct rte_eth_dev *dev,
133         uint16_t tx_queue_id,
134         uint16_t nb_tx_desc,
135         unsigned int socket_id,
136         const struct rte_eth_txconf *tx_conf __rte_unused)
137 {
138         uint32_t size = RTE_ETH_NAME_MAX_LEN + strlen("_txq") + 4;
139         char name[size];
140         struct rte_ring *r;
141
142         snprintf(name, sizeof(name), "%s_txq%04x",
143                 dev->data->name, tx_queue_id);
144         r = rte_ring_create(name, nb_tx_desc, socket_id,
145                 RING_F_SP_ENQ | RING_F_SC_DEQ);
146         if (r == NULL)
147                 return -1;
148
149         dev->data->tx_queues[tx_queue_id] = r;
150         return 0;
151 }
152
153 static int
154 pmd_dev_start(struct rte_eth_dev *dev)
155 {
156         struct pmd_internals *p = dev->data->dev_private;
157
158         if (tm_used(dev)) {
159                 int status = tm_start(p);
160
161                 if (status)
162                         return status;
163         }
164
165         dev->data->dev_link.link_status = ETH_LINK_UP;
166
167         if (p->params.soft.intrusive) {
168                 struct rte_eth_dev *hard_dev = DEV_HARD(p);
169
170                 /* The hard_dev->rx_pkt_burst should be stable by now */
171                 dev->rx_pkt_burst = hard_dev->rx_pkt_burst;
172         }
173
174         return 0;
175 }
176
177 static void
178 pmd_dev_stop(struct rte_eth_dev *dev)
179 {
180         struct pmd_internals *p = dev->data->dev_private;
181
182         dev->data->dev_link.link_status = ETH_LINK_DOWN;
183
184         if (tm_used(dev))
185                 tm_stop(p);
186 }
187
188 static void
189 pmd_dev_close(struct rte_eth_dev *dev)
190 {
191         uint32_t i;
192
193         /* TX queues */
194         for (i = 0; i < dev->data->nb_tx_queues; i++)
195                 rte_ring_free((struct rte_ring *)dev->data->tx_queues[i]);
196 }
197
198 static int
199 pmd_link_update(struct rte_eth_dev *dev __rte_unused,
200         int wait_to_complete __rte_unused)
201 {
202         return 0;
203 }
204
205 static int
206 pmd_tm_ops_get(struct rte_eth_dev *dev, void *arg)
207 {
208         *(const struct rte_tm_ops **)arg =
209                 (tm_enabled(dev)) ? &pmd_tm_ops : NULL;
210
211         return 0;
212 }
213
214 static const struct eth_dev_ops pmd_ops = {
215         .dev_configure = pmd_dev_configure,
216         .dev_start = pmd_dev_start,
217         .dev_stop = pmd_dev_stop,
218         .dev_close = pmd_dev_close,
219         .link_update = pmd_link_update,
220         .dev_infos_get = pmd_dev_infos_get,
221         .rx_queue_setup = pmd_rx_queue_setup,
222         .tx_queue_setup = pmd_tx_queue_setup,
223         .tm_ops_get = pmd_tm_ops_get,
224 };
225
226 static uint16_t
227 pmd_rx_pkt_burst(void *rxq,
228         struct rte_mbuf **rx_pkts,
229         uint16_t nb_pkts)
230 {
231         struct pmd_rx_queue *rx_queue = rxq;
232
233         return rte_eth_rx_burst(rx_queue->hard.port_id,
234                 rx_queue->hard.rx_queue_id,
235                 rx_pkts,
236                 nb_pkts);
237 }
238
239 static uint16_t
240 pmd_tx_pkt_burst(void *txq,
241         struct rte_mbuf **tx_pkts,
242         uint16_t nb_pkts)
243 {
244         return (uint16_t)rte_ring_enqueue_burst(txq,
245                 (void **)tx_pkts,
246                 nb_pkts,
247                 NULL);
248 }
249
250 static __rte_always_inline int
251 run_default(struct rte_eth_dev *dev)
252 {
253         struct pmd_internals *p = dev->data->dev_private;
254
255         /* Persistent context: Read Only (update not required) */
256         struct rte_mbuf **pkts = p->soft.def.pkts;
257         uint16_t nb_tx_queues = dev->data->nb_tx_queues;
258
259         /* Persistent context: Read - Write (update required) */
260         uint32_t txq_pos = p->soft.def.txq_pos;
261         uint32_t pkts_len = p->soft.def.pkts_len;
262         uint32_t flush_count = p->soft.def.flush_count;
263
264         /* Not part of the persistent context */
265         uint32_t pos;
266         uint16_t i;
267
268         /* Soft device TXQ read, Hard device TXQ write */
269         for (i = 0; i < nb_tx_queues; i++) {
270                 struct rte_ring *txq = dev->data->tx_queues[txq_pos];
271
272                 /* Read soft device TXQ burst to packet enqueue buffer */
273                 pkts_len += rte_ring_sc_dequeue_burst(txq,
274                         (void **)&pkts[pkts_len],
275                         DEFAULT_BURST_SIZE,
276                         NULL);
277
278                 /* Increment soft device TXQ */
279                 txq_pos++;
280                 if (txq_pos >= nb_tx_queues)
281                         txq_pos = 0;
282
283                 /* Hard device TXQ write when complete burst is available */
284                 if (pkts_len >= DEFAULT_BURST_SIZE) {
285                         for (pos = 0; pos < pkts_len; )
286                                 pos += rte_eth_tx_burst(p->hard.port_id,
287                                         p->params.hard.tx_queue_id,
288                                         &pkts[pos],
289                                         (uint16_t)(pkts_len - pos));
290
291                         pkts_len = 0;
292                         flush_count = 0;
293                         break;
294                 }
295         }
296
297         if (flush_count >= FLUSH_COUNT_THRESHOLD) {
298                 for (pos = 0; pos < pkts_len; )
299                         pos += rte_eth_tx_burst(p->hard.port_id,
300                                 p->params.hard.tx_queue_id,
301                                 &pkts[pos],
302                                 (uint16_t)(pkts_len - pos));
303
304                 pkts_len = 0;
305                 flush_count = 0;
306         }
307
308         p->soft.def.txq_pos = txq_pos;
309         p->soft.def.pkts_len = pkts_len;
310         p->soft.def.flush_count = flush_count + 1;
311
312         return 0;
313 }
314
315 static __rte_always_inline int
316 run_tm(struct rte_eth_dev *dev)
317 {
318         struct pmd_internals *p = dev->data->dev_private;
319
320         /* Persistent context: Read Only (update not required) */
321         struct rte_sched_port *sched = p->soft.tm.sched;
322         struct rte_mbuf **pkts_enq = p->soft.tm.pkts_enq;
323         struct rte_mbuf **pkts_deq = p->soft.tm.pkts_deq;
324         uint32_t enq_bsz = p->params.soft.tm.enq_bsz;
325         uint32_t deq_bsz = p->params.soft.tm.deq_bsz;
326         uint16_t nb_tx_queues = dev->data->nb_tx_queues;
327
328         /* Persistent context: Read - Write (update required) */
329         uint32_t txq_pos = p->soft.tm.txq_pos;
330         uint32_t pkts_enq_len = p->soft.tm.pkts_enq_len;
331         uint32_t flush_count = p->soft.tm.flush_count;
332
333         /* Not part of the persistent context */
334         uint32_t pkts_deq_len, pos;
335         uint16_t i;
336
337         /* Soft device TXQ read, TM enqueue */
338         for (i = 0; i < nb_tx_queues; i++) {
339                 struct rte_ring *txq = dev->data->tx_queues[txq_pos];
340
341                 /* Read TXQ burst to packet enqueue buffer */
342                 pkts_enq_len += rte_ring_sc_dequeue_burst(txq,
343                         (void **)&pkts_enq[pkts_enq_len],
344                         enq_bsz,
345                         NULL);
346
347                 /* Increment TXQ */
348                 txq_pos++;
349                 if (txq_pos >= nb_tx_queues)
350                         txq_pos = 0;
351
352                 /* TM enqueue when complete burst is available */
353                 if (pkts_enq_len >= enq_bsz) {
354                         rte_sched_port_enqueue(sched, pkts_enq, pkts_enq_len);
355
356                         pkts_enq_len = 0;
357                         flush_count = 0;
358                         break;
359                 }
360         }
361
362         if (flush_count >= FLUSH_COUNT_THRESHOLD) {
363                 if (pkts_enq_len)
364                         rte_sched_port_enqueue(sched, pkts_enq, pkts_enq_len);
365
366                 pkts_enq_len = 0;
367                 flush_count = 0;
368         }
369
370         p->soft.tm.txq_pos = txq_pos;
371         p->soft.tm.pkts_enq_len = pkts_enq_len;
372         p->soft.tm.flush_count = flush_count + 1;
373
374         /* TM dequeue, Hard device TXQ write */
375         pkts_deq_len = rte_sched_port_dequeue(sched, pkts_deq, deq_bsz);
376
377         for (pos = 0; pos < pkts_deq_len; )
378                 pos += rte_eth_tx_burst(p->hard.port_id,
379                         p->params.hard.tx_queue_id,
380                         &pkts_deq[pos],
381                         (uint16_t)(pkts_deq_len - pos));
382
383         return 0;
384 }
385
386 int
387 rte_pmd_softnic_run(uint16_t port_id)
388 {
389         struct rte_eth_dev *dev = &rte_eth_devices[port_id];
390
391 #ifdef RTE_LIBRTE_ETHDEV_DEBUG
392         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, 0);
393 #endif
394
395         return (tm_used(dev)) ? run_tm(dev) : run_default(dev);
396 }
397
398 static struct ether_addr eth_addr = { .addr_bytes = {0} };
399
400 static uint32_t
401 eth_dev_speed_max_mbps(uint32_t speed_capa)
402 {
403         uint32_t rate_mbps[32] = {
404                 ETH_SPEED_NUM_NONE,
405                 ETH_SPEED_NUM_10M,
406                 ETH_SPEED_NUM_10M,
407                 ETH_SPEED_NUM_100M,
408                 ETH_SPEED_NUM_100M,
409                 ETH_SPEED_NUM_1G,
410                 ETH_SPEED_NUM_2_5G,
411                 ETH_SPEED_NUM_5G,
412                 ETH_SPEED_NUM_10G,
413                 ETH_SPEED_NUM_20G,
414                 ETH_SPEED_NUM_25G,
415                 ETH_SPEED_NUM_40G,
416                 ETH_SPEED_NUM_50G,
417                 ETH_SPEED_NUM_56G,
418                 ETH_SPEED_NUM_100G,
419         };
420
421         uint32_t pos = (speed_capa) ? (31 - __builtin_clz(speed_capa)) : 0;
422         return rate_mbps[pos];
423 }
424
425 static int
426 default_init(struct pmd_internals *p,
427         struct pmd_params *params,
428         int numa_node)
429 {
430         p->soft.def.pkts = rte_zmalloc_socket(params->soft.name,
431                 2 * DEFAULT_BURST_SIZE * sizeof(struct rte_mbuf *),
432                 0,
433                 numa_node);
434
435         if (p->soft.def.pkts == NULL)
436                 return -ENOMEM;
437
438         return 0;
439 }
440
441 static void
442 default_free(struct pmd_internals *p)
443 {
444         rte_free(p->soft.def.pkts);
445 }
446
447 static void *
448 pmd_init(struct pmd_params *params, int numa_node)
449 {
450         struct pmd_internals *p;
451         int status;
452
453         p = rte_zmalloc_socket(params->soft.name,
454                 sizeof(struct pmd_internals),
455                 0,
456                 numa_node);
457         if (p == NULL)
458                 return NULL;
459
460         memcpy(&p->params, params, sizeof(p->params));
461         rte_eth_dev_get_port_by_name(params->hard.name, &p->hard.port_id);
462
463         /* Default */
464         status = default_init(p, params, numa_node);
465         if (status) {
466                 free(p->params.hard.name);
467                 rte_free(p);
468                 return NULL;
469         }
470
471         /* Traffic Management (TM)*/
472         if (params->soft.flags & PMD_FEATURE_TM) {
473                 status = tm_init(p, params, numa_node);
474                 if (status) {
475                         default_free(p);
476                         free(p->params.hard.name);
477                         rte_free(p);
478                         return NULL;
479                 }
480         }
481
482         return p;
483 }
484
485 static void
486 pmd_free(struct pmd_internals *p)
487 {
488         if (p->params.soft.flags & PMD_FEATURE_TM)
489                 tm_free(p);
490
491         default_free(p);
492
493         free(p->params.hard.name);
494         rte_free(p);
495 }
496
497 static int
498 pmd_ethdev_register(struct rte_vdev_device *vdev,
499         struct pmd_params *params,
500         void *dev_private)
501 {
502         struct rte_eth_dev_info hard_info;
503         struct rte_eth_dev *soft_dev;
504         uint32_t hard_speed;
505         int numa_node;
506         uint16_t hard_port_id;
507
508         rte_eth_dev_get_port_by_name(params->hard.name, &hard_port_id);
509         rte_eth_dev_info_get(hard_port_id, &hard_info);
510         hard_speed = eth_dev_speed_max_mbps(hard_info.speed_capa);
511         numa_node = rte_eth_dev_socket_id(hard_port_id);
512
513         /* Ethdev entry allocation */
514         soft_dev = rte_eth_dev_allocate(params->soft.name);
515         if (!soft_dev)
516                 return -ENOMEM;
517
518         /* dev */
519         soft_dev->rx_pkt_burst = (params->soft.intrusive) ?
520                 NULL : /* set up later */
521                 pmd_rx_pkt_burst;
522         soft_dev->tx_pkt_burst = pmd_tx_pkt_burst;
523         soft_dev->tx_pkt_prepare = NULL;
524         soft_dev->dev_ops = &pmd_ops;
525         soft_dev->device = &vdev->device;
526
527         /* dev->data */
528         soft_dev->data->dev_private = dev_private;
529         soft_dev->data->dev_link.link_speed = hard_speed;
530         soft_dev->data->dev_link.link_duplex = ETH_LINK_FULL_DUPLEX;
531         soft_dev->data->dev_link.link_autoneg = ETH_LINK_FIXED;
532         soft_dev->data->dev_link.link_status = ETH_LINK_DOWN;
533         soft_dev->data->mac_addrs = &eth_addr;
534         soft_dev->data->promiscuous = 1;
535         soft_dev->data->kdrv = RTE_KDRV_NONE;
536         soft_dev->data->numa_node = numa_node;
537
538         rte_eth_dev_probing_finish(soft_dev);
539
540         return 0;
541 }
542
543 static int
544 get_string(const char *key __rte_unused, const char *value, void *extra_args)
545 {
546         if (!value || !extra_args)
547                 return -EINVAL;
548
549         *(char **)extra_args = strdup(value);
550
551         if (!*(char **)extra_args)
552                 return -ENOMEM;
553
554         return 0;
555 }
556
557 static int
558 get_uint32(const char *key __rte_unused, const char *value, void *extra_args)
559 {
560         if (!value || !extra_args)
561                 return -EINVAL;
562
563         *(uint32_t *)extra_args = strtoull(value, NULL, 0);
564
565         return 0;
566 }
567
568 static int
569 pmd_parse_args(struct pmd_params *p, const char *name, const char *params)
570 {
571         struct rte_kvargs *kvlist;
572         int i, ret;
573
574         kvlist = rte_kvargs_parse(params, pmd_valid_args);
575         if (kvlist == NULL)
576                 return -EINVAL;
577
578         /* Set default values */
579         memset(p, 0, sizeof(*p));
580         p->soft.name = name;
581         p->soft.intrusive = INTRUSIVE;
582         p->soft.tm.rate = 0;
583         p->soft.tm.nb_queues = SOFTNIC_SOFT_TM_NB_QUEUES;
584         for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
585                 p->soft.tm.qsize[i] = SOFTNIC_SOFT_TM_QUEUE_SIZE;
586         p->soft.tm.enq_bsz = SOFTNIC_SOFT_TM_ENQ_BSZ;
587         p->soft.tm.deq_bsz = SOFTNIC_SOFT_TM_DEQ_BSZ;
588         p->hard.tx_queue_id = SOFTNIC_HARD_TX_QUEUE_ID;
589
590         /* SOFT: TM (optional) */
591         if (rte_kvargs_count(kvlist, PMD_PARAM_SOFT_TM) == 1) {
592                 char *s;
593
594                 ret = rte_kvargs_process(kvlist, PMD_PARAM_SOFT_TM,
595                         &get_string, &s);
596                 if (ret < 0)
597                         goto out_free;
598
599                 if (strcmp(s, "on") == 0)
600                         p->soft.flags |= PMD_FEATURE_TM;
601                 else if (strcmp(s, "off") == 0)
602                         p->soft.flags &= ~PMD_FEATURE_TM;
603                 else
604                         ret = -EINVAL;
605
606                 free(s);
607                 if (ret)
608                         goto out_free;
609         }
610
611         /* SOFT: TM rate (measured in bytes/second) (optional) */
612         if (rte_kvargs_count(kvlist, PMD_PARAM_SOFT_TM_RATE) == 1) {
613                 ret = rte_kvargs_process(kvlist, PMD_PARAM_SOFT_TM_RATE,
614                         &get_uint32, &p->soft.tm.rate);
615                 if (ret < 0)
616                         goto out_free;
617
618                 p->soft.flags |= PMD_FEATURE_TM;
619         }
620
621         /* SOFT: TM number of queues (optional) */
622         if (rte_kvargs_count(kvlist, PMD_PARAM_SOFT_TM_NB_QUEUES) == 1) {
623                 ret = rte_kvargs_process(kvlist, PMD_PARAM_SOFT_TM_NB_QUEUES,
624                         &get_uint32, &p->soft.tm.nb_queues);
625                 if (ret < 0)
626                         goto out_free;
627
628                 p->soft.flags |= PMD_FEATURE_TM;
629         }
630
631         /* SOFT: TM queue size 0 .. 3 (optional) */
632         if (rte_kvargs_count(kvlist, PMD_PARAM_SOFT_TM_QSIZE0) == 1) {
633                 uint32_t qsize;
634
635                 ret = rte_kvargs_process(kvlist, PMD_PARAM_SOFT_TM_QSIZE0,
636                         &get_uint32, &qsize);
637                 if (ret < 0)
638                         goto out_free;
639
640                 p->soft.tm.qsize[0] = (uint16_t)qsize;
641                 p->soft.flags |= PMD_FEATURE_TM;
642         }
643
644         if (rte_kvargs_count(kvlist, PMD_PARAM_SOFT_TM_QSIZE1) == 1) {
645                 uint32_t qsize;
646
647                 ret = rte_kvargs_process(kvlist, PMD_PARAM_SOFT_TM_QSIZE1,
648                         &get_uint32, &qsize);
649                 if (ret < 0)
650                         goto out_free;
651
652                 p->soft.tm.qsize[1] = (uint16_t)qsize;
653                 p->soft.flags |= PMD_FEATURE_TM;
654         }
655
656         if (rte_kvargs_count(kvlist, PMD_PARAM_SOFT_TM_QSIZE2) == 1) {
657                 uint32_t qsize;
658
659                 ret = rte_kvargs_process(kvlist, PMD_PARAM_SOFT_TM_QSIZE2,
660                         &get_uint32, &qsize);
661                 if (ret < 0)
662                         goto out_free;
663
664                 p->soft.tm.qsize[2] = (uint16_t)qsize;
665                 p->soft.flags |= PMD_FEATURE_TM;
666         }
667
668         if (rte_kvargs_count(kvlist, PMD_PARAM_SOFT_TM_QSIZE3) == 1) {
669                 uint32_t qsize;
670
671                 ret = rte_kvargs_process(kvlist, PMD_PARAM_SOFT_TM_QSIZE3,
672                         &get_uint32, &qsize);
673                 if (ret < 0)
674                         goto out_free;
675
676                 p->soft.tm.qsize[3] = (uint16_t)qsize;
677                 p->soft.flags |= PMD_FEATURE_TM;
678         }
679
680         /* SOFT: TM enqueue burst size (optional) */
681         if (rte_kvargs_count(kvlist, PMD_PARAM_SOFT_TM_ENQ_BSZ) == 1) {
682                 ret = rte_kvargs_process(kvlist, PMD_PARAM_SOFT_TM_ENQ_BSZ,
683                         &get_uint32, &p->soft.tm.enq_bsz);
684                 if (ret < 0)
685                         goto out_free;
686
687                 p->soft.flags |= PMD_FEATURE_TM;
688         }
689
690         /* SOFT: TM dequeue burst size (optional) */
691         if (rte_kvargs_count(kvlist, PMD_PARAM_SOFT_TM_DEQ_BSZ) == 1) {
692                 ret = rte_kvargs_process(kvlist, PMD_PARAM_SOFT_TM_DEQ_BSZ,
693                         &get_uint32, &p->soft.tm.deq_bsz);
694                 if (ret < 0)
695                         goto out_free;
696
697                 p->soft.flags |= PMD_FEATURE_TM;
698         }
699
700         /* HARD: name (mandatory) */
701         if (rte_kvargs_count(kvlist, PMD_PARAM_HARD_NAME) == 1) {
702                 ret = rte_kvargs_process(kvlist, PMD_PARAM_HARD_NAME,
703                         &get_string, &p->hard.name);
704                 if (ret < 0)
705                         goto out_free;
706         } else {
707                 ret = -EINVAL;
708                 goto out_free;
709         }
710
711         /* HARD: tx_queue_id (optional) */
712         if (rte_kvargs_count(kvlist, PMD_PARAM_HARD_TX_QUEUE_ID) == 1) {
713                 ret = rte_kvargs_process(kvlist, PMD_PARAM_HARD_TX_QUEUE_ID,
714                         &get_uint32, &p->hard.tx_queue_id);
715                 if (ret < 0)
716                         goto out_free;
717         }
718
719 out_free:
720         rte_kvargs_free(kvlist);
721         return ret;
722 }
723
724 static int
725 pmd_probe(struct rte_vdev_device *vdev)
726 {
727         struct pmd_params p;
728         const char *params;
729         int status;
730
731         struct rte_eth_dev_info hard_info;
732         uint32_t hard_speed;
733         uint16_t hard_port_id;
734         int numa_node;
735         void *dev_private;
736         struct rte_eth_dev *eth_dev;
737         const char *name = rte_vdev_device_name(vdev);
738
739         PMD_LOG(INFO, "Probing device \"%s\"", name);
740
741         /* Parse input arguments */
742         params = rte_vdev_device_args(vdev);
743
744         if (rte_eal_process_type() == RTE_PROC_SECONDARY &&
745             strlen(params) == 0) {
746                 eth_dev = rte_eth_dev_attach_secondary(name);
747                 if (!eth_dev) {
748                         PMD_LOG(ERR, "Failed to probe %s", name);
749                         return -1;
750                 }
751                 /* TODO: request info from primary to set up Rx and Tx */
752                 eth_dev->dev_ops = &pmd_ops;
753                 rte_eth_dev_probing_finish(eth_dev);
754                 return 0;
755         }
756
757         if (!params)
758                 return -EINVAL;
759
760         status = pmd_parse_args(&p, rte_vdev_device_name(vdev), params);
761         if (status)
762                 return status;
763
764         /* Check input arguments */
765         if (rte_eth_dev_get_port_by_name(p.hard.name, &hard_port_id))
766                 return -EINVAL;
767
768         rte_eth_dev_info_get(hard_port_id, &hard_info);
769         hard_speed = eth_dev_speed_max_mbps(hard_info.speed_capa);
770         numa_node = rte_eth_dev_socket_id(hard_port_id);
771
772         if (p.hard.tx_queue_id >= hard_info.max_tx_queues)
773                 return -EINVAL;
774
775         if (p.soft.flags & PMD_FEATURE_TM) {
776                 status = tm_params_check(&p, hard_speed);
777
778                 if (status)
779                         return status;
780         }
781
782         /* Allocate and initialize soft ethdev private data */
783         dev_private = pmd_init(&p, numa_node);
784         if (dev_private == NULL)
785                 return -ENOMEM;
786
787         /* Register soft ethdev */
788         PMD_LOG(INFO,
789                 "Creating soft ethdev \"%s\" for hard ethdev \"%s\"",
790                 p.soft.name, p.hard.name);
791
792         status = pmd_ethdev_register(vdev, &p, dev_private);
793         if (status) {
794                 pmd_free(dev_private);
795                 return status;
796         }
797
798         return 0;
799 }
800
801 static int
802 pmd_remove(struct rte_vdev_device *vdev)
803 {
804         struct rte_eth_dev *dev = NULL;
805         struct pmd_internals *p;
806
807         if (!vdev)
808                 return -EINVAL;
809
810         PMD_LOG(INFO, "Removing device \"%s\"",
811                 rte_vdev_device_name(vdev));
812
813         /* Find the ethdev entry */
814         dev = rte_eth_dev_allocated(rte_vdev_device_name(vdev));
815         if (dev == NULL)
816                 return -ENODEV;
817         p = dev->data->dev_private;
818
819         /* Free device data structures*/
820         pmd_free(p);
821         rte_free(dev->data);
822         rte_eth_dev_release_port(dev);
823
824         return 0;
825 }
826
827 static struct rte_vdev_driver pmd_softnic_drv = {
828         .probe = pmd_probe,
829         .remove = pmd_remove,
830 };
831
832 RTE_PMD_REGISTER_VDEV(net_softnic, pmd_softnic_drv);
833 RTE_PMD_REGISTER_PARAM_STRING(net_softnic,
834         PMD_PARAM_SOFT_TM        "=on|off "
835         PMD_PARAM_SOFT_TM_RATE "=<int> "
836         PMD_PARAM_SOFT_TM_NB_QUEUES "=<int> "
837         PMD_PARAM_SOFT_TM_QSIZE0 "=<int> "
838         PMD_PARAM_SOFT_TM_QSIZE1 "=<int> "
839         PMD_PARAM_SOFT_TM_QSIZE2 "=<int> "
840         PMD_PARAM_SOFT_TM_QSIZE3 "=<int> "
841         PMD_PARAM_SOFT_TM_ENQ_BSZ "=<int> "
842         PMD_PARAM_SOFT_TM_DEQ_BSZ "=<int> "
843         PMD_PARAM_HARD_NAME "=<string> "
844         PMD_PARAM_HARD_TX_QUEUE_ID "=<int>");
845
846 RTE_INIT(pmd_softnic_init_log);
847 static void
848 pmd_softnic_init_log(void)
849 {
850         pmd_softnic_logtype = rte_log_register("pmd.net.softnic");
851         if (pmd_softnic_logtype >= 0)
852                 rte_log_set_level(pmd_softnic_logtype, RTE_LOG_NOTICE);
853 }