14c7070dbSScott Long /*- 24c7070dbSScott Long * Copyright (c) 2014 Chelsio Communications, Inc. 34c7070dbSScott Long * All rights reserved. 44c7070dbSScott Long * Written by: Navdeep Parhar <np@FreeBSD.org> 54c7070dbSScott Long * 64c7070dbSScott Long * Redistribution and use in source and binary forms, with or without 74c7070dbSScott Long * modification, are permitted provided that the following conditions 84c7070dbSScott Long * are met: 94c7070dbSScott Long * 1. Redistributions of source code must retain the above copyright 104c7070dbSScott Long * notice, this list of conditions and the following disclaimer. 114c7070dbSScott Long * 2. Redistributions in binary form must reproduce the above copyright 124c7070dbSScott Long * notice, this list of conditions and the following disclaimer in the 134c7070dbSScott Long * documentation and/or other materials provided with the distribution. 144c7070dbSScott Long * 154c7070dbSScott Long * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 164c7070dbSScott Long * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 174c7070dbSScott Long * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 184c7070dbSScott Long * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 194c7070dbSScott Long * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 204c7070dbSScott Long * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 214c7070dbSScott Long * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 224c7070dbSScott Long * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 234c7070dbSScott Long * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 244c7070dbSScott Long * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 254c7070dbSScott Long * SUCH DAMAGE. 264c7070dbSScott Long * 274c7070dbSScott Long * $FreeBSD$ 284c7070dbSScott Long * 294c7070dbSScott Long */ 304c7070dbSScott Long 314c7070dbSScott Long #ifndef __NET_MP_RING_H 324c7070dbSScott Long #define __NET_MP_RING_H 334c7070dbSScott Long 344c7070dbSScott Long #ifndef _KERNEL 354c7070dbSScott Long #error "no user-serviceable parts inside" 364c7070dbSScott Long #endif 374c7070dbSScott Long 384c7070dbSScott Long struct ifmp_ring; 394c7070dbSScott Long typedef u_int (*mp_ring_drain_t)(struct ifmp_ring *, u_int, u_int); 404c7070dbSScott Long typedef u_int (*mp_ring_can_drain_t)(struct ifmp_ring *); 414c7070dbSScott Long typedef void (*mp_ring_serial_t)(struct ifmp_ring *); 424c7070dbSScott Long 43*33755776SMateusz Guzik #if defined(__powerpc__) || defined(__i386__) 445881181dSMatt Macy #define MP_RING_NO_64BIT_ATOMICS 455881181dSMatt Macy #endif 465881181dSMatt Macy 474c7070dbSScott Long struct ifmp_ring { 484c7070dbSScott Long volatile uint64_t state __aligned(CACHE_LINE_SIZE); 494c7070dbSScott Long 504c7070dbSScott Long int size __aligned(CACHE_LINE_SIZE); 514c7070dbSScott Long void * cookie; 524c7070dbSScott Long struct malloc_type * mt; 534c7070dbSScott Long mp_ring_drain_t drain; 544c7070dbSScott Long mp_ring_can_drain_t can_drain; /* cheap, may be unreliable */ 554c7070dbSScott Long counter_u64_t enqueues; 564c7070dbSScott Long counter_u64_t drops; 574c7070dbSScott Long counter_u64_t starts; 584c7070dbSScott Long counter_u64_t stalls; 594c7070dbSScott Long counter_u64_t restarts; /* recovered after stalling */ 604c7070dbSScott Long counter_u64_t abdications; 615881181dSMatt Macy #ifdef MP_RING_NO_64BIT_ATOMICS 624c7070dbSScott Long struct mtx lock; 634c7070dbSScott Long #endif 644c7070dbSScott Long void * volatile items[] __aligned(CACHE_LINE_SIZE); 654c7070dbSScott Long }; 664c7070dbSScott Long 674c7070dbSScott Long int ifmp_ring_alloc(struct ifmp_ring **, int, void *, mp_ring_drain_t, 684c7070dbSScott Long mp_ring_can_drain_t, struct malloc_type *, int); 694c7070dbSScott Long void ifmp_ring_free(struct ifmp_ring *); 70fe51d4cdSStephen Hurd int ifmp_ring_enqueue(struct ifmp_ring *, void **, int, int, int); 714c7070dbSScott Long void ifmp_ring_check_drainage(struct ifmp_ring *, int); 724c7070dbSScott Long void ifmp_ring_reset_stats(struct ifmp_ring *); 734c7070dbSScott Long int ifmp_ring_is_idle(struct ifmp_ring *); 744c7070dbSScott Long int ifmp_ring_is_stalled(struct ifmp_ring *r); 754c7070dbSScott Long #endif 76