xref: /freebsd/sys/net/netisr.c (revision 4b2eaea43fec8e8792be611dea204071a10b655a)
1 /*
2  * Copyright (c) 1997, Stefan Esser <se@freebsd.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice unmodified, this list of conditions, and the following
10  *    disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  *
26  * $FreeBSD$
27  */
28 
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/bus.h>
32 #include <sys/proc.h>
33 #include <sys/interrupt.h>
34 #include <sys/kernel.h>
35 
36 #include <net/netisr.h>
37 
38 static void swi_net(void *);
39 
40 void *net_ih;
41 volatile unsigned int netisr;
42 void (*netisrs[32])(void);
43 
44 void
45 legacy_setsoftnet(void)
46 {
47 	swi_sched(net_ih, 0);
48 }
49 
50 int
51 register_netisr(int num, netisr_t *handler)
52 {
53 
54 	if (num < 0 || num >= (sizeof(netisrs)/sizeof(*netisrs)) ) {
55 		printf("register_netisr: bad isr number: %d\n", num);
56 		return (EINVAL);
57 	}
58 	netisrs[num] = handler;
59 	return (0);
60 }
61 
62 int
63 unregister_netisr(int num)
64 {
65 
66 	if (num < 0 || num >= (sizeof(netisrs)/sizeof(*netisrs)) ) {
67 		printf("unregister_netisr: bad isr number: %d\n", num);
68 		return (EINVAL);
69 	}
70 	netisrs[num] = NULL;
71 	return (0);
72 }
73 
74 #ifdef DEVICE_POLLING
75 void netisr_pollmore(void);
76 #endif
77 
78 static void
79 swi_net(void *dummy)
80 {
81 	u_int bits;
82 	int i;
83 
84 #ifdef DEVICE_POLLING
85     for (;;) {
86 	int pollmore;
87 #endif
88 	bits = atomic_readandclear_int(&netisr);
89 #ifdef DEVICE_POLLING
90 	if (bits == 0)
91 		return;
92 	pollmore = bits & (1 << NETISR_POLL);
93 #endif
94 	while ((i = ffs(bits)) != 0) {
95 		i--;
96 		if (netisrs[i] != NULL)
97 			netisrs[i]();
98 		else
99 			printf("swi_net: unregistered isr number: %d.\n", i);
100 		bits &= ~(1 << i);
101 	}
102 #ifdef DEVICE_POLLING
103 	if (pollmore)
104 		netisr_pollmore();
105     }
106 #endif
107 }
108 
109 static void
110 start_netisr(void *dummy)
111 {
112 
113 	if (swi_add(NULL, "net", swi_net, NULL, SWI_NET, 0, &net_ih))
114 		panic("start_netisr");
115 }
116 SYSINIT(start_netisr, SI_SUB_SOFTINTR, SI_ORDER_FIRST, start_netisr, NULL)
117