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