xref: /netbsd-src/usr.sbin/intrctl/intrctl_io.c (revision bdc22b2e01993381dcefeff2bc9b56ca75a4235c)
1 /*	$NetBSD: intrctl_io.c,v 1.4 2018/06/23 11:11:00 jdolecek Exp $	*/
2 
3 /*
4  * Copyright (c) 2015 Internet Initiative Japan Inc.
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  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __RCSID("$NetBSD: intrctl_io.c,v 1.4 2018/06/23 11:11:00 jdolecek Exp $");
31 
32 #include <sys/sysctl.h>
33 #include <sys/intrio.h>
34 
35 #include <errno.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 
39 #include "intrctl_io.h"
40 
41 /*
42  * To support increasing the number of interrupts (devices are dynamically
43  * attached), retry sysctl(3) "retry" times.
44  */
45 void *
46 intrctl_io_alloc(int retry)
47 {
48 	size_t buf_size;
49 	int i, error;
50 	void *buf = NULL;
51 
52 	error = sysctlbyname("kern.intr.list", NULL, &buf_size, NULL, 0);
53 	if (error < 0) {
54 		goto error;
55 	}
56 
57 	buf = malloc(buf_size);
58 	if (buf == NULL) {
59 		goto error;
60 	}
61 
62 	for (i = 0; i < retry; i++) {
63 		error = sysctlbyname("kern.intr.list", buf, &buf_size, NULL, 0);
64 		if (error >= 0)
65 			return buf;
66 		else if (errno == ENOMEM) {
67 			void *temp;
68 
69 			temp = realloc(buf, buf_size);
70 			if (temp == NULL) {
71 				goto error;
72 			}
73 			buf = temp;
74 		} else {
75 			goto error;
76 		}
77 	}
78 error:
79 	if (buf != NULL)
80 		free(buf);
81 	return NULL;
82 }
83 
84 void
85 intrctl_io_free(void *handle)
86 {
87 
88 	free(handle);
89 }
90 
91 int
92 intrctl_io_ncpus(void *handle)
93 {
94 	struct intrio_list *list = handle;
95 
96 	return list->il_ncpus;
97 }
98 
99 int
100 intrctl_io_nintrs(void *handle)
101 {
102 	struct intrio_list *list = handle;
103 
104 	return list->il_nintrs;
105 }
106 
107 struct intrio_list_line *
108 intrctl_io_firstline(void *handle)
109 {
110 	struct intrio_list *list = handle;
111 	struct intrio_list_line *next;
112 	char *buf_end;
113 
114 	buf_end = (char *)list + list->il_bufsize;
115 	next = (struct intrio_list_line *)((char *)list + list->il_lineoffset);
116 	if ((char *)next >= buf_end)
117 		return NULL;
118 
119 	return next;
120 }
121 
122 struct intrio_list_line *
123 intrctl_io_nextline(void *handle, struct intrio_list_line *cur)
124 {
125 	struct intrio_list *list;
126 	struct intrio_list_line *next;
127 	size_t line_size;
128 	char *buf_end;
129 
130 	list = handle;
131 	buf_end = (char *)list + list->il_bufsize;
132 
133 	line_size = list->il_linesize;
134 	next = (struct intrio_list_line *)((char *)cur + line_size);
135 	if ((char *)next >= buf_end)
136 		return NULL;
137 
138 	return next;
139 }
140