1/*
2 * Device driver for the SYMBIOS/LSILOGIC 53C8XX and 53C1010 family
3 * of PCI-SCSI IO processors.
4 *
5 * Copyright (C) 1999-2001  Gerard Roudier <groudier@free.fr>
6 *
7 * This driver is derived from the Linux sym53c8xx driver.
8 * Copyright (C) 1998-2000  Gerard Roudier
9 *
10 * The sym53c8xx driver is derived from the ncr53c8xx driver that had been
11 * a port of the FreeBSD ncr driver to Linux-1.2.13.
12 *
13 * The original ncr driver has been written for 386bsd and FreeBSD by
14 *         Wolfgang Stanglmeier        <wolf@cologne.de>
15 *         Stefan Esser                <se@mi.Uni-Koeln.de>
16 * Copyright (C) 1994  Wolfgang Stanglmeier
17 *
18 * Other major contributions:
19 *
20 * NVRAM detection and reading.
21 * Copyright (C) 1997 Richard Waltham <dormouse@farsrobt.demon.co.uk>
22 *
23 *-----------------------------------------------------------------------------
24 *
25 * This program is free software; you can redistribute it and/or modify
26 * it under the terms of the GNU General Public License as published by
27 * the Free Software Foundation; either version 2 of the License, or
28 * (at your option) any later version.
29 *
30 * This program is distributed in the hope that it will be useful,
31 * but WITHOUT ANY WARRANTY; without even the implied warranty of
32 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
33 * GNU General Public License for more details.
34 *
35 * You should have received a copy of the GNU General Public License
36 * along with this program; if not, write to the Free Software
37 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
38 */
39
40#ifndef SYM_MISC_H
41#define SYM_MISC_H
42
43/*
44 *  A la VMS/CAM-3 queue management.
45 */
46typedef struct sym_quehead {
47	struct sym_quehead *flink;	/* Forward  pointer */
48	struct sym_quehead *blink;	/* Backward pointer */
49} SYM_QUEHEAD;
50
51#define sym_que_init(ptr) do { \
52	(ptr)->flink = (ptr); (ptr)->blink = (ptr); \
53} while (0)
54
55static __inline struct sym_quehead *sym_que_first(struct sym_quehead *head)
56{
57	return (head->flink == head) ? 0 : head->flink;
58}
59
60static __inline struct sym_quehead *sym_que_last(struct sym_quehead *head)
61{
62	return (head->blink == head) ? 0 : head->blink;
63}
64
65static __inline void __sym_que_add(struct sym_quehead * new,
66	struct sym_quehead * blink,
67	struct sym_quehead * flink)
68{
69	flink->blink	= new;
70	new->flink	= flink;
71	new->blink	= blink;
72	blink->flink	= new;
73}
74
75static __inline void __sym_que_del(struct sym_quehead * blink,
76	struct sym_quehead * flink)
77{
78	flink->blink = blink;
79	blink->flink = flink;
80}
81
82static __inline int sym_que_empty(struct sym_quehead *head)
83{
84	return head->flink == head;
85}
86
87static __inline void sym_que_splice(struct sym_quehead *list,
88	struct sym_quehead *head)
89{
90	struct sym_quehead *first = list->flink;
91
92	if (first != list) {
93		struct sym_quehead *last = list->blink;
94		struct sym_quehead *at   = head->flink;
95
96		first->blink = head;
97		head->flink  = first;
98
99		last->flink = at;
100		at->blink   = last;
101	}
102}
103
104static __inline void sym_que_move(struct sym_quehead *orig,
105	struct sym_quehead *dest)
106{
107	struct sym_quehead *first, *last;
108
109	first = orig->flink;
110	if (first != orig) {
111		first->blink = dest;
112		dest->flink  = first;
113		last = orig->blink;
114		last->flink  = dest;
115		dest->blink  = last;
116		orig->flink  = orig;
117		orig->blink  = orig;
118	} else {
119		dest->flink  = dest;
120		dest->blink  = dest;
121	}
122}
123
124#define sym_que_entry(ptr, type, member) \
125	((type *)((char *)(ptr)-(unsigned int)(&((type *)0)->member)))
126
127
128#define sym_insque(new, pos)		__sym_que_add(new, pos, (pos)->flink)
129
130#define sym_remque(el)			__sym_que_del((el)->blink, (el)->flink)
131
132#define sym_insque_head(new, head)	__sym_que_add(new, head, (head)->flink)
133
134static __inline struct sym_quehead *sym_remque_head(struct sym_quehead *head)
135{
136	struct sym_quehead *elem = head->flink;
137
138	if (elem != head)
139		__sym_que_del(head, elem->flink);
140	else
141		elem = NULL;
142	return elem;
143}
144
145#define sym_insque_tail(new, head)	__sym_que_add(new, (head)->blink, head)
146
147static __inline struct sym_quehead *sym_remque_tail(struct sym_quehead *head)
148{
149	struct sym_quehead *elem = head->blink;
150
151	if (elem != head)
152		__sym_que_del(elem->blink, head);
153	else
154		elem = 0;
155	return elem;
156}
157
158/*
159 *  This one may be useful.
160 */
161#define FOR_EACH_QUEUED_ELEMENT(head, qp) \
162	for (qp = (head)->flink; qp != (head); qp = qp->flink)
163/*
164 *  FreeBSD does not offer our kind of queue in the CAM CCB.
165 *  So, we have to cast.
166 */
167#define sym_qptr(p)	((struct sym_quehead *) (p))
168
169/*
170 *  Simple bitmap operations.
171 */
172#define sym_set_bit(p, n)	(((u32 *)(p))[(n)>>5] |=  (1<<((n)&0x1f)))
173#define sym_clr_bit(p, n)	(((u32 *)(p))[(n)>>5] &= ~(1<<((n)&0x1f)))
174#define sym_is_bit(p, n)	(((u32 *)(p))[(n)>>5] &   (1<<((n)&0x1f)))
175
176/*
177 * The below round up/down macros are to be used with a constant
178 * as argument (sizeof(...) for example), for the compiler to
179 * optimize the whole thing.
180 */
181#define _U_(a,m)	(a)<=(1<<m)?m:
182
183/*
184 * Round up logarithm to base 2 of a 16 bit constant.
185 */
186#define _LGRU16_(a) \
187( \
188 _U_(a, 0)_U_(a, 1)_U_(a, 2)_U_(a, 3)_U_(a, 4)_U_(a, 5)_U_(a, 6)_U_(a, 7) \
189 _U_(a, 8)_U_(a, 9)_U_(a,10)_U_(a,11)_U_(a,12)_U_(a,13)_U_(a,14)_U_(a,15) \
190 16)
191
192#endif /* SYM_MISC_H */
193