David Vrabel | 8cc13a0 | 2008-09-17 16:34:09 +0100 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 18 | #include <linux/version.h> |
| 19 | #include <linux/kernel.h> |
| 20 | #include <linux/uwb.h> |
| 21 | |
| 22 | #include "uwb-internal.h" |
| 23 | |
| 24 | static void uwb_rsv_timer(unsigned long arg); |
| 25 | |
| 26 | static const char *rsv_states[] = { |
| 27 | [UWB_RSV_STATE_NONE] = "none", |
| 28 | [UWB_RSV_STATE_O_INITIATED] = "initiated", |
| 29 | [UWB_RSV_STATE_O_PENDING] = "pending", |
| 30 | [UWB_RSV_STATE_O_MODIFIED] = "modified", |
| 31 | [UWB_RSV_STATE_O_ESTABLISHED] = "established", |
| 32 | [UWB_RSV_STATE_T_ACCEPTED] = "accepted", |
| 33 | [UWB_RSV_STATE_T_DENIED] = "denied", |
| 34 | [UWB_RSV_STATE_T_PENDING] = "pending", |
| 35 | }; |
| 36 | |
| 37 | static const char *rsv_types[] = { |
| 38 | [UWB_DRP_TYPE_ALIEN_BP] = "alien-bp", |
| 39 | [UWB_DRP_TYPE_HARD] = "hard", |
| 40 | [UWB_DRP_TYPE_SOFT] = "soft", |
| 41 | [UWB_DRP_TYPE_PRIVATE] = "private", |
| 42 | [UWB_DRP_TYPE_PCA] = "pca", |
| 43 | }; |
| 44 | |
| 45 | /** |
| 46 | * uwb_rsv_state_str - return a string for a reservation state |
| 47 | * @state: the reservation state. |
| 48 | */ |
| 49 | const char *uwb_rsv_state_str(enum uwb_rsv_state state) |
| 50 | { |
| 51 | if (state < UWB_RSV_STATE_NONE || state >= UWB_RSV_STATE_LAST) |
| 52 | return "unknown"; |
| 53 | return rsv_states[state]; |
| 54 | } |
| 55 | EXPORT_SYMBOL_GPL(uwb_rsv_state_str); |
| 56 | |
| 57 | /** |
| 58 | * uwb_rsv_type_str - return a string for a reservation type |
| 59 | * @type: the reservation type |
| 60 | */ |
| 61 | const char *uwb_rsv_type_str(enum uwb_drp_type type) |
| 62 | { |
| 63 | if (type < UWB_DRP_TYPE_ALIEN_BP || type > UWB_DRP_TYPE_PCA) |
| 64 | return "invalid"; |
| 65 | return rsv_types[type]; |
| 66 | } |
| 67 | EXPORT_SYMBOL_GPL(uwb_rsv_type_str); |
| 68 | |
| 69 | static void uwb_rsv_dump(struct uwb_rsv *rsv) |
| 70 | { |
| 71 | struct device *dev = &rsv->rc->uwb_dev.dev; |
| 72 | struct uwb_dev_addr devaddr; |
| 73 | char owner[UWB_ADDR_STRSIZE], target[UWB_ADDR_STRSIZE]; |
| 74 | |
| 75 | uwb_dev_addr_print(owner, sizeof(owner), &rsv->owner->dev_addr); |
| 76 | if (rsv->target.type == UWB_RSV_TARGET_DEV) |
| 77 | devaddr = rsv->target.dev->dev_addr; |
| 78 | else |
| 79 | devaddr = rsv->target.devaddr; |
| 80 | uwb_dev_addr_print(target, sizeof(target), &devaddr); |
| 81 | |
| 82 | dev_dbg(dev, "rsv %s -> %s: %s\n", owner, target, uwb_rsv_state_str(rsv->state)); |
| 83 | } |
| 84 | |
| 85 | /* |
| 86 | * Get a free stream index for a reservation. |
| 87 | * |
| 88 | * If the target is a DevAddr (e.g., a WUSB cluster reservation) then |
| 89 | * the stream is allocated from a pool of per-RC stream indexes, |
| 90 | * otherwise a unique stream index for the target is selected. |
| 91 | */ |
| 92 | static int uwb_rsv_get_stream(struct uwb_rsv *rsv) |
| 93 | { |
| 94 | struct uwb_rc *rc = rsv->rc; |
| 95 | unsigned long *streams_bm; |
| 96 | int stream; |
| 97 | |
| 98 | switch (rsv->target.type) { |
| 99 | case UWB_RSV_TARGET_DEV: |
| 100 | streams_bm = rsv->target.dev->streams; |
| 101 | break; |
| 102 | case UWB_RSV_TARGET_DEVADDR: |
| 103 | streams_bm = rc->uwb_dev.streams; |
| 104 | break; |
| 105 | default: |
| 106 | return -EINVAL; |
| 107 | } |
| 108 | |
| 109 | stream = find_first_zero_bit(streams_bm, UWB_NUM_STREAMS); |
| 110 | if (stream >= UWB_NUM_STREAMS) |
| 111 | return -EBUSY; |
| 112 | |
| 113 | rsv->stream = stream; |
| 114 | set_bit(stream, streams_bm); |
| 115 | |
| 116 | return 0; |
| 117 | } |
| 118 | |
| 119 | static void uwb_rsv_put_stream(struct uwb_rsv *rsv) |
| 120 | { |
| 121 | struct uwb_rc *rc = rsv->rc; |
| 122 | unsigned long *streams_bm; |
| 123 | |
| 124 | switch (rsv->target.type) { |
| 125 | case UWB_RSV_TARGET_DEV: |
| 126 | streams_bm = rsv->target.dev->streams; |
| 127 | break; |
| 128 | case UWB_RSV_TARGET_DEVADDR: |
| 129 | streams_bm = rc->uwb_dev.streams; |
| 130 | break; |
| 131 | default: |
| 132 | return; |
| 133 | } |
| 134 | |
| 135 | clear_bit(rsv->stream, streams_bm); |
| 136 | } |
| 137 | |
| 138 | /* |
| 139 | * Generate a MAS allocation with a single row component. |
| 140 | */ |
| 141 | static void uwb_rsv_gen_alloc_row(struct uwb_mas_bm *mas, |
| 142 | int first_mas, int mas_per_zone, |
| 143 | int zs, int ze) |
| 144 | { |
| 145 | struct uwb_mas_bm col; |
| 146 | int z; |
| 147 | |
| 148 | bitmap_zero(mas->bm, UWB_NUM_MAS); |
| 149 | bitmap_zero(col.bm, UWB_NUM_MAS); |
| 150 | bitmap_fill(col.bm, mas_per_zone); |
| 151 | bitmap_shift_left(col.bm, col.bm, first_mas + zs * UWB_MAS_PER_ZONE, UWB_NUM_MAS); |
| 152 | |
| 153 | for (z = zs; z <= ze; z++) { |
| 154 | bitmap_or(mas->bm, mas->bm, col.bm, UWB_NUM_MAS); |
| 155 | bitmap_shift_left(col.bm, col.bm, UWB_MAS_PER_ZONE, UWB_NUM_MAS); |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | /* |
| 160 | * Allocate some MAS for this reservation based on current local |
| 161 | * availability, the reservation parameters (max_mas, min_mas, |
| 162 | * sparsity), and the WiMedia rules for MAS allocations. |
| 163 | * |
| 164 | * Returns -EBUSY is insufficient free MAS are available. |
| 165 | * |
| 166 | * FIXME: to simplify this, only safe reservations with a single row |
| 167 | * component in zones 1 to 15 are tried (zone 0 is skipped to avoid |
| 168 | * problems with the MAS reserved for the BP). |
| 169 | * |
| 170 | * [ECMA-368] section B.2. |
| 171 | */ |
| 172 | static int uwb_rsv_alloc_mas(struct uwb_rsv *rsv) |
| 173 | { |
| 174 | static const int safe_mas_in_row[UWB_NUM_ZONES] = { |
| 175 | 8, 7, 6, 5, 4, 4, 4, 4, 4, 4, 4, 4, 4, 3, 2, 1, |
| 176 | }; |
| 177 | int n, r; |
| 178 | struct uwb_mas_bm mas; |
| 179 | bool found = false; |
| 180 | |
| 181 | /* |
| 182 | * Search all valid safe allocations until either: too few MAS |
| 183 | * are available; or the smallest allocation with sufficient |
| 184 | * MAS is found. |
| 185 | * |
| 186 | * The top of the zones are preferred, so space for larger |
| 187 | * allocations is available in the bottom of the zone (e.g., a |
| 188 | * 15 MAS allocation should start in row 14 leaving space for |
| 189 | * a 120 MAS allocation at row 0). |
| 190 | */ |
| 191 | for (n = safe_mas_in_row[0]; n >= 1; n--) { |
| 192 | int num_mas; |
| 193 | |
| 194 | num_mas = n * (UWB_NUM_ZONES - 1); |
| 195 | if (num_mas < rsv->min_mas) |
| 196 | break; |
| 197 | if (found && num_mas < rsv->max_mas) |
| 198 | break; |
| 199 | |
| 200 | for (r = UWB_MAS_PER_ZONE-1; r >= 0; r--) { |
| 201 | if (safe_mas_in_row[r] < n) |
| 202 | continue; |
| 203 | uwb_rsv_gen_alloc_row(&mas, r, n, 1, UWB_NUM_ZONES); |
| 204 | if (uwb_drp_avail_reserve_pending(rsv->rc, &mas) == 0) { |
| 205 | found = true; |
| 206 | break; |
| 207 | } |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | if (!found) |
| 212 | return -EBUSY; |
| 213 | |
| 214 | bitmap_copy(rsv->mas.bm, mas.bm, UWB_NUM_MAS); |
| 215 | return 0; |
| 216 | } |
| 217 | |
| 218 | static void uwb_rsv_stroke_timer(struct uwb_rsv *rsv) |
| 219 | { |
| 220 | int sframes = UWB_MAX_LOST_BEACONS; |
| 221 | |
| 222 | /* |
| 223 | * Multicast reservations can become established within 1 |
| 224 | * super frame and should not be terminated if no response is |
| 225 | * received. |
| 226 | */ |
| 227 | if (rsv->is_multicast) { |
| 228 | if (rsv->state == UWB_RSV_STATE_O_INITIATED) |
| 229 | sframes = 1; |
| 230 | if (rsv->state == UWB_RSV_STATE_O_ESTABLISHED) |
| 231 | sframes = 0; |
| 232 | } |
| 233 | |
| 234 | rsv->expired = false; |
| 235 | if (sframes > 0) { |
| 236 | /* |
| 237 | * Add an additional 2 superframes to account for the |
| 238 | * time to send the SET DRP IE command. |
| 239 | */ |
| 240 | unsigned timeout_us = (sframes + 2) * UWB_SUPERFRAME_LENGTH_US; |
| 241 | mod_timer(&rsv->timer, jiffies + usecs_to_jiffies(timeout_us)); |
| 242 | } else |
| 243 | del_timer(&rsv->timer); |
| 244 | } |
| 245 | |
| 246 | /* |
| 247 | * Update a reservations state, and schedule an update of the |
| 248 | * transmitted DRP IEs. |
| 249 | */ |
| 250 | static void uwb_rsv_state_update(struct uwb_rsv *rsv, |
| 251 | enum uwb_rsv_state new_state) |
| 252 | { |
| 253 | rsv->state = new_state; |
| 254 | rsv->ie_valid = false; |
| 255 | |
| 256 | uwb_rsv_dump(rsv); |
| 257 | |
| 258 | uwb_rsv_stroke_timer(rsv); |
| 259 | uwb_rsv_sched_update(rsv->rc); |
| 260 | } |
| 261 | |
| 262 | static void uwb_rsv_callback(struct uwb_rsv *rsv) |
| 263 | { |
| 264 | if (rsv->callback) |
| 265 | rsv->callback(rsv); |
| 266 | } |
| 267 | |
| 268 | void uwb_rsv_set_state(struct uwb_rsv *rsv, enum uwb_rsv_state new_state) |
| 269 | { |
| 270 | if (rsv->state == new_state) { |
| 271 | switch (rsv->state) { |
| 272 | case UWB_RSV_STATE_O_ESTABLISHED: |
| 273 | case UWB_RSV_STATE_T_ACCEPTED: |
| 274 | case UWB_RSV_STATE_NONE: |
| 275 | uwb_rsv_stroke_timer(rsv); |
| 276 | break; |
| 277 | default: |
| 278 | /* Expecting a state transition so leave timer |
| 279 | as-is. */ |
| 280 | break; |
| 281 | } |
| 282 | return; |
| 283 | } |
| 284 | |
| 285 | switch (new_state) { |
| 286 | case UWB_RSV_STATE_NONE: |
| 287 | uwb_drp_avail_release(rsv->rc, &rsv->mas); |
| 288 | uwb_rsv_put_stream(rsv); |
| 289 | uwb_rsv_state_update(rsv, UWB_RSV_STATE_NONE); |
| 290 | uwb_rsv_callback(rsv); |
| 291 | break; |
| 292 | case UWB_RSV_STATE_O_INITIATED: |
| 293 | uwb_rsv_state_update(rsv, UWB_RSV_STATE_O_INITIATED); |
| 294 | break; |
| 295 | case UWB_RSV_STATE_O_PENDING: |
| 296 | uwb_rsv_state_update(rsv, UWB_RSV_STATE_O_PENDING); |
| 297 | break; |
| 298 | case UWB_RSV_STATE_O_ESTABLISHED: |
| 299 | uwb_drp_avail_reserve(rsv->rc, &rsv->mas); |
| 300 | uwb_rsv_state_update(rsv, UWB_RSV_STATE_O_ESTABLISHED); |
| 301 | uwb_rsv_callback(rsv); |
| 302 | break; |
| 303 | case UWB_RSV_STATE_T_ACCEPTED: |
| 304 | uwb_drp_avail_reserve(rsv->rc, &rsv->mas); |
| 305 | uwb_rsv_state_update(rsv, UWB_RSV_STATE_T_ACCEPTED); |
| 306 | uwb_rsv_callback(rsv); |
| 307 | break; |
| 308 | case UWB_RSV_STATE_T_DENIED: |
| 309 | uwb_rsv_state_update(rsv, UWB_RSV_STATE_T_DENIED); |
| 310 | break; |
| 311 | default: |
| 312 | dev_err(&rsv->rc->uwb_dev.dev, "unhandled state: %s (%d)\n", |
| 313 | uwb_rsv_state_str(new_state), new_state); |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | static struct uwb_rsv *uwb_rsv_alloc(struct uwb_rc *rc) |
| 318 | { |
| 319 | struct uwb_rsv *rsv; |
| 320 | |
| 321 | rsv = kzalloc(sizeof(struct uwb_rsv), GFP_KERNEL); |
| 322 | if (!rsv) |
| 323 | return NULL; |
| 324 | |
| 325 | INIT_LIST_HEAD(&rsv->rc_node); |
| 326 | INIT_LIST_HEAD(&rsv->pal_node); |
| 327 | init_timer(&rsv->timer); |
| 328 | rsv->timer.function = uwb_rsv_timer; |
| 329 | rsv->timer.data = (unsigned long)rsv; |
| 330 | |
| 331 | rsv->rc = rc; |
| 332 | |
| 333 | return rsv; |
| 334 | } |
| 335 | |
| 336 | static void uwb_rsv_free(struct uwb_rsv *rsv) |
| 337 | { |
| 338 | uwb_dev_put(rsv->owner); |
| 339 | if (rsv->target.type == UWB_RSV_TARGET_DEV) |
| 340 | uwb_dev_put(rsv->target.dev); |
| 341 | kfree(rsv); |
| 342 | } |
| 343 | |
| 344 | /** |
| 345 | * uwb_rsv_create - allocate and initialize a UWB reservation structure |
| 346 | * @rc: the radio controller |
| 347 | * @cb: callback to use when the reservation completes or terminates |
| 348 | * @pal_priv: data private to the PAL to be passed in the callback |
| 349 | * |
| 350 | * The callback is called when the state of the reservation changes from: |
| 351 | * |
| 352 | * - pending to accepted |
| 353 | * - pending to denined |
| 354 | * - accepted to terminated |
| 355 | * - pending to terminated |
| 356 | */ |
| 357 | struct uwb_rsv *uwb_rsv_create(struct uwb_rc *rc, uwb_rsv_cb_f cb, void *pal_priv) |
| 358 | { |
| 359 | struct uwb_rsv *rsv; |
| 360 | |
| 361 | rsv = uwb_rsv_alloc(rc); |
| 362 | if (!rsv) |
| 363 | return NULL; |
| 364 | |
| 365 | rsv->callback = cb; |
| 366 | rsv->pal_priv = pal_priv; |
| 367 | |
| 368 | return rsv; |
| 369 | } |
| 370 | EXPORT_SYMBOL_GPL(uwb_rsv_create); |
| 371 | |
| 372 | void uwb_rsv_remove(struct uwb_rsv *rsv) |
| 373 | { |
| 374 | if (rsv->state != UWB_RSV_STATE_NONE) |
| 375 | uwb_rsv_set_state(rsv, UWB_RSV_STATE_NONE); |
| 376 | del_timer_sync(&rsv->timer); |
| 377 | list_del(&rsv->rc_node); |
| 378 | uwb_rsv_free(rsv); |
| 379 | } |
| 380 | |
| 381 | /** |
| 382 | * uwb_rsv_destroy - free a UWB reservation structure |
| 383 | * @rsv: the reservation to free |
| 384 | * |
| 385 | * The reservation will be terminated if it is pending or established. |
| 386 | */ |
| 387 | void uwb_rsv_destroy(struct uwb_rsv *rsv) |
| 388 | { |
| 389 | struct uwb_rc *rc = rsv->rc; |
| 390 | |
| 391 | mutex_lock(&rc->rsvs_mutex); |
| 392 | uwb_rsv_remove(rsv); |
| 393 | mutex_unlock(&rc->rsvs_mutex); |
| 394 | } |
| 395 | EXPORT_SYMBOL_GPL(uwb_rsv_destroy); |
| 396 | |
| 397 | /** |
| 398 | * usb_rsv_establish - start a reservation establishment |
| 399 | * @rsv: the reservation |
| 400 | * |
| 401 | * The PAL should fill in @rsv's owner, target, type, max_mas, |
| 402 | * min_mas, sparsity and is_multicast fields. If the target is a |
| 403 | * uwb_dev it must be referenced. |
| 404 | * |
| 405 | * The reservation's callback will be called when the reservation is |
| 406 | * accepted, denied or times out. |
| 407 | */ |
| 408 | int uwb_rsv_establish(struct uwb_rsv *rsv) |
| 409 | { |
| 410 | struct uwb_rc *rc = rsv->rc; |
| 411 | int ret; |
| 412 | |
| 413 | mutex_lock(&rc->rsvs_mutex); |
| 414 | |
| 415 | ret = uwb_rsv_get_stream(rsv); |
| 416 | if (ret) |
| 417 | goto out; |
| 418 | |
| 419 | ret = uwb_rsv_alloc_mas(rsv); |
| 420 | if (ret) { |
| 421 | uwb_rsv_put_stream(rsv); |
| 422 | goto out; |
| 423 | } |
| 424 | |
| 425 | list_add_tail(&rsv->rc_node, &rc->reservations); |
| 426 | rsv->owner = &rc->uwb_dev; |
| 427 | uwb_dev_get(rsv->owner); |
| 428 | uwb_rsv_set_state(rsv, UWB_RSV_STATE_O_INITIATED); |
| 429 | out: |
| 430 | mutex_unlock(&rc->rsvs_mutex); |
| 431 | return ret; |
| 432 | } |
| 433 | EXPORT_SYMBOL_GPL(uwb_rsv_establish); |
| 434 | |
| 435 | /** |
| 436 | * uwb_rsv_modify - modify an already established reservation |
| 437 | * @rsv: the reservation to modify |
| 438 | * @max_mas: new maximum MAS to reserve |
| 439 | * @min_mas: new minimum MAS to reserve |
| 440 | * @sparsity: new sparsity to use |
| 441 | * |
| 442 | * FIXME: implement this once there are PALs that use it. |
| 443 | */ |
| 444 | int uwb_rsv_modify(struct uwb_rsv *rsv, int max_mas, int min_mas, int sparsity) |
| 445 | { |
| 446 | return -ENOSYS; |
| 447 | } |
| 448 | EXPORT_SYMBOL_GPL(uwb_rsv_modify); |
| 449 | |
| 450 | /** |
| 451 | * uwb_rsv_terminate - terminate an established reservation |
| 452 | * @rsv: the reservation to terminate |
| 453 | * |
| 454 | * A reservation is terminated by removing the DRP IE from the beacon, |
| 455 | * the other end will consider the reservation to be terminated when |
| 456 | * it does not see the DRP IE for at least mMaxLostBeacons. |
| 457 | * |
| 458 | * If applicable, the reference to the target uwb_dev will be released. |
| 459 | */ |
| 460 | void uwb_rsv_terminate(struct uwb_rsv *rsv) |
| 461 | { |
| 462 | struct uwb_rc *rc = rsv->rc; |
| 463 | |
| 464 | mutex_lock(&rc->rsvs_mutex); |
| 465 | |
| 466 | uwb_rsv_set_state(rsv, UWB_RSV_STATE_NONE); |
| 467 | |
| 468 | mutex_unlock(&rc->rsvs_mutex); |
| 469 | } |
| 470 | EXPORT_SYMBOL_GPL(uwb_rsv_terminate); |
| 471 | |
| 472 | /** |
| 473 | * uwb_rsv_accept - accept a new reservation from a peer |
| 474 | * @rsv: the reservation |
| 475 | * @cb: call back for reservation changes |
| 476 | * @pal_priv: data to be passed in the above call back |
| 477 | * |
| 478 | * Reservation requests from peers are denied unless a PAL accepts it |
| 479 | * by calling this function. |
| 480 | */ |
| 481 | void uwb_rsv_accept(struct uwb_rsv *rsv, uwb_rsv_cb_f cb, void *pal_priv) |
| 482 | { |
| 483 | rsv->callback = cb; |
| 484 | rsv->pal_priv = pal_priv; |
| 485 | rsv->state = UWB_RSV_STATE_T_ACCEPTED; |
| 486 | } |
| 487 | EXPORT_SYMBOL_GPL(uwb_rsv_accept); |
| 488 | |
| 489 | /* |
| 490 | * Is a received DRP IE for this reservation? |
| 491 | */ |
| 492 | static bool uwb_rsv_match(struct uwb_rsv *rsv, struct uwb_dev *src, |
| 493 | struct uwb_ie_drp *drp_ie) |
| 494 | { |
| 495 | struct uwb_dev_addr *rsv_src; |
| 496 | int stream; |
| 497 | |
| 498 | stream = uwb_ie_drp_stream_index(drp_ie); |
| 499 | |
| 500 | if (rsv->stream != stream) |
| 501 | return false; |
| 502 | |
| 503 | switch (rsv->target.type) { |
| 504 | case UWB_RSV_TARGET_DEVADDR: |
| 505 | return rsv->stream == stream; |
| 506 | case UWB_RSV_TARGET_DEV: |
| 507 | if (uwb_ie_drp_owner(drp_ie)) |
| 508 | rsv_src = &rsv->owner->dev_addr; |
| 509 | else |
| 510 | rsv_src = &rsv->target.dev->dev_addr; |
| 511 | return uwb_dev_addr_cmp(&src->dev_addr, rsv_src) == 0; |
| 512 | } |
| 513 | return false; |
| 514 | } |
| 515 | |
| 516 | static struct uwb_rsv *uwb_rsv_new_target(struct uwb_rc *rc, |
| 517 | struct uwb_dev *src, |
| 518 | struct uwb_ie_drp *drp_ie) |
| 519 | { |
| 520 | struct uwb_rsv *rsv; |
| 521 | struct uwb_pal *pal; |
| 522 | enum uwb_rsv_state state; |
| 523 | |
| 524 | rsv = uwb_rsv_alloc(rc); |
| 525 | if (!rsv) |
| 526 | return NULL; |
| 527 | |
| 528 | rsv->rc = rc; |
| 529 | rsv->owner = src; |
| 530 | uwb_dev_get(rsv->owner); |
| 531 | rsv->target.type = UWB_RSV_TARGET_DEV; |
| 532 | rsv->target.dev = &rc->uwb_dev; |
| 533 | rsv->type = uwb_ie_drp_type(drp_ie); |
| 534 | rsv->stream = uwb_ie_drp_stream_index(drp_ie); |
| 535 | set_bit(rsv->stream, rsv->owner->streams); |
| 536 | uwb_drp_ie_to_bm(&rsv->mas, drp_ie); |
| 537 | |
| 538 | /* |
| 539 | * See if any PALs are interested in this reservation. If not, |
| 540 | * deny the request. |
| 541 | */ |
| 542 | rsv->state = UWB_RSV_STATE_T_DENIED; |
| 543 | spin_lock(&rc->pal_lock); |
| 544 | list_for_each_entry(pal, &rc->pals, node) { |
| 545 | if (pal->new_rsv) |
| 546 | pal->new_rsv(rsv); |
| 547 | if (rsv->state == UWB_RSV_STATE_T_ACCEPTED) |
| 548 | break; |
| 549 | } |
| 550 | spin_unlock(&rc->pal_lock); |
| 551 | |
| 552 | list_add_tail(&rsv->rc_node, &rc->reservations); |
| 553 | state = rsv->state; |
| 554 | rsv->state = UWB_RSV_STATE_NONE; |
| 555 | uwb_rsv_set_state(rsv, state); |
| 556 | |
| 557 | return rsv; |
| 558 | } |
| 559 | |
| 560 | /** |
| 561 | * uwb_rsv_find - find a reservation for a received DRP IE. |
| 562 | * @rc: the radio controller |
| 563 | * @src: source of the DRP IE |
| 564 | * @drp_ie: the DRP IE |
| 565 | * |
| 566 | * If the reservation cannot be found and the DRP IE is from a peer |
| 567 | * attempting to establish a new reservation, create a new reservation |
| 568 | * and add it to the list. |
| 569 | */ |
| 570 | struct uwb_rsv *uwb_rsv_find(struct uwb_rc *rc, struct uwb_dev *src, |
| 571 | struct uwb_ie_drp *drp_ie) |
| 572 | { |
| 573 | struct uwb_rsv *rsv; |
| 574 | |
| 575 | list_for_each_entry(rsv, &rc->reservations, rc_node) { |
| 576 | if (uwb_rsv_match(rsv, src, drp_ie)) |
| 577 | return rsv; |
| 578 | } |
| 579 | |
| 580 | if (uwb_ie_drp_owner(drp_ie)) |
| 581 | return uwb_rsv_new_target(rc, src, drp_ie); |
| 582 | |
| 583 | return NULL; |
| 584 | } |
| 585 | |
| 586 | /* |
| 587 | * Go through all the reservations and check for timeouts and (if |
| 588 | * necessary) update their DRP IEs. |
| 589 | * |
| 590 | * FIXME: look at building the SET_DRP_IE command here rather than |
| 591 | * having to rescan the list in uwb_rc_send_all_drp_ie(). |
| 592 | */ |
| 593 | static bool uwb_rsv_update_all(struct uwb_rc *rc) |
| 594 | { |
| 595 | struct uwb_rsv *rsv, *t; |
| 596 | bool ie_updated = false; |
| 597 | |
| 598 | list_for_each_entry_safe(rsv, t, &rc->reservations, rc_node) { |
| 599 | if (rsv->expired) |
| 600 | uwb_drp_handle_timeout(rsv); |
| 601 | if (!rsv->ie_valid) { |
| 602 | uwb_drp_ie_update(rsv); |
| 603 | ie_updated = true; |
| 604 | } |
| 605 | } |
| 606 | |
| 607 | return ie_updated; |
| 608 | } |
| 609 | |
| 610 | void uwb_rsv_sched_update(struct uwb_rc *rc) |
| 611 | { |
| 612 | queue_work(rc->rsv_workq, &rc->rsv_update_work); |
| 613 | } |
| 614 | |
| 615 | /* |
| 616 | * Update DRP IEs and, if necessary, the DRP Availability IE and send |
| 617 | * the updated IEs to the radio controller. |
| 618 | */ |
| 619 | static void uwb_rsv_update_work(struct work_struct *work) |
| 620 | { |
| 621 | struct uwb_rc *rc = container_of(work, struct uwb_rc, rsv_update_work); |
| 622 | bool ie_updated; |
| 623 | |
| 624 | mutex_lock(&rc->rsvs_mutex); |
| 625 | |
| 626 | ie_updated = uwb_rsv_update_all(rc); |
| 627 | |
| 628 | if (!rc->drp_avail.ie_valid) { |
| 629 | uwb_drp_avail_ie_update(rc); |
| 630 | ie_updated = true; |
| 631 | } |
| 632 | |
| 633 | if (ie_updated) |
| 634 | uwb_rc_send_all_drp_ie(rc); |
| 635 | |
| 636 | mutex_unlock(&rc->rsvs_mutex); |
| 637 | } |
| 638 | |
| 639 | static void uwb_rsv_timer(unsigned long arg) |
| 640 | { |
| 641 | struct uwb_rsv *rsv = (struct uwb_rsv *)arg; |
| 642 | |
| 643 | rsv->expired = true; |
| 644 | uwb_rsv_sched_update(rsv->rc); |
| 645 | } |
| 646 | |
| 647 | void uwb_rsv_init(struct uwb_rc *rc) |
| 648 | { |
| 649 | INIT_LIST_HEAD(&rc->reservations); |
| 650 | mutex_init(&rc->rsvs_mutex); |
| 651 | INIT_WORK(&rc->rsv_update_work, uwb_rsv_update_work); |
| 652 | |
| 653 | bitmap_complement(rc->uwb_dev.streams, rc->uwb_dev.streams, UWB_NUM_STREAMS); |
| 654 | } |
| 655 | |
| 656 | int uwb_rsv_setup(struct uwb_rc *rc) |
| 657 | { |
| 658 | char name[16]; |
| 659 | |
| 660 | snprintf(name, sizeof(name), "%s_rsvd", dev_name(&rc->uwb_dev.dev)); |
| 661 | rc->rsv_workq = create_singlethread_workqueue(name); |
| 662 | if (rc->rsv_workq == NULL) |
| 663 | return -ENOMEM; |
| 664 | |
| 665 | return 0; |
| 666 | } |
| 667 | |
| 668 | void uwb_rsv_cleanup(struct uwb_rc *rc) |
| 669 | { |
| 670 | struct uwb_rsv *rsv, *t; |
| 671 | |
| 672 | mutex_lock(&rc->rsvs_mutex); |
| 673 | list_for_each_entry_safe(rsv, t, &rc->reservations, rc_node) { |
| 674 | uwb_rsv_remove(rsv); |
| 675 | } |
| 676 | mutex_unlock(&rc->rsvs_mutex); |
| 677 | |
| 678 | cancel_work_sync(&rc->rsv_update_work); |
| 679 | destroy_workqueue(rc->rsv_workq); |
| 680 | } |