18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-or-later */
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Device driver for the SYMBIOS/LSILOGIC 53C8XX and 53C1010 family
48c2ecf20Sopenharmony_ci * of PCI-SCSI IO processors.
58c2ecf20Sopenharmony_ci *
68c2ecf20Sopenharmony_ci * Copyright (C) 1999-2001  Gerard Roudier <groudier@free.fr>
78c2ecf20Sopenharmony_ci *
88c2ecf20Sopenharmony_ci * This driver is derived from the Linux sym53c8xx driver.
98c2ecf20Sopenharmony_ci * Copyright (C) 1998-2000  Gerard Roudier
108c2ecf20Sopenharmony_ci *
118c2ecf20Sopenharmony_ci * The sym53c8xx driver is derived from the ncr53c8xx driver that had been
128c2ecf20Sopenharmony_ci * a port of the FreeBSD ncr driver to Linux-1.2.13.
138c2ecf20Sopenharmony_ci *
148c2ecf20Sopenharmony_ci * The original ncr driver has been written for 386bsd and FreeBSD by
158c2ecf20Sopenharmony_ci *         Wolfgang Stanglmeier        <wolf@cologne.de>
168c2ecf20Sopenharmony_ci *         Stefan Esser                <se@mi.Uni-Koeln.de>
178c2ecf20Sopenharmony_ci * Copyright (C) 1994  Wolfgang Stanglmeier
188c2ecf20Sopenharmony_ci *
198c2ecf20Sopenharmony_ci * Other major contributions:
208c2ecf20Sopenharmony_ci *
218c2ecf20Sopenharmony_ci * NVRAM detection and reading.
228c2ecf20Sopenharmony_ci * Copyright (C) 1997 Richard Waltham <dormouse@farsrobt.demon.co.uk>
238c2ecf20Sopenharmony_ci *
248c2ecf20Sopenharmony_ci *-----------------------------------------------------------------------------
258c2ecf20Sopenharmony_ci */
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_ci#ifndef SYM_MISC_H
288c2ecf20Sopenharmony_ci#define SYM_MISC_H
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ci/*
318c2ecf20Sopenharmony_ci *  A la VMS/CAM-3 queue management.
328c2ecf20Sopenharmony_ci */
338c2ecf20Sopenharmony_citypedef struct sym_quehead {
348c2ecf20Sopenharmony_ci	struct sym_quehead *flink;	/* Forward  pointer */
358c2ecf20Sopenharmony_ci	struct sym_quehead *blink;	/* Backward pointer */
368c2ecf20Sopenharmony_ci} SYM_QUEHEAD;
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_ci#define sym_que_init(ptr) do { \
398c2ecf20Sopenharmony_ci	(ptr)->flink = (ptr); (ptr)->blink = (ptr); \
408c2ecf20Sopenharmony_ci} while (0)
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_cistatic inline struct sym_quehead *sym_que_first(struct sym_quehead *head)
438c2ecf20Sopenharmony_ci{
448c2ecf20Sopenharmony_ci	return (head->flink == head) ? 0 : head->flink;
458c2ecf20Sopenharmony_ci}
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_cistatic inline struct sym_quehead *sym_que_last(struct sym_quehead *head)
488c2ecf20Sopenharmony_ci{
498c2ecf20Sopenharmony_ci	return (head->blink == head) ? 0 : head->blink;
508c2ecf20Sopenharmony_ci}
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_cistatic inline void __sym_que_add(struct sym_quehead * new,
538c2ecf20Sopenharmony_ci	struct sym_quehead * blink,
548c2ecf20Sopenharmony_ci	struct sym_quehead * flink)
558c2ecf20Sopenharmony_ci{
568c2ecf20Sopenharmony_ci	flink->blink	= new;
578c2ecf20Sopenharmony_ci	new->flink	= flink;
588c2ecf20Sopenharmony_ci	new->blink	= blink;
598c2ecf20Sopenharmony_ci	blink->flink	= new;
608c2ecf20Sopenharmony_ci}
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_cistatic inline void __sym_que_del(struct sym_quehead * blink,
638c2ecf20Sopenharmony_ci	struct sym_quehead * flink)
648c2ecf20Sopenharmony_ci{
658c2ecf20Sopenharmony_ci	flink->blink = blink;
668c2ecf20Sopenharmony_ci	blink->flink = flink;
678c2ecf20Sopenharmony_ci}
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_cistatic inline int sym_que_empty(struct sym_quehead *head)
708c2ecf20Sopenharmony_ci{
718c2ecf20Sopenharmony_ci	return head->flink == head;
728c2ecf20Sopenharmony_ci}
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_cistatic inline void sym_que_splice(struct sym_quehead *list,
758c2ecf20Sopenharmony_ci	struct sym_quehead *head)
768c2ecf20Sopenharmony_ci{
778c2ecf20Sopenharmony_ci	struct sym_quehead *first = list->flink;
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_ci	if (first != list) {
808c2ecf20Sopenharmony_ci		struct sym_quehead *last = list->blink;
818c2ecf20Sopenharmony_ci		struct sym_quehead *at   = head->flink;
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ci		first->blink = head;
848c2ecf20Sopenharmony_ci		head->flink  = first;
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_ci		last->flink = at;
878c2ecf20Sopenharmony_ci		at->blink   = last;
888c2ecf20Sopenharmony_ci	}
898c2ecf20Sopenharmony_ci}
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_cistatic inline void sym_que_move(struct sym_quehead *orig,
928c2ecf20Sopenharmony_ci	struct sym_quehead *dest)
938c2ecf20Sopenharmony_ci{
948c2ecf20Sopenharmony_ci	struct sym_quehead *first, *last;
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_ci	first = orig->flink;
978c2ecf20Sopenharmony_ci	if (first != orig) {
988c2ecf20Sopenharmony_ci		first->blink = dest;
998c2ecf20Sopenharmony_ci		dest->flink  = first;
1008c2ecf20Sopenharmony_ci		last = orig->blink;
1018c2ecf20Sopenharmony_ci		last->flink  = dest;
1028c2ecf20Sopenharmony_ci		dest->blink  = last;
1038c2ecf20Sopenharmony_ci		orig->flink  = orig;
1048c2ecf20Sopenharmony_ci		orig->blink  = orig;
1058c2ecf20Sopenharmony_ci	} else {
1068c2ecf20Sopenharmony_ci		dest->flink  = dest;
1078c2ecf20Sopenharmony_ci		dest->blink  = dest;
1088c2ecf20Sopenharmony_ci	}
1098c2ecf20Sopenharmony_ci}
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_ci#define sym_que_entry(ptr, type, member) container_of(ptr, type, member)
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_ci#define sym_insque(new, pos)		__sym_que_add(new, pos, (pos)->flink)
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_ci#define sym_remque(el)			__sym_que_del((el)->blink, (el)->flink)
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_ci#define sym_insque_head(new, head)	__sym_que_add(new, head, (head)->flink)
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_cistatic inline struct sym_quehead *sym_remque_head(struct sym_quehead *head)
1208c2ecf20Sopenharmony_ci{
1218c2ecf20Sopenharmony_ci	struct sym_quehead *elem = head->flink;
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_ci	if (elem != head)
1248c2ecf20Sopenharmony_ci		__sym_que_del(head, elem->flink);
1258c2ecf20Sopenharmony_ci	else
1268c2ecf20Sopenharmony_ci		elem = NULL;
1278c2ecf20Sopenharmony_ci	return elem;
1288c2ecf20Sopenharmony_ci}
1298c2ecf20Sopenharmony_ci
1308c2ecf20Sopenharmony_ci#define sym_insque_tail(new, head)	__sym_que_add(new, (head)->blink, head)
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_cistatic inline struct sym_quehead *sym_remque_tail(struct sym_quehead *head)
1338c2ecf20Sopenharmony_ci{
1348c2ecf20Sopenharmony_ci	struct sym_quehead *elem = head->blink;
1358c2ecf20Sopenharmony_ci
1368c2ecf20Sopenharmony_ci	if (elem != head)
1378c2ecf20Sopenharmony_ci		__sym_que_del(elem->blink, head);
1388c2ecf20Sopenharmony_ci	else
1398c2ecf20Sopenharmony_ci		elem = 0;
1408c2ecf20Sopenharmony_ci	return elem;
1418c2ecf20Sopenharmony_ci}
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_ci/*
1448c2ecf20Sopenharmony_ci *  This one may be useful.
1458c2ecf20Sopenharmony_ci */
1468c2ecf20Sopenharmony_ci#define FOR_EACH_QUEUED_ELEMENT(head, qp) \
1478c2ecf20Sopenharmony_ci	for (qp = (head)->flink; qp != (head); qp = qp->flink)
1488c2ecf20Sopenharmony_ci/*
1498c2ecf20Sopenharmony_ci *  FreeBSD does not offer our kind of queue in the CAM CCB.
1508c2ecf20Sopenharmony_ci *  So, we have to cast.
1518c2ecf20Sopenharmony_ci */
1528c2ecf20Sopenharmony_ci#define sym_qptr(p)	((struct sym_quehead *) (p))
1538c2ecf20Sopenharmony_ci
1548c2ecf20Sopenharmony_ci/*
1558c2ecf20Sopenharmony_ci *  Simple bitmap operations.
1568c2ecf20Sopenharmony_ci */
1578c2ecf20Sopenharmony_ci#define sym_set_bit(p, n)	(((u32 *)(p))[(n)>>5] |=  (1<<((n)&0x1f)))
1588c2ecf20Sopenharmony_ci#define sym_clr_bit(p, n)	(((u32 *)(p))[(n)>>5] &= ~(1<<((n)&0x1f)))
1598c2ecf20Sopenharmony_ci#define sym_is_bit(p, n)	(((u32 *)(p))[(n)>>5] &   (1<<((n)&0x1f)))
1608c2ecf20Sopenharmony_ci
1618c2ecf20Sopenharmony_ci/*
1628c2ecf20Sopenharmony_ci * The below round up/down macros are to be used with a constant
1638c2ecf20Sopenharmony_ci * as argument (sizeof(...) for example), for the compiler to
1648c2ecf20Sopenharmony_ci * optimize the whole thing.
1658c2ecf20Sopenharmony_ci */
1668c2ecf20Sopenharmony_ci#define _U_(a,m)	(a)<=(1<<m)?m:
1678c2ecf20Sopenharmony_ci
1688c2ecf20Sopenharmony_ci/*
1698c2ecf20Sopenharmony_ci * Round up logarithm to base 2 of a 16 bit constant.
1708c2ecf20Sopenharmony_ci */
1718c2ecf20Sopenharmony_ci#define _LGRU16_(a) \
1728c2ecf20Sopenharmony_ci( \
1738c2ecf20Sopenharmony_ci _U_(a, 0)_U_(a, 1)_U_(a, 2)_U_(a, 3)_U_(a, 4)_U_(a, 5)_U_(a, 6)_U_(a, 7) \
1748c2ecf20Sopenharmony_ci _U_(a, 8)_U_(a, 9)_U_(a,10)_U_(a,11)_U_(a,12)_U_(a,13)_U_(a,14)_U_(a,15) \
1758c2ecf20Sopenharmony_ci 16)
1768c2ecf20Sopenharmony_ci
1778c2ecf20Sopenharmony_ci#endif /* SYM_MISC_H */
178