xref: /NextBSD/sys/sys/buf_ring_sc.h (revision 6c042d91cd0d3e48a1580e4b238708d2c6dc57c2)
1 /*-
2  * Copyright (c) 2007-2015 Kip Macy <kmacy@freebsd.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD$
27  *
28  */
29 
30 #ifndef	_SYS_BUF_RING_SC_H_
31 #define	_SYS_BUF_RING_SC_H_
32 
33 #include <machine/cpu.h>
34 struct buf_ring_sc;
35 
36 struct buf_ring_sc_stats_v0 {
37 	uint64_t brs_enqueues;
38 	uint64_t brs_drops;
39 	uint64_t brs_abdications;
40 	uint64_t brs_stalls;
41 	uint64_t brs_starts;
42 	uint64_t brs_restarts;
43 };
44 
45 struct buf_ring_sc_consumer {
46 
47 	/* driver 'drain' function to remove from sw queue and place on hw queue */
48 	int (*brsc_drain) (struct buf_ring_sc *br, int avail, void *sc);
49 
50 	/* allow draining to continue in another context */
51 	void (*brsc_deferred) (struct buf_ring_sc *br, void *sc);
52 
53 	void *brsc_sc;
54 	int brsc_domain;
55 	int brsc_flags;
56 
57 };
58 
59 /* cache line align buf ring entries */
60 #define BR_FLAGS_ALIGNED 0x1
61 #define BR_FLAGS_NUMA    0x2
62 
63 struct buf_ring_sc *buf_ring_sc_alloc(int count, struct malloc_type *type, int flags, struct buf_ring_sc_consumer *brsc);
64 void buf_ring_sc_free(struct buf_ring_sc *br, struct malloc_type *type);
65 void buf_ring_sc_reset_stats(struct buf_ring_sc *br);
66 void buf_ring_sc_get_stats_v0(struct buf_ring_sc *br, struct buf_ring_sc_stats_v0 *brss);
67 
68 /**
69  * buf_ring_sc_enqueue - enqueue a buffer to the ring, possibly
70  *  acquiring the consumer lock
71  * @ents: buffers to enqueue
72  * @count: number of buffers
73  *
74  * return values:
75  *  - 0        - success
76  *  - ENOBUFS  - failure - could not enqueue all bufs
77  */
78 int buf_ring_sc_enqueue(struct buf_ring_sc *br, void *ents[], int count, int budget);
79 
80 /**
81  * buf_ring_sc_drain - check ring for entries and drain
82  * @budget: if non-zero max entries to drain
83  * if zero does a blocking acquisition
84  *
85  */
86 void buf_ring_sc_drain(struct buf_ring_sc *br, int budget);
87 
88 /**
89  * buf_ring_sc_peek - check ring for entries
90  * @ents: array of returned values
91  * @count: the size of ents
92  *
93  * returns: number of entries in ents
94  *
95  * Populate ents with up to count entries from the ring.
96  * returns the number of entries in ents
97  * To be used only by the user specified drain function,
98  * and only once per-call.
99  */
100 int buf_ring_sc_peek(struct buf_ring_sc *br, void *ents[], uint16_t count);
101 
102 /**
103  * buf_ring_sc_putback - return a buffer to the ring
104  * @new: buffer to return
105  * @idx: offset from consumer index to return it to
106  *
107  * Used to return a buffer (most likely already there)
108  * to the top of the ring at offset idx from the current
109  * consumer index. The caller should *not* have advanced
110  * the index.
111  * To be used only by the user specified drain function.
112  */
113 void buf_ring_sc_putback(struct buf_ring_sc *br, void *new, int idx);
114 
115 /**
116  * buf_ring_sc_abdicate - indicate unlock intent
117  *
118  * Mark the ring as transitioning to unowned.
119  * Returns in a critical section. Caller is responsible
120  * for calling buf_ring_sc_unlock subsequently with no
121  * intervening blocking operations
122  * This function is an *optional* optimization to give a
123  * wider window for handoff to the next consumer.
124   * To be used only by the user specified drain function.
125  */
126 void buf_ring_sc_abdicate(struct buf_ring_sc *br);
127 
128 int buf_ring_sc_count(struct buf_ring_sc *br);
129 int buf_ring_sc_empty(struct buf_ring_sc *br);
130 int buf_ring_sc_full(struct buf_ring_sc *br);
131 #endif /* BUF_RING_SC_H_ */
132