intrctl_io.c revision 1.3
1/* $NetBSD: intrctl_io.c,v 1.3 2016/08/05 06:58:55 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.3 2016/08/05 06:58:55 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 */ 45void * 46intrctl_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 } 78error: 79 if (buf != NULL) 80 free(buf); 81 return NULL; 82} 83 84void 85intrctl_io_free(void *handle) 86{ 87 88 free(handle); 89} 90 91int 92intrctl_io_ncpus(void *handle) 93{ 94 struct intrio_list *list = handle; 95 96 return list->il_ncpus; 97} 98 99int 100intrctl_io_nintrs(void *handle) 101{ 102 struct intrio_list *list = handle; 103 104 return list->il_nintrs; 105} 106 107struct intrio_list_line * 108intrctl_io_firstline(void *handle) 109{ 110 struct intrio_list *list = handle; 111 112 return (struct intrio_list_line *)((char *)list + list->il_lineoffset); 113} 114 115struct intrio_list_line * 116intrctl_io_nextline(void *handle, struct intrio_list_line *cur) 117{ 118 struct intrio_list *list; 119 struct intrio_list_line *next; 120 size_t line_size; 121 char *buf_end; 122 123 list = handle; 124 buf_end = (char *)list + list->il_bufsize; 125 126 line_size = list->il_linesize; 127 next = (struct intrio_list_line *)((char *)cur + line_size); 128 if ((char *)next >= buf_end) 129 return NULL; 130 131 return next; 132} 133