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