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