xref: /freebsd/sys/dev/dpaa2/dpaa2_buf.h (revision ade8a27ea4c28d12fabc2d5f8e44386a3add23d1)
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 #include <sys/systm.h>
37 #include <sys/mbuf.h>
38 
39 #include <machine/bus.h>
40 
41 #define DPAA2_RX_BUF_SIZE	(MJUM9BYTES)
42 
43 struct dpaa2_buf {
44 	bus_addr_t		 paddr;
45 	caddr_t			 vaddr;
46 	bus_dma_tag_t		 dmat;
47 	bus_dmamap_t		 dmap;
48 	bus_dma_segment_t	 seg;
49 	int			 nseg;
50 	struct mbuf		*m;
51 	struct dpaa2_buf	*sgt;
52 	void			*opt;
53 };
54 
55 #define DPAA2_BUF_INIT_TAGOPT(__buf, __tag, __opt) do {			\
56 	KASSERT((__buf) != NULL, ("%s: buf is NULL", __func__));	\
57 									\
58 	(__buf)->paddr = 0;						\
59 	(__buf)->vaddr = NULL;						\
60 	(__buf)->dmat = (__tag);					\
61 	(__buf)->dmap = NULL;						\
62 	(__buf)->seg.ds_addr = 0;					\
63 	(__buf)->seg.ds_len = 0;					\
64 	(__buf)->nseg = 0;						\
65 	(__buf)->m = NULL;						\
66 	(__buf)->sgt = NULL;						\
67 	(__buf)->opt = (__opt);						\
68 } while(0)
69 #define DPAA2_BUF_INIT(__buf)	DPAA2_BUF_INIT_TAGOPT((__buf), NULL, NULL)
70 
71 #if defined(INVARIANTS)
72 /*
73  * TXPREP/TXREADY macros allow to verify whether Tx buffer is prepared to be
74  * seeded and/or ready to be used for transmission.
75  *
76  * NOTE: Any modification should be carefully analyzed and justified.
77  */
78 #define DPAA2_BUF_ASSERT_TXPREP(__buf) do {				\
79 	struct dpaa2_buf *__sgt = (__buf)->sgt;				\
80 	KASSERT((__sgt) != NULL, ("%s: no S/G table?", __func__));	\
81 									\
82 	KASSERT((__buf)->paddr == 0,    ("%s: paddr set?", __func__));	\
83 	KASSERT((__buf)->vaddr == NULL, ("%s: vaddr set?", __func__));	\
84 	KASSERT((__buf)->dmat  != NULL, ("%s: no DMA tag?", __func__));	\
85 	KASSERT((__buf)->dmap  == NULL, ("%s: DMA map set?", __func__)); \
86 	KASSERT((__buf)->seg.ds_addr == 0, ("%s: already mapped?", __func__)); \
87 	KASSERT((__buf)->seg.ds_len  == 0, ("%s: already mapped?", __func__)); \
88 	KASSERT((__buf)->nseg  == 0,    ("%s: nseg > 0?", __func__));	\
89 	KASSERT((__buf)->m     == NULL, ("%s: mbuf set?", __func__));	\
90 	KASSERT((__buf)->opt   != NULL, ("%s: no Tx ring?", __func__));	\
91 									\
92 	KASSERT((__sgt)->paddr == 0,    ("%s: S/G paddr set?", __func__)); \
93 	KASSERT((__sgt)->vaddr == NULL, ("%s: S/G vaddr set?", __func__)); \
94 	KASSERT((__sgt)->dmat  != NULL, ("%s: no S/G DMA tag?", __func__)); \
95 	KASSERT((__sgt)->dmap  == NULL, ("%s: S/G DMA map set?", __func__)); \
96 	KASSERT((__sgt)->seg.ds_addr == 0, ("%s: S/G mapped?", __func__)); \
97 	KASSERT((__sgt)->seg.ds_len  == 0, ("%s: S/G mapped?", __func__)); \
98 	KASSERT((__sgt)->nseg  == 0,    ("%s: S/G nseg > 0?", __func__)); \
99 	KASSERT((__sgt)->m     == NULL, ("%s: S/G mbuf set?", __func__)); \
100 	KASSERT((__sgt)->opt == (__buf),("%s: buf not linked?", __func__)); \
101 } while(0)
102 #define DPAA2_BUF_ASSERT_TXREADY(__buf) do {				\
103 	struct dpaa2_buf *__sgt = (__buf)->sgt;				\
104 	KASSERT((__sgt) != NULL,        ("%s: no S/G table?", __func__)); \
105 									\
106 	KASSERT((__buf)->paddr == 0,    ("%s: paddr set?", __func__));	\
107 	KASSERT((__buf)->vaddr == NULL, ("%s: vaddr set?", __func__));	\
108 	KASSERT((__buf)->dmat  != NULL, ("%s: no DMA tag?", __func__));	\
109 	KASSERT((__buf)->dmap  != NULL, ("%s: no DMA map?", __func__)); \
110 	KASSERT((__buf)->seg.ds_addr == 0, ("%s: already mapped?", __func__)); \
111 	KASSERT((__buf)->seg.ds_len  == 0, ("%s: already mapped?", __func__)); \
112 	KASSERT((__buf)->nseg  == 0,    ("%s: nseg > 0?", __func__));	\
113 	KASSERT((__buf)->m     == NULL, ("%s: mbuf set?", __func__));	\
114 	KASSERT((__buf)->opt   != NULL, ("%s: no Tx ring?", __func__));	\
115 									\
116 	KASSERT((__sgt)->paddr == 0,    ("%s: S/G paddr set?", __func__)); \
117 	KASSERT((__sgt)->vaddr != NULL, ("%s: no S/G vaddr?", __func__)); \
118 	KASSERT((__sgt)->dmat  != NULL, ("%s: no S/G DMA tag?", __func__)); \
119 	KASSERT((__sgt)->dmap  != NULL, ("%s: no S/G DMA map?", __func__)); \
120 	KASSERT((__sgt)->seg.ds_addr == 0, ("%s: S/G mapped?", __func__)); \
121 	KASSERT((__sgt)->seg.ds_len  == 0, ("%s: S/G mapped?", __func__)); \
122 	KASSERT((__sgt)->nseg  == 0,    ("%s: S/G nseg > 0?", __func__)); \
123 	KASSERT((__sgt)->m     == NULL, ("%s: S/G mbuf set?", __func__)); \
124 	KASSERT((__sgt)->opt == (__buf),("%s: buf not linked?", __func__)); \
125 } while(0)
126 #else /* !INVARIANTS */
127 #define DPAA2_BUF_ASSERT_TXPREP(__buf) do {	\
128 } while(0)
129 #define DPAA2_BUF_ASSERT_TXREADY(__buf) do {	\
130 } while(0)
131 #endif /* INVARIANTS */
132 
133 #if defined(INVARIANTS)
134 /*
135  * RXPREP/RXREADY macros allow to verify whether Rx buffer is prepared to be
136  * seeded and/or ready to be used for reception.
137  *
138  * NOTE: Any modification should be carefully analyzed and justified.
139  */
140 #define DPAA2_BUF_ASSERT_RXPREP(__buf) do {				\
141 	KASSERT((__buf)->paddr == 0,    ("%s: paddr set?", __func__));	\
142 	KASSERT((__buf)->vaddr == NULL, ("%s: vaddr set?", __func__));	\
143 	KASSERT((__buf)->dmat  != NULL, ("%s: no DMA tag?", __func__));	\
144 	/* KASSERT((__buf)->dmap  == NULL, ("%s: DMA map set?", __func__)); */ \
145 	KASSERT((__buf)->seg.ds_addr == 0, ("%s: already mapped?", __func__)); \
146 	KASSERT((__buf)->seg.ds_len  == 0, ("%s: already mapped?", __func__)); \
147 	KASSERT((__buf)->nseg  == 0,    ("%s: nseg > 0?", __func__));	\
148 	KASSERT((__buf)->m     == NULL, ("%s: mbuf set?", __func__));	\
149 	KASSERT((__buf)->sgt   == NULL, ("%s: S/G table set?", __func__)); \
150 	KASSERT((__buf)->opt   != NULL, ("%s: no channel?", __func__));	\
151 } while(0)
152 #define DPAA2_BUF_ASSERT_RXREADY(__buf) do {				\
153 	KASSERT((__buf)->paddr != 0,    ("%s: paddr not set?", __func__)); \
154 	KASSERT((__buf)->vaddr != NULL, ("%s: vaddr not set?", __func__)); \
155 	KASSERT((__buf)->dmat  != NULL, ("%s: no DMA tag?", __func__));	\
156 	KASSERT((__buf)->dmap  != NULL, ("%s: no DMA map?", __func__)); \
157 	KASSERT((__buf)->seg.ds_addr != 0, ("%s: not mapped?", __func__)); \
158 	KASSERT((__buf)->seg.ds_len  != 0, ("%s: not mapped?", __func__)); \
159 	KASSERT((__buf)->nseg  == 1,    ("%s: nseg != 1?", __func__));	\
160 	KASSERT((__buf)->m     != NULL, ("%s: no mbuf?", __func__));	\
161 	KASSERT((__buf)->sgt   == NULL, ("%s: S/G table set?", __func__)); \
162 	KASSERT((__buf)->opt   != NULL, ("%s: no channel?", __func__));	\
163 } while(0)
164 #else /* !INVARIANTS */
165 #define DPAA2_BUF_ASSERT_RXPREP(__buf) do {	\
166 } while(0)
167 #define DPAA2_BUF_ASSERT_RXREADY(__buf) do {	\
168 } while(0)
169 #endif /* INVARIANTS */
170 
171 int dpaa2_buf_seed_pool(device_t, device_t, void *, uint32_t, int, struct mtx *);
172 int dpaa2_buf_seed_rxb(device_t, struct dpaa2_buf *, int, struct mtx *);
173 int dpaa2_buf_seed_txb(device_t, struct dpaa2_buf *);
174 
175 #endif /* _DPAA2_BUF_H */
176