* Copyright (c) 2010 Isilon Systems, Inc.
* Copyright (c) 2010 iX Systems, Inc.
* Copyright (c) 2010 Panasas, Inc.
- * Copyright (c) 2013, 2014 Mellanox Technologies, Ltd.
+ * Copyright (c) 2013-2015 Mellanox Technologies, Ltd.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
#ifndef _LINUX_BITOPS_H_
#define _LINUX_BITOPS_H_
+#include <sys/types.h>
+#include <sys/systm.h>
+
+#define BIT(nr) (1UL << (nr))
#ifdef __LP64__
#define BITS_PER_LONG 64
#else
#define BITS_PER_LONG 32
#endif
-#define BIT_MASK(n) (~0UL >> (BITS_PER_LONG - (n)))
+#define BITMAP_FIRST_WORD_MASK(start) (~0UL << ((start) % BITS_PER_LONG))
+#define BITMAP_LAST_WORD_MASK(n) (~0UL >> (BITS_PER_LONG - (n)))
#define BITS_TO_LONGS(n) howmany((n), BITS_PER_LONG)
+#define BIT_MASK(nr) (1UL << ((nr) & (BITS_PER_LONG - 1)))
#define BIT_WORD(nr) ((nr) / BITS_PER_LONG)
-
+#define GENMASK(lo, hi) (((2UL << ((hi) - (lo))) - 1UL) << (lo))
#define BITS_PER_BYTE 8
static inline int
return (bit + __ffsl(*addr));
}
if (size) {
- mask = (*addr) & BIT_MASK(size);
+ mask = (*addr) & BITMAP_LAST_WORD_MASK(size);
if (mask)
bit += __ffsl(mask);
else
return (bit + __ffsl(~(*addr)));
}
if (size) {
- mask = ~(*addr) & BIT_MASK(size);
+ mask = ~(*addr) & BITMAP_LAST_WORD_MASK(size);
if (mask)
bit += __ffsl(mask);
else
bit = BITS_PER_LONG * pos;
addr += pos;
if (offs) {
- mask = (*addr) & BIT_MASK(offs);
+ mask = (*addr) & BITMAP_LAST_WORD_MASK(offs);
if (mask)
return (bit + __flsl(mask));
}
bit = BITS_PER_LONG * pos;
addr += pos;
if (offs) {
- mask = (*addr) & ~BIT_MASK(offs);
+ mask = (*addr) & ~BITMAP_LAST_WORD_MASK(offs);
if (mask)
return (bit + __ffsl(mask));
if (size - bit <= BITS_PER_LONG)
return (bit + __ffsl(*addr));
}
if (size) {
- mask = (*addr) & BIT_MASK(size);
+ mask = (*addr) & BITMAP_LAST_WORD_MASK(size);
if (mask)
bit += __ffsl(mask);
else
bit = BITS_PER_LONG * pos;
addr += pos;
if (offs) {
- mask = ~(*addr) & ~BIT_MASK(offs);
+ mask = ~(*addr) & ~BITMAP_LAST_WORD_MASK(offs);
if (mask)
return (bit + __ffsl(mask));
if (size - bit <= BITS_PER_LONG)
return (bit + __ffsl(~(*addr)));
}
if (size) {
- mask = ~(*addr) & BIT_MASK(size);
+ mask = ~(*addr) & BITMAP_LAST_WORD_MASK(size);
if (mask)
bit += __ffsl(mask);
else
memset(addr, 0xff, len);
tail = size & (BITS_PER_LONG - 1);
if (tail)
- addr[size / BITS_PER_LONG] = BIT_MASK(tail);
+ addr[size / BITS_PER_LONG] = BITMAP_LAST_WORD_MASK(tail);
}
static inline int
bitmap_full(unsigned long *addr, int size)
{
- long mask;
+ unsigned long mask;
int tail;
int len;
int i;
return (0);
tail = size & (BITS_PER_LONG - 1);
if (tail) {
- mask = BIT_MASK(tail);
+ mask = BITMAP_LAST_WORD_MASK(tail);
if ((addr[i] & mask) != mask)
return (0);
}
static inline int
bitmap_empty(unsigned long *addr, int size)
{
- long mask;
+ unsigned long mask;
int tail;
int len;
int i;
return (0);
tail = size & (BITS_PER_LONG - 1);
if (tail) {
- mask = BIT_MASK(tail);
+ mask = BITMAP_LAST_WORD_MASK(tail);
if ((addr[i] & mask) != 0)
return (0);
}
return (1);
}
-#define NBLONG (NBBY * sizeof(long))
-
#define __set_bit(i, a) \
- atomic_set_long(&((volatile long *)(a))[(i)/NBLONG], 1UL << ((i) % NBLONG))
+ atomic_set_long(&((volatile long *)(a))[BIT_WORD(i)], BIT_MASK(i))
#define set_bit(i, a) \
- atomic_set_long(&((volatile long *)(a))[(i)/NBLONG], 1UL << ((i) % NBLONG))
+ atomic_set_long(&((volatile long *)(a))[BIT_WORD(i)], BIT_MASK(i))
#define __clear_bit(i, a) \
- atomic_clear_long(&((volatile long *)(a))[(i)/NBLONG], 1UL << ((i) % NBLONG))
+ atomic_clear_long(&((volatile long *)(a))[BIT_WORD(i)], BIT_MASK(i))
#define clear_bit(i, a) \
- atomic_clear_long(&((volatile long *)(a))[(i)/NBLONG], 1UL << ((i) % NBLONG))
+ atomic_clear_long(&((volatile long *)(a))[BIT_WORD(i)], BIT_MASK(i))
#define test_bit(i, a) \
- !!(atomic_load_acq_long(&((volatile long *)(a))[(i)/NBLONG]) & \
- (1UL << ((i) % NBLONG)))
+ !!(atomic_load_acq_long(&((volatile long *)(a))[BIT_WORD(i)]) & \
+ BIT_MASK(i))
static inline long
test_and_clear_bit(long bit, long *var)
{
long val;
- var += bit / (sizeof(long) * NBBY);
- bit %= sizeof(long) * NBBY;
+ var += BIT_WORD(bit);
+ bit %= BITS_PER_LONG;
bit = (1UL << bit);
do {
val = *(volatile long *)var;
{
long val;
- var += bit / (sizeof(long) * NBBY);
- bit %= sizeof(long) * NBBY;
+ var += BIT_WORD(bit);
+ bit %= BITS_PER_LONG;
bit = (1UL << bit);
do {
val = *(volatile long *)var;
return !!(val & bit);
}
-
-#define BITMAP_FIRST_WORD_MASK(start) (~0UL << ((start) % BITS_PER_LONG))
-#define BITMAP_LAST_WORD_MASK(nbits) \
-( \
- ((nbits) % BITS_PER_LONG) ? \
- (1UL<<((nbits) % BITS_PER_LONG))-1 : ~0UL \
-)
-
-
static inline void
bitmap_set(unsigned long *map, int start, int nr)
{
}
enum {
- REG_OP_ISFREE, /* true if region is all zero bits */
- REG_OP_ALLOC, /* set all bits in region */
- REG_OP_RELEASE, /* clear all bits in region */
+ REG_OP_ISFREE,
+ REG_OP_ALLOC,
+ REG_OP_RELEASE,
};
static int __reg_op(unsigned long *bitmap, int pos, int order, int reg_op)
{
- int nbits_reg; /* number of bits in region */
- int index; /* index first long of region in bitmap */
- int offset; /* bit offset region in bitmap[index] */
- int nlongs_reg; /* num longs spanned by region in bitmap */
- int nbitsinlong; /* num bits of region in each spanned long */
- unsigned long mask; /* bitmask for one long of region */
- int i; /* scans bitmap by longs */
- int ret = 0; /* return value */
-
- /*
- * Either nlongs_reg == 1 (for small orders that fit in one long)
- * or (offset == 0 && mask == ~0UL) (for larger multiword orders.)
- */
+ int nbits_reg;
+ int index;
+ int offset;
+ int nlongs_reg;
+ int nbitsinlong;
+ unsigned long mask;
+ int i;
+ int ret = 0;
+
nbits_reg = 1 << order;
index = pos / BITS_PER_LONG;
offset = pos - (index * BITS_PER_LONG);
nlongs_reg = BITS_TO_LONGS(nbits_reg);
nbitsinlong = min(nbits_reg, BITS_PER_LONG);
- /*
- * Can't do "mask = (1UL << nbitsinlong) - 1", as that
- * overflows if nbitsinlong == BITS_PER_LONG.
- */
mask = (1UL << (nbitsinlong - 1));
mask += mask - 1;
mask <<= offset;
if (bitmap[index + i] & mask)
goto done;
}
- ret = 1; /* all bits in region free (zero) */
+ ret = 1;
break;
case REG_OP_ALLOC:
return ret;
}
-/**
- * bitmap_find_free_region - find a contiguous aligned mem region
- * @bitmap: array of unsigned longs corresponding to the bitmap
- * @bits: number of bits in the bitmap
- * @order: region size (log base 2 of number of bits) to find
- *
- * Find a region of free (zero) bits in a @bitmap of @bits bits and
- * allocate them (set them to one). Only consider regions of length
- * a power (@order) of two, aligned to that power of two, which
- * makes the search algorithm much faster.
- *
- * Return the bit offset in bitmap of the allocated region,
- * or -errno on failure.
- */
static inline int
bitmap_find_free_region(unsigned long *bitmap, int bits, int order)
{
- int pos, end; /* scans bitmap by regions of size order */
+ int pos;
+ int end;
for (pos = 0 ; (end = pos + (1 << order)) <= bits; pos = end) {
if (!__reg_op(bitmap, pos, order, REG_OP_ISFREE))
return -ENOMEM;
}
-/**
- * bitmap_allocate_region - allocate bitmap region
- * @bitmap: array of unsigned longs corresponding to the bitmap
- * @pos: beginning of bit region to allocate
- * @order: region size (log base 2 of number of bits) to allocate
- *
- * Allocate (set bits in) a specified region of a bitmap.
- *
- * Return 0 on success, or %-EBUSY if specified region wasn't
- * free (not all bits were zero).
- */
-
static inline int
bitmap_allocate_region(unsigned long *bitmap, int pos, int order)
{
return 0;
}
-/**
- * bitmap_release_region - release allocated bitmap region
- * @bitmap: array of unsigned longs corresponding to the bitmap
- * @pos: beginning of bit region to release
- * @order: region size (log base 2 of number of bits) to release
- *
- * This is the complement to __bitmap_find_free_region() and releases
- * the found region (by clearing it in the bitmap).
- *
- * No return value.
- */
static inline void
bitmap_release_region(unsigned long *bitmap, int pos, int order)
{