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