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 */ 284c7070dbSScott Long 294c7070dbSScott Long #ifndef __NET_MP_RING_H 304c7070dbSScott Long #define __NET_MP_RING_H 314c7070dbSScott Long 324c7070dbSScott Long #ifndef _KERNEL 334c7070dbSScott Long #error "no user-serviceable parts inside" 344c7070dbSScott Long #endif 354c7070dbSScott Long 364c7070dbSScott Long struct ifmp_ring; 374c7070dbSScott Long typedef u_int (*mp_ring_drain_t)(struct ifmp_ring *, u_int, u_int); 384c7070dbSScott Long typedef u_int (*mp_ring_can_drain_t)(struct ifmp_ring *); 394c7070dbSScott Long typedef void (*mp_ring_serial_t)(struct ifmp_ring *); 404c7070dbSScott Long 41*33755776SMateusz Guzik #if defined(__powerpc__) || defined(__i386__) 425881181dSMatt Macy #define MP_RING_NO_64BIT_ATOMICS 435881181dSMatt Macy #endif 445881181dSMatt Macy 454c7070dbSScott Long struct ifmp_ring { 464c7070dbSScott Long volatile uint64_t state __aligned(CACHE_LINE_SIZE); 474c7070dbSScott Long 484c7070dbSScott Long int size __aligned(CACHE_LINE_SIZE); 494c7070dbSScott Long void * cookie; 504c7070dbSScott Long struct malloc_type * mt; 514c7070dbSScott Long mp_ring_drain_t drain; 524c7070dbSScott Long mp_ring_can_drain_t can_drain; /* cheap, may be unreliable */ 534c7070dbSScott Long counter_u64_t enqueues; 544c7070dbSScott Long counter_u64_t drops; 554c7070dbSScott Long counter_u64_t starts; 564c7070dbSScott Long counter_u64_t stalls; 574c7070dbSScott Long counter_u64_t restarts; /* recovered after stalling */ 584c7070dbSScott Long counter_u64_t abdications; 595881181dSMatt Macy #ifdef MP_RING_NO_64BIT_ATOMICS 604c7070dbSScott Long struct mtx lock; 614c7070dbSScott Long #endif 624c7070dbSScott Long void * volatile items[] __aligned(CACHE_LINE_SIZE); 634c7070dbSScott Long }; 644c7070dbSScott Long 654c7070dbSScott Long int ifmp_ring_alloc(struct ifmp_ring **, int, void *, mp_ring_drain_t, 664c7070dbSScott Long mp_ring_can_drain_t, struct malloc_type *, int); 674c7070dbSScott Long void ifmp_ring_free(struct ifmp_ring *); 68fe51d4cdSStephen Hurd int ifmp_ring_enqueue(struct ifmp_ring *, void **, int, int, int); 694c7070dbSScott Long void ifmp_ring_check_drainage(struct ifmp_ring *, int); 704c7070dbSScott Long void ifmp_ring_reset_stats(struct ifmp_ring *); 714c7070dbSScott Long int ifmp_ring_is_idle(struct ifmp_ring *); 724c7070dbSScott Long int ifmp_ring_is_stalled(struct ifmp_ring *r); 734c7070dbSScott Long #endif 74