Support polling(4) on multiple CPUs, i.e. each NIC can be assigned to a
[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.34 2007/09/30 04:37:27 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 #include <net/netmsg2.h>
45
46 /*
47  * Polling support for [network] device drivers.
48  *
49  * Drivers which support this feature try to register with the
50  * polling code.
51  *
52  * If registration is successful, the driver must disable interrupts,
53  * and further I/O is performed through the handler, which is invoked
54  * (at least once per clock tick) with 3 arguments: the "arg" passed at
55  * register time (a struct ifnet pointer), a command, and a "count" limit.
56  *
57  * The command can be one of the following:
58  *  POLL_ONLY: quick move of "count" packets from input/output queues.
59  *  POLL_AND_CHECK_STATUS: as above, plus check status registers or do
60  *      other more expensive operations. This command is issued periodically
61  *      but less frequently than POLL_ONLY.
62  *  POLL_DEREGISTER: deregister and return to interrupt mode.
63  *  POLL_REGISTER: register and disable interrupts
64  *
65  * The first two commands are only issued if the interface is marked as
66  * 'IFF_UP, IFF_RUNNING and IFF_POLLING', the last two only if IFF_RUNNING
67  * is set.
68  *
69  * The count limit specifies how much work the handler can do during the
70  * call -- typically this is the number of packets to be received, or
71  * transmitted, etc. (drivers are free to interpret this number, as long
72  * as the max time spent in the function grows roughly linearly with the
73  * count).
74  *
75  * Deregistration can be requested by the driver itself (typically in the
76  * *_stop() routine), or by the polling code, by invoking the handler.
77  *
78  * Polling can be globally enabled or disabled with the sysctl variable
79  * kern.polling.enable (default is 0, disabled)
80  *
81  * A second variable controls the sharing of CPU between polling/kernel
82  * network processing, and other activities (typically userlevel tasks):
83  * kern.polling.user_frac (between 0 and 100, default 50) sets the share
84  * of CPU allocated to user tasks. CPU is allocated proportionally to the
85  * shares, by dynamically adjusting the "count" (poll_burst).
86  *
87  * Other parameters can should be left to their default values.
88  * The following constraints hold
89  *
90  *      1 <= poll_each_burst <= poll_burst <= poll_burst_max
91  *      MIN_POLL_BURST_MAX <= poll_burst_max <= MAX_POLL_BURST_MAX
92  */
93
94 #define MIN_POLL_BURST_MAX      10
95 #define MAX_POLL_BURST_MAX      1000
96
97 #ifndef DEVICE_POLLING_FREQ_MAX
98 #define DEVICE_POLLING_FREQ_MAX         30000
99 #endif
100 #define DEVICE_POLLING_FREQ_DEFAULT     2000
101
102 #define POLL_LIST_LEN  128
103 struct pollrec {
104         struct ifnet    *ifp;
105 };
106
107 #define POLLCTX_MAX     32
108
109 struct pollctx {
110         struct sysctl_ctx_list  poll_sysctl_ctx;
111         struct sysctl_oid       *poll_sysctl_tree;
112
113         uint32_t                poll_burst;
114         uint32_t                poll_each_burst;
115         uint32_t                poll_burst_max;
116         uint32_t                user_frac;
117         int                     reg_frac_count;
118         uint32_t                reg_frac;
119         uint32_t                short_ticks;
120         uint32_t                lost_polls;
121         uint32_t                pending_polls;
122         int                     residual_burst;
123         uint32_t                phase;
124         uint32_t                suspect;
125         uint32_t                stalled;
126         struct timeval          poll_start_t;
127         struct timeval          prev_t;
128
129         uint32_t                poll_handlers; /* next free entry in pr[]. */
130         struct pollrec          pr[POLL_LIST_LEN];
131
132         int                     poll_cpuid;
133         struct systimer         pollclock;
134         int                     polling_enabled;
135         int                     pollhz;
136
137         struct netmsg           poll_netmsg;
138         struct netmsg           poll_more_netmsg;
139 };
140
141 static struct pollctx   *poll_context[POLLCTX_MAX];
142
143 SYSCTL_NODE(_kern, OID_AUTO, polling, CTLFLAG_RW, 0,
144         "Device polling parameters");
145
146 static int      poll_defcpu = -1;
147 SYSCTL_INT(_kern_polling, OID_AUTO, defcpu, CTLFLAG_RD,
148         &poll_defcpu, 0, "default CPU to run device polling");
149
150 static uint32_t poll_cpumask0 = 0xffffffff;
151 TUNABLE_INT("kern.polling.cpumask", &poll_cpumask0);
152
153 static uint32_t poll_cpumask;
154 SYSCTL_INT(_kern_polling, OID_AUTO, cpumask, CTLFLAG_RD,
155         &poll_cpumask, 0, "CPUs that can run device polling");
156
157 static int      polling_enabled = 1;    /* global polling enable */
158 TUNABLE_INT("kern.polling.enable", &polling_enabled);
159
160 static int      pollhz = DEVICE_POLLING_FREQ_DEFAULT;
161 TUNABLE_INT("kern.polling.pollhz", &pollhz);
162
163 /* The two netisr handlers */
164 static void     netisr_poll(struct netmsg *);
165 static void     netisr_pollmore(struct netmsg *);
166
167 static void     poll_register(struct netmsg *);
168 static void     poll_deregister(struct netmsg *);
169 static void     poll_sysctl_pollhz(struct netmsg *);
170 static void     poll_sysctl_polling(struct netmsg *);
171
172 /* Systimer handler */
173 static void     pollclock(systimer_t, struct intrframe *);
174
175 /* Sysctl handlers */
176 static int      sysctl_pollhz(SYSCTL_HANDLER_ARGS);
177 static int      sysctl_polling(SYSCTL_HANDLER_ARGS);
178 static void     poll_add_sysctl(struct sysctl_ctx_list *,
179                                 struct sysctl_oid_list *, struct pollctx *);
180
181 static void     schedpoll_oncpu(struct pollctx *, struct netmsg *, netisr_fn_t);
182
183 void            init_device_poll_pcpu(int);     /* per-cpu init routine */
184
185 /*
186  * Initialize per-cpu polling(4) context.  Called from kern_clock.c:
187  */
188 void
189 init_device_poll_pcpu(int cpuid)
190 {
191         struct pollctx *pctx;
192         char cpuid_str[3];
193
194         if (cpuid >= POLLCTX_MAX)
195                 return;
196
197         if (((1 << cpuid) & poll_cpumask0) == 0)
198                 return;
199
200         poll_cpumask |= (1 << cpuid);
201
202         pctx = kmalloc(sizeof(*pctx), M_DEVBUF, M_WAITOK | M_ZERO);
203
204         pctx->poll_burst = 5;
205         pctx->poll_each_burst = 5;
206         pctx->poll_burst_max = 150; /* good for 100Mbit net and HZ=1000 */
207         pctx->user_frac = 50;
208         pctx->reg_frac = 20;
209         pctx->polling_enabled = polling_enabled;
210         pctx->pollhz = pollhz;
211         pctx->poll_cpuid = cpuid;
212         netmsg_init(&pctx->poll_netmsg, &netisr_adone_rport, 0, NULL);
213         netmsg_init(&pctx->poll_more_netmsg, &netisr_adone_rport, 0, NULL);
214
215         KASSERT(cpuid < POLLCTX_MAX, ("cpu id must < %d", cpuid));
216         poll_context[cpuid] = pctx;
217
218         if (poll_defcpu < 0) {
219                 poll_defcpu = cpuid;
220
221                 /*
222                  * Initialize global sysctl nodes, for compat
223                  */
224                 poll_add_sysctl(NULL, SYSCTL_STATIC_CHILDREN(_kern_polling),
225                                 pctx);
226         }
227
228         /*
229          * Initialize per-cpu sysctl nodes
230          */
231         ksnprintf(cpuid_str, sizeof(cpuid_str), "%d", pctx->poll_cpuid);
232
233         sysctl_ctx_init(&pctx->poll_sysctl_ctx);
234         pctx->poll_sysctl_tree = SYSCTL_ADD_NODE(&pctx->poll_sysctl_ctx,
235                                  SYSCTL_STATIC_CHILDREN(_kern_polling),
236                                  OID_AUTO, cpuid_str, CTLFLAG_RD, 0, "");
237         poll_add_sysctl(&pctx->poll_sysctl_ctx,
238                         SYSCTL_CHILDREN(pctx->poll_sysctl_tree), pctx);
239
240         /*
241          * Initialize systimer
242          */
243         systimer_init_periodic_nq(&pctx->pollclock, pollclock, pctx, 1);
244 }
245
246 static __inline void
247 schedpoll(struct pollctx *pctx)
248 {
249         schedpoll_oncpu(pctx, &pctx->poll_netmsg, netisr_poll);
250 }
251
252 static __inline void
253 schedpollmore(struct pollctx *pctx)
254 {
255         schedpoll_oncpu(pctx, &pctx->poll_more_netmsg, netisr_pollmore);
256 }
257
258 /*
259  * Set the polling frequency
260  */
261 static int
262 sysctl_pollhz(SYSCTL_HANDLER_ARGS)
263 {
264         struct pollctx *pctx = arg1;
265         struct netmsg msg;
266         lwkt_port_t port;
267         int error, phz;
268
269         phz = pctx->pollhz;
270         error = sysctl_handle_int(oidp, &phz, 0, req);
271         if (error || req->newptr == NULL)
272                 return error;
273         if (phz <= 0)
274                 return EINVAL;
275         else if (phz > DEVICE_POLLING_FREQ_MAX)
276                 phz = DEVICE_POLLING_FREQ_MAX;
277
278         netmsg_init(&msg, &curthread->td_msgport, 0, poll_sysctl_pollhz);
279         msg.nm_lmsg.u.ms_result = phz;
280
281         port = cpu_portfn(pctx->poll_cpuid);
282         lwkt_domsg(port, &msg.nm_lmsg, 0);
283         return 0;
284 }
285
286 /*
287  * Master enable.  If polling is disabled, cut the polling systimer 
288  * frequency to 1hz.
289  */
290 static int
291 sysctl_polling(SYSCTL_HANDLER_ARGS)
292 {
293         struct pollctx *pctx = arg1;
294         struct netmsg msg;
295         lwkt_port_t port;
296         int error, enabled;
297
298         enabled = pctx->polling_enabled;
299         error = sysctl_handle_int(oidp, &enabled, 0, req);
300         if (error || req->newptr == NULL)
301                 return error;
302
303         netmsg_init(&msg, &curthread->td_msgport, 0, poll_sysctl_polling);
304         msg.nm_lmsg.u.ms_result = enabled;
305
306         port = cpu_portfn(pctx->poll_cpuid);
307         lwkt_domsg(port, &msg.nm_lmsg, 0);
308         return 0;
309 }
310
311 /*
312  * Hook from hardclock. Tries to schedule a netisr, but keeps track
313  * of lost ticks due to the previous handler taking too long.
314  * Normally, this should not happen, because polling handler should
315  * run for a short time. However, in some cases (e.g. when there are
316  * changes in link status etc.) the drivers take a very long time
317  * (even in the order of milliseconds) to reset and reconfigure the
318  * device, causing apparent lost polls.
319  *
320  * The first part of the code is just for debugging purposes, and tries
321  * to count how often hardclock ticks are shorter than they should,
322  * meaning either stray interrupts or delayed events.
323  *
324  * WARNING! called from fastint or IPI, the MP lock might not be held.
325  */
326 static void
327 pollclock(systimer_t info, struct intrframe *frame __unused)
328 {
329         struct pollctx *pctx = info->data;
330         struct timeval t;
331         int delta;
332
333         if (pctx->poll_handlers == 0)
334                 return;
335
336         microuptime(&t);
337         delta = (t.tv_usec - pctx->prev_t.tv_usec) +
338                 (t.tv_sec - pctx->prev_t.tv_sec)*1000000;
339         if (delta * hz < 500000)
340                 pctx->short_ticks++;
341         else
342                 pctx->prev_t = t;
343
344         if (pctx->pending_polls > 100) {
345                 /*
346                  * Too much, assume it has stalled (not always true
347                  * see comment above).
348                  */
349                 pctx->stalled++;
350                 pctx->pending_polls = 0;
351                 pctx->phase = 0;
352         }
353
354         if (pctx->phase <= 2) {
355                 if (pctx->phase != 0)
356                         pctx->suspect++;
357                 pctx->phase = 1;
358                 schedpoll(pctx);
359                 pctx->phase = 2;
360         }
361         if (pctx->pending_polls++ > 0)
362                 pctx->lost_polls++;
363 }
364
365 /*
366  * netisr_pollmore is called after other netisr's, possibly scheduling
367  * another NETISR_POLL call, or adapting the burst size for the next cycle.
368  *
369  * It is very bad to fetch large bursts of packets from a single card at once,
370  * because the burst could take a long time to be completely processed, or
371  * could saturate the intermediate queue (ipintrq or similar) leading to
372  * losses or unfairness. To reduce the problem, and also to account better for
373  * time spent in network-related processing, we split the burst in smaller
374  * chunks of fixed size, giving control to the other netisr's between chunks.
375  * This helps in improving the fairness, reducing livelock (because we
376  * emulate more closely the "process to completion" that we have with
377  * fastforwarding) and accounting for the work performed in low level
378  * handling and forwarding.
379  */
380
381 /* ARGSUSED */
382 static void
383 netisr_pollmore(struct netmsg *msg)
384 {
385         struct pollctx *pctx;
386         struct timeval t;
387         int kern_load, cpuid;
388
389         cpuid = mycpu->gd_cpuid;
390         KKASSERT(cpuid < POLLCTX_MAX);
391
392         pctx = poll_context[cpuid];
393         KKASSERT(pctx != NULL);
394         KKASSERT(pctx->poll_cpuid == cpuid);
395         KKASSERT(pctx == msg->nm_lmsg.u.ms_resultp);
396
397         lwkt_replymsg(&msg->nm_lmsg, 0);
398
399         pctx->phase = 5;
400         if (pctx->residual_burst > 0) {
401                 schedpoll(pctx);
402                 /* will run immediately on return, followed by netisrs */
403                 return;
404         }
405         /* here we can account time spent in netisr's in this tick */
406         microuptime(&t);
407         kern_load = (t.tv_usec - pctx->poll_start_t.tv_usec) +
408                 (t.tv_sec - pctx->poll_start_t.tv_sec)*1000000; /* us */
409         kern_load = (kern_load * hz) / 10000;                   /* 0..100 */
410         if (kern_load > (100 - pctx->user_frac)) { /* try decrease ticks */
411                 if (pctx->poll_burst > 1)
412                         pctx->poll_burst--;
413         } else {
414                 if (pctx->poll_burst < pctx->poll_burst_max)
415                         pctx->poll_burst++;
416         }
417
418         pctx->pending_polls--;
419         if (pctx->pending_polls == 0) { /* we are done */
420                 pctx->phase = 0;
421         } else {
422                 /*
423                  * Last cycle was long and caused us to miss one or more
424                  * hardclock ticks. Restart processing again, but slightly
425                  * reduce the burst size to prevent that this happens again.
426                  */
427                 pctx->poll_burst -= (pctx->poll_burst / 8);
428                 if (pctx->poll_burst < 1)
429                         pctx->poll_burst = 1;
430                 schedpoll(pctx);
431                 pctx->phase = 6;
432         }
433 }
434
435 /*
436  * netisr_poll is scheduled by schednetisr when appropriate, typically once
437  * per tick.
438  *
439  * Note that the message is replied immediately in order to allow a new
440  * ISR to be scheduled in the handler.
441  *
442  * XXX each registration should indicate whether it needs a critical
443  * section to operate.
444  */
445 /* ARGSUSED */
446 static void
447 netisr_poll(struct netmsg *msg)
448 {
449         struct pollctx *pctx;
450         int i, cycles, cpuid;
451         enum poll_cmd arg = POLL_ONLY;
452
453         cpuid = mycpu->gd_cpuid;
454         KKASSERT(cpuid < POLLCTX_MAX);
455
456         pctx = poll_context[cpuid];
457         KKASSERT(pctx != NULL);
458         KKASSERT(pctx->poll_cpuid == cpuid);
459         KKASSERT(pctx == msg->nm_lmsg.u.ms_resultp);
460
461         lwkt_replymsg(&msg->nm_lmsg, 0);
462
463         pctx->phase = 3;
464         if (pctx->residual_burst == 0) { /* first call in this tick */
465                 microuptime(&pctx->poll_start_t);
466                 /*
467                  * Check that paremeters are consistent with runtime
468                  * variables. Some of these tests could be done at sysctl
469                  * time, but the savings would be very limited because we
470                  * still have to check against reg_frac_count and
471                  * poll_each_burst. So, instead of writing separate sysctl
472                  * handlers, we do all here.
473                  */
474
475                 if (pctx->reg_frac > hz)
476                         pctx->reg_frac = hz;
477                 else if (pctx->reg_frac < 1)
478                         pctx->reg_frac = 1;
479                 if (pctx->reg_frac_count > pctx->reg_frac)
480                         pctx->reg_frac_count = pctx->reg_frac - 1;
481                 if (pctx->reg_frac_count-- == 0) {
482                         arg = POLL_AND_CHECK_STATUS;
483                         pctx->reg_frac_count = pctx->reg_frac - 1;
484                 }
485                 if (pctx->poll_burst_max < MIN_POLL_BURST_MAX)
486                         pctx->poll_burst_max = MIN_POLL_BURST_MAX;
487                 else if (pctx->poll_burst_max > MAX_POLL_BURST_MAX)
488                         pctx->poll_burst_max = MAX_POLL_BURST_MAX;
489
490                 if (pctx->poll_each_burst < 1)
491                         pctx->poll_each_burst = 1;
492                 else if (pctx->poll_each_burst > pctx->poll_burst_max)
493                         pctx->poll_each_burst = pctx->poll_burst_max;
494
495                 pctx->residual_burst = pctx->poll_burst;
496         }
497         cycles = (pctx->residual_burst < pctx->poll_each_burst) ?
498                 pctx->residual_burst : pctx->poll_each_burst;
499         pctx->residual_burst -= cycles;
500
501         if (pctx->polling_enabled) {
502                 for (i = 0 ; i < pctx->poll_handlers ; i++) {
503                         struct ifnet *ifp = pctx->pr[i].ifp;
504
505                         if (!lwkt_serialize_try(ifp->if_serializer))
506                                 continue;
507
508                         if ((ifp->if_flags & (IFF_UP|IFF_RUNNING|IFF_POLLING))
509                             == (IFF_UP|IFF_RUNNING|IFF_POLLING))
510                                 ifp->if_poll(ifp, arg, cycles);
511
512                         lwkt_serialize_exit(ifp->if_serializer);
513                 }
514         } else {        /* unregister */
515                 for (i = 0 ; i < pctx->poll_handlers ; i++) {
516                         struct ifnet *ifp = pctx->pr[i].ifp;
517
518                         lwkt_serialize_enter(ifp->if_serializer);
519
520                         if ((ifp->if_flags & IFF_POLLING) == 0) {
521                                 KKASSERT(ifp->if_poll_cpuid < 0);
522                                 lwkt_serialize_exit(ifp->if_serializer);
523                                 continue;
524                         }
525                         ifp->if_flags &= ~IFF_POLLING;
526                         ifp->if_poll_cpuid = -1;
527
528                         /*
529                          * Only call the interface deregistration
530                          * function if the interface is still 
531                          * running.
532                          */
533                         if (ifp->if_flags & IFF_RUNNING)
534                                 ifp->if_poll(ifp, POLL_DEREGISTER, 1);
535
536                         lwkt_serialize_exit(ifp->if_serializer);
537                 }
538                 pctx->residual_burst = 0;
539                 pctx->poll_handlers = 0;
540         }
541         schedpollmore(pctx);
542         pctx->phase = 4;
543 }
544
545 static void
546 poll_register(struct netmsg *msg)
547 {
548         struct ifnet *ifp = msg->nm_lmsg.u.ms_resultp;
549         struct pollctx *pctx;
550         int rc, cpuid;
551
552         cpuid = mycpu->gd_cpuid;
553         KKASSERT(cpuid < POLLCTX_MAX);
554
555         pctx = poll_context[cpuid];
556         KKASSERT(pctx != NULL);
557         KKASSERT(pctx->poll_cpuid == cpuid);
558
559         if (pctx->polling_enabled == 0) {
560                 /* Polling disabled, cannot register */
561                 rc = EOPNOTSUPP;
562                 goto back;
563         }
564
565         /*
566          * Check if there is room.
567          */
568         if (pctx->poll_handlers >= POLL_LIST_LEN) {
569                 /*
570                  * List full, cannot register more entries.
571                  * This should never happen; if it does, it is probably a
572                  * broken driver trying to register multiple times. Checking
573                  * this at runtime is expensive, and won't solve the problem
574                  * anyways, so just report a few times and then give up.
575                  */
576                 static int verbose = 10;        /* XXX */
577                 if (verbose >0) {
578                         kprintf("poll handlers list full, "
579                                 "maybe a broken driver ?\n");
580                         verbose--;
581                 }
582                 rc = ENOMEM;
583         } else {
584                 pctx->pr[pctx->poll_handlers].ifp = ifp;
585                 pctx->poll_handlers++;
586                 rc = 0;
587
588                 if (pctx->poll_handlers == 1) {
589                         KKASSERT(pctx->polling_enabled);
590                         systimer_adjust_periodic(&pctx->pollclock,
591                                                  pctx->pollhz);
592                 }
593         }
594 back:
595         lwkt_replymsg(&msg->nm_lmsg, rc);
596 }
597
598 /*
599  * Try to register routine for polling. Returns 1 if successful
600  * (and polling should be enabled), 0 otherwise.
601  *
602  * Called from mainline code only, not called from an interrupt.
603  */
604 int
605 ether_poll_register(struct ifnet *ifp)
606 {
607         if (poll_defcpu < 0)
608                 return 0;
609         KKASSERT(poll_defcpu < POLLCTX_MAX);
610
611         return ether_pollcpu_register(ifp, poll_defcpu);
612 }
613
614 int
615 ether_pollcpu_register(struct ifnet *ifp, int cpuid)
616 {
617         struct netmsg msg;
618         lwkt_port_t port;
619         int rc;
620
621         if (ifp->if_poll == NULL) {
622                 /* Device does not support polling */
623                 return 0;
624         }
625
626         if (cpuid < 0 || cpuid >= POLLCTX_MAX)
627                 return 0;
628
629         if (((1 << cpuid) & poll_cpumask) == 0) {
630                 /* Polling is not supported on 'cpuid' */
631                 return 0;
632         }
633         KKASSERT(poll_context[cpuid] != NULL);
634
635         /*
636          * Attempt to register.  Interlock with IFF_POLLING.
637          */
638         crit_enter();   /* XXX MP - not mp safe */
639
640         lwkt_serialize_enter(ifp->if_serializer);
641         if (ifp->if_flags & IFF_POLLING) {
642                 /* Already polling */
643                 KKASSERT(ifp->if_poll_cpuid >= 0);
644                 lwkt_serialize_exit(ifp->if_serializer);
645                 crit_exit();
646                 return 0;
647         }
648         KKASSERT(ifp->if_poll_cpuid < 0);
649         ifp->if_flags |= IFF_POLLING;
650         ifp->if_poll_cpuid = cpuid;
651         if (ifp->if_flags & IFF_RUNNING)
652                 ifp->if_poll(ifp, POLL_REGISTER, 0);
653         lwkt_serialize_exit(ifp->if_serializer);
654
655         netmsg_init(&msg, &curthread->td_msgport, 0, poll_register);
656         msg.nm_lmsg.u.ms_resultp = ifp;
657
658         port = cpu_portfn(cpuid);
659         lwkt_domsg(port, &msg.nm_lmsg, 0);
660
661         if (msg.nm_lmsg.ms_error) {
662                 lwkt_serialize_enter(ifp->if_serializer);
663                 ifp->if_flags &= ~IFF_POLLING;
664                 ifp->if_poll_cpuid = -1;
665                 if (ifp->if_flags & IFF_RUNNING)
666                         ifp->if_poll(ifp, POLL_DEREGISTER, 0);
667                 lwkt_serialize_exit(ifp->if_serializer);
668                 rc = 0;
669         } else {
670                 rc = 1;
671         }
672
673         crit_exit();
674         return rc;
675 }
676
677 static void
678 poll_deregister(struct netmsg *msg)
679 {
680         struct ifnet *ifp = msg->nm_lmsg.u.ms_resultp;
681         struct pollctx *pctx;
682         int rc, i, cpuid;
683
684         cpuid = mycpu->gd_cpuid;
685         KKASSERT(cpuid < POLLCTX_MAX);
686
687         pctx = poll_context[cpuid];
688         KKASSERT(pctx != NULL);
689         KKASSERT(pctx->poll_cpuid == cpuid);
690
691         for (i = 0 ; i < pctx->poll_handlers ; i++) {
692                 if (pctx->pr[i].ifp == ifp) /* Found it */
693                         break;
694         }
695         if (i == pctx->poll_handlers) {
696                 kprintf("ether_poll_deregister: ifp not found!!!\n");
697                 rc = ENOENT;
698         } else {
699                 pctx->poll_handlers--;
700                 if (i < pctx->poll_handlers) {
701                         /* Last entry replaces this one. */
702                         pctx->pr[i].ifp = pctx->pr[pctx->poll_handlers].ifp;
703                 }
704
705                 if (pctx->poll_handlers == 0)
706                         systimer_adjust_periodic(&pctx->pollclock, 1);
707                 rc = 0;
708         }
709         lwkt_replymsg(&msg->nm_lmsg, rc);
710 }
711
712 /*
713  * Remove interface from the polling list.  Occurs when polling is turned
714  * off.  Called from mainline code only, not called from an interrupt.
715  */
716 int
717 ether_poll_deregister(struct ifnet *ifp)
718 {
719         struct netmsg msg;
720         lwkt_port_t port;
721         int rc, cpuid;
722
723         KKASSERT(ifp != NULL);
724
725         if (ifp->if_poll == NULL)
726                 return 0;
727
728         crit_enter();
729
730         lwkt_serialize_enter(ifp->if_serializer);
731         if ((ifp->if_flags & IFF_POLLING) == 0) {
732                 KKASSERT(ifp->if_poll_cpuid < 0);
733                 lwkt_serialize_exit(ifp->if_serializer);
734                 crit_exit();
735                 return 0;
736         }
737
738         cpuid = ifp->if_poll_cpuid;
739         KKASSERT(cpuid >= 0);
740         KKASSERT(poll_context[cpuid] != NULL);
741
742         ifp->if_flags &= ~IFF_POLLING;
743         ifp->if_poll_cpuid = -1;
744         lwkt_serialize_exit(ifp->if_serializer);
745
746         netmsg_init(&msg, &curthread->td_msgport, 0, poll_deregister);
747         msg.nm_lmsg.u.ms_resultp = ifp;
748
749         port = cpu_portfn(cpuid);
750         lwkt_domsg(port, &msg.nm_lmsg, 0);
751
752         if (!msg.nm_lmsg.ms_error) {
753                 lwkt_serialize_enter(ifp->if_serializer);
754                 if (ifp->if_flags & IFF_RUNNING)
755                         ifp->if_poll(ifp, POLL_DEREGISTER, 1);
756                 lwkt_serialize_exit(ifp->if_serializer);
757                 rc = 1;
758         } else {
759                 rc = 0;
760         }
761
762         crit_exit();
763         return rc;
764 }
765
766 static void
767 poll_add_sysctl(struct sysctl_ctx_list *ctx, struct sysctl_oid_list *parent,
768                 struct pollctx *pctx)
769 {
770         SYSCTL_ADD_PROC(ctx, parent, OID_AUTO, "enable",
771                         CTLTYPE_INT | CTLFLAG_RW, pctx, 0, sysctl_polling,
772                         "I", "Polling enabled");
773
774         SYSCTL_ADD_PROC(ctx, parent, OID_AUTO, "pollhz",
775                         CTLTYPE_INT | CTLFLAG_RW, pctx, 0, sysctl_pollhz,
776                         "I", "Device polling frequency");
777
778         SYSCTL_ADD_UINT(ctx, parent, OID_AUTO, "phase", CTLFLAG_RD,
779                         &pctx->phase, 0, "Polling phase");
780
781         SYSCTL_ADD_UINT(ctx, parent, OID_AUTO, "suspect", CTLFLAG_RW,
782                         &pctx->suspect, 0, "suspect event");
783
784         SYSCTL_ADD_UINT(ctx, parent, OID_AUTO, "stalled", CTLFLAG_RW,
785                         &pctx->stalled, 0, "potential stalls");
786
787         SYSCTL_ADD_UINT(ctx, parent, OID_AUTO, "burst", CTLFLAG_RW,
788                         &pctx->poll_burst, 0, "Current polling burst size");
789
790         SYSCTL_ADD_UINT(ctx, parent, OID_AUTO, "each_burst", CTLFLAG_RW,
791                         &pctx->poll_each_burst, 0, "Max size of each burst");
792
793         SYSCTL_ADD_UINT(ctx, parent, OID_AUTO, "burst_max", CTLFLAG_RW,
794                         &pctx->poll_burst_max, 0, "Max Polling burst size");
795
796         SYSCTL_ADD_UINT(ctx, parent, OID_AUTO, "user_frac", CTLFLAG_RW,
797                         &pctx->user_frac, 0,
798                         "Desired user fraction of cpu time");
799
800         SYSCTL_ADD_UINT(ctx, parent, OID_AUTO, "reg_frac", CTLFLAG_RW,
801                         &pctx->reg_frac, 0,
802                         "Every this many cycles poll register");
803
804         SYSCTL_ADD_UINT(ctx, parent, OID_AUTO, "short_ticks", CTLFLAG_RW,
805                         &pctx->short_ticks, 0,
806                         "Hardclock ticks shorter than they should be");
807
808         SYSCTL_ADD_UINT(ctx, parent, OID_AUTO, "lost_polls", CTLFLAG_RW,
809                         &pctx->lost_polls, 0,
810                         "How many times we would have lost a poll tick");
811
812         SYSCTL_ADD_UINT(ctx, parent, OID_AUTO, "pending_polls", CTLFLAG_RD,
813                         &pctx->pending_polls, 0, "Do we need to poll again");
814
815         SYSCTL_ADD_INT(ctx, parent, OID_AUTO, "residual_burst", CTLFLAG_RW,
816                        &pctx->residual_burst, 0,
817                        "# of residual cycles in burst");
818
819         SYSCTL_ADD_UINT(ctx, parent, OID_AUTO, "handlers", CTLFLAG_RD,
820                         &pctx->poll_handlers, 0,
821                         "Number of registered poll handlers");
822 }
823
824 static void
825 schedpoll_oncpu(struct pollctx *pctx, struct netmsg *msg, netisr_fn_t handler)
826 {
827         if (msg->nm_lmsg.ms_flags & MSGF_DONE) {
828                 lwkt_port_t port;
829
830                 netmsg_init(msg, &netisr_adone_rport, 0, handler);
831 #ifdef INVARIANTS
832                 msg->nm_lmsg.u.ms_resultp = pctx;
833 #endif
834                 port = cpu_portfn(mycpu->gd_cpuid);
835                 lwkt_sendmsg(port, &msg->nm_lmsg);
836         }
837 }
838
839 static void
840 poll_sysctl_pollhz(struct netmsg *msg)
841 {
842         struct pollctx *pctx;
843         int cpuid;
844
845         cpuid = mycpu->gd_cpuid;
846         KKASSERT(cpuid < POLLCTX_MAX);
847
848         pctx = poll_context[cpuid];
849         KKASSERT(pctx != NULL);
850         KKASSERT(pctx->poll_cpuid == cpuid);
851
852         pctx->pollhz = msg->nm_lmsg.u.ms_result;
853         if (pctx->polling_enabled && pctx->poll_handlers)
854                 systimer_adjust_periodic(&pctx->pollclock, pctx->pollhz);
855         lwkt_replymsg(&msg->nm_lmsg, 0);
856 }
857
858 static void
859 poll_sysctl_polling(struct netmsg *msg)
860 {
861         struct pollctx *pctx;
862         int cpuid;
863
864         cpuid = mycpu->gd_cpuid;
865         KKASSERT(cpuid < POLLCTX_MAX);
866
867         pctx = poll_context[cpuid];
868         KKASSERT(pctx != NULL);
869         KKASSERT(pctx->poll_cpuid == cpuid);
870
871         pctx->polling_enabled = msg->nm_lmsg.u.ms_result;
872         if (pctx->polling_enabled && pctx->poll_handlers)
873                 systimer_adjust_periodic(&pctx->pollclock, pctx->pollhz);
874         else
875                 systimer_adjust_periodic(&pctx->pollclock, 1);
876         lwkt_replymsg(&msg->nm_lmsg, 0);
877 }