xref: /freebsd/sys/dev/dpaa2/dpaa2_buf.h (revision 6f1af0d7d2af54b339b5212434cd6d4fda628d80)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright © 2023 Dmitry Salychev
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 #ifndef	_DPAA2_BUF_H
29 #define	_DPAA2_BUF_H
30 
31 #include <sys/types.h>
32 #include <sys/param.h>
33 #include <sys/malloc.h>
34 #include <sys/lock.h>
35 #include <sys/mutex.h>
36 
37 #include <machine/bus.h>
38 
39 #define DPAA2_RX_BUF_SIZE	(MJUM9BYTES)
40 
41 struct dpaa2_buf {
42 	bus_addr_t		 paddr;
43 	caddr_t			 vaddr;
44 	bus_dma_tag_t		 dmat;
45 	bus_dmamap_t		 dmap;
46 	bus_dma_segment_t	 seg;
47 	int			 nseg;
48 	struct mbuf		*m;
49 	struct dpaa2_buf	*sgt;
50 	void			*opt;
51 };
52 
53 #define DPAA2_BUF_INIT_TAGOPT(__buf, __tag, __opt) do {			\
54 	KASSERT((__buf) != NULL, ("%s: buf is NULL", __func__));	\
55 									\
56 	(__buf)->paddr = 0;						\
57 	(__buf)->vaddr = NULL;						\
58 	(__buf)->dmat = (__tag);					\
59 	(__buf)->dmap = NULL;						\
60 	(__buf)->seg.ds_addr = 0;					\
61 	(__buf)->seg.ds_len = 0;					\
62 	(__buf)->nseg = 0;						\
63 	(__buf)->m = NULL;						\
64 	(__buf)->sgt = NULL;						\
65 	(__buf)->opt = (__opt);						\
66 } while(0)
67 #define DPAA2_BUF_INIT(__buf)	DPAA2_BUF_INIT_TAGOPT((__buf), NULL, NULL)
68 
69 #if defined(INVARIANTS)
70 /*
71  * TXPREP/TXREADY macros allow to verify whether Tx buffer is prepared to be
72  * seeded and/or ready to be used for transmission.
73  *
74  * NOTE: Any modification should be carefully analyzed and justified.
75  */
76 #define DPAA2_BUF_ASSERT_TXPREP(__buf) do {				\
77 	struct dpaa2_buf *__sgt = (__buf)->sgt;				\
78 	KASSERT((__sgt) != NULL, ("%s: no S/G table?", __func__));	\
79 									\
80 	KASSERT((__buf)->paddr == 0,    ("%s: paddr set?", __func__));	\
81 	KASSERT((__buf)->vaddr == NULL, ("%s: vaddr set?", __func__));	\
82 	KASSERT((__buf)->dmat  != NULL, ("%s: no DMA tag?", __func__));	\
83 	KASSERT((__buf)->dmap  == NULL, ("%s: DMA map set?", __func__)); \
84 	KASSERT((__buf)->seg.ds_addr == 0, ("%s: already mapped?", __func__)); \
85 	KASSERT((__buf)->seg.ds_len  == 0, ("%s: already mapped?", __func__)); \
86 	KASSERT((__buf)->nseg  == 0,    ("%s: nseg > 0?", __func__));	\
87 	KASSERT((__buf)->m     == NULL, ("%s: mbuf set?", __func__));	\
88 	KASSERT((__buf)->opt   != NULL, ("%s: no Tx ring?", __func__));	\
89 									\
90 	KASSERT((__sgt)->paddr == 0,    ("%s: S/G paddr set?", __func__)); \
91 	KASSERT((__sgt)->vaddr == NULL, ("%s: S/G vaddr set?", __func__)); \
92 	KASSERT((__sgt)->dmat  != NULL, ("%s: no S/G DMA tag?", __func__)); \
93 	KASSERT((__sgt)->dmap  == NULL, ("%s: S/G DMA map set?", __func__)); \
94 	KASSERT((__sgt)->seg.ds_addr == 0, ("%s: S/G mapped?", __func__)); \
95 	KASSERT((__sgt)->seg.ds_len  == 0, ("%s: S/G mapped?", __func__)); \
96 	KASSERT((__sgt)->nseg  == 0,    ("%s: S/G nseg > 0?", __func__)); \
97 	KASSERT((__sgt)->m     == NULL, ("%s: S/G mbuf set?", __func__)); \
98 	KASSERT((__sgt)->opt == (__buf),("%s: buf not linked?", __func__)); \
99 } while(0)
100 #define DPAA2_BUF_ASSERT_TXREADY(__buf) do {				\
101 	struct dpaa2_buf *__sgt = (__buf)->sgt;				\
102 	KASSERT((__sgt) != NULL,        ("%s: no S/G table?", __func__)); \
103 									\
104 	KASSERT((__buf)->paddr == 0,    ("%s: paddr set?", __func__));	\
105 	KASSERT((__buf)->vaddr == NULL, ("%s: vaddr set?", __func__));	\
106 	KASSERT((__buf)->dmat  != NULL, ("%s: no DMA tag?", __func__));	\
107 	KASSERT((__buf)->dmap  != NULL, ("%s: no DMA map?", __func__)); \
108 	KASSERT((__buf)->seg.ds_addr == 0, ("%s: already mapped?", __func__)); \
109 	KASSERT((__buf)->seg.ds_len  == 0, ("%s: already mapped?", __func__)); \
110 	KASSERT((__buf)->nseg  == 0,    ("%s: nseg > 0?", __func__));	\
111 	KASSERT((__buf)->m     == NULL, ("%s: mbuf set?", __func__));	\
112 	KASSERT((__buf)->opt   != NULL, ("%s: no Tx ring?", __func__));	\
113 									\
114 	KASSERT((__sgt)->paddr == 0,    ("%s: S/G paddr set?", __func__)); \
115 	KASSERT((__sgt)->vaddr != NULL, ("%s: no S/G vaddr?", __func__)); \
116 	KASSERT((__sgt)->dmat  != NULL, ("%s: no S/G DMA tag?", __func__)); \
117 	KASSERT((__sgt)->dmap  != NULL, ("%s: no S/G DMA map?", __func__)); \
118 	KASSERT((__sgt)->seg.ds_addr == 0, ("%s: S/G mapped?", __func__)); \
119 	KASSERT((__sgt)->seg.ds_len  == 0, ("%s: S/G mapped?", __func__)); \
120 	KASSERT((__sgt)->nseg  == 0,    ("%s: S/G nseg > 0?", __func__)); \
121 	KASSERT((__sgt)->m     == NULL, ("%s: S/G mbuf set?", __func__)); \
122 	KASSERT((__sgt)->opt == (__buf),("%s: buf not linked?", __func__)); \
123 } while(0)
124 #else /* !INVARIANTS */
125 #define DPAA2_BUF_ASSERT_TXPREP(__buf) do {	\
126 } while(0)
127 #define DPAA2_BUF_ASSERT_TXREADY(__buf) do {	\
128 } while(0)
129 #endif /* INVARIANTS */
130 
131 #if defined(INVARIANTS)
132 /*
133  * RXPREP/RXREADY macros allow to verify whether Rx buffer is prepared to be
134  * seeded and/or ready to be used for reception.
135  *
136  * NOTE: Any modification should be carefully analyzed and justified.
137  */
138 #define DPAA2_BUF_ASSERT_RXPREP(__buf) do {				\
139 	KASSERT((__buf)->paddr == 0,    ("%s: paddr set?", __func__));	\
140 	KASSERT((__buf)->vaddr == NULL, ("%s: vaddr set?", __func__));	\
141 	KASSERT((__buf)->dmat  != NULL, ("%s: no DMA tag?", __func__));	\
142 	/* KASSERT((__buf)->dmap  == NULL, ("%s: DMA map set?", __func__)); */ \
143 	KASSERT((__buf)->seg.ds_addr == 0, ("%s: already mapped?", __func__)); \
144 	KASSERT((__buf)->seg.ds_len  == 0, ("%s: already mapped?", __func__)); \
145 	KASSERT((__buf)->nseg  == 0,    ("%s: nseg > 0?", __func__));	\
146 	KASSERT((__buf)->m     == NULL, ("%s: mbuf set?", __func__));	\
147 	KASSERT((__buf)->sgt   == NULL, ("%s: S/G table set?", __func__)); \
148 	KASSERT((__buf)->opt   != NULL, ("%s: no channel?", __func__));	\
149 } while(0)
150 #define DPAA2_BUF_ASSERT_RXREADY(__buf) do {				\
151 	KASSERT((__buf)->paddr != 0,    ("%s: paddr not set?", __func__)); \
152 	KASSERT((__buf)->vaddr != NULL, ("%s: vaddr not set?", __func__)); \
153 	KASSERT((__buf)->dmat  != NULL, ("%s: no DMA tag?", __func__));	\
154 	KASSERT((__buf)->dmap  != NULL, ("%s: no DMA map?", __func__)); \
155 	KASSERT((__buf)->seg.ds_addr != 0, ("%s: not mapped?", __func__)); \
156 	KASSERT((__buf)->seg.ds_len  != 0, ("%s: not mapped?", __func__)); \
157 	KASSERT((__buf)->nseg  == 1,    ("%s: nseg != 1?", __func__));	\
158 	KASSERT((__buf)->m     != NULL, ("%s: no mbuf?", __func__));	\
159 	KASSERT((__buf)->sgt   == NULL, ("%s: S/G table set?", __func__)); \
160 	KASSERT((__buf)->opt   != NULL, ("%s: no channel?", __func__));	\
161 } while(0)
162 #else /* !INVARIANTS */
163 #define DPAA2_BUF_ASSERT_RXPREP(__buf) do {	\
164 } while(0)
165 #define DPAA2_BUF_ASSERT_RXREADY(__buf) do {	\
166 } while(0)
167 #endif /* INVARIANTS */
168 
169 int dpaa2_buf_seed_pool(device_t, device_t, void *, uint32_t, int, struct mtx *);
170 int dpaa2_buf_seed_rxb(device_t, struct dpaa2_buf *, int, struct mtx *);
171 int dpaa2_buf_seed_txb(device_t, struct dpaa2_buf *);
172 
173 #endif /* _DPAA2_BUF_H */
174