drp-ie.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /*
  2. * UWB DRP IE management.
  3. *
  4. * Copyright (C) 2005-2006 Intel Corporation
  5. * Copyright (C) 2008 Cambridge Silicon Radio Ltd.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License version
  9. * 2 as published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <linux/version.h>
  20. #include <linux/kernel.h>
  21. #include <linux/random.h>
  22. #include <linux/uwb.h>
  23. #include "uwb-internal.h"
  24. /*
  25. * Allocate a DRP IE.
  26. *
  27. * To save having to free/allocate a DRP IE when its MAS changes,
  28. * enough memory is allocated for the maxiumum number of DRP
  29. * allocation fields. This gives an overhead per reservation of up to
  30. * (UWB_NUM_ZONES - 1) * 4 = 60 octets.
  31. */
  32. static struct uwb_ie_drp *uwb_drp_ie_alloc(void)
  33. {
  34. struct uwb_ie_drp *drp_ie;
  35. unsigned tiebreaker;
  36. drp_ie = kzalloc(sizeof(struct uwb_ie_drp) +
  37. UWB_NUM_ZONES * sizeof(struct uwb_drp_alloc),
  38. GFP_KERNEL);
  39. if (drp_ie) {
  40. drp_ie->hdr.element_id = UWB_IE_DRP;
  41. get_random_bytes(&tiebreaker, sizeof(unsigned));
  42. uwb_ie_drp_set_tiebreaker(drp_ie, tiebreaker & 1);
  43. }
  44. return drp_ie;
  45. }
  46. /*
  47. * Fill a DRP IE's allocation fields from a MAS bitmap.
  48. */
  49. static void uwb_drp_ie_from_bm(struct uwb_ie_drp *drp_ie,
  50. struct uwb_mas_bm *mas)
  51. {
  52. int z, i, num_fields = 0, next = 0;
  53. struct uwb_drp_alloc *zones;
  54. __le16 current_bmp;
  55. DECLARE_BITMAP(tmp_bmp, UWB_NUM_MAS);
  56. DECLARE_BITMAP(tmp_mas_bm, UWB_MAS_PER_ZONE);
  57. zones = drp_ie->allocs;
  58. bitmap_copy(tmp_bmp, mas->bm, UWB_NUM_MAS);
  59. /* Determine unique MAS bitmaps in zones from bitmap. */
  60. for (z = 0; z < UWB_NUM_ZONES; z++) {
  61. bitmap_copy(tmp_mas_bm, tmp_bmp, UWB_MAS_PER_ZONE);
  62. if (bitmap_weight(tmp_mas_bm, UWB_MAS_PER_ZONE) > 0) {
  63. bool found = false;
  64. current_bmp = (__le16) *tmp_mas_bm;
  65. for (i = 0; i < next; i++) {
  66. if (current_bmp == zones[i].mas_bm) {
  67. zones[i].zone_bm |= 1 << z;
  68. found = true;
  69. break;
  70. }
  71. }
  72. if (!found) {
  73. num_fields++;
  74. zones[next].zone_bm = 1 << z;
  75. zones[next].mas_bm = current_bmp;
  76. next++;
  77. }
  78. }
  79. bitmap_shift_right(tmp_bmp, tmp_bmp, UWB_MAS_PER_ZONE, UWB_NUM_MAS);
  80. }
  81. /* Store in format ready for transmission (le16). */
  82. for (i = 0; i < num_fields; i++) {
  83. drp_ie->allocs[i].zone_bm = cpu_to_le16(zones[i].zone_bm);
  84. drp_ie->allocs[i].mas_bm = cpu_to_le16(zones[i].mas_bm);
  85. }
  86. drp_ie->hdr.length = sizeof(struct uwb_ie_drp) - sizeof(struct uwb_ie_hdr)
  87. + num_fields * sizeof(struct uwb_drp_alloc);
  88. }
  89. /**
  90. * uwb_drp_ie_update - update a reservation's DRP IE
  91. * @rsv: the reservation
  92. */
  93. int uwb_drp_ie_update(struct uwb_rsv *rsv)
  94. {
  95. struct device *dev = &rsv->rc->uwb_dev.dev;
  96. struct uwb_ie_drp *drp_ie;
  97. int reason_code, status;
  98. switch (rsv->state) {
  99. case UWB_RSV_STATE_NONE:
  100. kfree(rsv->drp_ie);
  101. rsv->drp_ie = NULL;
  102. return 0;
  103. case UWB_RSV_STATE_O_INITIATED:
  104. reason_code = UWB_DRP_REASON_ACCEPTED;
  105. status = 0;
  106. break;
  107. case UWB_RSV_STATE_O_PENDING:
  108. reason_code = UWB_DRP_REASON_ACCEPTED;
  109. status = 0;
  110. break;
  111. case UWB_RSV_STATE_O_MODIFIED:
  112. reason_code = UWB_DRP_REASON_MODIFIED;
  113. status = 1;
  114. break;
  115. case UWB_RSV_STATE_O_ESTABLISHED:
  116. reason_code = UWB_DRP_REASON_ACCEPTED;
  117. status = 1;
  118. break;
  119. case UWB_RSV_STATE_T_ACCEPTED:
  120. reason_code = UWB_DRP_REASON_ACCEPTED;
  121. status = 1;
  122. break;
  123. case UWB_RSV_STATE_T_DENIED:
  124. reason_code = UWB_DRP_REASON_DENIED;
  125. status = 0;
  126. break;
  127. default:
  128. dev_dbg(dev, "rsv with unhandled state (%d)\n", rsv->state);
  129. return -EINVAL;
  130. }
  131. if (rsv->drp_ie == NULL) {
  132. rsv->drp_ie = uwb_drp_ie_alloc();
  133. if (rsv->drp_ie == NULL)
  134. return -ENOMEM;
  135. }
  136. drp_ie = rsv->drp_ie;
  137. uwb_ie_drp_set_owner(drp_ie, uwb_rsv_is_owner(rsv));
  138. uwb_ie_drp_set_status(drp_ie, status);
  139. uwb_ie_drp_set_reason_code(drp_ie, reason_code);
  140. uwb_ie_drp_set_stream_index(drp_ie, rsv->stream);
  141. uwb_ie_drp_set_type(drp_ie, rsv->type);
  142. if (uwb_rsv_is_owner(rsv)) {
  143. switch (rsv->target.type) {
  144. case UWB_RSV_TARGET_DEV:
  145. drp_ie->dev_addr = rsv->target.dev->dev_addr;
  146. break;
  147. case UWB_RSV_TARGET_DEVADDR:
  148. drp_ie->dev_addr = rsv->target.devaddr;
  149. break;
  150. }
  151. } else
  152. drp_ie->dev_addr = rsv->owner->dev_addr;
  153. uwb_drp_ie_from_bm(drp_ie, &rsv->mas);
  154. rsv->ie_valid = true;
  155. return 0;
  156. }
  157. /*
  158. * Set MAS bits from given MAS bitmap in a single zone of large bitmap.
  159. *
  160. * We are given a zone id and the MAS bitmap of bits that need to be set in
  161. * this zone. Note that this zone may already have bits set and this only
  162. * adds settings - we cannot simply assign the MAS bitmap contents to the
  163. * zone contents. We iterate over the the bits (MAS) in the zone and set the
  164. * bits that are set in the given MAS bitmap.
  165. */
  166. static
  167. void uwb_drp_ie_single_zone_to_bm(struct uwb_mas_bm *bm, u8 zone, u16 mas_bm)
  168. {
  169. int mas;
  170. u16 mas_mask;
  171. for (mas = 0; mas < UWB_MAS_PER_ZONE; mas++) {
  172. mas_mask = 1 << mas;
  173. if (mas_bm & mas_mask)
  174. set_bit(zone * UWB_NUM_ZONES + mas, bm->bm);
  175. }
  176. }
  177. /**
  178. * uwb_drp_ie_zones_to_bm - convert DRP allocation fields to a bitmap
  179. * @mas: MAS bitmap that will be populated to correspond to the
  180. * allocation fields in the DRP IE
  181. * @drp_ie: the DRP IE that contains the allocation fields.
  182. *
  183. * The input format is an array of MAS allocation fields (16 bit Zone
  184. * bitmap, 16 bit MAS bitmap) as described in [ECMA-368] section
  185. * 16.8.6. The output is a full 256 bit MAS bitmap.
  186. *
  187. * We go over all the allocation fields, for each allocation field we
  188. * know which zones are impacted. We iterate over all the zones
  189. * impacted and call a function that will set the correct MAS bits in
  190. * each zone.
  191. */
  192. void uwb_drp_ie_to_bm(struct uwb_mas_bm *bm, const struct uwb_ie_drp *drp_ie)
  193. {
  194. int numallocs = (drp_ie->hdr.length - 4) / 4;
  195. const struct uwb_drp_alloc *alloc;
  196. int cnt;
  197. u16 zone_bm, mas_bm;
  198. u8 zone;
  199. u16 zone_mask;
  200. for (cnt = 0; cnt < numallocs; cnt++) {
  201. alloc = &drp_ie->allocs[cnt];
  202. zone_bm = le16_to_cpu(alloc->zone_bm);
  203. mas_bm = le16_to_cpu(alloc->mas_bm);
  204. for (zone = 0; zone < UWB_NUM_ZONES; zone++) {
  205. zone_mask = 1 << zone;
  206. if (zone_bm & zone_mask)
  207. uwb_drp_ie_single_zone_to_bm(bm, zone, mas_bm);
  208. }
  209. }
  210. }