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