large refactoring: use FSMs for lchans; add inter-BSC HO

Add FSMs:

- timeslot_fsm: handle dynamic timeslots and OML+RSL availability.
- lchan_fsm: handle an individual lchan activation, RTP stream and release,
  signal the appropriate calling FSMs on success, failure, release.
- mgw_endpoint_fsm: handle one entire endpoint with several CI.
- assignment_fsm: BSSMAP Assignment Request.
- handover_fsm: all of intra, inter-MO and inter-MT handover.

Above FSMs absorb large parts of the gscon FSM. The gscon FSM was surpassing
the maximum amount events (32), and it is more logical to treat assignment,
handover and MGW procedures in separate FSMs.

- Add logging macros for each FSM type:
  - LOG_TS()
  - LOG_LCHAN()
  - LOG_MGWEP(), LOG_CI()
  - LOG_ASSIGNMENT()
  - LOG_HO()
  These log with the osmo_fsm_inst where present.
  New style decision: logging without a final newline char is awkward,
  especially for gsmtap logging and when other logs interleave LOGPC() calls;
  we have various cases where the final \n goes missing, and also this invokes
  the log category checking N times instead of once.
  So I decided to make these macros *always* append a newline, but only if
  there is no final newline yet. I hope that the compiler optimizes the
  strlen() of the constant format strings away. Thus I can log with or without
  typing "\n" and always get an \n termination anyway.

General:

- replace osmo_timers, state enums and program-wide osmo_signal_dispatch()
  with dedicated FSM timeouts, states and events.

- introduce a common way to handle Tnnn timers: gsm_timers.h/.c: struct T_def.
  These can be used (with some macro magic) to define a state's timeout once,
  and not make mistakes for each osmo_fsm_inst_state_chg().

Details:

bsc_subscr_conn_fsm.c:

- move most states of this FSM to lchan_fsm, assignment_fsm, handover_fsm and
  mgw_endpoint_fsm.

- There is exactly one state for an ongoing Assignment, with all details
  handled in conn->assignment.fi. The state relies on the assignment_fsm's
  timeout.

- There is one state for an ongoing Handover; except for an incoming Handover
  from a remote BSS, the gscon remains in ST_INIT until the new lchan and conn
  are both established.

- move bssmap_add_lcls_status() to osmo_bsc_lcls.c

abis_rsl.c:

- move all dynamic timeslot logic away into timeslot_fsm. Only keep plain send/receive functions in
  abis_rsl.c

- reduce some rsl functions to merely send a message, rename to "_tx_".
  - rsl_ipacc_mdcx(): add '_tx_' in the name; move parts that change the lchan state out into the
    lchan_fsm, the lchan->abis_ip.* are now set there prior to invoking this function.

- move all timers and error/release handling away into various FSMs.

- tweak ipa_smod_s_for_lchan() and ipa_rtp_pt_for_lchan() to not require an
  lchan passed, but just mode,type that they require. Rename to
  ipacc_speech_mode*() and ipacc_payload_type().

- add rsl_forward_layer3_info, used for inter-BSC HO MO, to just send the RR
  message received during BSSMAP Handover Command.

- move various logging to LOG_LCHAN() in order to log with the lchan FSM instance.
  One drawback is that the lchan FSM is limited to one logging category, i.e. this moves some logging
  from DRR to DRSL. It might actually make sense to combine those categories.

- lose LOGP...LOGPC logging cascades: they are bad for gsmtap logging and for performance.

- handle_classmark_chg(): change logging, move cm2 len check out of the cm3 condition (I hope that's
  correct).

- gsm48_send_ho_cmd(): split off gsm48_make_ho_cmd() which doesn't send right away, so that during
  inter-bsc HO we can make an RR Handover Command to send via the MSC to the remote BSS.

assignment_fsm.c:

- the Chan Mode Modify in case of re-using the same lchan is not implemented
  yet, because this was also missing in the previous implementation (OS#3357).

osmo_bsc_api.c:

- simplify bsc_mr_config() and move to lchan_fsm.c, the only caller; rename to
  lchan_mr_config(). (bsc_mr_config() used to copy the values to mr_bts_lv
  twice, once by member assignment and then again with a memcpy.)

- During handover, we used to copy the MR config from the old lchan. Since we
  may handover between FR and HR, rather set the MR Config anew every time, so
  that FR rates are always available on FR lchans, and never on HR lchans.

Depends: I03ee7ce840ecfa0b6a33358e7385528aabd4873f (libosmocore),
         I1f2918418c38918c5ac70acaa51a47adfca12b5e (libosmocore)
Change-Id: I82e3f918295daa83274a4cf803f046979f284366
diff --git a/include/osmocom/bsc/handover.h b/include/osmocom/bsc/handover.h
index aa117cc..322913d 100644
--- a/include/osmocom/bsc/handover.h
+++ b/include/osmocom/bsc/handover.h
@@ -5,72 +5,38 @@
 #include <osmocom/core/linuxlist.h>
 #include <osmocom/core/timer.h>
 #include <osmocom/gsm/gsm_utils.h>
+#include <osmocom/gsm/gsm0808.h>
 
 #include <osmocom/bsc/neighbor_ident.h>
+#include <osmocom/bsc/gsm_data.h>
 
+struct gsm_network;
 struct gsm_lchan;
 struct gsm_bts;
 struct gsm_subscriber_connection;
 struct gsm_meas_rep mr;
 
-#define LOGPHOLCHANTOLCHAN(old_lchan, new_lchan, level, fmt, args...) \
-	LOGP(DHODEC, level, "(BTS %u trx %u arfcn %u ts %u lchan %u %s %s)->(BTS %u trx %u arfcn %u ts %u lchan %u %s) (subscr %s) " fmt, \
-	     old_lchan->ts->trx->bts->nr, \
-	     old_lchan->ts->trx->nr, \
-	     old_lchan->ts->trx->arfcn, \
-	     old_lchan->ts->nr, \
-	     old_lchan->nr, \
-	     gsm_lchant_name(old_lchan->type), \
-	     gsm48_chan_mode_name(old_lchan->tch_mode), \
-	     new_lchan->ts->trx->bts->nr, \
-	     new_lchan->ts->trx->nr, \
-	     new_lchan->ts->trx->arfcn, \
-	     new_lchan->ts->nr, \
-	     new_lchan->nr, \
-	     gsm_pchan_name(new_lchan->ts->pchan), \
-	     bsc_subscr_name(old_lchan->conn? old_lchan->conn->bsub : NULL), \
-	     ## args)
-
-#define LOGPHO(struct_bsc_handover, level, fmt, args ...) \
-	LOGPHOLCHANTOLCHAN(struct_bsc_handover->old_lchan, struct_bsc_handover->new_lchan, level, fmt, ## args)
-
-enum hodec_id {
-	HODEC_NONE,
-	HODEC1 = 1,
-	HODEC2 = 2,
+enum handover_result {
+	HO_RESULT_OK,
+	HO_RESULT_FAIL_NO_CHANNEL,
+	HO_RESULT_FAIL_RR_HO_FAIL,
+	HO_RESULT_FAIL_TIMEOUT,
+	HO_RESULT_CONN_RELEASE,
+	HO_RESULT_ERROR,
 };
 
-struct bsc_handover {
-	struct llist_head list;
+extern const struct value_string handover_result_names[];
+inline static const char *handover_result_name(enum handover_result val)
+{ return get_value_string(handover_result_names, val); }
 
-	/* Initial details of what is requested */
-	struct gsm_lchan *old_lchan;
-	struct gsm_bts *new_bts;
-	enum gsm_chan_t new_lchan_type;
-	bool async;
-
-	/* Derived and resulting state */
-	bool inter_cell;
-	uint8_t ho_ref;
-	enum hodec_id from_hodec_id;
-	struct gsm_lchan *new_lchan;
-	struct osmo_timer_list T3103;
-};
-
-int bsc_handover_start(enum hodec_id from_hodec_id, struct gsm_lchan *old_lchan, struct gsm_bts *new_bts,
-		       enum gsm_chan_t new_lchan_type);
-int bsc_handover_start_gscon(struct gsm_subscriber_connection *conn);
-void bsc_clear_handover(struct gsm_subscriber_connection *conn, int free_lchan);
-struct gsm_lchan *bsc_handover_pending(struct gsm_lchan *new_lchan);
-
-int bsc_ho_count(struct gsm_bts *bts, bool inter_cell);
+int bts_handover_count(struct gsm_bts *bts, int ho_scopes);
 
 /* Handover decision algorithms' actions to take on incoming handover-relevant events.
  *
  * All events that are interesting for handover decision are actually communicated by S_LCHAN_* signals,
  * so theoretically, each handover algorithm could evaluate those.  However, handover_logic.c cleans up
  * handover operation state upon receiving some of these signals. To allow a handover decision algorithm
- * to take advantage of e.g. the struct bsc_handover before it is discarded, the handover decision event
+ * to take advantage of e.g. the struct handover before it is discarded, the handover decision event
  * handler needs to be invoked before handover_logic.c discards the state. For example, if the handover
  * decision wants to place a penalty timer upon a handover failure, it still needs to know which target
  * cell the handover failed for; handover_logic.c erases that knowledge on handover failure, since it
@@ -90,13 +56,26 @@
 	int hodec_id;
 
 	void (*on_measurement_report)(struct gsm_meas_rep *mr);
-	void (*on_ho_chan_activ_nack)(struct bsc_handover *ho);
-	void (*on_ho_failure)(struct bsc_handover *ho);
+	void (*on_handover_end)(struct gsm_subscriber_connection *conn, enum handover_result result);
 };
 
 void handover_decision_callbacks_register(struct handover_decision_callbacks *hdc);
 struct handover_decision_callbacks *handover_decision_callbacks_get(int hodec_id);
 
+int bsc_tx_bssmap_ho_required(struct gsm_lchan *lchan, const struct gsm0808_cell_id_list2 *target_cells);
+int bsc_tx_bssmap_ho_request_ack(struct gsm_subscriber_connection *conn,
+				 struct msgb *rr_ho_command);
+int bsc_tx_bssmap_ho_detect(struct gsm_subscriber_connection *conn);
+enum handover_result bsc_tx_bssmap_ho_complete(struct gsm_subscriber_connection *conn,
+					       struct gsm_lchan *lchan);
+void bsc_tx_bssmap_ho_failure(struct gsm_subscriber_connection *conn);
+
 struct gsm_bts *bts_by_neighbor_ident(const struct gsm_network *net,
 				      const struct neighbor_ident_key *search_for);
 struct neighbor_ident_key *bts_ident_key(const struct gsm_bts *bts);
+
+void handover_parse_inter_bsc_mt(struct gsm_subscriber_connection *conn,
+				 struct msgb *ho_request_msg);
+
+void handover_mt_allocate_lchan(struct handover *ho);
+int handover_mt_send_rr_ho_command(struct handover *ho);