Linux 4.2.1
[linux/fpc-iii.git] / drivers / uwb / rsv.c
blob536ad42b0a4b1d7ed0091e4e943a19abf9d0980d
1 /*
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/slab.h>
21 #include <linux/random.h>
22 #include <linux/export.h>
24 #include "uwb-internal.h"
26 static void uwb_rsv_timer(unsigned long arg);
28 static const char *rsv_states[] = {
29 [UWB_RSV_STATE_NONE] = "none ",
30 [UWB_RSV_STATE_O_INITIATED] = "o initiated ",
31 [UWB_RSV_STATE_O_PENDING] = "o pending ",
32 [UWB_RSV_STATE_O_MODIFIED] = "o modified ",
33 [UWB_RSV_STATE_O_ESTABLISHED] = "o established ",
34 [UWB_RSV_STATE_O_TO_BE_MOVED] = "o to be moved ",
35 [UWB_RSV_STATE_O_MOVE_EXPANDING] = "o move expanding",
36 [UWB_RSV_STATE_O_MOVE_COMBINING] = "o move combining",
37 [UWB_RSV_STATE_O_MOVE_REDUCING] = "o move reducing ",
38 [UWB_RSV_STATE_T_ACCEPTED] = "t accepted ",
39 [UWB_RSV_STATE_T_CONFLICT] = "t conflict ",
40 [UWB_RSV_STATE_T_PENDING] = "t pending ",
41 [UWB_RSV_STATE_T_DENIED] = "t denied ",
42 [UWB_RSV_STATE_T_RESIZED] = "t resized ",
43 [UWB_RSV_STATE_T_EXPANDING_ACCEPTED] = "t expanding acc ",
44 [UWB_RSV_STATE_T_EXPANDING_CONFLICT] = "t expanding conf",
45 [UWB_RSV_STATE_T_EXPANDING_PENDING] = "t expanding pend",
46 [UWB_RSV_STATE_T_EXPANDING_DENIED] = "t expanding den ",
49 static const char *rsv_types[] = {
50 [UWB_DRP_TYPE_ALIEN_BP] = "alien-bp",
51 [UWB_DRP_TYPE_HARD] = "hard",
52 [UWB_DRP_TYPE_SOFT] = "soft",
53 [UWB_DRP_TYPE_PRIVATE] = "private",
54 [UWB_DRP_TYPE_PCA] = "pca",
57 bool uwb_rsv_has_two_drp_ies(struct uwb_rsv *rsv)
59 static const bool has_two_drp_ies[] = {
60 [UWB_RSV_STATE_O_INITIATED] = false,
61 [UWB_RSV_STATE_O_PENDING] = false,
62 [UWB_RSV_STATE_O_MODIFIED] = false,
63 [UWB_RSV_STATE_O_ESTABLISHED] = false,
64 [UWB_RSV_STATE_O_TO_BE_MOVED] = false,
65 [UWB_RSV_STATE_O_MOVE_COMBINING] = false,
66 [UWB_RSV_STATE_O_MOVE_REDUCING] = false,
67 [UWB_RSV_STATE_O_MOVE_EXPANDING] = true,
68 [UWB_RSV_STATE_T_ACCEPTED] = false,
69 [UWB_RSV_STATE_T_CONFLICT] = false,
70 [UWB_RSV_STATE_T_PENDING] = false,
71 [UWB_RSV_STATE_T_DENIED] = false,
72 [UWB_RSV_STATE_T_RESIZED] = false,
73 [UWB_RSV_STATE_T_EXPANDING_ACCEPTED] = true,
74 [UWB_RSV_STATE_T_EXPANDING_CONFLICT] = true,
75 [UWB_RSV_STATE_T_EXPANDING_PENDING] = true,
76 [UWB_RSV_STATE_T_EXPANDING_DENIED] = true,
79 return has_two_drp_ies[rsv->state];
82 /**
83 * uwb_rsv_state_str - return a string for a reservation state
84 * @state: the reservation state.
86 const char *uwb_rsv_state_str(enum uwb_rsv_state state)
88 if (state < UWB_RSV_STATE_NONE || state >= UWB_RSV_STATE_LAST)
89 return "unknown";
90 return rsv_states[state];
92 EXPORT_SYMBOL_GPL(uwb_rsv_state_str);
94 /**
95 * uwb_rsv_type_str - return a string for a reservation type
96 * @type: the reservation type
98 const char *uwb_rsv_type_str(enum uwb_drp_type type)
100 if (type < UWB_DRP_TYPE_ALIEN_BP || type > UWB_DRP_TYPE_PCA)
101 return "invalid";
102 return rsv_types[type];
104 EXPORT_SYMBOL_GPL(uwb_rsv_type_str);
106 void uwb_rsv_dump(char *text, struct uwb_rsv *rsv)
108 struct device *dev = &rsv->rc->uwb_dev.dev;
109 struct uwb_dev_addr devaddr;
110 char owner[UWB_ADDR_STRSIZE], target[UWB_ADDR_STRSIZE];
112 uwb_dev_addr_print(owner, sizeof(owner), &rsv->owner->dev_addr);
113 if (rsv->target.type == UWB_RSV_TARGET_DEV)
114 devaddr = rsv->target.dev->dev_addr;
115 else
116 devaddr = rsv->target.devaddr;
117 uwb_dev_addr_print(target, sizeof(target), &devaddr);
119 dev_dbg(dev, "rsv %s %s -> %s: %s\n",
120 text, owner, target, uwb_rsv_state_str(rsv->state));
123 static void uwb_rsv_release(struct kref *kref)
125 struct uwb_rsv *rsv = container_of(kref, struct uwb_rsv, kref);
127 kfree(rsv);
130 void uwb_rsv_get(struct uwb_rsv *rsv)
132 kref_get(&rsv->kref);
135 void uwb_rsv_put(struct uwb_rsv *rsv)
137 kref_put(&rsv->kref, uwb_rsv_release);
141 * Get a free stream index for a reservation.
143 * If the target is a DevAddr (e.g., a WUSB cluster reservation) then
144 * the stream is allocated from a pool of per-RC stream indexes,
145 * otherwise a unique stream index for the target is selected.
147 static int uwb_rsv_get_stream(struct uwb_rsv *rsv)
149 struct uwb_rc *rc = rsv->rc;
150 struct device *dev = &rc->uwb_dev.dev;
151 unsigned long *streams_bm;
152 int stream;
154 switch (rsv->target.type) {
155 case UWB_RSV_TARGET_DEV:
156 streams_bm = rsv->target.dev->streams;
157 break;
158 case UWB_RSV_TARGET_DEVADDR:
159 streams_bm = rc->uwb_dev.streams;
160 break;
161 default:
162 return -EINVAL;
165 stream = find_first_zero_bit(streams_bm, UWB_NUM_STREAMS);
166 if (stream >= UWB_NUM_STREAMS) {
167 dev_err(dev, "%s: no available stream found\n", __func__);
168 return -EBUSY;
171 rsv->stream = stream;
172 set_bit(stream, streams_bm);
174 dev_dbg(dev, "get stream %d\n", rsv->stream);
176 return 0;
179 static void uwb_rsv_put_stream(struct uwb_rsv *rsv)
181 struct uwb_rc *rc = rsv->rc;
182 struct device *dev = &rc->uwb_dev.dev;
183 unsigned long *streams_bm;
185 switch (rsv->target.type) {
186 case UWB_RSV_TARGET_DEV:
187 streams_bm = rsv->target.dev->streams;
188 break;
189 case UWB_RSV_TARGET_DEVADDR:
190 streams_bm = rc->uwb_dev.streams;
191 break;
192 default:
193 return;
196 clear_bit(rsv->stream, streams_bm);
198 dev_dbg(dev, "put stream %d\n", rsv->stream);
201 void uwb_rsv_backoff_win_timer(unsigned long arg)
203 struct uwb_drp_backoff_win *bow = (struct uwb_drp_backoff_win *)arg;
204 struct uwb_rc *rc = container_of(bow, struct uwb_rc, bow);
205 struct device *dev = &rc->uwb_dev.dev;
207 bow->can_reserve_extra_mases = true;
208 if (bow->total_expired <= 4) {
209 bow->total_expired++;
210 } else {
211 /* after 4 backoff window has expired we can exit from
212 * the backoff procedure */
213 bow->total_expired = 0;
214 bow->window = UWB_DRP_BACKOFF_WIN_MIN >> 1;
216 dev_dbg(dev, "backoff_win_timer total_expired=%d, n=%d\n", bow->total_expired, bow->n);
218 /* try to relocate all the "to be moved" relocations */
219 uwb_rsv_handle_drp_avail_change(rc);
222 void uwb_rsv_backoff_win_increment(struct uwb_rc *rc)
224 struct uwb_drp_backoff_win *bow = &rc->bow;
225 struct device *dev = &rc->uwb_dev.dev;
226 unsigned timeout_us;
228 dev_dbg(dev, "backoff_win_increment: window=%d\n", bow->window);
230 bow->can_reserve_extra_mases = false;
232 if((bow->window << 1) == UWB_DRP_BACKOFF_WIN_MAX)
233 return;
235 bow->window <<= 1;
236 bow->n = prandom_u32() & (bow->window - 1);
237 dev_dbg(dev, "new_window=%d, n=%d\n", bow->window, bow->n);
239 /* reset the timer associated variables */
240 timeout_us = bow->n * UWB_SUPERFRAME_LENGTH_US;
241 bow->total_expired = 0;
242 mod_timer(&bow->timer, jiffies + usecs_to_jiffies(timeout_us));
245 static void uwb_rsv_stroke_timer(struct uwb_rsv *rsv)
247 int sframes = UWB_MAX_LOST_BEACONS;
250 * Multicast reservations can become established within 1
251 * super frame and should not be terminated if no response is
252 * received.
254 if (rsv->state == UWB_RSV_STATE_NONE) {
255 sframes = 0;
256 } else if (rsv->is_multicast) {
257 if (rsv->state == UWB_RSV_STATE_O_INITIATED
258 || rsv->state == UWB_RSV_STATE_O_MOVE_EXPANDING
259 || rsv->state == UWB_RSV_STATE_O_MOVE_COMBINING
260 || rsv->state == UWB_RSV_STATE_O_MOVE_REDUCING)
261 sframes = 1;
262 if (rsv->state == UWB_RSV_STATE_O_ESTABLISHED)
263 sframes = 0;
267 if (sframes > 0) {
269 * Add an additional 2 superframes to account for the
270 * time to send the SET DRP IE command.
272 unsigned timeout_us = (sframes + 2) * UWB_SUPERFRAME_LENGTH_US;
273 mod_timer(&rsv->timer, jiffies + usecs_to_jiffies(timeout_us));
274 } else
275 del_timer(&rsv->timer);
279 * Update a reservations state, and schedule an update of the
280 * transmitted DRP IEs.
282 static void uwb_rsv_state_update(struct uwb_rsv *rsv,
283 enum uwb_rsv_state new_state)
285 rsv->state = new_state;
286 rsv->ie_valid = false;
288 uwb_rsv_dump("SU", rsv);
290 uwb_rsv_stroke_timer(rsv);
291 uwb_rsv_sched_update(rsv->rc);
294 static void uwb_rsv_callback(struct uwb_rsv *rsv)
296 if (rsv->callback)
297 rsv->callback(rsv);
300 void uwb_rsv_set_state(struct uwb_rsv *rsv, enum uwb_rsv_state new_state)
302 struct uwb_rsv_move *mv = &rsv->mv;
304 if (rsv->state == new_state) {
305 switch (rsv->state) {
306 case UWB_RSV_STATE_O_ESTABLISHED:
307 case UWB_RSV_STATE_O_MOVE_EXPANDING:
308 case UWB_RSV_STATE_O_MOVE_COMBINING:
309 case UWB_RSV_STATE_O_MOVE_REDUCING:
310 case UWB_RSV_STATE_T_ACCEPTED:
311 case UWB_RSV_STATE_T_EXPANDING_ACCEPTED:
312 case UWB_RSV_STATE_T_RESIZED:
313 case UWB_RSV_STATE_NONE:
314 uwb_rsv_stroke_timer(rsv);
315 break;
316 default:
317 /* Expecting a state transition so leave timer
318 as-is. */
319 break;
321 return;
324 uwb_rsv_dump("SC", rsv);
326 switch (new_state) {
327 case UWB_RSV_STATE_NONE:
328 uwb_rsv_state_update(rsv, UWB_RSV_STATE_NONE);
329 uwb_rsv_remove(rsv);
330 uwb_rsv_callback(rsv);
331 break;
332 case UWB_RSV_STATE_O_INITIATED:
333 uwb_rsv_state_update(rsv, UWB_RSV_STATE_O_INITIATED);
334 break;
335 case UWB_RSV_STATE_O_PENDING:
336 uwb_rsv_state_update(rsv, UWB_RSV_STATE_O_PENDING);
337 break;
338 case UWB_RSV_STATE_O_MODIFIED:
339 /* in the companion there are the MASes to drop */
340 bitmap_andnot(rsv->mas.bm, rsv->mas.bm, mv->companion_mas.bm, UWB_NUM_MAS);
341 uwb_rsv_state_update(rsv, UWB_RSV_STATE_O_MODIFIED);
342 break;
343 case UWB_RSV_STATE_O_ESTABLISHED:
344 if (rsv->state == UWB_RSV_STATE_O_MODIFIED
345 || rsv->state == UWB_RSV_STATE_O_MOVE_REDUCING) {
346 uwb_drp_avail_release(rsv->rc, &mv->companion_mas);
347 rsv->needs_release_companion_mas = false;
349 uwb_drp_avail_reserve(rsv->rc, &rsv->mas);
350 uwb_rsv_state_update(rsv, UWB_RSV_STATE_O_ESTABLISHED);
351 uwb_rsv_callback(rsv);
352 break;
353 case UWB_RSV_STATE_O_MOVE_EXPANDING:
354 rsv->needs_release_companion_mas = true;
355 uwb_rsv_state_update(rsv, UWB_RSV_STATE_O_MOVE_EXPANDING);
356 break;
357 case UWB_RSV_STATE_O_MOVE_COMBINING:
358 rsv->needs_release_companion_mas = false;
359 uwb_drp_avail_reserve(rsv->rc, &mv->companion_mas);
360 bitmap_or(rsv->mas.bm, rsv->mas.bm, mv->companion_mas.bm, UWB_NUM_MAS);
361 rsv->mas.safe += mv->companion_mas.safe;
362 rsv->mas.unsafe += mv->companion_mas.unsafe;
363 uwb_rsv_state_update(rsv, UWB_RSV_STATE_O_MOVE_COMBINING);
364 break;
365 case UWB_RSV_STATE_O_MOVE_REDUCING:
366 bitmap_andnot(mv->companion_mas.bm, rsv->mas.bm, mv->final_mas.bm, UWB_NUM_MAS);
367 rsv->needs_release_companion_mas = true;
368 rsv->mas.safe = mv->final_mas.safe;
369 rsv->mas.unsafe = mv->final_mas.unsafe;
370 bitmap_copy(rsv->mas.bm, mv->final_mas.bm, UWB_NUM_MAS);
371 bitmap_copy(rsv->mas.unsafe_bm, mv->final_mas.unsafe_bm, UWB_NUM_MAS);
372 uwb_rsv_state_update(rsv, UWB_RSV_STATE_O_MOVE_REDUCING);
373 break;
374 case UWB_RSV_STATE_T_ACCEPTED:
375 case UWB_RSV_STATE_T_RESIZED:
376 rsv->needs_release_companion_mas = false;
377 uwb_drp_avail_reserve(rsv->rc, &rsv->mas);
378 uwb_rsv_state_update(rsv, UWB_RSV_STATE_T_ACCEPTED);
379 uwb_rsv_callback(rsv);
380 break;
381 case UWB_RSV_STATE_T_DENIED:
382 uwb_rsv_state_update(rsv, UWB_RSV_STATE_T_DENIED);
383 break;
384 case UWB_RSV_STATE_T_CONFLICT:
385 uwb_rsv_state_update(rsv, UWB_RSV_STATE_T_CONFLICT);
386 break;
387 case UWB_RSV_STATE_T_PENDING:
388 uwb_rsv_state_update(rsv, UWB_RSV_STATE_T_PENDING);
389 break;
390 case UWB_RSV_STATE_T_EXPANDING_ACCEPTED:
391 rsv->needs_release_companion_mas = true;
392 uwb_drp_avail_reserve(rsv->rc, &mv->companion_mas);
393 uwb_rsv_state_update(rsv, UWB_RSV_STATE_T_EXPANDING_ACCEPTED);
394 break;
395 default:
396 dev_err(&rsv->rc->uwb_dev.dev, "unhandled state: %s (%d)\n",
397 uwb_rsv_state_str(new_state), new_state);
401 static void uwb_rsv_handle_timeout_work(struct work_struct *work)
403 struct uwb_rsv *rsv = container_of(work, struct uwb_rsv,
404 handle_timeout_work);
405 struct uwb_rc *rc = rsv->rc;
407 mutex_lock(&rc->rsvs_mutex);
409 uwb_rsv_dump("TO", rsv);
411 switch (rsv->state) {
412 case UWB_RSV_STATE_O_INITIATED:
413 if (rsv->is_multicast) {
414 uwb_rsv_set_state(rsv, UWB_RSV_STATE_O_ESTABLISHED);
415 goto unlock;
417 break;
418 case UWB_RSV_STATE_O_MOVE_EXPANDING:
419 if (rsv->is_multicast) {
420 uwb_rsv_set_state(rsv, UWB_RSV_STATE_O_MOVE_COMBINING);
421 goto unlock;
423 break;
424 case UWB_RSV_STATE_O_MOVE_COMBINING:
425 if (rsv->is_multicast) {
426 uwb_rsv_set_state(rsv, UWB_RSV_STATE_O_MOVE_REDUCING);
427 goto unlock;
429 break;
430 case UWB_RSV_STATE_O_MOVE_REDUCING:
431 if (rsv->is_multicast) {
432 uwb_rsv_set_state(rsv, UWB_RSV_STATE_O_ESTABLISHED);
433 goto unlock;
435 break;
436 case UWB_RSV_STATE_O_ESTABLISHED:
437 if (rsv->is_multicast)
438 goto unlock;
439 break;
440 case UWB_RSV_STATE_T_EXPANDING_ACCEPTED:
442 * The time out could be for the main or of the
443 * companion DRP, assume it's for the companion and
444 * drop that first. A further time out is required to
445 * drop the main.
447 uwb_rsv_set_state(rsv, UWB_RSV_STATE_T_ACCEPTED);
448 uwb_drp_avail_release(rsv->rc, &rsv->mv.companion_mas);
449 goto unlock;
450 case UWB_RSV_STATE_NONE:
451 goto unlock;
452 default:
453 break;
456 uwb_rsv_remove(rsv);
458 unlock:
459 mutex_unlock(&rc->rsvs_mutex);
462 static struct uwb_rsv *uwb_rsv_alloc(struct uwb_rc *rc)
464 struct uwb_rsv *rsv;
466 rsv = kzalloc(sizeof(struct uwb_rsv), GFP_KERNEL);
467 if (!rsv)
468 return NULL;
470 INIT_LIST_HEAD(&rsv->rc_node);
471 INIT_LIST_HEAD(&rsv->pal_node);
472 kref_init(&rsv->kref);
473 init_timer(&rsv->timer);
474 rsv->timer.function = uwb_rsv_timer;
475 rsv->timer.data = (unsigned long)rsv;
477 rsv->rc = rc;
478 INIT_WORK(&rsv->handle_timeout_work, uwb_rsv_handle_timeout_work);
480 return rsv;
484 * uwb_rsv_create - allocate and initialize a UWB reservation structure
485 * @rc: the radio controller
486 * @cb: callback to use when the reservation completes or terminates
487 * @pal_priv: data private to the PAL to be passed in the callback
489 * The callback is called when the state of the reservation changes from:
491 * - pending to accepted
492 * - pending to denined
493 * - accepted to terminated
494 * - pending to terminated
496 struct uwb_rsv *uwb_rsv_create(struct uwb_rc *rc, uwb_rsv_cb_f cb, void *pal_priv)
498 struct uwb_rsv *rsv;
500 rsv = uwb_rsv_alloc(rc);
501 if (!rsv)
502 return NULL;
504 rsv->callback = cb;
505 rsv->pal_priv = pal_priv;
507 return rsv;
509 EXPORT_SYMBOL_GPL(uwb_rsv_create);
511 void uwb_rsv_remove(struct uwb_rsv *rsv)
513 uwb_rsv_dump("RM", rsv);
515 if (rsv->state != UWB_RSV_STATE_NONE)
516 uwb_rsv_set_state(rsv, UWB_RSV_STATE_NONE);
518 if (rsv->needs_release_companion_mas)
519 uwb_drp_avail_release(rsv->rc, &rsv->mv.companion_mas);
520 uwb_drp_avail_release(rsv->rc, &rsv->mas);
522 if (uwb_rsv_is_owner(rsv))
523 uwb_rsv_put_stream(rsv);
525 uwb_dev_put(rsv->owner);
526 if (rsv->target.type == UWB_RSV_TARGET_DEV)
527 uwb_dev_put(rsv->target.dev);
529 list_del_init(&rsv->rc_node);
530 uwb_rsv_put(rsv);
534 * uwb_rsv_destroy - free a UWB reservation structure
535 * @rsv: the reservation to free
537 * The reservation must already be terminated.
539 void uwb_rsv_destroy(struct uwb_rsv *rsv)
541 uwb_rsv_put(rsv);
543 EXPORT_SYMBOL_GPL(uwb_rsv_destroy);
546 * usb_rsv_establish - start a reservation establishment
547 * @rsv: the reservation
549 * The PAL should fill in @rsv's owner, target, type, max_mas,
550 * min_mas, max_interval and is_multicast fields. If the target is a
551 * uwb_dev it must be referenced.
553 * The reservation's callback will be called when the reservation is
554 * accepted, denied or times out.
556 int uwb_rsv_establish(struct uwb_rsv *rsv)
558 struct uwb_rc *rc = rsv->rc;
559 struct uwb_mas_bm available;
560 struct device *dev = &rc->uwb_dev.dev;
561 int ret;
563 mutex_lock(&rc->rsvs_mutex);
564 ret = uwb_rsv_get_stream(rsv);
565 if (ret) {
566 dev_err(dev, "%s: uwb_rsv_get_stream failed: %d\n",
567 __func__, ret);
568 goto out;
571 rsv->tiebreaker = prandom_u32() & 1;
572 /* get available mas bitmap */
573 uwb_drp_available(rc, &available);
575 ret = uwb_rsv_find_best_allocation(rsv, &available, &rsv->mas);
576 if (ret == UWB_RSV_ALLOC_NOT_FOUND) {
577 ret = -EBUSY;
578 uwb_rsv_put_stream(rsv);
579 dev_err(dev, "%s: uwb_rsv_find_best_allocation failed: %d\n",
580 __func__, ret);
581 goto out;
584 ret = uwb_drp_avail_reserve_pending(rc, &rsv->mas);
585 if (ret != 0) {
586 uwb_rsv_put_stream(rsv);
587 dev_err(dev, "%s: uwb_drp_avail_reserve_pending failed: %d\n",
588 __func__, ret);
589 goto out;
592 uwb_rsv_get(rsv);
593 list_add_tail(&rsv->rc_node, &rc->reservations);
594 rsv->owner = &rc->uwb_dev;
595 uwb_dev_get(rsv->owner);
596 uwb_rsv_set_state(rsv, UWB_RSV_STATE_O_INITIATED);
597 out:
598 mutex_unlock(&rc->rsvs_mutex);
599 return ret;
601 EXPORT_SYMBOL_GPL(uwb_rsv_establish);
604 * uwb_rsv_modify - modify an already established reservation
605 * @rsv: the reservation to modify
606 * @max_mas: new maximum MAS to reserve
607 * @min_mas: new minimum MAS to reserve
608 * @max_interval: new max_interval to use
610 * FIXME: implement this once there are PALs that use it.
612 int uwb_rsv_modify(struct uwb_rsv *rsv, int max_mas, int min_mas, int max_interval)
614 return -ENOSYS;
616 EXPORT_SYMBOL_GPL(uwb_rsv_modify);
619 * move an already established reservation (rc->rsvs_mutex must to be
620 * taken when tis function is called)
622 int uwb_rsv_try_move(struct uwb_rsv *rsv, struct uwb_mas_bm *available)
624 struct uwb_rc *rc = rsv->rc;
625 struct uwb_drp_backoff_win *bow = &rc->bow;
626 struct device *dev = &rc->uwb_dev.dev;
627 struct uwb_rsv_move *mv;
628 int ret = 0;
630 if (bow->can_reserve_extra_mases == false)
631 return -EBUSY;
633 mv = &rsv->mv;
635 if (uwb_rsv_find_best_allocation(rsv, available, &mv->final_mas) == UWB_RSV_ALLOC_FOUND) {
637 if (!bitmap_equal(rsv->mas.bm, mv->final_mas.bm, UWB_NUM_MAS)) {
638 /* We want to move the reservation */
639 bitmap_andnot(mv->companion_mas.bm, mv->final_mas.bm, rsv->mas.bm, UWB_NUM_MAS);
640 uwb_drp_avail_reserve_pending(rc, &mv->companion_mas);
641 uwb_rsv_set_state(rsv, UWB_RSV_STATE_O_MOVE_EXPANDING);
643 } else {
644 dev_dbg(dev, "new allocation not found\n");
647 return ret;
650 /* It will try to move every reservation in state O_ESTABLISHED giving
651 * to the MAS allocator algorithm an availability that is the real one
652 * plus the allocation already established from the reservation. */
653 void uwb_rsv_handle_drp_avail_change(struct uwb_rc *rc)
655 struct uwb_drp_backoff_win *bow = &rc->bow;
656 struct uwb_rsv *rsv;
657 struct uwb_mas_bm mas;
659 if (bow->can_reserve_extra_mases == false)
660 return;
662 list_for_each_entry(rsv, &rc->reservations, rc_node) {
663 if (rsv->state == UWB_RSV_STATE_O_ESTABLISHED ||
664 rsv->state == UWB_RSV_STATE_O_TO_BE_MOVED) {
665 uwb_drp_available(rc, &mas);
666 bitmap_or(mas.bm, mas.bm, rsv->mas.bm, UWB_NUM_MAS);
667 uwb_rsv_try_move(rsv, &mas);
674 * uwb_rsv_terminate - terminate an established reservation
675 * @rsv: the reservation to terminate
677 * A reservation is terminated by removing the DRP IE from the beacon,
678 * the other end will consider the reservation to be terminated when
679 * it does not see the DRP IE for at least mMaxLostBeacons.
681 * If applicable, the reference to the target uwb_dev will be released.
683 void uwb_rsv_terminate(struct uwb_rsv *rsv)
685 struct uwb_rc *rc = rsv->rc;
687 mutex_lock(&rc->rsvs_mutex);
689 if (rsv->state != UWB_RSV_STATE_NONE)
690 uwb_rsv_set_state(rsv, UWB_RSV_STATE_NONE);
692 mutex_unlock(&rc->rsvs_mutex);
694 EXPORT_SYMBOL_GPL(uwb_rsv_terminate);
697 * uwb_rsv_accept - accept a new reservation from a peer
698 * @rsv: the reservation
699 * @cb: call back for reservation changes
700 * @pal_priv: data to be passed in the above call back
702 * Reservation requests from peers are denied unless a PAL accepts it
703 * by calling this function.
705 * The PAL call uwb_rsv_destroy() for all accepted reservations before
706 * calling uwb_pal_unregister().
708 void uwb_rsv_accept(struct uwb_rsv *rsv, uwb_rsv_cb_f cb, void *pal_priv)
710 uwb_rsv_get(rsv);
712 rsv->callback = cb;
713 rsv->pal_priv = pal_priv;
714 rsv->state = UWB_RSV_STATE_T_ACCEPTED;
716 EXPORT_SYMBOL_GPL(uwb_rsv_accept);
719 * Is a received DRP IE for this reservation?
721 static bool uwb_rsv_match(struct uwb_rsv *rsv, struct uwb_dev *src,
722 struct uwb_ie_drp *drp_ie)
724 struct uwb_dev_addr *rsv_src;
725 int stream;
727 stream = uwb_ie_drp_stream_index(drp_ie);
729 if (rsv->stream != stream)
730 return false;
732 switch (rsv->target.type) {
733 case UWB_RSV_TARGET_DEVADDR:
734 return rsv->stream == stream;
735 case UWB_RSV_TARGET_DEV:
736 if (uwb_ie_drp_owner(drp_ie))
737 rsv_src = &rsv->owner->dev_addr;
738 else
739 rsv_src = &rsv->target.dev->dev_addr;
740 return uwb_dev_addr_cmp(&src->dev_addr, rsv_src) == 0;
742 return false;
745 static struct uwb_rsv *uwb_rsv_new_target(struct uwb_rc *rc,
746 struct uwb_dev *src,
747 struct uwb_ie_drp *drp_ie)
749 struct uwb_rsv *rsv;
750 struct uwb_pal *pal;
751 enum uwb_rsv_state state;
753 rsv = uwb_rsv_alloc(rc);
754 if (!rsv)
755 return NULL;
757 rsv->rc = rc;
758 rsv->owner = src;
759 uwb_dev_get(rsv->owner);
760 rsv->target.type = UWB_RSV_TARGET_DEV;
761 rsv->target.dev = &rc->uwb_dev;
762 uwb_dev_get(&rc->uwb_dev);
763 rsv->type = uwb_ie_drp_type(drp_ie);
764 rsv->stream = uwb_ie_drp_stream_index(drp_ie);
765 uwb_drp_ie_to_bm(&rsv->mas, drp_ie);
768 * See if any PALs are interested in this reservation. If not,
769 * deny the request.
771 rsv->state = UWB_RSV_STATE_T_DENIED;
772 mutex_lock(&rc->uwb_dev.mutex);
773 list_for_each_entry(pal, &rc->pals, node) {
774 if (pal->new_rsv)
775 pal->new_rsv(pal, rsv);
776 if (rsv->state == UWB_RSV_STATE_T_ACCEPTED)
777 break;
779 mutex_unlock(&rc->uwb_dev.mutex);
781 list_add_tail(&rsv->rc_node, &rc->reservations);
782 state = rsv->state;
783 rsv->state = UWB_RSV_STATE_NONE;
785 /* FIXME: do something sensible here */
786 if (state == UWB_RSV_STATE_T_ACCEPTED
787 && uwb_drp_avail_reserve_pending(rc, &rsv->mas) == -EBUSY) {
788 /* FIXME: do something sensible here */
789 } else {
790 uwb_rsv_set_state(rsv, state);
793 return rsv;
797 * uwb_rsv_get_usable_mas - get the bitmap of the usable MAS of a reservations
798 * @rsv: the reservation.
799 * @mas: returns the available MAS.
801 * The usable MAS of a reservation may be less than the negotiated MAS
802 * if alien BPs are present.
804 void uwb_rsv_get_usable_mas(struct uwb_rsv *rsv, struct uwb_mas_bm *mas)
806 bitmap_zero(mas->bm, UWB_NUM_MAS);
807 bitmap_andnot(mas->bm, rsv->mas.bm, rsv->rc->cnflt_alien_bitmap.bm, UWB_NUM_MAS);
809 EXPORT_SYMBOL_GPL(uwb_rsv_get_usable_mas);
812 * uwb_rsv_find - find a reservation for a received DRP IE.
813 * @rc: the radio controller
814 * @src: source of the DRP IE
815 * @drp_ie: the DRP IE
817 * If the reservation cannot be found and the DRP IE is from a peer
818 * attempting to establish a new reservation, create a new reservation
819 * and add it to the list.
821 struct uwb_rsv *uwb_rsv_find(struct uwb_rc *rc, struct uwb_dev *src,
822 struct uwb_ie_drp *drp_ie)
824 struct uwb_rsv *rsv;
826 list_for_each_entry(rsv, &rc->reservations, rc_node) {
827 if (uwb_rsv_match(rsv, src, drp_ie))
828 return rsv;
831 if (uwb_ie_drp_owner(drp_ie))
832 return uwb_rsv_new_target(rc, src, drp_ie);
834 return NULL;
838 * Go through all the reservations and check for timeouts and (if
839 * necessary) update their DRP IEs.
841 * FIXME: look at building the SET_DRP_IE command here rather than
842 * having to rescan the list in uwb_rc_send_all_drp_ie().
844 static bool uwb_rsv_update_all(struct uwb_rc *rc)
846 struct uwb_rsv *rsv, *t;
847 bool ie_updated = false;
849 list_for_each_entry_safe(rsv, t, &rc->reservations, rc_node) {
850 if (!rsv->ie_valid) {
851 uwb_drp_ie_update(rsv);
852 ie_updated = true;
856 return ie_updated;
859 void uwb_rsv_queue_update(struct uwb_rc *rc)
861 unsigned long delay_us = UWB_MAS_LENGTH_US * UWB_MAS_PER_ZONE;
863 queue_delayed_work(rc->rsv_workq, &rc->rsv_update_work, usecs_to_jiffies(delay_us));
867 * uwb_rsv_sched_update - schedule an update of the DRP IEs
868 * @rc: the radio controller.
870 * To improve performance and ensure correctness with [ECMA-368] the
871 * number of SET-DRP-IE commands that are done are limited.
873 * DRP IEs update come from two sources: DRP events from the hardware
874 * which all occur at the beginning of the superframe ('syncronous'
875 * events) and reservation establishment/termination requests from
876 * PALs or timers ('asynchronous' events).
878 * A delayed work ensures that all the synchronous events result in
879 * one SET-DRP-IE command.
881 * Additional logic (the set_drp_ie_pending and rsv_updated_postponed
882 * flags) will prevent an asynchrous event starting a SET-DRP-IE
883 * command if one is currently awaiting a response.
885 * FIXME: this does leave a window where an asynchrous event can delay
886 * the SET-DRP-IE for a synchronous event by one superframe.
888 void uwb_rsv_sched_update(struct uwb_rc *rc)
890 spin_lock_irq(&rc->rsvs_lock);
891 if (!delayed_work_pending(&rc->rsv_update_work)) {
892 if (rc->set_drp_ie_pending > 0) {
893 rc->set_drp_ie_pending++;
894 goto unlock;
896 uwb_rsv_queue_update(rc);
898 unlock:
899 spin_unlock_irq(&rc->rsvs_lock);
903 * Update DRP IEs and, if necessary, the DRP Availability IE and send
904 * the updated IEs to the radio controller.
906 static void uwb_rsv_update_work(struct work_struct *work)
908 struct uwb_rc *rc = container_of(work, struct uwb_rc,
909 rsv_update_work.work);
910 bool ie_updated;
912 mutex_lock(&rc->rsvs_mutex);
914 ie_updated = uwb_rsv_update_all(rc);
916 if (!rc->drp_avail.ie_valid) {
917 uwb_drp_avail_ie_update(rc);
918 ie_updated = true;
921 if (ie_updated && (rc->set_drp_ie_pending == 0))
922 uwb_rc_send_all_drp_ie(rc);
924 mutex_unlock(&rc->rsvs_mutex);
927 static void uwb_rsv_alien_bp_work(struct work_struct *work)
929 struct uwb_rc *rc = container_of(work, struct uwb_rc,
930 rsv_alien_bp_work.work);
931 struct uwb_rsv *rsv;
933 mutex_lock(&rc->rsvs_mutex);
935 list_for_each_entry(rsv, &rc->reservations, rc_node) {
936 if (rsv->type != UWB_DRP_TYPE_ALIEN_BP) {
937 uwb_rsv_callback(rsv);
941 mutex_unlock(&rc->rsvs_mutex);
944 static void uwb_rsv_timer(unsigned long arg)
946 struct uwb_rsv *rsv = (struct uwb_rsv *)arg;
948 queue_work(rsv->rc->rsv_workq, &rsv->handle_timeout_work);
952 * uwb_rsv_remove_all - remove all reservations
953 * @rc: the radio controller
955 * A DRP IE update is not done.
957 void uwb_rsv_remove_all(struct uwb_rc *rc)
959 struct uwb_rsv *rsv, *t;
961 mutex_lock(&rc->rsvs_mutex);
962 list_for_each_entry_safe(rsv, t, &rc->reservations, rc_node) {
963 if (rsv->state != UWB_RSV_STATE_NONE)
964 uwb_rsv_set_state(rsv, UWB_RSV_STATE_NONE);
965 del_timer_sync(&rsv->timer);
967 /* Cancel any postponed update. */
968 rc->set_drp_ie_pending = 0;
969 mutex_unlock(&rc->rsvs_mutex);
971 cancel_delayed_work_sync(&rc->rsv_update_work);
972 flush_workqueue(rc->rsv_workq);
974 mutex_lock(&rc->rsvs_mutex);
975 list_for_each_entry_safe(rsv, t, &rc->reservations, rc_node) {
976 uwb_rsv_remove(rsv);
978 mutex_unlock(&rc->rsvs_mutex);
981 void uwb_rsv_init(struct uwb_rc *rc)
983 INIT_LIST_HEAD(&rc->reservations);
984 INIT_LIST_HEAD(&rc->cnflt_alien_list);
985 mutex_init(&rc->rsvs_mutex);
986 spin_lock_init(&rc->rsvs_lock);
987 INIT_DELAYED_WORK(&rc->rsv_update_work, uwb_rsv_update_work);
988 INIT_DELAYED_WORK(&rc->rsv_alien_bp_work, uwb_rsv_alien_bp_work);
989 rc->bow.can_reserve_extra_mases = true;
990 rc->bow.total_expired = 0;
991 rc->bow.window = UWB_DRP_BACKOFF_WIN_MIN >> 1;
992 init_timer(&rc->bow.timer);
993 rc->bow.timer.function = uwb_rsv_backoff_win_timer;
994 rc->bow.timer.data = (unsigned long)&rc->bow;
996 bitmap_complement(rc->uwb_dev.streams, rc->uwb_dev.streams, UWB_NUM_STREAMS);
999 int uwb_rsv_setup(struct uwb_rc *rc)
1001 char name[16];
1003 snprintf(name, sizeof(name), "%s_rsvd", dev_name(&rc->uwb_dev.dev));
1004 rc->rsv_workq = create_singlethread_workqueue(name);
1005 if (rc->rsv_workq == NULL)
1006 return -ENOMEM;
1008 return 0;
1011 void uwb_rsv_cleanup(struct uwb_rc *rc)
1013 uwb_rsv_remove_all(rc);
1014 destroy_workqueue(rc->rsv_workq);