drp-avail.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /*
  2. * Ultra Wide Band
  3. * DRP availability management
  4. *
  5. * Copyright (C) 2005-2006 Intel Corporation
  6. * Reinette Chatre <reinette.chatre@intel.com>
  7. * Copyright (C) 2008 Cambridge Silicon Radio Ltd.
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License version
  11. * 2 as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. *
  22. * Manage DRP Availability (the MAS available for DRP
  23. * reservations). Thus:
  24. *
  25. * - Handle DRP Availability Change notifications
  26. *
  27. * - Allow the reservation manager to indicate MAS reserved/released
  28. * by local (owned by/targeted at the radio controller)
  29. * reservations.
  30. *
  31. * - Based on the two sources above, generate a DRP Availability IE to
  32. * be included in the beacon.
  33. *
  34. * See also the documentation for struct uwb_drp_avail.
  35. */
  36. #include <linux/errno.h>
  37. #include <linux/module.h>
  38. #include <linux/device.h>
  39. #include <linux/bitmap.h>
  40. #include "uwb-internal.h"
  41. /**
  42. * uwb_drp_avail_init - initialize an RC's MAS availability
  43. *
  44. * All MAS are available initially. The RC will inform use which
  45. * slots are used for the BP (it may change in size).
  46. */
  47. void uwb_drp_avail_init(struct uwb_rc *rc)
  48. {
  49. bitmap_fill(rc->drp_avail.global, UWB_NUM_MAS);
  50. bitmap_fill(rc->drp_avail.local, UWB_NUM_MAS);
  51. bitmap_fill(rc->drp_avail.pending, UWB_NUM_MAS);
  52. }
  53. /*
  54. * Determine MAS available for new local reservations.
  55. *
  56. * avail = global & local & pending
  57. */
  58. void uwb_drp_available(struct uwb_rc *rc, struct uwb_mas_bm *avail)
  59. {
  60. bitmap_and(avail->bm, rc->drp_avail.global, rc->drp_avail.local, UWB_NUM_MAS);
  61. bitmap_and(avail->bm, avail->bm, rc->drp_avail.pending, UWB_NUM_MAS);
  62. }
  63. /**
  64. * uwb_drp_avail_reserve_pending - reserve MAS for a new reservation
  65. * @rc: the radio controller
  66. * @mas: the MAS to reserve
  67. *
  68. * Returns 0 on success, or -EBUSY if the MAS requested aren't available.
  69. */
  70. int uwb_drp_avail_reserve_pending(struct uwb_rc *rc, struct uwb_mas_bm *mas)
  71. {
  72. struct uwb_mas_bm avail;
  73. uwb_drp_available(rc, &avail);
  74. if (!bitmap_subset(mas->bm, avail.bm, UWB_NUM_MAS))
  75. return -EBUSY;
  76. bitmap_andnot(rc->drp_avail.pending, rc->drp_avail.pending, mas->bm, UWB_NUM_MAS);
  77. return 0;
  78. }
  79. /**
  80. * uwb_drp_avail_reserve - reserve MAS for an established reservation
  81. * @rc: the radio controller
  82. * @mas: the MAS to reserve
  83. */
  84. void uwb_drp_avail_reserve(struct uwb_rc *rc, struct uwb_mas_bm *mas)
  85. {
  86. bitmap_or(rc->drp_avail.pending, rc->drp_avail.pending, mas->bm, UWB_NUM_MAS);
  87. bitmap_andnot(rc->drp_avail.local, rc->drp_avail.local, mas->bm, UWB_NUM_MAS);
  88. rc->drp_avail.ie_valid = false;
  89. }
  90. /**
  91. * uwb_drp_avail_release - release MAS from a pending or established reservation
  92. * @rc: the radio controller
  93. * @mas: the MAS to release
  94. */
  95. void uwb_drp_avail_release(struct uwb_rc *rc, struct uwb_mas_bm *mas)
  96. {
  97. bitmap_or(rc->drp_avail.local, rc->drp_avail.local, mas->bm, UWB_NUM_MAS);
  98. bitmap_or(rc->drp_avail.pending, rc->drp_avail.pending, mas->bm, UWB_NUM_MAS);
  99. rc->drp_avail.ie_valid = false;
  100. uwb_rsv_handle_drp_avail_change(rc);
  101. }
  102. /**
  103. * uwb_drp_avail_ie_update - update the DRP Availability IE
  104. * @rc: the radio controller
  105. *
  106. * avail = global & local
  107. */
  108. void uwb_drp_avail_ie_update(struct uwb_rc *rc)
  109. {
  110. struct uwb_mas_bm avail;
  111. bitmap_and(avail.bm, rc->drp_avail.global, rc->drp_avail.local, UWB_NUM_MAS);
  112. rc->drp_avail.ie.hdr.element_id = UWB_IE_DRP_AVAILABILITY;
  113. rc->drp_avail.ie.hdr.length = UWB_NUM_MAS / 8;
  114. uwb_mas_bm_copy_le(rc->drp_avail.ie.bmp, &avail);
  115. rc->drp_avail.ie_valid = true;
  116. }
  117. /**
  118. * Create an unsigned long from a buffer containing a byte stream.
  119. *
  120. * @array: pointer to buffer
  121. * @itr: index of buffer from where we start
  122. * @len: the buffer's remaining size may not be exact multiple of
  123. * sizeof(unsigned long), @len is the length of buffer that needs
  124. * to be converted. This will be sizeof(unsigned long) or smaller
  125. * (BUG if not). If it is smaller then we will pad the remaining
  126. * space of the result with zeroes.
  127. */
  128. static
  129. unsigned long get_val(u8 *array, size_t itr, size_t len)
  130. {
  131. unsigned long val = 0;
  132. size_t top = itr + len;
  133. BUG_ON(len > sizeof(val));
  134. while (itr < top) {
  135. val <<= 8;
  136. val |= array[top - 1];
  137. top--;
  138. }
  139. val <<= 8 * (sizeof(val) - len); /* padding */
  140. return val;
  141. }
  142. /**
  143. * Initialize bitmap from data buffer.
  144. *
  145. * The bitmap to be converted could come from a IE, for example a
  146. * DRP Availability IE.
  147. * From ECMA-368 1.0 [16.8.7]: "
  148. * octets: 1 1 N * (0 to 32)
  149. * Element ID Length (=N) DRP Availability Bitmap
  150. *
  151. * The DRP Availability Bitmap field is up to 256 bits long, one
  152. * bit for each MAS in the superframe, where the least-significant
  153. * bit of the field corresponds to the first MAS in the superframe
  154. * and successive bits correspond to successive MASs."
  155. *
  156. * The DRP Availability bitmap is in octets from 0 to 32, so octet
  157. * 32 contains bits for MAS 1-8, etc. If the bitmap is smaller than 32
  158. * octets, the bits in octets not included at the end of the bitmap are
  159. * treated as zero. In this case (when the bitmap is smaller than 32
  160. * octets) the MAS represented range from MAS 1 to MAS (size of bitmap)
  161. * with the last octet still containing bits for MAS 1-8, etc.
  162. *
  163. * For example:
  164. * F00F0102 03040506 0708090A 0B0C0D0E 0F010203
  165. * ^^^^
  166. * ||||
  167. * ||||
  168. * |||\LSB of byte is MAS 9
  169. * ||\MSB of byte is MAS 16
  170. * |\LSB of first byte is MAS 1
  171. * \ MSB of byte is MAS 8
  172. *
  173. * An example of this encoding can be found in ECMA-368 Annex-D [Table D.11]
  174. *
  175. * The resulting bitmap will have the following mapping:
  176. * bit position 0 == MAS 1
  177. * bit position 1 == MAS 2
  178. * ...
  179. * bit position (UWB_NUM_MAS - 1) == MAS UWB_NUM_MAS
  180. *
  181. * @bmp_itr: pointer to bitmap (can be declared with DECLARE_BITMAP)
  182. * @buffer: pointer to buffer containing bitmap data in big endian
  183. * format (MSB first)
  184. * @buffer_size:number of bytes with which bitmap should be initialized
  185. */
  186. static
  187. void buffer_to_bmp(unsigned long *bmp_itr, void *_buffer,
  188. size_t buffer_size)
  189. {
  190. u8 *buffer = _buffer;
  191. size_t itr, len;
  192. unsigned long val;
  193. itr = 0;
  194. while (itr < buffer_size) {
  195. len = buffer_size - itr >= sizeof(val) ?
  196. sizeof(val) : buffer_size - itr;
  197. val = get_val(buffer, itr, len);
  198. bmp_itr[itr / sizeof(val)] = val;
  199. itr += sizeof(val);
  200. }
  201. }
  202. /**
  203. * Extract DRP Availability bitmap from the notification.
  204. *
  205. * The notification that comes in contains a bitmap of (UWB_NUM_MAS / 8) bytes
  206. * We convert that to our internal representation.
  207. */
  208. static
  209. int uwbd_evt_get_drp_avail(struct uwb_event *evt, unsigned long *bmp)
  210. {
  211. struct device *dev = &evt->rc->uwb_dev.dev;
  212. struct uwb_rc_evt_drp_avail *drp_evt;
  213. int result = -EINVAL;
  214. /* Is there enough data to decode the event? */
  215. if (evt->notif.size < sizeof(*drp_evt)) {
  216. dev_err(dev, "DRP Availability Change: Not enough "
  217. "data to decode event [%zu bytes, %zu "
  218. "needed]\n", evt->notif.size, sizeof(*drp_evt));
  219. goto error;
  220. }
  221. drp_evt = container_of(evt->notif.rceb, struct uwb_rc_evt_drp_avail, rceb);
  222. buffer_to_bmp(bmp, drp_evt->bmp, UWB_NUM_MAS/8);
  223. result = 0;
  224. error:
  225. return result;
  226. }
  227. /**
  228. * Process an incoming DRP Availability notification.
  229. *
  230. * @evt: Event information (packs the actual event data, which
  231. * radio controller it came to, etc).
  232. *
  233. * @returns: 0 on success (so uwbd() frees the event buffer), < 0
  234. * on error.
  235. *
  236. * According to ECMA-368 1.0 [16.8.7], bits set to ONE indicate that
  237. * the MAS slot is available, bits set to ZERO indicate that the slot
  238. * is busy.
  239. *
  240. * So we clear available slots, we set used slots :)
  241. *
  242. * The notification only marks non-availability based on the BP and
  243. * received DRP IEs that are not for this radio controller. A copy of
  244. * this bitmap is needed to generate the real availability (which
  245. * includes local and pending reservations).
  246. *
  247. * The DRP Availability IE that this radio controller emits will need
  248. * to be updated.
  249. */
  250. int uwbd_evt_handle_rc_drp_avail(struct uwb_event *evt)
  251. {
  252. int result;
  253. struct uwb_rc *rc = evt->rc;
  254. DECLARE_BITMAP(bmp, UWB_NUM_MAS);
  255. result = uwbd_evt_get_drp_avail(evt, bmp);
  256. if (result < 0)
  257. return result;
  258. mutex_lock(&rc->rsvs_mutex);
  259. bitmap_copy(rc->drp_avail.global, bmp, UWB_NUM_MAS);
  260. rc->drp_avail.ie_valid = false;
  261. uwb_rsv_handle_drp_avail_change(rc);
  262. mutex_unlock(&rc->rsvs_mutex);
  263. uwb_rsv_sched_update(rc);
  264. return 0;
  265. }