tbf: Move TFI selection into alloc_algorithm

Currently the TFI and the TRX have to be determined before the actual TBF
allocation function is called, passing TFI and TRX number as
parameters. This does fit to TFI reuse for different slots, since
this were tightly coupled with the slot selection.

This commit just moves the TFI selection into the alloc_algorithm
functions. The tfi parameter is removed from the the TFI alloc
functions. The trx parameter is changed into use_trx to optionally
limit the trx selection (same semantics like in tfi_find_free).

Sponsored-by: On-Waves ehf
diff --git a/tests/alloc/AllocTest.cpp b/tests/alloc/AllocTest.cpp
index 66bc84f..b6f263f 100644
--- a/tests/alloc/AllocTest.cpp
+++ b/tests/alloc/AllocTest.cpp
@@ -38,13 +38,13 @@
 
 static gprs_rlcmac_tbf *tbf_alloc(struct gprs_rlcmac_bts *bts,
 		GprsMs *ms, gprs_rlcmac_tbf_direction dir,
-		uint8_t tfi, uint8_t trx,
+		uint8_t use_trx,
 		uint8_t ms_class, uint8_t single_slot)
 {
 	if (dir == GPRS_RLCMAC_UL_TBF)
-		return tbf_alloc_ul_tbf(bts, ms, tfi, trx, ms_class, single_slot);
+		return tbf_alloc_ul_tbf(bts, ms, use_trx, ms_class, single_slot);
 	else
-		return tbf_alloc_dl_tbf(bts, ms, tfi, trx, ms_class, single_slot);
+		return tbf_alloc_dl_tbf(bts, ms, use_trx, ms_class, single_slot);
 }
 
 static void check_tfi_usage(BTS *the_bts)
@@ -133,9 +133,7 @@
 	 * least this part is working okay.
 	 */
 	for (int i = 0; i < count; ++i) {
-		tfi = the_bts.tfi_find_free(dir, &used_trx, 0);
-		OSMO_ASSERT(tfi >= 0);
-		tbfs[i] = tbf_alloc(bts, NULL, dir, tfi, used_trx, 0, 0);
+		tbfs[i] = tbf_alloc(bts, NULL, dir, -1, 0, 0);
 		OSMO_ASSERT(tbfs[i] != NULL);
 		tfi = the_bts.tfi_find_free(dir, &tmp_trx, used_trx);
 		OSMO_ASSERT(tbfs[i]->tfi() != tfi);
@@ -151,16 +149,13 @@
 		OSMO_ASSERT(tfi < 0);
 		break;
 	}
-	OSMO_ASSERT(!tbf_alloc(bts, NULL, dir, tfi, used_trx, 0, 0));
+	OSMO_ASSERT(!tbf_alloc(bts, NULL, dir, -1, 0, 0));
 
 	for (size_t i = 0; i < ARRAY_SIZE(tbfs); ++i)
 		if (tbfs[i])
 			tbf_free(tbfs[i]);
 
-	tfi = the_bts.tfi_find_free(dir, &used_trx, 0);
-	OSMO_ASSERT(tfi >= 0);
-
-	tbfs[tfi] = tbf_alloc(bts, NULL, dir, tfi, used_trx, 0, 0);
+	tbfs[tfi] = tbf_alloc(bts, NULL, dir, -1, 0, 0);
 	OSMO_ASSERT(tbfs[tfi]);
 	tbf_free(tbfs[tfi]);
 }
@@ -198,7 +193,6 @@
 		BTS the_bts;
 		struct gprs_rlcmac_bts *bts;
 		struct gprs_rlcmac_trx *trx;
-		int tfi;
 		uint8_t trx_no;
 
 		gprs_rlcmac_tbf *ul_tbf, *dl_tbf;
@@ -213,16 +207,15 @@
 		trx->pdch[6].enable();
 		trx->pdch[7].enable();
 
-		tfi = the_bts.tfi_find_free(GPRS_RLCMAC_UL_TBF, &trx_no, -1);
-		OSMO_ASSERT(tfi >= 0);
-		ul_tbf = tbf_alloc_ul_tbf(bts, NULL, tfi, trx_no, ms_class, 1);
+		ul_tbf = tbf_alloc_ul_tbf(bts, NULL, -1, ms_class, 1);
 		OSMO_ASSERT(ul_tbf);
+		OSMO_ASSERT(ul_tbf->ms());
+		OSMO_ASSERT(ul_tbf->ms()->current_trx());
+		trx_no = ul_tbf->ms()->current_trx()->trx_no;
 		dump_assignment(ul_tbf, "UL");
 
 		/* assume final ack has not been sent */
-		tfi = the_bts.tfi_find_free(GPRS_RLCMAC_UL_TBF, &trx_no, -1);
-		OSMO_ASSERT(tfi >= 0);
-		dl_tbf = tbf_alloc_dl_tbf(bts, ul_tbf->ms(), tfi, trx_no, ms_class, 0);
+		dl_tbf = tbf_alloc_dl_tbf(bts, ul_tbf->ms(), trx_no, ms_class, 0);
 		OSMO_ASSERT(dl_tbf);
 		dump_assignment(dl_tbf, "DL");
 
@@ -241,7 +234,6 @@
 		BTS the_bts;
 		struct gprs_rlcmac_bts *bts;
 		struct gprs_rlcmac_trx *trx;
-		int tfi;
 		uint8_t trx_no;
 
 		gprs_rlcmac_ul_tbf *ul_tbf;
@@ -257,16 +249,15 @@
 		trx->pdch[6].enable();
 		trx->pdch[7].enable();
 
-		tfi = the_bts.tfi_find_free(GPRS_RLCMAC_UL_TBF, &trx_no, -1);
-		OSMO_ASSERT(tfi >= 0);
-		dl_tbf = tbf_alloc_dl_tbf(bts, NULL, tfi, trx_no, ms_class, 1);
+		dl_tbf = tbf_alloc_dl_tbf(bts, NULL, -1, ms_class, 1);
 		dl_tbf->update_ms(0x23, GPRS_RLCMAC_DL_TBF);
 		OSMO_ASSERT(dl_tbf);
+		OSMO_ASSERT(dl_tbf->ms());
+		OSMO_ASSERT(dl_tbf->ms()->current_trx());
+		trx_no = dl_tbf->ms()->current_trx()->trx_no;
 		dump_assignment(dl_tbf, "DL");
 
-		tfi = the_bts.tfi_find_free(GPRS_RLCMAC_UL_TBF, &trx_no, -1);
-		OSMO_ASSERT(tfi >= 0);
-		ul_tbf = tbf_alloc_ul_tbf(bts, dl_tbf->ms(), tfi, trx_no, ms_class, 0);
+		ul_tbf = tbf_alloc_ul_tbf(bts, dl_tbf->ms(), trx_no, ms_class, 0);
 		ul_tbf->update_ms(0x23, GPRS_RLCMAC_UL_TBF);
 		ul_tbf->m_contention_resolution_done = 1;
 		OSMO_ASSERT(ul_tbf);
@@ -308,14 +299,15 @@
 
 		tfi = the_bts.tfi_find_free(GPRS_RLCMAC_UL_TBF, &trx_no, -1);
 		OSMO_ASSERT(tfi >= 0);
-		ul_tbf = tbf_alloc_ul_tbf(bts, NULL, tfi, trx_no, ms_class, 0);
+		ul_tbf = tbf_alloc_ul_tbf(bts, NULL, .1, ms_class, 0);
 		OSMO_ASSERT(ul_tbf);
+		OSMO_ASSERT(ul_tbf->ms());
+		OSMO_ASSERT(ul_tbf->ms()->current_trx());
+		trx_no = ul_tbf->ms()->current_trx()->trx_no;
 		dump_assignment(ul_tbf, "UL");
 
 		/* assume final ack has not been sent */
-		tfi = the_bts.tfi_find_free(GPRS_RLCMAC_UL_TBF, &trx_no, -1);
-		OSMO_ASSERT(tfi >= 0);
-		dl_tbf = tbf_alloc_dl_tbf(bts, ul_tbf->ms(), tfi, trx_no, ms_class, 0);
+		dl_tbf = tbf_alloc_dl_tbf(bts, ul_tbf->ms(), trx_no, ms_class, 0);
 		OSMO_ASSERT(dl_tbf);
 		dump_assignment(dl_tbf, "DL");
 
@@ -353,7 +345,6 @@
 		BTS the_bts;
 		struct gprs_rlcmac_bts *bts;
 		struct gprs_rlcmac_trx *trx;
-		int tfi;
 		uint8_t trx_no;
 
 		gprs_rlcmac_tbf *ul_tbf, *dl_tbf;
@@ -371,16 +362,14 @@
 		ENABLE_PDCH(6, ts6, trx);
 		ENABLE_PDCH(7, ts7, trx);
 
-		tfi = the_bts.tfi_find_free(GPRS_RLCMAC_UL_TBF, &trx_no, -1);
-
-		OSMO_ASSERT(tfi >= 0);
-		ul_tbf = tbf_alloc_ul_tbf(bts, NULL, tfi, trx_no, ms_class, 1);
+		ul_tbf = tbf_alloc_ul_tbf(bts, NULL, -1, ms_class, 1);
+		OSMO_ASSERT(ul_tbf->ms());
+		OSMO_ASSERT(ul_tbf->ms()->current_trx());
+		trx_no = ul_tbf->ms()->current_trx()->trx_no;
 		OSMO_ASSERT(ul_tbf);
 
 		/* assume final ack has not been sent */
-		tfi = the_bts.tfi_find_free(GPRS_RLCMAC_UL_TBF, &trx_no, -1);
-		OSMO_ASSERT(tfi >= 0);
-		dl_tbf = tbf_alloc_dl_tbf(bts, ul_tbf->ms(), tfi, trx_no, ms_class, 0);
+		dl_tbf = tbf_alloc_dl_tbf(bts, ul_tbf->ms(), trx_no, ms_class, 0);
 		OSMO_ASSERT(dl_tbf);
 
 		/* verify that both are on the same ts */
@@ -399,7 +388,6 @@
 		BTS the_bts;
 		struct gprs_rlcmac_bts *bts;
 		struct gprs_rlcmac_trx *trx;
-		int tfi;
 		uint8_t trx_no;
 
 		gprs_rlcmac_ul_tbf *ul_tbf;
@@ -418,15 +406,14 @@
 		ENABLE_PDCH(6, ts6, trx);
 		ENABLE_PDCH(7, ts7, trx);
 
-		tfi = the_bts.tfi_find_free(GPRS_RLCMAC_UL_TBF, &trx_no, -1);
-		OSMO_ASSERT(tfi >= 0);
-		dl_tbf = tbf_alloc_dl_tbf(bts, NULL, tfi, trx_no, ms_class, 1);
+		dl_tbf = tbf_alloc_dl_tbf(bts, NULL, -1, ms_class, 1);
 		OSMO_ASSERT(dl_tbf);
+		OSMO_ASSERT(dl_tbf->ms());
+		OSMO_ASSERT(dl_tbf->ms()->current_trx());
+		trx_no = dl_tbf->ms()->current_trx()->trx_no;
 		dl_tbf->update_ms(0x23, GPRS_RLCMAC_DL_TBF);
 
-		tfi = the_bts.tfi_find_free(GPRS_RLCMAC_UL_TBF, &trx_no, -1);
-		OSMO_ASSERT(tfi >= 0);
-		ul_tbf = tbf_alloc_ul_tbf(bts, dl_tbf->ms(), tfi, trx_no, ms_class, 0);
+		ul_tbf = tbf_alloc_ul_tbf(bts, dl_tbf->ms(), trx_no, ms_class, 0);
 		OSMO_ASSERT(ul_tbf);
 		ul_tbf->update_ms(0x23, GPRS_RLCMAC_UL_TBF);
 		ul_tbf->m_contention_resolution_done = 1;
@@ -472,7 +459,8 @@
 
 typedef int (*algo_t)(struct gprs_rlcmac_bts *bts,
 		struct GprsMs *ms,
-		struct gprs_rlcmac_tbf *tbf, uint32_t cust, uint8_t single);
+		struct gprs_rlcmac_tbf *tbf, uint32_t cust, uint8_t single,
+		int use_trx);
 
 static char get_dir_char(uint8_t mask, uint8_t tx, uint8_t rx)
 {
@@ -495,30 +483,28 @@
 	enum test_mode mode)
 {
 	struct gprs_rlcmac_bts *bts;
-	int tfi;
-	uint8_t trx_no;
+	uint8_t trx_no = -1;
 
 	bts = the_bts->bts_data();
 
 	gprs_rlcmac_tbf *tbf = NULL;
 
+	if (ms && ms->current_trx())
+		trx_no = ms->current_trx()->trx_no;
+
 	/* Allocate what is needed first */
 	switch (mode) {
 	case TEST_MODE_UL_ONLY:
 	case TEST_MODE_DL_AFTER_UL:
 	case TEST_MODE_UL_AND_DL:
-		tfi = the_bts->tfi_find_free(GPRS_RLCMAC_UL_TBF, &trx_no, -1);
-		if (tfi < 0)
-			return NULL;
-		tbf = tbf_alloc_ul_tbf(bts, ms, tfi, trx_no, ms_class, 0);
+		tbf = tbf_alloc_ul_tbf(bts, ms, trx_no, ms_class, 0);
 		if (tbf == NULL)
 			return NULL;
 		break;
 	case TEST_MODE_DL_ONLY:
 	case TEST_MODE_UL_AFTER_DL:
 	case TEST_MODE_DL_AND_UL:
-		tfi = the_bts->tfi_find_free(GPRS_RLCMAC_DL_TBF, &trx_no, -1);
-		tbf = tbf_alloc_dl_tbf(bts, ms, tfi, trx_no, ms_class, 0);
+		tbf = tbf_alloc_dl_tbf(bts, ms, trx_no, ms_class, 0);
 		if (tbf == NULL)
 			return NULL;
 	}
diff --git a/tests/alloc/AllocTest.err b/tests/alloc/AllocTest.err
index 2dc99fe..d848aff 100644
--- a/tests/alloc/AllocTest.err
+++ b/tests/alloc/AllocTest.err
@@ -1,36 +1,55 @@
 No TFI available.
 No TFI available.
-- Failed to allocate a TS, no USF available
 No TFI available.
-No TFI available.
+- Failed to allocate a TFI
 - Failed to allocate a TS, no USF available
 No TFI available.
 No TFI available.
 No TFI available.
+- Failed to allocate a TFI
+- Failed to allocate a TS, no USF available
 No TFI available.
 No TFI available.
+- Failed to allocate a TFI
 No TFI available.
 No TFI available.
+- Failed to allocate a TFI
 No TFI available.
 No TFI available.
+- Failed to allocate a TFI
 No TFI available.
 No TFI available.
+- Failed to allocate a TFI
 No TFI available.
 No TFI available.
+- Failed to allocate a TFI
 No TFI available.
 No TFI available.
+- Failed to allocate a TFI
 No TFI available.
 No TFI available.
+- Failed to allocate a TFI
 No TFI available.
 No TFI available.
+- Failed to allocate a TFI
 No TFI available.
 No TFI available.
+- Failed to allocate a TFI
 No TFI available.
 No TFI available.
+- Failed to allocate a TFI
 No TFI available.
 No TFI available.
+- Failed to allocate a TFI
 No TFI available.
 No TFI available.
+- Failed to allocate a TFI
 No TFI available.
 No TFI available.
+- Failed to allocate a TFI
 No TFI available.
+No TFI available.
+- Failed to allocate a TFI
+No TFI available.
+No TFI available.
+- Failed to allocate a TFI