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