xref: /freebsd/sys/dev/ena/ena_sysctl.c (revision 0bf48626aaa33768078f5872b922b1487b3a9296)
1 /*-
2  * BSD LICENSE
3  *
4  * Copyright (c) 2015-2019 Amazon.com, Inc. or its affiliates.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  *
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32 
33 #include "ena_sysctl.h"
34 
35 static void	ena_sysctl_add_wd(struct ena_adapter *);
36 static void	ena_sysctl_add_stats(struct ena_adapter *);
37 static void	ena_sysctl_add_tuneables(struct ena_adapter *);
38 static int	ena_sysctl_buf_ring_size(SYSCTL_HANDLER_ARGS);
39 static int	ena_sysctl_rx_queue_size(SYSCTL_HANDLER_ARGS);
40 
41 static SYSCTL_NODE(_hw, OID_AUTO, ena, CTLFLAG_RD, 0, "ENA driver parameters");
42 
43 /*
44  * Logging level for changing verbosity of the output
45  */
46 int ena_log_level = ENA_ALERT | ENA_WARNING;
47 SYSCTL_INT(_hw_ena, OID_AUTO, log_level, CTLFLAG_RWTUN,
48     &ena_log_level, 0, "Logging level indicating verbosity of the logs");
49 
50 
51 void
52 ena_sysctl_add_nodes(struct ena_adapter *adapter)
53 {
54 	ena_sysctl_add_wd(adapter);
55 	ena_sysctl_add_stats(adapter);
56 	ena_sysctl_add_tuneables(adapter);
57 }
58 
59 static void
60 ena_sysctl_add_wd(struct ena_adapter *adapter)
61 {
62 	device_t dev;
63 
64 	struct sysctl_ctx_list *ctx;
65 	struct sysctl_oid *tree;
66 	struct sysctl_oid_list *child;
67 
68 	dev = adapter->pdev;
69 
70 	ctx = device_get_sysctl_ctx(dev);
71 	tree = device_get_sysctl_tree(dev);
72 	child = SYSCTL_CHILDREN(tree);
73 
74 	/* Sysctl calls for Watchdog service */
75 	SYSCTL_ADD_INT(ctx, child, OID_AUTO, "wd_active",
76 	    CTLFLAG_RWTUN, &adapter->wd_active, 0,
77 	    "Watchdog is active");
78 
79 	SYSCTL_ADD_QUAD(ctx, child, OID_AUTO, "keep_alive_timeout",
80 	    CTLFLAG_RWTUN, &adapter->keep_alive_timeout,
81 	    "Timeout for Keep Alive messages");
82 
83 	SYSCTL_ADD_QUAD(ctx, child, OID_AUTO, "missing_tx_timeout",
84 	    CTLFLAG_RWTUN, &adapter->missing_tx_timeout,
85 	    "Timeout for TX completion");
86 
87 	SYSCTL_ADD_U32(ctx, child, OID_AUTO, "missing_tx_max_queues",
88 	    CTLFLAG_RWTUN, &adapter->missing_tx_max_queues, 0,
89 	    "Number of TX queues to check per run");
90 
91 	SYSCTL_ADD_U32(ctx, child, OID_AUTO, "missing_tx_threshold",
92 	    CTLFLAG_RWTUN, &adapter->missing_tx_threshold, 0,
93 	    "Max number of timeouted packets");
94 }
95 
96 static void
97 ena_sysctl_add_stats(struct ena_adapter *adapter)
98 {
99 	device_t dev;
100 
101 	struct ena_ring *tx_ring;
102 	struct ena_ring *rx_ring;
103 
104 	struct ena_hw_stats *hw_stats;
105 	struct ena_stats_dev *dev_stats;
106 	struct ena_stats_tx *tx_stats;
107 	struct ena_stats_rx *rx_stats;
108 	struct ena_com_stats_admin *admin_stats;
109 
110 	struct sysctl_ctx_list *ctx;
111 	struct sysctl_oid *tree;
112 	struct sysctl_oid_list *child;
113 
114 	struct sysctl_oid *queue_node, *tx_node, *rx_node, *hw_node;
115 	struct sysctl_oid *admin_node;
116 	struct sysctl_oid_list *queue_list, *tx_list, *rx_list, *hw_list;
117 	struct sysctl_oid_list *admin_list;
118 
119 #define QUEUE_NAME_LEN 32
120 	char namebuf[QUEUE_NAME_LEN];
121 	int i;
122 
123 	dev = adapter->pdev;
124 
125 	ctx = device_get_sysctl_ctx(dev);
126 	tree = device_get_sysctl_tree(dev);
127 	child = SYSCTL_CHILDREN(tree);
128 
129 	tx_ring = adapter->tx_ring;
130 	rx_ring = adapter->rx_ring;
131 
132 	hw_stats = &adapter->hw_stats;
133 	dev_stats = &adapter->dev_stats;
134 	admin_stats = &adapter->ena_dev->admin_queue.stats;
135 
136 	SYSCTL_ADD_COUNTER_U64(ctx, child, OID_AUTO, "wd_expired",
137 	    CTLFLAG_RD, &dev_stats->wd_expired,
138 	    "Watchdog expiry count");
139 	SYSCTL_ADD_COUNTER_U64(ctx, child, OID_AUTO, "interface_up",
140 	    CTLFLAG_RD, &dev_stats->interface_up,
141 	    "Network interface up count");
142 	SYSCTL_ADD_COUNTER_U64(ctx, child, OID_AUTO, "interface_down",
143 	    CTLFLAG_RD, &dev_stats->interface_down,
144 	    "Network interface down count");
145 	SYSCTL_ADD_COUNTER_U64(ctx, child, OID_AUTO, "admin_q_pause",
146 	    CTLFLAG_RD, &dev_stats->admin_q_pause,
147 	    "Admin queue pauses");
148 
149 	for (i = 0; i < adapter->num_queues; ++i, ++tx_ring, ++rx_ring) {
150 		snprintf(namebuf, QUEUE_NAME_LEN, "queue%d", i);
151 
152 		queue_node = SYSCTL_ADD_NODE(ctx, child, OID_AUTO,
153 		    namebuf, CTLFLAG_RD, NULL, "Queue Name");
154 		queue_list = SYSCTL_CHILDREN(queue_node);
155 
156 		/* TX specific stats */
157 		tx_node = SYSCTL_ADD_NODE(ctx, queue_list, OID_AUTO,
158 		    "tx_ring", CTLFLAG_RD, NULL, "TX ring");
159 		tx_list = SYSCTL_CHILDREN(tx_node);
160 
161 		tx_stats = &tx_ring->tx_stats;
162 
163 		SYSCTL_ADD_COUNTER_U64(ctx, tx_list, OID_AUTO,
164 		    "count", CTLFLAG_RD,
165 		    &tx_stats->cnt, "Packets sent");
166 		SYSCTL_ADD_COUNTER_U64(ctx, tx_list, OID_AUTO,
167 		    "bytes", CTLFLAG_RD,
168 		    &tx_stats->bytes, "Bytes sent");
169 		SYSCTL_ADD_COUNTER_U64(ctx, tx_list, OID_AUTO,
170 		    "prepare_ctx_err", CTLFLAG_RD,
171 		    &tx_stats->prepare_ctx_err,
172 		    "TX buffer preparation failures");
173 		SYSCTL_ADD_COUNTER_U64(ctx, tx_list, OID_AUTO,
174 		    "dma_mapping_err", CTLFLAG_RD,
175 		    &tx_stats->dma_mapping_err, "DMA mapping failures");
176 		SYSCTL_ADD_COUNTER_U64(ctx, tx_list, OID_AUTO,
177 		    "doorbells", CTLFLAG_RD,
178 		    &tx_stats->doorbells, "Queue doorbells");
179 		SYSCTL_ADD_COUNTER_U64(ctx, tx_list, OID_AUTO,
180 		    "missing_tx_comp", CTLFLAG_RD,
181 		    &tx_stats->missing_tx_comp, "TX completions missed");
182 		SYSCTL_ADD_COUNTER_U64(ctx, tx_list, OID_AUTO,
183 		    "bad_req_id", CTLFLAG_RD,
184 		    &tx_stats->bad_req_id, "Bad request id count");
185 		SYSCTL_ADD_COUNTER_U64(ctx, tx_list, OID_AUTO,
186 		        "mbuf_collapses", CTLFLAG_RD,
187 		        &tx_stats->collapse,
188 		        "Mbuf collapse count");
189 		SYSCTL_ADD_COUNTER_U64(ctx, tx_list, OID_AUTO,
190 		        "mbuf_collapse_err", CTLFLAG_RD,
191 		        &tx_stats->collapse_err,
192 		        "Mbuf collapse failures");
193 		SYSCTL_ADD_COUNTER_U64(ctx, tx_list, OID_AUTO,
194 		    "queue_wakeups", CTLFLAG_RD,
195 		    &tx_stats->queue_wakeup, "Queue wakeups");
196 		SYSCTL_ADD_COUNTER_U64(ctx, tx_list, OID_AUTO,
197 		    "queue_stops", CTLFLAG_RD,
198 		    &tx_stats->queue_stop, "Queue stops");
199 		SYSCTL_ADD_COUNTER_U64(ctx, tx_list, OID_AUTO,
200 		    "llq_buffer_copy", CTLFLAG_RD,
201 		    &tx_stats->llq_buffer_copy,
202 		    "Header copies for llq transaction");
203 
204 		/* RX specific stats */
205 		rx_node = SYSCTL_ADD_NODE(ctx, queue_list, OID_AUTO,
206 		    "rx_ring", CTLFLAG_RD, NULL, "RX ring");
207 		rx_list = SYSCTL_CHILDREN(rx_node);
208 
209 		rx_stats = &rx_ring->rx_stats;
210 
211 		SYSCTL_ADD_COUNTER_U64(ctx, rx_list, OID_AUTO,
212 		    "count", CTLFLAG_RD,
213 		    &rx_stats->cnt, "Packets received");
214 		SYSCTL_ADD_COUNTER_U64(ctx, rx_list, OID_AUTO,
215 		    "bytes", CTLFLAG_RD,
216 		    &rx_stats->bytes, "Bytes received");
217 		SYSCTL_ADD_COUNTER_U64(ctx, rx_list, OID_AUTO,
218 		    "refil_partial", CTLFLAG_RD,
219 		    &rx_stats->refil_partial, "Partial refilled mbufs");
220 		SYSCTL_ADD_COUNTER_U64(ctx, rx_list, OID_AUTO,
221 		    "bad_csum", CTLFLAG_RD,
222 		    &rx_stats->bad_csum, "Bad RX checksum");
223 		SYSCTL_ADD_COUNTER_U64(ctx, rx_list, OID_AUTO,
224 		    "mbuf_alloc_fail", CTLFLAG_RD,
225 		    &rx_stats->mbuf_alloc_fail, "Failed mbuf allocs");
226 		SYSCTL_ADD_COUNTER_U64(ctx, rx_list, OID_AUTO,
227 		    "mjum_alloc_fail", CTLFLAG_RD,
228 		    &rx_stats->mjum_alloc_fail, "Failed jumbo mbuf allocs");
229 		SYSCTL_ADD_COUNTER_U64(ctx, rx_list, OID_AUTO,
230 		    "dma_mapping_err", CTLFLAG_RD,
231 		    &rx_stats->dma_mapping_err, "DMA mapping errors");
232 		SYSCTL_ADD_COUNTER_U64(ctx, rx_list, OID_AUTO,
233 		    "bad_desc_num", CTLFLAG_RD,
234 		    &rx_stats->bad_desc_num, "Bad descriptor count");
235 		SYSCTL_ADD_COUNTER_U64(ctx, rx_list, OID_AUTO,
236 		    "bad_req_id", CTLFLAG_RD,
237 		    &rx_stats->bad_req_id, "Bad request id count");
238 		SYSCTL_ADD_COUNTER_U64(ctx, rx_list, OID_AUTO,
239 		    "empty_rx_ring", CTLFLAG_RD,
240 		    &rx_stats->empty_rx_ring, "RX descriptors depletion count");
241 	}
242 
243 	/* Stats read from device */
244 	hw_node = SYSCTL_ADD_NODE(ctx, child, OID_AUTO, "hw_stats",
245 	    CTLFLAG_RD, NULL, "Statistics from hardware");
246 	hw_list = SYSCTL_CHILDREN(hw_node);
247 
248 	SYSCTL_ADD_COUNTER_U64(ctx, hw_list, OID_AUTO, "rx_packets", CTLFLAG_RD,
249 	    &hw_stats->rx_packets, "Packets received");
250 	SYSCTL_ADD_COUNTER_U64(ctx, hw_list, OID_AUTO, "tx_packets", CTLFLAG_RD,
251 	    &hw_stats->tx_packets, "Packets transmitted");
252 	SYSCTL_ADD_COUNTER_U64(ctx, hw_list, OID_AUTO, "rx_bytes", CTLFLAG_RD,
253 	    &hw_stats->rx_bytes, "Bytes received");
254 	SYSCTL_ADD_COUNTER_U64(ctx, hw_list, OID_AUTO, "tx_bytes", CTLFLAG_RD,
255 	    &hw_stats->tx_bytes, "Bytes transmitted");
256 	SYSCTL_ADD_COUNTER_U64(ctx, hw_list, OID_AUTO, "rx_drops", CTLFLAG_RD,
257 	    &hw_stats->rx_drops, "Receive packet drops");
258 
259 	/* ENA Admin queue stats */
260 	admin_node = SYSCTL_ADD_NODE(ctx, child, OID_AUTO, "admin_stats",
261 	    CTLFLAG_RD, NULL, "ENA Admin Queue statistics");
262 	admin_list = SYSCTL_CHILDREN(admin_node);
263 
264 	SYSCTL_ADD_U32(ctx, admin_list, OID_AUTO, "aborted_cmd", CTLFLAG_RD,
265 	    &admin_stats->aborted_cmd, 0, "Aborted commands");
266 	SYSCTL_ADD_U32(ctx, admin_list, OID_AUTO, "sumbitted_cmd", CTLFLAG_RD,
267 	    &admin_stats->submitted_cmd, 0, "Submitted commands");
268 	SYSCTL_ADD_U32(ctx, admin_list, OID_AUTO, "completed_cmd", CTLFLAG_RD,
269 	    &admin_stats->completed_cmd, 0, "Completed commands");
270 	SYSCTL_ADD_U32(ctx, admin_list, OID_AUTO, "out_of_space", CTLFLAG_RD,
271 	    &admin_stats->out_of_space, 0, "Queue out of space");
272 	SYSCTL_ADD_U32(ctx, admin_list, OID_AUTO, "no_completion", CTLFLAG_RD,
273 	    &admin_stats->no_completion, 0, "Commands not completed");
274 }
275 
276 static void
277 ena_sysctl_add_tuneables(struct ena_adapter *adapter)
278 {
279 	device_t dev;
280 
281 	struct sysctl_ctx_list *ctx;
282 	struct sysctl_oid *tree;
283 	struct sysctl_oid_list *child;
284 
285 	dev = adapter->pdev;
286 
287 	ctx = device_get_sysctl_ctx(dev);
288 	tree = device_get_sysctl_tree(dev);
289 	child = SYSCTL_CHILDREN(tree);
290 
291 	/* Tuneable number of buffers in the buf-ring (drbr) */
292 	SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "buf_ring_size", CTLTYPE_INT |
293 	    CTLFLAG_RW, adapter, 0, ena_sysctl_buf_ring_size, "I",
294 	    "Size of the bufring");
295 
296 	/* Tuneable number of Rx ring size */
297 	SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "rx_queue_size", CTLTYPE_INT |
298 	    CTLFLAG_RW, adapter, 0, ena_sysctl_rx_queue_size, "I",
299 	    "Size of the Rx ring. The size should be a power of 2. "
300 	    "Max value is 8K");
301 }
302 
303 
304 static int
305 ena_sysctl_buf_ring_size(SYSCTL_HANDLER_ARGS)
306 {
307 	struct ena_adapter *adapter = arg1;
308 	int val;
309 	int error;
310 
311 	val = 0;
312 	error = sysctl_wire_old_buffer(req, sizeof(int));
313 	if (error == 0) {
314 		val = adapter->buf_ring_size;
315 		error = sysctl_handle_int(oidp, &val, 0, req);
316 	}
317 	if (error != 0 || req->newptr == NULL)
318 		return (error);
319 	if (val < 0)
320 		return (EINVAL);
321 
322 	device_printf(adapter->pdev,
323 	    "Requested new buf ring size: %d. Old size: %d\n",
324 	    val, adapter->buf_ring_size);
325 
326 	if (val != adapter->buf_ring_size) {
327 		adapter->buf_ring_size = val;
328 		adapter->reset_reason = ENA_REGS_RESET_OS_TRIGGER;
329 		ENA_FLAG_SET_ATOMIC(ENA_FLAG_TRIGGER_RESET, adapter);
330 	}
331 
332 	return (0);
333 }
334 
335 static int
336 ena_sysctl_rx_queue_size(SYSCTL_HANDLER_ARGS)
337 {
338 	struct ena_adapter *adapter = arg1;
339 	int val;
340 	int error;
341 
342 	val = 0;
343 	error = sysctl_wire_old_buffer(req, sizeof(int));
344 	if (error == 0) {
345 		val = adapter->rx_ring_size;
346 		error = sysctl_handle_int(oidp, &val, 0, req);
347 	}
348 	if (error != 0 || req->newptr == NULL)
349 		return (error);
350 	if  (val < 16)
351 		return (EINVAL);
352 
353 	device_printf(adapter->pdev,
354 	    "Requested new rx queue size: %d. Old size: %d\n",
355 	    val, adapter->rx_ring_size);
356 
357 	if (val != adapter->rx_ring_size) {
358 		adapter->rx_ring_size = val;
359 		adapter->reset_reason = ENA_REGS_RESET_OS_TRIGGER;
360 		ENA_FLAG_SET_ATOMIC(ENA_FLAG_TRIGGER_RESET, adapter);
361 	}
362 
363 	return (0);
364 }
365