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/src/gprs_rlcmac_ts_alloc.cpp b/src/gprs_rlcmac_ts_alloc.cpp
index ec228d1..3381436 100644
--- a/src/gprs_rlcmac_ts_alloc.cpp
+++ b/src/gprs_rlcmac_ts_alloc.cpp
@@ -277,6 +277,24 @@
 	attach_tbf_to_pdch(pdch, tbf);
 }
 
+static int tfi_find_free(BTS *bts, GprsMs *ms, enum gprs_rlcmac_tbf_direction dir,
+	int use_trx, int *trx_no_)
+{
+	int tfi;
+	uint8_t trx_no;
+
+	if (use_trx == -1 && ms->current_trx())
+		use_trx = ms->current_trx()->trx_no;
+
+	tfi = bts->tfi_find_free(dir, &trx_no, use_trx);
+	if (tfi < 0)
+		return -EBUSY;
+
+	if (trx_no_)
+		*trx_no_ = trx_no;
+
+	return tfi;
+}
 
 /* Slot Allocation: Algorithm A
  *
@@ -284,11 +302,14 @@
  */
 int alloc_algorithm_a(struct gprs_rlcmac_bts *bts,
 	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)
 {
 	struct gprs_rlcmac_pdch *pdch;
 	int ts = -1;
 	uint8_t ul_slots, dl_slots;
+	int trx_no;
+	int rc;
 	int usf = -1;
 	int mask = 0xff;
 	const char *mask_reason = NULL;
@@ -296,6 +317,14 @@
 	LOGP(DRLCMAC, LOGL_DEBUG, "Slot Allocation (Algorithm A) for class "
 		"%d\n", tbf->ms_class());
 
+	rc = tfi_find_free(bts->bts, ms, tbf->direction, use_trx, &trx_no);
+	if (rc < 0) {
+		LOGP(DRLCMAC, LOGL_NOTICE, "- Failed to allocate a TFI\n");
+		return rc;
+	}
+	tbf->m_tfi = rc;
+	tbf->trx = &bts->trx[trx_no];
+
 	dl_slots = ms->reserved_dl_slots();
 	ul_slots = ms->reserved_ul_slots();
 
@@ -348,7 +377,6 @@
 	ms->set_reserved_slots(tbf->trx, 1 << ts, 1 << ts);
 
 	tbf->upgrade_to_multislot = 0;
-
 	return 0;
 }
 
@@ -669,7 +697,7 @@
  */
 int alloc_algorithm_b(struct gprs_rlcmac_bts *bts,
 	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)
 {
 	uint8_t dl_slots = 0;
 	uint8_t ul_slots = 0;
@@ -679,6 +707,15 @@
 	char slot_info[9] = {0};
 	int ts;
 	int rc;
+	int trx_no;
+
+	rc = tfi_find_free(bts->bts, ms, tbf->direction, use_trx, &trx_no);
+	if (rc < 0) {
+		LOGP(DRLCMAC, LOGL_NOTICE, "- Failed to allocate a TFI\n");
+		return rc;
+	}
+	tbf->m_tfi = rc;
+	tbf->trx = &bts->trx[trx_no];
 
 	if (!ms) {
 		LOGP(DRLCMAC, LOGL_ERROR, "MS not set\n");