blob: 65d13e38803f9dddc11079b9a00176e7b94116d2 [file] [log] [blame]
Peter Oberparleiter83b33702007-04-27 16:01:34 +02001/*
Sebastian Ottaa92b332012-10-12 10:22:25 +02002 * Copyright IBM Corp. 2007, 2012
Peter Oberparleiter83b33702007-04-27 16:01:34 +02003 * Author(s): Peter Oberparleiter <peter.oberparleiter@de.ibm.com>
4 */
5
Michael Ernst883e5122008-08-01 16:39:14 +02006#include <linux/vmalloc.h>
Sebastian Ottaa92b332012-10-12 10:22:25 +02007#include <linux/bitmap.h>
Jiri Slaby1977f032007-10-18 23:40:25 -07008#include <linux/bitops.h>
Peter Oberparleiter83b33702007-04-27 16:01:34 +02009#include "idset.h"
10#include "css.h"
11
12struct idset {
13 int num_ssid;
14 int num_id;
15 unsigned long bitmap[0];
16};
17
18static inline unsigned long bitmap_size(int num_ssid, int num_id)
19{
20 return __BITOPS_WORDS(num_ssid * num_id) * sizeof(unsigned long);
21}
22
23static struct idset *idset_new(int num_ssid, int num_id)
24{
25 struct idset *set;
26
Michael Ernst883e5122008-08-01 16:39:14 +020027 set = vmalloc(sizeof(struct idset) + bitmap_size(num_ssid, num_id));
Peter Oberparleiter83b33702007-04-27 16:01:34 +020028 if (set) {
29 set->num_ssid = num_ssid;
30 set->num_id = num_id;
Michael Ernst883e5122008-08-01 16:39:14 +020031 memset(set->bitmap, 0, bitmap_size(num_ssid, num_id));
Peter Oberparleiter83b33702007-04-27 16:01:34 +020032 }
33 return set;
34}
35
36void idset_free(struct idset *set)
37{
Michael Ernst883e5122008-08-01 16:39:14 +020038 vfree(set);
Peter Oberparleiter83b33702007-04-27 16:01:34 +020039}
40
41void idset_clear(struct idset *set)
42{
43 memset(set->bitmap, 0, bitmap_size(set->num_ssid, set->num_id));
44}
45
46void idset_fill(struct idset *set)
47{
48 memset(set->bitmap, 0xff, bitmap_size(set->num_ssid, set->num_id));
49}
50
51static inline void idset_add(struct idset *set, int ssid, int id)
52{
53 set_bit(ssid * set->num_id + id, set->bitmap);
54}
55
56static inline void idset_del(struct idset *set, int ssid, int id)
57{
58 clear_bit(ssid * set->num_id + id, set->bitmap);
59}
60
61static inline int idset_contains(struct idset *set, int ssid, int id)
62{
63 return test_bit(ssid * set->num_id + id, set->bitmap);
64}
65
66static inline int idset_get_first(struct idset *set, int *ssid, int *id)
67{
68 int bitnum;
69
70 bitnum = find_first_bit(set->bitmap, set->num_ssid * set->num_id);
71 if (bitnum >= set->num_ssid * set->num_id)
72 return 0;
73 *ssid = bitnum / set->num_id;
74 *id = bitnum % set->num_id;
75 return 1;
76}
77
78struct idset *idset_sch_new(void)
79{
Sebastian Ottb0a285d2009-09-22 22:58:37 +020080 return idset_new(max_ssid + 1, __MAX_SUBCHANNEL + 1);
Peter Oberparleiter83b33702007-04-27 16:01:34 +020081}
82
83void idset_sch_add(struct idset *set, struct subchannel_id schid)
84{
85 idset_add(set, schid.ssid, schid.sch_no);
86}
87
88void idset_sch_del(struct idset *set, struct subchannel_id schid)
89{
90 idset_del(set, schid.ssid, schid.sch_no);
91}
92
Sebastian Ottcec85462012-10-15 12:24:56 +020093/* Clear ids starting from @schid up to end of subchannel set. */
94void idset_sch_del_subseq(struct idset *set, struct subchannel_id schid)
95{
96 int pos = schid.ssid * set->num_id + schid.sch_no;
97
98 bitmap_clear(set->bitmap, pos, set->num_id - schid.sch_no);
99}
100
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200101int idset_sch_contains(struct idset *set, struct subchannel_id schid)
102{
103 return idset_contains(set, schid.ssid, schid.sch_no);
104}
105
106int idset_sch_get_first(struct idset *set, struct subchannel_id *schid)
107{
108 int ssid = 0;
109 int id = 0;
110 int rc;
111
112 rc = idset_get_first(set, &ssid, &id);
113 if (rc) {
114 init_subchannel_id(schid);
115 schid->ssid = ssid;
116 schid->sch_no = id;
117 }
118 return rc;
119}
Sebastian Ott25530552009-09-22 22:58:34 +0200120
121int idset_is_empty(struct idset *set)
122{
Sebastian Ottaa92b332012-10-12 10:22:25 +0200123 return bitmap_empty(set->bitmap, set->num_ssid * set->num_id);
Sebastian Ott25530552009-09-22 22:58:34 +0200124}
Sebastian Ott703e5c92009-09-22 22:58:38 +0200125
126void idset_add_set(struct idset *to, struct idset *from)
127{
Sebastian Ott7f0bc6c2012-11-06 15:10:05 +0100128 int len = min(to->num_ssid * to->num_id, from->num_ssid * from->num_id);
Sebastian Ott703e5c92009-09-22 22:58:38 +0200129
Sebastian Ottaa92b332012-10-12 10:22:25 +0200130 bitmap_or(to->bitmap, to->bitmap, from->bitmap, len);
Sebastian Ott703e5c92009-09-22 22:58:38 +0200131}