1 /*        $NetBSD: dmovervar.h,v 1.11 2024/09/08 09:36:50 rillig Exp $          */
2 
3 /*
4  * Copyright (c) 2002, 2003 Wasabi Systems, Inc.
5  * All rights reserved.
6  *
7  * Written by Jason R. Thorpe for Wasabi Systems, Inc.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. All advertising materials mentioning features or use of this software
18  *    must display the following acknowledgement:
19  *        This product includes software developed for the NetBSD Project by
20  *        Wasabi Systems, Inc.
21  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
22  *    or promote products derived from this software without specific prior
23  *    written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
29  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35  * POSSIBILITY OF SUCH DAMAGE.
36  */
37 
38 #ifndef _DMOVER_DMOVERVAR_H_
39 #define _DMOVER_DMOVERVAR_H_
40 
41 #include <sys/lock.h>
42 #include <sys/queue.h>
43 
44 /*
45  * Types of buffers the dmover-api can handle.
46  */
47 typedef enum {
48           DMOVER_BUF_NONE                         = 0,
49           DMOVER_BUF_LINEAR             = 1,
50           DMOVER_BUF_UIO                          = 2
51 } dmover_buffer_type;
52 
53 typedef struct {
54           void *l_addr;
55           size_t l_len;
56 } dmover_buf_linear;
57 
58 typedef union {
59           dmover_buf_linear dmbuf_linear;
60           struct uio *dmbuf_uio;
61 } dmover_buffer;
62 
63 /*
64  * dmover_algdesc:
65  *
66  *        This structure describes a dmover algorithm.
67  *
68  *        All members of this structure are public.
69  */
70 struct dmover_algdesc {
71           const char *dad_name;                   /* algorithm name */
72           void *dad_data;                         /* opaque algorithm description */
73           int dad_ninputs;              /* number of inputs */
74 };
75 
76 /*
77  * dmover_assignment:
78  *
79  *        This structure contains the information necessary to assign
80  *        a request to a back-end.
81  *
82  *        All members of this structure are public.
83  */
84 struct dmover_assignment {
85           struct dmover_backend *das_backend;
86           const struct dmover_algdesc *das_algdesc;
87 };
88 
89 /*
90  * dmover_session:
91  *
92  *        State for a dmover session.
93  */
94 struct dmover_session {
95           /*
96            * PUBLIC MEMBERS
97            */
98           void      *dses_cookie;                 /* for client */
99           int       dses_ninputs;                 /* number of inputs for function */
100 
101           /*
102            * PRIVATE MEMBERS
103            */
104           LIST_ENTRY(dmover_session) __dses_list;
105 
106           /*
107            * XXX Assignment is static when a session is
108            * XXX created, for now.
109            */
110           struct dmover_assignment __dses_assignment;
111 
112           /* List of active requests on this session. */
113           TAILQ_HEAD(, dmover_request) __dses_pendreqs;
114           int       __dses_npendreqs;
115 };
116 
117 #define   dmover_session_insque(dses, dreq)                                     \
118 do {                                                                                      \
119           TAILQ_INSERT_TAIL(&(dses)->__dses_pendreqs, (dreq), dreq_sesq);       \
120           (dses)->__dses_npendreqs++;                                           \
121 } while (/*CONSTCOND*/0)
122 
123 #define   dmover_session_remque(dses, dreq)                                     \
124 do {                                                                                      \
125           TAILQ_REMOVE(&(dses)->__dses_pendreqs, (dreq), dreq_sesq);  \
126           (dses)->__dses_npendreqs--;                                           \
127 } while (/*CONSTCOND*/0)
128 
129 /*
130  * dmover_request:
131  *
132  *        A data dmover request.
133  */
134 struct dmover_request {
135           /*
136            * PUBLIC MEMBERS
137            */
138 
139           /* Links on session and back-end queues. */
140           TAILQ_ENTRY(dmover_request) dreq_sesq;
141           TAILQ_ENTRY(dmover_request) dreq_dmbq;
142 
143           /* Pointer to our session. */
144           struct dmover_session *dreq_session;
145 
146           /* Our current back-end assignment. */
147           struct dmover_assignment *dreq_assignment;
148 
149           /* Function to call when processing is complete. */
150           void      (*dreq_callback)(struct dmover_request *);
151           void      *dreq_cookie;       /* for client */
152 
153           volatile int dreq_flags; /* flags; see below */
154           int       dreq_error;            /* valid if DMOVER_REQ_ERROR is set */
155 
156           /*
157            * General purpose immediate value.  Can be used as an
158            * input, output, or both, depending on the function.
159            */
160           uint8_t   dreq_immediate[8];
161 
162           /* Output buffer. */
163           dmover_buffer_type dreq_outbuf_type;
164           dmover_buffer dreq_outbuf;
165 
166           /* Input buffer. */
167           dmover_buffer_type dreq_inbuf_type;
168           dmover_buffer *dreq_inbuf;
169 };
170 
171 /* dreq_flags */
172 #define   DMOVER_REQ_DONE               0x0001    /* request is completed */
173 #define   DMOVER_REQ_ERROR    0x0002    /* error occurred */
174 #define   DMOVER_REQ_RUNNING  0x0004    /* request is being executed */
175 #define   DMOVER_REQ_WAIT               0x0008    /* wait for completion */
176 
177 #define   __DMOVER_REQ_INBUF_FREE       0x01000000 /* need to free input buffer */
178 
179 #define   __DMOVER_REQ_FLAGS_PRESERVE                                           \
180           (DMOVER_REQ_WAIT | __DMOVER_REQ_INBUF_FREE)
181 
182 /*
183  * dmover_backend:
184  *
185  *        Glue between the dmover-api middle layer and the dmover
186  *        backends.
187  *
188  *        All members of this structure are public.
189  */
190 struct dmover_backend {
191           TAILQ_ENTRY(dmover_backend) dmb_list;
192 
193           const char *dmb_name;                   /* name of back-end */
194           u_int dmb_speed;              /* est. KB/s throughput */
195 
196           void      *dmb_cookie;                  /* for back-end */
197 
198           /* List of algorithms this back-ends supports. */
199           const struct dmover_algdesc *dmb_algdescs;
200           int dmb_nalgdescs;
201 
202           /* Back-end functions. */
203           void      (*dmb_process)(struct dmover_backend *);
204 
205           /* List of sessions currently on this back-end. */
206           LIST_HEAD(, dmover_session) dmb_sessions;
207           int       dmb_nsessions;                /* current number of sessions */
208 
209           /* List of active requests on this back-end. */
210           TAILQ_HEAD(, dmover_request) dmb_pendreqs;
211           int       dmb_npendreqs;
212 };
213 
214 #define   dmover_backend_insque(dmb, dreq)                                      \
215 do {                                                                                      \
216           TAILQ_INSERT_TAIL(&(dmb)->dmb_pendreqs, (dreq), dreq_dmbq); \
217           (dmb)->dmb_npendreqs++;                                                         \
218 } while (/*CONSTCOND*/0)
219 
220 #define   dmover_backend_remque(dmb, dreq)                                      \
221 do {                                                                                      \
222           TAILQ_REMOVE(&(dmb)->dmb_pendreqs, (dreq), dreq_dmbq);                \
223           (dmb)->dmb_npendreqs--;                                                         \
224 } while (/*CONSTCOND*/0)
225 
226 /*
227  * Well-known data mover functions.  Using these for the function name
228  * saves space.
229  */
230 extern const char dmover_funcname_zero[];
231 #define   DMOVER_FUNC_ZERO              dmover_funcname_zero
232 
233 extern const char dmover_funcname_fill8[];
234 #define   DMOVER_FUNC_FILL8             dmover_funcname_fill8
235 
236 extern const char dmover_funcname_copy[];
237 #define   DMOVER_FUNC_COPY              dmover_funcname_copy
238 
239 extern const char dmover_funcname_iscsi_crc32c[];
240 #define   DMOVER_FUNC_ISCSI_CRC32C      dmover_funcname_iscsi_crc32c
241 
242 extern const char dmover_funcname_xor2[];
243 #define   DMOVER_FUNC_XOR2              dmover_funcname_xor2
244 
245 extern const char dmover_funcname_xor3[];
246 #define   DMOVER_FUNC_XOR3              dmover_funcname_xor3
247 
248 extern const char dmover_funcname_xor4[];
249 #define   DMOVER_FUNC_XOR4              dmover_funcname_xor4
250 
251 extern const char dmover_funcname_xor5[];
252 #define   DMOVER_FUNC_XOR5              dmover_funcname_xor5
253 
254 extern const char dmover_funcname_xor6[];
255 #define   DMOVER_FUNC_XOR6              dmover_funcname_xor6
256 
257 extern const char dmover_funcname_xor7[];
258 #define   DMOVER_FUNC_XOR7              dmover_funcname_xor7
259 
260 extern const char dmover_funcname_xor8[];
261 #define   DMOVER_FUNC_XOR8              dmover_funcname_xor8
262 
263 extern const char dmover_funcname_xor9[];
264 #define   DMOVER_FUNC_XOR9              dmover_funcname_xor9
265 
266 extern const char dmover_funcname_xor10[];
267 #define   DMOVER_FUNC_XOR10             dmover_funcname_xor10
268 
269 extern const char dmover_funcname_xor11[];
270 #define   DMOVER_FUNC_XOR11             dmover_funcname_xor11
271 
272 extern const char dmover_funcname_xor12[];
273 #define   DMOVER_FUNC_XOR12             dmover_funcname_xor12
274 
275 extern const char dmover_funcname_xor13[];
276 #define   DMOVER_FUNC_XOR13             dmover_funcname_xor13
277 
278 extern const char dmover_funcname_xor14[];
279 #define   DMOVER_FUNC_XOR14             dmover_funcname_xor14
280 
281 extern const char dmover_funcname_xor15[];
282 #define   DMOVER_FUNC_XOR15             dmover_funcname_xor15
283 
284 extern const char dmover_funcname_xor16[];
285 #define   DMOVER_FUNC_XOR16             dmover_funcname_xor16
286 
287 /* Back-end management functions. */
288 void      dmover_backend_register(struct dmover_backend *);
289 void      dmover_backend_unregister(struct dmover_backend *);
290 int       dmover_backend_alloc(struct dmover_session *, const char *);
291 void      dmover_backend_release(struct dmover_session *);
292 
293 /* Session management functions. */
294 void      dmover_session_initialize(void);
295 int       dmover_session_create(const char *, struct dmover_session **);
296 void      dmover_session_destroy(struct dmover_session *);
297 
298 /* Request management functions. */
299 void      dmover_request_initialize(void);
300 struct dmover_request *dmover_request_alloc(struct dmover_session *,
301               dmover_buffer *);
302 void      dmover_request_free(struct dmover_request *);
303 
304 /* Processing engine functions. */
305 void      dmover_process_initialize(void);
306 void      dmover_process(struct dmover_request *);
307 void      dmover_done(struct dmover_request *);
308 
309 /* Utility functions. */
310 const struct dmover_algdesc *
311               dmover_algdesc_lookup(const struct dmover_algdesc *, int,
312               const char *);
313 
314 #endif /* _DMOVER_DMOVERVAR_H_ */
315