idset.c 2.7 KB

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