Fix a boot panic with the amd device. We inherited some busdma code from
[dragonfly.git] / sys / sys / buf2.h
1 /*
2  * Copyright (c) 1982, 1986, 1989, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  * (c) UNIX System Laboratories, Inc.
5  * All or some portions of this file are derived from material licensed
6  * to the University of California by American Telephone and Telegraph
7  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8  * the permission of UNIX System Laboratories, Inc.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *      This product includes software developed by the University of
21  *      California, Berkeley and its contributors.
22  * 4. Neither the name of the University nor the names of its contributors
23  *    may be used to endorse or promote products derived from this software
24  *    without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  *
38  *      @(#)buf.h       8.9 (Berkeley) 3/30/95
39  * $FreeBSD: src/sys/sys/buf.h,v 1.88.2.10 2003/01/25 19:02:23 dillon Exp $
40  * $DragonFly: src/sys/sys/buf2.h,v 1.7 2004/03/01 06:33:19 dillon Exp $
41  */
42
43 #ifndef _SYS_BUF2_H_
44 #define _SYS_BUF2_H_
45
46 #ifdef _KERNEL
47
48 #ifndef _SYS_GLOBALDATA_H_
49 #include <sys/globaldata.h>     /* curthread */
50 #endif
51
52 /*
53  * Initialize a lock.
54  */
55 #define BUF_LOCKINIT(bp) \
56         lockinit(&(bp)->b_lock, 0, buf_wmesg, 0, 0)
57
58 /*
59  *
60  * Get a lock sleeping non-interruptably until it becomes available.
61  */
62 static __inline int BUF_LOCK (struct buf *, int);
63 static __inline int
64 BUF_LOCK(struct buf *bp, int locktype)
65 {
66         lwkt_tokref ilock;
67         int s, ret;
68
69         s = splbio();
70         lwkt_gettoken(&ilock, &buftimetoken);
71         locktype |= LK_INTERLOCK;
72         bp->b_lock.lk_wmesg = buf_wmesg;
73         bp->b_lock.lk_prio = 0;         /* tsleep flags */
74         /* bp->b_lock.lk_timo = 0;   not necessary */
75         ret = lockmgr(&(bp)->b_lock, locktype, &ilock, curthread);
76         splx(s);
77         return ret;
78 }
79 /*
80  * Get a lock sleeping with specified interruptably and timeout.
81  */
82 static __inline int BUF_TIMELOCK (struct buf *, int, char *, int, int);
83 static __inline int
84 BUF_TIMELOCK(struct buf *bp, int locktype, char *wmesg, int catch, int timo)
85 {
86         lwkt_tokref ilock;
87         int s, ret;
88
89         s = splbio();
90         lwkt_gettoken(&ilock, &buftimetoken);
91         locktype |= LK_INTERLOCK | LK_TIMELOCK;
92         bp->b_lock.lk_wmesg = wmesg;
93         bp->b_lock.lk_prio = catch;     /* tsleep flags */
94         bp->b_lock.lk_timo = timo;
95         ret = lockmgr(&(bp)->b_lock, locktype, &ilock, curthread);
96         splx(s);
97         return ret;
98 }
99 /*
100  * Release a lock. Only the acquiring process may free the lock unless
101  * it has been handed off to biodone.
102  */
103 static __inline void BUF_UNLOCK (struct buf *);
104 static __inline void
105 BUF_UNLOCK(struct buf *bp)
106 {
107         int s;
108
109         s = splbio();
110         lockmgr(&(bp)->b_lock, LK_RELEASE, NULL, curthread);
111         splx(s);
112 }
113
114 /*
115  * Free a buffer lock.
116  */
117 #define BUF_LOCKFREE(bp)                        \
118         if (BUF_REFCNT(bp) > 0)                 \
119                 panic("free locked buf")
120 /*
121  * When initiating asynchronous I/O, change ownership of the lock to the
122  * kernel. Once done, the lock may legally released by biodone. The
123  * original owning process can no longer acquire it recursively, but must
124  * wait until the I/O is completed and the lock has been freed by biodone.
125  */
126 static __inline void BUF_KERNPROC (struct buf *);
127 static __inline void
128 BUF_KERNPROC(struct buf *bp)
129 {
130         struct thread *td = curthread;
131
132         if (bp->b_lock.lk_lockholder == td)
133                 td->td_locks--;
134         bp->b_lock.lk_lockholder = LK_KERNTHREAD;
135 }
136 /*
137  * Find out the number of references to a lock.
138  */
139 static __inline int BUF_REFCNT (struct buf *);
140 static __inline int
141 BUF_REFCNT(struct buf *bp)
142 {
143         int s, ret;
144
145         s = splbio();
146         ret = lockcount(&(bp)->b_lock);
147         splx(s);
148         return ret;
149 }
150
151 static __inline void
152 bufq_init(struct buf_queue_head *head)
153 {
154         TAILQ_INIT(&head->queue);
155         head->last_pblkno = 0;
156         head->insert_point = NULL;
157         head->switch_point = NULL;
158 }
159
160 static __inline void
161 bufq_insert_tail(struct buf_queue_head *head, struct buf *bp)
162 {
163         if ((bp->b_flags & B_ORDERED) != 0) {
164                 head->insert_point = bp;
165                 head->switch_point = NULL;
166         }
167         TAILQ_INSERT_TAIL(&head->queue, bp, b_act);
168 }
169
170 static __inline void
171 bufq_remove(struct buf_queue_head *head, struct buf *bp)
172 {
173         if (bp == head->switch_point)
174                 head->switch_point = TAILQ_NEXT(bp, b_act);
175         if (bp == head->insert_point) {
176                 head->insert_point = TAILQ_PREV(bp, buf_queue, b_act);
177                 if (head->insert_point == NULL)
178                         head->last_pblkno = 0;
179         } else if (bp == TAILQ_FIRST(&head->queue))
180                 head->last_pblkno = bp->b_pblkno;
181         TAILQ_REMOVE(&head->queue, bp, b_act);
182         if (TAILQ_FIRST(&head->queue) == head->switch_point)
183                 head->switch_point = NULL;
184 }
185
186 static __inline struct buf *
187 bufq_first(struct buf_queue_head *head)
188 {
189         return (TAILQ_FIRST(&head->queue));
190 }
191
192 #endif /* _KERNEL */
193
194 #endif /* !_SYS_BUF2_H_ */