xref: /netbsd-src/usr.sbin/intrctl/intrctl_io.c (revision 5b627c2de14c49aa4216a61bfd6a4bcea8604f00)
1 /*	$NetBSD: intrctl_io.c,v 1.5 2019/09/23 09:17:19 mrg 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.5 2019/09/23 09:17:19 mrg 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 *
intrctl_io_alloc(int retry)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 	free(buf);
80 	return NULL;
81 }
82 
83 void
intrctl_io_free(void * handle)84 intrctl_io_free(void *handle)
85 {
86 
87 	free(handle);
88 }
89 
90 int
intrctl_io_ncpus(void * handle)91 intrctl_io_ncpus(void *handle)
92 {
93 	struct intrio_list *list = handle;
94 
95 	return list->il_ncpus;
96 }
97 
98 int
intrctl_io_nintrs(void * handle)99 intrctl_io_nintrs(void *handle)
100 {
101 	struct intrio_list *list = handle;
102 
103 	return list->il_nintrs;
104 }
105 
106 struct intrio_list_line *
intrctl_io_firstline(void * handle)107 intrctl_io_firstline(void *handle)
108 {
109 	struct intrio_list *list = handle;
110 	struct intrio_list_line *next;
111 	char *buf_end;
112 
113 	buf_end = (char *)list + list->il_bufsize;
114 	next = (struct intrio_list_line *)((char *)list + list->il_lineoffset);
115 	if ((char *)next >= buf_end)
116 		return NULL;
117 
118 	return next;
119 }
120 
121 struct intrio_list_line *
intrctl_io_nextline(void * handle,struct intrio_list_line * cur)122 intrctl_io_nextline(void *handle, struct intrio_list_line *cur)
123 {
124 	struct intrio_list *list;
125 	struct intrio_list_line *next;
126 	size_t line_size;
127 	char *buf_end;
128 
129 	list = handle;
130 	buf_end = (char *)list + list->il_bufsize;
131 
132 	line_size = list->il_linesize;
133 	next = (struct intrio_list_line *)((char *)cur + line_size);
134 	if ((char *)next >= buf_end)
135 		return NULL;
136 
137 	return next;
138 }
139