linprocfs - Introduce /proc/mounts
[dragonfly.git] / sys / kern / kern_poll.c
blob1504b37c19a748069b0ed90c0418bf92ae3a5725
1 /*-
2 * Copyright (c) 2001-2002 Luigi Rizzo
4 * Supported by: the Xorp Project (www.xorp.org)
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.
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.
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.48 2008/09/24 12:07:19 sephe Exp $
31 #include "opt_polling.h"
33 #include <sys/param.h>
34 #include <sys/kernel.h>
35 #include <sys/ktr.h>
36 #include <sys/socket.h> /* needed by net/if.h */
37 #include <sys/sysctl.h>
39 #include <sys/thread2.h>
40 #include <sys/msgport2.h>
42 #include <net/if.h> /* for IFF_* flags */
43 #include <net/netmsg2.h>
46 * Polling support for [network] device drivers.
48 * Drivers which support this feature try to register with the
49 * polling code.
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.
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
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.
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).
74 * Deregistration can be requested by the driver itself (typically in the
75 * *_stop() routine), or by the polling code, by invoking the handler.
77 * Polling can be enabled or disabled on particular CPU_X with the sysctl
78 * variable kern.polling.X.enable (default is 1, enabled)
80 * A second variable controls the sharing of CPU between polling/kernel
81 * network processing, and other activities (typically userlevel tasks):
82 * kern.polling.X.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).
86 * Other parameters can should be left to their default values.
87 * The following constraints hold
89 * 1 <= poll_burst <= poll_burst_max
90 * 1 <= poll_each_burst <= poll_burst_max
91 * MIN_POLL_BURST_MAX <= poll_burst_max <= MAX_POLL_BURST_MAX
94 #define MIN_POLL_BURST_MAX 10
95 #define MAX_POLL_BURST_MAX 1000
96 #define POLL_BURST_MAX 150 /* good for 100Mbit net and HZ=1000 */
97 #define POLL_EACH_BURST 5
99 #ifndef DEVICE_POLLING_FREQ_MAX
100 #define DEVICE_POLLING_FREQ_MAX 30000
101 #endif
102 #define DEVICE_POLLING_FREQ_DEFAULT 2000
104 #define POLL_LIST_LEN 128
105 struct pollrec {
106 struct ifnet *ifp;
109 #define POLLCTX_MAX 32
111 struct pollctx {
112 struct sysctl_ctx_list poll_sysctl_ctx;
113 struct sysctl_oid *poll_sysctl_tree;
115 uint32_t poll_burst; /* state */
116 uint32_t poll_each_burst; /* tunable */
117 uint32_t poll_burst_max; /* tunable */
118 uint32_t user_frac; /* tunable */
119 int reg_frac_count; /* state */
120 uint32_t reg_frac; /* tunable */
121 uint32_t short_ticks; /* statistics */
122 uint32_t lost_polls; /* statistics */
123 uint32_t pending_polls; /* state */
124 int residual_burst; /* state */
125 uint32_t phase; /* state */
126 uint32_t suspect; /* statistics */
127 uint32_t stalled; /* statistics */
128 struct timeval poll_start_t; /* state */
129 struct timeval prev_t; /* state */
131 uint32_t poll_handlers; /* next free entry in pr[]. */
132 struct pollrec pr[POLL_LIST_LEN];
134 int poll_cpuid;
135 struct systimer pollclock;
136 int polling_enabled; /* tunable */
137 int pollhz; /* tunable */
139 struct netmsg poll_netmsg;
140 struct netmsg poll_more_netmsg;
143 static struct pollctx *poll_context[POLLCTX_MAX];
145 SYSCTL_NODE(_kern, OID_AUTO, polling, CTLFLAG_RW, 0,
146 "Device polling parameters");
148 static int poll_defcpu = -1;
149 SYSCTL_INT(_kern_polling, OID_AUTO, defcpu, CTLFLAG_RD,
150 &poll_defcpu, 0, "default CPU to run device polling");
152 static uint32_t poll_cpumask0 = 0xffffffff;
153 TUNABLE_INT("kern.polling.cpumask", (int *)&poll_cpumask0);
155 static uint32_t poll_cpumask;
156 SYSCTL_INT(_kern_polling, OID_AUTO, cpumask, CTLFLAG_RD,
157 &poll_cpumask, 0, "CPUs that can run device polling");
159 static int polling_enabled = 1; /* global polling enable */
160 TUNABLE_INT("kern.polling.enable", &polling_enabled);
162 static int pollhz = DEVICE_POLLING_FREQ_DEFAULT;
163 TUNABLE_INT("kern.polling.pollhz", &pollhz);
165 static int poll_burst_max = POLL_BURST_MAX;
166 TUNABLE_INT("kern.polling.burst_max", &poll_burst_max);
168 static int poll_each_burst = POLL_EACH_BURST;
169 TUNABLE_INT("kern.polling.each_burst", &poll_each_burst);
171 /* Netisr handlers */
172 static void netisr_poll(struct netmsg *);
173 static void netisr_pollmore(struct netmsg *);
174 static void poll_register(struct netmsg *);
175 static void poll_deregister(struct netmsg *);
176 static void poll_sysctl_pollhz(struct netmsg *);
177 static void poll_sysctl_polling(struct netmsg *);
178 static void poll_sysctl_regfrac(struct netmsg *);
179 static void poll_sysctl_burstmax(struct netmsg *);
180 static void poll_sysctl_eachburst(struct netmsg *);
182 /* Systimer handler */
183 static void pollclock(systimer_t, struct intrframe *);
185 /* Sysctl handlers */
186 static int sysctl_pollhz(SYSCTL_HANDLER_ARGS);
187 static int sysctl_polling(SYSCTL_HANDLER_ARGS);
188 static int sysctl_regfrac(SYSCTL_HANDLER_ARGS);
189 static int sysctl_burstmax(SYSCTL_HANDLER_ARGS);
190 static int sysctl_eachburst(SYSCTL_HANDLER_ARGS);
191 static void poll_add_sysctl(struct sysctl_ctx_list *,
192 struct sysctl_oid_list *, struct pollctx *);
194 static void schedpoll_oncpu(struct netmsg *);
196 void init_device_poll_pcpu(int); /* per-cpu init routine */
198 #define POLL_KTR_STRING "ifp=%p"
199 #define POLL_KTR_ARG_SIZE (sizeof(void *))
201 #ifndef KTR_POLLING
202 #define KTR_POLLING KTR_ALL
203 #endif
204 KTR_INFO_MASTER(poll);
205 KTR_INFO(KTR_POLLING, poll, beg, 0, POLL_KTR_STRING, POLL_KTR_ARG_SIZE);
206 KTR_INFO(KTR_POLLING, poll, end, 1, POLL_KTR_STRING, POLL_KTR_ARG_SIZE);
208 #define logpoll(name, arg) KTR_LOG(poll_ ## name, arg)
210 static __inline void
211 poll_reset_state(struct pollctx *pctx)
213 crit_enter();
214 pctx->poll_burst = 5;
215 pctx->reg_frac_count = 0;
216 pctx->pending_polls = 0;
217 pctx->residual_burst = 0;
218 pctx->phase = 0;
219 bzero(&pctx->poll_start_t, sizeof(pctx->poll_start_t));
220 bzero(&pctx->prev_t, sizeof(pctx->prev_t));
221 crit_exit();
225 * Initialize per-cpu polling(4) context. Called from kern_clock.c:
227 void
228 init_device_poll_pcpu(int cpuid)
230 struct pollctx *pctx;
231 char cpuid_str[3];
233 if (cpuid >= POLLCTX_MAX)
234 return;
236 if (((1 << cpuid) & poll_cpumask0) == 0)
237 return;
239 if (poll_burst_max < MIN_POLL_BURST_MAX)
240 poll_burst_max = MIN_POLL_BURST_MAX;
241 else if (poll_burst_max > MAX_POLL_BURST_MAX)
242 poll_burst_max = MAX_POLL_BURST_MAX;
244 if (poll_each_burst > poll_burst_max)
245 poll_each_burst = poll_burst_max;
247 poll_cpumask |= (1 << cpuid);
249 pctx = kmalloc(sizeof(*pctx), M_DEVBUF, M_WAITOK | M_ZERO);
251 pctx->poll_each_burst = poll_each_burst;
252 pctx->poll_burst_max = poll_burst_max;
253 pctx->user_frac = 50;
254 pctx->reg_frac = 20;
255 pctx->polling_enabled = polling_enabled;
256 pctx->pollhz = pollhz;
257 pctx->poll_cpuid = cpuid;
258 poll_reset_state(pctx);
260 netmsg_init(&pctx->poll_netmsg, NULL, &netisr_adone_rport,
261 MSGF_MPSAFE, netisr_poll);
262 #ifdef INVARIANTS
263 pctx->poll_netmsg.nm_lmsg.u.ms_resultp = pctx;
264 #endif
266 netmsg_init(&pctx->poll_more_netmsg, NULL, &netisr_adone_rport,
267 MSGF_MPSAFE, netisr_pollmore);
268 #ifdef INVARIANTS
269 pctx->poll_more_netmsg.nm_lmsg.u.ms_resultp = pctx;
270 #endif
272 KASSERT(cpuid < POLLCTX_MAX, ("cpu id must < %d", cpuid));
273 poll_context[cpuid] = pctx;
275 if (poll_defcpu < 0) {
276 poll_defcpu = cpuid;
279 * Initialize global sysctl nodes, for compat
281 poll_add_sysctl(NULL, SYSCTL_STATIC_CHILDREN(_kern_polling),
282 pctx);
286 * Initialize per-cpu sysctl nodes
288 ksnprintf(cpuid_str, sizeof(cpuid_str), "%d", pctx->poll_cpuid);
290 sysctl_ctx_init(&pctx->poll_sysctl_ctx);
291 pctx->poll_sysctl_tree = SYSCTL_ADD_NODE(&pctx->poll_sysctl_ctx,
292 SYSCTL_STATIC_CHILDREN(_kern_polling),
293 OID_AUTO, cpuid_str, CTLFLAG_RD, 0, "");
294 poll_add_sysctl(&pctx->poll_sysctl_ctx,
295 SYSCTL_CHILDREN(pctx->poll_sysctl_tree), pctx);
298 * Initialize systimer
300 systimer_init_periodic_nq(&pctx->pollclock, pollclock, pctx, 1);
303 static void
304 schedpoll_oncpu(struct netmsg *msg)
306 if (msg->nm_lmsg.ms_flags & MSGF_DONE)
307 lwkt_sendmsg(cpu_portfn(mycpuid), &msg->nm_lmsg);
310 static __inline void
311 schedpoll(struct pollctx *pctx)
313 crit_enter();
314 schedpoll_oncpu(&pctx->poll_netmsg);
315 crit_exit();
318 static __inline void
319 schedpollmore(struct pollctx *pctx)
321 schedpoll_oncpu(&pctx->poll_more_netmsg);
325 * Set the polling frequency
327 static int
328 sysctl_pollhz(SYSCTL_HANDLER_ARGS)
330 struct pollctx *pctx = arg1;
331 struct netmsg msg;
332 lwkt_port_t port;
333 int error, phz;
335 phz = pctx->pollhz;
336 error = sysctl_handle_int(oidp, &phz, 0, req);
337 if (error || req->newptr == NULL)
338 return error;
339 if (phz <= 0)
340 return EINVAL;
341 else if (phz > DEVICE_POLLING_FREQ_MAX)
342 phz = DEVICE_POLLING_FREQ_MAX;
344 netmsg_init(&msg, NULL, &curthread->td_msgport,
345 MSGF_MPSAFE, poll_sysctl_pollhz);
346 msg.nm_lmsg.u.ms_result = phz;
348 port = cpu_portfn(pctx->poll_cpuid);
349 lwkt_domsg(port, &msg.nm_lmsg, 0);
350 return 0;
354 * Master enable.
356 static int
357 sysctl_polling(SYSCTL_HANDLER_ARGS)
359 struct pollctx *pctx = arg1;
360 struct netmsg msg;
361 lwkt_port_t port;
362 int error, enabled;
364 enabled = pctx->polling_enabled;
365 error = sysctl_handle_int(oidp, &enabled, 0, req);
366 if (error || req->newptr == NULL)
367 return error;
369 netmsg_init(&msg, NULL, &curthread->td_msgport,
370 MSGF_MPSAFE, poll_sysctl_polling);
371 msg.nm_lmsg.u.ms_result = enabled;
373 port = cpu_portfn(pctx->poll_cpuid);
374 lwkt_domsg(port, &msg.nm_lmsg, 0);
375 return 0;
378 static int
379 sysctl_regfrac(SYSCTL_HANDLER_ARGS)
381 struct pollctx *pctx = arg1;
382 struct netmsg msg;
383 lwkt_port_t port;
384 uint32_t reg_frac;
385 int error;
387 reg_frac = pctx->reg_frac;
388 error = sysctl_handle_int(oidp, &reg_frac, 0, req);
389 if (error || req->newptr == NULL)
390 return error;
392 netmsg_init(&msg, NULL, &curthread->td_msgport,
393 MSGF_MPSAFE, poll_sysctl_regfrac);
394 msg.nm_lmsg.u.ms_result = reg_frac;
396 port = cpu_portfn(pctx->poll_cpuid);
397 lwkt_domsg(port, &msg.nm_lmsg, 0);
398 return 0;
401 static int
402 sysctl_burstmax(SYSCTL_HANDLER_ARGS)
404 struct pollctx *pctx = arg1;
405 struct netmsg msg;
406 lwkt_port_t port;
407 uint32_t burst_max;
408 int error;
410 burst_max = pctx->poll_burst_max;
411 error = sysctl_handle_int(oidp, &burst_max, 0, req);
412 if (error || req->newptr == NULL)
413 return error;
414 if (burst_max < MIN_POLL_BURST_MAX)
415 burst_max = MIN_POLL_BURST_MAX;
416 else if (burst_max > MAX_POLL_BURST_MAX)
417 burst_max = MAX_POLL_BURST_MAX;
419 netmsg_init(&msg, NULL, &curthread->td_msgport,
420 MSGF_MPSAFE, poll_sysctl_burstmax);
421 msg.nm_lmsg.u.ms_result = burst_max;
423 port = cpu_portfn(pctx->poll_cpuid);
424 lwkt_domsg(port, &msg.nm_lmsg, 0);
425 return 0;
428 static int
429 sysctl_eachburst(SYSCTL_HANDLER_ARGS)
431 struct pollctx *pctx = arg1;
432 struct netmsg msg;
433 lwkt_port_t port;
434 uint32_t each_burst;
435 int error;
437 each_burst = pctx->poll_each_burst;
438 error = sysctl_handle_int(oidp, &each_burst, 0, req);
439 if (error || req->newptr == NULL)
440 return error;
442 netmsg_init(&msg, NULL, &curthread->td_msgport,
443 MSGF_MPSAFE, poll_sysctl_eachburst);
444 msg.nm_lmsg.u.ms_result = each_burst;
446 port = cpu_portfn(pctx->poll_cpuid);
447 lwkt_domsg(port, &msg.nm_lmsg, 0);
448 return 0;
452 * Hook from polling systimer. Tries to schedule a netisr, but keeps
453 * track of lost ticks due to the previous handler taking too long.
454 * Normally, this should not happen, because polling handler should
455 * run for a short time. However, in some cases (e.g. when there are
456 * changes in link status etc.) the drivers take a very long time
457 * (even in the order of milliseconds) to reset and reconfigure the
458 * device, causing apparent lost polls.
460 * The first part of the code is just for debugging purposes, and tries
461 * to count how often hardclock ticks are shorter than they should,
462 * meaning either stray interrupts or delayed events.
464 * WARNING! called from fastint or IPI, the MP lock might not be held.
466 static void
467 pollclock(systimer_t info, struct intrframe *frame __unused)
469 struct pollctx *pctx = info->data;
470 struct timeval t;
471 int delta;
473 if (pctx->poll_handlers == 0)
474 return;
476 microuptime(&t);
477 delta = (t.tv_usec - pctx->prev_t.tv_usec) +
478 (t.tv_sec - pctx->prev_t.tv_sec)*1000000;
479 if (delta * pctx->pollhz < 500000)
480 pctx->short_ticks++;
481 else
482 pctx->prev_t = t;
484 if (pctx->pending_polls > 100) {
486 * Too much, assume it has stalled (not always true
487 * see comment above).
489 pctx->stalled++;
490 pctx->pending_polls = 0;
491 pctx->phase = 0;
494 if (pctx->phase <= 2) {
495 if (pctx->phase != 0)
496 pctx->suspect++;
497 pctx->phase = 1;
498 schedpoll(pctx);
499 pctx->phase = 2;
501 if (pctx->pending_polls++ > 0)
502 pctx->lost_polls++;
506 * netisr_pollmore is called after other netisr's, possibly scheduling
507 * another NETISR_POLL call, or adapting the burst size for the next cycle.
509 * It is very bad to fetch large bursts of packets from a single card at once,
510 * because the burst could take a long time to be completely processed leading
511 * to unfairness. To reduce the problem, and also to account better for time
512 * spent in network-related processing, we split the burst in smaller chunks
513 * of fixed size, giving control to the other netisr's between chunks. This
514 * helps in improving the fairness, reducing livelock (because we emulate more
515 * closely the "process to completion" that we have with fastforwarding) and
516 * accounting for the work performed in low level handling and forwarding.
519 /* ARGSUSED */
520 static void
521 netisr_pollmore(struct netmsg *msg)
523 struct pollctx *pctx;
524 struct timeval t;
525 int kern_load, cpuid;
526 uint32_t pending_polls;
528 cpuid = mycpu->gd_cpuid;
529 KKASSERT(cpuid < POLLCTX_MAX);
531 pctx = poll_context[cpuid];
532 KKASSERT(pctx != NULL);
533 KKASSERT(pctx->poll_cpuid == cpuid);
534 KKASSERT(pctx == msg->nm_lmsg.u.ms_resultp);
536 lwkt_replymsg(&msg->nm_lmsg, 0);
538 if (pctx->poll_handlers == 0)
539 return;
541 KASSERT(pctx->polling_enabled,
542 ("# of registered poll handlers are not zero, "
543 "but polling is not enabled\n"));
545 pctx->phase = 5;
546 if (pctx->residual_burst > 0) {
547 schedpoll(pctx);
548 /* will run immediately on return, followed by netisrs */
549 return;
551 /* here we can account time spent in netisr's in this tick */
552 microuptime(&t);
553 kern_load = (t.tv_usec - pctx->poll_start_t.tv_usec) +
554 (t.tv_sec - pctx->poll_start_t.tv_sec)*1000000; /* us */
555 kern_load = (kern_load * pctx->pollhz) / 10000; /* 0..100 */
556 if (kern_load > (100 - pctx->user_frac)) { /* try decrease ticks */
557 if (pctx->poll_burst > 1)
558 pctx->poll_burst--;
559 } else {
560 if (pctx->poll_burst < pctx->poll_burst_max)
561 pctx->poll_burst++;
564 crit_enter();
565 pctx->pending_polls--;
566 pending_polls = pctx->pending_polls;
567 crit_exit();
569 if (pending_polls == 0) { /* we are done */
570 pctx->phase = 0;
571 } else {
573 * Last cycle was long and caused us to miss one or more
574 * hardclock ticks. Restart processing again, but slightly
575 * reduce the burst size to prevent that this happens again.
577 pctx->poll_burst -= (pctx->poll_burst / 8);
578 if (pctx->poll_burst < 1)
579 pctx->poll_burst = 1;
580 schedpoll(pctx);
581 pctx->phase = 6;
586 * netisr_poll is scheduled by schedpoll when appropriate, typically once
587 * per polling systimer tick.
589 * Note that the message is replied immediately in order to allow a new
590 * ISR to be scheduled in the handler.
592 * XXX each registration should indicate whether it needs a critical
593 * section to operate.
595 /* ARGSUSED */
596 static void
597 netisr_poll(struct netmsg *msg)
599 struct pollctx *pctx;
600 int i, cycles, cpuid;
601 enum poll_cmd arg = POLL_ONLY;
603 cpuid = mycpu->gd_cpuid;
604 KKASSERT(cpuid < POLLCTX_MAX);
606 pctx = poll_context[cpuid];
607 KKASSERT(pctx != NULL);
608 KKASSERT(pctx->poll_cpuid == cpuid);
609 KKASSERT(pctx == msg->nm_lmsg.u.ms_resultp);
611 crit_enter();
612 lwkt_replymsg(&msg->nm_lmsg, 0);
613 crit_exit();
615 if (pctx->poll_handlers == 0)
616 return;
618 KASSERT(pctx->polling_enabled,
619 ("# of registered poll handlers are not zero, "
620 "but polling is not enabled\n"));
622 pctx->phase = 3;
623 if (pctx->residual_burst == 0) { /* first call in this tick */
624 microuptime(&pctx->poll_start_t);
626 if (pctx->reg_frac_count-- == 0) {
627 arg = POLL_AND_CHECK_STATUS;
628 pctx->reg_frac_count = pctx->reg_frac - 1;
631 pctx->residual_burst = pctx->poll_burst;
633 cycles = (pctx->residual_burst < pctx->poll_each_burst) ?
634 pctx->residual_burst : pctx->poll_each_burst;
635 pctx->residual_burst -= cycles;
637 for (i = 0 ; i < pctx->poll_handlers ; i++) {
638 struct ifnet *ifp = pctx->pr[i].ifp;
640 if (!ifnet_tryserialize_main(ifp))
641 continue;
643 if ((ifp->if_flags & (IFF_UP|IFF_RUNNING|IFF_POLLING))
644 == (IFF_UP|IFF_RUNNING|IFF_POLLING)) {
645 logpoll(beg, ifp);
646 crit_enter();
647 ifp->if_poll(ifp, arg, cycles);
648 crit_exit();
649 logpoll(end, ifp);
652 ifnet_deserialize_main(ifp);
655 schedpollmore(pctx);
656 pctx->phase = 4;
659 static void
660 poll_register(struct netmsg *msg)
662 struct ifnet *ifp = msg->nm_lmsg.u.ms_resultp;
663 struct pollctx *pctx;
664 int rc, cpuid;
666 cpuid = mycpu->gd_cpuid;
667 KKASSERT(cpuid < POLLCTX_MAX);
669 pctx = poll_context[cpuid];
670 KKASSERT(pctx != NULL);
671 KKASSERT(pctx->poll_cpuid == cpuid);
673 if (pctx->polling_enabled == 0) {
674 /* Polling disabled, cannot register */
675 rc = EOPNOTSUPP;
676 goto back;
680 * Check if there is room.
682 if (pctx->poll_handlers >= POLL_LIST_LEN) {
684 * List full, cannot register more entries.
685 * This should never happen; if it does, it is probably a
686 * broken driver trying to register multiple times. Checking
687 * this at runtime is expensive, and won't solve the problem
688 * anyways, so just report a few times and then give up.
690 static int verbose = 10; /* XXX */
691 if (verbose >0) {
692 kprintf("poll handlers list full, "
693 "maybe a broken driver ?\n");
694 verbose--;
696 rc = ENOMEM;
697 } else {
698 pctx->pr[pctx->poll_handlers].ifp = ifp;
699 pctx->poll_handlers++;
700 rc = 0;
702 if (pctx->poll_handlers == 1) {
703 KKASSERT(pctx->polling_enabled);
704 systimer_adjust_periodic(&pctx->pollclock,
705 pctx->pollhz);
708 back:
709 lwkt_replymsg(&msg->nm_lmsg, rc);
713 * Try to register routine for polling. Returns 1 if successful
714 * (and polling should be enabled), 0 otherwise.
716 * Called from mainline code only, not called from an interrupt.
719 ether_poll_register(struct ifnet *ifp)
721 if (poll_defcpu < 0)
722 return 0;
723 KKASSERT(poll_defcpu < POLLCTX_MAX);
725 return ether_pollcpu_register(ifp, poll_defcpu);
729 ether_pollcpu_register(struct ifnet *ifp, int cpuid)
731 struct netmsg msg;
732 lwkt_port_t port;
733 int rc;
735 if (ifp->if_poll == NULL) {
736 /* Device does not support polling */
737 return 0;
740 if (cpuid < 0 || cpuid >= POLLCTX_MAX)
741 return 0;
743 if (((1 << cpuid) & poll_cpumask) == 0) {
744 /* Polling is not supported on 'cpuid' */
745 return 0;
747 KKASSERT(poll_context[cpuid] != NULL);
750 * Attempt to register. Interlock with IFF_POLLING.
752 crit_enter(); /* XXX MP - not mp safe */
754 ifnet_serialize_all(ifp);
755 if (ifp->if_flags & IFF_POLLING) {
756 /* Already polling */
757 KKASSERT(ifp->if_poll_cpuid >= 0);
758 ifnet_deserialize_all(ifp);
759 crit_exit();
760 return 0;
762 KKASSERT(ifp->if_poll_cpuid < 0);
763 ifp->if_flags |= IFF_POLLING;
764 ifp->if_poll_cpuid = cpuid;
765 if (ifp->if_flags & IFF_RUNNING)
766 ifp->if_poll(ifp, POLL_REGISTER, 0);
767 ifnet_deserialize_all(ifp);
769 netmsg_init(&msg, NULL, &curthread->td_msgport,
770 MSGF_MPSAFE, poll_register);
771 msg.nm_lmsg.u.ms_resultp = ifp;
773 port = cpu_portfn(cpuid);
774 lwkt_domsg(port, &msg.nm_lmsg, 0);
776 if (msg.nm_lmsg.ms_error) {
777 ifnet_serialize_all(ifp);
778 ifp->if_flags &= ~IFF_POLLING;
779 ifp->if_poll_cpuid = -1;
780 if (ifp->if_flags & IFF_RUNNING)
781 ifp->if_poll(ifp, POLL_DEREGISTER, 0);
782 ifnet_deserialize_all(ifp);
783 rc = 0;
784 } else {
785 rc = 1;
788 crit_exit();
789 return rc;
792 static void
793 poll_deregister(struct netmsg *msg)
795 struct ifnet *ifp = msg->nm_lmsg.u.ms_resultp;
796 struct pollctx *pctx;
797 int rc, i, cpuid;
799 cpuid = mycpu->gd_cpuid;
800 KKASSERT(cpuid < POLLCTX_MAX);
802 pctx = poll_context[cpuid];
803 KKASSERT(pctx != NULL);
804 KKASSERT(pctx->poll_cpuid == cpuid);
806 for (i = 0 ; i < pctx->poll_handlers ; i++) {
807 if (pctx->pr[i].ifp == ifp) /* Found it */
808 break;
810 if (i == pctx->poll_handlers) {
811 kprintf("ether_poll_deregister: ifp not found!!!\n");
812 rc = ENOENT;
813 } else {
814 pctx->poll_handlers--;
815 if (i < pctx->poll_handlers) {
816 /* Last entry replaces this one. */
817 pctx->pr[i].ifp = pctx->pr[pctx->poll_handlers].ifp;
820 if (pctx->poll_handlers == 0) {
821 systimer_adjust_periodic(&pctx->pollclock, 1);
822 poll_reset_state(pctx);
824 rc = 0;
826 lwkt_replymsg(&msg->nm_lmsg, rc);
830 * Remove interface from the polling list. Occurs when polling is turned
831 * off. Called from mainline code only, not called from an interrupt.
834 ether_poll_deregister(struct ifnet *ifp)
836 struct netmsg msg;
837 lwkt_port_t port;
838 int rc, cpuid;
840 KKASSERT(ifp != NULL);
842 if (ifp->if_poll == NULL)
843 return 0;
845 crit_enter();
847 ifnet_serialize_all(ifp);
848 if ((ifp->if_flags & IFF_POLLING) == 0) {
849 KKASSERT(ifp->if_poll_cpuid < 0);
850 ifnet_deserialize_all(ifp);
851 crit_exit();
852 return 0;
855 cpuid = ifp->if_poll_cpuid;
856 KKASSERT(cpuid >= 0);
857 KKASSERT(poll_context[cpuid] != NULL);
859 ifp->if_flags &= ~IFF_POLLING;
860 ifp->if_poll_cpuid = -1;
861 ifnet_deserialize_all(ifp);
863 netmsg_init(&msg, NULL, &curthread->td_msgport,
864 MSGF_MPSAFE, poll_deregister);
865 msg.nm_lmsg.u.ms_resultp = ifp;
867 port = cpu_portfn(cpuid);
868 lwkt_domsg(port, &msg.nm_lmsg, 0);
870 if (!msg.nm_lmsg.ms_error) {
871 ifnet_serialize_all(ifp);
872 if (ifp->if_flags & IFF_RUNNING)
873 ifp->if_poll(ifp, POLL_DEREGISTER, 1);
874 ifnet_deserialize_all(ifp);
875 rc = 1;
876 } else {
877 rc = 0;
880 crit_exit();
881 return rc;
884 static void
885 poll_add_sysctl(struct sysctl_ctx_list *ctx, struct sysctl_oid_list *parent,
886 struct pollctx *pctx)
888 SYSCTL_ADD_PROC(ctx, parent, OID_AUTO, "enable",
889 CTLTYPE_INT | CTLFLAG_RW, pctx, 0, sysctl_polling,
890 "I", "Polling enabled");
892 SYSCTL_ADD_PROC(ctx, parent, OID_AUTO, "pollhz",
893 CTLTYPE_INT | CTLFLAG_RW, pctx, 0, sysctl_pollhz,
894 "I", "Device polling frequency");
896 SYSCTL_ADD_PROC(ctx, parent, OID_AUTO, "reg_frac",
897 CTLTYPE_UINT | CTLFLAG_RW, pctx, 0, sysctl_regfrac,
898 "IU", "Every this many cycles poll register");
900 SYSCTL_ADD_PROC(ctx, parent, OID_AUTO, "burst_max",
901 CTLTYPE_UINT | CTLFLAG_RW, pctx, 0, sysctl_burstmax,
902 "IU", "Max Polling burst size");
904 SYSCTL_ADD_PROC(ctx, parent, OID_AUTO, "each_burst",
905 CTLTYPE_UINT | CTLFLAG_RW, pctx, 0, sysctl_eachburst,
906 "IU", "Max size of each burst");
908 SYSCTL_ADD_UINT(ctx, parent, OID_AUTO, "phase", CTLFLAG_RD,
909 &pctx->phase, 0, "Polling phase");
911 SYSCTL_ADD_UINT(ctx, parent, OID_AUTO, "suspect", CTLFLAG_RW,
912 &pctx->suspect, 0, "suspect event");
914 SYSCTL_ADD_UINT(ctx, parent, OID_AUTO, "stalled", CTLFLAG_RW,
915 &pctx->stalled, 0, "potential stalls");
917 SYSCTL_ADD_UINT(ctx, parent, OID_AUTO, "burst", CTLFLAG_RD,
918 &pctx->poll_burst, 0, "Current polling burst size");
920 SYSCTL_ADD_UINT(ctx, parent, OID_AUTO, "user_frac", CTLFLAG_RW,
921 &pctx->user_frac, 0,
922 "Desired user fraction of cpu time");
924 SYSCTL_ADD_UINT(ctx, parent, OID_AUTO, "short_ticks", CTLFLAG_RW,
925 &pctx->short_ticks, 0,
926 "Hardclock ticks shorter than they should be");
928 SYSCTL_ADD_UINT(ctx, parent, OID_AUTO, "lost_polls", CTLFLAG_RW,
929 &pctx->lost_polls, 0,
930 "How many times we would have lost a poll tick");
932 SYSCTL_ADD_UINT(ctx, parent, OID_AUTO, "pending_polls", CTLFLAG_RD,
933 &pctx->pending_polls, 0, "Do we need to poll again");
935 SYSCTL_ADD_INT(ctx, parent, OID_AUTO, "residual_burst", CTLFLAG_RD,
936 &pctx->residual_burst, 0,
937 "# of residual cycles in burst");
939 SYSCTL_ADD_UINT(ctx, parent, OID_AUTO, "handlers", CTLFLAG_RD,
940 &pctx->poll_handlers, 0,
941 "Number of registered poll handlers");
944 static void
945 poll_sysctl_pollhz(struct netmsg *msg)
947 struct pollctx *pctx;
948 int cpuid;
950 cpuid = mycpu->gd_cpuid;
951 KKASSERT(cpuid < POLLCTX_MAX);
953 pctx = poll_context[cpuid];
954 KKASSERT(pctx != NULL);
955 KKASSERT(pctx->poll_cpuid == cpuid);
958 * If polling is disabled or there is no device registered,
959 * don't adjust polling systimer frequency.
960 * Polling systimer frequency will be adjusted once polling
961 * is enabled and there are registered devices.
963 pctx->pollhz = msg->nm_lmsg.u.ms_result;
964 if (pctx->polling_enabled && pctx->poll_handlers)
965 systimer_adjust_periodic(&pctx->pollclock, pctx->pollhz);
968 * Make sure that reg_frac and reg_frac_count are within valid range.
970 if (pctx->reg_frac > pctx->pollhz) {
971 pctx->reg_frac = pctx->pollhz;
972 if (pctx->reg_frac_count > pctx->reg_frac)
973 pctx->reg_frac_count = pctx->reg_frac - 1;
976 lwkt_replymsg(&msg->nm_lmsg, 0);
979 static void
980 poll_sysctl_polling(struct netmsg *msg)
982 struct pollctx *pctx;
983 int cpuid;
985 cpuid = mycpu->gd_cpuid;
986 KKASSERT(cpuid < POLLCTX_MAX);
988 pctx = poll_context[cpuid];
989 KKASSERT(pctx != NULL);
990 KKASSERT(pctx->poll_cpuid == cpuid);
993 * If polling is disabled or there is no device registered,
994 * cut the polling systimer frequency to 1hz.
996 pctx->polling_enabled = msg->nm_lmsg.u.ms_result;
997 if (pctx->polling_enabled && pctx->poll_handlers) {
998 systimer_adjust_periodic(&pctx->pollclock, pctx->pollhz);
999 } else {
1000 systimer_adjust_periodic(&pctx->pollclock, 1);
1001 poll_reset_state(pctx);
1004 if (!pctx->polling_enabled && pctx->poll_handlers != 0) {
1005 int i;
1007 for (i = 0 ; i < pctx->poll_handlers ; i++) {
1008 struct ifnet *ifp = pctx->pr[i].ifp;
1010 ifnet_serialize_all(ifp);
1012 if ((ifp->if_flags & IFF_POLLING) == 0) {
1013 KKASSERT(ifp->if_poll_cpuid < 0);
1014 ifnet_deserialize_all(ifp);
1015 continue;
1017 ifp->if_flags &= ~IFF_POLLING;
1018 ifp->if_poll_cpuid = -1;
1021 * Only call the interface deregistration
1022 * function if the interface is still
1023 * running.
1025 if (ifp->if_flags & IFF_RUNNING)
1026 ifp->if_poll(ifp, POLL_DEREGISTER, 1);
1028 ifnet_deserialize_all(ifp);
1030 pctx->poll_handlers = 0;
1033 lwkt_replymsg(&msg->nm_lmsg, 0);
1036 static void
1037 poll_sysctl_regfrac(struct netmsg *msg)
1039 struct pollctx *pctx;
1040 uint32_t reg_frac;
1041 int cpuid;
1043 cpuid = mycpu->gd_cpuid;
1044 KKASSERT(cpuid < POLLCTX_MAX);
1046 pctx = poll_context[cpuid];
1047 KKASSERT(pctx != NULL);
1048 KKASSERT(pctx->poll_cpuid == cpuid);
1050 reg_frac = msg->nm_lmsg.u.ms_result;
1051 if (reg_frac > pctx->pollhz)
1052 reg_frac = pctx->pollhz;
1053 else if (reg_frac < 1)
1054 reg_frac = 1;
1056 pctx->reg_frac = reg_frac;
1057 if (pctx->reg_frac_count > pctx->reg_frac)
1058 pctx->reg_frac_count = pctx->reg_frac - 1;
1060 lwkt_replymsg(&msg->nm_lmsg, 0);
1063 static void
1064 poll_sysctl_burstmax(struct netmsg *msg)
1066 struct pollctx *pctx;
1067 int cpuid;
1069 cpuid = mycpu->gd_cpuid;
1070 KKASSERT(cpuid < POLLCTX_MAX);
1072 pctx = poll_context[cpuid];
1073 KKASSERT(pctx != NULL);
1074 KKASSERT(pctx->poll_cpuid == cpuid);
1076 pctx->poll_burst_max = msg->nm_lmsg.u.ms_result;
1077 if (pctx->poll_each_burst > pctx->poll_burst_max)
1078 pctx->poll_each_burst = pctx->poll_burst_max;
1079 if (pctx->poll_burst > pctx->poll_burst_max)
1080 pctx->poll_burst = pctx->poll_burst_max;
1081 if (pctx->residual_burst > pctx->poll_burst_max)
1082 pctx->residual_burst = pctx->poll_burst_max;
1084 lwkt_replymsg(&msg->nm_lmsg, 0);
1087 static void
1088 poll_sysctl_eachburst(struct netmsg *msg)
1090 struct pollctx *pctx;
1091 uint32_t each_burst;
1092 int cpuid;
1094 cpuid = mycpu->gd_cpuid;
1095 KKASSERT(cpuid < POLLCTX_MAX);
1097 pctx = poll_context[cpuid];
1098 KKASSERT(pctx != NULL);
1099 KKASSERT(pctx->poll_cpuid == cpuid);
1101 each_burst = msg->nm_lmsg.u.ms_result;
1102 if (each_burst > pctx->poll_burst_max)
1103 each_burst = pctx->poll_burst_max;
1104 else if (each_burst < 1)
1105 each_burst = 1;
1106 pctx->poll_each_burst = each_burst;
1108 lwkt_replymsg(&msg->nm_lmsg, 0);