blob: d87a9a3be7cbaf655cc7337c5d1d4958f03e2457 [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
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 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 Freyther09ef27a2013-10-20 16:37:05 +020094 if (sba->trx_no == trx && sba->ts_no == ts && sba->fn == fn)
Holger Hans Peter Freyther621dc2f2013-10-19 20:11:46 +020095 return sba;
96 }
97
98 return NULL;
99}
100
Holger Hans Peter Freytherdea63b92013-10-26 18:56:43 +0200101gprs_rlcmac_sba *SBAController::find(const gprs_rlcmac_pdch *pdch, uint32_t fn)
102{
103 return find(pdch->trx_no(), pdch->ts_no, fn);
104}
105
Holger Hans Peter Freythercedf8902013-10-19 20:47:12 +0200106uint32_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 +0200107{
108 uint32_t sba_fn;
109 struct gprs_rlcmac_sba *sba;
110
111 /* check special TBF for events */
112 sba_fn = fn + 4;
113 if ((block_nr % 3) == 2)
114 sba_fn ++;
115 sba_fn = sba_fn % 2715648;
Holger Hans Peter Freythercedf8902013-10-19 20:47:12 +0200116 sba = find(trx, ts, sba_fn);
Holger Hans Peter Freyther621dc2f2013-10-19 20:11:46 +0200117 if (sba)
118 return sba_fn;
119
120 return 0xffffffff;
121}
122
Holger Hans Peter Freythercedf8902013-10-19 20:47:12 +0200123int SBAController::timeout(struct gprs_rlcmac_sba *sba)
Holger Hans Peter Freyther621dc2f2013-10-19 20:11:46 +0200124{
125 LOGP(DRLCMAC, LOGL_NOTICE, "Poll timeout for SBA\n");
126 llist_del(&sba->list);
127 talloc_free(sba);
128
129 return 0;
130}
131
Holger Hans Peter Freyther09ef27a2013-10-20 16:37:05 +0200132void SBAController::free_resources(struct gprs_rlcmac_pdch *pdch)
Holger Hans Peter Freythercedf8902013-10-19 20:47:12 +0200133{
134 struct gprs_rlcmac_sba *sba, *sba2;
Holger Hans Peter Freyther09ef27a2013-10-20 16:37:05 +0200135 const uint8_t trx_no = pdch->trx->trx_no;
136 const uint8_t ts_no = pdch->ts_no;
Holger Hans Peter Freythercedf8902013-10-19 20:47:12 +0200137
138 llist_for_each_entry_safe(sba, sba2, &m_sbas, list) {
Holger Hans Peter Freyther09ef27a2013-10-20 16:37:05 +0200139 if (sba->trx_no == trx_no && sba->ts_no == ts_no) {
Holger Hans Peter Freythercedf8902013-10-19 20:47:12 +0200140 llist_del(&sba->list);
141 talloc_free(sba);
142 }
143 }
144}