1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  *    Copyright IBM Corp. 2007, 2012
4  *    Author(s): Peter Oberparleiter <peter.oberparleiter@de.ibm.com>
5  */
6 
7 #include <linux/vmalloc.h>
8 #include <linux/bitmap.h>
9 #include <linux/bitops.h>
10 #include "idset.h"
11 #include "css.h"
12 
13 struct idset {
14 	int num_ssid;
15 	int num_id;
16 	unsigned long bitmap[0];
17 };
18 
bitmap_size(int num_ssid,int num_id)19 static inline unsigned long bitmap_size(int num_ssid, int num_id)
20 {
21 	return BITS_TO_LONGS(num_ssid * num_id) * sizeof(unsigned long);
22 }
23 
idset_new(int num_ssid,int num_id)24 static struct idset *idset_new(int num_ssid, int num_id)
25 {
26 	struct idset *set;
27 
28 	set = vmalloc(sizeof(struct idset) + bitmap_size(num_ssid, num_id));
29 	if (set) {
30 		set->num_ssid = num_ssid;
31 		set->num_id = num_id;
32 		memset(set->bitmap, 0, bitmap_size(num_ssid, num_id));
33 	}
34 	return set;
35 }
36 
idset_free(struct idset * set)37 void idset_free(struct idset *set)
38 {
39 	vfree(set);
40 }
41 
idset_fill(struct idset * set)42 void idset_fill(struct idset *set)
43 {
44 	memset(set->bitmap, 0xff, bitmap_size(set->num_ssid, set->num_id));
45 }
46 
idset_add(struct idset * set,int ssid,int id)47 static inline void idset_add(struct idset *set, int ssid, int id)
48 {
49 	set_bit(ssid * set->num_id + id, set->bitmap);
50 }
51 
idset_del(struct idset * set,int ssid,int id)52 static inline void idset_del(struct idset *set, int ssid, int id)
53 {
54 	clear_bit(ssid * set->num_id + id, set->bitmap);
55 }
56 
idset_contains(struct idset * set,int ssid,int id)57 static inline int idset_contains(struct idset *set, int ssid, int id)
58 {
59 	return test_bit(ssid * set->num_id + id, set->bitmap);
60 }
61 
idset_get_first(struct idset * set,int * ssid,int * id)62 static inline int idset_get_first(struct idset *set, int *ssid, int *id)
63 {
64 	int bitnum;
65 
66 	bitnum = find_first_bit(set->bitmap, set->num_ssid * set->num_id);
67 	if (bitnum >= set->num_ssid * set->num_id)
68 		return 0;
69 	*ssid = bitnum / set->num_id;
70 	*id = bitnum % set->num_id;
71 	return 1;
72 }
73 
idset_sch_new(void)74 struct idset *idset_sch_new(void)
75 {
76 	return idset_new(max_ssid + 1, __MAX_SUBCHANNEL + 1);
77 }
78 
idset_sch_add(struct idset * set,struct subchannel_id schid)79 void idset_sch_add(struct idset *set, struct subchannel_id schid)
80 {
81 	idset_add(set, schid.ssid, schid.sch_no);
82 }
83 
idset_sch_del(struct idset * set,struct subchannel_id schid)84 void idset_sch_del(struct idset *set, struct subchannel_id schid)
85 {
86 	idset_del(set, schid.ssid, schid.sch_no);
87 }
88 
89 /* Clear ids starting from @schid up to end of subchannel set. */
idset_sch_del_subseq(struct idset * set,struct subchannel_id schid)90 void idset_sch_del_subseq(struct idset *set, struct subchannel_id schid)
91 {
92 	int pos = schid.ssid * set->num_id + schid.sch_no;
93 
94 	bitmap_clear(set->bitmap, pos, set->num_id - schid.sch_no);
95 }
96 
idset_sch_contains(struct idset * set,struct subchannel_id schid)97 int idset_sch_contains(struct idset *set, struct subchannel_id schid)
98 {
99 	return idset_contains(set, schid.ssid, schid.sch_no);
100 }
101 
idset_is_empty(struct idset * set)102 int idset_is_empty(struct idset *set)
103 {
104 	return bitmap_empty(set->bitmap, set->num_ssid * set->num_id);
105 }
106 
idset_add_set(struct idset * to,struct idset * from)107 void idset_add_set(struct idset *to, struct idset *from)
108 {
109 	int len = min(to->num_ssid * to->num_id, from->num_ssid * from->num_id);
110 
111 	bitmap_or(to->bitmap, to->bitmap, from->bitmap, len);
112 }
113