diff --git a/sys/amd64/amd64/trap.c b/sys/amd64/amd64/trap.c index 23ad3fc2442f..51c4c79554f4 100644 --- a/sys/amd64/amd64/trap.c +++ b/sys/amd64/amd64/trap.c @@ -162,6 +162,11 @@ SYSCTL_INT(_machdep, OID_AUTO, panic_on_nmi, CTLFLAG_RW, extern char *syscallnames[]; #endif +#ifdef DEVICE_POLLING +extern u_int32_t poll_in_trap; +extern int ether_poll __P((int count)); +#endif /* DEVICE_POLLING */ + /* * Exception, fault, and trap interface to the FreeBSD kernel. * This common code is called from assembly language IDT gate entry @@ -239,6 +244,11 @@ trap(frame) trap_fatal(&frame, eva); } +#ifdef DEVICE_POLLING + if (poll_in_trap) + ether_poll(poll_in_trap); +#endif /* DEVICE_POLLING */ + if ((ISPL(frame.tf_cs) == SEL_UPL) || ((frame.tf_eflags & PSL_VM) && !in_vm86call)) { /* user trap */ diff --git a/sys/conf/files.i386 b/sys/conf/files.i386 index 4b827e397bd4..ea9c66644c96 100644 --- a/sys/conf/files.i386 +++ b/sys/conf/files.i386 @@ -383,6 +383,7 @@ isa/syscons_isa.c optional sc isa/vga_isa.c optional vga kern/imgact_aout.c standard kern/imgact_gzip.c optional gzip +kern/kern_poll.c optional device_polling kern/link_aout.c standard kern/md4c.c optional netsmb kern/subr_diskmbr.c standard diff --git a/sys/conf/options.i386 b/sys/conf/options.i386 index 588171a83121..2bb269fbf7c4 100644 --- a/sys/conf/options.i386 +++ b/sys/conf/options.i386 @@ -209,6 +209,11 @@ NETSMBCRYPTO opt_netsmb.h # SMB/CIFS filesystem SMBFS +# ------------------------------- +# Polling device handling +# ------------------------------- +DEVICE_POLLING opt_global.h + # ------------------------------- # EOF # ------------------------------- diff --git a/sys/dev/dc/if_dc.c b/sys/dev/dc/if_dc.c index 139983bb089a..caee9d0d44cf 100644 --- a/sys/dev/dc/if_dc.c +++ b/sys/dev/dc/if_dc.c @@ -2473,6 +2473,13 @@ static void dc_rxeof(sc) while(!(sc->dc_ldata->dc_rx_list[i].dc_status & DC_RXSTAT_OWN)) { +#ifdef DEVICE_POLLING + if (ifp->if_ipending & IFF_POLLING) { + if (sc->rxcycles <= 0) + break; + sc->rxcycles--; + } +#endif /* DEVICE_POLLING */ cur_rx = &sc->dc_ldata->dc_rx_list[i]; rxstat = cur_rx->dc_status; m = sc->dc_cdata.dc_rx_chain[i]; @@ -2786,6 +2793,60 @@ static void dc_tx_underrun(sc) return; } +#ifdef DEVICE_POLLING +static poll_handler_t dc_poll; + +static void +dc_poll(struct ifnet *ifp, enum poll_cmd cmd, int count) +{ + struct dc_softc *sc = ifp->if_softc; + + if (cmd == POLL_DEREGISTER) { /* final call, enable interrupts */ + /* Re-enable interrupts. */ + CSR_WRITE_4(sc, DC_IMR, DC_INTRS); + return; + } + sc->rxcycles = count; + dc_rxeof(sc); + dc_txeof(sc); + if (ifp->if_snd.ifq_head != NULL && !(ifp->if_flags & IFF_OACTIVE)) + dc_start(ifp); + + if (cmd == POLL_AND_CHECK_STATUS) { /* also check status register */ + u_int32_t status; + + status = CSR_READ_4(sc, DC_ISR); + status &= (DC_ISR_RX_WATDOGTIMEO|DC_ISR_RX_NOBUF| + DC_ISR_TX_NOBUF|DC_ISR_TX_IDLE|DC_ISR_TX_UNDERRUN| + DC_ISR_BUS_ERR); + if (!status) + return ; + /* ack what we have */ + CSR_WRITE_4(sc, DC_ISR, status); + + if (status & (DC_ISR_RX_WATDOGTIMEO|DC_ISR_RX_NOBUF) ) { + u_int32_t r = CSR_READ_4(sc, DC_FRAMESDISCARDED); + ifp->if_ierrors += (r & 0xffff) + ((r >> 17) & 0x7ff); + + if (dc_rx_resync(sc)) + dc_rxeof(sc); + } + /* restart transmit unit if necessary */ + if (status & DC_ISR_TX_IDLE && sc->dc_cdata.dc_tx_cnt) + CSR_WRITE_4(sc, DC_TXSTART, 0xFFFFFFFF); + + if (status & DC_ISR_TX_UNDERRUN) + dc_tx_underrun(sc); + + if (status & DC_ISR_BUS_ERR) { + printf("dc_poll: dc%d bus error\n", sc->dc_unit); + dc_reset(sc); + dc_init(sc); + } + } +} +#endif /* DEVICE_POLLING */ + static void dc_intr(arg) void *arg; { @@ -2800,6 +2861,14 @@ static void dc_intr(arg) DC_LOCK(sc); ifp = &sc->arpcom.ac_if; +#ifdef DEVICE_POLLING + if (ifp->if_ipending & IFF_POLLING) + goto done; + if (ether_poll_register(dc_poll, ifp)) { /* ok, disable interrupts */ + CSR_WRITE_4(sc, DC_IMR, 0x00000000); + goto done; + } +#endif /* DEVICE_POLLING */ /* Suppress unwanted interrupts */ if (!(ifp->if_flags & IFF_UP)) { @@ -2864,6 +2933,7 @@ static void dc_intr(arg) if (ifp->if_snd.ifq_head != NULL) dc_start(ifp); +done: DC_UNLOCK(sc); return; @@ -3163,6 +3233,16 @@ static void dc_init(xsc) /* * Enable interrupts. */ +#ifdef DEVICE_POLLING + /* + * ... but only if we are not polling, and make sure they are off in + * the case of polling. Some cards (e.g. fxp) turn interrupts on + * after a reset. + */ + if (ifp->if_ipending & IFF_POLLING) + CSR_WRITE_4(sc, DC_IMR, 0x00000000); + else +#endif CSR_WRITE_4(sc, DC_IMR, DC_INTRS); CSR_WRITE_4(sc, DC_ISR, 0xFFFFFFFF); @@ -3378,6 +3458,9 @@ static void dc_stop(sc) callout_stop(&sc->dc_stat_ch); ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE); +#ifdef DEVICE_POLLING + ether_poll_deregister(ifp); +#endif DC_CLRBIT(sc, DC_NETCFG, (DC_NETCFG_RX_ON|DC_NETCFG_TX_ON)); CSR_WRITE_4(sc, DC_IMR, 0x00000000); diff --git a/sys/dev/dc/if_dcreg.h b/sys/dev/dc/if_dcreg.h index 0052d33d9220..8980e4433d18 100644 --- a/sys/dev/dc/if_dcreg.h +++ b/sys/dev/dc/if_dcreg.h @@ -453,7 +453,11 @@ struct dc_desc { #define DC_FILTER_HASHONLY 0x10400000 #define DC_MAXFRAGS 16 +#ifdef DEVICE_POLLING +#define DC_RX_LIST_CNT 192 +#else #define DC_RX_LIST_CNT 64 +#endif #define DC_TX_LIST_CNT 256 #define DC_MIN_FRAMELEN 60 #define DC_RXLEN 1536 @@ -717,6 +721,9 @@ struct dc_softc { int dc_srm_media; #endif struct mtx dc_mtx; +#ifdef DEVICE_POLLING + int rxcycles; /* ... when polling */ +#endif }; diff --git a/sys/dev/fxp/if_fxp.c b/sys/dev/fxp/if_fxp.c index 840ed8701eb1..27b78432b391 100644 --- a/sys/dev/fxp/if_fxp.c +++ b/sys/dev/fxp/if_fxp.c @@ -1141,6 +1141,38 @@ fxp_start(struct ifnet *ifp) } } +static void fxp_intr_body(struct fxp_softc *sc, u_int8_t statack, int count); + +#ifdef DEVICE_POLLING +static poll_handler_t fxp_poll; + +static void +fxp_poll(struct ifnet *ifp, enum poll_cmd cmd, int count) +{ + struct fxp_softc *sc = ifp->if_softc; + u_int8_t statack ; + + if (cmd == POLL_DEREGISTER) { /* final call, enable interrupts */ + CSR_WRITE_1(sc, FXP_CSR_SCB_INTRCNTL, 0); + return; + } + statack = FXP_SCB_STATACK_CXTNO | FXP_SCB_STATACK_CNA | + FXP_SCB_STATACK_FR ; + if (cmd == POLL_AND_CHECK_STATUS) { + u_int8_t tmp ; + tmp = CSR_READ_1(sc, FXP_CSR_SCB_STATACK); + if (tmp == 0xff || tmp == 0) + return ; /* nothing to do */ + tmp &= ~statack ; + /* ack what we can */ + if (tmp != 0) + CSR_WRITE_1(sc, FXP_CSR_SCB_STATACK, tmp); + statack |= tmp ; + } + fxp_intr_body(sc, statack, count); +} +#endif /* DEVICE_POLLING */ + /* * Process interface interrupts. */ @@ -1148,9 +1180,21 @@ static void fxp_intr(void *xsc) { struct fxp_softc *sc = xsc; - struct ifnet *ifp = &sc->sc_if; u_int8_t statack; +#ifdef DEVICE_POLLING + struct ifnet *ifp = &sc->sc_if; + + if (ifp->if_ipending & IFF_POLLING) + return ; + if (ether_poll_register(fxp_poll, ifp)) { + /* disable interrupts */ + CSR_WRITE_1(sc, FXP_CSR_SCB_INTRCNTL, FXP_SCB_INTR_DISABLE); + fxp_poll(ifp, 0, 1); + return ; + } +#endif + if (sc->suspended) { return; } @@ -1169,6 +1213,14 @@ fxp_intr(void *xsc) * First ACK all the interrupts in this pass. */ CSR_WRITE_1(sc, FXP_CSR_SCB_STATACK, statack); + fxp_intr_body(sc, statack, -1); + } +} + +static void +fxp_intr_body(struct fxp_softc *sc, u_int8_t statack, int count) +{ + struct ifnet *ifp = &sc->sc_if; /* * Free any finished transmit mbuf chains. @@ -1220,6 +1272,9 @@ fxp_intr(void *xsc) rfa = (struct fxp_rfa *)(m->m_ext.ext_buf + RFA_ALIGNMENT_FUDGE); +#ifdef DEVICE_POLLING /* loop at most count times if count >=0 */ + if (count < 0 || count-- > 0) +#endif if (rfa->rfa_status & FXP_RFA_STATUS_C) { /* * Remove first packet from the chain. @@ -1276,7 +1331,6 @@ fxp_intr(void *xsc) fxp_scb_cmd(sc, FXP_SCB_COMMAND_RU_START); } } - } } /* @@ -1404,6 +1458,9 @@ fxp_stop(struct fxp_softc *sc) ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE); ifp->if_timer = 0; +#ifdef DEVICE_POLLING + ether_poll_deregister(ifp); +#endif /* * Cancel stats updater. */ diff --git a/sys/dev/fxp/if_fxpvar.h b/sys/dev/fxp/if_fxpvar.h index 23fdaa43ded3..b2ae9b26bfaf 100644 --- a/sys/dev/fxp/if_fxpvar.h +++ b/sys/dev/fxp/if_fxpvar.h @@ -57,7 +57,11 @@ * Number of receive frame area buffers. These are large so chose * wisely. */ +#ifdef DEVICE_POLLING +#define FXP_NRFABUFS 192 +#else #define FXP_NRFABUFS 64 +#endif /* * Maximum number of seconds that the receiver can be idle before we diff --git a/sys/i386/i386/trap.c b/sys/i386/i386/trap.c index 23ad3fc2442f..51c4c79554f4 100644 --- a/sys/i386/i386/trap.c +++ b/sys/i386/i386/trap.c @@ -162,6 +162,11 @@ SYSCTL_INT(_machdep, OID_AUTO, panic_on_nmi, CTLFLAG_RW, extern char *syscallnames[]; #endif +#ifdef DEVICE_POLLING +extern u_int32_t poll_in_trap; +extern int ether_poll __P((int count)); +#endif /* DEVICE_POLLING */ + /* * Exception, fault, and trap interface to the FreeBSD kernel. * This common code is called from assembly language IDT gate entry @@ -239,6 +244,11 @@ trap(frame) trap_fatal(&frame, eva); } +#ifdef DEVICE_POLLING + if (poll_in_trap) + ether_poll(poll_in_trap); +#endif /* DEVICE_POLLING */ + if ((ISPL(frame.tf_cs) == SEL_UPL) || ((frame.tf_eflags & PSL_VM) && !in_vm86call)) { /* user trap */ diff --git a/sys/kern/kern_clock.c b/sys/kern/kern_clock.c index 4c2ccc0a7928..a338dca6a4e6 100644 --- a/sys/kern/kern_clock.c +++ b/sys/kern/kern_clock.c @@ -69,6 +69,12 @@ #include #endif +#ifdef DEVICE_POLLING +#include /* for NETISR_POLL */ + +extern void ether_poll1(void); +extern void hardclock_device_poll(void); +#endif /* DEVICE_POLLING */ static void initclocks __P((void *dummy)); SYSINIT(clocks, SI_SUB_CLOCKS, SI_ORDER_FIRST, initclocks, NULL) @@ -140,6 +146,9 @@ initclocks(dummy) psdiv = pscnt = 1; cpu_initclocks(); +#ifdef DEVICE_POLLING + register_netisr(NETISR_POLL, ether_poll1); +#endif /* * Compute profhz/stathz, and fix profhz if needed. */ @@ -212,6 +221,9 @@ hardclock(frame) statclock(frame); tc_windup(); +#ifdef DEVICE_POLLING + hardclock_device_poll(); +#endif /* DEVICE_POLLING */ /* * Process callouts at a very low cpu priority, so we don't keep the diff --git a/sys/kern/kern_intr.c b/sys/kern/kern_intr.c index e873c6eca819..88868dc5e46e 100644 --- a/sys/kern/kern_intr.c +++ b/sys/kern/kern_intr.c @@ -623,7 +623,16 @@ swi_net(void *dummy) u_int bits; int i; +#ifdef DEVICE_POLLING + for (;;) { + int pollmore; +#endif bits = atomic_readandclear_int(&netisr); +#ifdef DEVICE_POLLING + if (bits == 0) + return; + pollmore = bits & (1 << NETISR_POLL); +#endif while ((i = ffs(bits)) != 0) { i--; if (netisrs[i] != NULL) @@ -632,6 +641,11 @@ swi_net(void *dummy) printf("swi_net: unregistered isr number: %d.\n", i); bits &= ~(1 << i); } +#ifdef DEVICE_POLLING + if (pollmore) + ether_pollmore(); + } +#endif } /* diff --git a/sys/kern/kern_poll.c b/sys/kern/kern_poll.c new file mode 100644 index 000000000000..763e5cb25628 --- /dev/null +++ b/sys/kern/kern_poll.c @@ -0,0 +1,422 @@ +/*- + * Copyright (c) 2001 Luigi Rizzo + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * $FreeBSD$ + */ + +#include +#include +#include +#include /* needed by net/if.h */ +#include + +#include /* for IFF_* flags */ +#include /* for NETISR_POLL */ + +#ifdef SMP +#error DEVICE_POLLING is not compatible with SMP +#endif + +void ether_poll1(void); +void ether_poll(int); /* polling while in trap */ +void ether_pollmore(void); +void hardclock_device_poll(void); + +/* + * Polling support for [network] device drivers. + * + * Drivers which support this feature try to register with the + * polling code. + * + * If registration is successful, the driver must disable interrupts, + * and further I/O is performed through the handler, which is invoked + * (at least once per clock tick) with 3 arguments: the "arg" passed at + * register time (a struct ifnet pointer), a command, and a "count" limit. + * + * The command can be one of the following: + * POLL_ONLY: quick move of "count" packets from input/output queues. + * POLL_AND_CHECK_STATUS: as above, plus check status registers or do + * other more expensive operations. This command is issued periodically + * but less frequently than POLL_ONLY. + * POLL_DEREGISTER: deregister and return to interrupt mode. + * + * The first two commands are only issued if the interface is marked as + * 'IFF_UP and IFF_RUNNING', the last one only if IFF_RUNNING is set. + * + * The count limit specifies how much work the handler can do during the + * call -- typically this is the number of packets to be received, or + * transmitted, etc. (drivers are free to interpret this number, as long + * as the max time spent in the function grows roughly linearly with the + * count). + * + * Deregistration can be requested by the driver itself (typically in the + * *_stop() routine), or by the polling code, by invoking the handler. + * + * Polling can be globally enabled or disabled with the sysctl variable + * kern.polling.enable (default is 0, disabled) + * + * A second variable controls the sharing of CPU between polling/kernel + * network processing, and other activities (typically userlevel tasks): + * kern.polling.user_frac (between 0 and 100, default 50) sets the share + * of CPU allocated to user tasks. CPU is allocated proportionally to the + * shares, by dynamically adjusting the "count" (poll_burst). + * + * Other parameters can should be left to their default values. + * The following constraints hold + * + * 1 <= poll_each_burst <= poll_burst <= poll_burst_max + * 0 <= poll_in_trap <= poll_each_burst + * MIN_POLL_BURST_MAX <= poll_burst_max <= MAX_POLL_BURST_MAX + */ + +#define MIN_POLL_BURST_MAX 10 +#define MAX_POLL_BURST_MAX 1000 + +SYSCTL_NODE(_kern, OID_AUTO, polling, CTLFLAG_RW, 0, + "Device polling parameters"); + +static u_int32_t poll_burst = 5; +SYSCTL_ULONG(_kern_polling, OID_AUTO, burst, CTLFLAG_RW, + &poll_burst, 0, "Current polling burst size"); + +static u_int32_t poll_each_burst = 5; +SYSCTL_ULONG(_kern_polling, OID_AUTO, each_burst, CTLFLAG_RW, + &poll_each_burst, 0, "Max size of each burst"); + +static u_int32_t poll_burst_max = 150; /* good for 100Mbit net and HZ=1000 */ +SYSCTL_ULONG(_kern_polling, OID_AUTO, burst_max, CTLFLAG_RW, + &poll_burst_max, 0, "Max Polling burst size"); + +u_int32_t poll_in_trap; /* used in trap.c */ +SYSCTL_ULONG(_kern_polling, OID_AUTO, poll_in_trap, CTLFLAG_RW, + &poll_in_trap, 0, "Poll burst size during a trap"); + +static u_int32_t user_frac = 50; +SYSCTL_ULONG(_kern_polling, OID_AUTO, user_frac, CTLFLAG_RW, + &user_frac, 0, "Desired user fraction of cpu time"); + +static u_int32_t reg_frac = 20 ; +SYSCTL_ULONG(_kern_polling, OID_AUTO, reg_frac, CTLFLAG_RW, + ®_frac, 0, "Every this many cycles poll register"); + +static u_int32_t short_ticks; +SYSCTL_ULONG(_kern_polling, OID_AUTO, short_ticks, CTLFLAG_RW, + &short_ticks, 0, "Hardclock ticks shorter than they should be"); + +static u_int32_t lost_polls; +SYSCTL_ULONG(_kern_polling, OID_AUTO, lost_polls, CTLFLAG_RW, + &lost_polls, 0, "How many times we would have lost a poll tick"); + +static u_int32_t poll_handlers; /* next free entry in pr[]. */ +SYSCTL_ULONG(_kern_polling, OID_AUTO, handlers, CTLFLAG_RD, + &poll_handlers, 0, "Number of registered poll handlers"); + +static int polling = 0; /* global polling enable */ +SYSCTL_ULONG(_kern_polling, OID_AUTO, enable, CTLFLAG_RW, + &polling, 0, "Polling enabled"); + + +static u_int32_t poll1_active; +static u_int32_t need_poll_again; + +#define POLL_LIST_LEN 128 +struct pollrec { + poll_handler_t *handler; + struct ifnet *ifp; +}; + +static struct pollrec pr[POLL_LIST_LEN]; + +/* + * Hook from hardclock. Tries to schedule a netisr, but keeps track + * of lost ticks due to the previous handler taking too long. + * The first part of the code is just for debugging purposes, and tries + * to count how often hardclock ticks are shorter than they should, + * meaning either stray interrupts or delayed events. + */ +void +hardclock_device_poll(void) +{ + static struct timeval prev_t, t; + int delta; + + microuptime(&t); + delta = (t.tv_usec - prev_t.tv_usec) + + (t.tv_sec - prev_t.tv_sec)*1000000; + if (delta * hz < 500000) + short_ticks++; + else + prev_t = t; + + if (poll_handlers > 0) { + if (poll1_active) { + lost_polls++; + need_poll_again++; + } else { + poll1_active = 1; + schednetisr(NETISR_POLL); + } + } +} + +/* + * ether_poll is called from the idle loop or from the trap handler. + */ +void +ether_poll(int count) +{ + int i; + int s = splimp(); + + mtx_lock(&Giant); + + if (count > poll_each_burst) + count = poll_each_burst; + for (i = 0 ; i < poll_handlers ; i++) + if (pr[i].handler && (IFF_UP|IFF_RUNNING) == + (pr[i].ifp->if_flags & (IFF_UP|IFF_RUNNING)) ) + pr[i].handler(pr[i].ifp, 0, count); /* quick check */ + mtx_unlock(&Giant); + splx(s); +} + +/* + * ether_pollmore is called after other netisr's, possibly scheduling + * another NETISR_POLL call, or adapting the burst size for the next cycle. + * + * It is very bad to fetch large bursts of packets from a single card at once, + * because the burst could take a long time to be completely processed, or + * could saturate the intermediate queue (ipintrq or similar) leading to + * losses or unfairness. To reduce the problem, and also to account better for + * time spent in network-related processnig, we split the burst in smaller + * chunks of fixed size, giving control to the other netisr's between chunks. + * This helps in improving the fairness, reducing livelock (because we + * emulate more closely the "process to completion" that we have with + * fastforwarding) and accounting for the work performed in low level + * handling and forwarding. + */ + +static int residual_burst = 0; + +static struct timeval poll_start_t; + +void +ether_pollmore() +{ + struct timeval t; + int kern_load; + int s = splhigh(); + + if (residual_burst > 0) { + schednetisr(NETISR_POLL); + /* will run immediately on return, followed by netisrs */ + splx(s); + return ; + } + /* here we can account time spent in netisr's in this tick */ + microuptime(&t); + kern_load = (t.tv_usec - poll_start_t.tv_usec) + + (t.tv_sec - poll_start_t.tv_sec)*1000000; /* us */ + kern_load = (kern_load * hz) / 10000; /* 0..100 */ + if (kern_load > (100 - user_frac)) { /* try decrease ticks */ + if (poll_burst > 1) + poll_burst--; + } else { + if (poll_burst < poll_burst_max) + poll_burst++; + } + + if (need_poll_again) { + /* + * Last cycle was long and caused us to miss one or more + * hardclock ticks. Restart processnig again, but slightly + * reduce the burst size to prevent that this happens again. + */ + need_poll_again--; + poll_burst -= (poll_burst / 8); + if (poll_burst < 1) + poll_burst = 1; + schednetisr(NETISR_POLL); + } else + poll1_active = 0; + splx(s); +} + +/* + * ether_poll1 is called by schednetisr when appropriate, typically once + * per tick. It is called at splnet() so first thing to do is to upgrade to + * splimp(), and call all registered handlers. + */ +void +ether_poll1(void) +{ + static int reg_frac_count; + int i, cycles; + enum poll_cmd arg = POLL_ONLY; + int s=splimp(); + mtx_lock(&Giant); + + if (residual_burst == 0) { /* first call in this tick */ + microuptime(&poll_start_t); + /* + * Check that paremeters are consistent with runtime + * variables. Some of these tests could be done at sysctl + * time, but the savings would be very limited because we + * still have to check against reg_frac_count and + * poll_each_burst. So, instead of writing separate sysctl + * handlers, we do all here. + */ + + if (reg_frac > hz) + reg_frac = hz; + else if (reg_frac < 1) + reg_frac = 1; + if (reg_frac_count > reg_frac) + reg_frac_count = reg_frac - 1; + if (reg_frac_count-- == 0) { + arg = POLL_AND_CHECK_STATUS; + reg_frac_count = reg_frac - 1; + } + if (poll_burst_max < MIN_POLL_BURST_MAX) + poll_burst_max = MIN_POLL_BURST_MAX; + else if (poll_burst_max > MAX_POLL_BURST_MAX) + poll_burst_max = MAX_POLL_BURST_MAX; + + if (poll_each_burst < 1) + poll_each_burst = 1; + else if (poll_each_burst > poll_burst_max) + poll_each_burst = poll_burst_max; + + residual_burst = poll_burst; + } + cycles = (residual_burst < poll_each_burst) ? + residual_burst : poll_each_burst; + residual_burst -= cycles; + + if (polling) { + for (i = 0 ; i < poll_handlers ; i++) + if (pr[i].handler && (IFF_UP|IFF_RUNNING) == + (pr[i].ifp->if_flags & (IFF_UP|IFF_RUNNING)) ) + pr[i].handler(pr[i].ifp, arg, cycles); + } else { /* unregister */ + for (i = 0 ; i < poll_handlers ; i++) { + if (pr[i].handler && + pr[i].ifp->if_flags & IFF_RUNNING) { + pr[i].ifp->if_ipending &= ~IFF_POLLING; + pr[i].handler(pr[i].ifp, POLL_DEREGISTER, 1); + } + pr[i].handler=NULL; + } + residual_burst = 0; + poll_handlers = 0; + } + /* on -stable, schednetisr(NETISR_POLLMORE); */ + mtx_unlock(&Giant); + splx(s); +} + +/* + * Try to register routine for polling. Returns 1 if successful + * (and polling should be enabled), 0 otherwise. + * A device is not supposed to register itself multiple times. + * + * This is called from within the *_intr() function, so we should + * probably not need further locking. XXX + */ +int +ether_poll_register(poll_handler_t *h, struct ifnet *ifp) +{ + int s; + + if (polling == 0) /* polling disabled, cannot register */ + return 0; + if (h == NULL || ifp == NULL) /* bad arguments */ + return 0; + if ( !(ifp->if_flags & IFF_UP) ) /* must be up */ + return 0; + if (ifp->if_ipending & IFF_POLLING) /* already polling */ + return 0; + + s = splhigh(); + if (poll_handlers >= POLL_LIST_LEN) { + /* + * List full, cannot register more entries. + * This should never happen; if it does, it is probably a + * broken driver trying to register multiple times. Checking + * this at runtime is expensive, and won't solve the problem + * anyways, so just report a few times and then give up. + */ + static int verbose = 10 ; + splx(s); + if (verbose >0) { + printf("poll handlers list full, " + "maybe a broken driver ?\n"); + verbose--; + } + return 0; /* no polling for you */ + } + + pr[poll_handlers].handler = h; + pr[poll_handlers].ifp = ifp; + poll_handlers++; + ifp->if_ipending |= IFF_POLLING; + splx(s); + return 1; /* polling enabled in next call */ +} + +/* + * Remove the interface from the list of polling ones. + * Normally run by *_stop(). + * We allow it being called with IFF_POLLING clear, the + * call is sufficiently rare so it is preferable to save the + * space for the extra test in each device in exchange of one + * additional function call. + */ +int +ether_poll_deregister(struct ifnet *ifp) +{ + int i; + + mtx_lock(&Giant); + if ( !ifp || !(ifp->if_ipending & IFF_POLLING) ) { + mtx_unlock(&Giant); + return 0; + } + for (i = 0 ; i < poll_handlers ; i++) + if (pr[i].ifp == ifp) /* found it */ + break; + ifp->if_ipending &= ~IFF_POLLING; /* found or not... */ + if (i == poll_handlers) { + mtx_unlock(&Giant); + printf("ether_poll_deregister: ifp not found!!!\n"); + return 0; + } + poll_handlers--; + if (i < poll_handlers) { /* Last entry replaces this one. */ + pr[i].handler = pr[poll_handlers].handler; + pr[i].ifp = pr[poll_handlers].ifp; + } + mtx_unlock(&Giant); + return 1; +} diff --git a/sys/net/if.h b/sys/net/if.h index 83116b968257..6cc5522118c7 100644 --- a/sys/net/if.h +++ b/sys/net/if.h @@ -131,6 +131,15 @@ struct if_data { #define IFF_ALTPHYS IFF_LINK2 /* use alternate physical connection */ #define IFF_MULTICAST 0x8000 /* supports multicast */ +/* + * The following flag(s) ought to go in if_flags, but we cannot change + * struct ifnet because of binary compatibility, so we store them in + * if_ipending, which is not used so far. + * If possible, make sure the value is not conflicting with other + * IFF flags, so we have an easier time when we want to merge them. + */ +#define IFF_POLLING 0x10000 /* Interface is in polling mode. */ + /* flags set internally only: */ #define IFF_CANTCHANGE \ (IFF_BROADCAST|IFF_POINTOPOINT|IFF_RUNNING|IFF_OACTIVE|\ diff --git a/sys/net/if_var.h b/sys/net/if_var.h index 58d37804620f..dacea6319072 100644 --- a/sys/net/if_var.h +++ b/sys/net/if_var.h @@ -459,6 +459,15 @@ int if_clone_destroy __P((const char *)); #define IF_LLADDR(ifp) \ LLADDR((struct sockaddr_dl *) ifaddr_byindex((ifp)->if_index)->ifa_addr) +#ifdef DEVICE_POLLING +enum poll_cmd { POLL_ONLY, POLL_AND_CHECK_STATUS, POLL_DEREGISTER }; + +typedef void poll_handler_t __P((struct ifnet *ifp, + enum poll_cmd cmd, int count)); +int ether_poll_register __P((poll_handler_t *h, struct ifnet *ifp)); +int ether_poll_deregister __P((struct ifnet *ifp)); +#endif /* DEVICE_POLLING */ + #endif /* _KERNEL */ #endif /* !_NET_IF_VAR_H_ */ diff --git a/sys/net/netisr.h b/sys/net/netisr.h index 621cf2268b96..35e8cc132252 100644 --- a/sys/net/netisr.h +++ b/sys/net/netisr.h @@ -52,6 +52,7 @@ * interrupt used for scheduling the network code to calls * on the lowest level routine of each protocol. */ +#define NETISR_POLL 0 /* polling callback, must be first */ #define NETISR_IP 2 /* same as AF_INET */ #define NETISR_NS 6 /* same as AF_NS */ #define NETISR_ATALK 16 /* same as AF_APPLETALK */ diff --git a/sys/pci/if_dc.c b/sys/pci/if_dc.c index 139983bb089a..caee9d0d44cf 100644 --- a/sys/pci/if_dc.c +++ b/sys/pci/if_dc.c @@ -2473,6 +2473,13 @@ static void dc_rxeof(sc) while(!(sc->dc_ldata->dc_rx_list[i].dc_status & DC_RXSTAT_OWN)) { +#ifdef DEVICE_POLLING + if (ifp->if_ipending & IFF_POLLING) { + if (sc->rxcycles <= 0) + break; + sc->rxcycles--; + } +#endif /* DEVICE_POLLING */ cur_rx = &sc->dc_ldata->dc_rx_list[i]; rxstat = cur_rx->dc_status; m = sc->dc_cdata.dc_rx_chain[i]; @@ -2786,6 +2793,60 @@ static void dc_tx_underrun(sc) return; } +#ifdef DEVICE_POLLING +static poll_handler_t dc_poll; + +static void +dc_poll(struct ifnet *ifp, enum poll_cmd cmd, int count) +{ + struct dc_softc *sc = ifp->if_softc; + + if (cmd == POLL_DEREGISTER) { /* final call, enable interrupts */ + /* Re-enable interrupts. */ + CSR_WRITE_4(sc, DC_IMR, DC_INTRS); + return; + } + sc->rxcycles = count; + dc_rxeof(sc); + dc_txeof(sc); + if (ifp->if_snd.ifq_head != NULL && !(ifp->if_flags & IFF_OACTIVE)) + dc_start(ifp); + + if (cmd == POLL_AND_CHECK_STATUS) { /* also check status register */ + u_int32_t status; + + status = CSR_READ_4(sc, DC_ISR); + status &= (DC_ISR_RX_WATDOGTIMEO|DC_ISR_RX_NOBUF| + DC_ISR_TX_NOBUF|DC_ISR_TX_IDLE|DC_ISR_TX_UNDERRUN| + DC_ISR_BUS_ERR); + if (!status) + return ; + /* ack what we have */ + CSR_WRITE_4(sc, DC_ISR, status); + + if (status & (DC_ISR_RX_WATDOGTIMEO|DC_ISR_RX_NOBUF) ) { + u_int32_t r = CSR_READ_4(sc, DC_FRAMESDISCARDED); + ifp->if_ierrors += (r & 0xffff) + ((r >> 17) & 0x7ff); + + if (dc_rx_resync(sc)) + dc_rxeof(sc); + } + /* restart transmit unit if necessary */ + if (status & DC_ISR_TX_IDLE && sc->dc_cdata.dc_tx_cnt) + CSR_WRITE_4(sc, DC_TXSTART, 0xFFFFFFFF); + + if (status & DC_ISR_TX_UNDERRUN) + dc_tx_underrun(sc); + + if (status & DC_ISR_BUS_ERR) { + printf("dc_poll: dc%d bus error\n", sc->dc_unit); + dc_reset(sc); + dc_init(sc); + } + } +} +#endif /* DEVICE_POLLING */ + static void dc_intr(arg) void *arg; { @@ -2800,6 +2861,14 @@ static void dc_intr(arg) DC_LOCK(sc); ifp = &sc->arpcom.ac_if; +#ifdef DEVICE_POLLING + if (ifp->if_ipending & IFF_POLLING) + goto done; + if (ether_poll_register(dc_poll, ifp)) { /* ok, disable interrupts */ + CSR_WRITE_4(sc, DC_IMR, 0x00000000); + goto done; + } +#endif /* DEVICE_POLLING */ /* Suppress unwanted interrupts */ if (!(ifp->if_flags & IFF_UP)) { @@ -2864,6 +2933,7 @@ static void dc_intr(arg) if (ifp->if_snd.ifq_head != NULL) dc_start(ifp); +done: DC_UNLOCK(sc); return; @@ -3163,6 +3233,16 @@ static void dc_init(xsc) /* * Enable interrupts. */ +#ifdef DEVICE_POLLING + /* + * ... but only if we are not polling, and make sure they are off in + * the case of polling. Some cards (e.g. fxp) turn interrupts on + * after a reset. + */ + if (ifp->if_ipending & IFF_POLLING) + CSR_WRITE_4(sc, DC_IMR, 0x00000000); + else +#endif CSR_WRITE_4(sc, DC_IMR, DC_INTRS); CSR_WRITE_4(sc, DC_ISR, 0xFFFFFFFF); @@ -3378,6 +3458,9 @@ static void dc_stop(sc) callout_stop(&sc->dc_stat_ch); ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE); +#ifdef DEVICE_POLLING + ether_poll_deregister(ifp); +#endif DC_CLRBIT(sc, DC_NETCFG, (DC_NETCFG_RX_ON|DC_NETCFG_TX_ON)); CSR_WRITE_4(sc, DC_IMR, 0x00000000); diff --git a/sys/pci/if_dcreg.h b/sys/pci/if_dcreg.h index 0052d33d9220..8980e4433d18 100644 --- a/sys/pci/if_dcreg.h +++ b/sys/pci/if_dcreg.h @@ -453,7 +453,11 @@ struct dc_desc { #define DC_FILTER_HASHONLY 0x10400000 #define DC_MAXFRAGS 16 +#ifdef DEVICE_POLLING +#define DC_RX_LIST_CNT 192 +#else #define DC_RX_LIST_CNT 64 +#endif #define DC_TX_LIST_CNT 256 #define DC_MIN_FRAMELEN 60 #define DC_RXLEN 1536 @@ -717,6 +721,9 @@ struct dc_softc { int dc_srm_media; #endif struct mtx dc_mtx; +#ifdef DEVICE_POLLING + int rxcycles; /* ... when polling */ +#endif }; diff --git a/sys/pci/if_sis.c b/sys/pci/if_sis.c index e74cfa09720e..ec7ee8282304 100644 --- a/sys/pci/if_sis.c +++ b/sys/pci/if_sis.c @@ -1273,6 +1273,13 @@ static void sis_rxeof(sc) while(SIS_OWNDESC(&sc->sis_ldata.sis_rx_list[i])) { +#ifdef DEVICE_POLLING + if (ifp->if_ipending & IFF_POLLING) { + if (sc->rxcycles <= 0) + break; + sc->rxcycles--; + } +#endif /* DEVICE_POLLING */ cur_rx = &sc->sis_ldata.sis_rx_list[i]; rxstat = cur_rx->sis_rxstat; bus_dmamap_sync(sc->sis_tag, @@ -1441,6 +1448,55 @@ static void sis_tick(xsc) return; } +#ifdef DEVICE_POLLING +static poll_handler_t sis_poll; + +static void +sis_poll(struct ifnet *ifp, enum poll_cmd cmd, int count) +{ + struct sis_softc *sc = ifp->if_softc; + SIS_LOCK(sc); + if (cmd == POLL_DEREGISTER) { /* final call, enable interrupts */ + CSR_WRITE_4(sc, SIS_IER, 1); + goto done; + } + + /* + * On the sis, reading the status register also clears it. + * So before returning to intr mode we must make sure that all + * possible pending sources of interrupts have been served. + * In practice this means run to completion the *eof routines, + * and then call the interrupt routine + */ + sc->rxcycles = count; + sis_rxeof(sc); + sis_txeof(sc); + if (ifp->if_snd.ifq_head != NULL) + sis_start(ifp); + + if (sc->rxcycles > 0 || cmd == POLL_AND_CHECK_STATUS) { + u_int32_t status; + + /* Reading the ISR register clears all interrupts. */ + status = CSR_READ_4(sc, SIS_ISR); + + if (status & (SIS_ISR_RX_ERR|SIS_ISR_RX_OFLOW)) + sis_rxeoc(sc); + + if (status & (SIS_ISR_RX_IDLE)) + SIS_SETBIT(sc, SIS_CSR, SIS_CSR_RX_ENABLE); + + if (status & SIS_ISR_SYSERR) { + sis_reset(sc); + sis_init(sc); + } + } +done: + SIS_UNLOCK(sc); + return; +} +#endif /* DEVICE_POLLING */ + static void sis_intr(arg) void *arg; { @@ -1452,6 +1508,15 @@ static void sis_intr(arg) ifp = &sc->arpcom.ac_if; SIS_LOCK(sc); +#ifdef DEVICE_POLLING + if (ifp->if_ipending & IFF_POLLING) + goto done; + if (ether_poll_register(sis_poll, ifp)) { /* ok, disable interrupts */ + CSR_WRITE_4(sc, SIS_IER, 0); + goto done; + } +#endif /* DEVICE_POLLING */ + /* Supress unwanted interrupts */ if (!(ifp->if_flags & IFF_UP)) { sis_stop(sc); @@ -1741,6 +1806,15 @@ static void sis_init(xsc) * Enable interrupts. */ CSR_WRITE_4(sc, SIS_IMR, SIS_INTRS); +#ifdef DEVICE_POLLING + /* + * ... only enable interrupts if we are not polling, make sure + * they are off otherwise. + */ + if (ifp->if_ipending & IFF_POLLING) + CSR_WRITE_4(sc, SIS_IER, 0); + else +#endif /* DEVICE_POLLING */ CSR_WRITE_4(sc, SIS_IER, 1); /* Enable receiver and transmitter. */ @@ -1910,7 +1984,9 @@ static void sis_stop(sc) untimeout(sis_tick, sc, sc->sis_stat_ch); ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE); - +#ifdef DEVICE_POLLING + ether_poll_deregister(ifp); +#endif CSR_WRITE_4(sc, SIS_IER, 0); CSR_WRITE_4(sc, SIS_IMR, 0); SIS_SETBIT(sc, SIS_CSR, SIS_CSR_TX_DISABLE|SIS_CSR_RX_DISABLE); diff --git a/sys/pci/if_sisreg.h b/sys/pci/if_sisreg.h index 7ac424f0d970..803217507e0f 100644 --- a/sys/pci/if_sisreg.h +++ b/sys/pci/if_sisreg.h @@ -418,6 +418,9 @@ struct sis_softc { bus_dma_tag_t sis_tag; struct sis_ring_data sis_cdata; struct callout_handle sis_stat_ch; +#ifdef DEVICE_POLLING + int rxcycles; +#endif struct mtx sis_mtx; };