1/*	$NetBSD: dmovervar.h,v 1.9 2005/12/11 12:21:20 christos 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 */
47typedef enum {
48	DMOVER_BUF_NONE			= 0,
49	DMOVER_BUF_LINEAR		= 1,
50	DMOVER_BUF_UIO			= 2
51} dmover_buffer_type;
52
53typedef struct {
54	void *l_addr;
55	size_t l_len;
56} dmover_buf_linear;
57
58typedef 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 an dmover algorithm.
67 *
68 *	All members of this structure are public.
69 */
70struct 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 */
84struct 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 */
94struct 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)				\
118do {									\
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)				\
124do {									\
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 */
134struct 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 */
190struct 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)				\
215do {									\
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)				\
221do {									\
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 */
230extern const char dmover_funcname_zero[];
231#define	DMOVER_FUNC_ZERO		dmover_funcname_zero
232
233extern const char dmover_funcname_fill8[];
234#define	DMOVER_FUNC_FILL8		dmover_funcname_fill8
235
236extern const char dmover_funcname_copy[];
237#define	DMOVER_FUNC_COPY		dmover_funcname_copy
238
239extern const char dmover_funcname_iscsi_crc32c[];
240#define	DMOVER_FUNC_ISCSI_CRC32C	dmover_funcname_iscsi_crc32c
241
242extern const char dmover_funcname_xor2[];
243#define	DMOVER_FUNC_XOR2		dmover_funcname_xor2
244
245extern const char dmover_funcname_xor3[];
246#define	DMOVER_FUNC_XOR3		dmover_funcname_xor3
247
248extern const char dmover_funcname_xor4[];
249#define	DMOVER_FUNC_XOR4		dmover_funcname_xor4
250
251extern const char dmover_funcname_xor5[];
252#define	DMOVER_FUNC_XOR5		dmover_funcname_xor5
253
254extern const char dmover_funcname_xor6[];
255#define	DMOVER_FUNC_XOR6		dmover_funcname_xor6
256
257extern const char dmover_funcname_xor7[];
258#define	DMOVER_FUNC_XOR7		dmover_funcname_xor7
259
260extern const char dmover_funcname_xor8[];
261#define	DMOVER_FUNC_XOR8		dmover_funcname_xor8
262
263extern const char dmover_funcname_xor9[];
264#define	DMOVER_FUNC_XOR9		dmover_funcname_xor9
265
266extern const char dmover_funcname_xor10[];
267#define	DMOVER_FUNC_XOR10		dmover_funcname_xor10
268
269extern const char dmover_funcname_xor11[];
270#define	DMOVER_FUNC_XOR11		dmover_funcname_xor11
271
272extern const char dmover_funcname_xor12[];
273#define	DMOVER_FUNC_XOR12		dmover_funcname_xor12
274
275extern const char dmover_funcname_xor13[];
276#define	DMOVER_FUNC_XOR13		dmover_funcname_xor13
277
278extern const char dmover_funcname_xor14[];
279#define	DMOVER_FUNC_XOR14		dmover_funcname_xor14
280
281extern const char dmover_funcname_xor15[];
282#define	DMOVER_FUNC_XOR15		dmover_funcname_xor15
283
284extern const char dmover_funcname_xor16[];
285#define	DMOVER_FUNC_XOR16		dmover_funcname_xor16
286
287/* Back-end management functions. */
288void	dmover_backend_register(struct dmover_backend *);
289void	dmover_backend_unregister(struct dmover_backend *);
290int	dmover_backend_alloc(struct dmover_session *, const char *);
291void	dmover_backend_release(struct dmover_session *);
292
293/* Session management functions. */
294void	dmover_session_initialize(void);
295int	dmover_session_create(const char *, struct dmover_session **);
296void	dmover_session_destroy(struct dmover_session *);
297
298/* Request management functions. */
299void	dmover_request_initialize(void);
300struct dmover_request *dmover_request_alloc(struct dmover_session *,
301	    dmover_buffer *);
302void	dmover_request_free(struct dmover_request *);
303
304/* Processing engine functions. */
305void	dmover_process_initialize(void);
306void	dmover_process(struct dmover_request *);
307void	dmover_done(struct dmover_request *);
308
309/* Utility functions. */
310const struct dmover_algdesc *
311	    dmover_algdesc_lookup(const struct dmover_algdesc *, int,
312	    const char *);
313
314#endif /* _DMOVER_DMOVERVAR_H_ */
315