blob: 6aeeb7cbd79d00734af1d24549ece96c1c2ebe47 [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;
Jacob Erlbeck506f1562015-07-02 13:19:49 +020051 int8_t trx, ts;
Holger Hans Peter Freyther621dc2f2013-10-19 20:11:46 +020052 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++) {
Jacob Erlbeck506f1562015-07-02 13:19:49 +020059 for (ts = 7; ts >= 0; 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 }
Jacob Erlbeck1139ec12015-07-15 14:17:19 +020065 if (ts >= 0)
Holger Hans Peter Freyther621dc2f2013-10-19 20:11:46 +020066 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
Holger Hans Peter Freyther09ef27a2013-10-20 16:37:05 +020076 sba->trx_no = trx;
77 sba->ts_no = ts;
Holger Hans Peter Freyther621dc2f2013-10-19 20:11:46 +020078 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 Freyther93e048f2013-10-27 10:00:47 +010082 m_bts.sba_allocated();
Holger Hans Peter Freyther621dc2f2013-10-19 20:11:46 +020083
84 *_trx = trx;
85 *_ts = ts;
86 *_fn = fn;
87 return 0;
88}
89
Holger Hans Peter Freythercedf8902013-10-19 20:47:12 +020090gprs_rlcmac_sba *SBAController::find(uint8_t trx, uint8_t ts, uint32_t fn)
Holger Hans Peter Freyther621dc2f2013-10-19 20:11:46 +020091{
92 struct gprs_rlcmac_sba *sba;
93
Holger Hans Peter Freythercedf8902013-10-19 20:47:12 +020094 llist_for_each_entry(sba, &m_sbas, list) {
Holger Hans Peter Freyther09ef27a2013-10-20 16:37:05 +020095 if (sba->trx_no == trx && sba->ts_no == ts && sba->fn == fn)
Holger Hans Peter Freyther621dc2f2013-10-19 20:11:46 +020096 return sba;
97 }
98
99 return NULL;
100}
101
Holger Hans Peter Freytherdea63b92013-10-26 18:56:43 +0200102gprs_rlcmac_sba *SBAController::find(const gprs_rlcmac_pdch *pdch, uint32_t fn)
103{
104 return find(pdch->trx_no(), pdch->ts_no, fn);
105}
106
Holger Hans Peter Freythercedf8902013-10-19 20:47:12 +0200107uint32_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 +0200108{
109 uint32_t sba_fn;
110 struct gprs_rlcmac_sba *sba;
111
112 /* check special TBF for events */
113 sba_fn = fn + 4;
114 if ((block_nr % 3) == 2)
115 sba_fn ++;
116 sba_fn = sba_fn % 2715648;
Holger Hans Peter Freythercedf8902013-10-19 20:47:12 +0200117 sba = find(trx, ts, sba_fn);
Holger Hans Peter Freyther621dc2f2013-10-19 20:11:46 +0200118 if (sba)
119 return sba_fn;
120
121 return 0xffffffff;
122}
123
Holger Hans Peter Freythercedf8902013-10-19 20:47:12 +0200124int SBAController::timeout(struct gprs_rlcmac_sba *sba)
Holger Hans Peter Freyther621dc2f2013-10-19 20:11:46 +0200125{
126 LOGP(DRLCMAC, LOGL_NOTICE, "Poll timeout for SBA\n");
Holger Hans Peter Freyther93e048f2013-10-27 10:00:47 +0100127 m_bts.sba_timedout();
Holger Hans Peter Freytherb98dd9e2013-10-27 10:20:37 +0100128 free_sba(sba);
Holger Hans Peter Freyther621dc2f2013-10-19 20:11:46 +0200129 return 0;
130}
131
Holger Hans Peter Freytherb98dd9e2013-10-27 10:20:37 +0100132void SBAController::free_sba(gprs_rlcmac_sba *sba)
133{
134 m_bts.sba_freed();
135 llist_del(&sba->list);
136 talloc_free(sba);
137}
138
Holger Hans Peter Freyther09ef27a2013-10-20 16:37:05 +0200139void SBAController::free_resources(struct gprs_rlcmac_pdch *pdch)
Holger Hans Peter Freythercedf8902013-10-19 20:47:12 +0200140{
141 struct gprs_rlcmac_sba *sba, *sba2;
Holger Hans Peter Freyther09ef27a2013-10-20 16:37:05 +0200142 const uint8_t trx_no = pdch->trx->trx_no;
143 const uint8_t ts_no = pdch->ts_no;
Holger Hans Peter Freythercedf8902013-10-19 20:47:12 +0200144
145 llist_for_each_entry_safe(sba, sba2, &m_sbas, list) {
Holger Hans Peter Freytherb98dd9e2013-10-27 10:20:37 +0100146 if (sba->trx_no == trx_no && sba->ts_no == ts_no)
147 free_sba(sba);
Holger Hans Peter Freythercedf8902013-10-19 20:47:12 +0200148 }
149}