iavf: new driver using new dev infra
[vpp.git] / src / plugins / avf / cli.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 #include <stdint.h>
18 #include <net/if.h>
19 #include <sys/ioctl.h>
20 #include <inttypes.h>
21
22 #include <vlib/vlib.h>
23 #include <vlib/unix/unix.h>
24 #include <vlib/pci/pci.h>
25 #include <vnet/ethernet/ethernet.h>
26
27 #include <avf/avf.h>
28
29 static clib_error_t *
30 avf_create_command_fn (vlib_main_t * vm, unformat_input_t * input,
31                        vlib_cli_command_t * cmd)
32 {
33   avf_create_if_args_t args;
34   u32 tmp;
35
36   clib_memset (&args, 0, sizeof (avf_create_if_args_t));
37
38   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
39     {
40       if (unformat (input, "%U", unformat_vlib_pci_addr, &args.addr))
41         ;
42       else if (unformat (input, "elog"))
43         args.enable_elog = 1;
44       else if (unformat (input, "rx-queue-size %u", &tmp))
45         args.rxq_size = tmp;
46       else if (unformat (input, "tx-queue-size %u", &tmp))
47         args.txq_size = tmp;
48       else if (unformat (input, "num-rx-queues %u", &tmp))
49         args.rxq_num = tmp;
50       else if (unformat (input, "num-tx-queues %u", &tmp))
51         args.txq_num = tmp;
52       else if (unformat (input, "name %s", &args.name))
53         ;
54       else
55         return clib_error_return (0, "unknown input `%U'",
56                                   format_unformat_error, input);
57     }
58
59   avf_create_if (vm, &args);
60
61   vec_free (args.name);
62
63   return args.error;
64 }
65
66 /* *INDENT-OFF* */
67 VLIB_CLI_COMMAND (avf_create_command, static) = {
68   .path = "create interface avf",
69   .short_help = "create interface avf <pci-address> "
70                 "[rx-queue-size <size>] [tx-queue-size <size>] "
71                 "[num-rx-queues <size>]",
72   .function = avf_create_command_fn,
73 };
74 /* *INDENT-ON* */
75
76 static clib_error_t *
77 avf_delete_command_fn (vlib_main_t * vm, unformat_input_t * input,
78                        vlib_cli_command_t * cmd)
79 {
80   u32 sw_if_index = ~0;
81   vnet_hw_interface_t *hw;
82   vnet_main_t *vnm = vnet_get_main ();
83
84   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
85     {
86       if (unformat (input, "sw_if_index %d", &sw_if_index))
87         ;
88       else if (unformat (input, "%U", unformat_vnet_sw_interface, vnm,
89                          &sw_if_index))
90         ;
91       else
92         return clib_error_return (0, "unknown input `%U'",
93                                   format_unformat_error, input);
94     }
95
96   if (sw_if_index == ~0)
97     return clib_error_return (0,
98                               "please specify interface name or sw_if_index");
99
100   hw = vnet_get_sup_hw_interface_api_visible_or_null (vnm, sw_if_index);
101   if (hw == NULL || avf_device_class.index != hw->dev_class_index)
102     return clib_error_return (0, "not an AVF interface");
103
104   vlib_process_signal_event (vm, avf_process_node.index,
105                              AVF_PROCESS_EVENT_DELETE_IF, hw->dev_instance);
106
107   return 0;
108 }
109
110 /* *INDENT-OFF* */
111 VLIB_CLI_COMMAND (avf_delete_command, static) = {
112   .path = "delete interface avf",
113   .short_help = "delete interface avf "
114     "{<interface> | sw_if_index <sw_idx>}",
115   .function = avf_delete_command_fn,
116   .is_mp_safe = 1,
117 };
118 /* *INDENT-ON* */
119
120 static clib_error_t *
121 avf_test_command_fn (vlib_main_t * vm, unformat_input_t * input,
122                      vlib_cli_command_t * cmd)
123 {
124   u32 sw_if_index = ~0;
125   vnet_hw_interface_t *hw;
126   avf_device_t *ad;
127   vnet_main_t *vnm = vnet_get_main ();
128   int test_irq = 0, enable_elog = 0, disable_elog = 0;
129
130   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
131     {
132       if (unformat (input, "sw_if_index %d", &sw_if_index))
133         ;
134       else if (unformat (input, "irq"))
135         test_irq = 1;
136       else if (unformat (input, "elog-on"))
137         enable_elog = 1;
138       else if (unformat (input, "elog-off"))
139         disable_elog = 1;
140       else if (unformat (input, "%U", unformat_vnet_sw_interface, vnm,
141                          &sw_if_index))
142         ;
143       else
144         return clib_error_return (0, "unknown input `%U'",
145                                   format_unformat_error, input);
146     }
147
148   if (sw_if_index == ~0)
149     return clib_error_return (0,
150                               "please specify interface name or sw_if_index");
151
152   hw = vnet_get_sup_hw_interface_api_visible_or_null (vnm, sw_if_index);
153   if (hw == NULL || avf_device_class.index != hw->dev_class_index)
154     return clib_error_return (0, "not a AVF interface");
155
156   ad = avf_get_device (hw->dev_instance);
157
158   if (enable_elog)
159     ad->flags |= AVF_DEVICE_F_ELOG;
160
161   if (disable_elog)
162     ad->flags &= ~AVF_DEVICE_F_ELOG;
163
164   if (test_irq)
165     avf_reg_write (ad, AVFINT_DYN_CTL0, (1 << 0) | (3 << 3) | (1 << 2));
166
167   return 0;
168 }
169
170 /* *INDENT-OFF* */
171 VLIB_CLI_COMMAND (avf_test_command, static) = {
172   .path = "test avf",
173   .short_help = "test avf [<interface> | sw_if_index <sw_idx>] [irq] "
174     "[elog-on] [elog-off]",
175   .function = avf_test_command_fn,
176 };
177 /* *INDENT-ON* */
178
179 clib_error_t *
180 avf_cli_init (vlib_main_t * vm)
181 {
182   return 0;
183 }
184
185 VLIB_INIT_FUNCTION (avf_cli_init);
186
187 /*
188  * fd.io coding-style-patch-verification: ON
189  *
190  * Local Variables:
191  * eval: (c-set-style "gnu")
192  * End:
193  */