18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * Copyright IBM Corp. 2007, 2012 48c2ecf20Sopenharmony_ci * Author(s): Peter Oberparleiter <peter.oberparleiter@de.ibm.com> 58c2ecf20Sopenharmony_ci */ 68c2ecf20Sopenharmony_ci 78c2ecf20Sopenharmony_ci#include <linux/vmalloc.h> 88c2ecf20Sopenharmony_ci#include <linux/bitmap.h> 98c2ecf20Sopenharmony_ci#include <linux/bitops.h> 108c2ecf20Sopenharmony_ci#include "idset.h" 118c2ecf20Sopenharmony_ci#include "css.h" 128c2ecf20Sopenharmony_ci 138c2ecf20Sopenharmony_cistruct idset { 148c2ecf20Sopenharmony_ci int num_ssid; 158c2ecf20Sopenharmony_ci int num_id; 168c2ecf20Sopenharmony_ci unsigned long bitmap[]; 178c2ecf20Sopenharmony_ci}; 188c2ecf20Sopenharmony_ci 198c2ecf20Sopenharmony_cistatic inline unsigned long bitmap_size(int num_ssid, int num_id) 208c2ecf20Sopenharmony_ci{ 218c2ecf20Sopenharmony_ci return BITS_TO_LONGS(num_ssid * num_id) * sizeof(unsigned long); 228c2ecf20Sopenharmony_ci} 238c2ecf20Sopenharmony_ci 248c2ecf20Sopenharmony_cistatic struct idset *idset_new(int num_ssid, int num_id) 258c2ecf20Sopenharmony_ci{ 268c2ecf20Sopenharmony_ci struct idset *set; 278c2ecf20Sopenharmony_ci 288c2ecf20Sopenharmony_ci set = vmalloc(sizeof(struct idset) + bitmap_size(num_ssid, num_id)); 298c2ecf20Sopenharmony_ci if (set) { 308c2ecf20Sopenharmony_ci set->num_ssid = num_ssid; 318c2ecf20Sopenharmony_ci set->num_id = num_id; 328c2ecf20Sopenharmony_ci memset(set->bitmap, 0, bitmap_size(num_ssid, num_id)); 338c2ecf20Sopenharmony_ci } 348c2ecf20Sopenharmony_ci return set; 358c2ecf20Sopenharmony_ci} 368c2ecf20Sopenharmony_ci 378c2ecf20Sopenharmony_civoid idset_free(struct idset *set) 388c2ecf20Sopenharmony_ci{ 398c2ecf20Sopenharmony_ci vfree(set); 408c2ecf20Sopenharmony_ci} 418c2ecf20Sopenharmony_ci 428c2ecf20Sopenharmony_civoid idset_fill(struct idset *set) 438c2ecf20Sopenharmony_ci{ 448c2ecf20Sopenharmony_ci memset(set->bitmap, 0xff, bitmap_size(set->num_ssid, set->num_id)); 458c2ecf20Sopenharmony_ci} 468c2ecf20Sopenharmony_ci 478c2ecf20Sopenharmony_cistatic inline void idset_add(struct idset *set, int ssid, int id) 488c2ecf20Sopenharmony_ci{ 498c2ecf20Sopenharmony_ci set_bit(ssid * set->num_id + id, set->bitmap); 508c2ecf20Sopenharmony_ci} 518c2ecf20Sopenharmony_ci 528c2ecf20Sopenharmony_cistatic inline void idset_del(struct idset *set, int ssid, int id) 538c2ecf20Sopenharmony_ci{ 548c2ecf20Sopenharmony_ci clear_bit(ssid * set->num_id + id, set->bitmap); 558c2ecf20Sopenharmony_ci} 568c2ecf20Sopenharmony_ci 578c2ecf20Sopenharmony_cistatic inline int idset_contains(struct idset *set, int ssid, int id) 588c2ecf20Sopenharmony_ci{ 598c2ecf20Sopenharmony_ci return test_bit(ssid * set->num_id + id, set->bitmap); 608c2ecf20Sopenharmony_ci} 618c2ecf20Sopenharmony_ci 628c2ecf20Sopenharmony_cistruct idset *idset_sch_new(void) 638c2ecf20Sopenharmony_ci{ 648c2ecf20Sopenharmony_ci return idset_new(max_ssid + 1, __MAX_SUBCHANNEL + 1); 658c2ecf20Sopenharmony_ci} 668c2ecf20Sopenharmony_ci 678c2ecf20Sopenharmony_civoid idset_sch_add(struct idset *set, struct subchannel_id schid) 688c2ecf20Sopenharmony_ci{ 698c2ecf20Sopenharmony_ci idset_add(set, schid.ssid, schid.sch_no); 708c2ecf20Sopenharmony_ci} 718c2ecf20Sopenharmony_ci 728c2ecf20Sopenharmony_civoid idset_sch_del(struct idset *set, struct subchannel_id schid) 738c2ecf20Sopenharmony_ci{ 748c2ecf20Sopenharmony_ci idset_del(set, schid.ssid, schid.sch_no); 758c2ecf20Sopenharmony_ci} 768c2ecf20Sopenharmony_ci 778c2ecf20Sopenharmony_ci/* Clear ids starting from @schid up to end of subchannel set. */ 788c2ecf20Sopenharmony_civoid idset_sch_del_subseq(struct idset *set, struct subchannel_id schid) 798c2ecf20Sopenharmony_ci{ 808c2ecf20Sopenharmony_ci int pos = schid.ssid * set->num_id + schid.sch_no; 818c2ecf20Sopenharmony_ci 828c2ecf20Sopenharmony_ci bitmap_clear(set->bitmap, pos, set->num_id - schid.sch_no); 838c2ecf20Sopenharmony_ci} 848c2ecf20Sopenharmony_ci 858c2ecf20Sopenharmony_ciint idset_sch_contains(struct idset *set, struct subchannel_id schid) 868c2ecf20Sopenharmony_ci{ 878c2ecf20Sopenharmony_ci return idset_contains(set, schid.ssid, schid.sch_no); 888c2ecf20Sopenharmony_ci} 898c2ecf20Sopenharmony_ci 908c2ecf20Sopenharmony_ciint idset_is_empty(struct idset *set) 918c2ecf20Sopenharmony_ci{ 928c2ecf20Sopenharmony_ci return bitmap_empty(set->bitmap, set->num_ssid * set->num_id); 938c2ecf20Sopenharmony_ci} 948c2ecf20Sopenharmony_ci 958c2ecf20Sopenharmony_civoid idset_add_set(struct idset *to, struct idset *from) 968c2ecf20Sopenharmony_ci{ 978c2ecf20Sopenharmony_ci int len = min(to->num_ssid * to->num_id, from->num_ssid * from->num_id); 988c2ecf20Sopenharmony_ci 998c2ecf20Sopenharmony_ci bitmap_or(to->bitmap, to->bitmap, from->bitmap, len); 1008c2ecf20Sopenharmony_ci} 101