blob: f5e27247a38feac0a0294a57fa49ad0a260be637 [file] [log] [blame]
David Vrabel8cc13a02008-09-17 16:34:09 +01001/*
2 * UWB reservation management.
3 *
4 * Copyright (C) 2008 Cambridge Silicon Radio Ltd.
5 *
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.
9 *
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.
14 *
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/>.
17 */
David Vrabel8cc13a02008-09-17 16:34:09 +010018#include <linux/kernel.h>
19#include <linux/uwb.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090020#include <linux/slab.h>
Stefano Panella5b377172008-12-12 13:00:06 +000021#include <linux/random.h>
Paul Gortmaker475c0a62011-07-10 13:18:02 -040022#include <linux/export.h>
David Vrabel8cc13a02008-09-17 16:34:09 +010023
24#include "uwb-internal.h"
25
26static void uwb_rsv_timer(unsigned long arg);
27
28static const char *rsv_states[] = {
Stefano Panella5b377172008-12-12 13:00:06 +000029 [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 ",
David Vrabel8cc13a02008-09-17 16:34:09 +010047};
48
49static 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",
55};
56
Stefano Panella5b377172008-12-12 13:00:06 +000057bool uwb_rsv_has_two_drp_ies(struct uwb_rsv *rsv)
58{
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,
77 };
78
79 return has_two_drp_ies[rsv->state];
80}
81
David Vrabel8cc13a02008-09-17 16:34:09 +010082/**
83 * uwb_rsv_state_str - return a string for a reservation state
84 * @state: the reservation state.
85 */
86const char *uwb_rsv_state_str(enum uwb_rsv_state state)
87{
88 if (state < UWB_RSV_STATE_NONE || state >= UWB_RSV_STATE_LAST)
89 return "unknown";
90 return rsv_states[state];
91}
92EXPORT_SYMBOL_GPL(uwb_rsv_state_str);
93
94/**
95 * uwb_rsv_type_str - return a string for a reservation type
96 * @type: the reservation type
97 */
98const char *uwb_rsv_type_str(enum uwb_drp_type type)
99{
100 if (type < UWB_DRP_TYPE_ALIEN_BP || type > UWB_DRP_TYPE_PCA)
101 return "invalid";
102 return rsv_types[type];
103}
104EXPORT_SYMBOL_GPL(uwb_rsv_type_str);
105
Stefano Panella5b377172008-12-12 13:00:06 +0000106void uwb_rsv_dump(char *text, struct uwb_rsv *rsv)
David Vrabel8cc13a02008-09-17 16:34:09 +0100107{
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];
111
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);
118
David Vrabel2226b1c2009-01-06 17:55:32 +0000119 dev_dbg(dev, "rsv %s %s -> %s: %s\n",
120 text, owner, target, uwb_rsv_state_str(rsv->state));
David Vrabel8cc13a02008-09-17 16:34:09 +0100121}
122
David Vrabelcae1c112008-10-27 15:22:46 +0000123static void uwb_rsv_release(struct kref *kref)
124{
125 struct uwb_rsv *rsv = container_of(kref, struct uwb_rsv, kref);
126
127 kfree(rsv);
128}
129
Stefano Panella5b377172008-12-12 13:00:06 +0000130void uwb_rsv_get(struct uwb_rsv *rsv)
David Vrabelcae1c112008-10-27 15:22:46 +0000131{
132 kref_get(&rsv->kref);
133}
134
Stefano Panella5b377172008-12-12 13:00:06 +0000135void uwb_rsv_put(struct uwb_rsv *rsv)
David Vrabelcae1c112008-10-27 15:22:46 +0000136{
137 kref_put(&rsv->kref, uwb_rsv_release);
138}
139
David Vrabel8cc13a02008-09-17 16:34:09 +0100140/*
141 * Get a free stream index for a reservation.
142 *
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.
146 */
147static int uwb_rsv_get_stream(struct uwb_rsv *rsv)
148{
149 struct uwb_rc *rc = rsv->rc;
Stefano Panella5b377172008-12-12 13:00:06 +0000150 struct device *dev = &rc->uwb_dev.dev;
David Vrabel8cc13a02008-09-17 16:34:09 +0100151 unsigned long *streams_bm;
152 int stream;
153
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;
163 }
164
165 stream = find_first_zero_bit(streams_bm, UWB_NUM_STREAMS);
Thomas Pugliese7b360ee2014-04-25 10:30:33 -0500166 if (stream >= UWB_NUM_STREAMS) {
167 dev_err(dev, "%s: no available stream found\n", __func__);
David Vrabel8cc13a02008-09-17 16:34:09 +0100168 return -EBUSY;
Thomas Pugliese7b360ee2014-04-25 10:30:33 -0500169 }
David Vrabel8cc13a02008-09-17 16:34:09 +0100170
171 rsv->stream = stream;
172 set_bit(stream, streams_bm);
173
Stefano Panella5b377172008-12-12 13:00:06 +0000174 dev_dbg(dev, "get stream %d\n", rsv->stream);
175
David Vrabel8cc13a02008-09-17 16:34:09 +0100176 return 0;
177}
178
179static void uwb_rsv_put_stream(struct uwb_rsv *rsv)
180{
181 struct uwb_rc *rc = rsv->rc;
Stefano Panella5b377172008-12-12 13:00:06 +0000182 struct device *dev = &rc->uwb_dev.dev;
David Vrabel8cc13a02008-09-17 16:34:09 +0100183 unsigned long *streams_bm;
184
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;
194 }
195
196 clear_bit(rsv->stream, streams_bm);
Stefano Panella5b377172008-12-12 13:00:06 +0000197
198 dev_dbg(dev, "put stream %d\n", rsv->stream);
David Vrabel8cc13a02008-09-17 16:34:09 +0100199}
200
Stefano Panella5b377172008-12-12 13:00:06 +0000201void uwb_rsv_backoff_win_timer(unsigned long arg)
David Vrabel8cc13a02008-09-17 16:34:09 +0100202{
Stefano Panella5b377172008-12-12 13:00:06 +0000203 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;
David Vrabel8cc13a02008-09-17 16:34:09 +0100206
Stefano Panella5b377172008-12-12 13:00:06 +0000207 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;
David Vrabel8cc13a02008-09-17 16:34:09 +0100215 }
Geert Uytterhoevend9c3ae22014-10-23 11:52:57 +0200216 dev_dbg(dev, "backoff_win_timer total_expired=%d, n=%d\n", bow->total_expired, bow->n);
Stefano Panella5b377172008-12-12 13:00:06 +0000217
218 /* try to relocate all the "to be moved" relocations */
219 uwb_rsv_handle_drp_avail_change(rc);
David Vrabel8cc13a02008-09-17 16:34:09 +0100220}
221
Stefano Panella5b377172008-12-12 13:00:06 +0000222void uwb_rsv_backoff_win_increment(struct uwb_rc *rc)
David Vrabel8cc13a02008-09-17 16:34:09 +0100223{
Stefano Panella5b377172008-12-12 13:00:06 +0000224 struct uwb_drp_backoff_win *bow = &rc->bow;
225 struct device *dev = &rc->uwb_dev.dev;
226 unsigned timeout_us;
David Vrabel8cc13a02008-09-17 16:34:09 +0100227
Stefano Panella5b377172008-12-12 13:00:06 +0000228 dev_dbg(dev, "backoff_win_increment: window=%d\n", bow->window);
David Vrabel8cc13a02008-09-17 16:34:09 +0100229
Stefano Panella5b377172008-12-12 13:00:06 +0000230 bow->can_reserve_extra_mases = false;
David Vrabel8cc13a02008-09-17 16:34:09 +0100231
Stefano Panella5b377172008-12-12 13:00:06 +0000232 if((bow->window << 1) == UWB_DRP_BACKOFF_WIN_MAX)
233 return;
David Vrabel8cc13a02008-09-17 16:34:09 +0100234
Stefano Panella5b377172008-12-12 13:00:06 +0000235 bow->window <<= 1;
Akinobu Mita78782022013-04-29 16:21:34 -0700236 bow->n = prandom_u32() & (bow->window - 1);
Geert Uytterhoevend9c3ae22014-10-23 11:52:57 +0200237 dev_dbg(dev, "new_window=%d, n=%d\n", bow->window, bow->n);
David Vrabel8cc13a02008-09-17 16:34:09 +0100238
Stefano Panella5b377172008-12-12 13:00:06 +0000239 /* reset the timer associated variables */
240 timeout_us = bow->n * UWB_SUPERFRAME_LENGTH_US;
241 bow->total_expired = 0;
Thomas Pugliese67d0fb22013-12-20 11:54:08 -0600242 mod_timer(&bow->timer, jiffies + usecs_to_jiffies(timeout_us));
David Vrabel8cc13a02008-09-17 16:34:09 +0100243}
244
245static void uwb_rsv_stroke_timer(struct uwb_rsv *rsv)
246{
247 int sframes = UWB_MAX_LOST_BEACONS;
248
249 /*
250 * Multicast reservations can become established within 1
251 * super frame and should not be terminated if no response is
252 * received.
253 */
Thomas Pugliese90ec00d2014-04-25 10:30:32 -0500254 if (rsv->state == UWB_RSV_STATE_NONE) {
255 sframes = 0;
256 } else if (rsv->is_multicast) {
Stefano Panella5b377172008-12-12 13:00:06 +0000257 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)
David Vrabel8cc13a02008-09-17 16:34:09 +0100261 sframes = 1;
262 if (rsv->state == UWB_RSV_STATE_O_ESTABLISHED)
263 sframes = 0;
Thomas Pugliese67d0fb22013-12-20 11:54:08 -0600264
David Vrabel8cc13a02008-09-17 16:34:09 +0100265 }
266
David Vrabel8cc13a02008-09-17 16:34:09 +0100267 if (sframes > 0) {
268 /*
269 * Add an additional 2 superframes to account for the
270 * time to send the SET DRP IE command.
271 */
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);
276}
277
278/*
279 * Update a reservations state, and schedule an update of the
280 * transmitted DRP IEs.
281 */
282static void uwb_rsv_state_update(struct uwb_rsv *rsv,
283 enum uwb_rsv_state new_state)
284{
285 rsv->state = new_state;
286 rsv->ie_valid = false;
287
Stefano Panella5b377172008-12-12 13:00:06 +0000288 uwb_rsv_dump("SU", rsv);
David Vrabel8cc13a02008-09-17 16:34:09 +0100289
290 uwb_rsv_stroke_timer(rsv);
291 uwb_rsv_sched_update(rsv->rc);
292}
293
294static void uwb_rsv_callback(struct uwb_rsv *rsv)
295{
296 if (rsv->callback)
297 rsv->callback(rsv);
298}
299
300void uwb_rsv_set_state(struct uwb_rsv *rsv, enum uwb_rsv_state new_state)
301{
Stefano Panella5b377172008-12-12 13:00:06 +0000302 struct uwb_rsv_move *mv = &rsv->mv;
303
David Vrabel8cc13a02008-09-17 16:34:09 +0100304 if (rsv->state == new_state) {
305 switch (rsv->state) {
306 case UWB_RSV_STATE_O_ESTABLISHED:
Stefano Panella5b377172008-12-12 13:00:06 +0000307 case UWB_RSV_STATE_O_MOVE_EXPANDING:
308 case UWB_RSV_STATE_O_MOVE_COMBINING:
309 case UWB_RSV_STATE_O_MOVE_REDUCING:
David Vrabel8cc13a02008-09-17 16:34:09 +0100310 case UWB_RSV_STATE_T_ACCEPTED:
Stefano Panella5b377172008-12-12 13:00:06 +0000311 case UWB_RSV_STATE_T_EXPANDING_ACCEPTED:
312 case UWB_RSV_STATE_T_RESIZED:
David Vrabel8cc13a02008-09-17 16:34:09 +0100313 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;
320 }
321 return;
322 }
323
Stefano Panella5b377172008-12-12 13:00:06 +0000324 uwb_rsv_dump("SC", rsv);
325
David Vrabel8cc13a02008-09-17 16:34:09 +0100326 switch (new_state) {
327 case UWB_RSV_STATE_NONE:
David Vrabel8cc13a02008-09-17 16:34:09 +0100328 uwb_rsv_state_update(rsv, UWB_RSV_STATE_NONE);
Thomas Pugliese90ec00d2014-04-25 10:30:32 -0500329 uwb_rsv_remove(rsv);
David Vrabel8cc13a02008-09-17 16:34:09 +0100330 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;
Stefano Panella5b377172008-12-12 13:00:06 +0000338 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;
David Vrabel8cc13a02008-09-17 16:34:09 +0100343 case UWB_RSV_STATE_O_ESTABLISHED:
Stefano Panella5b377172008-12-12 13:00:06 +0000344 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;
348 }
David Vrabel8cc13a02008-09-17 16:34:09 +0100349 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;
Stefano Panella5b377172008-12-12 13:00:06 +0000353 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;
David Vrabel8cc13a02008-09-17 16:34:09 +0100374 case UWB_RSV_STATE_T_ACCEPTED:
Stefano Panella5b377172008-12-12 13:00:06 +0000375 case UWB_RSV_STATE_T_RESIZED:
376 rsv->needs_release_companion_mas = false;
David Vrabel8cc13a02008-09-17 16:34:09 +0100377 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;
Stefano Panella5b377172008-12-12 13:00:06 +0000384 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;
David Vrabel8cc13a02008-09-17 16:34:09 +0100395 default:
396 dev_err(&rsv->rc->uwb_dev.dev, "unhandled state: %s (%d)\n",
397 uwb_rsv_state_str(new_state), new_state);
398 }
399}
400
Stefano Panella5b377172008-12-12 13:00:06 +0000401static void uwb_rsv_handle_timeout_work(struct work_struct *work)
402{
403 struct uwb_rsv *rsv = container_of(work, struct uwb_rsv,
404 handle_timeout_work);
405 struct uwb_rc *rc = rsv->rc;
406
407 mutex_lock(&rc->rsvs_mutex);
408
409 uwb_rsv_dump("TO", rsv);
410
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;
416 }
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;
422 }
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;
428 }
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;
434 }
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:
441 /*
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.
446 */
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;
Thomas Pugliese90ec00d2014-04-25 10:30:32 -0500450 case UWB_RSV_STATE_NONE:
451 goto unlock;
Stefano Panella5b377172008-12-12 13:00:06 +0000452 default:
453 break;
454 }
455
456 uwb_rsv_remove(rsv);
457
458unlock:
459 mutex_unlock(&rc->rsvs_mutex);
460}
461
David Vrabel8cc13a02008-09-17 16:34:09 +0100462static struct uwb_rsv *uwb_rsv_alloc(struct uwb_rc *rc)
463{
464 struct uwb_rsv *rsv;
465
466 rsv = kzalloc(sizeof(struct uwb_rsv), GFP_KERNEL);
467 if (!rsv)
468 return NULL;
469
470 INIT_LIST_HEAD(&rsv->rc_node);
471 INIT_LIST_HEAD(&rsv->pal_node);
David Vrabelcae1c112008-10-27 15:22:46 +0000472 kref_init(&rsv->kref);
Muhammad Falak R Wania895dc02015-10-21 00:00:37 +0530473 setup_timer(&rsv->timer, uwb_rsv_timer, (unsigned long)rsv);
David Vrabel8cc13a02008-09-17 16:34:09 +0100474
475 rsv->rc = rc;
Stefano Panella5b377172008-12-12 13:00:06 +0000476 INIT_WORK(&rsv->handle_timeout_work, uwb_rsv_handle_timeout_work);
David Vrabel8cc13a02008-09-17 16:34:09 +0100477
478 return rsv;
479}
480
David Vrabel8cc13a02008-09-17 16:34:09 +0100481/**
482 * uwb_rsv_create - allocate and initialize a UWB reservation structure
483 * @rc: the radio controller
484 * @cb: callback to use when the reservation completes or terminates
485 * @pal_priv: data private to the PAL to be passed in the callback
486 *
487 * The callback is called when the state of the reservation changes from:
488 *
489 * - pending to accepted
490 * - pending to denined
491 * - accepted to terminated
492 * - pending to terminated
493 */
494struct uwb_rsv *uwb_rsv_create(struct uwb_rc *rc, uwb_rsv_cb_f cb, void *pal_priv)
495{
496 struct uwb_rsv *rsv;
497
498 rsv = uwb_rsv_alloc(rc);
499 if (!rsv)
500 return NULL;
501
502 rsv->callback = cb;
503 rsv->pal_priv = pal_priv;
504
505 return rsv;
506}
507EXPORT_SYMBOL_GPL(uwb_rsv_create);
508
509void uwb_rsv_remove(struct uwb_rsv *rsv)
510{
Stefano Panella5b377172008-12-12 13:00:06 +0000511 uwb_rsv_dump("RM", rsv);
512
David Vrabel8cc13a02008-09-17 16:34:09 +0100513 if (rsv->state != UWB_RSV_STATE_NONE)
514 uwb_rsv_set_state(rsv, UWB_RSV_STATE_NONE);
Stefano Panella5b377172008-12-12 13:00:06 +0000515
516 if (rsv->needs_release_companion_mas)
517 uwb_drp_avail_release(rsv->rc, &rsv->mv.companion_mas);
518 uwb_drp_avail_release(rsv->rc, &rsv->mas);
519
520 if (uwb_rsv_is_owner(rsv))
521 uwb_rsv_put_stream(rsv);
David Vrabel2226b1c2009-01-06 17:55:32 +0000522
David Vrabelcae1c112008-10-27 15:22:46 +0000523 uwb_dev_put(rsv->owner);
524 if (rsv->target.type == UWB_RSV_TARGET_DEV)
525 uwb_dev_put(rsv->target.dev);
526
527 list_del_init(&rsv->rc_node);
528 uwb_rsv_put(rsv);
David Vrabel8cc13a02008-09-17 16:34:09 +0100529}
530
531/**
532 * uwb_rsv_destroy - free a UWB reservation structure
533 * @rsv: the reservation to free
534 *
David Vrabelcae1c112008-10-27 15:22:46 +0000535 * The reservation must already be terminated.
David Vrabel8cc13a02008-09-17 16:34:09 +0100536 */
537void uwb_rsv_destroy(struct uwb_rsv *rsv)
538{
David Vrabelcae1c112008-10-27 15:22:46 +0000539 uwb_rsv_put(rsv);
David Vrabel8cc13a02008-09-17 16:34:09 +0100540}
541EXPORT_SYMBOL_GPL(uwb_rsv_destroy);
542
543/**
544 * usb_rsv_establish - start a reservation establishment
545 * @rsv: the reservation
546 *
547 * The PAL should fill in @rsv's owner, target, type, max_mas,
Stefano Panella5b377172008-12-12 13:00:06 +0000548 * min_mas, max_interval and is_multicast fields. If the target is a
David Vrabel8cc13a02008-09-17 16:34:09 +0100549 * uwb_dev it must be referenced.
550 *
551 * The reservation's callback will be called when the reservation is
552 * accepted, denied or times out.
553 */
554int uwb_rsv_establish(struct uwb_rsv *rsv)
555{
556 struct uwb_rc *rc = rsv->rc;
Stefano Panella5b377172008-12-12 13:00:06 +0000557 struct uwb_mas_bm available;
Thomas Pugliese7b360ee2014-04-25 10:30:33 -0500558 struct device *dev = &rc->uwb_dev.dev;
David Vrabel8cc13a02008-09-17 16:34:09 +0100559 int ret;
560
561 mutex_lock(&rc->rsvs_mutex);
David Vrabel8cc13a02008-09-17 16:34:09 +0100562 ret = uwb_rsv_get_stream(rsv);
Thomas Pugliese7b360ee2014-04-25 10:30:33 -0500563 if (ret) {
564 dev_err(dev, "%s: uwb_rsv_get_stream failed: %d\n",
565 __func__, ret);
David Vrabel8cc13a02008-09-17 16:34:09 +0100566 goto out;
Thomas Pugliese7b360ee2014-04-25 10:30:33 -0500567 }
David Vrabel8cc13a02008-09-17 16:34:09 +0100568
Akinobu Mita78782022013-04-29 16:21:34 -0700569 rsv->tiebreaker = prandom_u32() & 1;
Stefano Panella5b377172008-12-12 13:00:06 +0000570 /* get available mas bitmap */
571 uwb_drp_available(rc, &available);
572
573 ret = uwb_rsv_find_best_allocation(rsv, &available, &rsv->mas);
574 if (ret == UWB_RSV_ALLOC_NOT_FOUND) {
575 ret = -EBUSY;
576 uwb_rsv_put_stream(rsv);
Thomas Pugliese7b360ee2014-04-25 10:30:33 -0500577 dev_err(dev, "%s: uwb_rsv_find_best_allocation failed: %d\n",
578 __func__, ret);
Stefano Panella5b377172008-12-12 13:00:06 +0000579 goto out;
580 }
581
582 ret = uwb_drp_avail_reserve_pending(rc, &rsv->mas);
583 if (ret != 0) {
David Vrabel8cc13a02008-09-17 16:34:09 +0100584 uwb_rsv_put_stream(rsv);
Thomas Pugliese7b360ee2014-04-25 10:30:33 -0500585 dev_err(dev, "%s: uwb_drp_avail_reserve_pending failed: %d\n",
586 __func__, ret);
David Vrabel8cc13a02008-09-17 16:34:09 +0100587 goto out;
588 }
589
David Vrabelcae1c112008-10-27 15:22:46 +0000590 uwb_rsv_get(rsv);
David Vrabel8cc13a02008-09-17 16:34:09 +0100591 list_add_tail(&rsv->rc_node, &rc->reservations);
592 rsv->owner = &rc->uwb_dev;
593 uwb_dev_get(rsv->owner);
594 uwb_rsv_set_state(rsv, UWB_RSV_STATE_O_INITIATED);
595out:
596 mutex_unlock(&rc->rsvs_mutex);
597 return ret;
598}
599EXPORT_SYMBOL_GPL(uwb_rsv_establish);
600
601/**
602 * uwb_rsv_modify - modify an already established reservation
603 * @rsv: the reservation to modify
604 * @max_mas: new maximum MAS to reserve
605 * @min_mas: new minimum MAS to reserve
Stefano Panella5b377172008-12-12 13:00:06 +0000606 * @max_interval: new max_interval to use
David Vrabel8cc13a02008-09-17 16:34:09 +0100607 *
608 * FIXME: implement this once there are PALs that use it.
609 */
Stefano Panella5b377172008-12-12 13:00:06 +0000610int uwb_rsv_modify(struct uwb_rsv *rsv, int max_mas, int min_mas, int max_interval)
David Vrabel8cc13a02008-09-17 16:34:09 +0100611{
612 return -ENOSYS;
613}
614EXPORT_SYMBOL_GPL(uwb_rsv_modify);
615
Stefano Panella5b377172008-12-12 13:00:06 +0000616/*
617 * move an already established reservation (rc->rsvs_mutex must to be
618 * taken when tis function is called)
619 */
620int uwb_rsv_try_move(struct uwb_rsv *rsv, struct uwb_mas_bm *available)
621{
622 struct uwb_rc *rc = rsv->rc;
623 struct uwb_drp_backoff_win *bow = &rc->bow;
624 struct device *dev = &rc->uwb_dev.dev;
625 struct uwb_rsv_move *mv;
626 int ret = 0;
Thomas Pugliese67d0fb22013-12-20 11:54:08 -0600627
Stefano Panella5b377172008-12-12 13:00:06 +0000628 if (bow->can_reserve_extra_mases == false)
629 return -EBUSY;
630
631 mv = &rsv->mv;
632
633 if (uwb_rsv_find_best_allocation(rsv, available, &mv->final_mas) == UWB_RSV_ALLOC_FOUND) {
634
635 if (!bitmap_equal(rsv->mas.bm, mv->final_mas.bm, UWB_NUM_MAS)) {
636 /* We want to move the reservation */
637 bitmap_andnot(mv->companion_mas.bm, mv->final_mas.bm, rsv->mas.bm, UWB_NUM_MAS);
638 uwb_drp_avail_reserve_pending(rc, &mv->companion_mas);
639 uwb_rsv_set_state(rsv, UWB_RSV_STATE_O_MOVE_EXPANDING);
640 }
641 } else {
642 dev_dbg(dev, "new allocation not found\n");
643 }
Thomas Pugliese67d0fb22013-12-20 11:54:08 -0600644
Stefano Panella5b377172008-12-12 13:00:06 +0000645 return ret;
646}
647
648/* It will try to move every reservation in state O_ESTABLISHED giving
649 * to the MAS allocator algorithm an availability that is the real one
650 * plus the allocation already established from the reservation. */
651void uwb_rsv_handle_drp_avail_change(struct uwb_rc *rc)
652{
653 struct uwb_drp_backoff_win *bow = &rc->bow;
654 struct uwb_rsv *rsv;
655 struct uwb_mas_bm mas;
Thomas Pugliese67d0fb22013-12-20 11:54:08 -0600656
Stefano Panella5b377172008-12-12 13:00:06 +0000657 if (bow->can_reserve_extra_mases == false)
658 return;
659
660 list_for_each_entry(rsv, &rc->reservations, rc_node) {
661 if (rsv->state == UWB_RSV_STATE_O_ESTABLISHED ||
662 rsv->state == UWB_RSV_STATE_O_TO_BE_MOVED) {
663 uwb_drp_available(rc, &mas);
664 bitmap_or(mas.bm, mas.bm, rsv->mas.bm, UWB_NUM_MAS);
665 uwb_rsv_try_move(rsv, &mas);
666 }
667 }
Thomas Pugliese67d0fb22013-12-20 11:54:08 -0600668
Stefano Panella5b377172008-12-12 13:00:06 +0000669}
670
David Vrabel8cc13a02008-09-17 16:34:09 +0100671/**
672 * uwb_rsv_terminate - terminate an established reservation
673 * @rsv: the reservation to terminate
674 *
675 * A reservation is terminated by removing the DRP IE from the beacon,
676 * the other end will consider the reservation to be terminated when
677 * it does not see the DRP IE for at least mMaxLostBeacons.
678 *
679 * If applicable, the reference to the target uwb_dev will be released.
680 */
681void uwb_rsv_terminate(struct uwb_rsv *rsv)
682{
683 struct uwb_rc *rc = rsv->rc;
684
685 mutex_lock(&rc->rsvs_mutex);
686
David Vrabel671e4702008-12-12 13:23:24 +0000687 if (rsv->state != UWB_RSV_STATE_NONE)
688 uwb_rsv_set_state(rsv, UWB_RSV_STATE_NONE);
David Vrabel8cc13a02008-09-17 16:34:09 +0100689
690 mutex_unlock(&rc->rsvs_mutex);
691}
692EXPORT_SYMBOL_GPL(uwb_rsv_terminate);
693
694/**
695 * uwb_rsv_accept - accept a new reservation from a peer
696 * @rsv: the reservation
697 * @cb: call back for reservation changes
698 * @pal_priv: data to be passed in the above call back
699 *
700 * Reservation requests from peers are denied unless a PAL accepts it
701 * by calling this function.
David Vrabelcae1c112008-10-27 15:22:46 +0000702 *
703 * The PAL call uwb_rsv_destroy() for all accepted reservations before
704 * calling uwb_pal_unregister().
David Vrabel8cc13a02008-09-17 16:34:09 +0100705 */
706void uwb_rsv_accept(struct uwb_rsv *rsv, uwb_rsv_cb_f cb, void *pal_priv)
707{
David Vrabelcae1c112008-10-27 15:22:46 +0000708 uwb_rsv_get(rsv);
709
David Vrabel8cc13a02008-09-17 16:34:09 +0100710 rsv->callback = cb;
711 rsv->pal_priv = pal_priv;
712 rsv->state = UWB_RSV_STATE_T_ACCEPTED;
713}
714EXPORT_SYMBOL_GPL(uwb_rsv_accept);
715
716/*
717 * Is a received DRP IE for this reservation?
718 */
719static bool uwb_rsv_match(struct uwb_rsv *rsv, struct uwb_dev *src,
720 struct uwb_ie_drp *drp_ie)
721{
722 struct uwb_dev_addr *rsv_src;
723 int stream;
724
725 stream = uwb_ie_drp_stream_index(drp_ie);
726
727 if (rsv->stream != stream)
728 return false;
729
730 switch (rsv->target.type) {
731 case UWB_RSV_TARGET_DEVADDR:
732 return rsv->stream == stream;
733 case UWB_RSV_TARGET_DEV:
734 if (uwb_ie_drp_owner(drp_ie))
735 rsv_src = &rsv->owner->dev_addr;
736 else
737 rsv_src = &rsv->target.dev->dev_addr;
738 return uwb_dev_addr_cmp(&src->dev_addr, rsv_src) == 0;
739 }
740 return false;
741}
742
743static struct uwb_rsv *uwb_rsv_new_target(struct uwb_rc *rc,
744 struct uwb_dev *src,
745 struct uwb_ie_drp *drp_ie)
746{
747 struct uwb_rsv *rsv;
748 struct uwb_pal *pal;
749 enum uwb_rsv_state state;
750
751 rsv = uwb_rsv_alloc(rc);
752 if (!rsv)
753 return NULL;
754
755 rsv->rc = rc;
756 rsv->owner = src;
757 uwb_dev_get(rsv->owner);
758 rsv->target.type = UWB_RSV_TARGET_DEV;
759 rsv->target.dev = &rc->uwb_dev;
Stefano Panella5b377172008-12-12 13:00:06 +0000760 uwb_dev_get(&rc->uwb_dev);
David Vrabel8cc13a02008-09-17 16:34:09 +0100761 rsv->type = uwb_ie_drp_type(drp_ie);
762 rsv->stream = uwb_ie_drp_stream_index(drp_ie);
David Vrabel8cc13a02008-09-17 16:34:09 +0100763 uwb_drp_ie_to_bm(&rsv->mas, drp_ie);
764
765 /*
766 * See if any PALs are interested in this reservation. If not,
767 * deny the request.
768 */
769 rsv->state = UWB_RSV_STATE_T_DENIED;
David Vrabel6fae35f2008-11-17 15:53:42 +0000770 mutex_lock(&rc->uwb_dev.mutex);
David Vrabel8cc13a02008-09-17 16:34:09 +0100771 list_for_each_entry(pal, &rc->pals, node) {
772 if (pal->new_rsv)
David Vrabele17be2b2008-11-17 15:24:14 +0000773 pal->new_rsv(pal, rsv);
David Vrabel8cc13a02008-09-17 16:34:09 +0100774 if (rsv->state == UWB_RSV_STATE_T_ACCEPTED)
775 break;
776 }
David Vrabel6fae35f2008-11-17 15:53:42 +0000777 mutex_unlock(&rc->uwb_dev.mutex);
David Vrabel8cc13a02008-09-17 16:34:09 +0100778
779 list_add_tail(&rsv->rc_node, &rc->reservations);
780 state = rsv->state;
781 rsv->state = UWB_RSV_STATE_NONE;
Stefano Panella5b377172008-12-12 13:00:06 +0000782
783 /* FIXME: do something sensible here */
784 if (state == UWB_RSV_STATE_T_ACCEPTED
785 && uwb_drp_avail_reserve_pending(rc, &rsv->mas) == -EBUSY) {
786 /* FIXME: do something sensible here */
787 } else {
788 uwb_rsv_set_state(rsv, state);
789 }
David Vrabel8cc13a02008-09-17 16:34:09 +0100790
791 return rsv;
792}
793
794/**
Stefano Panella5b377172008-12-12 13:00:06 +0000795 * uwb_rsv_get_usable_mas - get the bitmap of the usable MAS of a reservations
796 * @rsv: the reservation.
797 * @mas: returns the available MAS.
798 *
799 * The usable MAS of a reservation may be less than the negotiated MAS
800 * if alien BPs are present.
801 */
802void uwb_rsv_get_usable_mas(struct uwb_rsv *rsv, struct uwb_mas_bm *mas)
803{
804 bitmap_zero(mas->bm, UWB_NUM_MAS);
805 bitmap_andnot(mas->bm, rsv->mas.bm, rsv->rc->cnflt_alien_bitmap.bm, UWB_NUM_MAS);
806}
807EXPORT_SYMBOL_GPL(uwb_rsv_get_usable_mas);
808
809/**
David Vrabel8cc13a02008-09-17 16:34:09 +0100810 * uwb_rsv_find - find a reservation for a received DRP IE.
811 * @rc: the radio controller
812 * @src: source of the DRP IE
813 * @drp_ie: the DRP IE
814 *
815 * If the reservation cannot be found and the DRP IE is from a peer
816 * attempting to establish a new reservation, create a new reservation
817 * and add it to the list.
818 */
819struct uwb_rsv *uwb_rsv_find(struct uwb_rc *rc, struct uwb_dev *src,
820 struct uwb_ie_drp *drp_ie)
821{
822 struct uwb_rsv *rsv;
823
824 list_for_each_entry(rsv, &rc->reservations, rc_node) {
825 if (uwb_rsv_match(rsv, src, drp_ie))
826 return rsv;
827 }
828
829 if (uwb_ie_drp_owner(drp_ie))
830 return uwb_rsv_new_target(rc, src, drp_ie);
831
832 return NULL;
833}
834
835/*
836 * Go through all the reservations and check for timeouts and (if
837 * necessary) update their DRP IEs.
838 *
839 * FIXME: look at building the SET_DRP_IE command here rather than
840 * having to rescan the list in uwb_rc_send_all_drp_ie().
841 */
842static bool uwb_rsv_update_all(struct uwb_rc *rc)
843{
844 struct uwb_rsv *rsv, *t;
845 bool ie_updated = false;
846
847 list_for_each_entry_safe(rsv, t, &rc->reservations, rc_node) {
David Vrabel8cc13a02008-09-17 16:34:09 +0100848 if (!rsv->ie_valid) {
849 uwb_drp_ie_update(rsv);
850 ie_updated = true;
851 }
852 }
853
854 return ie_updated;
855}
856
Stefano Panella5b377172008-12-12 13:00:06 +0000857void uwb_rsv_queue_update(struct uwb_rc *rc)
858{
859 unsigned long delay_us = UWB_MAS_LENGTH_US * UWB_MAS_PER_ZONE;
860
861 queue_delayed_work(rc->rsv_workq, &rc->rsv_update_work, usecs_to_jiffies(delay_us));
862}
863
864/**
865 * uwb_rsv_sched_update - schedule an update of the DRP IEs
866 * @rc: the radio controller.
867 *
868 * To improve performance and ensure correctness with [ECMA-368] the
869 * number of SET-DRP-IE commands that are done are limited.
870 *
871 * DRP IEs update come from two sources: DRP events from the hardware
872 * which all occur at the beginning of the superframe ('syncronous'
873 * events) and reservation establishment/termination requests from
874 * PALs or timers ('asynchronous' events).
875 *
876 * A delayed work ensures that all the synchronous events result in
877 * one SET-DRP-IE command.
878 *
879 * Additional logic (the set_drp_ie_pending and rsv_updated_postponed
880 * flags) will prevent an asynchrous event starting a SET-DRP-IE
881 * command if one is currently awaiting a response.
882 *
883 * FIXME: this does leave a window where an asynchrous event can delay
884 * the SET-DRP-IE for a synchronous event by one superframe.
885 */
David Vrabel8cc13a02008-09-17 16:34:09 +0100886void uwb_rsv_sched_update(struct uwb_rc *rc)
887{
tpugliese31388872013-05-20 15:09:05 -0500888 spin_lock_irq(&rc->rsvs_lock);
Stefano Panella5b377172008-12-12 13:00:06 +0000889 if (!delayed_work_pending(&rc->rsv_update_work)) {
890 if (rc->set_drp_ie_pending > 0) {
891 rc->set_drp_ie_pending++;
892 goto unlock;
893 }
894 uwb_rsv_queue_update(rc);
895 }
896unlock:
tpugliese31388872013-05-20 15:09:05 -0500897 spin_unlock_irq(&rc->rsvs_lock);
David Vrabel8cc13a02008-09-17 16:34:09 +0100898}
899
900/*
901 * Update DRP IEs and, if necessary, the DRP Availability IE and send
902 * the updated IEs to the radio controller.
903 */
904static void uwb_rsv_update_work(struct work_struct *work)
905{
Stefano Panella5b377172008-12-12 13:00:06 +0000906 struct uwb_rc *rc = container_of(work, struct uwb_rc,
907 rsv_update_work.work);
David Vrabel8cc13a02008-09-17 16:34:09 +0100908 bool ie_updated;
909
910 mutex_lock(&rc->rsvs_mutex);
911
912 ie_updated = uwb_rsv_update_all(rc);
913
914 if (!rc->drp_avail.ie_valid) {
915 uwb_drp_avail_ie_update(rc);
916 ie_updated = true;
917 }
918
Stefano Panella5b377172008-12-12 13:00:06 +0000919 if (ie_updated && (rc->set_drp_ie_pending == 0))
David Vrabel8cc13a02008-09-17 16:34:09 +0100920 uwb_rc_send_all_drp_ie(rc);
921
922 mutex_unlock(&rc->rsvs_mutex);
923}
924
Stefano Panella5b377172008-12-12 13:00:06 +0000925static void uwb_rsv_alien_bp_work(struct work_struct *work)
926{
927 struct uwb_rc *rc = container_of(work, struct uwb_rc,
928 rsv_alien_bp_work.work);
929 struct uwb_rsv *rsv;
930
931 mutex_lock(&rc->rsvs_mutex);
Thomas Pugliese67d0fb22013-12-20 11:54:08 -0600932
Stefano Panella5b377172008-12-12 13:00:06 +0000933 list_for_each_entry(rsv, &rc->reservations, rc_node) {
934 if (rsv->type != UWB_DRP_TYPE_ALIEN_BP) {
Thomas Pugliesebf359df2013-12-20 11:54:09 -0600935 uwb_rsv_callback(rsv);
Stefano Panella5b377172008-12-12 13:00:06 +0000936 }
937 }
938
939 mutex_unlock(&rc->rsvs_mutex);
940}
941
David Vrabel8cc13a02008-09-17 16:34:09 +0100942static void uwb_rsv_timer(unsigned long arg)
943{
944 struct uwb_rsv *rsv = (struct uwb_rsv *)arg;
945
Stefano Panella5b377172008-12-12 13:00:06 +0000946 queue_work(rsv->rc->rsv_workq, &rsv->handle_timeout_work);
David Vrabel8cc13a02008-09-17 16:34:09 +0100947}
948
David Vrabel307ba6d2008-11-07 17:37:33 +0000949/**
950 * uwb_rsv_remove_all - remove all reservations
951 * @rc: the radio controller
952 *
953 * A DRP IE update is not done.
954 */
955void uwb_rsv_remove_all(struct uwb_rc *rc)
956{
957 struct uwb_rsv *rsv, *t;
958
959 mutex_lock(&rc->rsvs_mutex);
960 list_for_each_entry_safe(rsv, t, &rc->reservations, rc_node) {
David Vrabel2226b1c2009-01-06 17:55:32 +0000961 if (rsv->state != UWB_RSV_STATE_NONE)
962 uwb_rsv_set_state(rsv, UWB_RSV_STATE_NONE);
963 del_timer_sync(&rsv->timer);
David Vrabel307ba6d2008-11-07 17:37:33 +0000964 }
Stefano Panella5b377172008-12-12 13:00:06 +0000965 /* Cancel any postponed update. */
966 rc->set_drp_ie_pending = 0;
David Vrabel307ba6d2008-11-07 17:37:33 +0000967 mutex_unlock(&rc->rsvs_mutex);
968
Stefano Panella5b377172008-12-12 13:00:06 +0000969 cancel_delayed_work_sync(&rc->rsv_update_work);
David Vrabel2226b1c2009-01-06 17:55:32 +0000970 flush_workqueue(rc->rsv_workq);
971
972 mutex_lock(&rc->rsvs_mutex);
973 list_for_each_entry_safe(rsv, t, &rc->reservations, rc_node) {
974 uwb_rsv_remove(rsv);
975 }
976 mutex_unlock(&rc->rsvs_mutex);
David Vrabel307ba6d2008-11-07 17:37:33 +0000977}
978
David Vrabel8cc13a02008-09-17 16:34:09 +0100979void uwb_rsv_init(struct uwb_rc *rc)
980{
981 INIT_LIST_HEAD(&rc->reservations);
Stefano Panella5b377172008-12-12 13:00:06 +0000982 INIT_LIST_HEAD(&rc->cnflt_alien_list);
David Vrabel8cc13a02008-09-17 16:34:09 +0100983 mutex_init(&rc->rsvs_mutex);
Stefano Panella5b377172008-12-12 13:00:06 +0000984 spin_lock_init(&rc->rsvs_lock);
985 INIT_DELAYED_WORK(&rc->rsv_update_work, uwb_rsv_update_work);
986 INIT_DELAYED_WORK(&rc->rsv_alien_bp_work, uwb_rsv_alien_bp_work);
987 rc->bow.can_reserve_extra_mases = true;
988 rc->bow.total_expired = 0;
989 rc->bow.window = UWB_DRP_BACKOFF_WIN_MIN >> 1;
Muhammad Falak R Wania895dc02015-10-21 00:00:37 +0530990 setup_timer(&rc->bow.timer, uwb_rsv_backoff_win_timer,
991 (unsigned long)&rc->bow);
David Vrabel8cc13a02008-09-17 16:34:09 +0100992
993 bitmap_complement(rc->uwb_dev.streams, rc->uwb_dev.streams, UWB_NUM_STREAMS);
994}
995
996int uwb_rsv_setup(struct uwb_rc *rc)
997{
998 char name[16];
999
1000 snprintf(name, sizeof(name), "%s_rsvd", dev_name(&rc->uwb_dev.dev));
1001 rc->rsv_workq = create_singlethread_workqueue(name);
1002 if (rc->rsv_workq == NULL)
1003 return -ENOMEM;
1004
1005 return 0;
1006}
1007
1008void uwb_rsv_cleanup(struct uwb_rc *rc)
1009{
David Vrabel307ba6d2008-11-07 17:37:33 +00001010 uwb_rsv_remove_all(rc);
David Vrabel8cc13a02008-09-17 16:34:09 +01001011 destroy_workqueue(rc->rsv_workq);
1012}