Merge from vendor branch LIBARCHIVE:
[dragonfly.git] / sys / kern / kern_poll.c
1 /*-
2  * Copyright (c) 2001-2002 Luigi Rizzo
3  *
4  * Supported by: the Xorp Project (www.xorp.org)
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  *
27  * $FreeBSD: src/sys/kern/kern_poll.c,v 1.2.2.4 2002/06/27 23:26:33 luigi Exp $
28  * $DragonFly: src/sys/kern/kern_poll.c,v 1.33 2007/09/12 12:02:09 sephe Exp $
29  */
30
31 #include "opt_polling.h"
32
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/kernel.h>
36 #include <sys/socket.h>                 /* needed by net/if.h           */
37 #include <sys/sysctl.h>
38
39 #include <sys/thread2.h>
40 #include <sys/msgport2.h>
41
42 #include <net/if.h>                     /* for IFF_* flags              */
43 #include <net/netisr.h>                 /* for NETISR_POLL              */
44
45 /*
46  * Polling support for [network] device drivers.
47  *
48  * Drivers which support this feature try to register with the
49  * polling code.
50  *
51  * If registration is successful, the driver must disable interrupts,
52  * and further I/O is performed through the handler, which is invoked
53  * (at least once per clock tick) with 3 arguments: the "arg" passed at
54  * register time (a struct ifnet pointer), a command, and a "count" limit.
55  *
56  * The command can be one of the following:
57  *  POLL_ONLY: quick move of "count" packets from input/output queues.
58  *  POLL_AND_CHECK_STATUS: as above, plus check status registers or do
59  *      other more expensive operations. This command is issued periodically
60  *      but less frequently than POLL_ONLY.
61  *  POLL_DEREGISTER: deregister and return to interrupt mode.
62  *  POLL_REGISTER: register and disable interrupts
63  *
64  * The first two commands are only issued if the interface is marked as
65  * 'IFF_UP, IFF_RUNNING and IFF_POLLING', the last two only if IFF_RUNNING
66  * is set.
67  *
68  * The count limit specifies how much work the handler can do during the
69  * call -- typically this is the number of packets to be received, or
70  * transmitted, etc. (drivers are free to interpret this number, as long
71  * as the max time spent in the function grows roughly linearly with the
72  * count).
73  *
74  * Deregistration can be requested by the driver itself (typically in the
75  * *_stop() routine), or by the polling code, by invoking the handler.
76  *
77  * Polling can be globally enabled or disabled with the sysctl variable
78  * kern.polling.enable (default is 0, disabled)
79  *
80  * A second variable controls the sharing of CPU between polling/kernel
81  * network processing, and other activities (typically userlevel tasks):
82  * kern.polling.user_frac (between 0 and 100, default 50) sets the share
83  * of CPU allocated to user tasks. CPU is allocated proportionally to the
84  * shares, by dynamically adjusting the "count" (poll_burst).
85  *
86  * Other parameters can should be left to their default values.
87  * The following constraints hold
88  *
89  *      1 <= poll_each_burst <= poll_burst <= poll_burst_max
90  *      MIN_POLL_BURST_MAX <= poll_burst_max <= MAX_POLL_BURST_MAX
91  */
92
93 #define MIN_POLL_BURST_MAX      10
94 #define MAX_POLL_BURST_MAX      1000
95
96 #ifndef DEVICE_POLLING_FREQ_MAX
97 #define DEVICE_POLLING_FREQ_MAX         30000
98 #endif
99 #define DEVICE_POLLING_FREQ_DEFAULT     2000
100
101 #define POLL_LIST_LEN  128
102 struct pollrec {
103         struct ifnet    *ifp;
104 };
105
106 #define POLLCTX_MAX     32
107
108 struct pollctx {
109         struct sysctl_ctx_list  poll_sysctl_ctx;
110         struct sysctl_oid       *poll_sysctl_tree;
111
112         uint32_t                poll_burst;
113         uint32_t                poll_each_burst;
114         uint32_t                poll_burst_max;
115         uint32_t                user_frac;
116         int                     reg_frac_count;
117         uint32_t                reg_frac;
118         uint32_t                short_ticks;
119         uint32_t                lost_polls;
120         uint32_t                pending_polls;
121         int                     residual_burst;
122         uint32_t                phase;
123         uint32_t                suspect;
124         uint32_t                stalled;
125         struct timeval          poll_start_t;
126         struct timeval          prev_t;
127
128         uint32_t                poll_handlers; /* next free entry in pr[]. */
129         struct pollrec          pr[POLL_LIST_LEN];
130
131         int                     poll_cpuid;
132         struct systimer         pollclock;
133         int                     polling_enabled;
134         int                     pollhz;
135 };
136
137 static struct pollctx   *poll_context[POLLCTX_MAX];
138
139 SYSCTL_NODE(_kern, OID_AUTO, polling, CTLFLAG_RW, 0,
140         "Device polling parameters");
141
142 static int      poll_defcpu = -1;
143 SYSCTL_INT(_kern_polling, OID_AUTO, defcpu, CTLFLAG_RD,
144         &poll_defcpu, 0, "default CPU# to run device polling");
145
146 static uint32_t poll_cpumask = 0x1;
147 #ifdef notyet
148 TUNABLE_INT("kern.polling.cpumask", &poll_cpumask);
149 #endif
150
151 static int      polling_enabled = 0;    /* global polling enable */
152 TUNABLE_INT("kern.polling.enable", &polling_enabled);
153
154 static int      pollhz = DEVICE_POLLING_FREQ_DEFAULT;
155 TUNABLE_INT("kern.polling.pollhz", &pollhz);
156
157 /* The two netisr handlers */
158 static void     netisr_poll(struct netmsg *);
159 static void     netisr_pollmore(struct netmsg *);
160
161 /* Systimer handler */
162 static void     pollclock(systimer_t, struct intrframe *);
163
164 /* Sysctl handlers */
165 static int      sysctl_pollhz(SYSCTL_HANDLER_ARGS);
166 static int      sysctl_polling(SYSCTL_HANDLER_ARGS);
167 static void     poll_add_sysctl(struct sysctl_ctx_list *,
168                                 struct sysctl_oid_list *, struct pollctx *);
169
170 void            init_device_poll(void);         /* init routine */
171 void            init_device_poll_pcpu(int);     /* per-cpu init routine */
172
173 /*
174  * register relevant netisr. Called from kern_clock.c:
175  */
176 void
177 init_device_poll(void)
178 {
179         netisr_register(NETISR_POLL, cpu0_portfn, netisr_poll);
180         netisr_register(NETISR_POLLMORE, cpu0_portfn, netisr_pollmore);
181 }
182
183 void
184 init_device_poll_pcpu(int cpuid)
185 {
186         struct pollctx *pctx;
187         char cpuid_str[3];
188
189         if (((1 << cpuid) & poll_cpumask) == 0)
190                 return;
191
192         pctx = kmalloc(sizeof(*pctx), M_DEVBUF, M_WAITOK | M_ZERO);
193
194         pctx->poll_burst = 5;
195         pctx->poll_each_burst = 5;
196         pctx->poll_burst_max = 150; /* good for 100Mbit net and HZ=1000 */
197         pctx->user_frac = 50;
198         pctx->reg_frac = 20;
199         pctx->polling_enabled = polling_enabled;
200         pctx->pollhz = pollhz;
201         pctx->poll_cpuid = cpuid;
202
203         KASSERT(cpuid < POLLCTX_MAX, ("cpu id must < %d", cpuid));
204         poll_context[cpuid] = pctx;
205
206         if (poll_defcpu < 0) {
207                 poll_defcpu = cpuid;
208
209                 /*
210                  * Initialize global sysctl nodes, for compat
211                  */
212                 poll_add_sysctl(NULL, SYSCTL_STATIC_CHILDREN(_kern_polling),
213                                 pctx);
214         }
215
216         /*
217          * Initialize per-cpu sysctl nodes
218          */
219         ksnprintf(cpuid_str, sizeof(cpuid_str), "%d", pctx->poll_cpuid);
220
221         sysctl_ctx_init(&pctx->poll_sysctl_ctx);
222         pctx->poll_sysctl_tree = SYSCTL_ADD_NODE(&pctx->poll_sysctl_ctx,
223                                  SYSCTL_STATIC_CHILDREN(_kern_polling),
224                                  OID_AUTO, cpuid_str, CTLFLAG_RD, 0, "");
225         poll_add_sysctl(&pctx->poll_sysctl_ctx,
226                         SYSCTL_CHILDREN(pctx->poll_sysctl_tree), pctx);
227
228         /*
229          * Initialize systimer
230          */
231         systimer_init_periodic_nq(&pctx->pollclock, pollclock, pctx,
232                                   pctx->polling_enabled ? pctx->pollhz : 1);
233 }
234
235 /*
236  * Set the polling frequency
237  */
238 static int
239 sysctl_pollhz(SYSCTL_HANDLER_ARGS)
240 {
241         struct pollctx *pctx = arg1;
242         int error, phz;
243
244         phz = pctx->pollhz;
245         error = sysctl_handle_int(oidp, &phz, 0, req);
246         if (error || req->newptr == NULL)
247                 return error;
248         if (phz <= 0)
249                 return EINVAL;
250         else if (phz > DEVICE_POLLING_FREQ_MAX)
251                 phz = DEVICE_POLLING_FREQ_MAX;
252
253         crit_enter();
254         pctx->pollhz = phz;
255         if (pctx->polling_enabled)
256                 systimer_adjust_periodic(&pctx->pollclock, phz);
257         crit_exit();
258         return 0;
259 }
260
261 /*
262  * Master enable.  If polling is disabled, cut the polling systimer 
263  * frequency to 1hz.
264  */
265 static int
266 sysctl_polling(SYSCTL_HANDLER_ARGS)
267 {
268         struct pollctx *pctx = arg1;
269         int error, enabled;
270
271         enabled = pctx->polling_enabled;
272         error = sysctl_handle_int(oidp, &enabled, 0, req);
273         if (error || req->newptr == NULL)
274                 return error;
275
276         crit_enter();
277         pctx->polling_enabled = enabled;
278         if (pctx->polling_enabled)
279                 systimer_adjust_periodic(&pctx->pollclock, pollhz);
280         else
281                 systimer_adjust_periodic(&pctx->pollclock, 1);
282         crit_exit();
283         return 0;
284 }
285
286 /*
287  * Hook from hardclock. Tries to schedule a netisr, but keeps track
288  * of lost ticks due to the previous handler taking too long.
289  * Normally, this should not happen, because polling handler should
290  * run for a short time. However, in some cases (e.g. when there are
291  * changes in link status etc.) the drivers take a very long time
292  * (even in the order of milliseconds) to reset and reconfigure the
293  * device, causing apparent lost polls.
294  *
295  * The first part of the code is just for debugging purposes, and tries
296  * to count how often hardclock ticks are shorter than they should,
297  * meaning either stray interrupts or delayed events.
298  *
299  * WARNING! called from fastint or IPI, the MP lock might not be held.
300  */
301 static void
302 pollclock(systimer_t info, struct intrframe *frame __unused)
303 {
304         struct pollctx *pctx = info->data;
305         struct timeval t;
306         int delta;
307
308         if (pctx->poll_handlers == 0)
309                 return;
310
311         microuptime(&t);
312         delta = (t.tv_usec - pctx->prev_t.tv_usec) +
313                 (t.tv_sec - pctx->prev_t.tv_sec)*1000000;
314         if (delta * hz < 500000)
315                 pctx->short_ticks++;
316         else
317                 pctx->prev_t = t;
318
319         if (pctx->pending_polls > 100) {
320                 /*
321                  * Too much, assume it has stalled (not always true
322                  * see comment above).
323                  */
324                 pctx->stalled++;
325                 pctx->pending_polls = 0;
326                 pctx->phase = 0;
327         }
328
329         if (pctx->phase <= 2) {
330                 if (pctx->phase != 0)
331                         pctx->suspect++;
332                 pctx->phase = 1;
333                 schednetisr(NETISR_POLL);
334                 pctx->phase = 2;
335         }
336         if (pctx->pending_polls++ > 0)
337                 pctx->lost_polls++;
338 }
339
340 /*
341  * netisr_pollmore is called after other netisr's, possibly scheduling
342  * another NETISR_POLL call, or adapting the burst size for the next cycle.
343  *
344  * It is very bad to fetch large bursts of packets from a single card at once,
345  * because the burst could take a long time to be completely processed, or
346  * could saturate the intermediate queue (ipintrq or similar) leading to
347  * losses or unfairness. To reduce the problem, and also to account better for
348  * time spent in network-related processing, we split the burst in smaller
349  * chunks of fixed size, giving control to the other netisr's between chunks.
350  * This helps in improving the fairness, reducing livelock (because we
351  * emulate more closely the "process to completion" that we have with
352  * fastforwarding) and accounting for the work performed in low level
353  * handling and forwarding.
354  */
355
356 /* ARGSUSED */
357 static void
358 netisr_pollmore(struct netmsg *msg)
359 {
360         struct pollctx *pctx;
361         struct timeval t;
362         int kern_load, cpuid;
363
364         cpuid = mycpu->gd_cpuid;
365         KKASSERT(cpuid < POLLCTX_MAX);
366
367         pctx = poll_context[cpuid];
368         KKASSERT(pctx != NULL);
369         KKASSERT(pctx->poll_cpuid == cpuid);
370
371         crit_enter();
372         lwkt_replymsg(&msg->nm_lmsg, 0);
373         pctx->phase = 5;
374         if (pctx->residual_burst > 0) {
375                 schednetisr(NETISR_POLL);
376                 /* will run immediately on return, followed by netisrs */
377                 goto out;
378         }
379         /* here we can account time spent in netisr's in this tick */
380         microuptime(&t);
381         kern_load = (t.tv_usec - pctx->poll_start_t.tv_usec) +
382                 (t.tv_sec - pctx->poll_start_t.tv_sec)*1000000; /* us */
383         kern_load = (kern_load * hz) / 10000;                   /* 0..100 */
384         if (kern_load > (100 - pctx->user_frac)) { /* try decrease ticks */
385                 if (pctx->poll_burst > 1)
386                         pctx->poll_burst--;
387         } else {
388                 if (pctx->poll_burst < pctx->poll_burst_max)
389                         pctx->poll_burst++;
390         }
391
392         pctx->pending_polls--;
393         if (pctx->pending_polls == 0) { /* we are done */
394                 pctx->phase = 0;
395         } else {
396                 /*
397                  * Last cycle was long and caused us to miss one or more
398                  * hardclock ticks. Restart processing again, but slightly
399                  * reduce the burst size to prevent that this happens again.
400                  */
401                 pctx->poll_burst -= (pctx->poll_burst / 8);
402                 if (pctx->poll_burst < 1)
403                         pctx->poll_burst = 1;
404                 schednetisr(NETISR_POLL);
405                 pctx->phase = 6;
406         }
407 out:
408         crit_exit();
409 }
410
411 /*
412  * netisr_poll is scheduled by schednetisr when appropriate, typically once
413  * per tick.
414  *
415  * Note that the message is replied immediately in order to allow a new
416  * ISR to be scheduled in the handler.
417  *
418  * XXX each registration should indicate whether it needs a critical
419  * section to operate.
420  */
421 /* ARGSUSED */
422 static void
423 netisr_poll(struct netmsg *msg)
424 {
425         struct pollctx *pctx;
426         int i, cycles, cpuid;
427         enum poll_cmd arg = POLL_ONLY;
428
429         cpuid = mycpu->gd_cpuid;
430         KKASSERT(cpuid < POLLCTX_MAX);
431
432         pctx = poll_context[cpuid];
433         KKASSERT(pctx != NULL);
434         KKASSERT(pctx->poll_cpuid == cpuid);
435
436         lwkt_replymsg(&msg->nm_lmsg, 0);
437         crit_enter();
438         pctx->phase = 3;
439         if (pctx->residual_burst == 0) { /* first call in this tick */
440                 microuptime(&pctx->poll_start_t);
441                 /*
442                  * Check that paremeters are consistent with runtime
443                  * variables. Some of these tests could be done at sysctl
444                  * time, but the savings would be very limited because we
445                  * still have to check against reg_frac_count and
446                  * poll_each_burst. So, instead of writing separate sysctl
447                  * handlers, we do all here.
448                  */
449
450                 if (pctx->reg_frac > hz)
451                         pctx->reg_frac = hz;
452                 else if (pctx->reg_frac < 1)
453                         pctx->reg_frac = 1;
454                 if (pctx->reg_frac_count > pctx->reg_frac)
455                         pctx->reg_frac_count = pctx->reg_frac - 1;
456                 if (pctx->reg_frac_count-- == 0) {
457                         arg = POLL_AND_CHECK_STATUS;
458                         pctx->reg_frac_count = pctx->reg_frac - 1;
459                 }
460                 if (pctx->poll_burst_max < MIN_POLL_BURST_MAX)
461                         pctx->poll_burst_max = MIN_POLL_BURST_MAX;
462                 else if (pctx->poll_burst_max > MAX_POLL_BURST_MAX)
463                         pctx->poll_burst_max = MAX_POLL_BURST_MAX;
464
465                 if (pctx->poll_each_burst < 1)
466                         pctx->poll_each_burst = 1;
467                 else if (pctx->poll_each_burst > pctx->poll_burst_max)
468                         pctx->poll_each_burst = pctx->poll_burst_max;
469
470                 pctx->residual_burst = pctx->poll_burst;
471         }
472         cycles = (pctx->residual_burst < pctx->poll_each_burst) ?
473                 pctx->residual_burst : pctx->poll_each_burst;
474         pctx->residual_burst -= cycles;
475
476         if (pctx->polling_enabled) {
477                 for (i = 0 ; i < pctx->poll_handlers ; i++) {
478                         struct ifnet *ifp = pctx->pr[i].ifp;
479
480                         if ((ifp->if_flags & (IFF_UP|IFF_RUNNING|IFF_POLLING))
481                             == (IFF_UP|IFF_RUNNING|IFF_POLLING)) {
482                                 if (lwkt_serialize_try(ifp->if_serializer)) {
483                                         ifp->if_poll(ifp, arg, cycles);
484                                         lwkt_serialize_exit(ifp->if_serializer);
485                                 }
486                         }
487                 }
488         } else {        /* unregister */
489                 for (i = 0 ; i < pctx->poll_handlers ; i++) {
490                         struct ifnet *ifp = pctx->pr[i].ifp;
491
492                         if ((ifp->if_flags & IFF_POLLING) == 0)
493                                 continue;
494                         /*
495                          * Only call the interface deregistration
496                          * function if the interface is still 
497                          * running.
498                          */
499                         lwkt_serialize_enter(ifp->if_serializer);
500                         ifp->if_flags &= ~IFF_POLLING;
501                         if (ifp->if_flags & IFF_RUNNING)
502                                 ifp->if_poll(ifp, POLL_DEREGISTER, 1);
503                         lwkt_serialize_exit(ifp->if_serializer);
504                 }
505                 pctx->residual_burst = 0;
506                 pctx->poll_handlers = 0;
507         }
508         schednetisr(NETISR_POLLMORE);
509         pctx->phase = 4;
510         crit_exit();
511 }
512
513 /*
514  * Try to register routine for polling. Returns 1 if successful
515  * (and polling should be enabled), 0 otherwise.
516  *
517  * Called from mainline code only, not called from an interrupt.
518  */
519 int
520 ether_poll_register(struct ifnet *ifp)
521 {
522         struct pollctx *pctx;
523         int rc;
524
525         if (poll_defcpu < 0)
526                 return 0;
527         KKASSERT(poll_defcpu < POLLCTX_MAX);
528
529         pctx = poll_context[poll_defcpu];
530         KKASSERT(pctx != NULL);
531         KKASSERT(pctx->poll_cpuid == poll_defcpu);
532
533         if (pctx->polling_enabled == 0) /* polling disabled, cannot register */
534                 return 0;
535         if (ifp->if_flags & IFF_POLLING)        /* already polling      */
536                 return 0;
537         if (ifp->if_poll == NULL)               /* no polling support   */
538                 return 0;
539
540         /*
541          * Attempt to register.  Interlock with IFF_POLLING.
542          */
543         crit_enter();   /* XXX MP - not mp safe */
544         lwkt_serialize_enter(ifp->if_serializer);
545         ifp->if_flags |= IFF_POLLING;
546         if (ifp->if_flags & IFF_RUNNING)
547                 ifp->if_poll(ifp, POLL_REGISTER, 0);
548         lwkt_serialize_exit(ifp->if_serializer);
549         if ((ifp->if_flags & IFF_POLLING) == 0) {
550                 crit_exit();
551                 return 0;
552         }
553
554         /*
555          * Check if there is room.  If there isn't, deregister.
556          */
557         if (pctx->poll_handlers >= POLL_LIST_LEN) {
558                 /*
559                  * List full, cannot register more entries.
560                  * This should never happen; if it does, it is probably a
561                  * broken driver trying to register multiple times. Checking
562                  * this at runtime is expensive, and won't solve the problem
563                  * anyways, so just report a few times and then give up.
564                  */
565                 static int verbose = 10;        /* XXX */
566                 if (verbose >0) {
567                         kprintf("poll handlers list full, "
568                                 "maybe a broken driver ?\n");
569                         verbose--;
570                 }
571                 lwkt_serialize_enter(ifp->if_serializer);
572                 ifp->if_flags &= ~IFF_POLLING;
573                 if (ifp->if_flags & IFF_RUNNING)
574                         ifp->if_poll(ifp, POLL_DEREGISTER, 0);
575                 lwkt_serialize_exit(ifp->if_serializer);
576                 rc = 0;
577         } else {
578                 pctx->pr[pctx->poll_handlers].ifp = ifp;
579                 pctx->poll_handlers++;
580                 rc = 1;
581         }
582         crit_exit();
583         return (rc);
584 }
585
586 /*
587  * Remove interface from the polling list.  Occurs when polling is turned
588  * off.  Called from mainline code only, not called from an interrupt.
589  */
590 int
591 ether_poll_deregister(struct ifnet *ifp)
592 {
593         struct pollctx *pctx;
594         int i;
595
596         KKASSERT(ifp != NULL);
597
598         if (poll_defcpu < 0)
599                 return 0;
600         KKASSERT(poll_defcpu < POLLCTX_MAX);
601
602         pctx = poll_context[poll_defcpu];
603         KKASSERT(pctx != NULL);
604         KKASSERT(pctx->poll_cpuid == poll_defcpu);
605
606         crit_enter();
607         if ((ifp->if_flags & IFF_POLLING) == 0) {
608                 crit_exit();
609                 return 0;
610         }
611         for (i = 0 ; i < pctx->poll_handlers ; i++) {
612                 if (pctx->pr[i].ifp == ifp) /* found it */
613                         break;
614         }
615         ifp->if_flags &= ~IFF_POLLING; /* found or not... */
616         if (i == pctx->poll_handlers) {
617                 crit_exit();
618                 kprintf("ether_poll_deregister: ifp not found!!!\n");
619                 return 0;
620         }
621         pctx->poll_handlers--;
622         if (i < pctx->poll_handlers) { /* Last entry replaces this one. */
623                 pctx->pr[i].ifp = pctx->pr[pctx->poll_handlers].ifp;
624         }
625         crit_exit();
626
627         /*
628          * Only call the deregistration function if the interface is still
629          * in a run state.
630          */
631         if (ifp->if_flags & IFF_RUNNING) {
632                 lwkt_serialize_enter(ifp->if_serializer);
633                 ifp->if_poll(ifp, POLL_DEREGISTER, 1);
634                 lwkt_serialize_exit(ifp->if_serializer);
635         }
636         return (1);
637 }
638
639 static void
640 poll_add_sysctl(struct sysctl_ctx_list *ctx, struct sysctl_oid_list *parent,
641                 struct pollctx *pctx)
642 {
643         SYSCTL_ADD_PROC(ctx, parent, OID_AUTO, "enable",
644                         CTLTYPE_INT | CTLFLAG_RW, pctx, 0, sysctl_polling,
645                         "I", "Polling enabled");
646
647         SYSCTL_ADD_PROC(ctx, parent, OID_AUTO, "pollhz",
648                         CTLTYPE_INT | CTLFLAG_RW, pctx, 0, sysctl_pollhz,
649                         "I", "Device polling frequency");
650
651         SYSCTL_ADD_UINT(ctx, parent, OID_AUTO, "phase", CTLFLAG_RD,
652                         &pctx->phase, 0, "Polling phase");
653
654         SYSCTL_ADD_UINT(ctx, parent, OID_AUTO, "suspect", CTLFLAG_RW,
655                         &pctx->suspect, 0, "suspect event");
656
657         SYSCTL_ADD_UINT(ctx, parent, OID_AUTO, "stalled", CTLFLAG_RW,
658                         &pctx->stalled, 0, "potential stalls");
659
660         SYSCTL_ADD_UINT(ctx, parent, OID_AUTO, "burst", CTLFLAG_RW,
661                         &pctx->poll_burst, 0, "Current polling burst size");
662
663         SYSCTL_ADD_UINT(ctx, parent, OID_AUTO, "each_burst", CTLFLAG_RW,
664                         &pctx->poll_each_burst, 0, "Max size of each burst");
665
666         SYSCTL_ADD_UINT(ctx, parent, OID_AUTO, "burst_max", CTLFLAG_RW,
667                         &pctx->poll_burst_max, 0, "Max Polling burst size");
668
669         SYSCTL_ADD_UINT(ctx, parent, OID_AUTO, "user_frac", CTLFLAG_RW,
670                         &pctx->user_frac, 0,
671                         "Desired user fraction of cpu time");
672
673         SYSCTL_ADD_UINT(ctx, parent, OID_AUTO, "reg_frac", CTLFLAG_RW,
674                         &pctx->reg_frac, 0,
675                         "Every this many cycles poll register");
676
677         SYSCTL_ADD_UINT(ctx, parent, OID_AUTO, "short_ticks", CTLFLAG_RW,
678                         &pctx->short_ticks, 0,
679                         "Hardclock ticks shorter than they should be");
680
681         SYSCTL_ADD_UINT(ctx, parent, OID_AUTO, "lost_polls", CTLFLAG_RW,
682                         &pctx->lost_polls, 0,
683                         "How many times we would have lost a poll tick");
684
685         SYSCTL_ADD_UINT(ctx, parent, OID_AUTO, "pending_polls", CTLFLAG_RD,
686                         &pctx->pending_polls, 0, "Do we need to poll again");
687
688         SYSCTL_ADD_INT(ctx, parent, OID_AUTO, "residual_burst", CTLFLAG_RW,
689                        &pctx->residual_burst, 0,
690                        "# of residual cycles in burst");
691
692         SYSCTL_ADD_UINT(ctx, parent, OID_AUTO, "handlers", CTLFLAG_RD,
693                         &pctx->poll_handlers, 0,
694                         "Number of registered poll handlers");
695 }