2 * drivers/s390/cio/idset.c
4 * Copyright IBM Corp. 2007
5 * Author(s): Peter Oberparleiter <peter.oberparleiter@de.ibm.com>
8 #include <linux/slab.h>
9 #include <asm/bitops.h>
16 unsigned long bitmap[0];
19 static inline unsigned long bitmap_size(int num_ssid, int num_id)
21 return __BITOPS_WORDS(num_ssid * num_id) * sizeof(unsigned long);
24 static struct idset *idset_new(int num_ssid, int num_id)
28 set = kzalloc(sizeof(struct idset) + bitmap_size(num_ssid, num_id),
31 set->num_ssid = num_ssid;
37 void idset_free(struct idset *set)
42 void idset_clear(struct idset *set)
44 memset(set->bitmap, 0, bitmap_size(set->num_ssid, set->num_id));
47 void idset_fill(struct idset *set)
49 memset(set->bitmap, 0xff, bitmap_size(set->num_ssid, set->num_id));
52 static inline void idset_add(struct idset *set, int ssid, int id)
54 set_bit(ssid * set->num_id + id, set->bitmap);
57 static inline void idset_del(struct idset *set, int ssid, int id)
59 clear_bit(ssid * set->num_id + id, set->bitmap);
62 static inline int idset_contains(struct idset *set, int ssid, int id)
64 return test_bit(ssid * set->num_id + id, set->bitmap);
67 static inline int idset_get_first(struct idset *set, int *ssid, int *id)
71 bitnum = find_first_bit(set->bitmap, set->num_ssid * set->num_id);
72 if (bitnum >= set->num_ssid * set->num_id)
74 *ssid = bitnum / set->num_id;
75 *id = bitnum % set->num_id;
79 struct idset *idset_sch_new(void)
81 return idset_new(__MAX_SSID + 1, __MAX_SUBCHANNEL + 1);
84 void idset_sch_add(struct idset *set, struct subchannel_id schid)
86 idset_add(set, schid.ssid, schid.sch_no);
89 void idset_sch_del(struct idset *set, struct subchannel_id schid)
91 idset_del(set, schid.ssid, schid.sch_no);
94 int idset_sch_contains(struct idset *set, struct subchannel_id schid)
96 return idset_contains(set, schid.ssid, schid.sch_no);
99 int idset_sch_get_first(struct idset *set, struct subchannel_id *schid)
105 rc = idset_get_first(set, &ssid, &id);
107 init_subchannel_id(schid);