blob: 4b856e00c8a06141439dbdf4065d7d102d874f8e [file] [log] [blame]
Holger Hans Peter Freyther621dc2f2013-10-19 20:11:46 +02001/* sba.cpp
2 *
3 * Copyright (C) 2012 Ivan Klyuchnikov
4 * Copyright (C) 2012 Andreas Eversberg <jolly@eversberg.eu>
5 * Copyright (C) 2013 by Holger Hans Peter Freyther
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 */
21
Holger Hans Peter Freythercedf8902013-10-19 20:47:12 +020022#include <sba.h>
Holger Hans Peter Freyther621dc2f2013-10-19 20:11:46 +020023#include <gprs_rlcmac.h>
24#include <gprs_debug.h>
25#include <bts.h>
26
27extern "C" {
28#include <osmocom/core/talloc.h>
29}
30
31#include <errno.h>
32
33extern void *tall_pcu_ctx;
34
35/* starting time for assigning single slot
36 * This offset must be a multiple of 13. */
37#define AGCH_START_OFFSET 52
38
Holger Hans Peter Freythercedf8902013-10-19 20:47:12 +020039SBAController::SBAController(BTS &bts)
40 : m_bts(bts)
41{
42 INIT_LLIST_HEAD(&m_sbas);
43}
Holger Hans Peter Freyther621dc2f2013-10-19 20:11:46 +020044
Holger Hans Peter Freythercedf8902013-10-19 20:47:12 +020045int SBAController::alloc(
Holger Hans Peter Freyther621dc2f2013-10-19 20:11:46 +020046 uint8_t *_trx, uint8_t *_ts, uint32_t *_fn, uint8_t ta)
47{
48
49 struct gprs_rlcmac_pdch *pdch;
50 struct gprs_rlcmac_sba *sba;
51 uint8_t trx, ts;
52 uint32_t fn;
53
54 sba = talloc_zero(tall_pcu_ctx, struct gprs_rlcmac_sba);
55 if (!sba)
56 return -ENOMEM;
57
58 for (trx = 0; trx < 8; trx++) {
59 for (ts = 0; ts < 8; ts++) {
Holger Hans Peter Freythercedf8902013-10-19 20:47:12 +020060 pdch = &m_bts.bts_data()->trx[trx].pdch[ts];
Holger Hans Peter Freyther621dc2f2013-10-19 20:11:46 +020061 if (!pdch->is_enabled())
62 continue;
63 break;
64 }
65 if (ts < 8)
66 break;
67 }
68 if (trx == 8) {
69 LOGP(DRLCMAC, LOGL_NOTICE, "No PDCH available.\n");
70 talloc_free(sba);
71 return -EINVAL;
72 }
73
74 fn = (pdch->last_rts_fn + AGCH_START_OFFSET) % 2715648;
75
76 sba->trx = trx;
77 sba->ts = ts;
78 sba->fn = fn;
79 sba->ta = ta;
80
Holger Hans Peter Freythercedf8902013-10-19 20:47:12 +020081 llist_add(&sba->list, &m_sbas);
Holger Hans Peter Freyther621dc2f2013-10-19 20:11:46 +020082
83 *_trx = trx;
84 *_ts = ts;
85 *_fn = fn;
86 return 0;
87}
88
Holger Hans Peter Freythercedf8902013-10-19 20:47:12 +020089gprs_rlcmac_sba *SBAController::find(uint8_t trx, uint8_t ts, uint32_t fn)
Holger Hans Peter Freyther621dc2f2013-10-19 20:11:46 +020090{
91 struct gprs_rlcmac_sba *sba;
92
Holger Hans Peter Freythercedf8902013-10-19 20:47:12 +020093 llist_for_each_entry(sba, &m_sbas, list) {
Holger Hans Peter Freyther621dc2f2013-10-19 20:11:46 +020094 if (sba->trx == trx && sba->ts == ts && sba->fn == fn)
95 return sba;
96 }
97
98 return NULL;
99}
100
Holger Hans Peter Freythercedf8902013-10-19 20:47:12 +0200101uint32_t SBAController::sched(uint8_t trx, uint8_t ts, uint32_t fn, uint8_t block_nr)
Holger Hans Peter Freyther621dc2f2013-10-19 20:11:46 +0200102{
103 uint32_t sba_fn;
104 struct gprs_rlcmac_sba *sba;
105
106 /* check special TBF for events */
107 sba_fn = fn + 4;
108 if ((block_nr % 3) == 2)
109 sba_fn ++;
110 sba_fn = sba_fn % 2715648;
Holger Hans Peter Freythercedf8902013-10-19 20:47:12 +0200111 sba = find(trx, ts, sba_fn);
Holger Hans Peter Freyther621dc2f2013-10-19 20:11:46 +0200112 if (sba)
113 return sba_fn;
114
115 return 0xffffffff;
116}
117
Holger Hans Peter Freythercedf8902013-10-19 20:47:12 +0200118int SBAController::timeout(struct gprs_rlcmac_sba *sba)
Holger Hans Peter Freyther621dc2f2013-10-19 20:11:46 +0200119{
120 LOGP(DRLCMAC, LOGL_NOTICE, "Poll timeout for SBA\n");
121 llist_del(&sba->list);
122 talloc_free(sba);
123
124 return 0;
125}
126
Holger Hans Peter Freythercedf8902013-10-19 20:47:12 +0200127void SBAController::free_resources(uint8_t trx, uint8_t ts)
128{
129 struct gprs_rlcmac_sba *sba, *sba2;
130
131 llist_for_each_entry_safe(sba, sba2, &m_sbas, list) {
132 if (sba->trx == trx && sba->ts == ts) {
133 llist_del(&sba->list);
134 talloc_free(sba);
135 }
136 }
137}