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[];
17 };
18
idset_bitmap_size(int num_ssid,int num_id)19 static inline unsigned long idset_bitmap_size(int num_ssid, int num_id)
20 {
21 return bitmap_size(size_mul(num_ssid, num_id));
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) +
29 idset_bitmap_size(num_ssid, num_id));
30 if (set) {
31 set->num_ssid = num_ssid;
32 set->num_id = num_id;
33 memset(set->bitmap, 0, idset_bitmap_size(num_ssid, num_id));
34 }
35 return set;
36 }
37
idset_free(struct idset * set)38 void idset_free(struct idset *set)
39 {
40 vfree(set);
41 }
42
idset_fill(struct idset * set)43 void idset_fill(struct idset *set)
44 {
45 memset(set->bitmap, 0xff,
46 idset_bitmap_size(set->num_ssid, set->num_id));
47 }
48
idset_add(struct idset * set,int ssid,int id)49 static inline void idset_add(struct idset *set, int ssid, int id)
50 {
51 set_bit(ssid * set->num_id + id, set->bitmap);
52 }
53
idset_del(struct idset * set,int ssid,int id)54 static inline void idset_del(struct idset *set, int ssid, int id)
55 {
56 clear_bit(ssid * set->num_id + id, set->bitmap);
57 }
58
idset_contains(struct idset * set,int ssid,int id)59 static inline int idset_contains(struct idset *set, int ssid, int id)
60 {
61 return test_bit(ssid * set->num_id + id, set->bitmap);
62 }
63
idset_sch_new(void)64 struct idset *idset_sch_new(void)
65 {
66 return idset_new(max_ssid + 1, __MAX_SUBCHANNEL + 1);
67 }
68
idset_sch_add(struct idset * set,struct subchannel_id schid)69 void idset_sch_add(struct idset *set, struct subchannel_id schid)
70 {
71 idset_add(set, schid.ssid, schid.sch_no);
72 }
73
idset_sch_del(struct idset * set,struct subchannel_id schid)74 void idset_sch_del(struct idset *set, struct subchannel_id schid)
75 {
76 idset_del(set, schid.ssid, schid.sch_no);
77 }
78
79 /* Clear ids starting from @schid up to end of subchannel set. */
idset_sch_del_subseq(struct idset * set,struct subchannel_id schid)80 void idset_sch_del_subseq(struct idset *set, struct subchannel_id schid)
81 {
82 int pos = schid.ssid * set->num_id + schid.sch_no;
83
84 bitmap_clear(set->bitmap, pos, set->num_id - schid.sch_no);
85 }
86
idset_sch_contains(struct idset * set,struct subchannel_id schid)87 int idset_sch_contains(struct idset *set, struct subchannel_id schid)
88 {
89 return idset_contains(set, schid.ssid, schid.sch_no);
90 }
91
idset_is_empty(struct idset * set)92 int idset_is_empty(struct idset *set)
93 {
94 return bitmap_empty(set->bitmap, set->num_ssid * set->num_id);
95 }
96
idset_add_set(struct idset * to,struct idset * from)97 void idset_add_set(struct idset *to, struct idset *from)
98 {
99 int len = min(to->num_ssid * to->num_id, from->num_ssid * from->num_id);
100
101 bitmap_or(to->bitmap, to->bitmap, from->bitmap, len);
102 }
103