ccbque.h revision 42118
1/*	$NetBSD$	*/
2/*
3 * [NetBSD for NEC PC98 series]
4 *  Copyright (c) 1994, 1995, 1996 NetBSD/pc98 porting staff.
5 *  All rights reserved.
6 *
7 *  Redistribution and use in source and binary forms, with or without
8 *  modification, are permitted provided that the following conditions
9 *  are met:
10 *  1. Redistributions of source code must retain the above copyright
11 *     notice, this list of conditions and the following disclaimer.
12 *  2. Redistributions in binary form must reproduce the above copyright
13 *     notice, this list of conditions and the following disclaimer in the
14 *     documentation and/or other materials provided with the distribution.
15 *  3. The name of the author may not be used to endorse or promote products
16 *     derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
22 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
29 */
30/*
31 * Common command control queue funcs.
32 * Written by N. Honda.
33 */
34
35#ifndef	_CCBQUE_H_
36#define	_CCBQUE_H_
37
38#define	CCB_MWANTED 0x01
39
40/* (I)  structure and prototype */
41#define GENERIC_CCB_ASSERT(DEV, CCBTYPE)				\
42TAILQ_HEAD(CCBTYPE##tab, CCBTYPE);					\
43struct CCBTYPE##que {							\
44	struct CCBTYPE##tab CCBTYPE##tab;				\
45	int count;							\
46	int maxccb;							\
47	u_int flags;							\
48};									\
49									\
50void DEV##_init_ccbque __P((int));					\
51struct CCBTYPE *DEV##_get_ccb __P((void));				\
52void DEV##_free_ccb __P((register struct CCBTYPE *));
53
54/* (II)  static allocated memory */
55#define GENERIC_CCB_STATIC_ALLOC(DEV, CCBTYPE)				\
56static struct CCBTYPE##que CCBTYPE##que;
57
58/* (III)  functions */
59#define GENERIC_CCB(DEV, CCBTYPE, CHAIN)				\
60									\
61void									\
62DEV##_init_ccbque(count)						\
63	int count;							\
64{									\
65	if (CCBTYPE##que.maxccb == 0)					\
66		TAILQ_INIT(&CCBTYPE##que.CCBTYPE##tab);			\
67	CCBTYPE##que.maxccb += count;					\
68}									\
69									\
70struct CCBTYPE *							\
71DEV##_get_ccb()								\
72{									\
73	register struct CCBTYPE *cb;					\
74	int s = splbio();						\
75									\
76again:									\
77	if (CCBTYPE##que.count < CCBTYPE##que.maxccb)			\
78	{								\
79		CCBTYPE##que.count ++;					\
80		cb = CCBTYPE##que.CCBTYPE##tab.tqh_first;		\
81		if (cb != NULL)						\
82		{							\
83			TAILQ_REMOVE(&CCBTYPE##que.CCBTYPE##tab, cb, CHAIN);\
84			goto out;					\
85		}							\
86		else							\
87		{							\
88			cb = malloc(sizeof(*cb), M_DEVBUF, M_NOWAIT);	\
89			if (cb != NULL)					\
90			{						\
91				bzero(cb, sizeof(*cb));			\
92				goto out;				\
93			}						\
94		}							\
95		CCBTYPE##que.count --;					\
96	}								\
97									\
98	cb = NULL;							\
99									\
100out:									\
101	splx(s);							\
102	return cb;							\
103}									\
104									\
105void									\
106DEV##_free_ccb(cb)							\
107	register struct CCBTYPE *cb;					\
108{									\
109	int s = splbio();						\
110									\
111	TAILQ_INSERT_TAIL(&CCBTYPE##que.CCBTYPE##tab, cb, CHAIN);	\
112	CCBTYPE##que.count --;						\
113									\
114	if (CCBTYPE##que.flags & CCB_MWANTED)				\
115	{								\
116		CCBTYPE##que.flags &= ~CCB_MWANTED;			\
117		wakeup ((caddr_t) &CCBTYPE##que.count);			\
118	}								\
119	splx(s);							\
120}
121#endif	/* !_CCBQUE_H_ */
122