drp-ie.c 9.5 KB

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