xref: /netbsd-src/usr.sbin/intrctl/intrctl_io.c (revision 946379e7b37692fc43f68eb0d1c10daa0a7f3b6c)
1 /*	$NetBSD: intrctl_io.c,v 1.1 2015/08/17 06:42:46 knakahara 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.1 2015/08/17 06:42:46 knakahara 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;
51 
52 	error = sysctlbyname("kern.intr.list", NULL, &buf_size, NULL, 0);
53 	if (error < 0) {
54 		return NULL;
55 	}
56 
57 	buf = malloc(buf_size);
58 	if (buf == NULL) {
59 		return NULL;
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 (error == -ENOMEM) {
67 			void *temp;
68 
69 			temp = realloc(buf, buf_size);
70 			if (temp == NULL) {
71 				free(buf);
72 				return NULL;
73 			}
74 			buf = temp;
75 		} else {
76 			free(buf);
77 			return NULL;
78 		}
79 	}
80 	return NULL;
81 }
82 
83 void
84 intrctl_io_free(void *handle)
85 {
86 
87 	free(handle);
88 }
89 
90 int
91 intrctl_io_ncpus(void *handle)
92 {
93 	struct intrio_list *list = handle;
94 
95 	return list->il_ncpus;
96 }
97 
98 int
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 *
107 intrctl_io_firstline(void *handle)
108 {
109 	struct intrio_list *list = handle;
110 
111 	return (struct intrio_list_line *)((char *)list + list->il_lineoffset);
112 }
113 
114 struct intrio_list_line *
115 intrctl_io_nextline(void *handle, struct intrio_list_line *cur)
116 {
117 	struct intrio_list *list;
118 	struct intrio_list_line *next;
119 	size_t line_size;
120 	char *buf_end;
121 
122 	list = handle;
123 	buf_end = (char *)list + list->il_bufsize;
124 
125 	line_size = list->il_linesize;
126 	next = (struct intrio_list_line *)((char *)cur + line_size);
127 	if ((char *)next >= buf_end)
128 		return NULL;
129 
130 	return next;
131 }
132