2 * UWB reservation management.
4 * Copyright (C) 2008 Cambridge Silicon Radio Ltd.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License version
8 * 2 as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 #include <linux/kernel.h>
19 #include <linux/uwb.h>
20 #include <linux/random.h>
22 #include "uwb-internal.h"
24 static void uwb_rsv_timer(unsigned long arg);
26 static const char *rsv_states[] = {
27 [UWB_RSV_STATE_NONE] = "none ",
28 [UWB_RSV_STATE_O_INITIATED] = "o initiated ",
29 [UWB_RSV_STATE_O_PENDING] = "o pending ",
30 [UWB_RSV_STATE_O_MODIFIED] = "o modified ",
31 [UWB_RSV_STATE_O_ESTABLISHED] = "o established ",
32 [UWB_RSV_STATE_O_TO_BE_MOVED] = "o to be moved ",
33 [UWB_RSV_STATE_O_MOVE_EXPANDING] = "o move expanding",
34 [UWB_RSV_STATE_O_MOVE_COMBINING] = "o move combining",
35 [UWB_RSV_STATE_O_MOVE_REDUCING] = "o move reducing ",
36 [UWB_RSV_STATE_T_ACCEPTED] = "t accepted ",
37 [UWB_RSV_STATE_T_CONFLICT] = "t conflict ",
38 [UWB_RSV_STATE_T_PENDING] = "t pending ",
39 [UWB_RSV_STATE_T_DENIED] = "t denied ",
40 [UWB_RSV_STATE_T_RESIZED] = "t resized ",
41 [UWB_RSV_STATE_T_EXPANDING_ACCEPTED] = "t expanding acc ",
42 [UWB_RSV_STATE_T_EXPANDING_CONFLICT] = "t expanding conf",
43 [UWB_RSV_STATE_T_EXPANDING_PENDING] = "t expanding pend",
44 [UWB_RSV_STATE_T_EXPANDING_DENIED] = "t expanding den ",
47 static const char *rsv_types[] = {
48 [UWB_DRP_TYPE_ALIEN_BP] = "alien-bp",
49 [UWB_DRP_TYPE_HARD] = "hard",
50 [UWB_DRP_TYPE_SOFT] = "soft",
51 [UWB_DRP_TYPE_PRIVATE] = "private",
52 [UWB_DRP_TYPE_PCA] = "pca",
55 bool uwb_rsv_has_two_drp_ies(struct uwb_rsv *rsv)
57 static const bool has_two_drp_ies[] = {
58 [UWB_RSV_STATE_O_INITIATED] = false,
59 [UWB_RSV_STATE_O_PENDING] = false,
60 [UWB_RSV_STATE_O_MODIFIED] = false,
61 [UWB_RSV_STATE_O_ESTABLISHED] = false,
62 [UWB_RSV_STATE_O_TO_BE_MOVED] = false,
63 [UWB_RSV_STATE_O_MOVE_COMBINING] = false,
64 [UWB_RSV_STATE_O_MOVE_REDUCING] = false,
65 [UWB_RSV_STATE_O_MOVE_EXPANDING] = true,
66 [UWB_RSV_STATE_T_ACCEPTED] = false,
67 [UWB_RSV_STATE_T_CONFLICT] = false,
68 [UWB_RSV_STATE_T_PENDING] = false,
69 [UWB_RSV_STATE_T_DENIED] = false,
70 [UWB_RSV_STATE_T_RESIZED] = false,
71 [UWB_RSV_STATE_T_EXPANDING_ACCEPTED] = true,
72 [UWB_RSV_STATE_T_EXPANDING_CONFLICT] = true,
73 [UWB_RSV_STATE_T_EXPANDING_PENDING] = true,
74 [UWB_RSV_STATE_T_EXPANDING_DENIED] = true,
77 return has_two_drp_ies[rsv->state];
81 * uwb_rsv_state_str - return a string for a reservation state
82 * @state: the reservation state.
84 const char *uwb_rsv_state_str(enum uwb_rsv_state state)
86 if (state < UWB_RSV_STATE_NONE || state >= UWB_RSV_STATE_LAST)
88 return rsv_states[state];
90 EXPORT_SYMBOL_GPL(uwb_rsv_state_str);
93 * uwb_rsv_type_str - return a string for a reservation type
94 * @type: the reservation type
96 const char *uwb_rsv_type_str(enum uwb_drp_type type)
98 if (type < UWB_DRP_TYPE_ALIEN_BP || type > UWB_DRP_TYPE_PCA)
100 return rsv_types[type];
102 EXPORT_SYMBOL_GPL(uwb_rsv_type_str);
104 void uwb_rsv_dump(char *text, struct uwb_rsv *rsv)
106 struct device *dev = &rsv->rc->uwb_dev.dev;
107 struct uwb_dev_addr devaddr;
108 char owner[UWB_ADDR_STRSIZE], target[UWB_ADDR_STRSIZE];
110 uwb_dev_addr_print(owner, sizeof(owner), &rsv->owner->dev_addr);
111 if (rsv->target.type == UWB_RSV_TARGET_DEV)
112 devaddr = rsv->target.dev->dev_addr;
114 devaddr = rsv->target.devaddr;
115 uwb_dev_addr_print(target, sizeof(target), &devaddr);
117 dev_dbg(dev, "rsv %s %s -> %s: %s\n",
118 text, owner, target, uwb_rsv_state_str(rsv->state));
121 static void uwb_rsv_release(struct kref *kref)
123 struct uwb_rsv *rsv = container_of(kref, struct uwb_rsv, kref);
128 void uwb_rsv_get(struct uwb_rsv *rsv)
130 kref_get(&rsv->kref);
133 void uwb_rsv_put(struct uwb_rsv *rsv)
135 kref_put(&rsv->kref, uwb_rsv_release);
139 * Get a free stream index for a reservation.
141 * If the target is a DevAddr (e.g., a WUSB cluster reservation) then
142 * the stream is allocated from a pool of per-RC stream indexes,
143 * otherwise a unique stream index for the target is selected.
145 static int uwb_rsv_get_stream(struct uwb_rsv *rsv)
147 struct uwb_rc *rc = rsv->rc;
148 struct device *dev = &rc->uwb_dev.dev;
149 unsigned long *streams_bm;
152 switch (rsv->target.type) {
153 case UWB_RSV_TARGET_DEV:
154 streams_bm = rsv->target.dev->streams;
156 case UWB_RSV_TARGET_DEVADDR:
157 streams_bm = rc->uwb_dev.streams;
163 stream = find_first_zero_bit(streams_bm, UWB_NUM_STREAMS);
164 if (stream >= UWB_NUM_STREAMS)
167 rsv->stream = stream;
168 set_bit(stream, streams_bm);
170 dev_dbg(dev, "get stream %d\n", rsv->stream);
175 static void uwb_rsv_put_stream(struct uwb_rsv *rsv)
177 struct uwb_rc *rc = rsv->rc;
178 struct device *dev = &rc->uwb_dev.dev;
179 unsigned long *streams_bm;
181 switch (rsv->target.type) {
182 case UWB_RSV_TARGET_DEV:
183 streams_bm = rsv->target.dev->streams;
185 case UWB_RSV_TARGET_DEVADDR:
186 streams_bm = rc->uwb_dev.streams;
192 clear_bit(rsv->stream, streams_bm);
194 dev_dbg(dev, "put stream %d\n", rsv->stream);
197 void uwb_rsv_backoff_win_timer(unsigned long arg)
199 struct uwb_drp_backoff_win *bow = (struct uwb_drp_backoff_win *)arg;
200 struct uwb_rc *rc = container_of(bow, struct uwb_rc, bow);
201 struct device *dev = &rc->uwb_dev.dev;
203 bow->can_reserve_extra_mases = true;
204 if (bow->total_expired <= 4) {
205 bow->total_expired++;
207 /* after 4 backoff window has expired we can exit from
208 * the backoff procedure */
209 bow->total_expired = 0;
210 bow->window = UWB_DRP_BACKOFF_WIN_MIN >> 1;
212 dev_dbg(dev, "backoff_win_timer total_expired=%d, n=%d\n: ", bow->total_expired, bow->n);
214 /* try to relocate all the "to be moved" relocations */
215 uwb_rsv_handle_drp_avail_change(rc);
218 void uwb_rsv_backoff_win_increment(struct uwb_rc *rc)
220 struct uwb_drp_backoff_win *bow = &rc->bow;
221 struct device *dev = &rc->uwb_dev.dev;
224 dev_dbg(dev, "backoff_win_increment: window=%d\n", bow->window);
226 bow->can_reserve_extra_mases = false;
228 if((bow->window << 1) == UWB_DRP_BACKOFF_WIN_MAX)
232 bow->n = random32() & (bow->window - 1);
233 dev_dbg(dev, "new_window=%d, n=%d\n: ", bow->window, bow->n);
235 /* reset the timer associated variables */
236 timeout_us = bow->n * UWB_SUPERFRAME_LENGTH_US;
237 bow->total_expired = 0;
238 mod_timer(&bow->timer, jiffies + usecs_to_jiffies(timeout_us));
241 static void uwb_rsv_stroke_timer(struct uwb_rsv *rsv)
243 int sframes = UWB_MAX_LOST_BEACONS;
246 * Multicast reservations can become established within 1
247 * super frame and should not be terminated if no response is
250 if (rsv->is_multicast) {
251 if (rsv->state == UWB_RSV_STATE_O_INITIATED
252 || rsv->state == UWB_RSV_STATE_O_MOVE_EXPANDING
253 || rsv->state == UWB_RSV_STATE_O_MOVE_COMBINING
254 || rsv->state == UWB_RSV_STATE_O_MOVE_REDUCING)
256 if (rsv->state == UWB_RSV_STATE_O_ESTABLISHED)
263 * Add an additional 2 superframes to account for the
264 * time to send the SET DRP IE command.
266 unsigned timeout_us = (sframes + 2) * UWB_SUPERFRAME_LENGTH_US;
267 mod_timer(&rsv->timer, jiffies + usecs_to_jiffies(timeout_us));
269 del_timer(&rsv->timer);
273 * Update a reservations state, and schedule an update of the
274 * transmitted DRP IEs.
276 static void uwb_rsv_state_update(struct uwb_rsv *rsv,
277 enum uwb_rsv_state new_state)
279 rsv->state = new_state;
280 rsv->ie_valid = false;
282 uwb_rsv_dump("SU", rsv);
284 uwb_rsv_stroke_timer(rsv);
285 uwb_rsv_sched_update(rsv->rc);
288 static void uwb_rsv_callback(struct uwb_rsv *rsv)
294 void uwb_rsv_set_state(struct uwb_rsv *rsv, enum uwb_rsv_state new_state)
296 struct uwb_rsv_move *mv = &rsv->mv;
298 if (rsv->state == new_state) {
299 switch (rsv->state) {
300 case UWB_RSV_STATE_O_ESTABLISHED:
301 case UWB_RSV_STATE_O_MOVE_EXPANDING:
302 case UWB_RSV_STATE_O_MOVE_COMBINING:
303 case UWB_RSV_STATE_O_MOVE_REDUCING:
304 case UWB_RSV_STATE_T_ACCEPTED:
305 case UWB_RSV_STATE_T_EXPANDING_ACCEPTED:
306 case UWB_RSV_STATE_T_RESIZED:
307 case UWB_RSV_STATE_NONE:
308 uwb_rsv_stroke_timer(rsv);
311 /* Expecting a state transition so leave timer
318 uwb_rsv_dump("SC", rsv);
321 case UWB_RSV_STATE_NONE:
322 uwb_rsv_state_update(rsv, UWB_RSV_STATE_NONE);
323 uwb_rsv_callback(rsv);
325 case UWB_RSV_STATE_O_INITIATED:
326 uwb_rsv_state_update(rsv, UWB_RSV_STATE_O_INITIATED);
328 case UWB_RSV_STATE_O_PENDING:
329 uwb_rsv_state_update(rsv, UWB_RSV_STATE_O_PENDING);
331 case UWB_RSV_STATE_O_MODIFIED:
332 /* in the companion there are the MASes to drop */
333 bitmap_andnot(rsv->mas.bm, rsv->mas.bm, mv->companion_mas.bm, UWB_NUM_MAS);
334 uwb_rsv_state_update(rsv, UWB_RSV_STATE_O_MODIFIED);
336 case UWB_RSV_STATE_O_ESTABLISHED:
337 if (rsv->state == UWB_RSV_STATE_O_MODIFIED
338 || rsv->state == UWB_RSV_STATE_O_MOVE_REDUCING) {
339 uwb_drp_avail_release(rsv->rc, &mv->companion_mas);
340 rsv->needs_release_companion_mas = false;
342 uwb_drp_avail_reserve(rsv->rc, &rsv->mas);
343 uwb_rsv_state_update(rsv, UWB_RSV_STATE_O_ESTABLISHED);
344 uwb_rsv_callback(rsv);
346 case UWB_RSV_STATE_O_MOVE_EXPANDING:
347 rsv->needs_release_companion_mas = true;
348 uwb_rsv_state_update(rsv, UWB_RSV_STATE_O_MOVE_EXPANDING);
350 case UWB_RSV_STATE_O_MOVE_COMBINING:
351 rsv->needs_release_companion_mas = false;
352 uwb_drp_avail_reserve(rsv->rc, &mv->companion_mas);
353 bitmap_or(rsv->mas.bm, rsv->mas.bm, mv->companion_mas.bm, UWB_NUM_MAS);
354 rsv->mas.safe += mv->companion_mas.safe;
355 rsv->mas.unsafe += mv->companion_mas.unsafe;
356 uwb_rsv_state_update(rsv, UWB_RSV_STATE_O_MOVE_COMBINING);
358 case UWB_RSV_STATE_O_MOVE_REDUCING:
359 bitmap_andnot(mv->companion_mas.bm, rsv->mas.bm, mv->final_mas.bm, UWB_NUM_MAS);
360 rsv->needs_release_companion_mas = true;
361 rsv->mas.safe = mv->final_mas.safe;
362 rsv->mas.unsafe = mv->final_mas.unsafe;
363 bitmap_copy(rsv->mas.bm, mv->final_mas.bm, UWB_NUM_MAS);
364 bitmap_copy(rsv->mas.unsafe_bm, mv->final_mas.unsafe_bm, UWB_NUM_MAS);
365 uwb_rsv_state_update(rsv, UWB_RSV_STATE_O_MOVE_REDUCING);
367 case UWB_RSV_STATE_T_ACCEPTED:
368 case UWB_RSV_STATE_T_RESIZED:
369 rsv->needs_release_companion_mas = false;
370 uwb_drp_avail_reserve(rsv->rc, &rsv->mas);
371 uwb_rsv_state_update(rsv, UWB_RSV_STATE_T_ACCEPTED);
372 uwb_rsv_callback(rsv);
374 case UWB_RSV_STATE_T_DENIED:
375 uwb_rsv_state_update(rsv, UWB_RSV_STATE_T_DENIED);
377 case UWB_RSV_STATE_T_CONFLICT:
378 uwb_rsv_state_update(rsv, UWB_RSV_STATE_T_CONFLICT);
380 case UWB_RSV_STATE_T_PENDING:
381 uwb_rsv_state_update(rsv, UWB_RSV_STATE_T_PENDING);
383 case UWB_RSV_STATE_T_EXPANDING_ACCEPTED:
384 rsv->needs_release_companion_mas = true;
385 uwb_drp_avail_reserve(rsv->rc, &mv->companion_mas);
386 uwb_rsv_state_update(rsv, UWB_RSV_STATE_T_EXPANDING_ACCEPTED);
389 dev_err(&rsv->rc->uwb_dev.dev, "unhandled state: %s (%d)\n",
390 uwb_rsv_state_str(new_state), new_state);
394 static void uwb_rsv_handle_timeout_work(struct work_struct *work)
396 struct uwb_rsv *rsv = container_of(work, struct uwb_rsv,
397 handle_timeout_work);
398 struct uwb_rc *rc = rsv->rc;
400 mutex_lock(&rc->rsvs_mutex);
402 uwb_rsv_dump("TO", rsv);
404 switch (rsv->state) {
405 case UWB_RSV_STATE_O_INITIATED:
406 if (rsv->is_multicast) {
407 uwb_rsv_set_state(rsv, UWB_RSV_STATE_O_ESTABLISHED);
411 case UWB_RSV_STATE_O_MOVE_EXPANDING:
412 if (rsv->is_multicast) {
413 uwb_rsv_set_state(rsv, UWB_RSV_STATE_O_MOVE_COMBINING);
417 case UWB_RSV_STATE_O_MOVE_COMBINING:
418 if (rsv->is_multicast) {
419 uwb_rsv_set_state(rsv, UWB_RSV_STATE_O_MOVE_REDUCING);
423 case UWB_RSV_STATE_O_MOVE_REDUCING:
424 if (rsv->is_multicast) {
425 uwb_rsv_set_state(rsv, UWB_RSV_STATE_O_ESTABLISHED);
429 case UWB_RSV_STATE_O_ESTABLISHED:
430 if (rsv->is_multicast)
433 case UWB_RSV_STATE_T_EXPANDING_ACCEPTED:
435 * The time out could be for the main or of the
436 * companion DRP, assume it's for the companion and
437 * drop that first. A further time out is required to
440 uwb_rsv_set_state(rsv, UWB_RSV_STATE_T_ACCEPTED);
441 uwb_drp_avail_release(rsv->rc, &rsv->mv.companion_mas);
450 mutex_unlock(&rc->rsvs_mutex);
453 static struct uwb_rsv *uwb_rsv_alloc(struct uwb_rc *rc)
457 rsv = kzalloc(sizeof(struct uwb_rsv), GFP_KERNEL);
461 INIT_LIST_HEAD(&rsv->rc_node);
462 INIT_LIST_HEAD(&rsv->pal_node);
463 kref_init(&rsv->kref);
464 init_timer(&rsv->timer);
465 rsv->timer.function = uwb_rsv_timer;
466 rsv->timer.data = (unsigned long)rsv;
469 INIT_WORK(&rsv->handle_timeout_work, uwb_rsv_handle_timeout_work);
475 * uwb_rsv_create - allocate and initialize a UWB reservation structure
476 * @rc: the radio controller
477 * @cb: callback to use when the reservation completes or terminates
478 * @pal_priv: data private to the PAL to be passed in the callback
480 * The callback is called when the state of the reservation changes from:
482 * - pending to accepted
483 * - pending to denined
484 * - accepted to terminated
485 * - pending to terminated
487 struct uwb_rsv *uwb_rsv_create(struct uwb_rc *rc, uwb_rsv_cb_f cb, void *pal_priv)
491 rsv = uwb_rsv_alloc(rc);
496 rsv->pal_priv = pal_priv;
500 EXPORT_SYMBOL_GPL(uwb_rsv_create);
502 void uwb_rsv_remove(struct uwb_rsv *rsv)
504 uwb_rsv_dump("RM", rsv);
506 if (rsv->state != UWB_RSV_STATE_NONE)
507 uwb_rsv_set_state(rsv, UWB_RSV_STATE_NONE);
509 if (rsv->needs_release_companion_mas)
510 uwb_drp_avail_release(rsv->rc, &rsv->mv.companion_mas);
511 uwb_drp_avail_release(rsv->rc, &rsv->mas);
513 if (uwb_rsv_is_owner(rsv))
514 uwb_rsv_put_stream(rsv);
516 uwb_dev_put(rsv->owner);
517 if (rsv->target.type == UWB_RSV_TARGET_DEV)
518 uwb_dev_put(rsv->target.dev);
520 list_del_init(&rsv->rc_node);
525 * uwb_rsv_destroy - free a UWB reservation structure
526 * @rsv: the reservation to free
528 * The reservation must already be terminated.
530 void uwb_rsv_destroy(struct uwb_rsv *rsv)
534 EXPORT_SYMBOL_GPL(uwb_rsv_destroy);
537 * usb_rsv_establish - start a reservation establishment
538 * @rsv: the reservation
540 * The PAL should fill in @rsv's owner, target, type, max_mas,
541 * min_mas, max_interval and is_multicast fields. If the target is a
542 * uwb_dev it must be referenced.
544 * The reservation's callback will be called when the reservation is
545 * accepted, denied or times out.
547 int uwb_rsv_establish(struct uwb_rsv *rsv)
549 struct uwb_rc *rc = rsv->rc;
550 struct uwb_mas_bm available;
553 mutex_lock(&rc->rsvs_mutex);
554 ret = uwb_rsv_get_stream(rsv);
558 rsv->tiebreaker = random32() & 1;
559 /* get available mas bitmap */
560 uwb_drp_available(rc, &available);
562 ret = uwb_rsv_find_best_allocation(rsv, &available, &rsv->mas);
563 if (ret == UWB_RSV_ALLOC_NOT_FOUND) {
565 uwb_rsv_put_stream(rsv);
569 ret = uwb_drp_avail_reserve_pending(rc, &rsv->mas);
571 uwb_rsv_put_stream(rsv);
576 list_add_tail(&rsv->rc_node, &rc->reservations);
577 rsv->owner = &rc->uwb_dev;
578 uwb_dev_get(rsv->owner);
579 uwb_rsv_set_state(rsv, UWB_RSV_STATE_O_INITIATED);
581 mutex_unlock(&rc->rsvs_mutex);
584 EXPORT_SYMBOL_GPL(uwb_rsv_establish);
587 * uwb_rsv_modify - modify an already established reservation
588 * @rsv: the reservation to modify
589 * @max_mas: new maximum MAS to reserve
590 * @min_mas: new minimum MAS to reserve
591 * @max_interval: new max_interval to use
593 * FIXME: implement this once there are PALs that use it.
595 int uwb_rsv_modify(struct uwb_rsv *rsv, int max_mas, int min_mas, int max_interval)
599 EXPORT_SYMBOL_GPL(uwb_rsv_modify);
602 * move an already established reservation (rc->rsvs_mutex must to be
603 * taken when tis function is called)
605 int uwb_rsv_try_move(struct uwb_rsv *rsv, struct uwb_mas_bm *available)
607 struct uwb_rc *rc = rsv->rc;
608 struct uwb_drp_backoff_win *bow = &rc->bow;
609 struct device *dev = &rc->uwb_dev.dev;
610 struct uwb_rsv_move *mv;
613 if (bow->can_reserve_extra_mases == false)
618 if (uwb_rsv_find_best_allocation(rsv, available, &mv->final_mas) == UWB_RSV_ALLOC_FOUND) {
620 if (!bitmap_equal(rsv->mas.bm, mv->final_mas.bm, UWB_NUM_MAS)) {
621 /* We want to move the reservation */
622 bitmap_andnot(mv->companion_mas.bm, mv->final_mas.bm, rsv->mas.bm, UWB_NUM_MAS);
623 uwb_drp_avail_reserve_pending(rc, &mv->companion_mas);
624 uwb_rsv_set_state(rsv, UWB_RSV_STATE_O_MOVE_EXPANDING);
627 dev_dbg(dev, "new allocation not found\n");
633 /* It will try to move every reservation in state O_ESTABLISHED giving
634 * to the MAS allocator algorithm an availability that is the real one
635 * plus the allocation already established from the reservation. */
636 void uwb_rsv_handle_drp_avail_change(struct uwb_rc *rc)
638 struct uwb_drp_backoff_win *bow = &rc->bow;
640 struct uwb_mas_bm mas;
642 if (bow->can_reserve_extra_mases == false)
645 list_for_each_entry(rsv, &rc->reservations, rc_node) {
646 if (rsv->state == UWB_RSV_STATE_O_ESTABLISHED ||
647 rsv->state == UWB_RSV_STATE_O_TO_BE_MOVED) {
648 uwb_drp_available(rc, &mas);
649 bitmap_or(mas.bm, mas.bm, rsv->mas.bm, UWB_NUM_MAS);
650 uwb_rsv_try_move(rsv, &mas);
657 * uwb_rsv_terminate - terminate an established reservation
658 * @rsv: the reservation to terminate
660 * A reservation is terminated by removing the DRP IE from the beacon,
661 * the other end will consider the reservation to be terminated when
662 * it does not see the DRP IE for at least mMaxLostBeacons.
664 * If applicable, the reference to the target uwb_dev will be released.
666 void uwb_rsv_terminate(struct uwb_rsv *rsv)
668 struct uwb_rc *rc = rsv->rc;
670 mutex_lock(&rc->rsvs_mutex);
672 if (rsv->state != UWB_RSV_STATE_NONE)
673 uwb_rsv_set_state(rsv, UWB_RSV_STATE_NONE);
675 mutex_unlock(&rc->rsvs_mutex);
677 EXPORT_SYMBOL_GPL(uwb_rsv_terminate);
680 * uwb_rsv_accept - accept a new reservation from a peer
681 * @rsv: the reservation
682 * @cb: call back for reservation changes
683 * @pal_priv: data to be passed in the above call back
685 * Reservation requests from peers are denied unless a PAL accepts it
686 * by calling this function.
688 * The PAL call uwb_rsv_destroy() for all accepted reservations before
689 * calling uwb_pal_unregister().
691 void uwb_rsv_accept(struct uwb_rsv *rsv, uwb_rsv_cb_f cb, void *pal_priv)
696 rsv->pal_priv = pal_priv;
697 rsv->state = UWB_RSV_STATE_T_ACCEPTED;
699 EXPORT_SYMBOL_GPL(uwb_rsv_accept);
702 * Is a received DRP IE for this reservation?
704 static bool uwb_rsv_match(struct uwb_rsv *rsv, struct uwb_dev *src,
705 struct uwb_ie_drp *drp_ie)
707 struct uwb_dev_addr *rsv_src;
710 stream = uwb_ie_drp_stream_index(drp_ie);
712 if (rsv->stream != stream)
715 switch (rsv->target.type) {
716 case UWB_RSV_TARGET_DEVADDR:
717 return rsv->stream == stream;
718 case UWB_RSV_TARGET_DEV:
719 if (uwb_ie_drp_owner(drp_ie))
720 rsv_src = &rsv->owner->dev_addr;
722 rsv_src = &rsv->target.dev->dev_addr;
723 return uwb_dev_addr_cmp(&src->dev_addr, rsv_src) == 0;
728 static struct uwb_rsv *uwb_rsv_new_target(struct uwb_rc *rc,
730 struct uwb_ie_drp *drp_ie)
734 enum uwb_rsv_state state;
736 rsv = uwb_rsv_alloc(rc);
742 uwb_dev_get(rsv->owner);
743 rsv->target.type = UWB_RSV_TARGET_DEV;
744 rsv->target.dev = &rc->uwb_dev;
745 uwb_dev_get(&rc->uwb_dev);
746 rsv->type = uwb_ie_drp_type(drp_ie);
747 rsv->stream = uwb_ie_drp_stream_index(drp_ie);
748 uwb_drp_ie_to_bm(&rsv->mas, drp_ie);
751 * See if any PALs are interested in this reservation. If not,
754 rsv->state = UWB_RSV_STATE_T_DENIED;
755 mutex_lock(&rc->uwb_dev.mutex);
756 list_for_each_entry(pal, &rc->pals, node) {
758 pal->new_rsv(pal, rsv);
759 if (rsv->state == UWB_RSV_STATE_T_ACCEPTED)
762 mutex_unlock(&rc->uwb_dev.mutex);
764 list_add_tail(&rsv->rc_node, &rc->reservations);
766 rsv->state = UWB_RSV_STATE_NONE;
768 /* FIXME: do something sensible here */
769 if (state == UWB_RSV_STATE_T_ACCEPTED
770 && uwb_drp_avail_reserve_pending(rc, &rsv->mas) == -EBUSY) {
771 /* FIXME: do something sensible here */
773 uwb_rsv_set_state(rsv, state);
780 * uwb_rsv_get_usable_mas - get the bitmap of the usable MAS of a reservations
781 * @rsv: the reservation.
782 * @mas: returns the available MAS.
784 * The usable MAS of a reservation may be less than the negotiated MAS
785 * if alien BPs are present.
787 void uwb_rsv_get_usable_mas(struct uwb_rsv *rsv, struct uwb_mas_bm *mas)
789 bitmap_zero(mas->bm, UWB_NUM_MAS);
790 bitmap_andnot(mas->bm, rsv->mas.bm, rsv->rc->cnflt_alien_bitmap.bm, UWB_NUM_MAS);
792 EXPORT_SYMBOL_GPL(uwb_rsv_get_usable_mas);
795 * uwb_rsv_find - find a reservation for a received DRP IE.
796 * @rc: the radio controller
797 * @src: source of the DRP IE
798 * @drp_ie: the DRP IE
800 * If the reservation cannot be found and the DRP IE is from a peer
801 * attempting to establish a new reservation, create a new reservation
802 * and add it to the list.
804 struct uwb_rsv *uwb_rsv_find(struct uwb_rc *rc, struct uwb_dev *src,
805 struct uwb_ie_drp *drp_ie)
809 list_for_each_entry(rsv, &rc->reservations, rc_node) {
810 if (uwb_rsv_match(rsv, src, drp_ie))
814 if (uwb_ie_drp_owner(drp_ie))
815 return uwb_rsv_new_target(rc, src, drp_ie);
821 * Go through all the reservations and check for timeouts and (if
822 * necessary) update their DRP IEs.
824 * FIXME: look at building the SET_DRP_IE command here rather than
825 * having to rescan the list in uwb_rc_send_all_drp_ie().
827 static bool uwb_rsv_update_all(struct uwb_rc *rc)
829 struct uwb_rsv *rsv, *t;
830 bool ie_updated = false;
832 list_for_each_entry_safe(rsv, t, &rc->reservations, rc_node) {
833 if (!rsv->ie_valid) {
834 uwb_drp_ie_update(rsv);
842 void uwb_rsv_queue_update(struct uwb_rc *rc)
844 unsigned long delay_us = UWB_MAS_LENGTH_US * UWB_MAS_PER_ZONE;
846 queue_delayed_work(rc->rsv_workq, &rc->rsv_update_work, usecs_to_jiffies(delay_us));
850 * uwb_rsv_sched_update - schedule an update of the DRP IEs
851 * @rc: the radio controller.
853 * To improve performance and ensure correctness with [ECMA-368] the
854 * number of SET-DRP-IE commands that are done are limited.
856 * DRP IEs update come from two sources: DRP events from the hardware
857 * which all occur at the beginning of the superframe ('syncronous'
858 * events) and reservation establishment/termination requests from
859 * PALs or timers ('asynchronous' events).
861 * A delayed work ensures that all the synchronous events result in
862 * one SET-DRP-IE command.
864 * Additional logic (the set_drp_ie_pending and rsv_updated_postponed
865 * flags) will prevent an asynchrous event starting a SET-DRP-IE
866 * command if one is currently awaiting a response.
868 * FIXME: this does leave a window where an asynchrous event can delay
869 * the SET-DRP-IE for a synchronous event by one superframe.
871 void uwb_rsv_sched_update(struct uwb_rc *rc)
873 spin_lock_bh(&rc->rsvs_lock);
874 if (!delayed_work_pending(&rc->rsv_update_work)) {
875 if (rc->set_drp_ie_pending > 0) {
876 rc->set_drp_ie_pending++;
879 uwb_rsv_queue_update(rc);
882 spin_unlock_bh(&rc->rsvs_lock);
886 * Update DRP IEs and, if necessary, the DRP Availability IE and send
887 * the updated IEs to the radio controller.
889 static void uwb_rsv_update_work(struct work_struct *work)
891 struct uwb_rc *rc = container_of(work, struct uwb_rc,
892 rsv_update_work.work);
895 mutex_lock(&rc->rsvs_mutex);
897 ie_updated = uwb_rsv_update_all(rc);
899 if (!rc->drp_avail.ie_valid) {
900 uwb_drp_avail_ie_update(rc);
904 if (ie_updated && (rc->set_drp_ie_pending == 0))
905 uwb_rc_send_all_drp_ie(rc);
907 mutex_unlock(&rc->rsvs_mutex);
910 static void uwb_rsv_alien_bp_work(struct work_struct *work)
912 struct uwb_rc *rc = container_of(work, struct uwb_rc,
913 rsv_alien_bp_work.work);
916 mutex_lock(&rc->rsvs_mutex);
918 list_for_each_entry(rsv, &rc->reservations, rc_node) {
919 if (rsv->type != UWB_DRP_TYPE_ALIEN_BP) {
924 mutex_unlock(&rc->rsvs_mutex);
927 static void uwb_rsv_timer(unsigned long arg)
929 struct uwb_rsv *rsv = (struct uwb_rsv *)arg;
931 queue_work(rsv->rc->rsv_workq, &rsv->handle_timeout_work);
935 * uwb_rsv_remove_all - remove all reservations
936 * @rc: the radio controller
938 * A DRP IE update is not done.
940 void uwb_rsv_remove_all(struct uwb_rc *rc)
942 struct uwb_rsv *rsv, *t;
944 mutex_lock(&rc->rsvs_mutex);
945 list_for_each_entry_safe(rsv, t, &rc->reservations, rc_node) {
946 if (rsv->state != UWB_RSV_STATE_NONE)
947 uwb_rsv_set_state(rsv, UWB_RSV_STATE_NONE);
948 del_timer_sync(&rsv->timer);
950 /* Cancel any postponed update. */
951 rc->set_drp_ie_pending = 0;
952 mutex_unlock(&rc->rsvs_mutex);
954 cancel_delayed_work_sync(&rc->rsv_update_work);
955 flush_workqueue(rc->rsv_workq);
957 mutex_lock(&rc->rsvs_mutex);
958 list_for_each_entry_safe(rsv, t, &rc->reservations, rc_node) {
961 mutex_unlock(&rc->rsvs_mutex);
964 void uwb_rsv_init(struct uwb_rc *rc)
966 INIT_LIST_HEAD(&rc->reservations);
967 INIT_LIST_HEAD(&rc->cnflt_alien_list);
968 mutex_init(&rc->rsvs_mutex);
969 spin_lock_init(&rc->rsvs_lock);
970 INIT_DELAYED_WORK(&rc->rsv_update_work, uwb_rsv_update_work);
971 INIT_DELAYED_WORK(&rc->rsv_alien_bp_work, uwb_rsv_alien_bp_work);
972 rc->bow.can_reserve_extra_mases = true;
973 rc->bow.total_expired = 0;
974 rc->bow.window = UWB_DRP_BACKOFF_WIN_MIN >> 1;
975 init_timer(&rc->bow.timer);
976 rc->bow.timer.function = uwb_rsv_backoff_win_timer;
977 rc->bow.timer.data = (unsigned long)&rc->bow;
979 bitmap_complement(rc->uwb_dev.streams, rc->uwb_dev.streams, UWB_NUM_STREAMS);
982 int uwb_rsv_setup(struct uwb_rc *rc)
986 snprintf(name, sizeof(name), "%s_rsvd", dev_name(&rc->uwb_dev.dev));
987 rc->rsv_workq = create_singlethread_workqueue(name);
988 if (rc->rsv_workq == NULL)
994 void uwb_rsv_cleanup(struct uwb_rc *rc)
996 uwb_rsv_remove_all(rc);
997 destroy_workqueue(rc->rsv_workq);