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