drp-ie.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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/kernel.h>
  20. #include <linux/random.h>
  21. #include <linux/uwb.h>
  22. #include "uwb-internal.h"
  23. /*
  24. * Return the reason code for a reservations's DRP IE.
  25. */
  26. int uwb_rsv_reason_code(struct uwb_rsv *rsv)
  27. {
  28. static const int reason_codes[] = {
  29. [UWB_RSV_STATE_O_INITIATED] = UWB_DRP_REASON_ACCEPTED,
  30. [UWB_RSV_STATE_O_PENDING] = UWB_DRP_REASON_ACCEPTED,
  31. [UWB_RSV_STATE_O_MODIFIED] = UWB_DRP_REASON_MODIFIED,
  32. [UWB_RSV_STATE_O_ESTABLISHED] = UWB_DRP_REASON_ACCEPTED,
  33. [UWB_RSV_STATE_O_TO_BE_MOVED] = UWB_DRP_REASON_ACCEPTED,
  34. [UWB_RSV_STATE_O_MOVE_COMBINING] = UWB_DRP_REASON_MODIFIED,
  35. [UWB_RSV_STATE_O_MOVE_REDUCING] = UWB_DRP_REASON_MODIFIED,
  36. [UWB_RSV_STATE_O_MOVE_EXPANDING] = UWB_DRP_REASON_ACCEPTED,
  37. [UWB_RSV_STATE_T_ACCEPTED] = UWB_DRP_REASON_ACCEPTED,
  38. [UWB_RSV_STATE_T_CONFLICT] = UWB_DRP_REASON_CONFLICT,
  39. [UWB_RSV_STATE_T_PENDING] = UWB_DRP_REASON_PENDING,
  40. [UWB_RSV_STATE_T_DENIED] = UWB_DRP_REASON_DENIED,
  41. [UWB_RSV_STATE_T_RESIZED] = UWB_DRP_REASON_ACCEPTED,
  42. [UWB_RSV_STATE_T_EXPANDING_ACCEPTED] = UWB_DRP_REASON_ACCEPTED,
  43. [UWB_RSV_STATE_T_EXPANDING_CONFLICT] = UWB_DRP_REASON_CONFLICT,
  44. [UWB_RSV_STATE_T_EXPANDING_PENDING] = UWB_DRP_REASON_PENDING,
  45. [UWB_RSV_STATE_T_EXPANDING_DENIED] = UWB_DRP_REASON_DENIED,
  46. };
  47. return reason_codes[rsv->state];
  48. }
  49. /*
  50. * Return the reason code for a reservations's companion DRP IE .
  51. */
  52. int uwb_rsv_companion_reason_code(struct uwb_rsv *rsv)
  53. {
  54. static const int companion_reason_codes[] = {
  55. [UWB_RSV_STATE_O_MOVE_EXPANDING] = UWB_DRP_REASON_ACCEPTED,
  56. [UWB_RSV_STATE_T_EXPANDING_ACCEPTED] = UWB_DRP_REASON_ACCEPTED,
  57. [UWB_RSV_STATE_T_EXPANDING_CONFLICT] = UWB_DRP_REASON_CONFLICT,
  58. [UWB_RSV_STATE_T_EXPANDING_PENDING] = UWB_DRP_REASON_PENDING,
  59. [UWB_RSV_STATE_T_EXPANDING_DENIED] = UWB_DRP_REASON_DENIED,
  60. };
  61. return companion_reason_codes[rsv->state];
  62. }
  63. /*
  64. * Return the status bit for a reservations's DRP IE.
  65. */
  66. int uwb_rsv_status(struct uwb_rsv *rsv)
  67. {
  68. static const int statuses[] = {
  69. [UWB_RSV_STATE_O_INITIATED] = 0,
  70. [UWB_RSV_STATE_O_PENDING] = 0,
  71. [UWB_RSV_STATE_O_MODIFIED] = 1,
  72. [UWB_RSV_STATE_O_ESTABLISHED] = 1,
  73. [UWB_RSV_STATE_O_TO_BE_MOVED] = 0,
  74. [UWB_RSV_STATE_O_MOVE_COMBINING] = 1,
  75. [UWB_RSV_STATE_O_MOVE_REDUCING] = 1,
  76. [UWB_RSV_STATE_O_MOVE_EXPANDING] = 1,
  77. [UWB_RSV_STATE_T_ACCEPTED] = 1,
  78. [UWB_RSV_STATE_T_CONFLICT] = 0,
  79. [UWB_RSV_STATE_T_PENDING] = 0,
  80. [UWB_RSV_STATE_T_DENIED] = 0,
  81. [UWB_RSV_STATE_T_RESIZED] = 1,
  82. [UWB_RSV_STATE_T_EXPANDING_ACCEPTED] = 1,
  83. [UWB_RSV_STATE_T_EXPANDING_CONFLICT] = 1,
  84. [UWB_RSV_STATE_T_EXPANDING_PENDING] = 1,
  85. [UWB_RSV_STATE_T_EXPANDING_DENIED] = 1,
  86. };
  87. return statuses[rsv->state];
  88. }
  89. /*
  90. * Return the status bit for a reservations's companion DRP IE .
  91. */
  92. int uwb_rsv_companion_status(struct uwb_rsv *rsv)
  93. {
  94. static const int companion_statuses[] = {
  95. [UWB_RSV_STATE_O_MOVE_EXPANDING] = 0,
  96. [UWB_RSV_STATE_T_EXPANDING_ACCEPTED] = 1,
  97. [UWB_RSV_STATE_T_EXPANDING_CONFLICT] = 0,
  98. [UWB_RSV_STATE_T_EXPANDING_PENDING] = 0,
  99. [UWB_RSV_STATE_T_EXPANDING_DENIED] = 0,
  100. };
  101. return companion_statuses[rsv->state];
  102. }
  103. /*
  104. * Allocate a DRP IE.
  105. *
  106. * To save having to free/allocate a DRP IE when its MAS changes,
  107. * enough memory is allocated for the maxiumum number of DRP
  108. * allocation fields. This gives an overhead per reservation of up to
  109. * (UWB_NUM_ZONES - 1) * 4 = 60 octets.
  110. */
  111. static struct uwb_ie_drp *uwb_drp_ie_alloc(void)
  112. {
  113. struct uwb_ie_drp *drp_ie;
  114. drp_ie = kzalloc(sizeof(struct uwb_ie_drp) +
  115. UWB_NUM_ZONES * sizeof(struct uwb_drp_alloc),
  116. GFP_KERNEL);
  117. if (drp_ie) {
  118. drp_ie->hdr.element_id = UWB_IE_DRP;
  119. }
  120. return drp_ie;
  121. }
  122. /*
  123. * Fill a DRP IE's allocation fields from a MAS bitmap.
  124. */
  125. static void uwb_drp_ie_from_bm(struct uwb_ie_drp *drp_ie,
  126. struct uwb_mas_bm *mas)
  127. {
  128. int z, i, num_fields = 0, next = 0;
  129. struct uwb_drp_alloc *zones;
  130. __le16 current_bmp;
  131. DECLARE_BITMAP(tmp_bmp, UWB_NUM_MAS);
  132. DECLARE_BITMAP(tmp_mas_bm, UWB_MAS_PER_ZONE);
  133. zones = drp_ie->allocs;
  134. bitmap_copy(tmp_bmp, mas->bm, UWB_NUM_MAS);
  135. /* Determine unique MAS bitmaps in zones from bitmap. */
  136. for (z = 0; z < UWB_NUM_ZONES; z++) {
  137. bitmap_copy(tmp_mas_bm, tmp_bmp, UWB_MAS_PER_ZONE);
  138. if (bitmap_weight(tmp_mas_bm, UWB_MAS_PER_ZONE) > 0) {
  139. bool found = false;
  140. current_bmp = (__le16) *tmp_mas_bm;
  141. for (i = 0; i < next; i++) {
  142. if (current_bmp == zones[i].mas_bm) {
  143. zones[i].zone_bm |= 1 << z;
  144. found = true;
  145. break;
  146. }
  147. }
  148. if (!found) {
  149. num_fields++;
  150. zones[next].zone_bm = 1 << z;
  151. zones[next].mas_bm = current_bmp;
  152. next++;
  153. }
  154. }
  155. bitmap_shift_right(tmp_bmp, tmp_bmp, UWB_MAS_PER_ZONE, UWB_NUM_MAS);
  156. }
  157. /* Store in format ready for transmission (le16). */
  158. for (i = 0; i < num_fields; i++) {
  159. drp_ie->allocs[i].zone_bm = cpu_to_le16(zones[i].zone_bm);
  160. drp_ie->allocs[i].mas_bm = cpu_to_le16(zones[i].mas_bm);
  161. }
  162. drp_ie->hdr.length = sizeof(struct uwb_ie_drp) - sizeof(struct uwb_ie_hdr)
  163. + num_fields * sizeof(struct uwb_drp_alloc);
  164. }
  165. /**
  166. * uwb_drp_ie_update - update a reservation's DRP IE
  167. * @rsv: the reservation
  168. */
  169. int uwb_drp_ie_update(struct uwb_rsv *rsv)
  170. {
  171. struct uwb_ie_drp *drp_ie;
  172. struct uwb_rsv_move *mv;
  173. int unsafe;
  174. if (rsv->state == UWB_RSV_STATE_NONE) {
  175. kfree(rsv->drp_ie);
  176. rsv->drp_ie = NULL;
  177. return 0;
  178. }
  179. unsafe = rsv->mas.unsafe ? 1 : 0;
  180. if (rsv->drp_ie == NULL) {
  181. rsv->drp_ie = uwb_drp_ie_alloc();
  182. if (rsv->drp_ie == NULL)
  183. return -ENOMEM;
  184. }
  185. drp_ie = rsv->drp_ie;
  186. uwb_ie_drp_set_unsafe(drp_ie, unsafe);
  187. uwb_ie_drp_set_tiebreaker(drp_ie, rsv->tiebreaker);
  188. uwb_ie_drp_set_owner(drp_ie, uwb_rsv_is_owner(rsv));
  189. uwb_ie_drp_set_status(drp_ie, uwb_rsv_status(rsv));
  190. uwb_ie_drp_set_reason_code(drp_ie, uwb_rsv_reason_code(rsv));
  191. uwb_ie_drp_set_stream_index(drp_ie, rsv->stream);
  192. uwb_ie_drp_set_type(drp_ie, rsv->type);
  193. if (uwb_rsv_is_owner(rsv)) {
  194. switch (rsv->target.type) {
  195. case UWB_RSV_TARGET_DEV:
  196. drp_ie->dev_addr = rsv->target.dev->dev_addr;
  197. break;
  198. case UWB_RSV_TARGET_DEVADDR:
  199. drp_ie->dev_addr = rsv->target.devaddr;
  200. break;
  201. }
  202. } else
  203. drp_ie->dev_addr = rsv->owner->dev_addr;
  204. uwb_drp_ie_from_bm(drp_ie, &rsv->mas);
  205. if (uwb_rsv_has_two_drp_ies(rsv)) {
  206. mv = &rsv->mv;
  207. if (mv->companion_drp_ie == NULL) {
  208. mv->companion_drp_ie = uwb_drp_ie_alloc();
  209. if (mv->companion_drp_ie == NULL)
  210. return -ENOMEM;
  211. }
  212. drp_ie = mv->companion_drp_ie;
  213. /* keep all the same configuration of the main drp_ie */
  214. memcpy(drp_ie, rsv->drp_ie, sizeof(struct uwb_ie_drp));
  215. /* FIXME: handle properly the unsafe bit */
  216. uwb_ie_drp_set_unsafe(drp_ie, 1);
  217. uwb_ie_drp_set_status(drp_ie, uwb_rsv_companion_status(rsv));
  218. uwb_ie_drp_set_reason_code(drp_ie, uwb_rsv_companion_reason_code(rsv));
  219. uwb_drp_ie_from_bm(drp_ie, &mv->companion_mas);
  220. }
  221. rsv->ie_valid = true;
  222. return 0;
  223. }
  224. /*
  225. * Set MAS bits from given MAS bitmap in a single zone of large bitmap.
  226. *
  227. * We are given a zone id and the MAS bitmap of bits that need to be set in
  228. * this zone. Note that this zone may already have bits set and this only
  229. * adds settings - we cannot simply assign the MAS bitmap contents to the
  230. * zone contents. We iterate over the the bits (MAS) in the zone and set the
  231. * bits that are set in the given MAS bitmap.
  232. */
  233. static
  234. void uwb_drp_ie_single_zone_to_bm(struct uwb_mas_bm *bm, u8 zone, u16 mas_bm)
  235. {
  236. int mas;
  237. u16 mas_mask;
  238. for (mas = 0; mas < UWB_MAS_PER_ZONE; mas++) {
  239. mas_mask = 1 << mas;
  240. if (mas_bm & mas_mask)
  241. set_bit(zone * UWB_NUM_ZONES + mas, bm->bm);
  242. }
  243. }
  244. /**
  245. * uwb_drp_ie_zones_to_bm - convert DRP allocation fields to a bitmap
  246. * @mas: MAS bitmap that will be populated to correspond to the
  247. * allocation fields in the DRP IE
  248. * @drp_ie: the DRP IE that contains the allocation fields.
  249. *
  250. * The input format is an array of MAS allocation fields (16 bit Zone
  251. * bitmap, 16 bit MAS bitmap) as described in [ECMA-368] section
  252. * 16.8.6. The output is a full 256 bit MAS bitmap.
  253. *
  254. * We go over all the allocation fields, for each allocation field we
  255. * know which zones are impacted. We iterate over all the zones
  256. * impacted and call a function that will set the correct MAS bits in
  257. * each zone.
  258. */
  259. void uwb_drp_ie_to_bm(struct uwb_mas_bm *bm, const struct uwb_ie_drp *drp_ie)
  260. {
  261. int numallocs = (drp_ie->hdr.length - 4) / 4;
  262. const struct uwb_drp_alloc *alloc;
  263. int cnt;
  264. u16 zone_bm, mas_bm;
  265. u8 zone;
  266. u16 zone_mask;
  267. bitmap_zero(bm->bm, UWB_NUM_MAS);
  268. for (cnt = 0; cnt < numallocs; cnt++) {
  269. alloc = &drp_ie->allocs[cnt];
  270. zone_bm = le16_to_cpu(alloc->zone_bm);
  271. mas_bm = le16_to_cpu(alloc->mas_bm);
  272. for (zone = 0; zone < UWB_NUM_ZONES; zone++) {
  273. zone_mask = 1 << zone;
  274. if (zone_bm & zone_mask)
  275. uwb_drp_ie_single_zone_to_bm(bm, zone, mas_bm);
  276. }
  277. }
  278. }