xref: /dflybsd-src/sys/net/if_poll.c (revision bff82488b6f45c2f067e4c552e649b1d3e07cd7c)
1b3a7093fSSepherosa Ziehau /*-
2b3a7093fSSepherosa Ziehau  * Copyright (c) 2001-2002 Luigi Rizzo
3b3a7093fSSepherosa Ziehau  *
4b3a7093fSSepherosa Ziehau  * Supported by: the Xorp Project (www.xorp.org)
5b3a7093fSSepherosa Ziehau  *
6b3a7093fSSepherosa Ziehau  * Redistribution and use in source and binary forms, with or without
7b3a7093fSSepherosa Ziehau  * modification, are permitted provided that the following conditions
8b3a7093fSSepherosa Ziehau  * are met:
9b3a7093fSSepherosa Ziehau  * 1. Redistributions of source code must retain the above copyright
10b3a7093fSSepherosa Ziehau  *    notice, this list of conditions and the following disclaimer.
11b3a7093fSSepherosa Ziehau  * 2. Redistributions in binary form must reproduce the above copyright
12b3a7093fSSepherosa Ziehau  *    notice, this list of conditions and the following disclaimer in the
13b3a7093fSSepherosa Ziehau  *    documentation and/or other materials provided with the distribution.
14b3a7093fSSepherosa Ziehau  *
15b3a7093fSSepherosa Ziehau  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
16b3a7093fSSepherosa Ziehau  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17b3a7093fSSepherosa Ziehau  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18b3a7093fSSepherosa Ziehau  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
19b3a7093fSSepherosa Ziehau  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20b3a7093fSSepherosa Ziehau  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21b3a7093fSSepherosa Ziehau  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22b3a7093fSSepherosa Ziehau  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23b3a7093fSSepherosa Ziehau  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24b3a7093fSSepherosa Ziehau  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25b3a7093fSSepherosa Ziehau  * SUCH DAMAGE.
26b3a7093fSSepherosa Ziehau  *
27b3a7093fSSepherosa Ziehau  * $FreeBSD: src/sys/kern/kern_poll.c,v 1.2.2.4 2002/06/27 23:26:33 luigi Exp $
28b3a7093fSSepherosa Ziehau  */
29b3a7093fSSepherosa Ziehau 
30b3a7093fSSepherosa Ziehau #include "opt_ifpoll.h"
31b3a7093fSSepherosa Ziehau 
32b3a7093fSSepherosa Ziehau #include <sys/param.h>
33b3a7093fSSepherosa Ziehau #include <sys/kernel.h>
34b3a7093fSSepherosa Ziehau #include <sys/ktr.h>
35b3a7093fSSepherosa Ziehau #include <sys/malloc.h>
36b3a7093fSSepherosa Ziehau #include <sys/serialize.h>
37b3a7093fSSepherosa Ziehau #include <sys/socket.h>
38b3a7093fSSepherosa Ziehau #include <sys/sysctl.h>
3984a3d873SSepherosa Ziehau #include <sys/microtime_pcpu.h>
40b3a7093fSSepherosa Ziehau 
41b3a7093fSSepherosa Ziehau #include <sys/thread2.h>
42b3a7093fSSepherosa Ziehau #include <sys/msgport2.h>
43b3a7093fSSepherosa Ziehau 
44b3a7093fSSepherosa Ziehau #include <net/if.h>
45*bff82488SAaron LI #include <net/if_var.h>
46b3a7093fSSepherosa Ziehau #include <net/if_poll.h>
47b3a7093fSSepherosa Ziehau #include <net/netmsg2.h>
485337421cSSepherosa Ziehau #include <net/netisr2.h>
49b3a7093fSSepherosa Ziehau 
50b3a7093fSSepherosa Ziehau /*
51b3a7093fSSepherosa Ziehau  * Polling support for network device drivers.
52b3a7093fSSepherosa Ziehau  *
53b3a7093fSSepherosa Ziehau  * Drivers which support this feature try to register one status polling
54b3a7093fSSepherosa Ziehau  * handler and several TX/RX polling handlers with the polling code.
55f994de37SSepherosa Ziehau  * If interface's if_npoll is called with non-NULL second argument, then
56b3a7093fSSepherosa Ziehau  * a register operation is requested, else a deregister operation is
57b3a7093fSSepherosa Ziehau  * requested.  If the requested operation is "register", driver should
58b3a7093fSSepherosa Ziehau  * setup the ifpoll_info passed in accoding its own needs:
59b3a7093fSSepherosa Ziehau  *   ifpoll_info.ifpi_status.status_func == NULL
60b3a7093fSSepherosa Ziehau  *     No status polling handler will be installed on CPU(0)
61b3a7093fSSepherosa Ziehau  *   ifpoll_info.ifpi_rx[n].poll_func == NULL
62b3a7093fSSepherosa Ziehau  *     No RX polling handler will be installed on CPU(n)
63b3a7093fSSepherosa Ziehau  *   ifpoll_info.ifpi_tx[n].poll_func == NULL
64b3a7093fSSepherosa Ziehau  *     No TX polling handler will be installed on CPU(n)
65b3a7093fSSepherosa Ziehau  *
6685b010b0SSepherosa Ziehau  * Serializer field of ifpoll_info.ifpi_status and ifpoll_info.ifpi_tx[n]
6785b010b0SSepherosa Ziehau  * must _not_ be NULL.  The serializer will be held before the status_func
6885b010b0SSepherosa Ziehau  * and poll_func being called.  Serializer field of ifpoll_info.ifpi_rx[n]
6985b010b0SSepherosa Ziehau  * can be NULL, but the interface's if_flags must have IFF_IDIRECT set,
7085b010b0SSepherosa Ziehau  * which indicates that the network processing of the input packets is
7185b010b0SSepherosa Ziehau  * running directly instead of being redispatched.
7285b010b0SSepherosa Ziehau  *
73968f17f7SSepherosa Ziehau  * RX is polled at the specified polling frequency (net.ifpoll.X.pollhz).
7426673976SSepherosa Ziehau  * TX and status polling could be done at lower frequency than RX frequency
7526673976SSepherosa Ziehau  * (net.ifpoll.0.status_frac and net.ifpoll.X.tx_frac).  To avoid systimer
7626673976SSepherosa Ziehau  * staggering at high frequency, RX systimer gives TX and status polling a
7726673976SSepherosa Ziehau  * piggyback (XXX).
78968f17f7SSepherosa Ziehau  *
79b3a7093fSSepherosa Ziehau  * All of the registered polling handlers are called only if the interface
8085b010b0SSepherosa Ziehau  * is marked as IFF_UP, IFF_RUNNING and IFF_NPOLLING.  However, the
8185b010b0SSepherosa Ziehau  * interface's register and deregister function (ifnet.if_npoll) will be
8285b010b0SSepherosa Ziehau  * called even if interface is not marked with IFF_RUNNING or IFF_UP.
83b3a7093fSSepherosa Ziehau  *
84b3a7093fSSepherosa Ziehau  * If registration is successful, the driver must disable interrupts,
85b3a7093fSSepherosa Ziehau  * and further I/O is performed through the TX/RX polling handler, which
86b3a7093fSSepherosa Ziehau  * are invoked (at least once per clock tick) with 3 arguments: the "arg"
87b3a7093fSSepherosa Ziehau  * passed at register time, a struct ifnet pointer, and a "count" limit.
88b3a7093fSSepherosa Ziehau  * The registered serializer will be held before calling the related
89b3a7093fSSepherosa Ziehau  * polling handler.
90b3a7093fSSepherosa Ziehau  *
91b3a7093fSSepherosa Ziehau  * The count limit specifies how much work the handler can do during the
92b3a7093fSSepherosa Ziehau  * call -- typically this is the number of packets to be received, or
93b3a7093fSSepherosa Ziehau  * transmitted, etc. (drivers are free to interpret this number, as long
94b3a7093fSSepherosa Ziehau  * as the max time spent in the function grows roughly linearly with the
95b3a7093fSSepherosa Ziehau  * count).
96b3a7093fSSepherosa Ziehau  *
97b3a7093fSSepherosa Ziehau  * A second variable controls the sharing of CPU between polling/kernel
98b3a7093fSSepherosa Ziehau  * network processing, and other activities (typically userlevel tasks):
9963e6ef20SSepherosa Ziehau  * net.ifpoll.X.{rx,tx}.user_frac (between 0 and 100, default 50) sets the
100b3a7093fSSepherosa Ziehau  * share of CPU allocated to user tasks.  CPU is allocated proportionally
101b3a7093fSSepherosa Ziehau  * to the shares, by dynamically adjusting the "count" (poll_burst).
102b3a7093fSSepherosa Ziehau  *
103b3a7093fSSepherosa Ziehau  * Other parameters can should be left to their default values.
104b3a7093fSSepherosa Ziehau  * The following constraints hold
105b3a7093fSSepherosa Ziehau  *
106b3a7093fSSepherosa Ziehau  *	1 <= poll_burst <= poll_burst_max
107b3a7093fSSepherosa Ziehau  *	1 <= poll_each_burst <= poll_burst_max
108b3a7093fSSepherosa Ziehau  *	MIN_POLL_BURST_MAX <= poll_burst_max <= MAX_POLL_BURST_MAX
109b3a7093fSSepherosa Ziehau  */
110b3a7093fSSepherosa Ziehau 
111b3a7093fSSepherosa Ziehau #define IFPOLL_LIST_LEN		128
112b3a7093fSSepherosa Ziehau #define IFPOLL_FREQ_MAX		30000
113b3a7093fSSepherosa Ziehau 
114b3a7093fSSepherosa Ziehau #define MIN_IOPOLL_BURST_MAX	10
1155c721a05SSepherosa Ziehau #define MAX_IOPOLL_BURST_MAX	5000
116fa7903f3SSepherosa Ziehau #define IOPOLL_BURST_MAX	250	/* good for 1000Mbit net and HZ=6000 */
117b3a7093fSSepherosa Ziehau 
118107282b9SSepherosa Ziehau #define IOPOLL_EACH_BURST	50
119ac65d38fSSepherosa Ziehau #define IOPOLL_USER_FRAC	50
120b3a7093fSSepherosa Ziehau 
121fa7903f3SSepherosa Ziehau #define IFPOLL_FREQ_DEFAULT	6000
122b3a7093fSSepherosa Ziehau 
1230c7fdccdSSepherosa Ziehau #define IFPOLL_TXFRAC_DEFAULT	1	/* 1/1 of the pollhz */
124fa7903f3SSepherosa Ziehau #define IFPOLL_STFRAC_DEFAULT	120	/* 1/120 of the pollhz */
125b3a7093fSSepherosa Ziehau 
126b3a7093fSSepherosa Ziehau #define IFPOLL_RX		0x1
127b3a7093fSSepherosa Ziehau #define IFPOLL_TX		0x2
128b3a7093fSSepherosa Ziehau 
129b3a7093fSSepherosa Ziehau struct iopoll_rec {
130b3a7093fSSepherosa Ziehau 	struct lwkt_serialize	*serializer;
131b3a7093fSSepherosa Ziehau 	struct ifnet		*ifp;
132b3a7093fSSepherosa Ziehau 	void			*arg;
133b3a7093fSSepherosa Ziehau 	ifpoll_iofn_t		poll_func;
134b3a7093fSSepherosa Ziehau };
135b3a7093fSSepherosa Ziehau 
136b3a7093fSSepherosa Ziehau struct iopoll_ctx {
13784a3d873SSepherosa Ziehau 	union microtime_pcpu	prev_t;
138e3f2143dSSepherosa Ziehau 	u_long			short_ticks;		/* statistics */
139e3f2143dSSepherosa Ziehau 	u_long			lost_polls;		/* statistics */
140e3f2143dSSepherosa Ziehau 	u_long			suspect;		/* statistics */
141e3f2143dSSepherosa Ziehau 	u_long			stalled;		/* statistics */
142b3a7093fSSepherosa Ziehau 	uint32_t		pending_polls;		/* state */
143b3a7093fSSepherosa Ziehau 
144002c1265SMatthew Dillon 	struct netmsg_base	poll_netmsg;
14556b64290SSepherosa Ziehau 	struct netmsg_base	poll_more_netmsg;
146b3a7093fSSepherosa Ziehau 
147b3a7093fSSepherosa Ziehau 	int			poll_cpuid;
148968f17f7SSepherosa Ziehau 	int			pollhz;
149b3a7093fSSepherosa Ziehau 	uint32_t		phase;			/* state */
150b3a7093fSSepherosa Ziehau 	int			residual_burst;		/* state */
151b3a7093fSSepherosa Ziehau 	uint32_t		poll_each_burst;	/* tunable */
15284a3d873SSepherosa Ziehau 	union microtime_pcpu	poll_start_t;		/* state */
153b3a7093fSSepherosa Ziehau 
154b3a7093fSSepherosa Ziehau 	uint32_t		poll_burst;		/* state */
155b3a7093fSSepherosa Ziehau 	uint32_t		poll_burst_max;		/* tunable */
156b3a7093fSSepherosa Ziehau 	uint32_t		user_frac;		/* tunable */
157112d5942SSepherosa Ziehau 	uint32_t		kern_frac;		/* state */
158b3a7093fSSepherosa Ziehau 
15956b64290SSepherosa Ziehau 	uint32_t		poll_handlers; /* next free entry in pr[]. */
16056b64290SSepherosa Ziehau 	struct iopoll_rec	pr[IFPOLL_LIST_LEN];
16156b64290SSepherosa Ziehau 
162b3a7093fSSepherosa Ziehau 	struct sysctl_ctx_list	poll_sysctl_ctx;
163b3a7093fSSepherosa Ziehau 	struct sysctl_oid	*poll_sysctl_tree;
16483d8ec1eSSepherosa Ziehau };
165b3a7093fSSepherosa Ziehau 
166968f17f7SSepherosa Ziehau struct poll_comm {
167968f17f7SSepherosa Ziehau 	struct systimer		pollclock;
168968f17f7SSepherosa Ziehau 	int			poll_cpuid;
169968f17f7SSepherosa Ziehau 
170968f17f7SSepherosa Ziehau 	int			stfrac_count;		/* state */
171968f17f7SSepherosa Ziehau 	int			poll_stfrac;		/* tunable */
172968f17f7SSepherosa Ziehau 
173968f17f7SSepherosa Ziehau 	int			txfrac_count;		/* state */
174968f17f7SSepherosa Ziehau 	int			poll_txfrac;		/* tunable */
175968f17f7SSepherosa Ziehau 
176968f17f7SSepherosa Ziehau 	int			pollhz;			/* tunable */
177968f17f7SSepherosa Ziehau 
178a7519a15SSepherosa Ziehau 	struct sysctl_ctx_list	sysctl_ctx;
179a7519a15SSepherosa Ziehau 	struct sysctl_oid	*sysctl_tree;
18083d8ec1eSSepherosa Ziehau };
181a7519a15SSepherosa Ziehau 
182b3a7093fSSepherosa Ziehau struct stpoll_rec {
183b3a7093fSSepherosa Ziehau 	struct lwkt_serialize	*serializer;
184b3a7093fSSepherosa Ziehau 	struct ifnet		*ifp;
185b3a7093fSSepherosa Ziehau 	ifpoll_stfn_t		status_func;
186b3a7093fSSepherosa Ziehau };
187b3a7093fSSepherosa Ziehau 
188b3a7093fSSepherosa Ziehau struct stpoll_ctx {
189002c1265SMatthew Dillon 	struct netmsg_base	poll_netmsg;
190b3a7093fSSepherosa Ziehau 
191b3a7093fSSepherosa Ziehau 	uint32_t		poll_handlers; /* next free entry in pr[]. */
192b3a7093fSSepherosa Ziehau 	struct stpoll_rec	pr[IFPOLL_LIST_LEN];
193b3a7093fSSepherosa Ziehau 
194b3a7093fSSepherosa Ziehau 	struct sysctl_ctx_list	poll_sysctl_ctx;
195b3a7093fSSepherosa Ziehau 	struct sysctl_oid	*poll_sysctl_tree;
1967b33812fSSepherosa Ziehau } __cachealign;
197b3a7093fSSepherosa Ziehau 
198b3a7093fSSepherosa Ziehau struct iopoll_sysctl_netmsg {
199002c1265SMatthew Dillon 	struct netmsg_base	base;
200b3a7093fSSepherosa Ziehau 	struct iopoll_ctx	*ctx;
201b3a7093fSSepherosa Ziehau };
202b3a7093fSSepherosa Ziehau 
20349f7483bSSepherosa Ziehau static void	ifpoll_init_pcpu(int);
204002c1265SMatthew Dillon static void	ifpoll_register_handler(netmsg_t);
205002c1265SMatthew Dillon static void	ifpoll_deregister_handler(netmsg_t);
206b3a7093fSSepherosa Ziehau 
207968f17f7SSepherosa Ziehau /*
208968f17f7SSepherosa Ziehau  * Status polling
209968f17f7SSepherosa Ziehau  */
210968f17f7SSepherosa Ziehau static void	stpoll_init(void);
211002c1265SMatthew Dillon static void	stpoll_handler(netmsg_t);
212968f17f7SSepherosa Ziehau static void	stpoll_clock(struct stpoll_ctx *);
213968f17f7SSepherosa Ziehau static int	stpoll_register(struct ifnet *, const struct ifpoll_status *);
214968f17f7SSepherosa Ziehau static int	stpoll_deregister(struct ifnet *);
215b3a7093fSSepherosa Ziehau 
216968f17f7SSepherosa Ziehau /*
217968f17f7SSepherosa Ziehau  * RX/TX polling
218968f17f7SSepherosa Ziehau  */
219968f17f7SSepherosa Ziehau static struct iopoll_ctx *iopoll_ctx_create(int, int);
220968f17f7SSepherosa Ziehau static void	iopoll_init(int);
22198dc3fc1SSepherosa Ziehau static void	rxpoll_handler(netmsg_t);
22298dc3fc1SSepherosa Ziehau static void	txpoll_handler(netmsg_t);
22398dc3fc1SSepherosa Ziehau static void	rxpollmore_handler(netmsg_t);
22498dc3fc1SSepherosa Ziehau static void	txpollmore_handler(netmsg_t);
225968f17f7SSepherosa Ziehau static void	iopoll_clock(struct iopoll_ctx *);
226968f17f7SSepherosa Ziehau static int	iopoll_register(struct ifnet *, struct iopoll_ctx *,
227968f17f7SSepherosa Ziehau 		    const struct ifpoll_io *);
228968f17f7SSepherosa Ziehau static int	iopoll_deregister(struct ifnet *, struct iopoll_ctx *);
229968f17f7SSepherosa Ziehau 
230968f17f7SSepherosa Ziehau static void	iopoll_add_sysctl(struct sysctl_ctx_list *,
23198dc3fc1SSepherosa Ziehau 		    struct sysctl_oid_list *, struct iopoll_ctx *, int);
232002c1265SMatthew Dillon static void	sysctl_burstmax_handler(netmsg_t);
233968f17f7SSepherosa Ziehau static int	sysctl_burstmax(SYSCTL_HANDLER_ARGS);
234002c1265SMatthew Dillon static void	sysctl_eachburst_handler(netmsg_t);
235968f17f7SSepherosa Ziehau static int	sysctl_eachburst(SYSCTL_HANDLER_ARGS);
236968f17f7SSepherosa Ziehau 
237968f17f7SSepherosa Ziehau /*
238968f17f7SSepherosa Ziehau  * Common functions
239968f17f7SSepherosa Ziehau  */
240968f17f7SSepherosa Ziehau static void	poll_comm_init(int);
241968f17f7SSepherosa Ziehau static void	poll_comm_start(int);
242968f17f7SSepherosa Ziehau static void	poll_comm_adjust_pollhz(struct poll_comm *);
24396d52ac8SSepherosa Ziehau static void	poll_comm_systimer0(systimer_t, int, struct intrframe *);
24496d52ac8SSepherosa Ziehau static void	poll_comm_systimer(systimer_t, int, struct intrframe *);
245002c1265SMatthew Dillon static void	sysctl_pollhz_handler(netmsg_t);
246002c1265SMatthew Dillon static void	sysctl_stfrac_handler(netmsg_t);
247002c1265SMatthew Dillon static void	sysctl_txfrac_handler(netmsg_t);
248968f17f7SSepherosa Ziehau static int	sysctl_pollhz(SYSCTL_HANDLER_ARGS);
24926673976SSepherosa Ziehau static int	sysctl_stfrac(SYSCTL_HANDLER_ARGS);
25026673976SSepherosa Ziehau static int	sysctl_txfrac(SYSCTL_HANDLER_ARGS);
251b5de76b1SSepherosa Ziehau static int	sysctl_compat_npoll_stfrac(SYSCTL_HANDLER_ARGS);
252b5de76b1SSepherosa Ziehau static int	sysctl_compat_npoll_cpuid(SYSCTL_HANDLER_ARGS);
253b3a7093fSSepherosa Ziehau 
254b3a7093fSSepherosa Ziehau static struct stpoll_ctx	stpoll_context;
2559826be42SSepherosa Ziehau static struct poll_comm		*poll_common[MAXCPU];
2569826be42SSepherosa Ziehau static struct iopoll_ctx	*rxpoll_context[MAXCPU];
2579826be42SSepherosa Ziehau static struct iopoll_ctx	*txpoll_context[MAXCPU];
258b3a7093fSSepherosa Ziehau 
259b3a7093fSSepherosa Ziehau SYSCTL_NODE(_net, OID_AUTO, ifpoll, CTLFLAG_RW, 0,
260b3a7093fSSepherosa Ziehau 	    "Network device polling parameters");
261b3a7093fSSepherosa Ziehau 
262b3a7093fSSepherosa Ziehau static int	iopoll_burst_max = IOPOLL_BURST_MAX;
263b3a7093fSSepherosa Ziehau static int	iopoll_each_burst = IOPOLL_EACH_BURST;
264ac65d38fSSepherosa Ziehau static int	iopoll_user_frac = IOPOLL_USER_FRAC;
265b3a7093fSSepherosa Ziehau 
266b3a7093fSSepherosa Ziehau static int	ifpoll_pollhz = IFPOLL_FREQ_DEFAULT;
267b3a7093fSSepherosa Ziehau static int	ifpoll_stfrac = IFPOLL_STFRAC_DEFAULT;
268b3a7093fSSepherosa Ziehau static int	ifpoll_txfrac = IFPOLL_TXFRAC_DEFAULT;
269b3a7093fSSepherosa Ziehau 
270968f17f7SSepherosa Ziehau TUNABLE_INT("net.ifpoll.burst_max", &iopoll_burst_max);
271968f17f7SSepherosa Ziehau TUNABLE_INT("net.ifpoll.each_burst", &iopoll_each_burst);
272ac65d38fSSepherosa Ziehau TUNABLE_INT("net.ifpoll.user_frac", &iopoll_user_frac);
273b3a7093fSSepherosa Ziehau TUNABLE_INT("net.ifpoll.pollhz", &ifpoll_pollhz);
274b3a7093fSSepherosa Ziehau TUNABLE_INT("net.ifpoll.status_frac", &ifpoll_stfrac);
275b3a7093fSSepherosa Ziehau TUNABLE_INT("net.ifpoll.tx_frac", &ifpoll_txfrac);
276b3a7093fSSepherosa Ziehau 
27795ff9ed0SSepherosa Ziehau #if !defined(KTR_IF_POLL)
27895ff9ed0SSepherosa Ziehau #define  KTR_IF_POLL		KTR_ALL
27995ff9ed0SSepherosa Ziehau #endif
28095ff9ed0SSepherosa Ziehau KTR_INFO_MASTER(if_poll);
28195ff9ed0SSepherosa Ziehau KTR_INFO(KTR_IF_POLL, if_poll, rx_start, 0, "rx start");
28295ff9ed0SSepherosa Ziehau KTR_INFO(KTR_IF_POLL, if_poll, rx_end, 1, "rx end");
28395ff9ed0SSepherosa Ziehau KTR_INFO(KTR_IF_POLL, if_poll, tx_start, 2, "tx start");
28495ff9ed0SSepherosa Ziehau KTR_INFO(KTR_IF_POLL, if_poll, tx_end, 3, "tx end");
28595ff9ed0SSepherosa Ziehau KTR_INFO(KTR_IF_POLL, if_poll, rx_mstart, 4, "rx more start");
28695ff9ed0SSepherosa Ziehau KTR_INFO(KTR_IF_POLL, if_poll, rx_mend, 5, "rx more end");
28795ff9ed0SSepherosa Ziehau KTR_INFO(KTR_IF_POLL, if_poll, tx_mstart, 6, "tx more start");
28895ff9ed0SSepherosa Ziehau KTR_INFO(KTR_IF_POLL, if_poll, tx_mend, 7, "tx more end");
28995ff9ed0SSepherosa Ziehau KTR_INFO(KTR_IF_POLL, if_poll, ioclock_start, 8, "ioclock start");
29095ff9ed0SSepherosa Ziehau KTR_INFO(KTR_IF_POLL, if_poll, ioclock_end, 9, "ioclock end");
29195ff9ed0SSepherosa Ziehau #define logpoll(name)	KTR_LOG(if_poll_ ## name)
29295ff9ed0SSepherosa Ziehau 
293d61a4c96SSepherosa Ziehau #define IFPOLL_FREQ_ADJ(comm)	(((comm)->poll_cpuid * 3) % 50)
294d61a4c96SSepherosa Ziehau 
295d61a4c96SSepherosa Ziehau static __inline int
poll_comm_pollhz_div(const struct poll_comm * comm,int pollhz)296d61a4c96SSepherosa Ziehau poll_comm_pollhz_div(const struct poll_comm *comm, int pollhz)
297d61a4c96SSepherosa Ziehau {
298d61a4c96SSepherosa Ziehau 	return pollhz + IFPOLL_FREQ_ADJ(comm);
299d61a4c96SSepherosa Ziehau }
300d61a4c96SSepherosa Ziehau 
301d61a4c96SSepherosa Ziehau static __inline int
poll_comm_pollhz_conv(const struct poll_comm * comm,int pollhz)302d61a4c96SSepherosa Ziehau poll_comm_pollhz_conv(const struct poll_comm *comm, int pollhz)
303d61a4c96SSepherosa Ziehau {
304d61a4c96SSepherosa Ziehau 	return pollhz - IFPOLL_FREQ_ADJ(comm);
305d61a4c96SSepherosa Ziehau }
306d61a4c96SSepherosa Ziehau 
307a7254d9cSSepherosa Ziehau static __inline void
ifpoll_sendmsg_oncpu(netmsg_t msg)308002c1265SMatthew Dillon ifpoll_sendmsg_oncpu(netmsg_t msg)
309b3a7093fSSepherosa Ziehau {
310002c1265SMatthew Dillon 	if (msg->lmsg.ms_flags & MSGF_DONE)
311e3bcf7f5SSepherosa Ziehau 		netisr_sendmsg_oncpu(&msg->base);
312b3a7093fSSepherosa Ziehau }
313b3a7093fSSepherosa Ziehau 
314a7254d9cSSepherosa Ziehau static __inline void
sched_stpoll(struct stpoll_ctx * st_ctx)315b3a7093fSSepherosa Ziehau sched_stpoll(struct stpoll_ctx *st_ctx)
316b3a7093fSSepherosa Ziehau {
317002c1265SMatthew Dillon 	ifpoll_sendmsg_oncpu((netmsg_t)&st_ctx->poll_netmsg);
318b3a7093fSSepherosa Ziehau }
319b3a7093fSSepherosa Ziehau 
320a7254d9cSSepherosa Ziehau static __inline void
sched_iopoll(struct iopoll_ctx * io_ctx)321b3a7093fSSepherosa Ziehau sched_iopoll(struct iopoll_ctx *io_ctx)
322b3a7093fSSepherosa Ziehau {
323002c1265SMatthew Dillon 	ifpoll_sendmsg_oncpu((netmsg_t)&io_ctx->poll_netmsg);
324b3a7093fSSepherosa Ziehau }
325b3a7093fSSepherosa Ziehau 
326a7254d9cSSepherosa Ziehau static __inline void
sched_iopollmore(struct iopoll_ctx * io_ctx,boolean_t direct)3278d0afa86SSepherosa Ziehau sched_iopollmore(struct iopoll_ctx *io_ctx, boolean_t direct)
328b3a7093fSSepherosa Ziehau {
3298d0afa86SSepherosa Ziehau 
3308d0afa86SSepherosa Ziehau 	if (!direct) {
331002c1265SMatthew Dillon 		ifpoll_sendmsg_oncpu((netmsg_t)&io_ctx->poll_more_netmsg);
3328d0afa86SSepherosa Ziehau 	} else {
3338d0afa86SSepherosa Ziehau 		struct netmsg_base *nmsg = &io_ctx->poll_more_netmsg;
3348d0afa86SSepherosa Ziehau 
3358d0afa86SSepherosa Ziehau 		nmsg->lmsg.ms_flags &= ~(MSGF_REPLY | MSGF_DONE);
3368d0afa86SSepherosa Ziehau 		nmsg->lmsg.ms_flags |= MSGF_SYNC;
3378d0afa86SSepherosa Ziehau 		nmsg->nm_dispatch((netmsg_t)nmsg);
3388d0afa86SSepherosa Ziehau 		KKASSERT(nmsg->lmsg.ms_flags & MSGF_DONE);
3398d0afa86SSepherosa Ziehau 	}
340b3a7093fSSepherosa Ziehau }
341b3a7093fSSepherosa Ziehau 
342b3a7093fSSepherosa Ziehau /*
34349f7483bSSepherosa Ziehau  * Initialize per-cpu polling(4) context.
344b3a7093fSSepherosa Ziehau  */
34549f7483bSSepherosa Ziehau static void
ifpoll_init_pcpu(int cpuid)346b3a7093fSSepherosa Ziehau ifpoll_init_pcpu(int cpuid)
347b3a7093fSSepherosa Ziehau {
348968f17f7SSepherosa Ziehau 
349968f17f7SSepherosa Ziehau 	poll_comm_init(cpuid);
350b3a7093fSSepherosa Ziehau 
351968f17f7SSepherosa Ziehau 	if (cpuid == 0)
352b3a7093fSSepherosa Ziehau 		stpoll_init();
353b3a7093fSSepherosa Ziehau 	iopoll_init(cpuid);
354968f17f7SSepherosa Ziehau 
355968f17f7SSepherosa Ziehau 	poll_comm_start(cpuid);
356b3a7093fSSepherosa Ziehau }
357b3a7093fSSepherosa Ziehau 
35849f7483bSSepherosa Ziehau static void
ifpoll_init_handler(netmsg_t msg)35949f7483bSSepherosa Ziehau ifpoll_init_handler(netmsg_t msg)
36049f7483bSSepherosa Ziehau {
361e3bcf7f5SSepherosa Ziehau 	int cpu = mycpuid;
36249f7483bSSepherosa Ziehau 
36349f7483bSSepherosa Ziehau 	ifpoll_init_pcpu(cpu);
364e3bcf7f5SSepherosa Ziehau 	netisr_forwardmsg(&msg->base, cpu + 1);
36549f7483bSSepherosa Ziehau }
36649f7483bSSepherosa Ziehau 
36749f7483bSSepherosa Ziehau static void
ifpoll_sysinit(void * dummy __unused)36849f7483bSSepherosa Ziehau ifpoll_sysinit(void *dummy __unused)
36949f7483bSSepherosa Ziehau {
37049f7483bSSepherosa Ziehau 	struct netmsg_base msg;
37149f7483bSSepherosa Ziehau 
37249f7483bSSepherosa Ziehau 	netmsg_init(&msg, NULL, &curthread->td_msgport, 0, ifpoll_init_handler);
37332f8b1acSSepherosa Ziehau 	netisr_domsg_global(&msg);
37449f7483bSSepherosa Ziehau }
37549f7483bSSepherosa Ziehau SYSINIT(ifpoll, SI_SUB_PRE_DRIVERS, SI_ORDER_ANY, ifpoll_sysinit, NULL);
37649f7483bSSepherosa Ziehau 
377b3a7093fSSepherosa Ziehau int
ifpoll_register(struct ifnet * ifp)378b3a7093fSSepherosa Ziehau ifpoll_register(struct ifnet *ifp)
379b3a7093fSSepherosa Ziehau {
3809826be42SSepherosa Ziehau 	struct ifpoll_info *info;
381002c1265SMatthew Dillon 	struct netmsg_base nmsg;
382b3a7093fSSepherosa Ziehau 	int error;
383b3a7093fSSepherosa Ziehau 
384f994de37SSepherosa Ziehau 	if (ifp->if_npoll == NULL) {
385b3a7093fSSepherosa Ziehau 		/* Device does not support polling */
386b3a7093fSSepherosa Ziehau 		return EOPNOTSUPP;
387b3a7093fSSepherosa Ziehau 	}
388b3a7093fSSepherosa Ziehau 
3899826be42SSepherosa Ziehau 	info = kmalloc(sizeof(*info), M_TEMP, M_WAITOK | M_ZERO);
3909826be42SSepherosa Ziehau 
391b3a7093fSSepherosa Ziehau 	/*
392b3a7093fSSepherosa Ziehau 	 * Attempt to register.  Interlock with IFF_NPOLLING.
393b3a7093fSSepherosa Ziehau 	 */
394b3a7093fSSepherosa Ziehau 
395b3a7093fSSepherosa Ziehau 	ifnet_serialize_all(ifp);
396b3a7093fSSepherosa Ziehau 
397b3a7093fSSepherosa Ziehau 	if (ifp->if_flags & IFF_NPOLLING) {
398b3a7093fSSepherosa Ziehau 		/* Already polling */
399b3a7093fSSepherosa Ziehau 		ifnet_deserialize_all(ifp);
4009826be42SSepherosa Ziehau 		kfree(info, M_TEMP);
401b3a7093fSSepherosa Ziehau 		return EBUSY;
402b3a7093fSSepherosa Ziehau 	}
403b3a7093fSSepherosa Ziehau 
4049826be42SSepherosa Ziehau 	info->ifpi_ifp = ifp;
405b3a7093fSSepherosa Ziehau 
406b3a7093fSSepherosa Ziehau 	ifp->if_flags |= IFF_NPOLLING;
407f994de37SSepherosa Ziehau 	ifp->if_npoll(ifp, info);
408b3a7093fSSepherosa Ziehau 
409b3a7093fSSepherosa Ziehau 	ifnet_deserialize_all(ifp);
410b3a7093fSSepherosa Ziehau 
41148e7b118SMatthew Dillon 	netmsg_init(&nmsg, NULL, &curthread->td_msgport,
412c3c96e44SMatthew Dillon 		    0, ifpoll_register_handler);
4139826be42SSepherosa Ziehau 	nmsg.lmsg.u.ms_resultp = info;
414b3a7093fSSepherosa Ziehau 
41532f8b1acSSepherosa Ziehau 	error = netisr_domsg_global(&nmsg);
416b3a7093fSSepherosa Ziehau 	if (error) {
417b3a7093fSSepherosa Ziehau 		if (!ifpoll_deregister(ifp)) {
418b3a7093fSSepherosa Ziehau 			if_printf(ifp, "ifpoll_register: "
419b3a7093fSSepherosa Ziehau 				  "ifpoll_deregister failed!\n");
420b3a7093fSSepherosa Ziehau 		}
421b3a7093fSSepherosa Ziehau 	}
4229826be42SSepherosa Ziehau 
4239826be42SSepherosa Ziehau 	kfree(info, M_TEMP);
424b3a7093fSSepherosa Ziehau 	return error;
425b3a7093fSSepherosa Ziehau }
426b3a7093fSSepherosa Ziehau 
427b3a7093fSSepherosa Ziehau int
ifpoll_deregister(struct ifnet * ifp)428b3a7093fSSepherosa Ziehau ifpoll_deregister(struct ifnet *ifp)
429b3a7093fSSepherosa Ziehau {
430002c1265SMatthew Dillon 	struct netmsg_base nmsg;
431b3a7093fSSepherosa Ziehau 	int error;
432b3a7093fSSepherosa Ziehau 
433f994de37SSepherosa Ziehau 	if (ifp->if_npoll == NULL)
434b3a7093fSSepherosa Ziehau 		return EOPNOTSUPP;
435b3a7093fSSepherosa Ziehau 
436b3a7093fSSepherosa Ziehau 	ifnet_serialize_all(ifp);
437b3a7093fSSepherosa Ziehau 
438b3a7093fSSepherosa Ziehau 	if ((ifp->if_flags & IFF_NPOLLING) == 0) {
439b3a7093fSSepherosa Ziehau 		ifnet_deserialize_all(ifp);
440b3a7093fSSepherosa Ziehau 		return EINVAL;
441b3a7093fSSepherosa Ziehau 	}
442b3a7093fSSepherosa Ziehau 	ifp->if_flags &= ~IFF_NPOLLING;
443b3a7093fSSepherosa Ziehau 
444b3a7093fSSepherosa Ziehau 	ifnet_deserialize_all(ifp);
445b3a7093fSSepherosa Ziehau 
44648e7b118SMatthew Dillon 	netmsg_init(&nmsg, NULL, &curthread->td_msgport,
447c3c96e44SMatthew Dillon 		    0, ifpoll_deregister_handler);
448002c1265SMatthew Dillon 	nmsg.lmsg.u.ms_resultp = ifp;
449b3a7093fSSepherosa Ziehau 
45032f8b1acSSepherosa Ziehau 	error = netisr_domsg_global(&nmsg);
451b3a7093fSSepherosa Ziehau 	if (!error) {
452b3a7093fSSepherosa Ziehau 		ifnet_serialize_all(ifp);
453f994de37SSepherosa Ziehau 		ifp->if_npoll(ifp, NULL);
454b3a7093fSSepherosa Ziehau 		ifnet_deserialize_all(ifp);
455b3a7093fSSepherosa Ziehau 	}
456b3a7093fSSepherosa Ziehau 	return error;
457b3a7093fSSepherosa Ziehau }
458b3a7093fSSepherosa Ziehau 
459b3a7093fSSepherosa Ziehau static void
ifpoll_register_handler(netmsg_t nmsg)460002c1265SMatthew Dillon ifpoll_register_handler(netmsg_t nmsg)
461b3a7093fSSepherosa Ziehau {
462002c1265SMatthew Dillon 	const struct ifpoll_info *info = nmsg->lmsg.u.ms_resultp;
463e3bcf7f5SSepherosa Ziehau 	int cpuid = mycpuid;
464b3a7093fSSepherosa Ziehau 	int error;
465b3a7093fSSepherosa Ziehau 
466d1958e12SSepherosa Ziehau 	KKASSERT(cpuid < netisr_ncpus);
467ec7f7fc8SSepherosa Ziehau 	KKASSERT(&curthread->td_msgport == netisr_cpuport(cpuid));
468b3a7093fSSepherosa Ziehau 
469b3a7093fSSepherosa Ziehau 	if (cpuid == 0) {
470b3a7093fSSepherosa Ziehau 		error = stpoll_register(info->ifpi_ifp, &info->ifpi_status);
471b3a7093fSSepherosa Ziehau 		if (error)
472b3a7093fSSepherosa Ziehau 			goto failed;
473b3a7093fSSepherosa Ziehau 	}
474b3a7093fSSepherosa Ziehau 
475b3a7093fSSepherosa Ziehau 	error = iopoll_register(info->ifpi_ifp, rxpoll_context[cpuid],
476b3a7093fSSepherosa Ziehau 				&info->ifpi_rx[cpuid]);
477b3a7093fSSepherosa Ziehau 	if (error)
478b3a7093fSSepherosa Ziehau 		goto failed;
479b3a7093fSSepherosa Ziehau 
480b3a7093fSSepherosa Ziehau 	error = iopoll_register(info->ifpi_ifp, txpoll_context[cpuid],
481b3a7093fSSepherosa Ziehau 				&info->ifpi_tx[cpuid]);
482b3a7093fSSepherosa Ziehau 	if (error)
483b3a7093fSSepherosa Ziehau 		goto failed;
484b3a7093fSSepherosa Ziehau 
485968f17f7SSepherosa Ziehau 	/* Adjust polling frequency, after all registration is done */
486968f17f7SSepherosa Ziehau 	poll_comm_adjust_pollhz(poll_common[cpuid]);
487968f17f7SSepherosa Ziehau 
488e3bcf7f5SSepherosa Ziehau 	netisr_forwardmsg(&nmsg->base, cpuid + 1);
489b3a7093fSSepherosa Ziehau 	return;
490b3a7093fSSepherosa Ziehau failed:
491e3bcf7f5SSepherosa Ziehau 	netisr_replymsg(&nmsg->base, error);
492b3a7093fSSepherosa Ziehau }
493b3a7093fSSepherosa Ziehau 
494b3a7093fSSepherosa Ziehau static void
ifpoll_deregister_handler(netmsg_t nmsg)495002c1265SMatthew Dillon ifpoll_deregister_handler(netmsg_t nmsg)
496b3a7093fSSepherosa Ziehau {
497002c1265SMatthew Dillon 	struct ifnet *ifp = nmsg->lmsg.u.ms_resultp;
498e3bcf7f5SSepherosa Ziehau 	int cpuid = mycpuid;
499b3a7093fSSepherosa Ziehau 
500d1958e12SSepherosa Ziehau 	KKASSERT(cpuid < netisr_ncpus);
501ec7f7fc8SSepherosa Ziehau 	KKASSERT(&curthread->td_msgport == netisr_cpuport(cpuid));
502b3a7093fSSepherosa Ziehau 
503b3a7093fSSepherosa Ziehau 	/* Ignore errors */
504b3a7093fSSepherosa Ziehau 	if (cpuid == 0)
505b3a7093fSSepherosa Ziehau 		stpoll_deregister(ifp);
506b3a7093fSSepherosa Ziehau 	iopoll_deregister(ifp, rxpoll_context[cpuid]);
507b3a7093fSSepherosa Ziehau 	iopoll_deregister(ifp, txpoll_context[cpuid]);
508b3a7093fSSepherosa Ziehau 
509968f17f7SSepherosa Ziehau 	/* Adjust polling frequency, after all deregistration is done */
510968f17f7SSepherosa Ziehau 	poll_comm_adjust_pollhz(poll_common[cpuid]);
511968f17f7SSepherosa Ziehau 
512e3bcf7f5SSepherosa Ziehau 	netisr_forwardmsg(&nmsg->base, cpuid + 1);
513b3a7093fSSepherosa Ziehau }
514b3a7093fSSepherosa Ziehau 
515b3a7093fSSepherosa Ziehau static void
stpoll_init(void)516b3a7093fSSepherosa Ziehau stpoll_init(void)
517b3a7093fSSepherosa Ziehau {
518b3a7093fSSepherosa Ziehau 	struct stpoll_ctx *st_ctx = &stpoll_context;
519968f17f7SSepherosa Ziehau 	const struct poll_comm *comm = poll_common[0];
520b3a7093fSSepherosa Ziehau 
521b3a7093fSSepherosa Ziehau 	sysctl_ctx_init(&st_ctx->poll_sysctl_ctx);
522b3a7093fSSepherosa Ziehau 	st_ctx->poll_sysctl_tree = SYSCTL_ADD_NODE(&st_ctx->poll_sysctl_ctx,
523766f5d44SSepherosa Ziehau 				   SYSCTL_CHILDREN(comm->sysctl_tree),
524b3a7093fSSepherosa Ziehau 				   OID_AUTO, "status", CTLFLAG_RD, 0, "");
525b3a7093fSSepherosa Ziehau 
526b3a7093fSSepherosa Ziehau 	SYSCTL_ADD_UINT(&st_ctx->poll_sysctl_ctx,
527b3a7093fSSepherosa Ziehau 			SYSCTL_CHILDREN(st_ctx->poll_sysctl_tree),
528b3a7093fSSepherosa Ziehau 			OID_AUTO, "handlers", CTLFLAG_RD,
529b3a7093fSSepherosa Ziehau 			&st_ctx->poll_handlers, 0,
530b3a7093fSSepherosa Ziehau 			"Number of registered status poll handlers");
531b3a7093fSSepherosa Ziehau 
53248e7b118SMatthew Dillon 	netmsg_init(&st_ctx->poll_netmsg, NULL, &netisr_adone_rport,
533c3c96e44SMatthew Dillon 		    0, stpoll_handler);
534b3a7093fSSepherosa Ziehau }
535b3a7093fSSepherosa Ziehau 
536b3a7093fSSepherosa Ziehau /*
537b3a7093fSSepherosa Ziehau  * stpoll_handler is scheduled by sched_stpoll when appropriate, typically
538b3a7093fSSepherosa Ziehau  * once per polling systimer tick.
539b3a7093fSSepherosa Ziehau  */
540b3a7093fSSepherosa Ziehau static void
stpoll_handler(netmsg_t msg)541002c1265SMatthew Dillon stpoll_handler(netmsg_t msg)
542b3a7093fSSepherosa Ziehau {
543b3a7093fSSepherosa Ziehau 	struct stpoll_ctx *st_ctx = &stpoll_context;
544ec7f6da8SSepherosa Ziehau 	struct thread *td = curthread;
545968f17f7SSepherosa Ziehau 	int i;
546b3a7093fSSepherosa Ziehau 
5475204e13cSSepherosa Ziehau 	ASSERT_NETISR0;
548ec7f6da8SSepherosa Ziehau 
549ec7f6da8SSepherosa Ziehau 	crit_enter_quick(td);
550b3a7093fSSepherosa Ziehau 
551b3a7093fSSepherosa Ziehau 	/* Reply ASAP */
552e3bcf7f5SSepherosa Ziehau 	netisr_replymsg(&msg->base, 0);
553b3a7093fSSepherosa Ziehau 
554ec7f6da8SSepherosa Ziehau 	if (st_ctx->poll_handlers == 0) {
555ec7f6da8SSepherosa Ziehau 		crit_exit_quick(td);
556b3a7093fSSepherosa Ziehau 		return;
557ec7f6da8SSepherosa Ziehau 	}
558b3a7093fSSepherosa Ziehau 
559b3a7093fSSepherosa Ziehau 	for (i = 0; i < st_ctx->poll_handlers; ++i) {
560b3a7093fSSepherosa Ziehau 		const struct stpoll_rec *rec = &st_ctx->pr[i];
561b3a7093fSSepherosa Ziehau 		struct ifnet *ifp = rec->ifp;
562b3a7093fSSepherosa Ziehau 
563b3a7093fSSepherosa Ziehau 		if (!lwkt_serialize_try(rec->serializer))
564b3a7093fSSepherosa Ziehau 			continue;
565b3a7093fSSepherosa Ziehau 
566b3a7093fSSepherosa Ziehau 		if ((ifp->if_flags & (IFF_RUNNING | IFF_NPOLLING)) ==
567ec7f6da8SSepherosa Ziehau 		    (IFF_RUNNING | IFF_NPOLLING))
5682f00683bSSepherosa Ziehau 			rec->status_func(ifp);
569b3a7093fSSepherosa Ziehau 
570b3a7093fSSepherosa Ziehau 		lwkt_serialize_exit(rec->serializer);
571b3a7093fSSepherosa Ziehau 	}
572ec7f6da8SSepherosa Ziehau 
573ec7f6da8SSepherosa Ziehau 	crit_exit_quick(td);
574b3a7093fSSepherosa Ziehau }
575b3a7093fSSepherosa Ziehau 
576b3a7093fSSepherosa Ziehau /*
577b3a7093fSSepherosa Ziehau  * Hook from status poll systimer.  Tries to schedule an status poll.
57842bb6c2eSSepherosa Ziehau  * NOTE: Caller should hold critical section.
579b3a7093fSSepherosa Ziehau  */
580b3a7093fSSepherosa Ziehau static void
stpoll_clock(struct stpoll_ctx * st_ctx)581b3a7093fSSepherosa Ziehau stpoll_clock(struct stpoll_ctx *st_ctx)
582b3a7093fSSepherosa Ziehau {
58342bb6c2eSSepherosa Ziehau 	KKASSERT(mycpuid == 0);
584b3a7093fSSepherosa Ziehau 
585b3a7093fSSepherosa Ziehau 	if (st_ctx->poll_handlers == 0)
586b3a7093fSSepherosa Ziehau 		return;
587b3a7093fSSepherosa Ziehau 	sched_stpoll(st_ctx);
588b3a7093fSSepherosa Ziehau }
589b3a7093fSSepherosa Ziehau 
590b3a7093fSSepherosa Ziehau static int
stpoll_register(struct ifnet * ifp,const struct ifpoll_status * st_rec)591b3a7093fSSepherosa Ziehau stpoll_register(struct ifnet *ifp, const struct ifpoll_status *st_rec)
592b3a7093fSSepherosa Ziehau {
593b3a7093fSSepherosa Ziehau 	struct stpoll_ctx *st_ctx = &stpoll_context;
594b3a7093fSSepherosa Ziehau 	int error;
595b3a7093fSSepherosa Ziehau 
5965204e13cSSepherosa Ziehau 	ASSERT_NETISR0;
597b3a7093fSSepherosa Ziehau 
598b3a7093fSSepherosa Ziehau 	if (st_rec->status_func == NULL)
599b3a7093fSSepherosa Ziehau 		return 0;
600b3a7093fSSepherosa Ziehau 
601b3a7093fSSepherosa Ziehau 	/*
602b3a7093fSSepherosa Ziehau 	 * Check if there is room.
603b3a7093fSSepherosa Ziehau 	 */
604b3a7093fSSepherosa Ziehau 	if (st_ctx->poll_handlers >= IFPOLL_LIST_LEN) {
605b3a7093fSSepherosa Ziehau 		/*
606b3a7093fSSepherosa Ziehau 		 * List full, cannot register more entries.
607b3a7093fSSepherosa Ziehau 		 * This should never happen; if it does, it is probably a
608b3a7093fSSepherosa Ziehau 		 * broken driver trying to register multiple times. Checking
609b3a7093fSSepherosa Ziehau 		 * this at runtime is expensive, and won't solve the problem
610b3a7093fSSepherosa Ziehau 		 * anyways, so just report a few times and then give up.
611b3a7093fSSepherosa Ziehau 		 */
612b3a7093fSSepherosa Ziehau 		static int verbose = 10; /* XXX */
613b3a7093fSSepherosa Ziehau 
614b3a7093fSSepherosa Ziehau 		if (verbose > 0) {
615b3a7093fSSepherosa Ziehau 			kprintf("status poll handlers list full, "
616b3a7093fSSepherosa Ziehau 				"maybe a broken driver ?\n");
617b3a7093fSSepherosa Ziehau 			verbose--;
618b3a7093fSSepherosa Ziehau 		}
619b3a7093fSSepherosa Ziehau 		error = ENOENT;
620b3a7093fSSepherosa Ziehau 	} else {
621b3a7093fSSepherosa Ziehau 		struct stpoll_rec *rec = &st_ctx->pr[st_ctx->poll_handlers];
622b3a7093fSSepherosa Ziehau 
623b3a7093fSSepherosa Ziehau 		rec->ifp = ifp;
624b3a7093fSSepherosa Ziehau 		rec->serializer = st_rec->serializer;
625b3a7093fSSepherosa Ziehau 		rec->status_func = st_rec->status_func;
626b3a7093fSSepherosa Ziehau 
627b3a7093fSSepherosa Ziehau 		st_ctx->poll_handlers++;
628b3a7093fSSepherosa Ziehau 		error = 0;
629b3a7093fSSepherosa Ziehau 	}
630b3a7093fSSepherosa Ziehau 	return error;
631b3a7093fSSepherosa Ziehau }
632b3a7093fSSepherosa Ziehau 
633b3a7093fSSepherosa Ziehau static int
stpoll_deregister(struct ifnet * ifp)634b3a7093fSSepherosa Ziehau stpoll_deregister(struct ifnet *ifp)
635b3a7093fSSepherosa Ziehau {
636b3a7093fSSepherosa Ziehau 	struct stpoll_ctx *st_ctx = &stpoll_context;
637b3a7093fSSepherosa Ziehau 	int i, error;
638b3a7093fSSepherosa Ziehau 
6395204e13cSSepherosa Ziehau 	ASSERT_NETISR0;
640b3a7093fSSepherosa Ziehau 
641b3a7093fSSepherosa Ziehau 	for (i = 0; i < st_ctx->poll_handlers; ++i) {
642b3a7093fSSepherosa Ziehau 		if (st_ctx->pr[i].ifp == ifp) /* Found it */
643b3a7093fSSepherosa Ziehau 			break;
644b3a7093fSSepherosa Ziehau 	}
645b3a7093fSSepherosa Ziehau 	if (i == st_ctx->poll_handlers) {
646b3a7093fSSepherosa Ziehau 		error = ENOENT;
647b3a7093fSSepherosa Ziehau 	} else {
648b3a7093fSSepherosa Ziehau 		st_ctx->poll_handlers--;
649b3a7093fSSepherosa Ziehau 		if (i < st_ctx->poll_handlers) {
650b3a7093fSSepherosa Ziehau 			/* Last entry replaces this one. */
651b3a7093fSSepherosa Ziehau 			st_ctx->pr[i] = st_ctx->pr[st_ctx->poll_handlers];
652b3a7093fSSepherosa Ziehau 		}
653b3a7093fSSepherosa Ziehau 		error = 0;
654b3a7093fSSepherosa Ziehau 	}
655b3a7093fSSepherosa Ziehau 	return error;
656b3a7093fSSepherosa Ziehau }
657b3a7093fSSepherosa Ziehau 
658b3a7093fSSepherosa Ziehau static __inline void
iopoll_reset_state(struct iopoll_ctx * io_ctx)659b3a7093fSSepherosa Ziehau iopoll_reset_state(struct iopoll_ctx *io_ctx)
660b3a7093fSSepherosa Ziehau {
661b3a7093fSSepherosa Ziehau 	crit_enter();
6625c721a05SSepherosa Ziehau 	io_ctx->poll_burst = io_ctx->poll_each_burst;
663b3a7093fSSepherosa Ziehau 	io_ctx->pending_polls = 0;
664b3a7093fSSepherosa Ziehau 	io_ctx->residual_burst = 0;
665b3a7093fSSepherosa Ziehau 	io_ctx->phase = 0;
666112d5942SSepherosa Ziehau 	io_ctx->kern_frac = 0;
667b3a7093fSSepherosa Ziehau 	bzero(&io_ctx->poll_start_t, sizeof(io_ctx->poll_start_t));
668b3a7093fSSepherosa Ziehau 	bzero(&io_ctx->prev_t, sizeof(io_ctx->prev_t));
669b3a7093fSSepherosa Ziehau 	crit_exit();
670b3a7093fSSepherosa Ziehau }
671b3a7093fSSepherosa Ziehau 
672b3a7093fSSepherosa Ziehau static void
iopoll_init(int cpuid)673b3a7093fSSepherosa Ziehau iopoll_init(int cpuid)
674b3a7093fSSepherosa Ziehau {
675d1958e12SSepherosa Ziehau 	KKASSERT(cpuid < netisr_ncpus);
676b3a7093fSSepherosa Ziehau 
677b3a7093fSSepherosa Ziehau 	rxpoll_context[cpuid] = iopoll_ctx_create(cpuid, IFPOLL_RX);
678b3a7093fSSepherosa Ziehau 	txpoll_context[cpuid] = iopoll_ctx_create(cpuid, IFPOLL_TX);
679b3a7093fSSepherosa Ziehau }
680b3a7093fSSepherosa Ziehau 
681b3a7093fSSepherosa Ziehau static struct iopoll_ctx *
iopoll_ctx_create(int cpuid,int poll_type)682b3a7093fSSepherosa Ziehau iopoll_ctx_create(int cpuid, int poll_type)
683b3a7093fSSepherosa Ziehau {
684968f17f7SSepherosa Ziehau 	struct poll_comm *comm;
685b3a7093fSSepherosa Ziehau 	struct iopoll_ctx *io_ctx;
686b3a7093fSSepherosa Ziehau 	const char *poll_type_str;
68798dc3fc1SSepherosa Ziehau 	netisr_fn_t handler, more_handler;
688b3a7093fSSepherosa Ziehau 
689b3a7093fSSepherosa Ziehau 	KKASSERT(poll_type == IFPOLL_RX || poll_type == IFPOLL_TX);
690b3a7093fSSepherosa Ziehau 
691b3a7093fSSepherosa Ziehau 	/*
692b3a7093fSSepherosa Ziehau 	 * Make sure that tunables are in sane state
693b3a7093fSSepherosa Ziehau 	 */
694b3a7093fSSepherosa Ziehau 	if (iopoll_burst_max < MIN_IOPOLL_BURST_MAX)
695b3a7093fSSepherosa Ziehau 		iopoll_burst_max = MIN_IOPOLL_BURST_MAX;
696b3a7093fSSepherosa Ziehau 	else if (iopoll_burst_max > MAX_IOPOLL_BURST_MAX)
697b3a7093fSSepherosa Ziehau 		iopoll_burst_max = MAX_IOPOLL_BURST_MAX;
698b3a7093fSSepherosa Ziehau 
699b3a7093fSSepherosa Ziehau 	if (iopoll_each_burst > iopoll_burst_max)
700b3a7093fSSepherosa Ziehau 		iopoll_each_burst = iopoll_burst_max;
701b3a7093fSSepherosa Ziehau 
702968f17f7SSepherosa Ziehau 	comm = poll_common[cpuid];
703968f17f7SSepherosa Ziehau 
704b3a7093fSSepherosa Ziehau 	/*
705b3a7093fSSepherosa Ziehau 	 * Create the per-cpu polling context
706b3a7093fSSepherosa Ziehau 	 */
70783d8ec1eSSepherosa Ziehau 	io_ctx = kmalloc(sizeof(*io_ctx), M_DEVBUF, M_WAITOK | M_ZERO);
708b3a7093fSSepherosa Ziehau 
709b3a7093fSSepherosa Ziehau 	io_ctx->poll_each_burst = iopoll_each_burst;
710b3a7093fSSepherosa Ziehau 	io_ctx->poll_burst_max = iopoll_burst_max;
711ac65d38fSSepherosa Ziehau 	io_ctx->user_frac = iopoll_user_frac;
712968f17f7SSepherosa Ziehau 	if (poll_type == IFPOLL_RX)
713968f17f7SSepherosa Ziehau 		io_ctx->pollhz = comm->pollhz;
714968f17f7SSepherosa Ziehau 	else
715968f17f7SSepherosa Ziehau 		io_ctx->pollhz = comm->pollhz / (comm->poll_txfrac + 1);
716b3a7093fSSepherosa Ziehau 	io_ctx->poll_cpuid = cpuid;
717b3a7093fSSepherosa Ziehau 	iopoll_reset_state(io_ctx);
718b3a7093fSSepherosa Ziehau 
71998dc3fc1SSepherosa Ziehau 	if (poll_type == IFPOLL_RX) {
72098dc3fc1SSepherosa Ziehau 		handler = rxpoll_handler;
72198dc3fc1SSepherosa Ziehau 		more_handler = rxpollmore_handler;
72298dc3fc1SSepherosa Ziehau 	} else {
72398dc3fc1SSepherosa Ziehau 		handler = txpoll_handler;
72498dc3fc1SSepherosa Ziehau 		more_handler = txpollmore_handler;
72598dc3fc1SSepherosa Ziehau 	}
72698dc3fc1SSepherosa Ziehau 
72748e7b118SMatthew Dillon 	netmsg_init(&io_ctx->poll_netmsg, NULL, &netisr_adone_rport,
72898dc3fc1SSepherosa Ziehau 	    0, handler);
729002c1265SMatthew Dillon 	io_ctx->poll_netmsg.lmsg.u.ms_resultp = io_ctx;
730b3a7093fSSepherosa Ziehau 
73148e7b118SMatthew Dillon 	netmsg_init(&io_ctx->poll_more_netmsg, NULL, &netisr_adone_rport,
73298dc3fc1SSepherosa Ziehau 	    0, more_handler);
733002c1265SMatthew Dillon 	io_ctx->poll_more_netmsg.lmsg.u.ms_resultp = io_ctx;
734b3a7093fSSepherosa Ziehau 
735b3a7093fSSepherosa Ziehau 	/*
736b3a7093fSSepherosa Ziehau 	 * Initialize per-cpu sysctl nodes
737b3a7093fSSepherosa Ziehau 	 */
738b3a7093fSSepherosa Ziehau 	if (poll_type == IFPOLL_RX)
739b3a7093fSSepherosa Ziehau 		poll_type_str = "rx";
740b3a7093fSSepherosa Ziehau 	else
741b3a7093fSSepherosa Ziehau 		poll_type_str = "tx";
742b3a7093fSSepherosa Ziehau 
743b3a7093fSSepherosa Ziehau 	sysctl_ctx_init(&io_ctx->poll_sysctl_ctx);
744b3a7093fSSepherosa Ziehau 	io_ctx->poll_sysctl_tree = SYSCTL_ADD_NODE(&io_ctx->poll_sysctl_ctx,
745a7519a15SSepherosa Ziehau 				   SYSCTL_CHILDREN(comm->sysctl_tree),
746a7519a15SSepherosa Ziehau 				   OID_AUTO, poll_type_str, CTLFLAG_RD, 0, "");
747b3a7093fSSepherosa Ziehau 	iopoll_add_sysctl(&io_ctx->poll_sysctl_ctx,
74898dc3fc1SSepherosa Ziehau 	    SYSCTL_CHILDREN(io_ctx->poll_sysctl_tree), io_ctx, poll_type);
749b3a7093fSSepherosa Ziehau 
750b3a7093fSSepherosa Ziehau 	return io_ctx;
751b3a7093fSSepherosa Ziehau }
752b3a7093fSSepherosa Ziehau 
753b3a7093fSSepherosa Ziehau /*
754b3a7093fSSepherosa Ziehau  * Hook from iopoll systimer.  Tries to schedule an iopoll, but keeps
755b3a7093fSSepherosa Ziehau  * track of lost ticks due to the previous handler taking too long.
756b3a7093fSSepherosa Ziehau  * Normally, this should not happen, because polling handler should
757b3a7093fSSepherosa Ziehau  * run for a short time.  However, in some cases (e.g. when there are
758b3a7093fSSepherosa Ziehau  * changes in link status etc.) the drivers take a very long time
759b3a7093fSSepherosa Ziehau  * (even in the order of milliseconds) to reset and reconfigure the
760b3a7093fSSepherosa Ziehau  * device, causing apparent lost polls.
761b3a7093fSSepherosa Ziehau  *
762b3a7093fSSepherosa Ziehau  * The first part of the code is just for debugging purposes, and tries
763b3a7093fSSepherosa Ziehau  * to count how often hardclock ticks are shorter than they should,
764b3a7093fSSepherosa Ziehau  * meaning either stray interrupts or delayed events.
765b3a7093fSSepherosa Ziehau  *
766b3a7093fSSepherosa Ziehau  * WARNING! called from fastint or IPI, the MP lock might not be held.
76742bb6c2eSSepherosa Ziehau  * NOTE: Caller should hold critical section.
768b3a7093fSSepherosa Ziehau  */
769b3a7093fSSepherosa Ziehau static void
iopoll_clock(struct iopoll_ctx * io_ctx)770b3a7093fSSepherosa Ziehau iopoll_clock(struct iopoll_ctx *io_ctx)
771b3a7093fSSepherosa Ziehau {
77284a3d873SSepherosa Ziehau 	union microtime_pcpu t;
773968f17f7SSepherosa Ziehau 	int delta;
774b3a7093fSSepherosa Ziehau 
77542bb6c2eSSepherosa Ziehau 	KKASSERT(mycpuid == io_ctx->poll_cpuid);
776b3a7093fSSepherosa Ziehau 
777b3a7093fSSepherosa Ziehau 	if (io_ctx->poll_handlers == 0)
778b3a7093fSSepherosa Ziehau 		return;
779b3a7093fSSepherosa Ziehau 
78095ff9ed0SSepherosa Ziehau 	logpoll(ioclock_start);
78195ff9ed0SSepherosa Ziehau 
78284a3d873SSepherosa Ziehau 	microtime_pcpu_get(&t);
78384a3d873SSepherosa Ziehau 	delta = microtime_pcpu_diff(&io_ctx->prev_t, &t);
784968f17f7SSepherosa Ziehau 	if (delta * io_ctx->pollhz < 500000)
785b3a7093fSSepherosa Ziehau 		io_ctx->short_ticks++;
786b3a7093fSSepherosa Ziehau 	else
787b3a7093fSSepherosa Ziehau 		io_ctx->prev_t = t;
788b3a7093fSSepherosa Ziehau 
789b3a7093fSSepherosa Ziehau 	if (io_ctx->pending_polls > 100) {
790b3a7093fSSepherosa Ziehau 		/*
791b3a7093fSSepherosa Ziehau 		 * Too much, assume it has stalled (not always true
792b3a7093fSSepherosa Ziehau 		 * see comment above).
793b3a7093fSSepherosa Ziehau 		 */
794b3a7093fSSepherosa Ziehau 		io_ctx->stalled++;
795b3a7093fSSepherosa Ziehau 		io_ctx->pending_polls = 0;
796b3a7093fSSepherosa Ziehau 		io_ctx->phase = 0;
797b3a7093fSSepherosa Ziehau 	}
798b3a7093fSSepherosa Ziehau 
799b3a7093fSSepherosa Ziehau 	if (io_ctx->phase <= 2) {
800b3a7093fSSepherosa Ziehau 		if (io_ctx->phase != 0)
801b3a7093fSSepherosa Ziehau 			io_ctx->suspect++;
802b3a7093fSSepherosa Ziehau 		io_ctx->phase = 1;
803b3a7093fSSepherosa Ziehau 		sched_iopoll(io_ctx);
804b3a7093fSSepherosa Ziehau 		io_ctx->phase = 2;
805b3a7093fSSepherosa Ziehau 	}
806b3a7093fSSepherosa Ziehau 	if (io_ctx->pending_polls++ > 0)
807b3a7093fSSepherosa Ziehau 		io_ctx->lost_polls++;
80895ff9ed0SSepherosa Ziehau 
80995ff9ed0SSepherosa Ziehau 	logpoll(ioclock_end);
810b3a7093fSSepherosa Ziehau }
811b3a7093fSSepherosa Ziehau 
812b3a7093fSSepherosa Ziehau /*
81398dc3fc1SSepherosa Ziehau  * rxpoll_handler and txpoll_handler are scheduled by sched_iopoll when
81498dc3fc1SSepherosa Ziehau  * appropriate, typically once per polling systimer tick.
815b3a7093fSSepherosa Ziehau  *
816b3a7093fSSepherosa Ziehau  * Note that the message is replied immediately in order to allow a new
817b3a7093fSSepherosa Ziehau  * ISR to be scheduled in the handler.
818b3a7093fSSepherosa Ziehau  */
819b3a7093fSSepherosa Ziehau static void
rxpoll_handler(netmsg_t msg)82098dc3fc1SSepherosa Ziehau rxpoll_handler(netmsg_t msg)
821b3a7093fSSepherosa Ziehau {
822b3a7093fSSepherosa Ziehau 	struct iopoll_ctx *io_ctx;
823ec7f6da8SSepherosa Ziehau 	struct thread *td = curthread;
8240178c976SSepherosa Ziehau 	boolean_t direct = TRUE, crit;
825b3a7093fSSepherosa Ziehau 	int i, cycles;
826b3a7093fSSepherosa Ziehau 
82795ff9ed0SSepherosa Ziehau 	logpoll(rx_start);
82895ff9ed0SSepherosa Ziehau 
829002c1265SMatthew Dillon 	io_ctx = msg->lmsg.u.ms_resultp;
830ec7f7fc8SSepherosa Ziehau 	KKASSERT(&td->td_msgport == netisr_cpuport(io_ctx->poll_cpuid));
831ec7f6da8SSepherosa Ziehau 
8320178c976SSepherosa Ziehau 	crit = TRUE;
833ec7f6da8SSepherosa Ziehau 	crit_enter_quick(td);
834b3a7093fSSepherosa Ziehau 
835b3a7093fSSepherosa Ziehau 	/* Reply ASAP */
836e3bcf7f5SSepherosa Ziehau 	netisr_replymsg(&msg->base, 0);
837b3a7093fSSepherosa Ziehau 
838ec7f6da8SSepherosa Ziehau 	if (io_ctx->poll_handlers == 0) {
839ec7f6da8SSepherosa Ziehau 		crit_exit_quick(td);
84095ff9ed0SSepherosa Ziehau 		logpoll(rx_end);
841b3a7093fSSepherosa Ziehau 		return;
842ec7f6da8SSepherosa Ziehau 	}
843b3a7093fSSepherosa Ziehau 
844b3a7093fSSepherosa Ziehau 	io_ctx->phase = 3;
845b3a7093fSSepherosa Ziehau 	if (io_ctx->residual_burst == 0) {
846b3a7093fSSepherosa Ziehau 		/* First call in this tick */
84784a3d873SSepherosa Ziehau 		microtime_pcpu_get(&io_ctx->poll_start_t);
848b3a7093fSSepherosa Ziehau 		io_ctx->residual_burst = io_ctx->poll_burst;
849b3a7093fSSepherosa Ziehau 	}
850b3a7093fSSepherosa Ziehau 	cycles = (io_ctx->residual_burst < io_ctx->poll_each_burst) ?
851b3a7093fSSepherosa Ziehau 		 io_ctx->residual_burst : io_ctx->poll_each_burst;
852b3a7093fSSepherosa Ziehau 	io_ctx->residual_burst -= cycles;
853b3a7093fSSepherosa Ziehau 
854b3a7093fSSepherosa Ziehau 	for (i = 0; i < io_ctx->poll_handlers; i++) {
855b3a7093fSSepherosa Ziehau 		const struct iopoll_rec *rec = &io_ctx->pr[i];
856b3a7093fSSepherosa Ziehau 		struct ifnet *ifp = rec->ifp;
857b3a7093fSSepherosa Ziehau 
8580178c976SSepherosa Ziehau 		if (rec->serializer != NULL) {
8590178c976SSepherosa Ziehau 			if (!crit) {
8600178c976SSepherosa Ziehau 				crit = TRUE;
8610178c976SSepherosa Ziehau 				crit_enter_quick(td);
8620178c976SSepherosa Ziehau 			}
8630178c976SSepherosa Ziehau 			if (__predict_false(
8640178c976SSepherosa Ziehau 			    !lwkt_serialize_try(rec->serializer))) {
8650178c976SSepherosa Ziehau 				/* RX serializer generally will not fail. */
866b3a7093fSSepherosa Ziehau 				continue;
8670178c976SSepherosa Ziehau 			}
8680178c976SSepherosa Ziehau 		} else if (crit) {
8690178c976SSepherosa Ziehau 			/*
8700178c976SSepherosa Ziehau 			 * Exit critical section, if the RX polling
8710178c976SSepherosa Ziehau 			 * handler does not require serialization,
8720178c976SSepherosa Ziehau 			 * i.e. RX polling is doing direct input.
8730178c976SSepherosa Ziehau 			 */
8740178c976SSepherosa Ziehau 			crit_exit_quick(td);
8750178c976SSepherosa Ziehau 			crit = FALSE;
8760178c976SSepherosa Ziehau 		}
877b3a7093fSSepherosa Ziehau 
8788d0afa86SSepherosa Ziehau 		if ((ifp->if_flags & IFF_IDIRECT) == 0) {
8798d0afa86SSepherosa Ziehau 			direct = FALSE;
8800178c976SSepherosa Ziehau 			KASSERT(rec->serializer != NULL,
8810178c976SSepherosa Ziehau 			    ("rx polling handler is not serialized"));
8828d0afa86SSepherosa Ziehau 		}
8838d0afa86SSepherosa Ziehau #ifdef INVARIANTS
8848d0afa86SSepherosa Ziehau 		else {
8858d0afa86SSepherosa Ziehau 			KASSERT(rec->serializer == NULL,
8868d0afa86SSepherosa Ziehau 			    ("serialized direct input"));
8878d0afa86SSepherosa Ziehau 		}
8888d0afa86SSepherosa Ziehau #endif
8898d0afa86SSepherosa Ziehau 
8908d0afa86SSepherosa Ziehau 		if ((ifp->if_flags & (IFF_UP | IFF_RUNNING | IFF_NPOLLING)) ==
8918d0afa86SSepherosa Ziehau 		    (IFF_UP | IFF_RUNNING | IFF_NPOLLING))
892b3a7093fSSepherosa Ziehau 			rec->poll_func(ifp, rec->arg, cycles);
893b3a7093fSSepherosa Ziehau 
8948d0afa86SSepherosa Ziehau 		if (rec->serializer != NULL)
895b3a7093fSSepherosa Ziehau 			lwkt_serialize_exit(rec->serializer);
896b3a7093fSSepherosa Ziehau 	}
897b3a7093fSSepherosa Ziehau 
8980178c976SSepherosa Ziehau 	if (crit) {
8999feed54bSSepherosa Ziehau 		/*
9009feed54bSSepherosa Ziehau 		 * Do a quick exit/enter to catch any higher-priority
9019feed54bSSepherosa Ziehau 		 * interrupt sources.
9029feed54bSSepherosa Ziehau 		 */
903ec7f6da8SSepherosa Ziehau 		crit_exit_quick(td);
9040178c976SSepherosa Ziehau 	}
9059feed54bSSepherosa Ziehau 	crit_enter_quick(td);
906ec7f6da8SSepherosa Ziehau 
907b3a7093fSSepherosa Ziehau 	io_ctx->phase = 4;
9088d0afa86SSepherosa Ziehau 	sched_iopollmore(io_ctx, direct);
9099feed54bSSepherosa Ziehau 
9109feed54bSSepherosa Ziehau 	crit_exit_quick(td);
91195ff9ed0SSepherosa Ziehau 
91295ff9ed0SSepherosa Ziehau 	logpoll(rx_end);
913b3a7093fSSepherosa Ziehau }
914b3a7093fSSepherosa Ziehau 
91598dc3fc1SSepherosa Ziehau static void
txpoll_handler(netmsg_t msg)91698dc3fc1SSepherosa Ziehau txpoll_handler(netmsg_t msg)
91798dc3fc1SSepherosa Ziehau {
91898dc3fc1SSepherosa Ziehau 	struct iopoll_ctx *io_ctx;
91998dc3fc1SSepherosa Ziehau 	struct thread *td = curthread;
92098dc3fc1SSepherosa Ziehau 	int i;
92198dc3fc1SSepherosa Ziehau 
92295ff9ed0SSepherosa Ziehau 	logpoll(tx_start);
92395ff9ed0SSepherosa Ziehau 
92498dc3fc1SSepherosa Ziehau 	io_ctx = msg->lmsg.u.ms_resultp;
925ec7f7fc8SSepherosa Ziehau 	KKASSERT(&td->td_msgport == netisr_cpuport(io_ctx->poll_cpuid));
92698dc3fc1SSepherosa Ziehau 
92798dc3fc1SSepherosa Ziehau 	crit_enter_quick(td);
92898dc3fc1SSepherosa Ziehau 
92998dc3fc1SSepherosa Ziehau 	/* Reply ASAP */
930e3bcf7f5SSepherosa Ziehau 	netisr_replymsg(&msg->base, 0);
93198dc3fc1SSepherosa Ziehau 
93298dc3fc1SSepherosa Ziehau 	if (io_ctx->poll_handlers == 0) {
93398dc3fc1SSepherosa Ziehau 		crit_exit_quick(td);
93495ff9ed0SSepherosa Ziehau 		logpoll(tx_end);
93598dc3fc1SSepherosa Ziehau 		return;
93698dc3fc1SSepherosa Ziehau 	}
93798dc3fc1SSepherosa Ziehau 
93898dc3fc1SSepherosa Ziehau 	io_ctx->phase = 3;
93998dc3fc1SSepherosa Ziehau 
94098dc3fc1SSepherosa Ziehau 	for (i = 0; i < io_ctx->poll_handlers; i++) {
94198dc3fc1SSepherosa Ziehau 		const struct iopoll_rec *rec = &io_ctx->pr[i];
94298dc3fc1SSepherosa Ziehau 		struct ifnet *ifp = rec->ifp;
94398dc3fc1SSepherosa Ziehau 
94498dc3fc1SSepherosa Ziehau 		if (!lwkt_serialize_try(rec->serializer))
94598dc3fc1SSepherosa Ziehau 			continue;
94698dc3fc1SSepherosa Ziehau 
9478d0afa86SSepherosa Ziehau 		if ((ifp->if_flags & (IFF_UP | IFF_RUNNING | IFF_NPOLLING)) ==
9488d0afa86SSepherosa Ziehau 		    (IFF_UP | IFF_RUNNING | IFF_NPOLLING))
94998dc3fc1SSepherosa Ziehau 			rec->poll_func(ifp, rec->arg, -1);
95098dc3fc1SSepherosa Ziehau 
95198dc3fc1SSepherosa Ziehau 		lwkt_serialize_exit(rec->serializer);
95298dc3fc1SSepherosa Ziehau 	}
95398dc3fc1SSepherosa Ziehau 
954b3a7093fSSepherosa Ziehau 	/*
95598dc3fc1SSepherosa Ziehau 	 * Do a quick exit/enter to catch any higher-priority
95698dc3fc1SSepherosa Ziehau 	 * interrupt sources.
95798dc3fc1SSepherosa Ziehau 	 */
95898dc3fc1SSepherosa Ziehau 	crit_exit_quick(td);
95998dc3fc1SSepherosa Ziehau 	crit_enter_quick(td);
96098dc3fc1SSepherosa Ziehau 
96198dc3fc1SSepherosa Ziehau 	io_ctx->phase = 4;
9628d0afa86SSepherosa Ziehau 	sched_iopollmore(io_ctx, TRUE);
96398dc3fc1SSepherosa Ziehau 
96498dc3fc1SSepherosa Ziehau 	crit_exit_quick(td);
96595ff9ed0SSepherosa Ziehau 
96695ff9ed0SSepherosa Ziehau 	logpoll(tx_end);
96798dc3fc1SSepherosa Ziehau }
96898dc3fc1SSepherosa Ziehau 
96998dc3fc1SSepherosa Ziehau /*
97098dc3fc1SSepherosa Ziehau  * rxpollmore_handler and txpollmore_handler are called after other netisr's,
97198dc3fc1SSepherosa Ziehau  * possibly scheduling another rxpoll_handler or txpoll_handler call, or
97298dc3fc1SSepherosa Ziehau  * adapting the burst size for the next cycle.
973b3a7093fSSepherosa Ziehau  *
974b3a7093fSSepherosa Ziehau  * It is very bad to fetch large bursts of packets from a single card at once,
975b3a7093fSSepherosa Ziehau  * because the burst could take a long time to be completely processed leading
976b3a7093fSSepherosa Ziehau  * to unfairness.  To reduce the problem, and also to account better for time
977b3a7093fSSepherosa Ziehau  * spent in network-related processing, we split the burst in smaller chunks
978b3a7093fSSepherosa Ziehau  * of fixed size, giving control to the other netisr's between chunks.  This
979b3a7093fSSepherosa Ziehau  * helps in improving the fairness, reducing livelock and accounting for the
980b3a7093fSSepherosa Ziehau  * work performed in low level handling.
981b3a7093fSSepherosa Ziehau  */
982b3a7093fSSepherosa Ziehau static void
rxpollmore_handler(netmsg_t msg)98398dc3fc1SSepherosa Ziehau rxpollmore_handler(netmsg_t msg)
984b3a7093fSSepherosa Ziehau {
9859feed54bSSepherosa Ziehau 	struct thread *td = curthread;
986b3a7093fSSepherosa Ziehau 	struct iopoll_ctx *io_ctx;
98784a3d873SSepherosa Ziehau 	union microtime_pcpu t;
988968f17f7SSepherosa Ziehau 	int kern_load;
989b3a7093fSSepherosa Ziehau 	uint32_t pending_polls;
990b3a7093fSSepherosa Ziehau 
99195ff9ed0SSepherosa Ziehau 	logpoll(rx_mstart);
99295ff9ed0SSepherosa Ziehau 
993002c1265SMatthew Dillon 	io_ctx = msg->lmsg.u.ms_resultp;
994ec7f7fc8SSepherosa Ziehau 	KKASSERT(&td->td_msgport == netisr_cpuport(io_ctx->poll_cpuid));
9959feed54bSSepherosa Ziehau 
9969feed54bSSepherosa Ziehau 	crit_enter_quick(td);
997b3a7093fSSepherosa Ziehau 
998b3a7093fSSepherosa Ziehau 	/* Replay ASAP */
999e3bcf7f5SSepherosa Ziehau 	netisr_replymsg(&msg->base, 0);
1000b3a7093fSSepherosa Ziehau 
10019feed54bSSepherosa Ziehau 	if (io_ctx->poll_handlers == 0) {
10029feed54bSSepherosa Ziehau 		crit_exit_quick(td);
100395ff9ed0SSepherosa Ziehau 		logpoll(rx_mend);
1004b3a7093fSSepherosa Ziehau 		return;
10059feed54bSSepherosa Ziehau 	}
1006b3a7093fSSepherosa Ziehau 
1007b3a7093fSSepherosa Ziehau 	io_ctx->phase = 5;
1008b3a7093fSSepherosa Ziehau 	if (io_ctx->residual_burst > 0) {
1009b3a7093fSSepherosa Ziehau 		sched_iopoll(io_ctx);
10109feed54bSSepherosa Ziehau 		crit_exit_quick(td);
1011b3a7093fSSepherosa Ziehau 		/* Will run immediately on return, followed by netisrs */
101295ff9ed0SSepherosa Ziehau 		logpoll(rx_mend);
1013b3a7093fSSepherosa Ziehau 		return;
1014b3a7093fSSepherosa Ziehau 	}
1015b3a7093fSSepherosa Ziehau 
1016b3a7093fSSepherosa Ziehau 	/* Here we can account time spent in iopoll's in this tick */
101784a3d873SSepherosa Ziehau 	microtime_pcpu_get(&t);
101884a3d873SSepherosa Ziehau 	kern_load = microtime_pcpu_diff(&io_ctx->poll_start_t, &t);
1019968f17f7SSepherosa Ziehau 	kern_load = (kern_load * io_ctx->pollhz) / 10000; /* 0..100 */
1020112d5942SSepherosa Ziehau 	io_ctx->kern_frac = kern_load;
1021112d5942SSepherosa Ziehau 
1022b3a7093fSSepherosa Ziehau 	if (kern_load > (100 - io_ctx->user_frac)) {
1023b3a7093fSSepherosa Ziehau 		/* Try decrease ticks */
1024b3a7093fSSepherosa Ziehau 		if (io_ctx->poll_burst > 1)
1025b3a7093fSSepherosa Ziehau 			io_ctx->poll_burst--;
1026b3a7093fSSepherosa Ziehau 	} else {
1027b3a7093fSSepherosa Ziehau 		if (io_ctx->poll_burst < io_ctx->poll_burst_max)
1028b3a7093fSSepherosa Ziehau 			io_ctx->poll_burst++;
1029b3a7093fSSepherosa Ziehau 	}
1030b3a7093fSSepherosa Ziehau 
1031b3a7093fSSepherosa Ziehau 	io_ctx->pending_polls--;
1032b3a7093fSSepherosa Ziehau 	pending_polls = io_ctx->pending_polls;
1033b3a7093fSSepherosa Ziehau 
1034b3a7093fSSepherosa Ziehau 	if (pending_polls == 0) {
1035b3a7093fSSepherosa Ziehau 		/* We are done */
1036b3a7093fSSepherosa Ziehau 		io_ctx->phase = 0;
1037b3a7093fSSepherosa Ziehau 	} else {
1038b3a7093fSSepherosa Ziehau 		/*
1039b3a7093fSSepherosa Ziehau 		 * Last cycle was long and caused us to miss one or more
1040b3a7093fSSepherosa Ziehau 		 * hardclock ticks.  Restart processing again, but slightly
1041b3a7093fSSepherosa Ziehau 		 * reduce the burst size to prevent that this happens again.
1042b3a7093fSSepherosa Ziehau 		 */
1043b3a7093fSSepherosa Ziehau 		io_ctx->poll_burst -= (io_ctx->poll_burst / 8);
1044b3a7093fSSepherosa Ziehau 		if (io_ctx->poll_burst < 1)
1045b3a7093fSSepherosa Ziehau 			io_ctx->poll_burst = 1;
1046b3a7093fSSepherosa Ziehau 		sched_iopoll(io_ctx);
1047b3a7093fSSepherosa Ziehau 		io_ctx->phase = 6;
1048b3a7093fSSepherosa Ziehau 	}
10499feed54bSSepherosa Ziehau 
10509feed54bSSepherosa Ziehau 	crit_exit_quick(td);
105195ff9ed0SSepherosa Ziehau 
105295ff9ed0SSepherosa Ziehau 	logpoll(rx_mend);
1053b3a7093fSSepherosa Ziehau }
1054b3a7093fSSepherosa Ziehau 
1055b3a7093fSSepherosa Ziehau static void
txpollmore_handler(netmsg_t msg)105698dc3fc1SSepherosa Ziehau txpollmore_handler(netmsg_t msg)
1057b3a7093fSSepherosa Ziehau {
105898dc3fc1SSepherosa Ziehau 	struct thread *td = curthread;
105998dc3fc1SSepherosa Ziehau 	struct iopoll_ctx *io_ctx;
106098dc3fc1SSepherosa Ziehau 	uint32_t pending_polls;
106198dc3fc1SSepherosa Ziehau 
106295ff9ed0SSepherosa Ziehau 	logpoll(tx_mstart);
106395ff9ed0SSepherosa Ziehau 
106498dc3fc1SSepherosa Ziehau 	io_ctx = msg->lmsg.u.ms_resultp;
1065ec7f7fc8SSepherosa Ziehau 	KKASSERT(&td->td_msgport == netisr_cpuport(io_ctx->poll_cpuid));
106698dc3fc1SSepherosa Ziehau 
106798dc3fc1SSepherosa Ziehau 	crit_enter_quick(td);
106898dc3fc1SSepherosa Ziehau 
106998dc3fc1SSepherosa Ziehau 	/* Replay ASAP */
1070e3bcf7f5SSepherosa Ziehau 	netisr_replymsg(&msg->base, 0);
107198dc3fc1SSepherosa Ziehau 
107298dc3fc1SSepherosa Ziehau 	if (io_ctx->poll_handlers == 0) {
107398dc3fc1SSepherosa Ziehau 		crit_exit_quick(td);
107495ff9ed0SSepherosa Ziehau 		logpoll(tx_mend);
107598dc3fc1SSepherosa Ziehau 		return;
107698dc3fc1SSepherosa Ziehau 	}
107798dc3fc1SSepherosa Ziehau 
107898dc3fc1SSepherosa Ziehau 	io_ctx->phase = 5;
107998dc3fc1SSepherosa Ziehau 
108098dc3fc1SSepherosa Ziehau 	io_ctx->pending_polls--;
108198dc3fc1SSepherosa Ziehau 	pending_polls = io_ctx->pending_polls;
108298dc3fc1SSepherosa Ziehau 
108398dc3fc1SSepherosa Ziehau 	if (pending_polls == 0) {
108498dc3fc1SSepherosa Ziehau 		/* We are done */
108598dc3fc1SSepherosa Ziehau 		io_ctx->phase = 0;
108698dc3fc1SSepherosa Ziehau 	} else {
108798dc3fc1SSepherosa Ziehau 		/*
108898dc3fc1SSepherosa Ziehau 		 * Last cycle was long and caused us to miss one or more
108998dc3fc1SSepherosa Ziehau 		 * hardclock ticks.  Restart processing again.
109098dc3fc1SSepherosa Ziehau 		 */
109198dc3fc1SSepherosa Ziehau 		sched_iopoll(io_ctx);
109298dc3fc1SSepherosa Ziehau 		io_ctx->phase = 6;
109398dc3fc1SSepherosa Ziehau 	}
109498dc3fc1SSepherosa Ziehau 
109598dc3fc1SSepherosa Ziehau 	crit_exit_quick(td);
109695ff9ed0SSepherosa Ziehau 
109795ff9ed0SSepherosa Ziehau 	logpoll(tx_mend);
109898dc3fc1SSepherosa Ziehau }
109998dc3fc1SSepherosa Ziehau 
110098dc3fc1SSepherosa Ziehau static void
iopoll_add_sysctl(struct sysctl_ctx_list * ctx,struct sysctl_oid_list * parent,struct iopoll_ctx * io_ctx,int poll_type)110198dc3fc1SSepherosa Ziehau iopoll_add_sysctl(struct sysctl_ctx_list *ctx, struct sysctl_oid_list *parent,
110298dc3fc1SSepherosa Ziehau     struct iopoll_ctx *io_ctx, int poll_type)
110398dc3fc1SSepherosa Ziehau {
110498dc3fc1SSepherosa Ziehau 	if (poll_type == IFPOLL_RX) {
1105b3a7093fSSepherosa Ziehau 		SYSCTL_ADD_PROC(ctx, parent, OID_AUTO, "burst_max",
1106b3a7093fSSepherosa Ziehau 		    CTLTYPE_UINT | CTLFLAG_RW, io_ctx, 0, sysctl_burstmax,
1107b3a7093fSSepherosa Ziehau 		    "IU", "Max Polling burst size");
1108b3a7093fSSepherosa Ziehau 
1109b3a7093fSSepherosa Ziehau 		SYSCTL_ADD_PROC(ctx, parent, OID_AUTO, "each_burst",
1110b3a7093fSSepherosa Ziehau 		    CTLTYPE_UINT | CTLFLAG_RW, io_ctx, 0, sysctl_eachburst,
1111b3a7093fSSepherosa Ziehau 		    "IU", "Max size of each burst");
1112b3a7093fSSepherosa Ziehau 
111398dc3fc1SSepherosa Ziehau 		SYSCTL_ADD_UINT(ctx, parent, OID_AUTO, "burst", CTLFLAG_RD,
111498dc3fc1SSepherosa Ziehau 		    &io_ctx->poll_burst, 0, "Current polling burst size");
111598dc3fc1SSepherosa Ziehau 
111698dc3fc1SSepherosa Ziehau 		SYSCTL_ADD_UINT(ctx, parent, OID_AUTO, "user_frac", CTLFLAG_RW,
111798dc3fc1SSepherosa Ziehau 		    &io_ctx->user_frac, 0, "Desired user fraction of cpu time");
111898dc3fc1SSepherosa Ziehau 
111998dc3fc1SSepherosa Ziehau 		SYSCTL_ADD_UINT(ctx, parent, OID_AUTO, "kern_frac", CTLFLAG_RD,
112098dc3fc1SSepherosa Ziehau 		    &io_ctx->kern_frac, 0, "Kernel fraction of cpu time");
112198dc3fc1SSepherosa Ziehau 
112298dc3fc1SSepherosa Ziehau 		SYSCTL_ADD_INT(ctx, parent, OID_AUTO, "residual_burst", CTLFLAG_RD,
112398dc3fc1SSepherosa Ziehau 		    &io_ctx->residual_burst, 0,
112498dc3fc1SSepherosa Ziehau 		    "# of residual cycles in burst");
112598dc3fc1SSepherosa Ziehau 	}
112698dc3fc1SSepherosa Ziehau 
1127b3a7093fSSepherosa Ziehau 	SYSCTL_ADD_UINT(ctx, parent, OID_AUTO, "phase", CTLFLAG_RD,
1128b3a7093fSSepherosa Ziehau 	    &io_ctx->phase, 0, "Polling phase");
1129b3a7093fSSepherosa Ziehau 
1130e3f2143dSSepherosa Ziehau 	SYSCTL_ADD_ULONG(ctx, parent, OID_AUTO, "suspect", CTLFLAG_RW,
1131e3f2143dSSepherosa Ziehau 	    &io_ctx->suspect, "Suspected events");
1132b3a7093fSSepherosa Ziehau 
1133e3f2143dSSepherosa Ziehau 	SYSCTL_ADD_ULONG(ctx, parent, OID_AUTO, "stalled", CTLFLAG_RW,
1134e3f2143dSSepherosa Ziehau 	    &io_ctx->stalled, "Potential stalls");
1135b3a7093fSSepherosa Ziehau 
1136e3f2143dSSepherosa Ziehau 	SYSCTL_ADD_ULONG(ctx, parent, OID_AUTO, "short_ticks", CTLFLAG_RW,
1137e3f2143dSSepherosa Ziehau 	    &io_ctx->short_ticks,
1138b3a7093fSSepherosa Ziehau 	    "Hardclock ticks shorter than they should be");
1139b3a7093fSSepherosa Ziehau 
1140e3f2143dSSepherosa Ziehau 	SYSCTL_ADD_ULONG(ctx, parent, OID_AUTO, "lost_polls", CTLFLAG_RW,
1141e3f2143dSSepherosa Ziehau 	    &io_ctx->lost_polls,
1142b3a7093fSSepherosa Ziehau 	    "How many times we would have lost a poll tick");
1143b3a7093fSSepherosa Ziehau 
1144b3a7093fSSepherosa Ziehau 	SYSCTL_ADD_UINT(ctx, parent, OID_AUTO, "pending_polls", CTLFLAG_RD,
1145b3a7093fSSepherosa Ziehau 	    &io_ctx->pending_polls, 0, "Do we need to poll again");
1146b3a7093fSSepherosa Ziehau 
1147b3a7093fSSepherosa Ziehau 	SYSCTL_ADD_UINT(ctx, parent, OID_AUTO, "handlers", CTLFLAG_RD,
1148e3f2143dSSepherosa Ziehau 	    &io_ctx->poll_handlers, 0, "Number of registered poll handlers");
1149b3a7093fSSepherosa Ziehau }
1150b3a7093fSSepherosa Ziehau 
1151b3a7093fSSepherosa Ziehau static void
sysctl_burstmax_handler(netmsg_t nmsg)1152002c1265SMatthew Dillon sysctl_burstmax_handler(netmsg_t nmsg)
1153b3a7093fSSepherosa Ziehau {
1154b3a7093fSSepherosa Ziehau 	struct iopoll_sysctl_netmsg *msg = (struct iopoll_sysctl_netmsg *)nmsg;
1155b3a7093fSSepherosa Ziehau 	struct iopoll_ctx *io_ctx;
1156b3a7093fSSepherosa Ziehau 
1157b3a7093fSSepherosa Ziehau 	io_ctx = msg->ctx;
1158ec7f7fc8SSepherosa Ziehau 	KKASSERT(&curthread->td_msgport == netisr_cpuport(io_ctx->poll_cpuid));
1159b3a7093fSSepherosa Ziehau 
1160002c1265SMatthew Dillon 	io_ctx->poll_burst_max = nmsg->lmsg.u.ms_result;
1161b3a7093fSSepherosa Ziehau 	if (io_ctx->poll_each_burst > io_ctx->poll_burst_max)
1162b3a7093fSSepherosa Ziehau 		io_ctx->poll_each_burst = io_ctx->poll_burst_max;
1163b3a7093fSSepherosa Ziehau 	if (io_ctx->poll_burst > io_ctx->poll_burst_max)
1164b3a7093fSSepherosa Ziehau 		io_ctx->poll_burst = io_ctx->poll_burst_max;
1165b3a7093fSSepherosa Ziehau 	if (io_ctx->residual_burst > io_ctx->poll_burst_max)
1166b3a7093fSSepherosa Ziehau 		io_ctx->residual_burst = io_ctx->poll_burst_max;
1167b3a7093fSSepherosa Ziehau 
1168e3bcf7f5SSepherosa Ziehau 	netisr_replymsg(&nmsg->base, 0);
1169b3a7093fSSepherosa Ziehau }
1170b3a7093fSSepherosa Ziehau 
1171b3a7093fSSepherosa Ziehau static int
sysctl_burstmax(SYSCTL_HANDLER_ARGS)1172b3a7093fSSepherosa Ziehau sysctl_burstmax(SYSCTL_HANDLER_ARGS)
1173b3a7093fSSepherosa Ziehau {
1174b3a7093fSSepherosa Ziehau 	struct iopoll_ctx *io_ctx = arg1;
1175b3a7093fSSepherosa Ziehau 	struct iopoll_sysctl_netmsg msg;
1176b3a7093fSSepherosa Ziehau 	uint32_t burst_max;
1177b3a7093fSSepherosa Ziehau 	int error;
1178b3a7093fSSepherosa Ziehau 
1179b3a7093fSSepherosa Ziehau 	burst_max = io_ctx->poll_burst_max;
1180b3a7093fSSepherosa Ziehau 	error = sysctl_handle_int(oidp, &burst_max, 0, req);
1181b3a7093fSSepherosa Ziehau 	if (error || req->newptr == NULL)
1182b3a7093fSSepherosa Ziehau 		return error;
1183b3a7093fSSepherosa Ziehau 	if (burst_max < MIN_IOPOLL_BURST_MAX)
1184b3a7093fSSepherosa Ziehau 		burst_max = MIN_IOPOLL_BURST_MAX;
1185b3a7093fSSepherosa Ziehau 	else if (burst_max > MAX_IOPOLL_BURST_MAX)
1186b3a7093fSSepherosa Ziehau 		burst_max = MAX_IOPOLL_BURST_MAX;
1187b3a7093fSSepherosa Ziehau 
1188002c1265SMatthew Dillon 	netmsg_init(&msg.base, NULL, &curthread->td_msgport,
1189c3c96e44SMatthew Dillon 		    0, sysctl_burstmax_handler);
1190002c1265SMatthew Dillon 	msg.base.lmsg.u.ms_result = burst_max;
1191b3a7093fSSepherosa Ziehau 	msg.ctx = io_ctx;
1192b3a7093fSSepherosa Ziehau 
1193e3bcf7f5SSepherosa Ziehau 	return netisr_domsg(&msg.base, io_ctx->poll_cpuid);
1194b3a7093fSSepherosa Ziehau }
1195b3a7093fSSepherosa Ziehau 
1196b3a7093fSSepherosa Ziehau static void
sysctl_eachburst_handler(netmsg_t nmsg)1197002c1265SMatthew Dillon sysctl_eachburst_handler(netmsg_t nmsg)
1198b3a7093fSSepherosa Ziehau {
1199b3a7093fSSepherosa Ziehau 	struct iopoll_sysctl_netmsg *msg = (struct iopoll_sysctl_netmsg *)nmsg;
1200b3a7093fSSepherosa Ziehau 	struct iopoll_ctx *io_ctx;
1201b3a7093fSSepherosa Ziehau 	uint32_t each_burst;
1202b3a7093fSSepherosa Ziehau 
1203b3a7093fSSepherosa Ziehau 	io_ctx = msg->ctx;
1204ec7f7fc8SSepherosa Ziehau 	KKASSERT(&curthread->td_msgport == netisr_cpuport(io_ctx->poll_cpuid));
1205b3a7093fSSepherosa Ziehau 
1206002c1265SMatthew Dillon 	each_burst = nmsg->lmsg.u.ms_result;
1207b3a7093fSSepherosa Ziehau 	if (each_burst > io_ctx->poll_burst_max)
1208b3a7093fSSepherosa Ziehau 		each_burst = io_ctx->poll_burst_max;
1209b3a7093fSSepherosa Ziehau 	else if (each_burst < 1)
1210b3a7093fSSepherosa Ziehau 		each_burst = 1;
1211b3a7093fSSepherosa Ziehau 	io_ctx->poll_each_burst = each_burst;
1212b3a7093fSSepherosa Ziehau 
1213e3bcf7f5SSepherosa Ziehau 	netisr_replymsg(&nmsg->base, 0);
1214b3a7093fSSepherosa Ziehau }
1215b3a7093fSSepherosa Ziehau 
1216b3a7093fSSepherosa Ziehau static int
sysctl_eachburst(SYSCTL_HANDLER_ARGS)1217b3a7093fSSepherosa Ziehau sysctl_eachburst(SYSCTL_HANDLER_ARGS)
1218b3a7093fSSepherosa Ziehau {
1219b3a7093fSSepherosa Ziehau 	struct iopoll_ctx *io_ctx = arg1;
1220b3a7093fSSepherosa Ziehau 	struct iopoll_sysctl_netmsg msg;
1221b3a7093fSSepherosa Ziehau 	uint32_t each_burst;
1222b3a7093fSSepherosa Ziehau 	int error;
1223b3a7093fSSepherosa Ziehau 
1224b3a7093fSSepherosa Ziehau 	each_burst = io_ctx->poll_each_burst;
1225b3a7093fSSepherosa Ziehau 	error = sysctl_handle_int(oidp, &each_burst, 0, req);
1226b3a7093fSSepherosa Ziehau 	if (error || req->newptr == NULL)
1227b3a7093fSSepherosa Ziehau 		return error;
1228b3a7093fSSepherosa Ziehau 
1229002c1265SMatthew Dillon 	netmsg_init(&msg.base, NULL, &curthread->td_msgport,
1230c3c96e44SMatthew Dillon 		    0, sysctl_eachburst_handler);
1231002c1265SMatthew Dillon 	msg.base.lmsg.u.ms_result = each_burst;
1232b3a7093fSSepherosa Ziehau 	msg.ctx = io_ctx;
1233b3a7093fSSepherosa Ziehau 
1234e3bcf7f5SSepherosa Ziehau 	return netisr_domsg(&msg.base, io_ctx->poll_cpuid);
1235b3a7093fSSepherosa Ziehau }
1236b3a7093fSSepherosa Ziehau 
1237b3a7093fSSepherosa Ziehau static int
iopoll_register(struct ifnet * ifp,struct iopoll_ctx * io_ctx,const struct ifpoll_io * io_rec)1238b3a7093fSSepherosa Ziehau iopoll_register(struct ifnet *ifp, struct iopoll_ctx *io_ctx,
1239b3a7093fSSepherosa Ziehau 		const struct ifpoll_io *io_rec)
1240b3a7093fSSepherosa Ziehau {
1241b3a7093fSSepherosa Ziehau 	int error;
1242b3a7093fSSepherosa Ziehau 
1243ec7f7fc8SSepherosa Ziehau 	KKASSERT(&curthread->td_msgport == netisr_cpuport(io_ctx->poll_cpuid));
1244b3a7093fSSepherosa Ziehau 
1245b3a7093fSSepherosa Ziehau 	if (io_rec->poll_func == NULL)
1246b3a7093fSSepherosa Ziehau 		return 0;
1247b3a7093fSSepherosa Ziehau 
1248b3a7093fSSepherosa Ziehau 	/*
1249b3a7093fSSepherosa Ziehau 	 * Check if there is room.
1250b3a7093fSSepherosa Ziehau 	 */
1251b3a7093fSSepherosa Ziehau 	if (io_ctx->poll_handlers >= IFPOLL_LIST_LEN) {
1252b3a7093fSSepherosa Ziehau 		/*
1253b3a7093fSSepherosa Ziehau 		 * List full, cannot register more entries.
1254b3a7093fSSepherosa Ziehau 		 * This should never happen; if it does, it is probably a
1255b3a7093fSSepherosa Ziehau 		 * broken driver trying to register multiple times. Checking
1256b3a7093fSSepherosa Ziehau 		 * this at runtime is expensive, and won't solve the problem
1257b3a7093fSSepherosa Ziehau 		 * anyways, so just report a few times and then give up.
1258b3a7093fSSepherosa Ziehau 		 */
1259b3a7093fSSepherosa Ziehau 		static int verbose = 10; /* XXX */
1260b3a7093fSSepherosa Ziehau 		if (verbose > 0) {
1261b3a7093fSSepherosa Ziehau 			kprintf("io poll handlers list full, "
1262b3a7093fSSepherosa Ziehau 				"maybe a broken driver ?\n");
1263b3a7093fSSepherosa Ziehau 			verbose--;
1264b3a7093fSSepherosa Ziehau 		}
1265b3a7093fSSepherosa Ziehau 		error = ENOENT;
1266b3a7093fSSepherosa Ziehau 	} else {
1267b3a7093fSSepherosa Ziehau 		struct iopoll_rec *rec = &io_ctx->pr[io_ctx->poll_handlers];
1268b3a7093fSSepherosa Ziehau 
1269b3a7093fSSepherosa Ziehau 		rec->ifp = ifp;
1270b3a7093fSSepherosa Ziehau 		rec->serializer = io_rec->serializer;
1271b3a7093fSSepherosa Ziehau 		rec->arg = io_rec->arg;
1272b3a7093fSSepherosa Ziehau 		rec->poll_func = io_rec->poll_func;
1273b3a7093fSSepherosa Ziehau 
1274b3a7093fSSepherosa Ziehau 		io_ctx->poll_handlers++;
1275b3a7093fSSepherosa Ziehau 		error = 0;
1276b3a7093fSSepherosa Ziehau 	}
1277b3a7093fSSepherosa Ziehau 	return error;
1278b3a7093fSSepherosa Ziehau }
1279b3a7093fSSepherosa Ziehau 
1280b3a7093fSSepherosa Ziehau static int
iopoll_deregister(struct ifnet * ifp,struct iopoll_ctx * io_ctx)1281b3a7093fSSepherosa Ziehau iopoll_deregister(struct ifnet *ifp, struct iopoll_ctx *io_ctx)
1282b3a7093fSSepherosa Ziehau {
1283b3a7093fSSepherosa Ziehau 	int i, error;
1284b3a7093fSSepherosa Ziehau 
1285ec7f7fc8SSepherosa Ziehau 	KKASSERT(&curthread->td_msgport == netisr_cpuport(io_ctx->poll_cpuid));
1286b3a7093fSSepherosa Ziehau 
1287b3a7093fSSepherosa Ziehau 	for (i = 0; i < io_ctx->poll_handlers; ++i) {
1288b3a7093fSSepherosa Ziehau 		if (io_ctx->pr[i].ifp == ifp) /* Found it */
1289b3a7093fSSepherosa Ziehau 			break;
1290b3a7093fSSepherosa Ziehau 	}
1291b3a7093fSSepherosa Ziehau 	if (i == io_ctx->poll_handlers) {
1292b3a7093fSSepherosa Ziehau 		error = ENOENT;
1293b3a7093fSSepherosa Ziehau 	} else {
1294b3a7093fSSepherosa Ziehau 		io_ctx->poll_handlers--;
1295b3a7093fSSepherosa Ziehau 		if (i < io_ctx->poll_handlers) {
1296b3a7093fSSepherosa Ziehau 			/* Last entry replaces this one. */
1297b3a7093fSSepherosa Ziehau 			io_ctx->pr[i] = io_ctx->pr[io_ctx->poll_handlers];
1298b3a7093fSSepherosa Ziehau 		}
1299b3a7093fSSepherosa Ziehau 
1300968f17f7SSepherosa Ziehau 		if (io_ctx->poll_handlers == 0)
1301b3a7093fSSepherosa Ziehau 			iopoll_reset_state(io_ctx);
1302b3a7093fSSepherosa Ziehau 		error = 0;
1303b3a7093fSSepherosa Ziehau 	}
1304b3a7093fSSepherosa Ziehau 	return error;
1305b3a7093fSSepherosa Ziehau }
1306968f17f7SSepherosa Ziehau 
1307968f17f7SSepherosa Ziehau static void
poll_comm_init(int cpuid)1308968f17f7SSepherosa Ziehau poll_comm_init(int cpuid)
1309968f17f7SSepherosa Ziehau {
1310968f17f7SSepherosa Ziehau 	struct poll_comm *comm;
1311968f17f7SSepherosa Ziehau 	char cpuid_str[16];
1312968f17f7SSepherosa Ziehau 
131383d8ec1eSSepherosa Ziehau 	comm = kmalloc(sizeof(*comm), M_DEVBUF, M_WAITOK | M_ZERO);
1314968f17f7SSepherosa Ziehau 
13150c7fdccdSSepherosa Ziehau 	if (ifpoll_stfrac < 1)
131626673976SSepherosa Ziehau 		ifpoll_stfrac = IFPOLL_STFRAC_DEFAULT;
13170c7fdccdSSepherosa Ziehau 	if (ifpoll_txfrac < 1)
131826673976SSepherosa Ziehau 		ifpoll_txfrac = IFPOLL_TXFRAC_DEFAULT;
131926673976SSepherosa Ziehau 
1320968f17f7SSepherosa Ziehau 	comm->poll_cpuid = cpuid;
1321d61a4c96SSepherosa Ziehau 	comm->pollhz = poll_comm_pollhz_div(comm, ifpoll_pollhz);
13220c7fdccdSSepherosa Ziehau 	comm->poll_stfrac = ifpoll_stfrac - 1;
13230c7fdccdSSepherosa Ziehau 	comm->poll_txfrac = ifpoll_txfrac - 1;
1324968f17f7SSepherosa Ziehau 
1325968f17f7SSepherosa Ziehau 	ksnprintf(cpuid_str, sizeof(cpuid_str), "%d", cpuid);
1326968f17f7SSepherosa Ziehau 
1327968f17f7SSepherosa Ziehau 	sysctl_ctx_init(&comm->sysctl_ctx);
1328968f17f7SSepherosa Ziehau 	comm->sysctl_tree = SYSCTL_ADD_NODE(&comm->sysctl_ctx,
1329968f17f7SSepherosa Ziehau 			    SYSCTL_STATIC_CHILDREN(_net_ifpoll),
1330968f17f7SSepherosa Ziehau 			    OID_AUTO, cpuid_str, CTLFLAG_RD, 0, "");
1331968f17f7SSepherosa Ziehau 
1332968f17f7SSepherosa Ziehau 	SYSCTL_ADD_PROC(&comm->sysctl_ctx, SYSCTL_CHILDREN(comm->sysctl_tree),
1333968f17f7SSepherosa Ziehau 			OID_AUTO, "pollhz", CTLTYPE_INT | CTLFLAG_RW,
1334968f17f7SSepherosa Ziehau 			comm, 0, sysctl_pollhz,
1335968f17f7SSepherosa Ziehau 			"I", "Device polling frequency");
1336968f17f7SSepherosa Ziehau 
133726673976SSepherosa Ziehau 	if (cpuid == 0) {
133826673976SSepherosa Ziehau 		SYSCTL_ADD_PROC(&comm->sysctl_ctx,
133926673976SSepherosa Ziehau 				SYSCTL_CHILDREN(comm->sysctl_tree),
134026673976SSepherosa Ziehau 				OID_AUTO, "status_frac",
134126673976SSepherosa Ziehau 				CTLTYPE_INT | CTLFLAG_RW,
134226673976SSepherosa Ziehau 				comm, 0, sysctl_stfrac,
134326673976SSepherosa Ziehau 				"I", "# of cycles before status is polled");
134426673976SSepherosa Ziehau 	}
134526673976SSepherosa Ziehau 	SYSCTL_ADD_PROC(&comm->sysctl_ctx, SYSCTL_CHILDREN(comm->sysctl_tree),
134626673976SSepherosa Ziehau 			OID_AUTO, "tx_frac", CTLTYPE_INT | CTLFLAG_RW,
134726673976SSepherosa Ziehau 			comm, 0, sysctl_txfrac,
134826673976SSepherosa Ziehau 			"I", "# of cycles before TX is polled");
134926673976SSepherosa Ziehau 
1350968f17f7SSepherosa Ziehau 	poll_common[cpuid] = comm;
1351968f17f7SSepherosa Ziehau }
1352968f17f7SSepherosa Ziehau 
1353968f17f7SSepherosa Ziehau static void
poll_comm_start(int cpuid)1354968f17f7SSepherosa Ziehau poll_comm_start(int cpuid)
1355968f17f7SSepherosa Ziehau {
1356968f17f7SSepherosa Ziehau 	struct poll_comm *comm = poll_common[cpuid];
135796d52ac8SSepherosa Ziehau 	systimer_func_t func;
1358968f17f7SSepherosa Ziehau 
1359968f17f7SSepherosa Ziehau 	/*
1360968f17f7SSepherosa Ziehau 	 * Initialize systimer
1361968f17f7SSepherosa Ziehau 	 */
1362968f17f7SSepherosa Ziehau 	if (cpuid == 0)
1363968f17f7SSepherosa Ziehau 		func = poll_comm_systimer0;
1364968f17f7SSepherosa Ziehau 	else
1365968f17f7SSepherosa Ziehau 		func = poll_comm_systimer;
1366968f17f7SSepherosa Ziehau 	systimer_init_periodic_nq(&comm->pollclock, func, comm, 1);
1367968f17f7SSepherosa Ziehau }
1368968f17f7SSepherosa Ziehau 
1369968f17f7SSepherosa Ziehau static void
_poll_comm_systimer(struct poll_comm * comm)1370968f17f7SSepherosa Ziehau _poll_comm_systimer(struct poll_comm *comm)
1371968f17f7SSepherosa Ziehau {
13725f497de9SSepherosa Ziehau 	iopoll_clock(rxpoll_context[comm->poll_cpuid]);
1373968f17f7SSepherosa Ziehau 	if (comm->txfrac_count-- == 0) {
1374968f17f7SSepherosa Ziehau 		comm->txfrac_count = comm->poll_txfrac;
1375968f17f7SSepherosa Ziehau 		iopoll_clock(txpoll_context[comm->poll_cpuid]);
1376968f17f7SSepherosa Ziehau 	}
1377968f17f7SSepherosa Ziehau }
1378968f17f7SSepherosa Ziehau 
1379968f17f7SSepherosa Ziehau static void
poll_comm_systimer0(systimer_t info,int in_ipi __unused,struct intrframe * frame __unused)138096d52ac8SSepherosa Ziehau poll_comm_systimer0(systimer_t info, int in_ipi __unused,
138196d52ac8SSepherosa Ziehau     struct intrframe *frame __unused)
1382968f17f7SSepherosa Ziehau {
1383968f17f7SSepherosa Ziehau 	struct poll_comm *comm = info->data;
1384968f17f7SSepherosa Ziehau 	globaldata_t gd = mycpu;
1385968f17f7SSepherosa Ziehau 
1386968f17f7SSepherosa Ziehau 	KKASSERT(comm->poll_cpuid == gd->gd_cpuid && gd->gd_cpuid == 0);
1387968f17f7SSepherosa Ziehau 
1388968f17f7SSepherosa Ziehau 	crit_enter_gd(gd);
1389968f17f7SSepherosa Ziehau 
1390968f17f7SSepherosa Ziehau 	if (comm->stfrac_count-- == 0) {
1391968f17f7SSepherosa Ziehau 		comm->stfrac_count = comm->poll_stfrac;
1392968f17f7SSepherosa Ziehau 		stpoll_clock(&stpoll_context);
1393968f17f7SSepherosa Ziehau 	}
1394968f17f7SSepherosa Ziehau 	_poll_comm_systimer(comm);
1395968f17f7SSepherosa Ziehau 
1396968f17f7SSepherosa Ziehau 	crit_exit_gd(gd);
1397968f17f7SSepherosa Ziehau }
1398968f17f7SSepherosa Ziehau 
1399968f17f7SSepherosa Ziehau static void
poll_comm_systimer(systimer_t info,int in_ipi __unused,struct intrframe * frame __unused)140096d52ac8SSepherosa Ziehau poll_comm_systimer(systimer_t info, int in_ipi __unused,
140196d52ac8SSepherosa Ziehau     struct intrframe *frame __unused)
1402968f17f7SSepherosa Ziehau {
1403968f17f7SSepherosa Ziehau 	struct poll_comm *comm = info->data;
1404968f17f7SSepherosa Ziehau 	globaldata_t gd = mycpu;
1405968f17f7SSepherosa Ziehau 
1406968f17f7SSepherosa Ziehau 	KKASSERT(comm->poll_cpuid == gd->gd_cpuid && gd->gd_cpuid != 0);
1407968f17f7SSepherosa Ziehau 
1408968f17f7SSepherosa Ziehau 	crit_enter_gd(gd);
1409968f17f7SSepherosa Ziehau 	_poll_comm_systimer(comm);
1410968f17f7SSepherosa Ziehau 	crit_exit_gd(gd);
1411968f17f7SSepherosa Ziehau }
1412968f17f7SSepherosa Ziehau 
1413968f17f7SSepherosa Ziehau static void
poll_comm_adjust_pollhz(struct poll_comm * comm)1414968f17f7SSepherosa Ziehau poll_comm_adjust_pollhz(struct poll_comm *comm)
1415968f17f7SSepherosa Ziehau {
1416968f17f7SSepherosa Ziehau 	uint32_t handlers;
1417968f17f7SSepherosa Ziehau 	int pollhz = 1;
1418968f17f7SSepherosa Ziehau 
1419ec7f7fc8SSepherosa Ziehau 	KKASSERT(&curthread->td_msgport == netisr_cpuport(comm->poll_cpuid));
1420968f17f7SSepherosa Ziehau 
1421968f17f7SSepherosa Ziehau 	/*
1422968f17f7SSepherosa Ziehau 	 * If there is no polling handler registered, set systimer
1423968f17f7SSepherosa Ziehau 	 * frequency to the lowest value.  Polling systimer frequency
1424968f17f7SSepherosa Ziehau 	 * will be adjusted to the requested value, once there are
1425968f17f7SSepherosa Ziehau 	 * registered handlers.
1426968f17f7SSepherosa Ziehau 	 */
1427968f17f7SSepherosa Ziehau 	handlers = rxpoll_context[mycpuid]->poll_handlers +
1428968f17f7SSepherosa Ziehau 		   txpoll_context[mycpuid]->poll_handlers;
1429968f17f7SSepherosa Ziehau 	if (comm->poll_cpuid == 0)
1430968f17f7SSepherosa Ziehau 		handlers += stpoll_context.poll_handlers;
1431968f17f7SSepherosa Ziehau 	if (handlers)
1432968f17f7SSepherosa Ziehau 		pollhz = comm->pollhz;
1433968f17f7SSepherosa Ziehau 	systimer_adjust_periodic(&comm->pollclock, pollhz);
1434968f17f7SSepherosa Ziehau }
1435968f17f7SSepherosa Ziehau 
1436968f17f7SSepherosa Ziehau static int
sysctl_pollhz(SYSCTL_HANDLER_ARGS)1437968f17f7SSepherosa Ziehau sysctl_pollhz(SYSCTL_HANDLER_ARGS)
1438968f17f7SSepherosa Ziehau {
1439968f17f7SSepherosa Ziehau 	struct poll_comm *comm = arg1;
1440002c1265SMatthew Dillon 	struct netmsg_base nmsg;
1441968f17f7SSepherosa Ziehau 	int error, phz;
1442968f17f7SSepherosa Ziehau 
1443d61a4c96SSepherosa Ziehau 	phz = poll_comm_pollhz_conv(comm, comm->pollhz);
1444968f17f7SSepherosa Ziehau 	error = sysctl_handle_int(oidp, &phz, 0, req);
1445968f17f7SSepherosa Ziehau 	if (error || req->newptr == NULL)
1446968f17f7SSepherosa Ziehau 		return error;
1447968f17f7SSepherosa Ziehau 	if (phz <= 0)
1448968f17f7SSepherosa Ziehau 		return EINVAL;
1449968f17f7SSepherosa Ziehau 	else if (phz > IFPOLL_FREQ_MAX)
1450968f17f7SSepherosa Ziehau 		phz = IFPOLL_FREQ_MAX;
1451968f17f7SSepherosa Ziehau 
145248e7b118SMatthew Dillon 	netmsg_init(&nmsg, NULL, &curthread->td_msgport,
1453c3c96e44SMatthew Dillon 		    0, sysctl_pollhz_handler);
1454002c1265SMatthew Dillon 	nmsg.lmsg.u.ms_result = phz;
1455968f17f7SSepherosa Ziehau 
1456e3bcf7f5SSepherosa Ziehau 	return netisr_domsg(&nmsg, comm->poll_cpuid);
1457968f17f7SSepherosa Ziehau }
1458968f17f7SSepherosa Ziehau 
1459968f17f7SSepherosa Ziehau static void
sysctl_pollhz_handler(netmsg_t nmsg)1460002c1265SMatthew Dillon sysctl_pollhz_handler(netmsg_t nmsg)
1461968f17f7SSepherosa Ziehau {
1462968f17f7SSepherosa Ziehau 	struct poll_comm *comm = poll_common[mycpuid];
1463968f17f7SSepherosa Ziehau 
1464ec7f7fc8SSepherosa Ziehau 	KKASSERT(&curthread->td_msgport == netisr_cpuport(comm->poll_cpuid));
1465968f17f7SSepherosa Ziehau 
1466968f17f7SSepherosa Ziehau 	/* Save polling frequency */
1467d61a4c96SSepherosa Ziehau 	comm->pollhz = poll_comm_pollhz_div(comm, nmsg->lmsg.u.ms_result);
1468968f17f7SSepherosa Ziehau 
1469968f17f7SSepherosa Ziehau 	/*
1470968f17f7SSepherosa Ziehau 	 * Adjust cached pollhz
1471968f17f7SSepherosa Ziehau 	 */
1472968f17f7SSepherosa Ziehau 	rxpoll_context[mycpuid]->pollhz = comm->pollhz;
1473968f17f7SSepherosa Ziehau 	txpoll_context[mycpuid]->pollhz =
1474968f17f7SSepherosa Ziehau 	    comm->pollhz / (comm->poll_txfrac + 1);
1475968f17f7SSepherosa Ziehau 
1476968f17f7SSepherosa Ziehau 	/*
1477968f17f7SSepherosa Ziehau 	 * Adjust polling frequency
1478968f17f7SSepherosa Ziehau 	 */
1479968f17f7SSepherosa Ziehau 	poll_comm_adjust_pollhz(comm);
1480968f17f7SSepherosa Ziehau 
1481e3bcf7f5SSepherosa Ziehau 	netisr_replymsg(&nmsg->base, 0);
1482968f17f7SSepherosa Ziehau }
148326673976SSepherosa Ziehau 
148426673976SSepherosa Ziehau static int
sysctl_stfrac(SYSCTL_HANDLER_ARGS)148526673976SSepherosa Ziehau sysctl_stfrac(SYSCTL_HANDLER_ARGS)
148626673976SSepherosa Ziehau {
148726673976SSepherosa Ziehau 	struct poll_comm *comm = arg1;
1488002c1265SMatthew Dillon 	struct netmsg_base nmsg;
148926673976SSepherosa Ziehau 	int error, stfrac;
149026673976SSepherosa Ziehau 
149126673976SSepherosa Ziehau 	KKASSERT(comm->poll_cpuid == 0);
149226673976SSepherosa Ziehau 
14930c7fdccdSSepherosa Ziehau 	stfrac = comm->poll_stfrac + 1;
149426673976SSepherosa Ziehau 	error = sysctl_handle_int(oidp, &stfrac, 0, req);
149526673976SSepherosa Ziehau 	if (error || req->newptr == NULL)
149626673976SSepherosa Ziehau 		return error;
14970c7fdccdSSepherosa Ziehau 	if (stfrac < 1)
149826673976SSepherosa Ziehau 		return EINVAL;
149926673976SSepherosa Ziehau 
150048e7b118SMatthew Dillon 	netmsg_init(&nmsg, NULL, &curthread->td_msgport,
1501c3c96e44SMatthew Dillon 		    0, sysctl_stfrac_handler);
15020c7fdccdSSepherosa Ziehau 	nmsg.lmsg.u.ms_result = stfrac - 1;
150326673976SSepherosa Ziehau 
1504e3bcf7f5SSepherosa Ziehau 	return netisr_domsg(&nmsg, comm->poll_cpuid);
150526673976SSepherosa Ziehau }
150626673976SSepherosa Ziehau 
150726673976SSepherosa Ziehau static void
sysctl_stfrac_handler(netmsg_t nmsg)1508002c1265SMatthew Dillon sysctl_stfrac_handler(netmsg_t nmsg)
150926673976SSepherosa Ziehau {
151026673976SSepherosa Ziehau 	struct poll_comm *comm = poll_common[mycpuid];
1511002c1265SMatthew Dillon 	int stfrac = nmsg->lmsg.u.ms_result;
151226673976SSepherosa Ziehau 
1513ec7f7fc8SSepherosa Ziehau 	KKASSERT(&curthread->td_msgport == netisr_cpuport(comm->poll_cpuid));
151426673976SSepherosa Ziehau 
151526673976SSepherosa Ziehau 	crit_enter();
151626673976SSepherosa Ziehau 	comm->poll_stfrac = stfrac;
151726673976SSepherosa Ziehau 	if (comm->stfrac_count > comm->poll_stfrac)
151826673976SSepherosa Ziehau 		comm->stfrac_count = comm->poll_stfrac;
151926673976SSepherosa Ziehau 	crit_exit();
152026673976SSepherosa Ziehau 
1521e3bcf7f5SSepherosa Ziehau 	netisr_replymsg(&nmsg->base, 0);
152226673976SSepherosa Ziehau }
152326673976SSepherosa Ziehau 
152426673976SSepherosa Ziehau static int
sysctl_txfrac(SYSCTL_HANDLER_ARGS)152526673976SSepherosa Ziehau sysctl_txfrac(SYSCTL_HANDLER_ARGS)
152626673976SSepherosa Ziehau {
152726673976SSepherosa Ziehau 	struct poll_comm *comm = arg1;
1528002c1265SMatthew Dillon 	struct netmsg_base nmsg;
152926673976SSepherosa Ziehau 	int error, txfrac;
153026673976SSepherosa Ziehau 
15310c7fdccdSSepherosa Ziehau 	txfrac = comm->poll_txfrac + 1;
153226673976SSepherosa Ziehau 	error = sysctl_handle_int(oidp, &txfrac, 0, req);
153326673976SSepherosa Ziehau 	if (error || req->newptr == NULL)
153426673976SSepherosa Ziehau 		return error;
15350c7fdccdSSepherosa Ziehau 	if (txfrac < 1)
153626673976SSepherosa Ziehau 		return EINVAL;
153726673976SSepherosa Ziehau 
153848e7b118SMatthew Dillon 	netmsg_init(&nmsg, NULL, &curthread->td_msgport,
1539c3c96e44SMatthew Dillon 		    0, sysctl_txfrac_handler);
15400c7fdccdSSepherosa Ziehau 	nmsg.lmsg.u.ms_result = txfrac - 1;
154126673976SSepherosa Ziehau 
1542e3bcf7f5SSepherosa Ziehau 	return netisr_domsg(&nmsg, comm->poll_cpuid);
154326673976SSepherosa Ziehau }
154426673976SSepherosa Ziehau 
154526673976SSepherosa Ziehau static void
sysctl_txfrac_handler(netmsg_t nmsg)1546002c1265SMatthew Dillon sysctl_txfrac_handler(netmsg_t nmsg)
154726673976SSepherosa Ziehau {
154826673976SSepherosa Ziehau 	struct poll_comm *comm = poll_common[mycpuid];
1549002c1265SMatthew Dillon 	int txfrac = nmsg->lmsg.u.ms_result;
155026673976SSepherosa Ziehau 
1551ec7f7fc8SSepherosa Ziehau 	KKASSERT(&curthread->td_msgport == netisr_cpuport(comm->poll_cpuid));
155226673976SSepherosa Ziehau 
155326673976SSepherosa Ziehau 	crit_enter();
155426673976SSepherosa Ziehau 	comm->poll_txfrac = txfrac;
155526673976SSepherosa Ziehau 	if (comm->txfrac_count > comm->poll_txfrac)
155626673976SSepherosa Ziehau 		comm->txfrac_count = comm->poll_txfrac;
155726673976SSepherosa Ziehau 	crit_exit();
155826673976SSepherosa Ziehau 
1559e3bcf7f5SSepherosa Ziehau 	netisr_replymsg(&nmsg->base, 0);
156026673976SSepherosa Ziehau }
1561b5de76b1SSepherosa Ziehau 
1562b5de76b1SSepherosa Ziehau void
ifpoll_compat_setup(struct ifpoll_compat * cp,struct sysctl_ctx_list * sysctl_ctx,struct sysctl_oid * sysctl_tree,int unit,struct lwkt_serialize * slz)1563b5de76b1SSepherosa Ziehau ifpoll_compat_setup(struct ifpoll_compat *cp,
1564b5de76b1SSepherosa Ziehau     struct sysctl_ctx_list *sysctl_ctx,
1565b5de76b1SSepherosa Ziehau     struct sysctl_oid *sysctl_tree,
1566b5de76b1SSepherosa Ziehau     int unit, struct lwkt_serialize *slz)
1567b5de76b1SSepherosa Ziehau {
1568b5de76b1SSepherosa Ziehau 	cp->ifpc_stcount = 0;
15693567bc9bSSepherosa Ziehau 	cp->ifpc_stfrac = ((poll_common[0]->poll_stfrac + 1) *
15703567bc9bSSepherosa Ziehau 	    howmany(IOPOLL_BURST_MAX, IOPOLL_EACH_BURST)) - 1;
1571b5de76b1SSepherosa Ziehau 
1572d1958e12SSepherosa Ziehau 	cp->ifpc_cpuid = unit % netisr_ncpus;
1573b5de76b1SSepherosa Ziehau 	cp->ifpc_serializer = slz;
1574b5de76b1SSepherosa Ziehau 
15755d35ae74SSepherosa Ziehau 	if (sysctl_ctx != NULL && sysctl_tree != NULL) {
1576b5de76b1SSepherosa Ziehau 		SYSCTL_ADD_PROC(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree),
1577b5de76b1SSepherosa Ziehau 		    OID_AUTO, "npoll_stfrac", CTLTYPE_INT | CTLFLAG_RW,
1578b5de76b1SSepherosa Ziehau 		    cp, 0, sysctl_compat_npoll_stfrac, "I",
1579b5de76b1SSepherosa Ziehau 		    "polling status frac");
1580b5de76b1SSepherosa Ziehau 		SYSCTL_ADD_PROC(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree),
1581b5de76b1SSepherosa Ziehau 		    OID_AUTO, "npoll_cpuid", CTLTYPE_INT | CTLFLAG_RW,
1582b5de76b1SSepherosa Ziehau 		    cp, 0, sysctl_compat_npoll_cpuid, "I",
1583b5de76b1SSepherosa Ziehau 		    "polling cpuid");
1584b5de76b1SSepherosa Ziehau 	}
1585b5de76b1SSepherosa Ziehau }
1586b5de76b1SSepherosa Ziehau 
1587b5de76b1SSepherosa Ziehau static int
sysctl_compat_npoll_stfrac(SYSCTL_HANDLER_ARGS)1588b5de76b1SSepherosa Ziehau sysctl_compat_npoll_stfrac(SYSCTL_HANDLER_ARGS)
1589b5de76b1SSepherosa Ziehau {
1590b5de76b1SSepherosa Ziehau 	struct ifpoll_compat *cp = arg1;
1591b5de76b1SSepherosa Ziehau 	int error = 0, stfrac;
1592b5de76b1SSepherosa Ziehau 
1593b5de76b1SSepherosa Ziehau 	lwkt_serialize_enter(cp->ifpc_serializer);
1594b5de76b1SSepherosa Ziehau 
1595b5de76b1SSepherosa Ziehau 	stfrac = cp->ifpc_stfrac + 1;
1596b5de76b1SSepherosa Ziehau 	error = sysctl_handle_int(oidp, &stfrac, 0, req);
1597b5de76b1SSepherosa Ziehau 	if (!error && req->newptr != NULL) {
1598b5de76b1SSepherosa Ziehau 		if (stfrac < 1) {
1599b5de76b1SSepherosa Ziehau 			error = EINVAL;
1600b5de76b1SSepherosa Ziehau 		} else {
1601b5de76b1SSepherosa Ziehau 			cp->ifpc_stfrac = stfrac - 1;
1602b5de76b1SSepherosa Ziehau 			if (cp->ifpc_stcount > cp->ifpc_stfrac)
1603b5de76b1SSepherosa Ziehau 				cp->ifpc_stcount = cp->ifpc_stfrac;
1604b5de76b1SSepherosa Ziehau 		}
1605b5de76b1SSepherosa Ziehau 	}
1606b5de76b1SSepherosa Ziehau 
1607b5de76b1SSepherosa Ziehau 	lwkt_serialize_exit(cp->ifpc_serializer);
1608b5de76b1SSepherosa Ziehau 	return error;
1609b5de76b1SSepherosa Ziehau }
1610b5de76b1SSepherosa Ziehau 
1611b5de76b1SSepherosa Ziehau static int
sysctl_compat_npoll_cpuid(SYSCTL_HANDLER_ARGS)1612b5de76b1SSepherosa Ziehau sysctl_compat_npoll_cpuid(SYSCTL_HANDLER_ARGS)
1613b5de76b1SSepherosa Ziehau {
1614b5de76b1SSepherosa Ziehau 	struct ifpoll_compat *cp = arg1;
1615b5de76b1SSepherosa Ziehau 	int error = 0, cpuid;
1616b5de76b1SSepherosa Ziehau 
1617b5de76b1SSepherosa Ziehau 	lwkt_serialize_enter(cp->ifpc_serializer);
1618b5de76b1SSepherosa Ziehau 
1619b5de76b1SSepherosa Ziehau 	cpuid = cp->ifpc_cpuid;
1620b5de76b1SSepherosa Ziehau 	error = sysctl_handle_int(oidp, &cpuid, 0, req);
1621b5de76b1SSepherosa Ziehau 	if (!error && req->newptr != NULL) {
1622d1958e12SSepherosa Ziehau 		if (cpuid < 0 || cpuid >= netisr_ncpus)
1623b5de76b1SSepherosa Ziehau 			error = EINVAL;
1624b5de76b1SSepherosa Ziehau 		else
1625b5de76b1SSepherosa Ziehau 			cp->ifpc_cpuid = cpuid;
1626b5de76b1SSepherosa Ziehau 	}
1627b5de76b1SSepherosa Ziehau 
1628b5de76b1SSepherosa Ziehau 	lwkt_serialize_exit(cp->ifpc_serializer);
1629b5de76b1SSepherosa Ziehau 	return error;
1630b5de76b1SSepherosa Ziehau }
1631