Add prototype for ciss_print0 in place.
[dragonfly.git] / sys / kern / kern_poll.c
1 /*-
2  * Copyright (c) 2001-2002 Luigi Rizzo
3  *
4  * Supported by: the Xorp Project (www.xorp.org)
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  *
27  * $FreeBSD: src/sys/kern/kern_poll.c,v 1.2.2.4 2002/06/27 23:26:33 luigi Exp $
28  * $DragonFly: src/sys/kern/kern_poll.c,v 1.13 2004/06/27 19:40:12 dillon Exp $
29  */
30
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/kernel.h>
34 #include <sys/socket.h>                 /* needed by net/if.h           */
35 #include <sys/sysctl.h>
36
37 #include <sys/thread2.h>
38 #include <sys/msgport2.h>
39
40 #include <i386/include/md_var.h>        /* for vm_page_zero_idle()      */
41 #include <net/if.h>                     /* for IFF_* flags              */
42 #include <net/netisr.h>                 /* for NETISR_POLL              */
43
44 /* the two netisr handlers */
45 static int netisr_poll(struct netmsg *);
46 static int netisr_pollmore(struct netmsg *);
47
48 void init_device_poll(void);            /* init routine                 */
49 void hardclock_device_poll(void);       /* hook from hardclock          */
50 void ether_poll(int);                   /* polling while in trap        */
51
52 /*
53  * Polling support for [network] device drivers.
54  *
55  * Drivers which support this feature try to register with the
56  * polling code.
57  *
58  * If registration is successful, the driver must disable interrupts,
59  * and further I/O is performed through the handler, which is invoked
60  * (at least once per clock tick) with 3 arguments: the "arg" passed at
61  * register time (a struct ifnet pointer), a command, and a "count" limit.
62  *
63  * The command can be one of the following:
64  *  POLL_ONLY: quick move of "count" packets from input/output queues.
65  *  POLL_AND_CHECK_STATUS: as above, plus check status registers or do
66  *      other more expensive operations. This command is issued periodically
67  *      but less frequently than POLL_ONLY.
68  *  POLL_DEREGISTER: deregister and return to interrupt mode.
69  *
70  * The first two commands are only issued if the interface is marked as
71  * 'IFF_UP and IFF_RUNNING', the last one only if IFF_RUNNING is set.
72  *
73  * The count limit specifies how much work the handler can do during the
74  * call -- typically this is the number of packets to be received, or
75  * transmitted, etc. (drivers are free to interpret this number, as long
76  * as the max time spent in the function grows roughly linearly with the
77  * count).
78  *
79  * Deregistration can be requested by the driver itself (typically in the
80  * *_stop() routine), or by the polling code, by invoking the handler.
81  *
82  * Polling can be globally enabled or disabled with the sysctl variable
83  * kern.polling.enable (default is 0, disabled)
84  *
85  * A second variable controls the sharing of CPU between polling/kernel
86  * network processing, and other activities (typically userlevel tasks):
87  * kern.polling.user_frac (between 0 and 100, default 50) sets the share
88  * of CPU allocated to user tasks. CPU is allocated proportionally to the
89  * 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_each_burst <= poll_burst <= poll_burst_max
95  *      0 <= poll_in_trap <= poll_each_burst
96  *      MIN_POLL_BURST_MAX <= poll_burst_max <= MAX_POLL_BURST_MAX
97  */
98
99 #define MIN_POLL_BURST_MAX      10
100 #define MAX_POLL_BURST_MAX      1000
101
102 SYSCTL_NODE(_kern, OID_AUTO, polling, CTLFLAG_RW, 0,
103         "Device polling parameters");
104
105 static u_int32_t poll_burst = 5;
106 SYSCTL_UINT(_kern_polling, OID_AUTO, burst, CTLFLAG_RW,
107         &poll_burst, 0, "Current polling burst size");
108
109 static u_int32_t poll_each_burst = 5;
110 SYSCTL_UINT(_kern_polling, OID_AUTO, each_burst, CTLFLAG_RW,
111         &poll_each_burst, 0, "Max size of each burst");
112
113 static u_int32_t poll_burst_max = 150;  /* good for 100Mbit net and HZ=1000 */
114 SYSCTL_UINT(_kern_polling, OID_AUTO, burst_max, CTLFLAG_RW,
115         &poll_burst_max, 0, "Max Polling burst size");
116
117 u_int32_t poll_in_trap;                 /* used in trap.c */
118 SYSCTL_UINT(_kern_polling, OID_AUTO, poll_in_trap, CTLFLAG_RW,
119         &poll_in_trap, 0, "Poll burst size during a trap");
120
121 static u_int32_t user_frac = 50;
122 SYSCTL_UINT(_kern_polling, OID_AUTO, user_frac, CTLFLAG_RW,
123         &user_frac, 0, "Desired user fraction of cpu time");
124
125 static u_int32_t reg_frac = 20 ;
126 SYSCTL_UINT(_kern_polling, OID_AUTO, reg_frac, CTLFLAG_RW,
127         &reg_frac, 0, "Every this many cycles poll register");
128
129 static u_int32_t short_ticks;
130 SYSCTL_UINT(_kern_polling, OID_AUTO, short_ticks, CTLFLAG_RW,
131         &short_ticks, 0, "Hardclock ticks shorter than they should be");
132
133 static u_int32_t lost_polls;
134 SYSCTL_UINT(_kern_polling, OID_AUTO, lost_polls, CTLFLAG_RW,
135         &lost_polls, 0, "How many times we would have lost a poll tick");
136
137 static u_int32_t pending_polls;
138 SYSCTL_UINT(_kern_polling, OID_AUTO, pending_polls, CTLFLAG_RW,
139         &pending_polls, 0, "Do we need to poll again");
140
141 static int residual_burst = 0;
142 SYSCTL_INT(_kern_polling, OID_AUTO, residual_burst, CTLFLAG_RW,
143         &residual_burst, 0, "# of residual cycles in burst");
144
145 static u_int32_t poll_handlers; /* next free entry in pr[]. */
146 SYSCTL_UINT(_kern_polling, OID_AUTO, handlers, CTLFLAG_RD,
147         &poll_handlers, 0, "Number of registered poll handlers");
148
149 static int polling = 0;         /* global polling enable */
150 SYSCTL_UINT(_kern_polling, OID_AUTO, enable, CTLFLAG_RW,
151         &polling, 0, "Polling enabled");
152
153 static u_int32_t phase;
154 SYSCTL_UINT(_kern_polling, OID_AUTO, phase, CTLFLAG_RW,
155         &phase, 0, "Polling phase");
156
157 static u_int32_t suspect;
158 SYSCTL_UINT(_kern_polling, OID_AUTO, suspect, CTLFLAG_RW,
159         &suspect, 0, "suspect event");
160
161 static u_int32_t stalled;
162 SYSCTL_UINT(_kern_polling, OID_AUTO, stalled, CTLFLAG_RW,
163         &stalled, 0, "potential stalls");
164
165
166 #define POLL_LIST_LEN  128
167 struct pollrec {
168         poll_handler_t  *handler;
169         struct ifnet    *ifp;
170 };
171
172 static struct pollrec pr[POLL_LIST_LEN];
173
174 /*
175  * register relevant netisr. Called from kern_clock.c:
176  */
177 void
178 init_device_poll(void)
179 {
180         netisr_register(NETISR_POLL, cpu0_portfn, netisr_poll);
181         netisr_register(NETISR_POLLMORE, cpu0_portfn, netisr_pollmore);
182 }
183
184 /*
185  * Hook from hardclock. Tries to schedule a netisr, but keeps track
186  * of lost ticks due to the previous handler taking too long.
187  * Normally, this should not happen, because polling handler should
188  * run for a short time. However, in some cases (e.g. when there are
189  * changes in link status etc.) the drivers take a very long time
190  * (even in the order of milliseconds) to reset and reconfigure the
191  * device, causing apparent lost polls.
192  *
193  * The first part of the code is just for debugging purposes, and tries
194  * to count how often hardclock ticks are shorter than they should,
195  * meaning either stray interrupts or delayed events.
196  *
197  * WARNING! called from fastint or IPI, the MP lock might not be held.
198  */
199 void
200 hardclock_device_poll(void)
201 {
202         static struct timeval prev_t, t;
203         int delta;
204
205         if (poll_handlers == 0)
206                 return;
207
208         microuptime(&t);
209         delta = (t.tv_usec - prev_t.tv_usec) +
210                 (t.tv_sec - prev_t.tv_sec)*1000000;
211         if (delta * hz < 500000)
212                 short_ticks++;
213         else
214                 prev_t = t;
215
216         if (pending_polls > 100) {
217                 /*
218                  * Too much, assume it has stalled (not always true
219                  * see comment above).
220                  */
221                 stalled++;
222                 pending_polls = 0;
223                 phase = 0;
224         }
225
226         if (phase <= 2) {
227                 if (phase != 0)
228                         suspect++;
229                 phase = 1;
230                 schednetisr(NETISR_POLL);
231                 phase = 2;
232         }
233         if (pending_polls++ > 0)
234                 lost_polls++;
235 }
236
237 /*
238  * ether_poll is called from the idle loop or from the trap handler.
239  */
240 void
241 ether_poll(int count)
242 {
243         int i;
244         int s = splimp();
245
246         if (count > poll_each_burst)
247                 count = poll_each_burst;
248         for (i = 0 ; i < poll_handlers ; i++)
249                 if (pr[i].handler && (IFF_UP|IFF_RUNNING) ==
250                     (pr[i].ifp->if_flags & (IFF_UP|IFF_RUNNING)) )
251                         pr[i].handler(pr[i].ifp, 0, count); /* quick check */
252         splx(s);
253 }
254
255 /*
256  * netisr_pollmore is called after other netisr's, possibly scheduling
257  * another NETISR_POLL call, or adapting the burst size for the next cycle.
258  *
259  * It is very bad to fetch large bursts of packets from a single card at once,
260  * because the burst could take a long time to be completely processed, or
261  * could saturate the intermediate queue (ipintrq or similar) leading to
262  * losses or unfairness. To reduce the problem, and also to account better for
263  * time spent in network-related processing, we split the burst in smaller
264  * chunks of fixed size, giving control to the other netisr's between chunks.
265  * This helps in improving the fairness, reducing livelock (because we
266  * emulate more closely the "process to completion" that we have with
267  * fastforwarding) and accounting for the work performed in low level
268  * handling and forwarding.
269  */
270
271 static struct timeval poll_start_t;
272
273 /* ARGSUSED */
274 static int
275 netisr_pollmore(struct netmsg *msg)
276 {
277         struct timeval t;
278         int kern_load;
279         int s = splhigh();
280
281         lwkt_replymsg(&msg->nm_lmsg, 0);
282         phase = 5;
283         if (residual_burst > 0) {
284                 schednetisr(NETISR_POLL);
285                 /* will run immediately on return, followed by netisrs */
286                 goto out;
287         }
288         /* here we can account time spent in netisr's in this tick */
289         microuptime(&t);
290         kern_load = (t.tv_usec - poll_start_t.tv_usec) +
291                 (t.tv_sec - poll_start_t.tv_sec)*1000000;       /* us */
292         kern_load = (kern_load * hz) / 10000;                   /* 0..100 */
293         if (kern_load > (100 - user_frac)) { /* try decrease ticks */
294                 if (poll_burst > 1)
295                         poll_burst--;
296         } else {
297                 if (poll_burst < poll_burst_max)
298                         poll_burst++;
299         }
300
301         pending_polls--;
302         if (pending_polls == 0) {       /* we are done */
303                 phase = 0;
304         } else {
305                 /*
306                  * Last cycle was long and caused us to miss one or more
307                  * hardclock ticks. Restart processing again, but slightly
308                  * reduce the burst size to prevent that this happens again.
309                  */
310                 poll_burst -= (poll_burst / 8);
311                 if (poll_burst < 1)
312                         poll_burst = 1;
313                 schednetisr(NETISR_POLL);
314                 phase = 6;
315         }
316 out:
317         splx(s);
318         return(EASYNC);
319 }
320
321 /*
322  * netisr_poll is scheduled by schednetisr when appropriate, typically once
323  * per tick. It is called at splnet() so first thing to do is to upgrade to
324  * splimp(), and call all registered handlers.
325  *
326  * Note that the message is replied immediately in order to allow a new
327  * ISR to be scheduled in the handler.
328  */
329 /* ARGSUSED */
330 static int
331 netisr_poll(struct netmsg *msg)
332 {
333         static int reg_frac_count;
334         int i, cycles;
335         enum poll_cmd arg = POLL_ONLY;
336         int s;
337
338         lwkt_replymsg(&msg->nm_lmsg, 0);
339         s = splimp();
340         phase = 3;
341         if (residual_burst == 0) { /* first call in this tick */
342                 microuptime(&poll_start_t);
343                 /*
344                  * Check that paremeters are consistent with runtime
345                  * variables. Some of these tests could be done at sysctl
346                  * time, but the savings would be very limited because we
347                  * still have to check against reg_frac_count and
348                  * poll_each_burst. So, instead of writing separate sysctl
349                  * handlers, we do all here.
350                  */
351
352                 if (reg_frac > hz)
353                         reg_frac = hz;
354                 else if (reg_frac < 1)
355                         reg_frac = 1;
356                 if (reg_frac_count > reg_frac)
357                         reg_frac_count = reg_frac - 1;
358                 if (reg_frac_count-- == 0) {
359                         arg = POLL_AND_CHECK_STATUS;
360                         reg_frac_count = reg_frac - 1;
361                 }
362                 if (poll_burst_max < MIN_POLL_BURST_MAX)
363                         poll_burst_max = MIN_POLL_BURST_MAX;
364                 else if (poll_burst_max > MAX_POLL_BURST_MAX)
365                         poll_burst_max = MAX_POLL_BURST_MAX;
366
367                 if (poll_each_burst < 1)
368                         poll_each_burst = 1;
369                 else if (poll_each_burst > poll_burst_max)
370                         poll_each_burst = poll_burst_max;
371
372                 residual_burst = poll_burst;
373         }
374         cycles = (residual_burst < poll_each_burst) ?
375                 residual_burst : poll_each_burst;
376         residual_burst -= cycles;
377
378         if (polling) {
379                 for (i = 0 ; i < poll_handlers ; i++) {
380                         if (pr[i].handler && (IFF_UP|IFF_RUNNING) ==
381                             (pr[i].ifp->if_flags & (IFF_UP|IFF_RUNNING)) )
382                                 pr[i].handler(pr[i].ifp, arg, cycles);
383                 }
384         } else {        /* unregister */
385                 for (i = 0 ; i < poll_handlers ; i++) {
386                         if (pr[i].handler &&
387                             pr[i].ifp->if_flags & IFF_RUNNING) {
388                                 pr[i].ifp->if_flags &= ~IFF_POLLING;
389                                 pr[i].handler(pr[i].ifp, POLL_DEREGISTER, 1);
390                         }
391                         pr[i].handler=NULL;
392                 }
393                 residual_burst = 0;
394                 poll_handlers = 0;
395         }
396         schednetisr(NETISR_POLLMORE);
397         phase = 4;
398         splx(s);
399         return(EASYNC);
400 }
401
402 /*
403  * Try to register routine for polling. Returns 1 if successful
404  * (and polling should be enabled), 0 otherwise.
405  * A device is not supposed to register itself multiple times.
406  *
407  * This is called from within the *_intr() functions, so we do not need
408  * further locking.
409  */
410 int
411 ether_poll_register(poll_handler_t *h, struct ifnet *ifp)
412 {
413         int s;
414
415         if (polling == 0) /* polling disabled, cannot register */
416                 return 0;
417         if (h == NULL || ifp == NULL)           /* bad arguments        */
418                 return 0;
419         if ( !(ifp->if_flags & IFF_UP) )        /* must be up           */
420                 return 0;
421         if (ifp->if_flags & IFF_POLLING)        /* already polling      */
422                 return 0;
423
424         s = splhigh();
425         if (poll_handlers >= POLL_LIST_LEN) {
426                 /*
427                  * List full, cannot register more entries.
428                  * This should never happen; if it does, it is probably a
429                  * broken driver trying to register multiple times. Checking
430                  * this at runtime is expensive, and won't solve the problem
431                  * anyways, so just report a few times and then give up.
432                  */
433                 static int verbose = 10 ;
434                 splx(s);
435                 if (verbose >0) {
436                         printf("poll handlers list full, "
437                                 "maybe a broken driver ?\n");
438                         verbose--;
439                 }
440                 return 0; /* no polling for you */
441         }
442
443         pr[poll_handlers].handler = h;
444         pr[poll_handlers].ifp = ifp;
445         poll_handlers++;
446         ifp->if_flags |= IFF_POLLING;
447         splx(s);
448         return 1; /* polling enabled in next call */
449 }
450
451 /*
452  * Remove interface from the polling list. Normally called by *_stop().
453  * It is not an error to call it with IFF_POLLING clear, the call is
454  * sufficiently rare to be preferable to save the space for the extra
455  * test in each driver in exchange of one additional function call.
456  */
457 int
458 ether_poll_deregister(struct ifnet *ifp)
459 {
460         int i;
461         int s = splimp();
462         
463         if ( !ifp || !(ifp->if_flags & IFF_POLLING) ) {
464                 splx(s);
465                 return 0;
466         }
467         for (i = 0 ; i < poll_handlers ; i++)
468                 if (pr[i].ifp == ifp) /* found it */
469                         break;
470         ifp->if_flags &= ~IFF_POLLING; /* found or not... */
471         if (i == poll_handlers) {
472                 splx(s);
473                 printf("ether_poll_deregister: ifp not found!!!\n");
474                 return 0;
475         }
476         poll_handlers--;
477         if (i < poll_handlers) { /* Last entry replaces this one. */
478                 pr[i].handler = pr[poll_handlers].handler;
479                 pr[i].ifp = pr[poll_handlers].ifp;
480         }
481         splx(s);
482         return 1;
483 }
484
485 void
486 emergency_poll_enable(const char *name)
487 {
488         if (polling == 0) {
489                 polling = 1;
490                 printf("%s forced polling on\n", name);
491         }
492 }