Philipp Maier | 4bac398 | 2023-02-03 16:52:42 +0100 | [diff] [blame] | 1 | /* |
| 2 | * (C) 2022 by sysmocom s.f.m.c. GmbH <info@sysmocom.de> |
| 3 | * All Rights Reserved |
| 4 | * |
| 5 | * Author: Philipp Maier |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU Affero General Public License as published by |
| 9 | * the Free Software Foundation; either version 3 of the License, or |
| 10 | * (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 Affero General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU Affero General Public License |
| 18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | #include <er_ccu_descr.h> |
| 23 | #include <er_ccu_if.h> |
| 24 | |
| 25 | #include <string.h> |
| 26 | #include <errno.h> |
| 27 | |
| 28 | #include <osmocom/pcu/pcuif_proto.h> |
| 29 | #include <osmocom/abis/e1_input.h> |
| 30 | #include <osmocom/abis/abis.h> |
| 31 | #include <osmocom/abis/e1_input.h> |
| 32 | #include <osmocom/trau/trau_sync.h> |
| 33 | #include <osmocom/trau/trau_pcu_ericsson.h> |
| 34 | #include <osmocom/gsm/gsm0502.h> |
| 35 | #include <osmocom/core/talloc.h> |
| 36 | |
| 37 | #include <bts.h> |
| 38 | #include <pcu_l1_if.h> |
| 39 | #include <pcu_l1_if_phy.h> |
| 40 | |
| 41 | extern void *tall_pcu_ctx; |
| 42 | |
| 43 | const uint8_t fn_inc_table[4] = { 4, 4, 5, 0 }; |
| 44 | const uint8_t blk_nr_table[4] = { 4, 4, 5, 0 }; |
| 45 | |
| 46 | #define SYNC_CHECK_INTERVAL GSM_TDMA_SUPERFRAME * 8 |
| 47 | |
| 48 | /* Subtrahend to convert Ericsson adjusted (block ending) fn to regular fn (uplink only) */ |
| 49 | #define AFN_SUBTRAHEND 3 |
| 50 | |
| 51 | #define LOGPL1IF(ccu_descr, level, tag, fmt, args...) \ |
| 52 | LOGP(DL1IF, level, "%s: PDCH(trx=%u,ts=%u) E1-line(line=%u,ts=%u,ss=%u) " fmt, \ |
| 53 | tag, ccu_descr->pcu.trx_no, ccu_descr->pcu.ts, \ |
| 54 | ccu_descr->e1_conn_pars->e1_nr, ccu_descr->e1_conn_pars->e1_ts, \ |
| 55 | ccu_descr->e1_conn_pars->e1_ts_ss == E1_SUBSLOT_FULL ? 0 : ccu_descr->e1_conn_pars->e1_ts_ss, \ |
| 56 | ## args) |
| 57 | |
| 58 | /* Calculate GPRS block number from frame number */ |
| 59 | static uint8_t fn_to_block_nr(uint32_t fn) |
| 60 | { |
| 61 | /* Note: See also 3GPP TS 03.64 6.5.7.2.1, |
| 62 | * Mapping on the multiframe structure */ |
| 63 | |
| 64 | uint8_t rel_fn; |
| 65 | uint8_t super_block; |
| 66 | uint8_t local_block; |
| 67 | |
| 68 | rel_fn = fn % 52; |
| 69 | |
| 70 | /* Warn in case of frames that do not belong to a block */ |
| 71 | if (rel_fn == 12 || rel_fn == 25 || rel_fn == 38 || rel_fn == 51) |
| 72 | LOGP(DL1IF, LOGL_ERROR, "Frame number is referencing invalid block!\n"); |
| 73 | |
| 74 | super_block = (rel_fn / 13); |
| 75 | local_block = rel_fn % 13 / 4; |
| 76 | return super_block * 3 + local_block; |
| 77 | } |
| 78 | |
| 79 | static uint32_t fn_dl_advance(uint32_t fn, uint32_t n_blocks) |
| 80 | { |
| 81 | uint32_t i; |
| 82 | |
| 83 | uint8_t inc_fn; |
| 84 | |
| 85 | for (i = 0; i < n_blocks; i++) { |
| 86 | inc_fn = fn_inc_table[(fn % 13) / 4]; |
| 87 | fn = GSM_TDMA_FN_SUM(fn, inc_fn); |
| 88 | } |
| 89 | |
| 90 | return fn; |
| 91 | } |
| 92 | |
| 93 | static bool mac_block_is_noise(struct er_gprs_trau_frame *trau_frame) |
| 94 | { |
| 95 | switch (trau_frame->u.ccu_data_ind.cs_hdr) { |
| 96 | case CS_OR_HDR_CS1: |
| 97 | case CS_OR_HDR_CS2: |
| 98 | case CS_OR_HDR_CS3: |
| 99 | case CS_OR_HDR_CS4: |
| 100 | if (!trau_frame->u.ccu_data_ind.u.gprs.parity_ok) |
| 101 | return true; |
| 102 | break; |
| 103 | case CS_OR_HDR_HDR1: |
| 104 | case CS_OR_HDR_HDR2: |
| 105 | case CS_OR_HDR_HDR3: |
| 106 | if (!trau_frame->u.ccu_data_ind.u.egprs.hdr_good) |
| 107 | return true; |
| 108 | if (!trau_frame->u.ccu_data_ind.u.egprs.data_good[0] |
| 109 | && !trau_frame->u.ccu_data_ind.u.egprs.data_good[1]) |
| 110 | return true; |
| 111 | break; |
| 112 | case CS_OR_HDR_AB: |
| 113 | /* We are not interested in receiving access bursts. */ |
| 114 | return true; |
| 115 | } |
| 116 | |
| 117 | /* No noise, this block is interesting for us. */ |
| 118 | return false; |
| 119 | } |
| 120 | |
| 121 | static void log_data_ind(struct er_ccu_descr *ccu_descr, struct er_gprs_trau_frame *trau_frame, uint32_t afn_ul_comp, |
| 122 | uint32_t afn_dl_comp) |
| 123 | { |
| 124 | switch (trau_frame->u.ccu_data_ind.cs_hdr) { |
| 125 | case CS_OR_HDR_CS1: |
| 126 | case CS_OR_HDR_CS2: |
| 127 | case CS_OR_HDR_CS3: |
| 128 | case CS_OR_HDR_CS4: |
| 129 | LOGPL1IF(ccu_descr, LOGL_DEBUG, "CCU-DATA-IND", |
| 130 | "tav=%u, dbe=%u, cs_hdr=%u, rx_lev=%u, est_acc_del_dev=%u," |
| 131 | "block_qual=%u, parity_ok=%u, data=%s<==, afn_ul_comp=%u/%u\n", trau_frame->u.ccu_data_ind.tav, |
| 132 | trau_frame->u.ccu_data_ind.dbe, trau_frame->u.ccu_data_ind.cs_hdr, |
| 133 | trau_frame->u.ccu_data_ind.rx_lev, trau_frame->u.ccu_data_ind.est_acc_del_dev, |
| 134 | trau_frame->u.ccu_data_ind.u.gprs.block_qual, trau_frame->u.ccu_data_ind.u.gprs.parity_ok, |
| 135 | osmo_hexdump_nospc(trau_frame->u.ccu_data_ind.data, trau_frame->u.ccu_data_ind.data_len), |
| 136 | afn_ul_comp, afn_ul_comp % 52); |
| 137 | break; |
| 138 | case CS_OR_HDR_HDR1: |
| 139 | case CS_OR_HDR_HDR2: |
| 140 | case CS_OR_HDR_HDR3: |
| 141 | case CS_OR_HDR_AB: |
| 142 | LOGPL1IF(ccu_descr, LOGL_DEBUG, "CCU-DATA-IND", |
| 143 | "tav=%u, dbe=%u, cs_hdr=%u, rx_lev=%u, est_acc_del_dev=%u," |
| 144 | "mean_bep=%u, cv_bep=%u, hdr_good=%u, data_good[0]=%u, data_good[1]=%u, data=%s<==, afn_ul_comp=%u/%u\n", |
| 145 | trau_frame->u.ccu_data_ind.tav, trau_frame->u.ccu_data_ind.dbe, |
| 146 | trau_frame->u.ccu_data_ind.cs_hdr, trau_frame->u.ccu_data_ind.rx_lev, |
| 147 | trau_frame->u.ccu_data_ind.est_acc_del_dev, trau_frame->u.ccu_data_ind.u.egprs.mean_bep, |
| 148 | trau_frame->u.ccu_data_ind.u.egprs.cv_bep, trau_frame->u.ccu_data_ind.u.egprs.hdr_good, |
| 149 | trau_frame->u.ccu_data_ind.u.egprs.data_good[0], |
| 150 | trau_frame->u.ccu_data_ind.u.egprs.data_good[1], |
| 151 | osmo_hexdump_nospc(trau_frame->u.ccu_data_ind.data, trau_frame->u.ccu_data_ind.data_len), |
| 152 | afn_ul_comp, afn_ul_comp % 52); |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | /* Receive block from CCU */ |
| 157 | static void er_ccu_rx_cb(struct er_ccu_descr *ccu_descr, const ubit_t *bits, unsigned int num_bits) |
| 158 | { |
| 159 | int rc; |
| 160 | struct er_gprs_trau_frame trau_frame; |
| 161 | uint8_t inc_ul; |
| 162 | uint8_t inc_dl; |
| 163 | uint32_t afn_ul; |
| 164 | uint32_t afn_dl; |
| 165 | uint32_t afn_ul_comp; |
| 166 | uint32_t afn_dl_comp; |
| 167 | struct pcu_l1_meas meas = { 0 }; |
| 168 | struct gprs_rlcmac_bts *bts; |
| 169 | struct gprs_rlcmac_pdch *pdch; |
| 170 | |
| 171 | /* Compute the current frame numbers from the last frame number */ |
| 172 | inc_ul = fn_inc_table[(ccu_descr->sync.last_afn_ul % 13) / 4]; |
| 173 | inc_dl = fn_inc_table[(ccu_descr->sync.last_afn_dl % 13) / 4]; |
| 174 | afn_ul = GSM_TDMA_FN_SUM(ccu_descr->sync.last_afn_ul, inc_ul); |
| 175 | afn_dl = GSM_TDMA_FN_SUM(ccu_descr->sync.last_afn_dl, inc_dl); |
| 176 | |
| 177 | /* Compute compensated frame numbers. This will be the framenumbers we |
| 178 | * will use to exchange blocks with the PCU code. The following applies: |
| 179 | * |
| 180 | * 1. The uplink related frame numbers sent by the ericsson CCU refer to the end of a block. This is |
| 181 | * compensated by subtracting three frames. |
| 182 | * 2. The CCU downlink frame number runs one block past the uplink frame number. This needs to be |
| 183 | * compesated as well (+1). |
| 184 | * 3. The difference between the local (PCU) and the returned (CCU) pseq counter value is the number of blocks |
| 185 | * that the PCU must |
| 186 | * shift its downlink alignment in order to compensate the link latency between PCU and CCU. */ |
| 187 | afn_ul_comp = GSM_TDMA_FN_SUB(afn_ul, AFN_SUBTRAHEND); |
| 188 | afn_dl_comp = afn_dl; |
| 189 | afn_dl_comp = fn_dl_advance(afn_dl_comp, GSM_TDMA_FN_DIFF(ccu_descr->sync.pseq_pcu, ccu_descr->sync.pseq_ccu) + 1); |
| 190 | |
| 191 | LOGPL1IF(ccu_descr, LOGL_DEBUG, "CCU-SYNC", |
| 192 | "afn_ul=%u/%u, afn_dl=%u/%u, afn_diff=%u => afn_ul_comp=%u/%u, afn_dl_comp=%u/%u, afn_diff_comp=%u\n", |
| 193 | afn_ul, afn_ul % 52, afn_dl, afn_dl % 52, GSM_TDMA_FN_DIFF(afn_ul, afn_dl), afn_ul_comp, |
| 194 | afn_ul_comp % 52, afn_dl_comp, afn_dl_comp % 52, GSM_TDMA_FN_DIFF(afn_ul_comp, afn_dl_comp)); |
| 195 | |
| 196 | LOGPL1IF(ccu_descr, LOGL_DEBUG, "CCU-SYNC", "pseq_pcu=%u, pseq_ccu=%u, pseq_diff=%u\n", |
| 197 | ccu_descr->sync.pseq_pcu, ccu_descr->sync.pseq_ccu, GSM_TDMA_FN_DIFF(ccu_descr->sync.pseq_pcu, ccu_descr->sync.pseq_ccu)); |
| 198 | |
| 199 | /* Decode indication from CCU */ |
| 200 | if (ccu_descr->e1_conn_pars->e1_ts_ss == E1_SUBSLOT_FULL) |
| 201 | rc = er_gprs_trau_frame_decode_64k(&trau_frame, bits); |
| 202 | else |
| 203 | rc = er_gprs_trau_frame_decode_16k(&trau_frame, bits); |
| 204 | if (rc < 0) { |
| 205 | LOGPL1IF(ccu_descr, LOGL_ERROR, "CCU-XXXX-IND", |
| 206 | "unable to decode uplink TRAU frame, afn_ul_comp=%u/%u\n", afn_ul_comp, afn_ul_comp % 52); |
| 207 | |
| 208 | /* Report to the CCU that there is an issue with uplink TRAU frames, the CCU will then send |
| 209 | * a CCU-SYNC-IND within the next TRAU frame, so we can check if we are still in sync and trigger |
| 210 | * synchronization procedure if necessary. */ |
| 211 | ccu_descr->sync.ul_frame_err = true; |
| 212 | goto skip; |
| 213 | } |
| 214 | |
| 215 | switch (trau_frame.type) { |
| 216 | case ER_GPRS_TRAU_FT_SYNC: |
| 217 | if (trau_frame.u.ccu_sync_ind.pseq != 0x3FFFFF) { |
| 218 | LOGPL1IF(ccu_descr, LOGL_DEBUG, "CCU-SYNC-IND", |
| 219 | "tav=%u, dbe=%u, dfe=%u, pseq=%u, afn_ul=%u, afn_dl=%u\n", |
| 220 | trau_frame.u.ccu_sync_ind.tav, trau_frame.u.ccu_sync_ind.dbe, |
| 221 | trau_frame.u.ccu_sync_ind.dfe, trau_frame.u.ccu_sync_ind.pseq, |
| 222 | trau_frame.u.ccu_sync_ind.afn_ul, trau_frame.u.ccu_sync_ind.afn_dl); |
| 223 | |
| 224 | /* Synchronize the current CCU PSEQ state */ |
| 225 | ccu_descr->sync.pseq_ccu = trau_frame.u.ccu_sync_ind.pseq; |
| 226 | } else { |
| 227 | LOGPL1IF(ccu_descr, LOGL_DEBUG, "CCU-SYNC-IND", |
| 228 | "tav=%u, dbe=%u, dfe=%u, pseq=(none), afn_ul=%u, afn_dl=%u\n", |
| 229 | trau_frame.u.ccu_sync_ind.tav, trau_frame.u.ccu_sync_ind.dbe, |
| 230 | trau_frame.u.ccu_sync_ind.dfe, trau_frame.u.ccu_sync_ind.afn_ul, |
| 231 | trau_frame.u.ccu_sync_ind.afn_dl); |
| 232 | } |
| 233 | |
| 234 | ccu_descr->sync.tav = trau_frame.u.ccu_sync_ind.tav; |
| 235 | |
| 236 | /* Check if we are in sync with the CCU, if not trigger synchronization procedure */ |
| 237 | if (afn_ul != trau_frame.u.ccu_sync_ind.afn_ul || afn_dl != trau_frame.u.ccu_sync_ind.afn_dl) { |
| 238 | if (afn_ul != trau_frame.u.ccu_sync_ind.afn_ul) |
| 239 | LOGPL1IF(ccu_descr, LOGL_NOTICE, "CCU-SYNC-IND", |
| 240 | "afn_ul=%u (computed) != afn_ul=%u (sync-ind) => delta=%u\n", afn_ul, |
| 241 | trau_frame.u.ccu_sync_ind.afn_ul, |
| 242 | GSM_TDMA_FN_DIFF(afn_ul, trau_frame.u.ccu_sync_ind.afn_ul)); |
| 243 | if (afn_dl != trau_frame.u.ccu_sync_ind.afn_dl) |
| 244 | LOGPL1IF(ccu_descr, LOGL_NOTICE, "CCU-SYNC-IND", |
| 245 | "afn_dl=%u (computed) != afn_dl=%u (sync-ind) => delta=%u\n", afn_dl, |
| 246 | trau_frame.u.ccu_sync_ind.afn_dl, |
| 247 | GSM_TDMA_FN_DIFF(afn_dl, trau_frame.u.ccu_sync_ind.afn_dl)); |
| 248 | LOGPL1IF(ccu_descr, LOGL_NOTICE, "CCU-SYNC-IND", |
| 249 | "FN jump detected, lost sync with CCU -- (re)synchronizing...\n"); |
| 250 | ccu_descr->sync.ccu_synced = false; |
| 251 | } else { |
| 252 | LOGPL1IF(ccu_descr, LOGL_NOTICE, "CCU-SYNC-IND", "in sync with CCU\n"); |
| 253 | ccu_descr->sync.ccu_synced = true; |
| 254 | } |
| 255 | |
| 256 | /* Overwrite calculated afn_ul and afn_dl with the actual values from the SYNC indication */ |
| 257 | afn_ul = trau_frame.u.ccu_sync_ind.afn_ul; |
| 258 | afn_dl = trau_frame.u.ccu_sync_ind.afn_dl; |
| 259 | |
| 260 | break; |
| 261 | case ER_GPRS_TRAU_FT_DATA: |
| 262 | |
| 263 | ccu_descr->sync.tav = trau_frame.u.ccu_data_ind.tav; |
| 264 | |
| 265 | /* Ignore all data indications that contain only noise */ |
| 266 | if (mac_block_is_noise(&trau_frame)) |
| 267 | break; |
| 268 | |
| 269 | log_data_ind(ccu_descr, &trau_frame, afn_ul_comp, afn_dl_comp); |
| 270 | |
| 271 | /* Hand received MAC block into PCU */ |
| 272 | bts = gprs_pcu_get_bts_by_nr(the_pcu, ccu_descr->pcu.bts_nr); |
| 273 | if (!bts) |
| 274 | break; |
| 275 | meas.have_rssi = 1; |
| 276 | meas.rssi = rxlev2dbm(trau_frame.u.ccu_data_ind.rx_lev); |
| 277 | meas.have_link_qual = 1; |
| 278 | meas.link_qual = trau_frame.u.ccu_data_ind.u.gprs.block_qual; |
| 279 | pdch = &bts->trx[ccu_descr->pcu.trx_no].pdch[ccu_descr->pcu.ts]; |
| 280 | rc = pcu_rx_data_ind_pdtch(bts, pdch, trau_frame.u.ccu_data_ind.data, |
| 281 | trau_frame.u.ccu_data_ind.data_len, afn_ul_comp, &meas); |
| 282 | break; |
| 283 | default: |
| 284 | LOGPL1IF(ccu_descr, LOGL_ERROR, "CCU-XXXX-IND", "unhandled CCU indication!\n"); |
| 285 | } |
| 286 | |
| 287 | skip: |
| 288 | if (ccu_descr->sync.ccu_synced) { |
| 289 | bts = gprs_pcu_get_bts_by_nr(the_pcu, ccu_descr->pcu.bts_nr); |
| 290 | if (bts) { |
| 291 | /* The PCU timing is locked to the uplink fame number. The downlink frame number is advanced |
| 292 | * into the future so that the line latency is compensated and the frame arrives at the right |
| 293 | * point in time. */ |
| 294 | pdch = &bts->trx[ccu_descr->pcu.trx_no].pdch[ccu_descr->pcu.ts]; |
| 295 | pcu_rx_block_time(bts, pdch->trx->arfcn, afn_ul_comp, ccu_descr->pcu.ts); |
| 296 | rc = pcu_rx_rts_req_pdtch(bts, ccu_descr->pcu.trx_no, ccu_descr->pcu.ts, afn_dl_comp, |
| 297 | fn_to_block_nr(afn_dl_comp)); |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | /* We do not receive sync indications in every cycle. When traffic is transferred we won't get frame numbers |
| 302 | * from the CCU. In this case we must update the last_afn_ul/dl values from the computed frame numbers |
| 303 | * (see above) */ |
| 304 | ccu_descr->sync.last_afn_ul = afn_ul; |
| 305 | ccu_descr->sync.last_afn_dl = afn_dl; |
| 306 | ccu_descr->sync.pseq_pcu++; |
| 307 | ccu_descr->sync.pseq_ccu++; |
| 308 | } |
| 309 | |
| 310 | static void er_ccu_empty_cb(struct er_ccu_descr *ccu_descr) |
| 311 | { |
| 312 | struct er_gprs_trau_frame trau_frame; |
| 313 | ubit_t trau_frame_encoded[ER_GPRS_TRAU_FRAME_LEN_64K]; |
| 314 | int rc; |
| 315 | |
| 316 | memset(&trau_frame, 0, sizeof(trau_frame)); |
| 317 | trau_frame.u.pcu_sync_ind.pseq = ccu_descr->sync.pseq_pcu; |
| 318 | trau_frame.u.pcu_sync_ind.tav = ccu_descr->sync.tav; |
| 319 | trau_frame.u.pcu_sync_ind.fn_ul = 0x3FFFFF; |
| 320 | trau_frame.u.pcu_sync_ind.fn_dl = 0x3FFFFF; |
| 321 | trau_frame.u.pcu_sync_ind.fn_ss = 0x3FFFFF; |
| 322 | trau_frame.u.pcu_sync_ind.ls = 0x3FFFFF; |
| 323 | trau_frame.u.pcu_sync_ind.ss = 0x3FFFFF; |
| 324 | trau_frame.type = ER_GPRS_TRAU_FT_SYNC; |
| 325 | |
| 326 | if (ccu_descr->e1_conn_pars->e1_ts_ss == E1_SUBSLOT_FULL) |
| 327 | rc = er_gprs_trau_frame_encode_64k(trau_frame_encoded, &trau_frame); |
| 328 | else |
| 329 | rc = er_gprs_trau_frame_encode_16k(trau_frame_encoded, &trau_frame); |
| 330 | if (rc < 0) { |
| 331 | LOGPL1IF(ccu_descr, LOGL_ERROR, "PCU-SYNC-IND", "unable to encode TRAU frame\n"); |
| 332 | return; |
| 333 | } |
| 334 | LOGPL1IF(ccu_descr, LOGL_DEBUG, "PCU-SYNC-IND", "pseq=%u, tav=%u\n", |
| 335 | trau_frame.u.pcu_sync_ind.pseq, trau_frame.u.pcu_sync_ind.tav); |
| 336 | er_ccu_if_tx(ccu_descr, trau_frame_encoded, rc); |
| 337 | |
| 338 | /* Make sure timing adjustment value is reset after use */ |
| 339 | ccu_descr->sync.tav = TIME_ADJ_NONE; |
| 340 | } |
| 341 | |
| 342 | /* use the length of the block to determine the coding scheme */ |
| 343 | static int cs_hdr_from_len(uint8_t len) |
| 344 | { |
| 345 | switch (len) { |
| 346 | case 23: |
| 347 | return CS_OR_HDR_CS1; |
| 348 | case 34: |
| 349 | return CS_OR_HDR_CS2; |
| 350 | case 40: |
| 351 | return CS_OR_HDR_CS3; |
| 352 | case 54: |
| 353 | return CS_OR_HDR_CS4; |
| 354 | case 27: |
| 355 | case 33: |
| 356 | case 42: |
| 357 | case 49: |
| 358 | return CS_OR_HDR_HDR3; |
| 359 | case 60: |
| 360 | case 78: |
| 361 | return CS_OR_HDR_HDR2; |
| 362 | case 118: |
| 363 | case 142: |
| 364 | case 154: |
| 365 | return CS_OR_HDR_HDR1; |
| 366 | default: |
| 367 | return -EINVAL; |
| 368 | } |
| 369 | } |
| 370 | |
| 371 | /* send packet data request to L1 */ |
| 372 | int l1if_pdch_req(void *obj, uint8_t ts, int is_ptcch, uint32_t fn, |
| 373 | uint16_t arfcn, uint8_t block_nr, uint8_t *data, uint8_t len) |
| 374 | { |
| 375 | struct er_ccu_descr *ccu_descr = obj; |
| 376 | struct er_gprs_trau_frame trau_frame; |
| 377 | ubit_t trau_frame_encoded[ER_GPRS_TRAU_FRAME_LEN_64K]; |
| 378 | struct gprs_rlcmac_bts *bts; |
| 379 | int rc; |
| 380 | |
| 381 | /* Make sure that the CCU is synchronized and connected. */ |
| 382 | if (!ccu_descr) { |
| 383 | LOGP(DL1IF, LOGL_ERROR, "PCU-DATA-IND: PDCH(ts=%u, arfcn=%u) no CCU context, tossing MAC block...\n", |
| 384 | ts, arfcn); |
| 385 | return -EINVAL; |
| 386 | } |
| 387 | if (!ccu_descr->link.ccu_connected) { |
| 388 | LOGPL1IF(ccu_descr, LOGL_NOTICE, "PCU-DATA-IND", "CCU not connected, tossing MAC block...\n"); |
| 389 | return -EINVAL; |
| 390 | } |
| 391 | if (!ccu_descr->sync.ccu_synced) { |
| 392 | LOGPL1IF(ccu_descr, LOGL_NOTICE, "PCU-DATA-IND", "CCU not synchronized, tossing MAC block...\n"); |
| 393 | return -EINVAL; |
| 394 | } |
| 395 | |
| 396 | /* Hand received MAC block into PCU */ |
| 397 | bts = gprs_pcu_get_bts_by_nr(the_pcu, ccu_descr->pcu.bts_nr); |
| 398 | if (!bts) { |
| 399 | LOGPL1IF(ccu_descr, LOGL_NOTICE, "PCU-DATA-IND", "no BTS, tossing MAC block...\n"); |
| 400 | return -EINVAL; |
| 401 | } |
| 402 | |
| 403 | memset(&trau_frame, 0, sizeof(trau_frame)); |
| 404 | trau_frame.type = ER_GPRS_TRAU_FT_DATA; |
| 405 | |
| 406 | rc = cs_hdr_from_len(len); |
| 407 | if (rc < 0) { |
| 408 | LOGPL1IF(ccu_descr, LOGL_ERROR, "PCU-DATA-IND", |
| 409 | "unable to encode TRAU frame, invalid CS or MCS value set\n"); |
| 410 | return -EINVAL; |
| 411 | } |
| 412 | trau_frame.u.pcu_data_ind.cs_hdr = (enum er_cs_or_hdr)rc; |
| 413 | trau_frame.u.pcu_data_ind.tav = ccu_descr->sync.tav; |
| 414 | trau_frame.u.pcu_data_ind.ul_frame_err = ccu_descr->sync.ul_frame_err; |
| 415 | if (bts->mcs_mask) |
| 416 | trau_frame.u.pcu_data_ind.ul_chan_mode = ER_UL_CHMOD_NB_UNKN; |
| 417 | else |
| 418 | trau_frame.u.pcu_data_ind.ul_chan_mode = ER_UL_CHMOD_NB_GMSK; |
| 419 | OSMO_ASSERT(len < sizeof(trau_frame.u.pcu_data_ind.data)); |
| 420 | memcpy(trau_frame.u.pcu_data_ind.data, data, len); |
| 421 | |
| 422 | /* Regulary ignore one MAC block in uplink. The CCU will then send one CCU-SYNC-IND instead. We use this |
| 423 | * indication to check whether we are still in sync with the CCU. */ |
| 424 | if (fn % SYNC_CHECK_INTERVAL == 0) |
| 425 | trau_frame.u.pcu_data_ind.ul_chan_mode = ER_UL_CHMOD_VOID; |
| 426 | |
| 427 | if (ccu_descr->e1_conn_pars->e1_ts_ss == E1_SUBSLOT_FULL) |
| 428 | rc = er_gprs_trau_frame_encode_64k(trau_frame_encoded, &trau_frame); |
| 429 | else |
| 430 | rc = er_gprs_trau_frame_encode_16k(trau_frame_encoded, &trau_frame); |
| 431 | if (rc < 0) { |
| 432 | LOGPL1IF(ccu_descr, LOGL_ERROR, "PCU-DATA-IND", "unable to encode TRAU frame\n"); |
| 433 | return -EINVAL; |
| 434 | } |
| 435 | LOGPL1IF(ccu_descr, LOGL_DEBUG, "PCU-DATA-IND", |
| 436 | "tav=%u, ul_frame_err=%u, cs_hdr=%u, ul_chan_mode=%u, atten_db=%u, timing_offset=%u," |
| 437 | " data=%s==>, fn=%u/%u (comp)\n", trau_frame.u.pcu_data_ind.tav, |
| 438 | trau_frame.u.pcu_data_ind.ul_frame_err, trau_frame.u.pcu_data_ind.cs_hdr, |
| 439 | trau_frame.u.pcu_data_ind.ul_chan_mode, trau_frame.u.pcu_data_ind.atten_db, |
| 440 | trau_frame.u.pcu_data_ind.timing_offset, osmo_hexdump_nospc(trau_frame.u.pcu_data_ind.data, len), fn, |
| 441 | fn % 52); |
| 442 | er_ccu_if_tx(ccu_descr, trau_frame_encoded, rc); |
| 443 | |
| 444 | /* Make sure timing adjustment value is reset after use */ |
| 445 | ccu_descr->sync.tav = TIME_ADJ_NONE; |
| 446 | ccu_descr->sync.ul_frame_err = false; |
| 447 | |
| 448 | return 0; |
| 449 | } |
| 450 | |
| 451 | void *l1if_open_pdch(uint8_t bts_nr, uint8_t trx_no, uint32_t hlayer1, struct gsmtap_inst *gsmtap) |
| 452 | { |
| 453 | struct er_ccu_descr *ccu_descr; |
| 454 | |
| 455 | /* Note: We do not have enough information to really open anything at |
| 456 | * this point. We will just create the CCU context. */ |
| 457 | |
| 458 | ccu_descr = talloc_zero(tall_pcu_ctx, struct er_ccu_descr); |
| 459 | OSMO_ASSERT(ccu_descr); |
| 460 | ccu_descr->er_ccu_rx_cb = er_ccu_rx_cb; |
| 461 | ccu_descr->er_ccu_empty_cb = er_ccu_empty_cb; |
| 462 | ccu_descr->pcu.trx_no = trx_no; |
| 463 | ccu_descr->pcu.bts_nr = bts_nr; |
| 464 | |
| 465 | return ccu_descr; |
| 466 | } |
| 467 | |
| 468 | int l1if_close_pdch(void *obj) |
| 469 | { |
| 470 | struct er_ccu_descr *ccu_descr = obj; |
| 471 | |
| 472 | if (!ccu_descr) { |
| 473 | LOGP(DL1IF, LOGL_ERROR, "PCU-DATA-IND: no CCU context, cannot close unknown PDCH...\n"); |
| 474 | return -EINVAL; |
| 475 | } |
| 476 | |
| 477 | er_ccu_if_close(ccu_descr); |
| 478 | talloc_free(ccu_descr); |
| 479 | return 0; |
| 480 | } |
| 481 | |
| 482 | int l1if_connect_pdch(void *obj, uint8_t ts) |
| 483 | { |
| 484 | struct er_ccu_descr *ccu_descr = obj; |
| 485 | int rc; |
| 486 | |
| 487 | if (!ccu_descr) { |
| 488 | LOGP(DL1IF, LOGL_ERROR, "SETUP: PDCH(ts=%u) no CCU context, PDCH never opened before?\n", ts); |
| 489 | return -EINVAL; |
| 490 | } |
| 491 | |
| 492 | ccu_descr->pcu.ts = ts; |
| 493 | |
| 494 | rc = pcu_l1if_get_e1_ccu_conn_pars(&ccu_descr->e1_conn_pars, ccu_descr->pcu.bts_nr, ccu_descr->pcu.trx_no, |
| 495 | ccu_descr->pcu.ts); |
| 496 | if (rc < 0) { |
| 497 | LOGPL1IF(ccu_descr, LOGL_ERROR, "SETUP", "cannot find E1 connection parameters for CCU\n"); |
| 498 | return -EINVAL; |
| 499 | } |
| 500 | |
| 501 | rc = er_ccu_if_open(ccu_descr); |
| 502 | if (rc < 0) |
| 503 | return -EINVAL; |
| 504 | |
| 505 | return 0; |
| 506 | } |
| 507 | |
| 508 | int l1if_disconnect_pdch(void *obj, uint8_t ts) |
| 509 | { |
| 510 | struct er_ccu_descr *ccu_descr = obj; |
| 511 | |
| 512 | if (!ccu_descr) { |
| 513 | LOGP(DL1IF, LOGL_ERROR, "SETUP: PDCH(ts=%u) no CCU context, PDCH never opened before?\n", ts); |
| 514 | return -EINVAL; |
| 515 | } |
| 516 | |
| 517 | er_ccu_if_close(ccu_descr); |
| 518 | |
| 519 | return 0; |
| 520 | } |
| 521 | |
| 522 | int l1if_init(void) |
| 523 | { |
| 524 | er_ccu_if_init(tall_pcu_ctx); |
| 525 | return 0; |
| 526 | } |