7bcd5951ea57b1f228fc400638a4e0a984fddd25
[deb_dpdk.git] / drivers / net / sfc / sfc_kvargs.c
1 /*-
2  *   BSD LICENSE
3  *
4  * Copyright (c) 2016-2017 Solarflare Communications Inc.
5  * All rights reserved.
6  *
7  * This software was jointly developed between OKTET Labs (under contract
8  * for Solarflare) and Solarflare Communications, Inc.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright notice,
14  *    this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright notice,
16  *    this list of conditions and the following disclaimer in the documentation
17  *    and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
29  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31
32 #include <stdbool.h>
33 #include <strings.h>
34
35 #include <rte_devargs.h>
36 #include <rte_kvargs.h>
37
38 #include "sfc.h"
39 #include "sfc_kvargs.h"
40
41 int
42 sfc_kvargs_parse(struct sfc_adapter *sa)
43 {
44         struct rte_eth_dev *eth_dev = (sa)->eth_dev;
45         struct rte_devargs *devargs = eth_dev->device->devargs;
46         const char **params = (const char *[]){
47                 SFC_KVARG_STATS_UPDATE_PERIOD_MS,
48                 SFC_KVARG_DEBUG_INIT,
49                 SFC_KVARG_MCDI_LOGGING,
50                 SFC_KVARG_PERF_PROFILE,
51                 SFC_KVARG_RX_DATAPATH,
52                 SFC_KVARG_TX_DATAPATH,
53                 NULL,
54         };
55
56         if (devargs == NULL)
57                 return 0;
58
59         sa->kvargs = rte_kvargs_parse(devargs->args, params);
60         if (sa->kvargs == NULL)
61                 return EINVAL;
62
63         return 0;
64 }
65
66 void
67 sfc_kvargs_cleanup(struct sfc_adapter *sa)
68 {
69         rte_kvargs_free(sa->kvargs);
70 }
71
72 static int
73 sfc_kvarg_match_value(const char *value, const char * const *values,
74                       unsigned int n_values)
75 {
76         unsigned int i;
77
78         for (i = 0; i < n_values; ++i)
79                 if (strcasecmp(value, values[i]) == 0)
80                         return 1;
81
82         return 0;
83 }
84
85 int
86 sfc_kvargs_process(struct sfc_adapter *sa, const char *key_match,
87                    arg_handler_t handler, void *opaque_arg)
88 {
89         if (sa->kvargs == NULL)
90                 return 0;
91
92         return -rte_kvargs_process(sa->kvargs, key_match, handler, opaque_arg);
93 }
94
95 int
96 sfc_kvarg_bool_handler(__rte_unused const char *key,
97                        const char *value_str, void *opaque)
98 {
99         const char * const true_strs[] = {
100                 "1", "y", "yes", "on", "true"
101         };
102         const char * const false_strs[] = {
103                 "0", "n", "no", "off", "false"
104         };
105         bool *value = opaque;
106
107         if (sfc_kvarg_match_value(value_str, true_strs,
108                                   RTE_DIM(true_strs)))
109                 *value = true;
110         else if (sfc_kvarg_match_value(value_str, false_strs,
111                                        RTE_DIM(false_strs)))
112                 *value = false;
113         else
114                 return -EINVAL;
115
116         return 0;
117 }
118
119 int
120 sfc_kvarg_long_handler(__rte_unused const char *key,
121                        const char *value_str, void *opaque)
122 {
123         long value;
124         char *endptr;
125
126         if (!value_str || !opaque)
127                 return -EINVAL;
128
129         value = strtol(value_str, &endptr, 0);
130         if (endptr == value_str)
131                 return -EINVAL;
132
133         *(long *)opaque = value;
134
135         return 0;
136 }
137
138 int
139 sfc_kvarg_string_handler(__rte_unused const char *key,
140                          const char *value_str, void *opaque)
141 {
142         *(const char **)opaque = value_str;
143
144         return 0;
145 }