aboutsummaryrefslogtreecommitdiffstats
path: root/src/ericsson-rbs/er_ccu_if.c
blob: 98abbf7da64b1c84d01ae7cfa6566950304082eb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
/*
 * (C) 2022 by sysmocom s.f.m.c. GmbH <info@sysmocom.de>
 * All Rights Reserved
 *
 * Author: Philipp Maier
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include <er_ccu_if.h>
#include <er_ccu_descr.h>
#include <string.h>
#include <errno.h>

#include <osmocom/abis/e1_input.h>
#include <osmocom/abis/abis.h>
#include <osmocom/trau/trau_sync.h>
#include <osmocom/trau/trau_pcu_ericsson.h>
#include <bts.h>
#include <gprs_debug.h>
#include <pcu_l1_if.h>

#define E1_TS_BYTES 160
#define DEBUG_BITS_MAX 1280
#define DEBUG_BYTES_MAX 40

#define LOGPCCU(ccu_descr, level, tag, fmt, args...) \
	LOGP(DE1, level, "E1TS(%u:%u:%u) %s:" fmt, \
	     ccu_descr->e1_conn_pars->e1_nr, ccu_descr->e1_conn_pars->e1_ts, \
	     ccu_descr->e1_conn_pars->e1_ts_ss == E1_SUBSLOT_FULL ? 0 : ccu_descr->e1_conn_pars->e1_ts_ss, tag, \
	     ## args)

struct e1_ts_descr {
	uint8_t usecount;
	bool i460_ts_initialized;
	struct osmo_i460_timeslot i460_ts;
};

struct e1_line_descr {
	struct e1_ts_descr e1_ts[NUM_E1_TS - 1];
};

static struct e1_line_descr e1_lines[32];
static void *tall_ccu_ctx = NULL;

static const struct e1inp_line_ops dummy_e1_line_ops = {
	.sign_link_up = NULL,
	.sign_link_down = NULL,
	.sign_link = NULL,
};

/* called by trau frame synchronizer: feed received MAC blocks into PCU */
static void sync_frame_out_cb(void *user_data, const ubit_t *bits, unsigned int num_bits)
{
	struct er_ccu_descr *ccu_descr = user_data;

	if (!bits || num_bits == 0)
		return;

	LOGPCCU(ccu_descr, LOGL_DEBUG, "I.460-RX", "receiving %u TRAU frame bits from subslot (synchronized): %s...\n",
		num_bits, osmo_ubit_dump(bits, num_bits > DEBUG_BITS_MAX ? DEBUG_BITS_MAX : num_bits));

	ccu_descr->er_ccu_rx_cb(ccu_descr, bits, num_bits);
}

/* called by I.460 de-multiplexer: feed output of I.460 demux into TRAU frame sync */
static void e1_i460_demux_bits_cb(struct osmo_i460_subchan *schan, void *user_data, const ubit_t *bits,
				  unsigned int num_bits)
{
	struct er_ccu_descr *ccu_descr = user_data;

	LOGPCCU(ccu_descr, LOGL_DEBUG, "I.460-RX", "receiving %u TRAU frame bits from subslot: %s...\n", num_bits,
		osmo_ubit_dump(bits, num_bits > DEBUG_BITS_MAX ? DEBUG_BITS_MAX : num_bits));

	OSMO_ASSERT(ccu_descr->link.trau_sync_fi);
	osmo_trau_sync_rx_ubits(ccu_descr->link.trau_sync_fi, bits, num_bits);

}

/* called by I.460 de-multiplexer: ensure that sync indications are sent when mux buffer runs empty */
static void e1_i460_mux_empty_cb(struct osmo_i460_subchan *schan2, void *user_data)
{
	struct er_ccu_descr *ccu_descr = user_data;

	LOGPCCU(ccu_descr, LOGL_DEBUG, "I.460-TX", "demux buffer empty\n");
	ccu_descr->er_ccu_empty_cb(ccu_descr);
}

/* handle outgoing E1 traffic */
static void e1_send_ts_frame(struct e1inp_ts *ts)
{
	void *ctx = tall_ccu_ctx;
	struct e1_ts_descr *ts_descr;
	struct msgb *msg;
	uint8_t *ptr;

	/* The line number and ts number that arrives here should be clean. */
	OSMO_ASSERT(ts->line->num < ARRAY_SIZE(e1_lines));

	ts_descr = &e1_lines[ts->line->num].e1_ts[ts->num];

	/* Do not send anything in case the E1 timeslot is not ready. */
	if (ts_descr->usecount == 0)
		return;

	/* Get E1 frame from I.460 multiplexer */
	msg = msgb_alloc_c(ctx, E1_TS_BYTES, "E1-TX-timeslot-bytes");
	ptr = msgb_put(msg, E1_TS_BYTES);
	osmo_i460_mux_out(&ts_descr->i460_ts, ptr, E1_TS_BYTES);

	LOGPITS(ts, DE1, LOGL_DEBUG, "E1-TX: sending %u bytes: %s...\n",
	     msgb_length(msg), osmo_hexdump_nospc(msgb_data(msg),
						  msgb_length(msg) >
						  DEBUG_BYTES_MAX ? DEBUG_BYTES_MAX : msgb_length(msg)));

	/* Hand data over to the E1 stack */
	msgb_enqueue(&ts->raw.tx_queue, msg);
}

/* Callback function to handle incoming E1 traffic */
static void e1_recv_cb(struct e1inp_ts *ts, struct msgb *msg)
{
	struct e1_ts_descr *ts_descr;

	if (msg->len != E1_TS_BYTES) {
		LOGPITS(ts, DE1, LOGL_ERROR,
		     "E1-RX: receiving bad, expected length is %u, actual length is %u!\n",
		     E1_TS_BYTES, msg->len);
		msgb_free(msg);
		return;
	}

	LOGPITS(ts, DE1, LOGL_DEBUG, "E1-RX: receiving %u bytes: %s ...\n",
		msg->len, osmo_hexdump_nospc(msg->data, msg->len));

	/* Note: The line number and ts number that arrives here should be clean. */
	OSMO_ASSERT(ts->line->num < ARRAY_SIZE(e1_lines));
	ts_descr = &e1_lines[ts->line->num].e1_ts[ts->num];

	/* Hand data over to the I640 demultiplexer. */
	osmo_i460_demux_in(&ts_descr->i460_ts, msg->data, msg->len);

	/* Trigger sending of pending E1 traffic */
	e1_send_ts_frame(ts);

	/* e1inp_rx_ts(), the caller of this callback does not free() msgb. */
	msgb_free(msg);
}

static struct e1_ts_descr *ts_descr_from_ccu_descr(struct er_ccu_descr *ccu_descr)
{
	/* Make sure E1 line number is valid */
	if (ccu_descr->e1_conn_pars->e1_nr >= ARRAY_SIZE(e1_lines)) {
		LOGPCCU(ccu_descr, LOGL_ERROR, "SETUP", "Invalid E1 line number!\n");
		return NULL;
	}

	/* Make sure E1 timeslot number is valid */
	if (ccu_descr->e1_conn_pars->e1_ts < 1 || ccu_descr->e1_conn_pars->e1_ts > NUM_E1_TS - 1) {
		LOGPCCU(ccu_descr, LOGL_ERROR, "SETUP", "Invalid E1 timeslot number!\n");
		return NULL;
	}

	/* Timeslots are only initialized once and will stay open after that. */
	return &e1_lines[ccu_descr->e1_conn_pars->e1_nr].e1_ts[ccu_descr->e1_conn_pars->e1_ts];
}

/* Configure an I.460 subslot and add it to the CCU descriptor */
static int add_i460_subslot(void *ctx, struct er_ccu_descr *ccu_descr)
{
	struct e1_ts_descr *ts_descr;
	enum osmo_tray_sync_pat_id sync_pattern;

	if (ccu_descr->link.schan) {
		/* NOTE: This is a serious error: subslots should be removed when l1if_close_trx() is called by the
		 * PCU. This log line points towards a problem with the PDCH management inside the PCU! */
		LOGPCCU(ccu_descr, LOGL_ERROR, "SETUP", "I.460 subslot is already configured -- will not touch it!\n");
		return -EINVAL;
	}

	ts_descr = ts_descr_from_ccu_descr(ccu_descr);
	if (!ts_descr)
		return -EINVAL;
	if (ts_descr->usecount == 0) {
		LOGPCCU(ccu_descr, LOGL_ERROR, "SETUP", "E1 timeslot not ready!\n");
		return -EINVAL;
	}

	/* Set up I.460 subchannel and connect it to the MUX on the E1 timeslot */
	if (ccu_descr->e1_conn_pars->e1_ts_ss == E1_SUBSLOT_FULL) {
		LOGPCCU(ccu_descr, LOGL_INFO, "SETUP", "using 64k subslots\n");
		ccu_descr->link.scd.rate = OSMO_I460_RATE_64k;
		ccu_descr->link.scd.demux.num_bits = E1_TS_BYTES * 8;
		ccu_descr->link.scd.bit_offset = 0;
		sync_pattern = OSMO_TRAU_SYNCP_64_ER_CCU;
	} else {
		LOGPCCU(ccu_descr, LOGL_INFO, "SETUP", "using 16k subslots\n");
		if (ccu_descr->e1_conn_pars->e1_ts_ss > 3) {
			LOGPCCU(ccu_descr, LOGL_ERROR, "SETUP", "Invalid I.460 subslot number!\n");
			return -EINVAL;
		}
		ccu_descr->link.scd.rate = OSMO_I460_RATE_16k;
		ccu_descr->link.scd.demux.num_bits = E1_TS_BYTES / 4 * 8;
		ccu_descr->link.scd.bit_offset = ccu_descr->e1_conn_pars->e1_ts_ss * 2;
		sync_pattern = OSMO_TRAU_SYNCP_16_ER_CCU;
	}

	ccu_descr->link.scd.demux.out_cb_bits = e1_i460_demux_bits_cb;
	ccu_descr->link.scd.demux.out_cb_bytes = NULL;
	ccu_descr->link.scd.demux.user_data = ccu_descr;
	ccu_descr->link.scd.mux.in_cb_queue_empty = e1_i460_mux_empty_cb;
	ccu_descr->link.scd.mux.user_data = ccu_descr;

	LOGPCCU(ccu_descr, LOGL_INFO, "SETUP", "adding I.460 subchannel: bit_offset=%u, num_bits=%zu\n",
		ccu_descr->link.scd.bit_offset, ccu_descr->link.scd.demux.num_bits);
	ccu_descr->link.schan = osmo_i460_subchan_add(ctx, &ts_descr->i460_ts, &ccu_descr->link.scd);
	if (!ccu_descr->link.schan) {
		LOGPCCU(ccu_descr, LOGL_ERROR, "SETUP", "adding I.460 subchannel: failed!\n");
		return -EINVAL;
	}

	/* Configure TRAU synchronizer */
	ccu_descr->link.trau_sync_fi = osmo_trau_sync_alloc(tall_ccu_ctx, "trau-sync", sync_frame_out_cb, sync_pattern, ccu_descr);
	if (!ccu_descr->link.trau_sync_fi) {
		LOGPCCU(ccu_descr, LOGL_ERROR, "SETUP", "adding I.460 TRAU frame sync: failed!\n");
		return -EINVAL;
	}

	/* Ericsson uses a different synchronization pattern for MCS9 TRAU frames */
	if (sync_pattern == OSMO_TRAU_SYNCP_64_ER_CCU)
		osmo_trau_sync_set_secondary_pat(ccu_descr->link.trau_sync_fi, OSMO_TRAU_SYNCP_64_ER_CCU_MCS9, 1);

	return 0;
}

/* Remove an I.460 subslot from the CCU descriptor */
static void del_i460_subslot(struct er_ccu_descr *ccu_descr)
{
	if (ccu_descr->link.schan)
		osmo_i460_subchan_del(ccu_descr->link.schan);
	ccu_descr->link.schan = NULL;
	if (ccu_descr->link.trau_sync_fi)
		osmo_fsm_inst_term(ccu_descr->link.trau_sync_fi, OSMO_FSM_TERM_REGULAR, NULL);
	ccu_descr->link.trau_sync_fi = NULL;

	memset(&ccu_descr->link.scd, 0, sizeof(ccu_descr->link.scd));
}

/* Configure an E1 timeslot according to the description in the ccu_descr */
static int open_e1_timeslot(struct er_ccu_descr *ccu_descr)
{
	struct e1inp_line *e1_line;
	struct e1_ts_descr *ts_descr;
	int rc;

	/* Find timeslot descriptor and check if the timeslot is already open. */
	ts_descr = ts_descr_from_ccu_descr(ccu_descr);
	if (!ts_descr)
		return -EINVAL;
	if (ts_descr->usecount > 0) {
		LOGPCCU(ccu_descr, LOGL_DEBUG, "SETUP", "E1 timeslot already open -- using it as it is!\n");
		ts_descr->usecount++;
		return 0;
	}

	/* Find and set up E1 line */
	e1_line = e1inp_line_find(ccu_descr->e1_conn_pars->e1_nr);
	if (!e1_line) {
		LOGPCCU(ccu_descr, LOGL_ERROR, "SETUP", "no such E1 line!\n");
		return -EINVAL;
	}
	e1inp_line_bind_ops(e1_line, &dummy_e1_line_ops);

	/* Set up E1 timeslot */
	rc = e1inp_ts_config_raw(&e1_line->ts[ccu_descr->e1_conn_pars->e1_ts - 1], e1_line, e1_recv_cb);
	if (rc < 0) {
		LOGPCCU(ccu_descr, LOGL_ERROR, "SETUP", "configuration of timeslot failed!\n");
		return -EINVAL;
	}
	rc = e1inp_line_update(e1_line);
	if (rc < 0) {
		LOGPCCU(ccu_descr, LOGL_ERROR, "SETUP", "line update failed!\n");
		return -EINVAL;
	}

	/* Make sure the i460 mux is ready */
	if (!ts_descr->i460_ts_initialized) {
		osmo_i460_ts_init(&ts_descr->i460_ts);
		ts_descr->i460_ts_initialized = true;
	}

	ts_descr->usecount++;
	OSMO_ASSERT(ts_descr->usecount == 1);

	return 0;
}

/* Configure an E1 timeslot according to the description in the ccu_descr */
static int close_e1_timeslot(struct er_ccu_descr *ccu_descr)
{
	struct e1inp_line *e1_line;
	struct e1_ts_descr *ts_descr;
	int rc;

	/* Find timeslot descriptor and check if the timeslot is still used by another subslot. */
	ts_descr = ts_descr_from_ccu_descr(ccu_descr);
	if (!ts_descr)
		return -EINVAL;
	if (ts_descr->usecount > 1) {
		LOGPCCU(ccu_descr, LOGL_DEBUG, "SETUP",
			"E1 timeslot still in used by another subslot, leaving it open!\n");
		ts_descr->usecount--;
		return 0;
	} else if (ts_descr->usecount == 0) {
		/* This should not be as it means we close the timeslot too often. */
		LOGPCCU(ccu_descr, LOGL_ERROR, "SETUP", "E1 timeslot already closed, leaving it as it is...\n");
		return -EINVAL;
	}

	/* Find E1 line */
	e1_line = e1inp_line_find(ccu_descr->e1_conn_pars->e1_nr);
	if (!e1_line) {
		LOGPCCU(ccu_descr, LOGL_ERROR, "SETUP", "no such E1 line!\n");
		return -EINVAL;
	}

	/* Release E1 timeslot */
	rc = e1inp_ts_config_none(&e1_line->ts[ccu_descr->e1_conn_pars->e1_ts - 1], e1_line);
	if (rc < 0) {
		LOGPCCU(ccu_descr, LOGL_ERROR, "SETUP", "failed to disable E1 timeslot!\n");
		return -EINVAL;
	}
	rc = e1inp_line_update(e1_line);
	if (rc < 0) {
		LOGPCCU(ccu_descr, LOGL_ERROR, "SETUP", "failed to update E1 line!\n");
		return -EINVAL;
	}

	ts_descr->usecount--;
	OSMO_ASSERT(ts_descr->usecount == 0);

	return 0;
}

int er_ccu_if_open(struct er_ccu_descr *ccu_descr)
{
	if (ccu_descr->link.ccu_connected) {
		LOGPCCU(ccu_descr, LOGL_ERROR, "SETUP",
			"cannot connect CCU since it is already connected -- ignored!\n");
		return 0;
	}

	if (open_e1_timeslot(ccu_descr) < 0)
		return -EINVAL;

	if (add_i460_subslot(tall_ccu_ctx, ccu_descr) < 0)
		return -EINVAL;

	ccu_descr->link.ccu_connected = true;
	LOGPCCU(ccu_descr, LOGL_DEBUG, "SETUP", "CCU connected.\n");
	return 0;
}

void er_ccu_if_close(struct er_ccu_descr *ccu_descr)
{
	if (!ccu_descr->link.ccu_connected) {
		LOGPCCU(ccu_descr, LOGL_ERROR, "SETUP",
			"cannot disconnect CCU since it is already disconnected -- ignored!\n");
		return;
	}

	del_i460_subslot(ccu_descr);
	close_e1_timeslot(ccu_descr);

	ccu_descr->link.ccu_connected = false;
	LOGPCCU(ccu_descr, LOGL_DEBUG, "SETUP", "CCU disconnected.\n");
}

void er_ccu_if_tx(struct er_ccu_descr *ccu_descr, const ubit_t *bits, unsigned int num_bits)
{
	struct msgb *msg;
	uint8_t *ptr;

	if (!ccu_descr->link.ccu_connected) {
		LOGPCCU(ccu_descr, LOGL_ERROR, "SETUP", "cannot TX block, CCU is disconnected -- ignored!\n");
		return;
	}

	msg = msgb_alloc_c(tall_ccu_ctx, num_bits, "E1-I.460-PCU-IND-frame");
	ptr = msgb_put(msg, num_bits);
	memcpy(ptr, bits, num_bits);
	LOGPCCU(ccu_descr, LOGL_DEBUG, "I.460-TX", "sending %u bits: %s...\n", msgb_length(msg),
		osmo_ubit_dump(msgb_data(msg), msgb_length(msg) > DEBUG_BITS_MAX ? DEBUG_BITS_MAX : msgb_length(msg)));
	osmo_i460_mux_enqueue(ccu_descr->link.schan, msg);
}

void er_ccu_if_init(void *ctx)
{
	libosmo_abis_init(ctx);
	e1inp_vty_init();

	tall_ccu_ctx = talloc_new(ctx);
	memset(e1_lines, 0, sizeof(e1_lines));
}