blob: 547612f686bd0429e9e6845b8f2c8c1c2b3299e2 [file] [log] [blame]
Holger Hans Peter Freyther17c31ce2013-08-24 18:31:27 +02001/* Copied from gprs_bssgp_pcu.cpp
2 *
3 * Copyright (C) 2012 Ivan Klyuchnikov
Holger Hans Peter Freyther86921282013-08-24 21:26:42 +02004 * Copyright (C) 2012 Andreas Eversberg <jolly@eversberg.eu>
Holger Hans Peter Freyther17c31ce2013-08-24 18:31:27 +02005 * 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
22#include <tbf.h>
23#include <gprs_rlcmac.h>
24#include <gprs_debug.h>
25
26extern "C" {
27#include <osmocom/core/msgb.h>
Holger Hans Peter Freyther45561302013-10-16 17:55:57 +020028#include <osmocom/core/talloc.h>
Holger Hans Peter Freyther17c31ce2013-08-24 18:31:27 +020029}
30
31#include <errno.h>
32#include <string.h>
33
Holger Hans Peter Freyther7380bab2013-10-16 18:09:19 +020034extern void *tall_pcu_ctx;
35
Holger Hans Peter Freytherd1d114f2013-08-24 20:46:18 +020036static inline void tbf_update_ms_class(struct gprs_rlcmac_tbf *tbf,
37 const uint8_t ms_class)
38{
39 if (!tbf->ms_class && ms_class)
40 tbf->ms_class = ms_class;
41}
42
Holger Hans Peter Freytherd8689282013-08-24 20:51:06 +020043static inline void tbf_assign_imsi(struct gprs_rlcmac_tbf *tbf,
44 const char *imsi)
45{
46 strncpy(tbf->meas.imsi, imsi, sizeof(tbf->meas.imsi) - 1);
47}
48
Holger Hans Peter Freyther31d0df92013-08-24 20:42:45 +020049static struct gprs_rlcmac_tbf *tbf_lookup_dl(const uint32_t tlli, const char *imsi)
50{
51 /* TODO: look up by IMSI first, then tlli, then old_tlli */
52 return tbf_by_tlli(tlli, GPRS_RLCMAC_DL_TBF);
53}
54
55static int tbf_append_data(struct gprs_rlcmac_tbf *tbf,
56 struct gprs_rlcmac_bts *bts,
57 const uint8_t ms_class,
58 const uint16_t pdu_delay_csec,
59 const uint8_t *data, const uint16_t len)
60{
61 LOGP(DRLCMAC, LOGL_INFO, "TBF: APPEND TFI: %u TLLI: 0x%08x\n", tbf->tfi, tbf->tlli);
Holger Hans Peter Freyther1c344e22013-10-16 18:33:18 +020062 if (tbf->state_is(GPRS_RLCMAC_WAIT_RELEASE)) {
Holger Hans Peter Freyther31d0df92013-08-24 20:42:45 +020063 LOGP(DRLCMAC, LOGL_DEBUG, "TBF in WAIT RELEASE state "
64 "(T3193), so reuse TBF\n");
65 memcpy(tbf->llc_frame, data, len);
66 tbf->llc_length = len;
67 /* reset rlc states */
68 memset(&tbf->dir.dl, 0, sizeof(tbf->dir.dl));
69 /* keep to flags */
70 tbf->state_flags &= GPRS_RLCMAC_FLAG_TO_MASK;
71 tbf->state_flags &= ~(1 << GPRS_RLCMAC_FLAG_CCCH);
Holger Hans Peter Freytherd1d114f2013-08-24 20:46:18 +020072 tbf_update_ms_class(tbf, ms_class);
Holger Hans Peter Freyther31d0df92013-08-24 20:42:45 +020073 tbf_update(tbf);
74 gprs_rlcmac_trigger_downlink_assignment(tbf, tbf, NULL);
75 } else {
76 /* the TBF exists, so we must write it in the queue
77 * we prepend lifetime in front of PDU */
78 struct timeval *tv;
79 struct msgb *llc_msg = msgb_alloc(len + sizeof(*tv),
80 "llc_pdu_queue");
81 if (!llc_msg)
82 return -ENOMEM;
83 tv = (struct timeval *)msgb_put(llc_msg, sizeof(*tv));
84
85 uint16_t delay_csec;
86 if (bts->force_llc_lifetime)
87 delay_csec = bts->force_llc_lifetime;
88 else
89 delay_csec = pdu_delay_csec;
90 /* keep timestap at 0 for infinite delay */
91 if (delay_csec != 0xffff) {
92 /* calculate timestamp of timeout */
93 gettimeofday(tv, NULL);
94 tv->tv_usec += (delay_csec % 100) * 10000;
95 tv->tv_sec += delay_csec / 100;
96 if (tv->tv_usec > 999999) {
97 tv->tv_usec -= 1000000;
98 tv->tv_sec++;
99 }
100 }
101 memcpy(msgb_put(llc_msg, len), data, len);
102 msgb_enqueue(&tbf->llc_queue, llc_msg);
Holger Hans Peter Freytherd1d114f2013-08-24 20:46:18 +0200103 tbf_update_ms_class(tbf, ms_class);
Holger Hans Peter Freyther31d0df92013-08-24 20:42:45 +0200104 }
105
106 return 0;
107}
108
Holger Hans Peter Freyther443c8222013-08-24 20:59:46 +0200109static int tbf_new_dl_assignment(struct gprs_rlcmac_bts *bts,
110 const char *imsi,
111 const uint32_t tlli, const uint8_t ms_class,
112 const uint8_t *data, const uint16_t len)
113{
114 uint8_t trx, ta, ss;
115 int8_t use_trx;
116 struct gprs_rlcmac_tbf *old_tbf, *tbf;
117 int8_t tfi; /* must be signed */
118 int rc;
119
120 /* check for uplink data, so we copy our informations */
Holger Hans Peter Freytherbb20b2c2013-08-24 21:22:16 +0200121#warning "Do the same look up for IMSI, TLLI and OLD_TLLI"
122#warning "Refactor the below lines... into a new method"
Holger Hans Peter Freyther443c8222013-08-24 20:59:46 +0200123 tbf = tbf_by_tlli(tlli, GPRS_RLCMAC_UL_TBF);
124 if (tbf && tbf->dir.ul.contention_resolution_done
125 && !tbf->dir.ul.final_ack_sent) {
126 use_trx = tbf->trx;
127 ta = tbf->ta;
128 ss = 0;
129 old_tbf = tbf;
130 } else {
131 use_trx = -1;
132 /* we already have an uplink TBF, so we use that TA */
133 if (tbf)
134 ta = tbf->ta;
135 else {
136 /* recall TA */
137 rc = recall_timing_advance(tlli);
138 if (rc < 0) {
139 LOGP(DRLCMAC, LOGL_NOTICE, "TA unknown"
140 ", assuming 0\n");
141 ta = 0;
142 } else
143 ta = rc;
144 }
145 ss = 1; /* PCH assignment only allows one timeslot */
146 old_tbf = NULL;
147 }
148
149 // Create new TBF (any TRX)
Holger Hans Peter Freytherbcafdf82013-08-24 21:13:31 +0200150#warning "Copy and paste with alloc_ul_tbf"
Holger Hans Peter Freyther443c8222013-08-24 20:59:46 +0200151 tfi = tfi_find_free(bts, GPRS_RLCMAC_DL_TBF, &trx, use_trx);
152 if (tfi < 0) {
153 LOGP(DRLCMAC, LOGL_NOTICE, "No PDCH resource\n");
154 /* FIXME: send reject */
155 return -EBUSY;
156 }
157 /* set number of downlink slots according to multislot class */
158 tbf = tbf_alloc(bts, tbf, GPRS_RLCMAC_DL_TBF, tfi, trx, ms_class, ss);
159 if (!tbf) {
160 LOGP(DRLCMAC, LOGL_NOTICE, "No PDCH ressource\n");
161 /* FIXME: send reject */
162 return -EBUSY;
163 }
164 tbf->tlli = tlli;
165 tbf->tlli_valid = 1;
166 tbf->ta = ta;
167
168 LOGP(DRLCMAC, LOGL_DEBUG,
169 "TBF: [DOWNLINK] START TFI: %d TLLI: 0x%08x \n",
170 tbf->tfi, tbf->tlli);
171
172 /* new TBF, so put first frame */
173 memcpy(tbf->llc_frame, data, len);
174 tbf->llc_length = len;
175
176 /* trigger downlink assignment and set state to ASSIGN.
177 * we don't use old_downlink, so the possible uplink is used
178 * to trigger downlink assignment. if there is no uplink,
179 * AGCH is used. */
180 gprs_rlcmac_trigger_downlink_assignment(tbf, old_tbf, imsi);
181
182 /* store IMSI for debugging purpose. TODO: it is more than debugging */
183 tbf_assign_imsi(tbf, imsi);
184 return 0;
185}
186
Holger Hans Peter Freyther17c31ce2013-08-24 18:31:27 +0200187/**
188 * TODO: split into unit test-able parts...
189 */
190int tbf_handle(struct gprs_rlcmac_bts *bts,
191 const uint32_t tlli, const char *imsi,
Holger Hans Peter Freyther31d0df92013-08-24 20:42:45 +0200192 const uint8_t ms_class, const uint16_t delay_csec,
Holger Hans Peter Freyther17c31ce2013-08-24 18:31:27 +0200193 const uint8_t *data, const uint16_t len)
194{
195 struct gprs_rlcmac_tbf *tbf;
Holger Hans Peter Freyther17c31ce2013-08-24 18:31:27 +0200196
197 /* check for existing TBF */
Holger Hans Peter Freyther31d0df92013-08-24 20:42:45 +0200198 tbf = tbf_lookup_dl(tlli, imsi);
199 if (tbf) {
200 int rc = tbf_append_data(tbf, bts, ms_class,
201 delay_csec, data, len);
Holger Hans Peter Freyther443c8222013-08-24 20:59:46 +0200202 if (rc >= 0)
203 tbf_assign_imsi(tbf, imsi);
204 return rc;
205 }
Holger Hans Peter Freyther17c31ce2013-08-24 18:31:27 +0200206
Holger Hans Peter Freyther443c8222013-08-24 20:59:46 +0200207 return tbf_new_dl_assignment(bts, imsi, tlli, ms_class, data, len);
Holger Hans Peter Freyther17c31ce2013-08-24 18:31:27 +0200208}
Holger Hans Peter Freyther86921282013-08-24 21:26:42 +0200209
210struct gprs_rlcmac_tbf *tbf_alloc_ul(struct gprs_rlcmac_bts *bts,
211 int8_t use_trx, uint8_t ms_class,
212 uint32_t tlli, uint8_t ta, struct gprs_rlcmac_tbf *dl_tbf)
213{
214 uint8_t trx;
215 struct gprs_rlcmac_tbf *tbf;
216 uint8_t tfi;
217
218#warning "Copy and paste with tbf_new_dl_assignment"
219 /* create new TBF, use sme TRX as DL TBF */
220 tfi = tfi_find_free(bts, GPRS_RLCMAC_UL_TBF, &trx, use_trx);
221 if (tfi < 0) {
222 LOGP(DRLCMAC, LOGL_NOTICE, "No PDCH ressource\n");
223 /* FIXME: send reject */
224 return NULL;
225 }
226 /* use multislot class of downlink TBF */
227 tbf = tbf_alloc(bts, dl_tbf, GPRS_RLCMAC_UL_TBF, tfi, trx, ms_class, 0);
228 if (!tbf) {
229 LOGP(DRLCMAC, LOGL_NOTICE, "No PDCH ressource\n");
230 /* FIXME: send reject */
231 return NULL;
232 }
233 tbf->tlli = tlli;
234 tbf->tlli_valid = 1; /* no contention resolution */
235 tbf->dir.ul.contention_resolution_done = 1;
236 tbf->ta = ta; /* use current TA */
237 tbf_new_state(tbf, GPRS_RLCMAC_ASSIGN);
238 tbf->state_flags |= (1 << GPRS_RLCMAC_FLAG_PACCH);
239 tbf_timer_start(tbf, 3169, bts->t3169, 0);
240
241 return tbf;
242}
Holger Hans Peter Freyther964ddb62013-10-16 17:53:23 +0200243
Holger Hans Peter Freyther45561302013-10-16 17:55:57 +0200244static void tbf_unlink_pdch(struct gprs_rlcmac_tbf *tbf)
245{
246 struct gprs_rlcmac_bts *bts = gprs_rlcmac_bts;
247 struct gprs_rlcmac_pdch *pdch;
248 int ts;
249
250 if (tbf->direction == GPRS_RLCMAC_UL_TBF) {
251 bts->trx[tbf->trx].ul_tbf[tbf->tfi] = NULL;
252 for (ts = 0; ts < 8; ts++) {
253 pdch = tbf->pdch[ts];
254 if (pdch)
255 pdch->ul_tbf[tbf->tfi] = NULL;
256 tbf->pdch[ts] = NULL;
257 }
258 } else {
259 bts->trx[tbf->trx].dl_tbf[tbf->tfi] = NULL;
260 for (ts = 0; ts < 8; ts++) {
261 pdch = tbf->pdch[ts];
262 if (pdch)
263 pdch->dl_tbf[tbf->tfi] = NULL;
264 tbf->pdch[ts] = NULL;
265 }
266 }
267}
268
269void tbf_free(struct gprs_rlcmac_tbf *tbf)
270{
271 struct msgb *msg;
272
273 /* Give final measurement report */
274 gprs_rlcmac_rssi_rep(tbf);
275 gprs_rlcmac_lost_rep(tbf);
276
277 debug_diagram(tbf->diag, "+---------------+");
278 debug_diagram(tbf->diag, "| THE END |");
279 debug_diagram(tbf->diag, "+---------------+");
280 LOGP(DRLCMAC, LOGL_INFO, "Free %s TBF=%d with TLLI=0x%08x.\n",
281 (tbf->direction == GPRS_RLCMAC_UL_TBF) ? "UL" : "DL", tbf->tfi,
282 tbf->tlli);
283 if (tbf->ul_ass_state != GPRS_RLCMAC_UL_ASS_NONE)
284 LOGP(DRLCMAC, LOGL_ERROR, "Software error: Pending uplink "
285 "assignment. This may not happen, because the "
286 "assignment message never gets transmitted. Please "
287 "be shure not to free in this state. PLEASE FIX!\n");
288 if (tbf->dl_ass_state != GPRS_RLCMAC_DL_ASS_NONE)
289 LOGP(DRLCMAC, LOGL_ERROR, "Software error: Pending downlink "
290 "assignment. This may not happen, because the "
291 "assignment message never gets transmitted. Please "
292 "be shure not to free in this state. PLEASE FIX!\n");
293 tbf_timer_stop(tbf);
294 while ((msg = msgb_dequeue(&tbf->llc_queue)))
295 msgb_free(msg);
296 tbf_unlink_pdch(tbf);
297 llist_del(&tbf->list);
298 LOGP(DRLCMAC, LOGL_DEBUG, "********** TBF ends here **********\n");
299 talloc_free(tbf);
300}
301
302int tbf_update(struct gprs_rlcmac_tbf *tbf)
303{
304 struct gprs_rlcmac_bts *bts = gprs_rlcmac_bts;
305 struct gprs_rlcmac_tbf *ul_tbf = NULL;
306 int rc;
307
308 LOGP(DRLCMAC, LOGL_DEBUG, "********** TBF update **********\n");
309
310 if (tbf->direction != GPRS_RLCMAC_DL_TBF)
311 return -EINVAL;
312
313 if (!tbf->ms_class) {
314 LOGP(DRLCMAC, LOGL_DEBUG, "- Cannot update, no class\n");
315 return -EINVAL;
316 }
317
318 ul_tbf = tbf_by_tlli(tbf->tlli, GPRS_RLCMAC_UL_TBF);
319
320 tbf_unlink_pdch(tbf);
321 rc = bts->alloc_algorithm(bts, ul_tbf, tbf, bts->alloc_algorithm_curst, 0);
322 /* if no ressource */
323 if (rc < 0) {
324 LOGP(DRLCMAC, LOGL_ERROR, "No ressource after update???\n");
325 return -rc;
326 }
327
328 return 0;
329}
330
331int tbf_assign_control_ts(struct gprs_rlcmac_tbf *tbf)
332{
333 if (tbf->control_ts == 0xff)
334 LOGP(DRLCMAC, LOGL_INFO, "- Setting Control TS %d\n",
335 tbf->first_common_ts);
336 else if (tbf->control_ts != tbf->first_common_ts)
337 LOGP(DRLCMAC, LOGL_INFO, "- Changing Control TS %d\n",
338 tbf->first_common_ts);
339 tbf->control_ts = tbf->first_common_ts;
340
341 return 0;
342}
343
344static const char *tbf_state_name[] = {
345 "NULL",
346 "ASSIGN",
347 "FLOW",
348 "FINISHED",
349 "WAIT RELEASE",
350 "RELEASING",
351};
352
353void tbf_new_state(struct gprs_rlcmac_tbf *tbf,
354 enum gprs_rlcmac_tbf_state state)
355{
356 debug_diagram(tbf->diag, "->%s", tbf_state_name[state]);
357 LOGP(DRLCMAC, LOGL_DEBUG, "%s TBF=%d changes state from %s to %s\n",
358 (tbf->direction == GPRS_RLCMAC_UL_TBF) ? "UL" : "DL", tbf->tfi,
359 tbf_state_name[tbf->state], tbf_state_name[state]);
Holger Hans Peter Freyther1c344e22013-10-16 18:33:18 +0200360 tbf->set_state(state);
Holger Hans Peter Freyther45561302013-10-16 17:55:57 +0200361}
362
363void tbf_timer_start(struct gprs_rlcmac_tbf *tbf, unsigned int T,
364 unsigned int seconds, unsigned int microseconds)
365{
366 if (!osmo_timer_pending(&tbf->timer))
367 LOGP(DRLCMAC, LOGL_DEBUG, "Starting %s TBF=%d timer %u.\n",
368 (tbf->direction == GPRS_RLCMAC_UL_TBF) ? "UL" : "DL",
369 tbf->tfi, T);
370 else
371 LOGP(DRLCMAC, LOGL_DEBUG, "Restarting %s TBF=%d timer %u "
372 "while old timer %u pending \n",
373 (tbf->direction == GPRS_RLCMAC_UL_TBF) ? "UL" : "DL",
374 tbf->tfi, T, tbf->T);
375
376 tbf->T = T;
377 tbf->num_T_exp = 0;
378
379 /* Tunning timers can be safely re-scheduled. */
380 tbf->timer.data = tbf;
381 tbf->timer.cb = &tbf_timer_cb;
382
383 osmo_timer_schedule(&tbf->timer, seconds, microseconds);
384}
385
386void tbf_timer_stop(struct gprs_rlcmac_tbf *tbf)
387{
388 if (osmo_timer_pending(&tbf->timer)) {
389 LOGP(DRLCMAC, LOGL_DEBUG, "Stopping %s TBF=%d timer %u.\n",
390 (tbf->direction == GPRS_RLCMAC_UL_TBF) ? "UL" : "DL",
391 tbf->tfi, tbf->T);
392 osmo_timer_del(&tbf->timer);
393 }
394}
395
Holger Hans Peter Freyther7380bab2013-10-16 18:09:19 +0200396/* lookup TBF Entity (by TFI) */
397struct gprs_rlcmac_tbf *tbf_by_tfi(struct gprs_rlcmac_bts *bts,
398 uint8_t tfi, uint8_t trx, enum gprs_rlcmac_tbf_direction dir)
399{
400 struct gprs_rlcmac_tbf *tbf;
401
402 if (tfi >= 32 || trx >= 8)
403 return NULL;
404
405 if (dir == GPRS_RLCMAC_UL_TBF)
406 tbf = bts->trx[trx].ul_tbf[tfi];
407 else
408 tbf = bts->trx[trx].dl_tbf[tfi];
409 if (!tbf)
410 return NULL;
411
Holger Hans Peter Freyther1c344e22013-10-16 18:33:18 +0200412 if (tbf->state_is_not(GPRS_RLCMAC_RELEASING))
Holger Hans Peter Freyther7380bab2013-10-16 18:09:19 +0200413 return tbf;
414
415 return NULL;
416}
417
418/* search for active downlink or uplink tbf */
419struct gprs_rlcmac_tbf *tbf_by_tlli(uint32_t tlli,
420 enum gprs_rlcmac_tbf_direction dir)
421{
422 struct gprs_rlcmac_tbf *tbf;
423 if (dir == GPRS_RLCMAC_UL_TBF) {
424 llist_for_each_entry(tbf, &gprs_rlcmac_ul_tbfs, list) {
Holger Hans Peter Freyther1c344e22013-10-16 18:33:18 +0200425 if (tbf->state_is_not(GPRS_RLCMAC_RELEASING)
Holger Hans Peter Freyther7380bab2013-10-16 18:09:19 +0200426 && tbf->tlli == tlli && tbf->tlli_valid)
427 return tbf;
428 }
429 } else {
430 llist_for_each_entry(tbf, &gprs_rlcmac_dl_tbfs, list) {
Holger Hans Peter Freyther1c344e22013-10-16 18:33:18 +0200431 if (tbf->state_is_not(GPRS_RLCMAC_RELEASING)
Holger Hans Peter Freyther7380bab2013-10-16 18:09:19 +0200432 && tbf->tlli == tlli)
433 return tbf;
434 }
435 }
436 return NULL;
437}
438
439struct gprs_rlcmac_tbf *tbf_by_poll_fn(uint32_t fn, uint8_t trx, uint8_t ts)
440{
441 struct gprs_rlcmac_tbf *tbf;
442
443 /* only one TBF can poll on specific TS/FN, because scheduler can only
444 * schedule one downlink control block (with polling) at a FN per TS */
445 llist_for_each_entry(tbf, &gprs_rlcmac_ul_tbfs, list) {
Holger Hans Peter Freyther1c344e22013-10-16 18:33:18 +0200446 if (tbf->state_is_not(GPRS_RLCMAC_RELEASING)
Holger Hans Peter Freyther7380bab2013-10-16 18:09:19 +0200447 && tbf->poll_state == GPRS_RLCMAC_POLL_SCHED
448 && tbf->poll_fn == fn && tbf->trx == trx
449 && tbf->control_ts == ts)
450 return tbf;
451 }
452 llist_for_each_entry(tbf, &gprs_rlcmac_dl_tbfs, list) {
Holger Hans Peter Freyther1c344e22013-10-16 18:33:18 +0200453 if (tbf->state_is_not(GPRS_RLCMAC_RELEASING)
Holger Hans Peter Freyther7380bab2013-10-16 18:09:19 +0200454 && tbf->poll_state == GPRS_RLCMAC_POLL_SCHED
455 && tbf->poll_fn == fn && tbf->trx == trx
456 && tbf->control_ts == ts)
457 return tbf;
458 }
459 return NULL;
460}
461
462struct gprs_rlcmac_tbf *tbf_alloc(struct gprs_rlcmac_bts *bts,
463 struct gprs_rlcmac_tbf *old_tbf, enum gprs_rlcmac_tbf_direction dir,
464 uint8_t tfi, uint8_t trx,
465 uint8_t ms_class, uint8_t single_slot)
466{
467 struct gprs_rlcmac_tbf *tbf;
468 int rc;
469
470#ifdef DEBUG_DIAGRAM
471 /* hunt for first free number in diagram */
472 int diagram_num;
473 for (diagram_num = 0; ; diagram_num++) {
474 llist_for_each_entry(tbf, &gprs_rlcmac_ul_tbfs, list) {
475 if (tbf->diag == diagram_num)
476 goto next_diagram;
477 }
478 llist_for_each_entry(tbf, &gprs_rlcmac_dl_tbfs, list) {
479 if (tbf->diag == diagram_num)
480 goto next_diagram;
481 }
482 break;
483next_diagram:
484 continue;
485 }
486#endif
487
488 LOGP(DRLCMAC, LOGL_DEBUG, "********** TBF starts here **********\n");
489 LOGP(DRLCMAC, LOGL_INFO, "Allocating %s TBF: TFI=%d TRX=%d "
490 "MS_CLASS=%d\n", (dir == GPRS_RLCMAC_UL_TBF) ? "UL" : "DL",
491 tfi, trx, ms_class);
492
493 if (trx >= 8 || tfi >= 32)
494 return NULL;
495
496 tbf = talloc_zero(tall_pcu_ctx, struct gprs_rlcmac_tbf);
497 if (!tbf)
498 return NULL;
499
500#ifdef DEBUG_DIAGRAM
501 tbf->diag = diagram_num;
502#endif
503 tbf->direction = dir;
504 tbf->tfi = tfi;
505 tbf->trx = trx;
506 tbf->arfcn = bts->trx[trx].arfcn;
507 tbf->ms_class = ms_class;
508 tbf->ws = 64;
509 tbf->sns = 128;
510 /* select algorithm */
511 rc = bts->alloc_algorithm(bts, old_tbf, tbf, bts->alloc_algorithm_curst,
512 single_slot);
513 /* if no ressource */
514 if (rc < 0) {
515 talloc_free(tbf);
516 return NULL;
517 }
518 /* assign control ts */
519 tbf->control_ts = 0xff;
520 rc = tbf_assign_control_ts(tbf);
521 /* if no ressource */
522 if (rc < 0) {
523 talloc_free(tbf);
524 return NULL;
525 }
526
527 /* set timestamp */
528 gettimeofday(&tbf->meas.dl_bw_tv, NULL);
529 gettimeofday(&tbf->meas.rssi_tv, NULL);
530 gettimeofday(&tbf->meas.dl_loss_tv, NULL);
531
532 INIT_LLIST_HEAD(&tbf->llc_queue);
533 if (dir == GPRS_RLCMAC_UL_TBF)
534 llist_add(&tbf->list, &gprs_rlcmac_ul_tbfs);
535 else
536 llist_add(&tbf->list, &gprs_rlcmac_dl_tbfs);
537
538 debug_diagram(tbf->diag, "+-----------------+");
539 debug_diagram(tbf->diag, "|NEW %s TBF TFI=%2d|",
540 (dir == GPRS_RLCMAC_UL_TBF) ? "UL" : "DL", tfi);
541 debug_diagram(tbf->diag, "+-----------------+");
542
543 return tbf;
544}
545
546void tbf_timer_cb(void *_tbf)
547{
548 struct gprs_rlcmac_tbf *tbf = (struct gprs_rlcmac_tbf *)_tbf;
549
550 LOGP(DRLCMAC, LOGL_DEBUG, "%s TBF=%d timer %u expired.\n",
551 (tbf->direction == GPRS_RLCMAC_UL_TBF) ? "UL" : "DL", tbf->tfi,
552 tbf->T);
553
554 tbf->num_T_exp++;
555
556 switch (tbf->T) {
557#ifdef DEBUG_DL_ASS_IDLE
558 case 1234:
559 gprs_rlcmac_trigger_downlink_assignment(tbf, NULL, debug_imsi);
560 break;
561#endif
562 case 0: /* assignment */
563 if ((tbf->state_flags & (1 << GPRS_RLCMAC_FLAG_PACCH))) {
Holger Hans Peter Freyther1c344e22013-10-16 18:33:18 +0200564 if (tbf->state_is(GPRS_RLCMAC_ASSIGN)) {
Holger Hans Peter Freyther7380bab2013-10-16 18:09:19 +0200565 LOGP(DRLCMAC, LOGL_NOTICE, "Releasing due to "
566 "PACCH assignment timeout.\n");
567 tbf_free(tbf);
568 } else
569 LOGP(DRLCMAC, LOGL_ERROR, "Error: TBF is not "
570 "in assign state\n");
571 }
572 if ((tbf->state_flags & (1 << GPRS_RLCMAC_FLAG_CCCH))) {
573 /* change state to FLOW, so scheduler will start transmission */
574 tbf->dir.dl.wait_confirm = 0;
Holger Hans Peter Freyther1c344e22013-10-16 18:33:18 +0200575 if (tbf->state_is(GPRS_RLCMAC_ASSIGN)) {
Holger Hans Peter Freyther7380bab2013-10-16 18:09:19 +0200576 tbf_new_state(tbf, GPRS_RLCMAC_FLOW);
577 tbf_assign_control_ts(tbf);
578 } else
579 LOGP(DRLCMAC, LOGL_NOTICE, "Continue flow after "
580 "IMM.ASS confirm\n");
581 }
582 break;
583 case 3169:
584 case 3191:
585 case 3195:
586 LOGP(DRLCMAC, LOGL_NOTICE, "TBF T%d timeout during "
587 "transsmission\n", tbf->T);
588 tbf->rlcmac_diag();
589 /* fall through */
590 case 3193:
591 if (tbf->T == 3193)
592 debug_diagram(tbf->diag, "T3193 timeout");
593 LOGP(DRLCMAC, LOGL_DEBUG, "TBF will be freed due to timeout\n");
594 /* free TBF */
595 tbf_free(tbf);
596 break;
597 default:
598 LOGP(DRLCMAC, LOGL_ERROR, "Timer expired in unknown mode: %u\n",
599 tbf->T);
600 }
601}
602
603int gprs_rlcmac_tbf::rlcmac_diag()
604{
605 if ((state_flags & (1 << GPRS_RLCMAC_FLAG_CCCH)))
606 LOGP(DRLCMAC, LOGL_NOTICE, "- Assignment was on CCCH\n");
607 if ((state_flags & (1 << GPRS_RLCMAC_FLAG_PACCH)))
608 LOGP(DRLCMAC, LOGL_NOTICE, "- Assignment was on PACCH\n");
609 if ((state_flags & (1 << GPRS_RLCMAC_FLAG_UL_DATA)))
610 LOGP(DRLCMAC, LOGL_NOTICE, "- Uplink data was received\n");
611 else if (direction == GPRS_RLCMAC_UL_TBF)
612 LOGP(DRLCMAC, LOGL_NOTICE, "- No uplink data received yet\n");
613 if ((state_flags & (1 << GPRS_RLCMAC_FLAG_DL_ACK)))
614 LOGP(DRLCMAC, LOGL_NOTICE, "- Downlink ACK was received\n");
615 else if (direction == GPRS_RLCMAC_DL_TBF)
616 LOGP(DRLCMAC, LOGL_NOTICE, "- No downlink ACK received yet\n");
617
618 return 0;
619}
620
Holger Hans Peter Freyther964ddb62013-10-16 17:53:23 +0200621void gprs_rlcmac_tbf::free_all(struct gprs_rlcmac_trx *trx)
622{
623 for (uint8_t tfi = 0; tfi < 32; tfi++) {
624 struct gprs_rlcmac_tbf *tbf;
625
626 tbf = trx->ul_tbf[tfi];
627 if (tbf)
628 tbf_free(tbf);
629 tbf = trx->dl_tbf[tfi];
630 if (tbf)
631 tbf_free(tbf);
632 }
633}
Holger Hans Peter Freyther4f6a4e5d2013-10-16 17:58:46 +0200634
635void gprs_rlcmac_tbf::free_all(struct gprs_rlcmac_pdch *pdch)
636{
637 for (uint8_t tfi = 0; tfi < 32; tfi++) {
638 struct gprs_rlcmac_tbf *tbf;
639
640 tbf = pdch->ul_tbf[tfi];
641 if (tbf)
642 tbf_free(tbf);
643 tbf = pdch->dl_tbf[tfi];
644 if (tbf)
645 tbf_free(tbf);
646 }
647}