ifpoll: Put pollmore under crit section
[dragonfly.git] / sys / net / if_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_ifpoll.h"
31
32 #include <sys/param.h>
33 #include <sys/kernel.h>
34 #include <sys/ktr.h>
35 #include <sys/malloc.h>
36 #include <sys/serialize.h>
37 #include <sys/socket.h>
38 #include <sys/sysctl.h>
39
40 #include <sys/thread2.h>
41 #include <sys/msgport2.h>
42
43 #include <machine/atomic.h>
44 #include <machine/smp.h>
45
46 #include <net/if.h>
47 #include <net/if_poll.h>
48 #include <net/netmsg2.h>
49
50 /*
51  * Polling support for network device drivers.
52  *
53  * Drivers which support this feature try to register one status polling
54  * handler and several TX/RX polling handlers with the polling code.
55  * If interface's if_qpoll is called with non-NULL second argument, then
56  * a register operation is requested, else a deregister operation is
57  * requested.  If the requested operation is "register", driver should
58  * setup the ifpoll_info passed in accoding its own needs:
59  *   ifpoll_info.ifpi_status.status_func == NULL
60  *     No status polling handler will be installed on CPU(0)
61  *   ifpoll_info.ifpi_rx[n].poll_func == NULL
62  *     No RX polling handler will be installed on CPU(n)
63  *   ifpoll_info.ifpi_tx[n].poll_func == NULL
64  *     No TX polling handler will be installed on CPU(n)
65  *
66  * All of the registered polling handlers are called only if the interface
67  * is marked as 'IFF_RUNNING and IFF_NPOLLING'.  However, the interface's
68  * register and deregister function (ifnet.if_qpoll) will be called even
69  * if interface is not marked with 'IFF_RUNNING'.
70  *
71  * If registration is successful, the driver must disable interrupts,
72  * and further I/O is performed through the TX/RX polling handler, which
73  * are invoked (at least once per clock tick) with 3 arguments: the "arg"
74  * passed at register time, a struct ifnet pointer, and a "count" limit.
75  * The registered serializer will be held before calling the related
76  * polling handler.
77  *
78  * The count limit specifies how much work the handler can do during the
79  * call -- typically this is the number of packets to be received, or
80  * transmitted, etc. (drivers are free to interpret this number, as long
81  * as the max time spent in the function grows roughly linearly with the
82  * count).
83  *
84  * A second variable controls the sharing of CPU between polling/kernel
85  * network processing, and other activities (typically userlevel tasks):
86  * net.ifpoll.{rxX,txX}.user_frac (between 0 and 100, default 50) sets the
87  * share of CPU allocated to user tasks.  CPU is allocated proportionally
88  * to the shares, by dynamically adjusting the "count" (poll_burst).
89  *
90  * Other parameters can should be left to their default values.
91  * The following constraints hold
92  *
93  *      1 <= poll_burst <= poll_burst_max
94  *      1 <= poll_each_burst <= poll_burst_max
95  *      MIN_POLL_BURST_MAX <= poll_burst_max <= MAX_POLL_BURST_MAX
96  */
97
98 #define IFPOLL_LIST_LEN         128
99 #define IFPOLL_FREQ_MAX         30000
100
101 #define MIN_IOPOLL_BURST_MAX    10
102 #define MAX_IOPOLL_BURST_MAX    1000
103 #define IOPOLL_BURST_MAX        150     /* good for 100Mbit net and HZ=1000 */
104
105 #define IOPOLL_EACH_BURST       5
106
107 #define IFPOLL_FREQ_DEFAULT     2000
108 #define IOPOLL_FREQ_DEFAULT     IFPOLL_FREQ_DEFAULT
109 #define STPOLL_FREQ_DEFAULT     100
110
111 #define IFPOLL_TXFRAC_DEFAULT   1
112 #define IFPOLL_STFRAC_DEFAULT   20
113
114 #define IFPOLL_RX               0x1
115 #define IFPOLL_TX               0x2
116
117 struct iopoll_rec {
118         struct lwkt_serialize   *serializer;
119         struct ifnet            *ifp;
120         void                    *arg;
121         ifpoll_iofn_t           poll_func;
122 };
123
124 struct iopoll_ctx {
125 #ifdef IFPOLL_MULTI_SYSTIMER
126         struct systimer         pollclock;
127 #endif
128
129         struct timeval          prev_t;                 /* state */
130         uint32_t                short_ticks;            /* statistics */
131         uint32_t                lost_polls;             /* statistics */
132         uint32_t                suspect;                /* statistics */
133         uint32_t                stalled;                /* statistics */
134         uint32_t                pending_polls;          /* state */
135
136         struct netmsg           poll_netmsg;
137
138         int                     poll_cpuid;
139 #ifdef IFPOLL_MULTI_SYSTIMER
140         int                     pollhz;                 /* tunable */
141 #else
142         int                     poll_type;              /* IFPOLL_{RX,TX} */
143 #endif
144         uint32_t                phase;                  /* state */
145         int                     residual_burst;         /* state */
146         uint32_t                poll_each_burst;        /* tunable */
147         struct timeval          poll_start_t;           /* state */
148
149         uint32_t                poll_handlers; /* next free entry in pr[]. */
150         struct iopoll_rec       pr[IFPOLL_LIST_LEN];
151
152         struct netmsg           poll_more_netmsg;
153
154         uint32_t                poll_burst;             /* state */
155         uint32_t                poll_burst_max;         /* tunable */
156         uint32_t                user_frac;              /* tunable */
157
158         struct sysctl_ctx_list  poll_sysctl_ctx;
159         struct sysctl_oid       *poll_sysctl_tree;
160 } __cachealign;
161
162 struct stpoll_rec {
163         struct lwkt_serialize   *serializer;
164         struct ifnet            *ifp;
165         ifpoll_stfn_t           status_func;
166 };
167
168 struct stpoll_ctx {
169 #ifdef IFPOLL_MULTI_SYSTIMER
170         struct systimer         pollclock;
171 #endif
172
173         struct netmsg           poll_netmsg;
174
175 #ifdef IFPOLL_MULTI_SYSTIMER
176         int                     pollhz;                 /* tunable */
177 #endif
178         uint32_t                poll_handlers; /* next free entry in pr[]. */
179         struct stpoll_rec       pr[IFPOLL_LIST_LEN];
180
181         struct sysctl_ctx_list  poll_sysctl_ctx;
182         struct sysctl_oid       *poll_sysctl_tree;
183 };
184
185 struct iopoll_sysctl_netmsg {
186         struct netmsg           nmsg;
187         struct iopoll_ctx       *ctx;
188 };
189
190 #ifndef IFPOLL_MULTI_SYSTIMER
191
192 struct ifpoll_data {
193         struct systimer clock;
194         int             txfrac_count;
195         int             stfrac_count;
196         u_int           tx_cpumask;
197         u_int           rx_cpumask;
198 } __cachealign;
199
200 #endif
201
202 static struct stpoll_ctx        stpoll_context;
203 static struct iopoll_ctx        *rxpoll_context[IFPOLL_CTX_MAX];
204 static struct iopoll_ctx        *txpoll_context[IFPOLL_CTX_MAX];
205
206 SYSCTL_NODE(_net, OID_AUTO, ifpoll, CTLFLAG_RW, 0,
207             "Network device polling parameters");
208
209 static int      ifpoll_ncpus = IFPOLL_CTX_MAX;
210
211 static int      iopoll_burst_max = IOPOLL_BURST_MAX;
212 static int      iopoll_each_burst = IOPOLL_EACH_BURST;
213
214 TUNABLE_INT("net.ifpoll.burst_max", &iopoll_burst_max);
215 TUNABLE_INT("net.ifpoll.each_burst", &iopoll_each_burst);
216
217 #ifdef IFPOLL_MULTI_SYSTIMER
218
219 static int      stpoll_hz = STPOLL_FREQ_DEFAULT;
220 static int      iopoll_hz = IOPOLL_FREQ_DEFAULT;
221
222 TUNABLE_INT("net.ifpoll.stpoll_hz", &stpoll_hz);
223 TUNABLE_INT("net.ifpoll.iopoll_hz", &iopoll_hz);
224
225 #else   /* !IFPOLL_MULTI_SYSTIMER */
226
227 static struct ifpoll_data ifpoll0;
228 static int      ifpoll_pollhz = IFPOLL_FREQ_DEFAULT;
229 static int      ifpoll_stfrac = IFPOLL_STFRAC_DEFAULT;
230 static int      ifpoll_txfrac = IFPOLL_TXFRAC_DEFAULT;
231 static int      ifpoll_handlers;
232
233 TUNABLE_INT("net.ifpoll.pollhz", &ifpoll_pollhz);
234 TUNABLE_INT("net.ifpoll.status_frac", &ifpoll_stfrac);
235 TUNABLE_INT("net.ifpoll.tx_frac", &ifpoll_txfrac);
236
237 static void     sysctl_ifpollhz_handler(struct netmsg *);
238 static int      sysctl_ifpollhz(SYSCTL_HANDLER_ARGS);
239
240 SYSCTL_PROC(_net_ifpoll, OID_AUTO, pollhz, CTLTYPE_INT | CTLFLAG_RW,
241             0, 0, sysctl_ifpollhz, "I", "Polling frequency");
242 SYSCTL_INT(_net_ifpoll, OID_AUTO, tx_frac, CTLFLAG_RW,
243            &ifpoll_txfrac, 0, "Every this many cycles poll transmit");
244 SYSCTL_INT(_net_ifpoll, OID_AUTO, st_frac, CTLFLAG_RW,
245            &ifpoll_stfrac, 0, "Every this many cycles poll status");
246
247 #endif  /* IFPOLL_MULTI_SYSTIMER */
248
249 void            ifpoll_init_pcpu(int);
250
251 #ifndef IFPOLL_MULTI_SYSTIMER
252 static void     ifpoll_start_handler(struct netmsg *);
253 static void     ifpoll_stop_handler(struct netmsg *);
254 static void     ifpoll_handler_addevent(void);
255 static void     ifpoll_handler_delevent(void);
256 static void     ifpoll_ipi_handler(void *, int);
257 static void     ifpoll_systimer(systimer_t, struct intrframe *);
258 #endif
259
260 static void     ifpoll_register_handler(struct netmsg *);
261 static void     ifpoll_deregister_handler(struct netmsg *);
262
263 /*
264  * Status polling
265  */
266 static void     stpoll_init(void);
267 static void     stpoll_handler(struct netmsg *);
268 static void     stpoll_clock(struct stpoll_ctx *);
269 #ifdef IFPOLL_MULTI_SYSTIMER
270 static void     stpoll_systimer(systimer_t, struct intrframe *);
271 #endif
272 static int      stpoll_register(struct ifnet *, const struct ifpoll_status *);
273 static int      stpoll_deregister(struct ifnet *);
274
275 #ifdef IFPOLL_MULTI_SYSTIMER
276 static void     sysctl_stpollhz_handler(struct netmsg *);
277 static int      sysctl_stpollhz(SYSCTL_HANDLER_ARGS);
278 #endif
279
280 /*
281  * RX/TX polling
282  */
283 static struct iopoll_ctx *iopoll_ctx_create(int, int);
284 static void     iopoll_init(int);
285 static void     iopoll_handler(struct netmsg *);
286 static void     iopollmore_handler(struct netmsg *);
287 static void     iopoll_clock(struct iopoll_ctx *);
288 #ifdef IFPOLL_MULTI_SYSTIMER
289 static void     iopoll_systimer(systimer_t, struct intrframe *);
290 #endif
291 static int      iopoll_register(struct ifnet *, struct iopoll_ctx *,
292                     const struct ifpoll_io *);
293 static int      iopoll_deregister(struct ifnet *, struct iopoll_ctx *);
294
295 static void     iopoll_add_sysctl(struct sysctl_ctx_list *,
296                     struct sysctl_oid_list *, struct iopoll_ctx *);
297 #ifdef IFPOLL_MULTI_SYSTIMER
298 static void     sysctl_iopollhz_handler(struct netmsg *);
299 static int      sysctl_iopollhz(SYSCTL_HANDLER_ARGS);
300 #endif
301 static void     sysctl_burstmax_handler(struct netmsg *);
302 static int      sysctl_burstmax(SYSCTL_HANDLER_ARGS);
303 static void     sysctl_eachburst_handler(struct netmsg *);
304 static int      sysctl_eachburst(SYSCTL_HANDLER_ARGS);
305
306 static __inline void
307 ifpoll_sendmsg_oncpu(struct netmsg *msg)
308 {
309         if (msg->nm_lmsg.ms_flags & MSGF_DONE)
310                 ifnet_sendmsg(&msg->nm_lmsg, mycpuid);
311 }
312
313 static __inline void
314 sched_stpoll(struct stpoll_ctx *st_ctx)
315 {
316         ifpoll_sendmsg_oncpu(&st_ctx->poll_netmsg);
317 }
318
319 static __inline void
320 sched_iopoll(struct iopoll_ctx *io_ctx)
321 {
322         ifpoll_sendmsg_oncpu(&io_ctx->poll_netmsg);
323 }
324
325 static __inline void
326 sched_iopollmore(struct iopoll_ctx *io_ctx)
327 {
328         ifpoll_sendmsg_oncpu(&io_ctx->poll_more_netmsg);
329 }
330
331 /*
332  * Initialize per-cpu qpolling(4) context.  Called from kern_clock.c:
333  */
334 void
335 ifpoll_init_pcpu(int cpuid)
336 {
337         if (cpuid >= IFPOLL_CTX_MAX) {
338                 return;
339         } else if (cpuid == 0) {
340                 if (ifpoll_ncpus > ncpus)
341                         ifpoll_ncpus = ncpus;
342                 kprintf("ifpoll_ncpus %d\n", ifpoll_ncpus);
343
344 #ifndef IFPOLL_MULTI_SYSTIMER
345                 systimer_init_periodic_nq(&ifpoll0.clock,
346                                           ifpoll_systimer, NULL, 1);
347 #endif
348
349                 stpoll_init();
350         }
351         iopoll_init(cpuid);
352 }
353
354 #ifndef IFPOLL_MULTI_SYSTIMER
355
356 static void
357 ifpoll_ipi_handler(void *arg __unused, int poll)
358 {
359         KKASSERT(mycpuid < ifpoll_ncpus);
360
361         if (poll & IFPOLL_TX)
362                 iopoll_clock(txpoll_context[mycpuid]);
363         if (poll & IFPOLL_RX)
364                 iopoll_clock(rxpoll_context[mycpuid]);
365 }
366
367 static void
368 ifpoll_systimer(systimer_t info __unused, struct intrframe *frame __unused)
369 {
370         uint32_t cpumask = 0;
371
372         KKASSERT(mycpuid == 0);
373
374         if (ifpoll0.stfrac_count-- == 0) {
375                 ifpoll0.stfrac_count = ifpoll_stfrac;
376                 stpoll_clock(&stpoll_context);
377         }
378
379         if (ifpoll0.txfrac_count-- == 0) {
380                 ifpoll0.txfrac_count = ifpoll_txfrac;
381
382                 /* TODO: We may try to piggyback TX on RX */
383                 cpumask = smp_active_mask & ifpoll0.tx_cpumask;
384                 if (cpumask != 0) {
385                         lwkt_send_ipiq2_mask(cpumask, ifpoll_ipi_handler,
386                                              NULL, IFPOLL_TX);
387                 }
388         }
389
390         cpumask = smp_active_mask & ifpoll0.rx_cpumask;
391         if (cpumask != 0) {
392                 lwkt_send_ipiq2_mask(cpumask, ifpoll_ipi_handler,
393                                      NULL, IFPOLL_RX);
394         }
395 }
396
397 static void
398 ifpoll_start_handler(struct netmsg *nmsg)
399 {
400         KKASSERT(&curthread->td_msgport == ifnet_portfn(0));
401
402         kprintf("ifpoll: start\n");
403         systimer_adjust_periodic(&ifpoll0.clock, ifpoll_pollhz);
404         lwkt_replymsg(&nmsg->nm_lmsg, 0);
405 }
406
407 static void
408 ifpoll_stop_handler(struct netmsg *nmsg)
409 {
410         KKASSERT(&curthread->td_msgport == ifnet_portfn(0));
411
412         kprintf("ifpoll: stop\n");
413         systimer_adjust_periodic(&ifpoll0.clock, 1);
414         lwkt_replymsg(&nmsg->nm_lmsg, 0);
415 }
416
417 static void
418 ifpoll_handler_addevent(void)
419 {
420         if (atomic_fetchadd_int(&ifpoll_handlers, 1) == 0) {
421                 struct netmsg *nmsg;
422
423                 /* Start systimer */
424                 nmsg = kmalloc(sizeof(*nmsg), M_LWKTMSG, M_WAITOK);
425                 netmsg_init(nmsg, &netisr_afree_rport, 0, ifpoll_start_handler);
426                 ifnet_sendmsg(&nmsg->nm_lmsg, 0);
427         }
428 }
429
430 static void
431 ifpoll_handler_delevent(void)
432 {
433         KKASSERT(ifpoll_handlers > 0);
434         if (atomic_fetchadd_int(&ifpoll_handlers, -1) == 1) {
435                 struct netmsg *nmsg;
436
437                 /* Stop systimer */
438                 nmsg = kmalloc(sizeof(*nmsg), M_LWKTMSG, M_WAITOK);
439                 netmsg_init(nmsg, &netisr_afree_rport, 0, ifpoll_stop_handler);
440                 ifnet_sendmsg(&nmsg->nm_lmsg, 0);
441         }
442 }
443
444 static void
445 sysctl_ifpollhz_handler(struct netmsg *nmsg)
446 {
447         KKASSERT(&curthread->td_msgport == ifnet_portfn(0));
448
449         /*
450          * If there is no handler registered, don't adjust polling
451          * systimer frequency; polling systimer frequency will be
452          * adjusted once there is registered handler.
453          */
454         ifpoll_pollhz = nmsg->nm_lmsg.u.ms_result;
455         if (ifpoll_handlers)
456                 systimer_adjust_periodic(&ifpoll0.clock, ifpoll_pollhz);
457
458         lwkt_replymsg(&nmsg->nm_lmsg, 0);
459 }
460
461 static int
462 sysctl_ifpollhz(SYSCTL_HANDLER_ARGS)
463 {
464         struct netmsg nmsg;
465         int error, phz;
466
467         phz = ifpoll_pollhz;
468         error = sysctl_handle_int(oidp, &phz, 0, req);
469         if (error || req->newptr == NULL)
470                 return error;
471         if (phz <= 0)
472                 return EINVAL;
473         else if (phz > IFPOLL_FREQ_MAX)
474                 phz = IFPOLL_FREQ_MAX;
475
476         netmsg_init(&nmsg, &curthread->td_msgport, MSGF_MPSAFE,
477                     sysctl_ifpollhz_handler);
478         nmsg.nm_lmsg.u.ms_result = phz;
479
480         return ifnet_domsg(&nmsg.nm_lmsg, 0);
481 }
482
483 #endif  /* !IFPOLL_MULTI_SYSTIMER */
484
485 int
486 ifpoll_register(struct ifnet *ifp)
487 {
488         struct ifpoll_info info;
489         struct netmsg nmsg;
490         int error;
491
492         if (ifp->if_qpoll == NULL) {
493                 /* Device does not support polling */
494                 return EOPNOTSUPP;
495         }
496
497         /*
498          * Attempt to register.  Interlock with IFF_NPOLLING.
499          */
500
501         ifnet_serialize_all(ifp);
502
503         if (ifp->if_flags & IFF_NPOLLING) {
504                 /* Already polling */
505                 ifnet_deserialize_all(ifp);
506                 return EBUSY;
507         }
508
509         bzero(&info, sizeof(info));
510         info.ifpi_ifp = ifp;
511
512         ifp->if_flags |= IFF_NPOLLING;
513         ifp->if_qpoll(ifp, &info);
514
515         ifnet_deserialize_all(ifp);
516
517         netmsg_init(&nmsg, &curthread->td_msgport, MSGF_MPSAFE,
518                     ifpoll_register_handler);
519         nmsg.nm_lmsg.u.ms_resultp = &info;
520
521         error = ifnet_domsg(&nmsg.nm_lmsg, 0);
522         if (error) {
523                 if (!ifpoll_deregister(ifp)) {
524                         if_printf(ifp, "ifpoll_register: "
525                                   "ifpoll_deregister failed!\n");
526                 }
527         }
528         return error;
529 }
530
531 int
532 ifpoll_deregister(struct ifnet *ifp)
533 {
534         struct netmsg nmsg;
535         int error;
536
537         if (ifp->if_qpoll == NULL)
538                 return EOPNOTSUPP;
539
540         ifnet_serialize_all(ifp);
541
542         if ((ifp->if_flags & IFF_NPOLLING) == 0) {
543                 ifnet_deserialize_all(ifp);
544                 return EINVAL;
545         }
546         ifp->if_flags &= ~IFF_NPOLLING;
547
548         ifnet_deserialize_all(ifp);
549
550         netmsg_init(&nmsg, &curthread->td_msgport, MSGF_MPSAFE,
551                     ifpoll_deregister_handler);
552         nmsg.nm_lmsg.u.ms_resultp = ifp;
553
554         error = ifnet_domsg(&nmsg.nm_lmsg, 0);
555         if (!error) {
556                 ifnet_serialize_all(ifp);
557                 ifp->if_qpoll(ifp, NULL);
558                 ifnet_deserialize_all(ifp);
559         }
560         return error;
561 }
562
563 static void
564 ifpoll_register_handler(struct netmsg *nmsg)
565 {
566         const struct ifpoll_info *info = nmsg->nm_lmsg.u.ms_resultp;
567         int cpuid = mycpuid, nextcpu;
568         int error;
569
570         KKASSERT(cpuid < ifpoll_ncpus);
571         KKASSERT(&curthread->td_msgport == ifnet_portfn(cpuid));
572
573         if (cpuid == 0) {
574                 error = stpoll_register(info->ifpi_ifp, &info->ifpi_status);
575                 if (error)
576                         goto failed;
577         }
578
579         error = iopoll_register(info->ifpi_ifp, rxpoll_context[cpuid],
580                                 &info->ifpi_rx[cpuid]);
581         if (error)
582                 goto failed;
583
584         error = iopoll_register(info->ifpi_ifp, txpoll_context[cpuid],
585                                 &info->ifpi_tx[cpuid]);
586         if (error)
587                 goto failed;
588
589         nextcpu = cpuid + 1;
590         if (nextcpu < ifpoll_ncpus)
591                 ifnet_forwardmsg(&nmsg->nm_lmsg, nextcpu);
592         else
593                 lwkt_replymsg(&nmsg->nm_lmsg, 0);
594         return;
595 failed:
596         lwkt_replymsg(&nmsg->nm_lmsg, error);
597 }
598
599 static void
600 ifpoll_deregister_handler(struct netmsg *nmsg)
601 {
602         struct ifnet *ifp = nmsg->nm_lmsg.u.ms_resultp;
603         int cpuid = mycpuid, nextcpu;
604
605         KKASSERT(cpuid < ifpoll_ncpus);
606         KKASSERT(&curthread->td_msgport == ifnet_portfn(cpuid));
607
608         /* Ignore errors */
609         if (cpuid == 0)
610                 stpoll_deregister(ifp);
611         iopoll_deregister(ifp, rxpoll_context[cpuid]);
612         iopoll_deregister(ifp, txpoll_context[cpuid]);
613
614         nextcpu = cpuid + 1;
615         if (nextcpu < ifpoll_ncpus)
616                 ifnet_forwardmsg(&nmsg->nm_lmsg, nextcpu);
617         else
618                 lwkt_replymsg(&nmsg->nm_lmsg, 0);
619 }
620
621 static void
622 stpoll_init(void)
623 {
624         struct stpoll_ctx *st_ctx = &stpoll_context;
625
626 #ifdef IFPOLL_MULTI_SYSTIMER
627         st_ctx->pollhz = stpoll_hz;
628 #endif
629
630         sysctl_ctx_init(&st_ctx->poll_sysctl_ctx);
631         st_ctx->poll_sysctl_tree = SYSCTL_ADD_NODE(&st_ctx->poll_sysctl_ctx,
632                                    SYSCTL_STATIC_CHILDREN(_net_ifpoll),
633                                    OID_AUTO, "status", CTLFLAG_RD, 0, "");
634
635 #ifdef IFPOLL_MULTI_SYSTIMER
636         SYSCTL_ADD_PROC(&st_ctx->poll_sysctl_ctx,
637                         SYSCTL_CHILDREN(st_ctx->poll_sysctl_tree),
638                         OID_AUTO, "pollhz", CTLTYPE_INT | CTLFLAG_RW,
639                         st_ctx, 0, sysctl_stpollhz, "I",
640                         "Status polling frequency");
641 #endif
642
643         SYSCTL_ADD_UINT(&st_ctx->poll_sysctl_ctx,
644                         SYSCTL_CHILDREN(st_ctx->poll_sysctl_tree),
645                         OID_AUTO, "handlers", CTLFLAG_RD,
646                         &st_ctx->poll_handlers, 0,
647                         "Number of registered status poll handlers");
648
649         netmsg_init(&st_ctx->poll_netmsg, &netisr_adone_rport, MSGF_MPSAFE,
650                     stpoll_handler);
651
652 #ifdef IFPOLL_MULTI_SYSTIMER
653         systimer_init_periodic_nq(&st_ctx->pollclock,
654                                   stpoll_systimer, st_ctx, 1);
655 #endif
656 }
657
658 #ifdef IFPOLL_MULTI_SYSTIMER
659
660 static void
661 sysctl_stpollhz_handler(struct netmsg *msg)
662 {
663         struct stpoll_ctx *st_ctx = &stpoll_context;
664
665         KKASSERT(&curthread->td_msgport == ifnet_portfn(0));
666
667         /*
668          * If there is no handler registered, don't adjust polling
669          * systimer frequency; polling systimer frequency will be
670          * adjusted once there is registered handler.
671          */
672         st_ctx->pollhz = msg->nm_lmsg.u.ms_result;
673         if (st_ctx->poll_handlers)
674                 systimer_adjust_periodic(&st_ctx->pollclock, st_ctx->pollhz);
675
676         lwkt_replymsg(&msg->nm_lmsg, 0);
677 }
678
679 static int
680 sysctl_stpollhz(SYSCTL_HANDLER_ARGS)
681 {
682         struct stpoll_ctx *st_ctx = arg1;
683         struct netmsg msg;
684         int error, phz;
685
686         phz = st_ctx->pollhz;
687         error = sysctl_handle_int(oidp, &phz, 0, req);
688         if (error || req->newptr == NULL)
689                 return error;
690         if (phz <= 0)
691                 return EINVAL;
692         else if (phz > IFPOLL_FREQ_MAX)
693                 phz = IFPOLL_FREQ_MAX;
694
695         netmsg_init(&msg, &curthread->td_msgport, MSGF_MPSAFE,
696                     sysctl_stpollhz_handler);
697         msg.nm_lmsg.u.ms_result = phz;
698
699         return ifnet_domsg(&msg.nm_lmsg, 0);
700 }
701
702 #endif  /* IFPOLL_MULTI_SYSTIMER */
703
704 /*
705  * stpoll_handler is scheduled by sched_stpoll when appropriate, typically
706  * once per polling systimer tick.
707  */
708 static void
709 stpoll_handler(struct netmsg *msg)
710 {
711         struct stpoll_ctx *st_ctx = &stpoll_context;
712         struct thread *td = curthread;
713         int i, poll_hz;
714
715         KKASSERT(&td->td_msgport == ifnet_portfn(0));
716
717         crit_enter_quick(td);
718
719         /* Reply ASAP */
720         lwkt_replymsg(&msg->nm_lmsg, 0);
721
722         if (st_ctx->poll_handlers == 0) {
723                 crit_exit_quick(td);
724                 return;
725         }
726
727 #ifdef IFPOLL_MULTI_SYSTIMER
728         poll_hz = st_ctx->pollhz;
729 #else
730         poll_hz = ifpoll_pollhz / (ifpoll_stfrac + 1);
731 #endif
732
733         for (i = 0; i < st_ctx->poll_handlers; ++i) {
734                 const struct stpoll_rec *rec = &st_ctx->pr[i];
735                 struct ifnet *ifp = rec->ifp;
736
737                 if (!lwkt_serialize_try(rec->serializer))
738                         continue;
739
740                 if ((ifp->if_flags & (IFF_RUNNING | IFF_NPOLLING)) ==
741                     (IFF_RUNNING | IFF_NPOLLING))
742                         rec->status_func(ifp, poll_hz);
743
744                 lwkt_serialize_exit(rec->serializer);
745         }
746
747         crit_exit_quick(td);
748 }
749
750 /*
751  * Hook from status poll systimer.  Tries to schedule an status poll.
752  */
753 static void
754 stpoll_clock(struct stpoll_ctx *st_ctx)
755 {
756         globaldata_t gd = mycpu;
757
758         KKASSERT(gd->gd_cpuid == 0);
759
760         if (st_ctx->poll_handlers == 0)
761                 return;
762
763         crit_enter_gd(gd);
764         sched_stpoll(st_ctx);
765         crit_exit_gd(gd);
766 }
767
768 #ifdef IFPOLL_MULTI_SYSTIMER
769 static void
770 stpoll_systimer(systimer_t info, struct intrframe *frame __unused)
771 {
772         stpoll_clock(info->data);
773 }
774 #endif
775
776 static int
777 stpoll_register(struct ifnet *ifp, const struct ifpoll_status *st_rec)
778 {
779         struct stpoll_ctx *st_ctx = &stpoll_context;
780         int error;
781
782         KKASSERT(&curthread->td_msgport == ifnet_portfn(0));
783
784         if (st_rec->status_func == NULL)
785                 return 0;
786
787         /*
788          * Check if there is room.
789          */
790         if (st_ctx->poll_handlers >= IFPOLL_LIST_LEN) {
791                 /*
792                  * List full, cannot register more entries.
793                  * This should never happen; if it does, it is probably a
794                  * broken driver trying to register multiple times. Checking
795                  * this at runtime is expensive, and won't solve the problem
796                  * anyways, so just report a few times and then give up.
797                  */
798                 static int verbose = 10; /* XXX */
799
800                 if (verbose > 0) {
801                         kprintf("status poll handlers list full, "
802                                 "maybe a broken driver ?\n");
803                         verbose--;
804                 }
805                 error = ENOENT;
806         } else {
807                 struct stpoll_rec *rec = &st_ctx->pr[st_ctx->poll_handlers];
808
809                 rec->ifp = ifp;
810                 rec->serializer = st_rec->serializer;
811                 rec->status_func = st_rec->status_func;
812
813                 st_ctx->poll_handlers++;
814
815 #ifdef IFPOLL_MULTI_SYSTIMER
816                 if (st_ctx->poll_handlers == 1) {
817                         systimer_adjust_periodic(&st_ctx->pollclock,
818                                                  st_ctx->pollhz);
819                 }
820 #else
821                 ifpoll_handler_addevent();
822 #endif
823                 error = 0;
824         }
825         return error;
826 }
827
828 static int
829 stpoll_deregister(struct ifnet *ifp)
830 {
831         struct stpoll_ctx *st_ctx = &stpoll_context;
832         int i, error;
833
834         KKASSERT(&curthread->td_msgport == ifnet_portfn(0));
835
836         for (i = 0; i < st_ctx->poll_handlers; ++i) {
837                 if (st_ctx->pr[i].ifp == ifp) /* Found it */
838                         break;
839         }
840         if (i == st_ctx->poll_handlers) {
841                 kprintf("stpoll_deregister: ifp not found!!!\n");
842                 error = ENOENT;
843         } else {
844                 st_ctx->poll_handlers--;
845                 if (i < st_ctx->poll_handlers) {
846                         /* Last entry replaces this one. */
847                         st_ctx->pr[i] = st_ctx->pr[st_ctx->poll_handlers];
848                 }
849
850 #ifdef IFPOLL_MULTI_SYSTIMER
851                 if (st_ctx->poll_handlers == 0)
852                         systimer_adjust_periodic(&st_ctx->pollclock, 1);
853 #else
854                 ifpoll_handler_delevent();
855 #endif
856                 error = 0;
857         }
858         return error;
859 }
860
861 #ifndef IFPOLL_MULTI_SYSTIMER
862 static __inline int
863 iopoll_hz(struct iopoll_ctx *io_ctx)
864 {
865         int poll_hz;
866
867         poll_hz = ifpoll_pollhz;
868         if (io_ctx->poll_type == IFPOLL_TX)
869                 poll_hz /= ifpoll_txfrac + 1;
870         return poll_hz;
871 }
872 #endif
873
874 static __inline void
875 iopoll_reset_state(struct iopoll_ctx *io_ctx)
876 {
877         crit_enter();
878         io_ctx->poll_burst = 5;
879         io_ctx->pending_polls = 0;
880         io_ctx->residual_burst = 0;
881         io_ctx->phase = 0;
882         bzero(&io_ctx->poll_start_t, sizeof(io_ctx->poll_start_t));
883         bzero(&io_ctx->prev_t, sizeof(io_ctx->prev_t));
884         crit_exit();
885 }
886
887 static void
888 iopoll_init(int cpuid)
889 {
890         KKASSERT(cpuid < IFPOLL_CTX_MAX);
891
892         rxpoll_context[cpuid] = iopoll_ctx_create(cpuid, IFPOLL_RX);
893         txpoll_context[cpuid] = iopoll_ctx_create(cpuid, IFPOLL_TX);
894 }
895
896 static struct iopoll_ctx *
897 iopoll_ctx_create(int cpuid, int poll_type)
898 {
899         struct iopoll_ctx *io_ctx;
900         const char *poll_type_str;
901         char cpuid_str[16];
902
903         KKASSERT(poll_type == IFPOLL_RX || poll_type == IFPOLL_TX);
904
905         /*
906          * Make sure that tunables are in sane state
907          */
908         if (iopoll_burst_max < MIN_IOPOLL_BURST_MAX)
909                 iopoll_burst_max = MIN_IOPOLL_BURST_MAX;
910         else if (iopoll_burst_max > MAX_IOPOLL_BURST_MAX)
911                 iopoll_burst_max = MAX_IOPOLL_BURST_MAX;
912
913         if (iopoll_each_burst > iopoll_burst_max)
914                 iopoll_each_burst = iopoll_burst_max;
915
916         /*
917          * Create the per-cpu polling context
918          */
919         io_ctx = kmalloc(sizeof(*io_ctx), M_DEVBUF, M_WAITOK | M_ZERO);
920
921         io_ctx->poll_each_burst = iopoll_each_burst;
922         io_ctx->poll_burst_max = iopoll_burst_max;
923         io_ctx->user_frac = 50;
924 #ifdef IFPOLL_MULTI_SYSTIMER
925         io_ctx->pollhz = iopoll_hz;
926 #else
927         io_ctx->poll_type = poll_type;
928 #endif
929         io_ctx->poll_cpuid = cpuid;
930         iopoll_reset_state(io_ctx);
931
932         netmsg_init(&io_ctx->poll_netmsg, &netisr_adone_rport, MSGF_MPSAFE,
933                     iopoll_handler);
934         io_ctx->poll_netmsg.nm_lmsg.u.ms_resultp = io_ctx;
935
936         netmsg_init(&io_ctx->poll_more_netmsg, &netisr_adone_rport, MSGF_MPSAFE,
937                     iopollmore_handler);
938         io_ctx->poll_more_netmsg.nm_lmsg.u.ms_resultp = io_ctx;
939
940         /*
941          * Initialize per-cpu sysctl nodes
942          */
943         if (poll_type == IFPOLL_RX)
944                 poll_type_str = "rx";
945         else
946                 poll_type_str = "tx";
947         ksnprintf(cpuid_str, sizeof(cpuid_str), "%s%d",
948                   poll_type_str, io_ctx->poll_cpuid);
949
950         sysctl_ctx_init(&io_ctx->poll_sysctl_ctx);
951         io_ctx->poll_sysctl_tree = SYSCTL_ADD_NODE(&io_ctx->poll_sysctl_ctx,
952                                    SYSCTL_STATIC_CHILDREN(_net_ifpoll),
953                                    OID_AUTO, cpuid_str, CTLFLAG_RD, 0, "");
954         iopoll_add_sysctl(&io_ctx->poll_sysctl_ctx,
955                           SYSCTL_CHILDREN(io_ctx->poll_sysctl_tree), io_ctx);
956
957 #ifdef IFPOLL_MULTI_SYSTIMER
958         /*
959          * Initialize systimer
960          */
961         systimer_init_periodic_nq(&io_ctx->pollclock,
962                                   iopoll_systimer, io_ctx, 1);
963 #endif
964
965         return io_ctx;
966 }
967
968 /*
969  * Hook from iopoll systimer.  Tries to schedule an iopoll, but keeps
970  * track of lost ticks due to the previous handler taking too long.
971  * Normally, this should not happen, because polling handler should
972  * run for a short time.  However, in some cases (e.g. when there are
973  * changes in link status etc.) the drivers take a very long time
974  * (even in the order of milliseconds) to reset and reconfigure the
975  * device, causing apparent lost polls.
976  *
977  * The first part of the code is just for debugging purposes, and tries
978  * to count how often hardclock ticks are shorter than they should,
979  * meaning either stray interrupts or delayed events.
980  *
981  * WARNING! called from fastint or IPI, the MP lock might not be held.
982  */
983 static void
984 iopoll_clock(struct iopoll_ctx *io_ctx)
985 {
986         globaldata_t gd = mycpu;
987         struct timeval t;
988         int delta, poll_hz;
989
990         KKASSERT(gd->gd_cpuid == io_ctx->poll_cpuid);
991
992         if (io_ctx->poll_handlers == 0)
993                 return;
994
995 #ifdef IFPOLL_MULTI_SYSTIMER
996         poll_hz = io_ctx->pollhz;
997 #else
998         poll_hz = iopoll_hz(io_ctx);
999 #endif
1000
1001         microuptime(&t);
1002         delta = (t.tv_usec - io_ctx->prev_t.tv_usec) +
1003                 (t.tv_sec - io_ctx->prev_t.tv_sec) * 1000000;
1004         if (delta * poll_hz < 500000)
1005                 io_ctx->short_ticks++;
1006         else
1007                 io_ctx->prev_t = t;
1008
1009         if (io_ctx->pending_polls > 100) {
1010                 /*
1011                  * Too much, assume it has stalled (not always true
1012                  * see comment above).
1013                  */
1014                 io_ctx->stalled++;
1015                 io_ctx->pending_polls = 0;
1016                 io_ctx->phase = 0;
1017         }
1018
1019         if (io_ctx->phase <= 2) {
1020                 if (io_ctx->phase != 0)
1021                         io_ctx->suspect++;
1022                 io_ctx->phase = 1;
1023                 crit_enter_gd(gd);
1024                 sched_iopoll(io_ctx);
1025                 crit_exit_gd(gd);
1026                 io_ctx->phase = 2;
1027         }
1028         if (io_ctx->pending_polls++ > 0)
1029                 io_ctx->lost_polls++;
1030 }
1031
1032 #ifdef IFPOLL_MULTI_SYSTIMER
1033 static void
1034 iopoll_systimer(systimer_t info, struct intrframe *frame __unused)
1035 {
1036         iopoll_clock(info->data);
1037 }
1038 #endif
1039
1040 /*
1041  * iopoll_handler is scheduled by sched_iopoll when appropriate, typically
1042  * once per polling systimer tick.
1043  *
1044  * Note that the message is replied immediately in order to allow a new
1045  * ISR to be scheduled in the handler.
1046  */
1047 static void
1048 iopoll_handler(struct netmsg *msg)
1049 {
1050         struct iopoll_ctx *io_ctx;
1051         struct thread *td = curthread;
1052         int i, cycles;
1053
1054         io_ctx = msg->nm_lmsg.u.ms_resultp;
1055         KKASSERT(&td->td_msgport == ifnet_portfn(io_ctx->poll_cpuid));
1056
1057         crit_enter_quick(td);
1058
1059         /* Reply ASAP */
1060         lwkt_replymsg(&msg->nm_lmsg, 0);
1061
1062         if (io_ctx->poll_handlers == 0) {
1063                 crit_exit_quick(td);
1064                 return;
1065         }
1066
1067         io_ctx->phase = 3;
1068         if (io_ctx->residual_burst == 0) {
1069                 /* First call in this tick */
1070                 microuptime(&io_ctx->poll_start_t);
1071                 io_ctx->residual_burst = io_ctx->poll_burst;
1072         }
1073         cycles = (io_ctx->residual_burst < io_ctx->poll_each_burst) ?
1074                  io_ctx->residual_burst : io_ctx->poll_each_burst;
1075         io_ctx->residual_burst -= cycles;
1076
1077         for (i = 0; i < io_ctx->poll_handlers; i++) {
1078                 const struct iopoll_rec *rec = &io_ctx->pr[i];
1079                 struct ifnet *ifp = rec->ifp;
1080
1081                 if (!lwkt_serialize_try(rec->serializer))
1082                         continue;
1083
1084                 if ((ifp->if_flags & (IFF_RUNNING | IFF_NPOLLING)) ==
1085                     (IFF_RUNNING | IFF_NPOLLING))
1086                         rec->poll_func(ifp, rec->arg, cycles);
1087
1088                 lwkt_serialize_exit(rec->serializer);
1089         }
1090
1091         /*
1092          * Do a quick exit/enter to catch any higher-priority
1093          * interrupt sources.
1094          */
1095         crit_exit_quick(td);
1096         crit_enter_quick(td);
1097
1098         sched_iopollmore(io_ctx);
1099         io_ctx->phase = 4;
1100
1101         crit_exit_quick(td);
1102 }
1103
1104 /*
1105  * iopollmore_handler is called after other netisr's, possibly scheduling
1106  * another iopoll_handler call, or adapting the burst size for the next cycle.
1107  *
1108  * It is very bad to fetch large bursts of packets from a single card at once,
1109  * because the burst could take a long time to be completely processed leading
1110  * to unfairness.  To reduce the problem, and also to account better for time
1111  * spent in network-related processing, we split the burst in smaller chunks
1112  * of fixed size, giving control to the other netisr's between chunks.  This
1113  * helps in improving the fairness, reducing livelock and accounting for the
1114  * work performed in low level handling.
1115  */
1116 static void
1117 iopollmore_handler(struct netmsg *msg)
1118 {
1119         struct thread *td = curthread;
1120         struct iopoll_ctx *io_ctx;
1121         struct timeval t;
1122         int kern_load, poll_hz;
1123         uint32_t pending_polls;
1124
1125         io_ctx = msg->nm_lmsg.u.ms_resultp;
1126         KKASSERT(&td->td_msgport == ifnet_portfn(io_ctx->poll_cpuid));
1127
1128         crit_enter_quick(td);
1129
1130         /* Replay ASAP */
1131         lwkt_replymsg(&msg->nm_lmsg, 0);
1132
1133         if (io_ctx->poll_handlers == 0) {
1134                 crit_exit_quick(td);
1135                 return;
1136         }
1137
1138 #ifdef IFPOLL_MULTI_SYSTIMER
1139         poll_hz = io_ctx->pollhz;
1140 #else
1141         poll_hz = iopoll_hz(io_ctx);
1142 #endif
1143
1144         io_ctx->phase = 5;
1145         if (io_ctx->residual_burst > 0) {
1146                 sched_iopoll(io_ctx);
1147                 crit_exit_quick(td);
1148                 /* Will run immediately on return, followed by netisrs */
1149                 return;
1150         }
1151
1152         /* Here we can account time spent in iopoll's in this tick */
1153         microuptime(&t);
1154         kern_load = (t.tv_usec - io_ctx->poll_start_t.tv_usec) +
1155                     (t.tv_sec - io_ctx->poll_start_t.tv_sec) * 1000000; /* us */
1156         kern_load = (kern_load * poll_hz) / 10000; /* 0..100 */
1157         if (kern_load > (100 - io_ctx->user_frac)) {
1158                 /* Try decrease ticks */
1159                 if (io_ctx->poll_burst > 1)
1160                         io_ctx->poll_burst--;
1161         } else {
1162                 if (io_ctx->poll_burst < io_ctx->poll_burst_max)
1163                         io_ctx->poll_burst++;
1164         }
1165
1166         io_ctx->pending_polls--;
1167         pending_polls = io_ctx->pending_polls;
1168
1169         if (pending_polls == 0) {
1170                 /* We are done */
1171                 io_ctx->phase = 0;
1172         } else {
1173                 /*
1174                  * Last cycle was long and caused us to miss one or more
1175                  * hardclock ticks.  Restart processing again, but slightly
1176                  * reduce the burst size to prevent that this happens again.
1177                  */
1178                 io_ctx->poll_burst -= (io_ctx->poll_burst / 8);
1179                 if (io_ctx->poll_burst < 1)
1180                         io_ctx->poll_burst = 1;
1181                 sched_iopoll(io_ctx);
1182                 io_ctx->phase = 6;
1183         }
1184
1185         crit_exit_quick(td);
1186 }
1187
1188 static void
1189 iopoll_add_sysctl(struct sysctl_ctx_list *ctx, struct sysctl_oid_list *parent,
1190                   struct iopoll_ctx *io_ctx)
1191 {
1192 #ifdef IFPOLL_MULTI_SYSTIMER
1193         SYSCTL_ADD_PROC(ctx, parent, OID_AUTO, "pollhz",
1194                         CTLTYPE_INT | CTLFLAG_RW, io_ctx, 0, sysctl_iopollhz,
1195                         "I", "Device polling frequency");
1196 #endif
1197
1198         SYSCTL_ADD_PROC(ctx, parent, OID_AUTO, "burst_max",
1199                         CTLTYPE_UINT | CTLFLAG_RW, io_ctx, 0, sysctl_burstmax,
1200                         "IU", "Max Polling burst size");
1201
1202         SYSCTL_ADD_PROC(ctx, parent, OID_AUTO, "each_burst",
1203                         CTLTYPE_UINT | CTLFLAG_RW, io_ctx, 0, sysctl_eachburst,
1204                         "IU", "Max size of each burst");
1205
1206         SYSCTL_ADD_UINT(ctx, parent, OID_AUTO, "phase", CTLFLAG_RD,
1207                         &io_ctx->phase, 0, "Polling phase");
1208
1209         SYSCTL_ADD_UINT(ctx, parent, OID_AUTO, "suspect", CTLFLAG_RW,
1210                         &io_ctx->suspect, 0, "suspect event");
1211
1212         SYSCTL_ADD_UINT(ctx, parent, OID_AUTO, "stalled", CTLFLAG_RW,
1213                         &io_ctx->stalled, 0, "potential stalls");
1214
1215         SYSCTL_ADD_UINT(ctx, parent, OID_AUTO, "burst", CTLFLAG_RD,
1216                         &io_ctx->poll_burst, 0, "Current polling burst size");
1217
1218         SYSCTL_ADD_UINT(ctx, parent, OID_AUTO, "user_frac", CTLFLAG_RW,
1219                         &io_ctx->user_frac, 0,
1220                         "Desired user fraction of cpu time");
1221
1222         SYSCTL_ADD_UINT(ctx, parent, OID_AUTO, "short_ticks", CTLFLAG_RW,
1223                         &io_ctx->short_ticks, 0,
1224                         "Hardclock ticks shorter than they should be");
1225
1226         SYSCTL_ADD_UINT(ctx, parent, OID_AUTO, "lost_polls", CTLFLAG_RW,
1227                         &io_ctx->lost_polls, 0,
1228                         "How many times we would have lost a poll tick");
1229
1230         SYSCTL_ADD_UINT(ctx, parent, OID_AUTO, "pending_polls", CTLFLAG_RD,
1231                         &io_ctx->pending_polls, 0, "Do we need to poll again");
1232
1233         SYSCTL_ADD_INT(ctx, parent, OID_AUTO, "residual_burst", CTLFLAG_RD,
1234                        &io_ctx->residual_burst, 0,
1235                        "# of residual cycles in burst");
1236
1237         SYSCTL_ADD_UINT(ctx, parent, OID_AUTO, "handlers", CTLFLAG_RD,
1238                         &io_ctx->poll_handlers, 0,
1239                         "Number of registered poll handlers");
1240 }
1241
1242 #ifdef IFPOLL_MULTI_SYSTIMER
1243
1244 static int
1245 sysctl_iopollhz(SYSCTL_HANDLER_ARGS)
1246 {
1247         struct iopoll_ctx *io_ctx = arg1;
1248         struct iopoll_sysctl_netmsg msg;
1249         struct netmsg *nmsg;
1250         int error, phz;
1251
1252         phz = io_ctx->pollhz;
1253         error = sysctl_handle_int(oidp, &phz, 0, req);
1254         if (error || req->newptr == NULL)
1255                 return error;
1256         if (phz <= 0)
1257                 return EINVAL;
1258         else if (phz > IFPOLL_FREQ_MAX)
1259                 phz = IFPOLL_FREQ_MAX;
1260
1261         nmsg = &msg.nmsg;
1262         netmsg_init(nmsg, &curthread->td_msgport, MSGF_MPSAFE,
1263                     sysctl_iopollhz_handler);
1264         nmsg->nm_lmsg.u.ms_result = phz;
1265         msg.ctx = io_ctx;
1266
1267         return ifnet_domsg(&nmsg->nm_lmsg, io_ctx->poll_cpuid);
1268 }
1269
1270 static void
1271 sysctl_iopollhz_handler(struct netmsg *nmsg)
1272 {
1273         struct iopoll_sysctl_netmsg *msg = (struct iopoll_sysctl_netmsg *)nmsg;
1274         struct iopoll_ctx *io_ctx;
1275
1276         io_ctx = msg->ctx;
1277         KKASSERT(&curthread->td_msgport == ifnet_portfn(io_ctx->poll_cpuid));
1278
1279         /*
1280          * If polling is disabled or there is no polling handler
1281          * registered, don't adjust polling systimer frequency.
1282          * Polling systimer frequency will be adjusted once there
1283          * are registered handlers.
1284          */
1285         io_ctx->pollhz = nmsg->nm_lmsg.u.ms_result;
1286         if (io_ctx->poll_handlers)
1287                 systimer_adjust_periodic(&io_ctx->pollclock, io_ctx->pollhz);
1288
1289         lwkt_replymsg(&nmsg->nm_lmsg, 0);
1290 }
1291
1292 #endif  /* IFPOLL_MULTI_SYSTIMER */
1293
1294 static void
1295 sysctl_burstmax_handler(struct netmsg *nmsg)
1296 {
1297         struct iopoll_sysctl_netmsg *msg = (struct iopoll_sysctl_netmsg *)nmsg;
1298         struct iopoll_ctx *io_ctx;
1299
1300         io_ctx = msg->ctx;
1301         KKASSERT(&curthread->td_msgport == ifnet_portfn(io_ctx->poll_cpuid));
1302
1303         io_ctx->poll_burst_max = nmsg->nm_lmsg.u.ms_result;
1304         if (io_ctx->poll_each_burst > io_ctx->poll_burst_max)
1305                 io_ctx->poll_each_burst = io_ctx->poll_burst_max;
1306         if (io_ctx->poll_burst > io_ctx->poll_burst_max)
1307                 io_ctx->poll_burst = io_ctx->poll_burst_max;
1308         if (io_ctx->residual_burst > io_ctx->poll_burst_max)
1309                 io_ctx->residual_burst = io_ctx->poll_burst_max;
1310
1311         lwkt_replymsg(&nmsg->nm_lmsg, 0);
1312 }
1313
1314 static int
1315 sysctl_burstmax(SYSCTL_HANDLER_ARGS)
1316 {
1317         struct iopoll_ctx *io_ctx = arg1;
1318         struct iopoll_sysctl_netmsg msg;
1319         struct netmsg *nmsg;
1320         uint32_t burst_max;
1321         int error;
1322
1323         burst_max = io_ctx->poll_burst_max;
1324         error = sysctl_handle_int(oidp, &burst_max, 0, req);
1325         if (error || req->newptr == NULL)
1326                 return error;
1327         if (burst_max < MIN_IOPOLL_BURST_MAX)
1328                 burst_max = MIN_IOPOLL_BURST_MAX;
1329         else if (burst_max > MAX_IOPOLL_BURST_MAX)
1330                 burst_max = MAX_IOPOLL_BURST_MAX;
1331
1332         nmsg = &msg.nmsg;
1333         netmsg_init(nmsg, &curthread->td_msgport, MSGF_MPSAFE,
1334                     sysctl_burstmax_handler);
1335         nmsg->nm_lmsg.u.ms_result = burst_max;
1336         msg.ctx = io_ctx;
1337
1338         return ifnet_domsg(&nmsg->nm_lmsg, io_ctx->poll_cpuid);
1339 }
1340
1341 static void
1342 sysctl_eachburst_handler(struct netmsg *nmsg)
1343 {
1344         struct iopoll_sysctl_netmsg *msg = (struct iopoll_sysctl_netmsg *)nmsg;
1345         struct iopoll_ctx *io_ctx;
1346         uint32_t each_burst;
1347
1348         io_ctx = msg->ctx;
1349         KKASSERT(&curthread->td_msgport == ifnet_portfn(io_ctx->poll_cpuid));
1350
1351         each_burst = nmsg->nm_lmsg.u.ms_result;
1352         if (each_burst > io_ctx->poll_burst_max)
1353                 each_burst = io_ctx->poll_burst_max;
1354         else if (each_burst < 1)
1355                 each_burst = 1;
1356         io_ctx->poll_each_burst = each_burst;
1357
1358         lwkt_replymsg(&nmsg->nm_lmsg, 0);
1359 }
1360
1361 static int
1362 sysctl_eachburst(SYSCTL_HANDLER_ARGS)
1363 {
1364         struct iopoll_ctx *io_ctx = arg1;
1365         struct iopoll_sysctl_netmsg msg;
1366         struct netmsg *nmsg;
1367         uint32_t each_burst;
1368         int error;
1369
1370         each_burst = io_ctx->poll_each_burst;
1371         error = sysctl_handle_int(oidp, &each_burst, 0, req);
1372         if (error || req->newptr == NULL)
1373                 return error;
1374
1375         nmsg = &msg.nmsg;
1376         netmsg_init(nmsg, &curthread->td_msgport, MSGF_MPSAFE,
1377                     sysctl_eachburst_handler);
1378         nmsg->nm_lmsg.u.ms_result = each_burst;
1379         msg.ctx = io_ctx;
1380
1381         return ifnet_domsg(&nmsg->nm_lmsg, io_ctx->poll_cpuid);
1382 }
1383
1384 static int
1385 iopoll_register(struct ifnet *ifp, struct iopoll_ctx *io_ctx,
1386                 const struct ifpoll_io *io_rec)
1387 {
1388         int error;
1389
1390         KKASSERT(&curthread->td_msgport == ifnet_portfn(io_ctx->poll_cpuid));
1391
1392         if (io_rec->poll_func == NULL)
1393                 return 0;
1394
1395         /*
1396          * Check if there is room.
1397          */
1398         if (io_ctx->poll_handlers >= IFPOLL_LIST_LEN) {
1399                 /*
1400                  * List full, cannot register more entries.
1401                  * This should never happen; if it does, it is probably a
1402                  * broken driver trying to register multiple times. Checking
1403                  * this at runtime is expensive, and won't solve the problem
1404                  * anyways, so just report a few times and then give up.
1405                  */
1406                 static int verbose = 10; /* XXX */
1407                 if (verbose > 0) {
1408                         kprintf("io poll handlers list full, "
1409                                 "maybe a broken driver ?\n");
1410                         verbose--;
1411                 }
1412                 error = ENOENT;
1413         } else {
1414                 struct iopoll_rec *rec = &io_ctx->pr[io_ctx->poll_handlers];
1415
1416                 rec->ifp = ifp;
1417                 rec->serializer = io_rec->serializer;
1418                 rec->arg = io_rec->arg;
1419                 rec->poll_func = io_rec->poll_func;
1420
1421                 io_ctx->poll_handlers++;
1422                 if (io_ctx->poll_handlers == 1) {
1423 #ifdef IFPOLL_MULTI_SYSTIMER
1424                         systimer_adjust_periodic(&io_ctx->pollclock,
1425                                                  io_ctx->pollhz);
1426 #else
1427                         u_int *mask;
1428
1429                         if (io_ctx->poll_type == IFPOLL_RX)
1430                                 mask = &ifpoll0.rx_cpumask;
1431                         else
1432                                 mask = &ifpoll0.tx_cpumask;
1433                         KKASSERT((*mask & mycpu->gd_cpumask) == 0);
1434                         atomic_set_int(mask, mycpu->gd_cpumask);
1435 #endif
1436                 }
1437 #ifndef IFPOLL_MULTI_SYSTIMER
1438                 ifpoll_handler_addevent();
1439 #endif
1440                 error = 0;
1441         }
1442         return error;
1443 }
1444
1445 static int
1446 iopoll_deregister(struct ifnet *ifp, struct iopoll_ctx *io_ctx)
1447 {
1448         int i, error;
1449
1450         KKASSERT(&curthread->td_msgport == ifnet_portfn(io_ctx->poll_cpuid));
1451
1452         for (i = 0; i < io_ctx->poll_handlers; ++i) {
1453                 if (io_ctx->pr[i].ifp == ifp) /* Found it */
1454                         break;
1455         }
1456         if (i == io_ctx->poll_handlers) {
1457                 error = ENOENT;
1458         } else {
1459                 io_ctx->poll_handlers--;
1460                 if (i < io_ctx->poll_handlers) {
1461                         /* Last entry replaces this one. */
1462                         io_ctx->pr[i] = io_ctx->pr[io_ctx->poll_handlers];
1463                 }
1464
1465                 if (io_ctx->poll_handlers == 0) {
1466 #ifdef IFPOLL_MULTI_SYSTIMER
1467                         systimer_adjust_periodic(&io_ctx->pollclock, 1);
1468 #else
1469                         u_int *mask;
1470
1471                         if (io_ctx->poll_type == IFPOLL_RX)
1472                                 mask = &ifpoll0.rx_cpumask;
1473                         else
1474                                 mask = &ifpoll0.tx_cpumask;
1475                         KKASSERT(*mask & mycpu->gd_cpumask);
1476                         atomic_clear_int(mask, mycpu->gd_cpumask);
1477 #endif
1478                         iopoll_reset_state(io_ctx);
1479                 }
1480 #ifndef IFPOLL_MULTI_SYSTIMER
1481                 ifpoll_handler_delevent();
1482 #endif
1483                 error = 0;
1484         }
1485         return error;
1486 }